commons-math3-3.2-src/checkstyle.xml100644 1750 1750 17437 12126627721 16303 0ustarlucluc 0 0 commons-math3-3.2-src/license-header.txt100644 1750 1750 1443 12126627721 17002 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ commons-math3-3.2-src/test-jar.xml100644 1750 1750 11105 12126627721 15660 0ustarlucluc 0 0 JAVA ENVIRONMENT **************************Java runtime version***************************** ${java.runtime.version} **************************Java class path********************************** ${java.class.path} **************************Java home**************************************** ${java.home} *************************Java library path******************************* Java library path: ${java.library.path} =========================================================================== ================================= WARNING ================================ Junit isn't present in your ${ANT_HOME}/lib directory. Tests not executed. ========================================================================== commons-math3-3.2-src/build.xml100644 1750 1750 33665 12126627721 15245 0ustarlucluc 0 0 commons-math3-3.2-src/LICENSE.txt100644 1750 1750 46750 12126627721 15246 0ustarlucluc 0 0 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. APACHE COMMONS MATH DERIVATIVE WORKS: The Apache commons-math library includes a number of subcomponents whose implementation is derived from original sources written in C or Fortran. License terms of the original sources are reproduced below. =============================================================================== For the lmder, lmpar and qrsolv Fortran routine from minpack and translated in the LevenbergMarquardtOptimizer class in package org.apache.commons.math3.optimization.general Original source copyright and license statement: Minpack Copyright Notice (1999) University of Chicago. All rights reserved Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 3. The end-user documentation included with the redistribution, if any, must include the following acknowledgment: "This product includes software developed by the University of Chicago, as Operator of Argonne National Laboratory. Alternately, this acknowledgment may appear in the software itself, if and wherever such third-party acknowledgments normally appear. 4. WARRANTY DISCLAIMER. THE SOFTWARE IS SUPPLIED "AS IS" WITHOUT WARRANTY OF ANY KIND. THE COPYRIGHT HOLDER, THE UNITED STATES, THE UNITED STATES DEPARTMENT OF ENERGY, AND THEIR EMPLOYEES: (1) DISCLAIM ANY WARRANTIES, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO ANY IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE OR NON-INFRINGEMENT, (2) DO NOT ASSUME ANY LEGAL LIABILITY OR RESPONSIBILITY FOR THE ACCURACY, COMPLETENESS, OR USEFULNESS OF THE SOFTWARE, (3) DO NOT REPRESENT THAT USE OF THE SOFTWARE WOULD NOT INFRINGE PRIVATELY OWNED RIGHTS, (4) DO NOT WARRANT THAT THE SOFTWARE WILL FUNCTION UNINTERRUPTED, THAT IT IS ERROR-FREE OR THAT ANY ERRORS WILL BE CORRECTED. 5. LIMITATION OF LIABILITY. IN NO EVENT WILL THE COPYRIGHT HOLDER, THE UNITED STATES, THE UNITED STATES DEPARTMENT OF ENERGY, OR THEIR EMPLOYEES: BE LIABLE FOR ANY INDIRECT, INCIDENTAL, CONSEQUENTIAL, SPECIAL OR PUNITIVE DAMAGES OF ANY KIND OR NATURE, INCLUDING BUT NOT LIMITED TO LOSS OF PROFITS OR LOSS OF DATA, FOR ANY REASON WHATSOEVER, WHETHER SUCH LIABILITY IS ASSERTED ON THE BASIS OF CONTRACT, TORT (INCLUDING NEGLIGENCE OR STRICT LIABILITY), OR OTHERWISE, EVEN IF ANY OF SAID PARTIES HAS BEEN WARNED OF THE POSSIBILITY OF SUCH LOSS OR DAMAGES. =============================================================================== Copyright and license statement for the odex Fortran routine developed by E. Hairer and G. Wanner and translated in GraggBulirschStoerIntegrator class in package org.apache.commons.math3.ode.nonstiff: Copyright (c) 2004, Ernst Hairer Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: - Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. =============================================================================== Copyright and license statement for the original lapack fortran routines translated in EigenDecompositionImpl class in package org.apache.commons.math3.linear: Copyright (c) 1992-2008 The University of Tennessee. All rights reserved. $COPYRIGHT$ Additional copyrights may follow $HEADER$ Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: - Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer listed in this license in the documentation and/or other materials provided with the distribution. - Neither the name of the copyright holders nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. =============================================================================== Copyright and license statement for the original Mersenne twister C routines translated in MersenneTwister class in package org.apache.commons.math3.random: Copyright (C) 1997 - 2002, Makoto Matsumoto and Takuji Nishimura, All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 3. The names of its contributors may not be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. =============================================================================== The class "org.apache.commons.math3.exception.util.LocalizedFormatsTest" is an adapted version of "OrekitMessagesTest" test class for the Orekit library The "org.apache.commons.math3.analysis.interpolation.HermiteInterpolator" has been imported from the Orekit space flight dynamics library. Th Orekit library is described at: https://www.orekit.org/forge/projects/orekit The original files are distributed under the terms of the Apache 2 license which is: Copyright 2010 CS Communication & Systèmes commons-math3-3.2-src/RELEASE-NOTES.txt100644 1750 1750 16003 12126627721 16116 0ustarlucluc 0 0 Apache Commons Math 3.2 RELEASE NOTES The Commons Math team is pleased to announce the release of commons-math3-3.2 The Math project is a library of lightweight, self-contained mathematics and statistics components addressing the most common practical problems not immediately available in the Java programming language or commons-lang. This is a minor release: It combines bug fixes and new features. Changes to existing features were made in a backwards-compatible way such as to allow drop-in replacement of the v3.1[.1] JAR file. Most notable among the new features are: Framework for automatic differentiation, Lévy distribution, prime numbers, enumerated distributions, real field allowing to use several algorithms (solvers, linear algebra, 3D geometry) with different real-like classes (high accuracy or automatic differentiation), spherical coordinates with gradients and Hessians, reorganized clustering package with different distance implementations. The minimum version of the Java platform required to compile and use Commons Math is Java 5. Users are encouraged to upgrade to this version as this release not only includes bug fixes but also deprecates numerous classes and methods that will be deleted from the next major release (4.0). Caveat: 1. The implementation of the BOBYQA optimization algorithm is in alpha state (cf. MATH-621): Many code paths are untested, and we are looking for volunteers to improve the code readability, robustness and performance and to extend the unit tests suite. 2. A few methods in the FastMath class are in fact slower that their counterpart in either Math or StrictMath (cf. MATH-740 and MATH-901). Changes in this version include: New features: o MATH-917,MATH-918,MATH-919,MATH-920: All contents of package "o.a.c.m.stat.clustering" refactored into new package "o.a.c.m.ml.clustering" and added support for additional distance measures in package "o.a.c.m.ml.distance": "CanberraDistance", "ChebyshevDistance", "EuclideanDistance" and "ManhattanDistance". Thanks to Reid Hochstedler. o MATH-817: Added Multivariate Normal Mixture Model Fitting by Expectation Maximization. Thanks to Jared Becksfort. o MATH-811: Added a way to trigger only increasing or decreasing events in ODE integration. o MATH-946: Added array-scaling methods to MathArrays. Thanks to Jared Becksfort. o MATH-460: Added the Lévy distribution. Thanks to Andrew Waterman. o MATH-948: Implementations for inverse error function and inverse complementary error functions have been added. o MATH-845: Added utilities for prime numbers. Thanks to Sébastien Riou. o MATH-914: Check bounds in multi-start vector optimizers. o MATH-941: Added discrete distributions. Thanks to Piotr Wydrych. o Added Hermite interpolator for RealFieldElement instances. o Added RealFieldElement interface to represent anything that is real number like, implemented by both Decimal64, Dfp and DerivativeStructure. o Added partial derivatives computation for 3D vectors and rotations. o Added accurate linear combination of DerivativeStructure instances, avoiding cancellation. o Added conversion of gradients and Hessians from spherical to Cartesian coordinates in 3D. Fixed Bugs: o MATH-961: Fixed wrong array dimensions in secondary equations handling in some cases. o MATH-960: Fixed missing side effects of secondary equations on main state in Ordinary Differential Equations integration. o MATH-957: Fixed inverse cumulative probability for uniform distribution. Thanks to Evan Ward. o MATH-891: "SpearmansCorrelation" now works correctly in case of a provided "NaturalRanking" with a "NaNStrategy.REMOVED" strategy and the input data contains NaN values. From version 4.0 onwards this strategy will not be supported anymore. o MATH-934: Fixed Complex.reciprocal() for zero argument. o MATH-862: AbstractRealMatrix will now check for rectangular input arrays in its copySubMatrix methods. o MATH-949: Increment iteration counter in optimization algorithms. o MATH-950: Fixed missing update in ODE event handlers, when a RESET_STATE is triggered. o MATH-947: Fixed infinite loop when NaN occurs in singular value decomposition. o MATH-580: Extended ranges for FastMath performance tests. o MATH-925: Finalized implementation of diagonal matrix. o MATH-630: Added rank revealing QR decomposition. Thanks to Christopher Nix. o MATH-570: ArrayFieldVector can now be constructed from any FieldVector. Thanks to Arne Plöse. o MATH-861: Improved checking of null vector elements. Thanks to Sébastien Brisard. o MATH-936: Fixed generation of long random numbers between two bounds. o MATH-942: Fixed creation of generic array. Thanks to Piotr Wydrych. o MATH-940: Fixed abstract test class naming that broke ant builds. Thanks to Piotr Wydrych. o MATH-939: Allow covariance to be computed for one-dimensional variables. Thanks to Piotr Wydrych. o MATH-938: Fixed accuracy of 3D Line.revert(). o MATH-937: Improved javadoc to explain how switching functions should behave across events in ODE events detection. o MATH-935: Fixed DerivativeStructure.atan2 for special cases when both arguments are +/-0. o MATH-930: Improved class javadoc wrt convergence criteria and added additional constructors to override the default epsilon and cut-off values in class "SimplexSolver". o MATH-929: Fixed truncated value in "MultivariateNormalDistribution". Thanks to Piotr Wydrych. o MATH-927: Made "BitStreamGenerator" implement the "Serializable" interface. Thanks to Dennis Hendriks. Changes: o MATH-956: Replaced hard-coded numbers in "LevenbergMarquardtOptimizer". o MATH-955: Fixed loading of test file when path contains a space. Thanks to Evan Ward. o MATH-954: Improved speed of FastMath.abs methods for all signatures, by removing branching. Thanks to Charles Cooper. o MATH-953: Improved speed of several FastMath methods. Thanks to Charles Cooper. o MATH-951: Improved speed of FastMath copysign methods. Thanks to Charles Cooper. o MATH-671: Made EmpiricalDisribution smoothing kernel pluggable. o MATH-877: Allow direct use of SummaryStatistics in one-way ANOVA. Thanks to Peter Andrews. o Normal distribution now uses a direct implementation of the inverse error function to compute inverse cumulative probability instead of relying on a numerical solver. This is much faster, more accurate and does not need convergence threshold. o MATH-933: Throw "MathUnsupportedOperationException" from optimizers that do not support constraints (previous behaviour was to silently ignore the "SimpleBounds" argument). o MATH-931: Greater efficiency in "UnitSphereRandomVectorGenerator". Thanks to Sean Owen. For complete information on Commons Math, including instructions on how to submit bug reports, patches, or suggestions for improvement, see the Apache Commons Math website: http://commons.apache.org/proper/commons-math/ commons-math3-3.2-src/pom.xml100644 1750 1750 42027 12126627721 14731 0ustarlucluc 0 0 org.apache.commons commons-parent 28 4.0.0 org.apache.commons commons-math3 3.2 Commons Math 2003 The Math project is a library of lightweight, self-contained mathematics and statistics components addressing the most common practical problems not immediately available in the Java programming language or commons-lang. http://commons.apache.org/proper/commons-math/ jira http://issues.apache.org/jira/browse/MATH scm:svn:http://svn.apache.org/repos/asf/commons/proper/math/trunk scm:svn:https://svn.apache.org/repos/asf/commons/proper/math/trunk http://svn.apache.org/viewvc/commons/proper/math/trunk people.apache.org Commons Math scp://people.apache.org/www/commons.apache.org/math Mikkel Meyer Andersen mikl mikl at apache dot org Bill Barker billbarker billbarker at apache dot org Sébastien Brisard celestin celestin at apache dot org Albert Davidson Chou achou achou at apache dot org Mark Diggory mdiggory mdiggory at apache dot org Robert Burrell Donkin rdonkin rdonkin at apache dot org Luc Maisonobe luc luc at apache dot org Tim O'Brien tobrien tobrien at apache dot org J. Pietschmann pietsch j3322ptm at yahoo dot de Dimitri Pourbaix dimpbx dimpbx at apache dot org Gilles Sadowski erans erans at apache dot org Phil Steitz psteitz psteitz at apache dot org Greg Sterijevski gregs gregs at apache dot org Brent Worden brentworden brentworden at apache dot org Thomas Neidhart tn tn at apache dot org Eldar Agalarov C. Scott Ananian Mark Anderson Peter Andrews Rémi Arntzen Jared Becksfort Michael Bjorkegren John Bollinger Cyril Briquet Dave Brosius Dan Checkoway Charles Cooper Paul Cowan Benjamin Croizet Larry Diamond Rodrigo di Lorenzo Lopes Hasan Diwan Ted Dunning John Gant Ken Geis Bernhard Grünewaldt Elliotte Rusty Harold Dennis Hendriks Reid Hochstedler Matthias Hummel Curtis Jensen Ismael Juma Eugene Kirpichov Piotr Kochanski Bob MacCallum Jake Mannix Benjamin McCann Patrick Meyer J. Lewis Muir Christopher Nix Fredrik Norin Sujit Pal Todd C. Parnell Andreas Rieger Sébastien Riou Bill Rossi Matthew Rowles Pavel Ryzhov Joni Salonen Michael Saunders Christopher Schuck Christian Semrau David Stefka Mauro Talevi Radoslav Tsvetkov Kim van der Linde Evan Ward Andrew Waterman Jörg Weimar Christian Winter Piotr Wydrych Xiaogang Zhang junit junit 4.10 test math org.apache.commons.math3 3.2 (requires Java 1.5+) RC5 -bin 2.2 commons-math-${commons.release.2.version} (requires Java 1.5+) MATH 12310485 UTF-8 1.5 1.5 2.7.1 2.5.1 2.9.1 org.apache.maven.plugins maven-surefire-plugin **/*Test.java **/*TestBinary.java **/*TestPermutations.java **/*AbstractTest.java maven-assembly-plugin src/main/assembly/src.xml src/main/assembly/bin.xml org.codehaus.mojo clirr-maven-plugin ${commons.clirr.version} maven-pmd-plugin ${math.pmd.version} ${maven.compile.target} org.apache.maven.plugins maven-scm-publish-plugin javadocs org.apache.maven.plugins maven-changes-plugin ${commons.changes.version} %URL%/%ISSUE% false Fix Version,Key,Summary,Type,Resolution,Status Key DESC,Type,Fix Version DESC Fixed Resolved,Closed Bug,New Feature,Task,Improvement,Wish,Test ${commons.release.version} 100 changes-report jira-report org.apache.rat apache-rat-plugin ${commons.rat.version} src/test/maxima/special/RealFunctionValidation/MANIFEST.txt src/test/resources/org/apache/commons/math3/random/testData.txt src/test/resources/org/apache/commons/math3/random/emptyFile.txt src/test/resources/org/apache/commons/math3/stat/data/PiDigits.txt src/test/resources/org/apache/commons/math3/stat/data/NumAcc3.txt src/test/resources/org/apache/commons/math3/stat/data/Lew.txt src/test/resources/org/apache/commons/math3/stat/data/NumAcc2.txt src/test/resources/org/apache/commons/math3/stat/data/NumAcc1.txt src/test/resources/org/apache/commons/math3/stat/data/Lottery.txt src/test/resources/org/apache/commons/math3/stat/data/NumAcc4.txt src/test/resources/org/apache/commons/math3/stat/data/Michelso.txt src/test/resources/org/apache/commons/math3/stat/data/Mavro.txt bin/** .gitignore .git/** org.codehaus.mojo findbugs-maven-plugin ${math.findbugs.version} Normal Default ${basedir}/findbugs-exclude-filter.xml org.apache.maven.plugins maven-checkstyle-plugin ${math.checkstyle.version} ${basedir}/checkstyle.xml false ${basedir}/license-header.txt checkstyle org.codehaus.mojo clirr-maven-plugin ${commons.clirr.version} ${minSeverity} maven-pmd-plugin ${math.pmd.version} ${maven.compile.target} pmd commons-math3-3.2-src/findbugs-exclude-filter.xml100644 1750 1750 35654 12126627721 20661 0ustarlucluc 0 0 commons-math3-3.2-src/NOTICE.txt100644 1750 1750 7513 12126627721 15117 0ustarlucluc 0 0 Apache Commons Math Copyright 2001-2013 The Apache Software Foundation This product includes software developed at The Apache Software Foundation (http://www.apache.org/). =============================================================================== The inverse error function implementation in the Erf class is based on CUDA code developed by Mike Giles, Oxford-Man Institute of Quantitative Finance, and published in GPU Computing Gems, volume 2, 2010. =============================================================================== The BracketFinder (package org.apache.commons.math3.optimization.univariate) and PowellOptimizer (package org.apache.commons.math3.optimization.general) classes are based on the Python code in module "optimize.py" (version 0.5) developed by Travis E. Oliphant for the SciPy library (http://www.scipy.org/) Copyright © 2003-2009 SciPy Developers. =============================================================================== The LinearConstraint, LinearObjectiveFunction, LinearOptimizer, RelationShip, SimplexSolver and SimplexTableau classes in package org.apache.commons.math3.optimization.linear include software developed by Benjamin McCann (http://www.benmccann.com) and distributed with the following copyright: Copyright 2009 Google Inc. =============================================================================== This product includes software developed by the University of Chicago, as Operator of Argonne National Laboratory. The LevenbergMarquardtOptimizer class in package org.apache.commons.math3.optimization.general includes software translated from the lmder, lmpar and qrsolv Fortran routines from the Minpack package Minpack Copyright Notice (1999) University of Chicago. All rights reserved =============================================================================== The GraggBulirschStoerIntegrator class in package org.apache.commons.math3.ode.nonstiff includes software translated from the odex Fortran routine developed by E. Hairer and G. Wanner. Original source copyright: Copyright (c) 2004, Ernst Hairer =============================================================================== The EigenDecompositionImpl class in package org.apache.commons.math3.linear includes software translated from some LAPACK Fortran routines. Original source copyright: Copyright (c) 1992-2008 The University of Tennessee. All rights reserved. =============================================================================== The MersenneTwister class in package org.apache.commons.math3.random includes software translated from the 2002-01-26 version of the Mersenne-Twister generator written in C by Makoto Matsumoto and Takuji Nishimura. Original source copyright: Copyright (C) 1997 - 2002, Makoto Matsumoto and Takuji Nishimura, All rights reserved =============================================================================== The LocalizedFormatsTest class in the unit tests is an adapted version of the OrekitMessagesTest class from the orekit library distributed under the terms of the Apache 2 licence. Original source copyright: Copyright 2010 CS Systèmes d'Information =============================================================================== The HermiteInterpolator class and its corresponding test have been imported from the orekit library distributed under the terms of the Apache 2 licence. Original source copyright: Copyright 2010-2012 CS Systèmes d'Information =============================================================================== The creation of the package "o.a.c.m.analysis.integration.gauss" was inspired by an original code donated by Sébastien Brisard. =============================================================================== The complete text of licenses and disclaimers associated with the the original sources enumerated above at the time of code translation are in the LICENSE.txt file. commons-math3-3.2-src/src/ 40755 1750 1750 0 12126627721 14061 5ustarlucluc 0 0 commons-math3-3.2-src/src/test/ 40755 1750 1750 0 12126627677 15052 5ustarlucluc 0 0 commons-math3-3.2-src/src/test/java/ 40755 1750 1750 0 12126627667 15772 5ustarlucluc 0 0 commons-math3-3.2-src/src/test/java/org/ 40755 1750 1750 0 12126627667 16561 5ustarlucluc 0 0 commons-math3-3.2-src/src/test/java/org/apache/ 40755 1750 1750 0 12126627667 20002 5ustarlucluc 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/ 40755 1750 1750 0 12126627667 21455 5ustarlucluc 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/ 40755 1750 1750 0 12126627675 22470 5ustarlucluc 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/primes/ 40755 1750 1750 0 12126627672 23764 5ustarlucluc 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/optimization/ 40755 1750 1750 0 12126627671 25212 5ustarlucluc 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/optimization/direct/ 40755 1750 1750 0 12126627670 26463 5ustarlucluc 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/optimization/fitting/ 40755 1750 1750 0 12126627670 26655 5ustarlucluc 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/optimization/linear/ 40755 1750 1750 0 12126627670 26463 5ustarlucluc 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/optimization/general/ 40755 1750 1750 0 12126627671 26627 5ustarlucluc 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/optimization/univariate/ 40755 1750 1750 0 12126627670 27360 5ustarlucluc 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/ode/ 40755 1750 1750 0 12126627673 23235 5ustarlucluc 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/ode/events/ 40755 1750 1750 0 12126627672 24540 5ustarlucluc 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/ode/sampling/ 40755 1750 1750 0 12126627673 25047 5ustarlucluc 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/ode/nonstiff/ 40755 1750 1750 0 12126627673 25063 5ustarlucluc 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/complex/ 40755 1750 1750 0 12126627672 24134 5ustarlucluc 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/fitting/ 40755 1750 1750 0 12126627672 24131 5ustarlucluc 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/transform/ 40755 1750 1750 0 12126627674 24502 5ustarlucluc 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/filter/ 40755 1750 1750 0 12126627670 23750 5ustarlucluc 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/analysis/ 40755 1750 1750 0 12126627670 24306 5ustarlucluc 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/analysis/interpolation/ 40755 1750 1750 0 12126627670 27175 5ustarlucluc 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/analysis/integration/ 40755 1750 1750 0 12126627670 26631 5ustarlucluc 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/analysis/integration/gauss/ 40755 1750 1750 0 12126627670 27753 5ustarlucluc 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/analysis/function/ 40755 1750 1750 0 12126627670 26133 5ustarlucluc 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/analysis/polynomials/ 40755 1750 1750 0 12126627670 26654 5ustarlucluc 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/analysis/differentiation/ 40755 1750 1750 0 12126627670 27460 5ustarlucluc 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/analysis/solvers/ 40755 1750 1750 0 12126627670 26003 5ustarlucluc 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/optim/ 40755 1750 1750 0 12126627671 23614 5ustarlucluc 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/optim/linear/ 40755 1750 1750 0 12126627671 25066 5ustarlucluc 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/optim/nonlinear/ 40755 1750 1750 0 12126627671 25601 5ustarlucluc 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/optim/nonlinear/vector/ 40755 1750 1750 0 12126627671 27103 5ustarlucluc 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/optim/nonlinear/vector/jacobian/ 40755 1750 1750 0 12126627671 30651 5ustarlucluc 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/optim/nonlinear/scalar/ 40755 1750 1750 0 12126627671 27046 5ustarlucluc 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/optim/nonlinear/scalar/noderiv/ 40755 1750 1750 0 12126627671 30514 5ustarlucluc 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/optim/nonlinear/scalar/gradient/ 40755 1750 1750 0 12126627671 30643 5ustarlucluc 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/optim/univariate/ 40755 1750 1750 0 12126627671 25763 5ustarlucluc 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/geometry/ 40755 1750 1750 0 12126627671 24317 5ustarlucluc 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/geometry/partitioning/ 40755 1750 1750 0 12126627671 27026 5ustarlucluc 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/geometry/partitioning/utilities/ 40755 1750 1750 0 12126627671 31041 5ustarlucluc 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/geometry/euclidean/ 40755 1750 1750 0 12126627671 26250 5ustarlucluc 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/geometry/euclidean/twod/ 40755 1750 1750 0 12126627672 27226 5ustarlucluc 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/geometry/euclidean/threed/ 40755 1750 1750 0 12126627671 27523 5ustarlucluc 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/geometry/euclidean/oned/ 40755 1750 1750 0 12126627671 27175 5ustarlucluc 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/random/ 40755 1750 1750 0 12126627670 23743 5ustarlucluc 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/special/ 40755 1750 1750 0 12126627667 24111 5ustarlucluc 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/linear/ 40755 1750 1750 0 12126627674 23741 5ustarlucluc 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/ml/ 40755 1750 1750 0 12126627673 23076 5ustarlucluc 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/ml/distance/ 40755 1750 1750 0 12126627673 24670 5ustarlucluc 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/ml/clustering/ 40755 1750 1750 0 12126627673 25255 5ustarlucluc 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/dfp/ 40755 1750 1750 0 12126627673 23237 5ustarlucluc 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/stat/ 40755 1750 1750 0 12126627674 23442 5ustarlucluc 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/stat/regression/ 40755 1750 1750 0 12126627674 25622 5ustarlucluc 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/stat/inference/ 40755 1750 1750 0 12126627674 25400 5ustarlucluc 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/stat/ranking/ 40755 1750 1750 0 12126627674 25073 5ustarlucluc 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/stat/clustering/ 40755 1750 1750 0 12126627674 25621 5ustarlucluc 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/stat/data/ 40755 1750 1750 0 12126627674 24353 5ustarlucluc 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/stat/descriptive/ 40755 1750 1750 0 12126627674 25763 5ustarlucluc 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/stat/descriptive/rank/ 40755 1750 1750 0 12126627674 26716 5ustarlucluc 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/stat/descriptive/summary/ 40755 1750 1750 0 12126627674 27460 5ustarlucluc 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/stat/descriptive/moment/ 40755 1750 1750 0 12126627674 27262 5ustarlucluc 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/stat/correlation/ 40755 1750 1750 0 12126627674 25763 5ustarlucluc 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/distribution/ 40755 1750 1750 0 12126627667 25210 5ustarlucluc 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/distribution/fitting/ 40755 1750 1750 0 12126627667 26654 5ustarlucluc 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/exception/ 40755 1750 1750 0 12126627672 24463 5ustarlucluc 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/exception/util/ 40755 1750 1750 0 12126627672 25440 5ustarlucluc 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/util/ 40755 1750 1750 0 12126627675 23445 5ustarlucluc 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/genetics/ 40755 1750 1750 0 12126627672 24266 5ustarlucluc 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/fraction/ 40755 1750 1750 0 12126627672 24272 5ustarlucluc 0 0 commons-math3-3.2-src/src/test/maxima/ 40755 1750 1750 0 12126627677 16326 5ustarlucluc 0 0 commons-math3-3.2-src/src/test/maxima/special/ 40755 1750 1750 0 12126627677 17746 5ustarlucluc 0 0 commons-math3-3.2-src/src/test/maxima/special/RealFunctionValidation/ 40755 1750 1750 0 12126627677 24352 5ustarlucluc 0 0 commons-math3-3.2-src/src/test/maxima/special/reference/ 40755 1750 1750 0 12126627677 21704 5ustarlucluc 0 0 commons-math3-3.2-src/src/test/R/ 40755 1750 1750 0 12126627667 15252 5ustarlucluc 0 0 commons-math3-3.2-src/src/test/resources/ 40755 1750 1750 0 12126627675 17062 5ustarlucluc 0 0 commons-math3-3.2-src/src/test/resources/org/ 40755 1750 1750 0 12126627675 17651 5ustarlucluc 0 0 commons-math3-3.2-src/src/test/resources/org/apache/ 40755 1750 1750 0 12126627675 21072 5ustarlucluc 0 0 commons-math3-3.2-src/src/test/resources/org/apache/commons/ 40755 1750 1750 0 12126627675 22545 5ustarlucluc 0 0 commons-math3-3.2-src/src/test/resources/org/apache/commons/math3/ 40755 1750 1750 0 12126627677 23563 5ustarlucluc 0 0 commons-math3-3.2-src/src/test/resources/org/apache/commons/math3/optimization/ 40755 1750 1750 0 12126627677 26311 5ustarlucluc 0 0 commons-math3-3.2-src/src/test/resources/org/apache/commons/math3/optimization/general/ 40755 1750 1750 0 12126627677 27726 5ustarlucluc 0 0 commons-math3-3.2-src/src/test/resources/org/apache/commons/math3/optim/ 40755 1750 1750 0 12126627677 24713 5ustarlucluc 0 0 commons-math3-3.2-src/src/test/resources/org/apache/commons/math3/optim/nonlinear/ 40755 1750 1750 0 12126627677 26700 5ustarlucluc 0 0 commons-math3-3.2-src/src/test/resources/org/apache/commons/math3/optim/nonlinear/vector/ 40755 1750 1750 0 12126627677 30202 5ustarlucluc 0 0 commons-math3-3.2-src/src/test/resources/org/apache/commons/math3/optim/nonlinear/vector/jacobian/ 40755 1750 1750 0 12126627677 31750 5ustarlucluc 0 0 commons-math3-3.2-src/src/test/resources/org/apache/commons/math3/random/ 40755 1750 1750 0 12126627677 25043 5ustarlucluc 0 0 commons-math3-3.2-src/src/test/resources/org/apache/commons/math3/linear/ 40755 1750 1750 0 12126627677 25035 5ustarlucluc 0 0 commons-math3-3.2-src/src/test/resources/org/apache/commons/math3/stat/ 40755 1750 1750 0 12126627677 24536 5ustarlucluc 0 0 commons-math3-3.2-src/src/test/resources/org/apache/commons/math3/stat/data/ 40755 1750 1750 0 12126627677 25447 5ustarlucluc 0 0 commons-math3-3.2-src/src/test/resources/org/apache/commons/math3/distribution/ 40755 1750 1750 0 12126627677 26302 5ustarlucluc 0 0 commons-math3-3.2-src/src/main/ 40755 1750 1750 0 12126627721 15005 5ustarlucluc 0 0 commons-math3-3.2-src/src/main/java/ 40755 1750 1750 0 12126627677 15740 5ustarlucluc 0 0 commons-math3-3.2-src/src/main/java/org/ 40755 1750 1750 0 12126627677 16527 5ustarlucluc 0 0 commons-math3-3.2-src/src/main/java/org/apache/ 40755 1750 1750 0 12126627677 17750 5ustarlucluc 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/ 40755 1750 1750 0 12126627677 21423 5ustarlucluc 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/ 40755 1750 1750 0 12126627720 22424 5ustarlucluc 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/primes/ 40755 1750 1750 0 12126627715 23727 5ustarlucluc 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/optimization/ 40755 1750 1750 0 12126627713 25154 5ustarlucluc 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/optimization/direct/ 40755 1750 1750 0 12126627712 26425 5ustarlucluc 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/optimization/fitting/ 40755 1750 1750 0 12126627711 26616 5ustarlucluc 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/optimization/linear/ 40755 1750 1750 0 12126627713 26426 5ustarlucluc 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/optimization/general/ 40755 1750 1750 0 12126627713 26571 5ustarlucluc 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/optimization/univariate/ 40755 1750 1750 0 12126627713 27323 5ustarlucluc 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/ode/ 40755 1750 1750 0 12126627716 23200 5ustarlucluc 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/ode/events/ 40755 1750 1750 0 12126627715 24503 5ustarlucluc 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/ode/sampling/ 40755 1750 1750 0 12126627715 25011 5ustarlucluc 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/ode/nonstiff/ 40755 1750 1750 0 12126627715 25025 5ustarlucluc 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/complex/ 40755 1750 1750 0 12126627716 24100 5ustarlucluc 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/fitting/ 40755 1750 1750 0 12126627715 24074 5ustarlucluc 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/transform/ 40755 1750 1750 0 12126627720 24437 5ustarlucluc 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/filter/ 40755 1750 1750 0 12126627710 23710 5ustarlucluc 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/analysis/ 40755 1750 1750 0 12126627707 24254 5ustarlucluc 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/analysis/interpolation/ 40755 1750 1750 0 12126627702 27136 5ustarlucluc 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/analysis/integration/ 40755 1750 1750 0 12126627701 26571 5ustarlucluc 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/analysis/integration/gauss/ 40755 1750 1750 0 12126627701 27713 5ustarlucluc 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/analysis/function/ 40755 1750 1750 0 12126627701 26073 5ustarlucluc 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/analysis/polynomials/ 40755 1750 1750 0 12126627705 26620 5ustarlucluc 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/analysis/differentiation/ 40755 1750 1750 0 12126627706 27425 5ustarlucluc 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/analysis/solvers/ 40755 1750 1750 0 12126627704 25746 5ustarlucluc 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/optim/ 40755 1750 1750 0 12126627714 23557 5ustarlucluc 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/optim/linear/ 40755 1750 1750 0 12126627714 25031 5ustarlucluc 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/optim/nonlinear/ 40755 1750 1750 0 12126627713 25543 5ustarlucluc 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/optim/nonlinear/vector/ 40755 1750 1750 0 12126627713 27045 5ustarlucluc 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/optim/nonlinear/vector/jacobian/ 40755 1750 1750 0 12126627713 30613 5ustarlucluc 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/optim/nonlinear/scalar/ 40755 1750 1750 0 12126627713 27010 5ustarlucluc 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/optim/nonlinear/scalar/noderiv/ 40755 1750 1750 0 12126627713 30456 5ustarlucluc 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/optim/nonlinear/scalar/gradient/ 40755 1750 1750 0 12126627713 30605 5ustarlucluc 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/optim/univariate/ 40755 1750 1750 0 12126627713 25725 5ustarlucluc 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/geometry/ 40755 1750 1750 0 12126627714 24262 5ustarlucluc 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/geometry/partitioning/ 40755 1750 1750 0 12126627714 26771 5ustarlucluc 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/geometry/partitioning/utilities/ 40755 1750 1750 0 12126627714 31004 5ustarlucluc 0 0 ././@LongLink100644 0 0 150 12126630646 10255 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/geometry/partitioning/utilities/doc-files/commons-math3-3.2-src/src/main/java/org/apache/commons/math3/geometry/partitioning/utilities/doc-fil 40755 1750 1750 0 12126627714 32242 5ustarlucluc 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/geometry/euclidean/ 40755 1750 1750 0 12126627714 26213 5ustarlucluc 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/geometry/euclidean/twod/ 40755 1750 1750 0 12126627714 27170 5ustarlucluc 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/geometry/euclidean/threed/ 40755 1750 1750 0 12126627714 27466 5ustarlucluc 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/geometry/euclidean/oned/ 40755 1750 1750 0 12126627714 27140 5ustarlucluc 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/random/ 40755 1750 1750 0 12126627700 23702 5ustarlucluc 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/special/ 40755 1750 1750 0 12126627700 24042 5ustarlucluc 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/linear/ 40755 1750 1750 0 12126627717 23704 5ustarlucluc 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/ml/ 40755 1750 1750 0 12126627716 23041 5ustarlucluc 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/ml/distance/ 40755 1750 1750 0 12126627716 24633 5ustarlucluc 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/ml/clustering/ 40755 1750 1750 0 12126627716 25220 5ustarlucluc 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/dfp/ 40755 1750 1750 0 12126627716 23202 5ustarlucluc 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/stat/ 40755 1750 1750 0 12126627717 23405 5ustarlucluc 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/stat/regression/ 40755 1750 1750 0 12126627717 25565 5ustarlucluc 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/stat/inference/ 40755 1750 1750 0 12126627717 25343 5ustarlucluc 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/stat/ranking/ 40755 1750 1750 0 12126627717 25036 5ustarlucluc 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/stat/clustering/ 40755 1750 1750 0 12126627717 25564 5ustarlucluc 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/stat/descriptive/ 40755 1750 1750 0 12126627717 25726 5ustarlucluc 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/stat/descriptive/rank/ 40755 1750 1750 0 12126627717 26661 5ustarlucluc 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/stat/descriptive/summary/ 40755 1750 1750 0 12126627717 27423 5ustarlucluc 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/stat/descriptive/moment/ 40755 1750 1750 0 12126627717 27225 5ustarlucluc 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/stat/correlation/ 40755 1750 1750 0 12126627717 25726 5ustarlucluc 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/distribution/ 40755 1750 1750 0 12126627700 25141 5ustarlucluc 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/distribution/fitting/ 40755 1750 1750 0 12126627677 26622 5ustarlucluc 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/exception/ 40755 1750 1750 0 12126627715 24426 5ustarlucluc 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/exception/util/ 40755 1750 1750 0 12126627714 25402 5ustarlucluc 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/util/ 40755 1750 1750 0 12126627720 23401 5ustarlucluc 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/genetics/ 40755 1750 1750 0 12126627715 24231 5ustarlucluc 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/fraction/ 40755 1750 1750 0 12126627715 24235 5ustarlucluc 0 0 commons-math3-3.2-src/src/main/assembly/ 40755 1750 1750 0 12126627677 16636 5ustarlucluc 0 0 commons-math3-3.2-src/src/main/resources/ 40755 1750 1750 0 12126627721 17017 5ustarlucluc 0 0 commons-math3-3.2-src/src/main/resources/assets/ 40755 1750 1750 0 12126627721 20321 5ustarlucluc 0 0 commons-math3-3.2-src/src/main/resources/assets/org/ 40755 1750 1750 0 12126627721 21110 5ustarlucluc 0 0 commons-math3-3.2-src/src/main/resources/assets/org/apache/ 40755 1750 1750 0 12126627721 22331 5ustarlucluc 0 0 commons-math3-3.2-src/src/main/resources/assets/org/apache/commons/ 40755 1750 1750 0 12126627721 24004 5ustarlucluc 0 0 commons-math3-3.2-src/src/main/resources/assets/org/apache/commons/math3/ 40755 1750 1750 0 12126627721 25020 5ustarlucluc 0 0 commons-math3-3.2-src/src/main/resources/assets/org/apache/commons/math3/exception/ 40755 1750 1750 0 12126627721 27016 5ustarlucluc 0 0 commons-math3-3.2-src/src/main/resources/assets/org/apache/commons/math3/exception/util/ 40755 1750 1750 0 12126627721 27773 5ustarlucluc 0 0 commons-math3-3.2-src/src/main/resources/data/ 40755 1750 1750 0 12126627721 17730 5ustarlucluc 0 0 commons-math3-3.2-src/src/main/resources/data/org/ 40755 1750 1750 0 12126627721 20517 5ustarlucluc 0 0 commons-math3-3.2-src/src/main/resources/data/org/apache/ 40755 1750 1750 0 12126627721 21740 5ustarlucluc 0 0 commons-math3-3.2-src/src/main/resources/data/org/apache/commons/ 40755 1750 1750 0 12126627721 23413 5ustarlucluc 0 0 commons-math3-3.2-src/src/main/resources/data/org/apache/commons/math3/ 40755 1750 1750 0 12126627721 24427 5ustarlucluc 0 0 commons-math3-3.2-src/src/main/resources/data/org/apache/commons/math3/util/ 40755 1750 1750 0 12126627721 25404 5ustarlucluc 0 0 commons-math3-3.2-src/src/main/resources/META-INF/ 40755 1750 1750 0 12126627721 20157 5ustarlucluc 0 0 commons-math3-3.2-src/src/main/resources/META-INF/localization/ 40755 1750 1750 0 12126627721 22647 5ustarlucluc 0 0 commons-math3-3.2-src/src/main/resources/templates/ 40755 1750 1750 0 12126627721 21015 5ustarlucluc 0 0 commons-math3-3.2-src/src/changes/ 40755 1750 1750 0 12126627721 15471 5ustarlucluc 0 0 commons-math3-3.2-src/src/media/ 40755 1750 1750 0 12126627666 15150 5ustarlucluc 0 0 commons-math3-3.2-src/src/site/ 40755 1750 1750 0 12126627721 15025 5ustarlucluc 0 0 commons-math3-3.2-src/src/site/apt/ 40755 1750 1750 0 12126627721 15611 5ustarlucluc 0 0 commons-math3-3.2-src/src/site/apt/userguide/ 40755 1750 1750 0 12126627721 17605 5ustarlucluc 0 0 commons-math3-3.2-src/src/site/xdoc/ 40755 1750 1750 0 12126627721 15762 5ustarlucluc 0 0 commons-math3-3.2-src/src/site/xdoc/userguide/ 40755 1750 1750 0 12126627721 17756 5ustarlucluc 0 0 commons-math3-3.2-src/src/site/resources/ 40755 1750 1750 0 12126627721 17037 5ustarlucluc 0 0 commons-math3-3.2-src/src/site/resources/style/ 40755 1750 1750 0 12126627721 20177 5ustarlucluc 0 0 commons-math3-3.2-src/src/site/resources/userguide/ 40755 1750 1750 0 12126627721 21033 5ustarlucluc 0 0 commons-math3-3.2-src/src/site/resources/images/ 40755 1750 1750 0 12126627721 20304 5ustarlucluc 0 0 commons-math3-3.2-src/src/site/design/ 40755 1750 1750 0 12126627721 16276 5ustarlucluc 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/primes/PrimesTest.java100644 1750 1750 15317 12126627672 27052 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.primes; import java.util.HashSet; import java.util.List; import org.apache.commons.math3.exception.MathIllegalArgumentException; import org.apache.commons.math3.exception.util.LocalizedFormats; import org.junit.Assert; import org.junit.Test; public class PrimesTest { public static final int[] PRIMES = {//primes here have been verified one by one using Dario Alejandro Alpern's tool, see http://www.alpertron.com.ar/ECM.HTM 2,3,5,7,11,13,17,19,23,29,31,43,47,53,71,73,79,89,97, 107,137,151,157,271,293,331,409,607,617,683,829, 1049,1103,1229,1657, 2039,2053,//around first boundary in miller-rabin 2251,2389,2473,2699,3271,3389,3449,5653,6449,6869,9067,9091, 11251,12433,12959,22961,41047,46337,65413,80803,91577,92693, 118423,656519,795659, 1373639,1373677,//around second boundary in miller-rabin 588977,952381, 1013041,1205999,2814001, 22605091, 25325981,25326023,//around third boundary in miller-rabin 100000007,715827881, 2147483647//Integer.MAX_VALUE }; public static final int[] NOT_PRIMES = {//composite chosen at random + particular values used in algorithms such as boundaries for millerRabin 4,6,8,9,10,12,14,15,16,18,20,21,22,24,25, 275, 2037,2041,2045,2046,2047,2048,2049,2051,2055,//around first boundary in miller-rabin 9095, 463465, 1373637,1373641,1373651,1373652,1373653,1373654,1373655,1373673,1373675,1373679,//around second boundary in miller-rabin 25325979,25325983,25325993,25325997,25325999,25326001,25326003,25326007,25326009,25326011,25326021,25326025,//around third boundary in miller-rabin 100000005, 1073741341,1073741823,2147473649,2147483641,2147483643,2147483645,2147483646}; public static final int[] BELOW_2 = { Integer.MIN_VALUE,-1,0,1}; void assertPrimeFactorsException(int n, Throwable expected) { try { Primes.primeFactors(n); Assert.fail("Exception not thrown"); } catch (Throwable e) { Assert.assertEquals(expected.getClass(), e.getClass()); if (expected.getMessage() != null) { Assert.assertEquals(expected.getMessage(), e.getMessage()); } } } void assertNextPrimeException(int n, Throwable expected){ try { Primes.nextPrime(n); Assert.fail("Exception not thrown"); } catch(Throwable e) { Assert.assertEquals(expected.getClass(), e.getClass()); if (expected.getMessage() != null) { Assert.assertEquals(expected.getMessage(), e.getMessage()); } } } @Test public void testNextPrime() { Assert.assertEquals(2, Primes.nextPrime(0)); Assert.assertEquals(2, Primes.nextPrime(1)); Assert.assertEquals(2, Primes.nextPrime(2)); Assert.assertEquals(3, Primes.nextPrime(3)); Assert.assertEquals(5, Primes.nextPrime(4)); Assert.assertEquals(5, Primes.nextPrime(5)); for (int i = 0; i < SmallPrimes.PRIMES.length - 1; i++) { for (int j = SmallPrimes.PRIMES[i] + 1; j <= SmallPrimes.PRIMES[i + 1]; j++) { Assert.assertEquals(SmallPrimes.PRIMES[i+1], Primes.nextPrime(j)); } } Assert.assertEquals(25325981, Primes.nextPrime(25325981)); for (int i = 25325981 + 1; i <= 25326023; i++) { Assert.assertEquals(25326023, Primes.nextPrime(i)); } Assert.assertEquals(Integer.MAX_VALUE, Primes.nextPrime(Integer.MAX_VALUE - 10)); Assert.assertEquals(Integer.MAX_VALUE, Primes.nextPrime(Integer.MAX_VALUE - 1)); Assert.assertEquals(Integer.MAX_VALUE, Primes.nextPrime(Integer.MAX_VALUE)); assertNextPrimeException(Integer.MIN_VALUE, new MathIllegalArgumentException(LocalizedFormats.NUMBER_TOO_SMALL,Integer.MIN_VALUE,0)); assertNextPrimeException(-1, new MathIllegalArgumentException(LocalizedFormats.NUMBER_TOO_SMALL,-1,0)); assertNextPrimeException(-13, new MathIllegalArgumentException(LocalizedFormats.NUMBER_TOO_SMALL,-13,0)); } @Test public void testIsPrime() throws Exception { for (int i : BELOW_2) { Assert.assertEquals(false,Primes.isPrime(i)); } for (int i:NOT_PRIMES) { Assert.assertEquals(false,Primes.isPrime(i)); } for (int i:PRIMES) { Assert.assertEquals(true,Primes.isPrime(i)); } } static int sum(List numbers){ int out = 0; for (int i:numbers) { out += i; } return out; } static int product(List numbers) { int out = 1; for (int i : numbers) { out *= i; } return out; } static final HashSet PRIMES_SET = new HashSet(); static { for (int p : PRIMES) { PRIMES_SET.add(p); } } static void checkPrimeFactors(List factors){ for (int p : factors) { if (!PRIMES_SET.contains(p)) { Assert.fail("Not found in primes list: " + p); } } } @Test public void testPrimeFactors() throws Exception { for (int i : BELOW_2) { assertPrimeFactorsException(i, new MathIllegalArgumentException(LocalizedFormats.NUMBER_TOO_SMALL,i,2)); } for (int i : NOT_PRIMES) { List factors = Primes.primeFactors(i); checkPrimeFactors(factors); int prod = product(factors); Assert.assertEquals(i, prod); } for (int i : PRIMES) { List factors = Primes.primeFactors(i); Assert.assertEquals(i, (int)factors.get(0)); Assert.assertEquals(1, factors.size()); } } }././@LongLink100644 0 0 151 12126630646 10256 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/optimization/direct/CMAESOptimizerTest.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/optimization/direct/CMAESOptimizerTest.100644 1750 1750 66604 12126627670 32210 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.optimization.direct; import java.util.Arrays; import java.util.Random; import org.apache.commons.math3.Retry; import org.apache.commons.math3.RetryRunner; import org.apache.commons.math3.analysis.MultivariateFunction; import org.apache.commons.math3.exception.NumberIsTooLargeException; import org.apache.commons.math3.exception.NumberIsTooSmallException; import org.apache.commons.math3.exception.DimensionMismatchException; import org.apache.commons.math3.exception.MathUnsupportedOperationException; import org.apache.commons.math3.exception.MathIllegalStateException; import org.apache.commons.math3.exception.NotPositiveException; import org.apache.commons.math3.exception.OutOfRangeException; import org.apache.commons.math3.optimization.GoalType; import org.apache.commons.math3.optimization.PointValuePair; import org.apache.commons.math3.optimization.InitialGuess; import org.apache.commons.math3.optimization.SimpleBounds; import org.apache.commons.math3.random.MersenneTwister; import org.apache.commons.math3.util.FastMath; import org.junit.Assert; import org.junit.Test; import org.junit.Ignore; import org.junit.runner.RunWith; /** * Test for {@link CMAESOptimizer}. */ @RunWith(RetryRunner.class) public class CMAESOptimizerTest { static final int DIM = 13; static final int LAMBDA = 4 + (int)(3.*Math.log(DIM)); @Test(expected = NumberIsTooLargeException.class) public void testInitOutofbounds1() { double[] startPoint = point(DIM,3); double[] insigma = point(DIM, 0.3); double[][] boundaries = boundaries(DIM,-1,2); PointValuePair expected = new PointValuePair(point(DIM,1.0),0.0); doTest(new Rosen(), startPoint, insigma, boundaries, GoalType.MINIMIZE, LAMBDA, true, 0, 1e-13, 1e-13, 1e-6, 100000, expected); } @Test(expected = NumberIsTooSmallException.class) public void testInitOutofbounds2() { double[] startPoint = point(DIM, -2); double[] insigma = point(DIM, 0.3); double[][] boundaries = boundaries(DIM,-1,2); PointValuePair expected = new PointValuePair(point(DIM,1.0),0.0); doTest(new Rosen(), startPoint, insigma, boundaries, GoalType.MINIMIZE, LAMBDA, true, 0, 1e-13, 1e-13, 1e-6, 100000, expected); } @Test(expected = DimensionMismatchException.class) public void testBoundariesDimensionMismatch() { double[] startPoint = point(DIM,0.5); double[] insigma = point(DIM, 0.3); double[][] boundaries = boundaries(DIM+1,-1,2); PointValuePair expected = new PointValuePair(point(DIM,1.0),0.0); doTest(new Rosen(), startPoint, insigma, boundaries, GoalType.MINIMIZE, LAMBDA, true, 0, 1e-13, 1e-13, 1e-6, 100000, expected); } @Test(expected = NotPositiveException.class) public void testInputSigmaNegative() { double[] startPoint = point(DIM,0.5); double[] insigma = point(DIM,-0.5); double[][] boundaries = null; PointValuePair expected = new PointValuePair(point(DIM,1.0),0.0); doTest(new Rosen(), startPoint, insigma, boundaries, GoalType.MINIMIZE, LAMBDA, true, 0, 1e-13, 1e-13, 1e-6, 100000, expected); } @Test(expected = OutOfRangeException.class) public void testInputSigmaOutOfRange() { double[] startPoint = point(DIM,0.5); double[] insigma = point(DIM, 1.1); double[][] boundaries = boundaries(DIM,-0.5,0.5); PointValuePair expected = new PointValuePair(point(DIM,1.0),0.0); doTest(new Rosen(), startPoint, insigma, boundaries, GoalType.MINIMIZE, LAMBDA, true, 0, 1e-13, 1e-13, 1e-6, 100000, expected); } @Test(expected = DimensionMismatchException.class) public void testInputSigmaDimensionMismatch() { double[] startPoint = point(DIM,0.5); double[] insigma = point(DIM + 1, 0.5); double[][] boundaries = null; PointValuePair expected = new PointValuePair(point(DIM,1.0),0.0); doTest(new Rosen(), startPoint, insigma, boundaries, GoalType.MINIMIZE, LAMBDA, true, 0, 1e-13, 1e-13, 1e-6, 100000, expected); } @Test @Retry(3) public void testRosen() { double[] startPoint = point(DIM,0.1); double[] insigma = point(DIM,0.1); double[][] boundaries = null; PointValuePair expected = new PointValuePair(point(DIM,1.0),0.0); doTest(new Rosen(), startPoint, insigma, boundaries, GoalType.MINIMIZE, LAMBDA, true, 0, 1e-13, 1e-13, 1e-6, 100000, expected); doTest(new Rosen(), startPoint, insigma, boundaries, GoalType.MINIMIZE, LAMBDA, false, 0, 1e-13, 1e-13, 1e-6, 100000, expected); } @Test @Retry(3) public void testMaximize() { double[] startPoint = point(DIM,1.0); double[] insigma = point(DIM,0.1); double[][] boundaries = null; PointValuePair expected = new PointValuePair(point(DIM,0.0),1.0); doTest(new MinusElli(), startPoint, insigma, boundaries, GoalType.MAXIMIZE, LAMBDA, true, 0, 1.0-1e-13, 2e-10, 5e-6, 100000, expected); doTest(new MinusElli(), startPoint, insigma, boundaries, GoalType.MAXIMIZE, LAMBDA, false, 0, 1.0-1e-13, 2e-10, 5e-6, 100000, expected); boundaries = boundaries(DIM,-0.3,0.3); startPoint = point(DIM,0.1); doTest(new MinusElli(), startPoint, insigma, boundaries, GoalType.MAXIMIZE, LAMBDA, true, 0, 1.0-1e-13, 2e-10, 5e-6, 100000, expected); } @Test public void testEllipse() { double[] startPoint = point(DIM,1.0); double[] insigma = point(DIM,0.1); double[][] boundaries = null; PointValuePair expected = new PointValuePair(point(DIM,0.0),0.0); doTest(new Elli(), startPoint, insigma, boundaries, GoalType.MINIMIZE, LAMBDA, true, 0, 1e-13, 1e-13, 1e-6, 100000, expected); doTest(new Elli(), startPoint, insigma, boundaries, GoalType.MINIMIZE, LAMBDA, false, 0, 1e-13, 1e-13, 1e-6, 100000, expected); } @Test public void testElliRotated() { double[] startPoint = point(DIM,1.0); double[] insigma = point(DIM,0.1); double[][] boundaries = null; PointValuePair expected = new PointValuePair(point(DIM,0.0),0.0); doTest(new ElliRotated(), startPoint, insigma, boundaries, GoalType.MINIMIZE, LAMBDA, true, 0, 1e-13, 1e-13, 1e-6, 100000, expected); doTest(new ElliRotated(), startPoint, insigma, boundaries, GoalType.MINIMIZE, LAMBDA, false, 0, 1e-13, 1e-13, 1e-6, 100000, expected); } @Test public void testCigar() { double[] startPoint = point(DIM,1.0); double[] insigma = point(DIM,0.1); double[][] boundaries = null; PointValuePair expected = new PointValuePair(point(DIM,0.0),0.0); doTest(new Cigar(), startPoint, insigma, boundaries, GoalType.MINIMIZE, LAMBDA, true, 0, 1e-13, 1e-13, 1e-6, 200000, expected); doTest(new Cigar(), startPoint, insigma, boundaries, GoalType.MINIMIZE, LAMBDA, false, 0, 1e-13, 1e-13, 1e-6, 100000, expected); } @Test public void testCigarWithBoundaries() { double[] startPoint = point(DIM,1.0); double[] insigma = point(DIM,0.1); double[][] boundaries = boundaries(DIM, -1e100, Double.POSITIVE_INFINITY); PointValuePair expected = new PointValuePair(point(DIM,0.0),0.0); doTest(new Cigar(), startPoint, insigma, boundaries, GoalType.MINIMIZE, LAMBDA, true, 0, 1e-13, 1e-13, 1e-6, 200000, expected); doTest(new Cigar(), startPoint, insigma, boundaries, GoalType.MINIMIZE, LAMBDA, false, 0, 1e-13, 1e-13, 1e-6, 100000, expected); } @Test public void testTwoAxes() { double[] startPoint = point(DIM,1.0); double[] insigma = point(DIM,0.1); double[][] boundaries = null; PointValuePair expected = new PointValuePair(point(DIM,0.0),0.0); doTest(new TwoAxes(), startPoint, insigma, boundaries, GoalType.MINIMIZE, 2*LAMBDA, true, 0, 1e-13, 1e-13, 1e-6, 200000, expected); doTest(new TwoAxes(), startPoint, insigma, boundaries, GoalType.MINIMIZE, 2*LAMBDA, false, 0, 1e-13, 1e-8, 1e-3, 200000, expected); } @Test public void testCigTab() { double[] startPoint = point(DIM,1.0); double[] insigma = point(DIM,0.3); double[][] boundaries = null; PointValuePair expected = new PointValuePair(point(DIM,0.0),0.0); doTest(new CigTab(), startPoint, insigma, boundaries, GoalType.MINIMIZE, LAMBDA, true, 0, 1e-13, 1e-13, 5e-5, 100000, expected); doTest(new CigTab(), startPoint, insigma, boundaries, GoalType.MINIMIZE, LAMBDA, false, 0, 1e-13, 1e-13, 5e-5, 100000, expected); } @Test public void testSphere() { double[] startPoint = point(DIM,1.0); double[] insigma = point(DIM,0.1); double[][] boundaries = null; PointValuePair expected = new PointValuePair(point(DIM,0.0),0.0); doTest(new Sphere(), startPoint, insigma, boundaries, GoalType.MINIMIZE, LAMBDA, true, 0, 1e-13, 1e-13, 1e-6, 100000, expected); doTest(new Sphere(), startPoint, insigma, boundaries, GoalType.MINIMIZE, LAMBDA, false, 0, 1e-13, 1e-13, 1e-6, 100000, expected); } @Test public void testTablet() { double[] startPoint = point(DIM,1.0); double[] insigma = point(DIM,0.1); double[][] boundaries = null; PointValuePair expected = new PointValuePair(point(DIM,0.0),0.0); doTest(new Tablet(), startPoint, insigma, boundaries, GoalType.MINIMIZE, LAMBDA, true, 0, 1e-13, 1e-13, 1e-6, 100000, expected); doTest(new Tablet(), startPoint, insigma, boundaries, GoalType.MINIMIZE, LAMBDA, false, 0, 1e-13, 1e-13, 1e-6, 100000, expected); } @Test public void testDiffPow() { double[] startPoint = point(DIM,1.0); double[] insigma = point(DIM,0.1); double[][] boundaries = null; PointValuePair expected = new PointValuePair(point(DIM,0.0),0.0); doTest(new DiffPow(), startPoint, insigma, boundaries, GoalType.MINIMIZE, 10, true, 0, 1e-13, 1e-8, 1e-1, 100000, expected); doTest(new DiffPow(), startPoint, insigma, boundaries, GoalType.MINIMIZE, 10, false, 0, 1e-13, 1e-8, 2e-1, 100000, expected); } @Test public void testSsDiffPow() { double[] startPoint = point(DIM,1.0); double[] insigma = point(DIM,0.1); double[][] boundaries = null; PointValuePair expected = new PointValuePair(point(DIM,0.0),0.0); doTest(new SsDiffPow(), startPoint, insigma, boundaries, GoalType.MINIMIZE, 10, true, 0, 1e-13, 1e-4, 1e-1, 200000, expected); doTest(new SsDiffPow(), startPoint, insigma, boundaries, GoalType.MINIMIZE, 10, false, 0, 1e-13, 1e-4, 1e-1, 200000, expected); } @Test public void testAckley() { double[] startPoint = point(DIM,1.0); double[] insigma = point(DIM,1.0); double[][] boundaries = null; PointValuePair expected = new PointValuePair(point(DIM,0.0),0.0); doTest(new Ackley(), startPoint, insigma, boundaries, GoalType.MINIMIZE, 2*LAMBDA, true, 0, 1e-13, 1e-9, 1e-5, 100000, expected); doTest(new Ackley(), startPoint, insigma, boundaries, GoalType.MINIMIZE, 2*LAMBDA, false, 0, 1e-13, 1e-9, 1e-5, 100000, expected); } @Test public void testRastrigin() { double[] startPoint = point(DIM,0.1); double[] insigma = point(DIM,0.1); double[][] boundaries = null; PointValuePair expected = new PointValuePair(point(DIM,0.0),0.0); doTest(new Rastrigin(), startPoint, insigma, boundaries, GoalType.MINIMIZE, (int)(200*Math.sqrt(DIM)), true, 0, 1e-13, 1e-13, 1e-6, 200000, expected); doTest(new Rastrigin(), startPoint, insigma, boundaries, GoalType.MINIMIZE, (int)(200*Math.sqrt(DIM)), false, 0, 1e-13, 1e-13, 1e-6, 200000, expected); } @Test public void testConstrainedRosen() { double[] startPoint = point(DIM, 0.1); double[] insigma = point(DIM, 0.1); double[][] boundaries = boundaries(DIM, -1, 2); PointValuePair expected = new PointValuePair(point(DIM,1.0),0.0); doTest(new Rosen(), startPoint, insigma, boundaries, GoalType.MINIMIZE, 2*LAMBDA, true, 0, 1e-13, 1e-13, 1e-6, 100000, expected); doTest(new Rosen(), startPoint, insigma, boundaries, GoalType.MINIMIZE, 2*LAMBDA, false, 0, 1e-13, 1e-13, 1e-6, 100000, expected); } @Test public void testDiagonalRosen() { double[] startPoint = point(DIM,0.1); double[] insigma = point(DIM,0.1); double[][] boundaries = null; PointValuePair expected = new PointValuePair(point(DIM,1.0),0.0); doTest(new Rosen(), startPoint, insigma, boundaries, GoalType.MINIMIZE, LAMBDA, false, 1, 1e-13, 1e-10, 1e-4, 1000000, expected); } @Test public void testMath864() { final CMAESOptimizer optimizer = new CMAESOptimizer(); final MultivariateFunction fitnessFunction = new MultivariateFunction() { public double value(double[] parameters) { final double target = 1; final double error = target - parameters[0]; return error * error; } }; final double[] start = { 0 }; final double[] lower = { -1e6 }; final double[] upper = { 1.5 }; final double[] result = optimizer.optimize(10000, fitnessFunction, GoalType.MINIMIZE, start, lower, upper).getPoint(); Assert.assertTrue("Out of bounds (" + result[0] + " > " + upper[0] + ")", result[0] <= upper[0]); } /** * Cf. MATH-867 */ @Test public void testFitAccuracyDependsOnBoundary() { final CMAESOptimizer optimizer = new CMAESOptimizer(); final MultivariateFunction fitnessFunction = new MultivariateFunction() { public double value(double[] parameters) { final double target = 11.1; final double error = target - parameters[0]; return error * error; } }; final double[] start = { 1 }; // No bounds. PointValuePair result = optimizer.optimize(100000, fitnessFunction, GoalType.MINIMIZE, start); final double resNoBound = result.getPoint()[0]; // Optimum is near the lower bound. final double[] lower = { -20 }; final double[] upper = { 5e16 }; result = optimizer.optimize(100000, fitnessFunction, GoalType.MINIMIZE, start, lower, upper); final double resNearLo = result.getPoint()[0]; // Optimum is near the upper bound. lower[0] = -5e16; upper[0] = 20; result = optimizer.optimize(100000, fitnessFunction, GoalType.MINIMIZE, start, lower, upper); final double resNearHi = result.getPoint()[0]; // System.out.println("resNoBound=" + resNoBound + // " resNearLo=" + resNearLo + // " resNearHi=" + resNearHi); // The two values currently differ by a substantial amount, indicating that // the bounds definition can prevent reaching the optimum. Assert.assertEquals(resNoBound, resNearLo, 1e-3); Assert.assertEquals(resNoBound, resNearHi, 1e-3); } /** * @param func Function to optimize. * @param startPoint Starting point. * @param inSigma Individual input sigma. * @param boundaries Upper / lower point limit. * @param goal Minimization or maximization. * @param lambda Population size used for offspring. * @param isActive Covariance update mechanism. * @param diagonalOnly Simplified covariance update. * @param stopValue Termination criteria for optimization. * @param fTol Tolerance relative error on the objective function. * @param pointTol Tolerance for checking that the optimum is correct. * @param maxEvaluations Maximum number of evaluations. * @param expected Expected point / value. */ private void doTest(MultivariateFunction func, double[] startPoint, double[] inSigma, double[][] boundaries, GoalType goal, int lambda, boolean isActive, int diagonalOnly, double stopValue, double fTol, double pointTol, int maxEvaluations, PointValuePair expected) { int dim = startPoint.length; // test diagonalOnly = 0 - slow but normally fewer feval# CMAESOptimizer optim = new CMAESOptimizer(30000, stopValue, isActive, diagonalOnly, 0, new MersenneTwister(), false, null); final double[] lB = boundaries == null ? null : boundaries[0]; final double[] uB = boundaries == null ? null : boundaries[1]; PointValuePair result = boundaries == null ? optim.optimize(maxEvaluations, func, goal, new InitialGuess(startPoint), new CMAESOptimizer.Sigma(inSigma), new CMAESOptimizer.PopulationSize(lambda)) : optim.optimize(maxEvaluations, func, goal, new InitialGuess(startPoint), new SimpleBounds(lB, uB), new CMAESOptimizer.Sigma(inSigma), new CMAESOptimizer.PopulationSize(lambda)); // System.out.println("sol=" + Arrays.toString(result.getPoint())); Assert.assertEquals(expected.getValue(), result.getValue(), fTol); for (int i = 0; i < dim; i++) { Assert.assertEquals(expected.getPoint()[i], result.getPoint()[i], pointTol); } } private static double[] point(int n, double value) { double[] ds = new double[n]; Arrays.fill(ds, value); return ds; } private static double[][] boundaries(int dim, double lower, double upper) { double[][] boundaries = new double[2][dim]; for (int i = 0; i < dim; i++) boundaries[0][i] = lower; for (int i = 0; i < dim; i++) boundaries[1][i] = upper; return boundaries; } private static class Sphere implements MultivariateFunction { public double value(double[] x) { double f = 0; for (int i = 0; i < x.length; ++i) f += x[i] * x[i]; return f; } } private static class Cigar implements MultivariateFunction { private double factor; Cigar() { this(1e3); } Cigar(double axisratio) { factor = axisratio * axisratio; } public double value(double[] x) { double f = x[0] * x[0]; for (int i = 1; i < x.length; ++i) f += factor * x[i] * x[i]; return f; } } private static class Tablet implements MultivariateFunction { private double factor; Tablet() { this(1e3); } Tablet(double axisratio) { factor = axisratio * axisratio; } public double value(double[] x) { double f = factor * x[0] * x[0]; for (int i = 1; i < x.length; ++i) f += x[i] * x[i]; return f; } } private static class CigTab implements MultivariateFunction { private double factor; CigTab() { this(1e4); } CigTab(double axisratio) { factor = axisratio; } public double value(double[] x) { int end = x.length - 1; double f = x[0] * x[0] / factor + factor * x[end] * x[end]; for (int i = 1; i < end; ++i) f += x[i] * x[i]; return f; } } private static class TwoAxes implements MultivariateFunction { private double factor; TwoAxes() { this(1e6); } TwoAxes(double axisratio) { factor = axisratio * axisratio; } public double value(double[] x) { double f = 0; for (int i = 0; i < x.length; ++i) f += (i < x.length / 2 ? factor : 1) * x[i] * x[i]; return f; } } private static class ElliRotated implements MultivariateFunction { private Basis B = new Basis(); private double factor; ElliRotated() { this(1e3); } ElliRotated(double axisratio) { factor = axisratio * axisratio; } public double value(double[] x) { double f = 0; x = B.Rotate(x); for (int i = 0; i < x.length; ++i) f += Math.pow(factor, i / (x.length - 1.)) * x[i] * x[i]; return f; } } private static class Elli implements MultivariateFunction { private double factor; Elli() { this(1e3); } Elli(double axisratio) { factor = axisratio * axisratio; } public double value(double[] x) { double f = 0; for (int i = 0; i < x.length; ++i) f += Math.pow(factor, i / (x.length - 1.)) * x[i] * x[i]; return f; } } private static class MinusElli implements MultivariateFunction { public double value(double[] x) { return 1.0-(new Elli().value(x)); } } private static class DiffPow implements MultivariateFunction { public double value(double[] x) { double f = 0; for (int i = 0; i < x.length; ++i) f += Math.pow(Math.abs(x[i]), 2. + 10 * (double) i / (x.length - 1.)); return f; } } private static class SsDiffPow implements MultivariateFunction { public double value(double[] x) { double f = Math.pow(new DiffPow().value(x), 0.25); return f; } } private static class Rosen implements MultivariateFunction { public double value(double[] x) { double f = 0; for (int i = 0; i < x.length - 1; ++i) f += 1e2 * (x[i] * x[i] - x[i + 1]) * (x[i] * x[i] - x[i + 1]) + (x[i] - 1.) * (x[i] - 1.); return f; } } private static class Ackley implements MultivariateFunction { private double axisratio; Ackley(double axra) { axisratio = axra; } public Ackley() { this(1); } public double value(double[] x) { double f = 0; double res2 = 0; double fac = 0; for (int i = 0; i < x.length; ++i) { fac = Math.pow(axisratio, (i - 1.) / (x.length - 1.)); f += fac * fac * x[i] * x[i]; res2 += Math.cos(2. * Math.PI * fac * x[i]); } f = (20. - 20. * Math.exp(-0.2 * Math.sqrt(f / x.length)) + Math.exp(1.) - Math.exp(res2 / x.length)); return f; } } private static class Rastrigin implements MultivariateFunction { private double axisratio; private double amplitude; Rastrigin() { this(1, 10); } Rastrigin(double axisratio, double amplitude) { this.axisratio = axisratio; this.amplitude = amplitude; } public double value(double[] x) { double f = 0; double fac; for (int i = 0; i < x.length; ++i) { fac = Math.pow(axisratio, (i - 1.) / (x.length - 1.)); if (i == 0 && x[i] < 0) fac *= 1.; f += fac * fac * x[i] * x[i] + amplitude * (1. - Math.cos(2. * Math.PI * fac * x[i])); } return f; } } private static class Basis { double[][] basis; Random rand = new Random(2); // use not always the same basis double[] Rotate(double[] x) { GenBasis(x.length); double[] y = new double[x.length]; for (int i = 0; i < x.length; ++i) { y[i] = 0; for (int j = 0; j < x.length; ++j) y[i] += basis[i][j] * x[j]; } return y; } void GenBasis(int DIM) { if (basis != null ? basis.length == DIM : false) return; double sp; int i, j, k; /* generate orthogonal basis */ basis = new double[DIM][DIM]; for (i = 0; i < DIM; ++i) { /* sample components gaussian */ for (j = 0; j < DIM; ++j) basis[i][j] = rand.nextGaussian(); /* substract projection of previous vectors */ for (j = i - 1; j >= 0; --j) { for (sp = 0., k = 0; k < DIM; ++k) sp += basis[i][k] * basis[j][k]; /* scalar product */ for (k = 0; k < DIM; ++k) basis[i][k] -= sp * basis[j][k]; /* substract */ } /* normalize */ for (sp = 0., k = 0; k < DIM; ++k) sp += basis[i][k] * basis[i][k]; /* squared norm */ for (k = 0; k < DIM; ++k) basis[i][k] /= Math.sqrt(sp); } } } } ././@LongLink100644 0 0 165 12126630646 10263 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/optimization/direct/SimplexOptimizerNelderMeadTest.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/optimization/direct/SimplexOptimizerNel100644 1750 1750 25776 12126627670 32527 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.optimization.direct; import org.apache.commons.math3.exception.TooManyEvaluationsException; import org.apache.commons.math3.analysis.MultivariateFunction; import org.apache.commons.math3.analysis.MultivariateVectorFunction; import org.apache.commons.math3.linear.Array2DRowRealMatrix; import org.apache.commons.math3.linear.RealMatrix; import org.apache.commons.math3.optimization.GoalType; import org.apache.commons.math3.optimization.LeastSquaresConverter; import org.apache.commons.math3.optimization.PointValuePair; import org.apache.commons.math3.util.FastMath; import org.junit.Assert; import org.junit.Test; public class SimplexOptimizerNelderMeadTest { @Test public void testMinimize1() { SimplexOptimizer optimizer = new SimplexOptimizer(1e-10, 1e-30); optimizer.setSimplex(new NelderMeadSimplex(new double[] { 0.2, 0.2 })); final FourExtrema fourExtrema = new FourExtrema(); final PointValuePair optimum = optimizer.optimize(100, fourExtrema, GoalType.MINIMIZE, new double[] { -3, 0 }); Assert.assertEquals(fourExtrema.xM, optimum.getPoint()[0], 2e-7); Assert.assertEquals(fourExtrema.yP, optimum.getPoint()[1], 2e-5); Assert.assertEquals(fourExtrema.valueXmYp, optimum.getValue(), 6e-12); Assert.assertTrue(optimizer.getEvaluations() > 60); Assert.assertTrue(optimizer.getEvaluations() < 90); } @Test public void testMinimize2() { SimplexOptimizer optimizer = new SimplexOptimizer(1e-10, 1e-30); optimizer.setSimplex(new NelderMeadSimplex(new double[] { 0.2, 0.2 })); final FourExtrema fourExtrema = new FourExtrema(); final PointValuePair optimum = optimizer.optimize(100, fourExtrema, GoalType.MINIMIZE, new double[] { 1, 0 }); Assert.assertEquals(fourExtrema.xP, optimum.getPoint()[0], 5e-6); Assert.assertEquals(fourExtrema.yM, optimum.getPoint()[1], 6e-6); Assert.assertEquals(fourExtrema.valueXpYm, optimum.getValue(), 1e-11); Assert.assertTrue(optimizer.getEvaluations() > 60); Assert.assertTrue(optimizer.getEvaluations() < 90); } @Test public void testMaximize1() { SimplexOptimizer optimizer = new SimplexOptimizer(1e-10, 1e-30); optimizer.setSimplex(new NelderMeadSimplex(new double[] { 0.2, 0.2 })); final FourExtrema fourExtrema = new FourExtrema(); final PointValuePair optimum = optimizer.optimize(100, fourExtrema, GoalType.MAXIMIZE, new double[] { -3, 0 }); Assert.assertEquals(fourExtrema.xM, optimum.getPoint()[0], 1e-5); Assert.assertEquals(fourExtrema.yM, optimum.getPoint()[1], 3e-6); Assert.assertEquals(fourExtrema.valueXmYm, optimum.getValue(), 3e-12); Assert.assertTrue(optimizer.getEvaluations() > 60); Assert.assertTrue(optimizer.getEvaluations() < 90); } @Test public void testMaximize2() { SimplexOptimizer optimizer = new SimplexOptimizer(1e-10, 1e-30); optimizer.setSimplex(new NelderMeadSimplex(new double[] { 0.2, 0.2 })); final FourExtrema fourExtrema = new FourExtrema(); final PointValuePair optimum = optimizer.optimize(100, fourExtrema, GoalType.MAXIMIZE, new double[] { 1, 0 }); Assert.assertEquals(fourExtrema.xP, optimum.getPoint()[0], 4e-6); Assert.assertEquals(fourExtrema.yP, optimum.getPoint()[1], 5e-6); Assert.assertEquals(fourExtrema.valueXpYp, optimum.getValue(), 7e-12); Assert.assertTrue(optimizer.getEvaluations() > 60); Assert.assertTrue(optimizer.getEvaluations() < 90); } @Test public void testRosenbrock() { Rosenbrock rosenbrock = new Rosenbrock(); SimplexOptimizer optimizer = new SimplexOptimizer(-1, 1e-3); optimizer.setSimplex(new NelderMeadSimplex(new double[][] { { -1.2, 1 }, { 0.9, 1.2 } , { 3.5, -2.3 } })); PointValuePair optimum = optimizer.optimize(100, rosenbrock, GoalType.MINIMIZE, new double[] { -1.2, 1 }); Assert.assertEquals(rosenbrock.getCount(), optimizer.getEvaluations()); Assert.assertTrue(optimizer.getEvaluations() > 40); Assert.assertTrue(optimizer.getEvaluations() < 50); Assert.assertTrue(optimum.getValue() < 8e-4); } @Test public void testPowell() { Powell powell = new Powell(); SimplexOptimizer optimizer = new SimplexOptimizer(-1, 1e-3); optimizer.setSimplex(new NelderMeadSimplex(4)); PointValuePair optimum = optimizer.optimize(200, powell, GoalType.MINIMIZE, new double[] { 3, -1, 0, 1 }); Assert.assertEquals(powell.getCount(), optimizer.getEvaluations()); Assert.assertTrue(optimizer.getEvaluations() > 110); Assert.assertTrue(optimizer.getEvaluations() < 130); Assert.assertTrue(optimum.getValue() < 2e-3); } @Test public void testLeastSquares1() { final RealMatrix factors = new Array2DRowRealMatrix(new double[][] { { 1, 0 }, { 0, 1 } }, false); LeastSquaresConverter ls = new LeastSquaresConverter(new MultivariateVectorFunction() { public double[] value(double[] variables) { return factors.operate(variables); } }, new double[] { 2.0, -3.0 }); SimplexOptimizer optimizer = new SimplexOptimizer(-1, 1e-6); optimizer.setSimplex(new NelderMeadSimplex(2)); PointValuePair optimum = optimizer.optimize(200, ls, GoalType.MINIMIZE, new double[] { 10, 10 }); Assert.assertEquals( 2, optimum.getPointRef()[0], 3e-5); Assert.assertEquals(-3, optimum.getPointRef()[1], 4e-4); Assert.assertTrue(optimizer.getEvaluations() > 60); Assert.assertTrue(optimizer.getEvaluations() < 80); Assert.assertTrue(optimum.getValue() < 1.0e-6); } @Test public void testLeastSquares2() { final RealMatrix factors = new Array2DRowRealMatrix(new double[][] { { 1, 0 }, { 0, 1 } }, false); LeastSquaresConverter ls = new LeastSquaresConverter(new MultivariateVectorFunction() { public double[] value(double[] variables) { return factors.operate(variables); } }, new double[] { 2, -3 }, new double[] { 10, 0.1 }); SimplexOptimizer optimizer = new SimplexOptimizer(-1, 1e-6); optimizer.setSimplex(new NelderMeadSimplex(2)); PointValuePair optimum = optimizer.optimize(200, ls, GoalType.MINIMIZE, new double[] { 10, 10 }); Assert.assertEquals( 2, optimum.getPointRef()[0], 5e-5); Assert.assertEquals(-3, optimum.getPointRef()[1], 8e-4); Assert.assertTrue(optimizer.getEvaluations() > 60); Assert.assertTrue(optimizer.getEvaluations() < 80); Assert.assertTrue(optimum.getValue() < 1e-6); } @Test public void testLeastSquares3() { final RealMatrix factors = new Array2DRowRealMatrix(new double[][] { { 1, 0 }, { 0, 1 } }, false); LeastSquaresConverter ls = new LeastSquaresConverter(new MultivariateVectorFunction() { public double[] value(double[] variables) { return factors.operate(variables); } }, new double[] { 2, -3 }, new Array2DRowRealMatrix(new double [][] { { 1, 1.2 }, { 1.2, 2 } })); SimplexOptimizer optimizer = new SimplexOptimizer(-1, 1e-6); optimizer.setSimplex(new NelderMeadSimplex(2)); PointValuePair optimum = optimizer.optimize(200, ls, GoalType.MINIMIZE, new double[] { 10, 10 }); Assert.assertEquals( 2, optimum.getPointRef()[0], 2e-3); Assert.assertEquals(-3, optimum.getPointRef()[1], 8e-4); Assert.assertTrue(optimizer.getEvaluations() > 60); Assert.assertTrue(optimizer.getEvaluations() < 80); Assert.assertTrue(optimum.getValue() < 1e-6); } @Test(expected = TooManyEvaluationsException.class) public void testMaxIterations() { Powell powell = new Powell(); SimplexOptimizer optimizer = new SimplexOptimizer(-1, 1e-3); optimizer.setSimplex(new NelderMeadSimplex(4)); optimizer.optimize(20, powell, GoalType.MINIMIZE, new double[] { 3, -1, 0, 1 }); } private static class FourExtrema implements MultivariateFunction { // The following function has 4 local extrema. final double xM = -3.841947088256863675365; final double yM = -1.391745200270734924416; final double xP = 0.2286682237349059125691; final double yP = -yM; final double valueXmYm = 0.2373295333134216789769; // Local maximum. final double valueXmYp = -valueXmYm; // Local minimum. final double valueXpYm = -0.7290400707055187115322; // Global minimum. final double valueXpYp = -valueXpYm; // Global maximum. public double value(double[] variables) { final double x = variables[0]; final double y = variables[1]; return (x == 0 || y == 0) ? 0 : FastMath.atan(x) * FastMath.atan(x + 2) * FastMath.atan(y) * FastMath.atan(y) / (x * y); } } private static class Rosenbrock implements MultivariateFunction { private int count; public Rosenbrock() { count = 0; } public double value(double[] x) { ++count; double a = x[1] - x[0] * x[0]; double b = 1.0 - x[0]; return 100 * a * a + b * b; } public int getCount() { return count; } } private static class Powell implements MultivariateFunction { private int count; public Powell() { count = 0; } public double value(double[] x) { ++count; double a = x[0] + 10 * x[1]; double b = x[2] - x[3]; double c = x[1] - 2 * x[2]; double d = x[0] - x[3]; return a * a + 5 * b * b + c * c * c * c + 10 * d * d * d * d; } public int getCount() { return count; } } } ././@LongLink100644 0 0 175 12126630646 10264 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/optimization/direct/MultivariateFunctionPenaltyAdapterTest.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/optimization/direct/MultivariateFunctio100644 1750 1750 20276 12126627670 32530 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.optimization.direct; import org.apache.commons.math3.analysis.MultivariateFunction; import org.apache.commons.math3.optimization.GoalType; import org.apache.commons.math3.optimization.PointValuePair; import org.apache.commons.math3.optimization.SimplePointChecker; import org.junit.Assert; import org.junit.Test; public class MultivariateFunctionPenaltyAdapterTest { @Test public void testStartSimplexInsideRange() { final BiQuadratic biQuadratic = new BiQuadratic(2.0, 2.5, 1.0, 3.0, 2.0, 3.0); final MultivariateFunctionPenaltyAdapter wrapped = new MultivariateFunctionPenaltyAdapter(biQuadratic, biQuadratic.getLower(), biQuadratic.getUpper(), 1000.0, new double[] { 100.0, 100.0 }); SimplexOptimizer optimizer = new SimplexOptimizer(1e-10, 1e-30); optimizer.setSimplex(new NelderMeadSimplex(new double[] { 1.0, 0.5 })); final PointValuePair optimum = optimizer.optimize(300, wrapped, GoalType.MINIMIZE, new double[] { 1.5, 2.25 }); Assert.assertEquals(biQuadratic.getBoundedXOptimum(), optimum.getPoint()[0], 2e-7); Assert.assertEquals(biQuadratic.getBoundedYOptimum(), optimum.getPoint()[1], 2e-7); } @Test public void testStartSimplexOutsideRange() { final BiQuadratic biQuadratic = new BiQuadratic(2.0, 2.5, 1.0, 3.0, 2.0, 3.0); final MultivariateFunctionPenaltyAdapter wrapped = new MultivariateFunctionPenaltyAdapter(biQuadratic, biQuadratic.getLower(), biQuadratic.getUpper(), 1000.0, new double[] { 100.0, 100.0 }); SimplexOptimizer optimizer = new SimplexOptimizer(1e-10, 1e-30); optimizer.setSimplex(new NelderMeadSimplex(new double[] { 1.0, 0.5 })); final PointValuePair optimum = optimizer.optimize(300, wrapped, GoalType.MINIMIZE, new double[] { -1.5, 4.0 }); Assert.assertEquals(biQuadratic.getBoundedXOptimum(), optimum.getPoint()[0], 2e-7); Assert.assertEquals(biQuadratic.getBoundedYOptimum(), optimum.getPoint()[1], 2e-7); } @Test public void testOptimumOutsideRange() { final BiQuadratic biQuadratic = new BiQuadratic(4.0, 0.0, 1.0, 3.0, 2.0, 3.0); final MultivariateFunctionPenaltyAdapter wrapped = new MultivariateFunctionPenaltyAdapter(biQuadratic, biQuadratic.getLower(), biQuadratic.getUpper(), 1000.0, new double[] { 100.0, 100.0 }); SimplexOptimizer optimizer = new SimplexOptimizer(new SimplePointChecker(1.0e-11, 1.0e-20)); optimizer.setSimplex(new NelderMeadSimplex(new double[] { 1.0, 0.5 })); final PointValuePair optimum = optimizer.optimize(600, wrapped, GoalType.MINIMIZE, new double[] { -1.5, 4.0 }); Assert.assertEquals(biQuadratic.getBoundedXOptimum(), optimum.getPoint()[0], 2e-7); Assert.assertEquals(biQuadratic.getBoundedYOptimum(), optimum.getPoint()[1], 2e-7); } @Test public void testUnbounded() { final BiQuadratic biQuadratic = new BiQuadratic(4.0, 0.0, Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY, Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY); final MultivariateFunctionPenaltyAdapter wrapped = new MultivariateFunctionPenaltyAdapter(biQuadratic, biQuadratic.getLower(), biQuadratic.getUpper(), 1000.0, new double[] { 100.0, 100.0 }); SimplexOptimizer optimizer = new SimplexOptimizer(1e-10, 1e-30); optimizer.setSimplex(new NelderMeadSimplex(new double[] { 1.0, 0.5 })); final PointValuePair optimum = optimizer.optimize(300, wrapped, GoalType.MINIMIZE, new double[] { -1.5, 4.0 }); Assert.assertEquals(biQuadratic.getBoundedXOptimum(), optimum.getPoint()[0], 2e-7); Assert.assertEquals(biQuadratic.getBoundedYOptimum(), optimum.getPoint()[1], 2e-7); } @Test public void testHalfBounded() { final BiQuadratic biQuadratic = new BiQuadratic(4.0, 4.0, 1.0, Double.POSITIVE_INFINITY, Double.NEGATIVE_INFINITY, 3.0); final MultivariateFunctionPenaltyAdapter wrapped = new MultivariateFunctionPenaltyAdapter(biQuadratic, biQuadratic.getLower(), biQuadratic.getUpper(), 1000.0, new double[] { 100.0, 100.0 }); SimplexOptimizer optimizer = new SimplexOptimizer(new SimplePointChecker(1.0e-10, 1.0e-20)); optimizer.setSimplex(new NelderMeadSimplex(new double[] { 1.0, 0.5 })); final PointValuePair optimum = optimizer.optimize(400, wrapped, GoalType.MINIMIZE, new double[] { -1.5, 4.0 }); Assert.assertEquals(biQuadratic.getBoundedXOptimum(), optimum.getPoint()[0], 2e-7); Assert.assertEquals(biQuadratic.getBoundedYOptimum(), optimum.getPoint()[1], 2e-7); } private static class BiQuadratic implements MultivariateFunction { private final double xOptimum; private final double yOptimum; private final double xMin; private final double xMax; private final double yMin; private final double yMax; public BiQuadratic(final double xOptimum, final double yOptimum, final double xMin, final double xMax, final double yMin, final double yMax) { this.xOptimum = xOptimum; this.yOptimum = yOptimum; this.xMin = xMin; this.xMax = xMax; this.yMin = yMin; this.yMax = yMax; } public double value(double[] point) { // the function should never be called with out of range points Assert.assertTrue(point[0] >= xMin); Assert.assertTrue(point[0] <= xMax); Assert.assertTrue(point[1] >= yMin); Assert.assertTrue(point[1] <= yMax); final double dx = point[0] - xOptimum; final double dy = point[1] - yOptimum; return dx * dx + dy * dy; } public double[] getLower() { return new double[] { xMin, yMin }; } public double[] getUpper() { return new double[] { xMax, yMax }; } public double getBoundedXOptimum() { return (xOptimum < xMin) ? xMin : ((xOptimum > xMax) ? xMax : xOptimum); } public double getBoundedYOptimum() { return (yOptimum < yMin) ? yMin : ((yOptimum > yMax) ? yMax : yOptimum); } } } ././@LongLink100644 0 0 152 12126630646 10257 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/optimization/direct/BOBYQAOptimizerTest.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/optimization/direct/BOBYQAOptimizerTest100644 1750 1750 50666 12126627670 32260 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.optimization.direct; import java.util.Arrays; import java.util.Random; import org.apache.commons.math3.analysis.MultivariateFunction; import org.apache.commons.math3.exception.DimensionMismatchException; import org.apache.commons.math3.exception.TooManyEvaluationsException; import org.apache.commons.math3.exception.NumberIsTooLargeException; import org.apache.commons.math3.exception.NumberIsTooSmallException; import org.apache.commons.math3.optimization.GoalType; import org.apache.commons.math3.optimization.PointValuePair; import org.apache.commons.math3.optimization.InitialGuess; import org.apache.commons.math3.optimization.SimpleBounds; import org.junit.Assert; import org.junit.Ignore; import org.junit.Test; /** * Test for {@link BOBYQAOptimizer}. */ public class BOBYQAOptimizerTest { static final int DIM = 13; @Test(expected=NumberIsTooLargeException.class) public void testInitOutOfBounds() { double[] startPoint = point(DIM, 3); double[][] boundaries = boundaries(DIM, -1, 2); doTest(new Rosen(), startPoint, boundaries, GoalType.MINIMIZE, 1e-13, 1e-6, 2000, null); } @Test(expected=DimensionMismatchException.class) public void testBoundariesDimensionMismatch() { double[] startPoint = point(DIM, 0.5); double[][] boundaries = boundaries(DIM + 1, -1, 2); doTest(new Rosen(), startPoint, boundaries, GoalType.MINIMIZE, 1e-13, 1e-6, 2000, null); } @Test(expected=NumberIsTooSmallException.class) public void testProblemDimensionTooSmall() { double[] startPoint = point(1, 0.5); doTest(new Rosen(), startPoint, null, GoalType.MINIMIZE, 1e-13, 1e-6, 2000, null); } @Test(expected=TooManyEvaluationsException.class) public void testMaxEvaluations() { final int lowMaxEval = 2; double[] startPoint = point(DIM, 0.1); double[][] boundaries = null; doTest(new Rosen(), startPoint, boundaries, GoalType.MINIMIZE, 1e-13, 1e-6, lowMaxEval, null); } @Test public void testRosen() { double[] startPoint = point(DIM,0.1); double[][] boundaries = null; PointValuePair expected = new PointValuePair(point(DIM,1.0),0.0); doTest(new Rosen(), startPoint, boundaries, GoalType.MINIMIZE, 1e-13, 1e-6, 2000, expected); } @Test public void testMaximize() { double[] startPoint = point(DIM,1.0); double[][] boundaries = null; PointValuePair expected = new PointValuePair(point(DIM,0.0),1.0); doTest(new MinusElli(), startPoint, boundaries, GoalType.MAXIMIZE, 2e-10, 5e-6, 1000, expected); boundaries = boundaries(DIM,-0.3,0.3); startPoint = point(DIM,0.1); doTest(new MinusElli(), startPoint, boundaries, GoalType.MAXIMIZE, 2e-10, 5e-6, 1000, expected); } @Test public void testEllipse() { double[] startPoint = point(DIM,1.0); double[][] boundaries = null; PointValuePair expected = new PointValuePair(point(DIM,0.0),0.0); doTest(new Elli(), startPoint, boundaries, GoalType.MINIMIZE, 1e-13, 1e-6, 1000, expected); } @Test public void testElliRotated() { double[] startPoint = point(DIM,1.0); double[][] boundaries = null; PointValuePair expected = new PointValuePair(point(DIM,0.0),0.0); doTest(new ElliRotated(), startPoint, boundaries, GoalType.MINIMIZE, 1e-12, 1e-6, 10000, expected); } @Test public void testCigar() { double[] startPoint = point(DIM,1.0); double[][] boundaries = null; PointValuePair expected = new PointValuePair(point(DIM,0.0),0.0); doTest(new Cigar(), startPoint, boundaries, GoalType.MINIMIZE, 1e-13, 1e-6, 100, expected); } @Test public void testTwoAxes() { double[] startPoint = point(DIM,1.0); double[][] boundaries = null; PointValuePair expected = new PointValuePair(point(DIM,0.0),0.0); doTest(new TwoAxes(), startPoint, boundaries, GoalType.MINIMIZE, 2* 1e-13, 1e-6, 100, expected); } @Test public void testCigTab() { double[] startPoint = point(DIM,1.0); double[][] boundaries = null; PointValuePair expected = new PointValuePair(point(DIM,0.0),0.0); doTest(new CigTab(), startPoint, boundaries, GoalType.MINIMIZE, 1e-13, 5e-5, 100, expected); } @Test public void testSphere() { double[] startPoint = point(DIM,1.0); double[][] boundaries = null; PointValuePair expected = new PointValuePair(point(DIM,0.0),0.0); doTest(new Sphere(), startPoint, boundaries, GoalType.MINIMIZE, 1e-13, 1e-6, 100, expected); } @Test public void testTablet() { double[] startPoint = point(DIM,1.0); double[][] boundaries = null; PointValuePair expected = new PointValuePair(point(DIM,0.0),0.0); doTest(new Tablet(), startPoint, boundaries, GoalType.MINIMIZE, 1e-13, 1e-6, 100, expected); } @Test public void testDiffPow() { double[] startPoint = point(DIM/2,1.0); double[][] boundaries = null; PointValuePair expected = new PointValuePair(point(DIM/2,0.0),0.0); doTest(new DiffPow(), startPoint, boundaries, GoalType.MINIMIZE, 1e-8, 1e-1, 12000, expected); } @Test public void testSsDiffPow() { double[] startPoint = point(DIM/2,1.0); double[][] boundaries = null; PointValuePair expected = new PointValuePair(point(DIM/2,0.0),0.0); doTest(new SsDiffPow(), startPoint, boundaries, GoalType.MINIMIZE, 1e-2, 1.3e-1, 50000, expected); } @Test public void testAckley() { double[] startPoint = point(DIM,0.1); double[][] boundaries = null; PointValuePair expected = new PointValuePair(point(DIM,0.0),0.0); doTest(new Ackley(), startPoint, boundaries, GoalType.MINIMIZE, 1e-8, 1e-5, 1000, expected); } @Test public void testRastrigin() { double[] startPoint = point(DIM,1.0); double[][] boundaries = null; PointValuePair expected = new PointValuePair(point(DIM,0.0),0.0); doTest(new Rastrigin(), startPoint, boundaries, GoalType.MINIMIZE, 1e-13, 1e-6, 1000, expected); } @Test public void testConstrainedRosen() { double[] startPoint = point(DIM,0.1); double[][] boundaries = boundaries(DIM,-1,2); PointValuePair expected = new PointValuePair(point(DIM,1.0),0.0); doTest(new Rosen(), startPoint, boundaries, GoalType.MINIMIZE, 1e-13, 1e-6, 2000, expected); } // See MATH-728 // TODO: this test is temporarily disabled for 3.2 release as a bug in Cobertura // makes it run for several hours before completing @Ignore @Test public void testConstrainedRosenWithMoreInterpolationPoints() { final double[] startPoint = point(DIM, 0.1); final double[][] boundaries = boundaries(DIM, -1, 2); final PointValuePair expected = new PointValuePair(point(DIM, 1.0), 0.0); // This should have been 78 because in the code the hard limit is // said to be // ((DIM + 1) * (DIM + 2)) / 2 - (2 * DIM + 1) // i.e. 78 in this case, but the test fails for 48, 59, 62, 63, 64, // 65, 66, ... final int maxAdditionalPoints = 47; for (int num = 1; num <= maxAdditionalPoints; num++) { doTest(new Rosen(), startPoint, boundaries, GoalType.MINIMIZE, 1e-12, 1e-6, 2000, num, expected, "num=" + num); } } /** * @param func Function to optimize. * @param startPoint Starting point. * @param boundaries Upper / lower point limit. * @param goal Minimization or maximization. * @param fTol Tolerance relative error on the objective function. * @param pointTol Tolerance for checking that the optimum is correct. * @param maxEvaluations Maximum number of evaluations. * @param expected Expected point / value. */ private void doTest(MultivariateFunction func, double[] startPoint, double[][] boundaries, GoalType goal, double fTol, double pointTol, int maxEvaluations, PointValuePair expected) { doTest(func, startPoint, boundaries, goal, fTol, pointTol, maxEvaluations, 0, expected, ""); } /** * @param func Function to optimize. * @param startPoint Starting point. * @param boundaries Upper / lower point limit. * @param goal Minimization or maximization. * @param fTol Tolerance relative error on the objective function. * @param pointTol Tolerance for checking that the optimum is correct. * @param maxEvaluations Maximum number of evaluations. * @param additionalInterpolationPoints Number of interpolation to used * in addition to the default (2 * dim + 1). * @param expected Expected point / value. */ private void doTest(MultivariateFunction func, double[] startPoint, double[][] boundaries, GoalType goal, double fTol, double pointTol, int maxEvaluations, int additionalInterpolationPoints, PointValuePair expected, String assertMsg) { // System.out.println(func.getClass().getName() + " BEGIN"); // XXX int dim = startPoint.length; // MultivariateOptimizer optim = // new PowellOptimizer(1e-13, Math.ulp(1d)); // PointValuePair result = optim.optimize(100000, func, goal, startPoint); final double[] lB = boundaries == null ? null : boundaries[0]; final double[] uB = boundaries == null ? null : boundaries[1]; final int numIterpolationPoints = 2 * dim + 1 + additionalInterpolationPoints; BOBYQAOptimizer optim = new BOBYQAOptimizer(numIterpolationPoints); PointValuePair result = boundaries == null ? optim.optimize(maxEvaluations, func, goal, new InitialGuess(startPoint)) : optim.optimize(maxEvaluations, func, goal, new InitialGuess(startPoint), new SimpleBounds(lB, uB)); // System.out.println(func.getClass().getName() + " = " // + optim.getEvaluations() + " f("); // for (double x: result.getPoint()) System.out.print(x + " "); // System.out.println(") = " + result.getValue()); Assert.assertEquals(assertMsg, expected.getValue(), result.getValue(), fTol); for (int i = 0; i < dim; i++) { Assert.assertEquals(expected.getPoint()[i], result.getPoint()[i], pointTol); } // System.out.println(func.getClass().getName() + " END"); // XXX } private static double[] point(int n, double value) { double[] ds = new double[n]; Arrays.fill(ds, value); return ds; } private static double[][] boundaries(int dim, double lower, double upper) { double[][] boundaries = new double[2][dim]; for (int i = 0; i < dim; i++) boundaries[0][i] = lower; for (int i = 0; i < dim; i++) boundaries[1][i] = upper; return boundaries; } private static class Sphere implements MultivariateFunction { public double value(double[] x) { double f = 0; for (int i = 0; i < x.length; ++i) f += x[i] * x[i]; return f; } } private static class Cigar implements MultivariateFunction { private double factor; Cigar() { this(1e3); } Cigar(double axisratio) { factor = axisratio * axisratio; } public double value(double[] x) { double f = x[0] * x[0]; for (int i = 1; i < x.length; ++i) f += factor * x[i] * x[i]; return f; } } private static class Tablet implements MultivariateFunction { private double factor; Tablet() { this(1e3); } Tablet(double axisratio) { factor = axisratio * axisratio; } public double value(double[] x) { double f = factor * x[0] * x[0]; for (int i = 1; i < x.length; ++i) f += x[i] * x[i]; return f; } } private static class CigTab implements MultivariateFunction { private double factor; CigTab() { this(1e4); } CigTab(double axisratio) { factor = axisratio; } public double value(double[] x) { int end = x.length - 1; double f = x[0] * x[0] / factor + factor * x[end] * x[end]; for (int i = 1; i < end; ++i) f += x[i] * x[i]; return f; } } private static class TwoAxes implements MultivariateFunction { private double factor; TwoAxes() { this(1e6); } TwoAxes(double axisratio) { factor = axisratio * axisratio; } public double value(double[] x) { double f = 0; for (int i = 0; i < x.length; ++i) f += (i < x.length / 2 ? factor : 1) * x[i] * x[i]; return f; } } private static class ElliRotated implements MultivariateFunction { private Basis B = new Basis(); private double factor; ElliRotated() { this(1e3); } ElliRotated(double axisratio) { factor = axisratio * axisratio; } public double value(double[] x) { double f = 0; x = B.Rotate(x); for (int i = 0; i < x.length; ++i) f += Math.pow(factor, i / (x.length - 1.)) * x[i] * x[i]; return f; } } private static class Elli implements MultivariateFunction { private double factor; Elli() { this(1e3); } Elli(double axisratio) { factor = axisratio * axisratio; } public double value(double[] x) { double f = 0; for (int i = 0; i < x.length; ++i) f += Math.pow(factor, i / (x.length - 1.)) * x[i] * x[i]; return f; } } private static class MinusElli implements MultivariateFunction { private final Elli elli = new Elli(); public double value(double[] x) { return 1.0 - elli.value(x); } } private static class DiffPow implements MultivariateFunction { // private int fcount = 0; public double value(double[] x) { double f = 0; for (int i = 0; i < x.length; ++i) f += Math.pow(Math.abs(x[i]), 2. + 10 * (double) i / (x.length - 1.)); // System.out.print("" + (fcount++) + ") "); // for (int i = 0; i < x.length; i++) // System.out.print(x[i] + " "); // System.out.println(" = " + f); return f; } } private static class SsDiffPow implements MultivariateFunction { public double value(double[] x) { double f = Math.pow(new DiffPow().value(x), 0.25); return f; } } private static class Rosen implements MultivariateFunction { public double value(double[] x) { double f = 0; for (int i = 0; i < x.length - 1; ++i) f += 1e2 * (x[i] * x[i] - x[i + 1]) * (x[i] * x[i] - x[i + 1]) + (x[i] - 1.) * (x[i] - 1.); return f; } } private static class Ackley implements MultivariateFunction { private double axisratio; Ackley(double axra) { axisratio = axra; } public Ackley() { this(1); } public double value(double[] x) { double f = 0; double res2 = 0; double fac = 0; for (int i = 0; i < x.length; ++i) { fac = Math.pow(axisratio, (i - 1.) / (x.length - 1.)); f += fac * fac * x[i] * x[i]; res2 += Math.cos(2. * Math.PI * fac * x[i]); } f = (20. - 20. * Math.exp(-0.2 * Math.sqrt(f / x.length)) + Math.exp(1.) - Math.exp(res2 / x.length)); return f; } } private static class Rastrigin implements MultivariateFunction { private double axisratio; private double amplitude; Rastrigin() { this(1, 10); } Rastrigin(double axisratio, double amplitude) { this.axisratio = axisratio; this.amplitude = amplitude; } public double value(double[] x) { double f = 0; double fac; for (int i = 0; i < x.length; ++i) { fac = Math.pow(axisratio, (i - 1.) / (x.length - 1.)); if (i == 0 && x[i] < 0) fac *= 1.; f += fac * fac * x[i] * x[i] + amplitude * (1. - Math.cos(2. * Math.PI * fac * x[i])); } return f; } } private static class Basis { double[][] basis; Random rand = new Random(2); // use not always the same basis double[] Rotate(double[] x) { GenBasis(x.length); double[] y = new double[x.length]; for (int i = 0; i < x.length; ++i) { y[i] = 0; for (int j = 0; j < x.length; ++j) y[i] += basis[i][j] * x[j]; } return y; } void GenBasis(int DIM) { if (basis != null ? basis.length == DIM : false) return; double sp; int i, j, k; /* generate orthogonal basis */ basis = new double[DIM][DIM]; for (i = 0; i < DIM; ++i) { /* sample components gaussian */ for (j = 0; j < DIM; ++j) basis[i][j] = rand.nextGaussian(); /* substract projection of previous vectors */ for (j = i - 1; j >= 0; --j) { for (sp = 0., k = 0; k < DIM; ++k) sp += basis[i][k] * basis[j][k]; /* scalar product */ for (k = 0; k < DIM; ++k) basis[i][k] -= sp * basis[j][k]; /* substract */ } /* normalize */ for (sp = 0., k = 0; k < DIM; ++k) sp += basis[i][k] * basis[i][k]; /* squared norm */ for (k = 0; k < DIM; ++k) basis[i][k] /= Math.sqrt(sp); } } } } ././@LongLink100644 0 0 173 12126630646 10262 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/optimization/direct/SimplexOptimizerMultiDirectionalTest.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/optimization/direct/SimplexOptimizerMul100644 1750 1750 21605 12126627670 32531 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.optimization.direct; import org.apache.commons.math3.analysis.MultivariateFunction; import org.apache.commons.math3.optimization.GoalType; import org.apache.commons.math3.optimization.PointValuePair; import org.apache.commons.math3.optimization.SimpleValueChecker; import org.apache.commons.math3.util.FastMath; import org.junit.Assert; import org.junit.Test; public class SimplexOptimizerMultiDirectionalTest { @Test public void testMinimize1() { SimplexOptimizer optimizer = new SimplexOptimizer(1e-11, 1e-30); optimizer.setSimplex(new MultiDirectionalSimplex(new double[] { 0.2, 0.2 })); final FourExtrema fourExtrema = new FourExtrema(); final PointValuePair optimum = optimizer.optimize(200, fourExtrema, GoalType.MINIMIZE, new double[] { -3, 0 }); Assert.assertEquals(fourExtrema.xM, optimum.getPoint()[0], 4e-6); Assert.assertEquals(fourExtrema.yP, optimum.getPoint()[1], 3e-6); Assert.assertEquals(fourExtrema.valueXmYp, optimum.getValue(), 8e-13); Assert.assertTrue(optimizer.getEvaluations() > 120); Assert.assertTrue(optimizer.getEvaluations() < 150); } @Test public void testMinimize2() { SimplexOptimizer optimizer = new SimplexOptimizer(1e-11, 1e-30); optimizer.setSimplex(new MultiDirectionalSimplex(new double[] { 0.2, 0.2 })); final FourExtrema fourExtrema = new FourExtrema(); final PointValuePair optimum = optimizer.optimize(200, fourExtrema, GoalType.MINIMIZE, new double[] { 1, 0 }); Assert.assertEquals(fourExtrema.xP, optimum.getPoint()[0], 2e-8); Assert.assertEquals(fourExtrema.yM, optimum.getPoint()[1], 3e-6); Assert.assertEquals(fourExtrema.valueXpYm, optimum.getValue(), 2e-12); Assert.assertTrue(optimizer.getEvaluations() > 120); Assert.assertTrue(optimizer.getEvaluations() < 150); } @Test public void testMaximize1() { SimplexOptimizer optimizer = new SimplexOptimizer(1e-11, 1e-30); optimizer.setSimplex(new MultiDirectionalSimplex(new double[] { 0.2, 0.2 })); final FourExtrema fourExtrema = new FourExtrema(); final PointValuePair optimum = optimizer.optimize(200, fourExtrema, GoalType.MAXIMIZE, new double[] { -3.0, 0.0 }); Assert.assertEquals(fourExtrema.xM, optimum.getPoint()[0], 7e-7); Assert.assertEquals(fourExtrema.yM, optimum.getPoint()[1], 3e-7); Assert.assertEquals(fourExtrema.valueXmYm, optimum.getValue(), 2e-14); Assert.assertTrue(optimizer.getEvaluations() > 120); Assert.assertTrue(optimizer.getEvaluations() < 150); } @Test public void testMaximize2() { SimplexOptimizer optimizer = new SimplexOptimizer(new SimpleValueChecker(1e-15, 1e-30)); optimizer.setSimplex(new MultiDirectionalSimplex(new double[] { 0.2, 0.2 })); final FourExtrema fourExtrema = new FourExtrema(); final PointValuePair optimum = optimizer.optimize(200, fourExtrema, GoalType.MAXIMIZE, new double[] { 1, 0 }); Assert.assertEquals(fourExtrema.xP, optimum.getPoint()[0], 2e-8); Assert.assertEquals(fourExtrema.yP, optimum.getPoint()[1], 3e-6); Assert.assertEquals(fourExtrema.valueXpYp, optimum.getValue(), 2e-12); Assert.assertTrue(optimizer.getEvaluations() > 180); Assert.assertTrue(optimizer.getEvaluations() < 220); } @Test public void testRosenbrock() { MultivariateFunction rosenbrock = new MultivariateFunction() { public double value(double[] x) { ++count; double a = x[1] - x[0] * x[0]; double b = 1.0 - x[0]; return 100 * a * a + b * b; } }; count = 0; SimplexOptimizer optimizer = new SimplexOptimizer(-1, 1e-3); optimizer.setSimplex(new MultiDirectionalSimplex(new double[][] { { -1.2, 1.0 }, { 0.9, 1.2 } , { 3.5, -2.3 } })); PointValuePair optimum = optimizer.optimize(100, rosenbrock, GoalType.MINIMIZE, new double[] { -1.2, 1 }); Assert.assertEquals(count, optimizer.getEvaluations()); Assert.assertTrue(optimizer.getEvaluations() > 50); Assert.assertTrue(optimizer.getEvaluations() < 100); Assert.assertTrue(optimum.getValue() > 1e-2); } @Test public void testPowell() { MultivariateFunction powell = new MultivariateFunction() { public double value(double[] x) { ++count; double a = x[0] + 10 * x[1]; double b = x[2] - x[3]; double c = x[1] - 2 * x[2]; double d = x[0] - x[3]; return a * a + 5 * b * b + c * c * c * c + 10 * d * d * d * d; } }; count = 0; SimplexOptimizer optimizer = new SimplexOptimizer(-1, 1e-3); optimizer.setSimplex(new MultiDirectionalSimplex(4)); PointValuePair optimum = optimizer.optimize(1000, powell, GoalType.MINIMIZE, new double[] { 3, -1, 0, 1 }); Assert.assertEquals(count, optimizer.getEvaluations()); Assert.assertTrue(optimizer.getEvaluations() > 800); Assert.assertTrue(optimizer.getEvaluations() < 900); Assert.assertTrue(optimum.getValue() > 1e-2); } @Test public void testMath283() { // fails because MultiDirectional.iterateSimplex is looping forever // the while(true) should be replaced with a convergence check SimplexOptimizer optimizer = new SimplexOptimizer(1e-14, 1e-14); optimizer.setSimplex(new MultiDirectionalSimplex(2)); final Gaussian2D function = new Gaussian2D(0, 0, 1); PointValuePair estimate = optimizer.optimize(1000, function, GoalType.MAXIMIZE, function.getMaximumPosition()); final double EPSILON = 1e-5; final double expectedMaximum = function.getMaximum(); final double actualMaximum = estimate.getValue(); Assert.assertEquals(expectedMaximum, actualMaximum, EPSILON); final double[] expectedPosition = function.getMaximumPosition(); final double[] actualPosition = estimate.getPoint(); Assert.assertEquals(expectedPosition[0], actualPosition[0], EPSILON ); Assert.assertEquals(expectedPosition[1], actualPosition[1], EPSILON ); } private static class FourExtrema implements MultivariateFunction { // The following function has 4 local extrema. final double xM = -3.841947088256863675365; final double yM = -1.391745200270734924416; final double xP = 0.2286682237349059125691; final double yP = -yM; final double valueXmYm = 0.2373295333134216789769; // Local maximum. final double valueXmYp = -valueXmYm; // Local minimum. final double valueXpYm = -0.7290400707055187115322; // Global minimum. final double valueXpYp = -valueXpYm; // Global maximum. public double value(double[] variables) { final double x = variables[0]; final double y = variables[1]; return (x == 0 || y == 0) ? 0 : FastMath.atan(x) * FastMath.atan(x + 2) * FastMath.atan(y) * FastMath.atan(y) / (x * y); } } private static class Gaussian2D implements MultivariateFunction { private final double[] maximumPosition; private final double std; public Gaussian2D(double xOpt, double yOpt, double std) { maximumPosition = new double[] { xOpt, yOpt }; this.std = std; } public double getMaximum() { return value(maximumPosition); } public double[] getMaximumPosition() { return maximumPosition.clone(); } public double value(double[] point) { final double x = point[0], y = point[1]; final double twoS2 = 2.0 * std * std; return 1.0 / (twoS2 * FastMath.PI) * FastMath.exp(-(x * x + y * y) / twoS2); } } private int count; } ././@LongLink100644 0 0 175 12126630646 10264 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/optimization/direct/MultivariateFunctionMappingAdapterTest.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/optimization/direct/MultivariateFunctio100644 1750 1750 17662 12126627670 32535 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.optimization.direct; import org.apache.commons.math3.analysis.MultivariateFunction; import org.apache.commons.math3.optimization.GoalType; import org.apache.commons.math3.optimization.PointValuePair; import org.junit.Assert; import org.junit.Test; public class MultivariateFunctionMappingAdapterTest { @Test public void testStartSimplexInsideRange() { final BiQuadratic biQuadratic = new BiQuadratic(2.0, 2.5, 1.0, 3.0, 2.0, 3.0); final MultivariateFunctionMappingAdapter wrapped = new MultivariateFunctionMappingAdapter(biQuadratic, biQuadratic.getLower(), biQuadratic.getUpper()); SimplexOptimizer optimizer = new SimplexOptimizer(1e-10, 1e-30); optimizer.setSimplex(new NelderMeadSimplex(new double[][] { wrapped.boundedToUnbounded(new double[] { 1.5, 2.75 }), wrapped.boundedToUnbounded(new double[] { 1.5, 2.95 }), wrapped.boundedToUnbounded(new double[] { 1.7, 2.90 }) })); final PointValuePair optimum = optimizer.optimize(300, wrapped, GoalType.MINIMIZE, wrapped.boundedToUnbounded(new double[] { 1.5, 2.25 })); final double[] bounded = wrapped.unboundedToBounded(optimum.getPoint()); Assert.assertEquals(biQuadratic.getBoundedXOptimum(), bounded[0], 2e-7); Assert.assertEquals(biQuadratic.getBoundedYOptimum(), bounded[1], 2e-7); } @Test public void testOptimumOutsideRange() { final BiQuadratic biQuadratic = new BiQuadratic(4.0, 0.0, 1.0, 3.0, 2.0, 3.0); final MultivariateFunctionMappingAdapter wrapped = new MultivariateFunctionMappingAdapter(biQuadratic, biQuadratic.getLower(), biQuadratic.getUpper()); SimplexOptimizer optimizer = new SimplexOptimizer(1e-10, 1e-30); optimizer.setSimplex(new NelderMeadSimplex(new double[][] { wrapped.boundedToUnbounded(new double[] { 1.5, 2.75 }), wrapped.boundedToUnbounded(new double[] { 1.5, 2.95 }), wrapped.boundedToUnbounded(new double[] { 1.7, 2.90 }) })); final PointValuePair optimum = optimizer.optimize(100, wrapped, GoalType.MINIMIZE, wrapped.boundedToUnbounded(new double[] { 1.5, 2.25 })); final double[] bounded = wrapped.unboundedToBounded(optimum.getPoint()); Assert.assertEquals(biQuadratic.getBoundedXOptimum(), bounded[0], 2e-7); Assert.assertEquals(biQuadratic.getBoundedYOptimum(), bounded[1], 2e-7); } @Test public void testUnbounded() { final BiQuadratic biQuadratic = new BiQuadratic(4.0, 0.0, Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY, Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY); final MultivariateFunctionMappingAdapter wrapped = new MultivariateFunctionMappingAdapter(biQuadratic, biQuadratic.getLower(), biQuadratic.getUpper()); SimplexOptimizer optimizer = new SimplexOptimizer(1e-10, 1e-30); optimizer.setSimplex(new NelderMeadSimplex(new double[][] { wrapped.boundedToUnbounded(new double[] { 1.5, 2.75 }), wrapped.boundedToUnbounded(new double[] { 1.5, 2.95 }), wrapped.boundedToUnbounded(new double[] { 1.7, 2.90 }) })); final PointValuePair optimum = optimizer.optimize(300, wrapped, GoalType.MINIMIZE, wrapped.boundedToUnbounded(new double[] { 1.5, 2.25 })); final double[] bounded = wrapped.unboundedToBounded(optimum.getPoint()); Assert.assertEquals(biQuadratic.getBoundedXOptimum(), bounded[0], 2e-7); Assert.assertEquals(biQuadratic.getBoundedYOptimum(), bounded[1], 2e-7); } @Test public void testHalfBounded() { final BiQuadratic biQuadratic = new BiQuadratic(4.0, 4.0, 1.0, Double.POSITIVE_INFINITY, Double.NEGATIVE_INFINITY, 3.0); final MultivariateFunctionMappingAdapter wrapped = new MultivariateFunctionMappingAdapter(biQuadratic, biQuadratic.getLower(), biQuadratic.getUpper()); SimplexOptimizer optimizer = new SimplexOptimizer(1e-13, 1e-30); optimizer.setSimplex(new NelderMeadSimplex(new double[][] { wrapped.boundedToUnbounded(new double[] { 1.5, 2.75 }), wrapped.boundedToUnbounded(new double[] { 1.5, 2.95 }), wrapped.boundedToUnbounded(new double[] { 1.7, 2.90 }) })); final PointValuePair optimum = optimizer.optimize(200, wrapped, GoalType.MINIMIZE, wrapped.boundedToUnbounded(new double[] { 1.5, 2.25 })); final double[] bounded = wrapped.unboundedToBounded(optimum.getPoint()); Assert.assertEquals(biQuadratic.getBoundedXOptimum(), bounded[0], 1e-7); Assert.assertEquals(biQuadratic.getBoundedYOptimum(), bounded[1], 1e-7); } private static class BiQuadratic implements MultivariateFunction { private final double xOptimum; private final double yOptimum; private final double xMin; private final double xMax; private final double yMin; private final double yMax; public BiQuadratic(final double xOptimum, final double yOptimum, final double xMin, final double xMax, final double yMin, final double yMax) { this.xOptimum = xOptimum; this.yOptimum = yOptimum; this.xMin = xMin; this.xMax = xMax; this.yMin = yMin; this.yMax = yMax; } public double value(double[] point) { // the function should never be called with out of range points Assert.assertTrue(point[0] >= xMin); Assert.assertTrue(point[0] <= xMax); Assert.assertTrue(point[1] >= yMin); Assert.assertTrue(point[1] <= yMax); final double dx = point[0] - xOptimum; final double dy = point[1] - yOptimum; return dx * dx + dy * dy; } public double[] getLower() { return new double[] { xMin, yMin }; } public double[] getUpper() { return new double[] { xMax, yMax }; } public double getBoundedXOptimum() { return (xOptimum < xMin) ? xMin : ((xOptimum > xMax) ? xMax : xOptimum); } public double getBoundedYOptimum() { return (yOptimum < yMin) ? yMin : ((yOptimum > yMax) ? yMax : yOptimum); } } } ././@LongLink100644 0 0 152 12126630646 10257 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/optimization/direct/PowellOptimizerTest.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/optimization/direct/PowellOptimizerTest100644 1750 1750 21177 12126627670 32540 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.optimization.direct; import org.apache.commons.math3.analysis.MultivariateFunction; import org.apache.commons.math3.analysis.SumSincFunction; import org.apache.commons.math3.optimization.GoalType; import org.apache.commons.math3.optimization.MultivariateOptimizer; import org.apache.commons.math3.optimization.PointValuePair; import org.apache.commons.math3.util.FastMath; import org.junit.Assert; import org.junit.Test; /** * Test for {@link PowellOptimizer}. */ public class PowellOptimizerTest { @Test public void testSumSinc() { final MultivariateFunction func = new SumSincFunction(-1); int dim = 2; final double[] minPoint = new double[dim]; for (int i = 0; i < dim; i++) { minPoint[i] = 0; } double[] init = new double[dim]; // Initial is minimum. for (int i = 0; i < dim; i++) { init[i] = minPoint[i]; } doTest(func, minPoint, init, GoalType.MINIMIZE, 1e-9, 1e-9); // Initial is far from minimum. for (int i = 0; i < dim; i++) { init[i] = minPoint[i] + 3; } doTest(func, minPoint, init, GoalType.MINIMIZE, 1e-9, 1e-5); // More stringent line search tolerance enhances the precision // of the result. doTest(func, minPoint, init, GoalType.MINIMIZE, 1e-9, 1e-9, 1e-7); } @Test public void testQuadratic() { final MultivariateFunction func = new MultivariateFunction() { public double value(double[] x) { final double a = x[0] - 1; final double b = x[1] - 1; return a * a + b * b + 1; } }; int dim = 2; final double[] minPoint = new double[dim]; for (int i = 0; i < dim; i++) { minPoint[i] = 1; } double[] init = new double[dim]; // Initial is minimum. for (int i = 0; i < dim; i++) { init[i] = minPoint[i]; } doTest(func, minPoint, init, GoalType.MINIMIZE, 1e-9, 1e-8); // Initial is far from minimum. for (int i = 0; i < dim; i++) { init[i] = minPoint[i] - 20; } doTest(func, minPoint, init, GoalType.MINIMIZE, 1e-9, 1e-8); } @Test public void testMaximizeQuadratic() { final MultivariateFunction func = new MultivariateFunction() { public double value(double[] x) { final double a = x[0] - 1; final double b = x[1] - 1; return -a * a - b * b + 1; } }; int dim = 2; final double[] maxPoint = new double[dim]; for (int i = 0; i < dim; i++) { maxPoint[i] = 1; } double[] init = new double[dim]; // Initial is minimum. for (int i = 0; i < dim; i++) { init[i] = maxPoint[i]; } doTest(func, maxPoint, init, GoalType.MAXIMIZE, 1e-9, 1e-8); // Initial is far from minimum. for (int i = 0; i < dim; i++) { init[i] = maxPoint[i] - 20; } doTest(func, maxPoint, init, GoalType.MAXIMIZE, 1e-9, 1e-8); } /** * Ensure that we do not increase the number of function evaluations when * the function values are scaled up. * Note that the tolerances parameters passed to the constructor must * still hold sensible values because they are used to set the line search * tolerances. */ @Test public void testRelativeToleranceOnScaledValues() { final MultivariateFunction func = new MultivariateFunction() { public double value(double[] x) { final double a = x[0] - 1; final double b = x[1] - 1; return a * a * FastMath.sqrt(FastMath.abs(a)) + b * b + 1; } }; int dim = 2; final double[] minPoint = new double[dim]; for (int i = 0; i < dim; i++) { minPoint[i] = 1; } double[] init = new double[dim]; // Initial is far from minimum. for (int i = 0; i < dim; i++) { init[i] = minPoint[i] - 20; } final double relTol = 1e-10; final int maxEval = 1000; // Very small absolute tolerance to rely solely on the relative // tolerance as a stopping criterion final MultivariateOptimizer optim = new PowellOptimizer(relTol, 1e-100); final PointValuePair funcResult = optim.optimize(maxEval, func, GoalType.MINIMIZE, init); final double funcValue = func.value(funcResult.getPoint()); final int funcEvaluations = optim.getEvaluations(); final double scale = 1e10; final MultivariateFunction funcScaled = new MultivariateFunction() { public double value(double[] x) { return scale * func.value(x); } }; final PointValuePair funcScaledResult = optim.optimize(maxEval, funcScaled, GoalType.MINIMIZE, init); final double funcScaledValue = funcScaled.value(funcScaledResult.getPoint()); final int funcScaledEvaluations = optim.getEvaluations(); // Check that both minima provide the same objective funciton values, // within the relative function tolerance. Assert.assertEquals(1, funcScaledValue / (scale * funcValue), relTol); // Check that the numbers of evaluations are the same. Assert.assertEquals(funcEvaluations, funcScaledEvaluations); } /** * @param func Function to optimize. * @param optimum Expected optimum. * @param init Starting point. * @param goal Minimization or maximization. * @param fTol Tolerance (relative error on the objective function) for * "Powell" algorithm. * @param pointTol Tolerance for checking that the optimum is correct. */ private void doTest(MultivariateFunction func, double[] optimum, double[] init, GoalType goal, double fTol, double pointTol) { final MultivariateOptimizer optim = new PowellOptimizer(fTol, Math.ulp(1d)); final PointValuePair result = optim.optimize(1000, func, goal, init); final double[] point = result.getPoint(); for (int i = 0, dim = optimum.length; i < dim; i++) { Assert.assertEquals("found[" + i + "]=" + point[i] + " value=" + result.getValue(), optimum[i], point[i], pointTol); } } /** * @param func Function to optimize. * @param optimum Expected optimum. * @param init Starting point. * @param goal Minimization or maximization. * @param fTol Tolerance (relative error on the objective function) for * "Powell" algorithm. * @param fLineTol Tolerance (relative error on the objective function) * for the internal line search algorithm. * @param pointTol Tolerance for checking that the optimum is correct. */ private void doTest(MultivariateFunction func, double[] optimum, double[] init, GoalType goal, double fTol, double fLineTol, double pointTol) { final MultivariateOptimizer optim = new PowellOptimizer(fTol, Math.ulp(1d), fLineTol, Math.ulp(1d)); final PointValuePair result = optim.optimize(1000, func, goal, init); final double[] point = result.getPoint(); for (int i = 0, dim = optimum.length; i < dim; i++) { Assert.assertEquals("found[" + i + "]=" + point[i] + " value=" + result.getValue(), optimum[i], point[i], pointTol); } } } commons-math3-3.2-src/src/test/java/org/apache/commons/math3/optimization/PointValuePairTest.java100644 1750 1750 2711 12126627671 31715 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.optimization; import org.apache.commons.math3.TestUtils; import org.junit.Assert; import org.junit.Test; public class PointValuePairTest { @Test public void testSerial() { PointValuePair pv1 = new PointValuePair(new double[] { 1.0, 2.0, 3.0 }, 4.0); PointValuePair pv2 = (PointValuePair) TestUtils.serializeAndRecover(pv1); Assert.assertEquals(pv1.getKey().length, pv2.getKey().length); for (int i = 0; i < pv1.getKey().length; ++i) { Assert.assertEquals(pv1.getKey()[i], pv2.getKey()[i], 1.0e-15); } Assert.assertEquals(pv1.getValue(), pv2.getValue(), 1.0e-15); } } ././@LongLink100644 0 0 147 12126630646 10263 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/optimization/fitting/CurveFitterTest.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/optimization/fitting/CurveFitterTest.ja100644 1750 1750 13536 12126627670 32420 0ustarlucluc 0 0 // Licensed to the Apache Software Foundation (ASF) under one // or more contributor license agreements. See the NOTICE file // distributed with this work for additional information // regarding copyright ownership. The ASF licenses this file // to you 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. package org.apache.commons.math3.optimization.fitting; import org.apache.commons.math3.optimization.general.LevenbergMarquardtOptimizer; import org.apache.commons.math3.analysis.ParametricUnivariateFunction; import org.apache.commons.math3.util.FastMath; import org.junit.Assert; import org.junit.Test; public class CurveFitterTest { @Test public void testMath303() { LevenbergMarquardtOptimizer optimizer = new LevenbergMarquardtOptimizer(); CurveFitter fitter = new CurveFitter(optimizer); fitter.addObservedPoint(2.805d, 0.6934785852953367d); fitter.addObservedPoint(2.74333333333333d, 0.6306772025518496d); fitter.addObservedPoint(1.655d, 0.9474675497289684); fitter.addObservedPoint(1.725d, 0.9013594835804194d); ParametricUnivariateFunction sif = new SimpleInverseFunction(); double[] initialguess1 = new double[1]; initialguess1[0] = 1.0d; Assert.assertEquals(1, fitter.fit(sif, initialguess1).length); double[] initialguess2 = new double[2]; initialguess2[0] = 1.0d; initialguess2[1] = .5d; Assert.assertEquals(2, fitter.fit(sif, initialguess2).length); } @Test public void testMath304() { LevenbergMarquardtOptimizer optimizer = new LevenbergMarquardtOptimizer(); CurveFitter fitter = new CurveFitter(optimizer); fitter.addObservedPoint(2.805d, 0.6934785852953367d); fitter.addObservedPoint(2.74333333333333d, 0.6306772025518496d); fitter.addObservedPoint(1.655d, 0.9474675497289684); fitter.addObservedPoint(1.725d, 0.9013594835804194d); ParametricUnivariateFunction sif = new SimpleInverseFunction(); double[] initialguess1 = new double[1]; initialguess1[0] = 1.0d; Assert.assertEquals(1.6357215104109237, fitter.fit(sif, initialguess1)[0], 1.0e-14); double[] initialguess2 = new double[1]; initialguess2[0] = 10.0d; Assert.assertEquals(1.6357215104109237, fitter.fit(sif, initialguess1)[0], 1.0e-14); } @Test public void testMath372() { LevenbergMarquardtOptimizer optimizer = new LevenbergMarquardtOptimizer(); CurveFitter curveFitter = new CurveFitter(optimizer); curveFitter.addObservedPoint( 15, 4443); curveFitter.addObservedPoint( 31, 8493); curveFitter.addObservedPoint( 62, 17586); curveFitter.addObservedPoint(125, 30582); curveFitter.addObservedPoint(250, 45087); curveFitter.addObservedPoint(500, 50683); ParametricUnivariateFunction f = new ParametricUnivariateFunction() { public double value(double x, double ... parameters) { double a = parameters[0]; double b = parameters[1]; double c = parameters[2]; double d = parameters[3]; return d + ((a - d) / (1 + FastMath.pow(x / c, b))); } public double[] gradient(double x, double ... parameters) { double a = parameters[0]; double b = parameters[1]; double c = parameters[2]; double d = parameters[3]; double[] gradients = new double[4]; double den = 1 + FastMath.pow(x / c, b); // derivative with respect to a gradients[0] = 1 / den; // derivative with respect to b // in the reported (invalid) issue, there was a sign error here gradients[1] = -((a - d) * FastMath.pow(x / c, b) * FastMath.log(x / c)) / (den * den); // derivative with respect to c gradients[2] = (b * FastMath.pow(x / c, b - 1) * (x / (c * c)) * (a - d)) / (den * den); // derivative with respect to d gradients[3] = 1 - (1 / den); return gradients; } }; double[] initialGuess = new double[] { 1500, 0.95, 65, 35000 }; double[] estimatedParameters = curveFitter.fit(f, initialGuess); Assert.assertEquals( 2411.00, estimatedParameters[0], 500.00); Assert.assertEquals( 1.62, estimatedParameters[1], 0.04); Assert.assertEquals( 111.22, estimatedParameters[2], 0.30); Assert.assertEquals(55347.47, estimatedParameters[3], 300.00); Assert.assertTrue(optimizer.getRMS() < 600.0); } private static class SimpleInverseFunction implements ParametricUnivariateFunction { public double value(double x, double ... parameters) { return parameters[0] / x + (parameters.length < 2 ? 0 : parameters[1]); } public double[] gradient(double x, double ... doubles) { double[] gradientVector = new double[doubles.length]; gradientVector[0] = 1 / x; if (doubles.length >= 2) { gradientVector[1] = 1; } return gradientVector; } } } ././@LongLink100644 0 0 152 12126630646 10257 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/optimization/fitting/GaussianFitterTest.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/optimization/fitting/GaussianFitterTest100644 1750 1750 30176 12126627670 32514 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.optimization.fitting; import org.apache.commons.math3.exception.MathIllegalArgumentException; import org.apache.commons.math3.optimization.general.LevenbergMarquardtOptimizer; import org.junit.Assert; import org.junit.Test; /** * Tests {@link GaussianFitter}. * * @since 2.2 * @version $Id: GaussianFitterTest.java 1349707 2012-06-13 09:30:56Z erans $ */ public class GaussianFitterTest { /** Good data. */ protected static final double[][] DATASET1 = new double[][] { {4.0254623, 531026.0}, {4.02804905, 664002.0}, {4.02934242, 787079.0}, {4.03128248, 984167.0}, {4.03386923, 1294546.0}, {4.03580929, 1560230.0}, {4.03839603, 1887233.0}, {4.0396894, 2113240.0}, {4.04162946, 2375211.0}, {4.04421621, 2687152.0}, {4.04550958, 2862644.0}, {4.04744964, 3078898.0}, {4.05003639, 3327238.0}, {4.05132976, 3461228.0}, {4.05326982, 3580526.0}, {4.05585657, 3576946.0}, {4.05779662, 3439750.0}, {4.06038337, 3220296.0}, {4.06167674, 3070073.0}, {4.0636168, 2877648.0}, {4.06620355, 2595848.0}, {4.06749692, 2390157.0}, {4.06943698, 2175960.0}, {4.07202373, 1895104.0}, {4.0733171, 1687576.0}, {4.07525716, 1447024.0}, {4.0778439, 1130879.0}, {4.07978396, 904900.0}, {4.08237071, 717104.0}, {4.08366408, 620014.0} }; /** Poor data: right of peak not symmetric with left of peak. */ protected static final double[][] DATASET2 = new double[][] { {-20.15, 1523.0}, {-19.65, 1566.0}, {-19.15, 1592.0}, {-18.65, 1927.0}, {-18.15, 3089.0}, {-17.65, 6068.0}, {-17.15, 14239.0}, {-16.65, 34124.0}, {-16.15, 64097.0}, {-15.65, 110352.0}, {-15.15, 164742.0}, {-14.65, 209499.0}, {-14.15, 267274.0}, {-13.65, 283290.0}, {-13.15, 275363.0}, {-12.65, 258014.0}, {-12.15, 225000.0}, {-11.65, 200000.0}, {-11.15, 190000.0}, {-10.65, 185000.0}, {-10.15, 180000.0}, { -9.65, 179000.0}, { -9.15, 178000.0}, { -8.65, 177000.0}, { -8.15, 176000.0}, { -7.65, 175000.0}, { -7.15, 174000.0}, { -6.65, 173000.0}, { -6.15, 172000.0}, { -5.65, 171000.0}, { -5.15, 170000.0} }; /** Poor data: long tails. */ protected static final double[][] DATASET3 = new double[][] { {-90.15, 1513.0}, {-80.15, 1514.0}, {-70.15, 1513.0}, {-60.15, 1514.0}, {-50.15, 1513.0}, {-40.15, 1514.0}, {-30.15, 1513.0}, {-20.15, 1523.0}, {-19.65, 1566.0}, {-19.15, 1592.0}, {-18.65, 1927.0}, {-18.15, 3089.0}, {-17.65, 6068.0}, {-17.15, 14239.0}, {-16.65, 34124.0}, {-16.15, 64097.0}, {-15.65, 110352.0}, {-15.15, 164742.0}, {-14.65, 209499.0}, {-14.15, 267274.0}, {-13.65, 283290.0}, {-13.15, 275363.0}, {-12.65, 258014.0}, {-12.15, 214073.0}, {-11.65, 182244.0}, {-11.15, 136419.0}, {-10.65, 97823.0}, {-10.15, 58930.0}, { -9.65, 35404.0}, { -9.15, 16120.0}, { -8.65, 9823.0}, { -8.15, 5064.0}, { -7.65, 2575.0}, { -7.15, 1642.0}, { -6.65, 1101.0}, { -6.15, 812.0}, { -5.65, 690.0}, { -5.15, 565.0}, { 5.15, 564.0}, { 15.15, 565.0}, { 25.15, 564.0}, { 35.15, 565.0}, { 45.15, 564.0}, { 55.15, 565.0}, { 65.15, 564.0}, { 75.15, 565.0} }; /** Poor data: right of peak is missing. */ protected static final double[][] DATASET4 = new double[][] { {-20.15, 1523.0}, {-19.65, 1566.0}, {-19.15, 1592.0}, {-18.65, 1927.0}, {-18.15, 3089.0}, {-17.65, 6068.0}, {-17.15, 14239.0}, {-16.65, 34124.0}, {-16.15, 64097.0}, {-15.65, 110352.0}, {-15.15, 164742.0}, {-14.65, 209499.0}, {-14.15, 267274.0}, {-13.65, 283290.0} }; /** Good data, but few points. */ protected static final double[][] DATASET5 = new double[][] { {4.0254623, 531026.0}, {4.03128248, 984167.0}, {4.03839603, 1887233.0}, {4.04421621, 2687152.0}, {4.05132976, 3461228.0}, {4.05326982, 3580526.0}, {4.05779662, 3439750.0}, {4.0636168, 2877648.0}, {4.06943698, 2175960.0}, {4.07525716, 1447024.0}, {4.08237071, 717104.0}, {4.08366408, 620014.0} }; /** * Basic. */ @Test public void testFit01() { GaussianFitter fitter = new GaussianFitter(new LevenbergMarquardtOptimizer()); addDatasetToGaussianFitter(DATASET1, fitter); double[] parameters = fitter.fit(); Assert.assertEquals(3496978.1837704973, parameters[0], 1e-4); Assert.assertEquals(4.054933085999146, parameters[1], 1e-4); Assert.assertEquals(0.015039355620304326, parameters[2], 1e-4); } /** * Zero points is not enough observed points. */ @Test(expected=MathIllegalArgumentException.class) public void testFit02() { GaussianFitter fitter = new GaussianFitter(new LevenbergMarquardtOptimizer()); fitter.fit(); } /** * Two points is not enough observed points. */ @Test(expected=MathIllegalArgumentException.class) public void testFit03() { GaussianFitter fitter = new GaussianFitter(new LevenbergMarquardtOptimizer()); addDatasetToGaussianFitter(new double[][] { {4.0254623, 531026.0}, {4.02804905, 664002.0}}, fitter); fitter.fit(); } /** * Poor data: right of peak not symmetric with left of peak. */ @Test public void testFit04() { GaussianFitter fitter = new GaussianFitter(new LevenbergMarquardtOptimizer()); addDatasetToGaussianFitter(DATASET2, fitter); double[] parameters = fitter.fit(); Assert.assertEquals(233003.2967252038, parameters[0], 1e-4); Assert.assertEquals(-10.654887521095983, parameters[1], 1e-4); Assert.assertEquals(4.335937353196641, parameters[2], 1e-4); } /** * Poor data: long tails. */ @Test public void testFit05() { GaussianFitter fitter = new GaussianFitter(new LevenbergMarquardtOptimizer()); addDatasetToGaussianFitter(DATASET3, fitter); double[] parameters = fitter.fit(); Assert.assertEquals(283863.81929180305, parameters[0], 1e-4); Assert.assertEquals(-13.29641995105174, parameters[1], 1e-4); Assert.assertEquals(1.7297330293549908, parameters[2], 1e-4); } /** * Poor data: right of peak is missing. */ @Test public void testFit06() { GaussianFitter fitter = new GaussianFitter(new LevenbergMarquardtOptimizer()); addDatasetToGaussianFitter(DATASET4, fitter); double[] parameters = fitter.fit(); Assert.assertEquals(285250.66754309234, parameters[0], 1e-4); Assert.assertEquals(-13.528375695228455, parameters[1], 1e-4); Assert.assertEquals(1.5204344894331614, parameters[2], 1e-4); } /** * Basic with smaller dataset. */ @Test public void testFit07() { GaussianFitter fitter = new GaussianFitter(new LevenbergMarquardtOptimizer()); addDatasetToGaussianFitter(DATASET5, fitter); double[] parameters = fitter.fit(); Assert.assertEquals(3514384.729342235, parameters[0], 1e-4); Assert.assertEquals(4.054970307455625, parameters[1], 1e-4); Assert.assertEquals(0.015029412832160017, parameters[2], 1e-4); } @Test public void testMath519() { // The optimizer will try negative sigma values but "GaussianFitter" // will catch the raised exceptions and return NaN values instead. final double[] data = { 1.1143831578403364E-29, 4.95281403484594E-28, 1.1171347211930288E-26, 1.7044813962636277E-25, 1.9784716574832164E-24, 1.8630236407866774E-23, 1.4820532905097742E-22, 1.0241963854632831E-21, 6.275077366673128E-21, 3.461808994532493E-20, 1.7407124684715706E-19, 8.056687953553974E-19, 3.460193945992071E-18, 1.3883326374011525E-17, 5.233894983671116E-17, 1.8630791465263745E-16, 6.288759227922111E-16, 2.0204433920597856E-15, 6.198768938576155E-15, 1.821419346860626E-14, 5.139176445538471E-14, 1.3956427429045787E-13, 3.655705706448139E-13, 9.253753324779779E-13, 2.267636001476696E-12, 5.3880460095836855E-12, 1.2431632654852931E-11 }; GaussianFitter fitter = new GaussianFitter(new LevenbergMarquardtOptimizer()); for (int i = 0; i < data.length; i++) { fitter.addObservedPoint(i, data[i]); } final double[] p = fitter.fit(); Assert.assertEquals(53.1572792, p[1], 1e-7); Assert.assertEquals(5.75214622, p[2], 1e-8); } @Test public void testMath798() { final GaussianFitter fitter = new GaussianFitter(new LevenbergMarquardtOptimizer()); // When the data points are not commented out below, the fit stalls. // This is expected however, since the whole dataset hardly looks like // a Gaussian. // When commented out, the fit proceeds fine. fitter.addObservedPoint(0.23, 395.0); //fitter.addObservedPoint(0.68, 0.0); fitter.addObservedPoint(1.14, 376.0); //fitter.addObservedPoint(1.59, 0.0); fitter.addObservedPoint(2.05, 163.0); //fitter.addObservedPoint(2.50, 0.0); fitter.addObservedPoint(2.95, 49.0); //fitter.addObservedPoint(3.41, 0.0); fitter.addObservedPoint(3.86, 16.0); //fitter.addObservedPoint(4.32, 0.0); fitter.addObservedPoint(4.77, 1.0); final double[] p = fitter.fit(); // Values are copied from a previous run of this test. Assert.assertEquals(420.8397296167364, p[0], 1e-12); Assert.assertEquals(0.603770729862231, p[1], 1e-15); Assert.assertEquals(1.0786447936766612, p[2], 1e-14); } /** * Adds the specified points to specified GaussianFitter * instance. * * @param points data points where first dimension is a point index and * second dimension is an array of length two representing the point * with the first value corresponding to X and the second value * corresponding to Y * @param fitter fitter to which the points in points should be * added as observed points */ protected static void addDatasetToGaussianFitter(double[][] points, GaussianFitter fitter) { for (int i = 0; i < points.length; i++) { fitter.addObservedPoint(points[i][0], points[i][1]); } } } ././@LongLink100644 0 0 152 12126630646 10257 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/optimization/fitting/HarmonicFitterTest.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/optimization/fitting/HarmonicFitterTest100644 1750 1750 16515 12126627670 32503 0ustarlucluc 0 0 // Licensed to the Apache Software Foundation (ASF) under one // or more contributor license agreements. See the NOTICE file // distributed with this work for additional information // regarding copyright ownership. The ASF licenses this file // to you 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. package org.apache.commons.math3.optimization.fitting; import java.util.Random; import org.apache.commons.math3.analysis.function.HarmonicOscillator; import org.apache.commons.math3.optimization.general.LevenbergMarquardtOptimizer; import org.apache.commons.math3.exception.NumberIsTooSmallException; import org.apache.commons.math3.exception.MathIllegalStateException; import org.apache.commons.math3.util.FastMath; import org.apache.commons.math3.util.MathUtils; import org.junit.Test; import org.junit.Assert; public class HarmonicFitterTest { @Test(expected=NumberIsTooSmallException.class) public void testPreconditions1() { HarmonicFitter fitter = new HarmonicFitter(new LevenbergMarquardtOptimizer()); fitter.fit(); } // This test fails (throwing "ConvergenceException" instead). // @Test(expected=ZeroException.class) // public void testPreconditions2() { // HarmonicFitter fitter = // new HarmonicFitter(new LevenbergMarquardtOptimizer()); // final double x = 1.2; // fitter.addObservedPoint(1, x, 1); // fitter.addObservedPoint(1, x, -1); // fitter.addObservedPoint(1, x, 0.5); // fitter.addObservedPoint(1, x, 0); // final double[] fitted = fitter.fit(); // } @Test public void testNoError() { final double a = 0.2; final double w = 3.4; final double p = 4.1; HarmonicOscillator f = new HarmonicOscillator(a, w, p); HarmonicFitter fitter = new HarmonicFitter(new LevenbergMarquardtOptimizer()); for (double x = 0.0; x < 1.3; x += 0.01) { fitter.addObservedPoint(1, x, f.value(x)); } final double[] fitted = fitter.fit(); Assert.assertEquals(a, fitted[0], 1.0e-13); Assert.assertEquals(w, fitted[1], 1.0e-13); Assert.assertEquals(p, MathUtils.normalizeAngle(fitted[2], p), 1e-13); HarmonicOscillator ff = new HarmonicOscillator(fitted[0], fitted[1], fitted[2]); for (double x = -1.0; x < 1.0; x += 0.01) { Assert.assertTrue(FastMath.abs(f.value(x) - ff.value(x)) < 1e-13); } } @Test public void test1PercentError() { Random randomizer = new Random(64925784252l); final double a = 0.2; final double w = 3.4; final double p = 4.1; HarmonicOscillator f = new HarmonicOscillator(a, w, p); HarmonicFitter fitter = new HarmonicFitter(new LevenbergMarquardtOptimizer()); for (double x = 0.0; x < 10.0; x += 0.1) { fitter.addObservedPoint(1, x, f.value(x) + 0.01 * randomizer.nextGaussian()); } final double[] fitted = fitter.fit(); Assert.assertEquals(a, fitted[0], 7.6e-4); Assert.assertEquals(w, fitted[1], 2.7e-3); Assert.assertEquals(p, MathUtils.normalizeAngle(fitted[2], p), 1.3e-2); } @Test public void testTinyVariationsData() { Random randomizer = new Random(64925784252l); HarmonicFitter fitter = new HarmonicFitter(new LevenbergMarquardtOptimizer()); for (double x = 0.0; x < 10.0; x += 0.1) { fitter.addObservedPoint(1, x, 1e-7 * randomizer.nextGaussian()); } fitter.fit(); // This test serves to cover the part of the code of "guessAOmega" // when the algorithm using integrals fails. } @Test public void testInitialGuess() { Random randomizer = new Random(45314242l); final double a = 0.2; final double w = 3.4; final double p = 4.1; HarmonicOscillator f = new HarmonicOscillator(a, w, p); HarmonicFitter fitter = new HarmonicFitter(new LevenbergMarquardtOptimizer()); for (double x = 0.0; x < 10.0; x += 0.1) { fitter.addObservedPoint(1, x, f.value(x) + 0.01 * randomizer.nextGaussian()); } final double[] fitted = fitter.fit(new double[] { 0.15, 3.6, 4.5 }); Assert.assertEquals(a, fitted[0], 1.2e-3); Assert.assertEquals(w, fitted[1], 3.3e-3); Assert.assertEquals(p, MathUtils.normalizeAngle(fitted[2], p), 1.7e-2); } @Test public void testUnsorted() { Random randomizer = new Random(64925784252l); final double a = 0.2; final double w = 3.4; final double p = 4.1; HarmonicOscillator f = new HarmonicOscillator(a, w, p); HarmonicFitter fitter = new HarmonicFitter(new LevenbergMarquardtOptimizer()); // build a regularly spaced array of measurements int size = 100; double[] xTab = new double[size]; double[] yTab = new double[size]; for (int i = 0; i < size; ++i) { xTab[i] = 0.1 * i; yTab[i] = f.value(xTab[i]) + 0.01 * randomizer.nextGaussian(); } // shake it for (int i = 0; i < size; ++i) { int i1 = randomizer.nextInt(size); int i2 = randomizer.nextInt(size); double xTmp = xTab[i1]; double yTmp = yTab[i1]; xTab[i1] = xTab[i2]; yTab[i1] = yTab[i2]; xTab[i2] = xTmp; yTab[i2] = yTmp; } // pass it to the fitter for (int i = 0; i < size; ++i) { fitter.addObservedPoint(1, xTab[i], yTab[i]); } final double[] fitted = fitter.fit(); Assert.assertEquals(a, fitted[0], 7.6e-4); Assert.assertEquals(w, fitted[1], 3.5e-3); Assert.assertEquals(p, MathUtils.normalizeAngle(fitted[2], p), 1.5e-2); } @Test(expected=MathIllegalStateException.class) public void testMath844() { final double[] y = { 0, 1, 2, 3, 2, 1, 0, -1, -2, -3, -2, -1, 0, 1, 2, 3, 2, 1, 0, -1, -2, -3, -2, -1, 0, 1, 2, 3, 2, 1, 0 }; final int len = y.length; final WeightedObservedPoint[] points = new WeightedObservedPoint[len]; for (int i = 0; i < len; i++) { points[i] = new WeightedObservedPoint(1, i, y[i]); } // The guesser fails because the function is far from an harmonic // function: It is a triangular periodic function with amplitude 3 // and period 12, and all sample points are taken at integer abscissae // so function values all belong to the integer subset {-3, -2, -1, 0, // 1, 2, 3}. final HarmonicFitter.ParameterGuesser guesser = new HarmonicFitter.ParameterGuesser(points); } } ././@LongLink100644 0 0 154 12126630646 10261 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/optimization/fitting/PolynomialFitterTest.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/optimization/fitting/PolynomialFitterTe100644 1750 1750 27744 12126627670 32525 0ustarlucluc 0 0 // Licensed to the Apache Software Foundation (ASF) under one // or more contributor license agreements. See the NOTICE file // distributed with this work for additional information // regarding copyright ownership. The ASF licenses this file // to you 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. package org.apache.commons.math3.optimization.fitting; import java.util.Random; import org.apache.commons.math3.analysis.polynomials.PolynomialFunction; import org.apache.commons.math3.analysis.polynomials.PolynomialFunction.Parametric; import org.apache.commons.math3.exception.ConvergenceException; import org.apache.commons.math3.exception.TooManyEvaluationsException; import org.apache.commons.math3.optimization.DifferentiableMultivariateVectorOptimizer; import org.apache.commons.math3.optimization.general.GaussNewtonOptimizer; import org.apache.commons.math3.optimization.general.LevenbergMarquardtOptimizer; import org.apache.commons.math3.optimization.SimpleVectorValueChecker; import org.apache.commons.math3.util.FastMath; import org.apache.commons.math3.distribution.RealDistribution; import org.apache.commons.math3.distribution.UniformRealDistribution; import org.apache.commons.math3.TestUtils; import org.junit.Test; import org.junit.Assert; /** * Test for class {@link CurveFitter} where the function to fit is a * polynomial. */ public class PolynomialFitterTest { @Test public void testFit() { final RealDistribution rng = new UniformRealDistribution(-100, 100); rng.reseedRandomGenerator(64925784252L); final LevenbergMarquardtOptimizer optim = new LevenbergMarquardtOptimizer(); final PolynomialFitter fitter = new PolynomialFitter(optim); final double[] coeff = { 12.9, -3.4, 2.1 }; // 12.9 - 3.4 x + 2.1 x^2 final PolynomialFunction f = new PolynomialFunction(coeff); // Collect data from a known polynomial. for (int i = 0; i < 100; i++) { final double x = rng.sample(); fitter.addObservedPoint(x, f.value(x)); } // Start fit from initial guesses that are far from the optimal values. final double[] best = fitter.fit(new double[] { -1e-20, 3e15, -5e25 }); TestUtils.assertEquals("best != coeff", coeff, best, 1e-12); } @Test public void testNoError() { Random randomizer = new Random(64925784252l); for (int degree = 1; degree < 10; ++degree) { PolynomialFunction p = buildRandomPolynomial(degree, randomizer); PolynomialFitter fitter = new PolynomialFitter(new LevenbergMarquardtOptimizer()); for (int i = 0; i <= degree; ++i) { fitter.addObservedPoint(1.0, i, p.value(i)); } final double[] init = new double[degree + 1]; PolynomialFunction fitted = new PolynomialFunction(fitter.fit(init)); for (double x = -1.0; x < 1.0; x += 0.01) { double error = FastMath.abs(p.value(x) - fitted.value(x)) / (1.0 + FastMath.abs(p.value(x))); Assert.assertEquals(0.0, error, 1.0e-6); } } } @Test public void testSmallError() { Random randomizer = new Random(53882150042l); double maxError = 0; for (int degree = 0; degree < 10; ++degree) { PolynomialFunction p = buildRandomPolynomial(degree, randomizer); PolynomialFitter fitter = new PolynomialFitter(new LevenbergMarquardtOptimizer()); for (double x = -1.0; x < 1.0; x += 0.01) { fitter.addObservedPoint(1.0, x, p.value(x) + 0.1 * randomizer.nextGaussian()); } final double[] init = new double[degree + 1]; PolynomialFunction fitted = new PolynomialFunction(fitter.fit(init)); for (double x = -1.0; x < 1.0; x += 0.01) { double error = FastMath.abs(p.value(x) - fitted.value(x)) / (1.0 + FastMath.abs(p.value(x))); maxError = FastMath.max(maxError, error); Assert.assertTrue(FastMath.abs(error) < 0.1); } } Assert.assertTrue(maxError > 0.01); } @Test public void testMath798() { final double tol = 1e-14; final SimpleVectorValueChecker checker = new SimpleVectorValueChecker(tol, tol); final double[] init = new double[] { 0, 0 }; final int maxEval = 3; final double[] lm = doMath798(new LevenbergMarquardtOptimizer(checker), maxEval, init); final double[] gn = doMath798(new GaussNewtonOptimizer(checker), maxEval, init); for (int i = 0; i <= 1; i++) { Assert.assertEquals(lm[i], gn[i], tol); } } /** * This test shows that the user can set the maximum number of iterations * to avoid running for too long. * But in the test case, the real problem is that the tolerance is way too * stringent. */ @Test(expected=TooManyEvaluationsException.class) public void testMath798WithToleranceTooLow() { final double tol = 1e-100; final SimpleVectorValueChecker checker = new SimpleVectorValueChecker(tol, tol); final double[] init = new double[] { 0, 0 }; final int maxEval = 10000; // Trying hard to fit. final double[] gn = doMath798(new GaussNewtonOptimizer(checker), maxEval, init); } /** * This test shows that the user can set the maximum number of iterations * to avoid running for too long. * Even if the real problem is that the tolerance is way too stringent, it * is possible to get the best solution so far, i.e. a checker will return * the point when the maximum iteration count has been reached. */ @Test public void testMath798WithToleranceTooLowButNoException() { final double tol = 1e-100; final double[] init = new double[] { 0, 0 }; final int maxEval = 10000; // Trying hard to fit. final SimpleVectorValueChecker checker = new SimpleVectorValueChecker(tol, tol, maxEval); final double[] lm = doMath798(new LevenbergMarquardtOptimizer(checker), maxEval, init); final double[] gn = doMath798(new GaussNewtonOptimizer(checker), maxEval, init); for (int i = 0; i <= 1; i++) { Assert.assertEquals(lm[i], gn[i], 1e-15); } } /** * @param optimizer Optimizer. * @param maxEval Maximum number of function evaluations. * @param init First guess. * @return the solution found by the given optimizer. */ private double[] doMath798(DifferentiableMultivariateVectorOptimizer optimizer, int maxEval, double[] init) { final CurveFitter fitter = new CurveFitter(optimizer); fitter.addObservedPoint(-0.2, -7.12442E-13); fitter.addObservedPoint(-0.199, -4.33397E-13); fitter.addObservedPoint(-0.198, -2.823E-13); fitter.addObservedPoint(-0.197, -1.40405E-13); fitter.addObservedPoint(-0.196, -7.80821E-15); fitter.addObservedPoint(-0.195, 6.20484E-14); fitter.addObservedPoint(-0.194, 7.24673E-14); fitter.addObservedPoint(-0.193, 1.47152E-13); fitter.addObservedPoint(-0.192, 1.9629E-13); fitter.addObservedPoint(-0.191, 2.12038E-13); fitter.addObservedPoint(-0.19, 2.46906E-13); fitter.addObservedPoint(-0.189, 2.77495E-13); fitter.addObservedPoint(-0.188, 2.51281E-13); fitter.addObservedPoint(-0.187, 2.64001E-13); fitter.addObservedPoint(-0.186, 2.8882E-13); fitter.addObservedPoint(-0.185, 3.13604E-13); fitter.addObservedPoint(-0.184, 3.14248E-13); fitter.addObservedPoint(-0.183, 3.1172E-13); fitter.addObservedPoint(-0.182, 3.12912E-13); fitter.addObservedPoint(-0.181, 3.06761E-13); fitter.addObservedPoint(-0.18, 2.8559E-13); fitter.addObservedPoint(-0.179, 2.86806E-13); fitter.addObservedPoint(-0.178, 2.985E-13); fitter.addObservedPoint(-0.177, 2.67148E-13); fitter.addObservedPoint(-0.176, 2.94173E-13); fitter.addObservedPoint(-0.175, 3.27528E-13); fitter.addObservedPoint(-0.174, 3.33858E-13); fitter.addObservedPoint(-0.173, 2.97511E-13); fitter.addObservedPoint(-0.172, 2.8615E-13); fitter.addObservedPoint(-0.171, 2.84624E-13); final double[] coeff = fitter.fit(maxEval, new PolynomialFunction.Parametric(), init); return coeff; } @Test public void testRedundantSolvable() { // Levenberg-Marquardt should handle redundant information gracefully checkUnsolvableProblem(new LevenbergMarquardtOptimizer(), true); } @Test public void testRedundantUnsolvable() { // Gauss-Newton should not be able to solve redundant information checkUnsolvableProblem(new GaussNewtonOptimizer(true, new SimpleVectorValueChecker(1e-15, 1e-15)), false); } @Test public void testLargeSample() { Random randomizer = new Random(0x5551480dca5b369bl); double maxError = 0; for (int degree = 0; degree < 10; ++degree) { PolynomialFunction p = buildRandomPolynomial(degree, randomizer); PolynomialFitter fitter = new PolynomialFitter(new LevenbergMarquardtOptimizer()); for (int i = 0; i < 40000; ++i) { double x = -1.0 + i / 20000.0; fitter.addObservedPoint(1.0, x, p.value(x) + 0.1 * randomizer.nextGaussian()); } final double[] init = new double[degree + 1]; PolynomialFunction fitted = new PolynomialFunction(fitter.fit(init)); for (double x = -1.0; x < 1.0; x += 0.01) { double error = FastMath.abs(p.value(x) - fitted.value(x)) / (1.0 + FastMath.abs(p.value(x))); maxError = FastMath.max(maxError, error); Assert.assertTrue(FastMath.abs(error) < 0.01); } } Assert.assertTrue(maxError > 0.001); } private void checkUnsolvableProblem(DifferentiableMultivariateVectorOptimizer optimizer, boolean solvable) { Random randomizer = new Random(1248788532l); for (int degree = 0; degree < 10; ++degree) { PolynomialFunction p = buildRandomPolynomial(degree, randomizer); PolynomialFitter fitter = new PolynomialFitter(optimizer); // reusing the same point over and over again does not bring // information, the problem cannot be solved in this case for // degrees greater than 1 (but one point is sufficient for // degree 0) for (double x = -1.0; x < 1.0; x += 0.01) { fitter.addObservedPoint(1.0, 0.0, p.value(0.0)); } try { final double[] init = new double[degree + 1]; fitter.fit(init); Assert.assertTrue(solvable || (degree == 0)); } catch(ConvergenceException e) { Assert.assertTrue((! solvable) && (degree > 0)); } } } private PolynomialFunction buildRandomPolynomial(int degree, Random randomizer) { final double[] coefficients = new double[degree + 1]; for (int i = 0; i <= degree; ++i) { coefficients[i] = randomizer.nextGaussian(); } return new PolynomialFunction(coefficients); } } ././@LongLink100644 0 0 207 12126630646 10260 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/optimization/MultivariateDifferentiableVectorMultiStartOptimizerTest.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/optimization/MultivariateDifferentiable100644 1750 1750 26476 12126627671 32563 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.optimization; import org.apache.commons.math3.analysis.differentiation.DerivativeStructure; import org.apache.commons.math3.analysis.differentiation.MultivariateDifferentiableVectorFunction; import org.apache.commons.math3.exception.MathIllegalStateException; import org.apache.commons.math3.linear.BlockRealMatrix; import org.apache.commons.math3.linear.RealMatrix; import org.apache.commons.math3.optimization.general.GaussNewtonOptimizer; import org.apache.commons.math3.random.GaussianRandomGenerator; import org.apache.commons.math3.random.JDKRandomGenerator; import org.apache.commons.math3.random.RandomVectorGenerator; import org.apache.commons.math3.random.UncorrelatedRandomVectorGenerator; import org.junit.Assert; import org.junit.Test; /** *

Some of the unit tests are re-implementations of the MINPACK file17 and file22 test files. * The redistribution policy for MINPACK is available here, for * convenience, it is reproduced below.

* * * *
* Minpack Copyright Notice (1999) University of Chicago. * All rights reserved *
* Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: *
    *
  1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer.
  2. *
  3. Redistributions in binary form must reproduce the above * copyright notice, this list of conditions and the following * disclaimer in the documentation and/or other materials provided * with the distribution.
  4. *
  5. The end-user documentation included with the redistribution, if any, * must include the following acknowledgment: * This product includes software developed by the University of * Chicago, as Operator of Argonne National Laboratory. * Alternately, this acknowledgment may appear in the software itself, * if and wherever such third-party acknowledgments normally appear.
  6. *
  7. WARRANTY DISCLAIMER. THE SOFTWARE IS SUPPLIED "AS IS" * WITHOUT WARRANTY OF ANY KIND. THE COPYRIGHT HOLDER, THE * UNITED STATES, THE UNITED STATES DEPARTMENT OF ENERGY, AND * THEIR EMPLOYEES: (1) DISCLAIM ANY WARRANTIES, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO ANY IMPLIED WARRANTIES * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE * OR NON-INFRINGEMENT, (2) DO NOT ASSUME ANY LEGAL LIABILITY * OR RESPONSIBILITY FOR THE ACCURACY, COMPLETENESS, OR * USEFULNESS OF THE SOFTWARE, (3) DO NOT REPRESENT THAT USE OF * THE SOFTWARE WOULD NOT INFRINGE PRIVATELY OWNED RIGHTS, (4) * DO NOT WARRANT THAT THE SOFTWARE WILL FUNCTION * UNINTERRUPTED, THAT IT IS ERROR-FREE OR THAT ANY ERRORS WILL * BE CORRECTED.
  8. *
  9. LIMITATION OF LIABILITY. IN NO EVENT WILL THE COPYRIGHT * HOLDER, THE UNITED STATES, THE UNITED STATES DEPARTMENT OF * ENERGY, OR THEIR EMPLOYEES: BE LIABLE FOR ANY INDIRECT, * INCIDENTAL, CONSEQUENTIAL, SPECIAL OR PUNITIVE DAMAGES OF * ANY KIND OR NATURE, INCLUDING BUT NOT LIMITED TO LOSS OF * PROFITS OR LOSS OF DATA, FOR ANY REASON WHATSOEVER, WHETHER * SUCH LIABILITY IS ASSERTED ON THE BASIS OF CONTRACT, TORT * (INCLUDING NEGLIGENCE OR STRICT LIABILITY), OR OTHERWISE, * EVEN IF ANY OF SAID PARTIES HAS BEEN WARNED OF THE * POSSIBILITY OF SUCH LOSS OR DAMAGES.
  10. *
    * @author Argonne National Laboratory. MINPACK project. March 1980 (original fortran minpack tests) * @author Burton S. Garbow (original fortran minpack tests) * @author Kenneth E. Hillstrom (original fortran minpack tests) * @author Jorge J. More (original fortran minpack tests) * @author Luc Maisonobe (non-minpack tests and minpack tests Java translation) */ public class MultivariateDifferentiableVectorMultiStartOptimizerTest { @Test public void testTrivial() { LinearProblem problem = new LinearProblem(new double[][] { { 2 } }, new double[] { 3 }); // TODO: the wrapper around GaussNewtonOptimizer is a temporary hack for // version 3.1 of the library. It should be removed when GaussNewtonOptimizer // will officialy be declared as implementing MultivariateDifferentiableVectorOptimizer MultivariateDifferentiableVectorOptimizer underlyingOptimizer = new MultivariateDifferentiableVectorOptimizer() { private GaussNewtonOptimizer gn = new GaussNewtonOptimizer(true, new SimpleVectorValueChecker(1.0e-6, 1.0e-6)); public PointVectorValuePair optimize(int maxEval, MultivariateDifferentiableVectorFunction f, double[] target, double[] weight, double[] startPoint) { return gn.optimize(maxEval, f, target, weight, startPoint); } public int getMaxEvaluations() { return gn.getMaxEvaluations(); } public int getEvaluations() { return gn.getEvaluations(); } public ConvergenceChecker getConvergenceChecker() { return gn.getConvergenceChecker(); } }; JDKRandomGenerator g = new JDKRandomGenerator(); g.setSeed(16069223052l); RandomVectorGenerator generator = new UncorrelatedRandomVectorGenerator(1, new GaussianRandomGenerator(g)); MultivariateDifferentiableVectorMultiStartOptimizer optimizer = new MultivariateDifferentiableVectorMultiStartOptimizer(underlyingOptimizer, 10, generator); // no optima before first optimization attempt try { optimizer.getOptima(); Assert.fail("an exception should have been thrown"); } catch (MathIllegalStateException ise) { // expected } PointVectorValuePair optimum = optimizer.optimize(100, problem, problem.target, new double[] { 1 }, new double[] { 0 }); Assert.assertEquals(1.5, optimum.getPoint()[0], 1.0e-10); Assert.assertEquals(3.0, optimum.getValue()[0], 1.0e-10); PointVectorValuePair[] optima = optimizer.getOptima(); Assert.assertEquals(10, optima.length); for (int i = 0; i < optima.length; ++i) { Assert.assertEquals(1.5, optima[i].getPoint()[0], 1.0e-10); Assert.assertEquals(3.0, optima[i].getValue()[0], 1.0e-10); } Assert.assertTrue(optimizer.getEvaluations() > 20); Assert.assertTrue(optimizer.getEvaluations() < 50); Assert.assertEquals(100, optimizer.getMaxEvaluations()); } @Test(expected=TestException.class) public void testNoOptimum() { // TODO: the wrapper around GaussNewtonOptimizer is a temporary hack for // version 3.1 of the library. It should be removed when GaussNewtonOptimizer // will officialy be declared as implementing MultivariateDifferentiableVectorOptimizer MultivariateDifferentiableVectorOptimizer underlyingOptimizer = new MultivariateDifferentiableVectorOptimizer() { private GaussNewtonOptimizer gn = new GaussNewtonOptimizer(true, new SimpleVectorValueChecker(1.0e-6, 1.0e-6)); public PointVectorValuePair optimize(int maxEval, MultivariateDifferentiableVectorFunction f, double[] target, double[] weight, double[] startPoint) { return gn.optimize(maxEval, f, target, weight, startPoint); } public int getMaxEvaluations() { return gn.getMaxEvaluations(); } public int getEvaluations() { return gn.getEvaluations(); } public ConvergenceChecker getConvergenceChecker() { return gn.getConvergenceChecker(); } }; JDKRandomGenerator g = new JDKRandomGenerator(); g.setSeed(12373523445l); RandomVectorGenerator generator = new UncorrelatedRandomVectorGenerator(1, new GaussianRandomGenerator(g)); MultivariateDifferentiableVectorMultiStartOptimizer optimizer = new MultivariateDifferentiableVectorMultiStartOptimizer(underlyingOptimizer, 10, generator); optimizer.optimize(100, new MultivariateDifferentiableVectorFunction() { public double[] value(double[] point) { throw new TestException(); } public DerivativeStructure[] value(DerivativeStructure[] point) { return point; } }, new double[] { 2 }, new double[] { 1 }, new double[] { 0 }); } private static class TestException extends RuntimeException { private static final long serialVersionUID = -7809988995389067683L; } private static class LinearProblem implements MultivariateDifferentiableVectorFunction { final RealMatrix factors; final double[] target; public LinearProblem(double[][] factors, double[] target) { this.factors = new BlockRealMatrix(factors); this.target = target; } public double[] value(double[] variables) { return factors.operate(variables); } public DerivativeStructure[] value(DerivativeStructure[] variables) { DerivativeStructure[] y = new DerivativeStructure[factors.getRowDimension()]; for (int i = 0; i < y.length; ++i) { y[i] = variables[0].getField().getZero(); for (int j = 0; j < factors.getColumnDimension(); ++j) { y[i] = y[i].add(variables[j].multiply(factors.getEntry(i, j))); } } return y; } } } ././@LongLink100644 0 0 154 12126630646 10261 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/optimization/SimpleVectorValueCheckerTest.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/optimization/SimpleVectorValueCheckerTe100644 1750 1750 4353 12126627671 32426 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.optimization; import org.apache.commons.math3.exception.NotStrictlyPositiveException; import org.junit.Test; import org.junit.Assert; public class SimpleVectorValueCheckerTest { @Test(expected=NotStrictlyPositiveException.class) public void testIterationCheckPrecondition() { new SimpleVectorValueChecker(1e-1, 1e-2, 0); } @Test public void testIterationCheck() { final int max = 10; final SimpleVectorValueChecker checker = new SimpleVectorValueChecker(1e-1, 1e-2, max); Assert.assertTrue(checker.converged(max, null, null)); Assert.assertTrue(checker.converged(max + 1, null, null)); } @Test public void testIterationCheckDisabled() { final SimpleVectorValueChecker checker = new SimpleVectorValueChecker(1e-8, 1e-8); final PointVectorValuePair a = new PointVectorValuePair(new double[] { 1d }, new double[] { 1d }); final PointVectorValuePair b = new PointVectorValuePair(new double[] { 10d }, new double[] { 10d }); Assert.assertFalse(checker.converged(-1, a, b)); Assert.assertFalse(checker.converged(0, a, b)); Assert.assertFalse(checker.converged(1000000, a, b)); Assert.assertTrue(checker.converged(-1, a, a)); Assert.assertTrue(checker.converged(-1, b, b)); } } ././@LongLink100644 0 0 163 12126630646 10261 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/optimization/MultivariateMultiStartOptimizerTest.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/optimization/MultivariateMultiStartOpti100644 1750 1750 5651 12126627671 32574 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.optimization; import org.apache.commons.math3.analysis.MultivariateFunction; import org.apache.commons.math3.optimization.direct.NelderMeadSimplex; import org.apache.commons.math3.optimization.direct.SimplexOptimizer; import org.apache.commons.math3.random.GaussianRandomGenerator; import org.apache.commons.math3.random.JDKRandomGenerator; import org.apache.commons.math3.random.RandomVectorGenerator; import org.apache.commons.math3.random.UncorrelatedRandomVectorGenerator; import org.junit.Assert; import org.junit.Test; public class MultivariateMultiStartOptimizerTest { @Test public void testRosenbrock() { Rosenbrock rosenbrock = new Rosenbrock(); SimplexOptimizer underlying = new SimplexOptimizer(new SimpleValueChecker(-1, 1.0e-3)); NelderMeadSimplex simplex = new NelderMeadSimplex(new double[][] { { -1.2, 1.0 }, { 0.9, 1.2 } , { 3.5, -2.3 } }); underlying.setSimplex(simplex); JDKRandomGenerator g = new JDKRandomGenerator(); g.setSeed(16069223052l); RandomVectorGenerator generator = new UncorrelatedRandomVectorGenerator(2, new GaussianRandomGenerator(g)); MultivariateMultiStartOptimizer optimizer = new MultivariateMultiStartOptimizer(underlying, 10, generator); PointValuePair optimum = optimizer.optimize(1100, rosenbrock, GoalType.MINIMIZE, new double[] { -1.2, 1.0 }); Assert.assertEquals(rosenbrock.getCount(), optimizer.getEvaluations()); Assert.assertTrue(optimizer.getEvaluations() > 900); Assert.assertTrue(optimizer.getEvaluations() < 1200); Assert.assertTrue(optimum.getValue() < 8.0e-4); } private static class Rosenbrock implements MultivariateFunction { private int count; public Rosenbrock() { count = 0; } public double value(double[] x) { ++count; double a = x[1] - x[0] * x[0]; double b = 1.0 - x[0]; return 100 * a * a + b * b; } public int getCount() { return count; } } } ././@LongLink100644 0 0 146 12126630646 10262 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/optimization/SimpleValueCheckerTest.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/optimization/SimpleValueCheckerTest.jav100644 1750 1750 4016 12126627671 32365 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.optimization; import org.apache.commons.math3.exception.NotStrictlyPositiveException; import org.junit.Test; import org.junit.Assert; public class SimpleValueCheckerTest { @Test(expected=NotStrictlyPositiveException.class) public void testIterationCheckPrecondition() { new SimpleValueChecker(1e-1, 1e-2, 0); } @Test public void testIterationCheck() { final int max = 10; final SimpleValueChecker checker = new SimpleValueChecker(1e-1, 1e-2, max); Assert.assertTrue(checker.converged(max, null, null)); Assert.assertTrue(checker.converged(max + 1, null, null)); } @Test public void testIterationCheckDisabled() { final SimpleValueChecker checker = new SimpleValueChecker(1e-8, 1e-8); final PointValuePair a = new PointValuePair(new double[] { 1d }, 1d); final PointValuePair b = new PointValuePair(new double[] { 10d }, 10d); Assert.assertFalse(checker.converged(-1, a, b)); Assert.assertFalse(checker.converged(0, a, b)); Assert.assertFalse(checker.converged(1000000, a, b)); Assert.assertTrue(checker.converged(-1, a, a)); Assert.assertTrue(checker.converged(-1, b, b)); } } ././@LongLink100644 0 0 146 12126630646 10262 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/optimization/SimplePointCheckerTest.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/optimization/SimplePointCheckerTest.jav100644 1750 1750 4166 12126627671 32410 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.optimization; import org.apache.commons.math3.exception.NotStrictlyPositiveException; import org.junit.Test; import org.junit.Assert; public class SimplePointCheckerTest { @Test(expected=NotStrictlyPositiveException.class) public void testIterationCheckPrecondition() { new SimplePointChecker(1e-1, 1e-2, 0); } @Test public void testIterationCheck() { final int max = 10; final SimplePointChecker checker = new SimplePointChecker(1e-1, 1e-2, max); Assert.assertTrue(checker.converged(max, null, null)); Assert.assertTrue(checker.converged(max + 1, null, null)); } @Test public void testIterationCheckDisabled() { final SimplePointChecker checker = new SimplePointChecker(1e-8, 1e-8); final PointValuePair a = new PointValuePair(new double[] { 1d }, 1d); final PointValuePair b = new PointValuePair(new double[] { 10d }, 10d); Assert.assertFalse(checker.converged(-1, a, b)); Assert.assertFalse(checker.converged(0, a, b)); Assert.assertFalse(checker.converged(1000000, a, b)); Assert.assertTrue(checker.converged(-1, a, a)); Assert.assertTrue(checker.converged(-1, b, b)); } } ././@LongLink100644 0 0 151 12126630646 10256 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/optimization/linear/SimplexTableauTest.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/optimization/linear/SimplexTableauTest.100644 1750 1750 12022 12126627670 32355 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.optimization.linear; import java.util.ArrayList; import java.util.Collection; import org.apache.commons.math3.TestUtils; import org.apache.commons.math3.optimization.GoalType; import org.junit.Assert; import org.junit.Test; public class SimplexTableauTest { @Test public void testInitialization() { LinearObjectiveFunction f = createFunction(); Collection constraints = createConstraints(); SimplexTableau tableau = new SimplexTableau(f, constraints, GoalType.MAXIMIZE, false, 1.0e-6); double[][] expectedInitialTableau = { {-1, 0, -1, -1, 2, 0, 0, 0, -4}, { 0, 1, -15, -10, 25, 0, 0, 0, 0}, { 0, 0, 1, 0, -1, 1, 0, 0, 2}, { 0, 0, 0, 1, -1, 0, 1, 0, 3}, { 0, 0, 1, 1, -2, 0, 0, 1, 4} }; assertMatrixEquals(expectedInitialTableau, tableau.getData()); } @Test public void testDropPhase1Objective() { LinearObjectiveFunction f = createFunction(); Collection constraints = createConstraints(); SimplexTableau tableau = new SimplexTableau(f, constraints, GoalType.MAXIMIZE, false, 1.0e-6); double[][] expectedTableau = { { 1, -15, -10, 0, 0, 0, 0}, { 0, 1, 0, 1, 0, 0, 2}, { 0, 0, 1, 0, 1, 0, 3}, { 0, 1, 1, 0, 0, 1, 4} }; tableau.dropPhase1Objective(); assertMatrixEquals(expectedTableau, tableau.getData()); } @Test public void testTableauWithNoArtificialVars() { LinearObjectiveFunction f = new LinearObjectiveFunction(new double[] {15, 10}, 0); Collection constraints = new ArrayList(); constraints.add(new LinearConstraint(new double[] {1, 0}, Relationship.LEQ, 2)); constraints.add(new LinearConstraint(new double[] {0, 1}, Relationship.LEQ, 3)); constraints.add(new LinearConstraint(new double[] {1, 1}, Relationship.LEQ, 4)); SimplexTableau tableau = new SimplexTableau(f, constraints, GoalType.MAXIMIZE, false, 1.0e-6); double[][] initialTableau = { {1, -15, -10, 25, 0, 0, 0, 0}, {0, 1, 0, -1, 1, 0, 0, 2}, {0, 0, 1, -1, 0, 1, 0, 3}, {0, 1, 1, -2, 0, 0, 1, 4} }; assertMatrixEquals(initialTableau, tableau.getData()); } @Test public void testSerial() { LinearObjectiveFunction f = createFunction(); Collection constraints = createConstraints(); SimplexTableau tableau = new SimplexTableau(f, constraints, GoalType.MAXIMIZE, false, 1.0e-6); Assert.assertEquals(tableau, TestUtils.serializeAndRecover(tableau)); } private LinearObjectiveFunction createFunction() { return new LinearObjectiveFunction(new double[] {15, 10}, 0); } private Collection createConstraints() { Collection constraints = new ArrayList(); constraints.add(new LinearConstraint(new double[] {1, 0}, Relationship.LEQ, 2)); constraints.add(new LinearConstraint(new double[] {0, 1}, Relationship.LEQ, 3)); constraints.add(new LinearConstraint(new double[] {1, 1}, Relationship.EQ, 4)); return constraints; } private void assertMatrixEquals(double[][] expected, double[][] result) { Assert.assertEquals("Wrong number of rows.", expected.length, result.length); for (int i = 0; i < expected.length; i++) { Assert.assertEquals("Wrong number of columns.", expected[i].length, result[i].length); for (int j = 0; j < expected[i].length; j++) { Assert.assertEquals("Wrong value at position [" + i + "," + j + "]", expected[i][j], result[i][j], 1.0e-15); } } } } ././@LongLink100644 0 0 150 12126630646 10255 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/optimization/linear/SimplexSolverTest.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/optimization/linear/SimplexSolverTest.j100644 1750 1750 117756 12126627670 32470 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.optimization.linear; import org.junit.Assert; import java.util.ArrayList; import java.util.Collection; import java.util.List; import org.apache.commons.math3.optimization.GoalType; import org.apache.commons.math3.optimization.PointValuePair; import org.apache.commons.math3.util.Precision; import org.junit.Test; public class SimplexSolverTest { @Test public void testMath828() { LinearObjectiveFunction f = new LinearObjectiveFunction( new double[] { 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0}, 0.0); ArrayList constraints = new ArrayList(); constraints.add(new LinearConstraint(new double[] {0.0, 39.0, 23.0, 96.0, 15.0, 48.0, 9.0, 21.0, 48.0, 36.0, 76.0, 19.0, 88.0, 17.0, 16.0, 36.0,}, Relationship.GEQ, 15.0)); constraints.add(new LinearConstraint(new double[] {0.0, 59.0, 93.0, 12.0, 29.0, 78.0, 73.0, 87.0, 32.0, 70.0, 68.0, 24.0, 11.0, 26.0, 65.0, 25.0,}, Relationship.GEQ, 29.0)); constraints.add(new LinearConstraint(new double[] {0.0, 74.0, 5.0, 82.0, 6.0, 97.0, 55.0, 44.0, 52.0, 54.0, 5.0, 93.0, 91.0, 8.0, 20.0, 97.0,}, Relationship.GEQ, 6.0)); constraints.add(new LinearConstraint(new double[] {8.0, -3.0, -28.0, -72.0, -8.0, -31.0, -31.0, -74.0, -47.0, -59.0, -24.0, -57.0, -56.0, -16.0, -92.0, -59.0,}, Relationship.GEQ, 0.0)); constraints.add(new LinearConstraint(new double[] {25.0, -7.0, -99.0, -78.0, -25.0, -14.0, -16.0, -89.0, -39.0, -56.0, -53.0, -9.0, -18.0, -26.0, -11.0, -61.0,}, Relationship.GEQ, 0.0)); constraints.add(new LinearConstraint(new double[] {33.0, -95.0, -15.0, -4.0, -33.0, -3.0, -20.0, -96.0, -27.0, -13.0, -80.0, -24.0, -3.0, -13.0, -57.0, -76.0,}, Relationship.GEQ, 0.0)); constraints.add(new LinearConstraint(new double[] {7.0, -95.0, -39.0, -93.0, -7.0, -94.0, -94.0, -62.0, -76.0, -26.0, -53.0, -57.0, -31.0, -76.0, -53.0, -52.0,}, Relationship.GEQ, 0.0)); double epsilon = 1e-6; PointValuePair solution = new SimplexSolver().optimize(f, constraints, GoalType.MINIMIZE, true); Assert.assertEquals(1.0d, solution.getValue(), epsilon); Assert.assertTrue(validSolution(solution, constraints, epsilon)); } @Test public void testMath828Cycle() { LinearObjectiveFunction f = new LinearObjectiveFunction( new double[] { 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0}, 0.0); ArrayList constraints = new ArrayList(); constraints.add(new LinearConstraint(new double[] {0.0, 16.0, 14.0, 69.0, 1.0, 85.0, 52.0, 43.0, 64.0, 97.0, 14.0, 74.0, 89.0, 28.0, 94.0, 58.0, 13.0, 22.0, 21.0, 17.0, 30.0, 25.0, 1.0, 59.0, 91.0, 78.0, 12.0, 74.0, 56.0, 3.0, 88.0,}, Relationship.GEQ, 91.0)); constraints.add(new LinearConstraint(new double[] {0.0, 60.0, 40.0, 81.0, 71.0, 72.0, 46.0, 45.0, 38.0, 48.0, 40.0, 17.0, 33.0, 85.0, 64.0, 32.0, 84.0, 3.0, 54.0, 44.0, 71.0, 67.0, 90.0, 95.0, 54.0, 99.0, 99.0, 29.0, 52.0, 98.0, 9.0,}, Relationship.GEQ, 54.0)); constraints.add(new LinearConstraint(new double[] {0.0, 41.0, 12.0, 86.0, 90.0, 61.0, 31.0, 41.0, 23.0, 89.0, 17.0, 74.0, 44.0, 27.0, 16.0, 47.0, 80.0, 32.0, 11.0, 56.0, 68.0, 82.0, 11.0, 62.0, 62.0, 53.0, 39.0, 16.0, 48.0, 1.0, 63.0,}, Relationship.GEQ, 62.0)); constraints.add(new LinearConstraint(new double[] {83.0, -76.0, -94.0, -19.0, -15.0, -70.0, -72.0, -57.0, -63.0, -65.0, -22.0, -94.0, -22.0, -88.0, -86.0, -89.0, -72.0, -16.0, -80.0, -49.0, -70.0, -93.0, -95.0, -17.0, -83.0, -97.0, -31.0, -47.0, -31.0, -13.0, -23.0,}, Relationship.GEQ, 0.0)); constraints.add(new LinearConstraint(new double[] {41.0, -96.0, -41.0, -48.0, -70.0, -43.0, -43.0, -43.0, -97.0, -37.0, -85.0, -70.0, -45.0, -67.0, -87.0, -69.0, -94.0, -54.0, -54.0, -92.0, -79.0, -10.0, -35.0, -20.0, -41.0, -41.0, -65.0, -25.0, -12.0, -8.0, -46.0,}, Relationship.GEQ, 0.0)); constraints.add(new LinearConstraint(new double[] {27.0, -42.0, -65.0, -49.0, -53.0, -42.0, -17.0, -2.0, -61.0, -31.0, -76.0, -47.0, -8.0, -93.0, -86.0, -62.0, -65.0, -63.0, -22.0, -43.0, -27.0, -23.0, -32.0, -74.0, -27.0, -63.0, -47.0, -78.0, -29.0, -95.0, -73.0,}, Relationship.GEQ, 0.0)); constraints.add(new LinearConstraint(new double[] {15.0, -46.0, -41.0, -83.0, -98.0, -99.0, -21.0, -35.0, -7.0, -14.0, -80.0, -63.0, -18.0, -42.0, -5.0, -34.0, -56.0, -70.0, -16.0, -18.0, -74.0, -61.0, -47.0, -41.0, -15.0, -79.0, -18.0, -47.0, -88.0, -68.0, -55.0,}, Relationship.GEQ, 0.0)); double epsilon = 1e-6; PointValuePair solution = new SimplexSolver().optimize(f, constraints, GoalType.MINIMIZE, true); Assert.assertEquals(1.0d, solution.getValue(), epsilon); Assert.assertTrue(validSolution(solution, constraints, epsilon)); } @Test public void testMath781() { LinearObjectiveFunction f = new LinearObjectiveFunction(new double[] { 2, 6, 7 }, 0); ArrayList constraints = new ArrayList(); constraints.add(new LinearConstraint(new double[] { 1, 2, 1 }, Relationship.LEQ, 2)); constraints.add(new LinearConstraint(new double[] { -1, 1, 1 }, Relationship.LEQ, -1)); constraints.add(new LinearConstraint(new double[] { 2, -3, 1 }, Relationship.LEQ, -1)); double epsilon = 1e-6; SimplexSolver solver = new SimplexSolver(); PointValuePair solution = solver.optimize(f, constraints, GoalType.MAXIMIZE, false); Assert.assertTrue(Precision.compareTo(solution.getPoint()[0], 0.0d, epsilon) > 0); Assert.assertTrue(Precision.compareTo(solution.getPoint()[1], 0.0d, epsilon) > 0); Assert.assertTrue(Precision.compareTo(solution.getPoint()[2], 0.0d, epsilon) < 0); Assert.assertEquals(2.0d, solution.getValue(), epsilon); } @Test public void testMath713NegativeVariable() { LinearObjectiveFunction f = new LinearObjectiveFunction(new double[] {1.0, 1.0}, 0.0d); ArrayList constraints = new ArrayList(); constraints.add(new LinearConstraint(new double[] {1, 0}, Relationship.EQ, 1)); double epsilon = 1e-6; SimplexSolver solver = new SimplexSolver(); PointValuePair solution = solver.optimize(f, constraints, GoalType.MINIMIZE, true); Assert.assertTrue(Precision.compareTo(solution.getPoint()[0], 0.0d, epsilon) >= 0); Assert.assertTrue(Precision.compareTo(solution.getPoint()[1], 0.0d, epsilon) >= 0); } @Test public void testMath434NegativeVariable() { LinearObjectiveFunction f = new LinearObjectiveFunction(new double[] {0.0, 0.0, 1.0}, 0.0d); ArrayList constraints = new ArrayList(); constraints.add(new LinearConstraint(new double[] {1, 1, 0}, Relationship.EQ, 5)); constraints.add(new LinearConstraint(new double[] {0, 0, 1}, Relationship.GEQ, -10)); double epsilon = 1e-6; SimplexSolver solver = new SimplexSolver(); PointValuePair solution = solver.optimize(f, constraints, GoalType.MINIMIZE, false); Assert.assertEquals(5.0, solution.getPoint()[0] + solution.getPoint()[1], epsilon); Assert.assertEquals(-10.0, solution.getPoint()[2], epsilon); Assert.assertEquals(-10.0, solution.getValue(), epsilon); } @Test(expected = NoFeasibleSolutionException.class) public void testMath434UnfeasibleSolution() { double epsilon = 1e-6; LinearObjectiveFunction f = new LinearObjectiveFunction(new double[] {1.0, 0.0}, 0.0); ArrayList constraints = new ArrayList(); constraints.add(new LinearConstraint(new double[] {epsilon/2, 0.5}, Relationship.EQ, 0)); constraints.add(new LinearConstraint(new double[] {1e-3, 0.1}, Relationship.EQ, 10)); SimplexSolver solver = new SimplexSolver(); // allowing only non-negative values, no feasible solution shall be found solver.optimize(f, constraints, GoalType.MINIMIZE, true); } @Test public void testMath434PivotRowSelection() { LinearObjectiveFunction f = new LinearObjectiveFunction(new double[] {1.0}, 0.0); double epsilon = 1e-6; ArrayList constraints = new ArrayList(); constraints.add(new LinearConstraint(new double[] {200}, Relationship.GEQ, 1)); constraints.add(new LinearConstraint(new double[] {100}, Relationship.GEQ, 0.499900001)); SimplexSolver solver = new SimplexSolver(); PointValuePair solution = solver.optimize(f, constraints, GoalType.MINIMIZE, false); Assert.assertTrue(Precision.compareTo(solution.getPoint()[0] * 200.d, 1.d, epsilon) >= 0); Assert.assertEquals(0.0050, solution.getValue(), epsilon); } @Test public void testMath434PivotRowSelection2() { LinearObjectiveFunction f = new LinearObjectiveFunction(new double[] {0.0d, 1.0d, 1.0d, 0.0d, 0.0d, 0.0d, 0.0d}, 0.0d); ArrayList constraints = new ArrayList(); constraints.add(new LinearConstraint(new double[] {1.0d, -0.1d, 0.0d, 0.0d, 0.0d, 0.0d, 0.0d}, Relationship.EQ, -0.1d)); constraints.add(new LinearConstraint(new double[] {1.0d, 0.0d, 0.0d, 0.0d, 0.0d, 0.0d, 0.0d}, Relationship.GEQ, -1e-18d)); constraints.add(new LinearConstraint(new double[] {0.0d, 1.0d, 0.0d, 0.0d, 0.0d, 0.0d, 0.0d}, Relationship.GEQ, 0.0d)); constraints.add(new LinearConstraint(new double[] {0.0d, 0.0d, 0.0d, 1.0d, 0.0d, -0.0128588d, 1e-5d}, Relationship.EQ, 0.0d)); constraints.add(new LinearConstraint(new double[] {0.0d, 0.0d, 0.0d, 0.0d, 1.0d, 1e-5d, -0.0128586d}, Relationship.EQ, 1e-10d)); constraints.add(new LinearConstraint(new double[] {0.0d, 0.0d, 1.0d, -1.0d, 0.0d, 0.0d, 0.0d}, Relationship.GEQ, 0.0d)); constraints.add(new LinearConstraint(new double[] {0.0d, 0.0d, 1.0d, 1.0d, 0.0d, 0.0d, 0.0d}, Relationship.GEQ, 0.0d)); constraints.add(new LinearConstraint(new double[] {0.0d, 0.0d, 1.0d, 0.0d, -1.0d, 0.0d, 0.0d}, Relationship.GEQ, 0.0d)); constraints.add(new LinearConstraint(new double[] {0.0d, 0.0d, 1.0d, 0.0d, 1.0d, 0.0d, 0.0d}, Relationship.GEQ, 0.0d)); double epsilon = 1e-7; SimplexSolver simplex = new SimplexSolver(); PointValuePair solution = simplex.optimize(f, constraints, GoalType.MINIMIZE, false); Assert.assertTrue(Precision.compareTo(solution.getPoint()[0], -1e-18d, epsilon) >= 0); Assert.assertEquals(1.0d, solution.getPoint()[1], epsilon); Assert.assertEquals(0.0d, solution.getPoint()[2], epsilon); Assert.assertEquals(1.0d, solution.getValue(), epsilon); } @Test public void testMath272() { LinearObjectiveFunction f = new LinearObjectiveFunction(new double[] { 2, 2, 1 }, 0); Collection constraints = new ArrayList(); constraints.add(new LinearConstraint(new double[] { 1, 1, 0 }, Relationship.GEQ, 1)); constraints.add(new LinearConstraint(new double[] { 1, 0, 1 }, Relationship.GEQ, 1)); constraints.add(new LinearConstraint(new double[] { 0, 1, 0 }, Relationship.GEQ, 1)); SimplexSolver solver = new SimplexSolver(); PointValuePair solution = solver.optimize(f, constraints, GoalType.MINIMIZE, true); Assert.assertEquals(0.0, solution.getPoint()[0], .0000001); Assert.assertEquals(1.0, solution.getPoint()[1], .0000001); Assert.assertEquals(1.0, solution.getPoint()[2], .0000001); Assert.assertEquals(3.0, solution.getValue(), .0000001); } @Test public void testMath286() { LinearObjectiveFunction f = new LinearObjectiveFunction(new double[] { 0.8, 0.2, 0.7, 0.3, 0.6, 0.4 }, 0 ); Collection constraints = new ArrayList(); constraints.add(new LinearConstraint(new double[] { 1, 0, 1, 0, 1, 0 }, Relationship.EQ, 23.0)); constraints.add(new LinearConstraint(new double[] { 0, 1, 0, 1, 0, 1 }, Relationship.EQ, 23.0)); constraints.add(new LinearConstraint(new double[] { 1, 0, 0, 0, 0, 0 }, Relationship.GEQ, 10.0)); constraints.add(new LinearConstraint(new double[] { 0, 0, 1, 0, 0, 0 }, Relationship.GEQ, 8.0)); constraints.add(new LinearConstraint(new double[] { 0, 0, 0, 0, 1, 0 }, Relationship.GEQ, 5.0)); SimplexSolver solver = new SimplexSolver(); PointValuePair solution = solver.optimize(f, constraints, GoalType.MAXIMIZE, true); Assert.assertEquals(25.8, solution.getValue(), .0000001); Assert.assertEquals(23.0, solution.getPoint()[0] + solution.getPoint()[2] + solution.getPoint()[4], 0.0000001); Assert.assertEquals(23.0, solution.getPoint()[1] + solution.getPoint()[3] + solution.getPoint()[5], 0.0000001); Assert.assertTrue(solution.getPoint()[0] >= 10.0 - 0.0000001); Assert.assertTrue(solution.getPoint()[2] >= 8.0 - 0.0000001); Assert.assertTrue(solution.getPoint()[4] >= 5.0 - 0.0000001); } @Test public void testDegeneracy() { LinearObjectiveFunction f = new LinearObjectiveFunction(new double[] { 0.8, 0.7 }, 0 ); Collection constraints = new ArrayList(); constraints.add(new LinearConstraint(new double[] { 1, 1 }, Relationship.LEQ, 18.0)); constraints.add(new LinearConstraint(new double[] { 1, 0 }, Relationship.GEQ, 10.0)); constraints.add(new LinearConstraint(new double[] { 0, 1 }, Relationship.GEQ, 8.0)); SimplexSolver solver = new SimplexSolver(); PointValuePair solution = solver.optimize(f, constraints, GoalType.MAXIMIZE, true); Assert.assertEquals(13.6, solution.getValue(), .0000001); } @Test public void testMath288() { LinearObjectiveFunction f = new LinearObjectiveFunction(new double[] { 7, 3, 0, 0 }, 0 ); Collection constraints = new ArrayList(); constraints.add(new LinearConstraint(new double[] { 3, 0, -5, 0 }, Relationship.LEQ, 0.0)); constraints.add(new LinearConstraint(new double[] { 2, 0, 0, -5 }, Relationship.LEQ, 0.0)); constraints.add(new LinearConstraint(new double[] { 0, 3, 0, -5 }, Relationship.LEQ, 0.0)); constraints.add(new LinearConstraint(new double[] { 1, 0, 0, 0 }, Relationship.LEQ, 1.0)); constraints.add(new LinearConstraint(new double[] { 0, 1, 0, 0 }, Relationship.LEQ, 1.0)); SimplexSolver solver = new SimplexSolver(); PointValuePair solution = solver.optimize(f, constraints, GoalType.MAXIMIZE, true); Assert.assertEquals(10.0, solution.getValue(), .0000001); } @Test public void testMath290GEQ() { LinearObjectiveFunction f = new LinearObjectiveFunction(new double[] { 1, 5 }, 0 ); Collection constraints = new ArrayList(); constraints.add(new LinearConstraint(new double[] { 2, 0 }, Relationship.GEQ, -1.0)); SimplexSolver solver = new SimplexSolver(); PointValuePair solution = solver.optimize(f, constraints, GoalType.MINIMIZE, true); Assert.assertEquals(0, solution.getValue(), .0000001); Assert.assertEquals(0, solution.getPoint()[0], .0000001); Assert.assertEquals(0, solution.getPoint()[1], .0000001); } @Test(expected=NoFeasibleSolutionException.class) public void testMath290LEQ() { LinearObjectiveFunction f = new LinearObjectiveFunction(new double[] { 1, 5 }, 0 ); Collection constraints = new ArrayList(); constraints.add(new LinearConstraint(new double[] { 2, 0 }, Relationship.LEQ, -1.0)); SimplexSolver solver = new SimplexSolver(); solver.optimize(f, constraints, GoalType.MINIMIZE, true); } @Test public void testMath293() { LinearObjectiveFunction f = new LinearObjectiveFunction(new double[] { 0.8, 0.2, 0.7, 0.3, 0.4, 0.6}, 0 ); Collection constraints = new ArrayList(); constraints.add(new LinearConstraint(new double[] { 1, 0, 1, 0, 1, 0 }, Relationship.EQ, 30.0)); constraints.add(new LinearConstraint(new double[] { 0, 1, 0, 1, 0, 1 }, Relationship.EQ, 30.0)); constraints.add(new LinearConstraint(new double[] { 0.8, 0.2, 0.0, 0.0, 0.0, 0.0 }, Relationship.GEQ, 10.0)); constraints.add(new LinearConstraint(new double[] { 0.0, 0.0, 0.7, 0.3, 0.0, 0.0 }, Relationship.GEQ, 10.0)); constraints.add(new LinearConstraint(new double[] { 0.0, 0.0, 0.0, 0.0, 0.4, 0.6 }, Relationship.GEQ, 10.0)); SimplexSolver solver = new SimplexSolver(); PointValuePair solution1 = solver.optimize(f, constraints, GoalType.MAXIMIZE, true); Assert.assertEquals(15.7143, solution1.getPoint()[0], .0001); Assert.assertEquals(0.0, solution1.getPoint()[1], .0001); Assert.assertEquals(14.2857, solution1.getPoint()[2], .0001); Assert.assertEquals(0.0, solution1.getPoint()[3], .0001); Assert.assertEquals(0.0, solution1.getPoint()[4], .0001); Assert.assertEquals(30.0, solution1.getPoint()[5], .0001); Assert.assertEquals(40.57143, solution1.getValue(), .0001); double valA = 0.8 * solution1.getPoint()[0] + 0.2 * solution1.getPoint()[1]; double valB = 0.7 * solution1.getPoint()[2] + 0.3 * solution1.getPoint()[3]; double valC = 0.4 * solution1.getPoint()[4] + 0.6 * solution1.getPoint()[5]; f = new LinearObjectiveFunction(new double[] { 0.8, 0.2, 0.7, 0.3, 0.4, 0.6}, 0 ); constraints = new ArrayList(); constraints.add(new LinearConstraint(new double[] { 1, 0, 1, 0, 1, 0 }, Relationship.EQ, 30.0)); constraints.add(new LinearConstraint(new double[] { 0, 1, 0, 1, 0, 1 }, Relationship.EQ, 30.0)); constraints.add(new LinearConstraint(new double[] { 0.8, 0.2, 0.0, 0.0, 0.0, 0.0 }, Relationship.GEQ, valA)); constraints.add(new LinearConstraint(new double[] { 0.0, 0.0, 0.7, 0.3, 0.0, 0.0 }, Relationship.GEQ, valB)); constraints.add(new LinearConstraint(new double[] { 0.0, 0.0, 0.0, 0.0, 0.4, 0.6 }, Relationship.GEQ, valC)); PointValuePair solution2 = solver.optimize(f, constraints, GoalType.MAXIMIZE, true); Assert.assertEquals(40.57143, solution2.getValue(), .0001); } @Test public void testSimplexSolver() { LinearObjectiveFunction f = new LinearObjectiveFunction(new double[] { 15, 10 }, 7); Collection constraints = new ArrayList(); constraints.add(new LinearConstraint(new double[] { 1, 0 }, Relationship.LEQ, 2)); constraints.add(new LinearConstraint(new double[] { 0, 1 }, Relationship.LEQ, 3)); constraints.add(new LinearConstraint(new double[] { 1, 1 }, Relationship.EQ, 4)); SimplexSolver solver = new SimplexSolver(); PointValuePair solution = solver.optimize(f, constraints, GoalType.MAXIMIZE, false); Assert.assertEquals(2.0, solution.getPoint()[0], 0.0); Assert.assertEquals(2.0, solution.getPoint()[1], 0.0); Assert.assertEquals(57.0, solution.getValue(), 0.0); } @Test public void testSingleVariableAndConstraint() { LinearObjectiveFunction f = new LinearObjectiveFunction(new double[] { 3 }, 0); Collection constraints = new ArrayList(); constraints.add(new LinearConstraint(new double[] { 1 }, Relationship.LEQ, 10)); SimplexSolver solver = new SimplexSolver(); PointValuePair solution = solver.optimize(f, constraints, GoalType.MAXIMIZE, false); Assert.assertEquals(10.0, solution.getPoint()[0], 0.0); Assert.assertEquals(30.0, solution.getValue(), 0.0); } /** * With no artificial variables needed (no equals and no greater than * constraints) we can go straight to Phase 2. */ @Test public void testModelWithNoArtificialVars() { LinearObjectiveFunction f = new LinearObjectiveFunction(new double[] { 15, 10 }, 0); Collection constraints = new ArrayList(); constraints.add(new LinearConstraint(new double[] { 1, 0 }, Relationship.LEQ, 2)); constraints.add(new LinearConstraint(new double[] { 0, 1 }, Relationship.LEQ, 3)); constraints.add(new LinearConstraint(new double[] { 1, 1 }, Relationship.LEQ, 4)); SimplexSolver solver = new SimplexSolver(); PointValuePair solution = solver.optimize(f, constraints, GoalType.MAXIMIZE, false); Assert.assertEquals(2.0, solution.getPoint()[0], 0.0); Assert.assertEquals(2.0, solution.getPoint()[1], 0.0); Assert.assertEquals(50.0, solution.getValue(), 0.0); } @Test public void testMinimization() { LinearObjectiveFunction f = new LinearObjectiveFunction(new double[] { -2, 1 }, -5); Collection constraints = new ArrayList(); constraints.add(new LinearConstraint(new double[] { 1, 2 }, Relationship.LEQ, 6)); constraints.add(new LinearConstraint(new double[] { 3, 2 }, Relationship.LEQ, 12)); constraints.add(new LinearConstraint(new double[] { 0, 1 }, Relationship.GEQ, 0)); SimplexSolver solver = new SimplexSolver(); PointValuePair solution = solver.optimize(f, constraints, GoalType.MINIMIZE, false); Assert.assertEquals(4.0, solution.getPoint()[0], 0.0); Assert.assertEquals(0.0, solution.getPoint()[1], 0.0); Assert.assertEquals(-13.0, solution.getValue(), 0.0); } @Test public void testSolutionWithNegativeDecisionVariable() { LinearObjectiveFunction f = new LinearObjectiveFunction(new double[] { -2, 1 }, 0); Collection constraints = new ArrayList(); constraints.add(new LinearConstraint(new double[] { 1, 1 }, Relationship.GEQ, 6)); constraints.add(new LinearConstraint(new double[] { 1, 2 }, Relationship.LEQ, 14)); SimplexSolver solver = new SimplexSolver(); PointValuePair solution = solver.optimize(f, constraints, GoalType.MAXIMIZE, false); Assert.assertEquals(-2.0, solution.getPoint()[0], 0.0); Assert.assertEquals(8.0, solution.getPoint()[1], 0.0); Assert.assertEquals(12.0, solution.getValue(), 0.0); } @Test(expected = NoFeasibleSolutionException.class) public void testInfeasibleSolution() { LinearObjectiveFunction f = new LinearObjectiveFunction(new double[] { 15 }, 0); Collection constraints = new ArrayList(); constraints.add(new LinearConstraint(new double[] { 1 }, Relationship.LEQ, 1)); constraints.add(new LinearConstraint(new double[] { 1 }, Relationship.GEQ, 3)); SimplexSolver solver = new SimplexSolver(); solver.optimize(f, constraints, GoalType.MAXIMIZE, false); } @Test(expected = UnboundedSolutionException.class) public void testUnboundedSolution() { LinearObjectiveFunction f = new LinearObjectiveFunction(new double[] { 15, 10 }, 0); Collection constraints = new ArrayList(); constraints.add(new LinearConstraint(new double[] { 1, 0 }, Relationship.EQ, 2)); SimplexSolver solver = new SimplexSolver(); solver.optimize(f, constraints, GoalType.MAXIMIZE, false); } @Test public void testRestrictVariablesToNonNegative() { LinearObjectiveFunction f = new LinearObjectiveFunction(new double[] { 409, 523, 70, 204, 339 }, 0); Collection constraints = new ArrayList(); constraints.add(new LinearConstraint(new double[] { 43, 56, 345, 56, 5 }, Relationship.LEQ, 4567456)); constraints.add(new LinearConstraint(new double[] { 12, 45, 7, 56, 23 }, Relationship.LEQ, 56454)); constraints.add(new LinearConstraint(new double[] { 8, 768, 0, 34, 7456 }, Relationship.LEQ, 1923421)); constraints.add(new LinearConstraint(new double[] { 12342, 2342, 34, 678, 2342 }, Relationship.GEQ, 4356)); constraints.add(new LinearConstraint(new double[] { 45, 678, 76, 52, 23 }, Relationship.EQ, 456356)); SimplexSolver solver = new SimplexSolver(); PointValuePair solution = solver.optimize(f, constraints, GoalType.MAXIMIZE, true); Assert.assertEquals(2902.92783505155, solution.getPoint()[0], .0000001); Assert.assertEquals(480.419243986254, solution.getPoint()[1], .0000001); Assert.assertEquals(0.0, solution.getPoint()[2], .0000001); Assert.assertEquals(0.0, solution.getPoint()[3], .0000001); Assert.assertEquals(0.0, solution.getPoint()[4], .0000001); Assert.assertEquals(1438556.7491409, solution.getValue(), .0000001); } @Test public void testEpsilon() { LinearObjectiveFunction f = new LinearObjectiveFunction(new double[] { 10, 5, 1 }, 0); Collection constraints = new ArrayList(); constraints.add(new LinearConstraint(new double[] { 9, 8, 0 }, Relationship.EQ, 17)); constraints.add(new LinearConstraint(new double[] { 0, 7, 8 }, Relationship.LEQ, 7)); constraints.add(new LinearConstraint(new double[] { 10, 0, 2 }, Relationship.LEQ, 10)); SimplexSolver solver = new SimplexSolver(); PointValuePair solution = solver.optimize(f, constraints, GoalType.MAXIMIZE, false); Assert.assertEquals(1.0, solution.getPoint()[0], 0.0); Assert.assertEquals(1.0, solution.getPoint()[1], 0.0); Assert.assertEquals(0.0, solution.getPoint()[2], 0.0); Assert.assertEquals(15.0, solution.getValue(), 0.0); } @Test public void testTrivialModel() { LinearObjectiveFunction f = new LinearObjectiveFunction(new double[] { 1, 1 }, 0); Collection constraints = new ArrayList(); constraints.add(new LinearConstraint(new double[] { 1, 1 }, Relationship.EQ, 0)); SimplexSolver solver = new SimplexSolver(); PointValuePair solution = solver.optimize(f, constraints, GoalType.MAXIMIZE, true); Assert.assertEquals(0, solution.getValue(), .0000001); } @Test public void testLargeModel() { double[] objective = new double[] { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 12, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 12, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 12, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 12, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 12, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 12, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}; LinearObjectiveFunction f = new LinearObjectiveFunction(objective, 0); Collection constraints = new ArrayList(); constraints.add(equationFromString(objective.length, "x0 + x1 + x2 + x3 - x12 = 0")); constraints.add(equationFromString(objective.length, "x4 + x5 + x6 + x7 + x8 + x9 + x10 + x11 - x13 = 0")); constraints.add(equationFromString(objective.length, "x4 + x5 + x6 + x7 + x8 + x9 + x10 + x11 >= 49")); constraints.add(equationFromString(objective.length, "x0 + x1 + x2 + x3 >= 42")); constraints.add(equationFromString(objective.length, "x14 + x15 + x16 + x17 - x26 = 0")); constraints.add(equationFromString(objective.length, "x18 + x19 + x20 + x21 + x22 + x23 + x24 + x25 - x27 = 0")); constraints.add(equationFromString(objective.length, "x14 + x15 + x16 + x17 - x12 = 0")); constraints.add(equationFromString(objective.length, "x18 + x19 + x20 + x21 + x22 + x23 + x24 + x25 - x13 = 0")); constraints.add(equationFromString(objective.length, "x28 + x29 + x30 + x31 - x40 = 0")); constraints.add(equationFromString(objective.length, "x32 + x33 + x34 + x35 + x36 + x37 + x38 + x39 - x41 = 0")); constraints.add(equationFromString(objective.length, "x32 + x33 + x34 + x35 + x36 + x37 + x38 + x39 >= 49")); constraints.add(equationFromString(objective.length, "x28 + x29 + x30 + x31 >= 42")); constraints.add(equationFromString(objective.length, "x42 + x43 + x44 + x45 - x54 = 0")); constraints.add(equationFromString(objective.length, "x46 + x47 + x48 + x49 + x50 + x51 + x52 + x53 - x55 = 0")); constraints.add(equationFromString(objective.length, "x42 + x43 + x44 + x45 - x40 = 0")); constraints.add(equationFromString(objective.length, "x46 + x47 + x48 + x49 + x50 + x51 + x52 + x53 - x41 = 0")); constraints.add(equationFromString(objective.length, "x56 + x57 + x58 + x59 - x68 = 0")); constraints.add(equationFromString(objective.length, "x60 + x61 + x62 + x63 + x64 + x65 + x66 + x67 - x69 = 0")); constraints.add(equationFromString(objective.length, "x60 + x61 + x62 + x63 + x64 + x65 + x66 + x67 >= 51")); constraints.add(equationFromString(objective.length, "x56 + x57 + x58 + x59 >= 44")); constraints.add(equationFromString(objective.length, "x70 + x71 + x72 + x73 - x82 = 0")); constraints.add(equationFromString(objective.length, "x74 + x75 + x76 + x77 + x78 + x79 + x80 + x81 - x83 = 0")); constraints.add(equationFromString(objective.length, "x70 + x71 + x72 + x73 - x68 = 0")); constraints.add(equationFromString(objective.length, "x74 + x75 + x76 + x77 + x78 + x79 + x80 + x81 - x69 = 0")); constraints.add(equationFromString(objective.length, "x84 + x85 + x86 + x87 - x96 = 0")); constraints.add(equationFromString(objective.length, "x88 + x89 + x90 + x91 + x92 + x93 + x94 + x95 - x97 = 0")); constraints.add(equationFromString(objective.length, "x88 + x89 + x90 + x91 + x92 + x93 + x94 + x95 >= 51")); constraints.add(equationFromString(objective.length, "x84 + x85 + x86 + x87 >= 44")); constraints.add(equationFromString(objective.length, "x98 + x99 + x100 + x101 - x110 = 0")); constraints.add(equationFromString(objective.length, "x102 + x103 + x104 + x105 + x106 + x107 + x108 + x109 - x111 = 0")); constraints.add(equationFromString(objective.length, "x98 + x99 + x100 + x101 - x96 = 0")); constraints.add(equationFromString(objective.length, "x102 + x103 + x104 + x105 + x106 + x107 + x108 + x109 - x97 = 0")); constraints.add(equationFromString(objective.length, "x112 + x113 + x114 + x115 - x124 = 0")); constraints.add(equationFromString(objective.length, "x116 + x117 + x118 + x119 + x120 + x121 + x122 + x123 - x125 = 0")); constraints.add(equationFromString(objective.length, "x116 + x117 + x118 + x119 + x120 + x121 + x122 + x123 >= 49")); constraints.add(equationFromString(objective.length, "x112 + x113 + x114 + x115 >= 42")); constraints.add(equationFromString(objective.length, "x126 + x127 + x128 + x129 - x138 = 0")); constraints.add(equationFromString(objective.length, "x130 + x131 + x132 + x133 + x134 + x135 + x136 + x137 - x139 = 0")); constraints.add(equationFromString(objective.length, "x126 + x127 + x128 + x129 - x124 = 0")); constraints.add(equationFromString(objective.length, "x130 + x131 + x132 + x133 + x134 + x135 + x136 + x137 - x125 = 0")); constraints.add(equationFromString(objective.length, "x140 + x141 + x142 + x143 - x152 = 0")); constraints.add(equationFromString(objective.length, "x144 + x145 + x146 + x147 + x148 + x149 + x150 + x151 - x153 = 0")); constraints.add(equationFromString(objective.length, "x144 + x145 + x146 + x147 + x148 + x149 + x150 + x151 >= 59")); constraints.add(equationFromString(objective.length, "x140 + x141 + x142 + x143 >= 42")); constraints.add(equationFromString(objective.length, "x154 + x155 + x156 + x157 - x166 = 0")); constraints.add(equationFromString(objective.length, "x158 + x159 + x160 + x161 + x162 + x163 + x164 + x165 - x167 = 0")); constraints.add(equationFromString(objective.length, "x154 + x155 + x156 + x157 - x152 = 0")); constraints.add(equationFromString(objective.length, "x158 + x159 + x160 + x161 + x162 + x163 + x164 + x165 - x153 = 0")); constraints.add(equationFromString(objective.length, "x83 + x82 - x168 = 0")); constraints.add(equationFromString(objective.length, "x111 + x110 - x169 = 0")); constraints.add(equationFromString(objective.length, "x170 - x182 = 0")); constraints.add(equationFromString(objective.length, "x171 - x183 = 0")); constraints.add(equationFromString(objective.length, "x172 - x184 = 0")); constraints.add(equationFromString(objective.length, "x173 - x185 = 0")); constraints.add(equationFromString(objective.length, "x174 - x186 = 0")); constraints.add(equationFromString(objective.length, "x175 + x176 - x187 = 0")); constraints.add(equationFromString(objective.length, "x177 - x188 = 0")); constraints.add(equationFromString(objective.length, "x178 - x189 = 0")); constraints.add(equationFromString(objective.length, "x179 - x190 = 0")); constraints.add(equationFromString(objective.length, "x180 - x191 = 0")); constraints.add(equationFromString(objective.length, "x181 - x192 = 0")); constraints.add(equationFromString(objective.length, "x170 - x26 = 0")); constraints.add(equationFromString(objective.length, "x171 - x27 = 0")); constraints.add(equationFromString(objective.length, "x172 - x54 = 0")); constraints.add(equationFromString(objective.length, "x173 - x55 = 0")); constraints.add(equationFromString(objective.length, "x174 - x168 = 0")); constraints.add(equationFromString(objective.length, "x177 - x169 = 0")); constraints.add(equationFromString(objective.length, "x178 - x138 = 0")); constraints.add(equationFromString(objective.length, "x179 - x139 = 0")); constraints.add(equationFromString(objective.length, "x180 - x166 = 0")); constraints.add(equationFromString(objective.length, "x181 - x167 = 0")); constraints.add(equationFromString(objective.length, "x193 - x205 = 0")); constraints.add(equationFromString(objective.length, "x194 - x206 = 0")); constraints.add(equationFromString(objective.length, "x195 - x207 = 0")); constraints.add(equationFromString(objective.length, "x196 - x208 = 0")); constraints.add(equationFromString(objective.length, "x197 - x209 = 0")); constraints.add(equationFromString(objective.length, "x198 + x199 - x210 = 0")); constraints.add(equationFromString(objective.length, "x200 - x211 = 0")); constraints.add(equationFromString(objective.length, "x201 - x212 = 0")); constraints.add(equationFromString(objective.length, "x202 - x213 = 0")); constraints.add(equationFromString(objective.length, "x203 - x214 = 0")); constraints.add(equationFromString(objective.length, "x204 - x215 = 0")); constraints.add(equationFromString(objective.length, "x193 - x182 = 0")); constraints.add(equationFromString(objective.length, "x194 - x183 = 0")); constraints.add(equationFromString(objective.length, "x195 - x184 = 0")); constraints.add(equationFromString(objective.length, "x196 - x185 = 0")); constraints.add(equationFromString(objective.length, "x197 - x186 = 0")); constraints.add(equationFromString(objective.length, "x198 + x199 - x187 = 0")); constraints.add(equationFromString(objective.length, "x200 - x188 = 0")); constraints.add(equationFromString(objective.length, "x201 - x189 = 0")); constraints.add(equationFromString(objective.length, "x202 - x190 = 0")); constraints.add(equationFromString(objective.length, "x203 - x191 = 0")); constraints.add(equationFromString(objective.length, "x204 - x192 = 0")); SimplexSolver solver = new SimplexSolver(); PointValuePair solution = solver.optimize(f, constraints, GoalType.MINIMIZE, true); Assert.assertEquals(7518.0, solution.getValue(), .0000001); } /** * Converts a test string to a {@link LinearConstraint}. * Ex: x0 + x1 + x2 + x3 - x12 = 0 */ private LinearConstraint equationFromString(int numCoefficients, String s) { Relationship relationship; if (s.contains(">=")) { relationship = Relationship.GEQ; } else if (s.contains("<=")) { relationship = Relationship.LEQ; } else if (s.contains("=")) { relationship = Relationship.EQ; } else { throw new IllegalArgumentException(); } String[] equationParts = s.split("[>|<]?="); double rhs = Double.parseDouble(equationParts[1].trim()); double[] lhs = new double[numCoefficients]; String left = equationParts[0].replaceAll(" ?x", ""); String[] coefficients = left.split(" "); for (String coefficient : coefficients) { double value = coefficient.charAt(0) == '-' ? -1 : 1; int index = Integer.parseInt(coefficient.replaceFirst("[+|-]", "").trim()); lhs[index] = value; } return new LinearConstraint(lhs, relationship, rhs); } private static boolean validSolution(PointValuePair solution, List constraints, double epsilon) { double[] vals = solution.getPoint(); for (LinearConstraint c : constraints) { double[] coeffs = c.getCoefficients().toArray(); double result = 0.0d; for (int i = 0; i < vals.length; i++) { result += vals[i] * coeffs[i]; } switch (c.getRelationship()) { case EQ: if (!Precision.equals(result, c.getValue(), epsilon)) { return false; } break; case GEQ: if (Precision.compareTo(result, c.getValue(), epsilon) < 0) { return false; } break; case LEQ: if (Precision.compareTo(result, c.getValue(), epsilon) > 0) { return false; } break; } } return true; } } ././@LongLink100644 0 0 170 12126630646 10257 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/optimization/general/RandomStraightLinePointGenerator.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/optimization/general/RandomStraightLine100644 1750 1750 7172 12126627671 32414 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.optimization.general; import java.awt.geom.Point2D; import org.apache.commons.math3.random.RandomGenerator; import org.apache.commons.math3.random.Well44497b; import org.apache.commons.math3.distribution.RealDistribution; import org.apache.commons.math3.distribution.UniformRealDistribution; import org.apache.commons.math3.distribution.NormalDistribution; /** * Factory for generating a cloud of points that approximate a straight line. */ public class RandomStraightLinePointGenerator { /** Slope. */ private final double slope; /** Intercept. */ private final double intercept; /** RNG for the x-coordinate. */ private final RealDistribution x; /** RNG for the error on the y-coordinate. */ private final RealDistribution error; /** * The generator will create a cloud of points whose x-coordinates * will be randomly sampled between {@code xLo} and {@code xHi}, and * the corresponding y-coordinates will be computed as *
    
         *  y = a x + b + N(0, error)
         * 
    * where {@code N(mean, sigma)} is a Gaussian distribution with the * given mean and standard deviation. * * @param a Slope. * @param b Intercept. * @param sigma Standard deviation on the y-coordinate of the point. * @param lo Lowest value of the x-coordinate. * @param hi Highest value of the x-coordinate. * @param seed RNG seed. */ public RandomStraightLinePointGenerator(double a, double b, double sigma, double lo, double hi, long seed) { final RandomGenerator rng = new Well44497b(seed); slope = a; intercept = b; error = new NormalDistribution(rng, 0, sigma, NormalDistribution.DEFAULT_INVERSE_ABSOLUTE_ACCURACY); x = new UniformRealDistribution(rng, lo, hi, UniformRealDistribution.DEFAULT_INVERSE_ABSOLUTE_ACCURACY); } /** * Point generator. * * @param n Number of points to create. * @return the cloud of {@code n} points. */ public Point2D.Double[] generate(int n) { final Point2D.Double[] cloud = new Point2D.Double[n]; for (int i = 0; i < n; i++) { cloud[i] = create(); } return cloud; } /** * Create one point. * * @return a point. */ private Point2D.Double create() { final double abscissa = x.sample(); final double yModel = slope * abscissa + intercept; final double ordinate = yModel + error.sample(); return new Point2D.Double(abscissa, ordinate); } } ././@LongLink100644 0 0 171 12126630646 10260 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/optimization/general/AbstractLeastSquaresOptimizerTest.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/optimization/general/AbstractLeastSquar100644 1750 1750 10302 12126627671 32433 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with this * work for additional information regarding copyright ownership. The ASF * licenses this file to You 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. */ package org.apache.commons.math3.optimization.general; import java.io.IOException; import java.util.Arrays; import org.junit.Assert; import org.apache.commons.math3.optimization.PointVectorValuePair; import org.apache.commons.math3.util.FastMath; import org.junit.Test; public class AbstractLeastSquaresOptimizerTest { public static AbstractLeastSquaresOptimizer createOptimizer() { return new AbstractLeastSquaresOptimizer(null) { @Override protected PointVectorValuePair doOptimize() { final double[] params = getStartPoint(); final double[] res = computeResiduals(computeObjectiveValue(params)); setCost(computeCost(res)); return new PointVectorValuePair(params, null); } }; } @Test public void testGetChiSquare() throws IOException { final StatisticalReferenceDataset dataset; dataset = StatisticalReferenceDatasetFactory.createKirby2(); final AbstractLeastSquaresOptimizer optimizer; optimizer = createOptimizer(); final double[] a = dataset.getParameters(); final double[] y = dataset.getData()[1]; final double[] w = new double[y.length]; Arrays.fill(w, 1.0); optimizer.optimize(1, dataset.getLeastSquaresProblem(), y, w, a); final double expected = dataset.getResidualSumOfSquares(); final double actual = optimizer.getChiSquare(); Assert.assertEquals(dataset.getName(), expected, actual, 1E-11 * expected); } @Test public void testGetRMS() throws IOException { final StatisticalReferenceDataset dataset; dataset = StatisticalReferenceDatasetFactory.createKirby2(); final AbstractLeastSquaresOptimizer optimizer; optimizer = createOptimizer(); final double[] a = dataset.getParameters(); final double[] y = dataset.getData()[1]; final double[] w = new double[y.length]; Arrays.fill(w, 1.0); optimizer.optimize(1, dataset.getLeastSquaresProblem(), y, w, a); final double expected = FastMath .sqrt(dataset.getResidualSumOfSquares() / dataset.getNumObservations()); final double actual = optimizer.getRMS(); Assert.assertEquals(dataset.getName(), expected, actual, 1E-11 * expected); } @Test public void testComputeSigma() throws IOException { final StatisticalReferenceDataset dataset; dataset = StatisticalReferenceDatasetFactory.createKirby2(); final AbstractLeastSquaresOptimizer optimizer; optimizer = createOptimizer(); final double[] a = dataset.getParameters(); final double[] y = dataset.getData()[1]; final double[] w = new double[y.length]; Arrays.fill(w, 1.0); final int dof = y.length - a.length; final PointVectorValuePair optimum = optimizer.optimize(1, dataset.getLeastSquaresProblem(), y, w, a); final double[] sig = optimizer.computeSigma(optimum.getPoint(), 1e-14); final double[] expected = dataset.getParametersStandardDeviations(); for (int i = 0; i < sig.length; i++) { final double actual = FastMath.sqrt(optimizer.getChiSquare() / dof) * sig[i]; Assert.assertEquals(dataset.getName() + ", parameter #" + i, expected[i], actual, 1e-7 * expected[i]); } } } commons-math3-3.2-src/src/test/java/org/apache/commons/math3/optimization/general/CircleScalar.java100644 1750 1750 6001 12126627671 32113 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.optimization.general; import java.util.ArrayList; import org.apache.commons.math3.analysis.differentiation.DerivativeStructure; import org.apache.commons.math3.analysis.differentiation.MultivariateDifferentiableFunction; import org.apache.commons.math3.geometry.euclidean.twod.Vector2D; /** * Class used in the tests. */ public class CircleScalar implements MultivariateDifferentiableFunction { private ArrayList points; public CircleScalar() { points = new ArrayList(); } public void addPoint(double px, double py) { points.add(new Vector2D(px, py)); } public double getRadius(Vector2D center) { double r = 0; for (Vector2D point : points) { r += point.distance(center); } return r / points.size(); } private DerivativeStructure distance(Vector2D point, DerivativeStructure cx, DerivativeStructure cy) { DerivativeStructure dx = cx.subtract(point.getX()); DerivativeStructure dy = cy.subtract(point.getY()); return dx.multiply(dx).add(dy.multiply(dy)).sqrt(); } public DerivativeStructure getRadius(DerivativeStructure cx, DerivativeStructure cy) { DerivativeStructure r = cx.getField().getZero(); for (Vector2D point : points) { r = r.add(distance(point, cx, cy)); } return r.divide(points.size()); } public double value(double[] variables) { Vector2D center = new Vector2D(variables[0], variables[1]); double radius = getRadius(center); double sum = 0; for (Vector2D point : points) { double di = point.distance(center) - radius; sum += di * di; } return sum; } public DerivativeStructure value(DerivativeStructure[] variables) { DerivativeStructure radius = getRadius(variables[0], variables[1]); DerivativeStructure sum = variables[0].getField().getZero(); for (Vector2D point : points) { DerivativeStructure di = distance(point, variables[0], variables[1]).subtract(radius); sum = sum.add(di.multiply(di)); } return sum; } } ././@LongLink100644 0 0 162 12126630646 10260 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/optimization/general/RandomCirclePointGenerator.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/optimization/general/RandomCirclePointG100644 1750 1750 7065 12126627671 32342 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.optimization.general; import org.apache.commons.math3.random.RandomGenerator; import org.apache.commons.math3.random.Well44497b; import org.apache.commons.math3.util.MathUtils; import org.apache.commons.math3.util.FastMath; import org.apache.commons.math3.distribution.RealDistribution; import org.apache.commons.math3.distribution.UniformRealDistribution; import org.apache.commons.math3.distribution.NormalDistribution; import org.apache.commons.math3.geometry.euclidean.twod.Vector2D; /** * Factory for generating a cloud of points that approximate a circle. */ public class RandomCirclePointGenerator { /** RNG for the x-coordinate of the center. */ private final RealDistribution cX; /** RNG for the y-coordinate of the center. */ private final RealDistribution cY; /** RNG for the parametric position of the point. */ private final RealDistribution tP; /** Radius of the circle. */ private final double radius; /** * @param x Abscissa of the circle center. * @param y Ordinate of the circle center. * @param radius Radius of the circle. * @param xSigma Error on the x-coordinate of the circumference points. * @param ySigma Error on the y-coordinate of the circumference points. * @param seed RNG seed. */ public RandomCirclePointGenerator(double x, double y, double radius, double xSigma, double ySigma, long seed) { final RandomGenerator rng = new Well44497b(seed); this.radius = radius; cX = new NormalDistribution(rng, x, xSigma, NormalDistribution.DEFAULT_INVERSE_ABSOLUTE_ACCURACY); cY = new NormalDistribution(rng, y, ySigma, NormalDistribution.DEFAULT_INVERSE_ABSOLUTE_ACCURACY); tP = new UniformRealDistribution(rng, 0, MathUtils.TWO_PI, UniformRealDistribution.DEFAULT_INVERSE_ABSOLUTE_ACCURACY); } /** * Point generator. * * @param n Number of points to create. * @return the cloud of {@code n} points. */ public Vector2D[] generate(int n) { final Vector2D[] cloud = new Vector2D[n]; for (int i = 0; i < n; i++) { cloud[i] = create(); } return cloud; } /** * Create one point. * * @return a point. */ private Vector2D create() { final double t = tP.sample(); final double pX = cX.sample() + radius * FastMath.cos(t); final double pY = cY.sample() + radius * FastMath.sin(t); return new Vector2D(pX, pY); } } ././@LongLink100644 0 0 203 12126630646 10254 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/optimization/general/AbstractLeastSquaresOptimizerTestValidation.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/optimization/general/AbstractLeastSquar100644 1750 1750 30617 12126627671 32446 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with this * work for additional information regarding copyright ownership. The ASF * licenses this file to You 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. */ package org.apache.commons.math3.optimization.general; import java.util.Arrays; import java.util.List; import java.util.ArrayList; import java.awt.geom.Point2D; import org.apache.commons.math3.optimization.PointVectorValuePair; import org.apache.commons.math3.stat.descriptive.SummaryStatistics; import org.apache.commons.math3.stat.descriptive.StatisticalSummary; import org.apache.commons.math3.util.FastMath; import org.junit.Test; import org.junit.Assert; /** * This class demonstrates the main functionality of the * {@link AbstractLeastSquaresOptimizer}, common to the * optimizer implementations in package * {@link org.apache.commons.math3.optimization.general}. *
    * Not enabled by default, as the class name does not end with "Test". *
    * Invoke by running *
    
     *  mvn test -Dtest=AbstractLeastSquaresOptimizerTestValidation
     * 
    * or by running *
    
     *  mvn test -Dtest=AbstractLeastSquaresOptimizerTestValidation -DargLine="-DmcRuns=1234 -server"
     * 
    */ public class AbstractLeastSquaresOptimizerTestValidation { private static final int MONTE_CARLO_RUNS = Integer.parseInt(System.getProperty("mcRuns", "100")); /** * Using a Monte-Carlo procedure, this test checks the error estimations * as provided by the square-root of the diagonal elements of the * covariance matrix. *
    * The test generates sets of observations, each sampled from * a Gaussian distribution. *
    * The optimization problem solved is defined in class * {@link StraightLineProblem}. *
    * The output (on stdout) will be a table summarizing the distribution * of parameters generated by the Monte-Carlo process and by the direct * estimation provided by the diagonal elements of the covariance matrix. */ @Test public void testParametersErrorMonteCarloObservations() { // Error on the observations. final double yError = 15; // True values of the parameters. final double slope = 123.456; final double offset = -98.765; // Samples generator. final RandomStraightLinePointGenerator lineGenerator = new RandomStraightLinePointGenerator(slope, offset, yError, -1e3, 1e4, 138577L); // Number of observations. final int numObs = 100; // XXX Should be a command-line option. // number of parameters. final int numParams = 2; // Parameters found for each of Monte-Carlo run. final SummaryStatistics[] paramsFoundByDirectSolution = new SummaryStatistics[numParams]; // Sigma estimations (square-root of the diagonal elements of the // covariance matrix), for each Monte-Carlo run. final SummaryStatistics[] sigmaEstimate = new SummaryStatistics[numParams]; // Initialize statistics accumulators. for (int i = 0; i < numParams; i++) { paramsFoundByDirectSolution[i] = new SummaryStatistics(); sigmaEstimate[i] = new SummaryStatistics(); } // Dummy optimizer (to compute the covariance matrix). final AbstractLeastSquaresOptimizer optim = new DummyOptimizer(); final double[] init = { slope, offset }; // Monte-Carlo (generates many sets of observations). final int mcRepeat = MONTE_CARLO_RUNS; int mcCount = 0; while (mcCount < mcRepeat) { // Observations. final Point2D.Double[] obs = lineGenerator.generate(numObs); final StraightLineProblem problem = new StraightLineProblem(yError); for (int i = 0; i < numObs; i++) { final Point2D.Double p = obs[i]; problem.addPoint(p.x, p.y); } // Direct solution (using simple regression). final double[] regress = problem.solve(); // Estimation of the standard deviation (diagonal elements of the // covariance matrix). final PointVectorValuePair optimum = optim.optimize(Integer.MAX_VALUE, problem, problem.target(), problem.weight(), init); final double[] sigma = optim.computeSigma(optimum.getPoint(), 1e-14); // Accumulate statistics. for (int i = 0; i < numParams; i++) { paramsFoundByDirectSolution[i].addValue(regress[i]); sigmaEstimate[i].addValue(sigma[i]); } // Next Monte-Carlo. ++mcCount; } // Print statistics. final String line = "--------------------------------------------------------------"; System.out.println(" True value Mean Std deviation"); for (int i = 0; i < numParams; i++) { System.out.println(line); System.out.println("Parameter #" + i); StatisticalSummary s = paramsFoundByDirectSolution[i].getSummary(); System.out.printf(" %+.6e %+.6e %+.6e\n", init[i], s.getMean(), s.getStandardDeviation()); s = sigmaEstimate[i].getSummary(); System.out.printf("sigma: %+.6e (%+.6e)\n", s.getMean(), s.getStandardDeviation()); } System.out.println(line); // Check the error estimation. for (int i = 0; i < numParams; i++) { Assert.assertEquals(paramsFoundByDirectSolution[i].getSummary().getStandardDeviation(), sigmaEstimate[i].getSummary().getMean(), 8e-2); } } /** * In this test, the set of observations is fixed. * Using a Monte-Carlo procedure, it generates sets of parameters, * and determine the parameter change that will result in the * normalized chi-square becoming larger by one than the value from * the best fit solution. *
    * The optimization problem solved is defined in class * {@link StraightLineProblem}. *
    * The output (on stdout) will be a list of lines containing: *
      *
    • slope of the straight line,
    • *
    • intercept of the straight line,
    • *
    • chi-square of the solution defined by the above two values.
    • *
    * The output is separated into two blocks (with a blank line between * them); the first block will contain all parameter sets for which * {@code chi2 < chi2_b + 1} * and the second block, all sets for which * {@code chi2 >= chi2_b + 1} * where {@code chi2_b} is the lowest chi-square (corresponding to the * best solution). */ @Test public void testParametersErrorMonteCarloParameters() { // Error on the observations. final double yError = 15; // True values of the parameters. final double slope = 123.456; final double offset = -98.765; // Samples generator. final RandomStraightLinePointGenerator lineGenerator = new RandomStraightLinePointGenerator(slope, offset, yError, -1e3, 1e4, 13839013L); // Number of observations. final int numObs = 10; // number of parameters. final int numParams = 2; // Create a single set of observations. final Point2D.Double[] obs = lineGenerator.generate(numObs); final StraightLineProblem problem = new StraightLineProblem(yError); for (int i = 0; i < numObs; i++) { final Point2D.Double p = obs[i]; problem.addPoint(p.x, p.y); } // Direct solution (using simple regression). final double[] regress = problem.solve(); // Dummy optimizer (to compute the chi-square). final AbstractLeastSquaresOptimizer optim = new DummyOptimizer(); final double[] init = { slope, offset }; // Get chi-square of the best parameters set for the given set of // observations. final double bestChi2N = getChi2N(optim, problem, regress); final double[] sigma = optim.computeSigma(regress, 1e-14); // Monte-Carlo (generates a grid of parameters). final int mcRepeat = MONTE_CARLO_RUNS; final int gridSize = (int) FastMath.sqrt(mcRepeat); // Parameters found for each of Monte-Carlo run. // Index 0 = slope // Index 1 = offset // Index 2 = normalized chi2 final List paramsAndChi2 = new ArrayList(gridSize * gridSize); final double slopeRange = 10 * sigma[0]; final double offsetRange = 10 * sigma[1]; final double minSlope = slope - 0.5 * slopeRange; final double minOffset = offset - 0.5 * offsetRange; final double deltaSlope = slopeRange/ gridSize; final double deltaOffset = offsetRange / gridSize; for (int i = 0; i < gridSize; i++) { final double s = minSlope + i * deltaSlope; for (int j = 0; j < gridSize; j++) { final double o = minOffset + j * deltaOffset; final double chi2N = getChi2N(optim, problem, new double[] {s, o}); paramsAndChi2.add(new double[] {s, o, chi2N}); } } // Output (for use with "gnuplot"). // Some info. // For plotting separately sets of parameters that have a large chi2. final double chi2NPlusOne = bestChi2N + 1; int numLarger = 0; final String lineFmt = "%+.10e %+.10e %.8e\n"; // Point with smallest chi-square. System.out.printf(lineFmt, regress[0], regress[1], bestChi2N); System.out.println(); // Empty line. // Points within the confidence interval. for (double[] d : paramsAndChi2) { if (d[2] <= chi2NPlusOne) { System.out.printf(lineFmt, d[0], d[1], d[2]); } } System.out.println(); // Empty line. // Points outside the confidence interval. for (double[] d : paramsAndChi2) { if (d[2] > chi2NPlusOne) { ++numLarger; System.out.printf(lineFmt, d[0], d[1], d[2]); } } System.out.println(); // Empty line. System.out.println("# sigma=" + Arrays.toString(sigma)); System.out.println("# " + numLarger + " sets filtered out"); } /** * @return the normalized chi-square. */ private double getChi2N(AbstractLeastSquaresOptimizer optim, StraightLineProblem problem, double[] params) { final double[] t = problem.target(); final double[] w = problem.weight(); optim.optimize(Integer.MAX_VALUE, problem, t, w, params); return optim.getChiSquare() / (t.length - params.length); } } /** * A dummy optimizer. * Used for computing the covariance matrix. */ class DummyOptimizer extends AbstractLeastSquaresOptimizer { public DummyOptimizer() { super(null); } /** * This method does nothing and returns a dummy value. */ @Override public PointVectorValuePair doOptimize() { final double[] params = getStartPoint(); final double[] res = computeResiduals(computeObjectiveValue(params)); setCost(computeCost(res)); return new PointVectorValuePair(params, null); } } ././@LongLink100644 0 0 177 12126630646 10266 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/optimization/general/NonLinearConjugateGradientOptimizerTest.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/optimization/general/NonLinearConjugate100644 1750 1750 44207 12126627671 32423 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.optimization.general; import java.io.Serializable; import org.apache.commons.math3.analysis.DifferentiableMultivariateFunction; import org.apache.commons.math3.analysis.MultivariateFunction; import org.apache.commons.math3.analysis.MultivariateVectorFunction; import org.apache.commons.math3.analysis.differentiation.DerivativeStructure; import org.apache.commons.math3.analysis.differentiation.MultivariateDifferentiableFunction; import org.apache.commons.math3.analysis.solvers.BrentSolver; import org.apache.commons.math3.exception.DimensionMismatchException; import org.apache.commons.math3.exception.MathIllegalArgumentException; import org.apache.commons.math3.geometry.euclidean.twod.Vector2D; import org.apache.commons.math3.linear.BlockRealMatrix; import org.apache.commons.math3.linear.RealMatrix; import org.apache.commons.math3.optimization.GoalType; import org.apache.commons.math3.optimization.PointValuePair; import org.apache.commons.math3.optimization.SimpleValueChecker; import org.junit.Assert; import org.junit.Test; /** *

    Some of the unit tests are re-implementations of the MINPACK file17 and file22 test files. * The redistribution policy for MINPACK is available here, for * convenience, it is reproduced below.

    * * * *
    * Minpack Copyright Notice (1999) University of Chicago. * All rights reserved *
    * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: *
      *
    1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer.
    2. *
    3. Redistributions in binary form must reproduce the above * copyright notice, this list of conditions and the following * disclaimer in the documentation and/or other materials provided * with the distribution.
    4. *
    5. The end-user documentation included with the redistribution, if any, * must include the following acknowledgment: * This product includes software developed by the University of * Chicago, as Operator of Argonne National Laboratory. * Alternately, this acknowledgment may appear in the software itself, * if and wherever such third-party acknowledgments normally appear.
    6. *
    7. WARRANTY DISCLAIMER. THE SOFTWARE IS SUPPLIED "AS IS" * WITHOUT WARRANTY OF ANY KIND. THE COPYRIGHT HOLDER, THE * UNITED STATES, THE UNITED STATES DEPARTMENT OF ENERGY, AND * THEIR EMPLOYEES: (1) DISCLAIM ANY WARRANTIES, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO ANY IMPLIED WARRANTIES * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE * OR NON-INFRINGEMENT, (2) DO NOT ASSUME ANY LEGAL LIABILITY * OR RESPONSIBILITY FOR THE ACCURACY, COMPLETENESS, OR * USEFULNESS OF THE SOFTWARE, (3) DO NOT REPRESENT THAT USE OF * THE SOFTWARE WOULD NOT INFRINGE PRIVATELY OWNED RIGHTS, (4) * DO NOT WARRANT THAT THE SOFTWARE WILL FUNCTION * UNINTERRUPTED, THAT IT IS ERROR-FREE OR THAT ANY ERRORS WILL * BE CORRECTED.
    8. *
    9. LIMITATION OF LIABILITY. IN NO EVENT WILL THE COPYRIGHT * HOLDER, THE UNITED STATES, THE UNITED STATES DEPARTMENT OF * ENERGY, OR THEIR EMPLOYEES: BE LIABLE FOR ANY INDIRECT, * INCIDENTAL, CONSEQUENTIAL, SPECIAL OR PUNITIVE DAMAGES OF * ANY KIND OR NATURE, INCLUDING BUT NOT LIMITED TO LOSS OF * PROFITS OR LOSS OF DATA, FOR ANY REASON WHATSOEVER, WHETHER * SUCH LIABILITY IS ASSERTED ON THE BASIS OF CONTRACT, TORT * (INCLUDING NEGLIGENCE OR STRICT LIABILITY), OR OTHERWISE, * EVEN IF ANY OF SAID PARTIES HAS BEEN WARNED OF THE * POSSIBILITY OF SUCH LOSS OR DAMAGES.
    10. *
      * @author Argonne National Laboratory. MINPACK project. March 1980 (original fortran minpack tests) * @author Burton S. Garbow (original fortran minpack tests) * @author Kenneth E. Hillstrom (original fortran minpack tests) * @author Jorge J. More (original fortran minpack tests) * @author Luc Maisonobe (non-minpack tests and minpack tests Java translation) */ public class NonLinearConjugateGradientOptimizerTest { @Test public void testTrivial() { LinearProblem problem = new LinearProblem(new double[][] { { 2 } }, new double[] { 3 }); NonLinearConjugateGradientOptimizer optimizer = new NonLinearConjugateGradientOptimizer(ConjugateGradientFormula.POLAK_RIBIERE, new SimpleValueChecker(1e-6, 1e-6)); PointValuePair optimum = optimizer.optimize(100, problem, GoalType.MINIMIZE, new double[] { 0 }); Assert.assertEquals(1.5, optimum.getPoint()[0], 1.0e-10); Assert.assertEquals(0.0, optimum.getValue(), 1.0e-10); } @Test public void testColumnsPermutation() { LinearProblem problem = new LinearProblem(new double[][] { { 1.0, -1.0 }, { 0.0, 2.0 }, { 1.0, -2.0 } }, new double[] { 4.0, 6.0, 1.0 }); NonLinearConjugateGradientOptimizer optimizer = new NonLinearConjugateGradientOptimizer(ConjugateGradientFormula.POLAK_RIBIERE, new SimpleValueChecker(1e-6, 1e-6)); PointValuePair optimum = optimizer.optimize(100, problem, GoalType.MINIMIZE, new double[] { 0, 0 }); Assert.assertEquals(7.0, optimum.getPoint()[0], 1.0e-10); Assert.assertEquals(3.0, optimum.getPoint()[1], 1.0e-10); Assert.assertEquals(0.0, optimum.getValue(), 1.0e-10); } @Test public void testNoDependency() { LinearProblem problem = new LinearProblem(new double[][] { { 2, 0, 0, 0, 0, 0 }, { 0, 2, 0, 0, 0, 0 }, { 0, 0, 2, 0, 0, 0 }, { 0, 0, 0, 2, 0, 0 }, { 0, 0, 0, 0, 2, 0 }, { 0, 0, 0, 0, 0, 2 } }, new double[] { 0.0, 1.1, 2.2, 3.3, 4.4, 5.5 }); NonLinearConjugateGradientOptimizer optimizer = new NonLinearConjugateGradientOptimizer(ConjugateGradientFormula.POLAK_RIBIERE, new SimpleValueChecker(1e-6, 1e-6)); PointValuePair optimum = optimizer.optimize(100, problem, GoalType.MINIMIZE, new double[] { 0, 0, 0, 0, 0, 0 }); for (int i = 0; i < problem.target.length; ++i) { Assert.assertEquals(0.55 * i, optimum.getPoint()[i], 1.0e-10); } } @Test public void testOneSet() { LinearProblem problem = new LinearProblem(new double[][] { { 1, 0, 0 }, { -1, 1, 0 }, { 0, -1, 1 } }, new double[] { 1, 1, 1}); NonLinearConjugateGradientOptimizer optimizer = new NonLinearConjugateGradientOptimizer(ConjugateGradientFormula.POLAK_RIBIERE, new SimpleValueChecker(1e-6, 1e-6)); PointValuePair optimum = optimizer.optimize(100, problem, GoalType.MINIMIZE, new double[] { 0, 0, 0 }); Assert.assertEquals(1.0, optimum.getPoint()[0], 1.0e-10); Assert.assertEquals(2.0, optimum.getPoint()[1], 1.0e-10); Assert.assertEquals(3.0, optimum.getPoint()[2], 1.0e-10); } @Test public void testTwoSets() { final double epsilon = 1.0e-7; LinearProblem problem = new LinearProblem(new double[][] { { 2, 1, 0, 4, 0, 0 }, { -4, -2, 3, -7, 0, 0 }, { 4, 1, -2, 8, 0, 0 }, { 0, -3, -12, -1, 0, 0 }, { 0, 0, 0, 0, epsilon, 1 }, { 0, 0, 0, 0, 1, 1 } }, new double[] { 2, -9, 2, 2, 1 + epsilon * epsilon, 2}); final Preconditioner preconditioner = new Preconditioner() { public double[] precondition(double[] point, double[] r) { double[] d = r.clone(); d[0] /= 72.0; d[1] /= 30.0; d[2] /= 314.0; d[3] /= 260.0; d[4] /= 2 * (1 + epsilon * epsilon); d[5] /= 4.0; return d; } }; NonLinearConjugateGradientOptimizer optimizer = new NonLinearConjugateGradientOptimizer(ConjugateGradientFormula.POLAK_RIBIERE, new SimpleValueChecker(1e-13, 1e-13), new BrentSolver(), preconditioner); PointValuePair optimum = optimizer.optimize(100, problem, GoalType.MINIMIZE, new double[] { 0, 0, 0, 0, 0, 0 }); Assert.assertEquals( 3.0, optimum.getPoint()[0], 1.0e-10); Assert.assertEquals( 4.0, optimum.getPoint()[1], 1.0e-10); Assert.assertEquals(-1.0, optimum.getPoint()[2], 1.0e-10); Assert.assertEquals(-2.0, optimum.getPoint()[3], 1.0e-10); Assert.assertEquals( 1.0 + epsilon, optimum.getPoint()[4], 1.0e-10); Assert.assertEquals( 1.0 - epsilon, optimum.getPoint()[5], 1.0e-10); } @Test public void testNonInversible() { LinearProblem problem = new LinearProblem(new double[][] { { 1, 2, -3 }, { 2, 1, 3 }, { -3, 0, -9 } }, new double[] { 1, 1, 1 }); NonLinearConjugateGradientOptimizer optimizer = new NonLinearConjugateGradientOptimizer(ConjugateGradientFormula.POLAK_RIBIERE, new SimpleValueChecker(1e-6, 1e-6)); PointValuePair optimum = optimizer.optimize(100, problem, GoalType.MINIMIZE, new double[] { 0, 0, 0 }); Assert.assertTrue(optimum.getValue() > 0.5); } @Test public void testIllConditioned() { LinearProblem problem1 = new LinearProblem(new double[][] { { 10.0, 7.0, 8.0, 7.0 }, { 7.0, 5.0, 6.0, 5.0 }, { 8.0, 6.0, 10.0, 9.0 }, { 7.0, 5.0, 9.0, 10.0 } }, new double[] { 32, 23, 33, 31 }); NonLinearConjugateGradientOptimizer optimizer = new NonLinearConjugateGradientOptimizer(ConjugateGradientFormula.POLAK_RIBIERE, new SimpleValueChecker(1e-13, 1e-13), new BrentSolver(1e-15, 1e-15)); PointValuePair optimum1 = optimizer.optimize(200, problem1, GoalType.MINIMIZE, new double[] { 0, 1, 2, 3 }); Assert.assertEquals(1.0, optimum1.getPoint()[0], 1.0e-4); Assert.assertEquals(1.0, optimum1.getPoint()[1], 1.0e-4); Assert.assertEquals(1.0, optimum1.getPoint()[2], 1.0e-4); Assert.assertEquals(1.0, optimum1.getPoint()[3], 1.0e-4); LinearProblem problem2 = new LinearProblem(new double[][] { { 10.00, 7.00, 8.10, 7.20 }, { 7.08, 5.04, 6.00, 5.00 }, { 8.00, 5.98, 9.89, 9.00 }, { 6.99, 4.99, 9.00, 9.98 } }, new double[] { 32, 23, 33, 31 }); PointValuePair optimum2 = optimizer.optimize(200, problem2, GoalType.MINIMIZE, new double[] { 0, 1, 2, 3 }); Assert.assertEquals(-81.0, optimum2.getPoint()[0], 1.0e-1); Assert.assertEquals(137.0, optimum2.getPoint()[1], 1.0e-1); Assert.assertEquals(-34.0, optimum2.getPoint()[2], 1.0e-1); Assert.assertEquals( 22.0, optimum2.getPoint()[3], 1.0e-1); } @Test public void testMoreEstimatedParametersSimple() { LinearProblem problem = new LinearProblem(new double[][] { { 3.0, 2.0, 0.0, 0.0 }, { 0.0, 1.0, -1.0, 1.0 }, { 2.0, 0.0, 1.0, 0.0 } }, new double[] { 7.0, 3.0, 5.0 }); NonLinearConjugateGradientOptimizer optimizer = new NonLinearConjugateGradientOptimizer(ConjugateGradientFormula.POLAK_RIBIERE, new SimpleValueChecker(1e-6, 1e-6)); PointValuePair optimum = optimizer.optimize(100, problem, GoalType.MINIMIZE, new double[] { 7, 6, 5, 4 }); Assert.assertEquals(0, optimum.getValue(), 1.0e-10); } @Test public void testMoreEstimatedParametersUnsorted() { LinearProblem problem = new LinearProblem(new double[][] { { 1.0, 1.0, 0.0, 0.0, 0.0, 0.0 }, { 0.0, 0.0, 1.0, 1.0, 1.0, 0.0 }, { 0.0, 0.0, 0.0, 0.0, 1.0, -1.0 }, { 0.0, 0.0, -1.0, 1.0, 0.0, 1.0 }, { 0.0, 0.0, 0.0, -1.0, 1.0, 0.0 } }, new double[] { 3.0, 12.0, -1.0, 7.0, 1.0 }); NonLinearConjugateGradientOptimizer optimizer = new NonLinearConjugateGradientOptimizer(ConjugateGradientFormula.POLAK_RIBIERE, new SimpleValueChecker(1e-6, 1e-6)); PointValuePair optimum = optimizer.optimize(100, problem, GoalType.MINIMIZE, new double[] { 2, 2, 2, 2, 2, 2 }); Assert.assertEquals(0, optimum.getValue(), 1.0e-10); } @Test public void testRedundantEquations() { LinearProblem problem = new LinearProblem(new double[][] { { 1.0, 1.0 }, { 1.0, -1.0 }, { 1.0, 3.0 } }, new double[] { 3.0, 1.0, 5.0 }); NonLinearConjugateGradientOptimizer optimizer = new NonLinearConjugateGradientOptimizer(ConjugateGradientFormula.POLAK_RIBIERE, new SimpleValueChecker(1e-6, 1e-6)); PointValuePair optimum = optimizer.optimize(100, problem, GoalType.MINIMIZE, new double[] { 1, 1 }); Assert.assertEquals(2.0, optimum.getPoint()[0], 1.0e-8); Assert.assertEquals(1.0, optimum.getPoint()[1], 1.0e-8); } @Test public void testInconsistentEquations() { LinearProblem problem = new LinearProblem(new double[][] { { 1.0, 1.0 }, { 1.0, -1.0 }, { 1.0, 3.0 } }, new double[] { 3.0, 1.0, 4.0 }); NonLinearConjugateGradientOptimizer optimizer = new NonLinearConjugateGradientOptimizer(ConjugateGradientFormula.POLAK_RIBIERE, new SimpleValueChecker(1e-6, 1e-6)); PointValuePair optimum = optimizer.optimize(100, problem, GoalType.MINIMIZE, new double[] { 1, 1 }); Assert.assertTrue(optimum.getValue() > 0.1); } @Test public void testCircleFitting() { CircleScalar circle = new CircleScalar(); circle.addPoint( 30.0, 68.0); circle.addPoint( 50.0, -6.0); circle.addPoint(110.0, -20.0); circle.addPoint( 35.0, 15.0); circle.addPoint( 45.0, 97.0); NonLinearConjugateGradientOptimizer optimizer = new NonLinearConjugateGradientOptimizer(ConjugateGradientFormula.POLAK_RIBIERE, new SimpleValueChecker(1e-30, 1e-30), new BrentSolver(1e-15, 1e-13)); PointValuePair optimum = optimizer.optimize(100, circle, GoalType.MINIMIZE, new double[] { 98.680, 47.345 }); Vector2D center = new Vector2D(optimum.getPointRef()[0], optimum.getPointRef()[1]); Assert.assertEquals(69.960161753, circle.getRadius(center), 1.0e-8); Assert.assertEquals(96.075902096, center.getX(), 1.0e-8); Assert.assertEquals(48.135167894, center.getY(), 1.0e-8); } private static class LinearProblem implements MultivariateDifferentiableFunction, Serializable { private static final long serialVersionUID = 703247177355019415L; final RealMatrix factors; final double[] target; public LinearProblem(double[][] factors, double[] target) { this.factors = new BlockRealMatrix(factors); this.target = target; } public double value(double[] variables) { double[] y = factors.operate(variables); double sum = 0; for (int i = 0; i < y.length; ++i) { double ri = y[i] - target[i]; sum += ri * ri; } return sum; } public DerivativeStructure value(DerivativeStructure[] variables) { DerivativeStructure[] y = new DerivativeStructure[factors.getRowDimension()]; for (int i = 0; i < y.length; ++i) { y[i] = variables[0].getField().getZero(); for (int j = 0; j < factors.getColumnDimension(); ++j) { y[i] = y[i].add(variables[j].multiply(factors.getEntry(i, j))); } } DerivativeStructure sum = variables[0].getField().getZero(); for (int i = 0; i < y.length; ++i) { DerivativeStructure ri = y[i].subtract(target[i]); sum = sum.add(ri.multiply(ri)); } return sum; } } } ././@LongLink100644 0 0 167 12126630646 10265 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/optimization/general/LevenbergMarquardtOptimizerTest.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/optimization/general/LevenbergMarquardt100644 1750 1750 41603 12126627671 32465 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.optimization.general; import java.io.Serializable; import java.util.ArrayList; import java.util.List; import org.apache.commons.math3.analysis.differentiation.DerivativeStructure; import org.apache.commons.math3.analysis.differentiation.MultivariateDifferentiableVectorFunction; import org.apache.commons.math3.exception.ConvergenceException; import org.apache.commons.math3.exception.DimensionMismatchException; import org.apache.commons.math3.exception.TooManyEvaluationsException; import org.apache.commons.math3.geometry.euclidean.twod.Vector2D; import org.apache.commons.math3.linear.SingularMatrixException; import org.apache.commons.math3.optimization.PointVectorValuePair; import org.apache.commons.math3.util.FastMath; import org.apache.commons.math3.util.Precision; import org.junit.Assert; import org.junit.Test; import org.junit.Ignore; /** *

      Some of the unit tests are re-implementations of the MINPACK file17 and file22 test files. * The redistribution policy for MINPACK is available here, for * convenience, it is reproduced below.

      * * * *
      * Minpack Copyright Notice (1999) University of Chicago. * All rights reserved *
      * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: *
        *
      1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer.
      2. *
      3. Redistributions in binary form must reproduce the above * copyright notice, this list of conditions and the following * disclaimer in the documentation and/or other materials provided * with the distribution.
      4. *
      5. The end-user documentation included with the redistribution, if any, * must include the following acknowledgment: * This product includes software developed by the University of * Chicago, as Operator of Argonne National Laboratory. * Alternately, this acknowledgment may appear in the software itself, * if and wherever such third-party acknowledgments normally appear.
      6. *
      7. WARRANTY DISCLAIMER. THE SOFTWARE IS SUPPLIED "AS IS" * WITHOUT WARRANTY OF ANY KIND. THE COPYRIGHT HOLDER, THE * UNITED STATES, THE UNITED STATES DEPARTMENT OF ENERGY, AND * THEIR EMPLOYEES: (1) DISCLAIM ANY WARRANTIES, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO ANY IMPLIED WARRANTIES * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE * OR NON-INFRINGEMENT, (2) DO NOT ASSUME ANY LEGAL LIABILITY * OR RESPONSIBILITY FOR THE ACCURACY, COMPLETENESS, OR * USEFULNESS OF THE SOFTWARE, (3) DO NOT REPRESENT THAT USE OF * THE SOFTWARE WOULD NOT INFRINGE PRIVATELY OWNED RIGHTS, (4) * DO NOT WARRANT THAT THE SOFTWARE WILL FUNCTION * UNINTERRUPTED, THAT IT IS ERROR-FREE OR THAT ANY ERRORS WILL * BE CORRECTED.
      8. *
      9. LIMITATION OF LIABILITY. IN NO EVENT WILL THE COPYRIGHT * HOLDER, THE UNITED STATES, THE UNITED STATES DEPARTMENT OF * ENERGY, OR THEIR EMPLOYEES: BE LIABLE FOR ANY INDIRECT, * INCIDENTAL, CONSEQUENTIAL, SPECIAL OR PUNITIVE DAMAGES OF * ANY KIND OR NATURE, INCLUDING BUT NOT LIMITED TO LOSS OF * PROFITS OR LOSS OF DATA, FOR ANY REASON WHATSOEVER, WHETHER * SUCH LIABILITY IS ASSERTED ON THE BASIS OF CONTRACT, TORT * (INCLUDING NEGLIGENCE OR STRICT LIABILITY), OR OTHERWISE, * EVEN IF ANY OF SAID PARTIES HAS BEEN WARNED OF THE * POSSIBILITY OF SUCH LOSS OR DAMAGES.
      10. *
        * @author Argonne National Laboratory. MINPACK project. March 1980 (original fortran minpack tests) * @author Burton S. Garbow (original fortran minpack tests) * @author Kenneth E. Hillstrom (original fortran minpack tests) * @author Jorge J. More (original fortran minpack tests) * @author Luc Maisonobe (non-minpack tests and minpack tests Java translation) */ public class LevenbergMarquardtOptimizerTest extends AbstractLeastSquaresOptimizerAbstractTest { @Override public AbstractLeastSquaresOptimizer createOptimizer() { return new LevenbergMarquardtOptimizer(); } @Override @Test(expected=SingularMatrixException.class) public void testNonInvertible() { /* * Overrides the method from parent class, since the default singularity * threshold (1e-14) does not trigger the expected exception. */ LinearProblem problem = new LinearProblem(new double[][] { { 1, 2, -3 }, { 2, 1, 3 }, { -3, 0, -9 } }, new double[] { 1, 1, 1 }); AbstractLeastSquaresOptimizer optimizer = createOptimizer(); PointVectorValuePair optimum = optimizer.optimize(100, problem, problem.target, new double[] { 1, 1, 1 }, new double[] { 0, 0, 0 }); Assert.assertTrue(FastMath.sqrt(problem.target.length) * optimizer.getRMS() > 0.6); optimizer.computeCovariances(optimum.getPoint(), 1.5e-14); } @Test public void testControlParameters() { CircleVectorial circle = new CircleVectorial(); circle.addPoint( 30.0, 68.0); circle.addPoint( 50.0, -6.0); circle.addPoint(110.0, -20.0); circle.addPoint( 35.0, 15.0); circle.addPoint( 45.0, 97.0); checkEstimate(circle, 0.1, 10, 1.0e-14, 1.0e-16, 1.0e-10, false); checkEstimate(circle, 0.1, 10, 1.0e-15, 1.0e-17, 1.0e-10, true); checkEstimate(circle, 0.1, 5, 1.0e-15, 1.0e-16, 1.0e-10, true); circle.addPoint(300, -300); checkEstimate(circle, 0.1, 20, 1.0e-18, 1.0e-16, 1.0e-10, true); } private void checkEstimate(MultivariateDifferentiableVectorFunction problem, double initialStepBoundFactor, int maxCostEval, double costRelativeTolerance, double parRelativeTolerance, double orthoTolerance, boolean shouldFail) { try { LevenbergMarquardtOptimizer optimizer = new LevenbergMarquardtOptimizer(initialStepBoundFactor, costRelativeTolerance, parRelativeTolerance, orthoTolerance, Precision.SAFE_MIN); optimizer.optimize(maxCostEval, problem, new double[] { 0, 0, 0, 0, 0 }, new double[] { 1, 1, 1, 1, 1 }, new double[] { 98.680, 47.345 }); Assert.assertTrue(!shouldFail); } catch (DimensionMismatchException ee) { Assert.assertTrue(shouldFail); } catch (TooManyEvaluationsException ee) { Assert.assertTrue(shouldFail); } } // Test is skipped because it fails with the latest code update. @Ignore@Test public void testMath199() { try { QuadraticProblem problem = new QuadraticProblem(); problem.addPoint (0, -3.182591015485607); problem.addPoint (1, -2.5581184967730577); problem.addPoint (2, -2.1488478161387325); problem.addPoint (3, -1.9122489313410047); problem.addPoint (4, 1.7785661310051026); LevenbergMarquardtOptimizer optimizer = new LevenbergMarquardtOptimizer(100, 1e-10, 1e-10, 1e-10, 0); optimizer.optimize(100, problem, new double[] { 0, 0, 0, 0, 0 }, new double[] { 0.0, 4.4e-323, 1.0, 4.4e-323, 0.0 }, new double[] { 0, 0, 0 }); Assert.fail("an exception should have been thrown"); } catch (ConvergenceException ee) { // expected behavior } } /** * Non-linear test case: fitting of decay curve (from Chapter 8 of * Bevington's textbook, "Data reduction and analysis for the physical sciences"). * XXX The expected ("reference") values may not be accurate and the tolerance too * relaxed for this test to be currently really useful (the issue is under * investigation). */ @Test public void testBevington() { final double[][] dataPoints = { // column 1 = times { 15, 30, 45, 60, 75, 90, 105, 120, 135, 150, 165, 180, 195, 210, 225, 240, 255, 270, 285, 300, 315, 330, 345, 360, 375, 390, 405, 420, 435, 450, 465, 480, 495, 510, 525, 540, 555, 570, 585, 600, 615, 630, 645, 660, 675, 690, 705, 720, 735, 750, 765, 780, 795, 810, 825, 840, 855, 870, 885, }, // column 2 = measured counts { 775, 479, 380, 302, 185, 157, 137, 119, 110, 89, 74, 61, 66, 68, 48, 54, 51, 46, 55, 29, 28, 37, 49, 26, 35, 29, 31, 24, 25, 35, 24, 30, 26, 28, 21, 18, 20, 27, 17, 17, 14, 17, 24, 11, 22, 17, 12, 10, 13, 16, 9, 9, 14, 21, 17, 13, 12, 18, 10, }, }; final BevingtonProblem problem = new BevingtonProblem(); final int len = dataPoints[0].length; final double[] weights = new double[len]; for (int i = 0; i < len; i++) { problem.addPoint(dataPoints[0][i], dataPoints[1][i]); weights[i] = 1 / dataPoints[1][i]; } final LevenbergMarquardtOptimizer optimizer = new LevenbergMarquardtOptimizer(); final PointVectorValuePair optimum = optimizer.optimize(100, problem, dataPoints[1], weights, new double[] { 10, 900, 80, 27, 225 }); final double[] solution = optimum.getPoint(); final double[] expectedSolution = { 10.4, 958.3, 131.4, 33.9, 205.0 }; final double[][] covarMatrix = optimizer.computeCovariances(solution, 1e-14); final double[][] expectedCovarMatrix = { { 3.38, -3.69, 27.98, -2.34, -49.24 }, { -3.69, 2492.26, 81.89, -69.21, -8.9 }, { 27.98, 81.89, 468.99, -44.22, -615.44 }, { -2.34, -69.21, -44.22, 6.39, 53.80 }, { -49.24, -8.9, -615.44, 53.8, 929.45 } }; final int numParams = expectedSolution.length; // Check that the computed solution is within the reference error range. for (int i = 0; i < numParams; i++) { final double error = FastMath.sqrt(expectedCovarMatrix[i][i]); Assert.assertEquals("Parameter " + i, expectedSolution[i], solution[i], error); } // Check that each entry of the computed covariance matrix is within 10% // of the reference matrix entry. for (int i = 0; i < numParams; i++) { for (int j = 0; j < numParams; j++) { Assert.assertEquals("Covariance matrix [" + i + "][" + j + "]", expectedCovarMatrix[i][j], covarMatrix[i][j], FastMath.abs(0.1 * expectedCovarMatrix[i][j])); } } } @Test public void testCircleFitting2() { final double xCenter = 123.456; final double yCenter = 654.321; final double xSigma = 10; final double ySigma = 15; final double radius = 111.111; // The test is extremely sensitive to the seed. final long seed = 59421061L; final RandomCirclePointGenerator factory = new RandomCirclePointGenerator(xCenter, yCenter, radius, xSigma, ySigma, seed); final CircleProblem circle = new CircleProblem(xSigma, ySigma); final int numPoints = 10; for (Vector2D p : factory.generate(numPoints)) { circle.addPoint(p); // System.out.println(p.x + " " + p.y); } // First guess for the center's coordinates and radius. final double[] init = { 90, 659, 115 }; final LevenbergMarquardtOptimizer optimizer = new LevenbergMarquardtOptimizer(); final PointVectorValuePair optimum = optimizer.optimize(100, circle, circle.target(), circle.weight(), init); final double[] paramFound = optimum.getPoint(); // Retrieve errors estimation. final double[][] covMatrix = optimizer.computeCovariances(paramFound, 1e-14); final double[] asymptoticStandardErrorFound = optimizer.guessParametersErrors(); final double[] sigmaFound = new double[covMatrix.length]; for (int i = 0; i < covMatrix.length; i++) { sigmaFound[i] = FastMath.sqrt(covMatrix[i][i]); // System.out.println("i=" + i + " value=" + paramFound[i] // + " sigma=" + sigmaFound[i] // + " ase=" + asymptoticStandardErrorFound[i]); } // System.out.println("chi2=" + optimizer.getChiSquare()); // Check that the parameters are found within the assumed error bars. Assert.assertEquals(xCenter, paramFound[0], asymptoticStandardErrorFound[0]); Assert.assertEquals(yCenter, paramFound[1], asymptoticStandardErrorFound[1]); Assert.assertEquals(radius, paramFound[2], asymptoticStandardErrorFound[2]); } private static class QuadraticProblem implements MultivariateDifferentiableVectorFunction, Serializable { private static final long serialVersionUID = 7072187082052755854L; private List x; private List y; public QuadraticProblem() { x = new ArrayList(); y = new ArrayList(); } public void addPoint(double x, double y) { this.x.add(x); this.y.add(y); } public double[] value(double[] variables) { double[] values = new double[x.size()]; for (int i = 0; i < values.length; ++i) { values[i] = (variables[0] * x.get(i) + variables[1]) * x.get(i) + variables[2]; } return values; } public DerivativeStructure[] value(DerivativeStructure[] variables) { DerivativeStructure[] values = new DerivativeStructure[x.size()]; for (int i = 0; i < values.length; ++i) { values[i] = (variables[0].multiply(x.get(i)).add(variables[1])).multiply(x.get(i)).add(variables[2]); } return values; } } private static class BevingtonProblem implements MultivariateDifferentiableVectorFunction { private List time; private List count; public BevingtonProblem() { time = new ArrayList(); count = new ArrayList(); } public void addPoint(double t, double c) { time.add(t); count.add(c); } public double[] value(double[] params) { double[] values = new double[time.size()]; for (int i = 0; i < values.length; ++i) { final double t = time.get(i); values[i] = params[0] + params[1] * Math.exp(-t / params[3]) + params[2] * Math.exp(-t / params[4]); } return values; } public DerivativeStructure[] value(DerivativeStructure[] params) { DerivativeStructure[] values = new DerivativeStructure[time.size()]; for (int i = 0; i < values.length; ++i) { final double t = time.get(i); values[i] = params[0].add( params[1].multiply(params[3].reciprocal().multiply(-t).exp())).add( params[2].multiply(params[4].reciprocal().multiply(-t).exp())); } return values; } } } ././@LongLink100644 0 0 145 12126630646 10261 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/optimization/general/CircleProblem.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/optimization/general/CircleProblem.java100644 1750 1750 11357 12126627671 32340 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.optimization.general; import java.util.ArrayList; import org.apache.commons.math3.analysis.differentiation.DerivativeStructure; import org.apache.commons.math3.analysis.differentiation.MultivariateDifferentiableVectorFunction; import org.apache.commons.math3.geometry.euclidean.twod.Vector2D; import org.apache.commons.math3.util.FastMath; /** * Class that models a circle. * The parameters of problem are: *
          *
        • the x-coordinate of the circle center,
        • *
        • the y-coordinate of the circle center,
        • *
        • the radius of the circle.
        • *
        * The model functions are: *
          *
        • for each triplet (cx, cy, r), the (x, y) coordinates of a point on the * corresponding circle.
        • *
        */ class CircleProblem implements MultivariateDifferentiableVectorFunction { /** Cloud of points assumed to be fitted by a circle. */ private final ArrayList points; /** Error on the x-coordinate of the points. */ private final double xSigma; /** Error on the y-coordinate of the points. */ private final double ySigma; /** * @param xError Assumed error for the x-coordinate of the circle points. * @param yError Assumed error for the y-coordinate of the circle points. */ public CircleProblem(double xError, double yError) { points = new ArrayList(); xSigma = xError; ySigma = yError; } public void addPoint(Vector2D p) { points.add(p); } public double[] target() { final double[] t = new double[points.size() * 2]; for (int i = 0; i < points.size(); i++) { final Vector2D p = points.get(i); final int index = i * 2; t[index] = p.getX(); t[index + 1] = p.getY(); } return t; } public double[] weight() { final double wX = 1 / (xSigma * xSigma); final double wY = 1 / (ySigma * ySigma); final double[] w = new double[points.size() * 2]; for (int i = 0; i < points.size(); i++) { final int index = i * 2; w[index] = wX; w[index + 1] = wY; } return w; } public double[] value(double[] params) { final double cx = params[0]; final double cy = params[1]; final double r = params[2]; final double[] model = new double[points.size() * 2]; for (int i = 0; i < points.size(); i++) { final Vector2D p = points.get(i); // Find the circle point closest to the observed point // (observed points are points add through the addPoint method above) final double dX = cx - p.getX(); final double dY = cy - p.getY(); final double scaling = r / FastMath.hypot(dX, dY); final int index = i * 2; model[index] = cx - scaling * dX; model[index + 1] = cy - scaling * dY; } return model; } public DerivativeStructure[] value(DerivativeStructure[] params) { final DerivativeStructure cx = params[0]; final DerivativeStructure cy = params[1]; final DerivativeStructure r = params[2]; final DerivativeStructure[] model = new DerivativeStructure[points.size() * 2]; for (int i = 0; i < points.size(); i++) { final Vector2D p = points.get(i); // Find the circle point closest to the observed point // (observed points are points add through the addPoint method above) final DerivativeStructure dX = cx.subtract(p.getX()); final DerivativeStructure dY = cy.subtract(p.getY()); final DerivativeStructure scaling = r.divide(dX.multiply(dX).add(dY.multiply(dY)).sqrt()); final int index = i * 2; model[index] = cx.subtract(scaling.multiply(dX)); model[index + 1] = cy.subtract(scaling.multiply(dY)); } return model; } } ././@LongLink100644 0 0 160 12126630646 10256 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/optimization/general/GaussNewtonOptimizerTest.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/optimization/general/GaussNewtonOptimiz100644 1750 1750 14604 12126627671 32525 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.optimization.general; import java.io.IOException; import org.apache.commons.math3.exception.ConvergenceException; import org.apache.commons.math3.exception.TooManyEvaluationsException; import org.apache.commons.math3.optimization.SimpleVectorValueChecker; import org.junit.Test; /** *

        Some of the unit tests are re-implementations of the MINPACK file17 and file22 test files. * The redistribution policy for MINPACK is available here, for * convenience, it is reproduced below.

        * * * *
        * Minpack Copyright Notice (1999) University of Chicago. * All rights reserved *
        * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: *
          *
        1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer.
        2. *
        3. Redistributions in binary form must reproduce the above * copyright notice, this list of conditions and the following * disclaimer in the documentation and/or other materials provided * with the distribution.
        4. *
        5. The end-user documentation included with the redistribution, if any, * must include the following acknowledgment: * This product includes software developed by the University of * Chicago, as Operator of Argonne National Laboratory. * Alternately, this acknowledgment may appear in the software itself, * if and wherever such third-party acknowledgments normally appear.
        6. *
        7. WARRANTY DISCLAIMER. THE SOFTWARE IS SUPPLIED "AS IS" * WITHOUT WARRANTY OF ANY KIND. THE COPYRIGHT HOLDER, THE * UNITED STATES, THE UNITED STATES DEPARTMENT OF ENERGY, AND * THEIR EMPLOYEES: (1) DISCLAIM ANY WARRANTIES, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO ANY IMPLIED WARRANTIES * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE * OR NON-INFRINGEMENT, (2) DO NOT ASSUME ANY LEGAL LIABILITY * OR RESPONSIBILITY FOR THE ACCURACY, COMPLETENESS, OR * USEFULNESS OF THE SOFTWARE, (3) DO NOT REPRESENT THAT USE OF * THE SOFTWARE WOULD NOT INFRINGE PRIVATELY OWNED RIGHTS, (4) * DO NOT WARRANT THAT THE SOFTWARE WILL FUNCTION * UNINTERRUPTED, THAT IT IS ERROR-FREE OR THAT ANY ERRORS WILL * BE CORRECTED.
        8. *
        9. LIMITATION OF LIABILITY. IN NO EVENT WILL THE COPYRIGHT * HOLDER, THE UNITED STATES, THE UNITED STATES DEPARTMENT OF * ENERGY, OR THEIR EMPLOYEES: BE LIABLE FOR ANY INDIRECT, * INCIDENTAL, CONSEQUENTIAL, SPECIAL OR PUNITIVE DAMAGES OF * ANY KIND OR NATURE, INCLUDING BUT NOT LIMITED TO LOSS OF * PROFITS OR LOSS OF DATA, FOR ANY REASON WHATSOEVER, WHETHER * SUCH LIABILITY IS ASSERTED ON THE BASIS OF CONTRACT, TORT * (INCLUDING NEGLIGENCE OR STRICT LIABILITY), OR OTHERWISE, * EVEN IF ANY OF SAID PARTIES HAS BEEN WARNED OF THE * POSSIBILITY OF SUCH LOSS OR DAMAGES.
        10. *
          * @author Argonne National Laboratory. MINPACK project. March 1980 (original fortran minpack tests) * @author Burton S. Garbow (original fortran minpack tests) * @author Kenneth E. Hillstrom (original fortran minpack tests) * @author Jorge J. More (original fortran minpack tests) * @author Luc Maisonobe (non-minpack tests and minpack tests Java translation) */ public class GaussNewtonOptimizerTest extends AbstractLeastSquaresOptimizerAbstractTest { @Override public AbstractLeastSquaresOptimizer createOptimizer() { return new GaussNewtonOptimizer(new SimpleVectorValueChecker(1.0e-6, 1.0e-6)); } @Override @Test(expected = ConvergenceException.class) public void testMoreEstimatedParametersSimple() { /* * Exception is expected with this optimizer */ super.testMoreEstimatedParametersSimple(); } @Override @Test(expected=ConvergenceException.class) public void testMoreEstimatedParametersUnsorted() { /* * Exception is expected with this optimizer */ super.testMoreEstimatedParametersUnsorted(); } @Test(expected=TooManyEvaluationsException.class) public void testMaxEvaluations() throws Exception { CircleVectorial circle = new CircleVectorial(); circle.addPoint( 30.0, 68.0); circle.addPoint( 50.0, -6.0); circle.addPoint(110.0, -20.0); circle.addPoint( 35.0, 15.0); circle.addPoint( 45.0, 97.0); GaussNewtonOptimizer optimizer = new GaussNewtonOptimizer(new SimpleVectorValueChecker(1.0e-30, 1.0e-30)); optimizer.optimize(100, circle, new double[] { 0, 0, 0, 0, 0 }, new double[] { 1, 1, 1, 1, 1 }, new double[] { 98.680, 47.345 }); } @Override @Test(expected=ConvergenceException.class) public void testCircleFittingBadInit() { /* * This test does not converge with this optimizer. */ super.testCircleFittingBadInit(); } @Override @Test(expected = ConvergenceException.class) public void testHahn1() throws IOException { /* * TODO This test leads to a singular problem with the Gauss-Newton * optimizer. This should be inquired. */ super.testHahn1(); } } ././@LongLink100644 0 0 201 12126630646 10252 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/optimization/general/AbstractLeastSquaresOptimizerAbstractTest.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/optimization/general/AbstractLeastSquar100644 1750 1750 62112 12126627671 32441 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.optimization.general; import java.io.IOException; import java.io.Serializable; import java.util.Arrays; import org.apache.commons.math3.analysis.differentiation.DerivativeStructure; import org.apache.commons.math3.analysis.differentiation.MultivariateDifferentiableVectorFunction; import org.apache.commons.math3.exception.ConvergenceException; import org.apache.commons.math3.exception.DimensionMismatchException; import org.apache.commons.math3.exception.NumberIsTooSmallException; import org.apache.commons.math3.geometry.euclidean.twod.Vector2D; import org.apache.commons.math3.linear.BlockRealMatrix; import org.apache.commons.math3.linear.RealMatrix; import org.apache.commons.math3.optimization.PointVectorValuePair; import org.apache.commons.math3.util.FastMath; import org.junit.Assert; import org.junit.Test; /** *

          Some of the unit tests are re-implementations of the MINPACK file17 and file22 test files. * The redistribution policy for MINPACK is available here, for * convenience, it is reproduced below.

          * * * *
          * Minpack Copyright Notice (1999) University of Chicago. * All rights reserved *
          * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: *
            *
          1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer.
          2. *
          3. Redistributions in binary form must reproduce the above * copyright notice, this list of conditions and the following * disclaimer in the documentation and/or other materials provided * with the distribution.
          4. *
          5. The end-user documentation included with the redistribution, if any, * must include the following acknowledgment: * This product includes software developed by the University of * Chicago, as Operator of Argonne National Laboratory. * Alternately, this acknowledgment may appear in the software itself, * if and wherever such third-party acknowledgments normally appear.
          6. *
          7. WARRANTY DISCLAIMER. THE SOFTWARE IS SUPPLIED "AS IS" * WITHOUT WARRANTY OF ANY KIND. THE COPYRIGHT HOLDER, THE * UNITED STATES, THE UNITED STATES DEPARTMENT OF ENERGY, AND * THEIR EMPLOYEES: (1) DISCLAIM ANY WARRANTIES, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO ANY IMPLIED WARRANTIES * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE * OR NON-INFRINGEMENT, (2) DO NOT ASSUME ANY LEGAL LIABILITY * OR RESPONSIBILITY FOR THE ACCURACY, COMPLETENESS, OR * USEFULNESS OF THE SOFTWARE, (3) DO NOT REPRESENT THAT USE OF * THE SOFTWARE WOULD NOT INFRINGE PRIVATELY OWNED RIGHTS, (4) * DO NOT WARRANT THAT THE SOFTWARE WILL FUNCTION * UNINTERRUPTED, THAT IT IS ERROR-FREE OR THAT ANY ERRORS WILL * BE CORRECTED.
          8. *
          9. LIMITATION OF LIABILITY. IN NO EVENT WILL THE COPYRIGHT * HOLDER, THE UNITED STATES, THE UNITED STATES DEPARTMENT OF * ENERGY, OR THEIR EMPLOYEES: BE LIABLE FOR ANY INDIRECT, * INCIDENTAL, CONSEQUENTIAL, SPECIAL OR PUNITIVE DAMAGES OF * ANY KIND OR NATURE, INCLUDING BUT NOT LIMITED TO LOSS OF * PROFITS OR LOSS OF DATA, FOR ANY REASON WHATSOEVER, WHETHER * SUCH LIABILITY IS ASSERTED ON THE BASIS OF CONTRACT, TORT * (INCLUDING NEGLIGENCE OR STRICT LIABILITY), OR OTHERWISE, * EVEN IF ANY OF SAID PARTIES HAS BEEN WARNED OF THE * POSSIBILITY OF SUCH LOSS OR DAMAGES.
          10. *
            * @author Argonne National Laboratory. MINPACK project. March 1980 (original fortran minpack tests) * @author Burton S. Garbow (original fortran minpack tests) * @author Kenneth E. Hillstrom (original fortran minpack tests) * @author Jorge J. More (original fortran minpack tests) * @author Luc Maisonobe (non-minpack tests and minpack tests Java translation) * @version $Id: AbstractLeastSquaresOptimizerAbstractTest.java 1407467 2012-11-09 14:30:49Z erans $ */ public abstract class AbstractLeastSquaresOptimizerAbstractTest { public abstract AbstractLeastSquaresOptimizer createOptimizer(); @Test public void testTrivial() { LinearProblem problem = new LinearProblem(new double[][] { { 2 } }, new double[] { 3 }); AbstractLeastSquaresOptimizer optimizer = createOptimizer(); PointVectorValuePair optimum = optimizer.optimize(100, problem, problem.target, new double[] { 1 }, new double[] { 0 }); Assert.assertEquals(0, optimizer.getRMS(), 1.0e-10); Assert.assertEquals(1.5, optimum.getPoint()[0], 1.0e-10); Assert.assertEquals(3.0, optimum.getValue()[0], 1.0e-10); try { optimizer.guessParametersErrors(); Assert.fail("an exception should have been thrown"); } catch (NumberIsTooSmallException ee) { // expected behavior } } @Test public void testQRColumnsPermutation() { LinearProblem problem = new LinearProblem(new double[][] { { 1.0, -1.0 }, { 0.0, 2.0 }, { 1.0, -2.0 } }, new double[] { 4.0, 6.0, 1.0 }); AbstractLeastSquaresOptimizer optimizer = createOptimizer(); PointVectorValuePair optimum = optimizer.optimize(100, problem, problem.target, new double[] { 1, 1, 1 }, new double[] { 0, 0 }); Assert.assertEquals(0, optimizer.getRMS(), 1.0e-10); Assert.assertEquals(7.0, optimum.getPoint()[0], 1.0e-10); Assert.assertEquals(3.0, optimum.getPoint()[1], 1.0e-10); Assert.assertEquals(4.0, optimum.getValue()[0], 1.0e-10); Assert.assertEquals(6.0, optimum.getValue()[1], 1.0e-10); Assert.assertEquals(1.0, optimum.getValue()[2], 1.0e-10); } @Test public void testNoDependency() { LinearProblem problem = new LinearProblem(new double[][] { { 2, 0, 0, 0, 0, 0 }, { 0, 2, 0, 0, 0, 0 }, { 0, 0, 2, 0, 0, 0 }, { 0, 0, 0, 2, 0, 0 }, { 0, 0, 0, 0, 2, 0 }, { 0, 0, 0, 0, 0, 2 } }, new double[] { 0.0, 1.1, 2.2, 3.3, 4.4, 5.5 }); AbstractLeastSquaresOptimizer optimizer = createOptimizer(); PointVectorValuePair optimum = optimizer.optimize(100, problem, problem.target, new double[] { 1, 1, 1, 1, 1, 1 }, new double[] { 0, 0, 0, 0, 0, 0 }); Assert.assertEquals(0, optimizer.getRMS(), 1.0e-10); for (int i = 0; i < problem.target.length; ++i) { Assert.assertEquals(0.55 * i, optimum.getPoint()[i], 1.0e-10); } } @Test public void testOneSet() { LinearProblem problem = new LinearProblem(new double[][] { { 1, 0, 0 }, { -1, 1, 0 }, { 0, -1, 1 } }, new double[] { 1, 1, 1}); AbstractLeastSquaresOptimizer optimizer = createOptimizer(); PointVectorValuePair optimum = optimizer.optimize(100, problem, problem.target, new double[] { 1, 1, 1 }, new double[] { 0, 0, 0 }); Assert.assertEquals(0, optimizer.getRMS(), 1.0e-10); Assert.assertEquals(1.0, optimum.getPoint()[0], 1.0e-10); Assert.assertEquals(2.0, optimum.getPoint()[1], 1.0e-10); Assert.assertEquals(3.0, optimum.getPoint()[2], 1.0e-10); } @Test public void testTwoSets() { double epsilon = 1.0e-7; LinearProblem problem = new LinearProblem(new double[][] { { 2, 1, 0, 4, 0, 0 }, { -4, -2, 3, -7, 0, 0 }, { 4, 1, -2, 8, 0, 0 }, { 0, -3, -12, -1, 0, 0 }, { 0, 0, 0, 0, epsilon, 1 }, { 0, 0, 0, 0, 1, 1 } }, new double[] { 2, -9, 2, 2, 1 + epsilon * epsilon, 2}); AbstractLeastSquaresOptimizer optimizer = createOptimizer(); PointVectorValuePair optimum = optimizer.optimize(100, problem, problem.target, new double[] { 1, 1, 1, 1, 1, 1 }, new double[] { 0, 0, 0, 0, 0, 0 }); Assert.assertEquals(0, optimizer.getRMS(), 1.0e-10); Assert.assertEquals( 3.0, optimum.getPoint()[0], 1.0e-10); Assert.assertEquals( 4.0, optimum.getPoint()[1], 1.0e-10); Assert.assertEquals(-1.0, optimum.getPoint()[2], 1.0e-10); Assert.assertEquals(-2.0, optimum.getPoint()[3], 1.0e-10); Assert.assertEquals( 1.0 + epsilon, optimum.getPoint()[4], 1.0e-10); Assert.assertEquals( 1.0 - epsilon, optimum.getPoint()[5], 1.0e-10); } @Test(expected=ConvergenceException.class) public void testNonInvertible() throws Exception { LinearProblem problem = new LinearProblem(new double[][] { { 1, 2, -3 }, { 2, 1, 3 }, { -3, 0, -9 } }, new double[] { 1, 1, 1 }); AbstractLeastSquaresOptimizer optimizer = createOptimizer(); optimizer.optimize(100, problem, problem.target, new double[] { 1, 1, 1 }, new double[] { 0, 0, 0 }); } @Test public void testIllConditioned() { LinearProblem problem1 = new LinearProblem(new double[][] { { 10.0, 7.0, 8.0, 7.0 }, { 7.0, 5.0, 6.0, 5.0 }, { 8.0, 6.0, 10.0, 9.0 }, { 7.0, 5.0, 9.0, 10.0 } }, new double[] { 32, 23, 33, 31 }); AbstractLeastSquaresOptimizer optimizer = createOptimizer(); PointVectorValuePair optimum1 = optimizer.optimize(100, problem1, problem1.target, new double[] { 1, 1, 1, 1 }, new double[] { 0, 1, 2, 3 }); Assert.assertEquals(0, optimizer.getRMS(), 1.0e-10); Assert.assertEquals(1.0, optimum1.getPoint()[0], 1.0e-10); Assert.assertEquals(1.0, optimum1.getPoint()[1], 1.0e-10); Assert.assertEquals(1.0, optimum1.getPoint()[2], 1.0e-10); Assert.assertEquals(1.0, optimum1.getPoint()[3], 1.0e-10); LinearProblem problem2 = new LinearProblem(new double[][] { { 10.00, 7.00, 8.10, 7.20 }, { 7.08, 5.04, 6.00, 5.00 }, { 8.00, 5.98, 9.89, 9.00 }, { 6.99, 4.99, 9.00, 9.98 } }, new double[] { 32, 23, 33, 31 }); PointVectorValuePair optimum2 = optimizer.optimize(100, problem2, problem2.target, new double[] { 1, 1, 1, 1 }, new double[] { 0, 1, 2, 3 }); Assert.assertEquals(0, optimizer.getRMS(), 1.0e-10); Assert.assertEquals(-81.0, optimum2.getPoint()[0], 1.0e-8); Assert.assertEquals(137.0, optimum2.getPoint()[1], 1.0e-8); Assert.assertEquals(-34.0, optimum2.getPoint()[2], 1.0e-8); Assert.assertEquals( 22.0, optimum2.getPoint()[3], 1.0e-8); } @Test public void testMoreEstimatedParametersSimple() { LinearProblem problem = new LinearProblem(new double[][] { { 3.0, 2.0, 0.0, 0.0 }, { 0.0, 1.0, -1.0, 1.0 }, { 2.0, 0.0, 1.0, 0.0 } }, new double[] { 7.0, 3.0, 5.0 }); AbstractLeastSquaresOptimizer optimizer = createOptimizer(); optimizer.optimize(100, problem, problem.target, new double[] { 1, 1, 1 }, new double[] { 7, 6, 5, 4 }); Assert.assertEquals(0, optimizer.getRMS(), 1.0e-10); } @Test public void testMoreEstimatedParametersUnsorted() { LinearProblem problem = new LinearProblem(new double[][] { { 1.0, 1.0, 0.0, 0.0, 0.0, 0.0 }, { 0.0, 0.0, 1.0, 1.0, 1.0, 0.0 }, { 0.0, 0.0, 0.0, 0.0, 1.0, -1.0 }, { 0.0, 0.0, -1.0, 1.0, 0.0, 1.0 }, { 0.0, 0.0, 0.0, -1.0, 1.0, 0.0 } }, new double[] { 3.0, 12.0, -1.0, 7.0, 1.0 }); AbstractLeastSquaresOptimizer optimizer = createOptimizer(); PointVectorValuePair optimum = optimizer.optimize(100, problem, problem.target, new double[] { 1, 1, 1, 1, 1 }, new double[] { 2, 2, 2, 2, 2, 2 }); Assert.assertEquals(0, optimizer.getRMS(), 1.0e-10); Assert.assertEquals(3.0, optimum.getPointRef()[2], 1.0e-10); Assert.assertEquals(4.0, optimum.getPointRef()[3], 1.0e-10); Assert.assertEquals(5.0, optimum.getPointRef()[4], 1.0e-10); Assert.assertEquals(6.0, optimum.getPointRef()[5], 1.0e-10); } @Test public void testRedundantEquations() { LinearProblem problem = new LinearProblem(new double[][] { { 1.0, 1.0 }, { 1.0, -1.0 }, { 1.0, 3.0 } }, new double[] { 3.0, 1.0, 5.0 }); AbstractLeastSquaresOptimizer optimizer = createOptimizer(); PointVectorValuePair optimum = optimizer.optimize(100, problem, problem.target, new double[] { 1, 1, 1 }, new double[] { 1, 1 }); Assert.assertEquals(0, optimizer.getRMS(), 1.0e-10); Assert.assertEquals(2.0, optimum.getPointRef()[0], 1.0e-10); Assert.assertEquals(1.0, optimum.getPointRef()[1], 1.0e-10); } @Test public void testInconsistentEquations() { LinearProblem problem = new LinearProblem(new double[][] { { 1.0, 1.0 }, { 1.0, -1.0 }, { 1.0, 3.0 } }, new double[] { 3.0, 1.0, 4.0 }); AbstractLeastSquaresOptimizer optimizer = createOptimizer(); optimizer.optimize(100, problem, problem.target, new double[] { 1, 1, 1 }, new double[] { 1, 1 }); Assert.assertTrue(optimizer.getRMS() > 0.1); } @Test(expected=DimensionMismatchException.class) public void testInconsistentSizes1() { LinearProblem problem = new LinearProblem(new double[][] { { 1, 0 }, { 0, 1 } }, new double[] { -1, 1 }); AbstractLeastSquaresOptimizer optimizer = createOptimizer(); PointVectorValuePair optimum = optimizer.optimize(100, problem, problem.target, new double[] { 1, 1 }, new double[] { 0, 0 }); Assert.assertEquals(0, optimizer.getRMS(), 1.0e-10); Assert.assertEquals(-1, optimum.getPoint()[0], 1.0e-10); Assert.assertEquals(+1, optimum.getPoint()[1], 1.0e-10); optimizer.optimize(100, problem, problem.target, new double[] { 1 }, new double[] { 0, 0 }); } @Test(expected=DimensionMismatchException.class) public void testInconsistentSizes2() { LinearProblem problem = new LinearProblem(new double[][] { { 1, 0 }, { 0, 1 } }, new double[] { -1, 1 }); AbstractLeastSquaresOptimizer optimizer = createOptimizer(); PointVectorValuePair optimum = optimizer.optimize(100, problem, problem.target, new double[] { 1, 1 }, new double[] { 0, 0 }); Assert.assertEquals(0, optimizer.getRMS(), 1.0e-10); Assert.assertEquals(-1, optimum.getPoint()[0], 1.0e-10); Assert.assertEquals(+1, optimum.getPoint()[1], 1.0e-10); optimizer.optimize(100, problem, new double[] { 1 }, new double[] { 1 }, new double[] { 0, 0 }); } @Test public void testCircleFitting() { CircleVectorial circle = new CircleVectorial(); circle.addPoint( 30.0, 68.0); circle.addPoint( 50.0, -6.0); circle.addPoint(110.0, -20.0); circle.addPoint( 35.0, 15.0); circle.addPoint( 45.0, 97.0); AbstractLeastSquaresOptimizer optimizer = createOptimizer(); PointVectorValuePair optimum = optimizer.optimize(100, circle, new double[] { 0, 0, 0, 0, 0 }, new double[] { 1, 1, 1, 1, 1 }, new double[] { 98.680, 47.345 }); Assert.assertTrue(optimizer.getEvaluations() < 10); Assert.assertTrue(optimizer.getJacobianEvaluations() < 10); double rms = optimizer.getRMS(); Assert.assertEquals(1.768262623567235, FastMath.sqrt(circle.getN()) * rms, 1.0e-10); Vector2D center = new Vector2D(optimum.getPointRef()[0], optimum.getPointRef()[1]); Assert.assertEquals(69.96016176931406, circle.getRadius(center), 1.0e-6); Assert.assertEquals(96.07590211815305, center.getX(), 1.0e-6); Assert.assertEquals(48.13516790438953, center.getY(), 1.0e-6); double[][] cov = optimizer.computeCovariances(optimum.getPoint(), 1e-14); Assert.assertEquals(1.839, cov[0][0], 0.001); Assert.assertEquals(0.731, cov[0][1], 0.001); Assert.assertEquals(cov[0][1], cov[1][0], 1.0e-14); Assert.assertEquals(0.786, cov[1][1], 0.001); // add perfect measurements and check errors are reduced double r = circle.getRadius(center); for (double d= 0; d < 2 * FastMath.PI; d += 0.01) { circle.addPoint(center.getX() + r * FastMath.cos(d), center.getY() + r * FastMath.sin(d)); } double[] target = new double[circle.getN()]; Arrays.fill(target, 0.0); double[] weights = new double[circle.getN()]; Arrays.fill(weights, 2.0); optimum = optimizer.optimize(100, circle, target, weights, new double[] { 98.680, 47.345 }); cov = optimizer.computeCovariances(optimum.getPoint(), 1e-14); Assert.assertEquals(0.0016, cov[0][0], 0.001); Assert.assertEquals(3.2e-7, cov[0][1], 1.0e-9); Assert.assertEquals(cov[0][1], cov[1][0], 1.0e-14); Assert.assertEquals(0.0016, cov[1][1], 0.001); } @Test public void testCircleFittingBadInit() { CircleVectorial circle = new CircleVectorial(); double[][] points = circlePoints; double[] target = new double[points.length]; Arrays.fill(target, 0.0); double[] weights = new double[points.length]; Arrays.fill(weights, 2.0); for (int i = 0; i < points.length; ++i) { circle.addPoint(points[i][0], points[i][1]); } AbstractLeastSquaresOptimizer optimizer = createOptimizer(); PointVectorValuePair optimum = optimizer.optimize(100, circle, target, weights, new double[] { -12, -12 }); Vector2D center = new Vector2D(optimum.getPointRef()[0], optimum.getPointRef()[1]); Assert.assertTrue(optimizer.getEvaluations() < 25); Assert.assertTrue(optimizer.getJacobianEvaluations() < 20); Assert.assertEquals( 0.043, optimizer.getRMS(), 1.0e-3); Assert.assertEquals( 0.292235, circle.getRadius(center), 1.0e-6); Assert.assertEquals(-0.151738, center.getX(), 1.0e-6); Assert.assertEquals( 0.2075001, center.getY(), 1.0e-6); } @Test public void testCircleFittingGoodInit() { CircleVectorial circle = new CircleVectorial(); double[][] points = circlePoints; double[] target = new double[points.length]; Arrays.fill(target, 0.0); double[] weights = new double[points.length]; Arrays.fill(weights, 2.0); for (int i = 0; i < points.length; ++i) { circle.addPoint(points[i][0], points[i][1]); } AbstractLeastSquaresOptimizer optimizer = createOptimizer(); PointVectorValuePair optimum = optimizer.optimize(100, circle, target, weights, new double[] { 0, 0 }); Assert.assertEquals(-0.1517383071957963, optimum.getPointRef()[0], 1.0e-6); Assert.assertEquals(0.2074999736353867, optimum.getPointRef()[1], 1.0e-6); Assert.assertEquals(0.04268731682389561, optimizer.getRMS(), 1.0e-8); } private final double[][] circlePoints = new double[][] { {-0.312967, 0.072366}, {-0.339248, 0.132965}, {-0.379780, 0.202724}, {-0.390426, 0.260487}, {-0.361212, 0.328325}, {-0.346039, 0.392619}, {-0.280579, 0.444306}, {-0.216035, 0.470009}, {-0.149127, 0.493832}, {-0.075133, 0.483271}, {-0.007759, 0.452680}, { 0.060071, 0.410235}, { 0.103037, 0.341076}, { 0.118438, 0.273884}, { 0.131293, 0.192201}, { 0.115869, 0.129797}, { 0.072223, 0.058396}, { 0.022884, 0.000718}, {-0.053355, -0.020405}, {-0.123584, -0.032451}, {-0.216248, -0.032862}, {-0.278592, -0.005008}, {-0.337655, 0.056658}, {-0.385899, 0.112526}, {-0.405517, 0.186957}, {-0.415374, 0.262071}, {-0.387482, 0.343398}, {-0.347322, 0.397943}, {-0.287623, 0.458425}, {-0.223502, 0.475513}, {-0.135352, 0.478186}, {-0.061221, 0.483371}, { 0.003711, 0.422737}, { 0.065054, 0.375830}, { 0.108108, 0.297099}, { 0.123882, 0.222850}, { 0.117729, 0.134382}, { 0.085195, 0.056820}, { 0.029800, -0.019138}, {-0.027520, -0.072374}, {-0.102268, -0.091555}, {-0.200299, -0.106578}, {-0.292731, -0.091473}, {-0.356288, -0.051108}, {-0.420561, 0.014926}, {-0.471036, 0.074716}, {-0.488638, 0.182508}, {-0.485990, 0.254068}, {-0.463943, 0.338438}, {-0.406453, 0.404704}, {-0.334287, 0.466119}, {-0.254244, 0.503188}, {-0.161548, 0.495769}, {-0.075733, 0.495560}, { 0.001375, 0.434937}, { 0.082787, 0.385806}, { 0.115490, 0.323807}, { 0.141089, 0.223450}, { 0.138693, 0.131703}, { 0.126415, 0.049174}, { 0.066518, -0.010217}, {-0.005184, -0.070647}, {-0.080985, -0.103635}, {-0.177377, -0.116887}, {-0.260628, -0.100258}, {-0.335756, -0.056251}, {-0.405195, -0.000895}, {-0.444937, 0.085456}, {-0.484357, 0.175597}, {-0.472453, 0.248681}, {-0.438580, 0.347463}, {-0.402304, 0.422428}, {-0.326777, 0.479438}, {-0.247797, 0.505581}, {-0.152676, 0.519380}, {-0.071754, 0.516264}, { 0.015942, 0.472802}, { 0.076608, 0.419077}, { 0.127673, 0.330264}, { 0.159951, 0.262150}, { 0.153530, 0.172681}, { 0.140653, 0.089229}, { 0.078666, 0.024981}, { 0.023807, -0.037022}, {-0.048837, -0.077056}, {-0.127729, -0.075338}, {-0.221271, -0.067526} }; public void doTestStRD(final StatisticalReferenceDataset dataset, final double errParams, final double errParamsSd) { final AbstractLeastSquaresOptimizer optimizer = createOptimizer(); final double[] w = new double[dataset.getNumObservations()]; Arrays.fill(w, 1.0); final double[][] data = dataset.getData(); final double[] initial = dataset.getStartingPoint(0); final MultivariateDifferentiableVectorFunction problem; problem = dataset.getLeastSquaresProblem(); final PointVectorValuePair optimum; optimum = optimizer.optimize(100, problem, data[1], w, initial); final double[] actual = optimum.getPoint(); for (int i = 0; i < actual.length; i++) { double expected = dataset.getParameter(i); double delta = FastMath.abs(errParams * expected); Assert.assertEquals(dataset.getName() + ", param #" + i, expected, actual[i], delta); } } @Test public void testKirby2() throws IOException { doTestStRD(StatisticalReferenceDatasetFactory.createKirby2(), 1E-7, 1E-7); } @Test public void testHahn1() throws IOException { doTestStRD(StatisticalReferenceDatasetFactory.createHahn1(), 1E-7, 1E-4); } static class LinearProblem implements MultivariateDifferentiableVectorFunction, Serializable { private static final long serialVersionUID = 703247177355019415L; final RealMatrix factors; final double[] target; public LinearProblem(double[][] factors, double[] target) { this.factors = new BlockRealMatrix(factors); this.target = target; } public double[] value(double[] variables) { return factors.operate(variables); } public DerivativeStructure[] value(DerivativeStructure[] variables) { DerivativeStructure[] value = new DerivativeStructure[factors.getRowDimension()]; for (int i = 0; i < value.length; ++i) { value[i] = variables[0].getField().getZero(); for (int j = 0; j < factors.getColumnDimension(); ++j) { value[i] = value[i].add(variables[j].multiply(factors.getEntry(i, j))); } } return value; } } } ././@LongLink100644 0 0 147 12126630646 10263 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/optimization/general/CircleVectorial.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/optimization/general/CircleVectorial.ja100644 1750 1750 6137 12126627671 32321 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.optimization.general; import java.util.ArrayList; import org.apache.commons.math3.analysis.differentiation.DerivativeStructure; import org.apache.commons.math3.analysis.differentiation.MultivariateDifferentiableVectorFunction; import org.apache.commons.math3.geometry.euclidean.twod.Vector2D; /** * Class used in the tests. */ class CircleVectorial implements MultivariateDifferentiableVectorFunction { private ArrayList points; public CircleVectorial() { points = new ArrayList(); } public void addPoint(double px, double py) { points.add(new Vector2D(px, py)); } public int getN() { return points.size(); } public double getRadius(Vector2D center) { double r = 0; for (Vector2D point : points) { r += point.distance(center); } return r / points.size(); } private DerivativeStructure distance(Vector2D point, DerivativeStructure cx, DerivativeStructure cy) { DerivativeStructure dx = cx.subtract(point.getX()); DerivativeStructure dy = cy.subtract(point.getY()); return dx.multiply(dx).add(dy.multiply(dy)).sqrt(); } public DerivativeStructure getRadius(DerivativeStructure cx, DerivativeStructure cy) { DerivativeStructure r = cx.getField().getZero(); for (Vector2D point : points) { r = r.add(distance(point, cx, cy)); } return r.divide(points.size()); } public double[] value(double[] variables) { Vector2D center = new Vector2D(variables[0], variables[1]); double radius = getRadius(center); double[] residuals = new double[points.size()]; for (int i = 0; i < residuals.length; ++i) { residuals[i] = points.get(i).distance(center) - radius; } return residuals; } public DerivativeStructure[] value(DerivativeStructure[] variables) { DerivativeStructure radius = getRadius(variables[0], variables[1]); DerivativeStructure[] residuals = new DerivativeStructure[points.size()]; for (int i = 0; i < residuals.length; ++i) { residuals[i] = distance(points.get(i), variables[0], variables[1]).subtract(radius); } return residuals; } } ././@LongLink100644 0 0 172 12126630646 10261 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/optimization/general/StatisticalReferenceDatasetFactory.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/optimization/general/StatisticalReferen100644 1750 1750 12477 12126627671 32475 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.optimization.general; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import org.apache.commons.math3.analysis.differentiation.DerivativeStructure; /** * A factory to create instances of {@link StatisticalReferenceDataset} from * available resources. */ public class StatisticalReferenceDatasetFactory { private StatisticalReferenceDatasetFactory() { // Do nothing } /** * Creates a new buffered reader from the specified resource name. * * @param name the name of the resource * @return a buffered reader * @throws IOException if an I/O error occured */ public static BufferedReader createBufferedReaderFromResource(final String name) throws IOException { final InputStream resourceAsStream; resourceAsStream = StatisticalReferenceDatasetFactory.class .getResourceAsStream(name); if (resourceAsStream == null) { throw new IOException("could not find resource " + name); } return new BufferedReader(new InputStreamReader(resourceAsStream)); } public static StatisticalReferenceDataset createKirby2() throws IOException { final BufferedReader in = createBufferedReaderFromResource("Kirby2.dat"); StatisticalReferenceDataset dataset = null; try { dataset = new StatisticalReferenceDataset(in) { @Override public DerivativeStructure getModelValue(final double x, final DerivativeStructure[] a) { final DerivativeStructure p = a[0].add(a[1].add(a[2].multiply(x)).multiply(x)); final DerivativeStructure q = a[3].add(a[4].multiply(x)).multiply(x).add(1.0); return p.divide(q); } }; } finally { in.close(); } return dataset; } public static StatisticalReferenceDataset createHahn1() throws IOException { final BufferedReader in = createBufferedReaderFromResource("Hahn1.dat"); StatisticalReferenceDataset dataset = null; try { dataset = new StatisticalReferenceDataset(in) { @Override public DerivativeStructure getModelValue(final double x, final DerivativeStructure[] a) { final DerivativeStructure p = a[0].add(a[1].add(a[2].add(a[3].multiply(x)).multiply(x)).multiply(x)); final DerivativeStructure q = a[4].add(a[5].add(a[6].multiply(x)).multiply(x)).multiply(x).add(1.0); return p.divide(q); } }; } finally { in.close(); } return dataset; } public static StatisticalReferenceDataset createMGH17() throws IOException { final BufferedReader in = createBufferedReaderFromResource("MGH17.dat"); StatisticalReferenceDataset dataset = null; try { dataset = new StatisticalReferenceDataset(in) { @Override public DerivativeStructure getModelValue(final double x, final DerivativeStructure[] a) { return a[0].add(a[1].multiply(a[3].multiply(-x).exp())).add(a[2].multiply(a[4].multiply(-x).exp())); } }; } finally { in.close(); } return dataset; } public static StatisticalReferenceDataset createLanczos1() throws IOException { final BufferedReader in = createBufferedReaderFromResource("Lanczos1.dat"); StatisticalReferenceDataset dataset = null; try { dataset = new StatisticalReferenceDataset(in) { @Override public DerivativeStructure getModelValue(final double x, final DerivativeStructure[] a) { return a[0].multiply(a[3].multiply(-x).exp()).add( a[1].multiply(a[4].multiply(-x).exp())).add( a[2].multiply(a[5].multiply(-x).exp())); } }; } finally { in.close(); } return dataset; } /** * Returns an array with all available reference datasets. * * @return the array of datasets * @throws IOException if an I/O error occurs */ public StatisticalReferenceDataset[] createAll() throws IOException { return new StatisticalReferenceDataset[] { createKirby2(), createMGH17() }; } } ././@LongLink100644 0 0 153 12126630646 10260 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/optimization/general/StraightLineProblem.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/optimization/general/StraightLineProble100644 1750 1750 11405 12126627671 32431 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.optimization.general; import java.util.ArrayList; import org.apache.commons.math3.analysis.differentiation.DerivativeStructure; import org.apache.commons.math3.analysis.differentiation.MultivariateDifferentiableVectorFunction; import org.apache.commons.math3.analysis.differentiation.UnivariateDifferentiableFunction; import org.apache.commons.math3.stat.regression.SimpleRegression; /** * Class that models a straight line defined as {@code y = a x + b}. * The parameters of problem are: *
              *
            • {@code a}
            • *
            • {@code b}
            • *
            * The model functions are: *
              *
            • for each pair (a, b), the y-coordinate of the line.
            • *
            */ class StraightLineProblem implements MultivariateDifferentiableVectorFunction { /** Cloud of points assumed to be fitted by a straight line. */ private final ArrayList points; /** Error (on the y-coordinate of the points). */ private final double sigma; /** * @param error Assumed error for the y-coordinate. */ public StraightLineProblem(double error) { points = new ArrayList(); sigma = error; } public void addPoint(double px, double py) { points.add(new double[] { px, py }); } /** * @return the list of x-coordinates. */ public double[] x() { final double[] v = new double[points.size()]; for (int i = 0; i < points.size(); i++) { final double[] p = points.get(i); v[i] = p[0]; // x-coordinate. } return v; } /** * @return the list of y-coordinates. */ public double[] y() { final double[] v = new double[points.size()]; for (int i = 0; i < points.size(); i++) { final double[] p = points.get(i); v[i] = p[1]; // y-coordinate. } return v; } public double[] target() { return y(); } public double[] weight() { final double weight = 1 / (sigma * sigma); final double[] w = new double[points.size()]; for (int i = 0; i < points.size(); i++) { w[i] = weight; } return w; } public double[] value(double[] params) { final Model line = new Model(new DerivativeStructure(0, 0, params[0]), new DerivativeStructure(0, 0, params[1])); final double[] model = new double[points.size()]; for (int i = 0; i < points.size(); i++) { final double[] p = points.get(i); model[i] = line.value(p[0]); } return model; } public DerivativeStructure[] value(DerivativeStructure[] params) { final Model line = new Model(params[0], params[1]); final DerivativeStructure[] model = new DerivativeStructure[points.size()]; for (int i = 0; i < points.size(); i++) { final DerivativeStructure p0 = params[0].getField().getZero().add(points.get(i)[0]); model[i] = line.value(p0); } return model; } /** * Directly solve the linear problem, using the {@link SimpleRegression} * class. */ public double[] solve() { final SimpleRegression regress = new SimpleRegression(true); for (double[] d : points) { regress.addData(d[0], d[1]); } final double[] result = { regress.getSlope(), regress.getIntercept() }; return result; } /** * Linear function. */ public static class Model implements UnivariateDifferentiableFunction { final DerivativeStructure a; final DerivativeStructure b; public Model(DerivativeStructure a, DerivativeStructure b) { this.a = a; this.b = b; } public double value(double x) { return a.getValue() * x + b.getValue(); } public DerivativeStructure value(DerivativeStructure x) { return x.multiply(a).add(b); } } } ././@LongLink100644 0 0 163 12126630646 10261 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/optimization/general/StatisticalReferenceDataset.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/optimization/general/StatisticalReferen100644 1750 1750 30144 12126627671 32464 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.optimization.general; import java.io.BufferedReader; import java.io.IOException; import java.util.ArrayList; import org.apache.commons.math3.analysis.differentiation.DerivativeStructure; import org.apache.commons.math3.analysis.differentiation.MultivariateDifferentiableVectorFunction; import org.apache.commons.math3.util.MathArrays; /** * This class gives access to the statistical reference datasets provided by the * NIST (available * here). * Instances of this class can be created by invocation of the * {@link StatisticalReferenceDatasetFactory}. */ public abstract class StatisticalReferenceDataset { /** The name of this dataset. */ private final String name; /** The total number of observations (data points). */ private final int numObservations; /** The total number of parameters. */ private final int numParameters; /** The total number of starting points for the optimizations. */ private final int numStartingPoints; /** The values of the predictor. */ private final double[] x; /** The values of the response. */ private final double[] y; /** * The starting values. {@code startingValues[j][i]} is the value of the * {@code i}-th parameter in the {@code j}-th set of starting values. */ private final double[][] startingValues; /** The certified values of the parameters. */ private final double[] a; /** The certified values of the standard deviation of the parameters. */ private final double[] sigA; /** The certified value of the residual sum of squares. */ private double residualSumOfSquares; /** The least-squares problem. */ private final MultivariateDifferentiableVectorFunction problem; /** * Creates a new instance of this class from the specified data file. The * file must follow the StRD format. * * @param in the data file * @throws IOException if an I/O error occurs */ public StatisticalReferenceDataset(final BufferedReader in) throws IOException { final ArrayList lines = new ArrayList(); for (String line = in.readLine(); line != null; line = in.readLine()) { lines.add(line); } int[] index = findLineNumbers("Data", lines); if (index == null) { throw new AssertionError("could not find line indices for data"); } this.numObservations = index[1] - index[0] + 1; this.x = new double[this.numObservations]; this.y = new double[this.numObservations]; for (int i = 0; i < this.numObservations; i++) { final String line = lines.get(index[0] + i - 1); final String[] tokens = line.trim().split(" ++"); // Data columns are in reverse order!!! this.y[i] = Double.parseDouble(tokens[0]); this.x[i] = Double.parseDouble(tokens[1]); } index = findLineNumbers("Starting Values", lines); if (index == null) { throw new AssertionError( "could not find line indices for starting values"); } this.numParameters = index[1] - index[0] + 1; double[][] start = null; this.a = new double[numParameters]; this.sigA = new double[numParameters]; for (int i = 0; i < numParameters; i++) { final String line = lines.get(index[0] + i - 1); final String[] tokens = line.trim().split(" ++"); if (start == null) { start = new double[tokens.length - 4][numParameters]; } for (int j = 2; j < tokens.length - 2; j++) { start[j - 2][i] = Double.parseDouble(tokens[j]); } this.a[i] = Double.parseDouble(tokens[tokens.length - 2]); this.sigA[i] = Double.parseDouble(tokens[tokens.length - 1]); } if (start == null) { throw new IOException("could not find starting values"); } this.numStartingPoints = start.length; this.startingValues = start; double dummyDouble = Double.NaN; String dummyString = null; for (String line : lines) { if (line.contains("Dataset Name:")) { dummyString = line .substring(line.indexOf("Dataset Name:") + 13, line.indexOf("(")).trim(); } if (line.contains("Residual Sum of Squares")) { final String[] tokens = line.split(" ++"); dummyDouble = Double.parseDouble(tokens[4].trim()); } } if (Double.isNaN(dummyDouble)) { throw new IOException( "could not find certified value of residual sum of squares"); } this.residualSumOfSquares = dummyDouble; if (dummyString == null) { throw new IOException("could not find dataset name"); } this.name = dummyString; this.problem = new MultivariateDifferentiableVectorFunction() { public double[] value(final double[] a) { DerivativeStructure[] dsA = new DerivativeStructure[a.length]; for (int i = 0; i < a.length; ++i) { dsA[i] = new DerivativeStructure(a.length, 0, a[i]); } final int n = getNumObservations(); final double[] yhat = new double[n]; for (int i = 0; i < n; i++) { yhat[i] = getModelValue(getX(i), dsA).getValue(); } return yhat; } public DerivativeStructure[] value(final DerivativeStructure[] a) { final int n = getNumObservations(); final DerivativeStructure[] yhat = new DerivativeStructure[n]; for (int i = 0; i < n; i++) { yhat[i] = getModelValue(getX(i), a); } return yhat; } }; } /** * Returns the name of this dataset. * * @return the name of the dataset */ public String getName() { return name; } /** * Returns the total number of observations (data points). * * @return the number of observations */ public int getNumObservations() { return numObservations; } /** * Returns a copy of the data arrays. The data is laid out as follows
          11. * {@code data[0][i] = x[i]},
          12. {@code data[1][i] = y[i]},
          13. * * @return the array of data points. */ public double[][] getData() { return new double[][] { MathArrays.copyOf(x), MathArrays.copyOf(y) }; } /** * Returns the x-value of the {@code i}-th data point. * * @param i the index of the data point * @return the x-value */ public double getX(final int i) { return x[i]; } /** * Returns the y-value of the {@code i}-th data point. * * @param i the index of the data point * @return the y-value */ public double getY(final int i) { return y[i]; } /** * Returns the total number of parameters. * * @return the number of parameters */ public int getNumParameters() { return numParameters; } /** * Returns the certified values of the paramters. * * @return the values of the parameters */ public double[] getParameters() { return MathArrays.copyOf(a); } /** * Returns the certified value of the {@code i}-th parameter. * * @param i the index of the parameter * @return the value of the parameter */ public double getParameter(final int i) { return a[i]; } /** * Reurns the certified values of the standard deviations of the parameters. * * @return the standard deviations of the parameters */ public double[] getParametersStandardDeviations() { return MathArrays.copyOf(sigA); } /** * Returns the certified value of the standard deviation of the {@code i}-th * parameter. * * @param i the index of the parameter * @return the standard deviation of the parameter */ public double getParameterStandardDeviation(final int i) { return sigA[i]; } /** * Returns the certified value of the residual sum of squares. * * @return the residual sum of squares */ public double getResidualSumOfSquares() { return residualSumOfSquares; } /** * Returns the total number of starting points (initial guesses for the * optimization process). * * @return the number of starting points */ public int getNumStartingPoints() { return numStartingPoints; } /** * Returns the {@code i}-th set of initial values of the parameters. * * @param i the index of the starting point * @return the starting point */ public double[] getStartingPoint(final int i) { return MathArrays.copyOf(startingValues[i]); } /** * Returns the least-squares problem corresponding to fitting the model to * the specified data. * * @return the least-squares problem */ public MultivariateDifferentiableVectorFunction getLeastSquaresProblem() { return problem; } /** * Returns the value of the model for the specified values of the predictor * variable and the parameters. * * @param x the predictor variable * @param a the parameters * @return the value of the model */ public abstract DerivativeStructure getModelValue(final double x, final DerivativeStructure[] a); /** *

            * Parses the specified text lines, and extracts the indices of the first * and last lines of the data defined by the specified {@code key}. This key * must be one of *

            *
              *
            • {@code "Starting Values"},
            • *
            • {@code "Certified Values"},
            • *
            • {@code "Data"}.
            • *
            *

            * In the NIST data files, the line indices are separated by the keywords * {@code "lines"} and {@code "to"}. *

            * * @param lines the line of text to be parsed * @return an array of two {@code int}s. First value is the index of the * first line, second value is the index of the last line. * {@code null} if the line could not be parsed. */ private static int[] findLineNumbers(final String key, final Iterable lines) { for (String text : lines) { boolean flag = text.contains(key) && text.contains("lines") && text.contains("to") && text.contains(")"); if (flag) { final int[] numbers = new int[2]; final String from = text.substring(text.indexOf("lines") + 5, text.indexOf("to")); numbers[0] = Integer.parseInt(from.trim()); final String to = text.substring(text.indexOf("to") + 2, text.indexOf(")")); numbers[1] = Integer.parseInt(to.trim()); return numbers; } } return null; } } commons-math3-3.2-src/src/test/java/org/apache/commons/math3/optimization/general/MinpackTest.java100644 1750 1750 150650 12126627671 32060 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.optimization.general; import java.io.Serializable; import java.util.Arrays; import org.apache.commons.math3.exception.TooManyEvaluationsException; import org.apache.commons.math3.analysis.differentiation.DerivativeStructure; import org.apache.commons.math3.analysis.differentiation.MultivariateDifferentiableVectorFunction; import org.apache.commons.math3.optimization.PointVectorValuePair; import org.apache.commons.math3.util.FastMath; import org.junit.Assert; import org.junit.Test; /** *

            Some of the unit tests are re-implementations of the MINPACK file17 and file22 test files. * The redistribution policy for MINPACK is available here, for * convenience, it is reproduced below.

            * * * *
            * Minpack Copyright Notice (1999) University of Chicago. * All rights reserved *
            * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: *
              *
            1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer.
            2. *
            3. Redistributions in binary form must reproduce the above * copyright notice, this list of conditions and the following * disclaimer in the documentation and/or other materials provided * with the distribution.
            4. *
            5. The end-user documentation included with the redistribution, if any, * must include the following acknowledgment: * This product includes software developed by the University of * Chicago, as Operator of Argonne National Laboratory. * Alternately, this acknowledgment may appear in the software itself, * if and wherever such third-party acknowledgments normally appear.
            6. *
            7. WARRANTY DISCLAIMER. THE SOFTWARE IS SUPPLIED "AS IS" * WITHOUT WARRANTY OF ANY KIND. THE COPYRIGHT HOLDER, THE * UNITED STATES, THE UNITED STATES DEPARTMENT OF ENERGY, AND * THEIR EMPLOYEES: (1) DISCLAIM ANY WARRANTIES, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO ANY IMPLIED WARRANTIES * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE * OR NON-INFRINGEMENT, (2) DO NOT ASSUME ANY LEGAL LIABILITY * OR RESPONSIBILITY FOR THE ACCURACY, COMPLETENESS, OR * USEFULNESS OF THE SOFTWARE, (3) DO NOT REPRESENT THAT USE OF * THE SOFTWARE WOULD NOT INFRINGE PRIVATELY OWNED RIGHTS, (4) * DO NOT WARRANT THAT THE SOFTWARE WILL FUNCTION * UNINTERRUPTED, THAT IT IS ERROR-FREE OR THAT ANY ERRORS WILL * BE CORRECTED.
            8. *
            9. LIMITATION OF LIABILITY. IN NO EVENT WILL THE COPYRIGHT * HOLDER, THE UNITED STATES, THE UNITED STATES DEPARTMENT OF * ENERGY, OR THEIR EMPLOYEES: BE LIABLE FOR ANY INDIRECT, * INCIDENTAL, CONSEQUENTIAL, SPECIAL OR PUNITIVE DAMAGES OF * ANY KIND OR NATURE, INCLUDING BUT NOT LIMITED TO LOSS OF * PROFITS OR LOSS OF DATA, FOR ANY REASON WHATSOEVER, WHETHER * SUCH LIABILITY IS ASSERTED ON THE BASIS OF CONTRACT, TORT * (INCLUDING NEGLIGENCE OR STRICT LIABILITY), OR OTHERWISE, * EVEN IF ANY OF SAID PARTIES HAS BEEN WARNED OF THE * POSSIBILITY OF SUCH LOSS OR DAMAGES.
            10. *
              * @author Argonne National Laboratory. MINPACK project. March 1980 (original fortran minpack tests) * @author Burton S. Garbow (original fortran minpack tests) * @author Kenneth E. Hillstrom (original fortran minpack tests) * @author Jorge J. More (original fortran minpack tests) * @author Luc Maisonobe (non-minpack tests and minpack tests Java translation) */ public class MinpackTest { @Test public void testMinpackLinearFullRank() { minpackTest(new LinearFullRankFunction(10, 5, 1.0, 5.0, 2.23606797749979), false); minpackTest(new LinearFullRankFunction(50, 5, 1.0, 8.06225774829855, 6.70820393249937), false); } @Test public void testMinpackLinearRank1() { minpackTest(new LinearRank1Function(10, 5, 1.0, 291.521868819476, 1.4638501094228), false); minpackTest(new LinearRank1Function(50, 5, 1.0, 3101.60039334535, 3.48263016573496), false); } @Test public void testMinpackLinearRank1ZeroColsAndRows() { minpackTest(new LinearRank1ZeroColsAndRowsFunction(10, 5, 1.0), false); minpackTest(new LinearRank1ZeroColsAndRowsFunction(50, 5, 1.0), false); } @Test public void testMinpackRosenbrok() { minpackTest(new RosenbrockFunction(new double[] { -1.2, 1.0 }, FastMath.sqrt(24.2)), false); minpackTest(new RosenbrockFunction(new double[] { -12.0, 10.0 }, FastMath.sqrt(1795769.0)), false); minpackTest(new RosenbrockFunction(new double[] { -120.0, 100.0 }, 11.0 * FastMath.sqrt(169000121.0)), false); } @Test public void testMinpackHelicalValley() { minpackTest(new HelicalValleyFunction(new double[] { -1.0, 0.0, 0.0 }, 50.0), false); minpackTest(new HelicalValleyFunction(new double[] { -10.0, 0.0, 0.0 }, 102.95630140987), false); minpackTest(new HelicalValleyFunction(new double[] { -100.0, 0.0, 0.0}, 991.261822123701), false); } @Test public void testMinpackPowellSingular() { minpackTest(new PowellSingularFunction(new double[] { 3.0, -1.0, 0.0, 1.0 }, 14.6628782986152), false); minpackTest(new PowellSingularFunction(new double[] { 30.0, -10.0, 0.0, 10.0 }, 1270.9838708654), false); minpackTest(new PowellSingularFunction(new double[] { 300.0, -100.0, 0.0, 100.0 }, 126887.903284750), false); } @Test public void testMinpackFreudensteinRoth() { minpackTest(new FreudensteinRothFunction(new double[] { 0.5, -2.0 }, 20.0124960961895, 6.99887517584575, new double[] { 11.4124844654993, -0.896827913731509 }), false); minpackTest(new FreudensteinRothFunction(new double[] { 5.0, -20.0 }, 12432.833948863, 6.9988751744895, new double[] { 11.41300466147456, -0.896796038685959 }), false); minpackTest(new FreudensteinRothFunction(new double[] { 50.0, -200.0 }, 11426454.595762, 6.99887517242903, new double[] { 11.412781785788564, -0.8968051074920405 }), false); } @Test public void testMinpackBard() { minpackTest(new BardFunction(1.0, 6.45613629515967, 0.0906359603390466, new double[] { 0.0824105765758334, 1.1330366534715, 2.34369463894115 }), false); minpackTest(new BardFunction(10.0, 36.1418531596785, 4.17476870138539, new double[] { 0.840666673818329, -158848033.259565, -164378671.653535 }), false); minpackTest(new BardFunction(100.0, 384.114678637399, 4.17476870135969, new double[] { 0.840666673867645, -158946167.205518, -164464906.857771 }), false); } @Test public void testMinpackKowalikOsborne() { minpackTest(new KowalikOsborneFunction(new double[] { 0.25, 0.39, 0.415, 0.39 }, 0.0728915102882945, 0.017535837721129, new double[] { 0.192807810476249, 0.191262653354071, 0.123052801046931, 0.136053221150517 }), false); minpackTest(new KowalikOsborneFunction(new double[] { 2.5, 3.9, 4.15, 3.9 }, 2.97937007555202, 0.032052192917937, new double[] { 728675.473768287, -14.0758803129393, -32977797.7841797, -20571594.1977912 }), false); minpackTest(new KowalikOsborneFunction(new double[] { 25.0, 39.0, 41.5, 39.0 }, 29.9590617016037, 0.0175364017658228, new double[] { 0.192948328597594, 0.188053165007911, 0.122430604321144, 0.134575665392506 }), false); } @Test public void testMinpackMeyer() { minpackTest(new MeyerFunction(new double[] { 0.02, 4000.0, 250.0 }, 41153.4665543031, 9.37794514651874, new double[] { 0.00560963647102661, 6181.34634628659, 345.223634624144 }), false); minpackTest(new MeyerFunction(new double[] { 0.2, 40000.0, 2500.0 }, 4168216.89130846, 792.917871779501, new double[] { 1.42367074157994e-11, 33695.7133432541, 901.268527953801 }), true); } @Test public void testMinpackWatson() { minpackTest(new WatsonFunction(6, 0.0, 5.47722557505166, 0.0478295939097601, new double[] { -0.0157249615083782, 1.01243488232965, -0.232991722387673, 1.26043101102818, -1.51373031394421, 0.99299727291842 }), false); minpackTest(new WatsonFunction(6, 10.0, 6433.12578950026, 0.0478295939096951, new double[] { -0.0157251901386677, 1.01243485860105, -0.232991545843829, 1.26042932089163, -1.51372776706575, 0.99299573426328 }), false); minpackTest(new WatsonFunction(6, 100.0, 674256.040605213, 0.047829593911544, new double[] { -0.0157247019712586, 1.01243490925658, -0.232991922761641, 1.26043292929555, -1.51373320452707, 0.99299901922322 }), false); minpackTest(new WatsonFunction(9, 0.0, 5.47722557505166, 0.00118311459212420, new double[] { -0.153070644166722e-4, 0.999789703934597, 0.0147639634910978, 0.146342330145992, 1.00082109454817, -2.61773112070507, 4.10440313943354, -3.14361226236241, 1.05262640378759 }), false); minpackTest(new WatsonFunction(9, 10.0, 12088.127069307, 0.00118311459212513, new double[] { -0.153071334849279e-4, 0.999789703941234, 0.0147639629786217, 0.146342334818836, 1.00082107321386, -2.61773107084722, 4.10440307655564, -3.14361222178686, 1.05262639322589 }), false); minpackTest(new WatsonFunction(9, 100.0, 1269109.29043834, 0.00118311459212384, new double[] { -0.153069523352176e-4, 0.999789703958371, 0.0147639625185392, 0.146342341096326, 1.00082104729164, -2.61773101573645, 4.10440301427286, -3.14361218602503, 1.05262638516774 }), false); minpackTest(new WatsonFunction(12, 0.0, 5.47722557505166, 0.217310402535861e-4, new double[] { -0.660266001396382e-8, 1.00000164411833, -0.000563932146980154, 0.347820540050756, -0.156731500244233, 1.05281515825593, -3.24727109519451, 7.2884347837505, -10.271848098614, 9.07411353715783, -4.54137541918194, 1.01201187975044 }), false); minpackTest(new WatsonFunction(12, 10.0, 19220.7589790951, 0.217310402518509e-4, new double[] { -0.663710223017410e-8, 1.00000164411787, -0.000563932208347327, 0.347820540486998, -0.156731503955652, 1.05281517654573, -3.2472711515214, 7.28843489430665, -10.2718482369638, 9.07411364383733, -4.54137546533666, 1.01201188830857 }), false); minpackTest(new WatsonFunction(12, 100.0, 2018918.04462367, 0.217310402539845e-4, new double[] { -0.663806046485249e-8, 1.00000164411786, -0.000563932210324959, 0.347820540503588, -0.156731504091375, 1.05281517718031, -3.24727115337025, 7.28843489775302, -10.2718482410813, 9.07411364688464, -4.54137546660822, 1.0120118885369 }), false); } @Test public void testMinpackBox3Dimensional() { minpackTest(new Box3DimensionalFunction(10, new double[] { 0.0, 10.0, 20.0 }, 32.1115837449572), false); } @Test public void testMinpackJennrichSampson() { minpackTest(new JennrichSampsonFunction(10, new double[] { 0.3, 0.4 }, 64.5856498144943, 11.1517793413499, new double[] { // 0.2578330049, 0.257829976764542 0.2578199266368004, 0.25782997676455244 }), false); } @Test public void testMinpackBrownDennis() { minpackTest(new BrownDennisFunction(20, new double[] { 25.0, 5.0, -5.0, -1.0 }, 2815.43839161816, 292.954288244866, new double[] { -11.59125141003, 13.2024883984741, -0.403574643314272, 0.236736269844604 }), false); minpackTest(new BrownDennisFunction(20, new double[] { 250.0, 50.0, -50.0, -10.0 }, 555073.354173069, 292.954270581415, new double[] { -11.5959274272203, 13.2041866926242, -0.403417362841545, 0.236771143410386 }), false); minpackTest(new BrownDennisFunction(20, new double[] { 2500.0, 500.0, -500.0, -100.0 }, 61211252.2338581, 292.954306151134, new double[] { -11.5902596937374, 13.2020628854665, -0.403688070279258, 0.236665033746463 }), false); } @Test public void testMinpackChebyquad() { minpackTest(new ChebyquadFunction(1, 8, 1.0, 1.88623796907732, 1.88623796907732, new double[] { 0.5 }), false); minpackTest(new ChebyquadFunction(1, 8, 10.0, 5383344372.34005, 1.88424820499951, new double[] { 0.9817314924684 }), false); minpackTest(new ChebyquadFunction(1, 8, 100.0, 0.118088726698392e19, 1.88424820499347, new double[] { 0.9817314852934 }), false); minpackTest(new ChebyquadFunction(8, 8, 1.0, 0.196513862833975, 0.0593032355046727, new double[] { 0.0431536648587336, 0.193091637843267, 0.266328593812698, 0.499999334628884, 0.500000665371116, 0.733671406187302, 0.806908362156733, 0.956846335141266 }), false); minpackTest(new ChebyquadFunction(9, 9, 1.0, 0.16994993465202, 0.0, new double[] { 0.0442053461357828, 0.199490672309881, 0.23561910847106, 0.416046907892598, 0.5, 0.583953092107402, 0.764380891528940, 0.800509327690119, 0.955794653864217 }), false); minpackTest(new ChebyquadFunction(10, 10, 1.0, 0.183747831178711, 0.0806471004038253, new double[] { 0.0596202671753563, 0.166708783805937, 0.239171018813509, 0.398885290346268, 0.398883667870681, 0.601116332129320, 0.60111470965373, 0.760828981186491, 0.833291216194063, 0.940379732824644 }), false); } @Test public void testMinpackBrownAlmostLinear() { minpackTest(new BrownAlmostLinearFunction(10, 0.5, 16.5302162063499, 0.0, new double[] { 0.979430303349862, 0.979430303349862, 0.979430303349862, 0.979430303349862, 0.979430303349862, 0.979430303349862, 0.979430303349862, 0.979430303349862, 0.979430303349862, 1.20569696650138 }), false); minpackTest(new BrownAlmostLinearFunction(10, 5.0, 9765624.00089211, 0.0, new double[] { 0.979430303349865, 0.979430303349865, 0.979430303349865, 0.979430303349865, 0.979430303349865, 0.979430303349865, 0.979430303349865, 0.979430303349865, 0.979430303349865, 1.20569696650135 }), false); minpackTest(new BrownAlmostLinearFunction(10, 50.0, 0.9765625e17, 0.0, new double[] { 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0 }), false); minpackTest(new BrownAlmostLinearFunction(30, 0.5, 83.476044467848, 0.0, new double[] { 0.997754216442807, 0.997754216442807, 0.997754216442807, 0.997754216442807, 0.997754216442807, 0.997754216442807, 0.997754216442807, 0.997754216442807, 0.997754216442807, 0.997754216442807, 0.997754216442807, 0.997754216442807, 0.997754216442807, 0.997754216442807, 0.997754216442807, 0.997754216442807, 0.997754216442807, 0.997754216442807, 0.997754216442807, 0.997754216442807, 0.997754216442807, 0.997754216442807, 0.997754216442807, 0.997754216442807, 0.997754216442807, 0.997754216442807, 0.997754216442807, 0.997754216442807, 0.997754216442807, 1.06737350671578 }), false); minpackTest(new BrownAlmostLinearFunction(40, 0.5, 128.026364472323, 0.0, new double[] { 1.00000000000002, 1.00000000000002, 1.00000000000002, 1.00000000000002, 1.00000000000002, 1.00000000000002, 1.00000000000002, 1.00000000000002, 1.00000000000002, 1.00000000000002, 1.00000000000002, 1.00000000000002, 1.00000000000002, 1.00000000000002, 1.00000000000002, 1.00000000000002, 1.00000000000002, 1.00000000000002, 1.00000000000002, 1.00000000000002, 1.00000000000002, 1.00000000000002, 1.00000000000002, 1.00000000000002, 1.00000000000002, 1.00000000000002, 1.00000000000002, 1.00000000000002, 1.00000000000002, 1.00000000000002, 1.00000000000002, 1.00000000000002, 1.00000000000002, 1.00000000000002, 0.999999999999121 }), false); } @Test public void testMinpackOsborne1() { minpackTest(new Osborne1Function(new double[] { 0.5, 1.5, -1.0, 0.01, 0.02, }, 0.937564021037838, 0.00739249260904843, new double[] { 0.375410049244025, 1.93584654543108, -1.46468676748716, 0.0128675339110439, 0.0221227011813076 }), false); } @Test public void testMinpackOsborne2() { minpackTest(new Osborne2Function(new double[] { 1.3, 0.65, 0.65, 0.7, 0.6, 3.0, 5.0, 7.0, 2.0, 4.5, 5.5 }, 1.44686540984712, 0.20034404483314, new double[] { 1.30997663810096, 0.43155248076, 0.633661261602859, 0.599428560991695, 0.754179768272449, 0.904300082378518, 1.36579949521007, 4.82373199748107, 2.39868475104871, 4.56887554791452, 5.67534206273052 }), false); } private void minpackTest(MinpackFunction function, boolean exceptionExpected) { LevenbergMarquardtOptimizer optimizer = new LevenbergMarquardtOptimizer(FastMath.sqrt(2.22044604926e-16), FastMath.sqrt(2.22044604926e-16), 2.22044604926e-16); // Assert.assertTrue(function.checkTheoreticalStartCost(optimizer.getRMS())); try { PointVectorValuePair optimum = optimizer.optimize(400 * (function.getN() + 1), function, function.getTarget(), function.getWeight(), function.getStartPoint()); Assert.assertFalse(exceptionExpected); function.checkTheoreticalMinCost(optimizer.getRMS()); function.checkTheoreticalMinParams(optimum); } catch (TooManyEvaluationsException e) { Assert.assertTrue(exceptionExpected); } } private static abstract class MinpackFunction implements MultivariateDifferentiableVectorFunction, Serializable { private static final long serialVersionUID = -6209760235478794233L; protected int n; protected int m; protected double[] startParams; protected double theoreticalMinCost; protected double[] theoreticalMinParams; protected double costAccuracy; protected double paramsAccuracy; protected MinpackFunction(int m, double[] startParams, double theoreticalMinCost, double[] theoreticalMinParams) { this.m = m; this.n = startParams.length; this.startParams = startParams.clone(); this.theoreticalMinCost = theoreticalMinCost; this.theoreticalMinParams = theoreticalMinParams; this.costAccuracy = 1.0e-8; this.paramsAccuracy = 1.0e-5; } protected static double[] buildArray(int n, double x) { double[] array = new double[n]; Arrays.fill(array, x); return array; } public double[] getTarget() { return buildArray(m, 0.0); } public double[] getWeight() { return buildArray(m, 1.0); } public double[] getStartPoint() { return startParams.clone(); } protected void setCostAccuracy(double costAccuracy) { this.costAccuracy = costAccuracy; } protected void setParamsAccuracy(double paramsAccuracy) { this.paramsAccuracy = paramsAccuracy; } public int getN() { return startParams.length; } public void checkTheoreticalMinCost(double rms) { double threshold = costAccuracy * (1.0 + theoreticalMinCost); Assert.assertEquals(theoreticalMinCost, FastMath.sqrt(m) * rms, threshold); } public void checkTheoreticalMinParams(PointVectorValuePair optimum) { double[] params = optimum.getPointRef(); if (theoreticalMinParams != null) { for (int i = 0; i < theoreticalMinParams.length; ++i) { double mi = theoreticalMinParams[i]; double vi = params[i]; Assert.assertEquals(mi, vi, paramsAccuracy * (1.0 + FastMath.abs(mi))); } } } public double[] value(double[] variables) { DerivativeStructure[] dsV = new DerivativeStructure[variables.length]; for (int i = 0; i < variables.length; ++i) { dsV[i] = new DerivativeStructure(0, 0, variables[i]); } DerivativeStructure[] dsY = value(dsV); double[] y = new double[dsY.length]; for (int i = 0; i < dsY.length; ++i) { y[i] = dsY[i].getValue(); } return y; } public abstract DerivativeStructure[] value(DerivativeStructure[] variables); } private static class LinearFullRankFunction extends MinpackFunction { private static final long serialVersionUID = -9030323226268039536L; public LinearFullRankFunction(int m, int n, double x0, double theoreticalStartCost, double theoreticalMinCost) { super(m, buildArray(n, x0), theoreticalMinCost, buildArray(n, -1.0)); } @Override public DerivativeStructure[] value(DerivativeStructure[] variables) { DerivativeStructure sum = variables[0].getField().getZero(); for (int i = 0; i < n; ++i) { sum = sum.add(variables[i]); } DerivativeStructure t = sum.multiply(2.0 / m).add(1); DerivativeStructure[] f = new DerivativeStructure[m]; for (int i = 0; i < n; ++i) { f[i] = variables[i].subtract(t); } Arrays.fill(f, n, m, t.negate()); return f; } } private static class LinearRank1Function extends MinpackFunction { private static final long serialVersionUID = 8494863245104608300L; public LinearRank1Function(int m, int n, double x0, double theoreticalStartCost, double theoreticalMinCost) { super(m, buildArray(n, x0), theoreticalMinCost, null); } @Override public DerivativeStructure[] value(DerivativeStructure[] variables) { DerivativeStructure[] f = new DerivativeStructure[m]; DerivativeStructure sum = variables[0].getField().getZero(); for (int i = 0; i < n; ++i) { sum = sum.add(variables[i].multiply(i + 1)); } for (int i = 0; i < m; ++i) { f[i] = sum.multiply(i + 1).subtract(1); } return f; } } private static class LinearRank1ZeroColsAndRowsFunction extends MinpackFunction { private static final long serialVersionUID = -3316653043091995018L; public LinearRank1ZeroColsAndRowsFunction(int m, int n, double x0) { super(m, buildArray(n, x0), FastMath.sqrt((m * (m + 3) - 6) / (2.0 * (2 * m - 3))), null); } @Override public DerivativeStructure[] value(DerivativeStructure[] variables) { DerivativeStructure[] f = new DerivativeStructure[m]; DerivativeStructure sum = variables[0].getField().getZero(); for (int i = 1; i < (n - 1); ++i) { sum = sum.add(variables[i].multiply(i + 1)); } for (int i = 0; i < (m - 1); ++i) { f[i] = sum.multiply(i).subtract(1); } f[m - 1] = variables[0].getField().getOne().negate(); return f; } } private static class RosenbrockFunction extends MinpackFunction { private static final long serialVersionUID = 2893438180956569134L; public RosenbrockFunction(double[] startParams, double theoreticalStartCost) { super(2, startParams, 0.0, buildArray(2, 1.0)); } @Override public DerivativeStructure[] value(DerivativeStructure[] variables) { DerivativeStructure x1 = variables[0]; DerivativeStructure x2 = variables[1]; return new DerivativeStructure[] { x2.subtract(x1.multiply(x1)).multiply(10), x1.negate().add(1) }; } } private static class HelicalValleyFunction extends MinpackFunction { private static final long serialVersionUID = 220613787843200102L; public HelicalValleyFunction(double[] startParams, double theoreticalStartCost) { super(3, startParams, 0.0, new double[] { 1.0, 0.0, 0.0 }); } @Override public DerivativeStructure[] value(DerivativeStructure[] variables) { DerivativeStructure x1 = variables[0]; DerivativeStructure x2 = variables[1]; DerivativeStructure x3 = variables[2]; DerivativeStructure tmp1 = variables[0].getField().getZero(); if (x1.getValue() == 0) { tmp1 = tmp1.add((x2.getValue() >= 0) ? 0.25 : -0.25); } else { tmp1 = x2.divide(x1).atan().divide(twoPi); if (x1.getValue() < 0) { tmp1 = tmp1.add(0.5); } } DerivativeStructure tmp2 = x1.multiply(x1).add(x2.multiply(x2)).sqrt(); return new DerivativeStructure[] { x3.subtract(tmp1.multiply(10)).multiply(10), tmp2.subtract(1).multiply(10), x3 }; } private static final double twoPi = 2.0 * FastMath.PI; } private static class PowellSingularFunction extends MinpackFunction { private static final long serialVersionUID = 7298364171208142405L; public PowellSingularFunction(double[] startParams, double theoreticalStartCost) { super(4, startParams, 0.0, buildArray(4, 0.0)); } @Override public DerivativeStructure[] value(DerivativeStructure[] variables) { DerivativeStructure x1 = variables[0]; DerivativeStructure x2 = variables[1]; DerivativeStructure x3 = variables[2]; DerivativeStructure x4 = variables[3]; return new DerivativeStructure[] { x1.add(x2.multiply(10)), x3.subtract(x4).multiply(sqrt5), x2.subtract(x3.multiply(2)).multiply(x2.subtract(x3.multiply(2))), x1.subtract(x4).multiply(x1.subtract(x4)).multiply(sqrt10) }; } private static final double sqrt5 = FastMath.sqrt( 5.0); private static final double sqrt10 = FastMath.sqrt(10.0); } private static class FreudensteinRothFunction extends MinpackFunction { private static final long serialVersionUID = 2892404999344244214L; public FreudensteinRothFunction(double[] startParams, double theoreticalStartCost, double theoreticalMinCost, double[] theoreticalMinParams) { super(2, startParams, theoreticalMinCost, theoreticalMinParams); } @Override public DerivativeStructure[] value(DerivativeStructure[] variables) { DerivativeStructure x1 = variables[0]; DerivativeStructure x2 = variables[1]; return new DerivativeStructure[] { x1.subtract(13.0).add(x2.negate().add(5.0).multiply(x2).subtract(2).multiply(x2)), x1.subtract(29.0).add(x2.add(1).multiply(x2).subtract(14).multiply(x2)) }; } } private static class BardFunction extends MinpackFunction { private static final long serialVersionUID = 5990442612572087668L; public BardFunction(double x0, double theoreticalStartCost, double theoreticalMinCost, double[] theoreticalMinParams) { super(15, buildArray(3, x0), theoreticalMinCost, theoreticalMinParams); } @Override public DerivativeStructure[] value(DerivativeStructure[] variables) { DerivativeStructure x1 = variables[0]; DerivativeStructure x2 = variables[1]; DerivativeStructure x3 = variables[2]; DerivativeStructure[] f = new DerivativeStructure[m]; for (int i = 0; i < m; ++i) { double tmp1 = i + 1; double tmp2 = 15 - i; double tmp3 = (i <= 7) ? tmp1 : tmp2; f[i] = x1.add(x2.multiply(tmp2).add(x3.multiply(tmp3)).reciprocal().multiply(tmp1)).negate().add(y[i]); } return f; } private static final double[] y = { 0.14, 0.18, 0.22, 0.25, 0.29, 0.32, 0.35, 0.39, 0.37, 0.58, 0.73, 0.96, 1.34, 2.10, 4.39 }; } private static class KowalikOsborneFunction extends MinpackFunction { private static final long serialVersionUID = -4867445739880495801L; public KowalikOsborneFunction(double[] startParams, double theoreticalStartCost, double theoreticalMinCost, double[] theoreticalMinParams) { super(11, startParams, theoreticalMinCost, theoreticalMinParams); if (theoreticalStartCost > 20.0) { setCostAccuracy(2.0e-4); setParamsAccuracy(5.0e-3); } } @Override public DerivativeStructure[] value(DerivativeStructure[] variables) { DerivativeStructure x1 = variables[0]; DerivativeStructure x2 = variables[1]; DerivativeStructure x3 = variables[2]; DerivativeStructure x4 = variables[3]; DerivativeStructure[] f = new DerivativeStructure[m]; for (int i = 0; i < m; ++i) { f[i] = x1.multiply(x2.add(v[i]).multiply(v[i])).divide(x4.add(x3.add(v[i]).multiply(v[i]))).negate().add(y[i]); } return f; } private static final double[] v = { 4.0, 2.0, 1.0, 0.5, 0.25, 0.167, 0.125, 0.1, 0.0833, 0.0714, 0.0625 }; private static final double[] y = { 0.1957, 0.1947, 0.1735, 0.1600, 0.0844, 0.0627, 0.0456, 0.0342, 0.0323, 0.0235, 0.0246 }; } private static class MeyerFunction extends MinpackFunction { private static final long serialVersionUID = -838060619150131027L; public MeyerFunction(double[] startParams, double theoreticalStartCost, double theoreticalMinCost, double[] theoreticalMinParams) { super(16, startParams, theoreticalMinCost, theoreticalMinParams); if (theoreticalStartCost > 1.0e6) { setCostAccuracy(7.0e-3); setParamsAccuracy(2.0e-2); } } @Override public DerivativeStructure[] value(DerivativeStructure[] variables) { DerivativeStructure x1 = variables[0]; DerivativeStructure x2 = variables[1]; DerivativeStructure x3 = variables[2]; DerivativeStructure[] f = new DerivativeStructure[m]; for (int i = 0; i < m; ++i) { f[i] = x1.multiply(x2.divide(x3.add(5.0 * (i + 1) + 45.0)).exp()).subtract(y[i]); } return f; } private static final double[] y = { 34780.0, 28610.0, 23650.0, 19630.0, 16370.0, 13720.0, 11540.0, 9744.0, 8261.0, 7030.0, 6005.0, 5147.0, 4427.0, 3820.0, 3307.0, 2872.0 }; } private static class WatsonFunction extends MinpackFunction { private static final long serialVersionUID = -9034759294980218927L; public WatsonFunction(int n, double x0, double theoreticalStartCost, double theoreticalMinCost, double[] theoreticalMinParams) { super(31, buildArray(n, x0), theoreticalMinCost, theoreticalMinParams); } @Override public DerivativeStructure[] value(DerivativeStructure[] variables) { DerivativeStructure[] f = new DerivativeStructure[m]; for (int i = 0; i < (m - 2); ++i) { double div = (i + 1) / 29.0; DerivativeStructure s1 = variables[0].getField().getZero(); DerivativeStructure dx = variables[0].getField().getOne(); for (int j = 1; j < n; ++j) { s1 = s1.add(dx.multiply(j).multiply(variables[j])); dx = dx.multiply(div); } DerivativeStructure s2 = variables[0].getField().getZero(); dx = variables[0].getField().getOne(); for (int j = 0; j < n; ++j) { s2 = s2.add(dx.multiply(variables[j])); dx = dx.multiply(div); } f[i] = s1.subtract(s2.multiply(s2)).subtract(1); } DerivativeStructure x1 = variables[0]; DerivativeStructure x2 = variables[1]; f[m - 2] = x1; f[m - 1] = x2.subtract(x1.multiply(x1)).subtract(1); return f; } } private static class Box3DimensionalFunction extends MinpackFunction { private static final long serialVersionUID = 5511403858142574493L; public Box3DimensionalFunction(int m, double[] startParams, double theoreticalStartCost) { super(m, startParams, 0.0, new double[] { 1.0, 10.0, 1.0 }); } @Override public DerivativeStructure[] value(DerivativeStructure[] variables) { DerivativeStructure x1 = variables[0]; DerivativeStructure x2 = variables[1]; DerivativeStructure x3 = variables[2]; DerivativeStructure[] f = new DerivativeStructure[m]; for (int i = 0; i < m; ++i) { double tmp = (i + 1) / 10.0; f[i] = x1.multiply(-tmp).exp().subtract(x2.multiply(-tmp).exp()).add( x3.multiply(FastMath.exp(-i - 1) - FastMath.exp(-tmp))); } return f; } } private static class JennrichSampsonFunction extends MinpackFunction { private static final long serialVersionUID = -2489165190443352947L; public JennrichSampsonFunction(int m, double[] startParams, double theoreticalStartCost, double theoreticalMinCost, double[] theoreticalMinParams) { super(m, startParams, theoreticalMinCost, theoreticalMinParams); } @Override public DerivativeStructure[] value(DerivativeStructure[] variables) { DerivativeStructure x1 = variables[0]; DerivativeStructure x2 = variables[1]; DerivativeStructure[] f = new DerivativeStructure[m]; for (int i = 0; i < m; ++i) { double temp = i + 1; f[i] = x1.multiply(temp).exp().add(x2.multiply(temp).exp()).subtract(2 + 2 * temp).negate(); } return f; } } private static class BrownDennisFunction extends MinpackFunction { private static final long serialVersionUID = 8340018645694243910L; public BrownDennisFunction(int m, double[] startParams, double theoreticalStartCost, double theoreticalMinCost, double[] theoreticalMinParams) { super(m, startParams, theoreticalMinCost, theoreticalMinParams); setCostAccuracy(2.5e-8); } @Override public DerivativeStructure[] value(DerivativeStructure[] variables) { DerivativeStructure x1 = variables[0]; DerivativeStructure x2 = variables[1]; DerivativeStructure x3 = variables[2]; DerivativeStructure x4 = variables[3]; DerivativeStructure[] f = new DerivativeStructure[m]; for (int i = 0; i < m; ++i) { double temp = (i + 1) / 5.0; DerivativeStructure tmp1 = x1.add(x2.multiply(temp)).subtract(FastMath.exp(temp)); DerivativeStructure tmp2 = x3.add(x4.multiply(FastMath.sin(temp))).subtract(FastMath.cos(temp)); f[i] = tmp1.multiply(tmp1).add(tmp2.multiply(tmp2)); } return f; } } private static class ChebyquadFunction extends MinpackFunction { private static final long serialVersionUID = -2394877275028008594L; private static double[] buildChebyquadArray(int n, double factor) { double[] array = new double[n]; double inv = factor / (n + 1); for (int i = 0; i < n; ++i) { array[i] = (i + 1) * inv; } return array; } public ChebyquadFunction(int n, int m, double factor, double theoreticalStartCost, double theoreticalMinCost, double[] theoreticalMinParams) { super(m, buildChebyquadArray(n, factor), theoreticalMinCost, theoreticalMinParams); } @Override public DerivativeStructure[] value(DerivativeStructure[] variables) { DerivativeStructure[] f = new DerivativeStructure[m]; Arrays.fill(f, variables[0].getField().getZero()); for (int j = 0; j < n; ++j) { DerivativeStructure tmp1 = variables[0].getField().getOne(); DerivativeStructure tmp2 = variables[j].multiply(2).subtract(1); DerivativeStructure temp = tmp2.multiply(2); for (int i = 0; i < m; ++i) { f[i] = f[i].add(tmp2); DerivativeStructure ti = temp.multiply(tmp2).subtract(tmp1); tmp1 = tmp2; tmp2 = ti; } } double dx = 1.0 / n; boolean iev = false; for (int i = 0; i < m; ++i) { f[i] = f[i].multiply(dx); if (iev) { f[i] = f[i].add(1.0 / (i * (i + 2))); } iev = ! iev; } return f; } } private static class BrownAlmostLinearFunction extends MinpackFunction { private static final long serialVersionUID = 8239594490466964725L; public BrownAlmostLinearFunction(int m, double factor, double theoreticalStartCost, double theoreticalMinCost, double[] theoreticalMinParams) { super(m, buildArray(m, factor), theoreticalMinCost, theoreticalMinParams); } @Override public DerivativeStructure[] value(DerivativeStructure[] variables) { DerivativeStructure[] f = new DerivativeStructure[m]; DerivativeStructure sum = variables[0].getField().getZero().subtract(n + 1); DerivativeStructure prod = variables[0].getField().getOne(); for (int j = 0; j < n; ++j) { sum = sum.add(variables[j]); prod = prod.multiply(variables[j]); } for (int i = 0; i < n; ++i) { f[i] = variables[i].add(sum); } f[n - 1] = prod.subtract(1); return f; } } private static class Osborne1Function extends MinpackFunction { private static final long serialVersionUID = 4006743521149849494L; public Osborne1Function(double[] startParams, double theoreticalStartCost, double theoreticalMinCost, double[] theoreticalMinParams) { super(33, startParams, theoreticalMinCost, theoreticalMinParams); } @Override public DerivativeStructure[] value(DerivativeStructure[] variables) { DerivativeStructure x1 = variables[0]; DerivativeStructure x2 = variables[1]; DerivativeStructure x3 = variables[2]; DerivativeStructure x4 = variables[3]; DerivativeStructure x5 = variables[4]; DerivativeStructure[] f = new DerivativeStructure[m]; for (int i = 0; i < m; ++i) { double temp = 10.0 * i; DerivativeStructure tmp1 = x4.multiply(-temp).exp(); DerivativeStructure tmp2 = x5.multiply(-temp).exp(); f[i] = x1.add(x2.multiply(tmp1)).add(x3.multiply(tmp2)).negate().add(y[i]); } return f; } private static final double[] y = { 0.844, 0.908, 0.932, 0.936, 0.925, 0.908, 0.881, 0.850, 0.818, 0.784, 0.751, 0.718, 0.685, 0.658, 0.628, 0.603, 0.580, 0.558, 0.538, 0.522, 0.506, 0.490, 0.478, 0.467, 0.457, 0.448, 0.438, 0.431, 0.424, 0.420, 0.414, 0.411, 0.406 }; } private static class Osborne2Function extends MinpackFunction { private static final long serialVersionUID = -8418268780389858746L; public Osborne2Function(double[] startParams, double theoreticalStartCost, double theoreticalMinCost, double[] theoreticalMinParams) { super(65, startParams, theoreticalMinCost, theoreticalMinParams); } @Override public DerivativeStructure[] value(DerivativeStructure[] variables) { DerivativeStructure x01 = variables[0]; DerivativeStructure x02 = variables[1]; DerivativeStructure x03 = variables[2]; DerivativeStructure x04 = variables[3]; DerivativeStructure x05 = variables[4]; DerivativeStructure x06 = variables[5]; DerivativeStructure x07 = variables[6]; DerivativeStructure x08 = variables[7]; DerivativeStructure x09 = variables[8]; DerivativeStructure x10 = variables[9]; DerivativeStructure x11 = variables[10]; DerivativeStructure[] f = new DerivativeStructure[m]; for (int i = 0; i < m; ++i) { double temp = i / 10.0; DerivativeStructure tmp1 = x05.multiply(-temp).exp(); DerivativeStructure tmp2 = x06.negate().multiply(x09.subtract(temp).multiply(x09.subtract(temp))).exp(); DerivativeStructure tmp3 = x07.negate().multiply(x10.subtract(temp).multiply(x10.subtract(temp))).exp(); DerivativeStructure tmp4 = x08.negate().multiply(x11.subtract(temp).multiply(x11.subtract(temp))).exp(); f[i] = x01.multiply(tmp1).add(x02.multiply(tmp2)).add(x03.multiply(tmp3)).add(x04.multiply(tmp4)).negate().add(y[i]); } return f; } private static final double[] y = { 1.366, 1.191, 1.112, 1.013, 0.991, 0.885, 0.831, 0.847, 0.786, 0.725, 0.746, 0.679, 0.608, 0.655, 0.616, 0.606, 0.602, 0.626, 0.651, 0.724, 0.649, 0.649, 0.694, 0.644, 0.624, 0.661, 0.612, 0.558, 0.533, 0.495, 0.500, 0.423, 0.395, 0.375, 0.372, 0.391, 0.396, 0.405, 0.428, 0.429, 0.523, 0.562, 0.607, 0.653, 0.672, 0.708, 0.633, 0.668, 0.645, 0.632, 0.591, 0.559, 0.597, 0.625, 0.739, 0.710, 0.729, 0.720, 0.636, 0.581, 0.428, 0.292, 0.162, 0.098, 0.054 }; } } ././@LongLink100644 0 0 173 12126630646 10262 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/optimization/univariate/SimpleUnivariateValueCheckerTest.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/optimization/univariate/SimpleUnivariat100644 1750 1750 4133 12126627670 32515 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.optimization.univariate; import org.apache.commons.math3.exception.NotStrictlyPositiveException; import org.junit.Test; import org.junit.Assert; public class SimpleUnivariateValueCheckerTest { @Test(expected=NotStrictlyPositiveException.class) public void testIterationCheckPrecondition() { new SimpleUnivariateValueChecker(1e-1, 1e-2, 0); } @Test public void testIterationCheck() { final int max = 10; final SimpleUnivariateValueChecker checker = new SimpleUnivariateValueChecker(1e-1, 1e-2, max); Assert.assertTrue(checker.converged(max, null, null)); Assert.assertTrue(checker.converged(max + 1, null, null)); } @Test public void testIterationCheckDisabled() { final SimpleUnivariateValueChecker checker = new SimpleUnivariateValueChecker(1e-8, 1e-8); final UnivariatePointValuePair a = new UnivariatePointValuePair(1d, 1d); final UnivariatePointValuePair b = new UnivariatePointValuePair(10d, 10d); Assert.assertFalse(checker.converged(-1, a, b)); Assert.assertFalse(checker.converged(0, a, b)); Assert.assertFalse(checker.converged(1000000, a, b)); Assert.assertTrue(checker.converged(-1, a, a)); Assert.assertTrue(checker.converged(-1, b, b)); } } ././@LongLink100644 0 0 174 12126630646 10263 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/optimization/univariate/UnivariateMultiStartOptimizerTest.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/optimization/univariate/UnivariateMulti100644 1750 1750 11077 12126627670 32550 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.optimization.univariate; import org.apache.commons.math3.analysis.QuinticFunction; import org.apache.commons.math3.analysis.UnivariateFunction; import org.apache.commons.math3.analysis.function.Sin; import org.apache.commons.math3.optimization.GoalType; import org.apache.commons.math3.random.JDKRandomGenerator; import org.apache.commons.math3.util.FastMath; import org.junit.Assert; import org.junit.Test; public class UnivariateMultiStartOptimizerTest { @Test public void testSinMin() { UnivariateFunction f = new Sin(); UnivariateOptimizer underlying = new BrentOptimizer(1e-10, 1e-14); JDKRandomGenerator g = new JDKRandomGenerator(); g.setSeed(44428400075l); UnivariateMultiStartOptimizer optimizer = new UnivariateMultiStartOptimizer(underlying, 10, g); optimizer.optimize(300, f, GoalType.MINIMIZE, -100.0, 100.0); UnivariatePointValuePair[] optima = optimizer.getOptima(); for (int i = 1; i < optima.length; ++i) { double d = (optima[i].getPoint() - optima[i-1].getPoint()) / (2 * FastMath.PI); Assert.assertTrue(FastMath.abs(d - FastMath.rint(d)) < 1.0e-8); Assert.assertEquals(-1.0, f.value(optima[i].getPoint()), 1.0e-10); Assert.assertEquals(f.value(optima[i].getPoint()), optima[i].getValue(), 1.0e-10); } Assert.assertTrue(optimizer.getEvaluations() > 200); Assert.assertTrue(optimizer.getEvaluations() < 300); } @Test public void testQuinticMin() { // The quintic function has zeros at 0, +-0.5 and +-1. // The function has extrema (first derivative is zero) at 0.27195613 and 0.82221643, UnivariateFunction f = new QuinticFunction(); UnivariateOptimizer underlying = new BrentOptimizer(1e-9, 1e-14); JDKRandomGenerator g = new JDKRandomGenerator(); g.setSeed(4312000053L); UnivariateMultiStartOptimizer optimizer = new UnivariateMultiStartOptimizer(underlying, 5, g); UnivariatePointValuePair optimum = optimizer.optimize(300, f, GoalType.MINIMIZE, -0.3, -0.2); Assert.assertEquals(-0.2719561293, optimum.getPoint(), 1e-9); Assert.assertEquals(-0.0443342695, optimum.getValue(), 1e-9); UnivariatePointValuePair[] optima = optimizer.getOptima(); for (int i = 0; i < optima.length; ++i) { Assert.assertEquals(f.value(optima[i].getPoint()), optima[i].getValue(), 1e-9); } Assert.assertTrue(optimizer.getEvaluations() >= 50); Assert.assertTrue(optimizer.getEvaluations() <= 100); } @Test public void testBadFunction() { UnivariateFunction f = new UnivariateFunction() { public double value(double x) { if (x < 0) { throw new LocalException(); } return 0; } }; UnivariateOptimizer underlying = new BrentOptimizer(1e-9, 1e-14); JDKRandomGenerator g = new JDKRandomGenerator(); g.setSeed(4312000053L); UnivariateMultiStartOptimizer optimizer = new UnivariateMultiStartOptimizer(underlying, 5, g); try { optimizer.optimize(300, f, GoalType.MINIMIZE, -0.3, -0.2); Assert.fail(); } catch (LocalException e) { // Expected. } // Ensure that the exception was thrown because no optimum was found. Assert.assertTrue(optimizer.getOptima()[0] == null); } private static class LocalException extends RuntimeException { private static final long serialVersionUID = 1194682757034350629L; } } ././@LongLink100644 0 0 155 12126630646 10262 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/optimization/univariate/BrentOptimizerTest.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/optimization/univariate/BrentOptimizerT100644 1750 1750 26277 12126627670 32537 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.optimization.univariate; import org.apache.commons.math3.analysis.QuinticFunction; import org.apache.commons.math3.analysis.UnivariateFunction; import org.apache.commons.math3.analysis.function.Sin; import org.apache.commons.math3.analysis.function.StepFunction; import org.apache.commons.math3.analysis.FunctionUtils; import org.apache.commons.math3.exception.NumberIsTooLargeException; import org.apache.commons.math3.exception.NumberIsTooSmallException; import org.apache.commons.math3.exception.TooManyEvaluationsException; import org.apache.commons.math3.optimization.ConvergenceChecker; import org.apache.commons.math3.optimization.GoalType; import org.apache.commons.math3.stat.descriptive.DescriptiveStatistics; import org.apache.commons.math3.util.FastMath; import org.junit.Assert; import org.junit.Test; /** * @version $Id$ */ public final class BrentOptimizerTest { @Test public void testSinMin() { UnivariateFunction f = new Sin(); UnivariateOptimizer optimizer = new BrentOptimizer(1e-10, 1e-14); Assert.assertEquals(3 * Math.PI / 2, optimizer.optimize(200, f, GoalType.MINIMIZE, 4, 5).getPoint(), 1e-8); Assert.assertTrue(optimizer.getEvaluations() <= 50); Assert.assertEquals(200, optimizer.getMaxEvaluations()); Assert.assertEquals(3 * Math.PI / 2, optimizer.optimize(200, f, GoalType.MINIMIZE, 1, 5).getPoint(), 1e-8); Assert.assertTrue(optimizer.getEvaluations() <= 100); Assert.assertTrue(optimizer.getEvaluations() >= 15); try { optimizer.optimize(10, f, GoalType.MINIMIZE, 4, 5); Assert.fail("an exception should have been thrown"); } catch (TooManyEvaluationsException fee) { // expected } } @Test public void testSinMinWithValueChecker() { final UnivariateFunction f = new Sin(); final ConvergenceChecker checker = new SimpleUnivariateValueChecker(1e-5, 1e-14); // The default stopping criterion of Brent's algorithm should not // pass, but the search will stop at the given relative tolerance // for the function value. final UnivariateOptimizer optimizer = new BrentOptimizer(1e-10, 1e-14, checker); final UnivariatePointValuePair result = optimizer.optimize(200, f, GoalType.MINIMIZE, 4, 5); Assert.assertEquals(3 * Math.PI / 2, result.getPoint(), 1e-3); } @Test public void testBoundaries() { final double lower = -1.0; final double upper = +1.0; UnivariateFunction f = new UnivariateFunction() { public double value(double x) { if (x < lower) { throw new NumberIsTooSmallException(x, lower, true); } else if (x > upper) { throw new NumberIsTooLargeException(x, upper, true); } else { return x; } } }; UnivariateOptimizer optimizer = new BrentOptimizer(1e-10, 1e-14); Assert.assertEquals(lower, optimizer.optimize(100, f, GoalType.MINIMIZE, lower, upper).getPoint(), 1.0e-8); Assert.assertEquals(upper, optimizer.optimize(100, f, GoalType.MAXIMIZE, lower, upper).getPoint(), 1.0e-8); } @Test public void testQuinticMin() { // The function has local minima at -0.27195613 and 0.82221643. UnivariateFunction f = new QuinticFunction(); UnivariateOptimizer optimizer = new BrentOptimizer(1e-10, 1e-14); Assert.assertEquals(-0.27195613, optimizer.optimize(200, f, GoalType.MINIMIZE, -0.3, -0.2).getPoint(), 1.0e-8); Assert.assertEquals( 0.82221643, optimizer.optimize(200, f, GoalType.MINIMIZE, 0.3, 0.9).getPoint(), 1.0e-8); Assert.assertTrue(optimizer.getEvaluations() <= 50); // search in a large interval Assert.assertEquals(-0.27195613, optimizer.optimize(200, f, GoalType.MINIMIZE, -1.0, 0.2).getPoint(), 1.0e-8); Assert.assertTrue(optimizer.getEvaluations() <= 50); } @Test public void testQuinticMinStatistics() { // The function has local minima at -0.27195613 and 0.82221643. UnivariateFunction f = new QuinticFunction(); UnivariateOptimizer optimizer = new BrentOptimizer(1e-11, 1e-14); final DescriptiveStatistics[] stat = new DescriptiveStatistics[2]; for (int i = 0; i < stat.length; i++) { stat[i] = new DescriptiveStatistics(); } final double min = -0.75; final double max = 0.25; final int nSamples = 200; final double delta = (max - min) / nSamples; for (int i = 0; i < nSamples; i++) { final double start = min + i * delta; stat[0].addValue(optimizer.optimize(40, f, GoalType.MINIMIZE, min, max, start).getPoint()); stat[1].addValue(optimizer.getEvaluations()); } final double meanOptValue = stat[0].getMean(); final double medianEval = stat[1].getPercentile(50); Assert.assertTrue(meanOptValue > -0.2719561281); Assert.assertTrue(meanOptValue < -0.2719561280); Assert.assertEquals(23, (int) medianEval); } @Test public void testQuinticMax() { // The quintic function has zeros at 0, +-0.5 and +-1. // The function has a local maximum at 0.27195613. UnivariateFunction f = new QuinticFunction(); UnivariateOptimizer optimizer = new BrentOptimizer(1e-12, 1e-14); Assert.assertEquals(0.27195613, optimizer.optimize(100, f, GoalType.MAXIMIZE, 0.2, 0.3).getPoint(), 1e-8); try { optimizer.optimize(5, f, GoalType.MAXIMIZE, 0.2, 0.3); Assert.fail("an exception should have been thrown"); } catch (TooManyEvaluationsException miee) { // expected } } @Test public void testMinEndpoints() { UnivariateFunction f = new Sin(); UnivariateOptimizer optimizer = new BrentOptimizer(1e-8, 1e-14); // endpoint is minimum double result = optimizer.optimize(50, f, GoalType.MINIMIZE, 3 * Math.PI / 2, 5).getPoint(); Assert.assertEquals(3 * Math.PI / 2, result, 1e-6); result = optimizer.optimize(50, f, GoalType.MINIMIZE, 4, 3 * Math.PI / 2).getPoint(); Assert.assertEquals(3 * Math.PI / 2, result, 1e-6); } @Test public void testMath832() { final UnivariateFunction f = new UnivariateFunction() { public double value(double x) { final double sqrtX = FastMath.sqrt(x); final double a = 1e2 * sqrtX; final double b = 1e6 / x; final double c = 1e4 / sqrtX; return a + b + c; } }; UnivariateOptimizer optimizer = new BrentOptimizer(1e-10, 1e-8); final double result = optimizer.optimize(1483, f, GoalType.MINIMIZE, Double.MIN_VALUE, Double.MAX_VALUE).getPoint(); Assert.assertEquals(804.9355825, result, 1e-6); } /** * Contrived example showing that prior to the resolution of MATH-855 * (second revision), the algorithm would not return the best point if * it happened to be the initial guess. */ @Test public void testKeepInitIfBest() { final double minSin = 3 * Math.PI / 2; final double offset = 1e-8; final double delta = 1e-7; final UnivariateFunction f1 = new Sin(); final UnivariateFunction f2 = new StepFunction(new double[] { minSin, minSin + offset, minSin + 2 * offset}, new double[] { 0, -1, 0 }); final UnivariateFunction f = FunctionUtils.add(f1, f2); // A slightly less stringent tolerance would make the test pass // even with the previous implementation. final double relTol = 1e-8; final UnivariateOptimizer optimizer = new BrentOptimizer(relTol, 1e-100); final double init = minSin + 1.5 * offset; final UnivariatePointValuePair result = optimizer.optimize(200, f, GoalType.MINIMIZE, minSin - 6.789 * delta, minSin + 9.876 * delta, init); final int numEval = optimizer.getEvaluations(); final double sol = result.getPoint(); final double expected = init; // System.out.println("numEval=" + numEval); // System.out.println("min=" + init + " f=" + f.value(init)); // System.out.println("sol=" + sol + " f=" + f.value(sol)); // System.out.println("exp=" + expected + " f=" + f.value(expected)); Assert.assertTrue("Best point not reported", f.value(sol) <= f.value(expected)); } /** * Contrived example showing that prior to the resolution of MATH-855, * the algorithm, by always returning the last evaluated point, would * sometimes not report the best point it had found. */ @Test public void testMath855() { final double minSin = 3 * Math.PI / 2; final double offset = 1e-8; final double delta = 1e-7; final UnivariateFunction f1 = new Sin(); final UnivariateFunction f2 = new StepFunction(new double[] { minSin, minSin + offset, minSin + 5 * offset }, new double[] { 0, -1, 0 }); final UnivariateFunction f = FunctionUtils.add(f1, f2); final UnivariateOptimizer optimizer = new BrentOptimizer(1e-8, 1e-100); final UnivariatePointValuePair result = optimizer.optimize(200, f, GoalType.MINIMIZE, minSin - 6.789 * delta, minSin + 9.876 * delta); final int numEval = optimizer.getEvaluations(); final double sol = result.getPoint(); final double expected = 4.712389027602411; // System.out.println("min=" + (minSin + offset) + " f=" + f.value(minSin + offset)); // System.out.println("sol=" + sol + " f=" + f.value(sol)); // System.out.println("exp=" + expected + " f=" + f.value(expected)); Assert.assertTrue("Best point not reported", f.value(sol) <= f.value(expected)); } } ././@LongLink100644 0 0 154 12126630646 10261 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/optimization/univariate/BracketFinderTest.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/optimization/univariate/BracketFinderTe100644 1750 1750 10037 12126627670 32415 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.optimization.univariate; import org.apache.commons.math3.analysis.UnivariateFunction; import org.apache.commons.math3.optimization.GoalType; import org.junit.Assert; import org.junit.Test; /** * Test for {@link BracketFinder}. */ public class BracketFinderTest { @Test public void testCubicMin() { final BracketFinder bFind = new BracketFinder(); final UnivariateFunction func = new UnivariateFunction() { public double value(double x) { if (x < -2) { return value(-2); } else { return (x - 1) * (x + 2) * (x + 3); } } }; bFind.search(func, GoalType.MINIMIZE, -2 , -1); final double tol = 1e-15; // Comparing with results computed in Python. Assert.assertEquals(-2, bFind.getLo(), tol); Assert.assertEquals(-1, bFind.getMid(), tol); Assert.assertEquals(0.61803399999999997, bFind.getHi(), tol); } @Test public void testCubicMax() { final BracketFinder bFind = new BracketFinder(); final UnivariateFunction func = new UnivariateFunction() { public double value(double x) { if (x < -2) { return value(-2); } else { return -(x - 1) * (x + 2) * (x + 3); } } }; bFind.search(func, GoalType.MAXIMIZE, -2 , -1); final double tol = 1e-15; Assert.assertEquals(-2, bFind.getLo(), tol); Assert.assertEquals(-1, bFind.getMid(), tol); Assert.assertEquals(0.61803399999999997, bFind.getHi(), tol); } @Test public void testMinimumIsOnIntervalBoundary() { final UnivariateFunction func = new UnivariateFunction() { public double value(double x) { return x * x; } }; final BracketFinder bFind = new BracketFinder(); bFind.search(func, GoalType.MINIMIZE, 0, 1); Assert.assertTrue(bFind.getLo() <= 0); Assert.assertTrue(0 <= bFind.getHi()); bFind.search(func, GoalType.MINIMIZE, -1, 0); Assert.assertTrue(bFind.getLo() <= 0); Assert.assertTrue(0 <= bFind.getHi()); } @Test public void testIntervalBoundsOrdering() { final UnivariateFunction func = new UnivariateFunction() { public double value(double x) { return x * x; } }; final BracketFinder bFind = new BracketFinder(); bFind.search(func, GoalType.MINIMIZE, -1, 1); Assert.assertTrue(bFind.getLo() <= 0); Assert.assertTrue(0 <= bFind.getHi()); bFind.search(func, GoalType.MINIMIZE, 1, -1); Assert.assertTrue(bFind.getLo() <= 0); Assert.assertTrue(0 <= bFind.getHi()); bFind.search(func, GoalType.MINIMIZE, 1, 2); Assert.assertTrue(bFind.getLo() <= 0); Assert.assertTrue(0 <= bFind.getHi()); bFind.search(func, GoalType.MINIMIZE, 2, 1); Assert.assertTrue(bFind.getLo() <= 0); Assert.assertTrue(0 <= bFind.getHi()); } } ././@LongLink100644 0 0 150 12126630646 10255 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/optimization/PointVectorValuePairTest.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/optimization/PointVectorValuePairTest.j100644 1750 1750 3322 12126627671 32407 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.optimization; import org.apache.commons.math3.TestUtils; import org.junit.Assert; import org.junit.Test; public class PointVectorValuePairTest { @Test public void testSerial() { PointVectorValuePair pv1 = new PointVectorValuePair(new double[] { 1.0, 2.0, 3.0 }, new double[] { 4.0, 5.0 }); PointVectorValuePair pv2 = (PointVectorValuePair) TestUtils.serializeAndRecover(pv1); Assert.assertEquals(pv1.getKey().length, pv2.getKey().length); for (int i = 0; i < pv1.getKey().length; ++i) { Assert.assertEquals(pv1.getKey()[i], pv2.getKey()[i], 1.0e-15); } Assert.assertEquals(pv1.getValue().length, pv2.getValue().length); for (int i = 0; i < pv1.getValue().length; ++i) { Assert.assertEquals(pv1.getValue()[i], pv2.getValue()[i], 1.0e-15); } } } ././@LongLink100644 0 0 201 12126630646 10252 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/optimization/MultivariateDifferentiableMultiStartOptimizerTest.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/optimization/MultivariateDifferentiable100644 1750 1750 10757 12126627671 32556 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.optimization; import org.apache.commons.math3.analysis.differentiation.MultivariateDifferentiableFunction; import org.apache.commons.math3.geometry.euclidean.twod.Vector2D; import org.apache.commons.math3.optimization.general.CircleScalar; import org.apache.commons.math3.optimization.general.ConjugateGradientFormula; import org.apache.commons.math3.optimization.general.NonLinearConjugateGradientOptimizer; import org.apache.commons.math3.random.GaussianRandomGenerator; import org.apache.commons.math3.random.JDKRandomGenerator; import org.apache.commons.math3.random.RandomVectorGenerator; import org.apache.commons.math3.random.UncorrelatedRandomVectorGenerator; import org.junit.Assert; import org.junit.Test; public class MultivariateDifferentiableMultiStartOptimizerTest { @Test public void testCircleFitting() { CircleScalar circle = new CircleScalar(); circle.addPoint( 30.0, 68.0); circle.addPoint( 50.0, -6.0); circle.addPoint(110.0, -20.0); circle.addPoint( 35.0, 15.0); circle.addPoint( 45.0, 97.0); // TODO: the wrapper around NonLinearConjugateGradientOptimizer is a temporary hack for // version 3.1 of the library. It should be removed when NonLinearConjugateGradientOptimizer // will officially be declared as implementing MultivariateDifferentiableOptimizer MultivariateDifferentiableOptimizer underlying = new MultivariateDifferentiableOptimizer() { private final NonLinearConjugateGradientOptimizer cg = new NonLinearConjugateGradientOptimizer(ConjugateGradientFormula.POLAK_RIBIERE, new SimpleValueChecker(1.0e-10, 1.0e-10)); public PointValuePair optimize(int maxEval, MultivariateDifferentiableFunction f, GoalType goalType, double[] startPoint) { return cg.optimize(maxEval, f, goalType, startPoint); } public int getMaxEvaluations() { return cg.getMaxEvaluations(); } public int getEvaluations() { return cg.getEvaluations(); } public ConvergenceChecker getConvergenceChecker() { return cg.getConvergenceChecker(); } }; JDKRandomGenerator g = new JDKRandomGenerator(); g.setSeed(753289573253l); RandomVectorGenerator generator = new UncorrelatedRandomVectorGenerator(new double[] { 50.0, 50.0 }, new double[] { 10.0, 10.0 }, new GaussianRandomGenerator(g)); MultivariateDifferentiableMultiStartOptimizer optimizer = new MultivariateDifferentiableMultiStartOptimizer(underlying, 10, generator); PointValuePair optimum = optimizer.optimize(200, circle, GoalType.MINIMIZE, new double[] { 98.680, 47.345 }); Assert.assertEquals(200, optimizer.getMaxEvaluations()); PointValuePair[] optima = optimizer.getOptima(); for (PointValuePair o : optima) { Vector2D center = new Vector2D(o.getPointRef()[0], o.getPointRef()[1]); Assert.assertEquals(69.960161753, circle.getRadius(center), 1.0e-8); Assert.assertEquals(96.075902096, center.getX(), 1.0e-8); Assert.assertEquals(48.135167894, center.getY(), 1.0e-8); } Assert.assertTrue(optimizer.getEvaluations() > 70); Assert.assertTrue(optimizer.getEvaluations() < 90); Assert.assertEquals(3.1267527, optimum.getValue(), 1.0e-8); } } commons-math3-3.2-src/src/test/java/org/apache/commons/math3/ode/TestProblem6.java100644 1750 1750 4702 12126627673 26526 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.ode; /** * This class is used in the junit tests for the ODE integrators. *

              This specific problem is the following differential equation : *

               *    y' = 3x^5 - y
               * 
              * when the initial condition is y(0) = -360, the solution of this * equation degenerates to a simple quintic polynomial function : *
               *   y (t) = 3x^5 - 15x^4 + 60x^3 - 180x^2 + 360x - 360
               * 
              *

              */ public class TestProblem6 extends TestProblemAbstract { /** theoretical state */ private double[] y; /** * Simple constructor. */ public TestProblem6() { super(); double[] y0 = { -360.0 }; setInitialConditions(0.0, y0); setFinalConditions(1.0); double[] errorScale = { 1.0 }; setErrorScale(errorScale); y = new double[y0.length]; } /** * Copy constructor. * @param problem problem to copy */ public TestProblem6(TestProblem6 problem) { super(problem); y = problem.y.clone(); } /** {@inheritDoc} */ @Override public TestProblem6 copy() { return new TestProblem6(this); } @Override public void doComputeDerivatives(double t, double[] y, double[] yDot) { // compute the derivatives double t2 = t * t; double t4 = t2 * t2; double t5 = t4 * t; for (int i = 0; i < n; ++i) { yDot[i] = 3 * t5 - y[i]; } } @Override public double[] computeTheoreticalState(double t) { for (int i = 0; i < n; ++i) { y[i] = ((((3 * t - 15) * t + 60) * t - 180) * t + 360) * t - 360; } return y; } } commons-math3-3.2-src/src/test/java/org/apache/commons/math3/ode/events/EventFilterTest.java100644 1750 1750 25122 12126627672 30611 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.ode.events; import org.apache.commons.math3.analysis.solvers.BracketingNthOrderBrentSolver; import org.apache.commons.math3.exception.DimensionMismatchException; import org.apache.commons.math3.exception.MaxCountExceededException; import org.apache.commons.math3.exception.NoBracketingException; import org.apache.commons.math3.exception.NumberIsTooSmallException; import org.apache.commons.math3.ode.FirstOrderDifferentialEquations; import org.apache.commons.math3.ode.FirstOrderIntegrator; import org.apache.commons.math3.ode.nonstiff.DormandPrince853Integrator; import org.apache.commons.math3.random.RandomGenerator; import org.apache.commons.math3.random.Well19937a; import org.apache.commons.math3.util.FastMath; import org.junit.Assert; import org.junit.Test; public class EventFilterTest { @Test public void testHistoryIncreasingForward() { // start point: g > 0 testHistory(FilterType.TRIGGER_ONLY_INCREASING_EVENTS, 0.5 * FastMath.PI, 30.5 * FastMath.PI, FastMath.PI, -1); // start point: g = 0 testHistory(FilterType.TRIGGER_ONLY_INCREASING_EVENTS, 0, 30.5 * FastMath.PI, FastMath.PI, -1); // start point: g < 0 testHistory(FilterType.TRIGGER_ONLY_INCREASING_EVENTS, 1.5 * FastMath.PI, 30.5 * FastMath.PI, FastMath.PI, +1); } @Test public void testHistoryIncreasingBackward() { // start point: g > 0 testHistory(FilterType.TRIGGER_ONLY_INCREASING_EVENTS, 0.5 * FastMath.PI, -30.5 * FastMath.PI, FastMath.PI, -1); // start point: g = 0 testHistory(FilterType.TRIGGER_ONLY_INCREASING_EVENTS, 0, -30.5 * FastMath.PI, FastMath.PI, +1); // start point: g < 0 testHistory(FilterType.TRIGGER_ONLY_INCREASING_EVENTS, 1.5 * FastMath.PI, -30.5 * FastMath.PI, FastMath.PI, -1); } @Test public void testHistoryDecreasingForward() { // start point: g > 0 testHistory(FilterType.TRIGGER_ONLY_DECREASING_EVENTS, 0.5 * FastMath.PI, 30.5 * FastMath.PI, 0, +1); // start point: g = 0 testHistory(FilterType.TRIGGER_ONLY_DECREASING_EVENTS, 0, 30.5 * FastMath.PI, 0, +1); // start point: g < 0 testHistory(FilterType.TRIGGER_ONLY_DECREASING_EVENTS, 1.5 * FastMath.PI, 30.5 * FastMath.PI, 0, +1); } @Test public void testHistoryDecreasingBackward() { // start point: g > 0 testHistory(FilterType.TRIGGER_ONLY_DECREASING_EVENTS, 0.5 * FastMath.PI, -30.5 * FastMath.PI, 0, -1); // start point: g = 0 testHistory(FilterType.TRIGGER_ONLY_DECREASING_EVENTS, 0, -30.5 * FastMath.PI, 0, -1); // start point: g < 0 testHistory(FilterType.TRIGGER_ONLY_DECREASING_EVENTS, 1.5 * FastMath.PI, -30.5 * FastMath.PI, 0, +1); } public void testHistory(FilterType type, double t0, double t1, double refSwitch, double signEven) { Event onlyIncreasing = new Event(false, true); EventFilter eventFilter = new EventFilter(onlyIncreasing, type); eventFilter.init(t0, new double[] {1.0, 0.0}, t1); // first pass to set up switches history for a long period double h = FastMath.copySign(0.05, t1 - t0); double n = (int) FastMath.floor((t1 - t0) / h); for (int i = 0; i < n; ++i) { double t = t0 + i * h; eventFilter.g(t, new double[] { FastMath.sin(t), FastMath.cos(t) }); } // verify old events are preserved, even if randomly accessed RandomGenerator rng = new Well19937a(0xb0e7401265af8cd3l); for (int i = 0; i < 5000; i++) { double t = t0 + (t1 - t0) * rng.nextDouble(); double g = eventFilter.g(t, new double[] { FastMath.sin(t), FastMath.cos(t) }); int turn = (int) FastMath.floor((t - refSwitch) / (2 * FastMath.PI)); if (turn % 2 == 0) { Assert.assertEquals( signEven * FastMath.sin(t), g, 1.0e-10); } else { Assert.assertEquals(-signEven * FastMath.sin(t), g, 1.0e-10); } } } @Test public void testIncreasingOnly() throws DimensionMismatchException, NumberIsTooSmallException, MaxCountExceededException, NoBracketingException { double e = 1e-15; FirstOrderIntegrator integrator; integrator = new DormandPrince853Integrator(1.0e-3, 100.0, 1e-7, 1e-7); Event allEvents = new Event(true, true); integrator.addEventHandler(allEvents, 0.1, e, 1000, new BracketingNthOrderBrentSolver(1.0e-7, 5)); Event onlyIncreasing = new Event(false, true); integrator.addEventHandler(new EventFilter(onlyIncreasing, FilterType.TRIGGER_ONLY_INCREASING_EVENTS), 0.1, e, 100, new BracketingNthOrderBrentSolver(1.0e-7, 5)); double t0 = 0.5 * FastMath.PI; double tEnd = 5.5 * FastMath.PI; double[] y = { 0.0, 1.0 }; Assert.assertEquals(tEnd, integrator.integrate(new SineCosine(), t0, y, tEnd, y), 1.0e-7); Assert.assertEquals(5, allEvents.getEventCount()); Assert.assertEquals(2, onlyIncreasing.getEventCount()); } @Test public void testDecreasingOnly() throws DimensionMismatchException, NumberIsTooSmallException, MaxCountExceededException, NoBracketingException { double e = 1e-15; FirstOrderIntegrator integrator; integrator = new DormandPrince853Integrator(1.0e-3, 100.0, 1e-7, 1e-7); Event allEvents = new Event(true, true); integrator.addEventHandler(allEvents, 0.1, e, 1000, new BracketingNthOrderBrentSolver(1.0e-7, 5)); Event onlyDecreasing = new Event(true, false); integrator.addEventHandler(new EventFilter(onlyDecreasing, FilterType.TRIGGER_ONLY_DECREASING_EVENTS), 0.1, e, 1000, new BracketingNthOrderBrentSolver(1.0e-7, 5)); double t0 = 0.5 * FastMath.PI; double tEnd = 5.5 * FastMath.PI; double[] y = { 0.0, 1.0 }; Assert.assertEquals(tEnd, integrator.integrate(new SineCosine(), t0, y, tEnd, y), 1.0e-7); Assert.assertEquals(5, allEvents.getEventCount()); Assert.assertEquals(3, onlyDecreasing.getEventCount()); } @Test public void testTwoOppositeFilters() throws DimensionMismatchException, NumberIsTooSmallException, MaxCountExceededException, NoBracketingException { double e = 1e-15; FirstOrderIntegrator integrator; integrator = new DormandPrince853Integrator(1.0e-3, 100.0, 1e-7, 1e-7); Event allEvents = new Event(true, true); integrator.addEventHandler(allEvents, 0.1, e, 1000, new BracketingNthOrderBrentSolver(1.0e-7, 5)); Event onlyIncreasing = new Event(false, true); integrator.addEventHandler(new EventFilter(onlyIncreasing, FilterType.TRIGGER_ONLY_INCREASING_EVENTS), 0.1, e, 1000, new BracketingNthOrderBrentSolver(1.0e-7, 5)); Event onlyDecreasing = new Event(true, false); integrator.addEventHandler(new EventFilter(onlyDecreasing, FilterType.TRIGGER_ONLY_DECREASING_EVENTS), 0.1, e, 1000, new BracketingNthOrderBrentSolver(1.0e-7, 5)); double t0 = 0.5 * FastMath.PI; double tEnd = 5.5 * FastMath.PI; double[] y = { 0.0, 1.0 }; Assert.assertEquals(tEnd, integrator.integrate(new SineCosine(), t0, y, tEnd, y), 1.0e-7); Assert.assertEquals(5, allEvents.getEventCount()); Assert.assertEquals(2, onlyIncreasing.getEventCount()); Assert.assertEquals(3, onlyDecreasing.getEventCount()); } private static class SineCosine implements FirstOrderDifferentialEquations { public int getDimension() { return 2; } public void computeDerivatives(double t, double[] y, double[] yDot) { yDot[0] = y[1]; yDot[1] = -y[0]; } } /** State events for this unit test. */ protected static class Event implements EventHandler { private final boolean expectDecreasing; private final boolean expectIncreasing; private int eventCount; public Event(boolean expectDecreasing, boolean expectIncreasing) { this.expectDecreasing = expectDecreasing; this.expectIncreasing = expectIncreasing; } public int getEventCount() { return eventCount; } public void init(double t0, double[] y0, double t) { eventCount = 0; } public double g(double t, double[] y) { return y[0]; } public Action eventOccurred(double t, double[] y, boolean increasing) { if (increasing) { Assert.assertTrue(expectIncreasing); } else { Assert.assertTrue(expectDecreasing); } eventCount++; return Action.RESET_STATE; } public void resetState(double t, double[] y) { // in fact, we don't really reset anything for this test } } } commons-math3-3.2-src/src/test/java/org/apache/commons/math3/ode/events/ReappearingEventTest.java100644 1750 1750 7152 12126627672 31604 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.ode.events; import static org.junit.Assert.assertEquals; import java.util.Arrays; import org.apache.commons.math3.analysis.solvers.PegasusSolver; import org.apache.commons.math3.exception.DimensionMismatchException; import org.apache.commons.math3.exception.MaxCountExceededException; import org.apache.commons.math3.exception.NoBracketingException; import org.apache.commons.math3.exception.NumberIsTooSmallException; import org.apache.commons.math3.ode.FirstOrderDifferentialEquations; import org.apache.commons.math3.ode.FirstOrderIntegrator; import org.apache.commons.math3.ode.events.EventHandler; import org.apache.commons.math3.ode.nonstiff.DormandPrince853Integrator; import org.apache.commons.math3.ode.nonstiff.GraggBulirschStoerIntegrator; import org.junit.Test; public class ReappearingEventTest { @Test public void testDormandPrince() throws DimensionMismatchException, NumberIsTooSmallException, MaxCountExceededException, NoBracketingException { double tEnd = test(1); assertEquals(10.0, tEnd, 1e-7); } @Test public void testGragg() throws DimensionMismatchException, NumberIsTooSmallException, MaxCountExceededException, NoBracketingException { double tEnd = test(2); assertEquals(10.0, tEnd, 1e-7); } public double test(int integratorType) throws DimensionMismatchException, NumberIsTooSmallException, MaxCountExceededException, NoBracketingException { double e = 1e-15; FirstOrderIntegrator integrator; integrator = (integratorType == 1) ? new DormandPrince853Integrator(e, 100.0, 1e-7, 1e-7) : new GraggBulirschStoerIntegrator(e, 100.0, 1e-7, 1e-7); PegasusSolver rootSolver = new PegasusSolver(e, e); integrator.addEventHandler(new Event(), 0.1, e, 1000, rootSolver); double t0 = 6.0; double tEnd = 10.0; double[] y = {2.0, 2.0, 2.0, 4.0, 2.0, 7.0, 15.0}; return integrator.integrate(new Ode(), t0, y, tEnd, y); } private static class Ode implements FirstOrderDifferentialEquations { public int getDimension() { return 7; } public void computeDerivatives(double t, double[] y, double[] yDot) { Arrays.fill(yDot, 1.0); } } /** State events for this unit test. */ protected static class Event implements EventHandler { public void init(double t0, double[] y0, double t) { } public double g(double t, double[] y) { return y[6] - 15.0; } public Action eventOccurred(double t, double[] y, boolean increasing) { return Action.STOP; } public void resetState(double t, double[] y) { // Never called. } } } commons-math3-3.2-src/src/test/java/org/apache/commons/math3/ode/events/OverlappingEventsTest.java100644 1750 1750 15270 12126627672 32040 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.ode.events; import java.util.ArrayList; import java.util.List; import org.apache.commons.math3.analysis.solvers.BaseSecantSolver; import org.apache.commons.math3.analysis.solvers.PegasusSolver; import org.apache.commons.math3.exception.DimensionMismatchException; import org.apache.commons.math3.exception.MaxCountExceededException; import org.apache.commons.math3.exception.NoBracketingException; import org.apache.commons.math3.exception.NumberIsTooSmallException; import org.apache.commons.math3.ode.FirstOrderDifferentialEquations; import org.apache.commons.math3.ode.FirstOrderIntegrator; import org.apache.commons.math3.ode.nonstiff.DormandPrince853Integrator; import org.junit.Assert; import org.junit.Test; /** Tests for overlapping state events. Also tests an event function that does * not converge to zero, but does have values of opposite sign around its root. */ public class OverlappingEventsTest implements FirstOrderDifferentialEquations { /** Expected event times for first event. */ private static final double[] EVENT_TIMES1 = {1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0}; /** Expected event times for second event. */ private static final double[] EVENT_TIMES2 = {0.5, 1.0, 1.5, 2.0, 2.5, 3.0, 3.5, 4.0, 4.5, 5.0, 5.5, 6.0, 6.5, 7.0, 7.5, 8.0, 8.5, 9.0, 9.5}; /** Test for events that occur at the exact same time, but due to numerical * calculations occur very close together instead. Uses event type 0. See * {@link org.apache.commons.math3.ode.events.EventHandler#g(double, double[]) * EventHandler.g(double, double[])}. */ @Test public void testOverlappingEvents0() throws DimensionMismatchException, NumberIsTooSmallException, MaxCountExceededException, NoBracketingException { test(0); } /** Test for events that occur at the exact same time, but due to numerical * calculations occur very close together instead. Uses event type 1. See * {@link org.apache.commons.math3.ode.events.EventHandler#g(double, double[]) * EventHandler.g(double, double[])}. */ @Test public void testOverlappingEvents1() throws DimensionMismatchException, NumberIsTooSmallException, MaxCountExceededException, NoBracketingException { test(1); } /** Test for events that occur at the exact same time, but due to numerical * calculations occur very close together instead. * @param eventType the type of events to use. See * {@link org.apache.commons.math3.ode.events.EventHandler#g(double, double[]) * EventHandler.g(double, double[])}. */ public void test(int eventType) throws DimensionMismatchException, NumberIsTooSmallException, MaxCountExceededException, NoBracketingException { double e = 1e-15; FirstOrderIntegrator integrator = new DormandPrince853Integrator(e, 100.0, 1e-7, 1e-7); BaseSecantSolver rootSolver = new PegasusSolver(e, e); EventHandler evt1 = new Event(0, eventType); EventHandler evt2 = new Event(1, eventType); integrator.addEventHandler(evt1, 0.1, e, 999, rootSolver); integrator.addEventHandler(evt2, 0.1, e, 999, rootSolver); double t = 0.0; double tEnd = 10.0; double[] y = {0.0, 0.0}; List events1 = new ArrayList(); List events2 = new ArrayList(); while (t < tEnd) { t = integrator.integrate(this, t, y, tEnd, y); //System.out.println("t=" + t + ",\t\ty=[" + y[0] + "," + y[1] + "]"); if (y[0] >= 1.0) { y[0] = 0.0; events1.add(t); //System.out.println("Event 1 @ t=" + t); } if (y[1] >= 1.0) { y[1] = 0.0; events2.add(t); //System.out.println("Event 2 @ t=" + t); } } Assert.assertEquals(EVENT_TIMES1.length, events1.size()); Assert.assertEquals(EVENT_TIMES2.length, events2.size()); for(int i = 0; i < EVENT_TIMES1.length; i++) { Assert.assertEquals(EVENT_TIMES1[i], events1.get(i), 1e-7); } for(int i = 0; i < EVENT_TIMES2.length; i++) { Assert.assertEquals(EVENT_TIMES2[i], events2.get(i), 1e-7); } //System.out.println(); } /** {@inheritDoc} */ public int getDimension() { return 2; } /** {@inheritDoc} */ public void computeDerivatives(double t, double[] y, double[] yDot) { yDot[0] = 1.0; yDot[1] = 2.0; } /** State events for this unit test. */ private class Event implements EventHandler { /** The index of the continuous variable to use. */ private final int idx; /** The event type to use. See {@link #g}. */ private final int eventType; /** Constructor for the {@link Event} class. * @param idx the index of the continuous variable to use * @param eventType the type of event to use. See {@link #g} */ public Event(int idx, int eventType) { this.idx = idx; this.eventType = eventType; } /** {@inheritDoc} */ public void init(double t0, double[] y0, double t) { } /** {@inheritDoc} */ public double g(double t, double[] y) { return (eventType == 0) ? y[idx] >= 1.0 ? 1.0 : -1.0 : y[idx] - 1.0; } /** {@inheritDoc} */ public Action eventOccurred(double t, double[] y, boolean increasing) { return Action.STOP; } /** {@inheritDoc} */ public void resetState(double t, double[] y) { // Never called. } } } commons-math3-3.2-src/src/test/java/org/apache/commons/math3/ode/events/EventStateTest.java100644 1750 1750 13052 12126627672 30443 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.ode.events; import org.apache.commons.math3.analysis.solvers.BrentSolver; import org.apache.commons.math3.exception.DimensionMismatchException; import org.apache.commons.math3.exception.MaxCountExceededException; import org.apache.commons.math3.exception.NoBracketingException; import org.apache.commons.math3.exception.NumberIsTooSmallException; import org.apache.commons.math3.ode.FirstOrderDifferentialEquations; import org.apache.commons.math3.ode.nonstiff.DormandPrince853Integrator; import org.apache.commons.math3.ode.sampling.AbstractStepInterpolator; import org.apache.commons.math3.ode.sampling.DummyStepInterpolator; import org.junit.Assert; import org.junit.Test; public class EventStateTest { // JIRA: MATH-322 @Test public void closeEvents() throws MaxCountExceededException, NoBracketingException { final double r1 = 90.0; final double r2 = 135.0; final double gap = r2 - r1; EventHandler closeEventsGenerator = new EventHandler() { public void init(double t0, double[] y0, double t) { } public void resetState(double t, double[] y) { } public double g(double t, double[] y) { return (t - r1) * (r2 - t); } public Action eventOccurred(double t, double[] y, boolean increasing) { return Action.CONTINUE; } }; final double tolerance = 0.1; EventState es = new EventState(closeEventsGenerator, 1.5 * gap, tolerance, 100, new BrentSolver(tolerance)); AbstractStepInterpolator interpolator = new DummyStepInterpolator(new double[0], new double[0], true); interpolator.storeTime(r1 - 2.5 * gap); interpolator.shift(); interpolator.storeTime(r1 - 1.5 * gap); es.reinitializeBegin(interpolator); interpolator.shift(); interpolator.storeTime(r1 - 0.5 * gap); Assert.assertFalse(es.evaluateStep(interpolator)); interpolator.shift(); interpolator.storeTime(0.5 * (r1 + r2)); Assert.assertTrue(es.evaluateStep(interpolator)); Assert.assertEquals(r1, es.getEventTime(), tolerance); es.stepAccepted(es.getEventTime(), new double[0]); interpolator.shift(); interpolator.storeTime(r2 + 0.4 * gap); Assert.assertTrue(es.evaluateStep(interpolator)); Assert.assertEquals(r2, es.getEventTime(), tolerance); } // Jira: MATH-695 @Test public void testIssue695() throws DimensionMismatchException, NumberIsTooSmallException, MaxCountExceededException, NoBracketingException { FirstOrderDifferentialEquations equation = new FirstOrderDifferentialEquations() { public int getDimension() { return 1; } public void computeDerivatives(double t, double[] y, double[] yDot) { yDot[0] = 1.0; } }; DormandPrince853Integrator integrator = new DormandPrince853Integrator(0.001, 1000, 1.0e-14, 1.0e-14); integrator.addEventHandler(new ResettingEvent(10.99), 0.1, 1.0e-9, 1000); integrator.addEventHandler(new ResettingEvent(11.01), 0.1, 1.0e-9, 1000); integrator.setInitialStepSize(3.0); double target = 30.0; double[] y = new double[1]; double tEnd = integrator.integrate(equation, 0.0, y, target, y); Assert.assertEquals(target, tEnd, 1.0e-10); Assert.assertEquals(32.0, y[0], 1.0e-10); } private static class ResettingEvent implements EventHandler { private static double lastTriggerTime = Double.NEGATIVE_INFINITY; private final double tEvent; public ResettingEvent(final double tEvent) { this.tEvent = tEvent; } public void init(double t0, double[] y0, double t) { } public double g(double t, double[] y) { // the bug corresponding to issue 695 causes the g function // to be called at obsolete times t despite an event // occurring later has already been triggered. // When this occurs, the following assertion is violated Assert.assertTrue("going backard in time! (" + t + " < " + lastTriggerTime + ")", t >= lastTriggerTime); return t - tEvent; } public Action eventOccurred(double t, double[] y, boolean increasing) { // remember in a class variable when the event was triggered lastTriggerTime = t; return Action.RESET_STATE; } public void resetState(double t, double[] y) { y[0] += 1.0; } } } commons-math3-3.2-src/src/test/java/org/apache/commons/math3/ode/ContinuousOutputModelTest.java100644 1750 1750 16641 12126627673 31435 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.ode; import java.util.Random; import org.apache.commons.math3.exception.DimensionMismatchException; import org.apache.commons.math3.exception.MathIllegalArgumentException; import org.apache.commons.math3.exception.MaxCountExceededException; import org.apache.commons.math3.exception.NoBracketingException; import org.apache.commons.math3.exception.NumberIsTooSmallException; import org.apache.commons.math3.ode.nonstiff.DormandPrince54Integrator; import org.apache.commons.math3.ode.nonstiff.DormandPrince853Integrator; import org.apache.commons.math3.ode.sampling.DummyStepInterpolator; import org.apache.commons.math3.ode.sampling.StepInterpolator; import org.apache.commons.math3.util.FastMath; import org.junit.After; import org.junit.Assert; import org.junit.Before; import org.junit.Test; public class ContinuousOutputModelTest { public ContinuousOutputModelTest() { pb = null; integ = null; } @Test public void testBoundaries() throws DimensionMismatchException, NumberIsTooSmallException, MaxCountExceededException, NoBracketingException { integ.addStepHandler(new ContinuousOutputModel()); integ.integrate(pb, pb.getInitialTime(), pb.getInitialState(), pb.getFinalTime(), new double[pb.getDimension()]); ContinuousOutputModel cm = (ContinuousOutputModel) integ.getStepHandlers().iterator().next(); cm.setInterpolatedTime(2.0 * pb.getInitialTime() - pb.getFinalTime()); cm.setInterpolatedTime(2.0 * pb.getFinalTime() - pb.getInitialTime()); cm.setInterpolatedTime(0.5 * (pb.getFinalTime() + pb.getInitialTime())); } @Test public void testRandomAccess() throws DimensionMismatchException, NumberIsTooSmallException, MaxCountExceededException, NoBracketingException { ContinuousOutputModel cm = new ContinuousOutputModel(); integ.addStepHandler(cm); integ.integrate(pb, pb.getInitialTime(), pb.getInitialState(), pb.getFinalTime(), new double[pb.getDimension()]); Random random = new Random(347588535632l); double maxError = 0.0; for (int i = 0; i < 1000; ++i) { double r = random.nextDouble(); double time = r * pb.getInitialTime() + (1.0 - r) * pb.getFinalTime(); cm.setInterpolatedTime(time); double[] interpolatedY = cm.getInterpolatedState (); double[] theoreticalY = pb.computeTheoreticalState(time); double dx = interpolatedY[0] - theoreticalY[0]; double dy = interpolatedY[1] - theoreticalY[1]; double error = dx * dx + dy * dy; if (error > maxError) { maxError = error; } } Assert.assertTrue(maxError < 1.0e-9); } @Test public void testModelsMerging() throws MaxCountExceededException, MathIllegalArgumentException { // theoretical solution: y[0] = cos(t), y[1] = sin(t) FirstOrderDifferentialEquations problem = new FirstOrderDifferentialEquations() { public void computeDerivatives(double t, double[] y, double[] dot) { dot[0] = -y[1]; dot[1] = y[0]; } public int getDimension() { return 2; } }; // integrate backward from π to 0; ContinuousOutputModel cm1 = new ContinuousOutputModel(); FirstOrderIntegrator integ1 = new DormandPrince853Integrator(0, 1.0, 1.0e-8, 1.0e-8); integ1.addStepHandler(cm1); integ1.integrate(problem, FastMath.PI, new double[] { -1.0, 0.0 }, 0, new double[2]); // integrate backward from 2π to π ContinuousOutputModel cm2 = new ContinuousOutputModel(); FirstOrderIntegrator integ2 = new DormandPrince853Integrator(0, 0.1, 1.0e-12, 1.0e-12); integ2.addStepHandler(cm2); integ2.integrate(problem, 2.0 * FastMath.PI, new double[] { 1.0, 0.0 }, FastMath.PI, new double[2]); // merge the two half circles ContinuousOutputModel cm = new ContinuousOutputModel(); cm.append(cm2); cm.append(new ContinuousOutputModel()); cm.append(cm1); // check circle Assert.assertEquals(2.0 * FastMath.PI, cm.getInitialTime(), 1.0e-12); Assert.assertEquals(0, cm.getFinalTime(), 1.0e-12); Assert.assertEquals(cm.getFinalTime(), cm.getInterpolatedTime(), 1.0e-12); for (double t = 0; t < 2.0 * FastMath.PI; t += 0.1) { cm.setInterpolatedTime(t); double[] y = cm.getInterpolatedState(); Assert.assertEquals(FastMath.cos(t), y[0], 1.0e-7); Assert.assertEquals(FastMath.sin(t), y[1], 1.0e-7); } } @Test public void testErrorConditions() throws MaxCountExceededException, MathIllegalArgumentException { ContinuousOutputModel cm = new ContinuousOutputModel(); cm.handleStep(buildInterpolator(0, new double[] { 0.0, 1.0, -2.0 }, 1), true); // dimension mismatch Assert.assertTrue(checkAppendError(cm, 1.0, new double[] { 0.0, 1.0 }, 2.0)); // hole between time ranges Assert.assertTrue(checkAppendError(cm, 10.0, new double[] { 0.0, 1.0, -2.0 }, 20.0)); // propagation direction mismatch Assert.assertTrue(checkAppendError(cm, 1.0, new double[] { 0.0, 1.0, -2.0 }, 0.0)); // no errors Assert.assertFalse(checkAppendError(cm, 1.0, new double[] { 0.0, 1.0, -2.0 }, 2.0)); } private boolean checkAppendError(ContinuousOutputModel cm, double t0, double[] y0, double t1) throws MaxCountExceededException, MathIllegalArgumentException { try { ContinuousOutputModel otherCm = new ContinuousOutputModel(); otherCm.handleStep(buildInterpolator(t0, y0, t1), true); cm.append(otherCm); } catch(IllegalArgumentException iae) { return true; // there was an allowable error } return false; // no allowable error } private StepInterpolator buildInterpolator(double t0, double[] y0, double t1) { DummyStepInterpolator interpolator = new DummyStepInterpolator(y0, new double[y0.length], t1 >= t0); interpolator.storeTime(t0); interpolator.shift(); interpolator.storeTime(t1); return interpolator; } public void checkValue(double value, double reference) { Assert.assertTrue(FastMath.abs(value - reference) < 1.0e-10); } @Before public void setUp() { pb = new TestProblem3(0.9); double minStep = 0; double maxStep = pb.getFinalTime() - pb.getInitialTime(); integ = new DormandPrince54Integrator(minStep, maxStep, 1.0e-8, 1.0e-8); } @After public void tearDown() { pb = null; integ = null; } TestProblem3 pb; FirstOrderIntegrator integ; } commons-math3-3.2-src/src/test/java/org/apache/commons/math3/ode/TestProblem2.java100644 1750 1750 4411 12126627673 26517 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.ode; import org.apache.commons.math3.util.FastMath; /** * This class is used in the junit tests for the ODE integrators. *

              This specific problem is the following differential equation : *

               *    y' = t^3 - t y
               * 
              * with the initial condition y (0) = 0. The solution of this equation * is the following function : *
               *   y (t) = t^2 + 2 (exp (- t^2 / 2) - 1)
               * 
              *

              */ public class TestProblem2 extends TestProblemAbstract { /** theoretical state */ private double[] y; /** * Simple constructor. */ public TestProblem2() { super(); double[] y0 = { 0.0 }; setInitialConditions(0.0, y0); setFinalConditions(1.0); double[] errorScale = { 1.0 }; setErrorScale(errorScale); y = new double[y0.length]; } /** * Copy constructor. * @param problem problem to copy */ public TestProblem2(TestProblem2 problem) { super(problem); y = problem.y.clone(); } /** {@inheritDoc} */ @Override public TestProblem2 copy() { return new TestProblem2(this); } @Override public void doComputeDerivatives(double t, double[] y, double[] yDot) { // compute the derivatives for (int i = 0; i < n; ++i) yDot[i] = t * (t * t - y[i]); } @Override public double[] computeTheoreticalState(double t) { double t2 = t * t; double c = t2 + 2 * (FastMath.exp (-0.5 * t2) - 1); for (int i = 0; i < n; ++i) { y[i] = c; } return y; } } commons-math3-3.2-src/src/test/java/org/apache/commons/math3/ode/JacobianMatricesTest.java100644 1750 1750 57211 12126627673 30261 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.ode; import org.apache.commons.math3.exception.DimensionMismatchException; import org.apache.commons.math3.exception.MaxCountExceededException; import org.apache.commons.math3.exception.NoBracketingException; import org.apache.commons.math3.exception.NumberIsTooSmallException; import org.apache.commons.math3.ode.JacobianMatrices.MismatchedEquations; import org.apache.commons.math3.ode.nonstiff.DormandPrince54Integrator; import org.apache.commons.math3.stat.descriptive.SummaryStatistics; import org.apache.commons.math3.util.FastMath; import org.junit.Assert; import org.junit.Test; public class JacobianMatricesTest { @Test public void testLowAccuracyExternalDifferentiation() throws NumberIsTooSmallException, DimensionMismatchException, MaxCountExceededException, NoBracketingException { // this test does not really test JacobianMatrices, // it only shows that WITHOUT this class, attempting to recover // the jacobians from external differentiation on simple integration // results with low accuracy gives very poor results. In fact, // the curves dy/dp = g(b) when b varies from 2.88 to 3.08 are // essentially noise. // This test is taken from Hairer, Norsett and Wanner book // Solving Ordinary Differential Equations I (Nonstiff problems), // the curves dy/dp = g(b) are in figure 6.5 FirstOrderIntegrator integ = new DormandPrince54Integrator(1.0e-8, 100.0, new double[] { 1.0e-4, 1.0e-4 }, new double[] { 1.0e-4, 1.0e-4 }); double hP = 1.0e-12; SummaryStatistics residualsP0 = new SummaryStatistics(); SummaryStatistics residualsP1 = new SummaryStatistics(); for (double b = 2.88; b < 3.08; b += 0.001) { Brusselator brusselator = new Brusselator(b); double[] y = { 1.3, b }; integ.integrate(brusselator, 0, y, 20.0, y); double[] yP = { 1.3, b + hP }; integ.integrate(brusselator, 0, yP, 20.0, yP); residualsP0.addValue((yP[0] - y[0]) / hP - brusselator.dYdP0()); residualsP1.addValue((yP[1] - y[1]) / hP - brusselator.dYdP1()); } Assert.assertTrue((residualsP0.getMax() - residualsP0.getMin()) > 500); Assert.assertTrue(residualsP0.getStandardDeviation() > 30); Assert.assertTrue((residualsP1.getMax() - residualsP1.getMin()) > 700); Assert.assertTrue(residualsP1.getStandardDeviation() > 40); } @Test public void testHighAccuracyExternalDifferentiation() throws NumberIsTooSmallException, DimensionMismatchException, MaxCountExceededException, NoBracketingException, UnknownParameterException { FirstOrderIntegrator integ = new DormandPrince54Integrator(1.0e-8, 100.0, new double[] { 1.0e-10, 1.0e-10 }, new double[] { 1.0e-10, 1.0e-10 }); double hP = 1.0e-12; SummaryStatistics residualsP0 = new SummaryStatistics(); SummaryStatistics residualsP1 = new SummaryStatistics(); for (double b = 2.88; b < 3.08; b += 0.001) { ParamBrusselator brusselator = new ParamBrusselator(b); double[] y = { 1.3, b }; integ.integrate(brusselator, 0, y, 20.0, y); double[] yP = { 1.3, b + hP }; brusselator.setParameter("b", b + hP); integ.integrate(brusselator, 0, yP, 20.0, yP); residualsP0.addValue((yP[0] - y[0]) / hP - brusselator.dYdP0()); residualsP1.addValue((yP[1] - y[1]) / hP - brusselator.dYdP1()); } Assert.assertTrue((residualsP0.getMax() - residualsP0.getMin()) > 0.02); Assert.assertTrue((residualsP0.getMax() - residualsP0.getMin()) < 0.03); Assert.assertTrue(residualsP0.getStandardDeviation() > 0.003); Assert.assertTrue(residualsP0.getStandardDeviation() < 0.004); Assert.assertTrue((residualsP1.getMax() - residualsP1.getMin()) > 0.04); Assert.assertTrue((residualsP1.getMax() - residualsP1.getMin()) < 0.05); Assert.assertTrue(residualsP1.getStandardDeviation() > 0.007); Assert.assertTrue(residualsP1.getStandardDeviation() < 0.008); } @Test public void testInternalDifferentiation() throws NumberIsTooSmallException, DimensionMismatchException, MaxCountExceededException, NoBracketingException, UnknownParameterException, MismatchedEquations { AbstractIntegrator integ = new DormandPrince54Integrator(1.0e-8, 100.0, new double[] { 1.0e-4, 1.0e-4 }, new double[] { 1.0e-4, 1.0e-4 }); double hP = 1.0e-12; double hY = 1.0e-12; SummaryStatistics residualsP0 = new SummaryStatistics(); SummaryStatistics residualsP1 = new SummaryStatistics(); for (double b = 2.88; b < 3.08; b += 0.001) { ParamBrusselator brusselator = new ParamBrusselator(b); brusselator.setParameter(ParamBrusselator.B, b); double[] z = { 1.3, b }; double[][] dZdZ0 = new double[2][2]; double[] dZdP = new double[2]; JacobianMatrices jacob = new JacobianMatrices(brusselator, new double[] { hY, hY }, ParamBrusselator.B); jacob.setParameterizedODE(brusselator); jacob.setParameterStep(ParamBrusselator.B, hP); jacob.setInitialParameterJacobian(ParamBrusselator.B, new double[] { 0.0, 1.0 }); ExpandableStatefulODE efode = new ExpandableStatefulODE(brusselator); efode.setTime(0); efode.setPrimaryState(z); jacob.registerVariationalEquations(efode); integ.setMaxEvaluations(5000); integ.integrate(efode, 20.0); jacob.getCurrentMainSetJacobian(dZdZ0); jacob.getCurrentParameterJacobian(ParamBrusselator.B, dZdP); // Assert.assertEquals(5000, integ.getMaxEvaluations()); // Assert.assertTrue(integ.getEvaluations() > 1500); // Assert.assertTrue(integ.getEvaluations() < 2100); // Assert.assertEquals(4 * integ.getEvaluations(), integ.getEvaluations()); residualsP0.addValue(dZdP[0] - brusselator.dYdP0()); residualsP1.addValue(dZdP[1] - brusselator.dYdP1()); } Assert.assertTrue((residualsP0.getMax() - residualsP0.getMin()) < 0.02); Assert.assertTrue(residualsP0.getStandardDeviation() < 0.003); Assert.assertTrue((residualsP1.getMax() - residualsP1.getMin()) < 0.05); Assert.assertTrue(residualsP1.getStandardDeviation() < 0.01); } @Test public void testAnalyticalDifferentiation() throws MaxCountExceededException, DimensionMismatchException, NumberIsTooSmallException, NoBracketingException, UnknownParameterException, MismatchedEquations { AbstractIntegrator integ = new DormandPrince54Integrator(1.0e-8, 100.0, new double[] { 1.0e-4, 1.0e-4 }, new double[] { 1.0e-4, 1.0e-4 }); SummaryStatistics residualsP0 = new SummaryStatistics(); SummaryStatistics residualsP1 = new SummaryStatistics(); for (double b = 2.88; b < 3.08; b += 0.001) { Brusselator brusselator = new Brusselator(b); double[] z = { 1.3, b }; double[][] dZdZ0 = new double[2][2]; double[] dZdP = new double[2]; JacobianMatrices jacob = new JacobianMatrices(brusselator, Brusselator.B); jacob.addParameterJacobianProvider(brusselator); jacob.setInitialParameterJacobian(Brusselator.B, new double[] { 0.0, 1.0 }); ExpandableStatefulODE efode = new ExpandableStatefulODE(brusselator); efode.setTime(0); efode.setPrimaryState(z); jacob.registerVariationalEquations(efode); integ.setMaxEvaluations(5000); integ.integrate(efode, 20.0); jacob.getCurrentMainSetJacobian(dZdZ0); jacob.getCurrentParameterJacobian(Brusselator.B, dZdP); // Assert.assertEquals(5000, integ.getMaxEvaluations()); // Assert.assertTrue(integ.getEvaluations() > 350); // Assert.assertTrue(integ.getEvaluations() < 510); residualsP0.addValue(dZdP[0] - brusselator.dYdP0()); residualsP1.addValue(dZdP[1] - brusselator.dYdP1()); } Assert.assertTrue((residualsP0.getMax() - residualsP0.getMin()) < 0.014); Assert.assertTrue(residualsP0.getStandardDeviation() < 0.003); Assert.assertTrue((residualsP1.getMax() - residualsP1.getMin()) < 0.05); Assert.assertTrue(residualsP1.getStandardDeviation() < 0.01); } @Test public void testFinalResult() throws MaxCountExceededException, DimensionMismatchException, NumberIsTooSmallException, NoBracketingException, UnknownParameterException, MismatchedEquations { AbstractIntegrator integ = new DormandPrince54Integrator(1.0e-8, 100.0, new double[] { 1.0e-10, 1.0e-10 }, new double[] { 1.0e-10, 1.0e-10 }); double[] y = new double[] { 0.0, 1.0 }; Circle circle = new Circle(y, 1.0, 1.0, 0.1); JacobianMatrices jacob = new JacobianMatrices(circle, Circle.CX, Circle.CY, Circle.OMEGA); jacob.addParameterJacobianProvider(circle); jacob.setInitialMainStateJacobian(circle.exactDyDy0(0)); jacob.setInitialParameterJacobian(Circle.CX, circle.exactDyDcx(0)); jacob.setInitialParameterJacobian(Circle.CY, circle.exactDyDcy(0)); jacob.setInitialParameterJacobian(Circle.OMEGA, circle.exactDyDom(0)); ExpandableStatefulODE efode = new ExpandableStatefulODE(circle); efode.setTime(0); efode.setPrimaryState(y); jacob.registerVariationalEquations(efode); integ.setMaxEvaluations(5000); double t = 18 * FastMath.PI; integ.integrate(efode, t); y = efode.getPrimaryState(); for (int i = 0; i < y.length; ++i) { Assert.assertEquals(circle.exactY(t)[i], y[i], 1.0e-9); } double[][] dydy0 = new double[2][2]; jacob.getCurrentMainSetJacobian(dydy0); for (int i = 0; i < dydy0.length; ++i) { for (int j = 0; j < dydy0[i].length; ++j) { Assert.assertEquals(circle.exactDyDy0(t)[i][j], dydy0[i][j], 1.0e-9); } } double[] dydcx = new double[2]; jacob.getCurrentParameterJacobian(Circle.CX, dydcx); for (int i = 0; i < dydcx.length; ++i) { Assert.assertEquals(circle.exactDyDcx(t)[i], dydcx[i], 1.0e-7); } double[] dydcy = new double[2]; jacob.getCurrentParameterJacobian(Circle.CY, dydcy); for (int i = 0; i < dydcy.length; ++i) { Assert.assertEquals(circle.exactDyDcy(t)[i], dydcy[i], 1.0e-7); } double[] dydom = new double[2]; jacob.getCurrentParameterJacobian(Circle.OMEGA, dydom); for (int i = 0; i < dydom.length; ++i) { Assert.assertEquals(circle.exactDyDom(t)[i], dydom[i], 1.0e-7); } } @Test public void testParameterizable() throws MaxCountExceededException, DimensionMismatchException, NumberIsTooSmallException, NoBracketingException, UnknownParameterException, MismatchedEquations { AbstractIntegrator integ = new DormandPrince54Integrator(1.0e-8, 100.0, new double[] { 1.0e-10, 1.0e-10 }, new double[] { 1.0e-10, 1.0e-10 }); double[] y = new double[] { 0.0, 1.0 }; ParameterizedCircle pcircle = new ParameterizedCircle(y, 1.0, 1.0, 0.1); double hP = 1.0e-12; double hY = 1.0e-12; JacobianMatrices jacob = new JacobianMatrices(pcircle, new double[] { hY, hY }, ParameterizedCircle.CX, ParameterizedCircle.CY, ParameterizedCircle.OMEGA); jacob.setParameterizedODE(pcircle); jacob.setParameterStep(ParameterizedCircle.CX, hP); jacob.setParameterStep(ParameterizedCircle.CY, hP); jacob.setParameterStep(ParameterizedCircle.OMEGA, hP); jacob.setInitialMainStateJacobian(pcircle.exactDyDy0(0)); jacob.setInitialParameterJacobian(ParameterizedCircle.CX, pcircle.exactDyDcx(0)); jacob.setInitialParameterJacobian(ParameterizedCircle.CY, pcircle.exactDyDcy(0)); jacob.setInitialParameterJacobian(ParameterizedCircle.OMEGA, pcircle.exactDyDom(0)); ExpandableStatefulODE efode = new ExpandableStatefulODE(pcircle); efode.setTime(0); efode.setPrimaryState(y); jacob.registerVariationalEquations(efode); integ.setMaxEvaluations(50000); double t = 18 * FastMath.PI; integ.integrate(efode, t); y = efode.getPrimaryState(); for (int i = 0; i < y.length; ++i) { Assert.assertEquals(pcircle.exactY(t)[i], y[i], 1.0e-9); } double[][] dydy0 = new double[2][2]; jacob.getCurrentMainSetJacobian(dydy0); for (int i = 0; i < dydy0.length; ++i) { for (int j = 0; j < dydy0[i].length; ++j) { Assert.assertEquals(pcircle.exactDyDy0(t)[i][j], dydy0[i][j], 5.0e-4); } } double[] dydp0 = new double[2]; jacob.getCurrentParameterJacobian(ParameterizedCircle.CX, dydp0); for (int i = 0; i < dydp0.length; ++i) { Assert.assertEquals(pcircle.exactDyDcx(t)[i], dydp0[i], 5.0e-4); } double[] dydp1 = new double[2]; jacob.getCurrentParameterJacobian(ParameterizedCircle.OMEGA, dydp1); for (int i = 0; i < dydp1.length; ++i) { Assert.assertEquals(pcircle.exactDyDom(t)[i], dydp1[i], 1.0e-2); } } private static class Brusselator extends AbstractParameterizable implements MainStateJacobianProvider, ParameterJacobianProvider { public static final String B = "b"; private double b; public Brusselator(double b) { super(B); this.b = b; } public int getDimension() { return 2; } public void computeDerivatives(double t, double[] y, double[] yDot) { double prod = y[0] * y[0] * y[1]; yDot[0] = 1 + prod - (b + 1) * y[0]; yDot[1] = b * y[0] - prod; } public void computeMainStateJacobian(double t, double[] y, double[] yDot, double[][] dFdY) { double p = 2 * y[0] * y[1]; double y02 = y[0] * y[0]; dFdY[0][0] = p - (1 + b); dFdY[0][1] = y02; dFdY[1][0] = b - p; dFdY[1][1] = -y02; } public void computeParameterJacobian(double t, double[] y, double[] yDot, String paramName, double[] dFdP) { if (isSupported(paramName)) { dFdP[0] = -y[0]; dFdP[1] = y[0]; } else { dFdP[0] = 0; dFdP[1] = 0; } } public double dYdP0() { return -1088.232716447743 + (1050.775747149553 + (-339.012934631828 + 36.52917025056327 * b) * b) * b; } public double dYdP1() { return 1502.824469929139 + (-1438.6974831849952 + (460.959476642384 - 49.43847385647082 * b) * b) * b; } } private static class ParamBrusselator extends AbstractParameterizable implements FirstOrderDifferentialEquations, ParameterizedODE { public static final String B = "b"; private double b; public ParamBrusselator(double b) { super(B); this.b = b; } public int getDimension() { return 2; } /** {@inheritDoc} */ public double getParameter(final String name) throws UnknownParameterException { complainIfNotSupported(name); return b; } /** {@inheritDoc} */ public void setParameter(final String name, final double value) throws UnknownParameterException { complainIfNotSupported(name); b = value; } public void computeDerivatives(double t, double[] y, double[] yDot) { double prod = y[0] * y[0] * y[1]; yDot[0] = 1 + prod - (b + 1) * y[0]; yDot[1] = b * y[0] - prod; } public double dYdP0() { return -1088.232716447743 + (1050.775747149553 + (-339.012934631828 + 36.52917025056327 * b) * b) * b; } public double dYdP1() { return 1502.824469929139 + (-1438.6974831849952 + (460.959476642384 - 49.43847385647082 * b) * b) * b; } } /** ODE representing a point moving on a circle with provided center and angular rate. */ private static class Circle extends AbstractParameterizable implements MainStateJacobianProvider, ParameterJacobianProvider { public static final String CX = "cx"; public static final String CY = "cy"; public static final String OMEGA = "omega"; private final double[] y0; private double cx; private double cy; private double omega; public Circle(double[] y0, double cx, double cy, double omega) { super(CX,CY,OMEGA); this.y0 = y0.clone(); this.cx = cx; this.cy = cy; this.omega = omega; } public int getDimension() { return 2; } public void computeDerivatives(double t, double[] y, double[] yDot) { yDot[0] = omega * (cy - y[1]); yDot[1] = omega * (y[0] - cx); } public void computeMainStateJacobian(double t, double[] y, double[] yDot, double[][] dFdY) { dFdY[0][0] = 0; dFdY[0][1] = -omega; dFdY[1][0] = omega; dFdY[1][1] = 0; } public void computeParameterJacobian(double t, double[] y, double[] yDot, String paramName, double[] dFdP) throws UnknownParameterException { complainIfNotSupported(paramName); if (paramName.equals(CX)) { dFdP[0] = 0; dFdP[1] = -omega; } else if (paramName.equals(CY)) { dFdP[0] = omega; dFdP[1] = 0; } else { dFdP[0] = cy - y[1]; dFdP[1] = y[0] - cx; } } public double[] exactY(double t) { double cos = FastMath.cos(omega * t); double sin = FastMath.sin(omega * t); double dx0 = y0[0] - cx; double dy0 = y0[1] - cy; return new double[] { cx + cos * dx0 - sin * dy0, cy + sin * dx0 + cos * dy0 }; } public double[][] exactDyDy0(double t) { double cos = FastMath.cos(omega * t); double sin = FastMath.sin(omega * t); return new double[][] { { cos, -sin }, { sin, cos } }; } public double[] exactDyDcx(double t) { double cos = FastMath.cos(omega * t); double sin = FastMath.sin(omega * t); return new double[] {1 - cos, -sin}; } public double[] exactDyDcy(double t) { double cos = FastMath.cos(omega * t); double sin = FastMath.sin(omega * t); return new double[] {sin, 1 - cos}; } public double[] exactDyDom(double t) { double cos = FastMath.cos(omega * t); double sin = FastMath.sin(omega * t); double dx0 = y0[0] - cx; double dy0 = y0[1] - cy; return new double[] { -t * (sin * dx0 + cos * dy0) , t * (cos * dx0 - sin * dy0) }; } } /** ODE representing a point moving on a circle with provided center and angular rate. */ private static class ParameterizedCircle extends AbstractParameterizable implements FirstOrderDifferentialEquations, ParameterizedODE { public static final String CX = "cx"; public static final String CY = "cy"; public static final String OMEGA = "omega"; private final double[] y0; private double cx; private double cy; private double omega; public ParameterizedCircle(double[] y0, double cx, double cy, double omega) { super(CX,CY,OMEGA); this.y0 = y0.clone(); this.cx = cx; this.cy = cy; this.omega = omega; } public int getDimension() { return 2; } public void computeDerivatives(double t, double[] y, double[] yDot) { yDot[0] = omega * (cy - y[1]); yDot[1] = omega * (y[0] - cx); } public double getParameter(final String name) throws UnknownParameterException { if (name.equals(CX)) { return cx; } else if (name.equals(CY)) { return cy; } else if (name.equals(OMEGA)) { return omega; } else { throw new UnknownParameterException(name); } } public void setParameter(final String name, final double value) throws UnknownParameterException { if (name.equals(CX)) { cx = value; } else if (name.equals(CY)) { cy = value; } else if (name.equals(OMEGA)) { omega = value; } else { throw new UnknownParameterException(name); } } public double[] exactY(double t) { double cos = FastMath.cos(omega * t); double sin = FastMath.sin(omega * t); double dx0 = y0[0] - cx; double dy0 = y0[1] - cy; return new double[] { cx + cos * dx0 - sin * dy0, cy + sin * dx0 + cos * dy0 }; } public double[][] exactDyDy0(double t) { double cos = FastMath.cos(omega * t); double sin = FastMath.sin(omega * t); return new double[][] { { cos, -sin }, { sin, cos } }; } public double[] exactDyDcx(double t) { double cos = FastMath.cos(omega * t); double sin = FastMath.sin(omega * t); return new double[] {1 - cos, -sin}; } public double[] exactDyDcy(double t) { double cos = FastMath.cos(omega * t); double sin = FastMath.sin(omega * t); return new double[] {sin, 1 - cos}; } public double[] exactDyDom(double t) { double cos = FastMath.cos(omega * t); double sin = FastMath.sin(omega * t); double dx0 = y0[0] - cx; double dy0 = y0[1] - cy; return new double[] { -t * (sin * dx0 + cos * dy0) , t * (cos * dx0 - sin * dy0) }; } } } ././@LongLink100644 0 0 150 12126630646 10255 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/ode/sampling/StepNormalizerOutputTest.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/ode/sampling/StepNormalizerOutputTest.j100644 1750 1750 4732 12126627673 32364 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.ode.sampling; /** Step normalizer output tests, for problems where the first and last points * are not fixed points. */ public class StepNormalizerOutputTest extends StepNormalizerOutputTestBase { @Override protected double getStart() { return 0.3; } @Override protected double getEnd() { return 10.1; } @Override protected double[] getExpInc() { return new double[] { 0.3, 0.8, 1.3, 1.8, 2.3, 2.8, 3.3, 3.8, 4.3, 4.8, 5.3, 5.8, 6.3, 6.8, 7.3, 7.8, 8.3, 8.8, 9.3, 9.8, 10.1 }; } @Override protected double[] getExpIncRev() { return new double[] { 10.1, 9.6, 9.1, 8.6, 8.1, 7.6, 7.1, 6.6, 6.1, 5.6, 5.1, 4.6, 4.1, 3.6, 3.1, 2.6, 2.1, 1.6, 1.1, 0.6, 0.3 }; } @Override protected double[] getExpMul() { return new double[] { 0.3, 0.5, 1.0, 1.5, 2.0, 2.5, 3.0, 3.5, 4.0, 4.5, 5.0, 5.5, 6.0, 6.5, 7.0, 7.5, 8.0, 8.5, 9.0, 9.5, 10.0, 10.1 }; } @Override protected double[] getExpMulRev() { return new double[] { 10.1, 10.0, 9.5, 9.0, 8.5, 8.0, 7.5, 7.0, 6.5, 6.0, 5.5, 5.0, 4.5, 4.0, 3.5, 3.0, 2.5, 2.0, 1.5, 1.0, 0.5, 0.3 }; } @Override protected int[][] getO() { return new int[][] {{1, 1}, {1, 1}, {0, 1}, {0, 1}, {1, 0}, {1, 0}, {0, 0}, {0, 0}, {1, 1}, {1, 1}, {0, 1}, {0, 1}, {1, 0}, {1, 0}, {0, 0}, {0, 0}}; } } ././@LongLink100644 0 0 157 12126630646 10264 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/ode/sampling/StepNormalizerOutputOverlapTest.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/ode/sampling/StepNormalizerOutputOverla100644 1750 1750 4731 12126627673 32444 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.ode.sampling; /** Step normalizer output tests, for problems where the first and last points * are overlap fixed points. */ public class StepNormalizerOutputOverlapTest extends StepNormalizerOutputTestBase { @Override protected double getStart() { return 0.0; } @Override protected double getEnd() { return 10.0; } @Override protected double[] getExpInc() { return new double[] { 0.0, 0.5, 1.0, 1.5, 2.0, 2.5, 3.0, 3.5, 4.0, 4.5, 5.0, 5.5, 6.0, 6.5, 7.0, 7.5, 8.0, 8.5, 9.0, 9.5, 10.0 }; } @Override protected double[] getExpIncRev() { return new double[] { 10.0, 9.5, 9.0, 8.5, 8.0, 7.5, 7.0, 6.5, 6.0, 5.5, 5.0, 4.5, 4.0, 3.5, 3.0, 2.5, 2.0, 1.5, 1.0, 0.5, 0.0 }; } @Override protected double[] getExpMul() { return new double[] { 0.0, 0.5, 1.0, 1.5, 2.0, 2.5, 3.0, 3.5, 4.0, 4.5, 5.0, 5.5, 6.0, 6.5, 7.0, 7.5, 8.0, 8.5, 9.0, 9.5, 10.0 }; } @Override protected double[] getExpMulRev() { return new double[] { 10.0, 9.5, 9.0, 8.5, 8.0, 7.5, 7.0, 6.5, 6.0, 5.5, 5.0, 4.5, 4.0, 3.5, 3.0, 2.5, 2.0, 1.5, 1.0, 0.5, 0.0 }; } @Override protected int[][] getO() { return new int[][] {{1, 0}, {1, 0}, {0, 0}, {0, 0}, {1, 0}, {1, 0}, {0, 0}, {0, 0}, {1, 0}, {1, 0}, {0, 0}, {0, 0}, {1, 0}, {1, 0}, {0, 0}, {0, 0}}; } } ././@LongLink100644 0 0 151 12126630646 10256 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/ode/sampling/StepInterpolatorTestUtils.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/ode/sampling/StepInterpolatorTestUtils.100644 1750 1750 10275 12126627673 32371 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.ode.sampling; import org.apache.commons.math3.exception.DimensionMismatchException; import org.apache.commons.math3.exception.MaxCountExceededException; import org.apache.commons.math3.exception.NoBracketingException; import org.apache.commons.math3.exception.NumberIsTooSmallException; import org.apache.commons.math3.ode.FirstOrderIntegrator; import org.apache.commons.math3.ode.TestProblemAbstract; import org.apache.commons.math3.util.FastMath; import org.junit.Assert; public class StepInterpolatorTestUtils { public static void checkDerivativesConsistency(final FirstOrderIntegrator integrator, final TestProblemAbstract problem, final double threshold) throws DimensionMismatchException, NumberIsTooSmallException, MaxCountExceededException, NoBracketingException { integrator.addStepHandler(new StepHandler() { public void handleStep(StepInterpolator interpolator, boolean isLast) throws MaxCountExceededException { final double h = 0.001 * (interpolator.getCurrentTime() - interpolator.getPreviousTime()); final double t = interpolator.getCurrentTime() - 300 * h; if (FastMath.abs(h) < 10 * FastMath.ulp(t)) { return; } interpolator.setInterpolatedTime(t - 4 * h); final double[] yM4h = interpolator.getInterpolatedState().clone(); interpolator.setInterpolatedTime(t - 3 * h); final double[] yM3h = interpolator.getInterpolatedState().clone(); interpolator.setInterpolatedTime(t - 2 * h); final double[] yM2h = interpolator.getInterpolatedState().clone(); interpolator.setInterpolatedTime(t - h); final double[] yM1h = interpolator.getInterpolatedState().clone(); interpolator.setInterpolatedTime(t + h); final double[] yP1h = interpolator.getInterpolatedState().clone(); interpolator.setInterpolatedTime(t + 2 * h); final double[] yP2h = interpolator.getInterpolatedState().clone(); interpolator.setInterpolatedTime(t + 3 * h); final double[] yP3h = interpolator.getInterpolatedState().clone(); interpolator.setInterpolatedTime(t + 4 * h); final double[] yP4h = interpolator.getInterpolatedState().clone(); interpolator.setInterpolatedTime(t); final double[] yDot = interpolator.getInterpolatedDerivatives(); for (int i = 0; i < yDot.length; ++i) { final double approYDot = ( -3 * (yP4h[i] - yM4h[i]) + 32 * (yP3h[i] - yM3h[i]) + -168 * (yP2h[i] - yM2h[i]) + 672 * (yP1h[i] - yM1h[i])) / (840 * h); Assert.assertEquals(approYDot, yDot[i], threshold); } } public void init(double t0, double[] y0, double t) { } }); integrator.integrate(problem, problem.getInitialTime(), problem.getInitialState(), problem.getFinalTime(), new double[problem.getDimension()]); } } commons-math3-3.2-src/src/test/java/org/apache/commons/math3/ode/sampling/StepNormalizerTest.java100644 1750 1750 12242 12126627673 31646 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.ode.sampling; import org.apache.commons.math3.exception.DimensionMismatchException; import org.apache.commons.math3.exception.MaxCountExceededException; import org.apache.commons.math3.exception.NoBracketingException; import org.apache.commons.math3.exception.NumberIsTooSmallException; import org.apache.commons.math3.ode.FirstOrderIntegrator; import org.apache.commons.math3.ode.TestProblem3; import org.apache.commons.math3.ode.nonstiff.DormandPrince54Integrator; import org.apache.commons.math3.util.FastMath; import org.junit.After; import org.junit.Assert; import org.junit.Before; import org.junit.Test; public class StepNormalizerTest { public StepNormalizerTest() { pb = null; integ = null; } @Test public void testBoundaries() throws DimensionMismatchException, NumberIsTooSmallException, MaxCountExceededException, NoBracketingException { double range = pb.getFinalTime() - pb.getInitialTime(); setLastSeen(false); integ.addStepHandler(new StepNormalizer(range / 10.0, new FixedStepHandler() { private boolean firstCall = true; public void init(double t0, double[] y0, double t) { } public void handleStep(double t, double[] y, double[] yDot, boolean isLast) { if (firstCall) { checkValue(t, pb.getInitialTime()); firstCall = false; } if (isLast) { setLastSeen(true); checkValue(t, pb.getFinalTime()); } } })); integ.integrate(pb, pb.getInitialTime(), pb.getInitialState(), pb.getFinalTime(), new double[pb.getDimension()]); Assert.assertTrue(lastSeen); } @Test public void testBeforeEnd() throws DimensionMismatchException, NumberIsTooSmallException, MaxCountExceededException, NoBracketingException { final double range = pb.getFinalTime() - pb.getInitialTime(); setLastSeen(false); integ.addStepHandler(new StepNormalizer(range / 10.5, new FixedStepHandler() { public void init(double t0, double[] y0, double t) { } public void handleStep(double t, double[] y, double[] yDot, boolean isLast) { if (isLast) { setLastSeen(true); checkValue(t, pb.getFinalTime() - range / 21.0); } } })); integ.integrate(pb, pb.getInitialTime(), pb.getInitialState(), pb.getFinalTime(), new double[pb.getDimension()]); Assert.assertTrue(lastSeen); } public void checkValue(double value, double reference) { Assert.assertTrue(FastMath.abs(value - reference) < 1.0e-10); } public void setLastSeen(boolean lastSeen) { this.lastSeen = lastSeen; } @Before public void setUp() { pb = new TestProblem3(0.9); double minStep = 0; double maxStep = pb.getFinalTime() - pb.getInitialTime(); integ = new DormandPrince54Integrator(minStep, maxStep, 10.e-8, 1.0e-8); lastSeen = false; } @After public void tearDown() { pb = null; integ = null; } TestProblem3 pb; FirstOrderIntegrator integ; boolean lastSeen; } ././@LongLink100644 0 0 154 12126630646 10261 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/ode/sampling/StepNormalizerOutputTestBase.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/ode/sampling/StepNormalizerOutputTestBa100644 1750 1750 25560 12126627673 32421 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.ode.sampling; import static org.junit.Assert.assertArrayEquals; import java.util.ArrayList; import java.util.List; import org.apache.commons.math3.exception.DimensionMismatchException; import org.apache.commons.math3.exception.MaxCountExceededException; import org.apache.commons.math3.exception.NoBracketingException; import org.apache.commons.math3.exception.NumberIsTooSmallException; import org.apache.commons.math3.ode.FirstOrderDifferentialEquations; import org.apache.commons.math3.ode.FirstOrderIntegrator; import org.apache.commons.math3.ode.nonstiff.GraggBulirschStoerIntegrator; import org.junit.Test; /** Base class for step normalizer output tests. */ public abstract class StepNormalizerOutputTestBase implements FirstOrderDifferentialEquations, FixedStepHandler { /** The normalized output time values. */ private List output; /** * Returns the start time. * @return the start time */ protected abstract double getStart(); /** * Returns the end time. * @return the end time */ protected abstract double getEnd(); /** * Returns the expected normalized output time values for increment mode. * @return the expected normalized output time values for increment mode */ protected abstract double[] getExpInc(); /** * Returns the expected reversed normalized output time values for * increment mode. * @return the expected reversed normalized output time values for * increment mode */ protected abstract double[] getExpIncRev(); /** * Returns the expected normalized output time values for multiples mode. * @return the expected normalized output time values for multiples mode */ protected abstract double[] getExpMul(); /** * Returns the expected reversed normalized output time values for * multiples mode. * @return the expected reversed normalized output time values for * multiples mode */ protected abstract double[] getExpMulRev(); /** * Returns the offsets for the unit tests below, in the order they are * given below. For each test, the left and right offsets are returned. * @return the offsets for the unit tests below, in the order they are * given below */ protected abstract int[][] getO(); /** * Get the array, given left and right offsets. * @param a the input array * @param offsetL the left side offset * @param offsetR the right side offset * @return the modified array */ private double[] getArray(double[] a, int offsetL, int offsetR) { double[] copy = new double[a.length - offsetR - offsetL]; System.arraycopy(a, offsetL, copy, 0, copy.length); return copy; } @Test public void testIncNeither() throws DimensionMismatchException, NumberIsTooSmallException, MaxCountExceededException, NoBracketingException { double[] exp = getArray(getExpInc(), getO()[0][0], getO()[0][1]); doTest(StepNormalizerMode.INCREMENT, StepNormalizerBounds.NEITHER, exp, false); } @Test public void testIncNeitherRev() throws DimensionMismatchException, NumberIsTooSmallException, MaxCountExceededException, NoBracketingException { double[] exp = getArray(getExpIncRev(), getO()[1][0], getO()[1][1]); doTest(StepNormalizerMode.INCREMENT, StepNormalizerBounds.NEITHER, exp, true); } @Test public void testIncFirst() throws DimensionMismatchException, NumberIsTooSmallException, MaxCountExceededException, NoBracketingException { double[] exp = getArray(getExpInc(), getO()[2][0], getO()[2][1]); doTest(StepNormalizerMode.INCREMENT, StepNormalizerBounds.FIRST, exp, false); } @Test public void testIncFirstRev() throws DimensionMismatchException, NumberIsTooSmallException, MaxCountExceededException, NoBracketingException { double[] exp = getArray(getExpIncRev(), getO()[3][0], getO()[3][1]); doTest(StepNormalizerMode.INCREMENT, StepNormalizerBounds.FIRST, exp, true); } @Test public void testIncLast() throws DimensionMismatchException, NumberIsTooSmallException, MaxCountExceededException, NoBracketingException { double[] exp = getArray(getExpInc(), getO()[4][0], getO()[4][1]); doTest(StepNormalizerMode.INCREMENT, StepNormalizerBounds.LAST, exp, false); } @Test public void testIncLastRev() throws DimensionMismatchException, NumberIsTooSmallException, MaxCountExceededException, NoBracketingException { double[] exp = getArray(getExpIncRev(), getO()[5][0], getO()[5][1]); doTest(StepNormalizerMode.INCREMENT, StepNormalizerBounds.LAST, exp, true); } @Test public void testIncBoth() throws DimensionMismatchException, NumberIsTooSmallException, MaxCountExceededException, NoBracketingException { double[] exp = getArray(getExpInc(), getO()[6][0], getO()[6][1]); doTest(StepNormalizerMode.INCREMENT, StepNormalizerBounds.BOTH, exp, false); } @Test public void testIncBothRev() throws DimensionMismatchException, NumberIsTooSmallException, MaxCountExceededException, NoBracketingException { double[] exp = getArray(getExpIncRev(), getO()[7][0], getO()[7][1]); doTest(StepNormalizerMode.INCREMENT, StepNormalizerBounds.BOTH, exp, true); } @Test public void testMulNeither() throws DimensionMismatchException, NumberIsTooSmallException, MaxCountExceededException, NoBracketingException { double[] exp = getArray(getExpMul(), getO()[8][0], getO()[8][1]); doTest(StepNormalizerMode.MULTIPLES, StepNormalizerBounds.NEITHER, exp, false); } @Test public void testMulNeitherRev() throws DimensionMismatchException, NumberIsTooSmallException, MaxCountExceededException, NoBracketingException { double[] exp = getArray(getExpMulRev(), getO()[9][0], getO()[9][1]); doTest(StepNormalizerMode.MULTIPLES, StepNormalizerBounds.NEITHER, exp, true); } @Test public void testMulFirst() throws DimensionMismatchException, NumberIsTooSmallException, MaxCountExceededException, NoBracketingException { double[] exp = getArray(getExpMul(), getO()[10][0], getO()[10][1]); doTest(StepNormalizerMode.MULTIPLES, StepNormalizerBounds.FIRST, exp, false); } @Test public void testMulFirstRev() throws DimensionMismatchException, NumberIsTooSmallException, MaxCountExceededException, NoBracketingException { double[] exp = getArray(getExpMulRev(), getO()[11][0], getO()[11][1]); doTest(StepNormalizerMode.MULTIPLES, StepNormalizerBounds.FIRST, exp, true); } @Test public void testMulLast() throws DimensionMismatchException, NumberIsTooSmallException, MaxCountExceededException, NoBracketingException { double[] exp = getArray(getExpMul(), getO()[12][0], getO()[12][1]); doTest(StepNormalizerMode.MULTIPLES, StepNormalizerBounds.LAST, exp, false); } @Test public void testMulLastRev() throws DimensionMismatchException, NumberIsTooSmallException, MaxCountExceededException, NoBracketingException { double[] exp = getArray(getExpMulRev(), getO()[13][0], getO()[13][1]); doTest(StepNormalizerMode.MULTIPLES, StepNormalizerBounds.LAST, exp, true); } @Test public void testMulBoth() throws DimensionMismatchException, NumberIsTooSmallException, MaxCountExceededException, NoBracketingException { double[] exp = getArray(getExpMul(), getO()[14][0], getO()[14][1]); doTest(StepNormalizerMode.MULTIPLES, StepNormalizerBounds.BOTH, exp, false); } @Test public void testMulBothRev() throws DimensionMismatchException, NumberIsTooSmallException, MaxCountExceededException, NoBracketingException { double[] exp = getArray(getExpMulRev(), getO()[15][0], getO()[15][1]); doTest(StepNormalizerMode.MULTIPLES, StepNormalizerBounds.BOTH, exp, true); } /** * The actual step normalizer output test code, shared by all the unit * tests. * * @param mode the step normalizer mode to use * @param bounds the step normalizer bounds setting to use * @param expected the expected output (normalized time points) * @param reverse whether to reverse the integration direction * @throws NoBracketingException * @throws MaxCountExceededException * @throws NumberIsTooSmallException * @throws DimensionMismatchException */ private void doTest(StepNormalizerMode mode, StepNormalizerBounds bounds, double[] expected, boolean reverse) throws DimensionMismatchException, NumberIsTooSmallException, MaxCountExceededException, NoBracketingException { // Forward test. FirstOrderIntegrator integ = new GraggBulirschStoerIntegrator( 1e-8, 1.0, 1e-5, 1e-5); integ.addStepHandler(new StepNormalizer(0.5, this, mode, bounds)); double[] y = {0.0}; double start = reverse ? getEnd() : getStart(); double end = reverse ? getStart() : getEnd(); output = new ArrayList(); integ.integrate(this, start, y, end, y); double[] actual = new double[output.size()]; for(int i = 0; i < actual.length; i++) { actual[i] = output.get(i); } assertArrayEquals(expected, actual, 1e-5); } /** {@inheritDoc} */ public int getDimension() { return 1; } /** {@inheritDoc} */ public void computeDerivatives(double t, double[] y, double[] yDot) { yDot[0] = y[0]; } /** {@inheritDoc} */ public void init(double t0, double[] y0, double t) { } /** {@inheritDoc} */ public void handleStep(double t, double[] y, double[] yDot, boolean isLast) { output.add(t); } } ././@LongLink100644 0 0 145 12126630646 10261 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/ode/sampling/DummyStepInterpolator.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/ode/sampling/DummyStepInterpolator.java100644 1750 1750 12464 12126627673 32370 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.ode.sampling; import java.io.IOException; import java.io.ObjectInput; import java.io.ObjectOutput; import org.apache.commons.math3.ode.EquationsMapper; /** This class is a step interpolator that does nothing. * *

              This class is used when the {@link StepHandler "step handler"} * set up by the user does not need step interpolation. It does not * recompute the state when {@link AbstractStepInterpolator#setInterpolatedTime * setInterpolatedTime} is called. This implies the interpolated state * is always the state at the end of the current step.

              * * @see StepHandler * * @version $Id: DummyStepInterpolator.java 1302386 2012-03-19 11:59:25Z sebb $ * @since 1.2 */ public class DummyStepInterpolator extends AbstractStepInterpolator { /** Serializable version identifier. */ private static final long serialVersionUID = 1708010296707839488L; /** Current derivative. */ private double[] currentDerivative; /** Simple constructor. * This constructor builds an instance that is not usable yet, the * AbstractStepInterpolator.reinitialize protected method * should be called before using the instance in order to initialize * the internal arrays. This constructor is used only in order to delay * the initialization in some cases. As an example, the {@link * org.apache.commons.math3.ode.nonstiff.EmbeddedRungeKuttaIntegrator} uses * the prototyping design pattern to create the step interpolators by * cloning an uninitialized model and latter initializing the copy. */ public DummyStepInterpolator() { super(); currentDerivative = null; } /** Simple constructor. * @param y reference to the integrator array holding the state at * the end of the step * @param yDot reference to the integrator array holding the state * derivative at some arbitrary point within the step * @param forward integration direction indicator */ public DummyStepInterpolator(final double[] y, final double[] yDot, final boolean forward) { super(y, forward, new EquationsMapper(0, y.length), new EquationsMapper[0]); currentDerivative = yDot; } /** Copy constructor. * @param interpolator interpolator to copy from. The copy is a deep * copy: its arrays are separated from the original arrays of the * instance */ public DummyStepInterpolator(final DummyStepInterpolator interpolator) { super(interpolator); currentDerivative = interpolator.currentDerivative.clone(); } /** Really copy the finalized instance. * @return a copy of the finalized instance */ @Override protected StepInterpolator doCopy() { return new DummyStepInterpolator(this); } /** Compute the state at the interpolated time. * In this class, this method does nothing: the interpolated state * is always the state at the end of the current step. * @param theta normalized interpolation abscissa within the step * (theta is zero at the previous time step and one at the current time step) * @param oneMinusThetaH time gap between the interpolated time and * the current time */ @Override protected void computeInterpolatedStateAndDerivatives(final double theta, final double oneMinusThetaH) { System.arraycopy(currentState, 0, interpolatedState, 0, currentState.length); System.arraycopy(currentDerivative, 0, interpolatedDerivatives, 0, currentDerivative.length); } /** Write the instance to an output channel. * @param out output channel * @exception IOException if the instance cannot be written */ @Override public void writeExternal(final ObjectOutput out) throws IOException { // save the state of the base class writeBaseExternal(out); if (currentDerivative != null) { for (int i = 0; i < currentDerivative.length; ++i) { out.writeDouble(currentDerivative[i]); } } } /** Read the instance from an input channel. * @param in input channel * @exception IOException if the instance cannot be read */ @Override public void readExternal(final ObjectInput in) throws IOException, ClassNotFoundException { // read the base class final double t = readBaseExternal(in); if (currentState == null) { currentDerivative = null; } else { currentDerivative = new double[currentState.length]; for (int i = 0; i < currentDerivative.length; ++i) { currentDerivative[i] = in.readDouble(); } } // we can now set the interpolated time and state setInterpolatedTime(t); } } ././@LongLink100644 0 0 155 12126630646 10262 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/ode/sampling/NordsieckStepInterpolatorTest.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/ode/sampling/NordsieckStepInterpolatorT100644 1750 1750 7611 12126627673 32400 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.ode.sampling; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.util.Random; import org.apache.commons.math3.exception.DimensionMismatchException; import org.apache.commons.math3.exception.MaxCountExceededException; import org.apache.commons.math3.exception.NoBracketingException; import org.apache.commons.math3.exception.NumberIsTooSmallException; import org.apache.commons.math3.ode.ContinuousOutputModel; import org.apache.commons.math3.ode.TestProblem1; import org.apache.commons.math3.ode.TestProblem3; import org.apache.commons.math3.ode.nonstiff.AdamsBashforthIntegrator; import org.junit.Assert; import org.junit.Test; public class NordsieckStepInterpolatorTest { @Test public void derivativesConsistency() throws NumberIsTooSmallException, DimensionMismatchException, MaxCountExceededException, NoBracketingException { TestProblem3 pb = new TestProblem3(); AdamsBashforthIntegrator integ = new AdamsBashforthIntegrator(4, 0.0, 1.0, 1.0e-10, 1.0e-10); StepInterpolatorTestUtils.checkDerivativesConsistency(integ, pb, 5e-9); } @Test public void serialization() throws IOException, ClassNotFoundException, NumberIsTooSmallException, DimensionMismatchException, MaxCountExceededException, NoBracketingException { TestProblem1 pb = new TestProblem1(); AdamsBashforthIntegrator integ = new AdamsBashforthIntegrator(4, 0.0, 1.0, 1.0e-10, 1.0e-10); integ.addStepHandler(new ContinuousOutputModel()); integ.integrate(pb, pb.getInitialTime(), pb.getInitialState(), pb.getFinalTime(), new double[pb.getDimension()]); ByteArrayOutputStream bos = new ByteArrayOutputStream(); ObjectOutputStream oos = new ObjectOutputStream(bos); for (StepHandler handler : integ.getStepHandlers()) { oos.writeObject(handler); } Assert.assertTrue(bos.size () > 25500); Assert.assertTrue(bos.size () < 26500); ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray()); ObjectInputStream ois = new ObjectInputStream(bis); ContinuousOutputModel cm = (ContinuousOutputModel) ois.readObject(); Random random = new Random(347588535632l); double maxError = 0.0; for (int i = 0; i < 1000; ++i) { double r = random.nextDouble(); double time = r * pb.getInitialTime() + (1.0 - r) * pb.getFinalTime(); cm.setInterpolatedTime(time); double[] interpolatedY = cm.getInterpolatedState (); double[] theoreticalY = pb.computeTheoreticalState(time); double dx = interpolatedY[0] - theoreticalY[0]; double dy = interpolatedY[1] - theoreticalY[1]; double error = dx * dx + dy * dy; if (error > maxError) { maxError = error; } } Assert.assertTrue(maxError < 1.0e-6); } } ././@LongLink100644 0 0 151 12126630646 10256 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/ode/sampling/DummyStepInterpolatorTest.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/ode/sampling/DummyStepInterpolatorTest.100644 1750 1750 10742 12126627673 32363 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.ode.sampling; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import org.apache.commons.math3.exception.MaxCountExceededException; import org.apache.commons.math3.util.FastMath; import org.junit.Assert; import org.junit.Test; public class DummyStepInterpolatorTest { @Test public void testNoReset() throws MaxCountExceededException { double[] y = { 0.0, 1.0, -2.0 }; DummyStepInterpolator interpolator = new DummyStepInterpolator(y, new double[y.length], true); interpolator.storeTime(0); interpolator.shift(); interpolator.storeTime(1); double[] result = interpolator.getInterpolatedState(); for (int i = 0; i < result.length; ++i) { Assert.assertTrue(FastMath.abs(result[i] - y[i]) < 1.0e-10); } } @Test public void testFixedState() throws MaxCountExceededException { double[] y = { 1.0, 3.0, -4.0 }; DummyStepInterpolator interpolator = new DummyStepInterpolator(y, new double[y.length], true); interpolator.storeTime(0); interpolator.shift(); interpolator.storeTime(1); interpolator.setInterpolatedTime(0.1); double[] result = interpolator.getInterpolatedState(); for (int i = 0; i < result.length; ++i) { Assert.assertTrue(FastMath.abs(result[i] - y[i]) < 1.0e-10); } interpolator.setInterpolatedTime(0.5); result = interpolator.getInterpolatedState(); for (int i = 0; i < result.length; ++i) { Assert.assertTrue(FastMath.abs(result[i] - y[i]) < 1.0e-10); } } @Test public void testSerialization() throws IOException, ClassNotFoundException, MaxCountExceededException { double[] y = { 0.0, 1.0, -2.0 }; DummyStepInterpolator interpolator = new DummyStepInterpolator(y, new double[y.length], true); interpolator.storeTime(0); interpolator.shift(); interpolator.storeTime(1); ByteArrayOutputStream bos = new ByteArrayOutputStream(); ObjectOutputStream oos = new ObjectOutputStream(bos); oos.writeObject(interpolator); Assert.assertTrue(bos.size () > 300); Assert.assertTrue(bos.size () < 500); ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray()); ObjectInputStream ois = new ObjectInputStream(bis); DummyStepInterpolator dsi = (DummyStepInterpolator) ois.readObject(); dsi.setInterpolatedTime(0.5); double[] result = dsi.getInterpolatedState(); for (int i = 0; i < result.length; ++i) { Assert.assertTrue(FastMath.abs(result[i] - y[i]) < 1.0e-10); } } @Test public void testImpossibleSerialization() throws IOException { double[] y = { 0.0, 1.0, -2.0 }; AbstractStepInterpolator interpolator = new BadStepInterpolator(y, true); interpolator.storeTime(0); interpolator.shift(); interpolator.storeTime(1); ByteArrayOutputStream bos = new ByteArrayOutputStream(); ObjectOutputStream oos = new ObjectOutputStream(bos); try { oos.writeObject(interpolator); Assert.fail("an exception should have been thrown"); } catch (LocalException le) { // expected behavior } } private static class BadStepInterpolator extends DummyStepInterpolator { @SuppressWarnings("unused") public BadStepInterpolator() { } public BadStepInterpolator(double[] y, boolean forward) { super(y, new double[y.length], forward); } @Override protected void doFinalize() { throw new LocalException(); } } private static class LocalException extends RuntimeException { private static final long serialVersionUID = 1L; } } commons-math3-3.2-src/src/test/java/org/apache/commons/math3/ode/TestProblem4.java100644 1750 1750 7404 12126627673 26526 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.ode; import org.apache.commons.math3.ode.events.EventHandler; import org.apache.commons.math3.util.FastMath; /** * This class is used in the junit tests for the ODE integrators. *

              This specific problem is the following differential equation : *

               *    x'' = -x
               * 
              * And when x decreases down to 0, the state should be changed as follows : *
               *   x' -> -x'
               * 
              * The theoretical solution of this problem is x = |sin(t+a)| *

              */ public class TestProblem4 extends TestProblemAbstract { /** Time offset. */ private double a; /** theoretical state */ private double[] y; /** Simple constructor. */ public TestProblem4() { super(); a = 1.2; double[] y0 = { FastMath.sin(a), FastMath.cos(a) }; setInitialConditions(0.0, y0); setFinalConditions(15); double[] errorScale = { 1.0, 0.0 }; setErrorScale(errorScale); y = new double[y0.length]; } /** * Copy constructor. * @param problem problem to copy */ public TestProblem4(TestProblem4 problem) { super(problem); a = problem.a; y = problem.y.clone(); } /** {@inheritDoc} */ @Override public TestProblem4 copy() { return new TestProblem4(this); } @Override public EventHandler[] getEventsHandlers() { return new EventHandler[] { new Bounce(), new Stop() }; } /** * Get the theoretical events times. * @return theoretical events times */ @Override public double[] getTheoreticalEventsTimes() { return new double[] { 1 * FastMath.PI - a, 2 * FastMath.PI - a, 3 * FastMath.PI - a, 4 * FastMath.PI - a, 12.0 }; } @Override public void doComputeDerivatives(double t, double[] y, double[] yDot) { yDot[0] = y[1]; yDot[1] = -y[0]; } @Override public double[] computeTheoreticalState(double t) { double sin = FastMath.sin(t + a); double cos = FastMath.cos(t + a); y[0] = FastMath.abs(sin); y[1] = (sin >= 0) ? cos : -cos; return y; } private static class Bounce implements EventHandler { private int sign; public Bounce() { sign = +1; } public void init(double t0, double[] y0, double t) { } public double g(double t, double[] y) { return sign * y[0]; } public Action eventOccurred(double t, double[] y, boolean increasing) { // this sign change is needed because the state will be reset soon sign = -sign; return Action.RESET_STATE; } public void resetState(double t, double[] y) { y[0] = -y[0]; y[1] = -y[1]; } } private static class Stop implements EventHandler { public Stop() { } public void init(double t0, double[] y0, double t) { } public double g(double t, double[] y) { return t - 12.0; } public Action eventOccurred(double t, double[] y, boolean increasing) { return Action.STOP; } public void resetState(double t, double[] y) { } } } commons-math3-3.2-src/src/test/java/org/apache/commons/math3/ode/FirstOrderConverterTest.java100644 1750 1750 7615 12126627673 31021 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.ode; import org.apache.commons.math3.exception.DimensionMismatchException; import org.apache.commons.math3.exception.MaxCountExceededException; import org.apache.commons.math3.exception.NoBracketingException; import org.apache.commons.math3.exception.NumberIsTooSmallException; import org.apache.commons.math3.ode.nonstiff.ClassicalRungeKuttaIntegrator; import org.apache.commons.math3.util.FastMath; import org.junit.Assert; import org.junit.Test; public class FirstOrderConverterTest { @Test public void testDoubleDimension() { for (int i = 1; i < 10; ++i) { SecondOrderDifferentialEquations eqn2 = new Equations(i, 0.2); FirstOrderConverter eqn1 = new FirstOrderConverter(eqn2); Assert.assertTrue(eqn1.getDimension() == (2 * eqn2.getDimension())); } } @Test public void testDecreasingSteps() throws DimensionMismatchException, NumberIsTooSmallException, MaxCountExceededException, NoBracketingException { double previousError = Double.NaN; for (int i = 0; i < 10; ++i) { double step = FastMath.pow(2.0, -(i + 1)); double error = integrateWithSpecifiedStep(4.0, 0.0, 1.0, step) - FastMath.sin(4.0); if (i > 0) { Assert.assertTrue(FastMath.abs(error) < FastMath.abs(previousError)); } previousError = error; } } @Test public void testSmallStep() throws DimensionMismatchException, NumberIsTooSmallException, MaxCountExceededException, NoBracketingException { double error = integrateWithSpecifiedStep(4.0, 0.0, 1.0, 1.0e-4) - FastMath.sin(4.0); Assert.assertTrue(FastMath.abs(error) < 1.0e-10); } @Test public void testBigStep() throws DimensionMismatchException, NumberIsTooSmallException, MaxCountExceededException, NoBracketingException { double error = integrateWithSpecifiedStep(4.0, 0.0, 1.0, 0.5) - FastMath.sin(4.0); Assert.assertTrue(FastMath.abs(error) > 0.1); } private static class Equations implements SecondOrderDifferentialEquations { private int n; private double omega2; public Equations(int n, double omega) { this.n = n; omega2 = omega * omega; } public int getDimension() { return n; } public void computeSecondDerivatives(double t, double[] y, double[] yDot, double[] yDDot) { for (int i = 0; i < n; ++i) { yDDot[i] = -omega2 * y[i]; } } } private double integrateWithSpecifiedStep(double omega, double t0, double t, double step) throws DimensionMismatchException, NumberIsTooSmallException, MaxCountExceededException, NoBracketingException { double[] y0 = new double[2]; y0[0] = FastMath.sin(omega * t0); y0[1] = omega * FastMath.cos(omega * t0); ClassicalRungeKuttaIntegrator i = new ClassicalRungeKuttaIntegrator(step); double[] y = new double[2]; i.integrate(new FirstOrderConverter(new Equations(1, omega)), t0, y0, t, y); return y[0]; } } commons-math3-3.2-src/src/test/java/org/apache/commons/math3/ode/TestProblem1.java100644 1750 1750 4260 12126627673 26520 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.ode; import org.apache.commons.math3.util.FastMath; /** * This class is used in the junit tests for the ODE integrators. *

              This specific problem is the following differential equation : *

               *    y' = -y
               * 
              * the solution of this equation is a simple exponential function : *
               *   y (t) = y (t0) exp (t0-t)
               * 
              *

              */ public class TestProblem1 extends TestProblemAbstract { /** theoretical state */ private double[] y; /** * Simple constructor. */ public TestProblem1() { super(); double[] y0 = { 1.0, 0.1 }; setInitialConditions(0.0, y0); setFinalConditions(4.0); double[] errorScale = { 1.0, 1.0 }; setErrorScale(errorScale); y = new double[y0.length]; } /** * Copy constructor. * @param problem problem to copy */ public TestProblem1(TestProblem1 problem) { super(problem); y = problem.y.clone(); } /** {@inheritDoc} */ @Override public TestProblem1 copy() { return new TestProblem1(this); } @Override public void doComputeDerivatives(double t, double[] y, double[] yDot) { // compute the derivatives for (int i = 0; i < n; ++i) yDot[i] = -y[i]; } @Override public double[] computeTheoreticalState(double t) { double c = FastMath.exp (t0 - t); for (int i = 0; i < n; ++i) { y[i] = c * y0[i]; } return y; } } commons-math3-3.2-src/src/test/java/org/apache/commons/math3/ode/TestProblemAbstract.java100644 1750 1750 10305 12126627673 30140 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.ode; import org.apache.commons.math3.ode.FirstOrderDifferentialEquations; import org.apache.commons.math3.ode.events.EventHandler; /** * This class is used as the base class of the problems that are * integrated during the junit tests for the ODE integrators. */ public abstract class TestProblemAbstract implements FirstOrderDifferentialEquations { /** Dimension of the problem. */ protected int n; /** Number of functions calls. */ protected int calls; /** Initial time */ protected double t0; /** Initial state */ protected double[] y0; /** Final time */ protected double t1; /** Error scale */ protected double[] errorScale; /** * Simple constructor. */ protected TestProblemAbstract() { n = 0; calls = 0; t0 = 0; y0 = null; t1 = 0; errorScale = null; } /** * Copy constructor. * @param problem problem to copy */ protected TestProblemAbstract(TestProblemAbstract problem) { n = problem.n; calls = problem.calls; t0 = problem.t0; if (problem.y0 == null) { y0 = null; } else { y0 = problem.y0.clone(); } if (problem.errorScale == null) { errorScale = null; } else { errorScale = problem.errorScale.clone(); } t1 = problem.t1; } /** * Copy operation. * @return a copy of the instance */ public abstract TestProblemAbstract copy(); /** * Set the initial conditions * @param t0 initial time * @param y0 initial state vector */ protected void setInitialConditions(double t0, double[] y0) { calls = 0; n = y0.length; this.t0 = t0; this.y0 = y0.clone(); } /** * Set the final conditions. * @param t1 final time */ protected void setFinalConditions(double t1) { this.t1 = t1; } /** * Set the error scale * @param errorScale error scale */ protected void setErrorScale(double[] errorScale) { this.errorScale = errorScale.clone(); } public int getDimension() { return n; } /** * Get the initial time. * @return initial time */ public double getInitialTime() { return t0; } /** * Get the initial state vector. * @return initial state vector */ public double[] getInitialState() { return y0; } /** * Get the final time. * @return final time */ public double getFinalTime() { return t1; } /** * Get the error scale. * @return error scale */ public double[] getErrorScale() { return errorScale; } /** * Get the events handlers. * @return events handlers */ public EventHandler[] getEventsHandlers() { return new EventHandler[0]; } /** * Get the theoretical events times. * @return theoretical events times */ public double[] getTheoreticalEventsTimes() { return new double[0]; } /** * Get the number of calls. * @return nuber of calls */ public int getCalls() { return calls; } public void computeDerivatives(double t, double[] y, double[] yDot) { ++calls; doComputeDerivatives(t, y, yDot); } abstract public void doComputeDerivatives(double t, double[] y, double[] yDot); /** * Compute the theoretical state at the specified time. * @param t time at which the state is required * @return state vector at time t */ abstract public double[] computeTheoreticalState(double t); } commons-math3-3.2-src/src/test/java/org/apache/commons/math3/ode/TestProblem3.java100644 1750 1750 6315 12126627673 26525 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.ode; import org.apache.commons.math3.util.FastMath; /** * This class is used in the junit tests for the ODE integrators. *

              This specific problem is the following differential equation : *

               *    y1'' = -y1/r^3  y1 (0) = 1-e  y1' (0) = 0
               *    y2'' = -y2/r^3  y2 (0) = 0    y2' (0) =sqrt((1+e)/(1-e))
               *    r = sqrt (y1^2 + y2^2), e = 0.9
               * 
              * This is a two-body problem in the plane which can be solved by * Kepler's equation *
               *   y1 (t) = ...
               * 
              *

              */ public class TestProblem3 extends TestProblemAbstract { /** Eccentricity */ double e; /** theoretical state */ private double[] y; /** * Simple constructor. * @param e eccentricity */ public TestProblem3(double e) { super(); this.e = e; double[] y0 = { 1 - e, 0, 0, FastMath.sqrt((1+e)/(1-e)) }; setInitialConditions(0.0, y0); setFinalConditions(20.0); double[] errorScale = { 1.0, 1.0, 1.0, 1.0 }; setErrorScale(errorScale); y = new double[y0.length]; } /** * Simple constructor. */ public TestProblem3() { this(0.1); } /** * Copy constructor. * @param problem problem to copy */ public TestProblem3(TestProblem3 problem) { super(problem); e = problem.e; y = problem.y.clone(); } /** {@inheritDoc} */ @Override public TestProblem3 copy() { return new TestProblem3(this); } @Override public void doComputeDerivatives(double t, double[] y, double[] yDot) { // current radius double r2 = y[0] * y[0] + y[1] * y[1]; double invR3 = 1 / (r2 * FastMath.sqrt(r2)); // compute the derivatives yDot[0] = y[2]; yDot[1] = y[3]; yDot[2] = -invR3 * y[0]; yDot[3] = -invR3 * y[1]; } @Override public double[] computeTheoreticalState(double t) { // solve Kepler's equation double E = t; double d = 0; double corr = 999.0; for (int i = 0; (i < 50) && (FastMath.abs(corr) > 1.0e-12); ++i) { double f2 = e * FastMath.sin(E); double f0 = d - f2; double f1 = 1 - e * FastMath.cos(E); double f12 = f1 + f1; corr = f0 * f12 / (f1 * f12 - f0 * f2); d -= corr; E = t + d; } double cosE = FastMath.cos(E); double sinE = FastMath.sin(E); y[0] = cosE - e; y[1] = FastMath.sqrt(1 - e * e) * sinE; y[2] = -sinE / (1 - e * cosE); y[3] = FastMath.sqrt(1 - e * e) * cosE / (1 - e * cosE); return y; } } commons-math3-3.2-src/src/test/java/org/apache/commons/math3/ode/TestProblem5.java100644 1750 1750 2375 12126627673 26531 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.ode; /** * This class is used in the junit tests for the ODE integrators. *

              This is the same as problem 1 except integration is done * backward in time

              */ public class TestProblem5 extends TestProblem1 { /** * Simple constructor. */ public TestProblem5() { super(); setFinalConditions(2 * t0 - t1); } /** {@inheritDoc} */ @Override public TestProblem5 copy() { return new TestProblem5(); } } ././@LongLink100644 0 0 166 12126630646 10264 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/ode/nonstiff/GraggBulirschStoerStepInterpolatorTest.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/ode/nonstiff/GraggBulirschStoerStepInte100644 1750 1750 14642 12126627673 32346 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.ode.nonstiff; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.util.Random; import org.apache.commons.math3.exception.DimensionMismatchException; import org.apache.commons.math3.exception.MaxCountExceededException; import org.apache.commons.math3.exception.NoBracketingException; import org.apache.commons.math3.exception.NumberIsTooSmallException; import org.apache.commons.math3.ode.ContinuousOutputModel; import org.apache.commons.math3.ode.TestProblem3; import org.apache.commons.math3.ode.sampling.StepHandler; import org.apache.commons.math3.ode.sampling.StepInterpolator; import org.apache.commons.math3.ode.sampling.StepInterpolatorTestUtils; import org.apache.commons.math3.util.FastMath; import org.junit.Assert; import org.junit.Test; public class GraggBulirschStoerStepInterpolatorTest { @Test public void derivativesConsistency() throws DimensionMismatchException, NumberIsTooSmallException, MaxCountExceededException, NoBracketingException { TestProblem3 pb = new TestProblem3(0.9); double minStep = 0; double maxStep = pb.getFinalTime() - pb.getInitialTime(); double absTolerance = 1.0e-8; double relTolerance = 1.0e-8; GraggBulirschStoerIntegrator integ = new GraggBulirschStoerIntegrator(minStep, maxStep, absTolerance, relTolerance); StepInterpolatorTestUtils.checkDerivativesConsistency(integ, pb, 1.0e-8); } @Test public void serialization() throws IOException, ClassNotFoundException, DimensionMismatchException, NumberIsTooSmallException, MaxCountExceededException, NoBracketingException { TestProblem3 pb = new TestProblem3(0.9); double minStep = 0; double maxStep = pb.getFinalTime() - pb.getInitialTime(); double absTolerance = 1.0e-8; double relTolerance = 1.0e-8; GraggBulirschStoerIntegrator integ = new GraggBulirschStoerIntegrator(minStep, maxStep, absTolerance, relTolerance); integ.addStepHandler(new ContinuousOutputModel()); integ.integrate(pb, pb.getInitialTime(), pb.getInitialState(), pb.getFinalTime(), new double[pb.getDimension()]); ByteArrayOutputStream bos = new ByteArrayOutputStream(); ObjectOutputStream oos = new ObjectOutputStream(bos); for (StepHandler handler : integ.getStepHandlers()) { oos.writeObject(handler); } Assert.assertTrue(bos.size () > 35000); Assert.assertTrue(bos.size () < 36000); ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray()); ObjectInputStream ois = new ObjectInputStream(bis); ContinuousOutputModel cm = (ContinuousOutputModel) ois.readObject(); Random random = new Random(347588535632l); double maxError = 0.0; for (int i = 0; i < 1000; ++i) { double r = random.nextDouble(); double time = r * pb.getInitialTime() + (1.0 - r) * pb.getFinalTime(); cm.setInterpolatedTime(time); double[] interpolatedY = cm.getInterpolatedState (); double[] theoreticalY = pb.computeTheoreticalState(time); double dx = interpolatedY[0] - theoreticalY[0]; double dy = interpolatedY[1] - theoreticalY[1]; double error = dx * dx + dy * dy; if (error > maxError) { maxError = error; } } Assert.assertTrue(maxError < 5.0e-10); } @Test public void checklone() throws DimensionMismatchException, NumberIsTooSmallException, MaxCountExceededException, NoBracketingException { TestProblem3 pb = new TestProblem3(0.9); double minStep = 0; double maxStep = pb.getFinalTime() - pb.getInitialTime(); double scalAbsoluteTolerance = 1.0e-8; double scalRelativeTolerance = scalAbsoluteTolerance; GraggBulirschStoerIntegrator integ = new GraggBulirschStoerIntegrator(minStep, maxStep, scalAbsoluteTolerance, scalRelativeTolerance); integ.addStepHandler(new StepHandler() { public void handleStep(StepInterpolator interpolator, boolean isLast) throws MaxCountExceededException { StepInterpolator cloned = interpolator.copy(); double tA = cloned.getPreviousTime(); double tB = cloned.getCurrentTime(); double halfStep = FastMath.abs(tB - tA) / 2; Assert.assertEquals(interpolator.getPreviousTime(), tA, 1.0e-12); Assert.assertEquals(interpolator.getCurrentTime(), tB, 1.0e-12); for (int i = 0; i < 10; ++i) { double t = (i * tB + (9 - i) * tA) / 9; interpolator.setInterpolatedTime(t); Assert.assertTrue(FastMath.abs(cloned.getInterpolatedTime() - t) > (halfStep / 10)); cloned.setInterpolatedTime(t); Assert.assertEquals(t, cloned.getInterpolatedTime(), 1.0e-12); double[] referenceState = interpolator.getInterpolatedState(); double[] cloneState = cloned.getInterpolatedState(); for (int j = 0; j < referenceState.length; ++j) { Assert.assertEquals(referenceState[j], cloneState[j], 1.0e-12); } } } public void init(double t0, double[] y0, double t) { } }); integ.integrate(pb, pb.getInitialTime(), pb.getInitialState(), pb.getFinalTime(), new double[pb.getDimension()]); } } commons-math3-3.2-src/src/test/java/org/apache/commons/math3/ode/nonstiff/GillIntegratorTest.java100644 1750 1750 23023 12126627673 31631 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.ode.nonstiff; import org.apache.commons.math3.exception.DimensionMismatchException; import org.apache.commons.math3.exception.MaxCountExceededException; import org.apache.commons.math3.exception.NoBracketingException; import org.apache.commons.math3.exception.NumberIsTooSmallException; import org.apache.commons.math3.ode.FirstOrderDifferentialEquations; import org.apache.commons.math3.ode.FirstOrderIntegrator; import org.apache.commons.math3.ode.TestProblem1; import org.apache.commons.math3.ode.TestProblem3; import org.apache.commons.math3.ode.TestProblem5; import org.apache.commons.math3.ode.TestProblemAbstract; import org.apache.commons.math3.ode.TestProblemFactory; import org.apache.commons.math3.ode.TestProblemHandler; import org.apache.commons.math3.ode.events.EventHandler; import org.apache.commons.math3.ode.sampling.StepHandler; import org.apache.commons.math3.ode.sampling.StepInterpolator; import org.apache.commons.math3.util.FastMath; import org.junit.Assert; import org.junit.Test; public class GillIntegratorTest { @Test(expected=DimensionMismatchException.class) public void testDimensionCheck() throws DimensionMismatchException, NumberIsTooSmallException, MaxCountExceededException, NoBracketingException { TestProblem1 pb = new TestProblem1(); new GillIntegrator(0.01).integrate(pb, 0.0, new double[pb.getDimension()+10], 1.0, new double[pb.getDimension()+10]); Assert.fail("an exception should have been thrown"); } @Test public void testDecreasingSteps() throws DimensionMismatchException, NumberIsTooSmallException, MaxCountExceededException, NoBracketingException { TestProblemAbstract[] problems = TestProblemFactory.getProblems(); for (int k = 0; k < problems.length; ++k) { double previousValueError = Double.NaN; double previousTimeError = Double.NaN; for (int i = 5; i < 10; ++i) { TestProblemAbstract pb = problems[k].copy(); double step = (pb.getFinalTime() - pb.getInitialTime()) * FastMath.pow(2.0, -i); FirstOrderIntegrator integ = new GillIntegrator(step); TestProblemHandler handler = new TestProblemHandler(pb, integ); integ.addStepHandler(handler); EventHandler[] functions = pb.getEventsHandlers(); for (int l = 0; l < functions.length; ++l) { integ.addEventHandler(functions[l], Double.POSITIVE_INFINITY, 1.0e-6 * step, 1000); } double stopTime = integ.integrate(pb, pb.getInitialTime(), pb.getInitialState(), pb.getFinalTime(), new double[pb.getDimension()]); if (functions.length == 0) { Assert.assertEquals(pb.getFinalTime(), stopTime, 1.0e-10); } double valueError = handler.getMaximalValueError(); if (i > 5) { Assert.assertTrue(valueError < 1.01 * FastMath.abs(previousValueError)); } previousValueError = valueError; double timeError = handler.getMaximalTimeError(); if (i > 5) { Assert.assertTrue(timeError <= FastMath.abs(previousTimeError)); } previousTimeError = timeError; } } } @Test public void testSmallStep() throws DimensionMismatchException, NumberIsTooSmallException, MaxCountExceededException, NoBracketingException { TestProblem1 pb = new TestProblem1(); double step = (pb.getFinalTime() - pb.getInitialTime()) * 0.001; FirstOrderIntegrator integ = new GillIntegrator(step); TestProblemHandler handler = new TestProblemHandler(pb, integ); integ.addStepHandler(handler); integ.integrate(pb, pb.getInitialTime(), pb.getInitialState(), pb.getFinalTime(), new double[pb.getDimension()]); Assert.assertTrue(handler.getLastError() < 2.0e-13); Assert.assertTrue(handler.getMaximalValueError() < 4.0e-12); Assert.assertEquals(0, handler.getMaximalTimeError(), 1.0e-12); Assert.assertEquals("Gill", integ.getName()); } @Test public void testBigStep() throws DimensionMismatchException, NumberIsTooSmallException, MaxCountExceededException, NoBracketingException { TestProblem1 pb = new TestProblem1(); double step = (pb.getFinalTime() - pb.getInitialTime()) * 0.2; FirstOrderIntegrator integ = new GillIntegrator(step); TestProblemHandler handler = new TestProblemHandler(pb, integ); integ.addStepHandler(handler); integ.integrate(pb, pb.getInitialTime(), pb.getInitialState(), pb.getFinalTime(), new double[pb.getDimension()]); Assert.assertTrue(handler.getLastError() > 0.0004); Assert.assertTrue(handler.getMaximalValueError() > 0.005); Assert.assertEquals(0, handler.getMaximalTimeError(), 1.0e-12); } @Test public void testBackward() throws DimensionMismatchException, NumberIsTooSmallException, MaxCountExceededException, NoBracketingException { TestProblem5 pb = new TestProblem5(); double step = FastMath.abs(pb.getFinalTime() - pb.getInitialTime()) * 0.001; FirstOrderIntegrator integ = new GillIntegrator(step); TestProblemHandler handler = new TestProblemHandler(pb, integ); integ.addStepHandler(handler); integ.integrate(pb, pb.getInitialTime(), pb.getInitialState(), pb.getFinalTime(), new double[pb.getDimension()]); Assert.assertTrue(handler.getLastError() < 5.0e-10); Assert.assertTrue(handler.getMaximalValueError() < 7.0e-10); Assert.assertEquals(0, handler.getMaximalTimeError(), 1.0e-12); Assert.assertEquals("Gill", integ.getName()); } @Test public void testKepler() throws DimensionMismatchException, NumberIsTooSmallException, MaxCountExceededException, NoBracketingException { final TestProblem3 pb = new TestProblem3(0.9); double step = (pb.getFinalTime() - pb.getInitialTime()) * 0.0003; FirstOrderIntegrator integ = new GillIntegrator(step); integ.addStepHandler(new KeplerStepHandler(pb)); integ.integrate(pb, pb.getInitialTime(), pb.getInitialState(), pb.getFinalTime(), new double[pb.getDimension()]); } @Test public void testUnstableDerivative() throws DimensionMismatchException, NumberIsTooSmallException, MaxCountExceededException, NoBracketingException { final StepProblem stepProblem = new StepProblem(0.0, 1.0, 2.0); FirstOrderIntegrator integ = new GillIntegrator(0.3); integ.addEventHandler(stepProblem, 1.0, 1.0e-12, 1000); double[] y = { Double.NaN }; integ.integrate(stepProblem, 0.0, new double[] { 0.0 }, 10.0, y); Assert.assertEquals(8.0, y[0], 1.0e-12); } private static class KeplerStepHandler implements StepHandler { public KeplerStepHandler(TestProblem3 pb) { this.pb = pb; } public void init(double t0, double[] y0, double t) { maxError = 0; } public void handleStep(StepInterpolator interpolator, boolean isLast) throws MaxCountExceededException { double[] interpolatedY = interpolator.getInterpolatedState(); double[] theoreticalY = pb.computeTheoreticalState(interpolator.getCurrentTime()); double dx = interpolatedY[0] - theoreticalY[0]; double dy = interpolatedY[1] - theoreticalY[1]; double error = dx * dx + dy * dy; if (error > maxError) { maxError = error; } if (isLast) { // even with more than 1000 evaluations per period, // RK4 is not able to integrate such an eccentric // orbit with a good accuracy Assert.assertTrue(maxError > 0.001); } } private double maxError; private TestProblem3 pb; } @Test public void testStepSize() throws DimensionMismatchException, NumberIsTooSmallException, MaxCountExceededException, NoBracketingException { final double step = 1.23456; FirstOrderIntegrator integ = new GillIntegrator(step); integ.addStepHandler(new StepHandler() { public void handleStep(StepInterpolator interpolator, boolean isLast) { if (! isLast) { Assert.assertEquals(step, interpolator.getCurrentTime() - interpolator.getPreviousTime(), 1.0e-12); } } public void init(double t0, double[] y0, double t) { } }); integ.integrate(new FirstOrderDifferentialEquations() { public void computeDerivatives(double t, double[] y, double[] dot) { dot[0] = 1.0; } public int getDimension() { return 1; } }, 0.0, new double[] { 0.0 }, 5.0, new double[1]); } } commons-math3-3.2-src/src/test/java/org/apache/commons/math3/ode/nonstiff/StepProblem.java100644 1750 1750 3535 12126627673 30265 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.ode.nonstiff; import org.apache.commons.math3.ode.FirstOrderDifferentialEquations; import org.apache.commons.math3.ode.events.EventHandler; public class StepProblem implements FirstOrderDifferentialEquations, EventHandler { public StepProblem(double rateBefore, double rateAfter, double switchTime) { this.rateAfter = rateAfter; this.switchTime = switchTime; setRate(rateBefore); } public void computeDerivatives(double t, double[] y, double[] yDot) { yDot[0] = rate; } public int getDimension() { return 1; } public void setRate(double rate) { this.rate = rate; } public void init(double t0, double[] y0, double t) { } public Action eventOccurred(double t, double[] y, boolean increasing) { setRate(rateAfter); return Action.RESET_DERIVATIVES; } public double g(double t, double[] y) { return t - switchTime; } public void resetState(double t, double[] y) { } private double rate; private double rateAfter; private double switchTime; } ././@LongLink100644 0 0 163 12126630646 10261 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/ode/nonstiff/DormandPrince54StepInterpolatorTest.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/ode/nonstiff/DormandPrince54StepInterpo100644 1750 1750 15337 12126627673 32227 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.ode.nonstiff; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.util.Random; import org.apache.commons.math3.exception.DimensionMismatchException; import org.apache.commons.math3.exception.MaxCountExceededException; import org.apache.commons.math3.exception.NoBracketingException; import org.apache.commons.math3.exception.NumberIsTooSmallException; import org.apache.commons.math3.ode.ContinuousOutputModel; import org.apache.commons.math3.ode.TestProblem3; import org.apache.commons.math3.ode.sampling.StepHandler; import org.apache.commons.math3.ode.sampling.StepInterpolator; import org.apache.commons.math3.ode.sampling.StepInterpolatorTestUtils; import org.apache.commons.math3.util.FastMath; import org.junit.Assert; import org.junit.Test; public class DormandPrince54StepInterpolatorTest { @Test public void derivativesConsistency() throws DimensionMismatchException, NumberIsTooSmallException, MaxCountExceededException, NoBracketingException { TestProblem3 pb = new TestProblem3(0.1); double minStep = 0; double maxStep = pb.getFinalTime() - pb.getInitialTime(); double scalAbsoluteTolerance = 1.0e-8; double scalRelativeTolerance = scalAbsoluteTolerance; DormandPrince54Integrator integ = new DormandPrince54Integrator(minStep, maxStep, scalAbsoluteTolerance, scalRelativeTolerance); StepInterpolatorTestUtils.checkDerivativesConsistency(integ, pb, 1.0e-10); } @Test public void serialization() throws IOException, ClassNotFoundException, DimensionMismatchException, NumberIsTooSmallException, MaxCountExceededException, NoBracketingException { TestProblem3 pb = new TestProblem3(0.9); double minStep = 0; double maxStep = pb.getFinalTime() - pb.getInitialTime(); double scalAbsoluteTolerance = 1.0e-8; double scalRelativeTolerance = scalAbsoluteTolerance; DormandPrince54Integrator integ = new DormandPrince54Integrator(minStep, maxStep, scalAbsoluteTolerance, scalRelativeTolerance); integ.addStepHandler(new ContinuousOutputModel()); integ.integrate(pb, pb.getInitialTime(), pb.getInitialState(), pb.getFinalTime(), new double[pb.getDimension()]); ByteArrayOutputStream bos = new ByteArrayOutputStream(); ObjectOutputStream oos = new ObjectOutputStream(bos); for (StepHandler handler : integ.getStepHandlers()) { oos.writeObject(handler); } Assert.assertTrue(bos.size () > 135000); Assert.assertTrue(bos.size () < 145000); ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray()); ObjectInputStream ois = new ObjectInputStream(bis); ContinuousOutputModel cm = (ContinuousOutputModel) ois.readObject(); Random random = new Random(347588535632l); double maxError = 0.0; for (int i = 0; i < 1000; ++i) { double r = random.nextDouble(); double time = r * pb.getInitialTime() + (1.0 - r) * pb.getFinalTime(); cm.setInterpolatedTime(time); double[] interpolatedY = cm.getInterpolatedState (); double[] theoreticalY = pb.computeTheoreticalState(time); double dx = interpolatedY[0] - theoreticalY[0]; double dy = interpolatedY[1] - theoreticalY[1]; double error = dx * dx + dy * dy; if (error > maxError) { maxError = error; } } Assert.assertTrue(maxError < 7.0e-10); } @Test public void checkClone() throws DimensionMismatchException, NumberIsTooSmallException, MaxCountExceededException, NoBracketingException { TestProblem3 pb = new TestProblem3(0.9); double minStep = 0; double maxStep = pb.getFinalTime() - pb.getInitialTime(); double scalAbsoluteTolerance = 1.0e-8; double scalRelativeTolerance = scalAbsoluteTolerance; DormandPrince54Integrator integ = new DormandPrince54Integrator(minStep, maxStep, scalAbsoluteTolerance, scalRelativeTolerance); integ.addStepHandler(new StepHandler() { public void handleStep(StepInterpolator interpolator, boolean isLast) throws MaxCountExceededException { StepInterpolator cloned = interpolator.copy(); double tA = cloned.getPreviousTime(); double tB = cloned.getCurrentTime(); double halfStep = FastMath.abs(tB - tA) / 2; Assert.assertEquals(interpolator.getPreviousTime(), tA, 1.0e-12); Assert.assertEquals(interpolator.getCurrentTime(), tB, 1.0e-12); for (int i = 0; i < 10; ++i) { double t = (i * tB + (9 - i) * tA) / 9; interpolator.setInterpolatedTime(t); Assert.assertTrue(FastMath.abs(cloned.getInterpolatedTime() - t) > (halfStep / 10)); cloned.setInterpolatedTime(t); Assert.assertEquals(t, cloned.getInterpolatedTime(), 1.0e-12); double[] referenceState = interpolator.getInterpolatedState(); double[] cloneState = cloned.getInterpolatedState(); for (int j = 0; j < referenceState.length; ++j) { Assert.assertEquals(referenceState[j], cloneState[j], 1.0e-12); } } } public void init(double t0, double[] y0, double t) { } }); integ.integrate(pb, pb.getInitialTime(), pb.getInitialState(), pb.getFinalTime(), new double[pb.getDimension()]); } } ././@LongLink100644 0 0 160 12126630646 10256 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/ode/nonstiff/GraggBulirschStoerIntegratorTest.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/ode/nonstiff/GraggBulirschStoerIntegrat100644 1750 1750 41011 12126627673 32356 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.ode.nonstiff; import org.apache.commons.math3.exception.DimensionMismatchException; import org.apache.commons.math3.exception.MaxCountExceededException; import org.apache.commons.math3.exception.NoBracketingException; import org.apache.commons.math3.exception.NumberIsTooSmallException; import org.apache.commons.math3.ode.FirstOrderDifferentialEquations; import org.apache.commons.math3.ode.FirstOrderIntegrator; import org.apache.commons.math3.ode.TestProblem1; import org.apache.commons.math3.ode.TestProblem3; import org.apache.commons.math3.ode.TestProblem4; import org.apache.commons.math3.ode.TestProblem5; import org.apache.commons.math3.ode.TestProblemAbstract; import org.apache.commons.math3.ode.TestProblemHandler; import org.apache.commons.math3.ode.events.EventHandler; import org.apache.commons.math3.ode.sampling.StepHandler; import org.apache.commons.math3.ode.sampling.StepInterpolator; import org.apache.commons.math3.util.FastMath; import org.junit.Assert; import org.junit.Test; public class GraggBulirschStoerIntegratorTest { @Test(expected=DimensionMismatchException.class) public void testDimensionCheck() throws DimensionMismatchException, NumberIsTooSmallException, MaxCountExceededException, NoBracketingException { TestProblem1 pb = new TestProblem1(); AdaptiveStepsizeIntegrator integrator = new GraggBulirschStoerIntegrator(0.0, 1.0, 1.0e-10, 1.0e-10); integrator.integrate(pb, 0.0, new double[pb.getDimension()+10], 1.0, new double[pb.getDimension()+10]); } @Test(expected=NumberIsTooSmallException.class) public void testNullIntervalCheck() throws DimensionMismatchException, NumberIsTooSmallException, MaxCountExceededException, NoBracketingException { TestProblem1 pb = new TestProblem1(); GraggBulirschStoerIntegrator integrator = new GraggBulirschStoerIntegrator(0.0, 1.0, 1.0e-10, 1.0e-10); integrator.integrate(pb, 0.0, new double[pb.getDimension()], 0.0, new double[pb.getDimension()]); } @Test(expected=NumberIsTooSmallException.class) public void testMinStep() throws DimensionMismatchException, NumberIsTooSmallException, MaxCountExceededException, NoBracketingException { TestProblem5 pb = new TestProblem5(); double minStep = 0.1 * FastMath.abs(pb.getFinalTime() - pb.getInitialTime()); double maxStep = FastMath.abs(pb.getFinalTime() - pb.getInitialTime()); double[] vecAbsoluteTolerance = { 1.0e-20, 1.0e-21 }; double[] vecRelativeTolerance = { 1.0e-20, 1.0e-21 }; FirstOrderIntegrator integ = new GraggBulirschStoerIntegrator(minStep, maxStep, vecAbsoluteTolerance, vecRelativeTolerance); TestProblemHandler handler = new TestProblemHandler(pb, integ); integ.addStepHandler(handler); integ.integrate(pb, pb.getInitialTime(), pb.getInitialState(), pb.getFinalTime(), new double[pb.getDimension()]); } @Test public void testBackward() throws DimensionMismatchException, NumberIsTooSmallException, MaxCountExceededException, NoBracketingException { TestProblem5 pb = new TestProblem5(); double minStep = 0; double maxStep = pb.getFinalTime() - pb.getInitialTime(); double scalAbsoluteTolerance = 1.0e-8; double scalRelativeTolerance = 0.01 * scalAbsoluteTolerance; FirstOrderIntegrator integ = new GraggBulirschStoerIntegrator(minStep, maxStep, scalAbsoluteTolerance, scalRelativeTolerance); TestProblemHandler handler = new TestProblemHandler(pb, integ); integ.addStepHandler(handler); integ.integrate(pb, pb.getInitialTime(), pb.getInitialState(), pb.getFinalTime(), new double[pb.getDimension()]); Assert.assertTrue(handler.getLastError() < 7.5e-9); Assert.assertTrue(handler.getMaximalValueError() < 8.1e-9); Assert.assertEquals(0, handler.getMaximalTimeError(), 1.0e-12); Assert.assertEquals("Gragg-Bulirsch-Stoer", integ.getName()); } @Test public void testIncreasingTolerance() throws DimensionMismatchException, NumberIsTooSmallException, MaxCountExceededException, NoBracketingException { int previousCalls = Integer.MAX_VALUE; for (int i = -12; i < -4; ++i) { TestProblem1 pb = new TestProblem1(); double minStep = 0; double maxStep = pb.getFinalTime() - pb.getInitialTime(); double absTolerance = FastMath.pow(10.0, i); double relTolerance = absTolerance; FirstOrderIntegrator integ = new GraggBulirschStoerIntegrator(minStep, maxStep, absTolerance, relTolerance); TestProblemHandler handler = new TestProblemHandler(pb, integ); integ.addStepHandler(handler); integ.integrate(pb, pb.getInitialTime(), pb.getInitialState(), pb.getFinalTime(), new double[pb.getDimension()]); // the coefficients are only valid for this test // and have been obtained from trial and error // there is no general relation between local and global errors double ratio = handler.getMaximalValueError() / absTolerance; Assert.assertTrue(ratio < 2.4); Assert.assertTrue(ratio > 0.02); Assert.assertEquals(0, handler.getMaximalTimeError(), 1.0e-12); int calls = pb.getCalls(); Assert.assertEquals(integ.getEvaluations(), calls); Assert.assertTrue(calls <= previousCalls); previousCalls = calls; } } @Test public void testIntegratorControls() throws DimensionMismatchException, NumberIsTooSmallException, MaxCountExceededException, NoBracketingException { TestProblem3 pb = new TestProblem3(0.999); GraggBulirschStoerIntegrator integ = new GraggBulirschStoerIntegrator(0, pb.getFinalTime() - pb.getInitialTime(), 1.0e-8, 1.0e-10); double errorWithDefaultSettings = getMaxError(integ, pb); // stability control integ.setStabilityCheck(true, 2, 1, 0.99); Assert.assertTrue(errorWithDefaultSettings < getMaxError(integ, pb)); integ.setStabilityCheck(true, -1, -1, -1); integ.setControlFactors(0.5, 0.99, 0.1, 2.5); Assert.assertTrue(errorWithDefaultSettings < getMaxError(integ, pb)); integ.setControlFactors(-1, -1, -1, -1); integ.setOrderControl(10, 0.7, 0.95); Assert.assertTrue(errorWithDefaultSettings < getMaxError(integ, pb)); integ.setOrderControl(-1, -1, -1); integ.setInterpolationControl(true, 3); Assert.assertTrue(errorWithDefaultSettings < getMaxError(integ, pb)); integ.setInterpolationControl(true, -1); } private double getMaxError(FirstOrderIntegrator integrator, TestProblemAbstract pb) throws DimensionMismatchException, NumberIsTooSmallException, MaxCountExceededException, NoBracketingException { TestProblemHandler handler = new TestProblemHandler(pb, integrator); integrator.addStepHandler(handler); integrator.integrate(pb, pb.getInitialTime(), pb.getInitialState(), pb.getFinalTime(), new double[pb.getDimension()]); return handler.getMaximalValueError(); } @Test public void testEvents() throws DimensionMismatchException, NumberIsTooSmallException, MaxCountExceededException, NoBracketingException { TestProblem4 pb = new TestProblem4(); double minStep = 0; double maxStep = pb.getFinalTime() - pb.getInitialTime(); double scalAbsoluteTolerance = 1.0e-10; double scalRelativeTolerance = 0.01 * scalAbsoluteTolerance; FirstOrderIntegrator integ = new GraggBulirschStoerIntegrator(minStep, maxStep, scalAbsoluteTolerance, scalRelativeTolerance); TestProblemHandler handler = new TestProblemHandler(pb, integ); integ.addStepHandler(handler); EventHandler[] functions = pb.getEventsHandlers(); double convergence = 1.0e-8 * maxStep; for (int l = 0; l < functions.length; ++l) { integ.addEventHandler(functions[l], Double.POSITIVE_INFINITY, convergence, 1000); } Assert.assertEquals(functions.length, integ.getEventHandlers().size()); integ.integrate(pb, pb.getInitialTime(), pb.getInitialState(), pb.getFinalTime(), new double[pb.getDimension()]); Assert.assertTrue(handler.getMaximalValueError() < 4.0e-7); Assert.assertEquals(0, handler.getMaximalTimeError(), convergence); Assert.assertEquals(12.0, handler.getLastTime(), convergence); integ.clearEventHandlers(); Assert.assertEquals(0, integ.getEventHandlers().size()); } @Test public void testKepler() throws DimensionMismatchException, NumberIsTooSmallException, MaxCountExceededException, NoBracketingException { final TestProblem3 pb = new TestProblem3(0.9); double minStep = 0; double maxStep = pb.getFinalTime() - pb.getInitialTime(); double absTolerance = 1.0e-6; double relTolerance = 1.0e-6; FirstOrderIntegrator integ = new GraggBulirschStoerIntegrator(minStep, maxStep, absTolerance, relTolerance); integ.addStepHandler(new KeplerStepHandler(pb)); integ.integrate(pb, pb.getInitialTime(), pb.getInitialState(), pb.getFinalTime(), new double[pb.getDimension()]); Assert.assertEquals(integ.getEvaluations(), pb.getCalls()); Assert.assertTrue(pb.getCalls() < 2150); } @Test public void testVariableSteps() throws DimensionMismatchException, NumberIsTooSmallException, MaxCountExceededException, NoBracketingException { final TestProblem3 pb = new TestProblem3(0.9); double minStep = 0; double maxStep = pb.getFinalTime() - pb.getInitialTime(); double absTolerance = 1.0e-8; double relTolerance = 1.0e-8; FirstOrderIntegrator integ = new GraggBulirschStoerIntegrator(minStep, maxStep, absTolerance, relTolerance); integ.addStepHandler(new VariableStepHandler()); double stopTime = integ.integrate(pb, pb.getInitialTime(), pb.getInitialState(), pb.getFinalTime(), new double[pb.getDimension()]); Assert.assertEquals(pb.getFinalTime(), stopTime, 1.0e-10); Assert.assertEquals("Gragg-Bulirsch-Stoer", integ.getName()); } @Test public void testTooLargeFirstStep() throws DimensionMismatchException, NumberIsTooSmallException, MaxCountExceededException, NoBracketingException { AdaptiveStepsizeIntegrator integ = new GraggBulirschStoerIntegrator(0, Double.POSITIVE_INFINITY, Double.NaN, Double.NaN); final double start = 0.0; final double end = 0.001; FirstOrderDifferentialEquations equations = new FirstOrderDifferentialEquations() { public int getDimension() { return 1; } public void computeDerivatives(double t, double[] y, double[] yDot) { Assert.assertTrue(t >= FastMath.nextAfter(start, Double.NEGATIVE_INFINITY)); Assert.assertTrue(t <= FastMath.nextAfter(end, Double.POSITIVE_INFINITY)); yDot[0] = -100.0 * y[0]; } }; integ.setStepSizeControl(0, 1.0, 1.0e-6, 1.0e-8); integ.integrate(equations, start, new double[] { 1.0 }, end, new double[1]); } @Test public void testUnstableDerivative() throws DimensionMismatchException, NumberIsTooSmallException, MaxCountExceededException, NoBracketingException { final StepProblem stepProblem = new StepProblem(0.0, 1.0, 2.0); FirstOrderIntegrator integ = new GraggBulirschStoerIntegrator(0.1, 10, 1.0e-12, 0.0); integ.addEventHandler(stepProblem, 1.0, 1.0e-12, 1000); double[] y = { Double.NaN }; integ.integrate(stepProblem, 0.0, new double[] { 0.0 }, 10.0, y); Assert.assertEquals(8.0, y[0], 1.0e-12); } @Test public void testIssue596() throws DimensionMismatchException, NumberIsTooSmallException, MaxCountExceededException, NoBracketingException { FirstOrderIntegrator integ = new GraggBulirschStoerIntegrator(1e-10, 100.0, 1e-7, 1e-7); integ.addStepHandler(new StepHandler() { public void init(double t0, double[] y0, double t) { } public void handleStep(StepInterpolator interpolator, boolean isLast) throws MaxCountExceededException { double t = interpolator.getCurrentTime(); interpolator.setInterpolatedTime(t); double[] y = interpolator.getInterpolatedState(); double[] yDot = interpolator.getInterpolatedDerivatives(); Assert.assertEquals(3.0 * t - 5.0, y[0], 1.0e-14); Assert.assertEquals(3.0, yDot[0], 1.0e-14); } }); double[] y = {4.0}; double t0 = 3.0; double tend = 10.0; integ.integrate(new FirstOrderDifferentialEquations() { public int getDimension() { return 1; } public void computeDerivatives(double t, double[] y, double[] yDot) { yDot[0] = 3.0; } }, t0, y, tend, y); } private static class KeplerStepHandler implements StepHandler { public KeplerStepHandler(TestProblem3 pb) { this.pb = pb; } public void init(double t0, double[] y0, double t) { nbSteps = 0; maxError = 0; } public void handleStep(StepInterpolator interpolator, boolean isLast) throws MaxCountExceededException { ++nbSteps; for (int a = 1; a < 100; ++a) { double prev = interpolator.getPreviousTime(); double curr = interpolator.getCurrentTime(); double interp = ((100 - a) * prev + a * curr) / 100; interpolator.setInterpolatedTime(interp); double[] interpolatedY = interpolator.getInterpolatedState (); double[] theoreticalY = pb.computeTheoreticalState(interpolator.getInterpolatedTime()); double dx = interpolatedY[0] - theoreticalY[0]; double dy = interpolatedY[1] - theoreticalY[1]; double error = dx * dx + dy * dy; if (error > maxError) { maxError = error; } } if (isLast) { Assert.assertTrue(maxError < 2.7e-6); Assert.assertTrue(nbSteps < 80); } } private int nbSteps; private double maxError; private TestProblem3 pb; } public static class VariableStepHandler implements StepHandler { public VariableStepHandler() { firstTime = true; minStep = 0; maxStep = 0; } public void init(double t0, double[] y0, double t) { firstTime = true; minStep = 0; maxStep = 0; } public void handleStep(StepInterpolator interpolator, boolean isLast) { double step = FastMath.abs(interpolator.getCurrentTime() - interpolator.getPreviousTime()); if (firstTime) { minStep = FastMath.abs(step); maxStep = minStep; firstTime = false; } else { if (step < minStep) { minStep = step; } if (step > maxStep) { maxStep = step; } } if (isLast) { Assert.assertTrue(minStep < 8.2e-3); Assert.assertTrue(maxStep > 1.5); } } private boolean firstTime; private double minStep; private double maxStep; } } ././@LongLink100644 0 0 164 12126630646 10262 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/ode/nonstiff/DormandPrince853StepInterpolatorTest.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/ode/nonstiff/DormandPrince853StepInterp100644 1750 1750 15247 12126627673 32137 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.ode.nonstiff; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.util.Random; import org.apache.commons.math3.exception.DimensionMismatchException; import org.apache.commons.math3.exception.MaxCountExceededException; import org.apache.commons.math3.exception.NoBracketingException; import org.apache.commons.math3.exception.NumberIsTooSmallException; import org.apache.commons.math3.ode.ContinuousOutputModel; import org.apache.commons.math3.ode.TestProblem3; import org.apache.commons.math3.ode.sampling.StepHandler; import org.apache.commons.math3.ode.sampling.StepInterpolator; import org.apache.commons.math3.ode.sampling.StepInterpolatorTestUtils; import org.apache.commons.math3.util.FastMath; import org.junit.Assert; import org.junit.Test; public class DormandPrince853StepInterpolatorTest { @Test public void derivativesConsistency() throws DimensionMismatchException, NumberIsTooSmallException, MaxCountExceededException, NoBracketingException { TestProblem3 pb = new TestProblem3(0.1); double minStep = 0; double maxStep = pb.getFinalTime() - pb.getInitialTime(); double scalAbsoluteTolerance = 1.0e-8; double scalRelativeTolerance = scalAbsoluteTolerance; DormandPrince853Integrator integ = new DormandPrince853Integrator(minStep, maxStep, scalAbsoluteTolerance, scalRelativeTolerance); StepInterpolatorTestUtils.checkDerivativesConsistency(integ, pb, 1.0e-10); } @Test public void serialization() throws IOException, ClassNotFoundException, DimensionMismatchException, NumberIsTooSmallException, MaxCountExceededException, NoBracketingException { TestProblem3 pb = new TestProblem3(0.9); double minStep = 0; double maxStep = pb.getFinalTime() - pb.getInitialTime(); double scalAbsoluteTolerance = 1.0e-8; double scalRelativeTolerance = scalAbsoluteTolerance; DormandPrince853Integrator integ = new DormandPrince853Integrator(minStep, maxStep, scalAbsoluteTolerance, scalRelativeTolerance); integ.addStepHandler(new ContinuousOutputModel()); integ.integrate(pb, pb.getInitialTime(), pb.getInitialState(), pb.getFinalTime(), new double[pb.getDimension()]); ByteArrayOutputStream bos = new ByteArrayOutputStream(); ObjectOutputStream oos = new ObjectOutputStream(bos); for (StepHandler handler : integ.getStepHandlers()) { oos.writeObject(handler); } Assert.assertTrue(bos.size () > 90000); Assert.assertTrue(bos.size () < 100000); ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray()); ObjectInputStream ois = new ObjectInputStream(bis); ContinuousOutputModel cm = (ContinuousOutputModel) ois.readObject(); Random random = new Random(347588535632l); double maxError = 0.0; for (int i = 0; i < 1000; ++i) { double r = random.nextDouble(); double time = r * pb.getInitialTime() + (1.0 - r) * pb.getFinalTime(); cm.setInterpolatedTime(time); double[] interpolatedY = cm.getInterpolatedState (); double[] theoreticalY = pb.computeTheoreticalState(time); double dx = interpolatedY[0] - theoreticalY[0]; double dy = interpolatedY[1] - theoreticalY[1]; double error = dx * dx + dy * dy; if (error > maxError) { maxError = error; } } Assert.assertTrue(maxError < 2.4e-10); } @Test public void checklone() throws DimensionMismatchException, NumberIsTooSmallException, MaxCountExceededException, NoBracketingException { TestProblem3 pb = new TestProblem3(0.9); double minStep = 0; double maxStep = pb.getFinalTime() - pb.getInitialTime(); double scalAbsoluteTolerance = 1.0e-8; double scalRelativeTolerance = scalAbsoluteTolerance; DormandPrince853Integrator integ = new DormandPrince853Integrator(minStep, maxStep, scalAbsoluteTolerance, scalRelativeTolerance); integ.addStepHandler(new StepHandler() { public void handleStep(StepInterpolator interpolator, boolean isLast) throws MaxCountExceededException { StepInterpolator cloned = interpolator.copy(); double tA = cloned.getPreviousTime(); double tB = cloned.getCurrentTime(); double halfStep = FastMath.abs(tB - tA) / 2; Assert.assertEquals(interpolator.getPreviousTime(), tA, 1.0e-12); Assert.assertEquals(interpolator.getCurrentTime(), tB, 1.0e-12); for (int i = 0; i < 10; ++i) { double t = (i * tB + (9 - i) * tA) / 9; interpolator.setInterpolatedTime(t); Assert.assertTrue(FastMath.abs(cloned.getInterpolatedTime() - t) > (halfStep / 10)); cloned.setInterpolatedTime(t); Assert.assertEquals(t, cloned.getInterpolatedTime(), 1.0e-12); double[] referenceState = interpolator.getInterpolatedState(); double[] cloneState = cloned.getInterpolatedState(); for (int j = 0; j < referenceState.length; ++j) { Assert.assertEquals(referenceState[j], cloneState[j], 1.0e-12); } } } public void init(double t0, double[] y0, double t) { } }); integ.integrate(pb, pb.getInitialTime(), pb.getInitialState(), pb.getFinalTime(), new double[pb.getDimension()]); } } ././@LongLink100644 0 0 161 12126630646 10257 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/ode/nonstiff/ClassicalRungeKuttaIntegratorTest.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/ode/nonstiff/ClassicalRungeKuttaIntegra100644 1750 1750 30245 12126627673 32351 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.ode.nonstiff; import org.apache.commons.math3.exception.DimensionMismatchException; import org.apache.commons.math3.exception.MaxCountExceededException; import org.apache.commons.math3.exception.NoBracketingException; import org.apache.commons.math3.exception.NumberIsTooSmallException; import org.apache.commons.math3.ode.FirstOrderDifferentialEquations; import org.apache.commons.math3.ode.FirstOrderIntegrator; import org.apache.commons.math3.ode.TestProblem1; import org.apache.commons.math3.ode.TestProblem3; import org.apache.commons.math3.ode.TestProblem5; import org.apache.commons.math3.ode.TestProblemAbstract; import org.apache.commons.math3.ode.TestProblemFactory; import org.apache.commons.math3.ode.TestProblemHandler; import org.apache.commons.math3.ode.events.EventHandler; import org.apache.commons.math3.ode.sampling.StepHandler; import org.apache.commons.math3.ode.sampling.StepInterpolator; import org.apache.commons.math3.util.FastMath; import org.junit.Assert; import org.junit.Test; public class ClassicalRungeKuttaIntegratorTest { @Test public void testMissedEndEvent() throws DimensionMismatchException, NumberIsTooSmallException, MaxCountExceededException, NoBracketingException { final double t0 = 1878250320.0000029; final double tEvent = 1878250379.9999986; final double[] k = { 1.0e-4, 1.0e-5, 1.0e-6 }; FirstOrderDifferentialEquations ode = new FirstOrderDifferentialEquations() { public int getDimension() { return k.length; } public void computeDerivatives(double t, double[] y, double[] yDot) { for (int i = 0; i < y.length; ++i) { yDot[i] = k[i] * y[i]; } } }; ClassicalRungeKuttaIntegrator integrator = new ClassicalRungeKuttaIntegrator(60.0); double[] y0 = new double[k.length]; for (int i = 0; i < y0.length; ++i) { y0[i] = i + 1; } double[] y = new double[k.length]; double finalT = integrator.integrate(ode, t0, y0, tEvent, y); Assert.assertEquals(tEvent, finalT, 5.0e-6); for (int i = 0; i < y.length; ++i) { Assert.assertEquals(y0[i] * FastMath.exp(k[i] * (finalT - t0)), y[i], 1.0e-9); } integrator.addEventHandler(new EventHandler() { public void init(double t0, double[] y0, double t) { } public void resetState(double t, double[] y) { } public double g(double t, double[] y) { return t - tEvent; } public Action eventOccurred(double t, double[] y, boolean increasing) { Assert.assertEquals(tEvent, t, 5.0e-6); return Action.CONTINUE; } }, Double.POSITIVE_INFINITY, 1.0e-20, 100); finalT = integrator.integrate(ode, t0, y0, tEvent + 120, y); Assert.assertEquals(tEvent + 120, finalT, 5.0e-6); for (int i = 0; i < y.length; ++i) { Assert.assertEquals(y0[i] * FastMath.exp(k[i] * (finalT - t0)), y[i], 1.0e-9); } } @Test public void testSanityChecks() throws DimensionMismatchException, NumberIsTooSmallException, MaxCountExceededException, NoBracketingException { try { TestProblem1 pb = new TestProblem1(); new ClassicalRungeKuttaIntegrator(0.01).integrate(pb, 0.0, new double[pb.getDimension()+10], 1.0, new double[pb.getDimension()]); Assert.fail("an exception should have been thrown"); } catch(DimensionMismatchException ie) { } try { TestProblem1 pb = new TestProblem1(); new ClassicalRungeKuttaIntegrator(0.01).integrate(pb, 0.0, new double[pb.getDimension()], 1.0, new double[pb.getDimension()+10]); Assert.fail("an exception should have been thrown"); } catch(DimensionMismatchException ie) { } try { TestProblem1 pb = new TestProblem1(); new ClassicalRungeKuttaIntegrator(0.01).integrate(pb, 0.0, new double[pb.getDimension()], 0.0, new double[pb.getDimension()]); Assert.fail("an exception should have been thrown"); } catch(NumberIsTooSmallException ie) { } } @Test public void testDecreasingSteps() throws DimensionMismatchException, NumberIsTooSmallException, MaxCountExceededException, NoBracketingException { TestProblemAbstract[] problems = TestProblemFactory.getProblems(); for (int k = 0; k < problems.length; ++k) { double previousValueError = Double.NaN; double previousTimeError = Double.NaN; for (int i = 4; i < 10; ++i) { TestProblemAbstract pb = problems[k].copy(); double step = (pb.getFinalTime() - pb.getInitialTime()) * FastMath.pow(2.0, -i); FirstOrderIntegrator integ = new ClassicalRungeKuttaIntegrator(step); TestProblemHandler handler = new TestProblemHandler(pb, integ); integ.addStepHandler(handler); EventHandler[] functions = pb.getEventsHandlers(); for (int l = 0; l < functions.length; ++l) { integ.addEventHandler(functions[l], Double.POSITIVE_INFINITY, 1.0e-6 * step, 1000); } Assert.assertEquals(functions.length, integ.getEventHandlers().size()); double stopTime = integ.integrate(pb, pb.getInitialTime(), pb.getInitialState(), pb.getFinalTime(), new double[pb.getDimension()]); if (functions.length == 0) { Assert.assertEquals(pb.getFinalTime(), stopTime, 1.0e-10); } double error = handler.getMaximalValueError(); if (i > 4) { Assert.assertTrue(error < 1.01 * FastMath.abs(previousValueError)); } previousValueError = error; double timeError = handler.getMaximalTimeError(); if (i > 4) { Assert.assertTrue(timeError <= FastMath.abs(previousTimeError)); } previousTimeError = timeError; integ.clearEventHandlers(); Assert.assertEquals(0, integ.getEventHandlers().size()); } } } @Test public void testSmallStep() throws DimensionMismatchException, NumberIsTooSmallException, MaxCountExceededException, NoBracketingException { TestProblem1 pb = new TestProblem1(); double step = (pb.getFinalTime() - pb.getInitialTime()) * 0.001; FirstOrderIntegrator integ = new ClassicalRungeKuttaIntegrator(step); TestProblemHandler handler = new TestProblemHandler(pb, integ); integ.addStepHandler(handler); integ.integrate(pb, pb.getInitialTime(), pb.getInitialState(), pb.getFinalTime(), new double[pb.getDimension()]); Assert.assertTrue(handler.getLastError() < 2.0e-13); Assert.assertTrue(handler.getMaximalValueError() < 4.0e-12); Assert.assertEquals(0, handler.getMaximalTimeError(), 1.0e-12); Assert.assertEquals("classical Runge-Kutta", integ.getName()); } @Test public void testBigStep() throws DimensionMismatchException, NumberIsTooSmallException, MaxCountExceededException, NoBracketingException { TestProblem1 pb = new TestProblem1(); double step = (pb.getFinalTime() - pb.getInitialTime()) * 0.2; FirstOrderIntegrator integ = new ClassicalRungeKuttaIntegrator(step); TestProblemHandler handler = new TestProblemHandler(pb, integ); integ.addStepHandler(handler); integ.integrate(pb, pb.getInitialTime(), pb.getInitialState(), pb.getFinalTime(), new double[pb.getDimension()]); Assert.assertTrue(handler.getLastError() > 0.0004); Assert.assertTrue(handler.getMaximalValueError() > 0.005); Assert.assertEquals(0, handler.getMaximalTimeError(), 1.0e-12); } @Test public void testBackward() throws DimensionMismatchException, NumberIsTooSmallException, MaxCountExceededException, NoBracketingException { TestProblem5 pb = new TestProblem5(); double step = FastMath.abs(pb.getFinalTime() - pb.getInitialTime()) * 0.001; FirstOrderIntegrator integ = new ClassicalRungeKuttaIntegrator(step); TestProblemHandler handler = new TestProblemHandler(pb, integ); integ.addStepHandler(handler); integ.integrate(pb, pb.getInitialTime(), pb.getInitialState(), pb.getFinalTime(), new double[pb.getDimension()]); Assert.assertTrue(handler.getLastError() < 5.0e-10); Assert.assertTrue(handler.getMaximalValueError() < 7.0e-10); Assert.assertEquals(0, handler.getMaximalTimeError(), 1.0e-12); Assert.assertEquals("classical Runge-Kutta", integ.getName()); } @Test public void testKepler() throws DimensionMismatchException, NumberIsTooSmallException, MaxCountExceededException, NoBracketingException { final TestProblem3 pb = new TestProblem3(0.9); double step = (pb.getFinalTime() - pb.getInitialTime()) * 0.0003; FirstOrderIntegrator integ = new ClassicalRungeKuttaIntegrator(step); integ.addStepHandler(new KeplerHandler(pb)); integ.integrate(pb, pb.getInitialTime(), pb.getInitialState(), pb.getFinalTime(), new double[pb.getDimension()]); } private static class KeplerHandler implements StepHandler { public KeplerHandler(TestProblem3 pb) { this.pb = pb; maxError = 0; } public void init(double t0, double[] y0, double t) { maxError = 0; } public void handleStep(StepInterpolator interpolator, boolean isLast) throws MaxCountExceededException { double[] interpolatedY = interpolator.getInterpolatedState (); double[] theoreticalY = pb.computeTheoreticalState(interpolator.getCurrentTime()); double dx = interpolatedY[0] - theoreticalY[0]; double dy = interpolatedY[1] - theoreticalY[1]; double error = dx * dx + dy * dy; if (error > maxError) { maxError = error; } if (isLast) { // even with more than 1000 evaluations per period, // RK4 is not able to integrate such an eccentric // orbit with a good accuracy Assert.assertTrue(maxError > 0.005); } } private double maxError = 0; private TestProblem3 pb; } @Test public void testStepSize() throws DimensionMismatchException, NumberIsTooSmallException, MaxCountExceededException, NoBracketingException { final double step = 1.23456; FirstOrderIntegrator integ = new ClassicalRungeKuttaIntegrator(step); integ.addStepHandler(new StepHandler() { public void handleStep(StepInterpolator interpolator, boolean isLast) { if (! isLast) { Assert.assertEquals(step, interpolator.getCurrentTime() - interpolator.getPreviousTime(), 1.0e-12); } } public void init(double t0, double[] y0, double t) { } }); integ.integrate(new FirstOrderDifferentialEquations() { public void computeDerivatives(double t, double[] y, double[] dot) { dot[0] = 1.0; } public int getDimension() { return 1; } }, 0.0, new double[] { 0.0 }, 5.0, new double[1]); } } ././@LongLink100644 0 0 146 12126630646 10262 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/ode/nonstiff/MidpointIntegratorTest.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/ode/nonstiff/MidpointIntegratorTest.jav100644 1750 1750 17045 12126627673 32373 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.ode.nonstiff; import org.apache.commons.math3.exception.DimensionMismatchException; import org.apache.commons.math3.exception.MaxCountExceededException; import org.apache.commons.math3.exception.NoBracketingException; import org.apache.commons.math3.exception.NumberIsTooSmallException; import org.apache.commons.math3.ode.FirstOrderDifferentialEquations; import org.apache.commons.math3.ode.FirstOrderIntegrator; import org.apache.commons.math3.ode.TestProblem1; import org.apache.commons.math3.ode.TestProblem5; import org.apache.commons.math3.ode.TestProblemAbstract; import org.apache.commons.math3.ode.TestProblemFactory; import org.apache.commons.math3.ode.TestProblemHandler; import org.apache.commons.math3.ode.events.EventHandler; import org.apache.commons.math3.ode.sampling.StepHandler; import org.apache.commons.math3.ode.sampling.StepInterpolator; import org.apache.commons.math3.util.FastMath; import org.junit.Assert; import org.junit.Test; public class MidpointIntegratorTest { @Test(expected=DimensionMismatchException.class) public void testDimensionCheck() throws DimensionMismatchException, NumberIsTooSmallException, MaxCountExceededException, NoBracketingException { TestProblem1 pb = new TestProblem1(); new MidpointIntegrator(0.01).integrate(pb, 0.0, new double[pb.getDimension()+10], 1.0, new double[pb.getDimension()+10]); Assert.fail("an exception should have been thrown"); } @Test public void testDecreasingSteps() throws DimensionMismatchException, NumberIsTooSmallException, MaxCountExceededException, NoBracketingException { TestProblemAbstract[] problems = TestProblemFactory.getProblems(); for (int k = 0; k < problems.length; ++k) { double previousValueError = Double.NaN; double previousTimeError = Double.NaN; for (int i = 4; i < 10; ++i) { TestProblemAbstract pb = problems[k].copy(); double step = (pb.getFinalTime() - pb.getInitialTime()) * FastMath.pow(2.0, -i); FirstOrderIntegrator integ = new MidpointIntegrator(step); TestProblemHandler handler = new TestProblemHandler(pb, integ); integ.addStepHandler(handler); EventHandler[] functions = pb.getEventsHandlers(); for (int l = 0; l < functions.length; ++l) { integ.addEventHandler(functions[l], Double.POSITIVE_INFINITY, 1.0e-6 * step, 1000); } double stopTime = integ.integrate(pb, pb.getInitialTime(), pb.getInitialState(), pb.getFinalTime(), new double[pb.getDimension()]); if (functions.length == 0) { Assert.assertEquals(pb.getFinalTime(), stopTime, 1.0e-10); } double valueError = handler.getMaximalValueError(); if (i > 4) { Assert.assertTrue(valueError < FastMath.abs(previousValueError)); } previousValueError = valueError; double timeError = handler.getMaximalTimeError(); if (i > 4) { Assert.assertTrue(timeError <= FastMath.abs(previousTimeError)); } previousTimeError = timeError; } } } @Test public void testSmallStep() throws DimensionMismatchException, NumberIsTooSmallException, MaxCountExceededException, NoBracketingException { TestProblem1 pb = new TestProblem1(); double step = (pb.getFinalTime() - pb.getInitialTime()) * 0.001; FirstOrderIntegrator integ = new MidpointIntegrator(step); TestProblemHandler handler = new TestProblemHandler(pb, integ); integ.addStepHandler(handler); integ.integrate(pb, pb.getInitialTime(), pb.getInitialState(), pb.getFinalTime(), new double[pb.getDimension()]); Assert.assertTrue(handler.getLastError() < 2.0e-7); Assert.assertTrue(handler.getMaximalValueError() < 1.0e-6); Assert.assertEquals(0, handler.getMaximalTimeError(), 1.0e-12); Assert.assertEquals("midpoint", integ.getName()); } @Test public void testBigStep() throws DimensionMismatchException, NumberIsTooSmallException, MaxCountExceededException, NoBracketingException { TestProblem1 pb = new TestProblem1(); double step = (pb.getFinalTime() - pb.getInitialTime()) * 0.2; FirstOrderIntegrator integ = new MidpointIntegrator(step); TestProblemHandler handler = new TestProblemHandler(pb, integ); integ.addStepHandler(handler); integ.integrate(pb, pb.getInitialTime(), pb.getInitialState(), pb.getFinalTime(), new double[pb.getDimension()]); Assert.assertTrue(handler.getLastError() > 0.01); Assert.assertTrue(handler.getMaximalValueError() > 0.05); Assert.assertEquals(0, handler.getMaximalTimeError(), 1.0e-12); } @Test public void testBackward() throws DimensionMismatchException, NumberIsTooSmallException, MaxCountExceededException, NoBracketingException { TestProblem5 pb = new TestProblem5(); double step = FastMath.abs(pb.getFinalTime() - pb.getInitialTime()) * 0.001; FirstOrderIntegrator integ = new MidpointIntegrator(step); TestProblemHandler handler = new TestProblemHandler(pb, integ); integ.addStepHandler(handler); integ.integrate(pb, pb.getInitialTime(), pb.getInitialState(), pb.getFinalTime(), new double[pb.getDimension()]); Assert.assertTrue(handler.getLastError() < 6.0e-4); Assert.assertTrue(handler.getMaximalValueError() < 6.0e-4); Assert.assertEquals(0, handler.getMaximalTimeError(), 1.0e-12); Assert.assertEquals("midpoint", integ.getName()); } @Test public void testStepSize() throws DimensionMismatchException, NumberIsTooSmallException, MaxCountExceededException, NoBracketingException { final double step = 1.23456; FirstOrderIntegrator integ = new MidpointIntegrator(step); integ.addStepHandler(new StepHandler() { public void handleStep(StepInterpolator interpolator, boolean isLast) { if (! isLast) { Assert.assertEquals(step, interpolator.getCurrentTime() - interpolator.getPreviousTime(), 1.0e-12); } } public void init(double t0, double[] y0, double t) { } }); integ.integrate(new FirstOrderDifferentialEquations() { public void computeDerivatives(double t, double[] y, double[] dot) { dot[0] = 1.0; } public int getDimension() { return 1; } }, 0.0, new double[] { 0.0 }, 5.0, new double[1]); } } ././@LongLink100644 0 0 160 12126630646 10256 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/ode/nonstiff/HighamHall54StepInterpolatorTest.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/ode/nonstiff/HighamHall54StepInterpolat100644 1750 1750 15135 12126627673 32175 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.ode.nonstiff; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.util.Random; import org.apache.commons.math3.exception.DimensionMismatchException; import org.apache.commons.math3.exception.MaxCountExceededException; import org.apache.commons.math3.exception.NoBracketingException; import org.apache.commons.math3.exception.NumberIsTooSmallException; import org.apache.commons.math3.ode.ContinuousOutputModel; import org.apache.commons.math3.ode.TestProblem3; import org.apache.commons.math3.ode.sampling.StepHandler; import org.apache.commons.math3.ode.sampling.StepInterpolator; import org.apache.commons.math3.ode.sampling.StepInterpolatorTestUtils; import org.apache.commons.math3.util.FastMath; import org.junit.Assert; import org.junit.Test; public class HighamHall54StepInterpolatorTest { @Test public void derivativesConsistency() throws DimensionMismatchException, NumberIsTooSmallException, MaxCountExceededException, NoBracketingException { TestProblem3 pb = new TestProblem3(0.1); double minStep = 0; double maxStep = pb.getFinalTime() - pb.getInitialTime(); double scalAbsoluteTolerance = 1.0e-8; double scalRelativeTolerance = scalAbsoluteTolerance; HighamHall54Integrator integ = new HighamHall54Integrator(minStep, maxStep, scalAbsoluteTolerance, scalRelativeTolerance); StepInterpolatorTestUtils.checkDerivativesConsistency(integ, pb, 1.1e-10); } @Test public void serialization() throws IOException, ClassNotFoundException, DimensionMismatchException, NumberIsTooSmallException, MaxCountExceededException, NoBracketingException { TestProblem3 pb = new TestProblem3(0.9); double minStep = 0; double maxStep = pb.getFinalTime() - pb.getInitialTime(); double scalAbsoluteTolerance = 1.0e-8; double scalRelativeTolerance = scalAbsoluteTolerance; HighamHall54Integrator integ = new HighamHall54Integrator(minStep, maxStep, scalAbsoluteTolerance, scalRelativeTolerance); integ.addStepHandler(new ContinuousOutputModel()); integ.integrate(pb, pb.getInitialTime(), pb.getInitialState(), pb.getFinalTime(), new double[pb.getDimension()]); ByteArrayOutputStream bos = new ByteArrayOutputStream(); ObjectOutputStream oos = new ObjectOutputStream(bos); for (StepHandler handler : integ.getStepHandlers()) { oos.writeObject(handler); } Assert.assertTrue(bos.size () > 185000); Assert.assertTrue(bos.size () < 195000); ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray()); ObjectInputStream ois = new ObjectInputStream(bis); ContinuousOutputModel cm = (ContinuousOutputModel) ois.readObject(); Random random = new Random(347588535632l); double maxError = 0.0; for (int i = 0; i < 1000; ++i) { double r = random.nextDouble(); double time = r * pb.getInitialTime() + (1.0 - r) * pb.getFinalTime(); cm.setInterpolatedTime(time); double[] interpolatedY = cm.getInterpolatedState (); double[] theoreticalY = pb.computeTheoreticalState(time); double dx = interpolatedY[0] - theoreticalY[0]; double dy = interpolatedY[1] - theoreticalY[1]; double error = dx * dx + dy * dy; if (error > maxError) { maxError = error; } } Assert.assertTrue(maxError < 1.6e-10); } @Test public void checkClone() throws DimensionMismatchException, NumberIsTooSmallException, MaxCountExceededException, NoBracketingException { TestProblem3 pb = new TestProblem3(0.9); double minStep = 0; double maxStep = pb.getFinalTime() - pb.getInitialTime(); double scalAbsoluteTolerance = 1.0e-8; double scalRelativeTolerance = scalAbsoluteTolerance; HighamHall54Integrator integ = new HighamHall54Integrator(minStep, maxStep, scalAbsoluteTolerance, scalRelativeTolerance); integ.addStepHandler(new StepHandler() { public void handleStep(StepInterpolator interpolator, boolean isLast) throws MaxCountExceededException { StepInterpolator cloned = interpolator.copy(); double tA = cloned.getPreviousTime(); double tB = cloned.getCurrentTime(); double halfStep = FastMath.abs(tB - tA) / 2; Assert.assertEquals(interpolator.getPreviousTime(), tA, 1.0e-12); Assert.assertEquals(interpolator.getCurrentTime(), tB, 1.0e-12); for (int i = 0; i < 10; ++i) { double t = (i * tB + (9 - i) * tA) / 9; interpolator.setInterpolatedTime(t); Assert.assertTrue(FastMath.abs(cloned.getInterpolatedTime() - t) > (halfStep / 10)); cloned.setInterpolatedTime(t); Assert.assertEquals(t, cloned.getInterpolatedTime(), 1.0e-12); double[] referenceState = interpolator.getInterpolatedState(); double[] cloneState = cloned.getInterpolatedState(); for (int j = 0; j < referenceState.length; ++j) { Assert.assertEquals(referenceState[j], cloneState[j], 1.0e-12); } } } public void init(double t0, double[] y0, double t) { } }); integ.integrate(pb, pb.getInitialTime(), pb.getInitialState(), pb.getFinalTime(), new double[pb.getDimension()]); } } ././@LongLink100644 0 0 155 12126630646 10262 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/ode/nonstiff/DormandPrince54IntegratorTest.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/ode/nonstiff/DormandPrince54IntegratorT100644 1750 1750 33234 12126627673 32211 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.ode.nonstiff; import org.apache.commons.math3.exception.DimensionMismatchException; import org.apache.commons.math3.exception.MaxCountExceededException; import org.apache.commons.math3.exception.NoBracketingException; import org.apache.commons.math3.exception.NumberIsTooSmallException; import org.apache.commons.math3.ode.FirstOrderIntegrator; import org.apache.commons.math3.ode.TestProblem1; import org.apache.commons.math3.ode.TestProblem3; import org.apache.commons.math3.ode.TestProblem4; import org.apache.commons.math3.ode.TestProblem5; import org.apache.commons.math3.ode.TestProblemAbstract; import org.apache.commons.math3.ode.TestProblemHandler; import org.apache.commons.math3.ode.events.EventHandler; import org.apache.commons.math3.ode.sampling.StepHandler; import org.apache.commons.math3.ode.sampling.StepInterpolator; import org.apache.commons.math3.util.FastMath; import org.junit.Assert; import org.junit.Test; public class DormandPrince54IntegratorTest { @Test(expected=DimensionMismatchException.class) public void testDimensionCheck() throws DimensionMismatchException, NumberIsTooSmallException, MaxCountExceededException, NoBracketingException { TestProblem1 pb = new TestProblem1(); DormandPrince54Integrator integrator = new DormandPrince54Integrator(0.0, 1.0, 1.0e-10, 1.0e-10); integrator.integrate(pb, 0.0, new double[pb.getDimension()+10], 1.0, new double[pb.getDimension()+10]); } @Test(expected=NumberIsTooSmallException.class) public void testMinStep() throws DimensionMismatchException, NumberIsTooSmallException, MaxCountExceededException, NoBracketingException { TestProblem1 pb = new TestProblem1(); double minStep = 0.1 * (pb.getFinalTime() - pb.getInitialTime()); double maxStep = pb.getFinalTime() - pb.getInitialTime(); double[] vecAbsoluteTolerance = { 1.0e-15, 1.0e-16 }; double[] vecRelativeTolerance = { 1.0e-15, 1.0e-16 }; FirstOrderIntegrator integ = new DormandPrince54Integrator(minStep, maxStep, vecAbsoluteTolerance, vecRelativeTolerance); TestProblemHandler handler = new TestProblemHandler(pb, integ); integ.addStepHandler(handler); integ.integrate(pb, pb.getInitialTime(), pb.getInitialState(), pb.getFinalTime(), new double[pb.getDimension()]); Assert.fail("an exception should have been thrown"); } @Test public void testSmallLastStep() throws DimensionMismatchException, NumberIsTooSmallException, MaxCountExceededException, NoBracketingException { TestProblemAbstract pb = new TestProblem5(); double minStep = 1.25; double maxStep = FastMath.abs(pb.getFinalTime() - pb.getInitialTime()); double scalAbsoluteTolerance = 6.0e-4; double scalRelativeTolerance = 6.0e-4; AdaptiveStepsizeIntegrator integ = new DormandPrince54Integrator(minStep, maxStep, scalAbsoluteTolerance, scalRelativeTolerance); DP54SmallLastHandler handler = new DP54SmallLastHandler(minStep); integ.addStepHandler(handler); integ.setInitialStepSize(1.7); integ.integrate(pb, pb.getInitialTime(), pb.getInitialState(), pb.getFinalTime(), new double[pb.getDimension()]); Assert.assertTrue(handler.wasLastSeen()); Assert.assertEquals("Dormand-Prince 5(4)", integ.getName()); } @Test public void testBackward() throws DimensionMismatchException, NumberIsTooSmallException, MaxCountExceededException, NoBracketingException { TestProblem5 pb = new TestProblem5(); double minStep = 0; double maxStep = pb.getFinalTime() - pb.getInitialTime(); double scalAbsoluteTolerance = 1.0e-8; double scalRelativeTolerance = 0.01 * scalAbsoluteTolerance; FirstOrderIntegrator integ = new DormandPrince54Integrator(minStep, maxStep, scalAbsoluteTolerance, scalRelativeTolerance); TestProblemHandler handler = new TestProblemHandler(pb, integ); integ.addStepHandler(handler); integ.integrate(pb, pb.getInitialTime(), pb.getInitialState(), pb.getFinalTime(), new double[pb.getDimension()]); Assert.assertTrue(handler.getLastError() < 2.0e-7); Assert.assertTrue(handler.getMaximalValueError() < 2.0e-7); Assert.assertEquals(0, handler.getMaximalTimeError(), 1.0e-12); Assert.assertEquals("Dormand-Prince 5(4)", integ.getName()); } private static class DP54SmallLastHandler implements StepHandler { public DP54SmallLastHandler(double minStep) { lastSeen = false; this.minStep = minStep; } public void init(double t0, double[] y0, double t) { } public void handleStep(StepInterpolator interpolator, boolean isLast) { if (isLast) { lastSeen = true; double h = interpolator.getCurrentTime() - interpolator.getPreviousTime(); Assert.assertTrue(FastMath.abs(h) < minStep); } } public boolean wasLastSeen() { return lastSeen; } private boolean lastSeen; private double minStep; } @Test public void testIncreasingTolerance() throws DimensionMismatchException, NumberIsTooSmallException, MaxCountExceededException, NoBracketingException { int previousCalls = Integer.MAX_VALUE; for (int i = -12; i < -2; ++i) { TestProblem1 pb = new TestProblem1(); double minStep = 0; double maxStep = pb.getFinalTime() - pb.getInitialTime(); double scalAbsoluteTolerance = FastMath.pow(10.0, i); double scalRelativeTolerance = 0.01 * scalAbsoluteTolerance; EmbeddedRungeKuttaIntegrator integ = new DormandPrince54Integrator(minStep, maxStep, scalAbsoluteTolerance, scalRelativeTolerance); TestProblemHandler handler = new TestProblemHandler(pb, integ); integ.setSafety(0.8); integ.setMaxGrowth(5.0); integ.setMinReduction(0.3); integ.addStepHandler(handler); integ.integrate(pb, pb.getInitialTime(), pb.getInitialState(), pb.getFinalTime(), new double[pb.getDimension()]); Assert.assertEquals(0.8, integ.getSafety(), 1.0e-12); Assert.assertEquals(5.0, integ.getMaxGrowth(), 1.0e-12); Assert.assertEquals(0.3, integ.getMinReduction(), 1.0e-12); // the 0.7 factor is only valid for this test // and has been obtained from trial and error // there is no general relation between local and global errors Assert.assertTrue(handler.getMaximalValueError() < (0.7 * scalAbsoluteTolerance)); Assert.assertEquals(0, handler.getMaximalTimeError(), 1.0e-12); int calls = pb.getCalls(); Assert.assertEquals(integ.getEvaluations(), calls); Assert.assertTrue(calls <= previousCalls); previousCalls = calls; } } @Test public void testEvents() throws DimensionMismatchException, NumberIsTooSmallException, MaxCountExceededException, NoBracketingException { TestProblem4 pb = new TestProblem4(); double minStep = 0; double maxStep = pb.getFinalTime() - pb.getInitialTime(); double scalAbsoluteTolerance = 1.0e-8; double scalRelativeTolerance = 0.01 * scalAbsoluteTolerance; FirstOrderIntegrator integ = new DormandPrince54Integrator(minStep, maxStep, scalAbsoluteTolerance, scalRelativeTolerance); TestProblemHandler handler = new TestProblemHandler(pb, integ); integ.addStepHandler(handler); EventHandler[] functions = pb.getEventsHandlers(); double convergence = 1.0e-8 * maxStep; for (int l = 0; l < functions.length; ++l) { integ.addEventHandler(functions[l], Double.POSITIVE_INFINITY, convergence, 1000); } Assert.assertEquals(functions.length, integ.getEventHandlers().size()); integ.integrate(pb, pb.getInitialTime(), pb.getInitialState(), pb.getFinalTime(), new double[pb.getDimension()]); Assert.assertTrue(handler.getMaximalValueError() < 5.0e-6); Assert.assertEquals(0, handler.getMaximalTimeError(), convergence); Assert.assertEquals(12.0, handler.getLastTime(), convergence); integ.clearEventHandlers(); Assert.assertEquals(0, integ.getEventHandlers().size()); } @Test public void testKepler() throws DimensionMismatchException, NumberIsTooSmallException, MaxCountExceededException, NoBracketingException { final TestProblem3 pb = new TestProblem3(0.9); double minStep = 0; double maxStep = pb.getFinalTime() - pb.getInitialTime(); double scalAbsoluteTolerance = 1.0e-8; double scalRelativeTolerance = scalAbsoluteTolerance; FirstOrderIntegrator integ = new DormandPrince54Integrator(minStep, maxStep, scalAbsoluteTolerance, scalRelativeTolerance); integ.addStepHandler(new KeplerHandler(pb)); integ.integrate(pb, pb.getInitialTime(), pb.getInitialState(), pb.getFinalTime(), new double[pb.getDimension()]); Assert.assertEquals(integ.getEvaluations(), pb.getCalls()); Assert.assertTrue(pb.getCalls() < 2800); } @Test public void testVariableSteps() throws DimensionMismatchException, NumberIsTooSmallException, MaxCountExceededException, NoBracketingException { final TestProblem3 pb = new TestProblem3(0.9); double minStep = 0; double maxStep = pb.getFinalTime() - pb.getInitialTime(); double scalAbsoluteTolerance = 1.0e-8; double scalRelativeTolerance = scalAbsoluteTolerance; FirstOrderIntegrator integ = new DormandPrince54Integrator(minStep, maxStep, scalAbsoluteTolerance, scalRelativeTolerance); integ.addStepHandler(new VariableHandler()); double stopTime = integ.integrate(pb, pb.getInitialTime(), pb.getInitialState(), pb.getFinalTime(), new double[pb.getDimension()]); Assert.assertEquals(pb.getFinalTime(), stopTime, 1.0e-10); } private static class KeplerHandler implements StepHandler { public KeplerHandler(TestProblem3 pb) { this.pb = pb; } public void init(double t0, double[] y0, double t) { nbSteps = 0; maxError = 0; } public void handleStep(StepInterpolator interpolator, boolean isLast) throws MaxCountExceededException { ++nbSteps; for (int a = 1; a < 10; ++a) { double prev = interpolator.getPreviousTime(); double curr = interpolator.getCurrentTime(); double interp = ((10 - a) * prev + a * curr) / 10; interpolator.setInterpolatedTime(interp); double[] interpolatedY = interpolator.getInterpolatedState (); double[] theoreticalY = pb.computeTheoreticalState(interpolator.getInterpolatedTime()); double dx = interpolatedY[0] - theoreticalY[0]; double dy = interpolatedY[1] - theoreticalY[1]; double error = dx * dx + dy * dy; if (error > maxError) { maxError = error; } } if (isLast) { Assert.assertTrue(maxError < 7.0e-10); Assert.assertTrue(nbSteps < 400); } } private int nbSteps; private double maxError; private TestProblem3 pb; } private static class VariableHandler implements StepHandler { public VariableHandler() { firstTime = true; minStep = 0; maxStep = 0; } public void init(double t0, double[] y0, double t) { firstTime = true; minStep = 0; maxStep = 0; } public void handleStep(StepInterpolator interpolator, boolean isLast) { double step = FastMath.abs(interpolator.getCurrentTime() - interpolator.getPreviousTime()); if (firstTime) { minStep = FastMath.abs(step); maxStep = minStep; firstTime = false; } else { if (step < minStep) { minStep = step; } if (step > maxStep) { maxStep = step; } } if (isLast) { Assert.assertTrue(minStep < (1.0 / 450.0)); Assert.assertTrue(maxStep > (1.0 / 4.2)); } } private boolean firstTime; private double minStep; private double maxStep; } } ././@LongLink100644 0 0 167 12126630646 10265 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/ode/nonstiff/ClassicalRungeKuttaStepInterpolatorTest.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/ode/nonstiff/ClassicalRungeKuttaStepInt100644 1750 1750 7533 12126627673 32332 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.ode.nonstiff; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.util.Random; import org.apache.commons.math3.exception.DimensionMismatchException; import org.apache.commons.math3.exception.MaxCountExceededException; import org.apache.commons.math3.exception.NoBracketingException; import org.apache.commons.math3.exception.NumberIsTooSmallException; import org.apache.commons.math3.ode.ContinuousOutputModel; import org.apache.commons.math3.ode.TestProblem3; import org.apache.commons.math3.ode.sampling.StepHandler; import org.apache.commons.math3.ode.sampling.StepInterpolatorTestUtils; import org.junit.Assert; import org.junit.Test; public class ClassicalRungeKuttaStepInterpolatorTest { @Test public void derivativesConsistency() throws DimensionMismatchException, NumberIsTooSmallException, MaxCountExceededException, NoBracketingException { TestProblem3 pb = new TestProblem3(); double step = (pb.getFinalTime() - pb.getInitialTime()) * 0.001; ClassicalRungeKuttaIntegrator integ = new ClassicalRungeKuttaIntegrator(step); StepInterpolatorTestUtils.checkDerivativesConsistency(integ, pb, 1.0e-10); } @Test public void serialization() throws IOException, ClassNotFoundException, DimensionMismatchException, NumberIsTooSmallException, MaxCountExceededException, NoBracketingException { TestProblem3 pb = new TestProblem3(0.9); double step = (pb.getFinalTime() - pb.getInitialTime()) * 0.0003; ClassicalRungeKuttaIntegrator integ = new ClassicalRungeKuttaIntegrator(step); integ.addStepHandler(new ContinuousOutputModel()); integ.integrate(pb, pb.getInitialTime(), pb.getInitialState(), pb.getFinalTime(), new double[pb.getDimension()]); ByteArrayOutputStream bos = new ByteArrayOutputStream(); ObjectOutputStream oos = new ObjectOutputStream(bos); for (StepHandler handler : integ.getStepHandlers()) { oos.writeObject(handler); } Assert.assertTrue(bos.size () > 880000); Assert.assertTrue(bos.size () < 900000); ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray()); ObjectInputStream ois = new ObjectInputStream(bis); ContinuousOutputModel cm = (ContinuousOutputModel) ois.readObject(); Random random = new Random(347588535632l); double maxError = 0.0; for (int i = 0; i < 1000; ++i) { double r = random.nextDouble(); double time = r * pb.getInitialTime() + (1.0 - r) * pb.getFinalTime(); cm.setInterpolatedTime(time); double[] interpolatedY = cm.getInterpolatedState (); double[] theoreticalY = pb.computeTheoreticalState(time); double dx = interpolatedY[0] - theoreticalY[0]; double dy = interpolatedY[1] - theoreticalY[1]; double error = dx * dx + dy * dy; if (error > maxError) { maxError = error; } } Assert.assertTrue(maxError > 0.005); } } ././@LongLink100644 0 0 150 12126630646 10255 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/ode/nonstiff/GillStepInterpolatorTest.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/ode/nonstiff/GillStepInterpolatorTest.j100644 1750 1750 7423 12126627673 32327 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.ode.nonstiff; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.util.Random; import org.apache.commons.math3.exception.DimensionMismatchException; import org.apache.commons.math3.exception.MaxCountExceededException; import org.apache.commons.math3.exception.NoBracketingException; import org.apache.commons.math3.exception.NumberIsTooSmallException; import org.apache.commons.math3.ode.ContinuousOutputModel; import org.apache.commons.math3.ode.TestProblem3; import org.apache.commons.math3.ode.sampling.StepHandler; import org.apache.commons.math3.ode.sampling.StepInterpolatorTestUtils; import org.junit.Assert; import org.junit.Test; public class GillStepInterpolatorTest { @Test public void testDerivativesConsistency() throws DimensionMismatchException, NumberIsTooSmallException, MaxCountExceededException, NoBracketingException { TestProblem3 pb = new TestProblem3(); double step = (pb.getFinalTime() - pb.getInitialTime()) * 0.001; GillIntegrator integ = new GillIntegrator(step); StepInterpolatorTestUtils.checkDerivativesConsistency(integ, pb, 1.0e-10); } @Test public void serialization() throws IOException, ClassNotFoundException, DimensionMismatchException, NumberIsTooSmallException, MaxCountExceededException, NoBracketingException { TestProblem3 pb = new TestProblem3(0.9); double step = (pb.getFinalTime() - pb.getInitialTime()) * 0.0003; GillIntegrator integ = new GillIntegrator(step); integ.addStepHandler(new ContinuousOutputModel()); integ.integrate(pb, pb.getInitialTime(), pb.getInitialState(), pb.getFinalTime(), new double[pb.getDimension()]); ByteArrayOutputStream bos = new ByteArrayOutputStream(); ObjectOutputStream oos = new ObjectOutputStream(bos); for (StepHandler handler : integ.getStepHandlers()) { oos.writeObject(handler); } Assert.assertTrue(bos.size () > 880000); Assert.assertTrue(bos.size () < 900000); ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray()); ObjectInputStream ois = new ObjectInputStream(bis); ContinuousOutputModel cm = (ContinuousOutputModel) ois.readObject(); Random random = new Random(347588535632l); double maxError = 0.0; for (int i = 0; i < 1000; ++i) { double r = random.nextDouble(); double time = r * pb.getInitialTime() + (1.0 - r) * pb.getFinalTime(); cm.setInterpolatedTime(time); double[] interpolatedY = cm.getInterpolatedState (); double[] theoreticalY = pb.computeTheoreticalState(time); double dx = interpolatedY[0] - theoreticalY[0]; double dy = interpolatedY[1] - theoreticalY[1]; double error = dx * dx + dy * dy; if (error > maxError) { maxError = error; } } Assert.assertTrue(maxError < 0.003); } } ././@LongLink100644 0 0 152 12126630646 10257 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/ode/nonstiff/AdamsMoultonIntegratorTest.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/ode/nonstiff/AdamsMoultonIntegratorTest100644 1750 1750 17021 12126627673 32426 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.ode.nonstiff; import org.apache.commons.math3.exception.DimensionMismatchException; import org.apache.commons.math3.exception.MaxCountExceededException; import org.apache.commons.math3.exception.NoBracketingException; import org.apache.commons.math3.exception.NumberIsTooSmallException; import org.apache.commons.math3.ode.FirstOrderIntegrator; import org.apache.commons.math3.ode.TestProblem1; import org.apache.commons.math3.ode.TestProblem5; import org.apache.commons.math3.ode.TestProblem6; import org.apache.commons.math3.ode.TestProblemHandler; import org.apache.commons.math3.util.FastMath; import org.junit.Assert; import org.junit.Test; public class AdamsMoultonIntegratorTest { @Test(expected=DimensionMismatchException.class) public void dimensionCheck() throws DimensionMismatchException, NumberIsTooSmallException, MaxCountExceededException, NoBracketingException { TestProblem1 pb = new TestProblem1(); FirstOrderIntegrator integ = new AdamsMoultonIntegrator(2, 0.0, 1.0, 1.0e-10, 1.0e-10); integ.integrate(pb, 0.0, new double[pb.getDimension()+10], 1.0, new double[pb.getDimension()+10]); } @Test(expected=NumberIsTooSmallException.class) public void testMinStep() throws DimensionMismatchException, NumberIsTooSmallException, MaxCountExceededException, NoBracketingException { TestProblem1 pb = new TestProblem1(); double minStep = 0.1 * (pb.getFinalTime() - pb.getInitialTime()); double maxStep = pb.getFinalTime() - pb.getInitialTime(); double[] vecAbsoluteTolerance = { 1.0e-15, 1.0e-16 }; double[] vecRelativeTolerance = { 1.0e-15, 1.0e-16 }; FirstOrderIntegrator integ = new AdamsMoultonIntegrator(4, minStep, maxStep, vecAbsoluteTolerance, vecRelativeTolerance); TestProblemHandler handler = new TestProblemHandler(pb, integ); integ.addStepHandler(handler); integ.integrate(pb, pb.getInitialTime(), pb.getInitialState(), pb.getFinalTime(), new double[pb.getDimension()]); } @Test public void testIncreasingTolerance() throws DimensionMismatchException, NumberIsTooSmallException, MaxCountExceededException, NoBracketingException { int previousCalls = Integer.MAX_VALUE; for (int i = -12; i < -2; ++i) { TestProblem1 pb = new TestProblem1(); double minStep = 0; double maxStep = pb.getFinalTime() - pb.getInitialTime(); double scalAbsoluteTolerance = FastMath.pow(10.0, i); double scalRelativeTolerance = 0.01 * scalAbsoluteTolerance; FirstOrderIntegrator integ = new AdamsMoultonIntegrator(4, minStep, maxStep, scalAbsoluteTolerance, scalRelativeTolerance); TestProblemHandler handler = new TestProblemHandler(pb, integ); integ.addStepHandler(handler); integ.integrate(pb, pb.getInitialTime(), pb.getInitialState(), pb.getFinalTime(), new double[pb.getDimension()]); // the 0.5 and 11.0 factors are only valid for this test // and has been obtained from trial and error // there is no general relation between local and global errors Assert.assertTrue(handler.getMaximalValueError() > ( 0.5 * scalAbsoluteTolerance)); Assert.assertTrue(handler.getMaximalValueError() < (11.0 * scalAbsoluteTolerance)); Assert.assertEquals(0, handler.getMaximalTimeError(), 1.0e-16); int calls = pb.getCalls(); Assert.assertEquals(integ.getEvaluations(), calls); Assert.assertTrue(calls <= previousCalls); previousCalls = calls; } } @Test(expected = MaxCountExceededException.class) public void exceedMaxEvaluations() throws DimensionMismatchException, NumberIsTooSmallException, MaxCountExceededException, NoBracketingException { TestProblem1 pb = new TestProblem1(); double range = pb.getFinalTime() - pb.getInitialTime(); AdamsMoultonIntegrator integ = new AdamsMoultonIntegrator(2, 0, range, 1.0e-12, 1.0e-12); TestProblemHandler handler = new TestProblemHandler(pb, integ); integ.addStepHandler(handler); integ.setMaxEvaluations(650); integ.integrate(pb, pb.getInitialTime(), pb.getInitialState(), pb.getFinalTime(), new double[pb.getDimension()]); } @Test public void backward() throws DimensionMismatchException, NumberIsTooSmallException, MaxCountExceededException, NoBracketingException { TestProblem5 pb = new TestProblem5(); double range = FastMath.abs(pb.getFinalTime() - pb.getInitialTime()); FirstOrderIntegrator integ = new AdamsMoultonIntegrator(4, 0, range, 1.0e-12, 1.0e-12); TestProblemHandler handler = new TestProblemHandler(pb, integ); integ.addStepHandler(handler); integ.integrate(pb, pb.getInitialTime(), pb.getInitialState(), pb.getFinalTime(), new double[pb.getDimension()]); Assert.assertTrue(handler.getLastError() < 1.0e-9); Assert.assertTrue(handler.getMaximalValueError() < 1.0e-9); Assert.assertEquals(0, handler.getMaximalTimeError(), 1.0e-16); Assert.assertEquals("Adams-Moulton", integ.getName()); } @Test public void polynomial() throws DimensionMismatchException, NumberIsTooSmallException, MaxCountExceededException, NoBracketingException { TestProblem6 pb = new TestProblem6(); double range = FastMath.abs(pb.getFinalTime() - pb.getInitialTime()); for (int nSteps = 2; nSteps < 8; ++nSteps) { AdamsMoultonIntegrator integ = new AdamsMoultonIntegrator(nSteps, 1.0e-6 * range, 0.1 * range, 1.0e-5, 1.0e-5); TestProblemHandler handler = new TestProblemHandler(pb, integ); integ.addStepHandler(handler); integ.integrate(pb, pb.getInitialTime(), pb.getInitialState(), pb.getFinalTime(), new double[pb.getDimension()]); if (nSteps < 4) { Assert.assertTrue(handler.getMaximalValueError() > 7.0e-04); } else { Assert.assertTrue(handler.getMaximalValueError() < 3.0e-13); } } } } ././@LongLink100644 0 0 154 12126630646 10261 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/ode/nonstiff/AdamsBashforthIntegratorTest.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/ode/nonstiff/AdamsBashforthIntegratorTe100644 1750 1750 16645 12126627673 32355 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.ode.nonstiff; import org.apache.commons.math3.exception.DimensionMismatchException; import org.apache.commons.math3.exception.MaxCountExceededException; import org.apache.commons.math3.exception.NoBracketingException; import org.apache.commons.math3.exception.NumberIsTooSmallException; import org.apache.commons.math3.ode.FirstOrderIntegrator; import org.apache.commons.math3.ode.TestProblem1; import org.apache.commons.math3.ode.TestProblem5; import org.apache.commons.math3.ode.TestProblem6; import org.apache.commons.math3.ode.TestProblemHandler; import org.apache.commons.math3.util.FastMath; import org.junit.Assert; import org.junit.Test; public class AdamsBashforthIntegratorTest { @Test(expected=DimensionMismatchException.class) public void dimensionCheck() throws NumberIsTooSmallException, DimensionMismatchException, MaxCountExceededException, NoBracketingException { TestProblem1 pb = new TestProblem1(); FirstOrderIntegrator integ = new AdamsBashforthIntegrator(2, 0.0, 1.0, 1.0e-10, 1.0e-10); integ.integrate(pb, 0.0, new double[pb.getDimension()+10], 1.0, new double[pb.getDimension()+10]); } @Test(expected=NumberIsTooSmallException.class) public void testMinStep() throws DimensionMismatchException, NumberIsTooSmallException, MaxCountExceededException, NoBracketingException { TestProblem1 pb = new TestProblem1(); double minStep = 0.1 * (pb.getFinalTime() - pb.getInitialTime()); double maxStep = pb.getFinalTime() - pb.getInitialTime(); double[] vecAbsoluteTolerance = { 1.0e-15, 1.0e-16 }; double[] vecRelativeTolerance = { 1.0e-15, 1.0e-16 }; FirstOrderIntegrator integ = new AdamsBashforthIntegrator(4, minStep, maxStep, vecAbsoluteTolerance, vecRelativeTolerance); TestProblemHandler handler = new TestProblemHandler(pb, integ); integ.addStepHandler(handler); integ.integrate(pb, pb.getInitialTime(), pb.getInitialState(), pb.getFinalTime(), new double[pb.getDimension()]); } @Test public void testIncreasingTolerance() throws DimensionMismatchException, NumberIsTooSmallException, MaxCountExceededException, NoBracketingException { int previousCalls = Integer.MAX_VALUE; for (int i = -12; i < -5; ++i) { TestProblem1 pb = new TestProblem1(); double minStep = 0; double maxStep = pb.getFinalTime() - pb.getInitialTime(); double scalAbsoluteTolerance = FastMath.pow(10.0, i); double scalRelativeTolerance = 0.01 * scalAbsoluteTolerance; FirstOrderIntegrator integ = new AdamsBashforthIntegrator(4, minStep, maxStep, scalAbsoluteTolerance, scalRelativeTolerance); TestProblemHandler handler = new TestProblemHandler(pb, integ); integ.addStepHandler(handler); integ.integrate(pb, pb.getInitialTime(), pb.getInitialState(), pb.getFinalTime(), new double[pb.getDimension()]); // the 50 and 300 factors are only valid for this test // and has been obtained from trial and error // there is no general relation between local and global errors Assert.assertTrue(handler.getMaximalValueError() > (50.0 * scalAbsoluteTolerance)); Assert.assertTrue(handler.getMaximalValueError() < (300.0 * scalAbsoluteTolerance)); Assert.assertEquals(0, handler.getMaximalTimeError(), 1.0e-16); int calls = pb.getCalls(); Assert.assertEquals(integ.getEvaluations(), calls); Assert.assertTrue(calls <= previousCalls); previousCalls = calls; } } @Test(expected = MaxCountExceededException.class) public void exceedMaxEvaluations() throws DimensionMismatchException, NumberIsTooSmallException, MaxCountExceededException, NoBracketingException { TestProblem1 pb = new TestProblem1(); double range = pb.getFinalTime() - pb.getInitialTime(); AdamsBashforthIntegrator integ = new AdamsBashforthIntegrator(2, 0, range, 1.0e-12, 1.0e-12); TestProblemHandler handler = new TestProblemHandler(pb, integ); integ.addStepHandler(handler); integ.setMaxEvaluations(650); integ.integrate(pb, pb.getInitialTime(), pb.getInitialState(), pb.getFinalTime(), new double[pb.getDimension()]); } @Test public void backward() throws DimensionMismatchException, NumberIsTooSmallException, MaxCountExceededException, NoBracketingException { TestProblem5 pb = new TestProblem5(); double range = FastMath.abs(pb.getFinalTime() - pb.getInitialTime()); FirstOrderIntegrator integ = new AdamsBashforthIntegrator(4, 0, range, 1.0e-12, 1.0e-12); TestProblemHandler handler = new TestProblemHandler(pb, integ); integ.addStepHandler(handler); integ.integrate(pb, pb.getInitialTime(), pb.getInitialState(), pb.getFinalTime(), new double[pb.getDimension()]); Assert.assertTrue(handler.getLastError() < 1.5e-8); Assert.assertTrue(handler.getMaximalValueError() < 1.5e-8); Assert.assertEquals(0, handler.getMaximalTimeError(), 1.0e-16); Assert.assertEquals("Adams-Bashforth", integ.getName()); } @Test public void polynomial() throws DimensionMismatchException, NumberIsTooSmallException, MaxCountExceededException, NoBracketingException { TestProblem6 pb = new TestProblem6(); double range = FastMath.abs(pb.getFinalTime() - pb.getInitialTime()); for (int nSteps = 2; nSteps < 8; ++nSteps) { AdamsBashforthIntegrator integ = new AdamsBashforthIntegrator(nSteps, 1.0e-6 * range, 0.1 * range, 1.0e-5, 1.0e-5); TestProblemHandler handler = new TestProblemHandler(pb, integ); integ.addStepHandler(handler); integ.integrate(pb, pb.getInitialTime(), pb.getInitialState(), pb.getFinalTime(), new double[pb.getDimension()]); if (nSteps < 4) { Assert.assertTrue(handler.getMaximalValueError() > 1.0e-03); } else { Assert.assertTrue(handler.getMaximalValueError() < 4.0e-12); } } } } commons-math3-3.2-src/src/test/java/org/apache/commons/math3/ode/nonstiff/EulerIntegratorTest.java100644 1750 1750 17060 12126627673 32022 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.ode.nonstiff; import org.apache.commons.math3.exception.DimensionMismatchException; import org.apache.commons.math3.exception.MaxCountExceededException; import org.apache.commons.math3.exception.NoBracketingException; import org.apache.commons.math3.exception.NumberIsTooSmallException; import org.apache.commons.math3.ode.FirstOrderDifferentialEquations; import org.apache.commons.math3.ode.FirstOrderIntegrator; import org.apache.commons.math3.ode.TestProblem1; import org.apache.commons.math3.ode.TestProblem5; import org.apache.commons.math3.ode.TestProblemAbstract; import org.apache.commons.math3.ode.TestProblemFactory; import org.apache.commons.math3.ode.TestProblemHandler; import org.apache.commons.math3.ode.events.EventHandler; import org.apache.commons.math3.ode.sampling.StepHandler; import org.apache.commons.math3.ode.sampling.StepInterpolator; import org.apache.commons.math3.util.FastMath; import org.junit.Assert; import org.junit.Test; public class EulerIntegratorTest { @Test(expected=DimensionMismatchException.class) public void testDimensionCheck() throws DimensionMismatchException, NumberIsTooSmallException, MaxCountExceededException, NoBracketingException { TestProblem1 pb = new TestProblem1(); new EulerIntegrator(0.01).integrate(pb, 0.0, new double[pb.getDimension()+10], 1.0, new double[pb.getDimension()+10]); Assert.fail("an exception should have been thrown"); } @Test public void testDecreasingSteps() throws DimensionMismatchException, NumberIsTooSmallException, MaxCountExceededException, NoBracketingException { TestProblemAbstract[] problems = TestProblemFactory.getProblems(); for (int k = 0; k < problems.length; ++k) { double previousValueError = Double.NaN; double previousTimeError = Double.NaN; for (int i = 4; i < 8; ++i) { TestProblemAbstract pb = problems[k].copy(); double step = (pb.getFinalTime() - pb.getInitialTime()) * FastMath.pow(2.0, -i); FirstOrderIntegrator integ = new EulerIntegrator(step); TestProblemHandler handler = new TestProblemHandler(pb, integ); integ.addStepHandler(handler); EventHandler[] functions = pb.getEventsHandlers(); for (int l = 0; l < functions.length; ++l) { integ.addEventHandler(functions[l], Double.POSITIVE_INFINITY, 1.0e-6 * step, 1000); } double stopTime = integ.integrate(pb, pb.getInitialTime(), pb.getInitialState(), pb.getFinalTime(), new double[pb.getDimension()]); if (functions.length == 0) { Assert.assertEquals(pb.getFinalTime(), stopTime, 1.0e-10); } double valueError = handler.getMaximalValueError(); if (i > 4) { Assert.assertTrue(valueError < FastMath.abs(previousValueError)); } previousValueError = valueError; double timeError = handler.getMaximalTimeError(); if (i > 4) { Assert.assertTrue(timeError <= FastMath.abs(previousTimeError)); } previousTimeError = timeError; } } } @Test public void testSmallStep() throws DimensionMismatchException, NumberIsTooSmallException, MaxCountExceededException, NoBracketingException { TestProblem1 pb = new TestProblem1(); double step = (pb.getFinalTime() - pb.getInitialTime()) * 0.001; FirstOrderIntegrator integ = new EulerIntegrator(step); TestProblemHandler handler = new TestProblemHandler(pb, integ); integ.addStepHandler(handler); integ.integrate(pb, pb.getInitialTime(), pb.getInitialState(), pb.getFinalTime(), new double[pb.getDimension()]); Assert.assertTrue(handler.getLastError() < 2.0e-4); Assert.assertTrue(handler.getMaximalValueError() < 1.0e-3); Assert.assertEquals(0, handler.getMaximalTimeError(), 1.0e-12); Assert.assertEquals("Euler", integ.getName()); } @Test public void testBigStep() throws DimensionMismatchException, NumberIsTooSmallException, MaxCountExceededException, NoBracketingException { TestProblem1 pb = new TestProblem1(); double step = (pb.getFinalTime() - pb.getInitialTime()) * 0.2; FirstOrderIntegrator integ = new EulerIntegrator(step); TestProblemHandler handler = new TestProblemHandler(pb, integ); integ.addStepHandler(handler); integ.integrate(pb, pb.getInitialTime(), pb.getInitialState(), pb.getFinalTime(), new double[pb.getDimension()]); Assert.assertTrue(handler.getLastError() > 0.01); Assert.assertTrue(handler.getMaximalValueError() > 0.2); Assert.assertEquals(0, handler.getMaximalTimeError(), 1.0e-12); } @Test public void testBackward() throws DimensionMismatchException, NumberIsTooSmallException, MaxCountExceededException, NoBracketingException { TestProblem5 pb = new TestProblem5(); double step = FastMath.abs(pb.getFinalTime() - pb.getInitialTime()) * 0.001; FirstOrderIntegrator integ = new EulerIntegrator(step); TestProblemHandler handler = new TestProblemHandler(pb, integ); integ.addStepHandler(handler); integ.integrate(pb, pb.getInitialTime(), pb.getInitialState(), pb.getFinalTime(), new double[pb.getDimension()]); Assert.assertTrue(handler.getLastError() < 0.45); Assert.assertTrue(handler.getMaximalValueError() < 0.45); Assert.assertEquals(0, handler.getMaximalTimeError(), 1.0e-12); Assert.assertEquals("Euler", integ.getName()); } @Test public void testStepSize() throws DimensionMismatchException, NumberIsTooSmallException, MaxCountExceededException, NoBracketingException { final double step = 1.23456; FirstOrderIntegrator integ = new EulerIntegrator(step); integ.addStepHandler(new StepHandler() { public void handleStep(StepInterpolator interpolator, boolean isLast) { if (! isLast) { Assert.assertEquals(step, interpolator.getCurrentTime() - interpolator.getPreviousTime(), 1.0e-12); } } public void init(double t0, double[] y0, double t) { } }); integ.integrate(new FirstOrderDifferentialEquations() { public void computeDerivatives(double t, double[] y, double[] dot) { dot[0] = 1.0; } public int getDimension() { return 1; } }, 0.0, new double[] { 0.0 }, 5.0, new double[1]); } } ././@LongLink100644 0 0 161 12126630646 10257 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/ode/nonstiff/ThreeEighthesStepInterpolatorTest.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/ode/nonstiff/ThreeEighthesStepInterpola100644 1750 1750 7474 12126627673 32361 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.ode.nonstiff; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.util.Random; import org.apache.commons.math3.exception.DimensionMismatchException; import org.apache.commons.math3.exception.MaxCountExceededException; import org.apache.commons.math3.exception.NoBracketingException; import org.apache.commons.math3.exception.NumberIsTooSmallException; import org.apache.commons.math3.ode.ContinuousOutputModel; import org.apache.commons.math3.ode.TestProblem3; import org.apache.commons.math3.ode.sampling.StepHandler; import org.apache.commons.math3.ode.sampling.StepInterpolatorTestUtils; import org.junit.Assert; import org.junit.Test; public class ThreeEighthesStepInterpolatorTest { @Test public void derivativesConsistency() throws DimensionMismatchException, NumberIsTooSmallException, MaxCountExceededException, NoBracketingException { TestProblem3 pb = new TestProblem3(); double step = (pb.getFinalTime() - pb.getInitialTime()) * 0.001; ThreeEighthesIntegrator integ = new ThreeEighthesIntegrator(step); StepInterpolatorTestUtils.checkDerivativesConsistency(integ, pb, 1.0e-10); } @Test public void serialization() throws IOException, ClassNotFoundException, DimensionMismatchException, NumberIsTooSmallException, MaxCountExceededException, NoBracketingException { TestProblem3 pb = new TestProblem3(0.9); double step = (pb.getFinalTime() - pb.getInitialTime()) * 0.0003; ThreeEighthesIntegrator integ = new ThreeEighthesIntegrator(step); integ.addStepHandler(new ContinuousOutputModel()); integ.integrate(pb, pb.getInitialTime(), pb.getInitialState(), pb.getFinalTime(), new double[pb.getDimension()]); ByteArrayOutputStream bos = new ByteArrayOutputStream(); ObjectOutputStream oos = new ObjectOutputStream(bos); for (StepHandler handler : integ.getStepHandlers()) { oos.writeObject(handler); } Assert.assertTrue(bos.size () > 880000); Assert.assertTrue(bos.size () < 900000); ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray()); ObjectInputStream ois = new ObjectInputStream(bis); ContinuousOutputModel cm = (ContinuousOutputModel) ois.readObject(); Random random = new Random(347588535632l); double maxError = 0.0; for (int i = 0; i < 1000; ++i) { double r = random.nextDouble(); double time = r * pb.getInitialTime() + (1.0 - r) * pb.getFinalTime(); cm.setInterpolatedTime(time); double[] interpolatedY = cm.getInterpolatedState (); double[] theoreticalY = pb.computeTheoreticalState(time); double dx = interpolatedY[0] - theoreticalY[0]; double dy = interpolatedY[1] - theoreticalY[1]; double error = dx * dx + dy * dy; if (error > maxError) { maxError = error; } } Assert.assertTrue(maxError > 0.005); } } ././@LongLink100644 0 0 154 12126630646 10261 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/ode/nonstiff/MidpointStepInterpolatorTest.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/ode/nonstiff/MidpointStepInterpolatorTe100644 1750 1750 7526 12126627673 32430 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.ode.nonstiff; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.util.Random; import org.apache.commons.math3.exception.DimensionMismatchException; import org.apache.commons.math3.exception.MaxCountExceededException; import org.apache.commons.math3.exception.NoBracketingException; import org.apache.commons.math3.exception.NumberIsTooSmallException; import org.apache.commons.math3.ode.ContinuousOutputModel; import org.apache.commons.math3.ode.TestProblem1; import org.apache.commons.math3.ode.TestProblem3; import org.apache.commons.math3.ode.sampling.StepHandler; import org.apache.commons.math3.ode.sampling.StepInterpolatorTestUtils; import org.junit.Assert; import org.junit.Test; public class MidpointStepInterpolatorTest { @Test public void testDerivativesConsistency() throws DimensionMismatchException, NumberIsTooSmallException, MaxCountExceededException, NoBracketingException { TestProblem3 pb = new TestProblem3(); double step = (pb.getFinalTime() - pb.getInitialTime()) * 0.001; MidpointIntegrator integ = new MidpointIntegrator(step); StepInterpolatorTestUtils.checkDerivativesConsistency(integ, pb, 1.0e-10); } @Test public void serialization() throws IOException, ClassNotFoundException, DimensionMismatchException, NumberIsTooSmallException, MaxCountExceededException, NoBracketingException { TestProblem1 pb = new TestProblem1(); double step = (pb.getFinalTime() - pb.getInitialTime()) * 0.001; MidpointIntegrator integ = new MidpointIntegrator(step); integ.addStepHandler(new ContinuousOutputModel()); integ.integrate(pb, pb.getInitialTime(), pb.getInitialState(), pb.getFinalTime(), new double[pb.getDimension()]); ByteArrayOutputStream bos = new ByteArrayOutputStream(); ObjectOutputStream oos = new ObjectOutputStream(bos); for (StepHandler handler : integ.getStepHandlers()) { oos.writeObject(handler); } Assert.assertTrue(bos.size () > 135000); Assert.assertTrue(bos.size () < 145000); ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray()); ObjectInputStream ois = new ObjectInputStream(bis); ContinuousOutputModel cm = (ContinuousOutputModel) ois.readObject(); Random random = new Random(347588535632l); double maxError = 0.0; for (int i = 0; i < 1000; ++i) { double r = random.nextDouble(); double time = r * pb.getInitialTime() + (1.0 - r) * pb.getFinalTime(); cm.setInterpolatedTime(time); double[] interpolatedY = cm.getInterpolatedState (); double[] theoreticalY = pb.computeTheoreticalState(time); double dx = interpolatedY[0] - theoreticalY[0]; double dy = interpolatedY[1] - theoreticalY[1]; double error = dx * dx + dy * dy; if (error > maxError) { maxError = error; } } Assert.assertTrue(maxError < 1.0e-6); } } ././@LongLink100644 0 0 151 12126630646 10256 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/ode/nonstiff/EulerStepInterpolatorTest.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/ode/nonstiff/EulerStepInterpolatorTest.100644 1750 1750 16204 12126627673 32357 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.ode.nonstiff; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.util.Random; import org.apache.commons.math3.exception.DimensionMismatchException; import org.apache.commons.math3.exception.MaxCountExceededException; import org.apache.commons.math3.exception.NoBracketingException; import org.apache.commons.math3.exception.NumberIsTooSmallException; import org.apache.commons.math3.ode.ContinuousOutputModel; import org.apache.commons.math3.ode.EquationsMapper; import org.apache.commons.math3.ode.TestProblem1; import org.apache.commons.math3.ode.TestProblem3; import org.apache.commons.math3.ode.sampling.StepHandler; import org.apache.commons.math3.ode.sampling.StepInterpolatorTestUtils; import org.apache.commons.math3.util.FastMath; import org.junit.Assert; import org.junit.Test; public class EulerStepInterpolatorTest { @Test public void noReset() throws MaxCountExceededException { double[] y = { 0.0, 1.0, -2.0 }; double[][] yDot = { { 1.0, 2.0, -2.0 } }; EulerStepInterpolator interpolator = new EulerStepInterpolator(); interpolator.reinitialize(new DummyIntegrator(interpolator), y, yDot, true, new EquationsMapper(0, y.length), new EquationsMapper[0]); interpolator.storeTime(0); interpolator.shift(); interpolator.storeTime(1); double[] result = interpolator.getInterpolatedState(); for (int i = 0; i < result.length; ++i) { Assert.assertTrue(FastMath.abs(result[i] - y[i]) < 1.0e-10); } } @Test public void interpolationAtBounds() throws MaxCountExceededException { double t0 = 0; double[] y0 = {0.0, 1.0, -2.0}; double[] y = y0.clone(); double[][] yDot = { new double[y0.length] }; EulerStepInterpolator interpolator = new EulerStepInterpolator(); interpolator.reinitialize(new DummyIntegrator(interpolator), y, yDot, true, new EquationsMapper(0, y.length), new EquationsMapper[0]); interpolator.storeTime(t0); double dt = 1.0; interpolator.shift(); y[0] = 1.0; y[1] = 3.0; y[2] = -4.0; yDot[0][0] = (y[0] - y0[0]) / dt; yDot[0][1] = (y[1] - y0[1]) / dt; yDot[0][2] = (y[2] - y0[2]) / dt; interpolator.storeTime(t0 + dt); interpolator.setInterpolatedTime(interpolator.getPreviousTime()); double[] result = interpolator.getInterpolatedState(); for (int i = 0; i < result.length; ++i) { Assert.assertTrue(FastMath.abs(result[i] - y0[i]) < 1.0e-10); } interpolator.setInterpolatedTime(interpolator.getCurrentTime()); result = interpolator.getInterpolatedState(); for (int i = 0; i < result.length; ++i) { Assert.assertTrue(FastMath.abs(result[i] - y[i]) < 1.0e-10); } } @Test public void interpolationInside() throws MaxCountExceededException { double[] y = { 0.0, 1.0, -2.0 }; double[][] yDot = { { 1.0, 2.0, -2.0 } }; EulerStepInterpolator interpolator = new EulerStepInterpolator(); interpolator.reinitialize(new DummyIntegrator(interpolator), y, yDot, true, new EquationsMapper(0, y.length), new EquationsMapper[0]); interpolator.storeTime(0); interpolator.shift(); y[0] = 1.0; y[1] = 3.0; y[2] = -4.0; interpolator.storeTime(1); interpolator.setInterpolatedTime(0.1); double[] result = interpolator.getInterpolatedState(); Assert.assertTrue(FastMath.abs(result[0] - 0.1) < 1.0e-10); Assert.assertTrue(FastMath.abs(result[1] - 1.2) < 1.0e-10); Assert.assertTrue(FastMath.abs(result[2] + 2.2) < 1.0e-10); interpolator.setInterpolatedTime(0.5); result = interpolator.getInterpolatedState(); Assert.assertTrue(FastMath.abs(result[0] - 0.5) < 1.0e-10); Assert.assertTrue(FastMath.abs(result[1] - 2.0) < 1.0e-10); Assert.assertTrue(FastMath.abs(result[2] + 3.0) < 1.0e-10); } @Test public void derivativesConsistency() throws DimensionMismatchException, NumberIsTooSmallException, MaxCountExceededException, NoBracketingException { TestProblem3 pb = new TestProblem3(); double step = (pb.getFinalTime() - pb.getInitialTime()) * 0.001; EulerIntegrator integ = new EulerIntegrator(step); StepInterpolatorTestUtils.checkDerivativesConsistency(integ, pb, 1.0e-10); } @Test public void serialization() throws IOException, ClassNotFoundException, DimensionMismatchException, NumberIsTooSmallException, MaxCountExceededException, NoBracketingException { TestProblem1 pb = new TestProblem1(); double step = (pb.getFinalTime() - pb.getInitialTime()) * 0.001; EulerIntegrator integ = new EulerIntegrator(step); integ.addStepHandler(new ContinuousOutputModel()); integ.integrate(pb, pb.getInitialTime(), pb.getInitialState(), pb.getFinalTime(), new double[pb.getDimension()]); ByteArrayOutputStream bos = new ByteArrayOutputStream(); ObjectOutputStream oos = new ObjectOutputStream(bos); for (StepHandler handler : integ.getStepHandlers()) { oos.writeObject(handler); } ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray()); ObjectInputStream ois = new ObjectInputStream(bis); ContinuousOutputModel cm = (ContinuousOutputModel) ois.readObject(); Random random = new Random(347588535632l); double maxError = 0.0; for (int i = 0; i < 1000; ++i) { double r = random.nextDouble(); double time = r * pb.getInitialTime() + (1.0 - r) * pb.getFinalTime(); cm.setInterpolatedTime(time); double[] interpolatedY = cm.getInterpolatedState (); double[] theoreticalY = pb.computeTheoreticalState(time); double dx = interpolatedY[0] - theoreticalY[0]; double dy = interpolatedY[1] - theoreticalY[1]; double error = dx * dx + dy * dy; if (error > maxError) { maxError = error; } } Assert.assertTrue(maxError < 0.001); } private static class DummyIntegrator extends RungeKuttaIntegrator { protected DummyIntegrator(RungeKuttaStepInterpolator prototype) { super("dummy", new double[0], new double[0][0], new double[0], prototype, Double.NaN); } } } ././@LongLink100644 0 0 153 12126630646 10260 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/ode/nonstiff/ThreeEighthesIntegratorTest.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/ode/nonstiff/ThreeEighthesIntegratorTes100644 1750 1750 22143 12126627673 32370 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.ode.nonstiff; import org.apache.commons.math3.exception.DimensionMismatchException; import org.apache.commons.math3.exception.MaxCountExceededException; import org.apache.commons.math3.exception.NoBracketingException; import org.apache.commons.math3.exception.NumberIsTooSmallException; import org.apache.commons.math3.ode.FirstOrderDifferentialEquations; import org.apache.commons.math3.ode.FirstOrderIntegrator; import org.apache.commons.math3.ode.TestProblem1; import org.apache.commons.math3.ode.TestProblem3; import org.apache.commons.math3.ode.TestProblem5; import org.apache.commons.math3.ode.TestProblemAbstract; import org.apache.commons.math3.ode.TestProblemFactory; import org.apache.commons.math3.ode.TestProblemHandler; import org.apache.commons.math3.ode.events.EventHandler; import org.apache.commons.math3.ode.sampling.StepHandler; import org.apache.commons.math3.ode.sampling.StepInterpolator; import org.apache.commons.math3.util.FastMath; import org.junit.Assert; import org.junit.Test; public class ThreeEighthesIntegratorTest { @Test(expected=DimensionMismatchException.class) public void testDimensionCheck() throws DimensionMismatchException, NumberIsTooSmallException, MaxCountExceededException, NoBracketingException { TestProblem1 pb = new TestProblem1(); new ThreeEighthesIntegrator(0.01).integrate(pb, 0.0, new double[pb.getDimension()+10], 1.0, new double[pb.getDimension()+10]); Assert.fail("an exception should have been thrown"); } @Test public void testDecreasingSteps() throws DimensionMismatchException, NumberIsTooSmallException, MaxCountExceededException, NoBracketingException { TestProblemAbstract[] problems = TestProblemFactory.getProblems(); for (int k = 0; k < problems.length; ++k) { double previousValueError = Double.NaN; double previousTimeError = Double.NaN; for (int i = 4; i < 10; ++i) { TestProblemAbstract pb = problems[k].copy(); double step = (pb.getFinalTime() - pb.getInitialTime()) * FastMath.pow(2.0, -i); FirstOrderIntegrator integ = new ThreeEighthesIntegrator(step); TestProblemHandler handler = new TestProblemHandler(pb, integ); integ.addStepHandler(handler); EventHandler[] functions = pb.getEventsHandlers(); for (int l = 0; l < functions.length; ++l) { integ.addEventHandler(functions[l], Double.POSITIVE_INFINITY, 1.0e-6 * step, 1000); } double stopTime = integ.integrate(pb, pb.getInitialTime(), pb.getInitialState(), pb.getFinalTime(), new double[pb.getDimension()]); if (functions.length == 0) { Assert.assertEquals(pb.getFinalTime(), stopTime, 1.0e-10); } double error = handler.getMaximalValueError(); if (i > 4) { Assert.assertTrue(error < 1.01 * FastMath.abs(previousValueError)); } previousValueError = error; double timeError = handler.getMaximalTimeError(); if (i > 4) { Assert.assertTrue(timeError <= FastMath.abs(previousTimeError)); } previousTimeError = timeError; } } } @Test public void testSmallStep() throws DimensionMismatchException, NumberIsTooSmallException, MaxCountExceededException, NoBracketingException { TestProblem1 pb = new TestProblem1(); double step = (pb.getFinalTime() - pb.getInitialTime()) * 0.001; FirstOrderIntegrator integ = new ThreeEighthesIntegrator(step); TestProblemHandler handler = new TestProblemHandler(pb, integ); integ.addStepHandler(handler); integ.integrate(pb, pb.getInitialTime(), pb.getInitialState(), pb.getFinalTime(), new double[pb.getDimension()]); Assert.assertTrue(handler.getLastError() < 2.0e-13); Assert.assertTrue(handler.getMaximalValueError() < 4.0e-12); Assert.assertEquals(0, handler.getMaximalTimeError(), 1.0e-12); Assert.assertEquals("3/8", integ.getName()); } @Test public void testBigStep() throws DimensionMismatchException, NumberIsTooSmallException, MaxCountExceededException, NoBracketingException { TestProblem1 pb = new TestProblem1(); double step = (pb.getFinalTime() - pb.getInitialTime()) * 0.2; FirstOrderIntegrator integ = new ThreeEighthesIntegrator(step); TestProblemHandler handler = new TestProblemHandler(pb, integ); integ.addStepHandler(handler); integ.integrate(pb, pb.getInitialTime(), pb.getInitialState(), pb.getFinalTime(), new double[pb.getDimension()]); Assert.assertTrue(handler.getLastError() > 0.0004); Assert.assertTrue(handler.getMaximalValueError() > 0.005); Assert.assertEquals(0, handler.getMaximalTimeError(), 1.0e-12); } @Test public void testBackward() throws DimensionMismatchException, NumberIsTooSmallException, MaxCountExceededException, NoBracketingException { TestProblem5 pb = new TestProblem5(); double step = FastMath.abs(pb.getFinalTime() - pb.getInitialTime()) * 0.001; FirstOrderIntegrator integ = new ThreeEighthesIntegrator(step); TestProblemHandler handler = new TestProblemHandler(pb, integ); integ.addStepHandler(handler); integ.integrate(pb, pb.getInitialTime(), pb.getInitialState(), pb.getFinalTime(), new double[pb.getDimension()]); Assert.assertTrue(handler.getLastError() < 5.0e-10); Assert.assertTrue(handler.getMaximalValueError() < 7.0e-10); Assert.assertEquals(0, handler.getMaximalTimeError(), 1.0e-12); Assert.assertEquals("3/8", integ.getName()); } @Test public void testKepler() throws DimensionMismatchException, NumberIsTooSmallException, MaxCountExceededException, NoBracketingException { final TestProblem3 pb = new TestProblem3(0.9); double step = (pb.getFinalTime() - pb.getInitialTime()) * 0.0003; FirstOrderIntegrator integ = new ThreeEighthesIntegrator(step); integ.addStepHandler(new KeplerHandler(pb)); integ.integrate(pb, pb.getInitialTime(), pb.getInitialState(), pb.getFinalTime(), new double[pb.getDimension()]); } private static class KeplerHandler implements StepHandler { public KeplerHandler(TestProblem3 pb) { this.pb = pb; maxError = 0; } public void init(double t0, double[] y0, double t) { maxError = 0; } public void handleStep(StepInterpolator interpolator, boolean isLast) throws MaxCountExceededException { double[] interpolatedY = interpolator.getInterpolatedState(); double[] theoreticalY = pb.computeTheoreticalState(interpolator.getCurrentTime()); double dx = interpolatedY[0] - theoreticalY[0]; double dy = interpolatedY[1] - theoreticalY[1]; double error = dx * dx + dy * dy; if (error > maxError) { maxError = error; } if (isLast) { // even with more than 1000 evaluations per period, // RK4 is not able to integrate such an eccentric // orbit with a good accuracy Assert.assertTrue(maxError > 0.005); } } private TestProblem3 pb; private double maxError = 0; } @Test public void testStepSize() throws DimensionMismatchException, NumberIsTooSmallException, MaxCountExceededException, NoBracketingException { final double step = 1.23456; FirstOrderIntegrator integ = new ThreeEighthesIntegrator(step); integ.addStepHandler(new StepHandler() { public void handleStep(StepInterpolator interpolator, boolean isLast) { if (! isLast) { Assert.assertEquals(step, interpolator.getCurrentTime() - interpolator.getPreviousTime(), 1.0e-12); } } public void init(double t0, double[] y0, double t) { } }); integ.integrate(new FirstOrderDifferentialEquations() { public void computeDerivatives(double t, double[] y, double[] dot) { dot[0] = 1.0; } public int getDimension() { return 1; } }, 0.0, new double[] { 0.0 }, 5.0, new double[1]); } } ././@LongLink100644 0 0 152 12126630646 10257 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/ode/nonstiff/HighamHall54IntegratorTest.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/ode/nonstiff/HighamHall54IntegratorTest100644 1750 1750 35526 12126627673 32204 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.ode.nonstiff; import org.apache.commons.math3.exception.DimensionMismatchException; import org.apache.commons.math3.exception.MaxCountExceededException; import org.apache.commons.math3.exception.NoBracketingException; import org.apache.commons.math3.exception.NumberIsTooSmallException; import org.apache.commons.math3.exception.TooManyEvaluationsException; import org.apache.commons.math3.ode.FirstOrderDifferentialEquations; import org.apache.commons.math3.ode.FirstOrderIntegrator; import org.apache.commons.math3.ode.TestProblem1; import org.apache.commons.math3.ode.TestProblem3; import org.apache.commons.math3.ode.TestProblem4; import org.apache.commons.math3.ode.TestProblem5; import org.apache.commons.math3.ode.TestProblemHandler; import org.apache.commons.math3.ode.events.EventHandler; import org.apache.commons.math3.util.FastMath; import org.junit.Assert; import org.junit.Test; public class HighamHall54IntegratorTest { @Test public void testWrongDerivative() throws DimensionMismatchException, NumberIsTooSmallException, MaxCountExceededException, NoBracketingException { HighamHall54Integrator integrator = new HighamHall54Integrator(0.0, 1.0, 1.0e-10, 1.0e-10); FirstOrderDifferentialEquations equations = new FirstOrderDifferentialEquations() { public void computeDerivatives(double t, double[] y, double[] dot) { if (t < -0.5) { throw new LocalException(); } else { throw new RuntimeException("oops"); } } public int getDimension() { return 1; } }; try { integrator.integrate(equations, -1.0, new double[1], 0.0, new double[1]); Assert.fail("an exception should have been thrown"); } catch(LocalException de) { // expected behavior } try { integrator.integrate(equations, 0.0, new double[1], 1.0, new double[1]); Assert.fail("an exception should have been thrown"); } catch(RuntimeException de) { // expected behavior } } @Test(expected=NumberIsTooSmallException.class) public void testMinStep() throws DimensionMismatchException, NumberIsTooSmallException, MaxCountExceededException, NoBracketingException { TestProblem1 pb = new TestProblem1(); double minStep = 0.1 * (pb.getFinalTime() - pb.getInitialTime()); double maxStep = pb.getFinalTime() - pb.getInitialTime(); double[] vecAbsoluteTolerance = { 1.0e-15, 1.0e-16 }; double[] vecRelativeTolerance = { 1.0e-15, 1.0e-16 }; FirstOrderIntegrator integ = new HighamHall54Integrator(minStep, maxStep, vecAbsoluteTolerance, vecRelativeTolerance); TestProblemHandler handler = new TestProblemHandler(pb, integ); integ.addStepHandler(handler); integ.integrate(pb, pb.getInitialTime(), pb.getInitialState(), pb.getFinalTime(), new double[pb.getDimension()]); Assert.fail("an exception should have been thrown"); } @Test public void testIncreasingTolerance() throws DimensionMismatchException, NumberIsTooSmallException, MaxCountExceededException, NoBracketingException { int previousCalls = Integer.MAX_VALUE; for (int i = -12; i < -2; ++i) { TestProblem1 pb = new TestProblem1(); double minStep = 0; double maxStep = pb.getFinalTime() - pb.getInitialTime(); double scalAbsoluteTolerance = FastMath.pow(10.0, i); double scalRelativeTolerance = 0.01 * scalAbsoluteTolerance; FirstOrderIntegrator integ = new HighamHall54Integrator(minStep, maxStep, scalAbsoluteTolerance, scalRelativeTolerance); TestProblemHandler handler = new TestProblemHandler(pb, integ); integ.addStepHandler(handler); integ.integrate(pb, pb.getInitialTime(), pb.getInitialState(), pb.getFinalTime(), new double[pb.getDimension()]); // the 1.3 factor is only valid for this test // and has been obtained from trial and error // there is no general relation between local and global errors Assert.assertTrue(handler.getMaximalValueError() < (1.3 * scalAbsoluteTolerance)); Assert.assertEquals(0, handler.getMaximalTimeError(), 1.0e-12); int calls = pb.getCalls(); Assert.assertEquals(integ.getEvaluations(), calls); Assert.assertTrue(calls <= previousCalls); previousCalls = calls; } } @Test public void testBackward() throws DimensionMismatchException, NumberIsTooSmallException, MaxCountExceededException, NoBracketingException { TestProblem5 pb = new TestProblem5(); double minStep = 0; double maxStep = pb.getFinalTime() - pb.getInitialTime(); double scalAbsoluteTolerance = 1.0e-8; double scalRelativeTolerance = 0.01 * scalAbsoluteTolerance; FirstOrderIntegrator integ = new HighamHall54Integrator(minStep, maxStep, scalAbsoluteTolerance, scalRelativeTolerance); TestProblemHandler handler = new TestProblemHandler(pb, integ); integ.addStepHandler(handler); integ.integrate(pb, pb.getInitialTime(), pb.getInitialState(), pb.getFinalTime(), new double[pb.getDimension()]); Assert.assertTrue(handler.getLastError() < 5.0e-7); Assert.assertTrue(handler.getMaximalValueError() < 5.0e-7); Assert.assertEquals(0, handler.getMaximalTimeError(), 1.0e-12); Assert.assertEquals("Higham-Hall 5(4)", integ.getName()); } @Test public void testEvents() throws DimensionMismatchException, NumberIsTooSmallException, MaxCountExceededException, NoBracketingException { TestProblem4 pb = new TestProblem4(); double minStep = 0; double maxStep = pb.getFinalTime() - pb.getInitialTime(); double scalAbsoluteTolerance = 1.0e-8; double scalRelativeTolerance = 0.01 * scalAbsoluteTolerance; FirstOrderIntegrator integ = new HighamHall54Integrator(minStep, maxStep, scalAbsoluteTolerance, scalRelativeTolerance); TestProblemHandler handler = new TestProblemHandler(pb, integ); integ.addStepHandler(handler); EventHandler[] functions = pb.getEventsHandlers(); double convergence = 1.0e-8 * maxStep; for (int l = 0; l < functions.length; ++l) { integ.addEventHandler(functions[l], Double.POSITIVE_INFINITY, convergence, 1000); } Assert.assertEquals(functions.length, integ.getEventHandlers().size()); integ.integrate(pb, pb.getInitialTime(), pb.getInitialState(), pb.getFinalTime(), new double[pb.getDimension()]); Assert.assertTrue(handler.getMaximalValueError() < 1.0e-7); Assert.assertEquals(0, handler.getMaximalTimeError(), convergence); Assert.assertEquals(12.0, handler.getLastTime(), convergence); integ.clearEventHandlers(); Assert.assertEquals(0, integ.getEventHandlers().size()); } @Test(expected=LocalException.class) public void testEventsErrors() throws DimensionMismatchException, NumberIsTooSmallException, MaxCountExceededException, NoBracketingException { final TestProblem1 pb = new TestProblem1(); double minStep = 0; double maxStep = pb.getFinalTime() - pb.getInitialTime(); double scalAbsoluteTolerance = 1.0e-8; double scalRelativeTolerance = 0.01 * scalAbsoluteTolerance; FirstOrderIntegrator integ = new HighamHall54Integrator(minStep, maxStep, scalAbsoluteTolerance, scalRelativeTolerance); TestProblemHandler handler = new TestProblemHandler(pb, integ); integ.addStepHandler(handler); integ.addEventHandler(new EventHandler() { public void init(double t0, double[] y0, double t) { } public Action eventOccurred(double t, double[] y, boolean increasing) { return Action.CONTINUE; } public double g(double t, double[] y) { double middle = (pb.getInitialTime() + pb.getFinalTime()) / 2; double offset = t - middle; if (offset > 0) { throw new LocalException(); } return offset; } public void resetState(double t, double[] y) { } }, Double.POSITIVE_INFINITY, 1.0e-8 * maxStep, 1000); integ.integrate(pb, pb.getInitialTime(), pb.getInitialState(), pb.getFinalTime(), new double[pb.getDimension()]); } private static class LocalException extends RuntimeException { private static final long serialVersionUID = 3041292643919807960L; } @Test public void testEventsNoConvergence() throws DimensionMismatchException, NumberIsTooSmallException, MaxCountExceededException, NoBracketingException { final TestProblem1 pb = new TestProblem1(); double minStep = 0; double maxStep = pb.getFinalTime() - pb.getInitialTime(); double scalAbsoluteTolerance = 1.0e-8; double scalRelativeTolerance = 0.01 * scalAbsoluteTolerance; FirstOrderIntegrator integ = new HighamHall54Integrator(minStep, maxStep, scalAbsoluteTolerance, scalRelativeTolerance); TestProblemHandler handler = new TestProblemHandler(pb, integ); integ.addStepHandler(handler); integ.addEventHandler(new EventHandler() { public void init(double t0, double[] y0, double t) { } public Action eventOccurred(double t, double[] y, boolean increasing) { return Action.CONTINUE; } public double g(double t, double[] y) { double middle = (pb.getInitialTime() + pb.getFinalTime()) / 2; double offset = t - middle; return (offset > 0) ? (offset + 0.5) : (offset - 0.5); } public void resetState(double t, double[] y) { } }, Double.POSITIVE_INFINITY, 1.0e-8 * maxStep, 3); try { integ.integrate(pb, pb.getInitialTime(), pb.getInitialState(), pb.getFinalTime(), new double[pb.getDimension()]); Assert.fail("an exception should have been thrown"); } catch (TooManyEvaluationsException tmee) { // Expected. } } @Test public void testSanityChecks() throws DimensionMismatchException, NumberIsTooSmallException, MaxCountExceededException, NoBracketingException { final TestProblem3 pb = new TestProblem3(0.9); double minStep = 0; double maxStep = pb.getFinalTime() - pb.getInitialTime(); try { FirstOrderIntegrator integ = new HighamHall54Integrator(minStep, maxStep, new double[4], new double[4]); integ.integrate(pb, pb.getInitialTime(), new double[6], pb.getFinalTime(), new double[pb.getDimension()]); Assert.fail("an exception should have been thrown"); } catch (DimensionMismatchException ie) { // expected behavior } try { FirstOrderIntegrator integ = new HighamHall54Integrator(minStep, maxStep, new double[4], new double[4]); integ.integrate(pb, pb.getInitialTime(), pb.getInitialState(), pb.getFinalTime(), new double[6]); Assert.fail("an exception should have been thrown"); } catch (DimensionMismatchException ie) { // expected behavior } try { FirstOrderIntegrator integ = new HighamHall54Integrator(minStep, maxStep, new double[2], new double[4]); integ.integrate(pb, pb.getInitialTime(), pb.getInitialState(), pb.getFinalTime(), new double[pb.getDimension()]); Assert.fail("an exception should have been thrown"); } catch (DimensionMismatchException ie) { // expected behavior } try { FirstOrderIntegrator integ = new HighamHall54Integrator(minStep, maxStep, new double[4], new double[2]); integ.integrate(pb, pb.getInitialTime(), pb.getInitialState(), pb.getFinalTime(), new double[pb.getDimension()]); Assert.fail("an exception should have been thrown"); } catch (DimensionMismatchException ie) { // expected behavior } try { FirstOrderIntegrator integ = new HighamHall54Integrator(minStep, maxStep, new double[4], new double[4]); integ.integrate(pb, pb.getInitialTime(), pb.getInitialState(), pb.getInitialTime(), new double[pb.getDimension()]); Assert.fail("an exception should have been thrown"); } catch (NumberIsTooSmallException ie) { // expected behavior } } @Test public void testKepler() throws DimensionMismatchException, NumberIsTooSmallException, MaxCountExceededException, NoBracketingException { final TestProblem3 pb = new TestProblem3(0.9); double minStep = 0; double maxStep = pb.getFinalTime() - pb.getInitialTime(); double[] vecAbsoluteTolerance = { 1.0e-8, 1.0e-8, 1.0e-10, 1.0e-10 }; double[] vecRelativeTolerance = { 1.0e-10, 1.0e-10, 1.0e-8, 1.0e-8 }; FirstOrderIntegrator integ = new HighamHall54Integrator(minStep, maxStep, vecAbsoluteTolerance, vecRelativeTolerance); TestProblemHandler handler = new TestProblemHandler(pb, integ); integ.addStepHandler(handler); integ.integrate(pb, pb.getInitialTime(), pb.getInitialState(), pb.getFinalTime(), new double[pb.getDimension()]); Assert.assertEquals(0.0, handler.getMaximalValueError(), 1.5e-4); Assert.assertEquals("Higham-Hall 5(4)", integ.getName()); } } ././@LongLink100644 0 0 156 12126630646 10263 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/ode/nonstiff/DormandPrince853IntegratorTest.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/ode/nonstiff/DormandPrince853Integrator100644 1750 1750 44473 12126627673 32163 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.ode.nonstiff; import org.apache.commons.math3.exception.DimensionMismatchException; import org.apache.commons.math3.exception.MaxCountExceededException; import org.apache.commons.math3.exception.NoBracketingException; import org.apache.commons.math3.exception.NumberIsTooSmallException; import org.apache.commons.math3.ode.FirstOrderDifferentialEquations; import org.apache.commons.math3.ode.FirstOrderIntegrator; import org.apache.commons.math3.ode.TestProblem1; import org.apache.commons.math3.ode.TestProblem3; import org.apache.commons.math3.ode.TestProblem4; import org.apache.commons.math3.ode.TestProblem5; import org.apache.commons.math3.ode.TestProblemHandler; import org.apache.commons.math3.ode.events.EventHandler; import org.apache.commons.math3.ode.sampling.StepHandler; import org.apache.commons.math3.ode.sampling.StepInterpolator; import org.apache.commons.math3.util.FastMath; import org.junit.Assert; import org.junit.Test; public class DormandPrince853IntegratorTest { @Test public void testMissedEndEvent() throws DimensionMismatchException, NumberIsTooSmallException, MaxCountExceededException, NoBracketingException { final double t0 = 1878250320.0000029; final double tEvent = 1878250379.9999986; final double[] k = { 1.0e-4, 1.0e-5, 1.0e-6 }; FirstOrderDifferentialEquations ode = new FirstOrderDifferentialEquations() { public int getDimension() { return k.length; } public void computeDerivatives(double t, double[] y, double[] yDot) { for (int i = 0; i < y.length; ++i) { yDot[i] = k[i] * y[i]; } } }; DormandPrince853Integrator integrator = new DormandPrince853Integrator(0.0, 100.0, 1.0e-10, 1.0e-10); double[] y0 = new double[k.length]; for (int i = 0; i < y0.length; ++i) { y0[i] = i + 1; } double[] y = new double[k.length]; integrator.setInitialStepSize(60.0); double finalT = integrator.integrate(ode, t0, y0, tEvent, y); Assert.assertEquals(tEvent, finalT, 5.0e-6); for (int i = 0; i < y.length; ++i) { Assert.assertEquals(y0[i] * FastMath.exp(k[i] * (finalT - t0)), y[i], 1.0e-9); } integrator.setInitialStepSize(60.0); integrator.addEventHandler(new EventHandler() { public void init(double t0, double[] y0, double t) { } public void resetState(double t, double[] y) { } public double g(double t, double[] y) { return t - tEvent; } public Action eventOccurred(double t, double[] y, boolean increasing) { Assert.assertEquals(tEvent, t, 5.0e-6); return Action.CONTINUE; } }, Double.POSITIVE_INFINITY, 1.0e-20, 100); finalT = integrator.integrate(ode, t0, y0, tEvent + 120, y); Assert.assertEquals(tEvent + 120, finalT, 5.0e-6); for (int i = 0; i < y.length; ++i) { Assert.assertEquals(y0[i] * FastMath.exp(k[i] * (finalT - t0)), y[i], 1.0e-9); } } @Test(expected=DimensionMismatchException.class) public void testDimensionCheck() throws DimensionMismatchException, NumberIsTooSmallException, MaxCountExceededException, NoBracketingException { TestProblem1 pb = new TestProblem1(); DormandPrince853Integrator integrator = new DormandPrince853Integrator(0.0, 1.0, 1.0e-10, 1.0e-10); integrator.integrate(pb, 0.0, new double[pb.getDimension()+10], 1.0, new double[pb.getDimension()+10]); Assert.fail("an exception should have been thrown"); } @Test(expected=NumberIsTooSmallException.class) public void testNullIntervalCheck() throws DimensionMismatchException, NumberIsTooSmallException, MaxCountExceededException, NoBracketingException { TestProblem1 pb = new TestProblem1(); DormandPrince853Integrator integrator = new DormandPrince853Integrator(0.0, 1.0, 1.0e-10, 1.0e-10); integrator.integrate(pb, 0.0, new double[pb.getDimension()], 0.0, new double[pb.getDimension()]); Assert.fail("an exception should have been thrown"); } @Test(expected=NumberIsTooSmallException.class) public void testMinStep() throws DimensionMismatchException, NumberIsTooSmallException, MaxCountExceededException, NoBracketingException { TestProblem1 pb = new TestProblem1(); double minStep = 0.1 * (pb.getFinalTime() - pb.getInitialTime()); double maxStep = pb.getFinalTime() - pb.getInitialTime(); double[] vecAbsoluteTolerance = { 1.0e-15, 1.0e-16 }; double[] vecRelativeTolerance = { 1.0e-15, 1.0e-16 }; FirstOrderIntegrator integ = new DormandPrince853Integrator(minStep, maxStep, vecAbsoluteTolerance, vecRelativeTolerance); TestProblemHandler handler = new TestProblemHandler(pb, integ); integ.addStepHandler(handler); integ.integrate(pb, pb.getInitialTime(), pb.getInitialState(), pb.getFinalTime(), new double[pb.getDimension()]); Assert.fail("an exception should have been thrown"); } @Test public void testIncreasingTolerance() throws DimensionMismatchException, NumberIsTooSmallException, MaxCountExceededException, NoBracketingException { int previousCalls = Integer.MAX_VALUE; AdaptiveStepsizeIntegrator integ = new DormandPrince853Integrator(0, Double.POSITIVE_INFINITY, Double.NaN, Double.NaN); for (int i = -12; i < -2; ++i) { TestProblem1 pb = new TestProblem1(); double minStep = 0; double maxStep = pb.getFinalTime() - pb.getInitialTime(); double scalAbsoluteTolerance = FastMath.pow(10.0, i); double scalRelativeTolerance = 0.01 * scalAbsoluteTolerance; integ.setStepSizeControl(minStep, maxStep, scalAbsoluteTolerance, scalRelativeTolerance); TestProblemHandler handler = new TestProblemHandler(pb, integ); integ.addStepHandler(handler); integ.integrate(pb, pb.getInitialTime(), pb.getInitialState(), pb.getFinalTime(), new double[pb.getDimension()]); // the 1.3 factor is only valid for this test // and has been obtained from trial and error // there is no general relation between local and global errors Assert.assertTrue(handler.getMaximalValueError() < (1.3 * scalAbsoluteTolerance)); Assert.assertEquals(0, handler.getMaximalTimeError(), 1.0e-12); int calls = pb.getCalls(); Assert.assertEquals(integ.getEvaluations(), calls); Assert.assertTrue(calls <= previousCalls); previousCalls = calls; } } @Test public void testTooLargeFirstStep() throws DimensionMismatchException, NumberIsTooSmallException, MaxCountExceededException, NoBracketingException { AdaptiveStepsizeIntegrator integ = new DormandPrince853Integrator(0, Double.POSITIVE_INFINITY, Double.NaN, Double.NaN); final double start = 0.0; final double end = 0.001; FirstOrderDifferentialEquations equations = new FirstOrderDifferentialEquations() { public int getDimension() { return 1; } public void computeDerivatives(double t, double[] y, double[] yDot) { Assert.assertTrue(t >= FastMath.nextAfter(start, Double.NEGATIVE_INFINITY)); Assert.assertTrue(t <= FastMath.nextAfter(end, Double.POSITIVE_INFINITY)); yDot[0] = -100.0 * y[0]; } }; integ.setStepSizeControl(0, 1.0, 1.0e-6, 1.0e-8); integ.integrate(equations, start, new double[] { 1.0 }, end, new double[1]); } @Test public void testBackward() throws DimensionMismatchException, NumberIsTooSmallException, MaxCountExceededException, NoBracketingException { TestProblem5 pb = new TestProblem5(); double minStep = 0; double maxStep = pb.getFinalTime() - pb.getInitialTime(); double scalAbsoluteTolerance = 1.0e-8; double scalRelativeTolerance = 0.01 * scalAbsoluteTolerance; FirstOrderIntegrator integ = new DormandPrince853Integrator(minStep, maxStep, scalAbsoluteTolerance, scalRelativeTolerance); TestProblemHandler handler = new TestProblemHandler(pb, integ); integ.addStepHandler(handler); integ.integrate(pb, pb.getInitialTime(), pb.getInitialState(), pb.getFinalTime(), new double[pb.getDimension()]); Assert.assertTrue(handler.getLastError() < 1.1e-7); Assert.assertTrue(handler.getMaximalValueError() < 1.1e-7); Assert.assertEquals(0, handler.getMaximalTimeError(), 1.0e-12); Assert.assertEquals("Dormand-Prince 8 (5, 3)", integ.getName()); } @Test public void testEvents() throws DimensionMismatchException, NumberIsTooSmallException, MaxCountExceededException, NoBracketingException { TestProblem4 pb = new TestProblem4(); double minStep = 0; double maxStep = pb.getFinalTime() - pb.getInitialTime(); double scalAbsoluteTolerance = 1.0e-9; double scalRelativeTolerance = 0.01 * scalAbsoluteTolerance; FirstOrderIntegrator integ = new DormandPrince853Integrator(minStep, maxStep, scalAbsoluteTolerance, scalRelativeTolerance); TestProblemHandler handler = new TestProblemHandler(pb, integ); integ.addStepHandler(handler); EventHandler[] functions = pb.getEventsHandlers(); double convergence = 1.0e-8 * maxStep; for (int l = 0; l < functions.length; ++l) { integ.addEventHandler(functions[l], Double.POSITIVE_INFINITY, convergence, 1000); } Assert.assertEquals(functions.length, integ.getEventHandlers().size()); integ.integrate(pb, pb.getInitialTime(), pb.getInitialState(), pb.getFinalTime(), new double[pb.getDimension()]); Assert.assertEquals(0, handler.getMaximalValueError(), 2.1e-7); Assert.assertEquals(0, handler.getMaximalTimeError(), convergence); Assert.assertEquals(12.0, handler.getLastTime(), convergence); integ.clearEventHandlers(); Assert.assertEquals(0, integ.getEventHandlers().size()); } @Test public void testKepler() throws DimensionMismatchException, NumberIsTooSmallException, MaxCountExceededException, NoBracketingException { final TestProblem3 pb = new TestProblem3(0.9); double minStep = 0; double maxStep = pb.getFinalTime() - pb.getInitialTime(); double scalAbsoluteTolerance = 1.0e-8; double scalRelativeTolerance = scalAbsoluteTolerance; FirstOrderIntegrator integ = new DormandPrince853Integrator(minStep, maxStep, scalAbsoluteTolerance, scalRelativeTolerance); integ.addStepHandler(new KeplerHandler(pb)); integ.integrate(pb, pb.getInitialTime(), pb.getInitialState(), pb.getFinalTime(), new double[pb.getDimension()]); Assert.assertEquals(integ.getEvaluations(), pb.getCalls()); Assert.assertTrue(pb.getCalls() < 3300); } @Test public void testVariableSteps() throws DimensionMismatchException, NumberIsTooSmallException, MaxCountExceededException, NoBracketingException { final TestProblem3 pb = new TestProblem3(0.9); double minStep = 0; double maxStep = pb.getFinalTime() - pb.getInitialTime(); double scalAbsoluteTolerance = 1.0e-8; double scalRelativeTolerance = scalAbsoluteTolerance; FirstOrderIntegrator integ = new DormandPrince853Integrator(minStep, maxStep, scalAbsoluteTolerance, scalRelativeTolerance); integ.addStepHandler(new VariableHandler()); double stopTime = integ.integrate(pb, pb.getInitialTime(), pb.getInitialState(), pb.getFinalTime(), new double[pb.getDimension()]); Assert.assertEquals(pb.getFinalTime(), stopTime, 1.0e-10); Assert.assertEquals("Dormand-Prince 8 (5, 3)", integ.getName()); } @Test public void testUnstableDerivative() throws DimensionMismatchException, NumberIsTooSmallException, MaxCountExceededException, NoBracketingException { final StepProblem stepProblem = new StepProblem(0.0, 1.0, 2.0); FirstOrderIntegrator integ = new DormandPrince853Integrator(0.1, 10, 1.0e-12, 0.0); integ.addEventHandler(stepProblem, 1.0, 1.0e-12, 1000); double[] y = { Double.NaN }; integ.integrate(stepProblem, 0.0, new double[] { 0.0 }, 10.0, y); Assert.assertEquals(8.0, y[0], 1.0e-12); } @Test public void testEventsScheduling() { FirstOrderDifferentialEquations sincos = new FirstOrderDifferentialEquations() { public int getDimension() { return 2; } public void computeDerivatives(double t, double[] y, double[] yDot) { yDot[0] = y[1]; yDot[1] = -y[0]; } }; SchedulingChecker sinChecker = new SchedulingChecker(0); // events at 0, PI, 2PI ... SchedulingChecker cosChecker = new SchedulingChecker(1); // events at PI/2, 3PI/2, 5PI/2 ... FirstOrderIntegrator integ = new DormandPrince853Integrator(0.001, 1.0, 1.0e-12, 0.0); integ.addEventHandler(sinChecker, 0.01, 1.0e-7, 100); integ.addStepHandler(sinChecker); integ.addEventHandler(cosChecker, 0.01, 1.0e-7, 100); integ.addStepHandler(cosChecker); double t0 = 0.5; double[] y0 = new double[] { FastMath.sin(t0), FastMath.cos(t0) }; double t = 10.0; double[] y = new double[2]; integ.integrate(sincos, t0, y0, t, y); } private static class SchedulingChecker implements StepHandler, EventHandler { int index; double tMin; public SchedulingChecker(int index) { this.index = index; } public void init(double t0, double[] y0, double t) { tMin = t0; } public void handleStep(StepInterpolator interpolator, boolean isLast) { tMin = interpolator.getCurrentTime(); } public double g(double t, double[] y) { // once a step has been handled by handleStep, // events checking should only refer to dates after the step Assert.assertTrue(t >= tMin); return y[index]; } public Action eventOccurred(double t, double[] y, boolean increasing) { return Action.RESET_STATE; } public void resetState(double t, double[] y) { // in fact, we don't need to reset anything for the test } } private static class KeplerHandler implements StepHandler { public KeplerHandler(TestProblem3 pb) { this.pb = pb; } public void init(double t0, double[] y0, double t) { nbSteps = 0; maxError = 0; } public void handleStep(StepInterpolator interpolator, boolean isLast) throws MaxCountExceededException { ++nbSteps; for (int a = 1; a < 10; ++a) { double prev = interpolator.getPreviousTime(); double curr = interpolator.getCurrentTime(); double interp = ((10 - a) * prev + a * curr) / 10; interpolator.setInterpolatedTime(interp); double[] interpolatedY = interpolator.getInterpolatedState (); double[] theoreticalY = pb.computeTheoreticalState(interpolator.getInterpolatedTime()); double dx = interpolatedY[0] - theoreticalY[0]; double dy = interpolatedY[1] - theoreticalY[1]; double error = dx * dx + dy * dy; if (error > maxError) { maxError = error; } } if (isLast) { Assert.assertTrue(maxError < 2.4e-10); Assert.assertTrue(nbSteps < 150); } } private int nbSteps; private double maxError; private TestProblem3 pb; } private static class VariableHandler implements StepHandler { public VariableHandler() { firstTime = true; minStep = 0; maxStep = 0; } public void init(double t0, double[] y0, double t) { firstTime = true; minStep = 0; maxStep = 0; } public void handleStep(StepInterpolator interpolator, boolean isLast) { double step = FastMath.abs(interpolator.getCurrentTime() - interpolator.getPreviousTime()); if (firstTime) { minStep = FastMath.abs(step); maxStep = minStep; firstTime = false; } else { if (step < minStep) { minStep = step; } if (step > maxStep) { maxStep = step; } } if (isLast) { Assert.assertTrue(minStep < (1.0 / 100.0)); Assert.assertTrue(maxStep > (1.0 / 2.0)); } } private boolean firstTime = true; private double minStep = 0; private double maxStep = 0; } } commons-math3-3.2-src/src/test/java/org/apache/commons/math3/ode/TestProblemFactory.java100644 1750 1750 2677 12126627673 30001 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.ode; /** * This class is used in the junit tests for the ODE integrators. */ public class TestProblemFactory { /** Problems pool. */ private static final TestProblemAbstract[] pool = { new TestProblem1(), new TestProblem2(), new TestProblem3(), new TestProblem4(), new TestProblem5(), new TestProblem6() }; /** * Private constructor. * This is a utility class, so there are no instance at all. */ private TestProblemFactory() { } /** * Get the problems. * @return array of problems to solve */ public static TestProblemAbstract[] getProblems() { return pool; } } commons-math3-3.2-src/src/test/java/org/apache/commons/math3/ode/TestProblemHandler.java100644 1750 1750 12122 12126627673 27751 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.ode; import org.apache.commons.math3.exception.MaxCountExceededException; import org.apache.commons.math3.ode.sampling.StepHandler; import org.apache.commons.math3.ode.sampling.StepInterpolator; import org.apache.commons.math3.util.FastMath; /** * This class is used to handle steps for the test problems * integrated during the junit tests for the ODE integrators. */ public class TestProblemHandler implements StepHandler { /** Associated problem. */ private TestProblemAbstract problem; /** Maximal errors encountered during the integration. */ private double maxValueError; private double maxTimeError; /** Error at the end of the integration. */ private double lastError; /** Time at the end of integration. */ private double lastTime; /** ODE solver used. */ private ODEIntegrator integrator; /** Expected start for step. */ private double expectedStepStart; /** * Simple constructor. * @param problem problem for which steps should be handled * @param integrator ODE solver used */ public TestProblemHandler(TestProblemAbstract problem, ODEIntegrator integrator) { this.problem = problem; this.integrator = integrator; maxValueError = 0; maxTimeError = 0; lastError = 0; expectedStepStart = Double.NaN; } public void init(double t0, double[] y0, double t) { maxValueError = 0; maxTimeError = 0; lastError = 0; expectedStepStart = Double.NaN; } public void handleStep(StepInterpolator interpolator, boolean isLast) throws MaxCountExceededException { double start = integrator.getCurrentStepStart(); if (FastMath.abs((start - problem.getInitialTime()) / integrator.getCurrentSignedStepsize()) > 0.001) { // multistep integrators do not handle the first steps themselves // so we have to make sure the integrator we look at has really started its work if (!Double.isNaN(expectedStepStart)) { // the step should either start at the end of the integrator step // or at an event if the step is split into several substeps double stepError = FastMath.max(maxTimeError, FastMath.abs(start - expectedStepStart)); for (double eventTime : problem.getTheoreticalEventsTimes()) { stepError = FastMath.min(stepError, FastMath.abs(start - eventTime)); } maxTimeError = FastMath.max(maxTimeError, stepError); } expectedStepStart = start + integrator.getCurrentSignedStepsize(); } double pT = interpolator.getPreviousTime(); double cT = interpolator.getCurrentTime(); double[] errorScale = problem.getErrorScale(); // store the error at the last step if (isLast) { double[] interpolatedY = interpolator.getInterpolatedState(); double[] theoreticalY = problem.computeTheoreticalState(cT); for (int i = 0; i < interpolatedY.length; ++i) { double error = FastMath.abs(interpolatedY[i] - theoreticalY[i]); lastError = FastMath.max(error, lastError); } lastTime = cT; } // walk through the step for (int k = 0; k <= 20; ++k) { double time = pT + (k * (cT - pT)) / 20; interpolator.setInterpolatedTime(time); double[] interpolatedY = interpolator.getInterpolatedState(); double[] theoreticalY = problem.computeTheoreticalState(interpolator.getInterpolatedTime()); // update the errors for (int i = 0; i < interpolatedY.length; ++i) { double error = errorScale[i] * FastMath.abs(interpolatedY[i] - theoreticalY[i]); maxValueError = FastMath.max(error, maxValueError); } } } /** * Get the maximal value error encountered during integration. * @return maximal value error */ public double getMaximalValueError() { return maxValueError; } /** * Get the maximal time error encountered during integration. * @return maximal time error */ public double getMaximalTimeError() { return maxTimeError; } /** * Get the error at the end of the integration. * @return error at the end of the integration */ public double getLastError() { return lastError; } /** * Get the time at the end of the integration. * @return time at the end of the integration. */ public double getLastTime() { return lastTime; } } commons-math3-3.2-src/src/test/java/org/apache/commons/math3/complex/ComplexFieldTest.java100644 1750 1750 2710 12126627672 30307 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.complex; import org.apache.commons.math3.TestUtils; import org.junit.Assert; import org.junit.Test; public class ComplexFieldTest { @Test public void testZero() { Assert.assertEquals(Complex.ZERO, ComplexField.getInstance().getZero()); } @Test public void testOne() { Assert.assertEquals(Complex.ONE, ComplexField.getInstance().getOne()); } @Test public void testSerial() { // deserializing the singleton should give the singleton itself back ComplexField field = ComplexField.getInstance(); Assert.assertTrue(field == TestUtils.serializeAndRecover(field)); } } commons-math3-3.2-src/src/test/java/org/apache/commons/math3/complex/FrenchComplexFormatTest.java100644 1750 1750 2137 12126627672 31645 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.complex; import java.util.Locale; public class FrenchComplexFormatTest extends ComplexFormatAbstractTest { @Override protected char getDecimalCharacter() { return ','; } @Override protected Locale getLocale() { return Locale.FRENCH; } } commons-math3-3.2-src/src/test/java/org/apache/commons/math3/complex/RootsOfUnityTest.java100644 1750 1750 7102 12126627672 30360 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.complex; import org.apache.commons.math3.exception.MathIllegalStateException; import org.apache.commons.math3.exception.ZeroException; import org.apache.commons.math3.util.FastMath; import org.junit.Assert; import org.junit.Test; /** * Unit tests for the {@link RootsOfUnity} class. * * @version $Id: RootsOfUnityTest.java 1244107 2012-02-14 16:17:55Z erans $ */ public class RootsOfUnityTest { @Test(expected = MathIllegalStateException.class) public void testMathIllegalState1() { final RootsOfUnity roots = new RootsOfUnity(); roots.getReal(0); } @Test(expected = MathIllegalStateException.class) public void testMathIllegalState2() { final RootsOfUnity roots = new RootsOfUnity(); roots.getImaginary(0); } @Test(expected = MathIllegalStateException.class) public void testMathIllegalState3() { final RootsOfUnity roots = new RootsOfUnity(); roots.isCounterClockWise(); } @Test(expected = ZeroException.class) public void testZeroNumberOfRoots() { final RootsOfUnity roots = new RootsOfUnity(); roots.computeRoots(0); } @Test public void testGetNumberOfRoots() { final RootsOfUnity roots = new RootsOfUnity(); Assert.assertEquals("", 0, roots.getNumberOfRoots()); roots.computeRoots(5); Assert.assertEquals("", 5, roots.getNumberOfRoots()); /* * Testing -5 right after 5 is important, as the roots in this case are * not recomputed. */ roots.computeRoots(-5); Assert.assertEquals("", 5, roots.getNumberOfRoots()); roots.computeRoots(6); Assert.assertEquals("", 6, roots.getNumberOfRoots()); } @Test public void testComputeRoots() { final RootsOfUnity roots = new RootsOfUnity(); for (int n = -10; n < 11; n++) { /* * Testing -n right after n is important, as the roots in this case * are not recomputed. */ if (n != 0) { roots.computeRoots(n); doTestComputeRoots(roots); roots.computeRoots(-n); doTestComputeRoots(roots); } } } private void doTestComputeRoots(final RootsOfUnity roots) { final int n = roots.isCounterClockWise() ? roots.getNumberOfRoots() : -roots.getNumberOfRoots(); final double tol = 10 * Math.ulp(1.0); for (int k = 0; k < n; k++) { final double t = 2.0 * FastMath.PI * k / n; final String msg = String.format("n = %d, k = %d", n, k); Assert.assertEquals(msg, FastMath.cos(t), roots.getReal(k), tol); Assert.assertEquals(msg, FastMath.sin(t), roots.getImaginary(k), tol); } } } commons-math3-3.2-src/src/test/java/org/apache/commons/math3/complex/QuaternionTest.java100644 1750 1750 42437 12126627672 30113 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.complex; import java.util.Random; import org.apache.commons.math3.complex.Quaternion; import org.apache.commons.math3.exception.DimensionMismatchException; import org.apache.commons.math3.exception.ZeroException; import org.apache.commons.math3.geometry.euclidean.threed.Rotation; import org.apache.commons.math3.geometry.euclidean.threed.Vector3D; import org.apache.commons.math3.util.FastMath; import org.junit.Test; import org.junit.Assert; public class QuaternionTest { /** Epsilon for double comparison. */ private static final double EPS = Math.ulp(1d); /** Epsilon for double comparison. */ private static final double COMPARISON_EPS = 1e-14; @Test public final void testAccessors1() { final double q0 = 2; final double q1 = 5.4; final double q2 = 17; final double q3 = 0.0005; final Quaternion q = new Quaternion(q0, q1, q2, q3); Assert.assertEquals(q0, q.getQ0(), 0); Assert.assertEquals(q1, q.getQ1(), 0); Assert.assertEquals(q2, q.getQ2(), 0); Assert.assertEquals(q3, q.getQ3(), 0); } @Test public final void testAccessors2() { final double q0 = 2; final double q1 = 5.4; final double q2 = 17; final double q3 = 0.0005; final Quaternion q = new Quaternion(q0, q1, q2, q3); final double sP = q.getScalarPart(); final double[] vP = q.getVectorPart(); Assert.assertEquals(q0, sP, 0); Assert.assertEquals(q1, vP[0], 0); Assert.assertEquals(q2, vP[1], 0); Assert.assertEquals(q3, vP[2], 0); } @Test public final void testAccessors3() { final double q0 = 2; final double q1 = 5.4; final double q2 = 17; final double q3 = 0.0005; final Quaternion q = new Quaternion(q0, new double[] { q1, q2, q3 }); final double sP = q.getScalarPart(); final double[] vP = q.getVectorPart(); Assert.assertEquals(q0, sP, 0); Assert.assertEquals(q1, vP[0], 0); Assert.assertEquals(q2, vP[1], 0); Assert.assertEquals(q3, vP[2], 0); } @Test(expected=DimensionMismatchException.class) public void testWrongDimension() { new Quaternion(new double[] { 1, 2 }); } @Test public final void testConjugate() { final double q0 = 2; final double q1 = 5.4; final double q2 = 17; final double q3 = 0.0005; final Quaternion q = new Quaternion(q0, q1, q2, q3); final Quaternion qConjugate = q.getConjugate(); Assert.assertEquals(q0, qConjugate.getQ0(), 0); Assert.assertEquals(-q1, qConjugate.getQ1(), 0); Assert.assertEquals(-q2, qConjugate.getQ2(), 0); Assert.assertEquals(-q3, qConjugate.getQ3(), 0); } @Test public final void testProductQuaternionQuaternion() { // Case : analytic test case final Quaternion qA = new Quaternion(1, 0.5, -3, 4); final Quaternion qB = new Quaternion(6, 2, 1, -9); final Quaternion qResult = Quaternion.multiply(qA, qB); Assert.assertEquals(44, qResult.getQ0(), EPS); Assert.assertEquals(28, qResult.getQ1(), EPS); Assert.assertEquals(-4.5, qResult.getQ2(), EPS); Assert.assertEquals(21.5, qResult.getQ3(), EPS); // comparison with the result given by the formula : // qResult = (scalarA * scalarB - vectorA . vectorB) + (scalarA * vectorB + scalarB * vectorA + vectorA ^ // vectorB) final Vector3D vectorA = new Vector3D(qA.getVectorPart()); final Vector3D vectorB = new Vector3D(qB.getVectorPart()); final Vector3D vectorResult = new Vector3D(qResult.getVectorPart()); final double scalarPartRef = qA.getScalarPart() * qB.getScalarPart() - Vector3D.dotProduct(vectorA, vectorB); Assert.assertEquals(scalarPartRef, qResult.getScalarPart(), EPS); final Vector3D vectorPartRef = ((vectorA.scalarMultiply(qB.getScalarPart())).add(vectorB.scalarMultiply(qA .getScalarPart()))).add(Vector3D.crossProduct(vectorA, vectorB)); final double norm = (vectorResult.subtract(vectorPartRef)).getNorm(); Assert.assertEquals(0, norm, EPS); // Conjugate of the product of two quaternions and product of their conjugates : // Conj(qA * qB) = Conj(qB) * Conj(qA) final Quaternion conjugateOfProduct = qB.getConjugate().multiply(qA.getConjugate()); final Quaternion productOfConjugate = (qA.multiply(qB)).getConjugate(); Assert.assertEquals(conjugateOfProduct.getQ0(), productOfConjugate.getQ0(), EPS); Assert.assertEquals(conjugateOfProduct.getQ1(), productOfConjugate.getQ1(), EPS); Assert.assertEquals(conjugateOfProduct.getQ2(), productOfConjugate.getQ2(), EPS); Assert.assertEquals(conjugateOfProduct.getQ3(), productOfConjugate.getQ3(), EPS); } @Test public final void testProductQuaternionVector() { // Case : Product between a vector and a quaternion : QxV final Quaternion quaternion = new Quaternion(4, 7, -1, 2); final double[] vector = {2.0, 1.0, 3.0}; final Quaternion qResultQxV = Quaternion.multiply(quaternion, new Quaternion(vector)); Assert.assertEquals(-19, qResultQxV.getQ0(), EPS); Assert.assertEquals(3, qResultQxV.getQ1(), EPS); Assert.assertEquals(-13, qResultQxV.getQ2(), EPS); Assert.assertEquals(21, qResultQxV.getQ3(), EPS); // comparison with the result given by the formula : // qResult = (- vectorQ . vector) + (scalarQ * vector + vectorQ ^ vector) final double[] vectorQ = quaternion.getVectorPart(); final double[] vectorResultQxV = qResultQxV.getVectorPart(); final double scalarPartRefQxV = -Vector3D.dotProduct(new Vector3D(vectorQ), new Vector3D(vector)); Assert.assertEquals(scalarPartRefQxV, qResultQxV.getScalarPart(), EPS); final Vector3D vectorPartRefQxV = (new Vector3D(vector).scalarMultiply(quaternion.getScalarPart())).add(Vector3D .crossProduct(new Vector3D(vectorQ), new Vector3D(vector))); final double normQxV = (new Vector3D(vectorResultQxV).subtract(vectorPartRefQxV)).getNorm(); Assert.assertEquals(0, normQxV, EPS); // Case : Product between a vector and a quaternion : VxQ final Quaternion qResultVxQ = Quaternion.multiply(new Quaternion(vector), quaternion); Assert.assertEquals(-19, qResultVxQ.getQ0(), EPS); Assert.assertEquals(13, qResultVxQ.getQ1(), EPS); Assert.assertEquals(21, qResultVxQ.getQ2(), EPS); Assert.assertEquals(3, qResultVxQ.getQ3(), EPS); final double[] vectorResultVxQ = qResultVxQ.getVectorPart(); // comparison with the result given by the formula : // qResult = (- vector . vectorQ) + (scalarQ * vector + vector ^ vectorQ) final double scalarPartRefVxQ = -Vector3D.dotProduct(new Vector3D(vectorQ), new Vector3D(vector)); Assert.assertEquals(scalarPartRefVxQ, qResultVxQ.getScalarPart(), EPS); final Vector3D vectorPartRefVxQ = (new Vector3D(vector).scalarMultiply(quaternion.getScalarPart())).add(Vector3D .crossProduct(new Vector3D(vector), new Vector3D(vectorQ))); final double normVxQ = (new Vector3D(vectorResultVxQ).subtract(vectorPartRefVxQ)).getNorm(); Assert.assertEquals(0, normVxQ, EPS); } @Test public final void testDotProductQuaternionQuaternion() { // expected output final double expected = -6.; // inputs final Quaternion q1 = new Quaternion(1, 2, 2, 1); final Quaternion q2 = new Quaternion(3, -2, -1, -3); final double actual1 = Quaternion.dotProduct(q1, q2); final double actual2 = q1.dotProduct(q2); Assert.assertEquals(expected, actual1, EPS); Assert.assertEquals(expected, actual2, EPS); } @Test public final void testScalarMultiplyDouble() { // expected outputs final double w = 1.6; final double x = -4.8; final double y = 11.20; final double z = 2.56; // inputs final Quaternion q1 = new Quaternion(0.5, -1.5, 3.5, 0.8); final double a = 3.2; final Quaternion q = q1.multiply(a); Assert.assertEquals(w, q.getQ0(), COMPARISON_EPS); Assert.assertEquals(x, q.getQ1(), COMPARISON_EPS); Assert.assertEquals(y, q.getQ2(), COMPARISON_EPS); Assert.assertEquals(z, q.getQ3(), COMPARISON_EPS); } @Test public final void testAddQuaternionQuaternion() { // expected outputs final double w = 4; final double x = -1; final double y = 2; final double z = -4; // inputs final Quaternion q1 = new Quaternion(1., 2., -2., -1.); final Quaternion q2 = new Quaternion(3., -3., 4., -3.); final Quaternion qa = Quaternion.add(q1, q2); final Quaternion qb = q1.add(q2); Assert.assertEquals(w, qa.getQ0(), EPS); Assert.assertEquals(x, qa.getQ1(), EPS); Assert.assertEquals(y, qa.getQ2(), EPS); Assert.assertEquals(z, qa.getQ3(), EPS); Assert.assertEquals(w, qb.getQ0(), EPS); Assert.assertEquals(x, qb.getQ1(), EPS); Assert.assertEquals(y, qb.getQ2(), EPS); Assert.assertEquals(z, qb.getQ3(), EPS); } @Test public final void testSubtractQuaternionQuaternion() { // expected outputs final double w = -2.; final double x = 5.; final double y = -6.; final double z = 2.; // inputs final Quaternion q1 = new Quaternion(1., 2., -2., -1.); final Quaternion q2 = new Quaternion(3., -3., 4., -3.); final Quaternion qa = Quaternion.subtract(q1, q2); final Quaternion qb = q1.subtract(q2); Assert.assertEquals(w, qa.getQ0(), EPS); Assert.assertEquals(x, qa.getQ1(), EPS); Assert.assertEquals(y, qa.getQ2(), EPS); Assert.assertEquals(z, qa.getQ3(), EPS); Assert.assertEquals(w, qb.getQ0(), EPS); Assert.assertEquals(x, qb.getQ1(), EPS); Assert.assertEquals(y, qb.getQ2(), EPS); Assert.assertEquals(z, qb.getQ3(), EPS); } @Test public final void testNorm() { final double q0 = 2; final double q1 = 1; final double q2 = -4; final double q3 = 3; final Quaternion q = new Quaternion(q0, q1, q2, q3); final double norm = q.getNorm(); Assert.assertEquals(Math.sqrt(30), norm, 0); final double normSquareRef = Quaternion.multiply(q, q.getConjugate()).getScalarPart(); Assert.assertEquals(Math.sqrt(normSquareRef), norm, 0); } @Test public final void testNormalize() { final Quaternion q = new Quaternion(2, 1, -4, -2); final Quaternion versor = q.normalize(); Assert.assertEquals(2.0 / 5.0, versor.getQ0(), 0); Assert.assertEquals(1.0 / 5.0, versor.getQ1(), 0); Assert.assertEquals(-4.0 / 5.0, versor.getQ2(), 0); Assert.assertEquals(-2.0 / 5.0, versor.getQ3(), 0); Assert.assertEquals(1, versor.getNorm(), 0); } @Test(expected=ZeroException.class) public final void testNormalizeFail() { final Quaternion zeroQ = new Quaternion(0, 0, 0, 0); zeroQ.normalize(); } @Test public final void testObjectEquals() { final double one = 1; final Quaternion q1 = new Quaternion(one, one, one, one); Assert.assertTrue(q1.equals(q1)); final Quaternion q2 = new Quaternion(one, one, one, one); Assert.assertTrue(q2.equals(q1)); final Quaternion q3 = new Quaternion(one, FastMath.nextUp(one), one, one); Assert.assertFalse(q3.equals(q1)); } @Test public final void testQuaternionEquals() { final double inc = 1e-5; final Quaternion q1 = new Quaternion(2, 1, -4, -2); final Quaternion q2 = new Quaternion(q1.getQ0() + inc, q1.getQ1(), q1.getQ2(), q1.getQ3()); final Quaternion q3 = new Quaternion(q1.getQ0(), q1.getQ1() + inc, q1.getQ2(), q1.getQ3()); final Quaternion q4 = new Quaternion(q1.getQ0(), q1.getQ1(), q1.getQ2() + inc, q1.getQ3()); final Quaternion q5 = new Quaternion(q1.getQ0(), q1.getQ1(), q1.getQ2(), q1.getQ3() + inc); Assert.assertFalse(q1.equals(q2, 0.9 * inc)); Assert.assertFalse(q1.equals(q3, 0.9 * inc)); Assert.assertFalse(q1.equals(q4, 0.9 * inc)); Assert.assertFalse(q1.equals(q5, 0.9 * inc)); Assert.assertTrue(q1.equals(q2, 1.1 * inc)); Assert.assertTrue(q1.equals(q3, 1.1 * inc)); Assert.assertTrue(q1.equals(q4, 1.1 * inc)); Assert.assertTrue(q1.equals(q5, 1.1 * inc)); } @Test public final void testQuaternionEquals2() { final Quaternion q1 = new Quaternion(1, 4, 2, 3); final double gap = 1e-5; final Quaternion q2 = new Quaternion(1 + gap, 4 + gap, 2 + gap, 3 + gap); Assert.assertTrue(q1.equals(q2, 10 * gap)); Assert.assertFalse(q1.equals(q2, gap)); Assert.assertFalse(q1.equals(q2, gap / 10)); } @Test public final void testIsUnitQuaternion() { final Random r = new Random(48); final int numberOfTrials = 1000; for (int i = 0; i < numberOfTrials; i++) { final Quaternion q1 = new Quaternion(r.nextDouble(), r.nextDouble(), r.nextDouble(), r.nextDouble()); final Quaternion q2 = q1.normalize(); Assert.assertTrue(q2.isUnitQuaternion(COMPARISON_EPS)); } final Quaternion q = new Quaternion(1, 1, 1, 1); Assert.assertFalse(q.isUnitQuaternion(COMPARISON_EPS)); } @Test public final void testIsPureQuaternion() { final Quaternion q1 = new Quaternion(0, 5, 4, 8); Assert.assertTrue(q1.isPureQuaternion(EPS)); final Quaternion q2 = new Quaternion(0 - EPS, 5, 4, 8); Assert.assertTrue(q2.isPureQuaternion(EPS)); final Quaternion q3 = new Quaternion(0 - 1.1 * EPS, 5, 4, 8); Assert.assertFalse(q3.isPureQuaternion(EPS)); final Random r = new Random(48); final double[] v = {r.nextDouble(), r.nextDouble(), r.nextDouble()}; final Quaternion q4 = new Quaternion(v); Assert.assertTrue(q4.isPureQuaternion(0)); final Quaternion q5 = new Quaternion(0, v); Assert.assertTrue(q5.isPureQuaternion(0)); } @Test public final void testPolarForm() { final Random r = new Random(48); final int numberOfTrials = 1000; for (int i = 0; i < numberOfTrials; i++) { final Quaternion q = new Quaternion(2 * (r.nextDouble() - 0.5), 2 * (r.nextDouble() - 0.5), 2 * (r.nextDouble() - 0.5), 2 * (r.nextDouble() - 0.5)); final Quaternion qP = q.getPositivePolarForm(); Assert.assertTrue(qP.isUnitQuaternion(COMPARISON_EPS)); Assert.assertTrue(qP.getQ0() >= 0); final Rotation rot = new Rotation(q.getQ0(), q.getQ1(), q.getQ2(), q.getQ3(), true); final Rotation rotP = new Rotation(qP.getQ0(), qP.getQ1(), qP.getQ2(), qP.getQ3(), true); Assert.assertEquals(rot.getAngle(), rotP.getAngle(), COMPARISON_EPS); Assert.assertEquals(rot.getAxis().getX(), rot.getAxis().getX(), COMPARISON_EPS); Assert.assertEquals(rot.getAxis().getY(), rot.getAxis().getY(), COMPARISON_EPS); Assert.assertEquals(rot.getAxis().getZ(), rot.getAxis().getZ(), COMPARISON_EPS); } } @Test public final void testGetInverse() { final Quaternion q = new Quaternion(1.5, 4, 2, -2.5); final Quaternion inverseQ = q.getInverse(); Assert.assertEquals(1.5 / 28.5, inverseQ.getQ0(), 0); Assert.assertEquals(-4.0 / 28.5, inverseQ.getQ1(), 0); Assert.assertEquals(-2.0 / 28.5, inverseQ.getQ2(), 0); Assert.assertEquals(2.5 / 28.5, inverseQ.getQ3(), 0); final Quaternion product = Quaternion.multiply(inverseQ, q); Assert.assertEquals(1, product.getQ0(), EPS); Assert.assertEquals(0, product.getQ1(), EPS); Assert.assertEquals(0, product.getQ2(), EPS); Assert.assertEquals(0, product.getQ3(), EPS); final Quaternion qNul = new Quaternion(0, 0, 0, 0); try { final Quaternion inverseQNul = qNul.getInverse(); Assert.fail("expecting ZeroException but got : " + inverseQNul); } catch (ZeroException ex) { // expected } } @Test public final void testToString() { final Quaternion q = new Quaternion(1, 2, 3, 4); Assert.assertTrue(q.toString().equals("[1.0 2.0 3.0 4.0]")); } } commons-math3-3.2-src/src/test/java/org/apache/commons/math3/complex/ComplexTest.java100644 1750 1750 132575 12126627672 27420 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.complex; import org.apache.commons.math3.TestUtils; import org.apache.commons.math3.exception.NullArgumentException; import org.apache.commons.math3.util.FastMath; import org.junit.Assert; import org.junit.Test; import java.util.List; /** * @version $Id: ComplexTest.java 1459927 2013-03-22 18:55:58Z luc $ */ public class ComplexTest { private double inf = Double.POSITIVE_INFINITY; private double neginf = Double.NEGATIVE_INFINITY; private double nan = Double.NaN; private double pi = FastMath.PI; private Complex oneInf = new Complex(1, inf); private Complex oneNegInf = new Complex(1, neginf); private Complex infOne = new Complex(inf, 1); private Complex infZero = new Complex(inf, 0); private Complex infNaN = new Complex(inf, nan); private Complex infNegInf = new Complex(inf, neginf); private Complex infInf = new Complex(inf, inf); private Complex negInfInf = new Complex(neginf, inf); private Complex negInfZero = new Complex(neginf, 0); private Complex negInfOne = new Complex(neginf, 1); private Complex negInfNaN = new Complex(neginf, nan); private Complex negInfNegInf = new Complex(neginf, neginf); private Complex oneNaN = new Complex(1, nan); private Complex zeroInf = new Complex(0, inf); private Complex zeroNaN = new Complex(0, nan); private Complex nanInf = new Complex(nan, inf); private Complex nanNegInf = new Complex(nan, neginf); private Complex nanZero = new Complex(nan, 0); @Test public void testConstructor() { Complex z = new Complex(3.0, 4.0); Assert.assertEquals(3.0, z.getReal(), 1.0e-5); Assert.assertEquals(4.0, z.getImaginary(), 1.0e-5); } @Test public void testConstructorNaN() { Complex z = new Complex(3.0, Double.NaN); Assert.assertTrue(z.isNaN()); z = new Complex(nan, 4.0); Assert.assertTrue(z.isNaN()); z = new Complex(3.0, 4.0); Assert.assertFalse(z.isNaN()); } @Test public void testAbs() { Complex z = new Complex(3.0, 4.0); Assert.assertEquals(5.0, z.abs(), 1.0e-5); } @Test public void testAbsNaN() { Assert.assertTrue(Double.isNaN(Complex.NaN.abs())); Complex z = new Complex(inf, nan); Assert.assertTrue(Double.isNaN(z.abs())); } @Test public void testAbsInfinite() { Complex z = new Complex(inf, 0); Assert.assertEquals(inf, z.abs(), 0); z = new Complex(0, neginf); Assert.assertEquals(inf, z.abs(), 0); z = new Complex(inf, neginf); Assert.assertEquals(inf, z.abs(), 0); } @Test public void testAdd() { Complex x = new Complex(3.0, 4.0); Complex y = new Complex(5.0, 6.0); Complex z = x.add(y); Assert.assertEquals(8.0, z.getReal(), 1.0e-5); Assert.assertEquals(10.0, z.getImaginary(), 1.0e-5); } @Test public void testAddNaN() { Complex x = new Complex(3.0, 4.0); Complex z = x.add(Complex.NaN); Assert.assertSame(Complex.NaN, z); z = new Complex(1, nan); Complex w = x.add(z); Assert.assertSame(Complex.NaN, w); } @Test public void testAddInf() { Complex x = new Complex(1, 1); Complex z = new Complex(inf, 0); Complex w = x.add(z); Assert.assertEquals(w.getImaginary(), 1, 0); Assert.assertEquals(inf, w.getReal(), 0); x = new Complex(neginf, 0); Assert.assertTrue(Double.isNaN(x.add(z).getReal())); } @Test public void testScalarAdd() { Complex x = new Complex(3.0, 4.0); double yDouble = 2.0; Complex yComplex = new Complex(yDouble); Assert.assertEquals(x.add(yComplex), x.add(yDouble)); } @Test public void testScalarAddNaN() { Complex x = new Complex(3.0, 4.0); double yDouble = Double.NaN; Complex yComplex = new Complex(yDouble); Assert.assertEquals(x.add(yComplex), x.add(yDouble)); } @Test public void testScalarAddInf() { Complex x = new Complex(1, 1); double yDouble = Double.POSITIVE_INFINITY; Complex yComplex = new Complex(yDouble); Assert.assertEquals(x.add(yComplex), x.add(yDouble)); x = new Complex(neginf, 0); Assert.assertEquals(x.add(yComplex), x.add(yDouble)); } @Test public void testConjugate() { Complex x = new Complex(3.0, 4.0); Complex z = x.conjugate(); Assert.assertEquals(3.0, z.getReal(), 1.0e-5); Assert.assertEquals(-4.0, z.getImaginary(), 1.0e-5); } @Test public void testConjugateNaN() { Complex z = Complex.NaN.conjugate(); Assert.assertTrue(z.isNaN()); } @Test public void testConjugateInfiinite() { Complex z = new Complex(0, inf); Assert.assertEquals(neginf, z.conjugate().getImaginary(), 0); z = new Complex(0, neginf); Assert.assertEquals(inf, z.conjugate().getImaginary(), 0); } @Test public void testDivide() { Complex x = new Complex(3.0, 4.0); Complex y = new Complex(5.0, 6.0); Complex z = x.divide(y); Assert.assertEquals(39.0 / 61.0, z.getReal(), 1.0e-5); Assert.assertEquals(2.0 / 61.0, z.getImaginary(), 1.0e-5); } @Test public void testDivideReal() { Complex x = new Complex(2d, 3d); Complex y = new Complex(2d, 0d); Assert.assertEquals(new Complex(1d, 1.5), x.divide(y)); } @Test public void testDivideImaginary() { Complex x = new Complex(2d, 3d); Complex y = new Complex(0d, 2d); Assert.assertEquals(new Complex(1.5d, -1d), x.divide(y)); } @Test public void testDivideInf() { Complex x = new Complex(3, 4); Complex w = new Complex(neginf, inf); Assert.assertTrue(x.divide(w).equals(Complex.ZERO)); Complex z = w.divide(x); Assert.assertTrue(Double.isNaN(z.getReal())); Assert.assertEquals(inf, z.getImaginary(), 0); w = new Complex(inf, inf); z = w.divide(x); Assert.assertTrue(Double.isNaN(z.getImaginary())); Assert.assertEquals(inf, z.getReal(), 0); w = new Complex(1, inf); z = w.divide(w); Assert.assertTrue(Double.isNaN(z.getReal())); Assert.assertTrue(Double.isNaN(z.getImaginary())); } @Test public void testDivideZero() { Complex x = new Complex(3.0, 4.0); Complex z = x.divide(Complex.ZERO); // Assert.assertEquals(z, Complex.INF); // See MATH-657 Assert.assertEquals(z, Complex.NaN); } @Test public void testDivideZeroZero() { Complex x = new Complex(0.0, 0.0); Complex z = x.divide(Complex.ZERO); Assert.assertEquals(z, Complex.NaN); } @Test public void testDivideNaN() { Complex x = new Complex(3.0, 4.0); Complex z = x.divide(Complex.NaN); Assert.assertTrue(z.isNaN()); } @Test public void testDivideNaNInf() { Complex z = oneInf.divide(Complex.ONE); Assert.assertTrue(Double.isNaN(z.getReal())); Assert.assertEquals(inf, z.getImaginary(), 0); z = negInfNegInf.divide(oneNaN); Assert.assertTrue(Double.isNaN(z.getReal())); Assert.assertTrue(Double.isNaN(z.getImaginary())); z = negInfInf.divide(Complex.ONE); Assert.assertTrue(Double.isNaN(z.getReal())); Assert.assertTrue(Double.isNaN(z.getImaginary())); } @Test public void testScalarDivide() { Complex x = new Complex(3.0, 4.0); double yDouble = 2.0; Complex yComplex = new Complex(yDouble); Assert.assertEquals(x.divide(yComplex), x.divide(yDouble)); } @Test public void testScalarDivideNaN() { Complex x = new Complex(3.0, 4.0); double yDouble = Double.NaN; Complex yComplex = new Complex(yDouble); Assert.assertEquals(x.divide(yComplex), x.divide(yDouble)); } @Test public void testScalarDivideInf() { Complex x = new Complex(1,1); double yDouble = Double.POSITIVE_INFINITY; Complex yComplex = new Complex(yDouble); TestUtils.assertEquals(x.divide(yComplex), x.divide(yDouble), 0); yDouble = Double.NEGATIVE_INFINITY; yComplex = new Complex(yDouble); TestUtils.assertEquals(x.divide(yComplex), x.divide(yDouble), 0); x = new Complex(1, Double.NEGATIVE_INFINITY); TestUtils.assertEquals(x.divide(yComplex), x.divide(yDouble), 0); } @Test public void testScalarDivideZero() { Complex x = new Complex(1,1); TestUtils.assertEquals(x.divide(Complex.ZERO), x.divide(0), 0); } @Test public void testReciprocal() { Complex z = new Complex(5.0, 6.0); Complex act = z.reciprocal(); double expRe = 5.0 / 61.0; double expIm = -6.0 / 61.0; Assert.assertEquals(expRe, act.getReal(), FastMath.ulp(expRe)); Assert.assertEquals(expIm, act.getImaginary(), FastMath.ulp(expIm)); } @Test public void testReciprocalReal() { Complex z = new Complex(-2.0, 0.0); Assert.assertEquals(new Complex(-0.5, 0.0), z.reciprocal()); } @Test public void testReciprocalImaginary() { Complex z = new Complex(0.0, -2.0); Assert.assertEquals(new Complex(0.0, 0.5), z.reciprocal()); } @Test public void testReciprocalInf() { Complex z = new Complex(neginf, inf); Assert.assertTrue(z.reciprocal().equals(Complex.ZERO)); z = new Complex(1, inf).reciprocal(); Assert.assertEquals(z, Complex.ZERO); } @Test public void testReciprocalZero() { Assert.assertEquals(Complex.ZERO.reciprocal(), Complex.INF); } @Test public void testReciprocalNaN() { Assert.assertTrue(Complex.NaN.reciprocal().isNaN()); } @Test public void testMultiply() { Complex x = new Complex(3.0, 4.0); Complex y = new Complex(5.0, 6.0); Complex z = x.multiply(y); Assert.assertEquals(-9.0, z.getReal(), 1.0e-5); Assert.assertEquals(38.0, z.getImaginary(), 1.0e-5); } @Test public void testMultiplyNaN() { Complex x = new Complex(3.0, 4.0); Complex z = x.multiply(Complex.NaN); Assert.assertSame(Complex.NaN, z); z = Complex.NaN.multiply(5); Assert.assertSame(Complex.NaN, z); } @Test public void testMultiplyInfInf() { // Assert.assertTrue(infInf.multiply(infInf).isNaN()); // MATH-620 Assert.assertTrue(infInf.multiply(infInf).isInfinite()); } @Test public void testMultiplyNaNInf() { Complex z = new Complex(1,1); Complex w = z.multiply(infOne); Assert.assertEquals(w.getReal(), inf, 0); Assert.assertEquals(w.getImaginary(), inf, 0); // [MATH-164] Assert.assertTrue(new Complex( 1,0).multiply(infInf).equals(Complex.INF)); Assert.assertTrue(new Complex(-1,0).multiply(infInf).equals(Complex.INF)); Assert.assertTrue(new Complex( 1,0).multiply(negInfZero).equals(Complex.INF)); w = oneInf.multiply(oneNegInf); Assert.assertEquals(w.getReal(), inf, 0); Assert.assertEquals(w.getImaginary(), inf, 0); w = negInfNegInf.multiply(oneNaN); Assert.assertTrue(Double.isNaN(w.getReal())); Assert.assertTrue(Double.isNaN(w.getImaginary())); z = new Complex(1, neginf); Assert.assertSame(Complex.INF, z.multiply(z)); } @Test public void testScalarMultiply() { Complex x = new Complex(3.0, 4.0); double yDouble = 2.0; Complex yComplex = new Complex(yDouble); Assert.assertEquals(x.multiply(yComplex), x.multiply(yDouble)); int zInt = -5; Complex zComplex = new Complex(zInt); Assert.assertEquals(x.multiply(zComplex), x.multiply(zInt)); } @Test public void testScalarMultiplyNaN() { Complex x = new Complex(3.0, 4.0); double yDouble = Double.NaN; Complex yComplex = new Complex(yDouble); Assert.assertEquals(x.multiply(yComplex), x.multiply(yDouble)); } @Test public void testScalarMultiplyInf() { Complex x = new Complex(1, 1); double yDouble = Double.POSITIVE_INFINITY; Complex yComplex = new Complex(yDouble); Assert.assertEquals(x.multiply(yComplex), x.multiply(yDouble)); yDouble = Double.NEGATIVE_INFINITY; yComplex = new Complex(yDouble); Assert.assertEquals(x.multiply(yComplex), x.multiply(yDouble)); } @Test public void testNegate() { Complex x = new Complex(3.0, 4.0); Complex z = x.negate(); Assert.assertEquals(-3.0, z.getReal(), 1.0e-5); Assert.assertEquals(-4.0, z.getImaginary(), 1.0e-5); } @Test public void testNegateNaN() { Complex z = Complex.NaN.negate(); Assert.assertTrue(z.isNaN()); } @Test public void testSubtract() { Complex x = new Complex(3.0, 4.0); Complex y = new Complex(5.0, 6.0); Complex z = x.subtract(y); Assert.assertEquals(-2.0, z.getReal(), 1.0e-5); Assert.assertEquals(-2.0, z.getImaginary(), 1.0e-5); } @Test public void testSubtractNaN() { Complex x = new Complex(3.0, 4.0); Complex z = x.subtract(Complex.NaN); Assert.assertSame(Complex.NaN, z); z = new Complex(1, nan); Complex w = x.subtract(z); Assert.assertSame(Complex.NaN, w); } @Test public void testSubtractInf() { Complex x = new Complex(1, 1); Complex z = new Complex(neginf, 0); Complex w = x.subtract(z); Assert.assertEquals(w.getImaginary(), 1, 0); Assert.assertEquals(inf, w.getReal(), 0); x = new Complex(neginf, 0); Assert.assertTrue(Double.isNaN(x.subtract(z).getReal())); } @Test public void testScalarSubtract() { Complex x = new Complex(3.0, 4.0); double yDouble = 2.0; Complex yComplex = new Complex(yDouble); Assert.assertEquals(x.subtract(yComplex), x.subtract(yDouble)); } @Test public void testScalarSubtractNaN() { Complex x = new Complex(3.0, 4.0); double yDouble = Double.NaN; Complex yComplex = new Complex(yDouble); Assert.assertEquals(x.subtract(yComplex), x.subtract(yDouble)); } @Test public void testScalarSubtractInf() { Complex x = new Complex(1, 1); double yDouble = Double.POSITIVE_INFINITY; Complex yComplex = new Complex(yDouble); Assert.assertEquals(x.subtract(yComplex), x.subtract(yDouble)); x = new Complex(neginf, 0); Assert.assertEquals(x.subtract(yComplex), x.subtract(yDouble)); } @Test public void testEqualsNull() { Complex x = new Complex(3.0, 4.0); Assert.assertFalse(x.equals(null)); } @Test public void testEqualsClass() { Complex x = new Complex(3.0, 4.0); Assert.assertFalse(x.equals(this)); } @Test public void testEqualsSame() { Complex x = new Complex(3.0, 4.0); Assert.assertTrue(x.equals(x)); } @Test public void testEqualsTrue() { Complex x = new Complex(3.0, 4.0); Complex y = new Complex(3.0, 4.0); Assert.assertTrue(x.equals(y)); } @Test public void testEqualsRealDifference() { Complex x = new Complex(0.0, 0.0); Complex y = new Complex(0.0 + Double.MIN_VALUE, 0.0); Assert.assertFalse(x.equals(y)); } @Test public void testEqualsImaginaryDifference() { Complex x = new Complex(0.0, 0.0); Complex y = new Complex(0.0, 0.0 + Double.MIN_VALUE); Assert.assertFalse(x.equals(y)); } @Test public void testEqualsNaN() { Complex realNaN = new Complex(Double.NaN, 0.0); Complex imaginaryNaN = new Complex(0.0, Double.NaN); Complex complexNaN = Complex.NaN; Assert.assertTrue(realNaN.equals(imaginaryNaN)); Assert.assertTrue(imaginaryNaN.equals(complexNaN)); Assert.assertTrue(realNaN.equals(complexNaN)); } @Test public void testHashCode() { Complex x = new Complex(0.0, 0.0); Complex y = new Complex(0.0, 0.0 + Double.MIN_VALUE); Assert.assertFalse(x.hashCode()==y.hashCode()); y = new Complex(0.0 + Double.MIN_VALUE, 0.0); Assert.assertFalse(x.hashCode()==y.hashCode()); Complex realNaN = new Complex(Double.NaN, 0.0); Complex imaginaryNaN = new Complex(0.0, Double.NaN); Assert.assertEquals(realNaN.hashCode(), imaginaryNaN.hashCode()); Assert.assertEquals(imaginaryNaN.hashCode(), Complex.NaN.hashCode()); } @Test public void testAcos() { Complex z = new Complex(3, 4); Complex expected = new Complex(0.936812, -2.30551); TestUtils.assertEquals(expected, z.acos(), 1.0e-5); TestUtils.assertEquals(new Complex(FastMath.acos(0), 0), Complex.ZERO.acos(), 1.0e-12); } @Test public void testAcosInf() { TestUtils.assertSame(Complex.NaN, oneInf.acos()); TestUtils.assertSame(Complex.NaN, oneNegInf.acos()); TestUtils.assertSame(Complex.NaN, infOne.acos()); TestUtils.assertSame(Complex.NaN, negInfOne.acos()); TestUtils.assertSame(Complex.NaN, infInf.acos()); TestUtils.assertSame(Complex.NaN, infNegInf.acos()); TestUtils.assertSame(Complex.NaN, negInfInf.acos()); TestUtils.assertSame(Complex.NaN, negInfNegInf.acos()); } @Test public void testAcosNaN() { Assert.assertTrue(Complex.NaN.acos().isNaN()); } @Test public void testAsin() { Complex z = new Complex(3, 4); Complex expected = new Complex(0.633984, 2.30551); TestUtils.assertEquals(expected, z.asin(), 1.0e-5); } @Test public void testAsinNaN() { Assert.assertTrue(Complex.NaN.asin().isNaN()); } @Test public void testAsinInf() { TestUtils.assertSame(Complex.NaN, oneInf.asin()); TestUtils.assertSame(Complex.NaN, oneNegInf.asin()); TestUtils.assertSame(Complex.NaN, infOne.asin()); TestUtils.assertSame(Complex.NaN, negInfOne.asin()); TestUtils.assertSame(Complex.NaN, infInf.asin()); TestUtils.assertSame(Complex.NaN, infNegInf.asin()); TestUtils.assertSame(Complex.NaN, negInfInf.asin()); TestUtils.assertSame(Complex.NaN, negInfNegInf.asin()); } @Test public void testAtan() { Complex z = new Complex(3, 4); Complex expected = new Complex(1.44831, 0.158997); TestUtils.assertEquals(expected, z.atan(), 1.0e-5); } @Test public void testAtanInf() { TestUtils.assertSame(Complex.NaN, oneInf.atan()); TestUtils.assertSame(Complex.NaN, oneNegInf.atan()); TestUtils.assertSame(Complex.NaN, infOne.atan()); TestUtils.assertSame(Complex.NaN, negInfOne.atan()); TestUtils.assertSame(Complex.NaN, infInf.atan()); TestUtils.assertSame(Complex.NaN, infNegInf.atan()); TestUtils.assertSame(Complex.NaN, negInfInf.atan()); TestUtils.assertSame(Complex.NaN, negInfNegInf.atan()); } @Test public void testAtanI() { Assert.assertTrue(Complex.I.atan().isNaN()); } @Test public void testAtanNaN() { Assert.assertTrue(Complex.NaN.atan().isNaN()); } @Test public void testCos() { Complex z = new Complex(3, 4); Complex expected = new Complex(-27.03495, -3.851153); TestUtils.assertEquals(expected, z.cos(), 1.0e-5); } @Test public void testCosNaN() { Assert.assertTrue(Complex.NaN.cos().isNaN()); } @Test public void testCosInf() { TestUtils.assertSame(infNegInf, oneInf.cos()); TestUtils.assertSame(infInf, oneNegInf.cos()); TestUtils.assertSame(Complex.NaN, infOne.cos()); TestUtils.assertSame(Complex.NaN, negInfOne.cos()); TestUtils.assertSame(Complex.NaN, infInf.cos()); TestUtils.assertSame(Complex.NaN, infNegInf.cos()); TestUtils.assertSame(Complex.NaN, negInfInf.cos()); TestUtils.assertSame(Complex.NaN, negInfNegInf.cos()); } @Test public void testCosh() { Complex z = new Complex(3, 4); Complex expected = new Complex(-6.58066, -7.58155); TestUtils.assertEquals(expected, z.cosh(), 1.0e-5); } @Test public void testCoshNaN() { Assert.assertTrue(Complex.NaN.cosh().isNaN()); } @Test public void testCoshInf() { TestUtils.assertSame(Complex.NaN, oneInf.cosh()); TestUtils.assertSame(Complex.NaN, oneNegInf.cosh()); TestUtils.assertSame(infInf, infOne.cosh()); TestUtils.assertSame(infNegInf, negInfOne.cosh()); TestUtils.assertSame(Complex.NaN, infInf.cosh()); TestUtils.assertSame(Complex.NaN, infNegInf.cosh()); TestUtils.assertSame(Complex.NaN, negInfInf.cosh()); TestUtils.assertSame(Complex.NaN, negInfNegInf.cosh()); } @Test public void testExp() { Complex z = new Complex(3, 4); Complex expected = new Complex(-13.12878, -15.20078); TestUtils.assertEquals(expected, z.exp(), 1.0e-5); TestUtils.assertEquals(Complex.ONE, Complex.ZERO.exp(), 10e-12); Complex iPi = Complex.I.multiply(new Complex(pi,0)); TestUtils.assertEquals(Complex.ONE.negate(), iPi.exp(), 10e-12); } @Test public void testExpNaN() { Assert.assertTrue(Complex.NaN.exp().isNaN()); } @Test public void testExpInf() { TestUtils.assertSame(Complex.NaN, oneInf.exp()); TestUtils.assertSame(Complex.NaN, oneNegInf.exp()); TestUtils.assertSame(infInf, infOne.exp()); TestUtils.assertSame(Complex.ZERO, negInfOne.exp()); TestUtils.assertSame(Complex.NaN, infInf.exp()); TestUtils.assertSame(Complex.NaN, infNegInf.exp()); TestUtils.assertSame(Complex.NaN, negInfInf.exp()); TestUtils.assertSame(Complex.NaN, negInfNegInf.exp()); } @Test public void testLog() { Complex z = new Complex(3, 4); Complex expected = new Complex(1.60944, 0.927295); TestUtils.assertEquals(expected, z.log(), 1.0e-5); } @Test public void testLogNaN() { Assert.assertTrue(Complex.NaN.log().isNaN()); } @Test public void testLogInf() { TestUtils.assertEquals(new Complex(inf, pi / 2), oneInf.log(), 10e-12); TestUtils.assertEquals(new Complex(inf, -pi / 2), oneNegInf.log(), 10e-12); TestUtils.assertEquals(infZero, infOne.log(), 10e-12); TestUtils.assertEquals(new Complex(inf, pi), negInfOne.log(), 10e-12); TestUtils.assertEquals(new Complex(inf, pi / 4), infInf.log(), 10e-12); TestUtils.assertEquals(new Complex(inf, -pi / 4), infNegInf.log(), 10e-12); TestUtils.assertEquals(new Complex(inf, 3d * pi / 4), negInfInf.log(), 10e-12); TestUtils.assertEquals(new Complex(inf, - 3d * pi / 4), negInfNegInf.log(), 10e-12); } @Test public void testLogZero() { TestUtils.assertSame(negInfZero, Complex.ZERO.log()); } @Test public void testPow() { Complex x = new Complex(3, 4); Complex y = new Complex(5, 6); Complex expected = new Complex(-1.860893, 11.83677); TestUtils.assertEquals(expected, x.pow(y), 1.0e-5); } @Test public void testPowNaNBase() { Complex x = new Complex(3, 4); Assert.assertTrue(Complex.NaN.pow(x).isNaN()); } @Test public void testPowNaNExponent() { Complex x = new Complex(3, 4); Assert.assertTrue(x.pow(Complex.NaN).isNaN()); } @Test public void testPowInf() { TestUtils.assertSame(Complex.NaN,Complex.ONE.pow(oneInf)); TestUtils.assertSame(Complex.NaN,Complex.ONE.pow(oneNegInf)); TestUtils.assertSame(Complex.NaN,Complex.ONE.pow(infOne)); TestUtils.assertSame(Complex.NaN,Complex.ONE.pow(infInf)); TestUtils.assertSame(Complex.NaN,Complex.ONE.pow(infNegInf)); TestUtils.assertSame(Complex.NaN,Complex.ONE.pow(negInfInf)); TestUtils.assertSame(Complex.NaN,Complex.ONE.pow(negInfNegInf)); TestUtils.assertSame(Complex.NaN,infOne.pow(Complex.ONE)); TestUtils.assertSame(Complex.NaN,negInfOne.pow(Complex.ONE)); TestUtils.assertSame(Complex.NaN,infInf.pow(Complex.ONE)); TestUtils.assertSame(Complex.NaN,infNegInf.pow(Complex.ONE)); TestUtils.assertSame(Complex.NaN,negInfInf.pow(Complex.ONE)); TestUtils.assertSame(Complex.NaN,negInfNegInf.pow(Complex.ONE)); TestUtils.assertSame(Complex.NaN,negInfNegInf.pow(infNegInf)); TestUtils.assertSame(Complex.NaN,negInfNegInf.pow(negInfNegInf)); TestUtils.assertSame(Complex.NaN,negInfNegInf.pow(infInf)); TestUtils.assertSame(Complex.NaN,infInf.pow(infNegInf)); TestUtils.assertSame(Complex.NaN,infInf.pow(negInfNegInf)); TestUtils.assertSame(Complex.NaN,infInf.pow(infInf)); TestUtils.assertSame(Complex.NaN,infNegInf.pow(infNegInf)); TestUtils.assertSame(Complex.NaN,infNegInf.pow(negInfNegInf)); TestUtils.assertSame(Complex.NaN,infNegInf.pow(infInf)); } @Test public void testPowZero() { TestUtils.assertSame(Complex.NaN, Complex.ZERO.pow(Complex.ONE)); TestUtils.assertSame(Complex.NaN, Complex.ZERO.pow(Complex.ZERO)); TestUtils.assertSame(Complex.NaN, Complex.ZERO.pow(Complex.I)); TestUtils.assertEquals(Complex.ONE, Complex.ONE.pow(Complex.ZERO), 10e-12); TestUtils.assertEquals(Complex.ONE, Complex.I.pow(Complex.ZERO), 10e-12); TestUtils.assertEquals(Complex.ONE, new Complex(-1, 3).pow(Complex.ZERO), 10e-12); } @Test public void testScalarPow() { Complex x = new Complex(3, 4); double yDouble = 5.0; Complex yComplex = new Complex(yDouble); Assert.assertEquals(x.pow(yComplex), x.pow(yDouble)); } @Test public void testScalarPowNaNBase() { Complex x = Complex.NaN; double yDouble = 5.0; Complex yComplex = new Complex(yDouble); Assert.assertEquals(x.pow(yComplex), x.pow(yDouble)); } @Test public void testScalarPowNaNExponent() { Complex x = new Complex(3, 4); double yDouble = Double.NaN; Complex yComplex = new Complex(yDouble); Assert.assertEquals(x.pow(yComplex), x.pow(yDouble)); } @Test public void testScalarPowInf() { TestUtils.assertSame(Complex.NaN,Complex.ONE.pow(Double.POSITIVE_INFINITY)); TestUtils.assertSame(Complex.NaN,Complex.ONE.pow(Double.NEGATIVE_INFINITY)); TestUtils.assertSame(Complex.NaN,infOne.pow(1.0)); TestUtils.assertSame(Complex.NaN,negInfOne.pow(1.0)); TestUtils.assertSame(Complex.NaN,infInf.pow(1.0)); TestUtils.assertSame(Complex.NaN,infNegInf.pow(1.0)); TestUtils.assertSame(Complex.NaN,negInfInf.pow(10)); TestUtils.assertSame(Complex.NaN,negInfNegInf.pow(1.0)); TestUtils.assertSame(Complex.NaN,negInfNegInf.pow(Double.POSITIVE_INFINITY)); TestUtils.assertSame(Complex.NaN,negInfNegInf.pow(Double.POSITIVE_INFINITY)); TestUtils.assertSame(Complex.NaN,infInf.pow(Double.POSITIVE_INFINITY)); TestUtils.assertSame(Complex.NaN,infInf.pow(Double.NEGATIVE_INFINITY)); TestUtils.assertSame(Complex.NaN,infNegInf.pow(Double.NEGATIVE_INFINITY)); TestUtils.assertSame(Complex.NaN,infNegInf.pow(Double.POSITIVE_INFINITY)); } @Test public void testScalarPowZero() { TestUtils.assertSame(Complex.NaN, Complex.ZERO.pow(1.0)); TestUtils.assertSame(Complex.NaN, Complex.ZERO.pow(0.0)); TestUtils.assertEquals(Complex.ONE, Complex.ONE.pow(0.0), 10e-12); TestUtils.assertEquals(Complex.ONE, Complex.I.pow(0.0), 10e-12); TestUtils.assertEquals(Complex.ONE, new Complex(-1, 3).pow(0.0), 10e-12); } @Test(expected=NullArgumentException.class) public void testpowNull() { Complex.ONE.pow(null); } @Test public void testSin() { Complex z = new Complex(3, 4); Complex expected = new Complex(3.853738, -27.01681); TestUtils.assertEquals(expected, z.sin(), 1.0e-5); } @Test public void testSinInf() { TestUtils.assertSame(infInf, oneInf.sin()); TestUtils.assertSame(infNegInf, oneNegInf.sin()); TestUtils.assertSame(Complex.NaN, infOne.sin()); TestUtils.assertSame(Complex.NaN, negInfOne.sin()); TestUtils.assertSame(Complex.NaN, infInf.sin()); TestUtils.assertSame(Complex.NaN, infNegInf.sin()); TestUtils.assertSame(Complex.NaN, negInfInf.sin()); TestUtils.assertSame(Complex.NaN, negInfNegInf.sin()); } @Test public void testSinNaN() { Assert.assertTrue(Complex.NaN.sin().isNaN()); } @Test public void testSinh() { Complex z = new Complex(3, 4); Complex expected = new Complex(-6.54812, -7.61923); TestUtils.assertEquals(expected, z.sinh(), 1.0e-5); } @Test public void testSinhNaN() { Assert.assertTrue(Complex.NaN.sinh().isNaN()); } @Test public void testSinhInf() { TestUtils.assertSame(Complex.NaN, oneInf.sinh()); TestUtils.assertSame(Complex.NaN, oneNegInf.sinh()); TestUtils.assertSame(infInf, infOne.sinh()); TestUtils.assertSame(negInfInf, negInfOne.sinh()); TestUtils.assertSame(Complex.NaN, infInf.sinh()); TestUtils.assertSame(Complex.NaN, infNegInf.sinh()); TestUtils.assertSame(Complex.NaN, negInfInf.sinh()); TestUtils.assertSame(Complex.NaN, negInfNegInf.sinh()); } @Test public void testSqrtRealPositive() { Complex z = new Complex(3, 4); Complex expected = new Complex(2, 1); TestUtils.assertEquals(expected, z.sqrt(), 1.0e-5); } @Test public void testSqrtRealZero() { Complex z = new Complex(0.0, 4); Complex expected = new Complex(1.41421, 1.41421); TestUtils.assertEquals(expected, z.sqrt(), 1.0e-5); } @Test public void testSqrtRealNegative() { Complex z = new Complex(-3.0, 4); Complex expected = new Complex(1, 2); TestUtils.assertEquals(expected, z.sqrt(), 1.0e-5); } @Test public void testSqrtImaginaryZero() { Complex z = new Complex(-3.0, 0.0); Complex expected = new Complex(0.0, 1.73205); TestUtils.assertEquals(expected, z.sqrt(), 1.0e-5); } @Test public void testSqrtImaginaryNegative() { Complex z = new Complex(-3.0, -4.0); Complex expected = new Complex(1.0, -2.0); TestUtils.assertEquals(expected, z.sqrt(), 1.0e-5); } @Test public void testSqrtPolar() { double r = 1; for (int i = 0; i < 5; i++) { r += i; double theta = 0; for (int j =0; j < 11; j++) { theta += pi /12; Complex z = ComplexUtils.polar2Complex(r, theta); Complex sqrtz = ComplexUtils.polar2Complex(FastMath.sqrt(r), theta / 2); TestUtils.assertEquals(sqrtz, z.sqrt(), 10e-12); } } } @Test public void testSqrtNaN() { Assert.assertTrue(Complex.NaN.sqrt().isNaN()); } @Test public void testSqrtInf() { TestUtils.assertSame(infNaN, oneInf.sqrt()); TestUtils.assertSame(infNaN, oneNegInf.sqrt()); TestUtils.assertSame(infZero, infOne.sqrt()); TestUtils.assertSame(zeroInf, negInfOne.sqrt()); TestUtils.assertSame(infNaN, infInf.sqrt()); TestUtils.assertSame(infNaN, infNegInf.sqrt()); TestUtils.assertSame(nanInf, negInfInf.sqrt()); TestUtils.assertSame(nanNegInf, negInfNegInf.sqrt()); } @Test public void testSqrt1z() { Complex z = new Complex(3, 4); Complex expected = new Complex(4.08033, -2.94094); TestUtils.assertEquals(expected, z.sqrt1z(), 1.0e-5); } @Test public void testSqrt1zNaN() { Assert.assertTrue(Complex.NaN.sqrt1z().isNaN()); } @Test public void testTan() { Complex z = new Complex(3, 4); Complex expected = new Complex(-0.000187346, 0.999356); TestUtils.assertEquals(expected, z.tan(), 1.0e-5); /* Check that no overflow occurs (MATH-722) */ Complex actual = new Complex(3.0, 1E10).tan(); expected = new Complex(0, 1); TestUtils.assertEquals(expected, actual, 1.0e-5); actual = new Complex(3.0, -1E10).tan(); expected = new Complex(0, -1); TestUtils.assertEquals(expected, actual, 1.0e-5); } @Test public void testTanNaN() { Assert.assertTrue(Complex.NaN.tan().isNaN()); } @Test public void testTanInf() { TestUtils.assertSame(Complex.valueOf(0.0, 1.0), oneInf.tan()); TestUtils.assertSame(Complex.valueOf(0.0, -1.0), oneNegInf.tan()); TestUtils.assertSame(Complex.NaN, infOne.tan()); TestUtils.assertSame(Complex.NaN, negInfOne.tan()); TestUtils.assertSame(Complex.NaN, infInf.tan()); TestUtils.assertSame(Complex.NaN, infNegInf.tan()); TestUtils.assertSame(Complex.NaN, negInfInf.tan()); TestUtils.assertSame(Complex.NaN, negInfNegInf.tan()); } @Test public void testTanCritical() { TestUtils.assertSame(infNaN, new Complex(pi/2, 0).tan()); TestUtils.assertSame(negInfNaN, new Complex(-pi/2, 0).tan()); } @Test public void testTanh() { Complex z = new Complex(3, 4); Complex expected = new Complex(1.00071, 0.00490826); TestUtils.assertEquals(expected, z.tanh(), 1.0e-5); /* Check that no overflow occurs (MATH-722) */ Complex actual = new Complex(1E10, 3.0).tanh(); expected = new Complex(1, 0); TestUtils.assertEquals(expected, actual, 1.0e-5); actual = new Complex(-1E10, 3.0).tanh(); expected = new Complex(-1, 0); TestUtils.assertEquals(expected, actual, 1.0e-5); } @Test public void testTanhNaN() { Assert.assertTrue(Complex.NaN.tanh().isNaN()); } @Test public void testTanhInf() { TestUtils.assertSame(Complex.NaN, oneInf.tanh()); TestUtils.assertSame(Complex.NaN, oneNegInf.tanh()); TestUtils.assertSame(Complex.valueOf(1.0, 0.0), infOne.tanh()); TestUtils.assertSame(Complex.valueOf(-1.0, 0.0), negInfOne.tanh()); TestUtils.assertSame(Complex.NaN, infInf.tanh()); TestUtils.assertSame(Complex.NaN, infNegInf.tanh()); TestUtils.assertSame(Complex.NaN, negInfInf.tanh()); TestUtils.assertSame(Complex.NaN, negInfNegInf.tanh()); } @Test public void testTanhCritical() { TestUtils.assertSame(nanInf, new Complex(0, pi/2).tanh()); } /** test issue MATH-221 */ @Test public void testMath221() { Assert.assertEquals(new Complex(0,-1), new Complex(0,1).multiply(new Complex(-1,0))); } /** * Test: computing third roots of z. *
                   * 
                   * z = -2 + 2 * i
                   *   => z_0 =  1      +          i
                   *   => z_1 = -1.3660 + 0.3660 * i
                   *   => z_2 =  0.3660 - 1.3660 * i
                   * 
                   * 
              */ @Test public void testNthRoot_normal_thirdRoot() { // The complex number we want to compute all third-roots for. Complex z = new Complex(-2,2); // The List holding all third roots Complex[] thirdRootsOfZ = z.nthRoot(3).toArray(new Complex[0]); // Returned Collection must not be empty! Assert.assertEquals(3, thirdRootsOfZ.length); // test z_0 Assert.assertEquals(1.0, thirdRootsOfZ[0].getReal(), 1.0e-5); Assert.assertEquals(1.0, thirdRootsOfZ[0].getImaginary(), 1.0e-5); // test z_1 Assert.assertEquals(-1.3660254037844386, thirdRootsOfZ[1].getReal(), 1.0e-5); Assert.assertEquals(0.36602540378443843, thirdRootsOfZ[1].getImaginary(), 1.0e-5); // test z_2 Assert.assertEquals(0.366025403784439, thirdRootsOfZ[2].getReal(), 1.0e-5); Assert.assertEquals(-1.3660254037844384, thirdRootsOfZ[2].getImaginary(), 1.0e-5); } /** * Test: computing fourth roots of z. *
                   * 
                   * z = 5 - 2 * i
                   *   => z_0 =  1.5164 - 0.1446 * i
                   *   => z_1 =  0.1446 + 1.5164 * i
                   *   => z_2 = -1.5164 + 0.1446 * i
                   *   => z_3 = -1.5164 - 0.1446 * i
                   * 
                   * 
              */ @Test public void testNthRoot_normal_fourthRoot() { // The complex number we want to compute all third-roots for. Complex z = new Complex(5,-2); // The List holding all fourth roots Complex[] fourthRootsOfZ = z.nthRoot(4).toArray(new Complex[0]); // Returned Collection must not be empty! Assert.assertEquals(4, fourthRootsOfZ.length); // test z_0 Assert.assertEquals(1.5164629308487783, fourthRootsOfZ[0].getReal(), 1.0e-5); Assert.assertEquals(-0.14469266210702247, fourthRootsOfZ[0].getImaginary(), 1.0e-5); // test z_1 Assert.assertEquals(0.14469266210702256, fourthRootsOfZ[1].getReal(), 1.0e-5); Assert.assertEquals(1.5164629308487783, fourthRootsOfZ[1].getImaginary(), 1.0e-5); // test z_2 Assert.assertEquals(-1.5164629308487783, fourthRootsOfZ[2].getReal(), 1.0e-5); Assert.assertEquals(0.14469266210702267, fourthRootsOfZ[2].getImaginary(), 1.0e-5); // test z_3 Assert.assertEquals(-0.14469266210702275, fourthRootsOfZ[3].getReal(), 1.0e-5); Assert.assertEquals(-1.5164629308487783, fourthRootsOfZ[3].getImaginary(), 1.0e-5); } /** * Test: computing third roots of z. *
                   * 
                   * z = 8
                   *   => z_0 =  2
                   *   => z_1 = -1 + 1.73205 * i
                   *   => z_2 = -1 - 1.73205 * i
                   * 
                   * 
              */ @Test public void testNthRoot_cornercase_thirdRoot_imaginaryPartEmpty() { // The number 8 has three third roots. One we all already know is the number 2. // But there are two more complex roots. Complex z = new Complex(8,0); // The List holding all third roots Complex[] thirdRootsOfZ = z.nthRoot(3).toArray(new Complex[0]); // Returned Collection must not be empty! Assert.assertEquals(3, thirdRootsOfZ.length); // test z_0 Assert.assertEquals(2.0, thirdRootsOfZ[0].getReal(), 1.0e-5); Assert.assertEquals(0.0, thirdRootsOfZ[0].getImaginary(), 1.0e-5); // test z_1 Assert.assertEquals(-1.0, thirdRootsOfZ[1].getReal(), 1.0e-5); Assert.assertEquals(1.7320508075688774, thirdRootsOfZ[1].getImaginary(), 1.0e-5); // test z_2 Assert.assertEquals(-1.0, thirdRootsOfZ[2].getReal(), 1.0e-5); Assert.assertEquals(-1.732050807568877, thirdRootsOfZ[2].getImaginary(), 1.0e-5); } /** * Test: computing third roots of z with real part 0. *
                   * 
                   * z = 2 * i
                   *   => z_0 =  1.0911 + 0.6299 * i
                   *   => z_1 = -1.0911 + 0.6299 * i
                   *   => z_2 = -2.3144 - 1.2599 * i
                   * 
                   * 
              */ @Test public void testNthRoot_cornercase_thirdRoot_realPartZero() { // complex number with only imaginary part Complex z = new Complex(0,2); // The List holding all third roots Complex[] thirdRootsOfZ = z.nthRoot(3).toArray(new Complex[0]); // Returned Collection must not be empty! Assert.assertEquals(3, thirdRootsOfZ.length); // test z_0 Assert.assertEquals(1.0911236359717216, thirdRootsOfZ[0].getReal(), 1.0e-5); Assert.assertEquals(0.6299605249474365, thirdRootsOfZ[0].getImaginary(), 1.0e-5); // test z_1 Assert.assertEquals(-1.0911236359717216, thirdRootsOfZ[1].getReal(), 1.0e-5); Assert.assertEquals(0.6299605249474365, thirdRootsOfZ[1].getImaginary(), 1.0e-5); // test z_2 Assert.assertEquals(-2.3144374213981936E-16, thirdRootsOfZ[2].getReal(), 1.0e-5); Assert.assertEquals(-1.2599210498948732, thirdRootsOfZ[2].getImaginary(), 1.0e-5); } /** * Test cornercases with NaN and Infinity. */ @Test public void testNthRoot_cornercase_NAN_Inf() { // NaN + finite -> NaN List roots = oneNaN.nthRoot(3); Assert.assertEquals(1,roots.size()); Assert.assertEquals(Complex.NaN, roots.get(0)); roots = nanZero.nthRoot(3); Assert.assertEquals(1,roots.size()); Assert.assertEquals(Complex.NaN, roots.get(0)); // NaN + infinite -> NaN roots = nanInf.nthRoot(3); Assert.assertEquals(1,roots.size()); Assert.assertEquals(Complex.NaN, roots.get(0)); // finite + infinite -> Inf roots = oneInf.nthRoot(3); Assert.assertEquals(1,roots.size()); Assert.assertEquals(Complex.INF, roots.get(0)); // infinite + infinite -> Inf roots = negInfInf.nthRoot(3); Assert.assertEquals(1,roots.size()); Assert.assertEquals(Complex.INF, roots.get(0)); } /** * Test standard values */ @Test public void testGetArgument() { Complex z = new Complex(1, 0); Assert.assertEquals(0.0, z.getArgument(), 1.0e-12); z = new Complex(1, 1); Assert.assertEquals(FastMath.PI/4, z.getArgument(), 1.0e-12); z = new Complex(0, 1); Assert.assertEquals(FastMath.PI/2, z.getArgument(), 1.0e-12); z = new Complex(-1, 1); Assert.assertEquals(3 * FastMath.PI/4, z.getArgument(), 1.0e-12); z = new Complex(-1, 0); Assert.assertEquals(FastMath.PI, z.getArgument(), 1.0e-12); z = new Complex(-1, -1); Assert.assertEquals(-3 * FastMath.PI/4, z.getArgument(), 1.0e-12); z = new Complex(0, -1); Assert.assertEquals(-FastMath.PI/2, z.getArgument(), 1.0e-12); z = new Complex(1, -1); Assert.assertEquals(-FastMath.PI/4, z.getArgument(), 1.0e-12); } /** * Verify atan2-style handling of infinite parts */ @Test public void testGetArgumentInf() { Assert.assertEquals(FastMath.PI/4, infInf.getArgument(), 1.0e-12); Assert.assertEquals(FastMath.PI/2, oneInf.getArgument(), 1.0e-12); Assert.assertEquals(0.0, infOne.getArgument(), 1.0e-12); Assert.assertEquals(FastMath.PI/2, zeroInf.getArgument(), 1.0e-12); Assert.assertEquals(0.0, infZero.getArgument(), 1.0e-12); Assert.assertEquals(FastMath.PI, negInfOne.getArgument(), 1.0e-12); Assert.assertEquals(-3.0*FastMath.PI/4, negInfNegInf.getArgument(), 1.0e-12); Assert.assertEquals(-FastMath.PI/2, oneNegInf.getArgument(), 1.0e-12); } /** * Verify that either part NaN results in NaN */ @Test public void testGetArgumentNaN() { Assert.assertTrue(Double.isNaN(nanZero.getArgument())); Assert.assertTrue(Double.isNaN(zeroNaN.getArgument())); Assert.assertTrue(Double.isNaN(Complex.NaN.getArgument())); } @Test public void testSerial() { Complex z = new Complex(3.0, 4.0); Assert.assertEquals(z, TestUtils.serializeAndRecover(z)); Complex ncmplx = (Complex)TestUtils.serializeAndRecover(oneNaN); Assert.assertEquals(nanZero, ncmplx); Assert.assertTrue(ncmplx.isNaN()); Complex infcmplx = (Complex)TestUtils.serializeAndRecover(infInf); Assert.assertEquals(infInf, infcmplx); Assert.assertTrue(infcmplx.isInfinite()); TestComplex tz = new TestComplex(3.0, 4.0); Assert.assertEquals(tz, TestUtils.serializeAndRecover(tz)); TestComplex ntcmplx = (TestComplex)TestUtils.serializeAndRecover(new TestComplex(oneNaN)); Assert.assertEquals(nanZero, ntcmplx); Assert.assertTrue(ntcmplx.isNaN()); TestComplex inftcmplx = (TestComplex)TestUtils.serializeAndRecover(new TestComplex(infInf)); Assert.assertEquals(infInf, inftcmplx); Assert.assertTrue(inftcmplx.isInfinite()); } /** * Class to test extending Complex */ public static class TestComplex extends Complex { /** * Serialization identifier. */ private static final long serialVersionUID = 3268726724160389237L; public TestComplex(double real, double imaginary) { super(real, imaginary); } public TestComplex(Complex other){ this(other.getReal(), other.getImaginary()); } @Override protected TestComplex createComplex(double real, double imaginary){ return new TestComplex(real, imaginary); } } } commons-math3-3.2-src/src/test/java/org/apache/commons/math3/complex/ComplexUtilsTest.java100644 1750 1750 10617 12126627672 30411 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.complex; import org.apache.commons.math3.TestUtils; import org.apache.commons.math3.util.FastMath; import org.junit.Test; import org.junit.Assert; /** * @version $Id: ComplexUtilsTest.java 1361793 2012-07-15 20:50:13Z erans $ */ public class ComplexUtilsTest { private double inf = Double.POSITIVE_INFINITY; private double negInf = Double.NEGATIVE_INFINITY; private double nan = Double.NaN; private double pi = FastMath.PI; private Complex negInfInf = new Complex(negInf, inf); private Complex infNegInf = new Complex(inf, negInf); private Complex infInf = new Complex(inf, inf); private Complex negInfNegInf = new Complex(negInf, negInf); private Complex infNaN = new Complex(inf, nan); @Test public void testPolar2Complex() { TestUtils.assertEquals(Complex.ONE, ComplexUtils.polar2Complex(1, 0), 10e-12); TestUtils.assertEquals(Complex.ZERO, ComplexUtils.polar2Complex(0, 1), 10e-12); TestUtils.assertEquals(Complex.ZERO, ComplexUtils.polar2Complex(0, -1), 10e-12); TestUtils.assertEquals(Complex.I, ComplexUtils.polar2Complex(1, pi/2), 10e-12); TestUtils.assertEquals(Complex.I.negate(), ComplexUtils.polar2Complex(1, -pi/2), 10e-12); double r = 0; for (int i = 0; i < 5; i++) { r += i; double theta = 0; for (int j =0; j < 20; j++) { theta += pi / 6; TestUtils.assertEquals(altPolar(r, theta), ComplexUtils.polar2Complex(r, theta), 10e-12); } theta = -2 * pi; for (int j =0; j < 20; j++) { theta -= pi / 6; TestUtils.assertEquals(altPolar(r, theta), ComplexUtils.polar2Complex(r, theta), 10e-12); } } } protected Complex altPolar(double r, double theta) { return Complex.I.multiply(new Complex(theta, 0)).exp().multiply(new Complex(r, 0)); } @Test(expected=IllegalArgumentException.class) public void testPolar2ComplexIllegalModulus() { ComplexUtils.polar2Complex(-1, 0); } @Test public void testPolar2ComplexNaN() { TestUtils.assertSame(Complex.NaN, ComplexUtils.polar2Complex(nan, 1)); TestUtils.assertSame(Complex.NaN, ComplexUtils.polar2Complex(1, nan)); TestUtils.assertSame(Complex.NaN, ComplexUtils.polar2Complex(nan, nan)); } @Test public void testPolar2ComplexInf() { TestUtils.assertSame(Complex.NaN, ComplexUtils.polar2Complex(1, inf)); TestUtils.assertSame(Complex.NaN, ComplexUtils.polar2Complex(1, negInf)); TestUtils.assertSame(Complex.NaN, ComplexUtils.polar2Complex(inf, inf)); TestUtils.assertSame(Complex.NaN, ComplexUtils.polar2Complex(inf, negInf)); TestUtils.assertSame(infInf, ComplexUtils.polar2Complex(inf, pi/4)); TestUtils.assertSame(infNaN, ComplexUtils.polar2Complex(inf, 0)); TestUtils.assertSame(infNegInf, ComplexUtils.polar2Complex(inf, -pi/4)); TestUtils.assertSame(negInfInf, ComplexUtils.polar2Complex(inf, 3*pi/4)); TestUtils.assertSame(negInfNegInf, ComplexUtils.polar2Complex(inf, 5*pi/4)); } @Test public void testConvertToComplex() { final double[] real = new double[] { negInf, -123.45, 0, 1, 234.56, pi, inf }; final Complex[] complex = ComplexUtils.convertToComplex(real); for (int i = 0; i < real.length; i++) { Assert.assertEquals(real[i], complex[i].getReal(), 0d); } } } commons-math3-3.2-src/src/test/java/org/apache/commons/math3/complex/ComplexFormatAbstractTest.java100644 1750 1750 25633 12126627672 32231 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.complex; import java.text.NumberFormat; import java.text.ParsePosition; import java.util.Locale; import org.junit.Test; import org.junit.Assert; import org.apache.commons.math3.util.FastMath; public abstract class ComplexFormatAbstractTest { ComplexFormat complexFormat = null; ComplexFormat complexFormatJ = null; protected abstract Locale getLocale(); protected abstract char getDecimalCharacter(); protected ComplexFormatAbstractTest() { complexFormat = ComplexFormat.getInstance(getLocale()); complexFormatJ = ComplexFormat.getInstance("j", getLocale()); } @Test public void testSimpleNoDecimals() { Complex c = new Complex(1, 2); String expected = "1 + 2i"; String actual = complexFormat.format(c); Assert.assertEquals(expected, actual); } @Test public void testTrimOneImaginary() { final ComplexFormat fmt = ComplexFormat.getInstance(getLocale()); fmt.getImaginaryFormat().setMaximumFractionDigits(1); Complex c = new Complex(1, 1.04); String expected = "1 + i"; String actual = fmt.format(c); Assert.assertEquals(expected, actual); c = new Complex(1, 1.09); expected = "1 + 1" + getDecimalCharacter() + "1i"; actual = fmt.format(c); Assert.assertEquals(expected, actual); c = new Complex(1, -1.09); expected = "1 - 1" + getDecimalCharacter() + "1i"; actual = fmt.format(c); Assert.assertEquals(expected, actual); c = new Complex(1, -1.04); expected = "1 - i"; actual = fmt.format(c); Assert.assertEquals(expected, actual); } @Test public void testSimpleWithDecimals() { Complex c = new Complex(1.23, 1.43); String expected = "1" + getDecimalCharacter() + "23 + 1" + getDecimalCharacter() + "43i"; String actual = complexFormat.format(c); Assert.assertEquals(expected, actual); } @Test public void testSimpleWithDecimalsTrunc() { Complex c = new Complex(1.232323232323, 1.434343434343); String expected = "1" + getDecimalCharacter() + "2323232323 + 1" + getDecimalCharacter() + "4343434343i"; String actual = complexFormat.format(c); Assert.assertEquals(expected, actual); } @Test public void testNegativeReal() { Complex c = new Complex(-1.232323232323, 1.43); String expected = "-1" + getDecimalCharacter() + "2323232323 + 1" + getDecimalCharacter() + "43i"; String actual = complexFormat.format(c); Assert.assertEquals(expected, actual); } @Test public void testNegativeImaginary() { Complex c = new Complex(1.23, -1.434343434343); String expected = "1" + getDecimalCharacter() + "23 - 1" + getDecimalCharacter() + "4343434343i"; String actual = complexFormat.format(c); Assert.assertEquals(expected, actual); } @Test public void testNegativeBoth() { Complex c = new Complex(-1.232323232323, -1.434343434343); String expected = "-1" + getDecimalCharacter() + "2323232323 - 1" + getDecimalCharacter() + "4343434343i"; String actual = complexFormat.format(c); Assert.assertEquals(expected, actual); } @Test public void testZeroReal() { Complex c = new Complex(0.0, -1.434343434343); String expected = "0 - 1" + getDecimalCharacter() + "4343434343i"; String actual = complexFormat.format(c); Assert.assertEquals(expected, actual); } @Test public void testZeroImaginary() { Complex c = new Complex(30.23333333333, 0); String expected = "30" + getDecimalCharacter() + "2333333333"; String actual = complexFormat.format(c); Assert.assertEquals(expected, actual); } @Test public void testDifferentImaginaryChar() { Complex c = new Complex(1, 1); String expected = "1 + j"; String actual = complexFormatJ.format(c); Assert.assertEquals(expected, actual); } @Test public void testDefaultFormatComplex() { Locale defaultLocal = Locale.getDefault(); Locale.setDefault(getLocale()); Complex c = new Complex(232.22222222222, -342.3333333333); String expected = "232" + getDecimalCharacter() + "2222222222 - 342" + getDecimalCharacter() + "3333333333i"; String actual = (new ComplexFormat()).format(c); Assert.assertEquals(expected, actual); Locale.setDefault(defaultLocal); } @Test public void testNan() { Complex c = new Complex(Double.NaN, Double.NaN); String expected = "(NaN) + (NaN)i"; String actual = complexFormat.format(c); Assert.assertEquals(expected, actual); } @Test public void testPositiveInfinity() { Complex c = new Complex(Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY); String expected = "(Infinity) + (Infinity)i"; String actual = complexFormat.format(c); Assert.assertEquals(expected, actual); } @Test public void testNegativeInfinity() { Complex c = new Complex(Double.NEGATIVE_INFINITY, Double.NEGATIVE_INFINITY); String expected = "(-Infinity) - (Infinity)i"; String actual = complexFormat.format(c); Assert.assertEquals(expected, actual); } @Test public void testParseSimpleNoDecimals() { String source = "1 + 1i"; Complex expected = new Complex(1, 1); Complex actual = complexFormat.parse(source); Assert.assertEquals(expected, actual); } @Test public void testParseSimpleWithDecimals() { String source = "1" + getDecimalCharacter() + "23 + 1" + getDecimalCharacter() + "43i"; Complex expected = new Complex(1.23, 1.43); Complex actual = complexFormat.parse(source); Assert.assertEquals(expected, actual); } @Test public void testParseSimpleWithDecimalsTrunc() { String source = "1" + getDecimalCharacter() + "232323232323 + 1" + getDecimalCharacter() + "434343434343i"; Complex expected = new Complex(1.232323232323, 1.434343434343); Complex actual = complexFormat.parse(source); Assert.assertEquals(expected, actual); } @Test public void testParseNegativeReal() { String source = "-1" + getDecimalCharacter() + "232323232323 + 1" + getDecimalCharacter() + "4343i"; Complex expected = new Complex(-1.232323232323, 1.4343); Complex actual = complexFormat.parse(source); Assert.assertEquals(expected, actual); } @Test public void testParseNegativeImaginary() { String source = "1" + getDecimalCharacter() + "2323 - 1" + getDecimalCharacter() + "434343434343i"; Complex expected = new Complex(1.2323, -1.434343434343); Complex actual = complexFormat.parse(source); Assert.assertEquals(expected, actual); } @Test public void testParseNegativeBoth() { String source = "-1" + getDecimalCharacter() + "232323232323 - 1" + getDecimalCharacter() + "434343434343i"; Complex expected = new Complex(-1.232323232323, -1.434343434343); Complex actual = complexFormat.parse(source); Assert.assertEquals(expected, actual); } @Test public void testParseZeroReal() { String source = "0" + getDecimalCharacter() + "0 - 1" + getDecimalCharacter() + "4343i"; Complex expected = new Complex(0.0, -1.4343); Complex actual = complexFormat.parse(source); Assert.assertEquals(expected, actual); } @Test public void testParseZeroImaginary() { String source = "-1" + getDecimalCharacter() + "2323"; Complex expected = new Complex(-1.2323, 0); Complex actual = complexFormat.parse(source); Assert.assertEquals(expected, actual); } @Test public void testParseDifferentImaginaryChar() { String source = "-1" + getDecimalCharacter() + "2323 - 1" + getDecimalCharacter() + "4343j"; Complex expected = new Complex(-1.2323, -1.4343); Complex actual = complexFormatJ.parse(source); Assert.assertEquals(expected, actual); } @Test public void testParseNan() { String source = "(NaN) + (NaN)i"; Complex expected = new Complex(Double.NaN, Double.NaN); Complex actual = complexFormat.parse(source); Assert.assertEquals(expected, actual); } @Test public void testParsePositiveInfinity() { String source = "(Infinity) + (Infinity)i"; Complex expected = new Complex(Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY); Complex actual = complexFormat.parse(source); Assert.assertEquals(expected, actual); } @Test public void testPaseNegativeInfinity() { String source = "(-Infinity) - (Infinity)i"; Complex expected = new Complex(Double.NEGATIVE_INFINITY, Double.NEGATIVE_INFINITY); Complex actual = complexFormat.parse(source); Assert.assertEquals(expected, actual); } @Test public void testConstructorSingleFormat() { NumberFormat nf = NumberFormat.getInstance(); ComplexFormat cf = new ComplexFormat(nf); Assert.assertNotNull(cf); Assert.assertEquals(nf, cf.getRealFormat()); } @Test public void testGetImaginaryFormat() { NumberFormat nf = NumberFormat.getInstance(); ComplexFormat cf = new ComplexFormat(nf); Assert.assertSame(nf, cf.getImaginaryFormat()); } @Test public void testGetRealFormat() { NumberFormat nf = NumberFormat.getInstance(); ComplexFormat cf = new ComplexFormat(nf); Assert.assertSame(nf, cf.getRealFormat()); } @Test public void testFormatNumber() { ComplexFormat cf = ComplexFormat.getInstance(getLocale()); Double pi = Double.valueOf(FastMath.PI); String text = cf.format(pi); Assert.assertEquals("3" + getDecimalCharacter() + "1415926536", text); } @Test public void testForgottenImaginaryCharacter() { ParsePosition pos = new ParsePosition(0); Assert.assertNull(new ComplexFormat().parse("1 + 1", pos)); Assert.assertEquals(5, pos.getErrorIndex()); } } commons-math3-3.2-src/src/test/java/org/apache/commons/math3/complex/ComplexFormatTest.java100644 1750 1750 2124 12126627672 30513 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.complex; import java.util.Locale; public class ComplexFormatTest extends ComplexFormatAbstractTest { @Override protected char getDecimalCharacter() { return '.'; } @Override protected Locale getLocale() { return Locale.US; } } commons-math3-3.2-src/src/test/java/org/apache/commons/math3/fitting/CurveFitterTest.java100644 1750 1750 13523 12126627672 30217 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.fitting; import org.apache.commons.math3.optim.nonlinear.vector.jacobian.LevenbergMarquardtOptimizer; import org.apache.commons.math3.analysis.ParametricUnivariateFunction; import org.apache.commons.math3.util.FastMath; import org.junit.Assert; import org.junit.Test; public class CurveFitterTest { @Test public void testMath303() { LevenbergMarquardtOptimizer optimizer = new LevenbergMarquardtOptimizer(); CurveFitter fitter = new CurveFitter(optimizer); fitter.addObservedPoint(2.805d, 0.6934785852953367d); fitter.addObservedPoint(2.74333333333333d, 0.6306772025518496d); fitter.addObservedPoint(1.655d, 0.9474675497289684); fitter.addObservedPoint(1.725d, 0.9013594835804194d); ParametricUnivariateFunction sif = new SimpleInverseFunction(); double[] initialguess1 = new double[1]; initialguess1[0] = 1.0d; Assert.assertEquals(1, fitter.fit(sif, initialguess1).length); double[] initialguess2 = new double[2]; initialguess2[0] = 1.0d; initialguess2[1] = .5d; Assert.assertEquals(2, fitter.fit(sif, initialguess2).length); } @Test public void testMath304() { LevenbergMarquardtOptimizer optimizer = new LevenbergMarquardtOptimizer(); CurveFitter fitter = new CurveFitter(optimizer); fitter.addObservedPoint(2.805d, 0.6934785852953367d); fitter.addObservedPoint(2.74333333333333d, 0.6306772025518496d); fitter.addObservedPoint(1.655d, 0.9474675497289684); fitter.addObservedPoint(1.725d, 0.9013594835804194d); ParametricUnivariateFunction sif = new SimpleInverseFunction(); double[] initialguess1 = new double[1]; initialguess1[0] = 1.0d; Assert.assertEquals(1.6357215104109237, fitter.fit(sif, initialguess1)[0], 1.0e-14); double[] initialguess2 = new double[1]; initialguess2[0] = 10.0d; Assert.assertEquals(1.6357215104109237, fitter.fit(sif, initialguess1)[0], 1.0e-14); } @Test public void testMath372() { LevenbergMarquardtOptimizer optimizer = new LevenbergMarquardtOptimizer(); CurveFitter curveFitter = new CurveFitter(optimizer); curveFitter.addObservedPoint( 15, 4443); curveFitter.addObservedPoint( 31, 8493); curveFitter.addObservedPoint( 62, 17586); curveFitter.addObservedPoint(125, 30582); curveFitter.addObservedPoint(250, 45087); curveFitter.addObservedPoint(500, 50683); ParametricUnivariateFunction f = new ParametricUnivariateFunction() { public double value(double x, double ... parameters) { double a = parameters[0]; double b = parameters[1]; double c = parameters[2]; double d = parameters[3]; return d + ((a - d) / (1 + FastMath.pow(x / c, b))); } public double[] gradient(double x, double ... parameters) { double a = parameters[0]; double b = parameters[1]; double c = parameters[2]; double d = parameters[3]; double[] gradients = new double[4]; double den = 1 + FastMath.pow(x / c, b); // derivative with respect to a gradients[0] = 1 / den; // derivative with respect to b // in the reported (invalid) issue, there was a sign error here gradients[1] = -((a - d) * FastMath.pow(x / c, b) * FastMath.log(x / c)) / (den * den); // derivative with respect to c gradients[2] = (b * FastMath.pow(x / c, b - 1) * (x / (c * c)) * (a - d)) / (den * den); // derivative with respect to d gradients[3] = 1 - (1 / den); return gradients; } }; double[] initialGuess = new double[] { 1500, 0.95, 65, 35000 }; double[] estimatedParameters = curveFitter.fit(f, initialGuess); Assert.assertEquals( 2411.00, estimatedParameters[0], 500.00); Assert.assertEquals( 1.62, estimatedParameters[1], 0.04); Assert.assertEquals( 111.22, estimatedParameters[2], 0.30); Assert.assertEquals(55347.47, estimatedParameters[3], 300.00); Assert.assertTrue(optimizer.getRMS() < 600.0); } private static class SimpleInverseFunction implements ParametricUnivariateFunction { public double value(double x, double ... parameters) { return parameters[0] / x + (parameters.length < 2 ? 0 : parameters[1]); } public double[] gradient(double x, double ... doubles) { double[] gradientVector = new double[doubles.length]; gradientVector[0] = 1 / x; if (doubles.length >= 2) { gradientVector[1] = 1; } return gradientVector; } } } commons-math3-3.2-src/src/test/java/org/apache/commons/math3/fitting/GaussianFitterTest.java100644 1750 1750 30172 12126627672 30704 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.fitting; import org.apache.commons.math3.optim.nonlinear.vector.jacobian.LevenbergMarquardtOptimizer; import org.apache.commons.math3.exception.MathIllegalArgumentException; import org.junit.Assert; import org.junit.Test; /** * Tests {@link GaussianFitter}. * * @since 2.2 * @version $Id: GaussianFitterTest.java 1349707 2012-06-13 09:30:56Z erans $ */ public class GaussianFitterTest { /** Good data. */ protected static final double[][] DATASET1 = new double[][] { {4.0254623, 531026.0}, {4.02804905, 664002.0}, {4.02934242, 787079.0}, {4.03128248, 984167.0}, {4.03386923, 1294546.0}, {4.03580929, 1560230.0}, {4.03839603, 1887233.0}, {4.0396894, 2113240.0}, {4.04162946, 2375211.0}, {4.04421621, 2687152.0}, {4.04550958, 2862644.0}, {4.04744964, 3078898.0}, {4.05003639, 3327238.0}, {4.05132976, 3461228.0}, {4.05326982, 3580526.0}, {4.05585657, 3576946.0}, {4.05779662, 3439750.0}, {4.06038337, 3220296.0}, {4.06167674, 3070073.0}, {4.0636168, 2877648.0}, {4.06620355, 2595848.0}, {4.06749692, 2390157.0}, {4.06943698, 2175960.0}, {4.07202373, 1895104.0}, {4.0733171, 1687576.0}, {4.07525716, 1447024.0}, {4.0778439, 1130879.0}, {4.07978396, 904900.0}, {4.08237071, 717104.0}, {4.08366408, 620014.0} }; /** Poor data: right of peak not symmetric with left of peak. */ protected static final double[][] DATASET2 = new double[][] { {-20.15, 1523.0}, {-19.65, 1566.0}, {-19.15, 1592.0}, {-18.65, 1927.0}, {-18.15, 3089.0}, {-17.65, 6068.0}, {-17.15, 14239.0}, {-16.65, 34124.0}, {-16.15, 64097.0}, {-15.65, 110352.0}, {-15.15, 164742.0}, {-14.65, 209499.0}, {-14.15, 267274.0}, {-13.65, 283290.0}, {-13.15, 275363.0}, {-12.65, 258014.0}, {-12.15, 225000.0}, {-11.65, 200000.0}, {-11.15, 190000.0}, {-10.65, 185000.0}, {-10.15, 180000.0}, { -9.65, 179000.0}, { -9.15, 178000.0}, { -8.65, 177000.0}, { -8.15, 176000.0}, { -7.65, 175000.0}, { -7.15, 174000.0}, { -6.65, 173000.0}, { -6.15, 172000.0}, { -5.65, 171000.0}, { -5.15, 170000.0} }; /** Poor data: long tails. */ protected static final double[][] DATASET3 = new double[][] { {-90.15, 1513.0}, {-80.15, 1514.0}, {-70.15, 1513.0}, {-60.15, 1514.0}, {-50.15, 1513.0}, {-40.15, 1514.0}, {-30.15, 1513.0}, {-20.15, 1523.0}, {-19.65, 1566.0}, {-19.15, 1592.0}, {-18.65, 1927.0}, {-18.15, 3089.0}, {-17.65, 6068.0}, {-17.15, 14239.0}, {-16.65, 34124.0}, {-16.15, 64097.0}, {-15.65, 110352.0}, {-15.15, 164742.0}, {-14.65, 209499.0}, {-14.15, 267274.0}, {-13.65, 283290.0}, {-13.15, 275363.0}, {-12.65, 258014.0}, {-12.15, 214073.0}, {-11.65, 182244.0}, {-11.15, 136419.0}, {-10.65, 97823.0}, {-10.15, 58930.0}, { -9.65, 35404.0}, { -9.15, 16120.0}, { -8.65, 9823.0}, { -8.15, 5064.0}, { -7.65, 2575.0}, { -7.15, 1642.0}, { -6.65, 1101.0}, { -6.15, 812.0}, { -5.65, 690.0}, { -5.15, 565.0}, { 5.15, 564.0}, { 15.15, 565.0}, { 25.15, 564.0}, { 35.15, 565.0}, { 45.15, 564.0}, { 55.15, 565.0}, { 65.15, 564.0}, { 75.15, 565.0} }; /** Poor data: right of peak is missing. */ protected static final double[][] DATASET4 = new double[][] { {-20.15, 1523.0}, {-19.65, 1566.0}, {-19.15, 1592.0}, {-18.65, 1927.0}, {-18.15, 3089.0}, {-17.65, 6068.0}, {-17.15, 14239.0}, {-16.65, 34124.0}, {-16.15, 64097.0}, {-15.65, 110352.0}, {-15.15, 164742.0}, {-14.65, 209499.0}, {-14.15, 267274.0}, {-13.65, 283290.0} }; /** Good data, but few points. */ protected static final double[][] DATASET5 = new double[][] { {4.0254623, 531026.0}, {4.03128248, 984167.0}, {4.03839603, 1887233.0}, {4.04421621, 2687152.0}, {4.05132976, 3461228.0}, {4.05326982, 3580526.0}, {4.05779662, 3439750.0}, {4.0636168, 2877648.0}, {4.06943698, 2175960.0}, {4.07525716, 1447024.0}, {4.08237071, 717104.0}, {4.08366408, 620014.0} }; /** * Basic. */ @Test public void testFit01() { GaussianFitter fitter = new GaussianFitter(new LevenbergMarquardtOptimizer()); addDatasetToGaussianFitter(DATASET1, fitter); double[] parameters = fitter.fit(); Assert.assertEquals(3496978.1837704973, parameters[0], 1e-4); Assert.assertEquals(4.054933085999146, parameters[1], 1e-4); Assert.assertEquals(0.015039355620304326, parameters[2], 1e-4); } /** * Zero points is not enough observed points. */ @Test(expected=MathIllegalArgumentException.class) public void testFit02() { GaussianFitter fitter = new GaussianFitter(new LevenbergMarquardtOptimizer()); fitter.fit(); } /** * Two points is not enough observed points. */ @Test(expected=MathIllegalArgumentException.class) public void testFit03() { GaussianFitter fitter = new GaussianFitter(new LevenbergMarquardtOptimizer()); addDatasetToGaussianFitter(new double[][] { {4.0254623, 531026.0}, {4.02804905, 664002.0}}, fitter); fitter.fit(); } /** * Poor data: right of peak not symmetric with left of peak. */ @Test public void testFit04() { GaussianFitter fitter = new GaussianFitter(new LevenbergMarquardtOptimizer()); addDatasetToGaussianFitter(DATASET2, fitter); double[] parameters = fitter.fit(); Assert.assertEquals(233003.2967252038, parameters[0], 1e-4); Assert.assertEquals(-10.654887521095983, parameters[1], 1e-4); Assert.assertEquals(4.335937353196641, parameters[2], 1e-4); } /** * Poor data: long tails. */ @Test public void testFit05() { GaussianFitter fitter = new GaussianFitter(new LevenbergMarquardtOptimizer()); addDatasetToGaussianFitter(DATASET3, fitter); double[] parameters = fitter.fit(); Assert.assertEquals(283863.81929180305, parameters[0], 1e-4); Assert.assertEquals(-13.29641995105174, parameters[1], 1e-4); Assert.assertEquals(1.7297330293549908, parameters[2], 1e-4); } /** * Poor data: right of peak is missing. */ @Test public void testFit06() { GaussianFitter fitter = new GaussianFitter(new LevenbergMarquardtOptimizer()); addDatasetToGaussianFitter(DATASET4, fitter); double[] parameters = fitter.fit(); Assert.assertEquals(285250.66754309234, parameters[0], 1e-4); Assert.assertEquals(-13.528375695228455, parameters[1], 1e-4); Assert.assertEquals(1.5204344894331614, parameters[2], 1e-4); } /** * Basic with smaller dataset. */ @Test public void testFit07() { GaussianFitter fitter = new GaussianFitter(new LevenbergMarquardtOptimizer()); addDatasetToGaussianFitter(DATASET5, fitter); double[] parameters = fitter.fit(); Assert.assertEquals(3514384.729342235, parameters[0], 1e-4); Assert.assertEquals(4.054970307455625, parameters[1], 1e-4); Assert.assertEquals(0.015029412832160017, parameters[2], 1e-4); } @Test public void testMath519() { // The optimizer will try negative sigma values but "GaussianFitter" // will catch the raised exceptions and return NaN values instead. final double[] data = { 1.1143831578403364E-29, 4.95281403484594E-28, 1.1171347211930288E-26, 1.7044813962636277E-25, 1.9784716574832164E-24, 1.8630236407866774E-23, 1.4820532905097742E-22, 1.0241963854632831E-21, 6.275077366673128E-21, 3.461808994532493E-20, 1.7407124684715706E-19, 8.056687953553974E-19, 3.460193945992071E-18, 1.3883326374011525E-17, 5.233894983671116E-17, 1.8630791465263745E-16, 6.288759227922111E-16, 2.0204433920597856E-15, 6.198768938576155E-15, 1.821419346860626E-14, 5.139176445538471E-14, 1.3956427429045787E-13, 3.655705706448139E-13, 9.253753324779779E-13, 2.267636001476696E-12, 5.3880460095836855E-12, 1.2431632654852931E-11 }; GaussianFitter fitter = new GaussianFitter(new LevenbergMarquardtOptimizer()); for (int i = 0; i < data.length; i++) { fitter.addObservedPoint(i, data[i]); } final double[] p = fitter.fit(); Assert.assertEquals(53.1572792, p[1], 1e-7); Assert.assertEquals(5.75214622, p[2], 1e-8); } @Test public void testMath798() { final GaussianFitter fitter = new GaussianFitter(new LevenbergMarquardtOptimizer()); // When the data points are not commented out below, the fit stalls. // This is expected however, since the whole dataset hardly looks like // a Gaussian. // When commented out, the fit proceeds fine. fitter.addObservedPoint(0.23, 395.0); //fitter.addObservedPoint(0.68, 0.0); fitter.addObservedPoint(1.14, 376.0); //fitter.addObservedPoint(1.59, 0.0); fitter.addObservedPoint(2.05, 163.0); //fitter.addObservedPoint(2.50, 0.0); fitter.addObservedPoint(2.95, 49.0); //fitter.addObservedPoint(3.41, 0.0); fitter.addObservedPoint(3.86, 16.0); //fitter.addObservedPoint(4.32, 0.0); fitter.addObservedPoint(4.77, 1.0); final double[] p = fitter.fit(); // Values are copied from a previous run of this test. Assert.assertEquals(420.8397296167364, p[0], 1e-12); Assert.assertEquals(0.603770729862231, p[1], 1e-15); Assert.assertEquals(1.0786447936766612, p[2], 1e-14); } /** * Adds the specified points to specified GaussianFitter * instance. * * @param points data points where first dimension is a point index and * second dimension is an array of length two representing the point * with the first value corresponding to X and the second value * corresponding to Y * @param fitter fitter to which the points in points should be * added as observed points */ protected static void addDatasetToGaussianFitter(double[][] points, GaussianFitter fitter) { for (int i = 0; i < points.length; i++) { fitter.addObservedPoint(points[i][0], points[i][1]); } } } commons-math3-3.2-src/src/test/java/org/apache/commons/math3/fitting/HarmonicFitterTest.java100644 1750 1750 15466 12126627672 30703 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.fitting; import java.util.Random; import org.apache.commons.math3.optim.nonlinear.vector.jacobian.LevenbergMarquardtOptimizer; import org.apache.commons.math3.analysis.function.HarmonicOscillator; import org.apache.commons.math3.exception.NumberIsTooSmallException; import org.apache.commons.math3.exception.MathIllegalStateException; import org.apache.commons.math3.util.FastMath; import org.apache.commons.math3.util.MathUtils; import org.junit.Test; import org.junit.Assert; public class HarmonicFitterTest { @Test(expected=NumberIsTooSmallException.class) public void testPreconditions1() { HarmonicFitter fitter = new HarmonicFitter(new LevenbergMarquardtOptimizer()); fitter.fit(); } @Test public void testNoError() { final double a = 0.2; final double w = 3.4; final double p = 4.1; HarmonicOscillator f = new HarmonicOscillator(a, w, p); HarmonicFitter fitter = new HarmonicFitter(new LevenbergMarquardtOptimizer()); for (double x = 0.0; x < 1.3; x += 0.01) { fitter.addObservedPoint(1, x, f.value(x)); } final double[] fitted = fitter.fit(); Assert.assertEquals(a, fitted[0], 1.0e-13); Assert.assertEquals(w, fitted[1], 1.0e-13); Assert.assertEquals(p, MathUtils.normalizeAngle(fitted[2], p), 1e-13); HarmonicOscillator ff = new HarmonicOscillator(fitted[0], fitted[1], fitted[2]); for (double x = -1.0; x < 1.0; x += 0.01) { Assert.assertTrue(FastMath.abs(f.value(x) - ff.value(x)) < 1e-13); } } @Test public void test1PercentError() { Random randomizer = new Random(64925784252l); final double a = 0.2; final double w = 3.4; final double p = 4.1; HarmonicOscillator f = new HarmonicOscillator(a, w, p); HarmonicFitter fitter = new HarmonicFitter(new LevenbergMarquardtOptimizer()); for (double x = 0.0; x < 10.0; x += 0.1) { fitter.addObservedPoint(1, x, f.value(x) + 0.01 * randomizer.nextGaussian()); } final double[] fitted = fitter.fit(); Assert.assertEquals(a, fitted[0], 7.6e-4); Assert.assertEquals(w, fitted[1], 2.7e-3); Assert.assertEquals(p, MathUtils.normalizeAngle(fitted[2], p), 1.3e-2); } @Test public void testTinyVariationsData() { Random randomizer = new Random(64925784252l); HarmonicFitter fitter = new HarmonicFitter(new LevenbergMarquardtOptimizer()); for (double x = 0.0; x < 10.0; x += 0.1) { fitter.addObservedPoint(1, x, 1e-7 * randomizer.nextGaussian()); } fitter.fit(); // This test serves to cover the part of the code of "guessAOmega" // when the algorithm using integrals fails. } @Test public void testInitialGuess() { Random randomizer = new Random(45314242l); final double a = 0.2; final double w = 3.4; final double p = 4.1; HarmonicOscillator f = new HarmonicOscillator(a, w, p); HarmonicFitter fitter = new HarmonicFitter(new LevenbergMarquardtOptimizer()); for (double x = 0.0; x < 10.0; x += 0.1) { fitter.addObservedPoint(1, x, f.value(x) + 0.01 * randomizer.nextGaussian()); } final double[] fitted = fitter.fit(new double[] { 0.15, 3.6, 4.5 }); Assert.assertEquals(a, fitted[0], 1.2e-3); Assert.assertEquals(w, fitted[1], 3.3e-3); Assert.assertEquals(p, MathUtils.normalizeAngle(fitted[2], p), 1.7e-2); } @Test public void testUnsorted() { Random randomizer = new Random(64925784252l); final double a = 0.2; final double w = 3.4; final double p = 4.1; HarmonicOscillator f = new HarmonicOscillator(a, w, p); HarmonicFitter fitter = new HarmonicFitter(new LevenbergMarquardtOptimizer()); // build a regularly spaced array of measurements int size = 100; double[] xTab = new double[size]; double[] yTab = new double[size]; for (int i = 0; i < size; ++i) { xTab[i] = 0.1 * i; yTab[i] = f.value(xTab[i]) + 0.01 * randomizer.nextGaussian(); } // shake it for (int i = 0; i < size; ++i) { int i1 = randomizer.nextInt(size); int i2 = randomizer.nextInt(size); double xTmp = xTab[i1]; double yTmp = yTab[i1]; xTab[i1] = xTab[i2]; yTab[i1] = yTab[i2]; xTab[i2] = xTmp; yTab[i2] = yTmp; } // pass it to the fitter for (int i = 0; i < size; ++i) { fitter.addObservedPoint(1, xTab[i], yTab[i]); } final double[] fitted = fitter.fit(); Assert.assertEquals(a, fitted[0], 7.6e-4); Assert.assertEquals(w, fitted[1], 3.5e-3); Assert.assertEquals(p, MathUtils.normalizeAngle(fitted[2], p), 1.5e-2); } @Test(expected=MathIllegalStateException.class) public void testMath844() { final double[] y = { 0, 1, 2, 3, 2, 1, 0, -1, -2, -3, -2, -1, 0, 1, 2, 3, 2, 1, 0, -1, -2, -3, -2, -1, 0, 1, 2, 3, 2, 1, 0 }; final int len = y.length; final WeightedObservedPoint[] points = new WeightedObservedPoint[len]; for (int i = 0; i < len; i++) { points[i] = new WeightedObservedPoint(1, i, y[i]); } // The guesser fails because the function is far from an harmonic // function: It is a triangular periodic function with amplitude 3 // and period 12, and all sample points are taken at integer abscissae // so function values all belong to the integer subset {-3, -2, -1, 0, // 1, 2, 3}. final HarmonicFitter.ParameterGuesser guesser = new HarmonicFitter.ParameterGuesser(points); } } commons-math3-3.2-src/src/test/java/org/apache/commons/math3/fitting/PolynomialFitterTest.java100644 1750 1750 27705 12126627672 31265 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.fitting; import java.util.Random; import org.apache.commons.math3.analysis.polynomials.PolynomialFunction; import org.apache.commons.math3.analysis.polynomials.PolynomialFunction.Parametric; import org.apache.commons.math3.exception.ConvergenceException; import org.apache.commons.math3.exception.TooManyEvaluationsException; import org.apache.commons.math3.optim.nonlinear.vector.MultivariateVectorOptimizer; import org.apache.commons.math3.optim.nonlinear.vector.jacobian.LevenbergMarquardtOptimizer; import org.apache.commons.math3.optim.nonlinear.vector.jacobian.GaussNewtonOptimizer; import org.apache.commons.math3.optim.SimpleVectorValueChecker; import org.apache.commons.math3.util.FastMath; import org.apache.commons.math3.distribution.RealDistribution; import org.apache.commons.math3.distribution.UniformRealDistribution; import org.apache.commons.math3.TestUtils; import org.junit.Test; import org.junit.Assert; /** * Test for class {@link CurveFitter} where the function to fit is a * polynomial. */ public class PolynomialFitterTest { @Test public void testFit() { final RealDistribution rng = new UniformRealDistribution(-100, 100); rng.reseedRandomGenerator(64925784252L); final LevenbergMarquardtOptimizer optim = new LevenbergMarquardtOptimizer(); final PolynomialFitter fitter = new PolynomialFitter(optim); final double[] coeff = { 12.9, -3.4, 2.1 }; // 12.9 - 3.4 x + 2.1 x^2 final PolynomialFunction f = new PolynomialFunction(coeff); // Collect data from a known polynomial. for (int i = 0; i < 100; i++) { final double x = rng.sample(); fitter.addObservedPoint(x, f.value(x)); } // Start fit from initial guesses that are far from the optimal values. final double[] best = fitter.fit(new double[] { -1e-20, 3e15, -5e25 }); TestUtils.assertEquals("best != coeff", coeff, best, 1e-12); } @Test public void testNoError() { Random randomizer = new Random(64925784252l); for (int degree = 1; degree < 10; ++degree) { PolynomialFunction p = buildRandomPolynomial(degree, randomizer); PolynomialFitter fitter = new PolynomialFitter(new LevenbergMarquardtOptimizer()); for (int i = 0; i <= degree; ++i) { fitter.addObservedPoint(1.0, i, p.value(i)); } final double[] init = new double[degree + 1]; PolynomialFunction fitted = new PolynomialFunction(fitter.fit(init)); for (double x = -1.0; x < 1.0; x += 0.01) { double error = FastMath.abs(p.value(x) - fitted.value(x)) / (1.0 + FastMath.abs(p.value(x))); Assert.assertEquals(0.0, error, 1.0e-6); } } } @Test public void testSmallError() { Random randomizer = new Random(53882150042l); double maxError = 0; for (int degree = 0; degree < 10; ++degree) { PolynomialFunction p = buildRandomPolynomial(degree, randomizer); PolynomialFitter fitter = new PolynomialFitter(new LevenbergMarquardtOptimizer()); for (double x = -1.0; x < 1.0; x += 0.01) { fitter.addObservedPoint(1.0, x, p.value(x) + 0.1 * randomizer.nextGaussian()); } final double[] init = new double[degree + 1]; PolynomialFunction fitted = new PolynomialFunction(fitter.fit(init)); for (double x = -1.0; x < 1.0; x += 0.01) { double error = FastMath.abs(p.value(x) - fitted.value(x)) / (1.0 + FastMath.abs(p.value(x))); maxError = FastMath.max(maxError, error); Assert.assertTrue(FastMath.abs(error) < 0.1); } } Assert.assertTrue(maxError > 0.01); } @Test public void testMath798() { final double tol = 1e-14; final SimpleVectorValueChecker checker = new SimpleVectorValueChecker(tol, tol); final double[] init = new double[] { 0, 0 }; final int maxEval = 3; final double[] lm = doMath798(new LevenbergMarquardtOptimizer(checker), maxEval, init); final double[] gn = doMath798(new GaussNewtonOptimizer(checker), maxEval, init); for (int i = 0; i <= 1; i++) { Assert.assertEquals(lm[i], gn[i], tol); } } /** * This test shows that the user can set the maximum number of iterations * to avoid running for too long. * But in the test case, the real problem is that the tolerance is way too * stringent. */ @Test(expected=TooManyEvaluationsException.class) public void testMath798WithToleranceTooLow() { final double tol = 1e-100; final SimpleVectorValueChecker checker = new SimpleVectorValueChecker(tol, tol); final double[] init = new double[] { 0, 0 }; final int maxEval = 10000; // Trying hard to fit. final double[] gn = doMath798(new GaussNewtonOptimizer(checker), maxEval, init); } /** * This test shows that the user can set the maximum number of iterations * to avoid running for too long. * Even if the real problem is that the tolerance is way too stringent, it * is possible to get the best solution so far, i.e. a checker will return * the point when the maximum iteration count has been reached. */ @Test public void testMath798WithToleranceTooLowButNoException() { final double tol = 1e-100; final double[] init = new double[] { 0, 0 }; final int maxEval = 10000; // Trying hard to fit. final SimpleVectorValueChecker checker = new SimpleVectorValueChecker(tol, tol, maxEval); final double[] lm = doMath798(new LevenbergMarquardtOptimizer(checker), maxEval, init); final double[] gn = doMath798(new GaussNewtonOptimizer(checker), maxEval, init); for (int i = 0; i <= 1; i++) { Assert.assertEquals(lm[i], gn[i], 1e-15); } } /** * @param optimizer Optimizer. * @param maxEval Maximum number of function evaluations. * @param init First guess. * @return the solution found by the given optimizer. */ private double[] doMath798(MultivariateVectorOptimizer optimizer, int maxEval, double[] init) { final CurveFitter fitter = new CurveFitter(optimizer); fitter.addObservedPoint(-0.2, -7.12442E-13); fitter.addObservedPoint(-0.199, -4.33397E-13); fitter.addObservedPoint(-0.198, -2.823E-13); fitter.addObservedPoint(-0.197, -1.40405E-13); fitter.addObservedPoint(-0.196, -7.80821E-15); fitter.addObservedPoint(-0.195, 6.20484E-14); fitter.addObservedPoint(-0.194, 7.24673E-14); fitter.addObservedPoint(-0.193, 1.47152E-13); fitter.addObservedPoint(-0.192, 1.9629E-13); fitter.addObservedPoint(-0.191, 2.12038E-13); fitter.addObservedPoint(-0.19, 2.46906E-13); fitter.addObservedPoint(-0.189, 2.77495E-13); fitter.addObservedPoint(-0.188, 2.51281E-13); fitter.addObservedPoint(-0.187, 2.64001E-13); fitter.addObservedPoint(-0.186, 2.8882E-13); fitter.addObservedPoint(-0.185, 3.13604E-13); fitter.addObservedPoint(-0.184, 3.14248E-13); fitter.addObservedPoint(-0.183, 3.1172E-13); fitter.addObservedPoint(-0.182, 3.12912E-13); fitter.addObservedPoint(-0.181, 3.06761E-13); fitter.addObservedPoint(-0.18, 2.8559E-13); fitter.addObservedPoint(-0.179, 2.86806E-13); fitter.addObservedPoint(-0.178, 2.985E-13); fitter.addObservedPoint(-0.177, 2.67148E-13); fitter.addObservedPoint(-0.176, 2.94173E-13); fitter.addObservedPoint(-0.175, 3.27528E-13); fitter.addObservedPoint(-0.174, 3.33858E-13); fitter.addObservedPoint(-0.173, 2.97511E-13); fitter.addObservedPoint(-0.172, 2.8615E-13); fitter.addObservedPoint(-0.171, 2.84624E-13); final double[] coeff = fitter.fit(maxEval, new PolynomialFunction.Parametric(), init); return coeff; } @Test public void testRedundantSolvable() { // Levenberg-Marquardt should handle redundant information gracefully checkUnsolvableProblem(new LevenbergMarquardtOptimizer(), true); } @Test public void testRedundantUnsolvable() { // Gauss-Newton should not be able to solve redundant information checkUnsolvableProblem(new GaussNewtonOptimizer(true, new SimpleVectorValueChecker(1e-15, 1e-15)), false); } @Test public void testLargeSample() { Random randomizer = new Random(0x5551480dca5b369bl); double maxError = 0; for (int degree = 0; degree < 10; ++degree) { PolynomialFunction p = buildRandomPolynomial(degree, randomizer); PolynomialFitter fitter = new PolynomialFitter(new LevenbergMarquardtOptimizer()); for (int i = 0; i < 40000; ++i) { double x = -1.0 + i / 20000.0; fitter.addObservedPoint(1.0, x, p.value(x) + 0.1 * randomizer.nextGaussian()); } final double[] init = new double[degree + 1]; PolynomialFunction fitted = new PolynomialFunction(fitter.fit(init)); for (double x = -1.0; x < 1.0; x += 0.01) { double error = FastMath.abs(p.value(x) - fitted.value(x)) / (1.0 + FastMath.abs(p.value(x))); maxError = FastMath.max(maxError, error); Assert.assertTrue(FastMath.abs(error) < 0.01); } } Assert.assertTrue(maxError > 0.001); } private void checkUnsolvableProblem(MultivariateVectorOptimizer optimizer, boolean solvable) { Random randomizer = new Random(1248788532l); for (int degree = 0; degree < 10; ++degree) { PolynomialFunction p = buildRandomPolynomial(degree, randomizer); PolynomialFitter fitter = new PolynomialFitter(optimizer); // reusing the same point over and over again does not bring // information, the problem cannot be solved in this case for // degrees greater than 1 (but one point is sufficient for // degree 0) for (double x = -1.0; x < 1.0; x += 0.01) { fitter.addObservedPoint(1.0, 0.0, p.value(0.0)); } try { final double[] init = new double[degree + 1]; fitter.fit(init); Assert.assertTrue(solvable || (degree == 0)); } catch(ConvergenceException e) { Assert.assertTrue((! solvable) && (degree > 0)); } } } private PolynomialFunction buildRandomPolynomial(int degree, Random randomizer) { final double[] coefficients = new double[degree + 1]; for (int i = 0; i <= degree; ++i) { coefficients[i] = randomizer.nextGaussian(); } return new PolynomialFunction(coefficients); } } ././@LongLink100644 0 0 150 12126630646 10255 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/transform/FastHadamardTransformerTest.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/transform/FastHadamardTransformerTest.j100644 1750 1750 10345 12126627674 32377 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.transform; import org.apache.commons.math3.util.Precision; import org.junit.Assert; import org.junit.Test; /** * JUnit Test for HadamardTransformerTest * @see org.apache.commons.math3.transform.FastHadamardTransformer */ public final class FastHadamardTransformerTest { /** * Test of transformer for the a 8-point FHT (means n=8) */ @Test public void test8Points() { checkAllTransforms(new int[] { 1, 4, -2, 3, 0, 1, 4, -1 }, new int[] { 10, -4, 2, -4, 2, -12, 6, 8 }); } /** * Test of transformer for the a 4-points FHT (means n=4) */ @Test public void test4Points() { checkAllTransforms(new int[] { 1, 2, 3, 4 }, new int[] { 10, -2, -4, 0 }); } /** * Test the inverse transform of an integer vector is not always an integer vector */ @Test public void testNoIntInverse() { FastHadamardTransformer transformer = new FastHadamardTransformer(); double[] x = transformer.transform(new double[] { 0, 1, 0, 1}, TransformType.INVERSE); Assert.assertEquals( 0.5, x[0], 0); Assert.assertEquals(-0.5, x[1], 0); Assert.assertEquals( 0.0, x[2], 0); Assert.assertEquals( 0.0, x[3], 0); } /** * Test of transformer for wrong number of points */ @Test public void test3Points() { try { new FastHadamardTransformer().transform(new double[3], TransformType.FORWARD); Assert.fail("an exception should have been thrown"); } catch (IllegalArgumentException iae) { // expected } } private void checkAllTransforms(int[]x, int[] y) { checkDoubleTransform(x, y); checkInverseDoubleTransform(x, y); checkIntTransform(x, y); } private void checkDoubleTransform(int[]x, int[] y) { // Initiate the transformer FastHadamardTransformer transformer = new FastHadamardTransformer(); // check double transform double[] dX = new double[x.length]; for (int i = 0; i < dX.length; ++i) { dX[i] = x[i]; } double dResult[] = transformer.transform(dX, TransformType.FORWARD); for (int i = 0; i < dResult.length; i++) { // compare computed results to precomputed results Assert.assertTrue(Precision.equals(y[i], dResult[i], 1)); } } private void checkIntTransform(int[]x, int[] y) { // Initiate the transformer FastHadamardTransformer transformer = new FastHadamardTransformer(); // check integer transform int iResult[] = transformer.transform(x); for (int i = 0; i < iResult.length; i++) { // compare computed results to precomputed results Assert.assertEquals(y[i], iResult[i]); } } private void checkInverseDoubleTransform(int[]x, int[] y) { // Initiate the transformer FastHadamardTransformer transformer = new FastHadamardTransformer(); // check double transform double[] dY = new double[y.length]; for (int i = 0; i < dY.length; ++i) { dY[i] = y[i]; } double dResult[] = transformer.transform(dY, TransformType.INVERSE); for (int i = 0; i < dResult.length; i++) { // compare computed results to precomputed results Assert.assertTrue(Precision.equals(x[i], dResult[i], 1)); } } } commons-math3-3.2-src/src/test/java/org/apache/commons/math3/transform/FastSineTransformerTest.java100644 1750 1750 23423 12126627674 32265 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.transform; import java.util.Arrays; import java.util.Collection; import org.apache.commons.math3.analysis.UnivariateFunction; import org.apache.commons.math3.analysis.function.Sin; import org.apache.commons.math3.analysis.function.Sinc; import org.apache.commons.math3.exception.MathIllegalArgumentException; import org.apache.commons.math3.exception.MathIllegalStateException; import org.apache.commons.math3.util.FastMath; import org.junit.Assert; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.Parameterized; import org.junit.runners.Parameterized.Parameters; /** * Test case for fast sine transformer. *

              * FST algorithm is exact, the small tolerance number is used only * to account for round-off errors. * * @version $Id: FastSineTransformerTest.java 1374632 2012-08-18 18:11:11Z luc $ */ @RunWith(value = Parameterized.class) public final class FastSineTransformerTest extends RealTransformerAbstractTest { private final DstNormalization normalization; private final int[] invalidDataSize; private final double[] relativeTolerance; private final int[] validDataSize; public FastSineTransformerTest(final DstNormalization normalization) { this.normalization = normalization; this.validDataSize = new int[] { 1, 2, 4, 8, 16, 32, 64, 128 }; this.invalidDataSize = new int[] { 129 }; this.relativeTolerance = new double[] { 1E-15, 1E-15, 1E-14, 1E-14, 1E-13, 1E-12, 1E-11, 1E-11 }; } /** * Returns an array containing {@code true, false} in order to check both * standard and orthogonal DSTs. * * @return an array of parameters for this parameterized test */ @Parameters public static Collection data() { final DstNormalization[] normalization = DstNormalization.values(); final Object[][] data = new DstNormalization[normalization.length][1]; for (int i = 0; i < normalization.length; i++) { data[i][0] = normalization[i]; } return Arrays.asList(data); } /** * {@inheritDoc} * * Overriding the default implementation allows to ensure that the first * element of the data set is zero. */ @Override double[] createRealData(final int n) { final double[] data = super.createRealData(n); data[0] = 0.0; return data; } @Override RealTransformer createRealTransformer() { return new FastSineTransformer(normalization); } @Override int getInvalidDataSize(final int i) { return invalidDataSize[i]; } @Override int getNumberOfInvalidDataSizes() { return invalidDataSize.length; } @Override int getNumberOfValidDataSizes() { return validDataSize.length; } @Override double getRelativeTolerance(final int i) { return relativeTolerance[i]; } @Override int getValidDataSize(final int i) { return validDataSize[i]; } @Override UnivariateFunction getValidFunction() { return new Sinc(); } @Override double getValidLowerBound() { return 0.0; } @Override double getValidUpperBound() { return FastMath.PI; } @Override double[] transform(final double[] x, final TransformType type) { final int n = x.length; final double[] y = new double[n]; final double[] sin = new double[2 * n]; for (int i = 0; i < sin.length; i++) { sin[i] = FastMath.sin(FastMath.PI * i / (double) n); } for (int j = 0; j < n; j++) { double yj = 0.0; for (int i = 0; i < n; i++) { yj += x[i] * sin[(i * j) % sin.length]; } y[j] = yj; } final double s; if (type == TransformType.FORWARD) { if (normalization == DstNormalization.STANDARD_DST_I) { s = 1.0; } else if (normalization == DstNormalization.ORTHOGONAL_DST_I) { s = FastMath.sqrt(2.0 / (double) n); } else { throw new MathIllegalStateException(); } } else if (type == TransformType.INVERSE) { if (normalization == DstNormalization.STANDARD_DST_I) { s = 2.0 / n; } else if (normalization == DstNormalization.ORTHOGONAL_DST_I) { s = FastMath.sqrt(2.0 / (double) n); } else { throw new MathIllegalStateException(); } } else { /* * Should never occur. This clause is a safeguard in case other * types are used to TransformType (which should not be done). */ throw new MathIllegalStateException(); } TransformUtils.scaleArray(y, s); return y; } /* * Additional tests. */ @Test public void testTransformRealFirstElementNotZero() { final TransformType[] type = TransformType.values(); final double[] data = new double[] { 1.0, 1.0, 1.0, 1.0 }; final RealTransformer transformer = createRealTransformer(); for (int j = 0; j < type.length; j++) { try { transformer.transform(data, type[j]); Assert.fail(type[j].toString()); } catch (MathIllegalArgumentException e) { // Expected: do nothing } } } /* * Additional (legacy) tests. */ /** * Test of transformer for the ad hoc data. */ @Test public void testAdHocData() { FastSineTransformer transformer; transformer = new FastSineTransformer(DstNormalization.STANDARD_DST_I); double result[], tolerance = 1E-12; double x[] = { 0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0 }; double y[] = { 0.0, 20.1093579685034, -9.65685424949238, 5.98642305066196, -4.0, 2.67271455167720, -1.65685424949238, 0.795649469518633 }; result = transformer.transform(x, TransformType.FORWARD); for (int i = 0; i < result.length; i++) { Assert.assertEquals(y[i], result[i], tolerance); } result = transformer.transform(y, TransformType.INVERSE); for (int i = 0; i < result.length; i++) { Assert.assertEquals(x[i], result[i], tolerance); } TransformUtils.scaleArray(x, FastMath.sqrt(x.length / 2.0)); transformer = new FastSineTransformer(DstNormalization.ORTHOGONAL_DST_I); result = transformer.transform(y, TransformType.FORWARD); for (int i = 0; i < result.length; i++) { Assert.assertEquals(x[i], result[i], tolerance); } result = transformer.transform(x, TransformType.INVERSE); for (int i = 0; i < result.length; i++) { Assert.assertEquals(y[i], result[i], tolerance); } } /** * Test of transformer for the sine function. */ @Test public void testSinFunction() { UnivariateFunction f = new Sin(); FastSineTransformer transformer; transformer = new FastSineTransformer(DstNormalization.STANDARD_DST_I); double min, max, result[], tolerance = 1E-12; int N = 1 << 8; min = 0.0; max = 2.0 * FastMath.PI; result = transformer.transform(f, min, max, N, TransformType.FORWARD); Assert.assertEquals(N >> 1, result[2], tolerance); for (int i = 0; i < N; i += (i == 1 ? 2 : 1)) { Assert.assertEquals(0.0, result[i], tolerance); } min = -FastMath.PI; max = FastMath.PI; result = transformer.transform(f, min, max, N, TransformType.FORWARD); Assert.assertEquals(-(N >> 1), result[2], tolerance); for (int i = 0; i < N; i += (i == 1 ? 2 : 1)) { Assert.assertEquals(0.0, result[i], tolerance); } } /** * Test of parameters for the transformer. */ @Test public void testParameters() throws Exception { UnivariateFunction f = new Sin(); FastSineTransformer transformer; transformer = new FastSineTransformer(DstNormalization.STANDARD_DST_I); try { // bad interval transformer.transform(f, 1, -1, 64, TransformType.FORWARD); Assert.fail("Expecting IllegalArgumentException - bad interval"); } catch (IllegalArgumentException ex) { // expected } try { // bad samples number transformer.transform(f, -1, 1, 0, TransformType.FORWARD); Assert.fail("Expecting IllegalArgumentException - bad samples number"); } catch (IllegalArgumentException ex) { // expected } try { // bad samples number transformer.transform(f, -1, 1, 100, TransformType.FORWARD); Assert.fail("Expecting IllegalArgumentException - bad samples number"); } catch (IllegalArgumentException ex) { // expected } } } ././@LongLink100644 0 0 147 12126630646 10263 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/transform/FastFourierTransformerTest.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/transform/FastFourierTransformerTest.ja100644 1750 1750 55365 12126627674 32465 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.transform; import java.util.Random; import org.apache.commons.math3.analysis.UnivariateFunction; import org.apache.commons.math3.analysis.function.Sin; import org.apache.commons.math3.analysis.function.Sinc; import org.apache.commons.math3.complex.Complex; import org.apache.commons.math3.exception.MathIllegalArgumentException; import org.apache.commons.math3.exception.NotStrictlyPositiveException; import org.apache.commons.math3.exception.NumberIsTooLargeException; import org.apache.commons.math3.util.FastMath; import org.junit.Assert; import org.junit.Test; /** * Test case for fast Fourier transformer. *

              * FFT algorithm is exact, the small tolerance number is used only * to account for round-off errors. * * @version $Id: FastFourierTransformerTest.java 1374632 2012-08-18 18:11:11Z luc $ */ public final class FastFourierTransformerTest { /** The common seed of all random number generators used in this test. */ private final static long SEED = 20110111L; /* * Precondition checks. */ @Test public void testTransformComplexSizeNotAPowerOfTwo() { final int n = 127; final Complex[] x = createComplexData(n); final DftNormalization[] norm; norm = DftNormalization.values(); final TransformType[] type; type = TransformType.values(); for (int i = 0; i < norm.length; i++) { for (int j = 0; j < type.length; j++) { final FastFourierTransformer fft; fft = new FastFourierTransformer(norm[i]); try { fft.transform(x, type[j]); Assert.fail(norm[i] + ", " + type[j] + ": MathIllegalArgumentException was expected"); } catch (MathIllegalArgumentException e) { // Expected behaviour } } } } @Test public void testTransformRealSizeNotAPowerOfTwo() { final int n = 127; final double[] x = createRealData(n); final DftNormalization[] norm; norm = DftNormalization.values(); final TransformType[] type; type = TransformType.values(); for (int i = 0; i < norm.length; i++) { for (int j = 0; j < type.length; j++) { final FastFourierTransformer fft; fft = new FastFourierTransformer(norm[i]); try { fft.transform(x, type[j]); Assert.fail(norm[i] + ", " + type[j] + ": MathIllegalArgumentException was expected"); } catch (MathIllegalArgumentException e) { // Expected behaviour } } } } @Test public void testTransformFunctionSizeNotAPowerOfTwo() { final int n = 127; final UnivariateFunction f = new Sin(); final DftNormalization[] norm; norm = DftNormalization.values(); final TransformType[] type; type = TransformType.values(); for (int i = 0; i < norm.length; i++) { for (int j = 0; j < type.length; j++) { final FastFourierTransformer fft; fft = new FastFourierTransformer(norm[i]); try { fft.transform(f, 0.0, Math.PI, n, type[j]); Assert.fail(norm[i] + ", " + type[j] + ": MathIllegalArgumentException was expected"); } catch (MathIllegalArgumentException e) { // Expected behaviour } } } } @Test public void testTransformFunctionNotStrictlyPositiveNumberOfSamples() { final int n = -128; final UnivariateFunction f = new Sin(); final DftNormalization[] norm; norm = DftNormalization.values(); final TransformType[] type; type = TransformType.values(); for (int i = 0; i < norm.length; i++) { for (int j = 0; j < type.length; j++) { final FastFourierTransformer fft; fft = new FastFourierTransformer(norm[i]); try { fft.transform(f, 0.0, Math.PI, n, type[j]); fft.transform(f, 0.0, Math.PI, n, type[j]); Assert.fail(norm[i] + ", " + type[j] + ": NotStrictlyPositiveException was expected"); } catch (NotStrictlyPositiveException e) { // Expected behaviour } } } } @Test public void testTransformFunctionInvalidBounds() { final int n = 128; final UnivariateFunction f = new Sin(); final DftNormalization[] norm; norm = DftNormalization.values(); final TransformType[] type; type = TransformType.values(); for (int i = 0; i < norm.length; i++) { for (int j = 0; j < type.length; j++) { final FastFourierTransformer fft; fft = new FastFourierTransformer(norm[i]); try { fft.transform(f, Math.PI, 0.0, n, type[j]); Assert.fail(norm[i] + ", " + type[j] + ": NumberIsTooLargeException was expected"); } catch (NumberIsTooLargeException e) { // Expected behaviour } } } } /* * Utility methods for checking (successful) transforms. */ private static Complex[] createComplexData(final int n) { final Random random = new Random(SEED); final Complex[] data = new Complex[n]; for (int i = 0; i < n; i++) { final double re = 2.0 * random.nextDouble() - 1.0; final double im = 2.0 * random.nextDouble() - 1.0; data[i] = new Complex(re, im); } return data; } private static double[] createRealData(final int n) { final Random random = new Random(SEED); final double[] data = new double[n]; for (int i = 0; i < n; i++) { data[i] = 2.0 * random.nextDouble() - 1.0; } return data; } /** Naive implementation of DFT, for reference. */ private static Complex[] dft(final Complex[] x, final int sgn) { final int n = x.length; final double[] cos = new double[n]; final double[] sin = new double[n]; final Complex[] y = new Complex[n]; for (int i = 0; i < n; i++) { final double arg = 2.0 * FastMath.PI * i / n; cos[i] = FastMath.cos(arg); sin[i] = FastMath.sin(arg); } for (int i = 0; i < n; i++) { double yr = 0.0; double yi = 0.0; for (int j = 0; j < n; j++) { final int index = (i * j) % n; final double c = cos[index]; final double s = sin[index]; final double xr = x[j].getReal(); final double xi = x[j].getImaginary(); yr += c * xr - sgn * s * xi; yi += sgn * s * xr + c * xi; } y[i] = new Complex(yr, yi); } return y; } private static void doTestTransformComplex(final int n, final double tol, final DftNormalization normalization, final TransformType type) { final FastFourierTransformer fft; fft = new FastFourierTransformer(normalization); final Complex[] x = createComplexData(n); final Complex[] expected; final double s; if (type==TransformType.FORWARD) { expected = dft(x, -1); if (normalization == DftNormalization.STANDARD){ s = 1.0; } else { s = 1.0 / FastMath.sqrt(n); } } else { expected = dft(x, 1); if (normalization == DftNormalization.STANDARD) { s = 1.0 / n; } else { s = 1.0 / FastMath.sqrt(n); } } final Complex[] actual = fft.transform(x, type); for (int i = 0; i < n; i++) { final String msg; msg = String.format("%s, %s, %d, %d", normalization, type, n, i); final double re = s * expected[i].getReal(); Assert.assertEquals(msg, re, actual[i].getReal(), tol * FastMath.abs(re)); final double im = s * expected[i].getImaginary(); Assert.assertEquals(msg, im, actual[i].getImaginary(), tol * FastMath.abs(re)); } } private static void doTestTransformReal(final int n, final double tol, final DftNormalization normalization, final TransformType type) { final FastFourierTransformer fft; fft = new FastFourierTransformer(normalization); final double[] x = createRealData(n); final Complex[] xc = new Complex[n]; for (int i = 0; i < n; i++) { xc[i] = new Complex(x[i], 0.0); } final Complex[] expected; final double s; if (type == TransformType.FORWARD) { expected = dft(xc, -1); if (normalization == DftNormalization.STANDARD) { s = 1.0; } else { s = 1.0 / FastMath.sqrt(n); } } else { expected = dft(xc, 1); if (normalization == DftNormalization.STANDARD) { s = 1.0 / n; } else { s = 1.0 / FastMath.sqrt(n); } } final Complex[] actual = fft.transform(x, type); for (int i = 0; i < n; i++) { final String msg; msg = String.format("%s, %s, %d, %d", normalization, type, n, i); final double re = s * expected[i].getReal(); Assert.assertEquals(msg, re, actual[i].getReal(), tol * FastMath.abs(re)); final double im = s * expected[i].getImaginary(); Assert.assertEquals(msg, im, actual[i].getImaginary(), tol * FastMath.abs(re)); } } private static void doTestTransformFunction(final UnivariateFunction f, final double min, final double max, int n, final double tol, final DftNormalization normalization, final TransformType type) { final FastFourierTransformer fft; fft = new FastFourierTransformer(normalization); final Complex[] x = new Complex[n]; for (int i = 0; i < n; i++) { final double t = min + i * (max - min) / n; x[i] = new Complex(f.value(t)); } final Complex[] expected; final double s; if (type == TransformType.FORWARD) { expected = dft(x, -1); if (normalization == DftNormalization.STANDARD) { s = 1.0; } else { s = 1.0 / FastMath.sqrt(n); } } else { expected = dft(x, 1); if (normalization == DftNormalization.STANDARD) { s = 1.0 / n; } else { s = 1.0 / FastMath.sqrt(n); } } final Complex[] actual = fft.transform(f, min, max, n, type); for (int i = 0; i < n; i++) { final String msg = String.format("%d, %d", n, i); final double re = s * expected[i].getReal(); Assert.assertEquals(msg, re, actual[i].getReal(), tol * FastMath.abs(re)); final double im = s * expected[i].getImaginary(); Assert.assertEquals(msg, im, actual[i].getImaginary(), tol * FastMath.abs(re)); } } /* * Tests of standard transform (when data is valid). */ @Test public void testTransformComplex() { final DftNormalization[] norm; norm = DftNormalization.values(); final TransformType[] type; type = TransformType.values(); for (int i = 0; i < norm.length; i++) { for (int j = 0; j < type.length; j++) { doTestTransformComplex(2, 1.0E-15, norm[i], type[j]); doTestTransformComplex(4, 1.0E-14, norm[i], type[j]); doTestTransformComplex(8, 1.0E-14, norm[i], type[j]); doTestTransformComplex(16, 1.0E-13, norm[i], type[j]); doTestTransformComplex(32, 1.0E-13, norm[i], type[j]); doTestTransformComplex(64, 1.0E-12, norm[i], type[j]); doTestTransformComplex(128, 1.0E-12, norm[i], type[j]); } } } @Test public void testStandardTransformReal() { final DftNormalization[] norm; norm = DftNormalization.values(); final TransformType[] type; type = TransformType.values(); for (int i = 0; i < norm.length; i++) { for (int j = 0; j < type.length; j++) { doTestTransformReal(2, 1.0E-15, norm[i], type[j]); doTestTransformReal(4, 1.0E-14, norm[i], type[j]); doTestTransformReal(8, 1.0E-14, norm[i], type[j]); doTestTransformReal(16, 1.0E-13, norm[i], type[j]); doTestTransformReal(32, 1.0E-13, norm[i], type[j]); doTestTransformReal(64, 1.0E-13, norm[i], type[j]); doTestTransformReal(128, 1.0E-11, norm[i], type[j]); } } } @Test public void testStandardTransformFunction() { final UnivariateFunction f = new Sinc(); final double min = -FastMath.PI; final double max = FastMath.PI; final DftNormalization[] norm; norm = DftNormalization.values(); final TransformType[] type; type = TransformType.values(); for (int i = 0; i < norm.length; i++) { for (int j = 0; j < type.length; j++) { doTestTransformFunction(f, min, max, 2, 1.0E-15, norm[i], type[j]); doTestTransformFunction(f, min, max, 4, 1.0E-14, norm[i], type[j]); doTestTransformFunction(f, min, max, 8, 1.0E-14, norm[i], type[j]); doTestTransformFunction(f, min, max, 16, 1.0E-13, norm[i], type[j]); doTestTransformFunction(f, min, max, 32, 1.0E-13, norm[i], type[j]); doTestTransformFunction(f, min, max, 64, 1.0E-12, norm[i], type[j]); doTestTransformFunction(f, min, max, 128, 1.0E-11, norm[i], type[j]); } } } /* * Additional tests for 1D data. */ /** * Test of transformer for the ad hoc data taken from Mathematica. */ @Test public void testAdHocData() { FastFourierTransformer transformer; transformer = new FastFourierTransformer(DftNormalization.STANDARD); Complex result[]; double tolerance = 1E-12; double x[] = {1.3, 2.4, 1.7, 4.1, 2.9, 1.7, 5.1, 2.7}; Complex y[] = { new Complex(21.9, 0.0), new Complex(-2.09497474683058, 1.91507575950825), new Complex(-2.6, 2.7), new Complex(-1.10502525316942, -4.88492424049175), new Complex(0.1, 0.0), new Complex(-1.10502525316942, 4.88492424049175), new Complex(-2.6, -2.7), new Complex(-2.09497474683058, -1.91507575950825)}; result = transformer.transform(x, TransformType.FORWARD); for (int i = 0; i < result.length; i++) { Assert.assertEquals(y[i].getReal(), result[i].getReal(), tolerance); Assert.assertEquals(y[i].getImaginary(), result[i].getImaginary(), tolerance); } result = transformer.transform(y, TransformType.INVERSE); for (int i = 0; i < result.length; i++) { Assert.assertEquals(x[i], result[i].getReal(), tolerance); Assert.assertEquals(0.0, result[i].getImaginary(), tolerance); } double x2[] = {10.4, 21.6, 40.8, 13.6, 23.2, 32.8, 13.6, 19.2}; TransformUtils.scaleArray(x2, 1.0 / FastMath.sqrt(x2.length)); Complex y2[] = y; transformer = new FastFourierTransformer(DftNormalization.UNITARY); result = transformer.transform(y2, TransformType.FORWARD); for (int i = 0; i < result.length; i++) { Assert.assertEquals(x2[i], result[i].getReal(), tolerance); Assert.assertEquals(0.0, result[i].getImaginary(), tolerance); } result = transformer.transform(x2, TransformType.INVERSE); for (int i = 0; i < result.length; i++) { Assert.assertEquals(y2[i].getReal(), result[i].getReal(), tolerance); Assert.assertEquals(y2[i].getImaginary(), result[i].getImaginary(), tolerance); } } /** * Test of transformer for the sine function. */ @Test public void testSinFunction() { UnivariateFunction f = new Sin(); FastFourierTransformer transformer; transformer = new FastFourierTransformer(DftNormalization.STANDARD); Complex result[]; int N = 1 << 8; double min, max, tolerance = 1E-12; min = 0.0; max = 2.0 * FastMath.PI; result = transformer.transform(f, min, max, N, TransformType.FORWARD); Assert.assertEquals(0.0, result[1].getReal(), tolerance); Assert.assertEquals(-(N >> 1), result[1].getImaginary(), tolerance); Assert.assertEquals(0.0, result[N-1].getReal(), tolerance); Assert.assertEquals(N >> 1, result[N-1].getImaginary(), tolerance); for (int i = 0; i < N-1; i += (i == 0 ? 2 : 1)) { Assert.assertEquals(0.0, result[i].getReal(), tolerance); Assert.assertEquals(0.0, result[i].getImaginary(), tolerance); } min = -FastMath.PI; max = FastMath.PI; result = transformer.transform(f, min, max, N, TransformType.INVERSE); Assert.assertEquals(0.0, result[1].getReal(), tolerance); Assert.assertEquals(-0.5, result[1].getImaginary(), tolerance); Assert.assertEquals(0.0, result[N-1].getReal(), tolerance); Assert.assertEquals(0.5, result[N-1].getImaginary(), tolerance); for (int i = 0; i < N-1; i += (i == 0 ? 2 : 1)) { Assert.assertEquals(0.0, result[i].getReal(), tolerance); Assert.assertEquals(0.0, result[i].getImaginary(), tolerance); } } /* * Additional tests for 2D data. */ @Test public void test2DData() { FastFourierTransformer transformer; transformer = new FastFourierTransformer(DftNormalization.STANDARD); double tolerance = 1E-12; Complex[][] input = new Complex[][] {new Complex[] {new Complex(1, 0), new Complex(2, 0)}, new Complex[] {new Complex(3, 1), new Complex(4, 2)}}; Complex[][] goodOutput = new Complex[][] {new Complex[] {new Complex(5, 1.5), new Complex(-1, -.5)}, new Complex[] {new Complex(-2, -1.5), new Complex(0, .5)}}; for (int i = 0; i < goodOutput.length; i++) { TransformUtils.scaleArray( goodOutput[i], FastMath.sqrt(goodOutput[i].length) * FastMath.sqrt(goodOutput.length)); } Complex[][] output = (Complex[][])transformer.mdfft(input, TransformType.FORWARD); Complex[][] output2 = (Complex[][])transformer.mdfft(output, TransformType.INVERSE); Assert.assertEquals(input.length, output.length); Assert.assertEquals(input.length, output2.length); Assert.assertEquals(input[0].length, output[0].length); Assert.assertEquals(input[0].length, output2[0].length); Assert.assertEquals(input[1].length, output[1].length); Assert.assertEquals(input[1].length, output2[1].length); for (int i = 0; i < input.length; i++) { for (int j = 0; j < input[0].length; j++) { Assert.assertEquals(input[i][j].getImaginary(), output2[i][j].getImaginary(), tolerance); Assert.assertEquals(input[i][j].getReal(), output2[i][j].getReal(), tolerance); Assert.assertEquals(goodOutput[i][j].getImaginary(), output[i][j].getImaginary(), tolerance); Assert.assertEquals(goodOutput[i][j].getReal(), output[i][j].getReal(), tolerance); } } } @Test public void test2DDataUnitary() { FastFourierTransformer transformer; transformer = new FastFourierTransformer(DftNormalization.UNITARY); double tolerance = 1E-12; Complex[][] input = new Complex[][] {new Complex[] {new Complex(1, 0), new Complex(2, 0)}, new Complex[] {new Complex(3, 1), new Complex(4, 2)}}; Complex[][] goodOutput = new Complex[][] {new Complex[] {new Complex(5, 1.5), new Complex(-1, -.5)}, new Complex[] {new Complex(-2, -1.5), new Complex(0, .5)}}; Complex[][] output = (Complex[][])transformer.mdfft(input, TransformType.FORWARD); Complex[][] output2 = (Complex[][])transformer.mdfft(output, TransformType.INVERSE); Assert.assertEquals(input.length, output.length); Assert.assertEquals(input.length, output2.length); Assert.assertEquals(input[0].length, output[0].length); Assert.assertEquals(input[0].length, output2[0].length); Assert.assertEquals(input[1].length, output[1].length); Assert.assertEquals(input[1].length, output2[1].length); for (int i = 0; i < input.length; i++) { for (int j = 0; j < input[0].length; j++) { Assert.assertEquals(input[i][j].getImaginary(), output2[i][j].getImaginary(), tolerance); Assert.assertEquals(input[i][j].getReal(), output2[i][j].getReal(), tolerance); Assert.assertEquals(goodOutput[i][j].getImaginary(), output[i][j].getImaginary(), tolerance); Assert.assertEquals(goodOutput[i][j].getReal(), output[i][j].getReal(), tolerance); } } } } ././@LongLink100644 0 0 146 12126630646 10262 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/transform/FastCosineTransformerTest.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/transform/FastCosineTransformerTest.jav100644 1750 1750 22057 12126627674 32450 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.transform; import java.util.Arrays; import java.util.Collection; import org.apache.commons.math3.analysis.UnivariateFunction; import org.apache.commons.math3.analysis.function.Sin; import org.apache.commons.math3.analysis.function.Sinc; import org.apache.commons.math3.exception.MathIllegalStateException; import org.apache.commons.math3.util.FastMath; import org.junit.Assert; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.Parameterized; import org.junit.runners.Parameterized.Parameters; /** * Test case for fast cosine transformer. *

              * FCT algorithm is exact, the small tolerance number is used only to account * for round-off errors. * * @version $Id: FastCosineTransformerTest.java 1374632 2012-08-18 18:11:11Z luc $ */ @RunWith(value = Parameterized.class) public final class FastCosineTransformerTest extends RealTransformerAbstractTest { private DctNormalization normalization; private final int[] invalidDataSize; private final double[] relativeTolerance; private final int[] validDataSize; public FastCosineTransformerTest(final DctNormalization normalization) { this.normalization = normalization; this.validDataSize = new int[] { 2, 3, 5, 9, 17, 33, 65, 129 }; this.invalidDataSize = new int[] { 128 }; this.relativeTolerance = new double[] { 1E-15, 1E-15, 1E-14, 1E-13, 1E-13, 1E-12, 1E-11, 1E-10 }; } /** * Returns an array containing {@code true, false} in order to check both * standard and orthogonal DCTs. * * @return an array of parameters for this parameterized test */ @Parameters public static Collection data() { final DctNormalization[] normalization = DctNormalization.values(); final Object[][] data = new DctNormalization[normalization.length][1]; for (int i = 0; i < normalization.length; i++){ data[i][0] = normalization[i]; } return Arrays.asList(data); } @Override RealTransformer createRealTransformer() { return new FastCosineTransformer(normalization); } @Override int getInvalidDataSize(final int i) { return invalidDataSize[i]; } @Override int getNumberOfInvalidDataSizes() { return invalidDataSize.length; } @Override int getNumberOfValidDataSizes() { return validDataSize.length; } @Override double getRelativeTolerance(final int i) { return relativeTolerance[i]; } @Override int getValidDataSize(final int i) { return validDataSize[i]; } @Override UnivariateFunction getValidFunction() { return new Sinc(); } @Override double getValidLowerBound() { return 0.0; } @Override double getValidUpperBound() { return FastMath.PI; } @Override double[] transform(final double[] x, final TransformType type) { final int n = x.length; final double[] y = new double[n]; final double[] cos = new double[2 * (n - 1)]; for (int i = 0; i < cos.length; i++) { cos[i] = FastMath.cos(FastMath.PI * i / (n - 1.0)); } int sgn = 1; for (int j = 0; j < n; j++) { double yj = 0.5 * (x[0] + sgn * x[n - 1]); for (int i = 1; i < n - 1; i++) { yj += x[i] * cos[(i * j) % cos.length]; } y[j] = yj; sgn *= -1; } final double s; if (type == TransformType.FORWARD) { if (normalization == DctNormalization.STANDARD_DCT_I) { s = 1.0; } else if (normalization == DctNormalization.ORTHOGONAL_DCT_I) { s = FastMath.sqrt(2.0 / (n - 1.0)); } else { throw new MathIllegalStateException(); } } else if (type == TransformType.INVERSE) { if (normalization == DctNormalization.STANDARD_DCT_I) { s = 2.0 / (n - 1.0); } else if (normalization == DctNormalization.ORTHOGONAL_DCT_I) { s = FastMath.sqrt(2.0 / (n - 1.0)); } else { throw new MathIllegalStateException(); } } else { /* * Should never occur. This clause is a safeguard in case other * types are used to TransformType (which should not be done). */ throw new MathIllegalStateException(); } TransformUtils.scaleArray(y, s); return y; } /* * Additional tests. */ /** Test of transformer for the ad hoc data. */ @Test public void testAdHocData() { FastCosineTransformer transformer; transformer = new FastCosineTransformer(DctNormalization.STANDARD_DCT_I); double result[], tolerance = 1E-12; double x[] = { 0.0, 1.0, 4.0, 9.0, 16.0, 25.0, 36.0, 49.0, 64.0 }; double y[] = { 172.0, -105.096569476353, 27.3137084989848, -12.9593152353742, 8.0, -5.78585076868676, 4.68629150101524, -4.15826451958632, 4.0 }; result = transformer.transform(x, TransformType.FORWARD); for (int i = 0; i < result.length; i++) { Assert.assertEquals(y[i], result[i], tolerance); } result = transformer.transform(y, TransformType.INVERSE); for (int i = 0; i < result.length; i++) { Assert.assertEquals(x[i], result[i], tolerance); } TransformUtils.scaleArray(x, FastMath.sqrt(0.5 * (x.length - 1))); transformer = new FastCosineTransformer(DctNormalization.ORTHOGONAL_DCT_I); result = transformer.transform(y, TransformType.FORWARD); for (int i = 0; i < result.length; i++) { Assert.assertEquals(x[i], result[i], tolerance); } result = transformer.transform(x, TransformType.INVERSE); for (int i = 0; i < result.length; i++) { Assert.assertEquals(y[i], result[i], tolerance); } } /** Test of parameters for the transformer. */ @Test public void testParameters() throws Exception { UnivariateFunction f = new Sin(); FastCosineTransformer transformer; transformer = new FastCosineTransformer(DctNormalization.STANDARD_DCT_I); try { // bad interval transformer.transform(f, 1, -1, 65, TransformType.FORWARD); Assert.fail("Expecting IllegalArgumentException - bad interval"); } catch (IllegalArgumentException ex) { // expected } try { // bad samples number transformer.transform(f, -1, 1, 1, TransformType.FORWARD); Assert .fail("Expecting IllegalArgumentException - bad samples number"); } catch (IllegalArgumentException ex) { // expected } try { // bad samples number transformer.transform(f, -1, 1, 64, TransformType.FORWARD); Assert .fail("Expecting IllegalArgumentException - bad samples number"); } catch (IllegalArgumentException ex) { // expected } } /** Test of transformer for the sine function. */ @Test public void testSinFunction() { UnivariateFunction f = new Sin(); FastCosineTransformer transformer; transformer = new FastCosineTransformer(DctNormalization.STANDARD_DCT_I); double min, max, result[], tolerance = 1E-12; int N = 9; double expected[] = { 0.0, 3.26197262739567, 0.0, -2.17958042710327, 0.0, -0.648846697642915, 0.0, -0.433545502649478, 0.0 }; min = 0.0; max = 2.0 * FastMath.PI * N / (N - 1); result = transformer.transform(f, min, max, N, TransformType.FORWARD); for (int i = 0; i < N; i++) { Assert.assertEquals(expected[i], result[i], tolerance); } min = -FastMath.PI; max = FastMath.PI * (N + 1) / (N - 1); result = transformer.transform(f, min, max, N, TransformType.FORWARD); for (int i = 0; i < N; i++) { Assert.assertEquals(-expected[i], result[i], tolerance); } } } ././@LongLink100644 0 0 150 12126630646 10255 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/transform/RealTransformerAbstractTest.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/transform/RealTransformerAbstractTest.j100644 1750 1750 32626 12126627674 32435 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.transform; import java.util.Random; import org.apache.commons.math3.analysis.UnivariateFunction; import org.apache.commons.math3.exception.MathIllegalArgumentException; import org.apache.commons.math3.exception.NotStrictlyPositiveException; import org.apache.commons.math3.exception.NumberIsTooLargeException; import org.apache.commons.math3.util.FastMath; import org.junit.Assert; import org.junit.Test; /** * Abstract test for classes implementing the {@link RealTransformer} interface. * This abstract test handles the automatic generation of random data of various * sizes. For each generated data array, actual values (returned by the * transformer to be tested) are compared to expected values, returned by the * {@link #transform(double[], TransformType)} (to be implemented by the user: * a naive method may be used). Methods are also provided to test that invalid * parameters throw the expected exceptions. * * @since 3.0 * @version $Id: RealTransformerAbstractTest.java 1244375 2012-02-15 06:30:05Z celestin $ */ public abstract class RealTransformerAbstractTest { /** The common seed of all random number generators used in this test. */ private final static long SEED = 20110119L; /** * Returns a new instance of the {@link RealTransformer} to be tested. * * @return a the transformer to be tested */ abstract RealTransformer createRealTransformer(); /** * Returns an invalid data size. Transforms with this data size should * trigger a {@link MathIllegalArgumentException}. * * @param i the index of the invalid data size ({@code 0 <= i <} * {@link #getNumberOfInvalidDataSizes()} * @return an invalid data size */ abstract int getInvalidDataSize(int i); /** * Returns the total number of invalid data sizes to be tested. If data * array of any * size can be handled by the {@link RealTransformer} to be tested, this * method should return {@code 0}. * * @return the total number of invalid data sizes */ abstract int getNumberOfInvalidDataSizes(); /** * Returns the total number of valid data sizes to be tested. * * @return the total number of valid data sizes */ abstract int getNumberOfValidDataSizes(); /** * Returns the expected relative accuracy for data arrays of size * {@code getValidDataSize(i)}. * * @param i the index of the valid data size * @return the expected relative accuracy */ abstract double getRelativeTolerance(int i); /** * Returns a valid data size. This method allows for data arrays of various * sizes to be automatically tested (by allowing multiple values of the * specified index). * * @param i the index of the valid data size ({@code 0 <= i <} * {@link #getNumberOfValidDataSizes()} * @return a valid data size */ abstract int getValidDataSize(int i); /** * Returns a function for the accuracy check of * {@link RealTransformer#transform(UnivariateFunction, double, double, int)} * and * {@link RealTransformer#inverseTransform(UnivariateFunction, double, double, int)}. * This function should be valid. In other words, none of the above methods * should throw an exception when passed this function. * * @return a valid function */ abstract UnivariateFunction getValidFunction(); /** * Returns a sampling lower bound for the accuracy check of * {@link RealTransformer#transform(UnivariateFunction, double, double, int)} * and * {@link RealTransformer#inverseTransform(UnivariateFunction, double, double, int)}. * This lower bound should be valid. In other words, none of the above * methods should throw an exception when passed this bound. * * @return a valid lower bound */ abstract double getValidLowerBound(); /** * Returns a sampling upper bound for the accuracy check of * {@link RealTransformer#transform(UnivariateFunction, double, double, int)} * and * {@link RealTransformer#inverseTransform(UnivariateFunction, double, double, int)}. * This upper bound should be valid. In other words, none of the above * methods should throw an exception when passed this bound. * * @return a valid bound */ abstract double getValidUpperBound(); /** * Returns the expected transform of the specified real data array. * * @param x the real data array to be transformed * @param type the type of transform (forward, inverse) to be performed * @return the expected transform */ abstract double[] transform(double[] x, TransformType type); /* * Check of preconditions. */ /** * {@link RealTransformer#transform(double[], TransformType)} should throw a * {@link MathIllegalArgumentException} if data size is invalid. */ @Test public void testTransformRealInvalidDataSize() { final TransformType[] type = TransformType.values(); final RealTransformer transformer = createRealTransformer(); for (int i = 0; i < getNumberOfInvalidDataSizes(); i++) { final int n = getInvalidDataSize(i); for (int j = 0; j < type.length; j++) { try { transformer.transform(createRealData(n), type[j]); Assert.fail(type[j] + ", " + n); } catch (MathIllegalArgumentException e) { // Expected: do nothing } } } } /** * {@link RealTransformer#transform(UnivariateFunction, double, double, int, TransformType)} * should throw a {@link MathIllegalArgumentException} if number of samples * is invalid. */ @Test public void testTransformFunctionInvalidDataSize() { final TransformType[] type = TransformType.values(); final RealTransformer transformer = createRealTransformer(); final UnivariateFunction f = getValidFunction(); final double a = getValidLowerBound(); final double b = getValidUpperBound(); for (int i = 0; i < getNumberOfInvalidDataSizes(); i++) { final int n = getInvalidDataSize(i); for (int j = 0; j < type.length; j++) { try { transformer.transform(f, a, b, n, type[j]); Assert.fail(type[j] + ", " + n); } catch (MathIllegalArgumentException e) { // Expected: do nothing } } } } /** * {@link RealTransformer#transform(UnivariateFunction, double, double, int, TransformType)} * should throw a {@link NotStrictlyPositiveException} if number of samples * is not strictly positive. */ @Test public void testTransformFunctionNotStrictlyPositiveNumberOfSamples() { final TransformType[] type = TransformType.values(); final RealTransformer transformer = createRealTransformer(); final UnivariateFunction f = getValidFunction(); final double a = getValidLowerBound(); final double b = getValidUpperBound(); for (int i = 0; i < getNumberOfValidDataSizes(); i++) { final int n = getValidDataSize(i); for (int j = 0; j < type.length; j++) { try { transformer.transform(f, a, b, -n, type[j]); Assert.fail(type[j] + ", " + (-n)); } catch (NotStrictlyPositiveException e) { // Expected: do nothing } } } } /** * {@link RealTransformer#transform(UnivariateFunction, double, double, int, TransformType)} * should throw a {@link NumberIsTooLargeException} if sampling bounds are * not correctly ordered. */ @Test public void testTransformFunctionInvalidBounds() { final TransformType[] type = TransformType.values(); final RealTransformer transformer = createRealTransformer(); final UnivariateFunction f = getValidFunction(); final double a = getValidLowerBound(); final double b = getValidUpperBound(); for (int i = 0; i < getNumberOfValidDataSizes(); i++) { final int n = getValidDataSize(i); for (int j = 0; j < type.length; j++) { try { transformer.transform(f, b, a, n, type[j]); Assert.fail(type[j] + ", " + b + ", " + a); } catch (NumberIsTooLargeException e) { // Expected: do nothing } } } } /* * Accuracy tests of transform of valid data. */ /** * Accuracy check of {@link RealTransformer#transform(double[], TransformType)}. * For each valid data size returned by * {@link #getValidDataSize(int) getValidDataSize(i)}, * a random data array is generated with * {@link #createRealData(int) createRealData(i)}. The actual * transform is computed and compared to the expected transform, return by * {@link #transform(double[], TransformType)}. Actual and expected values * should be equal to within the relative error returned by * {@link #getRelativeTolerance(int) getRelativeTolerance(i)}. */ @Test public void testTransformReal() { final TransformType[] type = TransformType.values(); for (int i = 0; i < getNumberOfValidDataSizes(); i++) { final int n = getValidDataSize(i); final double tol = getRelativeTolerance(i); for (int j = 0; j < type.length; j++) { doTestTransformReal(n, tol, type[j]); } } } /** * Accuracy check of * {@link RealTransformer#transform(UnivariateFunction, double, double, int, TransformType)}. * For each valid data size returned by * {@link #getValidDataSize(int) getValidDataSize(i)}, * the {@link UnivariateFunction} returned by {@link #getValidFunction()} is * sampled. The actual transform is computed and compared to the expected * transform, return by {@link #transform(double[], TransformType)}. Actual * and expected values should be equal to within the relative error returned * by {@link #getRelativeTolerance(int) getRelativeTolerance(i)}. */ @Test public void testTransformFunction() { final TransformType[] type = TransformType.values(); for (int i = 0; i < getNumberOfValidDataSizes(); i++) { final int n = getValidDataSize(i); final double tol = getRelativeTolerance(i); for (int j = 0; j < type.length; j++) { doTestTransformFunction(n, tol, type[j]); } } } /* * Utility methods. */ /** * Returns a random array of doubles. Random generator always uses the same * seed. * * @param n the size of the array to be returned * @return a random array of specified size */ double[] createRealData(final int n) { final Random random = new Random(SEED); final double[] data = new double[n]; for (int i = 0; i < n; i++) { data[i] = 2.0 * random.nextDouble() - 1.0; } return data; } /* * The tests per se. */ private void doTestTransformReal(final int n, final double tol, final TransformType type) { final RealTransformer transformer = createRealTransformer(); final double[] x = createRealData(n); final double[] expected = transform(x, type); final double[] actual = transformer.transform(x, type); for (int i = 0; i < n; i++) { final String msg = String.format("%d, %d", n, i); final double delta = tol * FastMath.abs(expected[i]); Assert.assertEquals(msg, expected[i], actual[i], delta); } } private void doTestTransformFunction(final int n, final double tol, final TransformType type) { final RealTransformer transformer = createRealTransformer(); final UnivariateFunction f = getValidFunction(); final double a = getValidLowerBound(); final double b = getValidUpperBound(); final double[] x = createRealData(n); for (int i = 0; i < n; i++) { final double t = a + i * (b - a) / n; x[i] = f.value(t); } final double[] expected = transform(x, type); final double[] actual = transformer.transform(f, a, b, n, type); for (int i = 0; i < n; i++) { final String msg = String.format("%d, %d", n, i); final double delta = tol * FastMath.abs(expected[i]); Assert.assertEquals(msg, expected[i], actual[i], delta); } } } commons-math3-3.2-src/src/test/java/org/apache/commons/math3/filter/KalmanFilterTest.java100644 1750 1750 24502 12126627670 30144 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with this * work for additional information regarding copyright ownership. The ASF * licenses this file to You 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. */ package org.apache.commons.math3.filter; import org.apache.commons.math3.linear.Array2DRowRealMatrix; import org.apache.commons.math3.linear.ArrayRealVector; import org.apache.commons.math3.linear.MatrixDimensionMismatchException; import org.apache.commons.math3.linear.RealMatrix; import org.apache.commons.math3.linear.RealVector; import org.apache.commons.math3.random.JDKRandomGenerator; import org.apache.commons.math3.random.RandomGenerator; import org.apache.commons.math3.util.Precision; import org.junit.Assert; import org.junit.Test; /** * Tests for {@link KalmanFilter}. * * @version $Id$ */ public class KalmanFilterTest { @Test(expected=MatrixDimensionMismatchException.class) public void testTransitionMeasurementMatrixMismatch() { // A and H matrix do not match in dimensions // A = [ 1 ] RealMatrix A = new Array2DRowRealMatrix(new double[] { 1d }); // no control input RealMatrix B = null; // H = [ 1 1 ] RealMatrix H = new Array2DRowRealMatrix(new double[] { 1d, 1d }); // Q = [ 0 ] RealMatrix Q = new Array2DRowRealMatrix(new double[] { 0 }); // R = [ 0 ] RealMatrix R = new Array2DRowRealMatrix(new double[] { 0 }); ProcessModel pm = new DefaultProcessModel(A, B, Q, new ArrayRealVector(new double[] { 0 }), null); MeasurementModel mm = new DefaultMeasurementModel(H, R); new KalmanFilter(pm, mm); Assert.fail("transition and measurement matrix should not be compatible"); } @Test(expected=MatrixDimensionMismatchException.class) public void testTransitionControlMatrixMismatch() { // A and B matrix do not match in dimensions // A = [ 1 ] RealMatrix A = new Array2DRowRealMatrix(new double[] { 1d }); // B = [ 1 1 ] RealMatrix B = new Array2DRowRealMatrix(new double[] { 1d, 1d }); // H = [ 1 ] RealMatrix H = new Array2DRowRealMatrix(new double[] { 1d }); // Q = [ 0 ] RealMatrix Q = new Array2DRowRealMatrix(new double[] { 0 }); // R = [ 0 ] RealMatrix R = new Array2DRowRealMatrix(new double[] { 0 }); ProcessModel pm = new DefaultProcessModel(A, B, Q, new ArrayRealVector(new double[] { 0 }), null); MeasurementModel mm = new DefaultMeasurementModel(H, R); new KalmanFilter(pm, mm); Assert.fail("transition and control matrix should not be compatible"); } @Test public void testConstant() { // simulates a simple process with a constant state and no control input double constantValue = 10d; double measurementNoise = 0.1d; double processNoise = 1e-5d; // A = [ 1 ] RealMatrix A = new Array2DRowRealMatrix(new double[] { 1d }); // no control input RealMatrix B = null; // H = [ 1 ] RealMatrix H = new Array2DRowRealMatrix(new double[] { 1d }); // x = [ 10 ] RealVector x = new ArrayRealVector(new double[] { constantValue }); // Q = [ 1e-5 ] RealMatrix Q = new Array2DRowRealMatrix(new double[] { processNoise }); // R = [ 0.1 ] RealMatrix R = new Array2DRowRealMatrix(new double[] { measurementNoise }); ProcessModel pm = new DefaultProcessModel(A, B, Q, new ArrayRealVector(new double[] { constantValue }), null); MeasurementModel mm = new DefaultMeasurementModel(H, R); KalmanFilter filter = new KalmanFilter(pm, mm); Assert.assertEquals(1, filter.getMeasurementDimension()); Assert.assertEquals(1, filter.getStateDimension()); assertMatrixEquals(Q.getData(), filter.getErrorCovariance()); // check the initial state double[] expectedInitialState = new double[] { constantValue }; assertVectorEquals(expectedInitialState, filter.getStateEstimation()); RealVector pNoise = new ArrayRealVector(1); RealVector mNoise = new ArrayRealVector(1); RandomGenerator rand = new JDKRandomGenerator(); // iterate 60 steps for (int i = 0; i < 60; i++) { filter.predict(); // Simulate the process pNoise.setEntry(0, processNoise * rand.nextGaussian()); // x = A * x + p_noise x = A.operate(x).add(pNoise); // Simulate the measurement mNoise.setEntry(0, measurementNoise * rand.nextGaussian()); // z = H * x + m_noise RealVector z = H.operate(x).add(mNoise); filter.correct(z); // state estimate shouldn't be larger than measurement noise double diff = Math.abs(constantValue - filter.getStateEstimation()[0]); // System.out.println(diff); Assert.assertTrue(Precision.compareTo(diff, measurementNoise, 1e-6) < 0); } // error covariance should be already very low (< 0.02) Assert.assertTrue(Precision.compareTo(filter.getErrorCovariance()[0][0], 0.02d, 1e-6) < 0); } @Test public void testConstantAcceleration() { // simulates a vehicle, accelerating at a constant rate (0.1 m/s) // discrete time interval double dt = 0.1d; // position measurement noise (meter) double measurementNoise = 10d; // acceleration noise (meter/sec^2) double accelNoise = 0.2d; // A = [ 1 dt ] // [ 0 1 ] RealMatrix A = new Array2DRowRealMatrix(new double[][] { { 1, dt }, { 0, 1 } }); // B = [ dt^2/2 ] // [ dt ] RealMatrix B = new Array2DRowRealMatrix( new double[][] { { Math.pow(dt, 2d) / 2d }, { dt } }); // H = [ 1 0 ] RealMatrix H = new Array2DRowRealMatrix(new double[][] { { 1d, 0d } }); // x = [ 0 0 ] RealVector x = new ArrayRealVector(new double[] { 0, 0 }); RealMatrix tmp = new Array2DRowRealMatrix( new double[][] { { Math.pow(dt, 4d) / 4d, Math.pow(dt, 3d) / 2d }, { Math.pow(dt, 3d) / 2d, Math.pow(dt, 2d) } }); // Q = [ dt^4/4 dt^3/2 ] // [ dt^3/2 dt^2 ] RealMatrix Q = tmp.scalarMultiply(Math.pow(accelNoise, 2)); // P0 = [ 1 1 ] // [ 1 1 ] RealMatrix P0 = new Array2DRowRealMatrix(new double[][] { { 1, 1 }, { 1, 1 } }); // R = [ measurementNoise^2 ] RealMatrix R = new Array2DRowRealMatrix( new double[] { Math.pow(measurementNoise, 2) }); // constant control input, increase velocity by 0.1 m/s per cycle RealVector u = new ArrayRealVector(new double[] { 0.1d }); ProcessModel pm = new DefaultProcessModel(A, B, Q, x, P0); MeasurementModel mm = new DefaultMeasurementModel(H, R); KalmanFilter filter = new KalmanFilter(pm, mm); Assert.assertEquals(1, filter.getMeasurementDimension()); Assert.assertEquals(2, filter.getStateDimension()); assertMatrixEquals(P0.getData(), filter.getErrorCovariance()); // check the initial state double[] expectedInitialState = new double[] { 0.0, 0.0 }; assertVectorEquals(expectedInitialState, filter.getStateEstimation()); RandomGenerator rand = new JDKRandomGenerator(); RealVector tmpPNoise = new ArrayRealVector( new double[] { Math.pow(dt, 2d) / 2d, dt }); RealVector mNoise = new ArrayRealVector(1); // iterate 60 steps for (int i = 0; i < 60; i++) { filter.predict(u); // Simulate the process RealVector pNoise = tmpPNoise.mapMultiply(accelNoise * rand.nextGaussian()); // x = A * x + B * u + pNoise x = A.operate(x).add(B.operate(u)).add(pNoise); // Simulate the measurement mNoise.setEntry(0, measurementNoise * rand.nextGaussian()); // z = H * x + m_noise RealVector z = H.operate(x).add(mNoise); filter.correct(z); // state estimate shouldn't be larger than the measurement noise double diff = Math.abs(x.getEntry(0) - filter.getStateEstimation()[0]); Assert.assertTrue(Precision.compareTo(diff, measurementNoise, 1e-6) < 0); } // error covariance of the velocity should be already very low (< 0.1) Assert.assertTrue(Precision.compareTo(filter.getErrorCovariance()[1][1], 0.1d, 1e-6) < 0); } private void assertVectorEquals(double[] expected, double[] result) { Assert.assertEquals("Wrong number of rows.", expected.length, result.length); for (int i = 0; i < expected.length; i++) { Assert.assertEquals("Wrong value at position [" + i + "]", expected[i], result[i], 1.0e-6); } } private void assertMatrixEquals(double[][] expected, double[][] result) { Assert.assertEquals("Wrong number of rows.", expected.length, result.length); for (int i = 0; i < expected.length; i++) { Assert.assertEquals("Wrong number of columns.", expected[i].length, result[i].length); for (int j = 0; j < expected[i].length; j++) { Assert.assertEquals("Wrong value at position [" + i + "," + j + "]", expected[i][j], result[i][j], 1.0e-6); } } } } ././@LongLink100644 0 0 167 12126630646 10265 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/analysis/interpolation/BicubicSplineInterpolatorTest.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/analysis/interpolation/BicubicSplineInt100644 1750 1750 13444 12126627670 32431 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.analysis.interpolation; import org.apache.commons.math3.exception.DimensionMismatchException; import org.apache.commons.math3.exception.MathIllegalArgumentException; import org.apache.commons.math3.analysis.BivariateFunction; import org.junit.Assert; import org.junit.Test; /** * Test case for the bicubic interpolator. * * @version $Id$ */ public final class BicubicSplineInterpolatorTest { /** * Test preconditions. */ @Test public void testPreconditions() { double[] xval = new double[] {3, 4, 5, 6.5}; double[] yval = new double[] {-4, -3, -1, 2.5}; double[][] zval = new double[xval.length][yval.length]; BivariateGridInterpolator interpolator = new BicubicSplineInterpolator(); @SuppressWarnings("unused") BivariateFunction p = interpolator.interpolate(xval, yval, zval); double[] wxval = new double[] {3, 2, 5, 6.5}; try { p = interpolator.interpolate(wxval, yval, zval); Assert.fail("an exception should have been thrown"); } catch (MathIllegalArgumentException e) { // Expected } double[] wyval = new double[] {-4, -3, -1, -1}; try { p = interpolator.interpolate(xval, wyval, zval); Assert.fail("an exception should have been thrown"); } catch (MathIllegalArgumentException e) { // Expected } double[][] wzval = new double[xval.length][yval.length + 1]; try { p = interpolator.interpolate(xval, yval, wzval); Assert.fail("an exception should have been thrown"); } catch (DimensionMismatchException e) { // Expected } wzval = new double[xval.length - 1][yval.length]; try { p = interpolator.interpolate(xval, yval, wzval); Assert.fail("an exception should have been thrown"); } catch (DimensionMismatchException e) { // Expected } } /** * Test of interpolator for a plane. *

              * z = 2 x - 3 y + 5 */ @Test public void testPlane() { BivariateFunction f = new BivariateFunction() { public double value(double x, double y) { return 2 * x - 3 * y + 5; } }; BivariateGridInterpolator interpolator = new BicubicSplineInterpolator(); double[] xval = new double[] {3, 4, 5, 6.5}; double[] yval = new double[] {-4, -3, -1, 2, 2.5}; double[][] zval = new double[xval.length][yval.length]; for (int i = 0; i < xval.length; i++) { for (int j = 0; j < yval.length; j++) { zval[i][j] = f.value(xval[i], yval[j]); } } BivariateFunction p = interpolator.interpolate(xval, yval, zval); double x, y; double expected, result; x = 4; y = -3; expected = f.value(x, y); result = p.value(x, y); Assert.assertEquals("On sample point", expected, result, 1e-15); x = 4.5; y = -1.5; expected = f.value(x, y); result = p.value(x, y); Assert.assertEquals("half-way between sample points (middle of the patch)", expected, result, 0.3); x = 3.5; y = -3.5; expected = f.value(x, y); result = p.value(x, y); Assert.assertEquals("half-way between sample points (border of the patch)", expected, result, 0.3); } /** * Test of interpolator for a paraboloid. *

              * z = 2 x2 - 3 y2 + 4 x y - 5 */ @Test public void testParaboloid() { BivariateFunction f = new BivariateFunction() { public double value(double x, double y) { return 2 * x * x - 3 * y * y + 4 * x * y - 5; } }; BivariateGridInterpolator interpolator = new BicubicSplineInterpolator(); double[] xval = new double[] {3, 4, 5, 6.5}; double[] yval = new double[] {-4, -3, -2, -1, 0.5, 2.5}; double[][] zval = new double[xval.length][yval.length]; for (int i = 0; i < xval.length; i++) { for (int j = 0; j < yval.length; j++) { zval[i][j] = f.value(xval[i], yval[j]); } } BivariateFunction p = interpolator.interpolate(xval, yval, zval); double x, y; double expected, result; x = 5; y = 0.5; expected = f.value(x, y); result = p.value(x, y); Assert.assertEquals("On sample point", expected, result, 1e-13); x = 4.5; y = -1.5; expected = f.value(x, y); result = p.value(x, y); Assert.assertEquals("half-way between sample points (middle of the patch)", expected, result, 0.2); x = 3.5; y = -3.5; expected = f.value(x, y); result = p.value(x, y); Assert.assertEquals("half-way between sample points (border of the patch)", expected, result, 0.2); } } ././@LongLink100644 0 0 161 12126630646 10257 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/analysis/interpolation/HermiteInterpolatorTest.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/analysis/interpolation/HermiteInterpola100644 1750 1750 26123 12126627670 32514 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.analysis.interpolation; import java.util.Random; import org.apache.commons.math3.analysis.differentiation.DerivativeStructure; import org.apache.commons.math3.analysis.polynomials.PolynomialFunction; import org.apache.commons.math3.exception.NoDataException; import org.apache.commons.math3.util.FastMath; import org.junit.Assert; import org.junit.Test; public class HermiteInterpolatorTest { @Test public void testZero() { HermiteInterpolator interpolator = new HermiteInterpolator(); interpolator.addSamplePoint(0.0, new double[] { 0.0 }); for (double x = -10; x < 10; x += 1.0) { DerivativeStructure y = interpolator.value(new DerivativeStructure(1, 1, 0, x))[0]; Assert.assertEquals(0.0, y.getValue(), 1.0e-15); Assert.assertEquals(0.0, y.getPartialDerivative(1), 1.0e-15); } checkPolynomial(new PolynomialFunction(new double[] { 0.0 }), interpolator.getPolynomials()[0]); } @Test public void testQuadratic() { HermiteInterpolator interpolator = new HermiteInterpolator(); interpolator.addSamplePoint(0.0, new double[] { 2.0 }); interpolator.addSamplePoint(1.0, new double[] { 0.0 }); interpolator.addSamplePoint(2.0, new double[] { 0.0 }); for (double x = -10; x < 10; x += 1.0) { DerivativeStructure y = interpolator.value(new DerivativeStructure(1, 1, 0, x))[0]; Assert.assertEquals((x - 1.0) * (x - 2.0), y.getValue(), 1.0e-15); Assert.assertEquals(2 * x - 3.0, y.getPartialDerivative(1), 1.0e-15); } checkPolynomial(new PolynomialFunction(new double[] { 2.0, -3.0, 1.0 }), interpolator.getPolynomials()[0]); } @Test public void testMixedDerivatives() { HermiteInterpolator interpolator = new HermiteInterpolator(); interpolator.addSamplePoint(0.0, new double[] { 1.0 }, new double[] { 2.0 }); interpolator.addSamplePoint(1.0, new double[] { 4.0 }); interpolator.addSamplePoint(2.0, new double[] { 5.0 }, new double[] { 2.0 }); Assert.assertEquals(4, interpolator.getPolynomials()[0].degree()); DerivativeStructure y0 = interpolator.value(new DerivativeStructure(1, 1, 0, 0.0))[0]; Assert.assertEquals(1.0, y0.getValue(), 1.0e-15); Assert.assertEquals(2.0, y0.getPartialDerivative(1), 1.0e-15); Assert.assertEquals(4.0, interpolator.value(1.0)[0], 1.0e-15); DerivativeStructure y2 = interpolator.value(new DerivativeStructure(1, 1, 0, 2.0))[0]; Assert.assertEquals(5.0, y2.getValue(), 1.0e-15); Assert.assertEquals(2.0, y2.getPartialDerivative(1), 1.0e-15); checkPolynomial(new PolynomialFunction(new double[] { 1.0, 2.0, 4.0, -4.0, 1.0 }), interpolator.getPolynomials()[0]); } @Test public void testRandomPolynomialsValuesOnly() { Random random = new Random(0x42b1e7dbd361a932l); for (int i = 0; i < 100; ++i) { int maxDegree = 0; PolynomialFunction[] p = new PolynomialFunction[5]; for (int k = 0; k < p.length; ++k) { int degree = random.nextInt(7); p[k] = randomPolynomial(degree, random); maxDegree = FastMath.max(maxDegree, degree); } HermiteInterpolator interpolator = new HermiteInterpolator(); for (int j = 0; j < 1 + maxDegree; ++j) { double x = 0.1 * j; double[] values = new double[p.length]; for (int k = 0; k < p.length; ++k) { values[k] = p[k].value(x); } interpolator.addSamplePoint(x, values); } for (double x = 0; x < 2; x += 0.1) { double[] values = interpolator.value(x); Assert.assertEquals(p.length, values.length); for (int k = 0; k < p.length; ++k) { Assert.assertEquals(p[k].value(x), values[k], 1.0e-8 * FastMath.abs(p[k].value(x))); } } PolynomialFunction[] result = interpolator.getPolynomials(); for (int k = 0; k < p.length; ++k) { checkPolynomial(p[k], result[k]); } } } @Test public void testRandomPolynomialsFirstDerivative() { Random random = new Random(0x570803c982ca5d3bl); for (int i = 0; i < 100; ++i) { int maxDegree = 0; PolynomialFunction[] p = new PolynomialFunction[5]; PolynomialFunction[] pPrime = new PolynomialFunction[5]; for (int k = 0; k < p.length; ++k) { int degree = random.nextInt(7); p[k] = randomPolynomial(degree, random); pPrime[k] = p[k].polynomialDerivative(); maxDegree = FastMath.max(maxDegree, degree); } HermiteInterpolator interpolator = new HermiteInterpolator(); for (int j = 0; j < 1 + maxDegree / 2; ++j) { double x = 0.1 * j; double[] values = new double[p.length]; double[] derivatives = new double[p.length]; for (int k = 0; k < p.length; ++k) { values[k] = p[k].value(x); derivatives[k] = pPrime[k].value(x); } interpolator.addSamplePoint(x, values, derivatives); } for (double x = 0; x < 2; x += 0.1) { DerivativeStructure[] y = interpolator.value(new DerivativeStructure(1, 1, 0, x)); Assert.assertEquals(p.length, y.length); for (int k = 0; k < p.length; ++k) { Assert.assertEquals(p[k].value(x), y[k].getValue(), 1.0e-8 * FastMath.abs(p[k].value(x))); Assert.assertEquals(pPrime[k].value(x), y[k].getPartialDerivative(1), 4.0e-8 * FastMath.abs(p[k].value(x))); } } PolynomialFunction[] result = interpolator.getPolynomials(); for (int k = 0; k < p.length; ++k) { checkPolynomial(p[k], result[k]); } } } @Test public void testSine() { HermiteInterpolator interpolator = new HermiteInterpolator(); for (double x = 0; x < FastMath.PI; x += 0.5) { interpolator.addSamplePoint(x, new double[] { FastMath.sin(x) }); } for (double x = 0.1; x <= 2.9; x += 0.01) { DerivativeStructure y = interpolator.value(new DerivativeStructure(1, 2, 0, x))[0]; Assert.assertEquals( FastMath.sin(x), y.getValue(), 3.5e-5); Assert.assertEquals( FastMath.cos(x), y.getPartialDerivative(1), 1.3e-4); Assert.assertEquals(-FastMath.sin(x), y.getPartialDerivative(2), 2.9e-3); } } @Test public void testSquareRoot() { HermiteInterpolator interpolator = new HermiteInterpolator(); for (double x = 1.0; x < 3.6; x += 0.5) { interpolator.addSamplePoint(x, new double[] { FastMath.sqrt(x) }); } for (double x = 1.1; x < 3.5; x += 0.01) { DerivativeStructure y = interpolator.value(new DerivativeStructure(1, 1, 0, x))[0]; Assert.assertEquals(FastMath.sqrt(x), y.getValue(), 1.5e-4); Assert.assertEquals(0.5 / FastMath.sqrt(x), y.getPartialDerivative(1), 8.5e-4); } } @Test public void testWikipedia() { // this test corresponds to the example from Wikipedia page: // http://en.wikipedia.org/wiki/Hermite_interpolation HermiteInterpolator interpolator = new HermiteInterpolator(); interpolator.addSamplePoint(-1, new double[] { 2 }, new double[] { -8 }, new double[] { 56 }); interpolator.addSamplePoint( 0, new double[] { 1 }, new double[] { 0 }, new double[] { 0 }); interpolator.addSamplePoint( 1, new double[] { 2 }, new double[] { 8 }, new double[] { 56 }); for (double x = -1.0; x <= 1.0; x += 0.125) { DerivativeStructure y = interpolator.value(new DerivativeStructure(1, 1, 0, x))[0]; double x2 = x * x; double x4 = x2 * x2; double x8 = x4 * x4; Assert.assertEquals(x8 + 1, y.getValue(), 1.0e-15); Assert.assertEquals(8 * x4 * x2 * x, y.getPartialDerivative(1), 1.0e-15); } checkPolynomial(new PolynomialFunction(new double[] { 1, 0, 0, 0, 0, 0, 0, 0, 1 }), interpolator.getPolynomials()[0]); } @Test public void testOnePointParabola() { HermiteInterpolator interpolator = new HermiteInterpolator(); interpolator.addSamplePoint(0, new double[] { 1 }, new double[] { 1 }, new double[] { 2 }); for (double x = -1.0; x <= 1.0; x += 0.125) { DerivativeStructure y = interpolator.value(new DerivativeStructure(1, 1, 0, x))[0]; Assert.assertEquals(1 + x * (1 + x), y.getValue(), 1.0e-15); Assert.assertEquals(1 + 2 * x, y.getPartialDerivative(1), 1.0e-15); } checkPolynomial(new PolynomialFunction(new double[] { 1, 1, 1 }), interpolator.getPolynomials()[0]); } private PolynomialFunction randomPolynomial(int degree, Random random) { double[] coeff = new double[ 1 + degree]; for (int j = 0; j < degree; ++j) { coeff[j] = random.nextDouble(); } return new PolynomialFunction(coeff); } @Test(expected=NoDataException.class) public void testEmptySample() { new HermiteInterpolator().value(0.0); } @Test(expected=IllegalArgumentException.class) public void testDuplicatedAbscissa() { HermiteInterpolator interpolator = new HermiteInterpolator(); interpolator.addSamplePoint(1.0, new double[] { 0.0 }); interpolator.addSamplePoint(1.0, new double[] { 1.0 }); } private void checkPolynomial(PolynomialFunction expected, PolynomialFunction result) { Assert.assertTrue(result.degree() >= expected.degree()); double[] cE = expected.getCoefficients(); double[] cR = result.getCoefficients(); for (int i = 0; i < cE.length; ++i) { Assert.assertEquals(cE[i], cR[i], 1.0e-8 * FastMath.abs(cE[i])); } for (int i = cE.length; i < cR.length; ++i) { Assert.assertEquals(0.0, cR[i], 1.0e-9); } } } ././@LongLink100644 0 0 161 12126630646 10257 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/analysis/interpolation/NevilleInterpolatorTest.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/analysis/interpolation/NevilleInterpola100644 1750 1750 12533 12126627670 32515 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.analysis.interpolation; import org.apache.commons.math3.analysis.UnivariateFunction; import org.apache.commons.math3.analysis.function.Expm1; import org.apache.commons.math3.analysis.function.Sin; import org.apache.commons.math3.exception.NonMonotonicSequenceException; import org.apache.commons.math3.util.FastMath; import org.junit.Assert; import org.junit.Test; /** * Test case for Neville interpolator. *

              * The error of polynomial interpolation is * f(z) - p(z) = f^(n)(zeta) * (z-x[0])(z-x[1])...(z-x[n-1]) / n! * where f^(n) is the n-th derivative of the approximated function and * zeta is some point in the interval determined by x[] and z. *

              * Since zeta is unknown, f^(n)(zeta) cannot be calculated. But we can bound * it and use the absolute value upper bound for estimates. For reference, * see Introduction to Numerical Analysis, ISBN 038795452X, chapter 2. * * @version $Id: NevilleInterpolatorTest.java 1374632 2012-08-18 18:11:11Z luc $ */ public final class NevilleInterpolatorTest { /** * Test of interpolator for the sine function. *

              * |sin^(n)(zeta)| <= 1.0, zeta in [0, 2*PI] */ @Test public void testSinFunction() { UnivariateFunction f = new Sin(); UnivariateInterpolator interpolator = new NevilleInterpolator(); double x[], y[], z, expected, result, tolerance; // 6 interpolating points on interval [0, 2*PI] int n = 6; double min = 0.0, max = 2 * FastMath.PI; x = new double[n]; y = new double[n]; for (int i = 0; i < n; i++) { x[i] = min + i * (max - min) / n; y[i] = f.value(x[i]); } double derivativebound = 1.0; UnivariateFunction p = interpolator.interpolate(x, y); z = FastMath.PI / 4; expected = f.value(z); result = p.value(z); tolerance = FastMath.abs(derivativebound * partialerror(x, z)); Assert.assertEquals(expected, result, tolerance); z = FastMath.PI * 1.5; expected = f.value(z); result = p.value(z); tolerance = FastMath.abs(derivativebound * partialerror(x, z)); Assert.assertEquals(expected, result, tolerance); } /** * Test of interpolator for the exponential function. *

              * |expm1^(n)(zeta)| <= e, zeta in [-1, 1] */ @Test public void testExpm1Function() { UnivariateFunction f = new Expm1(); UnivariateInterpolator interpolator = new NevilleInterpolator(); double x[], y[], z, expected, result, tolerance; // 5 interpolating points on interval [-1, 1] int n = 5; double min = -1.0, max = 1.0; x = new double[n]; y = new double[n]; for (int i = 0; i < n; i++) { x[i] = min + i * (max - min) / n; y[i] = f.value(x[i]); } double derivativebound = FastMath.E; UnivariateFunction p = interpolator.interpolate(x, y); z = 0.0; expected = f.value(z); result = p.value(z); tolerance = FastMath.abs(derivativebound * partialerror(x, z)); Assert.assertEquals(expected, result, tolerance); z = 0.5; expected = f.value(z); result = p.value(z); tolerance = FastMath.abs(derivativebound * partialerror(x, z)); Assert.assertEquals(expected, result, tolerance); z = -0.5; expected = f.value(z); result = p.value(z); tolerance = FastMath.abs(derivativebound * partialerror(x, z)); Assert.assertEquals(expected, result, tolerance); } /** * Test of parameters for the interpolator. */ @Test public void testParameters() { UnivariateInterpolator interpolator = new NevilleInterpolator(); try { // bad abscissas array double x[] = { 1.0, 2.0, 2.0, 4.0 }; double y[] = { 0.0, 4.0, 4.0, 2.5 }; UnivariateFunction p = interpolator.interpolate(x, y); p.value(0.0); Assert.fail("Expecting NonMonotonicSequenceException - bad abscissas array"); } catch (NonMonotonicSequenceException ex) { // expected } } /** * Returns the partial error term (z-x[0])(z-x[1])...(z-x[n-1])/n! */ protected double partialerror(double x[], double z) throws IllegalArgumentException { if (x.length < 1) { throw new IllegalArgumentException ("Interpolation array cannot be empty."); } double out = 1; for (int i = 0; i < x.length; i++) { out *= (z - x[i]) / (i + 1); } return out; } } ././@LongLink100644 0 0 200 12126630646 10251 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/analysis/interpolation/BicubicSplineInterpolatingFunctionTest.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/analysis/interpolation/BicubicSplineInt100644 1750 1750 41355 12126627670 32433 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.analysis.interpolation; import org.apache.commons.math3.exception.DimensionMismatchException; import org.apache.commons.math3.exception.MathIllegalArgumentException; import org.apache.commons.math3.analysis.BivariateFunction; import org.junit.Assert; import org.junit.Test; /** * Test case for the bicubic function. * * @version $Id$ */ public final class BicubicSplineInterpolatingFunctionTest { /** * Test preconditions. */ @Test public void testPreconditions() { double[] xval = new double[] {3, 4, 5, 6.5}; double[] yval = new double[] {-4, -3, -1, 2.5}; double[][] zval = new double[xval.length][yval.length]; @SuppressWarnings("unused") BivariateFunction bcf = new BicubicSplineInterpolatingFunction(xval, yval, zval, zval, zval, zval); double[] wxval = new double[] {3, 2, 5, 6.5}; try { bcf = new BicubicSplineInterpolatingFunction(wxval, yval, zval, zval, zval, zval); Assert.fail("an exception should have been thrown"); } catch (MathIllegalArgumentException e) { // Expected } double[] wyval = new double[] {-4, -1, -1, 2.5}; try { bcf = new BicubicSplineInterpolatingFunction(xval, wyval, zval, zval, zval, zval); Assert.fail("an exception should have been thrown"); } catch (MathIllegalArgumentException e) { // Expected } double[][] wzval = new double[xval.length][yval.length - 1]; try { bcf = new BicubicSplineInterpolatingFunction(xval, yval, wzval, zval, zval, zval); Assert.fail("an exception should have been thrown"); } catch (DimensionMismatchException e) { // Expected } try { bcf = new BicubicSplineInterpolatingFunction(xval, yval, zval, wzval, zval, zval); Assert.fail("an exception should have been thrown"); } catch (DimensionMismatchException e) { // Expected } try { bcf = new BicubicSplineInterpolatingFunction(xval, yval, zval, zval, wzval, zval); Assert.fail("an exception should have been thrown"); } catch (DimensionMismatchException e) { // Expected } try { bcf = new BicubicSplineInterpolatingFunction(xval, yval, zval, zval, zval, wzval); Assert.fail("an exception should have been thrown"); } catch (DimensionMismatchException e) { // Expected } wzval = new double[xval.length - 1][yval.length]; try { bcf = new BicubicSplineInterpolatingFunction(xval, yval, wzval, zval, zval, zval); Assert.fail("an exception should have been thrown"); } catch (DimensionMismatchException e) { // Expected } try { bcf = new BicubicSplineInterpolatingFunction(xval, yval, zval, wzval, zval, zval); Assert.fail("an exception should have been thrown"); } catch (DimensionMismatchException e) { // Expected } try { bcf = new BicubicSplineInterpolatingFunction(xval, yval, zval, zval, wzval, zval); Assert.fail("an exception should have been thrown"); } catch (DimensionMismatchException e) { // Expected } try { bcf = new BicubicSplineInterpolatingFunction(xval, yval, zval, zval, zval, wzval); Assert.fail("an exception should have been thrown"); } catch (DimensionMismatchException e) { // Expected } } /** * Test for a plane. *

              * z = 2 x - 3 y + 5 */ @Test public void testPlane() { double[] xval = new double[] {3, 4, 5, 6.5}; double[] yval = new double[] {-4, -3, -1, 2, 2.5}; // Function values BivariateFunction f = new BivariateFunction() { public double value(double x, double y) { return 2 * x - 3 * y + 5; } }; double[][] zval = new double[xval.length][yval.length]; for (int i = 0; i < xval.length; i++) { for (int j = 0; j < yval.length; j++) { zval[i][j] = f.value(xval[i], yval[j]); } } // Partial derivatives with respect to x double[][] dZdX = new double[xval.length][yval.length]; for (int i = 0; i < xval.length; i++) { for (int j = 0; j < yval.length; j++) { dZdX[i][j] = 2; } } // Partial derivatives with respect to y double[][] dZdY = new double[xval.length][yval.length]; for (int i = 0; i < xval.length; i++) { for (int j = 0; j < yval.length; j++) { dZdY[i][j] = -3; } } // Partial cross-derivatives double[][] dZdXdY = new double[xval.length][yval.length]; for (int i = 0; i < xval.length; i++) { for (int j = 0; j < yval.length; j++) { dZdXdY[i][j] = 0; } } BivariateFunction bcf = new BicubicSplineInterpolatingFunction(xval, yval, zval, dZdX, dZdY, dZdXdY); double x, y; double expected, result; x = 4; y = -3; expected = f.value(x, y); result = bcf.value(x, y); Assert.assertEquals("On sample point", expected, result, 1e-15); x = 4.5; y = -1.5; expected = f.value(x, y); result = bcf.value(x, y); Assert.assertEquals("Half-way between sample points (middle of the patch)", expected, result, 0.3); x = 3.5; y = -3.5; expected = f.value(x, y); result = bcf.value(x, y); Assert.assertEquals("Half-way between sample points (border of the patch)", expected, result, 0.3); } /** * Test for a paraboloid. *

              * z = 2 x2 - 3 y2 + 4 x y - 5 */ @Test public void testParaboloid() { double[] xval = new double[] {3, 4, 5, 6.5}; double[] yval = new double[] {-4, -3, -1, 2, 2.5}; // Function values BivariateFunction f = new BivariateFunction() { public double value(double x, double y) { return 2 * x * x - 3 * y * y + 4 * x * y - 5; } }; double[][] zval = new double[xval.length][yval.length]; for (int i = 0; i < xval.length; i++) { for (int j = 0; j < yval.length; j++) { zval[i][j] = f.value(xval[i], yval[j]); } } // Partial derivatives with respect to x double[][] dZdX = new double[xval.length][yval.length]; BivariateFunction dfdX = new BivariateFunction() { public double value(double x, double y) { return 4 * (x + y); } }; for (int i = 0; i < xval.length; i++) { for (int j = 0; j < yval.length; j++) { dZdX[i][j] = dfdX.value(xval[i], yval[j]); } } // Partial derivatives with respect to y double[][] dZdY = new double[xval.length][yval.length]; BivariateFunction dfdY = new BivariateFunction() { public double value(double x, double y) { return 4 * x - 6 * y; } }; for (int i = 0; i < xval.length; i++) { for (int j = 0; j < yval.length; j++) { dZdY[i][j] = dfdY.value(xval[i], yval[j]); } } // Partial cross-derivatives double[][] dZdXdY = new double[xval.length][yval.length]; for (int i = 0; i < xval.length; i++) { for (int j = 0; j < yval.length; j++) { dZdXdY[i][j] = 4; } } BivariateFunction bcf = new BicubicSplineInterpolatingFunction(xval, yval, zval, dZdX, dZdY, dZdXdY); double x, y; double expected, result; x = 4; y = -3; expected = f.value(x, y); result = bcf.value(x, y); Assert.assertEquals("On sample point", expected, result, 1e-15); x = 4.5; y = -1.5; expected = f.value(x, y); result = bcf.value(x, y); Assert.assertEquals("Half-way between sample points (middle of the patch)", expected, result, 2); x = 3.5; y = -3.5; expected = f.value(x, y); result = bcf.value(x, y); Assert.assertEquals("Half-way between sample points (border of the patch)", expected, result, 2); } /** * Test for partial derivatives of {@link BicubicSplineFunction}. *

              * f(x, y) = ΣiΣj (i+1) (j+2) xi yj */ @Test public void testSplinePartialDerivatives() { final int N = 4; final double[] coeff = new double[16]; for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { coeff[i + N * j] = (i + 1) * (j + 2); } } final BicubicSplineFunction f = new BicubicSplineFunction(coeff); BivariateFunction derivative; final double x = 0.435; final double y = 0.776; final double tol = 1e-13; derivative = new BivariateFunction() { public double value(double x, double y) { final double x2 = x * x; final double y2 = y * y; final double y3 = y2 * y; final double yFactor = 2 + 3 * y + 4 * y2 + 5 * y3; return yFactor * (2 + 6 * x + 12 * x2); } }; Assert.assertEquals("dFdX", derivative.value(x, y), f.partialDerivativeX().value(x, y), tol); derivative = new BivariateFunction() { public double value(double x, double y) { final double x2 = x * x; final double x3 = x2 * x; final double y2 = y * y; final double xFactor = 1 + 2 * x + 3 * x2 + 4 * x3; return xFactor * (3 + 8 * y + 15 * y2); } }; Assert.assertEquals("dFdY", derivative.value(x, y), f.partialDerivativeY().value(x, y), tol); derivative = new BivariateFunction() { public double value(double x, double y) { final double y2 = y * y; final double y3 = y2 * y; final double yFactor = 2 + 3 * y + 4 * y2 + 5 * y3; return yFactor * (6 + 24 * x); } }; Assert.assertEquals("d2FdX2", derivative.value(x, y), f.partialDerivativeXX().value(x, y), tol); derivative = new BivariateFunction() { public double value(double x, double y) { final double x2 = x * x; final double x3 = x2 * x; final double xFactor = 1 + 2 * x + 3 * x2 + 4 * x3; return xFactor * (8 + 30 * y); } }; Assert.assertEquals("d2FdY2", derivative.value(x, y), f.partialDerivativeYY().value(x, y), tol); derivative = new BivariateFunction() { public double value(double x, double y) { final double x2 = x * x; final double y2 = y * y; final double yFactor = 3 + 8 * y + 15 * y2; return yFactor * (2 + 6 * x + 12 * x2); } }; Assert.assertEquals("d2FdXdY", derivative.value(x, y), f.partialDerivativeXY().value(x, y), tol); } /** * Test that the partial derivatives computed from a * {@link BicubicSplineInterpolatingFunction} match the input data. *

              * f(x, y) = 5 * - 3 x + 2 y * - x y + 2 x2 - 3 y2 * + 4 x2 y - x y2 - 3 x3 + y3 */ @Test public void testMatchingPartialDerivatives() { final int sz = 21; double[] val = new double[sz]; // Coordinate values final double delta = 1d / (sz - 1); for (int i = 0; i < sz; i++) { val[i] = i * delta; } // Function values BivariateFunction f = new BivariateFunction() { public double value(double x, double y) { final double x2 = x * x; final double x3 = x2 * x; final double y2 = y * y; final double y3 = y2 * y; return 5 - 3 * x + 2 * y - x * y + 2 * x2 - 3 * y2 + 4 * x2 * y - x * y2 - 3 * x3 + y3; } }; double[][] fval = new double[sz][sz]; for (int i = 0; i < sz; i++) { for (int j = 0; j < sz; j++) { fval[i][j] = f.value(val[i], val[j]); } } // Partial derivatives with respect to x double[][] dFdX = new double[sz][sz]; BivariateFunction dfdX = new BivariateFunction() { public double value(double x, double y) { final double x2 = x * x; final double y2 = y * y; return - 3 - y + 4 * x + 8 * x * y - y2 - 9 * x2; } }; for (int i = 0; i < sz; i++) { for (int j = 0; j < sz; j++) { dFdX[i][j] = dfdX.value(val[i], val[j]); } } // Partial derivatives with respect to y double[][] dFdY = new double[sz][sz]; BivariateFunction dfdY = new BivariateFunction() { public double value(double x, double y) { final double x2 = x * x; final double y2 = y * y; return 2 - x - 6 * y + 4 * x2 - 2 * x * y + 3 * y2; } }; for (int i = 0; i < sz; i++) { for (int j = 0; j < sz; j++) { dFdY[i][j] = dfdY.value(val[i], val[j]); } } // Partial cross-derivatives double[][] d2FdXdY = new double[sz][sz]; BivariateFunction d2fdXdY = new BivariateFunction() { public double value(double x, double y) { return -1 + 8 * x - 2 * y; } }; for (int i = 0; i < sz; i++) { for (int j = 0; j < sz; j++) { d2FdXdY[i][j] = d2fdXdY.value(val[i], val[j]); } } BicubicSplineInterpolatingFunction bcf = new BicubicSplineInterpolatingFunction(val, val, fval, dFdX, dFdY, d2FdXdY); double x, y; double expected, result; final double tol = 1e-12; for (int i = 0; i < sz; i++) { x = val[i]; for (int j = 0; j < sz; j++) { y = val[j]; expected = dfdX.value(x, y); result = bcf.partialDerivativeX(x, y); Assert.assertEquals(x + " " + y + " dFdX", expected, result, tol); expected = dfdY.value(x, y); result = bcf.partialDerivativeY(x, y); Assert.assertEquals(x + " " + y + " dFdY", expected, result, tol); expected = d2fdXdY.value(x, y); result = bcf.partialDerivativeXY(x, y); Assert.assertEquals(x + " " + y + " d2FdXdY", expected, result, tol); } } } } ././@LongLink100644 0 0 165 12126630646 10263 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/analysis/interpolation/MicrosphereInterpolatorTest.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/analysis/interpolation/MicrosphereInter100644 1750 1750 10560 12126627670 32521 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.analysis.interpolation; import org.apache.commons.math3.analysis.MultivariateFunction; import org.apache.commons.math3.util.FastMath; import org.junit.Assert; import org.junit.Test; /** * Test case for the "microsphere projection" interpolator. * * @version $Id: MicrosphereInterpolatorTest.java 1244107 2012-02-14 16:17:55Z erans $ */ public final class MicrosphereInterpolatorTest { /** * Test of interpolator for a plane. *

              * y = 2 x1 - 3 x2 + 5 */ @Test public void testLinearFunction2D() { MultivariateFunction f = new MultivariateFunction() { public double value(double[] x) { if (x.length != 2) { throw new IllegalArgumentException(); } return 2 * x[0] - 3 * x[1] + 5; } }; MultivariateInterpolator interpolator = new MicrosphereInterpolator(); // Interpolating points in [-1, 1][-1, 1] by steps of 1. final int n = 9; final int dim = 2; double[][] x = new double[n][dim]; double[] y = new double[n]; int index = 0; for (int i = -1; i <= 1; i++) { for (int j = -1; j <= 1; j++) { x[index][0] = i; x[index][1] = j; y[index] = f.value(x[index]); ++index; } } MultivariateFunction p = interpolator.interpolate(x, y); double[] c = new double[dim]; double expected, result; c[0] = 0; c[1] = 0; expected = f.value(c); result = p.value(c); Assert.assertEquals("On sample point", expected, result, FastMath.ulp(1d)); c[0] = 0 + 1e-5; c[1] = 1 - 1e-5; expected = f.value(c); result = p.value(c); Assert.assertEquals("1e-5 away from sample point", expected, result, 1e-4); } /** * Test of interpolator for a quadratic function. *

              * y = 2 x12 - 3 x22 * + 4 x1 x2 - 5 */ @Test public void testParaboloid2D() { MultivariateFunction f = new MultivariateFunction() { public double value(double[] x) { if (x.length != 2) { throw new IllegalArgumentException(); } return 2 * x[0] * x[0] - 3 * x[1] * x[1] + 4 * x[0] * x[1] - 5; } }; MultivariateInterpolator interpolator = new MicrosphereInterpolator(); // Interpolating points in [-10, 10][-10, 10] by steps of 2. final int n = 121; final int dim = 2; double[][] x = new double[n][dim]; double[] y = new double[n]; int index = 0; for (int i = -10; i <= 10; i += 2) { for (int j = -10; j <= 10; j += 2) { x[index][0] = i; x[index][1] = j; y[index] = f.value(x[index]); ++index; } } MultivariateFunction p = interpolator.interpolate(x, y); double[] c = new double[dim]; double expected, result; c[0] = 0; c[1] = 0; expected = f.value(c); result = p.value(c); Assert.assertEquals("On sample point", expected, result, FastMath.ulp(1d)); c[0] = 2 + 1e-5; c[1] = 2 - 1e-5; expected = f.value(c); result = p.value(c); Assert.assertEquals("1e-5 away from sample point", expected, result, 1e-3); } } ././@LongLink100644 0 0 170 12126630646 10257 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/analysis/interpolation/TricubicSplineInterpolatorTest.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/analysis/interpolation/TricubicSplineIn100644 1750 1750 16673 12126627670 32460 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.analysis.interpolation; import org.apache.commons.math3.exception.DimensionMismatchException; import org.apache.commons.math3.exception.MathIllegalArgumentException; import org.apache.commons.math3.util.FastMath; import org.apache.commons.math3.analysis.TrivariateFunction; import org.junit.Assert; import org.junit.Test; /** * Test case for the tricubic interpolator. * * @version $Id$ */ public final class TricubicSplineInterpolatorTest { /** * Test preconditions. */ @Test public void testPreconditions() { double[] xval = new double[] {3, 4, 5, 6.5}; double[] yval = new double[] {-4, -3, -1, 2.5}; double[] zval = new double[] {-12, -8, -5.5, -3, 0, 2.5}; double[][][] fval = new double[xval.length][yval.length][zval.length]; TrivariateGridInterpolator interpolator = new TricubicSplineInterpolator(); @SuppressWarnings("unused") TrivariateFunction p = interpolator.interpolate(xval, yval, zval, fval); double[] wxval = new double[] {3, 2, 5, 6.5}; try { p = interpolator.interpolate(wxval, yval, zval, fval); Assert.fail("an exception should have been thrown"); } catch (MathIllegalArgumentException e) { // Expected } double[] wyval = new double[] {-4, -3, -1, -1}; try { p = interpolator.interpolate(xval, wyval, zval, fval); Assert.fail("an exception should have been thrown"); } catch (MathIllegalArgumentException e) { // Expected } double[] wzval = new double[] {-12, -8, -5.5, -3, -4, 2.5}; try { p = interpolator.interpolate(xval, yval, wzval, fval); Assert.fail("an exception should have been thrown"); } catch (MathIllegalArgumentException e) { // Expected } double[][][] wfval = new double[xval.length][yval.length + 1][zval.length]; try { p = interpolator.interpolate(xval, yval, zval, wfval); Assert.fail("an exception should have been thrown"); } catch (DimensionMismatchException e) { // Expected } wfval = new double[xval.length - 1][yval.length][zval.length]; try { p = interpolator.interpolate(xval, yval, zval, wfval); Assert.fail("an exception should have been thrown"); } catch (DimensionMismatchException e) { // Expected } wfval = new double[xval.length][yval.length][zval.length - 1]; try { p = interpolator.interpolate(xval, yval, zval, wfval); Assert.fail("an exception should have been thrown"); } catch (DimensionMismatchException e) { // Expected } } /** * Test of interpolator for a plane. *

              * f(x, y, z) = 2 x - 3 y - z + 5 */ @Test public void testPlane() { TrivariateFunction f = new TrivariateFunction() { public double value(double x, double y, double z) { return 2 * x - 3 * y - z + 5; } }; TrivariateGridInterpolator interpolator = new TricubicSplineInterpolator(); double[] xval = new double[] {3, 4, 5, 6.5}; double[] yval = new double[] {-4, -3, -1, 2, 2.5}; double[] zval = new double[] {-12, -8, -5.5, -3, 0, 2.5}; double[][][] fval = new double[xval.length][yval.length][zval.length]; for (int i = 0; i < xval.length; i++) { for (int j = 0; j < yval.length; j++) { for (int k = 0; k < zval.length; k++) { fval[i][j][k] = f.value(xval[i], yval[j], zval[k]); } } } TrivariateFunction p = interpolator.interpolate(xval, yval, zval, fval); double x, y, z; double expected, result; x = 4; y = -3; z = 0; expected = f.value(x, y, z); result = p.value(x, y, z); Assert.assertEquals("On sample point", expected, result, 1e-15); x = 4.5; y = -1.5; z = -4.25; expected = f.value(x, y, z); result = p.value(x, y, z); Assert.assertEquals("half-way between sample points (middle of the patch)", expected, result, 0.3); x = 3.5; y = -3.5; z = -10; expected = f.value(x, y, z); result = p.value(x, y, z); Assert.assertEquals("half-way between sample points (border of the patch)", expected, result, 0.3); } /** * Test of interpolator for a sine wave. *

              *

              * f(x, y, z) = a cos [ω z - ky x - ky y] *

              * with A = 0.2, ω = 0.5, kx = 2, ky = 1. */ @Test public void testWave() { double[] xval = new double[] {3, 4, 5, 6.5}; double[] yval = new double[] {-4, -3, -1, 2, 2.5}; double[] zval = new double[] {-12, -8, -5.5, -3, 0, 4}; final double a = 0.2; final double omega = 0.5; final double kx = 2; final double ky = 1; // Function values TrivariateFunction f = new TrivariateFunction() { public double value(double x, double y, double z) { return a * FastMath.cos(omega * z - kx * x - ky * y); } }; double[][][] fval = new double[xval.length][yval.length][zval.length]; for (int i = 0; i < xval.length; i++) { for (int j = 0; j < yval.length; j++) { for (int k = 0; k < zval.length; k++) { fval[i][j][k] = f.value(xval[i], yval[j], zval[k]); } } } TrivariateGridInterpolator interpolator = new TricubicSplineInterpolator(); TrivariateFunction p = interpolator.interpolate(xval, yval, zval, fval); double x, y, z; double expected, result; x = 4; y = -3; z = 0; expected = f.value(x, y, z); result = p.value(x, y, z); Assert.assertEquals("On sample point", expected, result, 1e-12); x = 4.5; y = -1.5; z = -4.25; expected = f.value(x, y, z); result = p.value(x, y, z); Assert.assertEquals("Half-way between sample points (middle of the patch)", expected, result, 0.1); x = 3.5; y = -3.5; z = -10; expected = f.value(x, y, z); result = p.value(x, y, z); Assert.assertEquals("Half-way between sample points (border of the patch)", expected, result, 0.1); } } ././@LongLink100644 0 0 160 12126630646 10256 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/analysis/interpolation/LinearInterpolatorTest.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/analysis/interpolation/LinearInterpolat100644 1750 1750 13662 12126627670 32521 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.analysis.interpolation; import org.apache.commons.math3.exception.NonMonotonicSequenceException; import org.apache.commons.math3.exception.DimensionMismatchException; import org.apache.commons.math3.exception.NumberIsTooSmallException; import org.apache.commons.math3.TestUtils; import org.apache.commons.math3.analysis.UnivariateFunction; import org.apache.commons.math3.analysis.polynomials.PolynomialFunction; import org.apache.commons.math3.analysis.polynomials.PolynomialSplineFunction; import org.junit.Assert; import org.junit.Test; /** * Test the LinearInterpolator. */ public class LinearInterpolatorTest { /** error tolerance for spline interpolator value at knot points */ protected double knotTolerance = 1E-12; /** error tolerance for interpolating polynomial coefficients */ protected double coefficientTolerance = 1E-6; /** error tolerance for interpolated values */ protected double interpolationTolerance = 1E-12; @Test public void testInterpolateLinearDegenerateTwoSegment() { double x[] = { 0.0, 0.5, 1.0 }; double y[] = { 0.0, 0.5, 1.0 }; UnivariateInterpolator i = new LinearInterpolator(); UnivariateFunction f = i.interpolate(x, y); verifyInterpolation(f, x, y); // Verify coefficients using analytical values PolynomialFunction polynomials[] = ((PolynomialSplineFunction) f).getPolynomials(); double target[] = {y[0], 1d}; TestUtils.assertEquals(polynomials[0].getCoefficients(), target, coefficientTolerance); target = new double[]{y[1], 1d}; TestUtils.assertEquals(polynomials[1].getCoefficients(), target, coefficientTolerance); // Check interpolation Assert.assertEquals(0.0,f.value(0.0), interpolationTolerance); Assert.assertEquals(0.4,f.value(0.4), interpolationTolerance); Assert.assertEquals(1.0,f.value(1.0), interpolationTolerance); } @Test public void testInterpolateLinearDegenerateThreeSegment() { double x[] = { 0.0, 0.5, 1.0, 1.5 }; double y[] = { 0.0, 0.5, 1.0, 1.5 }; UnivariateInterpolator i = new LinearInterpolator(); UnivariateFunction f = i.interpolate(x, y); verifyInterpolation(f, x, y); // Verify coefficients using analytical values PolynomialFunction polynomials[] = ((PolynomialSplineFunction) f).getPolynomials(); double target[] = {y[0], 1d}; TestUtils.assertEquals(polynomials[0].getCoefficients(), target, coefficientTolerance); target = new double[]{y[1], 1d}; TestUtils.assertEquals(polynomials[1].getCoefficients(), target, coefficientTolerance); target = new double[]{y[2], 1d}; TestUtils.assertEquals(polynomials[2].getCoefficients(), target, coefficientTolerance); // Check interpolation Assert.assertEquals(0,f.value(0), interpolationTolerance); Assert.assertEquals(1.4,f.value(1.4), interpolationTolerance); Assert.assertEquals(1.5,f.value(1.5), interpolationTolerance); } @Test public void testInterpolateLinear() { double x[] = { 0.0, 0.5, 1.0 }; double y[] = { 0.0, 0.5, 0.0 }; UnivariateInterpolator i = new LinearInterpolator(); UnivariateFunction f = i.interpolate(x, y); verifyInterpolation(f, x, y); // Verify coefficients using analytical values PolynomialFunction polynomials[] = ((PolynomialSplineFunction) f).getPolynomials(); double target[] = {y[0], 1d}; TestUtils.assertEquals(polynomials[0].getCoefficients(), target, coefficientTolerance); target = new double[]{y[1], -1d}; TestUtils.assertEquals(polynomials[1].getCoefficients(), target, coefficientTolerance); } @Test public void testIllegalArguments() { // Data set arrays of different size. UnivariateInterpolator i = new LinearInterpolator(); try { double xval[] = { 0.0, 1.0 }; double yval[] = { 0.0, 1.0, 2.0 }; i.interpolate(xval, yval); Assert.fail("Failed to detect data set array with different sizes."); } catch (DimensionMismatchException iae) { // Expected. } // X values not sorted. try { double xval[] = { 0.0, 1.0, 0.5 }; double yval[] = { 0.0, 1.0, 2.0 }; i.interpolate(xval, yval); Assert.fail("Failed to detect unsorted arguments."); } catch (NonMonotonicSequenceException iae) { // Expected. } // Not enough data to interpolate. try { double xval[] = { 0.0 }; double yval[] = { 0.0 }; i.interpolate(xval, yval); Assert.fail("Failed to detect unsorted arguments."); } catch (NumberIsTooSmallException iae) { // Expected. } } /** * verifies that f(x[i]) = y[i] for i = 0..n-1 where n is common length. */ protected void verifyInterpolation(UnivariateFunction f, double x[], double y[]) { for (int i = 0; i < x.length; i++) { Assert.assertEquals(f.value(x[i]), y[i], knotTolerance); } } } ././@LongLink100644 0 0 174 12126630646 10263 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/analysis/interpolation/UnivariatePeriodicInterpolatorTest.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/analysis/interpolation/UnivariatePeriod100644 1750 1750 13665 12126627670 32522 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.analysis.interpolation; import java.util.Random; import org.apache.commons.math3.analysis.UnivariateFunction; import org.apache.commons.math3.util.FastMath; import org.apache.commons.math3.exception.NumberIsTooSmallException; import org.apache.commons.math3.exception.NonMonotonicSequenceException; import org.junit.Assert; import org.junit.Test; /** * Test for {@link UnivariatePeriodicInterpolator}. */ public class UnivariatePeriodicInterpolatorTest { private final Random rng = new Random(1224465L); @Test public void testSine() { final int n = 30; final double[] xval = new double[n]; final double[] yval = new double[n]; final double period = 12.3; final double offset = 45.67; double delta = 0; for (int i = 0; i < n; i++) { delta += rng.nextDouble() * period / n; xval[i] = offset + delta; yval[i] = FastMath.sin(xval[i]); } final UnivariateInterpolator inter = new LinearInterpolator(); final UnivariateFunction f = inter.interpolate(xval, yval); final UnivariateInterpolator interP = new UnivariatePeriodicInterpolator(new LinearInterpolator(), period, 1); final UnivariateFunction fP = interP.interpolate(xval, yval); // Comparing with original interpolation algorithm. final double xMin = xval[0]; final double xMax = xval[n - 1]; for (int i = 0; i < n; i++) { final double x = xMin + (xMax - xMin) * rng.nextDouble(); final double y = f.value(x); final double yP = fP.value(x); Assert.assertEquals("x=" + x, y, yP, Math.ulp(1d)); } // Test interpolation outside the primary interval. for (int i = 0; i < n; i++) { final double xIn = offset + rng.nextDouble() * period; final double xOut = xIn + rng.nextInt(123456789) * period; final double yIn = fP.value(xIn); final double yOut = fP.value(xOut); Assert.assertEquals(yIn, yOut, 1e-7); } } @Test public void testLessThanOnePeriodCoverage() { final int n = 30; final double[] xval = new double[n]; final double[] yval = new double[n]; final double period = 12.3; final double offset = 45.67; double delta = period / 2; for (int i = 0; i < n; i++) { delta += period / (2 * n) * rng.nextDouble(); xval[i] = offset + delta; yval[i] = FastMath.sin(xval[i]); } final UnivariateInterpolator interP = new UnivariatePeriodicInterpolator(new LinearInterpolator(), period, 1); final UnivariateFunction fP = interP.interpolate(xval, yval); // Test interpolation outside the sample data interval. for (int i = 0; i < n; i++) { final double xIn = offset + rng.nextDouble() * period; final double xOut = xIn + rng.nextInt(123456789) * period; final double yIn = fP.value(xIn); final double yOut = fP.value(xOut); Assert.assertEquals(yIn, yOut, 1e-7); } } @Test public void testMoreThanOnePeriodCoverage() { final int n = 30; final double[] xval = new double[n]; final double[] yval = new double[n]; final double period = 12.3; final double offset = 45.67; double delta = period / 2; for (int i = 0; i < n; i++) { delta += 10 * period / n * rng.nextDouble(); xval[i] = offset + delta; yval[i] = FastMath.sin(xval[i]); } final UnivariateInterpolator interP = new UnivariatePeriodicInterpolator(new LinearInterpolator(), period, 1); final UnivariateFunction fP = interP.interpolate(xval, yval); // Test interpolation outside the sample data interval. for (int i = 0; i < n; i++) { final double xIn = offset + rng.nextDouble() * period; final double xOut = xIn + rng.nextInt(123456789) * period; final double yIn = fP.value(xIn); final double yOut = fP.value(xOut); Assert.assertEquals(yIn, yOut, 1e-6); } } @Test(expected=NumberIsTooSmallException.class) public void testTooFewSamples() { final double[] xval = { 2, 3, 7 }; final double[] yval = { 1, 6, 5 }; final double period = 10; final UnivariateInterpolator interpolator = new UnivariatePeriodicInterpolator(new LinearInterpolator(), period); interpolator.interpolate(xval, yval); } @Test(expected=NonMonotonicSequenceException.class) public void testUnsortedSamples() { final double[] xval = { 2, 3, 7, 4, 6 }; final double[] yval = { 1, 6, 5, -1, -2 }; final double period = 10; final UnivariateInterpolator interpolator = new UnivariatePeriodicInterpolator(new LinearInterpolator(), period); interpolator.interpolate(xval, yval); } } ././@LongLink100644 0 0 160 12126630646 10256 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/analysis/interpolation/SplineInterpolatorTest.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/analysis/interpolation/SplineInterpolat100644 1750 1750 23576 12126627670 32546 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.analysis.interpolation; import org.apache.commons.math3.exception.NonMonotonicSequenceException; import org.apache.commons.math3.exception.DimensionMismatchException; import org.apache.commons.math3.exception.NumberIsTooSmallException; import org.apache.commons.math3.util.FastMath; import org.apache.commons.math3.TestUtils; import org.apache.commons.math3.analysis.UnivariateFunction; import org.apache.commons.math3.analysis.polynomials.PolynomialFunction; import org.apache.commons.math3.analysis.polynomials.PolynomialSplineFunction; import org.junit.Assert; import org.junit.Test; /** * Test the SplineInterpolator. * * @version $Id: SplineInterpolatorTest.java 1364030 2012-07-21 01:10:04Z erans $ */ public class SplineInterpolatorTest { /** error tolerance for spline interpolator value at knot points */ protected double knotTolerance = 1E-12; /** error tolerance for interpolating polynomial coefficients */ protected double coefficientTolerance = 1E-6; /** error tolerance for interpolated values -- high value is from sin test */ protected double interpolationTolerance = 1E-2; @Test public void testInterpolateLinearDegenerateTwoSegment() { double x[] = { 0.0, 0.5, 1.0 }; double y[] = { 0.0, 0.5, 1.0 }; UnivariateInterpolator i = new SplineInterpolator(); UnivariateFunction f = i.interpolate(x, y); verifyInterpolation(f, x, y); verifyConsistency((PolynomialSplineFunction) f, x); // Verify coefficients using analytical values PolynomialFunction polynomials[] = ((PolynomialSplineFunction) f).getPolynomials(); double target[] = {y[0], 1d}; TestUtils.assertEquals(polynomials[0].getCoefficients(), target, coefficientTolerance); target = new double[]{y[1], 1d}; TestUtils.assertEquals(polynomials[1].getCoefficients(), target, coefficientTolerance); // Check interpolation Assert.assertEquals(0.0,f.value(0.0), interpolationTolerance); Assert.assertEquals(0.4,f.value(0.4), interpolationTolerance); Assert.assertEquals(1.0,f.value(1.0), interpolationTolerance); } @Test public void testInterpolateLinearDegenerateThreeSegment() { double x[] = { 0.0, 0.5, 1.0, 1.5 }; double y[] = { 0.0, 0.5, 1.0, 1.5 }; UnivariateInterpolator i = new SplineInterpolator(); UnivariateFunction f = i.interpolate(x, y); verifyInterpolation(f, x, y); // Verify coefficients using analytical values PolynomialFunction polynomials[] = ((PolynomialSplineFunction) f).getPolynomials(); double target[] = {y[0], 1d}; TestUtils.assertEquals(polynomials[0].getCoefficients(), target, coefficientTolerance); target = new double[]{y[1], 1d}; TestUtils.assertEquals(polynomials[1].getCoefficients(), target, coefficientTolerance); target = new double[]{y[2], 1d}; TestUtils.assertEquals(polynomials[2].getCoefficients(), target, coefficientTolerance); // Check interpolation Assert.assertEquals(0,f.value(0), interpolationTolerance); Assert.assertEquals(1.4,f.value(1.4), interpolationTolerance); Assert.assertEquals(1.5,f.value(1.5), interpolationTolerance); } @Test public void testInterpolateLinear() { double x[] = { 0.0, 0.5, 1.0 }; double y[] = { 0.0, 0.5, 0.0 }; UnivariateInterpolator i = new SplineInterpolator(); UnivariateFunction f = i.interpolate(x, y); verifyInterpolation(f, x, y); verifyConsistency((PolynomialSplineFunction) f, x); // Verify coefficients using analytical values PolynomialFunction polynomials[] = ((PolynomialSplineFunction) f).getPolynomials(); double target[] = {y[0], 1.5d, 0d, -2d}; TestUtils.assertEquals(polynomials[0].getCoefficients(), target, coefficientTolerance); target = new double[]{y[1], 0d, -3d, 2d}; TestUtils.assertEquals(polynomials[1].getCoefficients(), target, coefficientTolerance); } @Test public void testInterpolateSin() { double x[] = { 0.0, FastMath.PI / 6d, FastMath.PI / 2d, 5d * FastMath.PI / 6d, FastMath.PI, 7d * FastMath.PI / 6d, 3d * FastMath.PI / 2d, 11d * FastMath.PI / 6d, 2.d * FastMath.PI }; double y[] = { 0d, 0.5d, 1d, 0.5d, 0d, -0.5d, -1d, -0.5d, 0d }; UnivariateInterpolator i = new SplineInterpolator(); UnivariateFunction f = i.interpolate(x, y); verifyInterpolation(f, x, y); verifyConsistency((PolynomialSplineFunction) f, x); /* Check coefficients against values computed using R (version 1.8.1, Red Hat Linux 9) * * To replicate in R: * x[1] <- 0 * x[2] <- pi / 6, etc, same for y[] (could use y <- scan() for y values) * g <- splinefun(x, y, "natural") * splinecoef <- eval(expression(z), envir = environment(g)) * print(splinecoef) */ PolynomialFunction polynomials[] = ((PolynomialSplineFunction) f).getPolynomials(); double target[] = {y[0], 1.002676d, 0d, -0.17415829d}; TestUtils.assertEquals(polynomials[0].getCoefficients(), target, coefficientTolerance); target = new double[]{y[1], 8.594367e-01, -2.735672e-01, -0.08707914}; TestUtils.assertEquals(polynomials[1].getCoefficients(), target, coefficientTolerance); target = new double[]{y[2], 1.471804e-17,-5.471344e-01, 0.08707914}; TestUtils.assertEquals(polynomials[2].getCoefficients(), target, coefficientTolerance); target = new double[]{y[3], -8.594367e-01, -2.735672e-01, 0.17415829}; TestUtils.assertEquals(polynomials[3].getCoefficients(), target, coefficientTolerance); target = new double[]{y[4], -1.002676, 6.548562e-17, 0.17415829}; TestUtils.assertEquals(polynomials[4].getCoefficients(), target, coefficientTolerance); target = new double[]{y[5], -8.594367e-01, 2.735672e-01, 0.08707914}; TestUtils.assertEquals(polynomials[5].getCoefficients(), target, coefficientTolerance); target = new double[]{y[6], 3.466465e-16, 5.471344e-01, -0.08707914}; TestUtils.assertEquals(polynomials[6].getCoefficients(), target, coefficientTolerance); target = new double[]{y[7], 8.594367e-01, 2.735672e-01, -0.17415829}; TestUtils.assertEquals(polynomials[7].getCoefficients(), target, coefficientTolerance); //Check interpolation Assert.assertEquals(FastMath.sqrt(2d) / 2d,f.value(FastMath.PI/4d),interpolationTolerance); Assert.assertEquals(FastMath.sqrt(2d) / 2d,f.value(3d*FastMath.PI/4d),interpolationTolerance); } @Test public void testIllegalArguments() { // Data set arrays of different size. UnivariateInterpolator i = new SplineInterpolator(); try { double xval[] = { 0.0, 1.0 }; double yval[] = { 0.0, 1.0, 2.0 }; i.interpolate(xval, yval); Assert.fail("Failed to detect data set array with different sizes."); } catch (DimensionMismatchException iae) { // Expected. } // X values not sorted. try { double xval[] = { 0.0, 1.0, 0.5 }; double yval[] = { 0.0, 1.0, 2.0 }; i.interpolate(xval, yval); Assert.fail("Failed to detect unsorted arguments."); } catch (NonMonotonicSequenceException iae) { // Expected. } // Not enough data to interpolate. try { double xval[] = { 0.0, 1.0 }; double yval[] = { 0.0, 1.0 }; i.interpolate(xval, yval); Assert.fail("Failed to detect unsorted arguments."); } catch (NumberIsTooSmallException iae) { // Expected. } } /** * verifies that f(x[i]) = y[i] for i = 0..n-1 where n is common length. */ protected void verifyInterpolation(UnivariateFunction f, double x[], double y[]) { for (int i = 0; i < x.length; i++) { Assert.assertEquals(f.value(x[i]), y[i], knotTolerance); } } /** * Verifies that interpolating polynomials satisfy consistency requirement: * adjacent polynomials must agree through two derivatives at knot points */ protected void verifyConsistency(PolynomialSplineFunction f, double x[]) { PolynomialFunction polynomials[] = f.getPolynomials(); for (int i = 1; i < x.length - 2; i++) { // evaluate polynomials and derivatives at x[i + 1] Assert.assertEquals(polynomials[i].value(x[i +1] - x[i]), polynomials[i + 1].value(0), 0.1); Assert.assertEquals(polynomials[i].derivative().value(x[i +1] - x[i]), polynomials[i + 1].derivative().value(0), 0.5); Assert.assertEquals(polynomials[i].polynomialDerivative().derivative().value(x[i +1] - x[i]), polynomials[i + 1].polynomialDerivative().derivative().value(0), 0.5); } } } ././@LongLink100644 0 0 201 12126630646 10252 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/analysis/interpolation/TricubicSplineInterpolatingFunctionTest.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/analysis/interpolation/TricubicSplineIn100644 1750 1750 57326 12126627670 32460 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.analysis.interpolation; import org.apache.commons.math3.exception.DimensionMismatchException; import org.apache.commons.math3.exception.MathIllegalArgumentException; import org.apache.commons.math3.util.FastMath; import org.apache.commons.math3.analysis.TrivariateFunction; import org.junit.Assert; import org.junit.Test; /** * Test case for the bicubic function. * * @version $Id$ */ public final class TricubicSplineInterpolatingFunctionTest { /** * Test preconditions. */ @Test public void testPreconditions() { double[] xval = new double[] {3, 4, 5, 6.5}; double[] yval = new double[] {-4, -3, -1, 2.5}; double[] zval = new double[] {-12, -8, -5.5, -3, 0, 2.5}; double[][][] fval = new double[xval.length][yval.length][zval.length]; @SuppressWarnings("unused") TrivariateFunction tcf = new TricubicSplineInterpolatingFunction(xval, yval, zval, fval, fval, fval, fval, fval, fval, fval, fval); double[] wxval = new double[] {3, 2, 5, 6.5}; try { tcf = new TricubicSplineInterpolatingFunction(wxval, yval, zval, fval, fval, fval, fval, fval, fval, fval, fval); Assert.fail("an exception should have been thrown"); } catch (MathIllegalArgumentException e) { // Expected } double[] wyval = new double[] {-4, -1, -1, 2.5}; try { tcf = new TricubicSplineInterpolatingFunction(xval, wyval, zval, fval, fval, fval, fval, fval, fval, fval, fval); Assert.fail("an exception should have been thrown"); } catch (MathIllegalArgumentException e) { // Expected } double[] wzval = new double[] {-12, -8, -9, -3, 0, 2.5}; try { tcf = new TricubicSplineInterpolatingFunction(xval, yval, wzval, fval, fval, fval, fval, fval, fval, fval, fval); Assert.fail("an exception should have been thrown"); } catch (MathIllegalArgumentException e) { // Expected } double[][][] wfval = new double[xval.length - 1][yval.length - 1][zval.length]; try { tcf = new TricubicSplineInterpolatingFunction(xval, yval, zval, wfval, fval, fval, fval, fval, fval, fval, fval); Assert.fail("an exception should have been thrown"); } catch (DimensionMismatchException e) { // Expected } try { tcf = new TricubicSplineInterpolatingFunction(xval, yval, zval, fval, wfval, fval, fval, fval, fval, fval, fval); Assert.fail("an exception should have been thrown"); } catch (DimensionMismatchException e) { // Expected } try { tcf = new TricubicSplineInterpolatingFunction(xval, yval, zval, fval, fval, wfval, fval, fval, fval, fval, fval); Assert.fail("an exception should have been thrown"); } catch (DimensionMismatchException e) { // Expected } try { tcf = new TricubicSplineInterpolatingFunction(xval, yval, zval, fval, fval, fval, wfval, fval, fval, fval, fval); Assert.fail("an exception should have been thrown"); } catch (DimensionMismatchException e) { // Expected } try { tcf = new TricubicSplineInterpolatingFunction(xval, yval, zval, fval, fval, fval, fval, wfval, fval, fval, fval); Assert.fail("an exception should have been thrown"); } catch (DimensionMismatchException e) { // Expected } try { tcf = new TricubicSplineInterpolatingFunction(xval, yval, zval, fval, fval, fval, fval, fval, wfval, fval, fval); Assert.fail("an exception should have been thrown"); } catch (DimensionMismatchException e) { // Expected } try { tcf = new TricubicSplineInterpolatingFunction(xval, yval, zval, fval, fval, fval, fval, fval, fval, wfval, fval); Assert.fail("an exception should have been thrown"); } catch (DimensionMismatchException e) { // Expected } try { tcf = new TricubicSplineInterpolatingFunction(xval, yval, zval, fval, fval, fval, fval, fval, fval, fval, wfval); Assert.fail("an exception should have been thrown"); } catch (DimensionMismatchException e) { // Expected } wfval = new double[xval.length][yval.length - 1][zval.length]; try { tcf = new TricubicSplineInterpolatingFunction(xval, yval, zval, wfval, fval, fval, fval, fval, fval, fval, fval); Assert.fail("an exception should have been thrown"); } catch (DimensionMismatchException e) { // Expected } try { tcf = new TricubicSplineInterpolatingFunction(xval, yval, zval, fval, wfval, fval, fval, fval, fval, fval, fval); Assert.fail("an exception should have been thrown"); } catch (DimensionMismatchException e) { // Expected } try { tcf = new TricubicSplineInterpolatingFunction(xval, yval, zval, fval, fval, wfval, fval, fval, fval, fval, fval); Assert.fail("an exception should have been thrown"); } catch (DimensionMismatchException e) { // Expected } try { tcf = new TricubicSplineInterpolatingFunction(xval, yval, zval, fval, fval, fval, wfval, fval, fval, fval, fval); Assert.fail("an exception should have been thrown"); } catch (DimensionMismatchException e) { // Expected } try { tcf = new TricubicSplineInterpolatingFunction(xval, yval, zval, fval, fval, fval, fval, wfval, fval, fval, fval); Assert.fail("an exception should have been thrown"); } catch (DimensionMismatchException e) { // Expected } try { tcf = new TricubicSplineInterpolatingFunction(xval, yval, zval, fval, fval, fval, fval, fval, wfval, fval, fval); Assert.fail("an exception should have been thrown"); } catch (DimensionMismatchException e) { // Expected } try { tcf = new TricubicSplineInterpolatingFunction(xval, yval, zval, fval, fval, fval, fval, fval, fval, wfval, fval); Assert.fail("an exception should have been thrown"); } catch (DimensionMismatchException e) { // Expected } try { tcf = new TricubicSplineInterpolatingFunction(xval, yval, zval, fval, fval, fval, fval, fval, fval, fval, wfval); Assert.fail("an exception should have been thrown"); } catch (DimensionMismatchException e) { // Expected } wfval = new double[xval.length][yval.length][zval.length - 1]; try { tcf = new TricubicSplineInterpolatingFunction(xval, yval, zval, wfval, fval, fval, fval, fval, fval, fval, fval); Assert.fail("an exception should have been thrown"); } catch (DimensionMismatchException e) { // Expected } try { tcf = new TricubicSplineInterpolatingFunction(xval, yval, zval, fval, wfval, fval, fval, fval, fval, fval, fval); Assert.fail("an exception should have been thrown"); } catch (DimensionMismatchException e) { // Expected } try { tcf = new TricubicSplineInterpolatingFunction(xval, yval, zval, fval, fval, wfval, fval, fval, fval, fval, fval); Assert.fail("an exception should have been thrown"); } catch (DimensionMismatchException e) { // Expected } try { tcf = new TricubicSplineInterpolatingFunction(xval, yval, zval, fval, fval, fval, wfval, fval, fval, fval, fval); Assert.fail("an exception should have been thrown"); } catch (DimensionMismatchException e) { // Expected } try { tcf = new TricubicSplineInterpolatingFunction(xval, yval, zval, fval, fval, fval, fval, wfval, fval, fval, fval); Assert.fail("an exception should have been thrown"); } catch (DimensionMismatchException e) { // Expected } try { tcf = new TricubicSplineInterpolatingFunction(xval, yval, zval, fval, fval, fval, fval, fval, wfval, fval, fval); Assert.fail("an exception should have been thrown"); } catch (DimensionMismatchException e) { // Expected } try { tcf = new TricubicSplineInterpolatingFunction(xval, yval, zval, fval, fval, fval, fval, fval, fval, wfval, fval); Assert.fail("an exception should have been thrown"); } catch (DimensionMismatchException e) { // Expected } try { tcf = new TricubicSplineInterpolatingFunction(xval, yval, zval, fval, fval, fval, fval, fval, fval, fval, wfval); Assert.fail("an exception should have been thrown"); } catch (DimensionMismatchException e) { // Expected } } /** * Test for a plane. *

              * f(x, y, z) = 2 x - 3 y - 4 z + 5 *

              */ @Test public void testPlane() { double[] xval = new double[] {3, 4, 5, 6.5}; double[] yval = new double[] {-4, -3, -1, 2, 2.5}; double[] zval = new double[] {-12, -8, -5.5, -3, 0, 2.5}; // Function values TrivariateFunction f = new TrivariateFunction() { public double value(double x, double y, double z) { return 2 * x - 3 * y - 4 * z + 5; } }; double[][][] fval = new double[xval.length][yval.length][zval.length]; for (int i = 0; i < xval.length; i++) { for (int j = 0; j < yval.length; j++) { for (int k = 0; k < zval.length; k++) { fval[i][j][k] = f.value(xval[i], yval[j], zval[k]); } } } // Partial derivatives with respect to x double[][][] dFdX = new double[xval.length][yval.length][zval.length]; for (int i = 0; i < xval.length; i++) { for (int j = 0; j < yval.length; j++) { for (int k = 0; k < zval.length; k++) { dFdX[i][j][k] = 2; } } } // Partial derivatives with respect to y double[][][] dFdY = new double[xval.length][yval.length][zval.length]; for (int i = 0; i < xval.length; i++) { for (int j = 0; j < yval.length; j++) { for (int k = 0; k < zval.length; k++) { dFdY[i][j][k] = -3; } } } // Partial derivatives with respect to z double[][][] dFdZ = new double[xval.length][yval.length][zval.length]; for (int i = 0; i < xval.length; i++) { for (int j = 0; j < yval.length; j++) { for (int k = 0; k < zval.length; k++) { dFdZ[i][j][k] = -4; } } } // Partial cross-derivatives double[][][] d2FdXdY = new double[xval.length][yval.length][zval.length]; double[][][] d2FdXdZ = new double[xval.length][yval.length][zval.length]; double[][][] d2FdYdZ = new double[xval.length][yval.length][zval.length]; double[][][] d3FdXdYdZ = new double[xval.length][yval.length][zval.length]; for (int i = 0; i < xval.length; i++) { for (int j = 0; j < yval.length; j++) { for (int k = 0; k < zval.length; k++) { d2FdXdY[i][j][k] = 0; d2FdXdZ[i][j][k] = 0; d2FdYdZ[i][j][k] = 0; d3FdXdYdZ[i][j][k] = 0; } } } TrivariateFunction tcf = new TricubicSplineInterpolatingFunction(xval, yval, zval, fval, dFdX, dFdY, dFdZ, d2FdXdY, d2FdXdZ, d2FdYdZ, d3FdXdYdZ); double x, y, z; double expected, result; x = 4; y = -3; z = 0; expected = f.value(x, y, z); result = tcf.value(x, y, z); Assert.assertEquals("On sample point", expected, result, 1e-15); x = 4.5; y = -1.5; z = -4.25; expected = f.value(x, y, z); result = tcf.value(x, y, z); Assert.assertEquals("Half-way between sample points (middle of the patch)", expected, result, 0.3); x = 3.5; y = -3.5; z = -10; expected = f.value(x, y, z); result = tcf.value(x, y, z); Assert.assertEquals("Half-way between sample points (border of the patch)", expected, result, 0.3); } /** * Sine wave. *

              * f(x, y, z) = a cos [ω z - ky x - ky y] *

              * with A = 0.2, ω = 0.5, kx = 2, ky = 1. */ @Test public void testWave() { double[] xval = new double[] {3, 4, 5, 6.5}; double[] yval = new double[] {-4, -3, -1, 2, 2.5}; double[] zval = new double[] {-12, -8, -5.5, -3, 0, 4}; final double a = 0.2; final double omega = 0.5; final double kx = 2; final double ky = 1; // Function values TrivariateFunction f = new TrivariateFunction() { public double value(double x, double y, double z) { return a * FastMath.cos(omega * z - kx * x - ky * y); } }; double[][][] fval = new double[xval.length][yval.length][zval.length]; for (int i = 0; i < xval.length; i++) { for (int j = 0; j < yval.length; j++) { for (int k = 0; k < zval.length; k++) { fval[i][j][k] = f.value(xval[i], yval[j], zval[k]); } } } // Partial derivatives with respect to x double[][][] dFdX = new double[xval.length][yval.length][zval.length]; TrivariateFunction dFdX_f = new TrivariateFunction() { public double value(double x, double y, double z) { return a * FastMath.sin(omega * z - kx * x - ky * y) * kx; } }; for (int i = 0; i < xval.length; i++) { for (int j = 0; j < yval.length; j++) { for (int k = 0; k < zval.length; k++) { dFdX[i][j][k] = dFdX_f.value(xval[i], yval[j], zval[k]); } } } // Partial derivatives with respect to y double[][][] dFdY = new double[xval.length][yval.length][zval.length]; TrivariateFunction dFdY_f = new TrivariateFunction() { public double value(double x, double y, double z) { return a * FastMath.sin(omega * z - kx * x - ky * y) * ky; } }; for (int i = 0; i < xval.length; i++) { for (int j = 0; j < yval.length; j++) { for (int k = 0; k < zval.length; k++) { dFdY[i][j][k] = dFdY_f.value(xval[i], yval[j], zval[k]); } } } // Partial derivatives with respect to z double[][][] dFdZ = new double[xval.length][yval.length][zval.length]; TrivariateFunction dFdZ_f = new TrivariateFunction() { public double value(double x, double y, double z) { return -a * FastMath.sin(omega * z - kx * x - ky * y) * omega; } }; for (int i = 0; i < xval.length; i++) { for (int j = 0; j < yval.length; j++) { for (int k = 0; k < zval.length; k++) { dFdZ[i][j][k] = dFdZ_f.value(xval[i], yval[j], zval[k]); } } } // Partial second derivatives w.r.t. (x, y) double[][][] d2FdXdY = new double[xval.length][yval.length][zval.length]; TrivariateFunction d2FdXdY_f = new TrivariateFunction() { public double value(double x, double y, double z) { return -a * FastMath.cos(omega * z - kx * x - ky * y) * kx * ky; } }; for (int i = 0; i < xval.length; i++) { for (int j = 0; j < yval.length; j++) { for (int k = 0; k < zval.length; k++) { d2FdXdY[i][j][k] = d2FdXdY_f.value(xval[i], yval[j], zval[k]); } } } // Partial second derivatives w.r.t. (x, z) double[][][] d2FdXdZ = new double[xval.length][yval.length][zval.length]; TrivariateFunction d2FdXdZ_f = new TrivariateFunction() { public double value(double x, double y, double z) { return a * FastMath.cos(omega * z - kx * x - ky * y) * kx * omega; } }; for (int i = 0; i < xval.length; i++) { for (int j = 0; j < yval.length; j++) { for (int k = 0; k < zval.length; k++) { d2FdXdZ[i][j][k] = d2FdXdZ_f.value(xval[i], yval[j], zval[k]); } } } // Partial second derivatives w.r.t. (y, z) double[][][] d2FdYdZ = new double[xval.length][yval.length][zval.length]; TrivariateFunction d2FdYdZ_f = new TrivariateFunction() { public double value(double x, double y, double z) { return a * FastMath.cos(omega * z - kx * x - ky * y) * ky * omega; } }; for (int i = 0; i < xval.length; i++) { for (int j = 0; j < yval.length; j++) { for (int k = 0; k < zval.length; k++) { d2FdYdZ[i][j][k] = d2FdYdZ_f.value(xval[i], yval[j], zval[k]); } } } // Partial third derivatives double[][][] d3FdXdYdZ = new double[xval.length][yval.length][zval.length]; TrivariateFunction d3FdXdYdZ_f = new TrivariateFunction() { public double value(double x, double y, double z) { return a * FastMath.sin(omega * z - kx * x - ky * y) * kx * ky * omega; } }; for (int i = 0; i < xval.length; i++) { for (int j = 0; j < yval.length; j++) { for (int k = 0; k < zval.length; k++) { d3FdXdYdZ[i][j][k] = d3FdXdYdZ_f.value(xval[i], yval[j], zval[k]); } } } TrivariateFunction tcf = new TricubicSplineInterpolatingFunction(xval, yval, zval, fval, dFdX, dFdY, dFdZ, d2FdXdY, d2FdXdZ, d2FdYdZ, d3FdXdYdZ); double x, y, z; double expected, result; x = 4; y = -3; z = 0; expected = f.value(x, y, z); result = tcf.value(x, y, z); Assert.assertEquals("On sample point", expected, result, 1e-14); x = 4.5; y = -1.5; z = -4.25; expected = f.value(x, y, z); result = tcf.value(x, y, z); Assert.assertEquals("Half-way between sample points (middle of the patch)", expected, result, 0.1); x = 3.5; y = -3.5; z = -10; expected = f.value(x, y, z); result = tcf.value(x, y, z); Assert.assertEquals("Half-way between sample points (border of the patch)", expected, result, 0.1); } } ././@LongLink100644 0 0 212 12126630646 10254 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/analysis/interpolation/SmoothingPolynomialBicubicSplineInterpolatorTest.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/analysis/interpolation/SmoothingPolynom100644 1750 1750 14654 12126627670 32574 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.analysis.interpolation; import org.apache.commons.math3.exception.DimensionMismatchException; import org.apache.commons.math3.exception.MathIllegalArgumentException; import org.apache.commons.math3.util.FastMath; import org.apache.commons.math3.analysis.BivariateFunction; import org.junit.Assert; import org.junit.Test; /** * Test case for the smoothing bicubic interpolator. * * @version $Id: SmoothingPolynomialBicubicSplineInterpolatorTest.java 1244107 2012-02-14 16:17:55Z erans $ */ public final class SmoothingPolynomialBicubicSplineInterpolatorTest { /** * Test preconditions. */ @Test public void testPreconditions() { double[] xval = new double[] {3, 4, 5, 6.5}; double[] yval = new double[] {-4, -3, -1, 2.5}; double[][] zval = new double[xval.length][yval.length]; BivariateGridInterpolator interpolator = new SmoothingPolynomialBicubicSplineInterpolator(0); @SuppressWarnings("unused") BivariateFunction p = interpolator.interpolate(xval, yval, zval); double[] wxval = new double[] {3, 2, 5, 6.5}; try { p = interpolator.interpolate(wxval, yval, zval); Assert.fail("an exception should have been thrown"); } catch (MathIllegalArgumentException e) { // Expected } double[] wyval = new double[] {-4, -3, -1, -1}; try { p = interpolator.interpolate(xval, wyval, zval); Assert.fail("an exception should have been thrown"); } catch (MathIllegalArgumentException e) { // Expected } double[][] wzval = new double[xval.length][yval.length + 1]; try { p = interpolator.interpolate(xval, yval, wzval); Assert.fail("an exception should have been thrown"); } catch (DimensionMismatchException e) { // Expected } wzval = new double[xval.length - 1][yval.length]; try { p = interpolator.interpolate(xval, yval, wzval); Assert.fail("an exception should have been thrown"); } catch (DimensionMismatchException e) { // Expected } wzval = new double[xval.length][yval.length - 1]; try { p = interpolator.interpolate(xval, yval, wzval); Assert.fail("an exception should have been thrown"); } catch (DimensionMismatchException e) { // Expected } } /** * Test of interpolator for a plane. *

              * z = 2 x - 3 y + 5 */ @Test public void testPlane() { BivariateFunction f = new BivariateFunction() { public double value(double x, double y) { return 2 * x - 3 * y + 5 + ((int) (FastMath.abs(5 * x + 3 * y)) % 2 == 0 ? 1 : -1); } }; BivariateGridInterpolator interpolator = new SmoothingPolynomialBicubicSplineInterpolator(1); double[] xval = new double[] {3, 4, 5, 6.5}; double[] yval = new double[] {-4, -3, -1, 2, 2.5}; double[][] zval = new double[xval.length][yval.length]; for (int i = 0; i < xval.length; i++) { for (int j = 0; j < yval.length; j++) { zval[i][j] = f.value(xval[i], yval[j]); } } BivariateFunction p = interpolator.interpolate(xval, yval, zval); double x, y; double expected, result; x = 4; y = -3; expected = f.value(x, y); result = p.value(x, y); Assert.assertEquals("On sample point", expected, result, 2); x = 4.5; y = -1.5; expected = f.value(x, y); result = p.value(x, y); Assert.assertEquals("half-way between sample points (middle of the patch)", expected, result, 2); x = 3.5; y = -3.5; expected = f.value(x, y); result = p.value(x, y); Assert.assertEquals("half-way between sample points (border of the patch)", expected, result, 2); } /** * Test of interpolator for a paraboloid. *

              * z = 2 x2 - 3 y2 + 4 x y - 5 */ @Test public void testParaboloid() { BivariateFunction f = new BivariateFunction() { public double value(double x, double y) { return 2 * x * x - 3 * y * y + 4 * x * y - 5 + ((int) (FastMath.abs(5 * x + 3 * y)) % 2 == 0 ? 1 : -1); } }; BivariateGridInterpolator interpolator = new SmoothingPolynomialBicubicSplineInterpolator(4); double[] xval = new double[] {3, 4, 5, 6.5}; double[] yval = new double[] {-4, -3, -2, -1, 0.5, 2.5}; double[][] zval = new double[xval.length][yval.length]; for (int i = 0; i < xval.length; i++) { for (int j = 0; j < yval.length; j++) { zval[i][j] = f.value(xval[i], yval[j]); } } BivariateFunction p = interpolator.interpolate(xval, yval, zval); double x, y; double expected, result; x = 5; y = 0.5; expected = f.value(x, y); result = p.value(x, y); Assert.assertEquals("On sample point", expected, result, 2); x = 4.5; y = -1.5; expected = f.value(x, y); result = p.value(x, y); Assert.assertEquals("half-way between sample points (middle of the patch)", expected, result, 2); x = 3.5; y = -3.5; expected = f.value(x, y); result = p.value(x, y); Assert.assertEquals("half-way between sample points (border of the patch)", expected, result, 2); } } ././@LongLink100644 0 0 157 12126630646 10264 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/analysis/interpolation/LoessInterpolatorTest.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/analysis/interpolation/LoessInterpolato100644 1750 1750 22173 12126627670 32550 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.analysis.interpolation; import org.apache.commons.math3.util.FastMath; import org.apache.commons.math3.exception.OutOfRangeException; import org.apache.commons.math3.exception.DimensionMismatchException; import org.apache.commons.math3.exception.NoDataException; import org.apache.commons.math3.exception.NonMonotonicSequenceException; import org.apache.commons.math3.exception.NotFiniteNumberException; import org.apache.commons.math3.exception.NumberIsTooSmallException; import org.junit.Assert; import org.junit.Test; /** * Test of the LoessInterpolator class. */ public class LoessInterpolatorTest { @Test public void testOnOnePoint() { double[] xval = {0.5}; double[] yval = {0.7}; double[] res = new LoessInterpolator().smooth(xval, yval); Assert.assertEquals(1, res.length); Assert.assertEquals(0.7, res[0], 0.0); } @Test public void testOnTwoPoints() { double[] xval = {0.5, 0.6}; double[] yval = {0.7, 0.8}; double[] res = new LoessInterpolator().smooth(xval, yval); Assert.assertEquals(2, res.length); Assert.assertEquals(0.7, res[0], 0.0); Assert.assertEquals(0.8, res[1], 0.0); } @Test public void testOnStraightLine() { double[] xval = {1,2,3,4,5}; double[] yval = {2,4,6,8,10}; LoessInterpolator li = new LoessInterpolator(0.6, 2, 1e-12); double[] res = li.smooth(xval, yval); Assert.assertEquals(5, res.length); for(int i = 0; i < 5; ++i) { Assert.assertEquals(yval[i], res[i], 1e-8); } } @Test public void testOnDistortedSine() { int numPoints = 100; double[] xval = new double[numPoints]; double[] yval = new double[numPoints]; double xnoise = 0.1; double ynoise = 0.2; generateSineData(xval, yval, xnoise, ynoise); LoessInterpolator li = new LoessInterpolator(0.3, 4, 1e-12); double[] res = li.smooth(xval, yval); // Check that the resulting curve differs from // the "real" sine less than the jittered one double noisyResidualSum = 0; double fitResidualSum = 0; for(int i = 0; i < numPoints; ++i) { double expected = FastMath.sin(xval[i]); double noisy = yval[i]; double fit = res[i]; noisyResidualSum += FastMath.pow(noisy - expected, 2); fitResidualSum += FastMath.pow(fit - expected, 2); } Assert.assertTrue(fitResidualSum < noisyResidualSum); } @Test public void testIncreasingBandwidthIncreasesSmoothness() { int numPoints = 100; double[] xval = new double[numPoints]; double[] yval = new double[numPoints]; double xnoise = 0.1; double ynoise = 0.1; generateSineData(xval, yval, xnoise, ynoise); // Check that variance decreases as bandwidth increases double[] bandwidths = {0.1, 0.5, 1.0}; double[] variances = new double[bandwidths.length]; for (int i = 0; i < bandwidths.length; i++) { double bw = bandwidths[i]; LoessInterpolator li = new LoessInterpolator(bw, 4, 1e-12); double[] res = li.smooth(xval, yval); for (int j = 1; j < res.length; ++j) { variances[i] += FastMath.pow(res[j] - res[j-1], 2); } } for(int i = 1; i < variances.length; ++i) { Assert.assertTrue(variances[i] < variances[i-1]); } } @Test public void testIncreasingRobustnessItersIncreasesSmoothnessWithOutliers() { int numPoints = 100; double[] xval = new double[numPoints]; double[] yval = new double[numPoints]; double xnoise = 0.1; double ynoise = 0.1; generateSineData(xval, yval, xnoise, ynoise); // Introduce a couple of outliers yval[numPoints/3] *= 100; yval[2 * numPoints/3] *= -100; // Check that variance decreases as the number of robustness // iterations increases double[] variances = new double[4]; for (int i = 0; i < 4; i++) { LoessInterpolator li = new LoessInterpolator(0.3, i, 1e-12); double[] res = li.smooth(xval, yval); for (int j = 1; j < res.length; ++j) { variances[i] += FastMath.abs(res[j] - res[j-1]); } } for(int i = 1; i < variances.length; ++i) { Assert.assertTrue(variances[i] < variances[i-1]); } } @Test(expected=DimensionMismatchException.class) public void testUnequalSizeArguments() { new LoessInterpolator().smooth(new double[] {1,2,3}, new double[] {1,2,3,4}); } @Test(expected=NoDataException.class) public void testEmptyData() { new LoessInterpolator().smooth(new double[] {}, new double[] {}); } @Test(expected=NonMonotonicSequenceException.class) public void testNonStrictlyIncreasing1() { new LoessInterpolator().smooth(new double[] {4,3,1,2}, new double[] {3,4,5,6}); } @Test(expected=NonMonotonicSequenceException.class) public void testNonStrictlyIncreasing2() { new LoessInterpolator().smooth(new double[] {1,2,2,3}, new double[] {3,4,5,6}); } @Test(expected=NotFiniteNumberException.class) public void testNotAllFiniteReal1() { new LoessInterpolator().smooth(new double[] {1,2,Double.NaN}, new double[] {3,4,5}); } @Test(expected=NotFiniteNumberException.class) public void testNotAllFiniteReal2() { new LoessInterpolator().smooth(new double[] {1,2,Double.POSITIVE_INFINITY}, new double[] {3,4,5}); } @Test(expected=NotFiniteNumberException.class) public void testNotAllFiniteReal3() { new LoessInterpolator().smooth(new double[] {1,2,Double.NEGATIVE_INFINITY}, new double[] {3,4,5}); } @Test(expected=NotFiniteNumberException.class) public void testNotAllFiniteReal4() { new LoessInterpolator().smooth(new double[] {3,4,5}, new double[] {1,2,Double.NaN}); } @Test(expected=NotFiniteNumberException.class) public void testNotAllFiniteReal5() { new LoessInterpolator().smooth(new double[] {3,4,5}, new double[] {1,2,Double.POSITIVE_INFINITY}); } @Test(expected=NotFiniteNumberException.class) public void testNotAllFiniteReal6() { new LoessInterpolator().smooth(new double[] {3,4,5}, new double[] {1,2,Double.NEGATIVE_INFINITY}); } @Test(expected=NumberIsTooSmallException.class) public void testInsufficientBandwidth() { LoessInterpolator li = new LoessInterpolator(0.1, 3, 1e-12); li.smooth(new double[] {1,2,3,4,5,6,7,8,9,10,11,12}, new double[] {1,2,3,4,5,6,7,8,9,10,11,12}); } @Test(expected=OutOfRangeException.class) public void testCompletelyIncorrectBandwidth1() { new LoessInterpolator(-0.2, 3, 1e-12); } @Test(expected=OutOfRangeException.class) public void testCompletelyIncorrectBandwidth2() { new LoessInterpolator(1.1, 3, 1e-12); } @Test public void testMath296withoutWeights() { double[] xval = { 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 1.7, 1.8, 1.9, 2.0}; double[] yval = { 0.47, 0.48, 0.55, 0.56, -0.08, -0.04, -0.07, -0.07, -0.56, -0.46, -0.56, -0.52, -3.03, -3.08, -3.09, -3.04, 3.54, 3.46, 3.36, 3.35}; // Output from R, rounded to .001 double[] yref = { 0.461, 0.499, 0.541, 0.308, 0.175, -0.042, -0.072, -0.196, -0.311, -0.446, -0.557, -1.497, -2.133, -3.08, -3.09, -0.621, 0.982, 3.449, 3.389, 3.336 }; LoessInterpolator li = new LoessInterpolator(0.3, 4, 1e-12); double[] res = li.smooth(xval, yval); Assert.assertEquals(xval.length, res.length); for(int i = 0; i < res.length; ++i) { Assert.assertEquals(yref[i], res[i], 0.02); } } private void generateSineData(double[] xval, double[] yval, double xnoise, double ynoise) { double dx = 2 * FastMath.PI / xval.length; double x = 0; for(int i = 0; i < xval.length; ++i) { xval[i] = x; yval[i] = FastMath.sin(x) + (2 * FastMath.random() - 1) * ynoise; x += dx * (1 + (2 * FastMath.random() - 1) * xnoise); } } } ././@LongLink100644 0 0 166 12126630646 10264 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/analysis/interpolation/FieldHermiteInterpolatorTest.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/analysis/interpolation/FieldHermiteInte100644 1750 1750 32117 12126627670 32422 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.analysis.interpolation; import java.util.Random; import org.apache.commons.math3.analysis.polynomials.PolynomialFunction; import org.apache.commons.math3.dfp.Dfp; import org.apache.commons.math3.dfp.DfpField; import org.apache.commons.math3.exception.NoDataException; import org.apache.commons.math3.fraction.BigFraction; import org.apache.commons.math3.util.FastMath; import org.junit.Assert; import org.junit.Test; public class FieldHermiteInterpolatorTest { @Test public void testZero() { FieldHermiteInterpolator interpolator = new FieldHermiteInterpolator(); interpolator.addSamplePoint(new BigFraction(0), new BigFraction[] { new BigFraction(0) }); for (int x = -10; x < 10; x++) { BigFraction y = interpolator.value(new BigFraction(x))[0]; Assert.assertEquals(BigFraction.ZERO, y); BigFraction[][] derivatives = interpolator.derivatives(new BigFraction(x), 1); Assert.assertEquals(BigFraction.ZERO, derivatives[0][0]); Assert.assertEquals(BigFraction.ZERO, derivatives[1][0]); } } @Test public void testQuadratic() { FieldHermiteInterpolator interpolator = new FieldHermiteInterpolator(); interpolator.addSamplePoint(new BigFraction(0), new BigFraction[] { new BigFraction(2) }); interpolator.addSamplePoint(new BigFraction(1), new BigFraction[] { new BigFraction(0) }); interpolator.addSamplePoint(new BigFraction(2), new BigFraction[] { new BigFraction(0) }); for (double x = -10; x < 10; x += 1.0) { BigFraction y = interpolator.value(new BigFraction(x))[0]; Assert.assertEquals((x - 1) * (x - 2), y.doubleValue(), 1.0e-15); BigFraction[][] derivatives = interpolator.derivatives(new BigFraction(x), 3); Assert.assertEquals((x - 1) * (x - 2), derivatives[0][0].doubleValue(), 1.0e-15); Assert.assertEquals(2 * x - 3, derivatives[1][0].doubleValue(), 1.0e-15); Assert.assertEquals(2, derivatives[2][0].doubleValue(), 1.0e-15); Assert.assertEquals(0, derivatives[3][0].doubleValue(), 1.0e-15); } } @Test public void testMixedDerivatives() { FieldHermiteInterpolator interpolator = new FieldHermiteInterpolator(); interpolator.addSamplePoint(new BigFraction(0), new BigFraction[] { new BigFraction(1) }, new BigFraction[] { new BigFraction(2) }); interpolator.addSamplePoint(new BigFraction(1), new BigFraction[] { new BigFraction(4) }); interpolator.addSamplePoint(new BigFraction(2), new BigFraction[] { new BigFraction(5) }, new BigFraction[] { new BigFraction(2) }); BigFraction[][] derivatives = interpolator.derivatives(new BigFraction(0), 5); Assert.assertEquals(new BigFraction( 1), derivatives[0][0]); Assert.assertEquals(new BigFraction( 2), derivatives[1][0]); Assert.assertEquals(new BigFraction( 8), derivatives[2][0]); Assert.assertEquals(new BigFraction(-24), derivatives[3][0]); Assert.assertEquals(new BigFraction( 24), derivatives[4][0]); Assert.assertEquals(new BigFraction( 0), derivatives[5][0]); derivatives = interpolator.derivatives(new BigFraction(1), 5); Assert.assertEquals(new BigFraction( 4), derivatives[0][0]); Assert.assertEquals(new BigFraction( 2), derivatives[1][0]); Assert.assertEquals(new BigFraction( -4), derivatives[2][0]); Assert.assertEquals(new BigFraction( 0), derivatives[3][0]); Assert.assertEquals(new BigFraction( 24), derivatives[4][0]); Assert.assertEquals(new BigFraction( 0), derivatives[5][0]); derivatives = interpolator.derivatives(new BigFraction(2), 5); Assert.assertEquals(new BigFraction( 5), derivatives[0][0]); Assert.assertEquals(new BigFraction( 2), derivatives[1][0]); Assert.assertEquals(new BigFraction( 8), derivatives[2][0]); Assert.assertEquals(new BigFraction( 24), derivatives[3][0]); Assert.assertEquals(new BigFraction( 24), derivatives[4][0]); Assert.assertEquals(new BigFraction( 0), derivatives[5][0]); } @Test public void testRandomPolynomialsValuesOnly() { Random random = new Random(0x42b1e7dbd361a932l); for (int i = 0; i < 100; ++i) { int maxDegree = 0; PolynomialFunction[] p = new PolynomialFunction[5]; for (int k = 0; k < p.length; ++k) { int degree = random.nextInt(7); p[k] = randomPolynomial(degree, random); maxDegree = FastMath.max(maxDegree, degree); } DfpField field = new DfpField(30); Dfp step = field.getOne().divide(field.newDfp(10)); FieldHermiteInterpolator interpolator = new FieldHermiteInterpolator(); for (int j = 0; j < 1 + maxDegree; ++j) { Dfp x = field.newDfp(j).multiply(step); Dfp[] values = new Dfp[p.length]; for (int k = 0; k < p.length; ++k) { values[k] = field.newDfp(p[k].value(x.getReal())); } interpolator.addSamplePoint(x, values); } for (int j = 0; j < 20; ++j) { Dfp x = field.newDfp(j).multiply(step); Dfp[] values = interpolator.value(x); Assert.assertEquals(p.length, values.length); for (int k = 0; k < p.length; ++k) { Assert.assertEquals(p[k].value(x.getReal()), values[k].getReal(), 1.0e-8 * FastMath.abs(p[k].value(x.getReal()))); } } } } @Test public void testRandomPolynomialsFirstDerivative() { Random random = new Random(0x570803c982ca5d3bl); for (int i = 0; i < 100; ++i) { int maxDegree = 0; PolynomialFunction[] p = new PolynomialFunction[5]; PolynomialFunction[] pPrime = new PolynomialFunction[5]; for (int k = 0; k < p.length; ++k) { int degree = random.nextInt(7); p[k] = randomPolynomial(degree, random); pPrime[k] = p[k].polynomialDerivative(); maxDegree = FastMath.max(maxDegree, degree); } DfpField field = new DfpField(30); Dfp step = field.getOne().divide(field.newDfp(10)); FieldHermiteInterpolator interpolator = new FieldHermiteInterpolator(); for (int j = 0; j < 1 + maxDegree / 2; ++j) { Dfp x = field.newDfp(j).multiply(step); Dfp[] values = new Dfp[p.length]; Dfp[] derivatives = new Dfp[p.length]; for (int k = 0; k < p.length; ++k) { values[k] = field.newDfp(p[k].value(x.getReal())); derivatives[k] = field.newDfp(pPrime[k].value(x.getReal())); } interpolator.addSamplePoint(x, values, derivatives); } Dfp h = step.divide(field.newDfp(100000)); for (int j = 0; j < 20; ++j) { Dfp x = field.newDfp(j).multiply(step); Dfp[] y = interpolator.value(x); Dfp[] yP = interpolator.value(x.add(h)); Dfp[] yM = interpolator.value(x.subtract(h)); Assert.assertEquals(p.length, y.length); for (int k = 0; k < p.length; ++k) { Assert.assertEquals(p[k].value(x.getReal()), y[k].getReal(), 1.0e-8 * FastMath.abs(p[k].value(x.getReal()))); Assert.assertEquals(pPrime[k].value(x.getReal()), yP[k].subtract(yM[k]).divide(h.multiply(2)).getReal(), 4.0e-8 * FastMath.abs(p[k].value(x.getReal()))); } } } } @Test public void testSine() { DfpField field = new DfpField(30); FieldHermiteInterpolator interpolator = new FieldHermiteInterpolator(); for (Dfp x = field.getZero(); x.getReal() < FastMath.PI; x = x.add(0.5)) { interpolator.addSamplePoint(x, new Dfp[] { x.sin() }); } for (Dfp x = field.newDfp(0.1); x.getReal() < 2.9; x = x.add(0.01)) { Dfp y = interpolator.value(x)[0]; Assert.assertEquals( x.sin().getReal(), y.getReal(), 3.5e-5); } } @Test public void testSquareRoot() { DfpField field = new DfpField(30); FieldHermiteInterpolator interpolator = new FieldHermiteInterpolator(); for (Dfp x = field.getOne(); x.getReal() < 3.6; x = x.add(0.5)) { interpolator.addSamplePoint(x, new Dfp[] { x.sqrt() }); } for (Dfp x = field.newDfp(1.1); x.getReal() < 3.5; x = x.add(0.01)) { Dfp y = interpolator.value(x)[0]; Assert.assertEquals(x.sqrt().getReal(), y.getReal(), 1.5e-4); } } @Test public void testWikipedia() { // this test corresponds to the example from Wikipedia page: // http://en.wikipedia.org/wiki/Hermite_interpolation FieldHermiteInterpolator interpolator = new FieldHermiteInterpolator(); interpolator.addSamplePoint(new BigFraction(-1), new BigFraction[] { new BigFraction( 2) }, new BigFraction[] { new BigFraction(-8) }, new BigFraction[] { new BigFraction(56) }); interpolator.addSamplePoint(new BigFraction( 0), new BigFraction[] { new BigFraction( 1) }, new BigFraction[] { new BigFraction( 0) }, new BigFraction[] { new BigFraction( 0) }); interpolator.addSamplePoint(new BigFraction( 1), new BigFraction[] { new BigFraction( 2) }, new BigFraction[] { new BigFraction( 8) }, new BigFraction[] { new BigFraction(56) }); for (BigFraction x = new BigFraction(-1); x.doubleValue() <= 1.0; x = x.add(new BigFraction(1, 8))) { BigFraction y = interpolator.value(x)[0]; BigFraction x2 = x.multiply(x); BigFraction x4 = x2.multiply(x2); BigFraction x8 = x4.multiply(x4); Assert.assertEquals(x8.add(new BigFraction(1)), y); } } @Test public void testOnePointParabola() { FieldHermiteInterpolator interpolator = new FieldHermiteInterpolator(); interpolator.addSamplePoint(new BigFraction(0), new BigFraction[] { new BigFraction(1) }, new BigFraction[] { new BigFraction(1) }, new BigFraction[] { new BigFraction(2) }); for (BigFraction x = new BigFraction(-1); x.doubleValue() <= 1.0; x = x.add(new BigFraction(1, 8))) { BigFraction y = interpolator.value(x)[0]; Assert.assertEquals(BigFraction.ONE.add(x.multiply(BigFraction.ONE.add(x))), y); } } private PolynomialFunction randomPolynomial(int degree, Random random) { double[] coeff = new double[ 1 + degree]; for (int j = 0; j < degree; ++j) { coeff[j] = random.nextDouble(); } return new PolynomialFunction(coeff); } @Test(expected=NoDataException.class) public void testEmptySampleValue() { new FieldHermiteInterpolator().value(BigFraction.ZERO); } @Test(expected=NoDataException.class) public void testEmptySampleDerivative() { new FieldHermiteInterpolator().derivatives(BigFraction.ZERO, 1); } @Test(expected=IllegalArgumentException.class) public void testDuplicatedAbscissa() { FieldHermiteInterpolator interpolator = new FieldHermiteInterpolator(); interpolator.addSamplePoint(new BigFraction(1), new BigFraction[] { new BigFraction(0) }); interpolator.addSamplePoint(new BigFraction(1), new BigFraction[] { new BigFraction(1) }); } } ././@LongLink100644 0 0 173 12126630646 10262 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/analysis/interpolation/DividedDifferenceInterpolatorTest.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/analysis/interpolation/DividedDifferenc100644 1750 1750 12630 12126627670 32415 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.analysis.interpolation; import org.apache.commons.math3.analysis.UnivariateFunction; import org.apache.commons.math3.analysis.function.Expm1; import org.apache.commons.math3.analysis.function.Sin; import org.apache.commons.math3.exception.NonMonotonicSequenceException; import org.apache.commons.math3.util.FastMath; import org.junit.Assert; import org.junit.Test; /** * Test case for Divided Difference interpolator. *

              * The error of polynomial interpolation is * f(z) - p(z) = f^(n)(zeta) * (z-x[0])(z-x[1])...(z-x[n-1]) / n! * where f^(n) is the n-th derivative of the approximated function and * zeta is some point in the interval determined by x[] and z. *

              * Since zeta is unknown, f^(n)(zeta) cannot be calculated. But we can bound * it and use the absolute value upper bound for estimates. For reference, * see Introduction to Numerical Analysis, ISBN 038795452X, chapter 2. * * @version $Id: DividedDifferenceInterpolatorTest.java 1374632 2012-08-18 18:11:11Z luc $ */ public final class DividedDifferenceInterpolatorTest { /** * Test of interpolator for the sine function. *

              * |sin^(n)(zeta)| <= 1.0, zeta in [0, 2*PI] */ @Test public void testSinFunction() { UnivariateFunction f = new Sin(); UnivariateInterpolator interpolator = new DividedDifferenceInterpolator(); double x[], y[], z, expected, result, tolerance; // 6 interpolating points on interval [0, 2*PI] int n = 6; double min = 0.0, max = 2 * FastMath.PI; x = new double[n]; y = new double[n]; for (int i = 0; i < n; i++) { x[i] = min + i * (max - min) / n; y[i] = f.value(x[i]); } double derivativebound = 1.0; UnivariateFunction p = interpolator.interpolate(x, y); z = FastMath.PI / 4; expected = f.value(z); result = p.value(z); tolerance = FastMath.abs(derivativebound * partialerror(x, z)); Assert.assertEquals(expected, result, tolerance); z = FastMath.PI * 1.5; expected = f.value(z); result = p.value(z); tolerance = FastMath.abs(derivativebound * partialerror(x, z)); Assert.assertEquals(expected, result, tolerance); } /** * Test of interpolator for the exponential function. *

              * |expm1^(n)(zeta)| <= e, zeta in [-1, 1] */ @Test public void testExpm1Function() { UnivariateFunction f = new Expm1(); UnivariateInterpolator interpolator = new DividedDifferenceInterpolator(); double x[], y[], z, expected, result, tolerance; // 5 interpolating points on interval [-1, 1] int n = 5; double min = -1.0, max = 1.0; x = new double[n]; y = new double[n]; for (int i = 0; i < n; i++) { x[i] = min + i * (max - min) / n; y[i] = f.value(x[i]); } double derivativebound = FastMath.E; UnivariateFunction p = interpolator.interpolate(x, y); z = 0.0; expected = f.value(z); result = p.value(z); tolerance = FastMath.abs(derivativebound * partialerror(x, z)); Assert.assertEquals(expected, result, tolerance); z = 0.5; expected = f.value(z); result = p.value(z); tolerance = FastMath.abs(derivativebound * partialerror(x, z)); Assert.assertEquals(expected, result, tolerance); z = -0.5; expected = f.value(z); result = p.value(z); tolerance = FastMath.abs(derivativebound * partialerror(x, z)); Assert.assertEquals(expected, result, tolerance); } /** * Test of parameters for the interpolator. */ @Test public void testParameters() { UnivariateInterpolator interpolator = new DividedDifferenceInterpolator(); try { // bad abscissas array double x[] = { 1.0, 2.0, 2.0, 4.0 }; double y[] = { 0.0, 4.0, 4.0, 2.5 }; UnivariateFunction p = interpolator.interpolate(x, y); p.value(0.0); Assert.fail("Expecting NonMonotonicSequenceException - bad abscissas array"); } catch (NonMonotonicSequenceException ex) { // expected } } /** * Returns the partial error term (z-x[0])(z-x[1])...(z-x[n-1])/n! */ protected double partialerror(double x[], double z) throws IllegalArgumentException { if (x.length < 1) { throw new IllegalArgumentException ("Interpolation array cannot be empty."); } double out = 1; for (int i = 0; i < x.length; i++) { out *= (z - x[i]) / (i + 1); } return out; } } commons-math3-3.2-src/src/test/java/org/apache/commons/math3/analysis/MonitoredFunction.java100644 1750 1750 2673 12126627670 30724 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.analysis; /** * Wrapper class for counting functions calls. * * @version $Id: MonitoredFunction.java 1244107 2012-02-14 16:17:55Z erans $ */ public class MonitoredFunction implements UnivariateFunction { public MonitoredFunction(UnivariateFunction f) { callsCount = 0; this.f = f; } public void setCallsCount(int callsCount) { this.callsCount = callsCount; } public int getCallsCount() { return callsCount; } public double value(double x) { ++callsCount; return f.value(x); } private int callsCount; private UnivariateFunction f; } ././@LongLink100644 0 0 157 12126630646 10264 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/analysis/integration/TrapezoidIntegratorTest.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/analysis/integration/TrapezoidIntegrato100644 1750 1750 11604 12126627670 32511 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.analysis.integration; import org.apache.commons.math3.analysis.QuinticFunction; import org.apache.commons.math3.analysis.UnivariateFunction; import org.apache.commons.math3.analysis.function.Sin; import org.apache.commons.math3.exception.NumberIsTooLargeException; import org.apache.commons.math3.exception.NumberIsTooSmallException; import org.apache.commons.math3.util.FastMath; import org.junit.Assert; import org.junit.Test; /** * Test case for trapezoid integrator. *

              * Test runs show that for a default relative accuracy of 1E-6, it * generally takes 10 to 15 iterations for the integral to converge. * * @version $Id: TrapezoidIntegratorTest.java 1374632 2012-08-18 18:11:11Z luc $ */ public final class TrapezoidIntegratorTest { /** * Test of integrator for the sine function. */ @Test public void testSinFunction() { UnivariateFunction f = new Sin(); UnivariateIntegrator integrator = new TrapezoidIntegrator(); double min, max, expected, result, tolerance; min = 0; max = FastMath.PI; expected = 2; tolerance = FastMath.abs(expected * integrator.getRelativeAccuracy()); result = integrator.integrate(10000, f, min, max); Assert.assertTrue(integrator.getEvaluations() < 2500); Assert.assertTrue(integrator.getIterations() < 15); Assert.assertEquals(expected, result, tolerance); min = -FastMath.PI/3; max = 0; expected = -0.5; tolerance = FastMath.abs(expected * integrator.getRelativeAccuracy()); result = integrator.integrate(10000, f, min, max); Assert.assertTrue(integrator.getEvaluations() < 2500); Assert.assertTrue(integrator.getIterations() < 15); Assert.assertEquals(expected, result, tolerance); } /** * Test of integrator for the quintic function. */ @Test public void testQuinticFunction() { UnivariateFunction f = new QuinticFunction(); UnivariateIntegrator integrator = new TrapezoidIntegrator(); double min, max, expected, result, tolerance; min = 0; max = 1; expected = -1.0/48; tolerance = FastMath.abs(expected * integrator.getRelativeAccuracy()); result = integrator.integrate(10000, f, min, max); Assert.assertTrue(integrator.getEvaluations() < 5000); Assert.assertTrue(integrator.getIterations() < 15); Assert.assertEquals(expected, result, tolerance); min = 0; max = 0.5; expected = 11.0/768; tolerance = FastMath.abs(expected * integrator.getRelativeAccuracy()); result = integrator.integrate(10000, f, min, max); Assert.assertTrue(integrator.getEvaluations() < 2500); Assert.assertTrue(integrator.getIterations() < 15); Assert.assertEquals(expected, result, tolerance); min = -1; max = 4; expected = 2048/3.0 - 78 + 1.0/48; tolerance = FastMath.abs(expected * integrator.getRelativeAccuracy()); result = integrator.integrate(10000, f, min, max); Assert.assertTrue(integrator.getEvaluations() < 5000); Assert.assertTrue(integrator.getIterations() < 15); Assert.assertEquals(expected, result, tolerance); } /** * Test of parameters for the integrator. */ @Test public void testParameters() { UnivariateFunction f = new Sin(); try { // bad interval new TrapezoidIntegrator().integrate(1000, f, 1, -1); Assert.fail("Expecting NumberIsTooLargeException - bad interval"); } catch (NumberIsTooLargeException ex) { // expected } try { // bad iteration limits new TrapezoidIntegrator(5, 4); Assert.fail("Expecting NumberIsTooSmallException - bad iteration limits"); } catch (NumberIsTooSmallException ex) { // expected } try { // bad iteration limits new TrapezoidIntegrator(10,99); Assert.fail("Expecting NumberIsTooLargeException - bad iteration limits"); } catch (NumberIsTooLargeException ex) { // expected } } } ././@LongLink100644 0 0 174 12126630646 10263 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/analysis/integration/IterativeLegendreGaussIntegratorTest.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/analysis/integration/IterativeLegendreG100644 1750 1750 15145 12126627670 32410 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.analysis.integration; import java.util.Random; import org.apache.commons.math3.analysis.QuinticFunction; import org.apache.commons.math3.analysis.UnivariateFunction; import org.apache.commons.math3.analysis.function.Sin; import org.apache.commons.math3.analysis.polynomials.PolynomialFunction; import org.apache.commons.math3.exception.TooManyEvaluationsException; import org.apache.commons.math3.util.FastMath; import org.junit.Assert; import org.junit.Test; public class IterativeLegendreGaussIntegratorTest { @Test public void testSinFunction() { UnivariateFunction f = new Sin(); BaseAbstractUnivariateIntegrator integrator = new IterativeLegendreGaussIntegrator(5, 1.0e-14, 1.0e-10, 2, 15); double min, max, expected, result, tolerance; min = 0; max = FastMath.PI; expected = 2; tolerance = FastMath.max(integrator.getAbsoluteAccuracy(), FastMath.abs(expected * integrator.getRelativeAccuracy())); result = integrator.integrate(10000, f, min, max); Assert.assertEquals(expected, result, tolerance); min = -FastMath.PI/3; max = 0; expected = -0.5; tolerance = FastMath.max(integrator.getAbsoluteAccuracy(), FastMath.abs(expected * integrator.getRelativeAccuracy())); result = integrator.integrate(10000, f, min, max); Assert.assertEquals(expected, result, tolerance); } @Test public void testQuinticFunction() { UnivariateFunction f = new QuinticFunction(); UnivariateIntegrator integrator = new IterativeLegendreGaussIntegrator(3, BaseAbstractUnivariateIntegrator.DEFAULT_RELATIVE_ACCURACY, BaseAbstractUnivariateIntegrator.DEFAULT_ABSOLUTE_ACCURACY, BaseAbstractUnivariateIntegrator.DEFAULT_MIN_ITERATIONS_COUNT, 64); double min, max, expected, result; min = 0; max = 1; expected = -1.0/48; result = integrator.integrate(10000, f, min, max); Assert.assertEquals(expected, result, 1.0e-16); min = 0; max = 0.5; expected = 11.0/768; result = integrator.integrate(10000, f, min, max); Assert.assertEquals(expected, result, 1.0e-16); min = -1; max = 4; expected = 2048/3.0 - 78 + 1.0/48; result = integrator.integrate(10000, f, min, max); Assert.assertEquals(expected, result, 1.0e-16); } @Test public void testExactIntegration() { Random random = new Random(86343623467878363l); for (int n = 2; n < 6; ++n) { IterativeLegendreGaussIntegrator integrator = new IterativeLegendreGaussIntegrator(n, BaseAbstractUnivariateIntegrator.DEFAULT_RELATIVE_ACCURACY, BaseAbstractUnivariateIntegrator.DEFAULT_ABSOLUTE_ACCURACY, BaseAbstractUnivariateIntegrator.DEFAULT_MIN_ITERATIONS_COUNT, 64); // an n points Gauss-Legendre integrator integrates 2n-1 degree polynoms exactly for (int degree = 0; degree <= 2 * n - 1; ++degree) { for (int i = 0; i < 10; ++i) { double[] coeff = new double[degree + 1]; for (int k = 0; k < coeff.length; ++k) { coeff[k] = 2 * random.nextDouble() - 1; } PolynomialFunction p = new PolynomialFunction(coeff); double result = integrator.integrate(10000, p, -5.0, 15.0); double reference = exactIntegration(p, -5.0, 15.0); Assert.assertEquals(n + " " + degree + " " + i, reference, result, 1.0e-12 * (1.0 + FastMath.abs(reference))); } } } } @Test public void testIssue464() { final double value = 0.2; UnivariateFunction f = new UnivariateFunction() { public double value(double x) { return (x >= 0 && x <= 5) ? value : 0.0; } }; IterativeLegendreGaussIntegrator gauss = new IterativeLegendreGaussIntegrator(5, 3, 100); // due to the discontinuity, integration implies *many* calls double maxX = 0.32462367623786328; Assert.assertEquals(maxX * value, gauss.integrate(Integer.MAX_VALUE, f, -10, maxX), 1.0e-7); Assert.assertTrue(gauss.getEvaluations() > 37000000); Assert.assertTrue(gauss.getIterations() < 30); // setting up limits prevents such large number of calls try { gauss.integrate(1000, f, -10, maxX); Assert.fail("expected TooManyEvaluationsException"); } catch (TooManyEvaluationsException tmee) { // expected Assert.assertEquals(1000, tmee.getMax()); } // integrating on the two sides should be simpler double sum1 = gauss.integrate(1000, f, -10, 0); int eval1 = gauss.getEvaluations(); double sum2 = gauss.integrate(1000, f, 0, maxX); int eval2 = gauss.getEvaluations(); Assert.assertEquals(maxX * value, sum1 + sum2, 1.0e-7); Assert.assertTrue(eval1 + eval2 < 200); } private double exactIntegration(PolynomialFunction p, double a, double b) { final double[] coeffs = p.getCoefficients(); double yb = coeffs[coeffs.length - 1] / coeffs.length; double ya = yb; for (int i = coeffs.length - 2; i >= 0; --i) { yb = yb * b + coeffs[i] / (i + 1); ya = ya * a + coeffs[i] / (i + 1); } return yb * b - ya * a; } } ././@LongLink100644 0 0 155 12126630646 10262 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/analysis/integration/RombergIntegratorTest.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/analysis/integration/RombergIntegratorT100644 1750 1750 11645 12126627670 32460 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.analysis.integration; import org.apache.commons.math3.analysis.QuinticFunction; import org.apache.commons.math3.analysis.UnivariateFunction; import org.apache.commons.math3.analysis.function.Sin; import org.apache.commons.math3.exception.NumberIsTooLargeException; import org.apache.commons.math3.exception.NumberIsTooSmallException; import org.apache.commons.math3.util.FastMath; import org.junit.Assert; import org.junit.Test; /** * Test case for Romberg integrator. *

              * Romberg algorithm is very fast for good behavior integrand. Test runs * show that for a default relative accuracy of 1E-6, it generally takes * takes less than 5 iterations for the integral to converge. * * @version $Id: RombergIntegratorTest.java 1374632 2012-08-18 18:11:11Z luc $ */ public final class RombergIntegratorTest { /** * Test of integrator for the sine function. */ @Test public void testSinFunction() { UnivariateFunction f = new Sin(); UnivariateIntegrator integrator = new RombergIntegrator(); double min, max, expected, result, tolerance; min = 0; max = FastMath.PI; expected = 2; tolerance = FastMath.abs(expected * integrator.getRelativeAccuracy()); result = integrator.integrate(100, f, min, max); Assert.assertTrue(integrator.getEvaluations() < 50); Assert.assertTrue(integrator.getIterations() < 10); Assert.assertEquals(expected, result, tolerance); min = -FastMath.PI/3; max = 0; expected = -0.5; tolerance = FastMath.abs(expected * integrator.getRelativeAccuracy()); result = integrator.integrate(100, f, min, max); Assert.assertTrue(integrator.getEvaluations() < 50); Assert.assertTrue(integrator.getIterations() < 10); Assert.assertEquals(expected, result, tolerance); } /** * Test of integrator for the quintic function. */ @Test public void testQuinticFunction() { UnivariateFunction f = new QuinticFunction(); UnivariateIntegrator integrator = new RombergIntegrator(); double min, max, expected, result, tolerance; min = 0; max = 1; expected = -1.0/48; tolerance = FastMath.abs(expected * integrator.getRelativeAccuracy()); result = integrator.integrate(100, f, min, max); Assert.assertTrue(integrator.getEvaluations() < 10); Assert.assertTrue(integrator.getIterations() < 5); Assert.assertEquals(expected, result, tolerance); min = 0; max = 0.5; expected = 11.0/768; tolerance = FastMath.abs(expected * integrator.getRelativeAccuracy()); result = integrator.integrate(100, f, min, max); Assert.assertTrue(integrator.getEvaluations() < 10); Assert.assertTrue(integrator.getIterations() < 5); Assert.assertEquals(expected, result, tolerance); min = -1; max = 4; expected = 2048/3.0 - 78 + 1.0/48; tolerance = FastMath.abs(expected * integrator.getRelativeAccuracy()); result = integrator.integrate(100, f, min, max); Assert.assertTrue(integrator.getEvaluations() < 10); Assert.assertTrue(integrator.getIterations() < 5); Assert.assertEquals(expected, result, tolerance); } /** * Test of parameters for the integrator. */ @Test public void testParameters() { UnivariateFunction f = new Sin(); try { // bad interval new RombergIntegrator().integrate(1000, f, 1, -1); Assert.fail("Expecting NumberIsTooLargeException - bad interval"); } catch (NumberIsTooLargeException ex) { // expected } try { // bad iteration limits new RombergIntegrator(5, 4); Assert.fail("Expecting NumberIsTooSmallException - bad iteration limits"); } catch (NumberIsTooSmallException ex) { // expected } try { // bad iteration limits new RombergIntegrator(10, 50); Assert.fail("Expecting NumberIsTooLargeException - bad iteration limits"); } catch (NumberIsTooLargeException ex) { // expected } } } ././@LongLink100644 0 0 163 12126630646 10261 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/analysis/integration/LegendreGaussIntegratorTest.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/analysis/integration/LegendreGaussInteg100644 1750 1750 14723 12126627670 32417 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.analysis.integration; import java.util.Random; import org.apache.commons.math3.analysis.QuinticFunction; import org.apache.commons.math3.analysis.UnivariateFunction; import org.apache.commons.math3.analysis.function.Sin; import org.apache.commons.math3.analysis.polynomials.PolynomialFunction; import org.apache.commons.math3.exception.TooManyEvaluationsException; import org.apache.commons.math3.util.FastMath; import org.junit.Assert; import org.junit.Test; @Deprecated public class LegendreGaussIntegratorTest { @Test public void testSinFunction() { UnivariateFunction f = new Sin(); BaseAbstractUnivariateIntegrator integrator = new LegendreGaussIntegrator(5, 1.0e-14, 1.0e-10, 2, 15); double min, max, expected, result, tolerance; min = 0; max = FastMath.PI; expected = 2; tolerance = FastMath.max(integrator.getAbsoluteAccuracy(), FastMath.abs(expected * integrator.getRelativeAccuracy())); result = integrator.integrate(10000, f, min, max); Assert.assertEquals(expected, result, tolerance); min = -FastMath.PI/3; max = 0; expected = -0.5; tolerance = FastMath.max(integrator.getAbsoluteAccuracy(), FastMath.abs(expected * integrator.getRelativeAccuracy())); result = integrator.integrate(10000, f, min, max); Assert.assertEquals(expected, result, tolerance); } @Test public void testQuinticFunction() { UnivariateFunction f = new QuinticFunction(); UnivariateIntegrator integrator = new LegendreGaussIntegrator(3, BaseAbstractUnivariateIntegrator.DEFAULT_RELATIVE_ACCURACY, BaseAbstractUnivariateIntegrator.DEFAULT_ABSOLUTE_ACCURACY, BaseAbstractUnivariateIntegrator.DEFAULT_MIN_ITERATIONS_COUNT, 64); double min, max, expected, result; min = 0; max = 1; expected = -1.0/48; result = integrator.integrate(10000, f, min, max); Assert.assertEquals(expected, result, 1.0e-16); min = 0; max = 0.5; expected = 11.0/768; result = integrator.integrate(10000, f, min, max); Assert.assertEquals(expected, result, 1.0e-16); min = -1; max = 4; expected = 2048/3.0 - 78 + 1.0/48; result = integrator.integrate(10000, f, min, max); Assert.assertEquals(expected, result, 1.0e-16); } @Test public void testExactIntegration() { Random random = new Random(86343623467878363l); for (int n = 2; n < 6; ++n) { LegendreGaussIntegrator integrator = new LegendreGaussIntegrator(n, BaseAbstractUnivariateIntegrator.DEFAULT_RELATIVE_ACCURACY, BaseAbstractUnivariateIntegrator.DEFAULT_ABSOLUTE_ACCURACY, BaseAbstractUnivariateIntegrator.DEFAULT_MIN_ITERATIONS_COUNT, 64); // an n points Gauss-Legendre integrator integrates 2n-1 degree polynoms exactly for (int degree = 0; degree <= 2 * n - 1; ++degree) { for (int i = 0; i < 10; ++i) { double[] coeff = new double[degree + 1]; for (int k = 0; k < coeff.length; ++k) { coeff[k] = 2 * random.nextDouble() - 1; } PolynomialFunction p = new PolynomialFunction(coeff); double result = integrator.integrate(10000, p, -5.0, 15.0); double reference = exactIntegration(p, -5.0, 15.0); Assert.assertEquals(n + " " + degree + " " + i, reference, result, 1.0e-12 * (1.0 + FastMath.abs(reference))); } } } } @Test public void testIssue464() { final double value = 0.2; UnivariateFunction f = new UnivariateFunction() { public double value(double x) { return (x >= 0 && x <= 5) ? value : 0.0; } }; LegendreGaussIntegrator gauss = new LegendreGaussIntegrator(5, 3, 100); // due to the discontinuity, integration implies *many* calls double maxX = 0.32462367623786328; Assert.assertEquals(maxX * value, gauss.integrate(Integer.MAX_VALUE, f, -10, maxX), 1.0e-7); Assert.assertTrue(gauss.getEvaluations() > 37000000); Assert.assertTrue(gauss.getIterations() < 30); // setting up limits prevents such large number of calls try { gauss.integrate(1000, f, -10, maxX); Assert.fail("expected TooManyEvaluationsException"); } catch (TooManyEvaluationsException tmee) { // expected Assert.assertEquals(1000, tmee.getMax()); } // integrating on the two sides should be simpler double sum1 = gauss.integrate(1000, f, -10, 0); int eval1 = gauss.getEvaluations(); double sum2 = gauss.integrate(1000, f, 0, maxX); int eval2 = gauss.getEvaluations(); Assert.assertEquals(maxX * value, sum1 + sum2, 1.0e-7); Assert.assertTrue(eval1 + eval2 < 200); } private double exactIntegration(PolynomialFunction p, double a, double b) { final double[] coeffs = p.getCoefficients(); double yb = coeffs[coeffs.length - 1] / coeffs.length; double ya = yb; for (int i = coeffs.length - 2; i >= 0; --i) { yb = yb * b + coeffs[i] / (i + 1); ya = ya * a + coeffs[i] / (i + 1); } return yb * b - ya * a; } } ././@LongLink100644 0 0 155 12126630646 10262 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/analysis/integration/SimpsonIntegratorTest.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/analysis/integration/SimpsonIntegratorT100644 1750 1750 11547 12126627670 32514 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.analysis.integration; import org.apache.commons.math3.analysis.QuinticFunction; import org.apache.commons.math3.analysis.UnivariateFunction; import org.apache.commons.math3.analysis.function.Sin; import org.apache.commons.math3.exception.NumberIsTooLargeException; import org.apache.commons.math3.exception.NumberIsTooSmallException; import org.apache.commons.math3.util.FastMath; import org.junit.Assert; import org.junit.Test; /** * Test case for Simpson integrator. *

              * Test runs show that for a default relative accuracy of 1E-6, it * generally takes 5 to 10 iterations for the integral to converge. * * @version $Id: SimpsonIntegratorTest.java 1374632 2012-08-18 18:11:11Z luc $ */ public final class SimpsonIntegratorTest { /** * Test of integrator for the sine function. */ @Test public void testSinFunction() { UnivariateFunction f = new Sin(); UnivariateIntegrator integrator = new SimpsonIntegrator(); double min, max, expected, result, tolerance; min = 0; max = FastMath.PI; expected = 2; tolerance = FastMath.abs(expected * integrator.getRelativeAccuracy()); result = integrator.integrate(1000, f, min, max); Assert.assertTrue(integrator.getEvaluations() < 100); Assert.assertTrue(integrator.getIterations() < 10); Assert.assertEquals(expected, result, tolerance); min = -FastMath.PI/3; max = 0; expected = -0.5; tolerance = FastMath.abs(expected * integrator.getRelativeAccuracy()); result = integrator.integrate(1000, f, min, max); Assert.assertTrue(integrator.getEvaluations() < 50); Assert.assertTrue(integrator.getIterations() < 10); Assert.assertEquals(expected, result, tolerance); } /** * Test of integrator for the quintic function. */ @Test public void testQuinticFunction() { UnivariateFunction f = new QuinticFunction(); UnivariateIntegrator integrator = new SimpsonIntegrator(); double min, max, expected, result, tolerance; min = 0; max = 1; expected = -1.0/48; tolerance = FastMath.abs(expected * integrator.getRelativeAccuracy()); result = integrator.integrate(1000, f, min, max); Assert.assertTrue(integrator.getEvaluations() < 150); Assert.assertTrue(integrator.getIterations() < 10); Assert.assertEquals(expected, result, tolerance); min = 0; max = 0.5; expected = 11.0/768; tolerance = FastMath.abs(expected * integrator.getRelativeAccuracy()); result = integrator.integrate(1000, f, min, max); Assert.assertTrue(integrator.getEvaluations() < 100); Assert.assertTrue(integrator.getIterations() < 10); Assert.assertEquals(expected, result, tolerance); min = -1; max = 4; expected = 2048/3.0 - 78 + 1.0/48; tolerance = FastMath.abs(expected * integrator.getRelativeAccuracy()); result = integrator.integrate(1000, f, min, max); Assert.assertTrue(integrator.getEvaluations() < 150); Assert.assertTrue(integrator.getIterations() < 10); Assert.assertEquals(expected, result, tolerance); } /** * Test of parameters for the integrator. */ @Test public void testParameters() { UnivariateFunction f = new Sin(); try { // bad interval new SimpsonIntegrator().integrate(1000, f, 1, -1); Assert.fail("Expecting NumberIsTooLargeException - bad interval"); } catch (NumberIsTooLargeException ex) { // expected } try { // bad iteration limits new SimpsonIntegrator(5, 4); Assert.fail("Expecting NumberIsTooSmallException - bad iteration limits"); } catch (NumberIsTooSmallException ex) { // expected } try { // bad iteration limits new SimpsonIntegrator(10, 99); Assert.fail("Expecting NumberIsTooLargeException - bad iteration limits"); } catch (NumberIsTooLargeException ex) { // expected } } } ././@LongLink100644 0 0 164 12126630646 10262 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/analysis/integration/gauss/LegendreParametricTest.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/analysis/integration/gauss/LegendrePara100644 1750 1750 6063 12126627670 32331 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.analysis.integration.gauss; import java.util.ArrayList; import java.util.Collection; import org.junit.runner.RunWith; import org.junit.runners.Parameterized; import org.junit.runners.Parameterized.Parameters; /** * Test of the {@link LegendreRuleFactory}. * This parameterized test extends the standard test for Gaussian quadrature * rule, where each monomial is tested in turn. * Parametrization allows to test automatically 0, 1, ... , {@link #MAX_NUM_POINTS} * quadrature rules. * * @version $Id$ */ @RunWith(value=Parameterized.class) public class LegendreParametricTest extends GaussianQuadratureAbstractTest { private static GaussIntegratorFactory factory = new GaussIntegratorFactory(); /** * The highest order quadrature rule to be tested. */ public static final int MAX_NUM_POINTS = 30; /** * Creates a new instance of this test, with the specified number of nodes * for the Gauss-Legendre quadrature rule. * * @param numberOfPoints Order of integration rule. * @param maxDegree Maximum degree of monomials to be tested. * @param eps Value of ε. * @param numUlps Value of the maximum relative error (in ulps). */ public LegendreParametricTest(int numberOfPoints, int maxDegree, double eps, double numUlps) { super(factory.legendre(numberOfPoints), maxDegree, eps, numUlps); } /** * Returns the collection of parameters to be passed to the constructor of * this class. * Gauss-Legendre quadrature rules of order 1, ..., {@link #MAX_NUM_POINTS} * will be constructed. * * @return the collection of parameters for this parameterized test. */ @Parameters public static Collection getParameters() { final ArrayList parameters = new ArrayList(); for (int k = 1; k <= MAX_NUM_POINTS; k++) { parameters.add(new Object[] { k, 2 * k - 1, Math.ulp(1d), 91d }); } return parameters; } @Override public double getExpectedValue(final int n) { if (n % 2 == 1) { return 0; } return 2d / (n + 1); } } ././@LongLink100644 0 0 167 12126630646 10265 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/analysis/integration/gauss/LegendreHighPrecisionTest.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/analysis/integration/gauss/LegendreHigh100644 1750 1750 4266 12126627670 32330 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.analysis.integration.gauss; import org.apache.commons.math3.analysis.function.Cos; import org.apache.commons.math3.analysis.function.Inverse; import org.apache.commons.math3.analysis.function.Log; import org.apache.commons.math3.analysis.UnivariateFunction; import org.junit.Test; import org.junit.Assert; /** * Test of the {@link LegendreHighPrecisionRuleFactory}. * * @version $Id$ */ public class LegendreHighPrecisionTest { private static GaussIntegratorFactory factory = new GaussIntegratorFactory(); @Test public void testCos() { final UnivariateFunction cos = new Cos(); final GaussIntegrator integrator = factory.legendreHighPrecision(7, 0, Math.PI / 2); final double s = integrator.integrate(cos); // System.out.println("s=" + s + " e=" + 1); Assert.assertEquals(1, s, Math.ulp(1d)); } @Test public void testInverse() { final UnivariateFunction inv = new Inverse(); final UnivariateFunction log = new Log(); final double lo = 12.34; final double hi = 456.78; final GaussIntegrator integrator = factory.legendreHighPrecision(60, lo, hi); final double s = integrator.integrate(inv); final double expected = log.value(hi) - log.value(lo); // System.out.println("s=" + s + " e=" + expected); Assert.assertEquals(expected, s, 1e-15); } } ././@LongLink100644 0 0 152 12126630646 10257 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/analysis/integration/gauss/LegendreTest.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/analysis/integration/gauss/LegendreTest100644 1750 1750 4202 12126627670 32356 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.analysis.integration.gauss; import org.apache.commons.math3.analysis.function.Cos; import org.apache.commons.math3.analysis.function.Inverse; import org.apache.commons.math3.analysis.function.Log; import org.apache.commons.math3.analysis.UnivariateFunction; import org.junit.Test; import org.junit.Assert; /** * Test of the {@link LegendreRuleFactory}. * * @version $Id$ */ public class LegendreTest { private static GaussIntegratorFactory factory = new GaussIntegratorFactory(); @Test public void testCos() { final UnivariateFunction cos = new Cos(); final GaussIntegrator integrator = factory.legendre(7, 0, Math.PI / 2); final double s = integrator.integrate(cos); // System.out.println("s=" + s + " e=" + 1); Assert.assertEquals(1, s, Math.ulp(1d)); } @Test public void testInverse() { final UnivariateFunction inv = new Inverse(); final UnivariateFunction log = new Log(); final double lo = 12.34; final double hi = 456.78; final GaussIntegrator integrator = factory.legendre(60, lo, hi); final double s = integrator.integrate(inv); final double expected = log.value(hi) - log.value(lo); // System.out.println("s=" + s + " e=" + expected); Assert.assertEquals(expected, s, 1e-14); } } ././@LongLink100644 0 0 174 12126630646 10263 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/analysis/integration/gauss/GaussianQuadratureAbstractTest.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/analysis/integration/gauss/GaussianQuad100644 1750 1750 11322 12126627670 32377 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.analysis.integration.gauss; import org.apache.commons.math3.analysis.function.Power; import org.junit.Test; import org.junit.Assert; /** * Base class for standard testing of Gaussian quadrature rules, * which are exact for polynomials up to a certain degree. In this test, each * monomial in turn is tested against the specified quadrature rule. * * @version $Id$ */ public abstract class GaussianQuadratureAbstractTest { /** * The maximum absolute error (for zero testing). */ private final double eps; /** * The maximum relative error (in ulps). */ private final double numUlps; /** * The quadrature rule under test. */ private final GaussIntegrator integrator; /** * Maximum degree of monomials to be tested. */ private final int maxDegree; /** * Creates a new instance of this abstract test with the specified * quadrature rule. * If the expected value is non-zero, equality of actual and expected values * is checked in the relative sense

              * |xact - xexp| ≤  n  * Math.ulp(xexp),
              where n is * the maximum relative error (in ulps). If the expected value is zero, the * test checks that
              |xact| ≤ ε, *
              where ε is the maximum absolute error. * * @param integrator Quadrature rule under test. * @param maxDegree Maximum degree of monomials to be tested. * @param eps ε. * @param numUlps Value of the maximum relative error (in ulps). */ public GaussianQuadratureAbstractTest(GaussIntegrator integrator, int maxDegree, double eps, double numUlps) { this.integrator = integrator; this.maxDegree = maxDegree; this.eps = eps; this.numUlps = numUlps; } /** * Returns the expected value of the integral of the specified monomial. * The integration is carried out on the natural interval of the quadrature * rule under test. * * @param n Degree of the monomial. * @return the expected value of the integral of xn. */ public abstract double getExpectedValue(final int n); /** * Checks that the value of the integral of each monomial * x0, ... , xp * returned by the quadrature rule under test conforms with the expected * value. * Here {@code p} denotes the degree of the highest polynomial for which * exactness is to be expected. */ @Test public void testAllMonomials() { for (int n = 0; n <= maxDegree; n++) { final double expected = getExpectedValue(n); final Power monomial = new Power(n); final double actual = integrator.integrate(monomial); // System.out.println(n + "/" + maxDegree + " " + integrator.getNumberOfPoints() // + " " + expected + " " + actual + " " + Math.ulp(expected)); if (expected == 0) { Assert.assertEquals("while integrating monomial x**" + n + " with a " + integrator.getNumberOfPoints() + "-point quadrature rule", expected, actual, eps); } else { double err = Math.abs(actual - expected) / Math.ulp(expected); Assert.assertEquals("while integrating monomial x**" + n + " with a " + + integrator.getNumberOfPoints() + "-point quadrature rule, " + " error was " + err + " ulps", expected, actual, Math.ulp(expected) * numUlps); } } } } ././@LongLink100644 0 0 161 12126630646 10257 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/analysis/integration/gauss/BaseRuleFactoryTest.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/analysis/integration/gauss/BaseRuleFact100644 1750 1750 7565 12126627670 32310 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.analysis.integration.gauss; import java.util.List; import java.util.ArrayList; import java.util.concurrent.ThreadPoolExecutor; import java.util.concurrent.ArrayBlockingQueue; import java.util.concurrent.TimeUnit; import java.util.concurrent.Callable; import java.util.concurrent.Future; import java.util.concurrent.ExecutionException; import java.util.concurrent.atomic.AtomicInteger; import org.apache.commons.math3.util.Pair; import org.junit.Test; import org.junit.Assert; /** * Test for {@link BaseRuleFactory}. * * @version $Id$ */ public class BaseRuleFactoryTest { /** * Tests that a given rule rule will be computed and added once to the cache * whatever the number of times this rule is called concurrently. */ @Test public void testConcurrentCreation() throws InterruptedException, ExecutionException { // Number of times the same rule will be called. final int numTasks = 20; final ThreadPoolExecutor exec = new ThreadPoolExecutor(3, numTasks, 1, TimeUnit.SECONDS, new ArrayBlockingQueue(2)); final List>> results = new ArrayList>>(); for (int i = 0; i < numTasks; i++) { results.add(exec.submit(new RuleBuilder())); } // Ensure that all computations have completed. for (Future> f : results) { f.get(); } // Assertion would fail if "getRuleInternal" were not "synchronized". final int n = RuleBuilder.getNumberOfCalls(); Assert.assertEquals("Rule computation was called " + n + " times", 1, n); } } class RuleBuilder implements Callable> { private static final DummyRuleFactory factory = new DummyRuleFactory(); public Pair call() { final int dummy = 2; // Always request the same rule. return factory.getRule(dummy); } public static int getNumberOfCalls() { return factory.getNumberOfCalls(); } } class DummyRuleFactory extends BaseRuleFactory { /** Rule computations counter. */ private static AtomicInteger nCalls = new AtomicInteger(); @Override protected Pair computeRule(int order) { // Tracks whether this computation has been called more than once. nCalls.getAndIncrement(); try { // Sleep to simulate computation time. Thread.sleep(20); } catch (InterruptedException e) { Assert.fail("Unexpected interruption"); } // Dummy rule (but contents must exist). final Double[] p = new Double[order]; final Double[] w = new Double[order]; for (int i = 0; i < order; i++) { p[i] = new Double(i); w[i] = new Double(i); } return new Pair(p, w); } public int getNumberOfCalls() { return nCalls.get(); } } ././@LongLink100644 0 0 201 12126630646 10252 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/analysis/integration/gauss/LegendreHighPrecisionParametricTest.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/analysis/integration/gauss/LegendreHigh100644 1750 1750 6216 12126627670 32325 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.analysis.integration.gauss; import java.util.ArrayList; import java.util.Collection; import org.junit.runner.RunWith; import org.junit.runners.Parameterized; import org.junit.runners.Parameterized.Parameters; /** * Test of the {@link LegendreHighPrecisionRuleFactory}. * This parameterized test extends the standard test for Gaussian quadrature * rule, where each monomial is tested in turn. * Parametrization allows to test automatically 0, 1, ... , {@link #MAX_NUM_POINTS} * quadrature rules. * * @version $Id$ */ @RunWith(value=Parameterized.class) public class LegendreHighPrecisionParametricTest extends GaussianQuadratureAbstractTest { private static GaussIntegratorFactory factory = new GaussIntegratorFactory(); /** * The highest order quadrature rule to be tested. */ public static final int MAX_NUM_POINTS = 30; /** * Creates a new instance of this test, with the specified number of nodes * for the Gauss-Legendre quadrature rule. * * @param numberOfPoints Order of integration rule. * @param maxDegree Maximum degree of monomials to be tested. * @param eps Value of ε. * @param numUlps Value of the maximum relative error (in ulps). */ public LegendreHighPrecisionParametricTest(int numberOfPoints, int maxDegree, double eps, double numUlps) { super(factory.legendreHighPrecision(numberOfPoints), maxDegree, eps, numUlps); } /** * Returns the collection of parameters to be passed to the constructor of * this class. * Gauss-Legendre quadrature rules of order 1, ..., {@link #MAX_NUM_POINTS} * will be constructed. * * @return the collection of parameters for this parameterized test. */ @Parameters public static Collection getParameters() { final ArrayList parameters = new ArrayList(); for (int k = 1; k <= MAX_NUM_POINTS; k++) { parameters.add(new Object[] { k, 2 * k - 1, Math.ulp(1d), 13d }); } return parameters; } @Override public double getExpectedValue(final int n) { if (n % 2 == 1) { return 0; } return 2d / (n + 1); } } commons-math3-3.2-src/src/test/java/org/apache/commons/math3/analysis/SumSincFunction.java100644 1750 1750 3340 12126627670 30335 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.analysis; import org.apache.commons.math3.analysis.function.Sinc; /** * Auxiliary class for testing optimizers. * * @version $Id$ */ public class SumSincFunction implements MultivariateFunction { private static final UnivariateFunction sinc = new Sinc(); /** * Factor that will multiply each term of the sum. */ private final double factor; /** * @param factor Factor that will multiply each term of the sum. */ public SumSincFunction(double factor) { this.factor = factor; } /** * @param point Argument. * @return the value of this function at point {@code x}. */ public double value(double[] point) { double sum = 0; for (int i = 0, max = point.length; i < max; i++) { final double x = point[i]; final double v = sinc.value(x); sum += v; } return factor * sum; } } commons-math3-3.2-src/src/test/java/org/apache/commons/math3/analysis/function/SqrtTest.java100644 1750 1750 5571 12126627670 30674 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.analysis.function; import org.apache.commons.math3.analysis.UnivariateFunction; import org.apache.commons.math3.analysis.differentiation.DerivativeStructure; import org.apache.commons.math3.analysis.differentiation.UnivariateDifferentiableFunction; import org.apache.commons.math3.util.FastMath; import org.junit.Test; import org.junit.Assert; public class SqrtTest { @Test public void testComparison() { final Sqrt s = new Sqrt(); final UnivariateFunction f = new UnivariateFunction() { public double value(double x) { return Math.sqrt(x); } }; for (double x = 1e-30; x < 1e10; x *= 2) { final double fX = f.value(x); final double sX = s.value(x); Assert.assertEquals("x=" + x, fX, sX, 0); } } @Test public void testDerivativeComparison() { final UnivariateDifferentiableFunction sPrime = new Sqrt(); final UnivariateFunction f = new UnivariateFunction() { public double value(double x) { return 1 / (2 * Math.sqrt(x)); } }; for (double x = 1e-30; x < 1e10; x *= 2) { final double fX = f.value(x); final double sX = sPrime.value(new DerivativeStructure(1, 1, 0, x)).getPartialDerivative(1); Assert.assertEquals("x=" + x, fX, sX, FastMath.ulp(fX)); } } @Test public void testDerivativesHighOrder() { DerivativeStructure s = new Sqrt().value(new DerivativeStructure(1, 5, 0, 1.2)); Assert.assertEquals(1.0954451150103322269, s.getPartialDerivative(0), 1.0e-16); Assert.assertEquals(0.45643546458763842789, s.getPartialDerivative(1), 1.0e-16); Assert.assertEquals(-0.1901814435781826783, s.getPartialDerivative(2), 1.0e-16); Assert.assertEquals(0.23772680447272834785, s.getPartialDerivative(3), 1.0e-16); Assert.assertEquals(-0.49526417598485072465, s.getPartialDerivative(4), 1.0e-16); Assert.assertEquals(1.4445205132891479465, s.getPartialDerivative(5), 5.0e-16); } } commons-math3-3.2-src/src/test/java/org/apache/commons/math3/analysis/function/GaussianTest.java100644 1750 1750 15313 12126627670 31530 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.analysis.function; import org.apache.commons.math3.analysis.UnivariateFunction; import org.apache.commons.math3.analysis.differentiation.DerivativeStructure; import org.apache.commons.math3.analysis.differentiation.UnivariateDifferentiableFunction; import org.apache.commons.math3.exception.DimensionMismatchException; import org.apache.commons.math3.exception.NotStrictlyPositiveException; import org.apache.commons.math3.exception.NullArgumentException; import org.apache.commons.math3.util.FastMath; import org.junit.Assert; import org.junit.Test; /** * Test for class {@link Gaussian}. */ public class GaussianTest { private final double EPS = Math.ulp(1d); @Test(expected=NotStrictlyPositiveException.class) public void testPreconditions() { new Gaussian(1, 2, -1); } @Test public void testSomeValues() { final UnivariateFunction f = new Gaussian(); Assert.assertEquals(1 / FastMath.sqrt(2 * Math.PI), f.value(0), EPS); } @Test public void testLargeArguments() { final UnivariateFunction f = new Gaussian(); Assert.assertEquals(0, f.value(Double.NEGATIVE_INFINITY), 0); Assert.assertEquals(0, f.value(-Double.MAX_VALUE), 0); Assert.assertEquals(0, f.value(-1e2), 0); Assert.assertEquals(0, f.value(1e2), 0); Assert.assertEquals(0, f.value(Double.MAX_VALUE), 0); Assert.assertEquals(0, f.value(Double.POSITIVE_INFINITY), 0); } @Test public void testDerivatives() { final UnivariateDifferentiableFunction gaussian = new Gaussian(2.0, 0.9, 3.0); final DerivativeStructure dsX = new DerivativeStructure(1, 4, 0, 1.1); final DerivativeStructure dsY = gaussian.value(dsX); Assert.assertEquals( 1.9955604901712128349, dsY.getValue(), EPS); Assert.assertEquals(-0.044345788670471396332, dsY.getPartialDerivative(1), EPS); Assert.assertEquals(-0.22074348138190206174, dsY.getPartialDerivative(2), EPS); Assert.assertEquals( 0.014760030401924800557, dsY.getPartialDerivative(3), EPS); Assert.assertEquals( 0.073253159785035691678, dsY.getPartialDerivative(4), EPS); } @Test public void testDerivativeLargeArguments() { final Gaussian f = new Gaussian(0, 1e-50); Assert.assertEquals(0, f.value(new DerivativeStructure(1, 1, 0, Double.NEGATIVE_INFINITY)).getPartialDerivative(1), 0); Assert.assertEquals(0, f.value(new DerivativeStructure(1, 1, 0, -Double.MAX_VALUE)).getPartialDerivative(1), 0); Assert.assertEquals(0, f.value(new DerivativeStructure(1, 1, 0, -1e50)).getPartialDerivative(1), 0); Assert.assertEquals(0, f.value(new DerivativeStructure(1, 1, 0, -1e2)).getPartialDerivative(1), 0); Assert.assertEquals(0, f.value(new DerivativeStructure(1, 1, 0, 1e2)).getPartialDerivative(1), 0); Assert.assertEquals(0, f.value(new DerivativeStructure(1, 1, 0, 1e50)).getPartialDerivative(1), 0); Assert.assertEquals(0, f.value(new DerivativeStructure(1, 1, 0, Double.MAX_VALUE)).getPartialDerivative(1), 0); Assert.assertEquals(0, f.value(new DerivativeStructure(1, 1, 0, Double.POSITIVE_INFINITY)).getPartialDerivative(1), 0); } @Test public void testDerivativesNaN() { final Gaussian f = new Gaussian(0, 1e-50); final DerivativeStructure fx = f.value(new DerivativeStructure(1, 5, 0, Double.NaN)); for (int i = 0; i <= fx.getOrder(); ++i) { Assert.assertTrue(Double.isNaN(fx.getPartialDerivative(i))); } } @Test(expected=NullArgumentException.class) public void testParametricUsage1() { final Gaussian.Parametric g = new Gaussian.Parametric(); g.value(0, null); } @Test(expected=DimensionMismatchException.class) public void testParametricUsage2() { final Gaussian.Parametric g = new Gaussian.Parametric(); g.value(0, new double[] {0}); } @Test(expected=NotStrictlyPositiveException.class) public void testParametricUsage3() { final Gaussian.Parametric g = new Gaussian.Parametric(); g.value(0, new double[] {0, 1, 0}); } @Test(expected=NullArgumentException.class) public void testParametricUsage4() { final Gaussian.Parametric g = new Gaussian.Parametric(); g.gradient(0, null); } @Test(expected=DimensionMismatchException.class) public void testParametricUsage5() { final Gaussian.Parametric g = new Gaussian.Parametric(); g.gradient(0, new double[] {0}); } @Test(expected=NotStrictlyPositiveException.class) public void testParametricUsage6() { final Gaussian.Parametric g = new Gaussian.Parametric(); g.gradient(0, new double[] {0, 1, 0}); } @Test public void testParametricValue() { final double norm = 2; final double mean = 3; final double sigma = 4; final Gaussian f = new Gaussian(norm, mean, sigma); final Gaussian.Parametric g = new Gaussian.Parametric(); Assert.assertEquals(f.value(-1), g.value(-1, new double[] {norm, mean, sigma}), 0); Assert.assertEquals(f.value(0), g.value(0, new double[] {norm, mean, sigma}), 0); Assert.assertEquals(f.value(2), g.value(2, new double[] {norm, mean, sigma}), 0); } @Test public void testParametricGradient() { final double norm = 2; final double mean = 3; final double sigma = 4; final Gaussian.Parametric f = new Gaussian.Parametric(); final double x = 1; final double[] grad = f.gradient(1, new double[] {norm, mean, sigma}); final double diff = x - mean; final double n = FastMath.exp(-diff * diff / (2 * sigma * sigma)); Assert.assertEquals(n, grad[0], EPS); final double m = norm * n * diff / (sigma * sigma); Assert.assertEquals(m, grad[1], EPS); final double s = m * diff / sigma; Assert.assertEquals(s, grad[2], EPS); } } commons-math3-3.2-src/src/test/java/org/apache/commons/math3/analysis/function/LogitTest.java100644 1750 1750 22674 12126627670 31044 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.analysis.function; import org.apache.commons.math3.analysis.UnivariateFunction; import org.apache.commons.math3.analysis.FunctionUtils; import org.apache.commons.math3.analysis.differentiation.DerivativeStructure; import org.apache.commons.math3.analysis.differentiation.UnivariateDifferentiableFunction; import org.apache.commons.math3.exception.NullArgumentException; import org.apache.commons.math3.exception.DimensionMismatchException; import org.apache.commons.math3.exception.OutOfRangeException; import org.apache.commons.math3.random.RandomGenerator; import org.apache.commons.math3.random.Well1024a; import org.apache.commons.math3.util.FastMath; import org.junit.Assert; import org.junit.Test; /** * Test for class {@link Logit}. */ public class LogitTest { private final double EPS = Math.ulp(1d); @Test(expected=OutOfRangeException.class) public void testPreconditions1() { final double lo = -1; final double hi = 2; final UnivariateFunction f = new Logit(lo, hi); f.value(lo - 1); } @Test(expected=OutOfRangeException.class) public void testPreconditions2() { final double lo = -1; final double hi = 2; final UnivariateFunction f = new Logit(lo, hi); f.value(hi + 1); } @Test public void testSomeValues() { final double lo = 1; final double hi = 2; final UnivariateFunction f = new Logit(lo, hi); Assert.assertEquals(Double.NEGATIVE_INFINITY, f.value(1), EPS); Assert.assertEquals(Double.POSITIVE_INFINITY, f.value(2), EPS); Assert.assertEquals(0, f.value(1.5), EPS); } @Test public void testDerivative() { final double lo = 1; final double hi = 2; final Logit f = new Logit(lo, hi); final DerivativeStructure f15 = f.value(new DerivativeStructure(1, 1, 0, 1.5)); Assert.assertEquals(4, f15.getPartialDerivative(1), EPS); } @Test public void testDerivativeLargeArguments() { final Logit f = new Logit(1, 2); for (double arg : new double[] { Double.NEGATIVE_INFINITY, -Double.MAX_VALUE, -1e155, 1e155, Double.MAX_VALUE, Double.POSITIVE_INFINITY }) { try { f.value(new DerivativeStructure(1, 1, 0, arg)); Assert.fail("an exception should have been thrown"); } catch (OutOfRangeException ore) { // expected } catch (Exception e) { Assert.fail("wrong exception caught: " + e.getMessage()); } } } @Test public void testDerivativesHighOrder() { DerivativeStructure l = new Logit(1, 3).value(new DerivativeStructure(1, 5, 0, 1.2)); Assert.assertEquals(-2.1972245773362193828, l.getPartialDerivative(0), 1.0e-16); Assert.assertEquals(5.5555555555555555555, l.getPartialDerivative(1), 9.0e-16); Assert.assertEquals(-24.691358024691358025, l.getPartialDerivative(2), 2.0e-14); Assert.assertEquals(250.34293552812071331, l.getPartialDerivative(3), 2.0e-13); Assert.assertEquals(-3749.4284407864654778, l.getPartialDerivative(4), 4.0e-12); Assert.assertEquals(75001.270131585632282, l.getPartialDerivative(5), 8.0e-11); } @Test(expected=NullArgumentException.class) public void testParametricUsage1() { final Logit.Parametric g = new Logit.Parametric(); g.value(0, null); } @Test(expected=DimensionMismatchException.class) public void testParametricUsage2() { final Logit.Parametric g = new Logit.Parametric(); g.value(0, new double[] {0}); } @Test(expected=NullArgumentException.class) public void testParametricUsage3() { final Logit.Parametric g = new Logit.Parametric(); g.gradient(0, null); } @Test(expected=DimensionMismatchException.class) public void testParametricUsage4() { final Logit.Parametric g = new Logit.Parametric(); g.gradient(0, new double[] {0}); } @Test(expected=OutOfRangeException.class) public void testParametricUsage5() { final Logit.Parametric g = new Logit.Parametric(); g.value(-1, new double[] {0, 1}); } @Test(expected=OutOfRangeException.class) public void testParametricUsage6() { final Logit.Parametric g = new Logit.Parametric(); g.value(2, new double[] {0, 1}); } @Test public void testParametricValue() { final double lo = 2; final double hi = 3; final Logit f = new Logit(lo, hi); final Logit.Parametric g = new Logit.Parametric(); Assert.assertEquals(f.value(2), g.value(2, new double[] {lo, hi}), 0); Assert.assertEquals(f.value(2.34567), g.value(2.34567, new double[] {lo, hi}), 0); Assert.assertEquals(f.value(3), g.value(3, new double[] {lo, hi}), 0); } @Test public void testValueWithInverseFunction() { final double lo = 2; final double hi = 3; final Logit f = new Logit(lo, hi); final Sigmoid g = new Sigmoid(lo, hi); RandomGenerator random = new Well1024a(0x49914cdd9f0b8db5l); final UnivariateDifferentiableFunction id = FunctionUtils.compose((UnivariateDifferentiableFunction) g, (UnivariateDifferentiableFunction) f); for (int i = 0; i < 10; i++) { final double x = lo + random.nextDouble() * (hi - lo); Assert.assertEquals(x, id.value(new DerivativeStructure(1, 1, 0, x)).getValue(), EPS); } Assert.assertEquals(lo, id.value(new DerivativeStructure(1, 1, 0, lo)).getValue(), EPS); Assert.assertEquals(hi, id.value(new DerivativeStructure(1, 1, 0, hi)).getValue(), EPS); } @Test public void testDerivativesWithInverseFunction() { double[] epsilon = new double[] { 1.0e-20, 4.0e-16, 3.0e-15, 2.0e-11, 3.0e-9, 1.0e-6 }; final double lo = 2; final double hi = 3; final Logit f = new Logit(lo, hi); final Sigmoid g = new Sigmoid(lo, hi); RandomGenerator random = new Well1024a(0x96885e9c1f81cea5l); final UnivariateDifferentiableFunction id = FunctionUtils.compose((UnivariateDifferentiableFunction) g, (UnivariateDifferentiableFunction) f); for (int maxOrder = 0; maxOrder < 6; ++maxOrder) { double max = 0; for (int i = 0; i < 10; i++) { final double x = lo + random.nextDouble() * (hi - lo); final DerivativeStructure dsX = new DerivativeStructure(1, maxOrder, 0, x); max = FastMath.max(max, FastMath.abs(dsX.getPartialDerivative(maxOrder) - id.value(dsX).getPartialDerivative(maxOrder))); Assert.assertEquals(dsX.getPartialDerivative(maxOrder), id.value(dsX).getPartialDerivative(maxOrder), epsilon[maxOrder]); } // each function evaluates correctly near boundaries, // but combination leads to NaN as some intermediate point is infinite final DerivativeStructure dsLo = new DerivativeStructure(1, maxOrder, 0, lo); if (maxOrder == 0) { Assert.assertTrue(Double.isInfinite(f.value(dsLo).getPartialDerivative(maxOrder))); Assert.assertEquals(lo, id.value(dsLo).getPartialDerivative(maxOrder), epsilon[maxOrder]); } else if (maxOrder == 1) { Assert.assertTrue(Double.isInfinite(f.value(dsLo).getPartialDerivative(maxOrder))); Assert.assertTrue(Double.isNaN(id.value(dsLo).getPartialDerivative(maxOrder))); } else { Assert.assertTrue(Double.isNaN(f.value(dsLo).getPartialDerivative(maxOrder))); Assert.assertTrue(Double.isNaN(id.value(dsLo).getPartialDerivative(maxOrder))); } final DerivativeStructure dsHi = new DerivativeStructure(1, maxOrder, 0, hi); if (maxOrder == 0) { Assert.assertTrue(Double.isInfinite(f.value(dsHi).getPartialDerivative(maxOrder))); Assert.assertEquals(hi, id.value(dsHi).getPartialDerivative(maxOrder), epsilon[maxOrder]); } else if (maxOrder == 1) { Assert.assertTrue(Double.isInfinite(f.value(dsHi).getPartialDerivative(maxOrder))); Assert.assertTrue(Double.isNaN(id.value(dsHi).getPartialDerivative(maxOrder))); } else { Assert.assertTrue(Double.isNaN(f.value(dsHi).getPartialDerivative(maxOrder))); Assert.assertTrue(Double.isNaN(id.value(dsHi).getPartialDerivative(maxOrder))); } } } } commons-math3-3.2-src/src/test/java/org/apache/commons/math3/analysis/function/SincTest.java100644 1750 1750 11270 12126627670 30650 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.analysis.function; import org.apache.commons.math3.analysis.UnivariateFunction; import org.apache.commons.math3.analysis.differentiation.DerivativeStructure; import org.apache.commons.math3.dfp.Dfp; import org.apache.commons.math3.dfp.DfpField; import org.apache.commons.math3.dfp.DfpMath; import org.apache.commons.math3.util.FastMath; import org.junit.Assert; import org.junit.Test; public class SincTest { @Test public void testShortcut() { final Sinc s = new Sinc(); final UnivariateFunction f = new UnivariateFunction() { public double value(double x) { Dfp dfpX = new DfpField(25).newDfp(x); return DfpMath.sin(dfpX).divide(dfpX).toDouble(); } }; for (double x = 1e-30; x < 1e10; x *= 2) { final double fX = f.value(x); final double sX = s.value(x); Assert.assertEquals("x=" + x, fX, sX, 2.0e-16); } } @Test public void testCrossings() { final Sinc s = new Sinc(true); final int numCrossings = 1000; final double tol = 2e-16; for (int i = 1; i <= numCrossings; i++) { Assert.assertEquals("i=" + i, 0, s.value(i), tol); } } @Test public void testZero() { final Sinc s = new Sinc(); Assert.assertEquals(1d, s.value(0), 0); } @Test public void testEuler() { final Sinc s = new Sinc(); final double x = 123456.789; double prod = 1; double xOverPow2 = x / 2; while (xOverPow2 > 0) { prod *= FastMath.cos(xOverPow2); xOverPow2 /= 2; } Assert.assertEquals(prod, s.value(x), 1e-13); } @Test public void testDerivativeZero() { final DerivativeStructure s0 = new Sinc(true).value(new DerivativeStructure(1, 1, 0, 0.0)); Assert.assertEquals(0, s0.getPartialDerivative(1), 0); } @Test public void testDerivatives1Dot2Unnormalized() { DerivativeStructure s = new Sinc(false).value(new DerivativeStructure(1, 5, 0, 1.2)); Assert.assertEquals( 0.77669923830602195806, s.getPartialDerivative(0), 1.0e-16); Assert.assertEquals(-0.34528456985779031701, s.getPartialDerivative(1), 1.0e-16); Assert.assertEquals(-0.2012249552097047631, s.getPartialDerivative(2), 1.0e-16); Assert.assertEquals( 0.2010975926270339262, s.getPartialDerivative(3), 4.0e-16); Assert.assertEquals( 0.106373929549242204, s.getPartialDerivative(4), 1.0e-15); Assert.assertEquals(-0.1412599110579478695, s.getPartialDerivative(5), 3.0e-15); } @Test public void testDerivatives1Dot2Normalized() { DerivativeStructure s = new Sinc(true).value(new DerivativeStructure(1, 5, 0, 1.2)); Assert.assertEquals(-0.15591488063143983888, s.getPartialDerivative(0), 6.0e-17); Assert.assertEquals(-0.54425176145292298767, s.getPartialDerivative(1), 2.0e-16); Assert.assertEquals(2.4459044611635856107, s.getPartialDerivative(2), 9.0e-16); Assert.assertEquals(0.5391369206235909586, s.getPartialDerivative(3), 7.0e-16); Assert.assertEquals(-16.984649869728849865, s.getPartialDerivative(4), 8.0e-15); Assert.assertEquals(5.0980327462666316586, s.getPartialDerivative(5), 9.0e-15); } @Test public void testDerivativeShortcut() { final Sinc sinc = new Sinc(); final UnivariateFunction f = new UnivariateFunction() { public double value(double x) { Dfp dfpX = new DfpField(25).newDfp(x); return DfpMath.cos(dfpX).subtract(DfpMath.sin(dfpX).divide(dfpX)).divide(dfpX).toDouble(); } }; for (double x = 1e-30; x < 1e10; x *= 2) { final double fX = f.value(x); final DerivativeStructure sX = sinc.value(new DerivativeStructure(1, 1, 0, x)); Assert.assertEquals("x=" + x, fX, sX.getPartialDerivative(1), 3.0e-13); } } } ././@LongLink100644 0 0 153 12126630646 10260 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/analysis/function/HarmonicOscillatorTest.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/analysis/function/HarmonicOscillatorTes100644 1750 1750 12562 12126627670 32451 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.analysis.function; import org.apache.commons.math3.analysis.UnivariateFunction; import org.apache.commons.math3.analysis.differentiation.DerivativeStructure; import org.apache.commons.math3.exception.DimensionMismatchException; import org.apache.commons.math3.exception.NullArgumentException; import org.apache.commons.math3.util.FastMath; import org.apache.commons.math3.util.Precision; import org.junit.Assert; import org.junit.Test; /** * Test for class {@link HarmonicOscillator}. */ public class HarmonicOscillatorTest { private final double EPS = Math.ulp(1d); @Test public void testSomeValues() { final double a = -1.2; final double w = 0.34; final double p = 5.6; final UnivariateFunction f = new HarmonicOscillator(a, w, p); final double d = 0.12345; for (int i = 0; i < 10; i++) { final double v = i * d; Assert.assertEquals(a * FastMath.cos(w * v + p), f.value(v), 0); } } @Test public void testDerivative() { final double a = -1.2; final double w = 0.34; final double p = 5.6; final HarmonicOscillator f = new HarmonicOscillator(a, w, p); for (int maxOrder = 0; maxOrder < 6; ++maxOrder) { final double d = 0.12345; for (int i = 0; i < 10; i++) { final double v = i * d; final DerivativeStructure h = f.value(new DerivativeStructure(1, maxOrder, 0, v)); for (int k = 0; k <= maxOrder; ++k) { final double trigo; switch (k % 4) { case 0: trigo = +FastMath.cos(w * v + p); break; case 1: trigo = -FastMath.sin(w * v + p); break; case 2: trigo = -FastMath.cos(w * v + p); break; default: trigo = +FastMath.sin(w * v + p); break; } Assert.assertEquals(a * FastMath.pow(w, k) * trigo, h.getPartialDerivative(k), Precision.EPSILON); } } } } @Test(expected=NullArgumentException.class) public void testParametricUsage1() { final HarmonicOscillator.Parametric g = new HarmonicOscillator.Parametric(); g.value(0, null); } @Test(expected=DimensionMismatchException.class) public void testParametricUsage2() { final HarmonicOscillator.Parametric g = new HarmonicOscillator.Parametric(); g.value(0, new double[] {0}); } @Test(expected=NullArgumentException.class) public void testParametricUsage3() { final HarmonicOscillator.Parametric g = new HarmonicOscillator.Parametric(); g.gradient(0, null); } @Test(expected=DimensionMismatchException.class) public void testParametricUsage4() { final HarmonicOscillator.Parametric g = new HarmonicOscillator.Parametric(); g.gradient(0, new double[] {0}); } @Test public void testParametricValue() { final double amplitude = 2; final double omega = 3; final double phase = 4; final HarmonicOscillator f = new HarmonicOscillator(amplitude, omega, phase); final HarmonicOscillator.Parametric g = new HarmonicOscillator.Parametric(); Assert.assertEquals(f.value(-1), g.value(-1, new double[] {amplitude, omega, phase}), 0); Assert.assertEquals(f.value(0), g.value(0, new double[] {amplitude, omega, phase}), 0); Assert.assertEquals(f.value(2), g.value(2, new double[] {amplitude, omega, phase}), 0); } @Test public void testParametricGradient() { final double amplitude = 2; final double omega = 3; final double phase = 4; final HarmonicOscillator.Parametric f = new HarmonicOscillator.Parametric(); final double x = 1; final double[] grad = f.gradient(1, new double[] {amplitude, omega, phase}); final double xTimesOmegaPlusPhase = omega * x + phase; final double a = FastMath.cos(xTimesOmegaPlusPhase); Assert.assertEquals(a, grad[0], EPS); final double w = -amplitude * x * FastMath.sin(xTimesOmegaPlusPhase); Assert.assertEquals(w, grad[1], EPS); final double p = -amplitude * FastMath.sin(xTimesOmegaPlusPhase); Assert.assertEquals(p, grad[2], EPS); } } commons-math3-3.2-src/src/test/java/org/apache/commons/math3/analysis/function/SigmoidTest.java100644 1750 1750 11346 12126627670 31353 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.analysis.function; import org.apache.commons.math3.analysis.UnivariateFunction; import org.apache.commons.math3.analysis.differentiation.DerivativeStructure; import org.apache.commons.math3.exception.NullArgumentException; import org.apache.commons.math3.exception.DimensionMismatchException; import org.junit.Assert; import org.junit.Test; /** * Test for class {@link Sigmoid}. */ public class SigmoidTest { private final double EPS = Math.ulp(1d); @Test public void testSomeValues() { final UnivariateFunction f = new Sigmoid(); Assert.assertEquals(0.5, f.value(0), EPS); Assert.assertEquals(0, f.value(Double.NEGATIVE_INFINITY), EPS); Assert.assertEquals(1, f.value(Double.POSITIVE_INFINITY), EPS); } @Test public void testDerivative() { final Sigmoid f = new Sigmoid(); final DerivativeStructure f0 = f.value(new DerivativeStructure(1, 1, 0, 0.0)); Assert.assertEquals(0.25, f0.getPartialDerivative(1), 0); } @Test public void testDerivativesHighOrder() { DerivativeStructure s = new Sigmoid(1, 3).value(new DerivativeStructure(1, 5, 0, 1.2)); Assert.assertEquals(2.5370495669980352859, s.getPartialDerivative(0), 5.0e-16); Assert.assertEquals(0.35578888129361140441, s.getPartialDerivative(1), 6.0e-17); Assert.assertEquals(-0.19107626464144938116, s.getPartialDerivative(2), 6.0e-17); Assert.assertEquals(-0.02396830286286711696, s.getPartialDerivative(3), 4.0e-17); Assert.assertEquals(0.21682059798981049049, s.getPartialDerivative(4), 3.0e-17); Assert.assertEquals(-0.19186320234632658055, s.getPartialDerivative(5), 2.0e-16); } @Test public void testDerivativeLargeArguments() { final Sigmoid f = new Sigmoid(1, 2); Assert.assertEquals(0, f.value(new DerivativeStructure(1, 1, 0, Double.NEGATIVE_INFINITY)).getPartialDerivative(1), 0); Assert.assertEquals(0, f.value(new DerivativeStructure(1, 1, 0, -Double.MAX_VALUE)).getPartialDerivative(1), 0); Assert.assertEquals(0, f.value(new DerivativeStructure(1, 1, 0, -1e50)).getPartialDerivative(1), 0); Assert.assertEquals(0, f.value(new DerivativeStructure(1, 1, 0, -1e3)).getPartialDerivative(1), 0); Assert.assertEquals(0, f.value(new DerivativeStructure(1, 1, 0, 1e3)).getPartialDerivative(1), 0); Assert.assertEquals(0, f.value(new DerivativeStructure(1, 1, 0, 1e50)).getPartialDerivative(1), 0); Assert.assertEquals(0, f.value(new DerivativeStructure(1, 1, 0, Double.MAX_VALUE)).getPartialDerivative(1), 0); Assert.assertEquals(0, f.value(new DerivativeStructure(1, 1, 0, Double.POSITIVE_INFINITY)).getPartialDerivative(1), 0); } @Test(expected=NullArgumentException.class) public void testParametricUsage1() { final Sigmoid.Parametric g = new Sigmoid.Parametric(); g.value(0, null); } @Test(expected=DimensionMismatchException.class) public void testParametricUsage2() { final Sigmoid.Parametric g = new Sigmoid.Parametric(); g.value(0, new double[] {0}); } @Test(expected=NullArgumentException.class) public void testParametricUsage3() { final Sigmoid.Parametric g = new Sigmoid.Parametric(); g.gradient(0, null); } @Test(expected=DimensionMismatchException.class) public void testParametricUsage4() { final Sigmoid.Parametric g = new Sigmoid.Parametric(); g.gradient(0, new double[] {0}); } @Test public void testParametricValue() { final double lo = 2; final double hi = 3; final Sigmoid f = new Sigmoid(lo, hi); final Sigmoid.Parametric g = new Sigmoid.Parametric(); Assert.assertEquals(f.value(-1), g.value(-1, new double[] {lo, hi}), 0); Assert.assertEquals(f.value(0), g.value(0, new double[] {lo, hi}), 0); Assert.assertEquals(f.value(2), g.value(2, new double[] {lo, hi}), 0); } } commons-math3-3.2-src/src/test/java/org/apache/commons/math3/analysis/function/LogisticTest.java100644 1750 1750 15235 12126627670 31536 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.analysis.function; import org.apache.commons.math3.analysis.UnivariateFunction; import org.apache.commons.math3.analysis.differentiation.DerivativeStructure; import org.apache.commons.math3.exception.NotStrictlyPositiveException; import org.apache.commons.math3.exception.NullArgumentException; import org.apache.commons.math3.exception.DimensionMismatchException; import org.apache.commons.math3.util.FastMath; import org.junit.Assert; import org.junit.Test; /** * Test for class {@link Logistic}. */ public class LogisticTest { private final double EPS = Math.ulp(1d); @Test(expected=NotStrictlyPositiveException.class) public void testPreconditions1() { new Logistic(1, 0, 1, 1, 0, -1); } @Test(expected=NotStrictlyPositiveException.class) public void testPreconditions2() { new Logistic(1, 0, 1, 1, 0, 0); } @Test public void testCompareSigmoid() { final UnivariateFunction sig = new Sigmoid(); final UnivariateFunction sigL = new Logistic(1, 0, 1, 1, 0, 1); final double min = -2; final double max = 2; final int n = 100; final double delta = (max - min) / n; for (int i = 0; i < n; i++) { final double x = min + i * delta; Assert.assertEquals("x=" + x, sig.value(x), sigL.value(x), EPS); } } @Test public void testSomeValues() { final double k = 4; final double m = 5; final double b = 2; final double q = 3; final double a = -1; final double n = 2; final UnivariateFunction f = new Logistic(k, m, b, q, a, n); double x; x = m; Assert.assertEquals("x=" + x, a + (k - a) / FastMath.sqrt(1 + q), f.value(x), EPS); x = Double.NEGATIVE_INFINITY; Assert.assertEquals("x=" + x, a, f.value(x), EPS); x = Double.POSITIVE_INFINITY; Assert.assertEquals("x=" + x, k, f.value(x), EPS); } @Test public void testCompareDerivativeSigmoid() { final double k = 3; final double a = 2; final Logistic f = new Logistic(k, 0, 1, 1, a, 1); final Sigmoid g = new Sigmoid(a, k); final double min = -10; final double max = 10; final double n = 20; final double delta = (max - min) / n; for (int i = 0; i < n; i++) { final DerivativeStructure x = new DerivativeStructure(1, 5, 0, min + i * delta); for (int order = 0; order <= x.getOrder(); ++order) { Assert.assertEquals("x=" + x.getValue(), g.value(x).getPartialDerivative(order), f.value(x).getPartialDerivative(order), 3.0e-15); } } } @Test(expected=NullArgumentException.class) public void testParametricUsage1() { final Logistic.Parametric g = new Logistic.Parametric(); g.value(0, null); } @Test(expected=DimensionMismatchException.class) public void testParametricUsage2() { final Logistic.Parametric g = new Logistic.Parametric(); g.value(0, new double[] {0}); } @Test(expected=NullArgumentException.class) public void testParametricUsage3() { final Logistic.Parametric g = new Logistic.Parametric(); g.gradient(0, null); } @Test(expected=DimensionMismatchException.class) public void testParametricUsage4() { final Logistic.Parametric g = new Logistic.Parametric(); g.gradient(0, new double[] {0}); } @Test(expected=NotStrictlyPositiveException.class) public void testParametricUsage5() { final Logistic.Parametric g = new Logistic.Parametric(); g.value(0, new double[] {1, 0, 1, 1, 0 ,0}); } @Test(expected=NotStrictlyPositiveException.class) public void testParametricUsage6() { final Logistic.Parametric g = new Logistic.Parametric(); g.gradient(0, new double[] {1, 0, 1, 1, 0 ,0}); } @Test public void testGradientComponent0Component4() { final double k = 3; final double a = 2; final Logistic.Parametric f = new Logistic.Parametric(); // Compare using the "Sigmoid" function. final Sigmoid.Parametric g = new Sigmoid.Parametric(); final double x = 0.12345; final double[] gf = f.gradient(x, new double[] {k, 0, 1, 1, a, 1}); final double[] gg = g.gradient(x, new double[] {a, k}); Assert.assertEquals(gg[0], gf[4], EPS); Assert.assertEquals(gg[1], gf[0], EPS); } @Test public void testGradientComponent5() { final double m = 1.2; final double k = 3.4; final double a = 2.3; final double q = 0.567; final double b = -FastMath.log(q); final double n = 3.4; final Logistic.Parametric f = new Logistic.Parametric(); final double x = m - 1; final double qExp1 = 2; final double[] gf = f.gradient(x, new double[] {k, m, b, q, a, n}); Assert.assertEquals((k - a) * FastMath.log(qExp1) / (n * n * FastMath.pow(qExp1, 1 / n)), gf[5], EPS); } @Test public void testGradientComponent1Component2Component3() { final double m = 1.2; final double k = 3.4; final double a = 2.3; final double b = 0.567; final double q = 1 / FastMath.exp(b * m); final double n = 3.4; final Logistic.Parametric f = new Logistic.Parametric(); final double x = 0; final double qExp1 = 2; final double[] gf = f.gradient(x, new double[] {k, m, b, q, a, n}); final double factor = (a - k) / (n * FastMath.pow(qExp1, 1 / n + 1)); Assert.assertEquals(factor * b, gf[1], EPS); Assert.assertEquals(factor * m, gf[2], EPS); Assert.assertEquals(factor / q, gf[3], EPS); } } ././@LongLink100644 0 0 145 12126630646 10261 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/analysis/function/StepFunctionTest.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/analysis/function/StepFunctionTest.java100644 1750 1750 10613 12126627670 32375 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.analysis.function; import org.apache.commons.math3.analysis.UnivariateFunction; import org.apache.commons.math3.exception.DimensionMismatchException; import org.apache.commons.math3.exception.NonMonotonicSequenceException; import org.apache.commons.math3.exception.NullArgumentException; import org.apache.commons.math3.exception.NoDataException; import org.junit.Assert; import org.junit.Test; /** * Test for class {@link StepFunction}. */ public class StepFunctionTest { private final double EPS = Math.ulp(1d); @Test(expected=NullArgumentException.class) public void testPreconditions1() { new StepFunction(null, new double[] {0, -1, -2}); } @Test(expected=NullArgumentException.class) public void testPreconditions2() { new StepFunction(new double[] {0, 1}, null); } @Test(expected=NoDataException.class) public void testPreconditions3() { new StepFunction(new double[] {0}, new double[] {}); } @Test(expected=NoDataException.class) public void testPreconditions4() { new StepFunction(new double[] {}, new double[] {0}); } @Test(expected=DimensionMismatchException.class) public void testPreconditions5() { new StepFunction(new double[] {0, 1}, new double[] {0, -1, -2}); } @Test(expected=NonMonotonicSequenceException.class) public void testPreconditions6() { new StepFunction(new double[] {1, 0, 1}, new double[] {0, -1, -2}); } @Test public void testSomeValues() { final double[] x = { -2, -0.5, 0, 1.9, 7.4, 21.3 }; final double[] y = { 4, -1, -5.5, 0.4, 5.8, 51.2 }; final UnivariateFunction f = new StepFunction(x, y); Assert.assertEquals(4, f.value(Double.NEGATIVE_INFINITY), EPS); Assert.assertEquals(4, f.value(-10), EPS); Assert.assertEquals(-1, f.value(-0.4), EPS); Assert.assertEquals(-5.5, f.value(0), EPS); Assert.assertEquals(0.4, f.value(2), EPS); Assert.assertEquals(5.8, f.value(10), EPS); Assert.assertEquals(51.2, f.value(30), EPS); Assert.assertEquals(51.2, f.value(Double.POSITIVE_INFINITY), EPS); } @Test public void testEndpointBehavior() { final double[] x = {0, 1, 2, 3}; final double[] xp = {-8, 1, 2, 3}; final double[] y = {1, 2, 3, 4}; final UnivariateFunction f = new StepFunction(x, y); final UnivariateFunction fp = new StepFunction(xp, y); Assert.assertEquals(f.value(-8), fp.value(-8), EPS); Assert.assertEquals(f.value(-10), fp.value(-10), EPS); Assert.assertEquals(f.value(0), fp.value(0), EPS); Assert.assertEquals(f.value(0.5), fp.value(0.5), EPS); for (int i = 0; i < x.length; i++) { Assert.assertEquals(y[i], f.value(x[i]), EPS); if (i > 0) { Assert.assertEquals(y[i - 1], f.value(x[i] - 0.5), EPS); } else { Assert.assertEquals(y[0], f.value(x[i] - 0.5), EPS); } } } @Test public void testHeaviside() { final UnivariateFunction h = new StepFunction(new double[] {-1, 0}, new double[] {0, 1}); Assert.assertEquals(0, h.value(Double.NEGATIVE_INFINITY), 0); Assert.assertEquals(0, h.value(-Double.MAX_VALUE), 0); Assert.assertEquals(0, h.value(-2), 0); Assert.assertEquals(0, h.value(-Double.MIN_VALUE), 0); Assert.assertEquals(1, h.value(0), 0); Assert.assertEquals(1, h.value(2), 0); Assert.assertEquals(1, h.value(Double.POSITIVE_INFINITY), 0); } } commons-math3-3.2-src/src/test/java/org/apache/commons/math3/analysis/XMinus5Function.java100644 1750 1750 2634 12126627670 30271 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.analysis; import org.apache.commons.math3.analysis.differentiation.DerivativeStructure; import org.apache.commons.math3.analysis.differentiation.UnivariateDifferentiableFunction; /** * Auxiliary class for testing solvers. * * @version $Id$ */ public class XMinus5Function implements UnivariateDifferentiableFunction { /* Evaluate x - 5 fuction. * @see org.apache.commons.math3.UnivariateFunction#value(double) */ public double value(double x) { return x - 5; } public DerivativeStructure value(DerivativeStructure t) { return t.subtract(5); } } ././@LongLink100644 0 0 156 12126630646 10263 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/analysis/polynomials/PolynomialFunctionTest.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/analysis/polynomials/PolynomialFunction100644 1750 1750 23563 12126627670 32556 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.analysis.polynomials; import org.apache.commons.math3.TestUtils; import org.apache.commons.math3.util.FastMath; import org.junit.Test; import org.junit.Assert; /** * Tests the PolynomialFunction implementation of a UnivariateFunction. * * @version $Id: PolynomialFunctionTest.java 1244107 2012-02-14 16:17:55Z erans $ */ public final class PolynomialFunctionTest { /** Error tolerance for tests */ protected double tolerance = 1e-12; /** * tests the value of a constant polynomial. * *

              value of this is 2.5 everywhere.

              */ @Test public void testConstants() { double[] c = { 2.5 }; PolynomialFunction f = new PolynomialFunction(c); // verify that we are equal to c[0] at several (nonsymmetric) places Assert.assertEquals(f.value(0), c[0], tolerance); Assert.assertEquals(f.value(-1), c[0], tolerance); Assert.assertEquals(f.value(-123.5), c[0], tolerance); Assert.assertEquals(f.value(3), c[0], tolerance); Assert.assertEquals(f.value(456.89), c[0], tolerance); Assert.assertEquals(f.degree(), 0); Assert.assertEquals(f.derivative().value(0), 0, tolerance); Assert.assertEquals(f.polynomialDerivative().derivative().value(0), 0, tolerance); } /** * tests the value of a linear polynomial. * *

              This will test the function f(x) = 3*x - 1.5

              *

              This will have the values * f(0) = -1.5, f(-1) = -4.5, f(-2.5) = -9, * f(0.5) = 0, f(1.5) = 3 and f(3) = 7.5 *

              */ @Test public void testLinear() { double[] c = { -1.5, 3 }; PolynomialFunction f = new PolynomialFunction(c); // verify that we are equal to c[0] when x=0 Assert.assertEquals(f.value(0), c[0], tolerance); // now check a few other places Assert.assertEquals(-4.5, f.value(-1), tolerance); Assert.assertEquals(-9, f.value(-2.5), tolerance); Assert.assertEquals(0, f.value(0.5), tolerance); Assert.assertEquals(3, f.value(1.5), tolerance); Assert.assertEquals(7.5, f.value(3), tolerance); Assert.assertEquals(f.degree(), 1); Assert.assertEquals(f.polynomialDerivative().derivative().value(0), 0, tolerance); } /** * Tests a second order polynomial. *

              This will test the function f(x) = 2x^2 - 3x -2 = (2x+1)(x-2)

              */ @Test public void testQuadratic() { double[] c = { -2, -3, 2 }; PolynomialFunction f = new PolynomialFunction(c); // verify that we are equal to c[0] when x=0 Assert.assertEquals(f.value(0), c[0], tolerance); // now check a few other places Assert.assertEquals(0, f.value(-0.5), tolerance); Assert.assertEquals(0, f.value(2), tolerance); Assert.assertEquals(-2, f.value(1.5), tolerance); Assert.assertEquals(7, f.value(-1.5), tolerance); Assert.assertEquals(265.5312, f.value(12.34), tolerance); } /** * This will test the quintic function * f(x) = x^2(x-5)(x+3)(x-1) = x^5 - 3x^4 -13x^3 + 15x^2

              */ @Test public void testQuintic() { double[] c = { 0, 0, 15, -13, -3, 1 }; PolynomialFunction f = new PolynomialFunction(c); // verify that we are equal to c[0] when x=0 Assert.assertEquals(f.value(0), c[0], tolerance); // now check a few other places Assert.assertEquals(0, f.value(5), tolerance); Assert.assertEquals(0, f.value(1), tolerance); Assert.assertEquals(0, f.value(-3), tolerance); Assert.assertEquals(54.84375, f.value(-1.5), tolerance); Assert.assertEquals(-8.06637, f.value(1.3), tolerance); Assert.assertEquals(f.degree(), 5); } /** * tests the firstDerivative function by comparison * *

              This will test the functions * f(x) = x^3 - 2x^2 + 6x + 3, g(x) = 3x^2 - 4x + 6 * and h(x) = 6x - 4 */ @Test public void testfirstDerivativeComparison() { double[] f_coeff = { 3, 6, -2, 1 }; double[] g_coeff = { 6, -4, 3 }; double[] h_coeff = { -4, 6 }; PolynomialFunction f = new PolynomialFunction(f_coeff); PolynomialFunction g = new PolynomialFunction(g_coeff); PolynomialFunction h = new PolynomialFunction(h_coeff); // compare f' = g Assert.assertEquals(f.derivative().value(0), g.value(0), tolerance); Assert.assertEquals(f.derivative().value(1), g.value(1), tolerance); Assert.assertEquals(f.derivative().value(100), g.value(100), tolerance); Assert.assertEquals(f.derivative().value(4.1), g.value(4.1), tolerance); Assert.assertEquals(f.derivative().value(-3.25), g.value(-3.25), tolerance); // compare g' = h Assert.assertEquals(g.derivative().value(FastMath.PI), h.value(FastMath.PI), tolerance); Assert.assertEquals(g.derivative().value(FastMath.E), h.value(FastMath.E), tolerance); } @Test public void testString() { PolynomialFunction p = new PolynomialFunction(new double[] { -5, 3, 1 }); checkPolynomial(p, "-5 + 3 x + x^2"); checkPolynomial(new PolynomialFunction(new double[] { 0, -2, 3 }), "-2 x + 3 x^2"); checkPolynomial(new PolynomialFunction(new double[] { 1, -2, 3 }), "1 - 2 x + 3 x^2"); checkPolynomial(new PolynomialFunction(new double[] { 0, 2, 3 }), "2 x + 3 x^2"); checkPolynomial(new PolynomialFunction(new double[] { 1, 2, 3 }), "1 + 2 x + 3 x^2"); checkPolynomial(new PolynomialFunction(new double[] { 1, 0, 3 }), "1 + 3 x^2"); checkPolynomial(new PolynomialFunction(new double[] { 0 }), "0"); } @Test public void testAddition() { PolynomialFunction p1 = new PolynomialFunction(new double[] { -2, 1 }); PolynomialFunction p2 = new PolynomialFunction(new double[] { 2, -1, 0 }); checkNullPolynomial(p1.add(p2)); p2 = p1.add(p1); checkPolynomial(p2, "-4 + 2 x"); p1 = new PolynomialFunction(new double[] { 1, -4, 2 }); p2 = new PolynomialFunction(new double[] { -1, 3, -2 }); p1 = p1.add(p2); Assert.assertEquals(1, p1.degree()); checkPolynomial(p1, "-x"); } @Test public void testSubtraction() { PolynomialFunction p1 = new PolynomialFunction(new double[] { -2, 1 }); checkNullPolynomial(p1.subtract(p1)); PolynomialFunction p2 = new PolynomialFunction(new double[] { -2, 6 }); p2 = p2.subtract(p1); checkPolynomial(p2, "5 x"); p1 = new PolynomialFunction(new double[] { 1, -4, 2 }); p2 = new PolynomialFunction(new double[] { -1, 3, 2 }); p1 = p1.subtract(p2); Assert.assertEquals(1, p1.degree()); checkPolynomial(p1, "2 - 7 x"); } @Test public void testMultiplication() { PolynomialFunction p1 = new PolynomialFunction(new double[] { -3, 2 }); PolynomialFunction p2 = new PolynomialFunction(new double[] { 3, 2, 1 }); checkPolynomial(p1.multiply(p2), "-9 + x^2 + 2 x^3"); p1 = new PolynomialFunction(new double[] { 0, 1 }); p2 = p1; for (int i = 2; i < 10; ++i) { p2 = p2.multiply(p1); checkPolynomial(p2, "x^" + i); } } @Test public void testSerial() { PolynomialFunction p2 = new PolynomialFunction(new double[] { 3, 2, 1 }); Assert.assertEquals(p2, TestUtils.serializeAndRecover(p2)); } /** * tests the firstDerivative function by comparison * *

              This will test the functions * f(x) = x^3 - 2x^2 + 6x + 3, g(x) = 3x^2 - 4x + 6 * and h(x) = 6x - 4 */ @Test public void testMath341() { double[] f_coeff = { 3, 6, -2, 1 }; double[] g_coeff = { 6, -4, 3 }; double[] h_coeff = { -4, 6 }; PolynomialFunction f = new PolynomialFunction(f_coeff); PolynomialFunction g = new PolynomialFunction(g_coeff); PolynomialFunction h = new PolynomialFunction(h_coeff); // compare f' = g Assert.assertEquals(f.derivative().value(0), g.value(0), tolerance); Assert.assertEquals(f.derivative().value(1), g.value(1), tolerance); Assert.assertEquals(f.derivative().value(100), g.value(100), tolerance); Assert.assertEquals(f.derivative().value(4.1), g.value(4.1), tolerance); Assert.assertEquals(f.derivative().value(-3.25), g.value(-3.25), tolerance); // compare g' = h Assert.assertEquals(g.derivative().value(FastMath.PI), h.value(FastMath.PI), tolerance); Assert.assertEquals(g.derivative().value(FastMath.E), h.value(FastMath.E), tolerance); } public void checkPolynomial(PolynomialFunction p, String reference) { Assert.assertEquals(reference, p.toString()); } private void checkNullPolynomial(PolynomialFunction p) { for (double coefficient : p.getCoefficients()) { Assert.assertEquals(0, coefficient, 1e-15); } } } ././@LongLink100644 0 0 154 12126630646 10261 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/analysis/polynomials/PolynomialsUtilsTest.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/analysis/polynomials/PolynomialsUtilsTe100644 1750 1750 41035 12126627670 32537 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.analysis.polynomials; import org.apache.commons.math3.analysis.UnivariateFunction; import org.apache.commons.math3.analysis.integration.IterativeLegendreGaussIntegrator; import org.apache.commons.math3.util.ArithmeticUtils; import org.apache.commons.math3.util.FastMath; import org.apache.commons.math3.util.Precision; import org.junit.Assert; import org.junit.Test; /** * Tests the PolynomialsUtils class. * * @version $Id: PolynomialsUtilsTest.java 1377244 2012-08-25 10:05:13Z luc $ */ public class PolynomialsUtilsTest { @Test public void testFirstChebyshevPolynomials() { checkPolynomial(PolynomialsUtils.createChebyshevPolynomial(3), "-3 x + 4 x^3"); checkPolynomial(PolynomialsUtils.createChebyshevPolynomial(2), "-1 + 2 x^2"); checkPolynomial(PolynomialsUtils.createChebyshevPolynomial(1), "x"); checkPolynomial(PolynomialsUtils.createChebyshevPolynomial(0), "1"); checkPolynomial(PolynomialsUtils.createChebyshevPolynomial(7), "-7 x + 56 x^3 - 112 x^5 + 64 x^7"); checkPolynomial(PolynomialsUtils.createChebyshevPolynomial(6), "-1 + 18 x^2 - 48 x^4 + 32 x^6"); checkPolynomial(PolynomialsUtils.createChebyshevPolynomial(5), "5 x - 20 x^3 + 16 x^5"); checkPolynomial(PolynomialsUtils.createChebyshevPolynomial(4), "1 - 8 x^2 + 8 x^4"); } @Test public void testChebyshevBounds() { for (int k = 0; k < 12; ++k) { PolynomialFunction Tk = PolynomialsUtils.createChebyshevPolynomial(k); for (double x = -1; x <= 1; x += 0.02) { Assert.assertTrue(k + " " + Tk.value(x), FastMath.abs(Tk.value(x)) < (1 + 1e-12)); } } } @Test public void testChebyshevDifferentials() { for (int k = 0; k < 12; ++k) { PolynomialFunction Tk0 = PolynomialsUtils.createChebyshevPolynomial(k); PolynomialFunction Tk1 = Tk0.polynomialDerivative(); PolynomialFunction Tk2 = Tk1.polynomialDerivative(); PolynomialFunction g0 = new PolynomialFunction(new double[] { k * k }); PolynomialFunction g1 = new PolynomialFunction(new double[] { 0, -1}); PolynomialFunction g2 = new PolynomialFunction(new double[] { 1, 0, -1 }); PolynomialFunction Tk0g0 = Tk0.multiply(g0); PolynomialFunction Tk1g1 = Tk1.multiply(g1); PolynomialFunction Tk2g2 = Tk2.multiply(g2); checkNullPolynomial(Tk0g0.add(Tk1g1.add(Tk2g2))); } } @Test public void testChebyshevOrthogonality() { UnivariateFunction weight = new UnivariateFunction() { public double value(double x) { return 1 / FastMath.sqrt(1 - x * x); } }; for (int i = 0; i < 10; ++i) { PolynomialFunction pi = PolynomialsUtils.createChebyshevPolynomial(i); for (int j = 0; j <= i; ++j) { PolynomialFunction pj = PolynomialsUtils.createChebyshevPolynomial(j); checkOrthogonality(pi, pj, weight, -0.9999, 0.9999, 1.5, 0.03); } } } @Test public void testFirstHermitePolynomials() { checkPolynomial(PolynomialsUtils.createHermitePolynomial(3), "-12 x + 8 x^3"); checkPolynomial(PolynomialsUtils.createHermitePolynomial(2), "-2 + 4 x^2"); checkPolynomial(PolynomialsUtils.createHermitePolynomial(1), "2 x"); checkPolynomial(PolynomialsUtils.createHermitePolynomial(0), "1"); checkPolynomial(PolynomialsUtils.createHermitePolynomial(7), "-1680 x + 3360 x^3 - 1344 x^5 + 128 x^7"); checkPolynomial(PolynomialsUtils.createHermitePolynomial(6), "-120 + 720 x^2 - 480 x^4 + 64 x^6"); checkPolynomial(PolynomialsUtils.createHermitePolynomial(5), "120 x - 160 x^3 + 32 x^5"); checkPolynomial(PolynomialsUtils.createHermitePolynomial(4), "12 - 48 x^2 + 16 x^4"); } @Test public void testHermiteDifferentials() { for (int k = 0; k < 12; ++k) { PolynomialFunction Hk0 = PolynomialsUtils.createHermitePolynomial(k); PolynomialFunction Hk1 = Hk0.polynomialDerivative(); PolynomialFunction Hk2 = Hk1.polynomialDerivative(); PolynomialFunction g0 = new PolynomialFunction(new double[] { 2 * k }); PolynomialFunction g1 = new PolynomialFunction(new double[] { 0, -2 }); PolynomialFunction g2 = new PolynomialFunction(new double[] { 1 }); PolynomialFunction Hk0g0 = Hk0.multiply(g0); PolynomialFunction Hk1g1 = Hk1.multiply(g1); PolynomialFunction Hk2g2 = Hk2.multiply(g2); checkNullPolynomial(Hk0g0.add(Hk1g1.add(Hk2g2))); } } @Test public void testHermiteOrthogonality() { UnivariateFunction weight = new UnivariateFunction() { public double value(double x) { return FastMath.exp(-x * x); } }; for (int i = 0; i < 10; ++i) { PolynomialFunction pi = PolynomialsUtils.createHermitePolynomial(i); for (int j = 0; j <= i; ++j) { PolynomialFunction pj = PolynomialsUtils.createHermitePolynomial(j); checkOrthogonality(pi, pj, weight, -50, 50, 1.5, 1.0e-8); } } } @Test public void testFirstLaguerrePolynomials() { checkPolynomial(PolynomialsUtils.createLaguerrePolynomial(3), 6l, "6 - 18 x + 9 x^2 - x^3"); checkPolynomial(PolynomialsUtils.createLaguerrePolynomial(2), 2l, "2 - 4 x + x^2"); checkPolynomial(PolynomialsUtils.createLaguerrePolynomial(1), 1l, "1 - x"); checkPolynomial(PolynomialsUtils.createLaguerrePolynomial(0), 1l, "1"); checkPolynomial(PolynomialsUtils.createLaguerrePolynomial(7), 5040l, "5040 - 35280 x + 52920 x^2 - 29400 x^3" + " + 7350 x^4 - 882 x^5 + 49 x^6 - x^7"); checkPolynomial(PolynomialsUtils.createLaguerrePolynomial(6), 720l, "720 - 4320 x + 5400 x^2 - 2400 x^3 + 450 x^4" + " - 36 x^5 + x^6"); checkPolynomial(PolynomialsUtils.createLaguerrePolynomial(5), 120l, "120 - 600 x + 600 x^2 - 200 x^3 + 25 x^4 - x^5"); checkPolynomial(PolynomialsUtils.createLaguerrePolynomial(4), 24l, "24 - 96 x + 72 x^2 - 16 x^3 + x^4"); } @Test public void testLaguerreDifferentials() { for (int k = 0; k < 12; ++k) { PolynomialFunction Lk0 = PolynomialsUtils.createLaguerrePolynomial(k); PolynomialFunction Lk1 = Lk0.polynomialDerivative(); PolynomialFunction Lk2 = Lk1.polynomialDerivative(); PolynomialFunction g0 = new PolynomialFunction(new double[] { k }); PolynomialFunction g1 = new PolynomialFunction(new double[] { 1, -1 }); PolynomialFunction g2 = new PolynomialFunction(new double[] { 0, 1 }); PolynomialFunction Lk0g0 = Lk0.multiply(g0); PolynomialFunction Lk1g1 = Lk1.multiply(g1); PolynomialFunction Lk2g2 = Lk2.multiply(g2); checkNullPolynomial(Lk0g0.add(Lk1g1.add(Lk2g2))); } } @Test public void testLaguerreOrthogonality() { UnivariateFunction weight = new UnivariateFunction() { public double value(double x) { return FastMath.exp(-x); } }; for (int i = 0; i < 10; ++i) { PolynomialFunction pi = PolynomialsUtils.createLaguerrePolynomial(i); for (int j = 0; j <= i; ++j) { PolynomialFunction pj = PolynomialsUtils.createLaguerrePolynomial(j); checkOrthogonality(pi, pj, weight, 0.0, 100.0, 0.99999, 1.0e-13); } } } @Test public void testFirstLegendrePolynomials() { checkPolynomial(PolynomialsUtils.createLegendrePolynomial(3), 2l, "-3 x + 5 x^3"); checkPolynomial(PolynomialsUtils.createLegendrePolynomial(2), 2l, "-1 + 3 x^2"); checkPolynomial(PolynomialsUtils.createLegendrePolynomial(1), 1l, "x"); checkPolynomial(PolynomialsUtils.createLegendrePolynomial(0), 1l, "1"); checkPolynomial(PolynomialsUtils.createLegendrePolynomial(7), 16l, "-35 x + 315 x^3 - 693 x^5 + 429 x^7"); checkPolynomial(PolynomialsUtils.createLegendrePolynomial(6), 16l, "-5 + 105 x^2 - 315 x^4 + 231 x^6"); checkPolynomial(PolynomialsUtils.createLegendrePolynomial(5), 8l, "15 x - 70 x^3 + 63 x^5"); checkPolynomial(PolynomialsUtils.createLegendrePolynomial(4), 8l, "3 - 30 x^2 + 35 x^4"); } @Test public void testLegendreDifferentials() { for (int k = 0; k < 12; ++k) { PolynomialFunction Pk0 = PolynomialsUtils.createLegendrePolynomial(k); PolynomialFunction Pk1 = Pk0.polynomialDerivative(); PolynomialFunction Pk2 = Pk1.polynomialDerivative(); PolynomialFunction g0 = new PolynomialFunction(new double[] { k * (k + 1) }); PolynomialFunction g1 = new PolynomialFunction(new double[] { 0, -2 }); PolynomialFunction g2 = new PolynomialFunction(new double[] { 1, 0, -1 }); PolynomialFunction Pk0g0 = Pk0.multiply(g0); PolynomialFunction Pk1g1 = Pk1.multiply(g1); PolynomialFunction Pk2g2 = Pk2.multiply(g2); checkNullPolynomial(Pk0g0.add(Pk1g1.add(Pk2g2))); } } @Test public void testLegendreOrthogonality() { UnivariateFunction weight = new UnivariateFunction() { public double value(double x) { return 1; } }; for (int i = 0; i < 10; ++i) { PolynomialFunction pi = PolynomialsUtils.createLegendrePolynomial(i); for (int j = 0; j <= i; ++j) { PolynomialFunction pj = PolynomialsUtils.createLegendrePolynomial(j); checkOrthogonality(pi, pj, weight, -1, 1, 0.1, 1.0e-13); } } } @Test public void testHighDegreeLegendre() { PolynomialsUtils.createLegendrePolynomial(40); double[] l40 = PolynomialsUtils.createLegendrePolynomial(40).getCoefficients(); double denominator = 274877906944d; double[] numerators = new double[] { +34461632205d, -28258538408100d, +3847870979902950d, -207785032914759300d, +5929294332103310025d, -103301483474866556880d, +1197358103913226000200d, -9763073770369381232400d, +58171647881784229843050d, -260061484647976556945400d, +888315281771246239250340d, -2345767627188139419665400d, +4819022625419112503443050d, -7710436200670580005508880d, +9566652323054238154983240d, -9104813935044723209570256d, +6516550296251767619752905d, -3391858621221953912598660d, +1211378079007840683070950d, -265365894974690562152100d, +26876802183334044115405d }; for (int i = 0; i < l40.length; ++i) { if (i % 2 == 0) { double ci = numerators[i / 2] / denominator; Assert.assertEquals(ci, l40[i], FastMath.abs(ci) * 1e-15); } else { Assert.assertEquals(0, l40[i], 0); } } } @Test public void testJacobiLegendre() { for (int i = 0; i < 10; ++i) { PolynomialFunction legendre = PolynomialsUtils.createLegendrePolynomial(i); PolynomialFunction jacobi = PolynomialsUtils.createJacobiPolynomial(i, 0, 0); checkNullPolynomial(legendre.subtract(jacobi)); } } @Test public void testJacobiEvaluationAt1() { for (int v = 0; v < 10; ++v) { for (int w = 0; w < 10; ++w) { for (int i = 0; i < 10; ++i) { PolynomialFunction jacobi = PolynomialsUtils.createJacobiPolynomial(i, v, w); double binomial = ArithmeticUtils.binomialCoefficient(v + i, i); Assert.assertTrue(Precision.equals(binomial, jacobi.value(1.0), 1)); } } } } @Test public void testJacobiOrthogonality() { for (int v = 0; v < 5; ++v) { for (int w = v; w < 5; ++w) { final int vv = v; final int ww = w; UnivariateFunction weight = new UnivariateFunction() { public double value(double x) { return FastMath.pow(1 - x, vv) * FastMath.pow(1 + x, ww); } }; for (int i = 0; i < 10; ++i) { PolynomialFunction pi = PolynomialsUtils.createJacobiPolynomial(i, v, w); for (int j = 0; j <= i; ++j) { PolynomialFunction pj = PolynomialsUtils.createJacobiPolynomial(j, v, w); checkOrthogonality(pi, pj, weight, -1, 1, 0.1, 1.0e-12); } } } } } @Test public void testShift() { // f1(x) = 1 + x + 2 x^2 PolynomialFunction f1x = new PolynomialFunction(new double[] { 1, 1, 2 }); PolynomialFunction f1x1 = new PolynomialFunction(PolynomialsUtils.shift(f1x.getCoefficients(), 1)); checkPolynomial(f1x1, "4 + 5 x + 2 x^2"); PolynomialFunction f1xM1 = new PolynomialFunction(PolynomialsUtils.shift(f1x.getCoefficients(), -1)); checkPolynomial(f1xM1, "2 - 3 x + 2 x^2"); PolynomialFunction f1x3 = new PolynomialFunction(PolynomialsUtils.shift(f1x.getCoefficients(), 3)); checkPolynomial(f1x3, "22 + 13 x + 2 x^2"); // f2(x) = 2 + 3 x^2 + 8 x^3 + 121 x^5 PolynomialFunction f2x = new PolynomialFunction(new double[]{2, 0, 3, 8, 0, 121}); PolynomialFunction f2x1 = new PolynomialFunction(PolynomialsUtils.shift(f2x.getCoefficients(), 1)); checkPolynomial(f2x1, "134 + 635 x + 1237 x^2 + 1218 x^3 + 605 x^4 + 121 x^5"); PolynomialFunction f2x3 = new PolynomialFunction(PolynomialsUtils.shift(f2x.getCoefficients(), 3)); checkPolynomial(f2x3, "29648 + 49239 x + 32745 x^2 + 10898 x^3 + 1815 x^4 + 121 x^5"); } private void checkPolynomial(PolynomialFunction p, long denominator, String reference) { PolynomialFunction q = new PolynomialFunction(new double[] { denominator}); Assert.assertEquals(reference, p.multiply(q).toString()); } private void checkPolynomial(PolynomialFunction p, String reference) { Assert.assertEquals(reference, p.toString()); } private void checkNullPolynomial(PolynomialFunction p) { for (double coefficient : p.getCoefficients()) { Assert.assertEquals(0, coefficient, 1e-13); } } private void checkOrthogonality(final PolynomialFunction p1, final PolynomialFunction p2, final UnivariateFunction weight, final double a, final double b, final double nonZeroThreshold, final double zeroThreshold) { UnivariateFunction f = new UnivariateFunction() { public double value(double x) { return weight.value(x) * p1.value(x) * p2.value(x); } }; double dotProduct = new IterativeLegendreGaussIntegrator(5, 1.0e-9, 1.0e-8, 2, 15).integrate(1000000, f, a, b); if (p1.degree() == p2.degree()) { // integral should be non-zero Assert.assertTrue("I(" + p1.degree() + ", " + p2.degree() + ") = "+ dotProduct, FastMath.abs(dotProduct) > nonZeroThreshold); } else { // integral should be zero Assert.assertEquals("I(" + p1.degree() + ", " + p2.degree() + ") = "+ dotProduct, 0.0, FastMath.abs(dotProduct), zeroThreshold); } } } ././@LongLink100644 0 0 170 12126630646 10257 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/analysis/polynomials/PolynomialFunctionNewtonFormTest.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/analysis/polynomials/PolynomialFunction100644 1750 1750 15035 12126627670 32551 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.analysis.polynomials; import org.apache.commons.math3.analysis.differentiation.DerivativeStructure; import org.apache.commons.math3.exception.MathIllegalArgumentException; import org.junit.Assert; import org.junit.Test; /** * Test case for Newton form of polynomial function. *

              * The small tolerance number is used only to account for round-off errors. * * @version $Id: PolynomialFunctionNewtonFormTest.java 1383437 2012-09-11 14:42:44Z luc $ */ public final class PolynomialFunctionNewtonFormTest { /** * Test of polynomial for the linear function. */ @Test public void testLinearFunction() { PolynomialFunctionNewtonForm p; double coefficients[], z, expected, result, tolerance = 1E-12; // p(x) = 1.5x - 4 = 2 + 1.5(x-4) double a[] = { 2.0, 1.5 }; double c[] = { 4.0 }; p = new PolynomialFunctionNewtonForm(a, c); z = 2.0; expected = -1.0; result = p.value(z); Assert.assertEquals(expected, result, tolerance); z = 4.5; expected = 2.75; result = p.value(z); Assert.assertEquals(expected, result, tolerance); z = 6.0; expected = 5.0; result = p.value(z); Assert.assertEquals(expected, result, tolerance); Assert.assertEquals(1, p.degree()); coefficients = p.getCoefficients(); Assert.assertEquals(2, coefficients.length); Assert.assertEquals(-4.0, coefficients[0], tolerance); Assert.assertEquals(1.5, coefficients[1], tolerance); } /** * Test of polynomial for the quadratic function. */ @Test public void testQuadraticFunction() { PolynomialFunctionNewtonForm p; double coefficients[], z, expected, result, tolerance = 1E-12; // p(x) = 2x^2 + 5x - 3 = 4 + 3(x-1) + 2(x-1)(x+2) double a[] = { 4.0, 3.0, 2.0 }; double c[] = { 1.0, -2.0 }; p = new PolynomialFunctionNewtonForm(a, c); z = 1.0; expected = 4.0; result = p.value(z); Assert.assertEquals(expected, result, tolerance); z = 2.5; expected = 22.0; result = p.value(z); Assert.assertEquals(expected, result, tolerance); z = -2.0; expected = -5.0; result = p.value(z); Assert.assertEquals(expected, result, tolerance); Assert.assertEquals(2, p.degree()); coefficients = p.getCoefficients(); Assert.assertEquals(3, coefficients.length); Assert.assertEquals(-3.0, coefficients[0], tolerance); Assert.assertEquals(5.0, coefficients[1], tolerance); Assert.assertEquals(2.0, coefficients[2], tolerance); } /** * Test of polynomial for the quintic function. */ @Test public void testQuinticFunction() { PolynomialFunctionNewtonForm p; double coefficients[], z, expected, result, tolerance = 1E-12; // p(x) = x^5 - x^4 - 7x^3 + x^2 + 6x // = 6x - 6x^2 -6x^2(x-1) + x^2(x-1)(x+1) + x^2(x-1)(x+1)(x-2) double a[] = { 0.0, 6.0, -6.0, -6.0, 1.0, 1.0 }; double c[] = { 0.0, 0.0, 1.0, -1.0, 2.0 }; p = new PolynomialFunctionNewtonForm(a, c); z = 0.0; expected = 0.0; result = p.value(z); Assert.assertEquals(expected, result, tolerance); z = -2.0; expected = 0.0; result = p.value(z); Assert.assertEquals(expected, result, tolerance); z = 4.0; expected = 360.0; result = p.value(z); Assert.assertEquals(expected, result, tolerance); Assert.assertEquals(5, p.degree()); coefficients = p.getCoefficients(); Assert.assertEquals(6, coefficients.length); Assert.assertEquals(0.0, coefficients[0], tolerance); Assert.assertEquals(6.0, coefficients[1], tolerance); Assert.assertEquals(1.0, coefficients[2], tolerance); Assert.assertEquals(-7.0, coefficients[3], tolerance); Assert.assertEquals(-1.0, coefficients[4], tolerance); Assert.assertEquals(1.0, coefficients[5], tolerance); } /** * Test for derivatives. */ @Test public void testDerivative() { // x^3 = 0 * [1] + 1 * [x] + 3 * [x(x-1)] + 1 * [x(x-1)(x-2)] PolynomialFunctionNewtonForm p = new PolynomialFunctionNewtonForm(new double[] { 0, 1, 3, 1 }, new double[] { 0, 1, 2 }); double eps = 2.0e-14; for (double t = 0.0; t < 10.0; t += 0.1) { DerivativeStructure x = new DerivativeStructure(1, 4, 0, t); DerivativeStructure y = p.value(x); Assert.assertEquals(t * t * t, y.getValue(), eps * t * t * t); Assert.assertEquals(3.0 * t * t, y.getPartialDerivative(1), eps * 3.0 * t * t); Assert.assertEquals(6.0 * t, y.getPartialDerivative(2), eps * 6.0 * t); Assert.assertEquals(6.0, y.getPartialDerivative(3), eps * 6.0); Assert.assertEquals(0.0, y.getPartialDerivative(4), eps); } } /** * Test of parameters for the polynomial. */ @Test public void testParameters() { try { // bad input array length double a[] = { 1.0 }; double c[] = { 2.0 }; new PolynomialFunctionNewtonForm(a, c); Assert.fail("Expecting MathIllegalArgumentException - bad input array length"); } catch (MathIllegalArgumentException ex) { // expected } try { // mismatch input arrays double a[] = { 1.0, 2.0, 3.0, 4.0 }; double c[] = { 4.0, 3.0, 2.0, 1.0 }; new PolynomialFunctionNewtonForm(a, c); Assert.fail("Expecting MathIllegalArgumentException - mismatch input arrays"); } catch (MathIllegalArgumentException ex) { // expected } } } ././@LongLink100644 0 0 172 12126630646 10261 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/analysis/polynomials/PolynomialFunctionLagrangeFormTest.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/analysis/polynomials/PolynomialFunction100644 1750 1750 12546 12126627670 32555 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.analysis.polynomials; import org.apache.commons.math3.exception.MathIllegalArgumentException; import org.junit.Assert; import org.junit.Test; /** * Test case for Lagrange form of polynomial function. *

              * We use n+1 points to interpolate a polynomial of degree n. This should * give us the exact same polynomial as result. Thus we can use a very * small tolerance to account only for round-off errors. * * @version $Id: PolynomialFunctionLagrangeFormTest.java 1364030 2012-07-21 01:10:04Z erans $ */ public final class PolynomialFunctionLagrangeFormTest { /** * Test of polynomial for the linear function. */ @Test public void testLinearFunction() { PolynomialFunctionLagrangeForm p; double c[], z, expected, result, tolerance = 1E-12; // p(x) = 1.5x - 4 double x[] = { 0.0, 3.0 }; double y[] = { -4.0, 0.5 }; p = new PolynomialFunctionLagrangeForm(x, y); z = 2.0; expected = -1.0; result = p.value(z); Assert.assertEquals(expected, result, tolerance); z = 4.5; expected = 2.75; result = p.value(z); Assert.assertEquals(expected, result, tolerance); z = 6.0; expected = 5.0; result = p.value(z); Assert.assertEquals(expected, result, tolerance); Assert.assertEquals(1, p.degree()); c = p.getCoefficients(); Assert.assertEquals(2, c.length); Assert.assertEquals(-4.0, c[0], tolerance); Assert.assertEquals(1.5, c[1], tolerance); } /** * Test of polynomial for the quadratic function. */ @Test public void testQuadraticFunction() { PolynomialFunctionLagrangeForm p; double c[], z, expected, result, tolerance = 1E-12; // p(x) = 2x^2 + 5x - 3 = (2x - 1)(x + 3) double x[] = { 0.0, -1.0, 0.5 }; double y[] = { -3.0, -6.0, 0.0 }; p = new PolynomialFunctionLagrangeForm(x, y); z = 1.0; expected = 4.0; result = p.value(z); Assert.assertEquals(expected, result, tolerance); z = 2.5; expected = 22.0; result = p.value(z); Assert.assertEquals(expected, result, tolerance); z = -2.0; expected = -5.0; result = p.value(z); Assert.assertEquals(expected, result, tolerance); Assert.assertEquals(2, p.degree()); c = p.getCoefficients(); Assert.assertEquals(3, c.length); Assert.assertEquals(-3.0, c[0], tolerance); Assert.assertEquals(5.0, c[1], tolerance); Assert.assertEquals(2.0, c[2], tolerance); } /** * Test of polynomial for the quintic function. */ @Test public void testQuinticFunction() { PolynomialFunctionLagrangeForm p; double c[], z, expected, result, tolerance = 1E-12; // p(x) = x^5 - x^4 - 7x^3 + x^2 + 6x = x(x^2 - 1)(x + 2)(x - 3) double x[] = { 1.0, -1.0, 2.0, 3.0, -3.0, 0.5 }; double y[] = { 0.0, 0.0, -24.0, 0.0, -144.0, 2.34375 }; p = new PolynomialFunctionLagrangeForm(x, y); z = 0.0; expected = 0.0; result = p.value(z); Assert.assertEquals(expected, result, tolerance); z = -2.0; expected = 0.0; result = p.value(z); Assert.assertEquals(expected, result, tolerance); z = 4.0; expected = 360.0; result = p.value(z); Assert.assertEquals(expected, result, tolerance); Assert.assertEquals(5, p.degree()); c = p.getCoefficients(); Assert.assertEquals(6, c.length); Assert.assertEquals(0.0, c[0], tolerance); Assert.assertEquals(6.0, c[1], tolerance); Assert.assertEquals(1.0, c[2], tolerance); Assert.assertEquals(-7.0, c[3], tolerance); Assert.assertEquals(-1.0, c[4], tolerance); Assert.assertEquals(1.0, c[5], tolerance); } /** * Test of parameters for the polynomial. */ @Test public void testParameters() { try { // bad input array length double x[] = { 1.0 }; double y[] = { 2.0 }; new PolynomialFunctionLagrangeForm(x, y); Assert.fail("Expecting MathIllegalArgumentException - bad input array length"); } catch (MathIllegalArgumentException ex) { // expected } try { // mismatch input arrays double x[] = { 1.0, 2.0, 3.0, 4.0 }; double y[] = { 0.0, -4.0, -24.0 }; new PolynomialFunctionLagrangeForm(x, y); Assert.fail("Expecting MathIllegalArgumentException - mismatch input arrays"); } catch (MathIllegalArgumentException ex) { // expected } } } ././@LongLink100644 0 0 164 12126630646 10262 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/analysis/polynomials/PolynomialSplineFunctionTest.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/analysis/polynomials/PolynomialSplineFu100644 1750 1750 13166 12126627670 32514 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.analysis.polynomials; import java.util.Arrays; import org.apache.commons.math3.exception.OutOfRangeException; import org.apache.commons.math3.analysis.UnivariateFunction; import org.apache.commons.math3.exception.MathIllegalArgumentException; import org.apache.commons.math3.exception.MathIllegalStateException; import org.junit.Assert; import org.junit.Test; /** * Tests the PolynomialSplineFunction implementation. * * @version $Id: PolynomialSplineFunctionTest.java 1364030 2012-07-21 01:10:04Z erans $ */ public class PolynomialSplineFunctionTest { /** Error tolerance for tests */ protected double tolerance = 1.0e-12; /** * Quadratic polynomials used in tests: * * x^2 + x [-1, 0) * x^2 + x + 2 [0, 1) * x^2 + x + 4 [1, 2) * * Defined so that evaluation using PolynomialSplineFunction evaluation * algorithm agrees at knot point boundaries. */ protected PolynomialFunction[] polynomials = { new PolynomialFunction(new double[] {0d, 1d, 1d}), new PolynomialFunction(new double[] {2d, 1d, 1d}), new PolynomialFunction(new double[] {4d, 1d, 1d}) }; /** Knot points */ protected double[] knots = {-1, 0, 1, 2}; /** Derivative of test polynomials -- 2x + 1 */ protected PolynomialFunction dp = new PolynomialFunction(new double[] {1d, 2d}); @Test public void testConstructor() { PolynomialSplineFunction spline = new PolynomialSplineFunction(knots, polynomials); Assert.assertTrue(Arrays.equals(knots, spline.getKnots())); Assert.assertEquals(1d, spline.getPolynomials()[0].getCoefficients()[2], 0); Assert.assertEquals(3, spline.getN()); try { // too few knots new PolynomialSplineFunction(new double[] {0}, polynomials); Assert.fail("Expecting MathIllegalArgumentException"); } catch (MathIllegalArgumentException ex) { // expected } try { // too many knots new PolynomialSplineFunction(new double[] {0,1,2,3,4}, polynomials); Assert.fail("Expecting MathIllegalArgumentException"); } catch (MathIllegalArgumentException ex) { // expected } try { // knots not increasing new PolynomialSplineFunction(new double[] {0,1, 3, 2}, polynomials); Assert.fail("Expecting MathIllegalArgumentException"); } catch (MathIllegalArgumentException ex) { // expected } } @Test public void testValues() { PolynomialSplineFunction spline = new PolynomialSplineFunction(knots, polynomials); UnivariateFunction dSpline = spline.derivative(); /** * interior points -- spline value at x should equal p(x - knot) * where knot is the largest knot point less than or equal to x and p * is the polynomial defined over the knot segment to which x belongs. */ double x = -1; int index = 0; for (int i = 0; i < 10; i++) { x+=0.25; index = findKnot(knots, x); Assert.assertEquals("spline function evaluation failed for x=" + x, polynomials[index].value(x - knots[index]), spline.value(x), tolerance); Assert.assertEquals("spline derivative evaluation failed for x=" + x, dp.value(x - knots[index]), dSpline.value(x), tolerance); } // knot points -- centering should zero arguments for (int i = 0; i < 3; i++) { Assert.assertEquals("spline function evaluation failed for knot=" + knots[i], polynomials[i].value(0), spline.value(knots[i]), tolerance); Assert.assertEquals("spline function evaluation failed for knot=" + knots[i], dp.value(0), dSpline.value(knots[i]), tolerance); } try { //outside of domain -- under min x = spline.value(-1.5); Assert.fail("Expecting OutOfRangeException"); } catch (OutOfRangeException ex) { // expected } try { //outside of domain -- over max x = spline.value(2.5); Assert.fail("Expecting OutOfRangeException"); } catch (OutOfRangeException ex) { // expected } } /** * Do linear search to find largest knot point less than or equal to x. * Implementation does binary search. */ protected int findKnot(double[] knots, double x) { if (x < knots[0] || x >= knots[knots.length -1]) { throw new OutOfRangeException(x, knots[0], knots[knots.length -1]); } for (int i = 0; i < knots.length; i++) { if (knots[i] > x) { return i - 1; } } throw new MathIllegalStateException(); } } ././@LongLink100644 0 0 152 12126630646 10257 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/analysis/differentiation/DSCompilerTest.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/analysis/differentiation/DSCompilerTest100644 1750 1750 74654 12126627670 32401 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.analysis.differentiation; import java.lang.reflect.Field; import java.util.HashMap; import java.util.Map; import org.apache.commons.math3.exception.DimensionMismatchException; import org.apache.commons.math3.util.ArithmeticUtils; import org.junit.Assert; import org.junit.Test; /** * Test for class {@link DSCompiler}. */ public class DSCompilerTest { @Test public void testSize() { for (int i = 0; i < 6; ++i) { for (int j = 0; j < 6; ++j) { long expected = ArithmeticUtils.binomialCoefficient(i + j, i); Assert.assertEquals(expected, DSCompiler.getCompiler(i, j).getSize()); Assert.assertEquals(expected, DSCompiler.getCompiler(j, i).getSize()); } } } @Test public void testIndices() { DSCompiler c = DSCompiler.getCompiler(0, 0); checkIndices(c.getPartialDerivativeOrders(0), new int[0]); c = DSCompiler.getCompiler(0, 1); checkIndices(c.getPartialDerivativeOrders(0), new int[0]); c = DSCompiler.getCompiler(1, 0); checkIndices(c.getPartialDerivativeOrders(0), 0); c = DSCompiler.getCompiler(1, 1); checkIndices(c.getPartialDerivativeOrders(0), 0); checkIndices(c.getPartialDerivativeOrders(1), 1); c = DSCompiler.getCompiler(1, 2); checkIndices(c.getPartialDerivativeOrders(0), 0); checkIndices(c.getPartialDerivativeOrders(1), 1); checkIndices(c.getPartialDerivativeOrders(2), 2); c = DSCompiler.getCompiler(2, 1); checkIndices(c.getPartialDerivativeOrders(0), 0, 0); checkIndices(c.getPartialDerivativeOrders(1), 1, 0); checkIndices(c.getPartialDerivativeOrders(2), 0, 1); c = DSCompiler.getCompiler(1, 3); checkIndices(c.getPartialDerivativeOrders(0), 0); checkIndices(c.getPartialDerivativeOrders(1), 1); checkIndices(c.getPartialDerivativeOrders(2), 2); checkIndices(c.getPartialDerivativeOrders(3), 3); c = DSCompiler.getCompiler(2, 2); checkIndices(c.getPartialDerivativeOrders(0), 0, 0); checkIndices(c.getPartialDerivativeOrders(1), 1, 0); checkIndices(c.getPartialDerivativeOrders(2), 2, 0); checkIndices(c.getPartialDerivativeOrders(3), 0, 1); checkIndices(c.getPartialDerivativeOrders(4), 1, 1); checkIndices(c.getPartialDerivativeOrders(5), 0, 2); c = DSCompiler.getCompiler(3, 1); checkIndices(c.getPartialDerivativeOrders(0), 0, 0, 0); checkIndices(c.getPartialDerivativeOrders(1), 1, 0, 0); checkIndices(c.getPartialDerivativeOrders(2), 0, 1, 0); checkIndices(c.getPartialDerivativeOrders(3), 0, 0, 1); c = DSCompiler.getCompiler(1, 4); checkIndices(c.getPartialDerivativeOrders(0), 0); checkIndices(c.getPartialDerivativeOrders(1), 1); checkIndices(c.getPartialDerivativeOrders(2), 2); checkIndices(c.getPartialDerivativeOrders(3), 3); checkIndices(c.getPartialDerivativeOrders(4), 4); c = DSCompiler.getCompiler(2, 3); checkIndices(c.getPartialDerivativeOrders(0), 0, 0); checkIndices(c.getPartialDerivativeOrders(1), 1, 0); checkIndices(c.getPartialDerivativeOrders(2), 2, 0); checkIndices(c.getPartialDerivativeOrders(3), 3, 0); checkIndices(c.getPartialDerivativeOrders(4), 0, 1); checkIndices(c.getPartialDerivativeOrders(5), 1, 1); checkIndices(c.getPartialDerivativeOrders(6), 2, 1); checkIndices(c.getPartialDerivativeOrders(7), 0, 2); checkIndices(c.getPartialDerivativeOrders(8), 1, 2); checkIndices(c.getPartialDerivativeOrders(9), 0, 3); c = DSCompiler.getCompiler(3, 2); checkIndices(c.getPartialDerivativeOrders(0), 0, 0, 0); checkIndices(c.getPartialDerivativeOrders(1), 1, 0, 0); checkIndices(c.getPartialDerivativeOrders(2), 2, 0, 0); checkIndices(c.getPartialDerivativeOrders(3), 0, 1, 0); checkIndices(c.getPartialDerivativeOrders(4), 1, 1, 0); checkIndices(c.getPartialDerivativeOrders(5), 0, 2, 0); checkIndices(c.getPartialDerivativeOrders(6), 0, 0, 1); checkIndices(c.getPartialDerivativeOrders(7), 1, 0, 1); checkIndices(c.getPartialDerivativeOrders(8), 0, 1, 1); checkIndices(c.getPartialDerivativeOrders(9), 0, 0, 2); c = DSCompiler.getCompiler(4, 1); checkIndices(c.getPartialDerivativeOrders(0), 0, 0, 0, 0); checkIndices(c.getPartialDerivativeOrders(1), 1, 0, 0, 0); checkIndices(c.getPartialDerivativeOrders(2), 0, 1, 0, 0); checkIndices(c.getPartialDerivativeOrders(3), 0, 0, 1, 0); checkIndices(c.getPartialDerivativeOrders(4), 0, 0, 0, 1); } @Test(expected=DimensionMismatchException.class) public void testIncompatibleParams() { DSCompiler.getCompiler(3, 2).checkCompatibility(DSCompiler.getCompiler(4, 2)); } @Test(expected=DimensionMismatchException.class) public void testIncompatibleOrder() { DSCompiler.getCompiler(3, 3).checkCompatibility(DSCompiler.getCompiler(3, 2)); } @Test public void testSymmetry() { for (int i = 0; i < 6; ++i) { for (int j = 0; j < 6; ++j) { DSCompiler c = DSCompiler.getCompiler(i, j); for (int k = 0; k < c.getSize(); ++k) { Assert.assertEquals(k, c.getPartialDerivativeIndex(c.getPartialDerivativeOrders(k))); } } } } @Test public void testMultiplicationRules() throws SecurityException, NoSuchFieldException, IllegalArgumentException, IllegalAccessException { Map referenceRules = new HashMap(); referenceRules.put("(f*g)", "f * g"); referenceRules.put("d(f*g)/dx", "f * dg/dx + df/dx * g"); referenceRules.put("d(f*g)/dy", referenceRules.get("d(f*g)/dx").replaceAll("x", "y")); referenceRules.put("d(f*g)/dz", referenceRules.get("d(f*g)/dx").replaceAll("x", "z")); referenceRules.put("d(f*g)/dt", referenceRules.get("d(f*g)/dx").replaceAll("x", "t")); referenceRules.put("d2(f*g)/dx2", "f * d2g/dx2 + 2 * df/dx * dg/dx + d2f/dx2 * g"); referenceRules.put("d2(f*g)/dy2", referenceRules.get("d2(f*g)/dx2").replaceAll("x", "y")); referenceRules.put("d2(f*g)/dz2", referenceRules.get("d2(f*g)/dx2").replaceAll("x", "z")); referenceRules.put("d2(f*g)/dt2", referenceRules.get("d2(f*g)/dx2").replaceAll("x", "t")); referenceRules.put("d2(f*g)/dxdy", "f * d2g/dxdy + df/dy * dg/dx + df/dx * dg/dy + d2f/dxdy * g"); referenceRules.put("d2(f*g)/dxdz", referenceRules.get("d2(f*g)/dxdy").replaceAll("y", "z")); referenceRules.put("d2(f*g)/dxdt", referenceRules.get("d2(f*g)/dxdy").replaceAll("y", "t")); referenceRules.put("d2(f*g)/dydz", referenceRules.get("d2(f*g)/dxdz").replaceAll("x", "y")); referenceRules.put("d2(f*g)/dydt", referenceRules.get("d2(f*g)/dxdt").replaceAll("x", "y")); referenceRules.put("d2(f*g)/dzdt", referenceRules.get("d2(f*g)/dxdt").replaceAll("x", "z")); referenceRules.put("d3(f*g)/dx3", "f * d3g/dx3 +" + " 3 * df/dx * d2g/dx2 +" + " 3 * d2f/dx2 * dg/dx +" + " d3f/dx3 * g"); referenceRules.put("d3(f*g)/dy3", referenceRules.get("d3(f*g)/dx3").replaceAll("x", "y")); referenceRules.put("d3(f*g)/dz3", referenceRules.get("d3(f*g)/dx3").replaceAll("x", "z")); referenceRules.put("d3(f*g)/dt3", referenceRules.get("d3(f*g)/dx3").replaceAll("x", "t")); referenceRules.put("d3(f*g)/dx2dy", "f * d3g/dx2dy +" + " df/dy * d2g/dx2 +" + " 2 * df/dx * d2g/dxdy +" + " 2 * d2f/dxdy * dg/dx +" + " d2f/dx2 * dg/dy +" + " d3f/dx2dy * g"); referenceRules.put("d3(f*g)/dxdy2", "f * d3g/dxdy2 +" + " 2 * df/dy * d2g/dxdy +" + " d2f/dy2 * dg/dx +" + " df/dx * d2g/dy2 +" + " 2 * d2f/dxdy * dg/dy +" + " d3f/dxdy2 * g"); referenceRules.put("d3(f*g)/dx2dz", referenceRules.get("d3(f*g)/dx2dy").replaceAll("y", "z")); referenceRules.put("d3(f*g)/dy2dz", referenceRules.get("d3(f*g)/dx2dz").replaceAll("x", "y")); referenceRules.put("d3(f*g)/dxdz2", referenceRules.get("d3(f*g)/dxdy2").replaceAll("y", "z")); referenceRules.put("d3(f*g)/dydz2", referenceRules.get("d3(f*g)/dxdz2").replaceAll("x", "y")); referenceRules.put("d3(f*g)/dx2dt", referenceRules.get("d3(f*g)/dx2dz").replaceAll("z", "t")); referenceRules.put("d3(f*g)/dy2dt", referenceRules.get("d3(f*g)/dx2dt").replaceAll("x", "y")); referenceRules.put("d3(f*g)/dz2dt", referenceRules.get("d3(f*g)/dx2dt").replaceAll("x", "z")); referenceRules.put("d3(f*g)/dxdt2", referenceRules.get("d3(f*g)/dxdy2").replaceAll("y", "t")); referenceRules.put("d3(f*g)/dydt2", referenceRules.get("d3(f*g)/dxdt2").replaceAll("x", "y")); referenceRules.put("d3(f*g)/dzdt2", referenceRules.get("d3(f*g)/dxdt2").replaceAll("x", "z")); referenceRules.put("d3(f*g)/dxdydz", "f * d3g/dxdydz +" + " df/dz * d2g/dxdy +" + " df/dy * d2g/dxdz +" + " d2f/dydz * dg/dx +" + " df/dx * d2g/dydz +" + " d2f/dxdz * dg/dy +" + " d2f/dxdy * dg/dz +" + " d3f/dxdydz * g"); referenceRules.put("d3(f*g)/dxdydt", referenceRules.get("d3(f*g)/dxdydz").replaceAll("z", "t")); referenceRules.put("d3(f*g)/dxdzdt", referenceRules.get("d3(f*g)/dxdydt").replaceAll("y", "z")); referenceRules.put("d3(f*g)/dydzdt", referenceRules.get("d3(f*g)/dxdzdt").replaceAll("x", "y")); Field multFieldArrayField = DSCompiler.class.getDeclaredField("multIndirection"); multFieldArrayField.setAccessible(true); for (int i = 0; i < 5; ++i) { for (int j = 0; j < 4; ++j) { DSCompiler compiler = DSCompiler.getCompiler(i, j); int[][][] multIndirection = (int[][][]) multFieldArrayField.get(compiler); for (int k = 0; k < multIndirection.length; ++k) { String product = ordersToString(compiler.getPartialDerivativeOrders(k), "(f*g)", "x", "y", "z", "t"); StringBuilder rule = new StringBuilder(); for (int[] term : multIndirection[k]) { if (rule.length() > 0) { rule.append(" + "); } if (term[0] > 1) { rule.append(term[0]).append(" * "); } rule.append(ordersToString(compiler.getPartialDerivativeOrders(term[1]), "f", "x", "y", "z", "t")); rule.append(" * "); rule.append(ordersToString(compiler.getPartialDerivativeOrders(term[2]), "g", "x", "y", "z", "t")); } Assert.assertEquals(product, referenceRules.get(product), rule.toString()); } } } } @Test public void testCompositionRules() throws SecurityException, NoSuchFieldException, IllegalArgumentException, IllegalAccessException { // the following reference rules have all been computed independently from the library, // using only pencil and paper and some search and replace to handle symmetries Map referenceRules = new HashMap(); referenceRules.put("(f(g))", "(f(g))"); referenceRules.put("d(f(g))/dx", "d(f(g))/dg * dg/dx"); referenceRules.put("d(f(g))/dy", referenceRules.get("d(f(g))/dx").replaceAll("x", "y")); referenceRules.put("d(f(g))/dz", referenceRules.get("d(f(g))/dx").replaceAll("x", "z")); referenceRules.put("d(f(g))/dt", referenceRules.get("d(f(g))/dx").replaceAll("x", "t")); referenceRules.put("d2(f(g))/dx2", "d2(f(g))/dg2 * dg/dx * dg/dx + d(f(g))/dg * d2g/dx2"); referenceRules.put("d2(f(g))/dy2", referenceRules.get("d2(f(g))/dx2").replaceAll("x", "y")); referenceRules.put("d2(f(g))/dz2", referenceRules.get("d2(f(g))/dx2").replaceAll("x", "z")); referenceRules.put("d2(f(g))/dt2", referenceRules.get("d2(f(g))/dx2").replaceAll("x", "t")); referenceRules.put("d2(f(g))/dxdy", "d2(f(g))/dg2 * dg/dx * dg/dy + d(f(g))/dg * d2g/dxdy"); referenceRules.put("d2(f(g))/dxdz", referenceRules.get("d2(f(g))/dxdy").replaceAll("y", "z")); referenceRules.put("d2(f(g))/dxdt", referenceRules.get("d2(f(g))/dxdy").replaceAll("y", "t")); referenceRules.put("d2(f(g))/dydz", referenceRules.get("d2(f(g))/dxdz").replaceAll("x", "y")); referenceRules.put("d2(f(g))/dydt", referenceRules.get("d2(f(g))/dxdt").replaceAll("x", "y")); referenceRules.put("d2(f(g))/dzdt", referenceRules.get("d2(f(g))/dxdt").replaceAll("x", "z")); referenceRules.put("d3(f(g))/dx3", "d3(f(g))/dg3 * dg/dx * dg/dx * dg/dx +" + " 3 * d2(f(g))/dg2 * dg/dx * d2g/dx2 +" + " d(f(g))/dg * d3g/dx3"); referenceRules.put("d3(f(g))/dy3", referenceRules.get("d3(f(g))/dx3").replaceAll("x", "y")); referenceRules.put("d3(f(g))/dz3", referenceRules.get("d3(f(g))/dx3").replaceAll("x", "z")); referenceRules.put("d3(f(g))/dt3", referenceRules.get("d3(f(g))/dx3").replaceAll("x", "t")); referenceRules.put("d3(f(g))/dxdy2", "d3(f(g))/dg3 * dg/dx * dg/dy * dg/dy +" + " 2 * d2(f(g))/dg2 * dg/dy * d2g/dxdy +" + " d2(f(g))/dg2 * dg/dx * d2g/dy2 +" + " d(f(g))/dg * d3g/dxdy2"); referenceRules.put("d3(f(g))/dxdz2", referenceRules.get("d3(f(g))/dxdy2").replaceAll("y", "z")); referenceRules.put("d3(f(g))/dxdt2", referenceRules.get("d3(f(g))/dxdy2").replaceAll("y", "t")); referenceRules.put("d3(f(g))/dydz2", referenceRules.get("d3(f(g))/dxdz2").replaceAll("x", "y")); referenceRules.put("d3(f(g))/dydt2", referenceRules.get("d3(f(g))/dxdt2").replaceAll("x", "y")); referenceRules.put("d3(f(g))/dzdt2", referenceRules.get("d3(f(g))/dxdt2").replaceAll("x", "z")); referenceRules.put("d3(f(g))/dx2dy", "d3(f(g))/dg3 * dg/dx * dg/dx * dg/dy +" + " 2 * d2(f(g))/dg2 * dg/dx * d2g/dxdy +" + " d2(f(g))/dg2 * d2g/dx2 * dg/dy +" + " d(f(g))/dg * d3g/dx2dy"); referenceRules.put("d3(f(g))/dx2dz", referenceRules.get("d3(f(g))/dx2dy").replaceAll("y", "z")); referenceRules.put("d3(f(g))/dx2dt", referenceRules.get("d3(f(g))/dx2dy").replaceAll("y", "t")); referenceRules.put("d3(f(g))/dy2dz", referenceRules.get("d3(f(g))/dx2dz").replaceAll("x", "y")); referenceRules.put("d3(f(g))/dy2dt", referenceRules.get("d3(f(g))/dx2dt").replaceAll("x", "y")); referenceRules.put("d3(f(g))/dz2dt", referenceRules.get("d3(f(g))/dx2dt").replaceAll("x", "z")); referenceRules.put("d3(f(g))/dxdydz", "d3(f(g))/dg3 * dg/dx * dg/dy * dg/dz +" + " d2(f(g))/dg2 * dg/dy * d2g/dxdz +" + " d2(f(g))/dg2 * dg/dx * d2g/dydz +" + " d2(f(g))/dg2 * d2g/dxdy * dg/dz +" + " d(f(g))/dg * d3g/dxdydz"); referenceRules.put("d3(f(g))/dxdydt", referenceRules.get("d3(f(g))/dxdydz").replaceAll("z", "t")); referenceRules.put("d3(f(g))/dxdzdt", referenceRules.get("d3(f(g))/dxdydt").replaceAll("y", "z")); referenceRules.put("d3(f(g))/dydzdt", referenceRules.get("d3(f(g))/dxdzdt").replaceAll("x", "y")); referenceRules.put("d4(f(g))/dx4", "d4(f(g))/dg4 * dg/dx * dg/dx * dg/dx * dg/dx +" + " 6 * d3(f(g))/dg3 * dg/dx * dg/dx * d2g/dx2 +" + " 3 * d2(f(g))/dg2 * d2g/dx2 * d2g/dx2 +" + " 4 * d2(f(g))/dg2 * dg/dx * d3g/dx3 +" + " d(f(g))/dg * d4g/dx4"); referenceRules.put("d4(f(g))/dy4", referenceRules.get("d4(f(g))/dx4").replaceAll("x", "y")); referenceRules.put("d4(f(g))/dz4", referenceRules.get("d4(f(g))/dx4").replaceAll("x", "z")); referenceRules.put("d4(f(g))/dt4", referenceRules.get("d4(f(g))/dx4").replaceAll("x", "t")); referenceRules.put("d4(f(g))/dx3dy", "d4(f(g))/dg4 * dg/dx * dg/dx * dg/dx * dg/dy +" + " 3 * d3(f(g))/dg3 * dg/dx * dg/dx * d2g/dxdy +" + " 3 * d3(f(g))/dg3 * dg/dx * d2g/dx2 * dg/dy +" + " 3 * d2(f(g))/dg2 * d2g/dx2 * d2g/dxdy +" + " 3 * d2(f(g))/dg2 * dg/dx * d3g/dx2dy +" + " d2(f(g))/dg2 * d3g/dx3 * dg/dy +" + " d(f(g))/dg * d4g/dx3dy"); referenceRules.put("d4(f(g))/dx3dz", referenceRules.get("d4(f(g))/dx3dy").replaceAll("y", "z")); referenceRules.put("d4(f(g))/dx3dt", referenceRules.get("d4(f(g))/dx3dy").replaceAll("y", "t")); referenceRules.put("d4(f(g))/dxdy3", "d4(f(g))/dg4 * dg/dx * dg/dy * dg/dy * dg/dy +" + " 3 * d3(f(g))/dg3 * dg/dy * dg/dy * d2g/dxdy +" + " 3 * d3(f(g))/dg3 * dg/dx * dg/dy * d2g/dy2 +" + " 3 * d2(f(g))/dg2 * d2g/dxdy * d2g/dy2 +" + " 3 * d2(f(g))/dg2 * dg/dy * d3g/dxdy2 +" + " d2(f(g))/dg2 * dg/dx * d3g/dy3 +" + " d(f(g))/dg * d4g/dxdy3"); referenceRules.put("d4(f(g))/dxdz3", referenceRules.get("d4(f(g))/dxdy3").replaceAll("y", "z")); referenceRules.put("d4(f(g))/dxdt3", referenceRules.get("d4(f(g))/dxdy3").replaceAll("y", "t")); referenceRules.put("d4(f(g))/dy3dz", referenceRules.get("d4(f(g))/dx3dz").replaceAll("x", "y")); referenceRules.put("d4(f(g))/dy3dt", referenceRules.get("d4(f(g))/dx3dt").replaceAll("x", "y")); referenceRules.put("d4(f(g))/dydz3", referenceRules.get("d4(f(g))/dxdz3").replaceAll("x", "y")); referenceRules.put("d4(f(g))/dydt3", referenceRules.get("d4(f(g))/dxdt3").replaceAll("x", "y")); referenceRules.put("d4(f(g))/dz3dt", referenceRules.get("d4(f(g))/dx3dt").replaceAll("x", "z")); referenceRules.put("d4(f(g))/dzdt3", referenceRules.get("d4(f(g))/dxdt3").replaceAll("x", "z")); referenceRules.put("d4(f(g))/dx2dy2", "d4(f(g))/dg4 * dg/dx * dg/dx * dg/dy * dg/dy +" + " 4 * d3(f(g))/dg3 * dg/dx * dg/dy * d2g/dxdy +" + " d3(f(g))/dg3 * dg/dx * dg/dx * d2g/dy2 +" + " 2 * d2(f(g))/dg2 * d2g/dxdy * d2g/dxdy +" + " 2 * d2(f(g))/dg2 * dg/dx * d3g/dxdy2 +" + " d3(f(g))/dg3 * d2g/dx2 * dg/dy * dg/dy +" + " 2 * d2(f(g))/dg2 * dg/dy * d3g/dx2dy +" + " d2(f(g))/dg2 * d2g/dx2 * d2g/dy2 +" + " d(f(g))/dg * d4g/dx2dy2"); referenceRules.put("d4(f(g))/dx2dz2", referenceRules.get("d4(f(g))/dx2dy2").replaceAll("y", "z")); referenceRules.put("d4(f(g))/dx2dt2", referenceRules.get("d4(f(g))/dx2dy2").replaceAll("y", "t")); referenceRules.put("d4(f(g))/dy2dz2", referenceRules.get("d4(f(g))/dx2dz2").replaceAll("x", "y")); referenceRules.put("d4(f(g))/dy2dt2", referenceRules.get("d4(f(g))/dx2dt2").replaceAll("x", "y")); referenceRules.put("d4(f(g))/dz2dt2", referenceRules.get("d4(f(g))/dx2dt2").replaceAll("x", "z")); referenceRules.put("d4(f(g))/dx2dydz", "d4(f(g))/dg4 * dg/dx * dg/dx * dg/dy * dg/dz +" + " 2 * d3(f(g))/dg3 * dg/dx * dg/dy * d2g/dxdz +" + " d3(f(g))/dg3 * dg/dx * dg/dx * d2g/dydz +" + " 2 * d3(f(g))/dg3 * dg/dx * d2g/dxdy * dg/dz +" + " 2 * d2(f(g))/dg2 * d2g/dxdy * d2g/dxdz +" + " 2 * d2(f(g))/dg2 * dg/dx * d3g/dxdydz +" + " d3(f(g))/dg3 * d2g/dx2 * dg/dy * dg/dz +" + " d2(f(g))/dg2 * dg/dy * d3g/dx2dz +" + " d2(f(g))/dg2 * d2g/dx2 * d2g/dydz +" + " d2(f(g))/dg2 * d3g/dx2dy * dg/dz +" + " d(f(g))/dg * d4g/dx2dydz"); referenceRules.put("d4(f(g))/dx2dydt", referenceRules.get("d4(f(g))/dx2dydz").replaceAll("z", "t")); referenceRules.put("d4(f(g))/dx2dzdt", referenceRules.get("d4(f(g))/dx2dydt").replaceAll("y", "z")); referenceRules.put("d4(f(g))/dxdy2dz", "d4(f(g))/dg4 * dg/dx * dg/dy * dg/dy * dg/dz +" + " d3(f(g))/dg3 * dg/dy * dg/dy * d2g/dxdz +" + " 2 * d3(f(g))/dg3 * dg/dx * dg/dy * d2g/dydz +" + " 2 * d3(f(g))/dg3 * dg/dy * d2g/dxdy * dg/dz +" + " 2 * d2(f(g))/dg2 * d2g/dxdy * d2g/dydz +" + " 2 * d2(f(g))/dg2 * dg/dy * d3g/dxdydz +" + " d3(f(g))/dg3 * dg/dx * d2g/dy2 * dg/dz +" + " d2(f(g))/dg2 * d2g/dy2 * d2g/dxdz +" + " d2(f(g))/dg2 * dg/dx * d3g/dy2dz +" + " d2(f(g))/dg2 * d3g/dxdy2 * dg/dz +" + " d(f(g))/dg * d4g/dxdy2dz"); referenceRules.put("d4(f(g))/dxdy2dt", referenceRules.get("d4(f(g))/dxdy2dz").replaceAll("z", "t")); referenceRules.put("d4(f(g))/dy2dzdt", referenceRules.get("d4(f(g))/dx2dzdt").replaceAll("x", "y")); referenceRules.put("d4(f(g))/dxdydz2", "d4(f(g))/dg4 * dg/dx * dg/dy * dg/dz * dg/dz +" + " 2 * d3(f(g))/dg3 * dg/dy * dg/dz * d2g/dxdz +" + " 2 * d3(f(g))/dg3 * dg/dx * dg/dz * d2g/dydz +" + " d3(f(g))/dg3 * dg/dx * dg/dy * d2g/dz2 +" + " 2 * d2(f(g))/dg2 * d2g/dxdz * d2g/dydz +" + " d2(f(g))/dg2 * dg/dy * d3g/dxdz2 +" + " d2(f(g))/dg2 * dg/dx * d3g/dydz2 +" + " d3(f(g))/dg3 * d2g/dxdy * dg/dz * dg/dz +" + " 2 * d2(f(g))/dg2 * dg/dz * d3g/dxdydz +" + " d2(f(g))/dg2 * d2g/dxdy * d2g/dz2 +" + " d(f(g))/dg * d4g/dxdydz2"); referenceRules.put("d4(f(g))/dxdz2dt", referenceRules.get("d4(f(g))/dxdy2dt").replaceAll("y", "z")); referenceRules.put("d4(f(g))/dydz2dt", referenceRules.get("d4(f(g))/dxdz2dt").replaceAll("x", "y")); referenceRules.put("d4(f(g))/dxdydt2", referenceRules.get("d4(f(g))/dxdydz2").replaceAll("z", "t")); referenceRules.put("d4(f(g))/dxdzdt2", referenceRules.get("d4(f(g))/dxdydt2").replaceAll("y", "z")); referenceRules.put("d4(f(g))/dydzdt2", referenceRules.get("d4(f(g))/dxdzdt2").replaceAll("x", "y")); referenceRules.put("d4(f(g))/dxdydzdt", "d4(f(g))/dg4 * dg/dx * dg/dy * dg/dz * dg/dt +" + " d3(f(g))/dg3 * dg/dy * dg/dz * d2g/dxdt +" + " d3(f(g))/dg3 * dg/dx * dg/dz * d2g/dydt +" + " d3(f(g))/dg3 * dg/dx * dg/dy * d2g/dzdt +" + " d3(f(g))/dg3 * dg/dy * d2g/dxdz * dg/dt +" + " d2(f(g))/dg2 * d2g/dxdz * d2g/dydt +" + " d2(f(g))/dg2 * dg/dy * d3g/dxdzdt +" + " d3(f(g))/dg3 * dg/dx * d2g/dydz * dg/dt +" + " d2(f(g))/dg2 * d2g/dydz * d2g/dxdt +" + " d2(f(g))/dg2 * dg/dx * d3g/dydzdt +" + " d3(f(g))/dg3 * d2g/dxdy * dg/dz * dg/dt +" + " d2(f(g))/dg2 * dg/dz * d3g/dxdydt +" + " d2(f(g))/dg2 * d2g/dxdy * d2g/dzdt +" + " d2(f(g))/dg2 * d3g/dxdydz * dg/dt +" + " d(f(g))/dg * d4g/dxdydzdt"); Field compFieldArrayField = DSCompiler.class.getDeclaredField("compIndirection"); compFieldArrayField.setAccessible(true); for (int i = 0; i < 5; ++i) { for (int j = 0; j < 5; ++j) { DSCompiler compiler = DSCompiler.getCompiler(i, j); int[][][] compIndirection = (int[][][]) compFieldArrayField.get(compiler); for (int k = 0; k < compIndirection.length; ++k) { String product = ordersToString(compiler.getPartialDerivativeOrders(k), "(f(g))", "x", "y", "z", "t"); StringBuilder rule = new StringBuilder(); for (int[] term : compIndirection[k]) { if (rule.length() > 0) { rule.append(" + "); } if (term[0] > 1) { rule.append(term[0]).append(" * "); } rule.append(orderToString(term[1], "(f(g))", "g")); for (int l = 2; l < term.length; ++l) { rule.append(" * "); rule.append(ordersToString(compiler.getPartialDerivativeOrders(term[l]), "g", "x", "y", "z", "t")); } } Assert.assertEquals(product, referenceRules.get(product), rule.toString()); } } } } private void checkIndices(int[] indices, int ... expected) { Assert.assertEquals(expected.length, indices.length); for (int i = 0; i < expected.length; ++i) { Assert.assertEquals(expected[i], indices[i]); } } private String orderToString(int order, String functionName, String parameterName) { if (order == 0) { return functionName; } else if (order == 1) { return "d" + functionName + "/d" + parameterName; } else { return "d" + order + functionName + "/d" + parameterName + order; } } private String ordersToString(int[] orders, String functionName, String ... parametersNames) { int sumOrders = 0; for (int order : orders) { sumOrders += order; } if (sumOrders == 0) { return functionName; } StringBuilder builder = new StringBuilder(); builder.append('d'); if (sumOrders > 1) { builder.append(sumOrders); } builder.append(functionName).append('/'); for (int i = 0; i < orders.length; ++i) { if (orders[i] > 0) { builder.append('d').append(parametersNames[i]); if (orders[i] > 1) { builder.append(orders[i]); } } } return builder.toString(); } } ././@LongLink100644 0 0 160 12126630646 10256 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/analysis/differentiation/GradientFunctionTest.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/analysis/differentiation/GradientFuncti100644 1750 1750 6073 12126627670 32414 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.analysis.differentiation; import org.apache.commons.math3.TestUtils; import org.apache.commons.math3.exception.DimensionMismatchException; import org.apache.commons.math3.exception.MathIllegalArgumentException; import org.apache.commons.math3.util.FastMath; import org.junit.Test; /** * Test for class {@link GradientFunction}. */ public class GradientFunctionTest { @Test public void test2DDistance() { EuclideanDistance f = new EuclideanDistance(); GradientFunction g = new GradientFunction(f); for (double x = -10; x < 10; x += 0.5) { for (double y = -10; y < 10; y += 0.5) { double[] point = new double[] { x, y }; TestUtils.assertEquals(f.gradient(point), g.value(point), 1.0e-15); } } } @Test public void test3DDistance() { EuclideanDistance f = new EuclideanDistance(); GradientFunction g = new GradientFunction(f); for (double x = -10; x < 10; x += 0.5) { for (double y = -10; y < 10; y += 0.5) { for (double z = -10; z < 10; z += 0.5) { double[] point = new double[] { x, y, z }; TestUtils.assertEquals(f.gradient(point), g.value(point), 1.0e-15); } } } } private static class EuclideanDistance implements MultivariateDifferentiableFunction { public double value(double[] point) { double d2 = 0; for (double x : point) { d2 += x * x; } return FastMath.sqrt(d2); } public DerivativeStructure value(DerivativeStructure[] point) throws DimensionMismatchException, MathIllegalArgumentException { DerivativeStructure d2 = point[0].getField().getZero(); for (DerivativeStructure x : point) { d2 = d2.add(x.multiply(x)); } return d2.sqrt(); } public double[] gradient(double[] point) { double[] gradient = new double[point.length]; double d = value(point); for (int i = 0; i < point.length; ++i) { gradient[i] = point[i] / d; } return gradient; } } } ././@LongLink100644 0 0 177 12126630646 10266 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/analysis/differentiation/FiniteDifferencesDifferentiatorTest.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/analysis/differentiation/FiniteDifferen100644 1750 1750 44674 12126627670 32420 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.analysis.differentiation; import org.apache.commons.math3.TestUtils; import org.apache.commons.math3.analysis.QuinticFunction; import org.apache.commons.math3.analysis.UnivariateFunction; import org.apache.commons.math3.analysis.UnivariateMatrixFunction; import org.apache.commons.math3.analysis.UnivariateVectorFunction; import org.apache.commons.math3.analysis.function.Gaussian; import org.apache.commons.math3.analysis.function.Sin; import org.apache.commons.math3.exception.MathInternalError; import org.apache.commons.math3.exception.NotPositiveException; import org.apache.commons.math3.exception.NumberIsTooLargeException; import org.apache.commons.math3.exception.NumberIsTooSmallException; import org.apache.commons.math3.util.FastMath; import org.junit.Assert; import org.junit.Test; /** * Test for class {@link FiniteDifferencesDifferentiator}. */ public class FiniteDifferencesDifferentiatorTest { @Test(expected=NumberIsTooSmallException.class) public void testWrongNumberOfPoints() { new FiniteDifferencesDifferentiator(1, 1.0); } @Test(expected=NotPositiveException.class) public void testWrongStepSize() { new FiniteDifferencesDifferentiator(3, 0.0); } @Test public void testSerialization() { FiniteDifferencesDifferentiator differentiator = new FiniteDifferencesDifferentiator(3, 1.0e-3); FiniteDifferencesDifferentiator recovered = (FiniteDifferencesDifferentiator) TestUtils.serializeAndRecover(differentiator); Assert.assertEquals(differentiator.getNbPoints(), recovered.getNbPoints()); Assert.assertEquals(differentiator.getStepSize(), recovered.getStepSize(), 1.0e-15); } @Test public void testConstant() { FiniteDifferencesDifferentiator differentiator = new FiniteDifferencesDifferentiator(5, 0.01); UnivariateDifferentiableFunction f = differentiator.differentiate(new UnivariateFunction() { public double value(double x) { return 42.0; } }); for (double x = -10; x < 10; x += 0.1) { DerivativeStructure y = f.value(new DerivativeStructure(1, 2, 0, x)); Assert.assertEquals(42.0, y.getValue(), 1.0e-15); Assert.assertEquals( 0.0, y.getPartialDerivative(1), 1.0e-15); Assert.assertEquals( 0.0, y.getPartialDerivative(2), 1.0e-15); } } @Test public void testLinear() { FiniteDifferencesDifferentiator differentiator = new FiniteDifferencesDifferentiator(5, 0.01); UnivariateDifferentiableFunction f = differentiator.differentiate(new UnivariateFunction() { public double value(double x) { return 2 - 3 * x; } }); for (double x = -10; x < 10; x += 0.1) { DerivativeStructure y = f.value(new DerivativeStructure(1, 2, 0, x)); Assert.assertEquals("" + (2 - 3 * x - y.getValue()), 2 - 3 * x, y.getValue(), 2.0e-15); Assert.assertEquals(-3.0, y.getPartialDerivative(1), 4.0e-13); Assert.assertEquals( 0.0, y.getPartialDerivative(2), 9.0e-11); } } @Test public void testGaussian() { FiniteDifferencesDifferentiator differentiator = new FiniteDifferencesDifferentiator(9, 0.02); UnivariateDifferentiableFunction gaussian = new Gaussian(1.0, 2.0); UnivariateDifferentiableFunction f = differentiator.differentiate(gaussian); double[] expectedError = new double[] { 6.939e-18, 1.284e-15, 2.477e-13, 1.168e-11, 2.840e-9, 7.971e-8 }; double[] maxError = new double[expectedError.length]; for (double x = -10; x < 10; x += 0.1) { DerivativeStructure dsX = new DerivativeStructure(1, maxError.length - 1, 0, x); DerivativeStructure yRef = gaussian.value(dsX); DerivativeStructure y = f.value(dsX); Assert.assertEquals(f.value(dsX.getValue()), f.value(dsX).getValue(), 1.0e-15); for (int order = 0; order <= yRef.getOrder(); ++order) { maxError[order] = FastMath.max(maxError[order], FastMath.abs(yRef.getPartialDerivative(order) - y.getPartialDerivative(order))); } } for (int i = 0; i < maxError.length; ++i) { Assert.assertEquals(expectedError[i], maxError[i], 0.01 * expectedError[i]); } } @Test public void testStepSizeUnstability() { UnivariateDifferentiableFunction quintic = new QuinticFunction(); UnivariateDifferentiableFunction goodStep = new FiniteDifferencesDifferentiator(7, 0.25).differentiate(quintic); UnivariateDifferentiableFunction badStep = new FiniteDifferencesDifferentiator(7, 1.0e-6).differentiate(quintic); double[] maxErrorGood = new double[7]; double[] maxErrorBad = new double[7]; for (double x = -10; x < 10; x += 0.1) { DerivativeStructure dsX = new DerivativeStructure(1, 6, 0, x); DerivativeStructure yRef = quintic.value(dsX); DerivativeStructure yGood = goodStep.value(dsX); DerivativeStructure yBad = badStep.value(dsX); for (int order = 0; order <= 6; ++order) { maxErrorGood[order] = FastMath.max(maxErrorGood[order], FastMath.abs(yRef.getPartialDerivative(order) - yGood.getPartialDerivative(order))); maxErrorBad[order] = FastMath.max(maxErrorBad[order], FastMath.abs(yRef.getPartialDerivative(order) - yBad.getPartialDerivative(order))); } } // the 0.25 step size is good for finite differences in the quintic on this abscissa range for 7 points // the errors are fair final double[] expectedGood = new double[] { 7.276e-12, 7.276e-11, 9.968e-10, 3.092e-9, 5.432e-8, 8.196e-8, 1.818e-6 }; // the 1.0e-6 step size is far too small for finite differences in the quintic on this abscissa range for 7 points // the errors are huge! final double[] expectedBad = new double[] { 2.910e-11, 2.087e-5, 147.7, 3.820e7, 6.354e14, 6.548e19, 1.543e27 }; for (int i = 0; i < maxErrorGood.length; ++i) { Assert.assertEquals(expectedGood[i], maxErrorGood[i], 0.01 * expectedGood[i]); Assert.assertEquals(expectedBad[i], maxErrorBad[i], 0.01 * expectedBad[i]); } } @Test(expected=NumberIsTooLargeException.class) public void testWrongOrder() { UnivariateDifferentiableFunction f = new FiniteDifferencesDifferentiator(3, 0.01).differentiate(new UnivariateFunction() { public double value(double x) { // this exception should not be thrown because wrong order // should be detected before function call throw new MathInternalError(); } }); f.value(new DerivativeStructure(1, 3, 0, 1.0)); } @Test(expected=NumberIsTooLargeException.class) public void testWrongOrderVector() { UnivariateDifferentiableVectorFunction f = new FiniteDifferencesDifferentiator(3, 0.01).differentiate(new UnivariateVectorFunction() { public double[] value(double x) { // this exception should not be thrown because wrong order // should be detected before function call throw new MathInternalError(); } }); f.value(new DerivativeStructure(1, 3, 0, 1.0)); } @Test(expected=NumberIsTooLargeException.class) public void testWrongOrderMatrix() { UnivariateDifferentiableMatrixFunction f = new FiniteDifferencesDifferentiator(3, 0.01).differentiate(new UnivariateMatrixFunction() { public double[][] value(double x) { // this exception should not be thrown because wrong order // should be detected before function call throw new MathInternalError(); } }); f.value(new DerivativeStructure(1, 3, 0, 1.0)); } @Test(expected=NumberIsTooLargeException.class) public void testTooLargeStep() { new FiniteDifferencesDifferentiator(3, 2.5, 0.0, 1.0); } @Test public void testBounds() { final double slope = 2.5; UnivariateFunction f = new UnivariateFunction() { public double value(double x) { if (x < 0) { throw new NumberIsTooSmallException(x, 0, true); } else if (x > 1) { throw new NumberIsTooLargeException(x, 1, true); } else { return slope * x; } } }; UnivariateDifferentiableFunction missingBounds = new FiniteDifferencesDifferentiator(3, 0.1).differentiate(f); UnivariateDifferentiableFunction properlyBounded = new FiniteDifferencesDifferentiator(3, 0.1, 0.0, 1.0).differentiate(f); DerivativeStructure tLow = new DerivativeStructure(1, 1, 0, 0.05); DerivativeStructure tHigh = new DerivativeStructure(1, 1, 0, 0.95); try { // here, we did not set the bounds, so the differences are evaluated out of domain // using f(-0.05), f(0.05), f(0.15) missingBounds.value(tLow); Assert.fail("an exception should have been thrown"); } catch (NumberIsTooSmallException nse) { Assert.assertEquals(-0.05, nse.getArgument().doubleValue(), 1.0e-10); } catch (Exception e) { Assert.fail("wrong exception caught: " + e.getClass().getName()); } try { // here, we did not set the bounds, so the differences are evaluated out of domain // using f(0.85), f(0.95), f(1.05) missingBounds.value(tHigh); Assert.fail("an exception should have been thrown"); } catch (NumberIsTooLargeException nle) { Assert.assertEquals(1.05, nle.getArgument().doubleValue(), 1.0e-10); } catch (Exception e) { Assert.fail("wrong exception caught: " + e.getClass().getName()); } // here, we did set the bounds, so evaluations are done within domain // using f(0.0), f(0.1), f(0.2) Assert.assertEquals(slope, properlyBounded.value(tLow).getPartialDerivative(1), 1.0e-10); // here, we did set the bounds, so evaluations are done within domain // using f(0.8), f(0.9), f(1.0) Assert.assertEquals(slope, properlyBounded.value(tHigh).getPartialDerivative(1), 1.0e-10); } @Test public void testBoundedSqrt() { UnivariateFunctionDifferentiator differentiator = new FiniteDifferencesDifferentiator(9, 1.0 / 32, 0.0, Double.POSITIVE_INFINITY); UnivariateDifferentiableFunction sqrt = differentiator.differentiate(new UnivariateFunction() { public double value(double x) { return FastMath.sqrt(x); } }); // we are able to compute derivative near 0, but the accuracy is much poorer there DerivativeStructure t001 = new DerivativeStructure(1, 1, 0, 0.01); Assert.assertEquals(0.5 / FastMath.sqrt(t001.getValue()), sqrt.value(t001).getPartialDerivative(1), 1.6); DerivativeStructure t01 = new DerivativeStructure(1, 1, 0, 0.1); Assert.assertEquals(0.5 / FastMath.sqrt(t01.getValue()), sqrt.value(t01).getPartialDerivative(1), 7.0e-3); DerivativeStructure t03 = new DerivativeStructure(1, 1, 0, 0.3); Assert.assertEquals(0.5 / FastMath.sqrt(t03.getValue()), sqrt.value(t03).getPartialDerivative(1), 2.1e-7); } @Test public void testVectorFunction() { FiniteDifferencesDifferentiator differentiator = new FiniteDifferencesDifferentiator(7, 0.01); UnivariateDifferentiableVectorFunction f = differentiator.differentiate(new UnivariateVectorFunction() { public double[] value(double x) { return new double[] { FastMath.cos(x), FastMath.sin(x) }; } }); for (double x = -10; x < 10; x += 0.1) { DerivativeStructure dsX = new DerivativeStructure(1, 2, 0, x); DerivativeStructure[] y = f.value(dsX); double cos = FastMath.cos(x); double sin = FastMath.sin(x); double[] f1 = f.value(dsX.getValue()); DerivativeStructure[] f2 = f.value(dsX); Assert.assertEquals(f1.length, f2.length); for (int i = 0; i < f1.length; ++i) { Assert.assertEquals(f1[i], f2[i].getValue(), 1.0e-15); } Assert.assertEquals( cos, y[0].getValue(), 7.0e-16); Assert.assertEquals( sin, y[1].getValue(), 7.0e-16); Assert.assertEquals(-sin, y[0].getPartialDerivative(1), 6.0e-14); Assert.assertEquals( cos, y[1].getPartialDerivative(1), 6.0e-14); Assert.assertEquals(-cos, y[0].getPartialDerivative(2), 2.0e-11); Assert.assertEquals(-sin, y[1].getPartialDerivative(2), 2.0e-11); } } @Test public void testMatrixFunction() { FiniteDifferencesDifferentiator differentiator = new FiniteDifferencesDifferentiator(7, 0.01); UnivariateDifferentiableMatrixFunction f = differentiator.differentiate(new UnivariateMatrixFunction() { public double[][] value(double x) { return new double[][] { { FastMath.cos(x), FastMath.sin(x) }, { FastMath.cosh(x), FastMath.sinh(x) } }; } }); for (double x = -1; x < 1; x += 0.02) { DerivativeStructure dsX = new DerivativeStructure(1, 2, 0, x); DerivativeStructure[][] y = f.value(dsX); double cos = FastMath.cos(x); double sin = FastMath.sin(x); double cosh = FastMath.cosh(x); double sinh = FastMath.sinh(x); double[][] f1 = f.value(dsX.getValue()); DerivativeStructure[][] f2 = f.value(dsX); Assert.assertEquals(f1.length, f2.length); for (int i = 0; i < f1.length; ++i) { Assert.assertEquals(f1[i].length, f2[i].length); for (int j = 0; j < f1[i].length; ++j) { Assert.assertEquals(f1[i][j], f2[i][j].getValue(), 1.0e-15); } } Assert.assertEquals(cos, y[0][0].getValue(), 7.0e-18); Assert.assertEquals(sin, y[0][1].getValue(), 6.0e-17); Assert.assertEquals(cosh, y[1][0].getValue(), 3.0e-16); Assert.assertEquals(sinh, y[1][1].getValue(), 3.0e-16); Assert.assertEquals(-sin, y[0][0].getPartialDerivative(1), 2.0e-14); Assert.assertEquals( cos, y[0][1].getPartialDerivative(1), 2.0e-14); Assert.assertEquals( sinh, y[1][0].getPartialDerivative(1), 3.0e-14); Assert.assertEquals( cosh, y[1][1].getPartialDerivative(1), 3.0e-14); Assert.assertEquals(-cos, y[0][0].getPartialDerivative(2), 3.0e-12); Assert.assertEquals(-sin, y[0][1].getPartialDerivative(2), 3.0e-12); Assert.assertEquals( cosh, y[1][0].getPartialDerivative(2), 6.0e-12); Assert.assertEquals( sinh, y[1][1].getPartialDerivative(2), 6.0e-12); } } @Test public void testSeveralFreeParameters() { FiniteDifferencesDifferentiator differentiator = new FiniteDifferencesDifferentiator(5, 0.001); UnivariateDifferentiableFunction sine = new Sin(); UnivariateDifferentiableFunction f = differentiator.differentiate(sine); double[] expectedError = new double[] { 6.696e-16, 1.371e-12, 2.007e-8, 1.754e-5 }; double[] maxError = new double[expectedError.length]; for (double x = -2; x < 2; x += 0.1) { for (double y = -2; y < 2; y += 0.1) { DerivativeStructure dsX = new DerivativeStructure(2, maxError.length - 1, 0, x); DerivativeStructure dsY = new DerivativeStructure(2, maxError.length - 1, 1, y); DerivativeStructure dsT = dsX.multiply(3).subtract(dsY.multiply(2)); DerivativeStructure sRef = sine.value(dsT); DerivativeStructure s = f.value(dsT); for (int xOrder = 0; xOrder <= sRef.getOrder(); ++xOrder) { for (int yOrder = 0; yOrder <= sRef.getOrder(); ++yOrder) { if (xOrder + yOrder <= sRef.getOrder()) { maxError[xOrder +yOrder] = FastMath.max(maxError[xOrder + yOrder], FastMath.abs(sRef.getPartialDerivative(xOrder, yOrder) - s.getPartialDerivative(xOrder, yOrder))); } } } } } for (int i = 0; i < maxError.length; ++i) { Assert.assertEquals(expectedError[i], maxError[i], 0.01 * expectedError[i]); } } } ././@LongLink100644 0 0 163 12126630646 10261 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/analysis/differentiation/DerivativeStructureTest.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/analysis/differentiation/DerivativeStru100644 1750 1750 215733 12126627670 32533 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.analysis.differentiation; import java.util.Arrays; import java.util.List; import org.apache.commons.math3.ExtendedFieldElementAbstractTest; import org.apache.commons.math3.TestUtils; import org.apache.commons.math3.analysis.polynomials.PolynomialFunction; import org.apache.commons.math3.exception.DimensionMismatchException; import org.apache.commons.math3.exception.NumberIsTooLargeException; import org.apache.commons.math3.random.Well1024a; import org.apache.commons.math3.util.ArithmeticUtils; import org.apache.commons.math3.util.FastMath; import org.junit.Assert; import org.junit.Test; /** * Test for class {@link DerivativeStructure}. */ public class DerivativeStructureTest extends ExtendedFieldElementAbstractTest { protected DerivativeStructure build(final double x) { return new DerivativeStructure(2, 1, 0, x); } @Test(expected=NumberIsTooLargeException.class) public void testWrongVariableIndex() { new DerivativeStructure(3, 1, 3, 1.0); } @Test(expected=DimensionMismatchException.class) public void testMissingOrders() { new DerivativeStructure(3, 1, 0, 1.0).getPartialDerivative(0, 1); } @Test(expected=NumberIsTooLargeException.class) public void testTooLargeOrder() { new DerivativeStructure(3, 1, 0, 1.0).getPartialDerivative(1, 1, 2); } @Test public void testVariableWithoutDerivative0() { DerivativeStructure v = new DerivativeStructure(1, 0, 0, 1.0); Assert.assertEquals(1.0, v.getValue(), 1.0e-15); } @Test(expected=NumberIsTooLargeException.class) public void testVariableWithoutDerivative1() { DerivativeStructure v = new DerivativeStructure(1, 0, 0, 1.0); Assert.assertEquals(1.0, v.getPartialDerivative(1), 1.0e-15); } @Test public void testVariable() { for (int maxOrder = 1; maxOrder < 5; ++maxOrder) { checkF0F1(new DerivativeStructure(3, maxOrder, 0, 1.0), 1.0, 1.0, 0.0, 0.0); checkF0F1(new DerivativeStructure(3, maxOrder, 1, 2.0), 2.0, 0.0, 1.0, 0.0); checkF0F1(new DerivativeStructure(3, maxOrder, 2, 3.0), 3.0, 0.0, 0.0, 1.0); } } @Test public void testConstant() { for (int maxOrder = 1; maxOrder < 5; ++maxOrder) { checkF0F1(new DerivativeStructure(3, maxOrder, FastMath.PI), FastMath.PI, 0.0, 0.0, 0.0); } } @Test public void testPrimitiveAdd() { for (int maxOrder = 1; maxOrder < 5; ++maxOrder) { checkF0F1(new DerivativeStructure(3, maxOrder, 0, 1.0).add(5), 6.0, 1.0, 0.0, 0.0); checkF0F1(new DerivativeStructure(3, maxOrder, 1, 2.0).add(5), 7.0, 0.0, 1.0, 0.0); checkF0F1(new DerivativeStructure(3, maxOrder, 2, 3.0).add(5), 8.0, 0.0, 0.0, 1.0); } } @Test public void testAdd() { for (int maxOrder = 1; maxOrder < 5; ++maxOrder) { DerivativeStructure x = new DerivativeStructure(3, maxOrder, 0, 1.0); DerivativeStructure y = new DerivativeStructure(3, maxOrder, 1, 2.0); DerivativeStructure z = new DerivativeStructure(3, maxOrder, 2, 3.0); DerivativeStructure xyz = x.add(y.add(z)); checkF0F1(xyz, x.getValue() + y.getValue() + z.getValue(), 1.0, 1.0, 1.0); } } @Test public void testPrimitiveSubtract() { for (int maxOrder = 1; maxOrder < 5; ++maxOrder) { checkF0F1(new DerivativeStructure(3, maxOrder, 0, 1.0).subtract(5), -4.0, 1.0, 0.0, 0.0); checkF0F1(new DerivativeStructure(3, maxOrder, 1, 2.0).subtract(5), -3.0, 0.0, 1.0, 0.0); checkF0F1(new DerivativeStructure(3, maxOrder, 2, 3.0).subtract(5), -2.0, 0.0, 0.0, 1.0); } } @Test public void testSubtract() { for (int maxOrder = 1; maxOrder < 5; ++maxOrder) { DerivativeStructure x = new DerivativeStructure(3, maxOrder, 0, 1.0); DerivativeStructure y = new DerivativeStructure(3, maxOrder, 1, 2.0); DerivativeStructure z = new DerivativeStructure(3, maxOrder, 2, 3.0); DerivativeStructure xyz = x.subtract(y.subtract(z)); checkF0F1(xyz, x.getValue() - (y.getValue() - z.getValue()), 1.0, -1.0, 1.0); } } @Test public void testPrimitiveMultiply() { for (int maxOrder = 1; maxOrder < 5; ++maxOrder) { checkF0F1(new DerivativeStructure(3, maxOrder, 0, 1.0).multiply(5), 5.0, 5.0, 0.0, 0.0); checkF0F1(new DerivativeStructure(3, maxOrder, 1, 2.0).multiply(5), 10.0, 0.0, 5.0, 0.0); checkF0F1(new DerivativeStructure(3, maxOrder, 2, 3.0).multiply(5), 15.0, 0.0, 0.0, 5.0); } } @Test public void testMultiply() { for (int maxOrder = 1; maxOrder < 5; ++maxOrder) { DerivativeStructure x = new DerivativeStructure(3, maxOrder, 0, 1.0); DerivativeStructure y = new DerivativeStructure(3, maxOrder, 1, 2.0); DerivativeStructure z = new DerivativeStructure(3, maxOrder, 2, 3.0); DerivativeStructure xyz = x.multiply(y.multiply(z)); for (int i = 0; i <= maxOrder; ++i) { for (int j = 0; j <= maxOrder; ++j) { for (int k = 0; k <= maxOrder; ++k) { if (i + j + k <= maxOrder) { Assert.assertEquals((i == 0 ? x.getValue() : (i == 1 ? 1.0 : 0.0)) * (j == 0 ? y.getValue() : (j == 1 ? 1.0 : 0.0)) * (k == 0 ? z.getValue() : (k == 1 ? 1.0 : 0.0)), xyz.getPartialDerivative(i, j, k), 1.0e-15); } } } } } } @Test public void testNegate() { for (int maxOrder = 1; maxOrder < 5; ++maxOrder) { checkF0F1(new DerivativeStructure(3, maxOrder, 0, 1.0).negate(), -1.0, -1.0, 0.0, 0.0); checkF0F1(new DerivativeStructure(3, maxOrder, 1, 2.0).negate(), -2.0, 0.0, -1.0, 0.0); checkF0F1(new DerivativeStructure(3, maxOrder, 2, 3.0).negate(), -3.0, 0.0, 0.0, -1.0); } } @Test public void testReciprocal() { for (double x = 0.1; x < 1.2; x += 0.1) { DerivativeStructure r = new DerivativeStructure(1, 6, 0, x).reciprocal(); Assert.assertEquals(1 / x, r.getValue(), 1.0e-15); for (int i = 1; i < r.getOrder(); ++i) { double expected = ArithmeticUtils.pow(-1, i) * ArithmeticUtils.factorial(i) / FastMath.pow(x, i + 1); Assert.assertEquals(expected, r.getPartialDerivative(i), 1.0e-15 * FastMath.abs(expected)); } } } @Test public void testPow() { for (int maxOrder = 1; maxOrder < 5; ++maxOrder) { for (int n = 0; n < 10; ++n) { DerivativeStructure x = new DerivativeStructure(3, maxOrder, 0, 1.0); DerivativeStructure y = new DerivativeStructure(3, maxOrder, 1, 2.0); DerivativeStructure z = new DerivativeStructure(3, maxOrder, 2, 3.0); List list = Arrays.asList(x, y, z, x.add(y).add(z), x.multiply(y).multiply(z)); if (n == 0) { for (DerivativeStructure ds : list) { checkEquals(ds.getField().getOne(), ds.pow(n), 1.0e-15); } } else if (n == 1) { for (DerivativeStructure ds : list) { checkEquals(ds, ds.pow(n), 1.0e-15); } } else { for (DerivativeStructure ds : list) { DerivativeStructure p = ds.getField().getOne(); for (int i = 0; i < n; ++i) { p = p.multiply(ds); } checkEquals(p, ds.pow(n), 1.0e-15); } } } } } @Test public void testExpression() { double epsilon = 2.5e-13; for (double x = 0; x < 2; x += 0.2) { DerivativeStructure dsX = new DerivativeStructure(3, 5, 0, x); for (double y = 0; y < 2; y += 0.2) { DerivativeStructure dsY = new DerivativeStructure(3, 5, 1, y); for (double z = 0; z >- 2; z -= 0.2) { DerivativeStructure dsZ = new DerivativeStructure(3, 5, 2, z); // f(x, y, z) = x + 5 x y - 2 z + (8 z x - y)^3 DerivativeStructure ds = new DerivativeStructure(1, dsX, 5, dsX.multiply(dsY), -2, dsZ, 1, new DerivativeStructure(8, dsZ.multiply(dsX), -1, dsY).pow(3)); DerivativeStructure dsOther = new DerivativeStructure(1, dsX, 5, dsX.multiply(dsY), -2, dsZ).add(new DerivativeStructure(8, dsZ.multiply(dsX), -1, dsY).pow(3)); double f = x + 5 * x * y - 2 * z + FastMath.pow(8 * z * x - y, 3); Assert.assertEquals(f, ds.getValue(), FastMath.abs(epsilon * f)); Assert.assertEquals(f, dsOther.getValue(), FastMath.abs(epsilon * f)); // df/dx = 1 + 5 y + 24 (8 z x - y)^2 z double dfdx = 1 + 5 * y + 24 * z * FastMath.pow(8 * z * x - y, 2); Assert.assertEquals(dfdx, ds.getPartialDerivative(1, 0, 0), FastMath.abs(epsilon * dfdx)); Assert.assertEquals(dfdx, dsOther.getPartialDerivative(1, 0, 0), FastMath.abs(epsilon * dfdx)); // df/dxdy = 5 + 48 z*(y - 8 z x) double dfdxdy = 5 + 48 * z * (y - 8 * z * x); Assert.assertEquals(dfdxdy, ds.getPartialDerivative(1, 1, 0), FastMath.abs(epsilon * dfdxdy)); Assert.assertEquals(dfdxdy, dsOther.getPartialDerivative(1, 1, 0), FastMath.abs(epsilon * dfdxdy)); // df/dxdydz = 48 (y - 16 z x) double dfdxdydz = 48 * (y - 16 * z * x); Assert.assertEquals(dfdxdydz, ds.getPartialDerivative(1, 1, 1), FastMath.abs(epsilon * dfdxdydz)); Assert.assertEquals(dfdxdydz, dsOther.getPartialDerivative(1, 1, 1), FastMath.abs(epsilon * dfdxdydz)); } } } } @Test public void testCompositionOneVariableX() { double epsilon = 1.0e-13; for (int maxOrder = 0; maxOrder < 5; ++maxOrder) { for (double x = 0.1; x < 1.2; x += 0.1) { DerivativeStructure dsX = new DerivativeStructure(1, maxOrder, 0, x); for (double y = 0.1; y < 1.2; y += 0.1) { DerivativeStructure dsY = new DerivativeStructure(1, maxOrder, y); DerivativeStructure f = dsX.divide(dsY).sqrt(); double f0 = FastMath.sqrt(x / y); Assert.assertEquals(f0, f.getValue(), FastMath.abs(epsilon * f0)); if (f.getOrder() > 0) { double f1 = 1 / (2 * FastMath.sqrt(x * y)); Assert.assertEquals(f1, f.getPartialDerivative(1), FastMath.abs(epsilon * f1)); if (f.getOrder() > 1) { double f2 = -f1 / (2 * x); Assert.assertEquals(f2, f.getPartialDerivative(2), FastMath.abs(epsilon * f2)); if (f.getOrder() > 2) { double f3 = (f0 + x / (2 * y * f0)) / (4 * x * x * x); Assert.assertEquals(f3, f.getPartialDerivative(3), FastMath.abs(epsilon * f3)); } } } } } } } @Test public void testTrigo() { double epsilon = 2.0e-12; for (int maxOrder = 0; maxOrder < 5; ++maxOrder) { for (double x = 0.1; x < 1.2; x += 0.1) { DerivativeStructure dsX = new DerivativeStructure(3, maxOrder, 0, x); for (double y = 0.1; y < 1.2; y += 0.1) { DerivativeStructure dsY = new DerivativeStructure(3, maxOrder, 1, y); for (double z = 0.1; z < 1.2; z += 0.1) { DerivativeStructure dsZ = new DerivativeStructure(3, maxOrder, 2, z); DerivativeStructure f = dsX.divide(dsY.cos().add(dsZ.tan())).sin(); double a = FastMath.cos(y) + FastMath.tan(z); double f0 = FastMath.sin(x / a); Assert.assertEquals(f0, f.getValue(), FastMath.abs(epsilon * f0)); if (f.getOrder() > 0) { double dfdx = FastMath.cos(x / a) / a; Assert.assertEquals(dfdx, f.getPartialDerivative(1, 0, 0), FastMath.abs(epsilon * dfdx)); double dfdy = x * FastMath.sin(y) * dfdx / a; Assert.assertEquals(dfdy, f.getPartialDerivative(0, 1, 0), FastMath.abs(epsilon * dfdy)); double cz = FastMath.cos(z); double cz2 = cz * cz; double dfdz = -x * dfdx / (a * cz2); Assert.assertEquals(dfdz, f.getPartialDerivative(0, 0, 1), FastMath.abs(epsilon * dfdz)); if (f.getOrder() > 1) { double df2dx2 = -(f0 / (a * a)); Assert.assertEquals(df2dx2, f.getPartialDerivative(2, 0, 0), FastMath.abs(epsilon * df2dx2)); double df2dy2 = x * FastMath.cos(y) * dfdx / a - x * x * FastMath.sin(y) * FastMath.sin(y) * f0 / (a * a * a * a) + 2 * FastMath.sin(y) * dfdy / a; Assert.assertEquals(df2dy2, f.getPartialDerivative(0, 2, 0), FastMath.abs(epsilon * df2dy2)); double c4 = cz2 * cz2; double df2dz2 = x * (2 * a * (1 - a * cz * FastMath.sin(z)) * dfdx - x * f0 / a ) / (a * a * a * c4); Assert.assertEquals(df2dz2, f.getPartialDerivative(0, 0, 2), FastMath.abs(epsilon * df2dz2)); double df2dxdy = dfdy / x - x * FastMath.sin(y) * f0 / (a * a * a); Assert.assertEquals(df2dxdy, f.getPartialDerivative(1, 1, 0), FastMath.abs(epsilon * df2dxdy)); } } } } } } } @Test public void testSqrtDefinition() { double[] epsilon = new double[] { 5.0e-16, 5.0e-16, 2.0e-15, 5.0e-14, 2.0e-12 }; for (int maxOrder = 0; maxOrder < 5; ++maxOrder) { for (double x = 0.1; x < 1.2; x += 0.001) { DerivativeStructure dsX = new DerivativeStructure(1, maxOrder, 0, x); DerivativeStructure sqrt1 = dsX.pow(0.5); DerivativeStructure sqrt2 = dsX.sqrt(); DerivativeStructure zero = sqrt1.subtract(sqrt2); for (int n = 0; n <= maxOrder; ++n) { Assert.assertEquals(0, zero.getPartialDerivative(n), epsilon[n]); } } } } @Test public void testRootNSingularity() { for (int n = 2; n < 10; ++n) { for (int maxOrder = 0; maxOrder < 12; ++maxOrder) { DerivativeStructure dsZero = new DerivativeStructure(1, maxOrder, 0, 0.0); DerivativeStructure rootN = dsZero.rootN(n); Assert.assertEquals(0.0, rootN.getValue(), 1.0e-20); if (maxOrder > 0) { Assert.assertTrue(Double.isInfinite(rootN.getPartialDerivative(1))); Assert.assertTrue(rootN.getPartialDerivative(1) > 0); for (int order = 2; order <= maxOrder; ++order) { // the following checks shows a LIMITATION of the current implementation // we have no way to tell dsZero is a pure linear variable x = 0 // we only say: "dsZero is a structure with value = 0.0, // first derivative = 1.0, second and higher derivatives = 0.0". // Function composition rule for second derivatives is: // d2[f(g(x))]/dx2 = f''(g(x)) * [g'(x)]^2 + f'(g(x)) * g''(x) // when function f is the nth root and x = 0 we have: // f(0) = 0, f'(0) = +infinity, f''(0) = -infinity (and higher // derivatives keep switching between +infinity and -infinity) // so given that in our case dsZero represents g, we have g(x) = 0, // g'(x) = 1 and g''(x) = 0 // applying the composition rules gives: // d2[f(g(x))]/dx2 = f''(g(x)) * [g'(x)]^2 + f'(g(x)) * g''(x) // = -infinity * 1^2 + +infinity * 0 // = -infinity + NaN // = NaN // if we knew dsZero is really the x variable and not the identity // function applied to x, we would not have computed f'(g(x)) * g''(x) // and we would have found that the result was -infinity and not NaN Assert.assertTrue(Double.isNaN(rootN.getPartialDerivative(order))); } } // the following shows that the limitation explained above is NOT a bug... // if we set up the higher order derivatives for g appropriately, we do // compute the higher order derivatives of the composition correctly double[] gDerivatives = new double[ 1 + maxOrder]; gDerivatives[0] = 0.0; for (int k = 1; k <= maxOrder; ++k) { gDerivatives[k] = FastMath.pow(-1.0, k + 1); } DerivativeStructure correctRoot = new DerivativeStructure(1, maxOrder, gDerivatives).rootN(n); Assert.assertEquals(0.0, correctRoot.getValue(), 1.0e-20); if (maxOrder > 0) { Assert.assertTrue(Double.isInfinite(correctRoot.getPartialDerivative(1))); Assert.assertTrue(correctRoot.getPartialDerivative(1) > 0); for (int order = 2; order <= maxOrder; ++order) { Assert.assertTrue(Double.isInfinite(correctRoot.getPartialDerivative(order))); if ((order % 2) == 0) { Assert.assertTrue(correctRoot.getPartialDerivative(order) < 0); } else { Assert.assertTrue(correctRoot.getPartialDerivative(order) > 0); } } } } } } @Test public void testSqrtPow2() { double[] epsilon = new double[] { 1.0e-16, 3.0e-16, 2.0e-15, 6.0e-14, 6.0e-12 }; for (int maxOrder = 0; maxOrder < 5; ++maxOrder) { for (double x = 0.1; x < 1.2; x += 0.001) { DerivativeStructure dsX = new DerivativeStructure(1, maxOrder, 0, x); DerivativeStructure rebuiltX = dsX.multiply(dsX).sqrt(); DerivativeStructure zero = rebuiltX.subtract(dsX); for (int n = 0; n <= maxOrder; ++n) { Assert.assertEquals(0.0, zero.getPartialDerivative(n), epsilon[n]); } } } } @Test public void testCbrtDefinition() { double[] epsilon = new double[] { 4.0e-16, 9.0e-16, 6.0e-15, 2.0e-13, 4.0e-12 }; for (int maxOrder = 0; maxOrder < 5; ++maxOrder) { for (double x = 0.1; x < 1.2; x += 0.001) { DerivativeStructure dsX = new DerivativeStructure(1, maxOrder, 0, x); DerivativeStructure cbrt1 = dsX.pow(1.0 / 3.0); DerivativeStructure cbrt2 = dsX.cbrt(); DerivativeStructure zero = cbrt1.subtract(cbrt2); for (int n = 0; n <= maxOrder; ++n) { Assert.assertEquals(0, zero.getPartialDerivative(n), epsilon[n]); } } } } @Test public void testCbrtPow3() { double[] epsilon = new double[] { 1.0e-16, 5.0e-16, 8.0e-15, 3.0e-13, 4.0e-11 }; for (int maxOrder = 0; maxOrder < 5; ++maxOrder) { for (double x = 0.1; x < 1.2; x += 0.001) { DerivativeStructure dsX = new DerivativeStructure(1, maxOrder, 0, x); DerivativeStructure rebuiltX = dsX.multiply(dsX.multiply(dsX)).cbrt(); DerivativeStructure zero = rebuiltX.subtract(dsX); for (int n = 0; n <= maxOrder; ++n) { Assert.assertEquals(0.0, zero.getPartialDerivative(n), epsilon[n]); } } } } @Test public void testPowReciprocalPow() { double[] epsilon = new double[] { 2.0e-15, 2.0e-14, 3.0e-13, 8.0e-12, 3.0e-10 }; for (int maxOrder = 0; maxOrder < 5; ++maxOrder) { for (double x = 0.1; x < 1.2; x += 0.01) { DerivativeStructure dsX = new DerivativeStructure(2, maxOrder, 0, x); for (double y = 0.1; y < 1.2; y += 0.01) { DerivativeStructure dsY = new DerivativeStructure(2, maxOrder, 1, y); DerivativeStructure rebuiltX = dsX.pow(dsY).pow(dsY.reciprocal()); DerivativeStructure zero = rebuiltX.subtract(dsX); for (int n = 0; n <= maxOrder; ++n) { for (int m = 0; m <= maxOrder; ++m) { if (n + m <= maxOrder) { Assert.assertEquals(0.0, zero.getPartialDerivative(n, m), epsilon[n + m]); } } } } } } } @Test public void testHypotDefinition() { double epsilon = 1.0e-20; for (int maxOrder = 0; maxOrder < 5; ++maxOrder) { for (double x = -1.7; x < 2; x += 0.2) { DerivativeStructure dsX = new DerivativeStructure(2, maxOrder, 0, x); for (double y = -1.7; y < 2; y += 0.2) { DerivativeStructure dsY = new DerivativeStructure(2, maxOrder, 1, y); DerivativeStructure hypot = DerivativeStructure.hypot(dsY, dsX); DerivativeStructure ref = dsX.multiply(dsX).add(dsY.multiply(dsY)).sqrt(); DerivativeStructure zero = hypot.subtract(ref); for (int n = 0; n <= maxOrder; ++n) { for (int m = 0; m <= maxOrder; ++m) { if (n + m <= maxOrder) { Assert.assertEquals(0, zero.getPartialDerivative(n, m), epsilon); } } } } } } } @Test public void testHypotNoOverflow() { DerivativeStructure dsX = new DerivativeStructure(2, 5, 0, +3.0e250); DerivativeStructure dsY = new DerivativeStructure(2, 5, 1, -4.0e250); DerivativeStructure hypot = DerivativeStructure.hypot(dsX, dsY); Assert.assertEquals(5.0e250, hypot.getValue(), 1.0e235); Assert.assertEquals(dsX.getValue() / hypot.getValue(), hypot.getPartialDerivative(1, 0), 1.0e-10); Assert.assertEquals(dsY.getValue() / hypot.getValue(), hypot.getPartialDerivative(0, 1), 1.0e-10); DerivativeStructure sqrt = dsX.multiply(dsX).add(dsY.multiply(dsY)).sqrt(); Assert.assertTrue(Double.isInfinite(sqrt.getValue())); } @Test public void testHypotNeglectible() { DerivativeStructure dsSmall = new DerivativeStructure(2, 5, 0, +3.0e-10); DerivativeStructure dsLarge = new DerivativeStructure(2, 5, 1, -4.0e25); Assert.assertEquals(dsLarge.abs().getValue(), DerivativeStructure.hypot(dsSmall, dsLarge).getValue(), 1.0e-10); Assert.assertEquals(0, DerivativeStructure.hypot(dsSmall, dsLarge).getPartialDerivative(1, 0), 1.0e-10); Assert.assertEquals(-1, DerivativeStructure.hypot(dsSmall, dsLarge).getPartialDerivative(0, 1), 1.0e-10); Assert.assertEquals(dsLarge.abs().getValue(), DerivativeStructure.hypot(dsLarge, dsSmall).getValue(), 1.0e-10); Assert.assertEquals(0, DerivativeStructure.hypot(dsLarge, dsSmall).getPartialDerivative(1, 0), 1.0e-10); Assert.assertEquals(-1, DerivativeStructure.hypot(dsLarge, dsSmall).getPartialDerivative(0, 1), 1.0e-10); } @Test public void testHypotSpecial() { Assert.assertTrue(Double.isNaN(DerivativeStructure.hypot(new DerivativeStructure(2, 5, 0, Double.NaN), new DerivativeStructure(2, 5, 0, +3.0e250)).getValue())); Assert.assertTrue(Double.isNaN(DerivativeStructure.hypot(new DerivativeStructure(2, 5, 0, +3.0e250), new DerivativeStructure(2, 5, 0, Double.NaN)).getValue())); Assert.assertTrue(Double.isInfinite(DerivativeStructure.hypot(new DerivativeStructure(2, 5, 0, Double.POSITIVE_INFINITY), new DerivativeStructure(2, 5, 0, +3.0e250)).getValue())); Assert.assertTrue(Double.isInfinite(DerivativeStructure.hypot(new DerivativeStructure(2, 5, 0, +3.0e250), new DerivativeStructure(2, 5, 0, Double.POSITIVE_INFINITY)).getValue())); } @Test public void testPrimitiveRemainder() { double epsilon = 1.0e-15; for (int maxOrder = 0; maxOrder < 5; ++maxOrder) { for (double x = -1.7; x < 2; x += 0.2) { DerivativeStructure dsX = new DerivativeStructure(2, maxOrder, 0, x); for (double y = -1.7; y < 2; y += 0.2) { DerivativeStructure remainder = dsX.remainder(y); DerivativeStructure ref = dsX.subtract(x - FastMath.IEEEremainder(x, y)); DerivativeStructure zero = remainder.subtract(ref); for (int n = 0; n <= maxOrder; ++n) { for (int m = 0; m <= maxOrder; ++m) { if (n + m <= maxOrder) { Assert.assertEquals(0, zero.getPartialDerivative(n, m), epsilon); } } } } } } } @Test public void testRemainder() { double epsilon = 2.0e-15; for (int maxOrder = 0; maxOrder < 5; ++maxOrder) { for (double x = -1.7; x < 2; x += 0.2) { DerivativeStructure dsX = new DerivativeStructure(2, maxOrder, 0, x); for (double y = -1.7; y < 2; y += 0.2) { DerivativeStructure dsY = new DerivativeStructure(2, maxOrder, 1, y); DerivativeStructure remainder = dsX.remainder(dsY); DerivativeStructure ref = dsX.subtract(dsY.multiply((x - FastMath.IEEEremainder(x, y)) / y)); DerivativeStructure zero = remainder.subtract(ref); for (int n = 0; n <= maxOrder; ++n) { for (int m = 0; m <= maxOrder; ++m) { if (n + m <= maxOrder) { Assert.assertEquals(0, zero.getPartialDerivative(n, m), epsilon); } } } } } } } @Test public void testExp() { double[] epsilon = new double[] { 1.0e-16, 1.0e-16, 1.0e-16, 1.0e-16, 1.0e-16 }; for (int maxOrder = 0; maxOrder < 5; ++maxOrder) { for (double x = 0.1; x < 1.2; x += 0.001) { double refExp = FastMath.exp(x); DerivativeStructure exp = new DerivativeStructure(1, maxOrder, 0, x).exp(); for (int n = 0; n <= maxOrder; ++n) { Assert.assertEquals(refExp, exp.getPartialDerivative(n), epsilon[n]); } } } } @Test public void testExpm1Definition() { double epsilon = 3.0e-16; for (int maxOrder = 0; maxOrder < 5; ++maxOrder) { for (double x = 0.1; x < 1.2; x += 0.001) { DerivativeStructure dsX = new DerivativeStructure(1, maxOrder, 0, x); DerivativeStructure expm11 = dsX.expm1(); DerivativeStructure expm12 = dsX.exp().subtract(dsX.getField().getOne()); DerivativeStructure zero = expm11.subtract(expm12); for (int n = 0; n <= maxOrder; ++n) { Assert.assertEquals(0, zero.getPartialDerivative(n), epsilon); } } } } @Test public void testLog() { double[] epsilon = new double[] { 1.0e-16, 1.0e-16, 3.0e-14, 7.0e-13, 3.0e-11 }; for (int maxOrder = 0; maxOrder < 5; ++maxOrder) { for (double x = 0.1; x < 1.2; x += 0.001) { DerivativeStructure log = new DerivativeStructure(1, maxOrder, 0, x).log(); Assert.assertEquals(FastMath.log(x), log.getValue(), epsilon[0]); for (int n = 1; n <= maxOrder; ++n) { double refDer = -ArithmeticUtils.factorial(n - 1) / FastMath.pow(-x, n); Assert.assertEquals(refDer, log.getPartialDerivative(n), epsilon[n]); } } } } @Test public void testLog1pDefinition() { double epsilon = 3.0e-16; for (int maxOrder = 0; maxOrder < 5; ++maxOrder) { for (double x = 0.1; x < 1.2; x += 0.001) { DerivativeStructure dsX = new DerivativeStructure(1, maxOrder, 0, x); DerivativeStructure log1p1 = dsX.log1p(); DerivativeStructure log1p2 = dsX.add(dsX.getField().getOne()).log(); DerivativeStructure zero = log1p1.subtract(log1p2); for (int n = 0; n <= maxOrder; ++n) { Assert.assertEquals(0, zero.getPartialDerivative(n), epsilon); } } } } @Test public void testLog10Definition() { double[] epsilon = new double[] { 3.0e-16, 3.0e-16, 8.0e-15, 3.0e-13, 8.0e-12 }; for (int maxOrder = 0; maxOrder < 5; ++maxOrder) { for (double x = 0.1; x < 1.2; x += 0.001) { DerivativeStructure dsX = new DerivativeStructure(1, maxOrder, 0, x); DerivativeStructure log101 = dsX.log10(); DerivativeStructure log102 = dsX.log().divide(FastMath.log(10.0)); DerivativeStructure zero = log101.subtract(log102); for (int n = 0; n <= maxOrder; ++n) { Assert.assertEquals(0, zero.getPartialDerivative(n), epsilon[n]); } } } } @Test public void testLogExp() { double[] epsilon = new double[] { 2.0e-16, 2.0e-16, 3.0e-16, 2.0e-15, 6.0e-15 }; for (int maxOrder = 0; maxOrder < 5; ++maxOrder) { for (double x = 0.1; x < 1.2; x += 0.001) { DerivativeStructure dsX = new DerivativeStructure(1, maxOrder, 0, x); DerivativeStructure rebuiltX = dsX.exp().log(); DerivativeStructure zero = rebuiltX.subtract(dsX); for (int n = 0; n <= maxOrder; ++n) { Assert.assertEquals(0.0, zero.getPartialDerivative(n), epsilon[n]); } } } } @Test public void testLog1pExpm1() { double[] epsilon = new double[] { 6.0e-17, 3.0e-16, 5.0e-16, 9.0e-16, 6.0e-15 }; for (int maxOrder = 0; maxOrder < 5; ++maxOrder) { for (double x = 0.1; x < 1.2; x += 0.001) { DerivativeStructure dsX = new DerivativeStructure(1, maxOrder, 0, x); DerivativeStructure rebuiltX = dsX.expm1().log1p(); DerivativeStructure zero = rebuiltX.subtract(dsX); for (int n = 0; n <= maxOrder; ++n) { Assert.assertEquals(0.0, zero.getPartialDerivative(n), epsilon[n]); } } } } @Test public void testLog10Power() { double[] epsilon = new double[] { 3.0e-16, 3.0e-16, 9.0e-16, 6.0e-15, 6.0e-14 }; for (int maxOrder = 0; maxOrder < 5; ++maxOrder) { for (double x = 0.1; x < 1.2; x += 0.001) { DerivativeStructure dsX = new DerivativeStructure(1, maxOrder, 0, x); DerivativeStructure rebuiltX = new DerivativeStructure(1, maxOrder, 10.0).pow(dsX).log10(); DerivativeStructure zero = rebuiltX.subtract(dsX); for (int n = 0; n <= maxOrder; ++n) { Assert.assertEquals(0, zero.getPartialDerivative(n), epsilon[n]); } } } } @Test public void testSinCos() { double epsilon = 5.0e-16; for (int maxOrder = 0; maxOrder < 6; ++maxOrder) { for (double x = 0.1; x < 1.2; x += 0.001) { DerivativeStructure dsX = new DerivativeStructure(1, maxOrder, 0, x); DerivativeStructure sin = dsX.sin(); DerivativeStructure cos = dsX.cos(); double s = FastMath.sin(x); double c = FastMath.cos(x); for (int n = 0; n <= maxOrder; ++n) { switch (n % 4) { case 0 : Assert.assertEquals( s, sin.getPartialDerivative(n), epsilon); Assert.assertEquals( c, cos.getPartialDerivative(n), epsilon); break; case 1 : Assert.assertEquals( c, sin.getPartialDerivative(n), epsilon); Assert.assertEquals(-s, cos.getPartialDerivative(n), epsilon); break; case 2 : Assert.assertEquals(-s, sin.getPartialDerivative(n), epsilon); Assert.assertEquals(-c, cos.getPartialDerivative(n), epsilon); break; default : Assert.assertEquals(-c, sin.getPartialDerivative(n), epsilon); Assert.assertEquals( s, cos.getPartialDerivative(n), epsilon); break; } } } } } @Test public void testSinAsin() { double[] epsilon = new double[] { 3.0e-16, 5.0e-16, 3.0e-15, 2.0e-14, 4.0e-13 }; for (int maxOrder = 0; maxOrder < 5; ++maxOrder) { for (double x = 0.1; x < 1.2; x += 0.001) { DerivativeStructure dsX = new DerivativeStructure(1, maxOrder, 0, x); DerivativeStructure rebuiltX = dsX.sin().asin(); DerivativeStructure zero = rebuiltX.subtract(dsX); for (int n = 0; n <= maxOrder; ++n) { Assert.assertEquals(0.0, zero.getPartialDerivative(n), epsilon[n]); } } } } @Test public void testCosAcos() { double[] epsilon = new double[] { 6.0e-16, 6.0e-15, 2.0e-13, 4.0e-12, 2.0e-10 }; for (int maxOrder = 0; maxOrder < 5; ++maxOrder) { for (double x = 0.1; x < 1.2; x += 0.001) { DerivativeStructure dsX = new DerivativeStructure(1, maxOrder, 0, x); DerivativeStructure rebuiltX = dsX.cos().acos(); DerivativeStructure zero = rebuiltX.subtract(dsX); for (int n = 0; n <= maxOrder; ++n) { Assert.assertEquals(0.0, zero.getPartialDerivative(n), epsilon[n]); } } } } @Test public void testTanAtan() { double[] epsilon = new double[] { 6.0e-17, 2.0e-16, 2.0e-15, 4.0e-14, 2.0e-12 }; for (int maxOrder = 0; maxOrder < 5; ++maxOrder) { for (double x = 0.1; x < 1.2; x += 0.001) { DerivativeStructure dsX = new DerivativeStructure(1, maxOrder, 0, x); DerivativeStructure rebuiltX = dsX.tan().atan(); DerivativeStructure zero = rebuiltX.subtract(dsX); for (int n = 0; n <= maxOrder; ++n) { Assert.assertEquals(0.0, zero.getPartialDerivative(n), epsilon[n]); } } } } @Test public void testTangentDefinition() { double[] epsilon = new double[] { 5.0e-16, 2.0e-15, 3.0e-14, 5.0e-13, 2.0e-11 }; for (int maxOrder = 0; maxOrder < 5; ++maxOrder) { for (double x = 0.1; x < 1.2; x += 0.001) { DerivativeStructure dsX = new DerivativeStructure(1, maxOrder, 0, x); DerivativeStructure tan1 = dsX.sin().divide(dsX.cos()); DerivativeStructure tan2 = dsX.tan(); DerivativeStructure zero = tan1.subtract(tan2); for (int n = 0; n <= maxOrder; ++n) { Assert.assertEquals(0, zero.getPartialDerivative(n), epsilon[n]); } } } } @Test public void testAtan2() { double[] epsilon = new double[] { 5.0e-16, 3.0e-15, 2.2e-14, 1.0e-12, 8.0e-11 }; for (int maxOrder = 0; maxOrder < 5; ++maxOrder) { for (double x = -1.7; x < 2; x += 0.2) { DerivativeStructure dsX = new DerivativeStructure(2, maxOrder, 0, x); for (double y = -1.7; y < 2; y += 0.2) { DerivativeStructure dsY = new DerivativeStructure(2, maxOrder, 1, y); DerivativeStructure atan2 = DerivativeStructure.atan2(dsY, dsX); DerivativeStructure ref = dsY.divide(dsX).atan(); if (x < 0) { ref = (y < 0) ? ref.subtract(FastMath.PI) : ref.add(FastMath.PI); } DerivativeStructure zero = atan2.subtract(ref); for (int n = 0; n <= maxOrder; ++n) { for (int m = 0; m <= maxOrder; ++m) { if (n + m <= maxOrder) { Assert.assertEquals(0, zero.getPartialDerivative(n, m), epsilon[n + m]); } } } } } } } @Test public void testAtan2SpecialCases() { DerivativeStructure pp = DerivativeStructure.atan2(new DerivativeStructure(2, 2, 1, +0.0), new DerivativeStructure(2, 2, 1, +0.0)); Assert.assertEquals(0, pp.getValue(), 1.0e-15); Assert.assertEquals(+1, FastMath.copySign(1, pp.getValue()), 1.0e-15); DerivativeStructure pn = DerivativeStructure.atan2(new DerivativeStructure(2, 2, 1, +0.0), new DerivativeStructure(2, 2, 1, -0.0)); Assert.assertEquals(FastMath.PI, pn.getValue(), 1.0e-15); DerivativeStructure np = DerivativeStructure.atan2(new DerivativeStructure(2, 2, 1, -0.0), new DerivativeStructure(2, 2, 1, +0.0)); Assert.assertEquals(0, np.getValue(), 1.0e-15); Assert.assertEquals(-1, FastMath.copySign(1, np.getValue()), 1.0e-15); DerivativeStructure nn = DerivativeStructure.atan2(new DerivativeStructure(2, 2, 1, -0.0), new DerivativeStructure(2, 2, 1, -0.0)); Assert.assertEquals(-FastMath.PI, nn.getValue(), 1.0e-15); } @Test public void testSinhDefinition() { double[] epsilon = new double[] { 3.0e-16, 3.0e-16, 5.0e-16, 2.0e-15, 6.0e-15 }; for (int maxOrder = 0; maxOrder < 5; ++maxOrder) { for (double x = 0.1; x < 1.2; x += 0.001) { DerivativeStructure dsX = new DerivativeStructure(1, maxOrder, 0, x); DerivativeStructure sinh1 = dsX.exp().subtract(dsX.exp().reciprocal()).multiply(0.5); DerivativeStructure sinh2 = dsX.sinh(); DerivativeStructure zero = sinh1.subtract(sinh2); for (int n = 0; n <= maxOrder; ++n) { Assert.assertEquals(0, zero.getPartialDerivative(n), epsilon[n]); } } } } @Test public void testCoshDefinition() { double[] epsilon = new double[] { 3.0e-16, 3.0e-16, 5.0e-16, 2.0e-15, 6.0e-15 }; for (int maxOrder = 0; maxOrder < 5; ++maxOrder) { for (double x = 0.1; x < 1.2; x += 0.001) { DerivativeStructure dsX = new DerivativeStructure(1, maxOrder, 0, x); DerivativeStructure cosh1 = dsX.exp().add(dsX.exp().reciprocal()).multiply(0.5); DerivativeStructure cosh2 = dsX.cosh(); DerivativeStructure zero = cosh1.subtract(cosh2); for (int n = 0; n <= maxOrder; ++n) { Assert.assertEquals(0, zero.getPartialDerivative(n), epsilon[n]); } } } } @Test public void testTanhDefinition() { double[] epsilon = new double[] { 3.0e-16, 5.0e-16, 7.0e-16, 3.0e-15, 2.0e-14 }; for (int maxOrder = 0; maxOrder < 5; ++maxOrder) { for (double x = 0.1; x < 1.2; x += 0.001) { DerivativeStructure dsX = new DerivativeStructure(1, maxOrder, 0, x); DerivativeStructure tanh1 = dsX.exp().subtract(dsX.exp().reciprocal()).divide(dsX.exp().add(dsX.exp().reciprocal())); DerivativeStructure tanh2 = dsX.tanh(); DerivativeStructure zero = tanh1.subtract(tanh2); for (int n = 0; n <= maxOrder; ++n) { Assert.assertEquals(0, zero.getPartialDerivative(n), epsilon[n]); } } } } @Test public void testSinhAsinh() { double[] epsilon = new double[] { 3.0e-16, 3.0e-16, 4.0e-16, 7.0e-16, 3.0e-15, 8.0e-15 }; for (int maxOrder = 0; maxOrder < 6; ++maxOrder) { for (double x = 0.1; x < 1.2; x += 0.001) { DerivativeStructure dsX = new DerivativeStructure(1, maxOrder, 0, x); DerivativeStructure rebuiltX = dsX.sinh().asinh(); DerivativeStructure zero = rebuiltX.subtract(dsX); for (int n = 0; n <= maxOrder; ++n) { Assert.assertEquals(0.0, zero.getPartialDerivative(n), epsilon[n]); } } } } @Test public void testCoshAcosh() { double[] epsilon = new double[] { 2.0e-15, 1.0e-14, 2.0e-13, 6.0e-12, 3.0e-10, 2.0e-8 }; for (int maxOrder = 0; maxOrder < 6; ++maxOrder) { for (double x = 0.1; x < 1.2; x += 0.001) { DerivativeStructure dsX = new DerivativeStructure(1, maxOrder, 0, x); DerivativeStructure rebuiltX = dsX.cosh().acosh(); DerivativeStructure zero = rebuiltX.subtract(dsX); for (int n = 0; n <= maxOrder; ++n) { Assert.assertEquals(0.0, zero.getPartialDerivative(n), epsilon[n]); } } } } @Test public void testTanhAtanh() { double[] epsilon = new double[] { 3.0e-16, 2.0e-16, 7.0e-16, 4.0e-15, 3.0e-14, 4.0e-13 }; for (int maxOrder = 0; maxOrder < 6; ++maxOrder) { for (double x = 0.1; x < 1.2; x += 0.001) { DerivativeStructure dsX = new DerivativeStructure(1, maxOrder, 0, x); DerivativeStructure rebuiltX = dsX.tanh().atanh(); DerivativeStructure zero = rebuiltX.subtract(dsX); for (int n = 0; n <= maxOrder; ++n) { Assert.assertEquals(0.0, zero.getPartialDerivative(n), epsilon[n]); } } } } @Test public void testCompositionOneVariableY() { double epsilon = 1.0e-13; for (int maxOrder = 0; maxOrder < 5; ++maxOrder) { for (double x = 0.1; x < 1.2; x += 0.1) { DerivativeStructure dsX = new DerivativeStructure(1, maxOrder, x); for (double y = 0.1; y < 1.2; y += 0.1) { DerivativeStructure dsY = new DerivativeStructure(1, maxOrder, 0, y); DerivativeStructure f = dsX.divide(dsY).sqrt(); double f0 = FastMath.sqrt(x / y); Assert.assertEquals(f0, f.getValue(), FastMath.abs(epsilon * f0)); if (f.getOrder() > 0) { double f1 = -x / (2 * y * y * f0); Assert.assertEquals(f1, f.getPartialDerivative(1), FastMath.abs(epsilon * f1)); if (f.getOrder() > 1) { double f2 = (f0 - x / (4 * y * f0)) / (y * y); Assert.assertEquals(f2, f.getPartialDerivative(2), FastMath.abs(epsilon * f2)); if (f.getOrder() > 2) { double f3 = (x / (8 * y * f0) - 2 * f0) / (y * y * y); Assert.assertEquals(f3, f.getPartialDerivative(3), FastMath.abs(epsilon * f3)); } } } } } } } @Test public void testTaylorPolynomial() { for (double x = 0; x < 1.2; x += 0.1) { DerivativeStructure dsX = new DerivativeStructure(3, 4, 0, x); for (double y = 0; y < 1.2; y += 0.2) { DerivativeStructure dsY = new DerivativeStructure(3, 4, 1, y); for (double z = 0; z < 1.2; z += 0.2) { DerivativeStructure dsZ = new DerivativeStructure(3, 4, 2, z); DerivativeStructure f = dsX.multiply(dsY).add(dsZ).multiply(dsX).multiply(dsY); for (double dx = -0.2; dx < 0.2; dx += 0.2) { for (double dy = -0.2; dy < 0.2; dy += 0.1) { for (double dz = -0.2; dz < 0.2; dz += 0.1) { double ref = (x + dx) * (y + dy) * ((x + dx) * (y + dy) + (z + dz)); Assert.assertEquals(ref, f.taylor(dx, dy, dz), 2.0e-15); } } } } } } } @Test public void testTaylorAtan2() { double[] expected = new double[] { 0.214, 0.0241, 0.00422, 6.48e-4, 8.04e-5 }; double x0 = 0.1; double y0 = -0.3; for (int maxOrder = 0; maxOrder < 5; ++maxOrder) { DerivativeStructure dsX = new DerivativeStructure(2, maxOrder, 0, x0); DerivativeStructure dsY = new DerivativeStructure(2, maxOrder, 1, y0); DerivativeStructure atan2 = DerivativeStructure.atan2(dsY, dsX); double maxError = 0; for (double dx = -0.05; dx < 0.05; dx += 0.001) { for (double dy = -0.05; dy < 0.05; dy += 0.001) { double ref = FastMath.atan2(y0 + dy, x0 + dx); maxError = FastMath.max(maxError, FastMath.abs(ref - atan2.taylor(dx, dy))); } } Assert.assertEquals(0.0, expected[maxOrder] - maxError, 0.01 * expected[maxOrder]); } } @Test public void testAbs() { DerivativeStructure minusOne = new DerivativeStructure(1, 1, 0, -1.0); Assert.assertEquals(+1.0, minusOne.abs().getPartialDerivative(0), 1.0e-15); Assert.assertEquals(-1.0, minusOne.abs().getPartialDerivative(1), 1.0e-15); DerivativeStructure plusOne = new DerivativeStructure(1, 1, 0, +1.0); Assert.assertEquals(+1.0, plusOne.abs().getPartialDerivative(0), 1.0e-15); Assert.assertEquals(+1.0, plusOne.abs().getPartialDerivative(1), 1.0e-15); DerivativeStructure minusZero = new DerivativeStructure(1, 1, 0, -0.0); Assert.assertEquals(+0.0, minusZero.abs().getPartialDerivative(0), 1.0e-15); Assert.assertEquals(-1.0, minusZero.abs().getPartialDerivative(1), 1.0e-15); DerivativeStructure plusZero = new DerivativeStructure(1, 1, 0, +0.0); Assert.assertEquals(+0.0, plusZero.abs().getPartialDerivative(0), 1.0e-15); Assert.assertEquals(+1.0, plusZero.abs().getPartialDerivative(1), 1.0e-15); } @Test public void testSignum() { DerivativeStructure minusOne = new DerivativeStructure(1, 1, 0, -1.0); Assert.assertEquals(-1.0, minusOne.signum().getPartialDerivative(0), 1.0e-15); Assert.assertEquals( 0.0, minusOne.signum().getPartialDerivative(1), 1.0e-15); DerivativeStructure plusOne = new DerivativeStructure(1, 1, 0, +1.0); Assert.assertEquals(+1.0, plusOne.signum().getPartialDerivative(0), 1.0e-15); Assert.assertEquals( 0.0, plusOne.signum().getPartialDerivative(1), 1.0e-15); DerivativeStructure minusZero = new DerivativeStructure(1, 1, 0, -0.0); Assert.assertEquals(-0.0, minusZero.signum().getPartialDerivative(0), 1.0e-15); Assert.assertTrue(Double.doubleToLongBits(minusZero.signum().getValue()) < 0); Assert.assertEquals( 0.0, minusZero.signum().getPartialDerivative(1), 1.0e-15); DerivativeStructure plusZero = new DerivativeStructure(1, 1, 0, +0.0); Assert.assertEquals(+0.0, plusZero.signum().getPartialDerivative(0), 1.0e-15); Assert.assertTrue(Double.doubleToLongBits(plusZero.signum().getValue()) == 0); Assert.assertEquals( 0.0, plusZero.signum().getPartialDerivative(1), 1.0e-15); } @Test public void testCeilFloorRintLong() { DerivativeStructure x = new DerivativeStructure(1, 1, 0, -1.5); Assert.assertEquals(-1.5, x.getPartialDerivative(0), 1.0e-15); Assert.assertEquals(+1.0, x.getPartialDerivative(1), 1.0e-15); Assert.assertEquals(-1.0, x.ceil().getPartialDerivative(0), 1.0e-15); Assert.assertEquals(+0.0, x.ceil().getPartialDerivative(1), 1.0e-15); Assert.assertEquals(-2.0, x.floor().getPartialDerivative(0), 1.0e-15); Assert.assertEquals(+0.0, x.floor().getPartialDerivative(1), 1.0e-15); Assert.assertEquals(-2.0, x.rint().getPartialDerivative(0), 1.0e-15); Assert.assertEquals(+0.0, x.rint().getPartialDerivative(1), 1.0e-15); Assert.assertEquals(-2.0, x.subtract(x.getField().getOne()).rint().getPartialDerivative(0), 1.0e-15); Assert.assertEquals(-1l, x.round()); } @Test public void testCopySign() { DerivativeStructure minusOne = new DerivativeStructure(1, 1, 0, -1.0); Assert.assertEquals(+1.0, minusOne.copySign(+1.0).getPartialDerivative(0), 1.0e-15); Assert.assertEquals(-1.0, minusOne.copySign(+1.0).getPartialDerivative(1), 1.0e-15); Assert.assertEquals(-1.0, minusOne.copySign(-1.0).getPartialDerivative(0), 1.0e-15); Assert.assertEquals(+1.0, minusOne.copySign(-1.0).getPartialDerivative(1), 1.0e-15); Assert.assertEquals(+1.0, minusOne.copySign(+0.0).getPartialDerivative(0), 1.0e-15); Assert.assertEquals(-1.0, minusOne.copySign(+0.0).getPartialDerivative(1), 1.0e-15); Assert.assertEquals(-1.0, minusOne.copySign(-0.0).getPartialDerivative(0), 1.0e-15); Assert.assertEquals(+1.0, minusOne.copySign(-0.0).getPartialDerivative(1), 1.0e-15); Assert.assertEquals(+1.0, minusOne.copySign(Double.NaN).getPartialDerivative(0), 1.0e-15); Assert.assertEquals(-1.0, minusOne.copySign(Double.NaN).getPartialDerivative(1), 1.0e-15); DerivativeStructure plusOne = new DerivativeStructure(1, 1, 0, +1.0); Assert.assertEquals(+1.0, plusOne.copySign(+1.0).getPartialDerivative(0), 1.0e-15); Assert.assertEquals(+1.0, plusOne.copySign(+1.0).getPartialDerivative(1), 1.0e-15); Assert.assertEquals(-1.0, plusOne.copySign(-1.0).getPartialDerivative(0), 1.0e-15); Assert.assertEquals(-1.0, plusOne.copySign(-1.0).getPartialDerivative(1), 1.0e-15); Assert.assertEquals(+1.0, plusOne.copySign(+0.0).getPartialDerivative(0), 1.0e-15); Assert.assertEquals(+1.0, plusOne.copySign(+0.0).getPartialDerivative(1), 1.0e-15); Assert.assertEquals(-1.0, plusOne.copySign(-0.0).getPartialDerivative(0), 1.0e-15); Assert.assertEquals(-1.0, plusOne.copySign(-0.0).getPartialDerivative(1), 1.0e-15); Assert.assertEquals(+1.0, plusOne.copySign(Double.NaN).getPartialDerivative(0), 1.0e-15); Assert.assertEquals(+1.0, plusOne.copySign(Double.NaN).getPartialDerivative(1), 1.0e-15); } @Test public void testToDegreesDefinition() { double epsilon = 3.0e-16; for (int maxOrder = 0; maxOrder < 6; ++maxOrder) { for (double x = 0.1; x < 1.2; x += 0.001) { DerivativeStructure dsX = new DerivativeStructure(1, maxOrder, 0, x); Assert.assertEquals(FastMath.toDegrees(x), dsX.toDegrees().getValue(), epsilon); for (int n = 1; n <= maxOrder; ++n) { if (n == 1) { Assert.assertEquals(180 / FastMath.PI, dsX.toDegrees().getPartialDerivative(1), epsilon); } else { Assert.assertEquals(0.0, dsX.toDegrees().getPartialDerivative(n), epsilon); } } } } } @Test public void testToRadiansDefinition() { double epsilon = 3.0e-16; for (int maxOrder = 0; maxOrder < 6; ++maxOrder) { for (double x = 0.1; x < 1.2; x += 0.001) { DerivativeStructure dsX = new DerivativeStructure(1, maxOrder, 0, x); Assert.assertEquals(FastMath.toRadians(x), dsX.toRadians().getValue(), epsilon); for (int n = 1; n <= maxOrder; ++n) { if (n == 1) { Assert.assertEquals(FastMath.PI / 180, dsX.toRadians().getPartialDerivative(1), epsilon); } else { Assert.assertEquals(0.0, dsX.toRadians().getPartialDerivative(n), epsilon); } } } } } @Test public void testDegRad() { double epsilon = 3.0e-16; for (int maxOrder = 0; maxOrder < 6; ++maxOrder) { for (double x = 0.1; x < 1.2; x += 0.001) { DerivativeStructure dsX = new DerivativeStructure(1, maxOrder, 0, x); DerivativeStructure rebuiltX = dsX.toDegrees().toRadians(); DerivativeStructure zero = rebuiltX.subtract(dsX); for (int n = 0; n <= maxOrder; ++n) { Assert.assertEquals(0.0, zero.getPartialDerivative(n), epsilon); } } } } @Test(expected=DimensionMismatchException.class) public void testComposeMismatchedDimensions() { new DerivativeStructure(1, 3, 0, 1.2).compose(new double[3]); } @Test public void testCompose() { double[] epsilon = new double[] { 1.0e-20, 5.0e-14, 2.0e-13, 3.0e-13, 2.0e-13, 1.0e-20 }; PolynomialFunction poly = new PolynomialFunction(new double[] { 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 }); for (int maxOrder = 0; maxOrder < 6; ++maxOrder) { PolynomialFunction[] p = new PolynomialFunction[maxOrder + 1]; p[0] = poly; for (int i = 1; i <= maxOrder; ++i) { p[i] = p[i - 1].polynomialDerivative(); } for (double x = 0.1; x < 1.2; x += 0.001) { DerivativeStructure dsX = new DerivativeStructure(1, maxOrder, 0, x); DerivativeStructure dsY1 = dsX.getField().getZero(); for (int i = poly.degree(); i >= 0; --i) { dsY1 = dsY1.multiply(dsX).add(poly.getCoefficients()[i]); } double[] f = new double[maxOrder + 1]; for (int i = 0; i < f.length; ++i) { f[i] = p[i].value(x); } DerivativeStructure dsY2 = dsX.compose(f); DerivativeStructure zero = dsY1.subtract(dsY2); for (int n = 0; n <= maxOrder; ++n) { Assert.assertEquals(0.0, zero.getPartialDerivative(n), epsilon[n]); } } } } @Test public void testField() { for (int maxOrder = 1; maxOrder < 5; ++maxOrder) { DerivativeStructure x = new DerivativeStructure(3, maxOrder, 0, 1.0); checkF0F1(x.getField().getZero(), 0.0, 0.0, 0.0, 0.0); checkF0F1(x.getField().getOne(), 1.0, 0.0, 0.0, 0.0); Assert.assertEquals(maxOrder, x.getField().getZero().getOrder()); Assert.assertEquals(3, x.getField().getZero().getFreeParameters()); Assert.assertEquals(DerivativeStructure.class, x.getField().getRuntimeClass()); } } @Test public void testOneParameterConstructor() { double x = 1.2; double cos = FastMath.cos(x); double sin = FastMath.sin(x); DerivativeStructure yRef = new DerivativeStructure(1, 4, 0, x).cos(); try { new DerivativeStructure(1, 4, 0.0, 0.0); Assert.fail("an exception should have been thrown"); } catch (DimensionMismatchException dme) { // expected } catch (Exception e) { Assert.fail("wrong exceptionc caught " + e.getClass().getName()); } double[] derivatives = new double[] { cos, -sin, -cos, sin, cos }; DerivativeStructure y = new DerivativeStructure(1, 4, derivatives); checkEquals(yRef, y, 1.0e-15); TestUtils.assertEquals(derivatives, y.getAllDerivatives(), 1.0e-15); } @Test public void testOneOrderConstructor() { double x = 1.2; double y = 2.4; double z = 12.5; DerivativeStructure xRef = new DerivativeStructure(3, 1, 0, x); DerivativeStructure yRef = new DerivativeStructure(3, 1, 1, y); DerivativeStructure zRef = new DerivativeStructure(3, 1, 2, z); try { new DerivativeStructure(3, 1, x + y - z, 1.0, 1.0); Assert.fail("an exception should have been thrown"); } catch (DimensionMismatchException dme) { // expected } catch (Exception e) { Assert.fail("wrong exceptionc caught " + e.getClass().getName()); } double[] derivatives = new double[] { x + y - z, 1.0, 1.0, -1.0 }; DerivativeStructure t = new DerivativeStructure(3, 1, derivatives); checkEquals(xRef.add(yRef.subtract(zRef)), t, 1.0e-15); TestUtils.assertEquals(derivatives, xRef.add(yRef.subtract(zRef)).getAllDerivatives(), 1.0e-15); } @Test public void testLinearCombination1DSDS() { final DerivativeStructure[] a = new DerivativeStructure[] { new DerivativeStructure(6, 1, 0, -1321008684645961.0 / 268435456.0), new DerivativeStructure(6, 1, 1, -5774608829631843.0 / 268435456.0), new DerivativeStructure(6, 1, 2, -7645843051051357.0 / 8589934592.0) }; final DerivativeStructure[] b = new DerivativeStructure[] { new DerivativeStructure(6, 1, 3, -5712344449280879.0 / 2097152.0), new DerivativeStructure(6, 1, 4, -4550117129121957.0 / 2097152.0), new DerivativeStructure(6, 1, 5, 8846951984510141.0 / 131072.0) }; final DerivativeStructure abSumInline = a[0].linearCombination(a[0], b[0], a[1], b[1], a[2], b[2]); final DerivativeStructure abSumArray = a[0].linearCombination(a, b); Assert.assertEquals(abSumInline.getValue(), abSumArray.getValue(), 0); Assert.assertEquals(-1.8551294182586248737720779899, abSumInline.getValue(), 1.0e-15); Assert.assertEquals(b[0].getValue(), abSumInline.getPartialDerivative(1, 0, 0, 0, 0, 0), 1.0e-15); Assert.assertEquals(b[1].getValue(), abSumInline.getPartialDerivative(0, 1, 0, 0, 0, 0), 1.0e-15); Assert.assertEquals(b[2].getValue(), abSumInline.getPartialDerivative(0, 0, 1, 0, 0, 0), 1.0e-15); Assert.assertEquals(a[0].getValue(), abSumInline.getPartialDerivative(0, 0, 0, 1, 0, 0), 1.0e-15); Assert.assertEquals(a[1].getValue(), abSumInline.getPartialDerivative(0, 0, 0, 0, 1, 0), 1.0e-15); Assert.assertEquals(a[2].getValue(), abSumInline.getPartialDerivative(0, 0, 0, 0, 0, 1), 1.0e-15); } @Test public void testLinearCombination1DoubleDS() { final double[] a = new double[] { -1321008684645961.0 / 268435456.0, -5774608829631843.0 / 268435456.0, -7645843051051357.0 / 8589934592.0 }; final DerivativeStructure[] b = new DerivativeStructure[] { new DerivativeStructure(3, 1, 0, -5712344449280879.0 / 2097152.0), new DerivativeStructure(3, 1, 1, -4550117129121957.0 / 2097152.0), new DerivativeStructure(3, 1, 2, 8846951984510141.0 / 131072.0) }; final DerivativeStructure abSumInline = b[0].linearCombination(a[0], b[0], a[1], b[1], a[2], b[2]); final DerivativeStructure abSumArray = b[0].linearCombination(a, b); Assert.assertEquals(abSumInline.getValue(), abSumArray.getValue(), 0); Assert.assertEquals(-1.8551294182586248737720779899, abSumInline.getValue(), 1.0e-15); Assert.assertEquals(a[0], abSumInline.getPartialDerivative(1, 0, 0), 1.0e-15); Assert.assertEquals(a[1], abSumInline.getPartialDerivative(0, 1, 0), 1.0e-15); Assert.assertEquals(a[2], abSumInline.getPartialDerivative(0, 0, 1), 1.0e-15); } @Test public void testLinearCombination2DSDS() { // we compare accurate versus naive dot product implementations // on regular vectors (i.e. not extreme cases like in the previous test) Well1024a random = new Well1024a(0xc6af886975069f11l); for (int i = 0; i < 10000; ++i) { final DerivativeStructure[] u = new DerivativeStructure[4]; final DerivativeStructure[] v = new DerivativeStructure[4]; for (int j = 0; j < u.length; ++j) { u[j] = new DerivativeStructure(u.length, 1, j, 1e17 * random.nextDouble()); v[j] = new DerivativeStructure(u.length, 1, 1e17 * random.nextDouble()); } DerivativeStructure lin = u[0].linearCombination(u[0], v[0], u[1], v[1]); double ref = u[0].getValue() * v[0].getValue() + u[1].getValue() * v[1].getValue(); Assert.assertEquals(ref, lin.getValue(), 1.0e-15 * FastMath.abs(ref)); Assert.assertEquals(v[0].getValue(), lin.getPartialDerivative(1, 0, 0, 0), 1.0e-15 * FastMath.abs(v[0].getValue())); Assert.assertEquals(v[1].getValue(), lin.getPartialDerivative(0, 1, 0, 0), 1.0e-15 * FastMath.abs(v[1].getValue())); lin = u[0].linearCombination(u[0], v[0], u[1], v[1], u[2], v[2]); ref = u[0].getValue() * v[0].getValue() + u[1].getValue() * v[1].getValue() + u[2].getValue() * v[2].getValue(); Assert.assertEquals(ref, lin.getValue(), 1.0e-15 * FastMath.abs(ref)); Assert.assertEquals(v[0].getValue(), lin.getPartialDerivative(1, 0, 0, 0), 1.0e-15 * FastMath.abs(v[0].getValue())); Assert.assertEquals(v[1].getValue(), lin.getPartialDerivative(0, 1, 0, 0), 1.0e-15 * FastMath.abs(v[1].getValue())); Assert.assertEquals(v[2].getValue(), lin.getPartialDerivative(0, 0, 1, 0), 1.0e-15 * FastMath.abs(v[2].getValue())); lin = u[0].linearCombination(u[0], v[0], u[1], v[1], u[2], v[2], u[3], v[3]); ref = u[0].getValue() * v[0].getValue() + u[1].getValue() * v[1].getValue() + u[2].getValue() * v[2].getValue() + u[3].getValue() * v[3].getValue(); Assert.assertEquals(ref, lin.getValue(), 1.0e-15 * FastMath.abs(ref)); Assert.assertEquals(v[0].getValue(), lin.getPartialDerivative(1, 0, 0, 0), 1.0e-15 * FastMath.abs(v[0].getValue())); Assert.assertEquals(v[1].getValue(), lin.getPartialDerivative(0, 1, 0, 0), 1.0e-15 * FastMath.abs(v[1].getValue())); Assert.assertEquals(v[2].getValue(), lin.getPartialDerivative(0, 0, 1, 0), 1.0e-15 * FastMath.abs(v[2].getValue())); Assert.assertEquals(v[3].getValue(), lin.getPartialDerivative(0, 0, 0, 1), 1.0e-15 * FastMath.abs(v[3].getValue())); } } @Test public void testLinearCombination2DoubleDS() { // we compare accurate versus naive dot product implementations // on regular vectors (i.e. not extreme cases like in the previous test) Well1024a random = new Well1024a(0xc6af886975069f11l); for (int i = 0; i < 10000; ++i) { final double[] u = new double[4]; final DerivativeStructure[] v = new DerivativeStructure[4]; for (int j = 0; j < u.length; ++j) { u[j] = 1e17 * random.nextDouble(); v[j] = new DerivativeStructure(u.length, 1, j, 1e17 * random.nextDouble()); } DerivativeStructure lin = v[0].linearCombination(u[0], v[0], u[1], v[1]); double ref = u[0] * v[0].getValue() + u[1] * v[1].getValue(); Assert.assertEquals(ref, lin.getValue(), 1.0e-15 * FastMath.abs(ref)); Assert.assertEquals(u[0], lin.getPartialDerivative(1, 0, 0, 0), 1.0e-15 * FastMath.abs(v[0].getValue())); Assert.assertEquals(u[1], lin.getPartialDerivative(0, 1, 0, 0), 1.0e-15 * FastMath.abs(v[1].getValue())); lin = v[0].linearCombination(u[0], v[0], u[1], v[1], u[2], v[2]); ref = u[0] * v[0].getValue() + u[1] * v[1].getValue() + u[2] * v[2].getValue(); Assert.assertEquals(ref, lin.getValue(), 1.0e-15 * FastMath.abs(ref)); Assert.assertEquals(u[0], lin.getPartialDerivative(1, 0, 0, 0), 1.0e-15 * FastMath.abs(v[0].getValue())); Assert.assertEquals(u[1], lin.getPartialDerivative(0, 1, 0, 0), 1.0e-15 * FastMath.abs(v[1].getValue())); Assert.assertEquals(u[2], lin.getPartialDerivative(0, 0, 1, 0), 1.0e-15 * FastMath.abs(v[2].getValue())); lin = v[0].linearCombination(u[0], v[0], u[1], v[1], u[2], v[2], u[3], v[3]); ref = u[0] * v[0].getValue() + u[1] * v[1].getValue() + u[2] * v[2].getValue() + u[3] * v[3].getValue(); Assert.assertEquals(ref, lin.getValue(), 1.0e-15 * FastMath.abs(ref)); Assert.assertEquals(u[0], lin.getPartialDerivative(1, 0, 0, 0), 1.0e-15 * FastMath.abs(v[0].getValue())); Assert.assertEquals(u[1], lin.getPartialDerivative(0, 1, 0, 0), 1.0e-15 * FastMath.abs(v[1].getValue())); Assert.assertEquals(u[2], lin.getPartialDerivative(0, 0, 1, 0), 1.0e-15 * FastMath.abs(v[2].getValue())); Assert.assertEquals(u[3], lin.getPartialDerivative(0, 0, 0, 1), 1.0e-15 * FastMath.abs(v[3].getValue())); } } @Test public void testSerialization() { DerivativeStructure a = new DerivativeStructure(3, 2, 0, 1.3); DerivativeStructure b = (DerivativeStructure) TestUtils.serializeAndRecover(a); Assert.assertEquals(a.getFreeParameters(), b.getFreeParameters()); Assert.assertEquals(a.getOrder(), b.getOrder()); checkEquals(a, b, 1.0e-15); } private void checkF0F1(DerivativeStructure ds, double value, double...derivatives) { // check dimension Assert.assertEquals(derivatives.length, ds.getFreeParameters()); // check value, directly and also as 0th order derivative Assert.assertEquals(value, ds.getValue(), 1.0e-15); Assert.assertEquals(value, ds.getPartialDerivative(new int[ds.getFreeParameters()]), 1.0e-15); // check first order derivatives for (int i = 0; i < derivatives.length; ++i) { int[] orders = new int[derivatives.length]; orders[i] = 1; Assert.assertEquals(derivatives[i], ds.getPartialDerivative(orders), 1.0e-15); } } private void checkEquals(DerivativeStructure ds1, DerivativeStructure ds2, double epsilon) { // check dimension Assert.assertEquals(ds1.getFreeParameters(), ds2.getFreeParameters()); Assert.assertEquals(ds1.getOrder(), ds2.getOrder()); int[] derivatives = new int[ds1.getFreeParameters()]; int sum = 0; while (true) { if (sum <= ds1.getOrder()) { Assert.assertEquals(ds1.getPartialDerivative(derivatives), ds2.getPartialDerivative(derivatives), epsilon); } boolean increment = true; sum = 0; for (int i = derivatives.length - 1; i >= 0; --i) { if (increment) { if (derivatives[i] == ds1.getOrder()) { derivatives[i] = 0; } else { derivatives[i]++; increment = false; } } sum += derivatives[i]; } if (increment) { return; } } } } ././@LongLink100644 0 0 160 12126630646 10256 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/analysis/differentiation/JacobianFunctionTest.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/analysis/differentiation/JacobianFuncti100644 1750 1750 7120 12126627670 32357 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.analysis.differentiation; import org.junit.Assert; import org.apache.commons.math3.TestUtils; import org.apache.commons.math3.util.FastMath; import org.junit.Test; /** * Test for class {@link JacobianFunction}. */ public class JacobianFunctionTest { @Test public void testSphere() { SphereMapping f = new SphereMapping(10.0); JacobianFunction j = new JacobianFunction(f); for (double latitude = -1.5; latitude < 1.5; latitude += 0.1) { for (double longitude = -3.1; longitude < 3.1; longitude += 0.1) { double[] point = new double[] { latitude, longitude }; double[][] referenceJacobian = f.jacobian(point); double[][] testJacobian = j.value(point); Assert.assertEquals(referenceJacobian.length, testJacobian.length); for (int i = 0; i < 3; ++i) { TestUtils.assertEquals(referenceJacobian[i], testJacobian[i], 2.0e-15); } } } } /* Maps (latitude, longitude) to (x, y, z) */ private static class SphereMapping implements MultivariateDifferentiableVectorFunction { private final double radius; public SphereMapping(final double radius) { this.radius = radius; } public double[] value(double[] point) { final double cLat = FastMath.cos(point[0]); final double sLat = FastMath.sin(point[0]); final double cLon = FastMath.cos(point[1]); final double sLon = FastMath.sin(point[1]); return new double[] { radius * cLon * cLat, radius * sLon * cLat, radius * sLat }; } public DerivativeStructure[] value(DerivativeStructure[] point) { final DerivativeStructure cLat = point[0].cos(); final DerivativeStructure sLat = point[0].sin(); final DerivativeStructure cLon = point[1].cos(); final DerivativeStructure sLon = point[1].sin(); return new DerivativeStructure[] { cLon.multiply(cLat).multiply(radius), sLon.multiply(cLat).multiply(radius), sLat.multiply(radius) }; } public double[][] jacobian(double[] point) { final double cLat = FastMath.cos(point[0]); final double sLat = FastMath.sin(point[0]); final double cLon = FastMath.cos(point[1]); final double sLon = FastMath.sin(point[1]); return new double[][] { { -radius * cLon * sLat, -radius * sLon * cLat }, { -radius * sLon * sLat, radius * cLon * cLat }, { radius * cLat, 0 } }; } } } commons-math3-3.2-src/src/test/java/org/apache/commons/math3/analysis/FunctionUtilsTest.java100644 1750 1750 36120 12126627670 30736 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.analysis; import org.apache.commons.math3.analysis.differentiation.DerivativeStructure; import org.apache.commons.math3.analysis.differentiation.MultivariateDifferentiableFunction; import org.apache.commons.math3.analysis.differentiation.UnivariateDifferentiableFunction; import org.apache.commons.math3.analysis.function.Add; import org.apache.commons.math3.analysis.function.Constant; import org.apache.commons.math3.analysis.function.Cos; import org.apache.commons.math3.analysis.function.Cosh; import org.apache.commons.math3.analysis.function.Divide; import org.apache.commons.math3.analysis.function.Identity; import org.apache.commons.math3.analysis.function.Inverse; import org.apache.commons.math3.analysis.function.Log; import org.apache.commons.math3.analysis.function.Max; import org.apache.commons.math3.analysis.function.Min; import org.apache.commons.math3.analysis.function.Minus; import org.apache.commons.math3.analysis.function.Multiply; import org.apache.commons.math3.analysis.function.Pow; import org.apache.commons.math3.analysis.function.Power; import org.apache.commons.math3.analysis.function.Sin; import org.apache.commons.math3.analysis.function.Sinc; import org.apache.commons.math3.exception.NotStrictlyPositiveException; import org.apache.commons.math3.exception.NumberIsTooLargeException; import org.apache.commons.math3.util.FastMath; import org.junit.Assert; import org.junit.Test; /** * Test for {@link FunctionUtils}. */ public class FunctionUtilsTest { private final double EPS = Math.ulp(1d); @Test public void testCompose() { UnivariateFunction id = new Identity(); Assert.assertEquals(3, FunctionUtils.compose(id, id, id).value(3), EPS); UnivariateFunction c = new Constant(4); Assert.assertEquals(4, FunctionUtils.compose(id, c).value(3), EPS); Assert.assertEquals(4, FunctionUtils.compose(c, id).value(3), EPS); UnivariateFunction m = new Minus(); Assert.assertEquals(-3, FunctionUtils.compose(m).value(3), EPS); Assert.assertEquals(3, FunctionUtils.compose(m, m).value(3), EPS); UnivariateFunction inv = new Inverse(); Assert.assertEquals(-0.25, FunctionUtils.compose(inv, m, c, id).value(3), EPS); UnivariateFunction pow = new Power(2); Assert.assertEquals(81, FunctionUtils.compose(pow, pow).value(3), EPS); } @Test public void testComposeDifferentiable() { UnivariateDifferentiableFunction id = new Identity(); Assert.assertEquals(1, FunctionUtils.compose(id, id, id).value(new DerivativeStructure(1, 1, 0, 3)).getPartialDerivative(1), EPS); UnivariateDifferentiableFunction c = new Constant(4); Assert.assertEquals(0, FunctionUtils.compose(id, c).value(new DerivativeStructure(1, 1, 0, 3)).getPartialDerivative(1), EPS); Assert.assertEquals(0, FunctionUtils.compose(c, id).value(new DerivativeStructure(1, 1, 0, 3)).getPartialDerivative(1), EPS); UnivariateDifferentiableFunction m = new Minus(); Assert.assertEquals(-1, FunctionUtils.compose(m).value(new DerivativeStructure(1, 1, 0, 3)).getPartialDerivative(1), EPS); Assert.assertEquals(1, FunctionUtils.compose(m, m).value(new DerivativeStructure(1, 1, 0, 3)).getPartialDerivative(1), EPS); UnivariateDifferentiableFunction inv = new Inverse(); Assert.assertEquals(0.25, FunctionUtils.compose(inv, m, id).value(new DerivativeStructure(1, 1, 0, 2)).getPartialDerivative(1), EPS); UnivariateDifferentiableFunction pow = new Power(2); Assert.assertEquals(108, FunctionUtils.compose(pow, pow).value(new DerivativeStructure(1, 1, 0, 3)).getPartialDerivative(1), EPS); UnivariateDifferentiableFunction log = new Log(); double a = 9876.54321; Assert.assertEquals(pow.value(new DerivativeStructure(1, 1, 0, a)).getPartialDerivative(1) / pow.value(a), FunctionUtils.compose(log, pow).value(new DerivativeStructure(1, 1, 0, a)).getPartialDerivative(1), EPS); } @Test public void testAdd() { UnivariateFunction id = new Identity(); UnivariateFunction c = new Constant(4); UnivariateFunction m = new Minus(); UnivariateFunction inv = new Inverse(); Assert.assertEquals(4.5, FunctionUtils.add(inv, m, c, id).value(2), EPS); Assert.assertEquals(4 + 2, FunctionUtils.add(c, id).value(2), EPS); Assert.assertEquals(4 - 2, FunctionUtils.add(c, FunctionUtils.compose(m, id)).value(2), EPS); } @Test public void testAddDifferentiable() { UnivariateDifferentiableFunction sin = new Sin(); UnivariateDifferentiableFunction c = new Constant(4); UnivariateDifferentiableFunction m = new Minus(); UnivariateDifferentiableFunction inv = new Inverse(); final double a = 123.456; Assert.assertEquals(- 1 / (a * a) -1 + Math.cos(a), FunctionUtils.add(inv, m, c, sin).value(new DerivativeStructure(1, 1, 0, a)).getPartialDerivative(1), EPS); } @Test public void testMultiply() { UnivariateFunction c = new Constant(4); Assert.assertEquals(16, FunctionUtils.multiply(c, c).value(12345), EPS); UnivariateFunction inv = new Inverse(); UnivariateFunction pow = new Power(2); Assert.assertEquals(1, FunctionUtils.multiply(FunctionUtils.compose(inv, pow), pow).value(3.5), EPS); } @Test public void testMultiplyDifferentiable() { UnivariateDifferentiableFunction c = new Constant(4); UnivariateDifferentiableFunction id = new Identity(); final double a = 1.2345678; Assert.assertEquals(8 * a, FunctionUtils.multiply(c, id, id).value(new DerivativeStructure(1, 1, 0, a)).getPartialDerivative(1), EPS); UnivariateDifferentiableFunction inv = new Inverse(); UnivariateDifferentiableFunction pow = new Power(2.5); UnivariateDifferentiableFunction cos = new Cos(); Assert.assertEquals(1.5 * Math.sqrt(a) * Math.cos(a) - Math.pow(a, 1.5) * Math.sin(a), FunctionUtils.multiply(inv, pow, cos).value(new DerivativeStructure(1, 1, 0, a)).getPartialDerivative(1), EPS); UnivariateDifferentiableFunction cosh = new Cosh(); Assert.assertEquals(1.5 * Math.sqrt(a) * Math.cosh(a) + Math.pow(a, 1.5) * Math.sinh(a), FunctionUtils.multiply(inv, pow, cosh).value(new DerivativeStructure(1, 1, 0, a)).getPartialDerivative(1), 8 * EPS); } @Test public void testCombine() { BivariateFunction bi = new Add(); UnivariateFunction id = new Identity(); UnivariateFunction m = new Minus(); UnivariateFunction c = FunctionUtils.combine(bi, id, m); Assert.assertEquals(0, c.value(2.3456), EPS); bi = new Multiply(); UnivariateFunction inv = new Inverse(); c = FunctionUtils.combine(bi, id, inv); Assert.assertEquals(1, c.value(2.3456), EPS); } @Test public void testCollector() { BivariateFunction bi = new Add(); MultivariateFunction coll = FunctionUtils.collector(bi, 0); Assert.assertEquals(10, coll.value(new double[] {1, 2, 3, 4}), EPS); bi = new Multiply(); coll = FunctionUtils.collector(bi, 1); Assert.assertEquals(24, coll.value(new double[] {1, 2, 3, 4}), EPS); bi = new Max(); coll = FunctionUtils.collector(bi, Double.NEGATIVE_INFINITY); Assert.assertEquals(10, coll.value(new double[] {1, -2, 7.5, 10, -24, 9.99}), 0); bi = new Min(); coll = FunctionUtils.collector(bi, Double.POSITIVE_INFINITY); Assert.assertEquals(-24, coll.value(new double[] {1, -2, 7.5, 10, -24, 9.99}), 0); } @Test public void testSinc() { BivariateFunction div = new Divide(); UnivariateFunction sin = new Sin(); UnivariateFunction id = new Identity(); UnivariateFunction sinc1 = FunctionUtils.combine(div, sin, id); UnivariateFunction sinc2 = new Sinc(); for (int i = 0; i < 10; i++) { double x = Math.random(); Assert.assertEquals(sinc1.value(x), sinc2.value(x), EPS); } } @Test public void testFixingArguments() { UnivariateFunction scaler = FunctionUtils.fix1stArgument(new Multiply(), 10); Assert.assertEquals(1.23456, scaler.value(0.123456), EPS); UnivariateFunction pow1 = new Power(2); UnivariateFunction pow2 = FunctionUtils.fix2ndArgument(new Pow(), 2); for (int i = 0; i < 10; i++) { double x = Math.random() * 10; Assert.assertEquals(pow1.value(x), pow2.value(x), 0); } } @Test(expected = NumberIsTooLargeException.class) public void testSampleWrongBounds(){ FunctionUtils.sample(new Sin(), Math.PI, 0.0, 10); } @Test(expected = NotStrictlyPositiveException.class) public void testSampleNegativeNumberOfPoints(){ FunctionUtils.sample(new Sin(), 0.0, Math.PI, -1); } @Test(expected = NotStrictlyPositiveException.class) public void testSampleNullNumberOfPoints(){ FunctionUtils.sample(new Sin(), 0.0, Math.PI, 0); } @Test public void testSample() { final int n = 11; final double min = 0.0; final double max = Math.PI; final double[] actual = FunctionUtils.sample(new Sin(), min, max, n); for (int i = 0; i < n; i++) { final double x = min + (max - min) / n * i; Assert.assertEquals("x = " + x, FastMath.sin(x), actual[i], 0.0); } } @Test @Deprecated public void testToDifferentiableUnivariateFunction() { // Sin implements both UnivariateDifferentiableFunction and DifferentiableUnivariateFunction Sin sin = new Sin(); DifferentiableUnivariateFunction converted = FunctionUtils.toDifferentiableUnivariateFunction(sin); for (double x = 0.1; x < 0.5; x += 0.01) { Assert.assertEquals(sin.value(x), converted.value(x), 1.0e-10); Assert.assertEquals(sin.derivative().value(x), converted.derivative().value(x), 1.0e-10); } } @Test @Deprecated public void testToUnivariateDifferential() { // Sin implements both UnivariateDifferentiableFunction and DifferentiableUnivariateFunction Sin sin = new Sin(); UnivariateDifferentiableFunction converted = FunctionUtils.toUnivariateDifferential(sin); for (double x = 0.1; x < 0.5; x += 0.01) { DerivativeStructure t = new DerivativeStructure(2, 1, x, 1.0, 2.0); Assert.assertEquals(sin.value(t).getValue(), converted.value(t).getValue(), 1.0e-10); Assert.assertEquals(sin.value(t).getPartialDerivative(1, 0), converted.value(t).getPartialDerivative(1, 0), 1.0e-10); Assert.assertEquals(sin.value(t).getPartialDerivative(0, 1), converted.value(t).getPartialDerivative(0, 1), 1.0e-10); } } @Test @Deprecated public void testToDifferentiableMultivariateFunction() { MultivariateDifferentiableFunction hypot = new MultivariateDifferentiableFunction() { public double value(double[] point) { return FastMath.hypot(point[0], point[1]); } public DerivativeStructure value(DerivativeStructure[] point) { return DerivativeStructure.hypot(point[0], point[1]); } }; DifferentiableMultivariateFunction converted = FunctionUtils.toDifferentiableMultivariateFunction(hypot); for (double x = 0.1; x < 0.5; x += 0.01) { for (double y = 0.1; y < 0.5; y += 0.01) { double[] point = new double[] { x, y }; Assert.assertEquals(hypot.value(point), converted.value(point), 1.0e-10); Assert.assertEquals(x / hypot.value(point), converted.gradient().value(point)[0], 1.0e-10); Assert.assertEquals(y / hypot.value(point), converted.gradient().value(point)[1], 1.0e-10); } } } @Test @Deprecated public void testToMultivariateDifferentiableFunction() { DifferentiableMultivariateFunction hypot = new DifferentiableMultivariateFunction() { public double value(double[] point) { return FastMath.hypot(point[0], point[1]); } public MultivariateFunction partialDerivative(final int k) { return new MultivariateFunction() { public double value(double[] point) { return point[k] / FastMath.hypot(point[0], point[1]); } }; } public MultivariateVectorFunction gradient() { return new MultivariateVectorFunction() { public double[] value(double[] point) { final double h = FastMath.hypot(point[0], point[1]); return new double[] { point[0] / h, point[1] / h }; } }; } }; MultivariateDifferentiableFunction converted = FunctionUtils.toMultivariateDifferentiableFunction(hypot); for (double x = 0.1; x < 0.5; x += 0.01) { for (double y = 0.1; y < 0.5; y += 0.01) { DerivativeStructure[] t = new DerivativeStructure[] { new DerivativeStructure(3, 1, x, 1.0, 2.0, 3.0 ), new DerivativeStructure(3, 1, y, 4.0, 5.0, 6.0 ) }; DerivativeStructure h = DerivativeStructure.hypot(t[0], t[1]); Assert.assertEquals(h.getValue(), converted.value(t).getValue(), 1.0e-10); Assert.assertEquals(h.getPartialDerivative(1, 0, 0), converted.value(t).getPartialDerivative(1, 0, 0), 1.0e-10); Assert.assertEquals(h.getPartialDerivative(0, 1, 0), converted.value(t).getPartialDerivative(0, 1, 0), 1.0e-10); Assert.assertEquals(h.getPartialDerivative(0, 0, 1), converted.value(t).getPartialDerivative(0, 0, 1), 1.0e-10); } } } } ././@LongLink100644 0 0 151 12126630646 10256 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/analysis/solvers/RegulaFalsiSolverTest.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/analysis/solvers/RegulaFalsiSolverTest.100644 1750 1750 4272 12126627670 32337 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.analysis.solvers; import org.apache.commons.math3.analysis.UnivariateFunction; import org.apache.commons.math3.exception.ConvergenceException; import org.junit.Test; import org.junit.Assert; /** * Test case for {@link RegulaFalsiSolver Regula Falsi} solver. * * @version $Id$ */ public final class RegulaFalsiSolverTest extends BaseSecantSolverAbstractTest { /** {@inheritDoc} */ @Override protected UnivariateSolver getSolver() { return new RegulaFalsiSolver(); } /** {@inheritDoc} */ @Override protected int[] getQuinticEvalCounts() { // While the Regula Falsi method guarantees convergence, convergence // may be extremely slow. The last test case does not converge within // even a million iterations. As such, it was disabled. return new int[] {3, 7, 8, 19, 18, 11, 67, 55, 288, 151, -1}; } @Test(expected=ConvergenceException.class) public void testIssue631() { final UnivariateFunction f = new UnivariateFunction() { /** {@inheritDoc} */ public double value(double x) { return Math.exp(x) - Math.pow(Math.PI, 3.0); } }; final UnivariateSolver solver = new RegulaFalsiSolver(); final double root = solver.solve(3624, f, 1, 10); Assert.assertEquals(3.4341896575482003, root, 1e-15); } } ././@LongLink100644 0 0 153 12126630646 10260 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/analysis/solvers/NewtonRaphsonSolverTest.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/analysis/solvers/NewtonRaphsonSolverTes100644 1750 1750 6507 12126627670 32507 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.analysis.solvers; import org.apache.commons.math3.analysis.QuinticFunction; import org.apache.commons.math3.analysis.differentiation.UnivariateDifferentiableFunction; import org.apache.commons.math3.analysis.function.Sin; import org.apache.commons.math3.util.FastMath; import org.junit.Assert; import org.junit.Test; /** * @version $Id: NewtonRaphsonSolverTest.java 1383441 2012-09-11 14:56:39Z luc $ */ public final class NewtonRaphsonSolverTest { /** * */ @Test public void testSinZero() { UnivariateDifferentiableFunction f = new Sin(); double result; NewtonRaphsonSolver solver = new NewtonRaphsonSolver(); result = solver.solve(100, f, 3, 4); Assert.assertEquals(result, FastMath.PI, solver.getAbsoluteAccuracy()); result = solver.solve(100, f, 1, 4); Assert.assertEquals(result, FastMath.PI, solver.getAbsoluteAccuracy()); Assert.assertTrue(solver.getEvaluations() > 0); } /** * */ @Test public void testQuinticZero() { final UnivariateDifferentiableFunction f = new QuinticFunction(); double result; NewtonRaphsonSolver solver = new NewtonRaphsonSolver(); result = solver.solve(100, f, -0.2, 0.2); Assert.assertEquals(result, 0, solver.getAbsoluteAccuracy()); result = solver.solve(100, f, -0.1, 0.3); Assert.assertEquals(result, 0, solver.getAbsoluteAccuracy()); result = solver.solve(100, f, -0.3, 0.45); Assert.assertEquals(result, 0, solver.getAbsoluteAccuracy()); result = solver.solve(100, f, 0.3, 0.7); Assert.assertEquals(result, 0.5, solver.getAbsoluteAccuracy()); result = solver.solve(100, f, 0.2, 0.6); Assert.assertEquals(result, 0.5, solver.getAbsoluteAccuracy()); result = solver.solve(100, f, 0.05, 0.95); Assert.assertEquals(result, 0.5, solver.getAbsoluteAccuracy()); result = solver.solve(100, f, 0.85, 1.25); Assert.assertEquals(result, 1.0, solver.getAbsoluteAccuracy()); result = solver.solve(100, f, 0.8, 1.2); Assert.assertEquals(result, 1.0, solver.getAbsoluteAccuracy()); result = solver.solve(100, f, 0.85, 1.75); Assert.assertEquals(result, 1.0, solver.getAbsoluteAccuracy()); result = solver.solve(100, f, 0.55, 1.45); Assert.assertEquals(result, 1.0, solver.getAbsoluteAccuracy()); result = solver.solve(100, f, 0.85, 5); Assert.assertEquals(result, 1.0, solver.getAbsoluteAccuracy()); } } ././@LongLink100644 0 0 160 12126630646 10256 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/analysis/solvers/BaseSecantSolverAbstractTest.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/analysis/solvers/BaseSecantSolverAbstra100644 1750 1750 24041 12126627670 32404 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.analysis.solvers; import org.apache.commons.math3.analysis.QuinticFunction; import org.apache.commons.math3.analysis.UnivariateFunction; import org.apache.commons.math3.analysis.XMinus5Function; import org.apache.commons.math3.analysis.function.Sin; import org.apache.commons.math3.exception.NoBracketingException; import org.apache.commons.math3.exception.NumberIsTooLargeException; import org.apache.commons.math3.util.FastMath; import org.junit.Assert; import org.junit.Test; /** * Base class for root-finding algorithms tests derived from * {@link BaseSecantSolver}. * * @version $Id$ */ public abstract class BaseSecantSolverAbstractTest { /** Returns the solver to use to perform the tests. * @return the solver to use to perform the tests */ protected abstract UnivariateSolver getSolver(); /** Returns the expected number of evaluations for the * {@link #testQuinticZero} unit test. A value of {@code -1} indicates that * the test should be skipped for that solver. * @return the expected number of evaluations for the * {@link #testQuinticZero} unit test */ protected abstract int[] getQuinticEvalCounts(); @Test public void testSinZero() { // The sinus function is behaved well around the root at pi. The second // order derivative is zero, which means linear approximating methods // still converge quadratically. UnivariateFunction f = new Sin(); double result; UnivariateSolver solver = getSolver(); result = solver.solve(100, f, 3, 4); //System.out.println( // "Root: " + result + " Evaluations: " + solver.getEvaluations()); Assert.assertEquals(result, FastMath.PI, solver.getAbsoluteAccuracy()); Assert.assertTrue(solver.getEvaluations() <= 6); result = solver.solve(100, f, 1, 4); //System.out.println( // "Root: " + result + " Evaluations: " + solver.getEvaluations()); Assert.assertEquals(result, FastMath.PI, solver.getAbsoluteAccuracy()); Assert.assertTrue(solver.getEvaluations() <= 7); } @Test public void testQuinticZero() { // The quintic function has zeros at 0, +-0.5 and +-1. // Around the root of 0 the function is well behaved, with a second // derivative of zero a 0. // The other roots are less well to find, in particular the root at 1, // because the function grows fast for x>1. // The function has extrema (first derivative is zero) at 0.27195613 // and 0.82221643, intervals containing these values are harder for // the solvers. UnivariateFunction f = new QuinticFunction(); double result; UnivariateSolver solver = getSolver(); double atol = solver.getAbsoluteAccuracy(); int[] counts = getQuinticEvalCounts(); // Tests data: initial bounds, and expected solution, per test case. double[][] testsData = {{-0.2, 0.2, 0.0}, {-0.1, 0.3, 0.0}, {-0.3, 0.45, 0.0}, { 0.3, 0.7, 0.5}, { 0.2, 0.6, 0.5}, { 0.05, 0.95, 0.5}, { 0.85, 1.25, 1.0}, { 0.8, 1.2, 1.0}, { 0.85, 1.75, 1.0}, { 0.55, 1.45, 1.0}, { 0.85, 5.0, 1.0}, }; int maxIter = 500; for(int i = 0; i < testsData.length; i++) { // Skip test, if needed. if (counts[i] == -1) continue; // Compute solution. double[] testData = testsData[i]; result = solver.solve(maxIter, f, testData[0], testData[1]); //System.out.println( // "Root: " + result + " Evaluations: " + solver.getEvaluations()); // Check solution. Assert.assertEquals(result, testData[2], atol); Assert.assertTrue(solver.getEvaluations() <= counts[i] + 1); } } @Test public void testRootEndpoints() { UnivariateFunction f = new XMinus5Function(); UnivariateSolver solver = getSolver(); // End-point is root. This should be a special case in the solver, and // the initial end-point should be returned exactly. double result = solver.solve(100, f, 5.0, 6.0); Assert.assertEquals(5.0, result, 0.0); result = solver.solve(100, f, 4.0, 5.0); Assert.assertEquals(5.0, result, 0.0); result = solver.solve(100, f, 5.0, 6.0, 5.5); Assert.assertEquals(5.0, result, 0.0); result = solver.solve(100, f, 4.0, 5.0, 4.5); Assert.assertEquals(5.0, result, 0.0); } @Test public void testBadEndpoints() { UnivariateFunction f = new Sin(); UnivariateSolver solver = getSolver(); try { // bad interval solver.solve(100, f, 1, -1); Assert.fail("Expecting NumberIsTooLargeException - bad interval"); } catch (NumberIsTooLargeException ex) { // expected } try { // no bracket solver.solve(100, f, 1, 1.5); Assert.fail("Expecting NoBracketingException - non-bracketing"); } catch (NoBracketingException ex) { // expected } try { // no bracket solver.solve(100, f, 1, 1.5, 1.2); Assert.fail("Expecting NoBracketingException - non-bracketing"); } catch (NoBracketingException ex) { // expected } } @Test public void testSolutionLeftSide() { UnivariateFunction f = new Sin(); UnivariateSolver solver = getSolver(); double left = -1.5; double right = 0.05; for(int i = 0; i < 10; i++) { // Test whether the allowed solutions are taken into account. double solution = getSolution(solver, 100, f, left, right, AllowedSolution.LEFT_SIDE); if (!Double.isNaN(solution)) { Assert.assertTrue(solution <= 0.0); } // Prepare for next test. left -= 0.1; right += 0.3; } } @Test public void testSolutionRightSide() { UnivariateFunction f = new Sin(); UnivariateSolver solver = getSolver(); double left = -1.5; double right = 0.05; for(int i = 0; i < 10; i++) { // Test whether the allowed solutions are taken into account. double solution = getSolution(solver, 100, f, left, right, AllowedSolution.RIGHT_SIDE); if (!Double.isNaN(solution)) { Assert.assertTrue(solution >= 0.0); } // Prepare for next test. left -= 0.1; right += 0.3; } } @Test public void testSolutionBelowSide() { UnivariateFunction f = new Sin(); UnivariateSolver solver = getSolver(); double left = -1.5; double right = 0.05; for(int i = 0; i < 10; i++) { // Test whether the allowed solutions are taken into account. double solution = getSolution(solver, 100, f, left, right, AllowedSolution.BELOW_SIDE); if (!Double.isNaN(solution)) { Assert.assertTrue(f.value(solution) <= 0.0); } // Prepare for next test. left -= 0.1; right += 0.3; } } @Test public void testSolutionAboveSide() { UnivariateFunction f = new Sin(); UnivariateSolver solver = getSolver(); double left = -1.5; double right = 0.05; for(int i = 0; i < 10; i++) { // Test whether the allowed solutions are taken into account. double solution = getSolution(solver, 100, f, left, right, AllowedSolution.ABOVE_SIDE); if (!Double.isNaN(solution)) { Assert.assertTrue(f.value(solution) >= 0.0); } // Prepare for next test. left -= 0.1; right += 0.3; } } private double getSolution(UnivariateSolver solver, int maxEval, UnivariateFunction f, double left, double right, AllowedSolution allowedSolution) { try { @SuppressWarnings("unchecked") BracketedUnivariateSolver bracketing = (BracketedUnivariateSolver) solver; return bracketing.solve(100, f, left, right, allowedSolution); } catch (ClassCastException cce) { double baseRoot = solver.solve(maxEval, f, left, right); if ((baseRoot <= left) || (baseRoot >= right)) { // the solution slipped out of interval return Double.NaN; } PegasusSolver bracketing = new PegasusSolver(solver.getRelativeAccuracy(), solver.getAbsoluteAccuracy(), solver.getFunctionValueAccuracy()); return UnivariateSolverUtils.forceSide(maxEval - solver.getEvaluations(), f, bracketing, baseRoot, left, right, allowedSolution); } } } commons-math3-3.2-src/src/test/java/org/apache/commons/math3/analysis/solvers/BrentSolverTest.java100644 1750 1750 30657 12126627670 32103 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.analysis.solvers; import org.apache.commons.math3.analysis.FunctionUtils; import org.apache.commons.math3.analysis.MonitoredFunction; import org.apache.commons.math3.analysis.QuinticFunction; import org.apache.commons.math3.analysis.UnivariateFunction; import org.apache.commons.math3.analysis.differentiation.DerivativeStructure; import org.apache.commons.math3.analysis.differentiation.UnivariateDifferentiableFunction; import org.apache.commons.math3.analysis.function.Constant; import org.apache.commons.math3.analysis.function.Inverse; import org.apache.commons.math3.analysis.function.Sin; import org.apache.commons.math3.analysis.function.Sqrt; import org.apache.commons.math3.exception.NoBracketingException; import org.apache.commons.math3.exception.NumberIsTooLargeException; import org.apache.commons.math3.exception.TooManyEvaluationsException; import org.apache.commons.math3.util.FastMath; import org.junit.Assert; import org.junit.Test; /** * Test case for {@link BrentSolver Brent} solver. * Because Brent-Dekker is guaranteed to converge in less than the default * maximum iteration count due to bisection fallback, it is quite hard to * debug. I include measured iteration counts plus one in order to detect * regressions. On average Brent-Dekker should use 4..5 iterations for the * default absolute accuracy of 10E-8 for sinus and the quintic function around * zero, and 5..10 iterations for the other zeros. * * @version $Id: BrentSolverTest.java 1383845 2012-09-12 08:34:10Z luc $ */ public final class BrentSolverTest { @Test public void testSinZero() { // The sinus function is behaved well around the root at pi. The second // order derivative is zero, which means linar approximating methods will // still converge quadratically. UnivariateFunction f = new Sin(); double result; UnivariateSolver solver = new BrentSolver(); // Somewhat benign interval. The function is monotone. result = solver.solve(100, f, 3, 4); // System.out.println( // "Root: " + result + " Evaluations: " + solver.getEvaluations()); Assert.assertEquals(result, FastMath.PI, solver.getAbsoluteAccuracy()); Assert.assertTrue(solver.getEvaluations() <= 7); // Larger and somewhat less benign interval. The function is grows first. result = solver.solve(100, f, 1, 4); // System.out.println( // "Root: " + result + " Evaluations: " + solver.getEvaluations()); Assert.assertEquals(result, FastMath.PI, solver.getAbsoluteAccuracy()); Assert.assertTrue(solver.getEvaluations() <= 8); } @Test public void testQuinticZero() { // The quintic function has zeros at 0, +-0.5 and +-1. // Around the root of 0 the function is well behaved, with a second derivative // of zero a 0. // The other roots are less well to find, in particular the root at 1, because // the function grows fast for x>1. // The function has extrema (first derivative is zero) at 0.27195613 and 0.82221643, // intervals containing these values are harder for the solvers. UnivariateFunction f = new QuinticFunction(); double result; // Brent-Dekker solver. UnivariateSolver solver = new BrentSolver(); // Symmetric bracket around 0. Test whether solvers can handle hitting // the root in the first iteration. result = solver.solve(100, f, -0.2, 0.2); //System.out.println( // "Root: " + result + " Evaluations: " + solver.getEvaluations()); Assert.assertEquals(result, 0, solver.getAbsoluteAccuracy()); Assert.assertTrue(solver.getEvaluations() <= 3); // 1 iterations on i586 JDK 1.4.1. // Asymmetric bracket around 0, just for fun. Contains extremum. result = solver.solve(100, f, -0.1, 0.3); //System.out.println( // "Root: " + result + " Evaluations: " + solver.getEvaluations()); Assert.assertEquals(result, 0, solver.getAbsoluteAccuracy()); // 5 iterations on i586 JDK 1.4.1. Assert.assertTrue(solver.getEvaluations() <= 7); // Large bracket around 0. Contains two extrema. result = solver.solve(100, f, -0.3, 0.45); //System.out.println( // "Root: " + result + " Evaluations: " + solver.getEvaluations()); Assert.assertEquals(result, 0, solver.getAbsoluteAccuracy()); // 6 iterations on i586 JDK 1.4.1. Assert.assertTrue(solver.getEvaluations() <= 8); // Benign bracket around 0.5, function is monotonous. result = solver.solve(100, f, 0.3, 0.7); //System.out.println( // "Root: " + result + " Evaluations: " + solver.getEvaluations()); Assert.assertEquals(result, 0.5, solver.getAbsoluteAccuracy()); // 6 iterations on i586 JDK 1.4.1. Assert.assertTrue(solver.getEvaluations() <= 9); // Less benign bracket around 0.5, contains one extremum. result = solver.solve(100, f, 0.2, 0.6); // System.out.println( // "Root: " + result + " Evaluations: " + solver.getEvaluations()); Assert.assertEquals(result, 0.5, solver.getAbsoluteAccuracy()); Assert.assertTrue(solver.getEvaluations() <= 10); // Large, less benign bracket around 0.5, contains both extrema. result = solver.solve(100, f, 0.05, 0.95); //System.out.println( // "Root: " + result + " Evaluations: " + solver.getEvaluations()); Assert.assertEquals(result, 0.5, solver.getAbsoluteAccuracy()); Assert.assertTrue(solver.getEvaluations() <= 11); // Relatively benign bracket around 1, function is monotonous. Fast growth for x>1 // is still a problem. result = solver.solve(100, f, 0.85, 1.25); //System.out.println( // "Root: " + result + " Evaluations: " + solver.getEvaluations()); Assert.assertEquals(result, 1.0, solver.getAbsoluteAccuracy()); Assert.assertTrue(solver.getEvaluations() <= 11); // Less benign bracket around 1 with extremum. result = solver.solve(100, f, 0.8, 1.2); //System.out.println( // "Root: " + result + " Evaluations: " + solver.getEvaluations()); Assert.assertEquals(result, 1.0, solver.getAbsoluteAccuracy()); Assert.assertTrue(solver.getEvaluations() <= 11); // Large bracket around 1. Monotonous. result = solver.solve(100, f, 0.85, 1.75); //System.out.println( // "Root: " + result + " Evaluations: " + solver.getEvaluations()); Assert.assertEquals(result, 1.0, solver.getAbsoluteAccuracy()); Assert.assertTrue(solver.getEvaluations() <= 13); // Large bracket around 1. Interval contains extremum. result = solver.solve(100, f, 0.55, 1.45); //System.out.println( // "Root: " + result + " Evaluations: " + solver.getEvaluations()); Assert.assertEquals(result, 1.0, solver.getAbsoluteAccuracy()); Assert.assertTrue(solver.getEvaluations() <= 10); // Very large bracket around 1 for testing fast growth behaviour. result = solver.solve(100, f, 0.85, 5); //System.out.println( // "Root: " + result + " Evaluations: " + solver.getEvaluations()); Assert.assertEquals(result, 1.0, solver.getAbsoluteAccuracy()); Assert.assertTrue(solver.getEvaluations() <= 15); try { result = solver.solve(5, f, 0.85, 5); Assert.fail("Expected TooManyEvaluationsException"); } catch (TooManyEvaluationsException e) { // Expected. } } @Test public void testRootEndpoints() { UnivariateFunction f = new Sin(); BrentSolver solver = new BrentSolver(); // endpoint is root double result = solver.solve(100, f, FastMath.PI, 4); Assert.assertEquals(FastMath.PI, result, solver.getAbsoluteAccuracy()); result = solver.solve(100, f, 3, FastMath.PI); Assert.assertEquals(FastMath.PI, result, solver.getAbsoluteAccuracy()); result = solver.solve(100, f, FastMath.PI, 4, 3.5); Assert.assertEquals(FastMath.PI, result, solver.getAbsoluteAccuracy()); result = solver.solve(100, f, 3, FastMath.PI, 3.07); Assert.assertEquals(FastMath.PI, result, solver.getAbsoluteAccuracy()); } @Test public void testBadEndpoints() { UnivariateFunction f = new Sin(); BrentSolver solver = new BrentSolver(); try { // bad interval solver.solve(100, f, 1, -1); Assert.fail("Expecting NumberIsTooLargeException - bad interval"); } catch (NumberIsTooLargeException ex) { // expected } try { // no bracket solver.solve(100, f, 1, 1.5); Assert.fail("Expecting NoBracketingException - non-bracketing"); } catch (NoBracketingException ex) { // expected } try { // no bracket solver.solve(100, f, 1, 1.5, 1.2); Assert.fail("Expecting NoBracketingException - non-bracketing"); } catch (NoBracketingException ex) { // expected } } @Test public void testInitialGuess() { MonitoredFunction f = new MonitoredFunction(new QuinticFunction()); BrentSolver solver = new BrentSolver(); double result; // no guess result = solver.solve(100, f, 0.6, 7.0); Assert.assertEquals(result, 1.0, solver.getAbsoluteAccuracy()); int referenceCallsCount = f.getCallsCount(); Assert.assertTrue(referenceCallsCount >= 13); // invalid guess (it *is* a root, but outside of the range) try { result = solver.solve(100, f, 0.6, 7.0, 0.0); Assert.fail("a NumberIsTooLargeException was expected"); } catch (NumberIsTooLargeException iae) { // expected behaviour } // bad guess f.setCallsCount(0); result = solver.solve(100, f, 0.6, 7.0, 0.61); Assert.assertEquals(result, 1.0, solver.getAbsoluteAccuracy()); Assert.assertTrue(f.getCallsCount() > referenceCallsCount); // good guess f.setCallsCount(0); result = solver.solve(100, f, 0.6, 7.0, 0.999999); Assert.assertEquals(result, 1.0, solver.getAbsoluteAccuracy()); Assert.assertTrue(f.getCallsCount() < referenceCallsCount); // perfect guess f.setCallsCount(0); result = solver.solve(100, f, 0.6, 7.0, 1.0); Assert.assertEquals(result, 1.0, solver.getAbsoluteAccuracy()); Assert.assertEquals(1, solver.getEvaluations()); Assert.assertEquals(1, f.getCallsCount()); } @Test public void testMath832() { final UnivariateFunction f = new UnivariateFunction() { private final UnivariateDifferentiableFunction sqrt = new Sqrt(); private final UnivariateDifferentiableFunction inv = new Inverse(); private final UnivariateDifferentiableFunction func = FunctionUtils.add(FunctionUtils.multiply(new Constant(1e2), sqrt), FunctionUtils.multiply(new Constant(1e6), inv), FunctionUtils.multiply(new Constant(1e4), FunctionUtils.compose(inv, sqrt))); public double value(double x) { return func.value(new DerivativeStructure(1, 1, 0, x)).getPartialDerivative(1); } }; BrentSolver solver = new BrentSolver(); final double result = solver.solve(99, f, 1, 1e30, 1 + 1e-10); Assert.assertEquals(804.93558250, result, 1e-8); } } ././@LongLink100644 0 0 145 12126630646 10261 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/analysis/solvers/RiddersSolverTest.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/analysis/solvers/RiddersSolverTest.java100644 1750 1750 13066 12126627670 32420 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.analysis.solvers; import org.apache.commons.math3.analysis.QuinticFunction; import org.apache.commons.math3.analysis.UnivariateFunction; import org.apache.commons.math3.analysis.function.Expm1; import org.apache.commons.math3.analysis.function.Sin; import org.apache.commons.math3.exception.NoBracketingException; import org.apache.commons.math3.exception.NumberIsTooLargeException; import org.apache.commons.math3.util.FastMath; import org.junit.Assert; import org.junit.Test; /** * Test case for {@link RiddersSolver Ridders} solver. *

              * Ridders' method converges superlinearly, more specific, its rate of * convergence is sqrt(2). Test runs show that for a default absolute * accuracy of 1E-6, it generally takes less than 5 iterations for close * initial bracket and 5 to 10 iterations for distant initial bracket * to converge. * * @version $Id: RiddersSolverTest.java 1374632 2012-08-18 18:11:11Z luc $ */ public final class RiddersSolverTest { /** * Test of solver for the sine function. */ @Test public void testSinFunction() { UnivariateFunction f = new Sin(); UnivariateSolver solver = new RiddersSolver(); double min, max, expected, result, tolerance; min = 3.0; max = 4.0; expected = FastMath.PI; tolerance = FastMath.max(solver.getAbsoluteAccuracy(), FastMath.abs(expected * solver.getRelativeAccuracy())); result = solver.solve(100, f, min, max); Assert.assertEquals(expected, result, tolerance); min = -1.0; max = 1.5; expected = 0.0; tolerance = FastMath.max(solver.getAbsoluteAccuracy(), FastMath.abs(expected * solver.getRelativeAccuracy())); result = solver.solve(100, f, min, max); Assert.assertEquals(expected, result, tolerance); } /** * Test of solver for the quintic function. */ @Test public void testQuinticFunction() { UnivariateFunction f = new QuinticFunction(); UnivariateSolver solver = new RiddersSolver(); double min, max, expected, result, tolerance; min = -0.4; max = 0.2; expected = 0.0; tolerance = FastMath.max(solver.getAbsoluteAccuracy(), FastMath.abs(expected * solver.getRelativeAccuracy())); result = solver.solve(100, f, min, max); Assert.assertEquals(expected, result, tolerance); min = 0.75; max = 1.5; expected = 1.0; tolerance = FastMath.max(solver.getAbsoluteAccuracy(), FastMath.abs(expected * solver.getRelativeAccuracy())); result = solver.solve(100, f, min, max); Assert.assertEquals(expected, result, tolerance); min = -0.9; max = -0.2; expected = -0.5; tolerance = FastMath.max(solver.getAbsoluteAccuracy(), FastMath.abs(expected * solver.getRelativeAccuracy())); result = solver.solve(100, f, min, max); Assert.assertEquals(expected, result, tolerance); } /** * Test of solver for the exponential function. */ @Test public void testExpm1Function() { UnivariateFunction f = new Expm1(); UnivariateSolver solver = new RiddersSolver(); double min, max, expected, result, tolerance; min = -1.0; max = 2.0; expected = 0.0; tolerance = FastMath.max(solver.getAbsoluteAccuracy(), FastMath.abs(expected * solver.getRelativeAccuracy())); result = solver.solve(100, f, min, max); Assert.assertEquals(expected, result, tolerance); min = -20.0; max = 10.0; expected = 0.0; tolerance = FastMath.max(solver.getAbsoluteAccuracy(), FastMath.abs(expected * solver.getRelativeAccuracy())); result = solver.solve(100, f, min, max); Assert.assertEquals(expected, result, tolerance); min = -50.0; max = 100.0; expected = 0.0; tolerance = FastMath.max(solver.getAbsoluteAccuracy(), FastMath.abs(expected * solver.getRelativeAccuracy())); result = solver.solve(100, f, min, max); Assert.assertEquals(expected, result, tolerance); } /** * Test of parameters for the solver. */ @Test public void testParameters() { UnivariateFunction f = new Sin(); UnivariateSolver solver = new RiddersSolver(); try { // bad interval solver.solve(100, f, 1, -1); Assert.fail("Expecting NumberIsTooLargeException - bad interval"); } catch (NumberIsTooLargeException ex) { // expected } try { // no bracketing solver.solve(100, f, 2, 3); Assert.fail("Expecting NoBracketingException - no bracketing"); } catch (NoBracketingException ex) { // expected } } } ././@LongLink100644 0 0 165 12126630646 10263 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/analysis/solvers/BracketingNthOrderBrentSolverTest.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/analysis/solvers/BracketingNthOrderBren100644 1750 1750 20432 12126627670 32372 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.analysis.solvers; import org.apache.commons.math3.analysis.QuinticFunction; import org.apache.commons.math3.analysis.UnivariateFunction; import org.apache.commons.math3.analysis.differentiation.DerivativeStructure; import org.apache.commons.math3.analysis.differentiation.UnivariateDifferentiableFunction; import org.apache.commons.math3.exception.NumberIsTooSmallException; import org.apache.commons.math3.exception.TooManyEvaluationsException; import org.junit.Assert; import org.junit.Test; /** * Test case for {@link BracketingNthOrderBrentSolver bracketing nth order Brent} solver. * * @version $Id: BracketingNthOrderBrentSolverTest.java 1383441 2012-09-11 14:56:39Z luc $ */ public final class BracketingNthOrderBrentSolverTest extends BaseSecantSolverAbstractTest { /** {@inheritDoc} */ @Override protected UnivariateSolver getSolver() { return new BracketingNthOrderBrentSolver(); } /** {@inheritDoc} */ @Override protected int[] getQuinticEvalCounts() { return new int[] {1, 3, 8, 1, 9, 4, 8, 1, 12, 1, 16}; } @Test(expected=NumberIsTooSmallException.class) public void testInsufficientOrder1() { new BracketingNthOrderBrentSolver(1.0e-10, 1); } @Test(expected=NumberIsTooSmallException.class) public void testInsufficientOrder2() { new BracketingNthOrderBrentSolver(1.0e-10, 1.0e-10, 1); } @Test(expected=NumberIsTooSmallException.class) public void testInsufficientOrder3() { new BracketingNthOrderBrentSolver(1.0e-10, 1.0e-10, 1.0e-10, 1); } @Test public void testConstructorsOK() { Assert.assertEquals(2, new BracketingNthOrderBrentSolver(1.0e-10, 2).getMaximalOrder()); Assert.assertEquals(2, new BracketingNthOrderBrentSolver(1.0e-10, 1.0e-10, 2).getMaximalOrder()); Assert.assertEquals(2, new BracketingNthOrderBrentSolver(1.0e-10, 1.0e-10, 1.0e-10, 2).getMaximalOrder()); } @Test public void testConvergenceOnFunctionAccuracy() { BracketingNthOrderBrentSolver solver = new BracketingNthOrderBrentSolver(1.0e-12, 1.0e-10, 0.001, 3); QuinticFunction f = new QuinticFunction(); double result = solver.solve(20, f, 0.2, 0.9, 0.4, AllowedSolution.BELOW_SIDE); Assert.assertEquals(0, f.value(result), solver.getFunctionValueAccuracy()); Assert.assertTrue(f.value(result) <= 0); Assert.assertTrue(result - 0.5 > solver.getAbsoluteAccuracy()); result = solver.solve(20, f, -0.9, -0.2, -0.4, AllowedSolution.ABOVE_SIDE); Assert.assertEquals(0, f.value(result), solver.getFunctionValueAccuracy()); Assert.assertTrue(f.value(result) >= 0); Assert.assertTrue(result + 0.5 < -solver.getAbsoluteAccuracy()); } @Test public void testIssue716() { BracketingNthOrderBrentSolver solver = new BracketingNthOrderBrentSolver(1.0e-12, 1.0e-10, 1.0e-22, 5); UnivariateFunction sharpTurn = new UnivariateFunction() { public double value(double x) { return (2 * x + 1) / (1.0e9 * (x + 1)); } }; double result = solver.solve(100, sharpTurn, -0.9999999, 30, 15, AllowedSolution.RIGHT_SIDE); Assert.assertEquals(0, sharpTurn.value(result), solver.getFunctionValueAccuracy()); Assert.assertTrue(sharpTurn.value(result) >= 0); Assert.assertEquals(-0.5, result, 1.0e-10); } @Test public void testFasterThanNewton() { // the following test functions come from Beny Neta's paper: // "Several New Methods for solving Equations" // intern J. Computer Math Vol 23 pp 265-282 // available here: http://www.math.nps.navy.mil/~bneta/SeveralNewMethods.PDF // the reference roots have been computed by the Dfp solver to more than // 80 digits and checked with emacs (only the first 20 digits are reproduced here) compare(new TestFunction(0.0, -2, 2) { @Override public DerivativeStructure value(DerivativeStructure x) { return x.sin().subtract(x.multiply(0.5)); } }); compare(new TestFunction(6.3087771299726890947, -5, 10) { @Override public DerivativeStructure value(DerivativeStructure x) { return x.pow(5).add(x).subtract(10000); } }); compare(new TestFunction(9.6335955628326951924, 0.001, 10) { @Override public DerivativeStructure value(DerivativeStructure x) { return x.sqrt().subtract(x.reciprocal()).subtract(3); } }); compare(new TestFunction(2.8424389537844470678, -5, 5) { @Override public DerivativeStructure value(DerivativeStructure x) { return x.exp().add(x).subtract(20); } }); compare(new TestFunction(8.3094326942315717953, 0.001, 10) { @Override public DerivativeStructure value(DerivativeStructure x) { return x.log().add(x.sqrt()).subtract(5); } }); compare(new TestFunction(1.4655712318767680266, -0.5, 1.5) { @Override public DerivativeStructure value(DerivativeStructure x) { return x.subtract(1).multiply(x).multiply(x).subtract(1); } }); } private void compare(TestFunction f) { compare(f, f.getRoot(), f.getMin(), f.getMax()); } private void compare(final UnivariateDifferentiableFunction f, double root, double min, double max) { NewtonRaphsonSolver newton = new NewtonRaphsonSolver(1.0e-12); BracketingNthOrderBrentSolver bracketing = new BracketingNthOrderBrentSolver(1.0e-12, 1.0e-12, 1.0e-18, 5); double resultN; try { resultN = newton.solve(100, f, min, max); } catch (TooManyEvaluationsException tmee) { resultN = Double.NaN; } double resultB; try { resultB = bracketing.solve(100, f, min, max); } catch (TooManyEvaluationsException tmee) { resultB = Double.NaN; } Assert.assertEquals(root, resultN, newton.getAbsoluteAccuracy()); Assert.assertEquals(root, resultB, bracketing.getAbsoluteAccuracy()); // bracketing solver evaluates only function value, we set the weight to 1 final int weightedBracketingEvaluations = bracketing.getEvaluations(); // Newton-Raphson solver evaluates both function value and derivative, we set the weight to 2 final int weightedNewtonEvaluations = 2 * newton.getEvaluations(); Assert.assertTrue(weightedBracketingEvaluations < weightedNewtonEvaluations); } private static abstract class TestFunction implements UnivariateDifferentiableFunction { private final double root; private final double min; private final double max; protected TestFunction(final double root, final double min, final double max) { this.root = root; this.min = min; this.max = max; } public double getRoot() { return root; } public double getMin() { return min; } public double getMax() { return max; } public double value(final double x) { return value(new DerivativeStructure(0, 0, x)).getValue(); } public abstract DerivativeStructure value(final DerivativeStructure t); } } commons-math3-3.2-src/src/test/java/org/apache/commons/math3/analysis/solvers/SecantSolverTest.java100644 1750 1750 2704 12126627670 32216 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.analysis.solvers; /** * Test case for {@link SecantSolver Secant} solver. * * @version $Id$ */ public final class SecantSolverTest extends BaseSecantSolverAbstractTest { /** {@inheritDoc} */ @Override protected UnivariateSolver getSolver() { return new SecantSolver(); } /** {@inheritDoc} */ @Override protected int[] getQuinticEvalCounts() { // As the Secant method does not maintain a bracketed solution, // convergence is not guaranteed. Two test cases are disabled (-1) due // to bad solutions. return new int[] {3, 7, -1, 8, 9, 8, 11, 12, 14, -1, 16}; } } ././@LongLink100644 0 0 147 12126630646 10263 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/analysis/solvers/BisectionSolverTest.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/analysis/solvers/BisectionSolverTest.ja100644 1750 1750 6700 12126627670 32371 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.analysis.solvers; import org.apache.commons.math3.analysis.QuinticFunction; import org.apache.commons.math3.analysis.UnivariateFunction; import org.apache.commons.math3.analysis.function.Sin; import org.apache.commons.math3.util.FastMath; import org.junit.Assert; import org.junit.Test; /** * @version $Id: BisectionSolverTest.java 1374632 2012-08-18 18:11:11Z luc $ */ public final class BisectionSolverTest { @Test public void testSinZero() { UnivariateFunction f = new Sin(); double result; BisectionSolver solver = new BisectionSolver(); result = solver.solve(100, f, 3, 4); Assert.assertEquals(result, FastMath.PI, solver.getAbsoluteAccuracy()); result = solver.solve(100, f, 1, 4); Assert.assertEquals(result, FastMath.PI, solver.getAbsoluteAccuracy()); } @Test public void testQuinticZero() { UnivariateFunction f = new QuinticFunction(); double result; BisectionSolver solver = new BisectionSolver(); result = solver.solve(100, f, -0.2, 0.2); Assert.assertEquals(result, 0, solver.getAbsoluteAccuracy()); result = solver.solve(100, f, -0.1, 0.3); Assert.assertEquals(result, 0, solver.getAbsoluteAccuracy()); result = solver.solve(100, f, -0.3, 0.45); Assert.assertEquals(result, 0, solver.getAbsoluteAccuracy()); result = solver.solve(100, f, 0.3, 0.7); Assert.assertEquals(result, 0.5, solver.getAbsoluteAccuracy()); result = solver.solve(100, f, 0.2, 0.6); Assert.assertEquals(result, 0.5, solver.getAbsoluteAccuracy()); result = solver.solve(100, f, 0.05, 0.95); Assert.assertEquals(result, 0.5, solver.getAbsoluteAccuracy()); result = solver.solve(100, f, 0.85, 1.25); Assert.assertEquals(result, 1.0, solver.getAbsoluteAccuracy()); result = solver.solve(100, f, 0.8, 1.2); Assert.assertEquals(result, 1.0, solver.getAbsoluteAccuracy()); result = solver.solve(100, f, 0.85, 1.75); Assert.assertEquals(result, 1.0, solver.getAbsoluteAccuracy()); result = solver.solve(100, f, 0.55, 1.45); Assert.assertEquals(result, 1.0, solver.getAbsoluteAccuracy()); result = solver.solve(100, f, 0.85, 5); Assert.assertEquals(result, 1.0, solver.getAbsoluteAccuracy()); Assert.assertTrue(solver.getEvaluations() > 0); } @Test public void testMath369() { UnivariateFunction f = new Sin(); BisectionSolver solver = new BisectionSolver(); Assert.assertEquals(FastMath.PI, solver.solve(100, f, 3.0, 3.2, 3.1), solver.getAbsoluteAccuracy()); } } ././@LongLink100644 0 0 145 12126630646 10261 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/analysis/solvers/MullerSolver2Test.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/analysis/solvers/MullerSolver2Test.java100644 1750 1750 13263 12126627670 32345 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.analysis.solvers; import org.apache.commons.math3.analysis.QuinticFunction; import org.apache.commons.math3.analysis.UnivariateFunction; import org.apache.commons.math3.analysis.function.Expm1; import org.apache.commons.math3.analysis.function.Sin; import org.apache.commons.math3.exception.NoBracketingException; import org.apache.commons.math3.exception.NumberIsTooLargeException; import org.apache.commons.math3.util.FastMath; import org.junit.Assert; import org.junit.Test; /** * Test case for {@link MullerSolver2 Muller} solver. *

              * Muller's method converges almost quadratically near roots, but it can * be very slow in regions far away from zeros. Test runs show that for * reasonably good initial values, for a default absolute accuracy of 1E-6, * it generally takes 5 to 10 iterations for the solver to converge. *

              * Tests for the exponential function illustrate the situations where * Muller solver performs poorly. * * @version $Id$ */ public final class MullerSolver2Test { /** * Test of solver for the sine function. */ @Test public void testSinFunction() { UnivariateFunction f = new Sin(); UnivariateSolver solver = new MullerSolver2(); double min, max, expected, result, tolerance; min = 3.0; max = 4.0; expected = FastMath.PI; tolerance = FastMath.max(solver.getAbsoluteAccuracy(), FastMath.abs(expected * solver.getRelativeAccuracy())); result = solver.solve(100, f, min, max); Assert.assertEquals(expected, result, tolerance); min = -1.0; max = 1.5; expected = 0.0; tolerance = FastMath.max(solver.getAbsoluteAccuracy(), FastMath.abs(expected * solver.getRelativeAccuracy())); result = solver.solve(100, f, min, max); Assert.assertEquals(expected, result, tolerance); } /** * Test of solver for the quintic function. */ @Test public void testQuinticFunction() { UnivariateFunction f = new QuinticFunction(); UnivariateSolver solver = new MullerSolver2(); double min, max, expected, result, tolerance; min = -0.4; max = 0.2; expected = 0.0; tolerance = FastMath.max(solver.getAbsoluteAccuracy(), FastMath.abs(expected * solver.getRelativeAccuracy())); result = solver.solve(100, f, min, max); Assert.assertEquals(expected, result, tolerance); min = 0.75; max = 1.5; expected = 1.0; tolerance = FastMath.max(solver.getAbsoluteAccuracy(), FastMath.abs(expected * solver.getRelativeAccuracy())); result = solver.solve(100, f, min, max); Assert.assertEquals(expected, result, tolerance); min = -0.9; max = -0.2; expected = -0.5; tolerance = FastMath.max(solver.getAbsoluteAccuracy(), FastMath.abs(expected * solver.getRelativeAccuracy())); result = solver.solve(100, f, min, max); Assert.assertEquals(expected, result, tolerance); } /** * Test of solver for the exponential function. *

              * It takes 25 to 50 iterations for the last two tests to converge. */ @Test public void testExpm1Function() { UnivariateFunction f = new Expm1(); UnivariateSolver solver = new MullerSolver2(); double min, max, expected, result, tolerance; min = -1.0; max = 2.0; expected = 0.0; tolerance = FastMath.max(solver.getAbsoluteAccuracy(), FastMath.abs(expected * solver.getRelativeAccuracy())); result = solver.solve(100, f, min, max); Assert.assertEquals(expected, result, tolerance); min = -20.0; max = 10.0; expected = 0.0; tolerance = FastMath.max(solver.getAbsoluteAccuracy(), FastMath.abs(expected * solver.getRelativeAccuracy())); result = solver.solve(100, f, min, max); Assert.assertEquals(expected, result, tolerance); min = -50.0; max = 100.0; expected = 0.0; tolerance = FastMath.max(solver.getAbsoluteAccuracy(), FastMath.abs(expected * solver.getRelativeAccuracy())); result = solver.solve(100, f, min, max); Assert.assertEquals(expected, result, tolerance); } /** * Test of parameters for the solver. */ @Test public void testParameters() { UnivariateFunction f = new Sin(); UnivariateSolver solver = new MullerSolver2(); try { // bad interval solver.solve(100, f, 1, -1); Assert.fail("Expecting NumberIsTooLargeException - bad interval"); } catch (NumberIsTooLargeException ex) { // expected } try { // no bracketing solver.solve(100, f, 2, 3); Assert.fail("Expecting NoBracketingException - no bracketing"); } catch (NoBracketingException ex) { // expected } } } ././@LongLink100644 0 0 155 12126630646 10262 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/analysis/solvers/UnivariateSolverUtilsTest.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/analysis/solvers/UnivariateSolverUtilsT100644 1750 1750 12464 12126627670 32521 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.analysis.solvers; import org.apache.commons.math3.analysis.QuinticFunction; import org.apache.commons.math3.analysis.UnivariateFunction; import org.apache.commons.math3.analysis.function.Sin; import org.apache.commons.math3.exception.MathIllegalArgumentException; import org.apache.commons.math3.util.FastMath; import org.junit.Assert; import org.junit.Test; /** * @version $Id: UnivariateSolverUtilsTest.java 1374632 2012-08-18 18:11:11Z luc $ */ public class UnivariateSolverUtilsTest { protected UnivariateFunction sin = new Sin(); @Test(expected=MathIllegalArgumentException.class) public void testSolveNull() { UnivariateSolverUtils.solve(null, 0.0, 4.0); } @Test(expected=MathIllegalArgumentException.class) public void testSolveBadEndpoints() { double root = UnivariateSolverUtils.solve(sin, 4.0, -0.1, 1e-6); System.out.println("root=" + root); } @Test public void testSolveBadAccuracy() { try { // bad accuracy UnivariateSolverUtils.solve(sin, 0.0, 4.0, 0.0); // Assert.fail("Expecting MathIllegalArgumentException"); // TODO needs rework since convergence behaviour was changed } catch (MathIllegalArgumentException ex) { // expected } } @Test public void testSolveSin() { double x = UnivariateSolverUtils.solve(sin, 1.0, 4.0); Assert.assertEquals(FastMath.PI, x, 1.0e-4); } @Test(expected=MathIllegalArgumentException.class) public void testSolveAccuracyNull() { double accuracy = 1.0e-6; UnivariateSolverUtils.solve(null, 0.0, 4.0, accuracy); } @Test public void testSolveAccuracySin() { double accuracy = 1.0e-6; double x = UnivariateSolverUtils.solve(sin, 1.0, 4.0, accuracy); Assert.assertEquals(FastMath.PI, x, accuracy); } @Test(expected=MathIllegalArgumentException.class) public void testSolveNoRoot() { UnivariateSolverUtils.solve(sin, 1.0, 1.5); } @Test public void testBracketSin() { double[] result = UnivariateSolverUtils.bracket(sin, 0.0, -2.0, 2.0); Assert.assertTrue(sin.value(result[0]) < 0); Assert.assertTrue(sin.value(result[1]) > 0); } @Test public void testBracketEndpointRoot() { double[] result = UnivariateSolverUtils.bracket(sin, 1.5, 0, 2.0); Assert.assertEquals(0.0, sin.value(result[0]), 1.0e-15); Assert.assertTrue(sin.value(result[1]) > 0); } @Test(expected=MathIllegalArgumentException.class) public void testNullFunction() { UnivariateSolverUtils.bracket(null, 1.5, 0, 2.0); } @Test(expected=MathIllegalArgumentException.class) public void testBadInitial() { UnivariateSolverUtils.bracket(sin, 2.5, 0, 2.0); } @Test(expected=MathIllegalArgumentException.class) public void testBadEndpoints() { // endpoints not valid UnivariateSolverUtils.bracket(sin, 1.5, 2.0, 1.0); } @Test(expected=MathIllegalArgumentException.class) public void testBadMaximumIterations() { // bad maximum iterations UnivariateSolverUtils.bracket(sin, 1.5, 0, 2.0, 0); } @Test public void testMisc() { UnivariateFunction f = new QuinticFunction(); double result; // Static solve method result = UnivariateSolverUtils.solve(f, -0.2, 0.2); Assert.assertEquals(result, 0, 1E-8); result = UnivariateSolverUtils.solve(f, -0.1, 0.3); Assert.assertEquals(result, 0, 1E-8); result = UnivariateSolverUtils.solve(f, -0.3, 0.45); Assert.assertEquals(result, 0, 1E-6); result = UnivariateSolverUtils.solve(f, 0.3, 0.7); Assert.assertEquals(result, 0.5, 1E-6); result = UnivariateSolverUtils.solve(f, 0.2, 0.6); Assert.assertEquals(result, 0.5, 1E-6); result = UnivariateSolverUtils.solve(f, 0.05, 0.95); Assert.assertEquals(result, 0.5, 1E-6); result = UnivariateSolverUtils.solve(f, 0.85, 1.25); Assert.assertEquals(result, 1.0, 1E-6); result = UnivariateSolverUtils.solve(f, 0.8, 1.2); Assert.assertEquals(result, 1.0, 1E-6); result = UnivariateSolverUtils.solve(f, 0.85, 1.75); Assert.assertEquals(result, 1.0, 1E-6); result = UnivariateSolverUtils.solve(f, 0.55, 1.45); Assert.assertEquals(result, 1.0, 1E-6); result = UnivariateSolverUtils.solve(f, 0.85, 5); Assert.assertEquals(result, 1.0, 1E-6); } } ././@LongLink100644 0 0 146 12126630646 10262 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/analysis/solvers/IllinoisSolverTest.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/analysis/solvers/IllinoisSolverTest.jav100644 1750 1750 2432 12126627670 32420 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.analysis.solvers; /** * Test case for {@link IllinoisSolver Illinois} solver. * * @version $Id$ */ public final class IllinoisSolverTest extends BaseSecantSolverAbstractTest { /** {@inheritDoc} */ @Override protected UnivariateSolver getSolver() { return new IllinoisSolver(); } /** {@inheritDoc} */ @Override protected int[] getQuinticEvalCounts() { return new int[] {3, 7, 9, 10, 10, 10, 12, 12, 14, 15, 20}; } } commons-math3-3.2-src/src/test/java/org/apache/commons/math3/analysis/solvers/MullerSolverTest.java100644 1750 1750 13637 12126627670 32270 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.analysis.solvers; import org.apache.commons.math3.analysis.QuinticFunction; import org.apache.commons.math3.analysis.UnivariateFunction; import org.apache.commons.math3.analysis.function.Expm1; import org.apache.commons.math3.analysis.function.Sin; import org.apache.commons.math3.exception.NoBracketingException; import org.apache.commons.math3.exception.NumberIsTooLargeException; import org.apache.commons.math3.util.FastMath; import org.junit.Assert; import org.junit.Test; /** * Test case for {@link MullerSolver Muller} solver. *

              * Muller's method converges almost quadratically near roots, but it can * be very slow in regions far away from zeros. Test runs show that for * reasonably good initial values, for a default absolute accuracy of 1E-6, * it generally takes 5 to 10 iterations for the solver to converge. *

              * Tests for the exponential function illustrate the situations where * Muller solver performs poorly. * * @version $Id: MullerSolverTest.java 1374632 2012-08-18 18:11:11Z luc $ */ public final class MullerSolverTest { /** * Test of solver for the sine function. */ @Test public void testSinFunction() { UnivariateFunction f = new Sin(); UnivariateSolver solver = new MullerSolver(); double min, max, expected, result, tolerance; min = 3.0; max = 4.0; expected = FastMath.PI; tolerance = FastMath.max(solver.getAbsoluteAccuracy(), FastMath.abs(expected * solver.getRelativeAccuracy())); result = solver.solve(100, f, min, max); Assert.assertEquals(expected, result, tolerance); min = -1.0; max = 1.5; expected = 0.0; tolerance = FastMath.max(solver.getAbsoluteAccuracy(), FastMath.abs(expected * solver.getRelativeAccuracy())); result = solver.solve(100, f, min, max); Assert.assertEquals(expected, result, tolerance); } /** * Test of solver for the quintic function. */ @Test public void testQuinticFunction() { UnivariateFunction f = new QuinticFunction(); UnivariateSolver solver = new MullerSolver(); double min, max, expected, result, tolerance; min = -0.4; max = 0.2; expected = 0.0; tolerance = FastMath.max(solver.getAbsoluteAccuracy(), FastMath.abs(expected * solver.getRelativeAccuracy())); result = solver.solve(100, f, min, max); Assert.assertEquals(expected, result, tolerance); min = 0.75; max = 1.5; expected = 1.0; tolerance = FastMath.max(solver.getAbsoluteAccuracy(), FastMath.abs(expected * solver.getRelativeAccuracy())); result = solver.solve(100, f, min, max); Assert.assertEquals(expected, result, tolerance); min = -0.9; max = -0.2; expected = -0.5; tolerance = FastMath.max(solver.getAbsoluteAccuracy(), FastMath.abs(expected * solver.getRelativeAccuracy())); result = solver.solve(100, f, min, max); Assert.assertEquals(expected, result, tolerance); } /** * Test of solver for the exponential function. *

              * It takes 10 to 15 iterations for the last two tests to converge. * In fact, if not for the bisection alternative, the solver would * exceed the default maximal iteration of 100. */ @Test public void testExpm1Function() { UnivariateFunction f = new Expm1(); UnivariateSolver solver = new MullerSolver(); double min, max, expected, result, tolerance; min = -1.0; max = 2.0; expected = 0.0; tolerance = FastMath.max(solver.getAbsoluteAccuracy(), FastMath.abs(expected * solver.getRelativeAccuracy())); result = solver.solve(100, f, min, max); Assert.assertEquals(expected, result, tolerance); min = -20.0; max = 10.0; expected = 0.0; tolerance = FastMath.max(solver.getAbsoluteAccuracy(), FastMath.abs(expected * solver.getRelativeAccuracy())); result = solver.solve(100, f, min, max); Assert.assertEquals(expected, result, tolerance); min = -50.0; max = 100.0; expected = 0.0; tolerance = FastMath.max(solver.getAbsoluteAccuracy(), FastMath.abs(expected * solver.getRelativeAccuracy())); result = solver.solve(100, f, min, max); Assert.assertEquals(expected, result, tolerance); } /** * Test of parameters for the solver. */ @Test public void testParameters() { UnivariateFunction f = new Sin(); UnivariateSolver solver = new MullerSolver(); try { // bad interval double root = solver.solve(100, f, 1, -1); System.out.println("root=" + root); Assert.fail("Expecting NumberIsTooLargeException - bad interval"); } catch (NumberIsTooLargeException ex) { // expected } try { // no bracketing solver.solve(100, f, 2, 3); Assert.fail("Expecting NoBracketingException - no bracketing"); } catch (NoBracketingException ex) { // expected } } } ././@LongLink100644 0 0 146 12126630646 10262 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/analysis/solvers/LaguerreSolverTest.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/analysis/solvers/LaguerreSolverTest.jav100644 1750 1750 14715 12126627670 32433 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.analysis.solvers; import org.apache.commons.math3.analysis.polynomials.PolynomialFunction; import org.apache.commons.math3.exception.NumberIsTooLargeException; import org.apache.commons.math3.exception.NoBracketingException; import org.apache.commons.math3.complex.Complex; import org.apache.commons.math3.util.FastMath; import org.apache.commons.math3.TestUtils; import org.junit.Assert; import org.junit.Test; /** * Test case for Laguerre solver. *

              * Laguerre's method is very efficient in solving polynomials. Test runs * show that for a default absolute accuracy of 1E-6, it generally takes * less than 5 iterations to find one root, provided solveAll() is not * invoked, and 15 to 20 iterations to find all roots for quintic function. * * @version $Id: LaguerreSolverTest.java 1361793 2012-07-15 20:50:13Z erans $ */ public final class LaguerreSolverTest { /** * Test of solver for the linear function. */ @Test public void testLinearFunction() { double min, max, expected, result, tolerance; // p(x) = 4x - 1 double coefficients[] = { -1.0, 4.0 }; PolynomialFunction f = new PolynomialFunction(coefficients); LaguerreSolver solver = new LaguerreSolver(); min = 0.0; max = 1.0; expected = 0.25; tolerance = FastMath.max(solver.getAbsoluteAccuracy(), FastMath.abs(expected * solver.getRelativeAccuracy())); result = solver.solve(100, f, min, max); Assert.assertEquals(expected, result, tolerance); } /** * Test of solver for the quadratic function. */ @Test public void testQuadraticFunction() { double min, max, expected, result, tolerance; // p(x) = 2x^2 + 5x - 3 = (x+3)(2x-1) double coefficients[] = { -3.0, 5.0, 2.0 }; PolynomialFunction f = new PolynomialFunction(coefficients); LaguerreSolver solver = new LaguerreSolver(); min = 0.0; max = 2.0; expected = 0.5; tolerance = FastMath.max(solver.getAbsoluteAccuracy(), FastMath.abs(expected * solver.getRelativeAccuracy())); result = solver.solve(100, f, min, max); Assert.assertEquals(expected, result, tolerance); min = -4.0; max = -1.0; expected = -3.0; tolerance = FastMath.max(solver.getAbsoluteAccuracy(), FastMath.abs(expected * solver.getRelativeAccuracy())); result = solver.solve(100, f, min, max); Assert.assertEquals(expected, result, tolerance); } /** * Test of solver for the quintic function. */ @Test public void testQuinticFunction() { double min, max, expected, result, tolerance; // p(x) = x^5 - x^4 - 12x^3 + x^2 - x - 12 = (x+1)(x+3)(x-4)(x^2-x+1) double coefficients[] = { -12.0, -1.0, 1.0, -12.0, -1.0, 1.0 }; PolynomialFunction f = new PolynomialFunction(coefficients); LaguerreSolver solver = new LaguerreSolver(); min = -2.0; max = 2.0; expected = -1.0; tolerance = FastMath.max(solver.getAbsoluteAccuracy(), FastMath.abs(expected * solver.getRelativeAccuracy())); result = solver.solve(100, f, min, max); Assert.assertEquals(expected, result, tolerance); min = -5.0; max = -2.5; expected = -3.0; tolerance = FastMath.max(solver.getAbsoluteAccuracy(), FastMath.abs(expected * solver.getRelativeAccuracy())); result = solver.solve(100, f, min, max); Assert.assertEquals(expected, result, tolerance); min = 3.0; max = 6.0; expected = 4.0; tolerance = FastMath.max(solver.getAbsoluteAccuracy(), FastMath.abs(expected * solver.getRelativeAccuracy())); result = solver.solve(100, f, min, max); Assert.assertEquals(expected, result, tolerance); } /** * Test of solver for the quintic function using * {@link LaguerreSolver#solveAllComplex(double[],double) solveAllComplex}. */ @Test public void testQuinticFunction2() { // p(x) = x^5 + 4x^3 + x^2 + 4 = (x+1)(x^2-x+1)(x^2+4) final double[] coefficients = { 4.0, 0.0, 1.0, 4.0, 0.0, 1.0 }; final LaguerreSolver solver = new LaguerreSolver(); final Complex[] result = solver.solveAllComplex(coefficients, 0); for (Complex expected : new Complex[] { new Complex(0, -2), new Complex(0, 2), new Complex(0.5, 0.5 * FastMath.sqrt(3)), new Complex(-1, 0), new Complex(0.5, -0.5 * FastMath.sqrt(3.0)) }) { final double tolerance = FastMath.max(solver.getAbsoluteAccuracy(), FastMath.abs(expected.abs() * solver.getRelativeAccuracy())); TestUtils.assertContains(result, expected, tolerance); } } /** * Test of parameters for the solver. */ @Test public void testParameters() { double coefficients[] = { -3.0, 5.0, 2.0 }; PolynomialFunction f = new PolynomialFunction(coefficients); LaguerreSolver solver = new LaguerreSolver(); try { // bad interval solver.solve(100, f, 1, -1); Assert.fail("Expecting NumberIsTooLargeException - bad interval"); } catch (NumberIsTooLargeException ex) { // expected } try { // no bracketing solver.solve(100, f, 2, 3); Assert.fail("Expecting NoBracketingException - no bracketing"); } catch (NoBracketingException ex) { // expected } } } ././@LongLink100644 0 0 145 12126630646 10261 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/analysis/solvers/PegasusSolverTest.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/analysis/solvers/PegasusSolverTest.java100644 1750 1750 2423 12126627670 32406 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.analysis.solvers; /** * Test case for {@link PegasusSolver Pegasus} solver. * * @version $Id$ */ public final class PegasusSolverTest extends BaseSecantSolverAbstractTest { /** {@inheritDoc} */ @Override protected UnivariateSolver getSolver() { return new PegasusSolver(); } /** {@inheritDoc} */ @Override protected int[] getQuinticEvalCounts() { return new int[] {3, 7, 9, 8, 9, 8, 10, 10, 12, 16, 18}; } } commons-math3-3.2-src/src/test/java/org/apache/commons/math3/analysis/solvers/NewtonSolverTest.java100644 1750 1750 10006 12126627670 32265 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.analysis.solvers; import org.apache.commons.math3.analysis.DifferentiableUnivariateFunction; import org.apache.commons.math3.analysis.QuinticFunction; import org.apache.commons.math3.analysis.UnivariateFunction; import org.apache.commons.math3.analysis.differentiation.DerivativeStructure; import org.apache.commons.math3.analysis.differentiation.UnivariateDifferentiableFunction; import org.apache.commons.math3.analysis.function.Sin; import org.apache.commons.math3.util.FastMath; import org.junit.Assert; import org.junit.Test; /** * @version $Id: NewtonSolverTest.java 1383441 2012-09-11 14:56:39Z luc $ * @deprecated */ @Deprecated public final class NewtonSolverTest { /** * */ @Test public void testSinZero() { DifferentiableUnivariateFunction f = new Sin(); double result; NewtonSolver solver = new NewtonSolver(); result = solver.solve(100, f, 3, 4); Assert.assertEquals(result, FastMath.PI, solver.getAbsoluteAccuracy()); result = solver.solve(100, f, 1, 4); Assert.assertEquals(result, FastMath.PI, solver.getAbsoluteAccuracy()); Assert.assertTrue(solver.getEvaluations() > 0); } /** * */ @Test public void testQuinticZero() { final UnivariateDifferentiableFunction q = new QuinticFunction(); DifferentiableUnivariateFunction f = new DifferentiableUnivariateFunction() { public double value(double x) { return q.value(x); } public UnivariateFunction derivative() { return new UnivariateFunction() { public double value(double x) { return q.value(new DerivativeStructure(1, 1, 0, x)).getPartialDerivative(1); } }; } }; double result; NewtonSolver solver = new NewtonSolver(); result = solver.solve(100, f, -0.2, 0.2); Assert.assertEquals(result, 0, solver.getAbsoluteAccuracy()); result = solver.solve(100, f, -0.1, 0.3); Assert.assertEquals(result, 0, solver.getAbsoluteAccuracy()); result = solver.solve(100, f, -0.3, 0.45); Assert.assertEquals(result, 0, solver.getAbsoluteAccuracy()); result = solver.solve(100, f, 0.3, 0.7); Assert.assertEquals(result, 0.5, solver.getAbsoluteAccuracy()); result = solver.solve(100, f, 0.2, 0.6); Assert.assertEquals(result, 0.5, solver.getAbsoluteAccuracy()); result = solver.solve(100, f, 0.05, 0.95); Assert.assertEquals(result, 0.5, solver.getAbsoluteAccuracy()); result = solver.solve(100, f, 0.85, 1.25); Assert.assertEquals(result, 1.0, solver.getAbsoluteAccuracy()); result = solver.solve(100, f, 0.8, 1.2); Assert.assertEquals(result, 1.0, solver.getAbsoluteAccuracy()); result = solver.solve(100, f, 0.85, 1.75); Assert.assertEquals(result, 1.0, solver.getAbsoluteAccuracy()); result = solver.solve(100, f, 0.55, 1.45); Assert.assertEquals(result, 1.0, solver.getAbsoluteAccuracy()); result = solver.solve(100, f, 0.85, 5); Assert.assertEquals(result, 1.0, solver.getAbsoluteAccuracy()); } } commons-math3-3.2-src/src/test/java/org/apache/commons/math3/analysis/QuinticFunction.java100644 1750 1750 3064 12126627670 30373 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.analysis; import org.apache.commons.math3.analysis.differentiation.DerivativeStructure; import org.apache.commons.math3.analysis.differentiation.UnivariateDifferentiableFunction; /** * Auxiliary class for testing solvers. * * @version $Id: QuinticFunction.java 1383441 2012-09-11 14:56:39Z luc $ */ public class QuinticFunction implements UnivariateDifferentiableFunction { /* Evaluate quintic. * @see org.apache.commons.math3.UnivariateFunction#value(double) */ public double value(double x) { return (x-1)*(x-0.5)*x*(x+0.5)*(x+1); } public DerivativeStructure value(DerivativeStructure t) { return t.subtract(1).multiply(t.subtract(0.5)).multiply(t).multiply(t.add(0.5)).multiply(t.add(1)); } } commons-math3-3.2-src/src/test/java/org/apache/commons/math3/optim/PointValuePairTest.java100644 1750 1750 2677 12126627671 30332 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.optim; import org.apache.commons.math3.TestUtils; import org.junit.Assert; import org.junit.Test; public class PointValuePairTest { @Test public void testSerial() { PointValuePair pv1 = new PointValuePair(new double[] { 1.0, 2.0, 3.0 }, 4.0); PointValuePair pv2 = (PointValuePair) TestUtils.serializeAndRecover(pv1); Assert.assertEquals(pv1.getKey().length, pv2.getKey().length); for (int i = 0; i < pv1.getKey().length; ++i) { Assert.assertEquals(pv1.getKey()[i], pv2.getKey()[i], 1.0e-15); } Assert.assertEquals(pv1.getValue(), pv2.getValue(), 1.0e-15); } } ././@LongLink100644 0 0 145 12126630646 10261 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/optim/SimpleVectorValueCheckerTest.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/optim/SimpleVectorValueCheckerTest.java100644 1750 1750 4343 12126627671 32316 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.optim; import org.apache.commons.math3.exception.NotStrictlyPositiveException; import org.junit.Test; import org.junit.Assert; public class SimpleVectorValueCheckerTest { @Test(expected=NotStrictlyPositiveException.class) public void testIterationCheckPrecondition() { new SimpleVectorValueChecker(1e-1, 1e-2, 0); } @Test public void testIterationCheck() { final int max = 10; final SimpleVectorValueChecker checker = new SimpleVectorValueChecker(1e-1, 1e-2, max); Assert.assertTrue(checker.converged(max, null, null)); Assert.assertTrue(checker.converged(max + 1, null, null)); } @Test public void testIterationCheckDisabled() { final SimpleVectorValueChecker checker = new SimpleVectorValueChecker(1e-8, 1e-8); final PointVectorValuePair a = new PointVectorValuePair(new double[] { 1d }, new double[] { 1d }); final PointVectorValuePair b = new PointVectorValuePair(new double[] { 10d }, new double[] { 10d }); Assert.assertFalse(checker.converged(-1, a, b)); Assert.assertFalse(checker.converged(0, a, b)); Assert.assertFalse(checker.converged(1000000, a, b)); Assert.assertTrue(checker.converged(-1, a, a)); Assert.assertTrue(checker.converged(-1, b, b)); } } commons-math3-3.2-src/src/test/java/org/apache/commons/math3/optim/SimpleValueCheckerTest.java100644 1750 1750 4007 12126627671 31130 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.optim; import org.apache.commons.math3.exception.NotStrictlyPositiveException; import org.junit.Test; import org.junit.Assert; public class SimpleValueCheckerTest { @Test(expected=NotStrictlyPositiveException.class) public void testIterationCheckPrecondition() { new SimpleValueChecker(1e-1, 1e-2, 0); } @Test public void testIterationCheck() { final int max = 10; final SimpleValueChecker checker = new SimpleValueChecker(1e-1, 1e-2, max); Assert.assertTrue(checker.converged(max, null, null)); Assert.assertTrue(checker.converged(max + 1, null, null)); } @Test public void testIterationCheckDisabled() { final SimpleValueChecker checker = new SimpleValueChecker(1e-8, 1e-8); final PointValuePair a = new PointValuePair(new double[] { 1d }, 1d); final PointValuePair b = new PointValuePair(new double[] { 10d }, 10d); Assert.assertFalse(checker.converged(-1, a, b)); Assert.assertFalse(checker.converged(0, a, b)); Assert.assertFalse(checker.converged(1000000, a, b)); Assert.assertTrue(checker.converged(-1, a, a)); Assert.assertTrue(checker.converged(-1, b, b)); } } commons-math3-3.2-src/src/test/java/org/apache/commons/math3/optim/SimplePointCheckerTest.java100644 1750 1750 4156 12126627671 31152 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.optim; import org.apache.commons.math3.exception.NotStrictlyPositiveException; import org.junit.Test; import org.junit.Assert; public class SimplePointCheckerTest { @Test(expected=NotStrictlyPositiveException.class) public void testIterationCheckPrecondition() { new SimplePointChecker(1e-1, 1e-2, 0); } @Test public void testIterationCheck() { final int max = 10; final SimplePointChecker checker = new SimplePointChecker(1e-1, 1e-2, max); Assert.assertTrue(checker.converged(max, null, null)); Assert.assertTrue(checker.converged(max + 1, null, null)); } @Test public void testIterationCheckDisabled() { final SimplePointChecker checker = new SimplePointChecker(1e-8, 1e-8); final PointValuePair a = new PointValuePair(new double[] { 1d }, 1d); final PointValuePair b = new PointValuePair(new double[] { 10d }, 10d); Assert.assertFalse(checker.converged(-1, a, b)); Assert.assertFalse(checker.converged(0, a, b)); Assert.assertFalse(checker.converged(1000000, a, b)); Assert.assertTrue(checker.converged(-1, a, a)); Assert.assertTrue(checker.converged(-1, b, b)); } } commons-math3-3.2-src/src/test/java/org/apache/commons/math3/optim/linear/SimplexTableauTest.java100644 1750 1750 12022 12126627671 31622 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.optim.linear; import java.util.ArrayList; import java.util.Collection; import org.apache.commons.math3.TestUtils; import org.apache.commons.math3.optim.nonlinear.scalar.GoalType; import org.junit.Assert; import org.junit.Test; public class SimplexTableauTest { @Test public void testInitialization() { LinearObjectiveFunction f = createFunction(); Collection constraints = createConstraints(); SimplexTableau tableau = new SimplexTableau(f, constraints, GoalType.MAXIMIZE, false, 1.0e-6); double[][] expectedInitialTableau = { {-1, 0, -1, -1, 2, 0, 0, 0, -4}, { 0, 1, -15, -10, 25, 0, 0, 0, 0}, { 0, 0, 1, 0, -1, 1, 0, 0, 2}, { 0, 0, 0, 1, -1, 0, 1, 0, 3}, { 0, 0, 1, 1, -2, 0, 0, 1, 4} }; assertMatrixEquals(expectedInitialTableau, tableau.getData()); } @Test public void testDropPhase1Objective() { LinearObjectiveFunction f = createFunction(); Collection constraints = createConstraints(); SimplexTableau tableau = new SimplexTableau(f, constraints, GoalType.MAXIMIZE, false, 1.0e-6); double[][] expectedTableau = { { 1, -15, -10, 0, 0, 0, 0}, { 0, 1, 0, 1, 0, 0, 2}, { 0, 0, 1, 0, 1, 0, 3}, { 0, 1, 1, 0, 0, 1, 4} }; tableau.dropPhase1Objective(); assertMatrixEquals(expectedTableau, tableau.getData()); } @Test public void testTableauWithNoArtificialVars() { LinearObjectiveFunction f = new LinearObjectiveFunction(new double[] {15, 10}, 0); Collection constraints = new ArrayList(); constraints.add(new LinearConstraint(new double[] {1, 0}, Relationship.LEQ, 2)); constraints.add(new LinearConstraint(new double[] {0, 1}, Relationship.LEQ, 3)); constraints.add(new LinearConstraint(new double[] {1, 1}, Relationship.LEQ, 4)); SimplexTableau tableau = new SimplexTableau(f, constraints, GoalType.MAXIMIZE, false, 1.0e-6); double[][] initialTableau = { {1, -15, -10, 25, 0, 0, 0, 0}, {0, 1, 0, -1, 1, 0, 0, 2}, {0, 0, 1, -1, 0, 1, 0, 3}, {0, 1, 1, -2, 0, 0, 1, 4} }; assertMatrixEquals(initialTableau, tableau.getData()); } @Test public void testSerial() { LinearObjectiveFunction f = createFunction(); Collection constraints = createConstraints(); SimplexTableau tableau = new SimplexTableau(f, constraints, GoalType.MAXIMIZE, false, 1.0e-6); Assert.assertEquals(tableau, TestUtils.serializeAndRecover(tableau)); } private LinearObjectiveFunction createFunction() { return new LinearObjectiveFunction(new double[] {15, 10}, 0); } private Collection createConstraints() { Collection constraints = new ArrayList(); constraints.add(new LinearConstraint(new double[] {1, 0}, Relationship.LEQ, 2)); constraints.add(new LinearConstraint(new double[] {0, 1}, Relationship.LEQ, 3)); constraints.add(new LinearConstraint(new double[] {1, 1}, Relationship.EQ, 4)); return constraints; } private void assertMatrixEquals(double[][] expected, double[][] result) { Assert.assertEquals("Wrong number of rows.", expected.length, result.length); for (int i = 0; i < expected.length; i++) { Assert.assertEquals("Wrong number of columns.", expected[i].length, result[i].length); for (int j = 0; j < expected[i].length; j++) { Assert.assertEquals("Wrong value at position [" + i + "," + j + "]", expected[i][j], result[i][j], 1.0e-15); } } } } commons-math3-3.2-src/src/test/java/org/apache/commons/math3/optim/linear/SimplexSolverTest.java100644 1750 1750 173445 12126627671 31560 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.optim.linear; import java.util.ArrayList; import java.util.Collection; import java.util.List; import org.apache.commons.math3.optim.MaxIter; import org.apache.commons.math3.optim.nonlinear.scalar.GoalType; import org.apache.commons.math3.optim.PointValuePair; import org.apache.commons.math3.util.Precision; import org.junit.Test; import org.junit.Assert; public class SimplexSolverTest { private static final MaxIter DEFAULT_MAX_ITER = new MaxIter(100); @Test public void testMath828() { LinearObjectiveFunction f = new LinearObjectiveFunction( new double[] { 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0}, 0.0); ArrayList constraints = new ArrayList(); constraints.add(new LinearConstraint(new double[] {0.0, 39.0, 23.0, 96.0, 15.0, 48.0, 9.0, 21.0, 48.0, 36.0, 76.0, 19.0, 88.0, 17.0, 16.0, 36.0,}, Relationship.GEQ, 15.0)); constraints.add(new LinearConstraint(new double[] {0.0, 59.0, 93.0, 12.0, 29.0, 78.0, 73.0, 87.0, 32.0, 70.0, 68.0, 24.0, 11.0, 26.0, 65.0, 25.0,}, Relationship.GEQ, 29.0)); constraints.add(new LinearConstraint(new double[] {0.0, 74.0, 5.0, 82.0, 6.0, 97.0, 55.0, 44.0, 52.0, 54.0, 5.0, 93.0, 91.0, 8.0, 20.0, 97.0,}, Relationship.GEQ, 6.0)); constraints.add(new LinearConstraint(new double[] {8.0, -3.0, -28.0, -72.0, -8.0, -31.0, -31.0, -74.0, -47.0, -59.0, -24.0, -57.0, -56.0, -16.0, -92.0, -59.0,}, Relationship.GEQ, 0.0)); constraints.add(new LinearConstraint(new double[] {25.0, -7.0, -99.0, -78.0, -25.0, -14.0, -16.0, -89.0, -39.0, -56.0, -53.0, -9.0, -18.0, -26.0, -11.0, -61.0,}, Relationship.GEQ, 0.0)); constraints.add(new LinearConstraint(new double[] {33.0, -95.0, -15.0, -4.0, -33.0, -3.0, -20.0, -96.0, -27.0, -13.0, -80.0, -24.0, -3.0, -13.0, -57.0, -76.0,}, Relationship.GEQ, 0.0)); constraints.add(new LinearConstraint(new double[] {7.0, -95.0, -39.0, -93.0, -7.0, -94.0, -94.0, -62.0, -76.0, -26.0, -53.0, -57.0, -31.0, -76.0, -53.0, -52.0,}, Relationship.GEQ, 0.0)); double epsilon = 1e-6; PointValuePair solution = new SimplexSolver().optimize(DEFAULT_MAX_ITER, f, new LinearConstraintSet(constraints), GoalType.MINIMIZE, new NonNegativeConstraint(true)); Assert.assertEquals(1.0d, solution.getValue(), epsilon); Assert.assertTrue(validSolution(solution, constraints, epsilon)); } @Test public void testMath828Cycle() { LinearObjectiveFunction f = new LinearObjectiveFunction( new double[] { 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0}, 0.0); ArrayList constraints = new ArrayList(); constraints.add(new LinearConstraint(new double[] {0.0, 16.0, 14.0, 69.0, 1.0, 85.0, 52.0, 43.0, 64.0, 97.0, 14.0, 74.0, 89.0, 28.0, 94.0, 58.0, 13.0, 22.0, 21.0, 17.0, 30.0, 25.0, 1.0, 59.0, 91.0, 78.0, 12.0, 74.0, 56.0, 3.0, 88.0,}, Relationship.GEQ, 91.0)); constraints.add(new LinearConstraint(new double[] {0.0, 60.0, 40.0, 81.0, 71.0, 72.0, 46.0, 45.0, 38.0, 48.0, 40.0, 17.0, 33.0, 85.0, 64.0, 32.0, 84.0, 3.0, 54.0, 44.0, 71.0, 67.0, 90.0, 95.0, 54.0, 99.0, 99.0, 29.0, 52.0, 98.0, 9.0,}, Relationship.GEQ, 54.0)); constraints.add(new LinearConstraint(new double[] {0.0, 41.0, 12.0, 86.0, 90.0, 61.0, 31.0, 41.0, 23.0, 89.0, 17.0, 74.0, 44.0, 27.0, 16.0, 47.0, 80.0, 32.0, 11.0, 56.0, 68.0, 82.0, 11.0, 62.0, 62.0, 53.0, 39.0, 16.0, 48.0, 1.0, 63.0,}, Relationship.GEQ, 62.0)); constraints.add(new LinearConstraint(new double[] {83.0, -76.0, -94.0, -19.0, -15.0, -70.0, -72.0, -57.0, -63.0, -65.0, -22.0, -94.0, -22.0, -88.0, -86.0, -89.0, -72.0, -16.0, -80.0, -49.0, -70.0, -93.0, -95.0, -17.0, -83.0, -97.0, -31.0, -47.0, -31.0, -13.0, -23.0,}, Relationship.GEQ, 0.0)); constraints.add(new LinearConstraint(new double[] {41.0, -96.0, -41.0, -48.0, -70.0, -43.0, -43.0, -43.0, -97.0, -37.0, -85.0, -70.0, -45.0, -67.0, -87.0, -69.0, -94.0, -54.0, -54.0, -92.0, -79.0, -10.0, -35.0, -20.0, -41.0, -41.0, -65.0, -25.0, -12.0, -8.0, -46.0,}, Relationship.GEQ, 0.0)); constraints.add(new LinearConstraint(new double[] {27.0, -42.0, -65.0, -49.0, -53.0, -42.0, -17.0, -2.0, -61.0, -31.0, -76.0, -47.0, -8.0, -93.0, -86.0, -62.0, -65.0, -63.0, -22.0, -43.0, -27.0, -23.0, -32.0, -74.0, -27.0, -63.0, -47.0, -78.0, -29.0, -95.0, -73.0,}, Relationship.GEQ, 0.0)); constraints.add(new LinearConstraint(new double[] {15.0, -46.0, -41.0, -83.0, -98.0, -99.0, -21.0, -35.0, -7.0, -14.0, -80.0, -63.0, -18.0, -42.0, -5.0, -34.0, -56.0, -70.0, -16.0, -18.0, -74.0, -61.0, -47.0, -41.0, -15.0, -79.0, -18.0, -47.0, -88.0, -68.0, -55.0,}, Relationship.GEQ, 0.0)); double epsilon = 1e-6; PointValuePair solution = new SimplexSolver().optimize(DEFAULT_MAX_ITER, f, new LinearConstraintSet(constraints), GoalType.MINIMIZE, new NonNegativeConstraint(true)); Assert.assertEquals(1.0d, solution.getValue(), epsilon); Assert.assertTrue(validSolution(solution, constraints, epsilon)); } @Test public void testMath781() { LinearObjectiveFunction f = new LinearObjectiveFunction(new double[] { 2, 6, 7 }, 0); ArrayList constraints = new ArrayList(); constraints.add(new LinearConstraint(new double[] { 1, 2, 1 }, Relationship.LEQ, 2)); constraints.add(new LinearConstraint(new double[] { -1, 1, 1 }, Relationship.LEQ, -1)); constraints.add(new LinearConstraint(new double[] { 2, -3, 1 }, Relationship.LEQ, -1)); double epsilon = 1e-6; SimplexSolver solver = new SimplexSolver(); PointValuePair solution = solver.optimize(DEFAULT_MAX_ITER, f, new LinearConstraintSet(constraints), GoalType.MAXIMIZE, new NonNegativeConstraint(false)); Assert.assertTrue(Precision.compareTo(solution.getPoint()[0], 0.0d, epsilon) > 0); Assert.assertTrue(Precision.compareTo(solution.getPoint()[1], 0.0d, epsilon) > 0); Assert.assertTrue(Precision.compareTo(solution.getPoint()[2], 0.0d, epsilon) < 0); Assert.assertEquals(2.0d, solution.getValue(), epsilon); } @Test public void testMath713NegativeVariable() { LinearObjectiveFunction f = new LinearObjectiveFunction(new double[] {1.0, 1.0}, 0.0d); ArrayList constraints = new ArrayList(); constraints.add(new LinearConstraint(new double[] {1, 0}, Relationship.EQ, 1)); double epsilon = 1e-6; SimplexSolver solver = new SimplexSolver(); PointValuePair solution = solver.optimize(DEFAULT_MAX_ITER, f, new LinearConstraintSet(constraints), GoalType.MINIMIZE, new NonNegativeConstraint(true)); Assert.assertTrue(Precision.compareTo(solution.getPoint()[0], 0.0d, epsilon) >= 0); Assert.assertTrue(Precision.compareTo(solution.getPoint()[1], 0.0d, epsilon) >= 0); } @Test public void testMath434NegativeVariable() { LinearObjectiveFunction f = new LinearObjectiveFunction(new double[] {0.0, 0.0, 1.0}, 0.0d); ArrayList constraints = new ArrayList(); constraints.add(new LinearConstraint(new double[] {1, 1, 0}, Relationship.EQ, 5)); constraints.add(new LinearConstraint(new double[] {0, 0, 1}, Relationship.GEQ, -10)); double epsilon = 1e-6; SimplexSolver solver = new SimplexSolver(); PointValuePair solution = solver.optimize(DEFAULT_MAX_ITER, f, new LinearConstraintSet(constraints), GoalType.MINIMIZE, new NonNegativeConstraint(false)); Assert.assertEquals(5.0, solution.getPoint()[0] + solution.getPoint()[1], epsilon); Assert.assertEquals(-10.0, solution.getPoint()[2], epsilon); Assert.assertEquals(-10.0, solution.getValue(), epsilon); } @Test(expected = NoFeasibleSolutionException.class) public void testMath434UnfeasibleSolution() { double epsilon = 1e-6; LinearObjectiveFunction f = new LinearObjectiveFunction(new double[] {1.0, 0.0}, 0.0); ArrayList constraints = new ArrayList(); constraints.add(new LinearConstraint(new double[] {epsilon/2, 0.5}, Relationship.EQ, 0)); constraints.add(new LinearConstraint(new double[] {1e-3, 0.1}, Relationship.EQ, 10)); SimplexSolver solver = new SimplexSolver(); // allowing only non-negative values, no feasible solution shall be found solver.optimize(DEFAULT_MAX_ITER, f, new LinearConstraintSet(constraints), GoalType.MINIMIZE, new NonNegativeConstraint(true)); } @Test public void testMath434PivotRowSelection() { LinearObjectiveFunction f = new LinearObjectiveFunction(new double[] {1.0}, 0.0); double epsilon = 1e-6; ArrayList constraints = new ArrayList(); constraints.add(new LinearConstraint(new double[] {200}, Relationship.GEQ, 1)); constraints.add(new LinearConstraint(new double[] {100}, Relationship.GEQ, 0.499900001)); SimplexSolver solver = new SimplexSolver(); PointValuePair solution = solver.optimize(DEFAULT_MAX_ITER, f, new LinearConstraintSet(constraints), GoalType.MINIMIZE, new NonNegativeConstraint(false)); Assert.assertTrue(Precision.compareTo(solution.getPoint()[0] * 200.d, 1.d, epsilon) >= 0); Assert.assertEquals(0.0050, solution.getValue(), epsilon); } @Test public void testMath434PivotRowSelection2() { LinearObjectiveFunction f = new LinearObjectiveFunction(new double[] {0.0d, 1.0d, 1.0d, 0.0d, 0.0d, 0.0d, 0.0d}, 0.0d); ArrayList constraints = new ArrayList(); constraints.add(new LinearConstraint(new double[] {1.0d, -0.1d, 0.0d, 0.0d, 0.0d, 0.0d, 0.0d}, Relationship.EQ, -0.1d)); constraints.add(new LinearConstraint(new double[] {1.0d, 0.0d, 0.0d, 0.0d, 0.0d, 0.0d, 0.0d}, Relationship.GEQ, -1e-18d)); constraints.add(new LinearConstraint(new double[] {0.0d, 1.0d, 0.0d, 0.0d, 0.0d, 0.0d, 0.0d}, Relationship.GEQ, 0.0d)); constraints.add(new LinearConstraint(new double[] {0.0d, 0.0d, 0.0d, 1.0d, 0.0d, -0.0128588d, 1e-5d}, Relationship.EQ, 0.0d)); constraints.add(new LinearConstraint(new double[] {0.0d, 0.0d, 0.0d, 0.0d, 1.0d, 1e-5d, -0.0128586d}, Relationship.EQ, 1e-10d)); constraints.add(new LinearConstraint(new double[] {0.0d, 0.0d, 1.0d, -1.0d, 0.0d, 0.0d, 0.0d}, Relationship.GEQ, 0.0d)); constraints.add(new LinearConstraint(new double[] {0.0d, 0.0d, 1.0d, 1.0d, 0.0d, 0.0d, 0.0d}, Relationship.GEQ, 0.0d)); constraints.add(new LinearConstraint(new double[] {0.0d, 0.0d, 1.0d, 0.0d, -1.0d, 0.0d, 0.0d}, Relationship.GEQ, 0.0d)); constraints.add(new LinearConstraint(new double[] {0.0d, 0.0d, 1.0d, 0.0d, 1.0d, 0.0d, 0.0d}, Relationship.GEQ, 0.0d)); double epsilon = 1e-7; SimplexSolver simplex = new SimplexSolver(); PointValuePair solution = simplex.optimize(DEFAULT_MAX_ITER, f, new LinearConstraintSet(constraints), GoalType.MINIMIZE, new NonNegativeConstraint(false)); Assert.assertTrue(Precision.compareTo(solution.getPoint()[0], -1e-18d, epsilon) >= 0); Assert.assertEquals(1.0d, solution.getPoint()[1], epsilon); Assert.assertEquals(0.0d, solution.getPoint()[2], epsilon); Assert.assertEquals(1.0d, solution.getValue(), epsilon); } @Test public void testMath272() { LinearObjectiveFunction f = new LinearObjectiveFunction(new double[] { 2, 2, 1 }, 0); Collection constraints = new ArrayList(); constraints.add(new LinearConstraint(new double[] { 1, 1, 0 }, Relationship.GEQ, 1)); constraints.add(new LinearConstraint(new double[] { 1, 0, 1 }, Relationship.GEQ, 1)); constraints.add(new LinearConstraint(new double[] { 0, 1, 0 }, Relationship.GEQ, 1)); SimplexSolver solver = new SimplexSolver(); PointValuePair solution = solver.optimize(DEFAULT_MAX_ITER, f, new LinearConstraintSet(constraints), GoalType.MINIMIZE, new NonNegativeConstraint(true)); Assert.assertEquals(0.0, solution.getPoint()[0], .0000001); Assert.assertEquals(1.0, solution.getPoint()[1], .0000001); Assert.assertEquals(1.0, solution.getPoint()[2], .0000001); Assert.assertEquals(3.0, solution.getValue(), .0000001); } @Test public void testMath286() { LinearObjectiveFunction f = new LinearObjectiveFunction(new double[] { 0.8, 0.2, 0.7, 0.3, 0.6, 0.4 }, 0 ); Collection constraints = new ArrayList(); constraints.add(new LinearConstraint(new double[] { 1, 0, 1, 0, 1, 0 }, Relationship.EQ, 23.0)); constraints.add(new LinearConstraint(new double[] { 0, 1, 0, 1, 0, 1 }, Relationship.EQ, 23.0)); constraints.add(new LinearConstraint(new double[] { 1, 0, 0, 0, 0, 0 }, Relationship.GEQ, 10.0)); constraints.add(new LinearConstraint(new double[] { 0, 0, 1, 0, 0, 0 }, Relationship.GEQ, 8.0)); constraints.add(new LinearConstraint(new double[] { 0, 0, 0, 0, 1, 0 }, Relationship.GEQ, 5.0)); SimplexSolver solver = new SimplexSolver(); PointValuePair solution = solver.optimize(DEFAULT_MAX_ITER, f, new LinearConstraintSet(constraints), GoalType.MAXIMIZE, new NonNegativeConstraint(true)); Assert.assertEquals(25.8, solution.getValue(), .0000001); Assert.assertEquals(23.0, solution.getPoint()[0] + solution.getPoint()[2] + solution.getPoint()[4], 0.0000001); Assert.assertEquals(23.0, solution.getPoint()[1] + solution.getPoint()[3] + solution.getPoint()[5], 0.0000001); Assert.assertTrue(solution.getPoint()[0] >= 10.0 - 0.0000001); Assert.assertTrue(solution.getPoint()[2] >= 8.0 - 0.0000001); Assert.assertTrue(solution.getPoint()[4] >= 5.0 - 0.0000001); } @Test public void testDegeneracy() { LinearObjectiveFunction f = new LinearObjectiveFunction(new double[] { 0.8, 0.7 }, 0 ); Collection constraints = new ArrayList(); constraints.add(new LinearConstraint(new double[] { 1, 1 }, Relationship.LEQ, 18.0)); constraints.add(new LinearConstraint(new double[] { 1, 0 }, Relationship.GEQ, 10.0)); constraints.add(new LinearConstraint(new double[] { 0, 1 }, Relationship.GEQ, 8.0)); SimplexSolver solver = new SimplexSolver(); PointValuePair solution = solver.optimize(DEFAULT_MAX_ITER, f, new LinearConstraintSet(constraints), GoalType.MINIMIZE, new NonNegativeConstraint(true)); Assert.assertEquals(13.6, solution.getValue(), .0000001); } @Test public void testMath288() { LinearObjectiveFunction f = new LinearObjectiveFunction(new double[] { 7, 3, 0, 0 }, 0 ); Collection constraints = new ArrayList(); constraints.add(new LinearConstraint(new double[] { 3, 0, -5, 0 }, Relationship.LEQ, 0.0)); constraints.add(new LinearConstraint(new double[] { 2, 0, 0, -5 }, Relationship.LEQ, 0.0)); constraints.add(new LinearConstraint(new double[] { 0, 3, 0, -5 }, Relationship.LEQ, 0.0)); constraints.add(new LinearConstraint(new double[] { 1, 0, 0, 0 }, Relationship.LEQ, 1.0)); constraints.add(new LinearConstraint(new double[] { 0, 1, 0, 0 }, Relationship.LEQ, 1.0)); SimplexSolver solver = new SimplexSolver(); PointValuePair solution = solver.optimize(DEFAULT_MAX_ITER, f, new LinearConstraintSet(constraints), GoalType.MAXIMIZE, new NonNegativeConstraint(true)); Assert.assertEquals(10.0, solution.getValue(), .0000001); } @Test public void testMath290GEQ() { LinearObjectiveFunction f = new LinearObjectiveFunction(new double[] { 1, 5 }, 0 ); Collection constraints = new ArrayList(); constraints.add(new LinearConstraint(new double[] { 2, 0 }, Relationship.GEQ, -1.0)); SimplexSolver solver = new SimplexSolver(); PointValuePair solution = solver.optimize(DEFAULT_MAX_ITER, f, new LinearConstraintSet(constraints), GoalType.MINIMIZE, new NonNegativeConstraint(true)); Assert.assertEquals(0, solution.getValue(), .0000001); Assert.assertEquals(0, solution.getPoint()[0], .0000001); Assert.assertEquals(0, solution.getPoint()[1], .0000001); } @Test(expected=NoFeasibleSolutionException.class) public void testMath290LEQ() { LinearObjectiveFunction f = new LinearObjectiveFunction(new double[] { 1, 5 }, 0 ); Collection constraints = new ArrayList(); constraints.add(new LinearConstraint(new double[] { 2, 0 }, Relationship.LEQ, -1.0)); SimplexSolver solver = new SimplexSolver(); solver.optimize(DEFAULT_MAX_ITER, f, new LinearConstraintSet(constraints), GoalType.MINIMIZE, new NonNegativeConstraint(true)); } @Test public void testMath293() { LinearObjectiveFunction f = new LinearObjectiveFunction(new double[] { 0.8, 0.2, 0.7, 0.3, 0.4, 0.6}, 0 ); Collection constraints = new ArrayList(); constraints.add(new LinearConstraint(new double[] { 1, 0, 1, 0, 1, 0 }, Relationship.EQ, 30.0)); constraints.add(new LinearConstraint(new double[] { 0, 1, 0, 1, 0, 1 }, Relationship.EQ, 30.0)); constraints.add(new LinearConstraint(new double[] { 0.8, 0.2, 0.0, 0.0, 0.0, 0.0 }, Relationship.GEQ, 10.0)); constraints.add(new LinearConstraint(new double[] { 0.0, 0.0, 0.7, 0.3, 0.0, 0.0 }, Relationship.GEQ, 10.0)); constraints.add(new LinearConstraint(new double[] { 0.0, 0.0, 0.0, 0.0, 0.4, 0.6 }, Relationship.GEQ, 10.0)); SimplexSolver solver = new SimplexSolver(); PointValuePair solution1 = solver.optimize(DEFAULT_MAX_ITER, f, new LinearConstraintSet(constraints), GoalType.MAXIMIZE, new NonNegativeConstraint(true)); Assert.assertEquals(15.7143, solution1.getPoint()[0], .0001); Assert.assertEquals(0.0, solution1.getPoint()[1], .0001); Assert.assertEquals(14.2857, solution1.getPoint()[2], .0001); Assert.assertEquals(0.0, solution1.getPoint()[3], .0001); Assert.assertEquals(0.0, solution1.getPoint()[4], .0001); Assert.assertEquals(30.0, solution1.getPoint()[5], .0001); Assert.assertEquals(40.57143, solution1.getValue(), .0001); double valA = 0.8 * solution1.getPoint()[0] + 0.2 * solution1.getPoint()[1]; double valB = 0.7 * solution1.getPoint()[2] + 0.3 * solution1.getPoint()[3]; double valC = 0.4 * solution1.getPoint()[4] + 0.6 * solution1.getPoint()[5]; f = new LinearObjectiveFunction(new double[] { 0.8, 0.2, 0.7, 0.3, 0.4, 0.6}, 0 ); constraints = new ArrayList(); constraints.add(new LinearConstraint(new double[] { 1, 0, 1, 0, 1, 0 }, Relationship.EQ, 30.0)); constraints.add(new LinearConstraint(new double[] { 0, 1, 0, 1, 0, 1 }, Relationship.EQ, 30.0)); constraints.add(new LinearConstraint(new double[] { 0.8, 0.2, 0.0, 0.0, 0.0, 0.0 }, Relationship.GEQ, valA)); constraints.add(new LinearConstraint(new double[] { 0.0, 0.0, 0.7, 0.3, 0.0, 0.0 }, Relationship.GEQ, valB)); constraints.add(new LinearConstraint(new double[] { 0.0, 0.0, 0.0, 0.0, 0.4, 0.6 }, Relationship.GEQ, valC)); PointValuePair solution2 = solver.optimize(DEFAULT_MAX_ITER, f, new LinearConstraintSet(constraints), GoalType.MAXIMIZE, new NonNegativeConstraint(true)); Assert.assertEquals(40.57143, solution2.getValue(), .0001); } @Test public void testMath930() { Collection constraints = new ArrayList(); constraints.add(new LinearConstraint(new double[] {1, -1, -1, 1, -1, 1, 1, -1, -1, 1, 1, -1, 1, -1, -1, 1, -1, 1, 1, -1, 1, -1, -1, 1, 1, -1, -1, 1, -1, 1, 1, -1, 0}, Relationship.GEQ, 0.0)); constraints.add(new LinearConstraint(new double[] {1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1}, Relationship.GEQ, 0.0)); constraints.add(new LinearConstraint(new double[] {1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1}, Relationship.LEQ, 0.0)); constraints.add(new LinearConstraint(new double[] {0, 1, 0, -1, 0, -1, 0, 1, 0, -1, 0, 1, 0, 1, 0, -1, 0, -1, 0, 1, 0, 1, 0, -1, 0, 1, 0, -1, 0, -1, 0, 1, 0}, Relationship.GEQ, 0.0)); constraints.add(new LinearConstraint(new double[] {0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -0.628803}, Relationship.GEQ, 0.0)); constraints.add(new LinearConstraint(new double[] {0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -0.676993}, Relationship.LEQ, 0.0)); constraints.add(new LinearConstraint(new double[] {0, 0, 1, -1, 0, 0, -1, 1, 0, 0, -1, 1, 0, 0, 1, -1, 0, 0, -1, 1, 0, 0, 1, -1, 0, 0, 1, -1, 0, 0, -1, 1, 0}, Relationship.GEQ, 0.0)); constraints.add(new LinearConstraint(new double[] {0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -0.136677}, Relationship.GEQ, 0.0)); constraints.add(new LinearConstraint(new double[] {0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -0.444434}, Relationship.LEQ, 0.0)); constraints.add(new LinearConstraint(new double[] {0, 0, 0, 1, 0, 0, 0, -1, 0, 0, 0, -1, 0, 0, 0, 1, 0, 0, 0, -1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, -1, 0}, Relationship.GEQ, 0.0)); constraints.add(new LinearConstraint(new double[] {0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -0.254028}, Relationship.GEQ, 0.0)); constraints.add(new LinearConstraint(new double[] {0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -0.302218}, Relationship.LEQ, 0.0)); constraints.add(new LinearConstraint(new double[] {0, 0, 0, 0, 1, -1, -1, 1, 0, 0, 0, 0, -1, 1, 1, -1, 0, 0, 0, 0, -1, 1, 1, -1, 0, 0, 0, 0, 1, -1, -1, 1, 0}, Relationship.GEQ, 0.0)); constraints.add(new LinearConstraint(new double[] {0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -0.653981}, Relationship.GEQ, 0.0)); constraints.add(new LinearConstraint(new double[] {0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -0.690437}, Relationship.LEQ, 0.0)); constraints.add(new LinearConstraint(new double[] {0, 0, 0, 0, 0, 1, 0, -1, 0, 0, 0, 0, 0, -1, 0, 1, 0, 0, 0, 0, 0, -1, 0, 1, 0, 0, 0, 0, 0, 1, 0, -1, 0}, Relationship.GEQ, 0.0)); constraints.add(new LinearConstraint(new double[] {0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -0.423786}, Relationship.GEQ, 0.0)); constraints.add(new LinearConstraint(new double[] {0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -0.486717}, Relationship.LEQ, 0.0)); constraints.add(new LinearConstraint(new double[] {0, 0, 0, 0, 0, 0, 1, -1, 0, 0, 0, 0, 0, 0, -1, 1, 0, 0, 0, 0, 0, 0, -1, 1, 0, 0, 0, 0, 0, 0, 1, -1, 0}, Relationship.GEQ, 0.0)); constraints.add(new LinearConstraint(new double[] {0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -0.049232}, Relationship.GEQ, 0.0)); constraints.add(new LinearConstraint(new double[] {0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -0.304747}, Relationship.LEQ, 0.0)); constraints.add(new LinearConstraint(new double[] {0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, -1, 0, 0, 0, 0, 0, 0, 0, -1, 0, 0, 0, 0, 0, 0, 0, 1, 0}, Relationship.GEQ, 0.0)); constraints.add(new LinearConstraint(new double[] {0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -0.129826}, Relationship.GEQ, 0.0)); constraints.add(new LinearConstraint(new double[] {0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -0.205625}, Relationship.LEQ, 0.0)); constraints.add(new LinearConstraint(new double[] {0, 0, 0, 0, 0, 0, 0, 0, 1, -1, -1, 1, -1, 1, 1, -1, 0, 0, 0, 0, 0, 0, 0, 0, -1, 1, 1, -1, 1, -1, -1, 1, 0}, Relationship.GEQ, 0.0)); constraints.add(new LinearConstraint(new double[] {0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -0.621944}, Relationship.GEQ, 0.0)); constraints.add(new LinearConstraint(new double[] {0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -0.764385}, Relationship.LEQ, 0.0)); constraints.add(new LinearConstraint(new double[] {0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, -1, 0, -1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, 0, 1, 0, 1, 0, -1, 0}, Relationship.GEQ, 0.0)); constraints.add(new LinearConstraint(new double[] {0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -0.432572}, Relationship.GEQ, 0.0)); constraints.add(new LinearConstraint(new double[] {0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -0.480762}, Relationship.LEQ, 0.0)); constraints.add(new LinearConstraint(new double[] {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, -1, 0, 0, -1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, 1, 0, 0, 1, -1, 0}, Relationship.GEQ, 0.0)); constraints.add(new LinearConstraint(new double[] {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -0.055983}, Relationship.GEQ, 0.0)); constraints.add(new LinearConstraint(new double[] {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -0.11378}, Relationship.LEQ, 0.0)); constraints.add(new LinearConstraint(new double[] {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, 0, 0, 0, 1, 0}, Relationship.GEQ, 0.0)); constraints.add(new LinearConstraint(new double[] {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -0.009607}, Relationship.GEQ, 0.0)); constraints.add(new LinearConstraint(new double[] {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -0.057797}, Relationship.LEQ, 0.0)); constraints.add(new LinearConstraint(new double[] {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, -1, -1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, 1, 1, -1, 0}, Relationship.GEQ, 0.0)); constraints.add(new LinearConstraint(new double[] {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -0.407308}, Relationship.GEQ, 0.0)); constraints.add(new LinearConstraint(new double[] {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -0.452749}, Relationship.LEQ, 0.0)); constraints.add(new LinearConstraint(new double[] {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, 0, 1, 0}, Relationship.GEQ, 0.0)); constraints.add(new LinearConstraint(new double[] {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -0.269677}, Relationship.GEQ, 0.0)); constraints.add(new LinearConstraint(new double[] {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -0.321806}, Relationship.LEQ, 0.0)); constraints.add(new LinearConstraint(new double[] {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, 1, 0}, Relationship.GEQ, 0.0)); constraints.add(new LinearConstraint(new double[] {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -0.049232}, Relationship.GEQ, 0.0)); constraints.add(new LinearConstraint(new double[] {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -0.06902}, Relationship.LEQ, 0.0)); constraints.add(new LinearConstraint(new double[] {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, 0}, Relationship.GEQ, 0.0)); constraints.add(new LinearConstraint(new double[] {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -0}, Relationship.GEQ, 0.0)); constraints.add(new LinearConstraint(new double[] {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -0.028754}, Relationship.LEQ, 0.0)); constraints.add(new LinearConstraint(new double[] {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, -1, -1, 1, -1, 1, 1, -1, -1, 1, 1, -1, 1, -1, -1, 1, 0}, Relationship.GEQ, 0.0)); constraints.add(new LinearConstraint(new double[] {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -0.484254}, Relationship.GEQ, 0.0)); constraints.add(new LinearConstraint(new double[] {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -0.524607}, Relationship.LEQ, 0.0)); constraints.add(new LinearConstraint(new double[] {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, -1, 0, -1, 0, 1, 0, -1, 0, 1, 0, 1, 0, -1, 0}, Relationship.GEQ, 0.0)); constraints.add(new LinearConstraint(new double[] {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -0.385492}, Relationship.GEQ, 0.0)); constraints.add(new LinearConstraint(new double[] {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -0.430134}, Relationship.LEQ, 0.0)); constraints.add(new LinearConstraint(new double[] {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, -1, 0, 0, -1, 1, 0, 0, -1, 1, 0, 0, 1, -1, 0}, Relationship.GEQ, 0.0)); constraints.add(new LinearConstraint(new double[] {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -0.34983}, Relationship.GEQ, 0.0)); constraints.add(new LinearConstraint(new double[] {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -0.375781}, Relationship.LEQ, 0.0)); constraints.add(new LinearConstraint(new double[] {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, -1, 0, 0, 0, -1, 0, 0, 0, 1, 0}, Relationship.GEQ, 0.0)); constraints.add(new LinearConstraint(new double[] {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -0.254028}, Relationship.GEQ, 0.0)); constraints.add(new LinearConstraint(new double[] {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -0.281308}, Relationship.LEQ, 0.0)); constraints.add(new LinearConstraint(new double[] {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, -1, -1, 1, 0, 0, 0, 0, -1, 1, 1, -1, 0}, Relationship.GEQ, 0.0)); constraints.add(new LinearConstraint(new double[] {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -0.304995}, Relationship.GEQ, 0.0)); constraints.add(new LinearConstraint(new double[] {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -0.345347}, Relationship.LEQ, 0.0)); constraints.add(new LinearConstraint(new double[] {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, -1, 0, 0, 0, 0, 0, -1, 0, 1, 0}, Relationship.GEQ, 0.0)); constraints.add(new LinearConstraint(new double[] {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -0.288899}, Relationship.GEQ, 0.0)); constraints.add(new LinearConstraint(new double[] {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -0.332212}, Relationship.LEQ, 0.0)); constraints.add(new LinearConstraint(new double[] {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, -1, 0, 0, 0, 0, 0, 0, -1, 1, 0}, Relationship.GEQ, 0.0)); constraints.add(new LinearConstraint(new double[] {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, -0.14351}, Relationship.GEQ, 0.0)); constraints.add(new LinearConstraint(new double[] {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, -0.17057}, Relationship.LEQ, 0.0)); constraints.add(new LinearConstraint(new double[] {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, -1, 0}, Relationship.GEQ, 0.0)); constraints.add(new LinearConstraint(new double[] {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, -0.129826}, Relationship.GEQ, 0.0)); constraints.add(new LinearConstraint(new double[] {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, -0.157435}, Relationship.LEQ, 0.0)); constraints.add(new LinearConstraint(new double[] {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, -1, -1, 1, -1, 1, 1, -1, 0}, Relationship.GEQ, 0.0)); constraints.add(new LinearConstraint(new double[] {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, -0}, Relationship.GEQ, 0.0)); constraints.add(new LinearConstraint(new double[] {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, -1}, Relationship.LEQ, 0.0)); constraints.add(new LinearConstraint(new double[] {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, -1, 0, -1, 0, 1, 0}, Relationship.GEQ, 0.0)); constraints.add(new LinearConstraint(new double[] {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, -0.141071}, Relationship.GEQ, 0.0)); constraints.add(new LinearConstraint(new double[] {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, -0.232574}, Relationship.LEQ, 0.0)); constraints.add(new LinearConstraint(new double[] {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, -1, 0, 0, -1, 1, 0}, Relationship.GEQ, 0.0)); constraints.add(new LinearConstraint(new double[] {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, -0}, Relationship.GEQ, 0.0)); constraints.add(new LinearConstraint(new double[] {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, -1}, Relationship.LEQ, 0.0)); constraints.add(new LinearConstraint(new double[] {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, -1, 0}, Relationship.GEQ, 0.0)); constraints.add(new LinearConstraint(new double[] {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, -0.009607}, Relationship.GEQ, 0.0)); constraints.add(new LinearConstraint(new double[] {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, -0.057797}, Relationship.LEQ, 0.0)); constraints.add(new LinearConstraint(new double[] {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, -1, -1, 1, 0}, Relationship.GEQ, 0.0)); constraints.add(new LinearConstraint(new double[] {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, -0}, Relationship.GEQ, 0.0)); constraints.add(new LinearConstraint(new double[] {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, -1}, Relationship.LEQ, 0.0)); constraints.add(new LinearConstraint(new double[] {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, -1, 0}, Relationship.GEQ, 0.0)); constraints.add(new LinearConstraint(new double[] {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, -0.091644}, Relationship.GEQ, 0.0)); constraints.add(new LinearConstraint(new double[] {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, -0.203531}, Relationship.LEQ, 0.0)); constraints.add(new LinearConstraint(new double[] {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, -1, 0}, Relationship.GEQ, 0.0)); constraints.add(new LinearConstraint(new double[] {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, -0}, Relationship.GEQ, 0.0)); constraints.add(new LinearConstraint(new double[] {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, -1}, Relationship.LEQ, 0.0)); constraints.add(new LinearConstraint(new double[] {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0}, Relationship.GEQ, 0.0)); constraints.add(new LinearConstraint(new double[] {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0}, Relationship.GEQ, 0.0)); constraints.add(new LinearConstraint(new double[] {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, -0.028754}, Relationship.LEQ, 0.0)); constraints.add(new LinearConstraint(new double[] {0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, Relationship.EQ, 1.0)); double[] objFunctionCoeff = new double[33]; objFunctionCoeff[3] = 1; LinearObjectiveFunction f = new LinearObjectiveFunction(objFunctionCoeff, 0); SimplexSolver solver = new SimplexSolver(1e-4, 10, 1e-6); PointValuePair solution = solver.optimize(new MaxIter(1000), f, new LinearConstraintSet(constraints), GoalType.MINIMIZE, new NonNegativeConstraint(true)); Assert.assertEquals(0.3752298, solution.getValue(), 1e-4); } @Test public void testSimplexSolver() { LinearObjectiveFunction f = new LinearObjectiveFunction(new double[] { 15, 10 }, 7); Collection constraints = new ArrayList(); constraints.add(new LinearConstraint(new double[] { 1, 0 }, Relationship.LEQ, 2)); constraints.add(new LinearConstraint(new double[] { 0, 1 }, Relationship.LEQ, 3)); constraints.add(new LinearConstraint(new double[] { 1, 1 }, Relationship.EQ, 4)); SimplexSolver solver = new SimplexSolver(); PointValuePair solution = solver.optimize(DEFAULT_MAX_ITER, f, new LinearConstraintSet(constraints), GoalType.MAXIMIZE, new NonNegativeConstraint(true)); Assert.assertEquals(2.0, solution.getPoint()[0], 0.0); Assert.assertEquals(2.0, solution.getPoint()[1], 0.0); Assert.assertEquals(57.0, solution.getValue(), 0.0); } @Test public void testSingleVariableAndConstraint() { LinearObjectiveFunction f = new LinearObjectiveFunction(new double[] { 3 }, 0); Collection constraints = new ArrayList(); constraints.add(new LinearConstraint(new double[] { 1 }, Relationship.LEQ, 10)); SimplexSolver solver = new SimplexSolver(); PointValuePair solution = solver.optimize(DEFAULT_MAX_ITER, f, new LinearConstraintSet(constraints), GoalType.MAXIMIZE, new NonNegativeConstraint(false)); Assert.assertEquals(10.0, solution.getPoint()[0], 0.0); Assert.assertEquals(30.0, solution.getValue(), 0.0); } /** * With no artificial variables needed (no equals and no greater than * constraints) we can go straight to Phase 2. */ @Test public void testModelWithNoArtificialVars() { LinearObjectiveFunction f = new LinearObjectiveFunction(new double[] { 15, 10 }, 0); Collection constraints = new ArrayList(); constraints.add(new LinearConstraint(new double[] { 1, 0 }, Relationship.LEQ, 2)); constraints.add(new LinearConstraint(new double[] { 0, 1 }, Relationship.LEQ, 3)); constraints.add(new LinearConstraint(new double[] { 1, 1 }, Relationship.LEQ, 4)); SimplexSolver solver = new SimplexSolver(); PointValuePair solution = solver.optimize(DEFAULT_MAX_ITER, f, new LinearConstraintSet(constraints), GoalType.MAXIMIZE, new NonNegativeConstraint(false)); Assert.assertEquals(2.0, solution.getPoint()[0], 0.0); Assert.assertEquals(2.0, solution.getPoint()[1], 0.0); Assert.assertEquals(50.0, solution.getValue(), 0.0); } @Test public void testMinimization() { LinearObjectiveFunction f = new LinearObjectiveFunction(new double[] { -2, 1 }, -5); Collection constraints = new ArrayList(); constraints.add(new LinearConstraint(new double[] { 1, 2 }, Relationship.LEQ, 6)); constraints.add(new LinearConstraint(new double[] { 3, 2 }, Relationship.LEQ, 12)); constraints.add(new LinearConstraint(new double[] { 0, 1 }, Relationship.GEQ, 0)); SimplexSolver solver = new SimplexSolver(); PointValuePair solution = solver.optimize(DEFAULT_MAX_ITER, f, new LinearConstraintSet(constraints), GoalType.MINIMIZE, new NonNegativeConstraint(false)); Assert.assertEquals(4.0, solution.getPoint()[0], 0.0); Assert.assertEquals(0.0, solution.getPoint()[1], 0.0); Assert.assertEquals(-13.0, solution.getValue(), 0.0); } @Test public void testSolutionWithNegativeDecisionVariable() { LinearObjectiveFunction f = new LinearObjectiveFunction(new double[] { -2, 1 }, 0); Collection constraints = new ArrayList(); constraints.add(new LinearConstraint(new double[] { 1, 1 }, Relationship.GEQ, 6)); constraints.add(new LinearConstraint(new double[] { 1, 2 }, Relationship.LEQ, 14)); SimplexSolver solver = new SimplexSolver(); PointValuePair solution = solver.optimize(DEFAULT_MAX_ITER, f, new LinearConstraintSet(constraints), GoalType.MAXIMIZE, new NonNegativeConstraint(false)); Assert.assertEquals(-2.0, solution.getPoint()[0], 0.0); Assert.assertEquals(8.0, solution.getPoint()[1], 0.0); Assert.assertEquals(12.0, solution.getValue(), 0.0); } @Test(expected = NoFeasibleSolutionException.class) public void testInfeasibleSolution() { LinearObjectiveFunction f = new LinearObjectiveFunction(new double[] { 15 }, 0); Collection constraints = new ArrayList(); constraints.add(new LinearConstraint(new double[] { 1 }, Relationship.LEQ, 1)); constraints.add(new LinearConstraint(new double[] { 1 }, Relationship.GEQ, 3)); SimplexSolver solver = new SimplexSolver(); solver.optimize(DEFAULT_MAX_ITER, f, new LinearConstraintSet(constraints), GoalType.MAXIMIZE, new NonNegativeConstraint(false)); } @Test(expected = UnboundedSolutionException.class) public void testUnboundedSolution() { LinearObjectiveFunction f = new LinearObjectiveFunction(new double[] { 15, 10 }, 0); Collection constraints = new ArrayList(); constraints.add(new LinearConstraint(new double[] { 1, 0 }, Relationship.EQ, 2)); SimplexSolver solver = new SimplexSolver(); solver.optimize(DEFAULT_MAX_ITER, f, new LinearConstraintSet(constraints), GoalType.MAXIMIZE, new NonNegativeConstraint(false)); } @Test public void testRestrictVariablesToNonNegative() { LinearObjectiveFunction f = new LinearObjectiveFunction(new double[] { 409, 523, 70, 204, 339 }, 0); Collection constraints = new ArrayList(); constraints.add(new LinearConstraint(new double[] { 43, 56, 345, 56, 5 }, Relationship.LEQ, 4567456)); constraints.add(new LinearConstraint(new double[] { 12, 45, 7, 56, 23 }, Relationship.LEQ, 56454)); constraints.add(new LinearConstraint(new double[] { 8, 768, 0, 34, 7456 }, Relationship.LEQ, 1923421)); constraints.add(new LinearConstraint(new double[] { 12342, 2342, 34, 678, 2342 }, Relationship.GEQ, 4356)); constraints.add(new LinearConstraint(new double[] { 45, 678, 76, 52, 23 }, Relationship.EQ, 456356)); SimplexSolver solver = new SimplexSolver(); PointValuePair solution = solver.optimize(DEFAULT_MAX_ITER, f, new LinearConstraintSet(constraints), GoalType.MAXIMIZE, new NonNegativeConstraint(true)); Assert.assertEquals(2902.92783505155, solution.getPoint()[0], .0000001); Assert.assertEquals(480.419243986254, solution.getPoint()[1], .0000001); Assert.assertEquals(0.0, solution.getPoint()[2], .0000001); Assert.assertEquals(0.0, solution.getPoint()[3], .0000001); Assert.assertEquals(0.0, solution.getPoint()[4], .0000001); Assert.assertEquals(1438556.7491409, solution.getValue(), .0000001); } @Test public void testEpsilon() { LinearObjectiveFunction f = new LinearObjectiveFunction(new double[] { 10, 5, 1 }, 0); Collection constraints = new ArrayList(); constraints.add(new LinearConstraint(new double[] { 9, 8, 0 }, Relationship.EQ, 17)); constraints.add(new LinearConstraint(new double[] { 0, 7, 8 }, Relationship.LEQ, 7)); constraints.add(new LinearConstraint(new double[] { 10, 0, 2 }, Relationship.LEQ, 10)); SimplexSolver solver = new SimplexSolver(); PointValuePair solution = solver.optimize(DEFAULT_MAX_ITER, f, new LinearConstraintSet(constraints), GoalType.MAXIMIZE, new NonNegativeConstraint(false)); Assert.assertEquals(1.0, solution.getPoint()[0], 0.0); Assert.assertEquals(1.0, solution.getPoint()[1], 0.0); Assert.assertEquals(0.0, solution.getPoint()[2], 0.0); Assert.assertEquals(15.0, solution.getValue(), 0.0); } @Test public void testTrivialModel() { LinearObjectiveFunction f = new LinearObjectiveFunction(new double[] { 1, 1 }, 0); Collection constraints = new ArrayList(); constraints.add(new LinearConstraint(new double[] { 1, 1 }, Relationship.EQ, 0)); SimplexSolver solver = new SimplexSolver(); PointValuePair solution = solver.optimize(DEFAULT_MAX_ITER, f, new LinearConstraintSet(constraints), GoalType.MAXIMIZE, new NonNegativeConstraint(true)); Assert.assertEquals(0, solution.getValue(), .0000001); } @Test public void testLargeModel() { double[] objective = new double[] { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 12, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 12, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 12, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 12, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 12, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 12, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}; LinearObjectiveFunction f = new LinearObjectiveFunction(objective, 0); Collection constraints = new ArrayList(); constraints.add(equationFromString(objective.length, "x0 + x1 + x2 + x3 - x12 = 0")); constraints.add(equationFromString(objective.length, "x4 + x5 + x6 + x7 + x8 + x9 + x10 + x11 - x13 = 0")); constraints.add(equationFromString(objective.length, "x4 + x5 + x6 + x7 + x8 + x9 + x10 + x11 >= 49")); constraints.add(equationFromString(objective.length, "x0 + x1 + x2 + x3 >= 42")); constraints.add(equationFromString(objective.length, "x14 + x15 + x16 + x17 - x26 = 0")); constraints.add(equationFromString(objective.length, "x18 + x19 + x20 + x21 + x22 + x23 + x24 + x25 - x27 = 0")); constraints.add(equationFromString(objective.length, "x14 + x15 + x16 + x17 - x12 = 0")); constraints.add(equationFromString(objective.length, "x18 + x19 + x20 + x21 + x22 + x23 + x24 + x25 - x13 = 0")); constraints.add(equationFromString(objective.length, "x28 + x29 + x30 + x31 - x40 = 0")); constraints.add(equationFromString(objective.length, "x32 + x33 + x34 + x35 + x36 + x37 + x38 + x39 - x41 = 0")); constraints.add(equationFromString(objective.length, "x32 + x33 + x34 + x35 + x36 + x37 + x38 + x39 >= 49")); constraints.add(equationFromString(objective.length, "x28 + x29 + x30 + x31 >= 42")); constraints.add(equationFromString(objective.length, "x42 + x43 + x44 + x45 - x54 = 0")); constraints.add(equationFromString(objective.length, "x46 + x47 + x48 + x49 + x50 + x51 + x52 + x53 - x55 = 0")); constraints.add(equationFromString(objective.length, "x42 + x43 + x44 + x45 - x40 = 0")); constraints.add(equationFromString(objective.length, "x46 + x47 + x48 + x49 + x50 + x51 + x52 + x53 - x41 = 0")); constraints.add(equationFromString(objective.length, "x56 + x57 + x58 + x59 - x68 = 0")); constraints.add(equationFromString(objective.length, "x60 + x61 + x62 + x63 + x64 + x65 + x66 + x67 - x69 = 0")); constraints.add(equationFromString(objective.length, "x60 + x61 + x62 + x63 + x64 + x65 + x66 + x67 >= 51")); constraints.add(equationFromString(objective.length, "x56 + x57 + x58 + x59 >= 44")); constraints.add(equationFromString(objective.length, "x70 + x71 + x72 + x73 - x82 = 0")); constraints.add(equationFromString(objective.length, "x74 + x75 + x76 + x77 + x78 + x79 + x80 + x81 - x83 = 0")); constraints.add(equationFromString(objective.length, "x70 + x71 + x72 + x73 - x68 = 0")); constraints.add(equationFromString(objective.length, "x74 + x75 + x76 + x77 + x78 + x79 + x80 + x81 - x69 = 0")); constraints.add(equationFromString(objective.length, "x84 + x85 + x86 + x87 - x96 = 0")); constraints.add(equationFromString(objective.length, "x88 + x89 + x90 + x91 + x92 + x93 + x94 + x95 - x97 = 0")); constraints.add(equationFromString(objective.length, "x88 + x89 + x90 + x91 + x92 + x93 + x94 + x95 >= 51")); constraints.add(equationFromString(objective.length, "x84 + x85 + x86 + x87 >= 44")); constraints.add(equationFromString(objective.length, "x98 + x99 + x100 + x101 - x110 = 0")); constraints.add(equationFromString(objective.length, "x102 + x103 + x104 + x105 + x106 + x107 + x108 + x109 - x111 = 0")); constraints.add(equationFromString(objective.length, "x98 + x99 + x100 + x101 - x96 = 0")); constraints.add(equationFromString(objective.length, "x102 + x103 + x104 + x105 + x106 + x107 + x108 + x109 - x97 = 0")); constraints.add(equationFromString(objective.length, "x112 + x113 + x114 + x115 - x124 = 0")); constraints.add(equationFromString(objective.length, "x116 + x117 + x118 + x119 + x120 + x121 + x122 + x123 - x125 = 0")); constraints.add(equationFromString(objective.length, "x116 + x117 + x118 + x119 + x120 + x121 + x122 + x123 >= 49")); constraints.add(equationFromString(objective.length, "x112 + x113 + x114 + x115 >= 42")); constraints.add(equationFromString(objective.length, "x126 + x127 + x128 + x129 - x138 = 0")); constraints.add(equationFromString(objective.length, "x130 + x131 + x132 + x133 + x134 + x135 + x136 + x137 - x139 = 0")); constraints.add(equationFromString(objective.length, "x126 + x127 + x128 + x129 - x124 = 0")); constraints.add(equationFromString(objective.length, "x130 + x131 + x132 + x133 + x134 + x135 + x136 + x137 - x125 = 0")); constraints.add(equationFromString(objective.length, "x140 + x141 + x142 + x143 - x152 = 0")); constraints.add(equationFromString(objective.length, "x144 + x145 + x146 + x147 + x148 + x149 + x150 + x151 - x153 = 0")); constraints.add(equationFromString(objective.length, "x144 + x145 + x146 + x147 + x148 + x149 + x150 + x151 >= 59")); constraints.add(equationFromString(objective.length, "x140 + x141 + x142 + x143 >= 42")); constraints.add(equationFromString(objective.length, "x154 + x155 + x156 + x157 - x166 = 0")); constraints.add(equationFromString(objective.length, "x158 + x159 + x160 + x161 + x162 + x163 + x164 + x165 - x167 = 0")); constraints.add(equationFromString(objective.length, "x154 + x155 + x156 + x157 - x152 = 0")); constraints.add(equationFromString(objective.length, "x158 + x159 + x160 + x161 + x162 + x163 + x164 + x165 - x153 = 0")); constraints.add(equationFromString(objective.length, "x83 + x82 - x168 = 0")); constraints.add(equationFromString(objective.length, "x111 + x110 - x169 = 0")); constraints.add(equationFromString(objective.length, "x170 - x182 = 0")); constraints.add(equationFromString(objective.length, "x171 - x183 = 0")); constraints.add(equationFromString(objective.length, "x172 - x184 = 0")); constraints.add(equationFromString(objective.length, "x173 - x185 = 0")); constraints.add(equationFromString(objective.length, "x174 - x186 = 0")); constraints.add(equationFromString(objective.length, "x175 + x176 - x187 = 0")); constraints.add(equationFromString(objective.length, "x177 - x188 = 0")); constraints.add(equationFromString(objective.length, "x178 - x189 = 0")); constraints.add(equationFromString(objective.length, "x179 - x190 = 0")); constraints.add(equationFromString(objective.length, "x180 - x191 = 0")); constraints.add(equationFromString(objective.length, "x181 - x192 = 0")); constraints.add(equationFromString(objective.length, "x170 - x26 = 0")); constraints.add(equationFromString(objective.length, "x171 - x27 = 0")); constraints.add(equationFromString(objective.length, "x172 - x54 = 0")); constraints.add(equationFromString(objective.length, "x173 - x55 = 0")); constraints.add(equationFromString(objective.length, "x174 - x168 = 0")); constraints.add(equationFromString(objective.length, "x177 - x169 = 0")); constraints.add(equationFromString(objective.length, "x178 - x138 = 0")); constraints.add(equationFromString(objective.length, "x179 - x139 = 0")); constraints.add(equationFromString(objective.length, "x180 - x166 = 0")); constraints.add(equationFromString(objective.length, "x181 - x167 = 0")); constraints.add(equationFromString(objective.length, "x193 - x205 = 0")); constraints.add(equationFromString(objective.length, "x194 - x206 = 0")); constraints.add(equationFromString(objective.length, "x195 - x207 = 0")); constraints.add(equationFromString(objective.length, "x196 - x208 = 0")); constraints.add(equationFromString(objective.length, "x197 - x209 = 0")); constraints.add(equationFromString(objective.length, "x198 + x199 - x210 = 0")); constraints.add(equationFromString(objective.length, "x200 - x211 = 0")); constraints.add(equationFromString(objective.length, "x201 - x212 = 0")); constraints.add(equationFromString(objective.length, "x202 - x213 = 0")); constraints.add(equationFromString(objective.length, "x203 - x214 = 0")); constraints.add(equationFromString(objective.length, "x204 - x215 = 0")); constraints.add(equationFromString(objective.length, "x193 - x182 = 0")); constraints.add(equationFromString(objective.length, "x194 - x183 = 0")); constraints.add(equationFromString(objective.length, "x195 - x184 = 0")); constraints.add(equationFromString(objective.length, "x196 - x185 = 0")); constraints.add(equationFromString(objective.length, "x197 - x186 = 0")); constraints.add(equationFromString(objective.length, "x198 + x199 - x187 = 0")); constraints.add(equationFromString(objective.length, "x200 - x188 = 0")); constraints.add(equationFromString(objective.length, "x201 - x189 = 0")); constraints.add(equationFromString(objective.length, "x202 - x190 = 0")); constraints.add(equationFromString(objective.length, "x203 - x191 = 0")); constraints.add(equationFromString(objective.length, "x204 - x192 = 0")); SimplexSolver solver = new SimplexSolver(); PointValuePair solution = solver.optimize(DEFAULT_MAX_ITER, f, new LinearConstraintSet(constraints), GoalType.MINIMIZE, new NonNegativeConstraint(true)); Assert.assertEquals(7518.0, solution.getValue(), .0000001); } /** * Converts a test string to a {@link LinearConstraint}. * Ex: x0 + x1 + x2 + x3 - x12 = 0 */ private LinearConstraint equationFromString(int numCoefficients, String s) { Relationship relationship; if (s.contains(">=")) { relationship = Relationship.GEQ; } else if (s.contains("<=")) { relationship = Relationship.LEQ; } else if (s.contains("=")) { relationship = Relationship.EQ; } else { throw new IllegalArgumentException(); } String[] equationParts = s.split("[>|<]?="); double rhs = Double.parseDouble(equationParts[1].trim()); double[] lhs = new double[numCoefficients]; String left = equationParts[0].replaceAll(" ?x", ""); String[] coefficients = left.split(" "); for (String coefficient : coefficients) { double value = coefficient.charAt(0) == '-' ? -1 : 1; int index = Integer.parseInt(coefficient.replaceFirst("[+|-]", "").trim()); lhs[index] = value; } return new LinearConstraint(lhs, relationship, rhs); } private static boolean validSolution(PointValuePair solution, List constraints, double epsilon) { double[] vals = solution.getPoint(); for (LinearConstraint c : constraints) { double[] coeffs = c.getCoefficients().toArray(); double result = 0.0d; for (int i = 0; i < vals.length; i++) { result += vals[i] * coeffs[i]; } switch (c.getRelationship()) { case EQ: if (!Precision.equals(result, c.getValue(), epsilon)) { return false; } break; case GEQ: if (Precision.compareTo(result, c.getValue(), epsilon) < 0) { return false; } break; case LEQ: if (Precision.compareTo(result, c.getValue(), epsilon) > 0) { return false; } break; } } return true; } } ././@LongLink100644 0 0 203 12126630646 10254 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/optim/nonlinear/vector/MultiStartMultivariateVectorOptimizerTest.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/optim/nonlinear/vector/MultiStartMultiv100644 1750 1750 27335 12126627671 32466 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.optim.nonlinear.vector; import org.apache.commons.math3.analysis.MultivariateMatrixFunction; import org.apache.commons.math3.analysis.MultivariateVectorFunction; import org.apache.commons.math3.exception.TooManyEvaluationsException; import org.apache.commons.math3.linear.BlockRealMatrix; import org.apache.commons.math3.linear.RealMatrix; import org.apache.commons.math3.optim.InitialGuess; import org.apache.commons.math3.optim.MaxEval; import org.apache.commons.math3.optim.OptimizationData; import org.apache.commons.math3.optim.PointVectorValuePair; import org.apache.commons.math3.optim.SimpleBounds; import org.apache.commons.math3.optim.SimpleVectorValueChecker; import org.apache.commons.math3.optim.nonlinear.vector.jacobian.GaussNewtonOptimizer; import org.apache.commons.math3.random.GaussianRandomGenerator; import org.apache.commons.math3.random.JDKRandomGenerator; import org.apache.commons.math3.random.RandomVectorGenerator; import org.apache.commons.math3.random.UncorrelatedRandomVectorGenerator; import org.junit.Assert; import org.junit.Test; /** *

              Some of the unit tests are re-implementations of the MINPACK file17 and file22 test files. * The redistribution policy for MINPACK is available here, for * convenience, it is reproduced below.

              * * * * *
              * Minpack Copyright Notice (1999) University of Chicago. * All rights reserved *
              * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: *
                *
              1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer.
              2. *
              3. Redistributions in binary form must reproduce the above * copyright notice, this list of conditions and the following * disclaimer in the documentation and/or other materials provided * with the distribution.
              4. *
              5. The end-user documentation included with the redistribution, if any, * must include the following acknowledgment: * This product includes software developed by the University of * Chicago, as Operator of Argonne National Laboratory. * Alternately, this acknowledgment may appear in the software itself, * if and wherever such third-party acknowledgments normally appear.
              6. *
              7. WARRANTY DISCLAIMER. THE SOFTWARE IS SUPPLIED "AS IS" * WITHOUT WARRANTY OF ANY KIND. THE COPYRIGHT HOLDER, THE * UNITED STATES, THE UNITED STATES DEPARTMENT OF ENERGY, AND * THEIR EMPLOYEES: (1) DISCLAIM ANY WARRANTIES, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO ANY IMPLIED WARRANTIES * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE * OR NON-INFRINGEMENT, (2) DO NOT ASSUME ANY LEGAL LIABILITY * OR RESPONSIBILITY FOR THE ACCURACY, COMPLETENESS, OR * USEFULNESS OF THE SOFTWARE, (3) DO NOT REPRESENT THAT USE OF * THE SOFTWARE WOULD NOT INFRINGE PRIVATELY OWNED RIGHTS, (4) * DO NOT WARRANT THAT THE SOFTWARE WILL FUNCTION * UNINTERRUPTED, THAT IT IS ERROR-FREE OR THAT ANY ERRORS WILL * BE CORRECTED.
              8. *
              9. LIMITATION OF LIABILITY. IN NO EVENT WILL THE COPYRIGHT * HOLDER, THE UNITED STATES, THE UNITED STATES DEPARTMENT OF * ENERGY, OR THEIR EMPLOYEES: BE LIABLE FOR ANY INDIRECT, * INCIDENTAL, CONSEQUENTIAL, SPECIAL OR PUNITIVE DAMAGES OF * ANY KIND OR NATURE, INCLUDING BUT NOT LIMITED TO LOSS OF * PROFITS OR LOSS OF DATA, FOR ANY REASON WHATSOEVER, WHETHER * SUCH LIABILITY IS ASSERTED ON THE BASIS OF CONTRACT, TORT * (INCLUDING NEGLIGENCE OR STRICT LIABILITY), OR OTHERWISE, * EVEN IF ANY OF SAID PARTIES HAS BEEN WARNED OF THE * POSSIBILITY OF SUCH LOSS OR DAMAGES.
              10. *
                * * @author Argonne National Laboratory. MINPACK project. March 1980 (original fortran minpack tests) * @author Burton S. Garbow (original fortran minpack tests) * @author Kenneth E. Hillstrom (original fortran minpack tests) * @author Jorge J. More (original fortran minpack tests) * @author Luc Maisonobe (non-minpack tests and minpack tests Java translation) */ public class MultiStartMultivariateVectorOptimizerTest { @Test(expected=NullPointerException.class) public void testGetOptimaBeforeOptimize() { JacobianMultivariateVectorOptimizer underlyingOptimizer = new GaussNewtonOptimizer(true, new SimpleVectorValueChecker(1e-6, 1e-6)); JDKRandomGenerator g = new JDKRandomGenerator(); g.setSeed(16069223052l); RandomVectorGenerator generator = new UncorrelatedRandomVectorGenerator(1, new GaussianRandomGenerator(g)); MultiStartMultivariateVectorOptimizer optimizer = new MultiStartMultivariateVectorOptimizer(underlyingOptimizer, 10, generator); optimizer.getOptima(); } @Test public void testTrivial() { LinearProblem problem = new LinearProblem(new double[][] { { 2 } }, new double[] { 3 }); JacobianMultivariateVectorOptimizer underlyingOptimizer = new GaussNewtonOptimizer(true, new SimpleVectorValueChecker(1e-6, 1e-6)); JDKRandomGenerator g = new JDKRandomGenerator(); g.setSeed(16069223052l); RandomVectorGenerator generator = new UncorrelatedRandomVectorGenerator(1, new GaussianRandomGenerator(g)); MultiStartMultivariateVectorOptimizer optimizer = new MultiStartMultivariateVectorOptimizer(underlyingOptimizer, 10, generator); PointVectorValuePair optimum = optimizer.optimize(new MaxEval(100), problem.getModelFunction(), problem.getModelFunctionJacobian(), problem.getTarget(), new Weight(new double[] { 1 }), new InitialGuess(new double[] { 0 })); Assert.assertEquals(1.5, optimum.getPoint()[0], 1e-10); Assert.assertEquals(3.0, optimum.getValue()[0], 1e-10); PointVectorValuePair[] optima = optimizer.getOptima(); Assert.assertEquals(10, optima.length); for (int i = 0; i < optima.length; i++) { Assert.assertEquals(1.5, optima[i].getPoint()[0], 1e-10); Assert.assertEquals(3.0, optima[i].getValue()[0], 1e-10); } Assert.assertTrue(optimizer.getEvaluations() > 20); Assert.assertTrue(optimizer.getEvaluations() < 50); Assert.assertEquals(100, optimizer.getMaxEvaluations()); } @Test public void testIssue914() { LinearProblem problem = new LinearProblem(new double[][] { { 2 } }, new double[] { 3 }); JacobianMultivariateVectorOptimizer underlyingOptimizer = new GaussNewtonOptimizer(true, new SimpleVectorValueChecker(1e-6, 1e-6)) { public PointVectorValuePair optimize(OptimizationData... optData) { // filter out simple bounds, as they are not supported // by the underlying optimizer, and we don't really care for this test OptimizationData[] filtered = optData.clone(); for (int i = 0; i < filtered.length; ++i) { if (filtered[i] instanceof SimpleBounds) { filtered[i] = null; } } return super.optimize(filtered); } }; JDKRandomGenerator g = new JDKRandomGenerator(); g.setSeed(16069223052l); RandomVectorGenerator generator = new UncorrelatedRandomVectorGenerator(1, new GaussianRandomGenerator(g)); MultiStartMultivariateVectorOptimizer optimizer = new MultiStartMultivariateVectorOptimizer(underlyingOptimizer, 10, generator); optimizer.optimize(new MaxEval(100), problem.getModelFunction(), problem.getModelFunctionJacobian(), problem.getTarget(), new Weight(new double[] { 1 }), new InitialGuess(new double[] { 0 }), new SimpleBounds(new double[] { -1.0e-10 }, new double[] { 1.0e-10 })); PointVectorValuePair[] optima = optimizer.getOptima(); // only the first start should have succeeded Assert.assertEquals(1, optima.length); } /** * Test demonstrating that the user exception is finally thrown if none * of the runs succeed. */ @Test(expected=TestException.class) public void testNoOptimum() { JacobianMultivariateVectorOptimizer underlyingOptimizer = new GaussNewtonOptimizer(true, new SimpleVectorValueChecker(1e-6, 1e-6)); JDKRandomGenerator g = new JDKRandomGenerator(); g.setSeed(12373523445l); RandomVectorGenerator generator = new UncorrelatedRandomVectorGenerator(1, new GaussianRandomGenerator(g)); MultiStartMultivariateVectorOptimizer optimizer = new MultiStartMultivariateVectorOptimizer(underlyingOptimizer, 10, generator); optimizer.optimize(new MaxEval(100), new Target(new double[] { 0 }), new Weight(new double[] { 1 }), new InitialGuess(new double[] { 0 }), new ModelFunction(new MultivariateVectorFunction() { public double[] value(double[] point) { throw new TestException(); } })); } private static class TestException extends RuntimeException { private static final long serialVersionUID = 1L;} private static class LinearProblem { private final RealMatrix factors; private final double[] target; public LinearProblem(double[][] factors, double[] target) { this.factors = new BlockRealMatrix(factors); this.target = target; } public Target getTarget() { return new Target(target); } public ModelFunction getModelFunction() { return new ModelFunction(new MultivariateVectorFunction() { public double[] value(double[] variables) { return factors.operate(variables); } }); } public ModelFunctionJacobian getModelFunctionJacobian() { return new ModelFunctionJacobian(new MultivariateMatrixFunction() { public double[][] value(double[] point) { return factors.getData(); } }); } } } ././@LongLink100644 0 0 203 12126630646 10254 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/optim/nonlinear/vector/jacobian/RandomStraightLinePointGenerator.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/optim/nonlinear/vector/jacobian/RandomS100644 1750 1750 7205 12126627671 32240 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.optim.nonlinear.vector.jacobian; import java.awt.geom.Point2D; import org.apache.commons.math3.random.RandomGenerator; import org.apache.commons.math3.random.Well44497b; import org.apache.commons.math3.distribution.RealDistribution; import org.apache.commons.math3.distribution.UniformRealDistribution; import org.apache.commons.math3.distribution.NormalDistribution; /** * Factory for generating a cloud of points that approximate a straight line. */ public class RandomStraightLinePointGenerator { /** Slope. */ private final double slope; /** Intercept. */ private final double intercept; /** RNG for the x-coordinate. */ private final RealDistribution x; /** RNG for the error on the y-coordinate. */ private final RealDistribution error; /** * The generator will create a cloud of points whose x-coordinates * will be randomly sampled between {@code xLo} and {@code xHi}, and * the corresponding y-coordinates will be computed as *
                
                     *  y = a x + b + N(0, error)
                     * 
                * where {@code N(mean, sigma)} is a Gaussian distribution with the * given mean and standard deviation. * * @param a Slope. * @param b Intercept. * @param sigma Standard deviation on the y-coordinate of the point. * @param lo Lowest value of the x-coordinate. * @param hi Highest value of the x-coordinate. * @param seed RNG seed. */ public RandomStraightLinePointGenerator(double a, double b, double sigma, double lo, double hi, long seed) { final RandomGenerator rng = new Well44497b(seed); slope = a; intercept = b; error = new NormalDistribution(rng, 0, sigma, NormalDistribution.DEFAULT_INVERSE_ABSOLUTE_ACCURACY); x = new UniformRealDistribution(rng, lo, hi, UniformRealDistribution.DEFAULT_INVERSE_ABSOLUTE_ACCURACY); } /** * Point generator. * * @param n Number of points to create. * @return the cloud of {@code n} points. */ public Point2D.Double[] generate(int n) { final Point2D.Double[] cloud = new Point2D.Double[n]; for (int i = 0; i < n; i++) { cloud[i] = create(); } return cloud; } /** * Create one point. * * @return a point. */ private Point2D.Double create() { final double abscissa = x.sample(); final double yModel = slope * abscissa + intercept; final double ordinate = yModel + error.sample(); return new Point2D.Double(abscissa, ordinate); } } ././@LongLink100644 0 0 204 12126630646 10255 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/optim/nonlinear/vector/jacobian/AbstractLeastSquaresOptimizerTest.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/optim/nonlinear/vector/jacobian/Abstrac100644 1750 1750 12555 12126627671 32300 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with this * work for additional information regarding copyright ownership. The ASF * licenses this file to You 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. */ package org.apache.commons.math3.optim.nonlinear.vector.jacobian; import java.io.IOException; import java.util.Arrays; import org.apache.commons.math3.optim.PointVectorValuePair; import org.apache.commons.math3.optim.InitialGuess; import org.apache.commons.math3.optim.MaxEval; import org.apache.commons.math3.optim.nonlinear.vector.Target; import org.apache.commons.math3.optim.nonlinear.vector.Weight; import org.apache.commons.math3.util.FastMath; import org.junit.Test; import org.junit.Assert; public class AbstractLeastSquaresOptimizerTest { public static AbstractLeastSquaresOptimizer createOptimizer() { return new AbstractLeastSquaresOptimizer(null) { @Override protected PointVectorValuePair doOptimize() { final double[] params = getStartPoint(); final double[] res = computeResiduals(computeObjectiveValue(params)); setCost(computeCost(res)); return new PointVectorValuePair(params, null); } }; } @Test public void testGetChiSquare() throws IOException { final StatisticalReferenceDataset dataset = StatisticalReferenceDatasetFactory.createKirby2(); final AbstractLeastSquaresOptimizer optimizer = createOptimizer(); final double[] a = dataset.getParameters(); final double[] y = dataset.getData()[1]; final double[] w = new double[y.length]; Arrays.fill(w, 1.0); StatisticalReferenceDataset.LeastSquaresProblem problem = dataset.getLeastSquaresProblem(); optimizer.optimize(new MaxEval(1), problem.getModelFunction(), problem.getModelFunctionJacobian(), new Target(y), new Weight(w), new InitialGuess(a)); final double expected = dataset.getResidualSumOfSquares(); final double actual = optimizer.getChiSquare(); Assert.assertEquals(dataset.getName(), expected, actual, 1E-11 * expected); } @Test public void testGetRMS() throws IOException { final StatisticalReferenceDataset dataset = StatisticalReferenceDatasetFactory.createKirby2(); final AbstractLeastSquaresOptimizer optimizer = createOptimizer(); final double[] a = dataset.getParameters(); final double[] y = dataset.getData()[1]; final double[] w = new double[y.length]; Arrays.fill(w, 1); StatisticalReferenceDataset.LeastSquaresProblem problem = dataset.getLeastSquaresProblem(); optimizer.optimize(new MaxEval(1), problem.getModelFunction(), problem.getModelFunctionJacobian(), new Target(y), new Weight(w), new InitialGuess(a)); final double expected = FastMath .sqrt(dataset.getResidualSumOfSquares() / dataset.getNumObservations()); final double actual = optimizer.getRMS(); Assert.assertEquals(dataset.getName(), expected, actual, 1E-11 * expected); } @Test public void testComputeSigma() throws IOException { final StatisticalReferenceDataset dataset = StatisticalReferenceDatasetFactory.createKirby2(); final AbstractLeastSquaresOptimizer optimizer = createOptimizer(); final double[] a = dataset.getParameters(); final double[] y = dataset.getData()[1]; final double[] w = new double[y.length]; Arrays.fill(w, 1); StatisticalReferenceDataset.LeastSquaresProblem problem = dataset.getLeastSquaresProblem(); final PointVectorValuePair optimum = optimizer.optimize(new MaxEval(1), problem.getModelFunction(), problem.getModelFunctionJacobian(), new Target(y), new Weight(w), new InitialGuess(a)); final double[] sig = optimizer.computeSigma(optimum.getPoint(), 1e-14); final int dof = y.length - a.length; final double[] expected = dataset.getParametersStandardDeviations(); for (int i = 0; i < sig.length; i++) { final double actual = FastMath.sqrt(optimizer.getChiSquare() / dof) * sig[i]; Assert.assertEquals(dataset.getName() + ", parameter #" + i, expected[i], actual, 1e-6 * expected[i]); } } } ././@LongLink100644 0 0 175 12126630646 10264 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/optim/nonlinear/vector/jacobian/RandomCirclePointGenerator.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/optim/nonlinear/vector/jacobian/RandomC100644 1750 1750 7077 12126627671 32227 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.optim.nonlinear.vector.jacobian; import org.apache.commons.math3.random.RandomGenerator; import org.apache.commons.math3.random.Well44497b; import org.apache.commons.math3.util.MathUtils; import org.apache.commons.math3.util.FastMath; import org.apache.commons.math3.distribution.RealDistribution; import org.apache.commons.math3.distribution.UniformRealDistribution; import org.apache.commons.math3.distribution.NormalDistribution; import org.apache.commons.math3.geometry.euclidean.twod.Vector2D; /** * Factory for generating a cloud of points that approximate a circle. */ public class RandomCirclePointGenerator { /** RNG for the x-coordinate of the center. */ private final RealDistribution cX; /** RNG for the y-coordinate of the center. */ private final RealDistribution cY; /** RNG for the parametric position of the point. */ private final RealDistribution tP; /** Radius of the circle. */ private final double radius; /** * @param x Abscissa of the circle center. * @param y Ordinate of the circle center. * @param radius Radius of the circle. * @param xSigma Error on the x-coordinate of the circumference points. * @param ySigma Error on the y-coordinate of the circumference points. * @param seed RNG seed. */ public RandomCirclePointGenerator(double x, double y, double radius, double xSigma, double ySigma, long seed) { final RandomGenerator rng = new Well44497b(seed); this.radius = radius; cX = new NormalDistribution(rng, x, xSigma, NormalDistribution.DEFAULT_INVERSE_ABSOLUTE_ACCURACY); cY = new NormalDistribution(rng, y, ySigma, NormalDistribution.DEFAULT_INVERSE_ABSOLUTE_ACCURACY); tP = new UniformRealDistribution(rng, 0, MathUtils.TWO_PI, UniformRealDistribution.DEFAULT_INVERSE_ABSOLUTE_ACCURACY); } /** * Point generator. * * @param n Number of points to create. * @return the cloud of {@code n} points. */ public Vector2D[] generate(int n) { final Vector2D[] cloud = new Vector2D[n]; for (int i = 0; i < n; i++) { cloud[i] = create(); } return cloud; } /** * Create one point. * * @return a point. */ private Vector2D create() { final double t = tP.sample(); final double pX = cX.sample() + radius * FastMath.cos(t); final double pY = cY.sample() + radius * FastMath.sin(t); return new Vector2D(pX, pY); } } ././@LongLink100644 0 0 216 12126630646 10260 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/optim/nonlinear/vector/jacobian/AbstractLeastSquaresOptimizerTestValidation.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/optim/nonlinear/vector/jacobian/Abstrac100644 1750 1750 32135 12126627671 32274 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with this * work for additional information regarding copyright ownership. The ASF * licenses this file to You 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. */ package org.apache.commons.math3.optim.nonlinear.vector.jacobian; import java.util.Arrays; import java.util.List; import java.util.ArrayList; import java.awt.geom.Point2D; import org.apache.commons.math3.optim.PointVectorValuePair; import org.apache.commons.math3.optim.InitialGuess; import org.apache.commons.math3.optim.MaxEval; import org.apache.commons.math3.optim.nonlinear.vector.Target; import org.apache.commons.math3.optim.nonlinear.vector.Weight; import org.apache.commons.math3.stat.descriptive.SummaryStatistics; import org.apache.commons.math3.stat.descriptive.StatisticalSummary; import org.apache.commons.math3.util.FastMath; import org.junit.Test; import org.junit.Assert; /** * This class demonstrates the main functionality of the * {@link AbstractLeastSquaresOptimizer}, common to the * optimizer implementations in package * {@link org.apache.commons.math3.optimization.general}. *
                * Not enabled by default, as the class name does not end with "Test". *
                * Invoke by running *
                
                 *  mvn test -Dtest=AbstractLeastSquaresOptimizerTestValidation
                 * 
                * or by running *
                
                 *  mvn test -Dtest=AbstractLeastSquaresOptimizerTestValidation -DargLine="-DmcRuns=1234 -server"
                 * 
                */ public class AbstractLeastSquaresOptimizerTestValidation { private static final int MONTE_CARLO_RUNS = Integer.parseInt(System.getProperty("mcRuns", "100")); /** * Using a Monte-Carlo procedure, this test checks the error estimations * as provided by the square-root of the diagonal elements of the * covariance matrix. *
                * The test generates sets of observations, each sampled from * a Gaussian distribution. *
                * The optimization problem solved is defined in class * {@link StraightLineProblem}. *
                * The output (on stdout) will be a table summarizing the distribution * of parameters generated by the Monte-Carlo process and by the direct * estimation provided by the diagonal elements of the covariance matrix. */ @Test public void testParametersErrorMonteCarloObservations() { // Error on the observations. final double yError = 15; // True values of the parameters. final double slope = 123.456; final double offset = -98.765; // Samples generator. final RandomStraightLinePointGenerator lineGenerator = new RandomStraightLinePointGenerator(slope, offset, yError, -1e3, 1e4, 138577L); // Number of observations. final int numObs = 100; // XXX Should be a command-line option. // number of parameters. final int numParams = 2; // Parameters found for each of Monte-Carlo run. final SummaryStatistics[] paramsFoundByDirectSolution = new SummaryStatistics[numParams]; // Sigma estimations (square-root of the diagonal elements of the // covariance matrix), for each Monte-Carlo run. final SummaryStatistics[] sigmaEstimate = new SummaryStatistics[numParams]; // Initialize statistics accumulators. for (int i = 0; i < numParams; i++) { paramsFoundByDirectSolution[i] = new SummaryStatistics(); sigmaEstimate[i] = new SummaryStatistics(); } // Dummy optimizer (to compute the covariance matrix). final AbstractLeastSquaresOptimizer optim = new DummyOptimizer(); final double[] init = { slope, offset }; // Monte-Carlo (generates many sets of observations). final int mcRepeat = MONTE_CARLO_RUNS; int mcCount = 0; while (mcCount < mcRepeat) { // Observations. final Point2D.Double[] obs = lineGenerator.generate(numObs); final StraightLineProblem problem = new StraightLineProblem(yError); for (int i = 0; i < numObs; i++) { final Point2D.Double p = obs[i]; problem.addPoint(p.x, p.y); } // Direct solution (using simple regression). final double[] regress = problem.solve(); // Estimation of the standard deviation (diagonal elements of the // covariance matrix). final PointVectorValuePair optimum = optim.optimize(new MaxEval(Integer.MAX_VALUE), problem.getModelFunction(), problem.getModelFunctionJacobian(), new Target(problem.target()), new Weight(problem.weight()), new InitialGuess(init)); final double[] sigma = optim.computeSigma(optimum.getPoint(), 1e-14); // Accumulate statistics. for (int i = 0; i < numParams; i++) { paramsFoundByDirectSolution[i].addValue(regress[i]); sigmaEstimate[i].addValue(sigma[i]); } // Next Monte-Carlo. ++mcCount; } // Print statistics. final String line = "--------------------------------------------------------------"; System.out.println(" True value Mean Std deviation"); for (int i = 0; i < numParams; i++) { System.out.println(line); System.out.println("Parameter #" + i); StatisticalSummary s = paramsFoundByDirectSolution[i].getSummary(); System.out.printf(" %+.6e %+.6e %+.6e\n", init[i], s.getMean(), s.getStandardDeviation()); s = sigmaEstimate[i].getSummary(); System.out.printf("sigma: %+.6e (%+.6e)\n", s.getMean(), s.getStandardDeviation()); } System.out.println(line); // Check the error estimation. for (int i = 0; i < numParams; i++) { Assert.assertEquals(paramsFoundByDirectSolution[i].getSummary().getStandardDeviation(), sigmaEstimate[i].getSummary().getMean(), 8e-2); } } /** * In this test, the set of observations is fixed. * Using a Monte-Carlo procedure, it generates sets of parameters, * and determine the parameter change that will result in the * normalized chi-square becoming larger by one than the value from * the best fit solution. *
                * The optimization problem solved is defined in class * {@link StraightLineProblem}. *
                * The output (on stdout) will be a list of lines containing: *
                  *
                • slope of the straight line,
                • *
                • intercept of the straight line,
                • *
                • chi-square of the solution defined by the above two values.
                • *
                * The output is separated into two blocks (with a blank line between * them); the first block will contain all parameter sets for which * {@code chi2 < chi2_b + 1} * and the second block, all sets for which * {@code chi2 >= chi2_b + 1} * where {@code chi2_b} is the lowest chi-square (corresponding to the * best solution). */ @Test public void testParametersErrorMonteCarloParameters() { // Error on the observations. final double yError = 15; // True values of the parameters. final double slope = 123.456; final double offset = -98.765; // Samples generator. final RandomStraightLinePointGenerator lineGenerator = new RandomStraightLinePointGenerator(slope, offset, yError, -1e3, 1e4, 13839013L); // Number of observations. final int numObs = 10; // number of parameters. final int numParams = 2; // Create a single set of observations. final Point2D.Double[] obs = lineGenerator.generate(numObs); final StraightLineProblem problem = new StraightLineProblem(yError); for (int i = 0; i < numObs; i++) { final Point2D.Double p = obs[i]; problem.addPoint(p.x, p.y); } // Direct solution (using simple regression). final double[] regress = problem.solve(); // Dummy optimizer (to compute the chi-square). final AbstractLeastSquaresOptimizer optim = new DummyOptimizer(); final double[] init = { slope, offset }; // Get chi-square of the best parameters set for the given set of // observations. final double bestChi2N = getChi2N(optim, problem, regress); final double[] sigma = optim.computeSigma(regress, 1e-14); // Monte-Carlo (generates a grid of parameters). final int mcRepeat = MONTE_CARLO_RUNS; final int gridSize = (int) FastMath.sqrt(mcRepeat); // Parameters found for each of Monte-Carlo run. // Index 0 = slope // Index 1 = offset // Index 2 = normalized chi2 final List paramsAndChi2 = new ArrayList(gridSize * gridSize); final double slopeRange = 10 * sigma[0]; final double offsetRange = 10 * sigma[1]; final double minSlope = slope - 0.5 * slopeRange; final double minOffset = offset - 0.5 * offsetRange; final double deltaSlope = slopeRange/ gridSize; final double deltaOffset = offsetRange / gridSize; for (int i = 0; i < gridSize; i++) { final double s = minSlope + i * deltaSlope; for (int j = 0; j < gridSize; j++) { final double o = minOffset + j * deltaOffset; final double chi2N = getChi2N(optim, problem, new double[] {s, o}); paramsAndChi2.add(new double[] {s, o, chi2N}); } } // Output (for use with "gnuplot"). // Some info. // For plotting separately sets of parameters that have a large chi2. final double chi2NPlusOne = bestChi2N + 1; int numLarger = 0; final String lineFmt = "%+.10e %+.10e %.8e\n"; // Point with smallest chi-square. System.out.printf(lineFmt, regress[0], regress[1], bestChi2N); System.out.println(); // Empty line. // Points within the confidence interval. for (double[] d : paramsAndChi2) { if (d[2] <= chi2NPlusOne) { System.out.printf(lineFmt, d[0], d[1], d[2]); } } System.out.println(); // Empty line. // Points outside the confidence interval. for (double[] d : paramsAndChi2) { if (d[2] > chi2NPlusOne) { ++numLarger; System.out.printf(lineFmt, d[0], d[1], d[2]); } } System.out.println(); // Empty line. System.out.println("# sigma=" + Arrays.toString(sigma)); System.out.println("# " + numLarger + " sets filtered out"); } /** * @return the normalized chi-square. */ private double getChi2N(AbstractLeastSquaresOptimizer optim, StraightLineProblem problem, double[] params) { final double[] t = problem.target(); final double[] w = problem.weight(); optim.optimize(new MaxEval(Integer.MAX_VALUE), problem.getModelFunction(), problem.getModelFunctionJacobian(), new Target(t), new Weight(w), new InitialGuess(params)); return optim.getChiSquare() / (t.length - params.length); } } /** * A dummy optimizer. * Used for computing the covariance matrix. */ class DummyOptimizer extends AbstractLeastSquaresOptimizer { public DummyOptimizer() { super(null); } /** * This method does nothing and returns a dummy value. */ @Override public PointVectorValuePair doOptimize() { final double[] params = getStartPoint(); final double[] res = computeResiduals(computeObjectiveValue(params)); setCost(computeCost(res)); return new PointVectorValuePair(params, null); } } ././@LongLink100644 0 0 202 12126630646 10253 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/optim/nonlinear/vector/jacobian/LevenbergMarquardtOptimizerTest.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/optim/nonlinear/vector/jacobian/Levenbe100644 1750 1750 45530 12126627671 32300 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.optim.nonlinear.vector.jacobian; import java.io.Serializable; import java.util.ArrayList; import java.util.List; import org.apache.commons.math3.optim.PointVectorValuePair; import org.apache.commons.math3.optim.InitialGuess; import org.apache.commons.math3.optim.MaxEval; import org.apache.commons.math3.optim.SimpleBounds; import org.apache.commons.math3.optim.nonlinear.vector.Target; import org.apache.commons.math3.optim.nonlinear.vector.Weight; import org.apache.commons.math3.optim.nonlinear.vector.ModelFunction; import org.apache.commons.math3.optim.nonlinear.vector.ModelFunctionJacobian; import org.apache.commons.math3.analysis.MultivariateVectorFunction; import org.apache.commons.math3.analysis.MultivariateMatrixFunction; import org.apache.commons.math3.exception.ConvergenceException; import org.apache.commons.math3.exception.DimensionMismatchException; import org.apache.commons.math3.exception.TooManyEvaluationsException; import org.apache.commons.math3.exception.MathUnsupportedOperationException; import org.apache.commons.math3.geometry.euclidean.twod.Vector2D; import org.apache.commons.math3.linear.SingularMatrixException; import org.apache.commons.math3.util.FastMath; import org.apache.commons.math3.util.Precision; import org.junit.Assert; import org.junit.Test; import org.junit.Ignore; /** *

                Some of the unit tests are re-implementations of the MINPACK file17 and file22 test files. * The redistribution policy for MINPACK is available here, for * convenience, it is reproduced below.

                * * * *
                * Minpack Copyright Notice (1999) University of Chicago. * All rights reserved *
                * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: *
                  *
                1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer.
                2. *
                3. Redistributions in binary form must reproduce the above * copyright notice, this list of conditions and the following * disclaimer in the documentation and/or other materials provided * with the distribution.
                4. *
                5. The end-user documentation included with the redistribution, if any, * must include the following acknowledgment: * This product includes software developed by the University of * Chicago, as Operator of Argonne National Laboratory. * Alternately, this acknowledgment may appear in the software itself, * if and wherever such third-party acknowledgments normally appear.
                6. *
                7. WARRANTY DISCLAIMER. THE SOFTWARE IS SUPPLIED "AS IS" * WITHOUT WARRANTY OF ANY KIND. THE COPYRIGHT HOLDER, THE * UNITED STATES, THE UNITED STATES DEPARTMENT OF ENERGY, AND * THEIR EMPLOYEES: (1) DISCLAIM ANY WARRANTIES, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO ANY IMPLIED WARRANTIES * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE * OR NON-INFRINGEMENT, (2) DO NOT ASSUME ANY LEGAL LIABILITY * OR RESPONSIBILITY FOR THE ACCURACY, COMPLETENESS, OR * USEFULNESS OF THE SOFTWARE, (3) DO NOT REPRESENT THAT USE OF * THE SOFTWARE WOULD NOT INFRINGE PRIVATELY OWNED RIGHTS, (4) * DO NOT WARRANT THAT THE SOFTWARE WILL FUNCTION * UNINTERRUPTED, THAT IT IS ERROR-FREE OR THAT ANY ERRORS WILL * BE CORRECTED.
                8. *
                9. LIMITATION OF LIABILITY. IN NO EVENT WILL THE COPYRIGHT * HOLDER, THE UNITED STATES, THE UNITED STATES DEPARTMENT OF * ENERGY, OR THEIR EMPLOYEES: BE LIABLE FOR ANY INDIRECT, * INCIDENTAL, CONSEQUENTIAL, SPECIAL OR PUNITIVE DAMAGES OF * ANY KIND OR NATURE, INCLUDING BUT NOT LIMITED TO LOSS OF * PROFITS OR LOSS OF DATA, FOR ANY REASON WHATSOEVER, WHETHER * SUCH LIABILITY IS ASSERTED ON THE BASIS OF CONTRACT, TORT * (INCLUDING NEGLIGENCE OR STRICT LIABILITY), OR OTHERWISE, * EVEN IF ANY OF SAID PARTIES HAS BEEN WARNED OF THE * POSSIBILITY OF SUCH LOSS OR DAMAGES.
                10. *
                  * @author Argonne National Laboratory. MINPACK project. March 1980 (original fortran minpack tests) * @author Burton S. Garbow (original fortran minpack tests) * @author Kenneth E. Hillstrom (original fortran minpack tests) * @author Jorge J. More (original fortran minpack tests) * @author Luc Maisonobe (non-minpack tests and minpack tests Java translation) */ public class LevenbergMarquardtOptimizerTest extends AbstractLeastSquaresOptimizerAbstractTest { @Override public AbstractLeastSquaresOptimizer createOptimizer() { return new LevenbergMarquardtOptimizer(); } @Test(expected=MathUnsupportedOperationException.class) public void testConstraintsUnsupported() { createOptimizer().optimize(new MaxEval(100), new Target(new double[] { 2 }), new Weight(new double[] { 1 }), new InitialGuess(new double[] { 1, 2 }), new SimpleBounds(new double[] { -10, 0 }, new double[] { 20, 30 })); } @Override @Test(expected=SingularMatrixException.class) public void testNonInvertible() { /* * Overrides the method from parent class, since the default singularity * threshold (1e-14) does not trigger the expected exception. */ LinearProblem problem = new LinearProblem(new double[][] { { 1, 2, -3 }, { 2, 1, 3 }, { -3, 0, -9 } }, new double[] { 1, 1, 1 }); AbstractLeastSquaresOptimizer optimizer = createOptimizer(); PointVectorValuePair optimum = optimizer.optimize(new MaxEval(100), problem.getModelFunction(), problem.getModelFunctionJacobian(), problem.getTarget(), new Weight(new double[] { 1, 1, 1 }), new InitialGuess(new double[] { 0, 0, 0 })); Assert.assertTrue(FastMath.sqrt(optimizer.getTargetSize()) * optimizer.getRMS() > 0.6); optimizer.computeCovariances(optimum.getPoint(), 1.5e-14); } @Test public void testControlParameters() { CircleVectorial circle = new CircleVectorial(); circle.addPoint( 30.0, 68.0); circle.addPoint( 50.0, -6.0); circle.addPoint(110.0, -20.0); circle.addPoint( 35.0, 15.0); circle.addPoint( 45.0, 97.0); checkEstimate(circle.getModelFunction(), circle.getModelFunctionJacobian(), 0.1, 10, 1.0e-14, 1.0e-16, 1.0e-10, false); checkEstimate(circle.getModelFunction(), circle.getModelFunctionJacobian(), 0.1, 10, 1.0e-15, 1.0e-17, 1.0e-10, true); checkEstimate(circle.getModelFunction(), circle.getModelFunctionJacobian(), 0.1, 5, 1.0e-15, 1.0e-16, 1.0e-10, true); circle.addPoint(300, -300); checkEstimate(circle.getModelFunction(), circle.getModelFunctionJacobian(), 0.1, 20, 1.0e-18, 1.0e-16, 1.0e-10, true); } private void checkEstimate(ModelFunction problem, ModelFunctionJacobian problemJacobian, double initialStepBoundFactor, int maxCostEval, double costRelativeTolerance, double parRelativeTolerance, double orthoTolerance, boolean shouldFail) { try { LevenbergMarquardtOptimizer optimizer = new LevenbergMarquardtOptimizer(initialStepBoundFactor, costRelativeTolerance, parRelativeTolerance, orthoTolerance, Precision.SAFE_MIN); optimizer.optimize(new MaxEval(maxCostEval), problem, problemJacobian, new Target(new double[] { 0, 0, 0, 0, 0 }), new Weight(new double[] { 1, 1, 1, 1, 1 }), new InitialGuess(new double[] { 98.680, 47.345 })); Assert.assertTrue(!shouldFail); } catch (DimensionMismatchException ee) { Assert.assertTrue(shouldFail); } catch (TooManyEvaluationsException ee) { Assert.assertTrue(shouldFail); } } /** * Non-linear test case: fitting of decay curve (from Chapter 8 of * Bevington's textbook, "Data reduction and analysis for the physical sciences"). * XXX The expected ("reference") values may not be accurate and the tolerance too * relaxed for this test to be currently really useful (the issue is under * investigation). */ @Test public void testBevington() { final double[][] dataPoints = { // column 1 = times { 15, 30, 45, 60, 75, 90, 105, 120, 135, 150, 165, 180, 195, 210, 225, 240, 255, 270, 285, 300, 315, 330, 345, 360, 375, 390, 405, 420, 435, 450, 465, 480, 495, 510, 525, 540, 555, 570, 585, 600, 615, 630, 645, 660, 675, 690, 705, 720, 735, 750, 765, 780, 795, 810, 825, 840, 855, 870, 885, }, // column 2 = measured counts { 775, 479, 380, 302, 185, 157, 137, 119, 110, 89, 74, 61, 66, 68, 48, 54, 51, 46, 55, 29, 28, 37, 49, 26, 35, 29, 31, 24, 25, 35, 24, 30, 26, 28, 21, 18, 20, 27, 17, 17, 14, 17, 24, 11, 22, 17, 12, 10, 13, 16, 9, 9, 14, 21, 17, 13, 12, 18, 10, }, }; final BevingtonProblem problem = new BevingtonProblem(); final int len = dataPoints[0].length; final double[] weights = new double[len]; for (int i = 0; i < len; i++) { problem.addPoint(dataPoints[0][i], dataPoints[1][i]); weights[i] = 1 / dataPoints[1][i]; } final LevenbergMarquardtOptimizer optimizer = new LevenbergMarquardtOptimizer(); final PointVectorValuePair optimum = optimizer.optimize(new MaxEval(100), problem.getModelFunction(), problem.getModelFunctionJacobian(), new Target(dataPoints[1]), new Weight(weights), new InitialGuess(new double[] { 10, 900, 80, 27, 225 })); final double[] solution = optimum.getPoint(); final double[] expectedSolution = { 10.4, 958.3, 131.4, 33.9, 205.0 }; final double[][] covarMatrix = optimizer.computeCovariances(solution, 1e-14); final double[][] expectedCovarMatrix = { { 3.38, -3.69, 27.98, -2.34, -49.24 }, { -3.69, 2492.26, 81.89, -69.21, -8.9 }, { 27.98, 81.89, 468.99, -44.22, -615.44 }, { -2.34, -69.21, -44.22, 6.39, 53.80 }, { -49.24, -8.9, -615.44, 53.8, 929.45 } }; final int numParams = expectedSolution.length; // Check that the computed solution is within the reference error range. for (int i = 0; i < numParams; i++) { final double error = FastMath.sqrt(expectedCovarMatrix[i][i]); Assert.assertEquals("Parameter " + i, expectedSolution[i], solution[i], error); } // Check that each entry of the computed covariance matrix is within 10% // of the reference matrix entry. for (int i = 0; i < numParams; i++) { for (int j = 0; j < numParams; j++) { Assert.assertEquals("Covariance matrix [" + i + "][" + j + "]", expectedCovarMatrix[i][j], covarMatrix[i][j], FastMath.abs(0.1 * expectedCovarMatrix[i][j])); } } } @Test public void testCircleFitting2() { final double xCenter = 123.456; final double yCenter = 654.321; final double xSigma = 10; final double ySigma = 15; final double radius = 111.111; // The test is extremely sensitive to the seed. final long seed = 59421061L; final RandomCirclePointGenerator factory = new RandomCirclePointGenerator(xCenter, yCenter, radius, xSigma, ySigma, seed); final CircleProblem circle = new CircleProblem(xSigma, ySigma); final int numPoints = 10; for (Vector2D p : factory.generate(numPoints)) { circle.addPoint(p.getX(), p.getY()); } // First guess for the center's coordinates and radius. final double[] init = { 90, 659, 115 }; final LevenbergMarquardtOptimizer optimizer = new LevenbergMarquardtOptimizer(); final PointVectorValuePair optimum = optimizer.optimize(new MaxEval(100), circle.getModelFunction(), circle.getModelFunctionJacobian(), new Target(circle.target()), new Weight(circle.weight()), new InitialGuess(init)); final double[] paramFound = optimum.getPoint(); // Retrieve errors estimation. final double[] asymptoticStandardErrorFound = optimizer.computeSigma(paramFound, 1e-14); // Check that the parameters are found within the assumed error bars. Assert.assertEquals(xCenter, paramFound[0], asymptoticStandardErrorFound[0]); Assert.assertEquals(yCenter, paramFound[1], asymptoticStandardErrorFound[1]); Assert.assertEquals(radius, paramFound[2], asymptoticStandardErrorFound[2]); } private static class QuadraticProblem { private List x; private List y; public QuadraticProblem() { x = new ArrayList(); y = new ArrayList(); } public void addPoint(double x, double y) { this.x.add(x); this.y.add(y); } public ModelFunction getModelFunction() { return new ModelFunction(new MultivariateVectorFunction() { public double[] value(double[] variables) { double[] values = new double[x.size()]; for (int i = 0; i < values.length; ++i) { values[i] = (variables[0] * x.get(i) + variables[1]) * x.get(i) + variables[2]; } return values; } }); } public ModelFunctionJacobian getModelFunctionJacobian() { return new ModelFunctionJacobian(new MultivariateMatrixFunction() { public double[][] value(double[] params) { double[][] jacobian = new double[x.size()][3]; for (int i = 0; i < jacobian.length; ++i) { jacobian[i][0] = x.get(i) * x.get(i); jacobian[i][1] = x.get(i); jacobian[i][2] = 1.0; } return jacobian; } }); } } private static class BevingtonProblem { private List time; private List count; public BevingtonProblem() { time = new ArrayList(); count = new ArrayList(); } public void addPoint(double t, double c) { time.add(t); count.add(c); } public ModelFunction getModelFunction() { return new ModelFunction(new MultivariateVectorFunction() { public double[] value(double[] params) { double[] values = new double[time.size()]; for (int i = 0; i < values.length; ++i) { final double t = time.get(i); values[i] = params[0] + params[1] * Math.exp(-t / params[3]) + params[2] * Math.exp(-t / params[4]); } return values; } }); } public ModelFunctionJacobian getModelFunctionJacobian() { return new ModelFunctionJacobian(new MultivariateMatrixFunction() { public double[][] value(double[] params) { double[][] jacobian = new double[time.size()][5]; for (int i = 0; i < jacobian.length; ++i) { final double t = time.get(i); jacobian[i][0] = 1; final double p3 = params[3]; final double p4 = params[4]; final double tOp3 = t / p3; final double tOp4 = t / p4; jacobian[i][1] = Math.exp(-tOp3); jacobian[i][2] = Math.exp(-tOp4); jacobian[i][3] = params[1] * Math.exp(-tOp3) * tOp3 / p3; jacobian[i][4] = params[2] * Math.exp(-tOp4) * tOp4 / p4; } return jacobian; } }); } } } ././@LongLink100644 0 0 160 12126630646 10256 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/optim/nonlinear/vector/jacobian/CircleProblem.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/optim/nonlinear/vector/jacobian/CircleP100644 1750 1750 15222 12126627671 32234 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.optim.nonlinear.vector.jacobian; import java.util.ArrayList; import org.apache.commons.math3.analysis.MultivariateVectorFunction; import org.apache.commons.math3.analysis.MultivariateMatrixFunction; import org.apache.commons.math3.util.MathUtils; import org.apache.commons.math3.util.FastMath; import org.apache.commons.math3.optim.nonlinear.vector.ModelFunction; import org.apache.commons.math3.optim.nonlinear.vector.ModelFunctionJacobian; /** * Class that models a circle. * The parameters of problem are: *
                    *
                  • the x-coordinate of the circle center,
                  • *
                  • the y-coordinate of the circle center,
                  • *
                  • the radius of the circle.
                  • *
                  * The model functions are: *
                    *
                  • for each triplet (cx, cy, r), the (x, y) coordinates of a point on the * corresponding circle.
                  • *
                  */ class CircleProblem { /** Cloud of points assumed to be fitted by a circle. */ private final ArrayList points; /** Error on the x-coordinate of the points. */ private final double xSigma; /** Error on the y-coordinate of the points. */ private final double ySigma; /** Number of points on the circumference (when searching which model point is closest to a given "observation". */ private final int resolution; /** * @param xError Assumed error for the x-coordinate of the circle points. * @param yError Assumed error for the y-coordinate of the circle points. * @param searchResolution Number of points to try when searching the one * that is closest to a given "observed" point. */ public CircleProblem(double xError, double yError, int searchResolution) { points = new ArrayList(); xSigma = xError; ySigma = yError; resolution = searchResolution; } /** * @param xError Assumed error for the x-coordinate of the circle points. * @param yError Assumed error for the y-coordinate of the circle points. */ public CircleProblem(double xError, double yError) { this(xError, yError, 500); } public void addPoint(double px, double py) { points.add(new double[] { px, py }); } public double[] target() { final double[] t = new double[points.size() * 2]; for (int i = 0; i < points.size(); i++) { final double[] p = points.get(i); final int index = i * 2; t[index] = p[0]; t[index + 1] = p[1]; } return t; } public double[] weight() { final double wX = 1 / (xSigma * xSigma); final double wY = 1 / (ySigma * ySigma); final double[] w = new double[points.size() * 2]; for (int i = 0; i < points.size(); i++) { final int index = i * 2; w[index] = wX; w[index + 1] = wY; } return w; } public ModelFunction getModelFunction() { return new ModelFunction(new MultivariateVectorFunction() { public double[] value(double[] params) { final double cx = params[0]; final double cy = params[1]; final double r = params[2]; final double[] model = new double[points.size() * 2]; final double deltaTheta = MathUtils.TWO_PI / resolution; for (int i = 0; i < points.size(); i++) { final double[] p = points.get(i); final double px = p[0]; final double py = p[1]; double bestX = 0; double bestY = 0; double dMin = Double.POSITIVE_INFINITY; // Find the angle for which the circle passes closest to the // current point (using a resolution of 100 points along the // circumference). for (double theta = 0; theta <= MathUtils.TWO_PI; theta += deltaTheta) { final double currentX = cx + r * FastMath.cos(theta); final double currentY = cy + r * FastMath.sin(theta); final double dX = currentX - px; final double dY = currentY - py; final double d = dX * dX + dY * dY; if (d < dMin) { dMin = d; bestX = currentX; bestY = currentY; } } final int index = i * 2; model[index] = bestX; model[index + 1] = bestY; } return model; } }); } public ModelFunctionJacobian getModelFunctionJacobian() { return new ModelFunctionJacobian(new MultivariateMatrixFunction() { public double[][] value(double[] point) { return jacobian(point); } }); } private double[][] jacobian(double[] params) { final double[][] jacobian = new double[points.size() * 2][3]; for (int i = 0; i < points.size(); i++) { final int index = i * 2; // Partial derivative wrt x-coordinate of center. jacobian[index][0] = 1; jacobian[index + 1][0] = 0; // Partial derivative wrt y-coordinate of center. jacobian[index][1] = 0; jacobian[index + 1][1] = 1; // Partial derivative wrt radius. final double[] p = points.get(i); jacobian[index][2] = (p[0] - params[0]) / params[2]; jacobian[index + 1][2] = (p[1] - params[1]) / params[2]; } return jacobian; } } ././@LongLink100644 0 0 173 12126630646 10262 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/optim/nonlinear/vector/jacobian/GaussNewtonOptimizerTest.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/optim/nonlinear/vector/jacobian/GaussNe100644 1750 1750 17114 12126627671 32262 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.optim.nonlinear.vector.jacobian; import java.io.IOException; import org.apache.commons.math3.exception.ConvergenceException; import org.apache.commons.math3.exception.TooManyEvaluationsException; import org.apache.commons.math3.exception.MathUnsupportedOperationException; import org.apache.commons.math3.optim.SimpleVectorValueChecker; import org.apache.commons.math3.optim.InitialGuess; import org.apache.commons.math3.optim.MaxEval; import org.apache.commons.math3.optim.SimpleBounds; import org.apache.commons.math3.optim.nonlinear.vector.Target; import org.apache.commons.math3.optim.nonlinear.vector.Weight; import org.apache.commons.math3.optim.nonlinear.vector.ModelFunction; import org.apache.commons.math3.optim.nonlinear.vector.ModelFunctionJacobian; import org.junit.Test; /** *

                  Some of the unit tests are re-implementations of the MINPACK file17 and file22 test files. * The redistribution policy for MINPACK is available here, for * convenience, it is reproduced below.

                  * * * *
                  * Minpack Copyright Notice (1999) University of Chicago. * All rights reserved *
                  * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: *
                    *
                  1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer.
                  2. *
                  3. Redistributions in binary form must reproduce the above * copyright notice, this list of conditions and the following * disclaimer in the documentation and/or other materials provided * with the distribution.
                  4. *
                  5. The end-user documentation included with the redistribution, if any, * must include the following acknowledgment: * This product includes software developed by the University of * Chicago, as Operator of Argonne National Laboratory. * Alternately, this acknowledgment may appear in the software itself, * if and wherever such third-party acknowledgments normally appear.
                  6. *
                  7. WARRANTY DISCLAIMER. THE SOFTWARE IS SUPPLIED "AS IS" * WITHOUT WARRANTY OF ANY KIND. THE COPYRIGHT HOLDER, THE * UNITED STATES, THE UNITED STATES DEPARTMENT OF ENERGY, AND * THEIR EMPLOYEES: (1) DISCLAIM ANY WARRANTIES, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO ANY IMPLIED WARRANTIES * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE * OR NON-INFRINGEMENT, (2) DO NOT ASSUME ANY LEGAL LIABILITY * OR RESPONSIBILITY FOR THE ACCURACY, COMPLETENESS, OR * USEFULNESS OF THE SOFTWARE, (3) DO NOT REPRESENT THAT USE OF * THE SOFTWARE WOULD NOT INFRINGE PRIVATELY OWNED RIGHTS, (4) * DO NOT WARRANT THAT THE SOFTWARE WILL FUNCTION * UNINTERRUPTED, THAT IT IS ERROR-FREE OR THAT ANY ERRORS WILL * BE CORRECTED.
                  8. *
                  9. LIMITATION OF LIABILITY. IN NO EVENT WILL THE COPYRIGHT * HOLDER, THE UNITED STATES, THE UNITED STATES DEPARTMENT OF * ENERGY, OR THEIR EMPLOYEES: BE LIABLE FOR ANY INDIRECT, * INCIDENTAL, CONSEQUENTIAL, SPECIAL OR PUNITIVE DAMAGES OF * ANY KIND OR NATURE, INCLUDING BUT NOT LIMITED TO LOSS OF * PROFITS OR LOSS OF DATA, FOR ANY REASON WHATSOEVER, WHETHER * SUCH LIABILITY IS ASSERTED ON THE BASIS OF CONTRACT, TORT * (INCLUDING NEGLIGENCE OR STRICT LIABILITY), OR OTHERWISE, * EVEN IF ANY OF SAID PARTIES HAS BEEN WARNED OF THE * POSSIBILITY OF SUCH LOSS OR DAMAGES.
                  10. *
                    * @author Argonne National Laboratory. MINPACK project. March 1980 (original fortran minpack tests) * @author Burton S. Garbow (original fortran minpack tests) * @author Kenneth E. Hillstrom (original fortran minpack tests) * @author Jorge J. More (original fortran minpack tests) * @author Luc Maisonobe (non-minpack tests and minpack tests Java translation) */ public class GaussNewtonOptimizerTest extends AbstractLeastSquaresOptimizerAbstractTest { @Override public AbstractLeastSquaresOptimizer createOptimizer() { return new GaussNewtonOptimizer(new SimpleVectorValueChecker(1.0e-6, 1.0e-6)); } @Test(expected=MathUnsupportedOperationException.class) public void testConstraintsUnsupported() { createOptimizer().optimize(new MaxEval(100), new Target(new double[] { 2 }), new Weight(new double[] { 1 }), new InitialGuess(new double[] { 1, 2 }), new SimpleBounds(new double[] { -10, 0 }, new double[] { 20, 30 })); } @Override @Test(expected = ConvergenceException.class) public void testMoreEstimatedParametersSimple() { /* * Exception is expected with this optimizer */ super.testMoreEstimatedParametersSimple(); } @Override @Test(expected=ConvergenceException.class) public void testMoreEstimatedParametersUnsorted() { /* * Exception is expected with this optimizer */ super.testMoreEstimatedParametersUnsorted(); } @Test(expected=TooManyEvaluationsException.class) public void testMaxEvaluations() throws Exception { CircleVectorial circle = new CircleVectorial(); circle.addPoint( 30.0, 68.0); circle.addPoint( 50.0, -6.0); circle.addPoint(110.0, -20.0); circle.addPoint( 35.0, 15.0); circle.addPoint( 45.0, 97.0); GaussNewtonOptimizer optimizer = new GaussNewtonOptimizer(new SimpleVectorValueChecker(1e-30, 1e-30)); optimizer.optimize(new MaxEval(100), circle.getModelFunction(), circle.getModelFunctionJacobian(), new Target(new double[] { 0, 0, 0, 0, 0 }), new Weight(new double[] { 1, 1, 1, 1, 1 }), new InitialGuess(new double[] { 98.680, 47.345 })); } @Override @Test(expected=ConvergenceException.class) public void testCircleFittingBadInit() { /* * This test does not converge with this optimizer. */ super.testCircleFittingBadInit(); } @Override @Test(expected = ConvergenceException.class) public void testHahn1() throws IOException { /* * TODO This test leads to a singular problem with the Gauss-Newton * optimizer. This should be inquired. */ super.testHahn1(); } } ././@LongLink100644 0 0 214 12126630646 10256 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/optim/nonlinear/vector/jacobian/AbstractLeastSquaresOptimizerAbstractTest.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/optim/nonlinear/vector/jacobian/Abstrac100644 1750 1750 75367 12126627671 32312 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.optim.nonlinear.vector.jacobian; import java.io.IOException; import java.io.Serializable; import java.util.Arrays; import org.apache.commons.math3.analysis.MultivariateVectorFunction; import org.apache.commons.math3.analysis.MultivariateMatrixFunction; import org.apache.commons.math3.exception.ConvergenceException; import org.apache.commons.math3.exception.DimensionMismatchException; import org.apache.commons.math3.exception.NumberIsTooSmallException; import org.apache.commons.math3.geometry.euclidean.twod.Vector2D; import org.apache.commons.math3.linear.BlockRealMatrix; import org.apache.commons.math3.linear.RealMatrix; import org.apache.commons.math3.optim.PointVectorValuePair; import org.apache.commons.math3.optim.InitialGuess; import org.apache.commons.math3.optim.MaxEval; import org.apache.commons.math3.optim.nonlinear.vector.Target; import org.apache.commons.math3.optim.nonlinear.vector.Weight; import org.apache.commons.math3.optim.nonlinear.vector.ModelFunction; import org.apache.commons.math3.optim.nonlinear.vector.ModelFunctionJacobian; import org.apache.commons.math3.util.FastMath; import org.junit.Assert; import org.junit.Test; /** *

                    Some of the unit tests are re-implementations of the MINPACK file17 and file22 test files. * The redistribution policy for MINPACK is available here, for * convenience, it is reproduced below.

                    * * * *
                    * Minpack Copyright Notice (1999) University of Chicago. * All rights reserved *
                    * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: *
                      *
                    1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer.
                    2. *
                    3. Redistributions in binary form must reproduce the above * copyright notice, this list of conditions and the following * disclaimer in the documentation and/or other materials provided * with the distribution.
                    4. *
                    5. The end-user documentation included with the redistribution, if any, * must include the following acknowledgment: * This product includes software developed by the University of * Chicago, as Operator of Argonne National Laboratory. * Alternately, this acknowledgment may appear in the software itself, * if and wherever such third-party acknowledgments normally appear.
                    6. *
                    7. WARRANTY DISCLAIMER. THE SOFTWARE IS SUPPLIED "AS IS" * WITHOUT WARRANTY OF ANY KIND. THE COPYRIGHT HOLDER, THE * UNITED STATES, THE UNITED STATES DEPARTMENT OF ENERGY, AND * THEIR EMPLOYEES: (1) DISCLAIM ANY WARRANTIES, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO ANY IMPLIED WARRANTIES * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE * OR NON-INFRINGEMENT, (2) DO NOT ASSUME ANY LEGAL LIABILITY * OR RESPONSIBILITY FOR THE ACCURACY, COMPLETENESS, OR * USEFULNESS OF THE SOFTWARE, (3) DO NOT REPRESENT THAT USE OF * THE SOFTWARE WOULD NOT INFRINGE PRIVATELY OWNED RIGHTS, (4) * DO NOT WARRANT THAT THE SOFTWARE WILL FUNCTION * UNINTERRUPTED, THAT IT IS ERROR-FREE OR THAT ANY ERRORS WILL * BE CORRECTED.
                    8. *
                    9. LIMITATION OF LIABILITY. IN NO EVENT WILL THE COPYRIGHT * HOLDER, THE UNITED STATES, THE UNITED STATES DEPARTMENT OF * ENERGY, OR THEIR EMPLOYEES: BE LIABLE FOR ANY INDIRECT, * INCIDENTAL, CONSEQUENTIAL, SPECIAL OR PUNITIVE DAMAGES OF * ANY KIND OR NATURE, INCLUDING BUT NOT LIMITED TO LOSS OF * PROFITS OR LOSS OF DATA, FOR ANY REASON WHATSOEVER, WHETHER * SUCH LIABILITY IS ASSERTED ON THE BASIS OF CONTRACT, TORT * (INCLUDING NEGLIGENCE OR STRICT LIABILITY), OR OTHERWISE, * EVEN IF ANY OF SAID PARTIES HAS BEEN WARNED OF THE * POSSIBILITY OF SUCH LOSS OR DAMAGES.
                    10. *
                      * @author Argonne National Laboratory. MINPACK project. March 1980 (original fortran minpack tests) * @author Burton S. Garbow (original fortran minpack tests) * @author Kenneth E. Hillstrom (original fortran minpack tests) * @author Jorge J. More (original fortran minpack tests) * @author Luc Maisonobe (non-minpack tests and minpack tests Java translation) * @version $Id: AbstractLeastSquaresOptimizerAbstractTest.java 1407467 2012-11-09 14:30:49Z erans $ */ public abstract class AbstractLeastSquaresOptimizerAbstractTest { public abstract AbstractLeastSquaresOptimizer createOptimizer(); @Test public void testGetIterations() { AbstractLeastSquaresOptimizer optim = createOptimizer(); optim.optimize(new MaxEval(100), new Target(new double[] { 1 }), new Weight(new double[] { 1 }), new InitialGuess(new double[] { 3 }), new ModelFunction(new MultivariateVectorFunction() { public double[] value(double[] point) { return new double[] { FastMath.pow(point[0], 4) }; } }), new ModelFunctionJacobian(new MultivariateMatrixFunction() { public double[][] value(double[] point) { return new double[][] { { 0.25 * FastMath.pow(point[0], 3) } }; } })); Assert.assertTrue(optim.getIterations() > 0); } @Test public void testTrivial() { LinearProblem problem = new LinearProblem(new double[][] { { 2 } }, new double[] { 3 }); AbstractLeastSquaresOptimizer optimizer = createOptimizer(); PointVectorValuePair optimum = optimizer.optimize(new MaxEval(100), problem.getModelFunction(), problem.getModelFunctionJacobian(), problem.getTarget(), new Weight(new double[] { 1 }), new InitialGuess(new double[] { 0 })); Assert.assertEquals(0, optimizer.getRMS(), 1e-10); Assert.assertEquals(1.5, optimum.getPoint()[0], 1e-10); Assert.assertEquals(3.0, optimum.getValue()[0], 1e-10); } @Test public void testQRColumnsPermutation() { LinearProblem problem = new LinearProblem(new double[][] { { 1, -1 }, { 0, 2 }, { 1, -2 } }, new double[] { 4, 6, 1 }); AbstractLeastSquaresOptimizer optimizer = createOptimizer(); PointVectorValuePair optimum = optimizer.optimize(new MaxEval(100), problem.getModelFunction(), problem.getModelFunctionJacobian(), problem.getTarget(), new Weight(new double[] { 1, 1, 1 }), new InitialGuess(new double[] { 0, 0 })); Assert.assertEquals(0, optimizer.getRMS(), 1e-10); Assert.assertEquals(7, optimum.getPoint()[0], 1e-10); Assert.assertEquals(3, optimum.getPoint()[1], 1e-10); Assert.assertEquals(4, optimum.getValue()[0], 1e-10); Assert.assertEquals(6, optimum.getValue()[1], 1e-10); Assert.assertEquals(1, optimum.getValue()[2], 1e-10); } @Test public void testNoDependency() { LinearProblem problem = new LinearProblem(new double[][] { { 2, 0, 0, 0, 0, 0 }, { 0, 2, 0, 0, 0, 0 }, { 0, 0, 2, 0, 0, 0 }, { 0, 0, 0, 2, 0, 0 }, { 0, 0, 0, 0, 2, 0 }, { 0, 0, 0, 0, 0, 2 } }, new double[] { 0, 1.1, 2.2, 3.3, 4.4, 5.5 }); AbstractLeastSquaresOptimizer optimizer = createOptimizer(); PointVectorValuePair optimum = optimizer.optimize(new MaxEval(100), problem.getModelFunction(), problem.getModelFunctionJacobian(), problem.getTarget(), new Weight(new double[] { 1, 1, 1, 1, 1, 1 }), new InitialGuess(new double[] { 0, 0, 0, 0, 0, 0 })); Assert.assertEquals(0, optimizer.getRMS(), 1e-10); for (int i = 0; i < problem.target.length; ++i) { Assert.assertEquals(0.55 * i, optimum.getPoint()[i], 1e-10); } } @Test public void testOneSet() { LinearProblem problem = new LinearProblem(new double[][] { { 1, 0, 0 }, { -1, 1, 0 }, { 0, -1, 1 } }, new double[] { 1, 1, 1}); AbstractLeastSquaresOptimizer optimizer = createOptimizer(); PointVectorValuePair optimum = optimizer.optimize(new MaxEval(100), problem.getModelFunction(), problem.getModelFunctionJacobian(), problem.getTarget(), new Weight(new double[] { 1, 1, 1 }), new InitialGuess(new double[] { 0, 0, 0 })); Assert.assertEquals(0, optimizer.getRMS(), 1e-10); Assert.assertEquals(1, optimum.getPoint()[0], 1e-10); Assert.assertEquals(2, optimum.getPoint()[1], 1e-10); Assert.assertEquals(3, optimum.getPoint()[2], 1e-10); } @Test public void testTwoSets() { double epsilon = 1e-7; LinearProblem problem = new LinearProblem(new double[][] { { 2, 1, 0, 4, 0, 0 }, { -4, -2, 3, -7, 0, 0 }, { 4, 1, -2, 8, 0, 0 }, { 0, -3, -12, -1, 0, 0 }, { 0, 0, 0, 0, epsilon, 1 }, { 0, 0, 0, 0, 1, 1 } }, new double[] { 2, -9, 2, 2, 1 + epsilon * epsilon, 2}); AbstractLeastSquaresOptimizer optimizer = createOptimizer(); PointVectorValuePair optimum = optimizer.optimize(new MaxEval(100), problem.getModelFunction(), problem.getModelFunctionJacobian(), problem.getTarget(), new Weight(new double[] { 1, 1, 1, 1, 1, 1 }), new InitialGuess(new double[] { 0, 0, 0, 0, 0, 0 })); Assert.assertEquals(0, optimizer.getRMS(), 1e-10); Assert.assertEquals(3, optimum.getPoint()[0], 1e-10); Assert.assertEquals(4, optimum.getPoint()[1], 1e-10); Assert.assertEquals(-1, optimum.getPoint()[2], 1e-10); Assert.assertEquals(-2, optimum.getPoint()[3], 1e-10); Assert.assertEquals(1 + epsilon, optimum.getPoint()[4], 1e-10); Assert.assertEquals(1 - epsilon, optimum.getPoint()[5], 1e-10); } @Test(expected=ConvergenceException.class) public void testNonInvertible() throws Exception { LinearProblem problem = new LinearProblem(new double[][] { { 1, 2, -3 }, { 2, 1, 3 }, { -3, 0, -9 } }, new double[] { 1, 1, 1 }); AbstractLeastSquaresOptimizer optimizer = createOptimizer(); optimizer.optimize(new MaxEval(100), problem.getModelFunction(), problem.getModelFunctionJacobian(), problem.getTarget(), new Weight(new double[] { 1, 1, 1 }), new InitialGuess(new double[] { 0, 0, 0 })); } @Test public void testIllConditioned() { LinearProblem problem1 = new LinearProblem(new double[][] { { 10, 7, 8, 7 }, { 7, 5, 6, 5 }, { 8, 6, 10, 9 }, { 7, 5, 9, 10 } }, new double[] { 32, 23, 33, 31 }); AbstractLeastSquaresOptimizer optimizer = createOptimizer(); PointVectorValuePair optimum1 = optimizer.optimize(new MaxEval(100), problem1.getModelFunction(), problem1.getModelFunctionJacobian(), problem1.getTarget(), new Weight(new double[] { 1, 1, 1, 1 }), new InitialGuess(new double[] { 0, 1, 2, 3 })); Assert.assertEquals(0, optimizer.getRMS(), 1e-10); Assert.assertEquals(1, optimum1.getPoint()[0], 1e-10); Assert.assertEquals(1, optimum1.getPoint()[1], 1e-10); Assert.assertEquals(1, optimum1.getPoint()[2], 1e-10); Assert.assertEquals(1, optimum1.getPoint()[3], 1e-10); LinearProblem problem2 = new LinearProblem(new double[][] { { 10.00, 7.00, 8.10, 7.20 }, { 7.08, 5.04, 6.00, 5.00 }, { 8.00, 5.98, 9.89, 9.00 }, { 6.99, 4.99, 9.00, 9.98 } }, new double[] { 32, 23, 33, 31 }); PointVectorValuePair optimum2 = optimizer.optimize(new MaxEval(100), problem2.getModelFunction(), problem2.getModelFunctionJacobian(), problem2.getTarget(), new Weight(new double[] { 1, 1, 1, 1 }), new InitialGuess(new double[] { 0, 1, 2, 3 })); Assert.assertEquals(0, optimizer.getRMS(), 1e-10); Assert.assertEquals(-81, optimum2.getPoint()[0], 1e-8); Assert.assertEquals(137, optimum2.getPoint()[1], 1e-8); Assert.assertEquals(-34, optimum2.getPoint()[2], 1e-8); Assert.assertEquals( 22, optimum2.getPoint()[3], 1e-8); } @Test public void testMoreEstimatedParametersSimple() { LinearProblem problem = new LinearProblem(new double[][] { { 3, 2, 0, 0 }, { 0, 1, -1, 1 }, { 2, 0, 1, 0 } }, new double[] { 7, 3, 5 }); AbstractLeastSquaresOptimizer optimizer = createOptimizer(); optimizer.optimize(new MaxEval(100), problem.getModelFunction(), problem.getModelFunctionJacobian(), problem.getTarget(), new Weight(new double[] { 1, 1, 1 }), new InitialGuess(new double[] { 7, 6, 5, 4 })); Assert.assertEquals(0, optimizer.getRMS(), 1e-10); } @Test public void testMoreEstimatedParametersUnsorted() { LinearProblem problem = new LinearProblem(new double[][] { { 1, 1, 0, 0, 0, 0 }, { 0, 0, 1, 1, 1, 0 }, { 0, 0, 0, 0, 1, -1 }, { 0, 0, -1, 1, 0, 1 }, { 0, 0, 0, -1, 1, 0 } }, new double[] { 3, 12, -1, 7, 1 }); AbstractLeastSquaresOptimizer optimizer = createOptimizer(); PointVectorValuePair optimum = optimizer.optimize(new MaxEval(100), problem.getModelFunction(), problem.getModelFunctionJacobian(), problem.getTarget(), new Weight(new double[] { 1, 1, 1, 1, 1 }), new InitialGuess(new double[] { 2, 2, 2, 2, 2, 2 })); Assert.assertEquals(0, optimizer.getRMS(), 1e-10); Assert.assertEquals(3, optimum.getPointRef()[2], 1e-10); Assert.assertEquals(4, optimum.getPointRef()[3], 1e-10); Assert.assertEquals(5, optimum.getPointRef()[4], 1e-10); Assert.assertEquals(6, optimum.getPointRef()[5], 1e-10); } @Test public void testRedundantEquations() { LinearProblem problem = new LinearProblem(new double[][] { { 1, 1 }, { 1, -1 }, { 1, 3 } }, new double[] { 3, 1, 5 }); AbstractLeastSquaresOptimizer optimizer = createOptimizer(); PointVectorValuePair optimum = optimizer.optimize(new MaxEval(100), problem.getModelFunction(), problem.getModelFunctionJacobian(), problem.getTarget(), new Weight(new double[] { 1, 1, 1 }), new InitialGuess(new double[] { 1, 1 })); Assert.assertEquals(0, optimizer.getRMS(), 1e-10); Assert.assertEquals(2, optimum.getPointRef()[0], 1e-10); Assert.assertEquals(1, optimum.getPointRef()[1], 1e-10); } @Test public void testInconsistentEquations() { LinearProblem problem = new LinearProblem(new double[][] { { 1, 1 }, { 1, -1 }, { 1, 3 } }, new double[] { 3, 1, 4 }); AbstractLeastSquaresOptimizer optimizer = createOptimizer(); optimizer.optimize(new MaxEval(100), problem.getModelFunction(), problem.getModelFunctionJacobian(), problem.getTarget(), new Weight(new double[] { 1, 1, 1 }), new InitialGuess(new double[] { 1, 1 })); Assert.assertTrue(optimizer.getRMS() > 0.1); } @Test(expected=DimensionMismatchException.class) public void testInconsistentSizes1() { LinearProblem problem = new LinearProblem(new double[][] { { 1, 0 }, { 0, 1 } }, new double[] { -1, 1 }); AbstractLeastSquaresOptimizer optimizer = createOptimizer(); PointVectorValuePair optimum = optimizer.optimize(new MaxEval(100), problem.getModelFunction(), problem.getModelFunctionJacobian(), problem.getTarget(), new Weight(new double[] { 1, 1 }), new InitialGuess(new double[] { 0, 0 })); Assert.assertEquals(0, optimizer.getRMS(), 1e-10); Assert.assertEquals(-1, optimum.getPoint()[0], 1e-10); Assert.assertEquals(1, optimum.getPoint()[1], 1e-10); optimizer.optimize(new MaxEval(100), problem.getModelFunction(), problem.getModelFunctionJacobian(), problem.getTarget(), new Weight(new double[] { 1 }), new InitialGuess(new double[] { 0, 0 })); } @Test(expected=DimensionMismatchException.class) public void testInconsistentSizes2() { LinearProblem problem = new LinearProblem(new double[][] { { 1, 0 }, { 0, 1 } }, new double[] { -1, 1 }); AbstractLeastSquaresOptimizer optimizer = createOptimizer(); PointVectorValuePair optimum = optimizer.optimize(new MaxEval(100), problem.getModelFunction(), problem.getModelFunctionJacobian(), problem.getTarget(), new Weight(new double[] { 1, 1 }), new InitialGuess(new double[] { 0, 0 })); Assert.assertEquals(0, optimizer.getRMS(), 1e-10); Assert.assertEquals(-1, optimum.getPoint()[0], 1e-10); Assert.assertEquals(1, optimum.getPoint()[1], 1e-10); optimizer.optimize(new MaxEval(100), problem.getModelFunction(), problem.getModelFunctionJacobian(), new Target(new double[] { 1 }), new Weight(new double[] { 1 }), new InitialGuess(new double[] { 0, 0 })); } @Test public void testCircleFitting() { CircleVectorial circle = new CircleVectorial(); circle.addPoint( 30, 68); circle.addPoint( 50, -6); circle.addPoint(110, -20); circle.addPoint( 35, 15); circle.addPoint( 45, 97); AbstractLeastSquaresOptimizer optimizer = createOptimizer(); PointVectorValuePair optimum = optimizer.optimize(new MaxEval(100), circle.getModelFunction(), circle.getModelFunctionJacobian(), new Target(new double[] { 0, 0, 0, 0, 0 }), new Weight(new double[] { 1, 1, 1, 1, 1 }), new InitialGuess(new double[] { 98.680, 47.345 })); Assert.assertTrue(optimizer.getEvaluations() < 10); double rms = optimizer.getRMS(); Assert.assertEquals(1.768262623567235, FastMath.sqrt(circle.getN()) * rms, 1e-10); Vector2D center = new Vector2D(optimum.getPointRef()[0], optimum.getPointRef()[1]); Assert.assertEquals(69.96016176931406, circle.getRadius(center), 1e-6); Assert.assertEquals(96.07590211815305, center.getX(), 1e-6); Assert.assertEquals(48.13516790438953, center.getY(), 1e-6); double[][] cov = optimizer.computeCovariances(optimum.getPoint(), 1e-14); Assert.assertEquals(1.839, cov[0][0], 0.001); Assert.assertEquals(0.731, cov[0][1], 0.001); Assert.assertEquals(cov[0][1], cov[1][0], 1e-14); Assert.assertEquals(0.786, cov[1][1], 0.001); // add perfect measurements and check errors are reduced double r = circle.getRadius(center); for (double d= 0; d < 2 * FastMath.PI; d += 0.01) { circle.addPoint(center.getX() + r * FastMath.cos(d), center.getY() + r * FastMath.sin(d)); } double[] target = new double[circle.getN()]; Arrays.fill(target, 0); double[] weights = new double[circle.getN()]; Arrays.fill(weights, 2); optimum = optimizer.optimize(new MaxEval(100), circle.getModelFunction(), circle.getModelFunctionJacobian(), new Target(target), new Weight(weights), new InitialGuess(new double[] { 98.680, 47.345 })); cov = optimizer.computeCovariances(optimum.getPoint(), 1e-14); Assert.assertEquals(0.0016, cov[0][0], 0.001); Assert.assertEquals(3.2e-7, cov[0][1], 1e-9); Assert.assertEquals(cov[0][1], cov[1][0], 1e-14); Assert.assertEquals(0.0016, cov[1][1], 0.001); } @Test public void testCircleFittingBadInit() { CircleVectorial circle = new CircleVectorial(); double[][] points = circlePoints; double[] target = new double[points.length]; Arrays.fill(target, 0); double[] weights = new double[points.length]; Arrays.fill(weights, 2); for (int i = 0; i < points.length; ++i) { circle.addPoint(points[i][0], points[i][1]); } AbstractLeastSquaresOptimizer optimizer = createOptimizer(); PointVectorValuePair optimum = optimizer.optimize(new MaxEval(100), circle.getModelFunction(), circle.getModelFunctionJacobian(), new Target(target), new Weight(weights), new InitialGuess(new double[] { -12, -12 })); Vector2D center = new Vector2D(optimum.getPointRef()[0], optimum.getPointRef()[1]); Assert.assertTrue(optimizer.getEvaluations() < 25); Assert.assertEquals( 0.043, optimizer.getRMS(), 1e-3); Assert.assertEquals( 0.292235, circle.getRadius(center), 1e-6); Assert.assertEquals(-0.151738, center.getX(), 1e-6); Assert.assertEquals( 0.2075001, center.getY(), 1e-6); } @Test public void testCircleFittingGoodInit() { CircleVectorial circle = new CircleVectorial(); double[][] points = circlePoints; double[] target = new double[points.length]; Arrays.fill(target, 0); double[] weights = new double[points.length]; Arrays.fill(weights, 2); for (int i = 0; i < points.length; ++i) { circle.addPoint(points[i][0], points[i][1]); } AbstractLeastSquaresOptimizer optimizer = createOptimizer(); PointVectorValuePair optimum = optimizer.optimize(new MaxEval(100), circle.getModelFunction(), circle.getModelFunctionJacobian(), new Target(target), new Weight(weights), new InitialGuess(new double[] { 0, 0 })); Assert.assertEquals(-0.1517383071957963, optimum.getPointRef()[0], 1e-6); Assert.assertEquals(0.2074999736353867, optimum.getPointRef()[1], 1e-6); Assert.assertEquals(0.04268731682389561, optimizer.getRMS(), 1e-8); } private final double[][] circlePoints = new double[][] { {-0.312967, 0.072366}, {-0.339248, 0.132965}, {-0.379780, 0.202724}, {-0.390426, 0.260487}, {-0.361212, 0.328325}, {-0.346039, 0.392619}, {-0.280579, 0.444306}, {-0.216035, 0.470009}, {-0.149127, 0.493832}, {-0.075133, 0.483271}, {-0.007759, 0.452680}, { 0.060071, 0.410235}, { 0.103037, 0.341076}, { 0.118438, 0.273884}, { 0.131293, 0.192201}, { 0.115869, 0.129797}, { 0.072223, 0.058396}, { 0.022884, 0.000718}, {-0.053355, -0.020405}, {-0.123584, -0.032451}, {-0.216248, -0.032862}, {-0.278592, -0.005008}, {-0.337655, 0.056658}, {-0.385899, 0.112526}, {-0.405517, 0.186957}, {-0.415374, 0.262071}, {-0.387482, 0.343398}, {-0.347322, 0.397943}, {-0.287623, 0.458425}, {-0.223502, 0.475513}, {-0.135352, 0.478186}, {-0.061221, 0.483371}, { 0.003711, 0.422737}, { 0.065054, 0.375830}, { 0.108108, 0.297099}, { 0.123882, 0.222850}, { 0.117729, 0.134382}, { 0.085195, 0.056820}, { 0.029800, -0.019138}, {-0.027520, -0.072374}, {-0.102268, -0.091555}, {-0.200299, -0.106578}, {-0.292731, -0.091473}, {-0.356288, -0.051108}, {-0.420561, 0.014926}, {-0.471036, 0.074716}, {-0.488638, 0.182508}, {-0.485990, 0.254068}, {-0.463943, 0.338438}, {-0.406453, 0.404704}, {-0.334287, 0.466119}, {-0.254244, 0.503188}, {-0.161548, 0.495769}, {-0.075733, 0.495560}, { 0.001375, 0.434937}, { 0.082787, 0.385806}, { 0.115490, 0.323807}, { 0.141089, 0.223450}, { 0.138693, 0.131703}, { 0.126415, 0.049174}, { 0.066518, -0.010217}, {-0.005184, -0.070647}, {-0.080985, -0.103635}, {-0.177377, -0.116887}, {-0.260628, -0.100258}, {-0.335756, -0.056251}, {-0.405195, -0.000895}, {-0.444937, 0.085456}, {-0.484357, 0.175597}, {-0.472453, 0.248681}, {-0.438580, 0.347463}, {-0.402304, 0.422428}, {-0.326777, 0.479438}, {-0.247797, 0.505581}, {-0.152676, 0.519380}, {-0.071754, 0.516264}, { 0.015942, 0.472802}, { 0.076608, 0.419077}, { 0.127673, 0.330264}, { 0.159951, 0.262150}, { 0.153530, 0.172681}, { 0.140653, 0.089229}, { 0.078666, 0.024981}, { 0.023807, -0.037022}, {-0.048837, -0.077056}, {-0.127729, -0.075338}, {-0.221271, -0.067526} }; public void doTestStRD(final StatisticalReferenceDataset dataset, final double errParams, final double errParamsSd) { final AbstractLeastSquaresOptimizer optimizer = createOptimizer(); final double[] w = new double[dataset.getNumObservations()]; Arrays.fill(w, 1); final double[][] data = dataset.getData(); final double[] initial = dataset.getStartingPoint(0); final StatisticalReferenceDataset.LeastSquaresProblem problem = dataset.getLeastSquaresProblem(); final PointVectorValuePair optimum = optimizer.optimize(new MaxEval(100), problem.getModelFunction(), problem.getModelFunctionJacobian(), new Target(data[1]), new Weight(w), new InitialGuess(initial)); final double[] actual = optimum.getPoint(); for (int i = 0; i < actual.length; i++) { double expected = dataset.getParameter(i); double delta = FastMath.abs(errParams * expected); Assert.assertEquals(dataset.getName() + ", param #" + i, expected, actual[i], delta); } } @Test public void testKirby2() throws IOException { doTestStRD(StatisticalReferenceDatasetFactory.createKirby2(), 1E-7, 1E-7); } @Test public void testHahn1() throws IOException { doTestStRD(StatisticalReferenceDatasetFactory.createHahn1(), 1E-7, 1E-4); } static class LinearProblem { private final RealMatrix factors; private final double[] target; public LinearProblem(double[][] factors, double[] target) { this.factors = new BlockRealMatrix(factors); this.target = target; } public Target getTarget() { return new Target(target); } public ModelFunction getModelFunction() { return new ModelFunction(new MultivariateVectorFunction() { public double[] value(double[] params) { return factors.operate(params); } }); } public ModelFunctionJacobian getModelFunctionJacobian() { return new ModelFunctionJacobian(new MultivariateMatrixFunction() { public double[][] value(double[] params) { return factors.getData(); } }); } } } ././@LongLink100644 0 0 162 12126630646 10260 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/optim/nonlinear/vector/jacobian/CircleVectorial.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/optim/nonlinear/vector/jacobian/CircleV100644 1750 1750 7142 12126627671 32224 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.optim.nonlinear.vector.jacobian; import java.util.ArrayList; import org.apache.commons.math3.analysis.MultivariateVectorFunction; import org.apache.commons.math3.analysis.MultivariateMatrixFunction; import org.apache.commons.math3.geometry.euclidean.twod.Vector2D; import org.apache.commons.math3.optim.nonlinear.vector.ModelFunction; import org.apache.commons.math3.optim.nonlinear.vector.ModelFunctionJacobian; /** * Class used in the tests. */ class CircleVectorial { private ArrayList points; public CircleVectorial() { points = new ArrayList(); } public void addPoint(double px, double py) { points.add(new Vector2D(px, py)); } public int getN() { return points.size(); } public double getRadius(Vector2D center) { double r = 0; for (Vector2D point : points) { r += point.distance(center); } return r / points.size(); } public ModelFunction getModelFunction() { return new ModelFunction(new MultivariateVectorFunction() { public double[] value(double[] params) { Vector2D center = new Vector2D(params[0], params[1]); double radius = getRadius(center); double[] residuals = new double[points.size()]; for (int i = 0; i < residuals.length; i++) { residuals[i] = points.get(i).distance(center) - radius; } return residuals; } }); } public ModelFunctionJacobian getModelFunctionJacobian() { return new ModelFunctionJacobian(new MultivariateMatrixFunction() { public double[][] value(double[] params) { final int n = points.size(); final Vector2D center = new Vector2D(params[0], params[1]); double dRdX = 0; double dRdY = 0; for (Vector2D pk : points) { double dk = pk.distance(center); dRdX += (center.getX() - pk.getX()) / dk; dRdY += (center.getY() - pk.getY()) / dk; } dRdX /= n; dRdY /= n; // Jacobian of the radius residuals. double[][] jacobian = new double[n][2]; for (int i = 0; i < n; i++) { final Vector2D pi = points.get(i); final double di = pi.distance(center); jacobian[i][0] = (center.getX() - pi.getX()) / di - dRdX; jacobian[i][1] = (center.getY() - pi.getY()) / di - dRdY; } return jacobian; } }); } } ././@LongLink100644 0 0 205 12126630646 10256 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/optim/nonlinear/vector/jacobian/StatisticalReferenceDatasetFactory.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/optim/nonlinear/vector/jacobian/Statist100644 1750 1750 16601 12126627671 32350 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.optim.nonlinear.vector.jacobian; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import org.apache.commons.math3.util.FastMath; /** * A factory to create instances of {@link StatisticalReferenceDataset} from * available resources. */ public class StatisticalReferenceDatasetFactory { private StatisticalReferenceDatasetFactory() { // Do nothing } /** * Creates a new buffered reader from the specified resource name. * * @param name the name of the resource * @return a buffered reader * @throws IOException if an I/O error occured */ public static BufferedReader createBufferedReaderFromResource(final String name) throws IOException { final InputStream resourceAsStream; resourceAsStream = StatisticalReferenceDatasetFactory.class .getResourceAsStream(name); if (resourceAsStream == null) { throw new IOException("could not find resource " + name); } return new BufferedReader(new InputStreamReader(resourceAsStream)); } public static StatisticalReferenceDataset createKirby2() throws IOException { final BufferedReader in = createBufferedReaderFromResource("Kirby2.dat"); StatisticalReferenceDataset dataset = null; try { dataset = new StatisticalReferenceDataset(in) { @Override public double getModelValue(final double x, final double[] a) { final double p = a[0] + x * (a[1] + x * a[2]); final double q = 1.0 + x * (a[3] + x * a[4]); return p / q; } @Override public double[] getModelDerivatives(final double x, final double[] a) { final double[] dy = new double[5]; final double p = a[0] + x * (a[1] + x * a[2]); final double q = 1.0 + x * (a[3] + x * a[4]); dy[0] = 1.0 / q; dy[1] = x / q; dy[2] = x * dy[1]; dy[3] = -x * p / (q * q); dy[4] = x * dy[3]; return dy; } }; } finally { in.close(); } return dataset; } public static StatisticalReferenceDataset createHahn1() throws IOException { final BufferedReader in = createBufferedReaderFromResource("Hahn1.dat"); StatisticalReferenceDataset dataset = null; try { dataset = new StatisticalReferenceDataset(in) { @Override public double getModelValue(final double x, final double[] a) { final double p = a[0] + x * (a[1] + x * (a[2] + x * a[3])); final double q = 1.0 + x * (a[4] + x * (a[5] + x * a[6])); return p / q; } @Override public double[] getModelDerivatives(final double x, final double[] a) { final double[] dy = new double[7]; final double p = a[0] + x * (a[1] + x * (a[2] + x * a[3])); final double q = 1.0 + x * (a[4] + x * (a[5] + x * a[6])); dy[0] = 1.0 / q; dy[1] = x * dy[0]; dy[2] = x * dy[1]; dy[3] = x * dy[2]; dy[4] = -x * p / (q * q); dy[5] = x * dy[4]; dy[6] = x * dy[5]; return dy; } }; } finally { in.close(); } return dataset; } public static StatisticalReferenceDataset createMGH17() throws IOException { final BufferedReader in = createBufferedReaderFromResource("MGH17.dat"); StatisticalReferenceDataset dataset = null; try { dataset = new StatisticalReferenceDataset(in) { @Override public double getModelValue(final double x, final double[] a) { return a[0] + a[1] * FastMath.exp(-a[3] * x) + a[2] * FastMath.exp(-a[4] * x); } @Override public double[] getModelDerivatives(final double x, final double[] a) { final double[] dy = new double[5]; dy[0] = 1.0; dy[1] = FastMath.exp(-x * a[3]); dy[2] = FastMath.exp(-x * a[4]); dy[3] = -x * a[1] * dy[1]; dy[4] = -x * a[2] * dy[2]; return dy; } }; } finally { in.close(); } return dataset; } public static StatisticalReferenceDataset createLanczos1() throws IOException { final BufferedReader in = createBufferedReaderFromResource("Lanczos1.dat"); StatisticalReferenceDataset dataset = null; try { dataset = new StatisticalReferenceDataset(in) { @Override public double getModelValue(final double x, final double[] a) { System.out.println(a[0]+", "+a[1]+", "+a[2]+", "+a[3]+", "+a[4]+", "+a[5]); return a[0] * FastMath.exp(-a[3] * x) + a[1] * FastMath.exp(-a[4] * x) + a[2] * FastMath.exp(-a[5] * x); } @Override public double[] getModelDerivatives(final double x, final double[] a) { final double[] dy = new double[6]; dy[0] = FastMath.exp(-x * a[3]); dy[1] = FastMath.exp(-x * a[4]); dy[2] = FastMath.exp(-x * a[5]); dy[3] = -x * a[0] * dy[0]; dy[4] = -x * a[1] * dy[1]; dy[5] = -x * a[2] * dy[2]; return dy; } }; } finally { in.close(); } return dataset; } /** * Returns an array with all available reference datasets. * * @return the array of datasets * @throws IOException if an I/O error occurs */ public StatisticalReferenceDataset[] createAll() throws IOException { return new StatisticalReferenceDataset[] { createKirby2(), createMGH17() }; } } ././@LongLink100644 0 0 166 12126630646 10264 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/optim/nonlinear/vector/jacobian/StraightLineProblem.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/optim/nonlinear/vector/jacobian/Straigh100644 1750 1750 11745 12126627671 32322 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.optim.nonlinear.vector.jacobian; import java.util.ArrayList; import org.apache.commons.math3.analysis.MultivariateVectorFunction; import org.apache.commons.math3.analysis.MultivariateMatrixFunction; import org.apache.commons.math3.analysis.UnivariateFunction; import org.apache.commons.math3.stat.regression.SimpleRegression; import org.apache.commons.math3.optim.nonlinear.vector.ModelFunction; import org.apache.commons.math3.optim.nonlinear.vector.ModelFunctionJacobian; /** * Class that models a straight line defined as {@code y = a x + b}. * The parameters of problem are: *
                        *
                      • {@code a}
                      • *
                      • {@code b}
                      • *
                      * The model functions are: *
                        *
                      • for each pair (a, b), the y-coordinate of the line.
                      • *
                      */ class StraightLineProblem { /** Cloud of points assumed to be fitted by a straight line. */ private final ArrayList points; /** Error (on the y-coordinate of the points). */ private final double sigma; /** * @param error Assumed error for the y-coordinate. */ public StraightLineProblem(double error) { points = new ArrayList(); sigma = error; } public void addPoint(double px, double py) { points.add(new double[] { px, py }); } /** * @return the list of x-coordinates. */ public double[] x() { final double[] v = new double[points.size()]; for (int i = 0; i < points.size(); i++) { final double[] p = points.get(i); v[i] = p[0]; // x-coordinate. } return v; } /** * @return the list of y-coordinates. */ public double[] y() { final double[] v = new double[points.size()]; for (int i = 0; i < points.size(); i++) { final double[] p = points.get(i); v[i] = p[1]; // y-coordinate. } return v; } public double[] target() { return y(); } public double[] weight() { final double weight = 1 / (sigma * sigma); final double[] w = new double[points.size()]; for (int i = 0; i < points.size(); i++) { w[i] = weight; } return w; } public ModelFunction getModelFunction() { return new ModelFunction(new MultivariateVectorFunction() { public double[] value(double[] params) { final Model line = new Model(params[0], params[1]); final double[] model = new double[points.size()]; for (int i = 0; i < points.size(); i++) { final double[] p = points.get(i); model[i] = line.value(p[0]); } return model; } }); } public ModelFunctionJacobian getModelFunctionJacobian() { return new ModelFunctionJacobian(new MultivariateMatrixFunction() { public double[][] value(double[] point) { return jacobian(point); } }); } /** * Directly solve the linear problem, using the {@link SimpleRegression} * class. */ public double[] solve() { final SimpleRegression regress = new SimpleRegression(true); for (double[] d : points) { regress.addData(d[0], d[1]); } final double[] result = { regress.getSlope(), regress.getIntercept() }; return result; } private double[][] jacobian(double[] params) { final double[][] jacobian = new double[points.size()][2]; for (int i = 0; i < points.size(); i++) { final double[] p = points.get(i); // Partial derivative wrt "a". jacobian[i][0] = p[0]; // Partial derivative wrt "b". jacobian[i][1] = 1; } return jacobian; } /** * Linear function. */ public static class Model implements UnivariateFunction { final double a; final double b; public Model(double a, double b) { this.a = a; this.b = b; } public double value(double x) { return a * x + b; } } } ././@LongLink100644 0 0 176 12126630646 10265 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/optim/nonlinear/vector/jacobian/StatisticalReferenceDataset.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/optim/nonlinear/vector/jacobian/Statist100644 1750 1750 31361 12126627671 32350 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.optim.nonlinear.vector.jacobian; import java.io.BufferedReader; import java.io.IOException; import java.util.ArrayList; import org.apache.commons.math3.analysis.MultivariateVectorFunction; import org.apache.commons.math3.analysis.MultivariateMatrixFunction; import org.apache.commons.math3.optim.nonlinear.vector.ModelFunction; import org.apache.commons.math3.optim.nonlinear.vector.ModelFunctionJacobian; import org.apache.commons.math3.util.MathArrays; /** * This class gives access to the statistical reference datasets provided by the * NIST (available * here). * Instances of this class can be created by invocation of the * {@link StatisticalReferenceDatasetFactory}. */ public abstract class StatisticalReferenceDataset { /** The name of this dataset. */ private final String name; /** The total number of observations (data points). */ private final int numObservations; /** The total number of parameters. */ private final int numParameters; /** The total number of starting points for the optimizations. */ private final int numStartingPoints; /** The values of the predictor. */ private final double[] x; /** The values of the response. */ private final double[] y; /** * The starting values. {@code startingValues[j][i]} is the value of the * {@code i}-th parameter in the {@code j}-th set of starting values. */ private final double[][] startingValues; /** The certified values of the parameters. */ private final double[] a; /** The certified values of the standard deviation of the parameters. */ private final double[] sigA; /** The certified value of the residual sum of squares. */ private double residualSumOfSquares; /** The least-squares problem. */ private final LeastSquaresProblem problem; /** * Creates a new instance of this class from the specified data file. The * file must follow the StRD format. * * @param in the data file * @throws IOException if an I/O error occurs */ public StatisticalReferenceDataset(final BufferedReader in) throws IOException { final ArrayList lines = new ArrayList(); for (String line = in.readLine(); line != null; line = in.readLine()) { lines.add(line); } int[] index = findLineNumbers("Data", lines); if (index == null) { throw new AssertionError("could not find line indices for data"); } this.numObservations = index[1] - index[0] + 1; this.x = new double[this.numObservations]; this.y = new double[this.numObservations]; for (int i = 0; i < this.numObservations; i++) { final String line = lines.get(index[0] + i - 1); final String[] tokens = line.trim().split(" ++"); // Data columns are in reverse order!!! this.y[i] = Double.parseDouble(tokens[0]); this.x[i] = Double.parseDouble(tokens[1]); } index = findLineNumbers("Starting Values", lines); if (index == null) { throw new AssertionError( "could not find line indices for starting values"); } this.numParameters = index[1] - index[0] + 1; double[][] start = null; this.a = new double[numParameters]; this.sigA = new double[numParameters]; for (int i = 0; i < numParameters; i++) { final String line = lines.get(index[0] + i - 1); final String[] tokens = line.trim().split(" ++"); if (start == null) { start = new double[tokens.length - 4][numParameters]; } for (int j = 2; j < tokens.length - 2; j++) { start[j - 2][i] = Double.parseDouble(tokens[j]); } this.a[i] = Double.parseDouble(tokens[tokens.length - 2]); this.sigA[i] = Double.parseDouble(tokens[tokens.length - 1]); } if (start == null) { throw new IOException("could not find starting values"); } this.numStartingPoints = start.length; this.startingValues = start; double dummyDouble = Double.NaN; String dummyString = null; for (String line : lines) { if (line.contains("Dataset Name:")) { dummyString = line .substring(line.indexOf("Dataset Name:") + 13, line.indexOf("(")).trim(); } if (line.contains("Residual Sum of Squares")) { final String[] tokens = line.split(" ++"); dummyDouble = Double.parseDouble(tokens[4].trim()); } } if (Double.isNaN(dummyDouble)) { throw new IOException( "could not find certified value of residual sum of squares"); } this.residualSumOfSquares = dummyDouble; if (dummyString == null) { throw new IOException("could not find dataset name"); } this.name = dummyString; this.problem = new LeastSquaresProblem(); } class LeastSquaresProblem { public ModelFunction getModelFunction() { return new ModelFunction(new MultivariateVectorFunction() { public double[] value(final double[] a) { final int n = getNumObservations(); final double[] yhat = new double[n]; for (int i = 0; i < n; i++) { yhat[i] = getModelValue(getX(i), a); } return yhat; } }); } public ModelFunctionJacobian getModelFunctionJacobian() { return new ModelFunctionJacobian(new MultivariateMatrixFunction() { public double[][] value(final double[] a) throws IllegalArgumentException { final int n = getNumObservations(); final double[][] j = new double[n][]; for (int i = 0; i < n; i++) { j[i] = getModelDerivatives(getX(i), a); } return j; } }); } } /** * Returns the name of this dataset. * * @return the name of the dataset */ public String getName() { return name; } /** * Returns the total number of observations (data points). * * @return the number of observations */ public int getNumObservations() { return numObservations; } /** * Returns a copy of the data arrays. The data is laid out as follows
                    11. * {@code data[0][i] = x[i]},
                    12. {@code data[1][i] = y[i]},
                    13. * * @return the array of data points. */ public double[][] getData() { return new double[][] { MathArrays.copyOf(x), MathArrays.copyOf(y) }; } /** * Returns the x-value of the {@code i}-th data point. * * @param i the index of the data point * @return the x-value */ public double getX(final int i) { return x[i]; } /** * Returns the y-value of the {@code i}-th data point. * * @param i the index of the data point * @return the y-value */ public double getY(final int i) { return y[i]; } /** * Returns the total number of parameters. * * @return the number of parameters */ public int getNumParameters() { return numParameters; } /** * Returns the certified values of the paramters. * * @return the values of the parameters */ public double[] getParameters() { return MathArrays.copyOf(a); } /** * Returns the certified value of the {@code i}-th parameter. * * @param i the index of the parameter * @return the value of the parameter */ public double getParameter(final int i) { return a[i]; } /** * Reurns the certified values of the standard deviations of the parameters. * * @return the standard deviations of the parameters */ public double[] getParametersStandardDeviations() { return MathArrays.copyOf(sigA); } /** * Returns the certified value of the standard deviation of the {@code i}-th * parameter. * * @param i the index of the parameter * @return the standard deviation of the parameter */ public double getParameterStandardDeviation(final int i) { return sigA[i]; } /** * Returns the certified value of the residual sum of squares. * * @return the residual sum of squares */ public double getResidualSumOfSquares() { return residualSumOfSquares; } /** * Returns the total number of starting points (initial guesses for the * optimization process). * * @return the number of starting points */ public int getNumStartingPoints() { return numStartingPoints; } /** * Returns the {@code i}-th set of initial values of the parameters. * * @param i the index of the starting point * @return the starting point */ public double[] getStartingPoint(final int i) { return MathArrays.copyOf(startingValues[i]); } /** * Returns the least-squares problem corresponding to fitting the model to * the specified data. * * @return the least-squares problem */ public LeastSquaresProblem getLeastSquaresProblem() { return problem; } /** * Returns the value of the model for the specified values of the predictor * variable and the parameters. * * @param x the predictor variable * @param a the parameters * @return the value of the model */ public abstract double getModelValue(final double x, final double[] a); /** * Returns the values of the partial derivatives of the model with respect * to the parameters. * * @param x the predictor variable * @param a the parameters * @return the partial derivatives */ public abstract double[] getModelDerivatives(final double x, final double[] a); /** *

                      * Parses the specified text lines, and extracts the indices of the first * and last lines of the data defined by the specified {@code key}. This key * must be one of *

                      *
                        *
                      • {@code "Starting Values"},
                      • *
                      • {@code "Certified Values"},
                      • *
                      • {@code "Data"}.
                      • *
                      *

                      * In the NIST data files, the line indices are separated by the keywords * {@code "lines"} and {@code "to"}. *

                      * * @param lines the line of text to be parsed * @return an array of two {@code int}s. First value is the index of the * first line, second value is the index of the last line. * {@code null} if the line could not be parsed. */ private static int[] findLineNumbers(final String key, final Iterable lines) { for (String text : lines) { boolean flag = text.contains(key) && text.contains("lines") && text.contains("to") && text.contains(")"); if (flag) { final int[] numbers = new int[2]; final String from = text.substring(text.indexOf("lines") + 5, text.indexOf("to")); numbers[0] = Integer.parseInt(from.trim()); final String to = text.substring(text.indexOf("to") + 2, text.indexOf(")")); numbers[1] = Integer.parseInt(to.trim()); return numbers; } } return null; } } ././@LongLink100644 0 0 156 12126630646 10263 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/optim/nonlinear/vector/jacobian/MinpackTest.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/optim/nonlinear/vector/jacobian/Minpack100644 1750 1750 202166 12126627671 32322 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.optim.nonlinear.vector.jacobian; import java.io.Serializable; import java.util.Arrays; import org.apache.commons.math3.exception.TooManyEvaluationsException; import org.apache.commons.math3.analysis.MultivariateVectorFunction; import org.apache.commons.math3.analysis.MultivariateMatrixFunction; import org.apache.commons.math3.optim.PointVectorValuePair; import org.apache.commons.math3.optim.InitialGuess; import org.apache.commons.math3.optim.MaxEval; import org.apache.commons.math3.optim.nonlinear.vector.Target; import org.apache.commons.math3.optim.nonlinear.vector.Weight; import org.apache.commons.math3.optim.nonlinear.vector.ModelFunction; import org.apache.commons.math3.optim.nonlinear.vector.ModelFunctionJacobian; import org.apache.commons.math3.util.FastMath; import org.junit.Assert; import org.junit.Test; /** *

                      Some of the unit tests are re-implementations of the MINPACK file17 and file22 test files. * The redistribution policy for MINPACK is available here, for * convenience, it is reproduced below.

                      * * * *
                      * Minpack Copyright Notice (1999) University of Chicago. * All rights reserved *
                      * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: *
                        *
                      1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer.
                      2. *
                      3. Redistributions in binary form must reproduce the above * copyright notice, this list of conditions and the following * disclaimer in the documentation and/or other materials provided * with the distribution.
                      4. *
                      5. The end-user documentation included with the redistribution, if any, * must include the following acknowledgment: * This product includes software developed by the University of * Chicago, as Operator of Argonne National Laboratory. * Alternately, this acknowledgment may appear in the software itself, * if and wherever such third-party acknowledgments normally appear.
                      6. *
                      7. WARRANTY DISCLAIMER. THE SOFTWARE IS SUPPLIED "AS IS" * WITHOUT WARRANTY OF ANY KIND. THE COPYRIGHT HOLDER, THE * UNITED STATES, THE UNITED STATES DEPARTMENT OF ENERGY, AND * THEIR EMPLOYEES: (1) DISCLAIM ANY WARRANTIES, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO ANY IMPLIED WARRANTIES * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE * OR NON-INFRINGEMENT, (2) DO NOT ASSUME ANY LEGAL LIABILITY * OR RESPONSIBILITY FOR THE ACCURACY, COMPLETENESS, OR * USEFULNESS OF THE SOFTWARE, (3) DO NOT REPRESENT THAT USE OF * THE SOFTWARE WOULD NOT INFRINGE PRIVATELY OWNED RIGHTS, (4) * DO NOT WARRANT THAT THE SOFTWARE WILL FUNCTION * UNINTERRUPTED, THAT IT IS ERROR-FREE OR THAT ANY ERRORS WILL * BE CORRECTED.
                      8. *
                      9. LIMITATION OF LIABILITY. IN NO EVENT WILL THE COPYRIGHT * HOLDER, THE UNITED STATES, THE UNITED STATES DEPARTMENT OF * ENERGY, OR THEIR EMPLOYEES: BE LIABLE FOR ANY INDIRECT, * INCIDENTAL, CONSEQUENTIAL, SPECIAL OR PUNITIVE DAMAGES OF * ANY KIND OR NATURE, INCLUDING BUT NOT LIMITED TO LOSS OF * PROFITS OR LOSS OF DATA, FOR ANY REASON WHATSOEVER, WHETHER * SUCH LIABILITY IS ASSERTED ON THE BASIS OF CONTRACT, TORT * (INCLUDING NEGLIGENCE OR STRICT LIABILITY), OR OTHERWISE, * EVEN IF ANY OF SAID PARTIES HAS BEEN WARNED OF THE * POSSIBILITY OF SUCH LOSS OR DAMAGES.
                      10. *
                        * @author Argonne National Laboratory. MINPACK project. March 1980 (original fortran minpack tests) * @author Burton S. Garbow (original fortran minpack tests) * @author Kenneth E. Hillstrom (original fortran minpack tests) * @author Jorge J. More (original fortran minpack tests) * @author Luc Maisonobe (non-minpack tests and minpack tests Java translation) */ public class MinpackTest { @Test public void testMinpackLinearFullRank() { minpackTest(new LinearFullRankFunction(10, 5, 1.0, 5.0, 2.23606797749979), false); minpackTest(new LinearFullRankFunction(50, 5, 1.0, 8.06225774829855, 6.70820393249937), false); } @Test public void testMinpackLinearRank1() { minpackTest(new LinearRank1Function(10, 5, 1.0, 291.521868819476, 1.4638501094228), false); minpackTest(new LinearRank1Function(50, 5, 1.0, 3101.60039334535, 3.48263016573496), false); } @Test public void testMinpackLinearRank1ZeroColsAndRows() { minpackTest(new LinearRank1ZeroColsAndRowsFunction(10, 5, 1.0), false); minpackTest(new LinearRank1ZeroColsAndRowsFunction(50, 5, 1.0), false); } @Test public void testMinpackRosenbrok() { minpackTest(new RosenbrockFunction(new double[] { -1.2, 1.0 }, FastMath.sqrt(24.2)), false); minpackTest(new RosenbrockFunction(new double[] { -12.0, 10.0 }, FastMath.sqrt(1795769.0)), false); minpackTest(new RosenbrockFunction(new double[] { -120.0, 100.0 }, 11.0 * FastMath.sqrt(169000121.0)), false); } @Test public void testMinpackHelicalValley() { minpackTest(new HelicalValleyFunction(new double[] { -1.0, 0.0, 0.0 }, 50.0), false); minpackTest(new HelicalValleyFunction(new double[] { -10.0, 0.0, 0.0 }, 102.95630140987), false); minpackTest(new HelicalValleyFunction(new double[] { -100.0, 0.0, 0.0}, 991.261822123701), false); } @Test public void testMinpackPowellSingular() { minpackTest(new PowellSingularFunction(new double[] { 3.0, -1.0, 0.0, 1.0 }, 14.6628782986152), false); minpackTest(new PowellSingularFunction(new double[] { 30.0, -10.0, 0.0, 10.0 }, 1270.9838708654), false); minpackTest(new PowellSingularFunction(new double[] { 300.0, -100.0, 0.0, 100.0 }, 126887.903284750), false); } @Test public void testMinpackFreudensteinRoth() { minpackTest(new FreudensteinRothFunction(new double[] { 0.5, -2.0 }, 20.0124960961895, 6.99887517584575, new double[] { 11.4124844654993, -0.896827913731509 }), false); minpackTest(new FreudensteinRothFunction(new double[] { 5.0, -20.0 }, 12432.833948863, 6.9988751744895, new double[] { 11.41300466147456, -0.896796038685959 }), false); minpackTest(new FreudensteinRothFunction(new double[] { 50.0, -200.0 }, 11426454.595762, 6.99887517242903, new double[] { 11.412781785788564, -0.8968051074920405 }), false); } @Test public void testMinpackBard() { minpackTest(new BardFunction(1.0, 6.45613629515967, 0.0906359603390466, new double[] { 0.0824105765758334, 1.1330366534715, 2.34369463894115 }), false); minpackTest(new BardFunction(10.0, 36.1418531596785, 4.17476870138539, new double[] { 0.840666673818329, -158848033.259565, -164378671.653535 }), false); minpackTest(new BardFunction(100.0, 384.114678637399, 4.17476870135969, new double[] { 0.840666673867645, -158946167.205518, -164464906.857771 }), false); } @Test public void testMinpackKowalikOsborne() { minpackTest(new KowalikOsborneFunction(new double[] { 0.25, 0.39, 0.415, 0.39 }, 0.0728915102882945, 0.017535837721129, new double[] { 0.192807810476249, 0.191262653354071, 0.123052801046931, 0.136053221150517 }), false); minpackTest(new KowalikOsborneFunction(new double[] { 2.5, 3.9, 4.15, 3.9 }, 2.97937007555202, 0.032052192917937, new double[] { 728675.473768287, -14.0758803129393, -32977797.7841797, -20571594.1977912 }), false); minpackTest(new KowalikOsborneFunction(new double[] { 25.0, 39.0, 41.5, 39.0 }, 29.9590617016037, 0.0175364017658228, new double[] { 0.192948328597594, 0.188053165007911, 0.122430604321144, 0.134575665392506 }), false); } @Test public void testMinpackMeyer() { minpackTest(new MeyerFunction(new double[] { 0.02, 4000.0, 250.0 }, 41153.4665543031, 9.37794514651874, new double[] { 0.00560963647102661, 6181.34634628659, 345.223634624144 }), false); minpackTest(new MeyerFunction(new double[] { 0.2, 40000.0, 2500.0 }, 4168216.89130846, 792.917871779501, new double[] { 1.42367074157994e-11, 33695.7133432541, 901.268527953801 }), true); } @Test public void testMinpackWatson() { minpackTest(new WatsonFunction(6, 0.0, 5.47722557505166, 0.0478295939097601, new double[] { -0.0157249615083782, 1.01243488232965, -0.232991722387673, 1.26043101102818, -1.51373031394421, 0.99299727291842 }), false); minpackTest(new WatsonFunction(6, 10.0, 6433.12578950026, 0.0478295939096951, new double[] { -0.0157251901386677, 1.01243485860105, -0.232991545843829, 1.26042932089163, -1.51372776706575, 0.99299573426328 }), false); minpackTest(new WatsonFunction(6, 100.0, 674256.040605213, 0.047829593911544, new double[] { -0.0157247019712586, 1.01243490925658, -0.232991922761641, 1.26043292929555, -1.51373320452707, 0.99299901922322 }), false); minpackTest(new WatsonFunction(9, 0.0, 5.47722557505166, 0.00118311459212420, new double[] { -0.153070644166722e-4, 0.999789703934597, 0.0147639634910978, 0.146342330145992, 1.00082109454817, -2.61773112070507, 4.10440313943354, -3.14361226236241, 1.05262640378759 }), false); minpackTest(new WatsonFunction(9, 10.0, 12088.127069307, 0.00118311459212513, new double[] { -0.153071334849279e-4, 0.999789703941234, 0.0147639629786217, 0.146342334818836, 1.00082107321386, -2.61773107084722, 4.10440307655564, -3.14361222178686, 1.05262639322589 }), false); minpackTest(new WatsonFunction(9, 100.0, 1269109.29043834, 0.00118311459212384, new double[] { -0.153069523352176e-4, 0.999789703958371, 0.0147639625185392, 0.146342341096326, 1.00082104729164, -2.61773101573645, 4.10440301427286, -3.14361218602503, 1.05262638516774 }), false); minpackTest(new WatsonFunction(12, 0.0, 5.47722557505166, 0.217310402535861e-4, new double[] { -0.660266001396382e-8, 1.00000164411833, -0.000563932146980154, 0.347820540050756, -0.156731500244233, 1.05281515825593, -3.24727109519451, 7.2884347837505, -10.271848098614, 9.07411353715783, -4.54137541918194, 1.01201187975044 }), false); minpackTest(new WatsonFunction(12, 10.0, 19220.7589790951, 0.217310402518509e-4, new double[] { -0.663710223017410e-8, 1.00000164411787, -0.000563932208347327, 0.347820540486998, -0.156731503955652, 1.05281517654573, -3.2472711515214, 7.28843489430665, -10.2718482369638, 9.07411364383733, -4.54137546533666, 1.01201188830857 }), false); minpackTest(new WatsonFunction(12, 100.0, 2018918.04462367, 0.217310402539845e-4, new double[] { -0.663806046485249e-8, 1.00000164411786, -0.000563932210324959, 0.347820540503588, -0.156731504091375, 1.05281517718031, -3.24727115337025, 7.28843489775302, -10.2718482410813, 9.07411364688464, -4.54137546660822, 1.0120118885369 }), false); } @Test public void testMinpackBox3Dimensional() { minpackTest(new Box3DimensionalFunction(10, new double[] { 0.0, 10.0, 20.0 }, 32.1115837449572), false); } @Test public void testMinpackJennrichSampson() { minpackTest(new JennrichSampsonFunction(10, new double[] { 0.3, 0.4 }, 64.5856498144943, 11.1517793413499, new double[] { // 0.2578330049, 0.257829976764542 0.2578199266368004, 0.25782997676455244 }), false); } @Test public void testMinpackBrownDennis() { minpackTest(new BrownDennisFunction(20, new double[] { 25.0, 5.0, -5.0, -1.0 }, 2815.43839161816, 292.954288244866, new double[] { -11.59125141003, 13.2024883984741, -0.403574643314272, 0.236736269844604 }), false); minpackTest(new BrownDennisFunction(20, new double[] { 250.0, 50.0, -50.0, -10.0 }, 555073.354173069, 292.954270581415, new double[] { -11.5959274272203, 13.2041866926242, -0.403417362841545, 0.236771143410386 }), false); minpackTest(new BrownDennisFunction(20, new double[] { 2500.0, 500.0, -500.0, -100.0 }, 61211252.2338581, 292.954306151134, new double[] { -11.5902596937374, 13.2020628854665, -0.403688070279258, 0.236665033746463 }), false); } @Test public void testMinpackChebyquad() { minpackTest(new ChebyquadFunction(1, 8, 1.0, 1.88623796907732, 1.88623796907732, new double[] { 0.5 }), false); minpackTest(new ChebyquadFunction(1, 8, 10.0, 5383344372.34005, 1.88424820499951, new double[] { 0.9817314924684 }), false); minpackTest(new ChebyquadFunction(1, 8, 100.0, 0.118088726698392e19, 1.88424820499347, new double[] { 0.9817314852934 }), false); minpackTest(new ChebyquadFunction(8, 8, 1.0, 0.196513862833975, 0.0593032355046727, new double[] { 0.0431536648587336, 0.193091637843267, 0.266328593812698, 0.499999334628884, 0.500000665371116, 0.733671406187302, 0.806908362156733, 0.956846335141266 }), false); minpackTest(new ChebyquadFunction(9, 9, 1.0, 0.16994993465202, 0.0, new double[] { 0.0442053461357828, 0.199490672309881, 0.23561910847106, 0.416046907892598, 0.5, 0.583953092107402, 0.764380891528940, 0.800509327690119, 0.955794653864217 }), false); minpackTest(new ChebyquadFunction(10, 10, 1.0, 0.183747831178711, 0.0806471004038253, new double[] { 0.0596202671753563, 0.166708783805937, 0.239171018813509, 0.398885290346268, 0.398883667870681, 0.601116332129320, 0.60111470965373, 0.760828981186491, 0.833291216194063, 0.940379732824644 }), false); } @Test public void testMinpackBrownAlmostLinear() { minpackTest(new BrownAlmostLinearFunction(10, 0.5, 16.5302162063499, 0.0, new double[] { 0.979430303349862, 0.979430303349862, 0.979430303349862, 0.979430303349862, 0.979430303349862, 0.979430303349862, 0.979430303349862, 0.979430303349862, 0.979430303349862, 1.20569696650138 }), false); minpackTest(new BrownAlmostLinearFunction(10, 5.0, 9765624.00089211, 0.0, new double[] { 0.979430303349865, 0.979430303349865, 0.979430303349865, 0.979430303349865, 0.979430303349865, 0.979430303349865, 0.979430303349865, 0.979430303349865, 0.979430303349865, 1.20569696650135 }), false); minpackTest(new BrownAlmostLinearFunction(10, 50.0, 0.9765625e17, 0.0, new double[] { 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0 }), false); minpackTest(new BrownAlmostLinearFunction(30, 0.5, 83.476044467848, 0.0, new double[] { 0.997754216442807, 0.997754216442807, 0.997754216442807, 0.997754216442807, 0.997754216442807, 0.997754216442807, 0.997754216442807, 0.997754216442807, 0.997754216442807, 0.997754216442807, 0.997754216442807, 0.997754216442807, 0.997754216442807, 0.997754216442807, 0.997754216442807, 0.997754216442807, 0.997754216442807, 0.997754216442807, 0.997754216442807, 0.997754216442807, 0.997754216442807, 0.997754216442807, 0.997754216442807, 0.997754216442807, 0.997754216442807, 0.997754216442807, 0.997754216442807, 0.997754216442807, 0.997754216442807, 1.06737350671578 }), false); minpackTest(new BrownAlmostLinearFunction(40, 0.5, 128.026364472323, 0.0, new double[] { 1.00000000000002, 1.00000000000002, 1.00000000000002, 1.00000000000002, 1.00000000000002, 1.00000000000002, 1.00000000000002, 1.00000000000002, 1.00000000000002, 1.00000000000002, 1.00000000000002, 1.00000000000002, 1.00000000000002, 1.00000000000002, 1.00000000000002, 1.00000000000002, 1.00000000000002, 1.00000000000002, 1.00000000000002, 1.00000000000002, 1.00000000000002, 1.00000000000002, 1.00000000000002, 1.00000000000002, 1.00000000000002, 1.00000000000002, 1.00000000000002, 1.00000000000002, 1.00000000000002, 1.00000000000002, 1.00000000000002, 1.00000000000002, 1.00000000000002, 1.00000000000002, 0.999999999999121 }), false); } @Test public void testMinpackOsborne1() { minpackTest(new Osborne1Function(new double[] { 0.5, 1.5, -1.0, 0.01, 0.02, }, 0.937564021037838, 0.00739249260904843, new double[] { 0.375410049244025, 1.93584654543108, -1.46468676748716, 0.0128675339110439, 0.0221227011813076 }), false); } @Test public void testMinpackOsborne2() { minpackTest(new Osborne2Function(new double[] { 1.3, 0.65, 0.65, 0.7, 0.6, 3.0, 5.0, 7.0, 2.0, 4.5, 5.5 }, 1.44686540984712, 0.20034404483314, new double[] { 1.30997663810096, 0.43155248076, 0.633661261602859, 0.599428560991695, 0.754179768272449, 0.904300082378518, 1.36579949521007, 4.82373199748107, 2.39868475104871, 4.56887554791452, 5.67534206273052 }), false); } private void minpackTest(MinpackFunction function, boolean exceptionExpected) { LevenbergMarquardtOptimizer optimizer = new LevenbergMarquardtOptimizer(FastMath.sqrt(2.22044604926e-16), FastMath.sqrt(2.22044604926e-16), 2.22044604926e-16); try { PointVectorValuePair optimum = optimizer.optimize(new MaxEval(400 * (function.getN() + 1)), function.getModelFunction(), function.getModelFunctionJacobian(), new Target(function.getTarget()), new Weight(function.getWeight()), new InitialGuess(function.getStartPoint())); Assert.assertFalse(exceptionExpected); function.checkTheoreticalMinCost(optimizer.getRMS()); function.checkTheoreticalMinParams(optimum); } catch (TooManyEvaluationsException e) { Assert.assertTrue(exceptionExpected); } } private static abstract class MinpackFunction { protected int n; protected int m; protected double[] startParams; protected double theoreticalMinCost; protected double[] theoreticalMinParams; protected double costAccuracy; protected double paramsAccuracy; protected MinpackFunction(int m, double[] startParams, double theoreticalMinCost, double[] theoreticalMinParams) { this.m = m; this.n = startParams.length; this.startParams = startParams.clone(); this.theoreticalMinCost = theoreticalMinCost; this.theoreticalMinParams = theoreticalMinParams; this.costAccuracy = 1.0e-8; this.paramsAccuracy = 1.0e-5; } protected static double[] buildArray(int n, double x) { double[] array = new double[n]; Arrays.fill(array, x); return array; } public double[] getTarget() { return buildArray(m, 0.0); } public double[] getWeight() { return buildArray(m, 1.0); } public double[] getStartPoint() { return startParams.clone(); } protected void setCostAccuracy(double costAccuracy) { this.costAccuracy = costAccuracy; } protected void setParamsAccuracy(double paramsAccuracy) { this.paramsAccuracy = paramsAccuracy; } public int getN() { return startParams.length; } public void checkTheoreticalMinCost(double rms) { double threshold = costAccuracy * (1.0 + theoreticalMinCost); Assert.assertEquals(theoreticalMinCost, FastMath.sqrt(m) * rms, threshold); } public void checkTheoreticalMinParams(PointVectorValuePair optimum) { double[] params = optimum.getPointRef(); if (theoreticalMinParams != null) { for (int i = 0; i < theoreticalMinParams.length; ++i) { double mi = theoreticalMinParams[i]; double vi = params[i]; Assert.assertEquals(mi, vi, paramsAccuracy * (1.0 + FastMath.abs(mi))); } } } public ModelFunction getModelFunction() { return new ModelFunction(new MultivariateVectorFunction() { public double[] value(double[] point) { return computeValue(point); } }); } public ModelFunctionJacobian getModelFunctionJacobian() { return new ModelFunctionJacobian(new MultivariateMatrixFunction() { public double[][] value(double[] point) { return computeJacobian(point); } }); } public abstract double[][] computeJacobian(double[] variables); public abstract double[] computeValue(double[] variables); } private static class LinearFullRankFunction extends MinpackFunction { private static final long serialVersionUID = -9030323226268039536L; public LinearFullRankFunction(int m, int n, double x0, double theoreticalStartCost, double theoreticalMinCost) { super(m, buildArray(n, x0), theoreticalMinCost, buildArray(n, -1.0)); } @Override public double[][] computeJacobian(double[] variables) { double t = 2.0 / m; double[][] jacobian = new double[m][]; for (int i = 0; i < m; ++i) { jacobian[i] = new double[n]; for (int j = 0; j < n; ++j) { jacobian[i][j] = (i == j) ? (1 - t) : -t; } } return jacobian; } @Override public double[] computeValue(double[] variables) { double sum = 0; for (int i = 0; i < n; ++i) { sum += variables[i]; } double t = 1 + 2 * sum / m; double[] f = new double[m]; for (int i = 0; i < n; ++i) { f[i] = variables[i] - t; } Arrays.fill(f, n, m, -t); return f; } } private static class LinearRank1Function extends MinpackFunction { private static final long serialVersionUID = 8494863245104608300L; public LinearRank1Function(int m, int n, double x0, double theoreticalStartCost, double theoreticalMinCost) { super(m, buildArray(n, x0), theoreticalMinCost, null); } @Override public double[][] computeJacobian(double[] variables) { double[][] jacobian = new double[m][]; for (int i = 0; i < m; ++i) { jacobian[i] = new double[n]; for (int j = 0; j < n; ++j) { jacobian[i][j] = (i + 1) * (j + 1); } } return jacobian; } @Override public double[] computeValue(double[] variables) { double[] f = new double[m]; double sum = 0; for (int i = 0; i < n; ++i) { sum += (i + 1) * variables[i]; } for (int i = 0; i < m; ++i) { f[i] = (i + 1) * sum - 1; } return f; } } private static class LinearRank1ZeroColsAndRowsFunction extends MinpackFunction { private static final long serialVersionUID = -3316653043091995018L; public LinearRank1ZeroColsAndRowsFunction(int m, int n, double x0) { super(m, buildArray(n, x0), FastMath.sqrt((m * (m + 3) - 6) / (2.0 * (2 * m - 3))), null); } @Override public double[][] computeJacobian(double[] variables) { double[][] jacobian = new double[m][]; for (int i = 0; i < m; ++i) { jacobian[i] = new double[n]; jacobian[i][0] = 0; for (int j = 1; j < (n - 1); ++j) { if (i == 0) { jacobian[i][j] = 0; } else if (i != (m - 1)) { jacobian[i][j] = i * (j + 1); } else { jacobian[i][j] = 0; } } jacobian[i][n - 1] = 0; } return jacobian; } @Override public double[] computeValue(double[] variables) { double[] f = new double[m]; double sum = 0; for (int i = 1; i < (n - 1); ++i) { sum += (i + 1) * variables[i]; } for (int i = 0; i < (m - 1); ++i) { f[i] = i * sum - 1; } f[m - 1] = -1; return f; } } private static class RosenbrockFunction extends MinpackFunction { private static final long serialVersionUID = 2893438180956569134L; public RosenbrockFunction(double[] startParams, double theoreticalStartCost) { super(2, startParams, 0.0, buildArray(2, 1.0)); } @Override public double[][] computeJacobian(double[] variables) { double x1 = variables[0]; return new double[][] { { -20 * x1, 10 }, { -1, 0 } }; } @Override public double[] computeValue(double[] variables) { double x1 = variables[0]; double x2 = variables[1]; return new double[] { 10 * (x2 - x1 * x1), 1 - x1 }; } } private static class HelicalValleyFunction extends MinpackFunction { private static final long serialVersionUID = 220613787843200102L; public HelicalValleyFunction(double[] startParams, double theoreticalStartCost) { super(3, startParams, 0.0, new double[] { 1.0, 0.0, 0.0 }); } @Override public double[][] computeJacobian(double[] variables) { double x1 = variables[0]; double x2 = variables[1]; double tmpSquare = x1 * x1 + x2 * x2; double tmp1 = twoPi * tmpSquare; double tmp2 = FastMath.sqrt(tmpSquare); return new double[][] { { 100 * x2 / tmp1, -100 * x1 / tmp1, 10 }, { 10 * x1 / tmp2, 10 * x2 / tmp2, 0 }, { 0, 0, 1 } }; } @Override public double[] computeValue(double[] variables) { double x1 = variables[0]; double x2 = variables[1]; double x3 = variables[2]; double tmp1; if (x1 == 0) { tmp1 = (x2 >= 0) ? 0.25 : -0.25; } else { tmp1 = FastMath.atan(x2 / x1) / twoPi; if (x1 < 0) { tmp1 += 0.5; } } double tmp2 = FastMath.sqrt(x1 * x1 + x2 * x2); return new double[] { 10.0 * (x3 - 10 * tmp1), 10.0 * (tmp2 - 1), x3 }; } private static final double twoPi = 2.0 * FastMath.PI; } private static class PowellSingularFunction extends MinpackFunction { private static final long serialVersionUID = 7298364171208142405L; public PowellSingularFunction(double[] startParams, double theoreticalStartCost) { super(4, startParams, 0.0, buildArray(4, 0.0)); } @Override public double[][] computeJacobian(double[] variables) { double x1 = variables[0]; double x2 = variables[1]; double x3 = variables[2]; double x4 = variables[3]; return new double[][] { { 1, 10, 0, 0 }, { 0, 0, sqrt5, -sqrt5 }, { 0, 2 * (x2 - 2 * x3), -4 * (x2 - 2 * x3), 0 }, { 2 * sqrt10 * (x1 - x4), 0, 0, -2 * sqrt10 * (x1 - x4) } }; } @Override public double[] computeValue(double[] variables) { double x1 = variables[0]; double x2 = variables[1]; double x3 = variables[2]; double x4 = variables[3]; return new double[] { x1 + 10 * x2, sqrt5 * (x3 - x4), (x2 - 2 * x3) * (x2 - 2 * x3), sqrt10 * (x1 - x4) * (x1 - x4) }; } private static final double sqrt5 = FastMath.sqrt( 5.0); private static final double sqrt10 = FastMath.sqrt(10.0); } private static class FreudensteinRothFunction extends MinpackFunction { private static final long serialVersionUID = 2892404999344244214L; public FreudensteinRothFunction(double[] startParams, double theoreticalStartCost, double theoreticalMinCost, double[] theoreticalMinParams) { super(2, startParams, theoreticalMinCost, theoreticalMinParams); } @Override public double[][] computeJacobian(double[] variables) { double x2 = variables[1]; return new double[][] { { 1, x2 * (10 - 3 * x2) - 2 }, { 1, x2 * ( 2 + 3 * x2) - 14, } }; } @Override public double[] computeValue(double[] variables) { double x1 = variables[0]; double x2 = variables[1]; return new double[] { -13.0 + x1 + ((5.0 - x2) * x2 - 2.0) * x2, -29.0 + x1 + ((1.0 + x2) * x2 - 14.0) * x2 }; } } private static class BardFunction extends MinpackFunction { private static final long serialVersionUID = 5990442612572087668L; public BardFunction(double x0, double theoreticalStartCost, double theoreticalMinCost, double[] theoreticalMinParams) { super(15, buildArray(3, x0), theoreticalMinCost, theoreticalMinParams); } @Override public double[][] computeJacobian(double[] variables) { double x2 = variables[1]; double x3 = variables[2]; double[][] jacobian = new double[m][]; for (int i = 0; i < m; ++i) { double tmp1 = i + 1; double tmp2 = 15 - i; double tmp3 = (i <= 7) ? tmp1 : tmp2; double tmp4 = x2 * tmp2 + x3 * tmp3; tmp4 *= tmp4; jacobian[i] = new double[] { -1, tmp1 * tmp2 / tmp4, tmp1 * tmp3 / tmp4 }; } return jacobian; } @Override public double[] computeValue(double[] variables) { double x1 = variables[0]; double x2 = variables[1]; double x3 = variables[2]; double[] f = new double[m]; for (int i = 0; i < m; ++i) { double tmp1 = i + 1; double tmp2 = 15 - i; double tmp3 = (i <= 7) ? tmp1 : tmp2; f[i] = y[i] - (x1 + tmp1 / (x2 * tmp2 + x3 * tmp3)); } return f; } private static final double[] y = { 0.14, 0.18, 0.22, 0.25, 0.29, 0.32, 0.35, 0.39, 0.37, 0.58, 0.73, 0.96, 1.34, 2.10, 4.39 }; } private static class KowalikOsborneFunction extends MinpackFunction { private static final long serialVersionUID = -4867445739880495801L; public KowalikOsborneFunction(double[] startParams, double theoreticalStartCost, double theoreticalMinCost, double[] theoreticalMinParams) { super(11, startParams, theoreticalMinCost, theoreticalMinParams); if (theoreticalStartCost > 20.0) { setCostAccuracy(2.0e-4); setParamsAccuracy(5.0e-3); } } @Override public double[][] computeJacobian(double[] variables) { double x1 = variables[0]; double x2 = variables[1]; double x3 = variables[2]; double x4 = variables[3]; double[][] jacobian = new double[m][]; for (int i = 0; i < m; ++i) { double tmp = v[i] * (v[i] + x3) + x4; double j1 = -v[i] * (v[i] + x2) / tmp; double j2 = -v[i] * x1 / tmp; double j3 = j1 * j2; double j4 = j3 / v[i]; jacobian[i] = new double[] { j1, j2, j3, j4 }; } return jacobian; } @Override public double[] computeValue(double[] variables) { double x1 = variables[0]; double x2 = variables[1]; double x3 = variables[2]; double x4 = variables[3]; double[] f = new double[m]; for (int i = 0; i < m; ++i) { f[i] = y[i] - x1 * (v[i] * (v[i] + x2)) / (v[i] * (v[i] + x3) + x4); } return f; } private static final double[] v = { 4.0, 2.0, 1.0, 0.5, 0.25, 0.167, 0.125, 0.1, 0.0833, 0.0714, 0.0625 }; private static final double[] y = { 0.1957, 0.1947, 0.1735, 0.1600, 0.0844, 0.0627, 0.0456, 0.0342, 0.0323, 0.0235, 0.0246 }; } private static class MeyerFunction extends MinpackFunction { private static final long serialVersionUID = -838060619150131027L; public MeyerFunction(double[] startParams, double theoreticalStartCost, double theoreticalMinCost, double[] theoreticalMinParams) { super(16, startParams, theoreticalMinCost, theoreticalMinParams); if (theoreticalStartCost > 1.0e6) { setCostAccuracy(7.0e-3); setParamsAccuracy(2.0e-2); } } @Override public double[][] computeJacobian(double[] variables) { double x1 = variables[0]; double x2 = variables[1]; double x3 = variables[2]; double[][] jacobian = new double[m][]; for (int i = 0; i < m; ++i) { double temp = 5.0 * (i + 1) + 45.0 + x3; double tmp1 = x2 / temp; double tmp2 = FastMath.exp(tmp1); double tmp3 = x1 * tmp2 / temp; jacobian[i] = new double[] { tmp2, tmp3, -tmp1 * tmp3 }; } return jacobian; } @Override public double[] computeValue(double[] variables) { double x1 = variables[0]; double x2 = variables[1]; double x3 = variables[2]; double[] f = new double[m]; for (int i = 0; i < m; ++i) { f[i] = x1 * FastMath.exp(x2 / (5.0 * (i + 1) + 45.0 + x3)) - y[i]; } return f; } private static final double[] y = { 34780.0, 28610.0, 23650.0, 19630.0, 16370.0, 13720.0, 11540.0, 9744.0, 8261.0, 7030.0, 6005.0, 5147.0, 4427.0, 3820.0, 3307.0, 2872.0 }; } private static class WatsonFunction extends MinpackFunction { private static final long serialVersionUID = -9034759294980218927L; public WatsonFunction(int n, double x0, double theoreticalStartCost, double theoreticalMinCost, double[] theoreticalMinParams) { super(31, buildArray(n, x0), theoreticalMinCost, theoreticalMinParams); } @Override public double[][] computeJacobian(double[] variables) { double[][] jacobian = new double[m][]; for (int i = 0; i < (m - 2); ++i) { double div = (i + 1) / 29.0; double s2 = 0.0; double dx = 1.0; for (int j = 0; j < n; ++j) { s2 += dx * variables[j]; dx *= div; } double temp= 2 * div * s2; dx = 1.0 / div; jacobian[i] = new double[n]; for (int j = 0; j < n; ++j) { jacobian[i][j] = dx * (j - temp); dx *= div; } } jacobian[m - 2] = new double[n]; jacobian[m - 2][0] = 1; jacobian[m - 1] = new double[n]; jacobian[m - 1][0]= -2 * variables[0]; jacobian[m - 1][1]= 1; return jacobian; } @Override public double[] computeValue(double[] variables) { double[] f = new double[m]; for (int i = 0; i < (m - 2); ++i) { double div = (i + 1) / 29.0; double s1 = 0; double dx = 1; for (int j = 1; j < n; ++j) { s1 += j * dx * variables[j]; dx *= div; } double s2 = 0; dx = 1; for (int j = 0; j < n; ++j) { s2 += dx * variables[j]; dx *= div; } f[i] = s1 - s2 * s2 - 1; } double x1 = variables[0]; double x2 = variables[1]; f[m - 2] = x1; f[m - 1] = x2 - x1 * x1 - 1; return f; } } private static class Box3DimensionalFunction extends MinpackFunction { private static final long serialVersionUID = 5511403858142574493L; public Box3DimensionalFunction(int m, double[] startParams, double theoreticalStartCost) { super(m, startParams, 0.0, new double[] { 1.0, 10.0, 1.0 }); } @Override public double[][] computeJacobian(double[] variables) { double x1 = variables[0]; double x2 = variables[1]; double[][] jacobian = new double[m][]; for (int i = 0; i < m; ++i) { double tmp = (i + 1) / 10.0; jacobian[i] = new double[] { -tmp * FastMath.exp(-tmp * x1), tmp * FastMath.exp(-tmp * x2), FastMath.exp(-i - 1) - FastMath.exp(-tmp) }; } return jacobian; } @Override public double[] computeValue(double[] variables) { double x1 = variables[0]; double x2 = variables[1]; double x3 = variables[2]; double[] f = new double[m]; for (int i = 0; i < m; ++i) { double tmp = (i + 1) / 10.0; f[i] = FastMath.exp(-tmp * x1) - FastMath.exp(-tmp * x2) + (FastMath.exp(-i - 1) - FastMath.exp(-tmp)) * x3; } return f; } } private static class JennrichSampsonFunction extends MinpackFunction { private static final long serialVersionUID = -2489165190443352947L; public JennrichSampsonFunction(int m, double[] startParams, double theoreticalStartCost, double theoreticalMinCost, double[] theoreticalMinParams) { super(m, startParams, theoreticalMinCost, theoreticalMinParams); } @Override public double[][] computeJacobian(double[] variables) { double x1 = variables[0]; double x2 = variables[1]; double[][] jacobian = new double[m][]; for (int i = 0; i < m; ++i) { double t = i + 1; jacobian[i] = new double[] { -t * FastMath.exp(t * x1), -t * FastMath.exp(t * x2) }; } return jacobian; } @Override public double[] computeValue(double[] variables) { double x1 = variables[0]; double x2 = variables[1]; double[] f = new double[m]; for (int i = 0; i < m; ++i) { double temp = i + 1; f[i] = 2 + 2 * temp - FastMath.exp(temp * x1) - FastMath.exp(temp * x2); } return f; } } private static class BrownDennisFunction extends MinpackFunction { private static final long serialVersionUID = 8340018645694243910L; public BrownDennisFunction(int m, double[] startParams, double theoreticalStartCost, double theoreticalMinCost, double[] theoreticalMinParams) { super(m, startParams, theoreticalMinCost, theoreticalMinParams); setCostAccuracy(2.5e-8); } @Override public double[][] computeJacobian(double[] variables) { double x1 = variables[0]; double x2 = variables[1]; double x3 = variables[2]; double x4 = variables[3]; double[][] jacobian = new double[m][]; for (int i = 0; i < m; ++i) { double temp = (i + 1) / 5.0; double ti = FastMath.sin(temp); double tmp1 = x1 + temp * x2 - FastMath.exp(temp); double tmp2 = x3 + ti * x4 - FastMath.cos(temp); jacobian[i] = new double[] { 2 * tmp1, 2 * temp * tmp1, 2 * tmp2, 2 * ti * tmp2 }; } return jacobian; } @Override public double[] computeValue(double[] variables) { double x1 = variables[0]; double x2 = variables[1]; double x3 = variables[2]; double x4 = variables[3]; double[] f = new double[m]; for (int i = 0; i < m; ++i) { double temp = (i + 1) / 5.0; double tmp1 = x1 + temp * x2 - FastMath.exp(temp); double tmp2 = x3 + FastMath.sin(temp) * x4 - FastMath.cos(temp); f[i] = tmp1 * tmp1 + tmp2 * tmp2; } return f; } } private static class ChebyquadFunction extends MinpackFunction { private static final long serialVersionUID = -2394877275028008594L; private static double[] buildChebyquadArray(int n, double factor) { double[] array = new double[n]; double inv = factor / (n + 1); for (int i = 0; i < n; ++i) { array[i] = (i + 1) * inv; } return array; } public ChebyquadFunction(int n, int m, double factor, double theoreticalStartCost, double theoreticalMinCost, double[] theoreticalMinParams) { super(m, buildChebyquadArray(n, factor), theoreticalMinCost, theoreticalMinParams); } @Override public double[][] computeJacobian(double[] variables) { double[][] jacobian = new double[m][]; for (int i = 0; i < m; ++i) { jacobian[i] = new double[n]; } double dx = 1.0 / n; for (int j = 0; j < n; ++j) { double tmp1 = 1; double tmp2 = 2 * variables[j] - 1; double temp = 2 * tmp2; double tmp3 = 0; double tmp4 = 2; for (int i = 0; i < m; ++i) { jacobian[i][j] = dx * tmp4; double ti = 4 * tmp2 + temp * tmp4 - tmp3; tmp3 = tmp4; tmp4 = ti; ti = temp * tmp2 - tmp1; tmp1 = tmp2; tmp2 = ti; } } return jacobian; } @Override public double[] computeValue(double[] variables) { double[] f = new double[m]; for (int j = 0; j < n; ++j) { double tmp1 = 1; double tmp2 = 2 * variables[j] - 1; double temp = 2 * tmp2; for (int i = 0; i < m; ++i) { f[i] += tmp2; double ti = temp * tmp2 - tmp1; tmp1 = tmp2; tmp2 = ti; } } double dx = 1.0 / n; boolean iev = false; for (int i = 0; i < m; ++i) { f[i] *= dx; if (iev) { f[i] += 1.0 / (i * (i + 2)); } iev = ! iev; } return f; } } private static class BrownAlmostLinearFunction extends MinpackFunction { private static final long serialVersionUID = 8239594490466964725L; public BrownAlmostLinearFunction(int m, double factor, double theoreticalStartCost, double theoreticalMinCost, double[] theoreticalMinParams) { super(m, buildArray(m, factor), theoreticalMinCost, theoreticalMinParams); } @Override public double[][] computeJacobian(double[] variables) { double[][] jacobian = new double[m][]; for (int i = 0; i < m; ++i) { jacobian[i] = new double[n]; } double prod = 1; for (int j = 0; j < n; ++j) { prod *= variables[j]; for (int i = 0; i < n; ++i) { jacobian[i][j] = 1; } jacobian[j][j] = 2; } for (int j = 0; j < n; ++j) { double temp = variables[j]; if (temp == 0) { temp = 1; prod = 1; for (int k = 0; k < n; ++k) { if (k != j) { prod *= variables[k]; } } } jacobian[n - 1][j] = prod / temp; } return jacobian; } @Override public double[] computeValue(double[] variables) { double[] f = new double[m]; double sum = -(n + 1); double prod = 1; for (int j = 0; j < n; ++j) { sum += variables[j]; prod *= variables[j]; } for (int i = 0; i < n; ++i) { f[i] = variables[i] + sum; } f[n - 1] = prod - 1; return f; } } private static class Osborne1Function extends MinpackFunction { private static final long serialVersionUID = 4006743521149849494L; public Osborne1Function(double[] startParams, double theoreticalStartCost, double theoreticalMinCost, double[] theoreticalMinParams) { super(33, startParams, theoreticalMinCost, theoreticalMinParams); } @Override public double[][] computeJacobian(double[] variables) { double x2 = variables[1]; double x3 = variables[2]; double x4 = variables[3]; double x5 = variables[4]; double[][] jacobian = new double[m][]; for (int i = 0; i < m; ++i) { double temp = 10.0 * i; double tmp1 = FastMath.exp(-temp * x4); double tmp2 = FastMath.exp(-temp * x5); jacobian[i] = new double[] { -1, -tmp1, -tmp2, temp * x2 * tmp1, temp * x3 * tmp2 }; } return jacobian; } @Override public double[] computeValue(double[] variables) { double x1 = variables[0]; double x2 = variables[1]; double x3 = variables[2]; double x4 = variables[3]; double x5 = variables[4]; double[] f = new double[m]; for (int i = 0; i < m; ++i) { double temp = 10.0 * i; double tmp1 = FastMath.exp(-temp * x4); double tmp2 = FastMath.exp(-temp * x5); f[i] = y[i] - (x1 + x2 * tmp1 + x3 * tmp2); } return f; } private static final double[] y = { 0.844, 0.908, 0.932, 0.936, 0.925, 0.908, 0.881, 0.850, 0.818, 0.784, 0.751, 0.718, 0.685, 0.658, 0.628, 0.603, 0.580, 0.558, 0.538, 0.522, 0.506, 0.490, 0.478, 0.467, 0.457, 0.448, 0.438, 0.431, 0.424, 0.420, 0.414, 0.411, 0.406 }; } private static class Osborne2Function extends MinpackFunction { private static final long serialVersionUID = -8418268780389858746L; public Osborne2Function(double[] startParams, double theoreticalStartCost, double theoreticalMinCost, double[] theoreticalMinParams) { super(65, startParams, theoreticalMinCost, theoreticalMinParams); } @Override public double[][] computeJacobian(double[] variables) { double x01 = variables[0]; double x02 = variables[1]; double x03 = variables[2]; double x04 = variables[3]; double x05 = variables[4]; double x06 = variables[5]; double x07 = variables[6]; double x08 = variables[7]; double x09 = variables[8]; double x10 = variables[9]; double x11 = variables[10]; double[][] jacobian = new double[m][]; for (int i = 0; i < m; ++i) { double temp = i / 10.0; double tmp1 = FastMath.exp(-x05 * temp); double tmp2 = FastMath.exp(-x06 * (temp - x09) * (temp - x09)); double tmp3 = FastMath.exp(-x07 * (temp - x10) * (temp - x10)); double tmp4 = FastMath.exp(-x08 * (temp - x11) * (temp - x11)); jacobian[i] = new double[] { -tmp1, -tmp2, -tmp3, -tmp4, temp * x01 * tmp1, x02 * (temp - x09) * (temp - x09) * tmp2, x03 * (temp - x10) * (temp - x10) * tmp3, x04 * (temp - x11) * (temp - x11) * tmp4, -2 * x02 * x06 * (temp - x09) * tmp2, -2 * x03 * x07 * (temp - x10) * tmp3, -2 * x04 * x08 * (temp - x11) * tmp4 }; } return jacobian; } @Override public double[] computeValue(double[] variables) { double x01 = variables[0]; double x02 = variables[1]; double x03 = variables[2]; double x04 = variables[3]; double x05 = variables[4]; double x06 = variables[5]; double x07 = variables[6]; double x08 = variables[7]; double x09 = variables[8]; double x10 = variables[9]; double x11 = variables[10]; double[] f = new double[m]; for (int i = 0; i < m; ++i) { double temp = i / 10.0; double tmp1 = FastMath.exp(-x05 * temp); double tmp2 = FastMath.exp(-x06 * (temp - x09) * (temp - x09)); double tmp3 = FastMath.exp(-x07 * (temp - x10) * (temp - x10)); double tmp4 = FastMath.exp(-x08 * (temp - x11) * (temp - x11)); f[i] = y[i] - (x01 * tmp1 + x02 * tmp2 + x03 * tmp3 + x04 * tmp4); } return f; } private static final double[] y = { 1.366, 1.191, 1.112, 1.013, 0.991, 0.885, 0.831, 0.847, 0.786, 0.725, 0.746, 0.679, 0.608, 0.655, 0.616, 0.606, 0.602, 0.626, 0.651, 0.724, 0.649, 0.649, 0.694, 0.644, 0.624, 0.661, 0.612, 0.558, 0.533, 0.495, 0.500, 0.423, 0.395, 0.375, 0.372, 0.391, 0.396, 0.405, 0.428, 0.429, 0.523, 0.562, 0.607, 0.653, 0.672, 0.708, 0.633, 0.668, 0.645, 0.632, 0.591, 0.559, 0.597, 0.625, 0.739, 0.710, 0.729, 0.720, 0.636, 0.581, 0.428, 0.292, 0.162, 0.098, 0.054 }; } } ././@LongLink100644 0 0 164 12126630646 10262 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/optim/nonlinear/scalar/noderiv/CMAESOptimizerTest.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/optim/nonlinear/scalar/noderiv/CMAESOpt100644 1750 1750 73070 12126627671 32076 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.optim.nonlinear.scalar.noderiv; import java.util.Arrays; import java.util.Random; import org.apache.commons.math3.Retry; import org.apache.commons.math3.RetryRunner; import org.apache.commons.math3.analysis.MultivariateFunction; import org.apache.commons.math3.exception.NumberIsTooLargeException; import org.apache.commons.math3.exception.NumberIsTooSmallException; import org.apache.commons.math3.exception.DimensionMismatchException; import org.apache.commons.math3.exception.MathUnsupportedOperationException; import org.apache.commons.math3.exception.MathIllegalStateException; import org.apache.commons.math3.exception.NotPositiveException; import org.apache.commons.math3.exception.OutOfRangeException; import org.apache.commons.math3.optim.nonlinear.scalar.GoalType; import org.apache.commons.math3.optim.PointValuePair; import org.apache.commons.math3.optim.InitialGuess; import org.apache.commons.math3.optim.SimpleBounds; import org.apache.commons.math3.optim.nonlinear.scalar.ObjectiveFunction; import org.apache.commons.math3.optim.MaxEval; import org.apache.commons.math3.random.MersenneTwister; import org.apache.commons.math3.util.FastMath; import org.junit.Assert; import org.junit.Test; import org.junit.Ignore; import org.junit.runner.RunWith; /** * Test for {@link CMAESOptimizer}. */ @RunWith(RetryRunner.class) public class CMAESOptimizerTest { static final int DIM = 13; static final int LAMBDA = 4 + (int)(3.*Math.log(DIM)); @Test(expected = NumberIsTooLargeException.class) public void testInitOutofbounds1() { double[] startPoint = point(DIM,3); double[] insigma = point(DIM, 0.3); double[][] boundaries = boundaries(DIM,-1,2); PointValuePair expected = new PointValuePair(point(DIM,1.0),0.0); doTest(new Rosen(), startPoint, insigma, boundaries, GoalType.MINIMIZE, LAMBDA, true, 0, 1e-13, 1e-13, 1e-6, 100000, expected); } @Test(expected = NumberIsTooSmallException.class) public void testInitOutofbounds2() { double[] startPoint = point(DIM, -2); double[] insigma = point(DIM, 0.3); double[][] boundaries = boundaries(DIM,-1,2); PointValuePair expected = new PointValuePair(point(DIM,1.0),0.0); doTest(new Rosen(), startPoint, insigma, boundaries, GoalType.MINIMIZE, LAMBDA, true, 0, 1e-13, 1e-13, 1e-6, 100000, expected); } @Test(expected = DimensionMismatchException.class) public void testBoundariesDimensionMismatch() { double[] startPoint = point(DIM,0.5); double[] insigma = point(DIM, 0.3); double[][] boundaries = boundaries(DIM+1,-1,2); PointValuePair expected = new PointValuePair(point(DIM,1.0),0.0); doTest(new Rosen(), startPoint, insigma, boundaries, GoalType.MINIMIZE, LAMBDA, true, 0, 1e-13, 1e-13, 1e-6, 100000, expected); } @Test(expected = NotPositiveException.class) public void testInputSigmaNegative() { double[] startPoint = point(DIM,0.5); double[] insigma = point(DIM,-0.5); double[][] boundaries = null; PointValuePair expected = new PointValuePair(point(DIM,1.0),0.0); doTest(new Rosen(), startPoint, insigma, boundaries, GoalType.MINIMIZE, LAMBDA, true, 0, 1e-13, 1e-13, 1e-6, 100000, expected); } @Test(expected = OutOfRangeException.class) public void testInputSigmaOutOfRange() { double[] startPoint = point(DIM,0.5); double[] insigma = point(DIM, 1.1); double[][] boundaries = boundaries(DIM,-0.5,0.5); PointValuePair expected = new PointValuePair(point(DIM,1.0),0.0); doTest(new Rosen(), startPoint, insigma, boundaries, GoalType.MINIMIZE, LAMBDA, true, 0, 1e-13, 1e-13, 1e-6, 100000, expected); } @Test(expected = DimensionMismatchException.class) public void testInputSigmaDimensionMismatch() { double[] startPoint = point(DIM,0.5); double[] insigma = point(DIM + 1, 0.5); double[][] boundaries = null; PointValuePair expected = new PointValuePair(point(DIM,1.0),0.0); doTest(new Rosen(), startPoint, insigma, boundaries, GoalType.MINIMIZE, LAMBDA, true, 0, 1e-13, 1e-13, 1e-6, 100000, expected); } @Test @Retry(3) public void testRosen() { double[] startPoint = point(DIM,0.1); double[] insigma = point(DIM,0.1); double[][] boundaries = null; PointValuePair expected = new PointValuePair(point(DIM,1.0),0.0); doTest(new Rosen(), startPoint, insigma, boundaries, GoalType.MINIMIZE, LAMBDA, true, 0, 1e-13, 1e-13, 1e-6, 100000, expected); doTest(new Rosen(), startPoint, insigma, boundaries, GoalType.MINIMIZE, LAMBDA, false, 0, 1e-13, 1e-13, 1e-6, 100000, expected); } @Test @Retry(3) public void testMaximize() { double[] startPoint = point(DIM,1.0); double[] insigma = point(DIM,0.1); double[][] boundaries = null; PointValuePair expected = new PointValuePair(point(DIM,0.0),1.0); doTest(new MinusElli(), startPoint, insigma, boundaries, GoalType.MAXIMIZE, LAMBDA, true, 0, 1.0-1e-13, 2e-10, 5e-6, 100000, expected); doTest(new MinusElli(), startPoint, insigma, boundaries, GoalType.MAXIMIZE, LAMBDA, false, 0, 1.0-1e-13, 2e-10, 5e-6, 100000, expected); boundaries = boundaries(DIM,-0.3,0.3); startPoint = point(DIM,0.1); doTest(new MinusElli(), startPoint, insigma, boundaries, GoalType.MAXIMIZE, LAMBDA, true, 0, 1.0-1e-13, 2e-10, 5e-6, 100000, expected); } @Test public void testEllipse() { double[] startPoint = point(DIM,1.0); double[] insigma = point(DIM,0.1); double[][] boundaries = null; PointValuePair expected = new PointValuePair(point(DIM,0.0),0.0); doTest(new Elli(), startPoint, insigma, boundaries, GoalType.MINIMIZE, LAMBDA, true, 0, 1e-13, 1e-13, 1e-6, 100000, expected); doTest(new Elli(), startPoint, insigma, boundaries, GoalType.MINIMIZE, LAMBDA, false, 0, 1e-13, 1e-13, 1e-6, 100000, expected); } @Test public void testElliRotated() { double[] startPoint = point(DIM,1.0); double[] insigma = point(DIM,0.1); double[][] boundaries = null; PointValuePair expected = new PointValuePair(point(DIM,0.0),0.0); doTest(new ElliRotated(), startPoint, insigma, boundaries, GoalType.MINIMIZE, LAMBDA, true, 0, 1e-13, 1e-13, 1e-6, 100000, expected); doTest(new ElliRotated(), startPoint, insigma, boundaries, GoalType.MINIMIZE, LAMBDA, false, 0, 1e-13, 1e-13, 1e-6, 100000, expected); } @Test public void testCigar() { double[] startPoint = point(DIM,1.0); double[] insigma = point(DIM,0.1); double[][] boundaries = null; PointValuePair expected = new PointValuePair(point(DIM,0.0),0.0); doTest(new Cigar(), startPoint, insigma, boundaries, GoalType.MINIMIZE, LAMBDA, true, 0, 1e-13, 1e-13, 1e-6, 200000, expected); doTest(new Cigar(), startPoint, insigma, boundaries, GoalType.MINIMIZE, LAMBDA, false, 0, 1e-13, 1e-13, 1e-6, 100000, expected); } @Test public void testCigarWithBoundaries() { double[] startPoint = point(DIM,1.0); double[] insigma = point(DIM,0.1); double[][] boundaries = boundaries(DIM, -1e100, Double.POSITIVE_INFINITY); PointValuePair expected = new PointValuePair(point(DIM,0.0),0.0); doTest(new Cigar(), startPoint, insigma, boundaries, GoalType.MINIMIZE, LAMBDA, true, 0, 1e-13, 1e-13, 1e-6, 200000, expected); doTest(new Cigar(), startPoint, insigma, boundaries, GoalType.MINIMIZE, LAMBDA, false, 0, 1e-13, 1e-13, 1e-6, 100000, expected); } @Test public void testTwoAxes() { double[] startPoint = point(DIM,1.0); double[] insigma = point(DIM,0.1); double[][] boundaries = null; PointValuePair expected = new PointValuePair(point(DIM,0.0),0.0); doTest(new TwoAxes(), startPoint, insigma, boundaries, GoalType.MINIMIZE, 2*LAMBDA, true, 0, 1e-13, 1e-13, 1e-6, 200000, expected); doTest(new TwoAxes(), startPoint, insigma, boundaries, GoalType.MINIMIZE, 2*LAMBDA, false, 0, 1e-13, 1e-8, 1e-3, 200000, expected); } @Test public void testCigTab() { double[] startPoint = point(DIM,1.0); double[] insigma = point(DIM,0.3); double[][] boundaries = null; PointValuePair expected = new PointValuePair(point(DIM,0.0),0.0); doTest(new CigTab(), startPoint, insigma, boundaries, GoalType.MINIMIZE, LAMBDA, true, 0, 1e-13, 1e-13, 5e-5, 100000, expected); doTest(new CigTab(), startPoint, insigma, boundaries, GoalType.MINIMIZE, LAMBDA, false, 0, 1e-13, 1e-13, 5e-5, 100000, expected); } @Test public void testSphere() { double[] startPoint = point(DIM,1.0); double[] insigma = point(DIM,0.1); double[][] boundaries = null; PointValuePair expected = new PointValuePair(point(DIM,0.0),0.0); doTest(new Sphere(), startPoint, insigma, boundaries, GoalType.MINIMIZE, LAMBDA, true, 0, 1e-13, 1e-13, 1e-6, 100000, expected); doTest(new Sphere(), startPoint, insigma, boundaries, GoalType.MINIMIZE, LAMBDA, false, 0, 1e-13, 1e-13, 1e-6, 100000, expected); } @Test public void testTablet() { double[] startPoint = point(DIM,1.0); double[] insigma = point(DIM,0.1); double[][] boundaries = null; PointValuePair expected = new PointValuePair(point(DIM,0.0),0.0); doTest(new Tablet(), startPoint, insigma, boundaries, GoalType.MINIMIZE, LAMBDA, true, 0, 1e-13, 1e-13, 1e-6, 100000, expected); doTest(new Tablet(), startPoint, insigma, boundaries, GoalType.MINIMIZE, LAMBDA, false, 0, 1e-13, 1e-13, 1e-6, 100000, expected); } @Test public void testDiffPow() { double[] startPoint = point(DIM,1.0); double[] insigma = point(DIM,0.1); double[][] boundaries = null; PointValuePair expected = new PointValuePair(point(DIM,0.0),0.0); doTest(new DiffPow(), startPoint, insigma, boundaries, GoalType.MINIMIZE, 10, true, 0, 1e-13, 1e-8, 1e-1, 100000, expected); doTest(new DiffPow(), startPoint, insigma, boundaries, GoalType.MINIMIZE, 10, false, 0, 1e-13, 1e-8, 2e-1, 100000, expected); } @Test public void testSsDiffPow() { double[] startPoint = point(DIM,1.0); double[] insigma = point(DIM,0.1); double[][] boundaries = null; PointValuePair expected = new PointValuePair(point(DIM,0.0),0.0); doTest(new SsDiffPow(), startPoint, insigma, boundaries, GoalType.MINIMIZE, 10, true, 0, 1e-13, 1e-4, 1e-1, 200000, expected); doTest(new SsDiffPow(), startPoint, insigma, boundaries, GoalType.MINIMIZE, 10, false, 0, 1e-13, 1e-4, 1e-1, 200000, expected); } @Test public void testAckley() { double[] startPoint = point(DIM,1.0); double[] insigma = point(DIM,1.0); double[][] boundaries = null; PointValuePair expected = new PointValuePair(point(DIM,0.0),0.0); doTest(new Ackley(), startPoint, insigma, boundaries, GoalType.MINIMIZE, 2*LAMBDA, true, 0, 1e-13, 1e-9, 1e-5, 100000, expected); doTest(new Ackley(), startPoint, insigma, boundaries, GoalType.MINIMIZE, 2*LAMBDA, false, 0, 1e-13, 1e-9, 1e-5, 100000, expected); } @Test public void testRastrigin() { double[] startPoint = point(DIM,0.1); double[] insigma = point(DIM,0.1); double[][] boundaries = null; PointValuePair expected = new PointValuePair(point(DIM,0.0),0.0); doTest(new Rastrigin(), startPoint, insigma, boundaries, GoalType.MINIMIZE, (int)(200*Math.sqrt(DIM)), true, 0, 1e-13, 1e-13, 1e-6, 200000, expected); doTest(new Rastrigin(), startPoint, insigma, boundaries, GoalType.MINIMIZE, (int)(200*Math.sqrt(DIM)), false, 0, 1e-13, 1e-13, 1e-6, 200000, expected); } @Test public void testConstrainedRosen() { double[] startPoint = point(DIM, 0.1); double[] insigma = point(DIM, 0.1); double[][] boundaries = boundaries(DIM, -1, 2); PointValuePair expected = new PointValuePair(point(DIM,1.0),0.0); doTest(new Rosen(), startPoint, insigma, boundaries, GoalType.MINIMIZE, 2*LAMBDA, true, 0, 1e-13, 1e-13, 1e-6, 100000, expected); doTest(new Rosen(), startPoint, insigma, boundaries, GoalType.MINIMIZE, 2*LAMBDA, false, 0, 1e-13, 1e-13, 1e-6, 100000, expected); } @Test public void testDiagonalRosen() { double[] startPoint = point(DIM,0.1); double[] insigma = point(DIM,0.1); double[][] boundaries = null; PointValuePair expected = new PointValuePair(point(DIM,1.0),0.0); doTest(new Rosen(), startPoint, insigma, boundaries, GoalType.MINIMIZE, LAMBDA, false, 1, 1e-13, 1e-10, 1e-4, 1000000, expected); } @Test public void testMath864() { final CMAESOptimizer optimizer = new CMAESOptimizer(30000, 0, true, 10, 0, new MersenneTwister(), false, null); final MultivariateFunction fitnessFunction = new MultivariateFunction() { public double value(double[] parameters) { final double target = 1; final double error = target - parameters[0]; return error * error; } }; final double[] start = { 0 }; final double[] lower = { -1e6 }; final double[] upper = { 1.5 }; final double[] sigma = { 1e-1 }; final double[] result = optimizer.optimize(new MaxEval(10000), new ObjectiveFunction(fitnessFunction), GoalType.MINIMIZE, new CMAESOptimizer.PopulationSize(5), new CMAESOptimizer.Sigma(sigma), new InitialGuess(start), new SimpleBounds(lower, upper)).getPoint(); Assert.assertTrue("Out of bounds (" + result[0] + " > " + upper[0] + ")", result[0] <= upper[0]); } /** * Cf. MATH-867 */ @Test public void testFitAccuracyDependsOnBoundary() { final CMAESOptimizer optimizer = new CMAESOptimizer(30000, 0, true, 10, 0, new MersenneTwister(), false, null); final MultivariateFunction fitnessFunction = new MultivariateFunction() { public double value(double[] parameters) { final double target = 11.1; final double error = target - parameters[0]; return error * error; } }; final double[] start = { 1 }; // No bounds. PointValuePair result = optimizer.optimize(new MaxEval(100000), new ObjectiveFunction(fitnessFunction), GoalType.MINIMIZE, SimpleBounds.unbounded(1), new CMAESOptimizer.PopulationSize(5), new CMAESOptimizer.Sigma(new double[] { 1e-1 }), new InitialGuess(start)); final double resNoBound = result.getPoint()[0]; // Optimum is near the lower bound. final double[] lower = { -20 }; final double[] upper = { 5e16 }; final double[] sigma = { 10 }; result = optimizer.optimize(new MaxEval(100000), new ObjectiveFunction(fitnessFunction), GoalType.MINIMIZE, new CMAESOptimizer.PopulationSize(5), new CMAESOptimizer.Sigma(sigma), new InitialGuess(start), new SimpleBounds(lower, upper)); final double resNearLo = result.getPoint()[0]; // Optimum is near the upper bound. lower[0] = -5e16; upper[0] = 20; result = optimizer.optimize(new MaxEval(100000), new ObjectiveFunction(fitnessFunction), GoalType.MINIMIZE, new CMAESOptimizer.PopulationSize(5), new CMAESOptimizer.Sigma(sigma), new InitialGuess(start), new SimpleBounds(lower, upper)); final double resNearHi = result.getPoint()[0]; // System.out.println("resNoBound=" + resNoBound + // " resNearLo=" + resNearLo + // " resNearHi=" + resNearHi); // The two values currently differ by a substantial amount, indicating that // the bounds definition can prevent reaching the optimum. Assert.assertEquals(resNoBound, resNearLo, 1e-3); Assert.assertEquals(resNoBound, resNearHi, 1e-3); } /** * @param func Function to optimize. * @param startPoint Starting point. * @param inSigma Individual input sigma. * @param boundaries Upper / lower point limit. * @param goal Minimization or maximization. * @param lambda Population size used for offspring. * @param isActive Covariance update mechanism. * @param diagonalOnly Simplified covariance update. * @param stopValue Termination criteria for optimization. * @param fTol Tolerance relative error on the objective function. * @param pointTol Tolerance for checking that the optimum is correct. * @param maxEvaluations Maximum number of evaluations. * @param expected Expected point / value. */ private void doTest(MultivariateFunction func, double[] startPoint, double[] inSigma, double[][] boundaries, GoalType goal, int lambda, boolean isActive, int diagonalOnly, double stopValue, double fTol, double pointTol, int maxEvaluations, PointValuePair expected) { int dim = startPoint.length; // test diagonalOnly = 0 - slow but normally fewer feval# CMAESOptimizer optim = new CMAESOptimizer(30000, stopValue, isActive, diagonalOnly, 0, new MersenneTwister(), false, null); PointValuePair result = boundaries == null ? optim.optimize(new MaxEval(maxEvaluations), new ObjectiveFunction(func), goal, new InitialGuess(startPoint), SimpleBounds.unbounded(dim), new CMAESOptimizer.Sigma(inSigma), new CMAESOptimizer.PopulationSize(lambda)) : optim.optimize(new MaxEval(maxEvaluations), new ObjectiveFunction(func), goal, new SimpleBounds(boundaries[0], boundaries[1]), new InitialGuess(startPoint), new CMAESOptimizer.Sigma(inSigma), new CMAESOptimizer.PopulationSize(lambda)); // System.out.println("sol=" + Arrays.toString(result.getPoint())); Assert.assertEquals(expected.getValue(), result.getValue(), fTol); for (int i = 0; i < dim; i++) { Assert.assertEquals(expected.getPoint()[i], result.getPoint()[i], pointTol); } Assert.assertTrue(optim.getIterations() > 0); } private static double[] point(int n, double value) { double[] ds = new double[n]; Arrays.fill(ds, value); return ds; } private static double[][] boundaries(int dim, double lower, double upper) { double[][] boundaries = new double[2][dim]; for (int i = 0; i < dim; i++) boundaries[0][i] = lower; for (int i = 0; i < dim; i++) boundaries[1][i] = upper; return boundaries; } private static class Sphere implements MultivariateFunction { public double value(double[] x) { double f = 0; for (int i = 0; i < x.length; ++i) f += x[i] * x[i]; return f; } } private static class Cigar implements MultivariateFunction { private double factor; Cigar() { this(1e3); } Cigar(double axisratio) { factor = axisratio * axisratio; } public double value(double[] x) { double f = x[0] * x[0]; for (int i = 1; i < x.length; ++i) f += factor * x[i] * x[i]; return f; } } private static class Tablet implements MultivariateFunction { private double factor; Tablet() { this(1e3); } Tablet(double axisratio) { factor = axisratio * axisratio; } public double value(double[] x) { double f = factor * x[0] * x[0]; for (int i = 1; i < x.length; ++i) f += x[i] * x[i]; return f; } } private static class CigTab implements MultivariateFunction { private double factor; CigTab() { this(1e4); } CigTab(double axisratio) { factor = axisratio; } public double value(double[] x) { int end = x.length - 1; double f = x[0] * x[0] / factor + factor * x[end] * x[end]; for (int i = 1; i < end; ++i) f += x[i] * x[i]; return f; } } private static class TwoAxes implements MultivariateFunction { private double factor; TwoAxes() { this(1e6); } TwoAxes(double axisratio) { factor = axisratio * axisratio; } public double value(double[] x) { double f = 0; for (int i = 0; i < x.length; ++i) f += (i < x.length / 2 ? factor : 1) * x[i] * x[i]; return f; } } private static class ElliRotated implements MultivariateFunction { private Basis B = new Basis(); private double factor; ElliRotated() { this(1e3); } ElliRotated(double axisratio) { factor = axisratio * axisratio; } public double value(double[] x) { double f = 0; x = B.Rotate(x); for (int i = 0; i < x.length; ++i) f += Math.pow(factor, i / (x.length - 1.)) * x[i] * x[i]; return f; } } private static class Elli implements MultivariateFunction { private double factor; Elli() { this(1e3); } Elli(double axisratio) { factor = axisratio * axisratio; } public double value(double[] x) { double f = 0; for (int i = 0; i < x.length; ++i) f += Math.pow(factor, i / (x.length - 1.)) * x[i] * x[i]; return f; } } private static class MinusElli implements MultivariateFunction { public double value(double[] x) { return 1.0-(new Elli().value(x)); } } private static class DiffPow implements MultivariateFunction { public double value(double[] x) { double f = 0; for (int i = 0; i < x.length; ++i) f += Math.pow(Math.abs(x[i]), 2. + 10 * (double) i / (x.length - 1.)); return f; } } private static class SsDiffPow implements MultivariateFunction { public double value(double[] x) { double f = Math.pow(new DiffPow().value(x), 0.25); return f; } } private static class Rosen implements MultivariateFunction { public double value(double[] x) { double f = 0; for (int i = 0; i < x.length - 1; ++i) f += 1e2 * (x[i] * x[i] - x[i + 1]) * (x[i] * x[i] - x[i + 1]) + (x[i] - 1.) * (x[i] - 1.); return f; } } private static class Ackley implements MultivariateFunction { private double axisratio; Ackley(double axra) { axisratio = axra; } public Ackley() { this(1); } public double value(double[] x) { double f = 0; double res2 = 0; double fac = 0; for (int i = 0; i < x.length; ++i) { fac = Math.pow(axisratio, (i - 1.) / (x.length - 1.)); f += fac * fac * x[i] * x[i]; res2 += Math.cos(2. * Math.PI * fac * x[i]); } f = (20. - 20. * Math.exp(-0.2 * Math.sqrt(f / x.length)) + Math.exp(1.) - Math.exp(res2 / x.length)); return f; } } private static class Rastrigin implements MultivariateFunction { private double axisratio; private double amplitude; Rastrigin() { this(1, 10); } Rastrigin(double axisratio, double amplitude) { this.axisratio = axisratio; this.amplitude = amplitude; } public double value(double[] x) { double f = 0; double fac; for (int i = 0; i < x.length; ++i) { fac = Math.pow(axisratio, (i - 1.) / (x.length - 1.)); if (i == 0 && x[i] < 0) fac *= 1.; f += fac * fac * x[i] * x[i] + amplitude * (1. - Math.cos(2. * Math.PI * fac * x[i])); } return f; } } private static class Basis { double[][] basis; Random rand = new Random(2); // use not always the same basis double[] Rotate(double[] x) { GenBasis(x.length); double[] y = new double[x.length]; for (int i = 0; i < x.length; ++i) { y[i] = 0; for (int j = 0; j < x.length; ++j) y[i] += basis[i][j] * x[j]; } return y; } void GenBasis(int DIM) { if (basis != null ? basis.length == DIM : false) return; double sp; int i, j, k; /* generate orthogonal basis */ basis = new double[DIM][DIM]; for (i = 0; i < DIM; ++i) { /* sample components gaussian */ for (j = 0; j < DIM; ++j) basis[i][j] = rand.nextGaussian(); /* substract projection of previous vectors */ for (j = i - 1; j >= 0; --j) { for (sp = 0., k = 0; k < DIM; ++k) sp += basis[i][k] * basis[j][k]; /* scalar product */ for (k = 0; k < DIM; ++k) basis[i][k] -= sp * basis[j][k]; /* substract */ } /* normalize */ for (sp = 0., k = 0; k < DIM; ++k) sp += basis[i][k] * basis[i][k]; /* squared norm */ for (k = 0; k < DIM; ++k) basis[i][k] /= Math.sqrt(sp); } } } } ././@LongLink100644 0 0 200 12126630646 10251 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/optim/nonlinear/scalar/noderiv/SimplexOptimizerNelderMeadTest.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/optim/nonlinear/scalar/noderiv/SimplexO100644 1750 1750 34024 12126627671 32317 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.optim.nonlinear.scalar.noderiv; import org.apache.commons.math3.exception.TooManyEvaluationsException; import org.apache.commons.math3.exception.MathUnsupportedOperationException; import org.apache.commons.math3.analysis.MultivariateFunction; import org.apache.commons.math3.analysis.MultivariateVectorFunction; import org.apache.commons.math3.linear.Array2DRowRealMatrix; import org.apache.commons.math3.linear.RealMatrix; import org.apache.commons.math3.optim.InitialGuess; import org.apache.commons.math3.optim.MaxEval; import org.apache.commons.math3.optim.SimpleBounds; import org.apache.commons.math3.optim.PointValuePair; import org.apache.commons.math3.optim.nonlinear.scalar.GoalType; import org.apache.commons.math3.optim.nonlinear.scalar.ObjectiveFunction; import org.apache.commons.math3.optim.nonlinear.scalar.LeastSquaresConverter; import org.apache.commons.math3.util.FastMath; import org.junit.Assert; import org.junit.Test; public class SimplexOptimizerNelderMeadTest { @Test(expected=MathUnsupportedOperationException.class) public void testBoundsUnsupported() { SimplexOptimizer optimizer = new SimplexOptimizer(1e-10, 1e-30); final FourExtrema fourExtrema = new FourExtrema(); optimizer.optimize(new MaxEval(100), new ObjectiveFunction(fourExtrema), GoalType.MINIMIZE, new InitialGuess(new double[] { -3, 0 }), new NelderMeadSimplex(new double[] { 0.2, 0.2 }), new SimpleBounds(new double[] { -5, -1 }, new double[] { 5, 1 })); } @Test public void testMinimize1() { SimplexOptimizer optimizer = new SimplexOptimizer(1e-10, 1e-30); final FourExtrema fourExtrema = new FourExtrema(); final PointValuePair optimum = optimizer.optimize(new MaxEval(100), new ObjectiveFunction(fourExtrema), GoalType.MINIMIZE, new InitialGuess(new double[] { -3, 0 }), new NelderMeadSimplex(new double[] { 0.2, 0.2 })); Assert.assertEquals(fourExtrema.xM, optimum.getPoint()[0], 2e-7); Assert.assertEquals(fourExtrema.yP, optimum.getPoint()[1], 2e-5); Assert.assertEquals(fourExtrema.valueXmYp, optimum.getValue(), 6e-12); Assert.assertTrue(optimizer.getEvaluations() > 60); Assert.assertTrue(optimizer.getEvaluations() < 90); // Check that the number of iterations is updated (MATH-949). Assert.assertTrue(optimizer.getIterations() > 0); } @Test public void testMinimize2() { SimplexOptimizer optimizer = new SimplexOptimizer(1e-10, 1e-30); final FourExtrema fourExtrema = new FourExtrema(); final PointValuePair optimum = optimizer.optimize(new MaxEval(100), new ObjectiveFunction(fourExtrema), GoalType.MINIMIZE, new InitialGuess(new double[] { 1, 0 }), new NelderMeadSimplex(new double[] { 0.2, 0.2 })); Assert.assertEquals(fourExtrema.xP, optimum.getPoint()[0], 5e-6); Assert.assertEquals(fourExtrema.yM, optimum.getPoint()[1], 6e-6); Assert.assertEquals(fourExtrema.valueXpYm, optimum.getValue(), 1e-11); Assert.assertTrue(optimizer.getEvaluations() > 60); Assert.assertTrue(optimizer.getEvaluations() < 90); // Check that the number of iterations is updated (MATH-949). Assert.assertTrue(optimizer.getIterations() > 0); } @Test public void testMaximize1() { SimplexOptimizer optimizer = new SimplexOptimizer(1e-10, 1e-30); final FourExtrema fourExtrema = new FourExtrema(); final PointValuePair optimum = optimizer.optimize(new MaxEval(100), new ObjectiveFunction(fourExtrema), GoalType.MAXIMIZE, new InitialGuess(new double[] { -3, 0 }), new NelderMeadSimplex(new double[] { 0.2, 0.2 })); Assert.assertEquals(fourExtrema.xM, optimum.getPoint()[0], 1e-5); Assert.assertEquals(fourExtrema.yM, optimum.getPoint()[1], 3e-6); Assert.assertEquals(fourExtrema.valueXmYm, optimum.getValue(), 3e-12); Assert.assertTrue(optimizer.getEvaluations() > 60); Assert.assertTrue(optimizer.getEvaluations() < 90); // Check that the number of iterations is updated (MATH-949). Assert.assertTrue(optimizer.getIterations() > 0); } @Test public void testMaximize2() { SimplexOptimizer optimizer = new SimplexOptimizer(1e-10, 1e-30); final FourExtrema fourExtrema = new FourExtrema(); final PointValuePair optimum = optimizer.optimize(new MaxEval(100), new ObjectiveFunction(fourExtrema), GoalType.MAXIMIZE, new InitialGuess(new double[] { 1, 0 }), new NelderMeadSimplex(new double[] { 0.2, 0.2 })); Assert.assertEquals(fourExtrema.xP, optimum.getPoint()[0], 4e-6); Assert.assertEquals(fourExtrema.yP, optimum.getPoint()[1], 5e-6); Assert.assertEquals(fourExtrema.valueXpYp, optimum.getValue(), 7e-12); Assert.assertTrue(optimizer.getEvaluations() > 60); Assert.assertTrue(optimizer.getEvaluations() < 90); // Check that the number of iterations is updated (MATH-949). Assert.assertTrue(optimizer.getIterations() > 0); } @Test public void testRosenbrock() { Rosenbrock rosenbrock = new Rosenbrock(); SimplexOptimizer optimizer = new SimplexOptimizer(-1, 1e-3); PointValuePair optimum = optimizer.optimize(new MaxEval(100), new ObjectiveFunction(rosenbrock), GoalType.MINIMIZE, new InitialGuess(new double[] { -1.2, 1 }), new NelderMeadSimplex(new double[][] { { -1.2, 1 }, { 0.9, 1.2 }, { 3.5, -2.3 } })); Assert.assertEquals(rosenbrock.getCount(), optimizer.getEvaluations()); Assert.assertTrue(optimizer.getEvaluations() > 40); Assert.assertTrue(optimizer.getEvaluations() < 50); Assert.assertTrue(optimum.getValue() < 8e-4); } @Test public void testPowell() { Powell powell = new Powell(); SimplexOptimizer optimizer = new SimplexOptimizer(-1, 1e-3); PointValuePair optimum = optimizer.optimize(new MaxEval(200), new ObjectiveFunction(powell), GoalType.MINIMIZE, new InitialGuess(new double[] { 3, -1, 0, 1 }), new NelderMeadSimplex(4)); Assert.assertEquals(powell.getCount(), optimizer.getEvaluations()); Assert.assertTrue(optimizer.getEvaluations() > 110); Assert.assertTrue(optimizer.getEvaluations() < 130); Assert.assertTrue(optimum.getValue() < 2e-3); } @Test public void testLeastSquares1() { final RealMatrix factors = new Array2DRowRealMatrix(new double[][] { { 1, 0 }, { 0, 1 } }, false); LeastSquaresConverter ls = new LeastSquaresConverter(new MultivariateVectorFunction() { public double[] value(double[] variables) { return factors.operate(variables); } }, new double[] { 2.0, -3.0 }); SimplexOptimizer optimizer = new SimplexOptimizer(-1, 1e-6); PointValuePair optimum = optimizer.optimize(new MaxEval(200), new ObjectiveFunction(ls), GoalType.MINIMIZE, new InitialGuess(new double[] { 10, 10 }), new NelderMeadSimplex(2)); Assert.assertEquals( 2, optimum.getPointRef()[0], 3e-5); Assert.assertEquals(-3, optimum.getPointRef()[1], 4e-4); Assert.assertTrue(optimizer.getEvaluations() > 60); Assert.assertTrue(optimizer.getEvaluations() < 80); Assert.assertTrue(optimum.getValue() < 1.0e-6); } @Test public void testLeastSquares2() { final RealMatrix factors = new Array2DRowRealMatrix(new double[][] { { 1, 0 }, { 0, 1 } }, false); LeastSquaresConverter ls = new LeastSquaresConverter(new MultivariateVectorFunction() { public double[] value(double[] variables) { return factors.operate(variables); } }, new double[] { 2, -3 }, new double[] { 10, 0.1 }); SimplexOptimizer optimizer = new SimplexOptimizer(-1, 1e-6); PointValuePair optimum = optimizer.optimize(new MaxEval(200), new ObjectiveFunction(ls), GoalType.MINIMIZE, new InitialGuess(new double[] { 10, 10 }), new NelderMeadSimplex(2)); Assert.assertEquals( 2, optimum.getPointRef()[0], 5e-5); Assert.assertEquals(-3, optimum.getPointRef()[1], 8e-4); Assert.assertTrue(optimizer.getEvaluations() > 60); Assert.assertTrue(optimizer.getEvaluations() < 80); Assert.assertTrue(optimum.getValue() < 1e-6); } @Test public void testLeastSquares3() { final RealMatrix factors = new Array2DRowRealMatrix(new double[][] { { 1, 0 }, { 0, 1 } }, false); LeastSquaresConverter ls = new LeastSquaresConverter(new MultivariateVectorFunction() { public double[] value(double[] variables) { return factors.operate(variables); } }, new double[] { 2, -3 }, new Array2DRowRealMatrix(new double [][] { { 1, 1.2 }, { 1.2, 2 } })); SimplexOptimizer optimizer = new SimplexOptimizer(-1, 1e-6); PointValuePair optimum = optimizer.optimize(new MaxEval(200), new ObjectiveFunction(ls), GoalType.MINIMIZE, new InitialGuess(new double[] { 10, 10 }), new NelderMeadSimplex(2)); Assert.assertEquals( 2, optimum.getPointRef()[0], 2e-3); Assert.assertEquals(-3, optimum.getPointRef()[1], 8e-4); Assert.assertTrue(optimizer.getEvaluations() > 60); Assert.assertTrue(optimizer.getEvaluations() < 80); Assert.assertTrue(optimum.getValue() < 1e-6); } @Test(expected=TooManyEvaluationsException.class) public void testMaxIterations() { Powell powell = new Powell(); SimplexOptimizer optimizer = new SimplexOptimizer(-1, 1e-3); optimizer.optimize(new MaxEval(20), new ObjectiveFunction(powell), GoalType.MINIMIZE, new InitialGuess(new double[] { 3, -1, 0, 1 }), new NelderMeadSimplex(4)); } private static class FourExtrema implements MultivariateFunction { // The following function has 4 local extrema. final double xM = -3.841947088256863675365; final double yM = -1.391745200270734924416; final double xP = 0.2286682237349059125691; final double yP = -yM; final double valueXmYm = 0.2373295333134216789769; // Local maximum. final double valueXmYp = -valueXmYm; // Local minimum. final double valueXpYm = -0.7290400707055187115322; // Global minimum. final double valueXpYp = -valueXpYm; // Global maximum. public double value(double[] variables) { final double x = variables[0]; final double y = variables[1]; return (x == 0 || y == 0) ? 0 : FastMath.atan(x) * FastMath.atan(x + 2) * FastMath.atan(y) * FastMath.atan(y) / (x * y); } } private static class Rosenbrock implements MultivariateFunction { private int count; public Rosenbrock() { count = 0; } public double value(double[] x) { ++count; double a = x[1] - x[0] * x[0]; double b = 1.0 - x[0]; return 100 * a * a + b * b; } public int getCount() { return count; } } private static class Powell implements MultivariateFunction { private int count; public Powell() { count = 0; } public double value(double[] x) { ++count; double a = x[0] + 10 * x[1]; double b = x[2] - x[3]; double c = x[1] - 2 * x[2]; double d = x[0] - x[3]; return a * a + 5 * b * b + c * c * c * c + 10 * d * d * d * d; } public int getCount() { return count; } } } ././@LongLink100644 0 0 165 12126630646 10263 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/optim/nonlinear/scalar/noderiv/BOBYQAOptimizerTest.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/optim/nonlinear/scalar/noderiv/BOBYQAOp100644 1750 1750 51034 12126627671 32033 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.optim.nonlinear.scalar.noderiv; import java.util.Arrays; import java.util.Random; import org.apache.commons.math3.analysis.MultivariateFunction; import org.apache.commons.math3.exception.DimensionMismatchException; import org.apache.commons.math3.exception.TooManyEvaluationsException; import org.apache.commons.math3.exception.NumberIsTooLargeException; import org.apache.commons.math3.exception.NumberIsTooSmallException; import org.apache.commons.math3.optim.MaxEval; import org.apache.commons.math3.optim.nonlinear.scalar.ObjectiveFunction; import org.apache.commons.math3.optim.nonlinear.scalar.GoalType; import org.apache.commons.math3.optim.PointValuePair; import org.apache.commons.math3.optim.InitialGuess; import org.apache.commons.math3.optim.SimpleBounds; import org.junit.Assert; import org.junit.Ignore; import org.junit.Test; /** * Test for {@link BOBYQAOptimizer}. */ public class BOBYQAOptimizerTest { static final int DIM = 13; @Test(expected=NumberIsTooLargeException.class) public void testInitOutOfBounds() { double[] startPoint = point(DIM, 3); double[][] boundaries = boundaries(DIM, -1, 2); doTest(new Rosen(), startPoint, boundaries, GoalType.MINIMIZE, 1e-13, 1e-6, 2000, null); } @Test(expected=DimensionMismatchException.class) public void testBoundariesDimensionMismatch() { double[] startPoint = point(DIM, 0.5); double[][] boundaries = boundaries(DIM + 1, -1, 2); doTest(new Rosen(), startPoint, boundaries, GoalType.MINIMIZE, 1e-13, 1e-6, 2000, null); } @Test(expected=NumberIsTooSmallException.class) public void testProblemDimensionTooSmall() { double[] startPoint = point(1, 0.5); doTest(new Rosen(), startPoint, null, GoalType.MINIMIZE, 1e-13, 1e-6, 2000, null); } @Test(expected=TooManyEvaluationsException.class) public void testMaxEvaluations() { final int lowMaxEval = 2; double[] startPoint = point(DIM, 0.1); double[][] boundaries = null; doTest(new Rosen(), startPoint, boundaries, GoalType.MINIMIZE, 1e-13, 1e-6, lowMaxEval, null); } @Test public void testRosen() { double[] startPoint = point(DIM,0.1); double[][] boundaries = null; PointValuePair expected = new PointValuePair(point(DIM,1.0),0.0); doTest(new Rosen(), startPoint, boundaries, GoalType.MINIMIZE, 1e-13, 1e-6, 2000, expected); } @Test public void testMaximize() { double[] startPoint = point(DIM,1.0); double[][] boundaries = null; PointValuePair expected = new PointValuePair(point(DIM,0.0),1.0); doTest(new MinusElli(), startPoint, boundaries, GoalType.MAXIMIZE, 2e-10, 5e-6, 1000, expected); boundaries = boundaries(DIM,-0.3,0.3); startPoint = point(DIM,0.1); doTest(new MinusElli(), startPoint, boundaries, GoalType.MAXIMIZE, 2e-10, 5e-6, 1000, expected); } @Test public void testEllipse() { double[] startPoint = point(DIM,1.0); double[][] boundaries = null; PointValuePair expected = new PointValuePair(point(DIM,0.0),0.0); doTest(new Elli(), startPoint, boundaries, GoalType.MINIMIZE, 1e-13, 1e-6, 1000, expected); } @Test public void testElliRotated() { double[] startPoint = point(DIM,1.0); double[][] boundaries = null; PointValuePair expected = new PointValuePair(point(DIM,0.0),0.0); doTest(new ElliRotated(), startPoint, boundaries, GoalType.MINIMIZE, 1e-12, 1e-6, 10000, expected); } @Test public void testCigar() { double[] startPoint = point(DIM,1.0); double[][] boundaries = null; PointValuePair expected = new PointValuePair(point(DIM,0.0),0.0); doTest(new Cigar(), startPoint, boundaries, GoalType.MINIMIZE, 1e-13, 1e-6, 100, expected); } @Test public void testTwoAxes() { double[] startPoint = point(DIM,1.0); double[][] boundaries = null; PointValuePair expected = new PointValuePair(point(DIM,0.0),0.0); doTest(new TwoAxes(), startPoint, boundaries, GoalType.MINIMIZE, 2* 1e-13, 1e-6, 100, expected); } @Test public void testCigTab() { double[] startPoint = point(DIM,1.0); double[][] boundaries = null; PointValuePair expected = new PointValuePair(point(DIM,0.0),0.0); doTest(new CigTab(), startPoint, boundaries, GoalType.MINIMIZE, 1e-13, 5e-5, 100, expected); } @Test public void testSphere() { double[] startPoint = point(DIM,1.0); double[][] boundaries = null; PointValuePair expected = new PointValuePair(point(DIM,0.0),0.0); doTest(new Sphere(), startPoint, boundaries, GoalType.MINIMIZE, 1e-13, 1e-6, 100, expected); } @Test public void testTablet() { double[] startPoint = point(DIM,1.0); double[][] boundaries = null; PointValuePair expected = new PointValuePair(point(DIM,0.0),0.0); doTest(new Tablet(), startPoint, boundaries, GoalType.MINIMIZE, 1e-13, 1e-6, 100, expected); } @Test public void testDiffPow() { double[] startPoint = point(DIM/2,1.0); double[][] boundaries = null; PointValuePair expected = new PointValuePair(point(DIM/2,0.0),0.0); doTest(new DiffPow(), startPoint, boundaries, GoalType.MINIMIZE, 1e-8, 1e-1, 12000, expected); } @Test public void testSsDiffPow() { double[] startPoint = point(DIM/2,1.0); double[][] boundaries = null; PointValuePair expected = new PointValuePair(point(DIM/2,0.0),0.0); doTest(new SsDiffPow(), startPoint, boundaries, GoalType.MINIMIZE, 1e-2, 1.3e-1, 50000, expected); } @Test public void testAckley() { double[] startPoint = point(DIM,0.1); double[][] boundaries = null; PointValuePair expected = new PointValuePair(point(DIM,0.0),0.0); doTest(new Ackley(), startPoint, boundaries, GoalType.MINIMIZE, 1e-8, 1e-5, 1000, expected); } @Test public void testRastrigin() { double[] startPoint = point(DIM,1.0); double[][] boundaries = null; PointValuePair expected = new PointValuePair(point(DIM,0.0),0.0); doTest(new Rastrigin(), startPoint, boundaries, GoalType.MINIMIZE, 1e-13, 1e-6, 1000, expected); } @Test public void testConstrainedRosen() { double[] startPoint = point(DIM,0.1); double[][] boundaries = boundaries(DIM,-1,2); PointValuePair expected = new PointValuePair(point(DIM,1.0),0.0); doTest(new Rosen(), startPoint, boundaries, GoalType.MINIMIZE, 1e-13, 1e-6, 2000, expected); } // See MATH-728 // TODO: this test is temporarily disabled for 3.2 release as a bug in Cobertura // makes it run for several hours before completing @Ignore @Test public void testConstrainedRosenWithMoreInterpolationPoints() { final double[] startPoint = point(DIM, 0.1); final double[][] boundaries = boundaries(DIM, -1, 2); final PointValuePair expected = new PointValuePair(point(DIM, 1.0), 0.0); // This should have been 78 because in the code the hard limit is // said to be // ((DIM + 1) * (DIM + 2)) / 2 - (2 * DIM + 1) // i.e. 78 in this case, but the test fails for 48, 59, 62, 63, 64, // 65, 66, ... final int maxAdditionalPoints = 47; for (int num = 1; num <= maxAdditionalPoints; num++) { doTest(new Rosen(), startPoint, boundaries, GoalType.MINIMIZE, 1e-12, 1e-6, 2000, num, expected, "num=" + num); } } /** * @param func Function to optimize. * @param startPoint Starting point. * @param boundaries Upper / lower point limit. * @param goal Minimization or maximization. * @param fTol Tolerance relative error on the objective function. * @param pointTol Tolerance for checking that the optimum is correct. * @param maxEvaluations Maximum number of evaluations. * @param expected Expected point / value. */ private void doTest(MultivariateFunction func, double[] startPoint, double[][] boundaries, GoalType goal, double fTol, double pointTol, int maxEvaluations, PointValuePair expected) { doTest(func, startPoint, boundaries, goal, fTol, pointTol, maxEvaluations, 0, expected, ""); } /** * @param func Function to optimize. * @param startPoint Starting point. * @param boundaries Upper / lower point limit. * @param goal Minimization or maximization. * @param fTol Tolerance relative error on the objective function. * @param pointTol Tolerance for checking that the optimum is correct. * @param maxEvaluations Maximum number of evaluations. * @param additionalInterpolationPoints Number of interpolation to used * in addition to the default (2 * dim + 1). * @param expected Expected point / value. */ private void doTest(MultivariateFunction func, double[] startPoint, double[][] boundaries, GoalType goal, double fTol, double pointTol, int maxEvaluations, int additionalInterpolationPoints, PointValuePair expected, String assertMsg) { // System.out.println(func.getClass().getName() + " BEGIN"); // XXX int dim = startPoint.length; final int numIterpolationPoints = 2 * dim + 1 + additionalInterpolationPoints; BOBYQAOptimizer optim = new BOBYQAOptimizer(numIterpolationPoints); PointValuePair result = boundaries == null ? optim.optimize(new MaxEval(maxEvaluations), new ObjectiveFunction(func), goal, SimpleBounds.unbounded(dim), new InitialGuess(startPoint)) : optim.optimize(new MaxEval(maxEvaluations), new ObjectiveFunction(func), goal, new InitialGuess(startPoint), new SimpleBounds(boundaries[0], boundaries[1])); // System.out.println(func.getClass().getName() + " = " // + optim.getEvaluations() + " f("); // for (double x: result.getPoint()) System.out.print(x + " "); // System.out.println(") = " + result.getValue()); Assert.assertEquals(assertMsg, expected.getValue(), result.getValue(), fTol); for (int i = 0; i < dim; i++) { Assert.assertEquals(expected.getPoint()[i], result.getPoint()[i], pointTol); } // System.out.println(func.getClass().getName() + " END"); // XXX } private static double[] point(int n, double value) { double[] ds = new double[n]; Arrays.fill(ds, value); return ds; } private static double[][] boundaries(int dim, double lower, double upper) { double[][] boundaries = new double[2][dim]; for (int i = 0; i < dim; i++) boundaries[0][i] = lower; for (int i = 0; i < dim; i++) boundaries[1][i] = upper; return boundaries; } private static class Sphere implements MultivariateFunction { public double value(double[] x) { double f = 0; for (int i = 0; i < x.length; ++i) f += x[i] * x[i]; return f; } } private static class Cigar implements MultivariateFunction { private double factor; Cigar() { this(1e3); } Cigar(double axisratio) { factor = axisratio * axisratio; } public double value(double[] x) { double f = x[0] * x[0]; for (int i = 1; i < x.length; ++i) f += factor * x[i] * x[i]; return f; } } private static class Tablet implements MultivariateFunction { private double factor; Tablet() { this(1e3); } Tablet(double axisratio) { factor = axisratio * axisratio; } public double value(double[] x) { double f = factor * x[0] * x[0]; for (int i = 1; i < x.length; ++i) f += x[i] * x[i]; return f; } } private static class CigTab implements MultivariateFunction { private double factor; CigTab() { this(1e4); } CigTab(double axisratio) { factor = axisratio; } public double value(double[] x) { int end = x.length - 1; double f = x[0] * x[0] / factor + factor * x[end] * x[end]; for (int i = 1; i < end; ++i) f += x[i] * x[i]; return f; } } private static class TwoAxes implements MultivariateFunction { private double factor; TwoAxes() { this(1e6); } TwoAxes(double axisratio) { factor = axisratio * axisratio; } public double value(double[] x) { double f = 0; for (int i = 0; i < x.length; ++i) f += (i < x.length / 2 ? factor : 1) * x[i] * x[i]; return f; } } private static class ElliRotated implements MultivariateFunction { private Basis B = new Basis(); private double factor; ElliRotated() { this(1e3); } ElliRotated(double axisratio) { factor = axisratio * axisratio; } public double value(double[] x) { double f = 0; x = B.Rotate(x); for (int i = 0; i < x.length; ++i) f += Math.pow(factor, i / (x.length - 1.)) * x[i] * x[i]; return f; } } private static class Elli implements MultivariateFunction { private double factor; Elli() { this(1e3); } Elli(double axisratio) { factor = axisratio * axisratio; } public double value(double[] x) { double f = 0; for (int i = 0; i < x.length; ++i) f += Math.pow(factor, i / (x.length - 1.)) * x[i] * x[i]; return f; } } private static class MinusElli implements MultivariateFunction { private final Elli elli = new Elli(); public double value(double[] x) { return 1.0 - elli.value(x); } } private static class DiffPow implements MultivariateFunction { // private int fcount = 0; public double value(double[] x) { double f = 0; for (int i = 0; i < x.length; ++i) f += Math.pow(Math.abs(x[i]), 2. + 10 * (double) i / (x.length - 1.)); // System.out.print("" + (fcount++) + ") "); // for (int i = 0; i < x.length; i++) // System.out.print(x[i] + " "); // System.out.println(" = " + f); return f; } } private static class SsDiffPow implements MultivariateFunction { public double value(double[] x) { double f = Math.pow(new DiffPow().value(x), 0.25); return f; } } private static class Rosen implements MultivariateFunction { public double value(double[] x) { double f = 0; for (int i = 0; i < x.length - 1; ++i) f += 1e2 * (x[i] * x[i] - x[i + 1]) * (x[i] * x[i] - x[i + 1]) + (x[i] - 1.) * (x[i] - 1.); return f; } } private static class Ackley implements MultivariateFunction { private double axisratio; Ackley(double axra) { axisratio = axra; } public Ackley() { this(1); } public double value(double[] x) { double f = 0; double res2 = 0; double fac = 0; for (int i = 0; i < x.length; ++i) { fac = Math.pow(axisratio, (i - 1.) / (x.length - 1.)); f += fac * fac * x[i] * x[i]; res2 += Math.cos(2. * Math.PI * fac * x[i]); } f = (20. - 20. * Math.exp(-0.2 * Math.sqrt(f / x.length)) + Math.exp(1.) - Math.exp(res2 / x.length)); return f; } } private static class Rastrigin implements MultivariateFunction { private double axisratio; private double amplitude; Rastrigin() { this(1, 10); } Rastrigin(double axisratio, double amplitude) { this.axisratio = axisratio; this.amplitude = amplitude; } public double value(double[] x) { double f = 0; double fac; for (int i = 0; i < x.length; ++i) { fac = Math.pow(axisratio, (i - 1.) / (x.length - 1.)); if (i == 0 && x[i] < 0) fac *= 1.; f += fac * fac * x[i] * x[i] + amplitude * (1. - Math.cos(2. * Math.PI * fac * x[i])); } return f; } } private static class Basis { double[][] basis; Random rand = new Random(2); // use not always the same basis double[] Rotate(double[] x) { GenBasis(x.length); double[] y = new double[x.length]; for (int i = 0; i < x.length; ++i) { y[i] = 0; for (int j = 0; j < x.length; ++j) y[i] += basis[i][j] * x[j]; } return y; } void GenBasis(int DIM) { if (basis != null ? basis.length == DIM : false) return; double sp; int i, j, k; /* generate orthogonal basis */ basis = new double[DIM][DIM]; for (i = 0; i < DIM; ++i) { /* sample components gaussian */ for (j = 0; j < DIM; ++j) basis[i][j] = rand.nextGaussian(); /* substract projection of previous vectors */ for (j = i - 1; j >= 0; --j) { for (sp = 0., k = 0; k < DIM; ++k) sp += basis[i][k] * basis[j][k]; /* scalar product */ for (k = 0; k < DIM; ++k) basis[i][k] -= sp * basis[j][k]; /* substract */ } /* normalize */ for (sp = 0., k = 0; k < DIM; ++k) sp += basis[i][k] * basis[i][k]; /* squared norm */ for (k = 0; k < DIM; ++k) basis[i][k] /= Math.sqrt(sp); } } } } ././@LongLink100644 0 0 206 12126630646 10257 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/optim/nonlinear/scalar/noderiv/SimplexOptimizerMultiDirectionalTest.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/optim/nonlinear/scalar/noderiv/SimplexO100644 1750 1750 27116 12126627671 32323 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.optim.nonlinear.scalar.noderiv; import org.apache.commons.math3.analysis.MultivariateFunction; import org.apache.commons.math3.optim.MaxEval; import org.apache.commons.math3.optim.InitialGuess; import org.apache.commons.math3.optim.PointValuePair; import org.apache.commons.math3.optim.SimpleValueChecker; import org.apache.commons.math3.optim.SimpleBounds; import org.apache.commons.math3.optim.nonlinear.scalar.GoalType; import org.apache.commons.math3.optim.nonlinear.scalar.ObjectiveFunction; import org.apache.commons.math3.exception.MathUnsupportedOperationException; import org.apache.commons.math3.util.FastMath; import org.junit.Assert; import org.junit.Test; public class SimplexOptimizerMultiDirectionalTest { @Test(expected=MathUnsupportedOperationException.class) public void testBoundsUnsupported() { SimplexOptimizer optimizer = new SimplexOptimizer(1e-10, 1e-30); final FourExtrema fourExtrema = new FourExtrema(); optimizer.optimize(new MaxEval(100), new ObjectiveFunction(fourExtrema), GoalType.MINIMIZE, new InitialGuess(new double[] { -3, 0 }), new NelderMeadSimplex(new double[] { 0.2, 0.2 }), new SimpleBounds(new double[] { -5, -1 }, new double[] { 5, 1 })); } @Test public void testMinimize1() { SimplexOptimizer optimizer = new SimplexOptimizer(1e-11, 1e-30); final FourExtrema fourExtrema = new FourExtrema(); final PointValuePair optimum = optimizer.optimize(new MaxEval(200), new ObjectiveFunction(fourExtrema), GoalType.MINIMIZE, new InitialGuess(new double[] { -3, 0 }), new MultiDirectionalSimplex(new double[] { 0.2, 0.2 })); Assert.assertEquals(fourExtrema.xM, optimum.getPoint()[0], 4e-6); Assert.assertEquals(fourExtrema.yP, optimum.getPoint()[1], 3e-6); Assert.assertEquals(fourExtrema.valueXmYp, optimum.getValue(), 8e-13); Assert.assertTrue(optimizer.getEvaluations() > 120); Assert.assertTrue(optimizer.getEvaluations() < 150); // Check that the number of iterations is updated (MATH-949). Assert.assertTrue(optimizer.getIterations() > 0); } @Test public void testMinimize2() { SimplexOptimizer optimizer = new SimplexOptimizer(1e-11, 1e-30); final FourExtrema fourExtrema = new FourExtrema(); final PointValuePair optimum = optimizer.optimize(new MaxEval(200), new ObjectiveFunction(fourExtrema), GoalType.MINIMIZE, new InitialGuess(new double[] { 1, 0 }), new MultiDirectionalSimplex(new double[] { 0.2, 0.2 })); Assert.assertEquals(fourExtrema.xP, optimum.getPoint()[0], 2e-8); Assert.assertEquals(fourExtrema.yM, optimum.getPoint()[1], 3e-6); Assert.assertEquals(fourExtrema.valueXpYm, optimum.getValue(), 2e-12); Assert.assertTrue(optimizer.getEvaluations() > 120); Assert.assertTrue(optimizer.getEvaluations() < 150); // Check that the number of iterations is updated (MATH-949). Assert.assertTrue(optimizer.getIterations() > 0); } @Test public void testMaximize1() { SimplexOptimizer optimizer = new SimplexOptimizer(1e-11, 1e-30); final FourExtrema fourExtrema = new FourExtrema(); final PointValuePair optimum = optimizer.optimize(new MaxEval(200), new ObjectiveFunction(fourExtrema), GoalType.MAXIMIZE, new InitialGuess(new double[] { -3.0, 0.0 }), new MultiDirectionalSimplex(new double[] { 0.2, 0.2 })); Assert.assertEquals(fourExtrema.xM, optimum.getPoint()[0], 7e-7); Assert.assertEquals(fourExtrema.yM, optimum.getPoint()[1], 3e-7); Assert.assertEquals(fourExtrema.valueXmYm, optimum.getValue(), 2e-14); Assert.assertTrue(optimizer.getEvaluations() > 120); Assert.assertTrue(optimizer.getEvaluations() < 150); // Check that the number of iterations is updated (MATH-949). Assert.assertTrue(optimizer.getIterations() > 0); } @Test public void testMaximize2() { SimplexOptimizer optimizer = new SimplexOptimizer(new SimpleValueChecker(1e-15, 1e-30)); final FourExtrema fourExtrema = new FourExtrema(); final PointValuePair optimum = optimizer.optimize(new MaxEval(200), new ObjectiveFunction(fourExtrema), GoalType.MAXIMIZE, new InitialGuess(new double[] { 1, 0 }), new MultiDirectionalSimplex(new double[] { 0.2, 0.2 })); Assert.assertEquals(fourExtrema.xP, optimum.getPoint()[0], 2e-8); Assert.assertEquals(fourExtrema.yP, optimum.getPoint()[1], 3e-6); Assert.assertEquals(fourExtrema.valueXpYp, optimum.getValue(), 2e-12); Assert.assertTrue(optimizer.getEvaluations() > 180); Assert.assertTrue(optimizer.getEvaluations() < 220); // Check that the number of iterations is updated (MATH-949). Assert.assertTrue(optimizer.getIterations() > 0); } @Test public void testRosenbrock() { MultivariateFunction rosenbrock = new MultivariateFunction() { public double value(double[] x) { ++count; double a = x[1] - x[0] * x[0]; double b = 1.0 - x[0]; return 100 * a * a + b * b; } }; count = 0; SimplexOptimizer optimizer = new SimplexOptimizer(-1, 1e-3); PointValuePair optimum = optimizer.optimize(new MaxEval(100), new ObjectiveFunction(rosenbrock), GoalType.MINIMIZE, new InitialGuess(new double[] { -1.2, 1 }), new MultiDirectionalSimplex(new double[][] { { -1.2, 1.0 }, { 0.9, 1.2 }, { 3.5, -2.3 } })); Assert.assertEquals(count, optimizer.getEvaluations()); Assert.assertTrue(optimizer.getEvaluations() > 50); Assert.assertTrue(optimizer.getEvaluations() < 100); Assert.assertTrue(optimum.getValue() > 1e-2); } @Test public void testPowell() { MultivariateFunction powell = new MultivariateFunction() { public double value(double[] x) { ++count; double a = x[0] + 10 * x[1]; double b = x[2] - x[3]; double c = x[1] - 2 * x[2]; double d = x[0] - x[3]; return a * a + 5 * b * b + c * c * c * c + 10 * d * d * d * d; } }; count = 0; SimplexOptimizer optimizer = new SimplexOptimizer(-1, 1e-3); PointValuePair optimum = optimizer.optimize(new MaxEval(1000), new ObjectiveFunction(powell), GoalType.MINIMIZE, new InitialGuess(new double[] { 3, -1, 0, 1 }), new MultiDirectionalSimplex(4)); Assert.assertEquals(count, optimizer.getEvaluations()); Assert.assertTrue(optimizer.getEvaluations() > 800); Assert.assertTrue(optimizer.getEvaluations() < 900); Assert.assertTrue(optimum.getValue() > 1e-2); } @Test public void testMath283() { // fails because MultiDirectional.iterateSimplex is looping forever // the while(true) should be replaced with a convergence check SimplexOptimizer optimizer = new SimplexOptimizer(1e-14, 1e-14); final Gaussian2D function = new Gaussian2D(0, 0, 1); PointValuePair estimate = optimizer.optimize(new MaxEval(1000), new ObjectiveFunction(function), GoalType.MAXIMIZE, new InitialGuess(function.getMaximumPosition()), new MultiDirectionalSimplex(2)); final double EPSILON = 1e-5; final double expectedMaximum = function.getMaximum(); final double actualMaximum = estimate.getValue(); Assert.assertEquals(expectedMaximum, actualMaximum, EPSILON); final double[] expectedPosition = function.getMaximumPosition(); final double[] actualPosition = estimate.getPoint(); Assert.assertEquals(expectedPosition[0], actualPosition[0], EPSILON ); Assert.assertEquals(expectedPosition[1], actualPosition[1], EPSILON ); } private static class FourExtrema implements MultivariateFunction { // The following function has 4 local extrema. final double xM = -3.841947088256863675365; final double yM = -1.391745200270734924416; final double xP = 0.2286682237349059125691; final double yP = -yM; final double valueXmYm = 0.2373295333134216789769; // Local maximum. final double valueXmYp = -valueXmYm; // Local minimum. final double valueXpYm = -0.7290400707055187115322; // Global minimum. final double valueXpYp = -valueXpYm; // Global maximum. public double value(double[] variables) { final double x = variables[0]; final double y = variables[1]; return (x == 0 || y == 0) ? 0 : FastMath.atan(x) * FastMath.atan(x + 2) * FastMath.atan(y) * FastMath.atan(y) / (x * y); } } private static class Gaussian2D implements MultivariateFunction { private final double[] maximumPosition; private final double std; public Gaussian2D(double xOpt, double yOpt, double std) { maximumPosition = new double[] { xOpt, yOpt }; this.std = std; } public double getMaximum() { return value(maximumPosition); } public double[] getMaximumPosition() { return maximumPosition.clone(); } public double value(double[] point) { final double x = point[0], y = point[1]; final double twoS2 = 2.0 * std * std; return 1.0 / (twoS2 * FastMath.PI) * FastMath.exp(-(x * x + y * y) / twoS2); } } private int count; } ././@LongLink100644 0 0 165 12126630646 10263 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/optim/nonlinear/scalar/noderiv/PowellOptimizerTest.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/optim/nonlinear/scalar/noderiv/PowellOp100644 1750 1750 24633 12126627671 32325 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.optim.nonlinear.scalar.noderiv; import org.apache.commons.math3.analysis.MultivariateFunction; import org.apache.commons.math3.analysis.SumSincFunction; import org.apache.commons.math3.optim.PointValuePair; import org.apache.commons.math3.optim.InitialGuess; import org.apache.commons.math3.optim.MaxEval; import org.apache.commons.math3.optim.SimpleBounds; import org.apache.commons.math3.optim.nonlinear.scalar.GoalType; import org.apache.commons.math3.optim.nonlinear.scalar.ObjectiveFunction; import org.apache.commons.math3.exception.MathUnsupportedOperationException; import org.apache.commons.math3.util.FastMath; import org.junit.Assert; import org.junit.Test; /** * Test for {@link PowellOptimizer}. */ public class PowellOptimizerTest { @Test(expected=MathUnsupportedOperationException.class) public void testBoundsUnsupported() { final MultivariateFunction func = new SumSincFunction(-1); final PowellOptimizer optim = new PowellOptimizer(1e-8, 1e-5, 1e-4, 1e-4); optim.optimize(new MaxEval(100), new ObjectiveFunction(func), GoalType.MINIMIZE, new InitialGuess(new double[] { -3, 0 }), new SimpleBounds(new double[] { -5, -1 }, new double[] { 5, 1 })); } @Test public void testSumSinc() { final MultivariateFunction func = new SumSincFunction(-1); int dim = 2; final double[] minPoint = new double[dim]; for (int i = 0; i < dim; i++) { minPoint[i] = 0; } double[] init = new double[dim]; // Initial is minimum. for (int i = 0; i < dim; i++) { init[i] = minPoint[i]; } doTest(func, minPoint, init, GoalType.MINIMIZE, 1e-9, 1e-9); // Initial is far from minimum. for (int i = 0; i < dim; i++) { init[i] = minPoint[i] + 3; } doTest(func, minPoint, init, GoalType.MINIMIZE, 1e-9, 1e-5); // More stringent line search tolerance enhances the precision // of the result. doTest(func, minPoint, init, GoalType.MINIMIZE, 1e-9, 1e-9, 1e-7); } @Test public void testQuadratic() { final MultivariateFunction func = new MultivariateFunction() { public double value(double[] x) { final double a = x[0] - 1; final double b = x[1] - 1; return a * a + b * b + 1; } }; int dim = 2; final double[] minPoint = new double[dim]; for (int i = 0; i < dim; i++) { minPoint[i] = 1; } double[] init = new double[dim]; // Initial is minimum. for (int i = 0; i < dim; i++) { init[i] = minPoint[i]; } doTest(func, minPoint, init, GoalType.MINIMIZE, 1e-9, 1e-8); // Initial is far from minimum. for (int i = 0; i < dim; i++) { init[i] = minPoint[i] - 20; } doTest(func, minPoint, init, GoalType.MINIMIZE, 1e-9, 1e-8); } @Test public void testMaximizeQuadratic() { final MultivariateFunction func = new MultivariateFunction() { public double value(double[] x) { final double a = x[0] - 1; final double b = x[1] - 1; return -a * a - b * b + 1; } }; int dim = 2; final double[] maxPoint = new double[dim]; for (int i = 0; i < dim; i++) { maxPoint[i] = 1; } double[] init = new double[dim]; // Initial is minimum. for (int i = 0; i < dim; i++) { init[i] = maxPoint[i]; } doTest(func, maxPoint, init, GoalType.MAXIMIZE, 1e-9, 1e-8); // Initial is far from minimum. for (int i = 0; i < dim; i++) { init[i] = maxPoint[i] - 20; } doTest(func, maxPoint, init, GoalType.MAXIMIZE, 1e-9, 1e-8); } /** * Ensure that we do not increase the number of function evaluations when * the function values are scaled up. * Note that the tolerances parameters passed to the constructor must * still hold sensible values because they are used to set the line search * tolerances. */ @Test public void testRelativeToleranceOnScaledValues() { final MultivariateFunction func = new MultivariateFunction() { public double value(double[] x) { final double a = x[0] - 1; final double b = x[1] - 1; return a * a * FastMath.sqrt(FastMath.abs(a)) + b * b + 1; } }; int dim = 2; final double[] minPoint = new double[dim]; for (int i = 0; i < dim; i++) { minPoint[i] = 1; } double[] init = new double[dim]; // Initial is far from minimum. for (int i = 0; i < dim; i++) { init[i] = minPoint[i] - 20; } final double relTol = 1e-10; final int maxEval = 1000; // Very small absolute tolerance to rely solely on the relative // tolerance as a stopping criterion final PowellOptimizer optim = new PowellOptimizer(relTol, 1e-100); final PointValuePair funcResult = optim.optimize(new MaxEval(maxEval), new ObjectiveFunction(func), GoalType.MINIMIZE, new InitialGuess(init)); final double funcValue = func.value(funcResult.getPoint()); final int funcEvaluations = optim.getEvaluations(); final double scale = 1e10; final MultivariateFunction funcScaled = new MultivariateFunction() { public double value(double[] x) { return scale * func.value(x); } }; final PointValuePair funcScaledResult = optim.optimize(new MaxEval(maxEval), new ObjectiveFunction(funcScaled), GoalType.MINIMIZE, new InitialGuess(init)); final double funcScaledValue = funcScaled.value(funcScaledResult.getPoint()); final int funcScaledEvaluations = optim.getEvaluations(); // Check that both minima provide the same objective funciton values, // within the relative function tolerance. Assert.assertEquals(1, funcScaledValue / (scale * funcValue), relTol); // Check that the numbers of evaluations are the same. Assert.assertEquals(funcEvaluations, funcScaledEvaluations); } /** * @param func Function to optimize. * @param optimum Expected optimum. * @param init Starting point. * @param goal Minimization or maximization. * @param fTol Tolerance (relative error on the objective function) for * "Powell" algorithm. * @param pointTol Tolerance for checking that the optimum is correct. */ private void doTest(MultivariateFunction func, double[] optimum, double[] init, GoalType goal, double fTol, double pointTol) { final PowellOptimizer optim = new PowellOptimizer(fTol, Math.ulp(1d)); final PointValuePair result = optim.optimize(new MaxEval(1000), new ObjectiveFunction(func), goal, new InitialGuess(init)); final double[] point = result.getPoint(); for (int i = 0, dim = optimum.length; i < dim; i++) { Assert.assertEquals("found[" + i + "]=" + point[i] + " value=" + result.getValue(), optimum[i], point[i], pointTol); } } /** * @param func Function to optimize. * @param optimum Expected optimum. * @param init Starting point. * @param goal Minimization or maximization. * @param fTol Tolerance (relative error on the objective function) for * "Powell" algorithm. * @param fLineTol Tolerance (relative error on the objective function) * for the internal line search algorithm. * @param pointTol Tolerance for checking that the optimum is correct. */ private void doTest(MultivariateFunction func, double[] optimum, double[] init, GoalType goal, double fTol, double fLineTol, double pointTol) { final PowellOptimizer optim = new PowellOptimizer(fTol, Math.ulp(1d), fLineTol, Math.ulp(1d)); final PointValuePair result = optim.optimize(new MaxEval(1000), new ObjectiveFunction(func), goal, new InitialGuess(init)); final double[] point = result.getPoint(); for (int i = 0, dim = optimum.length; i < dim; i++) { Assert.assertEquals("found[" + i + "]=" + point[i] + " value=" + result.getValue(), optimum[i], point[i], pointTol); } Assert.assertTrue(optim.getIterations() > 0); } } ././@LongLink100644 0 0 157 12126630646 10264 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/optim/nonlinear/scalar/gradient/CircleScalar.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/optim/nonlinear/scalar/gradient/CircleS100644 1750 1750 6277 12126627671 32223 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.optim.nonlinear.scalar.gradient; import java.util.ArrayList; import org.apache.commons.math3.analysis.MultivariateFunction; import org.apache.commons.math3.analysis.MultivariateVectorFunction; import org.apache.commons.math3.geometry.euclidean.twod.Vector2D; import org.apache.commons.math3.optim.nonlinear.scalar.ObjectiveFunction; import org.apache.commons.math3.optim.nonlinear.scalar.ObjectiveFunctionGradient; /** * Class used in the tests. */ public class CircleScalar { private ArrayList points; public CircleScalar() { points = new ArrayList(); } public void addPoint(double px, double py) { points.add(new Vector2D(px, py)); } public double getRadius(Vector2D center) { double r = 0; for (Vector2D point : points) { r += point.distance(center); } return r / points.size(); } public ObjectiveFunction getObjectiveFunction() { return new ObjectiveFunction(new MultivariateFunction() { public double value(double[] params) { Vector2D center = new Vector2D(params[0], params[1]); double radius = getRadius(center); double sum = 0; for (Vector2D point : points) { double di = point.distance(center) - radius; sum += di * di; } return sum; } }); } public ObjectiveFunctionGradient getObjectiveFunctionGradient() { return new ObjectiveFunctionGradient(new MultivariateVectorFunction() { public double[] value(double[] params) { Vector2D center = new Vector2D(params[0], params[1]); double radius = getRadius(center); // gradient of the sum of squared residuals double dJdX = 0; double dJdY = 0; for (Vector2D pk : points) { double dk = pk.distance(center); dJdX += (center.getX() - pk.getX()) * (dk - radius) / dk; dJdY += (center.getY() - pk.getY()) * (dk - radius) / dk; } dJdX *= 2; dJdY *= 2; return new double[] { dJdX, dJdY }; } }); } } ././@LongLink100644 0 0 212 12126630646 10254 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/optim/nonlinear/scalar/gradient/NonLinearConjugateGradientOptimizerTest.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/optim/nonlinear/scalar/gradient/NonLine100644 1750 1750 55267 12126627671 32264 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.optim.nonlinear.scalar.gradient; import java.io.Serializable; import org.apache.commons.math3.analysis.DifferentiableMultivariateFunction; import org.apache.commons.math3.analysis.MultivariateFunction; import org.apache.commons.math3.analysis.MultivariateVectorFunction; import org.apache.commons.math3.analysis.differentiation.DerivativeStructure; import org.apache.commons.math3.analysis.differentiation.MultivariateDifferentiableFunction; import org.apache.commons.math3.analysis.solvers.BrentSolver; import org.apache.commons.math3.exception.DimensionMismatchException; import org.apache.commons.math3.exception.MathIllegalArgumentException; import org.apache.commons.math3.exception.MathUnsupportedOperationException; import org.apache.commons.math3.geometry.euclidean.twod.Vector2D; import org.apache.commons.math3.linear.BlockRealMatrix; import org.apache.commons.math3.linear.RealMatrix; import org.apache.commons.math3.optim.PointValuePair; import org.apache.commons.math3.optim.SimpleValueChecker; import org.apache.commons.math3.optim.InitialGuess; import org.apache.commons.math3.optim.MaxEval; import org.apache.commons.math3.optim.SimpleBounds; import org.apache.commons.math3.optim.nonlinear.scalar.ObjectiveFunction; import org.apache.commons.math3.optim.nonlinear.scalar.GoalType; import org.apache.commons.math3.optim.nonlinear.scalar.ObjectiveFunctionGradient; import org.junit.Assert; import org.junit.Test; /** *

                        Some of the unit tests are re-implementations of the MINPACK file17 and file22 test files. * The redistribution policy for MINPACK is available here, for * convenience, it is reproduced below.

                        * * * * *
                        * Minpack Copyright Notice (1999) University of Chicago. * All rights reserved *
                        * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: *
                          *
                        1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer.
                        2. *
                        3. Redistributions in binary form must reproduce the above * copyright notice, this list of conditions and the following * disclaimer in the documentation and/or other materials provided * with the distribution.
                        4. *
                        5. The end-user documentation included with the redistribution, if any, * must include the following acknowledgment: * This product includes software developed by the University of * Chicago, as Operator of Argonne National Laboratory. * Alternately, this acknowledgment may appear in the software itself, * if and wherever such third-party acknowledgments normally appear.
                        6. *
                        7. WARRANTY DISCLAIMER. THE SOFTWARE IS SUPPLIED "AS IS" * WITHOUT WARRANTY OF ANY KIND. THE COPYRIGHT HOLDER, THE * UNITED STATES, THE UNITED STATES DEPARTMENT OF ENERGY, AND * THEIR EMPLOYEES: (1) DISCLAIM ANY WARRANTIES, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO ANY IMPLIED WARRANTIES * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE * OR NON-INFRINGEMENT, (2) DO NOT ASSUME ANY LEGAL LIABILITY * OR RESPONSIBILITY FOR THE ACCURACY, COMPLETENESS, OR * USEFULNESS OF THE SOFTWARE, (3) DO NOT REPRESENT THAT USE OF * THE SOFTWARE WOULD NOT INFRINGE PRIVATELY OWNED RIGHTS, (4) * DO NOT WARRANT THAT THE SOFTWARE WILL FUNCTION * UNINTERRUPTED, THAT IT IS ERROR-FREE OR THAT ANY ERRORS WILL * BE CORRECTED.
                        8. *
                        9. LIMITATION OF LIABILITY. IN NO EVENT WILL THE COPYRIGHT * HOLDER, THE UNITED STATES, THE UNITED STATES DEPARTMENT OF * ENERGY, OR THEIR EMPLOYEES: BE LIABLE FOR ANY INDIRECT, * INCIDENTAL, CONSEQUENTIAL, SPECIAL OR PUNITIVE DAMAGES OF * ANY KIND OR NATURE, INCLUDING BUT NOT LIMITED TO LOSS OF * PROFITS OR LOSS OF DATA, FOR ANY REASON WHATSOEVER, WHETHER * SUCH LIABILITY IS ASSERTED ON THE BASIS OF CONTRACT, TORT * (INCLUDING NEGLIGENCE OR STRICT LIABILITY), OR OTHERWISE, * EVEN IF ANY OF SAID PARTIES HAS BEEN WARNED OF THE * POSSIBILITY OF SUCH LOSS OR DAMAGES.
                        10. *
                          * * @author Argonne National Laboratory. MINPACK project. March 1980 (original fortran minpack tests) * @author Burton S. Garbow (original fortran minpack tests) * @author Kenneth E. Hillstrom (original fortran minpack tests) * @author Jorge J. More (original fortran minpack tests) * @author Luc Maisonobe (non-minpack tests and minpack tests Java translation) */ public class NonLinearConjugateGradientOptimizerTest { @Test(expected=MathUnsupportedOperationException.class) public void testBoundsUnsupported() { LinearProblem problem = new LinearProblem(new double[][] { { 2 } }, new double[] { 3 }); NonLinearConjugateGradientOptimizer optimizer = new NonLinearConjugateGradientOptimizer(NonLinearConjugateGradientOptimizer.Formula.POLAK_RIBIERE, new SimpleValueChecker(1e-6, 1e-6)); optimizer.optimize(new MaxEval(100), problem.getObjectiveFunction(), problem.getObjectiveFunctionGradient(), GoalType.MINIMIZE, new InitialGuess(new double[] { 0 }), new SimpleBounds(new double[] { -1 }, new double[] { 1 })); } @Test public void testTrivial() { LinearProblem problem = new LinearProblem(new double[][] { { 2 } }, new double[] { 3 }); NonLinearConjugateGradientOptimizer optimizer = new NonLinearConjugateGradientOptimizer(NonLinearConjugateGradientOptimizer.Formula.POLAK_RIBIERE, new SimpleValueChecker(1e-6, 1e-6)); PointValuePair optimum = optimizer.optimize(new MaxEval(100), problem.getObjectiveFunction(), problem.getObjectiveFunctionGradient(), GoalType.MINIMIZE, new InitialGuess(new double[] { 0 })); Assert.assertEquals(1.5, optimum.getPoint()[0], 1.0e-10); Assert.assertEquals(0.0, optimum.getValue(), 1.0e-10); // Check that the number of iterations is updated (MATH-949). Assert.assertTrue(optimizer.getIterations() > 0); } @Test public void testColumnsPermutation() { LinearProblem problem = new LinearProblem(new double[][] { { 1.0, -1.0 }, { 0.0, 2.0 }, { 1.0, -2.0 } }, new double[] { 4.0, 6.0, 1.0 }); NonLinearConjugateGradientOptimizer optimizer = new NonLinearConjugateGradientOptimizer(NonLinearConjugateGradientOptimizer.Formula.POLAK_RIBIERE, new SimpleValueChecker(1e-6, 1e-6)); PointValuePair optimum = optimizer.optimize(new MaxEval(100), problem.getObjectiveFunction(), problem.getObjectiveFunctionGradient(), GoalType.MINIMIZE, new InitialGuess(new double[] { 0, 0 })); Assert.assertEquals(7.0, optimum.getPoint()[0], 1.0e-10); Assert.assertEquals(3.0, optimum.getPoint()[1], 1.0e-10); Assert.assertEquals(0.0, optimum.getValue(), 1.0e-10); } @Test public void testNoDependency() { LinearProblem problem = new LinearProblem(new double[][] { { 2, 0, 0, 0, 0, 0 }, { 0, 2, 0, 0, 0, 0 }, { 0, 0, 2, 0, 0, 0 }, { 0, 0, 0, 2, 0, 0 }, { 0, 0, 0, 0, 2, 0 }, { 0, 0, 0, 0, 0, 2 } }, new double[] { 0.0, 1.1, 2.2, 3.3, 4.4, 5.5 }); NonLinearConjugateGradientOptimizer optimizer = new NonLinearConjugateGradientOptimizer(NonLinearConjugateGradientOptimizer.Formula.POLAK_RIBIERE, new SimpleValueChecker(1e-6, 1e-6)); PointValuePair optimum = optimizer.optimize(new MaxEval(100), problem.getObjectiveFunction(), problem.getObjectiveFunctionGradient(), GoalType.MINIMIZE, new InitialGuess(new double[] { 0, 0, 0, 0, 0, 0 })); for (int i = 0; i < problem.target.length; ++i) { Assert.assertEquals(0.55 * i, optimum.getPoint()[i], 1.0e-10); } } @Test public void testOneSet() { LinearProblem problem = new LinearProblem(new double[][] { { 1, 0, 0 }, { -1, 1, 0 }, { 0, -1, 1 } }, new double[] { 1, 1, 1}); NonLinearConjugateGradientOptimizer optimizer = new NonLinearConjugateGradientOptimizer(NonLinearConjugateGradientOptimizer.Formula.POLAK_RIBIERE, new SimpleValueChecker(1e-6, 1e-6)); PointValuePair optimum = optimizer.optimize(new MaxEval(100), problem.getObjectiveFunction(), problem.getObjectiveFunctionGradient(), GoalType.MINIMIZE, new InitialGuess(new double[] { 0, 0, 0 })); Assert.assertEquals(1.0, optimum.getPoint()[0], 1.0e-10); Assert.assertEquals(2.0, optimum.getPoint()[1], 1.0e-10); Assert.assertEquals(3.0, optimum.getPoint()[2], 1.0e-10); } @Test public void testTwoSets() { final double epsilon = 1.0e-7; LinearProblem problem = new LinearProblem(new double[][] { { 2, 1, 0, 4, 0, 0 }, { -4, -2, 3, -7, 0, 0 }, { 4, 1, -2, 8, 0, 0 }, { 0, -3, -12, -1, 0, 0 }, { 0, 0, 0, 0, epsilon, 1 }, { 0, 0, 0, 0, 1, 1 } }, new double[] { 2, -9, 2, 2, 1 + epsilon * epsilon, 2}); final Preconditioner preconditioner = new Preconditioner() { public double[] precondition(double[] point, double[] r) { double[] d = r.clone(); d[0] /= 72.0; d[1] /= 30.0; d[2] /= 314.0; d[3] /= 260.0; d[4] /= 2 * (1 + epsilon * epsilon); d[5] /= 4.0; return d; } }; NonLinearConjugateGradientOptimizer optimizer = new NonLinearConjugateGradientOptimizer(NonLinearConjugateGradientOptimizer.Formula.POLAK_RIBIERE, new SimpleValueChecker(1e-13, 1e-13), new BrentSolver(), preconditioner); PointValuePair optimum = optimizer.optimize(new MaxEval(100), problem.getObjectiveFunction(), problem.getObjectiveFunctionGradient(), GoalType.MINIMIZE, new InitialGuess(new double[] { 0, 0, 0, 0, 0, 0 })); Assert.assertEquals( 3.0, optimum.getPoint()[0], 1.0e-10); Assert.assertEquals( 4.0, optimum.getPoint()[1], 1.0e-10); Assert.assertEquals(-1.0, optimum.getPoint()[2], 1.0e-10); Assert.assertEquals(-2.0, optimum.getPoint()[3], 1.0e-10); Assert.assertEquals( 1.0 + epsilon, optimum.getPoint()[4], 1.0e-10); Assert.assertEquals( 1.0 - epsilon, optimum.getPoint()[5], 1.0e-10); } @Test public void testNonInversible() { LinearProblem problem = new LinearProblem(new double[][] { { 1, 2, -3 }, { 2, 1, 3 }, { -3, 0, -9 } }, new double[] { 1, 1, 1 }); NonLinearConjugateGradientOptimizer optimizer = new NonLinearConjugateGradientOptimizer(NonLinearConjugateGradientOptimizer.Formula.POLAK_RIBIERE, new SimpleValueChecker(1e-6, 1e-6)); PointValuePair optimum = optimizer.optimize(new MaxEval(100), problem.getObjectiveFunction(), problem.getObjectiveFunctionGradient(), GoalType.MINIMIZE, new InitialGuess(new double[] { 0, 0, 0 })); Assert.assertTrue(optimum.getValue() > 0.5); } @Test public void testIllConditioned() { LinearProblem problem1 = new LinearProblem(new double[][] { { 10.0, 7.0, 8.0, 7.0 }, { 7.0, 5.0, 6.0, 5.0 }, { 8.0, 6.0, 10.0, 9.0 }, { 7.0, 5.0, 9.0, 10.0 } }, new double[] { 32, 23, 33, 31 }); NonLinearConjugateGradientOptimizer optimizer = new NonLinearConjugateGradientOptimizer(NonLinearConjugateGradientOptimizer.Formula.POLAK_RIBIERE, new SimpleValueChecker(1e-13, 1e-13), new BrentSolver(1e-15, 1e-15)); PointValuePair optimum1 = optimizer.optimize(new MaxEval(200), problem1.getObjectiveFunction(), problem1.getObjectiveFunctionGradient(), GoalType.MINIMIZE, new InitialGuess(new double[] { 0, 1, 2, 3 })); Assert.assertEquals(1.0, optimum1.getPoint()[0], 1.0e-4); Assert.assertEquals(1.0, optimum1.getPoint()[1], 1.0e-4); Assert.assertEquals(1.0, optimum1.getPoint()[2], 1.0e-4); Assert.assertEquals(1.0, optimum1.getPoint()[3], 1.0e-4); LinearProblem problem2 = new LinearProblem(new double[][] { { 10.00, 7.00, 8.10, 7.20 }, { 7.08, 5.04, 6.00, 5.00 }, { 8.00, 5.98, 9.89, 9.00 }, { 6.99, 4.99, 9.00, 9.98 } }, new double[] { 32, 23, 33, 31 }); PointValuePair optimum2 = optimizer.optimize(new MaxEval(200), problem2.getObjectiveFunction(), problem2.getObjectiveFunctionGradient(), GoalType.MINIMIZE, new InitialGuess(new double[] { 0, 1, 2, 3 })); Assert.assertEquals(-81.0, optimum2.getPoint()[0], 1.0e-1); Assert.assertEquals(137.0, optimum2.getPoint()[1], 1.0e-1); Assert.assertEquals(-34.0, optimum2.getPoint()[2], 1.0e-1); Assert.assertEquals( 22.0, optimum2.getPoint()[3], 1.0e-1); } @Test public void testMoreEstimatedParametersSimple() { LinearProblem problem = new LinearProblem(new double[][] { { 3.0, 2.0, 0.0, 0.0 }, { 0.0, 1.0, -1.0, 1.0 }, { 2.0, 0.0, 1.0, 0.0 } }, new double[] { 7.0, 3.0, 5.0 }); NonLinearConjugateGradientOptimizer optimizer = new NonLinearConjugateGradientOptimizer(NonLinearConjugateGradientOptimizer.Formula.POLAK_RIBIERE, new SimpleValueChecker(1e-6, 1e-6)); PointValuePair optimum = optimizer.optimize(new MaxEval(100), problem.getObjectiveFunction(), problem.getObjectiveFunctionGradient(), GoalType.MINIMIZE, new InitialGuess(new double[] { 7, 6, 5, 4 })); Assert.assertEquals(0, optimum.getValue(), 1.0e-10); } @Test public void testMoreEstimatedParametersUnsorted() { LinearProblem problem = new LinearProblem(new double[][] { { 1.0, 1.0, 0.0, 0.0, 0.0, 0.0 }, { 0.0, 0.0, 1.0, 1.0, 1.0, 0.0 }, { 0.0, 0.0, 0.0, 0.0, 1.0, -1.0 }, { 0.0, 0.0, -1.0, 1.0, 0.0, 1.0 }, { 0.0, 0.0, 0.0, -1.0, 1.0, 0.0 } }, new double[] { 3.0, 12.0, -1.0, 7.0, 1.0 }); NonLinearConjugateGradientOptimizer optimizer = new NonLinearConjugateGradientOptimizer(NonLinearConjugateGradientOptimizer.Formula.POLAK_RIBIERE, new SimpleValueChecker(1e-6, 1e-6)); PointValuePair optimum = optimizer.optimize(new MaxEval(100), problem.getObjectiveFunction(), problem.getObjectiveFunctionGradient(), GoalType.MINIMIZE, new InitialGuess(new double[] { 2, 2, 2, 2, 2, 2 })); Assert.assertEquals(0, optimum.getValue(), 1.0e-10); } @Test public void testRedundantEquations() { LinearProblem problem = new LinearProblem(new double[][] { { 1.0, 1.0 }, { 1.0, -1.0 }, { 1.0, 3.0 } }, new double[] { 3.0, 1.0, 5.0 }); NonLinearConjugateGradientOptimizer optimizer = new NonLinearConjugateGradientOptimizer(NonLinearConjugateGradientOptimizer.Formula.POLAK_RIBIERE, new SimpleValueChecker(1e-6, 1e-6)); PointValuePair optimum = optimizer.optimize(new MaxEval(100), problem.getObjectiveFunction(), problem.getObjectiveFunctionGradient(), GoalType.MINIMIZE, new InitialGuess(new double[] { 1, 1 })); Assert.assertEquals(2.0, optimum.getPoint()[0], 1.0e-8); Assert.assertEquals(1.0, optimum.getPoint()[1], 1.0e-8); } @Test public void testInconsistentEquations() { LinearProblem problem = new LinearProblem(new double[][] { { 1.0, 1.0 }, { 1.0, -1.0 }, { 1.0, 3.0 } }, new double[] { 3.0, 1.0, 4.0 }); NonLinearConjugateGradientOptimizer optimizer = new NonLinearConjugateGradientOptimizer(NonLinearConjugateGradientOptimizer.Formula.POLAK_RIBIERE, new SimpleValueChecker(1e-6, 1e-6)); PointValuePair optimum = optimizer.optimize(new MaxEval(100), problem.getObjectiveFunction(), problem.getObjectiveFunctionGradient(), GoalType.MINIMIZE, new InitialGuess(new double[] { 1, 1 })); Assert.assertTrue(optimum.getValue() > 0.1); } @Test public void testCircleFitting() { CircleScalar problem = new CircleScalar(); problem.addPoint( 30.0, 68.0); problem.addPoint( 50.0, -6.0); problem.addPoint(110.0, -20.0); problem.addPoint( 35.0, 15.0); problem.addPoint( 45.0, 97.0); NonLinearConjugateGradientOptimizer optimizer = new NonLinearConjugateGradientOptimizer(NonLinearConjugateGradientOptimizer.Formula.POLAK_RIBIERE, new SimpleValueChecker(1e-30, 1e-30), new BrentSolver(1e-15, 1e-13)); PointValuePair optimum = optimizer.optimize(new MaxEval(100), problem.getObjectiveFunction(), problem.getObjectiveFunctionGradient(), GoalType.MINIMIZE, new InitialGuess(new double[] { 98.680, 47.345 })); Vector2D center = new Vector2D(optimum.getPointRef()[0], optimum.getPointRef()[1]); Assert.assertEquals(69.960161753, problem.getRadius(center), 1.0e-8); Assert.assertEquals(96.075902096, center.getX(), 1.0e-8); Assert.assertEquals(48.135167894, center.getY(), 1.0e-8); } private static class LinearProblem { final RealMatrix factors; final double[] target; public LinearProblem(double[][] factors, double[] target) { this.factors = new BlockRealMatrix(factors); this.target = target; } public ObjectiveFunction getObjectiveFunction() { return new ObjectiveFunction(new MultivariateFunction() { public double value(double[] point) { double[] y = factors.operate(point); double sum = 0; for (int i = 0; i < y.length; ++i) { double ri = y[i] - target[i]; sum += ri * ri; } return sum; } }); } public ObjectiveFunctionGradient getObjectiveFunctionGradient() { return new ObjectiveFunctionGradient(new MultivariateVectorFunction() { public double[] value(double[] point) { double[] r = factors.operate(point); for (int i = 0; i < r.length; ++i) { r[i] -= target[i]; } double[] p = factors.transpose().operate(r); for (int i = 0; i < p.length; ++i) { p[i] *= 2; } return p; } }); } } } ././@LongLink100644 0 0 200 12126630646 10251 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/optim/nonlinear/scalar/MultivariateFunctionPenaltyAdapterTest.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/optim/nonlinear/scalar/MultivariateFunc100644 1750 1750 22563 12126627671 32400 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.optim.nonlinear.scalar; import org.apache.commons.math3.analysis.MultivariateFunction; import org.apache.commons.math3.optim.MaxEval; import org.apache.commons.math3.optim.InitialGuess; import org.apache.commons.math3.optim.PointValuePair; import org.apache.commons.math3.optim.SimplePointChecker; import org.apache.commons.math3.optim.nonlinear.scalar.noderiv.SimplexOptimizer; import org.apache.commons.math3.optim.nonlinear.scalar.noderiv.AbstractSimplex; import org.apache.commons.math3.optim.nonlinear.scalar.noderiv.NelderMeadSimplex; import org.junit.Assert; import org.junit.Test; public class MultivariateFunctionPenaltyAdapterTest { @Test public void testStartSimplexInsideRange() { final BiQuadratic biQuadratic = new BiQuadratic(2.0, 2.5, 1.0, 3.0, 2.0, 3.0); final MultivariateFunctionPenaltyAdapter wrapped = new MultivariateFunctionPenaltyAdapter(biQuadratic, biQuadratic.getLower(), biQuadratic.getUpper(), 1000.0, new double[] { 100.0, 100.0 }); SimplexOptimizer optimizer = new SimplexOptimizer(1e-10, 1e-30); final AbstractSimplex simplex = new NelderMeadSimplex(new double[] { 1.0, 0.5 }); final PointValuePair optimum = optimizer.optimize(new MaxEval(300), new ObjectiveFunction(wrapped), simplex, GoalType.MINIMIZE, new InitialGuess(new double[] { 1.5, 2.25 })); Assert.assertEquals(biQuadratic.getBoundedXOptimum(), optimum.getPoint()[0], 2e-7); Assert.assertEquals(biQuadratic.getBoundedYOptimum(), optimum.getPoint()[1], 2e-7); } @Test public void testStartSimplexOutsideRange() { final BiQuadratic biQuadratic = new BiQuadratic(2.0, 2.5, 1.0, 3.0, 2.0, 3.0); final MultivariateFunctionPenaltyAdapter wrapped = new MultivariateFunctionPenaltyAdapter(biQuadratic, biQuadratic.getLower(), biQuadratic.getUpper(), 1000.0, new double[] { 100.0, 100.0 }); SimplexOptimizer optimizer = new SimplexOptimizer(1e-10, 1e-30); final AbstractSimplex simplex = new NelderMeadSimplex(new double[] { 1.0, 0.5 }); final PointValuePair optimum = optimizer.optimize(new MaxEval(300), new ObjectiveFunction(wrapped), simplex, GoalType.MINIMIZE, new InitialGuess(new double[] { -1.5, 4.0 })); Assert.assertEquals(biQuadratic.getBoundedXOptimum(), optimum.getPoint()[0], 2e-7); Assert.assertEquals(biQuadratic.getBoundedYOptimum(), optimum.getPoint()[1], 2e-7); } @Test public void testOptimumOutsideRange() { final BiQuadratic biQuadratic = new BiQuadratic(4.0, 0.0, 1.0, 3.0, 2.0, 3.0); final MultivariateFunctionPenaltyAdapter wrapped = new MultivariateFunctionPenaltyAdapter(biQuadratic, biQuadratic.getLower(), biQuadratic.getUpper(), 1000.0, new double[] { 100.0, 100.0 }); SimplexOptimizer optimizer = new SimplexOptimizer(new SimplePointChecker(1.0e-11, 1.0e-20)); final AbstractSimplex simplex = new NelderMeadSimplex(new double[] { 1.0, 0.5 }); final PointValuePair optimum = optimizer.optimize(new MaxEval(600), new ObjectiveFunction(wrapped), simplex, GoalType.MINIMIZE, new InitialGuess(new double[] { -1.5, 4.0 })); Assert.assertEquals(biQuadratic.getBoundedXOptimum(), optimum.getPoint()[0], 2e-7); Assert.assertEquals(biQuadratic.getBoundedYOptimum(), optimum.getPoint()[1], 2e-7); } @Test public void testUnbounded() { final BiQuadratic biQuadratic = new BiQuadratic(4.0, 0.0, Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY, Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY); final MultivariateFunctionPenaltyAdapter wrapped = new MultivariateFunctionPenaltyAdapter(biQuadratic, biQuadratic.getLower(), biQuadratic.getUpper(), 1000.0, new double[] { 100.0, 100.0 }); SimplexOptimizer optimizer = new SimplexOptimizer(1e-10, 1e-30); final AbstractSimplex simplex = new NelderMeadSimplex(new double[] { 1.0, 0.5 }); final PointValuePair optimum = optimizer.optimize(new MaxEval(300), new ObjectiveFunction(wrapped), simplex, GoalType.MINIMIZE, new InitialGuess(new double[] { -1.5, 4.0 })); Assert.assertEquals(biQuadratic.getBoundedXOptimum(), optimum.getPoint()[0], 2e-7); Assert.assertEquals(biQuadratic.getBoundedYOptimum(), optimum.getPoint()[1], 2e-7); } @Test public void testHalfBounded() { final BiQuadratic biQuadratic = new BiQuadratic(4.0, 4.0, 1.0, Double.POSITIVE_INFINITY, Double.NEGATIVE_INFINITY, 3.0); final MultivariateFunctionPenaltyAdapter wrapped = new MultivariateFunctionPenaltyAdapter(biQuadratic, biQuadratic.getLower(), biQuadratic.getUpper(), 1000.0, new double[] { 100.0, 100.0 }); SimplexOptimizer optimizer = new SimplexOptimizer(new SimplePointChecker(1.0e-10, 1.0e-20)); final AbstractSimplex simplex = new NelderMeadSimplex(new double[] { 1.0, 0.5 }); final PointValuePair optimum = optimizer.optimize(new MaxEval(400), new ObjectiveFunction(wrapped), simplex, GoalType.MINIMIZE, new InitialGuess(new double[] { -1.5, 4.0 })); Assert.assertEquals(biQuadratic.getBoundedXOptimum(), optimum.getPoint()[0], 2e-7); Assert.assertEquals(biQuadratic.getBoundedYOptimum(), optimum.getPoint()[1], 2e-7); } private static class BiQuadratic implements MultivariateFunction { private final double xOptimum; private final double yOptimum; private final double xMin; private final double xMax; private final double yMin; private final double yMax; public BiQuadratic(final double xOptimum, final double yOptimum, final double xMin, final double xMax, final double yMin, final double yMax) { this.xOptimum = xOptimum; this.yOptimum = yOptimum; this.xMin = xMin; this.xMax = xMax; this.yMin = yMin; this.yMax = yMax; } public double value(double[] point) { // the function should never be called with out of range points Assert.assertTrue(point[0] >= xMin); Assert.assertTrue(point[0] <= xMax); Assert.assertTrue(point[1] >= yMin); Assert.assertTrue(point[1] <= yMax); final double dx = point[0] - xOptimum; final double dy = point[1] - yOptimum; return dx * dx + dy * dy; } public double[] getLower() { return new double[] { xMin, yMin }; } public double[] getUpper() { return new double[] { xMax, yMax }; } public double getBoundedXOptimum() { return (xOptimum < xMin) ? xMin : ((xOptimum > xMax) ? xMax : xOptimum); } public double getBoundedYOptimum() { return (yOptimum < yMin) ? yMin : ((yOptimum > yMax) ? yMax : yOptimum); } } } ././@LongLink100644 0 0 175 12126630646 10264 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/optim/nonlinear/scalar/MultiStartMultivariateOptimizerTest.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/optim/nonlinear/scalar/MultiStartMultiv100644 1750 1750 13567 12126627671 32433 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.optim.nonlinear.scalar; import org.apache.commons.math3.analysis.MultivariateFunction; import org.apache.commons.math3.geometry.euclidean.twod.Vector2D; import org.apache.commons.math3.optim.MaxEval; import org.apache.commons.math3.optim.InitialGuess; import org.apache.commons.math3.optim.PointValuePair; import org.apache.commons.math3.optim.SimpleValueChecker; import org.apache.commons.math3.optim.nonlinear.scalar.gradient.CircleScalar; import org.apache.commons.math3.optim.nonlinear.scalar.gradient.NonLinearConjugateGradientOptimizer; import org.apache.commons.math3.optim.nonlinear.scalar.noderiv.NelderMeadSimplex; import org.apache.commons.math3.optim.nonlinear.scalar.noderiv.SimplexOptimizer; import org.apache.commons.math3.random.GaussianRandomGenerator; import org.apache.commons.math3.random.JDKRandomGenerator; import org.apache.commons.math3.random.RandomVectorGenerator; import org.apache.commons.math3.random.UncorrelatedRandomVectorGenerator; import org.junit.Assert; import org.junit.Test; public class MultiStartMultivariateOptimizerTest { @Test public void testCircleFitting() { CircleScalar circle = new CircleScalar(); circle.addPoint( 30.0, 68.0); circle.addPoint( 50.0, -6.0); circle.addPoint(110.0, -20.0); circle.addPoint( 35.0, 15.0); circle.addPoint( 45.0, 97.0); // TODO: the wrapper around NonLinearConjugateGradientOptimizer is a temporary hack for // version 3.1 of the library. It should be removed when NonLinearConjugateGradientOptimizer // will officially be declared as implementing MultivariateDifferentiableOptimizer GradientMultivariateOptimizer underlying = new NonLinearConjugateGradientOptimizer(NonLinearConjugateGradientOptimizer.Formula.POLAK_RIBIERE, new SimpleValueChecker(1e-10, 1e-10)); JDKRandomGenerator g = new JDKRandomGenerator(); g.setSeed(753289573253l); RandomVectorGenerator generator = new UncorrelatedRandomVectorGenerator(new double[] { 50, 50 }, new double[] { 10, 10 }, new GaussianRandomGenerator(g)); MultiStartMultivariateOptimizer optimizer = new MultiStartMultivariateOptimizer(underlying, 10, generator); PointValuePair optimum = optimizer.optimize(new MaxEval(200), circle.getObjectiveFunction(), circle.getObjectiveFunctionGradient(), GoalType.MINIMIZE, new InitialGuess(new double[] { 98.680, 47.345 })); Assert.assertEquals(200, optimizer.getMaxEvaluations()); PointValuePair[] optima = optimizer.getOptima(); for (PointValuePair o : optima) { Vector2D center = new Vector2D(o.getPointRef()[0], o.getPointRef()[1]); Assert.assertEquals(69.960161753, circle.getRadius(center), 1e-8); Assert.assertEquals(96.075902096, center.getX(), 1e-8); Assert.assertEquals(48.135167894, center.getY(), 1e-8); } Assert.assertTrue(optimizer.getEvaluations() > 70); Assert.assertTrue(optimizer.getEvaluations() < 90); Assert.assertEquals(3.1267527, optimum.getValue(), 1e-8); } @Test public void testRosenbrock() { Rosenbrock rosenbrock = new Rosenbrock(); SimplexOptimizer underlying = new SimplexOptimizer(new SimpleValueChecker(-1, 1e-3)); NelderMeadSimplex simplex = new NelderMeadSimplex(new double[][] { { -1.2, 1.0 }, { 0.9, 1.2 } , { 3.5, -2.3 } }); JDKRandomGenerator g = new JDKRandomGenerator(); g.setSeed(16069223052l); RandomVectorGenerator generator = new UncorrelatedRandomVectorGenerator(2, new GaussianRandomGenerator(g)); MultiStartMultivariateOptimizer optimizer = new MultiStartMultivariateOptimizer(underlying, 10, generator); PointValuePair optimum = optimizer.optimize(new MaxEval(1100), new ObjectiveFunction(rosenbrock), GoalType.MINIMIZE, simplex, new InitialGuess(new double[] { -1.2, 1.0 })); Assert.assertEquals(rosenbrock.getCount(), optimizer.getEvaluations()); Assert.assertTrue(optimizer.getEvaluations() > 900); Assert.assertTrue(optimizer.getEvaluations() < 1200); Assert.assertTrue(optimum.getValue() < 8e-4); } private static class Rosenbrock implements MultivariateFunction { private int count; public Rosenbrock() { count = 0; } public double value(double[] x) { ++count; double a = x[1] - x[0] * x[0]; double b = 1 - x[0]; return 100 * a * a + b * b; } public int getCount() { return count; } } } ././@LongLink100644 0 0 200 12126630646 10251 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/optim/nonlinear/scalar/MultivariateFunctionMappingAdapterTest.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/optim/nonlinear/scalar/MultivariateFunc100644 1750 1750 21651 12126627671 32375 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.optim.nonlinear.scalar; import org.apache.commons.math3.analysis.MultivariateFunction; import org.apache.commons.math3.optim.MaxEval; import org.apache.commons.math3.optim.InitialGuess; import org.apache.commons.math3.optim.PointValuePair; import org.apache.commons.math3.optim.SimplePointChecker; import org.apache.commons.math3.optim.nonlinear.scalar.noderiv.SimplexOptimizer; import org.apache.commons.math3.optim.nonlinear.scalar.noderiv.AbstractSimplex; import org.apache.commons.math3.optim.nonlinear.scalar.noderiv.NelderMeadSimplex; import org.junit.Assert; import org.junit.Test; public class MultivariateFunctionMappingAdapterTest { @Test public void testStartSimplexInsideRange() { final BiQuadratic biQuadratic = new BiQuadratic(2.0, 2.5, 1.0, 3.0, 2.0, 3.0); final MultivariateFunctionMappingAdapter wrapped = new MultivariateFunctionMappingAdapter(biQuadratic, biQuadratic.getLower(), biQuadratic.getUpper()); SimplexOptimizer optimizer = new SimplexOptimizer(1e-10, 1e-30); final AbstractSimplex simplex = new NelderMeadSimplex(new double[][] { wrapped.boundedToUnbounded(new double[] { 1.5, 2.75 }), wrapped.boundedToUnbounded(new double[] { 1.5, 2.95 }), wrapped.boundedToUnbounded(new double[] { 1.7, 2.90 }) }); final PointValuePair optimum = optimizer.optimize(new MaxEval(300), new ObjectiveFunction(wrapped), simplex, GoalType.MINIMIZE, new InitialGuess(wrapped.boundedToUnbounded(new double[] { 1.5, 2.25 }))); final double[] bounded = wrapped.unboundedToBounded(optimum.getPoint()); Assert.assertEquals(biQuadratic.getBoundedXOptimum(), bounded[0], 2e-7); Assert.assertEquals(biQuadratic.getBoundedYOptimum(), bounded[1], 2e-7); } @Test public void testOptimumOutsideRange() { final BiQuadratic biQuadratic = new BiQuadratic(4.0, 0.0, 1.0, 3.0, 2.0, 3.0); final MultivariateFunctionMappingAdapter wrapped = new MultivariateFunctionMappingAdapter(biQuadratic, biQuadratic.getLower(), biQuadratic.getUpper()); SimplexOptimizer optimizer = new SimplexOptimizer(1e-10, 1e-30); final AbstractSimplex simplex = new NelderMeadSimplex(new double[][] { wrapped.boundedToUnbounded(new double[] { 1.5, 2.75 }), wrapped.boundedToUnbounded(new double[] { 1.5, 2.95 }), wrapped.boundedToUnbounded(new double[] { 1.7, 2.90 }) }); final PointValuePair optimum = optimizer.optimize(new MaxEval(100), new ObjectiveFunction(wrapped), simplex, GoalType.MINIMIZE, new InitialGuess(wrapped.boundedToUnbounded(new double[] { 1.5, 2.25 }))); final double[] bounded = wrapped.unboundedToBounded(optimum.getPoint()); Assert.assertEquals(biQuadratic.getBoundedXOptimum(), bounded[0], 2e-7); Assert.assertEquals(biQuadratic.getBoundedYOptimum(), bounded[1], 2e-7); } @Test public void testUnbounded() { final BiQuadratic biQuadratic = new BiQuadratic(4.0, 0.0, Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY, Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY); final MultivariateFunctionMappingAdapter wrapped = new MultivariateFunctionMappingAdapter(biQuadratic, biQuadratic.getLower(), biQuadratic.getUpper()); SimplexOptimizer optimizer = new SimplexOptimizer(1e-10, 1e-30); final AbstractSimplex simplex = new NelderMeadSimplex(new double[][] { wrapped.boundedToUnbounded(new double[] { 1.5, 2.75 }), wrapped.boundedToUnbounded(new double[] { 1.5, 2.95 }), wrapped.boundedToUnbounded(new double[] { 1.7, 2.90 }) }); final PointValuePair optimum = optimizer.optimize(new MaxEval(300), new ObjectiveFunction(wrapped), simplex, GoalType.MINIMIZE, new InitialGuess(wrapped.boundedToUnbounded(new double[] { 1.5, 2.25 }))); final double[] bounded = wrapped.unboundedToBounded(optimum.getPoint()); Assert.assertEquals(biQuadratic.getBoundedXOptimum(), bounded[0], 2e-7); Assert.assertEquals(biQuadratic.getBoundedYOptimum(), bounded[1], 2e-7); } @Test public void testHalfBounded() { final BiQuadratic biQuadratic = new BiQuadratic(4.0, 4.0, 1.0, Double.POSITIVE_INFINITY, Double.NEGATIVE_INFINITY, 3.0); final MultivariateFunctionMappingAdapter wrapped = new MultivariateFunctionMappingAdapter(biQuadratic, biQuadratic.getLower(), biQuadratic.getUpper()); SimplexOptimizer optimizer = new SimplexOptimizer(1e-13, 1e-30); final AbstractSimplex simplex = new NelderMeadSimplex(new double[][] { wrapped.boundedToUnbounded(new double[] { 1.5, 2.75 }), wrapped.boundedToUnbounded(new double[] { 1.5, 2.95 }), wrapped.boundedToUnbounded(new double[] { 1.7, 2.90 }) }); final PointValuePair optimum = optimizer.optimize(new MaxEval(200), new ObjectiveFunction(wrapped), simplex, GoalType.MINIMIZE, new InitialGuess(wrapped.boundedToUnbounded(new double[] { 1.5, 2.25 }))); final double[] bounded = wrapped.unboundedToBounded(optimum.getPoint()); Assert.assertEquals(biQuadratic.getBoundedXOptimum(), bounded[0], 1e-7); Assert.assertEquals(biQuadratic.getBoundedYOptimum(), bounded[1], 1e-7); } private static class BiQuadratic implements MultivariateFunction { private final double xOptimum; private final double yOptimum; private final double xMin; private final double xMax; private final double yMin; private final double yMax; public BiQuadratic(final double xOptimum, final double yOptimum, final double xMin, final double xMax, final double yMin, final double yMax) { this.xOptimum = xOptimum; this.yOptimum = yOptimum; this.xMin = xMin; this.xMax = xMax; this.yMin = yMin; this.yMax = yMax; } public double value(double[] point) { // the function should never be called with out of range points Assert.assertTrue(point[0] >= xMin); Assert.assertTrue(point[0] <= xMax); Assert.assertTrue(point[1] >= yMin); Assert.assertTrue(point[1] <= yMax); final double dx = point[0] - xOptimum; final double dy = point[1] - yOptimum; return dx * dx + dy * dy; } public double[] getLower() { return new double[] { xMin, yMin }; } public double[] getUpper() { return new double[] { xMax, yMax }; } public double getBoundedXOptimum() { return (xOptimum < xMin) ? xMin : ((xOptimum > xMax) ? xMax : xOptimum); } public double getBoundedYOptimum() { return (yOptimum < yMin) ? yMin : ((yOptimum > yMax) ? yMax : yOptimum); } } } ././@LongLink100644 0 0 164 12126630646 10262 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/optim/univariate/SimpleUnivariateValueCheckerTest.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/optim/univariate/SimpleUnivariateValueC100644 1750 1750 4124 12126627671 32365 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.optim.univariate; import org.apache.commons.math3.exception.NotStrictlyPositiveException; import org.junit.Test; import org.junit.Assert; public class SimpleUnivariateValueCheckerTest { @Test(expected=NotStrictlyPositiveException.class) public void testIterationCheckPrecondition() { new SimpleUnivariateValueChecker(1e-1, 1e-2, 0); } @Test public void testIterationCheck() { final int max = 10; final SimpleUnivariateValueChecker checker = new SimpleUnivariateValueChecker(1e-1, 1e-2, max); Assert.assertTrue(checker.converged(max, null, null)); Assert.assertTrue(checker.converged(max + 1, null, null)); } @Test public void testIterationCheckDisabled() { final SimpleUnivariateValueChecker checker = new SimpleUnivariateValueChecker(1e-8, 1e-8); final UnivariatePointValuePair a = new UnivariatePointValuePair(1d, 1d); final UnivariatePointValuePair b = new UnivariatePointValuePair(10d, 10d); Assert.assertFalse(checker.converged(-1, a, b)); Assert.assertFalse(checker.converged(0, a, b)); Assert.assertFalse(checker.converged(1000000, a, b)); Assert.assertTrue(checker.converged(-1, a, a)); Assert.assertTrue(checker.converged(-1, b, b)); } } ././@LongLink100644 0 0 146 12126630646 10262 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/optim/univariate/BrentOptimizerTest.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/optim/univariate/BrentOptimizerTest.jav100644 1750 1750 35202 12126627671 32421 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.optim.univariate; import org.apache.commons.math3.analysis.QuinticFunction; import org.apache.commons.math3.analysis.UnivariateFunction; import org.apache.commons.math3.analysis.function.Sin; import org.apache.commons.math3.analysis.function.StepFunction; import org.apache.commons.math3.analysis.FunctionUtils; import org.apache.commons.math3.exception.NumberIsTooLargeException; import org.apache.commons.math3.exception.NumberIsTooSmallException; import org.apache.commons.math3.exception.TooManyEvaluationsException; import org.apache.commons.math3.optim.ConvergenceChecker; import org.apache.commons.math3.optim.nonlinear.scalar.GoalType; import org.apache.commons.math3.optim.MaxEval; import org.apache.commons.math3.stat.descriptive.DescriptiveStatistics; import org.apache.commons.math3.util.FastMath; import org.junit.Assert; import org.junit.Test; /** * @version $Id$ */ public final class BrentOptimizerTest { @Test public void testSinMin() { UnivariateFunction f = new Sin(); UnivariateOptimizer optimizer = new BrentOptimizer(1e-10, 1e-14); Assert.assertEquals(3 * Math.PI / 2, optimizer.optimize(new MaxEval(200), new UnivariateObjectiveFunction(f), GoalType.MINIMIZE, new SearchInterval(4, 5)).getPoint(), 1e-8); Assert.assertTrue(optimizer.getEvaluations() <= 50); Assert.assertEquals(200, optimizer.getMaxEvaluations()); Assert.assertEquals(3 * Math.PI / 2, optimizer.optimize(new MaxEval(200), new UnivariateObjectiveFunction(f), GoalType.MINIMIZE, new SearchInterval(1, 5)).getPoint(), 1e-8); Assert.assertTrue(optimizer.getEvaluations() <= 100); Assert.assertTrue(optimizer.getEvaluations() >= 15); try { optimizer.optimize(new MaxEval(10), new UnivariateObjectiveFunction(f), GoalType.MINIMIZE, new SearchInterval(4, 5)); Assert.fail("an exception should have been thrown"); } catch (TooManyEvaluationsException fee) { // expected } } @Test public void testSinMinWithValueChecker() { final UnivariateFunction f = new Sin(); final ConvergenceChecker checker = new SimpleUnivariateValueChecker(1e-5, 1e-14); // The default stopping criterion of Brent's algorithm should not // pass, but the search will stop at the given relative tolerance // for the function value. final UnivariateOptimizer optimizer = new BrentOptimizer(1e-10, 1e-14, checker); final UnivariatePointValuePair result = optimizer.optimize(new MaxEval(200), new UnivariateObjectiveFunction(f), GoalType.MINIMIZE, new SearchInterval(4, 5)); Assert.assertEquals(3 * Math.PI / 2, result.getPoint(), 1e-3); } @Test public void testBoundaries() { final double lower = -1.0; final double upper = +1.0; UnivariateFunction f = new UnivariateFunction() { public double value(double x) { if (x < lower) { throw new NumberIsTooSmallException(x, lower, true); } else if (x > upper) { throw new NumberIsTooLargeException(x, upper, true); } else { return x; } } }; UnivariateOptimizer optimizer = new BrentOptimizer(1e-10, 1e-14); Assert.assertEquals(lower, optimizer.optimize(new MaxEval(100), new UnivariateObjectiveFunction(f), GoalType.MINIMIZE, new SearchInterval(lower, upper)).getPoint(), 1.0e-8); Assert.assertEquals(upper, optimizer.optimize(new MaxEval(100), new UnivariateObjectiveFunction(f), GoalType.MAXIMIZE, new SearchInterval(lower, upper)).getPoint(), 1.0e-8); } @Test public void testQuinticMin() { // The function has local minima at -0.27195613 and 0.82221643. UnivariateFunction f = new QuinticFunction(); UnivariateOptimizer optimizer = new BrentOptimizer(1e-10, 1e-14); Assert.assertEquals(-0.27195613, optimizer.optimize(new MaxEval(200), new UnivariateObjectiveFunction(f), GoalType.MINIMIZE, new SearchInterval(-0.3, -0.2)).getPoint(), 1.0e-8); Assert.assertEquals( 0.82221643, optimizer.optimize(new MaxEval(200), new UnivariateObjectiveFunction(f), GoalType.MINIMIZE, new SearchInterval(0.3, 0.9)).getPoint(), 1.0e-8); Assert.assertTrue(optimizer.getEvaluations() <= 50); // search in a large interval Assert.assertEquals(-0.27195613, optimizer.optimize(new MaxEval(200), new UnivariateObjectiveFunction(f), GoalType.MINIMIZE, new SearchInterval(-1.0, 0.2)).getPoint(), 1.0e-8); Assert.assertTrue(optimizer.getEvaluations() <= 50); } @Test public void testQuinticMinStatistics() { // The function has local minima at -0.27195613 and 0.82221643. UnivariateFunction f = new QuinticFunction(); UnivariateOptimizer optimizer = new BrentOptimizer(1e-11, 1e-14); final DescriptiveStatistics[] stat = new DescriptiveStatistics[2]; for (int i = 0; i < stat.length; i++) { stat[i] = new DescriptiveStatistics(); } final double min = -0.75; final double max = 0.25; final int nSamples = 200; final double delta = (max - min) / nSamples; for (int i = 0; i < nSamples; i++) { final double start = min + i * delta; stat[0].addValue(optimizer.optimize(new MaxEval(40), new UnivariateObjectiveFunction(f), GoalType.MINIMIZE, new SearchInterval(min, max, start)).getPoint()); stat[1].addValue(optimizer.getEvaluations()); } final double meanOptValue = stat[0].getMean(); final double medianEval = stat[1].getPercentile(50); Assert.assertTrue(meanOptValue > -0.2719561281); Assert.assertTrue(meanOptValue < -0.2719561280); Assert.assertEquals(23, (int) medianEval); } @Test public void testQuinticMax() { // The quintic function has zeros at 0, +-0.5 and +-1. // The function has a local maximum at 0.27195613. UnivariateFunction f = new QuinticFunction(); UnivariateOptimizer optimizer = new BrentOptimizer(1e-12, 1e-14); Assert.assertEquals(0.27195613, optimizer.optimize(new MaxEval(100), new UnivariateObjectiveFunction(f), GoalType.MAXIMIZE, new SearchInterval(0.2, 0.3)).getPoint(), 1e-8); try { optimizer.optimize(new MaxEval(5), new UnivariateObjectiveFunction(f), GoalType.MAXIMIZE, new SearchInterval(0.2, 0.3)); Assert.fail("an exception should have been thrown"); } catch (TooManyEvaluationsException miee) { // expected } } @Test public void testMinEndpoints() { UnivariateFunction f = new Sin(); UnivariateOptimizer optimizer = new BrentOptimizer(1e-8, 1e-14); // endpoint is minimum double result = optimizer.optimize(new MaxEval(50), new UnivariateObjectiveFunction(f), GoalType.MINIMIZE, new SearchInterval(3 * Math.PI / 2, 5)).getPoint(); Assert.assertEquals(3 * Math.PI / 2, result, 1e-6); result = optimizer.optimize(new MaxEval(50), new UnivariateObjectiveFunction(f), GoalType.MINIMIZE, new SearchInterval(4, 3 * Math.PI / 2)).getPoint(); Assert.assertEquals(3 * Math.PI / 2, result, 1e-6); } @Test public void testMath832() { final UnivariateFunction f = new UnivariateFunction() { public double value(double x) { final double sqrtX = FastMath.sqrt(x); final double a = 1e2 * sqrtX; final double b = 1e6 / x; final double c = 1e4 / sqrtX; return a + b + c; } }; UnivariateOptimizer optimizer = new BrentOptimizer(1e-10, 1e-8); final double result = optimizer.optimize(new MaxEval(1483), new UnivariateObjectiveFunction(f), GoalType.MINIMIZE, new SearchInterval(Double.MIN_VALUE, Double.MAX_VALUE)).getPoint(); Assert.assertEquals(804.9355825, result, 1e-6); } /** * Contrived example showing that prior to the resolution of MATH-855 * (second revision), the algorithm would not return the best point if * it happened to be the initial guess. */ @Test public void testKeepInitIfBest() { final double minSin = 3 * Math.PI / 2; final double offset = 1e-8; final double delta = 1e-7; final UnivariateFunction f1 = new Sin(); final UnivariateFunction f2 = new StepFunction(new double[] { minSin, minSin + offset, minSin + 2 * offset}, new double[] { 0, -1, 0 }); final UnivariateFunction f = FunctionUtils.add(f1, f2); // A slightly less stringent tolerance would make the test pass // even with the previous implementation. final double relTol = 1e-8; final UnivariateOptimizer optimizer = new BrentOptimizer(relTol, 1e-100); final double init = minSin + 1.5 * offset; final UnivariatePointValuePair result = optimizer.optimize(new MaxEval(200), new UnivariateObjectiveFunction(f), GoalType.MINIMIZE, new SearchInterval(minSin - 6.789 * delta, minSin + 9.876 * delta, init)); final int numEval = optimizer.getEvaluations(); final double sol = result.getPoint(); final double expected = init; // System.out.println("numEval=" + numEval); // System.out.println("min=" + init + " f=" + f.value(init)); // System.out.println("sol=" + sol + " f=" + f.value(sol)); // System.out.println("exp=" + expected + " f=" + f.value(expected)); Assert.assertTrue("Best point not reported", f.value(sol) <= f.value(expected)); } /** * Contrived example showing that prior to the resolution of MATH-855, * the algorithm, by always returning the last evaluated point, would * sometimes not report the best point it had found. */ @Test public void testMath855() { final double minSin = 3 * Math.PI / 2; final double offset = 1e-8; final double delta = 1e-7; final UnivariateFunction f1 = new Sin(); final UnivariateFunction f2 = new StepFunction(new double[] { minSin, minSin + offset, minSin + 5 * offset }, new double[] { 0, -1, 0 }); final UnivariateFunction f = FunctionUtils.add(f1, f2); final UnivariateOptimizer optimizer = new BrentOptimizer(1e-8, 1e-100); final UnivariatePointValuePair result = optimizer.optimize(new MaxEval(200), new UnivariateObjectiveFunction(f), GoalType.MINIMIZE, new SearchInterval(minSin - 6.789 * delta, minSin + 9.876 * delta)); final int numEval = optimizer.getEvaluations(); final double sol = result.getPoint(); final double expected = 4.712389027602411; // System.out.println("min=" + (minSin + offset) + " f=" + f.value(minSin + offset)); // System.out.println("sol=" + sol + " f=" + f.value(sol)); // System.out.println("exp=" + expected + " f=" + f.value(expected)); Assert.assertTrue("Best point not reported", f.value(sol) <= f.value(expected)); } } ././@LongLink100644 0 0 145 12126630646 10261 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/optim/univariate/BracketFinderTest.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/optim/univariate/BracketFinderTest.java100644 1750 1750 10041 12126627671 32302 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.optim.univariate; import org.apache.commons.math3.analysis.UnivariateFunction; import org.apache.commons.math3.optim.nonlinear.scalar.GoalType; import org.junit.Assert; import org.junit.Test; /** * Test for {@link BracketFinder}. */ public class BracketFinderTest { @Test public void testCubicMin() { final BracketFinder bFind = new BracketFinder(); final UnivariateFunction func = new UnivariateFunction() { public double value(double x) { if (x < -2) { return value(-2); } else { return (x - 1) * (x + 2) * (x + 3); } } }; bFind.search(func, GoalType.MINIMIZE, -2 , -1); final double tol = 1e-15; // Comparing with results computed in Python. Assert.assertEquals(-2, bFind.getLo(), tol); Assert.assertEquals(-1, bFind.getMid(), tol); Assert.assertEquals(0.61803399999999997, bFind.getHi(), tol); } @Test public void testCubicMax() { final BracketFinder bFind = new BracketFinder(); final UnivariateFunction func = new UnivariateFunction() { public double value(double x) { if (x < -2) { return value(-2); } else { return -(x - 1) * (x + 2) * (x + 3); } } }; bFind.search(func, GoalType.MAXIMIZE, -2 , -1); final double tol = 1e-15; Assert.assertEquals(-2, bFind.getLo(), tol); Assert.assertEquals(-1, bFind.getMid(), tol); Assert.assertEquals(0.61803399999999997, bFind.getHi(), tol); } @Test public void testMinimumIsOnIntervalBoundary() { final UnivariateFunction func = new UnivariateFunction() { public double value(double x) { return x * x; } }; final BracketFinder bFind = new BracketFinder(); bFind.search(func, GoalType.MINIMIZE, 0, 1); Assert.assertTrue(bFind.getLo() <= 0); Assert.assertTrue(0 <= bFind.getHi()); bFind.search(func, GoalType.MINIMIZE, -1, 0); Assert.assertTrue(bFind.getLo() <= 0); Assert.assertTrue(0 <= bFind.getHi()); } @Test public void testIntervalBoundsOrdering() { final UnivariateFunction func = new UnivariateFunction() { public double value(double x) { return x * x; } }; final BracketFinder bFind = new BracketFinder(); bFind.search(func, GoalType.MINIMIZE, -1, 1); Assert.assertTrue(bFind.getLo() <= 0); Assert.assertTrue(0 <= bFind.getHi()); bFind.search(func, GoalType.MINIMIZE, 1, -1); Assert.assertTrue(bFind.getLo() <= 0); Assert.assertTrue(0 <= bFind.getHi()); bFind.search(func, GoalType.MINIMIZE, 1, 2); Assert.assertTrue(bFind.getLo() <= 0); Assert.assertTrue(0 <= bFind.getHi()); bFind.search(func, GoalType.MINIMIZE, 2, 1); Assert.assertTrue(bFind.getLo() <= 0); Assert.assertTrue(0 <= bFind.getHi()); } } ././@LongLink100644 0 0 165 12126630646 10263 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/optim/univariate/MultiStartUnivariateOptimizerTest.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/optim/univariate/MultiStartUnivariateOp100644 1750 1750 14040 12126627671 32461 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.optim.univariate; import org.apache.commons.math3.analysis.QuinticFunction; import org.apache.commons.math3.analysis.UnivariateFunction; import org.apache.commons.math3.analysis.function.Sin; import org.apache.commons.math3.optim.nonlinear.scalar.GoalType; import org.apache.commons.math3.optim.MaxEval; import org.apache.commons.math3.random.JDKRandomGenerator; import org.apache.commons.math3.util.FastMath; import org.apache.commons.math3.exception.MathIllegalStateException; import org.junit.Assert; import org.junit.Test; public class MultiStartUnivariateOptimizerTest { @Test(expected=MathIllegalStateException.class) public void testMissingMaxEval() { UnivariateOptimizer underlying = new BrentOptimizer(1e-10, 1e-14); JDKRandomGenerator g = new JDKRandomGenerator(); g.setSeed(44428400075l); MultiStartUnivariateOptimizer optimizer = new MultiStartUnivariateOptimizer(underlying, 10, g); optimizer.optimize(new UnivariateObjectiveFunction(new Sin()), GoalType.MINIMIZE, new SearchInterval(-1, 1)); } @Test(expected=MathIllegalStateException.class) public void testMissingSearchInterval() { UnivariateOptimizer underlying = new BrentOptimizer(1e-10, 1e-14); JDKRandomGenerator g = new JDKRandomGenerator(); g.setSeed(44428400075l); MultiStartUnivariateOptimizer optimizer = new MultiStartUnivariateOptimizer(underlying, 10, g); optimizer.optimize(new MaxEval(300), new UnivariateObjectiveFunction(new Sin()), GoalType.MINIMIZE); } @Test public void testSinMin() { UnivariateFunction f = new Sin(); UnivariateOptimizer underlying = new BrentOptimizer(1e-10, 1e-14); JDKRandomGenerator g = new JDKRandomGenerator(); g.setSeed(44428400075l); MultiStartUnivariateOptimizer optimizer = new MultiStartUnivariateOptimizer(underlying, 10, g); optimizer.optimize(new MaxEval(300), new UnivariateObjectiveFunction(f), GoalType.MINIMIZE, new SearchInterval(-100.0, 100.0)); UnivariatePointValuePair[] optima = optimizer.getOptima(); for (int i = 1; i < optima.length; ++i) { double d = (optima[i].getPoint() - optima[i-1].getPoint()) / (2 * FastMath.PI); Assert.assertTrue(FastMath.abs(d - FastMath.rint(d)) < 1.0e-8); Assert.assertEquals(-1.0, f.value(optima[i].getPoint()), 1.0e-10); Assert.assertEquals(f.value(optima[i].getPoint()), optima[i].getValue(), 1.0e-10); } Assert.assertTrue(optimizer.getEvaluations() > 200); Assert.assertTrue(optimizer.getEvaluations() < 300); } @Test public void testQuinticMin() { // The quintic function has zeros at 0, +-0.5 and +-1. // The function has extrema (first derivative is zero) at 0.27195613 and 0.82221643, UnivariateFunction f = new QuinticFunction(); UnivariateOptimizer underlying = new BrentOptimizer(1e-9, 1e-14); JDKRandomGenerator g = new JDKRandomGenerator(); g.setSeed(4312000053L); MultiStartUnivariateOptimizer optimizer = new MultiStartUnivariateOptimizer(underlying, 5, g); UnivariatePointValuePair optimum = optimizer.optimize(new MaxEval(300), new UnivariateObjectiveFunction(f), GoalType.MINIMIZE, new SearchInterval(-0.3, -0.2)); Assert.assertEquals(-0.27195613, optimum.getPoint(), 1e-9); Assert.assertEquals(-0.0443342695, optimum.getValue(), 1e-9); UnivariatePointValuePair[] optima = optimizer.getOptima(); for (int i = 0; i < optima.length; ++i) { Assert.assertEquals(f.value(optima[i].getPoint()), optima[i].getValue(), 1e-9); } Assert.assertTrue(optimizer.getEvaluations() >= 50); Assert.assertTrue(optimizer.getEvaluations() <= 100); } @Test public void testBadFunction() { UnivariateFunction f = new UnivariateFunction() { public double value(double x) { if (x < 0) { throw new LocalException(); } return 0; } }; UnivariateOptimizer underlying = new BrentOptimizer(1e-9, 1e-14); JDKRandomGenerator g = new JDKRandomGenerator(); g.setSeed(4312000053L); MultiStartUnivariateOptimizer optimizer = new MultiStartUnivariateOptimizer(underlying, 5, g); try { optimizer.optimize(new MaxEval(300), new UnivariateObjectiveFunction(f), GoalType.MINIMIZE, new SearchInterval(-0.3, -0.2)); Assert.fail(); } catch (LocalException e) { // Expected. } // Ensure that the exception was thrown because no optimum was found. Assert.assertTrue(optimizer.getOptima()[0] == null); } private static class LocalException extends RuntimeException { private static final long serialVersionUID = 1194682757034350629L; } } commons-math3-3.2-src/src/test/java/org/apache/commons/math3/optim/PointVectorValuePairTest.java100644 1750 1750 3310 12126627671 31476 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.optim; import org.apache.commons.math3.TestUtils; import org.junit.Assert; import org.junit.Test; public class PointVectorValuePairTest { @Test public void testSerial() { PointVectorValuePair pv1 = new PointVectorValuePair(new double[] { 1.0, 2.0, 3.0 }, new double[] { 4.0, 5.0 }); PointVectorValuePair pv2 = (PointVectorValuePair) TestUtils.serializeAndRecover(pv1); Assert.assertEquals(pv1.getKey().length, pv2.getKey().length); for (int i = 0; i < pv1.getKey().length; ++i) { Assert.assertEquals(pv1.getKey()[i], pv2.getKey()[i], 1.0e-15); } Assert.assertEquals(pv1.getValue().length, pv2.getValue().length); for (int i = 0; i < pv1.getValue().length; ++i) { Assert.assertEquals(pv1.getValue()[i], pv2.getValue()[i], 1.0e-15); } } } ././@LongLink100644 0 0 156 12126630646 10263 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/geometry/partitioning/utilities/AVLTreeTest.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/geometry/partitioning/utilities/AVLTree100644 1750 1750 14467 12126627671 32357 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.geometry.partitioning.utilities; import org.apache.commons.math3.geometry.partitioning.utilities.AVLTree; import org.junit.Assert; import org.junit.Test; public class AVLTreeTest { @Test public void testInsert() { // this array in this order allows to pass in all branches // of the insertion algorithm int[] array = { 16, 13, 15, 14, 2, 0, 12, 9, 8, 5, 11, 18, 19, 17, 4, 7, 1, 3, 6, 10 }; AVLTree tree = buildTree(array); Assert.assertEquals(array.length, tree.size()); for (int i = 0; i < array.length; ++i) { Assert.assertEquals(array[i], value(tree.getNotSmaller(new Integer(array[i])))); } checkOrder(tree); } @Test public void testDelete1() { int[][][] arrays = { { { 16, 13, 15, 14, 2, 0, 12, 9, 8, 5, 11, 18, 19, 17, 4, 7, 1, 3, 6, 10 }, { 11, 10, 9, 12, 16, 15, 13, 18, 5, 0, 3, 2, 14, 6, 19, 17, 8, 4, 7, 1 } }, { { 16, 13, 15, 14, 2, 0, 12, 9, 8, 5, 11, 18, 19, 17, 4, 7, 1, 3, 6, 10 }, { 0, 17, 14, 15, 16, 18, 6 } }, { { 6, 2, 7, 8, 1, 4, 3, 5 }, { 8 } }, { { 6, 2, 7, 8, 1, 4, 5 }, { 8 } }, { { 3, 7, 2, 1, 5, 8, 4 }, { 1 } }, { { 3, 7, 2, 1, 5, 8, 6 }, { 1 } } }; for (int i = 0; i < arrays.length; ++i) { AVLTree tree = buildTree(arrays[i][0]); Assert.assertTrue(! tree.delete(new Integer(-2000))); for (int j = 0; j < arrays[i][1].length; ++j) { Assert.assertTrue(tree.delete(tree.getNotSmaller(new Integer(arrays[i][1][j])).getElement())); Assert.assertEquals(arrays[i][0].length - j - 1, tree.size()); } } } @Test public void testNavigation() { int[] array = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; AVLTree tree = buildTree(array); AVLTree.Node node = tree.getSmallest(); Assert.assertEquals(array[0], value(node)); for (int i = 0; i < array.length; ++i) { Assert.assertEquals(array[i], value(node)); node = node.getNext(); } Assert.assertNull(node); node = tree.getLargest(); Assert.assertEquals(array[array.length - 1], value(node)); for (int i = array.length - 1; i >= 0; --i) { Assert.assertEquals(array[i], value(node)); node = node.getPrevious(); } Assert.assertNull(node); checkOrder(tree); } @Test public void testSearch() { int[] array = { 2, 4, 6, 8, 10, 12, 14 }; AVLTree tree = buildTree(array); Assert.assertNull(tree.getNotLarger(new Integer(array[0] - 1))); Assert.assertNull(tree.getNotSmaller(new Integer(array[array.length - 1] + 1))); for (int i = 0; i < array.length; ++i) { Assert.assertEquals(array[i], value(tree.getNotSmaller(new Integer(array[i] - 1)))); Assert.assertEquals(array[i], value(tree.getNotLarger(new Integer(array[i] + 1)))); } checkOrder(tree); } @Test public void testRepetition() { int[] array = { 1, 1, 3, 3, 4, 5, 6, 7, 7, 7, 7, 7 }; AVLTree tree = buildTree(array); Assert.assertEquals(array.length, tree.size()); AVLTree.Node node = tree.getNotSmaller(new Integer(3)); Assert.assertEquals(3, value(node)); Assert.assertEquals(1, value(node.getPrevious())); Assert.assertEquals(3, value(node.getNext())); Assert.assertEquals(4, value(node.getNext().getNext())); node = tree.getNotLarger(new Integer(2)); Assert.assertEquals(1, value(node)); Assert.assertEquals(1, value(node.getPrevious())); Assert.assertEquals(3, value(node.getNext())); Assert.assertNull(node.getPrevious().getPrevious()); AVLTree.Node otherNode = tree.getNotSmaller(new Integer(1)); Assert.assertTrue(node != otherNode); Assert.assertEquals(1, value(otherNode)); Assert.assertNull(otherNode.getPrevious()); node = tree.getNotLarger(new Integer(10)); Assert.assertEquals(7, value(node)); Assert.assertNull(node.getNext()); node = node.getPrevious(); Assert.assertEquals(7, value(node)); node = node.getPrevious(); Assert.assertEquals(7, value(node)); node = node.getPrevious(); Assert.assertEquals(7, value(node)); node = node.getPrevious(); Assert.assertEquals(7, value(node)); node = node.getPrevious(); Assert.assertEquals(6, value(node)); checkOrder(tree); } private AVLTree buildTree(int[] array) { AVLTree tree = new AVLTree(); for (int i = 0; i < array.length; ++i) { tree.insert(new Integer(array[i])); tree.insert(null); } return tree; } private int value(AVLTree.Node node) { return node.getElement().intValue(); } private void checkOrder(AVLTree tree) { AVLTree.Node next = null; for (AVLTree.Node node = tree.getSmallest(); node != null; node = next) { next = node.getNext(); if (next != null) { Assert.assertTrue(node.getElement().compareTo(next.getElement()) <= 0); } } } } ././@LongLink100644 0 0 146 12126630646 10262 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/geometry/euclidean/twod/SubLineTest.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/geometry/euclidean/twod/SubLineTest.jav100644 1750 1750 15571 12126627672 32257 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.geometry.euclidean.twod; import java.util.List; import org.apache.commons.math3.geometry.euclidean.oned.Euclidean1D; import org.apache.commons.math3.geometry.euclidean.oned.IntervalsSet; import org.apache.commons.math3.geometry.partitioning.RegionFactory; import org.junit.Assert; import org.junit.Test; public class SubLineTest { @Test public void testEndPoints() { Vector2D p1 = new Vector2D(-1, -7); Vector2D p2 = new Vector2D(7, -1); Segment segment = new Segment(p1, p2, new Line(p1, p2)); SubLine sub = new SubLine(segment); List segments = sub.getSegments(); Assert.assertEquals(1, segments.size()); Assert.assertEquals(0.0, new Vector2D(-1, -7).distance(segments.get(0).getStart()), 1.0e-10); Assert.assertEquals(0.0, new Vector2D( 7, -1).distance(segments.get(0).getEnd()), 1.0e-10); } @Test public void testNoEndPoints() { SubLine wholeLine = new Line(new Vector2D(-1, 7), new Vector2D(7, 1)).wholeHyperplane(); List segments = wholeLine.getSegments(); Assert.assertEquals(1, segments.size()); Assert.assertTrue(Double.isInfinite(segments.get(0).getStart().getX()) && segments.get(0).getStart().getX() < 0); Assert.assertTrue(Double.isInfinite(segments.get(0).getStart().getY()) && segments.get(0).getStart().getY() > 0); Assert.assertTrue(Double.isInfinite(segments.get(0).getEnd().getX()) && segments.get(0).getEnd().getX() > 0); Assert.assertTrue(Double.isInfinite(segments.get(0).getEnd().getY()) && segments.get(0).getEnd().getY() < 0); } @Test public void testNoSegments() { SubLine empty = new SubLine(new Line(new Vector2D(-1, -7), new Vector2D(7, -1)), new RegionFactory().getComplement(new IntervalsSet())); List segments = empty.getSegments(); Assert.assertEquals(0, segments.size()); } @Test public void testSeveralSegments() { SubLine twoSubs = new SubLine(new Line(new Vector2D(-1, -7), new Vector2D(7, -1)), new RegionFactory().union(new IntervalsSet(1, 2), new IntervalsSet(3, 4))); List segments = twoSubs.getSegments(); Assert.assertEquals(2, segments.size()); } @Test public void testHalfInfiniteNeg() { SubLine empty = new SubLine(new Line(new Vector2D(-1, -7), new Vector2D(7, -1)), new IntervalsSet(Double.NEGATIVE_INFINITY, 0.0)); List segments = empty.getSegments(); Assert.assertEquals(1, segments.size()); Assert.assertTrue(Double.isInfinite(segments.get(0).getStart().getX()) && segments.get(0).getStart().getX() < 0); Assert.assertTrue(Double.isInfinite(segments.get(0).getStart().getY()) && segments.get(0).getStart().getY() < 0); Assert.assertEquals(0.0, new Vector2D(3, -4).distance(segments.get(0).getEnd()), 1.0e-10); } @Test public void testHalfInfinitePos() { SubLine empty = new SubLine(new Line(new Vector2D(-1, -7), new Vector2D(7, -1)), new IntervalsSet(0.0, Double.POSITIVE_INFINITY)); List segments = empty.getSegments(); Assert.assertEquals(1, segments.size()); Assert.assertEquals(0.0, new Vector2D(3, -4).distance(segments.get(0).getStart()), 1.0e-10); Assert.assertTrue(Double.isInfinite(segments.get(0).getEnd().getX()) && segments.get(0).getEnd().getX() > 0); Assert.assertTrue(Double.isInfinite(segments.get(0).getEnd().getY()) && segments.get(0).getEnd().getY() > 0); } @Test public void testIntersectionInsideInside() { SubLine sub1 = new SubLine(new Vector2D(1, 1), new Vector2D(3, 1)); SubLine sub2 = new SubLine(new Vector2D(2, 0), new Vector2D(2, 2)); Assert.assertEquals(0.0, new Vector2D(2, 1).distance(sub1.intersection(sub2, true)), 1.0e-12); Assert.assertEquals(0.0, new Vector2D(2, 1).distance(sub1.intersection(sub2, false)), 1.0e-12); } @Test public void testIntersectionInsideBoundary() { SubLine sub1 = new SubLine(new Vector2D(1, 1), new Vector2D(3, 1)); SubLine sub2 = new SubLine(new Vector2D(2, 0), new Vector2D(2, 1)); Assert.assertEquals(0.0, new Vector2D(2, 1).distance(sub1.intersection(sub2, true)), 1.0e-12); Assert.assertNull(sub1.intersection(sub2, false)); } @Test public void testIntersectionInsideOutside() { SubLine sub1 = new SubLine(new Vector2D(1, 1), new Vector2D(3, 1)); SubLine sub2 = new SubLine(new Vector2D(2, 0), new Vector2D(2, 0.5)); Assert.assertNull(sub1.intersection(sub2, true)); Assert.assertNull(sub1.intersection(sub2, false)); } @Test public void testIntersectionBoundaryBoundary() { SubLine sub1 = new SubLine(new Vector2D(1, 1), new Vector2D(2, 1)); SubLine sub2 = new SubLine(new Vector2D(2, 0), new Vector2D(2, 1)); Assert.assertEquals(0.0, new Vector2D(2, 1).distance(sub1.intersection(sub2, true)), 1.0e-12); Assert.assertNull(sub1.intersection(sub2, false)); } @Test public void testIntersectionBoundaryOutside() { SubLine sub1 = new SubLine(new Vector2D(1, 1), new Vector2D(2, 1)); SubLine sub2 = new SubLine(new Vector2D(2, 0), new Vector2D(2, 0.5)); Assert.assertNull(sub1.intersection(sub2, true)); Assert.assertNull(sub1.intersection(sub2, false)); } @Test public void testIntersectionOutsideOutside() { SubLine sub1 = new SubLine(new Vector2D(1, 1), new Vector2D(1.5, 1)); SubLine sub2 = new SubLine(new Vector2D(2, 0), new Vector2D(2, 0.5)); Assert.assertNull(sub1.intersection(sub2, true)); Assert.assertNull(sub1.intersection(sub2, false)); } } ././@LongLink100644 0 0 152 12126630646 10257 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/geometry/euclidean/twod/PolygonsSetTest.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/geometry/euclidean/twod/PolygonsSetTest100644 1750 1750 141370 12126627672 32442 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.geometry.euclidean.twod; import java.util.ArrayList; import java.util.List; import org.apache.commons.math3.geometry.euclidean.oned.Interval; import org.apache.commons.math3.geometry.euclidean.oned.IntervalsSet; import org.apache.commons.math3.geometry.euclidean.oned.Vector1D; import org.apache.commons.math3.geometry.partitioning.BSPTree; import org.apache.commons.math3.geometry.partitioning.Region; import org.apache.commons.math3.geometry.partitioning.Region.Location; import org.apache.commons.math3.geometry.partitioning.RegionFactory; import org.apache.commons.math3.geometry.partitioning.SubHyperplane; import org.apache.commons.math3.util.FastMath; import org.junit.Assert; import org.junit.Test; public class PolygonsSetTest { @Test public void testSimplyConnected() { Vector2D[][] vertices = new Vector2D[][] { new Vector2D[] { new Vector2D(36.0, 22.0), new Vector2D(39.0, 32.0), new Vector2D(19.0, 32.0), new Vector2D( 6.0, 16.0), new Vector2D(31.0, 10.0), new Vector2D(42.0, 16.0), new Vector2D(34.0, 20.0), new Vector2D(29.0, 19.0), new Vector2D(23.0, 22.0), new Vector2D(33.0, 25.0) } }; PolygonsSet set = buildSet(vertices); Assert.assertEquals(Region.Location.OUTSIDE, set.checkPoint(new Vector2D(50.0, 30.0))); checkPoints(Region.Location.INSIDE, set, new Vector2D[] { new Vector2D(30.0, 15.0), new Vector2D(15.0, 20.0), new Vector2D(24.0, 25.0), new Vector2D(35.0, 30.0), new Vector2D(19.0, 17.0) }); checkPoints(Region.Location.OUTSIDE, set, new Vector2D[] { new Vector2D(50.0, 30.0), new Vector2D(30.0, 35.0), new Vector2D(10.0, 25.0), new Vector2D(10.0, 10.0), new Vector2D(40.0, 10.0), new Vector2D(50.0, 15.0), new Vector2D(30.0, 22.0) }); checkPoints(Region.Location.BOUNDARY, set, new Vector2D[] { new Vector2D(30.0, 32.0), new Vector2D(34.0, 20.0) }); checkVertices(set.getVertices(), vertices); } @Test public void testStair() { Vector2D[][] vertices = new Vector2D[][] { new Vector2D[] { new Vector2D( 0.0, 0.0), new Vector2D( 0.0, 2.0), new Vector2D(-0.1, 2.0), new Vector2D(-0.1, 1.0), new Vector2D(-0.3, 1.0), new Vector2D(-0.3, 1.5), new Vector2D(-1.3, 1.5), new Vector2D(-1.3, 2.0), new Vector2D(-1.8, 2.0), new Vector2D(-1.8 - 1.0 / FastMath.sqrt(2.0), 2.0 - 1.0 / FastMath.sqrt(2.0)) } }; PolygonsSet set = buildSet(vertices); checkVertices(set.getVertices(), vertices); Assert.assertEquals(1.1 + 0.95 * FastMath.sqrt(2.0), set.getSize(), 1.0e-10); } @Test public void testHole() { Vector2D[][] vertices = new Vector2D[][] { new Vector2D[] { new Vector2D(0.0, 0.0), new Vector2D(3.0, 0.0), new Vector2D(3.0, 3.0), new Vector2D(0.0, 3.0) }, new Vector2D[] { new Vector2D(1.0, 2.0), new Vector2D(2.0, 2.0), new Vector2D(2.0, 1.0), new Vector2D(1.0, 1.0) } }; PolygonsSet set = buildSet(vertices); checkPoints(Region.Location.INSIDE, set, new Vector2D[] { new Vector2D(0.5, 0.5), new Vector2D(1.5, 0.5), new Vector2D(2.5, 0.5), new Vector2D(0.5, 1.5), new Vector2D(2.5, 1.5), new Vector2D(0.5, 2.5), new Vector2D(1.5, 2.5), new Vector2D(2.5, 2.5), new Vector2D(0.5, 1.0) }); checkPoints(Region.Location.OUTSIDE, set, new Vector2D[] { new Vector2D(1.5, 1.5), new Vector2D(3.5, 1.0), new Vector2D(4.0, 1.5), new Vector2D(6.0, 6.0) }); checkPoints(Region.Location.BOUNDARY, set, new Vector2D[] { new Vector2D(1.0, 1.0), new Vector2D(1.5, 0.0), new Vector2D(1.5, 1.0), new Vector2D(1.5, 2.0), new Vector2D(1.5, 3.0), new Vector2D(3.0, 3.0) }); checkVertices(set.getVertices(), vertices); } @Test public void testDisjointPolygons() { Vector2D[][] vertices = new Vector2D[][] { new Vector2D[] { new Vector2D(0.0, 1.0), new Vector2D(2.0, 1.0), new Vector2D(1.0, 2.0) }, new Vector2D[] { new Vector2D(4.0, 0.0), new Vector2D(5.0, 1.0), new Vector2D(3.0, 1.0) } }; PolygonsSet set = buildSet(vertices); Assert.assertEquals(Region.Location.INSIDE, set.checkPoint(new Vector2D(1.0, 1.5))); checkPoints(Region.Location.INSIDE, set, new Vector2D[] { new Vector2D(1.0, 1.5), new Vector2D(4.5, 0.8) }); checkPoints(Region.Location.OUTSIDE, set, new Vector2D[] { new Vector2D(1.0, 0.0), new Vector2D(3.5, 1.2), new Vector2D(2.5, 1.0), new Vector2D(3.0, 4.0) }); checkPoints(Region.Location.BOUNDARY, set, new Vector2D[] { new Vector2D(1.0, 1.0), new Vector2D(3.5, 0.5), new Vector2D(0.0, 1.0) }); checkVertices(set.getVertices(), vertices); } @Test public void testOppositeHyperplanes() { Vector2D[][] vertices = new Vector2D[][] { new Vector2D[] { new Vector2D(1.0, 0.0), new Vector2D(2.0, 1.0), new Vector2D(3.0, 1.0), new Vector2D(2.0, 2.0), new Vector2D(1.0, 1.0), new Vector2D(0.0, 1.0) } }; PolygonsSet set = buildSet(vertices); checkVertices(set.getVertices(), vertices); } @Test public void testSingularPoint() { Vector2D[][] vertices = new Vector2D[][] { new Vector2D[] { new Vector2D( 0.0, 0.0), new Vector2D( 1.0, 0.0), new Vector2D( 1.0, 1.0), new Vector2D( 0.0, 1.0), new Vector2D( 0.0, 0.0), new Vector2D(-1.0, 0.0), new Vector2D(-1.0, -1.0), new Vector2D( 0.0, -1.0) } }; PolygonsSet set = buildSet(vertices); checkVertices(set.getVertices(), vertices); } @Test public void testLineIntersection() { Vector2D[][] vertices = new Vector2D[][] { new Vector2D[] { new Vector2D( 0.0, 0.0), new Vector2D( 2.0, 0.0), new Vector2D( 2.0, 1.0), new Vector2D( 3.0, 1.0), new Vector2D( 3.0, 3.0), new Vector2D( 1.0, 3.0), new Vector2D( 1.0, 2.0), new Vector2D( 0.0, 2.0) } }; PolygonsSet set = buildSet(vertices); Line l1 = new Line(new Vector2D(-1.5, 0.0), FastMath.PI / 4); SubLine s1 = (SubLine) set.intersection(l1.wholeHyperplane()); List i1 = ((IntervalsSet) s1.getRemainingRegion()).asList(); Assert.assertEquals(2, i1.size()); Interval v10 = i1.get(0); Vector2D p10Lower = l1.toSpace(new Vector1D(v10.getInf())); Assert.assertEquals(0.0, p10Lower.getX(), 1.0e-10); Assert.assertEquals(1.5, p10Lower.getY(), 1.0e-10); Vector2D p10Upper = l1.toSpace(new Vector1D(v10.getSup())); Assert.assertEquals(0.5, p10Upper.getX(), 1.0e-10); Assert.assertEquals(2.0, p10Upper.getY(), 1.0e-10); Interval v11 = i1.get(1); Vector2D p11Lower = l1.toSpace(new Vector1D(v11.getInf())); Assert.assertEquals(1.0, p11Lower.getX(), 1.0e-10); Assert.assertEquals(2.5, p11Lower.getY(), 1.0e-10); Vector2D p11Upper = l1.toSpace(new Vector1D(v11.getSup())); Assert.assertEquals(1.5, p11Upper.getX(), 1.0e-10); Assert.assertEquals(3.0, p11Upper.getY(), 1.0e-10); Line l2 = new Line(new Vector2D(-1.0, 2.0), 0); SubLine s2 = (SubLine) set.intersection(l2.wholeHyperplane()); List i2 = ((IntervalsSet) s2.getRemainingRegion()).asList(); Assert.assertEquals(1, i2.size()); Interval v20 = i2.get(0); Vector2D p20Lower = l2.toSpace(new Vector1D(v20.getInf())); Assert.assertEquals(1.0, p20Lower.getX(), 1.0e-10); Assert.assertEquals(2.0, p20Lower.getY(), 1.0e-10); Vector2D p20Upper = l2.toSpace(new Vector1D(v20.getSup())); Assert.assertEquals(3.0, p20Upper.getX(), 1.0e-10); Assert.assertEquals(2.0, p20Upper.getY(), 1.0e-10); } @Test public void testUnlimitedSubHyperplane() { Vector2D[][] vertices1 = new Vector2D[][] { new Vector2D[] { new Vector2D(0.0, 0.0), new Vector2D(4.0, 0.0), new Vector2D(1.4, 1.5), new Vector2D(0.0, 3.5) } }; PolygonsSet set1 = buildSet(vertices1); Vector2D[][] vertices2 = new Vector2D[][] { new Vector2D[] { new Vector2D(1.4, 0.2), new Vector2D(2.8, -1.2), new Vector2D(2.5, 0.6) } }; PolygonsSet set2 = buildSet(vertices2); PolygonsSet set = (PolygonsSet) new RegionFactory().union(set1.copySelf(), set2.copySelf()); checkVertices(set1.getVertices(), vertices1); checkVertices(set2.getVertices(), vertices2); checkVertices(set.getVertices(), new Vector2D[][] { new Vector2D[] { new Vector2D(0.0, 0.0), new Vector2D(1.6, 0.0), new Vector2D(2.8, -1.2), new Vector2D(2.6, 0.0), new Vector2D(4.0, 0.0), new Vector2D(1.4, 1.5), new Vector2D(0.0, 3.5) } }); } @Test public void testUnion() { Vector2D[][] vertices1 = new Vector2D[][] { new Vector2D[] { new Vector2D( 0.0, 0.0), new Vector2D( 2.0, 0.0), new Vector2D( 2.0, 2.0), new Vector2D( 0.0, 2.0) } }; PolygonsSet set1 = buildSet(vertices1); Vector2D[][] vertices2 = new Vector2D[][] { new Vector2D[] { new Vector2D( 1.0, 1.0), new Vector2D( 3.0, 1.0), new Vector2D( 3.0, 3.0), new Vector2D( 1.0, 3.0) } }; PolygonsSet set2 = buildSet(vertices2); PolygonsSet set = (PolygonsSet) new RegionFactory().union(set1.copySelf(), set2.copySelf()); checkVertices(set1.getVertices(), vertices1); checkVertices(set2.getVertices(), vertices2); checkVertices(set.getVertices(), new Vector2D[][] { new Vector2D[] { new Vector2D( 0.0, 0.0), new Vector2D( 2.0, 0.0), new Vector2D( 2.0, 1.0), new Vector2D( 3.0, 1.0), new Vector2D( 3.0, 3.0), new Vector2D( 1.0, 3.0), new Vector2D( 1.0, 2.0), new Vector2D( 0.0, 2.0) } }); checkPoints(Region.Location.INSIDE, set, new Vector2D[] { new Vector2D(1.0, 1.0), new Vector2D(0.5, 0.5), new Vector2D(2.0, 2.0), new Vector2D(2.5, 2.5), new Vector2D(0.5, 1.5), new Vector2D(1.5, 1.5), new Vector2D(1.5, 0.5), new Vector2D(1.5, 2.5), new Vector2D(2.5, 1.5), new Vector2D(2.5, 2.5) }); checkPoints(Region.Location.OUTSIDE, set, new Vector2D[] { new Vector2D(-0.5, 0.5), new Vector2D( 0.5, 2.5), new Vector2D( 2.5, 0.5), new Vector2D( 3.5, 2.5) }); checkPoints(Region.Location.BOUNDARY, set, new Vector2D[] { new Vector2D(0.0, 0.0), new Vector2D(0.5, 2.0), new Vector2D(2.0, 0.5), new Vector2D(2.5, 1.0), new Vector2D(3.0, 2.5) }); } @Test public void testIntersection() { Vector2D[][] vertices1 = new Vector2D[][] { new Vector2D[] { new Vector2D( 0.0, 0.0), new Vector2D( 2.0, 0.0), new Vector2D( 2.0, 2.0), new Vector2D( 0.0, 2.0) } }; PolygonsSet set1 = buildSet(vertices1); Vector2D[][] vertices2 = new Vector2D[][] { new Vector2D[] { new Vector2D( 1.0, 1.0), new Vector2D( 3.0, 1.0), new Vector2D( 3.0, 3.0), new Vector2D( 1.0, 3.0) } }; PolygonsSet set2 = buildSet(vertices2); PolygonsSet set = (PolygonsSet) new RegionFactory().intersection(set1.copySelf(), set2.copySelf()); checkVertices(set1.getVertices(), vertices1); checkVertices(set2.getVertices(), vertices2); checkVertices(set.getVertices(), new Vector2D[][] { new Vector2D[] { new Vector2D( 1.0, 1.0), new Vector2D( 2.0, 1.0), new Vector2D( 2.0, 2.0), new Vector2D( 1.0, 2.0) } }); checkPoints(Region.Location.INSIDE, set, new Vector2D[] { new Vector2D(1.5, 1.5) }); checkPoints(Region.Location.OUTSIDE, set, new Vector2D[] { new Vector2D(0.5, 1.5), new Vector2D(2.5, 1.5), new Vector2D(1.5, 0.5), new Vector2D(0.5, 0.5) }); checkPoints(Region.Location.BOUNDARY, set, new Vector2D[] { new Vector2D(1.0, 1.0), new Vector2D(2.0, 2.0), new Vector2D(1.0, 1.5), new Vector2D(1.5, 2.0) }); } @Test public void testXor() { Vector2D[][] vertices1 = new Vector2D[][] { new Vector2D[] { new Vector2D( 0.0, 0.0), new Vector2D( 2.0, 0.0), new Vector2D( 2.0, 2.0), new Vector2D( 0.0, 2.0) } }; PolygonsSet set1 = buildSet(vertices1); Vector2D[][] vertices2 = new Vector2D[][] { new Vector2D[] { new Vector2D( 1.0, 1.0), new Vector2D( 3.0, 1.0), new Vector2D( 3.0, 3.0), new Vector2D( 1.0, 3.0) } }; PolygonsSet set2 = buildSet(vertices2); PolygonsSet set = (PolygonsSet) new RegionFactory().xor(set1.copySelf(), set2.copySelf()); checkVertices(set1.getVertices(), vertices1); checkVertices(set2.getVertices(), vertices2); checkVertices(set.getVertices(), new Vector2D[][] { new Vector2D[] { new Vector2D( 0.0, 0.0), new Vector2D( 2.0, 0.0), new Vector2D( 2.0, 1.0), new Vector2D( 3.0, 1.0), new Vector2D( 3.0, 3.0), new Vector2D( 1.0, 3.0), new Vector2D( 1.0, 2.0), new Vector2D( 0.0, 2.0) }, new Vector2D[] { new Vector2D( 1.0, 1.0), new Vector2D( 1.0, 2.0), new Vector2D( 2.0, 2.0), new Vector2D( 2.0, 1.0) } }); checkPoints(Region.Location.INSIDE, set, new Vector2D[] { new Vector2D(0.5, 0.5), new Vector2D(2.5, 2.5), new Vector2D(0.5, 1.5), new Vector2D(1.5, 0.5), new Vector2D(1.5, 2.5), new Vector2D(2.5, 1.5), new Vector2D(2.5, 2.5) }); checkPoints(Region.Location.OUTSIDE, set, new Vector2D[] { new Vector2D(-0.5, 0.5), new Vector2D( 0.5, 2.5), new Vector2D( 2.5, 0.5), new Vector2D( 1.5, 1.5), new Vector2D( 3.5, 2.5) }); checkPoints(Region.Location.BOUNDARY, set, new Vector2D[] { new Vector2D(1.0, 1.0), new Vector2D(2.0, 2.0), new Vector2D(1.5, 1.0), new Vector2D(2.0, 1.5), new Vector2D(0.0, 0.0), new Vector2D(0.5, 2.0), new Vector2D(2.0, 0.5), new Vector2D(2.5, 1.0), new Vector2D(3.0, 2.5) }); } @Test public void testDifference() { Vector2D[][] vertices1 = new Vector2D[][] { new Vector2D[] { new Vector2D( 0.0, 0.0), new Vector2D( 2.0, 0.0), new Vector2D( 2.0, 2.0), new Vector2D( 0.0, 2.0) } }; PolygonsSet set1 = buildSet(vertices1); Vector2D[][] vertices2 = new Vector2D[][] { new Vector2D[] { new Vector2D( 1.0, 1.0), new Vector2D( 3.0, 1.0), new Vector2D( 3.0, 3.0), new Vector2D( 1.0, 3.0) } }; PolygonsSet set2 = buildSet(vertices2); PolygonsSet set = (PolygonsSet) new RegionFactory().difference(set1.copySelf(), set2.copySelf()); checkVertices(set1.getVertices(), vertices1); checkVertices(set2.getVertices(), vertices2); checkVertices(set.getVertices(), new Vector2D[][] { new Vector2D[] { new Vector2D( 0.0, 0.0), new Vector2D( 2.0, 0.0), new Vector2D( 2.0, 1.0), new Vector2D( 1.0, 1.0), new Vector2D( 1.0, 2.0), new Vector2D( 0.0, 2.0) } }); checkPoints(Region.Location.INSIDE, set, new Vector2D[] { new Vector2D(0.5, 0.5), new Vector2D(0.5, 1.5), new Vector2D(1.5, 0.5) }); checkPoints(Region.Location.OUTSIDE, set, new Vector2D[] { new Vector2D( 2.5, 2.5), new Vector2D(-0.5, 0.5), new Vector2D( 0.5, 2.5), new Vector2D( 2.5, 0.5), new Vector2D( 1.5, 1.5), new Vector2D( 3.5, 2.5), new Vector2D( 1.5, 2.5), new Vector2D( 2.5, 1.5), new Vector2D( 2.0, 1.5), new Vector2D( 2.0, 2.0), new Vector2D( 2.5, 1.0), new Vector2D( 2.5, 2.5), new Vector2D( 3.0, 2.5) }); checkPoints(Region.Location.BOUNDARY, set, new Vector2D[] { new Vector2D(1.0, 1.0), new Vector2D(1.5, 1.0), new Vector2D(0.0, 0.0), new Vector2D(0.5, 2.0), new Vector2D(2.0, 0.5) }); } @Test public void testEmptyDifference() { Vector2D[][] vertices1 = new Vector2D[][] { new Vector2D[] { new Vector2D( 0.5, 3.5), new Vector2D( 0.5, 4.5), new Vector2D(-0.5, 4.5), new Vector2D(-0.5, 3.5) } }; PolygonsSet set1 = buildSet(vertices1); Vector2D[][] vertices2 = new Vector2D[][] { new Vector2D[] { new Vector2D( 1.0, 2.0), new Vector2D( 1.0, 8.0), new Vector2D(-1.0, 8.0), new Vector2D(-1.0, 2.0) } }; PolygonsSet set2 = buildSet(vertices2); Assert.assertTrue(new RegionFactory().difference(set1.copySelf(), set2.copySelf()).isEmpty()); } @Test public void testChoppedHexagon() { double pi6 = FastMath.PI / 6.0; double sqrt3 = FastMath.sqrt(3.0); SubLine[] hyp = { new Line(new Vector2D( 0.0, 1.0), 5 * pi6).wholeHyperplane(), new Line(new Vector2D(-sqrt3, 1.0), 7 * pi6).wholeHyperplane(), new Line(new Vector2D(-sqrt3, 1.0), 9 * pi6).wholeHyperplane(), new Line(new Vector2D(-sqrt3, 0.0), 11 * pi6).wholeHyperplane(), new Line(new Vector2D( 0.0, 0.0), 13 * pi6).wholeHyperplane(), new Line(new Vector2D( 0.0, 1.0), 3 * pi6).wholeHyperplane(), new Line(new Vector2D(-5.0 * sqrt3 / 6.0, 0.0), 9 * pi6).wholeHyperplane() }; hyp[1] = (SubLine) hyp[1].split(hyp[0].getHyperplane()).getMinus(); hyp[2] = (SubLine) hyp[2].split(hyp[1].getHyperplane()).getMinus(); hyp[3] = (SubLine) hyp[3].split(hyp[2].getHyperplane()).getMinus(); hyp[4] = (SubLine) hyp[4].split(hyp[3].getHyperplane()).getMinus().split(hyp[0].getHyperplane()).getMinus(); hyp[5] = (SubLine) hyp[5].split(hyp[4].getHyperplane()).getMinus().split(hyp[0].getHyperplane()).getMinus(); hyp[6] = (SubLine) hyp[6].split(hyp[3].getHyperplane()).getMinus().split(hyp[1].getHyperplane()).getMinus(); BSPTree tree = new BSPTree(Boolean.TRUE); for (int i = hyp.length - 1; i >= 0; --i) { tree = new BSPTree(hyp[i], new BSPTree(Boolean.FALSE), tree, null); } PolygonsSet set = new PolygonsSet(tree); SubLine splitter = new Line(new Vector2D(-2.0 * sqrt3 / 3.0, 0.0), 9 * pi6).wholeHyperplane(); PolygonsSet slice = new PolygonsSet(new BSPTree(splitter, set.getTree(false).split(splitter).getPlus(), new BSPTree(Boolean.FALSE), null)); Assert.assertEquals(Region.Location.OUTSIDE, slice.checkPoint(new Vector2D(0.1, 0.5))); Assert.assertEquals(11.0 / 3.0, slice.getBoundarySize(), 1.0e-10); } @Test public void testConcentric() { double h = FastMath.sqrt(3.0) / 2.0; Vector2D[][] vertices1 = new Vector2D[][] { new Vector2D[] { new Vector2D( 0.00, 0.1 * h), new Vector2D( 0.05, 0.1 * h), new Vector2D( 0.10, 0.2 * h), new Vector2D( 0.05, 0.3 * h), new Vector2D(-0.05, 0.3 * h), new Vector2D(-0.10, 0.2 * h), new Vector2D(-0.05, 0.1 * h) } }; PolygonsSet set1 = buildSet(vertices1); Vector2D[][] vertices2 = new Vector2D[][] { new Vector2D[] { new Vector2D( 0.00, 0.0 * h), new Vector2D( 0.10, 0.0 * h), new Vector2D( 0.20, 0.2 * h), new Vector2D( 0.10, 0.4 * h), new Vector2D(-0.10, 0.4 * h), new Vector2D(-0.20, 0.2 * h), new Vector2D(-0.10, 0.0 * h) } }; PolygonsSet set2 = buildSet(vertices2); Assert.assertTrue(set2.contains(set1)); } @Test public void testBug20040520() { BSPTree a0 = new BSPTree(buildSegment(new Vector2D(0.85, -0.05), new Vector2D(0.90, -0.10)), new BSPTree(Boolean.FALSE), new BSPTree(Boolean.TRUE), null); BSPTree a1 = new BSPTree(buildSegment(new Vector2D(0.85, -0.10), new Vector2D(0.90, -0.10)), new BSPTree(Boolean.FALSE), a0, null); BSPTree a2 = new BSPTree(buildSegment(new Vector2D(0.90, -0.05), new Vector2D(0.85, -0.05)), new BSPTree(Boolean.FALSE), a1, null); BSPTree a3 = new BSPTree(buildSegment(new Vector2D(0.82, -0.05), new Vector2D(0.82, -0.08)), new BSPTree(Boolean.FALSE), new BSPTree(Boolean.TRUE), null); BSPTree a4 = new BSPTree(buildHalfLine(new Vector2D(0.85, -0.05), new Vector2D(0.80, -0.05), false), new BSPTree(Boolean.FALSE), a3, null); BSPTree a5 = new BSPTree(buildSegment(new Vector2D(0.82, -0.08), new Vector2D(0.82, -0.18)), new BSPTree(Boolean.FALSE), new BSPTree(Boolean.TRUE), null); BSPTree a6 = new BSPTree(buildHalfLine(new Vector2D(0.82, -0.18), new Vector2D(0.85, -0.15), true), new BSPTree(Boolean.FALSE), a5, null); BSPTree a7 = new BSPTree(buildHalfLine(new Vector2D(0.85, -0.05), new Vector2D(0.82, -0.08), false), a4, a6, null); BSPTree a8 = new BSPTree(buildLine(new Vector2D(0.85, -0.25), new Vector2D(0.85, 0.05)), a2, a7, null); BSPTree a9 = new BSPTree(buildLine(new Vector2D(0.90, 0.05), new Vector2D(0.90, -0.50)), a8, new BSPTree(Boolean.FALSE), null); BSPTree b0 = new BSPTree(buildSegment(new Vector2D(0.92, -0.12), new Vector2D(0.92, -0.08)), new BSPTree(Boolean.FALSE), new BSPTree(Boolean.TRUE), null); BSPTree b1 = new BSPTree(buildHalfLine(new Vector2D(0.92, -0.08), new Vector2D(0.90, -0.10), true), new BSPTree(Boolean.FALSE), b0, null); BSPTree b2 = new BSPTree(buildSegment(new Vector2D(0.92, -0.18), new Vector2D(0.92, -0.12)), new BSPTree(Boolean.FALSE), new BSPTree(Boolean.TRUE), null); BSPTree b3 = new BSPTree(buildSegment(new Vector2D(0.85, -0.15), new Vector2D(0.90, -0.20)), new BSPTree(Boolean.FALSE), b2, null); BSPTree b4 = new BSPTree(buildSegment(new Vector2D(0.95, -0.15), new Vector2D(0.85, -0.05)), b1, b3, null); BSPTree b5 = new BSPTree(buildHalfLine(new Vector2D(0.85, -0.05), new Vector2D(0.85, -0.25), true), new BSPTree(Boolean.FALSE), b4, null); BSPTree b6 = new BSPTree(buildLine(new Vector2D(0.0, -1.10), new Vector2D(1.0, -0.10)), new BSPTree(Boolean.FALSE), b5, null); PolygonsSet c = (PolygonsSet) new RegionFactory().union(new PolygonsSet(a9), new PolygonsSet(b6)); checkPoints(Region.Location.INSIDE, c, new Vector2D[] { new Vector2D(0.83, -0.06), new Vector2D(0.83, -0.15), new Vector2D(0.88, -0.15), new Vector2D(0.88, -0.09), new Vector2D(0.88, -0.07), new Vector2D(0.91, -0.18), new Vector2D(0.91, -0.10) }); checkPoints(Region.Location.OUTSIDE, c, new Vector2D[] { new Vector2D(0.80, -0.10), new Vector2D(0.83, -0.50), new Vector2D(0.83, -0.20), new Vector2D(0.83, -0.02), new Vector2D(0.87, -0.50), new Vector2D(0.87, -0.20), new Vector2D(0.87, -0.02), new Vector2D(0.91, -0.20), new Vector2D(0.91, -0.08), new Vector2D(0.93, -0.15) }); checkVertices(c.getVertices(), new Vector2D[][] { new Vector2D[] { new Vector2D(0.85, -0.15), new Vector2D(0.90, -0.20), new Vector2D(0.92, -0.18), new Vector2D(0.92, -0.08), new Vector2D(0.90, -0.10), new Vector2D(0.90, -0.05), new Vector2D(0.82, -0.05), new Vector2D(0.82, -0.18), } }); } @Test public void testBug20041003() { Line[] l = { new Line(new Vector2D(0.0, 0.625000007541172), new Vector2D(1.0, 0.625000007541172)), new Line(new Vector2D(-0.19204433621902645, 0.0), new Vector2D(-0.19204433621902645, 1.0)), new Line(new Vector2D(-0.40303524786887, 0.4248364535319128), new Vector2D(-1.12851149797877, -0.2634107480798909)), new Line(new Vector2D(0.0, 2.0), new Vector2D(1.0, 2.0)) }; BSPTree node1 = new BSPTree(new SubLine(l[0], new IntervalsSet(intersectionAbscissa(l[0], l[1]), intersectionAbscissa(l[0], l[2]))), new BSPTree(Boolean.TRUE), new BSPTree(Boolean.FALSE), null); BSPTree node2 = new BSPTree(new SubLine(l[1], new IntervalsSet(intersectionAbscissa(l[1], l[2]), intersectionAbscissa(l[1], l[3]))), node1, new BSPTree(Boolean.FALSE), null); BSPTree node3 = new BSPTree(new SubLine(l[2], new IntervalsSet(intersectionAbscissa(l[2], l[3]), Double.POSITIVE_INFINITY)), node2, new BSPTree(Boolean.FALSE), null); BSPTree node4 = new BSPTree(l[3].wholeHyperplane(), node3, new BSPTree(Boolean.FALSE), null); PolygonsSet set = new PolygonsSet(node4); Assert.assertEquals(0, set.getVertices().length); } @Test public void testSqueezedHexa() { PolygonsSet set = new PolygonsSet(1.0e-10, new Vector2D(-6, -4), new Vector2D(-8, -8), new Vector2D( 8, -8), new Vector2D( 6, -4), new Vector2D(10, 4), new Vector2D(-10, 4)); Assert.assertEquals(Location.OUTSIDE, set.checkPoint(new Vector2D(0, 6))); } @Test public void testIssue880Simplified() { Vector2D[] vertices1 = new Vector2D[] { new Vector2D( 90.13595870833188, 38.33604606376991), new Vector2D( 90.14047850603913, 38.34600084496253), new Vector2D( 90.11045289492762, 38.36801537312368), new Vector2D( 90.10871471476526, 38.36878044144294), new Vector2D( 90.10424901707671, 38.374300101757), new Vector2D( 90.0979455456843, 38.373578376172475), new Vector2D( 90.09081227075944, 38.37526295920463), new Vector2D( 90.09081378927135, 38.375193883266434) }; PolygonsSet set1 = new PolygonsSet(1.0e-10, vertices1); Assert.assertEquals(Location.OUTSIDE, set1.checkPoint(new Vector2D(90.12, 38.32))); Assert.assertEquals(Location.OUTSIDE, set1.checkPoint(new Vector2D(90.135, 38.355))); } @Test public void testIssue880Complete() { Vector2D[] vertices1 = new Vector2D[] { new Vector2D( 90.08714908223715, 38.370299337260235), new Vector2D( 90.08709517675004, 38.3702895991413), new Vector2D( 90.08401538704919, 38.368849330127944), new Vector2D( 90.08258210430711, 38.367634558585564), new Vector2D( 90.08251455106665, 38.36763409247078), new Vector2D( 90.08106599752608, 38.36761621664249), new Vector2D( 90.08249585300035, 38.36753627557965), new Vector2D( 90.09075743352184, 38.35914647644972), new Vector2D( 90.09099945896571, 38.35896264724079), new Vector2D( 90.09269383800086, 38.34595756121246), new Vector2D( 90.09638631543191, 38.3457988093121), new Vector2D( 90.09666417351019, 38.34523360999418), new Vector2D( 90.1297082145872, 38.337670454923625), new Vector2D( 90.12971687748956, 38.337669827794684), new Vector2D( 90.1240820219179, 38.34328502001131), new Vector2D( 90.13084259656404, 38.34017811765017), new Vector2D( 90.13378567942857, 38.33860579180606), new Vector2D( 90.13519557833206, 38.33621054663689), new Vector2D( 90.13545616732307, 38.33614965452864), new Vector2D( 90.13553111202748, 38.33613962818305), new Vector2D( 90.1356903436448, 38.33610227127048), new Vector2D( 90.13576283227428, 38.33609255422783), new Vector2D( 90.13595870833188, 38.33604606376991), new Vector2D( 90.1361556630693, 38.3360024198866), new Vector2D( 90.13622408795709, 38.335987048115726), new Vector2D( 90.13696189099994, 38.33581914328681), new Vector2D( 90.13746655304897, 38.33616706665265), new Vector2D( 90.13845973716064, 38.33650776167099), new Vector2D( 90.13950901827667, 38.3368469456463), new Vector2D( 90.14393814424852, 38.337591835857495), new Vector2D( 90.14483839716831, 38.337076122362475), new Vector2D( 90.14565474433601, 38.33769000964429), new Vector2D( 90.14569421179482, 38.3377117256905), new Vector2D( 90.14577067124333, 38.33770883625908), new Vector2D( 90.14600350631684, 38.337714326520995), new Vector2D( 90.14600355139731, 38.33771435193319), new Vector2D( 90.14600369112401, 38.33771443882085), new Vector2D( 90.14600382486884, 38.33771453466096), new Vector2D( 90.14600395205912, 38.33771463904344), new Vector2D( 90.14600407214999, 38.337714751520764), new Vector2D( 90.14600418462749, 38.337714871611695), new Vector2D( 90.14600422249327, 38.337714915811034), new Vector2D( 90.14867838361471, 38.34113888210675), new Vector2D( 90.14923750157374, 38.341582537502575), new Vector2D( 90.14877083250991, 38.34160685841391), new Vector2D( 90.14816667319519, 38.34244232585684), new Vector2D( 90.14797696744586, 38.34248455284745), new Vector2D( 90.14484318014337, 38.34385573215269), new Vector2D( 90.14477919958296, 38.3453797747614), new Vector2D( 90.14202393306448, 38.34464324839456), new Vector2D( 90.14198920640195, 38.344651155237216), new Vector2D( 90.14155207025175, 38.34486424263724), new Vector2D( 90.1415196143314, 38.344871730519), new Vector2D( 90.14128611910814, 38.34500196593859), new Vector2D( 90.14047850603913, 38.34600084496253), new Vector2D( 90.14045907000337, 38.34601860032171), new Vector2D( 90.14039496493928, 38.346223030432384), new Vector2D( 90.14037626063737, 38.346240203360026), new Vector2D( 90.14030005823724, 38.34646920000705), new Vector2D( 90.13799164754806, 38.34903093011013), new Vector2D( 90.11045289492762, 38.36801537312368), new Vector2D( 90.10871471476526, 38.36878044144294), new Vector2D( 90.10424901707671, 38.374300101757), new Vector2D( 90.10263482039932, 38.37310041316073), new Vector2D( 90.09834601753448, 38.373615053823414), new Vector2D( 90.0979455456843, 38.373578376172475), new Vector2D( 90.09086514328669, 38.37527884194668), new Vector2D( 90.09084931407364, 38.37590801712463), new Vector2D( 90.09081227075944, 38.37526295920463), new Vector2D( 90.09081378927135, 38.375193883266434) }; PolygonsSet set1 = new PolygonsSet(1.0e-8, vertices1); Assert.assertEquals(Location.OUTSIDE, set1.checkPoint(new Vector2D(90.0905, 38.3755))); Assert.assertEquals(Location.INSIDE, set1.checkPoint(new Vector2D(90.09084, 38.3755))); Assert.assertEquals(Location.OUTSIDE, set1.checkPoint(new Vector2D(90.0913, 38.3755))); Assert.assertEquals(Location.INSIDE, set1.checkPoint(new Vector2D(90.1042, 38.3739))); Assert.assertEquals(Location.INSIDE, set1.checkPoint(new Vector2D(90.1111, 38.3673))); Assert.assertEquals(Location.OUTSIDE, set1.checkPoint(new Vector2D(90.0959, 38.3457))); Vector2D[] vertices2 = new Vector2D[] { new Vector2D( 90.13067558880044, 38.36977255037573), new Vector2D( 90.12907570488, 38.36817308242706), new Vector2D( 90.1342774136516, 38.356886880294724), new Vector2D( 90.13090330629757, 38.34664392676211), new Vector2D( 90.13078571364593, 38.344904617518466), new Vector2D( 90.1315602208914, 38.3447185040846), new Vector2D( 90.1316336226821, 38.34470643148342), new Vector2D( 90.134020944832, 38.340936644972885), new Vector2D( 90.13912536387306, 38.335497255122334), new Vector2D( 90.1396178806582, 38.334878075552126), new Vector2D( 90.14083049696671, 38.33316530644106), new Vector2D( 90.14145252901329, 38.33152722916191), new Vector2D( 90.1404779335565, 38.32863516047786), new Vector2D( 90.14282712131586, 38.327504432532066), new Vector2D( 90.14616669875488, 38.3237354115015), new Vector2D( 90.14860976050608, 38.315714862457924), new Vector2D( 90.14999277782437, 38.3164932507504), new Vector2D( 90.15005207194997, 38.316534677663356), new Vector2D( 90.15508513859612, 38.31878731691609), new Vector2D( 90.15919938519221, 38.31852743183782), new Vector2D( 90.16093758658837, 38.31880662005153), new Vector2D( 90.16099420184912, 38.318825953291594), new Vector2D( 90.1665411125756, 38.31859497874757), new Vector2D( 90.16999653861313, 38.32505772048029), new Vector2D( 90.17475243391698, 38.32594398441148), new Vector2D( 90.17940844844992, 38.327427213761325), new Vector2D( 90.20951909541378, 38.330616833491774), new Vector2D( 90.2155400467941, 38.331746223670336), new Vector2D( 90.21559881391778, 38.33175551425302), new Vector2D( 90.21916646426041, 38.332584299620805), new Vector2D( 90.23863749852285, 38.34778978875795), new Vector2D( 90.25459855175802, 38.357790570608984), new Vector2D( 90.25964298227257, 38.356918010203174), new Vector2D( 90.26024593994703, 38.361692743151366), new Vector2D( 90.26146187570015, 38.36311080550837), new Vector2D( 90.26614159359622, 38.36510808579902), new Vector2D( 90.26621342936448, 38.36507942500333), new Vector2D( 90.26652190211962, 38.36494042196722), new Vector2D( 90.26621240678867, 38.365113172030874), new Vector2D( 90.26614057102057, 38.365141832826794), new Vector2D( 90.26380080055299, 38.3660381760273), new Vector2D( 90.26315345241, 38.36670658276421), new Vector2D( 90.26251574942881, 38.367490323488084), new Vector2D( 90.26247873448426, 38.36755266444749), new Vector2D( 90.26234628016698, 38.36787989125406), new Vector2D( 90.26214559424784, 38.36945909356126), new Vector2D( 90.25861728442555, 38.37200753430875), new Vector2D( 90.23905557537864, 38.375405314295904), new Vector2D( 90.22517251874075, 38.38984691662256), new Vector2D( 90.22549955153215, 38.3911564273979), new Vector2D( 90.22434386063355, 38.391476432092134), new Vector2D( 90.22147729457276, 38.39134652252034), new Vector2D( 90.22142070120117, 38.391349167741964), new Vector2D( 90.20665060751588, 38.39475580900313), new Vector2D( 90.20042268367109, 38.39842558622888), new Vector2D( 90.17423771242085, 38.402727751805344), new Vector2D( 90.16756796257476, 38.40913898597597), new Vector2D( 90.16728283954308, 38.411255399912875), new Vector2D( 90.16703538220418, 38.41136059866693), new Vector2D( 90.16725865657685, 38.41013618805954), new Vector2D( 90.16746107640665, 38.40902614307544), new Vector2D( 90.16122795307462, 38.39773101873203) }; PolygonsSet set2 = new PolygonsSet(1.0e-8, vertices2); PolygonsSet set = (PolygonsSet) new RegionFactory().difference(set1.copySelf(), set2.copySelf()); Vector2D[][] verticies = set.getVertices(); Assert.assertTrue(verticies[0][0] != null); Assert.assertEquals(1, verticies.length); } private PolygonsSet buildSet(Vector2D[][] vertices) { ArrayList> edges = new ArrayList>(); for (int i = 0; i < vertices.length; ++i) { int l = vertices[i].length; for (int j = 0; j < l; ++j) { edges.add(buildSegment(vertices[i][j], vertices[i][(j + 1) % l])); } } return new PolygonsSet(edges); } private SubHyperplane buildLine(Vector2D start, Vector2D end) { return new Line(start, end).wholeHyperplane(); } private double intersectionAbscissa(Line l0, Line l1) { Vector2D p = l0.intersection(l1); return (l0.toSubSpace(p)).getX(); } private SubHyperplane buildHalfLine(Vector2D start, Vector2D end, boolean startIsVirtual) { Line line = new Line(start, end); double lower = startIsVirtual ? Double.NEGATIVE_INFINITY : (line.toSubSpace(start)).getX(); double upper = startIsVirtual ? (line.toSubSpace(end)).getX() : Double.POSITIVE_INFINITY; return new SubLine(line, new IntervalsSet(lower, upper)); } private SubHyperplane buildSegment(Vector2D start, Vector2D end) { Line line = new Line(start, end); double lower = (line.toSubSpace(start)).getX(); double upper = (line.toSubSpace(end)).getX(); return new SubLine(line, new IntervalsSet(lower, upper)); } private void checkPoints(Region.Location expected, PolygonsSet set, Vector2D[] points) { for (int i = 0; i < points.length; ++i) { Assert.assertEquals(expected, set.checkPoint(points[i])); } } private boolean checkInSegment(Vector2D p, Vector2D p1, Vector2D p2, double tolerance) { Line line = new Line(p1, p2); if (line.getOffset(p) < tolerance) { double x = (line.toSubSpace(p)).getX(); double x1 = (line.toSubSpace(p1)).getX(); double x2 = (line.toSubSpace(p2)).getX(); return (((x - x1) * (x - x2) <= 0.0) || (p1.distance(p) < tolerance) || (p2.distance(p) < tolerance)); } else { return false; } } private void checkVertices(Vector2D[][] rebuiltVertices, Vector2D[][] vertices) { // each rebuilt vertex should be in a segment joining two original vertices for (int i = 0; i < rebuiltVertices.length; ++i) { for (int j = 0; j < rebuiltVertices[i].length; ++j) { boolean inSegment = false; Vector2D p = rebuiltVertices[i][j]; for (int k = 0; k < vertices.length; ++k) { Vector2D[] loop = vertices[k]; int length = loop.length; for (int l = 0; (! inSegment) && (l < length); ++l) { inSegment = checkInSegment(p, loop[l], loop[(l + 1) % length], 1.0e-10); } } Assert.assertTrue(inSegment); } } // each original vertex should have a corresponding rebuilt vertex for (int k = 0; k < vertices.length; ++k) { for (int l = 0; l < vertices[k].length; ++l) { double min = Double.POSITIVE_INFINITY; for (int i = 0; i < rebuiltVertices.length; ++i) { for (int j = 0; j < rebuiltVertices[i].length; ++j) { min = FastMath.min(vertices[k][l].distance(rebuiltVertices[i][j]), min); } } Assert.assertEquals(0.0, min, 1.0e-10); } } } } ././@LongLink100644 0 0 146 12126630646 10262 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/geometry/euclidean/twod/SegmentTest.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/geometry/euclidean/twod/SegmentTest.jav100644 1750 1750 3516 12126627672 32274 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.geometry.euclidean.twod; import org.apache.commons.math3.geometry.euclidean.twod.Line; import org.apache.commons.math3.geometry.euclidean.twod.Vector2D; import org.apache.commons.math3.util.FastMath; import org.junit.Assert; import org.junit.Test; public class SegmentTest { @Test public void testDistance() { Vector2D start = new Vector2D(2, 2); Vector2D end = new Vector2D(-2, -2); Segment segment = new Segment(start, end, new Line(start, end)); // distance to center of segment Assert.assertEquals(FastMath.sqrt(2), segment.distance(new Vector2D(1, -1)), 1.0e-10); // distance a point on segment Assert.assertEquals(FastMath.sin(Math.PI / 4.0), segment.distance(new Vector2D(0, -1)), 1.0e-10); // distance to end point Assert.assertEquals(FastMath.sqrt(8), segment.distance(new Vector2D(0, 4)), 1.0e-10); // distance to start point Assert.assertEquals(FastMath.sqrt(8), segment.distance(new Vector2D(0, -4)), 1.0e-10); } } commons-math3-3.2-src/src/test/java/org/apache/commons/math3/geometry/euclidean/twod/LineTest.java100644 1750 1750 13063 12126627672 31740 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.geometry.euclidean.twod; import org.apache.commons.math3.exception.MathIllegalArgumentException; import org.apache.commons.math3.geometry.euclidean.oned.Euclidean1D; import org.apache.commons.math3.geometry.euclidean.oned.Vector1D; import org.apache.commons.math3.geometry.euclidean.twod.Line; import org.apache.commons.math3.geometry.euclidean.twod.Vector2D; import org.apache.commons.math3.geometry.partitioning.Transform; import org.apache.commons.math3.util.FastMath; import org.junit.Assert; import org.junit.Test; import java.awt.geom.AffineTransform; public class LineTest { @Test public void testContains() { Line l = new Line(new Vector2D(0, 1), new Vector2D(1, 2)); Assert.assertTrue(l.contains(new Vector2D(0, 1))); Assert.assertTrue(l.contains(new Vector2D(1, 2))); Assert.assertTrue(l.contains(new Vector2D(7, 8))); Assert.assertTrue(! l.contains(new Vector2D(8, 7))); } @Test public void testAbscissa() { Line l = new Line(new Vector2D(2, 1), new Vector2D(-2, -2)); Assert.assertEquals(0.0, (l.toSubSpace(new Vector2D(-3, 4))).getX(), 1.0e-10); Assert.assertEquals(0.0, (l.toSubSpace(new Vector2D( 3, -4))).getX(), 1.0e-10); Assert.assertEquals(-5.0, (l.toSubSpace(new Vector2D( 7, -1))).getX(), 1.0e-10); Assert.assertEquals( 5.0, (l.toSubSpace(new Vector2D(-1, -7))).getX(), 1.0e-10); } @Test public void testOffset() { Line l = new Line(new Vector2D(2, 1), new Vector2D(-2, -2)); Assert.assertEquals(-5.0, l.getOffset(new Vector2D(5, -3)), 1.0e-10); Assert.assertEquals(+5.0, l.getOffset(new Vector2D(-5, 2)), 1.0e-10); } @Test public void testDistance() { Line l = new Line(new Vector2D(2, 1), new Vector2D(-2, -2)); Assert.assertEquals(+5.0, l.distance(new Vector2D(5, -3)), 1.0e-10); Assert.assertEquals(+5.0, l.distance(new Vector2D(-5, 2)), 1.0e-10); } @Test public void testPointAt() { Line l = new Line(new Vector2D(2, 1), new Vector2D(-2, -2)); for (double a = -2.0; a < 2.0; a += 0.2) { Vector1D pA = new Vector1D(a); Vector2D point = l.toSpace(pA); Assert.assertEquals(a, (l.toSubSpace(point)).getX(), 1.0e-10); Assert.assertEquals(0.0, l.getOffset(point), 1.0e-10); for (double o = -2.0; o < 2.0; o += 0.2) { point = l.getPointAt(pA, o); Assert.assertEquals(a, (l.toSubSpace(point)).getX(), 1.0e-10); Assert.assertEquals(o, l.getOffset(point), 1.0e-10); } } } @Test public void testOriginOffset() { Line l1 = new Line(new Vector2D(0, 1), new Vector2D(1, 2)); Assert.assertEquals(FastMath.sqrt(0.5), l1.getOriginOffset(), 1.0e-10); Line l2 = new Line(new Vector2D(1, 2), new Vector2D(0, 1)); Assert.assertEquals(-FastMath.sqrt(0.5), l2.getOriginOffset(), 1.0e-10); } @Test public void testParallel() { Line l1 = new Line(new Vector2D(0, 1), new Vector2D(1, 2)); Line l2 = new Line(new Vector2D(2, 2), new Vector2D(3, 3)); Assert.assertTrue(l1.isParallelTo(l2)); Line l3 = new Line(new Vector2D(1, 0), new Vector2D(0.5, -0.5)); Assert.assertTrue(l1.isParallelTo(l3)); Line l4 = new Line(new Vector2D(1, 0), new Vector2D(0.5, -0.51)); Assert.assertTrue(! l1.isParallelTo(l4)); } @Test public void testTransform() throws MathIllegalArgumentException { Line l1 = new Line(new Vector2D(1.0 ,1.0), new Vector2D(4.0 ,1.0)); Transform t1 = Line.getTransform(new AffineTransform(0.0, 0.5, -1.0, 0.0, 1.0, 1.5)); Assert.assertEquals(0.5 * FastMath.PI, ((Line) t1.apply(l1)).getAngle(), 1.0e-10); Line l2 = new Line(new Vector2D(0.0, 0.0), new Vector2D(1.0, 1.0)); Transform t2 = Line.getTransform(new AffineTransform(0.0, 0.5, -1.0, 0.0, 1.0, 1.5)); Assert.assertEquals(FastMath.atan2(1.0, -2.0), ((Line) t2.apply(l2)).getAngle(), 1.0e-10); } @Test public void testIntersection() { Line l1 = new Line(new Vector2D( 0, 1), new Vector2D(1, 2)); Line l2 = new Line(new Vector2D(-1, 2), new Vector2D(2, 1)); Vector2D p = l1.intersection(l2); Assert.assertEquals(0.5, p.getX(), 1.0e-10); Assert.assertEquals(1.5, p.getY(), 1.0e-10); } } ././@LongLink100644 0 0 165 12126630646 10263 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/geometry/euclidean/threed/SphericalCoordinatesTest.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/geometry/euclidean/threed/SphericalCoor100644 1750 1750 25047 12126627671 32330 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.geometry.euclidean.threed; import org.apache.commons.math3.TestUtils; import org.apache.commons.math3.analysis.differentiation.DerivativeStructure; import org.apache.commons.math3.exception.DimensionMismatchException; import org.apache.commons.math3.util.FastMath; import org.junit.Assert; import org.junit.Test; public class SphericalCoordinatesTest { @Test public void testCoordinatesStoC() throws DimensionMismatchException { double piO2 = 0.5 * FastMath.PI; SphericalCoordinates sc1 = new SphericalCoordinates(2.0, 0, piO2); Assert.assertEquals(0, sc1.getCartesian().distance(new Vector3D(2, 0, 0)), 1.0e-10); SphericalCoordinates sc2 = new SphericalCoordinates(2.0, piO2, piO2); Assert.assertEquals(0, sc2.getCartesian().distance(new Vector3D(0, 2, 0)), 1.0e-10); SphericalCoordinates sc3 = new SphericalCoordinates(2.0, FastMath.PI, piO2); Assert.assertEquals(0, sc3.getCartesian().distance(new Vector3D(-2, 0, 0)), 1.0e-10); SphericalCoordinates sc4 = new SphericalCoordinates(2.0, -piO2, piO2); Assert.assertEquals(0, sc4.getCartesian().distance(new Vector3D(0, -2, 0)), 1.0e-10); SphericalCoordinates sc5 = new SphericalCoordinates(2.0, 1.23456, 0); Assert.assertEquals(0, sc5.getCartesian().distance(new Vector3D(0, 0, 2)), 1.0e-10); SphericalCoordinates sc6 = new SphericalCoordinates(2.0, 6.54321, FastMath.PI); Assert.assertEquals(0, sc6.getCartesian().distance(new Vector3D(0, 0, -2)), 1.0e-10); } @Test public void testCoordinatesCtoS() throws DimensionMismatchException { double piO2 = 0.5 * FastMath.PI; SphericalCoordinates sc1 = new SphericalCoordinates(new Vector3D(2, 0, 0)); Assert.assertEquals(2, sc1.getR(), 1.0e-10); Assert.assertEquals(0, sc1.getTheta(), 1.0e-10); Assert.assertEquals(piO2, sc1.getPhi(), 1.0e-10); SphericalCoordinates sc2 = new SphericalCoordinates(new Vector3D(0, 2, 0)); Assert.assertEquals(2, sc2.getR(), 1.0e-10); Assert.assertEquals(piO2, sc2.getTheta(), 1.0e-10); Assert.assertEquals(piO2, sc2.getPhi(), 1.0e-10); SphericalCoordinates sc3 = new SphericalCoordinates(new Vector3D(-2, 0, 0)); Assert.assertEquals(2, sc3.getR(), 1.0e-10); Assert.assertEquals(FastMath.PI, sc3.getTheta(), 1.0e-10); Assert.assertEquals(piO2, sc3.getPhi(), 1.0e-10); SphericalCoordinates sc4 = new SphericalCoordinates(new Vector3D(0, -2, 0)); Assert.assertEquals(2, sc4.getR(), 1.0e-10); Assert.assertEquals(-piO2, sc4.getTheta(), 1.0e-10); Assert.assertEquals(piO2, sc4.getPhi(), 1.0e-10); SphericalCoordinates sc5 = new SphericalCoordinates(new Vector3D(0, 0, 2)); Assert.assertEquals(2, sc5.getR(), 1.0e-10); // don't check theta on poles, as it is singular Assert.assertEquals(0, sc5.getPhi(), 1.0e-10); SphericalCoordinates sc6 = new SphericalCoordinates(new Vector3D(0, 0, -2)); Assert.assertEquals(2, sc6.getR(), 1.0e-10); // don't check theta on poles, as it is singular Assert.assertEquals(FastMath.PI, sc6.getPhi(), 1.0e-10); } @Test public void testGradient() { for (double r = 0.2; r < 10; r += 0.5) { for (double theta = 0; theta < 2 * FastMath.PI; theta += 0.1) { for (double phi = 0.1; phi < FastMath.PI; phi += 0.1) { SphericalCoordinates sc = new SphericalCoordinates(r, theta, phi); DerivativeStructure svalue = valueSpherical(new DerivativeStructure(3, 1, 0, r), new DerivativeStructure(3, 1, 1, theta), new DerivativeStructure(3, 1, 2, phi)); double[] sGradient = new double[] { svalue.getPartialDerivative(1, 0, 0), svalue.getPartialDerivative(0, 1, 0), svalue.getPartialDerivative(0, 0, 1), }; DerivativeStructure cvalue = valueCartesian(new DerivativeStructure(3, 1, 0, sc.getCartesian().getX()), new DerivativeStructure(3, 1, 1, sc.getCartesian().getY()), new DerivativeStructure(3, 1, 2, sc.getCartesian().getZ())); Vector3D refCGradient = new Vector3D(cvalue.getPartialDerivative(1, 0, 0), cvalue.getPartialDerivative(0, 1, 0), cvalue.getPartialDerivative(0, 0, 1)); Vector3D testCGradient = new Vector3D(sc.toCartesianGradient(sGradient)); Assert.assertEquals(0, testCGradient.distance(refCGradient) / refCGradient.getNorm(), 5.0e-14); } } } } @Test public void testHessian() { for (double r = 0.2; r < 10; r += 0.5) { for (double theta = 0; theta < 2 * FastMath.PI; theta += 0.2) { for (double phi = 0.1; phi < FastMath.PI; phi += 0.2) { SphericalCoordinates sc = new SphericalCoordinates(r, theta, phi); DerivativeStructure svalue = valueSpherical(new DerivativeStructure(3, 2, 0, r), new DerivativeStructure(3, 2, 1, theta), new DerivativeStructure(3, 2, 2, phi)); double[] sGradient = new double[] { svalue.getPartialDerivative(1, 0, 0), svalue.getPartialDerivative(0, 1, 0), svalue.getPartialDerivative(0, 0, 1), }; double[][] sHessian = new double[3][3]; sHessian[0][0] = svalue.getPartialDerivative(2, 0, 0); // d2F/dR2 sHessian[1][0] = svalue.getPartialDerivative(1, 1, 0); // d2F/dRdTheta sHessian[2][0] = svalue.getPartialDerivative(1, 0, 1); // d2F/dRdPhi sHessian[0][1] = Double.NaN; // just to check upper-right part is not used sHessian[1][1] = svalue.getPartialDerivative(0, 2, 0); // d2F/dTheta2 sHessian[2][1] = svalue.getPartialDerivative(0, 1, 1); // d2F/dThetadPhi sHessian[0][2] = Double.NaN; // just to check upper-right part is not used sHessian[1][2] = Double.NaN; // just to check upper-right part is not used sHessian[2][2] = svalue.getPartialDerivative(0, 0, 2); // d2F/dPhi2 DerivativeStructure cvalue = valueCartesian(new DerivativeStructure(3, 2, 0, sc.getCartesian().getX()), new DerivativeStructure(3, 2, 1, sc.getCartesian().getY()), new DerivativeStructure(3, 2, 2, sc.getCartesian().getZ())); double[][] refCHessian = new double[3][3]; refCHessian[0][0] = cvalue.getPartialDerivative(2, 0, 0); // d2F/dX2 refCHessian[1][0] = cvalue.getPartialDerivative(1, 1, 0); // d2F/dXdY refCHessian[2][0] = cvalue.getPartialDerivative(1, 0, 1); // d2F/dXdZ refCHessian[0][1] = refCHessian[1][0]; refCHessian[1][1] = cvalue.getPartialDerivative(0, 2, 0); // d2F/dY2 refCHessian[2][1] = cvalue.getPartialDerivative(0, 1, 1); // d2F/dYdZ refCHessian[0][2] = refCHessian[2][0]; refCHessian[1][2] = refCHessian[2][1]; refCHessian[2][2] = cvalue.getPartialDerivative(0, 0, 2); // d2F/dZ2 double norm = 0; for (int i = 0; i < 3; ++i) { for (int j = 0; j < 3; ++j) { norm = FastMath.max(norm, FastMath.abs(refCHessian[i][j])); } } double[][] testCHessian = sc.toCartesianHessian(sHessian, sGradient); for (int i = 0; i < 3; ++i) { for (int j = 0; j < 3; ++j) { Assert.assertEquals("" + FastMath.abs((refCHessian[i][j] - testCHessian[i][j]) / norm), refCHessian[i][j], testCHessian[i][j], 1.0e-14 * norm); } } } } } } public DerivativeStructure valueCartesian(DerivativeStructure x, DerivativeStructure y, DerivativeStructure z) { return x.divide(y.multiply(5).add(10)).multiply(z.pow(3)); } public DerivativeStructure valueSpherical(DerivativeStructure r, DerivativeStructure theta, DerivativeStructure phi) { return valueCartesian(r.multiply(theta.cos()).multiply(phi.sin()), r.multiply(theta.sin()).multiply(phi.sin()), r.multiply(phi.cos())); } @Test public void testSerialization() { SphericalCoordinates a = new SphericalCoordinates(3, 2, 1); SphericalCoordinates b = (SphericalCoordinates) TestUtils.serializeAndRecover(a); Assert.assertEquals(0, a.getCartesian().distance(b.getCartesian()), 1.0e-10); Assert.assertEquals(a.getR(), b.getR(), 1.0e-10); Assert.assertEquals(a.getTheta(), b.getTheta(), 1.0e-10); Assert.assertEquals(a.getPhi(), b.getPhi(), 1.0e-10); } } ././@LongLink100644 0 0 150 12126630646 10255 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/geometry/euclidean/threed/SubLineTest.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/geometry/euclidean/threed/SubLineTest.j100644 1750 1750 20162 12126627671 32215 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.geometry.euclidean.threed; import java.util.List; import org.apache.commons.math3.exception.MathIllegalArgumentException; import org.apache.commons.math3.geometry.euclidean.oned.Euclidean1D; import org.apache.commons.math3.geometry.euclidean.oned.IntervalsSet; import org.apache.commons.math3.geometry.partitioning.RegionFactory; import org.junit.Assert; import org.junit.Test; public class SubLineTest { @Test public void testEndPoints() throws MathIllegalArgumentException { Vector3D p1 = new Vector3D(-1, -7, 2); Vector3D p2 = new Vector3D(7, -1, 0); Segment segment = new Segment(p1, p2, new Line(p1, p2)); SubLine sub = new SubLine(segment); List segments = sub.getSegments(); Assert.assertEquals(1, segments.size()); Assert.assertEquals(0.0, new Vector3D(-1, -7, 2).distance(segments.get(0).getStart()), 1.0e-10); Assert.assertEquals(0.0, new Vector3D( 7, -1, 0).distance(segments.get(0).getEnd()), 1.0e-10); } @Test public void testNoEndPoints() throws MathIllegalArgumentException { SubLine wholeLine = new Line(new Vector3D(-1, 7, 2), new Vector3D(7, 1, 0)).wholeLine(); List segments = wholeLine.getSegments(); Assert.assertEquals(1, segments.size()); Assert.assertTrue(Double.isInfinite(segments.get(0).getStart().getX()) && segments.get(0).getStart().getX() < 0); Assert.assertTrue(Double.isInfinite(segments.get(0).getStart().getY()) && segments.get(0).getStart().getY() > 0); Assert.assertTrue(Double.isInfinite(segments.get(0).getStart().getZ()) && segments.get(0).getStart().getZ() > 0); Assert.assertTrue(Double.isInfinite(segments.get(0).getEnd().getX()) && segments.get(0).getEnd().getX() > 0); Assert.assertTrue(Double.isInfinite(segments.get(0).getEnd().getY()) && segments.get(0).getEnd().getY() < 0); Assert.assertTrue(Double.isInfinite(segments.get(0).getEnd().getZ()) && segments.get(0).getEnd().getZ() < 0); } @Test public void testNoSegments() throws MathIllegalArgumentException { SubLine empty = new SubLine(new Line(new Vector3D(-1, -7, 2), new Vector3D(7, -1, 0)), (IntervalsSet) new RegionFactory().getComplement(new IntervalsSet())); List segments = empty.getSegments(); Assert.assertEquals(0, segments.size()); } @Test public void testSeveralSegments() throws MathIllegalArgumentException { SubLine twoSubs = new SubLine(new Line(new Vector3D(-1, -7, 2), new Vector3D(7, -1, 0)), (IntervalsSet) new RegionFactory().union(new IntervalsSet(1, 2), new IntervalsSet(3, 4))); List segments = twoSubs.getSegments(); Assert.assertEquals(2, segments.size()); } @Test public void testHalfInfiniteNeg() throws MathIllegalArgumentException { SubLine empty = new SubLine(new Line(new Vector3D(-1, -7, 2), new Vector3D(7, -1, -2)), new IntervalsSet(Double.NEGATIVE_INFINITY, 0.0)); List segments = empty.getSegments(); Assert.assertEquals(1, segments.size()); Assert.assertTrue(Double.isInfinite(segments.get(0).getStart().getX()) && segments.get(0).getStart().getX() < 0); Assert.assertTrue(Double.isInfinite(segments.get(0).getStart().getY()) && segments.get(0).getStart().getY() < 0); Assert.assertTrue(Double.isInfinite(segments.get(0).getStart().getZ()) && segments.get(0).getStart().getZ() > 0); Assert.assertEquals(0.0, new Vector3D(3, -4, 0).distance(segments.get(0).getEnd()), 1.0e-10); } @Test public void testHalfInfinitePos() throws MathIllegalArgumentException { SubLine empty = new SubLine(new Line(new Vector3D(-1, -7, 2), new Vector3D(7, -1, -2)), new IntervalsSet(0.0, Double.POSITIVE_INFINITY)); List segments = empty.getSegments(); Assert.assertEquals(1, segments.size()); Assert.assertEquals(0.0, new Vector3D(3, -4, 0).distance(segments.get(0).getStart()), 1.0e-10); Assert.assertTrue(Double.isInfinite(segments.get(0).getEnd().getX()) && segments.get(0).getEnd().getX() > 0); Assert.assertTrue(Double.isInfinite(segments.get(0).getEnd().getY()) && segments.get(0).getEnd().getY() > 0); Assert.assertTrue(Double.isInfinite(segments.get(0).getEnd().getZ()) && segments.get(0).getEnd().getZ() < 0); } @Test public void testIntersectionInsideInside() throws MathIllegalArgumentException { SubLine sub1 = new SubLine(new Vector3D(1, 1, 1), new Vector3D(3, 1, 1)); SubLine sub2 = new SubLine(new Vector3D(2, 0, 0), new Vector3D(2, 2, 2)); Assert.assertEquals(0.0, new Vector3D(2, 1, 1).distance(sub1.intersection(sub2, true)), 1.0e-12); Assert.assertEquals(0.0, new Vector3D(2, 1, 1).distance(sub1.intersection(sub2, false)), 1.0e-12); } @Test public void testIntersectionInsideBoundary() throws MathIllegalArgumentException { SubLine sub1 = new SubLine(new Vector3D(1, 1, 1), new Vector3D(3, 1, 1)); SubLine sub2 = new SubLine(new Vector3D(2, 0, 0), new Vector3D(2, 1, 1)); Assert.assertEquals(0.0, new Vector3D(2, 1, 1).distance(sub1.intersection(sub2, true)), 1.0e-12); Assert.assertNull(sub1.intersection(sub2, false)); } @Test public void testIntersectionInsideOutside() throws MathIllegalArgumentException { SubLine sub1 = new SubLine(new Vector3D(1, 1, 1), new Vector3D(3, 1, 1)); SubLine sub2 = new SubLine(new Vector3D(2, 0, 0), new Vector3D(2, 0.5, 0.5)); Assert.assertNull(sub1.intersection(sub2, true)); Assert.assertNull(sub1.intersection(sub2, false)); } @Test public void testIntersectionBoundaryBoundary() throws MathIllegalArgumentException { SubLine sub1 = new SubLine(new Vector3D(1, 1, 1), new Vector3D(2, 1, 1)); SubLine sub2 = new SubLine(new Vector3D(2, 0, 0), new Vector3D(2, 1, 1)); Assert.assertEquals(0.0, new Vector3D(2, 1, 1).distance(sub1.intersection(sub2, true)), 1.0e-12); Assert.assertNull(sub1.intersection(sub2, false)); } @Test public void testIntersectionBoundaryOutside() throws MathIllegalArgumentException { SubLine sub1 = new SubLine(new Vector3D(1, 1, 1), new Vector3D(2, 1, 1)); SubLine sub2 = new SubLine(new Vector3D(2, 0, 0), new Vector3D(2, 0.5, 0.5)); Assert.assertNull(sub1.intersection(sub2, true)); Assert.assertNull(sub1.intersection(sub2, false)); } @Test public void testIntersectionOutsideOutside() throws MathIllegalArgumentException { SubLine sub1 = new SubLine(new Vector3D(1, 1, 1), new Vector3D(1.5, 1, 1)); SubLine sub2 = new SubLine(new Vector3D(2, 0, 0), new Vector3D(2, 0.5, 0.5)); Assert.assertNull(sub1.intersection(sub2, true)); Assert.assertNull(sub1.intersection(sub2, false)); } } ././@LongLink100644 0 0 161 12126630646 10257 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/geometry/euclidean/threed/FieldRotationDfpTest.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/geometry/euclidean/threed/FieldRotation100644 1750 1750 76617 12126627671 32347 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.geometry.euclidean.threed; import org.apache.commons.math3.dfp.Dfp; import org.apache.commons.math3.dfp.DfpField; import org.apache.commons.math3.exception.MathArithmeticException; import org.apache.commons.math3.exception.MathIllegalArgumentException; import org.apache.commons.math3.random.UnitSphereRandomVectorGenerator; import org.apache.commons.math3.random.Well1024a; import org.apache.commons.math3.util.FastMath; import org.apache.commons.math3.util.MathUtils; import org.junit.Assert; import org.junit.Test; public class FieldRotationDfpTest { @Test public void testIdentity() { FieldRotation r = createRotation(1, 0, 0, 0, false); checkVector(r.applyTo(createVector(1, 0, 0)), createVector(1, 0, 0)); checkVector(r.applyTo(createVector(0, 1, 0)), createVector(0, 1, 0)); checkVector(r.applyTo(createVector(0, 0, 1)), createVector(0, 0, 1)); checkAngle(r.getAngle(), 0); r = createRotation(-1, 0, 0, 0, false); checkVector(r.applyTo(createVector(1, 0, 0)), createVector(1, 0, 0)); checkVector(r.applyTo(createVector(0, 1, 0)), createVector(0, 1, 0)); checkVector(r.applyTo(createVector(0, 0, 1)), createVector(0, 0, 1)); checkAngle(r.getAngle(), 0); r = createRotation(42, 0, 0, 0, true); checkVector(r.applyTo(createVector(1, 0, 0)), createVector(1, 0, 0)); checkVector(r.applyTo(createVector(0, 1, 0)), createVector(0, 1, 0)); checkVector(r.applyTo(createVector(0, 0, 1)), createVector(0, 0, 1)); checkAngle(r.getAngle(), 0); } @Test public void testAxisAngle() throws MathIllegalArgumentException { FieldRotation r = new FieldRotation(createAxis(10, 10, 10), createAngle(2 * FastMath.PI / 3)); checkVector(r.applyTo(createVector(1, 0, 0)), createVector(0, 1, 0)); checkVector(r.applyTo(createVector(0, 1, 0)), createVector(0, 0, 1)); checkVector(r.applyTo(createVector(0, 0, 1)), createVector(1, 0, 0)); double s = 1 / FastMath.sqrt(3); checkVector(r.getAxis(), createVector(s, s, s)); checkAngle(r.getAngle(), 2 * FastMath.PI / 3); try { new FieldRotation(createAxis(0, 0, 0), createAngle(2 * FastMath.PI / 3)); Assert.fail("an exception should have been thrown"); } catch (MathIllegalArgumentException e) { } r = new FieldRotation(createAxis(0, 0, 1), createAngle(1.5 * FastMath.PI)); checkVector(r.getAxis(), createVector(0, 0, -1)); checkAngle(r.getAngle(), 0.5 * FastMath.PI); r = new FieldRotation(createAxis(0, 1, 0), createAngle(FastMath.PI)); checkVector(r.getAxis(), createVector(0, 1, 0)); checkAngle(r.getAngle(), FastMath.PI); checkVector(createRotation(1, 0, 0, 0, false).getAxis(), createVector(1, 0, 0)); } @Test public void testRevert() { double a = 0.001; double b = 0.36; double c = 0.48; double d = 0.8; FieldRotation r = createRotation(a, b, c, d, true); FieldRotation reverted = r.revert(); FieldRotation rrT = r.applyTo(reverted); checkRotationDS(rrT, 1, 0, 0, 0); FieldRotation rTr = reverted.applyTo(r); checkRotationDS(rTr, 1, 0, 0, 0); Assert.assertEquals(r.getAngle().getReal(), reverted.getAngle().getReal(), 1.0e-15); Assert.assertEquals(-1, FieldVector3D.dotProduct(r.getAxis(), reverted.getAxis()).getReal(), 1.0e-15); } @Test public void testVectorOnePair() throws MathArithmeticException { FieldVector3D u = createVector(3, 2, 1); FieldVector3D v = createVector(-4, 2, 2); FieldRotation r = new FieldRotation(u, v); checkVector(r.applyTo(u.scalarMultiply(v.getNorm())), v.scalarMultiply(u.getNorm())); checkAngle(new FieldRotation(u, u.negate()).getAngle(), FastMath.PI); try { new FieldRotation(u, createVector(0, 0, 0)); Assert.fail("an exception should have been thrown"); } catch (MathArithmeticException e) { // expected behavior } } @Test public void testVectorTwoPairs() throws MathArithmeticException { FieldVector3D u1 = createVector(3, 0, 0); FieldVector3D u2 = createVector(0, 5, 0); FieldVector3D v1 = createVector(0, 0, 2); FieldVector3D v2 = createVector(-2, 0, 2); FieldRotation r = new FieldRotation(u1, u2, v1, v2); checkVector(r.applyTo(createVector(1, 0, 0)), createVector(0, 0, 1)); checkVector(r.applyTo(createVector(0, 1, 0)), createVector(-1, 0, 0)); r = new FieldRotation(u1, u2, u1.negate(), u2.negate()); FieldVector3D axis = r.getAxis(); if (FieldVector3D.dotProduct(axis, createVector(0, 0, 1)).getReal() > 0) { checkVector(axis, createVector(0, 0, 1)); } else { checkVector(axis, createVector(0, 0, -1)); } checkAngle(r.getAngle(), FastMath.PI); double sqrt = FastMath.sqrt(2) / 2; r = new FieldRotation(createVector(1, 0, 0), createVector(0, 1, 0), createVector(0.5, 0.5, sqrt), createVector(0.5, 0.5, -sqrt)); checkRotationDS(r, sqrt, 0.5, 0.5, 0); r = new FieldRotation(u1, u2, u1, FieldVector3D.crossProduct(u1, u2)); checkRotationDS(r, sqrt, -sqrt, 0, 0); checkRotationDS(new FieldRotation(u1, u2, u1, u2), 1, 0, 0, 0); try { new FieldRotation(u1, u2, createVector(0, 0, 0), v2); Assert.fail("an exception should have been thrown"); } catch (MathArithmeticException e) { // expected behavior } } @Test public void testMatrix() throws NotARotationMatrixException { try { createRotation(new double[][] { { 0.0, 1.0, 0.0 }, { 1.0, 0.0, 0.0 } }, 1.0e-7); Assert.fail("Expecting NotARotationMatrixException"); } catch (NotARotationMatrixException nrme) { // expected behavior } try { createRotation(new double[][] { { 0.445888, 0.797184, -0.407040 }, { 0.821760, -0.184320, 0.539200 }, { -0.354816, 0.574912, 0.737280 } }, 1.0e-7); Assert.fail("Expecting NotARotationMatrixException"); } catch (NotARotationMatrixException nrme) { // expected behavior } try { createRotation(new double[][] { { 0.4, 0.8, -0.4 }, { -0.4, 0.6, 0.7 }, { 0.8, -0.2, 0.5 } }, 1.0e-15); Assert.fail("Expecting NotARotationMatrixException"); } catch (NotARotationMatrixException nrme) { // expected behavior } checkRotationDS(createRotation(new double[][] { { 0.445888, 0.797184, -0.407040 }, { -0.354816, 0.574912, 0.737280 }, { 0.821760, -0.184320, 0.539200 } }, 1.0e-10), 0.8, 0.288, 0.384, 0.36); checkRotationDS(createRotation(new double[][] { { 0.539200, 0.737280, 0.407040 }, { 0.184320, -0.574912, 0.797184 }, { 0.821760, -0.354816, -0.445888 } }, 1.0e-10), 0.36, 0.8, 0.288, 0.384); checkRotationDS(createRotation(new double[][] { { -0.445888, 0.797184, -0.407040 }, { 0.354816, 0.574912, 0.737280 }, { 0.821760, 0.184320, -0.539200 } }, 1.0e-10), 0.384, 0.36, 0.8, 0.288); checkRotationDS(createRotation(new double[][] { { -0.539200, 0.737280, 0.407040 }, { -0.184320, -0.574912, 0.797184 }, { 0.821760, 0.354816, 0.445888 } }, 1.0e-10), 0.288, 0.384, 0.36, 0.8); double[][] m1 = { { 0.0, 1.0, 0.0 }, { 0.0, 0.0, 1.0 }, { 1.0, 0.0, 0.0 } }; FieldRotation r = createRotation(m1, 1.0e-7); checkVector(r.applyTo(createVector(1, 0, 0)), createVector(0, 0, 1)); checkVector(r.applyTo(createVector(0, 1, 0)), createVector(1, 0, 0)); checkVector(r.applyTo(createVector(0, 0, 1)), createVector(0, 1, 0)); double[][] m2 = { { 0.83203, -0.55012, -0.07139 }, { 0.48293, 0.78164, -0.39474 }, { 0.27296, 0.29396, 0.91602 } }; r = createRotation(m2, 1.0e-12); Dfp[][] m3 = r.getMatrix(); double d00 = m2[0][0] - m3[0][0].getReal(); double d01 = m2[0][1] - m3[0][1].getReal(); double d02 = m2[0][2] - m3[0][2].getReal(); double d10 = m2[1][0] - m3[1][0].getReal(); double d11 = m2[1][1] - m3[1][1].getReal(); double d12 = m2[1][2] - m3[1][2].getReal(); double d20 = m2[2][0] - m3[2][0].getReal(); double d21 = m2[2][1] - m3[2][1].getReal(); double d22 = m2[2][2] - m3[2][2].getReal(); Assert.assertTrue(FastMath.abs(d00) < 6.0e-6); Assert.assertTrue(FastMath.abs(d01) < 6.0e-6); Assert.assertTrue(FastMath.abs(d02) < 6.0e-6); Assert.assertTrue(FastMath.abs(d10) < 6.0e-6); Assert.assertTrue(FastMath.abs(d11) < 6.0e-6); Assert.assertTrue(FastMath.abs(d12) < 6.0e-6); Assert.assertTrue(FastMath.abs(d20) < 6.0e-6); Assert.assertTrue(FastMath.abs(d21) < 6.0e-6); Assert.assertTrue(FastMath.abs(d22) < 6.0e-6); Assert.assertTrue(FastMath.abs(d00) > 4.0e-7); Assert.assertTrue(FastMath.abs(d01) > 4.0e-7); Assert.assertTrue(FastMath.abs(d02) > 4.0e-7); Assert.assertTrue(FastMath.abs(d10) > 4.0e-7); Assert.assertTrue(FastMath.abs(d11) > 4.0e-7); Assert.assertTrue(FastMath.abs(d12) > 4.0e-7); Assert.assertTrue(FastMath.abs(d20) > 4.0e-7); Assert.assertTrue(FastMath.abs(d21) > 4.0e-7); Assert.assertTrue(FastMath.abs(d22) > 4.0e-7); for (int i = 0; i < 3; ++i) { for (int j = 0; j < 3; ++j) { double m3tm3 = m3[i][0].getReal() * m3[j][0].getReal() + m3[i][1].getReal() * m3[j][1].getReal() + m3[i][2].getReal() * m3[j][2].getReal(); if (i == j) { Assert.assertTrue(FastMath.abs(m3tm3 - 1.0) < 1.0e-10); } else { Assert.assertTrue(FastMath.abs(m3tm3) < 1.0e-10); } } } checkVector(r.applyTo(createVector(1, 0, 0)), new FieldVector3D(m3[0][0], m3[1][0], m3[2][0])); checkVector(r.applyTo(createVector(0, 1, 0)), new FieldVector3D(m3[0][1], m3[1][1], m3[2][1])); checkVector(r.applyTo(createVector(0, 0, 1)), new FieldVector3D(m3[0][2], m3[1][2], m3[2][2])); double[][] m4 = { { 1.0, 0.0, 0.0 }, { 0.0, -1.0, 0.0 }, { 0.0, 0.0, -1.0 } }; r = createRotation(m4, 1.0e-7); checkAngle(r.getAngle(), FastMath.PI); try { double[][] m5 = { { 0.0, 0.0, 1.0 }, { 0.0, 1.0, 0.0 }, { 1.0, 0.0, 0.0 } }; r = createRotation(m5, 1.0e-7); Assert.fail("got " + r + ", should have caught an exception"); } catch (NotARotationMatrixException e) { // expected } } @Test public void testAngles() throws CardanEulerSingularityException { DfpField field = new DfpField(15); RotationOrder[] CardanOrders = { RotationOrder.XYZ, RotationOrder.XZY, RotationOrder.YXZ, RotationOrder.YZX, RotationOrder.ZXY, RotationOrder.ZYX }; for (int i = 0; i < CardanOrders.length; ++i) { for (double alpha1 = 0.1; alpha1 < 6.2; alpha1 += 2.0) { for (double alpha2 = -1.55; alpha2 < 1.55; alpha2 += 0.8) { for (double alpha3 = 0.1; alpha3 < 6.2; alpha3 += 2.0) { FieldRotation r = new FieldRotation(CardanOrders[i], field.newDfp(alpha1), field.newDfp(alpha2), field.newDfp(alpha3)); Dfp[] angles = r.getAngles(CardanOrders[i]); checkAngle(angles[0], alpha1); checkAngle(angles[1], alpha2); checkAngle(angles[2], alpha3); } } } } RotationOrder[] EulerOrders = { RotationOrder.XYX, RotationOrder.XZX, RotationOrder.YXY, RotationOrder.YZY, RotationOrder.ZXZ, RotationOrder.ZYZ }; for (int i = 0; i < EulerOrders.length; ++i) { for (double alpha1 = 0.1; alpha1 < 6.2; alpha1 += 2.0) { for (double alpha2 = 0.05; alpha2 < 3.1; alpha2 += 0.8) { for (double alpha3 = 0.1; alpha3 < 6.2; alpha3 += 2.0) { FieldRotation r = new FieldRotation(EulerOrders[i], field.newDfp(alpha1), field.newDfp(alpha2), field.newDfp(alpha3)); Dfp[] angles = r.getAngles(EulerOrders[i]); checkAngle(angles[0], alpha1); checkAngle(angles[1], alpha2); checkAngle(angles[2], alpha3); } } } } } @Test public void testSingularities() { DfpField field = new DfpField(20); RotationOrder[] CardanOrders = { RotationOrder.XYZ, RotationOrder.XZY, RotationOrder.YXZ, RotationOrder.YZX, RotationOrder.ZXY, RotationOrder.ZYX }; double[] singularCardanAngle = { FastMath.PI / 2, -FastMath.PI / 2 }; for (int i = 0; i < CardanOrders.length; ++i) { for (int j = 0; j < singularCardanAngle.length; ++j) { FieldRotation r = new FieldRotation(CardanOrders[i], field.newDfp(0.1), field.newDfp(singularCardanAngle[j]), field.newDfp(0.3)); try { r.getAngles(CardanOrders[i]); Assert.fail("an exception should have been caught"); } catch (CardanEulerSingularityException cese) { // expected behavior } } } RotationOrder[] EulerOrders = { RotationOrder.XYX, RotationOrder.XZX, RotationOrder.YXY, RotationOrder.YZY, RotationOrder.ZXZ, RotationOrder.ZYZ }; double[] singularEulerAngle = { 0, FastMath.PI }; for (int i = 0; i < EulerOrders.length; ++i) { for (int j = 0; j < singularEulerAngle.length; ++j) { FieldRotation r = new FieldRotation(EulerOrders[i], field.newDfp(0.1), field.newDfp(singularEulerAngle[j]), field.newDfp(0.3)); try { r.getAngles(EulerOrders[i]); Assert.fail("an exception should have been caught"); } catch (CardanEulerSingularityException cese) { // expected behavior } } } } @Test public void testQuaternion() throws MathIllegalArgumentException { FieldRotation r1 = new FieldRotation(createVector(2, -3, 5), createAngle(1.7)); double n = 23.5; FieldRotation r2 = new FieldRotation(r1.getQ0().multiply(n), r1.getQ1().multiply(n), r1.getQ2().multiply(n), r1.getQ3().multiply(n), true); for (double x = -0.9; x < 0.9; x += 0.2) { for (double y = -0.9; y < 0.9; y += 0.2) { for (double z = -0.9; z < 0.9; z += 0.2) { FieldVector3D u = createVector(x, y, z); checkVector(r2.applyTo(u), r1.applyTo(u)); } } } r1 = createRotation(0.288, 0.384, 0.36, 0.8, false); checkRotationDS(r1, -r1.getQ0().getReal(), -r1.getQ1().getReal(), -r1.getQ2().getReal(), -r1.getQ3().getReal()); } @Test public void testCompose() throws MathIllegalArgumentException { FieldRotation r1 = new FieldRotation(createVector(2, -3, 5), createAngle(1.7)); FieldRotation r2 = new FieldRotation(createVector(-1, 3, 2), createAngle(0.3)); FieldRotation r3 = r2.applyTo(r1); FieldRotation r3Double = r2.applyTo(new Rotation(r1.getQ0().getReal(), r1.getQ1().getReal(), r1.getQ2().getReal(), r1.getQ3().getReal(), false)); for (double x = -0.9; x < 0.9; x += 0.2) { for (double y = -0.9; y < 0.9; y += 0.2) { for (double z = -0.9; z < 0.9; z += 0.2) { FieldVector3D u = createVector(x, y, z); checkVector(r2.applyTo(r1.applyTo(u)), r3.applyTo(u)); checkVector(r2.applyTo(r1.applyTo(u)), r3Double.applyTo(u)); } } } } @Test public void testComposeInverse() throws MathIllegalArgumentException { FieldRotation r1 = new FieldRotation(createVector(2, -3, 5), createAngle(1.7)); FieldRotation r2 = new FieldRotation(createVector(-1, 3, 2), createAngle(0.3)); FieldRotation r3 = r2.applyInverseTo(r1); FieldRotation r3Double = r2.applyInverseTo(new Rotation(r1.getQ0().getReal(), r1.getQ1().getReal(), r1.getQ2().getReal(), r1.getQ3().getReal(), false)); for (double x = -0.9; x < 0.9; x += 0.2) { for (double y = -0.9; y < 0.9; y += 0.2) { for (double z = -0.9; z < 0.9; z += 0.2) { FieldVector3D u = createVector(x, y, z); checkVector(r2.applyInverseTo(r1.applyTo(u)), r3.applyTo(u)); checkVector(r2.applyInverseTo(r1.applyTo(u)), r3Double.applyTo(u)); } } } } @Test public void testDoubleVectors() throws MathIllegalArgumentException { Well1024a random = new Well1024a(0x180b41cfeeffaf67l); UnitSphereRandomVectorGenerator g = new UnitSphereRandomVectorGenerator(3, random); for (int i = 0; i < 10; ++i) { double[] unit = g.nextVector(); FieldRotation r = new FieldRotation(createVector(unit[0], unit[1], unit[2]), createAngle(random.nextDouble())); for (double x = -0.9; x < 0.9; x += 0.4) { for (double y = -0.9; y < 0.9; y += 0.4) { for (double z = -0.9; z < 0.9; z += 0.4) { FieldVector3D uds = createVector(x, y, z); FieldVector3D ruds = r.applyTo(uds); FieldVector3D rIuds = r.applyInverseTo(uds); Vector3D u = new Vector3D(x, y, z); FieldVector3D ru = r.applyTo(u); FieldVector3D rIu = r.applyInverseTo(u); Dfp[] ruArray = new Dfp[3]; r.applyTo(new double[] { x, y, z}, ruArray); Dfp[] rIuArray = new Dfp[3]; r.applyInverseTo(new double[] { x, y, z}, rIuArray); checkVector(ruds, ru); checkVector(ruds, new FieldVector3D(ruArray)); checkVector(rIuds, rIu); checkVector(rIuds, new FieldVector3D(rIuArray)); } } } } } @Test public void testDoubleRotations() throws MathIllegalArgumentException { DfpField field = new DfpField(20); Well1024a random = new Well1024a(0x180b41cfeeffaf67l); UnitSphereRandomVectorGenerator g = new UnitSphereRandomVectorGenerator(3, random); for (int i = 0; i < 10; ++i) { double[] unit1 = g.nextVector(); Rotation r1 = new Rotation(new Vector3D(unit1[0], unit1[1], unit1[2]), random.nextDouble()); FieldRotation r1Prime = new FieldRotation(field.newDfp(r1.getQ0()), field.newDfp(r1.getQ1()), field.newDfp(r1.getQ2()), field.newDfp(r1.getQ3()), false); double[] unit2 = g.nextVector(); FieldRotation r2 = new FieldRotation(createVector(unit2[0], unit2[1], unit2[2]), createAngle(random.nextDouble())); FieldRotation rA = FieldRotation.applyTo(r1, r2); FieldRotation rB = r1Prime.applyTo(r2); FieldRotation rC = FieldRotation.applyInverseTo(r1, r2); FieldRotation rD = r1Prime.applyInverseTo(r2); for (double x = -0.9; x < 0.9; x += 0.4) { for (double y = -0.9; y < 0.9; y += 0.4) { for (double z = -0.9; z < 0.9; z += 0.4) { FieldVector3D uds = createVector(x, y, z); checkVector(r1Prime.applyTo(uds), FieldRotation.applyTo(r1, uds)); checkVector(r1Prime.applyInverseTo(uds), FieldRotation.applyInverseTo(r1, uds)); checkVector(rA.applyTo(uds), rB.applyTo(uds)); checkVector(rA.applyInverseTo(uds), rB.applyInverseTo(uds)); checkVector(rC.applyTo(uds), rD.applyTo(uds)); checkVector(rC.applyInverseTo(uds), rD.applyInverseTo(uds)); } } } } } @Test public void testArray() throws MathIllegalArgumentException { FieldRotation r = new FieldRotation(createAxis(2, -3, 5), createAngle(1.7)); for (double x = -0.9; x < 0.9; x += 0.2) { for (double y = -0.9; y < 0.9; y += 0.2) { for (double z = -0.9; z < 0.9; z += 0.2) { FieldVector3D u = createVector(x, y, z); FieldVector3D v = r.applyTo(u); Dfp[] out = new Dfp[3]; r.applyTo(new Dfp[] { u.getX(), u.getY(), u.getZ() }, out); Assert.assertEquals(v.getX().getReal(), out[0].getReal(), 1.0e-10); Assert.assertEquals(v.getY().getReal(), out[1].getReal(), 1.0e-10); Assert.assertEquals(v.getZ().getReal(), out[2].getReal(), 1.0e-10); r.applyInverseTo(out, out); Assert.assertEquals(u.getX().getReal(), out[0].getReal(), 1.0e-10); Assert.assertEquals(u.getY().getReal(), out[1].getReal(), 1.0e-10); Assert.assertEquals(u.getZ().getReal(), out[2].getReal(), 1.0e-10); } } } } @Test public void testApplyInverseTo() throws MathIllegalArgumentException { Dfp[] in = new Dfp[3]; Dfp[] out = new Dfp[3]; Dfp[] rebuilt = new Dfp[3]; FieldRotation r = new FieldRotation(createVector(2, -3, 5), createAngle(1.7)); for (double lambda = 0; lambda < 6.2; lambda += 0.2) { for (double phi = -1.55; phi < 1.55; phi += 0.2) { FieldVector3D u = createVector(FastMath.cos(lambda) * FastMath.cos(phi), FastMath.sin(lambda) * FastMath.cos(phi), FastMath.sin(phi)); r.applyInverseTo(r.applyTo(u)); checkVector(u, r.applyInverseTo(r.applyTo(u))); checkVector(u, r.applyTo(r.applyInverseTo(u))); in[0] = u.getX(); in[1] = u.getY(); in[2] = u.getZ(); r.applyTo(in, out); r.applyInverseTo(out, rebuilt); Assert.assertEquals(in[0].getReal(), rebuilt[0].getReal(), 1.0e-12); Assert.assertEquals(in[1].getReal(), rebuilt[1].getReal(), 1.0e-12); Assert.assertEquals(in[2].getReal(), rebuilt[2].getReal(), 1.0e-12); } } r = createRotation(1, 0, 0, 0, false); for (double lambda = 0; lambda < 6.2; lambda += 0.2) { for (double phi = -1.55; phi < 1.55; phi += 0.2) { FieldVector3D u = createVector(FastMath.cos(lambda) * FastMath.cos(phi), FastMath.sin(lambda) * FastMath.cos(phi), FastMath.sin(phi)); checkVector(u, r.applyInverseTo(r.applyTo(u))); checkVector(u, r.applyTo(r.applyInverseTo(u))); } } r = new FieldRotation(createVector(0, 0, 1), createAngle(FastMath.PI)); for (double lambda = 0; lambda < 6.2; lambda += 0.2) { for (double phi = -1.55; phi < 1.55; phi += 0.2) { FieldVector3D u = createVector(FastMath.cos(lambda) * FastMath.cos(phi), FastMath.sin(lambda) * FastMath.cos(phi), FastMath.sin(phi)); checkVector(u, r.applyInverseTo(r.applyTo(u))); checkVector(u, r.applyTo(r.applyInverseTo(u))); } } } @Test public void testIssue639() throws MathArithmeticException{ FieldVector3D u1 = createVector(-1321008684645961.0 / 268435456.0, -5774608829631843.0 / 268435456.0, -3822921525525679.0 / 4294967296.0); FieldVector3D u2 =createVector( -5712344449280879.0 / 2097152.0, -2275058564560979.0 / 1048576.0, 4423475992255071.0 / 65536.0); FieldRotation rot = new FieldRotation(u1, u2, createVector(1, 0, 0),createVector(0, 0, 1)); Assert.assertEquals( 0.6228370359608200639829222, rot.getQ0().getReal(), 1.0e-15); Assert.assertEquals( 0.0257707621456498790029987, rot.getQ1().getReal(), 1.0e-15); Assert.assertEquals(-0.0000000002503012255839931, rot.getQ2().getReal(), 1.0e-15); Assert.assertEquals(-0.7819270390861109450724902, rot.getQ3().getReal(), 1.0e-15); } @Test public void testIssue801() throws MathArithmeticException { FieldVector3D u1 = createVector(0.9999988431610581, -0.0015210774290851095, 0.0); FieldVector3D u2 = createVector(0.0, 0.0, 1.0); FieldVector3D v1 = createVector(0.9999999999999999, 0.0, 0.0); FieldVector3D v2 = createVector(0.0, 0.0, -1.0); FieldRotation quat = new FieldRotation(u1, u2, v1, v2); double q2 = quat.getQ0().getReal() * quat.getQ0().getReal() + quat.getQ1().getReal() * quat.getQ1().getReal() + quat.getQ2().getReal() * quat.getQ2().getReal() + quat.getQ3().getReal() * quat.getQ3().getReal(); Assert.assertEquals(1.0, q2, 1.0e-14); Assert.assertEquals(0.0, FieldVector3D.angle(v1, quat.applyTo(u1)).getReal(), 1.0e-14); Assert.assertEquals(0.0, FieldVector3D.angle(v2, quat.applyTo(u2)).getReal(), 1.0e-14); } private void checkAngle(Dfp a1, double a2) { Assert.assertEquals(a1.getReal(), MathUtils.normalizeAngle(a2, a1.getReal()), 1.0e-10); } private void checkRotationDS(FieldRotation r, double q0, double q1, double q2, double q3) { FieldRotation rPrime = createRotation(q0, q1, q2, q3, false); Assert.assertEquals(0, FieldRotation.distance(r, rPrime).getReal(), 1.0e-12); } private FieldRotation createRotation(double q0, double q1, double q2, double q3, boolean needsNormalization) { DfpField field = new DfpField(20); return new FieldRotation(field.newDfp(q0), field.newDfp(q1), field.newDfp(q2), field.newDfp(q3), needsNormalization); } private FieldRotation createRotation(double[][] m, double threshold) { DfpField field = new DfpField(20); Dfp[][] mds = new Dfp[m.length][m[0].length]; for (int i = 0; i < m.length; ++i) { for (int j = 0; j < m[i].length; ++j) { mds[i][j] = field.newDfp(m[i][j]); } } return new FieldRotation(mds, threshold); } private FieldVector3D createVector(double x, double y, double z) { DfpField field = new DfpField(20); return new FieldVector3D(field.newDfp(x), field.newDfp(y), field.newDfp(z)); } private FieldVector3D createAxis(double x, double y, double z) { DfpField field = new DfpField(20); return new FieldVector3D(field.newDfp(x), field.newDfp(y), field.newDfp(z)); } private Dfp createAngle(double alpha) { return new DfpField(20).newDfp(alpha); } private void checkVector(FieldVector3D u, FieldVector3D v) { Assert.assertEquals(u.getX().getReal(), v.getX().getReal(), 1.0e-12); Assert.assertEquals(u.getY().getReal(), v.getY().getReal(), 1.0e-12); Assert.assertEquals(u.getZ().getReal(), v.getZ().getReal(), 1.0e-12); } } ././@LongLink100644 0 0 156 12126630646 10263 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/geometry/euclidean/threed/FieldVector3DTest.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/geometry/euclidean/threed/FieldVector3D100644 1750 1750 124627 12126627671 32214 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.geometry.euclidean.threed; import java.text.DecimalFormat; import java.text.DecimalFormatSymbols; import java.text.NumberFormat; import java.util.Locale; import org.apache.commons.math3.analysis.differentiation.DerivativeStructure; import org.apache.commons.math3.exception.DimensionMismatchException; import org.apache.commons.math3.exception.MathArithmeticException; import org.apache.commons.math3.random.Well1024a; import org.apache.commons.math3.util.FastMath; import org.apache.commons.math3.util.Precision; import org.junit.Assert; import org.junit.Test; public class FieldVector3DTest { @Test public void testConstructors() throws DimensionMismatchException { double cosAlpha = 1 / 2.0; double sinAlpha = FastMath.sqrt(3) / 2.0; double cosDelta = FastMath.sqrt(2) / 2.0; double sinDelta = -FastMath.sqrt(2) / 2.0; FieldVector3D u = new FieldVector3D(2, new FieldVector3D(new DerivativeStructure(2, 1, 0, FastMath.PI / 3), new DerivativeStructure(2, 1, 1, -FastMath.PI / 4))); checkVector(u, 2 * cosAlpha * cosDelta, 2 * sinAlpha * cosDelta, 2 * sinDelta); Assert.assertEquals(-2 * sinAlpha * cosDelta, u.getX().getPartialDerivative(1, 0), 1.0e-12); Assert.assertEquals(+2 * cosAlpha * cosDelta, u.getY().getPartialDerivative(1, 0), 1.0e-12); Assert.assertEquals(0, u.getZ().getPartialDerivative(1, 0), 1.0e-12); Assert.assertEquals(-2 * cosAlpha * sinDelta, u.getX().getPartialDerivative(0, 1), 1.0e-12); Assert.assertEquals(-2 * sinAlpha * sinDelta, u.getY().getPartialDerivative(0, 1), 1.0e-12); Assert.assertEquals(2 * cosDelta, u.getZ().getPartialDerivative(0, 1), 1.0e-12); checkVector(new FieldVector3D(2, createVector(1, 0, 0, 3)), 2, 0, 0, 2, 0, 0, 0, 2, 0, 0, 0, 2); checkVector(new FieldVector3D(new DerivativeStructure(4, 1, 3, 2.0), createVector(1, 0, 0, 4)), 2, 0, 0, 2, 0, 0, 1, 0, 2, 0, 0, 0, 0, 2, 0); checkVector(new FieldVector3D(new DerivativeStructure(4, 1, 3, 2.0), new Vector3D(1, 0, 0)), 2, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0); checkVector(new FieldVector3D(2, createVector(1, 0, 0, 3), -3, createVector(0, 0, -1, 3)), 2, 0, 3, -1, 0, 0, 0, -1, 0, 0, 0, -1); checkVector(new FieldVector3D(new DerivativeStructure(4, 1, 3, 2.0), createVector(1, 0, 0, 4), new DerivativeStructure(4, 1, 3, -3.0), createVector(0, 0, -1, 4)), 2, 0, 3, -1, 0, 0, 1, 0, -1, 0, 0, 0, 0, -1, -1); checkVector(new FieldVector3D(new DerivativeStructure(4, 1, 3, 2.0), new Vector3D(1, 0, 0), new DerivativeStructure(4, 1, 3, -3.0), new Vector3D(0, 0, -1)), 2, 0, 3, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, -1); checkVector(new FieldVector3D(2, createVector(1, 0, 0, 3), 5, createVector(0, 1, 0, 3), -3, createVector(0, 0, -1, 3)), 2, 5, 3, 4, 0, 0, 0, 4, 0, 0, 0, 4); checkVector(new FieldVector3D(new DerivativeStructure(4, 1, 3, 2.0), createVector(1, 0, 0, 4), new DerivativeStructure(4, 1, 3, 5.0), createVector(0, 1, 0, 4), new DerivativeStructure(4, 1, 3, -3.0), createVector(0, 0, -1, 4)), 2, 5, 3, 4, 0, 0, 1, 0, 4, 0, 1, 0, 0, 4, -1); checkVector(new FieldVector3D(new DerivativeStructure(4, 1, 3, 2.0), new Vector3D(1, 0, 0), new DerivativeStructure(4, 1, 3, 5.0), new Vector3D(0, 1, 0), new DerivativeStructure(4, 1, 3, -3.0), new Vector3D(0, 0, -1)), 2, 5, 3, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, -1); checkVector(new FieldVector3D(2, createVector(1, 0, 0, 3), 5, createVector(0, 1, 0, 3), 5, createVector(0, -1, 0, 3), -3, createVector(0, 0, -1, 3)), 2, 0, 3, 9, 0, 0, 0, 9, 0, 0, 0, 9); checkVector(new FieldVector3D(new DerivativeStructure(4, 1, 3, 2.0), createVector(1, 0, 0, 4), new DerivativeStructure(4, 1, 3, 5.0), createVector(0, 1, 0, 4), new DerivativeStructure(4, 1, 3, 5.0), createVector(0, -1, 0, 4), new DerivativeStructure(4, 1, 3, -3.0), createVector(0, 0, -1, 4)), 2, 0, 3, 9, 0, 0, 1, 0, 9, 0, 0, 0, 0, 9, -1); checkVector(new FieldVector3D(new DerivativeStructure(4, 1, 3, 2.0), new Vector3D(1, 0, 0), new DerivativeStructure(4, 1, 3, 5.0), new Vector3D(0, 1, 0), new DerivativeStructure(4, 1, 3, 5.0), new Vector3D(0, -1, 0), new DerivativeStructure(4, 1, 3, -3.0), new Vector3D(0, 0, -1)), 2, 0, 3, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, -1); checkVector(new FieldVector3D(new DerivativeStructure[] { new DerivativeStructure(3, 1, 2, 2), new DerivativeStructure(3, 1, 1, 5), new DerivativeStructure(3, 1, 0, -3) }), 2, 5, -3, 0, 0, 1, 0, 1, 0, 1, 0, 0); } @Test public void testEquals() { FieldVector3D u1 = createVector(1, 2, 3, 3); FieldVector3D v = createVector(1, 2, 3 + 10 * Precision.EPSILON, 3); Assert.assertTrue(u1.equals(u1)); Assert.assertTrue(u1.equals(new FieldVector3D(new DerivativeStructure(3, 1, 0, 1.0), new DerivativeStructure(3, 1, 1, 2.0), new DerivativeStructure(3, 1, 2, 3.0)))); Assert.assertFalse(u1.equals(new FieldVector3D(new DerivativeStructure(3, 1, 1.0), new DerivativeStructure(3, 1, 1, 2.0), new DerivativeStructure(3, 1, 2, 3.0)))); Assert.assertFalse(u1.equals(new FieldVector3D(new DerivativeStructure(3, 1, 0, 1.0), new DerivativeStructure(3, 1, 2.0), new DerivativeStructure(3, 1, 2, 3.0)))); Assert.assertFalse(u1.equals(new FieldVector3D(new DerivativeStructure(3, 1, 0, 1.0), new DerivativeStructure(3, 1, 1, 2.0), new DerivativeStructure(3, 1, 3.0)))); Assert.assertFalse(u1.equals(v)); Assert.assertFalse(u1.equals(u1.toVector3D())); Assert.assertTrue(createVector(0, Double.NaN, 0, 3).equals(createVector(0, 0, Double.NaN, 3))); } @Test public void testHash() { Assert.assertEquals(createVector(0, Double.NaN, 0, 3).hashCode(), createVector(0, 0, Double.NaN, 3).hashCode()); FieldVector3D u = createVector(1, 2, 3, 3); FieldVector3D v = createVector(1, 2, 3 + 10 * Precision.EPSILON, 3); Assert.assertTrue(u.hashCode() != v.hashCode()); } @Test public void testInfinite() { Assert.assertTrue(createVector(1, 1, Double.NEGATIVE_INFINITY, 3).isInfinite()); Assert.assertTrue(createVector(1, Double.NEGATIVE_INFINITY, 1, 3).isInfinite()); Assert.assertTrue(createVector(Double.NEGATIVE_INFINITY, 1, 1, 3).isInfinite()); Assert.assertFalse(createVector(1, 1, 2, 3).isInfinite()); Assert.assertFalse(createVector(1, Double.NaN, Double.NEGATIVE_INFINITY, 3).isInfinite()); } @Test public void testNaN() { Assert.assertTrue(createVector(1, 1, Double.NaN, 3).isNaN()); Assert.assertTrue(createVector(1, Double.NaN, 1, 3).isNaN()); Assert.assertTrue(createVector(Double.NaN, 1, 1, 3).isNaN()); Assert.assertFalse(createVector(1, 1, 2, 3).isNaN()); Assert.assertFalse(createVector(1, 1, Double.NEGATIVE_INFINITY, 3).isNaN()); } @Test public void testToString() { Assert.assertEquals("{3; 2; 1}", createVector(3, 2, 1, 3).toString()); NumberFormat format = new DecimalFormat("0.000", new DecimalFormatSymbols(Locale.US)); Assert.assertEquals("{3.000; 2.000; 1.000}", createVector(3, 2, 1, 3).toString(format)); } @Test(expected=DimensionMismatchException.class) public void testWrongDimension() throws DimensionMismatchException { new FieldVector3D(new DerivativeStructure[] { new DerivativeStructure(3, 1, 0, 2), new DerivativeStructure(3, 1, 0, 5) }); } @Test public void testCoordinates() { FieldVector3D v = createVector(1, 2, 3, 3); Assert.assertTrue(FastMath.abs(v.getX().getReal() - 1) < 1.0e-12); Assert.assertTrue(FastMath.abs(v.getY().getReal() - 2) < 1.0e-12); Assert.assertTrue(FastMath.abs(v.getZ().getReal() - 3) < 1.0e-12); DerivativeStructure[] coordinates = v.toArray(); Assert.assertTrue(FastMath.abs(coordinates[0].getReal() - 1) < 1.0e-12); Assert.assertTrue(FastMath.abs(coordinates[1].getReal() - 2) < 1.0e-12); Assert.assertTrue(FastMath.abs(coordinates[2].getReal() - 3) < 1.0e-12); } @Test public void testNorm1() { Assert.assertEquals( 0.0, createVector(0, 0, 0, 3).getNorm1().getReal(), 0); Assert.assertEquals( 6.0, createVector(1, -2, 3, 3).getNorm1().getReal(), 0); Assert.assertEquals( 1.0, createVector(1, -2, 3, 3).getNorm1().getPartialDerivative(1, 0, 0), 0); Assert.assertEquals(-1.0, createVector(1, -2, 3, 3).getNorm1().getPartialDerivative(0, 1, 0), 0); Assert.assertEquals( 1.0, createVector(1, -2, 3, 3).getNorm1().getPartialDerivative(0, 0, 1), 0); } @Test public void testNorm() { double r = FastMath.sqrt(14); Assert.assertEquals(0.0, createVector(0, 0, 0, 3).getNorm().getReal(), 0); Assert.assertEquals(r, createVector(1, 2, 3, 3).getNorm().getReal(), 1.0e-12); Assert.assertEquals( 1.0 / r, createVector(1, 2, 3, 3).getNorm().getPartialDerivative(1, 0, 0), 0); Assert.assertEquals( 2.0 / r, createVector(1, 2, 3, 3).getNorm().getPartialDerivative(0, 1, 0), 0); Assert.assertEquals( 3.0 / r, createVector(1, 2, 3, 3).getNorm().getPartialDerivative(0, 0, 1), 0); } @Test public void testNormSq() { Assert.assertEquals(0.0, createVector(0, 0, 0, 3).getNormSq().getReal(), 0); Assert.assertEquals(14, createVector(1, 2, 3, 3).getNormSq().getReal(), 1.0e-12); Assert.assertEquals( 2, createVector(1, 2, 3, 3).getNormSq().getPartialDerivative(1, 0, 0), 0); Assert.assertEquals( 4, createVector(1, 2, 3, 3).getNormSq().getPartialDerivative(0, 1, 0), 0); Assert.assertEquals( 6, createVector(1, 2, 3, 3).getNormSq().getPartialDerivative(0, 0, 1), 0); } @Test public void testNormInf() { Assert.assertEquals( 0.0, createVector(0, 0, 0, 3).getNormInf().getReal(), 0); Assert.assertEquals( 3.0, createVector(1, -2, 3, 3).getNormInf().getReal(), 0); Assert.assertEquals( 0.0, createVector(1, -2, 3, 3).getNormInf().getPartialDerivative(1, 0, 0), 0); Assert.assertEquals( 0.0, createVector(1, -2, 3, 3).getNormInf().getPartialDerivative(0, 1, 0), 0); Assert.assertEquals( 1.0, createVector(1, -2, 3, 3).getNormInf().getPartialDerivative(0, 0, 1), 0); Assert.assertEquals( 3.0, createVector(2, -1, 3, 3).getNormInf().getReal(), 0); Assert.assertEquals( 0.0, createVector(2, -1, 3, 3).getNormInf().getPartialDerivative(1, 0, 0), 0); Assert.assertEquals( 0.0, createVector(2, -1, 3, 3).getNormInf().getPartialDerivative(0, 1, 0), 0); Assert.assertEquals( 1.0, createVector(2, -1, 3, 3).getNormInf().getPartialDerivative(0, 0, 1), 0); Assert.assertEquals( 3.0, createVector(1, -3, 2, 3).getNormInf().getReal(), 0); Assert.assertEquals( 0.0, createVector(1, -3, 2, 3).getNormInf().getPartialDerivative(1, 0, 0), 0); Assert.assertEquals(-1.0, createVector(1, -3, 2, 3).getNormInf().getPartialDerivative(0, 1, 0), 0); Assert.assertEquals( 0.0, createVector(1, -3, 2, 3).getNormInf().getPartialDerivative(0, 0, 1), 0); Assert.assertEquals( 3.0, createVector(2, -3, 1, 3).getNormInf().getReal(), 0); Assert.assertEquals( 0.0, createVector(2, -3, 1, 3).getNormInf().getPartialDerivative(1, 0, 0), 0); Assert.assertEquals(-1.0, createVector(2, -3, 1, 3).getNormInf().getPartialDerivative(0, 1, 0), 0); Assert.assertEquals( 0.0, createVector(2, -3, 1, 3).getNormInf().getPartialDerivative(0, 0, 1), 0); Assert.assertEquals( 3.0, createVector(3, -1, 2, 3).getNormInf().getReal(), 0); Assert.assertEquals( 1.0, createVector(3, -1, 2, 3).getNormInf().getPartialDerivative(1, 0, 0), 0); Assert.assertEquals( 0.0, createVector(3, -1, 2, 3).getNormInf().getPartialDerivative(0, 1, 0), 0); Assert.assertEquals( 0.0, createVector(3, -1, 2, 3).getNormInf().getPartialDerivative(0, 0, 1), 0); Assert.assertEquals( 3.0, createVector(3, -2, 1, 3).getNormInf().getReal(), 0); Assert.assertEquals( 1.0, createVector(3, -2, 1, 3).getNormInf().getPartialDerivative(1, 0, 0), 0); Assert.assertEquals( 0.0, createVector(3, -2, 1, 3).getNormInf().getPartialDerivative(0, 1, 0), 0); Assert.assertEquals( 0.0, createVector(3, -2, 1, 3).getNormInf().getPartialDerivative(0, 0, 1), 0); } @Test public void testDistance1() { FieldVector3D v1 = createVector(1, -2, 3, 3); FieldVector3D v2 = createVector(-4, 2, 0, 3); Assert.assertEquals(0.0, FieldVector3D.distance1(createVector(-1, 0, 0, 3), createVector(-1, 0, 0, 3)).getReal(), 0); DerivativeStructure distance = FieldVector3D.distance1(v1, v2); Assert.assertEquals(12.0, distance.getReal(), 1.0e-12); Assert.assertEquals(0, distance.getPartialDerivative(1, 0, 0), 1.0e-12); Assert.assertEquals(0, distance.getPartialDerivative(0, 1, 0), 1.0e-12); Assert.assertEquals(0, distance.getPartialDerivative(0, 0, 1), 1.0e-12); distance = FieldVector3D.distance1(v1, new Vector3D(-4, 2, 0)); Assert.assertEquals(12.0, distance.getReal(), 1.0e-12); Assert.assertEquals( 1, distance.getPartialDerivative(1, 0, 0), 1.0e-12); Assert.assertEquals(-1, distance.getPartialDerivative(0, 1, 0), 1.0e-12); Assert.assertEquals( 1, distance.getPartialDerivative(0, 0, 1), 1.0e-12); distance = FieldVector3D.distance1(new Vector3D(-4, 2, 0), v1); Assert.assertEquals(12.0, distance.getReal(), 1.0e-12); Assert.assertEquals( 1, distance.getPartialDerivative(1, 0, 0), 1.0e-12); Assert.assertEquals(-1, distance.getPartialDerivative(0, 1, 0), 1.0e-12); Assert.assertEquals( 1, distance.getPartialDerivative(0, 0, 1), 1.0e-12); } @Test public void testDistance() { FieldVector3D v1 = createVector(1, -2, 3, 3); FieldVector3D v2 = createVector(-4, 2, 0, 3); Assert.assertEquals(0.0, FieldVector3D.distance(createVector(-1, 0, 0, 3), createVector(-1, 0, 0, 3)).getReal(), 0); DerivativeStructure distance = FieldVector3D.distance(v1, v2); Assert.assertEquals(FastMath.sqrt(50), distance.getReal(), 1.0e-12); Assert.assertEquals(0, distance.getPartialDerivative(1, 0, 0), 1.0e-12); Assert.assertEquals(0, distance.getPartialDerivative(0, 1, 0), 1.0e-12); Assert.assertEquals(0, distance.getPartialDerivative(0, 0, 1), 1.0e-12); distance = FieldVector3D.distance(v1, new Vector3D(-4, 2, 0)); Assert.assertEquals(FastMath.sqrt(50), distance.getReal(), 1.0e-12); Assert.assertEquals( 5 / FastMath.sqrt(50), distance.getPartialDerivative(1, 0, 0), 1.0e-12); Assert.assertEquals(-4 / FastMath.sqrt(50), distance.getPartialDerivative(0, 1, 0), 1.0e-12); Assert.assertEquals( 3 / FastMath.sqrt(50), distance.getPartialDerivative(0, 0, 1), 1.0e-12); distance = FieldVector3D.distance(new Vector3D(-4, 2, 0), v1); Assert.assertEquals(FastMath.sqrt(50), distance.getReal(), 1.0e-12); Assert.assertEquals( 5 / FastMath.sqrt(50), distance.getPartialDerivative(1, 0, 0), 1.0e-12); Assert.assertEquals(-4 / FastMath.sqrt(50), distance.getPartialDerivative(0, 1, 0), 1.0e-12); Assert.assertEquals( 3 / FastMath.sqrt(50), distance.getPartialDerivative(0, 0, 1), 1.0e-12); } @Test public void testDistanceSq() { FieldVector3D v1 = createVector(1, -2, 3, 3); FieldVector3D v2 = createVector(-4, 2, 0, 3); Assert.assertEquals(0.0, FieldVector3D.distanceSq(createVector(-1, 0, 0, 3), createVector(-1, 0, 0, 3)).getReal(), 0); DerivativeStructure distanceSq = FieldVector3D.distanceSq(v1, v2); Assert.assertEquals(50.0, distanceSq.getReal(), 1.0e-12); Assert.assertEquals(0, distanceSq.getPartialDerivative(1, 0, 0), 1.0e-12); Assert.assertEquals(0, distanceSq.getPartialDerivative(0, 1, 0), 1.0e-12); Assert.assertEquals(0, distanceSq.getPartialDerivative(0, 0, 1), 1.0e-12); distanceSq = FieldVector3D.distanceSq(v1, new Vector3D(-4, 2, 0)); Assert.assertEquals(50.0, distanceSq.getReal(), 1.0e-12); Assert.assertEquals(10, distanceSq.getPartialDerivative(1, 0, 0), 1.0e-12); Assert.assertEquals(-8, distanceSq.getPartialDerivative(0, 1, 0), 1.0e-12); Assert.assertEquals( 6, distanceSq.getPartialDerivative(0, 0, 1), 1.0e-12); distanceSq = FieldVector3D.distanceSq(new Vector3D(-4, 2, 0), v1); Assert.assertEquals(50.0, distanceSq.getReal(), 1.0e-12); Assert.assertEquals(10, distanceSq.getPartialDerivative(1, 0, 0), 1.0e-12); Assert.assertEquals(-8, distanceSq.getPartialDerivative(0, 1, 0), 1.0e-12); Assert.assertEquals( 6, distanceSq.getPartialDerivative(0, 0, 1), 1.0e-12); } @Test public void testDistanceInf() { FieldVector3D v1 = createVector(1, -2, 3, 3); FieldVector3D v2 = createVector(-4, 2, 0, 3); Assert.assertEquals(0.0, FieldVector3D.distanceInf(createVector(-1, 0, 0, 3), createVector(-1, 0, 0, 3)).getReal(), 0); DerivativeStructure distance = FieldVector3D.distanceInf(v1, v2); Assert.assertEquals(5.0, distance.getReal(), 1.0e-12); Assert.assertEquals(0, distance.getPartialDerivative(1, 0, 0), 1.0e-12); Assert.assertEquals(0, distance.getPartialDerivative(0, 1, 0), 1.0e-12); Assert.assertEquals(0, distance.getPartialDerivative(0, 0, 1), 1.0e-12); distance = FieldVector3D.distanceInf(v1, new Vector3D(-4, 2, 0)); Assert.assertEquals(5.0, distance.getReal(), 1.0e-12); Assert.assertEquals(1, distance.getPartialDerivative(1, 0, 0), 1.0e-12); Assert.assertEquals(0, distance.getPartialDerivative(0, 1, 0), 1.0e-12); Assert.assertEquals(0, distance.getPartialDerivative(0, 0, 1), 1.0e-12); distance = FieldVector3D.distanceInf(new Vector3D(-4, 2, 0), v1); Assert.assertEquals(5.0, distance.getReal(), 1.0e-12); Assert.assertEquals(1, distance.getPartialDerivative(1, 0, 0), 1.0e-12); Assert.assertEquals(0, distance.getPartialDerivative(0, 1, 0), 1.0e-12); Assert.assertEquals(0, distance.getPartialDerivative(0, 0, 1), 1.0e-12); Assert.assertEquals(v1.subtract(v2).getNormInf().getReal(), FieldVector3D.distanceInf(v1, v2).getReal(), 1.0e-12); Assert.assertEquals(5.0, FieldVector3D.distanceInf(createVector( 1, -2, 3, 3), createVector(-4, 2, 0, 3)).getReal(), 1.0e-12); Assert.assertEquals(5.0, FieldVector3D.distanceInf(createVector( 1, 3, -2, 3), createVector(-4, 0, 2, 3)).getReal(), 1.0e-12); Assert.assertEquals(5.0, FieldVector3D.distanceInf(createVector(-2, 1, 3, 3), createVector( 2, -4, 0, 3)).getReal(), 1.0e-12); Assert.assertEquals(5.0, FieldVector3D.distanceInf(createVector(-2, 3, 1, 3), createVector( 2, 0, -4, 3)).getReal(), 1.0e-12); Assert.assertEquals(5.0, FieldVector3D.distanceInf(createVector(3, -2, 1, 3), createVector(0, 2, -4, 3)).getReal(), 1.0e-12); Assert.assertEquals(5.0, FieldVector3D.distanceInf(createVector(3, 1, -2, 3), createVector(0, -4, 2, 3)).getReal(), 1.0e-12); Assert.assertEquals(5.0, FieldVector3D.distanceInf(createVector( 1, -2, 3, 3), new Vector3D(-4, 2, 0)).getReal(), 1.0e-12); Assert.assertEquals(5.0, FieldVector3D.distanceInf(createVector( 1, 3, -2, 3), new Vector3D(-4, 0, 2)).getReal(), 1.0e-12); Assert.assertEquals(5.0, FieldVector3D.distanceInf(createVector(-2, 1, 3, 3), new Vector3D( 2, -4, 0)).getReal(), 1.0e-12); Assert.assertEquals(5.0, FieldVector3D.distanceInf(createVector(-2, 3, 1, 3), new Vector3D( 2, 0, -4)).getReal(), 1.0e-12); Assert.assertEquals(5.0, FieldVector3D.distanceInf(createVector(3, -2, 1, 3), new Vector3D(0, 2, -4)).getReal(), 1.0e-12); Assert.assertEquals(5.0, FieldVector3D.distanceInf(createVector(3, 1, -2, 3), new Vector3D(0, -4, 2)).getReal(), 1.0e-12); } @Test public void testSubtract() { FieldVector3D v1 = createVector(1, 2, 3, 3); FieldVector3D v2 = createVector(-3, -2, -1, 3); v1 = v1.subtract(v2); checkVector(v1, 4, 4, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0); checkVector(v2.subtract(v1), -7, -6, -5, 1, 0, 0, 0, 1, 0, 0, 0, 1); checkVector(v2.subtract(new Vector3D(4, 4, 4)), -7, -6, -5, 1, 0, 0, 0, 1, 0, 0, 0, 1); checkVector(v2.subtract(3, v1), -15, -14, -13, 1, 0, 0, 0, 1, 0, 0, 0, 1); checkVector(v2.subtract(3, new Vector3D(4, 4, 4)), -15, -14, -13, 1, 0, 0, 0, 1, 0, 0, 0, 1); checkVector(v2.subtract(new DerivativeStructure(3, 1, 2, 3), new Vector3D(4, 4, 4)), -15, -14, -13, 1, 0, -4, 0, 1, -4, 0, 0, -3); checkVector(createVector(1, 2, 3, 4).subtract(new DerivativeStructure(4, 1, 3, 5.0), createVector(3, -2, 1, 4)), -14, 12, -2, -4, 0, 0, -3, 0, -4, 0, 2, 0, 0, -4, -1); } @Test public void testAdd() { FieldVector3D v1 = createVector(1, 2, 3, 3); FieldVector3D v2 = createVector(-3, -2, -1, 3); v1 = v1.add(v2); checkVector(v1, -2, 0, 2, 2, 0, 0, 0, 2, 0, 0, 0, 2); checkVector(v2.add(v1), -5, -2, 1, 3, 0, 0, 0, 3, 0, 0, 0, 3); checkVector(v2.add(new Vector3D(-2, 0, 2)), -5, -2, 1, 1, 0, 0, 0, 1, 0, 0, 0, 1); checkVector(v2.add(3, v1), -9, -2, 5, 7, 0, 0, 0, 7, 0, 0, 0, 7); checkVector(v2.add(3, new Vector3D(-2, 0, 2)), -9, -2, 5, 1, 0, 0, 0, 1, 0, 0, 0, 1); checkVector(v2.add(new DerivativeStructure(3, 1, 2, 3), new Vector3D(-2, 0, 2)), -9, -2, 5, 1, 0, -2, 0, 1, 0, 0, 0, 3); checkVector(createVector(1, 2, 3, 4).add(new DerivativeStructure(4, 1, 3, 5.0), createVector(3, -2, 1, 4)), 16, -8, 8, 6, 0, 0, 3, 0, 6, 0, -2, 0, 0, 6, 1); } @Test public void testScalarProduct() { FieldVector3D v = createVector(1, 2, 3, 3); v = v.scalarMultiply(3); checkVector(v, 3, 6, 9); checkVector(v.scalarMultiply(0.5), 1.5, 3, 4.5); } @Test public void testVectorialProducts() { FieldVector3D v1 = createVector(2, 1, -4, 3); FieldVector3D v2 = createVector(3, 1, -1, 3); Assert.assertTrue(FastMath.abs(FieldVector3D.dotProduct(v1, v2).getReal() - 11) < 1.0e-12); Assert.assertTrue(FastMath.abs(FieldVector3D.dotProduct(v1, v2.toVector3D()).getReal() - 11) < 1.0e-12); Assert.assertTrue(FastMath.abs(FieldVector3D.dotProduct(v1.toVector3D(), v2).getReal() - 11) < 1.0e-12); FieldVector3D v3 = FieldVector3D.crossProduct(v1, v2); checkVector(v3, 3, -10, -1); Assert.assertTrue(FastMath.abs(FieldVector3D.dotProduct(v1, v3).getReal()) < 1.0e-12); Assert.assertTrue(FastMath.abs(FieldVector3D.dotProduct(v2, v3).getReal()) < 1.0e-12); v3 = FieldVector3D.crossProduct(v1, v2.toVector3D()); checkVector(v3, 3, -10, -1); Assert.assertTrue(FastMath.abs(FieldVector3D.dotProduct(v1, v3).getReal()) < 1.0e-12); Assert.assertTrue(FastMath.abs(FieldVector3D.dotProduct(v2, v3).getReal()) < 1.0e-12); v3 = FieldVector3D.crossProduct(v1.toVector3D(), v2); checkVector(v3, 3, -10, -1); Assert.assertTrue(FastMath.abs(FieldVector3D.dotProduct(v1, v3).getReal()) < 1.0e-12); Assert.assertTrue(FastMath.abs(FieldVector3D.dotProduct(v2, v3).getReal()) < 1.0e-12); } @Test public void testCrossProductCancellation() { FieldVector3D v1 = createVector(9070467121.0, 4535233560.0, 1, 3); FieldVector3D v2 = createVector(9070467123.0, 4535233561.0, 1, 3); checkVector(FieldVector3D.crossProduct(v1, v2), -1, 2, 1); double scale = FastMath.scalb(1.0, 100); FieldVector3D big1 = new FieldVector3D(scale, v1); FieldVector3D small2 = new FieldVector3D(1 / scale, v2); checkVector(FieldVector3D.crossProduct(big1, small2), -1, 2, 1); } @Test public void testAngular() { Assert.assertEquals(0, createVector(1, 0, 0, 3).getAlpha().getReal(), 1.0e-10); Assert.assertEquals(0, createVector(1, 0, 0, 3).getDelta().getReal(), 1.0e-10); Assert.assertEquals(FastMath.PI / 2, createVector(0, 1, 0, 3).getAlpha().getReal(), 1.0e-10); Assert.assertEquals(0, createVector(0, 1, 0, 3).getDelta().getReal(), 1.0e-10); Assert.assertEquals(FastMath.PI / 2, createVector(0, 0, 1, 3).getDelta().getReal(), 1.0e-10); FieldVector3D u = createVector(-1, 1, -1, 3); Assert.assertEquals(3 * FastMath.PI /4, u.getAlpha().getReal(), 1.0e-10); Assert.assertEquals(-1.0 / FastMath.sqrt(3), u.getDelta().sin().getReal(), 1.0e-10); } @Test public void testAngularSeparation() throws MathArithmeticException { FieldVector3D v1 = createVector(2, -1, 4, 3); FieldVector3D k = v1.normalize(); FieldVector3D i = k.orthogonal(); FieldVector3D v2 = k.scalarMultiply(FastMath.cos(1.2)).add(i.scalarMultiply(FastMath.sin(1.2))); Assert.assertTrue(FastMath.abs(FieldVector3D.angle(v1, v2).getReal() - 1.2) < 1.0e-12); Assert.assertTrue(FastMath.abs(FieldVector3D.angle(v1, v2.toVector3D()).getReal() - 1.2) < 1.0e-12); Assert.assertTrue(FastMath.abs(FieldVector3D.angle(v1.toVector3D(), v2).getReal() - 1.2) < 1.0e-12); try { FieldVector3D.angle(v1, Vector3D.ZERO); Assert.fail("an exception should have been thrown"); } catch (MathArithmeticException mae) { // expected } Assert.assertEquals(0.0, FieldVector3D.angle(v1, v1.toVector3D()).getReal(), 1.0e-15); Assert.assertEquals(FastMath.PI, FieldVector3D.angle(v1, v1.negate().toVector3D()).getReal(), 1.0e-15); } @Test public void testNormalize() throws MathArithmeticException { Assert.assertEquals(1.0, createVector(5, -4, 2, 3).normalize().getNorm().getReal(), 1.0e-12); try { createVector(0, 0, 0, 3).normalize(); Assert.fail("an exception should have been thrown"); } catch (MathArithmeticException ae) { // expected behavior } } @Test public void testNegate() { checkVector(createVector(0.1, 2.5, 1.3, 3).negate(), -0.1, -2.5, -1.3, -1, 0, 0, 0, -1, 0, 0, 0, -1); } @Test public void testOrthogonal() throws MathArithmeticException { FieldVector3D v1 = createVector(0.1, 2.5, 1.3, 3); Assert.assertEquals(0.0, FieldVector3D.dotProduct(v1, v1.orthogonal()).getReal(), 1.0e-12); FieldVector3D v2 = createVector(2.3, -0.003, 7.6, 3); Assert.assertEquals(0.0, FieldVector3D.dotProduct(v2, v2.orthogonal()).getReal(), 1.0e-12); FieldVector3D v3 = createVector(-1.7, 1.4, 0.2, 3); Assert.assertEquals(0.0, FieldVector3D.dotProduct(v3, v3.orthogonal()).getReal(), 1.0e-12); FieldVector3D v4 = createVector(4.2, 0.1, -1.8, 3); Assert.assertEquals(0.0, FieldVector3D.dotProduct(v4, v4.orthogonal()).getReal(), 1.0e-12); try { createVector(0, 0, 0, 3).orthogonal(); Assert.fail("an exception should have been thrown"); } catch (MathArithmeticException ae) { // expected behavior } } @Test public void testAngle() throws MathArithmeticException { Assert.assertEquals(0.22572612855273393616, FieldVector3D.angle(createVector(1, 2, 3, 3), createVector(4, 5, 6, 3)).getReal(), 1.0e-12); Assert.assertEquals(7.98595620686106654517199e-8, FieldVector3D.angle(createVector(1, 2, 3, 3), createVector(2, 4, 6.000001, 3)).getReal(), 1.0e-12); Assert.assertEquals(3.14159257373023116985197793156, FieldVector3D.angle(createVector(1, 2, 3, 3), createVector(-2, -4, -6.000001, 3)).getReal(), 1.0e-12); try { FieldVector3D.angle(createVector(0, 0, 0, 3), createVector(1, 0, 0, 3)); Assert.fail("an exception should have been thrown"); } catch (MathArithmeticException ae) { // expected behavior } } @Test public void testAccurateDotProduct() { // the following two vectors are nearly but not exactly orthogonal // naive dot product (i.e. computing u1.x * u2.x + u1.y * u2.y + u1.z * u2.z // leads to a result of 0.0, instead of the correct -1.855129... FieldVector3D u1 = createVector(-1321008684645961.0 / 268435456.0, -5774608829631843.0 / 268435456.0, -7645843051051357.0 / 8589934592.0, 3); FieldVector3D u2 = createVector(-5712344449280879.0 / 2097152.0, -4550117129121957.0 / 2097152.0, 8846951984510141.0 / 131072.0, 3); DerivativeStructure sNaive = u1.getX().multiply(u2.getX()).add(u1.getY().multiply(u2.getY())).add(u1.getZ().multiply(u2.getZ())); DerivativeStructure sAccurate = FieldVector3D.dotProduct(u1, u2); Assert.assertEquals(0.0, sNaive.getReal(), 1.0e-30); Assert.assertEquals(-2088690039198397.0 / 1125899906842624.0, sAccurate.getReal(), 1.0e-16); } @Test public void testDotProduct() { // we compare accurate versus naive dot product implementations // on regular vectors (i.e. not extreme cases like in the previous test) Well1024a random = new Well1024a(553267312521321234l); for (int i = 0; i < 10000; ++i) { double ux = 10000 * random.nextDouble(); double uy = 10000 * random.nextDouble(); double uz = 10000 * random.nextDouble(); double vx = 10000 * random.nextDouble(); double vy = 10000 * random.nextDouble(); double vz = 10000 * random.nextDouble(); double sNaive = ux * vx + uy * vy + uz * vz; FieldVector3D uds = createVector(ux, uy, uz, 3); FieldVector3D vds = createVector(vx, vy, vz, 3); Vector3D v = new Vector3D(vx, vy, vz); DerivativeStructure sAccurate = FieldVector3D.dotProduct(uds, vds); Assert.assertEquals(sNaive, sAccurate.getReal(), 2.5e-16 * sNaive); Assert.assertEquals(ux + vx, sAccurate.getPartialDerivative(1, 0, 0), 2.5e-16 * sNaive); Assert.assertEquals(uy + vy, sAccurate.getPartialDerivative(0, 1, 0), 2.5e-16 * sNaive); Assert.assertEquals(uz + vz, sAccurate.getPartialDerivative(0, 0, 1), 2.5e-16 * sNaive); sAccurate = FieldVector3D.dotProduct(uds, v); Assert.assertEquals(sNaive, sAccurate.getReal(), 2.5e-16 * sNaive); Assert.assertEquals(vx, sAccurate.getPartialDerivative(1, 0, 0), 2.5e-16 * sNaive); Assert.assertEquals(vy, sAccurate.getPartialDerivative(0, 1, 0), 2.5e-16 * sNaive); Assert.assertEquals(vz, sAccurate.getPartialDerivative(0, 0, 1), 2.5e-16 * sNaive); } } @Test public void testAccurateCrossProduct() { // the vectors u1 and u2 are nearly but not exactly anti-parallel // (7.31e-16 degrees from 180 degrees) naive cross product (i.e. // computing u1.x * u2.x + u1.y * u2.y + u1.z * u2.z // leads to a result of [0.0009765, -0.0001220, -0.0039062], // instead of the correct [0.0006913, -0.0001254, -0.0007909] final FieldVector3D u1 = createVector(-1321008684645961.0 / 268435456.0, -5774608829631843.0 / 268435456.0, -7645843051051357.0 / 8589934592.0, 3); final FieldVector3D u2 = createVector( 1796571811118507.0 / 2147483648.0, 7853468008299307.0 / 2147483648.0, 2599586637357461.0 / 17179869184.0, 3); final FieldVector3D u3 = createVector(12753243807587107.0 / 18446744073709551616.0, -2313766922703915.0 / 18446744073709551616.0, -227970081415313.0 / 288230376151711744.0, 3); FieldVector3D cNaive = new FieldVector3D(u1.getY().multiply(u2.getZ()).subtract(u1.getZ().multiply(u2.getY())), u1.getZ().multiply(u2.getX()).subtract(u1.getX().multiply(u2.getZ())), u1.getX().multiply(u2.getY()).subtract(u1.getY().multiply(u2.getX()))); FieldVector3D cAccurate = FieldVector3D.crossProduct(u1, u2); Assert.assertTrue(FieldVector3D.distance(u3, cNaive).getReal() > 2.9 * u3.getNorm().getReal()); Assert.assertEquals(0.0, FieldVector3D.distance(u3, cAccurate).getReal(), 1.0e-30 * cAccurate.getNorm().getReal()); } @Test public void testCrossProduct() { // we compare accurate versus naive cross product implementations // on regular vectors (i.e. not extreme cases like in the previous test) Well1024a random = new Well1024a(885362227452043214l); for (int i = 0; i < 10000; ++i) { double ux = random.nextDouble(); double uy = random.nextDouble(); double uz = random.nextDouble(); double vx = random.nextDouble(); double vy = random.nextDouble(); double vz = random.nextDouble(); Vector3D cNaive = new Vector3D(uy * vz - uz * vy, uz * vx - ux * vz, ux * vy - uy * vx); FieldVector3D uds = createVector(ux, uy, uz, 3); FieldVector3D vds = createVector(vx, vy, vz, 3); Vector3D v = new Vector3D(vx, vy, vz); checkVector(FieldVector3D.crossProduct(uds, vds), cNaive.getX(), cNaive.getY(), cNaive.getZ(), 0, vz - uz, uy - vy, uz - vz, 0, vx - ux, vy - uy, ux - vx, 0); checkVector(FieldVector3D.crossProduct(uds, v), cNaive.getX(), cNaive.getY(), cNaive.getZ(), 0, vz, -vy, -vz, 0, vx, vy, -vx, 0); } } private FieldVector3D createVector(double x, double y, double z, int params) { return new FieldVector3D(new DerivativeStructure(params, 1, 0, x), new DerivativeStructure(params, 1, 1, y), new DerivativeStructure(params, 1, 2, z)); } private void checkVector(FieldVector3D v, double x, double y, double z) { Assert.assertEquals(x, v.getX().getReal(), 1.0e-12); Assert.assertEquals(y, v.getY().getReal(), 1.0e-12); Assert.assertEquals(z, v.getZ().getReal(), 1.0e-12); } private void checkVector(FieldVector3D v, double x, double y, double z, double dxdx, double dxdy, double dxdz, double dydx, double dydy, double dydz, double dzdx, double dzdy, double dzdz) { Assert.assertEquals(x, v.getX().getReal(), 1.0e-12); Assert.assertEquals(y, v.getY().getReal(), 1.0e-12); Assert.assertEquals(z, v.getZ().getReal(), 1.0e-12); Assert.assertEquals(dxdx, v.getX().getPartialDerivative(1, 0, 0), 1.0e-12); Assert.assertEquals(dxdy, v.getX().getPartialDerivative(0, 1, 0), 1.0e-12); Assert.assertEquals(dxdz, v.getX().getPartialDerivative(0, 0, 1), 1.0e-12); Assert.assertEquals(dydx, v.getY().getPartialDerivative(1, 0, 0), 1.0e-12); Assert.assertEquals(dydy, v.getY().getPartialDerivative(0, 1, 0), 1.0e-12); Assert.assertEquals(dydz, v.getY().getPartialDerivative(0, 0, 1), 1.0e-12); Assert.assertEquals(dzdx, v.getZ().getPartialDerivative(1, 0, 0), 1.0e-12); Assert.assertEquals(dzdy, v.getZ().getPartialDerivative(0, 1, 0), 1.0e-12); Assert.assertEquals(dzdz, v.getZ().getPartialDerivative(0, 0, 1), 1.0e-12); } private void checkVector(FieldVector3D v, double x, double y, double z, double dxdx, double dxdy, double dxdz, double dxdt, double dydx, double dydy, double dydz, double dydt, double dzdx, double dzdy, double dzdz, double dzdt) { Assert.assertEquals(x, v.getX().getReal(), 1.0e-12); Assert.assertEquals(y, v.getY().getReal(), 1.0e-12); Assert.assertEquals(z, v.getZ().getReal(), 1.0e-12); Assert.assertEquals(dxdx, v.getX().getPartialDerivative(1, 0, 0, 0), 1.0e-12); Assert.assertEquals(dxdy, v.getX().getPartialDerivative(0, 1, 0, 0), 1.0e-12); Assert.assertEquals(dxdz, v.getX().getPartialDerivative(0, 0, 1, 0), 1.0e-12); Assert.assertEquals(dxdt, v.getX().getPartialDerivative(0, 0, 0, 1), 1.0e-12); Assert.assertEquals(dydx, v.getY().getPartialDerivative(1, 0, 0, 0), 1.0e-12); Assert.assertEquals(dydy, v.getY().getPartialDerivative(0, 1, 0, 0), 1.0e-12); Assert.assertEquals(dydz, v.getY().getPartialDerivative(0, 0, 1, 0), 1.0e-12); Assert.assertEquals(dydt, v.getY().getPartialDerivative(0, 0, 0, 1), 1.0e-12); Assert.assertEquals(dzdx, v.getZ().getPartialDerivative(1, 0, 0, 0), 1.0e-12); Assert.assertEquals(dzdy, v.getZ().getPartialDerivative(0, 1, 0, 0), 1.0e-12); Assert.assertEquals(dzdz, v.getZ().getPartialDerivative(0, 0, 1, 0), 1.0e-12); Assert.assertEquals(dzdt, v.getZ().getPartialDerivative(0, 0, 0, 1), 1.0e-12); } } ././@LongLink100644 0 0 160 12126630646 10256 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/geometry/euclidean/threed/FieldRotationDSTest.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/geometry/euclidean/threed/FieldRotation100644 1750 1750 125424 12126627671 32356 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.geometry.euclidean.threed; import org.apache.commons.math3.analysis.differentiation.DerivativeStructure; import org.apache.commons.math3.exception.MathArithmeticException; import org.apache.commons.math3.exception.MathIllegalArgumentException; import org.apache.commons.math3.linear.MatrixUtils; import org.apache.commons.math3.linear.RealMatrix; import org.apache.commons.math3.random.UnitSphereRandomVectorGenerator; import org.apache.commons.math3.random.Well1024a; import org.apache.commons.math3.util.FastMath; import org.apache.commons.math3.util.MathUtils; import org.junit.Assert; import org.junit.Test; public class FieldRotationDSTest { @Test public void testIdentity() { FieldRotation r = createRotation(1, 0, 0, 0, false); checkVector(r.applyTo(createVector(1, 0, 0)), createVector(1, 0, 0)); checkVector(r.applyTo(createVector(0, 1, 0)), createVector(0, 1, 0)); checkVector(r.applyTo(createVector(0, 0, 1)), createVector(0, 0, 1)); checkAngle(r.getAngle(), 0); r = createRotation(-1, 0, 0, 0, false); checkVector(r.applyTo(createVector(1, 0, 0)), createVector(1, 0, 0)); checkVector(r.applyTo(createVector(0, 1, 0)), createVector(0, 1, 0)); checkVector(r.applyTo(createVector(0, 0, 1)), createVector(0, 0, 1)); checkAngle(r.getAngle(), 0); r = createRotation(42, 0, 0, 0, true); checkVector(r.applyTo(createVector(1, 0, 0)), createVector(1, 0, 0)); checkVector(r.applyTo(createVector(0, 1, 0)), createVector(0, 1, 0)); checkVector(r.applyTo(createVector(0, 0, 1)), createVector(0, 0, 1)); checkAngle(r.getAngle(), 0); } @Test public void testAxisAngle() throws MathIllegalArgumentException { FieldRotation r = new FieldRotation(createAxis(10, 10, 10), createAngle(2 * FastMath.PI / 3)); checkVector(r.applyTo(createVector(1, 0, 0)), createVector(0, 1, 0)); checkVector(r.applyTo(createVector(0, 1, 0)), createVector(0, 0, 1)); checkVector(r.applyTo(createVector(0, 0, 1)), createVector(1, 0, 0)); double s = 1 / FastMath.sqrt(3); checkVector(r.getAxis(), createVector(s, s, s)); checkAngle(r.getAngle(), 2 * FastMath.PI / 3); try { new FieldRotation(createAxis(0, 0, 0), createAngle(2 * FastMath.PI / 3)); Assert.fail("an exception should have been thrown"); } catch (MathIllegalArgumentException e) { } r = new FieldRotation(createAxis(0, 0, 1), createAngle(1.5 * FastMath.PI)); checkVector(r.getAxis(), createVector(0, 0, -1)); checkAngle(r.getAngle(), 0.5 * FastMath.PI); r = new FieldRotation(createAxis(0, 1, 0), createAngle(FastMath.PI)); checkVector(r.getAxis(), createVector(0, 1, 0)); checkAngle(r.getAngle(), FastMath.PI); checkVector(createRotation(1, 0, 0, 0, false).getAxis(), createVector(1, 0, 0)); } @Test public void testRevert() { double a = 0.001; double b = 0.36; double c = 0.48; double d = 0.8; FieldRotation r = createRotation(a, b, c, d, true); double a2 = a * a; double b2 = b * b; double c2 = c * c; double d2 = d * d; double den = (a2 + b2 + c2 + d2) * FastMath.sqrt(a2 + b2 + c2 + d2); Assert.assertEquals((b2 + c2 + d2) / den, r.getQ0().getPartialDerivative(1, 0, 0, 0), 1.0e-15); Assert.assertEquals(-a * b / den, r.getQ0().getPartialDerivative(0, 1, 0, 0), 1.0e-15); Assert.assertEquals(-a * c / den, r.getQ0().getPartialDerivative(0, 0, 1, 0), 1.0e-15); Assert.assertEquals(-a * d / den, r.getQ0().getPartialDerivative(0, 0, 0, 1), 1.0e-15); Assert.assertEquals(-b * a / den, r.getQ1().getPartialDerivative(1, 0, 0, 0), 1.0e-15); Assert.assertEquals((a2 + c2 + d2) / den, r.getQ1().getPartialDerivative(0, 1, 0, 0), 1.0e-15); Assert.assertEquals(-b * c / den, r.getQ1().getPartialDerivative(0, 0, 1, 0), 1.0e-15); Assert.assertEquals(-b * d / den, r.getQ1().getPartialDerivative(0, 0, 0, 1), 1.0e-15); Assert.assertEquals(-c * a / den, r.getQ2().getPartialDerivative(1, 0, 0, 0), 1.0e-15); Assert.assertEquals(-c * b / den, r.getQ2().getPartialDerivative(0, 1, 0, 0), 1.0e-15); Assert.assertEquals((a2 + b2 + d2) / den, r.getQ2().getPartialDerivative(0, 0, 1, 0), 1.0e-15); Assert.assertEquals(-c * d / den, r.getQ2().getPartialDerivative(0, 0, 0, 1), 1.0e-15); Assert.assertEquals(-d * a / den, r.getQ3().getPartialDerivative(1, 0, 0, 0), 1.0e-15); Assert.assertEquals(-d * b / den, r.getQ3().getPartialDerivative(0, 1, 0, 0), 1.0e-15); Assert.assertEquals(-d * c / den, r.getQ3().getPartialDerivative(0, 0, 1, 0), 1.0e-15); Assert.assertEquals((a2 + b2 + c2) / den, r.getQ3().getPartialDerivative(0, 0, 0, 1), 1.0e-15); FieldRotation reverted = r.revert(); FieldRotation rrT = r.applyTo(reverted); checkRotationDS(rrT, 1, 0, 0, 0); Assert.assertEquals(0, rrT.getQ0().getPartialDerivative(1, 0, 0, 0), 1.0e-15); Assert.assertEquals(0, rrT.getQ0().getPartialDerivative(0, 1, 0, 0), 1.0e-15); Assert.assertEquals(0, rrT.getQ0().getPartialDerivative(0, 0, 1, 0), 1.0e-15); Assert.assertEquals(0, rrT.getQ0().getPartialDerivative(0, 0, 0, 1), 1.0e-15); Assert.assertEquals(0, rrT.getQ1().getPartialDerivative(1, 0, 0, 0), 1.0e-15); Assert.assertEquals(0, rrT.getQ1().getPartialDerivative(0, 1, 0, 0), 1.0e-15); Assert.assertEquals(0, rrT.getQ1().getPartialDerivative(0, 0, 1, 0), 1.0e-15); Assert.assertEquals(0, rrT.getQ1().getPartialDerivative(0, 0, 0, 1), 1.0e-15); Assert.assertEquals(0, rrT.getQ2().getPartialDerivative(1, 0, 0, 0), 1.0e-15); Assert.assertEquals(0, rrT.getQ2().getPartialDerivative(0, 1, 0, 0), 1.0e-15); Assert.assertEquals(0, rrT.getQ2().getPartialDerivative(0, 0, 1, 0), 1.0e-15); Assert.assertEquals(0, rrT.getQ2().getPartialDerivative(0, 0, 0, 1), 1.0e-15); Assert.assertEquals(0, rrT.getQ3().getPartialDerivative(1, 0, 0, 0), 1.0e-15); Assert.assertEquals(0, rrT.getQ3().getPartialDerivative(0, 1, 0, 0), 1.0e-15); Assert.assertEquals(0, rrT.getQ3().getPartialDerivative(0, 0, 1, 0), 1.0e-15); Assert.assertEquals(0, rrT.getQ3().getPartialDerivative(0, 0, 0, 1), 1.0e-15); FieldRotation rTr = reverted.applyTo(r); checkRotationDS(rTr, 1, 0, 0, 0); Assert.assertEquals(0, rTr.getQ0().getPartialDerivative(1, 0, 0, 0), 1.0e-15); Assert.assertEquals(0, rTr.getQ0().getPartialDerivative(0, 1, 0, 0), 1.0e-15); Assert.assertEquals(0, rTr.getQ0().getPartialDerivative(0, 0, 1, 0), 1.0e-15); Assert.assertEquals(0, rTr.getQ0().getPartialDerivative(0, 0, 0, 1), 1.0e-15); Assert.assertEquals(0, rTr.getQ1().getPartialDerivative(1, 0, 0, 0), 1.0e-15); Assert.assertEquals(0, rTr.getQ1().getPartialDerivative(0, 1, 0, 0), 1.0e-15); Assert.assertEquals(0, rTr.getQ1().getPartialDerivative(0, 0, 1, 0), 1.0e-15); Assert.assertEquals(0, rTr.getQ1().getPartialDerivative(0, 0, 0, 1), 1.0e-15); Assert.assertEquals(0, rTr.getQ2().getPartialDerivative(1, 0, 0, 0), 1.0e-15); Assert.assertEquals(0, rTr.getQ2().getPartialDerivative(0, 1, 0, 0), 1.0e-15); Assert.assertEquals(0, rTr.getQ2().getPartialDerivative(0, 0, 1, 0), 1.0e-15); Assert.assertEquals(0, rTr.getQ2().getPartialDerivative(0, 0, 0, 1), 1.0e-15); Assert.assertEquals(0, rTr.getQ3().getPartialDerivative(1, 0, 0, 0), 1.0e-15); Assert.assertEquals(0, rTr.getQ3().getPartialDerivative(0, 1, 0, 0), 1.0e-15); Assert.assertEquals(0, rTr.getQ3().getPartialDerivative(0, 0, 1, 0), 1.0e-15); Assert.assertEquals(0, rTr.getQ3().getPartialDerivative(0, 0, 0, 1), 1.0e-15); Assert.assertEquals(r.getAngle().getReal(), reverted.getAngle().getReal(), 1.0e-15); Assert.assertEquals(-1, FieldVector3D.dotProduct(r.getAxis(), reverted.getAxis()).getReal(), 1.0e-15); } @Test public void testVectorOnePair() throws MathArithmeticException { FieldVector3D u = createVector(3, 2, 1); FieldVector3D v = createVector(-4, 2, 2); FieldRotation r = new FieldRotation(u, v); checkVector(r.applyTo(u.scalarMultiply(v.getNorm())), v.scalarMultiply(u.getNorm())); checkAngle(new FieldRotation(u, u.negate()).getAngle(), FastMath.PI); try { new FieldRotation(u, createVector(0, 0, 0)); Assert.fail("an exception should have been thrown"); } catch (MathArithmeticException e) { // expected behavior } } @Test public void testVectorTwoPairs() throws MathArithmeticException { FieldVector3D u1 = createVector(3, 0, 0); FieldVector3D u2 = createVector(0, 5, 0); FieldVector3D v1 = createVector(0, 0, 2); FieldVector3D v2 = createVector(-2, 0, 2); FieldRotation r = new FieldRotation(u1, u2, v1, v2); checkVector(r.applyTo(createVector(1, 0, 0)), createVector(0, 0, 1)); checkVector(r.applyTo(createVector(0, 1, 0)), createVector(-1, 0, 0)); r = new FieldRotation(u1, u2, u1.negate(), u2.negate()); FieldVector3D axis = r.getAxis(); if (FieldVector3D.dotProduct(axis, createVector(0, 0, 1)).getReal() > 0) { checkVector(axis, createVector(0, 0, 1)); } else { checkVector(axis, createVector(0, 0, -1)); } checkAngle(r.getAngle(), FastMath.PI); double sqrt = FastMath.sqrt(2) / 2; r = new FieldRotation(createVector(1, 0, 0), createVector(0, 1, 0), createVector(0.5, 0.5, sqrt), createVector(0.5, 0.5, -sqrt)); checkRotationDS(r, sqrt, 0.5, 0.5, 0); r = new FieldRotation(u1, u2, u1, FieldVector3D.crossProduct(u1, u2)); checkRotationDS(r, sqrt, -sqrt, 0, 0); checkRotationDS(new FieldRotation(u1, u2, u1, u2), 1, 0, 0, 0); try { new FieldRotation(u1, u2, createVector(0, 0, 0), v2); Assert.fail("an exception should have been thrown"); } catch (MathArithmeticException e) { // expected behavior } } @Test public void testMatrix() throws NotARotationMatrixException { try { createRotation(new double[][] { { 0.0, 1.0, 0.0 }, { 1.0, 0.0, 0.0 } }, 1.0e-7); Assert.fail("Expecting NotARotationMatrixException"); } catch (NotARotationMatrixException nrme) { // expected behavior } try { createRotation(new double[][] { { 0.445888, 0.797184, -0.407040 }, { 0.821760, -0.184320, 0.539200 }, { -0.354816, 0.574912, 0.737280 } }, 1.0e-7); Assert.fail("Expecting NotARotationMatrixException"); } catch (NotARotationMatrixException nrme) { // expected behavior } try { createRotation(new double[][] { { 0.4, 0.8, -0.4 }, { -0.4, 0.6, 0.7 }, { 0.8, -0.2, 0.5 } }, 1.0e-15); Assert.fail("Expecting NotARotationMatrixException"); } catch (NotARotationMatrixException nrme) { // expected behavior } checkRotationDS(createRotation(new double[][] { { 0.445888, 0.797184, -0.407040 }, { -0.354816, 0.574912, 0.737280 }, { 0.821760, -0.184320, 0.539200 } }, 1.0e-10), 0.8, 0.288, 0.384, 0.36); checkRotationDS(createRotation(new double[][] { { 0.539200, 0.737280, 0.407040 }, { 0.184320, -0.574912, 0.797184 }, { 0.821760, -0.354816, -0.445888 } }, 1.0e-10), 0.36, 0.8, 0.288, 0.384); checkRotationDS(createRotation(new double[][] { { -0.445888, 0.797184, -0.407040 }, { 0.354816, 0.574912, 0.737280 }, { 0.821760, 0.184320, -0.539200 } }, 1.0e-10), 0.384, 0.36, 0.8, 0.288); checkRotationDS(createRotation(new double[][] { { -0.539200, 0.737280, 0.407040 }, { -0.184320, -0.574912, 0.797184 }, { 0.821760, 0.354816, 0.445888 } }, 1.0e-10), 0.288, 0.384, 0.36, 0.8); double[][] m1 = { { 0.0, 1.0, 0.0 }, { 0.0, 0.0, 1.0 }, { 1.0, 0.0, 0.0 } }; FieldRotation r = createRotation(m1, 1.0e-7); checkVector(r.applyTo(createVector(1, 0, 0)), createVector(0, 0, 1)); checkVector(r.applyTo(createVector(0, 1, 0)), createVector(1, 0, 0)); checkVector(r.applyTo(createVector(0, 0, 1)), createVector(0, 1, 0)); double[][] m2 = { { 0.83203, -0.55012, -0.07139 }, { 0.48293, 0.78164, -0.39474 }, { 0.27296, 0.29396, 0.91602 } }; r = createRotation(m2, 1.0e-12); DerivativeStructure[][] m3 = r.getMatrix(); double d00 = m2[0][0] - m3[0][0].getReal(); double d01 = m2[0][1] - m3[0][1].getReal(); double d02 = m2[0][2] - m3[0][2].getReal(); double d10 = m2[1][0] - m3[1][0].getReal(); double d11 = m2[1][1] - m3[1][1].getReal(); double d12 = m2[1][2] - m3[1][2].getReal(); double d20 = m2[2][0] - m3[2][0].getReal(); double d21 = m2[2][1] - m3[2][1].getReal(); double d22 = m2[2][2] - m3[2][2].getReal(); Assert.assertTrue(FastMath.abs(d00) < 6.0e-6); Assert.assertTrue(FastMath.abs(d01) < 6.0e-6); Assert.assertTrue(FastMath.abs(d02) < 6.0e-6); Assert.assertTrue(FastMath.abs(d10) < 6.0e-6); Assert.assertTrue(FastMath.abs(d11) < 6.0e-6); Assert.assertTrue(FastMath.abs(d12) < 6.0e-6); Assert.assertTrue(FastMath.abs(d20) < 6.0e-6); Assert.assertTrue(FastMath.abs(d21) < 6.0e-6); Assert.assertTrue(FastMath.abs(d22) < 6.0e-6); Assert.assertTrue(FastMath.abs(d00) > 4.0e-7); Assert.assertTrue(FastMath.abs(d01) > 4.0e-7); Assert.assertTrue(FastMath.abs(d02) > 4.0e-7); Assert.assertTrue(FastMath.abs(d10) > 4.0e-7); Assert.assertTrue(FastMath.abs(d11) > 4.0e-7); Assert.assertTrue(FastMath.abs(d12) > 4.0e-7); Assert.assertTrue(FastMath.abs(d20) > 4.0e-7); Assert.assertTrue(FastMath.abs(d21) > 4.0e-7); Assert.assertTrue(FastMath.abs(d22) > 4.0e-7); for (int i = 0; i < 3; ++i) { for (int j = 0; j < 3; ++j) { double m3tm3 = m3[i][0].getReal() * m3[j][0].getReal() + m3[i][1].getReal() * m3[j][1].getReal() + m3[i][2].getReal() * m3[j][2].getReal(); if (i == j) { Assert.assertTrue(FastMath.abs(m3tm3 - 1.0) < 1.0e-10); } else { Assert.assertTrue(FastMath.abs(m3tm3) < 1.0e-10); } } } checkVector(r.applyTo(createVector(1, 0, 0)), new FieldVector3D(m3[0][0], m3[1][0], m3[2][0])); checkVector(r.applyTo(createVector(0, 1, 0)), new FieldVector3D(m3[0][1], m3[1][1], m3[2][1])); checkVector(r.applyTo(createVector(0, 0, 1)), new FieldVector3D(m3[0][2], m3[1][2], m3[2][2])); double[][] m4 = { { 1.0, 0.0, 0.0 }, { 0.0, -1.0, 0.0 }, { 0.0, 0.0, -1.0 } }; r = createRotation(m4, 1.0e-7); checkAngle(r.getAngle(), FastMath.PI); try { double[][] m5 = { { 0.0, 0.0, 1.0 }, { 0.0, 1.0, 0.0 }, { 1.0, 0.0, 0.0 } }; r = createRotation(m5, 1.0e-7); Assert.fail("got " + r + ", should have caught an exception"); } catch (NotARotationMatrixException e) { // expected } } @Test public void testAngles() throws CardanEulerSingularityException { RotationOrder[] CardanOrders = { RotationOrder.XYZ, RotationOrder.XZY, RotationOrder.YXZ, RotationOrder.YZX, RotationOrder.ZXY, RotationOrder.ZYX }; for (int i = 0; i < CardanOrders.length; ++i) { for (double alpha1 = 0.1; alpha1 < 6.2; alpha1 += 0.3) { for (double alpha2 = -1.55; alpha2 < 1.55; alpha2 += 0.3) { for (double alpha3 = 0.1; alpha3 < 6.2; alpha3 += 0.3) { FieldRotation r = new FieldRotation(CardanOrders[i], new DerivativeStructure(3, 1, 0, alpha1), new DerivativeStructure(3, 1, 1, alpha2), new DerivativeStructure(3, 1, 2, alpha3)); DerivativeStructure[] angles = r.getAngles(CardanOrders[i]); checkAngle(angles[0], alpha1); checkAngle(angles[1], alpha2); checkAngle(angles[2], alpha3); } } } } RotationOrder[] EulerOrders = { RotationOrder.XYX, RotationOrder.XZX, RotationOrder.YXY, RotationOrder.YZY, RotationOrder.ZXZ, RotationOrder.ZYZ }; for (int i = 0; i < EulerOrders.length; ++i) { for (double alpha1 = 0.1; alpha1 < 6.2; alpha1 += 0.3) { for (double alpha2 = 0.05; alpha2 < 3.1; alpha2 += 0.3) { for (double alpha3 = 0.1; alpha3 < 6.2; alpha3 += 0.3) { FieldRotation r = new FieldRotation(EulerOrders[i], new DerivativeStructure(3, 1, 0, alpha1), new DerivativeStructure(3, 1, 1, alpha2), new DerivativeStructure(3, 1, 2, alpha3)); DerivativeStructure[] angles = r.getAngles(EulerOrders[i]); checkAngle(angles[0], alpha1); checkAngle(angles[1], alpha2); checkAngle(angles[2], alpha3); } } } } } @Test public void testSingularities() { RotationOrder[] CardanOrders = { RotationOrder.XYZ, RotationOrder.XZY, RotationOrder.YXZ, RotationOrder.YZX, RotationOrder.ZXY, RotationOrder.ZYX }; double[] singularCardanAngle = { FastMath.PI / 2, -FastMath.PI / 2 }; for (int i = 0; i < CardanOrders.length; ++i) { for (int j = 0; j < singularCardanAngle.length; ++j) { FieldRotation r = new FieldRotation(CardanOrders[i], new DerivativeStructure(3, 1, 0, 0.1), new DerivativeStructure(3, 1, 1, singularCardanAngle[j]), new DerivativeStructure(3, 1, 2, 0.3)); try { r.getAngles(CardanOrders[i]); Assert.fail("an exception should have been caught"); } catch (CardanEulerSingularityException cese) { // expected behavior } } } RotationOrder[] EulerOrders = { RotationOrder.XYX, RotationOrder.XZX, RotationOrder.YXY, RotationOrder.YZY, RotationOrder.ZXZ, RotationOrder.ZYZ }; double[] singularEulerAngle = { 0, FastMath.PI }; for (int i = 0; i < EulerOrders.length; ++i) { for (int j = 0; j < singularEulerAngle.length; ++j) { FieldRotation r = new FieldRotation(EulerOrders[i], new DerivativeStructure(3, 1, 0, 0.1), new DerivativeStructure(3, 1, 1, singularEulerAngle[j]), new DerivativeStructure(3, 1, 2, 0.3)); try { r.getAngles(EulerOrders[i]); Assert.fail("an exception should have been caught"); } catch (CardanEulerSingularityException cese) { // expected behavior } } } } @Test public void testQuaternion() throws MathIllegalArgumentException { FieldRotation r1 = new FieldRotation(createVector(2, -3, 5), createAngle(1.7)); double n = 23.5; FieldRotation r2 = new FieldRotation(r1.getQ0().multiply(n), r1.getQ1().multiply(n), r1.getQ2().multiply(n), r1.getQ3().multiply(n), true); for (double x = -0.9; x < 0.9; x += 0.2) { for (double y = -0.9; y < 0.9; y += 0.2) { for (double z = -0.9; z < 0.9; z += 0.2) { FieldVector3D u = createVector(x, y, z); checkVector(r2.applyTo(u), r1.applyTo(u)); } } } r1 = createRotation(0.288, 0.384, 0.36, 0.8, false); checkRotationDS(r1, -r1.getQ0().getReal(), -r1.getQ1().getReal(), -r1.getQ2().getReal(), -r1.getQ3().getReal()); Assert.assertEquals(0.288, r1.toRotation().getQ0(), 1.0e-15); Assert.assertEquals(0.384, r1.toRotation().getQ1(), 1.0e-15); Assert.assertEquals(0.36, r1.toRotation().getQ2(), 1.0e-15); Assert.assertEquals(0.8, r1.toRotation().getQ3(), 1.0e-15); } @Test public void testCompose() throws MathIllegalArgumentException { FieldRotation r1 = new FieldRotation(createVector(2, -3, 5), createAngle(1.7)); FieldRotation r2 = new FieldRotation(createVector(-1, 3, 2), createAngle(0.3)); FieldRotation r3 = r2.applyTo(r1); FieldRotation r3Double = r2.applyTo(new Rotation(r1.getQ0().getReal(), r1.getQ1().getReal(), r1.getQ2().getReal(), r1.getQ3().getReal(), false)); for (double x = -0.9; x < 0.9; x += 0.2) { for (double y = -0.9; y < 0.9; y += 0.2) { for (double z = -0.9; z < 0.9; z += 0.2) { FieldVector3D u = createVector(x, y, z); checkVector(r2.applyTo(r1.applyTo(u)), r3.applyTo(u)); checkVector(r2.applyTo(r1.applyTo(u)), r3Double.applyTo(u)); } } } } @Test public void testComposeInverse() throws MathIllegalArgumentException { FieldRotation r1 = new FieldRotation(createVector(2, -3, 5), createAngle(1.7)); FieldRotation r2 = new FieldRotation(createVector(-1, 3, 2), createAngle(0.3)); FieldRotation r3 = r2.applyInverseTo(r1); FieldRotation r3Double = r2.applyInverseTo(new Rotation(r1.getQ0().getReal(), r1.getQ1().getReal(), r1.getQ2().getReal(), r1.getQ3().getReal(), false)); for (double x = -0.9; x < 0.9; x += 0.2) { for (double y = -0.9; y < 0.9; y += 0.2) { for (double z = -0.9; z < 0.9; z += 0.2) { FieldVector3D u = createVector(x, y, z); checkVector(r2.applyInverseTo(r1.applyTo(u)), r3.applyTo(u)); checkVector(r2.applyInverseTo(r1.applyTo(u)), r3Double.applyTo(u)); } } } } @Test public void testDoubleVectors() throws MathIllegalArgumentException { Well1024a random = new Well1024a(0x180b41cfeeffaf67l); UnitSphereRandomVectorGenerator g = new UnitSphereRandomVectorGenerator(3, random); for (int i = 0; i < 10; ++i) { double[] unit = g.nextVector(); FieldRotation r = new FieldRotation(createVector(unit[0], unit[1], unit[2]), createAngle(random.nextDouble())); for (double x = -0.9; x < 0.9; x += 0.2) { for (double y = -0.9; y < 0.9; y += 0.2) { for (double z = -0.9; z < 0.9; z += 0.2) { FieldVector3D uds = createVector(x, y, z); FieldVector3D ruds = r.applyTo(uds); FieldVector3D rIuds = r.applyInverseTo(uds); Vector3D u = new Vector3D(x, y, z); FieldVector3D ru = r.applyTo(u); FieldVector3D rIu = r.applyInverseTo(u); DerivativeStructure[] ruArray = new DerivativeStructure[3]; r.applyTo(new double[] { x, y, z}, ruArray); DerivativeStructure[] rIuArray = new DerivativeStructure[3]; r.applyInverseTo(new double[] { x, y, z}, rIuArray); checkVector(ruds, ru); checkVector(ruds, new FieldVector3D(ruArray)); checkVector(rIuds, rIu); checkVector(rIuds, new FieldVector3D(rIuArray)); } } } } } @Test public void testDoubleRotations() throws MathIllegalArgumentException { Well1024a random = new Well1024a(0x180b41cfeeffaf67l); UnitSphereRandomVectorGenerator g = new UnitSphereRandomVectorGenerator(3, random); for (int i = 0; i < 10; ++i) { double[] unit1 = g.nextVector(); Rotation r1 = new Rotation(new Vector3D(unit1[0], unit1[1], unit1[2]), random.nextDouble()); FieldRotation r1Prime = new FieldRotation(new DerivativeStructure(4, 1, 0, r1.getQ0()), new DerivativeStructure(4, 1, 1, r1.getQ1()), new DerivativeStructure(4, 1, 2, r1.getQ2()), new DerivativeStructure(4, 1, 3, r1.getQ3()), false); double[] unit2 = g.nextVector(); FieldRotation r2 = new FieldRotation(createVector(unit2[0], unit2[1], unit2[2]), createAngle(random.nextDouble())); FieldRotation rA = FieldRotation.applyTo(r1, r2); FieldRotation rB = r1Prime.applyTo(r2); FieldRotation rC = FieldRotation.applyInverseTo(r1, r2); FieldRotation rD = r1Prime.applyInverseTo(r2); for (double x = -0.9; x < 0.9; x += 0.2) { for (double y = -0.9; y < 0.9; y += 0.2) { for (double z = -0.9; z < 0.9; z += 0.2) { FieldVector3D uds = createVector(x, y, z); checkVector(r1Prime.applyTo(uds), FieldRotation.applyTo(r1, uds)); checkVector(r1Prime.applyInverseTo(uds), FieldRotation.applyInverseTo(r1, uds)); checkVector(rA.applyTo(uds), rB.applyTo(uds)); checkVector(rA.applyInverseTo(uds), rB.applyInverseTo(uds)); checkVector(rC.applyTo(uds), rD.applyTo(uds)); checkVector(rC.applyInverseTo(uds), rD.applyInverseTo(uds)); } } } } } @Test public void testDerivatives() { double eps = 5.0e-16; double kx = 2; double ky = -3; double kz = 5; double n2 = kx * kx + ky * ky + kz * kz; double n = FastMath.sqrt(n2); double theta = 1.7; double cosTheta = FastMath.cos(theta); double sinTheta = FastMath.sin(theta); FieldRotation r = new FieldRotation(createAxis(kx, ky, kz), createAngle(theta)); Vector3D a = new Vector3D(kx / n, ky / n, kz / n); // Jacobian of the normalized rotation axis a with respect to the Cartesian vector k RealMatrix dadk = MatrixUtils.createRealMatrix(new double[][] { { (ky * ky + kz * kz) / ( n * n2), -kx * ky / ( n * n2), -kx * kz / ( n * n2) }, { -kx * ky / ( n * n2), (kx * kx + kz * kz) / ( n * n2), -ky * kz / ( n * n2) }, { -kx * kz / ( n * n2), -ky * kz / ( n * n2), (kx * kx + ky * ky) / ( n * n2) } }); for (double x = -0.9; x < 0.9; x += 0.2) { for (double y = -0.9; y < 0.9; y += 0.2) { for (double z = -0.9; z < 0.9; z += 0.2) { Vector3D u = new Vector3D(x, y, z); FieldVector3D v = r.applyTo(createVector(x, y, z)); // explicit formula for rotation of vector u around axis a with angle theta double dot = Vector3D.dotProduct(u, a); Vector3D cross = Vector3D.crossProduct(a, u); double c1 = 1 - cosTheta; double c2 = c1 * dot; Vector3D rt = new Vector3D(cosTheta, u, c2, a, sinTheta, cross); Assert.assertEquals(rt.getX(), v.getX().getReal(), eps); Assert.assertEquals(rt.getY(), v.getY().getReal(), eps); Assert.assertEquals(rt.getZ(), v.getZ().getReal(), eps); // Jacobian of the image v = r(u) with respect to rotation axis a // (analytical differentiation of the explicit formula) RealMatrix dvda = MatrixUtils.createRealMatrix(new double[][] { { c1 * x * a.getX() + c2, c1 * y * a.getX() + sinTheta * z, c1 * z * a.getX() - sinTheta * y }, { c1 * x * a.getY() - sinTheta * z, c1 * y * a.getY() + c2, c1 * z * a.getY() + sinTheta * x }, { c1 * x * a.getZ() + sinTheta * y, c1 * y * a.getZ() - sinTheta * x, c1 * z * a.getZ() + c2 } }); // compose Jacobians RealMatrix dvdk = dvda.multiply(dadk); // derivatives with respect to un-normalized axis Assert.assertEquals(dvdk.getEntry(0, 0), v.getX().getPartialDerivative(1, 0, 0, 0), eps); Assert.assertEquals(dvdk.getEntry(0, 1), v.getX().getPartialDerivative(0, 1, 0, 0), eps); Assert.assertEquals(dvdk.getEntry(0, 2), v.getX().getPartialDerivative(0, 0, 1, 0), eps); Assert.assertEquals(dvdk.getEntry(1, 0), v.getY().getPartialDerivative(1, 0, 0, 0), eps); Assert.assertEquals(dvdk.getEntry(1, 1), v.getY().getPartialDerivative(0, 1, 0, 0), eps); Assert.assertEquals(dvdk.getEntry(1, 2), v.getY().getPartialDerivative(0, 0, 1, 0), eps); Assert.assertEquals(dvdk.getEntry(2, 0), v.getZ().getPartialDerivative(1, 0, 0, 0), eps); Assert.assertEquals(dvdk.getEntry(2, 1), v.getZ().getPartialDerivative(0, 1, 0, 0), eps); Assert.assertEquals(dvdk.getEntry(2, 2), v.getZ().getPartialDerivative(0, 0, 1, 0), eps); // derivative with respect to rotation angle // (analytical differentiation of the explicit formula) Vector3D dvdTheta = new Vector3D(-sinTheta, u, sinTheta * dot, a, cosTheta, cross); Assert.assertEquals(dvdTheta.getX(), v.getX().getPartialDerivative(0, 0, 0, 1), eps); Assert.assertEquals(dvdTheta.getY(), v.getY().getPartialDerivative(0, 0, 0, 1), eps); Assert.assertEquals(dvdTheta.getZ(), v.getZ().getPartialDerivative(0, 0, 0, 1), eps); } } } } @Test public void testArray() throws MathIllegalArgumentException { FieldRotation r = new FieldRotation(createAxis(2, -3, 5), createAngle(1.7)); for (double x = -0.9; x < 0.9; x += 0.2) { for (double y = -0.9; y < 0.9; y += 0.2) { for (double z = -0.9; z < 0.9; z += 0.2) { FieldVector3D u = createVector(x, y, z); FieldVector3D v = r.applyTo(u); DerivativeStructure[] out = new DerivativeStructure[3]; r.applyTo(new DerivativeStructure[] { u.getX(), u.getY(), u.getZ() }, out); Assert.assertEquals(v.getX().getReal(), out[0].getReal(), 1.0e-10); Assert.assertEquals(v.getY().getReal(), out[1].getReal(), 1.0e-10); Assert.assertEquals(v.getZ().getReal(), out[2].getReal(), 1.0e-10); r.applyInverseTo(out, out); Assert.assertEquals(u.getX().getReal(), out[0].getReal(), 1.0e-10); Assert.assertEquals(u.getY().getReal(), out[1].getReal(), 1.0e-10); Assert.assertEquals(u.getZ().getReal(), out[2].getReal(), 1.0e-10); } } } } @Test public void testApplyInverseTo() throws MathIllegalArgumentException { DerivativeStructure[] in = new DerivativeStructure[3]; DerivativeStructure[] out = new DerivativeStructure[3]; DerivativeStructure[] rebuilt = new DerivativeStructure[3]; FieldRotation r = new FieldRotation(createVector(2, -3, 5), createAngle(1.7)); for (double lambda = 0; lambda < 6.2; lambda += 0.2) { for (double phi = -1.55; phi < 1.55; phi += 0.2) { FieldVector3D u = createVector(FastMath.cos(lambda) * FastMath.cos(phi), FastMath.sin(lambda) * FastMath.cos(phi), FastMath.sin(phi)); r.applyInverseTo(r.applyTo(u)); checkVector(u, r.applyInverseTo(r.applyTo(u))); checkVector(u, r.applyTo(r.applyInverseTo(u))); in[0] = u.getX(); in[1] = u.getY(); in[2] = u.getZ(); r.applyTo(in, out); r.applyInverseTo(out, rebuilt); Assert.assertEquals(in[0].getReal(), rebuilt[0].getReal(), 1.0e-12); Assert.assertEquals(in[1].getReal(), rebuilt[1].getReal(), 1.0e-12); Assert.assertEquals(in[2].getReal(), rebuilt[2].getReal(), 1.0e-12); } } r = createRotation(1, 0, 0, 0, false); for (double lambda = 0; lambda < 6.2; lambda += 0.2) { for (double phi = -1.55; phi < 1.55; phi += 0.2) { FieldVector3D u = createVector(FastMath.cos(lambda) * FastMath.cos(phi), FastMath.sin(lambda) * FastMath.cos(phi), FastMath.sin(phi)); checkVector(u, r.applyInverseTo(r.applyTo(u))); checkVector(u, r.applyTo(r.applyInverseTo(u))); } } r = new FieldRotation(createVector(0, 0, 1), createAngle(FastMath.PI)); for (double lambda = 0; lambda < 6.2; lambda += 0.2) { for (double phi = -1.55; phi < 1.55; phi += 0.2) { FieldVector3D u = createVector(FastMath.cos(lambda) * FastMath.cos(phi), FastMath.sin(lambda) * FastMath.cos(phi), FastMath.sin(phi)); checkVector(u, r.applyInverseTo(r.applyTo(u))); checkVector(u, r.applyTo(r.applyInverseTo(u))); } } } @Test public void testIssue639() throws MathArithmeticException{ FieldVector3D u1 = createVector(-1321008684645961.0 / 268435456.0, -5774608829631843.0 / 268435456.0, -3822921525525679.0 / 4294967296.0); FieldVector3D u2 =createVector( -5712344449280879.0 / 2097152.0, -2275058564560979.0 / 1048576.0, 4423475992255071.0 / 65536.0); FieldRotation rot = new FieldRotation(u1, u2, createVector(1, 0, 0),createVector(0, 0, 1)); Assert.assertEquals( 0.6228370359608200639829222, rot.getQ0().getReal(), 1.0e-15); Assert.assertEquals( 0.0257707621456498790029987, rot.getQ1().getReal(), 1.0e-15); Assert.assertEquals(-0.0000000002503012255839931, rot.getQ2().getReal(), 1.0e-15); Assert.assertEquals(-0.7819270390861109450724902, rot.getQ3().getReal(), 1.0e-15); } @Test public void testIssue801() throws MathArithmeticException { FieldVector3D u1 = createVector(0.9999988431610581, -0.0015210774290851095, 0.0); FieldVector3D u2 = createVector(0.0, 0.0, 1.0); FieldVector3D v1 = createVector(0.9999999999999999, 0.0, 0.0); FieldVector3D v2 = createVector(0.0, 0.0, -1.0); FieldRotation quat = new FieldRotation(u1, u2, v1, v2); double q2 = quat.getQ0().getReal() * quat.getQ0().getReal() + quat.getQ1().getReal() * quat.getQ1().getReal() + quat.getQ2().getReal() * quat.getQ2().getReal() + quat.getQ3().getReal() * quat.getQ3().getReal(); Assert.assertEquals(1.0, q2, 1.0e-14); Assert.assertEquals(0.0, FieldVector3D.angle(v1, quat.applyTo(u1)).getReal(), 1.0e-14); Assert.assertEquals(0.0, FieldVector3D.angle(v2, quat.applyTo(u2)).getReal(), 1.0e-14); } private void checkAngle(DerivativeStructure a1, double a2) { Assert.assertEquals(a1.getReal(), MathUtils.normalizeAngle(a2, a1.getReal()), 1.0e-10); } private void checkRotationDS(FieldRotation r, double q0, double q1, double q2, double q3) { FieldRotation rPrime = createRotation(q0, q1, q2, q3, false); Assert.assertEquals(0, FieldRotation.distance(r, rPrime).getReal(), 1.0e-12); } private FieldRotation createRotation(double q0, double q1, double q2, double q3, boolean needsNormalization) { return new FieldRotation(new DerivativeStructure(4, 1, 0, q0), new DerivativeStructure(4, 1, 1, q1), new DerivativeStructure(4, 1, 2, q2), new DerivativeStructure(4, 1, 3, q3), needsNormalization); } private FieldRotation createRotation(double[][] m, double threshold) { DerivativeStructure[][] mds = new DerivativeStructure[m.length][m[0].length]; int index = 0; for (int i = 0; i < m.length; ++i) { for (int j = 0; j < m[i].length; ++j) { mds[i][j] = new DerivativeStructure(4, 1, index, m[i][j]); index = (index + 1) % 4; } } return new FieldRotation(mds, threshold); } private FieldVector3D createVector(double x, double y, double z) { return new FieldVector3D(new DerivativeStructure(4, 1, x), new DerivativeStructure(4, 1, y), new DerivativeStructure(4, 1, z)); } private FieldVector3D createAxis(double x, double y, double z) { return new FieldVector3D(new DerivativeStructure(4, 1, 0, x), new DerivativeStructure(4, 1, 1, y), new DerivativeStructure(4, 1, 2, z)); } private DerivativeStructure createAngle(double alpha) { return new DerivativeStructure(4, 1, 3, alpha); } private void checkVector(FieldVector3D u, FieldVector3D v) { Assert.assertEquals(u.getX().getReal(), v.getX().getReal(), 1.0e-12); Assert.assertEquals(u.getY().getReal(), v.getY().getReal(), 1.0e-12); Assert.assertEquals(u.getZ().getReal(), v.getZ().getReal(), 1.0e-12); } } ././@LongLink100644 0 0 157 12126630646 10264 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/geometry/euclidean/threed/Vector3DFormatTest.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/geometry/euclidean/threed/Vector3DForma100644 1750 1750 2151 12126627671 32160 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.geometry.euclidean.threed; import java.util.Locale; public class Vector3DFormatTest extends Vector3DFormatAbstractTest { @Override protected char getDecimalCharacter() { return '.'; } @Override protected Locale getLocale() { return Locale.US; } } ././@LongLink100644 0 0 157 12126630646 10264 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/geometry/euclidean/threed/PolyhedronsSetTest.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/geometry/euclidean/threed/PolyhedronsSe100644 1750 1750 32107 12126627671 32364 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.geometry.euclidean.threed; import java.util.ArrayList; import org.apache.commons.math3.exception.MathArithmeticException; import org.apache.commons.math3.exception.MathIllegalArgumentException; import org.apache.commons.math3.geometry.euclidean.twod.Euclidean2D; import org.apache.commons.math3.geometry.euclidean.twod.PolygonsSet; import org.apache.commons.math3.geometry.euclidean.twod.SubLine; import org.apache.commons.math3.geometry.euclidean.twod.Vector2D; import org.apache.commons.math3.geometry.partitioning.BSPTree; import org.apache.commons.math3.geometry.partitioning.BSPTreeVisitor; import org.apache.commons.math3.geometry.partitioning.BoundaryAttribute; import org.apache.commons.math3.geometry.partitioning.Region; import org.apache.commons.math3.geometry.partitioning.RegionFactory; import org.apache.commons.math3.geometry.partitioning.SubHyperplane; import org.apache.commons.math3.util.FastMath; import org.junit.Assert; import org.junit.Test; public class PolyhedronsSetTest { @Test public void testBox() { PolyhedronsSet tree = new PolyhedronsSet(0, 1, 0, 1, 0, 1); Assert.assertEquals(1.0, tree.getSize(), 1.0e-10); Assert.assertEquals(6.0, tree.getBoundarySize(), 1.0e-10); Vector3D barycenter = (Vector3D) tree.getBarycenter(); Assert.assertEquals(0.5, barycenter.getX(), 1.0e-10); Assert.assertEquals(0.5, barycenter.getY(), 1.0e-10); Assert.assertEquals(0.5, barycenter.getZ(), 1.0e-10); for (double x = -0.25; x < 1.25; x += 0.1) { boolean xOK = (x >= 0.0) && (x <= 1.0); for (double y = -0.25; y < 1.25; y += 0.1) { boolean yOK = (y >= 0.0) && (y <= 1.0); for (double z = -0.25; z < 1.25; z += 0.1) { boolean zOK = (z >= 0.0) && (z <= 1.0); Region.Location expected = (xOK && yOK && zOK) ? Region.Location.INSIDE : Region.Location.OUTSIDE; Assert.assertEquals(expected, tree.checkPoint(new Vector3D(x, y, z))); } } } checkPoints(Region.Location.BOUNDARY, tree, new Vector3D[] { new Vector3D(0.0, 0.5, 0.5), new Vector3D(1.0, 0.5, 0.5), new Vector3D(0.5, 0.0, 0.5), new Vector3D(0.5, 1.0, 0.5), new Vector3D(0.5, 0.5, 0.0), new Vector3D(0.5, 0.5, 1.0) }); checkPoints(Region.Location.OUTSIDE, tree, new Vector3D[] { new Vector3D(0.0, 1.2, 1.2), new Vector3D(1.0, 1.2, 1.2), new Vector3D(1.2, 0.0, 1.2), new Vector3D(1.2, 1.0, 1.2), new Vector3D(1.2, 1.2, 0.0), new Vector3D(1.2, 1.2, 1.0) }); } @Test public void testTetrahedron() throws MathArithmeticException { Vector3D vertex1 = new Vector3D(1, 2, 3); Vector3D vertex2 = new Vector3D(2, 2, 4); Vector3D vertex3 = new Vector3D(2, 3, 3); Vector3D vertex4 = new Vector3D(1, 3, 4); @SuppressWarnings("unchecked") PolyhedronsSet tree = (PolyhedronsSet) new RegionFactory().buildConvex( new Plane(vertex3, vertex2, vertex1), new Plane(vertex2, vertex3, vertex4), new Plane(vertex4, vertex3, vertex1), new Plane(vertex1, vertex2, vertex4)); Assert.assertEquals(1.0 / 3.0, tree.getSize(), 1.0e-10); Assert.assertEquals(2.0 * FastMath.sqrt(3.0), tree.getBoundarySize(), 1.0e-10); Vector3D barycenter = (Vector3D) tree.getBarycenter(); Assert.assertEquals(1.5, barycenter.getX(), 1.0e-10); Assert.assertEquals(2.5, barycenter.getY(), 1.0e-10); Assert.assertEquals(3.5, barycenter.getZ(), 1.0e-10); double third = 1.0 / 3.0; checkPoints(Region.Location.BOUNDARY, tree, new Vector3D[] { vertex1, vertex2, vertex3, vertex4, new Vector3D(third, vertex1, third, vertex2, third, vertex3), new Vector3D(third, vertex2, third, vertex3, third, vertex4), new Vector3D(third, vertex3, third, vertex4, third, vertex1), new Vector3D(third, vertex4, third, vertex1, third, vertex2) }); checkPoints(Region.Location.OUTSIDE, tree, new Vector3D[] { new Vector3D(1, 2, 4), new Vector3D(2, 2, 3), new Vector3D(2, 3, 4), new Vector3D(1, 3, 3) }); } @Test public void testIsometry() throws MathArithmeticException, MathIllegalArgumentException { Vector3D vertex1 = new Vector3D(1.1, 2.2, 3.3); Vector3D vertex2 = new Vector3D(2.0, 2.4, 4.2); Vector3D vertex3 = new Vector3D(2.8, 3.3, 3.7); Vector3D vertex4 = new Vector3D(1.0, 3.6, 4.5); @SuppressWarnings("unchecked") PolyhedronsSet tree = (PolyhedronsSet) new RegionFactory().buildConvex( new Plane(vertex3, vertex2, vertex1), new Plane(vertex2, vertex3, vertex4), new Plane(vertex4, vertex3, vertex1), new Plane(vertex1, vertex2, vertex4)); Vector3D barycenter = (Vector3D) tree.getBarycenter(); Vector3D s = new Vector3D(10.2, 4.3, -6.7); Vector3D c = new Vector3D(-0.2, 2.1, -3.2); Rotation r = new Rotation(new Vector3D(6.2, -4.4, 2.1), 0.12); tree = tree.rotate(c, r).translate(s); Vector3D newB = new Vector3D(1.0, s, 1.0, c, 1.0, r.applyTo(barycenter.subtract(c))); Assert.assertEquals(0.0, newB.subtract(tree.getBarycenter()).getNorm(), 1.0e-10); final Vector3D[] expectedV = new Vector3D[] { new Vector3D(1.0, s, 1.0, c, 1.0, r.applyTo(vertex1.subtract(c))), new Vector3D(1.0, s, 1.0, c, 1.0, r.applyTo(vertex2.subtract(c))), new Vector3D(1.0, s, 1.0, c, 1.0, r.applyTo(vertex3.subtract(c))), new Vector3D(1.0, s, 1.0, c, 1.0, r.applyTo(vertex4.subtract(c))) }; tree.getTree(true).visit(new BSPTreeVisitor() { public Order visitOrder(BSPTree node) { return Order.MINUS_SUB_PLUS; } public void visitInternalNode(BSPTree node) { @SuppressWarnings("unchecked") BoundaryAttribute attribute = (BoundaryAttribute) node.getAttribute(); if (attribute.getPlusOutside() != null) { checkFacet((SubPlane) attribute.getPlusOutside()); } if (attribute.getPlusInside() != null) { checkFacet((SubPlane) attribute.getPlusInside()); } } public void visitLeafNode(BSPTree node) { } private void checkFacet(SubPlane facet) { Plane plane = (Plane) facet.getHyperplane(); Vector2D[][] vertices = ((PolygonsSet) facet.getRemainingRegion()).getVertices(); Assert.assertEquals(1, vertices.length); for (int i = 0; i < vertices[0].length; ++i) { Vector3D v = plane.toSpace(vertices[0][i]); double d = Double.POSITIVE_INFINITY; for (int k = 0; k < expectedV.length; ++k) { d = FastMath.min(d, v.subtract(expectedV[k]).getNorm()); } Assert.assertEquals(0, d, 1.0e-10); } } }); } @Test public void testBuildBox() { double x = 1.0; double y = 2.0; double z = 3.0; double w = 0.1; double l = 1.0; PolyhedronsSet tree = new PolyhedronsSet(x - l, x + l, y - w, y + w, z - w, z + w); Vector3D barycenter = (Vector3D) tree.getBarycenter(); Assert.assertEquals(x, barycenter.getX(), 1.0e-10); Assert.assertEquals(y, barycenter.getY(), 1.0e-10); Assert.assertEquals(z, barycenter.getZ(), 1.0e-10); Assert.assertEquals(8 * l * w * w, tree.getSize(), 1.0e-10); Assert.assertEquals(8 * w * (2 * l + w), tree.getBoundarySize(), 1.0e-10); } @Test public void testCross() { double x = 1.0; double y = 2.0; double z = 3.0; double w = 0.1; double l = 1.0; PolyhedronsSet xBeam = new PolyhedronsSet(x - l, x + l, y - w, y + w, z - w, z + w); PolyhedronsSet yBeam = new PolyhedronsSet(x - w, x + w, y - l, y + l, z - w, z + w); PolyhedronsSet zBeam = new PolyhedronsSet(x - w, x + w, y - w, y + w, z - l, z + l); RegionFactory factory = new RegionFactory(); PolyhedronsSet tree = (PolyhedronsSet) factory.union(xBeam, factory.union(yBeam, zBeam)); Vector3D barycenter = (Vector3D) tree.getBarycenter(); Assert.assertEquals(x, barycenter.getX(), 1.0e-10); Assert.assertEquals(y, barycenter.getY(), 1.0e-10); Assert.assertEquals(z, barycenter.getZ(), 1.0e-10); Assert.assertEquals(8 * w * w * (3 * l - 2 * w), tree.getSize(), 1.0e-10); Assert.assertEquals(24 * w * (2 * l - w), tree.getBoundarySize(), 1.0e-10); } @Test public void testIssue780() throws MathArithmeticException { float[] coords = { 1.000000f, -1.000000f, -1.000000f, 1.000000f, -1.000000f, 1.000000f, -1.000000f, -1.000000f, 1.000000f, -1.000000f, -1.000000f, -1.000000f, 1.000000f, 1.000000f, -1f, 0.999999f, 1.000000f, 1.000000f, // 1.000000f, 1.000000f, 1.000000f, -1.000000f, 1.000000f, 1.000000f, -1.000000f, 1.000000f, -1.000000f}; int[] indices = { 0, 1, 2, 0, 2, 3, 4, 7, 6, 4, 6, 5, 0, 4, 5, 0, 5, 1, 1, 5, 6, 1, 6, 2, 2, 6, 7, 2, 7, 3, 4, 0, 3, 4, 3, 7}; ArrayList> subHyperplaneList = new ArrayList>(); for (int idx = 0; idx < indices.length; idx += 3) { int idxA = indices[idx] * 3; int idxB = indices[idx + 1] * 3; int idxC = indices[idx + 2] * 3; Vector3D v_1 = new Vector3D(coords[idxA], coords[idxA + 1], coords[idxA + 2]); Vector3D v_2 = new Vector3D(coords[idxB], coords[idxB + 1], coords[idxB + 2]); Vector3D v_3 = new Vector3D(coords[idxC], coords[idxC + 1], coords[idxC + 2]); Vector3D[] vertices = {v_1, v_2, v_3}; Plane polyPlane = new Plane(v_1, v_2, v_3); ArrayList> lines = new ArrayList>(); Vector2D[] projPts = new Vector2D[vertices.length]; for (int ptIdx = 0; ptIdx < projPts.length; ptIdx++) { projPts[ptIdx] = polyPlane.toSubSpace(vertices[ptIdx]); } SubLine lineInPlane = null; for (int ptIdx = 0; ptIdx < projPts.length; ptIdx++) { lineInPlane = new SubLine(projPts[ptIdx], projPts[(ptIdx + 1) % projPts.length]); lines.add(lineInPlane); } Region polyRegion = new PolygonsSet(lines); SubPlane polygon = new SubPlane(polyPlane, polyRegion); subHyperplaneList.add(polygon); } PolyhedronsSet polyhedronsSet = new PolyhedronsSet(subHyperplaneList); Assert.assertEquals( 8.0, polyhedronsSet.getSize(), 3.0e-6); Assert.assertEquals(24.0, polyhedronsSet.getBoundarySize(), 5.0e-6); } private void checkPoints(Region.Location expected, PolyhedronsSet tree, Vector3D[] points) { for (int i = 0; i < points.length; ++i) { Assert.assertEquals(expected, tree.checkPoint(points[i])); } } } ././@LongLink100644 0 0 151 12126630646 10256 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/geometry/euclidean/threed/Vector3DTest.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/geometry/euclidean/threed/Vector3DTest.100644 1750 1750 43142 12126627671 32136 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.geometry.euclidean.threed; import java.text.DecimalFormat; import java.text.DecimalFormatSymbols; import java.text.NumberFormat; import java.util.Locale; import org.apache.commons.math3.TestUtils; import org.apache.commons.math3.exception.DimensionMismatchException; import org.apache.commons.math3.exception.MathArithmeticException; import org.apache.commons.math3.geometry.Space; import org.apache.commons.math3.random.Well1024a; import org.apache.commons.math3.util.FastMath; import org.apache.commons.math3.util.Precision; import org.junit.Assert; import org.junit.Test; public class Vector3DTest { @Test public void testConstructors() throws DimensionMismatchException { double r = FastMath.sqrt(2) /2; checkVector(new Vector3D(2, new Vector3D(FastMath.PI / 3, -FastMath.PI / 4)), r, r * FastMath.sqrt(3), -2 * r); checkVector(new Vector3D(2, Vector3D.PLUS_I, -3, Vector3D.MINUS_K), 2, 0, 3); checkVector(new Vector3D(2, Vector3D.PLUS_I, 5, Vector3D.PLUS_J, -3, Vector3D.MINUS_K), 2, 5, 3); checkVector(new Vector3D(2, Vector3D.PLUS_I, 5, Vector3D.PLUS_J, 5, Vector3D.MINUS_J, -3, Vector3D.MINUS_K), 2, 0, 3); checkVector(new Vector3D(new double[] { 2, 5, -3 }), 2, 5, -3); } @Test public void testSpace() { Space space = new Vector3D(1, 2, 2).getSpace(); Assert.assertEquals(3, space.getDimension()); Assert.assertEquals(2, space.getSubSpace().getDimension()); Space deserialized = (Space) TestUtils.serializeAndRecover(space); Assert.assertTrue(space == deserialized); } @Test public void testZero() { Assert.assertEquals(0, new Vector3D(1, 2, 2).getZero().getNorm(), 1.0e-15); } @Test public void testEquals() { Vector3D u1 = new Vector3D(1, 2, 3); Vector3D u2 = new Vector3D(1, 2, 3); Assert.assertTrue(u1.equals(u1)); Assert.assertTrue(u1.equals(u2)); Assert.assertFalse(u1.equals(new Rotation(1, 0, 0, 0, false))); Assert.assertFalse(u1.equals(new Vector3D(1, 2, 3 + 10 * Precision.EPSILON))); Assert.assertFalse(u1.equals(new Vector3D(1, 2 + 10 * Precision.EPSILON, 3))); Assert.assertFalse(u1.equals(new Vector3D(1 + 10 * Precision.EPSILON, 2, 3))); Assert.assertTrue(new Vector3D(0, Double.NaN, 0).equals(new Vector3D(0, 0, Double.NaN))); } @Test public void testHash() { Assert.assertEquals(new Vector3D(0, Double.NaN, 0).hashCode(), new Vector3D(0, 0, Double.NaN).hashCode()); Vector3D u = new Vector3D(1, 2, 3); Vector3D v = new Vector3D(1, 2, 3 + 10 * Precision.EPSILON); Assert.assertTrue(u.hashCode() != v.hashCode()); } @Test public void testInfinite() { Assert.assertTrue(new Vector3D(1, 1, Double.NEGATIVE_INFINITY).isInfinite()); Assert.assertTrue(new Vector3D(1, Double.NEGATIVE_INFINITY, 1).isInfinite()); Assert.assertTrue(new Vector3D(Double.NEGATIVE_INFINITY, 1, 1).isInfinite()); Assert.assertFalse(new Vector3D(1, 1, 2).isInfinite()); Assert.assertFalse(new Vector3D(1, Double.NaN, Double.NEGATIVE_INFINITY).isInfinite()); } @Test public void testNaN() { Assert.assertTrue(new Vector3D(1, 1, Double.NaN).isNaN()); Assert.assertTrue(new Vector3D(1, Double.NaN, 1).isNaN()); Assert.assertTrue(new Vector3D(Double.NaN, 1, 1).isNaN()); Assert.assertFalse(new Vector3D(1, 1, 2).isNaN()); Assert.assertFalse(new Vector3D(1, 1, Double.NEGATIVE_INFINITY).isNaN()); } @Test public void testToString() { Assert.assertEquals("{3; 2; 1}", new Vector3D(3, 2, 1).toString()); NumberFormat format = new DecimalFormat("0.000", new DecimalFormatSymbols(Locale.US)); Assert.assertEquals("{3.000; 2.000; 1.000}", new Vector3D(3, 2, 1).toString(format)); } @Test(expected=DimensionMismatchException.class) public void testWrongDimension() throws DimensionMismatchException { new Vector3D(new double[] { 2, 5 }); } @Test public void testCoordinates() { Vector3D v = new Vector3D(1, 2, 3); Assert.assertTrue(FastMath.abs(v.getX() - 1) < 1.0e-12); Assert.assertTrue(FastMath.abs(v.getY() - 2) < 1.0e-12); Assert.assertTrue(FastMath.abs(v.getZ() - 3) < 1.0e-12); double[] coordinates = v.toArray(); Assert.assertTrue(FastMath.abs(coordinates[0] - 1) < 1.0e-12); Assert.assertTrue(FastMath.abs(coordinates[1] - 2) < 1.0e-12); Assert.assertTrue(FastMath.abs(coordinates[2] - 3) < 1.0e-12); } @Test public void testNorm1() { Assert.assertEquals(0.0, Vector3D.ZERO.getNorm1(), 0); Assert.assertEquals(6.0, new Vector3D(1, -2, 3).getNorm1(), 0); } @Test public void testNorm() { Assert.assertEquals(0.0, Vector3D.ZERO.getNorm(), 0); Assert.assertEquals(FastMath.sqrt(14), new Vector3D(1, 2, 3).getNorm(), 1.0e-12); } @Test public void testNormSq() { Assert.assertEquals(0.0, new Vector3D(0, 0, 0).getNormSq(), 0); Assert.assertEquals(14, new Vector3D(1, 2, 3).getNormSq(), 1.0e-12); } @Test public void testNormInf() { Assert.assertEquals(0.0, Vector3D.ZERO.getNormInf(), 0); Assert.assertEquals(3.0, new Vector3D(1, -2, 3).getNormInf(), 0); } @Test public void testDistance1() { Vector3D v1 = new Vector3D(1, -2, 3); Vector3D v2 = new Vector3D(-4, 2, 0); Assert.assertEquals(0.0, Vector3D.distance1(Vector3D.MINUS_I, Vector3D.MINUS_I), 0); Assert.assertEquals(12.0, Vector3D.distance1(v1, v2), 1.0e-12); Assert.assertEquals(v1.subtract(v2).getNorm1(), Vector3D.distance1(v1, v2), 1.0e-12); } @Test public void testDistance() { Vector3D v1 = new Vector3D(1, -2, 3); Vector3D v2 = new Vector3D(-4, 2, 0); Assert.assertEquals(0.0, Vector3D.distance(Vector3D.MINUS_I, Vector3D.MINUS_I), 0); Assert.assertEquals(FastMath.sqrt(50), Vector3D.distance(v1, v2), 1.0e-12); Assert.assertEquals(v1.subtract(v2).getNorm(), Vector3D.distance(v1, v2), 1.0e-12); } @Test public void testDistanceSq() { Vector3D v1 = new Vector3D(1, -2, 3); Vector3D v2 = new Vector3D(-4, 2, 0); Assert.assertEquals(0.0, Vector3D.distanceSq(Vector3D.MINUS_I, Vector3D.MINUS_I), 0); Assert.assertEquals(50.0, Vector3D.distanceSq(v1, v2), 1.0e-12); Assert.assertEquals(Vector3D.distance(v1, v2) * Vector3D.distance(v1, v2), Vector3D.distanceSq(v1, v2), 1.0e-12); } @Test public void testDistanceInf() { Vector3D v1 = new Vector3D(1, -2, 3); Vector3D v2 = new Vector3D(-4, 2, 0); Assert.assertEquals(0.0, Vector3D.distanceInf(Vector3D.MINUS_I, Vector3D.MINUS_I), 0); Assert.assertEquals(5.0, Vector3D.distanceInf(v1, v2), 1.0e-12); Assert.assertEquals(v1.subtract(v2).getNormInf(), Vector3D.distanceInf(v1, v2), 1.0e-12); } @Test public void testSubtract() { Vector3D v1 = new Vector3D(1, 2, 3); Vector3D v2 = new Vector3D(-3, -2, -1); v1 = v1.subtract(v2); checkVector(v1, 4, 4, 4); checkVector(v2.subtract(v1), -7, -6, -5); checkVector(v2.subtract(3, v1), -15, -14, -13); } @Test public void testAdd() { Vector3D v1 = new Vector3D(1, 2, 3); Vector3D v2 = new Vector3D(-3, -2, -1); v1 = v1.add(v2); checkVector(v1, -2, 0, 2); checkVector(v2.add(v1), -5, -2, 1); checkVector(v2.add(3, v1), -9, -2, 5); } @Test public void testScalarProduct() { Vector3D v = new Vector3D(1, 2, 3); v = v.scalarMultiply(3); checkVector(v, 3, 6, 9); checkVector(v.scalarMultiply(0.5), 1.5, 3, 4.5); } @Test public void testVectorialProducts() { Vector3D v1 = new Vector3D(2, 1, -4); Vector3D v2 = new Vector3D(3, 1, -1); Assert.assertTrue(FastMath.abs(Vector3D.dotProduct(v1, v2) - 11) < 1.0e-12); Vector3D v3 = Vector3D.crossProduct(v1, v2); checkVector(v3, 3, -10, -1); Assert.assertTrue(FastMath.abs(Vector3D.dotProduct(v1, v3)) < 1.0e-12); Assert.assertTrue(FastMath.abs(Vector3D.dotProduct(v2, v3)) < 1.0e-12); } @Test public void testCrossProductCancellation() { Vector3D v1 = new Vector3D(9070467121.0, 4535233560.0, 1); Vector3D v2 = new Vector3D(9070467123.0, 4535233561.0, 1); checkVector(Vector3D.crossProduct(v1, v2), -1, 2, 1); double scale = FastMath.scalb(1.0, 100); Vector3D big1 = new Vector3D(scale, v1); Vector3D small2 = new Vector3D(1 / scale, v2); checkVector(Vector3D.crossProduct(big1, small2), -1, 2, 1); } @Test public void testAngular() { Assert.assertEquals(0, Vector3D.PLUS_I.getAlpha(), 1.0e-10); Assert.assertEquals(0, Vector3D.PLUS_I.getDelta(), 1.0e-10); Assert.assertEquals(FastMath.PI / 2, Vector3D.PLUS_J.getAlpha(), 1.0e-10); Assert.assertEquals(0, Vector3D.PLUS_J.getDelta(), 1.0e-10); Assert.assertEquals(0, Vector3D.PLUS_K.getAlpha(), 1.0e-10); Assert.assertEquals(FastMath.PI / 2, Vector3D.PLUS_K.getDelta(), 1.0e-10); Vector3D u = new Vector3D(-1, 1, -1); Assert.assertEquals(3 * FastMath.PI /4, u.getAlpha(), 1.0e-10); Assert.assertEquals(-1.0 / FastMath.sqrt(3), FastMath.sin(u.getDelta()), 1.0e-10); } @Test public void testAngularSeparation() throws MathArithmeticException { Vector3D v1 = new Vector3D(2, -1, 4); Vector3D k = v1.normalize(); Vector3D i = k.orthogonal(); Vector3D v2 = k.scalarMultiply(FastMath.cos(1.2)).add(i.scalarMultiply(FastMath.sin(1.2))); Assert.assertTrue(FastMath.abs(Vector3D.angle(v1, v2) - 1.2) < 1.0e-12); } @Test public void testNormalize() throws MathArithmeticException { Assert.assertEquals(1.0, new Vector3D(5, -4, 2).normalize().getNorm(), 1.0e-12); try { Vector3D.ZERO.normalize(); Assert.fail("an exception should have been thrown"); } catch (MathArithmeticException ae) { // expected behavior } } @Test public void testNegate() { checkVector(new Vector3D(0.1, 2.5, 1.3).negate(), -0.1, -2.5, -1.3); } @Test public void testOrthogonal() throws MathArithmeticException { Vector3D v1 = new Vector3D(0.1, 2.5, 1.3); Assert.assertEquals(0.0, Vector3D.dotProduct(v1, v1.orthogonal()), 1.0e-12); Vector3D v2 = new Vector3D(2.3, -0.003, 7.6); Assert.assertEquals(0.0, Vector3D.dotProduct(v2, v2.orthogonal()), 1.0e-12); Vector3D v3 = new Vector3D(-1.7, 1.4, 0.2); Assert.assertEquals(0.0, Vector3D.dotProduct(v3, v3.orthogonal()), 1.0e-12); Vector3D v4 = new Vector3D(4.2, 0.1, -1.8); Assert.assertEquals(0.0, Vector3D.dotProduct(v4, v4.orthogonal()), 1.0e-12); try { new Vector3D(0, 0, 0).orthogonal(); Assert.fail("an exception should have been thrown"); } catch (MathArithmeticException ae) { // expected behavior } } @Test public void testAngle() throws MathArithmeticException { Assert.assertEquals(0.22572612855273393616, Vector3D.angle(new Vector3D(1, 2, 3), new Vector3D(4, 5, 6)), 1.0e-12); Assert.assertEquals(7.98595620686106654517199e-8, Vector3D.angle(new Vector3D(1, 2, 3), new Vector3D(2, 4, 6.000001)), 1.0e-12); Assert.assertEquals(3.14159257373023116985197793156, Vector3D.angle(new Vector3D(1, 2, 3), new Vector3D(-2, -4, -6.000001)), 1.0e-12); try { Vector3D.angle(Vector3D.ZERO, Vector3D.PLUS_I); Assert.fail("an exception should have been thrown"); } catch (MathArithmeticException ae) { // expected behavior } } @Test public void testAccurateDotProduct() { // the following two vectors are nearly but not exactly orthogonal // naive dot product (i.e. computing u1.x * u2.x + u1.y * u2.y + u1.z * u2.z // leads to a result of 0.0, instead of the correct -1.855129... Vector3D u1 = new Vector3D(-1321008684645961.0 / 268435456.0, -5774608829631843.0 / 268435456.0, -7645843051051357.0 / 8589934592.0); Vector3D u2 = new Vector3D(-5712344449280879.0 / 2097152.0, -4550117129121957.0 / 2097152.0, 8846951984510141.0 / 131072.0); double sNaive = u1.getX() * u2.getX() + u1.getY() * u2.getY() + u1.getZ() * u2.getZ(); double sAccurate = u1.dotProduct(u2); Assert.assertEquals(0.0, sNaive, 1.0e-30); Assert.assertEquals(-2088690039198397.0 / 1125899906842624.0, sAccurate, 1.0e-16); } @Test public void testDotProduct() { // we compare accurate versus naive dot product implementations // on regular vectors (i.e. not extreme cases like in the previous test) Well1024a random = new Well1024a(553267312521321234l); for (int i = 0; i < 10000; ++i) { double ux = 10000 * random.nextDouble(); double uy = 10000 * random.nextDouble(); double uz = 10000 * random.nextDouble(); double vx = 10000 * random.nextDouble(); double vy = 10000 * random.nextDouble(); double vz = 10000 * random.nextDouble(); double sNaive = ux * vx + uy * vy + uz * vz; double sAccurate = new Vector3D(ux, uy, uz).dotProduct(new Vector3D(vx, vy, vz)); Assert.assertEquals(sNaive, sAccurate, 2.5e-16 * sAccurate); } } @Test public void testAccurateCrossProduct() { // the vectors u1 and u2 are nearly but not exactly anti-parallel // (7.31e-16 degrees from 180 degrees) naive cross product (i.e. // computing u1.x * u2.x + u1.y * u2.y + u1.z * u2.z // leads to a result of [0.0009765, -0.0001220, -0.0039062], // instead of the correct [0.0006913, -0.0001254, -0.0007909] final Vector3D u1 = new Vector3D(-1321008684645961.0 / 268435456.0, -5774608829631843.0 / 268435456.0, -7645843051051357.0 / 8589934592.0); final Vector3D u2 = new Vector3D( 1796571811118507.0 / 2147483648.0, 7853468008299307.0 / 2147483648.0, 2599586637357461.0 / 17179869184.0); final Vector3D u3 = new Vector3D(12753243807587107.0 / 18446744073709551616.0, -2313766922703915.0 / 18446744073709551616.0, -227970081415313.0 / 288230376151711744.0); Vector3D cNaive = new Vector3D(u1.getY() * u2.getZ() - u1.getZ() * u2.getY(), u1.getZ() * u2.getX() - u1.getX() * u2.getZ(), u1.getX() * u2.getY() - u1.getY() * u2.getX()); Vector3D cAccurate = u1.crossProduct(u2); Assert.assertTrue(u3.distance(cNaive) > 2.9 * u3.getNorm()); Assert.assertEquals(0.0, u3.distance(cAccurate), 1.0e-30 * cAccurate.getNorm()); } @Test public void testCrossProduct() { // we compare accurate versus naive cross product implementations // on regular vectors (i.e. not extreme cases like in the previous test) Well1024a random = new Well1024a(885362227452043214l); for (int i = 0; i < 10000; ++i) { double ux = 10000 * random.nextDouble(); double uy = 10000 * random.nextDouble(); double uz = 10000 * random.nextDouble(); double vx = 10000 * random.nextDouble(); double vy = 10000 * random.nextDouble(); double vz = 10000 * random.nextDouble(); Vector3D cNaive = new Vector3D(uy * vz - uz * vy, uz * vx - ux * vz, ux * vy - uy * vx); Vector3D cAccurate = new Vector3D(ux, uy, uz).crossProduct(new Vector3D(vx, vy, vz)); Assert.assertEquals(0.0, cAccurate.distance(cNaive), 6.0e-15 * cAccurate.getNorm()); } } private void checkVector(Vector3D v, double x, double y, double z) { Assert.assertEquals(x, v.getX(), 1.0e-12); Assert.assertEquals(y, v.getY(), 1.0e-12); Assert.assertEquals(z, v.getZ(), 1.0e-12); } } ././@LongLink100644 0 0 146 12126630646 10262 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/geometry/euclidean/threed/PlaneTest.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/geometry/euclidean/threed/PlaneTest.jav100644 1750 1750 16165 12126627671 32252 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.geometry.euclidean.threed; import org.apache.commons.math3.exception.MathArithmeticException; import org.apache.commons.math3.exception.MathIllegalArgumentException; import org.apache.commons.math3.geometry.euclidean.threed.Line; import org.apache.commons.math3.geometry.euclidean.threed.Plane; import org.apache.commons.math3.geometry.euclidean.threed.Rotation; import org.apache.commons.math3.geometry.euclidean.threed.Vector3D; import org.junit.Assert; import org.junit.Test; public class PlaneTest { @Test public void testContains() throws MathArithmeticException { Plane p = new Plane(new Vector3D(0, 0, 1), new Vector3D(0, 0, 1)); Assert.assertTrue(p.contains(new Vector3D(0, 0, 1))); Assert.assertTrue(p.contains(new Vector3D(17, -32, 1))); Assert.assertTrue(! p.contains(new Vector3D(17, -32, 1.001))); } @Test public void testOffset() throws MathArithmeticException { Vector3D p1 = new Vector3D(1, 1, 1); Plane p = new Plane(p1, new Vector3D(0.2, 0, 0)); Assert.assertEquals(-5.0, p.getOffset(new Vector3D(-4, 0, 0)), 1.0e-10); Assert.assertEquals(+5.0, p.getOffset(new Vector3D(6, 10, -12)), 1.0e-10); Assert.assertEquals(0.3, p.getOffset(new Vector3D(1.0, p1, 0.3, p.getNormal())), 1.0e-10); Assert.assertEquals(-0.3, p.getOffset(new Vector3D(1.0, p1, -0.3, p.getNormal())), 1.0e-10); } @Test public void testPoint() throws MathArithmeticException { Plane p = new Plane(new Vector3D(2, -3, 1), new Vector3D(1, 4, 9)); Assert.assertTrue(p.contains(p.getOrigin())); } @Test public void testThreePoints() throws MathArithmeticException { Vector3D p1 = new Vector3D(1.2, 3.4, -5.8); Vector3D p2 = new Vector3D(3.4, -5.8, 1.2); Vector3D p3 = new Vector3D(-2.0, 4.3, 0.7); Plane p = new Plane(p1, p2, p3); Assert.assertTrue(p.contains(p1)); Assert.assertTrue(p.contains(p2)); Assert.assertTrue(p.contains(p3)); } @Test public void testRotate() throws MathArithmeticException, MathIllegalArgumentException { Vector3D p1 = new Vector3D(1.2, 3.4, -5.8); Vector3D p2 = new Vector3D(3.4, -5.8, 1.2); Vector3D p3 = new Vector3D(-2.0, 4.3, 0.7); Plane p = new Plane(p1, p2, p3); Vector3D oldNormal = p.getNormal(); p = p.rotate(p2, new Rotation(p2.subtract(p1), 1.7)); Assert.assertTrue(p.contains(p1)); Assert.assertTrue(p.contains(p2)); Assert.assertTrue(! p.contains(p3)); p = p.rotate(p2, new Rotation(oldNormal, 0.1)); Assert.assertTrue(! p.contains(p1)); Assert.assertTrue(p.contains(p2)); Assert.assertTrue(! p.contains(p3)); p = p.rotate(p1, new Rotation(oldNormal, 0.1)); Assert.assertTrue(! p.contains(p1)); Assert.assertTrue(! p.contains(p2)); Assert.assertTrue(! p.contains(p3)); } @Test public void testTranslate() throws MathArithmeticException { Vector3D p1 = new Vector3D(1.2, 3.4, -5.8); Vector3D p2 = new Vector3D(3.4, -5.8, 1.2); Vector3D p3 = new Vector3D(-2.0, 4.3, 0.7); Plane p = new Plane(p1, p2, p3); p = p.translate(new Vector3D(2.0, p.getU(), -1.5, p.getV())); Assert.assertTrue(p.contains(p1)); Assert.assertTrue(p.contains(p2)); Assert.assertTrue(p.contains(p3)); p = p.translate(new Vector3D(-1.2, p.getNormal())); Assert.assertTrue(! p.contains(p1)); Assert.assertTrue(! p.contains(p2)); Assert.assertTrue(! p.contains(p3)); p = p.translate(new Vector3D(+1.2, p.getNormal())); Assert.assertTrue(p.contains(p1)); Assert.assertTrue(p.contains(p2)); Assert.assertTrue(p.contains(p3)); } @Test public void testIntersection() throws MathArithmeticException, MathIllegalArgumentException { Plane p = new Plane(new Vector3D(1, 2, 3), new Vector3D(-4, 1, -5)); Line l = new Line(new Vector3D(0.2, -3.5, 0.7), new Vector3D(1.2, -2.5, -0.3)); Vector3D point = p.intersection(l); Assert.assertTrue(p.contains(point)); Assert.assertTrue(l.contains(point)); Assert.assertNull(p.intersection(new Line(new Vector3D(10, 10, 10), new Vector3D(10, 10, 10).add(p.getNormal().orthogonal())))); } @Test public void testIntersection2() throws MathArithmeticException { Vector3D p1 = new Vector3D (1.2, 3.4, -5.8); Vector3D p2 = new Vector3D (3.4, -5.8, 1.2); Plane pA = new Plane(p1, p2, new Vector3D (-2.0, 4.3, 0.7)); Plane pB = new Plane(p1, new Vector3D (11.4, -3.8, 5.1), p2); Line l = pA.intersection(pB); Assert.assertTrue(l.contains(p1)); Assert.assertTrue(l.contains(p2)); Assert.assertNull(pA.intersection(pA)); } @Test public void testIntersection3() throws MathArithmeticException { Vector3D reference = new Vector3D (1.2, 3.4, -5.8); Plane p1 = new Plane(reference, new Vector3D(1, 3, 3)); Plane p2 = new Plane(reference, new Vector3D(-2, 4, 0)); Plane p3 = new Plane(reference, new Vector3D(7, 0, -4)); Vector3D p = Plane.intersection(p1, p2, p3); Assert.assertEquals(reference.getX(), p.getX(), 1.0e-10); Assert.assertEquals(reference.getY(), p.getY(), 1.0e-10); Assert.assertEquals(reference.getZ(), p.getZ(), 1.0e-10); } @Test public void testSimilar() throws MathArithmeticException { Vector3D p1 = new Vector3D (1.2, 3.4, -5.8); Vector3D p2 = new Vector3D (3.4, -5.8, 1.2); Vector3D p3 = new Vector3D (-2.0, 4.3, 0.7); Plane pA = new Plane(p1, p2, p3); Plane pB = new Plane(p1, new Vector3D (11.4, -3.8, 5.1), p2); Assert.assertTrue(! pA.isSimilarTo(pB)); Assert.assertTrue(pA.isSimilarTo(pA)); Assert.assertTrue(pA.isSimilarTo(new Plane(p1, p3, p2))); Vector3D shift = new Vector3D(0.3, pA.getNormal()); Assert.assertTrue(! pA.isSimilarTo(new Plane(p1.add(shift), p3.add(shift), p2.add(shift)))); } } ././@LongLink100644 0 0 167 12126630646 10265 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/geometry/euclidean/threed/Vector3DFormatAbstractTest.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/geometry/euclidean/threed/Vector3DForma100644 1750 1750 26776 12126627671 32223 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.geometry.euclidean.threed; import java.text.NumberFormat; import java.text.ParsePosition; import java.util.Locale; import org.apache.commons.math3.exception.MathParseException; import org.apache.commons.math3.geometry.euclidean.threed.Vector3D; import org.apache.commons.math3.geometry.euclidean.threed.Vector3DFormat; import org.junit.Test; import org.junit.Assert; public abstract class Vector3DFormatAbstractTest { Vector3DFormat vector3DFormat = null; Vector3DFormat vector3DFormatSquare = null; protected abstract Locale getLocale(); protected abstract char getDecimalCharacter(); protected Vector3DFormatAbstractTest() { vector3DFormat = Vector3DFormat.getInstance(getLocale()); final NumberFormat nf = NumberFormat.getInstance(getLocale()); nf.setMaximumFractionDigits(2); vector3DFormatSquare = new Vector3DFormat("[", "]", " : ", nf); } @Test public void testSimpleNoDecimals() { Vector3D c = new Vector3D(1, 1, 1); String expected = "{1; 1; 1}"; String actual = vector3DFormat.format(c); Assert.assertEquals(expected, actual); } @Test public void testSimpleWithDecimals() { Vector3D c = new Vector3D(1.23, 1.43, 1.63); String expected = "{1" + getDecimalCharacter() + "23; 1" + getDecimalCharacter() + "43; 1" + getDecimalCharacter() + "63}"; String actual = vector3DFormat.format(c); Assert.assertEquals(expected, actual); } @Test public void testSimpleWithDecimalsTrunc() { Vector3D c = new Vector3D(1.232323232323, 1.434343434343, 1.633333333333); String expected = "{1" + getDecimalCharacter() + "2323232323; 1" + getDecimalCharacter() + "4343434343; 1" + getDecimalCharacter() + "6333333333}"; String actual = vector3DFormat.format(c); Assert.assertEquals(expected, actual); } @Test public void testNegativeX() { Vector3D c = new Vector3D(-1.232323232323, 1.43, 1.63); String expected = "{-1" + getDecimalCharacter() + "2323232323; 1" + getDecimalCharacter() + "43; 1" + getDecimalCharacter() + "63}"; String actual = vector3DFormat.format(c); Assert.assertEquals(expected, actual); } @Test public void testNegativeY() { Vector3D c = new Vector3D(1.23, -1.434343434343, 1.63); String expected = "{1" + getDecimalCharacter() + "23; -1" + getDecimalCharacter() + "4343434343; 1" + getDecimalCharacter() + "63}"; String actual = vector3DFormat.format(c); Assert.assertEquals(expected, actual); } @Test public void testNegativeZ() { Vector3D c = new Vector3D(1.23, 1.43, -1.633333333333); String expected = "{1" + getDecimalCharacter() + "23; 1" + getDecimalCharacter() + "43; -1" + getDecimalCharacter() + "6333333333}"; String actual = vector3DFormat.format(c); Assert.assertEquals(expected, actual); } @Test public void testNonDefaultSetting() { Vector3D c = new Vector3D(1, 1, 1); String expected = "[1 : 1 : 1]"; String actual = vector3DFormatSquare.format(c); Assert.assertEquals(expected, actual); } @Test public void testDefaultFormatVector3D() { Locale defaultLocal = Locale.getDefault(); Locale.setDefault(getLocale()); Vector3D c = new Vector3D(232.22222222222, -342.3333333333, 432.44444444444); String expected = "{232" + getDecimalCharacter() + "2222222222; -342" + getDecimalCharacter() + "3333333333; 432" + getDecimalCharacter() + "4444444444}"; String actual = (new Vector3DFormat()).format(c); Assert.assertEquals(expected, actual); Locale.setDefault(defaultLocal); } @Test public void testNan() { Vector3D c = Vector3D.NaN; String expected = "{(NaN); (NaN); (NaN)}"; String actual = vector3DFormat.format(c); Assert.assertEquals(expected, actual); } @Test public void testPositiveInfinity() { Vector3D c = Vector3D.POSITIVE_INFINITY; String expected = "{(Infinity); (Infinity); (Infinity)}"; String actual = vector3DFormat.format(c); Assert.assertEquals(expected, actual); } @Test public void tesNegativeInfinity() { Vector3D c = Vector3D.NEGATIVE_INFINITY; String expected = "{(-Infinity); (-Infinity); (-Infinity)}"; String actual = vector3DFormat.format(c); Assert.assertEquals(expected, actual); } @Test public void testParseSimpleNoDecimals() throws MathParseException { String source = "{1; 1; 1}"; Vector3D expected = new Vector3D(1, 1, 1); Vector3D actual = vector3DFormat.parse(source); Assert.assertEquals(expected, actual); } @Test public void testParseIgnoredWhitespace() { Vector3D expected = new Vector3D(1, 1, 1); ParsePosition pos1 = new ParsePosition(0); String source1 = "{1;1;1}"; Assert.assertEquals(expected, vector3DFormat.parse(source1, pos1)); Assert.assertEquals(source1.length(), pos1.getIndex()); ParsePosition pos2 = new ParsePosition(0); String source2 = " { 1 ; 1 ; 1 } "; Assert.assertEquals(expected, vector3DFormat.parse(source2, pos2)); Assert.assertEquals(source2.length() - 1, pos2.getIndex()); } @Test public void testParseSimpleWithDecimals() throws MathParseException { String source = "{1" + getDecimalCharacter() + "23; 1" + getDecimalCharacter() + "43; 1" + getDecimalCharacter() + "63}"; Vector3D expected = new Vector3D(1.23, 1.43, 1.63); Vector3D actual = vector3DFormat.parse(source); Assert.assertEquals(expected, actual); } @Test public void testParseSimpleWithDecimalsTrunc() throws MathParseException { String source = "{1" + getDecimalCharacter() + "2323; 1" + getDecimalCharacter() + "4343; 1" + getDecimalCharacter() + "6333}"; Vector3D expected = new Vector3D(1.2323, 1.4343, 1.6333); Vector3D actual = vector3DFormat.parse(source); Assert.assertEquals(expected, actual); } @Test public void testParseNegativeX() throws MathParseException { String source = "{-1" + getDecimalCharacter() + "2323; 1" + getDecimalCharacter() + "4343; 1" + getDecimalCharacter() + "6333}"; Vector3D expected = new Vector3D(-1.2323, 1.4343, 1.6333); Vector3D actual = vector3DFormat.parse(source); Assert.assertEquals(expected, actual); } @Test public void testParseNegativeY() throws MathParseException { String source = "{1" + getDecimalCharacter() + "2323; -1" + getDecimalCharacter() + "4343; 1" + getDecimalCharacter() + "6333}"; Vector3D expected = new Vector3D(1.2323, -1.4343, 1.6333); Vector3D actual = vector3DFormat.parse(source); Assert.assertEquals(expected, actual); } @Test public void testParseNegativeZ() throws MathParseException { String source = "{1" + getDecimalCharacter() + "2323; 1" + getDecimalCharacter() + "4343; -1" + getDecimalCharacter() + "6333}"; Vector3D expected = new Vector3D(1.2323, 1.4343, -1.6333); Vector3D actual = vector3DFormat.parse(source); Assert.assertEquals(expected, actual); } @Test public void testParseNegativeAll() throws MathParseException { String source = "{-1" + getDecimalCharacter() + "2323; -1" + getDecimalCharacter() + "4343; -1" + getDecimalCharacter() + "6333}"; Vector3D expected = new Vector3D(-1.2323, -1.4343, -1.6333); Vector3D actual = vector3DFormat.parse(source); Assert.assertEquals(expected, actual); } @Test public void testParseZeroX() throws MathParseException { String source = "{0" + getDecimalCharacter() + "0; -1" + getDecimalCharacter() + "4343; 1" + getDecimalCharacter() + "6333}"; Vector3D expected = new Vector3D(0.0, -1.4343, 1.6333); Vector3D actual = vector3DFormat.parse(source); Assert.assertEquals(expected, actual); } @Test public void testParseNonDefaultSetting() throws MathParseException { String source = "[1" + getDecimalCharacter() + "2323 : 1" + getDecimalCharacter() + "4343 : 1" + getDecimalCharacter() + "6333]"; Vector3D expected = new Vector3D(1.2323, 1.4343, 1.6333); Vector3D actual = vector3DFormatSquare.parse(source); Assert.assertEquals(expected, actual); } @Test public void testParseNan() throws MathParseException { String source = "{(NaN); (NaN); (NaN)}"; Vector3D actual = vector3DFormat.parse(source); Assert.assertEquals(Vector3D.NaN, actual); } @Test public void testParsePositiveInfinity() throws MathParseException { String source = "{(Infinity); (Infinity); (Infinity)}"; Vector3D actual = vector3DFormat.parse(source); Assert.assertEquals(Vector3D.POSITIVE_INFINITY, actual); } @Test public void testParseNegativeInfinity() throws MathParseException { String source = "{(-Infinity); (-Infinity); (-Infinity)}"; Vector3D actual = vector3DFormat.parse(source); Assert.assertEquals(Vector3D.NEGATIVE_INFINITY, actual); } @Test public void testConstructorSingleFormat() { NumberFormat nf = NumberFormat.getInstance(); Vector3DFormat cf = new Vector3DFormat(nf); Assert.assertNotNull(cf); Assert.assertEquals(nf, cf.getFormat()); } @Test public void testForgottenPrefix() { ParsePosition pos = new ParsePosition(0); Assert.assertNull(new Vector3DFormat().parse("1; 1; 1}", pos)); Assert.assertEquals(0, pos.getErrorIndex()); } @Test public void testForgottenSeparator() { ParsePosition pos = new ParsePosition(0); Assert.assertNull(new Vector3DFormat().parse("{1; 1 1}", pos)); Assert.assertEquals(6, pos.getErrorIndex()); } @Test public void testForgottenSuffix() { ParsePosition pos = new ParsePosition(0); Assert.assertNull(new Vector3DFormat().parse("{1; 1; 1 ", pos)); Assert.assertEquals(8, pos.getErrorIndex()); } } ././@LongLink100644 0 0 165 12126630646 10263 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/geometry/euclidean/threed/FrenchVector3DFormatTest.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/geometry/euclidean/threed/FrenchVector3100644 1750 1750 2163 12126627671 32220 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.geometry.euclidean.threed; import java.util.Locale; public class FrenchVector3DFormatTest extends Vector3DFormatAbstractTest { @Override protected char getDecimalCharacter() { return ','; } @Override protected Locale getLocale() { return Locale.FRENCH; } } ././@LongLink100644 0 0 151 12126630646 10256 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/geometry/euclidean/threed/RotationTest.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/geometry/euclidean/threed/RotationTest.100644 1750 1750 47735 12126627671 32320 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.geometry.euclidean.threed; import org.apache.commons.math3.exception.MathArithmeticException; import org.apache.commons.math3.exception.MathIllegalArgumentException; import org.apache.commons.math3.util.FastMath; import org.apache.commons.math3.util.MathUtils; import org.junit.Assert; import org.junit.Test; public class RotationTest { @Test public void testIdentity() { Rotation r = Rotation.IDENTITY; checkVector(r.applyTo(Vector3D.PLUS_I), Vector3D.PLUS_I); checkVector(r.applyTo(Vector3D.PLUS_J), Vector3D.PLUS_J); checkVector(r.applyTo(Vector3D.PLUS_K), Vector3D.PLUS_K); checkAngle(r.getAngle(), 0); r = new Rotation(-1, 0, 0, 0, false); checkVector(r.applyTo(Vector3D.PLUS_I), Vector3D.PLUS_I); checkVector(r.applyTo(Vector3D.PLUS_J), Vector3D.PLUS_J); checkVector(r.applyTo(Vector3D.PLUS_K), Vector3D.PLUS_K); checkAngle(r.getAngle(), 0); r = new Rotation(42, 0, 0, 0, true); checkVector(r.applyTo(Vector3D.PLUS_I), Vector3D.PLUS_I); checkVector(r.applyTo(Vector3D.PLUS_J), Vector3D.PLUS_J); checkVector(r.applyTo(Vector3D.PLUS_K), Vector3D.PLUS_K); checkAngle(r.getAngle(), 0); } @Test public void testAxisAngle() throws MathIllegalArgumentException { Rotation r = new Rotation(new Vector3D(10, 10, 10), 2 * FastMath.PI / 3); checkVector(r.applyTo(Vector3D.PLUS_I), Vector3D.PLUS_J); checkVector(r.applyTo(Vector3D.PLUS_J), Vector3D.PLUS_K); checkVector(r.applyTo(Vector3D.PLUS_K), Vector3D.PLUS_I); double s = 1 / FastMath.sqrt(3); checkVector(r.getAxis(), new Vector3D(s, s, s)); checkAngle(r.getAngle(), 2 * FastMath.PI / 3); try { new Rotation(new Vector3D(0, 0, 0), 2 * FastMath.PI / 3); Assert.fail("an exception should have been thrown"); } catch (MathIllegalArgumentException e) { } r = new Rotation(Vector3D.PLUS_K, 1.5 * FastMath.PI); checkVector(r.getAxis(), new Vector3D(0, 0, -1)); checkAngle(r.getAngle(), 0.5 * FastMath.PI); r = new Rotation(Vector3D.PLUS_J, FastMath.PI); checkVector(r.getAxis(), Vector3D.PLUS_J); checkAngle(r.getAngle(), FastMath.PI); checkVector(Rotation.IDENTITY.getAxis(), Vector3D.PLUS_I); } @Test public void testRevert() { Rotation r = new Rotation(0.001, 0.36, 0.48, 0.8, true); Rotation reverted = r.revert(); checkRotation(r.applyTo(reverted), 1, 0, 0, 0); checkRotation(reverted.applyTo(r), 1, 0, 0, 0); Assert.assertEquals(r.getAngle(), reverted.getAngle(), 1.0e-12); Assert.assertEquals(-1, Vector3D.dotProduct(r.getAxis(), reverted.getAxis()), 1.0e-12); } @Test public void testVectorOnePair() throws MathArithmeticException { Vector3D u = new Vector3D(3, 2, 1); Vector3D v = new Vector3D(-4, 2, 2); Rotation r = new Rotation(u, v); checkVector(r.applyTo(u.scalarMultiply(v.getNorm())), v.scalarMultiply(u.getNorm())); checkAngle(new Rotation(u, u.negate()).getAngle(), FastMath.PI); try { new Rotation(u, Vector3D.ZERO); Assert.fail("an exception should have been thrown"); } catch (MathArithmeticException e) { // expected behavior } } @Test public void testVectorTwoPairs() throws MathArithmeticException { Vector3D u1 = new Vector3D(3, 0, 0); Vector3D u2 = new Vector3D(0, 5, 0); Vector3D v1 = new Vector3D(0, 0, 2); Vector3D v2 = new Vector3D(-2, 0, 2); Rotation r = new Rotation(u1, u2, v1, v2); checkVector(r.applyTo(Vector3D.PLUS_I), Vector3D.PLUS_K); checkVector(r.applyTo(Vector3D.PLUS_J), Vector3D.MINUS_I); r = new Rotation(u1, u2, u1.negate(), u2.negate()); Vector3D axis = r.getAxis(); if (Vector3D.dotProduct(axis, Vector3D.PLUS_K) > 0) { checkVector(axis, Vector3D.PLUS_K); } else { checkVector(axis, Vector3D.MINUS_K); } checkAngle(r.getAngle(), FastMath.PI); double sqrt = FastMath.sqrt(2) / 2; r = new Rotation(Vector3D.PLUS_I, Vector3D.PLUS_J, new Vector3D(0.5, 0.5, sqrt), new Vector3D(0.5, 0.5, -sqrt)); checkRotation(r, sqrt, 0.5, 0.5, 0); r = new Rotation(u1, u2, u1, Vector3D.crossProduct(u1, u2)); checkRotation(r, sqrt, -sqrt, 0, 0); checkRotation(new Rotation(u1, u2, u1, u2), 1, 0, 0, 0); try { new Rotation(u1, u2, Vector3D.ZERO, v2); Assert.fail("an exception should have been thrown"); } catch (MathArithmeticException e) { // expected behavior } } @Test public void testMatrix() throws NotARotationMatrixException { try { new Rotation(new double[][] { { 0.0, 1.0, 0.0 }, { 1.0, 0.0, 0.0 } }, 1.0e-7); Assert.fail("Expecting NotARotationMatrixException"); } catch (NotARotationMatrixException nrme) { // expected behavior } try { new Rotation(new double[][] { { 0.445888, 0.797184, -0.407040 }, { 0.821760, -0.184320, 0.539200 }, { -0.354816, 0.574912, 0.737280 } }, 1.0e-7); Assert.fail("Expecting NotARotationMatrixException"); } catch (NotARotationMatrixException nrme) { // expected behavior } try { new Rotation(new double[][] { { 0.4, 0.8, -0.4 }, { -0.4, 0.6, 0.7 }, { 0.8, -0.2, 0.5 } }, 1.0e-15); Assert.fail("Expecting NotARotationMatrixException"); } catch (NotARotationMatrixException nrme) { // expected behavior } checkRotation(new Rotation(new double[][] { { 0.445888, 0.797184, -0.407040 }, { -0.354816, 0.574912, 0.737280 }, { 0.821760, -0.184320, 0.539200 } }, 1.0e-10), 0.8, 0.288, 0.384, 0.36); checkRotation(new Rotation(new double[][] { { 0.539200, 0.737280, 0.407040 }, { 0.184320, -0.574912, 0.797184 }, { 0.821760, -0.354816, -0.445888 } }, 1.0e-10), 0.36, 0.8, 0.288, 0.384); checkRotation(new Rotation(new double[][] { { -0.445888, 0.797184, -0.407040 }, { 0.354816, 0.574912, 0.737280 }, { 0.821760, 0.184320, -0.539200 } }, 1.0e-10), 0.384, 0.36, 0.8, 0.288); checkRotation(new Rotation(new double[][] { { -0.539200, 0.737280, 0.407040 }, { -0.184320, -0.574912, 0.797184 }, { 0.821760, 0.354816, 0.445888 } }, 1.0e-10), 0.288, 0.384, 0.36, 0.8); double[][] m1 = { { 0.0, 1.0, 0.0 }, { 0.0, 0.0, 1.0 }, { 1.0, 0.0, 0.0 } }; Rotation r = new Rotation(m1, 1.0e-7); checkVector(r.applyTo(Vector3D.PLUS_I), Vector3D.PLUS_K); checkVector(r.applyTo(Vector3D.PLUS_J), Vector3D.PLUS_I); checkVector(r.applyTo(Vector3D.PLUS_K), Vector3D.PLUS_J); double[][] m2 = { { 0.83203, -0.55012, -0.07139 }, { 0.48293, 0.78164, -0.39474 }, { 0.27296, 0.29396, 0.91602 } }; r = new Rotation(m2, 1.0e-12); double[][] m3 = r.getMatrix(); double d00 = m2[0][0] - m3[0][0]; double d01 = m2[0][1] - m3[0][1]; double d02 = m2[0][2] - m3[0][2]; double d10 = m2[1][0] - m3[1][0]; double d11 = m2[1][1] - m3[1][1]; double d12 = m2[1][2] - m3[1][2]; double d20 = m2[2][0] - m3[2][0]; double d21 = m2[2][1] - m3[2][1]; double d22 = m2[2][2] - m3[2][2]; Assert.assertTrue(FastMath.abs(d00) < 6.0e-6); Assert.assertTrue(FastMath.abs(d01) < 6.0e-6); Assert.assertTrue(FastMath.abs(d02) < 6.0e-6); Assert.assertTrue(FastMath.abs(d10) < 6.0e-6); Assert.assertTrue(FastMath.abs(d11) < 6.0e-6); Assert.assertTrue(FastMath.abs(d12) < 6.0e-6); Assert.assertTrue(FastMath.abs(d20) < 6.0e-6); Assert.assertTrue(FastMath.abs(d21) < 6.0e-6); Assert.assertTrue(FastMath.abs(d22) < 6.0e-6); Assert.assertTrue(FastMath.abs(d00) > 4.0e-7); Assert.assertTrue(FastMath.abs(d01) > 4.0e-7); Assert.assertTrue(FastMath.abs(d02) > 4.0e-7); Assert.assertTrue(FastMath.abs(d10) > 4.0e-7); Assert.assertTrue(FastMath.abs(d11) > 4.0e-7); Assert.assertTrue(FastMath.abs(d12) > 4.0e-7); Assert.assertTrue(FastMath.abs(d20) > 4.0e-7); Assert.assertTrue(FastMath.abs(d21) > 4.0e-7); Assert.assertTrue(FastMath.abs(d22) > 4.0e-7); for (int i = 0; i < 3; ++i) { for (int j = 0; j < 3; ++j) { double m3tm3 = m3[i][0] * m3[j][0] + m3[i][1] * m3[j][1] + m3[i][2] * m3[j][2]; if (i == j) { Assert.assertTrue(FastMath.abs(m3tm3 - 1.0) < 1.0e-10); } else { Assert.assertTrue(FastMath.abs(m3tm3) < 1.0e-10); } } } checkVector(r.applyTo(Vector3D.PLUS_I), new Vector3D(m3[0][0], m3[1][0], m3[2][0])); checkVector(r.applyTo(Vector3D.PLUS_J), new Vector3D(m3[0][1], m3[1][1], m3[2][1])); checkVector(r.applyTo(Vector3D.PLUS_K), new Vector3D(m3[0][2], m3[1][2], m3[2][2])); double[][] m4 = { { 1.0, 0.0, 0.0 }, { 0.0, -1.0, 0.0 }, { 0.0, 0.0, -1.0 } }; r = new Rotation(m4, 1.0e-7); checkAngle(r.getAngle(), FastMath.PI); try { double[][] m5 = { { 0.0, 0.0, 1.0 }, { 0.0, 1.0, 0.0 }, { 1.0, 0.0, 0.0 } }; r = new Rotation(m5, 1.0e-7); Assert.fail("got " + r + ", should have caught an exception"); } catch (NotARotationMatrixException e) { // expected } } @Test public void testAngles() throws CardanEulerSingularityException { RotationOrder[] CardanOrders = { RotationOrder.XYZ, RotationOrder.XZY, RotationOrder.YXZ, RotationOrder.YZX, RotationOrder.ZXY, RotationOrder.ZYX }; for (int i = 0; i < CardanOrders.length; ++i) { for (double alpha1 = 0.1; alpha1 < 6.2; alpha1 += 0.3) { for (double alpha2 = -1.55; alpha2 < 1.55; alpha2 += 0.3) { for (double alpha3 = 0.1; alpha3 < 6.2; alpha3 += 0.3) { Rotation r = new Rotation(CardanOrders[i], alpha1, alpha2, alpha3); double[] angles = r.getAngles(CardanOrders[i]); checkAngle(angles[0], alpha1); checkAngle(angles[1], alpha2); checkAngle(angles[2], alpha3); } } } } RotationOrder[] EulerOrders = { RotationOrder.XYX, RotationOrder.XZX, RotationOrder.YXY, RotationOrder.YZY, RotationOrder.ZXZ, RotationOrder.ZYZ }; for (int i = 0; i < EulerOrders.length; ++i) { for (double alpha1 = 0.1; alpha1 < 6.2; alpha1 += 0.3) { for (double alpha2 = 0.05; alpha2 < 3.1; alpha2 += 0.3) { for (double alpha3 = 0.1; alpha3 < 6.2; alpha3 += 0.3) { Rotation r = new Rotation(EulerOrders[i], alpha1, alpha2, alpha3); double[] angles = r.getAngles(EulerOrders[i]); checkAngle(angles[0], alpha1); checkAngle(angles[1], alpha2); checkAngle(angles[2], alpha3); } } } } } @Test public void testSingularities() { RotationOrder[] CardanOrders = { RotationOrder.XYZ, RotationOrder.XZY, RotationOrder.YXZ, RotationOrder.YZX, RotationOrder.ZXY, RotationOrder.ZYX }; double[] singularCardanAngle = { FastMath.PI / 2, -FastMath.PI / 2 }; for (int i = 0; i < CardanOrders.length; ++i) { for (int j = 0; j < singularCardanAngle.length; ++j) { Rotation r = new Rotation(CardanOrders[i], 0.1, singularCardanAngle[j], 0.3); try { r.getAngles(CardanOrders[i]); Assert.fail("an exception should have been caught"); } catch (CardanEulerSingularityException cese) { // expected behavior } } } RotationOrder[] EulerOrders = { RotationOrder.XYX, RotationOrder.XZX, RotationOrder.YXY, RotationOrder.YZY, RotationOrder.ZXZ, RotationOrder.ZYZ }; double[] singularEulerAngle = { 0, FastMath.PI }; for (int i = 0; i < EulerOrders.length; ++i) { for (int j = 0; j < singularEulerAngle.length; ++j) { Rotation r = new Rotation(EulerOrders[i], 0.1, singularEulerAngle[j], 0.3); try { r.getAngles(EulerOrders[i]); Assert.fail("an exception should have been caught"); } catch (CardanEulerSingularityException cese) { // expected behavior } } } } @Test public void testQuaternion() throws MathIllegalArgumentException { Rotation r1 = new Rotation(new Vector3D(2, -3, 5), 1.7); double n = 23.5; Rotation r2 = new Rotation(n * r1.getQ0(), n * r1.getQ1(), n * r1.getQ2(), n * r1.getQ3(), true); for (double x = -0.9; x < 0.9; x += 0.2) { for (double y = -0.9; y < 0.9; y += 0.2) { for (double z = -0.9; z < 0.9; z += 0.2) { Vector3D u = new Vector3D(x, y, z); checkVector(r2.applyTo(u), r1.applyTo(u)); } } } r1 = new Rotation( 0.288, 0.384, 0.36, 0.8, false); checkRotation(r1, -r1.getQ0(), -r1.getQ1(), -r1.getQ2(), -r1.getQ3()); } @Test public void testCompose() throws MathIllegalArgumentException { Rotation r1 = new Rotation(new Vector3D(2, -3, 5), 1.7); Rotation r2 = new Rotation(new Vector3D(-1, 3, 2), 0.3); Rotation r3 = r2.applyTo(r1); for (double x = -0.9; x < 0.9; x += 0.2) { for (double y = -0.9; y < 0.9; y += 0.2) { for (double z = -0.9; z < 0.9; z += 0.2) { Vector3D u = new Vector3D(x, y, z); checkVector(r2.applyTo(r1.applyTo(u)), r3.applyTo(u)); } } } } @Test public void testComposeInverse() throws MathIllegalArgumentException { Rotation r1 = new Rotation(new Vector3D(2, -3, 5), 1.7); Rotation r2 = new Rotation(new Vector3D(-1, 3, 2), 0.3); Rotation r3 = r2.applyInverseTo(r1); for (double x = -0.9; x < 0.9; x += 0.2) { for (double y = -0.9; y < 0.9; y += 0.2) { for (double z = -0.9; z < 0.9; z += 0.2) { Vector3D u = new Vector3D(x, y, z); checkVector(r2.applyInverseTo(r1.applyTo(u)), r3.applyTo(u)); } } } } @Test public void testArray() throws MathIllegalArgumentException { Rotation r = new Rotation(new Vector3D(2, -3, 5), 1.7); for (double x = -0.9; x < 0.9; x += 0.2) { for (double y = -0.9; y < 0.9; y += 0.2) { for (double z = -0.9; z < 0.9; z += 0.2) { Vector3D u = new Vector3D(x, y, z); Vector3D v = r.applyTo(u); double[] inOut = new double[] { x, y, z }; r.applyTo(inOut, inOut); Assert.assertEquals(v.getX(), inOut[0], 1.0e-10); Assert.assertEquals(v.getY(), inOut[1], 1.0e-10); Assert.assertEquals(v.getZ(), inOut[2], 1.0e-10); r.applyInverseTo(inOut, inOut); Assert.assertEquals(u.getX(), inOut[0], 1.0e-10); Assert.assertEquals(u.getY(), inOut[1], 1.0e-10); Assert.assertEquals(u.getZ(), inOut[2], 1.0e-10); } } } } @Test public void testApplyInverseTo() throws MathIllegalArgumentException { Rotation r = new Rotation(new Vector3D(2, -3, 5), 1.7); for (double lambda = 0; lambda < 6.2; lambda += 0.2) { for (double phi = -1.55; phi < 1.55; phi += 0.2) { Vector3D u = new Vector3D(FastMath.cos(lambda) * FastMath.cos(phi), FastMath.sin(lambda) * FastMath.cos(phi), FastMath.sin(phi)); r.applyInverseTo(r.applyTo(u)); checkVector(u, r.applyInverseTo(r.applyTo(u))); checkVector(u, r.applyTo(r.applyInverseTo(u))); } } r = Rotation.IDENTITY; for (double lambda = 0; lambda < 6.2; lambda += 0.2) { for (double phi = -1.55; phi < 1.55; phi += 0.2) { Vector3D u = new Vector3D(FastMath.cos(lambda) * FastMath.cos(phi), FastMath.sin(lambda) * FastMath.cos(phi), FastMath.sin(phi)); checkVector(u, r.applyInverseTo(r.applyTo(u))); checkVector(u, r.applyTo(r.applyInverseTo(u))); } } r = new Rotation(Vector3D.PLUS_K, FastMath.PI); for (double lambda = 0; lambda < 6.2; lambda += 0.2) { for (double phi = -1.55; phi < 1.55; phi += 0.2) { Vector3D u = new Vector3D(FastMath.cos(lambda) * FastMath.cos(phi), FastMath.sin(lambda) * FastMath.cos(phi), FastMath.sin(phi)); checkVector(u, r.applyInverseTo(r.applyTo(u))); checkVector(u, r.applyTo(r.applyInverseTo(u))); } } } @Test public void testIssue639() throws MathArithmeticException{ Vector3D u1 = new Vector3D(-1321008684645961.0 / 268435456.0, -5774608829631843.0 / 268435456.0, -3822921525525679.0 / 4294967296.0); Vector3D u2 =new Vector3D( -5712344449280879.0 / 2097152.0, -2275058564560979.0 / 1048576.0, 4423475992255071.0 / 65536.0); Rotation rot = new Rotation(u1, u2, Vector3D.PLUS_I,Vector3D.PLUS_K); Assert.assertEquals( 0.6228370359608200639829222, rot.getQ0(), 1.0e-15); Assert.assertEquals( 0.0257707621456498790029987, rot.getQ1(), 1.0e-15); Assert.assertEquals(-0.0000000002503012255839931, rot.getQ2(), 1.0e-15); Assert.assertEquals(-0.7819270390861109450724902, rot.getQ3(), 1.0e-15); } @Test public void testIssue801() throws MathArithmeticException { Vector3D u1 = new Vector3D(0.9999988431610581, -0.0015210774290851095, 0.0); Vector3D u2 = new Vector3D(0.0, 0.0, 1.0); Vector3D v1 = new Vector3D(0.9999999999999999, 0.0, 0.0); Vector3D v2 = new Vector3D(0.0, 0.0, -1.0); Rotation quat = new Rotation(u1, u2, v1, v2); double q2 = quat.getQ0() * quat.getQ0() + quat.getQ1() * quat.getQ1() + quat.getQ2() * quat.getQ2() + quat.getQ3() * quat.getQ3(); Assert.assertEquals(1.0, q2, 1.0e-14); Assert.assertEquals(0.0, Vector3D.angle(v1, quat.applyTo(u1)), 1.0e-14); Assert.assertEquals(0.0, Vector3D.angle(v2, quat.applyTo(u2)), 1.0e-14); } private void checkVector(Vector3D v1, Vector3D v2) { Assert.assertTrue(v1.subtract(v2).getNorm() < 1.0e-10); } private void checkAngle(double a1, double a2) { Assert.assertEquals(a1, MathUtils.normalizeAngle(a2, a1), 1.0e-10); } private void checkRotation(Rotation r, double q0, double q1, double q2, double q3) { Assert.assertEquals(0, Rotation.distance(r, new Rotation(q0, q1, q2, q3, false)), 1.0e-12); } } ././@LongLink100644 0 0 156 12126630646 10263 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/geometry/euclidean/threed/RotationOrderTest.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/geometry/euclidean/threed/RotationOrder100644 1750 1750 3523 12126627671 32341 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.geometry.euclidean.threed; import java.lang.reflect.Field; import org.apache.commons.math3.geometry.euclidean.threed.RotationOrder; import org.junit.Assert; import org.junit.Test; public class RotationOrderTest { @Test public void testName() { RotationOrder[] orders = { RotationOrder.XYZ, RotationOrder.XZY, RotationOrder.YXZ, RotationOrder.YZX, RotationOrder.ZXY, RotationOrder.ZYX, RotationOrder.XYX, RotationOrder.XZX, RotationOrder.YXY, RotationOrder.YZY, RotationOrder.ZXZ, RotationOrder.ZYZ }; for (int i = 0; i < orders.length; ++i) { Assert.assertEquals(getFieldName(orders[i]), orders[i].toString()); } } private String getFieldName(RotationOrder order) { try { Field[] fields = RotationOrder.class.getFields(); for (int i = 0; i < fields.length; ++i) { if (fields[i].get(null) == order) { return fields[i].getName(); } } } catch (IllegalAccessException iae) { // ignored } return "unknown"; } } ././@LongLink100644 0 0 145 12126630646 10261 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/geometry/euclidean/threed/LineTest.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/geometry/euclidean/threed/LineTest.java100644 1750 1750 16273 12126627671 32243 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.geometry.euclidean.threed; import org.apache.commons.math3.exception.MathArithmeticException; import org.apache.commons.math3.exception.MathIllegalArgumentException; import org.apache.commons.math3.geometry.euclidean.threed.Line; import org.apache.commons.math3.geometry.euclidean.threed.Vector3D; import org.apache.commons.math3.util.FastMath; import org.junit.Assert; import org.junit.Test; public class LineTest { @Test public void testContains() throws MathIllegalArgumentException, MathArithmeticException { Vector3D p1 = new Vector3D(0, 0, 1); Line l = new Line(p1, new Vector3D(0, 0, 2)); Assert.assertTrue(l.contains(p1)); Assert.assertTrue(l.contains(new Vector3D(1.0, p1, 0.3, l.getDirection()))); Vector3D u = l.getDirection().orthogonal(); Vector3D v = Vector3D.crossProduct(l.getDirection(), u); for (double alpha = 0; alpha < 2 * FastMath.PI; alpha += 0.3) { Assert.assertTrue(! l.contains(p1.add(new Vector3D(FastMath.cos(alpha), u, FastMath.sin(alpha), v)))); } } @Test public void testSimilar() throws MathIllegalArgumentException, MathArithmeticException { Vector3D p1 = new Vector3D (1.2, 3.4, -5.8); Vector3D p2 = new Vector3D (3.4, -5.8, 1.2); Line lA = new Line(p1, p2); Line lB = new Line(p2, p1); Assert.assertTrue(lA.isSimilarTo(lB)); Assert.assertTrue(! lA.isSimilarTo(new Line(p1, p1.add(lA.getDirection().orthogonal())))); } @Test public void testPointDistance() throws MathIllegalArgumentException { Line l = new Line(new Vector3D(0, 1, 1), new Vector3D(0, 2, 2)); Assert.assertEquals(FastMath.sqrt(3.0 / 2.0), l.distance(new Vector3D(1, 0, 1)), 1.0e-10); Assert.assertEquals(0, l.distance(new Vector3D(0, -4, -4)), 1.0e-10); } @Test public void testLineDistance() throws MathIllegalArgumentException { Line l = new Line(new Vector3D(0, 1, 1), new Vector3D(0, 2, 2)); Assert.assertEquals(1.0, l.distance(new Line(new Vector3D(1, 0, 1), new Vector3D(1, 0, 2))), 1.0e-10); Assert.assertEquals(0.5, l.distance(new Line(new Vector3D(-0.5, 0, 0), new Vector3D(-0.5, -1, -1))), 1.0e-10); Assert.assertEquals(0.0, l.distance(l), 1.0e-10); Assert.assertEquals(0.0, l.distance(new Line(new Vector3D(0, -4, -4), new Vector3D(0, -5, -5))), 1.0e-10); Assert.assertEquals(0.0, l.distance(new Line(new Vector3D(0, -4, -4), new Vector3D(0, -3, -4))), 1.0e-10); Assert.assertEquals(0.0, l.distance(new Line(new Vector3D(0, -4, -4), new Vector3D(1, -4, -4))), 1.0e-10); Assert.assertEquals(FastMath.sqrt(8), l.distance(new Line(new Vector3D(0, -4, 0), new Vector3D(1, -4, 0))), 1.0e-10); } @Test public void testClosest() throws MathIllegalArgumentException { Line l = new Line(new Vector3D(0, 1, 1), new Vector3D(0, 2, 2)); Assert.assertEquals(0.0, l.closestPoint(new Line(new Vector3D(1, 0, 1), new Vector3D(1, 0, 2))).distance(new Vector3D(0, 0, 0)), 1.0e-10); Assert.assertEquals(0.5, l.closestPoint(new Line(new Vector3D(-0.5, 0, 0), new Vector3D(-0.5, -1, -1))).distance(new Vector3D(-0.5, 0, 0)), 1.0e-10); Assert.assertEquals(0.0, l.closestPoint(l).distance(new Vector3D(0, 0, 0)), 1.0e-10); Assert.assertEquals(0.0, l.closestPoint(new Line(new Vector3D(0, -4, -4), new Vector3D(0, -5, -5))).distance(new Vector3D(0, 0, 0)), 1.0e-10); Assert.assertEquals(0.0, l.closestPoint(new Line(new Vector3D(0, -4, -4), new Vector3D(0, -3, -4))).distance(new Vector3D(0, -4, -4)), 1.0e-10); Assert.assertEquals(0.0, l.closestPoint(new Line(new Vector3D(0, -4, -4), new Vector3D(1, -4, -4))).distance(new Vector3D(0, -4, -4)), 1.0e-10); Assert.assertEquals(0.0, l.closestPoint(new Line(new Vector3D(0, -4, 0), new Vector3D(1, -4, 0))).distance(new Vector3D(0, -2, -2)), 1.0e-10); } @Test public void testIntersection() throws MathIllegalArgumentException { Line l = new Line(new Vector3D(0, 1, 1), new Vector3D(0, 2, 2)); Assert.assertNull(l.intersection(new Line(new Vector3D(1, 0, 1), new Vector3D(1, 0, 2)))); Assert.assertNull(l.intersection(new Line(new Vector3D(-0.5, 0, 0), new Vector3D(-0.5, -1, -1)))); Assert.assertEquals(0.0, l.intersection(l).distance(new Vector3D(0, 0, 0)), 1.0e-10); Assert.assertEquals(0.0, l.intersection(new Line(new Vector3D(0, -4, -4), new Vector3D(0, -5, -5))).distance(new Vector3D(0, 0, 0)), 1.0e-10); Assert.assertEquals(0.0, l.intersection(new Line(new Vector3D(0, -4, -4), new Vector3D(0, -3, -4))).distance(new Vector3D(0, -4, -4)), 1.0e-10); Assert.assertEquals(0.0, l.intersection(new Line(new Vector3D(0, -4, -4), new Vector3D(1, -4, -4))).distance(new Vector3D(0, -4, -4)), 1.0e-10); Assert.assertNull(l.intersection(new Line(new Vector3D(0, -4, 0), new Vector3D(1, -4, 0)))); } @Test public void testRevert() { // setup Line line = new Line(new Vector3D(1653345.6696423641, 6170370.041579291, 90000), new Vector3D(1650757.5050732433, 6160710.879908984, 0.9)); Vector3D expected = line.getDirection().negate(); // action Line reverted = line.revert(); // verify Assert.assertArrayEquals(expected.toArray(), reverted.getDirection().toArray(), 0); } } ././@LongLink100644 0 0 147 12126630646 10263 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/geometry/euclidean/oned/IntervalTest.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/geometry/euclidean/oned/IntervalTest.ja100644 1750 1750 6634 12126627671 32243 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.geometry.euclidean.oned; import org.apache.commons.math3.geometry.partitioning.Region; import org.apache.commons.math3.util.FastMath; import org.apache.commons.math3.util.Precision; import org.junit.Assert; import org.junit.Test; public class IntervalTest { @Test public void testInterval() { Interval interval = new Interval(2.3, 5.7); Assert.assertEquals(3.4, interval.getSize(), 1.0e-10); Assert.assertEquals(4.0, interval.getBarycenter(), 1.0e-10); Assert.assertEquals(Region.Location.BOUNDARY, interval.checkPoint(2.3, 1.0e-10)); Assert.assertEquals(Region.Location.BOUNDARY, interval.checkPoint(5.7, 1.0e-10)); Assert.assertEquals(Region.Location.OUTSIDE, interval.checkPoint(1.2, 1.0e-10)); Assert.assertEquals(Region.Location.OUTSIDE, interval.checkPoint(8.7, 1.0e-10)); Assert.assertEquals(Region.Location.INSIDE, interval.checkPoint(3.0, 1.0e-10)); Assert.assertEquals(2.3, interval.getInf(), 1.0e-10); Assert.assertEquals(5.7, interval.getSup(), 1.0e-10); } @Test public void testTolerance() { Interval interval = new Interval(2.3, 5.7); Assert.assertEquals(Region.Location.OUTSIDE, interval.checkPoint(1.2, 1.0)); Assert.assertEquals(Region.Location.BOUNDARY, interval.checkPoint(1.2, 1.2)); Assert.assertEquals(Region.Location.OUTSIDE, interval.checkPoint(8.7, 2.9)); Assert.assertEquals(Region.Location.BOUNDARY, interval.checkPoint(8.7, 3.1)); Assert.assertEquals(Region.Location.INSIDE, interval.checkPoint(3.0, 0.6)); Assert.assertEquals(Region.Location.BOUNDARY, interval.checkPoint(3.0, 0.8)); } @Test public void testInfinite() { Interval interval = new Interval(9.0, Double.POSITIVE_INFINITY); Assert.assertEquals(Region.Location.BOUNDARY, interval.checkPoint(9.0, 1.0e-10)); Assert.assertEquals(Region.Location.OUTSIDE, interval.checkPoint(8.4, 1.0e-10)); for (double e = 1.0; e <= 6.0; e += 1.0) { Assert.assertEquals(Region.Location.INSIDE, interval.checkPoint(FastMath.pow(10.0, e), 1.0e-10)); } Assert.assertTrue(Double.isInfinite(interval.getSize())); Assert.assertEquals(9.0, interval.getInf(), 1.0e-10); Assert.assertTrue(Double.isInfinite(interval.getSup())); } @Test public void testSinglePoint() { Interval interval = new Interval(1.0, 1.0); Assert.assertEquals(0.0, interval.getSize(), Precision.SAFE_MIN); Assert.assertEquals(1.0, interval.getBarycenter(), Precision.EPSILON); } } ././@LongLink100644 0 0 153 12126630646 10260 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/geometry/euclidean/oned/IntervalsSetTest.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/geometry/euclidean/oned/IntervalsSetTes100644 1750 1750 12577 12126627671 32350 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.geometry.euclidean.oned; import java.util.List; import org.apache.commons.math3.geometry.euclidean.oned.Interval; import org.apache.commons.math3.geometry.euclidean.oned.IntervalsSet; import org.apache.commons.math3.geometry.euclidean.oned.Vector1D; import org.apache.commons.math3.geometry.partitioning.Region; import org.apache.commons.math3.geometry.partitioning.RegionFactory; import org.apache.commons.math3.util.FastMath; import org.apache.commons.math3.util.Precision; import org.junit.Assert; import org.junit.Test; public class IntervalsSetTest { @Test public void testInterval() { IntervalsSet set = new IntervalsSet(2.3, 5.7); Assert.assertEquals(3.4, set.getSize(), 1.0e-10); Assert.assertEquals(4.0, ((Vector1D) set.getBarycenter()).getX(), 1.0e-10); Assert.assertEquals(Region.Location.BOUNDARY, set.checkPoint(new Vector1D(2.3))); Assert.assertEquals(Region.Location.BOUNDARY, set.checkPoint(new Vector1D(5.7))); Assert.assertEquals(Region.Location.OUTSIDE, set.checkPoint(new Vector1D(1.2))); Assert.assertEquals(Region.Location.OUTSIDE, set.checkPoint(new Vector1D(8.7))); Assert.assertEquals(Region.Location.INSIDE, set.checkPoint(new Vector1D(3.0))); Assert.assertEquals(2.3, set.getInf(), 1.0e-10); Assert.assertEquals(5.7, set.getSup(), 1.0e-10); } @Test public void testInfinite() { IntervalsSet set = new IntervalsSet(9.0, Double.POSITIVE_INFINITY); Assert.assertEquals(Region.Location.BOUNDARY, set.checkPoint(new Vector1D(9.0))); Assert.assertEquals(Region.Location.OUTSIDE, set.checkPoint(new Vector1D(8.4))); for (double e = 1.0; e <= 6.0; e += 1.0) { Assert.assertEquals(Region.Location.INSIDE, set.checkPoint(new Vector1D(FastMath.pow(10.0, e)))); } Assert.assertTrue(Double.isInfinite(set.getSize())); Assert.assertEquals(9.0, set.getInf(), 1.0e-10); Assert.assertTrue(Double.isInfinite(set.getSup())); set = (IntervalsSet) new RegionFactory().getComplement(set); Assert.assertEquals(9.0, set.getSup(), 1.0e-10); Assert.assertTrue(Double.isInfinite(set.getInf())); } @Test public void testMultiple() { RegionFactory factory = new RegionFactory(); IntervalsSet set = (IntervalsSet) factory.intersection(factory.union(factory.difference(new IntervalsSet(1.0, 6.0), new IntervalsSet(3.0, 5.0)), new IntervalsSet(9.0, Double.POSITIVE_INFINITY)), new IntervalsSet(Double.NEGATIVE_INFINITY, 11.0)); Assert.assertEquals(5.0, set.getSize(), 1.0e-10); Assert.assertEquals(5.9, ((Vector1D) set.getBarycenter()).getX(), 1.0e-10); Assert.assertEquals(Region.Location.OUTSIDE, set.checkPoint(new Vector1D(0.0))); Assert.assertEquals(Region.Location.OUTSIDE, set.checkPoint(new Vector1D(4.0))); Assert.assertEquals(Region.Location.OUTSIDE, set.checkPoint(new Vector1D(8.0))); Assert.assertEquals(Region.Location.OUTSIDE, set.checkPoint(new Vector1D(12.0))); Assert.assertEquals(Region.Location.INSIDE, set.checkPoint(new Vector1D(1.2))); Assert.assertEquals(Region.Location.INSIDE, set.checkPoint(new Vector1D(5.9))); Assert.assertEquals(Region.Location.INSIDE, set.checkPoint(new Vector1D(9.01))); Assert.assertEquals(Region.Location.BOUNDARY, set.checkPoint(new Vector1D(5.0))); Assert.assertEquals(Region.Location.BOUNDARY, set.checkPoint(new Vector1D(11.0))); Assert.assertEquals( 1.0, set.getInf(), 1.0e-10); Assert.assertEquals(11.0, set.getSup(), 1.0e-10); List list = set.asList(); Assert.assertEquals(3, list.size()); Assert.assertEquals( 1.0, list.get(0).getInf(), 1.0e-10); Assert.assertEquals( 3.0, list.get(0).getSup(), 1.0e-10); Assert.assertEquals( 5.0, list.get(1).getInf(), 1.0e-10); Assert.assertEquals( 6.0, list.get(1).getSup(), 1.0e-10); Assert.assertEquals( 9.0, list.get(2).getInf(), 1.0e-10); Assert.assertEquals(11.0, list.get(2).getSup(), 1.0e-10); } @Test public void testSinglePoint() { IntervalsSet set = new IntervalsSet(1.0, 1.0); Assert.assertEquals(0.0, set.getSize(), Precision.SAFE_MIN); Assert.assertEquals(1.0, ((Vector1D) set.getBarycenter()).getX(), Precision.EPSILON); } } ././@LongLink100644 0 0 155 12126630646 10262 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/random/UnitSphereRandomVectorGeneratorTest.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/random/UnitSphereRandomVectorGeneratorT100644 1750 1750 5272 12126627670 32376 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.random; import org.apache.commons.math3.util.FastMath; import org.junit.Assert; import org.junit.Test; public class UnitSphereRandomVectorGeneratorTest { /** * Test the distribution of points from {@link UnitSphereRandomVectorGenerator#nextVector()} * in two dimensions. */ @Test public void test2DDistribution() { RandomGenerator rg = new JDKRandomGenerator(); rg.setSeed(17399225432l); UnitSphereRandomVectorGenerator generator = new UnitSphereRandomVectorGenerator(2, rg); // In 2D, angles with a given vector should be uniformly distributed int[] angleBuckets = new int[100]; int steps = 1000000; for (int i = 0; i < steps; ++i) { final double[] v = generator.nextVector(); Assert.assertEquals(2, v.length); Assert.assertEquals(1, length(v), 1e-10); // Compute angle formed with vector (1,0) // Cosine of angle is their dot product, because both are unit length // Dot product here is just the first element of the vector by construction final double angle = FastMath.acos(v[0]); final int bucket = (int) (angleBuckets.length * (angle / FastMath.PI)); ++angleBuckets[bucket]; } // Simplistic test for roughly even distribution final int expectedBucketSize = steps / angleBuckets.length; for (int bucket : angleBuckets) { Assert.assertTrue("Bucket count " + bucket + " vs expected " + expectedBucketSize, FastMath.abs(expectedBucketSize - bucket) < 350); } } /** * @return length (L2 norm) of given vector */ private static double length(double[] vector) { double total = 0; for (double d : vector) { total += d * d; } return FastMath.sqrt(total); } } commons-math3-3.2-src/src/test/java/org/apache/commons/math3/random/TestRandomGenerator.java100644 1750 1750 2671 12126627670 30640 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.random; import java.util.Random; /** * Dummy AbstractRandomGenerator concrete subclass that just wraps a * java.util.Random instance. Used by AbstractRandomGeneratorTest to test * default implementations in AbstractRandomGenerator. * * @version $Id: TestRandomGenerator.java 1244107 2012-02-14 16:17:55Z erans $ */ public class TestRandomGenerator extends AbstractRandomGenerator { private Random random = new Random(); @Override public void setSeed(long seed) { clear(); random.setSeed(seed); } @Override public double nextDouble() { return random.nextDouble(); } } commons-math3-3.2-src/src/test/java/org/apache/commons/math3/random/ISAACTest.java100644 1750 1750 110151 12126627670 26422 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.random; import org.junit.Assert; import org.junit.Test; public final class ISAACTest extends RandomGeneratorAbstractTest { @Override protected RandomGenerator makeGenerator() { return new ISAACRandom(500); } private static final int[] SEED_1 = { 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000 }; private static final int[] SEED_2 = { 0x61b12894, 0x4a43da95, 0x03e4d8c5, 0xd92e174f, 0xe8998d71, 0x0ecaaa89, 0xaba8a61d, 0xcfd457fc, 0xbf25f0a7, 0xe05b20a9, 0xdc744513, 0x9a3eb193, 0x1b69542b, 0xedb0890d, 0xca6b233d, 0xfcabf357, 0x297e95f0, 0x1a6c456f, 0x0e3738b0, 0x1c0517f2, 0xcfd105bd, 0x3b7c39eb, 0x141804e9, 0x8a13a0d1, 0x3e57cf5c, 0x35471206, 0x00115ef6, 0x3424ec23, 0x2a6a63a7, 0x4cecb3e8, 0xecb4d341, 0x63d25ac1, 0x8b68eafd, 0x0eca65b4, 0xd8354668, 0xb37b1ff8, 0x82e80ce3, 0x4c212f9c, 0x58d82d5f, 0x47f36348, 0x5bd88987, 0xf77ac66e, 0x75ff93ee, 0xef763453, 0x9705f8b6, 0x64e44649, 0x84f03338, 0x902120e9, 0x5350212e, 0x34f466f8, 0x97f96d0e, 0x7f1db8f0, 0x15879833, 0xefee14b4, 0xda25520a, 0x81c0dd7c, 0xa20bb729, 0x2fd76844, 0x1b522548, 0xf394565d, 0xabff5f1b, 0x38eaf2e7, 0x364a6ccf, 0x8ed5e169, 0xe76aae18, 0x0e4c0b62, 0x68356792, 0x8bc4aa83, 0x31546e69, 0xa6d04441, 0x2abef1df, 0xa40a164e, 0x8a8d00ba, 0x32b38dba, 0x6f66a7c6, 0x493b0c84, 0xd86846f0, 0x50d50178, 0x26a7e67c, 0x97802153, 0xf35f4ad3, 0x4b54b54e, 0x35bcaef1, 0xc3ed09d6, 0xc127bc55, 0xb2e34ea4, 0x8d674133, 0xed82f03d, 0xa8443b28, 0x30bf4bd9, 0x6748419c, 0x155eb313, 0xb17c85da, 0xcd0d8014, 0xf1e95740, 0x5e769ed2, 0xf8fa5819, 0x3bd0d93a, 0xa46eaf3f, 0x90f5ae2c, 0x912aa83d, 0x66995d3d, 0x8359b2a9, 0x64534b93, 0x1fa38094, 0x89fde50c, 0xef925ef5, 0x4edf2287, 0xc6e82b2b, 0x99812c32, 0x53f6fe01, 0xf104263f, 0xbc5d8b8e, 0x6f23f102, 0xb6cc174d, 0xb47b2421, 0xdb083f26, 0xd67a1f25, 0x4f4b0c50, 0x86fca924, 0x442036ba, 0x0d7dba08, 0xf6d89c1e, 0x65c9e28e, 0xd689fafc, 0x47f7ae9b, 0x3cb86674, 0x798dc4ce, 0x62a2d235, 0x73912420, 0x276ef9b4, 0x50deb689, 0x40b6b744, 0x84d79856, 0x86e5f0c6, 0x835a552b, 0xeb73ff95, 0xe428e392, 0x1d60bf15, 0x9ad09e35, 0x783e60ae, 0xb42feb61, 0x028a0343, 0x90febcfc, 0xdffb01c8, 0x1087ab1e, 0x84d8a8f9, 0xe5433763, 0x897548cc, 0xb4075ece, 0xd0a5a727, 0x0a84da19, 0x0f33964b, 0x5634a097, 0xb7cbac77, 0xe50340eb, 0x93f0ce63, 0x0c743ed4, 0xf6f6f015, 0x30f74cca, 0xbb0306a7, 0x3d037943, 0x22f2979e, 0x3591b2c3, 0x9a772e24, 0x5c5812fa, 0x9e733c8a, 0xaae1c943, 0xd0b7d924, 0x56b9c463, 0xfaedb557, 0x139b7aa5, 0xe173f123, 0x2d0686a2, 0x03cd0bef, 0x4f47c01e, 0x4cf9fb83, 0xba9d433e, 0x95a620d5, 0x62b67429, 0xe836067d, 0x606bc5f7, 0x36af81e2, 0xae0b8b30, 0x38ffe5fa, 0xb548228b, 0xc2a25bcb, 0x4ba139ee, 0xab214ad7, 0x66ef4f50, 0x5f8787fa, 0xcb5982b1, 0xdbb48ff8, 0x14eaa914, 0xe0874168, 0x3e578246, 0x488c1c11, 0x982039a2, 0xde096b35, 0xa420fb41, 0x197a1b67, 0x16eabc59, 0x0e689585, 0x24db72b7, 0xf89878c3, 0x04a5e854, 0x3346da02, 0xb4bb6a04, 0x278a0dd4, 0x7bb6e224, 0x92e219c3, 0x595f8a33, 0xedd87ae6, 0xa313cc9f, 0x385c1d3a, 0x0a8b1db5, 0xb192379d, 0x3a0be0eb, 0xb8ba269b, 0x1f0c62a5, 0x56307342, 0x8fc9ac94, 0xec91a232, 0xa7b8d3db, 0xfbf43a60, 0xa773463e, 0x72d5a4d1, 0x8ddf1755, 0x27da39bb, 0xa8d4668a, 0xd2f3bbfc, 0xa188d6af, 0x82ed668e, 0xb45f0032, 0xcdfd4ca0, 0x14e5a80d, 0x456594bc, 0x68efcdd5, 0x7dbf1f9d, 0x74599699, 0xfc8639e8, 0x4139e791, 0x9c06921a, 0xc5121d36, 0xd15b9425, 0x0670dca7, 0xbc60c353, 0xaa49e487, 0xa7cb5854, 0xd06ddbdb, 0xcb4be0d2, 0x8391ab98, 0xc750d7bf, 0x365340b1, 0x264677c7, 0xb76075a4 }; /** * The output sequence generated by reference ISAAC algorithm in C language. * For initial seeding is used SEED_1 data array. * 1024 signed 32-bit integers in hexadecimal representation. */ private static final int[] EXPECTED_SEQUENCE_1 = { 0x182600f3, 0x300b4a8d, 0x301b6622, 0xb08acd21, 0x296fd679, 0x995206e9, 0xb3ffa8b5, 0x0fc99c24, 0x5f071faf, 0x52251def, 0x894f41c2, 0xcc4c9afb, 0x96c33f74, 0x347cb71d, 0xc90f8fbd, 0xa658f57a, 0xc5c29e18, 0x6249fa29, 0xbae16ffa, 0x25c871bd, 0xf4c75e24, 0x5ab3eab9, 0xac450b8f, 0x1629cfa4, 0x0016e86f, 0xf27c4d0d, 0x67648b17, 0x05c04fce, 0x44d3ff79, 0xc6acd20f, 0x472fd994, 0x842131c4, 0xead4a900, 0xc01eda0d, 0x9e604c7b, 0xfb8a0e99, 0x02e17b6f, 0xe8b4f627, 0xc7041eae, 0x42d19cd7, 0xa358eb94, 0x19ca2158, 0x6be6ce81, 0x4b90a4de, 0x26f0774d, 0x4e83930a, 0x2492d476, 0xb97ffabe, 0x675cc8ae, 0x4cfdd254, 0x5d3c00ea, 0x7bba5ead, 0x6f461810, 0xbef63eea, 0x72eb767b, 0xed6e963b, 0xb026016d, 0x17cb7ebf, 0xa7dc6e56, 0xf460bdf1, 0x1ffe0e04, 0x902b347d, 0x02c0d8ab, 0x98cb3f8b, 0x6f359a39, 0x9521825f, 0x9026d97e, 0xde342516, 0x890a740c, 0x0f2969e4, 0x2e7ea9ed, 0x394b8a4f, 0x1bdf1fd0, 0x15d565b4, 0xbaf0406d, 0x4dac20db, 0x03359832, 0xe34802d5, 0xcc5fff02, 0x0935ad6e, 0x7c53c9b2, 0xb10b5d29, 0x4fbb94be, 0xd7e48546, 0xb7cfa23c, 0x7f081c9a, 0xe099baf1, 0x9c7dc323, 0xb831ad14, 0x5b563101, 0xfa55319b, 0x060ded54, 0xc5418124, 0x765f0dba, 0x1ad3d9d5, 0x3f07ec49, 0xdd5e06c6, 0xc230e2ac, 0xc6ba1971, 0x9cc17bcc, 0x10b22a22, 0x7dfc8c7f, 0xb3310333, 0x205530ee, 0xdbf38a8f, 0x003a02f5, 0x007e96a3, 0x36658201, 0x08dfd64f, 0x6275acf3, 0x3d29669b, 0x6b2f4538, 0xb0cc336b, 0x1d3043eb, 0x1ad1d764, 0x4c655b84, 0x7a725bb2, 0xb3fc5c66, 0x80b4b915, 0xb2cbd9e4, 0x2992dfc6, 0xdf8be548, 0xb310d06e, 0x436385c6, 0x44d6e893, 0x44c4d79d, 0xe3bb2064, 0xe41ea465, 0x3ff4cc70, 0x9d21ac42, 0x672c3725, 0xa43a1d02, 0xfd84b19b, 0x5b6fb132, 0x4af40896, 0xe15000a6, 0x7cab12f6, 0x8b8e753c, 0xfb253454, 0x359ac366, 0x67822b45, 0x290a1140, 0xade6e428, 0x6095efcb, 0x99d8d9e6, 0xa5b5981d, 0x332c95d6, 0xaf5cfcab, 0x161f5ca6, 0x1844cee2, 0xffb8ab5c, 0x82fccaeb, 0x49ecf97a, 0x7a60fabd, 0xf9585a3a, 0x4eb6bd32, 0x3b347002, 0xf4930dba, 0x5d21d51e, 0x64e8e3f4, 0x52801fa8, 0x71ce907c, 0x872783a4, 0x0761dc80, 0x5c509848, 0x41ba2adc, 0x7e2f5520, 0x85c5eec2, 0x368d3d00, 0x5fc7c5f3, 0xb849d785, 0xd95f25b3, 0x79801fd5, 0xbf2443d6, 0x360d41cd, 0x651b11c0, 0x801a89ca, 0x8b9e6b94, 0xfde283c4, 0xcc5e6974, 0x2b2f4c09, 0x8b2160a8, 0xdbf57f01, 0x76aa1c4e, 0x11f0831a, 0x54713d17, 0xc99a2639, 0x4c373e7a, 0x09e6e57f, 0x71f63b07, 0x7be3f02e, 0x2c907ade, 0xe5f489f6, 0x0b0cd6da, 0xb566e14c, 0x0f955969, 0xa0e5710b, 0x80d8c2de, 0x9971e496, 0xc7efbc2f, 0x97a48e53, 0x2d845c0d, 0xe1194b0e, 0xad2ba480, 0xd5253552, 0xca890b31, 0x60060afb, 0x89dae927, 0x565e2229, 0x43abc21c, 0x03dd14a5, 0xbbadd184, 0x9e979702, 0x2f659883, 0xf313adec, 0x621bd7ca, 0xb6470834, 0x4c3901c6, 0x32028bb8, 0x9ded8244, 0x66907654, 0x0a06b272, 0x4a8ec630, 0x4207d36f, 0x3e7a8b49, 0x13871be7, 0xbf7af48e, 0x3de0df39, 0x0e864542, 0x8c090a23, 0xaf90e49e, 0x97661c5e, 0x365aa66c, 0x0073e342, 0x9c8ac447, 0x6f57e7ce, 0xd5be7ffa, 0x89651d84, 0x53f78eaa, 0x8173dc04, 0xd70b1e10, 0x43c1a57b, 0x10c8a5ab, 0xed6abd62, 0x2f840e43, 0x4873d91e, 0x49f413fc, 0x5d89a1c1, 0xd3a388fc, 0x96c59cf4, 0x456f1edd, 0x3dd20023, 0xa264e933, 0xd32956e5, 0xd91aa738, 0xe76dd339, 0x7a68710f, 0x6554abda, 0x90c10757, 0x0b5e435f, 0xaf7d1fb8, 0x01913fd3, 0x6a158d10, 0xb8f6fd4a, 0xc2b9aa36, 0x96da2655, 0xfe1e42d5, 0x56e6cd21, 0xd5b2d750, 0x7229ea81, 0x5de87abb, 0xb6b9d766, 0x1e16614c, 0x3b708f99, 0x5cf824cd, 0xa4ca0cf1, 0x62d31911, 0x7cdd662f, 0xcb9e1563, 0x79ae4c10, 0x080c79ec, 0x18080c8e, 0x4a0a283c, 0x3dde9f39, 0x09c36f90, 0xad567643, 0x08294766, 0xb4415f7d, 0x5597ec0f, 0x78ffa568, 0x8bace62e, 0x4188bfcd, 0xc87c8006, 0xafa92a6d, 0x50fc8194, 0xcae8deba, 0x33f6d7b1, 0x53245b79, 0x61119a5a, 0x7e315aeb, 0xe75b41c9, 0xd2a93b51, 0xec46b0b6, 0x1ed3ff4e, 0x5d023e65, 0xadf6bc23, 0xf7f58f7b, 0xe4f3a26a, 0x0c571a7d, 0xed35e5ee, 0xeadebeac, 0x30bcc764, 0x66f1e0ab, 0x826dfa89, 0x0d9c7e7e, 0xe7e26581, 0xd5990dfb, 0x02c9b944, 0x4112d96c, 0x3ff1e524, 0xc35e4580, 0xfdfef62d, 0xb83f957a, 0xbfc7f7cc, 0xb510ce0e, 0xcd7411a7, 0x04db4e13, 0x76904b6d, 0x08607f04, 0x3718d597, 0x46c0a6f5, 0x8406b137, 0x309bfb78, 0xf7d3f39f, 0x8c2f0d55, 0xc613f157, 0x127dd430, 0x72c9137d, 0x68a39358, 0x07c28cd1, 0x848f520a, 0xdd2dc1d5, 0x9388b13b, 0x28e7cb78, 0x03fb88f4, 0xb0b84e7b, 0x14c8009b, 0x884d6825, 0x21c171ec, 0x0809e494, 0x6a107589, 0x12595a19, 0x0bb3263f, 0x4d8fae82, 0x2a98121a, 0xb00960ba, 0x6708a2bc, 0x35a124b5, 0xbccaaeed, 0x294d37e5, 0xd405ded8, 0x9f39e2d9, 0x21835c4d, 0xe89b1a3b, 0x7364944b, 0xbd2e5024, 0x6a123f57, 0x34105a8c, 0x5ad0d3b0, 0xcc033ce3, 0xd51f093d, 0x56a001e3, 0x01a9bd70, 0x8891b3db, 0x13add922, 0x3d77d9a2, 0x0e7e0e67, 0xd73f72d4, 0x917bdec2, 0xa37f63ff, 0x23d74f4e, 0x3a6ce389, 0x0606cf9f, 0xde11ed34, 0x70cc94ae, 0xcb0eee4a, 0x13edc0cb, 0xfe29661c, 0xdb6dbe96, 0xb388d96c, 0x33bc405d, 0xa6d12101, 0x2f36fa86, 0x7ded386f, 0xe6344451, 0xcd57c7f7, 0x1b0dcdc1, 0xcd49ebdb, 0x9e8a51da, 0x12a0594b, 0x60d4d5f8, 0x91c8d925, 0xe43d0fbb, 0x5d2a542f, 0x451e7ec8, 0x2b36505c, 0x37c0ed05, 0x2364a1aa, 0x814bc24c, 0xe3a662d9, 0xf2b5cc05, 0xb8b0ccfc, 0xb058bafb, 0x3aea3dec, 0x0d028684, 0x64af0fef, 0x210f3925, 0xb67ec13a, 0x97166d14, 0xf7e1cdd0, 0x5adb60e7, 0xd5295ebc, 0x28833522, 0x60eda8da, 0x7bc76811, 0xac9fe69d, 0x30ab93ec, 0x03696614, 0x15e3a5b9, 0xecc5dc91, 0x1d3b8e97, 0x7275e277, 0x538e1f4e, 0x6cb167db, 0xa7a2f402, 0x2db35dfe, 0xa8bcc22d, 0xd8c58a6a, 0x6a529b0b, 0x0fd43963, 0xafc17a97, 0x943c3c74, 0x95138769, 0x6f4e0772, 0xb143b688, 0x3b18e752, 0x69d2e4ae, 0x8107c9ff, 0xcdbc62e2, 0x5781414f, 0x8b87437e, 0xa70e1101, 0x91dabc65, 0x4e232cd0, 0x229749b5, 0xd7386806, 0xb3c3f24b, 0x60dc5207, 0x0bdb9c30, 0x1a70e7e9, 0xf37c71d5, 0x44b89b08, 0xb4d2f976, 0xb40e27bc, 0xffdf8a80, 0x9c411a2a, 0xd0f7b37d, 0xef53cec4, 0xeca4d58a, 0x0b923200, 0xcf22e064, 0x8ebfa303, 0xf7cf814c, 0x32ae2a2b, 0xb5e13dae, 0xc998f9ff, 0x349947b0, 0x29cf72ce, 0x17e38f85, 0xf3b26129, 0xd45d6d81, 0x09b3ce98, 0x860536b8, 0xe5792e1b, 0x12ad6419, 0xf5f71c69, 0xcbc8b7c2, 0x8f651659, 0xa0cc74f3, 0xd78cb99e, 0x51c08d83, 0x29f55449, 0x002ed713, 0x38a824f3, 0x57161df6, 0x7452e319, 0x25890e2e, 0xc7442433, 0x4a5f6355, 0x6a83e1e0, 0x823cedb6, 0xf1d444eb, 0x88381097, 0x5de3743e, 0x46ca4f9a, 0xd8370487, 0xedec154a, 0x433f1afb, 0xf5fad54f, 0x98db2fb4, 0xe448e96d, 0xf650e4c8, 0x4bb5af29, 0x9d855e89, 0xc54cd95b, 0x46d95ca5, 0xef73fbf0, 0xf943f672, 0x86ba527f, 0x9d8d1908, 0xf3310c92, 0x05340e15, 0x07cffad9, 0x21e2547e, 0x8c17eff0, 0xd32be060, 0x8aba3ffb, 0x94d40125, 0xc5a87748, 0x824c2009, 0x73c0e762, 0xcdfec2af, 0x0e6c51b3, 0xa86f875e, 0xbc6172c7, 0xf7f395f1, 0x3f7579b3, 0x7aa114ed, 0x165b1015, 0xd531161a, 0xe36ef5bb, 0xdc153e5f, 0x1d0cb81b, 0xceffc147, 0x6079e4ce, 0xc3142d8f, 0xa617a083, 0xb54fed6f, 0xc3c7be2c, 0x02614abf, 0x6fb5ce56, 0xd21e796c, 0x2d0985de, 0xe9f84163, 0xc1a71e3c, 0x2887d96f, 0x57c4c925, 0x05efe294, 0x88157153, 0x9a30c4e8, 0x8854a0a1, 0x02503f7d, 0x0cd6ef81, 0x1da4f25a, 0xe8fa3860, 0x32e39273, 0x4c8652d6, 0x9ab3a42f, 0x9ead7f70, 0x836d8003, 0x6cbe7935, 0x721502dd, 0x5a48755c, 0x07497cae, 0xde462f4d, 0x92f57ea7, 0x1fe26ce0, 0x27c82282, 0xd6ec2f2b, 0x80c6e402, 0xce86fdfc, 0x52649d6c, 0xc798f047, 0x45bae606, 0x891aec49, 0x66c97340, 0x9ca45e1c, 0x4286619c, 0xf5f9cc3b, 0x4e823ad3, 0xc0d5d42a, 0xaee19096, 0x3d469303, 0xfe4cb380, 0xc9cd808c, 0x37a97df6, 0x308f751f, 0x276df0b4, 0xe5fbb9c7, 0x97ca2070, 0x88412761, 0x2ce5d3d5, 0xd7b43abe, 0xa30519ad, 0x26414ff3, 0xc5bde908, 0x275ead3a, 0x26ceb003, 0xbf1bd691, 0x037464c0, 0xe24124c0, 0x81d4cc5f, 0x484525e4, 0x1c3a4524, 0x9e7e4f04, 0xe1279bff, 0x6dd1943a, 0x403dae08, 0x82846526, 0xd5683858, 0x29322d0d, 0xa949bea2, 0x74096ae7, 0x85a13f85, 0x68235b9d, 0x8ef4bce6, 0x142a6e85, 0xdad1b22a, 0xb7546681, 0x959e234e, 0xfd8650d8, 0x3e730fa8, 0x56f55a71, 0xd20adf03, 0x7cdc78a2, 0x19047c79, 0x253b1d7a, 0x4389a84a, 0x0aeb8165, 0x9c15db3b, 0xaafef5a7, 0xbe8b06b2, 0xb5fe87c0, 0xe2a4ef71, 0xd9d711f9, 0xecfcf20b, 0x80fac4c2, 0xbbb8abc4, 0x239e3b0a, 0x858129a6, 0xd97cd348, 0x8a30738a, 0xc5b71937, 0xd649a428, 0x18c1ef9a, 0x75c08a36, 0xc921f94e, 0xdf9afa29, 0x040f7074, 0x72f5972f, 0x84ef01da, 0x2cb7b77f, 0x867027d7, 0x9ce0199d, 0x71865c4c, 0x7a36af93, 0x6c48ddd8, 0x19b48fd0, 0x75f4e9e2, 0x0084cfe5, 0x63bfd4d8, 0x9783cdee, 0x64f2632c, 0xf1b20eaf, 0xcc8bfa2d, 0x39ddf25a, 0x740e0066, 0x9ddb477f, 0x12b2cfb6, 0xd067fce5, 0x1a4b6a53, 0x001cdb95, 0x12c06908, 0x531ac6bf, 0x513c1764, 0xf7476978, 0x1be79937, 0x8a8dfaa3, 0x70a1660e, 0xfb737b1a, 0x3f28ee63, 0xc1a51375, 0x84bd6dd6, 0xe4a51d21, 0xeafed133, 0x22ae0582, 0x4d678f26, 0xf854b522, 0x31907d62, 0x4b55dd99, 0x04d3658a, 0x19c1e69c, 0xc6112fdd, 0xc66699fd, 0xa0eabe6b, 0x3724d4dc, 0x0d28fe52, 0x46819e2e, 0x96f703ac, 0x21182ca3, 0x2e7b022f, 0x31860f13, 0x1956b1e5, 0xe1322228, 0x08063a85, 0x1d964589, 0x94d90481, 0x53c078f3, 0x5afb43ae, 0x1e3437f7, 0x877eb7b4, 0x5d67133c, 0xa385cb2c, 0xb82f2703, 0xef05ee06, 0x931dd7e2, 0x10d210aa, 0xe21339cc, 0x479c3a22, 0x613b67b2, 0x33c5321c, 0xa5f48ac4, 0xba5590c8, 0xeb244925, 0xd0ef3cc9, 0xd8c423fb, 0x15cfcc5c, 0x1feb2e4f, 0x36ec0ea3, 0xdbef7d9f, 0xd5ec6bf4, 0x3d3dff8a, 0x1e04a7f7, 0x8766bb54, 0x9a1d7745, 0xc79a1749, 0xb8d2486d, 0x582e3014, 0xa82427f5, 0x65dfa427, 0xbc5654c1, 0xbd086f26, 0x0451516d, 0xff4cfc35, 0x81f2864d, 0x31860f05, 0xd0638e1a, 0xb059261d, 0x3d1e9150, 0x21e2a51c, 0x82d53d12, 0x1010b275, 0x786b2908, 0x4d3cfbda, 0x94a302f4, 0x95c85eaa, 0xd7e1c7be, 0x82ac484f, 0xd24daa9a, 0x70055204, 0xbf27ef0d, 0x740ebb34, 0x47a1ff2f, 0x037e5e95, 0x3362d8d3, 0xb08c9e58, 0x7036b9a5, 0x3354197a, 0x326c9f12, 0xab99166e, 0x6c5d388b, 0xb222a768, 0x1bf121c5, 0x2ef76080, 0xb0658121, 0x8331bdd3, 0x64b5cd35, 0xc3c7fbe4, 0x576e7d5e, 0x1cbdc1b2, 0x5c54b675, 0xbffd76e3, 0x2ad7b53f, 0xe596de29, 0xc2d972db, 0xf1c92f34, 0x6af3ded6, 0xec317d66, 0x6a17bed5, 0x27750e9e, 0x8d950cb3, 0xb07688cc, 0x17a6ddd8, 0x5bf140e0, 0xed8713e0, 0x05890caa, 0xf66e4d73, 0x5ea0347f, 0xf535f2f5, 0x240c70f3, 0x5e922192, 0x8e59f944, 0x3185f7a7, 0x852dd342, 0x58c8e53b, 0x760071c5, 0xadb76f78, 0x185ab80c, 0x9d84df28, 0x4d2056da, 0x69bebccc, 0xa9fcb5f8, 0xb9d3ac81, 0xcc374aac, 0xc04e1ee9, 0x72634634, 0xb3bbf485, 0x1eb91de5, 0x5e8de602, 0x211cd561, 0xb03404e6, 0x051f3a53, 0xb263569c, 0x96cbc54f, 0x9eaf58e6, 0x32e9f0f2, 0x370f1cd9, 0x15bf840d, 0x1dbd5d06, 0xb04d214d, 0x6d1697ee, 0xc4b6fce1, 0x1b95f82d, 0x46979ca6, 0x0354cfc8, 0xd5950d3d, 0x10db605d, 0x18af3572, 0x990ec7a8, 0x2a0fe87f, 0x7937dbb8, 0xee376c86, 0xcc9f9070, 0xc953caa8, 0xd5c7c2ed, 0xee4f752e, 0x84f302f7, 0x1e08a48e, 0x44a5da35, 0x0fab83e2, 0xbb7cb9df, 0x2590afe1, 0xfef026aa, 0x83dbd643, 0xbe916d11, 0x27f7fba9, 0x82135d43, 0x6c5fa2a4, 0xe1e1370e, 0x51534581, 0x4cd9def3, 0xd94b4990, 0x74772379, 0x59264a1d, 0xc1dcdd8e, 0xed4ef1e9, 0xf29d901a, 0x68ecd0ad, 0x9ac31f92, 0x839a285e, 0x46447122, 0xc0e56c6e, 0xb09a4b83, 0xa9500b90, 0xda83c4e5, 0x4175b2f8, 0xeb4ddb4a, 0x236c6f2e, 0xeeb57a32, 0x2626e708, 0xa9d35265, 0x6ab3e9ab, 0xf12fcc1f, 0x1c317c43, 0x66c34fb3, 0xe17e58a0, 0x0295d4a1, 0x40cd40f9, 0x72700bb0, 0xd591e61e, 0x3e96b29b, 0xb50d5db3, 0xa3715dcc, 0x3405bcb4, 0x0e034d65, 0x210a4a2b, 0x7c302f2c, 0x24e8bef6, 0xa5135147, 0x0607ef80, 0x01f86c8f, 0x2c792c8a, 0x6ab73133, 0x6f436006, 0x09bf22a6, 0x1fde4622, 0x9841bd1c, 0xb23a7ad7, 0xdad579a4, 0x431229e9, 0xfa5dcb2d, 0x7da4f790, 0xa9b2c914, 0xcd191ced, 0x7a05e4aa, 0x73af1889, 0x192667b3, 0x538d4540, 0xacdbca81, 0x6b9d9e90, 0xc0462bba, 0xfcf5a7b9, 0x7b5c2904, 0x41a83c83, 0x7e69828f, 0x328a3bab, 0xdcd0f182, 0x1d113bcd, 0x1fb5c81c, 0x2d52afa0, 0x2d0c6111, 0x2a02ce14, 0x3fcd2c15, 0x54d574f9, 0xde57e605, 0x85dbb53d, 0xfc7cc003, 0x769c74d9, 0x6f834a4f, 0x79b3b87e, 0xe3d7c853, 0xa83e14b2, 0x3b1fc1ad, 0xb7dc2988, 0xb60ed652, 0xda3e3d1a, 0x5f2f680c, 0xb46e08da, 0x589b77da, 0xcef68535, 0x1c05d7a6, 0x24572da1, 0x939b02a5, 0xccd08d13, 0xdfa22970, 0xdff7581b, 0x2d5fade6, 0x5cfd3389, 0xce26cbb1, 0x376d7fd0, 0x02811e2e, 0xcc8030ab, 0xa7a4c9dc, 0x81db0ca7, 0x15a1bcef, 0x2045c6b5, 0x52c2f563, 0x6c829737, 0xb4f3384f, 0xb14d2f2b, 0xe102e00a, 0xba359973, 0x6045dd8b, 0xd0a5e531, 0xd679300f, 0xaabec63e, 0x526ad900, 0x95224f33, 0x723d6d44, 0x83584ad4, 0xa14ed869, 0x727bb03a, 0xdde37025, 0xb29d6587, 0xc3f3971d, 0x725933c2, 0x68f3eda4, 0xf73e9fdc, 0x944afd04, 0xa4c5e05f, 0x477f70ae, 0xffebfc90, 0xc21cff70, 0x84c68e32, 0xe82a3d6b, 0xba243867, 0x511c142f, 0x2062b8ac, 0x274a119f, 0x772afba2, 0x88a3974d, 0x205cf0de, 0xe45d5a2a, 0x1745056b, 0x2c8138f5, 0x982938a7, 0xfb265af9, 0x700c2cdf, 0x93e549b4, 0xb8abd353, 0xd74073e8, 0x289caf2a, 0x63e802e9, 0xc2f9adb7 }; /** * The output sequence generated by reference ISAAC algorithm in C language. * For initial seeding is used SEED_2 data array. * 1024 signed 32-bit integers in hexadecimal representation. */ private static final int[] EXPECTED_SEQUENCE_2 = { 0x67ae8a37, 0xa78322ea, 0xb9394933, 0x61f6e3c5, 0xbea576f1, 0xbb958f18, 0x12ce6479, 0xc593d5de, 0xdef281a0, 0x8435bb62, 0xf20b44db, 0x8a98479a, 0xbf2b8b66, 0x1080265e, 0xf0f8f12f, 0x021fa7f3, 0x81d2ed59, 0xb224a5f8, 0x0c1ff346, 0x92007ea8, 0x8fd1ce43, 0xeced69f5, 0x651fe09a, 0x45cf2c3e, 0x449b2b1e, 0x4f136be5, 0x8240cc97, 0xca979afa, 0x33b6a857, 0x7300f4f3, 0x79755d71, 0xcf11dd62, 0x916b7e04, 0x02076c0e, 0x9b4e3e68, 0x04836ed5, 0xf1b492c6, 0x887ef90c, 0x091b68f6, 0xaf7f0d3b, 0x89d7e5c1, 0x2b28fff7, 0xe6280e4f, 0x6681a805, 0xcb270bbb, 0x8e037463, 0x31a125f7, 0x0ba3c135, 0x7c2e8b3e, 0x6e21e06e, 0xc8b336ba, 0x08d677c3, 0x469fd05c, 0x71528649, 0x2024c602, 0x000e4f99, 0xb03395b1, 0x0a12d960, 0x68b15274, 0x7c415c07, 0x047c739b, 0x46658905, 0x45512a3c, 0x7f4cd2ff, 0x3d4d4ef6, 0xd7016dad, 0x6074bbf0, 0xbeaa55eb, 0xc519d326, 0x3ad349fd, 0x2fec4649, 0x14fa40ae, 0x96b51341, 0x2bf08ef1, 0xd1d374e4, 0x44168b14, 0xb01bee9b, 0x0b3f4332, 0xc799b9da, 0x76fc7dbd, 0x8c368a57, 0xe4cd2cad, 0xeeb0318a, 0xc2152908, 0x2b707a0e, 0x73457464, 0xc08e92a0, 0xfcdfca5b, 0x1320ed43, 0x333b77b9, 0x2e70948a, 0xa77d94f7, 0xbc1fb9fa, 0xa8ad15a1, 0x3c47b0f4, 0x867c4a8f, 0xb85784e0, 0x8a854e80, 0x456c8700, 0xc28f3a01, 0x415da6aa, 0x1315c6d8, 0x70a4ca70, 0xfdea940e, 0x686fbdc9, 0xda649eba, 0x661196f7, 0x795b5d27, 0xe10c78fa, 0x2fd89cf3, 0x61e850da, 0x00c49764, 0xee51d841, 0x00c18025, 0xdea163b3, 0x8b1b2080, 0x6abdd683, 0xe560c731, 0xc661b3e0, 0x23a3ff08, 0xa7579919, 0xfa443cba, 0x480bd220, 0x0a11740b, 0xb4b327c7, 0x831a0840, 0xb7c50ff3, 0x4266dff1, 0x835d0665, 0x52593038, 0x3917fb8e, 0x88c3b400, 0x05fb8823, 0xc9eaa89f, 0x6957fd17, 0x8dbcb8fe, 0xdec10c3c, 0x918d5fd8, 0x6af8619a, 0x8f472255, 0xc2f757b7, 0x9d62e804, 0x45e0e276, 0x482597d3, 0x06190214, 0x172b8501, 0xe91d7b8c, 0x4ee985fc, 0x3ecbf47a, 0xbbae5d98, 0x1f0bbdeb, 0x0d38208e, 0x6d4cb3e3, 0xa70050c4, 0xf0db408e, 0xddb6f1a7, 0x4bc4bc61, 0x90e1c1db, 0x203770dc, 0x39959247, 0xe2e0376a, 0xf70a8521, 0x81c759b2, 0x24d9915b, 0x09cc2ec3, 0x0fd0bff9, 0x58875636, 0xee78837e, 0x025a7eee, 0x4226859f, 0x85e21806, 0x9c1328bd, 0x0522fda0, 0x585441aa, 0x366f9ea0, 0xeb70934f, 0x0e394c41, 0xfa801419, 0x2b6d4c3e, 0xb09775fe, 0x3f0184ae, 0x3ace3518, 0xf80bf893, 0x9754753b, 0x78c46b93, 0x281e1918, 0x0dfcc5ee, 0xc0401601, 0xf8b11ce9, 0x9f799306, 0xb65c4232, 0x12ee4f73, 0xade72a42, 0x0ce54d71, 0xa6780e69, 0xe73bd8f9, 0xc245228f, 0x5fa2ed1a, 0x11627d1d, 0x2617ea2f, 0xd7404db6, 0x228fb877, 0xc5379535, 0xfe00008d, 0xc5f1491e, 0x1a3bdb0e, 0x9a90cc98, 0xa0abe3f5, 0xac7a0d18, 0x87bb3538, 0xa224baf7, 0xf2496ca4, 0x6a5b9bd6, 0x9a7da8d8, 0x72419677, 0xa36aec4d, 0x2a08ac0d, 0xfc4d7b21, 0x25f2aad0, 0x4f7146d4, 0xb4a603bd, 0x194e9792, 0x8f60cf1c, 0xed8ae037, 0xa47f90b1, 0x5eec55a3, 0x326c33d4, 0x6f79168f, 0xbcfc27fa, 0xd9e76d04, 0x79430e33, 0xd0c3b727, 0xd4bb06af, 0x8805066b, 0xaaef1130, 0x04958fef, 0x2e3270f4, 0xf5a8ffe8, 0x2a089c72, 0xff411bfc, 0xd6ed9552, 0x6253f5ef, 0x0c836c2f, 0xb79471b0, 0x127d177c, 0xf901cefa, 0xff75dc46, 0xde79ec4f, 0xe9f1f182, 0x9d28d8cd, 0xfcc98a94, 0x227670c2, 0x46b7c48c, 0x8fd77dcb, 0x60bc6d66, 0xe775322d, 0x0def2251, 0xf3dd14cb, 0x6c3f3468, 0x87696244, 0x10cca0be, 0x1d7fa716, 0x955b963b, 0xe53b6074, 0x77af9ec4, 0xfc856100, 0x91a06dc7, 0x8d55e3f1, 0xf8c805a3, 0xf3a1cb7a, 0xbcd51c6d, 0x301fdcdf, 0xdbcbcc54, 0x8b85fe57, 0x946d707e, 0x388a2ed4, 0xc4b93a5b, 0xd48631d2, 0xae2b4f28, 0x5b731392, 0xdf6e621e, 0xc4590c30, 0xa3a23cd5, 0xbfce9899, 0x4620cff9, 0x966c8c3f, 0x7a302556, 0x3fe549fa, 0x67533e77, 0x80250302, 0xcd899fe7, 0x694e77ea, 0x0879525d, 0xab6675e4, 0x763f8b35, 0x7684e6a1, 0x8fa35070, 0xe9fccaf1, 0x2d7195b7, 0x85b45186, 0xab799317, 0x2c84bd2c, 0xf8354c09, 0x02d96875, 0x8fdcc390, 0xf6af5aec, 0x2a584739, 0x8a1ba7e9, 0xea46f9b2, 0x98acd24f, 0xfc8a3a24, 0xa496eff9, 0x625c30ea, 0xc6ea0535, 0x3ed3b5d6, 0xffcd675d, 0x0b1719f6, 0x1b1c4e7b, 0x3206a672, 0x62fc1851, 0xa6a4c781, 0x78bbdbbe, 0x06c1c8ce, 0x5747c340, 0xfff7ab9c, 0xebaf9370, 0xf7b185a8, 0xf8309f84, 0xfa1601de, 0xf9fc8780, 0x59c2f8bd, 0xe74fcd5b, 0xf115f57f, 0xddda3332, 0x2ee56568, 0xa2243659, 0x9d6d578f, 0xbb507574, 0x95d44e0e, 0xdbdf2bc3, 0x0dc1b750, 0xc6a24241, 0x207d7115, 0xc337d024, 0x3817ef9a, 0xe9f12ccf, 0x4d67fc7d, 0x3da57a2b, 0x000e09a5, 0xe739c5a2, 0x7b7e1613, 0x23d576fe, 0x6941a210, 0x57521190, 0xdc4359c0, 0xd8eed2f8, 0x7862904f, 0xfc179a41, 0xeee2716e, 0x362cf76a, 0x0a087072, 0x3e6e2fa9, 0xaf2a0efb, 0x221d513f, 0xf054d856, 0xc3297367, 0x1c0998c8, 0xa664172f, 0xe2637c8e, 0xc17ac7d4, 0x0e041f43, 0x0d9c0ae4, 0x9346dacf, 0x7fb2a015, 0xe92276c2, 0x21478bfe, 0x119e2d0c, 0x5f76aeaf, 0xbe21aaec, 0x63174d5f, 0x13b796c3, 0x0fa0eba1, 0xe2f7277a, 0x3f555b42, 0x0215c7e4, 0x96266efa, 0x2953a4d1, 0xadfc171a, 0x396234a7, 0x560c0279, 0xefa6d2c6, 0xf48d9b5a, 0x4131c7b1, 0x9e302f70, 0x637c9f23, 0x22637330, 0x09927e76, 0x0898d1d6, 0x1b797274, 0x9ad491a2, 0xa2df3627, 0x012c3ed0, 0xc19c09d7, 0xa2fdaf56, 0x5b91f8fd, 0x3b7c49c9, 0x25694d29, 0xd7b42e9c, 0xa7be0053, 0xa91f1761, 0xd89e8b2a, 0x67846097, 0x76bd4006, 0xb8eb0712, 0x859bf877, 0xca42d70c, 0x24e80a69, 0xd92bc598, 0x55498c1e, 0x86deba8b, 0xf7c340b7, 0xa36caa12, 0x0631ddec, 0xc0146fe8, 0x2f959ef3, 0xf8400f0c, 0x58f676a0, 0x4ae4fe13, 0x9c4af056, 0x9e6f19d6, 0x12a9eb69, 0x1aeed54e, 0x34c91114, 0x97128045, 0x920d1f59, 0xffe7fbaa, 0x2db4a671, 0x6e6ff7aa, 0xd40d46bd, 0x1578f939, 0x15c5cbc6, 0xff356fd0, 0xd5d1680c, 0x5b11d14d, 0xe75541c0, 0x0fe2e2ba, 0x3ad55308, 0x8f036a69, 0xa9bfc3cd, 0x87685338, 0x510092b4, 0x1f66622a, 0x996337b2, 0xc531891f, 0x98371a93, 0xd9630100, 0x513ff133, 0xcf8381da, 0xed12e8e9, 0xe3ce7c7b, 0x8f731ab5, 0x511ba7c2, 0x9d754e87, 0x244603ac, 0xfd9985e1, 0xc1581765, 0x84e50a12, 0xa0ab0034, 0x63ee60c2, 0xdf5ab248, 0x09b42202, 0xca87f896, 0xca6ae5f0, 0xa569d829, 0x977cf29b, 0xd56a2a2f, 0x85ad1532, 0xfa2a131a, 0x00784081, 0x81f0e466, 0xebd340d3, 0xc37ad0e4, 0xd0aa6d7a, 0x36d2551f, 0xd6ff8448, 0xc7b89445, 0xa43421ad, 0x3be75400, 0x557a61ef, 0x0f519b14, 0x56503579, 0x1c8d164d, 0x0dcef35b, 0x3d9f1f2a, 0x56d056f2, 0x5d8fd4ec, 0xa481a350, 0x7cadd9c0, 0x70375ce2, 0x83263d2a, 0x5826ea3b, 0xfa523ce7, 0x50c9438b, 0x74fca95d, 0x62967ef5, 0x11fd6429, 0xcbb8e28c, 0x67fb9e81, 0xdc9e1147, 0xa29672f7, 0x1cf310f7, 0xb1e1d8e6, 0x3f862ff3, 0x6ade0327, 0xa92f3686, 0xed79f165, 0x359e1620, 0x36c68936, 0xe46fe521, 0x0c5e36b0, 0x6d9d9cdb, 0xc095eecd, 0x566dd305, 0x6d96cd36, 0x5d115a80, 0x2a9489a8, 0xdd067488, 0x73acf831, 0x7392c0f0, 0x30707838, 0x92826042, 0x67c54548, 0xf830434d, 0xebe67854, 0xaefc9a41, 0xcabf703f, 0x5242c77f, 0x1f3867a9, 0x48174739, 0x8657c39e, 0xa11247e2, 0xb4e6624d, 0xc7ffe78a, 0x1e11a841, 0x6690244b, 0x8dcc9292, 0x5ce4dcc4, 0xebcba02d, 0x2ef6503d, 0x4fb880bc, 0xb949a759, 0x7bb18a1e, 0x5973d2e8, 0x577ad8a6, 0xa9d4992e, 0x1a248a0c, 0xcc4450ed, 0x7e0178d3, 0xe98a8f3f, 0x209fb330, 0xf7bf40fc, 0x632231b3, 0x7055fdaf, 0x7719e655, 0xf8d49413, 0xc200aa04, 0x8a41183a, 0xdfa217c1, 0xcd0c165d, 0x08fec61c, 0xef608048, 0xe19fae2c, 0xedc6f3ea, 0x859a69f9, 0x5f96c76d, 0x571aec69, 0x9cfe7fa4, 0x692baf70, 0xbb143cb4, 0xe8968678, 0xfcb77411, 0x02d3268d, 0xcdc3daa3, 0x514e78e9, 0xa231a480, 0x8ac10400, 0xe19a2ca1, 0xfa790fe1, 0x808fec9c, 0xe4760960, 0x62e9d051, 0x5c4b006b, 0x22eb9703, 0x426b5907, 0xfa1cd338, 0xa3b4811a, 0xad6185c1, 0x349efbc0, 0xeee28d42, 0x02531fc5, 0xd11b2c4d, 0x5b3bf865, 0xf4823687, 0x4f92b6b7, 0xfb641c60, 0x0c526fa9, 0x42438de8, 0xd5cbf7a0, 0x54ad0d1f, 0xb4e63f09, 0x666285eb, 0xe7ec0275, 0x57e7225a, 0xafe6b0e3, 0x17431cd7, 0x33bc9204, 0x8a9cbdde, 0x94d8fe7d, 0xc943f36c, 0x1348c3c6, 0x43cf9b8c, 0x5a84ae20, 0x6d372dea, 0xdb0b3c92, 0xf0f2a72d, 0x473a1fe7, 0x062416df, 0x0a12c61c, 0x3680c102, 0x8d0189db, 0x0824325f, 0xffb97ead, 0x0d8d353f, 0x4a4e6ec2, 0x76243bb7, 0xdabfbeee, 0xcd8410d7, 0xa30f17c3, 0x2b59ceef, 0xda27f7c0, 0x791d813b, 0xc0516741, 0xb363e4ff, 0x31ddbfb7, 0x49db1590, 0xd843513c, 0x8d317a75, 0xb24387df, 0x63fd4066, 0xa0fce498, 0x7b42de97, 0x30eddc0c, 0x071ad222, 0x3a9054c4, 0x5ce35298, 0x375be64b, 0x10af32c8, 0xa999ade1, 0xfa9f4d31, 0xfbe24a2a, 0x4c92714b, 0xcce3056a, 0xa81d616a, 0x3bb49213, 0x72fd2b0e, 0x1b46d17e, 0x92159bc7, 0x7462e172, 0x4fdc3e05, 0xf309c063, 0x9133532c, 0xe62d9341, 0x681a4871, 0xb1598525, 0x498ca388, 0x96a7ea81, 0x791c8a85, 0x2a33a1e2, 0x1e6abc87, 0xb21a4878, 0x65fac53b, 0x59162ae1, 0x22858a30, 0x40f4e569, 0xe5cb0023, 0x626cd2a0, 0xfe6d8fc8, 0xbb7ed7c3, 0x9a557393, 0xd0ff5e60, 0x2a20ed9b, 0x4eaafb5a, 0xbe9425bd, 0x63620ce1, 0x31ea24ed, 0x082e426a, 0x7ff35a73, 0xa67fbaa9, 0xd2e3c5b9, 0x1a90e96a, 0x71f19184, 0xb836b88b, 0xe51fa187, 0x42576438, 0x58d28776, 0x47bd92a3, 0x09816862, 0x295138ef, 0x23ab2bb1, 0xd7c584e0, 0x1793062f, 0xcc47e852, 0xc2eb9703, 0xe6812d93, 0xa4aa4d2e, 0x7f635b79, 0xa7407b29, 0x9724c087, 0x406e08ce, 0x6bf1d8b7, 0x9ef5b815, 0xf2c6f094, 0x86269ca2, 0x17fdaa4f, 0x9b645b61, 0x701bbbeb, 0x8de7bcb7, 0xd468266a, 0x48df44ae, 0x570b08ca, 0x7a5ba43b, 0xfc927312, 0x3461a3dd, 0x0ffe5943, 0x87060375, 0x8d8afed7, 0x83d20387, 0x77eabb51, 0xf86d045b, 0x71a47537, 0xa4485ea8, 0xfd2b6ac3, 0xb4ba1fcf, 0x31dcee82, 0x8b41cdf6, 0xeacde42b, 0x02de5fbb, 0xb6311aa8, 0x1596ee5d, 0x355cc39d, 0xbe1a87c1, 0x01e1df07, 0xfe413d46, 0x5e5e13ab, 0x30233fd6, 0x99449292, 0x34955dcc, 0x1f37d394, 0xd43639bd, 0x65c98eee, 0x67b85593, 0x1660b2a1, 0xfd86e9a0, 0x33bb6e5a, 0xdd5892fb, 0xa6832091, 0xd077d216, 0x353bfe9a, 0xb4a10726, 0xca1a536e, 0xed8af6c1, 0x41d167d1, 0x5f554941, 0x93f4032a, 0x83d83ae5, 0xc8866a05, 0xc36d1e1f, 0x95a082c5, 0xd85e6cad, 0x302bc384, 0x41fb8717, 0x61221cc8, 0xce9a44cd, 0x2884ec21, 0x9712152d, 0x419e4939, 0x32367b47, 0x238ee432, 0xd27f0b8e, 0xa3eaed24, 0x1eefd0a9, 0xf38a2400, 0x72c0d4b2, 0x8bdbdec1, 0x563a2b59, 0x0d50177b, 0xb01576ef, 0x7dd9f33e, 0x7905c120, 0x461712d6, 0x78265e93, 0x54a91f0b, 0xb88eb9c0, 0x9d8997af, 0xcb1d1296, 0xfa0a3a77, 0x5bb26dd9, 0xf6da78df, 0x53b8e80c, 0xc55cdaf6, 0x871a3971, 0x0bd8d322, 0xfa9e0a9c, 0x95949e0e, 0xe94f0edb, 0x15e06b25, 0x8b3e34cc, 0x980261a9, 0x3a8fe440, 0xc72330aa, 0xbff5c8b6, 0x486a08e6, 0x6b3f0668, 0x53c90761, 0x1dc2374b, 0xba623bb6, 0x720d9fff, 0x8454fada, 0x29f09563, 0x6512330e, 0x84370042, 0xda55c14b, 0xe6397b27, 0xdb03bcc7, 0xd6e27986, 0x483ad4f2, 0xe0e2c39a, 0x459e6792, 0x03c120ec, 0x13df7847, 0xc3ee77e1, 0xbcee7cd4, 0xdb3734ec, 0x0e19ebb2, 0x1501517a, 0x815190f7, 0xea30ba2c, 0xed58523c, 0x9dc64c08, 0x58d8753f, 0x1fa771c5, 0x7721fb09, 0xc64d1f60, 0xf407dc18, 0x6fdb1e33, 0x89abccb8, 0x2fab8715, 0xd8ee352e, 0x41bfa764, 0xda0267ee, 0x65794080, 0xe3095d65, 0x08e2148b, 0x173103b5, 0x55673978, 0x8d76b213, 0x6ed42e4b, 0xbe589395, 0xcf4c4d8a, 0xd331b237, 0x0af2f4cb, 0x202be7fa, 0x2e87bc27, 0x140a95df, 0xa0d1ef7e, 0x1031da30, 0x630f3ea6, 0x0e3b0991, 0xba7c0462, 0xca8a192c, 0x668417e2, 0x2c6e8ec5, 0x3f2e4372, 0x310927e9, 0xa87b5f4e, 0x21e3f285, 0x66aab4be, 0x96804f73, 0x097c363b, 0x76445811, 0xaf92fb77, 0x660010b7, 0x3ff5abbb, 0xdaa505d0, 0x17dc3488, 0x45dac66a, 0xa834d6eb, 0xacf0641f, 0x05576174, 0x28bf1858, 0x08829e92, 0xd3c5d530, 0x6acd00b2, 0xf36c0645, 0x4385cae7, 0x93b11f88, 0x3dfe1da7, 0x2d5df9d8, 0xd51d498f, 0x1d122b84, 0x2ca7619f, 0x670fba3a, 0xa59f3019, 0xd25ade01, 0x43ef2f88, 0x05cf6af4, 0x6fc3b5e0, 0x305a309e, 0xb7bce57b, 0x49c55693, 0xd59bd6d0, 0xdf13274c, 0xa5640917, 0xb8e88520, 0xf81fb865, 0x245967cf, 0x64420112, 0x97720fd0, 0x0ef913f8, 0x9fcf14f4, 0x99a86a60, 0x150ae075, 0x1b4be51e, 0x12fe7368, 0x23781feb, 0x3657b0c3, 0x90f84f92, 0x082f626d, 0xf057cef1, 0xf04fc2c1, 0x8767f311, 0x240ab838, 0x160564c0, 0x96f4460d, 0x2c7e5c83, 0xb44e6da1, 0x43f86ae1, 0xe3afdf03, 0x34173462, 0x197d8030, 0xb02d6ae7, 0x0cec076c, 0x0fb9d05e, 0x1fa74d99, 0x03f9636e, 0x03afa44d, 0x79ceb46c, 0x8b9e3158, 0xad87d846, 0xaf794612, 0xdd00ae31, 0xde8d63de, 0x229c5e66, 0x2df46e14, 0x3cbb35d1, 0x9832a55b, 0xaff3c01c, 0xaf4cc2be, 0x05095c2b, 0xee6be44f, 0x7b9bd378, 0x09a5f11f, 0xfddc340a, 0x010da0fa, 0x60874e63, 0x9f03a38b, 0xddfe1c05, 0x8dadcc16, 0x6df97114, 0xe0779adc, 0x8de82987, 0x83cca69c, 0x38b19e7c, 0xebc30d07, 0xb36f46cc, 0xee4d1453, 0x7522c310, 0x3a43d376, 0xab400f15, 0x4fedfa99, 0x02e7323e, 0x4c57f680, 0xe4190ae5, 0x6a5bba49, 0xd3c223d8, 0x1b87ab96, 0xaef4795f, 0xf457cd2d, 0x2bae8689, 0xa229c48f, 0x41bd5e74, 0x25cb3da8, 0xfd47e4d0, 0x0a241ffc, 0x16c0dba7, 0x6f1469fd, 0x810c16da, 0x66a7b33c, 0xe6c9e001, 0x9ccefde6, 0xd24f7adc, 0x1bcc8980, 0x37084252, 0xb779d5cd, 0x52e456d0, 0x313ba4de, 0xffb09943, 0x0e9d4e1f, 0x5a3c51ac, 0x6f055f04, 0xb2ac9a26, 0xb7fac64f, 0x27cc0c8d, 0x342bbac3 }; @Test public void testReference() { ISAACRandom isaacRandom = new ISAACRandom(SEED_1); int[] actualSequence = getActualSequence(isaacRandom); Assert.assertArrayEquals(EXPECTED_SEQUENCE_1, actualSequence); isaacRandom.setSeed(SEED_2); actualSequence = getActualSequence(isaacRandom); Assert.assertArrayEquals(EXPECTED_SEQUENCE_2, actualSequence); } private int[] getActualSequence(ISAACRandom isaacRandom) { int[] actualSequence = new int[1024]; for (int i = 0; i < actualSequence.length; i++) { actualSequence[i] = isaacRandom.nextInt(); } return actualSequence; } } ././@LongLink100644 0 0 155 12126630646 10262 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/random/CorrelatedRandomVectorGeneratorTest.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/random/CorrelatedRandomVectorGeneratorT100644 1750 1750 17303 12126627670 32412 0ustarlucluc 0 0 //Licensed to the Apache Software Foundation (ASF) under one //or more contributor license agreements. See the NOTICE file //distributed with this work for additional information //regarding copyright ownership. The ASF licenses this file //to you 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. package org.apache.commons.math3.random; import java.util.Arrays; import org.apache.commons.math3.TestUtils; import org.apache.commons.math3.linear.Array2DRowRealMatrix; import org.apache.commons.math3.linear.MatrixUtils; import org.apache.commons.math3.linear.RealMatrix; import org.apache.commons.math3.stat.correlation.StorelessCovariance; import org.apache.commons.math3.stat.descriptive.moment.VectorialCovariance; import org.apache.commons.math3.stat.descriptive.moment.VectorialMean; import org.apache.commons.math3.util.FastMath; import org.junit.Test; import org.junit.Assert; public class CorrelatedRandomVectorGeneratorTest { private double[] mean; private RealMatrix covariance; private CorrelatedRandomVectorGenerator generator; public CorrelatedRandomVectorGeneratorTest() { mean = new double[] { 0.0, 1.0, -3.0, 2.3 }; RealMatrix b = MatrixUtils.createRealMatrix(4, 3); int counter = 0; for (int i = 0; i < b.getRowDimension(); ++i) { for (int j = 0; j < b.getColumnDimension(); ++j) { b.setEntry(i, j, 1.0 + 0.1 * ++counter); } } RealMatrix bbt = b.multiply(b.transpose()); covariance = MatrixUtils.createRealMatrix(mean.length, mean.length); for (int i = 0; i < covariance.getRowDimension(); ++i) { covariance.setEntry(i, i, bbt.getEntry(i, i)); for (int j = 0; j < covariance.getColumnDimension(); ++j) { double s = bbt.getEntry(i, j); covariance.setEntry(i, j, s); covariance.setEntry(j, i, s); } } RandomGenerator rg = new JDKRandomGenerator(); rg.setSeed(17399225432l); GaussianRandomGenerator rawGenerator = new GaussianRandomGenerator(rg); generator = new CorrelatedRandomVectorGenerator(mean, covariance, 1.0e-12 * covariance.getNorm(), rawGenerator); } @Test public void testRank() { Assert.assertEquals(2, generator.getRank()); } @Test public void testMath226() { double[] mean = { 1, 1, 10, 1 }; double[][] cov = { { 1, 3, 2, 6 }, { 3, 13, 16, 2 }, { 2, 16, 38, -1 }, { 6, 2, -1, 197 } }; RealMatrix covRM = MatrixUtils.createRealMatrix(cov); JDKRandomGenerator jg = new JDKRandomGenerator(); jg.setSeed(5322145245211l); NormalizedRandomGenerator rg = new GaussianRandomGenerator(jg); CorrelatedRandomVectorGenerator sg = new CorrelatedRandomVectorGenerator(mean, covRM, 0.00001, rg); double[] min = new double[mean.length]; Arrays.fill(min, Double.POSITIVE_INFINITY); double[] max = new double[mean.length]; Arrays.fill(max, Double.NEGATIVE_INFINITY); for (int i = 0; i < 10; i++) { double[] generated = sg.nextVector(); for (int j = 0; j < generated.length; ++j) { min[j] = FastMath.min(min[j], generated[j]); max[j] = FastMath.max(max[j], generated[j]); } } for (int j = 0; j < min.length; ++j) { Assert.assertTrue(max[j] - min[j] > 2.0); } } @Test public void testRootMatrix() { RealMatrix b = generator.getRootMatrix(); RealMatrix bbt = b.multiply(b.transpose()); for (int i = 0; i < covariance.getRowDimension(); ++i) { for (int j = 0; j < covariance.getColumnDimension(); ++j) { Assert.assertEquals(covariance.getEntry(i, j), bbt.getEntry(i, j), 1.0e-12); } } } @Test public void testMeanAndCovariance() { VectorialMean meanStat = new VectorialMean(mean.length); VectorialCovariance covStat = new VectorialCovariance(mean.length, true); for (int i = 0; i < 5000; ++i) { double[] v = generator.nextVector(); meanStat.increment(v); covStat.increment(v); } double[] estimatedMean = meanStat.getResult(); RealMatrix estimatedCovariance = covStat.getResult(); for (int i = 0; i < estimatedMean.length; ++i) { Assert.assertEquals(mean[i], estimatedMean[i], 0.07); for (int j = 0; j <= i; ++j) { Assert.assertEquals(covariance.getEntry(i, j), estimatedCovariance.getEntry(i, j), 0.1 * (1.0 + FastMath.abs(mean[i])) * (1.0 + FastMath.abs(mean[j]))); } } } @Test public void testSampleWithZeroCovariance() { final double[][] covMatrix1 = new double[][]{ {0.013445532, 0.010394690, 0.009881156, 0.010499559}, {0.010394690, 0.023006616, 0.008196856, 0.010732709}, {0.009881156, 0.008196856, 0.019023866, 0.009210099}, {0.010499559, 0.010732709, 0.009210099, 0.019107243} }; final double[][] covMatrix2 = new double[][]{ {0.0, 0.0, 0.0, 0.0, 0.0}, {0.0, 0.013445532, 0.010394690, 0.009881156, 0.010499559}, {0.0, 0.010394690, 0.023006616, 0.008196856, 0.010732709}, {0.0, 0.009881156, 0.008196856, 0.019023866, 0.009210099}, {0.0, 0.010499559, 0.010732709, 0.009210099, 0.019107243} }; final double[][] covMatrix3 = new double[][]{ {0.013445532, 0.010394690, 0.0, 0.009881156, 0.010499559}, {0.010394690, 0.023006616, 0.0, 0.008196856, 0.010732709}, {0.0, 0.0, 0.0, 0.0, 0.0}, {0.009881156, 0.008196856, 0.0, 0.019023866, 0.009210099}, {0.010499559, 0.010732709, 0.0, 0.009210099, 0.019107243} }; testSampler(covMatrix1, 10000, 0.001); testSampler(covMatrix2, 10000, 0.001); testSampler(covMatrix3, 10000, 0.001); } private CorrelatedRandomVectorGenerator createSampler(double[][] cov) { RealMatrix matrix = new Array2DRowRealMatrix(cov); double small = 10e-12 * matrix.getNorm(); return new CorrelatedRandomVectorGenerator( new double[cov.length], matrix, small, new GaussianRandomGenerator(new JDKRandomGenerator())); } private void testSampler(final double[][] covMatrix, int samples, double epsilon) { CorrelatedRandomVectorGenerator sampler = createSampler(covMatrix); StorelessCovariance cov = new StorelessCovariance(covMatrix.length); for (int i = 0; i < samples; ++i) { cov.increment(sampler.nextVector()); } double[][] sampleCov = cov.getData(); for (int r = 0; r < covMatrix.length; ++r) { TestUtils.assertEquals(covMatrix[r], sampleCov[r], epsilon); } } } ././@LongLink100644 0 0 151 12126630646 10256 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/random/SynchronizedRandomGeneratorTest.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/random/SynchronizedRandomGeneratorTest.100644 1750 1750 10566 12126627670 32420 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.random; import java.util.ArrayList; import java.util.List; import java.util.concurrent.Callable; import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.Future; import org.junit.Assert; import org.junit.Test; public class SynchronizedRandomGeneratorTest { private final int numberOfThreads = 5; private final int numberOfGenerators = 5; private final int numberOfSamples = 100000; @Test public void testAdapter() { final int seed = 12345; final RandomGenerator orig = new MersenneTwister(seed); final RandomGenerator wrap = new SynchronizedRandomGenerator(new MersenneTwister(seed)); final int bSize = 67; final byte[] bOrig = new byte[bSize]; final byte[] bWrap = new byte[bSize]; for (int i = 0; i < 100; i++) { orig.nextBytes(bOrig); wrap.nextBytes(bWrap); for (int k = 0; k < bSize; k++) { Assert.assertEquals(bOrig[k], bWrap[k]); } Assert.assertEquals(orig.nextInt(), wrap.nextInt()); final int range = (i + 1) * 89; Assert.assertEquals(orig.nextInt(range), wrap.nextInt(range)); Assert.assertEquals(orig.nextLong(), wrap.nextLong()); Assert.assertEquals(orig.nextBoolean(), wrap.nextBoolean()); Assert.assertEquals(orig.nextFloat(), wrap.nextFloat(), 0); Assert.assertEquals(orig.nextDouble(), wrap.nextDouble(), 0); Assert.assertEquals(orig.nextGaussian(), wrap.nextGaussian(), 0); } } @Test public void testMath899Sync() throws Throwable { try { // Running the test several times in order to decrease the // probability that a non-thread-safe code did not trigger // a concurrency problem. for (int i = 0; i < 10; i++) { doTestMath899(true, numberOfThreads, numberOfGenerators, numberOfSamples); } } catch (InterruptedException e) { Assert.fail(e.getMessage()); } catch (ExecutionException e) { throw e.getCause(); } } /** * @param sync Whether to use a synchronizing wrapper. */ private double[] doTestMath899(final boolean sync, final int numThreads, final int numGenerators, final int numSamples) throws InterruptedException, ExecutionException { final RandomGenerator rng = new MersenneTwister(); final RandomGenerator wrapper = sync ? new SynchronizedRandomGenerator(rng) : rng; final List> tasks = new ArrayList>(); for (int i = 0; i < numGenerators; i++) { tasks.add(new Callable() { public Double call() { Double lastValue = 0d; for (int j = 0; j < numSamples; j++) { lastValue = wrapper.nextGaussian(); } return lastValue; } }); } final ExecutorService exec = Executors.newFixedThreadPool(numThreads); final List> results = exec.invokeAll(tasks); final double[] values = new double[numGenerators]; for (int i = 0; i < numGenerators; i++) { values[i] = results.get(i).get(); } return values; } } ././@LongLink100644 0 0 145 12126630646 10261 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/random/GaussianRandomGeneratorTest.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/random/GaussianRandomGeneratorTest.java100644 1750 1750 2730 12126627670 32327 0ustarlucluc 0 0 //Licensed to the Apache Software Foundation (ASF) under one //or more contributor license agreements. See the NOTICE file //distributed with this work for additional information //regarding copyright ownership. The ASF licenses this file //to you 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. package org.apache.commons.math3.random; import org.apache.commons.math3.stat.StatUtils; import org.junit.Assert; import org.junit.Test; public class GaussianRandomGeneratorTest { @Test public void testMeanAndStandardDeviation() { RandomGenerator rg = new JDKRandomGenerator(); rg.setSeed(17399225432l); GaussianRandomGenerator generator = new GaussianRandomGenerator(rg); double[] sample = new double[10000]; for (int i = 0; i < sample.length; ++i) { sample[i] = generator.nextNormalizedDouble(); } Assert.assertEquals(0.0, StatUtils.mean(sample), 0.012); Assert.assertEquals(1.0, StatUtils.variance(sample), 0.01); } } commons-math3-3.2-src/src/test/java/org/apache/commons/math3/random/StableRandomGeneratorTest.java100644 1750 1750 10442 12126627670 32006 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.random; import org.apache.commons.math3.exception.OutOfRangeException; import org.apache.commons.math3.stat.StatUtils; import org.apache.commons.math3.stat.descriptive.DescriptiveStatistics; import org.junit.Assert; import org.junit.Test; /** * The class StableRandomGeneratorTest contains tests for the class * {@link StableRandomGenerator} * * @version $Revision: 1244107 $ */ public class StableRandomGeneratorTest { private RandomGenerator rg = new Well19937c(100); private final static int sampleSize = 10000; /** * Run the double nextDouble() method test Due to leptokurtic property the * acceptance range is widened. * * TODO: verify that tolerance this wide is really OK */ @Test public void testNextDouble() { StableRandomGenerator generator = new StableRandomGenerator(rg, 1.3, 0.1); double[] sample = new double[2 * sampleSize]; for (int i = 0; i < sample.length; ++i) { sample[i] = generator.nextNormalizedDouble(); } Assert.assertEquals(0.0, StatUtils.mean(sample), 0.3); } /** * If alpha = 2, than it must be Gaussian distribution */ @Test public void testGaussianCase() { StableRandomGenerator generator = new StableRandomGenerator(rg, 2d, 0.0); double[] sample = new double[sampleSize]; for (int i = 0; i < sample.length; ++i) { sample[i] = generator.nextNormalizedDouble(); } Assert.assertEquals(0.0, StatUtils.mean(sample), 0.02); Assert.assertEquals(1.0, StatUtils.variance(sample), 0.02); } /** * If alpha = 1, than it must be Cauchy distribution */ @Test public void testCauchyCase() { StableRandomGenerator generator = new StableRandomGenerator(rg, 1d, 0.0); DescriptiveStatistics summary = new DescriptiveStatistics(); for (int i = 0; i < sampleSize; ++i) { double sample = generator.nextNormalizedDouble(); summary.addValue(sample); } // Standard Cauchy distribution should have zero median and mode double median = summary.getPercentile(50); Assert.assertEquals(0.0, median, 0.2); } /** * Input parameter range tests */ @Test public void testAlphaRangeBelowZero() { try { new StableRandomGenerator(rg, -1.0, 0.0); Assert.fail("Expected OutOfRangeException"); } catch (OutOfRangeException e) { Assert.assertEquals(-1.0, e.getArgument()); } } @Test public void testAlphaRangeAboveTwo() { try { new StableRandomGenerator(rg, 3.0, 0.0); Assert.fail("Expected OutOfRangeException"); } catch (OutOfRangeException e) { Assert.assertEquals(3.0, e.getArgument()); } } @Test public void testBetaRangeBelowMinusOne() { try { new StableRandomGenerator(rg, 1.0, -2.0); Assert.fail("Expected OutOfRangeException"); } catch (OutOfRangeException e) { Assert.assertEquals(-2.0, e.getArgument()); } } @Test public void testBetaRangeAboveOne() { try { new StableRandomGenerator(rg, 1.0, 2.0); Assert.fail("Expected OutOfRangeException"); } catch (OutOfRangeException e) { Assert.assertEquals(2.0, e.getArgument()); } } }commons-math3-3.2-src/src/test/java/org/apache/commons/math3/random/RandomDataGeneratorTest.java100644 1750 1750 135663 12126627670 31502 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.random; import java.text.DecimalFormat; import java.util.ArrayList; import java.util.HashSet; import java.util.List; import org.apache.commons.math3.Retry; import org.apache.commons.math3.RetryRunner; import org.apache.commons.math3.TestUtils; import org.apache.commons.math3.distribution.BetaDistribution; import org.apache.commons.math3.distribution.BinomialDistribution; import org.apache.commons.math3.distribution.BinomialDistributionTest; import org.apache.commons.math3.distribution.CauchyDistribution; import org.apache.commons.math3.distribution.ChiSquaredDistribution; import org.apache.commons.math3.distribution.ExponentialDistribution; import org.apache.commons.math3.distribution.FDistribution; import org.apache.commons.math3.distribution.GammaDistribution; import org.apache.commons.math3.distribution.HypergeometricDistribution; import org.apache.commons.math3.distribution.HypergeometricDistributionTest; import org.apache.commons.math3.distribution.NormalDistribution; import org.apache.commons.math3.distribution.PascalDistribution; import org.apache.commons.math3.distribution.PascalDistributionTest; import org.apache.commons.math3.distribution.PoissonDistribution; import org.apache.commons.math3.distribution.TDistribution; import org.apache.commons.math3.distribution.WeibullDistribution; import org.apache.commons.math3.distribution.ZipfDistribution; import org.apache.commons.math3.distribution.ZipfDistributionTest; import org.apache.commons.math3.stat.Frequency; import org.apache.commons.math3.stat.descriptive.SummaryStatistics; import org.apache.commons.math3.stat.inference.ChiSquareTest; import org.apache.commons.math3.util.FastMath; import org.apache.commons.math3.exception.MathIllegalArgumentException; import org.junit.Assert; import org.junit.Test; import org.junit.runner.RunWith; /** * Test cases for the RandomDataGenerator class. * * @version $Id: RandomDataGeneratorTest.java 1457491 2013-03-17 17:15:31Z psteitz $ */ @RunWith(RetryRunner.class) public class RandomDataGeneratorTest { public RandomDataGeneratorTest() { randomData = new RandomDataGenerator(); randomData.reSeed(1000); } protected final long smallSampleSize = 1000; protected final double[] expected = { 250, 250, 250, 250 }; protected final int largeSampleSize = 10000; private final String[] hex = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f" }; protected RandomDataGenerator randomData = null; protected final ChiSquareTest testStatistic = new ChiSquareTest(); @Test public void testNextIntExtremeValues() { int x = randomData.nextInt(Integer.MIN_VALUE, Integer.MAX_VALUE); int y = randomData.nextInt(Integer.MIN_VALUE, Integer.MAX_VALUE); Assert.assertFalse(x == y); } @Test public void testNextLongExtremeValues() { long x = randomData.nextLong(Long.MIN_VALUE, Long.MAX_VALUE); long y = randomData.nextLong(Long.MIN_VALUE, Long.MAX_VALUE); Assert.assertFalse(x == y); } @Test public void testNextUniformExtremeValues() { double x = randomData.nextUniform(-Double.MAX_VALUE, Double.MAX_VALUE); double y = randomData.nextUniform(-Double.MAX_VALUE, Double.MAX_VALUE); Assert.assertFalse(x == y); Assert.assertFalse(Double.isNaN(x)); Assert.assertFalse(Double.isNaN(y)); Assert.assertFalse(Double.isInfinite(x)); Assert.assertFalse(Double.isInfinite(y)); } @Test public void testNextIntIAE() { try { randomData.nextInt(4, 3); Assert.fail("MathIllegalArgumentException expected"); } catch (MathIllegalArgumentException ex) { // ignored } } @Test public void testNextIntNegativeToPositiveRange() { for (int i = 0; i < 5; i++) { checkNextIntUniform(-3, 5); checkNextIntUniform(-3, 6); } } @Test public void testNextIntNegativeRange() { for (int i = 0; i < 5; i++) { checkNextIntUniform(-7, -4); checkNextIntUniform(-15, -2); checkNextIntUniform(Integer.MIN_VALUE + 1, Integer.MIN_VALUE + 12); } } @Test public void testNextIntPositiveRange() { for (int i = 0; i < 5; i++) { checkNextIntUniform(0, 3); checkNextIntUniform(2, 12); checkNextIntUniform(1,2); checkNextIntUniform(Integer.MAX_VALUE - 12, Integer.MAX_VALUE - 1); } } private void checkNextIntUniform(int min, int max) { final Frequency freq = new Frequency(); for (int i = 0; i < smallSampleSize; i++) { final int value = randomData.nextInt(min, max); Assert.assertTrue("nextInt range", (value >= min) && (value <= max)); freq.addValue(value); } final int len = max - min + 1; final long[] observed = new long[len]; for (int i = 0; i < len; i++) { observed[i] = freq.getCount(min + i); } final double[] expected = new double[len]; for (int i = 0; i < len; i++) { expected[i] = 1d / len; } TestUtils.assertChiSquareAccept(expected, observed, 0.001); } @Test public void testNextIntWideRange() { int lower = -0x6543210F; int upper = 0x456789AB; int max = Integer.MIN_VALUE; int min = Integer.MAX_VALUE; for (int i = 0; i < 1000000; ++i) { int r = randomData.nextInt(lower, upper); max = FastMath.max(max, r); min = FastMath.min(min, r); Assert.assertTrue(r >= lower); Assert.assertTrue(r <= upper); } double ratio = (((double) max) - ((double) min)) / (((double) upper) - ((double) lower)); Assert.assertTrue(ratio > 0.99999); } @Test public void testNextLongIAE() { try { randomData.nextLong(4, 3); Assert.fail("MathIllegalArgumentException expected"); } catch (MathIllegalArgumentException ex) { // ignored } } @Test public void testNextLongNegativeToPositiveRange() { for (int i = 0; i < 5; i++) { checkNextLongUniform(-3, 5); checkNextLongUniform(-3, 6); } } @Test public void testNextLongNegativeRange() { for (int i = 0; i < 5; i++) { checkNextLongUniform(-7, -4); checkNextLongUniform(-15, -2); checkNextLongUniform(Long.MIN_VALUE + 1, Long.MIN_VALUE + 12); } } @Test public void testNextLongPositiveRange() { for (int i = 0; i < 5; i++) { checkNextLongUniform(0, 3); checkNextLongUniform(2, 12); checkNextLongUniform(Long.MAX_VALUE - 12, Long.MAX_VALUE - 1); } } private void checkNextLongUniform(long min, long max) { final Frequency freq = new Frequency(); for (int i = 0; i < smallSampleSize; i++) { final long value = randomData.nextLong(min, max); Assert.assertTrue("nextLong range: " + value + " " + min + " " + max, (value >= min) && (value <= max)); freq.addValue(value); } final int len = ((int) (max - min)) + 1; final long[] observed = new long[len]; for (int i = 0; i < len; i++) { observed[i] = freq.getCount(min + i); } final double[] expected = new double[len]; for (int i = 0; i < len; i++) { expected[i] = 1d / len; } TestUtils.assertChiSquareAccept(expected, observed, 0.01); } @Test public void testNextLongWideRange() { long lower = -0x6543210FEDCBA987L; long upper = 0x456789ABCDEF0123L; long max = Long.MIN_VALUE; long min = Long.MAX_VALUE; for (int i = 0; i < 10000000; ++i) { long r = randomData.nextLong(lower, upper); max = FastMath.max(max, r); min = FastMath.min(min, r); Assert.assertTrue(r >= lower); Assert.assertTrue(r <= upper); } double ratio = (((double) max) - ((double) min)) / (((double) upper) - ((double) lower)); Assert.assertTrue(ratio > 0.99999); } @Test public void testNextSecureLongIAE() { try { randomData.nextSecureLong(4, 3); Assert.fail("MathIllegalArgumentException expected"); } catch (MathIllegalArgumentException ex) { // ignored } } @Test @Retry(3) public void testNextSecureLongNegativeToPositiveRange() { for (int i = 0; i < 5; i++) { checkNextSecureLongUniform(-3, 5); checkNextSecureLongUniform(-3, 6); } } @Test @Retry(3) public void testNextSecureLongNegativeRange() { for (int i = 0; i < 5; i++) { checkNextSecureLongUniform(-7, -4); checkNextSecureLongUniform(-15, -2); } } @Test @Retry(3) public void testNextSecureLongPositiveRange() { for (int i = 0; i < 5; i++) { checkNextSecureLongUniform(0, 3); checkNextSecureLongUniform(2, 12); } } private void checkNextSecureLongUniform(int min, int max) { final Frequency freq = new Frequency(); for (int i = 0; i < smallSampleSize; i++) { final long value = randomData.nextSecureLong(min, max); Assert.assertTrue("nextLong range", (value >= min) && (value <= max)); freq.addValue(value); } final int len = max - min + 1; final long[] observed = new long[len]; for (int i = 0; i < len; i++) { observed[i] = freq.getCount(min + i); } final double[] expected = new double[len]; for (int i = 0; i < len; i++) { expected[i] = 1d / len; } TestUtils.assertChiSquareAccept(expected, observed, 0.0001); } @Test public void testNextSecureIntIAE() { try { randomData.nextSecureInt(4, 3); Assert.fail("MathIllegalArgumentException expected"); } catch (MathIllegalArgumentException ex) { // ignored } } @Test @Retry(3) public void testNextSecureIntNegativeToPositiveRange() { for (int i = 0; i < 5; i++) { checkNextSecureIntUniform(-3, 5); checkNextSecureIntUniform(-3, 6); } } @Test @Retry(3) public void testNextSecureIntNegativeRange() { for (int i = 0; i < 5; i++) { checkNextSecureIntUniform(-7, -4); checkNextSecureIntUniform(-15, -2); } } @Test @Retry(3) public void testNextSecureIntPositiveRange() { for (int i = 0; i < 5; i++) { checkNextSecureIntUniform(0, 3); checkNextSecureIntUniform(2, 12); } } private void checkNextSecureIntUniform(int min, int max) { final Frequency freq = new Frequency(); for (int i = 0; i < smallSampleSize; i++) { final int value = randomData.nextSecureInt(min, max); Assert.assertTrue("nextInt range", (value >= min) && (value <= max)); freq.addValue(value); } final int len = max - min + 1; final long[] observed = new long[len]; for (int i = 0; i < len; i++) { observed[i] = freq.getCount(min + i); } final double[] expected = new double[len]; for (int i = 0; i < len; i++) { expected[i] = 1d / len; } TestUtils.assertChiSquareAccept(expected, observed, 0.0001); } /** * Make sure that empirical distribution of random Poisson(4)'s has P(X <= * 5) close to actual cumulative Poisson probability and that nextPoisson * fails when mean is non-positive. */ @Test public void testNextPoisson() { try { randomData.nextPoisson(0); Assert.fail("zero mean -- expecting MathIllegalArgumentException"); } catch (MathIllegalArgumentException ex) { // ignored } try { randomData.nextPoisson(-1); Assert.fail("negative mean supplied -- MathIllegalArgumentException expected"); } catch (MathIllegalArgumentException ex) { // ignored } try { randomData.nextPoisson(0); Assert.fail("0 mean supplied -- MathIllegalArgumentException expected"); } catch (MathIllegalArgumentException ex) { // ignored } final double mean = 4.0d; final int len = 5; PoissonDistribution poissonDistribution = new PoissonDistribution(mean); Frequency f = new Frequency(); randomData.reSeed(1000); for (int i = 0; i < largeSampleSize; i++) { f.addValue(randomData.nextPoisson(mean)); } final long[] observed = new long[len]; for (int i = 0; i < len; i++) { observed[i] = f.getCount(i + 1); } final double[] expected = new double[len]; for (int i = 0; i < len; i++) { expected[i] = poissonDistribution.probability(i + 1) * largeSampleSize; } TestUtils.assertChiSquareAccept(expected, observed, 0.0001); } @Test public void testNextPoissonConsistency() { // Small integral means for (int i = 1; i < 100; i++) { checkNextPoissonConsistency(i); } // non-integer means for (int i = 1; i < 10; i++) { checkNextPoissonConsistency(randomData.nextUniform(1, 1000)); } // large means // TODO: When MATH-282 is resolved, s/3000/10000 below for (int i = 1; i < 10; i++) { checkNextPoissonConsistency(randomData.nextUniform(1000, 3000)); } } /** * Verifies that nextPoisson(mean) generates an empirical distribution of values * consistent with PoissonDistributionImpl by generating 1000 values, computing a * grouped frequency distribution of the observed values and comparing this distribution * to the corresponding expected distribution computed using PoissonDistributionImpl. * Uses ChiSquare test of goodness of fit to evaluate the null hypothesis that the * distributions are the same. If the null hypothesis can be rejected with confidence * 1 - alpha, the check fails. */ public void checkNextPoissonConsistency(double mean) { // Generate sample values final int sampleSize = 1000; // Number of deviates to generate final int minExpectedCount = 7; // Minimum size of expected bin count long maxObservedValue = 0; final double alpha = 0.001; // Probability of false failure Frequency frequency = new Frequency(); for (int i = 0; i < sampleSize; i++) { long value = randomData.nextPoisson(mean); if (value > maxObservedValue) { maxObservedValue = value; } frequency.addValue(value); } /* * Set up bins for chi-square test. * Ensure expected counts are all at least minExpectedCount. * Start with upper and lower tail bins. * Lower bin = [0, lower); Upper bin = [upper, +inf). */ PoissonDistribution poissonDistribution = new PoissonDistribution(mean); int lower = 1; while (poissonDistribution.cumulativeProbability(lower - 1) * sampleSize < minExpectedCount) { lower++; } int upper = (int) (5 * mean); // Even for mean = 1, not much mass beyond 5 while ((1 - poissonDistribution.cumulativeProbability(upper - 1)) * sampleSize < minExpectedCount) { upper--; } // Set bin width for interior bins. For poisson, only need to look at end bins. int binWidth = 0; boolean widthSufficient = false; double lowerBinMass = 0; double upperBinMass = 0; while (!widthSufficient) { binWidth++; lowerBinMass = poissonDistribution.cumulativeProbability(lower - 1, lower + binWidth - 1); upperBinMass = poissonDistribution.cumulativeProbability(upper - binWidth - 1, upper - 1); widthSufficient = FastMath.min(lowerBinMass, upperBinMass) * sampleSize >= minExpectedCount; } /* * Determine interior bin bounds. Bins are * [1, lower = binBounds[0]), [lower, binBounds[1]), [binBounds[1], binBounds[2]), ... , * [binBounds[binCount - 2], upper = binBounds[binCount - 1]), [upper, +inf) * */ List binBounds = new ArrayList(); binBounds.add(lower); int bound = lower + binWidth; while (bound < upper - binWidth) { binBounds.add(bound); bound += binWidth; } binBounds.add(upper); // The size of bin [binBounds[binCount - 2], upper) satisfies binWidth <= size < 2*binWidth. // Compute observed and expected bin counts final int binCount = binBounds.size() + 1; long[] observed = new long[binCount]; double[] expected = new double[binCount]; // Bottom bin observed[0] = 0; for (int i = 0; i < lower; i++) { observed[0] += frequency.getCount(i); } expected[0] = poissonDistribution.cumulativeProbability(lower - 1) * sampleSize; // Top bin observed[binCount - 1] = 0; for (int i = upper; i <= maxObservedValue; i++) { observed[binCount - 1] += frequency.getCount(i); } expected[binCount - 1] = (1 - poissonDistribution.cumulativeProbability(upper - 1)) * sampleSize; // Interior bins for (int i = 1; i < binCount - 1; i++) { observed[i] = 0; for (int j = binBounds.get(i - 1); j < binBounds.get(i); j++) { observed[i] += frequency.getCount(j); } // Expected count is (mass in [binBounds[i-1], binBounds[i])) * sampleSize expected[i] = (poissonDistribution.cumulativeProbability(binBounds.get(i) - 1) - poissonDistribution.cumulativeProbability(binBounds.get(i - 1) -1)) * sampleSize; } // Use chisquare test to verify that generated values are poisson(mean)-distributed ChiSquareTest chiSquareTest = new ChiSquareTest(); // Fail if we can reject null hypothesis that distributions are the same if (chiSquareTest.chiSquareTest(expected, observed, alpha)) { StringBuilder msgBuffer = new StringBuilder(); DecimalFormat df = new DecimalFormat("#.##"); msgBuffer.append("Chisquare test failed for mean = "); msgBuffer.append(mean); msgBuffer.append(" p-value = "); msgBuffer.append(chiSquareTest.chiSquareTest(expected, observed)); msgBuffer.append(" chisquare statistic = "); msgBuffer.append(chiSquareTest.chiSquare(expected, observed)); msgBuffer.append(". \n"); msgBuffer.append("bin\t\texpected\tobserved\n"); for (int i = 0; i < expected.length; i++) { msgBuffer.append("["); msgBuffer.append(i == 0 ? 1: binBounds.get(i - 1)); msgBuffer.append(","); msgBuffer.append(i == binBounds.size() ? "inf": binBounds.get(i)); msgBuffer.append(")"); msgBuffer.append("\t\t"); msgBuffer.append(df.format(expected[i])); msgBuffer.append("\t\t"); msgBuffer.append(observed[i]); msgBuffer.append("\n"); } msgBuffer.append("This test can fail randomly due to sampling error with probability "); msgBuffer.append(alpha); msgBuffer.append("."); Assert.fail(msgBuffer.toString()); } } /** test dispersion and failure modes for nextHex() */ @Test public void testNextHex() { try { randomData.nextHexString(-1); Assert.fail("negative length supplied -- MathIllegalArgumentException expected"); } catch (MathIllegalArgumentException ex) { // ignored } try { randomData.nextHexString(0); Assert.fail("zero length supplied -- MathIllegalArgumentException expected"); } catch (MathIllegalArgumentException ex) { // ignored } String hexString = randomData.nextHexString(3); if (hexString.length() != 3) { Assert.fail("incorrect length for generated string"); } hexString = randomData.nextHexString(1); if (hexString.length() != 1) { Assert.fail("incorrect length for generated string"); } try { hexString = randomData.nextHexString(0); Assert.fail("zero length requested -- expecting MathIllegalArgumentException"); } catch (MathIllegalArgumentException ex) { // ignored } Frequency f = new Frequency(); for (int i = 0; i < smallSampleSize; i++) { hexString = randomData.nextHexString(100); if (hexString.length() != 100) { Assert.fail("incorrect length for generated string"); } for (int j = 0; j < hexString.length(); j++) { f.addValue(hexString.substring(j, j + 1)); } } double[] expected = new double[16]; long[] observed = new long[16]; for (int i = 0; i < 16; i++) { expected[i] = (double) smallSampleSize * 100 / 16; observed[i] = f.getCount(hex[i]); } TestUtils.assertChiSquareAccept(expected, observed, 0.001); } /** test dispersion and failure modes for nextHex() */ @Test @Retry(3) public void testNextSecureHex() { try { randomData.nextSecureHexString(-1); Assert.fail("negative length -- MathIllegalArgumentException expected"); } catch (MathIllegalArgumentException ex) { // ignored } try { randomData.nextSecureHexString(0); Assert.fail("zero length -- MathIllegalArgumentException expected"); } catch (MathIllegalArgumentException ex) { // ignored } String hexString = randomData.nextSecureHexString(3); if (hexString.length() != 3) { Assert.fail("incorrect length for generated string"); } hexString = randomData.nextSecureHexString(1); if (hexString.length() != 1) { Assert.fail("incorrect length for generated string"); } try { hexString = randomData.nextSecureHexString(0); Assert.fail("zero length requested -- expecting MathIllegalArgumentException"); } catch (MathIllegalArgumentException ex) { // ignored } Frequency f = new Frequency(); for (int i = 0; i < smallSampleSize; i++) { hexString = randomData.nextSecureHexString(100); if (hexString.length() != 100) { Assert.fail("incorrect length for generated string"); } for (int j = 0; j < hexString.length(); j++) { f.addValue(hexString.substring(j, j + 1)); } } double[] expected = new double[16]; long[] observed = new long[16]; for (int i = 0; i < 16; i++) { expected[i] = (double) smallSampleSize * 100 / 16; observed[i] = f.getCount(hex[i]); } TestUtils.assertChiSquareAccept(expected, observed, 0.001); } @Test public void testNextUniformIAE() { try { randomData.nextUniform(4, 3); Assert.fail("MathIllegalArgumentException expected"); } catch (MathIllegalArgumentException ex) { // ignored } try { randomData.nextUniform(0, Double.POSITIVE_INFINITY); Assert.fail("MathIllegalArgumentException expected"); } catch (MathIllegalArgumentException ex) { // ignored } try { randomData.nextUniform(Double.NEGATIVE_INFINITY, 0); Assert.fail("MathIllegalArgumentException expected"); } catch (MathIllegalArgumentException ex) { // ignored } try { randomData.nextUniform(0, Double.NaN); Assert.fail("MathIllegalArgumentException expected"); } catch (MathIllegalArgumentException ex) { // ignored } try { randomData.nextUniform(Double.NaN, 0); Assert.fail("MathIllegalArgumentException expected"); } catch (MathIllegalArgumentException ex) { // ignored } } @Test public void testNextUniformUniformPositiveBounds() { for (int i = 0; i < 5; i++) { checkNextUniformUniform(0, 10); } } @Test public void testNextUniformUniformNegativeToPositiveBounds() { for (int i = 0; i < 5; i++) { checkNextUniformUniform(-3, 5); } } @Test public void testNextUniformUniformNegaiveBounds() { for (int i = 0; i < 5; i++) { checkNextUniformUniform(-7, -3); } } @Test public void testNextUniformUniformMaximalInterval() { for (int i = 0; i < 5; i++) { checkNextUniformUniform(-Double.MAX_VALUE, Double.MAX_VALUE); } } private void checkNextUniformUniform(double min, double max) { // Set up bin bounds - min, binBound[0], ..., binBound[binCount-2], max final int binCount = 5; final double binSize = max / binCount - min/binCount; // Prevent overflow in extreme value case final double[] binBounds = new double[binCount - 1]; binBounds[0] = min + binSize; for (int i = 1; i < binCount - 1; i++) { binBounds[i] = binBounds[i - 1] + binSize; // + instead of * to avoid overflow in extreme case } final Frequency freq = new Frequency(); for (int i = 0; i < smallSampleSize; i++) { final double value = randomData.nextUniform(min, max); Assert.assertTrue("nextUniform range", (value > min) && (value < max)); // Find bin int j = 0; while (j < binCount - 1 && value > binBounds[j]) { j++; } freq.addValue(j); } final long[] observed = new long[binCount]; for (int i = 0; i < binCount; i++) { observed[i] = freq.getCount(i); } final double[] expected = new double[binCount]; for (int i = 0; i < binCount; i++) { expected[i] = 1d / binCount; } TestUtils.assertChiSquareAccept(expected, observed, 0.01); } /** test exclusive endpoints of nextUniform **/ @Test public void testNextUniformExclusiveEndpoints() { for (int i = 0; i < 1000; i++) { double u = randomData.nextUniform(0.99, 1); Assert.assertTrue(u > 0.99 && u < 1); } } /** test failure modes and distribution of nextGaussian() */ @Test public void testNextGaussian() { try { randomData.nextGaussian(0, 0); Assert.fail("zero sigma -- MathIllegalArgumentException expected"); } catch (MathIllegalArgumentException ex) { // ignored } double[] quartiles = TestUtils.getDistributionQuartiles(new NormalDistribution(0,1)); long[] counts = new long[4]; randomData.reSeed(1000); for (int i = 0; i < 1000; i++) { double value = randomData.nextGaussian(0, 1); TestUtils.updateCounts(value, counts, quartiles); } TestUtils.assertChiSquareAccept(expected, counts, 0.001); } /** test failure modes and distribution of nextExponential() */ @Test public void testNextExponential() { try { randomData.nextExponential(-1); Assert.fail("negative mean -- expecting MathIllegalArgumentException"); } catch (MathIllegalArgumentException ex) { // ignored } try { randomData.nextExponential(0); Assert.fail("zero mean -- expecting MathIllegalArgumentException"); } catch (MathIllegalArgumentException ex) { // ignored } double[] quartiles; long[] counts; // Mean 1 quartiles = TestUtils.getDistributionQuartiles(new ExponentialDistribution(1)); counts = new long[4]; randomData.reSeed(1000); for (int i = 0; i < 1000; i++) { double value = randomData.nextExponential(1); TestUtils.updateCounts(value, counts, quartiles); } TestUtils.assertChiSquareAccept(expected, counts, 0.001); // Mean 5 quartiles = TestUtils.getDistributionQuartiles(new ExponentialDistribution(5)); counts = new long[4]; randomData.reSeed(1000); for (int i = 0; i < 1000; i++) { double value = randomData.nextExponential(5); TestUtils.updateCounts(value, counts, quartiles); } TestUtils.assertChiSquareAccept(expected, counts, 0.001); } /** test reseeding, algorithm/provider games */ @Test public void testConfig() { randomData.reSeed(1000); double v = randomData.nextUniform(0, 1); randomData.reSeed(); Assert.assertTrue("different seeds", Math .abs(v - randomData.nextUniform(0, 1)) > 10E-12); randomData.reSeed(1000); Assert.assertEquals("same seeds", v, randomData.nextUniform(0, 1), 10E-12); randomData.reSeedSecure(1000); String hex = randomData.nextSecureHexString(40); randomData.reSeedSecure(); Assert.assertTrue("different seeds", !hex.equals(randomData .nextSecureHexString(40))); randomData.reSeedSecure(1000); Assert.assertTrue("same seeds", !hex .equals(randomData.nextSecureHexString(40))); /* * remove this test back soon, since it takes about 4 seconds * * try { randomData.setSecureAlgorithm("SHA1PRNG","SUN"); } catch * (NoSuchProviderException ex) { ; } Assert.assertTrue("different seeds", * !hex.equals(randomData.nextSecureHexString(40))); try { * randomData.setSecureAlgorithm("NOSUCHTHING","SUN"); * Assert.fail("expecting NoSuchAlgorithmException"); } catch * (NoSuchProviderException ex) { ; } catch (NoSuchAlgorithmException * ex) { ; } * * try { randomData.setSecureAlgorithm("SHA1PRNG","NOSUCHPROVIDER"); * Assert.fail("expecting NoSuchProviderException"); } catch * (NoSuchProviderException ex) { ; } */ // test reseeding without first using the generators RandomDataGenerator rd = new RandomDataGenerator(); rd.reSeed(100); rd.nextLong(1, 2); RandomDataGenerator rd2 = new RandomDataGenerator(); rd2.reSeedSecure(2000); rd2.nextSecureLong(1, 2); rd = new RandomDataGenerator(); rd.reSeed(); rd.nextLong(1, 2); rd2 = new RandomDataGenerator(); rd2.reSeedSecure(); rd2.nextSecureLong(1, 2); } /** tests for nextSample() sampling from Collection */ @Test public void testNextSample() { Object[][] c = { { "0", "1" }, { "0", "2" }, { "0", "3" }, { "0", "4" }, { "1", "2" }, { "1", "3" }, { "1", "4" }, { "2", "3" }, { "2", "4" }, { "3", "4" } }; long[] observed = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; double[] expected = { 100, 100, 100, 100, 100, 100, 100, 100, 100, 100 }; HashSet cPop = new HashSet(); // {0,1,2,3,4} for (int i = 0; i < 5; i++) { cPop.add(Integer.toString(i)); } Object[] sets = new Object[10]; // 2-sets from 5 for (int i = 0; i < 10; i++) { HashSet hs = new HashSet(); hs.add(c[i][0]); hs.add(c[i][1]); sets[i] = hs; } for (int i = 0; i < 1000; i++) { Object[] cSamp = randomData.nextSample(cPop, 2); observed[findSample(sets, cSamp)]++; } /* * Use ChiSquare dist with df = 10-1 = 9, alpha = .001 Change to 21.67 * for alpha = .01 */ Assert.assertTrue("chi-square test -- will fail about 1 in 1000 times", testStatistic.chiSquare(expected, observed) < 27.88); // Make sure sample of size = size of collection returns same collection HashSet hs = new HashSet(); hs.add("one"); Object[] one = randomData.nextSample(hs, 1); String oneString = (String) one[0]; if ((one.length != 1) || !oneString.equals("one")) { Assert.fail("bad sample for set size = 1, sample size = 1"); } // Make sure we fail for sample size > collection size try { one = randomData.nextSample(hs, 2); Assert.fail("sample size > set size, expecting MathIllegalArgumentException"); } catch (MathIllegalArgumentException ex) { // ignored } // Make sure we fail for empty collection try { hs = new HashSet(); one = randomData.nextSample(hs, 0); Assert.fail("n = k = 0, expecting MathIllegalArgumentException"); } catch (MathIllegalArgumentException ex) { // ignored } } @SuppressWarnings("unchecked") private int findSample(Object[] u, Object[] samp) { for (int i = 0; i < u.length; i++) { HashSet set = (HashSet) u[i]; HashSet sampSet = new HashSet(); for (int j = 0; j < samp.length; j++) { sampSet.add(samp[j]); } if (set.equals(sampSet)) { return i; } } Assert.fail("sample not found:{" + samp[0] + "," + samp[1] + "}"); return -1; } /** tests for nextPermutation */ @Test public void testNextPermutation() { int[][] p = { { 0, 1, 2 }, { 0, 2, 1 }, { 1, 0, 2 }, { 1, 2, 0 }, { 2, 0, 1 }, { 2, 1, 0 } }; long[] observed = { 0, 0, 0, 0, 0, 0 }; double[] expected = { 100, 100, 100, 100, 100, 100 }; for (int i = 0; i < 600; i++) { int[] perm = randomData.nextPermutation(3, 3); observed[findPerm(p, perm)]++; } String[] labels = {"{0, 1, 2}", "{ 0, 2, 1 }", "{ 1, 0, 2 }", "{ 1, 2, 0 }", "{ 2, 0, 1 }", "{ 2, 1, 0 }"}; TestUtils.assertChiSquareAccept(labels, expected, observed, 0.001); // Check size = 1 boundary case int[] perm = randomData.nextPermutation(1, 1); if ((perm.length != 1) || (perm[0] != 0)) { Assert.fail("bad permutation for n = 1, sample k = 1"); // Make sure we fail for k size > n try { perm = randomData.nextPermutation(2, 3); Assert.fail("permutation k > n, expecting MathIllegalArgumentException"); } catch (MathIllegalArgumentException ex) { // ignored } // Make sure we fail for n = 0 try { perm = randomData.nextPermutation(0, 0); Assert.fail("permutation k = n = 0, expecting MathIllegalArgumentException"); } catch (MathIllegalArgumentException ex) { // ignored } // Make sure we fail for k < n < 0 try { perm = randomData.nextPermutation(-1, -3); Assert.fail("permutation k < n < 0, expecting MathIllegalArgumentException"); } catch (MathIllegalArgumentException ex) { // ignored } } } // Disable until we have equals //public void testSerial() { // Assert.assertEquals(randomData, TestUtils.serializeAndRecover(randomData)); //} private int findPerm(int[][] p, int[] samp) { for (int i = 0; i < p.length; i++) { boolean good = true; for (int j = 0; j < samp.length; j++) { if (samp[j] != p[i][j]) { good = false; } } if (good) { return i; } } Assert.fail("permutation not found"); return -1; } @Test public void testNextInversionDeviate() { // Set the seed for the default random generator RandomGenerator rg = new Well19937c(100); RandomDataGenerator rdg = new RandomDataGenerator(rg); double[] quantiles = new double[10]; for (int i = 0; i < 10; i++) { quantiles[i] = rdg.nextUniform(0, 1); } // Reseed again so the inversion generator gets the same sequence rg.setSeed(100); BetaDistribution betaDistribution = new BetaDistribution(rg, 2, 4, BetaDistribution.DEFAULT_INVERSE_ABSOLUTE_ACCURACY); /* * Generate a sequence of deviates using inversion - the distribution function * evaluated at the random value from the distribution should match the uniform * random value used to generate it, which is stored in the quantiles[] array. */ for (int i = 0; i < 10; i++) { double value = betaDistribution.sample(); Assert.assertEquals(betaDistribution.cumulativeProbability(value), quantiles[i], 10E-9); } } @Test public void testNextBeta() { double[] quartiles = TestUtils.getDistributionQuartiles(new BetaDistribution(2,5)); long[] counts = new long[4]; randomData.reSeed(1000); for (int i = 0; i < 1000; i++) { double value = randomData.nextBeta(2, 5); TestUtils.updateCounts(value, counts, quartiles); } TestUtils.assertChiSquareAccept(expected, counts, 0.001); } @Test public void testNextCauchy() { double[] quartiles = TestUtils.getDistributionQuartiles(new CauchyDistribution(1.2, 2.1)); long[] counts = new long[4]; randomData.reSeed(1000); for (int i = 0; i < 1000; i++) { double value = randomData.nextCauchy(1.2, 2.1); TestUtils.updateCounts(value, counts, quartiles); } TestUtils.assertChiSquareAccept(expected, counts, 0.001); } @Test public void testNextChiSquare() { double[] quartiles = TestUtils.getDistributionQuartiles(new ChiSquaredDistribution(12)); long[] counts = new long[4]; randomData.reSeed(1000); for (int i = 0; i < 1000; i++) { double value = randomData.nextChiSquare(12); TestUtils.updateCounts(value, counts, quartiles); } TestUtils.assertChiSquareAccept(expected, counts, 0.001); } @Test public void testNextF() { double[] quartiles = TestUtils.getDistributionQuartiles(new FDistribution(12, 5)); long[] counts = new long[4]; randomData.reSeed(1000); for (int i = 0; i < 1000; i++) { double value = randomData.nextF(12, 5); TestUtils.updateCounts(value, counts, quartiles); } TestUtils.assertChiSquareAccept(expected, counts, 0.001); } @Test public void testNextGamma() { double[] quartiles; long[] counts; // Tests shape > 1, one case in the rejection sampling quartiles = TestUtils.getDistributionQuartiles(new GammaDistribution(4, 2)); counts = new long[4]; randomData.reSeed(1000); for (int i = 0; i < 1000; i++) { double value = randomData.nextGamma(4, 2); TestUtils.updateCounts(value, counts, quartiles); } TestUtils.assertChiSquareAccept(expected, counts, 0.001); // Tests shape <= 1, another case in the rejection sampling quartiles = TestUtils.getDistributionQuartiles(new GammaDistribution(0.3, 3)); counts = new long[4]; randomData.reSeed(1000); for (int i = 0; i < 1000; i++) { double value = randomData.nextGamma(0.3, 3); TestUtils.updateCounts(value, counts, quartiles); } TestUtils.assertChiSquareAccept(expected, counts, 0.001); } @Test public void testNextT() { double[] quartiles = TestUtils.getDistributionQuartiles(new TDistribution(10)); long[] counts = new long[4]; randomData.reSeed(1000); for (int i = 0; i < 1000; i++) { double value = randomData.nextT(10); TestUtils.updateCounts(value, counts, quartiles); } TestUtils.assertChiSquareAccept(expected, counts, 0.001); } @Test public void testNextWeibull() { double[] quartiles = TestUtils.getDistributionQuartiles(new WeibullDistribution(1.2, 2.1)); long[] counts = new long[4]; randomData.reSeed(1000); for (int i = 0; i < 1000; i++) { double value = randomData.nextWeibull(1.2, 2.1); TestUtils.updateCounts(value, counts, quartiles); } TestUtils.assertChiSquareAccept(expected, counts, 0.001); } @Test public void testNextBinomial() { BinomialDistributionTest testInstance = new BinomialDistributionTest(); int[] densityPoints = testInstance.makeDensityTestPoints(); double[] densityValues = testInstance.makeDensityTestValues(); int sampleSize = 1000; int length = TestUtils.eliminateZeroMassPoints(densityPoints, densityValues); BinomialDistribution distribution = (BinomialDistribution) testInstance.makeDistribution(); double[] expectedCounts = new double[length]; long[] observedCounts = new long[length]; for (int i = 0; i < length; i++) { expectedCounts[i] = sampleSize * densityValues[i]; } randomData.reSeed(1000); for (int i = 0; i < sampleSize; i++) { int value = randomData.nextBinomial(distribution.getNumberOfTrials(), distribution.getProbabilityOfSuccess()); for (int j = 0; j < length; j++) { if (value == densityPoints[j]) { observedCounts[j]++; } } } TestUtils.assertChiSquareAccept(densityPoints, expectedCounts, observedCounts, .001); } @Test public void testNextHypergeometric() { HypergeometricDistributionTest testInstance = new HypergeometricDistributionTest(); int[] densityPoints = testInstance.makeDensityTestPoints(); double[] densityValues = testInstance.makeDensityTestValues(); int sampleSize = 1000; int length = TestUtils.eliminateZeroMassPoints(densityPoints, densityValues); HypergeometricDistribution distribution = (HypergeometricDistribution) testInstance.makeDistribution(); double[] expectedCounts = new double[length]; long[] observedCounts = new long[length]; for (int i = 0; i < length; i++) { expectedCounts[i] = sampleSize * densityValues[i]; } randomData.reSeed(1000); for (int i = 0; i < sampleSize; i++) { int value = randomData.nextHypergeometric(distribution.getPopulationSize(), distribution.getNumberOfSuccesses(), distribution.getSampleSize()); for (int j = 0; j < length; j++) { if (value == densityPoints[j]) { observedCounts[j]++; } } } TestUtils.assertChiSquareAccept(densityPoints, expectedCounts, observedCounts, .001); } @Test public void testNextPascal() { PascalDistributionTest testInstance = new PascalDistributionTest(); int[] densityPoints = testInstance.makeDensityTestPoints(); double[] densityValues = testInstance.makeDensityTestValues(); int sampleSize = 1000; int length = TestUtils.eliminateZeroMassPoints(densityPoints, densityValues); PascalDistribution distribution = (PascalDistribution) testInstance.makeDistribution(); double[] expectedCounts = new double[length]; long[] observedCounts = new long[length]; for (int i = 0; i < length; i++) { expectedCounts[i] = sampleSize * densityValues[i]; } randomData.reSeed(1000); for (int i = 0; i < sampleSize; i++) { int value = randomData.nextPascal(distribution.getNumberOfSuccesses(), distribution.getProbabilityOfSuccess()); for (int j = 0; j < length; j++) { if (value == densityPoints[j]) { observedCounts[j]++; } } } TestUtils.assertChiSquareAccept(densityPoints, expectedCounts, observedCounts, .001); } @Test public void testNextZipf() { ZipfDistributionTest testInstance = new ZipfDistributionTest(); int[] densityPoints = testInstance.makeDensityTestPoints(); double[] densityValues = testInstance.makeDensityTestValues(); int sampleSize = 1000; int length = TestUtils.eliminateZeroMassPoints(densityPoints, densityValues); ZipfDistribution distribution = (ZipfDistribution) testInstance.makeDistribution(); double[] expectedCounts = new double[length]; long[] observedCounts = new long[length]; for (int i = 0; i < length; i++) { expectedCounts[i] = sampleSize * densityValues[i]; } randomData.reSeed(1000); for (int i = 0; i < sampleSize; i++) { int value = randomData.nextZipf(distribution.getNumberOfElements(), distribution.getExponent()); for (int j = 0; j < length; j++) { if (value == densityPoints[j]) { observedCounts[j]++; } } } TestUtils.assertChiSquareAccept(densityPoints, expectedCounts, observedCounts, .001); } @Test /** * MATH-720 */ public void testReseed() { PoissonDistribution x = new PoissonDistribution(3.0); x.reseedRandomGenerator(0); final double u = x.sample(); PoissonDistribution y = new PoissonDistribution(3.0); y.reseedRandomGenerator(0); Assert.assertEquals(u, y.sample(), 0); } } ././@LongLink100644 0 0 145 12126630646 10261 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/random/RandomGeneratorAbstractTest.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/random/RandomGeneratorAbstractTest.java100644 1750 1750 35144 12126627670 32345 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.random; import java.util.Arrays; import org.apache.commons.math3.TestUtils; import org.apache.commons.math3.stat.Frequency; import org.apache.commons.math3.stat.descriptive.SummaryStatistics; import org.apache.commons.math3.util.FastMath; import org.apache.commons.math3.exception.MathIllegalArgumentException; import org.junit.Assert; import org.junit.Before; import org.junit.Test; /** * Base class for RandomGenerator tests. * * Tests RandomGenerator methods directly and also executes RandomDataTest * test cases against a RandomDataImpl created using the provided generator. * * RandomGenerator test classes should extend this class, implementing * makeGenerator() to provide a concrete generator to test. The generator * returned by makeGenerator should be seeded with a fixed seed. * * @version $Id: RandomGeneratorAbstractTest.java 1454846 2013-03-10 13:02:04Z luc $ */ public abstract class RandomGeneratorAbstractTest extends RandomDataGeneratorTest { /** RandomGenerator under test */ protected RandomGenerator generator; /** * Override this method in subclasses to provide a concrete generator to test. * Return a generator seeded with a fixed seed. */ protected abstract RandomGenerator makeGenerator(); /** * Initialize generator and randomData instance in superclass. */ public RandomGeneratorAbstractTest() { generator = makeGenerator(); randomData = new RandomDataGenerator(generator); } /** * Set a fixed seed for the tests */ @Before public void setUp() { generator = makeGenerator(); } // Omit secureXxx tests, since they do not use the provided generator @Override public void testNextSecureLongIAE() {} @Override public void testNextSecureLongNegativeToPositiveRange() {} @Override public void testNextSecureLongNegativeRange() {} @Override public void testNextSecureLongPositiveRange() {} @Override public void testNextSecureIntIAE() {} @Override public void testNextSecureIntNegativeToPositiveRange() {} @Override public void testNextSecureIntNegativeRange() {} @Override public void testNextSecureIntPositiveRange() {} @Override public void testNextSecureHex() {} @Test /** * Tests uniformity of nextInt(int) distribution by generating 1000 * samples for each of 10 test values and for each sample performing * a chi-square test of homogeneity of the observed distribution with * the expected uniform distribution. Tests are performed at the .01 * level and an average failure rate higher than 2% (i.e. more than 20 * null hypothesis rejections) causes the test case to fail. * * All random values are generated using the generator instance used by * other tests and the generator is not reseeded, so this is a fixed seed * test. */ public void testNextIntDirect() { // Set up test values - end of the array filled randomly int[] testValues = new int[] {4, 10, 12, 32, 100, 10000, 0, 0, 0, 0}; for (int i = 6; i < 10; i++) { final int val = generator.nextInt(); testValues[i] = val < 0 ? -val : val + 1; } final int numTests = 1000; for (int i = 0; i < testValues.length; i++) { final int n = testValues[i]; // Set up bins int[] binUpperBounds; if (n < 32) { binUpperBounds = new int[n]; for (int k = 0; k < n; k++) { binUpperBounds[k] = k; } } else { binUpperBounds = new int[10]; final int step = n / 10; for (int k = 0; k < 9; k++) { binUpperBounds[k] = (k + 1) * step; } binUpperBounds[9] = n - 1; } // Run the tests int numFailures = 0; final int binCount = binUpperBounds.length; final long[] observed = new long[binCount]; final double[] expected = new double[binCount]; expected[0] = binUpperBounds[0] == 0 ? (double) smallSampleSize / (double) n : (double) ((binUpperBounds[0] + 1) * smallSampleSize) / (double) n; for (int k = 1; k < binCount; k++) { expected[k] = (double) smallSampleSize * (double) (binUpperBounds[k] - binUpperBounds[k - 1]) / (double) n; } for (int j = 0; j < numTests; j++) { Arrays.fill(observed, 0); for (int k = 0; k < smallSampleSize; k++) { final int value = generator.nextInt(n); Assert.assertTrue("nextInt range",(value >= 0) && (value < n)); for (int l = 0; l < binCount; l++) { if (binUpperBounds[l] >= value) { observed[l]++; break; } } } if (testStatistic.chiSquareTest(expected, observed) < 0.01) { numFailures++; } } if ((double) numFailures / (double) numTests > 0.02) { Assert.fail("Too many failures for n = " + n + " " + numFailures + " out of " + numTests + " tests failed."); } } } @Test public void testNextIntIAE2() { try { generator.nextInt(-1); Assert.fail("MathIllegalArgumentException expected"); } catch (MathIllegalArgumentException ex) { // ignored } try { generator.nextInt(0); } catch (MathIllegalArgumentException ex) { // ignored } } @Test public void testNextLongDirect() { long q1 = Long.MAX_VALUE/4; long q2 = 2 * q1; long q3 = 3 * q1; Frequency freq = new Frequency(); long val = 0; int value = 0; for (int i=0; i= 0) { ++walk; } else { --walk; } } Assert.assertTrue("Walked too far astray: " + walk + "\nNote: This " + "test will fail randomly about 1 in 100 times.", FastMath.abs(walk) < FastMath.sqrt(N) * 2.576); } @Test public void testNextLong2() { int walk = 0; final int N = 1000; for (int k = 0; k < N; ++k) { if (generator.nextLong() >= 0) { ++walk; } else { --walk; } } Assert.assertTrue("Walked too far astray: " + walk + "\nNote: This " + "test will fail randomly about 1 in 100 times.", FastMath.abs(walk) < FastMath.sqrt(N) * 2.576); } @Test public void testNexBoolean2() { int walk = 0; final int N = 10000; for (int k = 0; k < N; ++k) { if (generator.nextBoolean()) { ++walk; } else { --walk; } } Assert.assertTrue("Walked too far astray: " + walk + "\nNote: This " + "test will fail randomly about 1 in 100 times.", FastMath.abs(walk) < FastMath.sqrt(N) * 2.576); } @Test public void testNexBytes() { long[] count = new long[256]; byte[] bytes = new byte[10]; double[] expected = new double[256]; final int sampleSize = 100000; for (int i = 0; i < 256; i++) { expected[i] = (double) sampleSize / 265f; } for (int k = 0; k < sampleSize; ++k) { generator.nextBytes(bytes); for (byte b : bytes) { ++count[b + 128]; } } TestUtils.assertChiSquareAccept(expected, count, 0.001); } @Test public void testSeeding() { // makeGenerator initializes with fixed seed RandomGenerator gen = makeGenerator(); RandomGenerator gen1 = makeGenerator(); checkSameSequence(gen, gen1); // reseed, but recreate the second one // verifies MATH-723 gen.setSeed(100); gen1 = makeGenerator(); gen1.setSeed(100); checkSameSequence(gen, gen1); } private void checkSameSequence(RandomGenerator gen1, RandomGenerator gen2) { final int len = 11; // Needs to be an odd number to check MATH-723 final double[][] values = new double[2][len]; for (int i = 0; i < len; i++) { values[0][i] = gen1.nextDouble(); } for (int i = 0; i < len; i++) { values[1][i] = gen2.nextDouble(); } Assert.assertTrue(Arrays.equals(values[0], values[1])); for (int i = 0; i < len; i++) { values[0][i] = gen1.nextFloat(); } for (int i = 0; i < len; i++) { values[1][i] = gen2.nextFloat(); } Assert.assertTrue(Arrays.equals(values[0], values[1])); for (int i = 0; i < len; i++) { values[0][i] = gen1.nextInt(); } for (int i = 0; i < len; i++) { values[1][i] = gen2.nextInt(); } Assert.assertTrue(Arrays.equals(values[0], values[1])); for (int i = 0; i < len; i++) { values[0][i] = gen1.nextLong(); } for (int i = 0; i < len; i++) { values[1][i] = gen2.nextLong(); } Assert.assertTrue(Arrays.equals(values[0], values[1])); for (int i = 0; i < len; i++) { values[0][i] = gen1.nextInt(len); } for (int i = 0; i < len; i++) { values[1][i] = gen2.nextInt(len); } Assert.assertTrue(Arrays.equals(values[0], values[1])); for (int i = 0; i < len; i++) { values[0][i] = gen1.nextBoolean() ? 1 : 0; } for (int i = 0; i < len; i++) { values[1][i] = gen2.nextBoolean() ? 1 : 0; } Assert.assertTrue(Arrays.equals(values[0], values[1])); for (int i = 0; i < len; i++) { values[0][i] = gen1.nextGaussian(); } for (int i = 0; i < len; i++) { values[1][i] = gen2.nextGaussian(); } Assert.assertTrue(Arrays.equals(values[0], values[1])); } } ././@LongLink100644 0 0 145 12126630646 10261 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/random/AbstractRandomGeneratorTest.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/random/AbstractRandomGeneratorTest.java100644 1750 1750 2501 12126627670 32314 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.random; /** * Test cases for the AbstractRandomGenerator class. * * @version $Id: AbstractRandomGeneratorTest.java 1244107 2012-02-14 16:17:55Z erans $ */ public class AbstractRandomGeneratorTest extends RandomGeneratorAbstractTest { public AbstractRandomGeneratorTest() { super(); } @Override protected RandomGenerator makeGenerator() { RandomGenerator generator = new TestRandomGenerator(); generator.setSeed(1001); return generator; } } commons-math3-3.2-src/src/test/java/org/apache/commons/math3/random/RandomAdaptorTest.java100644 1750 1750 6171 12126627670 30303 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.random; import java.util.Random; import org.junit.Assert; import org.junit.Test; /** * Test cases for the RandomAdaptor class * * @version $Id: RandomAdaptorTest.java 1244107 2012-02-14 16:17:55Z erans $ */ public class RandomAdaptorTest { @Test public void testAdaptor() { ConstantGenerator generator = new ConstantGenerator(); Random random = RandomAdaptor.createAdaptor(generator); checkConstant(random); RandomAdaptor randomAdaptor = new RandomAdaptor(generator); checkConstant(randomAdaptor); } private void checkConstant(Random random) { byte[] bytes = new byte[] {0}; random.nextBytes(bytes); Assert.assertEquals(0, bytes[0]); Assert.assertEquals(false, random.nextBoolean()); Assert.assertEquals(0, random.nextDouble(), 0); Assert.assertEquals(0, random.nextFloat(), 0); Assert.assertEquals(0, random.nextGaussian(), 0); Assert.assertEquals(0, random.nextInt()); Assert.assertEquals(0, random.nextInt(1)); Assert.assertEquals(0, random.nextLong()); random.setSeed(100); Assert.assertEquals(0, random.nextDouble(), 0); } /* * "Constant" generator to test Adaptor delegation. * "Powered by Eclipse ;-)" * */ public static class ConstantGenerator implements RandomGenerator { private final double value; public ConstantGenerator() { value = 0; } public ConstantGenerator(double value) { this.value = value; } public boolean nextBoolean() { return false; } public void nextBytes(byte[] bytes) { } public double nextDouble() { return value; } public float nextFloat() { return (float) value; } public double nextGaussian() { return value; } public int nextInt() { return (int) value; } public int nextInt(int n) { return (int) value; } public long nextLong() { return (int) value; } public void setSeed(int seed) { } public void setSeed(int[] seed) { } public void setSeed(long seed) { } } } commons-math3-3.2-src/src/test/java/org/apache/commons/math3/random/Well1024aTest.java100644 1750 1750 13101 12126627670 27132 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.random; import org.junit.Assert; import org.junit.Test; public class Well1024aTest extends RandomGeneratorAbstractTest { @Override protected RandomGenerator makeGenerator() { return new Well1024a(1001); } @Test public void testReferenceCode() { Well1024a mt = new Well1024a(new int[] { 740849862, 1202665156, -199039369, -259008301, -291878969, -1164428990, -1565918811, 491009864, -1883086670, 1383450241, 1244617256, 689006653, -1576746370, -1307940314, 1421489086, 1742094000, -595495729, 1047766204, 1875773301, -1637793284, 1379017098, 262792705, 191880010, -251000180, -1753047622, -972355720, 90626881, 1644693418, 1503365577, 439653419, 1806361562, 1268823869 }); int[] refInt = { -1478749726, -1645579484, -2075363835, -2063444174, -1834148336, -1769045872, -40711346, 1717441026, 2130656771, 783441285, 570433609, 1560023451, 653233971, 1368672434, -72036215, 1071111800, 933776492, 26114960, 49888778, 1808107515, 1092989296, 754848337, 1336994364, -1987450448, -691190146, -1803870839, 1110716866, 1173269113, -391000050, 2014216908, 180756301, -382891013, -1908154585, 1580737629, 1080267957, -125532248, 2094530239, 2132964485, -438596348, -760299445, 1058181869, 2050816800, -1534429037, -62552782, 824524142, -818590371, -1857695907, -684762866, -156556543, -902759995, -880795194, -1387351132, -1263017515, 448006597, 201038266, 1929826313, -455367306, 672963027, 2000073013, -1546842042, 446341090, 1001696686, -779919012, -347722602, -1342821677, 1639571150, -835315755, 1505585376, 367004975, -2035864404, -1786623553, 1249724913, 182435312, 1444514513, 1815333708, 1333772382, 299664001, -284691169, 2034403374, 1423310887, -1319051884, 1557286441, -445198266, -251809030, 1602786123, 944036382, -1020529634, 258344235, 685254367, 1838964943, -156674528, -979736602, -538312836, 234643178, 211152102, -635498640, -1036733933, -1347589147, -565609042, -1358714165, 508618483, -786364693, 2071450261, 1206956772, -678931458, 167690617, 144698821, 1719720781, 1575869280, -1343221123, -1766469944, 284991647, -717305514, 892653651, -1368347075, -615701972, -730369849, 1360396003, -1869287623, 1778269052, -586061545, -699517114, 61530249, -1860611767, -519660852, 1841085925, 1555610093, -399979337, -790345742, 422355947, 2007965433, 2044952550, -1712164595, -102915702, -693865324, -1894042487, -1285020072, -215883074, 95833252, 1625818040, -1055951680, 513067085, 1825246558, -553461652, -1923361799, -1869480206, 567232636, -1751727150, -1832301399, -108136455, -1312244126, 14006795, 850221366, -382389732, -1741556188, -1317464467, 1948314870, 753994471, 1028235947, 342494132, -1862256693, 723808794, -234257642, 1609928369, -802733456, 1315831915, 1436072885, 1224767136, 2144557791, -1839965886, 224821018, -1461697757, -1080386760, 1638573498, -1188173812, -325181523, -1750676219, -1780415850, 698793362, -908352052, 299746482, -161660934, 1938166833, 800297005, 56640033, -1214932666, -1248124842, 1822796868, 1777615881, -718517774, 1908159957, 1733053281, 1851844331, 1283519375, -1771494956, 2060179999, 1666129209, 1919453531, -498145770, 697567008, 1855487148, -1587163491, 565216434, -1477877933, -925662919, -806492585, -1201439047, -1424534232, 1788616523, 69414717, 655893636, -1175978556, 24787512, -861550001, 439525754, -190433174, -383811606, -508589783, 1441608687, 608181366, 1539467064, 925903122, 697209654, 1878283393, -1967567432, -1659677763, -249658183, 847096354, 397741956, -125334541, -1286840731, 1016461908, -997968592, 1795331475, 1856856501, -1716726445, -582181331, -887091847, 426964855, -609219941, -1456232632, -483467616, 1069260754, 972242064, -1406786247, 1954194029, 52627891, 1212755081, 2117436668, 281073392, 741537353, -483063506, 1850906286, -244876135, -270818140, 1817568823 }; for (int i = 0; i < refInt.length; ++i) { Assert.assertEquals(refInt[i], mt.nextInt()); } } } commons-math3-3.2-src/src/test/java/org/apache/commons/math3/random/Well19937aTest.java100644 1750 1750 41055 12126627670 27251 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.random; import org.junit.Assert; import org.junit.Test; public class Well19937aTest extends RandomGeneratorAbstractTest { @Override public RandomGenerator makeGenerator() { return new Well19937a(100); } @Test public void testReferenceCode() { int[] base = { 740849862, 1202665156, -199039369, -259008301, -291878969, -1164428990, -1565918811, 491009864, -1883086670, 1383450241, 1244617256, 689006653, -1576746370, -1307940314, 1421489086, 1742094000, -595495729, 1047766204, 1875773301, -1637793284, 1379017098, 262792705, 191880010, -251000180, -1753047622, -972355720, 90626881, 1644693418, 1503365577, 439653419, 1806361562, 1268823869 }; int[] init = new int[624]; for (int i = 0; i < init.length; ++i) { init[i] = base[i % base.length] + i; } Well19937a mt = new Well19937a(init); int[] refInt = { -612874471, -354976292, -1838197125, -1781560577, 278390997, 1214938280, -1752615390, -760835246, -1712883765, -241205782, -145390202, 495649160, -514388259, -1271015916, -1640000013, 849273623, -549729394, -1206917255, -545909692, 811925434, -1665729633, -1525292882, 1416246482, -153220826, 1148868872, -326143196, 1724979062, 1790931148, -1648679618, -439051683, 112482777, -1484051520, -1881272572, -1270447031, -1216102401, 1579107248, -1224395621, 2144411988, -416216641, -1529222361, 1628987080, 164445245, 1506928916, 928145916, 1436000427, 862025970, 560077705, -1887251027, -514360858, 1735094506, 475624879, 1755802355, 295448361, -155399225, 3972415, 1368201076, -465126094, -1622687259, -246099304, 1798631152, -1937269102, -1700560396, -293352622, -896632303, -2088933220, -194382452, -480218162, -1618517785, -1925031481, -150217434, 1678937261, 2130832364, -485546678, -1499224981, 1430390884, -1895417302, 210514746, 1781140999, -1940853105, -1238099647, 485922557, -103223212, 633481679, -632946979, 695235541, -1661735272, 277603567, -958341538, 256982285, 1850270018, -327388076, -219053874, 1380560653, -1221689980, 1335863752, -545032383, -575291735, -1295623907, -140058298, 1063302709, -1290702617, -790401546, -170630961, -1203114473, 1458063108, -1212753301, 1546428514, 2112636413, -1463028928, -1812598032, -883529486, 1131084094, 62042165, 2135819802, -1192342739, 98361522, -1341042205, -475283063, -1632033747, 1745196892, 168608689, -914987039, 274428907, -881357258, 167940012, -1975737532, -903960486, -1370984244, -589352935, 1783633514, -570111010, 71495377, 194463285, -1243905021, -1398490898, 221691209, -55728834, -638916786, -770622372, -1911651810, -295233027, 301467998, 2058638784, 681490183, -1547865078, -1668135684, 1299261826, 1649468635, 287995017, -2076844852, 1193468826, -853948258, 120082777, 1051829542, -1288514343, -159456430, 275748820, -480127107, -604943233, -2138088332, 1202614819, 1427201263, -1906344469, -1230779533, 1690367192, 733159097, 794410312, -1114452505, -1601554413, 976747949, 1517787154, 2091780205, 1052078906, 1919282771, -191013374, 1805397142, 736939268, -1056272823, -727464316, -659459005, 797803875, -1104633884, 1042342081, -24514837, 1919469940, 1903722546, -814157872, 1605407665, -262351256, -288949635, 729204844, -1132605534, 745453338, 387915035, 1094173337, 2100279147, 156863702, -257377544, -719587984, -1496015613, 1908993744, 2016957554, 918749666, -135963651, -1356808639, -1711185741, 1472589240, -398100149, 628791415, -1381837652, -1820702771, -593586943, -1456631279, -1837975351, -1394249972, -556916726, 833231177, 43449750, 1029237092, -2086437337, -459463076, -533031784, -1739648287, -1374722961, 2024908394, 1389678488, 2018558, -1391707864, -795935743, 904816957, 836583280, 1766194531, -1374431014, -904437876, 2030248636, -265724199, 2056758426, -810499837, 887193593, -77811488, 1496312336, -1874348275, -456193866, -2137130942, 868120387, 29025455, -1999867716, 2001322335, -579152815, -390892056, 1592011837, -306394879, 93636886, -190879994, 1923358153, 269052141, -396050253, -987531729, 480350991, 1276744541, -1445571957, -957571005, -2046270221, -1715395752, 1113585628, -1782113514, -697560146, 835320000, 1014320959, -2119834109, 460056841, -1464772991, -1282790418, -2120806165, 86176097, -731086307, 832497517, -1876684928, 541008240, 551124479, -450919132, 647860281, -2115397586, 979247589, 1095559204, 1927958688, 169497703, 1999579054, 2019745038, 1656022059, -1109662138, 375237154, 1450814436, 919988416, 849761266, 1457057327, 1771166577, -1639880487, -852488298, 1767063646, 657295386, -585561879, 740792583, 1664558308, -654749506, 1109275990, 182597559, 1106789745, -1806628480, 25948116, 1748374299, 196057325, -164213209, 1687024594, 782029276, 1879737947, -1528219611, 412585737, 1190325629, 1985821911, -1272945202, -1238637137, 465818730, -1537670961, 1131953615, 905623579, 609183424, 1138422991, 1522974699, 589719061, -1310894604, 890952933, -885204790, -393535694, 1238408670, 1780660354, 677803525, -1121509064, 1553148616, 1109165936, -1450120385, 1525252521, -1354897489, -595402189, -1274551767, -869281409, 1788815975, 2020262116, 1124100185, -400839020, 310574108, 1354413045, -1310514485, 1895732085, 626639054, 1667355357, 2065637178, -1889009143, -440157749, 1762849463, -1693853642, -56602956, -930874188, -398470740, 778356402, -2113156881, 42854964, 1844399604, -2098310302, -1812029757, 1441188713, 899579267, 1266994172, 1841370863, -660740252, -43254718, 1124500192, 1884907320, 879997211, 1775139845, -1360112721, 1630490057, 362567879, 1113475029, 290319279, -1209506867, 398146039, -957742350, 1185761854, 1519676447, -912689915, -1117128973, -305563462, -1928033363, -1766324543, 1702753492, 1696951912, -1895072395, 932663591, -566548128, 991675996, 56529814, 980735023, 718166662, -650028466, -886842051, 1857048587, -569023569, -1820572202, -851452711, -958700452, -621825633, -65649888, -510143183, 761267599, -1692108035, 1729071710, 1623630864, -53498654, 267235687, 659201413, 1152882627, -824194574, 356636960, -502391121, -538453360, 66115376, -1633290370, -1522088932, 268949070, 684499443, -859474501, 1586764345, -1515639709, 319695602, -307025150, 69076508, 1050726785, -1340930110, 552191600, -207852941, -273572993, -539580440, 710343120, 1957076127, -1107172811, -561518280, -1775022699, 1978792904, 1935531451, -2084046304, -419742902, -737652926, 614022023, 1676952428, 769892939, -1092786807, -1117113223, -266029995, -350150999, 207738542, 1964896575, 48805284, 1736500159, 551289617, -1847923501, 1856609505, 2007480480, -681860219, -1198106493, 1483591043, -523895316, -1814473078, -1521087404, -1348859926, 1298056897, 1813789478, 946683654, 79196400, 1766250931, 472737685, 1764634332, -1844726079, -130619045, -508713868, -1762537125, 1010108863, 170107098, 1705386380, -1139681802, 183739097, 1662699401, 1842694501, 1714633805, 46208876, 616720693, -252553427, 1986302230, -103130254, 1943225981, 110746655, 553260552, 1588938073, -1934623163, -2144781332, -2086217416, 1941265852, -781953226, 1216234254, 605543697, -710872598, 2048636577, -1986927728, -1007017623, 1243051501, -614249563, -2128221291, 581579813, 1173464240, -1906830937, 261329601, -1805974103, 769823490, 1858731164, -561762071, 516417430, -1221329437, -825500715, 1091364656, -993658663, -1475434188, -1070804384, -1876492082, 899494424, 683486936, 878807455, 56642807, -1268202879, 1379172046, -1386869373, -1158233876, 1759190552, 1597629789, 1411151497, -1254268471, 1075936979, -918778269, -2132675184, 953140888, 1906982077, 1154200766, -365384600, -1142488826, 708535121, -2134869964, -1531201665, -2100526761, 1268256467, 2071480803, 193135243, 1374158182, 989505347, -933612202, -2134839213, -1302795271, -2092029041, 1812014826, 2090855917, 2005348528, 606434393, -60141386, 11156360, 539516285, -122485034, -893237911, -978127424, 1509901816, -451029719, 428544700, -1622965963, -1993611605, -1989324583, 1104111587, -795138585, -899552401, -2110167769, -234502445, 1586963605, -503778455, 529261062, 325327284, -106186403, 65369563, -1475700698, -228624261, 715975009, 1099352363, -1796883396, 1376542700, -308942420, -344940451, -395389249, -1562737166, 1869802677, 1273494710, 2075587668, -789570273, 1563347596, 1142901755, 1676422422, -1729157809, -1399423717, -1814262429, -1809707284, 1393992342, -570246212, 1065528749, -781643849, 1218667301, -1097949471, 1305629790, 901301039, -704762030, 360582612, 1411910672, 1848068741, -614500891, -146889637, -913903597, 723527277, -147033328, -199273155, 734997691, -2072735286, 2129258691, -1385074104, 931616624, 1065477319, -1543474555, -531410292, -2123119121, -1538464113, -1153585193, 1559931968, -654877011, 879865200, 1489681397, 1998864644, -1964160144, 163671782, -858364148, -323324233, 801208648, 705864113, 436184243, 643773864, 2087594507, 134637265, -749956494, -1657343972, -1828172168, -27357303, -1145161336, -1192513644, 216148260, 611393153, -13752671, -358631090, -1211920749, 593572064, 657629904, -1445961088, -250704995, 1797542707, -2122311891, -316774825, -296303057, -868002056, -86697533, 2020588145, 1203427903, -1371839056, 669531557, -2031033836, 1323994690, 13703036, 785437772, -1465821554, -694756014, -2131068154, -1745448876, -1095891733, 936594025, -1119068454, 855423970, 1705079340, -905640608, 162297141, 1336619311, -344353769, -92608588, -1080573824, 2002293105, -2088030765, -1684198727, -129054718, -949437132, -127983221, -216664110, 1700146143, -711174649, 1500113839, 1212236226, -2017364219, -1263597675, 511929344, -323998524, -2021313185, 1803000924, 927670608, 336267187, 1244256964, -1665390108, 991395134, -251232188, 1267445783, 1547951569, 740269916, 1776431169, 1687220659, 228229817, 271386089, -682906779, -438090344, 1190436796, -744272540, 1879221151, 1145200306, -1730983338, -1119209309, 90826726, 1567861540, 1638852830, -1645384932, 1566909531, 1088584561, 1030555565, -1872052014, 720320695, -885053674, -321216789, 739907579, 368580703, -443635520, 1232705619, -1355949988, -1047211249, -1571429448, 599299852, 1036970439, 1513838571, -51797291, -26647565, -1262878942, -916262582, 1579082269, -292007383, 1289013866, -1612184284, 1451738668, 448608569, 476432992, -1229609565, 786372409, 929928149, -150100614, 448155944, -1322320576, -856549627, 1057443268, -1536809554, 577508258, 584906122, 275295163, -604262071, -236043234, -1866434954, -2072447013, 646132876, 847562546, -310005953, -1104162658, 393261203, -730102354, 440824482, 1654535035, -1296359745, 1487359328, -977776604, -775827779, -1298695106, 519080622, 1697162240, 227873031, -371123123, 1273702312, -1710063656, -2138342344, 1139555478, 1531578907, -1498880699, 1183507362, 1875307493, -1649740413, 2135386504, -962458407, 424161768, 504272962, 202204247, 1783466420, 2015579232, -676642965, 2067456450, 914480415, -620398841, 1880405399, 1406637142, 1951104977, 633496157, 224861869, -58659291, 994942775, -479000645, 1421449115, 100168104, 249754169, -1219011494, 1736303638, 364013694, -1750035055, -479217141, 1652913106, -2109452331, 1633842910, -1547663337, 936627493, -1152799743, 896955899, -1407742850, -523769014, 357161414, 872293304, 744895980, 720829676, -240843156, -111779524, 1292836315, -1792141538, 1946959925, 1181751089, -1120674052, 1185192575, -1387002557, 1973209255, -120887476, -766577735, -443913073, 786620227, 428564781, -101232106, -425959852, 198082021, 1173272226, -1744840378, -1621135606, -1539498583, -1101274572, 43399711, -1256764602, 1201920787, 2049426139, 846545551, -2121520873, -1202939675, -470425740, 321987390, 1862019060, -951540342, -894238318, -430407175, -1662746491, 656574776, 1580373777, -431290218, 1645824323, -1953526979, -374682356, 474291752, 1071558425, 511038981, -760598678, -567797285, -1176476266, -268409005, -2130644484, -67970563, 1756046948, 1429860462, -1130984739, -124916495, -1544436836, -1863524031, 1024916487, -1388636482, -1573205065, 892628956, 1831270021, 1176430590, 1158914682, -2006787098, -1228130033, 1516111488, -1499151347, 470546266, 1642603981, 1425140838, -1823071475, -1775267236, -1009380612, 164746986, 1129677098, 1842642579, -482342932, -507480364, 1656012309, 1981601761, -881042120, -511987083, 342447017, 381192578, 983008095, 741012865, -1877136350, -199211983, -452784912, 1929572576, -1678291139, -864375281, -1610561247, -1936356726, -749553767, -865893512, -567081879, -1303973729, -939636958, -622974563, 428284937, 1049237414, 852280765, 86648946, -1353851401, -1045422335, 898035731, -1636093996, -1083174191, 245046915, -359768226, -1028491655, 1051575118, 1774289451, 1839389415, -1594053468, 736707953, 1873556950, 401186168, -583669552, -88375334, 2002752071, 264506453, -1304812107, -759203942, -114958524, -1878903503, 841613720, 1910863820, -1738114003, 701455920, 1791058048, -1850960547, 1672292671, 1172188809, 604848896, -1607489375, 305370478, -948153885, -1971080100, -1848966954, -584538365, 39416319, -1689119162, 944942598, 1777111075, 1534005553, 2022718432, -25820385, 3077695, -315950520, 1859184648, -1397829266, -1666371809, 858913807, -610818620, 1554973298, 580023809, -1662988256, -408630026, 1316681876, 738204271, 942829881, -758486983, 780345857, 667165037, -2086803585, 789741324 }; for (int i = 0; i < refInt.length; ++i) { Assert.assertEquals(refInt[i], mt.nextInt()); } } } commons-math3-3.2-src/src/test/java/org/apache/commons/math3/random/BitsStreamGeneratorTest.java100644 1750 1750 4506 12126627670 31474 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.random; import java.util.Random; /** * Test cases for the BitStreamGenerator class * * @version $Id: BitsStreamGeneratorTest.java 1454847 2013-03-10 13:07:08Z luc $ */ public class BitsStreamGeneratorTest extends RandomGeneratorAbstractTest { public BitsStreamGeneratorTest() { super(); } @Override protected RandomGenerator makeGenerator() { RandomGenerator generator = new TestBitStreamGenerator(); generator.setSeed(1000); return generator; } /** * Test BitStreamGenerator using a Random as bit source. */ static class TestBitStreamGenerator extends BitsStreamGenerator { private static final long serialVersionUID = 1L; private BitRandom ran = new BitRandom(); @Override public void setSeed(int seed) { ran.setSeed(seed); clear(); } @Override public void setSeed(int[] seed) { ran.setSeed(seed[0]); } @Override public void setSeed(long seed) { ran.setSeed((int) seed); } @Override protected int next(int bits) { return ran.nextBits(bits); } } /** * Extend Random to expose next(bits) */ @SuppressWarnings("serial") static class BitRandom extends Random { public BitRandom() { super(); } public int nextBits(int bits) { return next(bits); } } } commons-math3-3.2-src/src/test/java/org/apache/commons/math3/random/Well19937cTest.java100644 1750 1750 41055 12126627670 27253 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.random; import org.junit.Assert; import org.junit.Test; public class Well19937cTest extends RandomGeneratorAbstractTest { @Override public RandomGenerator makeGenerator() { return new Well19937c(100); } @Test public void testReferenceCode() { int[] base = { 740849862, 1202665156, -199039369, -259008301, -291878969, -1164428990, -1565918811, 491009864, -1883086670, 1383450241, 1244617256, 689006653, -1576746370, -1307940314, 1421489086, 1742094000, -595495729, 1047766204, 1875773301, -1637793284, 1379017098, 262792705, 191880010, -251000180, -1753047622, -972355720, 90626881, 1644693418, 1503365577, 439653419, 1806361562, 1268823869 }; int[] init = new int[624]; for (int i = 0; i < init.length; ++i) { init[i] = base[i % base.length] + i; } Well19937c mt = new Well19937c(init); int[] refInt = { 2128528153, 327121884, 935445371, -83026433, -1041143083, 2084595880, -1073535198, -1678863790, -523636021, -1514837782, -736786810, 1527711112, -1051227939, 978703380, 410322163, 1727815703, -648426354, 636056441, 1954420292, 17754810, -958628705, -1091307602, 1793078738, -1680336346, 1792171272, 941973796, -2066152330, -1248758068, -1061211586, 262020189, 1276960217, -233886784, 1767509252, -1811939255, -406116097, -742435920, -1349799525, 240329556, -332161345, 1488943143, -332244280, 2093328957, 674753300, -1930135556, 257111467, 63793650, -1964335223, 1315849133, -797349146, 1372022250, -1451892049, -1325138957, -870401239, -1294317369, 91490879, 386205044, -704074702, -1230679067, 1513674392, -262996240, 1196007314, 1398903796, 803719762, -1750926831, -1268814180, 1233515404, 1498313934, -970591257, 611113671, -261632474, 1834097325, 1709440492, -150396854, 2120561003, -62645660, 479080234, 1535125050, 1823378695, -1129289329, -1095198399, 2092564733, 78836308, -692015409, 1647147229, -1847922219, 1838279320, -848333841, -1375151778, 920238861, 1512628290, -749439404, 288851918, -427218675, 679640964, 425700808, -2077624511, -1929434455, -647176419, 650437190, -1926749131, -1564744729, 734494454, 108193743, 246246679, 810042628, 1952337771, 1089253730, -1874275331, 1428419392, -492969232, 1945270770, -201265602, -755490251, -624426214, -699605715, -113446478, 809091299, -1521531511, 1136505389, -523660964, 132928433, 1926559713, -1485314325, -508322506, 46307756, -1627479740, -589386406, -1855555892, 584299545, 1272841066, -597242658, 925134545, 1102566453, -753335037, -9523218, -1778632375, 568963646, 764338254, 1259944540, -2000124642, 1307414525, -151384482, 807294400, 1993749511, -15503094, -709471492, 2104830082, 1387684315, -1929056119, 224254668, -733550950, -889466978, -1987783335, -437144026, 995905753, -1021386158, -1096313388, -1014152835, -1303258241, 1201884788, -1845042397, 1421462511, 980805867, 2143771251, 481226968, 1790544569, 328448328, 1995857639, -66668269, -1411421267, -222586606, 866950765, -308713926, -1048350893, 993222402, -1139265642, -871837948, 1145571913, 381928580, 35386691, 1640961123, -1192981020, 775971009, 594246635, 1603197812, -575106766, 2023682000, -1636301903, -718093720, -1666421635, -2146115988, 320593570, 287355418, 454400027, 1112753817, 1751196267, 782077910, -1478447368, -1007557264, -862315517, -2035355952, 2123515250, -557641502, -1789932035, 879640129, 44167603, 791148984, 1382939723, -2135684233, 1825489580, 937345485, -1839983359, -1536880111, -1472578359, 1548052748, -1471535862, -14508727, 1509621398, -2134967452, -787485401, 815341660, -327905128, 1028096737, 866906991, -1585990806, 859229080, 234806270, 998518056, -1897890815, -900923587, 1179856752, 1529572451, 620486106, 1119836556, 1661285564, 2097404633, -1437490790, 265306115, -984880135, 1326751968, 1280043536, 680210701, 155786166, 1550973250, -325781949, -597789777, -1939780, 1345275487, 1930450001, 941449704, 669301309, 693651713, -990721514, 582968326, 976132553, -1892942099, -1065070157, -711990993, -688974833, -1026091683, 1115346827, -1305730749, -1733626381, -364566696, -21761572, -37152746, -262011730, 1302722752, -1806313409, -767072509, 764112137, 1671157377, 1837645038, -1021606421, -1781898911, -232127459, -310742675, -1818095744, -1128320656, -705565953, -354445532, -523172807, -433877202, 131904485, -64292316, 381829280, 229820263, 1797992622, 1359665678, 978481451, -885267130, -1415988446, -356533788, -961419072, 1938703090, 708344111, 679299953, 744615129, 1328811158, 1257588574, 569216282, -753296151, -1519838713, 2016884452, 1062684606, 1561736790, 2028643511, -1353001615, 886376832, 1466953172, 1664783899, 1290079981, -57483993, -1176112430, 1634916316, 1976304475, 1374136869, -648738039, 1058175869, -909000745, -1526439218, 726626991, 2066596202, 64980943, -26166577, -885900005, -1821546816, -1103727665, 730606315, -1324948459, -696956940, -1300869403, 1171578314, 797249074, -1600611618, 1928247682, 307164165, -1482476232, -1886179640, 1306433392, 1945271359, -1272113751, -1285984081, -2057145549, 795047465, 1262569087, -1239828121, 1426641636, -786371495, 2120199316, 1273690652, 74457589, -1033394229, 338952565, 46122958, 1225741533, 2115308090, 678200841, -1618264885, -101162569, -1628976330, -1232839500, 468709044, 1876019116, 92723122, 233398255, -554960844, 38494196, -406437278, 2083528643, -1106878615, -340722557, -2123964932, 223183343, 108918116, -1014629054, -901344544, -838896840, -1908460517, -1763508731, -926890833, 1703791049, -667755577, 1694418389, 791641263, 1095689677, 1119202039, -1419111438, -2012259010, 188017439, -1775110395, -1971099661, -1688113734, 131472813, -776304959, 1511388884, 2080864872, -1733824651, 1992147495, 1119828320, 1065336924, -1357606762, 462963503, 1180719494, -202678962, -892646595, 605869323, 1144255663, 878462678, -1051371303, 872374876, 631322271, -172600544, -1552071375, -1939570033, 151973117, 1640861022, 310682640, 34192866, 2057773671, -2004476027, -1879238973, 582736114, 900581664, -427390545, -1232348528, -535115984, 1321853054, 69386780, -1729375922, 1418473715, 1022091451, 496799289, -80757405, -1903543310, -1128846846, 1703964, 1984450945, 856753858, -812919184, 775486323, -1376056193, 638628840, 314243536, 1030626207, 644050997, 73923896, 362270613, 236584904, 1463240891, -223614432, 435371594, -751940030, -124274553, -1991092884, 1579624267, 1249632649, 157589625, -345229739, -366245207, -1399995986, 1651729983, 1965074340, -1108970305, 1163690769, 1732013523, -1461252895, 669755552, -476503675, -264578685, -32813949, 288222188, -25734262, 106040916, 1654395626, -365148479, 2014455846, -2040447994, 1351639280, -919975757, -1970412139, -47306532, 222377665, -363434917, -1091717516, 2090685531, -1221091649, -1729649190, -1239406708, 1064945398, -105437479, -419675255, 74701669, -12862899, -498269844, 1566898997, -1872838355, 1596887574, 485902962, 469225597, -881763553, 1307841032, -1642872487, 1388543045, 379792876, 1095683384, 840780732, 1934378038, 1851278350, -1359389423, 130868458, -313448799, -663624816, 1031714153, -608443411, -205137499, -1849464427, 1973593637, 1068741808, -1420655961, 1188762305, 954044841, -995454462, -1818101092, -1937201943, -324541290, -1520603933, 572873173, -554764496, 1051557081, -1245136076, -985349536, 329320398, 1787901464, -37803304, -1759310177, -1463492617, -1861729663, 1251768782, 256937091, -779036948, -2049893864, 1256022877, 1228075657, -1550195255, -611319853, 1190797155, 2047604112, -576077160, -1532843331, -1324899394, -159729560, -622525946, -1080302767, -236033484, 1895243903, -410123689, -1944154157, -681781021, 1208453003, 579595878, 1303914051, -145607082, -131567277, -1917288455, 894217359, -175688726, -1585480723, 663691440, -1140068263, -641711178, 1596080008, 629197693, 976422358, -1570451095, 525923776, 895046136, -504151767, 1602553020, -1233054923, -1798474837, -1488857895, 1055782627, 261863143, 1879276655, 488240679, 1910982611, -1919441259, 370435945, 1265230086, -1293284428, -1503576227, 2076963035, -1379628250, 1157098875, 1984461153, -1947837397, 1705880124, 1453607404, -1233649748, 1479943773, -863878721, -862415630, -736723275, 940306358, -1596000684, -1174889953, -615723892, -885006597, -1796723178, 1844159055, -188942309, 2107251811, -1675486996, -1009475178, -859263556, -431866963, -9593673, -1878920923, -104853791, -1535224994, -69315537, 586690130, -1292234796, 1378749456, -301873019, -319297563, 1677205851, 292450579, -1289441171, 1788113680, 1907606333, 1464711611, -1372023606, -1978832445, -1772259768, 1949124464, 1818322887, -1138036603, 1249727628, -1474866449, -1868013169, -1384567593, 717007936, 954189997, -1900561040, 738470389, -158973180, 1732860784, 1936031206, -1133354740, -1173166665, 1432976712, 852636081, 1732064691, -1831788120, 1273933579, 455403217, 1988395890, 106493468, 506092152, -610530423, 1698053512, 1311747476, 1969503012, -1887461759, 1613543073, 903200334, -737865837, 325656800, -1234001200, 1492148864, 2009861533, -368262605, 1091338541, 2076108119, -961392337, 1835877112, 316250307, -853333391, -2125443777, 815363504, -798707803, -158146540, 690786114, -530775684, 1203556940, 1611485582, -1661412270, -53184506, 2126287444, -232222229, 1559486057, 283532250, 1202760418, 932144172, 1082594656, -570104011, 413509167, -995027177, -996477516, -540544, -745537167, -712135469, -996294983, -592787198, 1889840948, 1314628747, -394266926, -682316577, 456447239, 1728806063, -396279614, -43387643, 1915717013, -861574144, -1078710588, -561401249, 1111464540, 63643984, -1693870413, -968369980, -1053148188, 708799038, 1883537988, 373371671, -156410415, -1596483236, -1846890431, 888692915, -1025632583, -1666477591, -343066267, -2059058792, 641501628, -1744347292, 1648632991, 1743540146, 2020952406, 164014499, 990508262, 1706408228, -1236471842, -347116260, 1843634523, 827255665, 300519853, -1265974830, -547247177, -583064554, -1995437077, 689210107, -93151393, 835365056, 1706367315, -1605902756, 200954895, 431093688, -277573364, -928486713, -552221973, 145432789, 1128919795, 1675095586, 1930359882, 1215849501, -1447770583, 657776490, 1885869860, -1629237204, -868897479, -1258169760, 1828140195, -883850439, 463933909, -347361158, 1478116648, 801176896, -1501915899, 1017335748, -1654508882, 123994786, 1588785290, 791166651, -1523108535, 340411166, -496474762, -1189711141, -7392628, 2045171250, -1245366209, 834787230, -1346883181, 2034209454, 737043362, 898803323, 1983089087, -1845404320, 9585188, -1180608323, 1665100606, 1949474222, -211115008, 1151308295, -2132174259, 913126312, -2085061672, 1419864120, -1134542954, -53833957, -246913211, 468382370, -1759479323, 1136686211, 1307012488, -2036299559, -1346099736, 314743106, -1683101865, -947151948, -234529696, -2103334293, -279256894, -1484257, -1053953017, 1801205399, 941594454, -874119215, -672865187, 762284205, -1494975451, 486607927, -898264389, -1711861093, -212572760, 2106484281, -1610786470, 1352525590, -837779586, 1568282001, -593019125, -1146260782, -1595879979, -640781858, 1107692311, 1547132709, -1928385535, -2057772805, 634887038, 329772618, 942136006, -864405576, 501883884, 1537141484, -1180626836, 1123055420, 1090885851, 421662750, 2033111605, 1710917425, -1118058244, 74321279, 257328195, -1199940697, 208625996, -442341447, 808119183, 1166827075, 1177417517, -1856155370, -1464837036, -60624923, -1306220638, -91104698, -1434621430, 548899241, 37351476, 1478278431, -1255061434, 248470035, -104642597, -1865169521, 1418373655, -1660810523, -2129015436, 154612798, 276575732, 1930338442, 179503250, -929294855, -39452027, -1377657544, 1442322193, 1137511318, -432158653, -984801987, 743099148, -1118893528, -904123623, -1273146363, -1884800406, -803169061, 1254123158, -484252077, 317646844, 404246525, -1230293916, 1121445742, -19657507, 652967153, -1055406692, -468950719, -1493532921, -1447624258, -1369679689, -1517000228, -145853307, 1518006526, 1591195514, -1475557146, -909722097, 2103182976, -406830579, -2124025254, -1804819507, -1357512858, 567321869, 409048156, 567805180, 1749009386, 1762759722, -1770324077, 1271140844, 468219092, 955792405, 1911965665, 1876314424, -718200715, -1278883927, 1392281730, -120519585, 851473793, 245054754, -33369039, -284877584, -479534880, -212346563, -122017521, -1461429983, 1331007370, 1788621721, 1739036536, 1446350953, -1985448033, 685528610, -1386434659, 1368233993, 2021786790, 1596478397, -1716635278, -2011083017, 171876097, -311529197, 687812052, 377000657, -1055547517, -1499047842, -1818434951, -120863666, 33888043, -1387509273, -541540700, 1162597745, -1331415338, 1931708792, -850270000, 663845594, 1536495943, -322924971, -1380272203, 261190298, -204874428, -2104974031, 883819928, 155808204, -1454446035, 1323388464, -1696505728, 1549800285, 1018150463, -1327715703, -1582480640, 1013659809, -1820360082, 1666498787, 1406120540, -196541482, 1248470531, -1250433281, 836375878, 177646854, -1927020253, 2145878321, 689712096, -596605921, 348283199, 1916993096, 481356808, -339687826, 1219340319, 718895887, -2007521340, -1859185806, 2042164737, -58146784, 742449142, 1930754708, 780832111, 715056441, -1393886151, -8150527, -599607443, -537300865, -1212516084 }; for (int i = 0; i < refInt.length; ++i) { Assert.assertEquals(refInt[i], mt.nextInt()); } } } commons-math3-3.2-src/src/test/java/org/apache/commons/math3/random/Well44497aTest.java100644 1750 1750 40762 12126627670 27254 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.random; import org.junit.Assert; import org.junit.Test; public class Well44497aTest extends RandomGeneratorAbstractTest { @Override public RandomGenerator makeGenerator() { return new Well44497a(100); } @Test public void testReferenceCode() { int[] base = { 740849862, 1202665156, -199039369, -259008301, -291878969, -1164428990, -1565918811, 491009864, -1883086670, 1383450241, 1244617256, 689006653, -1576746370, -1307940314, 1421489086, 1742094000, -595495729, 1047766204, 1875773301, -1637793284, 1379017098, 262792705, 191880010, -251000180, -1753047622, -972355720, 90626881, 1644693418, 1503365577, 439653419, 1806361562, 1268823869 }; int[] init = new int[1391]; for (int i = 0; i < init.length; ++i) { init[i] = base[i % base.length] + i; } Well44497a mt = new Well44497a(init); int[] refInt = { -1464956854, -1524360321, 986845646, -182050548, -818943186, -1744848370, 1392434650, -182648505, -2026593838, 1254866610, -410459761, -1392048371, -968730026, 1485793687, -728749746, -112685463, 275126404, -1101838984, 1193096287, 443511615, -510869213, 549869992, 1974458428, -1217587840, -335835016, -2048974745, 1066947099, -611611187, 1978925459, 688164478, -463344808, 56995910, 699288809, 606392470, 117418673, 1948706703, -485598135, 385841705, 1725261146, -919553921, 70643668, 2128611684, 1720197347, 738706713, 1162026860, -611442152, 1469145601, 2051653750, 609067755, -1971782890, -971114565, 776260144, 1619791127, -1547233838, 1502505722, 913168193, 1761269649, 81782996, 62251540, 1519079156, 1239007000, 489633356, -800433470, -2107278046, 495320431, 269446836, -2013306553, 1074614697, 1645125348, 584369930, -405429577, 1211134012, -2060113546, -2047824, -443978800, 271218497, -1002185964, 1519315874, -695096464, -79101601, -1521653608, 192426133, 1159511202, -1354494985, -477280535, 583522228, -661741458, -1251175621, -369487281, -2015449518, -2102058930, -645264919, -925270025, -1674575999, 1363844609, -831732660, -1668989157, -1861246633, 83763283, -1056074975, -519054258, -1546386261, 1691674654, -885968657, -1189571815, 2095154843, 1686743191, -1859471265, -261593938, 1721982136, -491120252, -949699153, 642525852, -2005306625, -1004765905, 742736856, 1653443876, 788423835, 1536155740, 879514143, -1510757104, 115238646, 28600662, 1485490803, 1272460710, 523153480, -766782926, 1332478031, 528775440, 302965264, -2046891123, 1108139271, 1611601128, 550846467, -439082190, 1244786747, 941120547, -35568474, 1756370964, 304870369, 1902684028, -408710726, 1673189520, 1180987663, -1488131864, 158973303, 154514890, -1387953397, 1453732833, -1342263302, -628153633, 4710424, 619931109, 721411332, -2135645486, 1688696681, -891749588, -1641122924, 1397432310, -865254619, -1635468227, -1827787970, -1311416657, -1022618057, 1411688086, -1579840139, -637954674, 2115653281, -1155985079, -1043532593, -374286955, -1825883832, -227940643, 1688394137, -524577925, -983222470, -1955769926, 626525757, -2009760930, -1855453635, -676923169, 754966926, -291202391, -2126042921, -1477304277, -1409345382, -1264640578, -441993991, -17611930, -1576809974, 2137694350, 1299022733, -762509116, -1087399293, 819303572, -14571174, -719035481, -1644675278, 1492736905, -15038081, 974773023, 1087127339, 1790024863, -1493135734, 1936273291, -442361741, 1639666948, 1147532756, 174955156, -1537685747, 187972574, 275303083, 1420277149, -1375787574, 1873043153, 38164241, 653451946, 687758113, 899667071, 1722219976, 2146668333, 587401069, -26582672, 2034645447, 1401801794, 1043291001, -1277898614, 2116116828, 1445274301, 150534325, 469242183, -937704471, 171074779, -204638071, 1269913689, -771734064, -12280461, -1182158859, 1704390140, -263303749, -848503723, -1822849148, -634064465, 1130988864, -1515750310, -908815400, 1487214333, 994482967, 853103628, 1711185413, 1520342001, 1067859186, 1693632130, -603831333, 292236742, -800655385, -1467184928, 221125007, -1697377800, 1293953144, 1730537111, 1073329737, 519625212, 689636032, 1127394154, -1496469136, -1214585810, 822152197, -1572579275, -527866383, -996792678, -2058452887, -1133767559, 576275042, 1579109209, -295089371, 1502267384, -724281876, -911879875, 1131096177, 333026744, 1238706603, 1067340063, -745697708, -973992204, 1560446744, -664017057, -616056490, 1099714049, 674159948, 383625825, 1411443110, 1862818263, -1896254899, 1322476914, -719144734, -1540101945, 988154902, 781856577, 2013381051, -2059071359, -142073207, 60252832, 2052050421, -666391497, 376633738, 1663011481, -1706886481, -1870003191, 1003819645, 898131216, 778824906, -656875645, -1730811011, -1751653787, 2056079904, 231977636, 1831419220, -465545074, -1505266471, 1034419975, -133864043, 1876779821, 1879792902, -100100435, -959264741, -472668436, 203584096, -46980157, -1478047098, -979669209, 809008494, 1279644171, 2055793632, 1385672419, -1756428826, -1790481031, -2089665073, -1608595011, 457322987, 1267418945, -19541848, -796352273, -1049973752, 30940894, -539710199, -1097391703, -779353550, -1328320498, -735447662, -918513196, 1516945649, 1218919237, -251287485, 1826366637, 353082340, 889839220, 399638904, -1462573609, -618450466, 1429903706, 2095548034, 1486594475, -1053248922, 74346322, -357998703, 1790710495, -146359619, 1581657509, -797737661, -920778913, 608399665, 646679359, 1861775150, -1014371223, 476735306, -1577737028, 383018939, 1234592859, 344770283, -472763155, 187217985, 1245828866, 1936329359, 61243025, -1979390025, 903671173, 302699505, -1677126111, -1194113496, 835857595, 706998946, 70931462, 1374113464, -1464459699, -231081598, 1366205112, 396990527, -1615015619, -968458597, 457632575, 24361353, -1120685182, 2101590608, 1654666456, -1208442054, 579414359, 1078056578, 217408674, -1560683025, 815178420, 1219326466, 450032327, 774403237, 54597342, -664057229, 447132403, 50603973, 435640301, -1224073863, -1339908037, 1775470944, -1378119263, -1375189988, -1287971162, 29816317, -1313418882, -1824967031, 443540716, 11064217, -1463969487, 1967601549, 124474667, 1230898256, -1741455555, 561643750, 933295231, -923145874, 245538199, 289478386, 200552280, -268887021, -1598221376, 1236123270, 318325803, 773964550, 191670680, 158043961, -762639146, -416703928, -721969492, 1664330785, -584949010, 1509045840, -2066001147, 1728613092, -1103375821, -1262079070, -2034789427, -418216342, -546365126, 1235751589, 1639799329, 2085089663, -697590049, -2007054256, -147701903, 209371702, -1868450893, 1241065116, 1537364837, -1035970557, 318040217, 150492098, 1841159805, -491979749, -1275490577, -1759443566, -697538216, -1589624976, -678703557, -189067001, 1539472677, -1396089831, 271512148, 180483983, 483714313, 703861378, 2122114992, -600097045, 522009268, 160429181, -744428886, 1541223403, -1211039718, -1167643980, 1551471162, -816207368, -1429258613, 1350901561, 1934120609, -961643277, -214772286, -2128270227, -1561239720, 1493926966, 1376671007, 94966082, 221846150, -164351411, -51309876, 497148497, 1233668542, 266257753, -773473851, 953946385, 420815294, -1390653175, 1834391782, 4704447, -891751440, -744104272, -1082756642, 1431640408, -1912055536, -159789461, -704946016, 1956368139, 642279822, -374415338, 1562655802, -272964020, 1071498305, 667364168, -1546405154, 341389690, 1360662999, 377696332, -437020076, -1668574556, 1242655350, -756555890, 645954261, 1914624235, 2134904445, -247737098, 143667521, -17668806, 1804148531, 414247300, 1030053929, -1595215075, 887532426, 553113691, 1173830167, -303724353, -280418143, -1143962122, -1898518451, 36464746, 1189572700, -1549967582, 1093341440, -452303819, -731023001, 1111173883, 1678013973, -836458212, -842956392, 212774049, -845621791, 966282353, -823130040, 700410571, 619075141, -304785045, -1816233676, -1789653997, -166914694, 690663021, -669570330, 1098624444, -987380984, 452844935, -1089825546, 1221160503, 1217375341, 512281644, -1106887134, 1665404458, -1462594714, -207498587, -789271490, -723469709, 512055365, 1445951882, 1692473633, -996873493, 1445046730, 993087194, -1666188562, -897427329, 1008869698, 1236029718, 1499207233, 1704947182, -1815799281, 686399988, -475436580, 1588892458, 884859588, -471913926, -487416631, 1323960741, -1257612174, -468909314, -1866654496, -1417895838, 1707647971, 997140465, -1358794225, 1929422460, -605622778, -1587468566, 469149623, 1121515927, 748484204, 1201983830, -1906623591, 76398473, 261109068, -796025669, -1964466661, 1739898262, -756850622, 1581369453, 1484675811, 484136467, -705983890, -1357931714, 548520423, 741992908, 1017931838, -2078503520, 2097871343, 569233897, -91903627, 1864053450, -876129714, 336670307, -1950420274, -872316480, -662220291, 275724295, 703565412, 1334248646, -217559198, 1044090753, 743502488, -1518545318, 20614180, -768582053, 976522354, -25129962, -983639284, 71722595, -119236393, 368844119, -795808244, 696073964, 1379765302, 235083623, 666280330, -1313689346, -643870520, 534522699, -250414377, -1239276164, 159264592, -1119503518, 1168161619, -1366518946, -1335653301, 248092140, 1390152547, 2051602724, -1023547981, -1479782621, -1785785862, 1609789158, -919124123, 1703200068, -852553456, 1573706142, -376011685, 305068766, -1231775451, -1536883494, -125122369, -896696968, 852651567, -458154391, 747781704, 1173040469, -1569200836, 312506093, -1680530410, 117086271, 794587661, -1231003908, -1048955503, 2119305423, 1636729108, -522378372, 1627421730, 545077470, -1683264872, 1486496559, -1793064672, 1908368749, -1226052295, 1399248776, -588193954, -1289386125, 534647065, 2126245059, -238362987, -1244573058, -1571832269, -2052693379, 1494767731, -528668449, -980826491, -151282847, -1468523556, 1876349941, -301654558, 1467960576, -741720848, -612158589, 92376910, 987915105, 1037689578, 793773489, -1387669541, 349490139, 564784004, -1161242130, 619703053, 2063233129, 190888106, 81845991, -1482466066, 283234313, 114355492, -1879406787, -1283370924, -1378903370, -730141747, 1570738286, -281348873, 2131743196, 795654462, -497365688, 437612465, 1928618254, 1433118279, -1801292119, -2059248836, -221673230, 163637697, -411319468, 244353317, 786753178, 489172932, 464627154, 1258915212, -229028334, -994675463, 1931657329, 1784181437, -97111947, 1728952452, -1329133577, -1606156362, 1341196121, 1679632329, -796545286, -1021125869, 1427825468, -214986389, 250791528, 1029777000, 90661677, 602529506, 2068040879, 1483801763, 2332097, -457467017, 672399614, 1542769193, 1781253216, -1967165705, -2052227925, -1248173215, -1676554121, 292413596, 209649573, 1750689340, 1946874730, -832845570, 1774178655, -450175610, -431901779, 613330756, 1969434259, 1251099237, -1320908513, -50659188, 273178515, -296290724, 1195998469, 1329813722, 759419114, 1003396150, -274557619, -548886303, -2055397788, -766678640, -464045978, -1835907569, -169406709, 820751456, 1778613303, -1582073956, -1728391771, -2075389498, -1606584632, -1702107251, -15724560, 45610235, -1967510298, -671487775, -1841110041, -913365944, 869680052, -798103472, -1564096927, -918899909, -810066882, 428829752, -1413487973, -844240890, 1343914280, -689285374, 1827745702, -799686631, 1696465705, -726159000, -1381157526, 1649221296, 1791106481, -1872852642, -485685063, 1534949133, -1611901907, -581776031, 242740701, -382394666, 668419384, 388297992, 748818886, 713804061, -1783774172, -1823401590, -1009098384, 2071462929, 1154475522, 1309810666, -1734475040, 1212095416, 988288210, -1457428115, 1699730041, -1804729443, -1922824494, 1000076038, -226555981, 131425181, -1071582828, 357680377, 1574190179, 996651958, 965704429, -47651768, 243931978, 808955117, -652323633, 544967309, -1199510217, 702795379, 997685748, 1593927308, 2119371055, 1451401230, -41992913, 2033816081, -1030495962, 1764010175, 457470691, -2001190141, -373358035, -1950331268, -1291674220, 642934467, -1825725718, -1555687487, 1664472129, -24722338, 1899539596, 78519318, 1662555805, 1744711308, -2142888582, -1597853572, 118030659, 1596713428, 404304267, -1350880388, 648702031, 1185458591, 1798138033, 819516445, -1466759682, -751277607, -879817426, -1931050435, 1465603177, -1402344216, 768491239, -1404853657, -1915685264, -1845859847, 313163207, 1239598382, 1988767047, -555152530, -1925665864, -182399255, -1392390808, 64861291, -511875035, 1879964459, 918905020, -840773616, 459610189, -1522352470, -1821396360, 977274705, -60616465, -1846727880, 1208592937, -515359427, 1127607806, -395032287, 491869604, 2053794084, 568321750, 1597027438, 1355613070, -2069482724, 1899252555, 844726247, -625112193, 1146099491, -1037855139, 1203928737, 1875686061, 994108281, 1471873396, 2026801570, 4941446, -1066074241, -983738686, 2037429697, -836521112, -633388883, 1221918725, 2137035208, -369891832, 372509548, -110916409, 80517712, -658056946, 727893428, -1353651002, -475459562, -291323023, 1059377566, 591801919, 1018232602, -348255729, 1863827426, 246032476, -1026132864, -1356383176, -1224690998, 262442981, 1257773681, -1738604660, 77131430, -1320261233, -2342727, -1817187590, -1883997191, 1367221809, -1863623746, -1132606249, 149024763, -1228275128, -578030399, 356914163, 2109691820, -880313621 }; for (int i = 0; i < refInt.length; ++i) { Assert.assertEquals(refInt[i], mt.nextInt()); } } } commons-math3-3.2-src/src/test/java/org/apache/commons/math3/random/EmpiricalDistributionTest.java100644 1750 1750 55360 12126627670 32101 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.random; import java.io.BufferedReader; import java.io.File; import java.io.IOException; import java.io.InputStreamReader; import java.net.URL; import java.util.ArrayList; import java.util.Arrays; import org.apache.commons.math3.TestUtils; import org.apache.commons.math3.analysis.UnivariateFunction; import org.apache.commons.math3.analysis.integration.BaseAbstractUnivariateIntegrator; import org.apache.commons.math3.analysis.integration.IterativeLegendreGaussIntegrator; import org.apache.commons.math3.distribution.AbstractRealDistribution; import org.apache.commons.math3.distribution.NormalDistribution; import org.apache.commons.math3.distribution.RealDistribution; import org.apache.commons.math3.distribution.RealDistributionAbstractTest; import org.apache.commons.math3.distribution.UniformRealDistribution; import org.apache.commons.math3.exception.NullArgumentException; import org.apache.commons.math3.exception.OutOfRangeException; import org.apache.commons.math3.stat.descriptive.SummaryStatistics; import org.junit.Assert; import org.junit.Before; import org.junit.Test; /** * Test cases for the EmpiricalDistribution class * * @version $Id: EmpiricalDistributionTest.java 1461172 2013-03-26 15:11:18Z luc $ */ public final class EmpiricalDistributionTest extends RealDistributionAbstractTest { protected EmpiricalDistribution empiricalDistribution = null; protected EmpiricalDistribution empiricalDistribution2 = null; protected File file = null; protected URL url = null; protected double[] dataArray = null; protected final int n = 10000; @Before public void setUp() { super.setUp(); empiricalDistribution = new EmpiricalDistribution(100); // empiricalDistribution = new EmpiricalDistribution(100, new RandomDataImpl()); // XXX Deprecated API url = getClass().getResource("testData.txt"); final ArrayList list = new ArrayList(); try { empiricalDistribution2 = new EmpiricalDistribution(100); // empiricalDistribution2 = new EmpiricalDistribution(100, new RandomDataImpl()); // XXX Deprecated API BufferedReader in = new BufferedReader(new InputStreamReader( url.openStream())); String str = null; while ((str = in.readLine()) != null) { list.add(Double.valueOf(str)); } in.close(); in = null; } catch (IOException ex) { Assert.fail("IOException " + ex); } dataArray = new double[list.size()]; int i = 0; for (Double data : list) { dataArray[i] = data.doubleValue(); i++; } } /** * Test EmpiricalDistrbution.load() using sample data file.
                          * Check that the sampleCount, mu and sigma match data in * the sample data file. Also verify that load is idempotent. */ @Test public void testLoad() throws Exception { // Load from a URL empiricalDistribution.load(url); checkDistribution(); // Load again from a file (also verifies idempotency of load) File file = new File(url.toURI()); empiricalDistribution.load(file); checkDistribution(); } private void checkDistribution() { // testData File has 10000 values, with mean ~ 5.0, std dev ~ 1 // Make sure that loaded distribution matches this Assert.assertEquals(empiricalDistribution.getSampleStats().getN(),1000,10E-7); //TODO: replace with statistical tests Assert.assertEquals(empiricalDistribution.getSampleStats().getMean(), 5.069831575018909,10E-7); Assert.assertEquals(empiricalDistribution.getSampleStats().getStandardDeviation(), 1.0173699343977738,10E-7); } /** * Test EmpiricalDistrbution.load(double[]) using data taken from * sample data file.
                          * Check that the sampleCount, mu and sigma match data in * the sample data file. */ @Test public void testDoubleLoad() throws Exception { empiricalDistribution2.load(dataArray); // testData File has 10000 values, with mean ~ 5.0, std dev ~ 1 // Make sure that loaded distribution matches this Assert.assertEquals(empiricalDistribution2.getSampleStats().getN(),1000,10E-7); //TODO: replace with statistical tests Assert.assertEquals(empiricalDistribution2.getSampleStats().getMean(), 5.069831575018909,10E-7); Assert.assertEquals(empiricalDistribution2.getSampleStats().getStandardDeviation(), 1.0173699343977738,10E-7); double[] bounds = empiricalDistribution2.getGeneratorUpperBounds(); Assert.assertEquals(bounds.length, 100); Assert.assertEquals(bounds[99], 1.0, 10e-12); } /** * Generate 1000 random values and make sure they look OK.
                          * Note that there is a non-zero (but very small) probability that * these tests will fail even if the code is working as designed. */ @Test public void testNext() throws Exception { tstGen(0.1); tstDoubleGen(0.1); } /** * Make sure exception thrown if digest getNext is attempted * before loading empiricalDistribution. */ @Test public void testNexFail() { try { empiricalDistribution.getNextValue(); empiricalDistribution2.getNextValue(); Assert.fail("Expecting IllegalStateException"); } catch (IllegalStateException ex) { // expected } } /** * Make sure we can handle a grid size that is too fine */ @Test public void testGridTooFine() throws Exception { empiricalDistribution = new EmpiricalDistribution(1001); tstGen(0.1); empiricalDistribution2 = new EmpiricalDistribution(1001); tstDoubleGen(0.1); } /** * How about too fat? */ @Test public void testGridTooFat() throws Exception { empiricalDistribution = new EmpiricalDistribution(1); tstGen(5); // ridiculous tolerance; but ridiculous grid size // really just checking to make sure we do not bomb empiricalDistribution2 = new EmpiricalDistribution(1); tstDoubleGen(5); } /** * Test bin index overflow problem (BZ 36450) */ @Test public void testBinIndexOverflow() throws Exception { double[] x = new double[] {9474.94326071674, 2080107.8865462579}; new EmpiricalDistribution().load(x); } @Test public void testSerialization() { // Empty EmpiricalDistribution dist = new EmpiricalDistribution(); EmpiricalDistribution dist2 = (EmpiricalDistribution) TestUtils.serializeAndRecover(dist); verifySame(dist, dist2); // Loaded empiricalDistribution2.load(dataArray); dist2 = (EmpiricalDistribution) TestUtils.serializeAndRecover(empiricalDistribution2); verifySame(empiricalDistribution2, dist2); } @Test(expected=NullArgumentException.class) public void testLoadNullDoubleArray() { new EmpiricalDistribution().load((double[]) null); } @Test(expected=NullArgumentException.class) public void testLoadNullURL() throws Exception { new EmpiricalDistribution().load((URL) null); } @Test(expected=NullArgumentException.class) public void testLoadNullFile() throws Exception { new EmpiricalDistribution().load((File) null); } /** * MATH-298 */ @Test public void testGetBinUpperBounds() { double[] testData = {0, 1, 1, 2, 3, 4, 4, 5, 6, 7, 8, 9, 10}; EmpiricalDistribution dist = new EmpiricalDistribution(5); dist.load(testData); double[] expectedBinUpperBounds = {2, 4, 6, 8, 10}; double[] expectedGeneratorUpperBounds = {4d/13d, 7d/13d, 9d/13d, 11d/13d, 1}; double tol = 10E-12; TestUtils.assertEquals(expectedBinUpperBounds, dist.getUpperBounds(), tol); TestUtils.assertEquals(expectedGeneratorUpperBounds, dist.getGeneratorUpperBounds(), tol); } @Test public void testGeneratorConfig() { double[] testData = {0, 1, 2, 3, 4}; RandomGenerator generator = new RandomAdaptorTest.ConstantGenerator(0.5); EmpiricalDistribution dist = new EmpiricalDistribution(5, generator); dist.load(testData); for (int i = 0; i < 5; i++) { Assert.assertEquals(2.0, dist.getNextValue(), 0d); } // Verify no NPE with null generator argument dist = new EmpiricalDistribution(5, (RandomGenerator) null); dist.load(testData); dist.getNextValue(); } @Test public void testReSeed() throws Exception { empiricalDistribution.load(url); empiricalDistribution.reSeed(100); final double [] values = new double[10]; for (int i = 0; i < 10; i++) { values[i] = empiricalDistribution.getNextValue(); } empiricalDistribution.reSeed(100); for (int i = 0; i < 10; i++) { Assert.assertEquals(values[i],empiricalDistribution.getNextValue(), 0d); } } private void verifySame(EmpiricalDistribution d1, EmpiricalDistribution d2) { Assert.assertEquals(d1.isLoaded(), d2.isLoaded()); Assert.assertEquals(d1.getBinCount(), d2.getBinCount()); Assert.assertEquals(d1.getSampleStats(), d2.getSampleStats()); if (d1.isLoaded()) { for (int i = 0; i < d1.getUpperBounds().length; i++) { Assert.assertEquals(d1.getUpperBounds()[i], d2.getUpperBounds()[i], 0); } Assert.assertEquals(d1.getBinStats(), d2.getBinStats()); } } private void tstGen(double tolerance)throws Exception { empiricalDistribution.load(url); empiricalDistribution.reSeed(1000); SummaryStatistics stats = new SummaryStatistics(); for (int i = 1; i < 1000; i++) { stats.addValue(empiricalDistribution.getNextValue()); } Assert.assertEquals("mean", 5.069831575018909, stats.getMean(),tolerance); Assert.assertEquals("std dev", 1.0173699343977738, stats.getStandardDeviation(),tolerance); } private void tstDoubleGen(double tolerance)throws Exception { empiricalDistribution2.load(dataArray); empiricalDistribution2.reSeed(1000); SummaryStatistics stats = new SummaryStatistics(); for (int i = 1; i < 1000; i++) { stats.addValue(empiricalDistribution2.getNextValue()); } Assert.assertEquals("mean", 5.069831575018909, stats.getMean(), tolerance); Assert.assertEquals("std dev", 1.0173699343977738, stats.getStandardDeviation(), tolerance); } // Setup for distribution tests @Override public RealDistribution makeDistribution() { // Create a uniform distribution on [0, 10,000] final double[] sourceData = new double[n + 1]; for (int i = 0; i < n + 1; i++) { sourceData[i] = i; } EmpiricalDistribution dist = new EmpiricalDistribution(); dist.load(sourceData); return dist; } /** Uniform bin mass = 10/10001 == mass of all but the first bin */ private final double binMass = 10d / (double) (n + 1); /** Mass of first bin = 11/10001 */ private final double firstBinMass = 11d / (double) (n + 1); @Override public double[] makeCumulativeTestPoints() { final double[] testPoints = new double[] {9, 10, 15, 1000, 5004, 9999}; return testPoints; } @Override public double[] makeCumulativeTestValues() { /* * Bins should be [0, 10], (10, 20], ..., (9990, 10000] * Kernels should be N(4.5, 3.02765), N(14.5, 3.02765)... * Each bin should have mass 10/10000 = .001 */ final double[] testPoints = getCumulativeTestPoints(); final double[] cumValues = new double[testPoints.length]; final EmpiricalDistribution empiricalDistribution = (EmpiricalDistribution) makeDistribution(); final double[] binBounds = empiricalDistribution.getUpperBounds(); for (int i = 0; i < testPoints.length; i++) { final int bin = findBin(testPoints[i]); final double lower = bin == 0 ? empiricalDistribution.getSupportLowerBound() : binBounds[bin - 1]; final double upper = binBounds[bin]; // Compute bMinus = sum or mass of bins below the bin containing the point // First bin has mass 11 / 10000, the rest have mass 10 / 10000. final double bMinus = bin == 0 ? 0 : (bin - 1) * binMass + firstBinMass; final RealDistribution kernel = findKernel(lower, upper); final double withinBinKernelMass = kernel.cumulativeProbability(lower, upper); final double kernelCum = kernel.cumulativeProbability(lower, testPoints[i]); cumValues[i] = bMinus + (bin == 0 ? firstBinMass : binMass) * kernelCum/withinBinKernelMass; } return cumValues; } @Override public double[] makeDensityTestValues() { final double[] testPoints = getCumulativeTestPoints(); final double[] densityValues = new double[testPoints.length]; final EmpiricalDistribution empiricalDistribution = (EmpiricalDistribution) makeDistribution(); final double[] binBounds = empiricalDistribution.getUpperBounds(); for (int i = 0; i < testPoints.length; i++) { final int bin = findBin(testPoints[i]); final double lower = bin == 0 ? empiricalDistribution.getSupportLowerBound() : binBounds[bin - 1]; final double upper = binBounds[bin]; final RealDistribution kernel = findKernel(lower, upper); final double withinBinKernelMass = kernel.cumulativeProbability(lower, upper); final double density = kernel.density(testPoints[i]); densityValues[i] = density * (bin == 0 ? firstBinMass : binMass) / withinBinKernelMass; } return densityValues; } /** * Modify test integration bounds from the default. Because the distribution * has discontinuities at bin boundaries, integrals spanning multiple bins * will face convergence problems. Only test within-bin integrals and spans * across no more than 3 bin boundaries. */ @Override @Test public void testDensityIntegrals() { final RealDistribution distribution = makeDistribution(); final double tol = 1.0e-9; final BaseAbstractUnivariateIntegrator integrator = new IterativeLegendreGaussIntegrator(5, 1.0e-12, 1.0e-10); final UnivariateFunction d = new UnivariateFunction() { public double value(double x) { return distribution.density(x); } }; final double[] lower = {0, 5, 1000, 5001, 9995}; final double[] upper = {5, 12, 1030, 5010, 10000}; for (int i = 1; i < 5; i++) { Assert.assertEquals( distribution.cumulativeProbability( lower[i], upper[i]), integrator.integrate( 1000000, // Triangle integrals are very slow to converge d, lower[i], upper[i]), tol); } } /** * Find the bin that x belongs (relative to {@link #makeDistribution()}). */ private int findBin(double x) { // Number of bins below x should be trunc(x/10) final double nMinus = Math.floor(x / 10); final int bin = (int) Math.round(nMinus); // If x falls on a bin boundary, it is in the lower bin return Math.floor(x / 10) == x / 10 ? bin - 1 : bin; } /** * Find the within-bin kernel for the bin with lower bound lower * and upper bound upper. All bins other than the first contain 10 points * exclusive of the lower bound and are centered at (lower + upper + 1) / 2. * The first bin includes its lower bound, 0, so has different mean and * standard deviation. */ private RealDistribution findKernel(double lower, double upper) { if (lower < 1) { return new NormalDistribution(5d, 3.3166247903554); } else { return new NormalDistribution((upper + lower + 1) / 2d, 3.0276503540974917); } } @Test public void testKernelOverrideConstant() { final EmpiricalDistribution dist = new ConstantKernelEmpiricalDistribution(5); final double[] data = {1d,2d,3d, 4d,5d,6d, 7d,8d,9d, 10d,11d,12d, 13d,14d,15d}; dist.load(data); // Bin masses concentrated on 2, 5, 8, 11, 14 <- effectively discrete uniform distribution over these double[] values = {2d, 5d, 8d, 11d, 14d}; for (int i = 0; i < 20; i++) { Assert.assertTrue(Arrays.binarySearch(values, dist.sample()) >= 0); } final double tol = 10E-12; Assert.assertEquals(0.0, dist.cumulativeProbability(1), tol); Assert.assertEquals(0.2, dist.cumulativeProbability(2), tol); Assert.assertEquals(0.6, dist.cumulativeProbability(10), tol); Assert.assertEquals(0.8, dist.cumulativeProbability(12), tol); Assert.assertEquals(0.8, dist.cumulativeProbability(13), tol); Assert.assertEquals(1.0, dist.cumulativeProbability(15), tol); Assert.assertEquals(2.0, dist.inverseCumulativeProbability(0.1), tol); Assert.assertEquals(2.0, dist.inverseCumulativeProbability(0.2), tol); Assert.assertEquals(5.0, dist.inverseCumulativeProbability(0.3), tol); Assert.assertEquals(5.0, dist.inverseCumulativeProbability(0.4), tol); Assert.assertEquals(8.0, dist.inverseCumulativeProbability(0.5), tol); Assert.assertEquals(8.0, dist.inverseCumulativeProbability(0.6), tol); } @Test public void testKernelOverrideUniform() { final EmpiricalDistribution dist = new UniformKernelEmpiricalDistribution(5); final double[] data = {1d,2d,3d, 4d,5d,6d, 7d,8d,9d, 10d,11d,12d, 13d,14d,15d}; dist.load(data); // Kernels are uniform distributions on [1,3], [4,6], [7,9], [10,12], [13,15] final double bounds[] = {3d, 6d, 9d, 12d}; final double tol = 10E-12; for (int i = 0; i < 20; i++) { final double v = dist.sample(); // Make sure v is not in the excluded range between bins - that is (bounds[i], bounds[i] + 1) for (int j = 0; j < bounds.length; j++) { Assert.assertFalse(v > bounds[j] + tol && v < bounds[j] + 1 - tol); } } Assert.assertEquals(0.0, dist.cumulativeProbability(1), tol); Assert.assertEquals(0.1, dist.cumulativeProbability(2), tol); Assert.assertEquals(0.6, dist.cumulativeProbability(10), tol); Assert.assertEquals(0.8, dist.cumulativeProbability(12), tol); Assert.assertEquals(0.8, dist.cumulativeProbability(13), tol); Assert.assertEquals(1.0, dist.cumulativeProbability(15), tol); Assert.assertEquals(2.0, dist.inverseCumulativeProbability(0.1), tol); Assert.assertEquals(3.0, dist.inverseCumulativeProbability(0.2), tol); Assert.assertEquals(5.0, dist.inverseCumulativeProbability(0.3), tol); Assert.assertEquals(6.0, dist.inverseCumulativeProbability(0.4), tol); Assert.assertEquals(8.0, dist.inverseCumulativeProbability(0.5), tol); Assert.assertEquals(9.0, dist.inverseCumulativeProbability(0.6), tol); } /** * Empirical distribution using a constant smoothing kernel. */ private class ConstantKernelEmpiricalDistribution extends EmpiricalDistribution { private static final long serialVersionUID = 1L; public ConstantKernelEmpiricalDistribution(int i) { super(i); } // Use constant distribution equal to bin mean within bin protected RealDistribution getKernel(SummaryStatistics bStats) { return new ConstantDistribution(bStats.getMean()); } } /** * Empirical distribution using a uniform smoothing kernel. */ private class UniformKernelEmpiricalDistribution extends EmpiricalDistribution { public UniformKernelEmpiricalDistribution(int i) { super(i); } protected RealDistribution getKernel(SummaryStatistics bStats) { return new UniformRealDistribution(randomData.getRandomGenerator(), bStats.getMin(), bStats.getMax(), UniformRealDistribution.DEFAULT_INVERSE_ABSOLUTE_ACCURACY); } } /** * Distribution that takes just one value. */ private class ConstantDistribution extends AbstractRealDistribution { private static final long serialVersionUID = 1L; /** Singleton value in the sample space */ private final double c; public ConstantDistribution(double c) { this.c = c; } public double density(double x) { return 0; } public double cumulativeProbability(double x) { return x < c ? 0 : 1; } @Override public double inverseCumulativeProbability(double p) { if (p < 0.0 || p > 1.0) { throw new OutOfRangeException(p, 0, 1); } return c; } public double getNumericalMean() { return c; } public double getNumericalVariance() { return 0; } public double getSupportLowerBound() { return c; } public double getSupportUpperBound() { return c; } public boolean isSupportLowerBoundInclusive() { return false; } public boolean isSupportUpperBoundInclusive() { return true; } public boolean isSupportConnected() { return true; } @Override public double sample() { return c; } } } commons-math3-3.2-src/src/test/java/org/apache/commons/math3/random/UniformRandomGeneratorTest.java100644 1750 1750 2724 12126627670 32177 0ustarlucluc 0 0 //Licensed to the Apache Software Foundation (ASF) under one //or more contributor license agreements. See the NOTICE file //distributed with this work for additional information //regarding copyright ownership. The ASF licenses this file //to you 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. package org.apache.commons.math3.random; import org.apache.commons.math3.stat.StatUtils; import org.junit.Assert; import org.junit.Test; public class UniformRandomGeneratorTest { @Test public void testMeanAndStandardDeviation() { RandomGenerator rg = new JDKRandomGenerator(); rg.setSeed(17399225432l); UniformRandomGenerator generator = new UniformRandomGenerator(rg); double[] sample = new double[10000]; for (int i = 0; i < sample.length; ++i) { sample[i] = generator.nextNormalizedDouble(); } Assert.assertEquals(0.0, StatUtils.mean(sample), 0.07); Assert.assertEquals(1.0, StatUtils.variance(sample), 0.02); } } commons-math3-3.2-src/src/test/java/org/apache/commons/math3/random/ValueServerTest.java100644 1750 1750 20427 12126627670 30033 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.random; import java.net.URL; import java.util.Arrays; import org.apache.commons.math3.RetryRunner; import org.apache.commons.math3.exception.MathIllegalStateException; import org.apache.commons.math3.exception.ZeroException; import org.apache.commons.math3.stat.descriptive.SummaryStatistics; import org.junit.Assert; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; /** * Test cases for the ValueServer class. * * @version $Id: ValueServerTest.java 1425978 2012-12-26 18:19:30Z psteitz $ */ @RunWith(RetryRunner.class) public final class ValueServerTest { private ValueServer vs = new ValueServer(new Well19937c(100)); @Before public void setUp() { vs.setMode(ValueServer.DIGEST_MODE); URL url = getClass().getResource("testData.txt"); vs.setValuesFileURL(url); } /** * Generate 1000 random values and make sure they look OK.
                          * Note that there is a non-zero (but very small) probability that * these tests will fail even if the code is working as designed. */ @Test public void testNextDigest() throws Exception { double next = 0.0; double tolerance = 0.1; vs.computeDistribution(); Assert.assertTrue("empirical distribution property", vs.getEmpiricalDistribution() != null); SummaryStatistics stats = new SummaryStatistics(); for (int i = 1; i < 1000; i++) { next = vs.getNext(); stats.addValue(next); } Assert.assertEquals("mean", 5.069831575018909, stats.getMean(), tolerance); Assert.assertEquals("std dev", 1.0173699343977738, stats.getStandardDeviation(), tolerance); vs.computeDistribution(500); stats = new SummaryStatistics(); for (int i = 1; i < 1000; i++) { next = vs.getNext(); stats.addValue(next); } Assert.assertEquals("mean", 5.069831575018909, stats.getMean(), tolerance); Assert.assertEquals("std dev", 1.0173699343977738, stats.getStandardDeviation(), tolerance); } /** * Verify that when provided with fixed seeds, stochastic modes * generate fixed sequences. Verifies the fix for MATH-654. */ @Test public void testFixedSeed() throws Exception { ValueServer valueServer = new ValueServer(); URL url = getClass().getResource("testData.txt"); valueServer.setValuesFileURL(url); valueServer.computeDistribution(); checkFixedSeed(valueServer, ValueServer.DIGEST_MODE); checkFixedSeed(valueServer, ValueServer.EXPONENTIAL_MODE); checkFixedSeed(valueServer, ValueServer.GAUSSIAN_MODE); checkFixedSeed(valueServer, ValueServer.UNIFORM_MODE); } /** * Do the check for {@link #testFixedSeed()} * @param mode ValueServer mode */ private void checkFixedSeed(ValueServer valueServer, int mode) throws Exception { valueServer.reSeed(1000); valueServer.setMode(mode); double[][] values = new double[2][100]; for (int i = 0; i < 100; i++) { values[0][i] = valueServer.getNext(); } valueServer.reSeed(1000); for (int i = 0; i < 100; i++) { values[1][i] = valueServer.getNext(); } Assert.assertTrue(Arrays.equals(values[0], values[1])); } /** * Make sure exception thrown if digest getNext is attempted * before loading empiricalDistribution. */ @Test public void testNextDigestFail() throws Exception { try { vs.getNext(); Assert.fail("Expecting IllegalStateException"); } catch (IllegalStateException ex) {} } @Test public void testEmptyReplayFile() throws Exception { try { URL url = getClass().getResource("emptyFile.txt"); vs.setMode(ValueServer.REPLAY_MODE); vs.setValuesFileURL(url); vs.getNext(); Assert.fail("an exception should have been thrown"); } catch (MathIllegalStateException mise) { // expected behavior } } @Test public void testEmptyDigestFile() throws Exception { try { URL url = getClass().getResource("emptyFile.txt"); vs.setMode(ValueServer.DIGEST_MODE); vs.setValuesFileURL(url); vs.computeDistribution(); Assert.fail("an exception should have been thrown"); } catch (ZeroException ze) { // expected behavior } } /** * Test ValueServer REPLAY_MODE using values in testData file.
                          * Check that the values 1,2,1001,1002 match data file values 1 and 2. * the sample data file. */ @Test public void testReplay() throws Exception { double firstDataValue = 4.038625496201205; double secondDataValue = 3.6485326248346936; double tolerance = 10E-15; double compareValue = 0.0d; vs.setMode(ValueServer.REPLAY_MODE); vs.resetReplayFile(); compareValue = vs.getNext(); Assert.assertEquals(compareValue,firstDataValue,tolerance); compareValue = vs.getNext(); Assert.assertEquals(compareValue,secondDataValue,tolerance); for (int i = 3; i < 1001; i++) { compareValue = vs.getNext(); } compareValue = vs.getNext(); Assert.assertEquals(compareValue,firstDataValue,tolerance); compareValue = vs.getNext(); Assert.assertEquals(compareValue,secondDataValue,tolerance); vs.closeReplayFile(); // make sure no NPE vs.closeReplayFile(); } /** * Test other ValueServer modes */ @Test public void testModes() throws Exception { vs.setMode(ValueServer.CONSTANT_MODE); vs.setMu(0); Assert.assertEquals("constant mode test",vs.getMu(),vs.getNext(),Double.MIN_VALUE); vs.setMode(ValueServer.UNIFORM_MODE); vs.setMu(2); double val = vs.getNext(); Assert.assertTrue(val > 0 && val < 4); vs.setSigma(1); vs.setMode(ValueServer.GAUSSIAN_MODE); val = vs.getNext(); Assert.assertTrue("gaussian value close enough to mean", val < vs.getMu() + 100*vs.getSigma()); vs.setMode(ValueServer.EXPONENTIAL_MODE); val = vs.getNext(); Assert.assertTrue(val > 0); try { vs.setMode(1000); vs.getNext(); Assert.fail("bad mode, expecting IllegalStateException"); } catch (IllegalStateException ex) { // ignored } } /** * Test fill */ @Test public void testFill() throws Exception { vs.setMode(ValueServer.CONSTANT_MODE); vs.setMu(2); double[] val = new double[5]; vs.fill(val); for (int i = 0; i < 5; i++) { Assert.assertEquals("fill test in place",2,val[i],Double.MIN_VALUE); } double v2[] = vs.fill(3); for (int i = 0; i < 3; i++) { Assert.assertEquals("fill test in place",2,v2[i],Double.MIN_VALUE); } } /** * Test getters to make Clover happy */ @Test public void testProperties() throws Exception { vs.setMode(ValueServer.CONSTANT_MODE); Assert.assertEquals("mode test",ValueServer.CONSTANT_MODE,vs.getMode()); vs.setValuesFileURL("http://www.apache.org"); URL url = vs.getValuesFileURL(); Assert.assertEquals("valuesFileURL test","http://www.apache.org",url.toString()); } } commons-math3-3.2-src/src/test/java/org/apache/commons/math3/random/MersenneTwisterTest.java100644 1750 1750 72357 12126627670 30737 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.random; import org.junit.Assert; import org.junit.Test; public class MersenneTwisterTest extends RandomGeneratorAbstractTest { @Override protected RandomGenerator makeGenerator() { return new MersenneTwister(111); } // TODO: Some of the tests moved up to RandomGeneratorAbstractTest tested alternative seeding / constructors // Tests exercising these features directly should be added to this class. @Test public void testMakotoNishimura() { MersenneTwister mt = new MersenneTwister(new int[] {0x123, 0x234, 0x345, 0x456}); long[] refInt = { 1067595299l, 955945823l, 477289528l, 4107218783l, 4228976476l, 3344332714l, 3355579695l, 227628506l, 810200273l, 2591290167l, 2560260675l, 3242736208l, 646746669l, 1479517882l, 4245472273l, 1143372638l, 3863670494l, 3221021970l, 1773610557l, 1138697238l, 1421897700l, 1269916527l, 2859934041l, 1764463362l, 3874892047l, 3965319921l, 72549643l, 2383988930l, 2600218693l, 3237492380l, 2792901476l, 725331109l, 605841842l, 271258942l, 715137098l, 3297999536l, 1322965544l, 4229579109l, 1395091102l, 3735697720l, 2101727825l, 3730287744l, 2950434330l, 1661921839l, 2895579582l, 2370511479l, 1004092106l, 2247096681l, 2111242379l, 3237345263l, 4082424759l, 219785033l, 2454039889l, 3709582971l, 835606218l, 2411949883l, 2735205030l, 756421180l, 2175209704l, 1873865952l, 2762534237l, 4161807854l, 3351099340l, 181129879l, 3269891896l, 776029799l, 2218161979l, 3001745796l, 1866825872l, 2133627728l, 34862734l, 1191934573l, 3102311354l, 2916517763l, 1012402762l, 2184831317l, 4257399449l, 2899497138l, 3818095062l, 3030756734l, 1282161629l, 420003642l, 2326421477l, 2741455717l, 1278020671l, 3744179621l, 271777016l, 2626330018l, 2560563991l, 3055977700l, 4233527566l, 1228397661l, 3595579322l, 1077915006l, 2395931898l, 1851927286l, 3013683506l, 1999971931l, 3006888962l, 1049781534l, 1488758959l, 3491776230l, 104418065l, 2448267297l, 3075614115l, 3872332600l, 891912190l, 3936547759l, 2269180963l, 2633455084l, 1047636807l, 2604612377l, 2709305729l, 1952216715l, 207593580l, 2849898034l, 670771757l, 2210471108l, 467711165l, 263046873l, 3569667915l, 1042291111l, 3863517079l, 1464270005l, 2758321352l, 3790799816l, 2301278724l, 3106281430l, 7974801l, 2792461636l, 555991332l, 621766759l, 1322453093l, 853629228l, 686962251l, 1455120532l, 957753161l, 1802033300l, 1021534190l, 3486047311l, 1902128914l, 3701138056l, 4176424663l, 1795608698l, 560858864l, 3737752754l, 3141170998l, 1553553385l, 3367807274l, 711546358l, 2475125503l, 262969859l, 251416325l, 2980076994l, 1806565895l, 969527843l, 3529327173l, 2736343040l, 2987196734l, 1649016367l, 2206175811l, 3048174801l, 3662503553l, 3138851612l, 2660143804l, 1663017612l, 1816683231l, 411916003l, 3887461314l, 2347044079l, 1015311755l, 1203592432l, 2170947766l, 2569420716l, 813872093l, 1105387678l, 1431142475l, 220570551l, 4243632715l, 4179591855l, 2607469131l, 3090613241l, 282341803l, 1734241730l, 1391822177l, 1001254810l, 827927915l, 1886687171l, 3935097347l, 2631788714l, 3905163266l, 110554195l, 2447955646l, 3717202975l, 3304793075l, 3739614479l, 3059127468l, 953919171l, 2590123714l, 1132511021l, 3795593679l, 2788030429l, 982155079l, 3472349556l, 859942552l, 2681007391l, 2299624053l, 647443547l, 233600422l, 608168955l, 3689327453l, 1849778220l, 1608438222l, 3968158357l, 2692977776l, 2851872572l, 246750393l, 3582818628l, 3329652309l, 4036366910l, 1012970930l, 950780808l, 3959768744l, 2538550045l, 191422718l, 2658142375l, 3276369011l, 2927737484l, 1234200027l, 1920815603l, 3536074689l, 1535612501l, 2184142071l, 3276955054l, 428488088l, 2378411984l, 4059769550l, 3913744741l, 2732139246l, 64369859l, 3755670074l, 842839565l, 2819894466l, 2414718973l, 1010060670l, 1839715346l, 2410311136l, 152774329l, 3485009480l, 4102101512l, 2852724304l, 879944024l, 1785007662l, 2748284463l, 1354768064l, 3267784736l, 2269127717l, 3001240761l, 3179796763l, 895723219l, 865924942l, 4291570937l, 89355264l, 1471026971l, 4114180745l, 3201939751l, 2867476999l, 2460866060l, 3603874571l, 2238880432l, 3308416168l, 2072246611l, 2755653839l, 3773737248l, 1709066580l, 4282731467l, 2746170170l, 2832568330l, 433439009l, 3175778732l, 26248366l, 2551382801l, 183214346l, 3893339516l, 1928168445l, 1337157619l, 3429096554l, 3275170900l, 1782047316l, 4264403756l, 1876594403l, 4289659572l, 3223834894l, 1728705513l, 4068244734l, 2867840287l, 1147798696l, 302879820l, 1730407747l, 1923824407l, 1180597908l, 1569786639l, 198796327l, 560793173l, 2107345620l, 2705990316l, 3448772106l, 3678374155l, 758635715l, 884524671l, 486356516l, 1774865603l, 3881226226l, 2635213607l, 1181121587l, 1508809820l, 3178988241l, 1594193633l, 1235154121l, 326117244l, 2304031425l, 937054774l, 2687415945l, 3192389340l, 2003740439l, 1823766188l, 2759543402l, 10067710l, 1533252662l, 4132494984l, 82378136l, 420615890l, 3467563163l, 541562091l, 3535949864l, 2277319197l, 3330822853l, 3215654174l, 4113831979l, 4204996991l, 2162248333l, 3255093522l, 2219088909l, 2978279037l, 255818579l, 2859348628l, 3097280311l, 2569721123l, 1861951120l, 2907080079l, 2719467166l, 998319094l, 2521935127l, 2404125338l, 259456032l, 2086860995l, 1839848496l, 1893547357l, 2527997525l, 1489393124l, 2860855349l, 76448234l, 2264934035l, 744914583l, 2586791259l, 1385380501l, 66529922l, 1819103258l, 1899300332l, 2098173828l, 1793831094l, 276463159l, 360132945l, 4178212058l, 595015228l, 177071838l, 2800080290l, 1573557746l, 1548998935l, 378454223l, 1460534296l, 1116274283l, 3112385063l, 3709761796l, 827999348l, 3580042847l, 1913901014l, 614021289l, 4278528023l, 1905177404l, 45407939l, 3298183234l, 1184848810l, 3644926330l, 3923635459l, 1627046213l, 3677876759l, 969772772l, 1160524753l, 1522441192l, 452369933l, 1527502551l, 832490847l, 1003299676l, 1071381111l, 2891255476l, 973747308l, 4086897108l, 1847554542l, 3895651598l, 2227820339l, 1621250941l, 2881344691l, 3583565821l, 3510404498l, 849362119l, 862871471l, 797858058l, 2867774932l, 2821282612l, 3272403146l, 3997979905l, 209178708l, 1805135652l, 6783381l, 2823361423l, 792580494l, 4263749770l, 776439581l, 3798193823l, 2853444094l, 2729507474l, 1071873341l, 1329010206l, 1289336450l, 3327680758l, 2011491779l, 80157208l, 922428856l, 1158943220l, 1667230961l, 2461022820l, 2608845159l, 387516115l, 3345351910l, 1495629111l, 4098154157l, 3156649613l, 3525698599l, 4134908037l, 446713264l, 2137537399l, 3617403512l, 813966752l, 1157943946l, 3734692965l, 1680301658l, 3180398473l, 3509854711l, 2228114612l, 1008102291l, 486805123l, 863791847l, 3189125290l, 1050308116l, 3777341526l, 4291726501l, 844061465l, 1347461791l, 2826481581l, 745465012l, 2055805750l, 4260209475l, 2386693097l, 2980646741l, 447229436l, 2077782664l, 1232942813l, 4023002732l, 1399011509l, 3140569849l, 2579909222l, 3794857471l, 900758066l, 2887199683l, 1720257997l, 3367494931l, 2668921229l, 955539029l, 3818726432l, 1105704962l, 3889207255l, 2277369307l, 2746484505l, 1761846513l, 2413916784l, 2685127085l, 4240257943l, 1166726899l, 4215215715l, 3082092067l, 3960461946l, 1663304043l, 2087473241l, 4162589986l, 2507310778l, 1579665506l, 767234210l, 970676017l, 492207530l, 1441679602l, 1314785090l, 3262202570l, 3417091742l, 1561989210l, 3011406780l, 1146609202l, 3262321040l, 1374872171l, 1634688712l, 1280458888l, 2230023982l, 419323804l, 3262899800l, 39783310l, 1641619040l, 1700368658l, 2207946628l, 2571300939l, 2424079766l, 780290914l, 2715195096l, 3390957695l, 163151474l, 2309534542l, 1860018424l, 555755123l, 280320104l, 1604831083l, 2713022383l, 1728987441l, 3639955502l, 623065489l, 3828630947l, 4275479050l, 3516347383l, 2343951195l, 2430677756l, 635534992l, 3868699749l, 808442435l, 3070644069l, 4282166003l, 2093181383l, 2023555632l, 1568662086l, 3422372620l, 4134522350l, 3016979543l, 3259320234l, 2888030729l, 3185253876l, 4258779643l, 1267304371l, 1022517473l, 815943045l, 929020012l, 2995251018l, 3371283296l, 3608029049l, 2018485115l, 122123397l, 2810669150l, 1411365618l, 1238391329l, 1186786476l, 3155969091l, 2242941310l, 1765554882l, 279121160l, 4279838515l, 1641578514l, 3796324015l, 13351065l, 103516986l, 1609694427l, 551411743l, 2493771609l, 1316337047l, 3932650856l, 4189700203l, 463397996l, 2937735066l, 1855616529l, 2626847990l, 55091862l, 3823351211l, 753448970l, 4045045500l, 1274127772l, 1124182256l, 92039808l, 2126345552l, 425973257l, 386287896l, 2589870191l, 1987762798l, 4084826973l, 2172456685l, 3366583455l, 3602966653l, 2378803535l, 2901764433l, 3716929006l, 3710159000l, 2653449155l, 3469742630l, 3096444476l, 3932564653l, 2595257433l, 318974657l, 3146202484l, 853571438l, 144400272l, 3768408841l, 782634401l, 2161109003l, 570039522l, 1886241521l, 14249488l, 2230804228l, 1604941699l, 3928713335l, 3921942509l, 2155806892l, 134366254l, 430507376l, 1924011722l, 276713377l, 196481886l, 3614810992l, 1610021185l, 1785757066l, 851346168l, 3761148643l, 2918835642l, 3364422385l, 3012284466l, 3735958851l, 2643153892l, 3778608231l, 1164289832l, 205853021l, 2876112231l, 3503398282l, 3078397001l, 3472037921l, 1748894853l, 2740861475l, 316056182l, 1660426908l, 168885906l, 956005527l, 3984354789l, 566521563l, 1001109523l, 1216710575l, 2952284757l, 3834433081l, 3842608301l, 2467352408l, 3974441264l, 3256601745l, 1409353924l, 1329904859l, 2307560293l, 3125217879l, 3622920184l, 3832785684l, 3882365951l, 2308537115l, 2659155028l, 1450441945l, 3532257603l, 3186324194l, 1225603425l, 1124246549l, 175808705l, 3009142319l, 2796710159l, 3651990107l, 160762750l, 1902254979l, 1698648476l, 1134980669l, 497144426l, 3302689335l, 4057485630l, 3603530763l, 4087252587l, 427812652l, 286876201l, 823134128l, 1627554964l, 3745564327l, 2589226092l, 4202024494l, 62878473l, 3275585894l, 3987124064l, 2791777159l, 1916869511l, 2585861905l, 1375038919l, 1403421920l, 60249114l, 3811870450l, 3021498009l, 2612993202l, 528933105l, 2757361321l, 3341402964l, 2621861700l, 273128190l, 4015252178l, 3094781002l, 1621621288l, 2337611177l, 1796718448l, 1258965619l, 4241913140l, 2138560392l, 3022190223l, 4174180924l, 450094611l, 3274724580l, 617150026l, 2704660665l, 1469700689l, 1341616587l, 356715071l, 1188789960l, 2278869135l, 1766569160l, 2795896635l, 57824704l, 2893496380l, 1235723989l, 1630694347l, 3927960522l, 428891364l, 1814070806l, 2287999787l, 4125941184l, 3968103889l, 3548724050l, 1025597707l, 1404281500l, 2002212197l, 92429143l, 2313943944l, 2403086080l, 3006180634l, 3561981764l, 1671860914l, 1768520622l, 1803542985l, 844848113l, 3006139921l, 1410888995l, 1157749833l, 2125704913l, 1789979528l, 1799263423l, 741157179l, 2405862309l, 767040434l, 2655241390l, 3663420179l, 2172009096l, 2511931187l, 1680542666l, 231857466l, 1154981000l, 157168255l, 1454112128l, 3505872099l, 1929775046l, 2309422350l, 2143329496l, 2960716902l, 407610648l, 2938108129l, 2581749599l, 538837155l, 2342628867l, 430543915l, 740188568l, 1937713272l, 3315215132l, 2085587024l, 4030765687l, 766054429l, 3517641839l, 689721775l, 1294158986l, 1753287754l, 4202601348l, 1974852792l, 33459103l, 3568087535l, 3144677435l, 1686130825l, 4134943013l, 3005738435l, 3599293386l, 426570142l, 754104406l, 3660892564l, 1964545167l, 829466833l, 821587464l, 1746693036l, 1006492428l, 1595312919l, 1256599985l, 1024482560l, 1897312280l, 2902903201l, 691790057l, 1037515867l, 3176831208l, 1968401055l, 2173506824l, 1089055278l, 1748401123l, 2941380082l, 968412354l, 1818753861l, 2973200866l, 3875951774l, 1119354008l, 3988604139l, 1647155589l, 2232450826l, 3486058011l, 3655784043l, 3759258462l, 847163678l, 1082052057l, 989516446l, 2871541755l, 3196311070l, 3929963078l, 658187585l, 3664944641l, 2175149170l, 2203709147l, 2756014689l, 2456473919l, 3890267390l, 1293787864l, 2830347984l, 3059280931l, 4158802520l, 1561677400l, 2586570938l, 783570352l, 1355506163l, 31495586l, 3789437343l, 3340549429l, 2092501630l, 896419368l, 671715824l, 3530450081l, 3603554138l, 1055991716l, 3442308219l, 1499434728l, 3130288473l, 3639507000l, 17769680l, 2259741420l, 487032199l, 4227143402l, 3693771256l, 1880482820l, 3924810796l, 381462353l, 4017855991l, 2452034943l, 2736680833l, 2209866385l, 2128986379l, 437874044l, 595759426l, 641721026l, 1636065708l, 3899136933l, 629879088l, 3591174506l, 351984326l, 2638783544l, 2348444281l, 2341604660l, 2123933692l, 143443325l, 1525942256l, 364660499l, 599149312l, 939093251l, 1523003209l, 106601097l, 376589484l, 1346282236l, 1297387043l, 764598052l, 3741218111l, 933457002l, 1886424424l, 3219631016l, 525405256l, 3014235619l, 323149677l, 2038881721l, 4100129043l, 2851715101l, 2984028078l, 1888574695l, 2014194741l, 3515193880l, 4180573530l, 3461824363l, 2641995497l, 3179230245l, 2902294983l, 2217320456l, 4040852155l, 1784656905l, 3311906931l, 87498458l, 2752971818l, 2635474297l, 2831215366l, 3682231106l, 2920043893l, 3772929704l, 2816374944l, 309949752l, 2383758854l, 154870719l, 385111597l, 1191604312l, 1840700563l, 872191186l, 2925548701l, 1310412747l, 2102066999l, 1504727249l, 3574298750l, 1191230036l, 3330575266l, 3180292097l, 3539347721l, 681369118l, 3305125752l, 3648233597l, 950049240l, 4173257693l, 1760124957l, 512151405l, 681175196l, 580563018l, 1169662867l, 4015033554l, 2687781101l, 699691603l, 2673494188l, 1137221356l, 123599888l, 472658308l, 1053598179l, 1012713758l, 3481064843l, 3759461013l, 3981457956l, 3830587662l, 1877191791l, 3650996736l, 988064871l, 3515461600l, 4089077232l, 2225147448l, 1249609188l, 2643151863l, 3896204135l, 2416995901l, 1397735321l, 3460025646l }; double[] refDouble = { 0.76275443, 0.99000644, 0.98670464, 0.10143112, 0.27933125, 0.69867227, 0.94218740, 0.03427201, 0.78842173, 0.28180608, 0.92179002, 0.20785655, 0.54534773, 0.69644020, 0.38107718, 0.23978165, 0.65286910, 0.07514568, 0.22765211, 0.94872929, 0.74557914, 0.62664415, 0.54708246, 0.90959343, 0.42043116, 0.86334511, 0.19189126, 0.14718544, 0.70259889, 0.63426346, 0.77408121, 0.04531601, 0.04605807, 0.88595519, 0.69398270, 0.05377184, 0.61711170, 0.05565708, 0.10133577, 0.41500776, 0.91810699, 0.22320679, 0.23353705, 0.92871862, 0.98897234, 0.19786706, 0.80558809, 0.06961067, 0.55840445, 0.90479405, 0.63288060, 0.95009721, 0.54948447, 0.20645042, 0.45000959, 0.87050869, 0.70806991, 0.19406895, 0.79286390, 0.49332866, 0.78483914, 0.75145146, 0.12341941, 0.42030252, 0.16728160, 0.59906494, 0.37575460, 0.97815160, 0.39815952, 0.43595080, 0.04952478, 0.33917805, 0.76509902, 0.61034321, 0.90654701, 0.92915732, 0.85365931, 0.18812377, 0.65913428, 0.28814566, 0.59476081, 0.27835931, 0.60722542, 0.68310435, 0.69387186, 0.03699800, 0.65897714, 0.17527003, 0.02889304, 0.86777366, 0.12352068, 0.91439461, 0.32022990, 0.44445731, 0.34903686, 0.74639273, 0.65918367, 0.92492794, 0.31872642, 0.77749724, 0.85413832, 0.76385624, 0.32744211, 0.91326300, 0.27458185, 0.22190155, 0.19865383, 0.31227402, 0.85321225, 0.84243342, 0.78544200, 0.71854080, 0.92503892, 0.82703064, 0.88306297, 0.47284073, 0.70059042, 0.48003761, 0.38671694, 0.60465770, 0.41747204, 0.47163243, 0.72750808, 0.65830223, 0.10955369, 0.64215401, 0.23456345, 0.95944940, 0.72822249, 0.40888451, 0.69980355, 0.26677428, 0.57333635, 0.39791582, 0.85377858, 0.76962816, 0.72004885, 0.90903087, 0.51376506, 0.37732665, 0.12691640, 0.71249738, 0.81217908, 0.37037313, 0.32772374, 0.14238259, 0.05614811, 0.74363008, 0.39773267, 0.94859135, 0.31452454, 0.11730313, 0.62962618, 0.33334237, 0.45547255, 0.10089665, 0.56550662, 0.60539371, 0.16027624, 0.13245301, 0.60959939, 0.04671662, 0.99356286, 0.57660859, 0.40269560, 0.45274629, 0.06699735, 0.85064246, 0.87742744, 0.54508392, 0.87242982, 0.29321385, 0.67660627, 0.68230715, 0.79052073, 0.48592054, 0.25186266, 0.93769755, 0.28565487, 0.47219067, 0.99054882, 0.13155240, 0.47110470, 0.98556600, 0.84397623, 0.12875246, 0.90953202, 0.49129015, 0.23792727, 0.79481194, 0.44337770, 0.96564297, 0.67749118, 0.55684872, 0.27286897, 0.79538393, 0.61965356, 0.22487929, 0.02226018, 0.49248200, 0.42247006, 0.91797788, 0.99250134, 0.23449967, 0.52531508, 0.10246337, 0.78685622, 0.34310922, 0.89892996, 0.40454552, 0.68608407, 0.30752487, 0.83601319, 0.54956031, 0.63777550, 0.82199797, 0.24890696, 0.48801123, 0.48661910, 0.51223987, 0.32969635, 0.31075073, 0.21393155, 0.73453207, 0.15565705, 0.58584522, 0.28976728, 0.97621478, 0.61498701, 0.23891470, 0.28518540, 0.46809591, 0.18371914, 0.37597910, 0.13492176, 0.66849449, 0.82811466, 0.56240330, 0.37548956, 0.27562998, 0.27521910, 0.74096121, 0.77176757, 0.13748143, 0.99747138, 0.92504502, 0.09175241, 0.21389176, 0.21766512, 0.31183245, 0.23271221, 0.21207367, 0.57903312, 0.77523344, 0.13242613, 0.31037988, 0.01204835, 0.71652949, 0.84487594, 0.14982178, 0.57423142, 0.45677888, 0.48420169, 0.53465428, 0.52667473, 0.46880526, 0.49849733, 0.05670710, 0.79022476, 0.03872047, 0.21697212, 0.20443086, 0.28949326, 0.81678186, 0.87629474, 0.92297064, 0.27373097, 0.84625273, 0.51505586, 0.00582792, 0.33295971, 0.91848412, 0.92537226, 0.91760033, 0.07541125, 0.71745848, 0.61158698, 0.00941650, 0.03135554, 0.71527471, 0.24821915, 0.63636652, 0.86159918, 0.26450229, 0.60160194, 0.35557725, 0.24477500, 0.07186456, 0.51757096, 0.62120362, 0.97981062, 0.69954667, 0.21065616, 0.13382753, 0.27693186, 0.59644095, 0.71500764, 0.04110751, 0.95730081, 0.91600724, 0.47704678, 0.26183479, 0.34706971, 0.07545431, 0.29398385, 0.93236070, 0.60486023, 0.48015011, 0.08870451, 0.45548581, 0.91872718, 0.38142712, 0.10668643, 0.01397541, 0.04520355, 0.93822273, 0.18011940, 0.57577277, 0.91427606, 0.30911399, 0.95853475, 0.23611214, 0.69619891, 0.69601980, 0.76765372, 0.58515930, 0.49479057, 0.11288752, 0.97187699, 0.32095365, 0.57563608, 0.40760618, 0.78703383, 0.43261152, 0.90877651, 0.84686346, 0.10599030, 0.72872803, 0.19315490, 0.66152912, 0.10210518, 0.06257876, 0.47950688, 0.47062066, 0.72701157, 0.48915116, 0.66110261, 0.60170685, 0.24516994, 0.12726050, 0.03451185, 0.90864994, 0.83494878, 0.94800035, 0.91035206, 0.14480751, 0.88458997, 0.53498312, 0.15963215, 0.55378627, 0.35171349, 0.28719791, 0.09097957, 0.00667896, 0.32309622, 0.87561479, 0.42534520, 0.91748977, 0.73908457, 0.41793223, 0.99279792, 0.87908370, 0.28458072, 0.59132853, 0.98672190, 0.28547393, 0.09452165, 0.89910674, 0.53681109, 0.37931425, 0.62683489, 0.56609740, 0.24801549, 0.52948179, 0.98328855, 0.66403523, 0.55523786, 0.75886666, 0.84784685, 0.86829981, 0.71448906, 0.84670080, 0.43922919, 0.20771016, 0.64157936, 0.25664246, 0.73055695, 0.86395782, 0.65852932, 0.99061803, 0.40280575, 0.39146298, 0.07291005, 0.97200603, 0.20555729, 0.59616495, 0.08138254, 0.45796388, 0.33681125, 0.33989127, 0.18717090, 0.53545811, 0.60550838, 0.86520709, 0.34290701, 0.72743276, 0.73023855, 0.34195926, 0.65019733, 0.02765254, 0.72575740, 0.32709576, 0.03420866, 0.26061893, 0.56997511, 0.28439072, 0.84422744, 0.77637570, 0.55982168, 0.06720327, 0.58449067, 0.71657369, 0.15819609, 0.58042821, 0.07947911, 0.40193792, 0.11376012, 0.88762938, 0.67532159, 0.71223735, 0.27829114, 0.04806073, 0.21144026, 0.58830274, 0.04140071, 0.43215628, 0.12952729, 0.94668759, 0.87391019, 0.98382450, 0.27750768, 0.90849647, 0.90962737, 0.59269720, 0.96102026, 0.49544979, 0.32007095, 0.62585546, 0.03119821, 0.85953001, 0.22017528, 0.05834068, 0.80731217, 0.53799961, 0.74166948, 0.77426600, 0.43938444, 0.54862081, 0.58575513, 0.15886492, 0.73214332, 0.11649057, 0.77463977, 0.85788827, 0.17061997, 0.66838056, 0.96076133, 0.07949296, 0.68521946, 0.89986254, 0.05667410, 0.12741385, 0.83470977, 0.63969104, 0.46612929, 0.10200126, 0.01194925, 0.10476340, 0.90285217, 0.31221221, 0.32980614, 0.46041971, 0.52024973, 0.05425470, 0.28330912, 0.60426543, 0.00598243, 0.97244013, 0.21135841, 0.78561597, 0.78428734, 0.63422849, 0.32909934, 0.44771136, 0.27380750, 0.14966697, 0.18156268, 0.65686758, 0.28726350, 0.97074787, 0.63676171, 0.96649494, 0.24526295, 0.08297372, 0.54257548, 0.03166785, 0.33735355, 0.15946671, 0.02102971, 0.46228045, 0.11892296, 0.33408336, 0.29875681, 0.29847692, 0.73767569, 0.02080745, 0.62980060, 0.08082293, 0.22993106, 0.25031439, 0.87787525, 0.45150053, 0.13673441, 0.63407612, 0.97907688, 0.52241942, 0.50580158, 0.06273902, 0.05270283, 0.77031811, 0.05113352, 0.24393329, 0.75036441, 0.37436336, 0.22877652, 0.59975358, 0.85707591, 0.88691457, 0.85547165, 0.36641027, 0.58720133, 0.45462835, 0.09243817, 0.32981586, 0.07820411, 0.25421519, 0.36004706, 0.60092307, 0.46192412, 0.36758683, 0.98424170, 0.08019934, 0.68594024, 0.45826386, 0.29962317, 0.79365413, 0.89231296, 0.49478547, 0.87645944, 0.23590734, 0.28106737, 0.75026285, 0.08136314, 0.79582424, 0.76010628, 0.82792971, 0.27947652, 0.72482861, 0.82191216, 0.46171689, 0.79189752, 0.96043686, 0.51609668, 0.88995725, 0.28998963, 0.55191845, 0.03934737, 0.83033700, 0.49553013, 0.98009549, 0.19017594, 0.98347750, 0.33452066, 0.87144372, 0.72106301, 0.71272114, 0.71465963, 0.88361677, 0.85571283, 0.73782329, 0.20920458, 0.34855153, 0.46766817, 0.02780062, 0.74898344, 0.03680650, 0.44866557, 0.77426312, 0.91025891, 0.25195236, 0.87319953, 0.63265037, 0.25552148, 0.27422476, 0.95217406, 0.39281839, 0.66441573, 0.09158900, 0.94515992, 0.07800798, 0.02507888, 0.39901462, 0.17382573, 0.12141278, 0.85502334, 0.19902911, 0.02160210, 0.44460522, 0.14688742, 0.68020336, 0.71323733, 0.60922473, 0.95400380, 0.99611159, 0.90897777, 0.41073520, 0.66206647, 0.32064685, 0.62805003, 0.50677209, 0.52690101, 0.87473387, 0.73918362, 0.39826974, 0.43683919, 0.80459118, 0.32422684, 0.01958019, 0.95319576, 0.98326137, 0.83931735, 0.69060863, 0.33671416, 0.68062550, 0.65152380, 0.33392969, 0.03451730, 0.95227244, 0.68200635, 0.85074171, 0.64721009, 0.51234433, 0.73402047, 0.00969637, 0.93835057, 0.80803854, 0.31485260, 0.20089527, 0.01323282, 0.59933780, 0.31584602, 0.20209563, 0.33754800, 0.68604181, 0.24443049, 0.19952227, 0.78162632, 0.10336988, 0.11360736, 0.23536740, 0.23262256, 0.67803776, 0.48749791, 0.74658435, 0.92156640, 0.56706407, 0.36683221, 0.99157136, 0.23421374, 0.45183767, 0.91609720, 0.85573315, 0.37706276, 0.77042618, 0.30891908, 0.40709595, 0.06944866, 0.61342849, 0.88817388, 0.58734506, 0.98711323, 0.14744128, 0.63242656, 0.87704136, 0.68347125, 0.84446569, 0.43265239, 0.25146321, 0.04130111, 0.34259839, 0.92697368, 0.40878778, 0.56990338, 0.76204273, 0.19820348, 0.66314909, 0.02482844, 0.06669207, 0.50205581, 0.26084093, 0.65139159, 0.41650223, 0.09733904, 0.56344203, 0.62651696, 0.67332139, 0.58037374, 0.47258086, 0.21010758, 0.05713135, 0.89390629, 0.10781246, 0.32037450, 0.07628388, 0.34227964, 0.42190597, 0.58201860, 0.77363549, 0.49595133, 0.86031236, 0.83906769, 0.81098161, 0.26694195, 0.14215941, 0.88210306, 0.53634237, 0.12090720, 0.82480459, 0.75930318, 0.31847147, 0.92768077, 0.01037616, 0.56201727, 0.88107122, 0.35925856, 0.85860762, 0.61109408, 0.70408301, 0.58434977, 0.92192494, 0.62667915, 0.75988365, 0.06858761, 0.36156496, 0.58057195, 0.13636150, 0.57719713, 0.59340255, 0.63530602, 0.22976282, 0.71915530, 0.41162531, 0.63979565, 0.09931342, 0.79344045, 0.10893790, 0.84450224, 0.23122236, 0.99485593, 0.73637397, 0.17276368, 0.13357764, 0.74965804, 0.64991737, 0.61990341, 0.41523170, 0.05878239, 0.05687301, 0.05497131, 0.42868366, 0.42571090, 0.25810502, 0.89642955, 0.30439758, 0.39310223, 0.11357431, 0.04288255, 0.23397550, 0.11200634, 0.85621396, 0.89733974, 0.37508865, 0.42077265, 0.68597384, 0.72781399, 0.19296476, 0.61699087, 0.31667128, 0.67756410, 0.00177323, 0.05725176, 0.79474693, 0.18885238, 0.06724856, 0.68193156, 0.42202167, 0.22082041, 0.28554673, 0.64995708, 0.87851940, 0.29124547, 0.61009521, 0.87374537, 0.05743712, 0.69902994, 0.81925115, 0.45653873, 0.37236821, 0.31118709, 0.52734307, 0.39672836, 0.38185294, 0.30163915, 0.17374510, 0.04913278, 0.90404879, 0.25742801, 0.58266467, 0.97663209, 0.79823377, 0.36437958, 0.15206043, 0.26529938, 0.22690047, 0.05839021, 0.84721160, 0.18622435, 0.37809403, 0.55706977, 0.49828704, 0.47659049, 0.24289680, 0.88477595, 0.07807463, 0.56245739, 0.73490635, 0.21099431, 0.13164942, 0.75840044, 0.66877037, 0.28988183, 0.44046090, 0.24967434, 0.80048356, 0.26029740, 0.30416821, 0.64151867, 0.52067892, 0.12880774, 0.85465381, 0.02690525, 0.19149288, 0.49630295, 0.79682619, 0.43566145, 0.00288078, 0.81484193, 0.03763639, 0.68529083, 0.01339574, 0.38405386, 0.30537067, 0.22994703, 0.44000045, 0.27217985, 0.53831243, 0.02870435, 0.86282045, 0.61831306, 0.09164956, 0.25609707, 0.07445781, 0.72185784, 0.90058883, 0.30070608, 0.94476583, 0.56822213, 0.21933909, 0.96772793, 0.80063440, 0.26307906, 0.31183306, 0.16501252, 0.55436179, 0.68562285, 0.23829083, 0.86511559, 0.57868991, 0.81888344, 0.20126869, 0.93172350, 0.66028129, 0.21786948, 0.78515828, 0.10262106, 0.35390326, 0.79303876, 0.63427924, 0.90479631, 0.31024934, 0.60635447, 0.56198079, 0.63573813, 0.91854197, 0.99701497, 0.83085849, 0.31692291, 0.01925964, 0.97446405, 0.98751283, 0.60944293, 0.13751018, 0.69519957, 0.68956636, 0.56969015, 0.46440193, 0.88341765, 0.36754434, 0.89223647, 0.39786427, 0.85055280, 0.12749961, 0.79452122, 0.89449784, 0.14567830, 0.45716830, 0.74822309, 0.28200437, 0.42546044, 0.17464886, 0.68308746, 0.65496587, 0.52935411, 0.12736159, 0.61523955, 0.81590528, 0.63107864, 0.39786553, 0.20102294, 0.53292914, 0.75485590, 0.59847044, 0.32861691, 0.12125866, 0.58917183, 0.07638293, 0.86845380, 0.29192617, 0.03989733, 0.52180460, 0.32503407, 0.64071852, 0.69516575, 0.74254998, 0.54587026, 0.48713246, 0.32920155, 0.08719954, 0.63497059, 0.54328459, 0.64178757, 0.45583809, 0.70694291, 0.85212760, 0.86074305, 0.33163422, 0.85739792, 0.59908488, 0.74566046, 0.72157152 }; for (int i = 0; i < refInt.length; ++i) { int r = mt.nextInt(); Assert.assertEquals(refInt[i], (r & 0x7fffffffl) | ((r < 0) ? 0x80000000l : 0x0l)); } for (int i = 0; i < refDouble.length; ++i) { int r = mt.nextInt(); Assert.assertEquals(refDouble[i], ((r & 0x7fffffffl) | ((r < 0) ? 0x80000000l : 0x0l)) / 4294967296.0, 1.0e-8); } } } ././@LongLink100644 0 0 157 12126630646 10264 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/random/UncorrelatedRandomVectorGeneratorTest.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/random/UncorrelatedRandomVectorGenerato100644 1750 1750 5213 12126627670 32424 0ustarlucluc 0 0 //Licensed to the Apache Software Foundation (ASF) under one //or more contributor license agreements. See the NOTICE file //distributed with this work for additional information //regarding copyright ownership. The ASF licenses this file //to you 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. package org.apache.commons.math3.random; import org.apache.commons.math3.linear.RealMatrix; import org.apache.commons.math3.stat.descriptive.moment.VectorialCovariance; import org.apache.commons.math3.stat.descriptive.moment.VectorialMean; import org.junit.Test; import org.junit.Assert; public class UncorrelatedRandomVectorGeneratorTest { private double[] mean; private double[] standardDeviation; private UncorrelatedRandomVectorGenerator generator; public UncorrelatedRandomVectorGeneratorTest() { mean = new double[] {0.0, 1.0, -3.0, 2.3}; standardDeviation = new double[] {1.0, 2.0, 10.0, 0.1}; RandomGenerator rg = new JDKRandomGenerator(); rg.setSeed(17399225432l); generator = new UncorrelatedRandomVectorGenerator(mean, standardDeviation, new GaussianRandomGenerator(rg)); } @Test public void testMeanAndCorrelation() { VectorialMean meanStat = new VectorialMean(mean.length); VectorialCovariance covStat = new VectorialCovariance(mean.length, true); for (int i = 0; i < 10000; ++i) { double[] v = generator.nextVector(); meanStat.increment(v); covStat.increment(v); } double[] estimatedMean = meanStat.getResult(); double scale; RealMatrix estimatedCorrelation = covStat.getResult(); for (int i = 0; i < estimatedMean.length; ++i) { Assert.assertEquals(mean[i], estimatedMean[i], 0.07); for (int j = 0; j < i; ++j) { scale = standardDeviation[i] * standardDeviation[j]; Assert.assertEquals(0, estimatedCorrelation.getEntry(i, j) / scale, 0.03); } scale = standardDeviation[i] * standardDeviation[i]; Assert.assertEquals(1, estimatedCorrelation.getEntry(i, i) / scale, 0.025); } } } commons-math3-3.2-src/src/test/java/org/apache/commons/math3/random/Well512aTest.java100644 1750 1750 12527 12126627670 27066 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.random; import org.junit.Assert; import org.junit.Test; public class Well512aTest extends RandomGeneratorAbstractTest { @Override public RandomGenerator makeGenerator() { return new Well512a(101); } @Test public void testReferenceCode() { Well512a mt = new Well512a(new int[] { 740849862, 1202665156, -199039369, -259008301, -291878969, -1164428990, -1565918811, 491009864, -1883086670, 1383450241, 1244617256, 689006653, -1576746370, -1307940314, 1421489086, 1742094000 }); int[] refInt = { 1634813289, 1876773016, -973836208, -2130023652, -1045460084, -1834384857, 1691032973, 609714289, 2033920362, 555915483, 6680992, 1958127415, 1866469645, -1471336965, 2049178762, -192324811, -2056050066, 810879705, 1405046309, -781317118, 1012782311, -1045081032, 728377508, 1473511660, 290489070, 326666761, 2018299979, -1876688058, 1239968501, 1464625040, 2025151042, -101397407, 1387902041, 210959839, 1366359326, -476473433, 153180037, -1607631523, -506743495, 17888738, 313865008, -340504498, 586684079, 1243699375, 753162229, -646761694, -739189655, -210120185, -1856358726, -628255542, -1812798197, 1416288088, 1077967722, -846846208, 1379850409, -580183344, -1858959, 210859778, 295841424, 1492774865, -1415543680, -344870570, -1942779197, 1549510646, -389544849, 314254218, 11784988, -1311757368, 1719514841, -764610517, 1296788970, -994707050, 783854563, 422654144, 387639079, 1219688425, 2144352572, -834212874, -1036550358, 935909479, -568610842, 1327498837, -588933178, 1910065754, -40851599, -182063170, 1302731458, 541311559, -1647345522, 805224371, -1721196679, 1518507830, -952689880, -433276260, 509675254, -777259954, 1277810106, 284054896, 936042202, 2036836351, 1956412426, -1186403024, 287795400, 2135311211, 720485927, 1500695024, -281656583, -1277937322, -1628968482, 1242814831, -2030700974, 1473867890, 440813549, -1357033971, 28384076, 1602731216, -641465746, -609054347, 635938444, 1472898176, 1476894555, -747974186, -1590337055, -884242108, -389736197, -2066984505, 1087103272, -1236446290, 31657463, 1835715432, -468439078, -2132633204, -434609235, 258308151, 1851926761, -1630139159, -1344617241, 1969204215, 619463174, -174392624, 207475487, -1619828078, 1327980298, -83968178, 445951782, -1786230541, 6279288, -580982231, 1550645552, 2006533941, 275746007, 455676647, 2019637349, 1115547704, -1313120106, -516213449, 73752461, -1382448112, 398589620, 1319888048, -1595572334, 1566934536, -1735685764, -1509545339, 1458173912, -549395819, -618827040, 1516624531, 1900757187, -1454200688, 965524719, 488355065, -1869294316, -810641680, -2059428251, 1454656431, 1329120541, -232185900, -994996943, 1855980910, -452077812, 1565630611, 759842266, 1241435187, -1390456063, 1946400597, -2032319771, 683667881, 905911106, 1983310786, 120010546, 526018017, -1946881912, 205004987, -1307250612, 2130980818, 2052864161, 189839787, 1789478047, 406168885, -1145186347, 8507675, 1277188815, 1492619042, 2009819675, -1627411598, -851016743, -1828234956, 1962622506, 2140398255, 236935165, -337237772, 1263419111, 516775236, -335741025, 1391328225, 455979249, -1457534664, -657606241, 485648133, 1762116343, 1194889600, 817834937, 321150162, 131159182, 290277758, -1876924740, -1770401129, 1291602973, -1003642974, -1580211929, 1520422021, -399171579, -24315308, 453805396, -659197747, -205656847, 466526550, 1444397201, 1178091401, -1157268826, -602394028, -1370668795, 1614896435, 1699071659, 1864753793, 1888518358, -1721244514, 1812776767, 668822227, -297283057, 2130183333, -1169618692, 912860240, -2028253096, 1244694278 }; for (int i = 0; i < refInt.length; ++i) { Assert.assertEquals(refInt[i], mt.nextInt()); } } } commons-math3-3.2-src/src/test/java/org/apache/commons/math3/random/Well44497bTest.java100644 1750 1750 40766 12126627670 27261 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.random; import org.junit.Assert; import org.junit.Test; public class Well44497bTest extends RandomGeneratorAbstractTest { @Override public RandomGenerator makeGenerator() { return new Well44497b(100); } @Test public void testReferenceCode() { int[] base = { 740849862, 1202665156, -199039369, -259008301, -291878969, -1164428990, -1565918811, 491009864, -1883086670, 1383450241, 1244617256, 689006653, -1576746370, -1307940314, 1421489086, 1742094000, -595495729, 1047766204, 1875773301, -1637793284, 1379017098, 262792705, 191880010, -251000180, -1753047622, -972355720, 90626881, 1644693418, 1503365577, 439653419, 1806361562, 1268823869 }; int[] init = new int[1391]; for (int i = 0; i < init.length; ++i) { init[i] = base[i % base.length] + i; } Well44497b mt = new Well44497b(init); int[] refInt = { -102003638, -1254584449, 836441550, 1949705484, 653000494, 1579400718, 699652570, -140738233, 1164288466, 419933874, 366568847, 780567309, 1867405910, -350557801, -964350642, -1323492759, 191502468, 398676344, 1568976991, 1005053759, 199053603, 31083944, 74697788, -1343941248, -1205631880, -1637961625, 361813531, -1706096179, -403340909, 1666226814, -2034962600, 1237102662, -1833248535, 1584255126, 1295661745, -1753848945, 1208145993, 930278953, -733716134, 192752767, 1692463060, 1727273316, 2122952931, -809025255, -992081044, -895539688, -419372543, -1835478922, 2089629419, 1646590742, -1261083717, -1005462992, 1619627287, -1437723182, 1619689210, 1319393089, -1816963183, -150214444, -513482220, 1897815796, -1861960936, -1766444468, 2034653890, 585657634, 1867016559, 696942260, -1536237241, -527055447, -1554805020, -1063992566, 1024799415, 1782080572, -1884276362, 129028272, 1427925968, -1154222271, -1383146732, -1580532830, -1907049616, -104299169, -1780913000, -2090815339, -1789809502, -1521443849, 1226625769, 1126090676, -2117094290, -449575109, -218982833, -695554478, 35923022, 1386126825, -95031305, -168657023, 436674049, -1137917876, -2045134053, -1025629865, 133144659, 64226081, -1966942130, 700813483, 344058910, -910646033, -212789479, 740360859, -1269028713, 1517763679, -664178514, -683718472, -71951996, 86583727, -1235669348, -1426987265, -166598353, 214190040, -1436967644, 233824411, 710927452, -1939548641, -433607408, -1075939594, -1549702826, -1310827917, -640604762, -696863672, -1282162126, -546470833, -1516734192, -513809904, -458526835, 708926727, -476643096, -2108375037, -2870478, -1460116421, 436587555, -948939610, 1891375124, 1944216545, 959034236, -1267038790, -1695098736, 1853748495, 1594424552, 1270099319, 1139585482, 1837434635, -709909535, -457524230, -887118113, -241703912, -1888225819, -751575804, 1122280146, 1194255209, 949350188, 892826516, -791212042, -151203035, -859297731, -1979039938, 323603119, -1022065097, -1804294506, -385802891, -2127523442, -720380255, -1315859127, 999649487, 335041941, -1732821688, -1833409827, 535715225, -1285355653, 1206723386, -1141619270, 759796285, -1599504546, -1988521411, 1056668895, -852564594, 1056509609, -1831687977, 754168875, -1301144422, 922880446, -1502666503, -949791898, -1043870198, -1136941938, -1649670259, 1342769348, 1692605059, -132279148, -1108038310, -14355545, -1611387086, 1651826569, 877600127, 1356160799, -759125205, -1300490081, -414938486, -201479285, 1958709363, 1513313540, -1396836908, 1352702612, 1142506253, 52969438, -365142357, -1619054179, -1368514102, 1470750417, -1420830959, -843909462, -1679570143, 1447444607, 234551752, -1507452115, -1433234579, -680890000, -497305145, 860408898, 263376761, 1537070218, -592353956, 1587852989, 1756653749, -2081320633, -1547020311, 723771611, -883819383, 1899879513, -268417584, 1058606451, 1665941493, -1630340612, -614737477, 891313237, 1368950660, -1166187089, 296322368, -1908770726, -2120378408, 1245479677, 1879710487, -1705947124, 1018371589, -1715010575, -1096078094, -1749891454, 2130888667, 318647750, 554592231, -489121568, -1809605233, -1697905160, -953926536, -2013960553, -148884919, 1822739964, -1466301760, 141999978, 1946526064, 1323036718, 864390149, -2141665227, 1510602225, 1468408474, 1277039721, -1368096647, 180965986, 2140852057, -688071899, 819713016, -154385940, -1182972611, 1257224305, 1392607672, 1364245931, -1768434401, 323522132, -555278604, 474186520, -1178901665, -2137343658, 1636421121, 1398987084, 1276656225, 1013316006, -955917865, -1537149363, -179145358, 342862050, 1172728007, 736300054, -1114656959, -1831840325, -1882353535, -442915191, -1206488416, -1818534411, 25312311, 2100297098, -1562767719, 1762553519, -1853194231, -1152612739, -2020055792, -809572150, 848584579, -535789699, 1817604709, 1343773216, -602234204, 1739930964, -833790834, 501215449, -730104057, 1217783189, -681773267, -611951354, 978387629, -1516811237, 974303980, -1389665696, 2091099075, -727528826, 2116026151, 271935854, 613242379, -2100429856, 190004963, -1629612570, -1362888327, 175094223, -917873219, -2008308245, -401946815, 504218792, -1966525201, 4106248, 164895454, 226502921, 655865257, -610528718, 189428750, 1055978898, 17603028, 591024369, 1127922501, -1546639293, 1994174637, -724136988, -673919372, -1665002120, -612145705, -793102882, -1904763558, 757565058, -2091240021, -2123324826, -1518702766, -802889839, -223045921, -1509216579, 1195556261, 2079259971, -903969953, -1781800655, 1834950463, -956531922, -1152550807, -1116052662, -348802884, -1395330117, -91758501, -19115285, 1926930669, -1015793599, 545904386, 1617858191, 716963473, 1917089719, -980914811, -212646927, -1634695647, -1857924568, -1462257477, 1273750178, 1060328454, -361604424, 867932749, 451213698, 405780152, 1165233215, 1877230909, 2103114395, 1644330815, 1252998537, 1961603970, -1533101488, 1790456024, -38226118, -1306744489, 713676418, -1535871838, 1378109935, -338185548, 1647669762, -477465913, 203482277, -1949756706, -503326093, -638704909, 320186309, -1435581459, 907446649, -77384645, 537368928, -335347295, -1912061924, 547819174, -225549827, 1089455486, 463516297, -240127764, -85895271, 2053179697, -287394931, 921878827, -933362608, -1178670275, -1200942874, -672571265, 574422382, 1441734039, -1814493454, 165751640, -176907245, -1170992192, -2123252090, -1435971541, 1591853830, -885477992, -792847559, 1359875286, 1038392904, -2027255124, 687291425, -165513490, 1391146576, -1387080955, 794663652, -807189965, 667820962, -545384499, -1371368854, -689031878, 1504805541, -752825823, -1920047745, -1884874017, -350566320, -197152911, -181743050, -798415949, -915922276, 1790690149, -363997181, 1923116185, -1326427198, -1621079427, -1997440997, 1798118127, -2053110382, -159879848, -1286787216, 1046436411, 1832030471, -389092059, 71686169, -76025260, 1914270607, 1854169353, 872157826, -1774323792, -575165717, -1919931724, 2051498109, -1176174934, -883578901, -1253047270, -1310573900, 245466682, -1784824328, -1319912821, 1377340217, 1364313761, -408687373, 142333378, -1952335763, -1703126184, 316314678, 2030555423, 488640834, -1783293306, 2116925005, -428337460, -42966447, -476226114, -325172903, -1690748475, 852791569, 26490302, 85251337, -1374975770, -376283969, 982639600, 595119792, 376403422, 1574509912, -1509664496, -1901241749, -59019104, 358566667, 341667214, 184541206, -550950854, -1897143732, 1595753537, -1236127928, 2014297822, -2033179270, -669806121, -1927596980, 1010735700, -581795164, 1922398838, -1456743538, -1633813803, 323177707, 2002098813, -2099067658, 277393729, -671911622, -384463053, 2028267908, 367391785, 1270052637, -172839030, -650486693, -831800809, -1255138113, -137512799, 1904317942, -8229811, 707361898, -276859812, 50417442, 1487081728, 1577776181, 1994451303, 1237303035, -602016235, -1905218276, -1895725672, 1172978849, 801129953, -1819485071, -587985848, -2010386741, -1645226427, -850866837, 816998708, 357665811, 1955856762, 1617902189, -1013761306, 146782652, 904185608, -500146809, 2085848310, 1917713975, -1823786899, 1994184748, 789196322, 1766857258, 1770685286, 58213029, -1699628994, 346827379, -1274423227, -5079670, -193099487, 1020296939, -1795904054, -1951053094, -43782418, -375403393, 1026761026, -207269610, 1364563521, 1578793454, 457809423, -534138380, -1052938788, -1897101526, 1449976516, 2052800058, -1145169719, 1476303269, 370625650, -325249282, 2165984, 1631432802, 1032336355, -1292978191, -1810172401, 725725820, -1162678778, 702624490, 1387673527, 981825943, -556408212, -1108487850, -1782136935, 1582316425, -1752682164, 307235003, 1386486299, -1343636074, 1936875586, -1265279891, -345847069, 928241171, 239050350, 1859358526, -664776217, -823267892, 346651710, -867656288, -1907921425, 1362445801, 541145461, -192727350, 1649366606, 244694627, -488180018, 214008256, 2032125437, -1678591993, -264593820, 1309017286, -652451998, 1845366657, -703990120, -550186406, -630285276, 1782372955, 1650564210, -1127904234, -1407983860, -1119422877, -1565976361, -1913545385, 549841420, -1410323732, -1964467146, 228296551, -421026422, 1929094398, -266906424, 264810315, -2008122665, -1088278148, 141242192, 1871293282, 234634067, 1724159838, 1638271051, -837713428, -657941661, 168093988, 708605363, -1881612509, -1810496006, -193495322, 1889982309, -2050702012, -693694192, -1249780322, 718733403, -76349730, -188217051, 920912090, -1814078273, 2013358456, -1948845521, -198407575, -1248904632, 1182772565, 1236791612, -1297489171, -1958468586, 1437011007, 390460941, 113068796, 1247982993, 2102889679, -1563524844, -128174212, -754095070, -1461699362, 943615640, -1013270737, 221253920, 1514140013, 1596946745, 674222984, 616356702, 1811224435, -1764322023, -1653707581, -1702404459, 390678142, -209506765, -1398278531, -117061517, 1625861343, 659048069, -1490678943, 846536668, 210715637, 1855458786, 1727745280, 1086729456, 1109111683, -985298098, -1813777567, -954599702, -1522494031, 1166103515, -191868965, -1048777852, -661271058, 1161457421, 1509090409, -919753558, -155431193, -1774302994, -366390263, 2090138916, -693431491, -1693888428, 1846774454, 925855693, 474383470, 208889079, 382195164, -283005634, -2095134392, 579927985, 1390765326, -1766119865, 900457129, -1503703236, 974952690, -107714111, 381338452, 1187256613, -860560742, 524103620, 1499506130, 197755276, -790802926, -406920967, -1972219791, -665721155, -113336203, 1037154436, -1185441801, -745541706, -546274471, 1988928457, -1975403782, -1167172845, 777779004, -1560935061, -140258712, -1243598232, -1394149587, -785002782, 311842991, -1025469277, -605350463, -1251538057, 537203966, 597777961, -1845767072, -1556349193, -1491015509, -1935936671, 2093498487, 1908270236, -315396187, 1356362300, -2025658518, 630119678, 276190559, 510123398, -1266145363, -170152124, -151540077, -477900187, 1895894303, 1870333068, -1169891437, 353366620, 2111175941, 1691245786, 1318765802, -90993610, 921309517, 118241505, 367005284, 1624861072, 2010785894, 865255951, 1717799691, -80757664, -644944841, 136999836, -341686875, -1908076090, -1968934200, -346397811, -184213520, -511811333, -2118173466, -1086490399, 1795322855, -635494328, 415716276, 851044432, -904636831, -1972230341, -64337858, 571177016, 1248814747, -1351030778, 457872680, 1843549954, 1718960038, 815088665, 1812961065, 360686952, -1356586646, 1657802416, 1776192945, -786723490, -342254407, -236653811, 771014701, 906386785, -308057635, 1907957462, 206000440, -42143480, 900403654, -917549795, -310520796, -1713627766, 2061136240, -377977839, 891282946, -821163030, 328143584, 1503793080, 551621842, -2086273683, -2070526343, 91195293, -1654389038, -1035734266, -336619597, -1220221027, -1468468844, 2105626873, -841372573, -122707018, -2013073683, 494461000, -2054807734, -67946259, 1914163407, 1941835405, -1027244745, -768123277, 419129844, -275750260, -171533009, 97756174, -17651409, -1578102255, 995291430, -1587462977, 692904675, 951632643, 1882101293, -1546298756, 2018418068, -1790777661, 1542305514, -1437624383, 469587009, -1647853474, -1318279028, 497228822, 726733469, 1693133452, -2091185798, -209017732, 126386499, 1056958932, -2105494133, 754067324, 96463951, 83701151, 1101658289, 1485852701, 553783806, 1898769881, -1072031442, 1438062141, 1992540265, 1152252136, 1019391719, -175951257, -6691216, 989789689, 968359367, -1330392786, 1704963399, -998432914, -948060232, -1921688855, -975840920, 1360273515, -872810459, 12676907, -1908050756, 883609616, 65641549, -200365398, 1386653304, -1203665071, 1878689007, 426262328, 315375145, 1900325181, 703658494, -765404895, 1070155172, 1399748900, -804264234, -1619419026, 1347225486, 230635292, 1093717835, 14020583, -2107039873, -968325341, -1679158691, 1959784097, 1065690797, 1090615161, 1311445364, 865835426, 870016646, 574122879, 1842697922, -1289210431, -1914001560, 1672467629, -900366331, -1524066872, 136503816, -1910431892, -1431958329, -830367152, -1316233970, -801974860, 1560669382, -81784810, 401822577, -949103202, 943897151, -722666726, -96825841, -1092898846, 230567004, -70355840, -1398069192, -312953142, 1475420133, -622491023, 1661205388, -19071322, 6024591, 1473041593, 2053897978, -1346768903, 1484764721, -1552461890, 1287146711, 1613069307, 902497864, -1504480063, 375292915, -836353108, 2047602411 }; for (int i = 0; i < refInt.length; ++i) { Assert.assertEquals(refInt[i], mt.nextInt()); } } } commons-math3-3.2-src/src/test/java/org/apache/commons/math3/RetryRunner.java100644 1750 1750 6063 12126627675 25734 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3; import org.junit.runners.BlockJUnit4ClassRunner; import org.junit.runners.model.FrameworkMethod; import org.junit.runners.model.InitializationError; import org.junit.runners.model.Statement; /** * A test runner that retries tests when assertions fail. * @version $Id: RetryRunner.java 1244107 2012-02-14 16:17:55Z erans $ */ public class RetryRunner extends BlockJUnit4ClassRunner { /** * Simple constructor. * * @param testClass Class to test. * @throws InitializationError if default runner cannot be built. */ public RetryRunner(final Class testClass) throws InitializationError { super(testClass); } @Override public Statement methodInvoker(final FrameworkMethod method, Object test) { final Statement singleTryStatement = super.methodInvoker(method, test); return new Statement() { /** * Evaluate the statement. * We attempt several runs for the test, at most MAX_ATTEMPTS. * if one attempt succeeds, we succeed, if all attempts fail, we * fail with the reason corresponding to the last attempt */ @Override public void evaluate() throws Throwable { Throwable failureReason = null; final Retry retry = method.getAnnotation(Retry.class); if (retry == null) { // Do a single test run attempt. singleTryStatement.evaluate(); } else { final int numRetries = retry.value(); for (int i = 0; i < numRetries; ++i) { try { // Do a single test run attempt. singleTryStatement.evaluate(); // Attempt succeeded, stop evaluation here. return; } catch (Throwable t) { // Attempt failed, store the reason. failureReason = t; } } // All attempts failed. throw failureReason; } } }; } } commons-math3-3.2-src/src/test/java/org/apache/commons/math3/special/GammaTest.java100644 1750 1750 111311 12126627667 26771 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.special; import org.apache.commons.math3.TestUtils; import org.apache.commons.math3.exception.NumberIsTooLargeException; import org.apache.commons.math3.exception.NumberIsTooSmallException; import org.apache.commons.math3.util.FastMath; import org.junit.Assert; import org.junit.Test; /** * @version $Id: GammaTest.java 1414531 2012-11-28 05:39:39Z celestin $ */ public class GammaTest { private void testRegularizedGamma(double expected, double a, double x) { double actualP = Gamma.regularizedGammaP(a, x); double actualQ = Gamma.regularizedGammaQ(a, x); TestUtils.assertEquals(expected, actualP, 10e-15); TestUtils.assertEquals(actualP, 1.0 - actualQ, 10e-15); } private void testLogGamma(double expected, double x) { double actual = Gamma.logGamma(x); TestUtils.assertEquals(expected, actual, 10e-15); } @Test public void testRegularizedGammaNanPositive() { testRegularizedGamma(Double.NaN, Double.NaN, 1.0); } @Test public void testRegularizedGammaPositiveNan() { testRegularizedGamma(Double.NaN, 1.0, Double.NaN); } @Test public void testRegularizedGammaNegativePositive() { testRegularizedGamma(Double.NaN, -1.5, 1.0); } @Test public void testRegularizedGammaPositiveNegative() { testRegularizedGamma(Double.NaN, 1.0, -1.0); } @Test public void testRegularizedGammaZeroPositive() { testRegularizedGamma(Double.NaN, 0.0, 1.0); } @Test public void testRegularizedGammaPositiveZero() { testRegularizedGamma(0.0, 1.0, 0.0); } @Test public void testRegularizedGammaPositivePositive() { testRegularizedGamma(0.632120558828558, 1.0, 1.0); } @Test public void testLogGammaNan() { testLogGamma(Double.NaN, Double.NaN); } @Test public void testLogGammaNegative() { testLogGamma(Double.NaN, -1.0); } @Test public void testLogGammaZero() { testLogGamma(Double.NaN, 0.0); } @Test public void testLogGammaPositive() { testLogGamma(0.6931471805599457, 3.0); } @Test public void testDigammaLargeArgs() { double eps = 1e-8; Assert.assertEquals(4.6001618527380874002, Gamma.digamma(100), eps); Assert.assertEquals(3.9019896734278921970, Gamma.digamma(50), eps); Assert.assertEquals(2.9705239922421490509, Gamma.digamma(20), eps); Assert.assertEquals(2.9958363947076465821, Gamma.digamma(20.5), eps); Assert.assertEquals(2.2622143570941481605, Gamma.digamma(10.1), eps); Assert.assertEquals(2.1168588189004379233, Gamma.digamma(8.8), eps); Assert.assertEquals(1.8727843350984671394, Gamma.digamma(7), eps); Assert.assertEquals(0.42278433509846713939, Gamma.digamma(2), eps); Assert.assertEquals(-100.56088545786867450, Gamma.digamma(0.01), eps); Assert.assertEquals(-4.0390398965921882955, Gamma.digamma(-0.8), eps); Assert.assertEquals(4.2003210041401844726, Gamma.digamma(-6.3), eps); } @Test public void testDigammaSmallArgs() { // values for negative powers of 10 from 1 to 30 as computed by webMathematica with 20 digits // see functions.wolfram.com double[] expected = {-10.423754940411076795, -100.56088545786867450, -1000.5755719318103005, -10000.577051183514335, -100000.57719921568107, -1.0000005772140199687e6, -1.0000000577215500408e7, -1.0000000057721564845e8, -1.0000000005772156633e9, -1.0000000000577215665e10, -1.0000000000057721566e11, -1.0000000000005772157e12, -1.0000000000000577216e13, -1.0000000000000057722e14, -1.0000000000000005772e15, -1e+16, -1e+17, -1e+18, -1e+19, -1e+20, -1e+21, -1e+22, -1e+23, -1e+24, -1e+25, -1e+26, -1e+27, -1e+28, -1e+29, -1e+30}; for (double n = 1; n < 30; n++) { checkRelativeError(String.format("Test %.0f: ", n), expected[(int) (n - 1)], Gamma.digamma(FastMath.pow(10.0, -n)), 1e-8); } } @Test public void testTrigamma() { double eps = 1e-8; // computed using webMathematica. For example, to compute trigamma($i) = Polygamma(1, $i), use // // http://functions.wolfram.com/webMathematica/Evaluated.jsp?name=PolyGamma2&plottype=0&vars={%221%22,%22$i%22}&digits=20 double[] data = { 1e-4, 1.0000000164469368793e8, 1e-3, 1.0000016425331958690e6, 1e-2, 10001.621213528313220, 1e-1, 101.43329915079275882, 1, 1.6449340668482264365, 2, 0.64493406684822643647, 3, 0.39493406684822643647, 4, 0.28382295573711532536, 5, 0.22132295573711532536, 10, 0.10516633568168574612, 20, 0.051270822935203119832, 50, 0.020201333226697125806, 100, 0.010050166663333571395 }; for (int i = data.length - 2; i >= 0; i -= 2) { Assert.assertEquals(String.format("trigamma %.0f", data[i]), data[i + 1], Gamma.trigamma(data[i]), eps); } } /** * Reference data for the {@link Gamma#logGamma(double)} function. This data * was generated with the following Maxima script. * *
                               * kill(all);
                               *
                               * fpprec : 64;
                               * gamln(x) := log(gamma(x));
                               * x : append(makelist(bfloat(i / 8), i, 1, 80),
                               *     [0.8b0, 1b2, 1b3, 1b4, 1b5, 1b6, 1b7, 1b8, 1b9, 1b10]);
                               *
                               * for i : 1 while i <= length(x) do
                               *     print("{", float(x[i]), ",", float(gamln(x[i])), "},");
                               * 
                          */ private static final double[][] LOG_GAMMA_REF = { { 0.125 , 2.019418357553796 }, { 0.25 , 1.288022524698077 }, { 0.375 , .8630739822706475 }, { 0.5 , .5723649429247001 }, { 0.625 , .3608294954889402 }, { 0.75 , .2032809514312954 }, { 0.875 , .08585870722533433 }, { 0.890625 , .07353860936979656 }, { 0.90625 , .06169536624059108 }, { 0.921875 , .05031670080005688 }, { 0.9375 , 0.0393909017345823 }, { 0.953125 , .02890678734595923 }, { 0.96875 , .01885367233441289 }, { 0.984375 , .009221337197578781 }, { 1.0 , 0.0 }, { 1.015625 , - 0.00881970970573307 }, { 1.03125 , - .01724677500176807 }, { 1.046875 , - .02528981394675729 }, { 1.0625 , - .03295710029357782 }, { 1.078125 , - .04025658272400143 }, { 1.09375 , - .04719590272716985 }, { 1.109375 , - .05378241123619192 }, { 1.125 , - .06002318412603958 }, { 1.25 , - .09827183642181316 }, { 1.375 , - .1177552707410788 }, { 1.5 , - .1207822376352452 }, { 1.625 , - .1091741337567954 }, { 1.75 , - .08440112102048555 }, { 1.875 , - 0.0476726853991883 }, { 1.890625 , - .04229320615532515 }, { 1.90625 , - .03674470657266143 }, { 1.921875 , - .03102893865389552 }, { 1.9375 , - .02514761940298887 }, { 1.953125 , - .01910243184040138 }, { 1.96875 , - .01289502598016741 }, { 1.984375 , - .006527019770560387 }, { 2.0 , 0.0 }, { 2.015625 , .006684476830232185 }, { 2.03125 , .01352488366498562 }, { 2.046875 , .02051972208453692 }, { 2.0625 , .02766752152285702 }, { 2.078125 , 0.0349668385135861 }, { 2.09375 , .04241625596251728 }, { 2.109375 , .05001438244545164 }, { 2.125 , .05775985153034387 }, { 2.25 , .1248717148923966 }, { 2.375 , .2006984603774558 }, { 2.5 , .2846828704729192 }, { 2.625 , .3763336820249054 }, { 2.75 , .4752146669149371 }, { 2.875 , .5809359740231859 }, { 2.890625 , .5946142560817441 }, { 2.90625 , .6083932548009232 }, { 2.921875 , .6222723333588501 }, { 2.9375 , .6362508628423761 }, { 2.953125 , .6503282221022278 }, { 2.96875 , .6645037976116387 }, { 2.984375 , 0.678776983328359 }, { 3.0 , .6931471805599453 }, { 3.015625 , .7076137978322324 }, { 3.03125 , .7221762507608962 }, { 3.046875 , .7368339619260166 }, { 3.0625 , 0.751586360749556 }, { 3.078125 , .7664328833756681 }, { 3.09375 , .7813729725537568 }, { 3.109375 , .7964060775242092 }, { 3.125 , 0.811531653906724 }, { 3.25 , .9358019311087253 }, { 3.375 , 1.06569589786406 }, { 3.5 , 1.200973602347074 }, { 3.625 , 1.341414578068493 }, { 3.75 , 1.486815578593417 }, { 3.875 , 1.6369886482725 }, { 4.0 , 1.791759469228055 }, { 4.125 , 1.950965937095089 }, { 4.25 , 2.114456927450371 }, { 4.375 , 2.282091222188554 }, { 4.5 , 2.453736570842442 }, { 4.625 , 2.62926886637513 }, { 4.75 , 2.808571418575736 }, { 4.875 , 2.99153431107781 }, { 5.0 , 3.178053830347946 }, { 5.125 , 3.368031956881733 }, { 5.25 , 3.561375910386697 }, { 5.375 , 3.757997741998131 }, { 5.5 , 3.957813967618717 }, { 5.625 , 4.160745237339519 }, { 5.75 , 4.366716036622286 }, { 5.875 , 4.57565441552762 }, { 6.0 , 4.787491742782046 }, { 6.125 , 5.002162481906205 }, { 6.25 , 5.219603986990229 }, { 6.375 , 5.439756316011858 }, { 6.5 , 5.662562059857142 }, { 6.625 , 5.887966185430003 }, { 6.75 , 6.115915891431546 }, { 6.875 , 6.346360475557843 }, { 7.0 , 6.579251212010101 }, { 7.125 , 6.814541238336996 }, { 7.25 , 7.05218545073854 }, { 7.375 , 7.292140407056348 }, { 7.5 , 7.534364236758733 }, { 7.625 , 7.778816557302289 }, { 7.75 , 8.025458396315983 }, { 7.875 , 8.274252119110479 }, { 8.0 , 8.525161361065415 }, { 8.125 , 8.77815096449171 }, { 8.25 , 9.033186919605123 }, { 8.375 , 9.290236309282232 }, { 8.5 , 9.549267257300997 }, { 8.625 , 9.810248879795765 }, { 8.75 , 10.07315123968124 }, { 8.875 , 10.33794530382217 }, { 9.0 , 10.60460290274525 }, { 9.125 , 10.87309669270751 }, { 9.25 , 11.14340011995171 }, { 9.375 , 11.41548738699336 }, { 9.5 , 11.68933342079727 }, { 9.625 , 11.96491384271319 }, { 9.75 , 12.24220494005076 }, { 9.875 , 12.52118363918365 }, { 10.0 , 12.80182748008147 }, { 0.8 , .1520596783998376 }, { 100.0 , 359.1342053695754 }, { 1000.0 , 5905.220423209181 }, { 10000.0 , 82099.71749644238 }, { 100000.0 , 1051287.708973657 }, { 1000000.0 , 1.2815504569147612e+7 }, { 10000000.0 , 1.511809493694739e+8 }, { 1.e+8 , 1.7420680661038346e+9 }, { 1.e+9 , 1.972326582750371e+10 }, { 1.e+10 , 2.202585092888106e+11 }, }; @Test public void testLogGamma() { final int ulps = 3; for (int i = 0; i < LOG_GAMMA_REF.length; i++) { final double[] data = LOG_GAMMA_REF[i]; final double x = data[0]; final double expected = data[1]; final double actual = Gamma.logGamma(x); final double tol; if (expected == 0.0) { tol = 1E-15; } else { tol = ulps * FastMath.ulp(expected); } Assert.assertEquals(Double.toString(x), expected, actual, tol); } } @Test public void testLogGammaPrecondition1() { Assert.assertTrue(Double.isNaN(Gamma.logGamma(0.0))); } @Test public void testLogGammaPrecondition2() { Assert.assertTrue(Double.isNaN(Gamma.logGamma(-1.0))); } /** *

                          * Reference values for the {@link Gamma#invGamma1pm1(double)} method. * These values were generated with the following Maxima script *

                          * *
                               * kill(all);
                               *
                               * fpprec : 64;
                               * gam1(x) := 1 / gamma(1 + x) - 1;
                               * x : makelist(bfloat(i / 8), i, -4, 12);
                               *
                               * for i : 1 while i <= length(x) do print("{",
                               *                                         float(x[i]),
                               *                                         ",",
                               *                                         float(gam1(x[i])),
                               *                                         "},");
                               * 
                          */ private static final double[][] INV_GAMMA1P_M1_REF = { { -0.5 , -.4358104164522437 }, { -0.375 , -.3029021533379859 }, { -0.25 , -0.183951060901737 }, { -0.125 , -.08227611018520711 }, { 0.0 , 0.0 }, { 0.125 , .06186116458306091 }, { 0.25 , .1032626513208373 }, { 0.375 , .1249687649039041 }, { 0.5 , .1283791670955126 }, { 0.625 , .1153565546592225 }, { 0.75 , 0.0880652521310173 }, { 0.875 , .04882730264547758 }, { 1.0 , 0.0 }, { 1.125 , -.05612340925950141 }, { 1.25 , -.1173898789433302 }, { 1.375 , -.1818408982517061 }, { 1.5 , -0.247747221936325 }, }; @Test public void testInvGamma1pm1() { final int ulps = 3; for (int i = 0; i < INV_GAMMA1P_M1_REF.length; i++) { final double[] ref = INV_GAMMA1P_M1_REF[i]; final double x = ref[0]; final double expected = ref[1]; final double actual = Gamma.invGamma1pm1(x); final double tol = ulps * FastMath.ulp(expected); Assert.assertEquals(Double.toString(x), expected, actual, tol); } } @Test(expected = NumberIsTooSmallException.class) public void testInvGamma1pm1Precondition1() { Gamma.invGamma1pm1(-0.51); } @Test(expected = NumberIsTooLargeException.class) public void testInvGamma1pm1Precondition2() { Gamma.invGamma1pm1(1.51); } private static final double[][] LOG_GAMMA1P_REF = { { - 0.5 , .5723649429247001 }, { - 0.375 , .3608294954889402 }, { - 0.25 , .2032809514312954 }, { - 0.125 , .08585870722533433 }, { 0.0 , 0.0 }, { 0.125 , - .06002318412603958 }, { 0.25 , - .09827183642181316 }, { 0.375 , - .1177552707410788 }, { 0.5 , - .1207822376352452 }, { 0.625 , - .1091741337567954 }, { 0.75 , - .08440112102048555 }, { 0.875 , - 0.0476726853991883 }, { 1.0 , 0.0 }, { 1.125 , .05775985153034387 }, { 1.25 , .1248717148923966 }, { 1.375 , .2006984603774558 }, { 1.5 , .2846828704729192 }, }; @Test public void testLogGamma1p() { final int ulps = 3; for (int i = 0; i < LOG_GAMMA1P_REF.length; i++) { final double[] ref = LOG_GAMMA1P_REF[i]; final double x = ref[0]; final double expected = ref[1]; final double actual = Gamma.logGamma1p(x); final double tol = ulps * FastMath.ulp(expected); Assert.assertEquals(Double.toString(x), expected, actual, tol); } } @Test(expected = NumberIsTooSmallException.class) public void testLogGamma1pPrecondition1() { Gamma.logGamma1p(-0.51); } @Test(expected = NumberIsTooLargeException.class) public void testLogGamma1pPrecondition2() { Gamma.logGamma1p(1.51); } /** * Reference data for the {@link Gamma#gamma(double)} function. This * data was generated with the following Maxima script. * *
                               * kill(all);
                               *
                               * fpprec : 64;
                               *
                               * EPSILON : 10**(-fpprec + 1);
                               * isInteger(x) := abs(x - floor(x)) <= EPSILON * abs(x);
                               *
                               * x : makelist(bfloat(i / 8), i, -160, 160);
                               * x : append(x, makelist(bfloat(i / 2), i, 41, 200));
                               *
                               * for i : 1 while i <= length(x) do if not(isInteger(x[i])) then
                               *     print("{", float(x[i]), ",", float(gamma(x[i])), "},");
                               * 
                          */ private static final double[][] GAMMA_REF = { { - 19.875 , 4.920331854832504e-18 }, { - 19.75 , 3.879938752480031e-18 }, { - 19.625 , 4.323498423815027e-18 }, { - 19.5 , 5.811045977502237e-18 }, { - 19.375 , 9.14330910942125e-18 }, { - 19.25 , 1.735229114436739e-17 }, { - 19.125 , 4.653521565668223e-17 }, { - 18.875 , - 9.779159561479603e-17 }, { - 18.75 , - 7.662879036148061e-17 }, { - 18.625 , - 8.484865656736991e-17 }, { - 18.5 , - 1.133153965612936e-16 }, { - 18.375 , - 1.771516139950367e-16 }, { - 18.25 , - 3.340316045290721e-16 }, { - 18.125 , - 8.899859994340475e-16 }, { - 17.875 , 1.845816367229275e-15 }, { - 17.75 , 1.436789819277761e-15 }, { - 17.625 , 1.580306228567265e-15 }, { - 17.5 , 2.096334836383932e-15 }, { - 17.375 , 3.255160907158799e-15 }, { - 17.25 , 6.096076782655566e-15 }, { - 17.125 , 1.613099623974211e-14 }, { - 16.875 , - 3.29939675642233e-14 }, { - 16.75 , - 2.550301929218027e-14 }, { - 16.625 , - 2.785289727849803e-14 }, { - 16.5 , - 3.66858596367188e-14 }, { - 16.375 , - 5.655842076188414e-14 }, { - 16.25 , - 1.051573245008085e-13 }, { - 16.125 , - 2.762433106055837e-13 }, { - 15.875 , 5.567732026462681e-13 }, { - 15.75 , 4.271755731440195e-13 }, { - 15.625 , 4.630544172550298e-13 }, { - 15.5 , 6.053166840058604e-13 }, { - 15.375 , 9.261441399758529e-13 }, { - 15.25 , 1.708806523138138e-12 }, { - 15.125 , 4.454423383515037e-12 }, { - 14.875 , - 8.838774592009505e-12 }, { - 14.75 , - 6.728015277018307e-12 }, { - 14.625 , - 7.235225269609841e-12 }, { - 14.5 , - 9.382408602090835e-12 }, { - 14.375 , - 1.423946615212874e-11 }, { - 14.25 , - 2.605929947785661e-11 }, { - 14.125 , - 6.737315367566492e-11 }, { - 13.875 , 1.314767720561414e-10 }, { - 13.75 , 9.923822533602004e-11 }, { - 13.625 , 1.058151695680439e-10 }, { - 13.5 , 1.360449247303171e-10 }, { - 13.375 , 2.046923259368506e-10 }, { - 13.25 , 3.713450175594567e-10 }, { - 13.125 , 9.516457956687671e-10 }, { - 12.875 , - 1.8242402122789617e-9 }, { - 12.75 , - 1.3645255983702756e-9 }, { - 12.625 , - 1.4417316853645984e-9 }, { - 12.5 , - 1.836606483859281e-9 }, { - 12.375 , - 2.7377598594053765e-9 }, { - 12.25 , - 4.9203214826628017e-9 }, { - 12.125 , - 1.2490351068152569e-8 }, { - 11.875 , 2.3487092733091633e-8 }, { - 11.75 , 1.7397701379221012e-8 }, { - 11.625 , 1.8201862527728055e-8 }, { - 11.5 , 2.295758104824101e-8 }, { - 11.375 , 3.3879778260141535e-8 }, { - 11.25 , 6.027393816261931e-8 }, { - 11.125 , 1.5144550670134987e-7 }, { - 10.875 , - 2.7890922620546316e-7 }, { - 10.75 , - 2.044229912058469e-7 }, { - 10.625 , - 2.1159665188483867e-7 }, { - 10.5 , - 2.640121820547716e-7 }, { - 10.375 , - 3.8538247770911e-7 }, { - 10.25 , - 6.780818043294673e-7 }, { - 10.125 , - 1.6848312620525174e-6 }, { - 9.875 , 3.0331378349844124e-6 }, { - 9.75 , 2.1975471554628537e-6 }, { - 9.625 , 2.2482144262764103e-6 }, { - 9.5 , 2.772127911575102e-6 }, { - 9.375 , 3.998343206232017e-6 }, { - 9.25 , 6.95033849437704e-6 }, { - 9.125 , 1.7058916528281737e-5 }, { - 8.875 , - 2.9952236120471065e-5 }, { - 8.75 , - 2.1426084765762826e-5 }, { - 8.625 , - 2.163906385291045e-5 }, { - 8.5 , - 2.633521515996347e-5 }, { - 8.375 , - 3.748446755842515e-5 }, { - 8.25 , - 6.429063107298763e-5 }, { - 8.125 , - 1.5566261332057085e-4 }, { - 7.875 , 2.658260955691807e-4 }, { - 7.75 , 1.874782417004247e-4 }, { - 7.625 , 1.8663692573135265e-4 }, { - 7.5 , 2.238493288596895e-4 }, { - 7.375 , 3.1393241580181064e-4 }, { - 7.25 , 5.303977063521479e-4 }, { - 7.125 , .001264758733229638 }, { - 6.875 , - .002093380502607298 }, { - 6.75 , - .001452956373178292 }, { - 6.625 , - .001423106558701564 }, { - 6.5 , - .001678869966447671 }, { - 6.375 , - .002315251566538353 }, { - 6.25 , - .003845383371053072 }, { - 6.125 , - .009011405974261174 }, { - 5.875 , .01439199095542518 }, { - 5.75 , .009807455518953468 }, { - 5.625 , .009428080951397862 }, { - 5.5 , .01091265478190986 }, { - 5.375 , 0.014759728736682 }, { - 5.25 , 0.0240336460690817 }, { - 5.125 , .05519486159234969 }, { - 4.875 , - 0.0845529468631229 }, { - 4.75 , - .05639286923398244 }, { - 4.625 , - .05303295535161297 }, { - 4.5 , - .06001960130050425 }, { - 4.375 , - .07933354195966577 }, { - 4.25 , - .1261766418626789 }, { - 4.125 , - .2828736656607921 }, { - 3.875 , .4121956159577241 }, { - 3.75 , .2678661288614166 }, { - 3.625 , 0.24527741850121 }, { - 3.5 , .2700882058522691 }, { - 3.375 , .3470842460735378 }, { - 3.25 , .5362507279163854 }, { - 3.125 , 1.166853870850768 }, { - 2.875 , - 1.597258011836181 }, { - 2.75 , - 1.004497983230312 }, { - 2.625 , - .8891306420668862 }, { - 2.5 , - .9453087204829419 }, { - 2.375 , - 1.17140933049819 }, { - 2.25 , - 1.742814865728253 }, { - 2.125 , - 3.646418346408649 }, { - 1.875 , 4.59211678402902 }, { - 1.75 , 2.762369453883359 }, { - 1.625 , 2.333967935425576 }, { - 1.5 , 2.363271801207355 }, { - 1.375 , 2.782097159933201 }, { - 1.25 , 3.921333447888569 }, { - 1.125 , 7.748638986118379 }, { - 0.875 , - 8.610218970054413 }, { - 0.75 , - 4.834146544295877 }, { - 0.625 , - 3.792697895066561 }, { - 0.5 , - 3.544907701811032 }, { - 0.375 , - 3.825383594908152 }, { - 0.25 , - 4.901666809860711 }, { - 0.125 , - 8.717218859383175 }, { 0.125 , 7.533941598797612 }, { 0.25 , 3.625609908221908 }, { 0.375 , 2.370436184416601 }, { 0.5 , 1.772453850905516 }, { 0.625 , 1.434518848090557 }, { 0.75 , 1.225416702465178 }, { 0.875 , 1.089652357422897 }, { 1.0 , 1.0 }, { 1.125 , .9417426998497015 }, { 1.25 , 0.906402477055477 }, { 1.375 , .8889135691562253 }, { 1.5 , 0.886226925452758 }, { 1.625 , 0.896574280056598 }, { 1.75 , .9190625268488832 }, { 1.875 , .9534458127450348 }, { 2.0 , 1.0 }, { 2.125 , 1.059460537330914 }, { 2.25 , 1.133003096319346 }, { 2.375 , 1.22225615758981 }, { 2.5 , 1.329340388179137 }, { 2.625 , 1.456933205091972 }, { 2.75 , 1.608359421985546 }, { 2.875 , 1.78771089889694 }, { 3.0 , 2.0 }, { 3.125 , 2.251353641828193 }, { 3.25 , 2.549256966718529 }, { 3.375 , 2.902858374275799 }, { 3.5 , 3.323350970447843 }, { 3.625 , 3.824449663366426 }, { 3.75 , 4.422988410460251 }, { 3.875 , 5.139668834328703 }, { 4.0 , 6.0 }, { 4.125 , 7.035480130713102 }, { 4.25 , 8.28508514183522 }, { 4.375 , 9.797147013180819 }, { 4.5 , 11.63172839656745 }, { 4.625 , 13.86363002970329 }, { 4.75 , 16.58620653922594 }, { 4.875 , 19.91621673302373 }, { 5.0 , 24.0 }, { 5.125 , 29.02135553919155 }, { 5.25 , 35.21161185279968 }, { 5.375 , 42.86251818266609 }, { 5.5 , 52.34277778455352 }, { 5.625 , 64.11928888737773 }, { 5.75 , 78.78448106132322 }, { 5.875 , 97.09155657349066 }, { 6.0 , 120.0 }, { 6.125 , 148.7344471383567 }, { 6.25 , 184.8609622271983 }, { 6.375 , 230.3860352318302 }, { 6.5 , 287.8852778150443 }, { 6.625 , 360.6709999914997 }, { 6.75 , 453.0107661026085 }, { 6.875 , 570.4128948692577 }, { 7.0 , 720.0 }, { 7.125 , 910.9984887224346 }, { 7.25 , 1155.38101391999 }, { 7.375 , 1468.710974602918 }, { 7.5 , 1871.254305797788 }, { 7.625 , 2389.445374943686 }, { 7.75 , 3057.822671192607 }, { 7.875 , 3921.588652226146 }, { 8.0 , 5040.0 }, { 8.125 , 6490.864232147346 }, { 8.25 , 8376.512350919926 }, { 8.375 , 10831.74343769652 }, { 8.5 , 14034.40729348341 }, { 8.625 , 18219.5209839456 }, { 8.75 , 23698.12570174271 }, { 8.875 , 30882.5106362809 }, { 9.0 , 40320.0 }, { 9.125 , 52738.27188619719 }, { 9.25 , 69106.22689508938 }, { 9.375 , 90715.85129070834 }, { 9.5 , 119292.461994609 }, { 9.625 , 157143.3684865308 }, { 9.75 , 207358.5998902487 }, { 9.875 , 274082.281896993 }, { 10.0 , 362880.0 }, { 10.125 , 481236.7309615494 }, { 10.25 , 639232.5987795768 }, { 10.375 , 850461.1058503906 }, { 10.5 , 1133278.388948786 }, { 10.625 , 1512504.921682859 }, { 10.75 , 2021746.348929925 }, { 10.875 , 2706562.533732806 }, { 11.0 , 3628800.0 }, { 11.125 , 4872521.900985687 }, { 11.25 , 6552134.137490662 }, { 11.375 , 8823533.973197803 }, { 11.5 , 1.1899423083962249e+7 }, { 11.625 , 1.6070364792880382e+7 }, { 11.75 , 2.1733773250996688e+7 }, { 11.875 , 2.943386755434427e+7 }, { 12.0 , 3.99168e+7 }, { 12.125 , 5.420680614846578e+7 }, { 12.25 , 7.371150904676994e+7 }, { 12.375 , 1.0036769894512501e+8 }, { 12.5 , 1.3684336546556586e+8 }, { 12.625 , 1.8681799071723443e+8 }, { 12.75 , 2.5537183569921107e+8 }, { 12.875 , 3.4952717720783816e+8 }, { 13.0 , 4.790016e+8 }, { 13.125 , 6.572575245501475e+8 }, { 13.25 , 9.029659858229319e+8 }, { 13.375 , 1.2420502744459219e+9 }, { 13.5 , 1.7105420683195732e+9 }, { 13.625 , 2.3585771328050845e+9 }, { 13.75 , 3.2559909051649416e+9 }, { 13.875 , 4.500162406550916e+9 }, { 14.0 , 6.2270208e+9 }, { 14.125 , 8.626505009720685e+9 }, { 14.25 , 1.196429931215385e+10 }, { 14.375 , 1.66124224207142e+10 }, { 14.5 , 2.309231792231424e+10 }, { 14.625 , 3.213561343446927e+10 }, { 14.75 , 4.476987494601794e+10 }, { 14.875 , 6.243975339089396e+10 }, { 15.0 , 8.71782912e+10 }, { 15.125 , 1.218493832623047e+11 }, { 15.25 , 1.704912651981923e+11 }, { 15.375 , 2.388035722977667e+11 }, { 15.5 , 3.348386098735565e+11 }, { 15.625 , 4.699833464791132e+11 }, { 15.75 , 6.603556554537646e+11 }, { 15.875 , 9.287913316895475e+11 }, { 16.0 , 1.307674368e+12 }, { 16.125 , 1.842971921842358e+12 }, { 16.25 , 2.599991794272433e+12 }, { 16.375 , 3.671604924078163e+12 }, { 16.5 , 5.189998453040126e+12 }, { 16.625 , 7.343489788736144e+12 }, { 16.75 , 1.040060157339679e+13 }, { 16.875 , 1.474456239057157e+13 }, { 17.0 , 2.0922789888e+13 }, { 17.125 , 2.971792223970803e+13 }, { 17.25 , 4.224986665692704e+13 }, { 17.375 , 6.012253063177992e+13 }, { 17.5 , 8.563497447516206e+13 }, { 17.625 , 1.220855177377384e+14 }, { 17.75 , 1.742100763543963e+14 }, { 17.875 , 2.488144903408952e+14 }, { 18.0 , 3.55687428096e+14 }, { 18.125 , 5.08919418355e+14 }, { 18.25 , 7.288101998319914e+14 }, { 18.375 , 1.044628969727176e+15 }, { 18.5 , 1.498612053315336e+15 }, { 18.625 , 2.151757250127639e+15 }, { 18.75 , 3.092228855290534e+15 }, { 18.875 , 4.447559014843502e+15 }, { 19.0 , 6.402373705728e+15 }, { 19.125 , 9.224164457684374e+15 }, { 19.25 , 1.330078614693384e+16 }, { 19.375 , 1.919505731873686e+16 }, { 19.5 , 2.772432298633372e+16 }, { 19.625 , 4.007647878362728e+16 }, { 19.75 , 5.797929103669752e+16 }, { 19.875 , 8.39476764051711e+16 }, { 20.0 , 1.21645100408832e+17 }, { 20.5 , 5.406242982335075e+17 }, { 21.0 , 2.43290200817664e+18 }, { 21.5 , 1.10827981137869e+19 }, { 22.0 , 5.109094217170944e+19 }, { 22.5 , 2.382801594464184e+20 }, { 23.0 , 1.124000727777608e+21 }, { 23.5 , 5.361303587544415e+21 }, { 24.0 , 2.585201673888498e+22 }, { 24.5 , 1.259906343072938e+23 }, { 25.0 , 6.204484017332395e+23 }, { 25.5 , 3.086770540528697e+24 }, { 26.0 , 1.551121004333099e+25 }, { 26.5 , 7.871264878348176e+25 }, { 27.0 , 4.032914611266056e+26 }, { 27.5 , 2.085885192762267e+27 }, { 28.0 , 1.088886945041835e+28 }, { 28.5 , 5.736184280096234e+28 }, { 29.0 , 3.048883446117139e+29 }, { 29.5 , 1.634812519827427e+30 }, { 30.0 , 8.841761993739702e+30 }, { 30.5 , 4.822696933490909e+31 }, { 31.0 , 2.65252859812191e+32 }, { 31.5 , 1.470922564714727e+33 }, { 32.0 , 8.222838654177922e+33 }, { 32.5 , 4.633406078851391e+34 }, { 33.0 , 2.631308369336935e+35 }, { 33.5 , 1.505856975626702e+36 }, { 34.0 , 8.683317618811885e+36 }, { 34.5 , 5.044620868349451e+37 }, { 35.0 , 2.952327990396041e+38 }, { 35.5 , 1.740394199580561e+39 }, { 36.0 , 1.033314796638614e+40 }, { 36.5 , 6.178399408510991e+40 }, { 37.0 , 3.719933267899013e+41 }, { 37.5 , 2.255115784106512e+42 }, { 38.0 , 1.376375309122634e+43 }, { 38.5 , 8.456684190399419e+43 }, { 39.0 , 5.230226174666011e+44 }, { 39.5 , 3.255823413303776e+45 }, { 40.0 , 2.039788208119745e+46 }, { 40.5 , 1.286050248254992e+47 }, { 41.0 , 8.159152832478976e+47 }, { 41.5 , 5.208503505432716e+48 }, { 42.0 , 3.345252661316381e+49 }, { 42.5 , 2.161528954754577e+50 }, { 43.0 , 1.40500611775288e+51 }, { 43.5 , 9.186498057706953e+51 }, { 44.0 , 6.041526306337383e+52 }, { 44.5 , 3.996126655102524e+53 }, { 45.0 , 2.658271574788449e+54 }, { 45.5 , 1.778276361520623e+55 }, { 46.0 , 1.196222208654802e+56 }, { 46.5 , 8.091157444918836e+56 }, { 47.0 , 5.502622159812088e+57 }, { 47.5 , 3.762388211887259e+58 }, { 48.0 , 2.586232415111682e+59 }, { 48.5 , 1.787134400646448e+60 }, { 49.0 , 1.241391559253607e+61 }, { 49.5 , 8.667601843135273e+61 }, { 50.0 , 6.082818640342675e+62 }, { 50.5 , 4.290462912351959e+63 }, { 51.0 , 3.041409320171338e+64 }, { 51.5 , 2.16668377073774e+65 }, { 52.0 , 1.551118753287382e+66 }, { 52.5 , 1.115842141929936e+67 }, { 53.0 , 8.065817517094388e+67 }, { 53.5 , 5.858171245132164e+68 }, { 54.0 , 4.274883284060025e+69 }, { 54.5 , 3.134121616145708e+70 }, { 55.0 , 2.308436973392413e+71 }, { 55.5 , 1.70809628079941e+72 }, { 56.0 , 1.269640335365828e+73 }, { 56.5 , 9.479934358436728e+73 }, { 57.0 , 7.109985878048635e+74 }, { 57.5 , 5.356162912516752e+75 }, { 58.0 , 4.052691950487721e+76 }, { 58.5 , 3.079793674697132e+77 }, { 59.0 , 2.350561331282878e+78 }, { 59.5 , 1.801679299697822e+79 }, { 60.0 , 1.386831185456898e+80 }, { 60.5 , 1.071999183320204e+81 }, { 61.0 , 8.320987112741391e+81 }, { 61.5 , 6.485595059087236e+82 }, { 62.0 , 5.075802138772249e+83 }, { 62.5 , 3.98864096133865e+84 }, { 63.0 , 3.146997326038794e+85 }, { 63.5 , 2.492900600836656e+86 }, { 64.0 , 1.98260831540444e+87 }, { 64.5 , 1.582991881531277e+88 }, { 65.0 , 1.268869321858841e+89 }, { 65.5 , 1.021029763587673e+90 }, { 66.0 , 8.247650592082472e+90 }, { 66.5 , 6.687744951499262e+91 }, { 67.0 , 5.443449390774431e+92 }, { 67.5 , 4.447350392747009e+93 }, { 68.0 , 3.647111091818868e+94 }, { 68.5 , 3.001961515104231e+95 }, { 69.0 , 2.48003554243683e+96 }, { 69.5 , 2.056343637846398e+97 }, { 70.0 , 1.711224524281413e+98 }, { 70.5 , 1.429158828303247e+99 }, { 71.0 , 1.19785716699699e+100 }, { 71.5 , 1.00755697395379e+101 }, { 72.0 , 8.50478588567862e+101 }, { 72.5 , 7.20403236376959e+102 }, { 73.0 , 6.12344583768861e+103 }, { 73.5 , 5.22292346373295e+104 }, { 74.0 , 4.47011546151268e+105 }, { 74.5 , 3.83884874584372e+106 }, { 75.0 , 3.30788544151939e+107 }, { 75.5 , 2.85994231565357e+108 }, { 76.0 , 2.48091408113954e+109 }, { 76.5 , 2.15925644831845e+110 }, { 77.0 , 1.88549470166605e+111 }, { 77.5 , 1.65183118296361e+112 }, { 78.0 , 1.45183092028286e+113 }, { 78.5 , 1.2801691667968e+114 }, { 79.0 , 1.13242811782063e+115 }, { 79.5 , 1.00493279593549e+116 }, { 80.0 , 8.94618213078298e+116 }, { 80.5 , 7.98921572768712e+117 }, { 81.0 , 7.15694570462638e+118 }, { 81.5 , 6.43131866078814e+119 }, { 82.0 , 5.79712602074737e+120 }, { 82.5 , 5.24152470854233e+121 }, { 83.0 , 4.75364333701284e+122 }, { 83.5 , 4.32425788454742e+123 }, { 84.0 , 3.94552396972066e+124 }, { 84.5 , 3.6107553335971e+125 }, { 85.0 , 3.31424013456535e+126 }, { 85.5 , 3.05108825688955e+127 }, { 86.0 , 2.81710411438055e+128 }, { 86.5 , 2.60868045964056e+129 }, { 87.0 , 2.42270953836727e+130 }, { 87.5 , 2.25650859758909e+131 }, { 88.0 , 2.10775729837953e+132 }, { 88.5 , 1.97444502289045e+133 }, { 89.0 , 1.85482642257398e+134 }, { 89.5 , 1.74738384525805e+135 }, { 90.0 , 1.65079551609085e+136 }, { 90.5 , 1.56390854150595e+137 }, { 91.0 , 1.48571596448176e+138 }, { 91.5 , 1.41533723006289e+139 }, { 92.0 , 1.3520015276784e+140 }, { 92.5 , 1.29503356550754e+141 }, { 93.0 , 1.24384140546413e+142 }, { 93.5 , 1.19790604809448e+143 }, { 94.0 , 1.15677250708164e+144 }, { 94.5 , 1.12004215496834e+145 }, { 95.0 , 1.08736615665674e+146 }, { 95.5 , 1.05843983644508e+147 }, { 96.0 , 1.03299784882391e+148 }, { 96.5 , 1.01081004380505e+149 }, { 97.0 , 9.9167793487095e+149 }, { 97.5 , 9.75431692271873e+150 }, { 98.0 , 9.61927596824821e+151 }, { 98.5 , 9.51045899965076e+152 }, { 99.0 , 9.42689044888325e+153 }, { 99.5 , 9.367802114656e+154 }, { 100.0 , 9.33262154439441e+155 }, }; @Test public void testGamma() { for (int i = 0; i < GAMMA_REF.length; i++) { final double[] ref = GAMMA_REF[i]; final double x = ref[0]; final double expected = ref[1]; final double actual = Gamma.gamma(x); final double absX = FastMath.abs(x); final int ulps; if (absX <= 8.0) { ulps = 3; } else if (absX <= 20.0) { ulps = 5; } else if (absX <= 30.0) { ulps = 50; } else if (absX <= 50.0) { ulps = 180; } else { ulps = 500; } final double tol = ulps * FastMath.ulp(expected); Assert.assertEquals(Double.toString(x), expected, actual, tol); } } @Test public void testGammaNegativeInteger() { for (int i = -100; i <= 0; i++) { Assert.assertTrue(Integer.toString(i), Double.isNaN(Gamma.gamma(i))); } } private void checkRelativeError(String msg, double expected, double actual, double tolerance) { Assert.assertEquals(msg, expected, actual, FastMath.abs(tolerance * actual)); } } commons-math3-3.2-src/src/test/java/org/apache/commons/math3/special/BetaTest.java100644 1750 1750 113776 12126627667 26643 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.special; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import org.apache.commons.math3.TestUtils; import org.apache.commons.math3.exception.MathIllegalArgumentException; import org.apache.commons.math3.exception.NumberIsTooSmallException; import org.apache.commons.math3.exception.OutOfRangeException; import org.apache.commons.math3.util.FastMath; import org.junit.Assert; import org.junit.Test; /** * @version $Id: BetaTest.java 1415853 2012-11-30 21:02:23Z celestin $ */ public class BetaTest { /* * Use reflection to test private methods. */ private static final Method LOG_GAMMA_SUM_METHOD; private static final Method LOG_GAMMA_MINUS_LOG_GAMMA_SUM_METHOD; private static final Method SUM_DELTA_MINUS_DELTA_SUM_METHOD; static { final Class b; final Class d = Double.TYPE; b = Beta.class; Method m = null; try { m = b.getDeclaredMethod("logGammaSum", d, d); } catch (NoSuchMethodException e) { Assert.fail(e.getMessage()); } LOG_GAMMA_SUM_METHOD = m; LOG_GAMMA_SUM_METHOD.setAccessible(true); m = null; try { m = b.getDeclaredMethod("logGammaMinusLogGammaSum",d, d); } catch (NoSuchMethodException e) { Assert.fail(e.getMessage()); } LOG_GAMMA_MINUS_LOG_GAMMA_SUM_METHOD = m; LOG_GAMMA_MINUS_LOG_GAMMA_SUM_METHOD.setAccessible(true); m = null; try { m = b.getDeclaredMethod("sumDeltaMinusDeltaSum",d, d); } catch (NoSuchMethodException e) { Assert.fail(e.getMessage()); } SUM_DELTA_MINUS_DELTA_SUM_METHOD = m; SUM_DELTA_MINUS_DELTA_SUM_METHOD.setAccessible(true); } private void testRegularizedBeta(double expected, double x, double a, double b) { double actual = Beta.regularizedBeta(x, a, b); TestUtils.assertEquals(expected, actual, 10e-15); } private void testLogBeta(double expected, double a, double b) { double actual = Beta.logBeta(a, b); TestUtils.assertEquals(expected, actual, 10e-15); } @Test public void testRegularizedBetaNanPositivePositive() { testRegularizedBeta(Double.NaN, Double.NaN, 1.0, 1.0); } @Test public void testRegularizedBetaPositiveNanPositive() { testRegularizedBeta(Double.NaN, 0.5, Double.NaN, 1.0); } @Test public void testRegularizedBetaPositivePositiveNan() { testRegularizedBeta(Double.NaN, 0.5, 1.0, Double.NaN); } @Test public void testRegularizedBetaNegativePositivePositive() { testRegularizedBeta(Double.NaN, -0.5, 1.0, 2.0); } @Test public void testRegularizedBetaPositiveNegativePositive() { testRegularizedBeta(Double.NaN, 0.5, -1.0, 2.0); } @Test public void testRegularizedBetaPositivePositiveNegative() { testRegularizedBeta(Double.NaN, 0.5, 1.0, -2.0); } @Test public void testRegularizedBetaZeroPositivePositive() { testRegularizedBeta(0.0, 0.0, 1.0, 2.0); } @Test public void testRegularizedBetaPositiveZeroPositive() { testRegularizedBeta(Double.NaN, 0.5, 0.0, 2.0); } @Test public void testRegularizedBetaPositivePositiveZero() { testRegularizedBeta(Double.NaN, 0.5, 1.0, 0.0); } @Test public void testRegularizedBetaPositivePositivePositive() { testRegularizedBeta(0.75, 0.5, 1.0, 2.0); } @Test public void testLogBetaNanPositive() { testLogBeta(Double.NaN, Double.NaN, 2.0); } @Test public void testLogBetaPositiveNan() { testLogBeta(Double.NaN, 1.0, Double.NaN); } @Test public void testLogBetaNegativePositive() { testLogBeta(Double.NaN, -1.0, 2.0); } @Test public void testLogBetaPositiveNegative() { testLogBeta(Double.NaN, 1.0, -2.0); } @Test public void testLogBetaZeroPositive() { testLogBeta(Double.NaN, 0.0, 2.0); } @Test public void testLogBetaPositiveZero() { testLogBeta(Double.NaN, 1.0, 0.0); } @Test public void testLogBetaPositivePositive() { testLogBeta(-0.693147180559945, 1.0, 2.0); } /** * Reference data for the {@link Gamma#logGammaSum(double, double)} * function. This data was generated with the following * Maxima script. * *
                               * kill(all);
                               *
                               * fpprec : 64;
                               * gsumln(a, b) := log(gamma(a + b));
                               *
                               * x : [1.0b0, 1.125b0, 1.25b0, 1.375b0, 1.5b0, 1.625b0, 1.75b0, 1.875b0, 2.0b0];
                               *
                               * for i : 1 while i <= length(x) do
                               *   for j : 1 while j <= length(x) do block(
                               *     a : x[i],
                               *     b : x[j],
                               *     print("{", float(a), ",", float(b), ",", float(gsumln(a, b)), "},")
                               *   );
                               * 
                          */ private static final double[][] LOG_GAMMA_SUM_REF = { { 1.0 , 1.0 , 0.0 }, { 1.0 , 1.125 , .05775985153034387 }, { 1.0 , 1.25 , .1248717148923966 }, { 1.0 , 1.375 , .2006984603774558 }, { 1.0 , 1.5 , .2846828704729192 }, { 1.0 , 1.625 , .3763336820249054 }, { 1.0 , 1.75 , .4752146669149371 }, { 1.0 , 1.875 , .5809359740231859 }, { 1.0 , 2.0 , .6931471805599453 }, { 1.125 , 1.0 , .05775985153034387 }, { 1.125 , 1.125 , .1248717148923966 }, { 1.125 , 1.25 , .2006984603774558 }, { 1.125 , 1.375 , .2846828704729192 }, { 1.125 , 1.5 , .3763336820249054 }, { 1.125 , 1.625 , .4752146669149371 }, { 1.125 , 1.75 , .5809359740231859 }, { 1.125 , 1.875 , .6931471805599453 }, { 1.125 , 2.0 , 0.811531653906724 }, { 1.25 , 1.0 , .1248717148923966 }, { 1.25 , 1.125 , .2006984603774558 }, { 1.25 , 1.25 , .2846828704729192 }, { 1.25 , 1.375 , .3763336820249054 }, { 1.25 , 1.5 , .4752146669149371 }, { 1.25 , 1.625 , .5809359740231859 }, { 1.25 , 1.75 , .6931471805599453 }, { 1.25 , 1.875 , 0.811531653906724 }, { 1.25 , 2.0 , .9358019311087253 }, { 1.375 , 1.0 , .2006984603774558 }, { 1.375 , 1.125 , .2846828704729192 }, { 1.375 , 1.25 , .3763336820249054 }, { 1.375 , 1.375 , .4752146669149371 }, { 1.375 , 1.5 , .5809359740231859 }, { 1.375 , 1.625 , .6931471805599453 }, { 1.375 , 1.75 , 0.811531653906724 }, { 1.375 , 1.875 , .9358019311087253 }, { 1.375 , 2.0 , 1.06569589786406 }, { 1.5 , 1.0 , .2846828704729192 }, { 1.5 , 1.125 , .3763336820249054 }, { 1.5 , 1.25 , .4752146669149371 }, { 1.5 , 1.375 , .5809359740231859 }, { 1.5 , 1.5 , .6931471805599453 }, { 1.5 , 1.625 , 0.811531653906724 }, { 1.5 , 1.75 , .9358019311087253 }, { 1.5 , 1.875 , 1.06569589786406 }, { 1.5 , 2.0 , 1.200973602347074 }, { 1.625 , 1.0 , .3763336820249054 }, { 1.625 , 1.125 , .4752146669149371 }, { 1.625 , 1.25 , .5809359740231859 }, { 1.625 , 1.375 , .6931471805599453 }, { 1.625 , 1.5 , 0.811531653906724 }, { 1.625 , 1.625 , .9358019311087253 }, { 1.625 , 1.75 , 1.06569589786406 }, { 1.625 , 1.875 , 1.200973602347074 }, { 1.625 , 2.0 , 1.341414578068493 }, { 1.75 , 1.0 , .4752146669149371 }, { 1.75 , 1.125 , .5809359740231859 }, { 1.75 , 1.25 , .6931471805599453 }, { 1.75 , 1.375 , 0.811531653906724 }, { 1.75 , 1.5 , .9358019311087253 }, { 1.75 , 1.625 , 1.06569589786406 }, { 1.75 , 1.75 , 1.200973602347074 }, { 1.75 , 1.875 , 1.341414578068493 }, { 1.75 , 2.0 , 1.486815578593417 }, { 1.875 , 1.0 , .5809359740231859 }, { 1.875 , 1.125 , .6931471805599453 }, { 1.875 , 1.25 , 0.811531653906724 }, { 1.875 , 1.375 , .9358019311087253 }, { 1.875 , 1.5 , 1.06569589786406 }, { 1.875 , 1.625 , 1.200973602347074 }, { 1.875 , 1.75 , 1.341414578068493 }, { 1.875 , 1.875 , 1.486815578593417 }, { 1.875 , 2.0 , 1.6369886482725 }, { 2.0 , 1.0 , .6931471805599453 }, { 2.0 , 1.125 , 0.811531653906724 }, { 2.0 , 1.25 , .9358019311087253 }, { 2.0 , 1.375 , 1.06569589786406 }, { 2.0 , 1.5 , 1.200973602347074 }, { 2.0 , 1.625 , 1.341414578068493 }, { 2.0 , 1.75 , 1.486815578593417 }, { 2.0 , 1.875 , 1.6369886482725 }, { 2.0 , 2.0 , 1.791759469228055 }, }; private static double logGammaSum(final double a, final double b) { /* * Use reflection to access private method. */ try { return ((Double) LOG_GAMMA_SUM_METHOD.invoke(null, a, b)).doubleValue(); } catch (final IllegalAccessException e) { Assert.fail(e.getMessage()); } catch (final IllegalArgumentException e) { Assert.fail(e.getMessage()); } catch (final InvocationTargetException e) { final Throwable te = e.getTargetException(); if (te instanceof MathIllegalArgumentException) { throw (MathIllegalArgumentException) te; } Assert.fail(e.getMessage()); } return Double.NaN; } @Test public void testLogGammaSum() { final int ulps = 2; for (int i = 0; i < LOG_GAMMA_SUM_REF.length; i++) { final double[] ref = LOG_GAMMA_SUM_REF[i]; final double a = ref[0]; final double b = ref[1]; final double expected = ref[2]; final double actual = logGammaSum(a, b); final double tol = ulps * FastMath.ulp(expected); final StringBuilder builder = new StringBuilder(); builder.append(a).append(", ").append(b); Assert.assertEquals(builder.toString(), expected, actual, tol); } } @Test(expected = OutOfRangeException.class) public void testLogGammaSumPrecondition1() { logGammaSum(0.0, 1.0); } @Test(expected = OutOfRangeException.class) public void testLogGammaSumPrecondition2() { logGammaSum(3.0, 1.0); } @Test(expected = OutOfRangeException.class) public void testLogGammaSumPrecondition3() { logGammaSum(1.0, 0.0); } @Test(expected = OutOfRangeException.class) public void testLogGammaSumPrecondition4() { logGammaSum(1.0, 3.0); } private static final double[][] LOG_GAMMA_MINUS_LOG_GAMMA_SUM_REF = { // { 0.0 , 8.0 , 0.0 }, // { 0.0 , 9.0 , 0.0 }, { 0.0 , 10.0 , 0.0 }, { 0.0 , 11.0 , 0.0 }, { 0.0 , 12.0 , 0.0 }, { 0.0 , 13.0 , 0.0 }, { 0.0 , 14.0 , 0.0 }, { 0.0 , 15.0 , 0.0 }, { 0.0 , 16.0 , 0.0 }, { 0.0 , 17.0 , 0.0 }, { 0.0 , 18.0 , 0.0 }, // { 1.0 , 8.0 , - 2.079441541679836 }, // { 1.0 , 9.0 , - 2.19722457733622 }, { 1.0 , 10.0 , - 2.302585092994046 }, { 1.0 , 11.0 , - 2.397895272798371 }, { 1.0 , 12.0 , - 2.484906649788 }, { 1.0 , 13.0 , - 2.564949357461537 }, { 1.0 , 14.0 , - 2.639057329615258 }, { 1.0 , 15.0 , - 2.70805020110221 }, { 1.0 , 16.0 , - 2.772588722239781 }, { 1.0 , 17.0 , - 2.833213344056216 }, { 1.0 , 18.0 , - 2.890371757896165 }, // { 2.0 , 8.0 , - 4.276666119016055 }, // { 2.0 , 9.0 , - 4.499809670330265 }, { 2.0 , 10.0 , - 4.700480365792417 }, { 2.0 , 11.0 , - 4.882801922586371 }, { 2.0 , 12.0 , - 5.049856007249537 }, { 2.0 , 13.0 , - 5.204006687076795 }, { 2.0 , 14.0 , - 5.347107530717468 }, { 2.0 , 15.0 , - 5.480638923341991 }, { 2.0 , 16.0 , - 5.605802066295998 }, { 2.0 , 17.0 , - 5.723585101952381 }, { 2.0 , 18.0 , - 5.834810737062605 }, // { 3.0 , 8.0 , - 6.579251212010101 }, // { 3.0 , 9.0 , - 6.897704943128636 }, { 3.0 , 10.0 , - 7.185387015580416 }, { 3.0 , 11.0 , - 7.447751280047908 }, { 3.0 , 12.0 , - 7.688913336864796 }, { 3.0 , 13.0 , - 7.912056888179006 }, { 3.0 , 14.0 , - 8.11969625295725 }, { 3.0 , 15.0 , - 8.313852267398207 }, { 3.0 , 16.0 , - 8.496173824192162 }, { 3.0 , 17.0 , - 8.668024081118821 }, { 3.0 , 18.0 , - 8.830543010616596 }, // { 4.0 , 8.0 , - 8.977146484808472 }, // { 4.0 , 9.0 , - 9.382611592916636 }, { 4.0 , 10.0 , - 9.750336373041954 }, { 4.0 , 11.0 , - 10.08680860966317 }, { 4.0 , 12.0 , - 10.39696353796701 }, { 4.0 , 13.0 , - 10.68464561041879 }, { 4.0 , 14.0 , - 10.95290959701347 }, { 4.0 , 15.0 , - 11.20422402529437 }, { 4.0 , 16.0 , - 11.4406128033586 }, { 4.0 , 17.0 , - 11.66375635467281 }, { 4.0 , 18.0 , - 11.87506544834002 }, // { 5.0 , 8.0 , - 11.46205313459647 }, // { 5.0 , 9.0 , - 11.94756095037817 }, { 5.0 , 10.0 , - 12.38939370265721 }, { 5.0 , 11.0 , - 12.79485881076538 }, { 5.0 , 12.0 , - 13.16955226020679 }, { 5.0 , 13.0 , - 13.517858954475 }, { 5.0 , 14.0 , - 13.84328135490963 }, { 5.0 , 15.0 , - 14.14866300446081 }, { 5.0 , 16.0 , - 14.43634507691259 }, { 5.0 , 17.0 , - 14.70827879239624 }, { 5.0 , 18.0 , - 14.96610790169833 }, // { 6.0 , 8.0 , - 14.02700249205801 }, // { 6.0 , 9.0 , - 14.58661827999343 }, { 6.0 , 10.0 , - 15.09744390375942 }, { 6.0 , 11.0 , - 15.56744753300516 }, { 6.0 , 12.0 , - 16.002765604263 }, { 6.0 , 13.0 , - 16.40823071237117 }, { 6.0 , 14.0 , - 16.78772033407607 }, { 6.0 , 15.0 , - 17.14439527801481 }, { 6.0 , 16.0 , - 17.48086751463602 }, { 6.0 , 17.0 , - 17.79932124575455 }, { 6.0 , 18.0 , - 18.10160211762749 }, // { 7.0 , 8.0 , - 16.66605982167327 }, // { 7.0 , 9.0 , - 17.29466848109564 }, { 7.0 , 10.0 , - 17.8700326259992 }, { 7.0 , 11.0 , - 18.40066087706137 }, { 7.0 , 12.0 , - 18.89313736215917 }, { 7.0 , 13.0 , - 19.35266969153761 }, { 7.0 , 14.0 , - 19.78345260763006 }, { 7.0 , 15.0 , - 20.18891771573823 }, { 7.0 , 16.0 , - 20.57190996799433 }, { 7.0 , 17.0 , - 20.9348154616837 }, { 7.0 , 18.0 , - 21.27965594797543 }, // { 8.0 , 8.0 , - 19.37411002277548 }, // { 8.0 , 9.0 , - 20.06725720333542 }, { 8.0 , 10.0 , - 20.70324597005542 }, { 8.0 , 11.0 , - 21.29103263495754 }, { 8.0 , 12.0 , - 21.83757634132561 }, { 8.0 , 13.0 , - 22.3484019650916 }, { 8.0 , 14.0 , - 22.82797504535349 }, { 8.0 , 15.0 , - 23.27996016909654 }, { 8.0 , 16.0 , - 23.70740418392348 }, { 8.0 , 17.0 , - 24.11286929203165 }, { 8.0 , 18.0 , - 24.49853177284363 }, // { 9.0 , 8.0 , - 22.14669874501526 }, // { 9.0 , 9.0 , - 22.90047054739164 }, { 9.0 , 10.0 , - 23.59361772795159 }, { 9.0 , 11.0 , - 24.23547161412398 }, { 9.0 , 12.0 , - 24.8333086148796 }, { 9.0 , 13.0 , - 25.39292440281502 }, { 9.0 , 14.0 , - 25.9190174987118 }, { 9.0 , 15.0 , - 26.41545438502569 }, { 9.0 , 16.0 , - 26.88545801427143 }, { 9.0 , 17.0 , - 27.33174511689985 }, { 9.0 , 18.0 , - 27.75662831086511 }, // { 10.0 , 8.0 , - 24.97991208907148 }, // { 10.0 , 9.0 , - 25.7908423052878 }, { 10.0 , 10.0 , - 26.53805670711802 }, { 10.0 , 11.0 , - 27.23120388767797 }, { 10.0 , 12.0 , - 27.87783105260302 }, { 10.0 , 13.0 , - 28.48396685617334 }, { 10.0 , 14.0 , - 29.05451171464095 }, { 10.0 , 15.0 , - 29.59350821537364 }, { 10.0 , 16.0 , - 30.10433383913963 }, { 10.0 , 17.0 , - 30.58984165492133 }, { 10.0 , 18.0 , - 31.05246517686944 }, }; private static double logGammaMinusLogGammaSum(final double a, final double b) { /* * Use reflection to access private method. */ try { final Method m = LOG_GAMMA_MINUS_LOG_GAMMA_SUM_METHOD; return ((Double) m.invoke(null, a, b)).doubleValue(); } catch (final IllegalAccessException e) { Assert.fail(e.getMessage()); } catch (final IllegalArgumentException e) { Assert.fail(e.getMessage()); } catch (final InvocationTargetException e) { final Throwable te = e.getTargetException(); if (te instanceof MathIllegalArgumentException) { throw (MathIllegalArgumentException) te; } Assert.fail(e.getMessage()); } return Double.NaN; } @Test public void testLogGammaMinusLogGammaSum() { final int ulps = 4; for (int i = 0; i < LOG_GAMMA_MINUS_LOG_GAMMA_SUM_REF.length; i++) { final double[] ref = LOG_GAMMA_MINUS_LOG_GAMMA_SUM_REF[i]; final double a = ref[0]; final double b = ref[1]; final double expected = ref[2]; final double actual = logGammaMinusLogGammaSum(a, b); final double tol = ulps * FastMath.ulp(expected); final StringBuilder builder = new StringBuilder(); builder.append(a).append(", ").append(b); Assert.assertEquals(builder.toString(), expected, actual, tol); } } @Test(expected = NumberIsTooSmallException.class) public void testLogGammaMinusLogGammaSumPrecondition1() { logGammaMinusLogGammaSum(-1.0, 8.0); } @Test(expected = NumberIsTooSmallException.class) public void testLogGammaMinusLogGammaSumPrecondition2() { logGammaMinusLogGammaSum(1.0, 7.0); } private static final double[][] SUM_DELTA_MINUS_DELTA_SUM_REF = { { 10.0 , 10.0 , .01249480717472882 }, { 10.0 , 11.0 , .01193628470267385 }, { 10.0 , 12.0 , .01148578547212797 }, { 10.0 , 13.0 , .01111659739668398 }, { 10.0 , 14.0 , .01080991216314295 }, { 10.0 , 15.0 , .01055214134859758 }, { 10.0 , 16.0 , .01033324912491747 }, { 10.0 , 17.0 , .01014568069918883 }, { 10.0 , 18.0 , .009983653199146491 }, { 10.0 , 19.0 , .009842674320242729 }, { 10.0 , 20.0 , 0.0097192081956071 }, { 11.0 , 10.0 , .01193628470267385 }, { 11.0 , 11.0 , .01135973290745925 }, { 11.0 , 12.0 , .01089355537047828 }, { 11.0 , 13.0 , .01051064829297728 }, { 11.0 , 14.0 , 0.0101918899639826 }, { 11.0 , 15.0 , .009923438811859604 }, { 11.0 , 16.0 , .009695052724952705 }, { 11.0 , 17.0 , 0.00949900745283617 }, { 11.0 , 18.0 , .009329379874933402 }, { 11.0 , 19.0 , 0.00918156080743147 }, { 11.0 , 20.0 , 0.00905191635141762 }, { 12.0 , 10.0 , .01148578547212797 }, { 12.0 , 11.0 , .01089355537047828 }, { 12.0 , 12.0 , .01041365883144029 }, { 12.0 , 13.0 , .01001867865848564 }, { 12.0 , 14.0 , 0.00968923999191334 }, { 12.0 , 15.0 , .009411294976563555 }, { 12.0 , 16.0 , .009174432043268762 }, { 12.0 , 17.0 , .008970786693291802 }, { 12.0 , 18.0 , .008794318926790865 }, { 12.0 , 19.0 , .008640321527910711 }, { 12.0 , 20.0 , .008505077879954796 }, { 13.0 , 10.0 , .01111659739668398 }, { 13.0 , 11.0 , .01051064829297728 }, { 13.0 , 12.0 , .01001867865848564 }, { 13.0 , 13.0 , .009613018147953376 }, { 13.0 , 14.0 , .009274085618154277 }, { 13.0 , 15.0 , 0.0089876637564166 }, { 13.0 , 16.0 , .008743200745261382 }, { 13.0 , 17.0 , .008532715206686251 }, { 13.0 , 18.0 , .008350069108807093 }, { 13.0 , 19.0 , .008190472517984874 }, { 13.0 , 20.0 , .008050138630244345 }, { 14.0 , 10.0 , .01080991216314295 }, { 14.0 , 11.0 , 0.0101918899639826 }, { 14.0 , 12.0 , 0.00968923999191334 }, { 14.0 , 13.0 , .009274085618154277 }, { 14.0 , 14.0 , .008926676241967286 }, { 14.0 , 15.0 , .008632654302369184 }, { 14.0 , 16.0 , .008381351102615795 }, { 14.0 , 17.0 , .008164687232662443 }, { 14.0 , 18.0 , .007976441942841219 }, { 14.0 , 19.0 , .007811755112234388 }, { 14.0 , 20.0 , .007666780069317652 }, { 15.0 , 10.0 , .01055214134859758 }, { 15.0 , 11.0 , .009923438811859604 }, { 15.0 , 12.0 , .009411294976563555 }, { 15.0 , 13.0 , 0.0089876637564166 }, { 15.0 , 14.0 , .008632654302369184 }, { 15.0 , 15.0 , 0.00833179217417291 }, { 15.0 , 16.0 , .008074310643041299 }, { 15.0 , 17.0 , .007852047581145882 }, { 15.0 , 18.0 , .007658712051540045 }, { 15.0 , 19.0 , .007489384065757007 }, { 15.0 , 20.0 , .007340165635725612 }, { 16.0 , 10.0 , .01033324912491747 }, { 16.0 , 11.0 , .009695052724952705 }, { 16.0 , 12.0 , .009174432043268762 }, { 16.0 , 13.0 , .008743200745261382 }, { 16.0 , 14.0 , .008381351102615795 }, { 16.0 , 15.0 , .008074310643041299 }, { 16.0 , 16.0 , .007811229919967624 }, { 16.0 , 17.0 , .007583876618287594 }, { 16.0 , 18.0 , .007385899933505551 }, { 16.0 , 19.0 , .007212328560607852 }, { 16.0 , 20.0 , .007059220321091879 }, { 17.0 , 10.0 , .01014568069918883 }, { 17.0 , 11.0 , 0.00949900745283617 }, { 17.0 , 12.0 , .008970786693291802 }, { 17.0 , 13.0 , .008532715206686251 }, { 17.0 , 14.0 , .008164687232662443 }, { 17.0 , 15.0 , .007852047581145882 }, { 17.0 , 16.0 , .007583876618287594 }, { 17.0 , 17.0 , .007351882161431358 }, { 17.0 , 18.0 , .007149662089534654 }, { 17.0 , 19.0 , .006972200907152378 }, { 17.0 , 20.0 , .006815518216094137 }, { 18.0 , 10.0 , .009983653199146491 }, { 18.0 , 11.0 , .009329379874933402 }, { 18.0 , 12.0 , .008794318926790865 }, { 18.0 , 13.0 , .008350069108807093 }, { 18.0 , 14.0 , .007976441942841219 }, { 18.0 , 15.0 , .007658712051540045 }, { 18.0 , 16.0 , .007385899933505551 }, { 18.0 , 17.0 , .007149662089534654 }, { 18.0 , 18.0 , .006943552208153373 }, { 18.0 , 19.0 , .006762516574228829 }, { 18.0 , 20.0 , .006602541598043117 }, { 19.0 , 10.0 , .009842674320242729 }, { 19.0 , 11.0 , 0.00918156080743147 }, { 19.0 , 12.0 , .008640321527910711 }, { 19.0 , 13.0 , .008190472517984874 }, { 19.0 , 14.0 , .007811755112234388 }, { 19.0 , 15.0 , .007489384065757007 }, { 19.0 , 16.0 , .007212328560607852 }, { 19.0 , 17.0 , .006972200907152378 }, { 19.0 , 18.0 , .006762516574228829 }, { 19.0 , 19.0 , .006578188655176814 }, { 19.0 , 20.0 , .006415174623476747 }, { 20.0 , 10.0 , 0.0097192081956071 }, { 20.0 , 11.0 , 0.00905191635141762 }, { 20.0 , 12.0 , .008505077879954796 }, { 20.0 , 13.0 , .008050138630244345 }, { 20.0 , 14.0 , .007666780069317652 }, { 20.0 , 15.0 , .007340165635725612 }, { 20.0 , 16.0 , .007059220321091879 }, { 20.0 , 17.0 , .006815518216094137 }, { 20.0 , 18.0 , .006602541598043117 }, { 20.0 , 19.0 , .006415174623476747 }, { 20.0 , 20.0 , .006249349445691423 }, }; private static double sumDeltaMinusDeltaSum(final double a, final double b) { /* * Use reflection to access private method. */ try { final Method m = SUM_DELTA_MINUS_DELTA_SUM_METHOD; return ((Double) m.invoke(null, a, b)).doubleValue(); } catch (final IllegalAccessException e) { Assert.fail(e.getMessage()); } catch (final IllegalArgumentException e) { Assert.fail(e.getMessage()); } catch (final InvocationTargetException e) { final Throwable te = e.getTargetException(); if (te instanceof MathIllegalArgumentException) { throw (MathIllegalArgumentException) te; } Assert.fail(e.getMessage()); } return Double.NaN; } @Test public void testSumDeltaMinusDeltaSum() { final int ulps = 3; for (int i = 0; i < SUM_DELTA_MINUS_DELTA_SUM_REF.length; i++) { final double[] ref = SUM_DELTA_MINUS_DELTA_SUM_REF[i]; final double a = ref[0]; final double b = ref[1]; final double expected = ref[2]; final double actual = sumDeltaMinusDeltaSum(a, b); final double tol = ulps * FastMath.ulp(expected); final StringBuilder builder = new StringBuilder(); builder.append(a).append(", ").append(b); Assert.assertEquals(builder.toString(), expected, actual, tol); } } @Test(expected = NumberIsTooSmallException.class) public void testSumDeltaMinusDeltaSumPrecondition1() { sumDeltaMinusDeltaSum(9.0, 10.0); } @Test(expected = NumberIsTooSmallException.class) public void testSumDeltaMinusDeltaSumPrecondition2() { sumDeltaMinusDeltaSum(10.0, 9.0); } private static final double[][] LOG_BETA_REF = { { 0.125 , 0.125 , 2.750814190409515 }, { 0.125 , 0.25 , 2.444366899981226 }, { 0.125 , 0.5 , 2.230953804989556 }, { 0.125 , 1.0 , 2.079441541679836 }, { 0.125 , 2.0 , 1.961658506023452 }, { 0.125 , 3.0 , 1.901033884207018 }, { 0.125 , 4.0 , 1.860211889686763 }, { 0.125 , 5.0 , 1.829440231020009 }, { 0.125 , 6.0 , 1.804747618429637 }, { 0.125 , 7.0 , 1.784128331226902 }, { 0.125 , 8.0 , 1.766428754127501 }, { 0.125 , 9.0 , 1.750924567591535 }, { 0.125 , 10.0 , 1.7371312454592 }, { 0.125 , 1000.0 , 1.156003642015969 }, { 0.125 , 1001.0 , 1.155878649827818 }, { 0.125 , 10000.0 , .8681312798751318 }, { 0.25 , 0.125 , 2.444366899981226 }, { 0.25 , 0.25 , 2.003680106471455 }, { 0.25 , 0.5 , 1.657106516191482 }, { 0.25 , 1.0 , 1.386294361119891 }, { 0.25 , 2.0 , 1.163150809805681 }, { 0.25 , 3.0 , 1.045367774149297 }, { 0.25 , 4.0 , 0.965325066475761 }, { 0.25 , 5.0 , .9047004446593261 }, { 0.25 , 6.0 , .8559102804898941 }, { 0.25 , 7.0 , 0.815088285969639 }, { 0.25 , 8.0 , .7799969661583689 }, { 0.25 , 9.0 , .7492253074916152 }, { 0.25 , 10.0 , .7218263333035008 }, { 0.25 , 1000.0 , - .4388225372378877 }, { 0.25 , 1001.0 , - .4390725059930951 }, { 0.25 , 10000.0 , - 1.014553193217846 }, { 0.5 , 0.125 , 2.230953804989556 }, { 0.5 , 0.25 , 1.657106516191482 }, { 0.5 , 0.5 , 1.1447298858494 }, { 0.5 , 1.0 , .6931471805599453 }, { 0.5 , 2.0 , .2876820724517809 }, { 0.5 , 3.0 , .06453852113757118 }, // { 0.5 , 4.0 , - .08961215868968714 }, { 0.5 , 5.0 , - .2073951943460706 }, { 0.5 , 6.0 , - .3027053741503954 }, { 0.5 , 7.0 , - .3827480818239319 }, { 0.5 , 8.0 , - .4517409533108833 }, { 0.5 , 9.0 , - .5123655751273182 }, { 0.5 , 10.0 , - .5664327963975939 }, { 0.5 , 1000.0 , - 2.881387696571577 }, { 0.5 , 1001.0 , - 2.881887571613228 }, { 0.5 , 10000.0 , - 4.032792743063396 }, { 1.0 , 0.125 , 2.079441541679836 }, { 1.0 , 0.25 , 1.386294361119891 }, { 1.0 , 0.5 , .6931471805599453 }, { 1.0 , 1.0 , 0.0 }, { 1.0 , 2.0 , - .6931471805599453 }, { 1.0 , 3.0 , - 1.09861228866811 }, { 1.0 , 4.0 , - 1.386294361119891 }, { 1.0 , 5.0 , - 1.6094379124341 }, { 1.0 , 6.0 , - 1.791759469228055 }, { 1.0 , 7.0 , - 1.945910149055313 }, { 1.0 , 8.0 , - 2.079441541679836 }, { 1.0 , 9.0 , - 2.19722457733622 }, { 1.0 , 10.0 , - 2.302585092994046 }, { 1.0 , 1000.0 , - 6.907755278982137 }, { 1.0 , 1001.0 , - 6.90875477931522 }, { 1.0 , 10000.0 , - 9.210340371976184 }, { 2.0 , 0.125 , 1.961658506023452 }, { 2.0 , 0.25 , 1.163150809805681 }, { 2.0 , 0.5 , .2876820724517809 }, { 2.0 , 1.0 , - .6931471805599453 }, { 2.0 , 2.0 , - 1.791759469228055 }, { 2.0 , 3.0 , - 2.484906649788 }, { 2.0 , 4.0 , - 2.995732273553991 }, { 2.0 , 5.0 , - 3.401197381662155 }, { 2.0 , 6.0 , - 3.737669618283368 }, { 2.0 , 7.0 , - 4.02535169073515 }, { 2.0 , 8.0 , - 4.276666119016055 }, { 2.0 , 9.0 , - 4.499809670330265 }, { 2.0 , 10.0 , - 4.700480365792417 }, { 2.0 , 1000.0 , - 13.81651005829736 }, { 2.0 , 1001.0 , - 13.81850806096003 }, { 2.0 , 10000.0 , - 18.4207807389527 }, { 3.0 , 0.125 , 1.901033884207018 }, { 3.0 , 0.25 , 1.045367774149297 }, { 3.0 , 0.5 , .06453852113757118 }, { 3.0 , 1.0 , - 1.09861228866811 }, { 3.0 , 2.0 , - 2.484906649788 }, { 3.0 , 3.0 , - 3.401197381662155 }, { 3.0 , 4.0 , - 4.0943445622221 }, { 3.0 , 5.0 , - 4.653960350157523 }, { 3.0 , 6.0 , - 5.123963979403259 }, { 3.0 , 7.0 , - 5.529429087511423 }, { 3.0 , 8.0 , - 5.886104031450156 }, { 3.0 , 9.0 , - 6.20455776256869 }, { 3.0 , 10.0 , - 6.492239835020471 }, { 3.0 , 1000.0 , - 20.03311615938222 }, { 3.0 , 1001.0 , - 20.03611166836202 }, { 3.0 , 10000.0 , - 26.9381739103716 }, { 4.0 , 0.125 , 1.860211889686763 }, { 4.0 , 0.25 , 0.965325066475761 }, // { 4.0 , 0.5 , - .08961215868968714 }, { 4.0 , 1.0 , - 1.386294361119891 }, { 4.0 , 2.0 , - 2.995732273553991 }, { 4.0 , 3.0 , - 4.0943445622221 }, { 4.0 , 4.0 , - 4.941642422609304 }, { 4.0 , 5.0 , - 5.634789603169249 }, { 4.0 , 6.0 , - 6.222576268071369 }, { 4.0 , 7.0 , - 6.733401891837359 }, { 4.0 , 8.0 , - 7.185387015580416 }, { 4.0 , 9.0 , - 7.590852123688581 }, { 4.0 , 10.0 , - 7.958576903813898 }, { 4.0 , 1000.0 , - 25.84525465867605 }, { 4.0 , 1001.0 , - 25.84924667994559 }, { 4.0 , 10000.0 , - 35.05020194868867 }, { 5.0 , 0.125 , 1.829440231020009 }, { 5.0 , 0.25 , .9047004446593261 }, { 5.0 , 0.5 , - .2073951943460706 }, { 5.0 , 1.0 , - 1.6094379124341 }, { 5.0 , 2.0 , - 3.401197381662155 }, { 5.0 , 3.0 , - 4.653960350157523 }, { 5.0 , 4.0 , - 5.634789603169249 }, { 5.0 , 5.0 , - 6.445719819385578 }, { 5.0 , 6.0 , - 7.138866999945524 }, { 5.0 , 7.0 , - 7.745002803515839 }, { 5.0 , 8.0 , - 8.283999304248526 }, { 5.0 , 9.0 , - 8.769507120030227 }, { 5.0 , 10.0 , - 9.211339872309265 }, { 5.0 , 1000.0 , - 31.37070759780783 }, { 5.0 , 1001.0 , - 31.37569513931887 }, { 5.0 , 10000.0 , - 42.87464787956629 }, { 6.0 , 0.125 , 1.804747618429637 }, { 6.0 , 0.25 , .8559102804898941 }, { 6.0 , 0.5 , - .3027053741503954 }, { 6.0 , 1.0 , - 1.791759469228055 }, { 6.0 , 2.0 , - 3.737669618283368 }, { 6.0 , 3.0 , - 5.123963979403259 }, { 6.0 , 4.0 , - 6.222576268071369 }, { 6.0 , 5.0 , - 7.138866999945524 }, { 6.0 , 6.0 , - 7.927324360309794 }, { 6.0 , 7.0 , - 8.620471540869739 }, { 6.0 , 8.0 , - 9.239510749275963 }, { 6.0 , 9.0 , - 9.799126537211386 }, { 6.0 , 10.0 , - 10.30995216097738 }, { 6.0 , 1000.0 , - 36.67401250586691 }, { 6.0 , 1001.0 , - 36.67999457754446 }, { 6.0 , 10000.0 , - 50.47605021415003 }, { 7.0 , 0.125 , 1.784128331226902 }, { 7.0 , 0.25 , 0.815088285969639 }, { 7.0 , 0.5 , - .3827480818239319 }, { 7.0 , 1.0 , - 1.945910149055313 }, { 7.0 , 2.0 , - 4.02535169073515 }, { 7.0 , 3.0 , - 5.529429087511423 }, { 7.0 , 4.0 , - 6.733401891837359 }, { 7.0 , 5.0 , - 7.745002803515839 }, { 7.0 , 6.0 , - 8.620471540869739 }, { 7.0 , 7.0 , - 9.39366142910322 }, { 7.0 , 8.0 , - 10.08680860966317 }, { 7.0 , 9.0 , - 10.71541726908554 }, { 7.0 , 10.0 , - 11.2907814139891 }, { 7.0 , 1000.0 , - 41.79599038729854 }, { 7.0 , 1001.0 , - 41.80296600103496 }, { 7.0 , 10000.0 , - 57.89523093697012 }, { 8.0 , 0.125 , 1.766428754127501 }, { 8.0 , 0.25 , .7799969661583689 }, { 8.0 , 0.5 , - .4517409533108833 }, { 8.0 , 1.0 , - 2.079441541679836 }, { 8.0 , 2.0 , - 4.276666119016055 }, { 8.0 , 3.0 , - 5.886104031450156 }, { 8.0 , 4.0 , - 7.185387015580416 }, { 8.0 , 5.0 , - 8.283999304248526 }, { 8.0 , 6.0 , - 9.239510749275963 }, { 8.0 , 7.0 , - 10.08680860966317 }, { 8.0 , 8.0 , - 10.84894866171006 }, { 8.0 , 9.0 , - 11.54209584227001 }, { 8.0 , 10.0 , - 12.17808460899001 }, { 8.0 , 1000.0 , - 46.76481113096179 }, { 8.0 , 1001.0 , - 46.77277930061096 }, { 8.0 , 10000.0 , - 65.16036091500527 }, { 9.0 , 0.125 , 1.750924567591535 }, { 9.0 , 0.25 , .7492253074916152 }, { 9.0 , 0.5 , - .5123655751273182 }, { 9.0 , 1.0 , - 2.19722457733622 }, { 9.0 , 2.0 , - 4.499809670330265 }, { 9.0 , 3.0 , - 6.20455776256869 }, { 9.0 , 4.0 , - 7.590852123688581 }, { 9.0 , 5.0 , - 8.769507120030227 }, { 9.0 , 6.0 , - 9.799126537211386 }, { 9.0 , 7.0 , - 10.71541726908554 }, { 9.0 , 8.0 , - 11.54209584227001 }, { 9.0 , 9.0 , - 12.29586764464639 }, { 9.0 , 10.0 , - 12.98901482520633 }, { 9.0 , 1000.0 , - 51.60109303791327 }, { 9.0 , 1001.0 , - 51.61005277928474 }, { 9.0 , 10000.0 , - 72.29205942547217 }, { 10.0 , 0.125 , 1.7371312454592 }, { 10.0 , 0.25 , .7218263333035008 }, { 10.0 , 0.5 , - .5664327963975939 }, { 10.0 , 1.0 , - 2.302585092994046 }, { 10.0 , 2.0 , - 4.700480365792417 }, { 10.0 , 3.0 , - 6.492239835020471 }, { 10.0 , 4.0 , - 7.958576903813898 }, { 10.0 , 5.0 , - 9.211339872309265 }, { 10.0 , 6.0 , - 10.30995216097738 }, { 10.0 , 7.0 , - 11.2907814139891 }, { 10.0 , 8.0 , - 12.17808460899001 }, { 10.0 , 9.0 , - 12.98901482520633 }, { 10.0 , 10.0 , - 13.73622922703655 }, { 10.0 , 1000.0 , - 56.32058348093065 }, { 10.0 , 1001.0 , - 56.33053381178382 }, { 10.0 , 10000.0 , - 79.30607481535498 }, { 1000.0 , 0.125 , 1.156003642015969 }, { 1000.0 , 0.25 , - .4388225372378877 }, { 1000.0 , 0.5 , - 2.881387696571577 }, { 1000.0 , 1.0 , - 6.907755278982137 }, { 1000.0 , 2.0 , - 13.81651005829736 }, { 1000.0 , 3.0 , - 20.03311615938222 }, { 1000.0 , 4.0 , - 25.84525465867605 }, { 1000.0 , 5.0 , - 31.37070759780783 }, { 1000.0 , 6.0 , - 36.67401250586691 }, { 1000.0 , 7.0 , - 41.79599038729854 }, { 1000.0 , 8.0 , - 46.76481113096179 }, { 1000.0 , 9.0 , - 51.60109303791327 }, { 1000.0 , 10.0 , - 56.32058348093065 }, { 1000.0 , 1000.0 , - 1388.482601635902 }, { 1000.0 , 1001.0 , - 1389.175748816462 }, { 1000.0 , 10000.0 , - 3353.484270767097 }, { 1001.0 , 0.125 , 1.155878649827818 }, { 1001.0 , 0.25 , - .4390725059930951 }, { 1001.0 , 0.5 , - 2.881887571613228 }, { 1001.0 , 1.0 , - 6.90875477931522 }, { 1001.0 , 2.0 , - 13.81850806096003 }, { 1001.0 , 3.0 , - 20.03611166836202 }, { 1001.0 , 4.0 , - 25.84924667994559 }, { 1001.0 , 5.0 , - 31.37569513931887 }, { 1001.0 , 6.0 , - 36.67999457754446 }, { 1001.0 , 7.0 , - 41.80296600103496 }, { 1001.0 , 8.0 , - 46.77277930061096 }, { 1001.0 , 9.0 , - 51.61005277928474 }, { 1001.0 , 10.0 , - 56.33053381178382 }, { 1001.0 , 1000.0 , - 1389.175748816462 }, { 1001.0 , 1001.0 , - 1389.869395872064 }, { 1001.0 , 10000.0 , - 3355.882166039895 }, { 10000.0 , 0.125 , .8681312798751318 }, { 10000.0 , 0.25 , - 1.014553193217846 }, { 10000.0 , 0.5 , - 4.032792743063396 }, { 10000.0 , 1.0 , - 9.210340371976184 }, { 10000.0 , 2.0 , - 18.4207807389527 }, { 10000.0 , 3.0 , - 26.9381739103716 }, { 10000.0 , 4.0 , - 35.05020194868867 }, { 10000.0 , 5.0 , - 42.87464787956629 }, { 10000.0 , 6.0 , - 50.47605021415003 }, { 10000.0 , 7.0 , - 57.89523093697012 }, { 10000.0 , 8.0 , - 65.16036091500527 }, { 10000.0 , 9.0 , - 72.29205942547217 }, { 10000.0 , 10.0 , - 79.30607481535498 }, { 10000.0 , 1000.0 , - 3353.484270767097 }, { 10000.0 , 1001.0 , - 3355.882166039895 }, { 10000.0 , 10000.0 , - 13866.28325676141 }, }; @Test public void testLogBeta() { final int ulps = 3; for (int i = 0; i < LOG_BETA_REF.length; i++) { final double[] ref = LOG_BETA_REF[i]; final double a = ref[0]; final double b = ref[1]; final double expected = ref[2]; final double actual = Beta.logBeta(a, b); final double tol = ulps * FastMath.ulp(expected); final StringBuilder builder = new StringBuilder(); builder.append(a).append(", ").append(b); Assert.assertEquals(builder.toString(), expected, actual, tol); } }} commons-math3-3.2-src/src/test/java/org/apache/commons/math3/special/ErfTest.java100644 1750 1750 24271 12126627667 26453 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.special; import org.apache.commons.math3.TestUtils; import org.apache.commons.math3.util.FastMath; import org.junit.Test; import org.junit.Assert; /** * @version $Id: ErfTest.java 1456905 2013-03-15 11:37:35Z luc $ */ public class ErfTest { @Test public void testErf0() { double actual = Erf.erf(0.0); double expected = 0.0; Assert.assertEquals(expected, actual, 1.0e-15); Assert.assertEquals(1 - expected, Erf.erfc(0.0), 1.0e-15); } @Test public void testErf1960() { double x = 1.960 / FastMath.sqrt(2.0); double actual = Erf.erf(x); double expected = 0.95; Assert.assertEquals(expected, actual, 1.0e-5); Assert.assertEquals(1 - actual, Erf.erfc(x), 1.0e-15); actual = Erf.erf(-x); expected = -expected; Assert.assertEquals(expected, actual, 1.0e-5); Assert.assertEquals(1 - actual, Erf.erfc(-x), 1.0e-15); } @Test public void testErf2576() { double x = 2.576 / FastMath.sqrt(2.0); double actual = Erf.erf(x); double expected = 0.99; Assert.assertEquals(expected, actual, 1.0e-5); Assert.assertEquals(1 - actual, Erf.erfc(x), 1e-15); actual = Erf.erf(-x); expected = -expected; Assert.assertEquals(expected, actual, 1.0e-5); Assert.assertEquals(1 - actual, Erf.erfc(-x), 1.0e-15); } @Test public void testErf2807() { double x = 2.807 / FastMath.sqrt(2.0); double actual = Erf.erf(x); double expected = 0.995; Assert.assertEquals(expected, actual, 1.0e-5); Assert.assertEquals(1 - actual, Erf.erfc(x), 1.0e-15); actual = Erf.erf(-x); expected = -expected; Assert.assertEquals(expected, actual, 1.0e-5); Assert.assertEquals(1 - actual, Erf.erfc(-x), 1.0e-15); } @Test public void testErf3291() { double x = 3.291 / FastMath.sqrt(2.0); double actual = Erf.erf(x); double expected = 0.999; Assert.assertEquals(expected, actual, 1.0e-5); Assert.assertEquals(1 - expected, Erf.erfc(x), 1.0e-5); actual = Erf.erf(-x); expected = -expected; Assert.assertEquals(expected, actual, 1.0e-5); Assert.assertEquals(1 - expected, Erf.erfc(-x), 1.0e-5); } /** * MATH-301, MATH-456 */ @Test public void testLargeValues() { for (int i = 1; i < 200; i*=10) { double result = Erf.erf(i); Assert.assertFalse(Double.isNaN(result)); Assert.assertTrue(result > 0 && result <= 1); result = Erf.erf(-i); Assert.assertFalse(Double.isNaN(result)); Assert.assertTrue(result >= -1 && result < 0); result = Erf.erfc(i); Assert.assertFalse(Double.isNaN(result)); Assert.assertTrue(result >= 0 && result < 1); result = Erf.erfc(-i); Assert.assertFalse(Double.isNaN(result)); Assert.assertTrue(result >= 1 && result <= 2); } Assert.assertEquals(-1, Erf.erf(Double.NEGATIVE_INFINITY), 0); Assert.assertEquals(1, Erf.erf(Double.POSITIVE_INFINITY), 0); Assert.assertEquals(2, Erf.erfc(Double.NEGATIVE_INFINITY), 0); Assert.assertEquals(0, Erf.erfc(Double.POSITIVE_INFINITY), 0); } /** * Compare Erf.erf against reference values computed using GCC 4.2.1 (Apple OSX packaged version) * erfl (extended precision erf). */ @Test public void testErfGnu() { final double tol = 1E-15; final double[] gnuValues = new double[] {-1, -1, -1, -1, -1, -1, -1, -1, -0.99999999999999997848, -0.99999999999999264217, -0.99999999999846254017, -0.99999999980338395581, -0.99999998458274209971, -0.9999992569016276586, -0.99997790950300141459, -0.99959304798255504108, -0.99532226501895273415, -0.96610514647531072711, -0.84270079294971486948, -0.52049987781304653809, 0, 0.52049987781304653809, 0.84270079294971486948, 0.96610514647531072711, 0.99532226501895273415, 0.99959304798255504108, 0.99997790950300141459, 0.9999992569016276586, 0.99999998458274209971, 0.99999999980338395581, 0.99999999999846254017, 0.99999999999999264217, 0.99999999999999997848, 1, 1, 1, 1, 1, 1, 1, 1}; double x = -10d; for (int i = 0; i < 41; i++) { Assert.assertEquals(gnuValues[i], Erf.erf(x), tol); x += 0.5d; } } /** * Compare Erf.erfc against reference values computed using GCC 4.2.1 (Apple OSX packaged version) * erfcl (extended precision erfc). */ @Test public void testErfcGnu() { final double tol = 1E-15; final double[] gnuValues = new double[] { 2, 2, 2, 2, 2, 2, 2, 2, 1.9999999999999999785, 1.9999999999999926422, 1.9999999999984625402, 1.9999999998033839558, 1.9999999845827420998, 1.9999992569016276586, 1.9999779095030014146, 1.9995930479825550411, 1.9953222650189527342, 1.9661051464753107271, 1.8427007929497148695, 1.5204998778130465381, 1, 0.47950012218695346194, 0.15729920705028513051, 0.033894853524689272893, 0.0046777349810472658333, 0.00040695201744495893941, 2.2090496998585441366E-05, 7.4309837234141274516E-07, 1.5417257900280018858E-08, 1.966160441542887477E-10, 1.5374597944280348501E-12, 7.3578479179743980661E-15, 2.1519736712498913103E-17, 3.8421483271206474691E-20, 4.1838256077794144006E-23, 2.7766493860305691016E-26, 1.1224297172982927079E-29, 2.7623240713337714448E-33, 4.1370317465138102353E-37, 3.7692144856548799402E-41, 2.0884875837625447567E-45}; double x = -10d; for (int i = 0; i < 41; i++) { Assert.assertEquals(gnuValues[i], Erf.erfc(x), tol); x += 0.5d; } } /** * Tests erfc against reference data computed using Maple reported in Marsaglia, G,, * "Evaluating the Normal Distribution," Journal of Statistical Software, July, 2004. * http//www.jstatsoft.org/v11/a05/paper */ @Test public void testErfcMaple() { double[][] ref = new double[][] {{0.1, 4.60172162722971e-01}, {1.2, 1.15069670221708e-01}, {2.3, 1.07241100216758e-02}, {3.4, 3.36929265676881e-04}, {4.5, 3.39767312473006e-06}, {5.6, 1.07175902583109e-08}, {6.7, 1.04209769879652e-11}, {7.8, 3.09535877195870e-15}, {8.9, 2.79233437493966e-19}, {10.0, 7.61985302416053e-24}, {11.1, 6.27219439321703e-29}, {12.2, 1.55411978638959e-34}, {13.3, 1.15734162836904e-40}, {14.4, 2.58717592540226e-47}, {15.5, 1.73446079179387e-54}, {16.6, 3.48454651995041e-62} }; for (int i = 0; i < 15; i++) { final double result = 0.5*Erf.erfc(ref[i][0]/Math.sqrt(2)); Assert.assertEquals(ref[i][1], result, 1E-15); TestUtils.assertRelativelyEquals(ref[i][1], result, 1E-13); } } /** * Test the implementation of Erf.erf(double, double) for consistency with results * obtained from Erf.erf(double) and Erf.erfc(double). */ @Test public void testTwoArgumentErf() { double[] xi = new double[]{-2.0, -1.0, -0.9, -0.1, 0.0, 0.1, 0.9, 1.0, 2.0}; for(double x1 : xi) { for(double x2 : xi) { double a = Erf.erf(x1, x2); double b = Erf.erf(x2) - Erf.erf(x1); double c = Erf.erfc(x1) - Erf.erfc(x2); Assert.assertEquals(a, b, 1E-15); Assert.assertEquals(a, c, 1E-15); } } } @Test public void testErfInvNaN() { Assert.assertTrue(Double.isNaN(Erf.erfInv(-1.001))); Assert.assertTrue(Double.isNaN(Erf.erfInv(+1.001))); } @Test public void testErfInvInfinite() { Assert.assertTrue(Double.isInfinite(Erf.erfInv(-1))); Assert.assertTrue(Erf.erfInv(-1) < 0); Assert.assertTrue(Double.isInfinite(Erf.erfInv(+1))); Assert.assertTrue(Erf.erfInv(+1) > 0); } @Test public void testErfInv() { for (double x = -5.9; x < 5.9; x += 0.01) { final double y = Erf.erf(x); final double dydx = 2 * FastMath.exp(-x * x) / FastMath.sqrt(FastMath.PI); Assert.assertEquals(x, Erf.erfInv(y), 1.0e-15 / dydx); } } @Test public void testErfcInvNaN() { Assert.assertTrue(Double.isNaN(Erf.erfcInv(-0.001))); Assert.assertTrue(Double.isNaN(Erf.erfcInv(+2.001))); } @Test public void testErfcInvInfinite() { Assert.assertTrue(Double.isInfinite(Erf.erfcInv(-0))); Assert.assertTrue(Erf.erfcInv( 0) > 0); Assert.assertTrue(Double.isInfinite(Erf.erfcInv(+2))); Assert.assertTrue(Erf.erfcInv(+2) < 0); } @Test public void testErfcInv() { for (double x = -5.85; x < 5.9; x += 0.01) { final double y = Erf.erfc(x); final double dydxAbs = 2 * FastMath.exp(-x * x) / FastMath.sqrt(FastMath.PI); Assert.assertEquals(x, Erf.erfcInv(y), 1.0e-15 / dydxAbs); } } } commons-math3-3.2-src/src/test/java/org/apache/commons/math3/PerfTestUtils.java100644 1750 1750 25117 12126627675 26233 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3; import java.util.Random; import java.util.concurrent.Callable; import org.apache.commons.math3.stat.descriptive.SummaryStatistics; import org.apache.commons.math3.stat.descriptive.StatisticalSummary; import org.apache.commons.math3.exception.MathIllegalStateException; import org.apache.commons.math3.exception.util.LocalizedFormats; /** * Simple benchmarking utilities. */ public class PerfTestUtils { /** Nanoseconds to milliseconds conversion factor ({@value}). */ public static final double NANO_TO_MILLI = 1e-6; /** Default number of code repeat per timed block. */ private static final int DEFAULT_REPEAT_CHUNK = 1000; /** Default number of code repeats for computing the average run time. */ private static final int DEFAULT_REPEAT_STAT = 10000; /** RNG. */ private static Random rng = new Random(); /** * Timing. * * @param repeatChunk Each timing measurement will done done for that * number of repeats of the code. * @param repeatStat Timing will be averaged over that number of runs. * @param runGC Call {@code System.gc()} between each timed block. When * set to {@code true}, the test will run much slower. * @param methods Codes being timed. * @return for each of the given {@code methods}, a * {@link StatisticalSummary} of the average times (in milliseconds) * taken by a single call to the {@code call} method (i.e. the time * taken by each timed block divided by {@code repeatChunk}). */ public static StatisticalSummary[] time(int repeatChunk, int repeatStat, boolean runGC, Callable ... methods) { final double[][][] times = timesAndResults(repeatChunk, repeatStat, runGC, methods); final int len = methods.length; final StatisticalSummary[] stats = new StatisticalSummary[len]; for (int j = 0; j < len; j++) { final SummaryStatistics s = new SummaryStatistics(); for (int k = 0; k < repeatStat; k++) { s.addValue(times[j][k][0]); } stats[j] = s.getSummary(); } return stats; } /** * Timing. * * @param repeatChunk Each timing measurement will done done for that * number of repeats of the code. * @param repeatStat Timing will be averaged over that number of runs. * @param runGC Call {@code System.gc()} between each timed block. When * set to {@code true}, the test will run much slower. * @param methods Codes being timed. * @return for each of the given {@code methods} (first dimension), and * each of the {@code repeatStat} runs (second dimension): *
                            *
                          • * the average time (in milliseconds) taken by a single call to the * {@code call} method (i.e. the time taken by each timed block divided * by {@code repeatChunk}) *
                          • *
                          • * the result returned by the {@code call} method. *
                          • *
                          */ public static double[][][] timesAndResults(int repeatChunk, int repeatStat, boolean runGC, Callable ... methods) { final int numMethods = methods.length; final double[][][] timesAndResults = new double[numMethods][repeatStat][2]; try { for (int k = 0; k < repeatStat; k++) { for (int j = 0; j < numMethods; j++) { if (runGC) { // Try to perform GC outside the timed block. System.gc(); } final Callable r = methods[j]; final double[] result = new double[repeatChunk]; // Timed block. final long start = System.nanoTime(); for (int i = 0; i < repeatChunk; i++) { result[i] = r.call(); } final long stop = System.nanoTime(); // Collect run time. timesAndResults[j][k][0] = (stop - start) * NANO_TO_MILLI; // Keep track of a randomly selected result. timesAndResults[j][k][1] = result[rng.nextInt(repeatChunk)]; } } } catch (Exception e) { // Abort benchmarking if codes throw exceptions. throw new MathIllegalStateException(LocalizedFormats.SIMPLE_MESSAGE, e.getMessage()); } final double normFactor = 1d / repeatChunk; for (int j = 0; j < numMethods; j++) { for (int k = 0; k < repeatStat; k++) { timesAndResults[j][k][0] *= normFactor; } } return timesAndResults; } /** * Timing and report (to standard output) the average time and standard * deviation of a single call. * The timing is performed by calling the * {@link #time(int,int,boolean,Callable[]) time} method. * * @param title Title of the test (for the report). * @param repeatChunk Each timing measurement will done done for that * number of repeats of the code. * @param repeatStat Timing will be averaged over that number of runs. * @param runGC Call {@code System.gc()} between each timed block. When * set to {@code true}, the test will run much slower. * @param methods Codes being timed. * @return for each of the given {@code methods}, a statistics of the * average times (in milliseconds) taken by a single call to the * {@code call} method (i.e. the time taken by each timed block divided * by {@code repeatChunk}). */ public static StatisticalSummary[] timeAndReport(String title, int repeatChunk, int repeatStat, boolean runGC, RunTest ... methods) { // Header format. final String hFormat = "%s (calls per timed block: %d, timed blocks: %d, time unit: ms)"; // Width of the longest name. int nameLength = 0; for (RunTest m : methods) { int len = m.getName().length(); if (len > nameLength) { nameLength = len; } } final String nameLengthFormat = "%" + nameLength + "s"; // Column format. final String cFormat = nameLengthFormat + " %14s %14s %10s %10s %15s"; // Result format. final String format = nameLengthFormat + " %.8e %.8e %.4e %.4e % .8e"; System.out.println(String.format(hFormat, title, repeatChunk, repeatStat)); System.out.println(String.format(cFormat, "name", "time/call", "std error", "total time", "ratio", "difference")); final StatisticalSummary[] time = time(repeatChunk, repeatStat, runGC, methods); final double refSum = time[0].getSum() * repeatChunk; for (int i = 0, max = time.length; i < max; i++) { final StatisticalSummary s = time[i]; final double sum = s.getSum() * repeatChunk; System.out.println(String.format(format, methods[i].getName(), s.getMean(), s.getStandardDeviation(), sum, sum / refSum, sum - refSum)); } return time; } /** * Timing and report (to standard output). * This method calls {@link #timeAndReport(String,int,int,boolean,RunTest[]) * timeAndReport(title, 1000, 10000, false, methods)}. * * @param title Title of the test (for the report). * @param methods Codes being timed. * @return for each of the given {@code methods}, a statistics of the * average times (in milliseconds) taken by a single call to the * {@code call} method (i.e. the time taken by each timed block divided * by {@code repeatChunk}). */ public static StatisticalSummary[] timeAndReport(String title, RunTest ... methods) { return timeAndReport(title, DEFAULT_REPEAT_CHUNK, DEFAULT_REPEAT_STAT, false, methods); } /** * Utility class for storing a test label. */ public static abstract class RunTest implements Callable { private final String name; /** * @param name Test name. */ public RunTest(String name) { this.name = name; } /** * @return the name of this test. */ public String getName() { return name; } /** {@inheritDoc} */ public abstract Double call() throws Exception; } } commons-math3-3.2-src/src/test/java/org/apache/commons/math3/TestUtils.java100644 1750 1750 51206 12126627675 25414 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.text.DecimalFormat; import org.apache.commons.math3.complex.Complex; import org.apache.commons.math3.complex.ComplexFormat; import org.apache.commons.math3.distribution.RealDistribution; import org.apache.commons.math3.linear.FieldMatrix; import org.apache.commons.math3.linear.RealMatrix; import org.apache.commons.math3.linear.RealVector; import org.apache.commons.math3.stat.inference.ChiSquareTest; import org.apache.commons.math3.util.FastMath; import org.apache.commons.math3.util.Precision; import org.junit.Assert; /** * @version $Id: TestUtils.java 1363575 2012-07-19 23:02:47Z erans $ */ public class TestUtils { /** * Collection of static methods used in math unit tests. */ private TestUtils() { super(); } /** * Verifies that expected and actual are within delta, or are both NaN or * infinities of the same sign. */ public static void assertEquals(double expected, double actual, double delta) { Assert.assertEquals(null, expected, actual, delta); } /** * Verifies that expected and actual are within delta, or are both NaN or * infinities of the same sign. */ public static void assertEquals(String msg, double expected, double actual, double delta) { // check for NaN if(Double.isNaN(expected)){ Assert.assertTrue("" + actual + " is not NaN.", Double.isNaN(actual)); } else { Assert.assertEquals(msg, expected, actual, delta); } } /** * Verifies that the two arguments are exactly the same, either * both NaN or infinities of same sign, or identical floating point values. */ public static void assertSame(double expected, double actual) { Assert.assertEquals(expected, actual, 0); } /** * Verifies that real and imaginary parts of the two complex arguments * are exactly the same. Also ensures that NaN / infinite components match. */ public static void assertSame(Complex expected, Complex actual) { assertSame(expected.getReal(), actual.getReal()); assertSame(expected.getImaginary(), actual.getImaginary()); } /** * Verifies that real and imaginary parts of the two complex arguments * differ by at most delta. Also ensures that NaN / infinite components match. */ public static void assertEquals(Complex expected, Complex actual, double delta) { Assert.assertEquals(expected.getReal(), actual.getReal(), delta); Assert.assertEquals(expected.getImaginary(), actual.getImaginary(), delta); } /** * Verifies that two double arrays have equal entries, up to tolerance */ public static void assertEquals(double expected[], double observed[], double tolerance) { assertEquals("Array comparison failure", expected, observed, tolerance); } /** * Serializes an object to a bytes array and then recovers the object from the bytes array. * Returns the deserialized object. * * @param o object to serialize and recover * @return the recovered, deserialized object */ public static Object serializeAndRecover(Object o) { try { // serialize the Object ByteArrayOutputStream bos = new ByteArrayOutputStream(); ObjectOutputStream so = new ObjectOutputStream(bos); so.writeObject(o); // deserialize the Object ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray()); ObjectInputStream si = new ObjectInputStream(bis); return si.readObject(); } catch (IOException ioe) { return null; } catch (ClassNotFoundException cnfe) { return null; } } /** * Verifies that serialization preserves equals and hashCode. * Serializes the object, then recovers it and checks equals and hash code. * * @param object the object to serialize and recover */ public static void checkSerializedEquality(Object object) { Object object2 = serializeAndRecover(object); Assert.assertEquals("Equals check", object, object2); Assert.assertEquals("HashCode check", object.hashCode(), object2.hashCode()); } /** * Verifies that the relative error in actual vs. expected is less than or * equal to relativeError. If expected is infinite or NaN, actual must be * the same (NaN or infinity of the same sign). * * @param expected expected value * @param actual observed value * @param relativeError maximum allowable relative error */ public static void assertRelativelyEquals(double expected, double actual, double relativeError) { assertRelativelyEquals(null, expected, actual, relativeError); } /** * Verifies that the relative error in actual vs. expected is less than or * equal to relativeError. If expected is infinite or NaN, actual must be * the same (NaN or infinity of the same sign). * * @param msg message to return with failure * @param expected expected value * @param actual observed value * @param relativeError maximum allowable relative error */ public static void assertRelativelyEquals(String msg, double expected, double actual, double relativeError) { if (Double.isNaN(expected)) { Assert.assertTrue(msg, Double.isNaN(actual)); } else if (Double.isNaN(actual)) { Assert.assertTrue(msg, Double.isNaN(expected)); } else if (Double.isInfinite(actual) || Double.isInfinite(expected)) { Assert.assertEquals(expected, actual, relativeError); } else if (expected == 0.0) { Assert.assertEquals(msg, actual, expected, relativeError); } else { double absError = FastMath.abs(expected) * relativeError; Assert.assertEquals(msg, expected, actual, absError); } } /** * Fails iff values does not contain a number within epsilon of z. * * @param msg message to return with failure * @param values complex array to search * @param z value sought * @param epsilon tolerance */ public static void assertContains(String msg, Complex[] values, Complex z, double epsilon) { for (Complex value : values) { if (Precision.equals(value.getReal(), z.getReal(), epsilon) && Precision.equals(value.getImaginary(), z.getImaginary(), epsilon)) { return; } } Assert.fail(msg + " Unable to find " + (new ComplexFormat()).format(z)); } /** * Fails iff values does not contain a number within epsilon of z. * * @param values complex array to search * @param z value sought * @param epsilon tolerance */ public static void assertContains(Complex[] values, Complex z, double epsilon) { assertContains(null, values, z, epsilon); } /** * Fails iff values does not contain a number within epsilon of x. * * @param msg message to return with failure * @param values double array to search * @param x value sought * @param epsilon tolerance */ public static void assertContains(String msg, double[] values, double x, double epsilon) { for (double value : values) { if (Precision.equals(value, x, epsilon)) { return; } } Assert.fail(msg + " Unable to find " + x); } /** * Fails iff values does not contain a number within epsilon of x. * * @param values double array to search * @param x value sought * @param epsilon tolerance */ public static void assertContains(double[] values, double x, double epsilon) { assertContains(null, values, x, epsilon); } /** * Asserts that all entries of the specified vectors are equal to within a * positive {@code delta}. * * @param message the identifying message for the assertion error (can be * {@code null}) * @param expected expected value * @param actual actual value * @param delta the maximum difference between the entries of the expected * and actual vectors for which both entries are still considered equal */ public static void assertEquals(final String message, final double[] expected, final RealVector actual, final double delta) { final String msgAndSep = message.equals("") ? "" : message + ", "; Assert.assertEquals(msgAndSep + "dimension", expected.length, actual.getDimension()); for (int i = 0; i < expected.length; i++) { Assert.assertEquals(msgAndSep + "entry #" + i, expected[i], actual.getEntry(i), delta); } } /** * Asserts that all entries of the specified vectors are equal to within a * positive {@code delta}. * * @param message the identifying message for the assertion error (can be * {@code null}) * @param expected expected value * @param actual actual value * @param delta the maximum difference between the entries of the expected * and actual vectors for which both entries are still considered equal */ public static void assertEquals(final String message, final RealVector expected, final RealVector actual, final double delta) { final String msgAndSep = message.equals("") ? "" : message + ", "; Assert.assertEquals(msgAndSep + "dimension", expected.getDimension(), actual.getDimension()); final int dim = expected.getDimension(); for (int i = 0; i < dim; i++) { Assert.assertEquals(msgAndSep + "entry #" + i, expected.getEntry(i), actual.getEntry(i), delta); } } /** verifies that two matrices are close (1-norm) */ public static void assertEquals(String msg, RealMatrix expected, RealMatrix observed, double tolerance) { Assert.assertNotNull(msg + "\nObserved should not be null",observed); if (expected.getColumnDimension() != observed.getColumnDimension() || expected.getRowDimension() != observed.getRowDimension()) { StringBuilder messageBuffer = new StringBuilder(msg); messageBuffer.append("\nObserved has incorrect dimensions."); messageBuffer.append("\nobserved is " + observed.getRowDimension() + " x " + observed.getColumnDimension()); messageBuffer.append("\nexpected " + expected.getRowDimension() + " x " + expected.getColumnDimension()); Assert.fail(messageBuffer.toString()); } RealMatrix delta = expected.subtract(observed); if (delta.getNorm() >= tolerance) { StringBuilder messageBuffer = new StringBuilder(msg); messageBuffer.append("\nExpected: " + expected); messageBuffer.append("\nObserved: " + observed); messageBuffer.append("\nexpected - observed: " + delta); Assert.fail(messageBuffer.toString()); } } /** verifies that two matrices are equal */ public static void assertEquals(FieldMatrix> expected, FieldMatrix> observed) { Assert.assertNotNull("Observed should not be null",observed); if (expected.getColumnDimension() != observed.getColumnDimension() || expected.getRowDimension() != observed.getRowDimension()) { StringBuilder messageBuffer = new StringBuilder(); messageBuffer.append("Observed has incorrect dimensions."); messageBuffer.append("\nobserved is " + observed.getRowDimension() + " x " + observed.getColumnDimension()); messageBuffer.append("\nexpected " + expected.getRowDimension() + " x " + expected.getColumnDimension()); Assert.fail(messageBuffer.toString()); } for (int i = 0; i < expected.getRowDimension(); ++i) { for (int j = 0; j < expected.getColumnDimension(); ++j) { FieldElement eij = expected.getEntry(i, j); FieldElement oij = observed.getEntry(i, j); Assert.assertEquals(eij, oij); } } } /** verifies that two arrays are close (sup norm) */ public static void assertEquals(String msg, double[] expected, double[] observed, double tolerance) { StringBuilder out = new StringBuilder(msg); if (expected.length != observed.length) { out.append("\n Arrays not same length. \n"); out.append("expected has length "); out.append(expected.length); out.append(" observed length = "); out.append(observed.length); Assert.fail(out.toString()); } boolean failure = false; for (int i=0; i < expected.length; i++) { if (!Precision.equalsIncludingNaN(expected[i], observed[i], tolerance)) { failure = true; out.append("\n Elements at index "); out.append(i); out.append(" differ. "); out.append(" expected = "); out.append(expected[i]); out.append(" observed = "); out.append(observed[i]); } } if (failure) { Assert.fail(out.toString()); } } /** verifies that two arrays are equal */ public static > void assertEquals(T[] m, T[] n) { if (m.length != n.length) { Assert.fail("vectors not same length"); } for (int i = 0; i < m.length; i++) { Assert.assertEquals(m[i],n[i]); } } /** * Computes the sum of squared deviations of from * @param values array of deviates * @param target value to compute deviations from * * @return sum of squared deviations */ public static double sumSquareDev(double[] values, double target) { double sumsq = 0d; for (int i = 0; i < values.length; i++) { final double dev = values[i] - target; sumsq += (dev * dev); } return sumsq; } /** * Asserts the null hypothesis for a ChiSquare test. Fails and dumps arguments and test * statistics if the null hypothesis can be rejected with confidence 100 * (1 - alpha)% * * @param valueLabels labels for the values of the discrete distribution under test * @param expected expected counts * @param observed observed counts * @param alpha significance level of the test */ public static void assertChiSquareAccept(String[] valueLabels, double[] expected, long[] observed, double alpha) { ChiSquareTest chiSquareTest = new ChiSquareTest(); // Fail if we can reject null hypothesis that distributions are the same if (chiSquareTest.chiSquareTest(expected, observed, alpha)) { StringBuilder msgBuffer = new StringBuilder(); DecimalFormat df = new DecimalFormat("#.##"); msgBuffer.append("Chisquare test failed"); msgBuffer.append(" p-value = "); msgBuffer.append(chiSquareTest.chiSquareTest(expected, observed)); msgBuffer.append(" chisquare statistic = "); msgBuffer.append(chiSquareTest.chiSquare(expected, observed)); msgBuffer.append(". \n"); msgBuffer.append("value\texpected\tobserved\n"); for (int i = 0; i < expected.length; i++) { msgBuffer.append(valueLabels[i]); msgBuffer.append("\t"); msgBuffer.append(df.format(expected[i])); msgBuffer.append("\t\t"); msgBuffer.append(observed[i]); msgBuffer.append("\n"); } msgBuffer.append("This test can fail randomly due to sampling error with probability "); msgBuffer.append(alpha); msgBuffer.append("."); Assert.fail(msgBuffer.toString()); } } /** * Asserts the null hypothesis for a ChiSquare test. Fails and dumps arguments and test * statistics if the null hypothesis can be rejected with confidence 100 * (1 - alpha)% * * @param values integer values whose observed and expected counts are being compared * @param expected expected counts * @param observed observed counts * @param alpha significance level of the test */ public static void assertChiSquareAccept(int[] values, double[] expected, long[] observed, double alpha) { String[] labels = new String[values.length]; for (int i = 0; i < values.length; i++) { labels[i] = Integer.toString(values[i]); } assertChiSquareAccept(labels, expected, observed, alpha); } /** * Asserts the null hypothesis for a ChiSquare test. Fails and dumps arguments and test * statistics if the null hypothesis can be rejected with confidence 100 * (1 - alpha)% * * @param expected expected counts * @param observed observed counts * @param alpha significance level of the test */ public static void assertChiSquareAccept(double[] expected, long[] observed, double alpha) { String[] labels = new String[expected.length]; for (int i = 0; i < labels.length; i++) { labels[i] = Integer.toString(i + 1); } assertChiSquareAccept(labels, expected, observed, alpha); } /** * Computes the 25th, 50th and 75th percentiles of the given distribution and returns * these values in an array. */ public static double[] getDistributionQuartiles(RealDistribution distribution) { double[] quantiles = new double[3]; quantiles[0] = distribution.inverseCumulativeProbability(0.25d); quantiles[1] = distribution.inverseCumulativeProbability(0.5d); quantiles[2] = distribution.inverseCumulativeProbability(0.75d); return quantiles; } /** * Updates observed counts of values in quartiles. * counts[0] <-> 1st quartile ... counts[3] <-> top quartile */ public static void updateCounts(double value, long[] counts, double[] quartiles) { if (value < quartiles[0]) { counts[0]++; } else if (value > quartiles[2]) { counts[3]++; } else if (value > quartiles[1]) { counts[2]++; } else { counts[1]++; } } /** * Eliminates points with zero mass from densityPoints and densityValues parallel * arrays. Returns the number of positive mass points and collapses the arrays so * that the first elements of the input arrays represent the positive * mass points. */ public static int eliminateZeroMassPoints(int[] densityPoints, double[] densityValues) { int positiveMassCount = 0; for (int i = 0; i < densityValues.length; i++) { if (densityValues[i] > 0) { positiveMassCount++; } } if (positiveMassCount < densityValues.length) { int[] newPoints = new int[positiveMassCount]; double[] newValues = new double[positiveMassCount]; int j = 0; for (int i = 0; i < densityValues.length; i++) { if (densityValues[i] > 0) { newPoints[j] = densityPoints[i]; newValues[j] = densityValues[i]; j++; } } System.arraycopy(newPoints,0,densityPoints,0,positiveMassCount); System.arraycopy(newValues,0,densityValues,0,positiveMassCount); } return positiveMassCount; } } commons-math3-3.2-src/src/test/java/org/apache/commons/math3/linear/Array2DRowRealMatrixTest.java100644 1750 1750 131464 12126627674 31537 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.linear; import org.junit.Test; import org.junit.Assert; import org.apache.commons.math3.TestUtils; import org.apache.commons.math3.util.FastMath; import org.apache.commons.math3.exception.DimensionMismatchException; import org.apache.commons.math3.exception.NullArgumentException; import org.apache.commons.math3.exception.OutOfRangeException; import org.apache.commons.math3.exception.NoDataException; import org.apache.commons.math3.exception.NumberIsTooSmallException; import org.apache.commons.math3.exception.MathIllegalArgumentException; import org.apache.commons.math3.exception.MathIllegalStateException; /** * Test cases for the {@link Array2DRowRealMatrix} class. * * @version $Id: Array2DRowRealMatrixTest.java 1459534 2013-03-21 21:24:45Z tn $ */ public final class Array2DRowRealMatrixTest { // 3 x 3 identity matrix protected double[][] id = { {1d,0d,0d}, {0d,1d,0d}, {0d,0d,1d} }; // Test data for group operations protected double[][] testData = { {1d,2d,3d}, {2d,5d,3d}, {1d,0d,8d} }; protected double[][] testDataLU = {{2d, 5d, 3d}, {.5d, -2.5d, 6.5d}, {0.5d, 0.2d, .2d}}; protected double[][] testDataPlus2 = { {3d,4d,5d}, {4d,7d,5d}, {3d,2d,10d} }; protected double[][] testDataMinus = { {-1d,-2d,-3d}, {-2d,-5d,-3d}, {-1d,0d,-8d} }; protected double[] testDataRow1 = {1d,2d,3d}; protected double[] testDataCol3 = {3d,3d,8d}; protected double[][] testDataInv = { {-40d,16d,9d}, {13d,-5d,-3d}, {5d,-2d,-1d} }; protected double[] preMultTest = {8,12,33}; protected double[][] testData2 ={ {1d,2d,3d}, {2d,5d,3d}}; protected double[][] testData2T = { {1d,2d}, {2d,5d}, {3d,3d}}; protected double[][] testDataPlusInv = { {-39d,18d,12d}, {15d,0d,0d}, {6d,-2d,7d} }; // lu decomposition tests protected double[][] luData = { {2d,3d,3d}, {0d,5d,7d}, {6d,9d,8d} }; protected double[][] luDataLUDecomposition = { {6d,9d,8d}, {0d,5d,7d}, {0.33333333333333,0d,0.33333333333333} }; // singular matrices protected double[][] singular = { {2d,3d}, {2d,3d} }; protected double[][] bigSingular = {{1d,2d,3d,4d}, {2d,5d,3d,4d}, {7d,3d,256d,1930d}, {3d,7d,6d,8d}}; // 4th row = 1st + 2nd protected double[][] detData = { {1d,2d,3d}, {4d,5d,6d}, {7d,8d,10d} }; protected double[][] detData2 = { {1d, 3d}, {2d, 4d}}; // vectors protected double[] testVector = {1,2,3}; protected double[] testVector2 = {1,2,3,4}; // submatrix accessor tests protected double[][] subTestData = {{1, 2, 3, 4}, {1.5, 2.5, 3.5, 4.5}, {2, 4, 6, 8}, {4, 5, 6, 7}}; // array selections protected double[][] subRows02Cols13 = { {2, 4}, {4, 8}}; protected double[][] subRows03Cols12 = { {2, 3}, {5, 6}}; protected double[][] subRows03Cols123 = { {2, 3, 4} , {5, 6, 7}}; // effective permutations protected double[][] subRows20Cols123 = { {4, 6, 8} , {2, 3, 4}}; protected double[][] subRows31Cols31 = {{7, 5}, {4.5, 2.5}}; // contiguous ranges protected double[][] subRows01Cols23 = {{3,4} , {3.5, 4.5}}; protected double[][] subRows23Cols00 = {{2} , {4}}; protected double[][] subRows00Cols33 = {{4}}; // row matrices protected double[][] subRow0 = {{1,2,3,4}}; protected double[][] subRow3 = {{4,5,6,7}}; // column matrices protected double[][] subColumn1 = {{2}, {2.5}, {4}, {5}}; protected double[][] subColumn3 = {{4}, {4.5}, {8}, {7}}; // tolerances protected double entryTolerance = 10E-16; protected double normTolerance = 10E-14; protected double powerTolerance = 10E-16; /** test dimensions */ @Test public void testDimensions() { Array2DRowRealMatrix m = new Array2DRowRealMatrix(testData); Array2DRowRealMatrix m2 = new Array2DRowRealMatrix(testData2); Assert.assertEquals("testData row dimension",3,m.getRowDimension()); Assert.assertEquals("testData column dimension",3,m.getColumnDimension()); Assert.assertTrue("testData is square",m.isSquare()); Assert.assertEquals("testData2 row dimension",m2.getRowDimension(),2); Assert.assertEquals("testData2 column dimension",m2.getColumnDimension(),3); Assert.assertTrue("testData2 is not square",!m2.isSquare()); } /** test copy functions */ @Test public void testCopyFunctions() { Array2DRowRealMatrix m1 = new Array2DRowRealMatrix(testData); Array2DRowRealMatrix m2 = new Array2DRowRealMatrix(m1.getData()); Assert.assertEquals(m2,m1); Array2DRowRealMatrix m3 = new Array2DRowRealMatrix(testData); Array2DRowRealMatrix m4 = new Array2DRowRealMatrix(m3.getData(), false); Assert.assertEquals(m4,m3); } /** test add */ @Test public void testAdd() { Array2DRowRealMatrix m = new Array2DRowRealMatrix(testData); Array2DRowRealMatrix mInv = new Array2DRowRealMatrix(testDataInv); RealMatrix mPlusMInv = m.add(mInv); double[][] sumEntries = mPlusMInv.getData(); for (int row = 0; row < m.getRowDimension(); row++) { for (int col = 0; col < m.getColumnDimension(); col++) { Assert.assertEquals("sum entry entry", testDataPlusInv[row][col],sumEntries[row][col], entryTolerance); } } } /** test add failure */ @Test public void testAddFail() { Array2DRowRealMatrix m = new Array2DRowRealMatrix(testData); Array2DRowRealMatrix m2 = new Array2DRowRealMatrix(testData2); try { m.add(m2); Assert.fail("MathIllegalArgumentException expected"); } catch (MathIllegalArgumentException ex) { // ignored } } /** test norm */ @Test public void testNorm() { Array2DRowRealMatrix m = new Array2DRowRealMatrix(testData); Array2DRowRealMatrix m2 = new Array2DRowRealMatrix(testData2); Assert.assertEquals("testData norm",14d,m.getNorm(),entryTolerance); Assert.assertEquals("testData2 norm",7d,m2.getNorm(),entryTolerance); } /** test Frobenius norm */ @Test public void testFrobeniusNorm() { Array2DRowRealMatrix m = new Array2DRowRealMatrix(testData); Array2DRowRealMatrix m2 = new Array2DRowRealMatrix(testData2); Assert.assertEquals("testData Frobenius norm", FastMath.sqrt(117.0), m.getFrobeniusNorm(), entryTolerance); Assert.assertEquals("testData2 Frobenius norm", FastMath.sqrt(52.0), m2.getFrobeniusNorm(), entryTolerance); } /** test m-n = m + -n */ @Test public void testPlusMinus() { Array2DRowRealMatrix m = new Array2DRowRealMatrix(testData); Array2DRowRealMatrix m2 = new Array2DRowRealMatrix(testDataInv); TestUtils.assertEquals("m-n = m + -n",m.subtract(m2), m2.scalarMultiply(-1d).add(m),entryTolerance); try { m.subtract(new Array2DRowRealMatrix(testData2)); Assert.fail("Expecting illegalArgumentException"); } catch (MathIllegalArgumentException ex) { // ignored } } /** test multiply */ @Test public void testMultiply() { Array2DRowRealMatrix m = new Array2DRowRealMatrix(testData); Array2DRowRealMatrix mInv = new Array2DRowRealMatrix(testDataInv); Array2DRowRealMatrix identity = new Array2DRowRealMatrix(id); Array2DRowRealMatrix m2 = new Array2DRowRealMatrix(testData2); TestUtils.assertEquals("inverse multiply",m.multiply(mInv), identity,entryTolerance); TestUtils.assertEquals("inverse multiply",mInv.multiply(m), identity,entryTolerance); TestUtils.assertEquals("identity multiply",m.multiply(identity), m,entryTolerance); TestUtils.assertEquals("identity multiply",identity.multiply(mInv), mInv,entryTolerance); TestUtils.assertEquals("identity multiply",m2.multiply(identity), m2,entryTolerance); try { m.multiply(new Array2DRowRealMatrix(bigSingular)); Assert.fail("Expecting illegalArgumentException"); } catch (MathIllegalArgumentException ex) { // ignored } } //Additional Test for Array2DRowRealMatrixTest.testMultiply private double[][] d3 = new double[][] {{1,2,3,4},{5,6,7,8}}; private double[][] d4 = new double[][] {{1},{2},{3},{4}}; private double[][] d5 = new double[][] {{30},{70}}; @Test public void testMultiply2() { RealMatrix m3 = new Array2DRowRealMatrix(d3); RealMatrix m4 = new Array2DRowRealMatrix(d4); RealMatrix m5 = new Array2DRowRealMatrix(d5); TestUtils.assertEquals("m3*m4=m5", m3.multiply(m4), m5, entryTolerance); } @Test public void testPower() { Array2DRowRealMatrix m = new Array2DRowRealMatrix(testData); Array2DRowRealMatrix mInv = new Array2DRowRealMatrix(testDataInv); Array2DRowRealMatrix mPlusInv = new Array2DRowRealMatrix(testDataPlusInv); Array2DRowRealMatrix identity = new Array2DRowRealMatrix(id); TestUtils.assertEquals("m^0", m.power(0), identity, entryTolerance); TestUtils.assertEquals("mInv^0", mInv.power(0), identity, entryTolerance); TestUtils.assertEquals("mPlusInv^0", mPlusInv.power(0), identity, entryTolerance); TestUtils.assertEquals("m^1", m.power(1), m, entryTolerance); TestUtils.assertEquals("mInv^1", mInv.power(1), mInv, entryTolerance); TestUtils.assertEquals("mPlusInv^1", mPlusInv.power(1), mPlusInv, entryTolerance); RealMatrix C1 = m.copy(); RealMatrix C2 = mInv.copy(); RealMatrix C3 = mPlusInv.copy(); for (int i = 2; i <= 10; ++i) { C1 = C1.multiply(m); C2 = C2.multiply(mInv); C3 = C3.multiply(mPlusInv); TestUtils.assertEquals("m^" + i, m.power(i), C1, entryTolerance); TestUtils.assertEquals("mInv^" + i, mInv.power(i), C2, entryTolerance); TestUtils.assertEquals("mPlusInv^" + i, mPlusInv.power(i), C3, entryTolerance); } try { Array2DRowRealMatrix mNotSquare = new Array2DRowRealMatrix(testData2T); mNotSquare.power(2); Assert.fail("Expecting NonSquareMatrixException"); } catch (NonSquareMatrixException ex) { // ignored } try { m.power(-1); Assert.fail("Expecting IllegalArgumentException"); } catch (IllegalArgumentException ex) { // ignored } } /** test trace */ @Test public void testTrace() { RealMatrix m = new Array2DRowRealMatrix(id); Assert.assertEquals("identity trace",3d,m.getTrace(),entryTolerance); m = new Array2DRowRealMatrix(testData2); try { m.getTrace(); Assert.fail("Expecting NonSquareMatrixException"); } catch (NonSquareMatrixException ex) { // ignored } } /** test sclarAdd */ @Test public void testScalarAdd() { RealMatrix m = new Array2DRowRealMatrix(testData); TestUtils.assertEquals("scalar add",new Array2DRowRealMatrix(testDataPlus2), m.scalarAdd(2d),entryTolerance); } /** test operate */ @Test public void testOperate() { RealMatrix m = new Array2DRowRealMatrix(id); TestUtils.assertEquals("identity operate", testVector, m.operate(testVector), entryTolerance); TestUtils.assertEquals("identity operate", testVector, m.operate(new ArrayRealVector(testVector)).toArray(), entryTolerance); m = new Array2DRowRealMatrix(bigSingular); try { m.operate(testVector); Assert.fail("Expecting illegalArgumentException"); } catch (MathIllegalArgumentException ex) { // ignored } } /** test issue MATH-209 */ @Test public void testMath209() { RealMatrix a = new Array2DRowRealMatrix(new double[][] { { 1, 2 }, { 3, 4 }, { 5, 6 } }, false); double[] b = a.operate(new double[] { 1, 1 }); Assert.assertEquals(a.getRowDimension(), b.length); Assert.assertEquals( 3.0, b[0], 1.0e-12); Assert.assertEquals( 7.0, b[1], 1.0e-12); Assert.assertEquals(11.0, b[2], 1.0e-12); } /** test transpose */ @Test public void testTranspose() { RealMatrix m = new Array2DRowRealMatrix(testData); RealMatrix mIT = new LUDecomposition(m).getSolver().getInverse().transpose(); RealMatrix mTI = new LUDecomposition(m.transpose()).getSolver().getInverse(); TestUtils.assertEquals("inverse-transpose", mIT, mTI, normTolerance); m = new Array2DRowRealMatrix(testData2); RealMatrix mt = new Array2DRowRealMatrix(testData2T); TestUtils.assertEquals("transpose",mt,m.transpose(),normTolerance); } /** test preMultiply by vector */ @Test public void testPremultiplyVector() { RealMatrix m = new Array2DRowRealMatrix(testData); TestUtils.assertEquals("premultiply", m.preMultiply(testVector), preMultTest, normTolerance); TestUtils.assertEquals("premultiply", m.preMultiply(new ArrayRealVector(testVector).toArray()), preMultTest, normTolerance); m = new Array2DRowRealMatrix(bigSingular); try { m.preMultiply(testVector); Assert.fail("expecting MathIllegalArgumentException"); } catch (MathIllegalArgumentException ex) { // ignored } } @Test public void testPremultiply() { RealMatrix m3 = new Array2DRowRealMatrix(d3); RealMatrix m4 = new Array2DRowRealMatrix(d4); RealMatrix m5 = new Array2DRowRealMatrix(d5); TestUtils.assertEquals("m3*m4=m5", m4.preMultiply(m3), m5, entryTolerance); Array2DRowRealMatrix m = new Array2DRowRealMatrix(testData); Array2DRowRealMatrix mInv = new Array2DRowRealMatrix(testDataInv); Array2DRowRealMatrix identity = new Array2DRowRealMatrix(id); TestUtils.assertEquals("inverse multiply",m.preMultiply(mInv), identity,entryTolerance); TestUtils.assertEquals("inverse multiply",mInv.preMultiply(m), identity,entryTolerance); TestUtils.assertEquals("identity multiply",m.preMultiply(identity), m,entryTolerance); TestUtils.assertEquals("identity multiply",identity.preMultiply(mInv), mInv,entryTolerance); try { m.preMultiply(new Array2DRowRealMatrix(bigSingular)); Assert.fail("Expecting illegalArgumentException"); } catch (MathIllegalArgumentException ex) { // ignored } } @Test public void testGetVectors() { RealMatrix m = new Array2DRowRealMatrix(testData); TestUtils.assertEquals("get row",m.getRow(0),testDataRow1,entryTolerance); TestUtils.assertEquals("get col",m.getColumn(2),testDataCol3,entryTolerance); try { m.getRow(10); Assert.fail("expecting OutOfRangeException"); } catch (OutOfRangeException ex) { // ignored } try { m.getColumn(-1); Assert.fail("expecting OutOfRangeException"); } catch (OutOfRangeException ex) { // ignored } } @Test public void testGetEntry() { RealMatrix m = new Array2DRowRealMatrix(testData); Assert.assertEquals("get entry",m.getEntry(0,1),2d,entryTolerance); try { m.getEntry(10, 4); Assert.fail("Expecting OutOfRangeException"); } catch (OutOfRangeException ex) { // expected } } /** test examples in user guide */ @Test public void testExamples() { // Create a real matrix with two rows and three columns double[][] matrixData = { {1d,2d,3d}, {2d,5d,3d}}; RealMatrix m = new Array2DRowRealMatrix(matrixData); // One more with three rows, two columns double[][] matrixData2 = { {1d,2d}, {2d,5d}, {1d, 7d}}; RealMatrix n = new Array2DRowRealMatrix(matrixData2); // Now multiply m by n RealMatrix p = m.multiply(n); Assert.assertEquals(2, p.getRowDimension()); Assert.assertEquals(2, p.getColumnDimension()); // Invert p RealMatrix pInverse = new LUDecomposition(p).getSolver().getInverse(); Assert.assertEquals(2, pInverse.getRowDimension()); Assert.assertEquals(2, pInverse.getColumnDimension()); // Solve example double[][] coefficientsData = {{2, 3, -2}, {-1, 7, 6}, {4, -3, -5}}; RealMatrix coefficients = new Array2DRowRealMatrix(coefficientsData); RealVector constants = new ArrayRealVector(new double[]{1, -2, 1}, false); RealVector solution = new LUDecomposition(coefficients).getSolver().solve(constants); final double cst0 = constants.getEntry(0); final double cst1 = constants.getEntry(1); final double cst2 = constants.getEntry(2); final double sol0 = solution.getEntry(0); final double sol1 = solution.getEntry(1); final double sol2 = solution.getEntry(2); Assert.assertEquals(2 * sol0 + 3 * sol1 -2 * sol2, cst0, 1E-12); Assert.assertEquals(-1 * sol0 + 7 * sol1 + 6 * sol2, cst1, 1E-12); Assert.assertEquals(4 * sol0 - 3 * sol1 -5 * sol2, cst2, 1E-12); } // test submatrix accessors @Test public void testGetSubMatrix() { RealMatrix m = new Array2DRowRealMatrix(subTestData); checkGetSubMatrix(m, subRows23Cols00, 2 , 3 , 0, 0, false); checkGetSubMatrix(m, subRows00Cols33, 0 , 0 , 3, 3, false); checkGetSubMatrix(m, subRows01Cols23, 0 , 1 , 2, 3, false); checkGetSubMatrix(m, subRows02Cols13, new int[] { 0, 2 }, new int[] { 1, 3 }, false); checkGetSubMatrix(m, subRows03Cols12, new int[] { 0, 3 }, new int[] { 1, 2 }, false); checkGetSubMatrix(m, subRows03Cols123, new int[] { 0, 3 }, new int[] { 1, 2, 3 }, false); checkGetSubMatrix(m, subRows20Cols123, new int[] { 2, 0 }, new int[] { 1, 2, 3 }, false); checkGetSubMatrix(m, subRows31Cols31, new int[] { 3, 1 }, new int[] { 3, 1 }, false); checkGetSubMatrix(m, subRows31Cols31, new int[] { 3, 1 }, new int[] { 3, 1 }, false); checkGetSubMatrix(m, null, 1, 0, 2, 4, true); checkGetSubMatrix(m, null, -1, 1, 2, 2, true); checkGetSubMatrix(m, null, 1, 0, 2, 2, true); checkGetSubMatrix(m, null, 1, 0, 2, 4, true); checkGetSubMatrix(m, null, new int[] {}, new int[] { 0 }, true); checkGetSubMatrix(m, null, new int[] { 0 }, new int[] { 4 }, true); } private void checkGetSubMatrix(RealMatrix m, double[][] reference, int startRow, int endRow, int startColumn, int endColumn, boolean mustFail) { try { RealMatrix sub = m.getSubMatrix(startRow, endRow, startColumn, endColumn); Assert.assertEquals(new Array2DRowRealMatrix(reference), sub); if (mustFail) { Assert.fail("Expecting OutOfRangeException or NumberIsTooSmallException or NoDataException"); } } catch (OutOfRangeException e) { if (!mustFail) { throw e; } } catch (NumberIsTooSmallException e) { if (!mustFail) { throw e; } } catch (NoDataException e) { if (!mustFail) { throw e; } } } private void checkGetSubMatrix(RealMatrix m, double[][] reference, int[] selectedRows, int[] selectedColumns, boolean mustFail) { try { RealMatrix sub = m.getSubMatrix(selectedRows, selectedColumns); Assert.assertEquals(new Array2DRowRealMatrix(reference), sub); if (mustFail) { Assert.fail("Expecting OutOfRangeException or NumberIsTooSmallException or NoDataException"); } } catch (OutOfRangeException e) { if (!mustFail) { throw e; } } catch (NumberIsTooSmallException e) { if (!mustFail) { throw e; } } catch (NoDataException e) { if (!mustFail) { throw e; } } } @Test public void testCopySubMatrix() { RealMatrix m = new Array2DRowRealMatrix(subTestData); checkCopy(m, subRows23Cols00, 2 , 3 , 0, 0, false); checkCopy(m, subRows00Cols33, 0 , 0 , 3, 3, false); checkCopy(m, subRows01Cols23, 0 , 1 , 2, 3, false); checkCopy(m, subRows02Cols13, new int[] { 0, 2 }, new int[] { 1, 3 }, false); checkCopy(m, subRows03Cols12, new int[] { 0, 3 }, new int[] { 1, 2 }, false); checkCopy(m, subRows03Cols123, new int[] { 0, 3 }, new int[] { 1, 2, 3 }, false); checkCopy(m, subRows20Cols123, new int[] { 2, 0 }, new int[] { 1, 2, 3 }, false); checkCopy(m, subRows31Cols31, new int[] { 3, 1 }, new int[] { 3, 1 }, false); checkCopy(m, subRows31Cols31, new int[] { 3, 1 }, new int[] { 3, 1 }, false); checkCopy(m, null, 1, 0, 2, 4, true); checkCopy(m, null, -1, 1, 2, 2, true); checkCopy(m, null, 1, 0, 2, 2, true); checkCopy(m, null, 1, 0, 2, 4, true); checkCopy(m, null, new int[] {}, new int[] { 0 }, true); checkCopy(m, null, new int[] { 0 }, new int[] { 4 }, true); // rectangular check double[][] copy = new double[][] { { 0, 0, 0 }, { 0, 0 } }; checkCopy(m, copy, 0, 1, 0, 2, true); checkCopy(m, copy, new int[] { 0, 1 }, new int[] { 0, 1, 2 }, true); } private void checkCopy(RealMatrix m, double[][] reference, int startRow, int endRow, int startColumn, int endColumn, boolean mustFail) { try { double[][] sub = (reference == null) ? new double[1][1] : createIdenticalCopy(reference); m.copySubMatrix(startRow, endRow, startColumn, endColumn, sub); Assert.assertEquals(new Array2DRowRealMatrix(reference), new Array2DRowRealMatrix(sub)); if (mustFail) { Assert.fail("Expecting OutOfRangeException or NumberIsTooSmallException or NoDataException"); } } catch (OutOfRangeException e) { if (!mustFail) { throw e; } } catch (NumberIsTooSmallException e) { if (!mustFail) { throw e; } } catch (NoDataException e) { if (!mustFail) { throw e; } } catch (MatrixDimensionMismatchException e) { if (!mustFail) { throw e; } } } private void checkCopy(RealMatrix m, double[][] reference, int[] selectedRows, int[] selectedColumns, boolean mustFail) { try { double[][] sub = (reference == null) ? new double[1][1] : createIdenticalCopy(reference); m.copySubMatrix(selectedRows, selectedColumns, sub); Assert.assertEquals(new Array2DRowRealMatrix(reference), new Array2DRowRealMatrix(sub)); if (mustFail) { Assert.fail("Expecting OutOfRangeException or NumberIsTooSmallException or NoDataException"); } } catch (OutOfRangeException e) { if (!mustFail) { throw e; } } catch (NumberIsTooSmallException e) { if (!mustFail) { throw e; } } catch (NoDataException e) { if (!mustFail) { throw e; } } catch (MatrixDimensionMismatchException e) { if (!mustFail) { throw e; } } } private double[][] createIdenticalCopy(final double[][] matrix) { final double[][] matrixCopy = new double[matrix.length][]; for (int i = 0; i < matrixCopy.length; i++) { matrixCopy[i] = new double[matrix[i].length]; } return matrixCopy; } @Test public void testGetRowMatrix() { RealMatrix m = new Array2DRowRealMatrix(subTestData); RealMatrix mRow0 = new Array2DRowRealMatrix(subRow0); RealMatrix mRow3 = new Array2DRowRealMatrix(subRow3); Assert.assertEquals("Row0", mRow0, m.getRowMatrix(0)); Assert.assertEquals("Row3", mRow3, m.getRowMatrix(3)); try { m.getRowMatrix(-1); Assert.fail("Expecting OutOfRangeException"); } catch (OutOfRangeException ex) { // expected } try { m.getRowMatrix(4); Assert.fail("Expecting OutOfRangeException"); } catch (OutOfRangeException ex) { // expected } } @Test public void testSetRowMatrix() { RealMatrix m = new Array2DRowRealMatrix(subTestData); RealMatrix mRow3 = new Array2DRowRealMatrix(subRow3); Assert.assertNotSame(mRow3, m.getRowMatrix(0)); m.setRowMatrix(0, mRow3); Assert.assertEquals(mRow3, m.getRowMatrix(0)); try { m.setRowMatrix(-1, mRow3); Assert.fail("Expecting OutOfRangeException"); } catch (OutOfRangeException ex) { // expected } try { m.setRowMatrix(0, m); Assert.fail("Expecting MatrixDimensionMismatchException"); } catch (MatrixDimensionMismatchException ex) { // expected } } @Test public void testGetColumnMatrix() { RealMatrix m = new Array2DRowRealMatrix(subTestData); RealMatrix mColumn1 = new Array2DRowRealMatrix(subColumn1); RealMatrix mColumn3 = new Array2DRowRealMatrix(subColumn3); Assert.assertEquals("Column1", mColumn1, m.getColumnMatrix(1)); Assert.assertEquals("Column3", mColumn3, m.getColumnMatrix(3)); try { m.getColumnMatrix(-1); Assert.fail("Expecting OutOfRangeException"); } catch (OutOfRangeException ex) { // expected } try { m.getColumnMatrix(4); Assert.fail("Expecting OutOfRangeException"); } catch (OutOfRangeException ex) { // expected } } @Test public void testSetColumnMatrix() { RealMatrix m = new Array2DRowRealMatrix(subTestData); RealMatrix mColumn3 = new Array2DRowRealMatrix(subColumn3); Assert.assertNotSame(mColumn3, m.getColumnMatrix(1)); m.setColumnMatrix(1, mColumn3); Assert.assertEquals(mColumn3, m.getColumnMatrix(1)); try { m.setColumnMatrix(-1, mColumn3); Assert.fail("Expecting OutOfRangeException"); } catch (OutOfRangeException ex) { // expected } try { m.setColumnMatrix(0, m); Assert.fail("Expecting MatrixDimensionMismatchException"); } catch (MatrixDimensionMismatchException ex) { // expected } } @Test public void testGetRowVector() { RealMatrix m = new Array2DRowRealMatrix(subTestData); RealVector mRow0 = new ArrayRealVector(subRow0[0]); RealVector mRow3 = new ArrayRealVector(subRow3[0]); Assert.assertEquals("Row0", mRow0, m.getRowVector(0)); Assert.assertEquals("Row3", mRow3, m.getRowVector(3)); try { m.getRowVector(-1); Assert.fail("Expecting OutOfRangeException"); } catch (OutOfRangeException ex) { // expected } try { m.getRowVector(4); Assert.fail("Expecting OutOfRangeException"); } catch (OutOfRangeException ex) { // expected } } @Test public void testSetRowVector() { RealMatrix m = new Array2DRowRealMatrix(subTestData); RealVector mRow3 = new ArrayRealVector(subRow3[0]); Assert.assertNotSame(mRow3, m.getRowMatrix(0)); m.setRowVector(0, mRow3); Assert.assertEquals(mRow3, m.getRowVector(0)); try { m.setRowVector(-1, mRow3); Assert.fail("Expecting OutOfRangeException"); } catch (OutOfRangeException ex) { // expected } try { m.setRowVector(0, new ArrayRealVector(5)); Assert.fail("Expecting MatrixDimensionMismatchException"); } catch (MatrixDimensionMismatchException ex) { // expected } } @Test public void testGetColumnVector() { RealMatrix m = new Array2DRowRealMatrix(subTestData); RealVector mColumn1 = columnToVector(subColumn1); RealVector mColumn3 = columnToVector(subColumn3); Assert.assertEquals("Column1", mColumn1, m.getColumnVector(1)); Assert.assertEquals("Column3", mColumn3, m.getColumnVector(3)); try { m.getColumnVector(-1); Assert.fail("Expecting OutOfRangeException"); } catch (OutOfRangeException ex) { // expected } try { m.getColumnVector(4); Assert.fail("Expecting OutOfRangeException"); } catch (OutOfRangeException ex) { // expected } } @Test public void testSetColumnVector() { RealMatrix m = new Array2DRowRealMatrix(subTestData); RealVector mColumn3 = columnToVector(subColumn3); Assert.assertNotSame(mColumn3, m.getColumnVector(1)); m.setColumnVector(1, mColumn3); Assert.assertEquals(mColumn3, m.getColumnVector(1)); try { m.setColumnVector(-1, mColumn3); Assert.fail("Expecting OutOfRangeException"); } catch (OutOfRangeException ex) { // expected } try { m.setColumnVector(0, new ArrayRealVector(5)); Assert.fail("Expecting MatrixDimensionMismatchException"); } catch (MatrixDimensionMismatchException ex) { // expected } } private RealVector columnToVector(double[][] column) { double[] data = new double[column.length]; for (int i = 0; i < data.length; ++i) { data[i] = column[i][0]; } return new ArrayRealVector(data, false); } @Test public void testGetRow() { RealMatrix m = new Array2DRowRealMatrix(subTestData); checkArrays(subRow0[0], m.getRow(0)); checkArrays(subRow3[0], m.getRow(3)); try { m.getRow(-1); Assert.fail("Expecting OutOfRangeException"); } catch (OutOfRangeException ex) { // expected } try { m.getRow(4); Assert.fail("Expecting OutOfRangeException"); } catch (OutOfRangeException ex) { // expected } } @Test public void testSetRow() { RealMatrix m = new Array2DRowRealMatrix(subTestData); Assert.assertTrue(subRow3[0][0] != m.getRow(0)[0]); m.setRow(0, subRow3[0]); checkArrays(subRow3[0], m.getRow(0)); try { m.setRow(-1, subRow3[0]); Assert.fail("Expecting OutOfRangeException"); } catch (OutOfRangeException ex) { // expected } try { m.setRow(0, new double[5]); Assert.fail("Expecting MatrixDimensionMismatchException"); } catch (MatrixDimensionMismatchException ex) { // expected } } @Test public void testGetColumn() { RealMatrix m = new Array2DRowRealMatrix(subTestData); double[] mColumn1 = columnToArray(subColumn1); double[] mColumn3 = columnToArray(subColumn3); checkArrays(mColumn1, m.getColumn(1)); checkArrays(mColumn3, m.getColumn(3)); try { m.getColumn(-1); Assert.fail("Expecting OutOfRangeException"); } catch (OutOfRangeException ex) { // expected } try { m.getColumn(4); Assert.fail("Expecting OutOfRangeException"); } catch (OutOfRangeException ex) { // expected } } @Test public void testSetColumn() { RealMatrix m = new Array2DRowRealMatrix(subTestData); double[] mColumn3 = columnToArray(subColumn3); Assert.assertTrue(mColumn3[0] != m.getColumn(1)[0]); m.setColumn(1, mColumn3); checkArrays(mColumn3, m.getColumn(1)); try { m.setColumn(-1, mColumn3); Assert.fail("Expecting OutOfRangeException"); } catch (OutOfRangeException ex) { // expected } try { m.setColumn(0, new double[5]); Assert.fail("Expecting MatrixDimensionMismatchException"); } catch (MatrixDimensionMismatchException ex) { // expected } } private double[] columnToArray(double[][] column) { double[] data = new double[column.length]; for (int i = 0; i < data.length; ++i) { data[i] = column[i][0]; } return data; } private void checkArrays(double[] expected, double[] actual) { Assert.assertEquals(expected.length, actual.length); for (int i = 0; i < expected.length; ++i) { Assert.assertEquals(expected[i], actual[i], 0); } } @Test public void testEqualsAndHashCode() { Array2DRowRealMatrix m = new Array2DRowRealMatrix(testData); Array2DRowRealMatrix m1 = (Array2DRowRealMatrix) m.copy(); Array2DRowRealMatrix mt = (Array2DRowRealMatrix) m.transpose(); Assert.assertTrue(m.hashCode() != mt.hashCode()); Assert.assertEquals(m.hashCode(), m1.hashCode()); Assert.assertEquals(m, m); Assert.assertEquals(m, m1); Assert.assertFalse(m.equals(null)); Assert.assertFalse(m.equals(mt)); Assert.assertFalse(m.equals(new Array2DRowRealMatrix(bigSingular))); } @Test public void testToString() { Array2DRowRealMatrix m = new Array2DRowRealMatrix(testData); Assert.assertEquals("Array2DRowRealMatrix{{1.0,2.0,3.0},{2.0,5.0,3.0},{1.0,0.0,8.0}}", m.toString()); m = new Array2DRowRealMatrix(); Assert.assertEquals("Array2DRowRealMatrix{}", m.toString()); } @Test public void testSetSubMatrix() { Array2DRowRealMatrix m = new Array2DRowRealMatrix(testData); m.setSubMatrix(detData2,1,1); RealMatrix expected = MatrixUtils.createRealMatrix (new double[][] {{1.0,2.0,3.0},{2.0,1.0,3.0},{1.0,2.0,4.0}}); Assert.assertEquals(expected, m); m.setSubMatrix(detData2,0,0); expected = MatrixUtils.createRealMatrix (new double[][] {{1.0,3.0,3.0},{2.0,4.0,3.0},{1.0,2.0,4.0}}); Assert.assertEquals(expected, m); m.setSubMatrix(testDataPlus2,0,0); expected = MatrixUtils.createRealMatrix (new double[][] {{3.0,4.0,5.0},{4.0,7.0,5.0},{3.0,2.0,10.0}}); Assert.assertEquals(expected, m); // dimension overflow try { m.setSubMatrix(testData,1,1); Assert.fail("expecting OutOfRangeException"); } catch (OutOfRangeException e) { // expected } // dimension underflow try { m.setSubMatrix(testData,-1,1); Assert.fail("expecting OutOfRangeException"); } catch (OutOfRangeException e) { // expected } try { m.setSubMatrix(testData,1,-1); Assert.fail("expecting OutOfRangeException"); } catch (OutOfRangeException e) { // expected } // null try { m.setSubMatrix(null,1,1); Assert.fail("expecting NullArgumentException"); } catch (NullArgumentException e) { // expected } Array2DRowRealMatrix m2 = new Array2DRowRealMatrix(); try { m2.setSubMatrix(testData,0,1); Assert.fail("expecting MathIllegalStateException"); } catch (MathIllegalStateException e) { // expected } try { m2.setSubMatrix(testData,1,0); Assert.fail("expecting MathIllegalStateException"); } catch (MathIllegalStateException e) { // expected } // ragged try { m.setSubMatrix(new double[][] {{1}, {2, 3}}, 0, 0); Assert.fail("expecting MathIllegalArgumentException"); } catch (MathIllegalArgumentException e) { // expected } // empty try { m.setSubMatrix(new double[][] {{}}, 0, 0); Assert.fail("expecting MathIllegalArgumentException"); } catch (MathIllegalArgumentException e) { // expected } } @Test public void testWalk() { int rows = 150; int columns = 75; RealMatrix m = new Array2DRowRealMatrix(rows, columns); m.walkInRowOrder(new SetVisitor()); GetVisitor getVisitor = new GetVisitor(); m.walkInOptimizedOrder(getVisitor); Assert.assertEquals(rows * columns, getVisitor.getCount()); m = new Array2DRowRealMatrix(rows, columns); m.walkInRowOrder(new SetVisitor(), 1, rows - 2, 1, columns - 2); getVisitor = new GetVisitor(); m.walkInOptimizedOrder(getVisitor, 1, rows - 2, 1, columns - 2); Assert.assertEquals((rows - 2) * (columns - 2), getVisitor.getCount()); for (int i = 0; i < rows; ++i) { Assert.assertEquals(0.0, m.getEntry(i, 0), 0); Assert.assertEquals(0.0, m.getEntry(i, columns - 1), 0); } for (int j = 0; j < columns; ++j) { Assert.assertEquals(0.0, m.getEntry(0, j), 0); Assert.assertEquals(0.0, m.getEntry(rows - 1, j), 0); } m = new Array2DRowRealMatrix(rows, columns); m.walkInColumnOrder(new SetVisitor()); getVisitor = new GetVisitor(); m.walkInOptimizedOrder(getVisitor); Assert.assertEquals(rows * columns, getVisitor.getCount()); m = new Array2DRowRealMatrix(rows, columns); m.walkInColumnOrder(new SetVisitor(), 1, rows - 2, 1, columns - 2); getVisitor = new GetVisitor(); m.walkInOptimizedOrder(getVisitor, 1, rows - 2, 1, columns - 2); Assert.assertEquals((rows - 2) * (columns - 2), getVisitor.getCount()); for (int i = 0; i < rows; ++i) { Assert.assertEquals(0.0, m.getEntry(i, 0), 0); Assert.assertEquals(0.0, m.getEntry(i, columns - 1), 0); } for (int j = 0; j < columns; ++j) { Assert.assertEquals(0.0, m.getEntry(0, j), 0); Assert.assertEquals(0.0, m.getEntry(rows - 1, j), 0); } m = new Array2DRowRealMatrix(rows, columns); m.walkInOptimizedOrder(new SetVisitor()); getVisitor = new GetVisitor(); m.walkInRowOrder(getVisitor); Assert.assertEquals(rows * columns, getVisitor.getCount()); m = new Array2DRowRealMatrix(rows, columns); m.walkInOptimizedOrder(new SetVisitor(), 1, rows - 2, 1, columns - 2); getVisitor = new GetVisitor(); m.walkInRowOrder(getVisitor, 1, rows - 2, 1, columns - 2); Assert.assertEquals((rows - 2) * (columns - 2), getVisitor.getCount()); for (int i = 0; i < rows; ++i) { Assert.assertEquals(0.0, m.getEntry(i, 0), 0); Assert.assertEquals(0.0, m.getEntry(i, columns - 1), 0); } for (int j = 0; j < columns; ++j) { Assert.assertEquals(0.0, m.getEntry(0, j), 0); Assert.assertEquals(0.0, m.getEntry(rows - 1, j), 0); } m = new Array2DRowRealMatrix(rows, columns); m.walkInOptimizedOrder(new SetVisitor()); getVisitor = new GetVisitor(); m.walkInColumnOrder(getVisitor); Assert.assertEquals(rows * columns, getVisitor.getCount()); m = new Array2DRowRealMatrix(rows, columns); m.walkInOptimizedOrder(new SetVisitor(), 1, rows - 2, 1, columns - 2); getVisitor = new GetVisitor(); m.walkInColumnOrder(getVisitor, 1, rows - 2, 1, columns - 2); Assert.assertEquals((rows - 2) * (columns - 2), getVisitor.getCount()); for (int i = 0; i < rows; ++i) { Assert.assertEquals(0.0, m.getEntry(i, 0), 0); Assert.assertEquals(0.0, m.getEntry(i, columns - 1), 0); } for (int j = 0; j < columns; ++j) { Assert.assertEquals(0.0, m.getEntry(0, j), 0); Assert.assertEquals(0.0, m.getEntry(rows - 1, j), 0); } } @Test public void testSerial() { Array2DRowRealMatrix m = new Array2DRowRealMatrix(testData); Assert.assertEquals(m,TestUtils.serializeAndRecover(m)); } private static class SetVisitor extends DefaultRealMatrixChangingVisitor { @Override public double visit(int i, int j, double value) { return i + j / 1024.0; } } private static class GetVisitor extends DefaultRealMatrixPreservingVisitor { private int count = 0; @Override public void visit(int i, int j, double value) { ++count; Assert.assertEquals(i + j / 1024.0, value, 0.0); } public int getCount() { return count; } } //--------------- -----------------Protected methods /** extracts the l and u matrices from compact lu representation */ protected void splitLU(RealMatrix lu, double[][] lowerData, double[][] upperData) { if (!lu.isSquare()) { throw new NonSquareMatrixException(lu.getRowDimension(), lu.getColumnDimension()); } if (lowerData.length != lowerData[0].length) { throw new DimensionMismatchException(lowerData.length, lowerData[0].length); } if (upperData.length != upperData[0].length) { throw new DimensionMismatchException(upperData.length, upperData[0].length); } if (lowerData.length != upperData.length) { throw new DimensionMismatchException(lowerData.length, upperData.length); } if (lowerData.length != lu.getRowDimension()) { throw new DimensionMismatchException(lowerData.length, lu.getRowDimension()); } int n = lu.getRowDimension(); for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (j < i) { lowerData[i][j] = lu.getEntry(i, j); upperData[i][j] = 0d; } else if (i == j) { lowerData[i][j] = 1d; upperData[i][j] = lu.getEntry(i, j); } else { lowerData[i][j] = 0d; upperData[i][j] = lu.getEntry(i, j); } } } } /** Returns the result of applying the given row permutation to the matrix */ protected RealMatrix permuteRows(RealMatrix matrix, int[] permutation) { if (!matrix.isSquare()) { throw new NonSquareMatrixException(matrix.getRowDimension(), matrix.getColumnDimension()); } if (matrix.getRowDimension() != permutation.length) { throw new DimensionMismatchException(matrix.getRowDimension(), permutation.length); } int n = matrix.getRowDimension(); int m = matrix.getColumnDimension(); double out[][] = new double[m][n]; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { out[i][j] = matrix.getEntry(permutation[i], j); } } return new Array2DRowRealMatrix(out); } // /** Useful for debugging */ // private void dumpMatrix(RealMatrix m) { // for (int i = 0; i < m.getRowDimension(); i++) { // String os = ""; // for (int j = 0; j < m.getColumnDimension(); j++) { // os += m.getEntry(i, j) + " "; // } // System.out.println(os); // } // } } commons-math3-3.2-src/src/test/java/org/apache/commons/math3/linear/InverseHilbertMatrix.java100644 1750 1750 6603 12126627674 31020 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.linear; import org.apache.commons.math3.exception.DimensionMismatchException; import org.apache.commons.math3.util.ArithmeticUtils; /** * This class implements inverses of Hilbert Matrices as * {@link RealLinearOperator}. */ public class InverseHilbertMatrix extends RealLinearOperator { /** The size of the matrix. */ private final int n; /** * Creates a new instance of this class. * * @param n Size of the matrix to be created. */ public InverseHilbertMatrix(final int n) { this.n = n; } /** {@inheritDoc} */ @Override public int getColumnDimension() { return n; } /** * Returns the {@code (i, j)} entry of the inverse Hilbert matrix. Exact * arithmetic is used; in case of overflow, an exception is thrown. * * @param i Row index (starts at 0). * @param j Column index (starts at 0). * @return The coefficient of the inverse Hilbert matrix. */ public long getEntry(final int i, final int j) { long val = i + j + 1; long aux = ArithmeticUtils.binomialCoefficient(n + i, n - j - 1); val = ArithmeticUtils.mulAndCheck(val, aux); aux = ArithmeticUtils.binomialCoefficient(n + j, n - i - 1); val = ArithmeticUtils.mulAndCheck(val, aux); aux = ArithmeticUtils.binomialCoefficient(i + j, i); val = ArithmeticUtils.mulAndCheck(val, aux); val = ArithmeticUtils.mulAndCheck(val, aux); return ((i + j) & 1) == 0 ? val : -val; } /** {@inheritDoc} */ @Override public int getRowDimension() { return n; } /** {@inheritDoc} */ @Override public RealVector operate(final RealVector x) { if (x.getDimension() != n) { throw new DimensionMismatchException(x.getDimension(), n); } final double[] y = new double[n]; for (int i = 0; i < n; i++) { double pos = 0.; double neg = 0.; for (int j = 0; j < n; j++) { final double xj = x.getEntry(j); final long coeff = getEntry(i, j); final double daux = coeff * xj; // Positive and negative values are sorted out in order to limit // catastrophic cancellations (do not forget that Hilbert // matrices are *very* ill-conditioned! if (daux > 0.) { pos += daux; } else { neg += daux; } } y[i] = pos + neg; } return new ArrayRealVector(y, false); } } commons-math3-3.2-src/src/test/java/org/apache/commons/math3/linear/EigenDecompositionTest.java100644 1750 1750 74434 12126627674 31361 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.linear; import java.util.Arrays; import java.util.Random; import org.apache.commons.math3.distribution.NormalDistribution; import org.apache.commons.math3.util.FastMath; import org.apache.commons.math3.util.Precision; import org.apache.commons.math3.exception.MathUnsupportedOperationException; import org.junit.After; import org.junit.Assert; import org.junit.Before; import org.junit.Ignore; import org.junit.Test; public class EigenDecompositionTest { private double[] refValues; private RealMatrix matrix; @Test public void testDimension1() { RealMatrix matrix = MatrixUtils.createRealMatrix(new double[][] { { 1.5 } }); EigenDecomposition ed; ed = new EigenDecomposition(matrix); Assert.assertEquals(1.5, ed.getRealEigenvalue(0), 1.0e-15); } @Test public void testDimension2() { RealMatrix matrix = MatrixUtils.createRealMatrix(new double[][] { { 59.0, 12.0 }, { 12.0, 66.0 } }); EigenDecomposition ed; ed = new EigenDecomposition(matrix); Assert.assertEquals(75.0, ed.getRealEigenvalue(0), 1.0e-15); Assert.assertEquals(50.0, ed.getRealEigenvalue(1), 1.0e-15); } @Test public void testDimension3() { RealMatrix matrix = MatrixUtils.createRealMatrix(new double[][] { { 39632.0, -4824.0, -16560.0 }, { -4824.0, 8693.0, 7920.0 }, { -16560.0, 7920.0, 17300.0 } }); EigenDecomposition ed; ed = new EigenDecomposition(matrix); Assert.assertEquals(50000.0, ed.getRealEigenvalue(0), 3.0e-11); Assert.assertEquals(12500.0, ed.getRealEigenvalue(1), 3.0e-11); Assert.assertEquals( 3125.0, ed.getRealEigenvalue(2), 3.0e-11); } @Test public void testDimension3MultipleRoot() { RealMatrix matrix = MatrixUtils.createRealMatrix(new double[][] { { 5, 10, 15 }, { 10, 20, 30 }, { 15, 30, 45 } }); EigenDecomposition ed; ed = new EigenDecomposition(matrix); Assert.assertEquals(70.0, ed.getRealEigenvalue(0), 3.0e-11); Assert.assertEquals(0.0, ed.getRealEigenvalue(1), 3.0e-11); Assert.assertEquals(0.0, ed.getRealEigenvalue(2), 3.0e-11); } @Test public void testDimension4WithSplit() { RealMatrix matrix = MatrixUtils.createRealMatrix(new double[][] { { 0.784, -0.288, 0.000, 0.000 }, { -0.288, 0.616, 0.000, 0.000 }, { 0.000, 0.000, 0.164, -0.048 }, { 0.000, 0.000, -0.048, 0.136 } }); EigenDecomposition ed; ed = new EigenDecomposition(matrix); Assert.assertEquals(1.0, ed.getRealEigenvalue(0), 1.0e-15); Assert.assertEquals(0.4, ed.getRealEigenvalue(1), 1.0e-15); Assert.assertEquals(0.2, ed.getRealEigenvalue(2), 1.0e-15); Assert.assertEquals(0.1, ed.getRealEigenvalue(3), 1.0e-15); } @Test public void testDimension4WithoutSplit() { RealMatrix matrix = MatrixUtils.createRealMatrix(new double[][] { { 0.5608, -0.2016, 0.1152, -0.2976 }, { -0.2016, 0.4432, -0.2304, 0.1152 }, { 0.1152, -0.2304, 0.3088, -0.1344 }, { -0.2976, 0.1152, -0.1344, 0.3872 } }); EigenDecomposition ed; ed = new EigenDecomposition(matrix); Assert.assertEquals(1.0, ed.getRealEigenvalue(0), 1.0e-15); Assert.assertEquals(0.4, ed.getRealEigenvalue(1), 1.0e-15); Assert.assertEquals(0.2, ed.getRealEigenvalue(2), 1.0e-15); Assert.assertEquals(0.1, ed.getRealEigenvalue(3), 1.0e-15); } // the following test triggered an ArrayIndexOutOfBoundsException in commons-math 2.0 @Test public void testMath308() { double[] mainTridiagonal = { 22.330154644539597, 46.65485522478641, 17.393672330044705, 54.46687435351116, 80.17800767709437 }; double[] secondaryTridiagonal = { 13.04450406501361, -5.977590941539671, 2.9040909856707517, 7.1570352792841225 }; // the reference values have been computed using routine DSTEMR // from the fortran library LAPACK version 3.2.1 double[] refEigenValues = { 82.044413207204002, 53.456697699894512, 52.536278520113882, 18.847969733754262, 14.138204224043099 }; RealVector[] refEigenVectors = { new ArrayRealVector(new double[] { -0.000462690386766, -0.002118073109055, 0.011530080757413, 0.252322434584915, 0.967572088232592 }), new ArrayRealVector(new double[] { 0.314647769490148, 0.750806415553905, -0.167700312025760, -0.537092972407375, 0.143854968127780 }), new ArrayRealVector(new double[] { 0.222368839324646, 0.514921891363332, -0.021377019336614, 0.801196801016305, -0.207446991247740 }), new ArrayRealVector(new double[] { -0.713933751051495, 0.190582113553930, -0.671410443368332, 0.056056055955050, -0.006541576993581 }), new ArrayRealVector(new double[] { -0.584677060845929, 0.367177264979103, 0.721453187784497, -0.052971054621812, 0.005740715188257 }) }; EigenDecomposition decomposition; decomposition = new EigenDecomposition(mainTridiagonal, secondaryTridiagonal); double[] eigenValues = decomposition.getRealEigenvalues(); for (int i = 0; i < refEigenValues.length; ++i) { Assert.assertEquals(refEigenValues[i], eigenValues[i], 1.0e-5); Assert.assertEquals(0, refEigenVectors[i].subtract(decomposition.getEigenvector(i)).getNorm(), 2.0e-7); } } @Test public void testMathpbx02() { double[] mainTridiagonal = { 7484.860960227216, 18405.28129035345, 13855.225609560746, 10016.708722343366, 559.8117399576674, 6750.190788301587, 71.21428769782159 }; double[] secondaryTridiagonal = { -4175.088570476366,1975.7955858241994,5193.178422374075, 1995.286659169179,75.34535882933804,-234.0808002076056 }; // the reference values have been computed using routine DSTEMR // from the fortran library LAPACK version 3.2.1 double[] refEigenValues = { 20654.744890306974412,16828.208208485466457, 6893.155912634994820,6757.083016675340332, 5887.799885688558788,64.309089923240379, 57.992628792736340 }; RealVector[] refEigenVectors = { new ArrayRealVector(new double[] {-0.270356342026904, 0.852811091326997, 0.399639490702077, 0.198794657813990, 0.019739323307666, 0.000106983022327, -0.000001216636321}), new ArrayRealVector(new double[] {0.179995273578326,-0.402807848153042,0.701870993525734,0.555058211014888,0.068079148898236,0.000509139115227,-0.000007112235617}), new ArrayRealVector(new double[] {-0.399582721284727,-0.056629954519333,-0.514406488522827,0.711168164518580,0.225548081276367,0.125943999652923,-0.004321507456014}), new ArrayRealVector(new double[] {0.058515721572821,0.010200130057739,0.063516274916536,-0.090696087449378,-0.017148420432597,0.991318870265707,-0.034707338554096}), new ArrayRealVector(new double[] {0.855205995537564,0.327134656629775,-0.265382397060548,0.282690729026706,0.105736068025572,-0.009138126622039,0.000367751821196}), new ArrayRealVector(new double[] {-0.002913069901144,-0.005177515777101,0.041906334478672,-0.109315918416258,0.436192305456741,0.026307315639535,0.891797507436344}), new ArrayRealVector(new double[] {-0.005738311176435,-0.010207611670378,0.082662420517928,-0.215733886094368,0.861606487840411,-0.025478530652759,-0.451080697503958}) }; // the following line triggers the exception EigenDecomposition decomposition; decomposition = new EigenDecomposition(mainTridiagonal, secondaryTridiagonal); double[] eigenValues = decomposition.getRealEigenvalues(); for (int i = 0; i < refEigenValues.length; ++i) { Assert.assertEquals(refEigenValues[i], eigenValues[i], 1.0e-3); if (refEigenVectors[i].dotProduct(decomposition.getEigenvector(i)) < 0) { Assert.assertEquals(0, refEigenVectors[i].add(decomposition.getEigenvector(i)).getNorm(), 1.0e-5); } else { Assert.assertEquals(0, refEigenVectors[i].subtract(decomposition.getEigenvector(i)).getNorm(), 1.0e-5); } } } @Test public void testMathpbx03() { double[] mainTridiagonal = { 1809.0978259647177,3395.4763425956166,1832.1894584712693,3804.364873592377, 806.0482458637571,2403.656427234185,28.48691431556015 }; double[] secondaryTridiagonal = { -656.8932064545833,-469.30804108920734,-1021.7714889369421, -1152.540497328983,-939.9765163817368,-12.885877015422391 }; // the reference values have been computed using routine DSTEMR // from the fortran library LAPACK version 3.2.1 double[] refEigenValues = { 4603.121913685183245,3691.195818048970978,2743.442955402465032,1657.596442107321764, 1336.797819095331306,30.129865209677519,17.035352085224986 }; RealVector[] refEigenVectors = { new ArrayRealVector(new double[] {-0.036249830202337,0.154184732411519,-0.346016328392363,0.867540105133093,-0.294483395433451,0.125854235969548,-0.000354507444044}), new ArrayRealVector(new double[] {-0.318654191697157,0.912992309960507,-0.129270874079777,-0.184150038178035,0.096521712579439,-0.070468788536461,0.000247918177736}), new ArrayRealVector(new double[] {-0.051394668681147,0.073102235876933,0.173502042943743,-0.188311980310942,-0.327158794289386,0.905206581432676,-0.004296342252659}), new ArrayRealVector(new double[] {0.838150199198361,0.193305209055716,-0.457341242126146,-0.166933875895419,0.094512811358535,0.119062381338757,-0.000941755685226}), new ArrayRealVector(new double[] {0.438071395458547,0.314969169786246,0.768480630802146,0.227919171600705,-0.193317045298647,-0.170305467485594,0.001677380536009}), new ArrayRealVector(new double[] {-0.003726503878741,-0.010091946369146,-0.067152015137611,-0.113798146542187,-0.313123000097908,-0.118940107954918,0.932862311396062}), new ArrayRealVector(new double[] {0.009373003194332,0.025570377559400,0.170955836081348,0.291954519805750,0.807824267665706,0.320108347088646,0.360202112392266}), }; // the following line triggers the exception EigenDecomposition decomposition; decomposition = new EigenDecomposition(mainTridiagonal, secondaryTridiagonal); double[] eigenValues = decomposition.getRealEigenvalues(); for (int i = 0; i < refEigenValues.length; ++i) { Assert.assertEquals(refEigenValues[i], eigenValues[i], 1.0e-4); if (refEigenVectors[i].dotProduct(decomposition.getEigenvector(i)) < 0) { Assert.assertEquals(0, refEigenVectors[i].add(decomposition.getEigenvector(i)).getNorm(), 1.0e-5); } else { Assert.assertEquals(0, refEigenVectors[i].subtract(decomposition.getEigenvector(i)).getNorm(), 1.0e-5); } } } /** test a matrix already in tridiagonal form. */ @Test public void testTridiagonal() { Random r = new Random(4366663527842l); double[] ref = new double[30]; for (int i = 0; i < ref.length; ++i) { if (i < 5) { ref[i] = 2 * r.nextDouble() - 1; } else { ref[i] = 0.0001 * r.nextDouble() + 6; } } Arrays.sort(ref); TriDiagonalTransformer t = new TriDiagonalTransformer(createTestMatrix(r, ref)); EigenDecomposition ed; ed = new EigenDecomposition(t.getMainDiagonalRef(), t.getSecondaryDiagonalRef()); double[] eigenValues = ed.getRealEigenvalues(); Assert.assertEquals(ref.length, eigenValues.length); for (int i = 0; i < ref.length; ++i) { Assert.assertEquals(ref[ref.length - i - 1], eigenValues[i], 2.0e-14); } } /** test dimensions */ @Test public void testDimensions() { final int m = matrix.getRowDimension(); EigenDecomposition ed; ed = new EigenDecomposition(matrix); Assert.assertEquals(m, ed.getV().getRowDimension()); Assert.assertEquals(m, ed.getV().getColumnDimension()); Assert.assertEquals(m, ed.getD().getColumnDimension()); Assert.assertEquals(m, ed.getD().getColumnDimension()); Assert.assertEquals(m, ed.getVT().getRowDimension()); Assert.assertEquals(m, ed.getVT().getColumnDimension()); } /** test eigenvalues */ @Test public void testEigenvalues() { EigenDecomposition ed; ed = new EigenDecomposition(matrix); double[] eigenValues = ed.getRealEigenvalues(); Assert.assertEquals(refValues.length, eigenValues.length); for (int i = 0; i < refValues.length; ++i) { Assert.assertEquals(refValues[i], eigenValues[i], 3.0e-15); } } /** test eigenvalues for a big matrix. */ @Test public void testBigMatrix() { Random r = new Random(17748333525117l); double[] bigValues = new double[200]; for (int i = 0; i < bigValues.length; ++i) { bigValues[i] = 2 * r.nextDouble() - 1; } Arrays.sort(bigValues); EigenDecomposition ed; ed = new EigenDecomposition(createTestMatrix(r, bigValues)); double[] eigenValues = ed.getRealEigenvalues(); Assert.assertEquals(bigValues.length, eigenValues.length); for (int i = 0; i < bigValues.length; ++i) { Assert.assertEquals(bigValues[bigValues.length - i - 1], eigenValues[i], 2.0e-14); } } @Test public void testSymmetric() { RealMatrix symmetric = MatrixUtils.createRealMatrix(new double[][] { {4, 1, 1}, {1, 2, 3}, {1, 3, 6} }); EigenDecomposition ed; ed = new EigenDecomposition(symmetric); RealMatrix d = ed.getD(); RealMatrix v = ed.getV(); RealMatrix vT = ed.getVT(); double norm = v.multiply(d).multiply(vT).subtract(symmetric).getNorm(); Assert.assertEquals(0, norm, 6.0e-13); } @Test public void testSquareRoot() { final double[][] data = { { 33, 24, 7 }, { 24, 57, 11 }, { 7, 11, 9 } }; final EigenDecomposition dec = new EigenDecomposition(MatrixUtils.createRealMatrix(data)); final RealMatrix sqrtM = dec.getSquareRoot(); // Reconstruct initial matrix. final RealMatrix m = sqrtM.multiply(sqrtM); final int dim = data.length; for (int r = 0; r < dim; r++) { for (int c = 0; c < dim; c++) { Assert.assertEquals("m[" + r + "][" + c + "]", data[r][c], m.getEntry(r, c), 1e-13); } } } @Test(expected=MathUnsupportedOperationException.class) public void testSquareRootNonSymmetric() { final double[][] data = { { 1, 2, 4 }, { 2, 3, 5 }, { 11, 5, 9 } }; final EigenDecomposition dec = new EigenDecomposition(MatrixUtils.createRealMatrix(data)); final RealMatrix sqrtM = dec.getSquareRoot(); } @Test(expected=MathUnsupportedOperationException.class) public void testSquareRootNonPositiveDefinite() { final double[][] data = { { 1, 2, 4 }, { 2, 3, 5 }, { 4, 5, -9 } }; final EigenDecomposition dec = new EigenDecomposition(MatrixUtils.createRealMatrix(data)); final RealMatrix sqrtM = dec.getSquareRoot(); } @Test public void testUnsymmetric() { // Vandermonde matrix V(x;i,j) = x_i^{n - j} with x = (-1,-2,3,4) double[][] vData = { { -1.0, 1.0, -1.0, 1.0 }, { -8.0, 4.0, -2.0, 1.0 }, { 27.0, 9.0, 3.0, 1.0 }, { 64.0, 16.0, 4.0, 1.0 } }; checkUnsymmetricMatrix(MatrixUtils.createRealMatrix(vData)); RealMatrix randMatrix = MatrixUtils.createRealMatrix(new double[][] { {0, 1, 0, 0}, {1, 0, 2.e-7, 0}, {0, -2.e-7, 0, 1}, {0, 0, 1, 0} }); checkUnsymmetricMatrix(randMatrix); // from http://eigen.tuxfamily.org/dox/classEigen_1_1RealSchur.html double[][] randData2 = { { 0.680, -0.3300, -0.2700, -0.717, -0.687, 0.0259 }, { -0.211, 0.5360, 0.0268, 0.214, -0.198, 0.6780 }, { 0.566, -0.4440, 0.9040, -0.967, -0.740, 0.2250 }, { 0.597, 0.1080, 0.8320, -0.514, -0.782, -0.4080 }, { 0.823, -0.0452, 0.2710, -0.726, 0.998, 0.2750 }, { -0.605, 0.2580, 0.4350, 0.608, -0.563, 0.0486 } }; checkUnsymmetricMatrix(MatrixUtils.createRealMatrix(randData2)); } @Test @Ignore public void testRandomUnsymmetricMatrix() { for (int run = 0; run < 100; run++) { Random r = new Random(System.currentTimeMillis()); // matrix size int size = r.nextInt(20) + 4; double[][] data = new double[size][size]; for (int i = 0; i < size; i++) { for (int j = 0; j < size; j++) { data[i][j] = r.nextInt(100); } } RealMatrix m = MatrixUtils.createRealMatrix(data); checkUnsymmetricMatrix(m); } } @Test @Ignore public void testNormalDistributionUnsymmetricMatrix() { for (int run = 0; run < 100; run++) { Random r = new Random(System.currentTimeMillis()); NormalDistribution dist = new NormalDistribution(0.0, r.nextDouble() * 5); // matrix size int size = r.nextInt(20) + 4; double[][] data = new double[size][size]; for (int i = 0; i < size; i++) { for (int j = 0; j < size; j++) { data[i][j] = dist.sample(); } } RealMatrix m = MatrixUtils.createRealMatrix(data); checkUnsymmetricMatrix(m); } } @Test public void testMath848() { double[][] data = { { 0.1849449280, -0.0646971046, 0.0774755812, -0.0969651755, -0.0692648806, 0.3282344352, -0.0177423074, 0.2063136340}, {-0.0742700134, -0.0289063030, -0.0017269460, -0.0375550146, -0.0487737922, -0.2616837868, -0.0821201295, -0.2530000167}, { 0.2549910127, 0.0995733692, -0.0009718388, 0.0149282808, 0.1791878897, -0.0823182816, 0.0582629256, 0.3219545182}, {-0.0694747557, -0.1880649148, -0.2740630911, 0.0720096468, -0.1800836914, -0.3518996425, 0.2486747833, 0.6257938167}, { 0.0536360918, -0.1339297778, 0.2241579764, -0.0195327484, -0.0054103808, 0.0347564518, 0.5120802482, -0.0329902864}, {-0.5933332356, -0.2488721082, 0.2357173629, 0.0177285473, 0.0856630593, -0.3567126300, -0.1600668126, -0.1010899621}, {-0.0514349819, -0.0854319435, 0.1125050061, 0.0063453560, -0.2250000688, -0.2209343090, 0.1964623477, -0.1512329924}, { 0.0197395947, -0.1997170581, -0.1425959019, -0.2749477910, -0.0969467073, 0.0603688520, -0.2826905192, 0.1794315473}}; RealMatrix m = MatrixUtils.createRealMatrix(data); checkUnsymmetricMatrix(m); } /** * Checks that the eigen decomposition of a general (unsymmetric) matrix is valid by * checking: A*V = V*D */ private void checkUnsymmetricMatrix(final RealMatrix m) { try { EigenDecomposition ed = new EigenDecomposition(m); RealMatrix d = ed.getD(); RealMatrix v = ed.getV(); //RealMatrix vT = ed.getVT(); RealMatrix x = m.multiply(v); RealMatrix y = v.multiply(d); double diffNorm = x.subtract(y).getNorm(); Assert.assertTrue("The norm of (X-Y) is too large: " + diffNorm + ", matrix=" + m.toString(), x.subtract(y).getNorm() < 1000 * Precision.EPSILON * FastMath.max(x.getNorm(), y.getNorm())); RealMatrix invV = new LUDecomposition(v).getSolver().getInverse(); double norm = v.multiply(d).multiply(invV).subtract(m).getNorm(); Assert.assertEquals(0.0, norm, 1.0e-10); } catch (Exception e) { Assert.fail("Failed to create EigenDecomposition for matrix " + m.toString() + ", ex=" + e.toString()); } } /** test eigenvectors */ @Test public void testEigenvectors() { EigenDecomposition ed; ed = new EigenDecomposition(matrix); for (int i = 0; i < matrix.getRowDimension(); ++i) { double lambda = ed.getRealEigenvalue(i); RealVector v = ed.getEigenvector(i); RealVector mV = matrix.operate(v); Assert.assertEquals(0, mV.subtract(v.mapMultiplyToSelf(lambda)).getNorm(), 1.0e-13); } } /** test A = VDVt */ @Test public void testAEqualVDVt() { EigenDecomposition ed; ed = new EigenDecomposition(matrix); RealMatrix v = ed.getV(); RealMatrix d = ed.getD(); RealMatrix vT = ed.getVT(); double norm = v.multiply(d).multiply(vT).subtract(matrix).getNorm(); Assert.assertEquals(0, norm, 6.0e-13); } /** test that V is orthogonal */ @Test public void testVOrthogonal() { RealMatrix v = new EigenDecomposition(matrix).getV(); RealMatrix vTv = v.transpose().multiply(v); RealMatrix id = MatrixUtils.createRealIdentityMatrix(vTv.getRowDimension()); Assert.assertEquals(0, vTv.subtract(id).getNorm(), 2.0e-13); } /** test diagonal matrix */ @Test public void testDiagonal() { double[] diagonal = new double[] { -3.0, -2.0, 2.0, 5.0 }; RealMatrix m = MatrixUtils.createRealDiagonalMatrix(diagonal); EigenDecomposition ed; ed = new EigenDecomposition(m); Assert.assertEquals(diagonal[0], ed.getRealEigenvalue(3), 2.0e-15); Assert.assertEquals(diagonal[1], ed.getRealEigenvalue(2), 2.0e-15); Assert.assertEquals(diagonal[2], ed.getRealEigenvalue(1), 2.0e-15); Assert.assertEquals(diagonal[3], ed.getRealEigenvalue(0), 2.0e-15); } /** * Matrix with eigenvalues {8, -1, -1} */ @Test public void testRepeatedEigenvalue() { RealMatrix repeated = MatrixUtils.createRealMatrix(new double[][] { {3, 2, 4}, {2, 0, 2}, {4, 2, 3} }); EigenDecomposition ed; ed = new EigenDecomposition(repeated); checkEigenValues((new double[] {8, -1, -1}), ed, 1E-12); checkEigenVector((new double[] {2, 1, 2}), ed, 1E-12); } /** * Matrix with eigenvalues {2, 0, 12} */ @Test public void testDistinctEigenvalues() { RealMatrix distinct = MatrixUtils.createRealMatrix(new double[][] { {3, 1, -4}, {1, 3, -4}, {-4, -4, 8} }); EigenDecomposition ed; ed = new EigenDecomposition(distinct); checkEigenValues((new double[] {2, 0, 12}), ed, 1E-12); checkEigenVector((new double[] {1, -1, 0}), ed, 1E-12); checkEigenVector((new double[] {1, 1, 1}), ed, 1E-12); checkEigenVector((new double[] {-1, -1, 2}), ed, 1E-12); } /** * Verifies operation on indefinite matrix */ @Test public void testZeroDivide() { RealMatrix indefinite = MatrixUtils.createRealMatrix(new double [][] { { 0.0, 1.0, -1.0 }, { 1.0, 1.0, 0.0 }, { -1.0,0.0, 1.0 } }); EigenDecomposition ed; ed = new EigenDecomposition(indefinite); checkEigenValues((new double[] {2, 1, -1}), ed, 1E-12); double isqrt3 = 1/FastMath.sqrt(3.0); checkEigenVector((new double[] {isqrt3,isqrt3,-isqrt3}), ed, 1E-12); double isqrt2 = 1/FastMath.sqrt(2.0); checkEigenVector((new double[] {0.0,-isqrt2,-isqrt2}), ed, 1E-12); double isqrt6 = 1/FastMath.sqrt(6.0); checkEigenVector((new double[] {2*isqrt6,-isqrt6,isqrt6}), ed, 1E-12); } /** * Verifies that the given EigenDecomposition has eigenvalues equivalent to * the targetValues, ignoring the order of the values and allowing * values to differ by tolerance. */ protected void checkEigenValues(double[] targetValues, EigenDecomposition ed, double tolerance) { double[] observed = ed.getRealEigenvalues(); for (int i = 0; i < observed.length; i++) { Assert.assertTrue(isIncludedValue(observed[i], targetValues, tolerance)); Assert.assertTrue(isIncludedValue(targetValues[i], observed, tolerance)); } } /** * Returns true iff there is an entry within tolerance of value in * searchArray. */ private boolean isIncludedValue(double value, double[] searchArray, double tolerance) { boolean found = false; int i = 0; while (!found && i < searchArray.length) { if (FastMath.abs(value - searchArray[i]) < tolerance) { found = true; } i++; } return found; } /** * Returns true iff eigenVector is a scalar multiple of one of the columns * of ed.getV(). Does not try linear combinations - i.e., should only be * used to find vectors in one-dimensional eigenspaces. */ protected void checkEigenVector(double[] eigenVector, EigenDecomposition ed, double tolerance) { Assert.assertTrue(isIncludedColumn(eigenVector, ed.getV(), tolerance)); } /** * Returns true iff there is a column that is a scalar multiple of column * in searchMatrix (modulo tolerance) */ private boolean isIncludedColumn(double[] column, RealMatrix searchMatrix, double tolerance) { boolean found = false; int i = 0; while (!found && i < searchMatrix.getColumnDimension()) { double multiplier = 1.0; boolean matching = true; int j = 0; while (matching && j < searchMatrix.getRowDimension()) { double colEntry = searchMatrix.getEntry(j, i); // Use the first entry where both are non-zero as scalar if (FastMath.abs(multiplier - 1.0) <= FastMath.ulp(1.0) && FastMath.abs(colEntry) > 1E-14 && FastMath.abs(column[j]) > 1e-14) { multiplier = colEntry / column[j]; } if (FastMath.abs(column[j] * multiplier - colEntry) > tolerance) { matching = false; } j++; } found = matching; i++; } return found; } @Before public void setUp() { refValues = new double[] { 2.003, 2.002, 2.001, 1.001, 1.000, 0.001 }; matrix = createTestMatrix(new Random(35992629946426l), refValues); } @After public void tearDown() { refValues = null; matrix = null; } static RealMatrix createTestMatrix(final Random r, final double[] eigenValues) { final int n = eigenValues.length; final RealMatrix v = createOrthogonalMatrix(r, n); final RealMatrix d = MatrixUtils.createRealDiagonalMatrix(eigenValues); return v.multiply(d).multiply(v.transpose()); } public static RealMatrix createOrthogonalMatrix(final Random r, final int size) { final double[][] data = new double[size][size]; for (int i = 0; i < size; ++i) { final double[] dataI = data[i]; double norm2 = 0; do { // generate randomly row I for (int j = 0; j < size; ++j) { dataI[j] = 2 * r.nextDouble() - 1; } // project the row in the subspace orthogonal to previous rows for (int k = 0; k < i; ++k) { final double[] dataK = data[k]; double dotProduct = 0; for (int j = 0; j < size; ++j) { dotProduct += dataI[j] * dataK[j]; } for (int j = 0; j < size; ++j) { dataI[j] -= dotProduct * dataK[j]; } } // normalize the row norm2 = 0; for (final double dataIJ : dataI) { norm2 += dataIJ * dataIJ; } final double inv = 1.0 / FastMath.sqrt(norm2); for (int j = 0; j < size; ++j) { dataI[j] *= inv; } } while (norm2 * size < 0.01); } return MatrixUtils.createRealMatrix(data); } } commons-math3-3.2-src/src/test/java/org/apache/commons/math3/linear/SparseFieldMatrixTest.java100644 1750 1750 101053 12126627674 31167 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.linear; import org.junit.Test; import org.junit.Assert; import org.apache.commons.math3.Field; import org.apache.commons.math3.fraction.Fraction; import org.apache.commons.math3.fraction.FractionConversionException; import org.apache.commons.math3.fraction.FractionField; import org.apache.commons.math3.exception.NoDataException; import org.apache.commons.math3.exception.OutOfRangeException; import org.apache.commons.math3.exception.NumberIsTooSmallException; import org.apache.commons.math3.exception.NullArgumentException; import org.apache.commons.math3.exception.MathIllegalArgumentException; /** * Test cases for the {@link SparseFieldMatrix} class. * * @version $Id: SparseFieldMatrixTest.java 1364030 2012-07-21 01:10:04Z erans $ */ public class SparseFieldMatrixTest { // 3 x 3 identity matrix protected Fraction[][] id = { {new Fraction(1), new Fraction(0), new Fraction(0) }, { new Fraction(0), new Fraction(1), new Fraction(0) }, { new Fraction(0), new Fraction(0), new Fraction(1) } }; // Test data for group operations protected Fraction[][] testData = { { new Fraction(1), new Fraction(2), new Fraction(3) }, { new Fraction(2), new Fraction(5), new Fraction(3) }, { new Fraction(1), new Fraction(0), new Fraction(8) } }; protected Fraction[][] testDataLU = null; protected Fraction[][] testDataPlus2 = { { new Fraction(3), new Fraction(4), new Fraction(5) }, { new Fraction(4), new Fraction(7), new Fraction(5) }, { new Fraction(3), new Fraction(2), new Fraction(10) } }; protected Fraction[][] testDataMinus = { { new Fraction(-1), new Fraction(-2), new Fraction(-3) }, { new Fraction(-2), new Fraction(-5), new Fraction(-3) }, { new Fraction(-1), new Fraction(0), new Fraction(-8) } }; protected Fraction[] testDataRow1 = { new Fraction(1), new Fraction(2), new Fraction(3) }; protected Fraction[] testDataCol3 = { new Fraction(3), new Fraction(3), new Fraction(8) }; protected Fraction[][] testDataInv = { { new Fraction(-40), new Fraction(16), new Fraction(9) }, { new Fraction(13), new Fraction(-5), new Fraction(-3) }, { new Fraction(5), new Fraction(-2), new Fraction(-1) } }; protected Fraction[] preMultTest = { new Fraction(8), new Fraction(12), new Fraction(33) }; protected Fraction[][] testData2 = { { new Fraction(1), new Fraction(2), new Fraction(3) }, { new Fraction(2), new Fraction(5), new Fraction(3) } }; protected Fraction[][] testData2T = { { new Fraction(1), new Fraction(2) }, { new Fraction(2), new Fraction(5) }, { new Fraction(3), new Fraction(3) } }; protected Fraction[][] testDataPlusInv = { { new Fraction(-39), new Fraction(18), new Fraction(12) }, { new Fraction(15), new Fraction(0), new Fraction(0) }, { new Fraction(6), new Fraction(-2), new Fraction(7) } }; // lu decomposition tests protected Fraction[][] luData = { { new Fraction(2), new Fraction(3), new Fraction(3) }, { new Fraction(0), new Fraction(5), new Fraction(7) }, { new Fraction(6), new Fraction(9), new Fraction(8) } }; protected Fraction[][] luDataLUDecomposition = null; // singular matrices protected Fraction[][] singular = { { new Fraction(2), new Fraction(3) }, { new Fraction(2), new Fraction(3) } }; protected Fraction[][] bigSingular = { { new Fraction(1), new Fraction(2), new Fraction(3), new Fraction(4) }, { new Fraction(2), new Fraction(5), new Fraction(3), new Fraction(4) }, { new Fraction(7), new Fraction(3), new Fraction(256), new Fraction(1930) }, { new Fraction(3), new Fraction(7), new Fraction(6), new Fraction(8) } }; // 4th // row // = // 1st // + // 2nd protected Fraction[][] detData = { { new Fraction(1), new Fraction(2), new Fraction(3) }, { new Fraction(4), new Fraction(5), new Fraction(6) }, { new Fraction(7), new Fraction(8), new Fraction(10) } }; protected Fraction[][] detData2 = { { new Fraction(1), new Fraction(3) }, { new Fraction(2), new Fraction(4) } }; // vectors protected Fraction[] testVector = { new Fraction(1), new Fraction(2), new Fraction(3) }; protected Fraction[] testVector2 = { new Fraction(1), new Fraction(2), new Fraction(3), new Fraction(4) }; // submatrix accessor tests protected Fraction[][] subTestData = null; // array selections protected Fraction[][] subRows02Cols13 = { {new Fraction(2), new Fraction(4) }, { new Fraction(4), new Fraction(8) } }; protected Fraction[][] subRows03Cols12 = { { new Fraction(2), new Fraction(3) }, { new Fraction(5), new Fraction(6) } }; protected Fraction[][] subRows03Cols123 = { { new Fraction(2), new Fraction(3), new Fraction(4) }, { new Fraction(5), new Fraction(6), new Fraction(7) } }; // effective permutations protected Fraction[][] subRows20Cols123 = { { new Fraction(4), new Fraction(6), new Fraction(8) }, { new Fraction(2), new Fraction(3), new Fraction(4) } }; protected Fraction[][] subRows31Cols31 = null; // contiguous ranges protected Fraction[][] subRows01Cols23 = null; protected Fraction[][] subRows23Cols00 = { { new Fraction(2) }, { new Fraction(4) } }; protected Fraction[][] subRows00Cols33 = { { new Fraction(4) } }; // row matrices protected Fraction[][] subRow0 = { { new Fraction(1), new Fraction(2), new Fraction(3), new Fraction(4) } }; protected Fraction[][] subRow3 = { { new Fraction(4), new Fraction(5), new Fraction(6), new Fraction(7) } }; // column matrices protected Fraction[][] subColumn1 = null; protected Fraction[][] subColumn3 = null; // tolerances protected double entryTolerance = 10E-16; protected double normTolerance = 10E-14; protected Field field = FractionField.getInstance(); public SparseFieldMatrixTest() { try { testDataLU = new Fraction[][]{ { new Fraction(2), new Fraction(5), new Fraction(3) }, { new Fraction(.5d), new Fraction(-2.5d), new Fraction(6.5d) }, { new Fraction(0.5d), new Fraction(0.2d), new Fraction(.2d) } }; luDataLUDecomposition = new Fraction[][]{ { new Fraction(6), new Fraction(9), new Fraction(8) }, { new Fraction(0), new Fraction(5), new Fraction(7) }, { new Fraction(0.33333333333333), new Fraction(0), new Fraction(0.33333333333333) } }; subTestData = new Fraction [][]{ { new Fraction(1), new Fraction(2), new Fraction(3), new Fraction(4) }, { new Fraction(1.5), new Fraction(2.5), new Fraction(3.5), new Fraction(4.5) }, { new Fraction(2), new Fraction(4), new Fraction(6), new Fraction(8) }, { new Fraction(4), new Fraction(5), new Fraction(6), new Fraction(7) } }; subRows31Cols31 = new Fraction[][]{ { new Fraction(7), new Fraction(5) }, { new Fraction(4.5), new Fraction(2.5) } }; subRows01Cols23 = new Fraction[][]{ { new Fraction(3), new Fraction(4) }, { new Fraction(3.5), new Fraction(4.5) } }; subColumn1 = new Fraction [][]{ { new Fraction(2) }, { new Fraction(2.5) }, { new Fraction(4) }, { new Fraction(5) } }; subColumn3 = new Fraction[][]{ { new Fraction(4) }, { new Fraction(4.5) }, { new Fraction(8) }, { new Fraction(7) } }; } catch (FractionConversionException e) { // ignore, can't happen } } /** test dimensions */ @Test public void testDimensions() { SparseFieldMatrix m = createSparseMatrix(testData); SparseFieldMatrix m2 = createSparseMatrix(testData2); Assert.assertEquals("testData row dimension", 3, m.getRowDimension()); Assert.assertEquals("testData column dimension", 3, m.getColumnDimension()); Assert.assertTrue("testData is square", m.isSquare()); Assert.assertEquals("testData2 row dimension", m2.getRowDimension(), 2); Assert.assertEquals("testData2 column dimension", m2.getColumnDimension(), 3); Assert.assertTrue("testData2 is not square", !m2.isSquare()); } /** test copy functions */ @Test public void testCopyFunctions() { SparseFieldMatrix m1 = createSparseMatrix(testData); FieldMatrix m2 = m1.copy(); Assert.assertEquals(m1.getClass(), m2.getClass()); Assert.assertEquals((m2), m1); SparseFieldMatrix m3 = createSparseMatrix(testData); FieldMatrix m4 = m3.copy(); Assert.assertEquals(m3.getClass(), m4.getClass()); Assert.assertEquals((m4), m3); } /** test add */ @Test public void testAdd() { SparseFieldMatrix m = createSparseMatrix(testData); SparseFieldMatrix mInv = createSparseMatrix(testDataInv); SparseFieldMatrix mDataPlusInv = createSparseMatrix(testDataPlusInv); FieldMatrix mPlusMInv = m.add(mInv); for (int row = 0; row < m.getRowDimension(); row++) { for (int col = 0; col < m.getColumnDimension(); col++) { Assert.assertEquals("sum entry entry", mDataPlusInv.getEntry(row, col).doubleValue(), mPlusMInv.getEntry(row, col).doubleValue(), entryTolerance); } } } /** test add failure */ @Test public void testAddFail() { SparseFieldMatrix m = createSparseMatrix(testData); SparseFieldMatrix m2 = createSparseMatrix(testData2); try { m.add(m2); Assert.fail("MathIllegalArgumentException expected"); } catch (MathIllegalArgumentException ex) { // ignored } } /** test m-n = m + -n */ @Test public void testPlusMinus() { SparseFieldMatrix m = createSparseMatrix(testData); SparseFieldMatrix n = createSparseMatrix(testDataInv); assertClose("m-n = m + -n", m.subtract(n), n.scalarMultiply(new Fraction(-1)).add(m), entryTolerance); try { m.subtract(createSparseMatrix(testData2)); Assert.fail("Expecting illegalArgumentException"); } catch (MathIllegalArgumentException ex) { // ignored } } /** test multiply */ @Test public void testMultiply() { SparseFieldMatrix m = createSparseMatrix(testData); SparseFieldMatrix mInv = createSparseMatrix(testDataInv); SparseFieldMatrix identity = createSparseMatrix(id); SparseFieldMatrix m2 = createSparseMatrix(testData2); assertClose("inverse multiply", m.multiply(mInv), identity, entryTolerance); assertClose("inverse multiply", m.multiply(new Array2DRowFieldMatrix(FractionField.getInstance(), testDataInv)), identity, entryTolerance); assertClose("inverse multiply", mInv.multiply(m), identity, entryTolerance); assertClose("identity multiply", m.multiply(identity), m, entryTolerance); assertClose("identity multiply", identity.multiply(mInv), mInv, entryTolerance); assertClose("identity multiply", m2.multiply(identity), m2, entryTolerance); try { m.multiply(createSparseMatrix(bigSingular)); Assert.fail("Expecting illegalArgumentException"); } catch (MathIllegalArgumentException ex) { // ignored } } // Additional Test for Array2DRowRealMatrixTest.testMultiply private Fraction[][] d3 = new Fraction[][] { { new Fraction(1), new Fraction(2), new Fraction(3), new Fraction(4) }, { new Fraction(5), new Fraction(6), new Fraction(7), new Fraction(8) } }; private Fraction[][] d4 = new Fraction[][] { { new Fraction(1) }, { new Fraction(2) }, { new Fraction(3) }, { new Fraction(4) } }; private Fraction[][] d5 = new Fraction[][] { { new Fraction(30) }, { new Fraction(70) } }; @Test public void testMultiply2() { FieldMatrix m3 = createSparseMatrix(d3); FieldMatrix m4 = createSparseMatrix(d4); FieldMatrix m5 = createSparseMatrix(d5); assertClose("m3*m4=m5", m3.multiply(m4), m5, entryTolerance); } /** test trace */ @Test public void testTrace() { FieldMatrix m = createSparseMatrix(id); Assert.assertEquals("identity trace", 3d, m.getTrace().doubleValue(), entryTolerance); m = createSparseMatrix(testData2); try { m.getTrace(); Assert.fail("Expecting NonSquareMatrixException"); } catch (NonSquareMatrixException ex) { // ignored } } /** test sclarAdd */ @Test public void testScalarAdd() { FieldMatrix m = createSparseMatrix(testData); assertClose("scalar add", createSparseMatrix(testDataPlus2), m.scalarAdd(new Fraction(2)), entryTolerance); } /** test operate */ @Test public void testOperate() { FieldMatrix m = createSparseMatrix(id); assertClose("identity operate", testVector, m.operate(testVector), entryTolerance); assertClose("identity operate", testVector, m.operate( new ArrayFieldVector(testVector)).getData(), entryTolerance); m = createSparseMatrix(bigSingular); try { m.operate(testVector); Assert.fail("Expecting illegalArgumentException"); } catch (MathIllegalArgumentException ex) { // ignored } } /** test issue MATH-209 */ @Test public void testMath209() { FieldMatrix a = createSparseMatrix(new Fraction[][] { { new Fraction(1), new Fraction(2) }, { new Fraction(3), new Fraction(4) }, { new Fraction(5), new Fraction(6) } }); Fraction[] b = a.operate(new Fraction[] { new Fraction(1), new Fraction(1) }); Assert.assertEquals(a.getRowDimension(), b.length); Assert.assertEquals(3.0, b[0].doubleValue(), 1.0e-12); Assert.assertEquals(7.0, b[1].doubleValue(), 1.0e-12); Assert.assertEquals(11.0, b[2].doubleValue(), 1.0e-12); } /** test transpose */ @Test public void testTranspose() { FieldMatrix m = createSparseMatrix(testData); FieldMatrix mIT = new FieldLUDecomposition(m).getSolver().getInverse().transpose(); FieldMatrix mTI = new FieldLUDecomposition(m.transpose()).getSolver().getInverse(); assertClose("inverse-transpose", mIT, mTI, normTolerance); m = createSparseMatrix(testData2); FieldMatrix mt = createSparseMatrix(testData2T); assertClose("transpose",mt,m.transpose(),normTolerance); } /** test preMultiply by vector */ @Test public void testPremultiplyVector() { FieldMatrix m = createSparseMatrix(testData); assertClose("premultiply", m.preMultiply(testVector), preMultTest, normTolerance); assertClose("premultiply", m.preMultiply( new ArrayFieldVector(testVector).getData()), preMultTest, normTolerance); m = createSparseMatrix(bigSingular); try { m.preMultiply(testVector); Assert.fail("expecting MathIllegalArgumentException"); } catch (MathIllegalArgumentException ex) { // ignored } } @Test public void testPremultiply() { FieldMatrix m3 = createSparseMatrix(d3); FieldMatrix m4 = createSparseMatrix(d4); FieldMatrix m5 = createSparseMatrix(d5); assertClose("m3*m4=m5", m4.preMultiply(m3), m5, entryTolerance); SparseFieldMatrix m = createSparseMatrix(testData); SparseFieldMatrix mInv = createSparseMatrix(testDataInv); SparseFieldMatrix identity = createSparseMatrix(id); assertClose("inverse multiply", m.preMultiply(mInv), identity, entryTolerance); assertClose("inverse multiply", mInv.preMultiply(m), identity, entryTolerance); assertClose("identity multiply", m.preMultiply(identity), m, entryTolerance); assertClose("identity multiply", identity.preMultiply(mInv), mInv, entryTolerance); try { m.preMultiply(createSparseMatrix(bigSingular)); Assert.fail("Expecting illegalArgumentException"); } catch (MathIllegalArgumentException ex) { // ignored } } @Test public void testGetVectors() { FieldMatrix m = createSparseMatrix(testData); assertClose("get row", m.getRow(0), testDataRow1, entryTolerance); assertClose("get col", m.getColumn(2), testDataCol3, entryTolerance); try { m.getRow(10); Assert.fail("expecting OutOfRangeException"); } catch (OutOfRangeException ex) { // ignored } try { m.getColumn(-1); Assert.fail("expecting OutOfRangeException"); } catch (OutOfRangeException ex) { // ignored } } @Test public void testGetEntry() { FieldMatrix m = createSparseMatrix(testData); Assert.assertEquals("get entry", m.getEntry(0, 1).doubleValue(), 2d, entryTolerance); try { m.getEntry(10, 4); Assert.fail("Expecting OutOfRangeException"); } catch (OutOfRangeException ex) { // expected } } /** test examples in user guide */ @Test public void testExamples() { // Create a real matrix with two rows and three columns Fraction[][] matrixData = { { new Fraction(1), new Fraction(2), new Fraction(3) }, { new Fraction(2), new Fraction(5), new Fraction(3) } }; FieldMatrix m = createSparseMatrix(matrixData); // One more with three rows, two columns Fraction[][] matrixData2 = { { new Fraction(1), new Fraction(2) }, { new Fraction(2), new Fraction(5) }, { new Fraction(1), new Fraction(7) } }; FieldMatrix n = createSparseMatrix(matrixData2); // Now multiply m by n FieldMatrix p = m.multiply(n); Assert.assertEquals(2, p.getRowDimension()); Assert.assertEquals(2, p.getColumnDimension()); // Invert p FieldMatrix pInverse = new FieldLUDecomposition(p).getSolver().getInverse(); Assert.assertEquals(2, pInverse.getRowDimension()); Assert.assertEquals(2, pInverse.getColumnDimension()); // Solve example Fraction[][] coefficientsData = { { new Fraction(2), new Fraction(3), new Fraction(-2) }, { new Fraction(-1), new Fraction(7), new Fraction(6) }, { new Fraction(4), new Fraction(-3), new Fraction(-5) } }; FieldMatrix coefficients = createSparseMatrix(coefficientsData); Fraction[] constants = { new Fraction(1), new Fraction(-2), new Fraction(1) }; Fraction[] solution; solution = new FieldLUDecomposition(coefficients) .getSolver() .solve(new ArrayFieldVector(constants, false)).toArray(); Assert.assertEquals((new Fraction(2).multiply((solution[0])).add(new Fraction(3).multiply(solution[1])).subtract(new Fraction(2).multiply(solution[2]))).doubleValue(), constants[0].doubleValue(), 1E-12); Assert.assertEquals(((new Fraction(-1).multiply(solution[0])).add(new Fraction(7).multiply(solution[1])).add(new Fraction(6).multiply(solution[2]))).doubleValue(), constants[1].doubleValue(), 1E-12); Assert.assertEquals(((new Fraction(4).multiply(solution[0])).subtract(new Fraction(3).multiply( solution[1])).subtract(new Fraction(5).multiply(solution[2]))).doubleValue(), constants[2].doubleValue(), 1E-12); } // test submatrix accessors @Test public void testSubMatrix() { FieldMatrix m = createSparseMatrix(subTestData); FieldMatrix mRows23Cols00 = createSparseMatrix(subRows23Cols00); FieldMatrix mRows00Cols33 = createSparseMatrix(subRows00Cols33); FieldMatrix mRows01Cols23 = createSparseMatrix(subRows01Cols23); FieldMatrix mRows02Cols13 = createSparseMatrix(subRows02Cols13); FieldMatrix mRows03Cols12 = createSparseMatrix(subRows03Cols12); FieldMatrix mRows03Cols123 = createSparseMatrix(subRows03Cols123); FieldMatrix mRows20Cols123 = createSparseMatrix(subRows20Cols123); FieldMatrix mRows31Cols31 = createSparseMatrix(subRows31Cols31); Assert.assertEquals("Rows23Cols00", mRows23Cols00, m.getSubMatrix(2, 3, 0, 0)); Assert.assertEquals("Rows00Cols33", mRows00Cols33, m.getSubMatrix(0, 0, 3, 3)); Assert.assertEquals("Rows01Cols23", mRows01Cols23, m.getSubMatrix(0, 1, 2, 3)); Assert.assertEquals("Rows02Cols13", mRows02Cols13, m.getSubMatrix(new int[] { 0, 2 }, new int[] { 1, 3 })); Assert.assertEquals("Rows03Cols12", mRows03Cols12, m.getSubMatrix(new int[] { 0, 3 }, new int[] { 1, 2 })); Assert.assertEquals("Rows03Cols123", mRows03Cols123, m.getSubMatrix(new int[] { 0, 3 }, new int[] { 1, 2, 3 })); Assert.assertEquals("Rows20Cols123", mRows20Cols123, m.getSubMatrix(new int[] { 2, 0 }, new int[] { 1, 2, 3 })); Assert.assertEquals("Rows31Cols31", mRows31Cols31, m.getSubMatrix(new int[] { 3, 1 }, new int[] { 3, 1 })); Assert.assertEquals("Rows31Cols31", mRows31Cols31, m.getSubMatrix(new int[] { 3, 1 }, new int[] { 3, 1 })); try { m.getSubMatrix(1, 0, 2, 4); Assert.fail("Expecting NumberIsTooSmallException"); } catch (NumberIsTooSmallException ex) { // expected } try { m.getSubMatrix(-1, 1, 2, 2); Assert.fail("Expecting OutOfRangeException"); } catch (OutOfRangeException ex) { // expected } try { m.getSubMatrix(1, 0, 2, 2); Assert.fail("Expecting NumberIsTooSmallException"); } catch (NumberIsTooSmallException ex) { // expected } try { m.getSubMatrix(1, 0, 2, 4); Assert.fail("Expecting NumberIsTooSmallException"); } catch (NumberIsTooSmallException ex) { // expected } try { m.getSubMatrix(new int[] {}, new int[] { 0 }); Assert.fail("Expecting NoDataException"); } catch (NoDataException ex) { // expected } try { m.getSubMatrix(new int[] { 0 }, new int[] { 4 }); Assert.fail("Expecting OutOfRangeException"); } catch (OutOfRangeException ex) { // expected } } @Test public void testGetRowMatrix() { FieldMatrix m = createSparseMatrix(subTestData); FieldMatrix mRow0 = createSparseMatrix(subRow0); FieldMatrix mRow3 = createSparseMatrix(subRow3); Assert.assertEquals("Row0", mRow0, m.getRowMatrix(0)); Assert.assertEquals("Row3", mRow3, m.getRowMatrix(3)); try { m.getRowMatrix(-1); Assert.fail("Expecting OutOfRangeException"); } catch (OutOfRangeException ex) { // expected } try { m.getRowMatrix(4); Assert.fail("Expecting OutOfRangeException"); } catch (OutOfRangeException ex) { // expected } } @Test public void testGetColumnMatrix() { FieldMatrix m = createSparseMatrix(subTestData); FieldMatrix mColumn1 = createSparseMatrix(subColumn1); FieldMatrix mColumn3 = createSparseMatrix(subColumn3); Assert.assertEquals("Column1", mColumn1, m.getColumnMatrix(1)); Assert.assertEquals("Column3", mColumn3, m.getColumnMatrix(3)); try { m.getColumnMatrix(-1); Assert.fail("Expecting OutOfRangeException"); } catch (OutOfRangeException ex) { // expected } try { m.getColumnMatrix(4); Assert.fail("Expecting OutOfRangeException"); } catch (OutOfRangeException ex) { // expected } } @Test public void testGetRowVector() { FieldMatrix m = createSparseMatrix(subTestData); FieldVector mRow0 = new ArrayFieldVector(subRow0[0]); FieldVector mRow3 = new ArrayFieldVector(subRow3[0]); Assert.assertEquals("Row0", mRow0, m.getRowVector(0)); Assert.assertEquals("Row3", mRow3, m.getRowVector(3)); try { m.getRowVector(-1); Assert.fail("Expecting OutOfRangeException"); } catch (OutOfRangeException ex) { // expected } try { m.getRowVector(4); Assert.fail("Expecting OutOfRangeException"); } catch (OutOfRangeException ex) { // expected } } @Test public void testGetColumnVector() { FieldMatrix m = createSparseMatrix(subTestData); FieldVector mColumn1 = columnToVector(subColumn1); FieldVector mColumn3 = columnToVector(subColumn3); Assert.assertEquals("Column1", mColumn1, m.getColumnVector(1)); Assert.assertEquals("Column3", mColumn3, m.getColumnVector(3)); try { m.getColumnVector(-1); Assert.fail("Expecting OutOfRangeException"); } catch (OutOfRangeException ex) { // expected } try { m.getColumnVector(4); Assert.fail("Expecting OutOfRangeException"); } catch (OutOfRangeException ex) { // expected } } private FieldVector columnToVector(Fraction[][] column) { Fraction[] data = new Fraction[column.length]; for (int i = 0; i < data.length; ++i) { data[i] = column[i][0]; } return new ArrayFieldVector(data, false); } @Test public void testEqualsAndHashCode() { SparseFieldMatrix m = createSparseMatrix(testData); SparseFieldMatrix m1 = (SparseFieldMatrix) m.copy(); SparseFieldMatrix mt = (SparseFieldMatrix) m.transpose(); Assert.assertTrue(m.hashCode() != mt.hashCode()); Assert.assertEquals(m.hashCode(), m1.hashCode()); Assert.assertEquals(m, m); Assert.assertEquals(m, m1); Assert.assertFalse(m.equals(null)); Assert.assertFalse(m.equals(mt)); Assert.assertFalse(m.equals(createSparseMatrix(bigSingular))); } /* Disable for now @Test public void testToString() { SparseFieldMatrix m = createSparseMatrix(testData); Assert.assertEquals("SparseFieldMatrix{{1.0,2.0,3.0},{2.0,5.0,3.0},{1.0,0.0,8.0}}", m.toString()); m = new SparseFieldMatrix(field, 1, 1); Assert.assertEquals("SparseFieldMatrix{{0.0}}", m.toString()); } */ @Test public void testSetSubMatrix() { SparseFieldMatrix m = createSparseMatrix(testData); m.setSubMatrix(detData2, 1, 1); FieldMatrix expected = createSparseMatrix(new Fraction[][] { { new Fraction(1), new Fraction(2), new Fraction(3) }, { new Fraction(2), new Fraction(1), new Fraction(3) }, { new Fraction(1), new Fraction(2), new Fraction(4) } }); Assert.assertEquals(expected, m); m.setSubMatrix(detData2, 0, 0); expected = createSparseMatrix(new Fraction[][] { { new Fraction(1), new Fraction(3), new Fraction(3) }, { new Fraction(2), new Fraction(4), new Fraction(3) }, { new Fraction(1), new Fraction(2), new Fraction(4) } }); Assert.assertEquals(expected, m); m.setSubMatrix(testDataPlus2, 0, 0); expected = createSparseMatrix(new Fraction[][] { { new Fraction(3), new Fraction(4), new Fraction(5) }, { new Fraction(4), new Fraction(7), new Fraction(5) }, { new Fraction(3), new Fraction(2), new Fraction(10) } }); Assert.assertEquals(expected, m); // javadoc example SparseFieldMatrix matrix = createSparseMatrix(new Fraction[][] { { new Fraction(1), new Fraction(2), new Fraction(3), new Fraction(4) }, { new Fraction(5), new Fraction(6), new Fraction(7), new Fraction(8) }, { new Fraction(9), new Fraction(0), new Fraction(1), new Fraction(2) } }); matrix.setSubMatrix(new Fraction[][] { { new Fraction(3), new Fraction(4) }, { new Fraction(5), new Fraction(6) } }, 1, 1); expected = createSparseMatrix(new Fraction[][] { { new Fraction(1), new Fraction(2), new Fraction(3), new Fraction(4) }, { new Fraction(5), new Fraction(3), new Fraction(4), new Fraction(8) }, { new Fraction(9), new Fraction(5), new Fraction(6), new Fraction(2) } }); Assert.assertEquals(expected, matrix); // dimension overflow try { m.setSubMatrix(testData, 1, 1); Assert.fail("expecting OutOfRangeException"); } catch (OutOfRangeException e) { // expected } // dimension underflow try { m.setSubMatrix(testData, -1, 1); Assert.fail("expecting OutOfRangeException"); } catch (OutOfRangeException e) { // expected } try { m.setSubMatrix(testData, 1, -1); Assert.fail("expecting OutOfRangeException"); } catch (OutOfRangeException e) { // expected } // null try { m.setSubMatrix(null, 1, 1); Assert.fail("expecting NullArgumentException"); } catch (NullArgumentException e) { // expected } try { new SparseFieldMatrix(field, 0, 0); Assert.fail("expecting MathIllegalArgumentException"); } catch (MathIllegalArgumentException e) { // expected } // ragged try { m.setSubMatrix(new Fraction[][] { { new Fraction(1) }, { new Fraction(2), new Fraction(3) } }, 0, 0); Assert.fail("expecting MathIllegalArgumentException"); } catch (MathIllegalArgumentException e) { // expected } // empty try { m.setSubMatrix(new Fraction[][] { {} }, 0, 0); Assert.fail("expecting MathIllegalArgumentException"); } catch (MathIllegalArgumentException e) { // expected } } // --------------- -----------------Protected methods /** verifies that two matrices are close (1-norm) */ protected void assertClose(String msg, FieldMatrix m, FieldMatrix n, double tolerance) { for(int i=0; i < m.getRowDimension(); i++){ for(int j=0; j < m.getColumnDimension(); j++){ Assert.assertEquals(msg, m.getEntry(i,j).doubleValue(), n.getEntry(i,j).doubleValue(), tolerance); } } } /** verifies that two vectors are close (sup norm) */ protected void assertClose(String msg, Fraction[] m, Fraction[] n, double tolerance) { if (m.length != n.length) { Assert.fail("vectors not same length"); } for (int i = 0; i < m.length; i++) { Assert.assertEquals(msg + " " + i + " elements differ", m[i].doubleValue(), n[i].doubleValue(), tolerance); } } private SparseFieldMatrix createSparseMatrix(Fraction[][] data) { SparseFieldMatrix matrix = new SparseFieldMatrix(field, data.length, data[0].length); for (int row = 0; row < data.length; row++) { for (int col = 0; col < data[row].length; col++) { matrix.setEntry(row, col, data[row][col]); } } return matrix; } } commons-math3-3.2-src/src/test/java/org/apache/commons/math3/linear/RealVectorFormatTest.java100644 1750 1750 2132 12126627674 30756 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.linear; import java.util.Locale; public class RealVectorFormatTest extends RealVectorFormatAbstractTest { @Override protected char getDecimalCharacter() { return '.'; } @Override protected Locale getLocale() { return Locale.US; } } commons-math3-3.2-src/src/test/java/org/apache/commons/math3/linear/SparseRealMatrixTest.java100644 1750 1750 65047 12126627674 31023 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.linear; import org.junit.Test; import org.junit.Assert; import org.apache.commons.math3.TestUtils; import org.apache.commons.math3.exception.NullArgumentException; import org.apache.commons.math3.exception.OutOfRangeException; import org.apache.commons.math3.exception.NoDataException; import org.apache.commons.math3.exception.NumberIsTooSmallException; import org.apache.commons.math3.exception.MathIllegalArgumentException; /** * Test cases for the {@link OpenMapRealMatrix} class. * * @version $Id: SparseRealMatrixTest.java 1364668 2012-07-23 15:07:39Z erans $ */ public final class SparseRealMatrixTest { // 3 x 3 identity matrix protected double[][] id = { { 1d, 0d, 0d }, { 0d, 1d, 0d }, { 0d, 0d, 1d } }; // Test data for group operations protected double[][] testData = { { 1d, 2d, 3d }, { 2d, 5d, 3d }, { 1d, 0d, 8d } }; protected double[][] testDataLU = { { 2d, 5d, 3d }, { .5d, -2.5d, 6.5d }, { 0.5d, 0.2d, .2d } }; protected double[][] testDataPlus2 = { { 3d, 4d, 5d }, { 4d, 7d, 5d }, { 3d, 2d, 10d } }; protected double[][] testDataMinus = { { -1d, -2d, -3d }, { -2d, -5d, -3d }, { -1d, 0d, -8d } }; protected double[] testDataRow1 = { 1d, 2d, 3d }; protected double[] testDataCol3 = { 3d, 3d, 8d }; protected double[][] testDataInv = { { -40d, 16d, 9d }, { 13d, -5d, -3d }, { 5d, -2d, -1d } }; protected double[] preMultTest = { 8, 12, 33 }; protected double[][] testData2 = { { 1d, 2d, 3d }, { 2d, 5d, 3d } }; protected double[][] testData2T = { { 1d, 2d }, { 2d, 5d }, { 3d, 3d } }; protected double[][] testDataPlusInv = { { -39d, 18d, 12d }, { 15d, 0d, 0d }, { 6d, -2d, 7d } }; // lu decomposition tests protected double[][] luData = { { 2d, 3d, 3d }, { 0d, 5d, 7d }, { 6d, 9d, 8d } }; protected double[][] luDataLUDecomposition = { { 6d, 9d, 8d }, { 0d, 5d, 7d }, { 0.33333333333333, 0d, 0.33333333333333 } }; // singular matrices protected double[][] singular = { { 2d, 3d }, { 2d, 3d } }; protected double[][] bigSingular = { { 1d, 2d, 3d, 4d }, { 2d, 5d, 3d, 4d }, { 7d, 3d, 256d, 1930d }, { 3d, 7d, 6d, 8d } }; // 4th // row // = // 1st // + // 2nd protected double[][] detData = { { 1d, 2d, 3d }, { 4d, 5d, 6d }, { 7d, 8d, 10d } }; protected double[][] detData2 = { { 1d, 3d }, { 2d, 4d } }; // vectors protected double[] testVector = { 1, 2, 3 }; protected double[] testVector2 = { 1, 2, 3, 4 }; // submatrix accessor tests protected double[][] subTestData = { { 1, 2, 3, 4 }, { 1.5, 2.5, 3.5, 4.5 }, { 2, 4, 6, 8 }, { 4, 5, 6, 7 } }; // array selections protected double[][] subRows02Cols13 = { { 2, 4 }, { 4, 8 } }; protected double[][] subRows03Cols12 = { { 2, 3 }, { 5, 6 } }; protected double[][] subRows03Cols123 = { { 2, 3, 4 }, { 5, 6, 7 } }; // effective permutations protected double[][] subRows20Cols123 = { { 4, 6, 8 }, { 2, 3, 4 } }; protected double[][] subRows31Cols31 = { { 7, 5 }, { 4.5, 2.5 } }; // contiguous ranges protected double[][] subRows01Cols23 = { { 3, 4 }, { 3.5, 4.5 } }; protected double[][] subRows23Cols00 = { { 2 }, { 4 } }; protected double[][] subRows00Cols33 = { { 4 } }; // row matrices protected double[][] subRow0 = { { 1, 2, 3, 4 } }; protected double[][] subRow3 = { { 4, 5, 6, 7 } }; // column matrices protected double[][] subColumn1 = { { 2 }, { 2.5 }, { 4 }, { 5 } }; protected double[][] subColumn3 = { { 4 }, { 4.5 }, { 8 }, { 7 } }; // tolerances protected double entryTolerance = 10E-16; protected double normTolerance = 10E-14; /** test dimensions */ @Test public void testDimensions() { OpenMapRealMatrix m = createSparseMatrix(testData); OpenMapRealMatrix m2 = createSparseMatrix(testData2); Assert.assertEquals("testData row dimension", 3, m.getRowDimension()); Assert.assertEquals("testData column dimension", 3, m.getColumnDimension()); Assert.assertTrue("testData is square", m.isSquare()); Assert.assertEquals("testData2 row dimension", m2.getRowDimension(), 2); Assert.assertEquals("testData2 column dimension", m2.getColumnDimension(), 3); Assert.assertTrue("testData2 is not square", !m2.isSquare()); } /** test copy functions */ @Test public void testCopyFunctions() { OpenMapRealMatrix m1 = createSparseMatrix(testData); RealMatrix m2 = m1.copy(); Assert.assertEquals(m1.getClass(), m2.getClass()); Assert.assertEquals((m2), m1); OpenMapRealMatrix m3 = createSparseMatrix(testData); RealMatrix m4 = m3.copy(); Assert.assertEquals(m3.getClass(), m4.getClass()); Assert.assertEquals((m4), m3); } /** test add */ @Test public void testAdd() { OpenMapRealMatrix m = createSparseMatrix(testData); OpenMapRealMatrix mInv = createSparseMatrix(testDataInv); OpenMapRealMatrix mDataPlusInv = createSparseMatrix(testDataPlusInv); RealMatrix mPlusMInv = m.add(mInv); for (int row = 0; row < m.getRowDimension(); row++) { for (int col = 0; col < m.getColumnDimension(); col++) { Assert.assertEquals("sum entry entry", mDataPlusInv.getEntry(row, col), mPlusMInv.getEntry(row, col), entryTolerance); } } } /** test add failure */ @Test public void testAddFail() { OpenMapRealMatrix m = createSparseMatrix(testData); OpenMapRealMatrix m2 = createSparseMatrix(testData2); try { m.add(m2); Assert.fail("MathIllegalArgumentException expected"); } catch (MathIllegalArgumentException ex) { // ignored } } /** test norm */ @Test public void testNorm() { OpenMapRealMatrix m = createSparseMatrix(testData); OpenMapRealMatrix m2 = createSparseMatrix(testData2); Assert.assertEquals("testData norm", 14d, m.getNorm(), entryTolerance); Assert.assertEquals("testData2 norm", 7d, m2.getNorm(), entryTolerance); } /** test m-n = m + -n */ @Test public void testPlusMinus() { OpenMapRealMatrix m = createSparseMatrix(testData); OpenMapRealMatrix n = createSparseMatrix(testDataInv); assertClose("m-n = m + -n", m.subtract(n), n.scalarMultiply(-1d).add(m), entryTolerance); try { m.subtract(createSparseMatrix(testData2)); Assert.fail("Expecting illegalArgumentException"); } catch (MathIllegalArgumentException ex) { // ignored } } /** test multiply */ @Test public void testMultiply() { OpenMapRealMatrix m = createSparseMatrix(testData); OpenMapRealMatrix mInv = createSparseMatrix(testDataInv); OpenMapRealMatrix identity = createSparseMatrix(id); OpenMapRealMatrix m2 = createSparseMatrix(testData2); assertClose("inverse multiply", m.multiply(mInv), identity, entryTolerance); assertClose("inverse multiply", m.multiply(new BlockRealMatrix(testDataInv)), identity, entryTolerance); assertClose("inverse multiply", mInv.multiply(m), identity, entryTolerance); assertClose("identity multiply", m.multiply(identity), m, entryTolerance); assertClose("identity multiply", identity.multiply(mInv), mInv, entryTolerance); assertClose("identity multiply", m2.multiply(identity), m2, entryTolerance); try { m.multiply(createSparseMatrix(bigSingular)); Assert.fail("Expecting illegalArgumentException"); } catch (MathIllegalArgumentException ex) { // ignored } } // Additional Test for Array2DRowRealMatrixTest.testMultiply private double[][] d3 = new double[][] { { 1, 2, 3, 4 }, { 5, 6, 7, 8 } }; private double[][] d4 = new double[][] { { 1 }, { 2 }, { 3 }, { 4 } }; private double[][] d5 = new double[][] { { 30 }, { 70 } }; @Test public void testMultiply2() { RealMatrix m3 = createSparseMatrix(d3); RealMatrix m4 = createSparseMatrix(d4); RealMatrix m5 = createSparseMatrix(d5); assertClose("m3*m4=m5", m3.multiply(m4), m5, entryTolerance); } /** test trace */ @Test public void testTrace() { RealMatrix m = createSparseMatrix(id); Assert.assertEquals("identity trace", 3d, m.getTrace(), entryTolerance); m = createSparseMatrix(testData2); try { m.getTrace(); Assert.fail("Expecting NonSquareMatrixException"); } catch (NonSquareMatrixException ex) { // ignored } } /** test sclarAdd */ @Test public void testScalarAdd() { RealMatrix m = createSparseMatrix(testData); assertClose("scalar add", createSparseMatrix(testDataPlus2), m.scalarAdd(2d), entryTolerance); } /** test operate */ @Test public void testOperate() { RealMatrix m = createSparseMatrix(id); assertClose("identity operate", testVector, m.operate(testVector), entryTolerance); assertClose("identity operate", testVector, m.operate( new ArrayRealVector(testVector)).toArray(), entryTolerance); m = createSparseMatrix(bigSingular); try { m.operate(testVector); Assert.fail("Expecting illegalArgumentException"); } catch (MathIllegalArgumentException ex) { // ignored } } /** test issue MATH-209 */ @Test public void testMath209() { RealMatrix a = createSparseMatrix(new double[][] { { 1, 2 }, { 3, 4 }, { 5, 6 } }); double[] b = a.operate(new double[] { 1, 1 }); Assert.assertEquals(a.getRowDimension(), b.length); Assert.assertEquals(3.0, b[0], 1.0e-12); Assert.assertEquals(7.0, b[1], 1.0e-12); Assert.assertEquals(11.0, b[2], 1.0e-12); } /** test transpose */ @Test public void testTranspose() { RealMatrix m = createSparseMatrix(testData); RealMatrix mIT = new LUDecomposition(m).getSolver().getInverse().transpose(); RealMatrix mTI = new LUDecomposition(m.transpose()).getSolver().getInverse(); assertClose("inverse-transpose", mIT, mTI, normTolerance); m = createSparseMatrix(testData2); RealMatrix mt = createSparseMatrix(testData2T); assertClose("transpose",mt,m.transpose(),normTolerance); } /** test preMultiply by vector */ @Test public void testPremultiplyVector() { RealMatrix m = createSparseMatrix(testData); assertClose("premultiply", m.preMultiply(testVector), preMultTest, normTolerance); assertClose("premultiply", m.preMultiply( new ArrayRealVector(testVector).toArray()), preMultTest, normTolerance); m = createSparseMatrix(bigSingular); try { m.preMultiply(testVector); Assert.fail("expecting MathIllegalArgumentException"); } catch (MathIllegalArgumentException ex) { // ignored } } @Test public void testPremultiply() { RealMatrix m3 = createSparseMatrix(d3); RealMatrix m4 = createSparseMatrix(d4); RealMatrix m5 = createSparseMatrix(d5); assertClose("m3*m4=m5", m4.preMultiply(m3), m5, entryTolerance); OpenMapRealMatrix m = createSparseMatrix(testData); OpenMapRealMatrix mInv = createSparseMatrix(testDataInv); OpenMapRealMatrix identity = createSparseMatrix(id); assertClose("inverse multiply", m.preMultiply(mInv), identity, entryTolerance); assertClose("inverse multiply", mInv.preMultiply(m), identity, entryTolerance); assertClose("identity multiply", m.preMultiply(identity), m, entryTolerance); assertClose("identity multiply", identity.preMultiply(mInv), mInv, entryTolerance); try { m.preMultiply(createSparseMatrix(bigSingular)); Assert.fail("Expecting illegalArgumentException"); } catch (MathIllegalArgumentException ex) { // ignored } } @Test public void testGetVectors() { RealMatrix m = createSparseMatrix(testData); assertClose("get row", m.getRow(0), testDataRow1, entryTolerance); assertClose("get col", m.getColumn(2), testDataCol3, entryTolerance); try { m.getRow(10); Assert.fail("expecting OutOfRangeException"); } catch (OutOfRangeException ex) { // ignored } try { m.getColumn(-1); Assert.fail("expecting OutOfRangeException"); } catch (OutOfRangeException ex) { // ignored } } @Test public void testGetEntry() { RealMatrix m = createSparseMatrix(testData); Assert.assertEquals("get entry", m.getEntry(0, 1), 2d, entryTolerance); try { m.getEntry(10, 4); Assert.fail("Expecting OutOfRangeException"); } catch (OutOfRangeException ex) { // expected } } /** test examples in user guide */ @Test public void testExamples() { // Create a real matrix with two rows and three columns double[][] matrixData = { { 1d, 2d, 3d }, { 2d, 5d, 3d } }; RealMatrix m = createSparseMatrix(matrixData); // One more with three rows, two columns double[][] matrixData2 = { { 1d, 2d }, { 2d, 5d }, { 1d, 7d } }; RealMatrix n = createSparseMatrix(matrixData2); // Now multiply m by n RealMatrix p = m.multiply(n); Assert.assertEquals(2, p.getRowDimension()); Assert.assertEquals(2, p.getColumnDimension()); // Invert p RealMatrix pInverse = new LUDecomposition(p).getSolver().getInverse(); Assert.assertEquals(2, pInverse.getRowDimension()); Assert.assertEquals(2, pInverse.getColumnDimension()); // Solve example double[][] coefficientsData = { { 2, 3, -2 }, { -1, 7, 6 }, { 4, -3, -5 } }; RealMatrix coefficients = createSparseMatrix(coefficientsData); RealVector constants = new ArrayRealVector(new double[]{ 1, -2, 1 }, false); RealVector solution = new LUDecomposition(coefficients).getSolver().solve(constants); final double cst0 = constants.getEntry(0); final double cst1 = constants.getEntry(1); final double cst2 = constants.getEntry(2); final double sol0 = solution.getEntry(0); final double sol1 = solution.getEntry(1); final double sol2 = solution.getEntry(2); Assert.assertEquals(2 * sol0 + 3 * sol1 - 2 * sol2, cst0, 1E-12); Assert.assertEquals(-1 * sol0 + 7 * sol1 + 6 * sol2, cst1, 1E-12); Assert.assertEquals(4 * sol0 - 3 * sol1 - 5 * sol2, cst2, 1E-12); } // test submatrix accessors @Test public void testSubMatrix() { RealMatrix m = createSparseMatrix(subTestData); RealMatrix mRows23Cols00 = createSparseMatrix(subRows23Cols00); RealMatrix mRows00Cols33 = createSparseMatrix(subRows00Cols33); RealMatrix mRows01Cols23 = createSparseMatrix(subRows01Cols23); RealMatrix mRows02Cols13 = createSparseMatrix(subRows02Cols13); RealMatrix mRows03Cols12 = createSparseMatrix(subRows03Cols12); RealMatrix mRows03Cols123 = createSparseMatrix(subRows03Cols123); RealMatrix mRows20Cols123 = createSparseMatrix(subRows20Cols123); RealMatrix mRows31Cols31 = createSparseMatrix(subRows31Cols31); Assert.assertEquals("Rows23Cols00", mRows23Cols00, m.getSubMatrix(2, 3, 0, 0)); Assert.assertEquals("Rows00Cols33", mRows00Cols33, m.getSubMatrix(0, 0, 3, 3)); Assert.assertEquals("Rows01Cols23", mRows01Cols23, m.getSubMatrix(0, 1, 2, 3)); Assert.assertEquals("Rows02Cols13", mRows02Cols13, m.getSubMatrix(new int[] { 0, 2 }, new int[] { 1, 3 })); Assert.assertEquals("Rows03Cols12", mRows03Cols12, m.getSubMatrix(new int[] { 0, 3 }, new int[] { 1, 2 })); Assert.assertEquals("Rows03Cols123", mRows03Cols123, m.getSubMatrix(new int[] { 0, 3 }, new int[] { 1, 2, 3 })); Assert.assertEquals("Rows20Cols123", mRows20Cols123, m.getSubMatrix(new int[] { 2, 0 }, new int[] { 1, 2, 3 })); Assert.assertEquals("Rows31Cols31", mRows31Cols31, m.getSubMatrix(new int[] { 3, 1 }, new int[] { 3, 1 })); Assert.assertEquals("Rows31Cols31", mRows31Cols31, m.getSubMatrix(new int[] { 3, 1 }, new int[] { 3, 1 })); try { m.getSubMatrix(1, 0, 2, 4); Assert.fail("Expecting NumberIsTooSmallException"); } catch (NumberIsTooSmallException ex) { // expected } try { m.getSubMatrix(-1, 1, 2, 2); Assert.fail("Expecting OutOfRangeException"); } catch (OutOfRangeException ex) { // expected } try { m.getSubMatrix(1, 0, 2, 2); Assert.fail("Expecting NumberIsTooSmallException"); } catch (NumberIsTooSmallException ex) { // expected } try { m.getSubMatrix(1, 0, 2, 4); Assert.fail("Expecting NumberIsTooSmallException"); } catch (NumberIsTooSmallException ex) { // expected } try { m.getSubMatrix(new int[] {}, new int[] { 0 }); Assert.fail("Expecting NoDataException"); } catch (NoDataException ex) { // expected } try { m.getSubMatrix(new int[] { 0 }, new int[] { 4 }); Assert.fail("Expecting OutOfRangeException"); } catch (OutOfRangeException ex) { // expected } } @Test public void testGetRowMatrix() { RealMatrix m = createSparseMatrix(subTestData); RealMatrix mRow0 = createSparseMatrix(subRow0); RealMatrix mRow3 = createSparseMatrix(subRow3); Assert.assertEquals("Row0", mRow0, m.getRowMatrix(0)); Assert.assertEquals("Row3", mRow3, m.getRowMatrix(3)); try { m.getRowMatrix(-1); Assert.fail("Expecting OutOfRangeException"); } catch (OutOfRangeException ex) { // expected } try { m.getRowMatrix(4); Assert.fail("Expecting OutOfRangeException"); } catch (OutOfRangeException ex) { // expected } } @Test public void testGetColumnMatrix() { RealMatrix m = createSparseMatrix(subTestData); RealMatrix mColumn1 = createSparseMatrix(subColumn1); RealMatrix mColumn3 = createSparseMatrix(subColumn3); Assert.assertEquals("Column1", mColumn1, m.getColumnMatrix(1)); Assert.assertEquals("Column3", mColumn3, m.getColumnMatrix(3)); try { m.getColumnMatrix(-1); Assert.fail("Expecting OutOfRangeException"); } catch (OutOfRangeException ex) { // expected } try { m.getColumnMatrix(4); Assert.fail("Expecting OutOfRangeException"); } catch (OutOfRangeException ex) { // expected } } @Test public void testGetRowVector() { RealMatrix m = createSparseMatrix(subTestData); RealVector mRow0 = new ArrayRealVector(subRow0[0]); RealVector mRow3 = new ArrayRealVector(subRow3[0]); Assert.assertEquals("Row0", mRow0, m.getRowVector(0)); Assert.assertEquals("Row3", mRow3, m.getRowVector(3)); try { m.getRowVector(-1); Assert.fail("Expecting OutOfRangeException"); } catch (OutOfRangeException ex) { // expected } try { m.getRowVector(4); Assert.fail("Expecting OutOfRangeException"); } catch (OutOfRangeException ex) { // expected } } @Test public void testGetColumnVector() { RealMatrix m = createSparseMatrix(subTestData); RealVector mColumn1 = columnToVector(subColumn1); RealVector mColumn3 = columnToVector(subColumn3); Assert.assertEquals("Column1", mColumn1, m.getColumnVector(1)); Assert.assertEquals("Column3", mColumn3, m.getColumnVector(3)); try { m.getColumnVector(-1); Assert.fail("Expecting OutOfRangeException"); } catch (OutOfRangeException ex) { // expected } try { m.getColumnVector(4); Assert.fail("Expecting OutOfRangeException"); } catch (OutOfRangeException ex) { // expected } } private RealVector columnToVector(double[][] column) { double[] data = new double[column.length]; for (int i = 0; i < data.length; ++i) { data[i] = column[i][0]; } return new ArrayRealVector(data, false); } @Test public void testEqualsAndHashCode() { OpenMapRealMatrix m = createSparseMatrix(testData); OpenMapRealMatrix m1 = m.copy(); OpenMapRealMatrix mt = (OpenMapRealMatrix) m.transpose(); Assert.assertTrue(m.hashCode() != mt.hashCode()); Assert.assertEquals(m.hashCode(), m1.hashCode()); Assert.assertEquals(m, m); Assert.assertEquals(m, m1); Assert.assertFalse(m.equals(null)); Assert.assertFalse(m.equals(mt)); Assert.assertFalse(m.equals(createSparseMatrix(bigSingular))); } @Test public void testToString() { OpenMapRealMatrix m = createSparseMatrix(testData); Assert.assertEquals("OpenMapRealMatrix{{1.0,2.0,3.0},{2.0,5.0,3.0},{1.0,0.0,8.0}}", m.toString()); m = new OpenMapRealMatrix(1, 1); Assert.assertEquals("OpenMapRealMatrix{{0.0}}", m.toString()); } @Test public void testSetSubMatrix() { OpenMapRealMatrix m = createSparseMatrix(testData); m.setSubMatrix(detData2, 1, 1); RealMatrix expected = createSparseMatrix(new double[][] { { 1.0, 2.0, 3.0 }, { 2.0, 1.0, 3.0 }, { 1.0, 2.0, 4.0 } }); Assert.assertEquals(expected, m); m.setSubMatrix(detData2, 0, 0); expected = createSparseMatrix(new double[][] { { 1.0, 3.0, 3.0 }, { 2.0, 4.0, 3.0 }, { 1.0, 2.0, 4.0 } }); Assert.assertEquals(expected, m); m.setSubMatrix(testDataPlus2, 0, 0); expected = createSparseMatrix(new double[][] { { 3.0, 4.0, 5.0 }, { 4.0, 7.0, 5.0 }, { 3.0, 2.0, 10.0 } }); Assert.assertEquals(expected, m); // javadoc example OpenMapRealMatrix matrix = createSparseMatrix(new double[][] { { 1, 2, 3, 4 }, { 5, 6, 7, 8 }, { 9, 0, 1, 2 } }); matrix.setSubMatrix(new double[][] { { 3, 4 }, { 5, 6 } }, 1, 1); expected = createSparseMatrix(new double[][] { { 1, 2, 3, 4 }, { 5, 3, 4, 8 }, { 9, 5, 6, 2 } }); Assert.assertEquals(expected, matrix); // dimension overflow try { m.setSubMatrix(testData, 1, 1); Assert.fail("expecting OutOfRangeException"); } catch (OutOfRangeException e) { // expected } // dimension underflow try { m.setSubMatrix(testData, -1, 1); Assert.fail("expecting OutOfRangeException"); } catch (OutOfRangeException e) { // expected } try { m.setSubMatrix(testData, 1, -1); Assert.fail("expecting OutOfRangeException"); } catch (OutOfRangeException e) { // expected } // null try { m.setSubMatrix(null, 1, 1); Assert.fail("expecting NullArgumentException"); } catch (NullArgumentException e) { // expected } try { new OpenMapRealMatrix(0, 0); Assert.fail("expecting MathIllegalArgumentException"); } catch (MathIllegalArgumentException e) { // expected } // ragged try { m.setSubMatrix(new double[][] { { 1 }, { 2, 3 } }, 0, 0); Assert.fail("expecting MathIllegalArgumentException"); } catch (MathIllegalArgumentException e) { // expected } // empty try { m.setSubMatrix(new double[][] { {} }, 0, 0); Assert.fail("expecting MathIllegalArgumentException"); } catch (MathIllegalArgumentException e) { // expected } } @Test public void testSerial() { OpenMapRealMatrix m = createSparseMatrix(testData); Assert.assertEquals(m,TestUtils.serializeAndRecover(m)); } // --------------- -----------------Protected methods /** verifies that two matrices are close (1-norm) */ protected void assertClose(String msg, RealMatrix m, RealMatrix n, double tolerance) { Assert.assertTrue(msg, m.subtract(n).getNorm() < tolerance); } /** verifies that two vectors are close (sup norm) */ protected void assertClose(String msg, double[] m, double[] n, double tolerance) { if (m.length != n.length) { Assert.fail("vectors not same length"); } for (int i = 0; i < m.length; i++) { Assert.assertEquals(msg + " " + i + " elements differ", m[i], n[i], tolerance); } } private OpenMapRealMatrix createSparseMatrix(double[][] data) { OpenMapRealMatrix matrix = new OpenMapRealMatrix(data.length, data[0].length); for (int row = 0; row < data.length; row++) { for (int col = 0; col < data[row].length; col++) { matrix.setEntry(row, col, data[row][col]); } } return matrix; } } commons-math3-3.2-src/src/test/java/org/apache/commons/math3/linear/BlockFieldMatrixTest.java100644 1750 1750 157040 12126627674 30773 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.linear; import java.util.Arrays; import java.util.Random; import org.junit.Test; import org.junit.Assert; import org.apache.commons.math3.TestUtils; import org.apache.commons.math3.fraction.Fraction; import org.apache.commons.math3.fraction.FractionField; import org.apache.commons.math3.exception.NoDataException; import org.apache.commons.math3.exception.NullArgumentException; import org.apache.commons.math3.exception.OutOfRangeException; import org.apache.commons.math3.exception.NumberIsTooSmallException; import org.apache.commons.math3.exception.NotStrictlyPositiveException; import org.apache.commons.math3.exception.MathIllegalArgumentException; /** * Test cases for the {@link BlockFieldMatrix} class. * * @version $Id: BlockFieldMatrixTest.java 1364030 2012-07-21 01:10:04Z erans $ */ public final class BlockFieldMatrixTest { // 3 x 3 identity matrix protected Fraction[][] id = { {new Fraction(1),new Fraction(0),new Fraction(0)}, {new Fraction(0),new Fraction(1),new Fraction(0)}, {new Fraction(0),new Fraction(0),new Fraction(1)} }; // Test data for group operations protected Fraction[][] testData = { {new Fraction(1),new Fraction(2),new Fraction(3)}, {new Fraction(2),new Fraction(5),new Fraction(3)}, {new Fraction(1),new Fraction(0),new Fraction(8)} }; protected Fraction[][] testDataLU = { {new Fraction(2), new Fraction(5), new Fraction(3)}, {new Fraction(1, 2), new Fraction(-5, 2), new Fraction(13, 2)}, {new Fraction(1, 2), new Fraction(1, 5), new Fraction(1, 5)} }; protected Fraction[][] testDataPlus2 = { {new Fraction(3),new Fraction(4),new Fraction(5)}, {new Fraction(4),new Fraction(7),new Fraction(5)}, {new Fraction(3),new Fraction(2),new Fraction(10)} }; protected Fraction[][] testDataMinus = { {new Fraction(-1),new Fraction(-2),new Fraction(-3)}, {new Fraction(-2),new Fraction(-5),new Fraction(-3)}, {new Fraction(-1),new Fraction(0),new Fraction(-8)} }; protected Fraction[] testDataRow1 = {new Fraction(1),new Fraction(2),new Fraction(3)}; protected Fraction[] testDataCol3 = {new Fraction(3),new Fraction(3),new Fraction(8)}; protected Fraction[][] testDataInv = { {new Fraction(-40),new Fraction(16),new Fraction(9)}, {new Fraction(13),new Fraction(-5),new Fraction(-3)}, {new Fraction(5),new Fraction(-2),new Fraction(-1)} }; protected Fraction[] preMultTest = {new Fraction(8), new Fraction(12), new Fraction(33)}; protected Fraction[][] testData2 = { {new Fraction(1),new Fraction(2),new Fraction(3)}, {new Fraction(2),new Fraction(5),new Fraction(3)} }; protected Fraction[][] testData2T = { {new Fraction(1),new Fraction(2)}, {new Fraction(2),new Fraction(5)}, {new Fraction(3),new Fraction(3)} }; protected Fraction[][] testDataPlusInv = { {new Fraction(-39),new Fraction(18),new Fraction(12)}, {new Fraction(15),new Fraction(0),new Fraction(0)}, {new Fraction(6),new Fraction(-2),new Fraction(7)} }; // lu decomposition tests protected Fraction[][] luData = { {new Fraction(2),new Fraction(3),new Fraction(3)}, {new Fraction(0),new Fraction(5),new Fraction(7)}, {new Fraction(6),new Fraction(9),new Fraction(8)} }; protected Fraction[][] luDataLUDecomposition = { {new Fraction(6),new Fraction(9),new Fraction(8)}, {new Fraction(0),new Fraction(5),new Fraction(7)}, {new Fraction(1, 3),new Fraction(0),new Fraction(1, 3)} }; // singular matrices protected Fraction[][] singular = { {new Fraction(2),new Fraction(3)}, {new Fraction(2),new Fraction(3)} }; protected Fraction[][] bigSingular = { {new Fraction(1),new Fraction(2),new Fraction(3),new Fraction(4)}, {new Fraction(2),new Fraction(5),new Fraction(3),new Fraction(4)}, {new Fraction(7),new Fraction(3),new Fraction(256),new Fraction(1930)}, {new Fraction(3),new Fraction(7),new Fraction(6),new Fraction(8)} }; // 4th row = 1st + 2nd protected Fraction[][] detData = { {new Fraction(1),new Fraction(2),new Fraction(3)}, {new Fraction(4),new Fraction(5),new Fraction(6)}, {new Fraction(7),new Fraction(8),new Fraction(10)} }; protected Fraction[][] detData2 = { {new Fraction(1), new Fraction(3)}, {new Fraction(2), new Fraction(4)}}; // vectors protected Fraction[] testVector = {new Fraction(1),new Fraction(2),new Fraction(3)}; protected Fraction[] testVector2 = {new Fraction(1),new Fraction(2),new Fraction(3),new Fraction(4)}; // submatrix accessor tests protected Fraction[][] subTestData = { {new Fraction(1), new Fraction(2), new Fraction(3), new Fraction(4)}, {new Fraction(3, 2), new Fraction(5, 2), new Fraction(7, 2), new Fraction(9, 2)}, {new Fraction(2), new Fraction(4), new Fraction(6), new Fraction(8)}, {new Fraction(4), new Fraction(5), new Fraction(6), new Fraction(7)} }; // array selections protected Fraction[][] subRows02Cols13 = { {new Fraction(2), new Fraction(4)}, {new Fraction(4), new Fraction(8)}}; protected Fraction[][] subRows03Cols12 = { {new Fraction(2), new Fraction(3)}, {new Fraction(5), new Fraction(6)}}; protected Fraction[][] subRows03Cols123 = { {new Fraction(2), new Fraction(3), new Fraction(4)}, {new Fraction(5), new Fraction(6), new Fraction(7)} }; // effective permutations protected Fraction[][] subRows20Cols123 = { {new Fraction(4), new Fraction(6), new Fraction(8)}, {new Fraction(2), new Fraction(3), new Fraction(4)} }; protected Fraction[][] subRows31Cols31 = {{new Fraction(7), new Fraction(5)}, {new Fraction(9, 2), new Fraction(5, 2)}}; // contiguous ranges protected Fraction[][] subRows01Cols23 = {{new Fraction(3),new Fraction(4)} , {new Fraction(7, 2), new Fraction(9, 2)}}; protected Fraction[][] subRows23Cols00 = {{new Fraction(2)} , {new Fraction(4)}}; protected Fraction[][] subRows00Cols33 = {{new Fraction(4)}}; // row matrices protected Fraction[][] subRow0 = {{new Fraction(1),new Fraction(2),new Fraction(3),new Fraction(4)}}; protected Fraction[][] subRow3 = {{new Fraction(4),new Fraction(5),new Fraction(6),new Fraction(7)}}; // column matrices protected Fraction[][] subColumn1 = {{new Fraction(2)}, {new Fraction(5, 2)}, {new Fraction(4)}, {new Fraction(5)}}; protected Fraction[][] subColumn3 = {{new Fraction(4)}, {new Fraction(9, 2)}, {new Fraction(8)}, {new Fraction(7)}}; // tolerances protected double entryTolerance = 10E-16; protected double normTolerance = 10E-14; /** test dimensions */ @Test public void testDimensions() { BlockFieldMatrix m = new BlockFieldMatrix(testData); BlockFieldMatrix m2 = new BlockFieldMatrix(testData2); Assert.assertEquals("testData row dimension",3,m.getRowDimension()); Assert.assertEquals("testData column dimension",3,m.getColumnDimension()); Assert.assertTrue("testData is square",m.isSquare()); Assert.assertEquals("testData2 row dimension",m2.getRowDimension(),2); Assert.assertEquals("testData2 column dimension",m2.getColumnDimension(),3); Assert.assertTrue("testData2 is not square",!m2.isSquare()); } /** test copy functions */ @Test public void testCopyFunctions() { Random r = new Random(66636328996002l); BlockFieldMatrix m1 = createRandomMatrix(r, 47, 83); BlockFieldMatrix m2 = new BlockFieldMatrix(m1.getData()); Assert.assertEquals(m1, m2); BlockFieldMatrix m3 = new BlockFieldMatrix(testData); BlockFieldMatrix m4 = new BlockFieldMatrix(m3.getData()); Assert.assertEquals(m3, m4); } /** test add */ @Test public void testAdd() { BlockFieldMatrix m = new BlockFieldMatrix(testData); BlockFieldMatrix mInv = new BlockFieldMatrix(testDataInv); FieldMatrix mPlusMInv = m.add(mInv); Fraction[][] sumEntries = mPlusMInv.getData(); for (int row = 0; row < m.getRowDimension(); row++) { for (int col = 0; col < m.getColumnDimension(); col++) { Assert.assertEquals(testDataPlusInv[row][col],sumEntries[row][col]); } } } /** test add failure */ @Test public void testAddFail() { BlockFieldMatrix m = new BlockFieldMatrix(testData); BlockFieldMatrix m2 = new BlockFieldMatrix(testData2); try { m.add(m2); Assert.fail("MathIllegalArgumentException expected"); } catch (MathIllegalArgumentException ex) { // ignored } } /** test m-n = m + -n */ @Test public void testPlusMinus() { BlockFieldMatrix m = new BlockFieldMatrix(testData); BlockFieldMatrix m2 = new BlockFieldMatrix(testDataInv); TestUtils.assertEquals(m.subtract(m2), m2.scalarMultiply(new Fraction(-1)).add(m)); try { m.subtract(new BlockFieldMatrix(testData2)); Assert.fail("Expecting illegalArgumentException"); } catch (MathIllegalArgumentException ex) { // ignored } } /** test multiply */ @Test public void testMultiply() { BlockFieldMatrix m = new BlockFieldMatrix(testData); BlockFieldMatrix mInv = new BlockFieldMatrix(testDataInv); BlockFieldMatrix identity = new BlockFieldMatrix(id); BlockFieldMatrix m2 = new BlockFieldMatrix(testData2); TestUtils.assertEquals(m.multiply(mInv), identity); TestUtils.assertEquals(mInv.multiply(m), identity); TestUtils.assertEquals(m.multiply(identity), m); TestUtils.assertEquals(identity.multiply(mInv), mInv); TestUtils.assertEquals(m2.multiply(identity), m2); try { m.multiply(new BlockFieldMatrix(bigSingular)); Assert.fail("Expecting illegalArgumentException"); } catch (MathIllegalArgumentException ex) { // expected } } @Test public void testSeveralBlocks() { FieldMatrix m = new BlockFieldMatrix(FractionField.getInstance(), 37, 41); for (int i = 0; i < m.getRowDimension(); ++i) { for (int j = 0; j < m.getColumnDimension(); ++j) { m.setEntry(i, j, new Fraction(i * 11 + j, 11)); } } FieldMatrix mT = m.transpose(); Assert.assertEquals(m.getRowDimension(), mT.getColumnDimension()); Assert.assertEquals(m.getColumnDimension(), mT.getRowDimension()); for (int i = 0; i < mT.getRowDimension(); ++i) { for (int j = 0; j < mT.getColumnDimension(); ++j) { Assert.assertEquals(m.getEntry(j, i), mT.getEntry(i, j)); } } FieldMatrix mPm = m.add(m); for (int i = 0; i < mPm.getRowDimension(); ++i) { for (int j = 0; j < mPm.getColumnDimension(); ++j) { Assert.assertEquals(m.getEntry(i, j).multiply(new Fraction(2)), mPm.getEntry(i, j)); } } FieldMatrix mPmMm = mPm.subtract(m); for (int i = 0; i < mPmMm.getRowDimension(); ++i) { for (int j = 0; j < mPmMm.getColumnDimension(); ++j) { Assert.assertEquals(m.getEntry(i, j), mPmMm.getEntry(i, j)); } } FieldMatrix mTm = mT.multiply(m); for (int i = 0; i < mTm.getRowDimension(); ++i) { for (int j = 0; j < mTm.getColumnDimension(); ++j) { Fraction sum = Fraction.ZERO; for (int k = 0; k < mT.getColumnDimension(); ++k) { sum = sum.add(new Fraction(k * 11 + i, 11).multiply(new Fraction(k * 11 + j, 11))); } Assert.assertEquals(sum, mTm.getEntry(i, j)); } } FieldMatrix mmT = m.multiply(mT); for (int i = 0; i < mmT.getRowDimension(); ++i) { for (int j = 0; j < mmT.getColumnDimension(); ++j) { Fraction sum = Fraction.ZERO; for (int k = 0; k < m.getColumnDimension(); ++k) { sum = sum.add(new Fraction(i * 11 + k, 11).multiply(new Fraction(j * 11 + k, 11))); } Assert.assertEquals(sum, mmT.getEntry(i, j)); } } FieldMatrix sub1 = m.getSubMatrix(2, 9, 5, 20); for (int i = 0; i < sub1.getRowDimension(); ++i) { for (int j = 0; j < sub1.getColumnDimension(); ++j) { Assert.assertEquals(new Fraction((i + 2) * 11 + (j + 5), 11), sub1.getEntry(i, j)); } } FieldMatrix sub2 = m.getSubMatrix(10, 12, 3, 40); for (int i = 0; i < sub2.getRowDimension(); ++i) { for (int j = 0; j < sub2.getColumnDimension(); ++j) { Assert.assertEquals(new Fraction((i + 10) * 11 + (j + 3), 11), sub2.getEntry(i, j)); } } FieldMatrix sub3 = m.getSubMatrix(30, 34, 0, 5); for (int i = 0; i < sub3.getRowDimension(); ++i) { for (int j = 0; j < sub3.getColumnDimension(); ++j) { Assert.assertEquals(new Fraction((i + 30) * 11 + (j + 0), 11), sub3.getEntry(i, j)); } } FieldMatrix sub4 = m.getSubMatrix(30, 32, 32, 35); for (int i = 0; i < sub4.getRowDimension(); ++i) { for (int j = 0; j < sub4.getColumnDimension(); ++j) { Assert.assertEquals(new Fraction((i + 30) * 11 + (j + 32), 11), sub4.getEntry(i, j)); } } } //Additional Test for BlockFieldMatrixTest.testMultiply private Fraction[][] d3 = new Fraction[][] { {new Fraction(1),new Fraction(2),new Fraction(3),new Fraction(4)}, {new Fraction(5),new Fraction(6),new Fraction(7),new Fraction(8)} }; private Fraction[][] d4 = new Fraction[][] { {new Fraction(1)}, {new Fraction(2)}, {new Fraction(3)}, {new Fraction(4)} }; private Fraction[][] d5 = new Fraction[][] {{new Fraction(30)},{new Fraction(70)}}; @Test public void testMultiply2() { FieldMatrix m3 = new BlockFieldMatrix(d3); FieldMatrix m4 = new BlockFieldMatrix(d4); FieldMatrix m5 = new BlockFieldMatrix(d5); TestUtils.assertEquals(m3.multiply(m4), m5); } /** test trace */ @Test public void testTrace() { FieldMatrix m = new BlockFieldMatrix(id); Assert.assertEquals(new Fraction(3),m.getTrace()); m = new BlockFieldMatrix(testData2); try { m.getTrace(); Assert.fail("Expecting NonSquareMatrixException"); } catch (NonSquareMatrixException ex) { // ignored } } /** test scalarAdd */ @Test public void testScalarAdd() { FieldMatrix m = new BlockFieldMatrix(testData); TestUtils.assertEquals(new BlockFieldMatrix(testDataPlus2), m.scalarAdd(new Fraction(2))); } /** test operate */ @Test public void testOperate() { FieldMatrix m = new BlockFieldMatrix(id); TestUtils.assertEquals(testVector, m.operate(testVector)); TestUtils.assertEquals(testVector, m.operate(new ArrayFieldVector(testVector)).getData()); m = new BlockFieldMatrix(bigSingular); try { m.operate(testVector); Assert.fail("Expecting illegalArgumentException"); } catch (MathIllegalArgumentException ex) { // ignored } } @Test public void testOperateLarge() { int p = (11 * BlockFieldMatrix.BLOCK_SIZE) / 10; int q = (11 * BlockFieldMatrix.BLOCK_SIZE) / 10; int r = BlockFieldMatrix.BLOCK_SIZE / 2; Random random = new Random(111007463902334l); FieldMatrix m1 = createRandomMatrix(random, p, q); FieldMatrix m2 = createRandomMatrix(random, q, r); FieldMatrix m1m2 = m1.multiply(m2); for (int i = 0; i < r; ++i) { TestUtils.assertEquals(m1m2.getColumn(i), m1.operate(m2.getColumn(i))); } } @Test public void testOperatePremultiplyLarge() { int p = (11 * BlockFieldMatrix.BLOCK_SIZE) / 10; int q = (11 * BlockFieldMatrix.BLOCK_SIZE) / 10; int r = BlockFieldMatrix.BLOCK_SIZE / 2; Random random = new Random(111007463902334l); FieldMatrix m1 = createRandomMatrix(random, p, q); FieldMatrix m2 = createRandomMatrix(random, q, r); FieldMatrix m1m2 = m1.multiply(m2); for (int i = 0; i < p; ++i) { TestUtils.assertEquals(m1m2.getRow(i), m2.preMultiply(m1.getRow(i))); } } /** test issue MATH-209 */ @Test public void testMath209() { FieldMatrix a = new BlockFieldMatrix(new Fraction[][] { { new Fraction(1), new Fraction(2) }, { new Fraction(3), new Fraction(4) }, { new Fraction(5), new Fraction(6) } }); Fraction[] b = a.operate(new Fraction[] { new Fraction(1), new Fraction(1) }); Assert.assertEquals(a.getRowDimension(), b.length); Assert.assertEquals( new Fraction(3), b[0]); Assert.assertEquals( new Fraction(7), b[1]); Assert.assertEquals(new Fraction(11), b[2]); } /** test transpose */ @Test public void testTranspose() { FieldMatrix m = new BlockFieldMatrix(testData); FieldMatrix mIT = new FieldLUDecomposition(m).getSolver().getInverse().transpose(); FieldMatrix mTI = new FieldLUDecomposition(m.transpose()).getSolver().getInverse(); TestUtils.assertEquals(mIT, mTI); m = new BlockFieldMatrix(testData2); FieldMatrix mt = new BlockFieldMatrix(testData2T); TestUtils.assertEquals(mt, m.transpose()); } /** test preMultiply by vector */ @Test public void testPremultiplyVector() { FieldMatrix m = new BlockFieldMatrix(testData); TestUtils.assertEquals(m.preMultiply(testVector), preMultTest); TestUtils.assertEquals(m.preMultiply(new ArrayFieldVector(testVector).getData()), preMultTest); m = new BlockFieldMatrix(bigSingular); try { m.preMultiply(testVector); Assert.fail("expecting MathIllegalArgumentException"); } catch (MathIllegalArgumentException ex) { // ignored } } @Test public void testPremultiply() { FieldMatrix m3 = new BlockFieldMatrix(d3); FieldMatrix m4 = new BlockFieldMatrix(d4); FieldMatrix m5 = new BlockFieldMatrix(d5); TestUtils.assertEquals(m4.preMultiply(m3), m5); BlockFieldMatrix m = new BlockFieldMatrix(testData); BlockFieldMatrix mInv = new BlockFieldMatrix(testDataInv); BlockFieldMatrix identity = new BlockFieldMatrix(id); TestUtils.assertEquals(m.preMultiply(mInv), identity); TestUtils.assertEquals(mInv.preMultiply(m), identity); TestUtils.assertEquals(m.preMultiply(identity), m); TestUtils.assertEquals(identity.preMultiply(mInv), mInv); try { m.preMultiply(new BlockFieldMatrix(bigSingular)); Assert.fail("Expecting illegalArgumentException"); } catch (MathIllegalArgumentException ex) { // ignored } } @Test public void testGetVectors() { FieldMatrix m = new BlockFieldMatrix(testData); TestUtils.assertEquals(m.getRow(0), testDataRow1); TestUtils.assertEquals(m.getColumn(2), testDataCol3); try { m.getRow(10); Assert.fail("expecting OutOfRangeException"); } catch (OutOfRangeException ex) { // ignored } try { m.getColumn(-1); Assert.fail("expecting OutOfRangeException"); } catch (OutOfRangeException ex) { // ignored } } @Test public void testGetEntry() { FieldMatrix m = new BlockFieldMatrix(testData); Assert.assertEquals(m.getEntry(0,1),new Fraction(2)); try { m.getEntry(10, 4); Assert.fail ("Expecting OutOfRangeException"); } catch (OutOfRangeException ex) { // expected } } /** test examples in user guide */ @Test public void testExamples() { // Create a real matrix with two rows and three columns Fraction[][] matrixData = { {new Fraction(1),new Fraction(2),new Fraction(3)}, {new Fraction(2),new Fraction(5),new Fraction(3)} }; FieldMatrix m = new BlockFieldMatrix(matrixData); // One more with three rows, two columns Fraction[][] matrixData2 = { {new Fraction(1),new Fraction(2)}, {new Fraction(2),new Fraction(5)}, {new Fraction(1), new Fraction(7)} }; FieldMatrix n = new BlockFieldMatrix(matrixData2); // Now multiply m by n FieldMatrix p = m.multiply(n); Assert.assertEquals(2, p.getRowDimension()); Assert.assertEquals(2, p.getColumnDimension()); // Invert p FieldMatrix pInverse = new FieldLUDecomposition(p).getSolver().getInverse(); Assert.assertEquals(2, pInverse.getRowDimension()); Assert.assertEquals(2, pInverse.getColumnDimension()); // Solve example Fraction[][] coefficientsData = { {new Fraction(2), new Fraction(3), new Fraction(-2)}, {new Fraction(-1), new Fraction(7), new Fraction(6)}, {new Fraction(4), new Fraction(-3), new Fraction(-5)} }; FieldMatrix coefficients = new BlockFieldMatrix(coefficientsData); Fraction[] constants = { new Fraction(1), new Fraction(-2), new Fraction(1) }; Fraction[] solution; solution = new FieldLUDecomposition(coefficients) .getSolver() .solve(new ArrayFieldVector(constants, false)).toArray(); Assert.assertEquals(new Fraction(2).multiply(solution[0]). add(new Fraction(3).multiply(solution[1])). subtract(new Fraction(2).multiply(solution[2])), constants[0]); Assert.assertEquals(new Fraction(-1).multiply(solution[0]). add(new Fraction(7).multiply(solution[1])). add(new Fraction(6).multiply(solution[2])), constants[1]); Assert.assertEquals(new Fraction(4).multiply(solution[0]). subtract(new Fraction(3).multiply(solution[1])). subtract(new Fraction(5).multiply(solution[2])), constants[2]); } // test submatrix accessors @Test public void testGetSubMatrix() { FieldMatrix m = new BlockFieldMatrix(subTestData); checkGetSubMatrix(m, subRows23Cols00, 2 , 3 , 0, 0); checkGetSubMatrix(m, subRows00Cols33, 0 , 0 , 3, 3); checkGetSubMatrix(m, subRows01Cols23, 0 , 1 , 2, 3); checkGetSubMatrix(m, subRows02Cols13, new int[] { 0, 2 }, new int[] { 1, 3 }); checkGetSubMatrix(m, subRows03Cols12, new int[] { 0, 3 }, new int[] { 1, 2 }); checkGetSubMatrix(m, subRows03Cols123, new int[] { 0, 3 }, new int[] { 1, 2, 3 }); checkGetSubMatrix(m, subRows20Cols123, new int[] { 2, 0 }, new int[] { 1, 2, 3 }); checkGetSubMatrix(m, subRows31Cols31, new int[] { 3, 1 }, new int[] { 3, 1 }); checkGetSubMatrix(m, subRows31Cols31, new int[] { 3, 1 }, new int[] { 3, 1 }); checkGetSubMatrix(m, null, 1, 0, 2, 4); checkGetSubMatrix(m, null, -1, 1, 2, 2); checkGetSubMatrix(m, null, 1, 0, 2, 2); checkGetSubMatrix(m, null, 1, 0, 2, 4); checkGetSubMatrix(m, null, new int[] {}, new int[] { 0 }); checkGetSubMatrix(m, null, new int[] { 0 }, new int[] { 4 }); } private void checkGetSubMatrix(FieldMatrix m, Fraction[][] reference, int startRow, int endRow, int startColumn, int endColumn) { try { FieldMatrix sub = m.getSubMatrix(startRow, endRow, startColumn, endColumn); if (reference != null) { Assert.assertEquals(new BlockFieldMatrix(reference), sub); } else { Assert.fail("Expecting OutOfRangeException or NotStrictlyPositiveException" + " or NumberIsTooSmallException or NoDataException"); } } catch (OutOfRangeException e) { if (reference != null) { throw e; } } catch (NotStrictlyPositiveException e) { if (reference != null) { throw e; } } catch (NumberIsTooSmallException e) { if (reference != null) { throw e; } } catch (NoDataException e) { if (reference != null) { throw e; } } } private void checkGetSubMatrix(FieldMatrix m, Fraction[][] reference, int[] selectedRows, int[] selectedColumns) { try { FieldMatrix sub = m.getSubMatrix(selectedRows, selectedColumns); if (reference != null) { Assert.assertEquals(new BlockFieldMatrix(reference), sub); } else { Assert.fail("Expecting OutOfRangeException"); } } catch (OutOfRangeException e) { if (reference != null) { throw e; } } catch (NotStrictlyPositiveException e) { if (reference != null) { throw e; } } catch (NumberIsTooSmallException e) { if (reference != null) { throw e; } } catch (NoDataException e) { if (reference != null) { throw e; } } } @Test public void testGetSetMatrixLarge() { int n = 3 * BlockFieldMatrix.BLOCK_SIZE; FieldMatrix m = new BlockFieldMatrix(FractionField.getInstance(), n, n); FieldMatrix sub = new BlockFieldMatrix(FractionField.getInstance(), n - 4, n - 4).scalarAdd(new Fraction(1)); m.setSubMatrix(sub.getData(), 2, 2); for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j) { if ((i < 2) || (i > n - 3) || (j < 2) || (j > n - 3)) { Assert.assertEquals(new Fraction(0), m.getEntry(i, j)); } else { Assert.assertEquals(new Fraction(1), m.getEntry(i, j)); } } } Assert.assertEquals(sub, m.getSubMatrix(2, n - 3, 2, n - 3)); } @Test public void testCopySubMatrix() { FieldMatrix m = new BlockFieldMatrix(subTestData); checkCopy(m, subRows23Cols00, 2 , 3 , 0, 0); checkCopy(m, subRows00Cols33, 0 , 0 , 3, 3); checkCopy(m, subRows01Cols23, 0 , 1 , 2, 3); checkCopy(m, subRows02Cols13, new int[] { 0, 2 }, new int[] { 1, 3 }); checkCopy(m, subRows03Cols12, new int[] { 0, 3 }, new int[] { 1, 2 }); checkCopy(m, subRows03Cols123, new int[] { 0, 3 }, new int[] { 1, 2, 3 }); checkCopy(m, subRows20Cols123, new int[] { 2, 0 }, new int[] { 1, 2, 3 }); checkCopy(m, subRows31Cols31, new int[] { 3, 1 }, new int[] { 3, 1 }); checkCopy(m, subRows31Cols31, new int[] { 3, 1 }, new int[] { 3, 1 }); checkCopy(m, null, 1, 0, 2, 4); checkCopy(m, null, -1, 1, 2, 2); checkCopy(m, null, 1, 0, 2, 2); checkCopy(m, null, 1, 0, 2, 4); checkCopy(m, null, new int[] {}, new int[] { 0 }); checkCopy(m, null, new int[] { 0 }, new int[] { 4 }); } private void checkCopy(FieldMatrix m, Fraction[][] reference, int startRow, int endRow, int startColumn, int endColumn) { try { Fraction[][] sub = (reference == null) ? new Fraction[1][1] : new Fraction[reference.length][reference[0].length]; m.copySubMatrix(startRow, endRow, startColumn, endColumn, sub); if (reference != null) { Assert.assertEquals(new BlockFieldMatrix(reference), new BlockFieldMatrix(sub)); } else { Assert.fail("Expecting OutOfRangeException or NumberIsTooSmallException or NoDataException"); } } catch (OutOfRangeException e) { if (reference != null) { throw e; } } catch (NumberIsTooSmallException e) { if (reference != null) { throw e; } } catch (NoDataException e) { if (reference != null) { throw e; } } } private void checkCopy(FieldMatrix m, Fraction[][] reference, int[] selectedRows, int[] selectedColumns) { try { Fraction[][] sub = (reference == null) ? new Fraction[1][1] : new Fraction[reference.length][reference[0].length]; m.copySubMatrix(selectedRows, selectedColumns, sub); if (reference != null) { Assert.assertEquals(new BlockFieldMatrix(reference), new BlockFieldMatrix(sub)); } else { Assert.fail("Expecting OutOfRangeException or NumberIsTooSmallException or NoDataException"); } } catch (OutOfRangeException e) { if (reference != null) { throw e; } } catch (NumberIsTooSmallException e) { if (reference != null) { throw e; } } catch (NoDataException e) { if (reference != null) { throw e; } } } @Test public void testGetRowMatrix() { FieldMatrix m = new BlockFieldMatrix(subTestData); FieldMatrix mRow0 = new BlockFieldMatrix(subRow0); FieldMatrix mRow3 = new BlockFieldMatrix(subRow3); Assert.assertEquals("Row0", mRow0, m.getRowMatrix(0)); Assert.assertEquals("Row3", mRow3, m.getRowMatrix(3)); try { m.getRowMatrix(-1); Assert.fail("Expecting OutOfRangeException"); } catch (OutOfRangeException ex) { // expected } try { m.getRowMatrix(4); Assert.fail("Expecting OutOfRangeException"); } catch (OutOfRangeException ex) { // expected } } @Test public void testSetRowMatrix() { FieldMatrix m = new BlockFieldMatrix(subTestData); FieldMatrix mRow3 = new BlockFieldMatrix(subRow3); Assert.assertNotSame(mRow3, m.getRowMatrix(0)); m.setRowMatrix(0, mRow3); Assert.assertEquals(mRow3, m.getRowMatrix(0)); try { m.setRowMatrix(-1, mRow3); Assert.fail("Expecting OutOfRangeException"); } catch (OutOfRangeException ex) { // expected } try { m.setRowMatrix(0, m); Assert.fail("Expecting MatrixDimensionMismatchException"); } catch (MatrixDimensionMismatchException ex) { // expected } } @Test public void testGetSetRowMatrixLarge() { int n = 3 * BlockFieldMatrix.BLOCK_SIZE; FieldMatrix m = new BlockFieldMatrix(FractionField.getInstance(), n, n); FieldMatrix sub = new BlockFieldMatrix(FractionField.getInstance(), 1, n).scalarAdd(new Fraction(1)); m.setRowMatrix(2, sub); for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j) { if (i != 2) { Assert.assertEquals(new Fraction(0), m.getEntry(i, j)); } else { Assert.assertEquals(new Fraction(1), m.getEntry(i, j)); } } } Assert.assertEquals(sub, m.getRowMatrix(2)); } @Test public void testGetColumnMatrix() { FieldMatrix m = new BlockFieldMatrix(subTestData); FieldMatrix mColumn1 = new BlockFieldMatrix(subColumn1); FieldMatrix mColumn3 = new BlockFieldMatrix(subColumn3); Assert.assertEquals(mColumn1, m.getColumnMatrix(1)); Assert.assertEquals(mColumn3, m.getColumnMatrix(3)); try { m.getColumnMatrix(-1); Assert.fail("Expecting OutOfRangeException"); } catch (OutOfRangeException ex) { // expected } try { m.getColumnMatrix(4); Assert.fail("Expecting OutOfRangeException"); } catch (OutOfRangeException ex) { // expected } } @Test public void testSetColumnMatrix() { FieldMatrix m = new BlockFieldMatrix(subTestData); FieldMatrix mColumn3 = new BlockFieldMatrix(subColumn3); Assert.assertNotSame(mColumn3, m.getColumnMatrix(1)); m.setColumnMatrix(1, mColumn3); Assert.assertEquals(mColumn3, m.getColumnMatrix(1)); try { m.setColumnMatrix(-1, mColumn3); Assert.fail("Expecting OutOfRangeException"); } catch (OutOfRangeException ex) { // expected } try { m.setColumnMatrix(0, m); Assert.fail("Expecting MatrixDimensionMismatchException"); } catch (MatrixDimensionMismatchException ex) { // expected } } @Test public void testGetSetColumnMatrixLarge() { int n = 3 * BlockFieldMatrix.BLOCK_SIZE; FieldMatrix m = new BlockFieldMatrix(FractionField.getInstance(), n, n); FieldMatrix sub = new BlockFieldMatrix(FractionField.getInstance(), n, 1).scalarAdd(new Fraction(1)); m.setColumnMatrix(2, sub); for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j) { if (j != 2) { Assert.assertEquals(new Fraction(0), m.getEntry(i, j)); } else { Assert.assertEquals(new Fraction(1), m.getEntry(i, j)); } } } Assert.assertEquals(sub, m.getColumnMatrix(2)); } @Test public void testGetRowVector() { FieldMatrix m = new BlockFieldMatrix(subTestData); FieldVector mRow0 = new ArrayFieldVector(subRow0[0]); FieldVector mRow3 = new ArrayFieldVector(subRow3[0]); Assert.assertEquals(mRow0, m.getRowVector(0)); Assert.assertEquals(mRow3, m.getRowVector(3)); try { m.getRowVector(-1); Assert.fail("Expecting OutOfRangeException"); } catch (OutOfRangeException ex) { // expected } try { m.getRowVector(4); Assert.fail("Expecting OutOfRangeException"); } catch (OutOfRangeException ex) { // expected } } @Test public void testSetRowVector() { FieldMatrix m = new BlockFieldMatrix(subTestData); FieldVector mRow3 = new ArrayFieldVector(subRow3[0]); Assert.assertNotSame(mRow3, m.getRowMatrix(0)); m.setRowVector(0, mRow3); Assert.assertEquals(mRow3, m.getRowVector(0)); try { m.setRowVector(-1, mRow3); Assert.fail("Expecting OutOfRangeException"); } catch (OutOfRangeException ex) { // expected } try { m.setRowVector(0, new ArrayFieldVector(FractionField.getInstance(), 5)); Assert.fail("Expecting MatrixDimensionMismatchException"); } catch (MatrixDimensionMismatchException ex) { // expected } } @Test public void testGetSetRowVectorLarge() { int n = 3 * BlockFieldMatrix.BLOCK_SIZE; FieldMatrix m = new BlockFieldMatrix(FractionField.getInstance(), n, n); FieldVector sub = new ArrayFieldVector(n, new Fraction(1)); m.setRowVector(2, sub); for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j) { if (i != 2) { Assert.assertEquals(new Fraction(0), m.getEntry(i, j)); } else { Assert.assertEquals(new Fraction(1), m.getEntry(i, j)); } } } Assert.assertEquals(sub, m.getRowVector(2)); } @Test public void testGetColumnVector() { FieldMatrix m = new BlockFieldMatrix(subTestData); FieldVector mColumn1 = columnToVector(subColumn1); FieldVector mColumn3 = columnToVector(subColumn3); Assert.assertEquals(mColumn1, m.getColumnVector(1)); Assert.assertEquals(mColumn3, m.getColumnVector(3)); try { m.getColumnVector(-1); Assert.fail("Expecting OutOfRangeException"); } catch (OutOfRangeException ex) { // expected } try { m.getColumnVector(4); Assert.fail("Expecting OutOfRangeException"); } catch (OutOfRangeException ex) { // expected } } @Test public void testSetColumnVector() { FieldMatrix m = new BlockFieldMatrix(subTestData); FieldVector mColumn3 = columnToVector(subColumn3); Assert.assertNotSame(mColumn3, m.getColumnVector(1)); m.setColumnVector(1, mColumn3); Assert.assertEquals(mColumn3, m.getColumnVector(1)); try { m.setColumnVector(-1, mColumn3); Assert.fail("Expecting OutOfRangeException"); } catch (OutOfRangeException ex) { // expected } try { m.setColumnVector(0, new ArrayFieldVector(FractionField.getInstance(), 5)); Assert.fail("Expecting MatrixDimensionMismatchException"); } catch (MatrixDimensionMismatchException ex) { // expected } } @Test public void testGetSetColumnVectorLarge() { int n = 3 * BlockFieldMatrix.BLOCK_SIZE; FieldMatrix m = new BlockFieldMatrix(FractionField.getInstance(), n, n); FieldVector sub = new ArrayFieldVector(n, new Fraction(1)); m.setColumnVector(2, sub); for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j) { if (j != 2) { Assert.assertEquals(new Fraction(0), m.getEntry(i, j)); } else { Assert.assertEquals(new Fraction(1), m.getEntry(i, j)); } } } Assert.assertEquals(sub, m.getColumnVector(2)); } private FieldVector columnToVector(Fraction[][] column) { Fraction[] data = new Fraction[column.length]; for (int i = 0; i < data.length; ++i) { data[i] = column[i][0]; } return new ArrayFieldVector(data, false); } @Test public void testGetRow() { FieldMatrix m = new BlockFieldMatrix(subTestData); checkArrays(subRow0[0], m.getRow(0)); checkArrays(subRow3[0], m.getRow(3)); try { m.getRow(-1); Assert.fail("Expecting OutOfRangeException"); } catch (OutOfRangeException ex) { // expected } try { m.getRow(4); Assert.fail("Expecting OutOfRangeException"); } catch (OutOfRangeException ex) { // expected } } @Test public void testSetRow() { FieldMatrix m = new BlockFieldMatrix(subTestData); Assert.assertTrue(subRow3[0][0] != m.getRow(0)[0]); m.setRow(0, subRow3[0]); checkArrays(subRow3[0], m.getRow(0)); try { m.setRow(-1, subRow3[0]); Assert.fail("Expecting OutOfRangeException"); } catch (OutOfRangeException ex) { // expected } try { m.setRow(0, new Fraction[5]); Assert.fail("Expecting MatrixDimensionMismatchException"); } catch (MatrixDimensionMismatchException ex) { // expected } } @Test public void testGetSetRowLarge() { int n = 3 * BlockFieldMatrix.BLOCK_SIZE; FieldMatrix m = new BlockFieldMatrix(FractionField.getInstance(), n, n); Fraction[] sub = new Fraction[n]; Arrays.fill(sub, new Fraction(1)); m.setRow(2, sub); for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j) { if (i != 2) { Assert.assertEquals(new Fraction(0), m.getEntry(i, j)); } else { Assert.assertEquals(new Fraction(1), m.getEntry(i, j)); } } } checkArrays(sub, m.getRow(2)); } @Test public void testGetColumn() { FieldMatrix m = new BlockFieldMatrix(subTestData); Fraction[] mColumn1 = columnToArray(subColumn1); Fraction[] mColumn3 = columnToArray(subColumn3); checkArrays(mColumn1, m.getColumn(1)); checkArrays(mColumn3, m.getColumn(3)); try { m.getColumn(-1); Assert.fail("Expecting OutOfRangeException"); } catch (OutOfRangeException ex) { // expected } try { m.getColumn(4); Assert.fail("Expecting OutOfRangeException"); } catch (OutOfRangeException ex) { // expected } } @Test public void testSetColumn() { FieldMatrix m = new BlockFieldMatrix(subTestData); Fraction[] mColumn3 = columnToArray(subColumn3); Assert.assertTrue(mColumn3[0] != m.getColumn(1)[0]); m.setColumn(1, mColumn3); checkArrays(mColumn3, m.getColumn(1)); try { m.setColumn(-1, mColumn3); Assert.fail("Expecting OutOfRangeException"); } catch (OutOfRangeException ex) { // expected } try { m.setColumn(0, new Fraction[5]); Assert.fail("Expecting MatrixDimensionMismatchException"); } catch (MatrixDimensionMismatchException ex) { // expected } } @Test public void testGetSetColumnLarge() { int n = 3 * BlockFieldMatrix.BLOCK_SIZE; FieldMatrix m = new BlockFieldMatrix(FractionField.getInstance(), n, n); Fraction[] sub = new Fraction[n]; Arrays.fill(sub, new Fraction(1)); m.setColumn(2, sub); for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j) { if (j != 2) { Assert.assertEquals(new Fraction(0), m.getEntry(i, j)); } else { Assert.assertEquals(new Fraction(1), m.getEntry(i, j)); } } } checkArrays(sub, m.getColumn(2)); } private Fraction[] columnToArray(Fraction[][] column) { Fraction[] data = new Fraction[column.length]; for (int i = 0; i < data.length; ++i) { data[i] = column[i][0]; } return data; } private void checkArrays(Fraction[] expected, Fraction[] actual) { Assert.assertEquals(expected.length, actual.length); for (int i = 0; i < expected.length; ++i) { Assert.assertEquals(expected[i], actual[i]); } } @Test public void testEqualsAndHashCode() { BlockFieldMatrix m = new BlockFieldMatrix(testData); BlockFieldMatrix m1 = (BlockFieldMatrix) m.copy(); BlockFieldMatrix mt = (BlockFieldMatrix) m.transpose(); Assert.assertTrue(m.hashCode() != mt.hashCode()); Assert.assertEquals(m.hashCode(), m1.hashCode()); Assert.assertEquals(m, m); Assert.assertEquals(m, m1); Assert.assertFalse(m.equals(null)); Assert.assertFalse(m.equals(mt)); Assert.assertFalse(m.equals(new BlockFieldMatrix(bigSingular))); } @Test public void testToString() { BlockFieldMatrix m = new BlockFieldMatrix(testData); Assert.assertEquals("BlockFieldMatrix{{1,2,3},{2,5,3},{1,0,8}}", m.toString()); } @Test public void testSetSubMatrix() { BlockFieldMatrix m = new BlockFieldMatrix(testData); m.setSubMatrix(detData2,1,1); FieldMatrix expected = new BlockFieldMatrix (new Fraction[][] {{new Fraction(1),new Fraction(2),new Fraction(3)},{new Fraction(2),new Fraction(1),new Fraction(3)},{new Fraction(1),new Fraction(2),new Fraction(4)}}); Assert.assertEquals(expected, m); m.setSubMatrix(detData2,0,0); expected = new BlockFieldMatrix (new Fraction[][] {{new Fraction(1),new Fraction(3),new Fraction(3)},{new Fraction(2),new Fraction(4),new Fraction(3)},{new Fraction(1),new Fraction(2),new Fraction(4)}}); Assert.assertEquals(expected, m); m.setSubMatrix(testDataPlus2,0,0); expected = new BlockFieldMatrix (new Fraction[][] {{new Fraction(3),new Fraction(4),new Fraction(5)},{new Fraction(4),new Fraction(7),new Fraction(5)},{new Fraction(3),new Fraction(2),new Fraction(10)}}); Assert.assertEquals(expected, m); // javadoc example BlockFieldMatrix matrix = new BlockFieldMatrix(new Fraction[][] { {new Fraction(1), new Fraction(2), new Fraction(3), new Fraction(4)}, {new Fraction(5), new Fraction(6), new Fraction(7), new Fraction(8)}, {new Fraction(9), new Fraction(0), new Fraction(1) , new Fraction(2)} }); matrix.setSubMatrix(new Fraction[][] { {new Fraction(3), new Fraction(4)}, {new Fraction(5), new Fraction(6)} }, 1, 1); expected = new BlockFieldMatrix(new Fraction[][] { {new Fraction(1), new Fraction(2), new Fraction(3),new Fraction(4)}, {new Fraction(5), new Fraction(3), new Fraction(4), new Fraction(8)}, {new Fraction(9), new Fraction(5) ,new Fraction(6), new Fraction(2)} }); Assert.assertEquals(expected, matrix); // dimension overflow try { m.setSubMatrix(testData,1,1); Assert.fail("expecting OutOfRangeException"); } catch (OutOfRangeException e) { // expected } // dimension underflow try { m.setSubMatrix(testData,-1,1); Assert.fail("expecting OutOfRangeException"); } catch (OutOfRangeException e) { // expected } try { m.setSubMatrix(testData,1,-1); Assert.fail("expecting OutOfRangeException"); } catch (OutOfRangeException e) { // expected } // null try { m.setSubMatrix(null,1,1); Assert.fail("expecting NullArgumentException"); } catch (NullArgumentException e) { // expected } // ragged try { m.setSubMatrix(new Fraction[][] {{new Fraction(1)}, {new Fraction(2), new Fraction(3)}}, 0, 0); Assert.fail("expecting MathIllegalArgumentException"); } catch (MathIllegalArgumentException e) { // expected } // empty try { m.setSubMatrix(new Fraction[][] {{}}, 0, 0); Assert.fail("expecting MathIllegalArgumentException"); } catch (MathIllegalArgumentException e) { // expected } } @Test public void testWalk() { int rows = 150; int columns = 75; FieldMatrix m = new BlockFieldMatrix(FractionField.getInstance(), rows, columns); m.walkInRowOrder(new SetVisitor()); GetVisitor getVisitor = new GetVisitor(); m.walkInOptimizedOrder(getVisitor); Assert.assertEquals(rows * columns, getVisitor.getCount()); m = new BlockFieldMatrix(FractionField.getInstance(), rows, columns); m.walkInRowOrder(new SetVisitor(), 1, rows - 2, 1, columns - 2); getVisitor = new GetVisitor(); m.walkInOptimizedOrder(getVisitor, 1, rows - 2, 1, columns - 2); Assert.assertEquals((rows - 2) * (columns - 2), getVisitor.getCount()); for (int i = 0; i < rows; ++i) { Assert.assertEquals(new Fraction(0), m.getEntry(i, 0)); Assert.assertEquals(new Fraction(0), m.getEntry(i, columns - 1)); } for (int j = 0; j < columns; ++j) { Assert.assertEquals(new Fraction(0), m.getEntry(0, j)); Assert.assertEquals(new Fraction(0), m.getEntry(rows - 1, j)); } m = new BlockFieldMatrix(FractionField.getInstance(), rows, columns); m.walkInColumnOrder(new SetVisitor()); getVisitor = new GetVisitor(); m.walkInOptimizedOrder(getVisitor); Assert.assertEquals(rows * columns, getVisitor.getCount()); m = new BlockFieldMatrix(FractionField.getInstance(), rows, columns); m.walkInColumnOrder(new SetVisitor(), 1, rows - 2, 1, columns - 2); getVisitor = new GetVisitor(); m.walkInOptimizedOrder(getVisitor, 1, rows - 2, 1, columns - 2); Assert.assertEquals((rows - 2) * (columns - 2), getVisitor.getCount()); for (int i = 0; i < rows; ++i) { Assert.assertEquals(new Fraction(0), m.getEntry(i, 0)); Assert.assertEquals(new Fraction(0), m.getEntry(i, columns - 1)); } for (int j = 0; j < columns; ++j) { Assert.assertEquals(new Fraction(0), m.getEntry(0, j)); Assert.assertEquals(new Fraction(0), m.getEntry(rows - 1, j)); } m = new BlockFieldMatrix(FractionField.getInstance(), rows, columns); m.walkInOptimizedOrder(new SetVisitor()); getVisitor = new GetVisitor(); m.walkInRowOrder(getVisitor); Assert.assertEquals(rows * columns, getVisitor.getCount()); m = new BlockFieldMatrix(FractionField.getInstance(), rows, columns); m.walkInOptimizedOrder(new SetVisitor(), 1, rows - 2, 1, columns - 2); getVisitor = new GetVisitor(); m.walkInRowOrder(getVisitor, 1, rows - 2, 1, columns - 2); Assert.assertEquals((rows - 2) * (columns - 2), getVisitor.getCount()); for (int i = 0; i < rows; ++i) { Assert.assertEquals(new Fraction(0), m.getEntry(i, 0)); Assert.assertEquals(new Fraction(0), m.getEntry(i, columns - 1)); } for (int j = 0; j < columns; ++j) { Assert.assertEquals(new Fraction(0), m.getEntry(0, j)); Assert.assertEquals(new Fraction(0), m.getEntry(rows - 1, j)); } m = new BlockFieldMatrix(FractionField.getInstance(), rows, columns); m.walkInOptimizedOrder(new SetVisitor()); getVisitor = new GetVisitor(); m.walkInColumnOrder(getVisitor); Assert.assertEquals(rows * columns, getVisitor.getCount()); m = new BlockFieldMatrix(FractionField.getInstance(), rows, columns); m.walkInOptimizedOrder(new SetVisitor(), 1, rows - 2, 1, columns - 2); getVisitor = new GetVisitor(); m.walkInColumnOrder(getVisitor, 1, rows - 2, 1, columns - 2); Assert.assertEquals((rows - 2) * (columns - 2), getVisitor.getCount()); for (int i = 0; i < rows; ++i) { Assert.assertEquals(new Fraction(0), m.getEntry(i, 0)); Assert.assertEquals(new Fraction(0), m.getEntry(i, columns - 1)); } for (int j = 0; j < columns; ++j) { Assert.assertEquals(new Fraction(0), m.getEntry(0, j)); Assert.assertEquals(new Fraction(0), m.getEntry(rows - 1, j)); } } @Test public void testSerial() { BlockFieldMatrix m = new BlockFieldMatrix(testData); Assert.assertEquals(m,TestUtils.serializeAndRecover(m)); } private static class SetVisitor extends DefaultFieldMatrixChangingVisitor { public SetVisitor() { super(Fraction.ZERO); } @Override public Fraction visit(int i, int j, Fraction value) { return new Fraction(i * 11 + j, 11); } } private static class GetVisitor extends DefaultFieldMatrixPreservingVisitor { private int count; public GetVisitor() { super(Fraction.ZERO); count = 0; } @Override public void visit(int i, int j, Fraction value) { ++count; Assert.assertEquals(new Fraction(i * 11 + j, 11), value); } public int getCount() { return count; } } private BlockFieldMatrix createRandomMatrix(Random r, int rows, int columns) { BlockFieldMatrix m = new BlockFieldMatrix(FractionField.getInstance(), rows, columns); for (int i = 0; i < rows; ++i) { for (int j = 0; j < columns; ++j) { int p = r.nextInt(20) - 10; int q = r.nextInt(20) - 10; if (q == 0) { q = 1; } m.setEntry(i, j, new Fraction(p, q)); } } return m; } } commons-math3-3.2-src/src/test/java/org/apache/commons/math3/linear/QRSolverTest.java100644 1750 1750 16474 12126627674 27312 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.linear; import java.util.Random; import org.apache.commons.math3.exception.MathIllegalArgumentException; import org.junit.Test; import org.junit.Assert; public class QRSolverTest { double[][] testData3x3NonSingular = { { 12, -51, 4 }, { 6, 167, -68 }, { -4, 24, -41 } }; double[][] testData3x3Singular = { { 1, 2, 2 }, { 2, 4, 6 }, { 4, 8, 12 } }; double[][] testData3x4 = { { 12, -51, 4, 1 }, { 6, 167, -68, 2 }, { -4, 24, -41, 3 } }; double[][] testData4x3 = { { 12, -51, 4 }, { 6, 167, -68 }, { -4, 24, -41 }, { -5, 34, 7 } }; /** test rank */ @Test public void testRank() { DecompositionSolver solver = new QRDecomposition(MatrixUtils.createRealMatrix(testData3x3NonSingular)).getSolver(); Assert.assertTrue(solver.isNonSingular()); solver = new QRDecomposition(MatrixUtils.createRealMatrix(testData3x3Singular)).getSolver(); Assert.assertFalse(solver.isNonSingular()); solver = new QRDecomposition(MatrixUtils.createRealMatrix(testData3x4)).getSolver(); Assert.assertTrue(solver.isNonSingular()); solver = new QRDecomposition(MatrixUtils.createRealMatrix(testData4x3)).getSolver(); Assert.assertTrue(solver.isNonSingular()); } /** test solve dimension errors */ @Test public void testSolveDimensionErrors() { DecompositionSolver solver = new QRDecomposition(MatrixUtils.createRealMatrix(testData3x3NonSingular)).getSolver(); RealMatrix b = MatrixUtils.createRealMatrix(new double[2][2]); try { solver.solve(b); Assert.fail("an exception should have been thrown"); } catch (MathIllegalArgumentException iae) { // expected behavior } try { solver.solve(b.getColumnVector(0)); Assert.fail("an exception should have been thrown"); } catch (MathIllegalArgumentException iae) { // expected behavior } } /** test solve rank errors */ @Test public void testSolveRankErrors() { DecompositionSolver solver = new QRDecomposition(MatrixUtils.createRealMatrix(testData3x3Singular)).getSolver(); RealMatrix b = MatrixUtils.createRealMatrix(new double[3][2]); try { solver.solve(b); Assert.fail("an exception should have been thrown"); } catch (SingularMatrixException iae) { // expected behavior } try { solver.solve(b.getColumnVector(0)); Assert.fail("an exception should have been thrown"); } catch (SingularMatrixException iae) { // expected behavior } } /** test solve */ @Test public void testSolve() { QRDecomposition decomposition = new QRDecomposition(MatrixUtils.createRealMatrix(testData3x3NonSingular)); DecompositionSolver solver = decomposition.getSolver(); RealMatrix b = MatrixUtils.createRealMatrix(new double[][] { { -102, 12250 }, { 544, 24500 }, { 167, -36750 } }); RealMatrix xRef = MatrixUtils.createRealMatrix(new double[][] { { 1, 2515 }, { 2, 422 }, { -3, 898 } }); // using RealMatrix Assert.assertEquals(0, solver.solve(b).subtract(xRef).getNorm(), 2.0e-16 * xRef.getNorm()); // using ArrayRealVector for (int i = 0; i < b.getColumnDimension(); ++i) { final RealVector x = solver.solve(b.getColumnVector(i)); final double error = x.subtract(xRef.getColumnVector(i)).getNorm(); Assert.assertEquals(0, error, 3.0e-16 * xRef.getColumnVector(i).getNorm()); } // using RealVector with an alternate implementation for (int i = 0; i < b.getColumnDimension(); ++i) { ArrayRealVectorTest.RealVectorTestImpl v = new ArrayRealVectorTest.RealVectorTestImpl(b.getColumn(i)); final RealVector x = solver.solve(v); final double error = x.subtract(xRef.getColumnVector(i)).getNorm(); Assert.assertEquals(0, error, 3.0e-16 * xRef.getColumnVector(i).getNorm()); } } @Test public void testOverdetermined() { final Random r = new Random(5559252868205245l); int p = (7 * BlockRealMatrix.BLOCK_SIZE) / 4; int q = (5 * BlockRealMatrix.BLOCK_SIZE) / 4; RealMatrix a = createTestMatrix(r, p, q); RealMatrix xRef = createTestMatrix(r, q, BlockRealMatrix.BLOCK_SIZE + 3); // build a perturbed system: A.X + noise = B RealMatrix b = a.multiply(xRef); final double noise = 0.001; b.walkInOptimizedOrder(new DefaultRealMatrixChangingVisitor() { @Override public double visit(int row, int column, double value) { return value * (1.0 + noise * (2 * r.nextDouble() - 1)); } }); // despite perturbation, the least square solution should be pretty good RealMatrix x = new QRDecomposition(a).getSolver().solve(b); Assert.assertEquals(0, x.subtract(xRef).getNorm(), 0.01 * noise * p * q); } @Test public void testUnderdetermined() { final Random r = new Random(42185006424567123l); int p = (5 * BlockRealMatrix.BLOCK_SIZE) / 4; int q = (7 * BlockRealMatrix.BLOCK_SIZE) / 4; RealMatrix a = createTestMatrix(r, p, q); RealMatrix xRef = createTestMatrix(r, q, BlockRealMatrix.BLOCK_SIZE + 3); RealMatrix b = a.multiply(xRef); RealMatrix x = new QRDecomposition(a).getSolver().solve(b); // too many equations, the system cannot be solved at all Assert.assertTrue(x.subtract(xRef).getNorm() / (p * q) > 0.01); // the last unknown should have been set to 0 Assert.assertEquals(0.0, x.getSubMatrix(p, q - 1, 0, x.getColumnDimension() - 1).getNorm(), 0); } private RealMatrix createTestMatrix(final Random r, final int rows, final int columns) { RealMatrix m = MatrixUtils.createRealMatrix(rows, columns); m.walkInOptimizedOrder(new DefaultRealMatrixChangingVisitor() { @Override public double visit(int row, int column, double value) { return 2.0 * r.nextDouble() - 1.0; } }); return m; } } ././@LongLink100644 0 0 150 12126630646 10255 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/linear/SingularValueDecompositionTest.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/linear/SingularValueDecompositionTest.j100644 1750 1750 33005 12126627674 32410 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.linear; import java.io.BufferedReader; import java.io.DataInputStream; import java.io.IOException; import java.io.InputStreamReader; import java.util.Random; import org.junit.Assert; import org.junit.Test; public class SingularValueDecompositionTest { private double[][] testSquare = { { 24.0 / 25.0, 43.0 / 25.0 }, { 57.0 / 25.0, 24.0 / 25.0 } }; private double[][] testNonSquare = { { -540.0 / 625.0, 963.0 / 625.0, -216.0 / 625.0 }, { -1730.0 / 625.0, -744.0 / 625.0, 1008.0 / 625.0 }, { -720.0 / 625.0, 1284.0 / 625.0, -288.0 / 625.0 }, { -360.0 / 625.0, 192.0 / 625.0, 1756.0 / 625.0 }, }; private static final double normTolerance = 10e-14; @Test public void testMoreRows() { final double[] singularValues = { 123.456, 2.3, 1.001, 0.999 }; final int rows = singularValues.length + 2; final int columns = singularValues.length; Random r = new Random(15338437322523l); SingularValueDecomposition svd = new SingularValueDecomposition(createTestMatrix(r, rows, columns, singularValues)); double[] computedSV = svd.getSingularValues(); Assert.assertEquals(singularValues.length, computedSV.length); for (int i = 0; i < singularValues.length; ++i) { Assert.assertEquals(singularValues[i], computedSV[i], 1.0e-10); } } @Test public void testMoreColumns() { final double[] singularValues = { 123.456, 2.3, 1.001, 0.999 }; final int rows = singularValues.length; final int columns = singularValues.length + 2; Random r = new Random(732763225836210l); SingularValueDecomposition svd = new SingularValueDecomposition(createTestMatrix(r, rows, columns, singularValues)); double[] computedSV = svd.getSingularValues(); Assert.assertEquals(singularValues.length, computedSV.length); for (int i = 0; i < singularValues.length; ++i) { Assert.assertEquals(singularValues[i], computedSV[i], 1.0e-10); } } /** test dimensions */ @Test public void testDimensions() { RealMatrix matrix = MatrixUtils.createRealMatrix(testSquare); final int m = matrix.getRowDimension(); final int n = matrix.getColumnDimension(); SingularValueDecomposition svd = new SingularValueDecomposition(matrix); Assert.assertEquals(m, svd.getU().getRowDimension()); Assert.assertEquals(m, svd.getU().getColumnDimension()); Assert.assertEquals(m, svd.getS().getColumnDimension()); Assert.assertEquals(n, svd.getS().getColumnDimension()); Assert.assertEquals(n, svd.getV().getRowDimension()); Assert.assertEquals(n, svd.getV().getColumnDimension()); } /** Test based on a dimension 4 Hadamard matrix. */ @Test public void testHadamard() { RealMatrix matrix = new Array2DRowRealMatrix(new double[][] { {15.0 / 2.0, 5.0 / 2.0, 9.0 / 2.0, 3.0 / 2.0 }, { 5.0 / 2.0, 15.0 / 2.0, 3.0 / 2.0, 9.0 / 2.0 }, { 9.0 / 2.0, 3.0 / 2.0, 15.0 / 2.0, 5.0 / 2.0 }, { 3.0 / 2.0, 9.0 / 2.0, 5.0 / 2.0, 15.0 / 2.0 } }, false); SingularValueDecomposition svd = new SingularValueDecomposition(matrix); Assert.assertEquals(16.0, svd.getSingularValues()[0], 1.0e-14); Assert.assertEquals( 8.0, svd.getSingularValues()[1], 1.0e-14); Assert.assertEquals( 4.0, svd.getSingularValues()[2], 1.0e-14); Assert.assertEquals( 2.0, svd.getSingularValues()[3], 1.0e-14); RealMatrix fullCovariance = new Array2DRowRealMatrix(new double[][] { { 85.0 / 1024, -51.0 / 1024, -75.0 / 1024, 45.0 / 1024 }, { -51.0 / 1024, 85.0 / 1024, 45.0 / 1024, -75.0 / 1024 }, { -75.0 / 1024, 45.0 / 1024, 85.0 / 1024, -51.0 / 1024 }, { 45.0 / 1024, -75.0 / 1024, -51.0 / 1024, 85.0 / 1024 } }, false); Assert.assertEquals(0.0, fullCovariance.subtract(svd.getCovariance(0.0)).getNorm(), 1.0e-14); RealMatrix halfCovariance = new Array2DRowRealMatrix(new double[][] { { 5.0 / 1024, -3.0 / 1024, 5.0 / 1024, -3.0 / 1024 }, { -3.0 / 1024, 5.0 / 1024, -3.0 / 1024, 5.0 / 1024 }, { 5.0 / 1024, -3.0 / 1024, 5.0 / 1024, -3.0 / 1024 }, { -3.0 / 1024, 5.0 / 1024, -3.0 / 1024, 5.0 / 1024 } }, false); Assert.assertEquals(0.0, halfCovariance.subtract(svd.getCovariance(6.0)).getNorm(), 1.0e-14); } /** test A = USVt */ @Test public void testAEqualUSVt() { checkAEqualUSVt(MatrixUtils.createRealMatrix(testSquare)); checkAEqualUSVt(MatrixUtils.createRealMatrix(testNonSquare)); checkAEqualUSVt(MatrixUtils.createRealMatrix(testNonSquare).transpose()); } public void checkAEqualUSVt(final RealMatrix matrix) { SingularValueDecomposition svd = new SingularValueDecomposition(matrix); RealMatrix u = svd.getU(); RealMatrix s = svd.getS(); RealMatrix v = svd.getV(); double norm = u.multiply(s).multiply(v.transpose()).subtract(matrix).getNorm(); Assert.assertEquals(0, norm, normTolerance); } /** test that U is orthogonal */ @Test public void testUOrthogonal() { checkOrthogonal(new SingularValueDecomposition(MatrixUtils.createRealMatrix(testSquare)).getU()); checkOrthogonal(new SingularValueDecomposition(MatrixUtils.createRealMatrix(testNonSquare)).getU()); checkOrthogonal(new SingularValueDecomposition(MatrixUtils.createRealMatrix(testNonSquare).transpose()).getU()); } /** test that V is orthogonal */ @Test public void testVOrthogonal() { checkOrthogonal(new SingularValueDecomposition(MatrixUtils.createRealMatrix(testSquare)).getV()); checkOrthogonal(new SingularValueDecomposition(MatrixUtils.createRealMatrix(testNonSquare)).getV()); checkOrthogonal(new SingularValueDecomposition(MatrixUtils.createRealMatrix(testNonSquare).transpose()).getV()); } public void checkOrthogonal(final RealMatrix m) { RealMatrix mTm = m.transpose().multiply(m); RealMatrix id = MatrixUtils.createRealIdentityMatrix(mTm.getRowDimension()); Assert.assertEquals(0, mTm.subtract(id).getNorm(), normTolerance); } /** test matrices values */ // This test is useless since whereas the columns of U and V are linked // together, the actual triplet (U,S,V) is not uniquely defined. public void testMatricesValues1() { SingularValueDecomposition svd = new SingularValueDecomposition(MatrixUtils.createRealMatrix(testSquare)); RealMatrix uRef = MatrixUtils.createRealMatrix(new double[][] { { 3.0 / 5.0, -4.0 / 5.0 }, { 4.0 / 5.0, 3.0 / 5.0 } }); RealMatrix sRef = MatrixUtils.createRealMatrix(new double[][] { { 3.0, 0.0 }, { 0.0, 1.0 } }); RealMatrix vRef = MatrixUtils.createRealMatrix(new double[][] { { 4.0 / 5.0, 3.0 / 5.0 }, { 3.0 / 5.0, -4.0 / 5.0 } }); // check values against known references RealMatrix u = svd.getU(); Assert.assertEquals(0, u.subtract(uRef).getNorm(), normTolerance); RealMatrix s = svd.getS(); Assert.assertEquals(0, s.subtract(sRef).getNorm(), normTolerance); RealMatrix v = svd.getV(); Assert.assertEquals(0, v.subtract(vRef).getNorm(), normTolerance); // check the same cached instance is returned the second time Assert.assertTrue(u == svd.getU()); Assert.assertTrue(s == svd.getS()); Assert.assertTrue(v == svd.getV()); } /** test matrices values */ // This test is useless since whereas the columns of U and V are linked // together, the actual triplet (U,S,V) is not uniquely defined. public void useless_testMatricesValues2() { RealMatrix uRef = MatrixUtils.createRealMatrix(new double[][] { { 0.0 / 5.0, 3.0 / 5.0, 0.0 / 5.0 }, { -4.0 / 5.0, 0.0 / 5.0, -3.0 / 5.0 }, { 0.0 / 5.0, 4.0 / 5.0, 0.0 / 5.0 }, { -3.0 / 5.0, 0.0 / 5.0, 4.0 / 5.0 } }); RealMatrix sRef = MatrixUtils.createRealMatrix(new double[][] { { 4.0, 0.0, 0.0 }, { 0.0, 3.0, 0.0 }, { 0.0, 0.0, 2.0 } }); RealMatrix vRef = MatrixUtils.createRealMatrix(new double[][] { { 80.0 / 125.0, -60.0 / 125.0, 75.0 / 125.0 }, { 24.0 / 125.0, 107.0 / 125.0, 60.0 / 125.0 }, { -93.0 / 125.0, -24.0 / 125.0, 80.0 / 125.0 } }); // check values against known references SingularValueDecomposition svd = new SingularValueDecomposition(MatrixUtils.createRealMatrix(testNonSquare)); RealMatrix u = svd.getU(); Assert.assertEquals(0, u.subtract(uRef).getNorm(), normTolerance); RealMatrix s = svd.getS(); Assert.assertEquals(0, s.subtract(sRef).getNorm(), normTolerance); RealMatrix v = svd.getV(); Assert.assertEquals(0, v.subtract(vRef).getNorm(), normTolerance); // check the same cached instance is returned the second time Assert.assertTrue(u == svd.getU()); Assert.assertTrue(s == svd.getS()); Assert.assertTrue(v == svd.getV()); } /** test MATH-465 */ @Test public void testRank() { double[][] d = { { 1, 1, 1 }, { 0, 0, 0 }, { 1, 2, 3 } }; RealMatrix m = new Array2DRowRealMatrix(d); SingularValueDecomposition svd = new SingularValueDecomposition(m); Assert.assertEquals(2, svd.getRank()); } /** test MATH-583 */ @Test public void testStability1() { RealMatrix m = new Array2DRowRealMatrix(201, 201); loadRealMatrix(m,"matrix1.csv"); try { new SingularValueDecomposition(m); } catch (Exception e) { Assert.fail("Exception whilst constructing SVD"); } } /** test MATH-327 */ @Test public void testStability2() { RealMatrix m = new Array2DRowRealMatrix(7, 168); loadRealMatrix(m,"matrix2.csv"); try { new SingularValueDecomposition(m); } catch (Throwable e) { Assert.fail("Exception whilst constructing SVD"); } } private void loadRealMatrix(RealMatrix m, String resourceName) { try { DataInputStream in = new DataInputStream(getClass().getResourceAsStream(resourceName)); BufferedReader br = new BufferedReader(new InputStreamReader(in)); String strLine; int row = 0; while ((strLine = br.readLine()) != null) { if (!strLine.startsWith("#")) { int col = 0; for (String entry : strLine.split(",")) { m.setEntry(row, col++, Double.parseDouble(entry)); } row++; } } in.close(); } catch (IOException e) {} } /** test condition number */ @Test public void testConditionNumber() { SingularValueDecomposition svd = new SingularValueDecomposition(MatrixUtils.createRealMatrix(testSquare)); // replace 1.0e-15 with 1.5e-15 Assert.assertEquals(3.0, svd.getConditionNumber(), 1.5e-15); } @Test public void testInverseConditionNumber() { SingularValueDecomposition svd = new SingularValueDecomposition(MatrixUtils.createRealMatrix(testSquare)); Assert.assertEquals(1.0/3.0, svd.getInverseConditionNumber(), 1.5e-15); } private RealMatrix createTestMatrix(final Random r, final int rows, final int columns, final double[] singularValues) { final RealMatrix u = EigenDecompositionTest.createOrthogonalMatrix(r, rows); final RealMatrix d = new Array2DRowRealMatrix(rows, columns); d.setSubMatrix(MatrixUtils.createRealDiagonalMatrix(singularValues).getData(), 0, 0); final RealMatrix v = EigenDecompositionTest.createOrthogonalMatrix(r, columns); return u.multiply(d).multiply(v); } @Test public void testIssue947() { double[][] nans = new double[][] { { Double.NaN, Double.NaN }, { Double.NaN, Double.NaN } }; RealMatrix m = new Array2DRowRealMatrix(nans, false); SingularValueDecomposition svd = new SingularValueDecomposition(m); Assert.assertTrue(Double.isNaN(svd.getSingularValues()[0])); Assert.assertTrue(Double.isNaN(svd.getSingularValues()[1])); } } commons-math3-3.2-src/src/test/java/org/apache/commons/math3/linear/RRQRSolverTest.java100644 1750 1750 17014 12126627674 27545 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.linear; import java.util.Random; import org.apache.commons.math3.exception.MathIllegalArgumentException; import org.junit.Test; import org.junit.Assert; public class RRQRSolverTest { double[][] testData3x3NonSingular = { { 12, -51, 4 }, { 6, 167, -68 }, { -4, 24, -41 } }; double[][] testData3x3Singular = { { 1, 2, 2 }, { 2, 4, 6 }, { 4, 8, 12 } }; double[][] testData3x4 = { { 12, -51, 4, 1 }, { 6, 167, -68, 2 }, { -4, 24, -41, 3 } }; double[][] testData4x3 = { { 12, -51, 4 }, { 6, 167, -68 }, { -4, 24, -41 }, { -5, 34, 7 } }; /** test rank */ @Test public void testRank() { DecompositionSolver solver = new RRQRDecomposition(MatrixUtils.createRealMatrix(testData3x3NonSingular), 1.0e-16).getSolver(); Assert.assertTrue(solver.isNonSingular()); solver = new RRQRDecomposition(MatrixUtils.createRealMatrix(testData3x3Singular), 1.0e-16).getSolver(); Assert.assertFalse(solver.isNonSingular()); solver = new RRQRDecomposition(MatrixUtils.createRealMatrix(testData3x4), 1.0e-16).getSolver(); Assert.assertTrue(solver.isNonSingular()); solver = new RRQRDecomposition(MatrixUtils.createRealMatrix(testData4x3), 1.0e-16).getSolver(); Assert.assertTrue(solver.isNonSingular()); } /** test solve dimension errors */ @Test public void testSolveDimensionErrors() { DecompositionSolver solver = new RRQRDecomposition(MatrixUtils.createRealMatrix(testData3x3NonSingular)).getSolver(); RealMatrix b = MatrixUtils.createRealMatrix(new double[2][2]); try { solver.solve(b); Assert.fail("an exception should have been thrown"); } catch (MathIllegalArgumentException iae) { // expected behavior } try { solver.solve(b.getColumnVector(0)); Assert.fail("an exception should have been thrown"); } catch (MathIllegalArgumentException iae) { // expected behavior } } /** test solve rank errors */ @Test public void testSolveRankErrors() { DecompositionSolver solver = new RRQRDecomposition(MatrixUtils.createRealMatrix(testData3x3Singular), 1.0e-16).getSolver(); RealMatrix b = MatrixUtils.createRealMatrix(new double[3][2]); try { solver.solve(b); Assert.fail("an exception should have been thrown"); } catch (SingularMatrixException iae) { // expected behavior } try { solver.solve(b.getColumnVector(0)); Assert.fail("an exception should have been thrown"); } catch (SingularMatrixException iae) { // expected behavior } } /** test solve */ @Test public void testSolve() { RealMatrix b = MatrixUtils.createRealMatrix(new double[][] { { -102, 12250 }, { 544, 24500 }, { 167, -36750 } }); RealMatrix xRef = MatrixUtils.createRealMatrix(new double[][] { { 1, 2515 }, { 2, 422 }, { -3, 898 } }); RRQRDecomposition decomposition = new RRQRDecomposition(MatrixUtils.createRealMatrix(testData3x3NonSingular)); DecompositionSolver solver = decomposition.getSolver(); // using RealMatrix Assert.assertEquals(0, solver.solve(b).subtract(xRef).getNorm(), 3.0e-16 * xRef.getNorm()); // using ArrayRealVector for (int i = 0; i < b.getColumnDimension(); ++i) { final RealVector x = solver.solve(b.getColumnVector(i)); final double error = x.subtract(xRef.getColumnVector(i)).getNorm(); Assert.assertEquals(0, error, 3.0e-16 * xRef.getColumnVector(i).getNorm()); } // using RealVector with an alternate implementation for (int i = 0; i < b.getColumnDimension(); ++i) { ArrayRealVectorTest.RealVectorTestImpl v = new ArrayRealVectorTest.RealVectorTestImpl(b.getColumn(i)); final RealVector x = solver.solve(v); final double error = x.subtract(xRef.getColumnVector(i)).getNorm(); Assert.assertEquals(0, error, 3.0e-16 * xRef.getColumnVector(i).getNorm()); } } @Test public void testOverdetermined() { final Random r = new Random(5559252868205245l); int p = (7 * BlockRealMatrix.BLOCK_SIZE) / 4; int q = (5 * BlockRealMatrix.BLOCK_SIZE) / 4; RealMatrix a = createTestMatrix(r, p, q); RealMatrix xRef = createTestMatrix(r, q, BlockRealMatrix.BLOCK_SIZE + 3); // build a perturbed system: A.X + noise = B RealMatrix b = a.multiply(xRef); final double noise = 0.001; b.walkInOptimizedOrder(new DefaultRealMatrixChangingVisitor() { @Override public double visit(int row, int column, double value) { return value * (1.0 + noise * (2 * r.nextDouble() - 1)); } }); // despite perturbation, the least square solution should be pretty good RealMatrix x = new RRQRDecomposition(a).getSolver().solve(b); Assert.assertEquals(0, x.subtract(xRef).getNorm(), 0.01 * noise * p * q); } @Test public void testUnderdetermined() { final Random r = new Random(42185006424567123l); int p = (5 * BlockRealMatrix.BLOCK_SIZE) / 4; int q = (7 * BlockRealMatrix.BLOCK_SIZE) / 4; RealMatrix a = createTestMatrix(r, p, q); RealMatrix xRef = createTestMatrix(r, q, BlockRealMatrix.BLOCK_SIZE + 3); RealMatrix b = a.multiply(xRef); RRQRDecomposition rrqrd = new RRQRDecomposition(a); RealMatrix x = rrqrd.getSolver().solve(b); // too many equations, the system cannot be solved at all Assert.assertTrue(x.subtract(xRef).getNorm() / (p * q) > 0.01); // the last permuted unknown should have been set to 0 RealMatrix permuted = rrqrd.getP().transpose().multiply(x); Assert.assertEquals(0.0, permuted.getSubMatrix(p, q - 1, 0, permuted.getColumnDimension() - 1).getNorm(), 0); } private RealMatrix createTestMatrix(final Random r, final int rows, final int columns) { RealMatrix m = MatrixUtils.createRealMatrix(rows, columns); m.walkInOptimizedOrder(new DefaultRealMatrixChangingVisitor() { @Override public double visit(int row, int column, double value) { return 2.0 * r.nextDouble() - 1.0; } }); return m; } } commons-math3-3.2-src/src/test/java/org/apache/commons/math3/linear/RealVectorTest.java100644 1750 1750 14462 12126627674 27636 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.linear; import java.util.Iterator; import org.apache.commons.math3.linear.RealVector.Entry; import org.junit.Assert; import org.junit.Ignore; import org.junit.Test; /** * Tests for {@link RealVector}. */ public class RealVectorTest extends RealVectorAbstractTest{ @Override public RealVector create(final double[] data) { return new RealVectorTestImpl(data); } @Test @Ignore("Abstract class RealVector does not implement append(RealVector).") @Override public void testAppendVector() { // Do nothing } @Test @Ignore("Abstract class RealVector does not implement append(double)") @Override public void testAppendScalar() { // Do nothing } @Test @Ignore("Abstract class RealVector does not implement getSubvector(int, int)") @Override public void testGetSubVector() { // Do nothing } @Test @Ignore("Abstract class RealVector does not implement getSubvector(int, int)") @Override public void testGetSubVectorInvalidIndex1() { // Do nothing } @Test @Ignore("Abstract class RealVector does not implement getSubvector(int, int)") @Override public void testGetSubVectorInvalidIndex2() { // Do nothing } @Test @Ignore("Abstract class RealVector does not implement getSubvector(int, int)") @Override public void testGetSubVectorInvalidIndex3() { // Do nothing } @Test @Ignore("Abstract class RealVector does not implement getSubvector(int, int)") @Override public void testGetSubVectorInvalidIndex4() { // Do nothing } @Test @Ignore("Abstract class RealVector does not implement setSubvector(int, RealVector)") @Override public void testSetSubVectorSameType() { // Do nothing } @Test @Ignore("Abstract class RealVector does not implement setSubvector(int, RealVector)") @Override public void testSetSubVectorMixedType() { // Do nothing } @Test @Ignore("Abstract class RealVector does not implement setSubvector(int, RealVector)") @Override public void testSetSubVectorInvalidIndex1() { // Do nothing } @Test @Ignore("Abstract class RealVector does not implement setSubvector(int, RealVector)") @Override public void testSetSubVectorInvalidIndex2() { // Do nothing } @Test @Ignore("Abstract class RealVector does not implement setSubvector(int, RealVector)") @Override public void testSetSubVectorInvalidIndex3() { // Do nothing } @Test @Ignore("Abstract class RealVector does not implement isNaN()") @Override public void testIsNaN() { // Do nothing } @Test @Ignore("Abstract class RealVector does not implement isNaN()") @Override public void testIsInfinite() { // Do nothing } @Test @Ignore("Abstract class RealVector does not implement ebeMultiply(RealVector)") @Override public void testEbeMultiplySameType() { // Do nothing } @Test @Ignore("Abstract class RealVector does not implement ebeMultiply(RealVector)") @Override public void testEbeMultiplyMixedTypes() { // Do nothing } @Test @Ignore("Abstract class RealVector does not implement ebeMultiply(RealVector)") @Override public void testEbeMultiplyDimensionMismatch() { // Do nothing } @Test @Ignore("Abstract class RealVector does not implement ebeDivide(RealVector)") @Override public void testEbeDivideSameType() { // Do nothing } @Test @Ignore("Abstract class RealVector does not implement ebeDivide(RealVector)") @Override public void testEbeDivideMixedTypes() { // Do nothing } @Test @Ignore("Abstract class RealVector does not implement ebeDivide(RealVector)") @Override public void testEbeDivideDimensionMismatch() { // Do nothing } @Test @Ignore("Abstract class RealVector does not implement getL1Norm()") @Override public void testGetL1Norm() { // Do nothing } @Test @Ignore("Abstract class RealVector does not implement getLInfNorm()") @Override public void testGetLInfNorm() { // Do nothing } @Test public void testSparseIterator() { /* * For non-default values, use x + 1, x + 2, etc... to make sure that * these values are really different from x. */ final double x = getPreferredEntryValue(); final double[] data = { x, x + 1d, x, x, x + 2d, x + 3d, x + 4d, x, x, x, x + 5d, x + 6d, x }; RealVector v = create(data); Entry e; int i = 0; final double[] nonDefault = { x + 1d, x + 2d, x + 3d, x + 4d, x + 5d, x + 6d }; for (Iterator it = v.sparseIterator(); it.hasNext(); i++) { e = it.next(); Assert.assertEquals(nonDefault[i], e.getValue(), 0); } double [] onlyOne = {x, x + 1d, x}; v = create(onlyOne); for(Iterator it = v.sparseIterator(); it.hasNext(); ) { e = it.next(); Assert.assertEquals(onlyOne[1], e.getValue(), 0); } } @Test @Ignore("Abstract class RealVector is not serializable.") @Override public void testSerial() { // Do nothing } @Test @Ignore("Abstract class RealVector does not override equals(Object).") @Override public void testEquals() { // Do nothing } } commons-math3-3.2-src/src/test/java/org/apache/commons/math3/linear/DiagonalMatrixTest.java100644 1750 1750 27037 12126627674 30475 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.linear; import org.apache.commons.math3.TestUtils; import org.apache.commons.math3.exception.DimensionMismatchException; import org.apache.commons.math3.exception.NullArgumentException; import org.apache.commons.math3.exception.NumberIsTooLargeException; import org.apache.commons.math3.exception.OutOfRangeException; import org.apache.commons.math3.util.Precision; import org.junit.Assert; import org.junit.Test; /** * Test cases for the {@link DiagonalMatrix} class. */ public class DiagonalMatrixTest { @Test public void testConstructor1() { final int dim = 3; final DiagonalMatrix m = new DiagonalMatrix(dim); Assert.assertEquals(dim, m.getRowDimension()); Assert.assertEquals(dim, m.getColumnDimension()); } @Test public void testConstructor2() { final double[] d = { -1.2, 3.4, 5 }; final DiagonalMatrix m = new DiagonalMatrix(d); for (int i = 0; i < m.getRowDimension(); i++) { for (int j = 0; j < m.getRowDimension(); j++) { if (i == j) { Assert.assertEquals(d[i], m.getEntry(i, j), 0d); } else { Assert.assertEquals(0d, m.getEntry(i, j), 0d); } } } // Check that the underlying was copied. d[0] = 0; Assert.assertFalse(d[0] == m.getEntry(0, 0)); } @Test public void testConstructor3() { final double[] d = { -1.2, 3.4, 5 }; final DiagonalMatrix m = new DiagonalMatrix(d, false); for (int i = 0; i < m.getRowDimension(); i++) { for (int j = 0; j < m.getRowDimension(); j++) { if (i == j) { Assert.assertEquals(d[i], m.getEntry(i, j), 0d); } else { Assert.assertEquals(0d, m.getEntry(i, j), 0d); } } } // Check that the underlying is referenced. d[0] = 0; Assert.assertTrue(d[0] == m.getEntry(0, 0)); } @Test(expected=DimensionMismatchException.class) public void testCreateError() { final double[] d = { -1.2, 3.4, 5 }; final DiagonalMatrix m = new DiagonalMatrix(d, false); m.createMatrix(5, 3); } @Test public void testCreate() { final double[] d = { -1.2, 3.4, 5 }; final DiagonalMatrix m = new DiagonalMatrix(d, false); final RealMatrix p = m.createMatrix(5, 5); Assert.assertTrue(p instanceof DiagonalMatrix); Assert.assertEquals(5, p.getRowDimension()); Assert.assertEquals(5, p.getColumnDimension()); } @Test public void testCopy() { final double[] d = { -1.2, 3.4, 5 }; final DiagonalMatrix m = new DiagonalMatrix(d, false); final DiagonalMatrix p = (DiagonalMatrix) m.copy(); for (int i = 0; i < m.getRowDimension(); ++i) { Assert.assertEquals(m.getEntry(i, i), p.getEntry(i, i), 1.0e-20); } } @Test public void testGetData() { final double[] data = { -1.2, 3.4, 5 }; final int dim = 3; final DiagonalMatrix m = new DiagonalMatrix(dim); for (int i = 0; i < dim; i++) { m.setEntry(i, i, data[i]); } final double[][] out = m.getData(); Assert.assertEquals(dim, out.length); for (int i = 0; i < m.getRowDimension(); i++) { Assert.assertEquals(dim, out[i].length); for (int j = 0; j < m.getRowDimension(); j++) { if (i == j) { Assert.assertEquals(data[i], out[i][j], 0d); } else { Assert.assertEquals(0d, out[i][j], 0d); } } } } @Test public void testAdd() { final double[] data1 = { -1.2, 3.4, 5 }; final DiagonalMatrix m1 = new DiagonalMatrix(data1); final double[] data2 = { 10.1, 2.3, 45 }; final DiagonalMatrix m2 = new DiagonalMatrix(data2); final DiagonalMatrix result = m1.add(m2); Assert.assertEquals(m1.getRowDimension(), result.getRowDimension()); for (int i = 0; i < result.getRowDimension(); i++) { for (int j = 0; j < result.getRowDimension(); j++) { if (i == j) { Assert.assertEquals(data1[i] + data2[i], result.getEntry(i, j), 0d); } else { Assert.assertEquals(0d, result.getEntry(i, j), 0d); } } } } @Test public void testSubtract() { final double[] data1 = { -1.2, 3.4, 5 }; final DiagonalMatrix m1 = new DiagonalMatrix(data1); final double[] data2 = { 10.1, 2.3, 45 }; final DiagonalMatrix m2 = new DiagonalMatrix(data2); final DiagonalMatrix result = m1.subtract(m2); Assert.assertEquals(m1.getRowDimension(), result.getRowDimension()); for (int i = 0; i < result.getRowDimension(); i++) { for (int j = 0; j < result.getRowDimension(); j++) { if (i == j) { Assert.assertEquals(data1[i] - data2[i], result.getEntry(i, j), 0d); } else { Assert.assertEquals(0d, result.getEntry(i, j), 0d); } } } } @Test public void testAddToEntry() { final double[] data = { -1.2, 3.4, 5 }; final DiagonalMatrix m = new DiagonalMatrix(data); for (int i = 0; i < m.getRowDimension(); i++) { m.addToEntry(i, i, i); Assert.assertEquals(data[i] + i, m.getEntry(i, i), 0d); } } @Test public void testMultiplyEntry() { final double[] data = { -1.2, 3.4, 5 }; final DiagonalMatrix m = new DiagonalMatrix(data); for (int i = 0; i < m.getRowDimension(); i++) { m.multiplyEntry(i, i, i); Assert.assertEquals(data[i] * i, m.getEntry(i, i), 0d); } } @Test public void testMultiply1() { final double[] data1 = { -1.2, 3.4, 5 }; final DiagonalMatrix m1 = new DiagonalMatrix(data1); final double[] data2 = { 10.1, 2.3, 45 }; final DiagonalMatrix m2 = new DiagonalMatrix(data2); final DiagonalMatrix result = (DiagonalMatrix) m1.multiply((RealMatrix) m2); Assert.assertEquals(m1.getRowDimension(), result.getRowDimension()); for (int i = 0; i < result.getRowDimension(); i++) { for (int j = 0; j < result.getRowDimension(); j++) { if (i == j) { Assert.assertEquals(data1[i] * data2[i], result.getEntry(i, j), 0d); } else { Assert.assertEquals(0d, result.getEntry(i, j), 0d); } } } } @Test public void testMultiply2() { final double[] data1 = { -1.2, 3.4, 5 }; final DiagonalMatrix diag1 = new DiagonalMatrix(data1); final double[][] data2 = { { -1.2, 3.4 }, { -5.6, 7.8 }, { 9.1, 2.3 } }; final RealMatrix dense2 = new Array2DRowRealMatrix(data2); final RealMatrix dense1 = new Array2DRowRealMatrix(diag1.getData()); final RealMatrix diagResult = diag1.multiply(dense2); final RealMatrix denseResult = dense1.multiply(dense2); for (int i = 0; i < dense1.getRowDimension(); i++) { for (int j = 0; j < dense2.getColumnDimension(); j++) { Assert.assertEquals(denseResult.getEntry(i, j), diagResult.getEntry(i, j), 0d); } } } @Test public void testOperate() { final double[] data = { -1.2, 3.4, 5 }; final DiagonalMatrix diag = new DiagonalMatrix(data); final RealMatrix dense = new Array2DRowRealMatrix(diag.getData()); final double[] v = { 6.7, 890.1, 23.4 }; final double[] diagResult = diag.operate(v); final double[] denseResult = dense.operate(v); TestUtils.assertEquals(diagResult, denseResult, 0d); } @Test public void testPreMultiply() { final double[] data = { -1.2, 3.4, 5 }; final DiagonalMatrix diag = new DiagonalMatrix(data); final RealMatrix dense = new Array2DRowRealMatrix(diag.getData()); final double[] v = { 6.7, 890.1, 23.4 }; final double[] diagResult = diag.preMultiply(v); final double[] denseResult = dense.preMultiply(v); TestUtils.assertEquals(diagResult, denseResult, 0d); } @Test(expected=NumberIsTooLargeException.class) public void testSetNonDiagonalEntry() { final DiagonalMatrix diag = new DiagonalMatrix(3); diag.setEntry(1, 2, 3.4); } @Test public void testSetNonDiagonalZero() { final DiagonalMatrix diag = new DiagonalMatrix(3); diag.setEntry(1, 2, 0.0); Assert.assertEquals(0.0, diag.getEntry(1, 2), Precision.SAFE_MIN); } @Test(expected=NumberIsTooLargeException.class) public void testAddNonDiagonalEntry() { final DiagonalMatrix diag = new DiagonalMatrix(3); diag.addToEntry(1, 2, 3.4); } @Test public void testAddNonDiagonalZero() { final DiagonalMatrix diag = new DiagonalMatrix(3); diag.addToEntry(1, 2, 0.0); Assert.assertEquals(0.0, diag.getEntry(1, 2), Precision.SAFE_MIN); } @Test public void testMultiplyNonDiagonalEntry() { final DiagonalMatrix diag = new DiagonalMatrix(3); diag.multiplyEntry(1, 2, 3.4); Assert.assertEquals(0.0, diag.getEntry(1, 2), Precision.SAFE_MIN); } @Test public void testMultiplyNonDiagonalZero() { final DiagonalMatrix diag = new DiagonalMatrix(3); diag.multiplyEntry(1, 2, 0.0); Assert.assertEquals(0.0, diag.getEntry(1, 2), Precision.SAFE_MIN); } @Test(expected=OutOfRangeException.class) public void testSetEntryOutOfRange() { final DiagonalMatrix diag = new DiagonalMatrix(3); diag.setEntry(3, 3, 3.4); } @Test(expected=NullArgumentException.class) public void testNull() { new DiagonalMatrix(null, false); } @Test(expected=NumberIsTooLargeException.class) public void testSetSubMatrixError() { final double[] data = { -1.2, 3.4, 5 }; final DiagonalMatrix diag = new DiagonalMatrix(data); diag.setSubMatrix(new double[][] { {1.0, 1.0}, {1.0, 1.0}}, 1, 1); } @Test public void testSetSubMatrix() { final double[] data = { -1.2, 3.4, 5 }; final DiagonalMatrix diag = new DiagonalMatrix(data); diag.setSubMatrix(new double[][] { {0.0, 5.0, 0.0}, {0.0, 0.0, 6.0}}, 1, 0); Assert.assertEquals(-1.2, diag.getEntry(0, 0), 1.0e-20); Assert.assertEquals( 5.0, diag.getEntry(1, 1), 1.0e-20); Assert.assertEquals( 6.0, diag.getEntry(2, 2), 1.0e-20); } } commons-math3-3.2-src/src/test/java/org/apache/commons/math3/linear/FrenchRealVectorFormatTest.java100644 1750 1750 2144 12126627674 32107 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.linear; import java.util.Locale; public class FrenchRealVectorFormatTest extends RealVectorFormatAbstractTest { @Override protected char getDecimalCharacter() { return ','; } @Override protected Locale getLocale() { return Locale.FRENCH; } } commons-math3-3.2-src/src/test/java/org/apache/commons/math3/linear/LUDecompositionTest.java100644 1750 1750 24662 12126627674 30650 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.linear; import org.junit.Test; import org.junit.Assert; public class LUDecompositionTest { private double[][] testData = { { 1.0, 2.0, 3.0}, { 2.0, 5.0, 3.0}, { 1.0, 0.0, 8.0} }; private double[][] testDataMinus = { { -1.0, -2.0, -3.0}, { -2.0, -5.0, -3.0}, { -1.0, 0.0, -8.0} }; private double[][] luData = { { 2.0, 3.0, 3.0 }, { 0.0, 5.0, 7.0 }, { 6.0, 9.0, 8.0 } }; // singular matrices private double[][] singular = { { 2.0, 3.0 }, { 2.0, 3.0 } }; private double[][] bigSingular = { { 1.0, 2.0, 3.0, 4.0 }, { 2.0, 5.0, 3.0, 4.0 }, { 7.0, 3.0, 256.0, 1930.0 }, { 3.0, 7.0, 6.0, 8.0 } }; // 4th row = 1st + 2nd private static final double entryTolerance = 10e-16; private static final double normTolerance = 10e-14; /** test dimensions */ @Test public void testDimensions() { RealMatrix matrix = MatrixUtils.createRealMatrix(testData); LUDecomposition LU = new LUDecomposition(matrix); Assert.assertEquals(testData.length, LU.getL().getRowDimension()); Assert.assertEquals(testData.length, LU.getL().getColumnDimension()); Assert.assertEquals(testData.length, LU.getU().getRowDimension()); Assert.assertEquals(testData.length, LU.getU().getColumnDimension()); Assert.assertEquals(testData.length, LU.getP().getRowDimension()); Assert.assertEquals(testData.length, LU.getP().getColumnDimension()); } /** test non-square matrix */ @Test public void testNonSquare() { try { new LUDecomposition(MatrixUtils.createRealMatrix(new double[3][2])); Assert.fail("Expecting NonSquareMatrixException"); } catch (NonSquareMatrixException ime) { // expected behavior } } /** test PA = LU */ @Test public void testPAEqualLU() { RealMatrix matrix = MatrixUtils.createRealMatrix(testData); LUDecomposition lu = new LUDecomposition(matrix); RealMatrix l = lu.getL(); RealMatrix u = lu.getU(); RealMatrix p = lu.getP(); double norm = l.multiply(u).subtract(p.multiply(matrix)).getNorm(); Assert.assertEquals(0, norm, normTolerance); matrix = MatrixUtils.createRealMatrix(testDataMinus); lu = new LUDecomposition(matrix); l = lu.getL(); u = lu.getU(); p = lu.getP(); norm = l.multiply(u).subtract(p.multiply(matrix)).getNorm(); Assert.assertEquals(0, norm, normTolerance); matrix = MatrixUtils.createRealIdentityMatrix(17); lu = new LUDecomposition(matrix); l = lu.getL(); u = lu.getU(); p = lu.getP(); norm = l.multiply(u).subtract(p.multiply(matrix)).getNorm(); Assert.assertEquals(0, norm, normTolerance); matrix = MatrixUtils.createRealMatrix(singular); lu = new LUDecomposition(matrix); Assert.assertFalse(lu.getSolver().isNonSingular()); Assert.assertNull(lu.getL()); Assert.assertNull(lu.getU()); Assert.assertNull(lu.getP()); matrix = MatrixUtils.createRealMatrix(bigSingular); lu = new LUDecomposition(matrix); Assert.assertFalse(lu.getSolver().isNonSingular()); Assert.assertNull(lu.getL()); Assert.assertNull(lu.getU()); Assert.assertNull(lu.getP()); } /** test that L is lower triangular with unit diagonal */ @Test public void testLLowerTriangular() { RealMatrix matrix = MatrixUtils.createRealMatrix(testData); RealMatrix l = new LUDecomposition(matrix).getL(); for (int i = 0; i < l.getRowDimension(); i++) { Assert.assertEquals(l.getEntry(i, i), 1, entryTolerance); for (int j = i + 1; j < l.getColumnDimension(); j++) { Assert.assertEquals(l.getEntry(i, j), 0, entryTolerance); } } } /** test that U is upper triangular */ @Test public void testUUpperTriangular() { RealMatrix matrix = MatrixUtils.createRealMatrix(testData); RealMatrix u = new LUDecomposition(matrix).getU(); for (int i = 0; i < u.getRowDimension(); i++) { for (int j = 0; j < i; j++) { Assert.assertEquals(u.getEntry(i, j), 0, entryTolerance); } } } /** test that P is a permutation matrix */ @Test public void testPPermutation() { RealMatrix matrix = MatrixUtils.createRealMatrix(testData); RealMatrix p = new LUDecomposition(matrix).getP(); RealMatrix ppT = p.multiply(p.transpose()); RealMatrix id = MatrixUtils.createRealIdentityMatrix(p.getRowDimension()); Assert.assertEquals(0, ppT.subtract(id).getNorm(), normTolerance); for (int i = 0; i < p.getRowDimension(); i++) { int zeroCount = 0; int oneCount = 0; int otherCount = 0; for (int j = 0; j < p.getColumnDimension(); j++) { final double e = p.getEntry(i, j); if (e == 0) { ++zeroCount; } else if (e == 1) { ++oneCount; } else { ++otherCount; } } Assert.assertEquals(p.getColumnDimension() - 1, zeroCount); Assert.assertEquals(1, oneCount); Assert.assertEquals(0, otherCount); } for (int j = 0; j < p.getColumnDimension(); j++) { int zeroCount = 0; int oneCount = 0; int otherCount = 0; for (int i = 0; i < p.getRowDimension(); i++) { final double e = p.getEntry(i, j); if (e == 0) { ++zeroCount; } else if (e == 1) { ++oneCount; } else { ++otherCount; } } Assert.assertEquals(p.getRowDimension() - 1, zeroCount); Assert.assertEquals(1, oneCount); Assert.assertEquals(0, otherCount); } } /** test singular */ @Test public void testSingular() { LUDecomposition lu = new LUDecomposition(MatrixUtils.createRealMatrix(testData)); Assert.assertTrue(lu.getSolver().isNonSingular()); lu = new LUDecomposition(MatrixUtils.createRealMatrix(singular)); Assert.assertFalse(lu.getSolver().isNonSingular()); lu = new LUDecomposition(MatrixUtils.createRealMatrix(bigSingular)); Assert.assertFalse(lu.getSolver().isNonSingular()); } /** test matrices values */ @Test public void testMatricesValues1() { LUDecomposition lu = new LUDecomposition(MatrixUtils.createRealMatrix(testData)); RealMatrix lRef = MatrixUtils.createRealMatrix(new double[][] { { 1.0, 0.0, 0.0 }, { 0.5, 1.0, 0.0 }, { 0.5, 0.2, 1.0 } }); RealMatrix uRef = MatrixUtils.createRealMatrix(new double[][] { { 2.0, 5.0, 3.0 }, { 0.0, -2.5, 6.5 }, { 0.0, 0.0, 0.2 } }); RealMatrix pRef = MatrixUtils.createRealMatrix(new double[][] { { 0.0, 1.0, 0.0 }, { 0.0, 0.0, 1.0 }, { 1.0, 0.0, 0.0 } }); int[] pivotRef = { 1, 2, 0 }; // check values against known references RealMatrix l = lu.getL(); Assert.assertEquals(0, l.subtract(lRef).getNorm(), 1.0e-13); RealMatrix u = lu.getU(); Assert.assertEquals(0, u.subtract(uRef).getNorm(), 1.0e-13); RealMatrix p = lu.getP(); Assert.assertEquals(0, p.subtract(pRef).getNorm(), 1.0e-13); int[] pivot = lu.getPivot(); for (int i = 0; i < pivotRef.length; ++i) { Assert.assertEquals(pivotRef[i], pivot[i]); } // check the same cached instance is returned the second time Assert.assertTrue(l == lu.getL()); Assert.assertTrue(u == lu.getU()); Assert.assertTrue(p == lu.getP()); } /** test matrices values */ @Test public void testMatricesValues2() { LUDecomposition lu = new LUDecomposition(MatrixUtils.createRealMatrix(luData)); RealMatrix lRef = MatrixUtils.createRealMatrix(new double[][] { { 1.0, 0.0, 0.0 }, { 0.0, 1.0, 0.0 }, { 1.0 / 3.0, 0.0, 1.0 } }); RealMatrix uRef = MatrixUtils.createRealMatrix(new double[][] { { 6.0, 9.0, 8.0 }, { 0.0, 5.0, 7.0 }, { 0.0, 0.0, 1.0 / 3.0 } }); RealMatrix pRef = MatrixUtils.createRealMatrix(new double[][] { { 0.0, 0.0, 1.0 }, { 0.0, 1.0, 0.0 }, { 1.0, 0.0, 0.0 } }); int[] pivotRef = { 2, 1, 0 }; // check values against known references RealMatrix l = lu.getL(); Assert.assertEquals(0, l.subtract(lRef).getNorm(), 1.0e-13); RealMatrix u = lu.getU(); Assert.assertEquals(0, u.subtract(uRef).getNorm(), 1.0e-13); RealMatrix p = lu.getP(); Assert.assertEquals(0, p.subtract(pRef).getNorm(), 1.0e-13); int[] pivot = lu.getPivot(); for (int i = 0; i < pivotRef.length; ++i) { Assert.assertEquals(pivotRef[i], pivot[i]); } // check the same cached instance is returned the second time Assert.assertTrue(l == lu.getL()); Assert.assertTrue(u == lu.getU()); Assert.assertTrue(p == lu.getP()); } } commons-math3-3.2-src/src/test/java/org/apache/commons/math3/linear/RealVectorAbstractTest.java100644 1750 1750 203747 12126627674 31350 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.linear; import java.io.Serializable; import java.util.Arrays; import java.util.Iterator; import java.util.NoSuchElementException; import org.apache.commons.math3.TestUtils; import org.apache.commons.math3.analysis.UnivariateFunction; import org.apache.commons.math3.analysis.function.Abs; import org.apache.commons.math3.analysis.function.Acos; import org.apache.commons.math3.analysis.function.Asin; import org.apache.commons.math3.analysis.function.Atan; import org.apache.commons.math3.analysis.function.Cbrt; import org.apache.commons.math3.analysis.function.Ceil; import org.apache.commons.math3.analysis.function.Cos; import org.apache.commons.math3.analysis.function.Cosh; import org.apache.commons.math3.analysis.function.Exp; import org.apache.commons.math3.analysis.function.Expm1; import org.apache.commons.math3.analysis.function.Floor; import org.apache.commons.math3.analysis.function.Inverse; import org.apache.commons.math3.analysis.function.Log; import org.apache.commons.math3.analysis.function.Log10; import org.apache.commons.math3.analysis.function.Log1p; import org.apache.commons.math3.analysis.function.Power; import org.apache.commons.math3.analysis.function.Rint; import org.apache.commons.math3.analysis.function.Signum; import org.apache.commons.math3.analysis.function.Sin; import org.apache.commons.math3.analysis.function.Sinh; import org.apache.commons.math3.analysis.function.Sqrt; import org.apache.commons.math3.analysis.function.Tan; import org.apache.commons.math3.analysis.function.Tanh; import org.apache.commons.math3.analysis.function.Ulp; import org.apache.commons.math3.exception.DimensionMismatchException; import org.apache.commons.math3.exception.MathArithmeticException; import org.apache.commons.math3.exception.NotPositiveException; import org.apache.commons.math3.exception.NumberIsTooSmallException; import org.apache.commons.math3.exception.OutOfRangeException; import org.apache.commons.math3.util.FastMath; import org.apache.commons.math3.util.MathArrays; import org.junit.Assert; import org.junit.Ignore; import org.junit.Test; public abstract class RealVectorAbstractTest { private enum BinaryOperation { ADD, SUB, MUL, DIV }; /** *

                          * This is an attempt at covering most particular cases of combining two * values. Here {@code x} is the value returned by * {@link #getPreferredEntryValue()}, while {@code y} and {@code z} are two * "normal" values. *

                          *
                            *
                          1. * Addition: the following cases should be covered *
                              *
                            • {@code (2 * x) + (-x)}
                            • *
                            • {@code (-x) + 2 * x}
                            • *
                            • {@code x + y}
                            • *
                            • {@code y + x}
                            • *
                            • {@code y + z}
                            • *
                            • {@code y + (x - y)}
                            • *
                            • {@code (y - x) + x}
                            • *
                            * The values to be considered are: * {@code x, y, z, 2 * x, -x, x - y, y - x}. *
                          2. *
                          3. * Subtraction: the following cases should be covered *
                              *
                            • {@code (2 * x) - x}
                            • *
                            • {@code x - y}
                            • *
                            • {@code y - x}
                            • *
                            • {@code y - z}
                            • *
                            • {@code y - (y - x)}
                            • *
                            • {@code (y + x) - y}
                            • *
                            * The values to be considered are: {@code x, y, z, x + y, y - x}. *
                          4. *
                          5. * Multiplication *
                              *
                            • {@code (x * x) * (1 / x)}
                            • *
                            • {@code (1 / x) * (x * x)}
                            • *
                            • {@code x * y}
                            • *
                            • {@code y * x}
                            • *
                            • {@code y * z}
                            • *
                            * The values to be considered are: {@code x, y, z, 1 / x, x * x}. *
                          6. *
                          7. * Division *
                              *
                            • {@code (x * x) / x}
                            • *
                            • {@code x / y}
                            • *
                            • {@code y / x}
                            • *
                            • {@code y / z}
                            • *
                            * The values to be considered are: {@code x, y, z, x * x}. *
                          8. *
                          * Also to be considered {@code NaN}, {@code POSITIVE_INFINITY}, * {@code NEGATIVE_INFINITY}, {@code +0.0}, {@code -0.0}. */ private final double[] values; /** * Creates a new instance of {@link RealVector}, with specified entries. * The returned vector must be of the type currently tested. It should be * noted that some tests assume that no references to the specified * {@code double[]} are kept in the returned object: if necessary, defensive * copy of this array should be made. * * @param data the entries of the vector to be created * @return a new {@link RealVector} of the type to be tested */ public abstract RealVector create(double[] data); /** * Creates a new instance of {@link RealVector}, with specified entries. * The type of the returned vector must be different from the type currently * tested. It should be noted that some tests assume that no references to * the specified {@code double[]} are kept in the returned object: if * necessary, defensive copy of this array should be made. * * @param data the entries of the vector to be created * @return a new {@link RealVector} of an alien type */ public RealVector createAlien(double[] data){ return new RealVectorTestImpl(data); } /** * Returns a preferred value of the entries, to be tested specifically. Some * implementations of {@link RealVector} (e.g. {@link OpenMapRealVector}) do * not store specific values of entries. In order to ensure that all tests * take into account this specific value, some entries of the vectors to be * tested are deliberately set to the value returned by the present method. * The default implementation returns {@code 0.0}. * * @return a value which should be present in all vectors to be * tested */ public double getPreferredEntryValue() { return 0.0; } public RealVectorAbstractTest() { /* * Make sure that x, y, z are three different values. Also, x is the * preferred value (e.g. the value which is not stored in sparse * implementations). */ final double x = getPreferredEntryValue(); final double y = x + 1d; final double z = y + 1d; values = new double[] { Double.NaN, Double.POSITIVE_INFINITY, Double.NEGATIVE_INFINITY, 0d, -0d, x, y, z, 2 * x, -x, 1 / x, x * x, x + y, x - y, y - x }; } @Test public void testGetDimension() { final double x = getPreferredEntryValue(); final double[] data1 = {x, x, x, x}; Assert.assertEquals(data1.length, create(data1).getDimension()); final double y = x + 1; final double[] data2 = {y, y, y, y}; Assert.assertEquals(data2.length, create(data2).getDimension()); } @Test public void testGetEntry() { final double x = getPreferredEntryValue(); final double[] data = {x, 1d, 2d, x, x}; final RealVector v = create(data); for (int i = 0; i < data.length; i++) { Assert.assertEquals("entry " + i, data[i], v.getEntry(i), 0d); } } @Test(expected=OutOfRangeException.class) public void testGetEntryInvalidIndex1() { create(new double[4]).getEntry(-1); } @Test(expected=OutOfRangeException.class) public void testGetEntryInvalidIndex2() { create(new double[4]).getEntry(4); } @Test public void testSetEntry() { final double x = getPreferredEntryValue(); final double[] data = {x, 1d, 2d, x, x}; final double[] expected = MathArrays.copyOf(data); final RealVector actual = create(data); /* * Try setting to any value. */ for (int i = 0; i < data.length; i++) { final double oldValue = data[i]; final double newValue = oldValue + 1d; expected[i] = newValue; actual.setEntry(i, newValue); TestUtils.assertEquals("while setting entry #" + i, expected, actual, 0d); expected[i] = oldValue; actual.setEntry(i, oldValue); } /* * Try setting to the preferred value. */ for (int i = 0; i < data.length; i++) { final double oldValue = data[i]; final double newValue = x; expected[i] = newValue; actual.setEntry(i, newValue); TestUtils.assertEquals("while setting entry #" + i, expected, actual, 0d); expected[i] = oldValue; actual.setEntry(i, oldValue); } } @Test(expected=OutOfRangeException.class) public void testSetEntryInvalidIndex1() { create(new double[4]).setEntry(-1, getPreferredEntryValue()); } @Test(expected=OutOfRangeException.class) public void testSetEntryInvalidIndex2() { create(new double[4]).setEntry(4, getPreferredEntryValue()); } @Test public void testAddToEntry() { final double x = getPreferredEntryValue(); final double[] data1 = {x, 1d, 2d, x, x}; final double[] data2 = {x, x, 3d, x, 4d, x}; final double[] expected = MathArrays.copyOf(data1); final RealVector actual = create(data1); /* * Try adding any value. */ double increment = 1d; for (int i = 0; i < data1.length; i++) { final double oldValue = data1[i]; expected[i] += increment; actual.addToEntry(i, increment); TestUtils.assertEquals("while incrementing entry #" + i, expected, actual, 0d); expected[i] = oldValue; actual.setEntry(i, oldValue); } /* * Try incrementing so that result is equal to preferred value. */ for (int i = 0; i < data1.length; i++) { final double oldValue = data1[i]; increment = x - oldValue; expected[i] = x; actual.addToEntry(i, increment); TestUtils.assertEquals("while incrementing entry #" + i, expected, actual, 0d); expected[i] = oldValue; actual.setEntry(i, oldValue); } } @Test(expected=OutOfRangeException.class) public void testAddToEntryInvalidIndex1() { create(new double[3]).addToEntry(-1, getPreferredEntryValue()); } @Test(expected=OutOfRangeException.class) public void testAddToEntryInvalidIndex2() { create(new double[3]).addToEntry(4, getPreferredEntryValue()); } private void doTestAppendVector(final String message, final RealVector v1, final RealVector v2, final double delta) { final int n1 = v1.getDimension(); final int n2 = v2.getDimension(); final RealVector v = v1.append(v2); Assert.assertEquals(message, n1 + n2, v.getDimension()); for (int i = 0; i < n1; i++) { final String msg = message + ", entry #" + i; Assert.assertEquals(msg, v1.getEntry(i), v.getEntry(i), delta); } for (int i = 0; i < n2; i++) { final String msg = message + ", entry #" + (n1 + i); Assert.assertEquals(msg, v2.getEntry(i), v.getEntry(n1 + i), delta); } } @Test public void testAppendVector() { final double x = getPreferredEntryValue(); final double[] data1 = {x, 1d, 2d, x, x}; final double[] data2 = {x, x, 3d, x, 4d, x}; doTestAppendVector("same type", create(data1), create(data2), 0d); doTestAppendVector("mixed types", create(data1), createAlien(data2), 0d); } private void doTestAppendScalar(final String message, final RealVector v, final double d, final double delta) { final int n = v.getDimension(); final RealVector w = v.append(d); Assert.assertEquals(message, n + 1, w.getDimension()); for (int i = 0; i < n; i++) { final String msg = message + ", entry #" + i; Assert.assertEquals(msg, v.getEntry(i), w.getEntry(i), delta); } final String msg = message + ", entry #" + n; Assert.assertEquals(msg, d, w.getEntry(n), delta); } @Test public void testAppendScalar() { final double x = getPreferredEntryValue(); final double[] data = new double[] {x, 1d, 2d, x, x}; doTestAppendScalar("", create(data), 1d, 0d); doTestAppendScalar("", create(data), x, 0d); } @Test public void testGetSubVector() { final double x = getPreferredEntryValue(); final double[] data = {x, x, x, 1d, x, 2d, x, x, 3d, x, x, x, 4d, x, x, x}; final int index = 1; final int n = data.length - 5; final RealVector actual = create(data).getSubVector(index, n); final double[] expected = new double[n]; System.arraycopy(data, index, expected, 0, n); TestUtils.assertEquals("", expected, actual, 0d); } @Test(expected = OutOfRangeException.class) public void testGetSubVectorInvalidIndex1() { final int n = 10; create(new double[n]).getSubVector(-1, 2); } @Test(expected = OutOfRangeException.class) public void testGetSubVectorInvalidIndex2() { final int n = 10; create(new double[n]).getSubVector(n, 2); } @Test(expected = OutOfRangeException.class) public void testGetSubVectorInvalidIndex3() { final int n = 10; create(new double[n]).getSubVector(0, n + 1); } @Test(expected = NotPositiveException.class) public void testGetSubVectorInvalidIndex4() { final int n = 10; create(new double[n]).getSubVector(3, -2); } @Test public void testSetSubVectorSameType() { final double x = getPreferredEntryValue(); final double[] expected = {x, x, x, 1d, x, 2d, x, x, 3d, x, x, x, 4d, x, x, x}; final double[] sub = {5d, x, 6d, 7d, 8d}; final RealVector actual = create(expected); final int index = 2; actual.setSubVector(index, create(sub)); for (int i = 0; i < sub.length; i++){ expected[index + i] = sub[i]; } TestUtils.assertEquals("", expected, actual, 0d); } @Test public void testSetSubVectorMixedType() { final double x = getPreferredEntryValue(); final double[] expected = {x, x, x, 1d, x, 2d, x, x, 3d, x, x, x, 4d, x, x, x}; final double[] sub = {5d, x, 6d, 7d, 8d}; final RealVector actual = create(expected); final int index = 2; actual.setSubVector(index, createAlien(sub)); for (int i = 0; i < sub.length; i++){ expected[index + i] = sub[i]; } TestUtils.assertEquals("", expected, actual, 0d); } @Test(expected = OutOfRangeException.class) public void testSetSubVectorInvalidIndex1() { create(new double[10]).setSubVector(-1, create(new double[2])); } @Test(expected = OutOfRangeException.class) public void testSetSubVectorInvalidIndex2() { create(new double[10]).setSubVector(10, create(new double[2])); } @Test(expected = OutOfRangeException.class) public void testSetSubVectorInvalidIndex3() { create(new double[10]).setSubVector(9, create(new double[2])); } @Test public void testIsNaN() { final RealVector v = create(new double[] {0, 1, 2}); Assert.assertFalse(v.isNaN()); v.setEntry(1, Double.NaN); Assert.assertTrue(v.isNaN()); } @Test public void testIsInfinite() { final RealVector v = create(new double[] { 0, 1, 2 }); Assert.assertFalse(v.isInfinite()); v.setEntry(0, Double.POSITIVE_INFINITY); Assert.assertTrue(v.isInfinite()); v.setEntry(1, Double.NaN); Assert.assertFalse(v.isInfinite()); } private void doTestEbeBinaryOperation(final BinaryOperation op, final boolean mixed) { final double[] data1 = new double[values.length * values.length]; final double[] data2 = new double[values.length * values.length]; int k = 0; for (int i = 0; i < values.length; i++) { for (int j = 0; j < values.length; j++) { data1[k] = values[i]; data2[k] = values[j]; ++k; } } final RealVector v1 = create(data1); final RealVector v2 = mixed ? createAlien(data2) : create(data2); final RealVector actual; switch (op) { case ADD: actual = v1.add(v2); break; case SUB: actual = v1.subtract(v2); break; case MUL: actual = v1.ebeMultiply(v2); break; case DIV: actual = v1.ebeDivide(v2); break; default: throw new AssertionError("unexpected value"); } final double[] expected = new double[data1.length]; for (int i = 0; i < expected.length; i++) { switch (op) { case ADD: expected[i] = data1[i] + data2[i]; break; case SUB: expected[i] = data1[i] - data2[i]; break; case MUL: expected[i] = data1[i] * data2[i]; break; case DIV: expected[i] = data1[i] / data2[i]; break; default: throw new AssertionError("unexpected value"); } } for (int i = 0; i < expected.length; i++) { final String msg = "entry #"+i+", left = "+data1[i]+", right = " + data2[i]; Assert.assertEquals(msg, expected[i], actual.getEntry(i), 0.0); } } private void doTestEbeBinaryOperationDimensionMismatch(final BinaryOperation op) { final int n = 10; switch (op) { case ADD: create(new double[n]).add(create(new double[n + 1])); break; case SUB: create(new double[n]).subtract(create(new double[n + 1])); break; case MUL: create(new double[n]).ebeMultiply(create(new double[n + 1])); break; case DIV: create(new double[n]).ebeDivide(create(new double[n + 1])); break; default: throw new AssertionError("unexpected value"); } } @Test public void testAddSameType() { doTestEbeBinaryOperation(BinaryOperation.ADD, false); } @Test public void testAddMixedTypes() { doTestEbeBinaryOperation(BinaryOperation.ADD, true); } @Test(expected = DimensionMismatchException.class) public void testAddDimensionMismatch() { doTestEbeBinaryOperationDimensionMismatch(BinaryOperation.ADD); } @Test public void testSubtractSameType() { doTestEbeBinaryOperation(BinaryOperation.SUB, false); } @Test public void testSubtractMixedTypes() { doTestEbeBinaryOperation(BinaryOperation.SUB, true); } @Test(expected = DimensionMismatchException.class) public void testSubtractDimensionMismatch() { doTestEbeBinaryOperationDimensionMismatch(BinaryOperation.SUB); } @Ignore("ebeMultiply(RealVector) is known to be faulty (MATH-803) and is deprecated.") @Test public void testEbeMultiplySameType() { doTestEbeBinaryOperation(BinaryOperation.MUL, false); } @Ignore("ebeMultiply(RealVector) is known to be faulty (MATH-803) and is deprecated.") @Test public void testEbeMultiplyMixedTypes() { doTestEbeBinaryOperation(BinaryOperation.MUL, true); } @Ignore("ebeMultiply(RealVector) is known to be faulty (MATH-803) and is deprecated.") @Test(expected = DimensionMismatchException.class) public void testEbeMultiplyDimensionMismatch() { doTestEbeBinaryOperationDimensionMismatch(BinaryOperation.MUL); } @Ignore("ebeDivide(RealVector) is known to be faulty (MATH-803) and is deprecated.") @Test public void testEbeDivideSameType() { doTestEbeBinaryOperation(BinaryOperation.DIV, false); } @Ignore("ebeDivide(RealVector) is known to be faulty (MATH-803) and is deprecated.") @Test public void testEbeDivideMixedTypes() { doTestEbeBinaryOperation(BinaryOperation.DIV, true); } @Ignore("ebeDivide(RealVector) is known to be faulty (MATH-803) and is deprecated.") @Test(expected = DimensionMismatchException.class) public void testEbeDivideDimensionMismatch() { doTestEbeBinaryOperationDimensionMismatch(BinaryOperation.DIV); } private void doTestGetDistance(final boolean mixed) { final double x = getPreferredEntryValue(); final double[] data1 = new double[] { x, x, 1d, x, 2d, x, x, 3d, x }; final double[] data2 = new double[] { 4d, x, x, 5d, 6d, 7d, x, x, 8d }; final RealVector v1 = create(data1); final RealVector v2; if (mixed) { v2 = createAlien(data2); } else { v2 = create(data2); } final double actual = v1.getDistance(v2); double expected = 0d; for (int i = 0; i < data1.length; i++) { final double delta = data2[i] - data1[i]; expected += delta * delta; } expected = FastMath.sqrt(expected); Assert.assertEquals("", expected, actual, 0d); } @Test public void testGetDistanceSameType() { doTestGetDistance(false); } @Test public void testGetDistanceMixedTypes() { doTestGetDistance(true); } @Test(expected = DimensionMismatchException.class) public void testGetDistanceDimensionMismatch() { create(new double[4]).getDistance(createAlien(new double[5])); } @Test public void testGetNorm() { final double x = getPreferredEntryValue(); final double[] data = new double[] { x, x, 1d, x, 2d, x, x, 3d, x }; final RealVector v = create(data); final double actual = v.getNorm(); double expected = 0d; for (int i = 0; i < data.length; i++) { expected += data[i] * data[i]; } expected = FastMath.sqrt(expected); Assert.assertEquals("", expected, actual, 0d); } private void doTestGetL1Distance(final boolean mixed) { final double x = getPreferredEntryValue(); final double[] data1 = new double[] { x, x, 1d, x, 2d, x, x, 3d, x }; final double[] data2 = new double[] { 4d, x, x, 5d, 6d, 7d, x, x, 8d }; final RealVector v1 = create(data1); final RealVector v2; if (mixed) { v2 = createAlien(data2); } else { v2 = create(data2); } final double actual = v1.getL1Distance(v2); double expected = 0d; for (int i = 0; i < data1.length; i++) { final double delta = data2[i] - data1[i]; expected += FastMath.abs(delta); } Assert.assertEquals("", expected, actual, 0d); } @Test public void testGetL1DistanceSameType() { doTestGetL1Distance(false); } @Test public void testGetL1DistanceMixedTypes() { doTestGetL1Distance(true); } @Test(expected = DimensionMismatchException.class) public void testGetL1DistanceDimensionMismatch() { create(new double[4]).getL1Distance(createAlien(new double[5])); } @Test public void testGetL1Norm() { final double x = getPreferredEntryValue(); final double[] data = new double[] { x, x, 1d, x, 2d, x, x, 3d, x }; final RealVector v = create(data); final double actual = v.getL1Norm(); double expected = 0d; for (int i = 0; i < data.length; i++) { expected += FastMath.abs(data[i]); } Assert.assertEquals("", expected, actual, 0d); } private void doTestGetLInfDistance(final boolean mixed) { final double x = getPreferredEntryValue(); final double[] data1 = new double[] { x, x, 1d, x, 2d, x, x, 3d, x }; final double[] data2 = new double[] { 4d, x, x, 5d, 6d, 7d, x, x, 8d }; final RealVector v1 = create(data1); final RealVector v2; if (mixed) { v2 = createAlien(data2); } else { v2 = create(data2); } final double actual = v1.getLInfDistance(v2); double expected = 0d; for (int i = 0; i < data1.length; i++) { final double delta = data2[i] - data1[i]; expected = FastMath.max(expected, FastMath.abs(delta)); } Assert.assertEquals("", expected, actual, 0d); } @Test public void testGetLInfDistanceSameType() { doTestGetLInfDistance(false); } @Test public void testGetLInfDistanceMixedTypes() { doTestGetLInfDistance(true); } @Test(expected = DimensionMismatchException.class) public void testGetLInfDistanceDimensionMismatch() { create(new double[4]).getLInfDistance(createAlien(new double[5])); } @Test public void testGetLInfNorm() { final double x = getPreferredEntryValue(); final double[] data = new double[] { x, x, 1d, x, 2d, x, x, 3d, x }; final RealVector v = create(data); final double actual = v.getLInfNorm(); double expected = 0d; for (int i = 0; i < data.length; i++) { expected = FastMath.max(expected, FastMath.abs(data[i])); } Assert.assertEquals("", expected, actual, 0d); } private void doTestMapBinaryOperation(final BinaryOperation op, final boolean inPlace) { final double[] expected = new double[values.length]; for (int i = 0; i < values.length; i++) { final double d = values[i]; for (int j = 0; j < expected.length; j++) { switch (op) { case ADD: expected[j] = values[j] + d; break; case SUB: expected[j] = values[j] - d; break; case MUL: expected[j] = values[j] * d; break; case DIV: expected[j] = values[j] / d; break; default: throw new AssertionError("unexpected value"); } } final RealVector v = create(values); final RealVector actual; if (inPlace) { switch (op) { case ADD: actual = v.mapAddToSelf(d); break; case SUB: actual = v.mapSubtractToSelf(d); break; case MUL: actual = v.mapMultiplyToSelf(d); break; case DIV: actual = v.mapDivideToSelf(d); break; default: throw new AssertionError("unexpected value"); } } else { switch (op) { case ADD: actual = v.mapAdd(d); break; case SUB: actual = v.mapSubtract(d); break; case MUL: actual = v.mapMultiply(d); break; case DIV: actual = v.mapDivide(d); break; default: throw new AssertionError("unexpected value"); } } TestUtils.assertEquals(Double.toString(d), expected, actual, 0d); } } @Test public void testMapAdd() { doTestMapBinaryOperation(BinaryOperation.ADD, false); } @Test public void testMapAddToSelf() { doTestMapBinaryOperation(BinaryOperation.ADD, true); } @Test public void testMapSubtract() { doTestMapBinaryOperation(BinaryOperation.SUB, false); } @Test public void testMapSubtractToSelf() { doTestMapBinaryOperation(BinaryOperation.SUB, true); } @Test public void testMapMultiply() { doTestMapBinaryOperation(BinaryOperation.MUL, false); } @Test public void testMapMultiplyToSelf() { doTestMapBinaryOperation(BinaryOperation.MUL, true); } @Test public void testMapDivide() { doTestMapBinaryOperation(BinaryOperation.DIV, false); } @Test public void testMapDivideToSelf() { doTestMapBinaryOperation(BinaryOperation.DIV, true); } private void doTestMapFunction(final UnivariateFunction f, final boolean inPlace) { final double[] data = new double[values.length + 6]; System.arraycopy(values, 0, data, 0, values.length); data[values.length + 0] = 0.5 * FastMath.PI; data[values.length + 1] = -0.5 * FastMath.PI; data[values.length + 2] = FastMath.E; data[values.length + 3] = -FastMath.E; data[values.length + 4] = 1.0; data[values.length + 5] = -1.0; final double[] expected = new double[data.length]; for (int i = 0; i < data.length; i++) { expected[i] = f.value(data[i]); } final RealVector v = create(data); final RealVector actual; if (inPlace) { actual = v.mapToSelf(f); Assert.assertSame(v, actual); } else { actual = v.map(f); } TestUtils.assertEquals(f.getClass().getSimpleName(), expected, actual, 1E-16); } private UnivariateFunction[] createFunctions() { return new UnivariateFunction[] { new Power(2.0), new Exp(), new Expm1(), new Log(), new Log10(), new Log1p(), new Cosh(), new Sinh(), new Tanh(), new Cos(), new Sin(), new Tan(), new Acos(), new Asin(), new Atan(), new Inverse(), new Abs(), new Sqrt(), new Cbrt(), new Ceil(), new Floor(), new Rint(), new Signum(), new Ulp() }; } @Test public void testMap() { final UnivariateFunction[] functions = createFunctions(); for (UnivariateFunction f : functions) { doTestMapFunction(f, false); } } @Test public void testMapToSelf() { final UnivariateFunction[] functions = createFunctions(); for (UnivariateFunction f : functions) { doTestMapFunction(f, true); } } private void doTestOuterProduct(final boolean mixed) { final double[] dataU = values; final RealVector u = create(dataU); final double[] dataV = new double[values.length + 3]; System.arraycopy(values, 0, dataV, 0, values.length); dataV[values.length] = 1d; dataV[values.length] = -2d; dataV[values.length] = 3d; final RealVector v; if (mixed) { v = createAlien(dataV); } else { v = create(dataV); } final RealMatrix uv = u.outerProduct(v); Assert.assertEquals("number of rows", dataU.length, uv .getRowDimension()); Assert.assertEquals("number of columns", dataV.length, uv .getColumnDimension()); for (int i = 0; i < dataU.length; i++) { for (int j = 0; j < dataV.length; j++) { final double expected = dataU[i] * dataV[j]; final double actual = uv.getEntry(i, j); Assert.assertEquals(dataU[i] + " * " + dataV[j], expected, actual, 0d); } } } @Test public void testOuterProductSameType() { doTestOuterProduct(false); } @Test public void testOuterProductMixedTypes() { doTestOuterProduct(true); } private void doTestProjection(final boolean mixed) { final double x = getPreferredEntryValue(); final double[] data1 = { x, 1d, x, x, 2d, x, x, x, 3d, x, x, x, x }; final double[] data2 = { 5d, -6d, 7d, x, x, -8d, -9d, 10d, 11d, x, 12d, 13d, -15d }; double dotProduct = 0d; double norm2 = 0d; for (int i = 0; i < data1.length; i++){ dotProduct += data1[i] * data2[i]; norm2 += data2[i] * data2[i]; } final double s = dotProduct / norm2; final double[] expected = new double[data1.length]; for (int i = 0; i < data2.length; i++) { expected[i] = s * data2[i]; } final RealVector v1 = create(data1); final RealVector v2; if (mixed) { v2 = createAlien(data2); } else { v2 = create(data2); } final RealVector actual = v1.projection(v2); TestUtils.assertEquals("", expected, actual, 0d); } @Test public void testProjectionSameType() { doTestProjection(false); } @Test public void testProjectionMixedTypes() { doTestProjection(true); } @Test(expected = MathArithmeticException.class) public void testProjectionNullVector() { create(new double[4]).projection(create(new double[4])); } @Test(expected = DimensionMismatchException.class) public void testProjectionDimensionMismatch() { final RealVector v1 = create(new double[4]); final RealVector v2 = create(new double[5]); v2.set(1.0); v1.projection(v2); } @Test public void testSet() { for (int i = 0; i < values.length; i++) { final double expected = values[i]; final RealVector v = create(values); v.set(expected); for (int j = 0; j < values.length; j++) { Assert.assertEquals("entry #" + j, expected, v.getEntry(j), 0); } } } @Test public void testToArray() { final double[] data = create(values).toArray(); Assert.assertNotSame(values, data); for (int i = 0; i < values.length; i++) { Assert.assertEquals("entry #" + i, values[i], data[i], 0); } } private void doTestUnitVector(final boolean inPlace) { final double x = getPreferredEntryValue(); final double[] data = { x, 1d, x, x, 2d, x, x, x, 3d, x, x, x, x }; double norm = 0d; for (int i = 0; i < data.length; i++) { norm += data[i] * data[i]; } norm = FastMath.sqrt(norm); final double[] expected = new double[data.length]; for (int i = 0; i < expected.length; i++) { expected[i] = data[i] / norm; } final RealVector v = create(data); final RealVector actual; if (inPlace) { v.unitize(); actual = v; } else { actual = v.unitVector(); Assert.assertNotSame(v, actual); } TestUtils.assertEquals("", expected, actual, 0d); } @Test public void testUnitVector() { doTestUnitVector(false); } @Test public void testUnitize() { doTestUnitVector(true); } private void doTestUnitVectorNullVector(final boolean inPlace) { final double[] data = { 0d, 0d, 0d, 0d, 0d }; if (inPlace) { create(data).unitize(); } else { create(data).unitVector(); } } @Test(expected=ArithmeticException.class) public void testUnitVectorNullVector() { doTestUnitVectorNullVector(false); } @Test(expected=ArithmeticException.class) public void testUnitizeNullVector() { doTestUnitVectorNullVector(true); } @Test public void testIterator() { final RealVector v = create(values); final Iterator it = v.iterator(); for (int i = 0; i < values.length; i++) { Assert.assertTrue("entry #" + i, it.hasNext()); final RealVector.Entry e = it.next(); Assert.assertEquals("", i, e.getIndex()); Assert.assertEquals("", values[i], e.getValue(), 0d); try { it.remove(); Assert.fail("UnsupportedOperationException should have been thrown"); } catch (UnsupportedOperationException exc) { // Expected behavior } } Assert.assertFalse(it.hasNext()); try { it.next(); Assert.fail("NoSuchElementException should have been thrown"); } catch (NoSuchElementException e) { // Expected behavior } } private void doTestCombine(final boolean inPlace, final boolean mixed) { final int n = values.length * values.length; final double[] data1 = new double[n]; final double[] data2 = new double[n]; for (int i = 0; i < values.length; i++) { for (int j = 0; j < values.length; j++) { final int index = values.length * i + j; data1[index] = values[i]; data2[index] = values[j]; } } final RealVector v1 = create(data1); final RealVector v2 = mixed ? createAlien(data2) : create(data2); final double[] expected = new double[n]; for (int i = 0; i < values.length; i++) { final double a1 = values[i]; for (int j = 0; j < values.length; j++) { final double a2 = values[j]; for (int k = 0; k < n; k++) { expected[k] = a1 * data1[k] + a2 * data2[k]; } final RealVector actual; if (inPlace) { final RealVector v1bis = v1.copy(); actual = v1bis.combineToSelf(a1, a2, v2); Assert.assertSame(v1bis, actual); } else { actual = v1.combine(a1, a2, v2); } TestUtils.assertEquals("a1 = " + a1 + ", a2 = " + a2, expected, actual, 0.); } } } private void doTestCombineDimensionMismatch(final boolean inPlace, final boolean mixed) { final RealVector v1 = create(new double[10]); final RealVector v2; if (mixed) { v2 = createAlien(new double[15]); } else { v2 = create(new double[15]); } if (inPlace) { v1.combineToSelf(1.0, 1.0, v2); } else { v1.combine(1.0, 1.0, v2); } } @Test public void testCombineSameType() { doTestCombine(false, false); } @Test public void testCombineMixedTypes() { doTestCombine(false, true); } @Test(expected = DimensionMismatchException.class) public void testCombineDimensionMismatchSameType() { doTestCombineDimensionMismatch(false, false); } @Test(expected = DimensionMismatchException.class) public void testCombineDimensionMismatchMixedTypes() { doTestCombineDimensionMismatch(false, true); } @Test public void testCombineToSelfSameType() { doTestCombine(true, false); } @Test public void testCombineToSelfMixedTypes() { doTestCombine(true, true); } @Test(expected = DimensionMismatchException.class) public void testCombineToSelfDimensionMismatchSameType() { doTestCombineDimensionMismatch(true, false); } @Test(expected = DimensionMismatchException.class) public void testCombineToSelfDimensionMismatchMixedTypes() { doTestCombineDimensionMismatch(true, true); } @Test public void testCopy() { final RealVector v = create(values); final RealVector w = v.copy(); Assert.assertNotSame(v, w); TestUtils.assertEquals("", values, w, 0d); } private void doTestDotProductRegularValues(final boolean mixed) { final double x = getPreferredEntryValue(); final double[] data1 = { x, 1d, x, x, 2d, x, x, x, 3d, x, x, x, x }; final double[] data2 = { 5d, -6d, 7d, x, x, -8d, -9d, 10d, 11d, x, 12d, 13d, -15d }; double expected = 0d; for (int i = 0; i < data1.length; i++){ expected += data1[i] * data2[i]; } final RealVector v1 = create(data1); final RealVector v2; if (mixed) { v2 = createAlien(data2); } else { v2 = create(data2); } final double actual = v1.dotProduct(v2); Assert.assertEquals("", expected, actual, 0d); } private void doTestDotProductSpecialValues(final boolean mixed) { for (int i = 0; i < values.length; i++) { final double[] data1 = { values[i] }; final RealVector v1 = create(data1); for (int j = 0; j < values.length; j++) { final double[] data2 = { values[j] }; final RealVector v2; if (mixed) { v2 = createAlien(data2); } else { v2 = create(data2); } final double expected = data1[0] * data2[0]; final double actual = v1.dotProduct(v2); Assert.assertEquals(data1[0] + " * " + data2[0], expected, actual, 0d); } } } private void doTestDotProductDimensionMismatch(final boolean mixed) { final double[] data1 = new double[10]; final double[] data2 = new double[data1.length + 1]; final RealVector v1 = create(data1); final RealVector v2; if (mixed) { v2 = createAlien(data2); } else { v2 = create(data2); } v1.dotProduct(v2); } @Test public void testDotProductSameType() { doTestDotProductRegularValues(false); doTestDotProductSpecialValues(false); } @Test(expected=DimensionMismatchException.class) public void testDotProductDimensionMismatchSameType() { doTestDotProductDimensionMismatch(false); } @Test public void testDotProductMixedTypes() { doTestDotProductRegularValues(true); doTestDotProductSpecialValues(true); } @Test(expected=DimensionMismatchException.class) public void testDotProductDimensionMismatchMixedTypes() { doTestDotProductDimensionMismatch(true); } private void doTestCosine(final boolean mixed) { final double x = getPreferredEntryValue(); final double[] data1 = { x, 1d, x, x, 2d, x, x, x, 3d, x, x, x, x }; final double[] data2 = { 5d, -6d, 7d, x, x, -8d, -9d, 10d, 11d, x, 12d, 13d, -15d }; double norm1 = 0d; double norm2 = 0d; double dotProduct = 0d; for (int i = 0; i < data1.length; i++){ norm1 += data1[i] * data1[i]; norm2 += data2[i] * data2[i]; dotProduct += data1[i] * data2[i]; } norm1 = FastMath.sqrt(norm1); norm2 = FastMath.sqrt(norm2); final double expected = dotProduct / (norm1 * norm2); final RealVector v1 = create(data1); final RealVector v2; if (mixed) { v2 = createAlien(data2); } else { v2 = create(data2); } final double actual = v1.cosine(v2); Assert.assertEquals("", expected, actual, 0d); } @Test public void testCosineSameType() { doTestCosine(false); } @Test public void testCosineMixedTypes() { doTestCosine(true); } @Test(expected=MathArithmeticException.class) public void testCosineLeftNullVector() { final RealVector v = create(new double[] {0, 0, 0}); final RealVector w = create(new double[] {1, 0, 0}); v.cosine(w); } @Test(expected=MathArithmeticException.class) public void testCosineRightNullVector() { final RealVector v = create(new double[] {0, 0, 0}); final RealVector w = create(new double[] {1, 0, 0}); w.cosine(v); } @Test(expected=DimensionMismatchException.class) public void testCosineDimensionMismatch() { final RealVector v = create(new double[] {1, 2, 3}); final RealVector w = create(new double[] {1, 2, 3, 4}); v.cosine(w); } @Test public void testEquals() { final RealVector v = create(new double[] { 0, 1, 2 }); Assert.assertTrue(v.equals(v)); Assert.assertTrue(v.equals(v.copy())); Assert.assertFalse(v.equals(null)); Assert.assertFalse(v.equals(v.getSubVector(0, v.getDimension() - 1))); Assert.assertTrue(v.equals(v.getSubVector(0, v.getDimension()))); } @Test public void testSerial() { RealVector v = create(new double[] { 0, 1, 2 }); Assert.assertEquals(v,TestUtils.serializeAndRecover(v)); } @Test public void testMinMax() { final RealVector v1 = create(new double[] {0, -6, 4, 12, 7}); Assert.assertEquals(1, v1.getMinIndex()); Assert.assertEquals(-6, v1.getMinValue(), 1.0e-12); Assert.assertEquals(3, v1.getMaxIndex()); Assert.assertEquals(12, v1.getMaxValue(), 1.0e-12); final RealVector v2 = create(new double[] {Double.NaN, 3, Double.NaN, -2}); Assert.assertEquals(3, v2.getMinIndex()); Assert.assertEquals(-2, v2.getMinValue(), 1.0e-12); Assert.assertEquals(1, v2.getMaxIndex()); Assert.assertEquals(3, v2.getMaxValue(), 1.0e-12); final RealVector v3 = create(new double[] {Double.NaN, Double.NaN}); Assert.assertEquals(-1, v3.getMinIndex()); Assert.assertTrue(Double.isNaN(v3.getMinValue())); Assert.assertEquals(-1, v3.getMaxIndex()); Assert.assertTrue(Double.isNaN(v3.getMaxValue())); final RealVector v4 = create(new double[0]); Assert.assertEquals(-1, v4.getMinIndex()); Assert.assertTrue(Double.isNaN(v4.getMinValue())); Assert.assertEquals(-1, v4.getMaxIndex()); Assert.assertTrue(Double.isNaN(v4.getMaxValue())); } /* * TESTS OF THE VISITOR PATTERN */ /** The whole vector is visited. */ @Test public void testWalkInDefaultOrderPreservingVisitor1() { final double[] data = new double[] { 0d, 1d, 0d, 0d, 2d, 0d, 0d, 0d, 3d }; final RealVector v = create(data); final RealVectorPreservingVisitor visitor; visitor = new RealVectorPreservingVisitor() { private int expectedIndex; public void visit(final int actualIndex, final double actualValue) { Assert.assertEquals(expectedIndex, actualIndex); Assert.assertEquals(Integer.toString(actualIndex), data[actualIndex], actualValue, 0d); ++expectedIndex; } public void start(final int actualSize, final int actualStart, final int actualEnd) { Assert.assertEquals(data.length, actualSize); Assert.assertEquals(0, actualStart); Assert.assertEquals(data.length - 1, actualEnd); expectedIndex = 0; } public double end() { return 0.0; } }; v.walkInDefaultOrder(visitor); } /** Visiting an invalid subvector. */ @Test public void testWalkInDefaultOrderPreservingVisitor2() { final RealVector v = create(new double[5]); final RealVectorPreservingVisitor visitor; visitor = new RealVectorPreservingVisitor() { public void visit(int index, double value) { // Do nothing } public void start(int dimension, int start, int end) { // Do nothing } public double end() { return 0.0; } }; try { v.walkInDefaultOrder(visitor, -1, 4); Assert.fail(); } catch (OutOfRangeException e) { // Expected behavior } try { v.walkInDefaultOrder(visitor, 5, 4); Assert.fail(); } catch (OutOfRangeException e) { // Expected behavior } try { v.walkInDefaultOrder(visitor, 0, -1); Assert.fail(); } catch (OutOfRangeException e) { // Expected behavior } try { v.walkInDefaultOrder(visitor, 0, 5); Assert.fail(); } catch (OutOfRangeException e) { // Expected behavior } try { v.walkInDefaultOrder(visitor, 4, 0); Assert.fail(); } catch (NumberIsTooSmallException e) { // Expected behavior } } /** Visiting a valid subvector. */ @Test public void testWalkInDefaultOrderPreservingVisitor3() { final double[] data = new double[] { 0d, 1d, 0d, 0d, 2d, 0d, 0d, 0d, 3d }; final int expectedStart = 2; final int expectedEnd = 7; final RealVector v = create(data); final RealVectorPreservingVisitor visitor; visitor = new RealVectorPreservingVisitor() { private int expectedIndex; public void visit(final int actualIndex, final double actualValue) { Assert.assertEquals(expectedIndex, actualIndex); Assert.assertEquals(Integer.toString(actualIndex), data[actualIndex], actualValue, 0d); ++expectedIndex; } public void start(final int actualSize, final int actualStart, final int actualEnd) { Assert.assertEquals(data.length, actualSize); Assert.assertEquals(expectedStart, actualStart); Assert.assertEquals(expectedEnd, actualEnd); expectedIndex = expectedStart; } public double end() { return 0.0; } }; v.walkInDefaultOrder(visitor, expectedStart, expectedEnd); } /** The whole vector is visited. */ @Test public void testWalkInOptimizedOrderPreservingVisitor1() { final double[] data = new double[] { 0d, 1d, 0d, 0d, 2d, 0d, 0d, 0d, 3d }; final RealVector v = create(data); final RealVectorPreservingVisitor visitor; visitor = new RealVectorPreservingVisitor() { private final boolean[] visited = new boolean[data.length]; public void visit(final int actualIndex, final double actualValue) { visited[actualIndex] = true; Assert.assertEquals(Integer.toString(actualIndex), data[actualIndex], actualValue, 0d); } public void start(final int actualSize, final int actualStart, final int actualEnd) { Assert.assertEquals(data.length, actualSize); Assert.assertEquals(0, actualStart); Assert.assertEquals(data.length - 1, actualEnd); Arrays.fill(visited, false); } public double end() { for (int i = 0; i < data.length; i++) { Assert.assertTrue("entry " + i + "has not been visited", visited[i]); } return 0.0; } }; v.walkInOptimizedOrder(visitor); } /** Visiting an invalid subvector. */ @Test public void testWalkInOptimizedOrderPreservingVisitor2() { final RealVector v = create(new double[5]); final RealVectorPreservingVisitor visitor; visitor = new RealVectorPreservingVisitor() { public void visit(int index, double value) { // Do nothing } public void start(int dimension, int start, int end) { // Do nothing } public double end() { return 0.0; } }; try { v.walkInOptimizedOrder(visitor, -1, 4); Assert.fail(); } catch (OutOfRangeException e) { // Expected behavior } try { v.walkInOptimizedOrder(visitor, 5, 4); Assert.fail(); } catch (OutOfRangeException e) { // Expected behavior } try { v.walkInOptimizedOrder(visitor, 0, -1); Assert.fail(); } catch (OutOfRangeException e) { // Expected behavior } try { v.walkInOptimizedOrder(visitor, 0, 5); Assert.fail(); } catch (OutOfRangeException e) { // Expected behavior } try { v.walkInOptimizedOrder(visitor, 4, 0); Assert.fail(); } catch (NumberIsTooSmallException e) { // Expected behavior } } /** Visiting a valid subvector. */ @Test public void testWalkInOptimizedOrderPreservingVisitor3() { final double[] data = new double[] { 0d, 1d, 0d, 0d, 2d, 0d, 0d, 0d, 3d }; final int expectedStart = 2; final int expectedEnd = 7; final RealVector v = create(data); final RealVectorPreservingVisitor visitor; visitor = new RealVectorPreservingVisitor() { private final boolean[] visited = new boolean[data.length]; public void visit(final int actualIndex, final double actualValue) { Assert.assertEquals(Integer.toString(actualIndex), data[actualIndex], actualValue, 0d); visited[actualIndex] = true; } public void start(final int actualSize, final int actualStart, final int actualEnd) { Assert.assertEquals(data.length, actualSize); Assert.assertEquals(expectedStart, actualStart); Assert.assertEquals(expectedEnd, actualEnd); Arrays.fill(visited, true); } public double end() { for (int i = expectedStart; i <= expectedEnd; i++) { Assert.assertTrue("entry " + i + "has not been visited", visited[i]); } return 0.0; } }; v.walkInOptimizedOrder(visitor, expectedStart, expectedEnd); } /** The whole vector is visited. */ @Test public void testWalkInDefaultOrderChangingVisitor1() { final double[] data = new double[] { 0d, 1d, 0d, 0d, 2d, 0d, 0d, 0d, 3d }; final RealVector v = create(data); final RealVectorChangingVisitor visitor; visitor = new RealVectorChangingVisitor() { private int expectedIndex; public double visit(final int actualIndex, final double actualValue) { Assert.assertEquals(expectedIndex, actualIndex); Assert.assertEquals(Integer.toString(actualIndex), data[actualIndex], actualValue, 0d); ++expectedIndex; return actualIndex + actualValue; } public void start(final int actualSize, final int actualStart, final int actualEnd) { Assert.assertEquals(data.length, actualSize); Assert.assertEquals(0, actualStart); Assert.assertEquals(data.length - 1, actualEnd); expectedIndex = 0; } public double end() { return 0.0; } }; v.walkInDefaultOrder(visitor); for (int i = 0; i < data.length; i++) { Assert.assertEquals("entry " + i, i + data[i], v.getEntry(i), 0.0); } } /** Visiting an invalid subvector. */ @Test public void testWalkInDefaultOrderChangingVisitor2() { final RealVector v = create(new double[5]); final RealVectorChangingVisitor visitor; visitor = new RealVectorChangingVisitor() { public double visit(int index, double value) { return 0.0; } public void start(int dimension, int start, int end) { // Do nothing } public double end() { return 0.0; } }; try { v.walkInDefaultOrder(visitor, -1, 4); Assert.fail(); } catch (OutOfRangeException e) { // Expected behavior } try { v.walkInDefaultOrder(visitor, 5, 4); Assert.fail(); } catch (OutOfRangeException e) { // Expected behavior } try { v.walkInDefaultOrder(visitor, 0, -1); Assert.fail(); } catch (OutOfRangeException e) { // Expected behavior } try { v.walkInDefaultOrder(visitor, 0, 5); Assert.fail(); } catch (OutOfRangeException e) { // Expected behavior } try { v.walkInDefaultOrder(visitor, 4, 0); Assert.fail(); } catch (NumberIsTooSmallException e) { // Expected behavior } } /** Visiting a valid subvector. */ @Test public void testWalkInDefaultOrderChangingVisitor3() { final double[] data = new double[] { 0d, 1d, 0d, 0d, 2d, 0d, 0d, 0d, 3d }; final int expectedStart = 2; final int expectedEnd = 7; final RealVector v = create(data); final RealVectorChangingVisitor visitor; visitor = new RealVectorChangingVisitor() { private int expectedIndex; public double visit(final int actualIndex, final double actualValue) { Assert.assertEquals(expectedIndex, actualIndex); Assert.assertEquals(Integer.toString(actualIndex), data[actualIndex], actualValue, 0d); ++expectedIndex; return actualIndex + actualValue; } public void start(final int actualSize, final int actualStart, final int actualEnd) { Assert.assertEquals(data.length, actualSize); Assert.assertEquals(expectedStart, actualStart); Assert.assertEquals(expectedEnd, actualEnd); expectedIndex = expectedStart; } public double end() { return 0.0; } }; v.walkInDefaultOrder(visitor, expectedStart, expectedEnd); for (int i = expectedStart; i <= expectedEnd; i++) { Assert.assertEquals("entry " + i, i + data[i], v.getEntry(i), 0.0); } } /** The whole vector is visited. */ @Test public void testWalkInOptimizedOrderChangingVisitor1() { final double[] data = new double[] { 0d, 1d, 0d, 0d, 2d, 0d, 0d, 0d, 3d }; final RealVector v = create(data); final RealVectorChangingVisitor visitor; visitor = new RealVectorChangingVisitor() { private final boolean[] visited = new boolean[data.length]; public double visit(final int actualIndex, final double actualValue) { visited[actualIndex] = true; Assert.assertEquals(Integer.toString(actualIndex), data[actualIndex], actualValue, 0d); return actualIndex + actualValue; } public void start(final int actualSize, final int actualStart, final int actualEnd) { Assert.assertEquals(data.length, actualSize); Assert.assertEquals(0, actualStart); Assert.assertEquals(data.length - 1, actualEnd); Arrays.fill(visited, false); } public double end() { for (int i = 0; i < data.length; i++) { Assert.assertTrue("entry " + i + "has not been visited", visited[i]); } return 0.0; } }; v.walkInOptimizedOrder(visitor); for (int i = 0; i < data.length; i++) { Assert.assertEquals("entry " + i, i + data[i], v.getEntry(i), 0.0); } } /** Visiting an invalid subvector. */ @Test public void testWalkInOptimizedOrderChangingVisitor2() { final RealVector v = create(new double[5]); final RealVectorChangingVisitor visitor; visitor = new RealVectorChangingVisitor() { public double visit(int index, double value) { return 0.0; } public void start(int dimension, int start, int end) { // Do nothing } public double end() { return 0.0; } }; try { v.walkInOptimizedOrder(visitor, -1, 4); Assert.fail(); } catch (OutOfRangeException e) { // Expected behavior } try { v.walkInOptimizedOrder(visitor, 5, 4); Assert.fail(); } catch (OutOfRangeException e) { // Expected behavior } try { v.walkInOptimizedOrder(visitor, 0, -1); Assert.fail(); } catch (OutOfRangeException e) { // Expected behavior } try { v.walkInOptimizedOrder(visitor, 0, 5); Assert.fail(); } catch (OutOfRangeException e) { // Expected behavior } try { v.walkInOptimizedOrder(visitor, 4, 0); Assert.fail(); } catch (NumberIsTooSmallException e) { // Expected behavior } } /** Visiting a valid subvector. */ @Test public void testWalkInOptimizedOrderChangingVisitor3() { final double[] data = new double[] { 0d, 1d, 0d, 0d, 2d, 0d, 0d, 0d, 3d }; final int expectedStart = 2; final int expectedEnd = 7; final RealVector v = create(data); final RealVectorChangingVisitor visitor; visitor = new RealVectorChangingVisitor() { private final boolean[] visited = new boolean[data.length]; public double visit(final int actualIndex, final double actualValue) { Assert.assertEquals(Integer.toString(actualIndex), data[actualIndex], actualValue, 0d); visited[actualIndex] = true; return actualIndex + actualValue; } public void start(final int actualSize, final int actualStart, final int actualEnd) { Assert.assertEquals(data.length, actualSize); Assert.assertEquals(expectedStart, actualStart); Assert.assertEquals(expectedEnd, actualEnd); Arrays.fill(visited, true); } public double end() { for (int i = expectedStart; i <= expectedEnd; i++) { Assert.assertTrue("entry " + i + "has not been visited", visited[i]); } return 0.0; } }; v.walkInOptimizedOrder(visitor, expectedStart, expectedEnd); for (int i = expectedStart; i <= expectedEnd; i++) { Assert.assertEquals("entry " + i, i + data[i], v.getEntry(i), 0.0); } } /** * Minimal implementation of the {@link RealVector} abstract class, for * mixed types unit tests. */ public static class RealVectorTestImpl extends RealVector implements Serializable { /** Serializable version identifier. */ private static final long serialVersionUID = 20120706L; /** Entries of the vector. */ protected double data[]; public RealVectorTestImpl(double[] d) { data = d.clone(); } private UnsupportedOperationException unsupported() { return new UnsupportedOperationException("Not supported, unneeded for test purposes"); } @Override public RealVector copy() { return new RealVectorTestImpl(data); } @Override public RealVector ebeMultiply(RealVector v) { throw unsupported(); } @Override public RealVector ebeDivide(RealVector v) { throw unsupported(); } @Override public double getEntry(int index) { checkIndex(index); return data[index]; } @Override public int getDimension() { return data.length; } @Override public RealVector append(RealVector v) { throw unsupported(); } @Override public RealVector append(double d) { throw unsupported(); } @Override public RealVector getSubVector(int index, int n) { throw unsupported(); } @Override public void setEntry(int index, double value) { checkIndex(index); data[index] = value; } @Override public void setSubVector(int index, RealVector v) { throw unsupported(); } @Override public boolean isNaN() { throw unsupported(); } @Override public boolean isInfinite() { throw unsupported(); } } } commons-math3-3.2-src/src/test/java/org/apache/commons/math3/linear/OpenMapRealMatrixTest.java100644 1750 1750 2147 12126627674 31075 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.linear; import org.apache.commons.math3.exception.NumberIsTooLargeException; import org.junit.Test; public final class OpenMapRealMatrixTest { @Test(expected=NumberIsTooLargeException.class) public void testMath679() { new OpenMapRealMatrix(3, Integer.MAX_VALUE); } }commons-math3-3.2-src/src/test/java/org/apache/commons/math3/linear/HilbertMatrix.java100644 1750 1750 4643 12126627674 27466 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.linear; import org.apache.commons.math3.exception.DimensionMismatchException; /** This class implements Hilbert Matrices as {@link RealLinearOperator}. */ public class HilbertMatrix extends RealLinearOperator { /** The size of the matrix. */ private final int n; /** * Creates a new instance of this class. * * @param n Size of the matrix to be created.. */ public HilbertMatrix(final int n) { this.n = n; } /** {@inheritDoc} */ @Override public int getColumnDimension() { return n; } /** {@inheritDoc} */ @Override public int getRowDimension() { return n; } /** {@inheritDoc} */ @Override public RealVector operate(final RealVector x) { if (x.getDimension() != n) { throw new DimensionMismatchException(x.getDimension(), n); } final double[] y = new double[n]; for (int i = 0; i < n; i++) { double pos = 0.; double neg = 0.; for (int j = 0; j < n; j++) { final double xj = x.getEntry(j); final double coeff = 1. / (i + j + 1.); // Positive and negative values are sorted out in order to limit // catastrophic cancellations (do not forget that Hilbert // matrices are *very* ill-conditioned! if (xj > 0.) { pos += coeff * xj; } else { neg += coeff * xj; } } y[i] = pos + neg; } return new ArrayRealVector(y, false); } } commons-math3-3.2-src/src/test/java/org/apache/commons/math3/linear/FieldMatrixImplTest.java100644 1750 1750 142423 12126627674 30641 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.linear; import org.junit.Test; import org.junit.Assert; import org.apache.commons.math3.TestUtils; import org.apache.commons.math3.fraction.Fraction; import org.apache.commons.math3.fraction.FractionField; import org.apache.commons.math3.exception.MathIllegalStateException; import org.apache.commons.math3.exception.DimensionMismatchException; import org.apache.commons.math3.exception.NoDataException; import org.apache.commons.math3.exception.OutOfRangeException; import org.apache.commons.math3.exception.NumberIsTooSmallException; import org.apache.commons.math3.exception.NotStrictlyPositiveException; import org.apache.commons.math3.exception.NullArgumentException; import org.apache.commons.math3.exception.MathIllegalArgumentException; /** * Test cases for the {@link Array2DRowFieldMatrix} class. * * @version $Id: FieldMatrixImplTest.java 1364030 2012-07-21 01:10:04Z erans $ */ public final class FieldMatrixImplTest { // 3 x 3 identity matrix protected Fraction[][] id = { {new Fraction(1),new Fraction(0),new Fraction(0)}, {new Fraction(0),new Fraction(1),new Fraction(0)}, {new Fraction(0),new Fraction(0),new Fraction(1)} }; // Test data for group operations protected Fraction[][] testData = { {new Fraction(1),new Fraction(2),new Fraction(3)}, {new Fraction(2),new Fraction(5),new Fraction(3)}, {new Fraction(1),new Fraction(0),new Fraction(8)} }; protected Fraction[][] testDataLU = {{new Fraction(2), new Fraction(5), new Fraction(3)}, {new Fraction(1, 2), new Fraction(-5, 2), new Fraction(13, 2)}, {new Fraction(1, 2), new Fraction(1, 5), new Fraction(1, 5)}}; protected Fraction[][] testDataPlus2 = { {new Fraction(3),new Fraction(4),new Fraction(5)}, {new Fraction(4),new Fraction(7),new Fraction(5)}, {new Fraction(3),new Fraction(2),new Fraction(10)} }; protected Fraction[][] testDataMinus = { {new Fraction(-1),new Fraction(-2),new Fraction(-3)}, {new Fraction(-2),new Fraction(-5),new Fraction(-3)}, {new Fraction(-1),new Fraction(0),new Fraction(-8)} }; protected Fraction[] testDataRow1 = {new Fraction(1),new Fraction(2),new Fraction(3)}; protected Fraction[] testDataCol3 = {new Fraction(3),new Fraction(3),new Fraction(8)}; protected Fraction[][] testDataInv = { {new Fraction(-40),new Fraction(16),new Fraction(9)}, {new Fraction(13),new Fraction(-5),new Fraction(-3)}, {new Fraction(5),new Fraction(-2),new Fraction(-1)} }; protected Fraction[] preMultTest = {new Fraction(8),new Fraction(12),new Fraction(33)}; protected Fraction[][] testData2 ={ {new Fraction(1),new Fraction(2),new Fraction(3)}, {new Fraction(2),new Fraction(5),new Fraction(3)}}; protected Fraction[][] testData2T = { {new Fraction(1),new Fraction(2)}, {new Fraction(2),new Fraction(5)}, {new Fraction(3),new Fraction(3)}}; protected Fraction[][] testDataPlusInv = { {new Fraction(-39),new Fraction(18),new Fraction(12)}, {new Fraction(15),new Fraction(0),new Fraction(0)}, {new Fraction(6),new Fraction(-2),new Fraction(7)} }; // lu decomposition tests protected Fraction[][] luData = { {new Fraction(2),new Fraction(3),new Fraction(3)}, {new Fraction(0),new Fraction(5),new Fraction(7)}, {new Fraction(6),new Fraction(9),new Fraction(8)} }; protected Fraction[][] luDataLUDecomposition = { {new Fraction(6),new Fraction(9),new Fraction(8)}, {new Fraction(0),new Fraction(5),new Fraction(7)}, {new Fraction(1, 3),new Fraction(0),new Fraction(1, 3)} }; // singular matrices protected Fraction[][] singular = { {new Fraction(2),new Fraction(3)}, {new Fraction(2),new Fraction(3)} }; protected Fraction[][] bigSingular = {{new Fraction(1),new Fraction(2),new Fraction(3),new Fraction(4)}, {new Fraction(2),new Fraction(5),new Fraction(3),new Fraction(4)}, {new Fraction(7),new Fraction(3),new Fraction(256),new Fraction(1930)}, {new Fraction(3),new Fraction(7),new Fraction(6),new Fraction(8)}}; // 4th row = 1st + 2nd protected Fraction[][] detData = { {new Fraction(1),new Fraction(2),new Fraction(3)}, {new Fraction(4),new Fraction(5),new Fraction(6)}, {new Fraction(7),new Fraction(8),new Fraction(10)} }; protected Fraction[][] detData2 = { {new Fraction(1), new Fraction(3)}, {new Fraction(2), new Fraction(4)}}; // vectors protected Fraction[] testVector = {new Fraction(1),new Fraction(2),new Fraction(3)}; protected Fraction[] testVector2 = {new Fraction(1),new Fraction(2),new Fraction(3),new Fraction(4)}; // submatrix accessor tests protected Fraction[][] subTestData = {{new Fraction(1), new Fraction(2), new Fraction(3), new Fraction(4)}, {new Fraction(3, 2), new Fraction(5, 2), new Fraction(7, 2), new Fraction(9, 2)}, {new Fraction(2), new Fraction(4), new Fraction(6), new Fraction(8)}, {new Fraction(4), new Fraction(5), new Fraction(6), new Fraction(7)}}; // array selections protected Fraction[][] subRows02Cols13 = { {new Fraction(2), new Fraction(4)}, {new Fraction(4), new Fraction(8)}}; protected Fraction[][] subRows03Cols12 = { {new Fraction(2), new Fraction(3)}, {new Fraction(5), new Fraction(6)}}; protected Fraction[][] subRows03Cols123 = { {new Fraction(2), new Fraction(3), new Fraction(4)} , {new Fraction(5), new Fraction(6), new Fraction(7)}}; // effective permutations protected Fraction[][] subRows20Cols123 = { {new Fraction(4), new Fraction(6), new Fraction(8)} , {new Fraction(2), new Fraction(3), new Fraction(4)}}; protected Fraction[][] subRows31Cols31 = {{new Fraction(7), new Fraction(5)}, {new Fraction(9, 2), new Fraction(5, 2)}}; // contiguous ranges protected Fraction[][] subRows01Cols23 = {{new Fraction(3),new Fraction(4)} , {new Fraction(7, 2), new Fraction(9, 2)}}; protected Fraction[][] subRows23Cols00 = {{new Fraction(2)} , {new Fraction(4)}}; protected Fraction[][] subRows00Cols33 = {{new Fraction(4)}}; // row matrices protected Fraction[][] subRow0 = {{new Fraction(1),new Fraction(2),new Fraction(3),new Fraction(4)}}; protected Fraction[][] subRow3 = {{new Fraction(4),new Fraction(5),new Fraction(6),new Fraction(7)}}; // column matrices protected Fraction[][] subColumn1 = {{new Fraction(2)}, {new Fraction(5, 2)}, {new Fraction(4)}, {new Fraction(5)}}; protected Fraction[][] subColumn3 = {{new Fraction(4)}, {new Fraction(9, 2)}, {new Fraction(8)}, {new Fraction(7)}}; // tolerances protected double entryTolerance = 10E-16; protected double normTolerance = 10E-14; /** test dimensions */ @Test public void testDimensions() { Array2DRowFieldMatrix m = new Array2DRowFieldMatrix(testData); Array2DRowFieldMatrix m2 = new Array2DRowFieldMatrix(testData2); Assert.assertEquals("testData row dimension",3,m.getRowDimension()); Assert.assertEquals("testData column dimension",3,m.getColumnDimension()); Assert.assertTrue("testData is square",m.isSquare()); Assert.assertEquals("testData2 row dimension",m2.getRowDimension(),2); Assert.assertEquals("testData2 column dimension",m2.getColumnDimension(),3); Assert.assertTrue("testData2 is not square",!m2.isSquare()); } /** test copy functions */ @Test public void testCopyFunctions() { Array2DRowFieldMatrix m1 = new Array2DRowFieldMatrix(testData); Array2DRowFieldMatrix m2 = new Array2DRowFieldMatrix(m1.getData()); Assert.assertEquals(m2,m1); Array2DRowFieldMatrix m3 = new Array2DRowFieldMatrix(testData); Array2DRowFieldMatrix m4 = new Array2DRowFieldMatrix(m3.getData(), false); Assert.assertEquals(m4,m3); } /** test add */ @Test public void testAdd() { Array2DRowFieldMatrix m = new Array2DRowFieldMatrix(testData); Array2DRowFieldMatrix mInv = new Array2DRowFieldMatrix(testDataInv); FieldMatrix mPlusMInv = m.add(mInv); Fraction[][] sumEntries = mPlusMInv.getData(); for (int row = 0; row < m.getRowDimension(); row++) { for (int col = 0; col < m.getColumnDimension(); col++) { Assert.assertEquals(testDataPlusInv[row][col],sumEntries[row][col]); } } } /** test add failure */ @Test public void testAddFail() { Array2DRowFieldMatrix m = new Array2DRowFieldMatrix(testData); Array2DRowFieldMatrix m2 = new Array2DRowFieldMatrix(testData2); try { m.add(m2); Assert.fail("MathIllegalArgumentException expected"); } catch (MathIllegalArgumentException ex) { // ignored } } /** test m-n = m + -n */ @Test public void testPlusMinus() { Array2DRowFieldMatrix m = new Array2DRowFieldMatrix(testData); Array2DRowFieldMatrix m2 = new Array2DRowFieldMatrix(testDataInv); TestUtils.assertEquals(m.subtract(m2),m2.scalarMultiply(new Fraction(-1)).add(m)); try { m.subtract(new Array2DRowFieldMatrix(testData2)); Assert.fail("Expecting illegalArgumentException"); } catch (MathIllegalArgumentException ex) { // ignored } } /** test multiply */ @Test public void testMultiply() { Array2DRowFieldMatrix m = new Array2DRowFieldMatrix(testData); Array2DRowFieldMatrix mInv = new Array2DRowFieldMatrix(testDataInv); Array2DRowFieldMatrix identity = new Array2DRowFieldMatrix(id); Array2DRowFieldMatrix m2 = new Array2DRowFieldMatrix(testData2); TestUtils.assertEquals(m.multiply(mInv), identity); TestUtils.assertEquals(mInv.multiply(m), identity); TestUtils.assertEquals(m.multiply(identity), m); TestUtils.assertEquals(identity.multiply(mInv), mInv); TestUtils.assertEquals(m2.multiply(identity), m2); try { m.multiply(new Array2DRowFieldMatrix(bigSingular)); Assert.fail("Expecting illegalArgumentException"); } catch (MathIllegalArgumentException ex) { // ignored } } //Additional Test for Array2DRowFieldMatrixTest.testMultiply private Fraction[][] d3 = new Fraction[][] {{new Fraction(1),new Fraction(2),new Fraction(3),new Fraction(4)},{new Fraction(5),new Fraction(6),new Fraction(7),new Fraction(8)}}; private Fraction[][] d4 = new Fraction[][] {{new Fraction(1)},{new Fraction(2)},{new Fraction(3)},{new Fraction(4)}}; private Fraction[][] d5 = new Fraction[][] {{new Fraction(30)},{new Fraction(70)}}; @Test public void testMultiply2() { FieldMatrix m3 = new Array2DRowFieldMatrix(d3); FieldMatrix m4 = new Array2DRowFieldMatrix(d4); FieldMatrix m5 = new Array2DRowFieldMatrix(d5); TestUtils.assertEquals(m3.multiply(m4), m5); } @Test public void testPower() { FieldMatrix m = new Array2DRowFieldMatrix(testData); FieldMatrix mInv = new Array2DRowFieldMatrix(testDataInv); FieldMatrix mPlusInv = new Array2DRowFieldMatrix(testDataPlusInv); FieldMatrix identity = new Array2DRowFieldMatrix(id); TestUtils.assertEquals(m.power(0), identity); TestUtils.assertEquals(mInv.power(0), identity); TestUtils.assertEquals(mPlusInv.power(0), identity); TestUtils.assertEquals(m.power(1), m); TestUtils.assertEquals(mInv.power(1), mInv); TestUtils.assertEquals(mPlusInv.power(1), mPlusInv); FieldMatrix C1 = m.copy(); FieldMatrix C2 = mInv.copy(); FieldMatrix C3 = mPlusInv.copy(); // stop at 5 to avoid overflow for (int i = 2; i <= 5; ++i) { C1 = C1.multiply(m); C2 = C2.multiply(mInv); C3 = C3.multiply(mPlusInv); TestUtils.assertEquals(m.power(i), C1); TestUtils.assertEquals(mInv.power(i), C2); TestUtils.assertEquals(mPlusInv.power(i), C3); } try { FieldMatrix mNotSquare = new Array2DRowFieldMatrix(testData2T); mNotSquare.power(2); Assert.fail("Expecting NonSquareMatrixException"); } catch (NonSquareMatrixException ex) { // ignored } try { m.power(-1); Assert.fail("Expecting IllegalArgumentException"); } catch (IllegalArgumentException ex) { // ignored } } /** test trace */ @Test public void testTrace() { FieldMatrix m = new Array2DRowFieldMatrix(id); Assert.assertEquals("identity trace",new Fraction(3),m.getTrace()); m = new Array2DRowFieldMatrix(testData2); try { m.getTrace(); Assert.fail("Expecting NonSquareMatrixException"); } catch (NonSquareMatrixException ex) { // ignored } } /** test sclarAdd */ @Test public void testScalarAdd() { FieldMatrix m = new Array2DRowFieldMatrix(testData); TestUtils.assertEquals(new Array2DRowFieldMatrix(testDataPlus2), m.scalarAdd(new Fraction(2))); } /** test operate */ @Test public void testOperate() { FieldMatrix m = new Array2DRowFieldMatrix(id); TestUtils.assertEquals(testVector, m.operate(testVector)); TestUtils.assertEquals(testVector, m.operate(new ArrayFieldVector(testVector)).getData()); m = new Array2DRowFieldMatrix(bigSingular); try { m.operate(testVector); Assert.fail("Expecting illegalArgumentException"); } catch (MathIllegalArgumentException ex) { // ignored } } /** test issue MATH-209 */ @Test public void testMath209() { FieldMatrix a = new Array2DRowFieldMatrix(new Fraction[][] { { new Fraction(1), new Fraction(2) }, { new Fraction(3), new Fraction(4) }, { new Fraction(5), new Fraction(6) } }, false); Fraction[] b = a.operate(new Fraction[] { new Fraction(1), new Fraction(1) }); Assert.assertEquals(a.getRowDimension(), b.length); Assert.assertEquals( new Fraction(3), b[0]); Assert.assertEquals( new Fraction(7), b[1]); Assert.assertEquals(new Fraction(11), b[2]); } /** test transpose */ @Test public void testTranspose() { FieldMatrix m = new Array2DRowFieldMatrix(testData); FieldMatrix mIT = new FieldLUDecomposition(m).getSolver().getInverse().transpose(); FieldMatrix mTI = new FieldLUDecomposition(m.transpose()).getSolver().getInverse(); TestUtils.assertEquals(mIT, mTI); m = new Array2DRowFieldMatrix(testData2); FieldMatrix mt = new Array2DRowFieldMatrix(testData2T); TestUtils.assertEquals(mt, m.transpose()); } /** test preMultiply by vector */ @Test public void testPremultiplyVector() { FieldMatrix m = new Array2DRowFieldMatrix(testData); TestUtils.assertEquals(m.preMultiply(testVector), preMultTest); TestUtils.assertEquals(m.preMultiply(new ArrayFieldVector(testVector).getData()), preMultTest); m = new Array2DRowFieldMatrix(bigSingular); try { m.preMultiply(testVector); Assert.fail("expecting MathIllegalArgumentException"); } catch (MathIllegalArgumentException ex) { // ignored } } @Test public void testPremultiply() { FieldMatrix m3 = new Array2DRowFieldMatrix(d3); FieldMatrix m4 = new Array2DRowFieldMatrix(d4); FieldMatrix m5 = new Array2DRowFieldMatrix(d5); TestUtils.assertEquals(m4.preMultiply(m3), m5); Array2DRowFieldMatrix m = new Array2DRowFieldMatrix(testData); Array2DRowFieldMatrix mInv = new Array2DRowFieldMatrix(testDataInv); Array2DRowFieldMatrix identity = new Array2DRowFieldMatrix(id); TestUtils.assertEquals(m.preMultiply(mInv), identity); TestUtils.assertEquals(mInv.preMultiply(m), identity); TestUtils.assertEquals(m.preMultiply(identity), m); TestUtils.assertEquals(identity.preMultiply(mInv), mInv); try { m.preMultiply(new Array2DRowFieldMatrix(bigSingular)); Assert.fail("Expecting illegalArgumentException"); } catch (MathIllegalArgumentException ex) { // ignored } } @Test public void testGetVectors() { FieldMatrix m = new Array2DRowFieldMatrix(testData); TestUtils.assertEquals(m.getRow(0), testDataRow1); TestUtils.assertEquals(m.getColumn(2), testDataCol3); try { m.getRow(10); Assert.fail("expecting OutOfRangeException"); } catch (OutOfRangeException ex) { // ignored } try { m.getColumn(-1); Assert.fail("expecting OutOfRangeException"); } catch (OutOfRangeException ex) { // ignored } } @Test public void testGetEntry() { FieldMatrix m = new Array2DRowFieldMatrix(testData); Assert.assertEquals("get entry", m.getEntry(0,1), new Fraction(2)); try { m.getEntry(10, 4); Assert.fail ("Expecting OutOfRangeException"); } catch (OutOfRangeException ex) { // expected } } /** test examples in user guide */ @Test public void testExamples() { // Create a real matrix with two rows and three columns Fraction[][] matrixData = { {new Fraction(1),new Fraction(2),new Fraction(3)}, {new Fraction(2),new Fraction(5),new Fraction(3)} }; FieldMatrix m = new Array2DRowFieldMatrix(matrixData); // One more with three rows, two columns Fraction[][] matrixData2 = { {new Fraction(1),new Fraction(2)}, {new Fraction(2),new Fraction(5)}, {new Fraction(1), new Fraction(7)} }; FieldMatrix n = new Array2DRowFieldMatrix(matrixData2); // Now multiply m by n FieldMatrix p = m.multiply(n); Assert.assertEquals(2, p.getRowDimension()); Assert.assertEquals(2, p.getColumnDimension()); // Invert p FieldMatrix pInverse = new FieldLUDecomposition(p).getSolver().getInverse(); Assert.assertEquals(2, pInverse.getRowDimension()); Assert.assertEquals(2, pInverse.getColumnDimension()); // Solve example Fraction[][] coefficientsData = { {new Fraction(2), new Fraction(3), new Fraction(-2)}, {new Fraction(-1), new Fraction(7), new Fraction(6)}, {new Fraction(4), new Fraction(-3), new Fraction(-5)} }; FieldMatrix coefficients = new Array2DRowFieldMatrix(coefficientsData); Fraction[] constants = { new Fraction(1), new Fraction(-2), new Fraction(1) }; Fraction[] solution; solution = new FieldLUDecomposition(coefficients) .getSolver() .solve(new ArrayFieldVector(constants, false)).toArray(); Assert.assertEquals(new Fraction(2).multiply(solution[0]). add(new Fraction(3).multiply(solution[1])). subtract(new Fraction(2).multiply(solution[2])), constants[0]); Assert.assertEquals(new Fraction(-1).multiply(solution[0]). add(new Fraction(7).multiply(solution[1])). add(new Fraction(6).multiply(solution[2])), constants[1]); Assert.assertEquals(new Fraction(4).multiply(solution[0]). subtract(new Fraction(3).multiply(solution[1])). subtract(new Fraction(5).multiply(solution[2])), constants[2]); } // test submatrix accessors @Test public void testGetSubMatrix() { FieldMatrix m = new Array2DRowFieldMatrix(subTestData); checkGetSubMatrix(m, subRows23Cols00, 2 , 3 , 0, 0); checkGetSubMatrix(m, subRows00Cols33, 0 , 0 , 3, 3); checkGetSubMatrix(m, subRows01Cols23, 0 , 1 , 2, 3); checkGetSubMatrix(m, subRows02Cols13, new int[] { 0, 2 }, new int[] { 1, 3 }); checkGetSubMatrix(m, subRows03Cols12, new int[] { 0, 3 }, new int[] { 1, 2 }); checkGetSubMatrix(m, subRows03Cols123, new int[] { 0, 3 }, new int[] { 1, 2, 3 }); checkGetSubMatrix(m, subRows20Cols123, new int[] { 2, 0 }, new int[] { 1, 2, 3 }); checkGetSubMatrix(m, subRows31Cols31, new int[] { 3, 1 }, new int[] { 3, 1 }); checkGetSubMatrix(m, subRows31Cols31, new int[] { 3, 1 }, new int[] { 3, 1 }); checkGetSubMatrix(m, null, 1, 0, 2, 4); checkGetSubMatrix(m, null, -1, 1, 2, 2); checkGetSubMatrix(m, null, 1, 0, 2, 2); checkGetSubMatrix(m, null, 1, 0, 2, 4); checkGetSubMatrix(m, null, new int[] {}, new int[] { 0 }); checkGetSubMatrix(m, null, new int[] { 0 }, new int[] { 4 }); } private void checkGetSubMatrix(FieldMatrix m, Fraction[][] reference, int startRow, int endRow, int startColumn, int endColumn) { try { FieldMatrix sub = m.getSubMatrix(startRow, endRow, startColumn, endColumn); if (reference != null) { Assert.assertEquals(new Array2DRowFieldMatrix(reference), sub); } else { Assert.fail("Expecting OutOfRangeException or NotStrictlyPositiveException" + " or NumberIsTooSmallException or NoDataException"); } } catch (OutOfRangeException e) { if (reference != null) { throw e; } } catch (NotStrictlyPositiveException e) { if (reference != null) { throw e; } } catch (NumberIsTooSmallException e) { if (reference != null) { throw e; } } catch (NoDataException e) { if (reference != null) { throw e; } } } private void checkGetSubMatrix(FieldMatrix m, Fraction[][] reference, int[] selectedRows, int[] selectedColumns) { try { FieldMatrix sub = m.getSubMatrix(selectedRows, selectedColumns); if (reference != null) { Assert.assertEquals(new Array2DRowFieldMatrix(reference), sub); } else { Assert.fail("Expecting OutOfRangeException or NotStrictlyPositiveException" + " or NumberIsTooSmallException or NoDataException"); } } catch (OutOfRangeException e) { if (reference != null) { throw e; } } catch (NotStrictlyPositiveException e) { if (reference != null) { throw e; } } catch (NumberIsTooSmallException e) { if (reference != null) { throw e; } } catch (NoDataException e) { if (reference != null) { throw e; } } } @Test public void testCopySubMatrix() { FieldMatrix m = new Array2DRowFieldMatrix(subTestData); checkCopy(m, subRows23Cols00, 2 , 3 , 0, 0); checkCopy(m, subRows00Cols33, 0 , 0 , 3, 3); checkCopy(m, subRows01Cols23, 0 , 1 , 2, 3); checkCopy(m, subRows02Cols13, new int[] { 0, 2 }, new int[] { 1, 3 }); checkCopy(m, subRows03Cols12, new int[] { 0, 3 }, new int[] { 1, 2 }); checkCopy(m, subRows03Cols123, new int[] { 0, 3 }, new int[] { 1, 2, 3 }); checkCopy(m, subRows20Cols123, new int[] { 2, 0 }, new int[] { 1, 2, 3 }); checkCopy(m, subRows31Cols31, new int[] { 3, 1 }, new int[] { 3, 1 }); checkCopy(m, subRows31Cols31, new int[] { 3, 1 }, new int[] { 3, 1 }); checkCopy(m, null, 1, 0, 2, 4); checkCopy(m, null, -1, 1, 2, 2); checkCopy(m, null, 1, 0, 2, 2); checkCopy(m, null, 1, 0, 2, 4); checkCopy(m, null, new int[] {}, new int[] { 0 }); checkCopy(m, null, new int[] { 0 }, new int[] { 4 }); } private void checkCopy(FieldMatrix m, Fraction[][] reference, int startRow, int endRow, int startColumn, int endColumn) { try { Fraction[][] sub = (reference == null) ? new Fraction[1][1] : new Fraction[reference.length][reference[0].length]; m.copySubMatrix(startRow, endRow, startColumn, endColumn, sub); if (reference != null) { Assert.assertEquals(new Array2DRowFieldMatrix(reference), new Array2DRowFieldMatrix(sub)); } else { Assert.fail("Expecting OutOfRangeException or NumberIsTooSmallException or NoDataException"); } } catch (OutOfRangeException e) { if (reference != null) { throw e; } } catch (NumberIsTooSmallException e) { if (reference != null) { throw e; } } catch (NoDataException e) { if (reference != null) { throw e; } } } private void checkCopy(FieldMatrix m, Fraction[][] reference, int[] selectedRows, int[] selectedColumns) { try { Fraction[][] sub = (reference == null) ? new Fraction[1][1] : new Fraction[reference.length][reference[0].length]; m.copySubMatrix(selectedRows, selectedColumns, sub); if (reference != null) { Assert.assertEquals(new Array2DRowFieldMatrix(reference), new Array2DRowFieldMatrix(sub)); } else { Assert.fail("Expecting OutOfRangeException or NumberIsTooSmallException or NoDataException"); } } catch (OutOfRangeException e) { if (reference != null) { throw e; } } catch (NumberIsTooSmallException e) { if (reference != null) { throw e; } } catch (NoDataException e) { if (reference != null) { throw e; } } } @Test public void testGetRowMatrix() { FieldMatrix m = new Array2DRowFieldMatrix(subTestData); FieldMatrix mRow0 = new Array2DRowFieldMatrix(subRow0); FieldMatrix mRow3 = new Array2DRowFieldMatrix(subRow3); Assert.assertEquals("Row0", mRow0, m.getRowMatrix(0)); Assert.assertEquals("Row3", mRow3, m.getRowMatrix(3)); try { m.getRowMatrix(-1); Assert.fail("Expecting OutOfRangeException"); } catch (OutOfRangeException ex) { // expected } try { m.getRowMatrix(4); Assert.fail("Expecting OutOfRangeException"); } catch (OutOfRangeException ex) { // expected } } @Test public void testSetRowMatrix() { FieldMatrix m = new Array2DRowFieldMatrix(subTestData); FieldMatrix mRow3 = new Array2DRowFieldMatrix(subRow3); Assert.assertNotSame(mRow3, m.getRowMatrix(0)); m.setRowMatrix(0, mRow3); Assert.assertEquals(mRow3, m.getRowMatrix(0)); try { m.setRowMatrix(-1, mRow3); Assert.fail("Expecting OutOfRangeException"); } catch (OutOfRangeException ex) { // expected } try { m.setRowMatrix(0, m); Assert.fail("Expecting MatrixDimensionMismatchException"); } catch (MatrixDimensionMismatchException ex) { // expected } } @Test public void testGetColumnMatrix() { FieldMatrix m = new Array2DRowFieldMatrix(subTestData); FieldMatrix mColumn1 = new Array2DRowFieldMatrix(subColumn1); FieldMatrix mColumn3 = new Array2DRowFieldMatrix(subColumn3); Assert.assertEquals("Column1", mColumn1, m.getColumnMatrix(1)); Assert.assertEquals("Column3", mColumn3, m.getColumnMatrix(3)); try { m.getColumnMatrix(-1); Assert.fail("Expecting OutOfRangeException"); } catch (OutOfRangeException ex) { // expected } try { m.getColumnMatrix(4); Assert.fail("Expecting OutOfRangeException"); } catch (OutOfRangeException ex) { // expected } } @Test public void testSetColumnMatrix() { FieldMatrix m = new Array2DRowFieldMatrix(subTestData); FieldMatrix mColumn3 = new Array2DRowFieldMatrix(subColumn3); Assert.assertNotSame(mColumn3, m.getColumnMatrix(1)); m.setColumnMatrix(1, mColumn3); Assert.assertEquals(mColumn3, m.getColumnMatrix(1)); try { m.setColumnMatrix(-1, mColumn3); Assert.fail("Expecting OutOfRangeException"); } catch (OutOfRangeException ex) { // expected } try { m.setColumnMatrix(0, m); Assert.fail("Expecting MatrixDimensionMismatchException"); } catch (MatrixDimensionMismatchException ex) { // expected } } @Test public void testGetRowVector() { FieldMatrix m = new Array2DRowFieldMatrix(subTestData); FieldVector mRow0 = new ArrayFieldVector(subRow0[0]); FieldVector mRow3 = new ArrayFieldVector(subRow3[0]); Assert.assertEquals("Row0", mRow0, m.getRowVector(0)); Assert.assertEquals("Row3", mRow3, m.getRowVector(3)); try { m.getRowVector(-1); Assert.fail("Expecting OutOfRangeException"); } catch (OutOfRangeException ex) { // expected } try { m.getRowVector(4); Assert.fail("Expecting OutOfRangeException"); } catch (OutOfRangeException ex) { // expected } } @Test public void testSetRowVector() { FieldMatrix m = new Array2DRowFieldMatrix(subTestData); FieldVector mRow3 = new ArrayFieldVector(subRow3[0]); Assert.assertNotSame(mRow3, m.getRowMatrix(0)); m.setRowVector(0, mRow3); Assert.assertEquals(mRow3, m.getRowVector(0)); try { m.setRowVector(-1, mRow3); Assert.fail("Expecting OutOfRangeException"); } catch (OutOfRangeException ex) { // expected } try { m.setRowVector(0, new ArrayFieldVector(FractionField.getInstance(), 5)); Assert.fail("Expecting MatrixDimensionMismatchException"); } catch (MatrixDimensionMismatchException ex) { // expected } } @Test public void testGetColumnVector() { FieldMatrix m = new Array2DRowFieldMatrix(subTestData); FieldVector mColumn1 = columnToVector(subColumn1); FieldVector mColumn3 = columnToVector(subColumn3); Assert.assertEquals("Column1", mColumn1, m.getColumnVector(1)); Assert.assertEquals("Column3", mColumn3, m.getColumnVector(3)); try { m.getColumnVector(-1); Assert.fail("Expecting OutOfRangeException"); } catch (OutOfRangeException ex) { // expected } try { m.getColumnVector(4); Assert.fail("Expecting OutOfRangeException"); } catch (OutOfRangeException ex) { // expected } } @Test public void testSetColumnVector() { FieldMatrix m = new Array2DRowFieldMatrix(subTestData); FieldVector mColumn3 = columnToVector(subColumn3); Assert.assertNotSame(mColumn3, m.getColumnVector(1)); m.setColumnVector(1, mColumn3); Assert.assertEquals(mColumn3, m.getColumnVector(1)); try { m.setColumnVector(-1, mColumn3); Assert.fail("Expecting OutOfRangeException"); } catch (OutOfRangeException ex) { // expected } try { m.setColumnVector(0, new ArrayFieldVector(FractionField.getInstance(), 5)); Assert.fail("Expecting MatrixDimensionMismatchException"); } catch (MatrixDimensionMismatchException ex) { // expected } } private FieldVector columnToVector(Fraction[][] column) { Fraction[] data = new Fraction[column.length]; for (int i = 0; i < data.length; ++i) { data[i] = column[i][0]; } return new ArrayFieldVector(data, false); } @Test public void testGetRow() { FieldMatrix m = new Array2DRowFieldMatrix(subTestData); checkArrays(subRow0[0], m.getRow(0)); checkArrays(subRow3[0], m.getRow(3)); try { m.getRow(-1); Assert.fail("Expecting OutOfRangeException"); } catch (OutOfRangeException ex) { // expected } try { m.getRow(4); Assert.fail("Expecting OutOfRangeException"); } catch (OutOfRangeException ex) { // expected } } @Test public void testSetRow() { FieldMatrix m = new Array2DRowFieldMatrix(subTestData); Assert.assertTrue(subRow3[0][0] != m.getRow(0)[0]); m.setRow(0, subRow3[0]); checkArrays(subRow3[0], m.getRow(0)); try { m.setRow(-1, subRow3[0]); Assert.fail("Expecting OutOfRangeException"); } catch (OutOfRangeException ex) { // expected } try { m.setRow(0, new Fraction[5]); Assert.fail("Expecting MatrixDimensionMismatchException"); } catch (MatrixDimensionMismatchException ex) { // expected } } @Test public void testGetColumn() { FieldMatrix m = new Array2DRowFieldMatrix(subTestData); Fraction[] mColumn1 = columnToArray(subColumn1); Fraction[] mColumn3 = columnToArray(subColumn3); checkArrays(mColumn1, m.getColumn(1)); checkArrays(mColumn3, m.getColumn(3)); try { m.getColumn(-1); Assert.fail("Expecting OutOfRangeException"); } catch (OutOfRangeException ex) { // expected } try { m.getColumn(4); Assert.fail("Expecting OutOfRangeException"); } catch (OutOfRangeException ex) { // expected } } @Test public void testSetColumn() { FieldMatrix m = new Array2DRowFieldMatrix(subTestData); Fraction[] mColumn3 = columnToArray(subColumn3); Assert.assertTrue(mColumn3[0] != m.getColumn(1)[0]); m.setColumn(1, mColumn3); checkArrays(mColumn3, m.getColumn(1)); try { m.setColumn(-1, mColumn3); Assert.fail("Expecting OutOfRangeException"); } catch (OutOfRangeException ex) { // expected } try { m.setColumn(0, new Fraction[5]); Assert.fail("Expecting MatrixDimensionMismatchException"); } catch (MatrixDimensionMismatchException ex) { // expected } } private Fraction[] columnToArray(Fraction[][] column) { Fraction[] data = new Fraction[column.length]; for (int i = 0; i < data.length; ++i) { data[i] = column[i][0]; } return data; } private void checkArrays(Fraction[] expected, Fraction[] actual) { Assert.assertEquals(expected.length, actual.length); for (int i = 0; i < expected.length; ++i) { Assert.assertEquals(expected[i], actual[i]); } } @Test public void testEqualsAndHashCode() { Array2DRowFieldMatrix m = new Array2DRowFieldMatrix(testData); Array2DRowFieldMatrix m1 = (Array2DRowFieldMatrix) m.copy(); Array2DRowFieldMatrix mt = (Array2DRowFieldMatrix) m.transpose(); Assert.assertTrue(m.hashCode() != mt.hashCode()); Assert.assertEquals(m.hashCode(), m1.hashCode()); Assert.assertEquals(m, m); Assert.assertEquals(m, m1); Assert.assertFalse(m.equals(null)); Assert.assertFalse(m.equals(mt)); Assert.assertFalse(m.equals(new Array2DRowFieldMatrix(bigSingular))); } @Test public void testToString() { Array2DRowFieldMatrix m = new Array2DRowFieldMatrix(testData); Assert.assertEquals("Array2DRowFieldMatrix{{1,2,3},{2,5,3},{1,0,8}}", m.toString()); m = new Array2DRowFieldMatrix(FractionField.getInstance()); Assert.assertEquals("Array2DRowFieldMatrix{}", m.toString()); } @Test public void testSetSubMatrix() { Array2DRowFieldMatrix m = new Array2DRowFieldMatrix(testData); m.setSubMatrix(detData2,1,1); FieldMatrix expected = new Array2DRowFieldMatrix (new Fraction[][] { {new Fraction(1),new Fraction(2),new Fraction(3)}, {new Fraction(2),new Fraction(1),new Fraction(3)}, {new Fraction(1),new Fraction(2),new Fraction(4)} }); Assert.assertEquals(expected, m); m.setSubMatrix(detData2,0,0); expected = new Array2DRowFieldMatrix (new Fraction[][] { {new Fraction(1),new Fraction(3),new Fraction(3)}, {new Fraction(2),new Fraction(4),new Fraction(3)}, {new Fraction(1),new Fraction(2),new Fraction(4)} }); Assert.assertEquals(expected, m); m.setSubMatrix(testDataPlus2,0,0); expected = new Array2DRowFieldMatrix (new Fraction[][] { {new Fraction(3),new Fraction(4),new Fraction(5)}, {new Fraction(4),new Fraction(7),new Fraction(5)}, {new Fraction(3),new Fraction(2),new Fraction(10)} }); Assert.assertEquals(expected, m); // dimension overflow try { m.setSubMatrix(testData,1,1); Assert.fail("expecting OutOfRangeException"); } catch (OutOfRangeException e) { // expected } // dimension underflow try { m.setSubMatrix(testData,-1,1); Assert.fail("expecting OutOfRangeException"); } catch (OutOfRangeException e) { // expected } try { m.setSubMatrix(testData,1,-1); Assert.fail("expecting OutOfRangeException"); } catch (OutOfRangeException e) { // expected } // null try { m.setSubMatrix(null, 1, 1); Assert.fail("expecting NullArgumentException"); } catch (NullArgumentException e) { // expected } Array2DRowFieldMatrix m2 = new Array2DRowFieldMatrix(FractionField.getInstance()); try { m2.setSubMatrix(testData,0,1); Assert.fail("expecting MathIllegalStateException"); } catch (MathIllegalStateException e) { // expected } try { m2.setSubMatrix(testData,1,0); Assert.fail("expecting MathIllegalStateException"); } catch (MathIllegalStateException e) { // expected } // ragged try { m.setSubMatrix(new Fraction[][] {{new Fraction(1)}, {new Fraction(2), new Fraction(3)}}, 0, 0); Assert.fail("expecting MathIllegalArgumentException"); } catch (MathIllegalArgumentException e) { // expected } // empty try { m.setSubMatrix(new Fraction[][] {{}}, 0, 0); Assert.fail("expecting MathIllegalArgumentException"); } catch (MathIllegalArgumentException e) { // expected } } @Test public void testWalk() { int rows = 150; int columns = 75; FieldMatrix m = new Array2DRowFieldMatrix(FractionField.getInstance(), rows, columns); m.walkInRowOrder(new SetVisitor()); GetVisitor getVisitor = new GetVisitor(); m.walkInOptimizedOrder(getVisitor); Assert.assertEquals(rows * columns, getVisitor.getCount()); m = new Array2DRowFieldMatrix(FractionField.getInstance(), rows, columns); m.walkInRowOrder(new SetVisitor(), 1, rows - 2, 1, columns - 2); getVisitor = new GetVisitor(); m.walkInOptimizedOrder(getVisitor, 1, rows - 2, 1, columns - 2); Assert.assertEquals((rows - 2) * (columns - 2), getVisitor.getCount()); for (int i = 0; i < rows; ++i) { Assert.assertEquals(new Fraction(0), m.getEntry(i, 0)); Assert.assertEquals(new Fraction(0), m.getEntry(i, columns - 1)); } for (int j = 0; j < columns; ++j) { Assert.assertEquals(new Fraction(0), m.getEntry(0, j)); Assert.assertEquals(new Fraction(0), m.getEntry(rows - 1, j)); } m = new Array2DRowFieldMatrix(FractionField.getInstance(), rows, columns); m.walkInColumnOrder(new SetVisitor()); getVisitor = new GetVisitor(); m.walkInOptimizedOrder(getVisitor); Assert.assertEquals(rows * columns, getVisitor.getCount()); m = new Array2DRowFieldMatrix(FractionField.getInstance(), rows, columns); m.walkInColumnOrder(new SetVisitor(), 1, rows - 2, 1, columns - 2); getVisitor = new GetVisitor(); m.walkInOptimizedOrder(getVisitor, 1, rows - 2, 1, columns - 2); Assert.assertEquals((rows - 2) * (columns - 2), getVisitor.getCount()); for (int i = 0; i < rows; ++i) { Assert.assertEquals(new Fraction(0), m.getEntry(i, 0)); Assert.assertEquals(new Fraction(0), m.getEntry(i, columns - 1)); } for (int j = 0; j < columns; ++j) { Assert.assertEquals(new Fraction(0), m.getEntry(0, j)); Assert.assertEquals(new Fraction(0), m.getEntry(rows - 1, j)); } m = new Array2DRowFieldMatrix(FractionField.getInstance(), rows, columns); m.walkInOptimizedOrder(new SetVisitor()); getVisitor = new GetVisitor(); m.walkInRowOrder(getVisitor); Assert.assertEquals(rows * columns, getVisitor.getCount()); m = new Array2DRowFieldMatrix(FractionField.getInstance(), rows, columns); m.walkInOptimizedOrder(new SetVisitor(), 1, rows - 2, 1, columns - 2); getVisitor = new GetVisitor(); m.walkInRowOrder(getVisitor, 1, rows - 2, 1, columns - 2); Assert.assertEquals((rows - 2) * (columns - 2), getVisitor.getCount()); for (int i = 0; i < rows; ++i) { Assert.assertEquals(new Fraction(0), m.getEntry(i, 0)); Assert.assertEquals(new Fraction(0), m.getEntry(i, columns - 1)); } for (int j = 0; j < columns; ++j) { Assert.assertEquals(new Fraction(0), m.getEntry(0, j)); Assert.assertEquals(new Fraction(0), m.getEntry(rows - 1, j)); } m = new Array2DRowFieldMatrix(FractionField.getInstance(), rows, columns); m.walkInOptimizedOrder(new SetVisitor()); getVisitor = new GetVisitor(); m.walkInColumnOrder(getVisitor); Assert.assertEquals(rows * columns, getVisitor.getCount()); m = new Array2DRowFieldMatrix(FractionField.getInstance(), rows, columns); m.walkInOptimizedOrder(new SetVisitor(), 1, rows - 2, 1, columns - 2); getVisitor = new GetVisitor(); m.walkInColumnOrder(getVisitor, 1, rows - 2, 1, columns - 2); Assert.assertEquals((rows - 2) * (columns - 2), getVisitor.getCount()); for (int i = 0; i < rows; ++i) { Assert.assertEquals(new Fraction(0), m.getEntry(i, 0)); Assert.assertEquals(new Fraction(0), m.getEntry(i, columns - 1)); } for (int j = 0; j < columns; ++j) { Assert.assertEquals(new Fraction(0), m.getEntry(0, j)); Assert.assertEquals(new Fraction(0), m.getEntry(rows - 1, j)); } } @Test public void testSerial() { Array2DRowFieldMatrix m = new Array2DRowFieldMatrix(testData); Assert.assertEquals(m,TestUtils.serializeAndRecover(m)); } private static class SetVisitor extends DefaultFieldMatrixChangingVisitor { public SetVisitor() { super(Fraction.ZERO); } @Override public Fraction visit(int i, int j, Fraction value) { return new Fraction(i * 1024 + j, 1024); } } private static class GetVisitor extends DefaultFieldMatrixPreservingVisitor { private int count; public GetVisitor() { super(Fraction.ZERO); count = 0; } @Override public void visit(int i, int j, Fraction value) { ++count; Assert.assertEquals(new Fraction(i * 1024 + j, 1024), value); } public int getCount() { return count; } } //--------------- -----------------Protected methods /** extracts the l and u matrices from compact lu representation */ protected void splitLU(FieldMatrix lu, Fraction[][] lowerData, Fraction[][] upperData) { if (!lu.isSquare()) { throw new NonSquareMatrixException(lu.getRowDimension(), lu.getColumnDimension()); } if (lowerData.length != lowerData[0].length) { throw new DimensionMismatchException(lowerData.length, lowerData[0].length); } if (upperData.length != upperData[0].length) { throw new DimensionMismatchException(upperData.length, upperData[0].length); } if (lowerData.length != upperData.length) { throw new DimensionMismatchException(lowerData.length, upperData.length); } if (lowerData.length != lu.getRowDimension()) { throw new DimensionMismatchException(lowerData.length, lu.getRowDimension()); } int n = lu.getRowDimension(); for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (j < i) { lowerData[i][j] = lu.getEntry(i, j); upperData[i][j] = Fraction.ZERO; } else if (i == j) { lowerData[i][j] = Fraction.ONE; upperData[i][j] = lu.getEntry(i, j); } else { lowerData[i][j] = Fraction.ZERO; upperData[i][j] = lu.getEntry(i, j); } } } } /** Returns the result of applying the given row permutation to the matrix */ protected FieldMatrix permuteRows(FieldMatrix matrix, int[] permutation) { if (!matrix.isSquare()) { throw new NonSquareMatrixException(matrix.getRowDimension(), matrix.getColumnDimension()); } if (matrix.getRowDimension() != permutation.length) { throw new DimensionMismatchException(matrix.getRowDimension(), permutation.length); } int n = matrix.getRowDimension(); int m = matrix.getColumnDimension(); Fraction out[][] = new Fraction[m][n]; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { out[i][j] = matrix.getEntry(permutation[i], j); } } return new Array2DRowFieldMatrix(out); } } commons-math3-3.2-src/src/test/java/org/apache/commons/math3/linear/CholeskyDecompositionTest.java100644 1750 1750 12330 12126627674 32076 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.linear; import org.junit.Test; import org.junit.Assert; public class CholeskyDecompositionTest { private double[][] testData = new double[][] { { 1, 2, 4, 7, 11 }, { 2, 13, 23, 38, 58 }, { 4, 23, 77, 122, 182 }, { 7, 38, 122, 294, 430 }, { 11, 58, 182, 430, 855 } }; /** test dimensions */ @Test public void testDimensions() { CholeskyDecomposition llt = new CholeskyDecomposition(MatrixUtils.createRealMatrix(testData)); Assert.assertEquals(testData.length, llt.getL().getRowDimension()); Assert.assertEquals(testData.length, llt.getL().getColumnDimension()); Assert.assertEquals(testData.length, llt.getLT().getRowDimension()); Assert.assertEquals(testData.length, llt.getLT().getColumnDimension()); } /** test non-square matrix */ @Test(expected = NonSquareMatrixException.class) public void testNonSquare() { new CholeskyDecomposition(MatrixUtils.createRealMatrix(new double[3][2])); } /** test non-symmetric matrix */ @Test(expected = NonSymmetricMatrixException.class) public void testNotSymmetricMatrixException() { double[][] changed = testData.clone(); changed[0][changed[0].length - 1] += 1.0e-5; new CholeskyDecomposition(MatrixUtils.createRealMatrix(changed)); } /** test non positive definite matrix */ @Test(expected = NonPositiveDefiniteMatrixException.class) public void testNotPositiveDefinite() { new CholeskyDecomposition(MatrixUtils.createRealMatrix(new double[][] { { 14, 11, 13, 15, 24 }, { 11, 34, 13, 8, 25 }, { 13, 13, 14, 15, 21 }, { 15, 8, 15, 18, 23 }, { 24, 25, 21, 23, 45 } })); } @Test(expected = NonPositiveDefiniteMatrixException.class) public void testMath274() { new CholeskyDecomposition(MatrixUtils.createRealMatrix(new double[][] { { 0.40434286, -0.09376327, 0.30328980, 0.04909388 }, {-0.09376327, 0.10400408, 0.07137959, 0.04762857 }, { 0.30328980, 0.07137959, 0.30458776, 0.04882449 }, { 0.04909388, 0.04762857, 0.04882449, 0.07543265 } })); } /** test A = LLT */ @Test public void testAEqualLLT() { RealMatrix matrix = MatrixUtils.createRealMatrix(testData); CholeskyDecomposition llt = new CholeskyDecomposition(matrix); RealMatrix l = llt.getL(); RealMatrix lt = llt.getLT(); double norm = l.multiply(lt).subtract(matrix).getNorm(); Assert.assertEquals(0, norm, 1.0e-15); } /** test that L is lower triangular */ @Test public void testLLowerTriangular() { RealMatrix matrix = MatrixUtils.createRealMatrix(testData); RealMatrix l = new CholeskyDecomposition(matrix).getL(); for (int i = 0; i < l.getRowDimension(); i++) { for (int j = i + 1; j < l.getColumnDimension(); j++) { Assert.assertEquals(0.0, l.getEntry(i, j), 0.0); } } } /** test that LT is transpose of L */ @Test public void testLTTransposed() { RealMatrix matrix = MatrixUtils.createRealMatrix(testData); CholeskyDecomposition llt = new CholeskyDecomposition(matrix); RealMatrix l = llt.getL(); RealMatrix lt = llt.getLT(); double norm = l.subtract(lt.transpose()).getNorm(); Assert.assertEquals(0, norm, 1.0e-15); } /** test matrices values */ @Test public void testMatricesValues() { RealMatrix lRef = MatrixUtils.createRealMatrix(new double[][] { { 1, 0, 0, 0, 0 }, { 2, 3, 0, 0, 0 }, { 4, 5, 6, 0, 0 }, { 7, 8, 9, 10, 0 }, { 11, 12, 13, 14, 15 } }); CholeskyDecomposition llt = new CholeskyDecomposition(MatrixUtils.createRealMatrix(testData)); // check values against known references RealMatrix l = llt.getL(); Assert.assertEquals(0, l.subtract(lRef).getNorm(), 1.0e-13); RealMatrix lt = llt.getLT(); Assert.assertEquals(0, lt.subtract(lRef.transpose()).getNorm(), 1.0e-13); // check the same cached instance is returned the second time Assert.assertTrue(l == llt.getL()); Assert.assertTrue(lt == llt.getLT()); } } commons-math3-3.2-src/src/test/java/org/apache/commons/math3/linear/BlockRealMatrixTest.java100644 1750 1750 136070 12126627674 30633 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.linear; import java.util.Arrays; import java.util.Random; import org.junit.Test; import org.junit.Assert; import org.apache.commons.math3.TestUtils; import org.apache.commons.math3.util.FastMath; import org.apache.commons.math3.exception.NullArgumentException; import org.apache.commons.math3.exception.OutOfRangeException; import org.apache.commons.math3.exception.NoDataException; import org.apache.commons.math3.exception.NumberIsTooSmallException; import org.apache.commons.math3.exception.MathIllegalArgumentException; /** * Test cases for the {@link BlockRealMatrix} class. * * @version $Id: BlockRealMatrixTest.java 1364030 2012-07-21 01:10:04Z erans $ */ public final class BlockRealMatrixTest { // 3 x 3 identity matrix protected double[][] id = { {1d,0d,0d}, {0d,1d,0d}, {0d,0d,1d} }; // Test data for group operations protected double[][] testData = { {1d,2d,3d}, {2d,5d,3d}, {1d,0d,8d} }; protected double[][] testDataLU = {{2d, 5d, 3d}, {.5d, -2.5d, 6.5d}, {0.5d, 0.2d, .2d}}; protected double[][] testDataPlus2 = { {3d,4d,5d}, {4d,7d,5d}, {3d,2d,10d} }; protected double[][] testDataMinus = { {-1d,-2d,-3d}, {-2d,-5d,-3d}, {-1d,0d,-8d} }; protected double[] testDataRow1 = {1d,2d,3d}; protected double[] testDataCol3 = {3d,3d,8d}; protected double[][] testDataInv = { {-40d,16d,9d}, {13d,-5d,-3d}, {5d,-2d,-1d} }; protected double[] preMultTest = {8,12,33}; protected double[][] testData2 ={ {1d,2d,3d}, {2d,5d,3d}}; protected double[][] testData2T = { {1d,2d}, {2d,5d}, {3d,3d}}; protected double[][] testDataPlusInv = { {-39d,18d,12d}, {15d,0d,0d}, {6d,-2d,7d} }; // lu decomposition tests protected double[][] luData = { {2d,3d,3d}, {0d,5d,7d}, {6d,9d,8d} }; protected double[][] luDataLUDecomposition = { {6d,9d,8d}, {0d,5d,7d}, {0.33333333333333,0d,0.33333333333333} }; // singular matrices protected double[][] singular = { {2d,3d}, {2d,3d} }; protected double[][] bigSingular = {{1d,2d,3d,4d}, {2d,5d,3d,4d}, {7d,3d,256d,1930d}, {3d,7d,6d,8d}}; // 4th row = 1st + 2nd protected double[][] detData = { {1d,2d,3d}, {4d,5d,6d}, {7d,8d,10d} }; protected double[][] detData2 = { {1d, 3d}, {2d, 4d}}; // vectors protected double[] testVector = {1,2,3}; protected double[] testVector2 = {1,2,3,4}; // submatrix accessor tests protected double[][] subTestData = {{1, 2, 3, 4}, {1.5, 2.5, 3.5, 4.5}, {2, 4, 6, 8}, {4, 5, 6, 7}}; // array selections protected double[][] subRows02Cols13 = { {2, 4}, {4, 8}}; protected double[][] subRows03Cols12 = { {2, 3}, {5, 6}}; protected double[][] subRows03Cols123 = { {2, 3, 4} , {5, 6, 7}}; // effective permutations protected double[][] subRows20Cols123 = { {4, 6, 8} , {2, 3, 4}}; protected double[][] subRows31Cols31 = {{7, 5}, {4.5, 2.5}}; // contiguous ranges protected double[][] subRows01Cols23 = {{3,4} , {3.5, 4.5}}; protected double[][] subRows23Cols00 = {{2} , {4}}; protected double[][] subRows00Cols33 = {{4}}; // row matrices protected double[][] subRow0 = {{1,2,3,4}}; protected double[][] subRow3 = {{4,5,6,7}}; // column matrices protected double[][] subColumn1 = {{2}, {2.5}, {4}, {5}}; protected double[][] subColumn3 = {{4}, {4.5}, {8}, {7}}; // tolerances protected double entryTolerance = 10E-16; protected double normTolerance = 10E-14; /** test dimensions */ @Test public void testDimensions() { BlockRealMatrix m = new BlockRealMatrix(testData); BlockRealMatrix m2 = new BlockRealMatrix(testData2); Assert.assertEquals("testData row dimension",3,m.getRowDimension()); Assert.assertEquals("testData column dimension",3,m.getColumnDimension()); Assert.assertTrue("testData is square",m.isSquare()); Assert.assertEquals("testData2 row dimension",m2.getRowDimension(),2); Assert.assertEquals("testData2 column dimension",m2.getColumnDimension(),3); Assert.assertTrue("testData2 is not square",!m2.isSquare()); } /** test copy functions */ @Test public void testCopyFunctions() { Random r = new Random(66636328996002l); BlockRealMatrix m1 = createRandomMatrix(r, 47, 83); BlockRealMatrix m2 = new BlockRealMatrix(m1.getData()); Assert.assertEquals(m1, m2); BlockRealMatrix m3 = new BlockRealMatrix(testData); BlockRealMatrix m4 = new BlockRealMatrix(m3.getData()); Assert.assertEquals(m3, m4); } /** test add */ @Test public void testAdd() { BlockRealMatrix m = new BlockRealMatrix(testData); BlockRealMatrix mInv = new BlockRealMatrix(testDataInv); RealMatrix mPlusMInv = m.add(mInv); double[][] sumEntries = mPlusMInv.getData(); for (int row = 0; row < m.getRowDimension(); row++) { for (int col = 0; col < m.getColumnDimension(); col++) { Assert.assertEquals("sum entry entry", testDataPlusInv[row][col],sumEntries[row][col], entryTolerance); } } } /** test add failure */ @Test public void testAddFail() { BlockRealMatrix m = new BlockRealMatrix(testData); BlockRealMatrix m2 = new BlockRealMatrix(testData2); try { m.add(m2); Assert.fail("MathIllegalArgumentException expected"); } catch (MathIllegalArgumentException ex) { // ignored } } /** test norm */ @Test public void testNorm() { BlockRealMatrix m = new BlockRealMatrix(testData); BlockRealMatrix m2 = new BlockRealMatrix(testData2); Assert.assertEquals("testData norm",14d,m.getNorm(),entryTolerance); Assert.assertEquals("testData2 norm",7d,m2.getNorm(),entryTolerance); } /** test Frobenius norm */ @Test public void testFrobeniusNorm() { BlockRealMatrix m = new BlockRealMatrix(testData); BlockRealMatrix m2 = new BlockRealMatrix(testData2); Assert.assertEquals("testData Frobenius norm", FastMath.sqrt(117.0), m.getFrobeniusNorm(), entryTolerance); Assert.assertEquals("testData2 Frobenius norm", FastMath.sqrt(52.0), m2.getFrobeniusNorm(), entryTolerance); } /** test m-n = m + -n */ @Test public void testPlusMinus() { BlockRealMatrix m = new BlockRealMatrix(testData); BlockRealMatrix m2 = new BlockRealMatrix(testDataInv); assertClose(m.subtract(m2), m2.scalarMultiply(-1d).add(m), entryTolerance); try { m.subtract(new BlockRealMatrix(testData2)); Assert.fail("Expecting illegalArgumentException"); } catch (MathIllegalArgumentException ex) { // ignored } } /** test multiply */ @Test public void testMultiply() { BlockRealMatrix m = new BlockRealMatrix(testData); BlockRealMatrix mInv = new BlockRealMatrix(testDataInv); BlockRealMatrix identity = new BlockRealMatrix(id); BlockRealMatrix m2 = new BlockRealMatrix(testData2); assertClose(m.multiply(mInv), identity, entryTolerance); assertClose(mInv.multiply(m), identity, entryTolerance); assertClose(m.multiply(identity), m, entryTolerance); assertClose(identity.multiply(mInv), mInv, entryTolerance); assertClose(m2.multiply(identity), m2, entryTolerance); try { m.multiply(new BlockRealMatrix(bigSingular)); Assert.fail("Expecting illegalArgumentException"); } catch (MathIllegalArgumentException ex) { // expected } } @Test public void testSeveralBlocks() { RealMatrix m = new BlockRealMatrix(35, 71); for (int i = 0; i < m.getRowDimension(); ++i) { for (int j = 0; j < m.getColumnDimension(); ++j) { m.setEntry(i, j, i + j / 1024.0); } } RealMatrix mT = m.transpose(); Assert.assertEquals(m.getRowDimension(), mT.getColumnDimension()); Assert.assertEquals(m.getColumnDimension(), mT.getRowDimension()); for (int i = 0; i < mT.getRowDimension(); ++i) { for (int j = 0; j < mT.getColumnDimension(); ++j) { Assert.assertEquals(m.getEntry(j, i), mT.getEntry(i, j), 0); } } RealMatrix mPm = m.add(m); for (int i = 0; i < mPm.getRowDimension(); ++i) { for (int j = 0; j < mPm.getColumnDimension(); ++j) { Assert.assertEquals(2 * m.getEntry(i, j), mPm.getEntry(i, j), 0); } } RealMatrix mPmMm = mPm.subtract(m); for (int i = 0; i < mPmMm.getRowDimension(); ++i) { for (int j = 0; j < mPmMm.getColumnDimension(); ++j) { Assert.assertEquals(m.getEntry(i, j), mPmMm.getEntry(i, j), 0); } } RealMatrix mTm = mT.multiply(m); for (int i = 0; i < mTm.getRowDimension(); ++i) { for (int j = 0; j < mTm.getColumnDimension(); ++j) { double sum = 0; for (int k = 0; k < mT.getColumnDimension(); ++k) { sum += (k + i / 1024.0) * (k + j / 1024.0); } Assert.assertEquals(sum, mTm.getEntry(i, j), 0); } } RealMatrix mmT = m.multiply(mT); for (int i = 0; i < mmT.getRowDimension(); ++i) { for (int j = 0; j < mmT.getColumnDimension(); ++j) { double sum = 0; for (int k = 0; k < m.getColumnDimension(); ++k) { sum += (i + k / 1024.0) * (j + k / 1024.0); } Assert.assertEquals(sum, mmT.getEntry(i, j), 0); } } RealMatrix sub1 = m.getSubMatrix(2, 9, 5, 20); for (int i = 0; i < sub1.getRowDimension(); ++i) { for (int j = 0; j < sub1.getColumnDimension(); ++j) { Assert.assertEquals((i + 2) + (j + 5) / 1024.0, sub1.getEntry(i, j), 0); } } RealMatrix sub2 = m.getSubMatrix(10, 12, 3, 70); for (int i = 0; i < sub2.getRowDimension(); ++i) { for (int j = 0; j < sub2.getColumnDimension(); ++j) { Assert.assertEquals((i + 10) + (j + 3) / 1024.0, sub2.getEntry(i, j), 0); } } RealMatrix sub3 = m.getSubMatrix(30, 34, 0, 5); for (int i = 0; i < sub3.getRowDimension(); ++i) { for (int j = 0; j < sub3.getColumnDimension(); ++j) { Assert.assertEquals((i + 30) + (j + 0) / 1024.0, sub3.getEntry(i, j), 0); } } RealMatrix sub4 = m.getSubMatrix(30, 32, 62, 65); for (int i = 0; i < sub4.getRowDimension(); ++i) { for (int j = 0; j < sub4.getColumnDimension(); ++j) { Assert.assertEquals((i + 30) + (j + 62) / 1024.0, sub4.getEntry(i, j), 0); } } } //Additional Test for BlockRealMatrixTest.testMultiply private double[][] d3 = new double[][] {{1,2,3,4},{5,6,7,8}}; private double[][] d4 = new double[][] {{1},{2},{3},{4}}; private double[][] d5 = new double[][] {{30},{70}}; @Test public void testMultiply2() { RealMatrix m3 = new BlockRealMatrix(d3); RealMatrix m4 = new BlockRealMatrix(d4); RealMatrix m5 = new BlockRealMatrix(d5); assertClose(m3.multiply(m4), m5, entryTolerance); } /** test trace */ @Test public void testTrace() { RealMatrix m = new BlockRealMatrix(id); Assert.assertEquals("identity trace",3d,m.getTrace(),entryTolerance); m = new BlockRealMatrix(testData2); try { m.getTrace(); Assert.fail("Expecting NonSquareMatrixException"); } catch (NonSquareMatrixException ex) { // ignored } } /** test scalarAdd */ @Test public void testScalarAdd() { RealMatrix m = new BlockRealMatrix(testData); assertClose(new BlockRealMatrix(testDataPlus2), m.scalarAdd(2d), entryTolerance); } /** test operate */ @Test public void testOperate() { RealMatrix m = new BlockRealMatrix(id); assertClose(testVector, m.operate(testVector), entryTolerance); assertClose(testVector, m.operate(new ArrayRealVector(testVector)).toArray(), entryTolerance); m = new BlockRealMatrix(bigSingular); try { m.operate(testVector); Assert.fail("Expecting illegalArgumentException"); } catch (MathIllegalArgumentException ex) { // ignored } } @Test public void testOperateLarge() { int p = (7 * BlockRealMatrix.BLOCK_SIZE) / 2; int q = (5 * BlockRealMatrix.BLOCK_SIZE) / 2; int r = 3 * BlockRealMatrix.BLOCK_SIZE; Random random = new Random(111007463902334l); RealMatrix m1 = createRandomMatrix(random, p, q); RealMatrix m2 = createRandomMatrix(random, q, r); RealMatrix m1m2 = m1.multiply(m2); for (int i = 0; i < r; ++i) { checkArrays(m1m2.getColumn(i), m1.operate(m2.getColumn(i))); } } @Test public void testOperatePremultiplyLarge() { int p = (7 * BlockRealMatrix.BLOCK_SIZE) / 2; int q = (5 * BlockRealMatrix.BLOCK_SIZE) / 2; int r = 3 * BlockRealMatrix.BLOCK_SIZE; Random random = new Random(111007463902334l); RealMatrix m1 = createRandomMatrix(random, p, q); RealMatrix m2 = createRandomMatrix(random, q, r); RealMatrix m1m2 = m1.multiply(m2); for (int i = 0; i < p; ++i) { checkArrays(m1m2.getRow(i), m2.preMultiply(m1.getRow(i))); } } /** test issue MATH-209 */ @Test public void testMath209() { RealMatrix a = new BlockRealMatrix(new double[][] { { 1, 2 }, { 3, 4 }, { 5, 6 } }); double[] b = a.operate(new double[] { 1, 1 }); Assert.assertEquals(a.getRowDimension(), b.length); Assert.assertEquals( 3.0, b[0], 1.0e-12); Assert.assertEquals( 7.0, b[1], 1.0e-12); Assert.assertEquals(11.0, b[2], 1.0e-12); } /** test transpose */ @Test public void testTranspose() { RealMatrix m = new BlockRealMatrix(testData); RealMatrix mIT = new LUDecomposition(m).getSolver().getInverse().transpose(); RealMatrix mTI = new LUDecomposition(m.transpose()).getSolver().getInverse(); assertClose(mIT, mTI, normTolerance); m = new BlockRealMatrix(testData2); RealMatrix mt = new BlockRealMatrix(testData2T); assertClose(mt, m.transpose(), normTolerance); } /** test preMultiply by vector */ @Test public void testPremultiplyVector() { RealMatrix m = new BlockRealMatrix(testData); assertClose(m.preMultiply(testVector), preMultTest, normTolerance); assertClose(m.preMultiply(new ArrayRealVector(testVector).toArray()), preMultTest, normTolerance); m = new BlockRealMatrix(bigSingular); try { m.preMultiply(testVector); Assert.fail("expecting MathIllegalArgumentException"); } catch (MathIllegalArgumentException ex) { // ignored } } @Test public void testPremultiply() { RealMatrix m3 = new BlockRealMatrix(d3); RealMatrix m4 = new BlockRealMatrix(d4); RealMatrix m5 = new BlockRealMatrix(d5); assertClose(m4.preMultiply(m3), m5, entryTolerance); BlockRealMatrix m = new BlockRealMatrix(testData); BlockRealMatrix mInv = new BlockRealMatrix(testDataInv); BlockRealMatrix identity = new BlockRealMatrix(id); assertClose(m.preMultiply(mInv), identity, entryTolerance); assertClose(mInv.preMultiply(m), identity, entryTolerance); assertClose(m.preMultiply(identity), m, entryTolerance); assertClose(identity.preMultiply(mInv), mInv, entryTolerance); try { m.preMultiply(new BlockRealMatrix(bigSingular)); Assert.fail("Expecting illegalArgumentException"); } catch (MathIllegalArgumentException ex) { // ignored } } @Test public void testGetVectors() { RealMatrix m = new BlockRealMatrix(testData); assertClose(m.getRow(0), testDataRow1, entryTolerance); assertClose(m.getColumn(2), testDataCol3, entryTolerance); try { m.getRow(10); Assert.fail("expecting OutOfRangeException"); } catch (OutOfRangeException ex) { // ignored } try { m.getColumn(-1); Assert.fail("expecting OutOfRangeException"); } catch (OutOfRangeException ex) { // ignored } } @Test public void testGetEntry() { RealMatrix m = new BlockRealMatrix(testData); Assert.assertEquals("get entry",m.getEntry(0,1),2d,entryTolerance); try { m.getEntry(10, 4); Assert.fail ("Expecting OutOfRangeException"); } catch (OutOfRangeException ex) { // expected } } /** test examples in user guide */ @Test public void testExamples() { // Create a real matrix with two rows and three columns double[][] matrixData = { {1d,2d,3d}, {2d,5d,3d}}; RealMatrix m = new BlockRealMatrix(matrixData); // One more with three rows, two columns double[][] matrixData2 = { {1d,2d}, {2d,5d}, {1d, 7d}}; RealMatrix n = new BlockRealMatrix(matrixData2); // Now multiply m by n RealMatrix p = m.multiply(n); Assert.assertEquals(2, p.getRowDimension()); Assert.assertEquals(2, p.getColumnDimension()); // Invert p RealMatrix pInverse = new LUDecomposition(p).getSolver().getInverse(); Assert.assertEquals(2, pInverse.getRowDimension()); Assert.assertEquals(2, pInverse.getColumnDimension()); // Solve example double[][] coefficientsData = {{2, 3, -2}, {-1, 7, 6}, {4, -3, -5}}; RealMatrix coefficients = new BlockRealMatrix(coefficientsData); RealVector constants = new ArrayRealVector(new double[]{1, -2, 1}, false); RealVector solution = new LUDecomposition(coefficients).getSolver().solve(constants); final double cst0 = constants.getEntry(0); final double cst1 = constants.getEntry(1); final double cst2 = constants.getEntry(2); final double sol0 = solution.getEntry(0); final double sol1 = solution.getEntry(1); final double sol2 = solution.getEntry(2); Assert.assertEquals(2 * sol0 + 3 * sol1 -2 * sol2, cst0, 1E-12); Assert.assertEquals(-1 * sol0 + 7 * sol1 + 6 * sol2, cst1, 1E-12); Assert.assertEquals(4 * sol0 - 3 * sol1 -5 * sol2, cst2, 1E-12); } // test submatrix accessors @Test public void testGetSubMatrix() { RealMatrix m = new BlockRealMatrix(subTestData); checkGetSubMatrix(m, subRows23Cols00, 2 , 3 , 0, 0); checkGetSubMatrix(m, subRows00Cols33, 0 , 0 , 3, 3); checkGetSubMatrix(m, subRows01Cols23, 0 , 1 , 2, 3); checkGetSubMatrix(m, subRows02Cols13, new int[] { 0, 2 }, new int[] { 1, 3 }); checkGetSubMatrix(m, subRows03Cols12, new int[] { 0, 3 }, new int[] { 1, 2 }); checkGetSubMatrix(m, subRows03Cols123, new int[] { 0, 3 }, new int[] { 1, 2, 3 }); checkGetSubMatrix(m, subRows20Cols123, new int[] { 2, 0 }, new int[] { 1, 2, 3 }); checkGetSubMatrix(m, subRows31Cols31, new int[] { 3, 1 }, new int[] { 3, 1 }); checkGetSubMatrix(m, subRows31Cols31, new int[] { 3, 1 }, new int[] { 3, 1 }); checkGetSubMatrix(m, null, 1, 0, 2, 4); checkGetSubMatrix(m, null, -1, 1, 2, 2); checkGetSubMatrix(m, null, 1, 0, 2, 2); checkGetSubMatrix(m, null, 1, 0, 2, 4); checkGetSubMatrix(m, null, new int[] {}, new int[] { 0 }); checkGetSubMatrix(m, null, new int[] { 0 }, new int[] { 4 }); } private void checkGetSubMatrix(RealMatrix m, double[][] reference, int startRow, int endRow, int startColumn, int endColumn) { try { RealMatrix sub = m.getSubMatrix(startRow, endRow, startColumn, endColumn); if (reference != null) { Assert.assertEquals(new BlockRealMatrix(reference), sub); } else { Assert.fail("Expecting OutOfRangeException or NumberIsTooSmallException or NoDataException"); } } catch (OutOfRangeException e) { if (reference != null) { throw e; } } catch (NumberIsTooSmallException e) { if (reference != null) { throw e; } } catch (NoDataException e) { if (reference != null) { throw e; } } } private void checkGetSubMatrix(RealMatrix m, double[][] reference, int[] selectedRows, int[] selectedColumns) { try { RealMatrix sub = m.getSubMatrix(selectedRows, selectedColumns); if (reference != null) { Assert.assertEquals(new BlockRealMatrix(reference), sub); } else { Assert.fail("Expecting OutOfRangeException or NumberIsTooSmallExceptiono r NoDataException"); } } catch (OutOfRangeException e) { if (reference != null) { throw e; } } catch (NumberIsTooSmallException e) { if (reference != null) { throw e; } } catch (NoDataException e) { if (reference != null) { throw e; } } } @Test public void testGetSetMatrixLarge() { int n = 3 * BlockRealMatrix.BLOCK_SIZE; RealMatrix m = new BlockRealMatrix(n, n); RealMatrix sub = new BlockRealMatrix(n - 4, n - 4).scalarAdd(1); m.setSubMatrix(sub.getData(), 2, 2); for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j) { if ((i < 2) || (i > n - 3) || (j < 2) || (j > n - 3)) { Assert.assertEquals(0.0, m.getEntry(i, j), 0.0); } else { Assert.assertEquals(1.0, m.getEntry(i, j), 0.0); } } } Assert.assertEquals(sub, m.getSubMatrix(2, n - 3, 2, n - 3)); } @Test public void testCopySubMatrix() { RealMatrix m = new BlockRealMatrix(subTestData); checkCopy(m, subRows23Cols00, 2 , 3 , 0, 0); checkCopy(m, subRows00Cols33, 0 , 0 , 3, 3); checkCopy(m, subRows01Cols23, 0 , 1 , 2, 3); checkCopy(m, subRows02Cols13, new int[] { 0, 2 }, new int[] { 1, 3 }); checkCopy(m, subRows03Cols12, new int[] { 0, 3 }, new int[] { 1, 2 }); checkCopy(m, subRows03Cols123, new int[] { 0, 3 }, new int[] { 1, 2, 3 }); checkCopy(m, subRows20Cols123, new int[] { 2, 0 }, new int[] { 1, 2, 3 }); checkCopy(m, subRows31Cols31, new int[] { 3, 1 }, new int[] { 3, 1 }); checkCopy(m, subRows31Cols31, new int[] { 3, 1 }, new int[] { 3, 1 }); checkCopy(m, null, 1, 0, 2, 4); checkCopy(m, null, -1, 1, 2, 2); checkCopy(m, null, 1, 0, 2, 2); checkCopy(m, null, 1, 0, 2, 4); checkCopy(m, null, new int[] {}, new int[] { 0 }); checkCopy(m, null, new int[] { 0 }, new int[] { 4 }); } private void checkCopy(RealMatrix m, double[][] reference, int startRow, int endRow, int startColumn, int endColumn) { try { double[][] sub = (reference == null) ? new double[1][1] : new double[reference.length][reference[0].length]; m.copySubMatrix(startRow, endRow, startColumn, endColumn, sub); if (reference != null) { Assert.assertEquals(new BlockRealMatrix(reference), new BlockRealMatrix(sub)); } else { Assert.fail("Expecting OutOfRangeException or NumberIsTooSmallException or NoDataException"); } } catch (OutOfRangeException e) { if (reference != null) { throw e; } } catch (NumberIsTooSmallException e) { if (reference != null) { throw e; } } catch (NoDataException e) { if (reference != null) { throw e; } } } private void checkCopy(RealMatrix m, double[][] reference, int[] selectedRows, int[] selectedColumns) { try { double[][] sub = (reference == null) ? new double[1][1] : new double[reference.length][reference[0].length]; m.copySubMatrix(selectedRows, selectedColumns, sub); if (reference != null) { Assert.assertEquals(new BlockRealMatrix(reference), new BlockRealMatrix(sub)); } else { Assert.fail("Expecting OutOfRangeException or NumberIsTooSmallException or NoDataException"); } } catch (OutOfRangeException e) { if (reference != null) { throw e; } } catch (NumberIsTooSmallException e) { if (reference != null) { throw e; } } catch (NoDataException e) { if (reference != null) { throw e; } } } @Test public void testGetRowMatrix() { RealMatrix m = new BlockRealMatrix(subTestData); RealMatrix mRow0 = new BlockRealMatrix(subRow0); RealMatrix mRow3 = new BlockRealMatrix(subRow3); Assert.assertEquals("Row0", mRow0, m.getRowMatrix(0)); Assert.assertEquals("Row3", mRow3, m.getRowMatrix(3)); try { m.getRowMatrix(-1); Assert.fail("Expecting OutOfRangeException"); } catch (OutOfRangeException ex) { // expected } try { m.getRowMatrix(4); Assert.fail("Expecting OutOfRangeException"); } catch (OutOfRangeException ex) { // expected } } @Test public void testSetRowMatrix() { RealMatrix m = new BlockRealMatrix(subTestData); RealMatrix mRow3 = new BlockRealMatrix(subRow3); Assert.assertNotSame(mRow3, m.getRowMatrix(0)); m.setRowMatrix(0, mRow3); Assert.assertEquals(mRow3, m.getRowMatrix(0)); try { m.setRowMatrix(-1, mRow3); Assert.fail("Expecting OutOfRangeException"); } catch (OutOfRangeException ex) { // expected } try { m.setRowMatrix(0, m); Assert.fail("Expecting MatrixDimensionMismatchException"); } catch (MatrixDimensionMismatchException ex) { // expected } } @Test public void testGetSetRowMatrixLarge() { int n = 3 * BlockRealMatrix.BLOCK_SIZE; RealMatrix m = new BlockRealMatrix(n, n); RealMatrix sub = new BlockRealMatrix(1, n).scalarAdd(1); m.setRowMatrix(2, sub); for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j) { if (i != 2) { Assert.assertEquals(0.0, m.getEntry(i, j), 0.0); } else { Assert.assertEquals(1.0, m.getEntry(i, j), 0.0); } } } Assert.assertEquals(sub, m.getRowMatrix(2)); } @Test public void testGetColumnMatrix() { RealMatrix m = new BlockRealMatrix(subTestData); RealMatrix mColumn1 = new BlockRealMatrix(subColumn1); RealMatrix mColumn3 = new BlockRealMatrix(subColumn3); Assert.assertEquals(mColumn1, m.getColumnMatrix(1)); Assert.assertEquals(mColumn3, m.getColumnMatrix(3)); try { m.getColumnMatrix(-1); Assert.fail("Expecting OutOfRangeException"); } catch (OutOfRangeException ex) { // expected } try { m.getColumnMatrix(4); Assert.fail("Expecting OutOfRangeException"); } catch (OutOfRangeException ex) { // expected } } @Test public void testSetColumnMatrix() { RealMatrix m = new BlockRealMatrix(subTestData); RealMatrix mColumn3 = new BlockRealMatrix(subColumn3); Assert.assertNotSame(mColumn3, m.getColumnMatrix(1)); m.setColumnMatrix(1, mColumn3); Assert.assertEquals(mColumn3, m.getColumnMatrix(1)); try { m.setColumnMatrix(-1, mColumn3); Assert.fail("Expecting OutOfRangeException"); } catch (OutOfRangeException ex) { // expected } try { m.setColumnMatrix(0, m); Assert.fail("Expecting MatrixDimensionMismatchException"); } catch (MatrixDimensionMismatchException ex) { // expected } } @Test public void testGetSetColumnMatrixLarge() { int n = 3 * BlockRealMatrix.BLOCK_SIZE; RealMatrix m = new BlockRealMatrix(n, n); RealMatrix sub = new BlockRealMatrix(n, 1).scalarAdd(1); m.setColumnMatrix(2, sub); for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j) { if (j != 2) { Assert.assertEquals(0.0, m.getEntry(i, j), 0.0); } else { Assert.assertEquals(1.0, m.getEntry(i, j), 0.0); } } } Assert.assertEquals(sub, m.getColumnMatrix(2)); } @Test public void testGetRowVector() { RealMatrix m = new BlockRealMatrix(subTestData); RealVector mRow0 = new ArrayRealVector(subRow0[0]); RealVector mRow3 = new ArrayRealVector(subRow3[0]); Assert.assertEquals(mRow0, m.getRowVector(0)); Assert.assertEquals(mRow3, m.getRowVector(3)); try { m.getRowVector(-1); Assert.fail("Expecting OutOfRangeException"); } catch (OutOfRangeException ex) { // expected } try { m.getRowVector(4); Assert.fail("Expecting OutOfRangeException"); } catch (OutOfRangeException ex) { // expected } } @Test public void testSetRowVector() { RealMatrix m = new BlockRealMatrix(subTestData); RealVector mRow3 = new ArrayRealVector(subRow3[0]); Assert.assertNotSame(mRow3, m.getRowMatrix(0)); m.setRowVector(0, mRow3); Assert.assertEquals(mRow3, m.getRowVector(0)); try { m.setRowVector(-1, mRow3); Assert.fail("Expecting OutOfRangeException"); } catch (OutOfRangeException ex) { // expected } try { m.setRowVector(0, new ArrayRealVector(5)); Assert.fail("Expecting MatrixDimensionMismatchException"); } catch (MatrixDimensionMismatchException ex) { // expected } } @Test public void testGetSetRowVectorLarge() { int n = 3 * BlockRealMatrix.BLOCK_SIZE; RealMatrix m = new BlockRealMatrix(n, n); RealVector sub = new ArrayRealVector(n, 1.0); m.setRowVector(2, sub); for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j) { if (i != 2) { Assert.assertEquals(0.0, m.getEntry(i, j), 0.0); } else { Assert.assertEquals(1.0, m.getEntry(i, j), 0.0); } } } Assert.assertEquals(sub, m.getRowVector(2)); } @Test public void testGetColumnVector() { RealMatrix m = new BlockRealMatrix(subTestData); RealVector mColumn1 = columnToVector(subColumn1); RealVector mColumn3 = columnToVector(subColumn3); Assert.assertEquals(mColumn1, m.getColumnVector(1)); Assert.assertEquals(mColumn3, m.getColumnVector(3)); try { m.getColumnVector(-1); Assert.fail("Expecting OutOfRangeException"); } catch (OutOfRangeException ex) { // expected } try { m.getColumnVector(4); Assert.fail("Expecting OutOfRangeException"); } catch (OutOfRangeException ex) { // expected } } @Test public void testSetColumnVector() { RealMatrix m = new BlockRealMatrix(subTestData); RealVector mColumn3 = columnToVector(subColumn3); Assert.assertNotSame(mColumn3, m.getColumnVector(1)); m.setColumnVector(1, mColumn3); Assert.assertEquals(mColumn3, m.getColumnVector(1)); try { m.setColumnVector(-1, mColumn3); Assert.fail("Expecting OutOfRangeException"); } catch (OutOfRangeException ex) { // expected } try { m.setColumnVector(0, new ArrayRealVector(5)); Assert.fail("Expecting MatrixDimensionMismatchException"); } catch (MatrixDimensionMismatchException ex) { // expected } } @Test public void testGetSetColumnVectorLarge() { int n = 3 * BlockRealMatrix.BLOCK_SIZE; RealMatrix m = new BlockRealMatrix(n, n); RealVector sub = new ArrayRealVector(n, 1.0); m.setColumnVector(2, sub); for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j) { if (j != 2) { Assert.assertEquals(0.0, m.getEntry(i, j), 0.0); } else { Assert.assertEquals(1.0, m.getEntry(i, j), 0.0); } } } Assert.assertEquals(sub, m.getColumnVector(2)); } private RealVector columnToVector(double[][] column) { double[] data = new double[column.length]; for (int i = 0; i < data.length; ++i) { data[i] = column[i][0]; } return new ArrayRealVector(data, false); } @Test public void testGetRow() { RealMatrix m = new BlockRealMatrix(subTestData); checkArrays(subRow0[0], m.getRow(0)); checkArrays(subRow3[0], m.getRow(3)); try { m.getRow(-1); Assert.fail("Expecting OutOfRangeException"); } catch (OutOfRangeException ex) { // expected } try { m.getRow(4); Assert.fail("Expecting OutOfRangeException"); } catch (OutOfRangeException ex) { // expected } } @Test public void testSetRow() { RealMatrix m = new BlockRealMatrix(subTestData); Assert.assertTrue(subRow3[0][0] != m.getRow(0)[0]); m.setRow(0, subRow3[0]); checkArrays(subRow3[0], m.getRow(0)); try { m.setRow(-1, subRow3[0]); Assert.fail("Expecting OutOfRangeException"); } catch (OutOfRangeException ex) { // expected } try { m.setRow(0, new double[5]); Assert.fail("Expecting MatrixDimensionMismatchException"); } catch (MatrixDimensionMismatchException ex) { // expected } } @Test public void testGetSetRowLarge() { int n = 3 * BlockRealMatrix.BLOCK_SIZE; RealMatrix m = new BlockRealMatrix(n, n); double[] sub = new double[n]; Arrays.fill(sub, 1.0); m.setRow(2, sub); for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j) { if (i != 2) { Assert.assertEquals(0.0, m.getEntry(i, j), 0.0); } else { Assert.assertEquals(1.0, m.getEntry(i, j), 0.0); } } } checkArrays(sub, m.getRow(2)); } @Test public void testGetColumn() { RealMatrix m = new BlockRealMatrix(subTestData); double[] mColumn1 = columnToArray(subColumn1); double[] mColumn3 = columnToArray(subColumn3); checkArrays(mColumn1, m.getColumn(1)); checkArrays(mColumn3, m.getColumn(3)); try { m.getColumn(-1); Assert.fail("Expecting OutOfRangeException"); } catch (OutOfRangeException ex) { // expected } try { m.getColumn(4); Assert.fail("Expecting OutOfRangeException"); } catch (OutOfRangeException ex) { // expected } } @Test public void testSetColumn() { RealMatrix m = new BlockRealMatrix(subTestData); double[] mColumn3 = columnToArray(subColumn3); Assert.assertTrue(mColumn3[0] != m.getColumn(1)[0]); m.setColumn(1, mColumn3); checkArrays(mColumn3, m.getColumn(1)); try { m.setColumn(-1, mColumn3); Assert.fail("Expecting OutOfRangeException"); } catch (OutOfRangeException ex) { // expected } try { m.setColumn(0, new double[5]); Assert.fail("Expecting MatrixDimensionMismatchException"); } catch (MatrixDimensionMismatchException ex) { // expected } } @Test public void testGetSetColumnLarge() { int n = 3 * BlockRealMatrix.BLOCK_SIZE; RealMatrix m = new BlockRealMatrix(n, n); double[] sub = new double[n]; Arrays.fill(sub, 1.0); m.setColumn(2, sub); for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j) { if (j != 2) { Assert.assertEquals(0.0, m.getEntry(i, j), 0.0); } else { Assert.assertEquals(1.0, m.getEntry(i, j), 0.0); } } } checkArrays(sub, m.getColumn(2)); } private double[] columnToArray(double[][] column) { double[] data = new double[column.length]; for (int i = 0; i < data.length; ++i) { data[i] = column[i][0]; } return data; } private void checkArrays(double[] expected, double[] actual) { Assert.assertEquals(expected.length, actual.length); for (int i = 0; i < expected.length; ++i) { Assert.assertEquals(expected[i], actual[i], 0); } } @Test public void testEqualsAndHashCode() { BlockRealMatrix m = new BlockRealMatrix(testData); BlockRealMatrix m1 = m.copy(); BlockRealMatrix mt = m.transpose(); Assert.assertTrue(m.hashCode() != mt.hashCode()); Assert.assertEquals(m.hashCode(), m1.hashCode()); Assert.assertEquals(m, m); Assert.assertEquals(m, m1); Assert.assertFalse(m.equals(null)); Assert.assertFalse(m.equals(mt)); Assert.assertFalse(m.equals(new BlockRealMatrix(bigSingular))); } @Test public void testToString() { BlockRealMatrix m = new BlockRealMatrix(testData); Assert.assertEquals("BlockRealMatrix{{1.0,2.0,3.0},{2.0,5.0,3.0},{1.0,0.0,8.0}}", m.toString()); } @Test public void testSetSubMatrix() { BlockRealMatrix m = new BlockRealMatrix(testData); m.setSubMatrix(detData2,1,1); RealMatrix expected = new BlockRealMatrix (new double[][] {{1.0,2.0,3.0},{2.0,1.0,3.0},{1.0,2.0,4.0}}); Assert.assertEquals(expected, m); m.setSubMatrix(detData2,0,0); expected = new BlockRealMatrix (new double[][] {{1.0,3.0,3.0},{2.0,4.0,3.0},{1.0,2.0,4.0}}); Assert.assertEquals(expected, m); m.setSubMatrix(testDataPlus2,0,0); expected = new BlockRealMatrix (new double[][] {{3.0,4.0,5.0},{4.0,7.0,5.0},{3.0,2.0,10.0}}); Assert.assertEquals(expected, m); // javadoc example BlockRealMatrix matrix = new BlockRealMatrix (new double[][] {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 0, 1 , 2}}); matrix.setSubMatrix(new double[][] {{3, 4}, {5, 6}}, 1, 1); expected = new BlockRealMatrix (new double[][] {{1, 2, 3, 4}, {5, 3, 4, 8}, {9, 5 ,6, 2}}); Assert.assertEquals(expected, matrix); // dimension overflow try { m.setSubMatrix(testData,1,1); Assert.fail("expecting OutOfRangeException"); } catch (OutOfRangeException e) { // expected } // dimension underflow try { m.setSubMatrix(testData,-1,1); Assert.fail("expecting OutOfRangeException"); } catch (OutOfRangeException e) { // expected } try { m.setSubMatrix(testData,1,-1); Assert.fail("expecting OutOfRangeException"); } catch (OutOfRangeException e) { // expected } // null try { m.setSubMatrix(null,1,1); Assert.fail("expecting NullArgumentException"); } catch (NullArgumentException e) { // expected } // ragged try { m.setSubMatrix(new double[][] {{1}, {2, 3}}, 0, 0); Assert.fail("expecting MathIllegalArgumentException"); } catch (MathIllegalArgumentException e) { // expected } // empty try { m.setSubMatrix(new double[][] {{}}, 0, 0); Assert.fail("expecting MathIllegalArgumentException"); } catch (MathIllegalArgumentException e) { // expected } } @Test public void testWalk() { int rows = 150; int columns = 75; RealMatrix m = new BlockRealMatrix(rows, columns); m.walkInRowOrder(new SetVisitor()); GetVisitor getVisitor = new GetVisitor(); m.walkInOptimizedOrder(getVisitor); Assert.assertEquals(rows * columns, getVisitor.getCount()); m = new BlockRealMatrix(rows, columns); m.walkInRowOrder(new SetVisitor(), 1, rows - 2, 1, columns - 2); getVisitor = new GetVisitor(); m.walkInOptimizedOrder(getVisitor, 1, rows - 2, 1, columns - 2); Assert.assertEquals((rows - 2) * (columns - 2), getVisitor.getCount()); for (int i = 0; i < rows; ++i) { Assert.assertEquals(0.0, m.getEntry(i, 0), 0); Assert.assertEquals(0.0, m.getEntry(i, columns - 1), 0); } for (int j = 0; j < columns; ++j) { Assert.assertEquals(0.0, m.getEntry(0, j), 0); Assert.assertEquals(0.0, m.getEntry(rows - 1, j), 0); } m = new BlockRealMatrix(rows, columns); m.walkInColumnOrder(new SetVisitor()); getVisitor = new GetVisitor(); m.walkInOptimizedOrder(getVisitor); Assert.assertEquals(rows * columns, getVisitor.getCount()); m = new BlockRealMatrix(rows, columns); m.walkInColumnOrder(new SetVisitor(), 1, rows - 2, 1, columns - 2); getVisitor = new GetVisitor(); m.walkInOptimizedOrder(getVisitor, 1, rows - 2, 1, columns - 2); Assert.assertEquals((rows - 2) * (columns - 2), getVisitor.getCount()); for (int i = 0; i < rows; ++i) { Assert.assertEquals(0.0, m.getEntry(i, 0), 0); Assert.assertEquals(0.0, m.getEntry(i, columns - 1), 0); } for (int j = 0; j < columns; ++j) { Assert.assertEquals(0.0, m.getEntry(0, j), 0); Assert.assertEquals(0.0, m.getEntry(rows - 1, j), 0); } m = new BlockRealMatrix(rows, columns); m.walkInOptimizedOrder(new SetVisitor()); getVisitor = new GetVisitor(); m.walkInRowOrder(getVisitor); Assert.assertEquals(rows * columns, getVisitor.getCount()); m = new BlockRealMatrix(rows, columns); m.walkInOptimizedOrder(new SetVisitor(), 1, rows - 2, 1, columns - 2); getVisitor = new GetVisitor(); m.walkInRowOrder(getVisitor, 1, rows - 2, 1, columns - 2); Assert.assertEquals((rows - 2) * (columns - 2), getVisitor.getCount()); for (int i = 0; i < rows; ++i) { Assert.assertEquals(0.0, m.getEntry(i, 0), 0); Assert.assertEquals(0.0, m.getEntry(i, columns - 1), 0); } for (int j = 0; j < columns; ++j) { Assert.assertEquals(0.0, m.getEntry(0, j), 0); Assert.assertEquals(0.0, m.getEntry(rows - 1, j), 0); } m = new BlockRealMatrix(rows, columns); m.walkInOptimizedOrder(new SetVisitor()); getVisitor = new GetVisitor(); m.walkInColumnOrder(getVisitor); Assert.assertEquals(rows * columns, getVisitor.getCount()); m = new BlockRealMatrix(rows, columns); m.walkInOptimizedOrder(new SetVisitor(), 1, rows - 2, 1, columns - 2); getVisitor = new GetVisitor(); m.walkInColumnOrder(getVisitor, 1, rows - 2, 1, columns - 2); Assert.assertEquals((rows - 2) * (columns - 2), getVisitor.getCount()); for (int i = 0; i < rows; ++i) { Assert.assertEquals(0.0, m.getEntry(i, 0), 0); Assert.assertEquals(0.0, m.getEntry(i, columns - 1), 0); } for (int j = 0; j < columns; ++j) { Assert.assertEquals(0.0, m.getEntry(0, j), 0); Assert.assertEquals(0.0, m.getEntry(rows - 1, j), 0); } } @Test public void testSerial() { BlockRealMatrix m = new BlockRealMatrix(testData); Assert.assertEquals(m,TestUtils.serializeAndRecover(m)); } private static class SetVisitor extends DefaultRealMatrixChangingVisitor { @Override public double visit(int i, int j, double value) { return i + j / 1024.0; } } private static class GetVisitor extends DefaultRealMatrixPreservingVisitor { private int count = 0; @Override public void visit(int i, int j, double value) { ++count; Assert.assertEquals(i + j / 1024.0, value, 0.0); } public int getCount() { return count; } } //--------------- -----------------Protected methods /** verifies that two matrices are close (1-norm) */ protected void assertClose(RealMatrix m, RealMatrix n, double tolerance) { Assert.assertTrue(m.subtract(n).getNorm() < tolerance); } /** verifies that two vectors are close (sup norm) */ protected void assertClose(double[] m, double[] n, double tolerance) { if (m.length != n.length) { Assert.fail("vectors not same length"); } for (int i = 0; i < m.length; i++) { Assert.assertEquals(m[i], n[i], tolerance); } } private BlockRealMatrix createRandomMatrix(Random r, int rows, int columns) { BlockRealMatrix m = new BlockRealMatrix(rows, columns); for (int i = 0; i < rows; ++i) { for (int j = 0; j < columns; ++j) { m.setEntry(i, j, 200 * r.nextDouble() - 100); } } return m; } } commons-math3-3.2-src/src/test/java/org/apache/commons/math3/linear/BiDiagonalTransformerTest.java100644 1750 1750 21032 12126627674 31773 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.linear; import org.apache.commons.math3.linear.BiDiagonalTransformer; import org.apache.commons.math3.linear.MatrixUtils; import org.apache.commons.math3.linear.RealMatrix; import org.apache.commons.math3.util.FastMath; import org.junit.Assert; import org.junit.Test; public class BiDiagonalTransformerTest { private double[][] testSquare = { { 24.0 / 25.0, 43.0 / 25.0 }, { 57.0 / 25.0, 24.0 / 25.0 } }; private double[][] testNonSquare = { { -540.0 / 625.0, 963.0 / 625.0, -216.0 / 625.0 }, { -1730.0 / 625.0, -744.0 / 625.0, 1008.0 / 625.0 }, { -720.0 / 625.0, 1284.0 / 625.0, -288.0 / 625.0 }, { -360.0 / 625.0, 192.0 / 625.0, 1756.0 / 625.0 }, }; @Test public void testDimensions() { checkdimensions(MatrixUtils.createRealMatrix(testSquare)); checkdimensions(MatrixUtils.createRealMatrix(testNonSquare)); checkdimensions(MatrixUtils.createRealMatrix(testNonSquare).transpose()); } private void checkdimensions(RealMatrix matrix) { final int m = matrix.getRowDimension(); final int n = matrix.getColumnDimension(); BiDiagonalTransformer transformer = new BiDiagonalTransformer(matrix); Assert.assertEquals(m, transformer.getU().getRowDimension()); Assert.assertEquals(m, transformer.getU().getColumnDimension()); Assert.assertEquals(m, transformer.getB().getRowDimension()); Assert.assertEquals(n, transformer.getB().getColumnDimension()); Assert.assertEquals(n, transformer.getV().getRowDimension()); Assert.assertEquals(n, transformer.getV().getColumnDimension()); } @Test public void testAEqualUSVt() { checkAEqualUSVt(MatrixUtils.createRealMatrix(testSquare)); checkAEqualUSVt(MatrixUtils.createRealMatrix(testNonSquare)); checkAEqualUSVt(MatrixUtils.createRealMatrix(testNonSquare).transpose()); } private void checkAEqualUSVt(RealMatrix matrix) { BiDiagonalTransformer transformer = new BiDiagonalTransformer(matrix); RealMatrix u = transformer.getU(); RealMatrix b = transformer.getB(); RealMatrix v = transformer.getV(); double norm = u.multiply(b).multiply(v.transpose()).subtract(matrix).getNorm(); Assert.assertEquals(0, norm, 1.0e-14); } @Test public void testUOrthogonal() { checkOrthogonal(new BiDiagonalTransformer(MatrixUtils.createRealMatrix(testSquare)).getU()); checkOrthogonal(new BiDiagonalTransformer(MatrixUtils.createRealMatrix(testNonSquare)).getU()); checkOrthogonal(new BiDiagonalTransformer(MatrixUtils.createRealMatrix(testNonSquare).transpose()).getU()); } @Test public void testVOrthogonal() { checkOrthogonal(new BiDiagonalTransformer(MatrixUtils.createRealMatrix(testSquare)).getV()); checkOrthogonal(new BiDiagonalTransformer(MatrixUtils.createRealMatrix(testNonSquare)).getV()); checkOrthogonal(new BiDiagonalTransformer(MatrixUtils.createRealMatrix(testNonSquare).transpose()).getV()); } private void checkOrthogonal(RealMatrix m) { RealMatrix mTm = m.transpose().multiply(m); RealMatrix id = MatrixUtils.createRealIdentityMatrix(mTm.getRowDimension()); Assert.assertEquals(0, mTm.subtract(id).getNorm(), 1.0e-14); } @Test public void testBBiDiagonal() { checkBiDiagonal(new BiDiagonalTransformer(MatrixUtils.createRealMatrix(testSquare)).getB()); checkBiDiagonal(new BiDiagonalTransformer(MatrixUtils.createRealMatrix(testNonSquare)).getB()); checkBiDiagonal(new BiDiagonalTransformer(MatrixUtils.createRealMatrix(testNonSquare).transpose()).getB()); } private void checkBiDiagonal(RealMatrix m) { final int rows = m.getRowDimension(); final int cols = m.getColumnDimension(); for (int i = 0; i < rows; ++i) { for (int j = 0; j < cols; ++j) { if (rows < cols) { if ((i < j) || (i > j + 1)) { Assert.assertEquals(0, m.getEntry(i, j), 1.0e-16); } } else { if ((i < j - 1) || (i > j)) { Assert.assertEquals(0, m.getEntry(i, j), 1.0e-16); } } } } } @Test public void testSingularMatrix() { BiDiagonalTransformer transformer = new BiDiagonalTransformer(MatrixUtils.createRealMatrix(new double[][] { { 1.0, 2.0, 3.0 }, { 2.0, 3.0, 4.0 }, { 3.0, 5.0, 7.0 } })); final double s3 = FastMath.sqrt(3.0); final double s14 = FastMath.sqrt(14.0); final double s1553 = FastMath.sqrt(1553.0); RealMatrix uRef = MatrixUtils.createRealMatrix(new double[][] { { -1.0 / s14, 5.0 / (s3 * s14), 1.0 / s3 }, { -2.0 / s14, -4.0 / (s3 * s14), 1.0 / s3 }, { -3.0 / s14, 1.0 / (s3 * s14), -1.0 / s3 } }); RealMatrix bRef = MatrixUtils.createRealMatrix(new double[][] { { -s14, s1553 / s14, 0.0 }, { 0.0, -87 * s3 / (s14 * s1553), -s3 * s14 / s1553 }, { 0.0, 0.0, 0.0 } }); RealMatrix vRef = MatrixUtils.createRealMatrix(new double[][] { { 1.0, 0.0, 0.0 }, { 0.0, -23 / s1553, 32 / s1553 }, { 0.0, -32 / s1553, -23 / s1553 } }); // check values against known references RealMatrix u = transformer.getU(); Assert.assertEquals(0, u.subtract(uRef).getNorm(), 1.0e-14); RealMatrix b = transformer.getB(); Assert.assertEquals(0, b.subtract(bRef).getNorm(), 1.0e-14); RealMatrix v = transformer.getV(); Assert.assertEquals(0, v.subtract(vRef).getNorm(), 1.0e-14); // check the same cached instance is returned the second time Assert.assertTrue(u == transformer.getU()); Assert.assertTrue(b == transformer.getB()); Assert.assertTrue(v == transformer.getV()); } @Test public void testMatricesValues() { BiDiagonalTransformer transformer = new BiDiagonalTransformer(MatrixUtils.createRealMatrix(testSquare)); final double s17 = FastMath.sqrt(17.0); RealMatrix uRef = MatrixUtils.createRealMatrix(new double[][] { { -8 / (5 * s17), 19 / (5 * s17) }, { -19 / (5 * s17), -8 / (5 * s17) } }); RealMatrix bRef = MatrixUtils.createRealMatrix(new double[][] { { -3 * s17 / 5, 32 * s17 / 85 }, { 0.0, -5 * s17 / 17 } }); RealMatrix vRef = MatrixUtils.createRealMatrix(new double[][] { { 1.0, 0.0 }, { 0.0, -1.0 } }); // check values against known references RealMatrix u = transformer.getU(); Assert.assertEquals(0, u.subtract(uRef).getNorm(), 1.0e-14); RealMatrix b = transformer.getB(); Assert.assertEquals(0, b.subtract(bRef).getNorm(), 1.0e-14); RealMatrix v = transformer.getV(); Assert.assertEquals(0, v.subtract(vRef).getNorm(), 1.0e-14); // check the same cached instance is returned the second time Assert.assertTrue(u == transformer.getU()); Assert.assertTrue(b == transformer.getB()); Assert.assertTrue(v == transformer.getV()); } @Test public void testUpperOrLower() { Assert.assertTrue(new BiDiagonalTransformer(MatrixUtils.createRealMatrix(testSquare)).isUpperBiDiagonal()); Assert.assertTrue(new BiDiagonalTransformer(MatrixUtils.createRealMatrix(testNonSquare)).isUpperBiDiagonal()); Assert.assertFalse(new BiDiagonalTransformer(MatrixUtils.createRealMatrix(testNonSquare).transpose()).isUpperBiDiagonal()); } } commons-math3-3.2-src/src/test/java/org/apache/commons/math3/linear/SingularValueSolverTest.java100644 1750 1750 12636 12126627674 31545 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.linear; import org.apache.commons.math3.exception.MathIllegalArgumentException; import org.junit.Assert; import org.junit.Test; public class SingularValueSolverTest { private double[][] testSquare = { { 24.0 / 25.0, 43.0 / 25.0 }, { 57.0 / 25.0, 24.0 / 25.0 } }; private static final double normTolerance = 10e-14; /** test solve dimension errors */ @Test public void testSolveDimensionErrors() { DecompositionSolver solver = new SingularValueDecomposition(MatrixUtils.createRealMatrix(testSquare)).getSolver(); RealMatrix b = MatrixUtils.createRealMatrix(new double[3][2]); try { solver.solve(b); Assert.fail("an exception should have been thrown"); } catch (MathIllegalArgumentException iae) { // expected behavior } try { solver.solve(b.getColumnVector(0)); Assert.fail("an exception should have been thrown"); } catch (MathIllegalArgumentException iae) { // expected behavior } try { solver.solve(new ArrayRealVectorTest.RealVectorTestImpl(b.getColumn(0))); Assert.fail("an exception should have been thrown"); } catch (MathIllegalArgumentException iae) { // expected behavior } } /** test least square solve */ @Test public void testLeastSquareSolve() { RealMatrix m = MatrixUtils.createRealMatrix(new double[][] { { 1.0, 0.0 }, { 0.0, 0.0 } }); DecompositionSolver solver = new SingularValueDecomposition(m).getSolver(); RealMatrix b = MatrixUtils.createRealMatrix(new double[][] { { 11, 12 }, { 21, 22 } }); RealMatrix xMatrix = solver.solve(b); Assert.assertEquals(11, xMatrix.getEntry(0, 0), 1.0e-15); Assert.assertEquals(12, xMatrix.getEntry(0, 1), 1.0e-15); Assert.assertEquals(0, xMatrix.getEntry(1, 0), 1.0e-15); Assert.assertEquals(0, xMatrix.getEntry(1, 1), 1.0e-15); RealVector xColVec = solver.solve(b.getColumnVector(0)); Assert.assertEquals(11, xColVec.getEntry(0), 1.0e-15); Assert.assertEquals(0, xColVec.getEntry(1), 1.0e-15); RealVector xColOtherVec = solver.solve(new ArrayRealVectorTest.RealVectorTestImpl(b.getColumn(0))); Assert.assertEquals(11, xColOtherVec.getEntry(0), 1.0e-15); Assert.assertEquals(0, xColOtherVec.getEntry(1), 1.0e-15); } /** test solve */ @Test public void testSolve() { DecompositionSolver solver = new SingularValueDecomposition(MatrixUtils.createRealMatrix(testSquare)).getSolver(); RealMatrix b = MatrixUtils.createRealMatrix(new double[][] { { 1, 2, 3 }, { 0, -5, 1 } }); RealMatrix xRef = MatrixUtils.createRealMatrix(new double[][] { { -8.0 / 25.0, -263.0 / 75.0, -29.0 / 75.0 }, { 19.0 / 25.0, 78.0 / 25.0, 49.0 / 25.0 } }); // using RealMatrix Assert.assertEquals(0, solver.solve(b).subtract(xRef).getNorm(), normTolerance); // using ArrayRealVector for (int i = 0; i < b.getColumnDimension(); ++i) { Assert.assertEquals(0, solver.solve(b.getColumnVector(i)).subtract(xRef.getColumnVector(i)).getNorm(), 1.0e-13); } // using RealVector with an alternate implementation for (int i = 0; i < b.getColumnDimension(); ++i) { ArrayRealVectorTest.RealVectorTestImpl v = new ArrayRealVectorTest.RealVectorTestImpl(b.getColumn(i)); Assert.assertEquals(0, solver.solve(v).subtract(xRef.getColumnVector(i)).getNorm(), 1.0e-13); } } /** test condition number */ @Test public void testConditionNumber() { SingularValueDecomposition svd = new SingularValueDecomposition(MatrixUtils.createRealMatrix(testSquare)); // replace 1.0e-15 with 1.5e-15 Assert.assertEquals(3.0, svd.getConditionNumber(), 1.5e-15); } @Test public void testMath320B() { RealMatrix rm = new Array2DRowRealMatrix(new double[][] { { 1.0, 2.0 }, { 1.0, 2.0 } }); SingularValueDecomposition svd = new SingularValueDecomposition(rm); RealMatrix recomposed = svd.getU().multiply(svd.getS()).multiply(svd.getVT()); Assert.assertEquals(0.0, recomposed.subtract(rm).getNorm(), 2.0e-15); } } commons-math3-3.2-src/src/test/java/org/apache/commons/math3/linear/LUSolverTest.java100644 1750 1750 14671 12126627674 27305 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.linear; import org.apache.commons.math3.exception.MathIllegalArgumentException; import org.junit.Test; import org.junit.Assert; public class LUSolverTest { private double[][] testData = { { 1.0, 2.0, 3.0}, { 2.0, 5.0, 3.0}, { 1.0, 0.0, 8.0} }; private double[][] luData = { { 2.0, 3.0, 3.0 }, { 0.0, 5.0, 7.0 }, { 6.0, 9.0, 8.0 } }; // singular matrices private double[][] singular = { { 2.0, 3.0 }, { 2.0, 3.0 } }; private double[][] bigSingular = { { 1.0, 2.0, 3.0, 4.0 }, { 2.0, 5.0, 3.0, 4.0 }, { 7.0, 3.0, 256.0, 1930.0 }, { 3.0, 7.0, 6.0, 8.0 } }; // 4th row = 1st + 2nd /** test threshold impact */ @Test public void testThreshold() { final RealMatrix matrix = MatrixUtils.createRealMatrix(new double[][] { { 1.0, 2.0, 3.0}, { 2.0, 5.0, 3.0}, { 4.000001, 9.0, 9.0} }); Assert.assertFalse(new LUDecomposition(matrix, 1.0e-5).getSolver().isNonSingular()); Assert.assertTrue(new LUDecomposition(matrix, 1.0e-10).getSolver().isNonSingular()); } /** test singular */ @Test public void testSingular() { DecompositionSolver solver = new LUDecomposition(MatrixUtils.createRealMatrix(testData)).getSolver(); Assert.assertTrue(solver.isNonSingular()); solver = new LUDecomposition(MatrixUtils.createRealMatrix(singular)).getSolver(); Assert.assertFalse(solver.isNonSingular()); solver = new LUDecomposition(MatrixUtils.createRealMatrix(bigSingular)).getSolver(); Assert.assertFalse(solver.isNonSingular()); } /** test solve dimension errors */ @Test public void testSolveDimensionErrors() { DecompositionSolver solver = new LUDecomposition(MatrixUtils.createRealMatrix(testData)).getSolver(); RealMatrix b = MatrixUtils.createRealMatrix(new double[2][2]); try { solver.solve(b); Assert.fail("an exception should have been thrown"); } catch (MathIllegalArgumentException iae) { // expected behavior } try { solver.solve(b.getColumnVector(0)); Assert.fail("an exception should have been thrown"); } catch (MathIllegalArgumentException iae) { // expected behavior } try { solver.solve(new ArrayRealVectorTest.RealVectorTestImpl(b.getColumn(0))); Assert.fail("an exception should have been thrown"); } catch (MathIllegalArgumentException iae) { // expected behavior } } /** test solve singularity errors */ @Test public void testSolveSingularityErrors() { DecompositionSolver solver = new LUDecomposition(MatrixUtils.createRealMatrix(singular)).getSolver(); RealMatrix b = MatrixUtils.createRealMatrix(new double[2][2]); try { solver.solve(b); Assert.fail("an exception should have been thrown"); } catch (SingularMatrixException ime) { // expected behavior } try { solver.solve(b.getColumnVector(0)); Assert.fail("an exception should have been thrown"); } catch (SingularMatrixException ime) { // expected behavior } try { solver.solve(new ArrayRealVectorTest.RealVectorTestImpl(b.getColumn(0))); Assert.fail("an exception should have been thrown"); } catch (SingularMatrixException ime) { // expected behavior } } /** test solve */ @Test public void testSolve() { DecompositionSolver solver = new LUDecomposition(MatrixUtils.createRealMatrix(testData)).getSolver(); RealMatrix b = MatrixUtils.createRealMatrix(new double[][] { { 1, 0 }, { 2, -5 }, { 3, 1 } }); RealMatrix xRef = MatrixUtils.createRealMatrix(new double[][] { { 19, -71 }, { -6, 22 }, { -2, 9 } }); // using RealMatrix Assert.assertEquals(0, solver.solve(b).subtract(xRef).getNorm(), 1.0e-13); // using ArrayRealVector for (int i = 0; i < b.getColumnDimension(); ++i) { Assert.assertEquals(0, solver.solve(b.getColumnVector(i)).subtract(xRef.getColumnVector(i)).getNorm(), 1.0e-13); } // using RealVector with an alternate implementation for (int i = 0; i < b.getColumnDimension(); ++i) { ArrayRealVectorTest.RealVectorTestImpl v = new ArrayRealVectorTest.RealVectorTestImpl(b.getColumn(i)); Assert.assertEquals(0, solver.solve(v).subtract(xRef.getColumnVector(i)).getNorm(), 1.0e-13); } } /** test determinant */ @Test public void testDeterminant() { Assert.assertEquals( -1, getDeterminant(MatrixUtils.createRealMatrix(testData)), 1.0e-15); Assert.assertEquals(-10, getDeterminant(MatrixUtils.createRealMatrix(luData)), 1.0e-14); Assert.assertEquals( 0, getDeterminant(MatrixUtils.createRealMatrix(singular)), 1.0e-17); Assert.assertEquals( 0, getDeterminant(MatrixUtils.createRealMatrix(bigSingular)), 1.0e-10); } private double getDeterminant(RealMatrix m) { return new LUDecomposition(m).getDeterminant(); } } commons-math3-3.2-src/src/test/java/org/apache/commons/math3/linear/SparseFieldVectorTest.java100644 1750 1750 30153 12126627674 31147 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.linear; import org.apache.commons.math3.fraction.Fraction; import org.apache.commons.math3.fraction.FractionConversionException; import org.apache.commons.math3.fraction.FractionField; import org.apache.commons.math3.exception.MathIllegalArgumentException; import org.junit.Assert; import org.junit.Test; /** * Test cases for the {@link SparseFieldVector} class. * * @version $Id: SparseFieldVectorTest.java 1244107 2012-02-14 16:17:55Z erans $ */ public class SparseFieldVectorTest { // protected Fraction[][] ma1 = {{new Fraction(1), new Fraction(2), new Fraction(3)}, {new Fraction(4), new Fraction(5), new Fraction(6)}, {new Fraction(7), new Fraction(8), new Fraction(9)}}; protected Fraction[] vec1 = {new Fraction(1), new Fraction(2), new Fraction(3)}; protected Fraction[] vec2 = {new Fraction(4), new Fraction(5), new Fraction(6)}; protected Fraction[] vec3 = {new Fraction(7), new Fraction(8), new Fraction(9)}; protected Fraction[] vec4 = {new Fraction(1), new Fraction(2), new Fraction(3), new Fraction(4), new Fraction(5), new Fraction(6), new Fraction(7), new Fraction(8), new Fraction(9)}; protected Fraction[] vec_null = {new Fraction(0), new Fraction(0), new Fraction(0)}; protected Fraction[] dvec1 = {new Fraction(1), new Fraction(2), new Fraction(3), new Fraction(4), new Fraction(5), new Fraction(6), new Fraction(7), new Fraction(8),new Fraction(9)}; protected Fraction[][] mat1 = {{new Fraction(1), new Fraction(2), new Fraction(3)}, {new Fraction(4), new Fraction(5), new Fraction(6)},{ new Fraction(7), new Fraction(8), new Fraction(9)}}; // tolerances protected double entryTolerance = 10E-16; protected double normTolerance = 10E-14; protected FractionField field = FractionField.getInstance(); @Test public void testMapFunctions() throws FractionConversionException { SparseFieldVector v1 = new SparseFieldVector(field,vec1); //octave = v1 .+ 2.0 FieldVector v_mapAdd = v1.mapAdd(new Fraction(2)); Fraction[] result_mapAdd = {new Fraction(3), new Fraction(4), new Fraction(5)}; Assert.assertArrayEquals("compare vectors" ,result_mapAdd,v_mapAdd.getData()); //octave = v1 .+ 2.0 FieldVector v_mapAddToSelf = v1.copy(); v_mapAddToSelf.mapAddToSelf(new Fraction(2)); Fraction[] result_mapAddToSelf = {new Fraction(3), new Fraction(4), new Fraction(5)}; Assert.assertArrayEquals("compare vectors" ,result_mapAddToSelf,v_mapAddToSelf.getData()); //octave = v1 .- 2.0 FieldVector v_mapSubtract = v1.mapSubtract(new Fraction(2)); Fraction[] result_mapSubtract = {new Fraction(-1), new Fraction(0), new Fraction(1)}; Assert.assertArrayEquals("compare vectors" ,result_mapSubtract,v_mapSubtract.getData()); //octave = v1 .- 2.0 FieldVector v_mapSubtractToSelf = v1.copy(); v_mapSubtractToSelf.mapSubtractToSelf(new Fraction(2)); Fraction[] result_mapSubtractToSelf = {new Fraction(-1), new Fraction(0), new Fraction(1)}; Assert.assertArrayEquals("compare vectors" ,result_mapSubtractToSelf,v_mapSubtractToSelf.getData()); //octave = v1 .* 2.0 FieldVector v_mapMultiply = v1.mapMultiply(new Fraction(2)); Fraction[] result_mapMultiply = {new Fraction(2), new Fraction(4), new Fraction(6)}; Assert.assertArrayEquals("compare vectors" ,result_mapMultiply,v_mapMultiply.getData()); //octave = v1 .* 2.0 FieldVector v_mapMultiplyToSelf = v1.copy(); v_mapMultiplyToSelf.mapMultiplyToSelf(new Fraction(2)); Fraction[] result_mapMultiplyToSelf = {new Fraction(2), new Fraction(4), new Fraction(6)}; Assert.assertArrayEquals("compare vectors" ,result_mapMultiplyToSelf,v_mapMultiplyToSelf.getData()); //octave = v1 ./ 2.0 FieldVector v_mapDivide = v1.mapDivide(new Fraction(2)); Fraction[] result_mapDivide = {new Fraction(.5d), new Fraction(1), new Fraction(1.5d)}; Assert.assertArrayEquals("compare vectors" ,result_mapDivide,v_mapDivide.getData()); //octave = v1 ./ 2.0 FieldVector v_mapDivideToSelf = v1.copy(); v_mapDivideToSelf.mapDivideToSelf(new Fraction(2)); Fraction[] result_mapDivideToSelf = {new Fraction(.5d), new Fraction(1), new Fraction(1.5d)}; Assert.assertArrayEquals("compare vectors" ,result_mapDivideToSelf,v_mapDivideToSelf.getData()); //octave = v1 .^-1 FieldVector v_mapInv = v1.mapInv(); Fraction[] result_mapInv = {new Fraction(1),new Fraction(0.5d),new Fraction(3.333333333333333e-01d)}; Assert.assertArrayEquals("compare vectors" ,result_mapInv,v_mapInv.getData()); //octave = v1 .^-1 FieldVector v_mapInvToSelf = v1.copy(); v_mapInvToSelf.mapInvToSelf(); Fraction[] result_mapInvToSelf = {new Fraction(1),new Fraction(0.5d),new Fraction(3.333333333333333e-01d)}; Assert.assertArrayEquals("compare vectors" ,result_mapInvToSelf,v_mapInvToSelf.getData()); } @Test public void testBasicFunctions() throws FractionConversionException { SparseFieldVector v1 = new SparseFieldVector(field,vec1); SparseFieldVector v2 = new SparseFieldVector(field,vec2); FieldVector v2_t = new ArrayFieldVectorTest.FieldVectorTestImpl(vec2); //octave = v1 + v2 FieldVector v_add = v1.add(v2); Fraction[] result_add = {new Fraction(5), new Fraction(7), new Fraction(9)}; Assert.assertArrayEquals("compare vect" ,v_add.getData(),result_add); FieldVector vt2 = new ArrayFieldVectorTest.FieldVectorTestImpl(vec2); FieldVector v_add_i = v1.add(vt2); Fraction[] result_add_i = {new Fraction(5), new Fraction(7), new Fraction(9)}; Assert.assertArrayEquals("compare vect" ,v_add_i.getData(),result_add_i); //octave = v1 - v2 SparseFieldVector v_subtract = v1.subtract(v2); Fraction[] result_subtract = {new Fraction(-3), new Fraction(-3), new Fraction(-3)}; assertClose("compare vect" ,v_subtract.getData(),result_subtract,normTolerance); FieldVector v_subtract_i = v1.subtract(vt2); Fraction[] result_subtract_i = {new Fraction(-3), new Fraction(-3), new Fraction(-3)}; assertClose("compare vect" ,v_subtract_i.getData(),result_subtract_i,normTolerance); // octave v1 .* v2 FieldVector v_ebeMultiply = v1.ebeMultiply(v2); Fraction[] result_ebeMultiply = {new Fraction(4), new Fraction(10), new Fraction(18)}; assertClose("compare vect" ,v_ebeMultiply.getData(),result_ebeMultiply,normTolerance); FieldVector v_ebeMultiply_2 = v1.ebeMultiply(v2_t); Fraction[] result_ebeMultiply_2 = {new Fraction(4), new Fraction(10), new Fraction(18)}; assertClose("compare vect" ,v_ebeMultiply_2.getData(),result_ebeMultiply_2,normTolerance); // octave v1 ./ v2 FieldVector v_ebeDivide = v1.ebeDivide(v2); Fraction[] result_ebeDivide = {new Fraction(0.25d), new Fraction(0.4d), new Fraction(0.5d)}; assertClose("compare vect" ,v_ebeDivide.getData(),result_ebeDivide,normTolerance); FieldVector v_ebeDivide_2 = v1.ebeDivide(v2_t); Fraction[] result_ebeDivide_2 = {new Fraction(0.25d), new Fraction(0.4d), new Fraction(0.5d)}; assertClose("compare vect" ,v_ebeDivide_2.getData(),result_ebeDivide_2,normTolerance); // octave dot(v1,v2) Fraction dot = v1.dotProduct(v2); Assert.assertEquals("compare val ",new Fraction(32), dot); // octave dot(v1,v2_t) Fraction dot_2 = v1.dotProduct(v2_t); Assert.assertEquals("compare val ",new Fraction(32), dot_2); FieldMatrix m_outerProduct = v1.outerProduct(v2); Assert.assertEquals("compare val ",new Fraction(4), m_outerProduct.getEntry(0,0)); FieldMatrix m_outerProduct_2 = v1.outerProduct(v2_t); Assert.assertEquals("compare val ",new Fraction(4), m_outerProduct_2.getEntry(0,0)); } @Test public void testOuterProduct() { final SparseFieldVector u = new SparseFieldVector(FractionField.getInstance(), new Fraction[] {new Fraction(1), new Fraction(2), new Fraction(-3)}); final SparseFieldVector v = new SparseFieldVector(FractionField.getInstance(), new Fraction[] {new Fraction(4), new Fraction(-2)}); final FieldMatrix uv = u.outerProduct(v); final double tol = Math.ulp(1d); Assert.assertEquals(new Fraction(4).doubleValue(), uv.getEntry(0, 0).doubleValue(), tol); Assert.assertEquals(new Fraction(-2).doubleValue(), uv.getEntry(0, 1).doubleValue(), tol); Assert.assertEquals(new Fraction(8).doubleValue(), uv.getEntry(1, 0).doubleValue(), tol); Assert.assertEquals(new Fraction(-4).doubleValue(), uv.getEntry(1, 1).doubleValue(), tol); Assert.assertEquals(new Fraction(-12).doubleValue(), uv.getEntry(2, 0).doubleValue(), tol); Assert.assertEquals(new Fraction(6).doubleValue(), uv.getEntry(2, 1).doubleValue(), tol); } @Test public void testMisc() { SparseFieldVector v1 = new SparseFieldVector(field,vec1); String out1 = v1.toString(); Assert.assertTrue("some output ", out1.length()!=0); try { v1.checkVectorDimensions(2); Assert.fail("MathIllegalArgumentException expected"); } catch (MathIllegalArgumentException ex) { // expected behavior } } @Test public void testPredicates() { SparseFieldVector v = new SparseFieldVector(field, new Fraction[] { new Fraction(0), new Fraction(1), new Fraction(2) }); v.setEntry(0, field.getZero()); Assert.assertEquals(v, new SparseFieldVector(field, new Fraction[] { new Fraction(0), new Fraction(1), new Fraction(2) })); Assert.assertNotSame(v, new SparseFieldVector(field, new Fraction[] { new Fraction(0), new Fraction(1), new Fraction(2), new Fraction(3) })); } /** verifies that two vectors are close (sup norm) */ protected void assertEquals(String msg, Fraction[] m, Fraction[] n) { if (m.length != n.length) { Assert.fail("vectors have different lengths"); } for (int i = 0; i < m.length; i++) { Assert.assertEquals(msg + " " + i + " elements differ", m[i],n[i]); } } /** verifies that two vectors are close (sup norm) */ protected void assertClose(String msg, Fraction[] m, Fraction[] n, double tolerance) { if (m.length != n.length) { Assert.fail("vectors have different lengths"); } for (int i = 0; i < m.length; i++) { Assert.assertEquals(msg + " " + i + " elements differ", m[i].doubleValue(),n[i].doubleValue(), tolerance); } } } commons-math3-3.2-src/src/test/java/org/apache/commons/math3/linear/EigenSolverTest.java100644 1750 1750 12204 12126627674 30002 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.linear; import java.util.Random; import org.apache.commons.math3.exception.MathIllegalArgumentException; import org.junit.Test; import org.junit.Assert; public class EigenSolverTest { /** test non invertible matrix */ @Test public void testNonInvertible() { Random r = new Random(9994100315209l); RealMatrix m = EigenDecompositionTest.createTestMatrix(r, new double[] { 1.0, 0.0, -1.0, -2.0, -3.0 }); DecompositionSolver es = new EigenDecomposition(m).getSolver(); Assert.assertFalse(es.isNonSingular()); try { es.getInverse(); Assert.fail("an exception should have been thrown"); } catch (SingularMatrixException ime) { // expected behavior } } /** test invertible matrix */ @Test public void testInvertible() { Random r = new Random(9994100315209l); RealMatrix m = EigenDecompositionTest.createTestMatrix(r, new double[] { 1.0, 0.5, -1.0, -2.0, -3.0 }); DecompositionSolver es = new EigenDecomposition(m).getSolver(); Assert.assertTrue(es.isNonSingular()); RealMatrix inverse = es.getInverse(); RealMatrix error = m.multiply(inverse).subtract(MatrixUtils.createRealIdentityMatrix(m.getRowDimension())); Assert.assertEquals(0, error.getNorm(), 4.0e-15); } /** test solve dimension errors */ @Test public void testSolveDimensionErrors() { final double[] refValues = new double[] { 2.003, 2.002, 2.001, 1.001, 1.000, 0.001 }; final RealMatrix matrix = EigenDecompositionTest.createTestMatrix(new Random(35992629946426l), refValues); DecompositionSolver es = new EigenDecomposition(matrix).getSolver(); RealMatrix b = MatrixUtils.createRealMatrix(new double[2][2]); try { es.solve(b); Assert.fail("an exception should have been thrown"); } catch (MathIllegalArgumentException iae) { // expected behavior } try { es.solve(b.getColumnVector(0)); Assert.fail("an exception should have been thrown"); } catch (MathIllegalArgumentException iae) { // expected behavior } try { es.solve(new ArrayRealVectorTest.RealVectorTestImpl(b.getColumn(0))); Assert.fail("an exception should have been thrown"); } catch (MathIllegalArgumentException iae) { // expected behavior } } /** test solve */ @Test public void testSolve() { RealMatrix m = MatrixUtils.createRealMatrix(new double[][] { { 91, 5, 29, 32, 40, 14 }, { 5, 34, -1, 0, 2, -1 }, { 29, -1, 12, 9, 21, 8 }, { 32, 0, 9, 14, 9, 0 }, { 40, 2, 21, 9, 51, 19 }, { 14, -1, 8, 0, 19, 14 } }); DecompositionSolver es = new EigenDecomposition(m).getSolver(); RealMatrix b = MatrixUtils.createRealMatrix(new double[][] { { 1561, 269, 188 }, { 69, -21, 70 }, { 739, 108, 63 }, { 324, 86, 59 }, { 1624, 194, 107 }, { 796, 69, 36 } }); RealMatrix xRef = MatrixUtils.createRealMatrix(new double[][] { { 1, 2, 1 }, { 2, -1, 2 }, { 4, 2, 3 }, { 8, -1, 0 }, { 16, 2, 0 }, { 32, -1, 0 } }); // using RealMatrix RealMatrix solution=es.solve(b); Assert.assertEquals(0, solution.subtract(xRef).getNorm(), 2.5e-12); // using RealVector for (int i = 0; i < b.getColumnDimension(); ++i) { Assert.assertEquals(0, es.solve(b.getColumnVector(i)).subtract(xRef.getColumnVector(i)).getNorm(), 2.0e-11); } // using RealVector with an alternate implementation for (int i = 0; i < b.getColumnDimension(); ++i) { ArrayRealVectorTest.RealVectorTestImpl v = new ArrayRealVectorTest.RealVectorTestImpl(b.getColumn(i)); Assert.assertEquals(0, es.solve(v).subtract(xRef.getColumnVector(i)).getNorm(), 2.0e-11); } } } ././@LongLink100644 0 0 153 12126630646 10260 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/linear/UnmodifiableOpenMapRealVectorTest.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/linear/UnmodifiableOpenMapRealVectorTes100644 1750 1750 3175 12126627674 32310 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.linear; /** * This is an implementation of {@link UnmodifiableRealVectorAbstractTest} for * unmodifiable views of {@link OpenMapRealVector}. * * @version $Id$ */ public class UnmodifiableOpenMapRealVectorTest extends UnmodifiableRealVectorAbstractTest { /** To ensure sufficient sparsity. */ public static final double PROBABILITY_OF_ZERO = 0.5; /** * Returns a random vector of type {@link ArrayRealVector}. * * @return a new random {@link ArrayRealVector}. */ @Override public RealVector createVector() { OpenMapRealVector v = new OpenMapRealVector(DIM, EPS); for (int i = 0; i < DIM; i++) { if (RANDOM.nextDouble() > PROBABILITY_OF_ZERO) { v.setEntry(i, RANDOM.nextDouble()); } } return v; } } commons-math3-3.2-src/src/test/java/org/apache/commons/math3/linear/SchurTransformerTest.java100644 1750 1750 17734 12126627674 31104 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.linear; import java.util.Random; import org.apache.commons.math3.distribution.NormalDistribution; import org.junit.Test; import org.junit.Assert; public class SchurTransformerTest { private double[][] testSquare5 = { { 5, 4, 3, 2, 1 }, { 1, 4, 0, 3, 3 }, { 2, 0, 3, 0, 0 }, { 3, 2, 1, 2, 5 }, { 4, 2, 1, 4, 1 } }; private double[][] testSquare3 = { { 2, -1, 1 }, { -1, 2, 1 }, { 1, -1, 2 } }; // from http://eigen.tuxfamily.org/dox/classEigen_1_1RealSchur.html private double[][] testRandom = { { 0.680, -0.3300, -0.2700, -0.717, -0.687, 0.0259 }, { -0.211, 0.5360, 0.0268, 0.214, -0.198, 0.6780 }, { 0.566, -0.4440, 0.9040, -0.967, -0.740, 0.2250 }, { 0.597, 0.1080, 0.8320, -0.514, -0.782, -0.4080 }, { 0.823, -0.0452, 0.2710, -0.726, 0.998, 0.2750 }, { -0.605, 0.2580, 0.4350, 0.608, -0.563, 0.0486 } }; @Test public void testNonSquare() { try { new SchurTransformer(MatrixUtils.createRealMatrix(new double[3][2])); Assert.fail("an exception should have been thrown"); } catch (NonSquareMatrixException ime) { // expected behavior } } @Test public void testAEqualPTPt() { checkAEqualPTPt(MatrixUtils.createRealMatrix(testSquare5)); checkAEqualPTPt(MatrixUtils.createRealMatrix(testSquare3)); checkAEqualPTPt(MatrixUtils.createRealMatrix(testRandom)); } @Test public void testPOrthogonal() { checkOrthogonal(new SchurTransformer(MatrixUtils.createRealMatrix(testSquare5)).getP()); checkOrthogonal(new SchurTransformer(MatrixUtils.createRealMatrix(testSquare3)).getP()); checkOrthogonal(new SchurTransformer(MatrixUtils.createRealMatrix(testRandom)).getP()); } @Test public void testPTOrthogonal() { checkOrthogonal(new SchurTransformer(MatrixUtils.createRealMatrix(testSquare5)).getPT()); checkOrthogonal(new SchurTransformer(MatrixUtils.createRealMatrix(testSquare3)).getPT()); checkOrthogonal(new SchurTransformer(MatrixUtils.createRealMatrix(testRandom)).getPT()); } @Test public void testSchurForm() { checkSchurForm(new SchurTransformer(MatrixUtils.createRealMatrix(testSquare5)).getT()); checkSchurForm(new SchurTransformer(MatrixUtils.createRealMatrix(testSquare3)).getT()); checkSchurForm(new SchurTransformer(MatrixUtils.createRealMatrix(testRandom)).getT()); } @Test public void testRandomData() { for (int run = 0; run < 100; run++) { Random r = new Random(System.currentTimeMillis()); // matrix size int size = r.nextInt(20) + 4; double[][] data = new double[size][size]; for (int i = 0; i < size; i++) { for (int j = 0; j < size; j++) { data[i][j] = r.nextInt(100); } } RealMatrix m = MatrixUtils.createRealMatrix(data); RealMatrix s = checkAEqualPTPt(m); checkSchurForm(s); } } @Test public void testRandomDataNormalDistribution() { for (int run = 0; run < 100; run++) { Random r = new Random(System.currentTimeMillis()); NormalDistribution dist = new NormalDistribution(0.0, r.nextDouble() * 5); // matrix size int size = r.nextInt(20) + 4; double[][] data = new double[size][size]; for (int i = 0; i < size; i++) { for (int j = 0; j < size; j++) { data[i][j] = dist.sample(); } } RealMatrix m = MatrixUtils.createRealMatrix(data); RealMatrix s = checkAEqualPTPt(m); checkSchurForm(s); } } @Test public void testMath848() { double[][] data = { { 0.1849449280, -0.0646971046, 0.0774755812, -0.0969651755, -0.0692648806, 0.3282344352, -0.0177423074, 0.2063136340}, {-0.0742700134, -0.0289063030, -0.0017269460, -0.0375550146, -0.0487737922, -0.2616837868, -0.0821201295, -0.2530000167}, { 0.2549910127, 0.0995733692, -0.0009718388, 0.0149282808, 0.1791878897, -0.0823182816, 0.0582629256, 0.3219545182}, {-0.0694747557, -0.1880649148, -0.2740630911, 0.0720096468, -0.1800836914, -0.3518996425, 0.2486747833, 0.6257938167}, { 0.0536360918, -0.1339297778, 0.2241579764, -0.0195327484, -0.0054103808, 0.0347564518, 0.5120802482, -0.0329902864}, {-0.5933332356, -0.2488721082, 0.2357173629, 0.0177285473, 0.0856630593, -0.3567126300, -0.1600668126, -0.1010899621}, {-0.0514349819, -0.0854319435, 0.1125050061, 0.0063453560, -0.2250000688, -0.2209343090, 0.1964623477, -0.1512329924}, { 0.0197395947, -0.1997170581, -0.1425959019, -0.2749477910, -0.0969467073, 0.0603688520, -0.2826905192, 0.1794315473}}; RealMatrix m = MatrixUtils.createRealMatrix(data); RealMatrix s = checkAEqualPTPt(m); checkSchurForm(s); } /////////////////////////////////////////////////////////////////////////// // Test helpers /////////////////////////////////////////////////////////////////////////// private RealMatrix checkAEqualPTPt(RealMatrix matrix) { SchurTransformer transformer = new SchurTransformer(matrix); RealMatrix p = transformer.getP(); RealMatrix t = transformer.getT(); RealMatrix pT = transformer.getPT(); RealMatrix result = p.multiply(t).multiply(pT); double norm = result.subtract(matrix).getNorm(); Assert.assertEquals(0, norm, 1.0e-9); return t; } private void checkOrthogonal(RealMatrix m) { RealMatrix mTm = m.transpose().multiply(m); RealMatrix id = MatrixUtils.createRealIdentityMatrix(mTm.getRowDimension()); Assert.assertEquals(0, mTm.subtract(id).getNorm(), 1.0e-14); } private void checkSchurForm(final RealMatrix m) { final int rows = m.getRowDimension(); final int cols = m.getColumnDimension(); for (int i = 0; i < rows; ++i) { for (int j = 0; j < cols; ++j) { if (i > j + 1) { Assert.assertEquals(0, m.getEntry(i, j), 1.0e-16); } } } } @SuppressWarnings("unused") private void checkMatricesValues(double[][] matrix, double[][] pRef, double[][] hRef) { SchurTransformer transformer = new SchurTransformer(MatrixUtils.createRealMatrix(matrix)); // check values against known references RealMatrix p = transformer.getP(); Assert.assertEquals(0, p.subtract(MatrixUtils.createRealMatrix(pRef)).getNorm(), 1.0e-14); RealMatrix t = transformer.getT(); Assert.assertEquals(0, t.subtract(MatrixUtils.createRealMatrix(hRef)).getNorm(), 1.0e-14); // check the same cached instance is returned the second time Assert.assertTrue(p == transformer.getP()); Assert.assertTrue(t == transformer.getT()); } } ././@LongLink100644 0 0 154 12126630646 10261 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/linear/UnmodifiableRealVectorAbstractTest.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/linear/UnmodifiableRealVectorAbstractTe100644 1750 1750 40104 12126627674 32342 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with this * work for additional information regarding copyright ownership. The ASF * licenses this file to You 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. */ package org.apache.commons.math3.linear; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.util.Set; import java.util.HashSet; import java.util.Iterator; import java.util.Random; import org.apache.commons.math3.analysis.UnivariateFunction; import org.apache.commons.math3.analysis.function.Sin; import org.apache.commons.math3.exception.MathUnsupportedOperationException; import org.apache.commons.math3.linear.RealVector.Entry; import org.junit.Assert; import org.junit.Test; /** * This is an abstract test of the {@link * RealVector#unmodifiableRealVector(RealVector) unmodifiable vector} * implementation. These unmodifiable vectors decorate a (modifiable) * {@link RealVector}; therefore, a new implementation of this abstract * test should be considered for each implementation of * {@link RealVector}. * * @version $Id$ * */ public abstract class UnmodifiableRealVectorAbstractTest { /** The dimension of the randomly generated vectors. */ protected static final int DIM = 100; /** Absolute tolerance. */ protected static final double EPS = 10 * Math.ulp(1d); /** * The list of methods which are excluded from the general test * {@link #testAllButExcluded()}. */ protected static final Set EXCLUDE = new HashSet(); /** The random number generator (always initialized with the same seed. */ protected static final Random RANDOM; static { EXCLUDE.add("getEntry"); EXCLUDE.add("setEntry"); EXCLUDE.add("addToEntry"); EXCLUDE.add("getSubVector"); EXCLUDE.add("setSubVector"); EXCLUDE.add("iterator"); EXCLUDE.add("sparseIterator"); EXCLUDE.add("walkInDefaultOrder"); EXCLUDE.add("walkInOptimizedOrder"); EXCLUDE.add("ebeDivide"); EXCLUDE.add("ebeMultiply"); // Excluded because they are inherited from "Object". for (Method m : Object.class.getMethods()) { EXCLUDE.add(m.getName()); } RANDOM = new Random(20110813); } /** * Returns {@code true} if the specified {@code double} are equal (within a * given tolerance). * * @param x First {@code double}. * @param y Second {@code double}. * @return {@code true} if {@code x} and {@code y} are equal. */ public static boolean equals(final double x, final double y) { if (x == y) { return true; } else if (Math.abs(x) <= EPS) { return Math.abs(y) <= EPS; } else if (Math.abs(y) <= EPS) { return Math.abs(x) <= EPS; } else { return Math.abs(x - y) <= EPS * Math.min(Math.abs(x), Math.abs(y)); } } /** * Returns {@code true} if the specified {@code double} arrays are equal * (within a given tolerance). * * @param x First array. * @param y Second array. * @return {@code true} if {@code x} and {@code y} are equal. */ public static boolean equals(final double[] x, final double[] y) { if (x.length != y.length) { return false; } final int n = x.length; for (int i = 0; i < n; i++) { if (!equals(x[i], y[i])) { return false; } } return true; } /** * Returns {@code true} if the specified {@code RealVector} are equal * (within a given tolerance). * * @param x First vector. * @param y Second vector. * @return {@code true} if {@code x} and {@code y} are equal. */ public static boolean equals(final RealVector x, final RealVector y) { if (x.getDimension() != y.getDimension()) { return false; } final int n = x.getDimension(); for (int i = 0; i < n; i++) { if (!equals(x.getEntry(i), y.getEntry(i))) { return false; } } return true; } /** * Returns {@code true} if the specified {@code RealVector} is equal to the * specified {@code double} array (within a given tolerance). * * @param x Vector. * @param y Array. * @return {@code true} if {@code x} and {@code y} are equal. */ public static boolean equals(final RealVector x, final double[] y) { if (x.getDimension() != y.length) { return false; } final int n = x.getDimension(); for (int i = 0; i < n; i++) { if (!equals(x.getEntry(i), y[i])) { return false; } } return true; } /** * Returns {@code true} if the specified {@code RealMatrix} are equal * (within a given tolerance). * * @param x First matrix. * @param y Second matrix. * @return {@code true} if {@code x} and {@code y} are equal. */ public static boolean equals(final RealMatrix x, final RealMatrix y) { if (x.getRowDimension() != y.getRowDimension()) { return false; } if (x.getColumnDimension() != y.getColumnDimension()) { return false; } final int rows = x.getRowDimension(); final int cols = x.getColumnDimension(); for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { if (!equals(x.getEntry(i, j), y.getEntry(i, j))) { return false; } } } return true; } /** * Returns {@code true} if the specified {@code Object} are equal. * * @param x First object. * @param y Second object. * @return {@code true} if {@code x} and {@code y} are equal. * @throws IllegalArgumentException if {@code x} and {@code y} could * not be compared. */ public static boolean equals(final Object x, final Object y) { if (x instanceof Boolean) { if (y instanceof Boolean) { return ((Boolean) x).booleanValue() == ((Boolean) y) .booleanValue(); } else { return false; } } if (x instanceof Integer) { if (y instanceof Integer) { return ((Integer) x).intValue() == ((Integer) y).intValue(); } else { return false; } } else if (x instanceof Double) { if (y instanceof Double) { return equals(((Double) x).doubleValue(), ((Double) y).doubleValue()); } else { return false; } } else if (x instanceof double[]) { if (y instanceof double[]) { return equals((double[]) x, (double[]) y); } else if (y instanceof RealVector) { return equals((RealVector) y, (double[]) x); } else { return false; } } else if (x instanceof RealVector) { if (y instanceof double[]) { return equals((RealVector) x, (double[]) y); } else if (y instanceof RealVector) { return equals((RealVector) x, (RealVector) y); } else { return false; } } else if (x instanceof RealMatrix) { if (y instanceof RealMatrix) { return equals((RealMatrix) x, (RealMatrix) y); } else { return false; } } else { throw new IllegalArgumentException("could not compare " + x + ", " + y); } } /** * Creates a new random vector of a specified type. This vector is then to * be wrapped in an unmodifiable vector. * * @return a new random vector. */ public abstract RealVector createVector(); /** * Creates a new random object of the specified type. * * @param c Class of the object to be created. * @return a new random object. * @throws IllegalArgumentException if the specified class is not * recognized by this method. */ public Object createParameter(final Class c) { if (c == Integer.TYPE) { return Integer.valueOf(RANDOM.nextInt()); } else if (c == Double.TYPE) { return Double.valueOf(RANDOM.nextDouble()); } else if (c == double[].class) { final double[] v = new double[DIM]; for (int i = 0; i < DIM; i++) { v[i] = RANDOM.nextDouble(); } return v; } else if (c.isAssignableFrom(RealVector.class)) { return createVector(); } else if (c.isAssignableFrom(UnivariateFunction.class)) { return new Sin(); } else { throw new IllegalArgumentException("could not create " + c); } } /** * This is the general test of most methods in the * {@link RealVector#unmodifiableRealVector(RealVector) unmodifiable vector}. * It works as follows. * First, an unmodifiable view of a copy of the specified random vector * {@code u} is created: this defines {@code v}. Then the same * method {@code m} is invoked on {@code u} and {@code v}, with randomly * generated parameters {@code args}. * If it turns out that {@code u} has changed after the call of method * {@code m}, then this test checks that the call of this method on * {@code v} resulted in a {@link MathUnsupportedOperationException}. If * {@code u} was not modified, then this test checks that the results * returned by the call of method {@code m} on {@code u} and {@code v} * returned the same result. * * @param m Method to be tested. * @param u Random vector from which the unmodifiable view is to be *constructed. * @param args Arguments to be passed to method {@code m}. */ private void callMethod(final Method m, final RealVector u, final Object... args) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException { final RealVector uu = u.copy(); final RealVector v = RealVector.unmodifiableRealVector(u.copy()); Object exp = m.invoke(u, args); if (equals(uu, u)) { Object act = m.invoke(v, args); Assert.assertTrue(m.toGenericString() + ", unmodifiable vector has changed", equals(uu, v)); Assert.assertTrue(m.toGenericString() + ", wrong result", equals(exp, act)); } else { boolean flag = false; try { m.invoke(v, args); } catch (InvocationTargetException e) { if (e.getCause() instanceof MathUnsupportedOperationException) { flag = true; } } Assert.assertTrue(m.toGenericString()+", exception should have been thrown", flag); } } /** * This test calls {@link #callMethod(Method, RealVector, Object...)} on * every method defined in interface {@link RealVector}. It generates the * appropriate random arguments. Some methods are manually excluded (see * {@link #EXCLUDE}), they must be handled by separate tests. */ @Test public void testAllButExcluded() throws IllegalAccessException, IllegalArgumentException, InvocationTargetException { Method[] method = RealVector.class.getMethods(); for (int i = 0; i < method.length; i++) { Method m = method[i]; if (!EXCLUDE.contains(m.getName())) { RealVector u = (RealVector) createParameter(RealVector.class); Class[] paramType = m.getParameterTypes(); Object[] param = new Object[paramType.length]; for (int j = 0; j < paramType.length; j++) { param[j] = createParameter(paramType[j]); } callMethod(m, u, param); } } } @Test public void testGetEntry() { RealVector u = createVector(); RealVector v = RealVector.unmodifiableRealVector(u); for (int i = 0; i < DIM; i++) { Assert.assertTrue(equals(u.getEntry(i), v.getEntry(i))); } } @Test(expected = MathUnsupportedOperationException.class) public void testSetEntry() { RealVector u = createVector(); RealVector v = RealVector.unmodifiableRealVector(u); for (int i = 0; i < DIM; i++) { v.setEntry(i, 0d); } } @Test(expected = MathUnsupportedOperationException.class) public void testAddToEntry() { RealVector u = createVector(); RealVector v = RealVector.unmodifiableRealVector(u); for (int i = 0; i < DIM; i++) { v.addToEntry(i, 0d); } } @Test public void testGetSubVector() { RealVector u = createVector(); RealVector v = RealVector.unmodifiableRealVector(u); for (int i = 0; i < DIM; i++) { for (int n = 1; n < DIM - i; n++) { RealVector exp = u.getSubVector(i, n); RealVector act = v.getSubVector(i, n); Assert.assertTrue(equals(exp, act)); } } } @Test(expected = MathUnsupportedOperationException.class) public void testSetSubVector() { RealVector u = createVector(); RealVector v = RealVector.unmodifiableRealVector(u); v.setSubVector(0, new ArrayRealVector()); } @Test public void testIterator() { RealVector u = createVector(); Iterator i = u.iterator(); RealVector v = RealVector.unmodifiableRealVector(u.copy()); Iterator j = v.iterator(); boolean flag; while (i.hasNext()) { Assert.assertTrue(j.hasNext()); Entry exp = i.next(); Entry act = j.next(); Assert.assertTrue(equals(exp.getIndex(), act.getIndex())); Assert.assertTrue(equals(exp.getValue(), act.getValue())); exp.setIndex(RANDOM.nextInt(DIM)); act.setIndex(RANDOM.nextInt(DIM)); flag = false; try { act.setValue(RANDOM.nextDouble()); } catch (MathUnsupportedOperationException e) { flag = true; } Assert.assertTrue("exception should have been thrown", flag); } Assert.assertFalse(j.hasNext()); } @Test public void testSparseIterator() { RealVector u = createVector(); Iterator i = u.sparseIterator(); RealVector v = RealVector.unmodifiableRealVector(u.copy()); Iterator j = v.sparseIterator(); boolean flag; while (i.hasNext()) { Assert.assertTrue(j.hasNext()); Entry exp = i.next(); Entry act = j.next(); Assert.assertTrue(equals(exp.getIndex(), act.getIndex())); Assert.assertTrue(equals(exp.getValue(), act.getValue())); exp.setIndex(RANDOM.nextInt(DIM)); act.setIndex(RANDOM.nextInt(DIM)); flag = false; try { act.setValue(RANDOM.nextDouble()); } catch (MathUnsupportedOperationException e) { flag = true; } Assert.assertTrue("exception should have been thrown", flag); } Assert.assertFalse(j.hasNext()); } } commons-math3-3.2-src/src/test/java/org/apache/commons/math3/linear/ArrayRealVectorTest.java100644 1750 1750 17211 12126627674 30630 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.linear; import org.apache.commons.math3.exception.MathIllegalArgumentException; import org.junit.Assert; import org.junit.Test; /** * Test cases for the {@link ArrayRealVector} class. * * @version $Id: ArrayRealVectorTest.java 1358366 2012-07-06 19:31:00Z celestin $ */ public class ArrayRealVectorTest extends RealVectorAbstractTest { @Override public RealVector create(final double[] data) { return new ArrayRealVector(data, true); } @Test public void testConstructors() { final double[] vec1 = {1d, 2d, 3d}; final double[] vec3 = {7d, 8d, 9d}; final double[] vec4 = {1d, 2d, 3d, 4d, 5d, 6d, 7d, 8d, 9d}; final Double[] dvec1 = {1d, 2d, 3d, 4d, 5d, 6d, 7d, 8d, 9d}; ArrayRealVector v0 = new ArrayRealVector(); Assert.assertEquals("testData len", 0, v0.getDimension()); ArrayRealVector v1 = new ArrayRealVector(7); Assert.assertEquals("testData len", 7, v1.getDimension()); Assert.assertEquals("testData is 0.0 ", 0.0, v1.getEntry(6), 0); ArrayRealVector v2 = new ArrayRealVector(5, 1.23); Assert.assertEquals("testData len", 5, v2.getDimension()); Assert.assertEquals("testData is 1.23 ", 1.23, v2.getEntry(4), 0); ArrayRealVector v3 = new ArrayRealVector(vec1); Assert.assertEquals("testData len", 3, v3.getDimension()); Assert.assertEquals("testData is 2.0 ", 2.0, v3.getEntry(1), 0); ArrayRealVector v3_bis = new ArrayRealVector(vec1, true); Assert.assertEquals("testData len", 3, v3_bis.getDimension()); Assert.assertEquals("testData is 2.0 ", 2.0, v3_bis.getEntry(1), 0); Assert.assertNotSame(v3_bis.getDataRef(), vec1); Assert.assertNotSame(v3_bis.toArray(), vec1); ArrayRealVector v3_ter = new ArrayRealVector(vec1, false); Assert.assertEquals("testData len", 3, v3_ter.getDimension()); Assert.assertEquals("testData is 2.0 ", 2.0, v3_ter.getEntry(1), 0); Assert.assertSame(v3_ter.getDataRef(), vec1); Assert.assertNotSame(v3_ter.toArray(), vec1); ArrayRealVector v4 = new ArrayRealVector(vec4, 3, 2); Assert.assertEquals("testData len", 2, v4.getDimension()); Assert.assertEquals("testData is 4.0 ", 4.0, v4.getEntry(0), 0); try { new ArrayRealVector(vec4, 8, 3); Assert.fail("MathIllegalArgumentException expected"); } catch (MathIllegalArgumentException ex) { // expected behavior } RealVector v5_i = new ArrayRealVector(dvec1); Assert.assertEquals("testData len", 9, v5_i.getDimension()); Assert.assertEquals("testData is 9.0 ", 9.0, v5_i.getEntry(8), 0); ArrayRealVector v5 = new ArrayRealVector(dvec1); Assert.assertEquals("testData len", 9, v5.getDimension()); Assert.assertEquals("testData is 9.0 ", 9.0, v5.getEntry(8), 0); ArrayRealVector v6 = new ArrayRealVector(dvec1, 3, 2); Assert.assertEquals("testData len", 2, v6.getDimension()); Assert.assertEquals("testData is 4.0 ", 4.0, v6.getEntry(0), 0); try { new ArrayRealVector(dvec1, 8, 3); Assert.fail("MathIllegalArgumentException expected"); } catch (MathIllegalArgumentException ex) { // expected behavior } ArrayRealVector v7 = new ArrayRealVector(v1); Assert.assertEquals("testData len", 7, v7.getDimension()); Assert.assertEquals("testData is 0.0 ", 0.0, v7.getEntry(6), 0); RealVectorTestImpl v7_i = new RealVectorTestImpl(vec1); ArrayRealVector v7_2 = new ArrayRealVector(v7_i); Assert.assertEquals("testData len", 3, v7_2.getDimension()); Assert.assertEquals("testData is 0.0 ", 2.0d, v7_2.getEntry(1), 0); ArrayRealVector v8 = new ArrayRealVector(v1, true); Assert.assertEquals("testData len", 7, v8.getDimension()); Assert.assertEquals("testData is 0.0 ", 0.0, v8.getEntry(6), 0); Assert.assertNotSame("testData not same object ", v1.getDataRef(), v8.getDataRef()); ArrayRealVector v8_2 = new ArrayRealVector(v1, false); Assert.assertEquals("testData len", 7, v8_2.getDimension()); Assert.assertEquals("testData is 0.0 ", 0.0, v8_2.getEntry(6), 0); Assert.assertEquals("testData same object ", v1.getDataRef(), v8_2.getDataRef()); ArrayRealVector v9 = new ArrayRealVector(v1, v3); Assert.assertEquals("testData len", 10, v9.getDimension()); Assert.assertEquals("testData is 1.0 ", 1.0, v9.getEntry(7), 0); ArrayRealVector v10 = new ArrayRealVector(v2, new RealVectorTestImpl(vec3)); Assert.assertEquals("testData len", 8, v10.getDimension()); Assert.assertEquals("testData is 1.23 ", 1.23, v10.getEntry(4), 0); Assert.assertEquals("testData is 7.0 ", 7.0, v10.getEntry(5), 0); ArrayRealVector v11 = new ArrayRealVector(new RealVectorTestImpl(vec3), v2); Assert.assertEquals("testData len", 8, v11.getDimension()); Assert.assertEquals("testData is 9.0 ", 9.0, v11.getEntry(2), 0); Assert.assertEquals("testData is 1.23 ", 1.23, v11.getEntry(3), 0); ArrayRealVector v12 = new ArrayRealVector(v2, vec3); Assert.assertEquals("testData len", 8, v12.getDimension()); Assert.assertEquals("testData is 1.23 ", 1.23, v12.getEntry(4), 0); Assert.assertEquals("testData is 7.0 ", 7.0, v12.getEntry(5), 0); ArrayRealVector v13 = new ArrayRealVector(vec3, v2); Assert.assertEquals("testData len", 8, v13.getDimension()); Assert.assertEquals("testData is 9.0 ", 9.0, v13.getEntry(2), 0); Assert.assertEquals("testData is 1.23 ", 1.23, v13.getEntry(3), 0); ArrayRealVector v14 = new ArrayRealVector(vec3, vec4); Assert.assertEquals("testData len", 12, v14.getDimension()); Assert.assertEquals("testData is 9.0 ", 9.0, v14.getEntry(2), 0); Assert.assertEquals("testData is 1.0 ", 1.0, v14.getEntry(3), 0); } @Test public void testGetDataRef() { final double[] data = {1d, 2d, 3d, 4d}; final ArrayRealVector v = new ArrayRealVector(data); v.getDataRef()[0] = 0d; Assert.assertEquals("", 0d, v.getEntry(0), 0); } @Test public void testPredicates() { Assert.assertEquals(create(new double[] { Double.NaN, 1, 2 }).hashCode(), create(new double[] { 0, Double.NaN, 2 }).hashCode()); Assert.assertTrue(create(new double[] { Double.NaN, 1, 2 }).hashCode() != create(new double[] { 0, 1, 2 }).hashCode()); } @Test public void testZeroVectors() { Assert.assertEquals(0, new ArrayRealVector(new double[0]).getDimension()); Assert.assertEquals(0, new ArrayRealVector(new double[0], true).getDimension()); Assert.assertEquals(0, new ArrayRealVector(new double[0], false).getDimension()); } } commons-math3-3.2-src/src/test/java/org/apache/commons/math3/linear/CholeskySolverTest.java100644 1750 1750 7741 12126627674 30526 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.linear; import org.apache.commons.math3.exception.MathIllegalArgumentException; import org.junit.Assert; import org.junit.Test; public class CholeskySolverTest { private double[][] testData = new double[][] { { 1, 2, 4, 7, 11 }, { 2, 13, 23, 38, 58 }, { 4, 23, 77, 122, 182 }, { 7, 38, 122, 294, 430 }, { 11, 58, 182, 430, 855 } }; /** test solve dimension errors */ @Test public void testSolveDimensionErrors() { DecompositionSolver solver = new CholeskyDecomposition(MatrixUtils.createRealMatrix(testData)).getSolver(); RealMatrix b = MatrixUtils.createRealMatrix(new double[2][2]); try { solver.solve(b); Assert.fail("an exception should have been thrown"); } catch (MathIllegalArgumentException iae) { // expected behavior } try { solver.solve(b.getColumnVector(0)); Assert.fail("an exception should have been thrown"); } catch (MathIllegalArgumentException iae) { // expected behavior } try { solver.solve(new ArrayRealVectorTest.RealVectorTestImpl(b.getColumn(0))); Assert.fail("an exception should have been thrown"); } catch (MathIllegalArgumentException iae) { // expected behavior } } /** test solve */ @Test public void testSolve() { DecompositionSolver solver = new CholeskyDecomposition(MatrixUtils.createRealMatrix(testData)).getSolver(); RealMatrix b = MatrixUtils.createRealMatrix(new double[][] { { 78, -13, 1 }, { 414, -62, -1 }, { 1312, -202, -37 }, { 2989, -542, 145 }, { 5510, -1465, 201 } }); RealMatrix xRef = MatrixUtils.createRealMatrix(new double[][] { { 1, 0, 1 }, { 0, 1, 1 }, { 2, 1, -4 }, { 2, 2, 2 }, { 5, -3, 0 } }); // using RealMatrix Assert.assertEquals(0, solver.solve(b).subtract(xRef).getNorm(), 1.0e-13); // using ArrayRealVector for (int i = 0; i < b.getColumnDimension(); ++i) { Assert.assertEquals(0, solver.solve(b.getColumnVector(i)).subtract(xRef.getColumnVector(i)).getNorm(), 1.0e-13); } // using RealVector with an alternate implementation for (int i = 0; i < b.getColumnDimension(); ++i) { ArrayRealVectorTest.RealVectorTestImpl v = new ArrayRealVectorTest.RealVectorTestImpl(b.getColumn(i)); Assert.assertEquals(0, solver.solve(v).subtract(xRef.getColumnVector(i)).getNorm(), 1.0e-13); } } /** test determinant */ @Test public void testDeterminant() { Assert.assertEquals(7290000.0, getDeterminant(MatrixUtils.createRealMatrix(testData)), 1.0e-15); } private double getDeterminant(RealMatrix m) { return new CholeskyDecomposition(m).getDeterminant(); } } ././@LongLink100644 0 0 146 12126630646 10262 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/linear/RealMatrixFormatAbstractTest.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/linear/RealMatrixFormatAbstractTest.jav100644 1750 1750 34151 12126627674 32331 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.linear; import java.text.NumberFormat; import java.text.ParsePosition; import java.util.Locale; import org.junit.Ignore; import org.junit.Test; import org.junit.Assert; import org.apache.commons.math3.exception.MathParseException; public abstract class RealMatrixFormatAbstractTest { RealMatrixFormat realMatrixFormat = null; RealMatrixFormat realMatrixFormatOctave = null; protected abstract Locale getLocale(); protected abstract char getDecimalCharacter(); public RealMatrixFormatAbstractTest() { realMatrixFormat = RealMatrixFormat.getInstance(getLocale()); final NumberFormat nf = NumberFormat.getInstance(getLocale()); nf.setMaximumFractionDigits(2); realMatrixFormatOctave = new RealMatrixFormat("[", "]", "", "", "; ", ", ", nf); } @Test public void testSimpleNoDecimals() { RealMatrix m = MatrixUtils.createRealMatrix(new double[][] {{1, 1, 1}, {1, 1, 1}}); String expected = "{{1,1,1},{1,1,1}}"; String actual = realMatrixFormat.format(m); Assert.assertEquals(expected, actual); } @Test public void testSimpleWithDecimals() { RealMatrix m = MatrixUtils.createRealMatrix(new double[][] {{1.23, 1.43, 1.63}, {2.46, 2.46, 2.66}}); String expected = "{{1" + getDecimalCharacter() + "23,1" + getDecimalCharacter() + "43,1" + getDecimalCharacter() + "63},{2" + getDecimalCharacter() + "46,2" + getDecimalCharacter() + "46,2" + getDecimalCharacter() + "66}}"; String actual = realMatrixFormat.format(m); Assert.assertEquals(expected, actual); } @Test public void testSimpleWithDecimalsTrunc() { RealMatrix m = MatrixUtils.createRealMatrix(new double[][] {{1.232323232323, 1.43, 1.63}, {2.46, 2.46, 2.666666666666}}); String expected = "{{1" + getDecimalCharacter() + "2323232323,1" + getDecimalCharacter() + "43,1" + getDecimalCharacter() + "63},{2" + getDecimalCharacter() + "46,2" + getDecimalCharacter() + "46,2" + getDecimalCharacter() + "6666666667}}"; String actual = realMatrixFormat.format(m); Assert.assertEquals(expected, actual); } @Test public void testNegativeComponent() { RealMatrix m = MatrixUtils.createRealMatrix(new double[][] {{-1.232323232323, 1.43, 1.63}, {2.46, 2.46, 2.66}}); String expected = "{{-1" + getDecimalCharacter() + "2323232323,1" + getDecimalCharacter() + "43,1" + getDecimalCharacter() + "63},{2" + getDecimalCharacter() + "46,2" + getDecimalCharacter() + "46,2" + getDecimalCharacter() + "66}}"; String actual = realMatrixFormat.format(m); Assert.assertEquals(expected, actual); } @Test public void testNegativeComponent2() { RealMatrix m = MatrixUtils.createRealMatrix(new double[][] {{1.23, -1.434343434343, 1.63}, {2.46, 2.46, 2.66}}); String expected = "{{1" + getDecimalCharacter() + "23,-1" + getDecimalCharacter() + "4343434343,1" + getDecimalCharacter() + "63},{2" + getDecimalCharacter() + "46,2" + getDecimalCharacter() + "46,2" + getDecimalCharacter() + "66}}"; String actual = realMatrixFormat.format(m); Assert.assertEquals(expected, actual); } @Test public void testNegativeSecondRow() { RealMatrix m = MatrixUtils.createRealMatrix(new double[][] {{1.23, 1.43, 1.63}, {-2.66666666666, 2.46, 2.66}}); String expected = "{{1" + getDecimalCharacter() + "23,1" + getDecimalCharacter() + "43,1" + getDecimalCharacter() + "63},{-2" + getDecimalCharacter() + "6666666667,2" + getDecimalCharacter() + "46,2" + getDecimalCharacter() + "66}}"; String actual = realMatrixFormat.format(m); Assert.assertEquals(expected, actual); } @Test public void testNonDefaultSetting() { RealMatrix m = MatrixUtils.createRealMatrix(new double[][] {{1, 1, 1}, {1, 1, 1}}); String expected = "[1, 1, 1; 1, 1, 1]"; String actual = realMatrixFormatOctave.format(m); Assert.assertEquals(expected, actual); } @Test public void testDefaultFormat() { Locale defaultLocale = Locale.getDefault(); Locale.setDefault(getLocale()); RealMatrix m = MatrixUtils.createRealMatrix(new double[][] {{232.2222222222, -342.33333333333, 432.44444444444}}); String expected = "{{232" + getDecimalCharacter() + "2222222222,-342" + getDecimalCharacter() + "3333333333,432" + getDecimalCharacter() + "4444444444}}"; String actual = (new RealMatrixFormat()).format(m); Assert.assertEquals(expected, actual); Locale.setDefault(defaultLocale); } @Test public void testNan() { RealMatrix m = MatrixUtils.createRealMatrix(new double[][] {{Double.NaN, Double.NaN, Double.NaN}}); String expected = "{{(NaN),(NaN),(NaN)}}"; String actual = realMatrixFormat.format(m); Assert.assertEquals(expected, actual); } @Test public void testPositiveInfinity() { RealMatrix m = MatrixUtils.createRealMatrix( new double[][] {{Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY}}); String expected = "{{(Infinity),(Infinity),(Infinity)}}"; String actual = realMatrixFormat.format(m); Assert.assertEquals(expected, actual); } @Test public void tesNegativeInfinity() { RealMatrix m = MatrixUtils.createRealMatrix( new double[][] {{Double.NEGATIVE_INFINITY, Double.NEGATIVE_INFINITY, Double.NEGATIVE_INFINITY}}); String expected = "{{(-Infinity),(-Infinity),(-Infinity)}}"; String actual = realMatrixFormat.format(m); Assert.assertEquals(expected, actual); } @Test public void testParseSimpleNoDecimals() { String source = "{{1, 1, 1}, {1, 1, 1}}"; RealMatrix expected = MatrixUtils.createRealMatrix(new double[][] {{1, 1, 1}, {1, 1, 1}}); RealMatrix actual = realMatrixFormat.parse(source); Assert.assertEquals(expected, actual); } @Test @Ignore public void testParseSimpleWithClosingRowSeparator() { String source = "{{1, 1, 1},{1, 1, 1}, }}"; RealMatrix expected = MatrixUtils.createRealMatrix(new double[][] {{1, 1, 1}, {1, 1, 1}}); RealMatrix actual = realMatrixFormat.parse(source); Assert.assertEquals(expected, actual); } @Test public void testParseIgnoredWhitespace() { RealMatrix expected = MatrixUtils.createRealMatrix(new double[][] {{1, 1, 1}, {1, 1, 1}}); ParsePosition pos1 = new ParsePosition(0); String source1 = "{{1,1,1},{1,1,1}}"; Assert.assertEquals(expected, realMatrixFormat.parse(source1, pos1)); Assert.assertEquals(source1.length(), pos1.getIndex()); ParsePosition pos2 = new ParsePosition(0); String source2 = " { { 1 , 1 , 1 } , { 1 , 1 , 1 } } "; Assert.assertEquals(expected, realMatrixFormat.parse(source2, pos2)); Assert.assertEquals(source2.length() - 1, pos2.getIndex()); } @Test public void testParseSimpleWithDecimals() { String source = "{{1" + getDecimalCharacter() + "23,1" + getDecimalCharacter() + "43,1" + getDecimalCharacter() + "63}}"; RealMatrix expected = MatrixUtils.createRealMatrix(new double[][] {{1.23, 1.43, 1.63}}); RealMatrix actual = realMatrixFormat.parse(source); Assert.assertEquals(expected, actual); } @Test public void testParseSimpleWithDecimalsTrunc() { String source = "{{1" + getDecimalCharacter() + "2323,1" + getDecimalCharacter() + "4343,1" + getDecimalCharacter() + "6333}}"; RealMatrix expected = MatrixUtils.createRealMatrix(new double[][] {{1.2323, 1.4343, 1.6333}}); RealMatrix actual = realMatrixFormat.parse(source); Assert.assertEquals(expected, actual); } @Test public void testParseNegativeComponent() { String source = "{{-1" + getDecimalCharacter() + "2323,1" + getDecimalCharacter() + "4343,1" + getDecimalCharacter() + "6333}}"; RealMatrix expected = MatrixUtils.createRealMatrix(new double[][] {{-1.2323, 1.4343, 1.6333}}); RealMatrix actual = realMatrixFormat.parse(source); Assert.assertEquals(expected, actual); } @Test public void testParseNegativeAll() { String source = "{{-1" + getDecimalCharacter() + "2323,-1" + getDecimalCharacter() + "4343,-1" + getDecimalCharacter() + "6333}}"; RealMatrix expected = MatrixUtils.createRealMatrix(new double[][] {{-1.2323, -1.4343, -1.6333}}); RealMatrix actual = realMatrixFormat.parse(source); Assert.assertEquals(expected, actual); } @Test public void testParseZeroComponent() { String source = "{{0" + getDecimalCharacter() + "0,-1" + getDecimalCharacter() + "4343,1" + getDecimalCharacter() + "6333}}"; RealMatrix expected = MatrixUtils.createRealMatrix(new double[][] {{0.0, -1.4343, 1.6333}}); RealMatrix actual = realMatrixFormat.parse(source); Assert.assertEquals(expected, actual); } @Test public void testParseNonDefaultSetting() { String source = "[1" + getDecimalCharacter() + "2323, 1" + getDecimalCharacter() + "4343, 1" + getDecimalCharacter() + "6333]"; RealMatrix expected = MatrixUtils.createRealMatrix(new double[][] {{1.2323, 1.4343, 1.6333}}); RealMatrix actual = realMatrixFormatOctave.parse(source); Assert.assertEquals(expected, actual); } @Test public void testParseNan() { String source = "{{(NaN), (NaN), (NaN)}}"; RealMatrix actual = realMatrixFormat.parse(source); RealMatrix expected = MatrixUtils.createRealMatrix(new double[][] {{Double.NaN, Double.NaN, Double.NaN}}); for (int i = 0; i < expected.getRowDimension(); i++) { for (int j = 0; j < expected.getColumnDimension(); j++) { Assert.assertTrue(Double.isNaN(actual.getEntry(i, j))); } } } @Test public void testParsePositiveInfinity() { String source = "{{(Infinity), (Infinity), (Infinity)}}"; RealMatrix actual = realMatrixFormat.parse(source); RealMatrix expected = MatrixUtils.createRealMatrix( new double[][] {{Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY}}); Assert.assertEquals(expected, actual); } @Test public void testParseNegativeInfinity() { String source = "{{(-Infinity), (-Infinity), (-Infinity)}}"; RealMatrix actual = realMatrixFormat.parse(source); RealMatrix expected = MatrixUtils.createRealMatrix( new double[][] {{Double.NEGATIVE_INFINITY, Double.NEGATIVE_INFINITY, Double.NEGATIVE_INFINITY}}); Assert.assertEquals(expected, actual); } @Test public void testParseNoComponents() { try { realMatrixFormat.parse("{{ }}"); Assert.fail("Expecting MathParseException"); } catch (MathParseException pe) { // expected behavior } } @Test public void testParseManyComponents() { RealMatrix parsed = realMatrixFormat.parse("{{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}}"); Assert.assertEquals(24, parsed.getColumnDimension()); } @Test public void testConstructorSingleFormat() { NumberFormat nf = NumberFormat.getInstance(); RealMatrixFormat mf = new RealMatrixFormat(nf); Assert.assertNotNull(mf); Assert.assertEquals(nf, mf.getFormat()); } @Test public void testForgottenPrefix() { ParsePosition pos = new ParsePosition(0); final String source = "1; 1; 1]"; Assert.assertNull("Should not parse <"+source+">", realMatrixFormat.parse(source, pos)); Assert.assertEquals(0, pos.getErrorIndex()); } @Test public void testForgottenSeparator() { ParsePosition pos = new ParsePosition(0); final String source = "{{1, 1 1}}"; Assert.assertNull("Should not parse <"+source+">", realMatrixFormat.parse(source, pos)); Assert.assertEquals(7, pos.getErrorIndex()); } @Test public void testForgottenSuffix() { ParsePosition pos = new ParsePosition(0); final String source = "{{1, 1, 1 "; Assert.assertNull("Should not parse <"+source+">", realMatrixFormat.parse(source, pos)); Assert.assertEquals(9, pos.getErrorIndex()); } } ././@LongLink100644 0 0 146 12126630646 10262 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/linear/RealVectorFormatAbstractTest.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/linear/RealVectorFormatAbstractTest.jav100644 1750 1750 31730 12126627674 32327 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.linear; import java.text.NumberFormat; import java.text.ParsePosition; import java.util.Locale; import org.junit.Test; import org.junit.Assert; import org.apache.commons.math3.exception.MathParseException; public abstract class RealVectorFormatAbstractTest { RealVectorFormat realVectorFormat = null; RealVectorFormat realVectorFormatSquare = null; protected abstract Locale getLocale(); protected abstract char getDecimalCharacter(); public RealVectorFormatAbstractTest() { realVectorFormat = RealVectorFormat.getInstance(getLocale()); final NumberFormat nf = NumberFormat.getInstance(getLocale()); nf.setMaximumFractionDigits(2); realVectorFormatSquare = new RealVectorFormat("[", "]", " : ", nf); } @Test public void testSimpleNoDecimals() { ArrayRealVector c = new ArrayRealVector(new double[] {1, 1, 1}); String expected = "{1; 1; 1}"; String actual = realVectorFormat.format(c); Assert.assertEquals(expected, actual); } @Test public void testSimpleWithDecimals() { ArrayRealVector c = new ArrayRealVector(new double[] {1.23, 1.43, 1.63}); String expected = "{1" + getDecimalCharacter() + "23; 1" + getDecimalCharacter() + "43; 1" + getDecimalCharacter() + "63}"; String actual = realVectorFormat.format(c); Assert.assertEquals(expected, actual); } @Test public void testSimpleWithDecimalsTrunc() { ArrayRealVector c = new ArrayRealVector(new double[] {1.232323232323, 1.43434343434343, 1.633333333333}); String expected = "{1" + getDecimalCharacter() + "2323232323; 1" + getDecimalCharacter() + "4343434343; 1" + getDecimalCharacter() + "6333333333}"; String actual = realVectorFormat.format(c); Assert.assertEquals(expected, actual); } @Test public void testNegativeX() { ArrayRealVector c = new ArrayRealVector(new double[] {-1.232323232323, 1.43, 1.63}); String expected = "{-1" + getDecimalCharacter() + "2323232323; 1" + getDecimalCharacter() + "43; 1" + getDecimalCharacter() + "63}"; String actual = realVectorFormat.format(c); Assert.assertEquals(expected, actual); } @Test public void testNegativeY() { ArrayRealVector c = new ArrayRealVector(new double[] {1.23, -1.434343434343, 1.63}); String expected = "{1" + getDecimalCharacter() + "23; -1" + getDecimalCharacter() + "4343434343; 1" + getDecimalCharacter() + "63}"; String actual = realVectorFormat.format(c); Assert.assertEquals(expected, actual); } @Test public void testNegativeZ() { ArrayRealVector c = new ArrayRealVector(new double[] {1.23, 1.43, -1.633333333333}); String expected = "{1" + getDecimalCharacter() + "23; 1" + getDecimalCharacter() + "43; -1" + getDecimalCharacter() + "6333333333}"; String actual = realVectorFormat.format(c); Assert.assertEquals(expected, actual); } @Test public void testNonDefaultSetting() { ArrayRealVector c = new ArrayRealVector(new double[] {1, 1, 1}); String expected = "[1 : 1 : 1]"; String actual = realVectorFormatSquare.format(c); Assert.assertEquals(expected, actual); } @Test public void testDefaultFormatRealVectorImpl() { Locale defaultLocal = Locale.getDefault(); Locale.setDefault(getLocale()); ArrayRealVector c = new ArrayRealVector(new double[] {232.22222222222, -342.3333333333, 432.44444444444}); String expected = "{232" + getDecimalCharacter() + "2222222222; -342" + getDecimalCharacter() + "3333333333; 432" + getDecimalCharacter() + "4444444444}"; String actual = (new RealVectorFormat()).format(c); Assert.assertEquals(expected, actual); Locale.setDefault(defaultLocal); } @Test public void testNan() { ArrayRealVector c = new ArrayRealVector(new double[] {Double.NaN, Double.NaN, Double.NaN}); String expected = "{(NaN); (NaN); (NaN)}"; String actual = realVectorFormat.format(c); Assert.assertEquals(expected, actual); } @Test public void testPositiveInfinity() { ArrayRealVector c = new ArrayRealVector(new double[] { Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY }); String expected = "{(Infinity); (Infinity); (Infinity)}"; String actual = realVectorFormat.format(c); Assert.assertEquals(expected, actual); } @Test public void tesNegativeInfinity() { ArrayRealVector c = new ArrayRealVector(new double[] { Double.NEGATIVE_INFINITY, Double.NEGATIVE_INFINITY, Double.NEGATIVE_INFINITY }); String expected = "{(-Infinity); (-Infinity); (-Infinity)}"; String actual = realVectorFormat.format(c); Assert.assertEquals(expected, actual); } @Test public void testParseSimpleNoDecimals() { String source = "{1; 1; 1}"; ArrayRealVector expected = new ArrayRealVector(new double[] {1, 1, 1}); ArrayRealVector actual = realVectorFormat.parse(source); Assert.assertEquals(expected, actual); } @Test public void testParseIgnoredWhitespace() { ArrayRealVector expected = new ArrayRealVector(new double[] {1, 1, 1}); ParsePosition pos1 = new ParsePosition(0); String source1 = "{1;1;1}"; Assert.assertEquals(expected, realVectorFormat.parse(source1, pos1)); Assert.assertEquals(source1.length(), pos1.getIndex()); ParsePosition pos2 = new ParsePosition(0); String source2 = " { 1 ; 1 ; 1 } "; Assert.assertEquals(expected, realVectorFormat.parse(source2, pos2)); Assert.assertEquals(source2.length() - 1, pos2.getIndex()); } @Test public void testParseSimpleWithDecimals() { String source = "{1" + getDecimalCharacter() + "23; 1" + getDecimalCharacter() + "43; 1" + getDecimalCharacter() + "63}"; ArrayRealVector expected = new ArrayRealVector(new double[] {1.23, 1.43, 1.63}); ArrayRealVector actual = realVectorFormat.parse(source); Assert.assertEquals(expected, actual); } @Test public void testParseSimpleWithDecimalsTrunc() { String source = "{1" + getDecimalCharacter() + "2323; 1" + getDecimalCharacter() + "4343; 1" + getDecimalCharacter() + "6333}"; ArrayRealVector expected = new ArrayRealVector(new double[] {1.2323, 1.4343, 1.6333}); ArrayRealVector actual = realVectorFormat.parse(source); Assert.assertEquals(expected, actual); } @Test public void testParseNegativeX() { String source = "{-1" + getDecimalCharacter() + "2323; 1" + getDecimalCharacter() + "4343; 1" + getDecimalCharacter() + "6333}"; ArrayRealVector expected = new ArrayRealVector(new double[] {-1.2323, 1.4343, 1.6333}); ArrayRealVector actual = realVectorFormat.parse(source); Assert.assertEquals(expected, actual); } @Test public void testParseNegativeY() { String source = "{1" + getDecimalCharacter() + "2323; -1" + getDecimalCharacter() + "4343; 1" + getDecimalCharacter() + "6333}"; ArrayRealVector expected = new ArrayRealVector(new double[] {1.2323, -1.4343, 1.6333}); ArrayRealVector actual = realVectorFormat.parse(source); Assert.assertEquals(expected, actual); } @Test public void testParseNegativeZ() { String source = "{1" + getDecimalCharacter() + "2323; 1" + getDecimalCharacter() + "4343; -1" + getDecimalCharacter() + "6333}"; ArrayRealVector expected = new ArrayRealVector(new double[] {1.2323, 1.4343, -1.6333}); ArrayRealVector actual = realVectorFormat.parse(source); Assert.assertEquals(expected, actual); } @Test public void testParseNegativeAll() { String source = "{-1" + getDecimalCharacter() + "2323; -1" + getDecimalCharacter() + "4343; -1" + getDecimalCharacter() + "6333}"; ArrayRealVector expected = new ArrayRealVector(new double[] {-1.2323, -1.4343, -1.6333}); ArrayRealVector actual = realVectorFormat.parse(source); Assert.assertEquals(expected, actual); } @Test public void testParseZeroX() { String source = "{0" + getDecimalCharacter() + "0; -1" + getDecimalCharacter() + "4343; 1" + getDecimalCharacter() + "6333}"; ArrayRealVector expected = new ArrayRealVector(new double[] {0.0, -1.4343, 1.6333}); ArrayRealVector actual = realVectorFormat.parse(source); Assert.assertEquals(expected, actual); } @Test public void testParseNonDefaultSetting() { String source = "[1" + getDecimalCharacter() + "2323 : 1" + getDecimalCharacter() + "4343 : 1" + getDecimalCharacter() + "6333]"; ArrayRealVector expected = new ArrayRealVector(new double[] {1.2323, 1.4343, 1.6333}); ArrayRealVector actual = realVectorFormatSquare.parse(source); Assert.assertEquals(expected, actual); } @Test public void testParseNan() { String source = "{(NaN); (NaN); (NaN)}"; ArrayRealVector actual = realVectorFormat.parse(source); Assert.assertEquals(new ArrayRealVector(new double[] {Double.NaN, Double.NaN, Double.NaN}), actual); } @Test public void testParsePositiveInfinity() { String source = "{(Infinity); (Infinity); (Infinity)}"; ArrayRealVector actual = realVectorFormat.parse(source); Assert.assertEquals(new ArrayRealVector(new double[] { Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY }), actual); } @Test public void testParseNegativeInfinity() { String source = "{(-Infinity); (-Infinity); (-Infinity)}"; ArrayRealVector actual = realVectorFormat.parse(source); Assert.assertEquals(new ArrayRealVector(new double[] { Double.NEGATIVE_INFINITY, Double.NEGATIVE_INFINITY, Double.NEGATIVE_INFINITY }), actual); } @Test public void testParseNoComponents() { try { realVectorFormat.parse("{ }"); Assert.fail("Expecting MathParseException"); } catch (MathParseException pe) { // expected behavior } } @Test public void testParseManyComponents() { ArrayRealVector parsed = realVectorFormat.parse("{0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0}"); Assert.assertEquals(24, parsed.getDimension()); } @Test public void testConstructorSingleFormat() { NumberFormat nf = NumberFormat.getInstance(); RealVectorFormat cf = new RealVectorFormat(nf); Assert.assertNotNull(cf); Assert.assertEquals(nf, cf.getFormat()); } @Test public void testForgottenPrefix() { ParsePosition pos = new ParsePosition(0); final String source = "1; 1; 1}"; Assert.assertNull("Should not parse <"+source+">",new RealVectorFormat().parse(source, pos)); Assert.assertEquals(0, pos.getErrorIndex()); } @Test public void testForgottenSeparator() { ParsePosition pos = new ParsePosition(0); final String source = "{1; 1 1}"; Assert.assertNull("Should not parse <"+source+">",new RealVectorFormat().parse(source, pos)); Assert.assertEquals(6, pos.getErrorIndex()); } @Test public void testForgottenSuffix() { ParsePosition pos = new ParsePosition(0); final String source = "{1; 1; 1 "; Assert.assertNull("Should not parse <"+source+">",new RealVectorFormat().parse(source, pos)); Assert.assertEquals(8, pos.getErrorIndex()); } } ././@LongLink100644 0 0 156 12126630646 10263 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/linear/RectangularCholeskyDecompositionTest.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/linear/RectangularCholeskyDecomposition100644 1750 1750 11020 12126627674 32501 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.linear; import org.junit.Test; import org.junit.Assert; public class RectangularCholeskyDecompositionTest { @Test public void testDecomposition3x3() { RealMatrix m = MatrixUtils.createRealMatrix(new double[][] { { 1, 9, 9 }, { 9, 225, 225 }, { 9, 225, 625 } }); RectangularCholeskyDecomposition d = new RectangularCholeskyDecomposition(m, 1.0e-6); // as this decomposition permutes lines and columns, the root is NOT triangular // (in fact here it is the lower right part of the matrix which is zero and // the upper left non-zero) Assert.assertEquals(0.8, d.getRootMatrix().getEntry(0, 2), 1.0e-15); Assert.assertEquals(25.0, d.getRootMatrix().getEntry(2, 0), 1.0e-15); Assert.assertEquals(0.0, d.getRootMatrix().getEntry(2, 2), 1.0e-15); RealMatrix root = d.getRootMatrix(); RealMatrix rebuiltM = root.multiply(root.transpose()); Assert.assertEquals(0.0, m.subtract(rebuiltM).getNorm(), 1.0e-15); } @Test public void testFullRank() { RealMatrix base = MatrixUtils.createRealMatrix(new double[][] { { 0.1159548705, 0., 0., 0. }, { 0.0896442724, 0.1223540781, 0., 0. }, { 0.0852155322, 4.558668e-3, 0.1083577299, 0. }, { 0.0905486674, 0.0213768077, 0.0128878333, 0.1014155693 } }); RealMatrix m = base.multiply(base.transpose()); RectangularCholeskyDecomposition d = new RectangularCholeskyDecomposition(m, 1.0e-10); RealMatrix root = d.getRootMatrix(); RealMatrix rebuiltM = root.multiply(root.transpose()); Assert.assertEquals(0.0, m.subtract(rebuiltM).getNorm(), 1.0e-15); // the pivoted Cholesky decomposition is *not* unique. Here, the root is // not equal to the original trianbular base matrix Assert.assertTrue(root.subtract(base).getNorm() > 0.3); } @Test public void testMath789() { final RealMatrix m1 = MatrixUtils.createRealMatrix(new double[][]{ {0.013445532, 0.010394690, 0.009881156, 0.010499559}, {0.010394690, 0.023006616, 0.008196856, 0.010732709}, {0.009881156, 0.008196856, 0.019023866, 0.009210099}, {0.010499559, 0.010732709, 0.009210099, 0.019107243} }); composeAndTest(m1, 4); final RealMatrix m2 = MatrixUtils.createRealMatrix(new double[][]{ {0.0, 0.0, 0.0, 0.0, 0.0}, {0.0, 0.013445532, 0.010394690, 0.009881156, 0.010499559}, {0.0, 0.010394690, 0.023006616, 0.008196856, 0.010732709}, {0.0, 0.009881156, 0.008196856, 0.019023866, 0.009210099}, {0.0, 0.010499559, 0.010732709, 0.009210099, 0.019107243} }); composeAndTest(m2, 4); final RealMatrix m3 = MatrixUtils.createRealMatrix(new double[][]{ {0.013445532, 0.010394690, 0.0, 0.009881156, 0.010499559}, {0.010394690, 0.023006616, 0.0, 0.008196856, 0.010732709}, {0.0, 0.0, 0.0, 0.0, 0.0}, {0.009881156, 0.008196856, 0.0, 0.019023866, 0.009210099}, {0.010499559, 0.010732709, 0.0, 0.009210099, 0.019107243} }); composeAndTest(m3, 4); } private void composeAndTest(RealMatrix m, int expectedRank) { RectangularCholeskyDecomposition r = new RectangularCholeskyDecomposition(m); Assert.assertEquals(expectedRank, r.getRank()); RealMatrix root = r.getRootMatrix(); RealMatrix rebuiltMatrix = root.multiply(root.transpose()); Assert.assertEquals(0.0, m.subtract(rebuiltMatrix).getNorm(), 1.0e-16); } } commons-math3-3.2-src/src/test/java/org/apache/commons/math3/linear/RRQRDecompositionTest.java100644 1750 1750 20240 12126627674 31102 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.linear; import java.util.Random; import org.junit.Assert; import org.junit.Test; public class RRQRDecompositionTest { private double[][] testData3x3NonSingular = { { 12, -51, 4 }, { 6, 167, -68 }, { -4, 24, -41 }, }; private double[][] testData3x3Singular = { { 1, 4, 7, }, { 2, 5, 8, }, { 3, 6, 9, }, }; private double[][] testData3x4 = { { 12, -51, 4, 1 }, { 6, 167, -68, 2 }, { -4, 24, -41, 3 }, }; private double[][] testData4x3 = { { 12, -51, 4, }, { 6, 167, -68, }, { -4, 24, -41, }, { -5, 34, 7, }, }; private static final double entryTolerance = 10e-16; private static final double normTolerance = 10e-14; /** test dimensions */ @Test public void testDimensions() { checkDimension(MatrixUtils.createRealMatrix(testData3x3NonSingular)); checkDimension(MatrixUtils.createRealMatrix(testData4x3)); checkDimension(MatrixUtils.createRealMatrix(testData3x4)); Random r = new Random(643895747384642l); int p = (5 * BlockRealMatrix.BLOCK_SIZE) / 4; int q = (7 * BlockRealMatrix.BLOCK_SIZE) / 4; checkDimension(createTestMatrix(r, p, q)); checkDimension(createTestMatrix(r, q, p)); } private void checkDimension(RealMatrix m) { int rows = m.getRowDimension(); int columns = m.getColumnDimension(); RRQRDecomposition qr = new RRQRDecomposition(m); Assert.assertEquals(rows, qr.getQ().getRowDimension()); Assert.assertEquals(rows, qr.getQ().getColumnDimension()); Assert.assertEquals(rows, qr.getR().getRowDimension()); Assert.assertEquals(columns, qr.getR().getColumnDimension()); } /** test AP = QR */ @Test public void testAPEqualQR() { checkAPEqualQR(MatrixUtils.createRealMatrix(testData3x3NonSingular)); checkAPEqualQR(MatrixUtils.createRealMatrix(testData3x3Singular)); checkAPEqualQR(MatrixUtils.createRealMatrix(testData3x4)); checkAPEqualQR(MatrixUtils.createRealMatrix(testData4x3)); Random r = new Random(643895747384642l); int p = (5 * BlockRealMatrix.BLOCK_SIZE) / 4; int q = (7 * BlockRealMatrix.BLOCK_SIZE) / 4; checkAPEqualQR(createTestMatrix(r, p, q)); checkAPEqualQR(createTestMatrix(r, q, p)); } private void checkAPEqualQR(RealMatrix m) { RRQRDecomposition rrqr = new RRQRDecomposition(m); double norm = rrqr.getQ().multiply(rrqr.getR()).subtract(m.multiply(rrqr.getP())).getNorm(); Assert.assertEquals(0, norm, normTolerance); } /** test the orthogonality of Q */ @Test public void testQOrthogonal() { checkQOrthogonal(MatrixUtils.createRealMatrix(testData3x3NonSingular)); checkQOrthogonal(MatrixUtils.createRealMatrix(testData3x3Singular)); checkQOrthogonal(MatrixUtils.createRealMatrix(testData3x4)); checkQOrthogonal(MatrixUtils.createRealMatrix(testData4x3)); Random r = new Random(643895747384642l); int p = (5 * BlockRealMatrix.BLOCK_SIZE) / 4; int q = (7 * BlockRealMatrix.BLOCK_SIZE) / 4; checkQOrthogonal(createTestMatrix(r, p, q)); checkQOrthogonal(createTestMatrix(r, q, p)); } private void checkQOrthogonal(RealMatrix m) { RRQRDecomposition qr = new RRQRDecomposition(m); RealMatrix eye = MatrixUtils.createRealIdentityMatrix(m.getRowDimension()); double norm = qr.getQT().multiply(qr.getQ()).subtract(eye).getNorm(); Assert.assertEquals(0, norm, normTolerance); } /** test that R is upper triangular */ @Test public void testRUpperTriangular() { RealMatrix matrix = MatrixUtils.createRealMatrix(testData3x3NonSingular); checkUpperTriangular(new RRQRDecomposition(matrix).getR()); matrix = MatrixUtils.createRealMatrix(testData3x3Singular); checkUpperTriangular(new RRQRDecomposition(matrix).getR()); matrix = MatrixUtils.createRealMatrix(testData3x4); checkUpperTriangular(new RRQRDecomposition(matrix).getR()); matrix = MatrixUtils.createRealMatrix(testData4x3); checkUpperTriangular(new RRQRDecomposition(matrix).getR()); Random r = new Random(643895747384642l); int p = (5 * BlockRealMatrix.BLOCK_SIZE) / 4; int q = (7 * BlockRealMatrix.BLOCK_SIZE) / 4; matrix = createTestMatrix(r, p, q); checkUpperTriangular(new RRQRDecomposition(matrix).getR()); matrix = createTestMatrix(r, p, q); checkUpperTriangular(new RRQRDecomposition(matrix).getR()); } private void checkUpperTriangular(RealMatrix m) { m.walkInOptimizedOrder(new DefaultRealMatrixPreservingVisitor() { @Override public void visit(int row, int column, double value) { if (column < row) { Assert.assertEquals(0.0, value, entryTolerance); } } }); } /** test that H is trapezoidal */ @Test public void testHTrapezoidal() { RealMatrix matrix = MatrixUtils.createRealMatrix(testData3x3NonSingular); checkTrapezoidal(new RRQRDecomposition(matrix).getH()); matrix = MatrixUtils.createRealMatrix(testData3x3Singular); checkTrapezoidal(new RRQRDecomposition(matrix).getH()); matrix = MatrixUtils.createRealMatrix(testData3x4); checkTrapezoidal(new RRQRDecomposition(matrix).getH()); matrix = MatrixUtils.createRealMatrix(testData4x3); checkTrapezoidal(new RRQRDecomposition(matrix).getH()); Random r = new Random(643895747384642l); int p = (5 * BlockRealMatrix.BLOCK_SIZE) / 4; int q = (7 * BlockRealMatrix.BLOCK_SIZE) / 4; matrix = createTestMatrix(r, p, q); checkTrapezoidal(new RRQRDecomposition(matrix).getH()); matrix = createTestMatrix(r, p, q); checkTrapezoidal(new RRQRDecomposition(matrix).getH()); } private void checkTrapezoidal(RealMatrix m) { m.walkInOptimizedOrder(new DefaultRealMatrixPreservingVisitor() { @Override public void visit(int row, int column, double value) { if (column > row) { Assert.assertEquals(0.0, value, entryTolerance); } } }); } @Test(expected=SingularMatrixException.class) public void testNonInvertible() { RRQRDecomposition qr = new RRQRDecomposition(MatrixUtils.createRealMatrix(testData3x3Singular), 3.0e-16); qr.getSolver().getInverse(); } private RealMatrix createTestMatrix(final Random r, final int rows, final int columns) { RealMatrix m = MatrixUtils.createRealMatrix(rows, columns); m.walkInOptimizedOrder(new DefaultRealMatrixChangingVisitor(){ @Override public double visit(int row, int column, double value) { return 2.0 * r.nextDouble() - 1.0; } }); return m; } /** test the rank is returned correctly */ @Test public void testRank() { double[][] d = { { 1, 1, 1 }, { 0, 0, 0 }, { 1, 2, 3 } }; RealMatrix m = new Array2DRowRealMatrix(d); RRQRDecomposition qr = new RRQRDecomposition(m); Assert.assertEquals(2, qr.getRank(1.0e-16)); } } commons-math3-3.2-src/src/test/java/org/apache/commons/math3/linear/QRDecompositionTest.java100644 1750 1750 22607 12126627674 30647 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.linear; import java.util.Random; import org.apache.commons.math3.linear.SingularMatrixException; import org.junit.Assert; import org.junit.Test; public class QRDecompositionTest { private double[][] testData3x3NonSingular = { { 12, -51, 4 }, { 6, 167, -68 }, { -4, 24, -41 }, }; private double[][] testData3x3Singular = { { 1, 4, 7, }, { 2, 5, 8, }, { 3, 6, 9, }, }; private double[][] testData3x4 = { { 12, -51, 4, 1 }, { 6, 167, -68, 2 }, { -4, 24, -41, 3 }, }; private double[][] testData4x3 = { { 12, -51, 4, }, { 6, 167, -68, }, { -4, 24, -41, }, { -5, 34, 7, }, }; private static final double entryTolerance = 10e-16; private static final double normTolerance = 10e-14; /** test dimensions */ @Test public void testDimensions() { checkDimension(MatrixUtils.createRealMatrix(testData3x3NonSingular)); checkDimension(MatrixUtils.createRealMatrix(testData4x3)); checkDimension(MatrixUtils.createRealMatrix(testData3x4)); Random r = new Random(643895747384642l); int p = (5 * BlockRealMatrix.BLOCK_SIZE) / 4; int q = (7 * BlockRealMatrix.BLOCK_SIZE) / 4; checkDimension(createTestMatrix(r, p, q)); checkDimension(createTestMatrix(r, q, p)); } private void checkDimension(RealMatrix m) { int rows = m.getRowDimension(); int columns = m.getColumnDimension(); QRDecomposition qr = new QRDecomposition(m); Assert.assertEquals(rows, qr.getQ().getRowDimension()); Assert.assertEquals(rows, qr.getQ().getColumnDimension()); Assert.assertEquals(rows, qr.getR().getRowDimension()); Assert.assertEquals(columns, qr.getR().getColumnDimension()); } /** test A = QR */ @Test public void testAEqualQR() { checkAEqualQR(MatrixUtils.createRealMatrix(testData3x3NonSingular)); checkAEqualQR(MatrixUtils.createRealMatrix(testData3x3Singular)); checkAEqualQR(MatrixUtils.createRealMatrix(testData3x4)); checkAEqualQR(MatrixUtils.createRealMatrix(testData4x3)); Random r = new Random(643895747384642l); int p = (5 * BlockRealMatrix.BLOCK_SIZE) / 4; int q = (7 * BlockRealMatrix.BLOCK_SIZE) / 4; checkAEqualQR(createTestMatrix(r, p, q)); checkAEqualQR(createTestMatrix(r, q, p)); } private void checkAEqualQR(RealMatrix m) { QRDecomposition qr = new QRDecomposition(m); double norm = qr.getQ().multiply(qr.getR()).subtract(m).getNorm(); Assert.assertEquals(0, norm, normTolerance); } /** test the orthogonality of Q */ @Test public void testQOrthogonal() { checkQOrthogonal(MatrixUtils.createRealMatrix(testData3x3NonSingular)); checkQOrthogonal(MatrixUtils.createRealMatrix(testData3x3Singular)); checkQOrthogonal(MatrixUtils.createRealMatrix(testData3x4)); checkQOrthogonal(MatrixUtils.createRealMatrix(testData4x3)); Random r = new Random(643895747384642l); int p = (5 * BlockRealMatrix.BLOCK_SIZE) / 4; int q = (7 * BlockRealMatrix.BLOCK_SIZE) / 4; checkQOrthogonal(createTestMatrix(r, p, q)); checkQOrthogonal(createTestMatrix(r, q, p)); } private void checkQOrthogonal(RealMatrix m) { QRDecomposition qr = new QRDecomposition(m); RealMatrix eye = MatrixUtils.createRealIdentityMatrix(m.getRowDimension()); double norm = qr.getQT().multiply(qr.getQ()).subtract(eye).getNorm(); Assert.assertEquals(0, norm, normTolerance); } /** test that R is upper triangular */ @Test public void testRUpperTriangular() { RealMatrix matrix = MatrixUtils.createRealMatrix(testData3x3NonSingular); checkUpperTriangular(new QRDecomposition(matrix).getR()); matrix = MatrixUtils.createRealMatrix(testData3x3Singular); checkUpperTriangular(new QRDecomposition(matrix).getR()); matrix = MatrixUtils.createRealMatrix(testData3x4); checkUpperTriangular(new QRDecomposition(matrix).getR()); matrix = MatrixUtils.createRealMatrix(testData4x3); checkUpperTriangular(new QRDecomposition(matrix).getR()); Random r = new Random(643895747384642l); int p = (5 * BlockRealMatrix.BLOCK_SIZE) / 4; int q = (7 * BlockRealMatrix.BLOCK_SIZE) / 4; matrix = createTestMatrix(r, p, q); checkUpperTriangular(new QRDecomposition(matrix).getR()); matrix = createTestMatrix(r, p, q); checkUpperTriangular(new QRDecomposition(matrix).getR()); } private void checkUpperTriangular(RealMatrix m) { m.walkInOptimizedOrder(new DefaultRealMatrixPreservingVisitor() { @Override public void visit(int row, int column, double value) { if (column < row) { Assert.assertEquals(0.0, value, entryTolerance); } } }); } /** test that H is trapezoidal */ @Test public void testHTrapezoidal() { RealMatrix matrix = MatrixUtils.createRealMatrix(testData3x3NonSingular); checkTrapezoidal(new QRDecomposition(matrix).getH()); matrix = MatrixUtils.createRealMatrix(testData3x3Singular); checkTrapezoidal(new QRDecomposition(matrix).getH()); matrix = MatrixUtils.createRealMatrix(testData3x4); checkTrapezoidal(new QRDecomposition(matrix).getH()); matrix = MatrixUtils.createRealMatrix(testData4x3); checkTrapezoidal(new QRDecomposition(matrix).getH()); Random r = new Random(643895747384642l); int p = (5 * BlockRealMatrix.BLOCK_SIZE) / 4; int q = (7 * BlockRealMatrix.BLOCK_SIZE) / 4; matrix = createTestMatrix(r, p, q); checkTrapezoidal(new QRDecomposition(matrix).getH()); matrix = createTestMatrix(r, p, q); checkTrapezoidal(new QRDecomposition(matrix).getH()); } private void checkTrapezoidal(RealMatrix m) { m.walkInOptimizedOrder(new DefaultRealMatrixPreservingVisitor() { @Override public void visit(int row, int column, double value) { if (column > row) { Assert.assertEquals(0.0, value, entryTolerance); } } }); } /** test matrices values */ @Test public void testMatricesValues() { QRDecomposition qr = new QRDecomposition(MatrixUtils.createRealMatrix(testData3x3NonSingular)); RealMatrix qRef = MatrixUtils.createRealMatrix(new double[][] { { -12.0 / 14.0, 69.0 / 175.0, -58.0 / 175.0 }, { -6.0 / 14.0, -158.0 / 175.0, 6.0 / 175.0 }, { 4.0 / 14.0, -30.0 / 175.0, -165.0 / 175.0 } }); RealMatrix rRef = MatrixUtils.createRealMatrix(new double[][] { { -14.0, -21.0, 14.0 }, { 0.0, -175.0, 70.0 }, { 0.0, 0.0, 35.0 } }); RealMatrix hRef = MatrixUtils.createRealMatrix(new double[][] { { 26.0 / 14.0, 0.0, 0.0 }, { 6.0 / 14.0, 648.0 / 325.0, 0.0 }, { -4.0 / 14.0, 36.0 / 325.0, 2.0 } }); // check values against known references RealMatrix q = qr.getQ(); Assert.assertEquals(0, q.subtract(qRef).getNorm(), 1.0e-13); RealMatrix qT = qr.getQT(); Assert.assertEquals(0, qT.subtract(qRef.transpose()).getNorm(), 1.0e-13); RealMatrix r = qr.getR(); Assert.assertEquals(0, r.subtract(rRef).getNorm(), 1.0e-13); RealMatrix h = qr.getH(); Assert.assertEquals(0, h.subtract(hRef).getNorm(), 1.0e-13); // check the same cached instance is returned the second time Assert.assertTrue(q == qr.getQ()); Assert.assertTrue(r == qr.getR()); Assert.assertTrue(h == qr.getH()); } @Test(expected=SingularMatrixException.class) public void testNonInvertible() { QRDecomposition qr = new QRDecomposition(MatrixUtils.createRealMatrix(testData3x3Singular)); qr.getSolver().getInverse(); } private RealMatrix createTestMatrix(final Random r, final int rows, final int columns) { RealMatrix m = MatrixUtils.createRealMatrix(rows, columns); m.walkInOptimizedOrder(new DefaultRealMatrixChangingVisitor(){ @Override public double visit(int row, int column, double value) { return 2.0 * r.nextDouble() - 1.0; } }); return m; } } commons-math3-3.2-src/src/test/java/org/apache/commons/math3/linear/SymmLQTest.java100644 1750 1750 60216 12126627674 26750 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.linear; import java.util.Arrays; import org.apache.commons.math3.exception.DimensionMismatchException; import org.apache.commons.math3.util.FastMath; import org.apache.commons.math3.util.IterationEvent; import org.apache.commons.math3.util.IterationListener; import org.junit.Assert; import org.junit.Test; public class SymmLQTest { public void saundersTest(final int n, final boolean goodb, final boolean precon, final double shift, final double pertbn) { final RealLinearOperator a = new RealLinearOperator() { @Override public RealVector operate(final RealVector x) { if (x.getDimension() != n) { throw new DimensionMismatchException(x.getDimension(), n); } final double[] y = new double[n]; for (int i = 0; i < n; i++) { y[i] = (i + 1) * 1.1 / n * x.getEntry(i); } return new ArrayRealVector(y, false); } @Override public int getRowDimension() { return n; } @Override public int getColumnDimension() { return n; } }; final double shiftm = shift; final double pertm = FastMath.abs(pertbn); final RealLinearOperator minv; if (precon) { minv = new RealLinearOperator() { @Override public int getRowDimension() { return n; } @Override public int getColumnDimension() { return n; } @Override public RealVector operate(final RealVector x) { if (x.getDimension() != n) { throw new DimensionMismatchException(x.getDimension(), n); } final double[] y = new double[n]; for (int i = 0; i < n; i++) { double d = (i + 1) * 1.1 / n; d = FastMath.abs(d - shiftm); if (i % 10 == 0) { d += pertm; } y[i] = x.getEntry(i) / d; } return new ArrayRealVector(y, false); } }; } else { minv = null; } final RealVector xtrue = new ArrayRealVector(n); for (int i = 0; i < n; i++) { xtrue.setEntry(i, n - i); } final RealVector b = a.operate(xtrue); b.combineToSelf(1.0, -shift, xtrue); final SymmLQ solver = new SymmLQ(2 * n, 1E-12, true); final RealVector x = solver.solve(a, minv, b, goodb, shift); final RealVector y = a.operate(x); final RealVector r1 = new ArrayRealVector(n); for (int i = 0; i < n; i++) { final double bi = b.getEntry(i); final double yi = y.getEntry(i); final double xi = x.getEntry(i); r1.setEntry(i, bi - yi + shift * xi); } final double enorm = x.subtract(xtrue).getNorm() / xtrue.getNorm(); final double etol = 1E-5; Assert.assertTrue("enorm=" + enorm + ", " + solver.getIterationManager().getIterations(), enorm <= etol); } @Test public void testSolveSaunders1() { saundersTest(1, false, false, 0., 0.); } @Test public void testSolveSaunders2() { saundersTest(2, false, false, 0., 0.); } @Test public void testSolveSaunders3() { saundersTest(1, false, true, 0., 0.); } @Test public void testSolveSaunders4() { saundersTest(2, false, true, 0., 0.); } @Test public void testSolveSaunders5() { saundersTest(5, false, true, 0., 0.); } @Test public void testSolveSaunders6() { saundersTest(5, false, true, 0.25, 0.); } @Test public void testSolveSaunders7() { saundersTest(50, false, false, 0., 0.); } @Test public void testSolveSaunders8() { saundersTest(50, false, false, 0.25, 0.); } @Test public void testSolveSaunders9() { saundersTest(50, false, true, 0., 0.10); } @Test public void testSolveSaunders10() { saundersTest(50, false, true, 0.25, 0.10); } @Test public void testSolveSaunders11() { saundersTest(1, true, false, 0., 0.); } @Test public void testSolveSaunders12() { saundersTest(2, true, false, 0., 0.); } @Test public void testSolveSaunders13() { saundersTest(1, true, true, 0., 0.); } @Test public void testSolveSaunders14() { saundersTest(2, true, true, 0., 0.); } @Test public void testSolveSaunders15() { saundersTest(5, true, true, 0., 0.); } @Test public void testSolveSaunders16() { saundersTest(5, true, true, 0.25, 0.); } @Test public void testSolveSaunders17() { saundersTest(50, true, false, 0., 0.); } @Test public void testSolveSaunders18() { saundersTest(50, true, false, 0.25, 0.); } @Test public void testSolveSaunders19() { saundersTest(50, true, true, 0., 0.10); } @Test public void testSolveSaunders20() { saundersTest(50, true, true, 0.25, 0.10); } @Test(expected = NonSquareOperatorException.class) public void testNonSquareOperator() { final Array2DRowRealMatrix a = new Array2DRowRealMatrix(2, 3); final IterativeLinearSolver solver; solver = new SymmLQ(10, 0., false); final ArrayRealVector b = new ArrayRealVector(a.getRowDimension()); final ArrayRealVector x = new ArrayRealVector(a.getColumnDimension()); solver.solve(a, b, x); } @Test(expected = DimensionMismatchException.class) public void testDimensionMismatchRightHandSide() { final Array2DRowRealMatrix a = new Array2DRowRealMatrix(3, 3); final IterativeLinearSolver solver; solver = new SymmLQ(10, 0., false); final ArrayRealVector b = new ArrayRealVector(2); solver.solve(a, b); } @Test(expected = DimensionMismatchException.class) public void testDimensionMismatchSolution() { final Array2DRowRealMatrix a = new Array2DRowRealMatrix(3, 3); final IterativeLinearSolver solver; solver = new SymmLQ(10, 0., false); final ArrayRealVector b = new ArrayRealVector(3); final ArrayRealVector x = new ArrayRealVector(2); solver.solve(a, b, x); } @Test public void testUnpreconditionedSolution() { final int n = 5; final int maxIterations = 100; final RealLinearOperator a = new HilbertMatrix(n); final InverseHilbertMatrix ainv = new InverseHilbertMatrix(n); final IterativeLinearSolver solver; solver = new SymmLQ(maxIterations, 1E-10, true); final RealVector b = new ArrayRealVector(n); for (int j = 0; j < n; j++) { b.set(0.); b.setEntry(j, 1.); final RealVector x = solver.solve(a, b); for (int i = 0; i < n; i++) { final double actual = x.getEntry(i); final double expected = ainv.getEntry(i, j); final double delta = 1E-6 * Math.abs(expected); final String msg = String.format("entry[%d][%d]", i, j); Assert.assertEquals(msg, expected, actual, delta); } } } @Test public void testUnpreconditionedInPlaceSolutionWithInitialGuess() { final int n = 5; final int maxIterations = 100; final RealLinearOperator a = new HilbertMatrix(n); final InverseHilbertMatrix ainv = new InverseHilbertMatrix(n); final IterativeLinearSolver solver; solver = new SymmLQ(maxIterations, 1E-10, true); final RealVector b = new ArrayRealVector(n); for (int j = 0; j < n; j++) { b.set(0.); b.setEntry(j, 1.); final RealVector x0 = new ArrayRealVector(n); x0.set(1.); final RealVector x = solver.solveInPlace(a, b, x0); Assert.assertSame("x should be a reference to x0", x0, x); for (int i = 0; i < n; i++) { final double actual = x.getEntry(i); final double expected = ainv.getEntry(i, j); final double delta = 1E-6 * Math.abs(expected); final String msg = String.format("entry[%d][%d)", i, j); Assert.assertEquals(msg, expected, actual, delta); } } } @Test public void testUnpreconditionedSolutionWithInitialGuess() { final int n = 5; final int maxIterations = 100; final RealLinearOperator a = new HilbertMatrix(n); final InverseHilbertMatrix ainv = new InverseHilbertMatrix(n); final IterativeLinearSolver solver; solver = new SymmLQ(maxIterations, 1E-10, true); final RealVector b = new ArrayRealVector(n); for (int j = 0; j < n; j++) { b.set(0.); b.setEntry(j, 1.); final RealVector x0 = new ArrayRealVector(n); x0.set(1.); final RealVector x = solver.solve(a, b, x0); Assert.assertNotSame("x should not be a reference to x0", x0, x); for (int i = 0; i < n; i++) { final double actual = x.getEntry(i); final double expected = ainv.getEntry(i, j); final double delta = 1E-6 * Math.abs(expected); final String msg = String.format("entry[%d][%d]", i, j); Assert.assertEquals(msg, expected, actual, delta); Assert.assertEquals(msg, x0.getEntry(i), 1., Math.ulp(1.)); } } } @Test(expected = NonSquareOperatorException.class) public void testNonSquarePreconditioner() { final Array2DRowRealMatrix a = new Array2DRowRealMatrix(2, 2); final RealLinearOperator m = new RealLinearOperator() { @Override public RealVector operate(final RealVector x) { throw new UnsupportedOperationException(); } @Override public int getRowDimension() { return 2; } @Override public int getColumnDimension() { return 3; } }; final PreconditionedIterativeLinearSolver solver; solver = new SymmLQ(10, 0., false); final ArrayRealVector b = new ArrayRealVector(a.getRowDimension()); solver.solve(a, m, b); } @Test(expected = DimensionMismatchException.class) public void testMismatchedOperatorDimensions() { final Array2DRowRealMatrix a = new Array2DRowRealMatrix(2, 2); final RealLinearOperator m = new RealLinearOperator() { @Override public RealVector operate(final RealVector x) { throw new UnsupportedOperationException(); } @Override public int getRowDimension() { return 3; } @Override public int getColumnDimension() { return 3; } }; final PreconditionedIterativeLinearSolver solver; solver = new SymmLQ(10, 0d, false); final ArrayRealVector b = new ArrayRealVector(a.getRowDimension()); solver.solve(a, m, b); } @Test(expected = NonPositiveDefiniteOperatorException.class) public void testNonPositiveDefinitePreconditioner() { final Array2DRowRealMatrix a = new Array2DRowRealMatrix(2, 2); a.setEntry(0, 0, 1d); a.setEntry(0, 1, 2d); a.setEntry(1, 0, 3d); a.setEntry(1, 1, 4d); final RealLinearOperator m = new RealLinearOperator() { @Override public RealVector operate(final RealVector x) { final ArrayRealVector y = new ArrayRealVector(2); y.setEntry(0, -x.getEntry(0)); y.setEntry(1, -x.getEntry(1)); return y; } @Override public int getRowDimension() { return 2; } @Override public int getColumnDimension() { return 2; } }; final PreconditionedIterativeLinearSolver solver; solver = new SymmLQ(10, 0d, true); final ArrayRealVector b = new ArrayRealVector(2); b.setEntry(0, -1d); b.setEntry(1, -1d); solver.solve(a, m, b); } @Test public void testPreconditionedSolution() { final int n = 8; final int maxIterations = 100; final RealLinearOperator a = new HilbertMatrix(n); final InverseHilbertMatrix ainv = new InverseHilbertMatrix(n); final RealLinearOperator m = JacobiPreconditioner.create(a); final PreconditionedIterativeLinearSolver solver; solver = new SymmLQ(maxIterations, 1E-15, true); final RealVector b = new ArrayRealVector(n); for (int j = 0; j < n; j++) { b.set(0.); b.setEntry(j, 1.); final RealVector x = solver.solve(a, m, b); for (int i = 0; i < n; i++) { final double actual = x.getEntry(i); final double expected = ainv.getEntry(i, j); final double delta = 1E-6 * Math.abs(expected); final String msg = String.format("coefficient (%d, %d)", i, j); Assert.assertEquals(msg, expected, actual, delta); } } } @Test public void testPreconditionedSolution2() { final int n = 100; final int maxIterations = 100000; final Array2DRowRealMatrix a = new Array2DRowRealMatrix(n, n); double daux = 1.; for (int i = 0; i < n; i++) { a.setEntry(i, i, daux); daux *= 1.2; for (int j = i + 1; j < n; j++) { if (i == j) { } else { final double value = 1.0; a.setEntry(i, j, value); a.setEntry(j, i, value); } } } final RealLinearOperator m = JacobiPreconditioner.create(a); final PreconditionedIterativeLinearSolver prec; final IterativeLinearSolver unprec; prec = new SymmLQ(maxIterations, 1E-15, true); unprec = new SymmLQ(maxIterations, 1E-15, true); final RealVector b = new ArrayRealVector(n); final String pattern = "preconditioned SymmLQ (%d iterations) should" + " have been faster than unpreconditioned (%d iterations)"; String msg; for (int j = 0; j < 1; j++) { b.set(0.); b.setEntry(j, 1.); final RealVector px = prec.solve(a, m, b); final RealVector x = unprec.solve(a, b); final int np = prec.getIterationManager().getIterations(); final int nup = unprec.getIterationManager().getIterations(); msg = String.format(pattern, np, nup); for (int i = 0; i < n; i++) { msg = String.format("row %d, column %d", i, j); final double expected = x.getEntry(i); final double actual = px.getEntry(i); final double delta = 5E-5 * Math.abs(expected); Assert.assertEquals(msg, expected, actual, delta); } } } @Test public void testEventManagement() { final int n = 5; final int maxIterations = 100; final RealLinearOperator a = new HilbertMatrix(n); final IterativeLinearSolver solver; /* * count[0] = number of calls to initializationPerformed * count[1] = number of calls to iterationStarted * count[2] = number of calls to iterationPerformed * count[3] = number of calls to terminationPerformed */ final int[] count = new int[] {0, 0, 0, 0}; final RealVector xFromListener = new ArrayRealVector(n); final IterationListener listener = new IterationListener() { public void initializationPerformed(final IterationEvent e) { ++count[0]; } public void iterationPerformed(final IterationEvent e) { ++count[2]; Assert.assertEquals("iteration performed", count[2], e.getIterations() - 1); } public void iterationStarted(final IterationEvent e) { ++count[1]; Assert.assertEquals("iteration started", count[1], e.getIterations() - 1); } public void terminationPerformed(final IterationEvent e) { ++count[3]; final IterativeLinearSolverEvent ilse; ilse = (IterativeLinearSolverEvent) e; xFromListener.setSubVector(0, ilse.getSolution()); } }; solver = new SymmLQ(maxIterations, 1E-10, true); solver.getIterationManager().addIterationListener(listener); final RealVector b = new ArrayRealVector(n); for (int j = 0; j < n; j++) { Arrays.fill(count, 0); b.set(0.); b.setEntry(j, 1.); final RealVector xFromSolver = solver.solve(a, b); String msg = String.format("column %d (initialization)", j); Assert.assertEquals(msg, 1, count[0]); msg = String.format("column %d (finalization)", j); Assert.assertEquals(msg, 1, count[3]); /* * Check that solution is not "over-refined". When the last * iteration has occurred, no further refinement should be * performed. */ for (int i = 0; i < n; i++){ msg = String.format("row %d, column %d", i, j); final double expected = xFromSolver.getEntry(i); final double actual = xFromListener.getEntry(i); Assert.assertEquals(msg, expected, actual, 0.0); } } } @Test(expected = NonSelfAdjointOperatorException.class) public void testNonSelfAdjointOperator() { final RealLinearOperator a; a = new Array2DRowRealMatrix(new double[][] { {1., 2., 3.}, {2., 4., 5.}, {2.999, 5., 6.} }); final RealVector b; b = new ArrayRealVector(new double[] {1., 1., 1.}); new SymmLQ(100, 1., true).solve(a, b); } @Test(expected = NonSelfAdjointOperatorException.class) public void testNonSelfAdjointPreconditioner() { final RealLinearOperator a = new Array2DRowRealMatrix(new double[][] { {1., 2., 3.}, {2., 4., 5.}, {3., 5., 6.} }); final Array2DRowRealMatrix mMat; mMat = new Array2DRowRealMatrix(new double[][] { {1., 0., 1.}, {0., 1., 0.}, {0., 0., 1.} }); final DecompositionSolver mSolver; mSolver = new LUDecomposition(mMat).getSolver(); final RealLinearOperator minv = new RealLinearOperator() { @Override public RealVector operate(final RealVector x) { return mSolver.solve(x); } @Override public int getRowDimension() { return mMat.getRowDimension(); } @Override public int getColumnDimension() { return mMat.getColumnDimension(); } }; final RealVector b = new ArrayRealVector(new double[] { 1., 1., 1. }); new SymmLQ(100, 1., true).solve(a, minv, b); } @Test public void testUnpreconditionedNormOfResidual() { final int n = 5; final int maxIterations = 100; final RealLinearOperator a = new HilbertMatrix(n); final IterativeLinearSolver solver; final IterationListener listener = new IterationListener() { private void doTestNormOfResidual(final IterationEvent e) { final IterativeLinearSolverEvent evt; evt = (IterativeLinearSolverEvent) e; final RealVector x = evt.getSolution(); final RealVector b = evt.getRightHandSideVector(); final RealVector r = b.subtract(a.operate(x)); final double rnorm = r.getNorm(); Assert.assertEquals("iteration performed (residual)", rnorm, evt.getNormOfResidual(), FastMath.max(1E-5 * rnorm, 1E-10)); } public void initializationPerformed(final IterationEvent e) { doTestNormOfResidual(e); } public void iterationPerformed(final IterationEvent e) { doTestNormOfResidual(e); } public void iterationStarted(final IterationEvent e) { doTestNormOfResidual(e); } public void terminationPerformed(final IterationEvent e) { doTestNormOfResidual(e); } }; solver = new SymmLQ(maxIterations, 1E-10, true); solver.getIterationManager().addIterationListener(listener); final RealVector b = new ArrayRealVector(n); for (int j = 0; j < n; j++) { b.set(0.); b.setEntry(j, 1.); solver.solve(a, b); } } @Test public void testPreconditionedNormOfResidual() { final int n = 5; final int maxIterations = 100; final RealLinearOperator a = new HilbertMatrix(n); final JacobiPreconditioner m = JacobiPreconditioner.create(a); final RealLinearOperator p = m.sqrt(); final PreconditionedIterativeLinearSolver solver; final IterationListener listener = new IterationListener() { private void doTestNormOfResidual(final IterationEvent e) { final IterativeLinearSolverEvent evt; evt = (IterativeLinearSolverEvent) e; final RealVector x = evt.getSolution(); final RealVector b = evt.getRightHandSideVector(); final RealVector r = b.subtract(a.operate(x)); final double rnorm = p.operate(r).getNorm(); Assert.assertEquals("iteration performed (residual)", rnorm, evt.getNormOfResidual(), FastMath.max(1E-5 * rnorm, 1E-10)); } public void initializationPerformed(final IterationEvent e) { doTestNormOfResidual(e); } public void iterationPerformed(final IterationEvent e) { doTestNormOfResidual(e); } public void iterationStarted(final IterationEvent e) { doTestNormOfResidual(e); } public void terminationPerformed(final IterationEvent e) { doTestNormOfResidual(e); } }; solver = new SymmLQ(maxIterations, 1E-10, true); solver.getIterationManager().addIterationListener(listener); final RealVector b = new ArrayRealVector(n); for (int j = 0; j < n; j++) { b.set(0.); b.setEntry(j, 1.); solver.solve(a, m, b); } } } commons-math3-3.2-src/src/test/java/org/apache/commons/math3/linear/SparseRealVectorTest.java100644 1750 1750 10744 12126627674 31013 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.linear; import org.junit.Assert; import org.junit.Ignore; import org.junit.Test; /** * Test cases for the {@link OpenMapRealVector} class. * * @version $Id: SparseRealVectorTest.java 1361170 2012-07-13 11:37:40Z celestin $ */ public class SparseRealVectorTest extends RealVectorAbstractTest { @Override public RealVector create(double[] data) { return new OpenMapRealVector(data); } @Test public void testConstructors() { final double[] vec1 = {1d, 2d, 3d}; final Double[] dvec1 = {1d, 2d, 3d, 4d, 5d, 6d, 7d, 8d, 9d}; OpenMapRealVector v0 = new OpenMapRealVector(); Assert.assertEquals("testData len", 0, v0.getDimension()); OpenMapRealVector v1 = new OpenMapRealVector(7); Assert.assertEquals("testData len", 7, v1.getDimension()); Assert.assertEquals("testData is 0.0 ", 0.0, v1.getEntry(6), 0); OpenMapRealVector v3 = new OpenMapRealVector(vec1); Assert.assertEquals("testData len", 3, v3.getDimension()); Assert.assertEquals("testData is 2.0 ", 2.0, v3.getEntry(1), 0); //SparseRealVector v4 = new SparseRealVector(vec4, 3, 2); //Assert.assertEquals("testData len", 2, v4.getDimension()); //Assert.assertEquals("testData is 4.0 ", 4.0, v4.getEntry(0)); //try { // new SparseRealVector(vec4, 8, 3); // Assert.fail("MathIllegalArgumentException expected"); //} catch (MathIllegalArgumentException ex) { // expected behavior //} RealVector v5_i = new OpenMapRealVector(dvec1); Assert.assertEquals("testData len", 9, v5_i.getDimension()); Assert.assertEquals("testData is 9.0 ", 9.0, v5_i.getEntry(8), 0); OpenMapRealVector v5 = new OpenMapRealVector(dvec1); Assert.assertEquals("testData len", 9, v5.getDimension()); Assert.assertEquals("testData is 9.0 ", 9.0, v5.getEntry(8), 0); OpenMapRealVector v7 = new OpenMapRealVector(v1); Assert.assertEquals("testData len", 7, v7.getDimension()); Assert.assertEquals("testData is 0.0 ", 0.0, v7.getEntry(6), 0); RealVectorTestImpl v7_i = new RealVectorTestImpl(vec1); OpenMapRealVector v7_2 = new OpenMapRealVector(v7_i); Assert.assertEquals("testData len", 3, v7_2.getDimension()); Assert.assertEquals("testData is 0.0 ", 2.0d, v7_2.getEntry(1), 0); OpenMapRealVector v8 = new OpenMapRealVector(v1); Assert.assertEquals("testData len", 7, v8.getDimension()); Assert.assertEquals("testData is 0.0 ", 0.0, v8.getEntry(6), 0); } /* Check that the operations do not throw an exception (cf. MATH-645). */ @Test public void testConcurrentModification() { final RealVector u = new OpenMapRealVector(3, 1e-6); u.setEntry(0, 1); u.setEntry(1, 0); u.setEntry(2, 2); final RealVector v1 = new OpenMapRealVector(3, 1e-6); v1.setEntry(0, 0); v1.setEntry(1, 3); v1.setEntry(2, 0); u.ebeMultiply(v1); u.ebeDivide(v1); } /** * XXX This test is disabled because it currently fails. * The bug must still be fixed in the sparse vector implementation. * When this is done, this override should be deleted. */ @Test @Override @Ignore("This test is skipped until MATH-821 is fixed") public void testMap() {} /** * XXX This test is disabled because it currently fails. * The bug must still be fixed in the sparse vector implementation. * When this is done, this override should be deleted. */ @Test @Override @Ignore("This test is skipped until MATH-821 is fixed") public void testMapToSelf() {} } commons-math3-3.2-src/src/test/java/org/apache/commons/math3/linear/FieldLUSolverTest.java100644 1750 1750 15126 12126627674 30245 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.linear; import org.apache.commons.math3.exception.MathIllegalArgumentException; import org.apache.commons.math3.fraction.Fraction; import org.apache.commons.math3.fraction.FractionField; import org.junit.Assert; import org.junit.Test; public class FieldLUSolverTest { private int[][] testData = { { 1, 2, 3}, { 2, 5, 3}, { 1, 0, 8} }; private int[][] luData = { { 2, 3, 3 }, { 0, 5, 7 }, { 6, 9, 8 } }; // singular matrices private int[][] singular = { { 2, 3 }, { 2, 3 } }; private int[][] bigSingular = { { 1, 2, 3, 4 }, { 2, 5, 3, 4 }, { 7, 3, 256, 1930 }, { 3, 7, 6, 8 } }; // 4th row = 1st + 2nd public static FieldMatrix createFractionMatrix(final int[][] data) { final int numRows = data.length; final int numCols = data[0].length; final Array2DRowFieldMatrix m; m = new Array2DRowFieldMatrix(FractionField.getInstance(), numRows, numCols); for (int i = 0; i < numRows; i++) { for (int j = 0; j < numCols; j++) { m.setEntry(i, j, new Fraction(data[i][j], 1)); } } return m; } /** test singular */ @Test public void testSingular() { FieldDecompositionSolver solver; solver = new FieldLUDecomposition(createFractionMatrix(testData)) .getSolver(); Assert.assertTrue(solver.isNonSingular()); solver = new FieldLUDecomposition(createFractionMatrix(singular)) .getSolver(); Assert.assertFalse(solver.isNonSingular()); solver = new FieldLUDecomposition(createFractionMatrix(bigSingular)) .getSolver(); Assert.assertFalse(solver.isNonSingular()); } /** test solve dimension errors */ @Test public void testSolveDimensionErrors() { FieldDecompositionSolver solver; solver = new FieldLUDecomposition(createFractionMatrix(testData)) .getSolver(); FieldMatrix b = createFractionMatrix(new int[2][2]); try { solver.solve(b); Assert.fail("an exception should have been thrown"); } catch (MathIllegalArgumentException iae) { // expected behavior } try { solver.solve(b.getColumnVector(0)); Assert.fail("an exception should have been thrown"); } catch (MathIllegalArgumentException iae) { // expected behavior } } /** test solve singularity errors */ @Test public void testSolveSingularityErrors() { FieldDecompositionSolver solver; solver = new FieldLUDecomposition(createFractionMatrix(singular)) .getSolver(); FieldMatrix b = createFractionMatrix(new int[2][2]); try { solver.solve(b); Assert.fail("an exception should have been thrown"); } catch (SingularMatrixException ime) { // expected behavior } try { solver.solve(b.getColumnVector(0)); Assert.fail("an exception should have been thrown"); } catch (SingularMatrixException ime) { // expected behavior } } /** test solve */ @Test public void testSolve() { FieldDecompositionSolver solver; solver = new FieldLUDecomposition(createFractionMatrix(testData)) .getSolver(); FieldMatrix b = createFractionMatrix(new int[][] { { 1, 0 }, { 2, -5 }, { 3, 1 } }); FieldMatrix xRef = createFractionMatrix(new int[][] { { 19, -71 }, { -6, 22 }, { -2, 9 } }); // using FieldMatrix FieldMatrix x = solver.solve(b); for (int i = 0; i < x.getRowDimension(); i++){ for (int j = 0; j < x.getColumnDimension(); j++){ Assert.assertEquals("(" + i + ", " + j + ")", xRef.getEntry(i, j), x.getEntry(i, j)); } } // using ArrayFieldVector for (int j = 0; j < b.getColumnDimension(); j++) { final FieldVector xj = solver.solve(b.getColumnVector(j)); for (int i = 0; i < xj.getDimension(); i++){ Assert.assertEquals("(" + i + ", " + j + ")", xRef.getEntry(i, j), xj.getEntry(i)); } } // using SparseFieldVector for (int j = 0; j < b.getColumnDimension(); j++) { final SparseFieldVector bj; bj = new SparseFieldVector(FractionField.getInstance(), b.getColumn(j)); final FieldVector xj = solver.solve(bj); for (int i = 0; i < xj.getDimension(); i++) { Assert.assertEquals("(" + i + ", " + j + ")", xRef.getEntry(i, j), xj.getEntry(i)); } } } /** test determinant */ @Test public void testDeterminant() { Assert.assertEquals( -1, getDeterminant(createFractionMatrix(testData)), 1E-15); Assert.assertEquals(-10, getDeterminant(createFractionMatrix(luData)), 1E-14); Assert.assertEquals( 0, getDeterminant(createFractionMatrix(singular)), 1E-15); Assert.assertEquals( 0, getDeterminant(createFractionMatrix(bigSingular)), 1E-15); } private double getDeterminant(final FieldMatrix m) { return new FieldLUDecomposition(m).getDeterminant().doubleValue(); } } commons-math3-3.2-src/src/test/java/org/apache/commons/math3/linear/HessenbergTransformerTest.java100644 1750 1750 21445 12126627674 32077 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.linear; import java.util.Random; import org.apache.commons.math3.distribution.NormalDistribution; import org.junit.Test; import org.junit.Assert; public class HessenbergTransformerTest { private double[][] testSquare5 = { { 5, 4, 3, 2, 1 }, { 1, 4, 0, 3, 3 }, { 2, 0, 3, 0, 0 }, { 3, 2, 1, 2, 5 }, { 4, 2, 1, 4, 1 } }; private double[][] testSquare3 = { { 2, -1, 1 }, { -1, 2, 1 }, { 1, -1, 2 } }; // from http://eigen.tuxfamily.org/dox/classEigen_1_1HessenbergDecomposition.html private double[][] testRandom = { { 0.680, 0.823, -0.4440, -0.2700 }, { -0.211, -0.605, 0.1080, 0.0268 }, { 0.566, -0.330, -0.0452, 0.9040 }, { 0.597, 0.536, 0.2580, 0.8320 } }; @Test public void testNonSquare() { try { new HessenbergTransformer(MatrixUtils.createRealMatrix(new double[3][2])); Assert.fail("an exception should have been thrown"); } catch (NonSquareMatrixException ime) { // expected behavior } } @Test public void testAEqualPHPt() { checkAEqualPHPt(MatrixUtils.createRealMatrix(testSquare5)); checkAEqualPHPt(MatrixUtils.createRealMatrix(testSquare3)); checkAEqualPHPt(MatrixUtils.createRealMatrix(testRandom)); } @Test public void testPOrthogonal() { checkOrthogonal(new HessenbergTransformer(MatrixUtils.createRealMatrix(testSquare5)).getP()); checkOrthogonal(new HessenbergTransformer(MatrixUtils.createRealMatrix(testSquare3)).getP()); } @Test public void testPTOrthogonal() { checkOrthogonal(new HessenbergTransformer(MatrixUtils.createRealMatrix(testSquare5)).getPT()); checkOrthogonal(new HessenbergTransformer(MatrixUtils.createRealMatrix(testSquare3)).getPT()); } @Test public void testHessenbergForm() { checkHessenbergForm(new HessenbergTransformer(MatrixUtils.createRealMatrix(testSquare5)).getH()); checkHessenbergForm(new HessenbergTransformer(MatrixUtils.createRealMatrix(testSquare3)).getH()); } @Test public void testRandomData() { for (int run = 0; run < 100; run++) { Random r = new Random(System.currentTimeMillis()); // matrix size int size = r.nextInt(20) + 4; double[][] data = new double[size][size]; for (int i = 0; i < size; i++) { for (int j = 0; j < size; j++) { data[i][j] = r.nextInt(100); } } RealMatrix m = MatrixUtils.createRealMatrix(data); RealMatrix h = checkAEqualPHPt(m); checkHessenbergForm(h); } } @Test public void testRandomDataNormalDistribution() { for (int run = 0; run < 100; run++) { Random r = new Random(System.currentTimeMillis()); NormalDistribution dist = new NormalDistribution(0.0, r.nextDouble() * 5); // matrix size int size = r.nextInt(20) + 4; double[][] data = new double[size][size]; for (int i = 0; i < size; i++) { for (int j = 0; j < size; j++) { data[i][j] = dist.sample(); } } RealMatrix m = MatrixUtils.createRealMatrix(data); RealMatrix h = checkAEqualPHPt(m); checkHessenbergForm(h); } } @Test public void testMatricesValues5() { checkMatricesValues(testSquare5, new double[][] { { 1.0, 0.0, 0.0, 0.0, 0.0 }, { 0.0, -0.182574185835055, 0.784218758628863, 0.395029040913988, -0.442289115981669 }, { 0.0, -0.365148371670111, -0.337950625265477, -0.374110794088820, -0.782621974707823 }, { 0.0, -0.547722557505166, 0.402941130124223, -0.626468266309003, 0.381019628053472 }, { 0.0, -0.730296743340221, -0.329285224617644, 0.558149336547665, 0.216118545309225 } }, new double[][] { { 5.0, -3.65148371670111, 2.59962019434982, -0.237003414680848, -3.13886458663398 }, { -5.47722557505166, 6.9, -2.29164066120599, 0.207283564429169, 0.703858369151728 }, { 0.0, -4.21386600008432, 2.30555659846067, 2.74935928725112, 0.857569835914113 }, { 0.0, 0.0, 2.86406180891882, -1.11582249161595, 0.817995267184158 }, { 0.0, 0.0, 0.0, 0.683518597386085, 1.91026589315528 } }); } @Test public void testMatricesValues3() { checkMatricesValues(testSquare3, new double[][] { { 1.0, 0.0, 0.0 }, { 0.0, -0.707106781186547, 0.707106781186547 }, { 0.0, 0.707106781186547, 0.707106781186548 }, }, new double[][] { { 2.0, 1.41421356237309, 0.0 }, { 1.41421356237310, 2.0, -1.0 }, { 0.0, 1.0, 2.0 }, }); } /////////////////////////////////////////////////////////////////////////// // Test helpers /////////////////////////////////////////////////////////////////////////// private RealMatrix checkAEqualPHPt(RealMatrix matrix) { HessenbergTransformer transformer = new HessenbergTransformer(matrix); RealMatrix p = transformer.getP(); RealMatrix pT = transformer.getPT(); RealMatrix h = transformer.getH(); RealMatrix result = p.multiply(h).multiply(pT); double norm = result.subtract(matrix).getNorm(); Assert.assertEquals(0, norm, 1.0e-10); for (int i = 0; i < matrix.getRowDimension(); ++i) { for (int j = 0; j < matrix.getColumnDimension(); ++j) { if (i > j + 1) { Assert.assertEquals(matrix.getEntry(i, j), result.getEntry(i, j), 1.0e-12); } } } return transformer.getH(); } private void checkOrthogonal(RealMatrix m) { RealMatrix mTm = m.transpose().multiply(m); RealMatrix id = MatrixUtils.createRealIdentityMatrix(mTm.getRowDimension()); Assert.assertEquals(0, mTm.subtract(id).getNorm(), 1.0e-14); } private void checkHessenbergForm(RealMatrix m) { final int rows = m.getRowDimension(); final int cols = m.getColumnDimension(); for (int i = 0; i < rows; ++i) { for (int j = 0; j < cols; ++j) { if (i > j + 1) { Assert.assertEquals(0, m.getEntry(i, j), 1.0e-16); } } } } private void checkMatricesValues(double[][] matrix, double[][] pRef, double[][] hRef) { HessenbergTransformer transformer = new HessenbergTransformer(MatrixUtils.createRealMatrix(matrix)); // check values against known references RealMatrix p = transformer.getP(); Assert.assertEquals(0, p.subtract(MatrixUtils.createRealMatrix(pRef)).getNorm(), 1.0e-14); RealMatrix h = transformer.getH(); Assert.assertEquals(0, h.subtract(MatrixUtils.createRealMatrix(hRef)).getNorm(), 1.0e-14); // check the same cached instance is returned the second time Assert.assertTrue(p == transformer.getP()); Assert.assertTrue(h == transformer.getH()); } } commons-math3-3.2-src/src/test/java/org/apache/commons/math3/linear/FieldLUDecompositionTest.java100644 1750 1750 32277 12126627674 31615 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.linear; import org.junit.Test; import org.junit.Assert; import org.apache.commons.math3.TestUtils; import org.apache.commons.math3.fraction.Fraction; import org.apache.commons.math3.fraction.FractionField; public class FieldLUDecompositionTest { private Fraction[][] testData = { { new Fraction(1), new Fraction(2), new Fraction(3)}, { new Fraction(2), new Fraction(5), new Fraction(3)}, { new Fraction(1), new Fraction(0), new Fraction(8)} }; private Fraction[][] testDataMinus = { { new Fraction(-1), new Fraction(-2), new Fraction(-3)}, { new Fraction(-2), new Fraction(-5), new Fraction(-3)}, { new Fraction(-1), new Fraction(0), new Fraction(-8)} }; private Fraction[][] luData = { { new Fraction(2), new Fraction(3), new Fraction(3) }, { new Fraction(2), new Fraction(3), new Fraction(7) }, { new Fraction(6), new Fraction(6), new Fraction(8) } }; // singular matrices private Fraction[][] singular = { { new Fraction(2), new Fraction(3) }, { new Fraction(2), new Fraction(3) } }; private Fraction[][] bigSingular = { { new Fraction(1), new Fraction(2), new Fraction(3), new Fraction(4) }, { new Fraction(2), new Fraction(5), new Fraction(3), new Fraction(4) }, { new Fraction(7), new Fraction(3), new Fraction(256), new Fraction(1930) }, { new Fraction(3), new Fraction(7), new Fraction(6), new Fraction(8) } }; // 4th row = 1st + 2nd /** test dimensions */ @Test public void testDimensions() { FieldMatrix matrix = new Array2DRowFieldMatrix(FractionField.getInstance(), testData); FieldLUDecomposition LU = new FieldLUDecomposition(matrix); Assert.assertEquals(testData.length, LU.getL().getRowDimension()); Assert.assertEquals(testData.length, LU.getL().getColumnDimension()); Assert.assertEquals(testData.length, LU.getU().getRowDimension()); Assert.assertEquals(testData.length, LU.getU().getColumnDimension()); Assert.assertEquals(testData.length, LU.getP().getRowDimension()); Assert.assertEquals(testData.length, LU.getP().getColumnDimension()); } /** test non-square matrix */ @Test public void testNonSquare() { try { // we don't use FractionField.getInstance() for testing purposes new FieldLUDecomposition(new Array2DRowFieldMatrix(new Fraction[][] { { Fraction.ZERO, Fraction.ZERO }, { Fraction.ZERO, Fraction.ZERO }, { Fraction.ZERO, Fraction.ZERO } })); Assert.fail("Expected NonSquareMatrixException"); } catch (NonSquareMatrixException ime) { // expected behavior } } /** test PA = LU */ @Test public void testPAEqualLU() { FieldMatrix matrix = new Array2DRowFieldMatrix(FractionField.getInstance(), testData); FieldLUDecomposition lu = new FieldLUDecomposition(matrix); FieldMatrix l = lu.getL(); FieldMatrix u = lu.getU(); FieldMatrix p = lu.getP(); TestUtils.assertEquals(p.multiply(matrix), l.multiply(u)); matrix = new Array2DRowFieldMatrix(FractionField.getInstance(), testDataMinus); lu = new FieldLUDecomposition(matrix); l = lu.getL(); u = lu.getU(); p = lu.getP(); TestUtils.assertEquals(p.multiply(matrix), l.multiply(u)); matrix = new Array2DRowFieldMatrix(FractionField.getInstance(), 17, 17); for (int i = 0; i < matrix.getRowDimension(); ++i) { matrix.setEntry(i, i, Fraction.ONE); } lu = new FieldLUDecomposition(matrix); l = lu.getL(); u = lu.getU(); p = lu.getP(); TestUtils.assertEquals(p.multiply(matrix), l.multiply(u)); matrix = new Array2DRowFieldMatrix(FractionField.getInstance(), singular); lu = new FieldLUDecomposition(matrix); Assert.assertFalse(lu.getSolver().isNonSingular()); Assert.assertNull(lu.getL()); Assert.assertNull(lu.getU()); Assert.assertNull(lu.getP()); matrix = new Array2DRowFieldMatrix(FractionField.getInstance(), bigSingular); lu = new FieldLUDecomposition(matrix); Assert.assertFalse(lu.getSolver().isNonSingular()); Assert.assertNull(lu.getL()); Assert.assertNull(lu.getU()); Assert.assertNull(lu.getP()); } /** test that L is lower triangular with unit diagonal */ @Test public void testLLowerTriangular() { FieldMatrix matrix = new Array2DRowFieldMatrix(FractionField.getInstance(), testData); FieldMatrix l = new FieldLUDecomposition(matrix).getL(); for (int i = 0; i < l.getRowDimension(); i++) { Assert.assertEquals(Fraction.ONE, l.getEntry(i, i)); for (int j = i + 1; j < l.getColumnDimension(); j++) { Assert.assertEquals(Fraction.ZERO, l.getEntry(i, j)); } } } /** test that U is upper triangular */ @Test public void testUUpperTriangular() { FieldMatrix matrix = new Array2DRowFieldMatrix(FractionField.getInstance(), testData); FieldMatrix u = new FieldLUDecomposition(matrix).getU(); for (int i = 0; i < u.getRowDimension(); i++) { for (int j = 0; j < i; j++) { Assert.assertEquals(Fraction.ZERO, u.getEntry(i, j)); } } } /** test that P is a permutation matrix */ @Test public void testPPermutation() { FieldMatrix matrix = new Array2DRowFieldMatrix(FractionField.getInstance(), testData); FieldMatrix p = new FieldLUDecomposition(matrix).getP(); FieldMatrix ppT = p.multiply(p.transpose()); FieldMatrix id = new Array2DRowFieldMatrix(FractionField.getInstance(), p.getRowDimension(), p.getRowDimension()); for (int i = 0; i < id.getRowDimension(); ++i) { id.setEntry(i, i, Fraction.ONE); } TestUtils.assertEquals(id, ppT); for (int i = 0; i < p.getRowDimension(); i++) { int zeroCount = 0; int oneCount = 0; int otherCount = 0; for (int j = 0; j < p.getColumnDimension(); j++) { final Fraction e = p.getEntry(i, j); if (e.equals(Fraction.ZERO)) { ++zeroCount; } else if (e.equals(Fraction.ONE)) { ++oneCount; } else { ++otherCount; } } Assert.assertEquals(p.getColumnDimension() - 1, zeroCount); Assert.assertEquals(1, oneCount); Assert.assertEquals(0, otherCount); } for (int j = 0; j < p.getColumnDimension(); j++) { int zeroCount = 0; int oneCount = 0; int otherCount = 0; for (int i = 0; i < p.getRowDimension(); i++) { final Fraction e = p.getEntry(i, j); if (e.equals(Fraction.ZERO)) { ++zeroCount; } else if (e.equals(Fraction.ONE)) { ++oneCount; } else { ++otherCount; } } Assert.assertEquals(p.getRowDimension() - 1, zeroCount); Assert.assertEquals(1, oneCount); Assert.assertEquals(0, otherCount); } } /** test singular */ @Test public void testSingular() { FieldLUDecomposition lu = new FieldLUDecomposition(new Array2DRowFieldMatrix(FractionField.getInstance(), testData)); Assert.assertTrue(lu.getSolver().isNonSingular()); lu = new FieldLUDecomposition(new Array2DRowFieldMatrix(FractionField.getInstance(), singular)); Assert.assertFalse(lu.getSolver().isNonSingular()); lu = new FieldLUDecomposition(new Array2DRowFieldMatrix(FractionField.getInstance(), bigSingular)); Assert.assertFalse(lu.getSolver().isNonSingular()); } /** test matrices values */ @Test public void testMatricesValues1() { FieldLUDecomposition lu = new FieldLUDecomposition(new Array2DRowFieldMatrix(FractionField.getInstance(), testData)); FieldMatrix lRef = new Array2DRowFieldMatrix(FractionField.getInstance(), new Fraction[][] { { new Fraction(1), new Fraction(0), new Fraction(0) }, { new Fraction(2), new Fraction(1), new Fraction(0) }, { new Fraction(1), new Fraction(-2), new Fraction(1) } }); FieldMatrix uRef = new Array2DRowFieldMatrix(FractionField.getInstance(), new Fraction[][] { { new Fraction(1), new Fraction(2), new Fraction(3) }, { new Fraction(0), new Fraction(1), new Fraction(-3) }, { new Fraction(0), new Fraction(0), new Fraction(-1) } }); FieldMatrix pRef = new Array2DRowFieldMatrix(FractionField.getInstance(), new Fraction[][] { { new Fraction(1), new Fraction(0), new Fraction(0) }, { new Fraction(0), new Fraction(1), new Fraction(0) }, { new Fraction(0), new Fraction(0), new Fraction(1) } }); int[] pivotRef = { 0, 1, 2 }; // check values against known references FieldMatrix l = lu.getL(); TestUtils.assertEquals(lRef, l); FieldMatrix u = lu.getU(); TestUtils.assertEquals(uRef, u); FieldMatrix p = lu.getP(); TestUtils.assertEquals(pRef, p); int[] pivot = lu.getPivot(); for (int i = 0; i < pivotRef.length; ++i) { Assert.assertEquals(pivotRef[i], pivot[i]); } // check the same cached instance is returned the second time Assert.assertTrue(l == lu.getL()); Assert.assertTrue(u == lu.getU()); Assert.assertTrue(p == lu.getP()); } /** test matrices values */ @Test public void testMatricesValues2() { FieldLUDecomposition lu = new FieldLUDecomposition(new Array2DRowFieldMatrix(FractionField.getInstance(), luData)); FieldMatrix lRef = new Array2DRowFieldMatrix(FractionField.getInstance(), new Fraction[][] { { new Fraction(1), new Fraction(0), new Fraction(0) }, { new Fraction(3), new Fraction(1), new Fraction(0) }, { new Fraction(1), new Fraction(0), new Fraction(1) } }); FieldMatrix uRef = new Array2DRowFieldMatrix(FractionField.getInstance(), new Fraction[][] { { new Fraction(2), new Fraction(3), new Fraction(3) }, { new Fraction(0), new Fraction(-3), new Fraction(-1) }, { new Fraction(0), new Fraction(0), new Fraction(4) } }); FieldMatrix pRef = new Array2DRowFieldMatrix(FractionField.getInstance(), new Fraction[][] { { new Fraction(1), new Fraction(0), new Fraction(0) }, { new Fraction(0), new Fraction(0), new Fraction(1) }, { new Fraction(0), new Fraction(1), new Fraction(0) } }); int[] pivotRef = { 0, 2, 1 }; // check values against known references FieldMatrix l = lu.getL(); TestUtils.assertEquals(lRef, l); FieldMatrix u = lu.getU(); TestUtils.assertEquals(uRef, u); FieldMatrix p = lu.getP(); TestUtils.assertEquals(pRef, p); int[] pivot = lu.getPivot(); for (int i = 0; i < pivotRef.length; ++i) { Assert.assertEquals(pivotRef[i], pivot[i]); } // check the same cached instance is returned the second time Assert.assertTrue(l == lu.getL()); Assert.assertTrue(u == lu.getU()); Assert.assertTrue(p == lu.getP()); } } commons-math3-3.2-src/src/test/java/org/apache/commons/math3/linear/RealMatrixFormatTest.java100644 1750 1750 2132 12126627674 30760 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.linear; import java.util.Locale; public class RealMatrixFormatTest extends RealMatrixFormatAbstractTest { @Override protected char getDecimalCharacter() { return '.'; } @Override protected Locale getLocale() { return Locale.US; } } commons-math3-3.2-src/src/test/java/org/apache/commons/math3/linear/TriDiagonalTransformerTest.java100644 1750 1750 16226 12126627674 32210 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.linear; import java.util.Arrays; import org.apache.commons.math3.util.FastMath; import org.junit.Test; import org.junit.Assert; public class TriDiagonalTransformerTest { private double[][] testSquare5 = { { 1, 2, 3, 1, 1 }, { 2, 1, 1, 3, 1 }, { 3, 1, 1, 1, 2 }, { 1, 3, 1, 2, 1 }, { 1, 1, 2, 1, 3 } }; private double[][] testSquare3 = { { 1, 3, 4 }, { 3, 2, 2 }, { 4, 2, 0 } }; @Test public void testNonSquare() { try { new TriDiagonalTransformer(MatrixUtils.createRealMatrix(new double[3][2])); Assert.fail("an exception should have been thrown"); } catch (NonSquareMatrixException ime) { // expected behavior } } @Test public void testAEqualQTQt() { checkAEqualQTQt(MatrixUtils.createRealMatrix(testSquare5)); checkAEqualQTQt(MatrixUtils.createRealMatrix(testSquare3)); } private void checkAEqualQTQt(RealMatrix matrix) { TriDiagonalTransformer transformer = new TriDiagonalTransformer(matrix); RealMatrix q = transformer.getQ(); RealMatrix qT = transformer.getQT(); RealMatrix t = transformer.getT(); double norm = q.multiply(t).multiply(qT).subtract(matrix).getNorm(); Assert.assertEquals(0, norm, 4.0e-15); } @Test public void testNoAccessBelowDiagonal() { checkNoAccessBelowDiagonal(testSquare5); checkNoAccessBelowDiagonal(testSquare3); } private void checkNoAccessBelowDiagonal(double[][] data) { double[][] modifiedData = new double[data.length][]; for (int i = 0; i < data.length; ++i) { modifiedData[i] = data[i].clone(); Arrays.fill(modifiedData[i], 0, i, Double.NaN); } RealMatrix matrix = MatrixUtils.createRealMatrix(modifiedData); TriDiagonalTransformer transformer = new TriDiagonalTransformer(matrix); RealMatrix q = transformer.getQ(); RealMatrix qT = transformer.getQT(); RealMatrix t = transformer.getT(); double norm = q.multiply(t).multiply(qT).subtract(MatrixUtils.createRealMatrix(data)).getNorm(); Assert.assertEquals(0, norm, 4.0e-15); } @Test public void testQOrthogonal() { checkOrthogonal(new TriDiagonalTransformer(MatrixUtils.createRealMatrix(testSquare5)).getQ()); checkOrthogonal(new TriDiagonalTransformer(MatrixUtils.createRealMatrix(testSquare3)).getQ()); } @Test public void testQTOrthogonal() { checkOrthogonal(new TriDiagonalTransformer(MatrixUtils.createRealMatrix(testSquare5)).getQT()); checkOrthogonal(new TriDiagonalTransformer(MatrixUtils.createRealMatrix(testSquare3)).getQT()); } private void checkOrthogonal(RealMatrix m) { RealMatrix mTm = m.transpose().multiply(m); RealMatrix id = MatrixUtils.createRealIdentityMatrix(mTm.getRowDimension()); Assert.assertEquals(0, mTm.subtract(id).getNorm(), 1.0e-15); } @Test public void testTTriDiagonal() { checkTriDiagonal(new TriDiagonalTransformer(MatrixUtils.createRealMatrix(testSquare5)).getT()); checkTriDiagonal(new TriDiagonalTransformer(MatrixUtils.createRealMatrix(testSquare3)).getT()); } private void checkTriDiagonal(RealMatrix m) { final int rows = m.getRowDimension(); final int cols = m.getColumnDimension(); for (int i = 0; i < rows; ++i) { for (int j = 0; j < cols; ++j) { if ((i < j - 1) || (i > j + 1)) { Assert.assertEquals(0, m.getEntry(i, j), 1.0e-16); } } } } @Test public void testMatricesValues5() { checkMatricesValues(testSquare5, new double[][] { { 1.0, 0.0, 0.0, 0.0, 0.0 }, { 0.0, -0.5163977794943222, 0.016748280772542083, 0.839800693771262, 0.16669620021405473 }, { 0.0, -0.7745966692414833, -0.4354553000860955, -0.44989322880603355, -0.08930153582895772 }, { 0.0, -0.2581988897471611, 0.6364346693566014, -0.30263204032131164, 0.6608313651342882 }, { 0.0, -0.2581988897471611, 0.6364346693566009, -0.027289660803112598, -0.7263191580755246 } }, new double[] { 1, 4.4, 1.433099579242636, -0.89537362758743, 2.062274048344794 }, new double[] { -FastMath.sqrt(15), -3.0832882879592476, 0.6082710842351517, 1.1786086405912128 }); } @Test public void testMatricesValues3() { checkMatricesValues(testSquare3, new double[][] { { 1.0, 0.0, 0.0 }, { 0.0, -0.6, 0.8 }, { 0.0, -0.8, -0.6 }, }, new double[] { 1, 2.64, -0.64 }, new double[] { -5, -1.52 }); } private void checkMatricesValues(double[][] matrix, double[][] qRef, double[] mainDiagnonal, double[] secondaryDiagonal) { TriDiagonalTransformer transformer = new TriDiagonalTransformer(MatrixUtils.createRealMatrix(matrix)); // check values against known references RealMatrix q = transformer.getQ(); Assert.assertEquals(0, q.subtract(MatrixUtils.createRealMatrix(qRef)).getNorm(), 1.0e-14); RealMatrix t = transformer.getT(); double[][] tData = new double[mainDiagnonal.length][mainDiagnonal.length]; for (int i = 0; i < mainDiagnonal.length; ++i) { tData[i][i] = mainDiagnonal[i]; if (i > 0) { tData[i][i - 1] = secondaryDiagonal[i - 1]; } if (i < secondaryDiagonal.length) { tData[i][i + 1] = secondaryDiagonal[i]; } } Assert.assertEquals(0, t.subtract(MatrixUtils.createRealMatrix(tData)).getNorm(), 1.0e-14); // check the same cached instance is returned the second time Assert.assertTrue(q == transformer.getQ()); Assert.assertTrue(t == transformer.getT()); } } commons-math3-3.2-src/src/test/java/org/apache/commons/math3/linear/ArrayFieldVectorTest.java100644 1750 1750 63417 12126627674 31001 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.linear; import java.io.Serializable; import java.lang.reflect.Array; import org.apache.commons.math3.Field; import org.apache.commons.math3.FieldElement; import org.apache.commons.math3.TestUtils; import org.apache.commons.math3.exception.MathIllegalArgumentException; import org.apache.commons.math3.exception.OutOfRangeException; import org.apache.commons.math3.fraction.Fraction; import org.apache.commons.math3.fraction.FractionField; import org.junit.Assert; import org.junit.Test; /** * Test cases for the {@link ArrayFieldVector} class. * * @version $Id: ArrayFieldVectorTest.java 1296542 2012-03-03 00:55:17Z sebb $ */ public class ArrayFieldVectorTest { // protected Fraction[][] ma1 = { {new Fraction(1), new Fraction(2), new Fraction(3)}, {new Fraction(4), new Fraction(5), new Fraction(6)}, {new Fraction(7), new Fraction(8), new Fraction(9)} }; protected Fraction[] vec1 = {new Fraction(1), new Fraction(2), new Fraction(3)}; protected Fraction[] vec2 = {new Fraction(4), new Fraction(5), new Fraction(6)}; protected Fraction[] vec3 = {new Fraction(7), new Fraction(8), new Fraction(9)}; protected Fraction[] vec4 = { new Fraction(1), new Fraction(2), new Fraction(3), new Fraction(4), new Fraction(5), new Fraction(6), new Fraction(7), new Fraction(8), new Fraction(9)}; protected Fraction[] vec_null = {new Fraction(0), new Fraction(0), new Fraction(0)}; protected Fraction[] dvec1 = {new Fraction(1), new Fraction(2), new Fraction(3), new Fraction(4), new Fraction(5), new Fraction(6), new Fraction(7), new Fraction(8), new Fraction(9)}; protected Fraction[][] mat1 = { {new Fraction(1), new Fraction(2), new Fraction(3)}, {new Fraction(4), new Fraction(5), new Fraction(6)}, {new Fraction(7), new Fraction(8), new Fraction(9)} }; // Testclass to test the FieldVector interface // only with enough content to support the test public static class FieldVectorTestImpl> implements FieldVector, Serializable { private static final long serialVersionUID = 3970959016014158539L; private final Field field; /** Entries of the vector. */ protected T[] data; /** Build an array of elements. * @param length size of the array to build * @return a new array */ @SuppressWarnings("unchecked") // field is of type T private T[] buildArray(final int length) { return (T[]) Array.newInstance(field.getRuntimeClass(), length); } public FieldVectorTestImpl(T[] d) { field = d[0].getField(); data = d.clone(); } public Field getField() { return field; } private UnsupportedOperationException unsupported() { return new UnsupportedOperationException("Not supported, unneeded for test purposes"); } public FieldVector copy() { throw unsupported(); } public FieldVector add(FieldVector v) { throw unsupported(); } public FieldVector add(T[] v) { throw unsupported(); } public FieldVector subtract(FieldVector v) { throw unsupported(); } public FieldVector subtract(T[] v) { throw unsupported(); } public FieldVector mapAdd(T d) { throw unsupported(); } public FieldVector mapAddToSelf(T d) { throw unsupported(); } public FieldVector mapSubtract(T d) { throw unsupported(); } public FieldVector mapSubtractToSelf(T d) { throw unsupported(); } public FieldVector mapMultiply(T d) { T[] out = buildArray(data.length); for (int i = 0; i < data.length; i++) { out[i] = data[i].multiply(d); } return new FieldVectorTestImpl(out); } public FieldVector mapMultiplyToSelf(T d) { throw unsupported(); } public FieldVector mapDivide(T d) { throw unsupported(); } public FieldVector mapDivideToSelf(T d) { throw unsupported(); } public FieldVector mapInv() { throw unsupported(); } public FieldVector mapInvToSelf() { throw unsupported(); } public FieldVector ebeMultiply(FieldVector v) { throw unsupported(); } public FieldVector ebeMultiply(T[] v) { throw unsupported(); } public FieldVector ebeDivide(FieldVector v) { throw unsupported(); } public FieldVector ebeDivide(T[] v) { throw unsupported(); } public T[] getData() { return data.clone(); } public T dotProduct(FieldVector v) { T dot = field.getZero(); for (int i = 0; i < data.length; i++) { dot = dot.add(data[i].multiply(v.getEntry(i))); } return dot; } public T dotProduct(T[] v) { T dot = field.getZero(); for (int i = 0; i < data.length; i++) { dot = dot.add(data[i].multiply(v[i])); } return dot; } public FieldVector projection(FieldVector v) { throw unsupported(); } public FieldVector projection(T[] v) { throw unsupported(); } public FieldMatrix outerProduct(FieldVector v) { throw unsupported(); } public FieldMatrix outerProduct(T[] v) { throw unsupported(); } public T getEntry(int index) { return data[index]; } public int getDimension() { return data.length; } public FieldVector append(FieldVector v) { throw unsupported(); } public FieldVector append(T d) { throw unsupported(); } public FieldVector append(T[] a) { throw unsupported(); } public FieldVector getSubVector(int index, int n) { throw unsupported(); } public void setEntry(int index, T value) { throw unsupported(); } public void setSubVector(int index, FieldVector v) { throw unsupported(); } public void setSubVector(int index, T[] v) { throw unsupported(); } public void set(T value) { throw unsupported(); } public T[] toArray() { throw unsupported(); } } @Test public void testConstructors() { ArrayFieldVector v0 = new ArrayFieldVector(FractionField.getInstance()); Assert.assertEquals(0, v0.getDimension()); ArrayFieldVector v1 = new ArrayFieldVector(FractionField.getInstance(), 7); Assert.assertEquals(7, v1.getDimension()); Assert.assertEquals(new Fraction(0), v1.getEntry(6)); ArrayFieldVector v2 = new ArrayFieldVector(5, new Fraction(123, 100)); Assert.assertEquals(5, v2.getDimension()); Assert.assertEquals(new Fraction(123, 100), v2.getEntry(4)); ArrayFieldVector v3 = new ArrayFieldVector(FractionField.getInstance(), vec1); Assert.assertEquals(3, v3.getDimension()); Assert.assertEquals(new Fraction(2), v3.getEntry(1)); ArrayFieldVector v4 = new ArrayFieldVector(FractionField.getInstance(), vec4, 3, 2); Assert.assertEquals(2, v4.getDimension()); Assert.assertEquals(new Fraction(4), v4.getEntry(0)); try { new ArrayFieldVector(vec4, 8, 3); Assert.fail("MathIllegalArgumentException expected"); } catch (MathIllegalArgumentException ex) { // expected behavior } FieldVector v5_i = new ArrayFieldVector(dvec1); Assert.assertEquals(9, v5_i.getDimension()); Assert.assertEquals(new Fraction(9), v5_i.getEntry(8)); ArrayFieldVector v5 = new ArrayFieldVector(dvec1); Assert.assertEquals(9, v5.getDimension()); Assert.assertEquals(new Fraction(9), v5.getEntry(8)); ArrayFieldVector v6 = new ArrayFieldVector(dvec1, 3, 2); Assert.assertEquals(2, v6.getDimension()); Assert.assertEquals(new Fraction(4), v6.getEntry(0)); try { new ArrayFieldVector(dvec1, 8, 3); Assert.fail("MathIllegalArgumentException expected"); } catch (MathIllegalArgumentException ex) { // expected behavior } ArrayFieldVector v7 = new ArrayFieldVector(v1); Assert.assertEquals(7, v7.getDimension()); Assert.assertEquals(new Fraction(0), v7.getEntry(6)); FieldVectorTestImpl v7_i = new FieldVectorTestImpl(vec1); ArrayFieldVector v7_2 = new ArrayFieldVector(v7_i); Assert.assertEquals(3, v7_2.getDimension()); Assert.assertEquals(new Fraction(2), v7_2.getEntry(1)); ArrayFieldVector v8 = new ArrayFieldVector(v1, true); Assert.assertEquals(7, v8.getDimension()); Assert.assertEquals(new Fraction(0), v8.getEntry(6)); Assert.assertNotSame("testData not same object ", v1.getDataRef(), v8.getDataRef()); ArrayFieldVector v8_2 = new ArrayFieldVector(v1, false); Assert.assertEquals(7, v8_2.getDimension()); Assert.assertEquals(new Fraction(0), v8_2.getEntry(6)); Assert.assertArrayEquals(v1.getDataRef(), v8_2.getDataRef()); ArrayFieldVector v9 = new ArrayFieldVector(v1, v3); Assert.assertEquals(10, v9.getDimension()); Assert.assertEquals(new Fraction(1), v9.getEntry(7)); } @Test public void testDataInOut() { ArrayFieldVector v1 = new ArrayFieldVector(vec1); ArrayFieldVector v2 = new ArrayFieldVector(vec2); ArrayFieldVector v4 = new ArrayFieldVector(vec4); FieldVectorTestImpl v2_t = new FieldVectorTestImpl(vec2); FieldVector v_append_1 = v1.append(v2); Assert.assertEquals(6, v_append_1.getDimension()); Assert.assertEquals(new Fraction(4), v_append_1.getEntry(3)); FieldVector v_append_2 = v1.append(new Fraction(2)); Assert.assertEquals(4, v_append_2.getDimension()); Assert.assertEquals(new Fraction(2), v_append_2.getEntry(3)); FieldVector v_append_4 = v1.append(v2_t); Assert.assertEquals(6, v_append_4.getDimension()); Assert.assertEquals(new Fraction(4), v_append_4.getEntry(3)); FieldVector v_copy = v1.copy(); Assert.assertEquals(3, v_copy.getDimension()); Assert.assertNotSame("testData not same object ", v1.getDataRef(), v_copy.getData()); Fraction[] a_frac = v1.toArray(); Assert.assertEquals(3, a_frac.length); Assert.assertNotSame("testData not same object ", v1.getDataRef(), a_frac); // ArrayFieldVector vout4 = (ArrayFieldVector) v1.clone(); // Assert.assertEquals(3, vout4.getDimension()); // Assert.assertEquals(v1.getDataRef(), vout4.getDataRef()); FieldVector vout5 = v4.getSubVector(3, 3); Assert.assertEquals(3, vout5.getDimension()); Assert.assertEquals(new Fraction(5), vout5.getEntry(1)); try { v4.getSubVector(3, 7); Assert.fail("OutOfRangeException expected"); } catch (OutOfRangeException ex) { // expected behavior } ArrayFieldVector v_set1 = (ArrayFieldVector) v1.copy(); v_set1.setEntry(1, new Fraction(11)); Assert.assertEquals(new Fraction(11), v_set1.getEntry(1)); try { v_set1.setEntry(3, new Fraction(11)); Assert.fail("OutOfRangeException expected"); } catch (OutOfRangeException ex) { // expected behavior } ArrayFieldVector v_set2 = (ArrayFieldVector) v4.copy(); v_set2.set(3, v1); Assert.assertEquals(new Fraction(1), v_set2.getEntry(3)); Assert.assertEquals(new Fraction(7), v_set2.getEntry(6)); try { v_set2.set(7, v1); Assert.fail("OutOfRangeException expected"); } catch (OutOfRangeException ex) { // expected behavior } ArrayFieldVector v_set3 = (ArrayFieldVector) v1.copy(); v_set3.set(new Fraction(13)); Assert.assertEquals(new Fraction(13), v_set3.getEntry(2)); try { v_set3.getEntry(23); Assert.fail("ArrayIndexOutOfBoundsException expected"); } catch (ArrayIndexOutOfBoundsException ex) { // expected behavior } ArrayFieldVector v_set4 = (ArrayFieldVector) v4.copy(); v_set4.setSubVector(3, v2_t); Assert.assertEquals(new Fraction(4), v_set4.getEntry(3)); Assert.assertEquals(new Fraction(7), v_set4.getEntry(6)); try { v_set4.setSubVector(7, v2_t); Assert.fail("OutOfRangeException expected"); } catch (OutOfRangeException ex) { // expected behavior } ArrayFieldVector vout10 = (ArrayFieldVector) v1.copy(); ArrayFieldVector vout10_2 = (ArrayFieldVector) v1.copy(); Assert.assertEquals(vout10, vout10_2); vout10_2.setEntry(0, new Fraction(11, 10)); Assert.assertNotSame(vout10, vout10_2); } @Test public void testMapFunctions() { ArrayFieldVector v1 = new ArrayFieldVector(vec1); //octave = v1 .+ 2.0 FieldVector v_mapAdd = v1.mapAdd(new Fraction(2)); Fraction[] result_mapAdd = {new Fraction(3), new Fraction(4), new Fraction(5)}; checkArray("compare vectors" ,result_mapAdd,v_mapAdd.getData()); //octave = v1 .+ 2.0 FieldVector v_mapAddToSelf = v1.copy(); v_mapAddToSelf.mapAddToSelf(new Fraction(2)); Fraction[] result_mapAddToSelf = {new Fraction(3), new Fraction(4), new Fraction(5)}; checkArray("compare vectors" ,result_mapAddToSelf,v_mapAddToSelf.getData()); //octave = v1 .- 2.0 FieldVector v_mapSubtract = v1.mapSubtract(new Fraction(2)); Fraction[] result_mapSubtract = {new Fraction(-1), new Fraction(0), new Fraction(1)}; checkArray("compare vectors" ,result_mapSubtract,v_mapSubtract.getData()); //octave = v1 .- 2.0 FieldVector v_mapSubtractToSelf = v1.copy(); v_mapSubtractToSelf.mapSubtractToSelf(new Fraction(2)); Fraction[] result_mapSubtractToSelf = {new Fraction(-1), new Fraction(0), new Fraction(1)}; checkArray("compare vectors" ,result_mapSubtractToSelf,v_mapSubtractToSelf.getData()); //octave = v1 .* 2.0 FieldVector v_mapMultiply = v1.mapMultiply(new Fraction(2)); Fraction[] result_mapMultiply = {new Fraction(2), new Fraction(4), new Fraction(6)}; checkArray("compare vectors" ,result_mapMultiply,v_mapMultiply.getData()); //octave = v1 .* 2.0 FieldVector v_mapMultiplyToSelf = v1.copy(); v_mapMultiplyToSelf.mapMultiplyToSelf(new Fraction(2)); Fraction[] result_mapMultiplyToSelf = {new Fraction(2), new Fraction(4), new Fraction(6)}; checkArray("compare vectors" ,result_mapMultiplyToSelf,v_mapMultiplyToSelf.getData()); //octave = v1 ./ 2.0 FieldVector v_mapDivide = v1.mapDivide(new Fraction(2)); Fraction[] result_mapDivide = {new Fraction(1, 2), new Fraction(1), new Fraction(3, 2)}; checkArray("compare vectors" ,result_mapDivide,v_mapDivide.getData()); //octave = v1 ./ 2.0 FieldVector v_mapDivideToSelf = v1.copy(); v_mapDivideToSelf.mapDivideToSelf(new Fraction(2)); Fraction[] result_mapDivideToSelf = {new Fraction(1, 2), new Fraction(1), new Fraction(3, 2)}; checkArray("compare vectors" ,result_mapDivideToSelf,v_mapDivideToSelf.getData()); //octave = v1 .^-1 FieldVector v_mapInv = v1.mapInv(); Fraction[] result_mapInv = {new Fraction(1),new Fraction(1, 2),new Fraction(1, 3)}; checkArray("compare vectors" ,result_mapInv,v_mapInv.getData()); //octave = v1 .^-1 FieldVector v_mapInvToSelf = v1.copy(); v_mapInvToSelf.mapInvToSelf(); Fraction[] result_mapInvToSelf = {new Fraction(1),new Fraction(1, 2),new Fraction(1, 3)}; checkArray("compare vectors" ,result_mapInvToSelf,v_mapInvToSelf.getData()); } @Test public void testBasicFunctions() { ArrayFieldVector v1 = new ArrayFieldVector(vec1); ArrayFieldVector v2 = new ArrayFieldVector(vec2); new ArrayFieldVector(vec_null); FieldVectorTestImpl v2_t = new FieldVectorTestImpl(vec2); //octave = v1 + v2 ArrayFieldVector v_add = v1.add(v2); Fraction[] result_add = {new Fraction(5), new Fraction(7), new Fraction(9)}; checkArray("compare vect" ,v_add.getData(),result_add); FieldVectorTestImpl vt2 = new FieldVectorTestImpl(vec2); FieldVector v_add_i = v1.add(vt2); Fraction[] result_add_i = {new Fraction(5), new Fraction(7), new Fraction(9)}; checkArray("compare vect" ,v_add_i.getData(),result_add_i); //octave = v1 - v2 ArrayFieldVector v_subtract = v1.subtract(v2); Fraction[] result_subtract = {new Fraction(-3), new Fraction(-3), new Fraction(-3)}; checkArray("compare vect" ,v_subtract.getData(),result_subtract); FieldVector v_subtract_i = v1.subtract(vt2); Fraction[] result_subtract_i = {new Fraction(-3), new Fraction(-3), new Fraction(-3)}; checkArray("compare vect" ,v_subtract_i.getData(),result_subtract_i); // octave v1 .* v2 ArrayFieldVector v_ebeMultiply = v1.ebeMultiply(v2); Fraction[] result_ebeMultiply = {new Fraction(4), new Fraction(10), new Fraction(18)}; checkArray("compare vect" ,v_ebeMultiply.getData(),result_ebeMultiply); FieldVector v_ebeMultiply_2 = v1.ebeMultiply(v2_t); Fraction[] result_ebeMultiply_2 = {new Fraction(4), new Fraction(10), new Fraction(18)}; checkArray("compare vect" ,v_ebeMultiply_2.getData(),result_ebeMultiply_2); // octave v1 ./ v2 ArrayFieldVector v_ebeDivide = v1.ebeDivide(v2); Fraction[] result_ebeDivide = {new Fraction(1, 4), new Fraction(2, 5), new Fraction(1, 2)}; checkArray("compare vect" ,v_ebeDivide.getData(),result_ebeDivide); FieldVector v_ebeDivide_2 = v1.ebeDivide(v2_t); Fraction[] result_ebeDivide_2 = {new Fraction(1, 4), new Fraction(2, 5), new Fraction(1, 2)}; checkArray("compare vect" ,v_ebeDivide_2.getData(),result_ebeDivide_2); // octave dot(v1,v2) Fraction dot = v1.dotProduct(v2); Assert.assertEquals("compare val ",new Fraction(32), dot); // octave dot(v1,v2_t) Fraction dot_2 = v1.dotProduct(v2_t); Assert.assertEquals("compare val ",new Fraction(32), dot_2); FieldMatrix m_outerProduct = v1.outerProduct(v2); Assert.assertEquals("compare val ",new Fraction(4), m_outerProduct.getEntry(0,0)); FieldMatrix m_outerProduct_2 = v1.outerProduct(v2_t); Assert.assertEquals("compare val ",new Fraction(4), m_outerProduct_2.getEntry(0,0)); ArrayFieldVector v_projection = v1.projection(v2); Fraction[] result_projection = {new Fraction(128, 77), new Fraction(160, 77), new Fraction(192, 77)}; checkArray("compare vect", v_projection.getData(), result_projection); FieldVector v_projection_2 = v1.projection(v2_t); Fraction[] result_projection_2 = {new Fraction(128, 77), new Fraction(160, 77), new Fraction(192, 77)}; checkArray("compare vect", v_projection_2.getData(), result_projection_2); } @Test public void testMisc() { ArrayFieldVector v1 = new ArrayFieldVector(vec1); ArrayFieldVector v4 = new ArrayFieldVector(vec4); FieldVector v4_2 = new ArrayFieldVector(vec4); String out1 = v1.toString(); Assert.assertTrue("some output ", out1.length()!=0); /* Fraction[] dout1 = v1.copyOut(); Assert.assertEquals(3, dout1.length); assertNotSame("testData not same object ", v1.getDataRef(), dout1); */ try { v1.checkVectorDimensions(2); Assert.fail("MathIllegalArgumentException expected"); } catch (MathIllegalArgumentException ex) { // expected behavior } try { v1.checkVectorDimensions(v4); Assert.fail("MathIllegalArgumentException expected"); } catch (MathIllegalArgumentException ex) { // expected behavior } try { v1.checkVectorDimensions(v4_2); Assert.fail("MathIllegalArgumentException expected"); } catch (MathIllegalArgumentException ex) { // expected behavior } } @Test public void testSerial() { ArrayFieldVector v = new ArrayFieldVector(vec1); Assert.assertEquals(v,TestUtils.serializeAndRecover(v)); } @Test public void testZeroVectors() { // when the field is not specified, array cannot be empty try { new ArrayFieldVector(new Fraction[0]); Assert.fail("MathIllegalArgumentException expected"); } catch (MathIllegalArgumentException ex) { // expected behavior } try { new ArrayFieldVector(new Fraction[0], true); Assert.fail("MathIllegalArgumentException expected"); } catch (MathIllegalArgumentException ex) { // expected behavior } try { new ArrayFieldVector(new Fraction[0], false); Assert.fail("MathIllegalArgumentException expected"); } catch (MathIllegalArgumentException ex) { // expected behavior } // when the field is specified, array can be empty Assert.assertEquals(0, new ArrayFieldVector(FractionField.getInstance(), new Fraction[0]).getDimension()); Assert.assertEquals(0, new ArrayFieldVector(FractionField.getInstance(), new Fraction[0], true).getDimension()); Assert.assertEquals(0, new ArrayFieldVector(FractionField.getInstance(), new Fraction[0], false).getDimension()); } @Test public void testOuterProduct() { final ArrayFieldVector u = new ArrayFieldVector(FractionField.getInstance(), new Fraction[] {new Fraction(1), new Fraction(2), new Fraction(-3)}); final ArrayFieldVector v = new ArrayFieldVector(FractionField.getInstance(), new Fraction[] {new Fraction(4), new Fraction(-2)}); final FieldMatrix uv = u.outerProduct(v); final double tol = Math.ulp(1d); Assert.assertEquals(new Fraction(4).doubleValue(), uv.getEntry(0, 0).doubleValue(), tol); Assert.assertEquals(new Fraction(-2).doubleValue(), uv.getEntry(0, 1).doubleValue(), tol); Assert.assertEquals(new Fraction(8).doubleValue(), uv.getEntry(1, 0).doubleValue(), tol); Assert.assertEquals(new Fraction(-4).doubleValue(), uv.getEntry(1, 1).doubleValue(), tol); Assert.assertEquals(new Fraction(-12).doubleValue(), uv.getEntry(2, 0).doubleValue(), tol); Assert.assertEquals(new Fraction(6).doubleValue(), uv.getEntry(2, 1).doubleValue(), tol); } /** verifies that two vectors are equals */ protected void checkArray(String msg, Fraction[] m, Fraction[] n) { if (m.length != n.length) { Assert.fail("vectors have different lengths"); } for (int i = 0; i < m.length; i++) { Assert.assertEquals(msg + " " + i + " elements differ", m[i],n[i]); } } } ././@LongLink100644 0 0 151 12126630646 10256 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/linear/UnmodifiableArrayRealVectorTest.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/linear/UnmodifiableArrayRealVectorTest.100644 1750 1750 2670 12126627674 32270 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.linear; /** * This is an implementation of {@link UnmodifiableRealVectorAbstractTest} for * unmodifiable views of {@link ArrayRealVectorTest}. * * @version $Id$ */ public class UnmodifiableArrayRealVectorTest extends UnmodifiableRealVectorAbstractTest { /** * Returns a random vector of type {@link ArrayRealVector}. * * @return a new random {@link ArrayRealVector}. */ @Override public RealVector createVector() { ArrayRealVector v = new ArrayRealVector(DIM); for (int i = 0; i < DIM; i++) { v.setEntry(i, RANDOM.nextDouble()); } return v; } } ././@LongLink100644 0 0 156 12126630646 10263 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/linear/MatrixDimensionMismatchExceptionTest.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/linear/MatrixDimensionMismatchException100644 1750 1750 2636 12126627674 32447 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.linear; import org.junit.Assert; import org.junit.Test; /** * Test for {@link MatrixDimensionMismatchException}. * * @version $Id$ */ public class MatrixDimensionMismatchExceptionTest { @Test public void testAccessors() { final MatrixDimensionMismatchException e = new MatrixDimensionMismatchException(1, 2, 3, 4); Assert.assertEquals(1, e.getWrongRowDimension()); Assert.assertEquals(2, e.getWrongColumnDimension()); Assert.assertEquals(3, e.getExpectedRowDimension()); Assert.assertEquals(4, e.getExpectedColumnDimension()); } } commons-math3-3.2-src/src/test/java/org/apache/commons/math3/linear/ConjugateGradientTest.java100644 1750 1750 56602 12126627674 31167 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.linear; import java.util.Arrays; import org.apache.commons.math3.exception.DimensionMismatchException; import org.apache.commons.math3.exception.MathUnsupportedOperationException; import org.apache.commons.math3.exception.MaxCountExceededException; import org.apache.commons.math3.util.FastMath; import org.apache.commons.math3.util.IterationEvent; import org.apache.commons.math3.util.IterationListener; import org.junit.Assert; import org.junit.Test; public class ConjugateGradientTest { @Test(expected = NonSquareOperatorException.class) public void testNonSquareOperator() { final Array2DRowRealMatrix a = new Array2DRowRealMatrix(2, 3); final IterativeLinearSolver solver; solver = new ConjugateGradient(10, 0., false); final ArrayRealVector b = new ArrayRealVector(a.getRowDimension()); final ArrayRealVector x = new ArrayRealVector(a.getColumnDimension()); solver.solve(a, b, x); } @Test(expected = DimensionMismatchException.class) public void testDimensionMismatchRightHandSide() { final Array2DRowRealMatrix a = new Array2DRowRealMatrix(3, 3); final IterativeLinearSolver solver; solver = new ConjugateGradient(10, 0., false); final ArrayRealVector b = new ArrayRealVector(2); final ArrayRealVector x = new ArrayRealVector(3); solver.solve(a, b, x); } @Test(expected = DimensionMismatchException.class) public void testDimensionMismatchSolution() { final Array2DRowRealMatrix a = new Array2DRowRealMatrix(3, 3); final IterativeLinearSolver solver; solver = new ConjugateGradient(10, 0., false); final ArrayRealVector b = new ArrayRealVector(3); final ArrayRealVector x = new ArrayRealVector(2); solver.solve(a, b, x); } @Test(expected = NonPositiveDefiniteOperatorException.class) public void testNonPositiveDefiniteLinearOperator() { final Array2DRowRealMatrix a = new Array2DRowRealMatrix(2, 2); a.setEntry(0, 0, -1.); a.setEntry(0, 1, 2.); a.setEntry(1, 0, 3.); a.setEntry(1, 1, 4.); final IterativeLinearSolver solver; solver = new ConjugateGradient(10, 0., true); final ArrayRealVector b = new ArrayRealVector(2); b.setEntry(0, -1.); b.setEntry(1, -1.); final ArrayRealVector x = new ArrayRealVector(2); solver.solve(a, b, x); } @Test public void testUnpreconditionedSolution() { final int n = 5; final int maxIterations = 100; final RealLinearOperator a = new HilbertMatrix(n); final InverseHilbertMatrix ainv = new InverseHilbertMatrix(n); final IterativeLinearSolver solver; solver = new ConjugateGradient(maxIterations, 1E-10, true); final RealVector b = new ArrayRealVector(n); for (int j = 0; j < n; j++) { b.set(0.); b.setEntry(j, 1.); final RealVector x = solver.solve(a, b); for (int i = 0; i < n; i++) { final double actual = x.getEntry(i); final double expected = ainv.getEntry(i, j); final double delta = 1E-10 * Math.abs(expected); final String msg = String.format("entry[%d][%d]", i, j); Assert.assertEquals(msg, expected, actual, delta); } } } @Test public void testUnpreconditionedInPlaceSolutionWithInitialGuess() { final int n = 5; final int maxIterations = 100; final RealLinearOperator a = new HilbertMatrix(n); final InverseHilbertMatrix ainv = new InverseHilbertMatrix(n); final IterativeLinearSolver solver; solver = new ConjugateGradient(maxIterations, 1E-10, true); final RealVector b = new ArrayRealVector(n); for (int j = 0; j < n; j++) { b.set(0.); b.setEntry(j, 1.); final RealVector x0 = new ArrayRealVector(n); x0.set(1.); final RealVector x = solver.solveInPlace(a, b, x0); Assert.assertSame("x should be a reference to x0", x0, x); for (int i = 0; i < n; i++) { final double actual = x.getEntry(i); final double expected = ainv.getEntry(i, j); final double delta = 1E-10 * Math.abs(expected); final String msg = String.format("entry[%d][%d)", i, j); Assert.assertEquals(msg, expected, actual, delta); } } } @Test public void testUnpreconditionedSolutionWithInitialGuess() { final int n = 5; final int maxIterations = 100; final RealLinearOperator a = new HilbertMatrix(n); final InverseHilbertMatrix ainv = new InverseHilbertMatrix(n); final IterativeLinearSolver solver; solver = new ConjugateGradient(maxIterations, 1E-10, true); final RealVector b = new ArrayRealVector(n); for (int j = 0; j < n; j++) { b.set(0.); b.setEntry(j, 1.); final RealVector x0 = new ArrayRealVector(n); x0.set(1.); final RealVector x = solver.solve(a, b, x0); Assert.assertNotSame("x should not be a reference to x0", x0, x); for (int i = 0; i < n; i++) { final double actual = x.getEntry(i); final double expected = ainv.getEntry(i, j); final double delta = 1E-10 * Math.abs(expected); final String msg = String.format("entry[%d][%d]", i, j); Assert.assertEquals(msg, expected, actual, delta); Assert.assertEquals(msg, x0.getEntry(i), 1., Math.ulp(1.)); } } } /** * Check whether the estimate of the (updated) residual corresponds to the * exact residual. This fails to be true for a large number of iterations, * due to the loss of orthogonality of the successive search directions. * Therefore, in the present test, the number of iterations is limited. */ @Test public void testUnpreconditionedResidual() { final int n = 10; final int maxIterations = n; final RealLinearOperator a = new HilbertMatrix(n); final ConjugateGradient solver; solver = new ConjugateGradient(maxIterations, 1E-15, true); final RealVector r = new ArrayRealVector(n); final RealVector x = new ArrayRealVector(n); final IterationListener listener = new IterationListener() { public void terminationPerformed(final IterationEvent e) { // Do nothing } public void iterationStarted(final IterationEvent e) { // Do nothing } public void iterationPerformed(final IterationEvent e) { final IterativeLinearSolverEvent evt; evt = (IterativeLinearSolverEvent) e; RealVector v = evt.getResidual(); r.setSubVector(0, v); v = evt.getSolution(); x.setSubVector(0, v); } public void initializationPerformed(final IterationEvent e) { // Do nothing } }; solver.getIterationManager().addIterationListener(listener); final RealVector b = new ArrayRealVector(n); for (int j = 0; j < n; j++) { b.set(0.); b.setEntry(j, 1.); boolean caught = false; try { solver.solve(a, b); } catch (MaxCountExceededException e) { caught = true; final RealVector y = a.operate(x); for (int i = 0; i < n; i++) { final double actual = b.getEntry(i) - y.getEntry(i); final double expected = r.getEntry(i); final double delta = 1E-6 * Math.abs(expected); final String msg = String .format("column %d, residual %d", i, j); Assert.assertEquals(msg, expected, actual, delta); } } Assert .assertTrue("MaxCountExceededException should have been caught", caught); } } @Test(expected = NonSquareOperatorException.class) public void testNonSquarePreconditioner() { final Array2DRowRealMatrix a = new Array2DRowRealMatrix(2, 2); final RealLinearOperator m = new RealLinearOperator() { @Override public RealVector operate(final RealVector x) { throw new UnsupportedOperationException(); } @Override public int getRowDimension() { return 2; } @Override public int getColumnDimension() { return 3; } }; final PreconditionedIterativeLinearSolver solver; solver = new ConjugateGradient(10, 0d, false); final ArrayRealVector b = new ArrayRealVector(a.getRowDimension()); solver.solve(a, m, b); } @Test(expected = DimensionMismatchException.class) public void testMismatchedOperatorDimensions() { final Array2DRowRealMatrix a = new Array2DRowRealMatrix(2, 2); final RealLinearOperator m = new RealLinearOperator() { @Override public RealVector operate(final RealVector x) { throw new UnsupportedOperationException(); } @Override public int getRowDimension() { return 3; } @Override public int getColumnDimension() { return 3; } }; final PreconditionedIterativeLinearSolver solver; solver = new ConjugateGradient(10, 0d, false); final ArrayRealVector b = new ArrayRealVector(a.getRowDimension()); solver.solve(a, m, b); } @Test(expected = NonPositiveDefiniteOperatorException.class) public void testNonPositiveDefinitePreconditioner() { final Array2DRowRealMatrix a = new Array2DRowRealMatrix(2, 2); a.setEntry(0, 0, 1d); a.setEntry(0, 1, 2d); a.setEntry(1, 0, 3d); a.setEntry(1, 1, 4d); final RealLinearOperator m = new RealLinearOperator() { @Override public RealVector operate(final RealVector x) { final ArrayRealVector y = new ArrayRealVector(2); y.setEntry(0, -x.getEntry(0)); y.setEntry(1, x.getEntry(1)); return y; } @Override public int getRowDimension() { return 2; } @Override public int getColumnDimension() { return 2; } }; final PreconditionedIterativeLinearSolver solver; solver = new ConjugateGradient(10, 0d, true); final ArrayRealVector b = new ArrayRealVector(2); b.setEntry(0, -1d); b.setEntry(1, -1d); solver.solve(a, m, b); } @Test public void testPreconditionedSolution() { final int n = 8; final int maxIterations = 100; final RealLinearOperator a = new HilbertMatrix(n); final InverseHilbertMatrix ainv = new InverseHilbertMatrix(n); final RealLinearOperator m = JacobiPreconditioner.create(a); final PreconditionedIterativeLinearSolver solver; solver = new ConjugateGradient(maxIterations, 1E-15, true); final RealVector b = new ArrayRealVector(n); for (int j = 0; j < n; j++) { b.set(0.); b.setEntry(j, 1.); final RealVector x = solver.solve(a, m, b); for (int i = 0; i < n; i++) { final double actual = x.getEntry(i); final double expected = ainv.getEntry(i, j); final double delta = 1E-6 * Math.abs(expected); final String msg = String.format("coefficient (%d, %d)", i, j); Assert.assertEquals(msg, expected, actual, delta); } } } @Test public void testPreconditionedResidual() { final int n = 10; final int maxIterations = n; final RealLinearOperator a = new HilbertMatrix(n); final RealLinearOperator m = JacobiPreconditioner.create(a); final ConjugateGradient solver; solver = new ConjugateGradient(maxIterations, 1E-15, true); final RealVector r = new ArrayRealVector(n); final RealVector x = new ArrayRealVector(n); final IterationListener listener = new IterationListener() { public void terminationPerformed(final IterationEvent e) { // Do nothing } public void iterationStarted(final IterationEvent e) { // Do nothing } public void iterationPerformed(final IterationEvent e) { final IterativeLinearSolverEvent evt; evt = (IterativeLinearSolverEvent) e; RealVector v = evt.getResidual(); r.setSubVector(0, v); v = evt.getSolution(); x.setSubVector(0, v); } public void initializationPerformed(final IterationEvent e) { // Do nothing } }; solver.getIterationManager().addIterationListener(listener); final RealVector b = new ArrayRealVector(n); for (int j = 0; j < n; j++) { b.set(0.); b.setEntry(j, 1.); boolean caught = false; try { solver.solve(a, m, b); } catch (MaxCountExceededException e) { caught = true; final RealVector y = a.operate(x); for (int i = 0; i < n; i++) { final double actual = b.getEntry(i) - y.getEntry(i); final double expected = r.getEntry(i); final double delta = 1E-6 * Math.abs(expected); final String msg = String .format("column %d, residual %d", i, j); Assert.assertEquals(msg, expected, actual, delta); } } Assert .assertTrue("MaxCountExceededException should have been caught", caught); } } @Test public void testPreconditionedSolution2() { final int n = 100; final int maxIterations = 100000; final Array2DRowRealMatrix a = new Array2DRowRealMatrix(n, n); double daux = 1.; for (int i = 0; i < n; i++) { a.setEntry(i, i, daux); daux *= 1.2; for (int j = i + 1; j < n; j++) { if (i == j) { } else { final double value = 1.0; a.setEntry(i, j, value); a.setEntry(j, i, value); } } } final RealLinearOperator m = JacobiPreconditioner.create(a); final PreconditionedIterativeLinearSolver pcg; final IterativeLinearSolver cg; pcg = new ConjugateGradient(maxIterations, 1E-6, true); cg = new ConjugateGradient(maxIterations, 1E-6, true); final RealVector b = new ArrayRealVector(n); final String pattern = "preconditioned gradient (%d iterations) should" + " have been faster than unpreconditioned (%d iterations)"; String msg; for (int j = 0; j < 1; j++) { b.set(0.); b.setEntry(j, 1.); final RealVector px = pcg.solve(a, m, b); final RealVector x = cg.solve(a, b); final int npcg = pcg.getIterationManager().getIterations(); final int ncg = cg.getIterationManager().getIterations(); msg = String.format(pattern, npcg, ncg); Assert.assertTrue(msg, npcg < ncg); for (int i = 0; i < n; i++) { msg = String.format("row %d, column %d", i, j); final double expected = x.getEntry(i); final double actual = px.getEntry(i); final double delta = 1E-6 * Math.abs(expected); Assert.assertEquals(msg, expected, actual, delta); } } } @Test public void testEventManagement() { final int n = 5; final int maxIterations = 100; final RealLinearOperator a = new HilbertMatrix(n); final IterativeLinearSolver solver; /* * count[0] = number of calls to initializationPerformed * count[1] = number of calls to iterationStarted * count[2] = number of calls to iterationPerformed * count[3] = number of calls to terminationPerformed */ final int[] count = new int[] {0, 0, 0, 0}; final IterationListener listener = new IterationListener() { private void doTestVectorsAreUnmodifiable(final IterationEvent e) { final IterativeLinearSolverEvent evt; evt = (IterativeLinearSolverEvent) e; try { evt.getResidual().set(0.0); Assert.fail("r is modifiable"); } catch (MathUnsupportedOperationException exc){ // Expected behavior } try { evt.getRightHandSideVector().set(0.0); Assert.fail("b is modifiable"); } catch (MathUnsupportedOperationException exc){ // Expected behavior } try { evt.getSolution().set(0.0); Assert.fail("x is modifiable"); } catch (MathUnsupportedOperationException exc){ // Expected behavior } } public void initializationPerformed(final IterationEvent e) { ++count[0]; doTestVectorsAreUnmodifiable(e); } public void iterationPerformed(final IterationEvent e) { ++count[2]; Assert.assertEquals("iteration performed", count[2], e.getIterations() - 1); doTestVectorsAreUnmodifiable(e); } public void iterationStarted(final IterationEvent e) { ++count[1]; Assert.assertEquals("iteration started", count[1], e.getIterations() - 1); doTestVectorsAreUnmodifiable(e); } public void terminationPerformed(final IterationEvent e) { ++count[3]; doTestVectorsAreUnmodifiable(e); } }; solver = new ConjugateGradient(maxIterations, 1E-10, true); solver.getIterationManager().addIterationListener(listener); final RealVector b = new ArrayRealVector(n); for (int j = 0; j < n; j++) { Arrays.fill(count, 0); b.set(0.); b.setEntry(j, 1.); solver.solve(a, b); String msg = String.format("column %d (initialization)", j); Assert.assertEquals(msg, 1, count[0]); msg = String.format("column %d (finalization)", j); Assert.assertEquals(msg, 1, count[3]); } } @Test public void testUnpreconditionedNormOfResidual() { final int n = 5; final int maxIterations = 100; final RealLinearOperator a = new HilbertMatrix(n); final IterativeLinearSolver solver; final IterationListener listener = new IterationListener() { private void doTestNormOfResidual(final IterationEvent e) { final IterativeLinearSolverEvent evt; evt = (IterativeLinearSolverEvent) e; final RealVector x = evt.getSolution(); final RealVector b = evt.getRightHandSideVector(); final RealVector r = b.subtract(a.operate(x)); final double rnorm = r.getNorm(); Assert.assertEquals("iteration performed (residual)", rnorm, evt.getNormOfResidual(), FastMath.max(1E-5 * rnorm, 1E-10)); } public void initializationPerformed(final IterationEvent e) { doTestNormOfResidual(e); } public void iterationPerformed(final IterationEvent e) { doTestNormOfResidual(e); } public void iterationStarted(final IterationEvent e) { doTestNormOfResidual(e); } public void terminationPerformed(final IterationEvent e) { doTestNormOfResidual(e); } }; solver = new ConjugateGradient(maxIterations, 1E-10, true); solver.getIterationManager().addIterationListener(listener); final RealVector b = new ArrayRealVector(n); for (int j = 0; j < n; j++) { b.set(0.); b.setEntry(j, 1.); solver.solve(a, b); } } @Test public void testPreconditionedNormOfResidual() { final int n = 5; final int maxIterations = 100; final RealLinearOperator a = new HilbertMatrix(n); final RealLinearOperator m = JacobiPreconditioner.create(a); final PreconditionedIterativeLinearSolver solver; final IterationListener listener = new IterationListener() { private void doTestNormOfResidual(final IterationEvent e) { final IterativeLinearSolverEvent evt; evt = (IterativeLinearSolverEvent) e; final RealVector x = evt.getSolution(); final RealVector b = evt.getRightHandSideVector(); final RealVector r = b.subtract(a.operate(x)); final double rnorm = r.getNorm(); Assert.assertEquals("iteration performed (residual)", rnorm, evt.getNormOfResidual(), FastMath.max(1E-5 * rnorm, 1E-10)); } public void initializationPerformed(final IterationEvent e) { doTestNormOfResidual(e); } public void iterationPerformed(final IterationEvent e) { doTestNormOfResidual(e); } public void iterationStarted(final IterationEvent e) { doTestNormOfResidual(e); } public void terminationPerformed(final IterationEvent e) { doTestNormOfResidual(e); } }; solver = new ConjugateGradient(maxIterations, 1E-10, true); solver.getIterationManager().addIterationListener(listener); final RealVector b = new ArrayRealVector(n); for (int j = 0; j < n; j++) { b.set(0.); b.setEntry(j, 1.); solver.solve(a, m, b); } } } commons-math3-3.2-src/src/test/java/org/apache/commons/math3/linear/MatrixUtilsTest.java100644 1750 1750 41152 12126627674 30051 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.linear; import java.math.BigDecimal; import org.apache.commons.math3.TestUtils; import org.apache.commons.math3.fraction.BigFraction; import org.apache.commons.math3.fraction.Fraction; import org.apache.commons.math3.fraction.FractionConversionException; import org.apache.commons.math3.fraction.FractionField; import org.apache.commons.math3.exception.NullArgumentException; import org.apache.commons.math3.exception.NotStrictlyPositiveException; import org.apache.commons.math3.exception.MathIllegalArgumentException; import org.junit.Assert; import org.junit.Test; /** * Test cases for the {@link MatrixUtils} class. * * @version $Id: MatrixUtilsTest.java 1403587 2012-10-30 00:21:20Z erans $ */ public final class MatrixUtilsTest { protected double[][] testData = { {1d,2d,3d}, {2d,5d,3d}, {1d,0d,8d} }; protected double[][] nullMatrix = null; protected double[] row = {1,2,3}; protected BigDecimal[] bigRow = {new BigDecimal(1),new BigDecimal(2),new BigDecimal(3)}; protected String[] stringRow = {"1", "2", "3"}; protected Fraction[] fractionRow = {new Fraction(1),new Fraction(2),new Fraction(3)}; protected double[][] rowMatrix = {{1,2,3}}; protected BigDecimal[][] bigRowMatrix = {{new BigDecimal(1), new BigDecimal(2), new BigDecimal(3)}}; protected String[][] stringRowMatrix = {{"1", "2", "3"}}; protected Fraction[][] fractionRowMatrix = {{new Fraction(1), new Fraction(2), new Fraction(3)}}; protected double[] col = {0,4,6}; protected BigDecimal[] bigCol = {new BigDecimal(0),new BigDecimal(4),new BigDecimal(6)}; protected String[] stringCol = {"0","4","6"}; protected Fraction[] fractionCol = {new Fraction(0),new Fraction(4),new Fraction(6)}; protected double[] nullDoubleArray = null; protected double[][] colMatrix = {{0},{4},{6}}; protected BigDecimal[][] bigColMatrix = {{new BigDecimal(0)},{new BigDecimal(4)},{new BigDecimal(6)}}; protected String[][] stringColMatrix = {{"0"}, {"4"}, {"6"}}; protected Fraction[][] fractionColMatrix = {{new Fraction(0)},{new Fraction(4)},{new Fraction(6)}}; @Test public void testCreateRealMatrix() { Assert.assertEquals(new BlockRealMatrix(testData), MatrixUtils.createRealMatrix(testData)); try { MatrixUtils.createRealMatrix(new double[][] {{1}, {1,2}}); // ragged Assert.fail("Expecting MathIllegalArgumentException"); } catch (MathIllegalArgumentException ex) { // expected } try { MatrixUtils.createRealMatrix(new double[][] {{}, {}}); // no columns Assert.fail("Expecting MathIllegalArgumentException"); } catch (MathIllegalArgumentException ex) { // expected } try { MatrixUtils.createRealMatrix(null); // null Assert.fail("Expecting NullArgumentException"); } catch (NullArgumentException ex) { // expected } } @Test public void testcreateFieldMatrix() { Assert.assertEquals(new Array2DRowFieldMatrix(asFraction(testData)), MatrixUtils.createFieldMatrix(asFraction(testData))); Assert.assertEquals(new Array2DRowFieldMatrix(FractionField.getInstance(), fractionColMatrix), MatrixUtils.createFieldMatrix(fractionColMatrix)); try { MatrixUtils.createFieldMatrix(asFraction(new double[][] {{1}, {1,2}})); // ragged Assert.fail("Expecting MathIllegalArgumentException"); } catch (MathIllegalArgumentException ex) { // expected } try { MatrixUtils.createFieldMatrix(asFraction(new double[][] {{}, {}})); // no columns Assert.fail("Expecting MathIllegalArgumentException"); } catch (MathIllegalArgumentException ex) { // expected } try { MatrixUtils.createFieldMatrix((Fraction[][])null); // null Assert.fail("Expecting NullArgumentException"); } catch (NullArgumentException ex) { // expected } } @Test public void testCreateRowRealMatrix() { Assert.assertEquals(MatrixUtils.createRowRealMatrix(row), new BlockRealMatrix(rowMatrix)); try { MatrixUtils.createRowRealMatrix(new double[] {}); // empty Assert.fail("Expecting NotStrictlyPositiveException"); } catch (NotStrictlyPositiveException ex) { // expected } try { MatrixUtils.createRowRealMatrix(null); // null Assert.fail("Expecting NullArgumentException"); } catch (NullArgumentException ex) { // expected } } @Test public void testCreateRowFieldMatrix() { Assert.assertEquals(MatrixUtils.createRowFieldMatrix(asFraction(row)), new Array2DRowFieldMatrix(asFraction(rowMatrix))); Assert.assertEquals(MatrixUtils.createRowFieldMatrix(fractionRow), new Array2DRowFieldMatrix(fractionRowMatrix)); try { MatrixUtils.createRowFieldMatrix(new Fraction[] {}); // empty Assert.fail("Expecting MathIllegalArgumentException"); } catch (MathIllegalArgumentException ex) { // expected } try { MatrixUtils.createRowFieldMatrix((Fraction[]) null); // null Assert.fail("Expecting NullArgumentException"); } catch (NullArgumentException ex) { // expected } } @Test public void testCreateColumnRealMatrix() { Assert.assertEquals(MatrixUtils.createColumnRealMatrix(col), new BlockRealMatrix(colMatrix)); try { MatrixUtils.createColumnRealMatrix(new double[] {}); // empty Assert.fail("Expecting MathIllegalArgumentException"); } catch (MathIllegalArgumentException ex) { // expected } try { MatrixUtils.createColumnRealMatrix(null); // null Assert.fail("Expecting NullArgumentException"); } catch (NullArgumentException ex) { // expected } } @Test public void testCreateColumnFieldMatrix() { Assert.assertEquals(MatrixUtils.createColumnFieldMatrix(asFraction(col)), new Array2DRowFieldMatrix(asFraction(colMatrix))); Assert.assertEquals(MatrixUtils.createColumnFieldMatrix(fractionCol), new Array2DRowFieldMatrix(fractionColMatrix)); try { MatrixUtils.createColumnFieldMatrix(new Fraction[] {}); // empty Assert.fail("Expecting MathIllegalArgumentException"); } catch (MathIllegalArgumentException ex) { // expected } try { MatrixUtils.createColumnFieldMatrix((Fraction[]) null); // null Assert.fail("Expecting NullArgumentException"); } catch (NullArgumentException ex) { // expected } } /** * Verifies that the matrix is an identity matrix */ protected void checkIdentityMatrix(RealMatrix m) { for (int i = 0; i < m.getRowDimension(); i++) { for (int j =0; j < m.getColumnDimension(); j++) { if (i == j) { Assert.assertEquals(m.getEntry(i, j), 1d, 0); } else { Assert.assertEquals(m.getEntry(i, j), 0d, 0); } } } } @Test public void testCreateIdentityMatrix() { checkIdentityMatrix(MatrixUtils.createRealIdentityMatrix(3)); checkIdentityMatrix(MatrixUtils.createRealIdentityMatrix(2)); checkIdentityMatrix(MatrixUtils.createRealIdentityMatrix(1)); try { MatrixUtils.createRealIdentityMatrix(0); Assert.fail("Expecting MathIllegalArgumentException"); } catch (MathIllegalArgumentException ex) { // expected } } /** * Verifies that the matrix is an identity matrix */ protected void checkIdentityFieldMatrix(FieldMatrix m) { for (int i = 0; i < m.getRowDimension(); i++) { for (int j =0; j < m.getColumnDimension(); j++) { if (i == j) { Assert.assertEquals(m.getEntry(i, j), Fraction.ONE); } else { Assert.assertEquals(m.getEntry(i, j), Fraction.ZERO); } } } } @Test public void testcreateFieldIdentityMatrix() { checkIdentityFieldMatrix(MatrixUtils.createFieldIdentityMatrix(FractionField.getInstance(), 3)); checkIdentityFieldMatrix(MatrixUtils.createFieldIdentityMatrix(FractionField.getInstance(), 2)); checkIdentityFieldMatrix(MatrixUtils.createFieldIdentityMatrix(FractionField.getInstance(), 1)); try { MatrixUtils.createRealIdentityMatrix(0); Assert.fail("Expecting MathIllegalArgumentException"); } catch (MathIllegalArgumentException ex) { // expected } } @Test public void testBigFractionConverter() { BigFraction[][] bfData = { { new BigFraction(1), new BigFraction(2), new BigFraction(3) }, { new BigFraction(2), new BigFraction(5), new BigFraction(3) }, { new BigFraction(1), new BigFraction(0), new BigFraction(8) } }; FieldMatrix m = new Array2DRowFieldMatrix(bfData, false); RealMatrix converted = MatrixUtils.bigFractionMatrixToRealMatrix(m); RealMatrix reference = new Array2DRowRealMatrix(testData, false); Assert.assertEquals(0.0, converted.subtract(reference).getNorm(), 0.0); } @Test public void testFractionConverter() { Fraction[][] fData = { { new Fraction(1), new Fraction(2), new Fraction(3) }, { new Fraction(2), new Fraction(5), new Fraction(3) }, { new Fraction(1), new Fraction(0), new Fraction(8) } }; FieldMatrix m = new Array2DRowFieldMatrix(fData, false); RealMatrix converted = MatrixUtils.fractionMatrixToRealMatrix(m); RealMatrix reference = new Array2DRowRealMatrix(testData, false); Assert.assertEquals(0.0, converted.subtract(reference).getNorm(), 0.0); } public static final Fraction[][] asFraction(double[][] data) { Fraction d[][] = new Fraction[data.length][]; try { for (int i = 0; i < data.length; ++i) { double[] dataI = data[i]; Fraction[] dI = new Fraction[dataI.length]; for (int j = 0; j < dataI.length; ++j) { dI[j] = new Fraction(dataI[j]); } d[i] = dI; } } catch (FractionConversionException fce) { Assert.fail(fce.getMessage()); } return d; } public static final Fraction[] asFraction(double[] data) { Fraction d[] = new Fraction[data.length]; try { for (int i = 0; i < data.length; ++i) { d[i] = new Fraction(data[i]); } } catch (FractionConversionException fce) { Assert.fail(fce.getMessage()); } return d; } @Test public void testSolveLowerTriangularSystem(){ RealMatrix rm = new Array2DRowRealMatrix( new double[][] { {2,0,0,0 }, { 1,1,0,0 }, { 3,3,3,0 }, { 3,3,3,4 } }, false); RealVector b = new ArrayRealVector(new double[] { 2,3,4,8 }, false); MatrixUtils.solveLowerTriangularSystem(rm, b); TestUtils.assertEquals( new double[]{1,2,-1.66666666666667, 1.0} , b.toArray() , 1.0e-12); } /* * Taken from R manual http://stat.ethz.ch/R-manual/R-patched/library/base/html/backsolve.html */ @Test public void testSolveUpperTriangularSystem(){ RealMatrix rm = new Array2DRowRealMatrix( new double[][] { {1,2,3 }, { 0,1,1 }, { 0,0,2 } }, false); RealVector b = new ArrayRealVector(new double[] { 8,4,2 }, false); MatrixUtils.solveUpperTriangularSystem(rm, b); TestUtils.assertEquals( new double[]{-1,3,1} , b.toArray() , 1.0e-12); } /** * This test should probably be replaced by one that could show * whether this algorithm can sometimes perform better (precision- or * performance-wise) than the direct inversion of the whole matrix. */ @Test public void testBlockInverse() { final double[][] data = { { -1, 0, 123, 4 }, { -56, 78.9, -0.1, -23.4 }, { 5.67, 8, -9, 1011 }, { 12, 345, -67.8, 9 }, }; final RealMatrix m = new Array2DRowRealMatrix(data); final int len = data.length; final double tol = 1e-14; for (int splitIndex = 0; splitIndex < 3; splitIndex++) { final RealMatrix mInv = MatrixUtils.blockInverse(m, splitIndex); final RealMatrix id = m.multiply(mInv); // Check that we recovered the identity matrix. for (int i = 0; i < len; i++) { for (int j = 0; j < len; j++) { final double entry = id.getEntry(i, j); if (i == j) { Assert.assertEquals("[" + i + "][" + j + "]", 1, entry, tol); } else { Assert.assertEquals("[" + i + "][" + j + "]", 0, entry, tol); } } } } } @Test public void testIsSymmetric() { final double eps = Math.ulp(1d); final double[][] dataSym = { { 1, 2, 3 }, { 2, 2, 5 }, { 3, 5, 6 }, }; Assert.assertTrue(MatrixUtils.isSymmetric(MatrixUtils.createRealMatrix(dataSym), eps)); final double[][] dataNonSym = { { 1, 2, -3 }, { 2, 2, 5 }, { 3, 5, 6 }, }; Assert.assertFalse(MatrixUtils.isSymmetric(MatrixUtils.createRealMatrix(dataNonSym), eps)); } @Test public void testIsSymmetricTolerance() { final double eps = 1e-4; final double[][] dataSym1 = { { 1, 1, 1.00009 }, { 1, 1, 1 }, { 1.0, 1, 1 }, }; Assert.assertTrue(MatrixUtils.isSymmetric(MatrixUtils.createRealMatrix(dataSym1), eps)); final double[][] dataSym2 = { { 1, 1, 0.99990 }, { 1, 1, 1 }, { 1.0, 1, 1 }, }; Assert.assertTrue(MatrixUtils.isSymmetric(MatrixUtils.createRealMatrix(dataSym2), eps)); final double[][] dataNonSym1 = { { 1, 1, 1.00011 }, { 1, 1, 1 }, { 1.0, 1, 1 }, }; Assert.assertFalse(MatrixUtils.isSymmetric(MatrixUtils.createRealMatrix(dataNonSym1), eps)); final double[][] dataNonSym2 = { { 1, 1, 0.99989 }, { 1, 1, 1 }, { 1.0, 1, 1 }, }; Assert.assertFalse(MatrixUtils.isSymmetric(MatrixUtils.createRealMatrix(dataNonSym2), eps)); } @Test public void testCheckSymmetric1() { final double[][] dataSym = { { 1, 2, 3 }, { 2, 2, 5 }, { 3, 5, 6 }, }; MatrixUtils.checkSymmetric(MatrixUtils.createRealMatrix(dataSym), Math.ulp(1d)); } @Test(expected=NonSymmetricMatrixException.class) public void testCheckSymmetric2() { final double[][] dataNonSym = { { 1, 2, -3 }, { 2, 2, 5 }, { 3, 5, 6 }, }; MatrixUtils.checkSymmetric(MatrixUtils.createRealMatrix(dataNonSym), Math.ulp(1d)); } } commons-math3-3.2-src/src/test/java/org/apache/commons/math3/ml/distance/ChebyshevDistanceTest.java100644 1750 1750 3035 12126627673 32064 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.ml.distance; import org.junit.Assert; import org.junit.Test; import org.apache.commons.math3.util.FastMath; /** * Tests for {@link ChebyshevDistance} class. */ public class ChebyshevDistanceTest { final DistanceMeasure distance = new ChebyshevDistance(); @Test public void testZero() { final double[] a = { 0, 1, -2, 3.4, 5, -6.7, 89 }; Assert.assertEquals(0, distance.compute(a, a), 0d); } @Test public void test() { final double[] a = { 1, 2, 3, 4 }; final double[] b = { -5, -6, 7, 8 }; final double expected = 8; Assert.assertEquals(expected, distance.compute(a, b), 0d); Assert.assertEquals(expected, distance.compute(b, a), 0d); } } commons-math3-3.2-src/src/test/java/org/apache/commons/math3/ml/distance/EuclideanDistanceTest.java100644 1750 1750 3056 12126627673 32040 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.ml.distance; import org.junit.Assert; import org.junit.Test; import org.apache.commons.math3.util.FastMath; /** * Tests for {@link EuclideanDistance} class. */ public class EuclideanDistanceTest { final DistanceMeasure distance = new EuclideanDistance(); @Test public void testZero() { final double[] a = { 0, 1, -2, 3.4, 5, -6.7, 89 }; Assert.assertEquals(0, distance.compute(a, a), 0d); } @Test public void test() { final double[] a = { 1, -2, 3, 4 }; final double[] b = { -5, -6, 7, 8 }; final double expected = FastMath.sqrt(84); Assert.assertEquals(expected, distance.compute(a, b), 0d); Assert.assertEquals(expected, distance.compute(b, a), 0d); } } commons-math3-3.2-src/src/test/java/org/apache/commons/math3/ml/distance/ManhattanDistanceTest.java100644 1750 1750 2760 12126627673 32063 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.ml.distance; import org.junit.Assert; import org.junit.Test; /** * Tests for {@link ManhattanDistance} class. */ public class ManhattanDistanceTest { final DistanceMeasure distance = new ManhattanDistance(); @Test public void testZero() { final double[] a = { 0, 1, -2, 3.4, 5, -6.7, 89 }; Assert.assertEquals(0, distance.compute(a, a), 0d); } @Test public void test() { final double[] a = { 1, -2, 3, 4 }; final double[] b = { -5, -6, 7, 8 }; final double expected = 18; Assert.assertEquals(expected, distance.compute(a, b), 0d); Assert.assertEquals(expected, distance.compute(b, a), 0d); } } commons-math3-3.2-src/src/test/java/org/apache/commons/math3/ml/distance/CanberraDistanceTest.java100644 1750 1750 3203 12126627673 31656 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.ml.distance; import org.junit.Assert; import org.junit.Test; /** * Tests for {@link CanberraDistance} class. */ public class CanberraDistanceTest { final DistanceMeasure distance = new CanberraDistance(); @Test public void testZero() { final double[] a = { 0, 1, -2, 3.4, 5, -6.7, 89 }; Assert.assertEquals(0, distance.compute(a, a), 0d); } @Test public void testZero2() { final double[] a = { 0, 0 }; Assert.assertEquals(0, distance.compute(a, a), 0d); } @Test public void test() { final double[] a = { 1, 2, 3, 4, 9 }; final double[] b = { -5, -6, 7, 4, 3 }; final double expected = 2.9; Assert.assertEquals(expected, distance.compute(a, b), 0d); Assert.assertEquals(expected, distance.compute(b, a), 0d); } } ././@LongLink100644 0 0 154 12126630646 10261 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/ml/clustering/KMeansPlusPlusClustererTest.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/ml/clustering/KMeansPlusPlusClustererTe100644 1750 1750 16553 12126627673 32377 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.ml.clustering; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; import java.util.List; import org.apache.commons.math3.exception.NumberIsTooSmallException; import org.apache.commons.math3.ml.distance.EuclideanDistance; import org.apache.commons.math3.random.JDKRandomGenerator; import org.apache.commons.math3.random.RandomGenerator; import org.junit.Assert; import org.junit.Before; import org.junit.Test; public class KMeansPlusPlusClustererTest { private RandomGenerator random; @Before public void setUp() { random = new JDKRandomGenerator(); random.setSeed(1746432956321l); } /** * JIRA: MATH-305 * * Two points, one cluster, one iteration */ @Test public void testPerformClusterAnalysisDegenerate() { KMeansPlusPlusClusterer transformer = new KMeansPlusPlusClusterer(1, 1); DoublePoint[] points = new DoublePoint[] { new DoublePoint(new int[] { 1959, 325100 }), new DoublePoint(new int[] { 1960, 373200 }), }; List> clusters = transformer.cluster(Arrays.asList(points)); Assert.assertEquals(1, clusters.size()); Assert.assertEquals(2, (clusters.get(0).getPoints().size())); DoublePoint pt1 = new DoublePoint(new int[] { 1959, 325100 }); DoublePoint pt2 = new DoublePoint(new int[] { 1960, 373200 }); Assert.assertTrue(clusters.get(0).getPoints().contains(pt1)); Assert.assertTrue(clusters.get(0).getPoints().contains(pt2)); } @Test public void testCertainSpace() { KMeansPlusPlusClusterer.EmptyClusterStrategy[] strategies = { KMeansPlusPlusClusterer.EmptyClusterStrategy.LARGEST_VARIANCE, KMeansPlusPlusClusterer.EmptyClusterStrategy.LARGEST_POINTS_NUMBER, KMeansPlusPlusClusterer.EmptyClusterStrategy.FARTHEST_POINT }; for (KMeansPlusPlusClusterer.EmptyClusterStrategy strategy : strategies) { int numberOfVariables = 27; // initialise testvalues int position1 = 1; int position2 = position1 + numberOfVariables; int position3 = position2 + numberOfVariables; int position4 = position3 + numberOfVariables; // testvalues will be multiplied int multiplier = 1000000; DoublePoint[] breakingPoints = new DoublePoint[numberOfVariables]; // define the space which will break the cluster algorithm for (int i = 0; i < numberOfVariables; i++) { int points[] = { position1, position2, position3, position4 }; // multiply the values for (int j = 0; j < points.length; j++) { points[j] = points[j] * multiplier; } DoublePoint DoublePoint = new DoublePoint(points); breakingPoints[i] = DoublePoint; position1 = position1 + numberOfVariables; position2 = position2 + numberOfVariables; position3 = position3 + numberOfVariables; position4 = position4 + numberOfVariables; } for (int n = 2; n < 27; ++n) { KMeansPlusPlusClusterer transformer = new KMeansPlusPlusClusterer(n, 100, new EuclideanDistance(), random, strategy); List> clusters = transformer.cluster(Arrays.asList(breakingPoints)); Assert.assertEquals(n, clusters.size()); int sum = 0; for (Cluster cluster : clusters) { sum += cluster.getPoints().size(); } Assert.assertEquals(numberOfVariables, sum); } } } /** * A helper class for testSmallDistances(). This class is similar to DoublePoint, but * it defines a different distanceFrom() method that tends to return distances less than 1. */ private class CloseDistance extends EuclideanDistance { private static final long serialVersionUID = 1L; @Override public double compute(double[] a, double[] b) { return super.compute(a, b) * 0.001; } } /** * Test points that are very close together. See issue MATH-546. */ @Test public void testSmallDistances() { // Create a bunch of CloseDoublePoints. Most are identical, but one is different by a // small distance. int[] repeatedArray = { 0 }; int[] uniqueArray = { 1 }; DoublePoint repeatedPoint = new DoublePoint(repeatedArray); DoublePoint uniquePoint = new DoublePoint(uniqueArray); Collection points = new ArrayList(); final int NUM_REPEATED_POINTS = 10 * 1000; for (int i = 0; i < NUM_REPEATED_POINTS; ++i) { points.add(repeatedPoint); } points.add(uniquePoint); // Ask a KMeansPlusPlusClusterer to run zero iterations (i.e., to simply choose initial // cluster centers). final long RANDOM_SEED = 0; final int NUM_CLUSTERS = 2; final int NUM_ITERATIONS = 0; random.setSeed(RANDOM_SEED); KMeansPlusPlusClusterer clusterer = new KMeansPlusPlusClusterer(NUM_CLUSTERS, NUM_ITERATIONS, new CloseDistance(), random); List> clusters = clusterer.cluster(points); // Check that one of the chosen centers is the unique point. boolean uniquePointIsCenter = false; for (CentroidCluster cluster : clusters) { if (cluster.getCenter().equals(uniquePoint)) { uniquePointIsCenter = true; } } Assert.assertTrue(uniquePointIsCenter); } /** * 2 variables cannot be clustered into 3 clusters. See issue MATH-436. */ @Test(expected=NumberIsTooSmallException.class) public void testPerformClusterAnalysisToManyClusters() { KMeansPlusPlusClusterer transformer = new KMeansPlusPlusClusterer(3, 1, new EuclideanDistance(), random); DoublePoint[] points = new DoublePoint[] { new DoublePoint(new int[] { 1959, 325100 }), new DoublePoint(new int[] { 1960, 373200 }) }; transformer.cluster(Arrays.asList(points)); } } ././@LongLink100644 0 0 161 12126630646 10257 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/ml/clustering/MultiKMeansPlusPlusClustererTest.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/ml/clustering/MultiKMeansPlusPlusCluste100644 1750 1750 7632 12126627673 32366 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.ml.clustering; import java.util.Arrays; import java.util.List; import org.junit.Assert; import org.junit.Test; public class MultiKMeansPlusPlusClustererTest { @Test public void dimension2() { MultiKMeansPlusPlusClusterer transformer = new MultiKMeansPlusPlusClusterer( new KMeansPlusPlusClusterer(3, 10), 5); DoublePoint[] points = new DoublePoint[] { // first expected cluster new DoublePoint(new int[] { -15, 3 }), new DoublePoint(new int[] { -15, 4 }), new DoublePoint(new int[] { -15, 5 }), new DoublePoint(new int[] { -14, 3 }), new DoublePoint(new int[] { -14, 5 }), new DoublePoint(new int[] { -13, 3 }), new DoublePoint(new int[] { -13, 4 }), new DoublePoint(new int[] { -13, 5 }), // second expected cluster new DoublePoint(new int[] { -1, 0 }), new DoublePoint(new int[] { -1, -1 }), new DoublePoint(new int[] { 0, -1 }), new DoublePoint(new int[] { 1, -1 }), new DoublePoint(new int[] { 1, -2 }), // third expected cluster new DoublePoint(new int[] { 13, 3 }), new DoublePoint(new int[] { 13, 4 }), new DoublePoint(new int[] { 14, 4 }), new DoublePoint(new int[] { 14, 7 }), new DoublePoint(new int[] { 16, 5 }), new DoublePoint(new int[] { 16, 6 }), new DoublePoint(new int[] { 17, 4 }), new DoublePoint(new int[] { 17, 7 }) }; List> clusters = transformer.cluster(Arrays.asList(points)); Assert.assertEquals(3, clusters.size()); boolean cluster1Found = false; boolean cluster2Found = false; boolean cluster3Found = false; double epsilon = 1e-6; for (CentroidCluster cluster : clusters) { Clusterable center = cluster.getCenter(); double[] point = center.getPoint(); if (point[0] < 0) { cluster1Found = true; Assert.assertEquals(8, cluster.getPoints().size()); Assert.assertEquals(-14, point[0], epsilon); Assert.assertEquals( 4, point[1], epsilon); } else if (point[1] < 0) { cluster2Found = true; Assert.assertEquals(5, cluster.getPoints().size()); Assert.assertEquals( 0, point[0], epsilon); Assert.assertEquals(-1, point[1], epsilon); } else { cluster3Found = true; Assert.assertEquals(8, cluster.getPoints().size()); Assert.assertEquals(15, point[0], epsilon); Assert.assertEquals(5, point[1], epsilon); } } Assert.assertTrue(cluster1Found); Assert.assertTrue(cluster2Found); Assert.assertTrue(cluster3Found); } } commons-math3-3.2-src/src/test/java/org/apache/commons/math3/ml/clustering/DBSCANClustererTest.java100644 1750 1750 25664 12126627673 31735 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.ml.clustering; import java.util.Arrays; import java.util.List; import org.apache.commons.math3.exception.MathIllegalArgumentException; import org.apache.commons.math3.exception.NullArgumentException; import org.junit.Assert; import org.junit.Test; public class DBSCANClustererTest { @Test public void testCluster() { // Test data generated using: http://people.cs.nctu.edu.tw/~rsliang/dbscan/testdatagen.html final DoublePoint[] points = new DoublePoint[] { new DoublePoint(new double[] { 83.08303244924173, 58.83387754182331 }), new DoublePoint(new double[] { 45.05445510940626, 23.469642649637535 }), new DoublePoint(new double[] { 14.96417921432294, 69.0264096390456 }), new DoublePoint(new double[] { 73.53189604333602, 34.896145021310076 }), new DoublePoint(new double[] { 73.28498173551634, 33.96860806993209 }), new DoublePoint(new double[] { 73.45828098873608, 33.92584423092194 }), new DoublePoint(new double[] { 73.9657889183145, 35.73191006924026 }), new DoublePoint(new double[] { 74.0074097183533, 36.81735596177168 }), new DoublePoint(new double[] { 73.41247541410848, 34.27314856695011 }), new DoublePoint(new double[] { 73.9156256353017, 36.83206791547127 }), new DoublePoint(new double[] { 74.81499205809087, 37.15682749846019 }), new DoublePoint(new double[] { 74.03144880081527, 37.57399178552441 }), new DoublePoint(new double[] { 74.51870941207744, 38.674258946906775 }), new DoublePoint(new double[] { 74.50754595105536, 35.58903978415765 }), new DoublePoint(new double[] { 74.51322752749547, 36.030572259100154 }), new DoublePoint(new double[] { 59.27900996617973, 46.41091720294207 }), new DoublePoint(new double[] { 59.73744793841615, 46.20015558367595 }), new DoublePoint(new double[] { 58.81134076672606, 45.71150126331486 }), new DoublePoint(new double[] { 58.52225539437495, 47.416083617601544 }), new DoublePoint(new double[] { 58.218626647023484, 47.36228902172297 }), new DoublePoint(new double[] { 60.27139669447206, 46.606106348801404 }), new DoublePoint(new double[] { 60.894962462363765, 46.976924697402865 }), new DoublePoint(new double[] { 62.29048673878424, 47.66970563563518 }), new DoublePoint(new double[] { 61.03857608977705, 46.212924720020965 }), new DoublePoint(new double[] { 60.16916214139201, 45.18193661351688 }), new DoublePoint(new double[] { 59.90036905976012, 47.555364347063005 }), new DoublePoint(new double[] { 62.33003634144552, 47.83941489877179 }), new DoublePoint(new double[] { 57.86035536718555, 47.31117930193432 }), new DoublePoint(new double[] { 58.13715479685925, 48.985960494028404 }), new DoublePoint(new double[] { 56.131923963548616, 46.8508904252667 }), new DoublePoint(new double[] { 55.976329887053, 47.46384037658572 }), new DoublePoint(new double[] { 56.23245975235477, 47.940035191131756 }), new DoublePoint(new double[] { 58.51687048212625, 46.622885352699086 }), new DoublePoint(new double[] { 57.85411081905477, 45.95394361577928 }), new DoublePoint(new double[] { 56.445776311447844, 45.162093662656844 }), new DoublePoint(new double[] { 57.36691949656233, 47.50097194337286 }), new DoublePoint(new double[] { 58.243626387557015, 46.114052729681134 }), new DoublePoint(new double[] { 56.27224595635198, 44.799080066150054 }), new DoublePoint(new double[] { 57.606924816500396, 46.94291057763621 }), new DoublePoint(new double[] { 30.18714230041951, 13.877149710431695 }), new DoublePoint(new double[] { 30.449448810657486, 13.490778346545994 }), new DoublePoint(new double[] { 30.295018390286714, 13.264889000216499 }), new DoublePoint(new double[] { 30.160201832884923, 11.89278262341395 }), new DoublePoint(new double[] { 31.341509791789576, 15.282655921997502 }), new DoublePoint(new double[] { 31.68601630325429, 14.756873246748 }), new DoublePoint(new double[] { 29.325963742565364, 12.097849250072613 }), new DoublePoint(new double[] { 29.54820742388256, 13.613295356975868 }), new DoublePoint(new double[] { 28.79359608888626, 10.36352064087987 }), new DoublePoint(new double[] { 31.01284597092308, 12.788479208014905 }), new DoublePoint(new double[] { 27.58509216737002, 11.47570110601373 }), new DoublePoint(new double[] { 28.593799561727792, 10.780998203903437 }), new DoublePoint(new double[] { 31.356105766724795, 15.080316198524088 }), new DoublePoint(new double[] { 31.25948503636755, 13.674329151166603 }), new DoublePoint(new double[] { 32.31590076372959, 14.95261758659035 }), new DoublePoint(new double[] { 30.460413702763617, 15.88402809202671 }), new DoublePoint(new double[] { 32.56178203062154, 14.586076852632686 }), new DoublePoint(new double[] { 32.76138648530468, 16.239837325178087 }), new DoublePoint(new double[] { 30.1829453331884, 14.709592407103628 }), new DoublePoint(new double[] { 29.55088173528202, 15.0651247180067 }), new DoublePoint(new double[] { 29.004155302187428, 14.089665298582986 }), new DoublePoint(new double[] { 29.339624439831823, 13.29096065578051 }), new DoublePoint(new double[] { 30.997460327576846, 14.551914158277214 }), new DoublePoint(new double[] { 30.66784126125276, 16.269703107886016 }) }; final DBSCANClusterer transformer = new DBSCANClusterer(2.0, 5); final List> clusters = transformer.cluster(Arrays.asList(points)); final List clusterOne = Arrays.asList(points[3], points[4], points[5], points[6], points[7], points[8], points[9], points[10], points[11], points[12], points[13], points[14]); final List clusterTwo = Arrays.asList(points[15], points[16], points[17], points[18], points[19], points[20], points[21], points[22], points[23], points[24], points[25], points[26], points[27], points[28], points[29], points[30], points[31], points[32], points[33], points[34], points[35], points[36], points[37], points[38]); final List clusterThree = Arrays.asList(points[39], points[40], points[41], points[42], points[43], points[44], points[45], points[46], points[47], points[48], points[49], points[50], points[51], points[52], points[53], points[54], points[55], points[56], points[57], points[58], points[59], points[60], points[61], points[62]); boolean cluster1Found = false; boolean cluster2Found = false; boolean cluster3Found = false; Assert.assertEquals(3, clusters.size()); for (final Cluster cluster : clusters) { if (cluster.getPoints().containsAll(clusterOne)) { cluster1Found = true; } if (cluster.getPoints().containsAll(clusterTwo)) { cluster2Found = true; } if (cluster.getPoints().containsAll(clusterThree)) { cluster3Found = true; } } Assert.assertTrue(cluster1Found); Assert.assertTrue(cluster2Found); Assert.assertTrue(cluster3Found); } @Test public void testSingleLink() { final DoublePoint[] points = { new DoublePoint(new int[] {10, 10}), // A new DoublePoint(new int[] {12, 9}), new DoublePoint(new int[] {10, 8}), new DoublePoint(new int[] {8, 8}), new DoublePoint(new int[] {8, 6}), new DoublePoint(new int[] {7, 7}), new DoublePoint(new int[] {5, 6}), // B new DoublePoint(new int[] {14, 8}), // C new DoublePoint(new int[] {7, 15}), // N - Noise, should not be present new DoublePoint(new int[] {17, 8}), // D - single-link connected to C should not be present }; final DBSCANClusterer clusterer = new DBSCANClusterer(3, 3); List> clusters = clusterer.cluster(Arrays.asList(points)); Assert.assertEquals(1, clusters.size()); final List clusterOne = Arrays.asList(points[0], points[1], points[2], points[3], points[4], points[5], points[6], points[7]); Assert.assertTrue(clusters.get(0).getPoints().containsAll(clusterOne)); } @Test public void testGetEps() { final DBSCANClusterer transformer = new DBSCANClusterer(2.0, 5); Assert.assertEquals(2.0, transformer.getEps(), 0.0); } @Test public void testGetMinPts() { final DBSCANClusterer transformer = new DBSCANClusterer(2.0, 5); Assert.assertEquals(5, transformer.getMinPts()); } @Test(expected = MathIllegalArgumentException.class) public void testNegativeEps() { new DBSCANClusterer(-2.0, 5); } @Test(expected = MathIllegalArgumentException.class) public void testNegativeMinPts() { new DBSCANClusterer(2.0, -5); } @Test(expected = NullArgumentException.class) public void testNullDataset() { DBSCANClusterer clusterer = new DBSCANClusterer(2.0, 5); clusterer.cluster(null); } } commons-math3-3.2-src/src/test/java/org/apache/commons/math3/dfp/DfpMathTest.java100644 1750 1750 44527 12126627673 26416 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.dfp; import org.junit.After; import org.junit.Assert; import org.junit.Before; import org.junit.Test; public class DfpMathTest { private DfpField factory; private Dfp pinf; private Dfp ninf; private Dfp nan; private Dfp qnan; @Before public void setUp() { // Some basic setup. Define some constants and clear the status flags factory = new DfpField(20); pinf = factory.newDfp("1").divide(factory.newDfp("0")); ninf = factory.newDfp("-1").divide(factory.newDfp("0")); nan = factory.newDfp("0").divide(factory.newDfp("0")); qnan = factory.newDfp((byte)1, Dfp.QNAN); ninf.getField().clearIEEEFlags(); // force loading of dfpmath Dfp pi = factory.getPi(); pi.getField().clearIEEEFlags(); } @After public void tearDown() { pinf = null; ninf = null; nan = null; qnan = null; } // Generic test function. Takes params x and y and tests them for // equality. Then checks the status flags against the flags argument. // If the test fail, it prints the desc string private void test(Dfp x, Dfp y, int flags, String desc) { boolean b = x.equals(y); if (!x.equals(y) && !x.unequal(y)) // NaNs involved b = (x.toString().equals(y.toString())); if (x.equals(factory.newDfp("0"))) // distinguish +/- zero b = (b && (x.toString().equals(y.toString()))); b = (b && x.getField().getIEEEFlags() == flags); if (!b) Assert.assertTrue("assersion failed "+desc+" x = "+x.toString()+" flags = "+x.getField().getIEEEFlags(), b); x.getField().clearIEEEFlags(); } @Test public void testPow() { // Test special cases exponent of zero test(DfpMath.pow(factory.newDfp("0"), factory.newDfp("0")), factory.newDfp("1"), 0, "pow #1"); test(DfpMath.pow(factory.newDfp("0"), factory.newDfp("-0")), factory.newDfp("1"), 0, "pow #2"); test(DfpMath.pow(factory.newDfp("2"), factory.newDfp("0")), factory.newDfp("1"), 0, "pow #3"); test(DfpMath.pow(factory.newDfp("-2"), factory.newDfp("-0")), factory.newDfp("1"), 0, "pow #4"); test(DfpMath.pow(pinf, factory.newDfp("-0")), factory.newDfp("1"), 0, "pow #5"); test(DfpMath.pow(pinf, factory.newDfp("0")), factory.newDfp("1"), 0, "pow #6"); test(DfpMath.pow(ninf, factory.newDfp("-0")), factory.newDfp("1"), 0, "pow #7"); test(DfpMath.pow(ninf, factory.newDfp("0")), factory.newDfp("1"), 0, "pow #8"); test(DfpMath.pow(qnan, factory.newDfp("0")), factory.newDfp("1"), 0, "pow #8"); // exponent of one test(DfpMath.pow(factory.newDfp("0"), factory.newDfp("1")), factory.newDfp("0"), 0, "pow #9"); test(DfpMath.pow(factory.newDfp("-0"), factory.newDfp("1")), factory.newDfp("-0"), 0, "pow #10"); test(DfpMath.pow(factory.newDfp("2"), factory.newDfp("1")), factory.newDfp("2"), 0, "pow #11"); test(DfpMath.pow(factory.newDfp("-2"), factory.newDfp("1")), factory.newDfp("-2"), 0, "pow #12"); test(DfpMath.pow(pinf, factory.newDfp("1")), pinf, 0, "pow #13"); test(DfpMath.pow(ninf, factory.newDfp("1")), ninf, 0, "pow #14"); test(DfpMath.pow(qnan, factory.newDfp("1")), qnan, DfpField.FLAG_INVALID, "pow #14.1"); // exponent of NaN test(DfpMath.pow(factory.newDfp("0"), qnan), qnan, DfpField.FLAG_INVALID, "pow #15"); test(DfpMath.pow(factory.newDfp("-0"), qnan), qnan, DfpField.FLAG_INVALID, "pow #16"); test(DfpMath.pow(factory.newDfp("2"), qnan), qnan, DfpField.FLAG_INVALID, "pow #17"); test(DfpMath.pow(factory.newDfp("-2"), qnan), qnan, DfpField.FLAG_INVALID, "pow #18"); test(DfpMath.pow(pinf, qnan), qnan, DfpField.FLAG_INVALID, "pow #19"); test(DfpMath.pow(ninf, qnan), qnan, DfpField.FLAG_INVALID, "pow #20"); test(DfpMath.pow(qnan, qnan), qnan, DfpField.FLAG_INVALID, "pow #21"); // radix of NaN test(DfpMath.pow(qnan, factory.newDfp("1")), qnan, DfpField.FLAG_INVALID, "pow #22"); test(DfpMath.pow(qnan, factory.newDfp("-1")), qnan, DfpField.FLAG_INVALID, "pow #23"); test(DfpMath.pow(qnan, pinf), qnan, DfpField.FLAG_INVALID, "pow #24"); test(DfpMath.pow(qnan, ninf), qnan, DfpField.FLAG_INVALID, "pow #25"); test(DfpMath.pow(qnan, qnan), qnan, DfpField.FLAG_INVALID, "pow #26"); // (x > 1) ^ pinf = pinf, (x < -1) ^ pinf = pinf test(DfpMath.pow(factory.newDfp("2"), pinf), pinf, 0, "pow #27"); test(DfpMath.pow(factory.newDfp("-2"), pinf), pinf, 0, "pow #28"); test(DfpMath.pow(pinf, pinf), pinf, 0, "pow #29"); test(DfpMath.pow(ninf, pinf), pinf, 0, "pow #30"); // (x > 1) ^ ninf = +0, (x < -1) ^ ninf = +0 test(DfpMath.pow(factory.newDfp("2"), ninf), factory.getZero(), 0, "pow #31"); test(DfpMath.pow(factory.newDfp("-2"), ninf), factory.getZero(), 0, "pow #32"); test(DfpMath.pow(pinf, ninf), factory.getZero(), 0, "pow #33"); test(DfpMath.pow(ninf, ninf), factory.getZero(), 0, "pow #34"); // (-1 < x < 1) ^ pinf = 0 test(DfpMath.pow(factory.newDfp("0.5"), pinf), factory.getZero(), 0, "pow #35"); test(DfpMath.pow(factory.newDfp("-0.5"), pinf), factory.getZero(), 0, "pow #36"); // (-1 < x < 1) ^ ninf = pinf test(DfpMath.pow(factory.newDfp("0.5"), ninf), pinf, 0, "pow #37"); test(DfpMath.pow(factory.newDfp("-0.5"), ninf), pinf, 0, "pow #38"); // +/- 1 ^ +/-inf = NaN test(DfpMath.pow(factory.getOne(), pinf), qnan, DfpField.FLAG_INVALID, "pow #39"); test(DfpMath.pow(factory.getOne(), ninf), qnan, DfpField.FLAG_INVALID, "pow #40"); test(DfpMath.pow(factory.newDfp("-1"), pinf), qnan, DfpField.FLAG_INVALID, "pow #41"); test(DfpMath.pow(factory.getOne().negate(), ninf), qnan, DfpField.FLAG_INVALID, "pow #42"); // +0 ^ +anything except 0, NAN = +0 test(DfpMath.pow(factory.newDfp("0"), factory.newDfp("1")), factory.newDfp("0"), 0, "pow #43"); test(DfpMath.pow(factory.newDfp("0"), factory.newDfp("1e30")), factory.newDfp("0"), 0, "pow #44"); test(DfpMath.pow(factory.newDfp("0"), factory.newDfp("1e-30")), factory.newDfp("0"), 0, "pow #45"); test(DfpMath.pow(factory.newDfp("0"), pinf), factory.newDfp("0"), 0, "pow #46"); // -0 ^ +anything except 0, NAN, odd integer = +0 test(DfpMath.pow(factory.newDfp("-0"), factory.newDfp("2")), factory.newDfp("0"), 0, "pow #47"); test(DfpMath.pow(factory.newDfp("-0"), factory.newDfp("1e30")), factory.newDfp("0"), 0, "pow #48"); test(DfpMath.pow(factory.newDfp("-0"), factory.newDfp("1e-30")), factory.newDfp("0"), DfpField.FLAG_INEXACT, "pow #49"); test(DfpMath.pow(factory.newDfp("-0"), pinf), factory.newDfp("0"), 0, "pow #50"); // +0 ^ -anything except 0, NAN = +INF test(DfpMath.pow(factory.newDfp("0"), factory.newDfp("-1")), pinf, 0, "pow #51"); test(DfpMath.pow(factory.newDfp("0"), factory.newDfp("-1e30")), pinf, 0, "pow #52"); test(DfpMath.pow(factory.newDfp("0"), factory.newDfp("-1e-30")), pinf, 0, "pow #53"); test(DfpMath.pow(factory.newDfp("0"), ninf), pinf, 0, "pow #54"); // -0 ^ -anything except 0, NAN, odd integer = +INF test(DfpMath.pow(factory.newDfp("-0"), factory.newDfp("-2")), pinf, 0, "pow #55"); test(DfpMath.pow(factory.newDfp("-0"), factory.newDfp("-1e30")), pinf, 0, "pow #56"); test(DfpMath.pow(factory.newDfp("-0"), factory.newDfp("-1e-30")), pinf, DfpField.FLAG_INEXACT, "pow #57"); test(DfpMath.pow(factory.newDfp("-0"), ninf), pinf, 0, "pow #58"); // -0 ^ -odd integer = -INF test(DfpMath.pow(factory.newDfp("-0"), factory.newDfp("-1")), ninf, DfpField.FLAG_INEXACT, "pow #59"); test(DfpMath.pow(factory.newDfp("-0"), factory.newDfp("-12345")), ninf, DfpField.FLAG_INEXACT, "pow #60"); // -0 ^ +odd integer = -0 test(DfpMath.pow(factory.newDfp("-0"), factory.newDfp("3")), factory.newDfp("-0"), DfpField.FLAG_INEXACT, "pow #61"); test(DfpMath.pow(factory.newDfp("-0"), factory.newDfp("12345")), factory.newDfp("-0"), DfpField.FLAG_INEXACT, "pow #62"); // pinf ^ +anything = pinf test(DfpMath.pow(pinf, factory.newDfp("3")), pinf, 0, "pow #63"); test(DfpMath.pow(pinf, factory.newDfp("1e30")), pinf, 0, "pow #64"); test(DfpMath.pow(pinf, factory.newDfp("1e-30")), pinf, 0, "pow #65"); test(DfpMath.pow(pinf, pinf), pinf, 0, "pow #66"); // pinf ^ -anything = +0 test(DfpMath.pow(pinf, factory.newDfp("-3")), factory.getZero(), 0, "pow #67"); test(DfpMath.pow(pinf, factory.newDfp("-1e30")), factory.getZero(), 0, "pow #68"); test(DfpMath.pow(pinf, factory.newDfp("-1e-30")), factory.getZero(), 0, "pow #69"); test(DfpMath.pow(pinf, ninf), factory.getZero(), 0, "pow #70"); // ninf ^ anything = -0 ^ -anything // ninf ^ -anything except 0, NAN, odd integer = +0 test(DfpMath.pow(ninf, factory.newDfp("-2")), factory.newDfp("0"), 0, "pow #71"); test(DfpMath.pow(ninf, factory.newDfp("-1e30")), factory.newDfp("0"), 0, "pow #72"); test(DfpMath.pow(ninf, factory.newDfp("-1e-30")), factory.newDfp("0"), DfpField.FLAG_INEXACT, "pow #73"); test(DfpMath.pow(ninf, ninf), factory.newDfp("0"), 0, "pow #74"); // ninf ^ +anything except 0, NAN, odd integer = +INF test(DfpMath.pow(ninf, factory.newDfp("2")), pinf, 0, "pow #75"); test(DfpMath.pow(ninf, factory.newDfp("1e30")), pinf, 0, "pow #76"); test(DfpMath.pow(ninf, factory.newDfp("1e-30")), pinf, DfpField.FLAG_INEXACT, "pow #77"); test(DfpMath.pow(ninf, pinf), pinf, 0, "pow #78"); // ninf ^ +odd integer = -INF test(DfpMath.pow(ninf, factory.newDfp("3")), ninf, DfpField.FLAG_INEXACT, "pow #79"); test(DfpMath.pow(ninf, factory.newDfp("12345")), ninf, DfpField.FLAG_INEXACT, "pow #80"); // ninf ^ -odd integer = -0 test(DfpMath.pow(ninf, factory.newDfp("-3")), factory.newDfp("-0"), DfpField.FLAG_INEXACT, "pow #81"); test(DfpMath.pow(ninf, factory.newDfp("-12345")), factory.newDfp("-0"), DfpField.FLAG_INEXACT, "pow #82"); // -anything ^ integer test(DfpMath.pow(factory.newDfp("-2"), factory.newDfp("3")), factory.newDfp("-8"), DfpField.FLAG_INEXACT, "pow #83"); test(DfpMath.pow(factory.newDfp("-2"), factory.newDfp("16")), factory.newDfp("65536"), 0, "pow #84"); test(DfpMath.pow(factory.newDfp("-2"), factory.newDfp("-3")), factory.newDfp("-0.125"), DfpField.FLAG_INEXACT, "pow #85"); test(DfpMath.pow(factory.newDfp("-2"), factory.newDfp("-4")), factory.newDfp("0.0625"), 0, "pow #86"); // -anything ^ noninteger = NaN test(DfpMath.pow(factory.newDfp("-2"), factory.newDfp("-4.1")), qnan, DfpField.FLAG_INVALID|DfpField.FLAG_INEXACT, "pow #87"); // Some fractional cases. test(DfpMath.pow(factory.newDfp("2"),factory.newDfp("1.5")), factory.newDfp("2.8284271247461901"), DfpField.FLAG_INEXACT, "pow #88"); } @Test public void testSin() { test(DfpMath.sin(pinf), nan, DfpField.FLAG_INVALID|DfpField.FLAG_INEXACT, "sin #1"); test(DfpMath.sin(nan), nan, DfpField.FLAG_INVALID|DfpField.FLAG_INEXACT, "sin #2"); test(DfpMath.sin(factory.getZero()), factory.getZero(), DfpField.FLAG_INEXACT, "sin #3"); test(DfpMath.sin(factory.getPi()), factory.getZero(), DfpField.FLAG_INEXACT, "sin #4"); test(DfpMath.sin(factory.getPi().negate()), factory.newDfp("-0"), DfpField.FLAG_INEXACT, "sin #5"); test(DfpMath.sin(factory.getPi().multiply(2)), factory.getZero(), DfpField.FLAG_INEXACT, "sin #6"); test(DfpMath.sin(factory.getPi().divide(2)), factory.getOne(), DfpField.FLAG_INEXACT, "sin #7"); test(DfpMath.sin(factory.getPi().divide(2).negate()), factory.getOne().negate(), DfpField.FLAG_INEXACT, "sin #8"); test(DfpMath.sin(DfpMath.atan(factory.getOne())), // pi/4 factory.newDfp("0.5").sqrt(), DfpField.FLAG_INEXACT, "sin #9"); test(DfpMath.sin(DfpMath.atan(factory.getOne())).negate(), // -pi/4 factory.newDfp("0.5").sqrt().negate(), DfpField.FLAG_INEXACT, "sin #10"); test(DfpMath.sin(DfpMath.atan(factory.getOne())).negate(), // -pi/4 factory.newDfp("0.5").sqrt().negate(), DfpField.FLAG_INEXACT, "sin #11"); test(DfpMath.sin(factory.newDfp("0.1")), factory.newDfp("0.0998334166468281523"), DfpField.FLAG_INEXACT, "sin #12"); test(DfpMath.sin(factory.newDfp("0.2")), factory.newDfp("0.19866933079506121546"), DfpField.FLAG_INEXACT, "sin #13"); test(DfpMath.sin(factory.newDfp("0.3")), factory.newDfp("0.2955202066613395751"), DfpField.FLAG_INEXACT, "sin #14"); test(DfpMath.sin(factory.newDfp("0.4")), factory.newDfp("0.38941834230865049166"), DfpField.FLAG_INEXACT, "sin #15"); test(DfpMath.sin(factory.newDfp("0.5")), factory.newDfp("0.47942553860420300026"), // off by one ULP DfpField.FLAG_INEXACT, "sin #16"); test(DfpMath.sin(factory.newDfp("0.6")), factory.newDfp("0.56464247339503535721"), // off by one ULP DfpField.FLAG_INEXACT, "sin #17"); test(DfpMath.sin(factory.newDfp("0.7")), factory.newDfp("0.64421768723769105367"), DfpField.FLAG_INEXACT, "sin #18"); test(DfpMath.sin(factory.newDfp("0.8")), factory.newDfp("0.71735609089952276163"), DfpField.FLAG_INEXACT, "sin #19"); test(DfpMath.sin(factory.newDfp("0.9")), // off by one ULP factory.newDfp("0.78332690962748338847"), DfpField.FLAG_INEXACT, "sin #20"); test(DfpMath.sin(factory.newDfp("1.0")), factory.newDfp("0.84147098480789650666"), DfpField.FLAG_INEXACT, "sin #21"); test(DfpMath.sin(factory.newDfp("1.1")), factory.newDfp("0.89120736006143533995"), DfpField.FLAG_INEXACT, "sin #22"); test(DfpMath.sin(factory.newDfp("1.2")), factory.newDfp("0.93203908596722634968"), DfpField.FLAG_INEXACT, "sin #23"); test(DfpMath.sin(factory.newDfp("1.3")), factory.newDfp("0.9635581854171929647"), DfpField.FLAG_INEXACT, "sin #24"); test(DfpMath.sin(factory.newDfp("1.4")), factory.newDfp("0.98544972998846018066"), DfpField.FLAG_INEXACT, "sin #25"); test(DfpMath.sin(factory.newDfp("1.5")), factory.newDfp("0.99749498660405443096"), DfpField.FLAG_INEXACT, "sin #26"); test(DfpMath.sin(factory.newDfp("1.6")), factory.newDfp("0.99957360304150516323"), DfpField.FLAG_INEXACT, "sin #27"); } } commons-math3-3.2-src/src/test/java/org/apache/commons/math3/dfp/DfpTest.java100644 1750 1750 202603 12126627673 25613 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.dfp; import org.apache.commons.math3.ExtendedFieldElementAbstractTest; import org.apache.commons.math3.util.FastMath; import org.apache.commons.math3.util.Precision; import org.junit.After; import org.junit.Assert; import org.junit.Before; import org.junit.Test; public class DfpTest extends ExtendedFieldElementAbstractTest { protected Dfp build(final double x) { return field.newDfp(x); } private DfpField field; private Dfp pinf; private Dfp ninf; private Dfp nan; private Dfp snan; private Dfp qnan; @Before public void setUp() { // Some basic setup. Define some constants and clear the status flags field = new DfpField(20); pinf = field.newDfp("1").divide(field.newDfp("0")); ninf = field.newDfp("-1").divide(field.newDfp("0")); nan = field.newDfp("0").divide(field.newDfp("0")); snan = field.newDfp((byte)1, Dfp.SNAN); qnan = field.newDfp((byte)1, Dfp.QNAN); ninf.getField().clearIEEEFlags(); } @After public void tearDown() { field = null; pinf = null; ninf = null; nan = null; snan = null; qnan = null; } // Generic test function. Takes params x and y and tests them for // equality. Then checks the status flags against the flags argument. // If the test fail, it prints the desc string private void test(Dfp x, Dfp y, int flags, String desc) { boolean b = x.equals(y); if (!x.equals(y) && !x.unequal(y)) // NaNs involved b = (x.toString().equals(y.toString())); if (x.equals(field.newDfp("0"))) // distinguish +/- zero b = (b && (x.toString().equals(y.toString()))); b = (b && x.getField().getIEEEFlags() == flags); if (!b) Assert.assertTrue("assersion failed "+desc+" x = "+x.toString()+" flags = "+x.getField().getIEEEFlags(), b); x.getField().clearIEEEFlags(); } @Test public void testByteConstructor() { Assert.assertEquals("0.", new Dfp(field, (byte) 0).toString()); Assert.assertEquals("1.", new Dfp(field, (byte) 1).toString()); Assert.assertEquals("-1.", new Dfp(field, (byte) -1).toString()); Assert.assertEquals("-128.", new Dfp(field, Byte.MIN_VALUE).toString()); Assert.assertEquals("127.", new Dfp(field, Byte.MAX_VALUE).toString()); } @Test public void testIntConstructor() { Assert.assertEquals("0.", new Dfp(field, 0).toString()); Assert.assertEquals("1.", new Dfp(field, 1).toString()); Assert.assertEquals("-1.", new Dfp(field, -1).toString()); Assert.assertEquals("1234567890.", new Dfp(field, 1234567890).toString()); Assert.assertEquals("-1234567890.", new Dfp(field, -1234567890).toString()); Assert.assertEquals("-2147483648.", new Dfp(field, Integer.MIN_VALUE).toString()); Assert.assertEquals("2147483647.", new Dfp(field, Integer.MAX_VALUE).toString()); } @Test public void testLongConstructor() { Assert.assertEquals("0.", new Dfp(field, 0l).toString()); Assert.assertEquals("1.", new Dfp(field, 1l).toString()); Assert.assertEquals("-1.", new Dfp(field, -1l).toString()); Assert.assertEquals("1234567890.", new Dfp(field, 1234567890l).toString()); Assert.assertEquals("-1234567890.", new Dfp(field, -1234567890l).toString()); Assert.assertEquals("-9223372036854775808.", new Dfp(field, Long.MIN_VALUE).toString()); Assert.assertEquals("9223372036854775807.", new Dfp(field, Long.MAX_VALUE).toString()); } /* * Test addition */ @Test public void testAdd() { test(field.newDfp("1").add(field.newDfp("1")), // Basic tests 1+1 = 2 field.newDfp("2"), 0, "Add #1"); test(field.newDfp("1").add(field.newDfp("-1")), // 1 + (-1) = 0 field.newDfp("0"), 0, "Add #2"); test(field.newDfp("-1").add(field.newDfp("1")), // (-1) + 1 = 0 field.newDfp("0"), 0, "Add #3"); test(field.newDfp("-1").add(field.newDfp("-1")), // (-1) + (-1) = -2 field.newDfp("-2"), 0, "Add #4"); // rounding mode is round half even test(field.newDfp("1").add(field.newDfp("1e-16")), // rounding on add field.newDfp("1.0000000000000001"), 0, "Add #5"); test(field.newDfp("1").add(field.newDfp("1e-17")), // rounding on add field.newDfp("1"), DfpField.FLAG_INEXACT, "Add #6"); test(field.newDfp("0.90999999999999999999").add(field.newDfp("0.1")), // rounding on add field.newDfp("1.01"), DfpField.FLAG_INEXACT, "Add #7"); test(field.newDfp(".10000000000000005000").add(field.newDfp(".9")), // rounding on add field.newDfp("1."), DfpField.FLAG_INEXACT, "Add #8"); test(field.newDfp(".10000000000000015000").add(field.newDfp(".9")), // rounding on add field.newDfp("1.0000000000000002"), DfpField.FLAG_INEXACT, "Add #9"); test(field.newDfp(".10000000000000014999").add(field.newDfp(".9")), // rounding on add field.newDfp("1.0000000000000001"), DfpField.FLAG_INEXACT, "Add #10"); test(field.newDfp(".10000000000000015001").add(field.newDfp(".9")), // rounding on add field.newDfp("1.0000000000000002"), DfpField.FLAG_INEXACT, "Add #11"); test(field.newDfp(".11111111111111111111").add(field.newDfp("11.1111111111111111")), // rounding on add field.newDfp("11.22222222222222222222"), DfpField.FLAG_INEXACT, "Add #12"); test(field.newDfp(".11111111111111111111").add(field.newDfp("1111111111111111.1111")), // rounding on add field.newDfp("1111111111111111.2222"), DfpField.FLAG_INEXACT, "Add #13"); test(field.newDfp(".11111111111111111111").add(field.newDfp("11111111111111111111")), // rounding on add field.newDfp("11111111111111111111"), DfpField.FLAG_INEXACT, "Add #14"); test(field.newDfp("9.9999999999999999999e131071").add(field.newDfp("-1e131052")), // overflow on add field.newDfp("9.9999999999999999998e131071"), 0, "Add #15"); test(field.newDfp("9.9999999999999999999e131071").add(field.newDfp("1e131052")), // overflow on add pinf, DfpField.FLAG_OVERFLOW, "Add #16"); test(field.newDfp("-9.9999999999999999999e131071").add(field.newDfp("-1e131052")), // overflow on add ninf, DfpField.FLAG_OVERFLOW, "Add #17"); test(field.newDfp("-9.9999999999999999999e131071").add(field.newDfp("1e131052")), // overflow on add field.newDfp("-9.9999999999999999998e131071"), 0, "Add #18"); test(field.newDfp("1e-131072").add(field.newDfp("1e-131072")), // underflow on add field.newDfp("2e-131072"), 0, "Add #19"); test(field.newDfp("1.0000000000000001e-131057").add(field.newDfp("-1e-131057")), // underflow on add field.newDfp("1e-131073"), DfpField.FLAG_UNDERFLOW, "Add #20"); test(field.newDfp("1.1e-131072").add(field.newDfp("-1e-131072")), // underflow on add field.newDfp("1e-131073"), DfpField.FLAG_UNDERFLOW, "Add #21"); test(field.newDfp("1.0000000000000001e-131072").add(field.newDfp("-1e-131072")), // underflow on add field.newDfp("1e-131088"), DfpField.FLAG_UNDERFLOW, "Add #22"); test(field.newDfp("1.0000000000000001e-131078").add(field.newDfp("-1e-131078")), // underflow on add field.newDfp("0"), DfpField.FLAG_UNDERFLOW, "Add #23"); test(field.newDfp("1.0").add(field.newDfp("-1e-20")), // loss of precision on alignment? field.newDfp("0.99999999999999999999"), 0, "Add #23.1"); test(field.newDfp("-0.99999999999999999999").add(field.newDfp("1")), // proper normalization? field.newDfp("0.00000000000000000001"), 0, "Add #23.2"); test(field.newDfp("1").add(field.newDfp("0")), // adding zeros field.newDfp("1"), 0, "Add #24"); test(field.newDfp("0").add(field.newDfp("0")), // adding zeros field.newDfp("0"), 0, "Add #25"); test(field.newDfp("-0").add(field.newDfp("0")), // adding zeros field.newDfp("0"), 0, "Add #26"); test(field.newDfp("0").add(field.newDfp("-0")), // adding zeros field.newDfp("0"), 0, "Add #27"); test(field.newDfp("-0").add(field.newDfp("-0")), // adding zeros field.newDfp("-0"), 0, "Add #28"); test(field.newDfp("1e-20").add(field.newDfp("0")), // adding zeros field.newDfp("1e-20"), 0, "Add #29"); test(field.newDfp("1e-40").add(field.newDfp("0")), // adding zeros field.newDfp("1e-40"), 0, "Add #30"); test(pinf.add(ninf), // adding infinities nan, DfpField.FLAG_INVALID, "Add #31"); test(ninf.add(pinf), // adding infinities nan, DfpField.FLAG_INVALID, "Add #32"); test(ninf.add(ninf), // adding infinities ninf, 0, "Add #33"); test(pinf.add(pinf), // adding infinities pinf, 0, "Add #34"); test(pinf.add(field.newDfp("0")), // adding infinities pinf, 0, "Add #35"); test(pinf.add(field.newDfp("-1e131071")), // adding infinities pinf, 0, "Add #36"); test(pinf.add(field.newDfp("1e131071")), // adding infinities pinf, 0, "Add #37"); test(field.newDfp("0").add(pinf), // adding infinities pinf, 0, "Add #38"); test(field.newDfp("-1e131071").add(pinf), // adding infinities pinf, 0, "Add #39"); test(field.newDfp("1e131071").add(pinf), // adding infinities pinf, 0, "Add #40"); test(ninf.add(field.newDfp("0")), // adding infinities ninf, 0, "Add #41"); test(ninf.add(field.newDfp("-1e131071")), // adding infinities ninf, 0, "Add #42"); test(ninf.add(field.newDfp("1e131071")), // adding infinities ninf, 0, "Add #43"); test(field.newDfp("0").add(ninf), // adding infinities ninf, 0, "Add #44"); test(field.newDfp("-1e131071").add(ninf), // adding infinities ninf, 0, "Add #45"); test(field.newDfp("1e131071").add(ninf), // adding infinities ninf, 0, "Add #46"); test(field.newDfp("9.9999999999999999999e131071").add(field.newDfp("5e131051")), // overflow pinf, DfpField.FLAG_OVERFLOW, "Add #47"); test(field.newDfp("9.9999999999999999999e131071").add(field.newDfp("4.9999999999999999999e131051")), // overflow field.newDfp("9.9999999999999999999e131071"), DfpField.FLAG_INEXACT, "Add #48"); test(nan.add(field.newDfp("1")), nan, 0, "Add #49"); test(field.newDfp("1").add(nan), nan, 0, "Add #50"); test(field.newDfp("12345678123456781234").add(field.newDfp("0.12345678123456781234")), field.newDfp("12345678123456781234"), DfpField.FLAG_INEXACT, "Add #51"); test(field.newDfp("12345678123456781234").add(field.newDfp("123.45678123456781234")), field.newDfp("12345678123456781357"), DfpField.FLAG_INEXACT, "Add #52"); test(field.newDfp("123.45678123456781234").add(field.newDfp("12345678123456781234")), field.newDfp("12345678123456781357"), DfpField.FLAG_INEXACT, "Add #53"); test(field.newDfp("12345678123456781234").add(field.newDfp(".00001234567812345678")), field.newDfp("12345678123456781234"), DfpField.FLAG_INEXACT, "Add #54"); test(field.newDfp("12345678123456781234").add(field.newDfp(".00000000123456781234")), field.newDfp("12345678123456781234"), DfpField.FLAG_INEXACT, "Add #55"); test(field.newDfp("-0").add(field.newDfp("-0")), field.newDfp("-0"), 0, "Add #56"); test(field.newDfp("0").add(field.newDfp("-0")), field.newDfp("0"), 0, "Add #57"); test(field.newDfp("-0").add(field.newDfp("0")), field.newDfp("0"), 0, "Add #58"); test(field.newDfp("0").add(field.newDfp("0")), field.newDfp("0"), 0, "Add #59"); } //////////////////////////////////////////////////////////////////////////////////////////////////////// // Test comparisons // utility function to help test comparisons private void cmptst(Dfp a, Dfp b, String op, boolean result, double num) { if (op == "equal") if (a.equals(b) != result) Assert.fail("assersion failed. "+op+" compare #"+num); if (op == "unequal") if (a.unequal(b) != result) Assert.fail("assersion failed. "+op+" compare #"+num); if (op == "lessThan") if (a.lessThan(b) != result) Assert.fail("assersion failed. "+op+" compare #"+num); if (op == "greaterThan") if (a.greaterThan(b) != result) Assert.fail("assersion failed. "+op+" compare #"+num); } @Test public void testCompare() { // test equal() comparison // check zero vs. zero field.clearIEEEFlags(); cmptst(field.newDfp("0"), field.newDfp("0"), "equal", true, 1); // 0 == 0 cmptst(field.newDfp("0"), field.newDfp("-0"), "equal", true, 2); // 0 == -0 cmptst(field.newDfp("-0"), field.newDfp("-0"), "equal", true, 3); // -0 == -0 cmptst(field.newDfp("-0"), field.newDfp("0"), "equal", true, 4); // -0 == 0 // check zero vs normal numbers cmptst(field.newDfp("0"), field.newDfp("1"), "equal", false, 5); // 0 == 1 cmptst(field.newDfp("1"), field.newDfp("0"), "equal", false, 6); // 1 == 0 cmptst(field.newDfp("-1"), field.newDfp("0"), "equal", false, 7); // -1 == 0 cmptst(field.newDfp("0"), field.newDfp("-1"), "equal", false, 8); // 0 == -1 cmptst(field.newDfp("0"), field.newDfp("1e-131072"), "equal", false, 9); // 0 == 1e-131072 // check flags if (field.getIEEEFlags() != 0) Assert.fail("assersion failed. compare flags = "+field.getIEEEFlags()); cmptst(field.newDfp("0"), field.newDfp("1e-131078"), "equal", false, 10); // 0 == 1e-131078 // check flags -- underflow should be set if (field.getIEEEFlags() != DfpField.FLAG_UNDERFLOW) Assert.fail("assersion failed. compare flags = "+field.getIEEEFlags()); field.clearIEEEFlags(); cmptst(field.newDfp("0"), field.newDfp("1e+131071"), "equal", false, 11); // 0 == 1e+131071 // check zero vs infinities cmptst(field.newDfp("0"), pinf, "equal", false, 12); // 0 == pinf cmptst(field.newDfp("0"), ninf, "equal", false, 13); // 0 == ninf cmptst(field.newDfp("-0"), pinf, "equal", false, 14); // -0 == pinf cmptst(field.newDfp("-0"), ninf, "equal", false, 15); // -0 == ninf cmptst(pinf, field.newDfp("0"), "equal", false, 16); // pinf == 0 cmptst(ninf, field.newDfp("0"), "equal", false, 17); // ninf == 0 cmptst(pinf, field.newDfp("-0"), "equal", false, 18); // pinf == -0 cmptst(ninf, field.newDfp("-0"), "equal", false, 19); // ninf == -0 cmptst(ninf, pinf, "equal", false, 19.10); // ninf == pinf cmptst(pinf, ninf, "equal", false, 19.11); // pinf == ninf cmptst(pinf, pinf, "equal", true, 19.12); // pinf == pinf cmptst(ninf, ninf, "equal", true, 19.13); // ninf == ninf // check some normal numbers cmptst(field.newDfp("1"), field.newDfp("1"), "equal", true, 20); // 1 == 1 cmptst(field.newDfp("1"), field.newDfp("-1"), "equal", false, 21); // 1 == -1 cmptst(field.newDfp("-1"), field.newDfp("-1"), "equal", true, 22); // -1 == -1 cmptst(field.newDfp("1"), field.newDfp("1.0000000000000001"), "equal", false, 23); // 1 == 1.0000000000000001 // The tests below checks to ensure that comparisons don't set FLAG_INEXACT // 100000 == 1.0000000000000001 cmptst(field.newDfp("1e20"), field.newDfp("1.0000000000000001"), "equal", false, 24); if (field.getIEEEFlags() != 0) Assert.fail("assersion failed. compare flags = "+field.getIEEEFlags()); cmptst(field.newDfp("0.000001"), field.newDfp("1e-6"), "equal", true, 25); // check some nans -- nans shouldnt equal anything cmptst(snan, snan, "equal", false, 27); cmptst(qnan, qnan, "equal", false, 28); cmptst(snan, qnan, "equal", false, 29); cmptst(qnan, snan, "equal", false, 30); cmptst(qnan, field.newDfp("0"), "equal", false, 31); cmptst(snan, field.newDfp("0"), "equal", false, 32); cmptst(field.newDfp("0"), snan, "equal", false, 33); cmptst(field.newDfp("0"), qnan, "equal", false, 34); cmptst(qnan, pinf, "equal", false, 35); cmptst(snan, pinf, "equal", false, 36); cmptst(pinf, snan, "equal", false, 37); cmptst(pinf, qnan, "equal", false, 38); cmptst(qnan, ninf, "equal", false, 39); cmptst(snan, ninf, "equal", false, 40); cmptst(ninf, snan, "equal", false, 41); cmptst(ninf, qnan, "equal", false, 42); cmptst(qnan, field.newDfp("-1"), "equal", false, 43); cmptst(snan, field.newDfp("-1"), "equal", false, 44); cmptst(field.newDfp("-1"), snan, "equal", false, 45); cmptst(field.newDfp("-1"), qnan, "equal", false, 46); cmptst(qnan, field.newDfp("1"), "equal", false, 47); cmptst(snan, field.newDfp("1"), "equal", false, 48); cmptst(field.newDfp("1"), snan, "equal", false, 49); cmptst(field.newDfp("1"), qnan, "equal", false, 50); cmptst(snan.negate(), snan, "equal", false, 51); cmptst(qnan.negate(), qnan, "equal", false, 52); // // Tests for un equal -- do it all over again // cmptst(field.newDfp("0"), field.newDfp("0"), "unequal", false, 1); // 0 == 0 cmptst(field.newDfp("0"), field.newDfp("-0"), "unequal", false, 2); // 0 == -0 cmptst(field.newDfp("-0"), field.newDfp("-0"), "unequal", false, 3); // -0 == -0 cmptst(field.newDfp("-0"), field.newDfp("0"), "unequal", false, 4); // -0 == 0 // check zero vs normal numbers cmptst(field.newDfp("0"), field.newDfp("1"), "unequal", true, 5); // 0 == 1 cmptst(field.newDfp("1"), field.newDfp("0"), "unequal", true, 6); // 1 == 0 cmptst(field.newDfp("-1"), field.newDfp("0"), "unequal", true, 7); // -1 == 0 cmptst(field.newDfp("0"), field.newDfp("-1"), "unequal", true, 8); // 0 == -1 cmptst(field.newDfp("0"), field.newDfp("1e-131072"), "unequal", true, 9); // 0 == 1e-131072 // check flags if (field.getIEEEFlags() != 0) Assert.fail("assersion failed. compare flags = "+field.getIEEEFlags()); cmptst(field.newDfp("0"), field.newDfp("1e-131078"), "unequal", true, 10); // 0 == 1e-131078 // check flags -- underflow should be set if (field.getIEEEFlags() != DfpField.FLAG_UNDERFLOW) Assert.fail("assersion failed. compare flags = "+field.getIEEEFlags()); field.clearIEEEFlags(); cmptst(field.newDfp("0"), field.newDfp("1e+131071"), "unequal", true, 11); // 0 == 1e+131071 // check zero vs infinities cmptst(field.newDfp("0"), pinf, "unequal", true, 12); // 0 == pinf cmptst(field.newDfp("0"), ninf, "unequal", true, 13); // 0 == ninf cmptst(field.newDfp("-0"), pinf, "unequal", true, 14); // -0 == pinf cmptst(field.newDfp("-0"), ninf, "unequal", true, 15); // -0 == ninf cmptst(pinf, field.newDfp("0"), "unequal", true, 16); // pinf == 0 cmptst(ninf, field.newDfp("0"), "unequal", true, 17); // ninf == 0 cmptst(pinf, field.newDfp("-0"), "unequal", true, 18); // pinf == -0 cmptst(ninf, field.newDfp("-0"), "unequal", true, 19); // ninf == -0 cmptst(ninf, pinf, "unequal", true, 19.10); // ninf == pinf cmptst(pinf, ninf, "unequal", true, 19.11); // pinf == ninf cmptst(pinf, pinf, "unequal", false, 19.12); // pinf == pinf cmptst(ninf, ninf, "unequal", false, 19.13); // ninf == ninf // check some normal numbers cmptst(field.newDfp("1"), field.newDfp("1"), "unequal", false, 20); // 1 == 1 cmptst(field.newDfp("1"), field.newDfp("-1"), "unequal", true, 21); // 1 == -1 cmptst(field.newDfp("-1"), field.newDfp("-1"), "unequal", false, 22); // -1 == -1 cmptst(field.newDfp("1"), field.newDfp("1.0000000000000001"), "unequal", true, 23); // 1 == 1.0000000000000001 // The tests below checks to ensure that comparisons don't set FLAG_INEXACT // 100000 == 1.0000000000000001 cmptst(field.newDfp("1e20"), field.newDfp("1.0000000000000001"), "unequal", true, 24); if (field.getIEEEFlags() != 0) Assert.fail("assersion failed. compare flags = "+field.getIEEEFlags()); cmptst(field.newDfp("0.000001"), field.newDfp("1e-6"), "unequal", false, 25); // check some nans -- nans shouldnt be unequal to anything cmptst(snan, snan, "unequal", false, 27); cmptst(qnan, qnan, "unequal", false, 28); cmptst(snan, qnan, "unequal", false, 29); cmptst(qnan, snan, "unequal", false, 30); cmptst(qnan, field.newDfp("0"), "unequal", false, 31); cmptst(snan, field.newDfp("0"), "unequal", false, 32); cmptst(field.newDfp("0"), snan, "unequal", false, 33); cmptst(field.newDfp("0"), qnan, "unequal", false, 34); cmptst(qnan, pinf, "unequal", false, 35); cmptst(snan, pinf, "unequal", false, 36); cmptst(pinf, snan, "unequal", false, 37); cmptst(pinf, qnan, "unequal", false, 38); cmptst(qnan, ninf, "unequal", false, 39); cmptst(snan, ninf, "unequal", false, 40); cmptst(ninf, snan, "unequal", false, 41); cmptst(ninf, qnan, "unequal", false, 42); cmptst(qnan, field.newDfp("-1"), "unequal", false, 43); cmptst(snan, field.newDfp("-1"), "unequal", false, 44); cmptst(field.newDfp("-1"), snan, "unequal", false, 45); cmptst(field.newDfp("-1"), qnan, "unequal", false, 46); cmptst(qnan, field.newDfp("1"), "unequal", false, 47); cmptst(snan, field.newDfp("1"), "unequal", false, 48); cmptst(field.newDfp("1"), snan, "unequal", false, 49); cmptst(field.newDfp("1"), qnan, "unequal", false, 50); cmptst(snan.negate(), snan, "unequal", false, 51); cmptst(qnan.negate(), qnan, "unequal", false, 52); if (field.getIEEEFlags() != 0) Assert.fail("assersion failed. compare unequal flags = "+field.getIEEEFlags()); // // Tests for lessThan -- do it all over again // cmptst(field.newDfp("0"), field.newDfp("0"), "lessThan", false, 1); // 0 < 0 cmptst(field.newDfp("0"), field.newDfp("-0"), "lessThan", false, 2); // 0 < -0 cmptst(field.newDfp("-0"), field.newDfp("-0"), "lessThan", false, 3); // -0 < -0 cmptst(field.newDfp("-0"), field.newDfp("0"), "lessThan", false, 4); // -0 < 0 // check zero vs normal numbers cmptst(field.newDfp("0"), field.newDfp("1"), "lessThan", true, 5); // 0 < 1 cmptst(field.newDfp("1"), field.newDfp("0"), "lessThan", false, 6); // 1 < 0 cmptst(field.newDfp("-1"), field.newDfp("0"), "lessThan", true, 7); // -1 < 0 cmptst(field.newDfp("0"), field.newDfp("-1"), "lessThan", false, 8); // 0 < -1 cmptst(field.newDfp("0"), field.newDfp("1e-131072"), "lessThan", true, 9); // 0 < 1e-131072 // check flags if (field.getIEEEFlags() != 0) Assert.fail("assersion failed. compare flags = "+field.getIEEEFlags()); cmptst(field.newDfp("0"), field.newDfp("1e-131078"), "lessThan", true, 10); // 0 < 1e-131078 // check flags -- underflow should be set if (field.getIEEEFlags() != DfpField.FLAG_UNDERFLOW) Assert.fail("assersion failed. compare flags = "+field.getIEEEFlags()); field.clearIEEEFlags(); cmptst(field.newDfp("0"), field.newDfp("1e+131071"), "lessThan", true, 11); // 0 < 1e+131071 // check zero vs infinities cmptst(field.newDfp("0"), pinf, "lessThan", true, 12); // 0 < pinf cmptst(field.newDfp("0"), ninf, "lessThan", false, 13); // 0 < ninf cmptst(field.newDfp("-0"), pinf, "lessThan", true, 14); // -0 < pinf cmptst(field.newDfp("-0"), ninf, "lessThan", false, 15); // -0 < ninf cmptst(pinf, field.newDfp("0"), "lessThan", false, 16); // pinf < 0 cmptst(ninf, field.newDfp("0"), "lessThan", true, 17); // ninf < 0 cmptst(pinf, field.newDfp("-0"), "lessThan", false, 18); // pinf < -0 cmptst(ninf, field.newDfp("-0"), "lessThan", true, 19); // ninf < -0 cmptst(ninf, pinf, "lessThan", true, 19.10); // ninf < pinf cmptst(pinf, ninf, "lessThan", false, 19.11); // pinf < ninf cmptst(pinf, pinf, "lessThan", false, 19.12); // pinf < pinf cmptst(ninf, ninf, "lessThan", false, 19.13); // ninf < ninf // check some normal numbers cmptst(field.newDfp("1"), field.newDfp("1"), "lessThan", false, 20); // 1 < 1 cmptst(field.newDfp("1"), field.newDfp("-1"), "lessThan", false, 21); // 1 < -1 cmptst(field.newDfp("-1"), field.newDfp("-1"), "lessThan", false, 22); // -1 < -1 cmptst(field.newDfp("1"), field.newDfp("1.0000000000000001"), "lessThan", true, 23); // 1 < 1.0000000000000001 // The tests below checks to ensure that comparisons don't set FLAG_INEXACT // 100000 < 1.0000000000000001 cmptst(field.newDfp("1e20"), field.newDfp("1.0000000000000001"), "lessThan", false, 24); if (field.getIEEEFlags() != 0) Assert.fail("assersion failed. compare flags = "+field.getIEEEFlags()); cmptst(field.newDfp("0.000001"), field.newDfp("1e-6"), "lessThan", false, 25); // check some nans -- nans shouldnt be lessThan to anything cmptst(snan, snan, "lessThan", false, 27); cmptst(qnan, qnan, "lessThan", false, 28); cmptst(snan, qnan, "lessThan", false, 29); cmptst(qnan, snan, "lessThan", false, 30); cmptst(qnan, field.newDfp("0"), "lessThan", false, 31); cmptst(snan, field.newDfp("0"), "lessThan", false, 32); cmptst(field.newDfp("0"), snan, "lessThan", false, 33); cmptst(field.newDfp("0"), qnan, "lessThan", false, 34); cmptst(qnan, pinf, "lessThan", false, 35); cmptst(snan, pinf, "lessThan", false, 36); cmptst(pinf, snan, "lessThan", false, 37); cmptst(pinf, qnan, "lessThan", false, 38); cmptst(qnan, ninf, "lessThan", false, 39); cmptst(snan, ninf, "lessThan", false, 40); cmptst(ninf, snan, "lessThan", false, 41); cmptst(ninf, qnan, "lessThan", false, 42); cmptst(qnan, field.newDfp("-1"), "lessThan", false, 43); cmptst(snan, field.newDfp("-1"), "lessThan", false, 44); cmptst(field.newDfp("-1"), snan, "lessThan", false, 45); cmptst(field.newDfp("-1"), qnan, "lessThan", false, 46); cmptst(qnan, field.newDfp("1"), "lessThan", false, 47); cmptst(snan, field.newDfp("1"), "lessThan", false, 48); cmptst(field.newDfp("1"), snan, "lessThan", false, 49); cmptst(field.newDfp("1"), qnan, "lessThan", false, 50); cmptst(snan.negate(), snan, "lessThan", false, 51); cmptst(qnan.negate(), qnan, "lessThan", false, 52); //lessThan compares with nans should raise FLAG_INVALID if (field.getIEEEFlags() != DfpField.FLAG_INVALID) Assert.fail("assersion failed. compare lessThan flags = "+field.getIEEEFlags()); field.clearIEEEFlags(); // // Tests for greaterThan -- do it all over again // cmptst(field.newDfp("0"), field.newDfp("0"), "greaterThan", false, 1); // 0 > 0 cmptst(field.newDfp("0"), field.newDfp("-0"), "greaterThan", false, 2); // 0 > -0 cmptst(field.newDfp("-0"), field.newDfp("-0"), "greaterThan", false, 3); // -0 > -0 cmptst(field.newDfp("-0"), field.newDfp("0"), "greaterThan", false, 4); // -0 > 0 // check zero vs normal numbers cmptst(field.newDfp("0"), field.newDfp("1"), "greaterThan", false, 5); // 0 > 1 cmptst(field.newDfp("1"), field.newDfp("0"), "greaterThan", true, 6); // 1 > 0 cmptst(field.newDfp("-1"), field.newDfp("0"), "greaterThan", false, 7); // -1 > 0 cmptst(field.newDfp("0"), field.newDfp("-1"), "greaterThan", true, 8); // 0 > -1 cmptst(field.newDfp("0"), field.newDfp("1e-131072"), "greaterThan", false, 9); // 0 > 1e-131072 // check flags if (field.getIEEEFlags() != 0) Assert.fail("assersion failed. compare flags = "+field.getIEEEFlags()); cmptst(field.newDfp("0"), field.newDfp("1e-131078"), "greaterThan", false, 10); // 0 > 1e-131078 // check flags -- underflow should be set if (field.getIEEEFlags() != DfpField.FLAG_UNDERFLOW) Assert.fail("assersion failed. compare flags = "+field.getIEEEFlags()); field.clearIEEEFlags(); cmptst(field.newDfp("0"), field.newDfp("1e+131071"), "greaterThan", false, 11); // 0 > 1e+131071 // check zero vs infinities cmptst(field.newDfp("0"), pinf, "greaterThan", false, 12); // 0 > pinf cmptst(field.newDfp("0"), ninf, "greaterThan", true, 13); // 0 > ninf cmptst(field.newDfp("-0"), pinf, "greaterThan", false, 14); // -0 > pinf cmptst(field.newDfp("-0"), ninf, "greaterThan", true, 15); // -0 > ninf cmptst(pinf, field.newDfp("0"), "greaterThan", true, 16); // pinf > 0 cmptst(ninf, field.newDfp("0"), "greaterThan", false, 17); // ninf > 0 cmptst(pinf, field.newDfp("-0"), "greaterThan", true, 18); // pinf > -0 cmptst(ninf, field.newDfp("-0"), "greaterThan", false, 19); // ninf > -0 cmptst(ninf, pinf, "greaterThan", false, 19.10); // ninf > pinf cmptst(pinf, ninf, "greaterThan", true, 19.11); // pinf > ninf cmptst(pinf, pinf, "greaterThan", false, 19.12); // pinf > pinf cmptst(ninf, ninf, "greaterThan", false, 19.13); // ninf > ninf // check some normal numbers cmptst(field.newDfp("1"), field.newDfp("1"), "greaterThan", false, 20); // 1 > 1 cmptst(field.newDfp("1"), field.newDfp("-1"), "greaterThan", true, 21); // 1 > -1 cmptst(field.newDfp("-1"), field.newDfp("-1"), "greaterThan", false, 22); // -1 > -1 cmptst(field.newDfp("1"), field.newDfp("1.0000000000000001"), "greaterThan", false, 23); // 1 > 1.0000000000000001 // The tests below checks to ensure that comparisons don't set FLAG_INEXACT // 100000 > 1.0000000000000001 cmptst(field.newDfp("1e20"), field.newDfp("1.0000000000000001"), "greaterThan", true, 24); if (field.getIEEEFlags() != 0) Assert.fail("assersion failed. compare flags = "+field.getIEEEFlags()); cmptst(field.newDfp("0.000001"), field.newDfp("1e-6"), "greaterThan", false, 25); // check some nans -- nans shouldnt be greaterThan to anything cmptst(snan, snan, "greaterThan", false, 27); cmptst(qnan, qnan, "greaterThan", false, 28); cmptst(snan, qnan, "greaterThan", false, 29); cmptst(qnan, snan, "greaterThan", false, 30); cmptst(qnan, field.newDfp("0"), "greaterThan", false, 31); cmptst(snan, field.newDfp("0"), "greaterThan", false, 32); cmptst(field.newDfp("0"), snan, "greaterThan", false, 33); cmptst(field.newDfp("0"), qnan, "greaterThan", false, 34); cmptst(qnan, pinf, "greaterThan", false, 35); cmptst(snan, pinf, "greaterThan", false, 36); cmptst(pinf, snan, "greaterThan", false, 37); cmptst(pinf, qnan, "greaterThan", false, 38); cmptst(qnan, ninf, "greaterThan", false, 39); cmptst(snan, ninf, "greaterThan", false, 40); cmptst(ninf, snan, "greaterThan", false, 41); cmptst(ninf, qnan, "greaterThan", false, 42); cmptst(qnan, field.newDfp("-1"), "greaterThan", false, 43); cmptst(snan, field.newDfp("-1"), "greaterThan", false, 44); cmptst(field.newDfp("-1"), snan, "greaterThan", false, 45); cmptst(field.newDfp("-1"), qnan, "greaterThan", false, 46); cmptst(qnan, field.newDfp("1"), "greaterThan", false, 47); cmptst(snan, field.newDfp("1"), "greaterThan", false, 48); cmptst(field.newDfp("1"), snan, "greaterThan", false, 49); cmptst(field.newDfp("1"), qnan, "greaterThan", false, 50); cmptst(snan.negate(), snan, "greaterThan", false, 51); cmptst(qnan.negate(), qnan, "greaterThan", false, 52); //greaterThan compares with nans should raise FLAG_INVALID if (field.getIEEEFlags() != DfpField.FLAG_INVALID) Assert.fail("assersion failed. compare greaterThan flags = "+field.getIEEEFlags()); field.clearIEEEFlags(); } // // Test multiplication // @Test public void testMultiply() { test(field.newDfp("1").multiply(field.newDfp("1")), // Basic tests 1*1 = 1 field.newDfp("1"), 0, "Multiply #1"); test(field.newDfp("1").multiply(1), // Basic tests 1*1 = 1 field.newDfp("1"), 0, "Multiply #2"); test(field.newDfp("-1").multiply(field.newDfp("1")), // Basic tests -1*1 = -1 field.newDfp("-1"), 0, "Multiply #3"); test(field.newDfp("-1").multiply(1), // Basic tests -1*1 = -1 field.newDfp("-1"), 0, "Multiply #4"); // basic tests with integers test(field.newDfp("2").multiply(field.newDfp("3")), field.newDfp("6"), 0, "Multiply #5"); test(field.newDfp("2").multiply(3), field.newDfp("6"), 0, "Multiply #6"); test(field.newDfp("-2").multiply(field.newDfp("3")), field.newDfp("-6"), 0, "Multiply #7"); test(field.newDfp("-2").multiply(3), field.newDfp("-6"), 0, "Multiply #8"); test(field.newDfp("2").multiply(field.newDfp("-3")), field.newDfp("-6"), 0, "Multiply #9"); test(field.newDfp("-2").multiply(field.newDfp("-3")), field.newDfp("6"), 0, "Multiply #10"); //multiply by zero test(field.newDfp("-2").multiply(field.newDfp("0")), field.newDfp("-0"), 0, "Multiply #11"); test(field.newDfp("-2").multiply(0), field.newDfp("-0"), 0, "Multiply #12"); test(field.newDfp("2").multiply(field.newDfp("0")), field.newDfp("0"), 0, "Multiply #13"); test(field.newDfp("2").multiply(0), field.newDfp("0"), 0, "Multiply #14"); test(field.newDfp("2").multiply(pinf), pinf, 0, "Multiply #15"); test(field.newDfp("2").multiply(ninf), ninf, 0, "Multiply #16"); test(field.newDfp("-2").multiply(pinf), ninf, 0, "Multiply #17"); test(field.newDfp("-2").multiply(ninf), pinf, 0, "Multiply #18"); test(ninf.multiply(field.newDfp("-2")), pinf, 0, "Multiply #18.1"); test(field.newDfp("5e131071").multiply(2), pinf, DfpField.FLAG_OVERFLOW, "Multiply #19"); test(field.newDfp("5e131071").multiply(field.newDfp("1.999999999999999")), field.newDfp("9.9999999999999950000e131071"), 0, "Multiply #20"); test(field.newDfp("-5e131071").multiply(2), ninf, DfpField.FLAG_OVERFLOW, "Multiply #22"); test(field.newDfp("-5e131071").multiply(field.newDfp("1.999999999999999")), field.newDfp("-9.9999999999999950000e131071"), 0, "Multiply #23"); test(field.newDfp("1e-65539").multiply(field.newDfp("1e-65539")), field.newDfp("1e-131078"), DfpField.FLAG_UNDERFLOW, "Multiply #24"); test(field.newDfp("1").multiply(nan), nan, 0, "Multiply #25"); test(nan.multiply(field.newDfp("1")), nan, 0, "Multiply #26"); test(nan.multiply(pinf), nan, 0, "Multiply #27"); test(pinf.multiply(nan), nan, 0, "Multiply #27"); test(pinf.multiply(field.newDfp("0")), nan, DfpField.FLAG_INVALID, "Multiply #28"); test(field.newDfp("0").multiply(pinf), nan, DfpField.FLAG_INVALID, "Multiply #29"); test(pinf.multiply(pinf), pinf, 0, "Multiply #30"); test(ninf.multiply(pinf), ninf, 0, "Multiply #31"); test(pinf.multiply(ninf), ninf, 0, "Multiply #32"); test(ninf.multiply(ninf), pinf, 0, "Multiply #33"); test(pinf.multiply(1), pinf, 0, "Multiply #34"); test(pinf.multiply(0), nan, DfpField.FLAG_INVALID, "Multiply #35"); test(nan.multiply(1), nan, 0, "Multiply #36"); test(field.newDfp("1").multiply(10000), field.newDfp("10000"), 0, "Multiply #37"); test(field.newDfp("2").multiply(1000000), field.newDfp("2000000"), 0, "Multiply #38"); test(field.newDfp("1").multiply(-1), field.newDfp("-1"), 0, "Multiply #39"); } @Test public void testDivide() { test(field.newDfp("1").divide(nan), // divide by NaN = NaN nan, 0, "Divide #1"); test(nan.divide(field.newDfp("1")), // NaN / number = NaN nan, 0, "Divide #2"); test(pinf.divide(field.newDfp("1")), pinf, 0, "Divide #3"); test(pinf.divide(field.newDfp("-1")), ninf, 0, "Divide #4"); test(pinf.divide(pinf), nan, DfpField.FLAG_INVALID, "Divide #5"); test(ninf.divide(pinf), nan, DfpField.FLAG_INVALID, "Divide #6"); test(pinf.divide(ninf), nan, DfpField.FLAG_INVALID, "Divide #7"); test(ninf.divide(ninf), nan, DfpField.FLAG_INVALID, "Divide #8"); test(field.newDfp("0").divide(field.newDfp("0")), nan, DfpField.FLAG_DIV_ZERO, "Divide #9"); test(field.newDfp("1").divide(field.newDfp("0")), pinf, DfpField.FLAG_DIV_ZERO, "Divide #10"); test(field.newDfp("1").divide(field.newDfp("-0")), ninf, DfpField.FLAG_DIV_ZERO, "Divide #11"); test(field.newDfp("-1").divide(field.newDfp("0")), ninf, DfpField.FLAG_DIV_ZERO, "Divide #12"); test(field.newDfp("-1").divide(field.newDfp("-0")), pinf, DfpField.FLAG_DIV_ZERO, "Divide #13"); test(field.newDfp("1").divide(field.newDfp("3")), field.newDfp("0.33333333333333333333"), DfpField.FLAG_INEXACT, "Divide #14"); test(field.newDfp("1").divide(field.newDfp("6")), field.newDfp("0.16666666666666666667"), DfpField.FLAG_INEXACT, "Divide #15"); test(field.newDfp("10").divide(field.newDfp("6")), field.newDfp("1.6666666666666667"), DfpField.FLAG_INEXACT, "Divide #16"); test(field.newDfp("100").divide(field.newDfp("6")), field.newDfp("16.6666666666666667"), DfpField.FLAG_INEXACT, "Divide #17"); test(field.newDfp("1000").divide(field.newDfp("6")), field.newDfp("166.6666666666666667"), DfpField.FLAG_INEXACT, "Divide #18"); test(field.newDfp("10000").divide(field.newDfp("6")), field.newDfp("1666.6666666666666667"), DfpField.FLAG_INEXACT, "Divide #19"); test(field.newDfp("1").divide(field.newDfp("1")), field.newDfp("1"), 0, "Divide #20"); test(field.newDfp("1").divide(field.newDfp("-1")), field.newDfp("-1"), 0, "Divide #21"); test(field.newDfp("-1").divide(field.newDfp("1")), field.newDfp("-1"), 0, "Divide #22"); test(field.newDfp("-1").divide(field.newDfp("-1")), field.newDfp("1"), 0, "Divide #23"); test(field.newDfp("1e-65539").divide(field.newDfp("1e65539")), field.newDfp("1e-131078"), DfpField.FLAG_UNDERFLOW, "Divide #24"); test(field.newDfp("1e65539").divide(field.newDfp("1e-65539")), pinf, DfpField.FLAG_OVERFLOW, "Divide #24"); test(field.newDfp("2").divide(field.newDfp("1.5")), // test trial-divisor too high field.newDfp("1.3333333333333333"), DfpField.FLAG_INEXACT, "Divide #25"); test(field.newDfp("2").divide(pinf), field.newDfp("0"), 0, "Divide #26"); test(field.newDfp("2").divide(ninf), field.newDfp("-0"), 0, "Divide #27"); test(field.newDfp("0").divide(field.newDfp("1")), field.newDfp("0"), 0, "Divide #28"); } @Test public void testReciprocal() { test(nan.reciprocal(), nan, 0, "Reciprocal #1"); test(field.newDfp("0").reciprocal(), pinf, DfpField.FLAG_DIV_ZERO, "Reciprocal #2"); test(field.newDfp("-0").reciprocal(), ninf, DfpField.FLAG_DIV_ZERO, "Reciprocal #3"); test(field.newDfp("3").reciprocal(), field.newDfp("0.33333333333333333333"), DfpField.FLAG_INEXACT, "Reciprocal #4"); test(field.newDfp("6").reciprocal(), field.newDfp("0.16666666666666666667"), DfpField.FLAG_INEXACT, "Reciprocal #5"); test(field.newDfp("1").reciprocal(), field.newDfp("1"), 0, "Reciprocal #6"); test(field.newDfp("-1").reciprocal(), field.newDfp("-1"), 0, "Reciprocal #7"); test(pinf.reciprocal(), field.newDfp("0"), 0, "Reciprocal #8"); test(ninf.reciprocal(), field.newDfp("-0"), 0, "Reciprocal #9"); } @Test public void testDivideInt() { test(nan.divide(1), // NaN / number = NaN nan, 0, "DivideInt #1"); test(pinf.divide(1), pinf, 0, "DivideInt #2"); test(field.newDfp("0").divide(0), nan, DfpField.FLAG_DIV_ZERO, "DivideInt #3"); test(field.newDfp("1").divide(0), pinf, DfpField.FLAG_DIV_ZERO, "DivideInt #4"); test(field.newDfp("-1").divide(0), ninf, DfpField.FLAG_DIV_ZERO, "DivideInt #5"); test(field.newDfp("1").divide(3), field.newDfp("0.33333333333333333333"), DfpField.FLAG_INEXACT, "DivideInt #6"); test(field.newDfp("1").divide(6), field.newDfp("0.16666666666666666667"), DfpField.FLAG_INEXACT, "DivideInt #7"); test(field.newDfp("10").divide(6), field.newDfp("1.6666666666666667"), DfpField.FLAG_INEXACT, "DivideInt #8"); test(field.newDfp("100").divide(6), field.newDfp("16.6666666666666667"), DfpField.FLAG_INEXACT, "DivideInt #9"); test(field.newDfp("1000").divide(6), field.newDfp("166.6666666666666667"), DfpField.FLAG_INEXACT, "DivideInt #10"); test(field.newDfp("10000").divide(6), field.newDfp("1666.6666666666666667"), DfpField.FLAG_INEXACT, "DivideInt #20"); test(field.newDfp("1").divide(1), field.newDfp("1"), 0, "DivideInt #21"); test(field.newDfp("1e-131077").divide(10), field.newDfp("1e-131078"), DfpField.FLAG_UNDERFLOW, "DivideInt #22"); test(field.newDfp("0").divide(1), field.newDfp("0"), 0, "DivideInt #23"); test(field.newDfp("1").divide(10000), nan, DfpField.FLAG_INVALID, "DivideInt #24"); test(field.newDfp("1").divide(-1), nan, DfpField.FLAG_INVALID, "DivideInt #25"); } @Test public void testNextAfter() { test(field.newDfp("1").nextAfter(pinf), field.newDfp("1.0000000000000001"), 0, "NextAfter #1"); test(field.newDfp("1.0000000000000001").nextAfter(ninf), field.newDfp("1"), 0, "NextAfter #1.5"); test(field.newDfp("1").nextAfter(ninf), field.newDfp("0.99999999999999999999"), 0, "NextAfter #2"); test(field.newDfp("0.99999999999999999999").nextAfter(field.newDfp("2")), field.newDfp("1"), 0, "NextAfter #3"); test(field.newDfp("-1").nextAfter(ninf), field.newDfp("-1.0000000000000001"), 0, "NextAfter #4"); test(field.newDfp("-1").nextAfter(pinf), field.newDfp("-0.99999999999999999999"), 0, "NextAfter #5"); test(field.newDfp("-0.99999999999999999999").nextAfter(field.newDfp("-2")), field.newDfp("-1"), 0, "NextAfter #6"); test(field.newDfp("2").nextAfter(field.newDfp("2")), field.newDfp("2"), 0, "NextAfter #7"); test(field.newDfp("0").nextAfter(field.newDfp("0")), field.newDfp("0"), 0, "NextAfter #8"); test(field.newDfp("-2").nextAfter(field.newDfp("-2")), field.newDfp("-2"), 0, "NextAfter #9"); test(field.newDfp("0").nextAfter(field.newDfp("1")), field.newDfp("1e-131092"), DfpField.FLAG_UNDERFLOW, "NextAfter #10"); test(field.newDfp("0").nextAfter(field.newDfp("-1")), field.newDfp("-1e-131092"), DfpField.FLAG_UNDERFLOW, "NextAfter #11"); test(field.newDfp("-1e-131092").nextAfter(pinf), field.newDfp("-0"), DfpField.FLAG_UNDERFLOW|DfpField.FLAG_INEXACT, "Next After #12"); test(field.newDfp("1e-131092").nextAfter(ninf), field.newDfp("0"), DfpField.FLAG_UNDERFLOW|DfpField.FLAG_INEXACT, "Next After #13"); test(field.newDfp("9.9999999999999999999e131078").nextAfter(pinf), pinf, DfpField.FLAG_OVERFLOW|DfpField.FLAG_INEXACT, "Next After #14"); } @Test public void testToString() { Assert.assertEquals("toString #1", "Infinity", pinf.toString()); Assert.assertEquals("toString #2", "-Infinity", ninf.toString()); Assert.assertEquals("toString #3", "NaN", nan.toString()); Assert.assertEquals("toString #4", "NaN", field.newDfp((byte) 1, Dfp.QNAN).toString()); Assert.assertEquals("toString #5", "NaN", field.newDfp((byte) 1, Dfp.SNAN).toString()); Assert.assertEquals("toString #6", "1.2300000000000000e100", field.newDfp("1.23e100").toString()); Assert.assertEquals("toString #7", "-1.2300000000000000e100", field.newDfp("-1.23e100").toString()); Assert.assertEquals("toString #8", "12345678.1234", field.newDfp("12345678.1234").toString()); Assert.assertEquals("toString #9", "0.00001234", field.newDfp("0.00001234").toString()); } @Test public void testRound() { field.setRoundingMode(DfpField.RoundingMode.ROUND_DOWN); // Round down test(field.newDfp("12345678901234567890").add(field.newDfp("0.9")), field.newDfp("12345678901234567890"), DfpField.FLAG_INEXACT, "Round #1"); test(field.newDfp("12345678901234567890").add(field.newDfp("0.99999999")), field.newDfp("12345678901234567890"), DfpField.FLAG_INEXACT, "Round #2"); test(field.newDfp("-12345678901234567890").add(field.newDfp("-0.99999999")), field.newDfp("-12345678901234567890"), DfpField.FLAG_INEXACT, "Round #3"); field.setRoundingMode(DfpField.RoundingMode.ROUND_UP); // Round up test(field.newDfp("12345678901234567890").add(field.newDfp("0.1")), field.newDfp("12345678901234567891"), DfpField.FLAG_INEXACT, "Round #4"); test(field.newDfp("12345678901234567890").add(field.newDfp("0.0001")), field.newDfp("12345678901234567891"), DfpField.FLAG_INEXACT, "Round #5"); test(field.newDfp("-12345678901234567890").add(field.newDfp("-0.1")), field.newDfp("-12345678901234567891"), DfpField.FLAG_INEXACT, "Round #6"); test(field.newDfp("-12345678901234567890").add(field.newDfp("-0.0001")), field.newDfp("-12345678901234567891"), DfpField.FLAG_INEXACT, "Round #7"); field.setRoundingMode(DfpField.RoundingMode.ROUND_HALF_UP); // Round half up test(field.newDfp("12345678901234567890").add(field.newDfp("0.4999")), field.newDfp("12345678901234567890"), DfpField.FLAG_INEXACT, "Round #8"); test(field.newDfp("12345678901234567890").add(field.newDfp("0.5000")), field.newDfp("12345678901234567891"), DfpField.FLAG_INEXACT, "Round #9"); test(field.newDfp("-12345678901234567890").add(field.newDfp("-0.4999")), field.newDfp("-12345678901234567890"), DfpField.FLAG_INEXACT, "Round #10"); test(field.newDfp("-12345678901234567890").add(field.newDfp("-0.5000")), field.newDfp("-12345678901234567891"), DfpField.FLAG_INEXACT, "Round #11"); field.setRoundingMode(DfpField.RoundingMode.ROUND_HALF_DOWN); // Round half down test(field.newDfp("12345678901234567890").add(field.newDfp("0.5001")), field.newDfp("12345678901234567891"), DfpField.FLAG_INEXACT, "Round #12"); test(field.newDfp("12345678901234567890").add(field.newDfp("0.5000")), field.newDfp("12345678901234567890"), DfpField.FLAG_INEXACT, "Round #13"); test(field.newDfp("-12345678901234567890").add(field.newDfp("-0.5001")), field.newDfp("-12345678901234567891"), DfpField.FLAG_INEXACT, "Round #14"); test(field.newDfp("-12345678901234567890").add(field.newDfp("-0.5000")), field.newDfp("-12345678901234567890"), DfpField.FLAG_INEXACT, "Round #15"); field.setRoundingMode(DfpField.RoundingMode.ROUND_HALF_ODD); // Round half odd test(field.newDfp("12345678901234567890").add(field.newDfp("0.5000")), field.newDfp("12345678901234567891"), DfpField.FLAG_INEXACT, "Round #16"); test(field.newDfp("12345678901234567891").add(field.newDfp("0.5000")), field.newDfp("12345678901234567891"), DfpField.FLAG_INEXACT, "Round #17"); test(field.newDfp("-12345678901234567890").add(field.newDfp("-0.5000")), field.newDfp("-12345678901234567891"), DfpField.FLAG_INEXACT, "Round #18"); test(field.newDfp("-12345678901234567891").add(field.newDfp("-0.5000")), field.newDfp("-12345678901234567891"), DfpField.FLAG_INEXACT, "Round #19"); field.setRoundingMode(DfpField.RoundingMode.ROUND_CEIL); // Round ceil test(field.newDfp("12345678901234567890").add(field.newDfp("0.0001")), field.newDfp("12345678901234567891"), DfpField.FLAG_INEXACT, "Round #20"); test(field.newDfp("-12345678901234567890").add(field.newDfp("-0.9999")), field.newDfp("-12345678901234567890"), DfpField.FLAG_INEXACT, "Round #21"); field.setRoundingMode(DfpField.RoundingMode.ROUND_FLOOR); // Round floor test(field.newDfp("12345678901234567890").add(field.newDfp("0.9999")), field.newDfp("12345678901234567890"), DfpField.FLAG_INEXACT, "Round #22"); test(field.newDfp("-12345678901234567890").add(field.newDfp("-0.0001")), field.newDfp("-12345678901234567891"), DfpField.FLAG_INEXACT, "Round #23"); field.setRoundingMode(DfpField.RoundingMode.ROUND_HALF_EVEN); // reset } @Test public void testCeil() { test(field.newDfp("1234.0000000000000001").ceil(), field.newDfp("1235"), DfpField.FLAG_INEXACT, "Ceil #1"); } @Test public void testFloor() { test(field.newDfp("1234.9999999999999999").floor(), field.newDfp("1234"), DfpField.FLAG_INEXACT, "Floor #1"); } @Test public void testRint() { test(field.newDfp("1234.50000000001").rint(), field.newDfp("1235"), DfpField.FLAG_INEXACT, "Rint #1"); test(field.newDfp("1234.5000").rint(), field.newDfp("1234"), DfpField.FLAG_INEXACT, "Rint #2"); test(field.newDfp("1235.5000").rint(), field.newDfp("1236"), DfpField.FLAG_INEXACT, "Rint #3"); } @Test public void testCopySign() { test(Dfp.copysign(field.newDfp("1234."), field.newDfp("-1")), field.newDfp("-1234"), 0, "CopySign #1"); test(Dfp.copysign(field.newDfp("-1234."), field.newDfp("-1")), field.newDfp("-1234"), 0, "CopySign #2"); test(Dfp.copysign(field.newDfp("-1234."), field.newDfp("1")), field.newDfp("1234"), 0, "CopySign #3"); test(Dfp.copysign(field.newDfp("1234."), field.newDfp("1")), field.newDfp("1234"), 0, "CopySign #4"); } @Test public void testIntValue() { Assert.assertEquals("intValue #1", 1234, field.newDfp("1234").intValue()); Assert.assertEquals("intValue #2", -1234, field.newDfp("-1234").intValue()); Assert.assertEquals("intValue #3", 1234, field.newDfp("1234.5").intValue()); Assert.assertEquals("intValue #4", 1235, field.newDfp("1234.500001").intValue()); Assert.assertEquals("intValue #5", 2147483647, field.newDfp("1e1000").intValue()); Assert.assertEquals("intValue #6", -2147483648, field.newDfp("-1e1000").intValue()); } @Test public void testLog10K() { Assert.assertEquals("log10K #1", 1, field.newDfp("123456").log10K()); Assert.assertEquals("log10K #2", 2, field.newDfp("123456789").log10K()); Assert.assertEquals("log10K #3", 0, field.newDfp("2").log10K()); Assert.assertEquals("log10K #3", 0, field.newDfp("1").log10K()); Assert.assertEquals("log10K #4", -1, field.newDfp("0.1").log10K()); } @Test public void testPower10K() { Dfp d = field.newDfp(); test(d.power10K(0), field.newDfp("1"), 0, "Power10 #1"); test(d.power10K(1), field.newDfp("10000"), 0, "Power10 #2"); test(d.power10K(2), field.newDfp("100000000"), 0, "Power10 #3"); test(d.power10K(-1), field.newDfp("0.0001"), 0, "Power10 #4"); test(d.power10K(-2), field.newDfp("0.00000001"), 0, "Power10 #5"); test(d.power10K(-3), field.newDfp("0.000000000001"), 0, "Power10 #6"); } @Test public void testLog10() { Assert.assertEquals("log10 #1", 1, field.newDfp("12").intLog10()); Assert.assertEquals("log10 #2", 2, field.newDfp("123").intLog10()); Assert.assertEquals("log10 #3", 3, field.newDfp("1234").intLog10()); Assert.assertEquals("log10 #4", 4, field.newDfp("12345").intLog10()); Assert.assertEquals("log10 #5", 5, field.newDfp("123456").intLog10()); Assert.assertEquals("log10 #6", 6, field.newDfp("1234567").intLog10()); Assert.assertEquals("log10 #6", 7, field.newDfp("12345678").intLog10()); Assert.assertEquals("log10 #7", 8, field.newDfp("123456789").intLog10()); Assert.assertEquals("log10 #8", 9, field.newDfp("1234567890").intLog10()); Assert.assertEquals("log10 #9", 10, field.newDfp("12345678901").intLog10()); Assert.assertEquals("log10 #10", 11, field.newDfp("123456789012").intLog10()); Assert.assertEquals("log10 #11", 12, field.newDfp("1234567890123").intLog10()); Assert.assertEquals("log10 #12", 0, field.newDfp("2").intLog10()); Assert.assertEquals("log10 #13", 0, field.newDfp("1").intLog10()); Assert.assertEquals("log10 #14", -1, field.newDfp("0.12").intLog10()); Assert.assertEquals("log10 #15", -2, field.newDfp("0.012").intLog10()); } @Test public void testPower10() { Dfp d = field.newDfp(); test(d.power10(0), field.newDfp("1"), 0, "Power10 #1"); test(d.power10(1), field.newDfp("10"), 0, "Power10 #2"); test(d.power10(2), field.newDfp("100"), 0, "Power10 #3"); test(d.power10(3), field.newDfp("1000"), 0, "Power10 #4"); test(d.power10(4), field.newDfp("10000"), 0, "Power10 #5"); test(d.power10(5), field.newDfp("100000"), 0, "Power10 #6"); test(d.power10(6), field.newDfp("1000000"), 0, "Power10 #7"); test(d.power10(7), field.newDfp("10000000"), 0, "Power10 #8"); test(d.power10(8), field.newDfp("100000000"), 0, "Power10 #9"); test(d.power10(9), field.newDfp("1000000000"), 0, "Power10 #10"); test(d.power10(-1), field.newDfp(".1"), 0, "Power10 #11"); test(d.power10(-2), field.newDfp(".01"), 0, "Power10 #12"); test(d.power10(-3), field.newDfp(".001"), 0, "Power10 #13"); test(d.power10(-4), field.newDfp(".0001"), 0, "Power10 #14"); test(d.power10(-5), field.newDfp(".00001"), 0, "Power10 #15"); test(d.power10(-6), field.newDfp(".000001"), 0, "Power10 #16"); test(d.power10(-7), field.newDfp(".0000001"), 0, "Power10 #17"); test(d.power10(-8), field.newDfp(".00000001"), 0, "Power10 #18"); test(d.power10(-9), field.newDfp(".000000001"), 0, "Power10 #19"); test(d.power10(-10), field.newDfp(".0000000001"), 0, "Power10 #20"); } @Test public void testRemainder() { test(field.newDfp("10").remainder(field.newDfp("3")), field.newDfp("1"), DfpField.FLAG_INEXACT, "Remainder #1"); test(field.newDfp("9").remainder(field.newDfp("3")), field.newDfp("0"), 0, "Remainder #2"); test(field.newDfp("-9").remainder(field.newDfp("3")), field.newDfp("-0"), 0, "Remainder #3"); } @Test public void testSqrt() { test(field.newDfp("0").sqrt(), field.newDfp("0"), 0, "Sqrt #1"); test(field.newDfp("-0").sqrt(), field.newDfp("-0"), 0, "Sqrt #2"); test(field.newDfp("1").sqrt(), field.newDfp("1"), 0, "Sqrt #3"); test(field.newDfp("2").sqrt(), field.newDfp("1.4142135623730950"), DfpField.FLAG_INEXACT, "Sqrt #4"); test(field.newDfp("3").sqrt(), field.newDfp("1.7320508075688773"), DfpField.FLAG_INEXACT, "Sqrt #5"); test(field.newDfp("5").sqrt(), field.newDfp("2.2360679774997897"), DfpField.FLAG_INEXACT, "Sqrt #6"); test(field.newDfp("500").sqrt(), field.newDfp("22.3606797749978970"), DfpField.FLAG_INEXACT, "Sqrt #6.2"); test(field.newDfp("50000").sqrt(), field.newDfp("223.6067977499789696"), DfpField.FLAG_INEXACT, "Sqrt #6.3"); test(field.newDfp("-1").sqrt(), nan, DfpField.FLAG_INVALID, "Sqrt #7"); test(pinf.sqrt(), pinf, 0, "Sqrt #8"); test(field.newDfp((byte) 1, Dfp.QNAN).sqrt(), nan, 0, "Sqrt #9"); test(field.newDfp((byte) 1, Dfp.SNAN).sqrt(), nan, DfpField.FLAG_INVALID, "Sqrt #9"); } @Test public void testIssue567() { DfpField field = new DfpField(100); Assert.assertEquals(0.0, field.getZero().toDouble(), Precision.SAFE_MIN); Assert.assertEquals(0.0, field.newDfp(0.0).toDouble(), Precision.SAFE_MIN); Assert.assertEquals(-1, FastMath.copySign(1, field.newDfp(-0.0).toDouble()), Precision.EPSILON); Assert.assertEquals(+1, FastMath.copySign(1, field.newDfp(+0.0).toDouble()), Precision.EPSILON); } @Test public void testIsZero() { Assert.assertTrue(field.getZero().isZero()); Assert.assertTrue(field.getZero().negate().isZero()); Assert.assertTrue(field.newDfp(+0.0).isZero()); Assert.assertTrue(field.newDfp(-0.0).isZero()); Assert.assertFalse(field.newDfp(1.0e-90).isZero()); Assert.assertFalse(nan.isZero()); Assert.assertFalse(nan.negate().isZero()); Assert.assertFalse(pinf.isZero()); Assert.assertFalse(pinf.negate().isZero()); Assert.assertFalse(ninf.isZero()); Assert.assertFalse(ninf.negate().isZero()); } @Test public void testSignPredicates() { Assert.assertTrue(field.getZero().negativeOrNull()); Assert.assertTrue(field.getZero().positiveOrNull()); Assert.assertFalse(field.getZero().strictlyNegative()); Assert.assertFalse(field.getZero().strictlyPositive()); Assert.assertTrue(field.getZero().negate().negativeOrNull()); Assert.assertTrue(field.getZero().negate().positiveOrNull()); Assert.assertFalse(field.getZero().negate().strictlyNegative()); Assert.assertFalse(field.getZero().negate().strictlyPositive()); Assert.assertFalse(field.getOne().negativeOrNull()); Assert.assertTrue(field.getOne().positiveOrNull()); Assert.assertFalse(field.getOne().strictlyNegative()); Assert.assertTrue(field.getOne().strictlyPositive()); Assert.assertTrue(field.getOne().negate().negativeOrNull()); Assert.assertFalse(field.getOne().negate().positiveOrNull()); Assert.assertTrue(field.getOne().negate().strictlyNegative()); Assert.assertFalse(field.getOne().negate().strictlyPositive()); Assert.assertFalse(nan.negativeOrNull()); Assert.assertFalse(nan.positiveOrNull()); Assert.assertFalse(nan.strictlyNegative()); Assert.assertFalse(nan.strictlyPositive()); Assert.assertFalse(nan.negate().negativeOrNull()); Assert.assertFalse(nan.negate().positiveOrNull()); Assert.assertFalse(nan.negate().strictlyNegative()); Assert.assertFalse(nan.negate().strictlyPositive()); Assert.assertFalse(pinf.negativeOrNull()); Assert.assertTrue(pinf.positiveOrNull()); Assert.assertFalse(pinf.strictlyNegative()); Assert.assertTrue(pinf.strictlyPositive()); Assert.assertTrue(pinf.negate().negativeOrNull()); Assert.assertFalse(pinf.negate().positiveOrNull()); Assert.assertTrue(pinf.negate().strictlyNegative()); Assert.assertFalse(pinf.negate().strictlyPositive()); Assert.assertTrue(ninf.negativeOrNull()); Assert.assertFalse(ninf.positiveOrNull()); Assert.assertTrue(ninf.strictlyNegative()); Assert.assertFalse(ninf.strictlyPositive()); Assert.assertFalse(ninf.negate().negativeOrNull()); Assert.assertTrue(ninf.negate().positiveOrNull()); Assert.assertFalse(ninf.negate().strictlyNegative()); Assert.assertTrue(ninf.negate().strictlyPositive()); } @Test public void testSpecialConstructors() { Assert.assertEquals(ninf, field.newDfp(Double.NEGATIVE_INFINITY)); Assert.assertEquals(ninf, field.newDfp("-Infinity")); Assert.assertEquals(pinf, field.newDfp(Double.POSITIVE_INFINITY)); Assert.assertEquals(pinf, field.newDfp("Infinity")); Assert.assertTrue(field.newDfp(Double.NaN).isNaN()); Assert.assertTrue(field.newDfp("NaN").isNaN()); } } commons-math3-3.2-src/src/test/java/org/apache/commons/math3/dfp/Decimal10.java100644 1750 1750 4766 12126627673 25713 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.dfp; public class Decimal10 extends DfpDec { Decimal10(final DfpField factory) { super(factory); } Decimal10(final DfpField factory, final byte x) { super(factory, x); } Decimal10(final DfpField factory, final int x) { super(factory, x); } Decimal10(final DfpField factory, final long x) { super(factory, x); } Decimal10(final DfpField factory, final double x) { super(factory, x); } public Decimal10(final Dfp d) { super(d); } public Decimal10(final DfpField factory, final String s) { super(factory, s); } protected Decimal10(final DfpField factory, final byte sign, final byte nans) { super(factory, sign, nans); } @Override public Dfp newInstance() { return new Decimal10(getField()); } @Override public Dfp newInstance(final byte x) { return new Decimal10(getField(), x); } @Override public Dfp newInstance(final int x) { return new Decimal10(getField(), x); } @Override public Dfp newInstance(final long x) { return new Decimal10(getField(), x); } @Override public Dfp newInstance(final double x) { return new Decimal10(getField(), x); } @Override public Dfp newInstance(final Dfp d) { return new Decimal10(d); } @Override public Dfp newInstance(final String s) { return new Decimal10(getField(), s); } @Override public Dfp newInstance(final byte sign, final byte nans) { return new Decimal10(getField(), sign, nans); } @Override protected int getDecimalDigits() { return 10; } } ././@LongLink100644 0 0 153 12126630646 10260 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/dfp/BracketingNthOrderBrentSolverDFPTest.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/dfp/BracketingNthOrderBrentSolverDFPTes100644 1750 1750 16270 12126627673 32220 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.dfp; import org.apache.commons.math3.analysis.solvers.AllowedSolution; import org.apache.commons.math3.exception.MathInternalError; import org.apache.commons.math3.exception.NumberIsTooSmallException; import org.junit.Assert; import org.junit.Before; import org.junit.Test; /** * Test case for {@link BracketingNthOrderBrentSolverDFP bracketing nth order Brent} solver. * * @version $Id: BracketingNthOrderBrentSolverDFPTest.java 1244107 2012-02-14 16:17:55Z erans $ */ public final class BracketingNthOrderBrentSolverDFPTest { @Test(expected=NumberIsTooSmallException.class) public void testInsufficientOrder3() { new BracketingNthOrderBrentSolverDFP(relativeAccuracy, absoluteAccuracy, functionValueAccuracy, 1); } @Test public void testConstructorOK() { BracketingNthOrderBrentSolverDFP solver = new BracketingNthOrderBrentSolverDFP(relativeAccuracy, absoluteAccuracy, functionValueAccuracy, 2); Assert.assertEquals(2, solver.getMaximalOrder()); } @Test public void testConvergenceOnFunctionAccuracy() { BracketingNthOrderBrentSolverDFP solver = new BracketingNthOrderBrentSolverDFP(relativeAccuracy, absoluteAccuracy, field.newDfp(1.0e-20), 20); UnivariateDfpFunction f = new UnivariateDfpFunction() { public Dfp value(Dfp x) { Dfp one = field.getOne(); Dfp oneHalf = one.divide(2); Dfp xMo = x.subtract(one); Dfp xMh = x.subtract(oneHalf); Dfp xPh = x.add(oneHalf); Dfp xPo = x.add(one); return xMo.multiply(xMh).multiply(x).multiply(xPh).multiply(xPo); } }; Dfp result = solver.solve(20, f, field.newDfp(0.2), field.newDfp(0.9), field.newDfp(0.4), AllowedSolution.BELOW_SIDE); Assert.assertTrue(f.value(result).abs().lessThan(solver.getFunctionValueAccuracy())); Assert.assertTrue(f.value(result).negativeOrNull()); Assert.assertTrue(result.subtract(field.newDfp(0.5)).subtract(solver.getAbsoluteAccuracy()).positiveOrNull()); result = solver.solve(20, f, field.newDfp(-0.9), field.newDfp(-0.2), field.newDfp(-0.4), AllowedSolution.ABOVE_SIDE); Assert.assertTrue(f.value(result).abs().lessThan(solver.getFunctionValueAccuracy())); Assert.assertTrue(f.value(result).positiveOrNull()); Assert.assertTrue(result.add(field.newDfp(0.5)).subtract(solver.getAbsoluteAccuracy()).negativeOrNull()); } @Test public void testNeta() { // the following test functions come from Beny Neta's paper: // "Several New Methods for solving Equations" // intern J. Computer Math Vol 23 pp 265-282 // available here: http://www.math.nps.navy.mil/~bneta/SeveralNewMethods.PDF for (AllowedSolution allowed : AllowedSolution.values()) { check(new UnivariateDfpFunction() { public Dfp value(Dfp x) { return DfpMath.sin(x).subtract(x.divide(2)); } }, 200, -2.0, 2.0, allowed); check(new UnivariateDfpFunction() { public Dfp value(Dfp x) { return DfpMath.pow(x, 5).add(x).subtract(field.newDfp(10000)); } }, 200, -5.0, 10.0, allowed); check(new UnivariateDfpFunction() { public Dfp value(Dfp x) { return x.sqrt().subtract(field.getOne().divide(x)).subtract(field.newDfp(3)); } }, 200, 0.001, 10.0, allowed); check(new UnivariateDfpFunction() { public Dfp value(Dfp x) { return DfpMath.exp(x).add(x).subtract(field.newDfp(20)); } }, 200, -5.0, 5.0, allowed); check(new UnivariateDfpFunction() { public Dfp value(Dfp x) { return DfpMath.log(x).add(x.sqrt()).subtract(field.newDfp(5)); } }, 200, 0.001, 10.0, allowed); check(new UnivariateDfpFunction() { public Dfp value(Dfp x) { return x.subtract(field.getOne()).multiply(x).multiply(x).subtract(field.getOne()); } }, 200, -0.5, 1.5, allowed); } } private void check(UnivariateDfpFunction f, int maxEval, double min, double max, AllowedSolution allowedSolution) { BracketingNthOrderBrentSolverDFP solver = new BracketingNthOrderBrentSolverDFP(relativeAccuracy, absoluteAccuracy, functionValueAccuracy, 20); Dfp xResult = solver.solve(maxEval, f, field.newDfp(min), field.newDfp(max), allowedSolution); Dfp yResult = f.value(xResult); switch (allowedSolution) { case ANY_SIDE : Assert.assertTrue(yResult.abs().lessThan(functionValueAccuracy.multiply(2))); break; case LEFT_SIDE : { boolean increasing = f.value(xResult).add(absoluteAccuracy).greaterThan(yResult); Assert.assertTrue(increasing ? yResult.negativeOrNull() : yResult.positiveOrNull()); break; } case RIGHT_SIDE : { boolean increasing = f.value(xResult).add(absoluteAccuracy).greaterThan(yResult); Assert.assertTrue(increasing ? yResult.positiveOrNull() : yResult.negativeOrNull()); break; } case BELOW_SIDE : Assert.assertTrue(yResult.negativeOrNull()); break; case ABOVE_SIDE : Assert.assertTrue(yResult.positiveOrNull()); break; default : // this should never happen throw new MathInternalError(null); } } @Before public void setUp() { field = new DfpField(50); absoluteAccuracy = field.newDfp(1.0e-45); relativeAccuracy = field.newDfp(1.0e-45); functionValueAccuracy = field.newDfp(1.0e-45); } private DfpField field; private Dfp absoluteAccuracy; private Dfp relativeAccuracy; private Dfp functionValueAccuracy; } commons-math3-3.2-src/src/test/java/org/apache/commons/math3/dfp/DfpDecTest.java100644 1750 1750 56002 12126627673 26207 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.dfp; import org.junit.After; import org.junit.Assert; import org.junit.Before; import org.junit.Test; public class DfpDecTest { private DfpField field; private Dfp pinf; private Dfp ninf; @Before public void setUp() { // Some basic setup. Define some constants and clear the status flags field = new DfpField(20); pinf = new DfpDec(field, 1).divide(new DfpDec(field, 0)); ninf = new DfpDec(field, -1).divide(new DfpDec(field, 0)); ninf.getField().clearIEEEFlags(); } @After public void tearDown() { field = null; pinf = null; ninf = null; } // Generic test function. Takes params x and y and tests them for // equality. Then checks the status flags against the flags argument. // If the test fail, it prints the desc string private void test(Dfp x, Dfp y, int flags, String desc) { boolean b = x.equals(y); if (!x.equals(y) && !x.unequal(y)) // NaNs involved b = (x.toString().equals(y.toString())); if (x.equals(new DfpDec(field, 0))) // distinguish +/- zero b = (b && (x.toString().equals(y.toString()))); b = (b && x.getField().getIEEEFlags() == flags); if (!b) Assert.assertTrue("assersion failed "+desc+" x = "+x.toString()+" flags = "+x.getField().getIEEEFlags(), b); x.getField().clearIEEEFlags(); } @Test public void testRound() { field.setRoundingMode(DfpField.RoundingMode.ROUND_HALF_EVEN); test(new DfpDec(field, "12345678901234567890"), new DfpDec(field, "12345678901234568000"), DfpField.FLAG_INEXACT, "Round #1"); test(new DfpDec(field, "0.12345678901234567890"), new DfpDec(field, "0.12345678901234568"), DfpField.FLAG_INEXACT, "Round #2"); test(new DfpDec(field, "0.12345678901234567500"), new DfpDec(field, "0.12345678901234568"), DfpField.FLAG_INEXACT, "Round #3"); test(new DfpDec(field, "0.12345678901234568500"), new DfpDec(field, "0.12345678901234568"), DfpField.FLAG_INEXACT, "Round #4"); test(new DfpDec(field, "0.12345678901234568501"), new DfpDec(field, "0.12345678901234569"), DfpField.FLAG_INEXACT, "Round #5"); test(new DfpDec(field, "0.12345678901234568499"), new DfpDec(field, "0.12345678901234568"), DfpField.FLAG_INEXACT, "Round #6"); test(new DfpDec(field, "1.2345678901234567890"), new DfpDec(field, "1.2345678901234568"), DfpField.FLAG_INEXACT, "Round #7"); test(new DfpDec(field, "1.2345678901234567500"), new DfpDec(field, "1.2345678901234568"), DfpField.FLAG_INEXACT, "Round #8"); test(new DfpDec(field, "1.2345678901234568500"), new DfpDec(field, "1.2345678901234568"), DfpField.FLAG_INEXACT, "Round #9"); test(new DfpDec(field, "1.2345678901234568000").add(new DfpDec(field, ".0000000000000000501")), new DfpDec(field, "1.2345678901234569"), DfpField.FLAG_INEXACT, "Round #10"); test(new DfpDec(field, "1.2345678901234568499"), new DfpDec(field, "1.2345678901234568"), DfpField.FLAG_INEXACT, "Round #11"); test(new DfpDec(field, "12.345678901234567890"), new DfpDec(field, "12.345678901234568"), DfpField.FLAG_INEXACT, "Round #12"); test(new DfpDec(field, "12.345678901234567500"), new DfpDec(field, "12.345678901234568"), DfpField.FLAG_INEXACT, "Round #13"); test(new DfpDec(field, "12.345678901234568500"), new DfpDec(field, "12.345678901234568"), DfpField.FLAG_INEXACT, "Round #14"); test(new DfpDec(field, "12.345678901234568").add(new DfpDec(field, ".000000000000000501")), new DfpDec(field, "12.345678901234569"), DfpField.FLAG_INEXACT, "Round #15"); test(new DfpDec(field, "12.345678901234568499"), new DfpDec(field, "12.345678901234568"), DfpField.FLAG_INEXACT, "Round #16"); test(new DfpDec(field, "123.45678901234567890"), new DfpDec(field, "123.45678901234568"), DfpField.FLAG_INEXACT, "Round #17"); test(new DfpDec(field, "123.45678901234567500"), new DfpDec(field, "123.45678901234568"), DfpField.FLAG_INEXACT, "Round #18"); test(new DfpDec(field, "123.45678901234568500"), new DfpDec(field, "123.45678901234568"), DfpField.FLAG_INEXACT, "Round #19"); test(new DfpDec(field, "123.456789012345685").add(new DfpDec(field, ".00000000000000501")), new DfpDec(field, "123.45678901234569"), DfpField.FLAG_INEXACT, "Round #20"); test(new DfpDec(field, "123.45678901234568499"), new DfpDec(field, "123.45678901234568"), DfpField.FLAG_INEXACT, "Round #21"); field.setRoundingMode(DfpField.RoundingMode.ROUND_DOWN); // Round down test(new DfpDec(field, "12345678901234567").add(new DfpDec(field, "0.9")), new DfpDec(field, "12345678901234567"), DfpField.FLAG_INEXACT, "Round #22"); test(new DfpDec(field, "12345678901234567").add(new DfpDec(field, "0.99999999")), new DfpDec(field, "12345678901234567"), DfpField.FLAG_INEXACT, "Round #23"); test(new DfpDec(field, "-12345678901234567").add(new DfpDec(field, "-0.99999999")), new DfpDec(field, "-12345678901234567"), DfpField.FLAG_INEXACT, "Round #24"); field.setRoundingMode(DfpField.RoundingMode.ROUND_UP); // Round up test(new DfpDec(field, "12345678901234567").add(new DfpDec(field, "0.1")), new DfpDec(field, "12345678901234568"), DfpField.FLAG_INEXACT, "Round #25"); test(new DfpDec(field, "12345678901234567").add(new DfpDec(field, "0.0001")), new DfpDec(field, "12345678901234568"), DfpField.FLAG_INEXACT, "Round #26"); test(new DfpDec(field, "-12345678901234567").add(new DfpDec(field, "-0.1")), new DfpDec(field, "-12345678901234568"), DfpField.FLAG_INEXACT, "Round #27"); test(new DfpDec(field, "-12345678901234567").add(new DfpDec(field, "-0.0001")), new DfpDec(field, "-12345678901234568"), DfpField.FLAG_INEXACT, "Round #28"); test(new DfpDec(field, "-12345678901234567").add(new DfpDec(field, "0")), new DfpDec(field, "-12345678901234567"), 0, "Round #28.5"); field.setRoundingMode(DfpField.RoundingMode.ROUND_HALF_UP); // Round half up test(new DfpDec(field, "12345678901234567").add(new DfpDec(field, "0.499999999999")), new DfpDec(field, "12345678901234567"), DfpField.FLAG_INEXACT, "Round #29"); test(new DfpDec(field, "12345678901234567").add(new DfpDec(field, "0.50000001")), new DfpDec(field, "12345678901234568"), DfpField.FLAG_INEXACT, "Round #30"); test(new DfpDec(field, "12345678901234567").add(new DfpDec(field, "0.5")), new DfpDec(field, "12345678901234568"), DfpField.FLAG_INEXACT, "Round #30.5"); test(new DfpDec(field, "-12345678901234567").add(new DfpDec(field, "-0.499999999999")), new DfpDec(field, "-12345678901234567"), DfpField.FLAG_INEXACT, "Round #31"); test(new DfpDec(field, "-12345678901234567").add(new DfpDec(field, "-0.50000001")), new DfpDec(field, "-12345678901234568"), DfpField.FLAG_INEXACT, "Round #32"); field.setRoundingMode(DfpField.RoundingMode.ROUND_HALF_DOWN); // Round half down test(new DfpDec(field, "12345678901234567").add(new DfpDec(field, "0.5001")), new DfpDec(field, "12345678901234568"), DfpField.FLAG_INEXACT, "Round #33"); test(new DfpDec(field, "12345678901234567").add(new DfpDec(field, "0.5000")), new DfpDec(field, "12345678901234567"), DfpField.FLAG_INEXACT, "Round #34"); test(new DfpDec(field, "-12345678901234567").add(new DfpDec(field, "-0.5001")), new DfpDec(field, "-12345678901234568"), DfpField.FLAG_INEXACT, "Round #35"); test(new DfpDec(field, "-12345678901234567").add(new DfpDec(field, "-0.6")), new DfpDec(field, "-12345678901234568"), DfpField.FLAG_INEXACT, "Round #35.5"); test(new DfpDec(field, "-12345678901234567").add(new DfpDec(field, "-0.5000")), new DfpDec(field, "-12345678901234567"), DfpField.FLAG_INEXACT, "Round #36"); field.setRoundingMode(DfpField.RoundingMode.ROUND_HALF_ODD); // Round half odd test(new DfpDec(field, "12345678901234568").add(new DfpDec(field, "0.5000")), new DfpDec(field, "12345678901234569"), DfpField.FLAG_INEXACT, "Round #37"); test(new DfpDec(field, "12345678901234567").add(new DfpDec(field, "0.5000")), new DfpDec(field, "12345678901234567"), DfpField.FLAG_INEXACT, "Round #38"); test(new DfpDec(field, "-12345678901234568").add(new DfpDec(field, "-0.5000")), new DfpDec(field, "-12345678901234569"), DfpField.FLAG_INEXACT, "Round #39"); test(new DfpDec(field, "-12345678901234567").add(new DfpDec(field, "-0.5000")), new DfpDec(field, "-12345678901234567"), DfpField.FLAG_INEXACT, "Round #40"); field.setRoundingMode(DfpField.RoundingMode.ROUND_CEIL); // Round ceil test(new DfpDec(field, "12345678901234567").add(new DfpDec(field, "0.0001")), new DfpDec(field, "12345678901234568"), DfpField.FLAG_INEXACT, "Round #41"); test(new DfpDec(field, "-12345678901234567").add(new DfpDec(field, "-0.9999")), new DfpDec(field, "-12345678901234567"), DfpField.FLAG_INEXACT, "Round #42"); field.setRoundingMode(DfpField.RoundingMode.ROUND_FLOOR); // Round floor test(new DfpDec(field, "12345678901234567").add(new DfpDec(field, "0.9999")), new DfpDec(field, "12345678901234567"), DfpField.FLAG_INEXACT, "Round #43"); test(new DfpDec(field, "-12345678901234567").add(new DfpDec(field, "-0.0001")), new DfpDec(field, "-12345678901234568"), DfpField.FLAG_INEXACT, "Round #44"); field.setRoundingMode(DfpField.RoundingMode.ROUND_HALF_EVEN); // reset } @Test public void testRoundDecimal10() { field.setRoundingMode(DfpField.RoundingMode.ROUND_HALF_EVEN); test(new Decimal10(field, "1234567891234567890"), new Decimal10(field, "1234567891000000000"), DfpField.FLAG_INEXACT, "RoundDecimal10 #1"); test(new Decimal10(field, "0.1234567891634567890"), new Decimal10(field, "0.1234567892"), DfpField.FLAG_INEXACT, "RoundDecimal10 #2"); test(new Decimal10(field, "0.1234567891500000000"), new Decimal10(field, "0.1234567892"), DfpField.FLAG_INEXACT, "RoundDecimal10 #3"); test(new Decimal10(field, "0.1234567890500"), new Decimal10(field, "0.1234567890"), DfpField.FLAG_INEXACT, "RoundDecimal10 #4"); test(new Decimal10(field, "0.1234567890501"), new Decimal10(field, "0.1234567891"), DfpField.FLAG_INEXACT, "RoundDecimal10 #5"); test(new Decimal10(field, "0.1234567890499"), new Decimal10(field, "0.1234567890"), DfpField.FLAG_INEXACT, "RoundDecimal10 #6"); test(new Decimal10(field, "1.234567890890"), new Decimal10(field, "1.234567891"), DfpField.FLAG_INEXACT, "RoundDecimal10 #7"); test(new Decimal10(field, "1.234567891500"), new Decimal10(field, "1.234567892"), DfpField.FLAG_INEXACT, "RoundDecimal10 #8"); test(new Decimal10(field, "1.234567890500"), new Decimal10(field, "1.234567890"), DfpField.FLAG_INEXACT, "RoundDecimal10 #9"); test(new Decimal10(field, "1.234567890000").add(new Decimal10(field, ".000000000501")), new Decimal10(field, "1.234567891"), DfpField.FLAG_INEXACT, "RoundDecimal10 #10"); test(new Decimal10(field, "1.234567890499"), new Decimal10(field, "1.234567890"), DfpField.FLAG_INEXACT, "RoundDecimal10 #11"); test(new Decimal10(field, "12.34567890890"), new Decimal10(field, "12.34567891"), DfpField.FLAG_INEXACT, "RoundDecimal10 #12"); test(new Decimal10(field, "12.34567891500"), new Decimal10(field, "12.34567892"), DfpField.FLAG_INEXACT, "RoundDecimal10 #13"); test(new Decimal10(field, "12.34567890500"), new Decimal10(field, "12.34567890"), DfpField.FLAG_INEXACT, "RoundDecimal10 #14"); test(new Decimal10(field, "12.34567890").add(new Decimal10(field, ".00000000501")), new Decimal10(field, "12.34567891"), DfpField.FLAG_INEXACT, "RoundDecimal10 #15"); test(new Decimal10(field, "12.34567890499"), new Decimal10(field, "12.34567890"), DfpField.FLAG_INEXACT, "RoundDecimal10 #16"); test(new Decimal10(field, "123.4567890890"), new Decimal10(field, "123.4567891"), DfpField.FLAG_INEXACT, "RoundDecimal10 #17"); test(new Decimal10(field, "123.4567891500"), new Decimal10(field, "123.4567892"), DfpField.FLAG_INEXACT, "RoundDecimal10 #18"); test(new Decimal10(field, "123.4567890500"), new Decimal10(field, "123.4567890"), DfpField.FLAG_INEXACT, "RoundDecimal10 #19"); test(new Decimal10(field, "123.4567890").add(new Decimal10(field, ".0000000501")), new Decimal10(field, "123.4567891"), DfpField.FLAG_INEXACT, "RoundDecimal10 #20"); test(new Decimal10(field, "123.4567890499"), new Decimal10(field, "123.4567890"), DfpField.FLAG_INEXACT, "RoundDecimal10 #21"); field.setRoundingMode(DfpField.RoundingMode.ROUND_DOWN); // RoundDecimal10 down test(new Decimal10(field, "1234567890").add(new Decimal10(field, "0.9")), new Decimal10(field, "1234567890"), DfpField.FLAG_INEXACT, "RoundDecimal10 #22"); test(new Decimal10(field, "1234567890").add(new Decimal10(field, "0.99999999")), new Decimal10(field, "1234567890"), DfpField.FLAG_INEXACT, "RoundDecimal10 #23"); test(new Decimal10(field, "-1234567890").add(new Decimal10(field, "-0.99999999")), new Decimal10(field, "-1234567890"), DfpField.FLAG_INEXACT, "RoundDecimal10 #24"); field.setRoundingMode(DfpField.RoundingMode.ROUND_UP); // RoundDecimal10 up test(new Decimal10(field, 1234567890).add(new Decimal10(field, "0.1")), new Decimal10(field, 1234567891l), DfpField.FLAG_INEXACT, "RoundDecimal10 #25"); test(new Decimal10(field, "1234567890").add(new Decimal10(field, "0.0001")), new Decimal10(field, "1234567891"), DfpField.FLAG_INEXACT, "RoundDecimal10 #26"); test(new Decimal10(field, "-1234567890").add(new Decimal10(field, "-0.1")), new Decimal10(field, "-1234567891"), DfpField.FLAG_INEXACT, "RoundDecimal10 #27"); test(new Decimal10(field, "-1234567890").add(new Decimal10(field, "-0.0001")), new Decimal10(field, "-1234567891"), DfpField.FLAG_INEXACT, "RoundDecimal10 #28"); test(new Decimal10(field, "-1234567890").add(new Decimal10(field, "0")), new Decimal10(field, "-1234567890"), 0, "RoundDecimal10 #28.5"); field.setRoundingMode(DfpField.RoundingMode.ROUND_HALF_UP); // RoundDecimal10 half up test(new Decimal10(field, "1234567890").add(new Decimal10(field, "0.4999999999")), new Decimal10(field, "1234567890"), DfpField.FLAG_INEXACT, "RoundDecimal10 #29"); test(new Decimal10(field, "1234567890").add(new Decimal10(field, "0.50000001")), new Decimal10(field, "1234567891"), DfpField.FLAG_INEXACT, "RoundDecimal10 #30"); test(new Decimal10(field, "1234567890").add(new Decimal10(field, "0.5")), new Decimal10(field, "1234567891"), DfpField.FLAG_INEXACT, "RoundDecimal10 #30.5"); test(new Decimal10(field, "-1234567890").add(new Decimal10(field, "-0.4999999999")), new Decimal10(field, "-1234567890"), DfpField.FLAG_INEXACT, "RoundDecimal10 #31"); test(new Decimal10(field, "-1234567890").add(new Decimal10(field, "-0.50000001")), new Decimal10(field, "-1234567891"), DfpField.FLAG_INEXACT, "RoundDecimal10 #32"); field.setRoundingMode(DfpField.RoundingMode.ROUND_HALF_DOWN); // RoundDecimal10 half down test(new Decimal10(field, "1234567890").add(new Decimal10(field, "0.5001")), new Decimal10(field, "1234567890"), DfpField.FLAG_INEXACT, "RoundDecimal10 #33"); test(new Decimal10(field, "1234567890").add(new Decimal10(field, "0.5000")), new Decimal10(field, "1234567890"), DfpField.FLAG_INEXACT, "RoundDecimal10 #34"); test(new Decimal10(field, "-1234567890").add(new Decimal10(field, "-0.5001")), new Decimal10(field, "-1234567890"), DfpField.FLAG_INEXACT, "RoundDecimal10 #35"); test(new Decimal10(field, "-1234567890").add(new Decimal10(field, "-0.6")), new Decimal10(field, "-1234567891"), DfpField.FLAG_INEXACT, "RoundDecimal10 #35.5"); test(new Decimal10(field, "-1234567890").add(new Decimal10(field, "-0.5000")), new Decimal10(field, "-1234567890"), DfpField.FLAG_INEXACT, "RoundDecimal10 #36"); field.setRoundingMode(DfpField.RoundingMode.ROUND_HALF_ODD); // RoundDecimal10 half odd test(new Decimal10(field, "1234567890").add(new Decimal10(field, "0.5000")), new Decimal10(field, "1234567891"), DfpField.FLAG_INEXACT, "RoundDecimal10 #37"); test(new Decimal10(field, "1234567891").add(new Decimal10(field, "0.5000")), new Decimal10(field, "1234567891"), DfpField.FLAG_INEXACT, "RoundDecimal10 #38"); test(new Decimal10(field, "-1234567890").add(new Decimal10(field, "-0.5000")), new Decimal10(field, "-1234567891"), DfpField.FLAG_INEXACT, "RoundDecimal10 #39"); test(new Decimal10(field, "-1234567891").add(new Decimal10(field, "-0.5000")), new Decimal10(field, "-1234567891"), DfpField.FLAG_INEXACT, "RoundDecimal10 #40"); field.setRoundingMode(DfpField.RoundingMode.ROUND_CEIL); // RoundDecimal10 ceil test(new Decimal10(field, "1234567890").add(new Decimal10(field, "0.0001")), new Decimal10(field, "1234567891"), DfpField.FLAG_INEXACT, "RoundDecimal10 #41"); test(new Decimal10(field, "-1234567890").add(new Decimal10(field, "-0.9999")), new Decimal10(field, "-1234567890"), DfpField.FLAG_INEXACT, "RoundDecimal10 #42"); field.setRoundingMode(DfpField.RoundingMode.ROUND_FLOOR); // RoundDecimal10 floor test(new Decimal10(field, "1234567890").add(new Decimal10(field, "0.9999")), new Decimal10(field, "1234567890"), DfpField.FLAG_INEXACT, "RoundDecimal10 #43"); test(new Decimal10(field, "-1234567890").add(new Decimal10(field, "-0.0001")), new Decimal10(field, "-1234567891"), DfpField.FLAG_INEXACT, "RoundDecimal10 #44"); field.setRoundingMode(DfpField.RoundingMode.ROUND_HALF_EVEN); // reset } @Test public void testNextAfter() { test(new DfpDec(field, 1).nextAfter(pinf), new DfpDec(field, "1.0000000000000001"), 0, "NextAfter #1"); test(new DfpDec(field, "1.0000000000000001").nextAfter(ninf), new DfpDec(field, 1), 0, "NextAfter #1.5"); test(new DfpDec(field, 1).nextAfter(ninf), new DfpDec(field, "0.99999999999999999"), 0, "NextAfter #2"); test(new DfpDec(field, "0.99999999999999999").nextAfter(new DfpDec(field, 2)), new DfpDec(field, 1), 0, "NextAfter #3"); test(new DfpDec(field, -1).nextAfter(ninf), new DfpDec(field, "-1.0000000000000001"), 0, "NextAfter #4"); test(new DfpDec(field, -1).nextAfter(pinf), new DfpDec(field, "-0.99999999999999999"), 0, "NextAfter #5"); test(new DfpDec(field, "-0.99999999999999999").nextAfter(new DfpDec(field, -2)), new DfpDec(field, (byte) -1), 0, "NextAfter #6"); test(new DfpDec(field, (byte) 2).nextAfter(new DfpDec(field, 2)), new DfpDec(field, 2l), 0, "NextAfter #7"); test(new DfpDec(field, 0).nextAfter(new DfpDec(field, 0)), new DfpDec(field, 0), 0, "NextAfter #8"); test(new DfpDec(field, -2).nextAfter(new DfpDec(field, -2)), new DfpDec(field, -2), 0, "NextAfter #9"); test(new DfpDec(field, 0).nextAfter(new DfpDec(field, 1)), new DfpDec(field, "1e-131092"), DfpField.FLAG_UNDERFLOW, "NextAfter #10"); test(new DfpDec(field, 0).nextAfter(new DfpDec(field, -1)), new DfpDec(field, "-1e-131092"), DfpField.FLAG_UNDERFLOW, "NextAfter #11"); test(new DfpDec(field, "-1e-131092").nextAfter(pinf), new DfpDec(field, "-0"), DfpField.FLAG_UNDERFLOW|DfpField.FLAG_INEXACT, "Next After #12"); test(new DfpDec(field, "1e-131092").nextAfter(ninf), new DfpDec(field, "0"), DfpField.FLAG_UNDERFLOW|DfpField.FLAG_INEXACT, "Next After #13"); test(new DfpDec(field, "9.9999999999999999e131078").nextAfter(pinf), pinf, DfpField.FLAG_OVERFLOW|DfpField.FLAG_INEXACT, "Next After #14"); } } ././@LongLink100644 0 0 162 12126630646 10260 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/stat/regression/OLSMultipleLinearRegressionTest.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/stat/regression/OLSMultipleLinearRegres100644 1750 1750 74152 12126627674 32347 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.stat.regression; import org.apache.commons.math3.TestUtils; import org.apache.commons.math3.linear.DefaultRealMatrixChangingVisitor; import org.apache.commons.math3.linear.MatrixUtils; import org.apache.commons.math3.linear.RealMatrix; import org.apache.commons.math3.linear.Array2DRowRealMatrix; import org.apache.commons.math3.linear.RealVector; import org.apache.commons.math3.stat.StatUtils; import org.junit.Assert; import org.junit.Before; import org.junit.Test; public class OLSMultipleLinearRegressionTest extends MultipleLinearRegressionAbstractTest { private double[] y; private double[][] x; @Before @Override public void setUp(){ y = new double[]{11.0, 12.0, 13.0, 14.0, 15.0, 16.0}; x = new double[6][]; x[0] = new double[]{0, 0, 0, 0, 0}; x[1] = new double[]{2.0, 0, 0, 0, 0}; x[2] = new double[]{0, 3.0, 0, 0, 0}; x[3] = new double[]{0, 0, 4.0, 0, 0}; x[4] = new double[]{0, 0, 0, 5.0, 0}; x[5] = new double[]{0, 0, 0, 0, 6.0}; super.setUp(); } @Override protected OLSMultipleLinearRegression createRegression() { OLSMultipleLinearRegression regression = new OLSMultipleLinearRegression(); regression.newSampleData(y, x); return regression; } @Override protected int getNumberOfRegressors() { return x[0].length + 1; } @Override protected int getSampleSize() { return y.length; } @Test(expected=IllegalArgumentException.class) public void cannotAddSampleDataWithSizeMismatch() { double[] y = new double[]{1.0, 2.0}; double[][] x = new double[1][]; x[0] = new double[]{1.0, 0}; createRegression().newSampleData(y, x); } @Test public void testPerfectFit() { double[] betaHat = regression.estimateRegressionParameters(); TestUtils.assertEquals(betaHat, new double[]{ 11.0, 1.0 / 2.0, 2.0 / 3.0, 3.0 / 4.0, 4.0 / 5.0, 5.0 / 6.0 }, 1e-14); double[] residuals = regression.estimateResiduals(); TestUtils.assertEquals(residuals, new double[]{0d,0d,0d,0d,0d,0d}, 1e-14); RealMatrix errors = new Array2DRowRealMatrix(regression.estimateRegressionParametersVariance(), false); final double[] s = { 1.0, -1.0 / 2.0, -1.0 / 3.0, -1.0 / 4.0, -1.0 / 5.0, -1.0 / 6.0 }; RealMatrix referenceVariance = new Array2DRowRealMatrix(s.length, s.length); referenceVariance.walkInOptimizedOrder(new DefaultRealMatrixChangingVisitor() { @Override public double visit(int row, int column, double value) { if (row == 0) { return s[column]; } double x = s[row] * s[column]; return (row == column) ? 2 * x : x; } }); Assert.assertEquals(0.0, errors.subtract(referenceVariance).getNorm(), 5.0e-16 * referenceVariance.getNorm()); Assert.assertEquals(1, ((OLSMultipleLinearRegression) regression).calculateRSquared(), 1E-12); } /** * Test Longley dataset against certified values provided by NIST. * Data Source: J. Longley (1967) "An Appraisal of Least Squares * Programs for the Electronic Computer from the Point of View of the User" * Journal of the American Statistical Association, vol. 62. September, * pp. 819-841. * * Certified values (and data) are from NIST: * http://www.itl.nist.gov/div898/strd/lls/data/LINKS/DATA/Longley.dat */ @Test public void testLongly() { // Y values are first, then independent vars // Each row is one observation double[] design = new double[] { 60323,83.0,234289,2356,1590,107608,1947, 61122,88.5,259426,2325,1456,108632,1948, 60171,88.2,258054,3682,1616,109773,1949, 61187,89.5,284599,3351,1650,110929,1950, 63221,96.2,328975,2099,3099,112075,1951, 63639,98.1,346999,1932,3594,113270,1952, 64989,99.0,365385,1870,3547,115094,1953, 63761,100.0,363112,3578,3350,116219,1954, 66019,101.2,397469,2904,3048,117388,1955, 67857,104.6,419180,2822,2857,118734,1956, 68169,108.4,442769,2936,2798,120445,1957, 66513,110.8,444546,4681,2637,121950,1958, 68655,112.6,482704,3813,2552,123366,1959, 69564,114.2,502601,3931,2514,125368,1960, 69331,115.7,518173,4806,2572,127852,1961, 70551,116.9,554894,4007,2827,130081,1962 }; final int nobs = 16; final int nvars = 6; // Estimate the model OLSMultipleLinearRegression model = new OLSMultipleLinearRegression(); model.newSampleData(design, nobs, nvars); // Check expected beta values from NIST double[] betaHat = model.estimateRegressionParameters(); TestUtils.assertEquals(betaHat, new double[]{-3482258.63459582, 15.0618722713733, -0.358191792925910E-01,-2.02022980381683, -1.03322686717359,-0.511041056535807E-01, 1829.15146461355}, 2E-8); // // Check expected residuals from R double[] residuals = model.estimateResiduals(); TestUtils.assertEquals(residuals, new double[]{ 267.340029759711,-94.0139423988359,46.28716775752924, -410.114621930906,309.7145907602313,-249.3112153297231, -164.0489563956039,-13.18035686637081,14.30477260005235, 455.394094551857,-17.26892711483297,-39.0550425226967, -155.5499735953195,-85.6713080421283,341.9315139607727, -206.7578251937366}, 1E-8); // Check standard errors from NIST double[] errors = model.estimateRegressionParametersStandardErrors(); TestUtils.assertEquals(new double[] {890420.383607373, 84.9149257747669, 0.334910077722432E-01, 0.488399681651699, 0.214274163161675, 0.226073200069370, 455.478499142212}, errors, 1E-6); // Check regression standard error against R Assert.assertEquals(304.8540735619638, model.estimateRegressionStandardError(), 1E-10); // Check R-Square statistics against R Assert.assertEquals(0.995479004577296, model.calculateRSquared(), 1E-12); Assert.assertEquals(0.992465007628826, model.calculateAdjustedRSquared(), 1E-12); checkVarianceConsistency(model); // Estimate model without intercept model.setNoIntercept(true); model.newSampleData(design, nobs, nvars); // Check expected beta values from R betaHat = model.estimateRegressionParameters(); TestUtils.assertEquals(betaHat, new double[]{-52.99357013868291, 0.07107319907358, -0.42346585566399,-0.57256866841929, -0.41420358884978, 48.41786562001326}, 1E-11); // Check standard errors from R errors = model.estimateRegressionParametersStandardErrors(); TestUtils.assertEquals(new double[] {129.54486693117232, 0.03016640003786, 0.41773654056612, 0.27899087467676, 0.32128496193363, 17.68948737819961}, errors, 1E-11); // Check expected residuals from R residuals = model.estimateResiduals(); TestUtils.assertEquals(residuals, new double[]{ 279.90274927293092, -130.32465380836874, 90.73228661967445, -401.31252201634948, -440.46768772620027, -543.54512853774793, 201.32111639536299, 215.90889365977932, 73.09368242049943, 913.21694494481869, 424.82484953610174, -8.56475876776709, -361.32974610842876, 27.34560497213464, 151.28955976355002, -492.49937355336846}, 1E-10); // Check regression standard error against R Assert.assertEquals(475.1655079819517, model.estimateRegressionStandardError(), 1E-10); // Check R-Square statistics against R Assert.assertEquals(0.9999670130706, model.calculateRSquared(), 1E-12); Assert.assertEquals(0.999947220913, model.calculateAdjustedRSquared(), 1E-12); } /** * Test R Swiss fertility dataset against R. * Data Source: R datasets package */ @Test public void testSwissFertility() { double[] design = new double[] { 80.2,17.0,15,12,9.96, 83.1,45.1,6,9,84.84, 92.5,39.7,5,5,93.40, 85.8,36.5,12,7,33.77, 76.9,43.5,17,15,5.16, 76.1,35.3,9,7,90.57, 83.8,70.2,16,7,92.85, 92.4,67.8,14,8,97.16, 82.4,53.3,12,7,97.67, 82.9,45.2,16,13,91.38, 87.1,64.5,14,6,98.61, 64.1,62.0,21,12,8.52, 66.9,67.5,14,7,2.27, 68.9,60.7,19,12,4.43, 61.7,69.3,22,5,2.82, 68.3,72.6,18,2,24.20, 71.7,34.0,17,8,3.30, 55.7,19.4,26,28,12.11, 54.3,15.2,31,20,2.15, 65.1,73.0,19,9,2.84, 65.5,59.8,22,10,5.23, 65.0,55.1,14,3,4.52, 56.6,50.9,22,12,15.14, 57.4,54.1,20,6,4.20, 72.5,71.2,12,1,2.40, 74.2,58.1,14,8,5.23, 72.0,63.5,6,3,2.56, 60.5,60.8,16,10,7.72, 58.3,26.8,25,19,18.46, 65.4,49.5,15,8,6.10, 75.5,85.9,3,2,99.71, 69.3,84.9,7,6,99.68, 77.3,89.7,5,2,100.00, 70.5,78.2,12,6,98.96, 79.4,64.9,7,3,98.22, 65.0,75.9,9,9,99.06, 92.2,84.6,3,3,99.46, 79.3,63.1,13,13,96.83, 70.4,38.4,26,12,5.62, 65.7,7.7,29,11,13.79, 72.7,16.7,22,13,11.22, 64.4,17.6,35,32,16.92, 77.6,37.6,15,7,4.97, 67.6,18.7,25,7,8.65, 35.0,1.2,37,53,42.34, 44.7,46.6,16,29,50.43, 42.8,27.7,22,29,58.33 }; final int nobs = 47; final int nvars = 4; // Estimate the model OLSMultipleLinearRegression model = new OLSMultipleLinearRegression(); model.newSampleData(design, nobs, nvars); // Check expected beta values from R double[] betaHat = model.estimateRegressionParameters(); TestUtils.assertEquals(betaHat, new double[]{91.05542390271397, -0.22064551045715, -0.26058239824328, -0.96161238456030, 0.12441843147162}, 1E-12); // Check expected residuals from R double[] residuals = model.estimateResiduals(); TestUtils.assertEquals(residuals, new double[]{ 7.1044267859730512,1.6580347433531366, 4.6944952770029644,8.4548022690166160,13.6547432343186212, -9.3586864458500774,7.5822446330520386,15.5568995563859289, 0.8113090736598980,7.1186762732484308,7.4251378771228724, 2.6761316873234109,0.8351584810309354,7.1769991119615177, -3.8746753206299553,-3.1337779476387251,-0.1412575244091504, 1.1186809170469780,-6.3588097346816594,3.4039270429434074, 2.3374058329820175,-7.9272368576900503,-7.8361010968497959, -11.2597369269357070,0.9445333697827101,6.6544245101380328, -0.9146136301118665,-4.3152449403848570,-4.3536932047009183, -3.8907885169304661,-6.3027643926302188,-7.8308982189289091, -3.1792280015332750,-6.7167298771158226,-4.8469946718041754, -10.6335664353633685,11.1031134362036958,6.0084032641811733, 5.4326230830188482,-7.2375578629692230,2.1671550814448222, 15.0147574652763112,4.8625103516321015,-7.1597256413907706, -0.4515205619767598,-10.2916870903837587,-15.7812984571900063}, 1E-12); // Check standard errors from R double[] errors = model.estimateRegressionParametersStandardErrors(); TestUtils.assertEquals(new double[] {6.94881329475087, 0.07360008972340, 0.27410957467466, 0.19454551679325, 0.03726654773803}, errors, 1E-10); // Check regression standard error against R Assert.assertEquals(7.73642194433223, model.estimateRegressionStandardError(), 1E-12); // Check R-Square statistics against R Assert.assertEquals(0.649789742860228, model.calculateRSquared(), 1E-12); Assert.assertEquals(0.6164363850373927, model.calculateAdjustedRSquared(), 1E-12); checkVarianceConsistency(model); // Estimate the model with no intercept model = new OLSMultipleLinearRegression(); model.setNoIntercept(true); model.newSampleData(design, nobs, nvars); // Check expected beta values from R betaHat = model.estimateRegressionParameters(); TestUtils.assertEquals(betaHat, new double[]{0.52191832900513, 2.36588087917963, -0.94770353802795, 0.30851985863609}, 1E-12); // Check expected residuals from R residuals = model.estimateResiduals(); TestUtils.assertEquals(residuals, new double[]{ 44.138759883538249, 27.720705122356215, 35.873200836126799, 34.574619581211977, 26.600168342080213, 15.074636243026923, -12.704904871199814, 1.497443824078134, 2.691972687079431, 5.582798774291231, -4.422986561283165, -9.198581600334345, 4.481765170730647, 2.273520207553216, -22.649827853221336, -17.747900013943308, 20.298314638496436, 6.861405135329779, -8.684712790954924, -10.298639278062371, -9.896618896845819, 4.568568616351242, -15.313570491727944, -13.762961360873966, 7.156100301980509, 16.722282219843990, 26.716200609071898, -1.991466398777079, -2.523342564719335, 9.776486693095093, -5.297535127628603, -16.639070567471094, -10.302057295211819, -23.549487860816846, 1.506624392156384, -17.939174438345930, 13.105792202765040, -1.943329906928462, -1.516005841666695, -0.759066561832886, 20.793137744128977, -2.485236153005426, 27.588238710486976, 2.658333257106881, -15.998337823623046, -5.550742066720694, -14.219077806826615}, 1E-12); // Check standard errors from R errors = model.estimateRegressionParametersStandardErrors(); TestUtils.assertEquals(new double[] {0.10470063765677, 0.41684100584290, 0.43370143099691, 0.07694953606522}, errors, 1E-10); // Check regression standard error against R Assert.assertEquals(17.24710630547, model.estimateRegressionStandardError(), 1E-10); // Check R-Square statistics against R Assert.assertEquals(0.946350722085, model.calculateRSquared(), 1E-12); Assert.assertEquals(0.9413600915813, model.calculateAdjustedRSquared(), 1E-12); } /** * Test hat matrix computation * */ @Test public void testHat() { /* * This example is from "The Hat Matrix in Regression and ANOVA", * David C. Hoaglin and Roy E. Welsch, * The American Statistician, Vol. 32, No. 1 (Feb., 1978), pp. 17-22. * */ double[] design = new double[] { 11.14, .499, 11.1, 12.74, .558, 8.9, 13.13, .604, 8.8, 11.51, .441, 8.9, 12.38, .550, 8.8, 12.60, .528, 9.9, 11.13, .418, 10.7, 11.7, .480, 10.5, 11.02, .406, 10.5, 11.41, .467, 10.7 }; int nobs = 10; int nvars = 2; // Estimate the model OLSMultipleLinearRegression model = new OLSMultipleLinearRegression(); model.newSampleData(design, nobs, nvars); RealMatrix hat = model.calculateHat(); // Reference data is upper half of symmetric hat matrix double[] referenceData = new double[] { .418, -.002, .079, -.274, -.046, .181, .128, .222, .050, .242, .242, .292, .136, .243, .128, -.041, .033, -.035, .004, .417, -.019, .273, .187, -.126, .044, -.153, .004, .604, .197, -.038, .168, -.022, .275, -.028, .252, .111, -.030, .019, -.010, -.010, .148, .042, .117, .012, .111, .262, .145, .277, .174, .154, .120, .168, .315, .148, .187 }; // Check against reference data and verify symmetry int k = 0; for (int i = 0; i < 10; i++) { for (int j = i; j < 10; j++) { Assert.assertEquals(referenceData[k], hat.getEntry(i, j), 10e-3); Assert.assertEquals(hat.getEntry(i, j), hat.getEntry(j, i), 10e-12); k++; } } /* * Verify that residuals computed using the hat matrix are close to * what we get from direct computation, i.e. r = (I - H) y */ double[] residuals = model.estimateResiduals(); RealMatrix I = MatrixUtils.createRealIdentityMatrix(10); double[] hatResiduals = I.subtract(hat).operate(model.getY()).toArray(); TestUtils.assertEquals(residuals, hatResiduals, 10e-12); } /** * test calculateYVariance */ @Test public void testYVariance() { // assumes: y = new double[]{11.0, 12.0, 13.0, 14.0, 15.0, 16.0}; OLSMultipleLinearRegression model = new OLSMultipleLinearRegression(); model.newSampleData(y, x); TestUtils.assertEquals(model.calculateYVariance(), 3.5, 0); } /** * Verifies that calculateYVariance and calculateResidualVariance return consistent * values with direct variance computation from Y, residuals, respectively. */ protected void checkVarianceConsistency(OLSMultipleLinearRegression model) { // Check Y variance consistency TestUtils.assertEquals(StatUtils.variance(model.getY().toArray()), model.calculateYVariance(), 0); // Check residual variance consistency double[] residuals = model.calculateResiduals().toArray(); RealMatrix X = model.getX(); TestUtils.assertEquals( StatUtils.variance(model.calculateResiduals().toArray()) * (residuals.length - 1), model.calculateErrorVariance() * (X.getRowDimension() - X.getColumnDimension()), 1E-20); } /** * Verifies that setting X and Y separately has the same effect as newSample(X,Y). */ @Test public void testNewSample2() { double[] y = new double[] {1, 2, 3, 4}; double[][] x = new double[][] { {19, 22, 33}, {20, 30, 40}, {25, 35, 45}, {27, 37, 47} }; OLSMultipleLinearRegression regression = new OLSMultipleLinearRegression(); regression.newSampleData(y, x); RealMatrix combinedX = regression.getX().copy(); RealVector combinedY = regression.getY().copy(); regression.newXSampleData(x); regression.newYSampleData(y); Assert.assertEquals(combinedX, regression.getX()); Assert.assertEquals(combinedY, regression.getY()); // No intercept regression.setNoIntercept(true); regression.newSampleData(y, x); combinedX = regression.getX().copy(); combinedY = regression.getY().copy(); regression.newXSampleData(x); regression.newYSampleData(y); Assert.assertEquals(combinedX, regression.getX()); Assert.assertEquals(combinedY, regression.getY()); } @Test(expected=IllegalArgumentException.class) public void testNewSampleDataYNull() { createRegression().newSampleData(null, new double[][] {}); } @Test(expected=IllegalArgumentException.class) public void testNewSampleDataXNull() { createRegression().newSampleData(new double[] {}, null); } /* * This is a test based on the Wampler1 data set * http://www.itl.nist.gov/div898/strd/lls/data/Wampler1.shtml */ @Test public void testWampler1() { double[] data = new double[]{ 1, 0, 6, 1, 63, 2, 364, 3, 1365, 4, 3906, 5, 9331, 6, 19608, 7, 37449, 8, 66430, 9, 111111, 10, 177156, 11, 271453, 12, 402234, 13, 579195, 14, 813616, 15, 1118481, 16, 1508598, 17, 2000719, 18, 2613660, 19, 3368421, 20}; OLSMultipleLinearRegression model = new OLSMultipleLinearRegression(); final int nvars = 5; final int nobs = 21; double[] tmp = new double[(nvars + 1) * nobs]; int off = 0; int off2 = 0; for (int i = 0; i < nobs; i++) { tmp[off2] = data[off]; tmp[off2 + 1] = data[off + 1]; tmp[off2 + 2] = tmp[off2 + 1] * tmp[off2 + 1]; tmp[off2 + 3] = tmp[off2 + 1] * tmp[off2 + 2]; tmp[off2 + 4] = tmp[off2 + 1] * tmp[off2 + 3]; tmp[off2 + 5] = tmp[off2 + 1] * tmp[off2 + 4]; off2 += (nvars + 1); off += 2; } model.newSampleData(tmp, nobs, nvars); double[] betaHat = model.estimateRegressionParameters(); TestUtils.assertEquals(betaHat, new double[]{1.0, 1.0, 1.0, 1.0, 1.0, 1.0}, 1E-8); double[] se = model.estimateRegressionParametersStandardErrors(); TestUtils.assertEquals(se, new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 0.0}, 1E-8); TestUtils.assertEquals(1.0, model.calculateRSquared(), 1.0e-10); TestUtils.assertEquals(0, model.estimateErrorVariance(), 1.0e-7); TestUtils.assertEquals(0.00, model.calculateResidualSumOfSquares(), 1.0e-6); return; } /* * This is a test based on the Wampler2 data set * http://www.itl.nist.gov/div898/strd/lls/data/Wampler2.shtml */ @Test public void testWampler2() { double[] data = new double[]{ 1.00000, 0, 1.11111, 1, 1.24992, 2, 1.42753, 3, 1.65984, 4, 1.96875, 5, 2.38336, 6, 2.94117, 7, 3.68928, 8, 4.68559, 9, 6.00000, 10, 7.71561, 11, 9.92992, 12, 12.75603, 13, 16.32384, 14, 20.78125, 15, 26.29536, 16, 33.05367, 17, 41.26528, 18, 51.16209, 19, 63.00000, 20}; OLSMultipleLinearRegression model = new OLSMultipleLinearRegression(); final int nvars = 5; final int nobs = 21; double[] tmp = new double[(nvars + 1) * nobs]; int off = 0; int off2 = 0; for (int i = 0; i < nobs; i++) { tmp[off2] = data[off]; tmp[off2 + 1] = data[off + 1]; tmp[off2 + 2] = tmp[off2 + 1] * tmp[off2 + 1]; tmp[off2 + 3] = tmp[off2 + 1] * tmp[off2 + 2]; tmp[off2 + 4] = tmp[off2 + 1] * tmp[off2 + 3]; tmp[off2 + 5] = tmp[off2 + 1] * tmp[off2 + 4]; off2 += (nvars + 1); off += 2; } model.newSampleData(tmp, nobs, nvars); double[] betaHat = model.estimateRegressionParameters(); TestUtils.assertEquals(betaHat, new double[]{ 1.0, 1.0e-1, 1.0e-2, 1.0e-3, 1.0e-4, 1.0e-5}, 1E-8); double[] se = model.estimateRegressionParametersStandardErrors(); TestUtils.assertEquals(se, new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 0.0}, 1E-8); TestUtils.assertEquals(1.0, model.calculateRSquared(), 1.0e-10); TestUtils.assertEquals(0, model.estimateErrorVariance(), 1.0e-7); TestUtils.assertEquals(0.00, model.calculateResidualSumOfSquares(), 1.0e-6); return; } /* * This is a test based on the Wampler3 data set * http://www.itl.nist.gov/div898/strd/lls/data/Wampler3.shtml */ @Test public void testWampler3() { double[] data = new double[]{ 760, 0, -2042, 1, 2111, 2, -1684, 3, 3888, 4, 1858, 5, 11379, 6, 17560, 7, 39287, 8, 64382, 9, 113159, 10, 175108, 11, 273291, 12, 400186, 13, 581243, 14, 811568, 15, 1121004, 16, 1506550, 17, 2002767, 18, 2611612, 19, 3369180, 20}; OLSMultipleLinearRegression model = new OLSMultipleLinearRegression(); final int nvars = 5; final int nobs = 21; double[] tmp = new double[(nvars + 1) * nobs]; int off = 0; int off2 = 0; for (int i = 0; i < nobs; i++) { tmp[off2] = data[off]; tmp[off2 + 1] = data[off + 1]; tmp[off2 + 2] = tmp[off2 + 1] * tmp[off2 + 1]; tmp[off2 + 3] = tmp[off2 + 1] * tmp[off2 + 2]; tmp[off2 + 4] = tmp[off2 + 1] * tmp[off2 + 3]; tmp[off2 + 5] = tmp[off2 + 1] * tmp[off2 + 4]; off2 += (nvars + 1); off += 2; } model.newSampleData(tmp, nobs, nvars); double[] betaHat = model.estimateRegressionParameters(); TestUtils.assertEquals(betaHat, new double[]{ 1.0, 1.0, 1.0, 1.0, 1.0, 1.0}, 1E-8); double[] se = model.estimateRegressionParametersStandardErrors(); TestUtils.assertEquals(se, new double[]{2152.32624678170, 2363.55173469681, 779.343524331583, 101.475507550350, 5.64566512170752, 0.112324854679312}, 1E-8); // TestUtils.assertEquals(.999995559025820, model.calculateRSquared(), 1.0e-10); TestUtils.assertEquals(5570284.53333333, model.estimateErrorVariance(), 1.0e-6); TestUtils.assertEquals(83554268.0000000, model.calculateResidualSumOfSquares(), 1.0e-5); return; } /* * This is a test based on the Wampler4 data set * http://www.itl.nist.gov/div898/strd/lls/data/Wampler4.shtml */ @Test public void testWampler4() { double[] data = new double[]{ 75901, 0, -204794, 1, 204863, 2, -204436, 3, 253665, 4, -200894, 5, 214131, 6, -185192, 7, 221249, 8, -138370, 9, 315911, 10, -27644, 11, 455253, 12, 197434, 13, 783995, 14, 608816, 15, 1370781, 16, 1303798, 17, 2205519, 18, 2408860, 19, 3444321, 20}; OLSMultipleLinearRegression model = new OLSMultipleLinearRegression(); final int nvars = 5; final int nobs = 21; double[] tmp = new double[(nvars + 1) * nobs]; int off = 0; int off2 = 0; for (int i = 0; i < nobs; i++) { tmp[off2] = data[off]; tmp[off2 + 1] = data[off + 1]; tmp[off2 + 2] = tmp[off2 + 1] * tmp[off2 + 1]; tmp[off2 + 3] = tmp[off2 + 1] * tmp[off2 + 2]; tmp[off2 + 4] = tmp[off2 + 1] * tmp[off2 + 3]; tmp[off2 + 5] = tmp[off2 + 1] * tmp[off2 + 4]; off2 += (nvars + 1); off += 2; } model.newSampleData(tmp, nobs, nvars); double[] betaHat = model.estimateRegressionParameters(); TestUtils.assertEquals(betaHat, new double[]{ 1.0, 1.0, 1.0, 1.0, 1.0, 1.0}, 1E-6); double[] se = model.estimateRegressionParametersStandardErrors(); TestUtils.assertEquals(se, new double[]{215232.624678170, 236355.173469681, 77934.3524331583, 10147.5507550350, 564.566512170752, 11.2324854679312}, 1E-8); TestUtils.assertEquals(.957478440825662, model.calculateRSquared(), 1.0e-10); TestUtils.assertEquals(55702845333.3333, model.estimateErrorVariance(), 1.0e-4); TestUtils.assertEquals(835542680000.000, model.calculateResidualSumOfSquares(), 1.0e-3); return; } } ././@LongLink100644 0 0 157 12126630646 10264 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/stat/regression/MillerUpdatingRegressionTest.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/stat/regression/MillerUpdatingRegressio100644 1750 1750 127772 12126627674 32525 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.stat.regression; import org.apache.commons.math3.linear.RealMatrix; import org.apache.commons.math3.stat.correlation.PearsonsCorrelation; import org.apache.commons.math3.TestUtils; import org.apache.commons.math3.util.FastMath; import org.junit.Assert; import org.junit.Test; /** * MillerUpdatingRegression tests. */ public class MillerUpdatingRegressionTest { public MillerUpdatingRegressionTest() { } /* This is the Greene Airline Cost data. * The data can be downloaded from http://www.indiana.edu/~statmath/stat/all/panel/airline.csv */ private final static double[][] airdata = { /*"I",*/new double[]{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6}, /*"T",*/ new double[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}, /*"C",*/ new double[]{1140640, 1215690, 1309570, 1511530, 1676730, 1823740, 2022890, 2314760, 2639160, 3247620, 3787750, 3867750, 3996020, 4282880, 4748320, 569292, 640614, 777655, 999294, 1203970, 1358100, 1501350, 1709270, 2025400, 2548370, 3137740, 3557700, 3717740, 3962370, 4209390, 286298, 309290, 342056, 374595, 450037, 510412, 575347, 669331, 783799, 913883, 1041520, 1125800, 1096070, 1198930, 1170470, 145167, 170192, 247506, 309391, 354338, 373941, 420915, 474017, 532590, 676771, 880438, 1052020, 1193680, 1303390, 1436970, 91361, 95428, 98187, 115967, 138382, 156228, 183169, 210212, 274024, 356915, 432344, 524294, 530924, 581447, 610257, 68978, 74904, 83829, 98148, 118449, 133161, 145062, 170711, 199775, 276797, 381478, 506969, 633388, 804388, 1009500}, /*"Q",*/ new double[]{0.952757, 0.986757, 1.09198, 1.17578, 1.16017, 1.17376, 1.29051, 1.39067, 1.61273, 1.82544, 1.54604, 1.5279, 1.6602, 1.82231, 1.93646, 0.520635, 0.534627, 0.655192, 0.791575, 0.842945, 0.852892, 0.922843, 1, 1.19845, 1.34067, 1.32624, 1.24852, 1.25432, 1.37177, 1.38974, 0.262424, 0.266433, 0.306043, 0.325586, 0.345706, 0.367517, 0.409937, 0.448023, 0.539595, 0.539382, 0.467967, 0.450544, 0.468793, 0.494397, 0.493317, 0.086393, 0.09674, 0.1415, 0.169715, 0.173805, 0.164272, 0.170906, 0.17784, 0.192248, 0.242469, 0.256505, 0.249657, 0.273923, 0.371131, 0.421411, 0.051028, 0.052646, 0.056348, 0.066953, 0.070308, 0.073961, 0.084946, 0.095474, 0.119814, 0.150046, 0.144014, 0.1693, 0.172761, 0.18667, 0.213279, 0.037682, 0.039784, 0.044331, 0.050245, 0.055046, 0.052462, 0.056977, 0.06149, 0.069027, 0.092749, 0.11264, 0.154154, 0.186461, 0.246847, 0.304013}, /*"PF",*/ new double[]{106650, 110307, 110574, 121974, 196606, 265609, 263451, 316411, 384110, 569251, 871636, 997239, 938002, 859572, 823411, 103795, 111477, 118664, 114797, 215322, 281704, 304818, 348609, 374579, 544109, 853356, 1003200, 941977, 856533, 821361, 118788, 123798, 122882, 131274, 222037, 278721, 306564, 356073, 378311, 555267, 850322, 1015610, 954508, 886999, 844079, 114987, 120501, 121908, 127220, 209405, 263148, 316724, 363598, 389436, 547376, 850418, 1011170, 951934, 881323, 831374, 118222, 116223, 115853, 129372, 243266, 277930, 317273, 358794, 397667, 566672, 848393, 1005740, 958231, 872924, 844622, 117112, 119420, 116087, 122997, 194309, 307923, 323595, 363081, 386422, 564867, 874818, 1013170, 930477, 851676, 819476}, /*"LF",*/ new double[]{0.534487, 0.532328, 0.547736, 0.540846, 0.591167, 0.575417, 0.594495, 0.597409, 0.638522, 0.676287, 0.605735, 0.61436, 0.633366, 0.650117, 0.625603, 0.490851, 0.473449, 0.503013, 0.512501, 0.566782, 0.558133, 0.558799, 0.57207, 0.624763, 0.628706, 0.58915, 0.532612, 0.526652, 0.540163, 0.528775, 0.524334, 0.537185, 0.582119, 0.579489, 0.606592, 0.60727, 0.582425, 0.573972, 0.654256, 0.631055, 0.56924, 0.589682, 0.587953, 0.565388, 0.577078, 0.432066, 0.439669, 0.488932, 0.484181, 0.529925, 0.532723, 0.549067, 0.55714, 0.611377, 0.645319, 0.611734, 0.580884, 0.572047, 0.59457, 0.585525, 0.442875, 0.462473, 0.519118, 0.529331, 0.557797, 0.556181, 0.569327, 0.583465, 0.631818, 0.604723, 0.587921, 0.616159, 0.605868, 0.594688, 0.635545, 0.448539, 0.475889, 0.500562, 0.500344, 0.528897, 0.495361, 0.510342, 0.518296, 0.546723, 0.554276, 0.517766, 0.580049, 0.556024, 0.537791, 0.525775} }; /** * Test of hasIntercept method, of class MillerUpdatingRegression. */ @Test public void testHasIntercept() { MillerUpdatingRegression instance = new MillerUpdatingRegression(10, false); if (instance.hasIntercept()) { Assert.fail("Should not have intercept"); } instance = new MillerUpdatingRegression(10, true); if (!instance.hasIntercept()) { Assert.fail("Should have intercept"); } } /** * Test of getN method, of class MillerUpdatingRegression. */ @Test public void testAddObsGetNClear() { MillerUpdatingRegression instance = new MillerUpdatingRegression(3, true); double[][] xAll = new double[airdata[0].length][]; double[] y = new double[airdata[0].length]; for (int i = 0; i < airdata[0].length; i++) { xAll[i] = new double[3]; xAll[i][0] = Math.log(airdata[3][i]); xAll[i][1] = Math.log(airdata[4][i]); xAll[i][2] = airdata[5][i]; y[i] = Math.log(airdata[2][i]); } instance.addObservations(xAll, y); if (instance.getN() != xAll.length) { Assert.fail("Number of observations not correct in bulk addition"); } instance.clear(); for (int i = 0; i < xAll.length; i++) { instance.addObservation(xAll[i], y[i]); } if (instance.getN() != xAll.length) { Assert.fail("Number of observations not correct in drip addition"); } return; } @Test public void testNegativeTestAddObs() { MillerUpdatingRegression instance = new MillerUpdatingRegression(3, true); try { instance.addObservation(new double[]{1.0}, 0.0); Assert.fail("Should throw IllegalArgumentException"); } catch (IllegalArgumentException iae) { } catch (Exception e) { Assert.fail("Should throw IllegalArgumentException"); } try { instance.addObservation(new double[]{1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0}, 0.0); Assert.fail("Should throw IllegalArgumentException"); } catch (IllegalArgumentException iae) { } catch (Exception e) { Assert.fail("Should throw IllegalArgumentException"); } try { instance.addObservation(new double[]{1.0, 1.0, 1.0}, 0.0); } catch (Exception e) { Assert.fail("Should throw IllegalArgumentException"); } //now we try it without an intercept instance = new MillerUpdatingRegression(3, false); try { instance.addObservation(new double[]{1.0}, 0.0); Assert.fail("Should throw IllegalArgumentException [NOINTERCEPT]"); } catch (IllegalArgumentException iae) { } catch (Exception e) { Assert.fail("Should throw IllegalArgumentException [NOINTERCEPT]"); } try { instance.addObservation(new double[]{1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0}, 0.0); Assert.fail("Should throw IllegalArgumentException [NOINTERCEPT]"); } catch (IllegalArgumentException iae) { } catch (Exception e) { Assert.fail("Should throw IllegalArgumentException [NOINTERCEPT]"); } try { instance.addObservation(new double[]{1.0, 1.0, 1.0}, 0.0); } catch (Exception e) { Assert.fail("Should throw IllegalArgumentException [NOINTERCEPT]"); } } @Test public void testNegativeTestAddMultipleObs() { MillerUpdatingRegression instance = new MillerUpdatingRegression(3, true); try { double[][] tst = {{1.0, 1.0, 1.0}, {1.20, 2.0, 2.1}}; double[] y = {1.0}; instance.addObservations(tst, y); Assert.fail("Should throw IllegalArgumentException"); } catch (IllegalArgumentException iae) { } catch (Exception e) { Assert.fail("Should throw IllegalArgumentException"); } try { double[][] tst = {{1.0, 1.0, 1.0}, {1.20, 2.0, 2.1}}; double[] y = {1.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0}; instance.addObservations(tst, y); Assert.fail("Should throw IllegalArgumentException"); } catch (IllegalArgumentException iae) { } catch (Exception e) { Assert.fail("Should throw IllegalArgumentException"); } } /* Results can be found at http://www.indiana.edu/~statmath/stat/all/panel/panel4.html * This test concerns a known data set */ @Test public void testRegressAirlineConstantExternal() { MillerUpdatingRegression instance = new MillerUpdatingRegression(4, false); double[][] x = new double[airdata[0].length][]; double[] y = new double[airdata[0].length]; for (int i = 0; i < airdata[0].length; i++) { x[i] = new double[4]; x[i][0] = 1.0; x[i][1] = Math.log(airdata[3][i]); x[i][2] = Math.log(airdata[4][i]); x[i][3] = airdata[5][i]; y[i] = Math.log(airdata[2][i]); } instance.addObservations(x, y); try { RegressionResults result = instance.regress(); Assert.assertNotNull("The test case is a prototype.", result); TestUtils.assertEquals( new double[]{9.5169, 0.8827, 0.4540, -1.6275}, result.getParameterEstimates(), 1e-4); TestUtils.assertEquals( new double[]{.2292445, .0132545, .0203042, .345302}, result.getStdErrorOfEstimates(), 1.0e-4); TestUtils.assertEquals(0.01552839, result.getMeanSquareError(), 1.0e-8); } catch (Exception e) { Assert.fail("Should not throw exception but does"); } } @Test public void testRegressAirlineConstantInternal() { MillerUpdatingRegression instance = new MillerUpdatingRegression(3, true); double[][] x = new double[airdata[0].length][]; double[] y = new double[airdata[0].length]; for (int i = 0; i < airdata[0].length; i++) { x[i] = new double[3]; x[i][0] = Math.log(airdata[3][i]); x[i][1] = Math.log(airdata[4][i]); x[i][2] = airdata[5][i]; y[i] = Math.log(airdata[2][i]); } instance.addObservations(x, y); try { RegressionResults result = instance.regress(); Assert.assertNotNull("The test case is a prototype.", result); TestUtils.assertEquals( new double[]{9.5169, 0.8827, 0.4540, -1.6275}, result.getParameterEstimates(), 1e-4); TestUtils.assertEquals( new double[]{.2292445, .0132545, .0203042, .345302}, result.getStdErrorOfEstimates(), 1.0e-4); TestUtils.assertEquals(0.9883, result.getRSquared(), 1.0e-4); TestUtils.assertEquals(0.01552839, result.getMeanSquareError(), 1.0e-8); } catch (Exception e) { Assert.fail("Should not throw exception but does"); } } @Test public void testFilippelli() { double[] data = new double[]{ 0.8116, -6.860120914, 0.9072, -4.324130045, 0.9052, -4.358625055, 0.9039, -4.358426747, 0.8053, -6.955852379, 0.8377, -6.661145254, 0.8667, -6.355462942, 0.8809, -6.118102026, 0.7975, -7.115148017, 0.8162, -6.815308569, 0.8515, -6.519993057, 0.8766, -6.204119983, 0.8885, -5.853871964, 0.8859, -6.109523091, 0.8959, -5.79832982, 0.8913, -5.482672118, 0.8959, -5.171791386, 0.8971, -4.851705903, 0.9021, -4.517126416, 0.909, -4.143573228, 0.9139, -3.709075441, 0.9199, -3.499489089, 0.8692, -6.300769497, 0.8872, -5.953504836, 0.89, -5.642065153, 0.891, -5.031376979, 0.8977, -4.680685696, 0.9035, -4.329846955, 0.9078, -3.928486195, 0.7675, -8.56735134, 0.7705, -8.363211311, 0.7713, -8.107682739, 0.7736, -7.823908741, 0.7775, -7.522878745, 0.7841, -7.218819279, 0.7971, -6.920818754, 0.8329, -6.628932138, 0.8641, -6.323946875, 0.8804, -5.991399828, 0.7668, -8.781464495, 0.7633, -8.663140179, 0.7678, -8.473531488, 0.7697, -8.247337057, 0.77, -7.971428747, 0.7749, -7.676129393, 0.7796, -7.352812702, 0.7897, -7.072065318, 0.8131, -6.774174009, 0.8498, -6.478861916, 0.8741, -6.159517513, 0.8061, -6.835647144, 0.846, -6.53165267, 0.8751, -6.224098421, 0.8856, -5.910094889, 0.8919, -5.598599459, 0.8934, -5.290645224, 0.894, -4.974284616, 0.8957, -4.64454848, 0.9047, -4.290560426, 0.9129, -3.885055584, 0.9209, -3.408378962, 0.9219, -3.13200249, 0.7739, -8.726767166, 0.7681, -8.66695597, 0.7665, -8.511026475, 0.7703, -8.165388579, 0.7702, -7.886056648, 0.7761, -7.588043762, 0.7809, -7.283412422, 0.7961, -6.995678626, 0.8253, -6.691862621, 0.8602, -6.392544977, 0.8809, -6.067374056, 0.8301, -6.684029655, 0.8664, -6.378719832, 0.8834, -6.065855188, 0.8898, -5.752272167, 0.8964, -5.132414673, 0.8963, -4.811352704, 0.9074, -4.098269308, 0.9119, -3.66174277, 0.9228, -3.2644011 }; MillerUpdatingRegression model = new MillerUpdatingRegression(10, true); int off = 0; double[] tmp = new double[10]; int nobs = 82; for (int i = 0; i < nobs; i++) { tmp[0] = data[off + 1]; // tmp[1] = tmp[0] * tmp[0]; // tmp[2] = tmp[0] * tmp[1]; //^3 // tmp[3] = tmp[1] * tmp[1]; //^4 // tmp[4] = tmp[2] * tmp[1]; //^5 // tmp[5] = tmp[2] * tmp[2]; //^6 // tmp[6] = tmp[2] * tmp[3]; //^7 // tmp[7] = tmp[3] * tmp[3]; //^8 // tmp[8] = tmp[4] * tmp[3]; //^9 // tmp[9] = tmp[4] * tmp[4]; //^10 tmp[1] = tmp[0] * tmp[0]; tmp[2] = tmp[0] * tmp[1]; tmp[3] = tmp[0] * tmp[2]; tmp[4] = tmp[0] * tmp[3]; tmp[5] = tmp[0] * tmp[4]; tmp[6] = tmp[0] * tmp[5]; tmp[7] = tmp[0] * tmp[6]; tmp[8] = tmp[0] * tmp[7]; tmp[9] = tmp[0] * tmp[8]; model.addObservation(tmp, data[off]); off += 2; } RegressionResults result = model.regress(); double[] betaHat = result.getParameterEstimates(); TestUtils.assertEquals(betaHat, new double[]{ -1467.48961422980, -2772.17959193342, -2316.37108160893, -1127.97394098372, -354.478233703349, -75.1242017393757, -10.8753180355343, -1.06221498588947, -0.670191154593408E-01, -0.246781078275479E-02, -0.402962525080404E-04 }, 1E-5); // // double[] se = result.getStdErrorOfEstimates(); TestUtils.assertEquals(se, new double[]{ 298.084530995537, 559.779865474950, 466.477572127796, 227.204274477751, 71.6478660875927, 15.2897178747400, 2.23691159816033, 0.221624321934227, 0.142363763154724E-01, 0.535617408889821E-03, 0.896632837373868E-05 }, 1E-5); // TestUtils.assertEquals(0.996727416185620, result.getRSquared(), 1.0e-8); TestUtils.assertEquals(0.112091743968020E-04, result.getMeanSquareError(), 1.0e-10); TestUtils.assertEquals(0.795851382172941E-03, result.getErrorSumSquares(), 1.0e-10); } @Test public void testWampler1() { double[] data = new double[]{ 1, 0, 6, 1, 63, 2, 364, 3, 1365, 4, 3906, 5, 9331, 6, 19608, 7, 37449, 8, 66430, 9, 111111, 10, 177156, 11, 271453, 12, 402234, 13, 579195, 14, 813616, 15, 1118481, 16, 1508598, 17, 2000719, 18, 2613660, 19, 3368421, 20}; MillerUpdatingRegression model = new MillerUpdatingRegression(5, true); int off = 0; double[] tmp = new double[5]; int nobs = 21; for (int i = 0; i < nobs; i++) { tmp[0] = data[off + 1]; tmp[1] = tmp[0] * tmp[0]; tmp[2] = tmp[0] * tmp[1]; tmp[3] = tmp[0] * tmp[2]; tmp[4] = tmp[0] * tmp[3]; model.addObservation(tmp, data[off]); off += 2; } RegressionResults result = model.regress(); double[] betaHat = result.getParameterEstimates(); TestUtils.assertEquals(betaHat, new double[]{1.0, 1.0, 1.0, 1.0, 1.0, 1.0}, 1E-8); // // double[] se = result.getStdErrorOfEstimates(); TestUtils.assertEquals(se, new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 0.0}, 1E-8); // TestUtils.assertEquals(1.0, result.getRSquared(), 1.0e-10); TestUtils.assertEquals(0, result.getMeanSquareError(), 1.0e-7); TestUtils.assertEquals(0.00, result.getErrorSumSquares(), 1.0e-6); return; } @Test public void testWampler2() { double[] data = new double[]{ 1.00000, 0, 1.11111, 1, 1.24992, 2, 1.42753, 3, 1.65984, 4, 1.96875, 5, 2.38336, 6, 2.94117, 7, 3.68928, 8, 4.68559, 9, 6.00000, 10, 7.71561, 11, 9.92992, 12, 12.75603, 13, 16.32384, 14, 20.78125, 15, 26.29536, 16, 33.05367, 17, 41.26528, 18, 51.16209, 19, 63.00000, 20}; MillerUpdatingRegression model = new MillerUpdatingRegression(5, true); int off = 0; double[] tmp = new double[5]; int nobs = 21; for (int i = 0; i < nobs; i++) { tmp[0] = data[off + 1]; tmp[1] = tmp[0] * tmp[0]; tmp[2] = tmp[0] * tmp[1]; tmp[3] = tmp[0] * tmp[2]; tmp[4] = tmp[0] * tmp[3]; model.addObservation(tmp, data[off]); off += 2; } RegressionResults result = model.regress(); double[] betaHat = result.getParameterEstimates(); TestUtils.assertEquals(betaHat, new double[]{1.0, 1.0e-1, 1.0e-2, 1.0e-3, 1.0e-4, 1.0e-5}, 1E-8); // // double[] se = result.getStdErrorOfEstimates(); TestUtils.assertEquals(se, new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 0.0}, 1E-8); // TestUtils.assertEquals(1.0, result.getRSquared(), 1.0e-10); TestUtils.assertEquals(0, result.getMeanSquareError(), 1.0e-7); TestUtils.assertEquals(0.00, result.getErrorSumSquares(), 1.0e-6); return; } @Test public void testWampler3() { double[] data = new double[]{ 760, 0, -2042, 1, 2111, 2, -1684, 3, 3888, 4, 1858, 5, 11379, 6, 17560, 7, 39287, 8, 64382, 9, 113159, 10, 175108, 11, 273291, 12, 400186, 13, 581243, 14, 811568, 15, 1121004, 16, 1506550, 17, 2002767, 18, 2611612, 19, 3369180, 20}; MillerUpdatingRegression model = new MillerUpdatingRegression(5, true); int off = 0; double[] tmp = new double[5]; int nobs = 21; for (int i = 0; i < nobs; i++) { tmp[0] = data[off + 1]; tmp[1] = tmp[0] * tmp[0]; tmp[2] = tmp[0] * tmp[1]; tmp[3] = tmp[0] * tmp[2]; tmp[4] = tmp[0] * tmp[3]; model.addObservation(tmp, data[off]); off += 2; } RegressionResults result = model.regress(); double[] betaHat = result.getParameterEstimates(); TestUtils.assertEquals(betaHat, new double[]{1.0, 1.0, 1.0, 1.0, 1.0, 1.0}, 1E-8); // double[] se = result.getStdErrorOfEstimates(); TestUtils.assertEquals(se, new double[]{2152.32624678170, 2363.55173469681, 779.343524331583, 101.475507550350, 5.64566512170752, 0.112324854679312}, 1E-8); // TestUtils.assertEquals(.999995559025820, result.getRSquared(), 1.0e-10); TestUtils.assertEquals(5570284.53333333, result.getMeanSquareError(), 1.0e-7); TestUtils.assertEquals(83554268.0000000, result.getErrorSumSquares(), 1.0e-6); return; } //@Test public void testWampler4() { double[] data = new double[]{ 75901, 0, -204794, 1, 204863, 2, -204436, 3, 253665, 4, -200894, 5, 214131, 6, -185192, 7, 221249, 8, -138370, 9, 315911, 10, -27644, 11, 455253, 12, 197434, 13, 783995, 14, 608816, 15, 1370781, 16, 1303798, 17, 2205519, 18, 2408860, 19, 3444321, 20}; MillerUpdatingRegression model = new MillerUpdatingRegression(5, true); int off = 0; double[] tmp = new double[5]; int nobs = 21; for (int i = 0; i < nobs; i++) { tmp[0] = data[off + 1]; tmp[1] = tmp[0] * tmp[0]; tmp[2] = tmp[0] * tmp[1]; tmp[3] = tmp[0] * tmp[2]; tmp[4] = tmp[0] * tmp[3]; model.addObservation(tmp, data[off]); off += 2; } RegressionResults result = model.regress(); double[] betaHat = result.getParameterEstimates(); TestUtils.assertEquals(betaHat, new double[]{1.0, 1.0, 1.0, 1.0, 1.0, 1.0}, 1E-8); // // double[] se = result.getStdErrorOfEstimates(); TestUtils.assertEquals(se, new double[]{215232.624678170, 236355.173469681, 77934.3524331583, 10147.5507550350, 564.566512170752, 11.2324854679312}, 1E-8); // TestUtils.assertEquals(.957478440825662, result.getRSquared(), 1.0e-10); TestUtils.assertEquals(55702845333.3333, result.getMeanSquareError(), 1.0e-4); TestUtils.assertEquals(835542680000.000, result.getErrorSumSquares(), 1.0e-3); return; } /** * Test Longley dataset against certified values provided by NIST. * Data Source: J. Longley (1967) "An Appraisal of Least Squares * Programs for the Electronic Computer from the Point of View of the User" * Journal of the American Statistical Association, vol. 62. September, * pp. 819-841. * * Certified values (and data) are from NIST: * http://www.itl.nist.gov/div898/strd/lls/data/LINKS/DATA/Longley.dat */ @Test public void testLongly() { // Y values are first, then independent vars // Each row is one observation double[] design = new double[]{ 60323, 83.0, 234289, 2356, 1590, 107608, 1947, 61122, 88.5, 259426, 2325, 1456, 108632, 1948, 60171, 88.2, 258054, 3682, 1616, 109773, 1949, 61187, 89.5, 284599, 3351, 1650, 110929, 1950, 63221, 96.2, 328975, 2099, 3099, 112075, 1951, 63639, 98.1, 346999, 1932, 3594, 113270, 1952, 64989, 99.0, 365385, 1870, 3547, 115094, 1953, 63761, 100.0, 363112, 3578, 3350, 116219, 1954, 66019, 101.2, 397469, 2904, 3048, 117388, 1955, 67857, 104.6, 419180, 2822, 2857, 118734, 1956, 68169, 108.4, 442769, 2936, 2798, 120445, 1957, 66513, 110.8, 444546, 4681, 2637, 121950, 1958, 68655, 112.6, 482704, 3813, 2552, 123366, 1959, 69564, 114.2, 502601, 3931, 2514, 125368, 1960, 69331, 115.7, 518173, 4806, 2572, 127852, 1961, 70551, 116.9, 554894, 4007, 2827, 130081, 1962 }; final int nobs = 16; final int nvars = 6; // Estimate the model MillerUpdatingRegression model = new MillerUpdatingRegression(6, true); int off = 0; double[] tmp = new double[6]; for (int i = 0; i < nobs; i++) { System.arraycopy(design, off + 1, tmp, 0, nvars); model.addObservation(tmp, design[off]); off += nvars + 1; } // Check expected beta values from NIST RegressionResults result = model.regress(); double[] betaHat = result.getParameterEstimates(); TestUtils.assertEquals(betaHat, new double[]{-3482258.63459582, 15.0618722713733, -0.358191792925910E-01, -2.02022980381683, -1.03322686717359, -0.511041056535807E-01, 1829.15146461355}, 1E-8); // // Check standard errors from NIST double[] errors = result.getStdErrorOfEstimates(); TestUtils.assertEquals(new double[]{890420.383607373, 84.9149257747669, 0.334910077722432E-01, 0.488399681651699, 0.214274163161675, 0.226073200069370, 455.478499142212}, errors, 1E-6); // // Check R-Square statistics against R TestUtils.assertEquals(0.995479004577296, result.getRSquared(), 1E-12); TestUtils.assertEquals(0.992465007628826, result.getAdjustedRSquared(), 1E-12); // // // // Estimate model without intercept model = new MillerUpdatingRegression(6, false); off = 0; for (int i = 0; i < nobs; i++) { System.arraycopy(design, off + 1, tmp, 0, nvars); model.addObservation(tmp, design[off]); off += nvars + 1; } // Check expected beta values from R result = model.regress(); betaHat = result.getParameterEstimates(); TestUtils.assertEquals(betaHat, new double[]{-52.99357013868291, 0.07107319907358, -0.42346585566399, -0.57256866841929, -0.41420358884978, 48.41786562001326}, 1E-11); // // Check standard errors from R errors = result.getStdErrorOfEstimates(); TestUtils.assertEquals(new double[]{129.54486693117232, 0.03016640003786, 0.41773654056612, 0.27899087467676, 0.32128496193363, 17.68948737819961}, errors, 1E-11); // // // Check R-Square statistics against R TestUtils.assertEquals(0.9999670130706, result.getRSquared(), 1E-12); TestUtils.assertEquals(0.999947220913, result.getAdjustedRSquared(), 1E-12); } // @Test // public void testRegressReorder() { // // System.out.println("testRegressReorder"); // MillerUpdatingRegression instance = new MillerUpdatingRegression(4, false); // double[][] x = new double[airdata[0].length][]; // double[] y = new double[airdata[0].length]; // for (int i = 0; i < airdata[0].length; i++) { // x[i] = new double[4]; // x[i][0] = 1.0; // x[i][1] = Math.log(airdata[3][i]); // x[i][2] = Math.log(airdata[4][i]); // x[i][3] = airdata[5][i]; // y[i] = Math.log(airdata[2][i]); // } // // instance.addObservations(x, y); // RegressionResults result = instance.regress(); // if (result == null) { // Assert.fail("Null result...."); // } // // instance.reorderRegressors(new int[]{3, 2}, 0); // RegressionResults resultInverse = instance.regress(); // // double[] beta = result.getParameterEstimates(); // double[] betar = resultInverse.getParameterEstimates(); // if (Math.abs(beta[0] - betar[0]) > 1.0e-14) { // Assert.fail("Parameters not correct after reorder (0,3)"); // } // if (Math.abs(beta[1] - betar[1]) > 1.0e-14) { // Assert.fail("Parameters not correct after reorder (1,2)"); // } // if (Math.abs(beta[2] - betar[2]) > 1.0e-14) { // Assert.fail("Parameters not correct after reorder (2,1)"); // } // if (Math.abs(beta[3] - betar[3]) > 1.0e-14) { // Assert.fail("Parameters not correct after reorder (3,0)"); // } // } @Test public void testOneRedundantColumn() { MillerUpdatingRegression instance = new MillerUpdatingRegression(4, false); MillerUpdatingRegression instance2 = new MillerUpdatingRegression(5, false); double[][] x = new double[airdata[0].length][]; double[][] x2 = new double[airdata[0].length][]; double[] y = new double[airdata[0].length]; for (int i = 0; i < airdata[0].length; i++) { x[i] = new double[4]; x2[i] = new double[5]; x[i][0] = 1.0; x[i][1] = Math.log(airdata[3][i]); x[i][2] = Math.log(airdata[4][i]); x[i][3] = airdata[5][i]; x2[i][0] = x[i][0]; x2[i][1] = x[i][1]; x2[i][2] = x[i][2]; x2[i][3] = x[i][3]; x2[i][4] = x[i][3]; y[i] = Math.log(airdata[2][i]); } instance.addObservations(x, y); RegressionResults result = instance.regress(); Assert.assertNotNull("Could not estimate initial regression", result); instance2.addObservations(x2, y); RegressionResults resultRedundant = instance2.regress(); Assert.assertNotNull("Could not estimate redundant regression", resultRedundant); double[] beta = result.getParameterEstimates(); double[] betar = resultRedundant.getParameterEstimates(); double[] se = result.getStdErrorOfEstimates(); double[] ser = resultRedundant.getStdErrorOfEstimates(); for (int i = 0; i < beta.length; i++) { if (Math.abs(beta[i] - betar[i]) > 1.0e-8) { Assert.fail("Parameters not correctly estimated"); } if (Math.abs(se[i] - ser[i]) > 1.0e-8) { Assert.fail("Standard errors not correctly estimated"); } for (int j = 0; j < i; j++) { if (Math.abs(result.getCovarianceOfParameters(i, j) - resultRedundant.getCovarianceOfParameters(i, j)) > 1.0e-8) { Assert.fail("Variance Covariance not correct"); } } } TestUtils.assertEquals(result.getAdjustedRSquared(), resultRedundant.getAdjustedRSquared(), 1.0e-8); TestUtils.assertEquals(result.getErrorSumSquares(), resultRedundant.getErrorSumSquares(), 1.0e-8); TestUtils.assertEquals(result.getMeanSquareError(), resultRedundant.getMeanSquareError(), 1.0e-8); TestUtils.assertEquals(result.getRSquared(), resultRedundant.getRSquared(), 1.0e-8); return; } @Test public void testThreeRedundantColumn() { MillerUpdatingRegression instance = new MillerUpdatingRegression(4, false); MillerUpdatingRegression instance2 = new MillerUpdatingRegression(7, false); double[][] x = new double[airdata[0].length][]; double[][] x2 = new double[airdata[0].length][]; double[] y = new double[airdata[0].length]; for (int i = 0; i < airdata[0].length; i++) { x[i] = new double[4]; x2[i] = new double[7]; x[i][0] = 1.0; x[i][1] = Math.log(airdata[3][i]); x[i][2] = Math.log(airdata[4][i]); x[i][3] = airdata[5][i]; x2[i][0] = x[i][0]; x2[i][1] = x[i][0]; x2[i][2] = x[i][1]; x2[i][3] = x[i][2]; x2[i][4] = x[i][1]; x2[i][5] = x[i][3]; x2[i][6] = x[i][2]; y[i] = Math.log(airdata[2][i]); } instance.addObservations(x, y); RegressionResults result = instance.regress(); Assert.assertNotNull("Could not estimate initial regression", result); instance2.addObservations(x2, y); RegressionResults resultRedundant = instance2.regress(); Assert.assertNotNull("Could not estimate redundant regression", resultRedundant); double[] beta = result.getParameterEstimates(); double[] betar = resultRedundant.getParameterEstimates(); double[] se = result.getStdErrorOfEstimates(); double[] ser = resultRedundant.getStdErrorOfEstimates(); if (Math.abs(beta[0] - betar[0]) > 1.0e-8) { Assert.fail("Parameters not correct after reorder (0,3)"); } if (Math.abs(beta[1] - betar[2]) > 1.0e-8) { Assert.fail("Parameters not correct after reorder (1,2)"); } if (Math.abs(beta[2] - betar[3]) > 1.0e-8) { Assert.fail("Parameters not correct after reorder (2,1)"); } if (Math.abs(beta[3] - betar[5]) > 1.0e-8) { Assert.fail("Parameters not correct after reorder (3,0)"); } if (Math.abs(se[0] - ser[0]) > 1.0e-8) { Assert.fail("Se not correct after reorder (0,3)"); } if (Math.abs(se[1] - ser[2]) > 1.0e-8) { Assert.fail("Se not correct after reorder (1,2)"); } if (Math.abs(se[2] - ser[3]) > 1.0e-8) { Assert.fail("Se not correct after reorder (2,1)"); } if (Math.abs(se[3] - ser[5]) > 1.0e-8) { Assert.fail("Se not correct after reorder (3,0)"); } if (Math.abs(result.getCovarianceOfParameters(0, 0) - resultRedundant.getCovarianceOfParameters(0, 0)) > 1.0e-8) { Assert.fail("VCV not correct after reorder (0,0)"); } if (Math.abs(result.getCovarianceOfParameters(0, 1) - resultRedundant.getCovarianceOfParameters(0, 2)) > 1.0e-8) { Assert.fail("VCV not correct after reorder (0,1)<->(0,2)"); } if (Math.abs(result.getCovarianceOfParameters(0, 2) - resultRedundant.getCovarianceOfParameters(0, 3)) > 1.0e-8) { Assert.fail("VCV not correct after reorder (0,2)<->(0,1)"); } if (Math.abs(result.getCovarianceOfParameters(0, 3) - resultRedundant.getCovarianceOfParameters(0, 5)) > 1.0e-8) { Assert.fail("VCV not correct after reorder (0,3)<->(0,3)"); } if (Math.abs(result.getCovarianceOfParameters(1, 0) - resultRedundant.getCovarianceOfParameters(2, 0)) > 1.0e-8) { Assert.fail("VCV not correct after reorder (1,0)<->(2,0)"); } if (Math.abs(result.getCovarianceOfParameters(1, 1) - resultRedundant.getCovarianceOfParameters(2, 2)) > 1.0e-8) { Assert.fail("VCV not correct (1,1)<->(2,1)"); } if (Math.abs(result.getCovarianceOfParameters(1, 2) - resultRedundant.getCovarianceOfParameters(2, 3)) > 1.0e-8) { Assert.fail("VCV not correct (1,2)<->(2,2)"); } if (Math.abs(result.getCovarianceOfParameters(2, 0) - resultRedundant.getCovarianceOfParameters(3, 0)) > 1.0e-8) { Assert.fail("VCV not correct (2,0)<->(1,0)"); } if (Math.abs(result.getCovarianceOfParameters(2, 1) - resultRedundant.getCovarianceOfParameters(3, 2)) > 1.0e-8) { Assert.fail("VCV not correct (2,1)<->(1,2)"); } if (Math.abs(result.getCovarianceOfParameters(3, 3) - resultRedundant.getCovarianceOfParameters(5, 5)) > 1.0e-8) { Assert.fail("VCV not correct (3,3)<->(3,2)"); } TestUtils.assertEquals(result.getAdjustedRSquared(), resultRedundant.getAdjustedRSquared(), 1.0e-8); TestUtils.assertEquals(result.getErrorSumSquares(), resultRedundant.getErrorSumSquares(), 1.0e-8); TestUtils.assertEquals(result.getMeanSquareError(), resultRedundant.getMeanSquareError(), 1.0e-8); TestUtils.assertEquals(result.getRSquared(), resultRedundant.getRSquared(), 1.0e-8); return; } @Test public void testPCorr() { MillerUpdatingRegression instance = new MillerUpdatingRegression(4, false); double[][] x = new double[airdata[0].length][]; double[] y = new double[airdata[0].length]; double[] cp = new double[10]; double[] yxcorr = new double[4]; double[] diag = new double[4]; double sumysq = 0.0; int off = 0; for (int i = 0; i < airdata[0].length; i++) { x[i] = new double[4]; x[i][0] = 1.0; x[i][1] = Math.log(airdata[3][i]); x[i][2] = Math.log(airdata[4][i]); x[i][3] = airdata[5][i]; y[i] = Math.log(airdata[2][i]); off = 0; for (int j = 0; j < 4; j++) { double tmp = x[i][j]; for (int k = 0; k <= j; k++, off++) { cp[off] += tmp * x[i][k]; } yxcorr[j] += tmp * y[i]; } sumysq += y[i] * y[i]; } PearsonsCorrelation pearson = new PearsonsCorrelation(x); RealMatrix corr = pearson.getCorrelationMatrix(); off = 0; for (int i = 0; i < 4; i++, off += (i + 1)) { diag[i] = FastMath.sqrt(cp[off]); } instance.addObservations(x, y); double[] pc = instance.getPartialCorrelations(0); int idx = 0; off = 0; int off2 = 6; for (int i = 0; i < 4; i++) { for (int j = 0; j < i; j++) { if (Math.abs(pc[idx] - cp[off] / (diag[i] * diag[j])) > 1.0e-8) { Assert.fail("Failed cross products... i = " + i + " j = " + j); } ++idx; ++off; } ++off; if (Math.abs(pc[i+off2] - yxcorr[ i] / (FastMath.sqrt(sumysq) * diag[i])) > 1.0e-8) { Assert.fail("Assert.failed cross product i = " + i + " y"); } } double[] pc2 = instance.getPartialCorrelations(1); idx = 0; for (int i = 1; i < 4; i++) { for (int j = 1; j < i; j++) { if (Math.abs(pc2[idx] - corr.getEntry(j, i)) > 1.0e-8) { Assert.fail("Failed cross products... i = " + i + " j = " + j); } ++idx; } } double[] pc3 = instance.getPartialCorrelations(2); if (pc3 == null) { Assert.fail("Should not be null"); } return; } @Test public void testHdiag() { MillerUpdatingRegression instance = new MillerUpdatingRegression(4, false); double[][] x = new double[airdata[0].length][]; double[] y = new double[airdata[0].length]; for (int i = 0; i < airdata[0].length; i++) { x[i] = new double[4]; x[i][0] = 1.0; x[i][1] = Math.log(airdata[3][i]); x[i][2] = Math.log(airdata[4][i]); x[i][3] = airdata[5][i]; y[i] = Math.log(airdata[2][i]); } instance.addObservations(x, y); OLSMultipleLinearRegression ols = new OLSMultipleLinearRegression(); ols.setNoIntercept(true); ols.newSampleData(y, x); RealMatrix rm = ols.calculateHat(); for (int i = 0; i < x.length; i++) { TestUtils.assertEquals(instance.getDiagonalOfHatMatrix(x[i]), rm.getEntry(i, i), 1.0e-8); } return; } @Test public void testHdiagConstant() { MillerUpdatingRegression instance = new MillerUpdatingRegression(3, true); double[][] x = new double[airdata[0].length][]; double[] y = new double[airdata[0].length]; for (int i = 0; i < airdata[0].length; i++) { x[i] = new double[3]; x[i][0] = Math.log(airdata[3][i]); x[i][1] = Math.log(airdata[4][i]); x[i][2] = airdata[5][i]; y[i] = Math.log(airdata[2][i]); } instance.addObservations(x, y); OLSMultipleLinearRegression ols = new OLSMultipleLinearRegression(); ols.setNoIntercept(false); ols.newSampleData(y, x); RealMatrix rm = ols.calculateHat(); for (int i = 0; i < x.length; i++) { TestUtils.assertEquals(instance.getDiagonalOfHatMatrix(x[i]), rm.getEntry(i, i), 1.0e-8); } return; } @Test public void testSubsetRegression() { MillerUpdatingRegression instance = new MillerUpdatingRegression(3, true); MillerUpdatingRegression redRegression = new MillerUpdatingRegression(2, true); double[][] x = new double[airdata[0].length][]; double[][] xReduced = new double[airdata[0].length][]; double[] y = new double[airdata[0].length]; for (int i = 0; i < airdata[0].length; i++) { x[i] = new double[3]; x[i][0] = Math.log(airdata[3][i]); x[i][1] = Math.log(airdata[4][i]); x[i][2] = airdata[5][i]; xReduced[i] = new double[2]; xReduced[i][0] = Math.log(airdata[3][i]); xReduced[i][1] = Math.log(airdata[4][i]); y[i] = Math.log(airdata[2][i]); } instance.addObservations(x, y); redRegression.addObservations(xReduced, y); RegressionResults resultsInstance = instance.regress( new int[]{0,1,2} ); RegressionResults resultsReduced = redRegression.regress(); TestUtils.assertEquals(resultsInstance.getParameterEstimates(), resultsReduced.getParameterEstimates(), 1.0e-12); TestUtils.assertEquals(resultsInstance.getStdErrorOfEstimates(), resultsReduced.getStdErrorOfEstimates(), 1.0e-12); } } ././@LongLink100644 0 0 147 12126630646 10263 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/stat/regression/SimpleRegressionTest.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/stat/regression/SimpleRegressionTest.ja100644 1750 1750 63464 12126627674 32422 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.stat.regression; import java.util.Random; import org.apache.commons.math3.exception.MathIllegalArgumentException; import org.apache.commons.math3.exception.OutOfRangeException; import org.junit.Assert; import org.junit.Test; /** * Test cases for the TestStatistic class. * * @version $Id: SimpleRegressionTest.java 1389150 2012-09-23 21:42:51Z psteitz $ */ public final class SimpleRegressionTest { /* * NIST "Norris" refernce data set from * http://www.itl.nist.gov/div898/strd/lls/data/LINKS/DATA/Norris.dat * Strangely, order is {y,x} */ private double[][] data = { { 0.1, 0.2 }, {338.8, 337.4 }, {118.1, 118.2 }, {888.0, 884.6 }, {9.2, 10.1 }, {228.1, 226.5 }, {668.5, 666.3 }, {998.5, 996.3 }, {449.1, 448.6 }, {778.9, 777.0 }, {559.2, 558.2 }, {0.3, 0.4 }, {0.1, 0.6 }, {778.1, 775.5 }, {668.8, 666.9 }, {339.3, 338.0 }, {448.9, 447.5 }, {10.8, 11.6 }, {557.7, 556.0 }, {228.3, 228.1 }, {998.0, 995.8 }, {888.8, 887.6 }, {119.6, 120.2 }, {0.3, 0.3 }, {0.6, 0.3 }, {557.6, 556.8 }, {339.3, 339.1 }, {888.0, 887.2 }, {998.5, 999.0 }, {778.9, 779.0 }, {10.2, 11.1 }, {117.6, 118.3 }, {228.9, 229.2 }, {668.4, 669.1 }, {449.2, 448.9 }, {0.2, 0.5 } }; /* * Correlation example from * http://www.xycoon.com/correlation.htm */ private double[][] corrData = { { 101.0, 99.2 }, {100.1, 99.0 }, {100.0, 100.0 }, {90.6, 111.6 }, {86.5, 122.2 }, {89.7, 117.6 }, {90.6, 121.1 }, {82.8, 136.0 }, {70.1, 154.2 }, {65.4, 153.6 }, {61.3, 158.5 }, {62.5, 140.6 }, {63.6, 136.2 }, {52.6, 168.0 }, {59.7, 154.3 }, {59.5, 149.0 }, {61.3, 165.5 } }; /* * From Moore and Mcabe, "Introduction to the Practice of Statistics" * Example 10.3 */ private double[][] infData = { { 15.6, 5.2 }, {26.8, 6.1 }, {37.8, 8.7 }, {36.4, 8.5 }, {35.5, 8.8 }, {18.6, 4.9 }, {15.3, 4.5 }, {7.9, 2.5 }, {0.0, 1.1 } }; /* * Points to remove in the remove tests */ private double[][] removeSingle = {infData[1]}; private double[][] removeMultiple = { infData[1], infData[2] }; private double removeX = infData[0][0]; private double removeY = infData[0][1]; /* * Data with bad linear fit */ private double[][] infData2 = { { 1, 1 }, {2, 0 }, {3, 5 }, {4, 2 }, {5, -1 }, {6, 12 } }; /* * Data from NIST NOINT1 */ private double[][] noint1 = { {130.0,60.0}, {131.0,61.0}, {132.0,62.0}, {133.0,63.0}, {134.0,64.0}, {135.0,65.0}, {136.0,66.0}, {137.0,67.0}, {138.0,68.0}, {139.0,69.0}, {140.0,70.0} }; /* * Data from NIST NOINT2 * */ private double[][] noint2 = { {3.0,4}, {4,5}, {4,6} }; @Test public void testRegressIfaceMethod(){ final SimpleRegression regression = new SimpleRegression(true); final UpdatingMultipleLinearRegression iface = regression; final SimpleRegression regressionNoint = new SimpleRegression( false ); final SimpleRegression regressionIntOnly= new SimpleRegression( false ); for (int i = 0; i < data.length; i++) { iface.addObservation( new double[]{data[i][1]}, data[i][0]); regressionNoint.addData(data[i][1], data[i][0]); regressionIntOnly.addData(1.0, data[i][0]); } //should not be null final RegressionResults fullReg = iface.regress( ); Assert.assertNotNull(fullReg); Assert.assertEquals("intercept", regression.getIntercept(), fullReg.getParameterEstimate(0), 1.0e-16); Assert.assertEquals("intercept std err",regression.getInterceptStdErr(), fullReg.getStdErrorOfEstimate(0),1.0E-16); Assert.assertEquals("slope", regression.getSlope(), fullReg.getParameterEstimate(1), 1.0e-16); Assert.assertEquals("slope std err",regression.getSlopeStdErr(), fullReg.getStdErrorOfEstimate(1),1.0E-16); Assert.assertEquals("number of observations",regression.getN(), fullReg.getN()); Assert.assertEquals("r-square",regression.getRSquare(), fullReg.getRSquared(), 1.0E-16); Assert.assertEquals("SSR", regression.getRegressionSumSquares(), fullReg.getRegressionSumSquares() ,1.0E-16); Assert.assertEquals("MSE", regression.getMeanSquareError(), fullReg.getMeanSquareError() ,1.0E-16); Assert.assertEquals("SSE", regression.getSumSquaredErrors(), fullReg.getErrorSumSquares() ,1.0E-16); final RegressionResults noInt = iface.regress( new int[]{1} ); Assert.assertNotNull(noInt); Assert.assertEquals("slope", regressionNoint.getSlope(), noInt.getParameterEstimate(0), 1.0e-12); Assert.assertEquals("slope std err",regressionNoint.getSlopeStdErr(), noInt.getStdErrorOfEstimate(0),1.0E-16); Assert.assertEquals("number of observations",regressionNoint.getN(), noInt.getN()); Assert.assertEquals("r-square",regressionNoint.getRSquare(), noInt.getRSquared(), 1.0E-16); Assert.assertEquals("SSR", regressionNoint.getRegressionSumSquares(), noInt.getRegressionSumSquares() ,1.0E-8); Assert.assertEquals("MSE", regressionNoint.getMeanSquareError(), noInt.getMeanSquareError() ,1.0E-16); Assert.assertEquals("SSE", regressionNoint.getSumSquaredErrors(), noInt.getErrorSumSquares() ,1.0E-16); final RegressionResults onlyInt = iface.regress( new int[]{0} ); Assert.assertNotNull(onlyInt); Assert.assertEquals("slope", regressionIntOnly.getSlope(), onlyInt.getParameterEstimate(0), 1.0e-12); Assert.assertEquals("slope std err",regressionIntOnly.getSlopeStdErr(), onlyInt.getStdErrorOfEstimate(0),1.0E-12); Assert.assertEquals("number of observations",regressionIntOnly.getN(), onlyInt.getN()); Assert.assertEquals("r-square",regressionIntOnly.getRSquare(), onlyInt.getRSquared(), 1.0E-14); Assert.assertEquals("SSE", regressionIntOnly.getSumSquaredErrors(), onlyInt.getErrorSumSquares() ,1.0E-8); Assert.assertEquals("SSR", regressionIntOnly.getRegressionSumSquares(), onlyInt.getRegressionSumSquares() ,1.0E-8); Assert.assertEquals("MSE", regressionIntOnly.getMeanSquareError(), onlyInt.getMeanSquareError() ,1.0E-8); } /** * Verify that regress generates exceptions as advertised for bad model specifications. */ @Test public void testRegressExceptions() { // No intercept final SimpleRegression noIntRegression = new SimpleRegression(false); noIntRegression.addData(noint2[0][1], noint2[0][0]); noIntRegression.addData(noint2[1][1], noint2[1][0]); noIntRegression.addData(noint2[2][1], noint2[2][0]); try { // null array noIntRegression.regress(null); Assert.fail("Expecting MathIllegalArgumentException for null array"); } catch (MathIllegalArgumentException ex) { // Expected } try { // empty array noIntRegression.regress(new int[] {}); Assert.fail("Expecting MathIllegalArgumentException for empty array"); } catch (MathIllegalArgumentException ex) { // Expected } try { // more than 1 regressor noIntRegression.regress(new int[] {0, 1}); Assert.fail("Expecting ModelSpecificationException - too many regressors"); } catch (ModelSpecificationException ex) { // Expected } try { // invalid regressor noIntRegression.regress(new int[] {1}); Assert.fail("Expecting OutOfRangeException - invalid regression"); } catch (OutOfRangeException ex) { // Expected } // With intercept final SimpleRegression regression = new SimpleRegression(true); regression.addData(noint2[0][1], noint2[0][0]); regression.addData(noint2[1][1], noint2[1][0]); regression.addData(noint2[2][1], noint2[2][0]); try { // null array regression.regress(null); Assert.fail("Expecting MathIllegalArgumentException for null array"); } catch (MathIllegalArgumentException ex) { // Expected } try { // empty array regression.regress(new int[] {}); Assert.fail("Expecting MathIllegalArgumentException for empty array"); } catch (MathIllegalArgumentException ex) { // Expected } try { // more than 2 regressors regression.regress(new int[] {0, 1, 2}); Assert.fail("Expecting ModelSpecificationException - too many regressors"); } catch (ModelSpecificationException ex) { // Expected } try { // wrong order regression.regress(new int[] {1,0}); Assert.fail("Expecting ModelSpecificationException - invalid regression"); } catch (ModelSpecificationException ex) { // Expected } try { // out of range regression.regress(new int[] {3,4}); Assert.fail("Expecting OutOfRangeException"); } catch (OutOfRangeException ex) { // Expected } try { // out of range regression.regress(new int[] {0,2}); Assert.fail("Expecting OutOfRangeException"); } catch (OutOfRangeException ex) { // Expected } try { // out of range regression.regress(new int[] {2}); Assert.fail("Expecting OutOfRangeException"); } catch (OutOfRangeException ex) { // Expected } } @Test public void testNoInterceot_noint2(){ SimpleRegression regression = new SimpleRegression(false); regression.addData(noint2[0][1], noint2[0][0]); regression.addData(noint2[1][1], noint2[1][0]); regression.addData(noint2[2][1], noint2[2][0]); Assert.assertEquals("intercept", 0, regression.getIntercept(), 0); Assert.assertEquals("slope", 0.727272727272727, regression.getSlope(), 10E-12); Assert.assertEquals("slope std err", 0.420827318078432E-01, regression.getSlopeStdErr(),10E-12); Assert.assertEquals("number of observations", 3, regression.getN()); Assert.assertEquals("r-square", 0.993348115299335, regression.getRSquare(), 10E-12); Assert.assertEquals("SSR", 40.7272727272727, regression.getRegressionSumSquares(), 10E-9); Assert.assertEquals("MSE", 0.136363636363636, regression.getMeanSquareError(), 10E-10); Assert.assertEquals("SSE", 0.272727272727273, regression.getSumSquaredErrors(),10E-9); } @Test public void testNoIntercept_noint1(){ SimpleRegression regression = new SimpleRegression(false); for (int i = 0; i < noint1.length; i++) { regression.addData(noint1[i][1], noint1[i][0]); } Assert.assertEquals("intercept", 0, regression.getIntercept(), 0); Assert.assertEquals("slope", 2.07438016528926, regression.getSlope(), 10E-12); Assert.assertEquals("slope std err", 0.165289256198347E-01, regression.getSlopeStdErr(),10E-12); Assert.assertEquals("number of observations", 11, regression.getN()); Assert.assertEquals("r-square", 0.999365492298663, regression.getRSquare(), 10E-12); Assert.assertEquals("SSR", 200457.727272727, regression.getRegressionSumSquares(), 10E-9); Assert.assertEquals("MSE", 12.7272727272727, regression.getMeanSquareError(), 10E-10); Assert.assertEquals("SSE", 127.272727272727, regression.getSumSquaredErrors(),10E-9); } @Test public void testNorris() { SimpleRegression regression = new SimpleRegression(); for (int i = 0; i < data.length; i++) { regression.addData(data[i][1], data[i][0]); } // Tests against certified values from // http://www.itl.nist.gov/div898/strd/lls/data/LINKS/DATA/Norris.dat Assert.assertEquals("slope", 1.00211681802045, regression.getSlope(), 10E-12); Assert.assertEquals("slope std err", 0.429796848199937E-03, regression.getSlopeStdErr(),10E-12); Assert.assertEquals("number of observations", 36, regression.getN()); Assert.assertEquals( "intercept", -0.262323073774029, regression.getIntercept(),10E-12); Assert.assertEquals("std err intercept", 0.232818234301152, regression.getInterceptStdErr(),10E-12); Assert.assertEquals("r-square", 0.999993745883712, regression.getRSquare(), 10E-12); Assert.assertEquals("SSR", 4255954.13232369, regression.getRegressionSumSquares(), 10E-9); Assert.assertEquals("MSE", 0.782864662630069, regression.getMeanSquareError(), 10E-10); Assert.assertEquals("SSE", 26.6173985294224, regression.getSumSquaredErrors(),10E-9); // ------------ End certified data tests Assert.assertEquals( "predict(0)", -0.262323073774029, regression.predict(0), 10E-12); Assert.assertEquals("predict(1)", 1.00211681802045 - 0.262323073774029, regression.predict(1), 10E-12); } @Test public void testCorr() { SimpleRegression regression = new SimpleRegression(); regression.addData(corrData); Assert.assertEquals("number of observations", 17, regression.getN()); Assert.assertEquals("r-square", .896123, regression.getRSquare(), 10E-6); Assert.assertEquals("r", -0.94663767742, regression.getR(), 1E-10); } @Test public void testNaNs() { SimpleRegression regression = new SimpleRegression(); Assert.assertTrue("intercept not NaN", Double.isNaN(regression.getIntercept())); Assert.assertTrue("slope not NaN", Double.isNaN(regression.getSlope())); Assert.assertTrue("slope std err not NaN", Double.isNaN(regression.getSlopeStdErr())); Assert.assertTrue("intercept std err not NaN", Double.isNaN(regression.getInterceptStdErr())); Assert.assertTrue("MSE not NaN", Double.isNaN(regression.getMeanSquareError())); Assert.assertTrue("e not NaN", Double.isNaN(regression.getR())); Assert.assertTrue("r-square not NaN", Double.isNaN(regression.getRSquare())); Assert.assertTrue( "RSS not NaN", Double.isNaN(regression.getRegressionSumSquares())); Assert.assertTrue("SSE not NaN",Double.isNaN(regression.getSumSquaredErrors())); Assert.assertTrue("SSTO not NaN", Double.isNaN(regression.getTotalSumSquares())); Assert.assertTrue("predict not NaN", Double.isNaN(regression.predict(0))); regression.addData(1, 2); regression.addData(1, 3); // No x variation, so these should still blow... Assert.assertTrue("intercept not NaN", Double.isNaN(regression.getIntercept())); Assert.assertTrue("slope not NaN", Double.isNaN(regression.getSlope())); Assert.assertTrue("slope std err not NaN", Double.isNaN(regression.getSlopeStdErr())); Assert.assertTrue("intercept std err not NaN", Double.isNaN(regression.getInterceptStdErr())); Assert.assertTrue("MSE not NaN", Double.isNaN(regression.getMeanSquareError())); Assert.assertTrue("e not NaN", Double.isNaN(regression.getR())); Assert.assertTrue("r-square not NaN", Double.isNaN(regression.getRSquare())); Assert.assertTrue("RSS not NaN", Double.isNaN(regression.getRegressionSumSquares())); Assert.assertTrue("SSE not NaN", Double.isNaN(regression.getSumSquaredErrors())); Assert.assertTrue("predict not NaN", Double.isNaN(regression.predict(0))); // but SSTO should be OK Assert.assertTrue("SSTO NaN", !Double.isNaN(regression.getTotalSumSquares())); regression = new SimpleRegression(); regression.addData(1, 2); regression.addData(3, 3); // All should be OK except MSE, s(b0), s(b1) which need one more df Assert.assertTrue("interceptNaN", !Double.isNaN(regression.getIntercept())); Assert.assertTrue("slope NaN", !Double.isNaN(regression.getSlope())); Assert.assertTrue("slope std err not NaN", Double.isNaN(regression.getSlopeStdErr())); Assert.assertTrue("intercept std err not NaN", Double.isNaN(regression.getInterceptStdErr())); Assert.assertTrue("MSE not NaN", Double.isNaN(regression.getMeanSquareError())); Assert.assertTrue("r NaN", !Double.isNaN(regression.getR())); Assert.assertTrue("r-square NaN", !Double.isNaN(regression.getRSquare())); Assert.assertTrue("RSS NaN", !Double.isNaN(regression.getRegressionSumSquares())); Assert.assertTrue("SSE NaN", !Double.isNaN(regression.getSumSquaredErrors())); Assert.assertTrue("SSTO NaN", !Double.isNaN(regression.getTotalSumSquares())); Assert.assertTrue("predict NaN", !Double.isNaN(regression.predict(0))); regression.addData(1, 4); // MSE, MSE, s(b0), s(b1) should all be OK now Assert.assertTrue("MSE NaN", !Double.isNaN(regression.getMeanSquareError())); Assert.assertTrue("slope std err NaN", !Double.isNaN(regression.getSlopeStdErr())); Assert.assertTrue("intercept std err NaN", !Double.isNaN(regression.getInterceptStdErr())); } @Test public void testClear() { SimpleRegression regression = new SimpleRegression(); regression.addData(corrData); Assert.assertEquals("number of observations", 17, regression.getN()); regression.clear(); Assert.assertEquals("number of observations", 0, regression.getN()); regression.addData(corrData); Assert.assertEquals("r-square", .896123, regression.getRSquare(), 10E-6); regression.addData(data); Assert.assertEquals("number of observations", 53, regression.getN()); } @Test public void testInference() { //---------- verified against R, version 1.8.1 ----- // infData SimpleRegression regression = new SimpleRegression(); regression.addData(infData); Assert.assertEquals("slope std err", 0.011448491, regression.getSlopeStdErr(), 1E-10); Assert.assertEquals("std err intercept", 0.286036932, regression.getInterceptStdErr(),1E-8); Assert.assertEquals("significance", 4.596e-07, regression.getSignificance(),1E-8); Assert.assertEquals("slope conf interval half-width", 0.0270713794287, regression.getSlopeConfidenceInterval(),1E-8); // infData2 regression = new SimpleRegression(); regression.addData(infData2); Assert.assertEquals("slope std err", 1.07260253, regression.getSlopeStdErr(), 1E-8); Assert.assertEquals("std err intercept",4.17718672, regression.getInterceptStdErr(),1E-8); Assert.assertEquals("significance", 0.261829133982, regression.getSignificance(),1E-11); Assert.assertEquals("slope conf interval half-width", 2.97802204827, regression.getSlopeConfidenceInterval(),1E-8); //------------- End R-verified tests ------------------------------- //FIXME: get a real example to test against with alpha = .01 Assert.assertTrue("tighter means wider", regression.getSlopeConfidenceInterval() < regression.getSlopeConfidenceInterval(0.01)); try { regression.getSlopeConfidenceInterval(1); Assert.fail("expecting MathIllegalArgumentException for alpha = 1"); } catch (MathIllegalArgumentException ex) { // ignored } } @Test public void testPerfect() { SimpleRegression regression = new SimpleRegression(); int n = 100; for (int i = 0; i < n; i++) { regression.addData(((double) i) / (n - 1), i); } Assert.assertEquals(0.0, regression.getSignificance(), 1.0e-5); Assert.assertTrue(regression.getSlope() > 0.0); Assert.assertTrue(regression.getSumSquaredErrors() >= 0.0); } @Test public void testPerfectNegative() { SimpleRegression regression = new SimpleRegression(); int n = 100; for (int i = 0; i < n; i++) { regression.addData(- ((double) i) / (n - 1), i); } Assert.assertEquals(0.0, regression.getSignificance(), 1.0e-5); Assert.assertTrue(regression.getSlope() < 0.0); } @Test public void testRandom() { SimpleRegression regression = new SimpleRegression(); Random random = new Random(1); int n = 100; for (int i = 0; i < n; i++) { regression.addData(((double) i) / (n - 1), random.nextDouble()); } Assert.assertTrue( 0.0 < regression.getSignificance() && regression.getSignificance() < 1.0); } // Jira MATH-85 = Bugzilla 39432 @Test public void testSSENonNegative() { double[] y = { 8915.102, 8919.302, 8923.502 }; double[] x = { 1.107178495E2, 1.107264895E2, 1.107351295E2 }; SimpleRegression reg = new SimpleRegression(); for (int i = 0; i < x.length; i++) { reg.addData(x[i], y[i]); } Assert.assertTrue(reg.getSumSquaredErrors() >= 0.0); } // Test remove X,Y (single observation) @Test public void testRemoveXY() { // Create regression with inference data then remove to test SimpleRegression regression = new SimpleRegression(); regression.addData(infData); regression.removeData(removeX, removeY); regression.addData(removeX, removeY); // Use the inference assertions to make sure that everything worked Assert.assertEquals("slope std err", 0.011448491, regression.getSlopeStdErr(), 1E-10); Assert.assertEquals("std err intercept", 0.286036932, regression.getInterceptStdErr(),1E-8); Assert.assertEquals("significance", 4.596e-07, regression.getSignificance(),1E-8); Assert.assertEquals("slope conf interval half-width", 0.0270713794287, regression.getSlopeConfidenceInterval(),1E-8); } // Test remove single observation in array @Test public void testRemoveSingle() { // Create regression with inference data then remove to test SimpleRegression regression = new SimpleRegression(); regression.addData(infData); regression.removeData(removeSingle); regression.addData(removeSingle); // Use the inference assertions to make sure that everything worked Assert.assertEquals("slope std err", 0.011448491, regression.getSlopeStdErr(), 1E-10); Assert.assertEquals("std err intercept", 0.286036932, regression.getInterceptStdErr(),1E-8); Assert.assertEquals("significance", 4.596e-07, regression.getSignificance(),1E-8); Assert.assertEquals("slope conf interval half-width", 0.0270713794287, regression.getSlopeConfidenceInterval(),1E-8); } // Test remove multiple observations @Test public void testRemoveMultiple() { // Create regression with inference data then remove to test SimpleRegression regression = new SimpleRegression(); regression.addData(infData); regression.removeData(removeMultiple); regression.addData(removeMultiple); // Use the inference assertions to make sure that everything worked Assert.assertEquals("slope std err", 0.011448491, regression.getSlopeStdErr(), 1E-10); Assert.assertEquals("std err intercept", 0.286036932, regression.getInterceptStdErr(),1E-8); Assert.assertEquals("significance", 4.596e-07, regression.getSignificance(),1E-8); Assert.assertEquals("slope conf interval half-width", 0.0270713794287, regression.getSlopeConfidenceInterval(),1E-8); } // Remove observation when empty @Test public void testRemoveObsFromEmpty() { SimpleRegression regression = new SimpleRegression(); regression.removeData(removeX, removeY); Assert.assertEquals(regression.getN(), 0); } // Remove single observation to empty @Test public void testRemoveObsFromSingle() { SimpleRegression regression = new SimpleRegression(); regression.addData(removeX, removeY); regression.removeData(removeX, removeY); Assert.assertEquals(regression.getN(), 0); } // Remove multiple observations to empty @Test public void testRemoveMultipleToEmpty() { SimpleRegression regression = new SimpleRegression(); regression.addData(removeMultiple); regression.removeData(removeMultiple); Assert.assertEquals(regression.getN(), 0); } // Remove multiple observations past empty (i.e. size of array > n) @Test public void testRemoveMultiplePastEmpty() { SimpleRegression regression = new SimpleRegression(); regression.addData(removeX, removeY); regression.removeData(removeMultiple); Assert.assertEquals(regression.getN(), 0); } } ././@LongLink100644 0 0 162 12126630646 10260 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/stat/regression/GLSMultipleLinearRegressionTest.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/stat/regression/GLSMultipleLinearRegres100644 1750 1750 30102 12126627674 32322 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.stat.regression; import org.junit.Assert; import org.junit.Before; import org.junit.Test; import org.apache.commons.math3.TestUtils; import org.apache.commons.math3.linear.MatrixUtils; import org.apache.commons.math3.linear.RealMatrix; import org.apache.commons.math3.linear.RealVector; import org.apache.commons.math3.random.CorrelatedRandomVectorGenerator; import org.apache.commons.math3.random.JDKRandomGenerator; import org.apache.commons.math3.random.GaussianRandomGenerator; import org.apache.commons.math3.random.RandomGenerator; import org.apache.commons.math3.stat.correlation.Covariance; import org.apache.commons.math3.stat.descriptive.DescriptiveStatistics; public class GLSMultipleLinearRegressionTest extends MultipleLinearRegressionAbstractTest { private double[] y; private double[][] x; private double[][] omega; private double[] longley = new double[] { 60323,83.0,234289,2356,1590,107608,1947, 61122,88.5,259426,2325,1456,108632,1948, 60171,88.2,258054,3682,1616,109773,1949, 61187,89.5,284599,3351,1650,110929,1950, 63221,96.2,328975,2099,3099,112075,1951, 63639,98.1,346999,1932,3594,113270,1952, 64989,99.0,365385,1870,3547,115094,1953, 63761,100.0,363112,3578,3350,116219,1954, 66019,101.2,397469,2904,3048,117388,1955, 67857,104.6,419180,2822,2857,118734,1956, 68169,108.4,442769,2936,2798,120445,1957, 66513,110.8,444546,4681,2637,121950,1958, 68655,112.6,482704,3813,2552,123366,1959, 69564,114.2,502601,3931,2514,125368,1960, 69331,115.7,518173,4806,2572,127852,1961, 70551,116.9,554894,4007,2827,130081,1962 }; @Before @Override public void setUp(){ y = new double[]{11.0, 12.0, 13.0, 14.0, 15.0, 16.0}; x = new double[6][]; x[0] = new double[]{0, 0, 0, 0, 0}; x[1] = new double[]{2.0, 0, 0, 0, 0}; x[2] = new double[]{0, 3.0, 0, 0, 0}; x[3] = new double[]{0, 0, 4.0, 0, 0}; x[4] = new double[]{0, 0, 0, 5.0, 0}; x[5] = new double[]{0, 0, 0, 0, 6.0}; omega = new double[6][]; omega[0] = new double[]{1.0, 0, 0, 0, 0, 0}; omega[1] = new double[]{0, 2.0, 0, 0, 0, 0}; omega[2] = new double[]{0, 0, 3.0, 0, 0, 0}; omega[3] = new double[]{0, 0, 0, 4.0, 0, 0}; omega[4] = new double[]{0, 0, 0, 0, 5.0, 0}; omega[5] = new double[]{0, 0, 0, 0, 0, 6.0}; super.setUp(); } @Test(expected=IllegalArgumentException.class) public void cannotAddXSampleData() { createRegression().newSampleData(new double[]{}, null, null); } @Test(expected=IllegalArgumentException.class) public void cannotAddNullYSampleData() { createRegression().newSampleData(null, new double[][]{}, null); } @Test(expected=IllegalArgumentException.class) public void cannotAddSampleDataWithSizeMismatch() { double[] y = new double[]{1.0, 2.0}; double[][] x = new double[1][]; x[0] = new double[]{1.0, 0}; createRegression().newSampleData(y, x, null); } @Test(expected=IllegalArgumentException.class) public void cannotAddNullCovarianceData() { createRegression().newSampleData(new double[]{}, new double[][]{}, null); } @Test(expected=IllegalArgumentException.class) public void notEnoughData() { double[] reducedY = new double[y.length - 1]; double[][] reducedX = new double[x.length - 1][]; double[][] reducedO = new double[omega.length - 1][]; System.arraycopy(y, 0, reducedY, 0, reducedY.length); System.arraycopy(x, 0, reducedX, 0, reducedX.length); System.arraycopy(omega, 0, reducedO, 0, reducedO.length); createRegression().newSampleData(reducedY, reducedX, reducedO); } @Test(expected=IllegalArgumentException.class) public void cannotAddCovarianceDataWithSampleSizeMismatch() { double[] y = new double[]{1.0, 2.0}; double[][] x = new double[2][]; x[0] = new double[]{1.0, 0}; x[1] = new double[]{0, 1.0}; double[][] omega = new double[1][]; omega[0] = new double[]{1.0, 0}; createRegression().newSampleData(y, x, omega); } @Test(expected=IllegalArgumentException.class) public void cannotAddCovarianceDataThatIsNotSquare() { double[] y = new double[]{1.0, 2.0}; double[][] x = new double[2][]; x[0] = new double[]{1.0, 0}; x[1] = new double[]{0, 1.0}; double[][] omega = new double[3][]; omega[0] = new double[]{1.0, 0}; omega[1] = new double[]{0, 1.0}; omega[2] = new double[]{0, 2.0}; createRegression().newSampleData(y, x, omega); } @Override protected GLSMultipleLinearRegression createRegression() { GLSMultipleLinearRegression regression = new GLSMultipleLinearRegression(); regression.newSampleData(y, x, omega); return regression; } @Override protected int getNumberOfRegressors() { return x[0].length + 1; } @Override protected int getSampleSize() { return y.length; } /** * test calculateYVariance */ @Test public void testYVariance() { // assumes: y = new double[]{11.0, 12.0, 13.0, 14.0, 15.0, 16.0}; GLSMultipleLinearRegression model = new GLSMultipleLinearRegression(); model.newSampleData(y, x, omega); TestUtils.assertEquals(model.calculateYVariance(), 3.5, 0); } /** * Verifies that setting X, Y and covariance separately has the same effect as newSample(X,Y,cov). */ @Test public void testNewSample2() { double[] y = new double[] {1, 2, 3, 4}; double[][] x = new double[][] { {19, 22, 33}, {20, 30, 40}, {25, 35, 45}, {27, 37, 47} }; double[][] covariance = MatrixUtils.createRealIdentityMatrix(4).scalarMultiply(2).getData(); GLSMultipleLinearRegression regression = new GLSMultipleLinearRegression(); regression.newSampleData(y, x, covariance); RealMatrix combinedX = regression.getX().copy(); RealVector combinedY = regression.getY().copy(); RealMatrix combinedCovInv = regression.getOmegaInverse(); regression.newXSampleData(x); regression.newYSampleData(y); Assert.assertEquals(combinedX, regression.getX()); Assert.assertEquals(combinedY, regression.getY()); Assert.assertEquals(combinedCovInv, regression.getOmegaInverse()); } /** * Verifies that GLS with identity covariance matrix gives the same results * as OLS. */ @Test public void testGLSOLSConsistency() { RealMatrix identityCov = MatrixUtils.createRealIdentityMatrix(16); GLSMultipleLinearRegression glsModel = new GLSMultipleLinearRegression(); OLSMultipleLinearRegression olsModel = new OLSMultipleLinearRegression(); glsModel.newSampleData(longley, 16, 6); olsModel.newSampleData(longley, 16, 6); glsModel.newCovarianceData(identityCov.getData()); double[] olsBeta = olsModel.calculateBeta().toArray(); double[] glsBeta = glsModel.calculateBeta().toArray(); // TODO: Should have assertRelativelyEquals(double[], double[], eps) in TestUtils // Should also add RealVector and RealMatrix versions for (int i = 0; i < olsBeta.length; i++) { TestUtils.assertRelativelyEquals(olsBeta[i], glsBeta[i], 10E-7); } } /** * Generate an error covariance matrix and sample data representing models * with this error structure. Then verify that GLS estimated coefficients, * on average, perform better than OLS. */ @Test public void testGLSEfficiency() { RandomGenerator rg = new JDKRandomGenerator(); rg.setSeed(200); // Seed has been selected to generate non-trivial covariance // Assume model has 16 observations (will use Longley data). Start by generating // non-constant variances for the 16 error terms. final int nObs = 16; double[] sigma = new double[nObs]; for (int i = 0; i < nObs; i++) { sigma[i] = 10 * rg.nextDouble(); } // Now generate 1000 error vectors to use to estimate the covariance matrix // Columns are draws on N(0, sigma[col]) final int numSeeds = 1000; RealMatrix errorSeeds = MatrixUtils.createRealMatrix(numSeeds, nObs); for (int i = 0; i < numSeeds; i++) { for (int j = 0; j < nObs; j++) { errorSeeds.setEntry(i, j, rg.nextGaussian() * sigma[j]); } } // Get covariance matrix for columns RealMatrix cov = (new Covariance(errorSeeds)).getCovarianceMatrix(); // Create a CorrelatedRandomVectorGenerator to use to generate correlated errors GaussianRandomGenerator rawGenerator = new GaussianRandomGenerator(rg); double[] errorMeans = new double[nObs]; // Counting on init to 0 here CorrelatedRandomVectorGenerator gen = new CorrelatedRandomVectorGenerator(errorMeans, cov, 1.0e-12 * cov.getNorm(), rawGenerator); // Now start generating models. Use Longley X matrix on LHS // and Longley OLS beta vector as "true" beta. Generate // Y values by XB + u where u is a CorrelatedRandomVector generated // from cov. OLSMultipleLinearRegression ols = new OLSMultipleLinearRegression(); ols.newSampleData(longley, nObs, 6); final RealVector b = ols.calculateBeta().copy(); final RealMatrix x = ols.getX().copy(); // Create a GLS model to reuse GLSMultipleLinearRegression gls = new GLSMultipleLinearRegression(); gls.newSampleData(longley, nObs, 6); gls.newCovarianceData(cov.getData()); // Create aggregators for stats measuring model performance DescriptiveStatistics olsBetaStats = new DescriptiveStatistics(); DescriptiveStatistics glsBetaStats = new DescriptiveStatistics(); // Generate Y vectors for 10000 models, estimate GLS and OLS and // Verify that OLS estimates are better final int nModels = 10000; for (int i = 0; i < nModels; i++) { // Generate y = xb + u with u cov RealVector u = MatrixUtils.createRealVector(gen.nextVector()); double[] y = u.add(x.operate(b)).toArray(); // Estimate OLS parameters ols.newYSampleData(y); RealVector olsBeta = ols.calculateBeta(); // Estimate GLS parameters gls.newYSampleData(y); RealVector glsBeta = gls.calculateBeta(); // Record deviations from "true" beta double dist = olsBeta.getDistance(b); olsBetaStats.addValue(dist * dist); dist = glsBeta.getDistance(b); glsBetaStats.addValue(dist * dist); } // Verify that GLS is on average more efficient, lower variance assert(olsBetaStats.getMean() > 1.5 * glsBetaStats.getMean()); assert(olsBetaStats.getStandardDeviation() > glsBetaStats.getStandardDeviation()); } } ././@LongLink100644 0 0 167 12126630646 10265 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/stat/regression/MultipleLinearRegressionAbstractTest.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/stat/regression/MultipleLinearRegressio100644 1750 1750 10701 12126627674 32472 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.stat.regression; import org.apache.commons.math3.linear.RealMatrix; import org.apache.commons.math3.linear.RealVector; import org.junit.Assert; import org.junit.Before; import org.junit.Test; public abstract class MultipleLinearRegressionAbstractTest { protected AbstractMultipleLinearRegression regression; @Before public void setUp(){ regression = createRegression(); } protected abstract AbstractMultipleLinearRegression createRegression(); protected abstract int getNumberOfRegressors(); protected abstract int getSampleSize(); @Test public void canEstimateRegressionParameters(){ double[] beta = regression.estimateRegressionParameters(); Assert.assertEquals(getNumberOfRegressors(), beta.length); } @Test public void canEstimateResiduals(){ double[] e = regression.estimateResiduals(); Assert.assertEquals(getSampleSize(), e.length); } @Test public void canEstimateRegressionParametersVariance(){ double[][] variance = regression.estimateRegressionParametersVariance(); Assert.assertEquals(getNumberOfRegressors(), variance.length); } @Test public void canEstimateRegressandVariance(){ if (getSampleSize() > getNumberOfRegressors()) { double variance = regression.estimateRegressandVariance(); Assert.assertTrue(variance > 0.0); } } /** * Verifies that newSampleData methods consistently insert unitary columns * in design matrix. Confirms the fix for MATH-411. */ @Test public void testNewSample() { double[] design = new double[] { 1, 19, 22, 33, 2, 20, 30, 40, 3, 25, 35, 45, 4, 27, 37, 47 }; double[] y = new double[] {1, 2, 3, 4}; double[][] x = new double[][] { {19, 22, 33}, {20, 30, 40}, {25, 35, 45}, {27, 37, 47} }; AbstractMultipleLinearRegression regression = createRegression(); regression.newSampleData(design, 4, 3); RealMatrix flatX = regression.getX().copy(); RealVector flatY = regression.getY().copy(); regression.newXSampleData(x); regression.newYSampleData(y); Assert.assertEquals(flatX, regression.getX()); Assert.assertEquals(flatY, regression.getY()); // No intercept regression.setNoIntercept(true); regression.newSampleData(design, 4, 3); flatX = regression.getX().copy(); flatY = regression.getY().copy(); regression.newXSampleData(x); regression.newYSampleData(y); Assert.assertEquals(flatX, regression.getX()); Assert.assertEquals(flatY, regression.getY()); } @Test(expected=IllegalArgumentException.class) public void testNewSampleNullData() { double[] data = null; createRegression().newSampleData(data, 2, 3); } @Test(expected=IllegalArgumentException.class) public void testNewSampleInvalidData() { double[] data = new double[] {1, 2, 3, 4}; createRegression().newSampleData(data, 2, 3); } @Test(expected=IllegalArgumentException.class) public void testNewSampleInsufficientData() { double[] data = new double[] {1, 2, 3, 4}; createRegression().newSampleData(data, 1, 3); } @Test(expected=IllegalArgumentException.class) public void testXSampleDataNull() { createRegression().newXSampleData(null); } @Test(expected=IllegalArgumentException.class) public void testYSampleDataNull() { createRegression().newYSampleData(null); } } ././@LongLink100644 0 0 154 12126630646 10261 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/stat/inference/WilcoxonSignedRankTestTest.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/stat/inference/WilcoxonSignedRankTestTe100644 1750 1750 16134 12126627674 32346 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.stat.inference; import org.apache.commons.math3.exception.DimensionMismatchException; import org.apache.commons.math3.exception.NoDataException; import org.apache.commons.math3.exception.NullArgumentException; import org.apache.commons.math3.exception.NumberIsTooLargeException; import org.junit.Assert; import org.junit.Test; /** * Test cases for the WilcoxonSignedRangTest class. * * @version $Id: WilcoxonSignedRankTestTest.java 1364030 2012-07-21 01:10:04Z erans $ */ public class WilcoxonSignedRankTestTest { protected WilcoxonSignedRankTest testStatistic = new WilcoxonSignedRankTest(); @Test public void testWilcoxonSignedRankSimple() { /* Target values computed using R version 2.11.1 * x <- c(1.83, 0.50, 1.62, 2.48, 1.68, 1.88, 1.55, 3.06, 1.30) * y <- c(0.878, 0.647, 0.598, 2.05, 1.06, 1.29, 1.06, 3.14, 1.29) */ final double x[] = {1.83, 0.50, 1.62, 2.48, 1.68, 1.88, 1.55, 3.06, 1.30}; final double y[] = {0.878, 0.647, 0.598, 2.05, 1.06, 1.29, 1.06, 3.14, 1.29}; /* EXACT: * wilcox.test(x, y, alternative = "two.sided", mu = 0, paired = TRUE, exact = TRUE, correct = FALSE) * V = 40, p-value = 0.03906 * * Corresponds to the value obtained in R. */ Assert.assertEquals(40, testStatistic.wilcoxonSignedRank(x, y), 1e-10); Assert.assertEquals(0.03906, testStatistic.wilcoxonSignedRankTest(x, y, true), 1e-5); /* ASYMPTOTIC: * wilcox.test(x, y, alternative = "two.sided", mu = 0, paired = TRUE, exact = FALSE, correct = FALSE) * V = 40, p-value = 0.03815 * * This is not entirely the same due to different corrects, * e.g. http://mlsc.lboro.ac.uk/resources/statistics/wsrt.pdf * and src/library/stats/R/wilcox.test.R in the R source */ Assert.assertEquals(40, testStatistic.wilcoxonSignedRank(x, y), 1e-10); Assert.assertEquals(0.0329693812, testStatistic.wilcoxonSignedRankTest(x, y, false), 1e-10); } @Test public void testWilcoxonSignedRankInputValidation() { /* * Exact only for sample size <= 30 */ final double[] x1 = new double[30]; final double[] x2 = new double[31]; final double[] y1 = new double[30]; final double[] y2 = new double[31]; for (int i = 0; i < 30; ++i) { x1[i] = x2[i] = y1[i] = y2[i] = i; } // Exactly 30 is okay //testStatistic.wilcoxonSignedRankTest(x1, y1, true); try { testStatistic.wilcoxonSignedRankTest(x2, y2, true); Assert.fail("More than 30 samples and exact chosen, NumberIsTooLargeException expected"); } catch (NumberIsTooLargeException ex) { // expected } /* Samples must be present, i.e. length > 0 */ try { testStatistic.wilcoxonSignedRankTest(new double[] { }, new double[] { 1.0 }, true); Assert.fail("x does not contain samples (exact), NoDataException expected"); } catch (NoDataException ex) { // expected } try { testStatistic.wilcoxonSignedRankTest(new double[] { }, new double[] { 1.0 }, false); Assert.fail("x does not contain samples (asymptotic), NoDataException expected"); } catch (NoDataException ex) { // expected } try { testStatistic.wilcoxonSignedRankTest(new double[] { 1.0 }, new double[] { }, true); Assert.fail("y does not contain samples (exact), NoDataException expected"); } catch (NoDataException ex) { // expected } try { testStatistic.wilcoxonSignedRankTest(new double[] { 1.0 }, new double[] { }, false); Assert.fail("y does not contain samples (asymptotic), NoDataException expected"); } catch (NoDataException ex) { // expected } /* Samples not same size, i.e. cannot be pairred */ try { testStatistic.wilcoxonSignedRankTest(new double[] { 1.0, 2.0 }, new double[] { 3.0 }, true); Assert.fail("x and y not same size (exact), DimensionMismatchException expected"); } catch (DimensionMismatchException ex) { // expected } try { testStatistic.wilcoxonSignedRankTest(new double[] { 1.0, 2.0 }, new double[] { 3.0 }, false); Assert.fail("x and y not same size (asymptotic), DimensionMismatchException expected"); } catch (DimensionMismatchException ex) { // expected } /* * x and y is null */ try { testStatistic.wilcoxonSignedRankTest(null, null, true); Assert.fail("x and y is null (exact), NullArgumentException expected"); } catch (NullArgumentException ex) { // expected } try { testStatistic.wilcoxonSignedRankTest(null, null, false); Assert.fail("x and y is null (asymptotic), NullArgumentException expected"); } catch (NullArgumentException ex) { // expected } /* * x or y is null */ try { testStatistic.wilcoxonSignedRankTest(null, new double[] { 1.0 }, true); Assert.fail("x is null (exact), NullArgumentException expected"); } catch (NullArgumentException ex) { // expected } try { testStatistic.wilcoxonSignedRankTest(null, new double[] { 1.0 }, false); Assert.fail("x is null (asymptotic), NullArgumentException expected"); } catch (NullArgumentException ex) { // expected } try { testStatistic.wilcoxonSignedRankTest(new double[] { 1.0 }, null, true); Assert.fail("y is null (exact), NullArgumentException expected"); } catch (NullArgumentException ex) { // expected } try { testStatistic.wilcoxonSignedRankTest(new double[] { 1.0 }, null, false); Assert.fail("y is null (asymptotic), NullArgumentException expected"); } catch (NullArgumentException ex) { // expected } } } commons-math3-3.2-src/src/test/java/org/apache/commons/math3/stat/inference/ChiSquareTestTest.java100644 1750 1750 24635 12126627674 31756 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.stat.inference; import org.apache.commons.math3.exception.DimensionMismatchException; import org.apache.commons.math3.exception.NotPositiveException; import org.apache.commons.math3.exception.NotStrictlyPositiveException; import org.apache.commons.math3.exception.OutOfRangeException; import org.apache.commons.math3.exception.ZeroException; import org.junit.Assert; import org.junit.Test; /** * Test cases for the ChiSquareTestImpl class. * * @version $Id: ChiSquareTestTest.java 1364030 2012-07-21 01:10:04Z erans $ */ public class ChiSquareTestTest { protected ChiSquareTest testStatistic = new ChiSquareTest(); @Test public void testChiSquare() { // Target values computed using R version 1.8.1 // Some assembly required ;-) // Use sum((obs - exp)^2/exp) for the chi-square statistic and // 1 - pchisq(sum((obs - exp)^2/exp), length(obs) - 1) for the p-value long[] observed = {10, 9, 11}; double[] expected = {10, 10, 10}; Assert.assertEquals("chi-square statistic", 0.2, testStatistic.chiSquare(expected, observed), 10E-12); Assert.assertEquals("chi-square p-value", 0.904837418036, testStatistic.chiSquareTest(expected, observed), 1E-10); long[] observed1 = { 500, 623, 72, 70, 31 }; double[] expected1 = { 485, 541, 82, 61, 37 }; Assert.assertEquals( "chi-square test statistic", 9.023307936427388, testStatistic.chiSquare(expected1, observed1), 1E-10); Assert.assertEquals("chi-square p-value", 0.06051952647453607, testStatistic.chiSquareTest(expected1, observed1), 1E-9); Assert.assertTrue("chi-square test reject", testStatistic.chiSquareTest(expected1, observed1, 0.08)); Assert.assertTrue("chi-square test accept", !testStatistic.chiSquareTest(expected1, observed1, 0.05)); try { testStatistic.chiSquareTest(expected1, observed1, 95); Assert.fail("alpha out of range, OutOfRangeException expected"); } catch (OutOfRangeException ex) { // expected } long[] tooShortObs = { 0 }; double[] tooShortEx = { 1 }; try { testStatistic.chiSquare(tooShortEx, tooShortObs); Assert.fail("arguments too short, DimensionMismatchException expected"); } catch (DimensionMismatchException ex) { // expected } // unmatched arrays long[] unMatchedObs = { 0, 1, 2, 3 }; double[] unMatchedEx = { 1, 1, 2 }; try { testStatistic.chiSquare(unMatchedEx, unMatchedObs); Assert.fail("arrays have different lengths, DimensionMismatchException expected"); } catch (DimensionMismatchException ex) { // expected } // 0 expected count expected[0] = 0; try { testStatistic.chiSquareTest(expected, observed, .01); Assert.fail("bad expected count, NotStrictlyPositiveException expected"); } catch (NotStrictlyPositiveException ex) { // expected } // negative observed count expected[0] = 1; observed[0] = -1; try { testStatistic.chiSquareTest(expected, observed, .01); Assert.fail("bad expected count, NotPositiveException expected"); } catch (NotPositiveException ex) { // expected } } @Test public void testChiSquareIndependence() { // Target values computed using R version 1.8.1 long[][] counts = { {40, 22, 43}, {91, 21, 28}, {60, 10, 22}}; Assert.assertEquals( "chi-square test statistic", 22.709027688, testStatistic.chiSquare(counts), 1E-9); Assert.assertEquals("chi-square p-value", 0.000144751460134, testStatistic.chiSquareTest(counts), 1E-9); Assert.assertTrue("chi-square test reject", testStatistic.chiSquareTest(counts, 0.0002)); Assert.assertTrue("chi-square test accept", !testStatistic.chiSquareTest(counts, 0.0001)); long[][] counts2 = {{10, 15}, {30, 40}, {60, 90} }; Assert.assertEquals( "chi-square test statistic", 0.168965517241, testStatistic.chiSquare(counts2), 1E-9); Assert.assertEquals("chi-square p-value",0.918987499852, testStatistic.chiSquareTest(counts2), 1E-9); Assert.assertTrue("chi-square test accept", !testStatistic.chiSquareTest(counts2, 0.1)); // ragged input array long[][] counts3 = { {40, 22, 43}, {91, 21, 28}, {60, 10}}; try { testStatistic.chiSquare(counts3); Assert.fail("Expecting DimensionMismatchException"); } catch (DimensionMismatchException ex) { // expected } // insufficient data long[][] counts4 = {{40, 22, 43}}; try { testStatistic.chiSquare(counts4); Assert.fail("Expecting DimensionMismatchException"); } catch (DimensionMismatchException ex) { // expected } long[][] counts5 = {{40}, {40}, {30}, {10}}; try { testStatistic.chiSquare(counts5); Assert.fail("Expecting DimensionMismatchException"); } catch (DimensionMismatchException ex) { // expected } // negative counts long[][] counts6 = {{10, -2}, {30, 40}, {60, 90} }; try { testStatistic.chiSquare(counts6); Assert.fail("Expecting NotPositiveException"); } catch (NotPositiveException ex) { // expected } // bad alpha try { testStatistic.chiSquareTest(counts, 0); Assert.fail("Expecting OutOfRangeException"); } catch (OutOfRangeException ex) { // expected } } @Test public void testChiSquareLargeTestStatistic() { double[] exp = new double[] { 3389119.5, 649136.6, 285745.4, 25357364.76, 11291189.78, 543628.0, 232921.0, 437665.75 }; long[] obs = new long[] { 2372383, 584222, 257170, 17750155, 7903832, 489265, 209628, 393899 }; org.apache.commons.math3.stat.inference.ChiSquareTest csti = new org.apache.commons.math3.stat.inference.ChiSquareTest(); double cst = csti.chiSquareTest(exp, obs); Assert.assertEquals("chi-square p-value", 0.0, cst, 1E-3); Assert.assertEquals( "chi-square test statistic", 114875.90421929007, testStatistic.chiSquare(exp, obs), 1E-9); } /** Contingency table containing zeros - PR # 32531 */ @Test public void testChiSquareZeroCount() { // Target values computed using R version 1.8.1 long[][] counts = { {40, 0, 4}, {91, 1, 2}, {60, 2, 0}}; Assert.assertEquals( "chi-square test statistic", 9.67444662263, testStatistic.chiSquare(counts), 1E-9); Assert.assertEquals("chi-square p-value", 0.0462835770603, testStatistic.chiSquareTest(counts), 1E-9); } /** Target values verified using DATAPLOT version 2006.3 */ @Test public void testChiSquareDataSetsComparisonEqualCounts() { long[] observed1 = {10, 12, 12, 10}; long[] observed2 = {5, 15, 14, 10}; Assert.assertEquals("chi-square p value", 0.541096, testStatistic.chiSquareTestDataSetsComparison( observed1, observed2), 1E-6); Assert.assertEquals("chi-square test statistic", 2.153846, testStatistic.chiSquareDataSetsComparison( observed1, observed2), 1E-6); Assert.assertFalse("chi-square test result", testStatistic.chiSquareTestDataSetsComparison( observed1, observed2, 0.4)); } /** Target values verified using DATAPLOT version 2006.3 */ @Test public void testChiSquareDataSetsComparisonUnEqualCounts() { long[] observed1 = {10, 12, 12, 10, 15}; long[] observed2 = {15, 10, 10, 15, 5}; Assert.assertEquals("chi-square p value", 0.124115, testStatistic.chiSquareTestDataSetsComparison( observed1, observed2), 1E-6); Assert.assertEquals("chi-square test statistic", 7.232189, testStatistic.chiSquareDataSetsComparison( observed1, observed2), 1E-6); Assert.assertTrue("chi-square test result", testStatistic.chiSquareTestDataSetsComparison( observed1, observed2, 0.13)); Assert.assertFalse("chi-square test result", testStatistic.chiSquareTestDataSetsComparison( observed1, observed2, 0.12)); } @Test public void testChiSquareDataSetsComparisonBadCounts() { long[] observed1 = {10, -1, 12, 10, 15}; long[] observed2 = {15, 10, 10, 15, 5}; try { testStatistic.chiSquareTestDataSetsComparison( observed1, observed2); Assert.fail("Expecting NotPositiveException - negative count"); } catch (NotPositiveException ex) { // expected } long[] observed3 = {10, 0, 12, 10, 15}; long[] observed4 = {15, 0, 10, 15, 5}; try { testStatistic.chiSquareTestDataSetsComparison( observed3, observed4); Assert.fail("Expecting ZeroException - double 0's"); } catch (ZeroException ex) { // expected } long[] observed5 = {10, 10, 12, 10, 15}; long[] observed6 = {0, 0, 0, 0, 0}; try { testStatistic.chiSquareTestDataSetsComparison( observed5, observed6); Assert.fail("Expecting ZeroException - vanishing counts"); } catch (ZeroException ex) { // expected } } } commons-math3-3.2-src/src/test/java/org/apache/commons/math3/stat/inference/TestUtilsTest.java100644 1750 1750 52522 12126627674 31166 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.stat.inference; import java.util.ArrayList; import java.util.List; import org.apache.commons.math3.exception.DimensionMismatchException; import org.apache.commons.math3.exception.NotPositiveException; import org.apache.commons.math3.exception.NotStrictlyPositiveException; import org.apache.commons.math3.exception.NullArgumentException; import org.apache.commons.math3.exception.NumberIsTooSmallException; import org.apache.commons.math3.exception.OutOfRangeException; import org.apache.commons.math3.stat.descriptive.SummaryStatistics; import org.junit.Assert; import org.junit.Test; /** * Test cases for the TestUtils class. * * @version $Id: TestUtilsTest.java 1408173 2012-11-12 04:32:02Z psteitz $ */ public class TestUtilsTest { @Test public void testChiSquare() { // Target values computed using R version 1.8.1 // Some assembly required ;-) // Use sum((obs - exp)^2/exp) for the chi-square statistic and // 1 - pchisq(sum((obs - exp)^2/exp), length(obs) - 1) for the p-value long[] observed = {10, 9, 11}; double[] expected = {10, 10, 10}; Assert.assertEquals("chi-square statistic", 0.2, TestUtils.chiSquare(expected, observed), 10E-12); Assert.assertEquals("chi-square p-value", 0.904837418036, TestUtils.chiSquareTest(expected, observed), 1E-10); long[] observed1 = { 500, 623, 72, 70, 31 }; double[] expected1 = { 485, 541, 82, 61, 37 }; Assert.assertEquals( "chi-square test statistic", 9.023307936427388, TestUtils.chiSquare(expected1, observed1), 1E-10); Assert.assertEquals("chi-square p-value", 0.06051952647453607, TestUtils.chiSquareTest(expected1, observed1), 1E-9); Assert.assertTrue("chi-square test reject", TestUtils.chiSquareTest(expected1, observed1, 0.07)); Assert.assertTrue("chi-square test accept", !TestUtils.chiSquareTest(expected1, observed1, 0.05)); try { TestUtils.chiSquareTest(expected1, observed1, 95); Assert.fail("alpha out of range, OutOfRangeException expected"); } catch (OutOfRangeException ex) { // expected } long[] tooShortObs = { 0 }; double[] tooShortEx = { 1 }; try { TestUtils.chiSquare(tooShortEx, tooShortObs); Assert.fail("arguments too short, DimensionMismatchException expected"); } catch (DimensionMismatchException ex) { // expected } // unmatched arrays long[] unMatchedObs = { 0, 1, 2, 3 }; double[] unMatchedEx = { 1, 1, 2 }; try { TestUtils.chiSquare(unMatchedEx, unMatchedObs); Assert.fail("arrays have different lengths, DimensionMismatchException expected"); } catch (DimensionMismatchException ex) { // expected } // 0 expected count expected[0] = 0; try { TestUtils.chiSquareTest(expected, observed, .01); Assert.fail("bad expected count, NotStrictlyPositiveException expected"); } catch (NotStrictlyPositiveException ex) { // expected } // negative observed count expected[0] = 1; observed[0] = -1; try { TestUtils.chiSquareTest(expected, observed, .01); Assert.fail("bad expected count, NotPositiveException expected"); } catch (NotPositiveException ex) { // expected } } @Test public void testChiSquareIndependence() { // Target values computed using R version 1.8.1 long[][] counts = { {40, 22, 43}, {91, 21, 28}, {60, 10, 22}}; Assert.assertEquals( "chi-square test statistic", 22.709027688, TestUtils.chiSquare(counts), 1E-9); Assert.assertEquals("chi-square p-value", 0.000144751460134, TestUtils.chiSquareTest(counts), 1E-9); Assert.assertTrue("chi-square test reject", TestUtils.chiSquareTest(counts, 0.0002)); Assert.assertTrue("chi-square test accept", !TestUtils.chiSquareTest(counts, 0.0001)); long[][] counts2 = {{10, 15}, {30, 40}, {60, 90} }; Assert.assertEquals( "chi-square test statistic", 0.168965517241, TestUtils.chiSquare(counts2), 1E-9); Assert.assertEquals("chi-square p-value",0.918987499852, TestUtils.chiSquareTest(counts2), 1E-9); Assert.assertTrue("chi-square test accept", !TestUtils.chiSquareTest(counts2, 0.1)); // ragged input array long[][] counts3 = { {40, 22, 43}, {91, 21, 28}, {60, 10}}; try { TestUtils.chiSquare(counts3); Assert.fail("Expecting DimensionMismatchException"); } catch (DimensionMismatchException ex) { // expected } // insufficient data long[][] counts4 = {{40, 22, 43}}; try { TestUtils.chiSquare(counts4); Assert.fail("Expecting DimensionMismatchException"); } catch (DimensionMismatchException ex) { // expected } long[][] counts5 = {{40}, {40}, {30}, {10}}; try { TestUtils.chiSquare(counts5); Assert.fail("Expecting DimensionMismatchException"); } catch (DimensionMismatchException ex) { // expected } // negative counts long[][] counts6 = {{10, -2}, {30, 40}, {60, 90} }; try { TestUtils.chiSquare(counts6); Assert.fail("Expecting NotPositiveException"); } catch (NotPositiveException ex) { // expected } // bad alpha try { TestUtils.chiSquareTest(counts, 0); Assert.fail("Expecting OutOfRangeException"); } catch (OutOfRangeException ex) { // expected } } @Test public void testChiSquareLargeTestStatistic() { double[] exp = new double[] { 3389119.5, 649136.6, 285745.4, 25357364.76, 11291189.78, 543628.0, 232921.0, 437665.75 }; long[] obs = new long[] { 2372383, 584222, 257170, 17750155, 7903832, 489265, 209628, 393899 }; org.apache.commons.math3.stat.inference.ChiSquareTest csti = new org.apache.commons.math3.stat.inference.ChiSquareTest(); double cst = csti.chiSquareTest(exp, obs); Assert.assertEquals("chi-square p-value", 0.0, cst, 1E-3); Assert.assertEquals( "chi-square test statistic", 114875.90421929007, TestUtils.chiSquare(exp, obs), 1E-9); } /** Contingency table containing zeros - PR # 32531 */ @Test public void testChiSquareZeroCount() { // Target values computed using R version 1.8.1 long[][] counts = { {40, 0, 4}, {91, 1, 2}, {60, 2, 0}}; Assert.assertEquals( "chi-square test statistic", 9.67444662263, TestUtils.chiSquare(counts), 1E-9); Assert.assertEquals("chi-square p-value", 0.0462835770603, TestUtils.chiSquareTest(counts), 1E-9); } private double[] tooShortObs = { 1.0 }; private double[] emptyObs = {}; private SummaryStatistics emptyStats = new SummaryStatistics(); @Test public void testOneSampleT() { double[] observed = {93.0, 103.0, 95.0, 101.0, 91.0, 105.0, 96.0, 94.0, 101.0, 88.0, 98.0, 94.0, 101.0, 92.0, 95.0 }; double mu = 100.0; SummaryStatistics sampleStats = null; sampleStats = new SummaryStatistics(); for (int i = 0; i < observed.length; i++) { sampleStats.addValue(observed[i]); } // Target comparison values computed using R version 1.8.1 (Linux version) Assert.assertEquals("t statistic", -2.81976445346, TestUtils.t(mu, observed), 10E-10); Assert.assertEquals("t statistic", -2.81976445346, TestUtils.t(mu, sampleStats), 10E-10); Assert.assertEquals("p value", 0.0136390585873, TestUtils.tTest(mu, observed), 10E-10); Assert.assertEquals("p value", 0.0136390585873, TestUtils.tTest(mu, sampleStats), 10E-10); try { TestUtils.t(mu, (double[]) null); Assert.fail("arguments too short, NullArgumentException expected"); } catch (NullArgumentException ex) { // expected } try { TestUtils.t(mu, (SummaryStatistics) null); Assert.fail("arguments too short, NullArgumentException expected"); } catch (NullArgumentException ex) { // expected } try { TestUtils.t(mu, emptyObs); Assert.fail("arguments too short, NumberIsTooSmallException expected"); } catch (NumberIsTooSmallException ex) { // expected } try { TestUtils.t(mu, emptyStats); Assert.fail("arguments too short, NumberIsTooSmallException expected"); } catch (NumberIsTooSmallException ex) { // expected } try { TestUtils.t(mu, tooShortObs); Assert.fail("insufficient data to compute t statistic, NumberIsTooSmallException expected"); } catch (NumberIsTooSmallException ex) { // expected } try { TestUtils.tTest(mu, tooShortObs); Assert.fail("insufficient data to perform t test, NumberIsTooSmallException expected"); } catch (NumberIsTooSmallException ex) { // expected } try { TestUtils.t(mu, (SummaryStatistics) null); Assert.fail("insufficient data to compute t statistic, NullArgumentException expected"); } catch (NullArgumentException ex) { // expected } try { TestUtils.tTest(mu, (SummaryStatistics) null); Assert.fail("insufficient data to perform t test, NullArgumentException expected"); } catch (NullArgumentException ex) { // expected } } @Test public void testOneSampleTTest() { double[] oneSidedP = {2d, 0d, 6d, 6d, 3d, 3d, 2d, 3d, -6d, 6d, 6d, 6d, 3d, 0d, 1d, 1d, 0d, 2d, 3d, 3d }; SummaryStatistics oneSidedPStats = new SummaryStatistics(); for (int i = 0; i < oneSidedP.length; i++) { oneSidedPStats.addValue(oneSidedP[i]); } // Target comparison values computed using R version 1.8.1 (Linux version) Assert.assertEquals("one sample t stat", 3.86485535541, TestUtils.t(0d, oneSidedP), 10E-10); Assert.assertEquals("one sample t stat", 3.86485535541, TestUtils.t(0d, oneSidedPStats),1E-10); Assert.assertEquals("one sample p value", 0.000521637019637, TestUtils.tTest(0d, oneSidedP) / 2d, 10E-10); Assert.assertEquals("one sample p value", 0.000521637019637, TestUtils.tTest(0d, oneSidedPStats) / 2d, 10E-5); Assert.assertTrue("one sample t-test reject", TestUtils.tTest(0d, oneSidedP, 0.01)); Assert.assertTrue("one sample t-test reject", TestUtils.tTest(0d, oneSidedPStats, 0.01)); Assert.assertTrue("one sample t-test accept", !TestUtils.tTest(0d, oneSidedP, 0.0001)); Assert.assertTrue("one sample t-test accept", !TestUtils.tTest(0d, oneSidedPStats, 0.0001)); try { TestUtils.tTest(0d, oneSidedP, 95); Assert.fail("alpha out of range, OutOfRangeException expected"); } catch (OutOfRangeException ex) { // expected } try { TestUtils.tTest(0d, oneSidedPStats, 95); Assert.fail("alpha out of range, OutOfRangeException expected"); } catch (OutOfRangeException ex) { // expected } } @Test public void testTwoSampleTHeterscedastic() { double[] sample1 = { 7d, -4d, 18d, 17d, -3d, -5d, 1d, 10d, 11d, -2d }; double[] sample2 = { -1d, 12d, -1d, -3d, 3d, -5d, 5d, 2d, -11d, -1d, -3d }; SummaryStatistics sampleStats1 = new SummaryStatistics(); for (int i = 0; i < sample1.length; i++) { sampleStats1.addValue(sample1[i]); } SummaryStatistics sampleStats2 = new SummaryStatistics(); for (int i = 0; i < sample2.length; i++) { sampleStats2.addValue(sample2[i]); } // Target comparison values computed using R version 1.8.1 (Linux version) Assert.assertEquals("two sample heteroscedastic t stat", 1.60371728768, TestUtils.t(sample1, sample2), 1E-10); Assert.assertEquals("two sample heteroscedastic t stat", 1.60371728768, TestUtils.t(sampleStats1, sampleStats2), 1E-10); Assert.assertEquals("two sample heteroscedastic p value", 0.128839369622, TestUtils.tTest(sample1, sample2), 1E-10); Assert.assertEquals("two sample heteroscedastic p value", 0.128839369622, TestUtils.tTest(sampleStats1, sampleStats2), 1E-10); Assert.assertTrue("two sample heteroscedastic t-test reject", TestUtils.tTest(sample1, sample2, 0.2)); Assert.assertTrue("two sample heteroscedastic t-test reject", TestUtils.tTest(sampleStats1, sampleStats2, 0.2)); Assert.assertTrue("two sample heteroscedastic t-test accept", !TestUtils.tTest(sample1, sample2, 0.1)); Assert.assertTrue("two sample heteroscedastic t-test accept", !TestUtils.tTest(sampleStats1, sampleStats2, 0.1)); try { TestUtils.tTest(sample1, sample2, .95); Assert.fail("alpha out of range, OutOfRangeException expected"); } catch (OutOfRangeException ex) { // expected } try { TestUtils.tTest(sampleStats1, sampleStats2, .95); Assert.fail("alpha out of range, OutOfRangeException expected"); } catch (OutOfRangeException ex) { // expected } try { TestUtils.tTest(sample1, tooShortObs, .01); Assert.fail("insufficient data, NumberIsTooSmallException expected"); } catch (NumberIsTooSmallException ex) { // expected } try { TestUtils.tTest(sampleStats1, (SummaryStatistics) null, .01); Assert.fail("insufficient data, NullArgumentException expected"); } catch (NullArgumentException ex) { // expected } try { TestUtils.tTest(sample1, tooShortObs); Assert.fail("insufficient data, NumberIsTooSmallException expected"); } catch (NumberIsTooSmallException ex) { // expected } try { TestUtils.tTest(sampleStats1, (SummaryStatistics) null); Assert.fail("insufficient data, NullArgumentException expected"); } catch (NullArgumentException ex) { // expected } try { TestUtils.t(sample1, tooShortObs); Assert.fail("insufficient data, NumberIsTooSmallException expected"); } catch (NumberIsTooSmallException ex) { // expected } try { TestUtils.t(sampleStats1, (SummaryStatistics) null); Assert.fail("insufficient data, NullArgumentException expected"); } catch (NullArgumentException ex) { // expected } } @Test public void testTwoSampleTHomoscedastic() { double[] sample1 ={2, 4, 6, 8, 10, 97}; double[] sample2 = {4, 6, 8, 10, 16}; SummaryStatistics sampleStats1 = new SummaryStatistics(); for (int i = 0; i < sample1.length; i++) { sampleStats1.addValue(sample1[i]); } SummaryStatistics sampleStats2 = new SummaryStatistics(); for (int i = 0; i < sample2.length; i++) { sampleStats2.addValue(sample2[i]); } // Target comparison values computed using R version 1.8.1 (Linux version) Assert.assertEquals("two sample homoscedastic t stat", 0.73096310086, TestUtils.homoscedasticT(sample1, sample2), 10E-11); Assert.assertEquals("two sample homoscedastic p value", 0.4833963785, TestUtils.homoscedasticTTest(sampleStats1, sampleStats2), 1E-10); Assert.assertTrue("two sample homoscedastic t-test reject", TestUtils.homoscedasticTTest(sample1, sample2, 0.49)); Assert.assertTrue("two sample homoscedastic t-test accept", !TestUtils.homoscedasticTTest(sample1, sample2, 0.48)); } @Test public void testSmallSamples() { double[] sample1 = {1d, 3d}; double[] sample2 = {4d, 5d}; // Target values computed using R, version 1.8.1 (linux version) Assert.assertEquals(-2.2360679775, TestUtils.t(sample1, sample2), 1E-10); Assert.assertEquals(0.198727388935, TestUtils.tTest(sample1, sample2), 1E-10); } @Test public void testPaired() { double[] sample1 = {1d, 3d, 5d, 7d}; double[] sample2 = {0d, 6d, 11d, 2d}; double[] sample3 = {5d, 7d, 8d, 10d}; // Target values computed using R, version 1.8.1 (linux version) Assert.assertEquals(-0.3133, TestUtils.pairedT(sample1, sample2), 1E-4); Assert.assertEquals(0.774544295819, TestUtils.pairedTTest(sample1, sample2), 1E-10); Assert.assertEquals(0.001208, TestUtils.pairedTTest(sample1, sample3), 1E-6); Assert.assertFalse(TestUtils.pairedTTest(sample1, sample3, .001)); Assert.assertTrue(TestUtils.pairedTTest(sample1, sample3, .002)); } private double[] classA = {93.0, 103.0, 95.0, 101.0}; private double[] classB = {99.0, 92.0, 102.0, 100.0, 102.0}; private double[] classC = {110.0, 115.0, 111.0, 117.0, 128.0}; private List classes = new ArrayList(); private OneWayAnova oneWayAnova = new OneWayAnova(); @Test public void testOneWayAnovaUtils() { classes.add(classA); classes.add(classB); classes.add(classC); Assert.assertEquals(oneWayAnova.anovaFValue(classes), TestUtils.oneWayAnovaFValue(classes), 10E-12); Assert.assertEquals(oneWayAnova.anovaPValue(classes), TestUtils.oneWayAnovaPValue(classes), 10E-12); Assert.assertEquals(oneWayAnova.anovaTest(classes, 0.01), TestUtils.oneWayAnovaTest(classes, 0.01)); } @Test public void testGTestGoodnesOfFit() throws Exception { double[] exp = new double[]{ 0.54d, 0.40d, 0.05d, 0.01d }; long[] obs = new long[]{ 70, 79, 3, 4 }; Assert.assertEquals("G test statistic", 13.144799, TestUtils.g(exp, obs), 1E-5); double p_gtgf = TestUtils.gTest(exp, obs); Assert.assertEquals("g-Test p-value", 0.004333, p_gtgf, 1E-5); Assert.assertTrue(TestUtils.gTest(exp, obs, 0.05)); } @Test public void testGTestIndependance() throws Exception { long[] obs1 = new long[]{ 268, 199, 42 }; long[] obs2 = new long[]{ 807, 759, 184 }; double g = TestUtils.gDataSetsComparison(obs1, obs2); Assert.assertEquals("G test statistic", 7.3008170, g, 1E-4); double p_gti = TestUtils.gTestDataSetsComparison(obs1, obs2); Assert.assertEquals("g-Test p-value", 0.0259805, p_gti, 1E-4); Assert.assertTrue(TestUtils.gTestDataSetsComparison(obs1, obs2, 0.05)); } @Test public void testRootLogLikelihood() { // positive where k11 is bigger than expected. Assert.assertTrue(TestUtils.rootLogLikelihoodRatio(904, 21060, 1144, 283012) > 0.0); // negative because k11 is lower than expected Assert.assertTrue(TestUtils.rootLogLikelihoodRatio(36, 21928, 60280, 623876) < 0.0); Assert.assertEquals(Math.sqrt(2.772589), TestUtils.rootLogLikelihoodRatio(1, 0, 0, 1), 0.000001); Assert.assertEquals(-Math.sqrt(2.772589), TestUtils.rootLogLikelihoodRatio(0, 1, 1, 0), 0.000001); Assert.assertEquals(Math.sqrt(27.72589), TestUtils.rootLogLikelihoodRatio(10, 0, 0, 10), 0.00001); Assert.assertEquals(Math.sqrt(39.33052), TestUtils.rootLogLikelihoodRatio(5, 1995, 0, 100000), 0.00001); Assert.assertEquals(-Math.sqrt(39.33052), TestUtils.rootLogLikelihoodRatio(0, 100000, 5, 1995), 0.00001); Assert.assertEquals(Math.sqrt(4730.737), TestUtils.rootLogLikelihoodRatio(1000, 1995, 1000, 100000), 0.001); Assert.assertEquals(-Math.sqrt(4730.737), TestUtils.rootLogLikelihoodRatio(1000, 100000, 1000, 1995), 0.001); Assert.assertEquals(Math.sqrt(5734.343), TestUtils.rootLogLikelihoodRatio(1000, 1000, 1000, 100000), 0.001); Assert.assertEquals(Math.sqrt(5714.932), TestUtils.rootLogLikelihoodRatio(1000, 1000, 1000, 99000), 0.001); } } commons-math3-3.2-src/src/test/java/org/apache/commons/math3/stat/inference/TTestTest.java100644 1750 1750 30135 12126627674 30265 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.stat.inference; import org.apache.commons.math3.exception.NullArgumentException; import org.apache.commons.math3.exception.NumberIsTooSmallException; import org.apache.commons.math3.exception.OutOfRangeException; import org.apache.commons.math3.stat.descriptive.SummaryStatistics; import org.junit.Assert; import org.junit.Before; import org.junit.Test; /** * Test cases for the TTestImpl class. * * @version $Id: TTestTest.java 1364030 2012-07-21 01:10:04Z erans $ */ public class TTestTest { protected TTest testStatistic = new TTest(); private double[] tooShortObs = { 1.0 }; private double[] emptyObs = {}; private SummaryStatistics emptyStats = new SummaryStatistics(); SummaryStatistics tooShortStats = null; @Before public void setUp() { tooShortStats = new SummaryStatistics(); tooShortStats.addValue(0d); } @Test public void testOneSampleT() { double[] observed = {93.0, 103.0, 95.0, 101.0, 91.0, 105.0, 96.0, 94.0, 101.0, 88.0, 98.0, 94.0, 101.0, 92.0, 95.0 }; double mu = 100.0; SummaryStatistics sampleStats = null; sampleStats = new SummaryStatistics(); for (int i = 0; i < observed.length; i++) { sampleStats.addValue(observed[i]); } // Target comparison values computed using R version 1.8.1 (Linux version) Assert.assertEquals("t statistic", -2.81976445346, testStatistic.t(mu, observed), 10E-10); Assert.assertEquals("t statistic", -2.81976445346, testStatistic.t(mu, sampleStats), 10E-10); Assert.assertEquals("p value", 0.0136390585873, testStatistic.tTest(mu, observed), 10E-10); Assert.assertEquals("p value", 0.0136390585873, testStatistic.tTest(mu, sampleStats), 10E-10); try { testStatistic.t(mu, (double[]) null); Assert.fail("arguments too short, NullArgumentException expected"); } catch (NullArgumentException ex) { // expected } try { testStatistic.t(mu, (SummaryStatistics) null); Assert.fail("arguments too short, NullArgumentException expected"); } catch (NullArgumentException ex) { // expected } try { testStatistic.t(mu, emptyObs); Assert.fail("arguments too short, NumberIsTooSmallException expected"); } catch (NumberIsTooSmallException ex) { // expected } try { testStatistic.t(mu, emptyStats); Assert.fail("arguments too short, NumberIsTooSmallException expected"); } catch (NumberIsTooSmallException ex) { // expected } try { testStatistic.t(mu, tooShortObs); Assert.fail("insufficient data to compute t statistic, NumberIsTooSmallException expected"); } catch (NumberIsTooSmallException ex) { // expected } try { testStatistic.tTest(mu, tooShortObs); Assert.fail("insufficient data to perform t test, NumberIsTooSmallException expected"); } catch (NumberIsTooSmallException ex) { // expected } try { testStatistic.t(mu, tooShortStats); Assert.fail("insufficient data to compute t statistic, NumberIsTooSmallException expected"); } catch (NumberIsTooSmallException ex) { // expected } try { testStatistic.tTest(mu, tooShortStats); Assert.fail("insufficient data to perform t test, NumberIsTooSmallException expected"); } catch (NumberIsTooSmallException ex) { // expected } } @Test public void testOneSampleTTest() { double[] oneSidedP = {2d, 0d, 6d, 6d, 3d, 3d, 2d, 3d, -6d, 6d, 6d, 6d, 3d, 0d, 1d, 1d, 0d, 2d, 3d, 3d }; SummaryStatistics oneSidedPStats = new SummaryStatistics(); for (int i = 0; i < oneSidedP.length; i++) { oneSidedPStats.addValue(oneSidedP[i]); } // Target comparison values computed using R version 1.8.1 (Linux version) Assert.assertEquals("one sample t stat", 3.86485535541, testStatistic.t(0d, oneSidedP), 10E-10); Assert.assertEquals("one sample t stat", 3.86485535541, testStatistic.t(0d, oneSidedPStats),1E-10); Assert.assertEquals("one sample p value", 0.000521637019637, testStatistic.tTest(0d, oneSidedP) / 2d, 10E-10); Assert.assertEquals("one sample p value", 0.000521637019637, testStatistic.tTest(0d, oneSidedPStats) / 2d, 10E-5); Assert.assertTrue("one sample t-test reject", testStatistic.tTest(0d, oneSidedP, 0.01)); Assert.assertTrue("one sample t-test reject", testStatistic.tTest(0d, oneSidedPStats, 0.01)); Assert.assertTrue("one sample t-test accept", !testStatistic.tTest(0d, oneSidedP, 0.0001)); Assert.assertTrue("one sample t-test accept", !testStatistic.tTest(0d, oneSidedPStats, 0.0001)); try { testStatistic.tTest(0d, oneSidedP, 95); Assert.fail("alpha out of range, OutOfRangeException expected"); } catch (OutOfRangeException ex) { // expected } try { testStatistic.tTest(0d, oneSidedPStats, 95); Assert.fail("alpha out of range, OutOfRangeException expected"); } catch (OutOfRangeException ex) { // expected } } @Test public void testTwoSampleTHeterscedastic() { double[] sample1 = { 7d, -4d, 18d, 17d, -3d, -5d, 1d, 10d, 11d, -2d }; double[] sample2 = { -1d, 12d, -1d, -3d, 3d, -5d, 5d, 2d, -11d, -1d, -3d }; SummaryStatistics sampleStats1 = new SummaryStatistics(); for (int i = 0; i < sample1.length; i++) { sampleStats1.addValue(sample1[i]); } SummaryStatistics sampleStats2 = new SummaryStatistics(); for (int i = 0; i < sample2.length; i++) { sampleStats2.addValue(sample2[i]); } // Target comparison values computed using R version 1.8.1 (Linux version) Assert.assertEquals("two sample heteroscedastic t stat", 1.60371728768, testStatistic.t(sample1, sample2), 1E-10); Assert.assertEquals("two sample heteroscedastic t stat", 1.60371728768, testStatistic.t(sampleStats1, sampleStats2), 1E-10); Assert.assertEquals("two sample heteroscedastic p value", 0.128839369622, testStatistic.tTest(sample1, sample2), 1E-10); Assert.assertEquals("two sample heteroscedastic p value", 0.128839369622, testStatistic.tTest(sampleStats1, sampleStats2), 1E-10); Assert.assertTrue("two sample heteroscedastic t-test reject", testStatistic.tTest(sample1, sample2, 0.2)); Assert.assertTrue("two sample heteroscedastic t-test reject", testStatistic.tTest(sampleStats1, sampleStats2, 0.2)); Assert.assertTrue("two sample heteroscedastic t-test accept", !testStatistic.tTest(sample1, sample2, 0.1)); Assert.assertTrue("two sample heteroscedastic t-test accept", !testStatistic.tTest(sampleStats1, sampleStats2, 0.1)); try { testStatistic.tTest(sample1, sample2, .95); Assert.fail("alpha out of range, OutOfRangeException expected"); } catch (OutOfRangeException ex) { // expected } try { testStatistic.tTest(sampleStats1, sampleStats2, .95); Assert.fail("alpha out of range, OutOfRangeException expected"); } catch (OutOfRangeException ex) { // expected } try { testStatistic.tTest(sample1, tooShortObs, .01); Assert.fail("insufficient data, NumberIsTooSmallException expected"); } catch (NumberIsTooSmallException ex) { // expected } try { testStatistic.tTest(sampleStats1, tooShortStats, .01); Assert.fail("insufficient data, NumberIsTooSmallException expected"); } catch (NumberIsTooSmallException ex) { // expected } try { testStatistic.tTest(sample1, tooShortObs); Assert.fail("insufficient data, NumberIsTooSmallException expected"); } catch (NumberIsTooSmallException ex) { // expected } try { testStatistic.tTest(sampleStats1, tooShortStats); Assert.fail("insufficient data, NumberIsTooSmallException expected"); } catch (NumberIsTooSmallException ex) { // expected } try { testStatistic.t(sample1, tooShortObs); Assert.fail("insufficient data, NumberIsTooSmallException expected"); } catch (NumberIsTooSmallException ex) { // expected } try { testStatistic.t(sampleStats1, tooShortStats); Assert.fail("insufficient data, NumberIsTooSmallException expected"); } catch (NumberIsTooSmallException ex) { // expected } } @Test public void testTwoSampleTHomoscedastic() { double[] sample1 ={2, 4, 6, 8, 10, 97}; double[] sample2 = {4, 6, 8, 10, 16}; SummaryStatistics sampleStats1 = new SummaryStatistics(); for (int i = 0; i < sample1.length; i++) { sampleStats1.addValue(sample1[i]); } SummaryStatistics sampleStats2 = new SummaryStatistics(); for (int i = 0; i < sample2.length; i++) { sampleStats2.addValue(sample2[i]); } // Target comparison values computed using R version 1.8.1 (Linux version) Assert.assertEquals("two sample homoscedastic t stat", 0.73096310086, testStatistic.homoscedasticT(sample1, sample2), 10E-11); Assert.assertEquals("two sample homoscedastic p value", 0.4833963785, testStatistic.homoscedasticTTest(sampleStats1, sampleStats2), 1E-10); Assert.assertTrue("two sample homoscedastic t-test reject", testStatistic.homoscedasticTTest(sample1, sample2, 0.49)); Assert.assertTrue("two sample homoscedastic t-test accept", !testStatistic.homoscedasticTTest(sample1, sample2, 0.48)); } @Test public void testSmallSamples() { double[] sample1 = {1d, 3d}; double[] sample2 = {4d, 5d}; // Target values computed using R, version 1.8.1 (linux version) Assert.assertEquals(-2.2360679775, testStatistic.t(sample1, sample2), 1E-10); Assert.assertEquals(0.198727388935, testStatistic.tTest(sample1, sample2), 1E-10); } @Test public void testPaired() { double[] sample1 = {1d, 3d, 5d, 7d}; double[] sample2 = {0d, 6d, 11d, 2d}; double[] sample3 = {5d, 7d, 8d, 10d}; // Target values computed using R, version 1.8.1 (linux version) Assert.assertEquals(-0.3133, testStatistic.pairedT(sample1, sample2), 1E-4); Assert.assertEquals(0.774544295819, testStatistic.pairedTTest(sample1, sample2), 1E-10); Assert.assertEquals(0.001208, testStatistic.pairedTTest(sample1, sample3), 1E-6); Assert.assertFalse(testStatistic.pairedTTest(sample1, sample3, .001)); Assert.assertTrue(testStatistic.pairedTTest(sample1, sample3, .002)); } } commons-math3-3.2-src/src/test/java/org/apache/commons/math3/stat/inference/GTestTest.java100644 1750 1750 24750 12126627674 30256 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.stat.inference; import org.apache.commons.math3.exception.DimensionMismatchException; import org.apache.commons.math3.exception.NotPositiveException; import org.apache.commons.math3.exception.NotStrictlyPositiveException; import org.apache.commons.math3.exception.OutOfRangeException; import org.apache.commons.math3.exception.ZeroException; import org.junit.Assert; import org.junit.Test; /** * Test cases for the GTest class. * * Data for the tests are from p64-69 in: McDonald, J.H. 2009. Handbook of * Biological Statistics (2nd ed.). Sparky House Publishing, Baltimore, * Maryland. * */ public class GTestTest { protected GTest testStatistic = new GTest(); @Test public void testGTestGoodnesOfFit1() throws Exception { final double[] exp = new double[]{ 3d, 1d }; final long[] obs = new long[]{ 423, 133 }; Assert.assertEquals("G test statistic", 0.348721, testStatistic.g(exp, obs), 1E-6); final double p_gtgf = testStatistic.gTest(exp, obs); Assert.assertEquals("g-Test p-value", 0.55483, p_gtgf, 1E-5); Assert.assertFalse(testStatistic.gTest(exp, obs, 0.05)); } @Test public void testGTestGoodnesOfFit2() throws Exception { final double[] exp = new double[]{ 0.54d, 0.40d, 0.05d, 0.01d }; final long[] obs = new long[]{ 70, 79, 3, 4 }; Assert.assertEquals("G test statistic", 13.144799, testStatistic.g(exp, obs), 1E-6); final double p_gtgf = testStatistic.gTest(exp, obs); Assert.assertEquals("g-Test p-value", 0.004333, p_gtgf, 1E-5); Assert.assertTrue(testStatistic.gTest(exp, obs, 0.05)); } @Test public void testGTestGoodnesOfFit3() throws Exception { final double[] exp = new double[]{ 0.167d, 0.483d, 0.350d }; final long[] obs = new long[]{ 14, 21, 25 }; Assert.assertEquals("G test statistic", 4.5554, testStatistic.g(exp, obs), 1E-4); // Intrinisic (Hardy-Weinberg proportions) P-Value should be 0.033 final double p_gtgf = testStatistic.gTestIntrinsic(exp, obs); Assert.assertEquals("g-Test p-value", 0.0328, p_gtgf, 1E-4); Assert.assertFalse(testStatistic.gTest(exp, obs, 0.05)); } @Test public void testGTestIndependance1() throws Exception { final long[] obs1 = new long[]{ 268, 199, 42 }; final long[] obs2 = new long[]{ 807, 759, 184 }; final double g = testStatistic.gDataSetsComparison(obs1, obs2); Assert.assertEquals("G test statistic", 7.3008170, g, 1E-6); final double p_gti = testStatistic.gTestDataSetsComparison(obs1, obs2); Assert.assertEquals("g-Test p-value", 0.0259805, p_gti, 1E-6); Assert.assertTrue(testStatistic.gTestDataSetsComparison(obs1, obs2, 0.05)); } @Test public void testGTestIndependance2() throws Exception { final long[] obs1 = new long[]{ 127, 99, 264 }; final long[] obs2 = new long[]{ 116, 67, 161 }; final double g = testStatistic.gDataSetsComparison(obs1, obs2); Assert.assertEquals("G test statistic", 6.227288, g, 1E-6); final double p_gti = testStatistic.gTestDataSetsComparison(obs1, obs2); Assert.assertEquals("g-Test p-value", 0.04443, p_gti, 1E-5); Assert.assertTrue(testStatistic.gTestDataSetsComparison(obs1, obs2, 0.05)); } @Test public void testGTestIndependance3() throws Exception { final long[] obs1 = new long[]{ 190, 149 }; final long[] obs2 = new long[]{ 42, 49 }; final double g = testStatistic.gDataSetsComparison(obs1, obs2); Assert.assertEquals("G test statistic", 2.8187, g, 1E-4); final double p_gti = testStatistic.gTestDataSetsComparison(obs1, obs2); Assert.assertEquals("g-Test p-value", 0.09317325, p_gti, 1E-6); Assert.assertFalse(testStatistic.gTestDataSetsComparison(obs1, obs2, 0.05)); } @Test public void testGTestSetsComparisonBadCounts() { long[] observed1 = {10, -1, 12, 10, 15}; long[] observed2 = {15, 10, 10, 15, 5}; try { testStatistic.gTestDataSetsComparison( observed1, observed2); Assert.fail("Expecting NotPositiveException - negative count"); } catch (NotPositiveException ex) { // expected } long[] observed3 = {10, 0, 12, 10, 15}; long[] observed4 = {15, 0, 10, 15, 5}; try { testStatistic.gTestDataSetsComparison( observed3, observed4); Assert.fail("Expecting ZeroException - double 0's"); } catch (ZeroException ex) { // expected } long[] observed5 = {10, 10, 12, 10, 15}; long[] observed6 = {0, 0, 0, 0, 0}; try { testStatistic.gTestDataSetsComparison( observed5, observed6); Assert.fail("Expecting ZeroException - vanishing counts"); } catch (ZeroException ex) { // expected } } @Test public void testUnmatchedArrays() { final long[] observed = { 0, 1, 2, 3 }; final double[] expected = { 1, 1, 2 }; final long[] observed2 = {3, 4}; try { testStatistic.gTest(expected, observed); Assert.fail("arrays have different lengths, DimensionMismatchException expected"); } catch (DimensionMismatchException ex) { // expected } try { testStatistic.gTestDataSetsComparison(observed, observed2); Assert.fail("arrays have different lengths, DimensionMismatchException expected"); } catch (DimensionMismatchException ex) { // expected } } @Test public void testNegativeObservedCounts() { final long[] observed = { 0, 1, 2, -3 }; final double[] expected = { 1, 1, 2, 3}; final long[] observed2 = {3, 4, 5, 0}; try { testStatistic.gTest(expected, observed); Assert.fail("negative observed count, NotPositiveException expected"); } catch (NotPositiveException ex) { // expected } try { testStatistic.gTestDataSetsComparison(observed, observed2); Assert.fail("negative observed count, NotPositiveException expected"); } catch (NotPositiveException ex) { // expected } } @Test public void testZeroExpectedCounts() { final long[] observed = { 0, 1, 2, -3 }; final double[] expected = { 1, 0, 2, 3}; try { testStatistic.gTest(expected, observed); Assert.fail("zero expected count, NotStrictlyPositiveException expected"); } catch (NotStrictlyPositiveException ex) { // expected } } @Test public void testBadAlpha() { final long[] observed = { 0, 1, 2, 3 }; final double[] expected = { 1, 2, 2, 3}; final long[] observed2 = { 0, 2, 2, 3 }; try { testStatistic.gTest(expected, observed, 0.8); Assert.fail("zero expected count, NotStrictlyPositiveException expected"); } catch (OutOfRangeException ex) { // expected } try { testStatistic.gTestDataSetsComparison(observed, observed2, -0.5); Assert.fail("zero expected count, NotStrictlyPositiveException expected"); } catch (OutOfRangeException ex) { // expected } } @Test public void testScaling() { final long[] observed = {9, 11, 10, 8, 12}; final double[] expected1 = {10, 10, 10, 10, 10}; final double[] expected2 = {1000, 1000, 1000, 1000, 1000}; final double[] expected3 = {1, 1, 1, 1, 1}; final double tol = 1E-15; Assert.assertEquals( testStatistic.gTest(expected1, observed), testStatistic.gTest(expected2, observed), tol); Assert.assertEquals( testStatistic.gTest(expected1, observed), testStatistic.gTest(expected3, observed), tol); } @Test public void testRootLogLikelihood() { // positive where k11 is bigger than expected. Assert.assertTrue(testStatistic.rootLogLikelihoodRatio(904, 21060, 1144, 283012) > 0.0); // negative because k11 is lower than expected Assert.assertTrue(testStatistic.rootLogLikelihoodRatio(36, 21928, 60280, 623876) < 0.0); Assert.assertEquals(Math.sqrt(2.772589), testStatistic.rootLogLikelihoodRatio(1, 0, 0, 1), 0.000001); Assert.assertEquals(-Math.sqrt(2.772589), testStatistic.rootLogLikelihoodRatio(0, 1, 1, 0), 0.000001); Assert.assertEquals(Math.sqrt(27.72589), testStatistic.rootLogLikelihoodRatio(10, 0, 0, 10), 0.00001); Assert.assertEquals(Math.sqrt(39.33052), testStatistic.rootLogLikelihoodRatio(5, 1995, 0, 100000), 0.00001); Assert.assertEquals(-Math.sqrt(39.33052), testStatistic.rootLogLikelihoodRatio(0, 100000, 5, 1995), 0.00001); Assert.assertEquals(Math.sqrt(4730.737), testStatistic.rootLogLikelihoodRatio(1000, 1995, 1000, 100000), 0.001); Assert.assertEquals(-Math.sqrt(4730.737), testStatistic.rootLogLikelihoodRatio(1000, 100000, 1000, 1995), 0.001); Assert.assertEquals(Math.sqrt(5734.343), testStatistic.rootLogLikelihoodRatio(1000, 1000, 1000, 100000), 0.001); Assert.assertEquals(Math.sqrt(5714.932), testStatistic.rootLogLikelihoodRatio(1000, 1000, 1000, 99000), 0.001); } } ././@LongLink100644 0 0 146 12126630646 10262 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/stat/inference/MannWhitneyUTestTest.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/stat/inference/MannWhitneyUTestTest.jav100644 1750 1750 7612 12126627674 32273 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.stat.inference; import org.apache.commons.math3.exception.NoDataException; import org.apache.commons.math3.exception.NullArgumentException; import org.junit.Assert; import org.junit.Test; /** * Test cases for the MannWhitneyUTestImpl class. * * @version $Id: MannWhitneyUTestTest.java 1364030 2012-07-21 01:10:04Z erans $ */ public class MannWhitneyUTestTest { protected MannWhitneyUTest testStatistic = new MannWhitneyUTest(); @Test public void testMannWhitneyUSimple() { /* Target values computed using R version 2.11.1 * x <- c(19, 22, 16, 29, 24) * y <- c(20, 11, 17, 12) * wilcox.test(x, y, alternative = "two.sided", mu = 0, paired = FALSE, exact = FALSE, correct = FALSE) * W = 17, p-value = 0.08641 */ final double x[] = {19, 22, 16, 29, 24}; final double y[] = {20, 11, 17, 12}; Assert.assertEquals(17, testStatistic.mannWhitneyU(x, y), 1e-10); Assert.assertEquals(0.08641, testStatistic.mannWhitneyUTest(x, y), 1e-5); } @Test public void testMannWhitneyUInputValidation() { /* Samples must be present, i.e. length > 0 */ try { testStatistic.mannWhitneyUTest(new double[] { }, new double[] { 1.0 }); Assert.fail("x does not contain samples (exact), NoDataException expected"); } catch (NoDataException ex) { // expected } try { testStatistic.mannWhitneyUTest(new double[] { 1.0 }, new double[] { }); Assert.fail("y does not contain samples (exact), NoDataException expected"); } catch (NoDataException ex) { // expected } /* * x and y is null */ try { testStatistic.mannWhitneyUTest(null, null); Assert.fail("x and y is null (exact), NullArgumentException expected"); } catch (NullArgumentException ex) { // expected } try { testStatistic.mannWhitneyUTest(null, null); Assert.fail("x and y is null (asymptotic), NullArgumentException expected"); } catch (NullArgumentException ex) { // expected } /* * x or y is null */ try { testStatistic.mannWhitneyUTest(null, new double[] { 1.0 }); Assert.fail("x is null (exact), NullArgumentException expected"); } catch (NullArgumentException ex) { // expected } try { testStatistic.mannWhitneyUTest(new double[] { 1.0 }, null); Assert.fail("y is null (exact), NullArgumentException expected"); } catch (NullArgumentException ex) { // expected } } @Test public void testBigDataSet() { double[] d1 = new double[1500]; double[] d2 = new double[1500]; for (int i = 0; i < 1500; i++) { d1[i] = 2 * i; d2[i] = 2 * i + 1; } double result = testStatistic.mannWhitneyUTest(d1, d2); Assert.assertTrue(result > 0.1); } } commons-math3-3.2-src/src/test/java/org/apache/commons/math3/stat/inference/OneWayAnovaTest.java100644 1750 1750 12574 12126627674 31420 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.stat.inference; import java.util.ArrayList; import java.util.List; import org.apache.commons.math3.exception.MathIllegalArgumentException; import org.apache.commons.math3.stat.descriptive.SummaryStatistics; import org.junit.Assert; import org.junit.Test; /** * Test cases for the OneWayAnovaImpl class. * * @version $Id: OneWayAnovaTest.java 1456958 2013-03-15 13:55:27Z luc $ */ public class OneWayAnovaTest { protected OneWayAnova testStatistic = new OneWayAnova(); private double[] emptyArray = {}; private double[] classA = {93.0, 103.0, 95.0, 101.0, 91.0, 105.0, 96.0, 94.0, 101.0 }; private double[] classB = {99.0, 92.0, 102.0, 100.0, 102.0, 89.0 }; private double[] classC = {110.0, 115.0, 111.0, 117.0, 128.0, 117.0 }; @Test public void testAnovaFValue() { // Target comparison values computed using R version 2.6.0 (Linux version) List threeClasses = new ArrayList(); threeClasses.add(classA); threeClasses.add(classB); threeClasses.add(classC); Assert.assertEquals("ANOVA F-value", 24.67361709460624, testStatistic.anovaFValue(threeClasses), 1E-12); List twoClasses = new ArrayList(); twoClasses.add(classA); twoClasses.add(classB); Assert.assertEquals("ANOVA F-value", 0.0150579150579, testStatistic.anovaFValue(twoClasses), 1E-12); List emptyContents = new ArrayList(); emptyContents.add(emptyArray); emptyContents.add(classC); try { testStatistic.anovaFValue(emptyContents); Assert.fail("empty array for key classX, MathIllegalArgumentException expected"); } catch (MathIllegalArgumentException ex) { // expected } List tooFew = new ArrayList(); tooFew.add(classA); try { testStatistic.anovaFValue(tooFew); Assert.fail("less than two classes, MathIllegalArgumentException expected"); } catch (MathIllegalArgumentException ex) { // expected } } @Test public void testAnovaPValue() { // Target comparison values computed using R version 2.6.0 (Linux version) List threeClasses = new ArrayList(); threeClasses.add(classA); threeClasses.add(classB); threeClasses.add(classC); Assert.assertEquals("ANOVA P-value", 6.959446E-06, testStatistic.anovaPValue(threeClasses), 1E-12); List twoClasses = new ArrayList(); twoClasses.add(classA); twoClasses.add(classB); Assert.assertEquals("ANOVA P-value", 0.904212960464, testStatistic.anovaPValue(twoClasses), 1E-12); } @Test public void testAnovaPValueSummaryStatistics() { // Target comparison values computed using R version 2.6.0 (Linux version) List threeClasses = new ArrayList(); SummaryStatistics statsA = new SummaryStatistics(); for (double a : classA) { statsA.addValue(a); } threeClasses.add(statsA); SummaryStatistics statsB = new SummaryStatistics(); for (double b : classB) { statsB.addValue(b); } threeClasses.add(statsB); SummaryStatistics statsC = new SummaryStatistics(); for (double c : classC) { statsC.addValue(c); } threeClasses.add(statsC); Assert.assertEquals("ANOVA P-value", 6.959446E-06, testStatistic.anovaPValue(threeClasses, true), 1E-12); List twoClasses = new ArrayList(); twoClasses.add(statsA); twoClasses.add(statsB); Assert.assertEquals("ANOVA P-value", 0.904212960464, testStatistic.anovaPValue(twoClasses, false), 1E-12); } @Test public void testAnovaTest() { // Target comparison values computed using R version 2.3.1 (Linux version) List threeClasses = new ArrayList(); threeClasses.add(classA); threeClasses.add(classB); threeClasses.add(classC); Assert.assertTrue("ANOVA Test P<0.01", testStatistic.anovaTest(threeClasses, 0.01)); List twoClasses = new ArrayList(); twoClasses.add(classA); twoClasses.add(classB); Assert.assertFalse("ANOVA Test P>0.01", testStatistic.anovaTest(twoClasses, 0.01)); } } commons-math3-3.2-src/src/test/java/org/apache/commons/math3/stat/ranking/NaturalRankingTest.java100644 1750 1750 23170 12126627674 31636 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.stat.ranking; import org.junit.Assert; import org.apache.commons.math3.TestUtils; import org.apache.commons.math3.exception.NotANumberException; import org.apache.commons.math3.random.JDKRandomGenerator; import org.apache.commons.math3.random.RandomGenerator; import org.junit.Test; /** * Test cases for NaturalRanking class * * @since 2.0 * @version $Id: NaturalRankingTest.java 1454897 2013-03-10 19:02:54Z luc $ */ public class NaturalRankingTest { private final double[] exampleData = { 20, 17, 30, 42.3, 17, 50, Double.NaN, Double.NEGATIVE_INFINITY, 17 }; private final double[] tiesFirst = { 0, 0, 2, 1, 4 }; private final double[] tiesLast = { 4, 4, 1, 0 }; private final double[] multipleNaNs = { 0, 1, Double.NaN, Double.NaN }; private final double[] multipleTies = { 3, 2, 5, 5, 6, 6, 1 }; private final double[] allSame = { 0, 0, 0, 0 }; @Test public void testDefault() { // Ties averaged, NaNs failed NaturalRanking ranking = new NaturalRanking(); double[] ranks; try { ranks = ranking.rank(exampleData); Assert.fail("expected NotANumberException due to NaNStrategy.FAILED"); } catch (NotANumberException e) { // expected } ranks = ranking.rank(tiesFirst); double[] correctRanks = new double[] { 1.5, 1.5, 4, 3, 5 }; TestUtils.assertEquals(correctRanks, ranks, 0d); ranks = ranking.rank(tiesLast); correctRanks = new double[] { 3.5, 3.5, 2, 1 }; TestUtils.assertEquals(correctRanks, ranks, 0d); try { ranks = ranking.rank(multipleNaNs); Assert.fail("expected NotANumberException due to NaNStrategy.FAILED"); } catch (NotANumberException e) { // expected } ranks = ranking.rank(multipleTies); correctRanks = new double[] { 3, 2, 4.5, 4.5, 6.5, 6.5, 1 }; TestUtils.assertEquals(correctRanks, ranks, 0d); ranks = ranking.rank(allSame); correctRanks = new double[] { 2.5, 2.5, 2.5, 2.5 }; TestUtils.assertEquals(correctRanks, ranks, 0d); } @Test public void testNaNsMaximalTiesMinimum() { NaturalRanking ranking = new NaturalRanking(NaNStrategy.MAXIMAL, TiesStrategy.MINIMUM); double[] ranks = ranking.rank(exampleData); double[] correctRanks = { 5, 2, 6, 7, 2, 8, 9, 1, 2 }; TestUtils.assertEquals(correctRanks, ranks, 0d); ranks = ranking.rank(tiesFirst); correctRanks = new double[] { 1, 1, 4, 3, 5 }; TestUtils.assertEquals(correctRanks, ranks, 0d); ranks = ranking.rank(tiesLast); correctRanks = new double[] { 3, 3, 2, 1 }; TestUtils.assertEquals(correctRanks, ranks, 0d); ranks = ranking.rank(multipleNaNs); correctRanks = new double[] { 1, 2, 3, 3 }; TestUtils.assertEquals(correctRanks, ranks, 0d); ranks = ranking.rank(multipleTies); correctRanks = new double[] { 3, 2, 4, 4, 6, 6, 1 }; TestUtils.assertEquals(correctRanks, ranks, 0d); ranks = ranking.rank(allSame); correctRanks = new double[] { 1, 1, 1, 1 }; TestUtils.assertEquals(correctRanks, ranks, 0d); } @Test public void testNaNsRemovedTiesSequential() { NaturalRanking ranking = new NaturalRanking(NaNStrategy.REMOVED, TiesStrategy.SEQUENTIAL); double[] ranks = ranking.rank(exampleData); double[] correctRanks = { 5, 2, 6, 7, 3, 8, 1, 4 }; TestUtils.assertEquals(correctRanks, ranks, 0d); ranks = ranking.rank(tiesFirst); correctRanks = new double[] { 1, 2, 4, 3, 5 }; TestUtils.assertEquals(correctRanks, ranks, 0d); ranks = ranking.rank(tiesLast); correctRanks = new double[] { 3, 4, 2, 1 }; TestUtils.assertEquals(correctRanks, ranks, 0d); ranks = ranking.rank(multipleNaNs); correctRanks = new double[] { 1, 2 }; TestUtils.assertEquals(correctRanks, ranks, 0d); ranks = ranking.rank(multipleTies); correctRanks = new double[] { 3, 2, 4, 5, 6, 7, 1 }; TestUtils.assertEquals(correctRanks, ranks, 0d); ranks = ranking.rank(allSame); correctRanks = new double[] { 1, 2, 3, 4 }; TestUtils.assertEquals(correctRanks, ranks, 0d); } @Test public void testNaNsMinimalTiesMaximum() { NaturalRanking ranking = new NaturalRanking(NaNStrategy.MINIMAL, TiesStrategy.MAXIMUM); double[] ranks = ranking.rank(exampleData); double[] correctRanks = { 6, 5, 7, 8, 5, 9, 2, 2, 5 }; TestUtils.assertEquals(correctRanks, ranks, 0d); ranks = ranking.rank(tiesFirst); correctRanks = new double[] { 2, 2, 4, 3, 5 }; TestUtils.assertEquals(correctRanks, ranks, 0d); ranks = ranking.rank(tiesLast); correctRanks = new double[] { 4, 4, 2, 1 }; TestUtils.assertEquals(correctRanks, ranks, 0d); ranks = ranking.rank(multipleNaNs); correctRanks = new double[] { 3, 4, 2, 2 }; TestUtils.assertEquals(correctRanks, ranks, 0d); ranks = ranking.rank(multipleTies); correctRanks = new double[] { 3, 2, 5, 5, 7, 7, 1 }; TestUtils.assertEquals(correctRanks, ranks, 0d); ranks = ranking.rank(allSame); correctRanks = new double[] { 4, 4, 4, 4 }; TestUtils.assertEquals(correctRanks, ranks, 0d); } @Test public void testNaNsMinimalTiesAverage() { NaturalRanking ranking = new NaturalRanking(NaNStrategy.MINIMAL); double[] ranks = ranking.rank(exampleData); double[] correctRanks = { 6, 4, 7, 8, 4, 9, 1.5, 1.5, 4 }; TestUtils.assertEquals(correctRanks, ranks, 0d); ranks = ranking.rank(tiesFirst); correctRanks = new double[] { 1.5, 1.5, 4, 3, 5 }; TestUtils.assertEquals(correctRanks, ranks, 0d); ranks = ranking.rank(tiesLast); correctRanks = new double[] { 3.5, 3.5, 2, 1 }; TestUtils.assertEquals(correctRanks, ranks, 0d); ranks = ranking.rank(multipleNaNs); correctRanks = new double[] { 3, 4, 1.5, 1.5 }; TestUtils.assertEquals(correctRanks, ranks, 0d); ranks = ranking.rank(multipleTies); correctRanks = new double[] { 3, 2, 4.5, 4.5, 6.5, 6.5, 1 }; TestUtils.assertEquals(correctRanks, ranks, 0d); ranks = ranking.rank(allSame); correctRanks = new double[] { 2.5, 2.5, 2.5, 2.5 }; TestUtils.assertEquals(correctRanks, ranks, 0d); } @Test public void testNaNsFixedTiesRandom() { RandomGenerator randomGenerator = new JDKRandomGenerator(); randomGenerator.setSeed(1000); NaturalRanking ranking = new NaturalRanking(NaNStrategy.FIXED, randomGenerator); double[] ranks = ranking.rank(exampleData); double[] correctRanks = { 5, 3, 6, 7, 3, 8, Double.NaN, 1, 2 }; TestUtils.assertEquals(correctRanks, ranks, 0d); ranks = ranking.rank(tiesFirst); correctRanks = new double[] { 1, 2, 4, 3, 5 }; TestUtils.assertEquals(correctRanks, ranks, 0d); ranks = ranking.rank(tiesLast); correctRanks = new double[] { 3, 3, 2, 1 }; TestUtils.assertEquals(correctRanks, ranks, 0d); ranks = ranking.rank(multipleNaNs); correctRanks = new double[] { 1, 2, Double.NaN, Double.NaN }; TestUtils.assertEquals(correctRanks, ranks, 0d); ranks = ranking.rank(multipleTies); correctRanks = new double[] { 3, 2, 4, 4, 6, 7, 1 }; TestUtils.assertEquals(correctRanks, ranks, 0d); ranks = ranking.rank(allSame); correctRanks = new double[] { 2, 3, 3, 3 }; TestUtils.assertEquals(correctRanks, ranks, 0d); } @Test public void testNaNsAndInfs() { double[] data = { 0, Double.POSITIVE_INFINITY, Double.NaN, Double.NEGATIVE_INFINITY }; NaturalRanking ranking = new NaturalRanking(NaNStrategy.MAXIMAL); double[] ranks = ranking.rank(data); double[] correctRanks = new double[] { 2, 3.5, 3.5, 1 }; TestUtils.assertEquals(correctRanks, ranks, 0d); ranking = new NaturalRanking(NaNStrategy.MINIMAL); ranks = ranking.rank(data); correctRanks = new double[] { 3, 4, 1.5, 1.5 }; TestUtils.assertEquals(correctRanks, ranks, 0d); } @Test(expected=NotANumberException.class) public void testNaNsFailed() { double[] data = { 0, Double.POSITIVE_INFINITY, Double.NaN, Double.NEGATIVE_INFINITY }; NaturalRanking ranking = new NaturalRanking(NaNStrategy.FAILED); ranking.rank(data); } @Test public void testNoNaNsFailed() { double[] data = { 1, 2, 3, 4 }; NaturalRanking ranking = new NaturalRanking(NaNStrategy.FAILED); double[] ranks = ranking.rank(data); TestUtils.assertEquals(data, ranks, 0d); } } commons-math3-3.2-src/src/test/java/org/apache/commons/math3/stat/CertifiedDataTest.java100644 1750 1750 13035 12126627674 27754 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.stat; import java.io.BufferedReader; import java.io.InputStream; import java.io.InputStreamReader; import org.apache.commons.math3.stat.descriptive.DescriptiveStatistics; import org.apache.commons.math3.stat.descriptive.SummaryStatistics; import org.junit.Assert; import org.junit.Test; /** * Certified data test cases. * @version $Id: CertifiedDataTest.java 1244107 2012-02-14 16:17:55Z erans $ */ public class CertifiedDataTest { protected double mean = Double.NaN; protected double std = Double.NaN; /** * Test SummaryStatistics - implementations that do not store the data * and use single pass algorithms to compute statistics */ @Test public void testSummaryStatistics() throws Exception { SummaryStatistics u = new SummaryStatistics(); loadStats("data/PiDigits.txt", u); Assert.assertEquals("PiDigits: std", std, u.getStandardDeviation(), 1E-13); Assert.assertEquals("PiDigits: mean", mean, u.getMean(), 1E-13); loadStats("data/Mavro.txt", u); Assert.assertEquals("Mavro: std", std, u.getStandardDeviation(), 1E-14); Assert.assertEquals("Mavro: mean", mean, u.getMean(), 1E-14); loadStats("data/Michelso.txt", u); Assert.assertEquals("Michelso: std", std, u.getStandardDeviation(), 1E-13); Assert.assertEquals("Michelso: mean", mean, u.getMean(), 1E-13); loadStats("data/NumAcc1.txt", u); Assert.assertEquals("NumAcc1: std", std, u.getStandardDeviation(), 1E-14); Assert.assertEquals("NumAcc1: mean", mean, u.getMean(), 1E-14); loadStats("data/NumAcc2.txt", u); Assert.assertEquals("NumAcc2: std", std, u.getStandardDeviation(), 1E-14); Assert.assertEquals("NumAcc2: mean", mean, u.getMean(), 1E-14); } /** * Test DescriptiveStatistics - implementations that store full array of * values and execute multi-pass algorithms */ @Test public void testDescriptiveStatistics() throws Exception { DescriptiveStatistics u = new DescriptiveStatistics(); loadStats("data/PiDigits.txt", u); Assert.assertEquals("PiDigits: std", std, u.getStandardDeviation(), 1E-14); Assert.assertEquals("PiDigits: mean", mean, u.getMean(), 1E-14); loadStats("data/Mavro.txt", u); Assert.assertEquals("Mavro: std", std, u.getStandardDeviation(), 1E-14); Assert.assertEquals("Mavro: mean", mean, u.getMean(), 1E-14); loadStats("data/Michelso.txt", u); Assert.assertEquals("Michelso: std", std, u.getStandardDeviation(), 1E-14); Assert.assertEquals("Michelso: mean", mean, u.getMean(), 1E-14); loadStats("data/NumAcc1.txt", u); Assert.assertEquals("NumAcc1: std", std, u.getStandardDeviation(), 1E-14); Assert.assertEquals("NumAcc1: mean", mean, u.getMean(), 1E-14); loadStats("data/NumAcc2.txt", u); Assert.assertEquals("NumAcc2: std", std, u.getStandardDeviation(), 1E-14); Assert.assertEquals("NumAcc2: mean", mean, u.getMean(), 1E-14); } /** * loads a DescriptiveStatistics off of a test file * @param file * @param statistical summary */ private void loadStats(String resource, Object u) throws Exception { DescriptiveStatistics d = null; SummaryStatistics s = null; if (u instanceof DescriptiveStatistics) { d = (DescriptiveStatistics) u; } else { s = (SummaryStatistics) u; } u.getClass().getDeclaredMethod( "clear", new Class[]{}).invoke(u, new Object[]{}); mean = Double.NaN; std = Double.NaN; InputStream resourceAsStream = CertifiedDataTest.class.getResourceAsStream(resource); Assert.assertNotNull("Could not find resource "+resource,resourceAsStream); BufferedReader in = new BufferedReader( new InputStreamReader( resourceAsStream)); String line = null; for (int j = 0; j < 60; j++) { line = in.readLine(); if (j == 40) { mean = Double.parseDouble( line.substring(line.lastIndexOf(":") + 1).trim()); } if (j == 41) { std = Double.parseDouble( line.substring(line.lastIndexOf(":") + 1).trim()); } } line = in.readLine(); while (line != null) { if (d != null) { d.addValue(Double.parseDouble(line.trim())); } else { s.addValue(Double.parseDouble(line.trim())); } line = in.readLine(); } resourceAsStream.close(); in.close(); } } ././@LongLink100644 0 0 156 12126630646 10263 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/stat/clustering/KMeansPlusPlusClustererTest.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/stat/clustering/KMeansPlusPlusClusterer100644 1750 1750 26454 12126627674 32453 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.stat.clustering; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; import java.util.List; import java.util.Random; import org.apache.commons.math3.exception.NumberIsTooSmallException; import org.junit.Assert; import org.junit.Test; public class KMeansPlusPlusClustererTest { @Test public void dimension2() { KMeansPlusPlusClusterer transformer = new KMeansPlusPlusClusterer(new Random(1746432956321l)); EuclideanIntegerPoint[] points = new EuclideanIntegerPoint[] { // first expected cluster new EuclideanIntegerPoint(new int[] { -15, 3 }), new EuclideanIntegerPoint(new int[] { -15, 4 }), new EuclideanIntegerPoint(new int[] { -15, 5 }), new EuclideanIntegerPoint(new int[] { -14, 3 }), new EuclideanIntegerPoint(new int[] { -14, 5 }), new EuclideanIntegerPoint(new int[] { -13, 3 }), new EuclideanIntegerPoint(new int[] { -13, 4 }), new EuclideanIntegerPoint(new int[] { -13, 5 }), // second expected cluster new EuclideanIntegerPoint(new int[] { -1, 0 }), new EuclideanIntegerPoint(new int[] { -1, -1 }), new EuclideanIntegerPoint(new int[] { 0, -1 }), new EuclideanIntegerPoint(new int[] { 1, -1 }), new EuclideanIntegerPoint(new int[] { 1, -2 }), // third expected cluster new EuclideanIntegerPoint(new int[] { 13, 3 }), new EuclideanIntegerPoint(new int[] { 13, 4 }), new EuclideanIntegerPoint(new int[] { 14, 4 }), new EuclideanIntegerPoint(new int[] { 14, 7 }), new EuclideanIntegerPoint(new int[] { 16, 5 }), new EuclideanIntegerPoint(new int[] { 16, 6 }), new EuclideanIntegerPoint(new int[] { 17, 4 }), new EuclideanIntegerPoint(new int[] { 17, 7 }) }; List> clusters = transformer.cluster(Arrays.asList(points), 3, 5, 10); Assert.assertEquals(3, clusters.size()); boolean cluster1Found = false; boolean cluster2Found = false; boolean cluster3Found = false; for (Cluster cluster : clusters) { int[] center = cluster.getCenter().getPoint(); if (center[0] < 0) { cluster1Found = true; Assert.assertEquals(8, cluster.getPoints().size()); Assert.assertEquals(-14, center[0]); Assert.assertEquals( 4, center[1]); } else if (center[1] < 0) { cluster2Found = true; Assert.assertEquals(5, cluster.getPoints().size()); Assert.assertEquals( 0, center[0]); Assert.assertEquals(-1, center[1]); } else { cluster3Found = true; Assert.assertEquals(8, cluster.getPoints().size()); Assert.assertEquals(15, center[0]); Assert.assertEquals(5, center[1]); } } Assert.assertTrue(cluster1Found); Assert.assertTrue(cluster2Found); Assert.assertTrue(cluster3Found); } /** * JIRA: MATH-305 * * Two points, one cluster, one iteration */ @Test public void testPerformClusterAnalysisDegenerate() { KMeansPlusPlusClusterer transformer = new KMeansPlusPlusClusterer( new Random(1746432956321l)); EuclideanIntegerPoint[] points = new EuclideanIntegerPoint[] { new EuclideanIntegerPoint(new int[] { 1959, 325100 }), new EuclideanIntegerPoint(new int[] { 1960, 373200 }), }; List> clusters = transformer.cluster(Arrays.asList(points), 1, 1); Assert.assertEquals(1, clusters.size()); Assert.assertEquals(2, (clusters.get(0).getPoints().size())); EuclideanIntegerPoint pt1 = new EuclideanIntegerPoint(new int[] { 1959, 325100 }); EuclideanIntegerPoint pt2 = new EuclideanIntegerPoint(new int[] { 1960, 373200 }); Assert.assertTrue(clusters.get(0).getPoints().contains(pt1)); Assert.assertTrue(clusters.get(0).getPoints().contains(pt2)); } @Test public void testCertainSpace() { KMeansPlusPlusClusterer.EmptyClusterStrategy[] strategies = { KMeansPlusPlusClusterer.EmptyClusterStrategy.LARGEST_VARIANCE, KMeansPlusPlusClusterer.EmptyClusterStrategy.LARGEST_POINTS_NUMBER, KMeansPlusPlusClusterer.EmptyClusterStrategy.FARTHEST_POINT }; for (KMeansPlusPlusClusterer.EmptyClusterStrategy strategy : strategies) { KMeansPlusPlusClusterer transformer = new KMeansPlusPlusClusterer(new Random(1746432956321l), strategy); int numberOfVariables = 27; // initialise testvalues int position1 = 1; int position2 = position1 + numberOfVariables; int position3 = position2 + numberOfVariables; int position4 = position3 + numberOfVariables; // testvalues will be multiplied int multiplier = 1000000; EuclideanIntegerPoint[] breakingPoints = new EuclideanIntegerPoint[numberOfVariables]; // define the space which will break the cluster algorithm for (int i = 0; i < numberOfVariables; i++) { int points[] = { position1, position2, position3, position4 }; // multiply the values for (int j = 0; j < points.length; j++) { points[j] = points[j] * multiplier; } EuclideanIntegerPoint euclideanIntegerPoint = new EuclideanIntegerPoint(points); breakingPoints[i] = euclideanIntegerPoint; position1 = position1 + numberOfVariables; position2 = position2 + numberOfVariables; position3 = position3 + numberOfVariables; position4 = position4 + numberOfVariables; } for (int n = 2; n < 27; ++n) { List> clusters = transformer.cluster(Arrays.asList(breakingPoints), n, 100); Assert.assertEquals(n, clusters.size()); int sum = 0; for (Cluster cluster : clusters) { sum += cluster.getPoints().size(); } Assert.assertEquals(numberOfVariables, sum); } } } /** * A helper class for testSmallDistances(). This class is similar to EuclideanIntegerPoint, but * it defines a different distanceFrom() method that tends to return distances less than 1. */ private class CloseIntegerPoint implements Clusterable { public CloseIntegerPoint(EuclideanIntegerPoint point) { euclideanPoint = point; } public double distanceFrom(CloseIntegerPoint p) { return euclideanPoint.distanceFrom(p.euclideanPoint) * 0.001; } public CloseIntegerPoint centroidOf(Collection p) { Collection euclideanPoints = new ArrayList(); for (CloseIntegerPoint point : p) { euclideanPoints.add(point.euclideanPoint); } return new CloseIntegerPoint(euclideanPoint.centroidOf(euclideanPoints)); } @Override public boolean equals(Object o) { if (!(o instanceof CloseIntegerPoint)) { return false; } CloseIntegerPoint p = (CloseIntegerPoint) o; return euclideanPoint.equals(p.euclideanPoint); } @Override public int hashCode() { return euclideanPoint.hashCode(); } private EuclideanIntegerPoint euclideanPoint; } /** * Test points that are very close together. See issue MATH-546. */ @Test public void testSmallDistances() { // Create a bunch of CloseIntegerPoints. Most are identical, but one is different by a // small distance. int[] repeatedArray = { 0 }; int[] uniqueArray = { 1 }; CloseIntegerPoint repeatedPoint = new CloseIntegerPoint(new EuclideanIntegerPoint(repeatedArray)); CloseIntegerPoint uniquePoint = new CloseIntegerPoint(new EuclideanIntegerPoint(uniqueArray)); Collection points = new ArrayList(); final int NUM_REPEATED_POINTS = 10 * 1000; for (int i = 0; i < NUM_REPEATED_POINTS; ++i) { points.add(repeatedPoint); } points.add(uniquePoint); // Ask a KMeansPlusPlusClusterer to run zero iterations (i.e., to simply choose initial // cluster centers). final long RANDOM_SEED = 0; final int NUM_CLUSTERS = 2; final int NUM_ITERATIONS = 0; KMeansPlusPlusClusterer clusterer = new KMeansPlusPlusClusterer(new Random(RANDOM_SEED)); List> clusters = clusterer.cluster(points, NUM_CLUSTERS, NUM_ITERATIONS); // Check that one of the chosen centers is the unique point. boolean uniquePointIsCenter = false; for (Cluster cluster : clusters) { if (cluster.getCenter().equals(uniquePoint)) { uniquePointIsCenter = true; } } Assert.assertTrue(uniquePointIsCenter); } /** * 2 variables cannot be clustered into 3 clusters. See issue MATH-436. */ @Test(expected=NumberIsTooSmallException.class) public void testPerformClusterAnalysisToManyClusters() { KMeansPlusPlusClusterer transformer = new KMeansPlusPlusClusterer( new Random(1746432956321l)); EuclideanIntegerPoint[] points = new EuclideanIntegerPoint[] { new EuclideanIntegerPoint(new int[] { 1959, 325100 }), new EuclideanIntegerPoint(new int[] { 1960, 373200 }) }; transformer.cluster(Arrays.asList(points), 3, 1); } } ././@LongLink100644 0 0 153 12126630646 10260 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/stat/clustering/EuclideanDoublePointTest.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/stat/clustering/EuclideanDoublePointTes100644 1750 1750 5067 12126627674 32363 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.stat.clustering; import java.util.ArrayList; import java.util.List; import org.apache.commons.math3.TestUtils; import org.apache.commons.math3.util.FastMath; import org.junit.Assert; import org.junit.Test; public class EuclideanDoublePointTest { @Test public void testArrayIsReference() { final double[] array = { -3.0, -2.0, -1.0, 0.0, 1.0 }; Assert.assertArrayEquals(array, new EuclideanDoublePoint(array).getPoint(), 1.0e-15); } @Test public void testDistance() { final EuclideanDoublePoint e1 = new EuclideanDoublePoint(new double[] { -3.0, -2.0, -1.0, 0.0, 1.0 }); final EuclideanDoublePoint e2 = new EuclideanDoublePoint(new double[] { 1.0, 0.0, -1.0, 1.0, 1.0 }); Assert.assertEquals(FastMath.sqrt(21.0), e1.distanceFrom(e2), 1.0e-15); Assert.assertEquals(0.0, e1.distanceFrom(e1), 1.0e-15); Assert.assertEquals(0.0, e2.distanceFrom(e2), 1.0e-15); } @Test public void testCentroid() { final List list = new ArrayList(); list.add(new EuclideanDoublePoint(new double[] { 1.0, 3.0 })); list.add(new EuclideanDoublePoint(new double[] { 2.0, 2.0 })); list.add(new EuclideanDoublePoint(new double[] { 3.0, 3.0 })); list.add(new EuclideanDoublePoint(new double[] { 2.0, 4.0 })); final EuclideanDoublePoint c = list.get(0).centroidOf(list); Assert.assertEquals(2.0, c.getPoint()[0], 1.0e-15); Assert.assertEquals(3.0, c.getPoint()[1], 1.0e-15); } @Test public void testSerial() { final EuclideanDoublePoint p = new EuclideanDoublePoint(new double[] { -3.0, -2.0, -1.0, 0.0, 1.0 }); Assert.assertEquals(p, TestUtils.serializeAndRecover(p)); } } ././@LongLink100644 0 0 154 12126630646 10261 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/stat/clustering/EuclideanIntegerPointTest.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/stat/clustering/EuclideanIntegerPointTe100644 1750 1750 4672 12126627674 32364 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.stat.clustering; import java.util.ArrayList; import java.util.List; import org.apache.commons.math3.TestUtils; import org.apache.commons.math3.util.FastMath; import org.junit.Assert; import org.junit.Test; public class EuclideanIntegerPointTest { @Test public void testArrayIsReference() { int[] array = { -3, -2, -1, 0, 1 }; Assert.assertTrue(array == new EuclideanIntegerPoint(array).getPoint()); } @Test public void testDistance() { EuclideanIntegerPoint e1 = new EuclideanIntegerPoint(new int[] { -3, -2, -1, 0, 1 }); EuclideanIntegerPoint e2 = new EuclideanIntegerPoint(new int[] { 1, 0, -1, 1, 1 }); Assert.assertEquals(FastMath.sqrt(21.0), e1.distanceFrom(e2), 1.0e-15); Assert.assertEquals(0.0, e1.distanceFrom(e1), 1.0e-15); Assert.assertEquals(0.0, e2.distanceFrom(e2), 1.0e-15); } @Test public void testCentroid() { List list = new ArrayList(); list.add(new EuclideanIntegerPoint(new int[] { 1, 3 })); list.add(new EuclideanIntegerPoint(new int[] { 2, 2 })); list.add(new EuclideanIntegerPoint(new int[] { 3, 3 })); list.add(new EuclideanIntegerPoint(new int[] { 2, 4 })); EuclideanIntegerPoint c = list.get(0).centroidOf(list); Assert.assertEquals(2, c.getPoint()[0]); Assert.assertEquals(3, c.getPoint()[1]); } @Test public void testSerial() { EuclideanIntegerPoint p = new EuclideanIntegerPoint(new int[] { -3, -2, -1, 0, 1 }); Assert.assertEquals(p, TestUtils.serializeAndRecover(p)); } } ././@LongLink100644 0 0 146 12126630646 10262 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/stat/clustering/DBSCANClustererTest.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/stat/clustering/DBSCANClustererTest.jav100644 1750 1750 27434 12126627674 32135 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.stat.clustering; import java.util.Arrays; import java.util.List; import org.apache.commons.math3.exception.MathIllegalArgumentException; import org.apache.commons.math3.exception.NullArgumentException; import org.junit.Assert; import org.junit.Test; public class DBSCANClustererTest { @Test public void testCluster() { // Test data generated using: http://people.cs.nctu.edu.tw/~rsliang/dbscan/testdatagen.html final EuclideanDoublePoint[] points = new EuclideanDoublePoint[] { new EuclideanDoublePoint(new double[] { 83.08303244924173, 58.83387754182331 }), new EuclideanDoublePoint(new double[] { 45.05445510940626, 23.469642649637535 }), new EuclideanDoublePoint(new double[] { 14.96417921432294, 69.0264096390456 }), new EuclideanDoublePoint(new double[] { 73.53189604333602, 34.896145021310076 }), new EuclideanDoublePoint(new double[] { 73.28498173551634, 33.96860806993209 }), new EuclideanDoublePoint(new double[] { 73.45828098873608, 33.92584423092194 }), new EuclideanDoublePoint(new double[] { 73.9657889183145, 35.73191006924026 }), new EuclideanDoublePoint(new double[] { 74.0074097183533, 36.81735596177168 }), new EuclideanDoublePoint(new double[] { 73.41247541410848, 34.27314856695011 }), new EuclideanDoublePoint(new double[] { 73.9156256353017, 36.83206791547127 }), new EuclideanDoublePoint(new double[] { 74.81499205809087, 37.15682749846019 }), new EuclideanDoublePoint(new double[] { 74.03144880081527, 37.57399178552441 }), new EuclideanDoublePoint(new double[] { 74.51870941207744, 38.674258946906775 }), new EuclideanDoublePoint(new double[] { 74.50754595105536, 35.58903978415765 }), new EuclideanDoublePoint(new double[] { 74.51322752749547, 36.030572259100154 }), new EuclideanDoublePoint(new double[] { 59.27900996617973, 46.41091720294207 }), new EuclideanDoublePoint(new double[] { 59.73744793841615, 46.20015558367595 }), new EuclideanDoublePoint(new double[] { 58.81134076672606, 45.71150126331486 }), new EuclideanDoublePoint(new double[] { 58.52225539437495, 47.416083617601544 }), new EuclideanDoublePoint(new double[] { 58.218626647023484, 47.36228902172297 }), new EuclideanDoublePoint(new double[] { 60.27139669447206, 46.606106348801404 }), new EuclideanDoublePoint(new double[] { 60.894962462363765, 46.976924697402865 }), new EuclideanDoublePoint(new double[] { 62.29048673878424, 47.66970563563518 }), new EuclideanDoublePoint(new double[] { 61.03857608977705, 46.212924720020965 }), new EuclideanDoublePoint(new double[] { 60.16916214139201, 45.18193661351688 }), new EuclideanDoublePoint(new double[] { 59.90036905976012, 47.555364347063005 }), new EuclideanDoublePoint(new double[] { 62.33003634144552, 47.83941489877179 }), new EuclideanDoublePoint(new double[] { 57.86035536718555, 47.31117930193432 }), new EuclideanDoublePoint(new double[] { 58.13715479685925, 48.985960494028404 }), new EuclideanDoublePoint(new double[] { 56.131923963548616, 46.8508904252667 }), new EuclideanDoublePoint(new double[] { 55.976329887053, 47.46384037658572 }), new EuclideanDoublePoint(new double[] { 56.23245975235477, 47.940035191131756 }), new EuclideanDoublePoint(new double[] { 58.51687048212625, 46.622885352699086 }), new EuclideanDoublePoint(new double[] { 57.85411081905477, 45.95394361577928 }), new EuclideanDoublePoint(new double[] { 56.445776311447844, 45.162093662656844 }), new EuclideanDoublePoint(new double[] { 57.36691949656233, 47.50097194337286 }), new EuclideanDoublePoint(new double[] { 58.243626387557015, 46.114052729681134 }), new EuclideanDoublePoint(new double[] { 56.27224595635198, 44.799080066150054 }), new EuclideanDoublePoint(new double[] { 57.606924816500396, 46.94291057763621 }), new EuclideanDoublePoint(new double[] { 30.18714230041951, 13.877149710431695 }), new EuclideanDoublePoint(new double[] { 30.449448810657486, 13.490778346545994 }), new EuclideanDoublePoint(new double[] { 30.295018390286714, 13.264889000216499 }), new EuclideanDoublePoint(new double[] { 30.160201832884923, 11.89278262341395 }), new EuclideanDoublePoint(new double[] { 31.341509791789576, 15.282655921997502 }), new EuclideanDoublePoint(new double[] { 31.68601630325429, 14.756873246748 }), new EuclideanDoublePoint(new double[] { 29.325963742565364, 12.097849250072613 }), new EuclideanDoublePoint(new double[] { 29.54820742388256, 13.613295356975868 }), new EuclideanDoublePoint(new double[] { 28.79359608888626, 10.36352064087987 }), new EuclideanDoublePoint(new double[] { 31.01284597092308, 12.788479208014905 }), new EuclideanDoublePoint(new double[] { 27.58509216737002, 11.47570110601373 }), new EuclideanDoublePoint(new double[] { 28.593799561727792, 10.780998203903437 }), new EuclideanDoublePoint(new double[] { 31.356105766724795, 15.080316198524088 }), new EuclideanDoublePoint(new double[] { 31.25948503636755, 13.674329151166603 }), new EuclideanDoublePoint(new double[] { 32.31590076372959, 14.95261758659035 }), new EuclideanDoublePoint(new double[] { 30.460413702763617, 15.88402809202671 }), new EuclideanDoublePoint(new double[] { 32.56178203062154, 14.586076852632686 }), new EuclideanDoublePoint(new double[] { 32.76138648530468, 16.239837325178087 }), new EuclideanDoublePoint(new double[] { 30.1829453331884, 14.709592407103628 }), new EuclideanDoublePoint(new double[] { 29.55088173528202, 15.0651247180067 }), new EuclideanDoublePoint(new double[] { 29.004155302187428, 14.089665298582986 }), new EuclideanDoublePoint(new double[] { 29.339624439831823, 13.29096065578051 }), new EuclideanDoublePoint(new double[] { 30.997460327576846, 14.551914158277214 }), new EuclideanDoublePoint(new double[] { 30.66784126125276, 16.269703107886016 }) }; final DBSCANClusterer transformer = new DBSCANClusterer(2.0, 5); final List> clusters = transformer.cluster(Arrays.asList(points)); final List clusterOne = Arrays.asList(points[3], points[4], points[5], points[6], points[7], points[8], points[9], points[10], points[11], points[12], points[13], points[14]); final List clusterTwo = Arrays.asList(points[15], points[16], points[17], points[18], points[19], points[20], points[21], points[22], points[23], points[24], points[25], points[26], points[27], points[28], points[29], points[30], points[31], points[32], points[33], points[34], points[35], points[36], points[37], points[38]); final List clusterThree = Arrays.asList(points[39], points[40], points[41], points[42], points[43], points[44], points[45], points[46], points[47], points[48], points[49], points[50], points[51], points[52], points[53], points[54], points[55], points[56], points[57], points[58], points[59], points[60], points[61], points[62]); boolean cluster1Found = false; boolean cluster2Found = false; boolean cluster3Found = false; Assert.assertEquals(3, clusters.size()); for (final Cluster cluster : clusters) { if (cluster.getPoints().containsAll(clusterOne)) { cluster1Found = true; } if (cluster.getPoints().containsAll(clusterTwo)) { cluster2Found = true; } if (cluster.getPoints().containsAll(clusterThree)) { cluster3Found = true; } } Assert.assertTrue(cluster1Found); Assert.assertTrue(cluster2Found); Assert.assertTrue(cluster3Found); } @Test public void testSingleLink() { final EuclideanIntegerPoint[] points = { new EuclideanIntegerPoint(new int[] {10, 10}), // A new EuclideanIntegerPoint(new int[] {12, 9}), new EuclideanIntegerPoint(new int[] {10, 8}), new EuclideanIntegerPoint(new int[] {8, 8}), new EuclideanIntegerPoint(new int[] {8, 6}), new EuclideanIntegerPoint(new int[] {7, 7}), new EuclideanIntegerPoint(new int[] {5, 6}), // B new EuclideanIntegerPoint(new int[] {14, 8}), // C new EuclideanIntegerPoint(new int[] {7, 15}), // N - Noise, should not be present new EuclideanIntegerPoint(new int[] {17, 8}), // D - single-link connected to C should not be present }; final DBSCANClusterer clusterer = new DBSCANClusterer(3, 3); List> clusters = clusterer.cluster(Arrays.asList(points)); Assert.assertEquals(1, clusters.size()); final List clusterOne = Arrays.asList(points[0], points[1], points[2], points[3], points[4], points[5], points[6], points[7]); Assert.assertTrue(clusters.get(0).getPoints().containsAll(clusterOne)); } @Test public void testGetEps() { final DBSCANClusterer transformer = new DBSCANClusterer(2.0, 5); Assert.assertEquals(2.0, transformer.getEps(), 0.0); } @Test public void testGetMinPts() { final DBSCANClusterer transformer = new DBSCANClusterer(2.0, 5); Assert.assertEquals(5, transformer.getMinPts()); } @Test(expected = MathIllegalArgumentException.class) public void testNegativeEps() { new DBSCANClusterer(-2.0, 5); } @Test(expected = MathIllegalArgumentException.class) public void testNegativeMinPts() { new DBSCANClusterer(2.0, -5); } @Test(expected = NullArgumentException.class) public void testNullDataset() { DBSCANClusterer clusterer = new DBSCANClusterer(2.0, 5); clusterer.cluster(null); } } commons-math3-3.2-src/src/test/java/org/apache/commons/math3/stat/FrequencyTest.java100644 1750 1750 30213 12126627674 27222 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.stat; import java.io.BufferedReader; import java.io.StringReader; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import org.apache.commons.math3.TestUtils; import org.junit.Assert; import org.junit.Before; import org.junit.Test; /** * Test cases for the {@link Frequency} class. * * @version $Id: FrequencyTest.java 1400683 2012-10-21 17:33:37Z tn $ */ public final class FrequencyTest { private long oneL = 1; private long twoL = 2; private long threeL = 3; private int oneI = 1; private int twoI = 2; private int threeI=3; private double tolerance = 10E-15; private Frequency f = null; @Before public void setUp() { f = new Frequency(); } /** test freq counts */ @Test public void testCounts() { Assert.assertEquals("total count",0,f.getSumFreq()); f.addValue(oneL); f.addValue(twoL); f.addValue(1); f.addValue(oneI); Assert.assertEquals("one frequency count",3,f.getCount(1)); Assert.assertEquals("two frequency count",1,f.getCount(2)); Assert.assertEquals("three frequency count",0,f.getCount(3)); Assert.assertEquals("total count",4,f.getSumFreq()); Assert.assertEquals("zero cumulative frequency", 0, f.getCumFreq(0)); Assert.assertEquals("one cumulative frequency", 3, f.getCumFreq(1)); Assert.assertEquals("two cumulative frequency", 4, f.getCumFreq(2)); Assert.assertEquals("Integer argument cum freq",4, f.getCumFreq(Integer.valueOf(2))); Assert.assertEquals("five cumulative frequency", 4, f.getCumFreq(5)); Assert.assertEquals("foo cumulative frequency", 0, f.getCumFreq("foo")); f.clear(); Assert.assertEquals("total count",0,f.getSumFreq()); // userguide examples ------------------------------------------------------------------- f.addValue("one"); f.addValue("One"); f.addValue("oNe"); f.addValue("Z"); Assert.assertEquals("one cumulative frequency", 1 , f.getCount("one")); Assert.assertEquals("Z cumulative pct", 0.5, f.getCumPct("Z"), tolerance); Assert.assertEquals("z cumulative pct", 1.0, f.getCumPct("z"), tolerance); Assert.assertEquals("Ot cumulative pct", 0.25, f.getCumPct("Ot"), tolerance); f.clear(); f = null; Frequency f = new Frequency(); f.addValue(1); f.addValue(Integer.valueOf(1)); f.addValue(Long.valueOf(1)); f.addValue(2); f.addValue(Integer.valueOf(-1)); Assert.assertEquals("1 count", 3, f.getCount(1)); Assert.assertEquals("1 count", 3, f.getCount(Integer.valueOf(1))); Assert.assertEquals("0 cum pct", 0.2, f.getCumPct(0), tolerance); Assert.assertEquals("1 pct", 0.6, f.getPct(Integer.valueOf(1)), tolerance); Assert.assertEquals("-2 cum pct", 0, f.getCumPct(-2), tolerance); Assert.assertEquals("10 cum pct", 1, f.getCumPct(10), tolerance); f = null; f = new Frequency(String.CASE_INSENSITIVE_ORDER); f.addValue("one"); f.addValue("One"); f.addValue("oNe"); f.addValue("Z"); Assert.assertEquals("one count", 3 , f.getCount("one")); Assert.assertEquals("Z cumulative pct -- case insensitive", 1 , f.getCumPct("Z"), tolerance); Assert.assertEquals("z cumulative pct -- case insensitive", 1 , f.getCumPct("z"), tolerance); f = null; f = new Frequency(); Assert.assertEquals(0L, f.getCount('a')); Assert.assertEquals(0L, f.getCumFreq('b')); TestUtils.assertEquals(Double.NaN, f.getPct('a'), 0.0); TestUtils.assertEquals(Double.NaN, f.getCumPct('b'), 0.0); f.addValue('a'); f.addValue('b'); f.addValue('c'); f.addValue('d'); Assert.assertEquals(1L, f.getCount('a')); Assert.assertEquals(2L, f.getCumFreq('b')); Assert.assertEquals(0.25, f.getPct('a'), 0.0); Assert.assertEquals(0.5, f.getCumPct('b'), 0.0); Assert.assertEquals(1.0, f.getCumPct('e'), 0.0); } /** test pcts */ @Test public void testPcts() { f.addValue(oneL); f.addValue(twoL); f.addValue(oneI); f.addValue(twoI); f.addValue(threeL); f.addValue(threeL); f.addValue(3); f.addValue(threeI); Assert.assertEquals("one pct",0.25,f.getPct(1),tolerance); Assert.assertEquals("two pct",0.25,f.getPct(Long.valueOf(2)),tolerance); Assert.assertEquals("three pct",0.5,f.getPct(threeL),tolerance); Assert.assertEquals("five pct",0,f.getPct(5),tolerance); Assert.assertEquals("foo pct",0,f.getPct("foo"),tolerance); Assert.assertEquals("one cum pct",0.25,f.getCumPct(1),tolerance); Assert.assertEquals("two cum pct",0.50,f.getCumPct(Long.valueOf(2)),tolerance); Assert.assertEquals("Integer argument",0.50,f.getCumPct(Integer.valueOf(2)),tolerance); Assert.assertEquals("three cum pct",1.0,f.getCumPct(threeL),tolerance); Assert.assertEquals("five cum pct",1.0,f.getCumPct(5),tolerance); Assert.assertEquals("zero cum pct",0.0,f.getCumPct(0),tolerance); Assert.assertEquals("foo cum pct",0,f.getCumPct("foo"),tolerance); } /** test adding incomparable values */ @Test public void testAdd() { char aChar = 'a'; char bChar = 'b'; String aString = "a"; f.addValue(aChar); f.addValue(bChar); try { f.addValue(aString); Assert.fail("Expecting IllegalArgumentException"); } catch (IllegalArgumentException ex) { // expected } try { f.addValue(2); Assert.fail("Expecting IllegalArgumentException"); } catch (IllegalArgumentException ex) { // expected } Assert.assertEquals("a pct",0.5,f.getPct(aChar),tolerance); Assert.assertEquals("b cum pct",1.0,f.getCumPct(bChar),tolerance); Assert.assertEquals("a string pct",0.0,f.getPct(aString),tolerance); Assert.assertEquals("a string cum pct",0.0,f.getCumPct(aString),tolerance); f = new Frequency(); f.addValue("One"); try { f.addValue(new Integer("One")); Assert.fail("Expecting IllegalArgumentException"); } catch (IllegalArgumentException ex) { // expected } } /** test empty table */ @Test public void testEmptyTable() { Assert.assertEquals("freq sum, empty table", 0, f.getSumFreq()); Assert.assertEquals("count, empty table", 0, f.getCount(0)); Assert.assertEquals("count, empty table",0, f.getCount(Integer.valueOf(0))); Assert.assertEquals("cum freq, empty table", 0, f.getCumFreq(0)); Assert.assertEquals("cum freq, empty table", 0, f.getCumFreq("x")); Assert.assertTrue("pct, empty table", Double.isNaN(f.getPct(0))); Assert.assertTrue("pct, empty table", Double.isNaN(f.getPct(Integer.valueOf(0)))); Assert.assertTrue("cum pct, empty table", Double.isNaN(f.getCumPct(0))); Assert.assertTrue("cum pct, empty table", Double.isNaN(f.getCumPct(Integer.valueOf(0)))); } /** * Tests toString() */ @Test public void testToString() throws Exception { f.addValue(oneL); f.addValue(twoL); f.addValue(oneI); f.addValue(twoI); String s = f.toString(); //System.out.println(s); Assert.assertNotNull(s); BufferedReader reader = new BufferedReader(new StringReader(s)); String line = reader.readLine(); // header line Assert.assertNotNull(line); line = reader.readLine(); // one's or two's line Assert.assertNotNull(line); line = reader.readLine(); // one's or two's line Assert.assertNotNull(line); line = reader.readLine(); // no more elements Assert.assertNull(line); } @Test public void testIntegerValues() { Comparable obj1 = null; obj1 = Integer.valueOf(1); Integer int1 = Integer.valueOf(1); f.addValue(obj1); f.addValue(int1); f.addValue(2); f.addValue(Long.valueOf(2)); Assert.assertEquals("Integer 1 count", 2, f.getCount(1)); Assert.assertEquals("Integer 1 count", 2, f.getCount(Integer.valueOf(1))); Assert.assertEquals("Integer 1 count", 2, f.getCount(Long.valueOf(1))); Assert.assertEquals("Integer 1 cumPct", 0.5, f.getCumPct(1), tolerance); Assert.assertEquals("Integer 1 cumPct", 0.5, f.getCumPct(Long.valueOf(1)), tolerance); Assert.assertEquals("Integer 1 cumPct", 0.5, f.getCumPct(Integer.valueOf(1)), tolerance); Iterator it = f.valuesIterator(); while (it.hasNext()) { Assert.assertTrue(it.next() instanceof Long); } } @Test public void testSerial() { f.addValue(oneL); f.addValue(twoL); f.addValue(oneI); f.addValue(twoI); Assert.assertEquals(f, TestUtils.serializeAndRecover(f)); } @Test public void testGetUniqueCount() { Assert.assertEquals(0, f.getUniqueCount()); f.addValue(oneL); Assert.assertEquals(1, f.getUniqueCount()); f.addValue(oneL); Assert.assertEquals(1, f.getUniqueCount()); f.addValue(twoI); Assert.assertEquals(2, f.getUniqueCount()); } @Test public void testIncrement() { Assert.assertEquals(0, f.getUniqueCount()); f.incrementValue(oneL, 1); Assert.assertEquals(1, f.getCount(oneL)); f.incrementValue(oneL, 4); Assert.assertEquals(5, f.getCount(oneL)); f.incrementValue(oneL, -5); Assert.assertEquals(0, f.getCount(oneL)); } @Test public void testMerge() { Assert.assertEquals(0, f.getUniqueCount()); f.addValue(oneL); f.addValue(twoL); f.addValue(oneI); f.addValue(twoI); Assert.assertEquals(2, f.getUniqueCount()); Assert.assertEquals(2, f.getCount(oneI)); Assert.assertEquals(2, f.getCount(twoI)); Frequency g = new Frequency(); g.addValue(oneL); g.addValue(threeL); g.addValue(threeI); Assert.assertEquals(2, g.getUniqueCount()); Assert.assertEquals(1, g.getCount(oneI)); Assert.assertEquals(2, g.getCount(threeI)); f.merge(g); Assert.assertEquals(3, f.getUniqueCount()); Assert.assertEquals(3, f.getCount(oneI)); Assert.assertEquals(2, f.getCount(twoI)); Assert.assertEquals(2, f.getCount(threeI)); } @Test public void testMergeCollection() { Assert.assertEquals(0, f.getUniqueCount()); f.addValue(oneL); Assert.assertEquals(1, f.getUniqueCount()); Assert.assertEquals(1, f.getCount(oneI)); Assert.assertEquals(0, f.getCount(twoI)); Frequency g = new Frequency(); g.addValue(twoL); Frequency h = new Frequency(); h.addValue(threeL); List coll = new ArrayList(); coll.add(g); coll.add(h); f.merge(coll); Assert.assertEquals(3, f.getUniqueCount()); Assert.assertEquals(1, f.getCount(oneI)); Assert.assertEquals(1, f.getCount(twoI)); Assert.assertEquals(1, f.getCount(threeI)); } } commons-math3-3.2-src/src/test/java/org/apache/commons/math3/stat/data/LotteryTest.java100644 1750 1750 2136 12126627674 27617 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.stat.data; /** * @version $Id: LotteryTest.java 1244107 2012-02-14 16:17:55Z erans $ */ public class LotteryTest extends CertifiedDataAbstractTest { @Override protected String getResourceName() { return "org/apache/commons/math3/stat/data/Lottery.txt"; } } commons-math3-3.2-src/src/test/java/org/apache/commons/math3/stat/data/LewTest.java100644 1750 1750 2122 12126627674 26677 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.stat.data; /** * @version $Id: LewTest.java 1244107 2012-02-14 16:17:55Z erans $ */ public class LewTest extends CertifiedDataAbstractTest { @Override protected String getResourceName() { return "org/apache/commons/math3/stat/data/Lew.txt"; } } ././@LongLink100644 0 0 146 12126630646 10262 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/stat/data/CertifiedDataAbstractTest.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/stat/data/CertifiedDataAbstractTest.jav100644 1750 1750 13030 12126627674 32203 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.stat.data; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.net.URL; import java.util.HashMap; import java.util.Map; import org.apache.commons.math3.TestUtils; import org.apache.commons.math3.stat.descriptive.DescriptiveStatistics; import org.apache.commons.math3.stat.descriptive.SummaryStatistics; import org.junit.After; import org.junit.Assert; import org.junit.Before; import org.junit.Test; /** * @version $Id: CertifiedDataAbstractTest.java 1364030 2012-07-21 01:10:04Z erans $ */ public abstract class CertifiedDataAbstractTest { private DescriptiveStatistics descriptives; private SummaryStatistics summaries; private Map certifiedValues; @Before public void setUp() throws IOException { descriptives = new DescriptiveStatistics(); summaries = new SummaryStatistics(); certifiedValues = new HashMap(); loadData(); } private void loadData() throws IOException { BufferedReader in = null; try { URL resourceURL = getClass().getClassLoader().getResource(getResourceName()); in = new BufferedReader(new InputStreamReader(resourceURL.openStream())); String line = in.readLine(); while (line != null) { /* this call to StringUtils did little for the * following conditional structure */ line = line.trim(); // not empty line or comment if (!("".equals(line) || line.startsWith("#"))) { int n = line.indexOf('='); if (n == -1) { // data value double value = Double.parseDouble(line); descriptives.addValue(value); summaries.addValue(value); } else { // certified value String name = line.substring(0, n).trim(); String valueString = line.substring(n + 1).trim(); Double value = Double.valueOf(valueString); certifiedValues.put(name, value); } } line = in.readLine(); } } finally { if (in != null) { in.close(); } } } protected abstract String getResourceName(); protected double getMaximumAbsoluteError() { return 1.0e-5; } @After public void tearDown() { descriptives.clear(); descriptives = null; summaries.clear(); summaries = null; certifiedValues.clear(); certifiedValues = null; } @Test public void testCertifiedValues() { for (String name : certifiedValues.keySet()) { Double expectedValue = certifiedValues.get(name); Double summariesValue = getProperty(summaries, name); if (summariesValue != null) { TestUtils.assertEquals("summary value for " + name + " is incorrect.", summariesValue.doubleValue(), expectedValue.doubleValue(), getMaximumAbsoluteError()); } Double descriptivesValue = getProperty(descriptives, name); if (descriptivesValue != null) { TestUtils.assertEquals("descriptive value for " + name + " is incorrect.", descriptivesValue.doubleValue(), expectedValue.doubleValue(), getMaximumAbsoluteError()); } } } protected Double getProperty(Object bean, String name) { try { // Get the value of prop String prop = "get" + name.substring(0,1).toUpperCase() + name.substring(1); Method meth = bean.getClass().getMethod(prop, new Class[0]); Object property = meth.invoke(bean, new Object[0]); if (meth.getReturnType().equals(Double.TYPE)) { return (Double) property; } else if (meth.getReturnType().equals(Long.TYPE)) { return Double.valueOf(((Long) property).doubleValue()); } else { Assert.fail("wrong type: " + meth.getReturnType().getName()); } } catch (NoSuchMethodException nsme) { // ignored } catch (InvocationTargetException ite) { Assert.fail(ite.getMessage()); } catch (IllegalAccessException iae) { Assert.fail(iae.getMessage()); } return null; } } ././@LongLink100644 0 0 152 12126630646 10257 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/stat/descriptive/ListUnivariateImplTest.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/stat/descriptive/ListUnivariateImplTest100644 1750 1750 13465 12126627674 32461 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.stat.descriptive; import java.util.ArrayList; import java.util.List; import org.apache.commons.math3.TestUtils; import org.apache.commons.math3.util.FastMath; import org.junit.Assert; import org.junit.Test; /** * Test cases for the {@link ListUnivariateImpl} class. * * @version $Id: ListUnivariateImplTest.java 1364030 2012-07-21 01:10:04Z erans $ */ public final class ListUnivariateImplTest { private double one = 1; private float two = 2; private int three = 3; private double mean = 2; private double sumSq = 18; private double sum = 8; private double var = 0.666666666666666666667; private double std = FastMath.sqrt(var); private double n = 4; private double min = 1; private double max = 3; private double tolerance = 10E-15; /** test stats */ @Test public void testStats() { List externalList = new ArrayList(); DescriptiveStatistics u = new ListUnivariateImpl( externalList ); Assert.assertEquals("total count",0,u.getN(),tolerance); u.addValue(one); u.addValue(two); u.addValue(two); u.addValue(three); Assert.assertEquals("N",n,u.getN(),tolerance); Assert.assertEquals("sum",sum,u.getSum(),tolerance); Assert.assertEquals("sumsq",sumSq,u.getSumsq(),tolerance); Assert.assertEquals("var",var,u.getVariance(),tolerance); Assert.assertEquals("std",std,u.getStandardDeviation(),tolerance); Assert.assertEquals("mean",mean,u.getMean(),tolerance); Assert.assertEquals("min",min,u.getMin(),tolerance); Assert.assertEquals("max",max,u.getMax(),tolerance); u.clear(); Assert.assertEquals("total count",0,u.getN(),tolerance); } @Test public void testN0andN1Conditions() { List list = new ArrayList(); DescriptiveStatistics u = new ListUnivariateImpl( list ); Assert.assertTrue("Mean of n = 0 set should be NaN", Double.isNaN( u.getMean() ) ); Assert.assertTrue("Standard Deviation of n = 0 set should be NaN", Double.isNaN( u.getStandardDeviation() ) ); Assert.assertTrue("Variance of n = 0 set should be NaN", Double.isNaN(u.getVariance() ) ); list.add( Double.valueOf(one)); Assert.assertTrue( "Mean of n = 1 set should be value of single item n1", u.getMean() == one); Assert.assertTrue( "StdDev of n = 1 set should be zero, instead it is: " + u.getStandardDeviation(), u.getStandardDeviation() == 0); Assert.assertTrue( "Variance of n = 1 set should be zero", u.getVariance() == 0); } @Test public void testSkewAndKurtosis() { DescriptiveStatistics u = new DescriptiveStatistics(); double[] testArray = { 12.5, 12, 11.8, 14.2, 14.9, 14.5, 21, 8.2, 10.3, 11.3, 14.1, 9.9, 12.2, 12, 12.1, 11, 19.8, 11, 10, 8.8, 9, 12.3 }; for( int i = 0; i < testArray.length; i++) { u.addValue( testArray[i]); } Assert.assertEquals("mean", 12.40455, u.getMean(), 0.0001); Assert.assertEquals("variance", 10.00236, u.getVariance(), 0.0001); Assert.assertEquals("skewness", 1.437424, u.getSkewness(), 0.0001); Assert.assertEquals("kurtosis", 2.37719, u.getKurtosis(), 0.0001); } @Test public void testProductAndGeometricMean() { ListUnivariateImpl u = new ListUnivariateImpl(new ArrayList()); u.setWindowSize(10); u.addValue( 1.0 ); u.addValue( 2.0 ); u.addValue( 3.0 ); u.addValue( 4.0 ); Assert.assertEquals( "Geometric mean not expected", 2.213364, u.getGeometricMean(), 0.00001 ); // Now test rolling - StorelessDescriptiveStatistics should discount the contribution // of a discarded element for( int i = 0; i < 10; i++ ) { u.addValue( i + 2 ); } // Values should be (2,3,4,5,6,7,8,9,10,11) Assert.assertEquals( "Geometric mean not expected", 5.755931, u.getGeometricMean(), 0.00001 ); } /** test stats */ @Test public void testSerialization() { DescriptiveStatistics u = new ListUnivariateImpl(); Assert.assertEquals("total count",0,u.getN(),tolerance); u.addValue(one); u.addValue(two); DescriptiveStatistics u2 = (DescriptiveStatistics)TestUtils.serializeAndRecover(u); u2.addValue(two); u2.addValue(three); Assert.assertEquals("N",n,u2.getN(),tolerance); Assert.assertEquals("sum",sum,u2.getSum(),tolerance); Assert.assertEquals("sumsq",sumSq,u2.getSumsq(),tolerance); Assert.assertEquals("var",var,u2.getVariance(),tolerance); Assert.assertEquals("std",std,u2.getStandardDeviation(),tolerance); Assert.assertEquals("mean",mean,u2.getMean(),tolerance); Assert.assertEquals("min",min,u2.getMin(),tolerance); Assert.assertEquals("max",max,u2.getMax(),tolerance); u2.clear(); Assert.assertEquals("total count",0,u2.getN(),tolerance); } } ././@LongLink100644 0 0 160 12126630646 10256 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/stat/descriptive/StatisticalSummaryValuesTest.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/stat/descriptive/StatisticalSummaryValu100644 1750 1750 6712 12126627674 32503 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.stat.descriptive; import java.util.Locale; import org.apache.commons.math3.TestUtils; import org.junit.Assert; import org.junit.Test; /** * Test cases for the {@link StatisticalSummaryValues} class. * * @version $Id: StatisticalSummaryValuesTest.java 1244107 2012-02-14 16:17:55Z erans $ */ public final class StatisticalSummaryValuesTest { @Test public void testSerialization() { StatisticalSummaryValues u = new StatisticalSummaryValues(1, 2, 3, 4, 5, 6); TestUtils.checkSerializedEquality(u); StatisticalSummaryValues t = (StatisticalSummaryValues) TestUtils.serializeAndRecover(u); verifyEquality(u, t); } @Test public void testEqualsAndHashCode() { StatisticalSummaryValues u = new StatisticalSummaryValues(1, 2, 3, 4, 5, 6); StatisticalSummaryValues t = null; Assert.assertTrue("reflexive", u.equals(u)); Assert.assertFalse("non-null compared to null", u.equals(t)); Assert.assertFalse("wrong type", u.equals(Double.valueOf(0))); t = new StatisticalSummaryValues(1, 2, 3, 4, 5, 6); Assert.assertTrue("instances with same data should be equal", t.equals(u)); Assert.assertEquals("hash code", u.hashCode(), t.hashCode()); u = new StatisticalSummaryValues(Double.NaN, 2, 3, 4, 5, 6); t = new StatisticalSummaryValues(1, Double.NaN, 3, 4, 5, 6); Assert.assertFalse("instances based on different data should be different", (u.equals(t) ||t.equals(u))); } private void verifyEquality(StatisticalSummaryValues s, StatisticalSummaryValues u) { Assert.assertEquals("N",s.getN(),u.getN()); TestUtils.assertEquals("sum",s.getSum(),u.getSum(), 0); TestUtils.assertEquals("var",s.getVariance(),u.getVariance(), 0); TestUtils.assertEquals("std",s.getStandardDeviation(),u.getStandardDeviation(), 0); TestUtils.assertEquals("mean",s.getMean(),u.getMean(), 0); TestUtils.assertEquals("min",s.getMin(),u.getMin(), 0); TestUtils.assertEquals("max",s.getMax(),u.getMax(), 0); } @Test public void testToString() { StatisticalSummaryValues u = new StatisticalSummaryValues(4.5, 16, 10, 5, 4, 45); Locale d = Locale.getDefault(); Locale.setDefault(Locale.US); Assert.assertEquals("StatisticalSummaryValues:\n" + "n: 10\n" + "min: 4.0\n" + "max: 5.0\n" + "mean: 4.5\n" + "std dev: 4.0\n" + "variance: 16.0\n" + "sum: 45.0\n", u.toString()); Locale.setDefault(d); } } ././@LongLink100644 0 0 165 12126630646 10263 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/stat/descriptive/MultivariateSummaryStatisticsTest.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/stat/descriptive/MultivariateSummarySta100644 1750 1750 31455 12126627674 32527 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.stat.descriptive; import java.util.Locale; import org.apache.commons.math3.exception.DimensionMismatchException; import org.apache.commons.math3.TestUtils; import org.apache.commons.math3.stat.descriptive.moment.Mean; import org.apache.commons.math3.util.FastMath; import org.junit.Test; import org.junit.Assert; /** * Test cases for the {@link MultivariateSummaryStatistics} class. * * @version $Id: MultivariateSummaryStatisticsTest.java 1364030 2012-07-21 01:10:04Z erans $ */ public class MultivariateSummaryStatisticsTest { protected MultivariateSummaryStatistics createMultivariateSummaryStatistics(int k, boolean isCovarianceBiasCorrected) { return new MultivariateSummaryStatistics(k, isCovarianceBiasCorrected); } @Test public void testSetterInjection() { MultivariateSummaryStatistics u = createMultivariateSummaryStatistics(2, true); u.setMeanImpl(new StorelessUnivariateStatistic[] { new sumMean(), new sumMean() }); u.addValue(new double[] { 1, 2 }); u.addValue(new double[] { 3, 4 }); Assert.assertEquals(4, u.getMean()[0], 1E-14); Assert.assertEquals(6, u.getMean()[1], 1E-14); u.clear(); u.addValue(new double[] { 1, 2 }); u.addValue(new double[] { 3, 4 }); Assert.assertEquals(4, u.getMean()[0], 1E-14); Assert.assertEquals(6, u.getMean()[1], 1E-14); u.clear(); u.setMeanImpl(new StorelessUnivariateStatistic[] { new Mean(), new Mean() }); // OK after clear u.addValue(new double[] { 1, 2 }); u.addValue(new double[] { 3, 4 }); Assert.assertEquals(2, u.getMean()[0], 1E-14); Assert.assertEquals(3, u.getMean()[1], 1E-14); Assert.assertEquals(2, u.getDimension()); } @Test public void testSetterIllegalState() { MultivariateSummaryStatistics u = createMultivariateSummaryStatistics(2, true); u.addValue(new double[] { 1, 2 }); u.addValue(new double[] { 3, 4 }); try { u.setMeanImpl(new StorelessUnivariateStatistic[] { new sumMean(), new sumMean() }); Assert.fail("Expecting IllegalStateException"); } catch (IllegalStateException ex) { // expected } } @Test public void testToString() { MultivariateSummaryStatistics stats = createMultivariateSummaryStatistics(2, true); stats.addValue(new double[] {1, 3}); stats.addValue(new double[] {2, 2}); stats.addValue(new double[] {3, 1}); Locale d = Locale.getDefault(); Locale.setDefault(Locale.US); final String suffix = System.getProperty("line.separator"); Assert.assertEquals("MultivariateSummaryStatistics:" + suffix+ "n: 3" +suffix+ "min: 1.0, 1.0" +suffix+ "max: 3.0, 3.0" +suffix+ "mean: 2.0, 2.0" +suffix+ "geometric mean: 1.817..., 1.817..." +suffix+ "sum of squares: 14.0, 14.0" +suffix+ "sum of logarithms: 1.791..., 1.791..." +suffix+ "standard deviation: 1.0, 1.0" +suffix+ "covariance: Array2DRowRealMatrix{{1.0,-1.0},{-1.0,1.0}}" +suffix, stats.toString().replaceAll("([0-9]+\\.[0-9][0-9][0-9])[0-9]+", "$1...")); Locale.setDefault(d); } @Test public void testShuffledStatistics() { // the purpose of this test is only to check the get/set methods // we are aware shuffling statistics like this is really not // something sensible to do in production ... MultivariateSummaryStatistics reference = createMultivariateSummaryStatistics(2, true); MultivariateSummaryStatistics shuffled = createMultivariateSummaryStatistics(2, true); StorelessUnivariateStatistic[] tmp = shuffled.getGeoMeanImpl(); shuffled.setGeoMeanImpl(shuffled.getMeanImpl()); shuffled.setMeanImpl(shuffled.getMaxImpl()); shuffled.setMaxImpl(shuffled.getMinImpl()); shuffled.setMinImpl(shuffled.getSumImpl()); shuffled.setSumImpl(shuffled.getSumsqImpl()); shuffled.setSumsqImpl(shuffled.getSumLogImpl()); shuffled.setSumLogImpl(tmp); for (int i = 100; i > 0; --i) { reference.addValue(new double[] {i, i}); shuffled.addValue(new double[] {i, i}); } TestUtils.assertEquals(reference.getMean(), shuffled.getGeometricMean(), 1.0e-10); TestUtils.assertEquals(reference.getMax(), shuffled.getMean(), 1.0e-10); TestUtils.assertEquals(reference.getMin(), shuffled.getMax(), 1.0e-10); TestUtils.assertEquals(reference.getSum(), shuffled.getMin(), 1.0e-10); TestUtils.assertEquals(reference.getSumSq(), shuffled.getSum(), 1.0e-10); TestUtils.assertEquals(reference.getSumLog(), shuffled.getSumSq(), 1.0e-10); TestUtils.assertEquals(reference.getGeometricMean(), shuffled.getSumLog(), 1.0e-10); } /** * Bogus mean implementation to test setter injection. * Returns the sum instead of the mean. */ static class sumMean implements StorelessUnivariateStatistic { private double sum = 0; private long n = 0; public double evaluate(double[] values, int begin, int length) { return 0; } public double evaluate(double[] values) { return 0; } public void clear() { sum = 0; n = 0; } public long getN() { return n; } public double getResult() { return sum; } public void increment(double d) { sum += d; n++; } public void incrementAll(double[] values, int start, int length) { } public void incrementAll(double[] values) { } public StorelessUnivariateStatistic copy() { return new sumMean(); } } @Test public void testDimension() { try { createMultivariateSummaryStatistics(2, true).addValue(new double[3]); Assert.fail("Expecting DimensionMismatchException"); } catch (DimensionMismatchException dme) { // expected behavior } } /** test stats */ @Test public void testStats() { MultivariateSummaryStatistics u = createMultivariateSummaryStatistics(2, true); Assert.assertEquals(0, u.getN()); u.addValue(new double[] { 1, 2 }); u.addValue(new double[] { 2, 3 }); u.addValue(new double[] { 2, 3 }); u.addValue(new double[] { 3, 4 }); Assert.assertEquals( 4, u.getN()); Assert.assertEquals( 8, u.getSum()[0], 1.0e-10); Assert.assertEquals(12, u.getSum()[1], 1.0e-10); Assert.assertEquals(18, u.getSumSq()[0], 1.0e-10); Assert.assertEquals(38, u.getSumSq()[1], 1.0e-10); Assert.assertEquals( 1, u.getMin()[0], 1.0e-10); Assert.assertEquals( 2, u.getMin()[1], 1.0e-10); Assert.assertEquals( 3, u.getMax()[0], 1.0e-10); Assert.assertEquals( 4, u.getMax()[1], 1.0e-10); Assert.assertEquals(2.4849066497880003102, u.getSumLog()[0], 1.0e-10); Assert.assertEquals( 4.276666119016055311, u.getSumLog()[1], 1.0e-10); Assert.assertEquals( 1.8612097182041991979, u.getGeometricMean()[0], 1.0e-10); Assert.assertEquals( 2.9129506302439405217, u.getGeometricMean()[1], 1.0e-10); Assert.assertEquals( 2, u.getMean()[0], 1.0e-10); Assert.assertEquals( 3, u.getMean()[1], 1.0e-10); Assert.assertEquals(FastMath.sqrt(2.0 / 3.0), u.getStandardDeviation()[0], 1.0e-10); Assert.assertEquals(FastMath.sqrt(2.0 / 3.0), u.getStandardDeviation()[1], 1.0e-10); Assert.assertEquals(2.0 / 3.0, u.getCovariance().getEntry(0, 0), 1.0e-10); Assert.assertEquals(2.0 / 3.0, u.getCovariance().getEntry(0, 1), 1.0e-10); Assert.assertEquals(2.0 / 3.0, u.getCovariance().getEntry(1, 0), 1.0e-10); Assert.assertEquals(2.0 / 3.0, u.getCovariance().getEntry(1, 1), 1.0e-10); u.clear(); Assert.assertEquals(0, u.getN()); } @Test public void testN0andN1Conditions() { MultivariateSummaryStatistics u = createMultivariateSummaryStatistics(1, true); Assert.assertTrue(Double.isNaN(u.getMean()[0])); Assert.assertTrue(Double.isNaN(u.getStandardDeviation()[0])); /* n=1 */ u.addValue(new double[] { 1 }); Assert.assertEquals(1.0, u.getMean()[0], 1.0e-10); Assert.assertEquals(1.0, u.getGeometricMean()[0], 1.0e-10); Assert.assertEquals(0.0, u.getStandardDeviation()[0], 1.0e-10); /* n=2 */ u.addValue(new double[] { 2 }); Assert.assertTrue(u.getStandardDeviation()[0] > 0); } @Test public void testNaNContracts() { MultivariateSummaryStatistics u = createMultivariateSummaryStatistics(1, true); Assert.assertTrue(Double.isNaN(u.getMean()[0])); Assert.assertTrue(Double.isNaN(u.getMin()[0])); Assert.assertTrue(Double.isNaN(u.getStandardDeviation()[0])); Assert.assertTrue(Double.isNaN(u.getGeometricMean()[0])); u.addValue(new double[] { 1.0 }); Assert.assertFalse(Double.isNaN(u.getMean()[0])); Assert.assertFalse(Double.isNaN(u.getMin()[0])); Assert.assertFalse(Double.isNaN(u.getStandardDeviation()[0])); Assert.assertFalse(Double.isNaN(u.getGeometricMean()[0])); } @Test public void testSerialization() { MultivariateSummaryStatistics u = createMultivariateSummaryStatistics(2, true); // Empty test TestUtils.checkSerializedEquality(u); MultivariateSummaryStatistics s = (MultivariateSummaryStatistics) TestUtils.serializeAndRecover(u); Assert.assertEquals(u, s); // Add some data u.addValue(new double[] { 2d, 1d }); u.addValue(new double[] { 1d, 1d }); u.addValue(new double[] { 3d, 1d }); u.addValue(new double[] { 4d, 1d }); u.addValue(new double[] { 5d, 1d }); // Test again TestUtils.checkSerializedEquality(u); s = (MultivariateSummaryStatistics) TestUtils.serializeAndRecover(u); Assert.assertEquals(u, s); } @Test public void testEqualsAndHashCode() { MultivariateSummaryStatistics u = createMultivariateSummaryStatistics(2, true); MultivariateSummaryStatistics t = null; int emptyHash = u.hashCode(); Assert.assertTrue(u.equals(u)); Assert.assertFalse(u.equals(t)); Assert.assertFalse(u.equals(Double.valueOf(0))); t = createMultivariateSummaryStatistics(2, true); Assert.assertTrue(t.equals(u)); Assert.assertTrue(u.equals(t)); Assert.assertEquals(emptyHash, t.hashCode()); // Add some data to u u.addValue(new double[] { 2d, 1d }); u.addValue(new double[] { 1d, 1d }); u.addValue(new double[] { 3d, 1d }); u.addValue(new double[] { 4d, 1d }); u.addValue(new double[] { 5d, 1d }); Assert.assertFalse(t.equals(u)); Assert.assertFalse(u.equals(t)); Assert.assertTrue(u.hashCode() != t.hashCode()); //Add data in same order to t t.addValue(new double[] { 2d, 1d }); t.addValue(new double[] { 1d, 1d }); t.addValue(new double[] { 3d, 1d }); t.addValue(new double[] { 4d, 1d }); t.addValue(new double[] { 5d, 1d }); Assert.assertTrue(t.equals(u)); Assert.assertTrue(u.equals(t)); Assert.assertEquals(u.hashCode(), t.hashCode()); // Clear and make sure summaries are indistinguishable from empty summary u.clear(); t.clear(); Assert.assertTrue(t.equals(u)); Assert.assertTrue(u.equals(t)); Assert.assertEquals(emptyHash, t.hashCode()); Assert.assertEquals(emptyHash, u.hashCode()); } } ././@LongLink100644 0 0 201 12126630646 10252 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/stat/descriptive/SynchronizedMultivariateSummaryStatisticsTest.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/stat/descriptive/SynchronizedMultivaria100644 1750 1750 2577 12126627674 32533 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with this * work for additional information regarding copyright ownership. The ASF * licenses this file to You 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. */ package org.apache.commons.math3.stat.descriptive; /** * Test cases for the {@link SynchronizedMultivariateSummaryStatisticsTest} class. * @version $Id: SynchronizedMultivariateSummaryStatisticsTest.java 1244107 2012-02-14 16:17:55Z erans $ * 2007) $ */ public final class SynchronizedMultivariateSummaryStatisticsTest extends MultivariateSummaryStatisticsTest { @Override protected MultivariateSummaryStatistics createMultivariateSummaryStatistics(int k, boolean isCovarianceBiasCorrected) { return new SynchronizedMultivariateSummaryStatistics(k, isCovarianceBiasCorrected); } } ././@LongLink100644 0 0 163 12126630646 10261 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/stat/descriptive/AbstractUnivariateStatisticTest.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/stat/descriptive/AbstractUnivariateStat100644 1750 1750 10032 12126627674 32446 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.stat.descriptive; import org.apache.commons.math3.exception.MathIllegalArgumentException; import org.apache.commons.math3.stat.descriptive.moment.Mean; import org.junit.Assert; import org.junit.Test; /** * Tests for AbstractUnivariateStatistic * * @version $Id: AbstractUnivariateStatisticTest.java 1244107 2012-02-14 16:17:55Z erans $ */ public class AbstractUnivariateStatisticTest { protected double[] testArray = {0, 1, 2, 3, 4, 5}; protected double[] testWeightsArray = {0.3, 0.2, 1.3, 1.1, 1.0, 1.8}; protected double[] testNegativeWeightsArray = {-0.3, 0.2, -1.3, 1.1, 1.0, 1.8}; protected double[] nullArray = null; protected double[] singletonArray = {0}; protected Mean testStatistic = new Mean(); @Test public void testTestPositive() { for (int j = 0; j < 6; j++) { for (int i = 1; i < (7 - j); i++) { Assert.assertTrue(testStatistic.test(testArray, 0, i)); } } Assert.assertTrue(testStatistic.test(singletonArray, 0, 1)); Assert.assertTrue(testStatistic.test(singletonArray, 0, 0, true)); } @Test public void testTestNegative() { Assert.assertFalse(testStatistic.test(singletonArray, 0, 0)); Assert.assertFalse(testStatistic.test(testArray, 0, 0)); try { testStatistic.test(singletonArray, 2, 1); // start past end Assert.fail("Expecting MathIllegalArgumentException"); } catch (MathIllegalArgumentException ex) { // expected } try { testStatistic.test(testArray, 0, 7); // end past end Assert.fail("Expecting MathIllegalArgumentException"); } catch (MathIllegalArgumentException ex) { // expected } try { testStatistic.test(testArray, -1, 1); // start negative Assert.fail("Expecting MathIllegalArgumentException"); } catch (MathIllegalArgumentException ex) { // expected } try { testStatistic.test(testArray, 0, -1); // length negative Assert.fail("Expecting MathIllegalArgumentException"); } catch (MathIllegalArgumentException ex) { // expected } try { testStatistic.test(nullArray, 0, 1); // null array Assert.fail("Expecting MathIllegalArgumentException"); } catch (MathIllegalArgumentException ex) { // expected } try { testStatistic.test(testArray, nullArray, 0, 1); // null weights array Assert.fail("Expecting MathIllegalArgumentException"); } catch (MathIllegalArgumentException ex) { // expected } try { testStatistic.test(singletonArray, testWeightsArray, 0, 1); // weights.length != value.length Assert.fail("Expecting MathIllegalArgumentException"); } catch (MathIllegalArgumentException ex) { // expected } try { testStatistic.test(testArray, testNegativeWeightsArray, 0, 6); // can't have negative weights Assert.fail("Expecting MathIllegalArgumentException"); } catch (MathIllegalArgumentException ex) { // expected } } } ././@LongLink100644 0 0 151 12126630646 10256 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/stat/descriptive/SummaryStatisticsTest.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/stat/descriptive/SummaryStatisticsTest.100644 1750 1750 30226 12126627674 32454 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.stat.descriptive; import org.apache.commons.math3.TestUtils; import org.apache.commons.math3.stat.descriptive.moment.GeometricMean; import org.apache.commons.math3.stat.descriptive.moment.Mean; import org.apache.commons.math3.stat.descriptive.moment.Variance; import org.apache.commons.math3.stat.descriptive.summary.Sum; import org.apache.commons.math3.util.FastMath; import org.junit.Assert; import org.junit.Test; /** * Test cases for the {@link SummaryStatistics} class. * * @version $Id: SummaryStatisticsTest.java 1364030 2012-07-21 01:10:04Z erans $ */ public class SummaryStatisticsTest { private double one = 1; private float twoF = 2; private long twoL = 2; private int three = 3; private double mean = 2; private double sumSq = 18; private double sum = 8; private double var = 0.666666666666666666667; private double popVar = 0.5; private double std = FastMath.sqrt(var); private double n = 4; private double min = 1; private double max = 3; private double tolerance = 10E-15; protected SummaryStatistics createSummaryStatistics() { return new SummaryStatistics(); } /** test stats */ @Test public void testStats() { SummaryStatistics u = createSummaryStatistics(); Assert.assertEquals("total count",0,u.getN(),tolerance); u.addValue(one); u.addValue(twoF); u.addValue(twoL); u.addValue(three); Assert.assertEquals("N",n,u.getN(),tolerance); Assert.assertEquals("sum",sum,u.getSum(),tolerance); Assert.assertEquals("sumsq",sumSq,u.getSumsq(),tolerance); Assert.assertEquals("var",var,u.getVariance(),tolerance); Assert.assertEquals("population var",popVar,u.getPopulationVariance(),tolerance); Assert.assertEquals("std",std,u.getStandardDeviation(),tolerance); Assert.assertEquals("mean",mean,u.getMean(),tolerance); Assert.assertEquals("min",min,u.getMin(),tolerance); Assert.assertEquals("max",max,u.getMax(),tolerance); u.clear(); Assert.assertEquals("total count",0,u.getN(),tolerance); } @Test public void testN0andN1Conditions() { SummaryStatistics u = createSummaryStatistics(); Assert.assertTrue("Mean of n = 0 set should be NaN", Double.isNaN( u.getMean() ) ); Assert.assertTrue("Standard Deviation of n = 0 set should be NaN", Double.isNaN( u.getStandardDeviation() ) ); Assert.assertTrue("Variance of n = 0 set should be NaN", Double.isNaN(u.getVariance() ) ); /* n=1 */ u.addValue(one); Assert.assertTrue("mean should be one (n = 1)", u.getMean() == one); Assert.assertTrue("geometric should be one (n = 1) instead it is " + u.getGeometricMean(), u.getGeometricMean() == one); Assert.assertTrue("Std should be zero (n = 1)", u.getStandardDeviation() == 0.0); Assert.assertTrue("variance should be zero (n = 1)", u.getVariance() == 0.0); /* n=2 */ u.addValue(twoF); Assert.assertTrue("Std should not be zero (n = 2)", u.getStandardDeviation() != 0.0); Assert.assertTrue("variance should not be zero (n = 2)", u.getVariance() != 0.0); } @Test public void testProductAndGeometricMean() { SummaryStatistics u = createSummaryStatistics(); u.addValue( 1.0 ); u.addValue( 2.0 ); u.addValue( 3.0 ); u.addValue( 4.0 ); Assert.assertEquals( "Geometric mean not expected", 2.213364, u.getGeometricMean(), 0.00001 ); } @Test public void testNaNContracts() { SummaryStatistics u = createSummaryStatistics(); Assert.assertTrue("mean not NaN",Double.isNaN(u.getMean())); Assert.assertTrue("min not NaN",Double.isNaN(u.getMin())); Assert.assertTrue("std dev not NaN",Double.isNaN(u.getStandardDeviation())); Assert.assertTrue("var not NaN",Double.isNaN(u.getVariance())); Assert.assertTrue("geom mean not NaN",Double.isNaN(u.getGeometricMean())); u.addValue(1.0); Assert.assertEquals( "mean not expected", 1.0, u.getMean(), Double.MIN_VALUE); Assert.assertEquals( "variance not expected", 0.0, u.getVariance(), Double.MIN_VALUE); Assert.assertEquals( "geometric mean not expected", 1.0, u.getGeometricMean(), Double.MIN_VALUE); u.addValue(-1.0); Assert.assertTrue("geom mean not NaN",Double.isNaN(u.getGeometricMean())); u.addValue(0.0); Assert.assertTrue("geom mean not NaN",Double.isNaN(u.getGeometricMean())); //FiXME: test all other NaN contract specs } @Test public void testGetSummary() { SummaryStatistics u = createSummaryStatistics(); StatisticalSummary summary = u.getSummary(); verifySummary(u, summary); u.addValue(1d); summary = u.getSummary(); verifySummary(u, summary); u.addValue(2d); summary = u.getSummary(); verifySummary(u, summary); u.addValue(2d); summary = u.getSummary(); verifySummary(u, summary); } @Test public void testSerialization() { SummaryStatistics u = createSummaryStatistics(); // Empty test TestUtils.checkSerializedEquality(u); SummaryStatistics s = (SummaryStatistics) TestUtils.serializeAndRecover(u); StatisticalSummary summary = s.getSummary(); verifySummary(u, summary); // Add some data u.addValue(2d); u.addValue(1d); u.addValue(3d); u.addValue(4d); u.addValue(5d); // Test again TestUtils.checkSerializedEquality(u); s = (SummaryStatistics) TestUtils.serializeAndRecover(u); summary = s.getSummary(); verifySummary(u, summary); } @Test public void testEqualsAndHashCode() { SummaryStatistics u = createSummaryStatistics(); SummaryStatistics t = null; int emptyHash = u.hashCode(); Assert.assertTrue("reflexive", u.equals(u)); Assert.assertFalse("non-null compared to null", u.equals(t)); Assert.assertFalse("wrong type", u.equals(Double.valueOf(0))); t = createSummaryStatistics(); Assert.assertTrue("empty instances should be equal", t.equals(u)); Assert.assertTrue("empty instances should be equal", u.equals(t)); Assert.assertEquals("empty hash code", emptyHash, t.hashCode()); // Add some data to u u.addValue(2d); u.addValue(1d); u.addValue(3d); u.addValue(4d); Assert.assertFalse("different n's should make instances not equal", t.equals(u)); Assert.assertFalse("different n's should make instances not equal", u.equals(t)); Assert.assertTrue("different n's should make hashcodes different", u.hashCode() != t.hashCode()); //Add data in same order to t t.addValue(2d); t.addValue(1d); t.addValue(3d); t.addValue(4d); Assert.assertTrue("summaries based on same data should be equal", t.equals(u)); Assert.assertTrue("summaries based on same data should be equal", u.equals(t)); Assert.assertEquals("summaries based on same data should have same hashcodes", u.hashCode(), t.hashCode()); // Clear and make sure summaries are indistinguishable from empty summary u.clear(); t.clear(); Assert.assertTrue("empty instances should be equal", t.equals(u)); Assert.assertTrue("empty instances should be equal", u.equals(t)); Assert.assertEquals("empty hash code", emptyHash, t.hashCode()); Assert.assertEquals("empty hash code", emptyHash, u.hashCode()); } @Test public void testCopy() { SummaryStatistics u = createSummaryStatistics(); u.addValue(2d); u.addValue(1d); u.addValue(3d); u.addValue(4d); SummaryStatistics v = new SummaryStatistics(u); Assert.assertEquals(u, v); Assert.assertEquals(v, u); // Make sure both behave the same with additional values added u.addValue(7d); u.addValue(9d); u.addValue(11d); u.addValue(23d); v.addValue(7d); v.addValue(9d); v.addValue(11d); v.addValue(23d); Assert.assertEquals(u, v); Assert.assertEquals(v, u); // Check implementation pointers are preserved u.clear(); u.setSumImpl(new Sum()); SummaryStatistics.copy(u,v); Assert.assertEquals(u.getSumImpl(), v.getSumImpl()); } private void verifySummary(SummaryStatistics u, StatisticalSummary s) { Assert.assertEquals("N",s.getN(),u.getN()); TestUtils.assertEquals("sum",s.getSum(),u.getSum(),tolerance); TestUtils.assertEquals("var",s.getVariance(),u.getVariance(),tolerance); TestUtils.assertEquals("std",s.getStandardDeviation(),u.getStandardDeviation(),tolerance); TestUtils.assertEquals("mean",s.getMean(),u.getMean(),tolerance); TestUtils.assertEquals("min",s.getMin(),u.getMin(),tolerance); TestUtils.assertEquals("max",s.getMax(),u.getMax(),tolerance); } @Test public void testSetterInjection() { SummaryStatistics u = createSummaryStatistics(); u.setMeanImpl(new Sum()); u.setSumLogImpl(new Sum()); u.addValue(1); u.addValue(3); Assert.assertEquals(4, u.getMean(), 1E-14); Assert.assertEquals(4, u.getSumOfLogs(), 1E-14); Assert.assertEquals(FastMath.exp(2), u.getGeometricMean(), 1E-14); u.clear(); u.addValue(1); u.addValue(2); Assert.assertEquals(3, u.getMean(), 1E-14); u.clear(); u.setMeanImpl(new Mean()); // OK after clear } @Test public void testSetterIllegalState() { SummaryStatistics u = createSummaryStatistics(); u.addValue(1); u.addValue(3); try { u.setMeanImpl(new Sum()); Assert.fail("Expecting IllegalStateException"); } catch (IllegalStateException ex) { // expected } } /** * JIRA: MATH-691 */ @Test public void testOverrideVarianceWithMathClass() { double[] scores = {1, 2, 3, 4}; SummaryStatistics stats = new SummaryStatistics(); stats.setVarianceImpl(new Variance(false)); //use "population variance" for(double i : scores) { stats.addValue(i); } Assert.assertEquals((new Variance(false)).evaluate(scores),stats.getVariance(), 0); } @Test public void testOverrideMeanWithMathClass() { double[] scores = {1, 2, 3, 4}; SummaryStatistics stats = new SummaryStatistics(); stats.setMeanImpl(new Mean()); for(double i : scores) { stats.addValue(i); } Assert.assertEquals((new Mean()).evaluate(scores),stats.getMean(), 0); } @Test public void testOverrideGeoMeanWithMathClass() { double[] scores = {1, 2, 3, 4}; SummaryStatistics stats = new SummaryStatistics(); stats.setGeoMeanImpl(new GeometricMean()); for(double i : scores) { stats.addValue(i); } Assert.assertEquals((new GeometricMean()).evaluate(scores),stats.getGeometricMean(), 0); } } ././@LongLink100644 0 0 163 12126630646 10261 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/stat/descriptive/UnivariateStatisticAbstractTest.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/stat/descriptive/UnivariateStatisticAbs100644 1750 1750 22323 12126627674 32452 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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 * s * 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. */ package org.apache.commons.math3.stat.descriptive; import java.util.ArrayList; import java.util.List; import org.apache.commons.math3.TestUtils; import org.apache.commons.math3.distribution.IntegerDistribution; import org.apache.commons.math3.distribution.UniformIntegerDistribution; import org.apache.commons.math3.distribution.RealDistribution; import org.apache.commons.math3.distribution.NormalDistribution; import org.apache.commons.math3.util.FastMath; import org.junit.Assert; import org.junit.Test; /** * Test cases for the {@link UnivariateStatistic} class. * @version $Id: UnivariateStatisticAbstractTest.java 1368738 2012-08-02 22:18:08Z erans $ */ public abstract class UnivariateStatisticAbstractTest { protected double mean = 12.404545454545455d; protected double geoMean = 12.070589161633011d; protected double var = 10.00235930735931d; protected double std = FastMath.sqrt(var); protected double skew = 1.437423729196190d; protected double kurt = 2.377191264804700d; protected double min = 8.2d; protected double max = 21d; protected double median = 12d; protected double percentile5 = 8.29d; protected double percentile95 = 20.82d; protected double product = 628096400563833396009676.9200400128d; protected double sumLog = 54.7969806116451507d; protected double sumSq = 3595.250d; protected double sum = 272.90d; protected double secondMoment = 210.04954545454547d; protected double thirdMoment = 868.0906859504136; protected double fourthMoment = 9244.080993773481; protected double weightedMean = 12.366995073891626d; protected double weightedVar = 9.974760968886391d; protected double weightedStd = FastMath.sqrt(weightedVar); protected double weightedProduct = 8517647448765288000000d; protected double weightedSum = 251.05d; protected double tolerance = 10E-12; protected double[] testArray = { 12.5, 12.0, 11.8, 14.2, 14.9, 14.5, 21.0, 8.2, 10.3, 11.3, 14.1, 9.9, 12.2, 12.0, 12.1, 11.0, 19.8, 11.0, 10.0, 8.8, 9.0, 12.3 }; protected double[] testWeightsArray = { 1.5, 0.8, 1.2, 0.4, 0.8, 1.8, 1.2, 1.1, 1.0, 0.7, 1.3, 0.6, 0.7, 1.3, 0.7, 1.0, 0.4, 0.1, 1.4, 0.9, 1.1, 0.3 }; protected double[] identicalWeightsArray = { 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5 }; protected double[] unitWeightsArray = { 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0 }; public abstract UnivariateStatistic getUnivariateStatistic(); public abstract double expectedValue(); public double getTolerance() { return tolerance; } @Test public void testEvaluation() { Assert.assertEquals( expectedValue(), getUnivariateStatistic().evaluate(testArray), getTolerance()); } @Test public void testEvaluateArraySegment() { final UnivariateStatistic stat = getUnivariateStatistic(); final double[] arrayZero = new double[5]; System.arraycopy(testArray, 0, arrayZero, 0, 5); Assert.assertEquals(stat.evaluate(arrayZero), stat.evaluate(testArray, 0, 5), 0); final double[] arrayOne = new double[5]; System.arraycopy(testArray, 5, arrayOne, 0, 5); Assert.assertEquals(stat.evaluate(arrayOne), stat.evaluate(testArray, 5, 5), 0); final double[] arrayEnd = new double[5]; System.arraycopy(testArray, testArray.length - 5, arrayEnd, 0, 5); Assert.assertEquals(stat.evaluate(arrayEnd), stat.evaluate(testArray, testArray.length - 5, 5), 0); } @Test public void testEvaluateArraySegmentWeighted() { // See if this statistic computes weighted statistics // If not, skip this test UnivariateStatistic statistic = getUnivariateStatistic(); if (!(statistic instanceof WeightedEvaluation)) { return; } final WeightedEvaluation stat = (WeightedEvaluation) getUnivariateStatistic(); final double[] arrayZero = new double[5]; final double[] weightZero = new double[5]; System.arraycopy(testArray, 0, arrayZero, 0, 5); System.arraycopy(testWeightsArray, 0, weightZero, 0, 5); Assert.assertEquals(stat.evaluate(arrayZero, weightZero), stat.evaluate(testArray, testWeightsArray, 0, 5), 0); final double[] arrayOne = new double[5]; final double[] weightOne = new double[5]; System.arraycopy(testArray, 5, arrayOne, 0, 5); System.arraycopy(testWeightsArray, 5, weightOne, 0, 5); Assert.assertEquals(stat.evaluate(arrayOne, weightOne), stat.evaluate(testArray, testWeightsArray, 5, 5), 0); final double[] arrayEnd = new double[5]; final double[] weightEnd = new double[5]; System.arraycopy(testArray, testArray.length - 5, arrayEnd, 0, 5); System.arraycopy(testWeightsArray, testArray.length - 5, weightEnd, 0, 5); Assert.assertEquals(stat.evaluate(arrayEnd, weightEnd), stat.evaluate(testArray, testWeightsArray, testArray.length - 5, 5), 0); } @Test public void testCopy() { UnivariateStatistic original = getUnivariateStatistic(); UnivariateStatistic copy = original.copy(); Assert.assertEquals( expectedValue(), copy.evaluate(testArray), getTolerance()); } /** * Tests consistency of weighted statistic computation. * For statistics that support weighted evaluation, this test case compares * the result of direct computation on an array with repeated values with * a weighted computation on the corresponding (shorter) array with each * value appearing only once but with a weight value equal to its multiplicity * in the repeating array. */ @Test public void testWeightedConsistency() { // See if this statistic computes weighted statistics // If not, skip this test UnivariateStatistic statistic = getUnivariateStatistic(); if (!(statistic instanceof WeightedEvaluation)) { return; } // Create arrays of values and corresponding integral weights // and longer array with values repeated according to the weights final int len = 10; // length of values array final double mu = 0; // mean of test data final double sigma = 5; // std dev of test data double[] values = new double[len]; double[] weights = new double[len]; // Fill weights array with random int values between 1 and 5 int[] intWeights = new int[len]; final IntegerDistribution weightDist = new UniformIntegerDistribution(1, 5); for (int i = 0; i < len; i++) { intWeights[i] = weightDist.sample(); weights[i] = intWeights[i]; } // Fill values array with random data from N(mu, sigma) // and fill valuesList with values from values array with // values[i] repeated weights[i] times, each i final RealDistribution valueDist = new NormalDistribution(mu, sigma); List valuesList = new ArrayList(); for (int i = 0; i < len; i++) { double value = valueDist.sample(); values[i] = value; for (int j = 0; j < intWeights[i]; j++) { valuesList.add(new Double(value)); } } // Dump valuesList into repeatedValues array int sumWeights = valuesList.size(); double[] repeatedValues = new double[sumWeights]; for (int i = 0; i < sumWeights; i++) { repeatedValues[i] = valuesList.get(i); } // Compare result of weighted statistic computation with direct computation // on array of repeated values WeightedEvaluation weightedStatistic = (WeightedEvaluation) statistic; TestUtils.assertRelativelyEquals(statistic.evaluate(repeatedValues), weightedStatistic.evaluate(values, weights, 0, values.length), 10E-12); // Check consistency of weighted evaluation methods Assert.assertEquals(weightedStatistic.evaluate(values, weights, 0, values.length), weightedStatistic.evaluate(values, weights), Double.MIN_VALUE); } } ././@LongLink100644 0 0 155 12126630646 10262 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/stat/descriptive/DescriptiveStatisticsTest.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/stat/descriptive/DescriptiveStatisticsT100644 1750 1750 32457 12126627674 32516 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with this * work for additional information regarding copyright ownership. The ASF * licenses this file to You 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. */ package org.apache.commons.math3.stat.descriptive; import java.util.Locale; import org.apache.commons.math3.TestUtils; import org.apache.commons.math3.stat.descriptive.rank.Percentile; import org.apache.commons.math3.stat.descriptive.moment.GeometricMean; import org.apache.commons.math3.stat.descriptive.moment.Mean; import org.apache.commons.math3.stat.descriptive.moment.Variance; import org.apache.commons.math3.stat.descriptive.rank.Max; import org.apache.commons.math3.stat.descriptive.rank.Min; import org.apache.commons.math3.stat.descriptive.summary.Sum; import org.apache.commons.math3.stat.descriptive.summary.SumOfSquares; import org.apache.commons.math3.util.Precision; import org.junit.Assert; import org.junit.Test; /** * Test cases for the DescriptiveStatistics class. * * @version $Id: DescriptiveStatisticsTest.java 1364030 2012-07-21 01:10:04Z erans $ * 2007) $ */ public class DescriptiveStatisticsTest { protected DescriptiveStatistics createDescriptiveStatistics() { return new DescriptiveStatistics(); } @Test public void testSetterInjection() { DescriptiveStatistics stats = createDescriptiveStatistics(); stats.addValue(1); stats.addValue(3); Assert.assertEquals(2, stats.getMean(), 1E-10); // Now lets try some new math stats.setMeanImpl(new deepMean()); Assert.assertEquals(42, stats.getMean(), 1E-10); } @Test public void testCopy() { DescriptiveStatistics stats = createDescriptiveStatistics(); stats.addValue(1); stats.addValue(3); DescriptiveStatistics copy = new DescriptiveStatistics(stats); Assert.assertEquals(2, copy.getMean(), 1E-10); // Now lets try some new math stats.setMeanImpl(new deepMean()); copy = stats.copy(); Assert.assertEquals(42, copy.getMean(), 1E-10); } @Test public void testWindowSize() { DescriptiveStatistics stats = createDescriptiveStatistics(); stats.setWindowSize(300); for (int i = 0; i < 100; ++i) { stats.addValue(i + 1); } int refSum = (100 * 101) / 2; Assert.assertEquals(refSum / 100.0, stats.getMean(), 1E-10); Assert.assertEquals(300, stats.getWindowSize()); try { stats.setWindowSize(-3); Assert.fail("an exception should have been thrown"); } catch (IllegalArgumentException iae) { // expected } Assert.assertEquals(300, stats.getWindowSize()); stats.setWindowSize(50); Assert.assertEquals(50, stats.getWindowSize()); int refSum2 = refSum - (50 * 51) / 2; Assert.assertEquals(refSum2 / 50.0, stats.getMean(), 1E-10); } @Test public void testGetValues() { DescriptiveStatistics stats = createDescriptiveStatistics(); for (int i = 100; i > 0; --i) { stats.addValue(i); } int refSum = (100 * 101) / 2; Assert.assertEquals(refSum / 100.0, stats.getMean(), 1E-10); double[] v = stats.getValues(); for (int i = 0; i < v.length; ++i) { Assert.assertEquals(100.0 - i, v[i], 1.0e-10); } double[] s = stats.getSortedValues(); for (int i = 0; i < s.length; ++i) { Assert.assertEquals(i + 1.0, s[i], 1.0e-10); } Assert.assertEquals(12.0, stats.getElement(88), 1.0e-10); } @Test public void testToString() { DescriptiveStatistics stats = createDescriptiveStatistics(); stats.addValue(1); stats.addValue(2); stats.addValue(3); Locale d = Locale.getDefault(); Locale.setDefault(Locale.US); Assert.assertEquals("DescriptiveStatistics:\n" + "n: 3\n" + "min: 1.0\n" + "max: 3.0\n" + "mean: 2.0\n" + "std dev: 1.0\n" + "median: 2.0\n" + "skewness: 0.0\n" + "kurtosis: NaN\n", stats.toString()); Locale.setDefault(d); } @Test public void testShuffledStatistics() { // the purpose of this test is only to check the get/set methods // we are aware shuffling statistics like this is really not // something sensible to do in production ... DescriptiveStatistics reference = createDescriptiveStatistics(); DescriptiveStatistics shuffled = createDescriptiveStatistics(); UnivariateStatistic tmp = shuffled.getGeometricMeanImpl(); shuffled.setGeometricMeanImpl(shuffled.getMeanImpl()); shuffled.setMeanImpl(shuffled.getKurtosisImpl()); shuffled.setKurtosisImpl(shuffled.getSkewnessImpl()); shuffled.setSkewnessImpl(shuffled.getVarianceImpl()); shuffled.setVarianceImpl(shuffled.getMaxImpl()); shuffled.setMaxImpl(shuffled.getMinImpl()); shuffled.setMinImpl(shuffled.getSumImpl()); shuffled.setSumImpl(shuffled.getSumsqImpl()); shuffled.setSumsqImpl(tmp); for (int i = 100; i > 0; --i) { reference.addValue(i); shuffled.addValue(i); } Assert.assertEquals(reference.getMean(), shuffled.getGeometricMean(), 1.0e-10); Assert.assertEquals(reference.getKurtosis(), shuffled.getMean(), 1.0e-10); Assert.assertEquals(reference.getSkewness(), shuffled.getKurtosis(), 1.0e-10); Assert.assertEquals(reference.getVariance(), shuffled.getSkewness(), 1.0e-10); Assert.assertEquals(reference.getMax(), shuffled.getVariance(), 1.0e-10); Assert.assertEquals(reference.getMin(), shuffled.getMax(), 1.0e-10); Assert.assertEquals(reference.getSum(), shuffled.getMin(), 1.0e-10); Assert.assertEquals(reference.getSumsq(), shuffled.getSum(), 1.0e-10); Assert.assertEquals(reference.getGeometricMean(), shuffled.getSumsq(), 1.0e-10); } @Test public void testPercentileSetter() { DescriptiveStatistics stats = createDescriptiveStatistics(); stats.addValue(1); stats.addValue(2); stats.addValue(3); Assert.assertEquals(2, stats.getPercentile(50.0), 1E-10); // Inject wrapped Percentile impl stats.setPercentileImpl(new goodPercentile()); Assert.assertEquals(2, stats.getPercentile(50.0), 1E-10); // Try "new math" impl stats.setPercentileImpl(new subPercentile()); Assert.assertEquals(10.0, stats.getPercentile(10.0), 1E-10); // Try to set bad impl try { stats.setPercentileImpl(new badPercentile()); Assert.fail("Expecting IllegalArgumentException"); } catch (IllegalArgumentException ex) { // expected } } @Test public void test20090720() { DescriptiveStatistics descriptiveStatistics = new DescriptiveStatistics(100); for (int i = 0; i < 161; i++) { descriptiveStatistics.addValue(1.2); } descriptiveStatistics.clear(); descriptiveStatistics.addValue(1.2); Assert.assertEquals(1, descriptiveStatistics.getN()); } @Test public void testRemoval() { final DescriptiveStatistics dstat = createDescriptiveStatistics(); checkremoval(dstat, 1, 6.0, 0.0, Double.NaN); checkremoval(dstat, 3, 5.0, 3.0, 4.5); checkremoval(dstat, 6, 3.5, 2.5, 3.0); checkremoval(dstat, 9, 3.5, 2.5, 3.0); checkremoval(dstat, DescriptiveStatistics.INFINITE_WINDOW, 3.5, 2.5, 3.0); } @Test public void testSummaryConsistency() { final DescriptiveStatistics dstats = new DescriptiveStatistics(); final SummaryStatistics sstats = new SummaryStatistics(); final int windowSize = 5; dstats.setWindowSize(windowSize); final double tol = 1E-12; for (int i = 0; i < 20; i++) { dstats.addValue(i); sstats.clear(); double[] values = dstats.getValues(); for (int j = 0; j < values.length; j++) { sstats.addValue(values[j]); } TestUtils.assertEquals(dstats.getMean(), sstats.getMean(), tol); TestUtils.assertEquals(new Mean().evaluate(values), dstats.getMean(), tol); TestUtils.assertEquals(dstats.getMax(), sstats.getMax(), tol); TestUtils.assertEquals(new Max().evaluate(values), dstats.getMax(), tol); TestUtils.assertEquals(dstats.getGeometricMean(), sstats.getGeometricMean(), tol); TestUtils.assertEquals(new GeometricMean().evaluate(values), dstats.getGeometricMean(), tol); TestUtils.assertEquals(dstats.getMin(), sstats.getMin(), tol); TestUtils.assertEquals(new Min().evaluate(values), dstats.getMin(), tol); TestUtils.assertEquals(dstats.getStandardDeviation(), sstats.getStandardDeviation(), tol); TestUtils.assertEquals(dstats.getVariance(), sstats.getVariance(), tol); TestUtils.assertEquals(new Variance().evaluate(values), dstats.getVariance(), tol); TestUtils.assertEquals(dstats.getSum(), sstats.getSum(), tol); TestUtils.assertEquals(new Sum().evaluate(values), dstats.getSum(), tol); TestUtils.assertEquals(dstats.getSumsq(), sstats.getSumsq(), tol); TestUtils.assertEquals(new SumOfSquares().evaluate(values), dstats.getSumsq(), tol); TestUtils.assertEquals(dstats.getPopulationVariance(), sstats.getPopulationVariance(), tol); TestUtils.assertEquals(new Variance(false).evaluate(values), dstats.getPopulationVariance(), tol); } } public void checkremoval(DescriptiveStatistics dstat, int wsize, double mean1, double mean2, double mean3) { dstat.setWindowSize(wsize); dstat.clear(); for (int i = 1 ; i <= 6 ; ++i) { dstat.addValue(i); } Assert.assertTrue(Precision.equalsIncludingNaN(mean1, dstat.getMean())); dstat.replaceMostRecentValue(0); Assert.assertTrue(Precision.equalsIncludingNaN(mean2, dstat.getMean())); dstat.removeMostRecentValue(); Assert.assertTrue(Precision.equalsIncludingNaN(mean3, dstat.getMean())); } // Test UnivariateStatistics impls for setter injection tests /** * A new way to compute the mean */ static class deepMean implements UnivariateStatistic { public double evaluate(double[] values, int begin, int length) { return 42; } public double evaluate(double[] values) { return 42; } public UnivariateStatistic copy() { return new deepMean(); } } /** * Test percentile implementation - wraps a Percentile */ static class goodPercentile implements UnivariateStatistic { private Percentile percentile = new Percentile(); public void setQuantile(double quantile) { percentile.setQuantile(quantile); } public double evaluate(double[] values, int begin, int length) { return percentile.evaluate(values, begin, length); } public double evaluate(double[] values) { return percentile.evaluate(values); } public UnivariateStatistic copy() { goodPercentile result = new goodPercentile(); result.setQuantile(percentile.getQuantile()); return result; } } /** * Test percentile subclass - another "new math" impl * Always returns currently set quantile */ static class subPercentile extends Percentile { @Override public double evaluate(double[] values, int begin, int length) { return getQuantile(); } @Override public double evaluate(double[] values) { return getQuantile(); } private static final long serialVersionUID = 8040701391045914979L; @Override public Percentile copy() { subPercentile result = new subPercentile(); return result; } } /** * "Bad" test percentile implementation - no setQuantile */ static class badPercentile implements UnivariateStatistic { private Percentile percentile = new Percentile(); public double evaluate(double[] values, int begin, int length) { return percentile.evaluate(values, begin, length); } public double evaluate(double[] values) { return percentile.evaluate(values); } public UnivariateStatistic copy() { return new badPercentile(); } } } ././@LongLink100644 0 0 162 12126630646 10260 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/stat/descriptive/AggregateSummaryStatisticsTest.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/stat/descriptive/AggregateSummaryStatis100644 1750 1750 27503 12126627674 32466 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.stat.descriptive; import java.util.ArrayList; import java.util.Collection; import org.apache.commons.math3.TestUtils; import org.apache.commons.math3.distribution.RealDistribution; import org.apache.commons.math3.distribution.UniformRealDistribution; import org.apache.commons.math3.distribution.IntegerDistribution; import org.apache.commons.math3.distribution.UniformIntegerDistribution; import org.apache.commons.math3.util.Precision; import org.junit.Assert; import org.junit.Test; /** * Test cases for {@link AggregateSummaryStatistics} * */ public class AggregateSummaryStatisticsTest { /** * Tests the standard aggregation behavior */ @Test public void testAggregation() { AggregateSummaryStatistics aggregate = new AggregateSummaryStatistics(); SummaryStatistics setOneStats = aggregate.createContributingStatistics(); SummaryStatistics setTwoStats = aggregate.createContributingStatistics(); Assert.assertNotNull("The set one contributing stats are null", setOneStats); Assert.assertNotNull("The set two contributing stats are null", setTwoStats); Assert.assertNotSame("Contributing stats objects are the same", setOneStats, setTwoStats); setOneStats.addValue(2); setOneStats.addValue(3); setOneStats.addValue(5); setOneStats.addValue(7); setOneStats.addValue(11); Assert.assertEquals("Wrong number of set one values", 5, setOneStats.getN()); Assert.assertTrue("Wrong sum of set one values", Precision.equals(28.0, setOneStats.getSum(), 1)); setTwoStats.addValue(2); setTwoStats.addValue(4); setTwoStats.addValue(8); Assert.assertEquals("Wrong number of set two values", 3, setTwoStats.getN()); Assert.assertTrue("Wrong sum of set two values", Precision.equals(14.0, setTwoStats.getSum(), 1)); Assert.assertEquals("Wrong number of aggregate values", 8, aggregate.getN()); Assert.assertTrue("Wrong aggregate sum", Precision.equals(42.0, aggregate.getSum(), 1)); } /** * Verify that aggregating over a partition gives the same results * as direct computation. * * 1) Randomly generate a dataset of 10-100 values * from [-100, 100] * 2) Divide the dataset it into 2-5 partitions * 3) Create an AggregateSummaryStatistic and ContributingStatistics * for each partition * 4) Compare results from the AggregateSummaryStatistic with values * returned by a single SummaryStatistics instance that is provided * the full dataset */ @Test public void testAggregationConsistency() { // Generate a random sample and random partition double[] totalSample = generateSample(); double[][] subSamples = generatePartition(totalSample); int nSamples = subSamples.length; // Create aggregator and total stats for comparison AggregateSummaryStatistics aggregate = new AggregateSummaryStatistics(); SummaryStatistics totalStats = new SummaryStatistics(); // Create array of component stats SummaryStatistics componentStats[] = new SummaryStatistics[nSamples]; for (int i = 0; i < nSamples; i++) { // Make componentStats[i] a contributing statistic to aggregate componentStats[i] = aggregate.createContributingStatistics(); // Add values from subsample for (int j = 0; j < subSamples[i].length; j++) { componentStats[i].addValue(subSamples[i][j]); } } // Compute totalStats directly for (int i = 0; i < totalSample.length; i++) { totalStats.addValue(totalSample[i]); } /* * Compare statistics in totalStats with aggregate. * Note that guaranteed success of this comparison depends on the * fact that gets values in exactly the same order * as . * */ Assert.assertEquals(totalStats.getSummary(), aggregate.getSummary()); } /** * Test aggregate function by randomly generating a dataset of 10-100 values * from [-100, 100], dividing it into 2-5 partitions, computing stats for each * partition and comparing the result of aggregate(...) applied to the collection * of per-partition SummaryStatistics with a single SummaryStatistics computed * over the full sample. * */ @Test public void testAggregate() { // Generate a random sample and random partition double[] totalSample = generateSample(); double[][] subSamples = generatePartition(totalSample); int nSamples = subSamples.length; // Compute combined stats directly SummaryStatistics totalStats = new SummaryStatistics(); for (int i = 0; i < totalSample.length; i++) { totalStats.addValue(totalSample[i]); } // Now compute subsample stats individually and aggregate SummaryStatistics[] subSampleStats = new SummaryStatistics[nSamples]; for (int i = 0; i < nSamples; i++) { subSampleStats[i] = new SummaryStatistics(); } Collection aggregate = new ArrayList(); for (int i = 0; i < nSamples; i++) { for (int j = 0; j < subSamples[i].length; j++) { subSampleStats[i].addValue(subSamples[i][j]); } aggregate.add(subSampleStats[i]); } // Compare values StatisticalSummary aggregatedStats = AggregateSummaryStatistics.aggregate(aggregate); assertEquals(totalStats.getSummary(), aggregatedStats, 10E-12); } @Test public void testAggregateDegenerate() { double[] totalSample = {1, 2, 3, 4, 5}; double[][] subSamples = {{1}, {2}, {3}, {4}, {5}}; // Compute combined stats directly SummaryStatistics totalStats = new SummaryStatistics(); for (int i = 0; i < totalSample.length; i++) { totalStats.addValue(totalSample[i]); } // Now compute subsample stats individually and aggregate SummaryStatistics[] subSampleStats = new SummaryStatistics[5]; for (int i = 0; i < 5; i++) { subSampleStats[i] = new SummaryStatistics(); } Collection aggregate = new ArrayList(); for (int i = 0; i < 5; i++) { for (int j = 0; j < subSamples[i].length; j++) { subSampleStats[i].addValue(subSamples[i][j]); } aggregate.add(subSampleStats[i]); } // Compare values StatisticalSummaryValues aggregatedStats = AggregateSummaryStatistics.aggregate(aggregate); assertEquals(totalStats.getSummary(), aggregatedStats, 10E-12); } @Test public void testAggregateSpecialValues() { double[] totalSample = {Double.POSITIVE_INFINITY, 2, 3, Double.NaN, 5}; double[][] subSamples = {{Double.POSITIVE_INFINITY, 2}, {3}, {Double.NaN}, {5}}; // Compute combined stats directly SummaryStatistics totalStats = new SummaryStatistics(); for (int i = 0; i < totalSample.length; i++) { totalStats.addValue(totalSample[i]); } // Now compute subsample stats individually and aggregate SummaryStatistics[] subSampleStats = new SummaryStatistics[5]; for (int i = 0; i < 4; i++) { subSampleStats[i] = new SummaryStatistics(); } Collection aggregate = new ArrayList(); for (int i = 0; i < 4; i++) { for (int j = 0; j < subSamples[i].length; j++) { subSampleStats[i].addValue(subSamples[i][j]); } aggregate.add(subSampleStats[i]); } // Compare values StatisticalSummaryValues aggregatedStats = AggregateSummaryStatistics.aggregate(aggregate); assertEquals(totalStats.getSummary(), aggregatedStats, 10E-12); } /** * Verifies that a StatisticalSummary and a StatisticalSummaryValues are equal up * to delta, with NaNs, infinities returned in the same spots. For max, min, n, values * have to agree exactly, delta is used only for sum, mean, variance, std dev. */ protected static void assertEquals(StatisticalSummary expected, StatisticalSummary observed, double delta) { TestUtils.assertEquals(expected.getMax(), observed.getMax(), 0); TestUtils.assertEquals(expected.getMin(), observed.getMin(), 0); Assert.assertEquals(expected.getN(), observed.getN()); TestUtils.assertEquals(expected.getSum(), observed.getSum(), delta); TestUtils.assertEquals(expected.getMean(), observed.getMean(), delta); TestUtils.assertEquals(expected.getStandardDeviation(), observed.getStandardDeviation(), delta); TestUtils.assertEquals(expected.getVariance(), observed.getVariance(), delta); } /** * Generates a random sample of double values. * Sample size is random, between 10 and 100 and values are * uniformly distributed over [-100, 100]. * * @return array of random double values */ private double[] generateSample() { final IntegerDistribution size = new UniformIntegerDistribution(10, 100); final RealDistribution randomData = new UniformRealDistribution(-100, 100); final int sampleSize = size.sample(); final double[] out = randomData.sample(sampleSize); return out; } /** * Generates a partition of into up to 5 sequentially selected * subsamples with randomly selected partition points. * * @param sample array to partition * @return rectangular array with rows = subsamples */ private double[][] generatePartition(double[] sample) { final int length = sample.length; final double[][] out = new double[5][]; int cur = 0; // beginning of current partition segment int offset = 0; // end of current partition segment int sampleCount = 0; // number of segments defined for (int i = 0; i < 5; i++) { if (cur == length || offset == length) { break; } final int next; if (i == 4 || cur == length - 1) { next = length - 1; } else { next = (new UniformIntegerDistribution(cur, length - 1)).sample(); } final int subLength = next - cur + 1; out[i] = new double[subLength]; System.arraycopy(sample, offset, out[i], 0, subLength); cur = next + 1; sampleCount++; offset += subLength; } if (sampleCount < 5) { double[][] out2 = new double[sampleCount][]; for (int j = 0; j < sampleCount; j++) { final int curSize = out[j].length; out2[j] = new double[curSize]; System.arraycopy(out[j], 0, out2[j], 0, curSize); } return out2; } else { return out; } } } ././@LongLink100644 0 0 157 12126630646 10264 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/stat/descriptive/MixedListUnivariateImplTest.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/stat/descriptive/MixedListUnivariateImp100644 1750 1750 15316 12126627674 32431 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.stat.descriptive; import java.io.Serializable; import java.util.ArrayList; import java.util.List; import org.apache.commons.math3.util.FastMath; import org.apache.commons.math3.util.NumberTransformer; import org.apache.commons.math3.util.TransformerMap; import org.junit.Assert; import org.junit.Test; /** * Test cases for the {@link ListUnivariateImpl} class. * * @version $Id: MixedListUnivariateImplTest.java 1364030 2012-07-21 01:10:04Z erans $ */ public final class MixedListUnivariateImplTest { private double one = 1; private float two = 2; private int three = 3; private double mean = 2; private double sumSq = 18; private double sum = 8; private double var = 0.666666666666666666667; private double std = FastMath.sqrt(var); private double n = 4; private double min = 1; private double max = 3; private double tolerance = 10E-15; private TransformerMap transformers = new TransformerMap(); public MixedListUnivariateImplTest() { transformers = new TransformerMap(); transformers.putTransformer(Foo.class, new FooTransformer()); transformers.putTransformer(Bar.class, new BarTransformer()); } /** test stats */ @Test public void testStats() { List externalList = new ArrayList(); DescriptiveStatistics u = new ListUnivariateImpl(externalList,transformers); Assert.assertEquals("total count", 0, u.getN(), tolerance); u.addValue(one); u.addValue(two); u.addValue(two); u.addValue(three); Assert.assertEquals("N", n, u.getN(), tolerance); Assert.assertEquals("sum", sum, u.getSum(), tolerance); Assert.assertEquals("sumsq", sumSq, u.getSumsq(), tolerance); Assert.assertEquals("var", var, u.getVariance(), tolerance); Assert.assertEquals("std", std, u.getStandardDeviation(), tolerance); Assert.assertEquals("mean", mean, u.getMean(), tolerance); Assert.assertEquals("min", min, u.getMin(), tolerance); Assert.assertEquals("max", max, u.getMax(), tolerance); u.clear(); Assert.assertEquals("total count", 0, u.getN(), tolerance); } @Test public void testN0andN1Conditions() { DescriptiveStatistics u = new ListUnivariateImpl(new ArrayList(),transformers); Assert.assertTrue( "Mean of n = 0 set should be NaN", Double.isNaN(u.getMean())); Assert.assertTrue( "Standard Deviation of n = 0 set should be NaN", Double.isNaN(u.getStandardDeviation())); Assert.assertTrue( "Variance of n = 0 set should be NaN", Double.isNaN(u.getVariance())); u.addValue(one); Assert.assertTrue( "Mean of n = 1 set should be value of single item n1, instead it is " + u.getMean() , u.getMean() == one); Assert.assertTrue( "StdDev of n = 1 set should be zero, instead it is: " + u.getStandardDeviation(), u.getStandardDeviation() == 0); Assert.assertTrue( "Variance of n = 1 set should be zero", u.getVariance() == 0); } @Test public void testSkewAndKurtosis() { ListUnivariateImpl u = new ListUnivariateImpl(new ArrayList(), transformers); u.addObject("12.5"); u.addObject(Integer.valueOf(12)); u.addObject("11.8"); u.addObject("14.2"); u.addObject(new Foo()); u.addObject("14.5"); u.addObject(Long.valueOf(21)); u.addObject("8.2"); u.addObject("10.3"); u.addObject("11.3"); u.addObject(Float.valueOf(14.1f)); u.addObject("9.9"); u.addObject("12.2"); u.addObject(new Bar()); u.addObject("12.1"); u.addObject("11"); u.addObject(Double.valueOf(19.8)); u.addObject("11"); u.addObject("10"); u.addObject("8.8"); u.addObject("9"); u.addObject("12.3"); Assert.assertEquals("mean", 12.40455, u.getMean(), 0.0001); Assert.assertEquals("variance", 10.00236, u.getVariance(), 0.0001); Assert.assertEquals("skewness", 1.437424, u.getSkewness(), 0.0001); Assert.assertEquals("kurtosis", 2.37719, u.getKurtosis(), 0.0001); } @Test public void testProductAndGeometricMean() { ListUnivariateImpl u = new ListUnivariateImpl(new ArrayList(),transformers); u.setWindowSize(10); u.addValue(1.0); u.addValue(2.0); u.addValue(3.0); u.addValue(4.0); Assert.assertEquals( "Geometric mean not expected", 2.213364, u.getGeometricMean(), 0.00001); // Now test rolling - StorelessDescriptiveStatistics should discount the contribution // of a discarded element for (int i = 0; i < 10; i++) { u.addValue(i + 2); } // Values should be (2,3,4,5,6,7,8,9,10,11) Assert.assertEquals( "Geometric mean not expected", 5.755931, u.getGeometricMean(), 0.00001); } public static final class Foo { public String heresFoo() { return "14.9"; } } public static final class FooTransformer implements NumberTransformer, Serializable { private static final long serialVersionUID = -4252248129291326127L; public double transform(Object o) { return Double.parseDouble(((Foo) o).heresFoo()); } } public static final class Bar { public String heresBar() { return "12.0"; } } public static final class BarTransformer implements NumberTransformer, Serializable { private static final long serialVersionUID = -1768345377764262043L; public double transform(Object o) { return Double.parseDouble(((Bar) o).heresBar()); } } } commons-math3-3.2-src/src/test/java/org/apache/commons/math3/stat/descriptive/rank/MinTest.java100644 1750 1750 5223 12126627674 31243 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.stat.descriptive.rank; import org.apache.commons.math3.stat.descriptive.StorelessUnivariateStatisticAbstractTest; import org.apache.commons.math3.stat.descriptive.UnivariateStatistic; import org.junit.Assert; import org.junit.Test; /** * Test cases for the {@link UnivariateStatistic} class. * @version $Id: MinTest.java 1244107 2012-02-14 16:17:55Z erans $ */ public class MinTest extends StorelessUnivariateStatisticAbstractTest{ protected Min stat; /** * {@inheritDoc} */ @Override public UnivariateStatistic getUnivariateStatistic() { return new Min(); } /** * {@inheritDoc} */ @Override public double expectedValue() { return this.min; } @Test public void testSpecialValues() { double[] testArray = {0d, Double.NaN, Double.POSITIVE_INFINITY, Double.NEGATIVE_INFINITY}; Min min = new Min(); Assert.assertTrue(Double.isNaN(min.getResult())); min.increment(testArray[0]); Assert.assertEquals(0d, min.getResult(), 0); min.increment(testArray[1]); Assert.assertEquals(0d, min.getResult(), 0); min.increment(testArray[2]); Assert.assertEquals(0d, min.getResult(), 0); min.increment(testArray[3]); Assert.assertEquals(Double.NEGATIVE_INFINITY, min.getResult(), 0); Assert.assertEquals(Double.NEGATIVE_INFINITY, min.evaluate(testArray), 0); } @Test public void testNaNs() { Min min = new Min(); double nan = Double.NaN; Assert.assertEquals(2d, min.evaluate(new double[]{nan, 2d, 3d}), 0); Assert.assertEquals(1d, min.evaluate(new double[]{1d, nan, 3d}), 0); Assert.assertEquals(1d, min.evaluate(new double[]{1d, 2d, nan}), 0); Assert.assertTrue(Double.isNaN(min.evaluate(new double[]{nan, nan, nan}))); } } commons-math3-3.2-src/src/test/java/org/apache/commons/math3/stat/descriptive/rank/MedianTest.java100644 1750 1750 2747 12126627674 31725 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.stat.descriptive.rank; import org.apache.commons.math3.stat.descriptive.UnivariateStatistic; import org.apache.commons.math3.stat.descriptive.UnivariateStatisticAbstractTest; /** * Test cases for the {@link UnivariateStatistic} class. * @version $Id: MedianTest.java 1244107 2012-02-14 16:17:55Z erans $ */ public class MedianTest extends UnivariateStatisticAbstractTest{ protected Median stat; /** * {@inheritDoc} */ @Override public UnivariateStatistic getUnivariateStatistic() { return new Median(); } /** * {@inheritDoc} */ @Override public double expectedValue() { return this.median; } } ././@LongLink100644 0 0 147 12126630646 10263 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/stat/descriptive/rank/PercentileTest.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/stat/descriptive/rank/PercentileTest.ja100644 1750 1750 14137 12126627674 32307 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.stat.descriptive.rank; import org.apache.commons.math3.stat.descriptive.UnivariateStatistic; import org.apache.commons.math3.stat.descriptive.UnivariateStatisticAbstractTest; import org.apache.commons.math3.exception.MathIllegalArgumentException; import org.junit.Assert; import org.junit.Test; /** * Test cases for the {@link UnivariateStatistic} class. * @version $Id: PercentileTest.java 1244107 2012-02-14 16:17:55Z erans $ */ public class PercentileTest extends UnivariateStatisticAbstractTest{ protected Percentile stat; /** * {@inheritDoc} */ @Override public UnivariateStatistic getUnivariateStatistic() { return new Percentile(95.0); } /** * {@inheritDoc} */ @Override public double expectedValue() { return this.percentile95; } @Test public void testHighPercentile(){ double[] d = new double[]{1, 2, 3}; Percentile p = new Percentile(75); Assert.assertEquals(3.0, p.evaluate(d), 1.0e-5); } @Test public void testLowPercentile() { double[] d = new double[] {0, 1}; Percentile p = new Percentile(25); Assert.assertEquals(0d, p.evaluate(d), Double.MIN_VALUE); } @Test public void testPercentile() { double[] d = new double[] {1, 3, 2, 4}; Percentile p = new Percentile(30); Assert.assertEquals(1.5, p.evaluate(d), 1.0e-5); p.setQuantile(25); Assert.assertEquals(1.25, p.evaluate(d), 1.0e-5); p.setQuantile(75); Assert.assertEquals(3.75, p.evaluate(d), 1.0e-5); p.setQuantile(50); Assert.assertEquals(2.5, p.evaluate(d), 1.0e-5); // invalid percentiles try { p.evaluate(d, 0, d.length, -1.0); Assert.fail(); } catch (MathIllegalArgumentException ex) { // success } try { p.evaluate(d, 0, d.length, 101.0); Assert.fail(); } catch (MathIllegalArgumentException ex) { // success } } @Test public void testNISTExample() { double[] d = new double[] {95.1772, 95.1567, 95.1937, 95.1959, 95.1442, 95.0610, 95.1591, 95.1195, 95.1772, 95.0925, 95.1990, 95.1682 }; Percentile p = new Percentile(90); Assert.assertEquals(95.1981, p.evaluate(d), 1.0e-4); Assert.assertEquals(95.1990, p.evaluate(d,0,d.length, 100d), 0); } @Test public void test5() { Percentile percentile = new Percentile(5); Assert.assertEquals(this.percentile5, percentile.evaluate(testArray), getTolerance()); } @Test public void testNullEmpty() { Percentile percentile = new Percentile(50); double[] nullArray = null; double[] emptyArray = new double[] {}; try { percentile.evaluate(nullArray); Assert.fail("Expecting MathIllegalArgumentException for null array"); } catch (MathIllegalArgumentException ex) { // expected } Assert.assertTrue(Double.isNaN(percentile.evaluate(emptyArray))); } @Test public void testSingleton() { Percentile percentile = new Percentile(50); double[] singletonArray = new double[] {1d}; Assert.assertEquals(1d, percentile.evaluate(singletonArray), 0); Assert.assertEquals(1d, percentile.evaluate(singletonArray, 0, 1), 0); Assert.assertEquals(1d, percentile.evaluate(singletonArray, 0, 1, 5), 0); Assert.assertEquals(1d, percentile.evaluate(singletonArray, 0, 1, 100), 0); Assert.assertTrue(Double.isNaN(percentile.evaluate(singletonArray, 0, 0))); } @Test public void testSpecialValues() { Percentile percentile = new Percentile(50); double[] specialValues = new double[] {0d, 1d, 2d, 3d, 4d, Double.NaN}; Assert.assertEquals(2.5d, percentile.evaluate(specialValues), 0); specialValues = new double[] {Double.NEGATIVE_INFINITY, 1d, 2d, 3d, Double.NaN, Double.POSITIVE_INFINITY}; Assert.assertEquals(2.5d, percentile.evaluate(specialValues), 0); specialValues = new double[] {1d, 1d, Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY}; Assert.assertTrue(Double.isInfinite(percentile.evaluate(specialValues))); specialValues = new double[] {1d, 1d, Double.NaN, Double.NaN}; Assert.assertTrue(Double.isNaN(percentile.evaluate(specialValues))); specialValues = new double[] {1d, 1d, Double.NEGATIVE_INFINITY, Double.NEGATIVE_INFINITY}; // Interpolation results in NEGATIVE_INFINITY + POSITIVE_INFINITY Assert.assertTrue(Double.isNaN(percentile.evaluate(specialValues))); } @Test public void testSetQuantile() { Percentile percentile = new Percentile(10); percentile.setQuantile(100); // OK Assert.assertEquals(100, percentile.getQuantile(), 0); try { percentile.setQuantile(0); Assert.fail("Expecting MathIllegalArgumentException"); } catch (MathIllegalArgumentException ex) { // expected } try { new Percentile(0); Assert.fail("Expecting MathIllegalArgumentException"); } catch (MathIllegalArgumentException ex) { // expected } } } commons-math3-3.2-src/src/test/java/org/apache/commons/math3/stat/descriptive/rank/MaxTest.java100644 1750 1750 5224 12126627674 31246 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.stat.descriptive.rank; import org.apache.commons.math3.stat.descriptive.StorelessUnivariateStatisticAbstractTest; import org.apache.commons.math3.stat.descriptive.UnivariateStatistic; import org.junit.Assert; import org.junit.Test; /** * Test cases for the {@link UnivariateStatistic} class. * @version $Id: MaxTest.java 1244107 2012-02-14 16:17:55Z erans $ */ public class MaxTest extends StorelessUnivariateStatisticAbstractTest { protected Max stat; /** * {@inheritDoc} */ @Override public UnivariateStatistic getUnivariateStatistic() { return new Max(); } /** * {@inheritDoc} */ @Override public double expectedValue() { return this.max; } @Test public void testSpecialValues() { double[] testArray = {0d, Double.NaN, Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY}; Max max = new Max(); Assert.assertTrue(Double.isNaN(max.getResult())); max.increment(testArray[0]); Assert.assertEquals(0d, max.getResult(), 0); max.increment(testArray[1]); Assert.assertEquals(0d, max.getResult(), 0); max.increment(testArray[2]); Assert.assertEquals(0d, max.getResult(), 0); max.increment(testArray[3]); Assert.assertEquals(Double.POSITIVE_INFINITY, max.getResult(), 0); Assert.assertEquals(Double.POSITIVE_INFINITY, max.evaluate(testArray), 0); } @Test public void testNaNs() { Max max = new Max(); double nan = Double.NaN; Assert.assertEquals(3d, max.evaluate(new double[]{nan, 2d, 3d}), 0); Assert.assertEquals(3d, max.evaluate(new double[]{1d, nan, 3d}), 0); Assert.assertEquals(2d, max.evaluate(new double[]{1d, 2d, nan}), 0); Assert.assertTrue(Double.isNaN(max.evaluate(new double[]{nan, nan, nan}))); } } ././@LongLink100644 0 0 174 12126630646 10263 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/stat/descriptive/StorelessUnivariateStatisticAbstractTest.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/stat/descriptive/StorelessUnivariateSta100644 1750 1750 20541 12126627674 32510 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.stat.descriptive; import org.apache.commons.math3.TestUtils; import org.apache.commons.math3.stat.descriptive.moment.SecondMoment; import org.apache.commons.math3.util.FastMath; import org.junit.Assert; import org.junit.Test; /** * Test cases for {@link StorelessUnivariateStatistic} classes. * @version $Id: StorelessUnivariateStatisticAbstractTest.java 1364030 2012-07-21 01:10:04Z erans $ */ public abstract class StorelessUnivariateStatisticAbstractTest extends UnivariateStatisticAbstractTest { /** Small sample arrays */ protected double[][] smallSamples = {{}, {1}, {1,2}, {1,2,3}, {1,2,3,4}}; /** Return a new instance of the statistic */ @Override public abstract UnivariateStatistic getUnivariateStatistic(); /**Expected value for the testArray defined in UnivariateStatisticAbstractTest */ @Override public abstract double expectedValue(); /** * Verifies that increment() and incrementAll work properly. */ @Test public void testIncrementation() { StorelessUnivariateStatistic statistic = (StorelessUnivariateStatistic) getUnivariateStatistic(); // Add testArray one value at a time and check result for (int i = 0; i < testArray.length; i++) { statistic.increment(testArray[i]); } Assert.assertEquals(expectedValue(), statistic.getResult(), getTolerance()); Assert.assertEquals(testArray.length, statistic.getN()); statistic.clear(); // Add testArray all at once and check again statistic.incrementAll(testArray); Assert.assertEquals(expectedValue(), statistic.getResult(), getTolerance()); Assert.assertEquals(testArray.length, statistic.getN()); statistic.clear(); // Cleared checkClearValue(statistic); Assert.assertEquals(0, statistic.getN()); } protected void checkClearValue(StorelessUnivariateStatistic statistic){ Assert.assertTrue(Double.isNaN(statistic.getResult())); } @Test public void testSerialization() { StorelessUnivariateStatistic statistic = (StorelessUnivariateStatistic) getUnivariateStatistic(); TestUtils.checkSerializedEquality(statistic); statistic.clear(); for (int i = 0; i < testArray.length; i++) { statistic.increment(testArray[i]); if(i % 5 == 0) statistic = (StorelessUnivariateStatistic)TestUtils.serializeAndRecover(statistic); } TestUtils.checkSerializedEquality(statistic); Assert.assertEquals(expectedValue(), statistic.getResult(), getTolerance()); statistic.clear(); checkClearValue(statistic); } @Test public void testEqualsAndHashCode() { StorelessUnivariateStatistic statistic = (StorelessUnivariateStatistic) getUnivariateStatistic(); StorelessUnivariateStatistic statistic2 = null; Assert.assertTrue("non-null, compared to null", !statistic.equals(statistic2)); Assert.assertTrue("reflexive, non-null", statistic.equals(statistic)); int emptyHash = statistic.hashCode(); statistic2 = (StorelessUnivariateStatistic) getUnivariateStatistic(); Assert.assertTrue("empty stats should be equal", statistic.equals(statistic2)); Assert.assertEquals("empty stats should have the same hashcode", emptyHash, statistic2.hashCode()); statistic.increment(1d); Assert.assertTrue("reflexive, non-empty", statistic.equals(statistic)); Assert.assertTrue("non-empty, compared to empty", !statistic.equals(statistic2)); Assert.assertTrue("non-empty, compared to empty", !statistic2.equals(statistic)); Assert.assertTrue("non-empty stat should have different hashcode from empty stat", statistic.hashCode() != emptyHash); statistic2.increment(1d); Assert.assertTrue("stats with same data should be equal", statistic.equals(statistic2)); Assert.assertEquals("stats with same data should have the same hashcode", statistic.hashCode(), statistic2.hashCode()); statistic.increment(Double.POSITIVE_INFINITY); Assert.assertTrue("stats with different n's should not be equal", !statistic2.equals(statistic)); Assert.assertTrue("stats with different n's should have different hashcodes", statistic.hashCode() != statistic2.hashCode()); statistic2.increment(Double.POSITIVE_INFINITY); Assert.assertTrue("stats with same data should be equal", statistic.equals(statistic2)); Assert.assertEquals("stats with same data should have the same hashcode", statistic.hashCode(), statistic2.hashCode()); statistic.clear(); statistic2.clear(); Assert.assertTrue("cleared stats should be equal", statistic.equals(statistic2)); Assert.assertEquals("cleared stats should have thashcode of empty stat", emptyHash, statistic2.hashCode()); Assert.assertEquals("cleared stats should have thashcode of empty stat", emptyHash, statistic.hashCode()); } @Test public void testMomentSmallSamples() { UnivariateStatistic stat = getUnivariateStatistic(); if (stat instanceof SecondMoment) { SecondMoment moment = (SecondMoment) getUnivariateStatistic(); Assert.assertTrue(Double.isNaN(moment.getResult())); moment.increment(1d); Assert.assertEquals(0d, moment.getResult(), 0); } } /** * Make sure that evaluate(double[]) and inrementAll(double[]), * getResult() give same results. */ @Test public void testConsistency() { StorelessUnivariateStatistic stat = (StorelessUnivariateStatistic) getUnivariateStatistic(); stat.incrementAll(testArray); Assert.assertEquals(stat.getResult(), stat.evaluate(testArray), getTolerance()); for (int i = 0; i < smallSamples.length; i++) { stat.clear(); for (int j =0; j < smallSamples[i].length; j++) { stat.increment(smallSamples[i][j]); } TestUtils.assertEquals(stat.getResult(), stat.evaluate(smallSamples[i]), getTolerance()); } } /** * Verifies that copied statistics remain equal to originals when * incremented the same way. * */ @Test public void testCopyConsistency() { StorelessUnivariateStatistic master = (StorelessUnivariateStatistic) getUnivariateStatistic(); StorelessUnivariateStatistic replica = null; // Randomly select a portion of testArray to load first long index = FastMath.round((FastMath.random()) * testArray.length); // Put first half in master and copy master to replica master.incrementAll(testArray, 0, (int) index); replica = master.copy(); // Check same Assert.assertTrue(replica.equals(master)); Assert.assertTrue(master.equals(replica)); // Now add second part to both and check again master.incrementAll(testArray, (int) index, (int) (testArray.length - index)); replica.incrementAll(testArray, (int) index, (int) (testArray.length - index)); Assert.assertTrue(replica.equals(master)); Assert.assertTrue(master.equals(replica)); } @Test public void testSerial() { StorelessUnivariateStatistic s = (StorelessUnivariateStatistic) getUnivariateStatistic(); Assert.assertEquals(s, TestUtils.serializeAndRecover(s)); } } ././@LongLink100644 0 0 171 12126630646 10260 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/stat/descriptive/SynchronizedDescriptiveStatisticsTest.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/stat/descriptive/SynchronizedDescriptiv100644 1750 1750 2400 12126627674 32513 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with this * work for additional information regarding copyright ownership. The ASF * licenses this file to You 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. */ package org.apache.commons.math3.stat.descriptive; /** * Test cases for the {@link SynchronizedDescriptiveStatisticsTest} class. * @version $Id: SynchronizedDescriptiveStatisticsTest.java 1244107 2012-02-14 16:17:55Z erans $ * 2007) $ */ public final class SynchronizedDescriptiveStatisticsTest extends DescriptiveStatisticsTest { @Override protected DescriptiveStatistics createDescriptiveStatistics() { return new SynchronizedDescriptiveStatistics(); } } ././@LongLink100644 0 0 145 12126630646 10261 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/stat/descriptive/summary/SumSqTest.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/stat/descriptive/summary/SumSqTest.java100644 1750 1750 4713 12126627674 32335 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.stat.descriptive.summary; import org.apache.commons.math3.stat.descriptive.StorelessUnivariateStatistic; import org.apache.commons.math3.stat.descriptive.StorelessUnivariateStatisticAbstractTest; import org.apache.commons.math3.stat.descriptive.UnivariateStatistic; import org.junit.Assert; import org.junit.Test; /** * Test cases for the {@link SumOfSquares} class. * * @version $Id: SumSqTest.java 1244107 2012-02-14 16:17:55Z erans $ */ public class SumSqTest extends StorelessUnivariateStatisticAbstractTest{ protected SumOfSquares stat; /** * {@inheritDoc} */ @Override public UnivariateStatistic getUnivariateStatistic() { return new SumOfSquares(); } /** * {@inheritDoc} */ @Override public double expectedValue() { return this.sumSq; } @Test public void testSpecialValues() { SumOfSquares sumSq = new SumOfSquares(); Assert.assertEquals(0, sumSq.getResult(), 0); sumSq.increment(2d); Assert.assertEquals(4d, sumSq.getResult(), 0); sumSq.increment(Double.POSITIVE_INFINITY); Assert.assertEquals(Double.POSITIVE_INFINITY, sumSq.getResult(), 0); sumSq.increment(Double.NEGATIVE_INFINITY); Assert.assertEquals(Double.POSITIVE_INFINITY, sumSq.getResult(), 0); sumSq.increment(Double.NaN); Assert.assertTrue(Double.isNaN(sumSq.getResult())); sumSq.increment(1); Assert.assertTrue(Double.isNaN(sumSq.getResult())); } @Override protected void checkClearValue(StorelessUnivariateStatistic statistic){ Assert.assertEquals(0, statistic.getResult(), 0); } } ././@LongLink100644 0 0 147 12126630646 10263 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/stat/descriptive/summary/ProductTest.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/stat/descriptive/summary/ProductTest.ja100644 1750 1750 6260 12126627674 32355 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.stat.descriptive.summary; import org.apache.commons.math3.stat.descriptive.StorelessUnivariateStatistic; import org.apache.commons.math3.stat.descriptive.StorelessUnivariateStatisticAbstractTest; import org.apache.commons.math3.stat.descriptive.UnivariateStatistic; import org.junit.Assert; import org.junit.Test; /** * Test cases for the {@link UnivariateStatistic} class. * @version $Id: ProductTest.java 1244107 2012-02-14 16:17:55Z erans $ */ public class ProductTest extends StorelessUnivariateStatisticAbstractTest{ protected Product stat; /** * {@inheritDoc} */ @Override public UnivariateStatistic getUnivariateStatistic() { return new Product(); } /** * {@inheritDoc} */ @Override public double getTolerance() { return 10E8; //sic -- big absolute error due to only 15 digits of accuracy in double } /** * {@inheritDoc} */ @Override public double expectedValue() { return this.product; } /**Expected value for the testArray defined in UnivariateStatisticAbstractTest */ public double expectedWeightedValue() { return this.weightedProduct; } @Test public void testSpecialValues() { Product product = new Product(); Assert.assertEquals(1, product.getResult(), 0); product.increment(1); Assert.assertEquals(1, product.getResult(), 0); product.increment(Double.POSITIVE_INFINITY); Assert.assertEquals(Double.POSITIVE_INFINITY, product.getResult(), 0); product.increment(Double.NEGATIVE_INFINITY); Assert.assertEquals(Double.NEGATIVE_INFINITY, product.getResult(), 0); product.increment(Double.NaN); Assert.assertTrue(Double.isNaN(product.getResult())); product.increment(1); Assert.assertTrue(Double.isNaN(product.getResult())); } @Test public void testWeightedProduct() { Product product = new Product(); Assert.assertEquals(expectedWeightedValue(), product.evaluate(testArray, testWeightsArray, 0, testArray.length),getTolerance()); Assert.assertEquals(expectedValue(), product.evaluate(testArray, unitWeightsArray, 0, testArray.length), getTolerance()); } @Override protected void checkClearValue(StorelessUnivariateStatistic statistic){ Assert.assertEquals(1, statistic.getResult(), 0); } } ././@LongLink100644 0 0 146 12126630646 10262 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/stat/descriptive/summary/SumLogTest.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/stat/descriptive/summary/SumLogTest.jav100644 1750 1750 5343 12126627674 32332 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.stat.descriptive.summary; import org.apache.commons.math3.stat.descriptive.StorelessUnivariateStatistic; import org.apache.commons.math3.stat.descriptive.StorelessUnivariateStatisticAbstractTest; import org.apache.commons.math3.stat.descriptive.UnivariateStatistic; import org.junit.Assert; import org.junit.Test; /** * Test cases for the {@link UnivariateStatistic} class. * @version $Id: SumLogTest.java 1244107 2012-02-14 16:17:55Z erans $ */ public class SumLogTest extends StorelessUnivariateStatisticAbstractTest{ protected SumOfLogs stat; /** * {@inheritDoc} */ @Override public UnivariateStatistic getUnivariateStatistic() { return new SumOfLogs(); } /** * {@inheritDoc} */ @Override public double expectedValue() { return this.sumLog; } @Test public void testSpecialValues() { SumOfLogs sum = new SumOfLogs(); // empty Assert.assertEquals(0, sum.getResult(), 0); // finite data sum.increment(1d); Assert.assertFalse(Double.isNaN(sum.getResult())); // add negative infinity sum.increment(0d); Assert.assertEquals(Double.NEGATIVE_INFINITY, sum.getResult(), 0); // add positive infinity -- should make NaN sum.increment(Double.POSITIVE_INFINITY); Assert.assertTrue(Double.isNaN(sum.getResult())); // clear sum.clear(); Assert.assertEquals(0, sum.getResult(), 0); // positive infinity by itself sum.increment(Double.POSITIVE_INFINITY); Assert.assertEquals(Double.POSITIVE_INFINITY, sum.getResult(), 0); // negative value -- should make NaN sum.increment(-2d); Assert.assertTrue(Double.isNaN(sum.getResult())); } @Override protected void checkClearValue(StorelessUnivariateStatistic statistic){ Assert.assertEquals(0, statistic.getResult(), 0); } } commons-math3-3.2-src/src/test/java/org/apache/commons/math3/stat/descriptive/summary/SumTest.java100644 1750 1750 5416 12126627674 32032 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.stat.descriptive.summary; import org.apache.commons.math3.stat.descriptive.StorelessUnivariateStatistic; import org.apache.commons.math3.stat.descriptive.StorelessUnivariateStatisticAbstractTest; import org.apache.commons.math3.stat.descriptive.UnivariateStatistic; import org.junit.Assert; import org.junit.Test; /** * Test cases for the {@link Sum} class. * @version $Id: SumTest.java 1244107 2012-02-14 16:17:55Z erans $ */ public class SumTest extends StorelessUnivariateStatisticAbstractTest{ protected Sum stat; /** * {@inheritDoc} */ @Override public UnivariateStatistic getUnivariateStatistic() { return new Sum(); } /** * {@inheritDoc} */ @Override public double expectedValue() { return this.sum; } /**Expected value for the testArray defined in UnivariateStatisticAbstractTest */ public double expectedWeightedValue() { return this.weightedSum; } @Test public void testSpecialValues() { Sum sum = new Sum(); Assert.assertEquals(0, sum.getResult(), 0); sum.increment(1); Assert.assertEquals(1, sum.getResult(), 0); sum.increment(Double.POSITIVE_INFINITY); Assert.assertEquals(Double.POSITIVE_INFINITY, sum.getResult(), 0); sum.increment(Double.NEGATIVE_INFINITY); Assert.assertTrue(Double.isNaN(sum.getResult())); sum.increment(1); Assert.assertTrue(Double.isNaN(sum.getResult())); } @Test public void testWeightedSum() { Sum sum = new Sum(); Assert.assertEquals(expectedWeightedValue(), sum.evaluate(testArray, testWeightsArray, 0, testArray.length), getTolerance()); Assert.assertEquals(expectedValue(), sum.evaluate(testArray, unitWeightsArray, 0, testArray.length), getTolerance()); } @Override protected void checkClearValue(StorelessUnivariateStatistic statistic){ Assert.assertEquals(0, statistic.getResult(), 0); } } ././@LongLink100644 0 0 147 12126630646 10263 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/stat/descriptive/moment/KurtosisTest.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/stat/descriptive/moment/KurtosisTest.ja100644 1750 1750 4147 12126627674 32364 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.stat.descriptive.moment; import org.apache.commons.math3.stat.descriptive.StorelessUnivariateStatisticAbstractTest; import org.apache.commons.math3.stat.descriptive.UnivariateStatistic; import org.junit.Assert; import org.junit.Test; /** * Test cases for the {@link UnivariateStatistic} class. * @version $Id: KurtosisTest.java 1244107 2012-02-14 16:17:55Z erans $ */ public class KurtosisTest extends StorelessUnivariateStatisticAbstractTest{ protected Kurtosis stat; /** * {@inheritDoc} */ @Override public UnivariateStatistic getUnivariateStatistic() { return new Kurtosis(); } /** * {@inheritDoc} */ @Override public double expectedValue() { return this.kurt; } /** * Make sure Double.NaN is returned iff n < 4 * */ @Test public void testNaN() { Kurtosis kurt = new Kurtosis(); Assert.assertTrue(Double.isNaN(kurt.getResult())); kurt.increment(1d); Assert.assertTrue(Double.isNaN(kurt.getResult())); kurt.increment(1d); Assert.assertTrue(Double.isNaN(kurt.getResult())); kurt.increment(1d); Assert.assertTrue(Double.isNaN(kurt.getResult())); kurt.increment(1d); Assert.assertFalse(Double.isNaN(kurt.getResult())); } } ././@LongLink100644 0 0 153 12126630646 10260 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/stat/descriptive/moment/FourthMomentTest.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/stat/descriptive/moment/FourthMomentTes100644 1750 1750 3336 12126627674 32412 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.stat.descriptive.moment; import org.apache.commons.math3.stat.descriptive.StorelessUnivariateStatisticAbstractTest; import org.apache.commons.math3.stat.descriptive.UnivariateStatistic; /** * Test cases for the {@link FourthMoment} class. * @version $Id: FourthMomentTest.java 1244107 2012-02-14 16:17:55Z erans $ */ public class FourthMomentTest extends StorelessUnivariateStatisticAbstractTest{ /** descriptive statistic. */ protected FourthMoment stat; /** * @see org.apache.commons.math3.stat.descriptive.UnivariateStatisticAbstractTest#getUnivariateStatistic() */ @Override public UnivariateStatistic getUnivariateStatistic() { return new FourthMoment(); } /** * @see org.apache.commons.math3.stat.descriptive.UnivariateStatisticAbstractTest#expectedValue() */ @Override public double expectedValue() { return this.fourthMoment; } } ././@LongLink100644 0 0 154 12126630646 10261 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/stat/descriptive/moment/VectorialMeanTest.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/stat/descriptive/moment/VectorialMeanTe100644 1750 1750 5373 12126627674 32334 0ustarlucluc 0 0 //Licensed to the Apache Software Foundation (ASF) under one //or more contributor license agreements. See the NOTICE file //distributed with this work for additional information //regarding copyright ownership. The ASF licenses this file //to you 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. package org.apache.commons.math3.stat.descriptive.moment; import org.apache.commons.math3.exception.DimensionMismatchException; import org.apache.commons.math3.TestUtils; import org.junit.Test; import org.junit.Assert; public class VectorialMeanTest { private double[][] points; public VectorialMeanTest() { points = new double[][] { { 1.2, 2.3, 4.5}, {-0.7, 2.3, 5.0}, { 3.1, 0.0, -3.1}, { 6.0, 1.2, 4.2}, {-0.7, 2.3, 5.0} }; } @Test public void testMismatch() { try { new VectorialMean(8).increment(new double[5]); Assert.fail("an exception should have been thrown"); } catch (DimensionMismatchException dme) { Assert.assertEquals(5, dme.getArgument()); Assert.assertEquals(8, dme.getDimension()); } } @Test public void testSimplistic() { VectorialMean stat = new VectorialMean(2); stat.increment(new double[] {-1.0, 1.0}); stat.increment(new double[] { 1.0, -1.0}); double[] mean = stat.getResult(); Assert.assertEquals(0.0, mean[0], 1.0e-12); Assert.assertEquals(0.0, mean[1], 1.0e-12); } @Test public void testBasicStats() { VectorialMean stat = new VectorialMean(points[0].length); for (int i = 0; i < points.length; ++i) { stat.increment(points[i]); } Assert.assertEquals(points.length, stat.getN()); double[] mean = stat.getResult(); double[] refMean = new double[] { 1.78, 1.62, 3.12}; for (int i = 0; i < mean.length; ++i) { Assert.assertEquals(refMean[i], mean[i], 1.0e-12); } } @Test public void testSerial() { VectorialMean stat = new VectorialMean(points[0].length); for (int i = 0; i < points.length; ++i) { stat.increment(points[i]); } Assert.assertEquals(stat, TestUtils.serializeAndRecover(stat)); } } ././@LongLink100644 0 0 162 12126630646 10260 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/stat/descriptive/moment/VectorialCovarianceTest.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/stat/descriptive/moment/VectorialCovari100644 1750 1750 6065 12126627674 32405 0ustarlucluc 0 0 //Licensed to the Apache Software Foundation (ASF) under one //or more contributor license agreements. See the NOTICE file //distributed with this work for additional information //regarding copyright ownership. The ASF licenses this file //to you 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. package org.apache.commons.math3.stat.descriptive.moment; import org.apache.commons.math3.exception.DimensionMismatchException; import org.apache.commons.math3.TestUtils; import org.apache.commons.math3.linear.RealMatrix; import org.junit.Test; import org.junit.Assert; public class VectorialCovarianceTest { private double[][] points; public VectorialCovarianceTest() { points = new double[][] { { 1.2, 2.3, 4.5}, {-0.7, 2.3, 5.0}, { 3.1, 0.0, -3.1}, { 6.0, 1.2, 4.2}, {-0.7, 2.3, 5.0} }; } @Test public void testMismatch() { try { new VectorialCovariance(8, true).increment(new double[5]); Assert.fail("an exception should have been thrown"); } catch (DimensionMismatchException dme) { Assert.assertEquals(5, dme.getArgument()); Assert.assertEquals(8, dme.getDimension()); } } @Test public void testSimplistic() { VectorialCovariance stat = new VectorialCovariance(2, true); stat.increment(new double[] {-1.0, 1.0}); stat.increment(new double[] { 1.0, -1.0}); RealMatrix c = stat.getResult(); Assert.assertEquals( 2.0, c.getEntry(0, 0), 1.0e-12); Assert.assertEquals(-2.0, c.getEntry(1, 0), 1.0e-12); Assert.assertEquals( 2.0, c.getEntry(1, 1), 1.0e-12); } @Test public void testBasicStats() { VectorialCovariance stat = new VectorialCovariance(points[0].length, true); for (int i = 0; i < points.length; ++i) { stat.increment(points[i]); } Assert.assertEquals(points.length, stat.getN()); RealMatrix c = stat.getResult(); double[][] refC = new double[][] { { 8.0470, -1.9195, -3.4445}, {-1.9195, 1.0470, 3.2795}, {-3.4445, 3.2795, 12.2070} }; for (int i = 0; i < c.getRowDimension(); ++i) { for (int j = 0; j <= i; ++j) { Assert.assertEquals(refC[i][j], c.getEntry(i, j), 1.0e-12); } } } @Test public void testSerial(){ VectorialCovariance stat = new VectorialCovariance(points[0].length, true); Assert.assertEquals(stat, TestUtils.serializeAndRecover(stat)); } } ././@LongLink100644 0 0 152 12126630646 10257 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/stat/descriptive/moment/FirstMomentTest.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/stat/descriptive/moment/FirstMomentTest100644 1750 1750 3322 12126627674 32411 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.stat.descriptive.moment; import org.apache.commons.math3.stat.descriptive.StorelessUnivariateStatisticAbstractTest; import org.apache.commons.math3.stat.descriptive.UnivariateStatistic; /** * Test cases for the {@link FirstMoment} class. * @version $Id: FirstMomentTest.java 1244107 2012-02-14 16:17:55Z erans $ */ public class FirstMomentTest extends StorelessUnivariateStatisticAbstractTest{ /** descriptive statistic. */ protected FirstMoment stat; /** * @see org.apache.commons.math3.stat.descriptive.UnivariateStatisticAbstractTest#getUnivariateStatistic() */ @Override public UnivariateStatistic getUnivariateStatistic() { return new FirstMoment(); } /** * @see org.apache.commons.math3.stat.descriptive.UnivariateStatisticAbstractTest#expectedValue() */ @Override public double expectedValue() { return this.mean; } } ././@LongLink100644 0 0 147 12126630646 10263 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/stat/descriptive/moment/SkewnessTest.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/stat/descriptive/moment/SkewnessTest.ja100644 1750 1750 4023 12126627674 32334 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.stat.descriptive.moment; import org.apache.commons.math3.stat.descriptive.StorelessUnivariateStatisticAbstractTest; import org.apache.commons.math3.stat.descriptive.UnivariateStatistic; import org.junit.Assert; import org.junit.Test; /** * Test cases for the {@link UnivariateStatistic} class. * * @version $Id: SkewnessTest.java 1244107 2012-02-14 16:17:55Z erans $ */ public class SkewnessTest extends StorelessUnivariateStatisticAbstractTest{ protected Skewness stat; /** * {@inheritDoc} */ @Override public UnivariateStatistic getUnivariateStatistic() { return new Skewness(); } /** * {@inheritDoc} */ @Override public double expectedValue() { return this.skew; } /** * Make sure Double.NaN is returned iff n < 3 * */ @Test public void testNaN() { Skewness skew = new Skewness(); Assert.assertTrue(Double.isNaN(skew.getResult())); skew.increment(1d); Assert.assertTrue(Double.isNaN(skew.getResult())); skew.increment(1d); Assert.assertTrue(Double.isNaN(skew.getResult())); skew.increment(1d); Assert.assertFalse(Double.isNaN(skew.getResult())); } } ././@LongLink100644 0 0 154 12126630646 10261 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/stat/descriptive/moment/GeometricMeanTest.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/stat/descriptive/moment/GeometricMeanTe100644 1750 1750 5106 12126627674 32314 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.stat.descriptive.moment; import org.apache.commons.math3.stat.descriptive.StorelessUnivariateStatisticAbstractTest; import org.apache.commons.math3.stat.descriptive.UnivariateStatistic; import org.junit.Assert; import org.junit.Test; /** * Test cases for the {@link UnivariateStatistic} class. * @version $Id: GeometricMeanTest.java 1244107 2012-02-14 16:17:55Z erans $ */ public class GeometricMeanTest extends StorelessUnivariateStatisticAbstractTest{ protected GeometricMean stat; /** * {@inheritDoc} */ @Override public UnivariateStatistic getUnivariateStatistic() { return new GeometricMean(); } /** * {@inheritDoc} */ @Override public double expectedValue() { return this.geoMean; } @Test public void testSpecialValues() { GeometricMean mean = new GeometricMean(); // empty Assert.assertTrue(Double.isNaN(mean.getResult())); // finite data mean.increment(1d); Assert.assertFalse(Double.isNaN(mean.getResult())); // add 0 -- makes log sum blow to minus infinity, should make 0 mean.increment(0d); Assert.assertEquals(0d, mean.getResult(), 0); // add positive infinity - note the minus infinity above mean.increment(Double.POSITIVE_INFINITY); Assert.assertTrue(Double.isNaN(mean.getResult())); // clear mean.clear(); Assert.assertTrue(Double.isNaN(mean.getResult())); // positive infinity by itself mean.increment(Double.POSITIVE_INFINITY); Assert.assertEquals(Double.POSITIVE_INFINITY, mean.getResult(), 0); // negative value -- should make NaN mean.increment(-2d); Assert.assertTrue(Double.isNaN(mean.getResult())); } } ././@LongLink100644 0 0 152 12126630646 10257 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/stat/descriptive/moment/ThirdMomentTest.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/stat/descriptive/moment/ThirdMomentTest100644 1750 1750 3327 12126627674 32401 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.stat.descriptive.moment; import org.apache.commons.math3.stat.descriptive.StorelessUnivariateStatisticAbstractTest; import org.apache.commons.math3.stat.descriptive.UnivariateStatistic; /** * Test cases for the {@link ThirdMoment} class. * @version $Id: ThirdMomentTest.java 1244107 2012-02-14 16:17:55Z erans $ */ public class ThirdMomentTest extends StorelessUnivariateStatisticAbstractTest{ /** descriptive statistic. */ protected ThirdMoment stat; /** * @see org.apache.commons.math3.stat.descriptive.UnivariateStatisticAbstractTest#getUnivariateStatistic() */ @Override public UnivariateStatistic getUnivariateStatistic() { return new ThirdMoment(); } /** * @see org.apache.commons.math3.stat.descriptive.UnivariateStatisticAbstractTest#expectedValue() */ @Override public double expectedValue() { return this.thirdMoment; } } commons-math3-3.2-src/src/test/java/org/apache/commons/math3/stat/descriptive/moment/MeanTest.java100644 1750 1750 4421 12126627674 31743 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.stat.descriptive.moment; import org.apache.commons.math3.stat.descriptive.StorelessUnivariateStatisticAbstractTest; import org.apache.commons.math3.stat.descriptive.UnivariateStatistic; import org.junit.Assert; import org.junit.Test; /** * Test cases for the {@link UnivariateStatistic} class. * @version $Id: MeanTest.java 1244107 2012-02-14 16:17:55Z erans $ */ public class MeanTest extends StorelessUnivariateStatisticAbstractTest{ protected Mean stat; /** * {@inheritDoc} */ @Override public UnivariateStatistic getUnivariateStatistic() { return new Mean(); } /** * {@inheritDoc} */ @Override public double expectedValue() { return this.mean; } /**Expected value for the testArray defined in UnivariateStatisticAbstractTest */ public double expectedWeightedValue() { return this.weightedMean; } @Test public void testSmallSamples() { Mean mean = new Mean(); Assert.assertTrue(Double.isNaN(mean.getResult())); mean.increment(1d); Assert.assertEquals(1d, mean.getResult(), 0); } @Test public void testWeightedMean() { Mean mean = new Mean(); Assert.assertEquals(expectedWeightedValue(), mean.evaluate(testArray, testWeightsArray, 0, testArray.length), getTolerance()); Assert.assertEquals(expectedValue(), mean.evaluate(testArray, identicalWeightsArray, 0, testArray.length), getTolerance()); } } ././@LongLink100644 0 0 160 12126630646 10256 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/stat/descriptive/moment/StandardDeviationTest.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/stat/descriptive/moment/StandardDeviati100644 1750 1750 6270 12126627674 32355 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.stat.descriptive.moment; import org.apache.commons.math3.stat.descriptive.StorelessUnivariateStatisticAbstractTest; import org.apache.commons.math3.stat.descriptive.UnivariateStatistic; import org.apache.commons.math3.util.FastMath; import org.junit.Assert; import org.junit.Test; /** * Test cases for the {@link UnivariateStatistic} class. * * @version $Id: StandardDeviationTest.java 1244107 2012-02-14 16:17:55Z erans $ */ public class StandardDeviationTest extends StorelessUnivariateStatisticAbstractTest{ protected StandardDeviation stat; /** * {@inheritDoc} */ @Override public UnivariateStatistic getUnivariateStatistic() { return new StandardDeviation(); } /** * {@inheritDoc} */ @Override public double expectedValue() { return this.std; } /** * Make sure Double.NaN is returned iff n = 0 * */ @Test public void testNaN() { StandardDeviation std = new StandardDeviation(); Assert.assertTrue(Double.isNaN(std.getResult())); std.increment(1d); Assert.assertEquals(0d, std.getResult(), 0); } /** * Test population version of variance */ @Test public void testPopulation() { double[] values = {-1.0d, 3.1d, 4.0d, -2.1d, 22d, 11.7d, 3d, 14d}; double sigma = populationStandardDeviation(values); SecondMoment m = new SecondMoment(); m.evaluate(values); // side effect is to add values StandardDeviation s1 = new StandardDeviation(); s1.setBiasCorrected(false); Assert.assertEquals(sigma, s1.evaluate(values), 1E-14); s1.incrementAll(values); Assert.assertEquals(sigma, s1.getResult(), 1E-14); s1 = new StandardDeviation(false, m); Assert.assertEquals(sigma, s1.getResult(), 1E-14); s1 = new StandardDeviation(false); Assert.assertEquals(sigma, s1.evaluate(values), 1E-14); s1.incrementAll(values); Assert.assertEquals(sigma, s1.getResult(), 1E-14); } /** * Definitional formula for population standard deviation */ protected double populationStandardDeviation(double[] v) { double mean = new Mean().evaluate(v); double sum = 0; for (int i = 0; i < v.length; i++) { sum += (v[i] - mean) * (v[i] - mean); } return FastMath.sqrt(sum / v.length); } } ././@LongLink100644 0 0 153 12126630646 10260 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/stat/descriptive/moment/SecondMomentTest.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/stat/descriptive/moment/SecondMomentTes100644 1750 1750 3340 12126627674 32351 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.stat.descriptive.moment; import org.apache.commons.math3.stat.descriptive.StorelessUnivariateStatisticAbstractTest; import org.apache.commons.math3.stat.descriptive.UnivariateStatistic; /** * Test cases for the {@link SecondMoment} class. * @version $Id: SecondMomentTest.java 1244107 2012-02-14 16:17:55Z erans $ */ public class SecondMomentTest extends StorelessUnivariateStatisticAbstractTest { /** descriptive statistic. */ protected SecondMoment stat; /** * @see org.apache.commons.math3.stat.descriptive.UnivariateStatisticAbstractTest#getUnivariateStatistic() */ @Override public UnivariateStatistic getUnivariateStatistic() { return new SecondMoment(); } /** * @see org.apache.commons.math3.stat.descriptive.UnivariateStatisticAbstractTest#expectedValue() */ @Override public double expectedValue() { return this.secondMoment; } } ././@LongLink100644 0 0 147 12126630646 10263 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/stat/descriptive/moment/VarianceTest.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/stat/descriptive/moment/VarianceTest.ja100644 1750 1750 10172 12126627674 32304 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.stat.descriptive.moment; import org.apache.commons.math3.stat.descriptive.StorelessUnivariateStatisticAbstractTest; import org.apache.commons.math3.stat.descriptive.UnivariateStatistic; import org.apache.commons.math3.util.MathArrays; import org.junit.Assert; import org.junit.Test; /** * Test cases for the {@link UnivariateStatistic} class. * * @version $Id: VarianceTest.java 1244107 2012-02-14 16:17:55Z erans $ */ public class VarianceTest extends StorelessUnivariateStatisticAbstractTest{ protected Variance stat; /** * {@inheritDoc} */ @Override public UnivariateStatistic getUnivariateStatistic() { return new Variance(); } /** * {@inheritDoc} */ @Override public double expectedValue() { return this.var; } /**Expected value for the testArray defined in UnivariateStatisticAbstractTest */ public double expectedWeightedValue() { return this.weightedVar; } /** * Make sure Double.NaN is returned iff n = 0 * */ @Test public void testNaN() { StandardDeviation std = new StandardDeviation(); Assert.assertTrue(Double.isNaN(std.getResult())); std.increment(1d); Assert.assertEquals(0d, std.getResult(), 0); } /** * Test population version of variance */ @Test public void testPopulation() { double[] values = {-1.0d, 3.1d, 4.0d, -2.1d, 22d, 11.7d, 3d, 14d}; SecondMoment m = new SecondMoment(); m.evaluate(values); // side effect is to add values Variance v1 = new Variance(); v1.setBiasCorrected(false); Assert.assertEquals(populationVariance(values), v1.evaluate(values), 1E-14); v1.incrementAll(values); Assert.assertEquals(populationVariance(values), v1.getResult(), 1E-14); v1 = new Variance(false, m); Assert.assertEquals(populationVariance(values), v1.getResult(), 1E-14); v1 = new Variance(false); Assert.assertEquals(populationVariance(values), v1.evaluate(values), 1E-14); v1.incrementAll(values); Assert.assertEquals(populationVariance(values), v1.getResult(), 1E-14); } /** * Definitional formula for population variance */ protected double populationVariance(double[] v) { double mean = new Mean().evaluate(v); double sum = 0; for (int i = 0; i < v.length; i++) { sum += (v[i] - mean) * (v[i] - mean); } return sum / v.length; } @Test public void testWeightedVariance() { Variance variance = new Variance(); Assert.assertEquals(expectedWeightedValue(), variance.evaluate(testArray, testWeightsArray, 0, testArray.length), getTolerance()); // All weights = 1 -> weighted variance = unweighted variance Assert.assertEquals(expectedValue(), variance.evaluate(testArray, unitWeightsArray, 0, testArray.length), getTolerance()); // All weights the same -> when weights are normalized to sum to the length of the values array, // weighted variance = unweighted value Assert.assertEquals(expectedValue(), variance.evaluate(testArray, MathArrays.normalizeArray(identicalWeightsArray, testArray.length), 0, testArray.length), getTolerance()); } } ././@LongLink100644 0 0 152 12126630646 10257 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/stat/descriptive/moment/InteractionTest.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/stat/descriptive/moment/InteractionTest100644 1750 1750 4341 12126627674 32423 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.stat.descriptive.moment; import org.junit.Assert; import org.junit.Test; /** * @version $Id: InteractionTest.java 1244107 2012-02-14 16:17:55Z erans $ */ public class InteractionTest { protected double mean = 12.40454545454550; protected double var = 10.00235930735930; protected double skew = 1.437423729196190; protected double kurt = 2.377191264804700; protected double tolerance = 10E-12; protected double[] testArray = { 12.5, 12, 11.8, 14.2, 14.9, 14.5, 21, 8.2, 10.3, 11.3, 14.1, 9.9, 12.2, 12, 12.1, 11, 19.8, 11, 10, 8.8, 9, 12.3 }; @Test public void testInteraction() { FourthMoment m4 = new FourthMoment(); Mean m = new Mean(m4); Variance v = new Variance(m4); Skewness s= new Skewness(m4); Kurtosis k = new Kurtosis(m4); for (int i = 0; i < testArray.length; i++){ m4.increment(testArray[i]); } Assert.assertEquals(mean,m.getResult(),tolerance); Assert.assertEquals(var,v.getResult(),tolerance); Assert.assertEquals(skew ,s.getResult(),tolerance); Assert.assertEquals(kurt,k.getResult(),tolerance); } } ././@LongLink100644 0 0 153 12126630646 10260 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/stat/descriptive/moment/SemiVarianceTest.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/stat/descriptive/moment/SemiVarianceTes100644 1750 1750 14517 12126627674 32354 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.stat.descriptive.moment; import org.apache.commons.math3.TestUtils; import org.apache.commons.math3.stat.StatUtils; import org.apache.commons.math3.exception.MathIllegalArgumentException; import org.junit.Assert; import org.junit.Test; public class SemiVarianceTest { @Test public void testInsufficientData() { double[] nothing = null; SemiVariance sv = new SemiVariance(); try { sv.evaluate(nothing); Assert.fail("null is not a valid data array."); } catch (MathIllegalArgumentException iae) { } try { sv.setVarianceDirection(SemiVariance.UPSIDE_VARIANCE); sv.evaluate(nothing); Assert.fail("null is not a valid data array."); } catch (MathIllegalArgumentException iae) { } nothing = new double[] {}; Assert.assertTrue(Double.isNaN(sv.evaluate(nothing))); } @Test public void testSingleDown() { SemiVariance sv = new SemiVariance(); double[] values = { 50.0d }; double singletest = sv.evaluate(values); Assert.assertEquals(0.0d, singletest, 0); } @Test public void testSingleUp() { SemiVariance sv = new SemiVariance(SemiVariance.UPSIDE_VARIANCE); double[] values = { 50.0d }; double singletest = sv.evaluate(values); Assert.assertEquals(0.0d, singletest, 0); } @Test public void testSample() { final double[] values = { -2.0d, 2.0d, 4.0d, -2.0d, 22.0d, 11.0d, 3.0d, 14.0d, 5.0d }; final int length = values.length; final double mean = StatUtils.mean(values); // 6.333... final SemiVariance sv = new SemiVariance(); // Default bias correction is true final double downsideSemiVariance = sv.evaluate(values); // Downside is the default Assert.assertEquals(TestUtils.sumSquareDev(new double[] {-2d, 2d, 4d, -2d, 3d, 5d}, mean) / (length - 1), downsideSemiVariance, 1E-14); sv.setVarianceDirection(SemiVariance.UPSIDE_VARIANCE); final double upsideSemiVariance = sv.evaluate(values); Assert.assertEquals(TestUtils.sumSquareDev(new double[] {22d, 11d, 14d}, mean) / (length - 1), upsideSemiVariance, 1E-14); // Verify that upper + lower semivariance against the mean sum to variance Assert.assertEquals(StatUtils.variance(values), downsideSemiVariance + upsideSemiVariance, 10e-12); } @Test public void testPopulation() { double[] values = { -2.0d, 2.0d, 4.0d, -2.0d, 22.0d, 11.0d, 3.0d, 14.0d, 5.0d }; SemiVariance sv = new SemiVariance(false); double singletest = sv.evaluate(values); Assert.assertEquals(19.556d, singletest, 0.01d); sv.setVarianceDirection(SemiVariance.UPSIDE_VARIANCE); singletest = sv.evaluate(values); Assert.assertEquals(36.222d, singletest, 0.01d); } @Test public void testNonMeanCutoffs() { double[] values = { -2.0d, 2.0d, 4.0d, -2.0d, 22.0d, 11.0d, 3.0d, 14.0d, 5.0d }; SemiVariance sv = new SemiVariance(false); // Turn off bias correction - use df = length double singletest = sv.evaluate(values, 1.0d, SemiVariance.DOWNSIDE_VARIANCE, false, 0, values.length); Assert.assertEquals(TestUtils.sumSquareDev(new double[] { -2d, -2d }, 1.0d) / values.length, singletest, 0.01d); singletest = sv.evaluate(values, 3.0d, SemiVariance.UPSIDE_VARIANCE, false, 0, values.length); Assert.assertEquals(TestUtils.sumSquareDev(new double[] { 4d, 22d, 11d, 14d, 5d }, 3.0d) / values.length, singletest, 0.01d); } /** * Check that the lower + upper semivariance against the mean sum to the * variance. */ @Test public void testVarianceDecompMeanCutoff() { double[] values = { -2.0d, 2.0d, 4.0d, -2.0d, 22.0d, 11.0d, 3.0d, 14.0d, 5.0d }; double variance = StatUtils.variance(values); SemiVariance sv = new SemiVariance(true); // Bias corrected sv.setVarianceDirection(SemiVariance.DOWNSIDE_VARIANCE); final double lower = sv.evaluate(values); sv.setVarianceDirection(SemiVariance.UPSIDE_VARIANCE); final double upper = sv.evaluate(values); Assert.assertEquals(variance, lower + upper, 10e-12); } /** * Check that upper and lower semivariances against a cutoff sum to the sum * of squared deviations of the full set of values against the cutoff * divided by df = length - 1 (assuming bias-corrected). */ @Test public void testVarianceDecompNonMeanCutoff() { double[] values = { -2.0d, 2.0d, 4.0d, -2.0d, 22.0d, 11.0d, 3.0d, 14.0d, 5.0d }; double target = 0; double totalSumOfSquares = TestUtils.sumSquareDev(values, target); SemiVariance sv = new SemiVariance(true); // Bias corrected sv.setVarianceDirection(SemiVariance.DOWNSIDE_VARIANCE); double lower = sv.evaluate(values, target); sv.setVarianceDirection(SemiVariance.UPSIDE_VARIANCE); double upper = sv.evaluate(values, target); Assert.assertEquals(totalSumOfSquares / (values.length - 1), lower + upper, 10e-12); } @Test public void testNoVariance() { final double[] values = {100d, 100d, 100d, 100d}; SemiVariance sv = new SemiVariance(); Assert.assertEquals(0, sv.evaluate(values), 10E-12); Assert.assertEquals(0, sv.evaluate(values, 100d), 10E-12); Assert.assertEquals(0, sv.evaluate(values, 100d, SemiVariance.UPSIDE_VARIANCE, false, 0, values.length), 10E-12); } } ././@LongLink100644 0 0 165 12126630646 10263 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/stat/descriptive/SynchronizedSummaryStatisticsTest.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/stat/descriptive/SynchronizedSummarySta100644 1750 1750 2343 12126627674 32512 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with this * work for additional information regarding copyright ownership. The ASF * licenses this file to You 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. */ package org.apache.commons.math3.stat.descriptive; /** * Test cases for the {@link SynchronizedSummaryStatisticsTest} class. * @version $Id: SynchronizedSummaryStatisticsTest.java 1244107 2012-02-14 16:17:55Z erans $ * 2007) $ */ public final class SynchronizedSummaryStatisticsTest extends SummaryStatisticsTest { @Override protected SummaryStatistics createSummaryStatistics() { return new SynchronizedSummaryStatistics(); } } ././@LongLink100644 0 0 146 12126630646 10262 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/stat/descriptive/ListUnivariateImpl.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/stat/descriptive/ListUnivariateImpl.jav100644 1750 1750 13674 12126627674 32402 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.stat.descriptive; import java.io.Serializable; import java.util.ArrayList; import java.util.List; import org.apache.commons.math3.exception.MathIllegalArgumentException; import org.apache.commons.math3.stat.descriptive.UnivariateStatistic; import org.apache.commons.math3.stat.descriptive.DescriptiveStatistics; import org.apache.commons.math3.util.DefaultTransformer; import org.apache.commons.math3.util.FastMath; import org.apache.commons.math3.util.NumberTransformer; /** * @version $Id: ListUnivariateImpl.java 1334421 2012-05-05 13:41:04Z tn $ */ public class ListUnivariateImpl extends DescriptiveStatistics implements Serializable { /** Serializable version identifier */ private static final long serialVersionUID = -8837442489133392138L; /** * Holds a reference to a list - GENERICs are going to make * our lives easier here as we could only accept List */ protected List list; /** Number Transformer maps Objects to Number for us. */ protected NumberTransformer transformer; /** * No argument Constructor */ public ListUnivariateImpl(){ this(new ArrayList()); } /** * Construct a ListUnivariate with a specific List. * @param list The list that will back this DescriptiveStatistics */ public ListUnivariateImpl(List list) { this(list, new DefaultTransformer()); } /** * Construct a ListUnivariate with a specific List. * @param list The list that will back this DescriptiveStatistics * @param transformer the number transformer used to convert the list items. */ public ListUnivariateImpl(List list, NumberTransformer transformer) { super(); this.list = list; this.transformer = transformer; } /** {@inheritDoc} */ @Override public double[] getValues() { int length = list.size(); // If the window size is not INFINITE_WINDOW AND // the current list is larger that the window size, we need to // take into account only the last n elements of the list // as definied by windowSize final int wSize = getWindowSize(); if (wSize != DescriptiveStatistics.INFINITE_WINDOW && wSize < list.size()) { length = list.size() - FastMath.max(0, list.size() - wSize); } // Create an array to hold all values double[] copiedArray = new double[length]; for (int i = 0; i < copiedArray.length; i++) { copiedArray[i] = getElement(i); } return copiedArray; } /** {@inheritDoc} */ @Override public double getElement(int index) { double value = Double.NaN; int calcIndex = index; final int wSize = getWindowSize(); if (wSize != DescriptiveStatistics.INFINITE_WINDOW && wSize < list.size()) { calcIndex = (list.size() - wSize) + index; } try { value = transformer.transform(list.get(calcIndex)); } catch (MathIllegalArgumentException e) { e.printStackTrace(); } return value; } /** {@inheritDoc} */ @Override public long getN() { int n = 0; final int wSize = getWindowSize(); if (wSize != DescriptiveStatistics.INFINITE_WINDOW) { if (list.size() > wSize) { n = wSize; } else { n = list.size(); } } else { n = list.size(); } return n; } /** {@inheritDoc} */ @Override public void addValue(double v) { list.add(Double.valueOf(v)); } /** * Adds an object to this list. * @param o Object to add to the list */ public void addObject(Object o) { list.add(o); } /** * Clears all statistics. *

                          * N.B.: This method has the side effect of clearing the underlying list. */ @Override public void clear() { list.clear(); } /** * Apply the given statistic to this univariate collection. * @param stat the statistic to apply * @return the computed value of the statistic. */ @Override public double apply(UnivariateStatistic stat) { double[] v = this.getValues(); if (v != null) { return stat.evaluate(v, 0, v.length); } return Double.NaN; } /** * Access the number transformer. * @return the number transformer. */ public NumberTransformer getTransformer() { return transformer; } /** * Modify the number transformer. * @param transformer the new number transformer. */ public void setTransformer(NumberTransformer transformer) { this.transformer = transformer; } /** {@inheritDoc} */ @Override public void setWindowSize(int windowSize) { super.setWindowSize(windowSize); //Discard elements from the front of the list if the windowSize is less than // the size of the list. int extra = list.size() - windowSize; for (int i = 0; i < extra; i++) { list.remove(0); } } } commons-math3-3.2-src/src/test/java/org/apache/commons/math3/stat/correlation/CovarianceTest.java100644 1750 1750 24616 12126627674 31666 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.stat.correlation; import org.apache.commons.math3.TestUtils; import org.apache.commons.math3.exception.NotStrictlyPositiveException; import org.apache.commons.math3.linear.RealMatrix; import org.apache.commons.math3.linear.Array2DRowRealMatrix; import org.apache.commons.math3.stat.descriptive.moment.Variance; import org.junit.Assert; import org.junit.Test; public class CovarianceTest { protected final double[] longleyData = new double[] { 60323,83.0,234289,2356,1590,107608,1947, 61122,88.5,259426,2325,1456,108632,1948, 60171,88.2,258054,3682,1616,109773,1949, 61187,89.5,284599,3351,1650,110929,1950, 63221,96.2,328975,2099,3099,112075,1951, 63639,98.1,346999,1932,3594,113270,1952, 64989,99.0,365385,1870,3547,115094,1953, 63761,100.0,363112,3578,3350,116219,1954, 66019,101.2,397469,2904,3048,117388,1955, 67857,104.6,419180,2822,2857,118734,1956, 68169,108.4,442769,2936,2798,120445,1957, 66513,110.8,444546,4681,2637,121950,1958, 68655,112.6,482704,3813,2552,123366,1959, 69564,114.2,502601,3931,2514,125368,1960, 69331,115.7,518173,4806,2572,127852,1961, 70551,116.9,554894,4007,2827,130081,1962 }; protected final double[] swissData = new double[] { 80.2,17.0,15,12,9.96, 83.1,45.1,6,9,84.84, 92.5,39.7,5,5,93.40, 85.8,36.5,12,7,33.77, 76.9,43.5,17,15,5.16, 76.1,35.3,9,7,90.57, 83.8,70.2,16,7,92.85, 92.4,67.8,14,8,97.16, 82.4,53.3,12,7,97.67, 82.9,45.2,16,13,91.38, 87.1,64.5,14,6,98.61, 64.1,62.0,21,12,8.52, 66.9,67.5,14,7,2.27, 68.9,60.7,19,12,4.43, 61.7,69.3,22,5,2.82, 68.3,72.6,18,2,24.20, 71.7,34.0,17,8,3.30, 55.7,19.4,26,28,12.11, 54.3,15.2,31,20,2.15, 65.1,73.0,19,9,2.84, 65.5,59.8,22,10,5.23, 65.0,55.1,14,3,4.52, 56.6,50.9,22,12,15.14, 57.4,54.1,20,6,4.20, 72.5,71.2,12,1,2.40, 74.2,58.1,14,8,5.23, 72.0,63.5,6,3,2.56, 60.5,60.8,16,10,7.72, 58.3,26.8,25,19,18.46, 65.4,49.5,15,8,6.10, 75.5,85.9,3,2,99.71, 69.3,84.9,7,6,99.68, 77.3,89.7,5,2,100.00, 70.5,78.2,12,6,98.96, 79.4,64.9,7,3,98.22, 65.0,75.9,9,9,99.06, 92.2,84.6,3,3,99.46, 79.3,63.1,13,13,96.83, 70.4,38.4,26,12,5.62, 65.7,7.7,29,11,13.79, 72.7,16.7,22,13,11.22, 64.4,17.6,35,32,16.92, 77.6,37.6,15,7,4.97, 67.6,18.7,25,7,8.65, 35.0,1.2,37,53,42.34, 44.7,46.6,16,29,50.43, 42.8,27.7,22,29,58.33 }; /** * Test Longley dataset against R. * Data Source: J. Longley (1967) "An Appraisal of Least Squares * Programs for the Electronic Computer from the Point of View of the User" * Journal of the American Statistical Association, vol. 62. September, * pp. 819-841. * * Data are from NIST: * http://www.itl.nist.gov/div898/strd/lls/data/LINKS/DATA/Longley.dat */ @Test public void testLongly() { RealMatrix matrix = createRealMatrix(longleyData, 16, 7); RealMatrix covarianceMatrix = new Covariance(matrix).getCovarianceMatrix(); double[] rData = new double[] { 12333921.73333333246, 3.679666000000000e+04, 343330206.333333313, 1649102.666666666744, 1117681.066666666651, 23461965.733333334, 16240.93333333333248, 36796.66000000000, 1.164576250000000e+02, 1063604.115416667, 6258.666250000000, 3490.253750000000, 73503.000000000, 50.92333333333334, 343330206.33333331347, 1.063604115416667e+06, 9879353659.329166412, 56124369.854166664183, 30880428.345833335072, 685240944.600000024, 470977.90000000002328, 1649102.66666666674, 6.258666250000000e+03, 56124369.854166664, 873223.429166666698, -115378.762499999997, 4462741.533333333, 2973.03333333333330, 1117681.06666666665, 3.490253750000000e+03, 30880428.345833335, -115378.762499999997, 484304.095833333326, 1764098.133333333, 1382.43333333333339, 23461965.73333333433, 7.350300000000000e+04, 685240944.600000024, 4462741.533333333209, 1764098.133333333302, 48387348.933333330, 32917.40000000000146, 16240.93333333333, 5.092333333333334e+01, 470977.900000000, 2973.033333333333, 1382.433333333333, 32917.40000000, 22.66666666666667 }; TestUtils.assertEquals("covariance matrix", createRealMatrix(rData, 7, 7), covarianceMatrix, 10E-9); } /** * Test R Swiss fertility dataset against R. * Data Source: R datasets package */ @Test public void testSwissFertility() { RealMatrix matrix = createRealMatrix(swissData, 47, 5); RealMatrix covarianceMatrix = new Covariance(matrix).getCovarianceMatrix(); double[] rData = new double[] { 156.0424976873265, 100.1691489361702, -64.36692876965772, -79.7295097132285, 241.5632030527289, 100.169148936170251, 515.7994172062905, -124.39283071230344, -139.6574005550416, 379.9043755781684, -64.3669287696577, -124.3928307123034, 63.64662349676226, 53.5758556891767, -190.5606105457909, -79.7295097132285, -139.6574005550416, 53.57585568917669, 92.4560592044403, -61.6988297872340, 241.5632030527289, 379.9043755781684, -190.56061054579092, -61.6988297872340, 1739.2945371877890 }; TestUtils.assertEquals("covariance matrix", createRealMatrix(rData, 5, 5), covarianceMatrix, 10E-13); } /** * Constant column */ @Test public void testConstant() { double[] noVariance = new double[] {1, 1, 1, 1}; double[] values = new double[] {1, 2, 3, 4}; Assert.assertEquals(0d, new Covariance().covariance(noVariance, values, true), Double.MIN_VALUE); Assert.assertEquals(0d, new Covariance().covariance(noVariance, noVariance, true), Double.MIN_VALUE); } /** * One column */ @Test public void testOneColumn() { RealMatrix cov = new Covariance(new double[][] {{1}, {2}}, false).getCovarianceMatrix(); Assert.assertEquals(1, cov.getRowDimension()); Assert.assertEquals(1, cov.getColumnDimension()); Assert.assertEquals(0.25, cov.getEntry(0, 0), 1.0e-15); } /** * Insufficient data */ @Test public void testInsufficientData() { double[] one = new double[] {1}; double[] two = new double[] {2}; try { new Covariance().covariance(one, two, false); Assert.fail("Expecting IllegalArgumentException"); } catch (IllegalArgumentException ex) { // Expected } try { new Covariance(new double[][] {{},{}}); Assert.fail("Expecting NotStrictlyPositiveException"); } catch (NotStrictlyPositiveException ex) { // Expected } } /** * Verify that diagonal entries are consistent with Variance computation and matrix matches * column-by-column covariances */ @Test public void testConsistency() { final RealMatrix matrix = createRealMatrix(swissData, 47, 5); final RealMatrix covarianceMatrix = new Covariance(matrix).getCovarianceMatrix(); // Variances on the diagonal Variance variance = new Variance(); for (int i = 0; i < 5; i++) { Assert.assertEquals(variance.evaluate(matrix.getColumn(i)), covarianceMatrix.getEntry(i,i), 10E-14); } // Symmetry, column-consistency Assert.assertEquals(covarianceMatrix.getEntry(2, 3), new Covariance().covariance(matrix.getColumn(2), matrix.getColumn(3), true), 10E-14); Assert.assertEquals(covarianceMatrix.getEntry(2, 3), covarianceMatrix.getEntry(3, 2), Double.MIN_VALUE); // All columns same -> all entries = column variance RealMatrix repeatedColumns = new Array2DRowRealMatrix(47, 3); for (int i = 0; i < 3; i++) { repeatedColumns.setColumnMatrix(i, matrix.getColumnMatrix(0)); } RealMatrix repeatedCovarianceMatrix = new Covariance(repeatedColumns).getCovarianceMatrix(); double columnVariance = variance.evaluate(matrix.getColumn(0)); for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { Assert.assertEquals(columnVariance, repeatedCovarianceMatrix.getEntry(i, j), 10E-14); } } // Check bias-correction defaults double[][] data = matrix.getData(); TestUtils.assertEquals("Covariances", covarianceMatrix, new Covariance().computeCovarianceMatrix(data),Double.MIN_VALUE); TestUtils.assertEquals("Covariances", covarianceMatrix, new Covariance().computeCovarianceMatrix(data, true),Double.MIN_VALUE); double[] x = data[0]; double[] y = data[1]; Assert.assertEquals(new Covariance().covariance(x, y), new Covariance().covariance(x, y, true), Double.MIN_VALUE); } protected RealMatrix createRealMatrix(double[] data, int nRows, int nCols) { double[][] matrixData = new double[nRows][nCols]; int ptr = 0; for (int i = 0; i < nRows; i++) { System.arraycopy(data, ptr, matrixData[i], 0, nCols); ptr += nCols; } return new Array2DRowRealMatrix(matrixData); } } ././@LongLink100644 0 0 153 12126630646 10260 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/stat/correlation/PearsonsCorrelationTest.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/stat/correlation/PearsonsCorrelationTes100644 1750 1750 33304 12126627674 32476 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.stat.correlation; import org.apache.commons.math3.TestUtils; import org.apache.commons.math3.distribution.TDistribution; import org.apache.commons.math3.linear.RealMatrix; import org.apache.commons.math3.linear.BlockRealMatrix; import org.apache.commons.math3.util.FastMath; import org.junit.Assert; import org.junit.Test; public class PearsonsCorrelationTest { protected final double[] longleyData = new double[] { 60323,83.0,234289,2356,1590,107608,1947, 61122,88.5,259426,2325,1456,108632,1948, 60171,88.2,258054,3682,1616,109773,1949, 61187,89.5,284599,3351,1650,110929,1950, 63221,96.2,328975,2099,3099,112075,1951, 63639,98.1,346999,1932,3594,113270,1952, 64989,99.0,365385,1870,3547,115094,1953, 63761,100.0,363112,3578,3350,116219,1954, 66019,101.2,397469,2904,3048,117388,1955, 67857,104.6,419180,2822,2857,118734,1956, 68169,108.4,442769,2936,2798,120445,1957, 66513,110.8,444546,4681,2637,121950,1958, 68655,112.6,482704,3813,2552,123366,1959, 69564,114.2,502601,3931,2514,125368,1960, 69331,115.7,518173,4806,2572,127852,1961, 70551,116.9,554894,4007,2827,130081,1962 }; protected final double[] swissData = new double[] { 80.2,17.0,15,12,9.96, 83.1,45.1,6,9,84.84, 92.5,39.7,5,5,93.40, 85.8,36.5,12,7,33.77, 76.9,43.5,17,15,5.16, 76.1,35.3,9,7,90.57, 83.8,70.2,16,7,92.85, 92.4,67.8,14,8,97.16, 82.4,53.3,12,7,97.67, 82.9,45.2,16,13,91.38, 87.1,64.5,14,6,98.61, 64.1,62.0,21,12,8.52, 66.9,67.5,14,7,2.27, 68.9,60.7,19,12,4.43, 61.7,69.3,22,5,2.82, 68.3,72.6,18,2,24.20, 71.7,34.0,17,8,3.30, 55.7,19.4,26,28,12.11, 54.3,15.2,31,20,2.15, 65.1,73.0,19,9,2.84, 65.5,59.8,22,10,5.23, 65.0,55.1,14,3,4.52, 56.6,50.9,22,12,15.14, 57.4,54.1,20,6,4.20, 72.5,71.2,12,1,2.40, 74.2,58.1,14,8,5.23, 72.0,63.5,6,3,2.56, 60.5,60.8,16,10,7.72, 58.3,26.8,25,19,18.46, 65.4,49.5,15,8,6.10, 75.5,85.9,3,2,99.71, 69.3,84.9,7,6,99.68, 77.3,89.7,5,2,100.00, 70.5,78.2,12,6,98.96, 79.4,64.9,7,3,98.22, 65.0,75.9,9,9,99.06, 92.2,84.6,3,3,99.46, 79.3,63.1,13,13,96.83, 70.4,38.4,26,12,5.62, 65.7,7.7,29,11,13.79, 72.7,16.7,22,13,11.22, 64.4,17.6,35,32,16.92, 77.6,37.6,15,7,4.97, 67.6,18.7,25,7,8.65, 35.0,1.2,37,53,42.34, 44.7,46.6,16,29,50.43, 42.8,27.7,22,29,58.33 }; /** * Test Longley dataset against R. */ @Test public void testLongly() { RealMatrix matrix = createRealMatrix(longleyData, 16, 7); PearsonsCorrelation corrInstance = new PearsonsCorrelation(matrix); RealMatrix correlationMatrix = corrInstance.getCorrelationMatrix(); double[] rData = new double[] { 1.000000000000000, 0.9708985250610560, 0.9835516111796693, 0.5024980838759942, 0.4573073999764817, 0.960390571594376, 0.9713294591921188, 0.970898525061056, 1.0000000000000000, 0.9915891780247822, 0.6206333925590966, 0.4647441876006747, 0.979163432977498, 0.9911491900672053, 0.983551611179669, 0.9915891780247822, 1.0000000000000000, 0.6042609398895580, 0.4464367918926265, 0.991090069458478, 0.9952734837647849, 0.502498083875994, 0.6206333925590966, 0.6042609398895580, 1.0000000000000000, -0.1774206295018783, 0.686551516365312, 0.6682566045621746, 0.457307399976482, 0.4647441876006747, 0.4464367918926265, -0.1774206295018783, 1.0000000000000000, 0.364416267189032, 0.4172451498349454, 0.960390571594376, 0.9791634329774981, 0.9910900694584777, 0.6865515163653120, 0.3644162671890320, 1.000000000000000, 0.9939528462329257, 0.971329459192119, 0.9911491900672053, 0.9952734837647849, 0.6682566045621746, 0.4172451498349454, 0.993952846232926, 1.0000000000000000 }; TestUtils.assertEquals("correlation matrix", createRealMatrix(rData, 7, 7), correlationMatrix, 10E-15); double[] rPvalues = new double[] { 4.38904690369668e-10, 8.36353208910623e-12, 7.8159700933611e-14, 0.0472894097790304, 0.01030636128354301, 0.01316878049026582, 0.0749178049642416, 0.06971758330341182, 0.0830166169296545, 0.510948586323452, 3.693245043123738e-09, 4.327782576751815e-11, 1.167954621905665e-13, 0.00331028281967516, 0.1652293725106684, 3.95834476307755e-10, 1.114663916723657e-13, 1.332267629550188e-15, 0.00466039138541463, 0.1078477071581498, 7.771561172376096e-15 }; RealMatrix rPMatrix = createLowerTriangularRealMatrix(rPvalues, 7); fillUpper(rPMatrix, 0d); TestUtils.assertEquals("correlation p values", rPMatrix, corrInstance.getCorrelationPValues(), 10E-15); } /** * Test R Swiss fertility dataset against R. */ @Test public void testSwissFertility() { RealMatrix matrix = createRealMatrix(swissData, 47, 5); PearsonsCorrelation corrInstance = new PearsonsCorrelation(matrix); RealMatrix correlationMatrix = corrInstance.getCorrelationMatrix(); double[] rData = new double[] { 1.0000000000000000, 0.3530791836199747, -0.6458827064572875, -0.6637888570350691, 0.4636847006517939, 0.3530791836199747, 1.0000000000000000,-0.6865422086171366, -0.6395225189483201, 0.4010950530487398, -0.6458827064572875, -0.6865422086171366, 1.0000000000000000, 0.6984152962884830, -0.5727418060641666, -0.6637888570350691, -0.6395225189483201, 0.6984152962884830, 1.0000000000000000, -0.1538589170909148, 0.4636847006517939, 0.4010950530487398, -0.5727418060641666, -0.1538589170909148, 1.0000000000000000 }; TestUtils.assertEquals("correlation matrix", createRealMatrix(rData, 5, 5), correlationMatrix, 10E-15); double[] rPvalues = new double[] { 0.01491720061472623, 9.45043734069043e-07, 9.95151527133974e-08, 3.658616965962355e-07, 1.304590105694471e-06, 4.811397236181847e-08, 0.001028523190118147, 0.005204433539191644, 2.588307925380906e-05, 0.301807756132683 }; RealMatrix rPMatrix = createLowerTriangularRealMatrix(rPvalues, 5); fillUpper(rPMatrix, 0d); TestUtils.assertEquals("correlation p values", rPMatrix, corrInstance.getCorrelationPValues(), 10E-15); } /** * Test p-value near 0. JIRA: MATH-371 */ @Test public void testPValueNearZero() { /* * Create a dataset that has r -> 1, p -> 0 as dimension increases. * Prior to the fix for MATH-371, p vanished for dimension >= 14. * Post fix, p-values diminish smoothly, vanishing at dimension = 127. * Tested value is ~1E-303. */ int dimension = 120; double[][] data = new double[dimension][2]; for (int i = 0; i < dimension; i++) { data[i][0] = i; data[i][1] = i + 1/((double)i + 1); } PearsonsCorrelation corrInstance = new PearsonsCorrelation(data); Assert.assertTrue(corrInstance.getCorrelationPValues().getEntry(0, 1) > 0); } /** * Constant column */ @Test public void testConstant() { double[] noVariance = new double[] {1, 1, 1, 1}; double[] values = new double[] {1, 2, 3, 4}; Assert.assertTrue(Double.isNaN(new PearsonsCorrelation().correlation(noVariance, values))); } /** * Insufficient data */ @Test public void testInsufficientData() { double[] one = new double[] {1}; double[] two = new double[] {2}; try { new PearsonsCorrelation().correlation(one, two); Assert.fail("Expecting IllegalArgumentException"); } catch (IllegalArgumentException ex) { // Expected } RealMatrix matrix = new BlockRealMatrix(new double[][] {{0},{1}}); try { new PearsonsCorrelation(matrix); Assert.fail("Expecting IllegalArgumentException"); } catch (IllegalArgumentException ex) { // Expected } } /** * Verify that direct t-tests using standard error estimates are consistent * with reported p-values */ @Test public void testStdErrorConsistency() { TDistribution tDistribution = new TDistribution(45); RealMatrix matrix = createRealMatrix(swissData, 47, 5); PearsonsCorrelation corrInstance = new PearsonsCorrelation(matrix); RealMatrix rValues = corrInstance.getCorrelationMatrix(); RealMatrix pValues = corrInstance.getCorrelationPValues(); RealMatrix stdErrors = corrInstance.getCorrelationStandardErrors(); for (int i = 0; i < 5; i++) { for (int j = 0; j < i; j++) { double t = FastMath.abs(rValues.getEntry(i, j)) / stdErrors.getEntry(i, j); double p = 2 * (1 - tDistribution.cumulativeProbability(t)); Assert.assertEquals(p, pValues.getEntry(i, j), 10E-15); } } } /** * Verify that creating correlation from covariance gives same results as * direct computation from the original matrix */ @Test public void testCovarianceConsistency() { RealMatrix matrix = createRealMatrix(longleyData, 16, 7); PearsonsCorrelation corrInstance = new PearsonsCorrelation(matrix); Covariance covInstance = new Covariance(matrix); PearsonsCorrelation corrFromCovInstance = new PearsonsCorrelation(covInstance); TestUtils.assertEquals("correlation values", corrInstance.getCorrelationMatrix(), corrFromCovInstance.getCorrelationMatrix(), 10E-15); TestUtils.assertEquals("p values", corrInstance.getCorrelationPValues(), corrFromCovInstance.getCorrelationPValues(), 10E-15); TestUtils.assertEquals("standard errors", corrInstance.getCorrelationStandardErrors(), corrFromCovInstance.getCorrelationStandardErrors(), 10E-15); PearsonsCorrelation corrFromCovInstance2 = new PearsonsCorrelation(covInstance.getCovarianceMatrix(), 16); TestUtils.assertEquals("correlation values", corrInstance.getCorrelationMatrix(), corrFromCovInstance2.getCorrelationMatrix(), 10E-15); TestUtils.assertEquals("p values", corrInstance.getCorrelationPValues(), corrFromCovInstance2.getCorrelationPValues(), 10E-15); TestUtils.assertEquals("standard errors", corrInstance.getCorrelationStandardErrors(), corrFromCovInstance2.getCorrelationStandardErrors(), 10E-15); } @Test public void testConsistency() { RealMatrix matrix = createRealMatrix(longleyData, 16, 7); PearsonsCorrelation corrInstance = new PearsonsCorrelation(matrix); double[][] data = matrix.getData(); double[] x = matrix.getColumn(0); double[] y = matrix.getColumn(1); Assert.assertEquals(new PearsonsCorrelation().correlation(x, y), corrInstance.getCorrelationMatrix().getEntry(0, 1), Double.MIN_VALUE); TestUtils.assertEquals("Correlation matrix", corrInstance.getCorrelationMatrix(), new PearsonsCorrelation().computeCorrelationMatrix(data), Double.MIN_VALUE); } protected RealMatrix createRealMatrix(double[] data, int nRows, int nCols) { double[][] matrixData = new double[nRows][nCols]; int ptr = 0; for (int i = 0; i < nRows; i++) { System.arraycopy(data, ptr, matrixData[i], 0, nCols); ptr += nCols; } return new BlockRealMatrix(matrixData); } protected RealMatrix createLowerTriangularRealMatrix(double[] data, int dimension) { int ptr = 0; RealMatrix result = new BlockRealMatrix(dimension, dimension); for (int i = 1; i < dimension; i++) { for (int j = 0; j < i; j++) { result.setEntry(i, j, data[ptr]); ptr++; } } return result; } protected void fillUpper(RealMatrix matrix, double diagonalValue) { int dimension = matrix.getColumnDimension(); for (int i = 0; i < dimension; i++) { matrix.setEntry(i, i, diagonalValue); for (int j = i+1; j < dimension; j++) { matrix.setEntry(i, j, matrix.getEntry(j, i)); } } } } ././@LongLink100644 0 0 153 12126630646 10260 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/stat/correlation/StorelessCovarianceTest.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/stat/correlation/StorelessCovarianceTes100644 1750 1750 22024 12126627674 32455 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.stat.correlation; import org.apache.commons.math3.TestUtils; import org.apache.commons.math3.linear.Array2DRowRealMatrix; import org.apache.commons.math3.linear.RealMatrix; import org.junit.Assert; import org.junit.Test; public class StorelessCovarianceTest { protected final double[] longleyData = new double[] { 60323,83.0,234289,2356,1590,107608,1947, 61122,88.5,259426,2325,1456,108632,1948, 60171,88.2,258054,3682,1616,109773,1949, 61187,89.5,284599,3351,1650,110929,1950, 63221,96.2,328975,2099,3099,112075,1951, 63639,98.1,346999,1932,3594,113270,1952, 64989,99.0,365385,1870,3547,115094,1953, 63761,100.0,363112,3578,3350,116219,1954, 66019,101.2,397469,2904,3048,117388,1955, 67857,104.6,419180,2822,2857,118734,1956, 68169,108.4,442769,2936,2798,120445,1957, 66513,110.8,444546,4681,2637,121950,1958, 68655,112.6,482704,3813,2552,123366,1959, 69564,114.2,502601,3931,2514,125368,1960, 69331,115.7,518173,4806,2572,127852,1961, 70551,116.9,554894,4007,2827,130081,1962 }; protected final double[] swissData = new double[] { 80.2,17.0,15,12,9.96, 83.1,45.1,6,9,84.84, 92.5,39.7,5,5,93.40, 85.8,36.5,12,7,33.77, 76.9,43.5,17,15,5.16, 76.1,35.3,9,7,90.57, 83.8,70.2,16,7,92.85, 92.4,67.8,14,8,97.16, 82.4,53.3,12,7,97.67, 82.9,45.2,16,13,91.38, 87.1,64.5,14,6,98.61, 64.1,62.0,21,12,8.52, 66.9,67.5,14,7,2.27, 68.9,60.7,19,12,4.43, 61.7,69.3,22,5,2.82, 68.3,72.6,18,2,24.20, 71.7,34.0,17,8,3.30, 55.7,19.4,26,28,12.11, 54.3,15.2,31,20,2.15, 65.1,73.0,19,9,2.84, 65.5,59.8,22,10,5.23, 65.0,55.1,14,3,4.52, 56.6,50.9,22,12,15.14, 57.4,54.1,20,6,4.20, 72.5,71.2,12,1,2.40, 74.2,58.1,14,8,5.23, 72.0,63.5,6,3,2.56, 60.5,60.8,16,10,7.72, 58.3,26.8,25,19,18.46, 65.4,49.5,15,8,6.10, 75.5,85.9,3,2,99.71, 69.3,84.9,7,6,99.68, 77.3,89.7,5,2,100.00, 70.5,78.2,12,6,98.96, 79.4,64.9,7,3,98.22, 65.0,75.9,9,9,99.06, 92.2,84.6,3,3,99.46, 79.3,63.1,13,13,96.83, 70.4,38.4,26,12,5.62, 65.7,7.7,29,11,13.79, 72.7,16.7,22,13,11.22, 64.4,17.6,35,32,16.92, 77.6,37.6,15,7,4.97, 67.6,18.7,25,7,8.65, 35.0,1.2,37,53,42.34, 44.7,46.6,16,29,50.43, 42.8,27.7,22,29,58.33 }; protected final double[][] longleyDataSimple = { {60323, 83.0}, {61122,88.5}, {60171, 88.2}, {61187, 89.5}, {63221, 96.2}, {63639, 98.1}, {64989, 99.0}, {63761, 100.0}, {66019, 101.2}, {67857, 104.6}, {68169, 108.4}, {66513, 110.8}, {68655, 112.6}, {69564, 114.2}, {69331, 115.7}, {70551, 116.9} }; @Test public void testLonglySimpleVar(){ double rCov = 12333921.73333333246; StorelessBivariateCovariance cov = new StorelessBivariateCovariance(); for(int i=0;i * To create a concrete test class for an integer distribution implementation, * implement makeDistribution() to return a distribution instance to use in * tests and each of the test data generation methods below. In each case, the * test points and test values arrays returned represent parallel arrays of * inputs and expected values for the distribution returned by makeDistribution(). *

                          * makeDensityTestPoints() -- arguments used to test probability density calculation * makeDensityTestValues() -- expected probability densities * makeCumulativeTestPoints() -- arguments used to test cumulative probabilities * makeCumulativeTestValues() -- expected cumulative probabilites * makeInverseCumulativeTestPoints() -- arguments used to test inverse cdf evaluation * makeInverseCumulativeTestValues() -- expected inverse cdf values *

                          * To implement additional test cases with different distribution instances and test data, * use the setXxx methods for the instance data in test cases and call the verifyXxx methods * to verify results. * * @version $Id: IntegerDistributionAbstractTest.java 1364028 2012-07-21 00:42:49Z erans $ */ public abstract class IntegerDistributionAbstractTest { //-------------------- Private test instance data ------------------------- /** Discrete distribution instance used to perform tests */ private IntegerDistribution distribution; /** Tolerance used in comparing expected and returned values */ private double tolerance = 1E-4; /** Arguments used to test probability density calculations */ private int[] densityTestPoints; /** Values used to test probability density calculations */ private double[] densityTestValues; /** Arguments used to test cumulative probability density calculations */ private int[] cumulativeTestPoints; /** Values used to test cumulative probability density calculations */ private double[] cumulativeTestValues; /** Arguments used to test inverse cumulative probability density calculations */ private double[] inverseCumulativeTestPoints; /** Values used to test inverse cumulative probability density calculations */ private int[] inverseCumulativeTestValues; //-------------------- Abstract methods ----------------------------------- /** Creates the default discrete distribution instance to use in tests. */ public abstract IntegerDistribution makeDistribution(); /** Creates the default probability density test input values */ public abstract int[] makeDensityTestPoints(); /** Creates the default probability density test expected values */ public abstract double[] makeDensityTestValues(); /** Creates the default cumulative probability density test input values */ public abstract int[] makeCumulativeTestPoints(); /** Creates the default cumulative probability density test expected values */ public abstract double[] makeCumulativeTestValues(); /** Creates the default inverse cumulative probability test input values */ public abstract double[] makeInverseCumulativeTestPoints(); /** Creates the default inverse cumulative probability density test expected values */ public abstract int[] makeInverseCumulativeTestValues(); //-------------------- Setup / tear down ---------------------------------- /** * Setup sets all test instance data to default values */ @Before public void setUp() { distribution = makeDistribution(); densityTestPoints = makeDensityTestPoints(); densityTestValues = makeDensityTestValues(); cumulativeTestPoints = makeCumulativeTestPoints(); cumulativeTestValues = makeCumulativeTestValues(); inverseCumulativeTestPoints = makeInverseCumulativeTestPoints(); inverseCumulativeTestValues = makeInverseCumulativeTestValues(); } /** * Cleans up test instance data */ @After public void tearDown() { distribution = null; densityTestPoints = null; densityTestValues = null; cumulativeTestPoints = null; cumulativeTestValues = null; inverseCumulativeTestPoints = null; inverseCumulativeTestValues = null; } //-------------------- Verification methods ------------------------------- /** * Verifies that probability density calculations match expected values * using current test instance data */ protected void verifyDensities() { for (int i = 0; i < densityTestPoints.length; i++) { Assert.assertEquals("Incorrect density value returned for " + densityTestPoints[i], densityTestValues[i], distribution.probability(densityTestPoints[i]), tolerance); } } /** * Verifies that cumulative probability density calculations match expected values * using current test instance data */ protected void verifyCumulativeProbabilities() { for (int i = 0; i < cumulativeTestPoints.length; i++) { Assert.assertEquals("Incorrect cumulative probability value returned for " + cumulativeTestPoints[i], cumulativeTestValues[i], distribution.cumulativeProbability(cumulativeTestPoints[i]), tolerance); } } /** * Verifies that inverse cumulative probability density calculations match expected values * using current test instance data */ protected void verifyInverseCumulativeProbabilities() { for (int i = 0; i < inverseCumulativeTestPoints.length; i++) { Assert.assertEquals("Incorrect inverse cumulative probability value returned for " + inverseCumulativeTestPoints[i], inverseCumulativeTestValues[i], distribution.inverseCumulativeProbability(inverseCumulativeTestPoints[i])); } } //------------------------ Default test cases ----------------------------- /** * Verifies that probability density calculations match expected values * using default test instance data */ @Test public void testDensities() { verifyDensities(); } /** * Verifies that cumulative probability density calculations match expected values * using default test instance data */ @Test public void testCumulativeProbabilities() { verifyCumulativeProbabilities(); } /** * Verifies that inverse cumulative probability density calculations match expected values * using default test instance data */ @Test public void testInverseCumulativeProbabilities() { verifyInverseCumulativeProbabilities(); } @Test public void testConsistencyAtSupportBounds() { final int lower = distribution.getSupportLowerBound(); Assert.assertEquals("Cumulative probability mmust be 0 below support lower bound.", 0.0, distribution.cumulativeProbability(lower - 1), 0.0); Assert.assertEquals("Cumulative probability of support lower bound must be equal to probability mass at this point.", distribution.probability(lower), distribution.cumulativeProbability(lower), tolerance); Assert.assertEquals("Inverse cumulative probability of 0 must be equal to support lower bound.", lower, distribution.inverseCumulativeProbability(0.0)); final int upper = distribution.getSupportUpperBound(); if (upper != Integer.MAX_VALUE) Assert.assertEquals("Cumulative probability of support upper bound must be equal to 1.", 1.0, distribution.cumulativeProbability(upper), 0.0); Assert.assertEquals("Inverse cumulative probability of 1 must be equal to support upper bound.", upper, distribution.inverseCumulativeProbability(1.0)); } /** * Verifies that illegal arguments are correctly handled */ @Test public void testIllegalArguments() { try { distribution.cumulativeProbability(1, 0); Assert.fail("Expecting MathIllegalArgumentException for bad cumulativeProbability interval"); } catch (MathIllegalArgumentException ex) { // expected } try { distribution.inverseCumulativeProbability(-1); Assert.fail("Expecting MathIllegalArgumentException for p = -1"); } catch (MathIllegalArgumentException ex) { // expected } try { distribution.inverseCumulativeProbability(2); Assert.fail("Expecting MathIllegalArgumentException for p = 2"); } catch (MathIllegalArgumentException ex) { // expected } } /** * Test sampling */ @Test public void testSampling() { int[] densityPoints = makeDensityTestPoints(); double[] densityValues = makeDensityTestValues(); int sampleSize = 1000; int length = TestUtils.eliminateZeroMassPoints(densityPoints, densityValues); AbstractIntegerDistribution distribution = (AbstractIntegerDistribution) makeDistribution(); double[] expectedCounts = new double[length]; long[] observedCounts = new long[length]; for (int i = 0; i < length; i++) { expectedCounts[i] = sampleSize * densityValues[i]; } distribution.reseedRandomGenerator(1000); // Use fixed seed int[] sample = distribution.sample(sampleSize); for (int i = 0; i < sampleSize; i++) { for (int j = 0; j < length; j++) { if (sample[i] == densityPoints[j]) { observedCounts[j]++; } } } TestUtils.assertChiSquareAccept(densityPoints, expectedCounts, observedCounts, .001); } //------------------ Getters / Setters for test instance data ----------- /** * @return Returns the cumulativeTestPoints. */ protected int[] getCumulativeTestPoints() { return cumulativeTestPoints; } /** * @param cumulativeTestPoints The cumulativeTestPoints to set. */ protected void setCumulativeTestPoints(int[] cumulativeTestPoints) { this.cumulativeTestPoints = cumulativeTestPoints; } /** * @return Returns the cumulativeTestValues. */ protected double[] getCumulativeTestValues() { return cumulativeTestValues; } /** * @param cumulativeTestValues The cumulativeTestValues to set. */ protected void setCumulativeTestValues(double[] cumulativeTestValues) { this.cumulativeTestValues = cumulativeTestValues; } /** * @return Returns the densityTestPoints. */ protected int[] getDensityTestPoints() { return densityTestPoints; } /** * @param densityTestPoints The densityTestPoints to set. */ protected void setDensityTestPoints(int[] densityTestPoints) { this.densityTestPoints = densityTestPoints; } /** * @return Returns the densityTestValues. */ protected double[] getDensityTestValues() { return densityTestValues; } /** * @param densityTestValues The densityTestValues to set. */ protected void setDensityTestValues(double[] densityTestValues) { this.densityTestValues = densityTestValues; } /** * @return Returns the distribution. */ protected IntegerDistribution getDistribution() { return distribution; } /** * @param distribution The distribution to set. */ protected void setDistribution(IntegerDistribution distribution) { this.distribution = distribution; } /** * @return Returns the inverseCumulativeTestPoints. */ protected double[] getInverseCumulativeTestPoints() { return inverseCumulativeTestPoints; } /** * @param inverseCumulativeTestPoints The inverseCumulativeTestPoints to set. */ protected void setInverseCumulativeTestPoints(double[] inverseCumulativeTestPoints) { this.inverseCumulativeTestPoints = inverseCumulativeTestPoints; } /** * @return Returns the inverseCumulativeTestValues. */ protected int[] getInverseCumulativeTestValues() { return inverseCumulativeTestValues; } /** * @param inverseCumulativeTestValues The inverseCumulativeTestValues to set. */ protected void setInverseCumulativeTestValues(int[] inverseCumulativeTestValues) { this.inverseCumulativeTestValues = inverseCumulativeTestValues; } /** * @return Returns the tolerance. */ protected double getTolerance() { return tolerance; } /** * @param tolerance The tolerance to set. */ protected void setTolerance(double tolerance) { this.tolerance = tolerance; } } ././@LongLink100644 0 0 152 12126630646 10257 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/distribution/ChiSquaredDistributionTest.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/distribution/ChiSquaredDistributionTest100644 1750 1750 13407 12126627667 32545 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.distribution; import org.junit.Assert; import org.junit.Test; /** * Test cases for {@link ChiSquaredDistribution}. * * @see RealDistributionAbstractTest * @version $Id: ChiSquaredDistributionTest.java 1364028 2012-07-21 00:42:49Z erans $ */ public class ChiSquaredDistributionTest extends RealDistributionAbstractTest { //-------------- Implementations for abstract methods ----------------------- /** Creates the default continuous distribution instance to use in tests. */ @Override public ChiSquaredDistribution makeDistribution() { return new ChiSquaredDistribution(5.0); } /** Creates the default cumulative probability distribution test input values */ @Override public double[] makeCumulativeTestPoints() { // quantiles computed using R version 2.9.2 return new double[] {0.210212602629, 0.554298076728, 0.831211613487, 1.14547622606, 1.61030798696, 20.5150056524, 15.0862724694, 12.8325019940, 11.0704976935, 9.23635689978}; } /** Creates the default cumulative probability density test expected values */ @Override public double[] makeCumulativeTestValues() { return new double[] {0.001, 0.01, 0.025, 0.05, 0.1, 0.999, 0.990, 0.975, 0.950, 0.900}; } /** Creates the default inverse cumulative probability test input values */ @Override public double[] makeInverseCumulativeTestPoints() { return new double[] {0, 0.001d, 0.01d, 0.025d, 0.05d, 0.1d, 0.999d, 0.990d, 0.975d, 0.950d, 0.900d, 1}; } /** Creates the default inverse cumulative probability density test expected values */ @Override public double[] makeInverseCumulativeTestValues() { return new double[] {0, 0.210212602629, 0.554298076728, 0.831211613487, 1.14547622606, 1.61030798696, 20.5150056524, 15.0862724694, 12.8325019940, 11.0704976935, 9.23635689978, Double.POSITIVE_INFINITY}; } /** Creates the default probability density test expected values */ @Override public double[] makeDensityTestValues() { return new double[] {0.0115379817652, 0.0415948507811, 0.0665060119842, 0.0919455953114, 0.121472591024, 0.000433630076361, 0.00412780610309, 0.00999340341045, 0.0193246438937, 0.0368460089216}; } // --------------------- Override tolerance -------------- @Override public void setUp() { super.setUp(); setTolerance(1e-9); } //---------------------------- Additional test cases ------------------------- @Test public void testSmallDf() { setDistribution(new ChiSquaredDistribution(0.1d)); setTolerance(1E-4); // quantiles computed using R version 1.8.1 (linux version) setCumulativeTestPoints(new double[] {1.168926E-60, 1.168926E-40, 1.063132E-32, 1.144775E-26, 1.168926E-20, 5.472917, 2.175255, 1.13438, 0.5318646, 0.1526342}); setInverseCumulativeTestValues(getCumulativeTestPoints()); setInverseCumulativeTestPoints(getCumulativeTestValues()); verifyCumulativeProbabilities(); verifyInverseCumulativeProbabilities(); } @Test public void testDfAccessors() { ChiSquaredDistribution distribution = (ChiSquaredDistribution) getDistribution(); Assert.assertEquals(5d, distribution.getDegreesOfFreedom(), Double.MIN_VALUE); } @Test public void testDensity() { double[] x = new double[]{-0.1, 1e-6, 0.5, 1, 2, 5}; //R 2.5: print(dchisq(x, df=1), digits=10) checkDensity(1, x, new double[]{0.00000000000, 398.94208093034, 0.43939128947, 0.24197072452, 0.10377687436, 0.01464498256}); //R 2.5: print(dchisq(x, df=0.1), digits=10) checkDensity(0.1, x, new double[]{0.000000000e+00, 2.486453997e+04, 7.464238732e-02, 3.009077718e-02, 9.447299159e-03, 8.827199396e-04}); //R 2.5: print(dchisq(x, df=2), digits=10) checkDensity(2, x, new double[]{0.00000000000, 0.49999975000, 0.38940039154, 0.30326532986, 0.18393972059, 0.04104249931}); //R 2.5: print(dchisq(x, df=10), digits=10) checkDensity(10, x, new double[]{0.000000000e+00, 1.302082682e-27, 6.337896998e-05, 7.897534632e-04, 7.664155024e-03, 6.680094289e-02}); } private void checkDensity(double df, double[] x, double[] expected) { ChiSquaredDistribution d = new ChiSquaredDistribution(df); for (int i = 0; i < x.length; i++) { Assert.assertEquals(expected[i], d.density(x[i]), 1e-5); } } @Test public void testMoments() { final double tol = 1e-9; ChiSquaredDistribution dist; dist = new ChiSquaredDistribution(1500); Assert.assertEquals(dist.getNumericalMean(), 1500, tol); Assert.assertEquals(dist.getNumericalVariance(), 3000, tol); dist = new ChiSquaredDistribution(1.12); Assert.assertEquals(dist.getNumericalMean(), 1.12, tol); Assert.assertEquals(dist.getNumericalVariance(), 2.24, tol); } } ././@LongLink100644 0 0 162 12126630646 10260 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/distribution/MultivariateNormalDistributionTest.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/distribution/MultivariateNormalDistribu100644 1750 1750 13020 12126627667 32571 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.distribution; import org.apache.commons.math3.stat.correlation.Covariance; import org.apache.commons.math3.linear.RealMatrix; import java.util.Random; import org.junit.After; import org.junit.Assert; import org.junit.Before; import org.junit.Test; /** * Test cases for {@link MultivariateNormalDistribution}. */ public class MultivariateNormalDistributionTest { /** * Test the ability of the distribution to report its mean value parameter. */ @Test public void testGetMean() { final double[] mu = { -1.5, 2 }; final double[][] sigma = { { 2, -1.1 }, { -1.1, 2 } }; final MultivariateNormalDistribution d = new MultivariateNormalDistribution(mu, sigma); final double[] m = d.getMeans(); for (int i = 0; i < m.length; i++) { Assert.assertEquals(mu[i], m[i], 0); } } /** * Test the ability of the distribution to report its covariance matrix parameter. */ @Test public void testGetCovarianceMatrix() { final double[] mu = { -1.5, 2 }; final double[][] sigma = { { 2, -1.1 }, { -1.1, 2 } }; final MultivariateNormalDistribution d = new MultivariateNormalDistribution(mu, sigma); final RealMatrix s = d.getCovariances(); final int dim = d.getDimension(); for (int i = 0; i < dim; i++) { for (int j = 0; j < dim; j++) { Assert.assertEquals(sigma[i][j], s.getEntry(i, j), 0); } } } /** * Test the accuracy of sampling from the distribution. */ @Test public void testSampling() { final double[] mu = { -1.5, 2 }; final double[][] sigma = { { 2, -1.1 }, { -1.1, 2 } }; final MultivariateNormalDistribution d = new MultivariateNormalDistribution(mu, sigma); d.reseedRandomGenerator(50); final int n = 500000; final double[][] samples = d.sample(n); final int dim = d.getDimension(); final double[] sampleMeans = new double[dim]; for (int i = 0; i < samples.length; i++) { for (int j = 0; j < dim; j++) { sampleMeans[j] += samples[i][j]; } } final double sampledValueTolerance = 1e-2; for (int j = 0; j < dim; j++) { sampleMeans[j] /= samples.length; Assert.assertEquals(mu[j], sampleMeans[j], sampledValueTolerance); } final double[][] sampleSigma = new Covariance(samples).getCovarianceMatrix().getData(); for (int i = 0; i < dim; i++) { for (int j = 0; j < dim; j++) { Assert.assertEquals(sigma[i][j], sampleSigma[i][j], sampledValueTolerance); } } } /** * Test the accuracy of the distribution when calculating densities. */ @Test public void testDensities() { final double[] mu = { -1.5, 2 }; final double[][] sigma = { { 2, -1.1 }, { -1.1, 2 } }; final MultivariateNormalDistribution d = new MultivariateNormalDistribution(mu, sigma); final double[][] testValues = { { -1.5, 2 }, { 4, 4 }, { 1.5, -2 }, { 0, 0 } }; final double[] densities = new double[testValues.length]; for (int i = 0; i < densities.length; i++) { densities[i] = d.density(testValues[i]); } // From dmvnorm function in R 2.15 CRAN package Mixtools v0.4.5 final double[] correctDensities = { 0.09528357207691344, 5.80932710124009e-09, 0.001387448895173267, 0.03309922090210541 }; for (int i = 0; i < testValues.length; i++) { Assert.assertEquals(correctDensities[i], densities[i], 1e-16); } } /** * Test the accuracy of the distribution when calculating densities. */ @Test public void testUnivariateDistribution() { final double[] mu = { -1.5 }; final double[][] sigma = { { 1 } }; final MultivariateNormalDistribution multi = new MultivariateNormalDistribution(mu, sigma); final NormalDistribution uni = new NormalDistribution(mu[0], sigma[0][0]); final Random rng = new Random(); final int numCases = 100; final double tol = Math.ulp(1d); for (int i = 0; i < numCases; i++) { final double v = rng.nextDouble() * 10 - 5; Assert.assertEquals(uni.density(v), multi.density(new double[] { v }), tol); } } } ././@LongLink100644 0 0 214 12126630646 10256 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/distribution/fitting/MultivariateNormalMixtureExpectationMaximizationTest.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/distribution/fitting/MultivariateNormal100644 1750 1750 40333 12126627667 32536 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with this * work for additional information regarding copyright ownership. The ASF * licenses this file to You 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. */ package org.apache.commons.math3.distribution.fitting; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import org.apache.commons.math3.distribution.MixtureMultivariateNormalDistribution; import org.apache.commons.math3.distribution.MultivariateNormalDistribution; import org.apache.commons.math3.exception.ConvergenceException; import org.apache.commons.math3.exception.DimensionMismatchException; import org.apache.commons.math3.exception.NotStrictlyPositiveException; import org.apache.commons.math3.exception.NumberIsTooSmallException; import org.apache.commons.math3.linear.Array2DRowRealMatrix; import org.apache.commons.math3.linear.RealMatrix; import org.apache.commons.math3.util.Pair; import org.junit.Assert; import org.junit.Test; /** * Test that demonstrates the use of * {@link MultivariateNormalMixtureExpectationMaximization}. */ public class MultivariateNormalMixtureExpectationMaximizationTest { @Test(expected = NotStrictlyPositiveException.class) public void testNonEmptyData() { // Should not accept empty data new MultivariateNormalMixtureExpectationMaximization(new double[][] {}); } @Test(expected = DimensionMismatchException.class) public void testNonJaggedData() { // Reject data with nonconstant numbers of columns double[][] data = new double[][] { { 1, 2, 3 }, { 4, 5, 6, 7 }, }; new MultivariateNormalMixtureExpectationMaximization(data); } @Test(expected = NumberIsTooSmallException.class) public void testMultipleColumnsRequired() { // Data should have at least 2 columns double[][] data = new double[][] { { 1 }, { 2 } }; new MultivariateNormalMixtureExpectationMaximization(data); } @Test(expected = NotStrictlyPositiveException.class) public void testMaxIterationsPositive() { // Maximum iterations for fit must be positive integer double[][] data = getTestSamples(); MultivariateNormalMixtureExpectationMaximization fitter = new MultivariateNormalMixtureExpectationMaximization(data); MixtureMultivariateNormalDistribution initialMix = MultivariateNormalMixtureExpectationMaximization.estimate(data, 2); fitter.fit(initialMix, 0, 1E-5); } @Test(expected = NotStrictlyPositiveException.class) public void testThresholdPositive() { // Maximum iterations for fit must be positive double[][] data = getTestSamples(); MultivariateNormalMixtureExpectationMaximization fitter = new MultivariateNormalMixtureExpectationMaximization( data); MixtureMultivariateNormalDistribution initialMix = MultivariateNormalMixtureExpectationMaximization.estimate(data, 2); fitter.fit(initialMix, 1000, 0); } @Test(expected = ConvergenceException.class) public void testConvergenceException() { // ConvergenceException thrown if fit terminates before threshold met double[][] data = getTestSamples(); MultivariateNormalMixtureExpectationMaximization fitter = new MultivariateNormalMixtureExpectationMaximization(data); MixtureMultivariateNormalDistribution initialMix = MultivariateNormalMixtureExpectationMaximization.estimate(data, 2); // 5 iterations not enough to meet convergence threshold fitter.fit(initialMix, 5, 1E-5); } @Test(expected = DimensionMismatchException.class) public void testIncompatibleIntialMixture() { // Data has 3 columns double[][] data = new double[][] { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } }; double[] weights = new double[] { 0.5, 0.5 }; // These distributions are compatible with 2-column data, not 3-column // data MultivariateNormalDistribution[] mvns = new MultivariateNormalDistribution[2]; mvns[0] = new MultivariateNormalDistribution(new double[] { -0.0021722935000328823, 3.5432892936887908 }, new double[][] { { 4.537422569229048, 3.5266152281729304 }, { 3.5266152281729304, 6.175448814169779 } }); mvns[1] = new MultivariateNormalDistribution(new double[] { 5.090902706507635, 8.68540656355283 }, new double[][] { { 2.886778573963039, 1.5257474543463154 }, { 1.5257474543463154, 3.3794567673616918 } }); // Create components and mixture List> components = new ArrayList>(); components.add(new Pair( weights[0], mvns[0])); components.add(new Pair( weights[1], mvns[1])); MixtureMultivariateNormalDistribution badInitialMix = new MixtureMultivariateNormalDistribution(components); MultivariateNormalMixtureExpectationMaximization fitter = new MultivariateNormalMixtureExpectationMaximization(data); fitter.fit(badInitialMix); } @Test public void testInitialMixture() { // Testing initial mixture estimated from data final double[] correctWeights = new double[] { 0.5, 0.5 }; final double[][] correctMeans = new double[][] { {-0.0021722935000328823, 3.5432892936887908}, {5.090902706507635, 8.68540656355283}, }; final RealMatrix[] correctCovMats = new Array2DRowRealMatrix[2]; correctCovMats[0] = new Array2DRowRealMatrix(new double[][] { { 4.537422569229048, 3.5266152281729304 }, { 3.5266152281729304, 6.175448814169779 } }); correctCovMats[1] = new Array2DRowRealMatrix( new double[][] { { 2.886778573963039, 1.5257474543463154 }, { 1.5257474543463154, 3.3794567673616918 } }); final MultivariateNormalDistribution[] correctMVNs = new MultivariateNormalDistribution[2]; correctMVNs[0] = new MultivariateNormalDistribution(correctMeans[0], correctCovMats[0].getData()); correctMVNs[1] = new MultivariateNormalDistribution(correctMeans[1], correctCovMats[1].getData()); final MixtureMultivariateNormalDistribution initialMix = MultivariateNormalMixtureExpectationMaximization.estimate(getTestSamples(), 2); int i = 0; for (Pair component : initialMix .getComponents()) { Assert.assertEquals(correctWeights[i], component.getFirst(), Math.ulp(1d)); final double[] means = component.getValue().getMeans(); Assert.assertTrue(Arrays.equals(correctMeans[i], means)); final RealMatrix covMat = component.getValue().getCovariances(); Assert.assertEquals(correctCovMats[i], covMat); i++; } } @Test public void testFit() { // Test that the loglikelihood, weights, and models are determined and // fitted correctly final double[][] data = getTestSamples(); final double correctLogLikelihood = -4.292431006791994; final double[] correctWeights = new double[] { 0.2962324189652912, 0.7037675810347089 }; final double[][] correctMeans = new double[][]{ {-1.4213112715121132, 1.6924690505757753}, {4.213612224374709, 7.975621325853645} }; final RealMatrix[] correctCovMats = new Array2DRowRealMatrix[2]; correctCovMats[0] = new Array2DRowRealMatrix(new double[][] { { 1.739356907285747, -0.5867644251487614 }, { -0.5867644251487614, 1.0232932029324642 } } ); correctCovMats[1] = new Array2DRowRealMatrix(new double[][] { { 4.245384898007161, 2.5797798966382155 }, { 2.5797798966382155, 3.9200272522448367 } }); final MultivariateNormalDistribution[] correctMVNs = new MultivariateNormalDistribution[2]; correctMVNs[0] = new MultivariateNormalDistribution(correctMeans[0], correctCovMats[0].getData()); correctMVNs[1] = new MultivariateNormalDistribution(correctMeans[1], correctCovMats[1].getData()); MultivariateNormalMixtureExpectationMaximization fitter = new MultivariateNormalMixtureExpectationMaximization(data); MixtureMultivariateNormalDistribution initialMix = MultivariateNormalMixtureExpectationMaximization.estimate(data, 2); fitter.fit(initialMix); MixtureMultivariateNormalDistribution fittedMix = fitter.getFittedModel(); List> components = fittedMix.getComponents(); Assert.assertEquals(correctLogLikelihood, fitter.getLogLikelihood(), Math.ulp(1d)); int i = 0; for (Pair component : components) { final double weight = component.getFirst(); final MultivariateNormalDistribution mvn = component.getSecond(); final double[] mean = mvn.getMeans(); final RealMatrix covMat = mvn.getCovariances(); Assert.assertEquals(correctWeights[i], weight, Math.ulp(1d)); Assert.assertTrue(Arrays.equals(correctMeans[i], mean)); Assert.assertEquals(correctCovMats[i], covMat); i++; } } private double[][] getTestSamples() { // generated using R Mixtools rmvnorm with mean vectors [-1.5, 2] and // [4, 8.2] return new double[][] { { 7.358553610469948, 11.31260831446758 }, { 7.175770420124739, 8.988812210204454 }, { 4.324151905768422, 6.837727899051482 }, { 2.157832219173036, 6.317444585521968 }, { -1.890157421896651, 1.74271202875498 }, { 0.8922409354455803, 1.999119343923781 }, { 3.396949764787055, 6.813170372579068 }, { -2.057498232686068, -0.002522983830852255 }, { 6.359932157365045, 8.343600029975851 }, { 3.353102234276168, 7.087541882898689 }, { -1.763877221595639, 0.9688890460330644 }, { 6.151457185125111, 9.075011757431174 }, { 4.281597398048899, 5.953270070976117 }, { 3.549576703974894, 8.616038155992861 }, { 6.004706732349854, 8.959423391087469 }, { 2.802915014676262, 6.285676742173564 }, { -0.6029879029880616, 1.083332958357485 }, { 3.631827105398369, 6.743428504049444 }, { 6.161125014007315, 9.60920569689001 }, { -1.049582894255342, 0.2020017892080281 }, { 3.910573022688315, 8.19609909534937 }, { 8.180454017634863, 7.861055769719962 }, { 1.488945440439716, 8.02699903761247 }, { 4.813750847823778, 12.34416881332515 }, { 0.0443208501259158, 5.901148093240691 }, { 4.416417235068346, 4.465243084006094 }, { 4.0002433603072, 6.721937850166174 }, { 3.190113818788205, 10.51648348411058 }, { 4.493600914967883, 7.938224231022314 }, { -3.675669533266189, 4.472845076673303 }, { 6.648645511703989, 12.03544085965724 }, { -1.330031331404445, 1.33931042964811 }, { -3.812111460708707, 2.50534195568356 }, { 5.669339356648331, 6.214488981177026 }, { 1.006596727153816, 1.51165463112716 }, { 5.039466365033024, 7.476532610478689 }, { 4.349091929968925, 7.446356406259756 }, { -1.220289665119069, 3.403926955951437 }, { 5.553003979122395, 6.886518211202239 }, { 2.274487732222856, 7.009541508533196 }, { 4.147567059965864, 7.34025244349202 }, { 4.083882618965819, 6.362852861075623 }, { 2.203122344647599, 7.260295257904624 }, { -2.147497550770442, 1.262293431529498 }, { 2.473700950426512, 6.558900135505638 }, { 8.267081298847554, 12.10214104577748 }, { 6.91977329776865, 9.91998488301285 }, { 0.1680479852730894, 6.28286034168897 }, { -1.268578659195158, 2.326711221485755 }, { 1.829966451374701, 6.254187605304518 }, { 5.648849025754848, 9.330002040750291 }, { -2.302874793257666, 3.585545172776065 }, { -2.629218791709046, 2.156215538500288 }, { 4.036618140700114, 10.2962785719958 }, { 0.4616386422783874, 0.6782756325806778 }, { -0.3447896073408363, 0.4999834691645118 }, { -0.475281453118318, 1.931470384180492 }, { 2.382509690609731, 6.071782429815853 }, { -3.203934441889096, 2.572079552602468 }, { 8.465636032165087, 13.96462998683518 }, { 2.36755660870416, 5.7844595007273 }, { 0.5935496528993371, 1.374615871358943 }, { -2.467481505748694, 2.097224634713005 }, { 4.27867444328542, 10.24772361238549 }, { -2.013791907543137, 2.013799426047639 }, { 6.424588084404173, 9.185334939684516 }, { -0.8448238876802175, 0.5447382022282812 }, { 1.342955703473923, 8.645456317633556 }, { 3.108712208751979, 8.512156853800064 }, { 4.343205178315472, 8.056869549234374 }, { -2.971767642212396, 3.201180146824761 }, { 2.583820931523672, 5.459873414473854 }, { 4.209139115268925, 8.171098193546225 }, { 0.4064909057902746, 1.454390775518743 }, { 3.068642411145223, 6.959485153620035 }, { 6.085968972900461, 7.391429799500965 }, { -1.342265795764202, 1.454550012997143 }, { 6.249773274516883, 6.290269880772023 }, { 4.986225847822566, 7.75266344868907 }, { 7.642443254378944, 10.19914817500263 }, { 6.438181159163673, 8.464396764810347 }, { 2.520859761025108, 7.68222425260111 }, { 2.883699944257541, 6.777960331348503 }, { 2.788004550956599, 6.634735386652733 }, { 3.331661231995638, 5.794191300046592 }, { 3.526172276645504, 6.710802266815884 }, { 3.188298528138741, 10.34495528210205 }, { 0.7345539486114623, 5.807604004180681 }, { 1.165044595880125, 7.830121829295257 }, { 7.146962523500671, 11.62995162065415 }, { 7.813872137162087, 10.62827008714735 }, { 3.118099164870063, 8.286003148186371 }, { -1.708739286262571, 1.561026755374264 }, { 1.786163047580084, 4.172394388214604 }, { 3.718506403232386, 7.807752990130349 }, { 6.167414046828899, 10.01104941031293 }, { -1.063477247689196, 1.61176085846339 }, { -3.396739609433642, 0.7127911050002151 }, { 2.438885945896797, 7.353011138689225 }, { -0.2073204144780931, 0.850771146627012 }, }; } } ././@LongLink100644 0 0 161 12126630646 10257 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/distribution/KolmogorovSmirnovDistributionTest.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/distribution/KolmogorovSmirnovDistribut100644 1750 1750 13044 12126627667 32660 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.distribution; import org.junit.Assert; import org.junit.Test; /** * Test cases for {@link KolmogorovSmirnovDistribution}. * * @version $Id: KolmogorovSmirnovDistributionTest.java 1364028 2012-07-21 00:42:49Z erans $ */ public class KolmogorovSmirnovDistributionTest { private static final double TOLERANCE = 10e-10; @Test public void testCumulativeDensityFunction() { KolmogorovSmirnovDistribution dist; /* The code below is generated using the R-script located in * /src/test/R/KolmogorovSmirnovDistributionTestCases.R */ /* R version 2.11.1 (2010-05-31) */ /* formatC(.C("pkolmogorov2x", p = as.double(0.005), n = as.integer(200), PACKAGE = "stats")$p, 40) gives * 4.907829957616471622388047046469198862537e-86 */ dist = new KolmogorovSmirnovDistribution(200); Assert.assertEquals(4.907829957616471622388047046469198862537e-86, dist.cdf(0.005, false), TOLERANCE); /* formatC(.C("pkolmogorov2x", p = as.double(0.02), n = as.integer(200), PACKAGE = "stats")$p, 40) gives * 5.151982014280041957199687829849210629618e-06 */ dist = new KolmogorovSmirnovDistribution(200); Assert.assertEquals(5.151982014280041957199687829849210629618e-06, dist.cdf(0.02, false), TOLERANCE); /* formatC(.C("pkolmogorov2x", p = as.double(0.031111), n = as.integer(200), PACKAGE = "stats")$p, 40) gives * 0.01291614648162886340443389343590752105229 */ dist = new KolmogorovSmirnovDistribution(200); Assert.assertEquals(0.01291614648162886340443389343590752105229, dist.cdf(0.031111, false), TOLERANCE); /* formatC(.C("pkolmogorov2x", p = as.double(0.04), n = as.integer(200), PACKAGE = "stats")$p, 40) gives * 0.1067137011362679355208626930107129737735 */ dist = new KolmogorovSmirnovDistribution(200); Assert.assertEquals(0.1067137011362679355208626930107129737735, dist.cdf(0.04, false), TOLERANCE); /* formatC(.C("pkolmogorov2x", p = as.double(0.005), n = as.integer(341), PACKAGE = "stats")$p, 40) gives * 1.914734701559404553985102395145063418825e-53 */ dist = new KolmogorovSmirnovDistribution(341); Assert.assertEquals(1.914734701559404553985102395145063418825e-53, dist.cdf(0.005, false), TOLERANCE); /* formatC(.C("pkolmogorov2x", p = as.double(0.02), n = as.integer(341), PACKAGE = "stats")$p, 40) gives * 0.001171328985781981343872182321774744195864 */ dist = new KolmogorovSmirnovDistribution(341); Assert.assertEquals(0.001171328985781981343872182321774744195864, dist.cdf(0.02, false), TOLERANCE); /* formatC(.C("pkolmogorov2x", p = as.double(0.031111), n = as.integer(341), PACKAGE = "stats")$p, 40) gives * 0.1142955196267499418105728636874118819833 */ dist = new KolmogorovSmirnovDistribution(341); Assert.assertEquals(0.1142955196267499418105728636874118819833, dist.cdf(0.031111, false), TOLERANCE); /* formatC(.C("pkolmogorov2x", p = as.double(0.04), n = as.integer(341), PACKAGE = "stats")$p, 40) gives * 0.3685529520496805266915885113121476024389 */ dist = new KolmogorovSmirnovDistribution(341); Assert.assertEquals(0.3685529520496805266915885113121476024389, dist.cdf(0.04, false), TOLERANCE); /* formatC(.C("pkolmogorov2x", p = as.double(0.005), n = as.integer(389), PACKAGE = "stats")$p, 40) gives * 1.810657144595055888918455512707637574637e-47 */ dist = new KolmogorovSmirnovDistribution(389); Assert.assertEquals(1.810657144595055888918455512707637574637e-47, dist.cdf(0.005, false), TOLERANCE); /* formatC(.C("pkolmogorov2x", p = as.double(0.02), n = as.integer(389), PACKAGE = "stats")$p, 40) gives * 0.003068542559702356568168690742481885536108 */ dist = new KolmogorovSmirnovDistribution(389); Assert.assertEquals(0.003068542559702356568168690742481885536108, dist.cdf(0.02, false), TOLERANCE); /* formatC(.C("pkolmogorov2x", p = as.double(0.031111), n = as.integer(389), PACKAGE = "stats")$p, 40) gives * 0.1658291700122746237244797384846606291831 */ dist = new KolmogorovSmirnovDistribution(389); Assert.assertEquals(0.1658291700122746237244797384846606291831, dist.cdf(0.031111, false), TOLERANCE); /* formatC(.C("pkolmogorov2x", p = as.double(0.04), n = as.integer(389), PACKAGE = "stats")$p, 40) gives * 0.4513143712128902529379104180407011881471 */ dist = new KolmogorovSmirnovDistribution(389); Assert.assertEquals(0.4513143712128902529379104180407011881471, dist.cdf(0.04, false), TOLERANCE); } } ././@LongLink100644 0 0 152 12126630646 10257 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/distribution/TriangularDistributionTest.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/distribution/TriangularDistributionTest100644 1750 1750 16376 12126627667 32635 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.distribution; import org.apache.commons.math3.exception.NumberIsTooLargeException; import org.apache.commons.math3.exception.NumberIsTooSmallException; import org.junit.Assert; import org.junit.Test; /** * Test cases for {@link TriangularDistribution}. See class javadoc for * {@link RealDistributionAbstractTest} for further details. */ public class TriangularDistributionTest extends RealDistributionAbstractTest { // --- Override tolerance ------------------------------------------------- @Override public void setUp() { super.setUp(); setTolerance(1e-4); } //--- Implementations for abstract methods -------------------------------- /** * Creates the default triangular distribution instance to use in tests. */ @Override public TriangularDistribution makeDistribution() { // Left side 5 wide, right side 10 wide. return new TriangularDistribution(-3, 2, 12); } /** * Creates the default cumulative probability distribution test input * values. */ @Override public double[] makeCumulativeTestPoints() { return new double[] { -3.0001, // below lower limit -3.0, // at lower limit -2.0, -1.0, 0.0, 1.0, // on lower side 2.0, // at mode 3.0, 4.0, 10.0, 11.0, // on upper side 12.0, // at upper limit 12.0001 // above upper limit }; } /** * Creates the default cumulative probability density test expected values. */ @Override public double[] makeCumulativeTestValues() { // Top at 2 / (b - a) = 2 / (12 - -3) = 2 / 15 = 7.5 // Area left = 7.5 * 5 * 0.5 = 18.75 (1/3 of the total area) // Area right = 7.5 * 10 * 0.5 = 37.5 (2/3 of the total area) // Area total = 18.75 + 37.5 = 56.25 // Derivative left side = 7.5 / 5 = 1.5 // Derivative right side = -7.5 / 10 = -0.75 double third = 1 / 3.0; double left = 18.75; double area = 56.25; return new double[] { 0.0, 0.0, 0.75 / area, 3 / area, 6.75 / area, 12 / area, third, (left + 7.125) / area, (left + 13.5) / area, (left + 36) / area, (left + 37.125) / area, 1.0, 1.0 }; } /** * Creates the default inverse cumulative probability distribution test * input values. */ @Override public double[] makeInverseCumulativeTestPoints() { // Exclude the points outside the limits, as they have cumulative // probability of zero and one, meaning the inverse returns the // limits and not the points outside the limits. double[] points = makeCumulativeTestValues(); double[] points2 = new double[points.length-2]; System.arraycopy(points, 1, points2, 0, points2.length); return points2; //return Arrays.copyOfRange(points, 1, points.length - 1); } /** * Creates the default inverse cumulative probability density test expected * values. */ @Override public double[] makeInverseCumulativeTestValues() { // Exclude the points outside the limits, as they have cumulative // probability of zero and one, meaning the inverse returns the // limits and not the points outside the limits. double[] points = makeCumulativeTestPoints(); double[] points2 = new double[points.length-2]; System.arraycopy(points, 1, points2, 0, points2.length); return points2; //return Arrays.copyOfRange(points, 1, points.length - 1); } /** Creates the default probability density test expected values. */ @Override public double[] makeDensityTestValues() { return new double[] { 0, 0, 2 / 75.0, 4 / 75.0, 6 / 75.0, 8 / 75.0, 10 / 75.0, 9 / 75.0, 8 / 75.0, 2 / 75.0, 1 / 75.0, 0, 0 }; } //--- Additional test cases ----------------------------------------------- /** Test lower bound getter. */ @Test public void testGetLowerBound() { TriangularDistribution distribution = makeDistribution(); Assert.assertEquals(-3.0, distribution.getSupportLowerBound(), 0); } /** Test upper bound getter. */ @Test public void testGetUpperBound() { TriangularDistribution distribution = makeDistribution(); Assert.assertEquals(12.0, distribution.getSupportUpperBound(), 0); } /** Test pre-condition for equal lower/upper limit. */ @Test(expected=NumberIsTooLargeException.class) public void testPreconditions1() { new TriangularDistribution(0, 0, 0); } /** Test pre-condition for lower limit larger than upper limit. */ @Test(expected=NumberIsTooLargeException.class) public void testPreconditions2() { new TriangularDistribution(1, 1, 0); } /** Test pre-condition for mode larger than upper limit. */ @Test(expected=NumberIsTooLargeException.class) public void testPreconditions3() { new TriangularDistribution(0, 2, 1); } /** Test pre-condition for mode smaller than lower limit. */ @Test(expected=NumberIsTooSmallException.class) public void testPreconditions4() { new TriangularDistribution(2, 1, 3); } /** Test mean/variance. */ @Test public void testMeanVariance() { TriangularDistribution dist; dist = new TriangularDistribution(0, 0.5, 1.0); Assert.assertEquals(dist.getNumericalMean(), 0.5, 0); Assert.assertEquals(dist.getNumericalVariance(), 1 / 24.0, 0); dist = new TriangularDistribution(0, 1, 1); Assert.assertEquals(dist.getNumericalMean(), 2 / 3.0, 0); Assert.assertEquals(dist.getNumericalVariance(), 1 / 18.0, 0); dist = new TriangularDistribution(-3, 2, 12); Assert.assertEquals(dist.getNumericalMean(), 3 + (2 / 3.0), 0); Assert.assertEquals(dist.getNumericalVariance(), 175 / 18.0, 0); } } ././@LongLink100644 0 0 157 12126630646 10264 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/distribution/AbstractIntegerDistributionTest.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/distribution/AbstractIntegerDistributio100644 1750 1750 6470 12126627667 32542 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.distribution; import org.junit.Assert; import org.junit.Test; /** * Test cases for AbstractIntegerDistribution default implementations. * * @version $Id$ */ public class AbstractIntegerDistributionTest { protected final DiceDistribution diceDistribution = new DiceDistribution(); protected final double p = diceDistribution.probability(1); @Test public void testCumulativeProbabilitiesSingleArguments() { for (int i = 1; i < 7; i++) { Assert.assertEquals(p * i, diceDistribution.cumulativeProbability(i), Double.MIN_VALUE); } Assert.assertEquals(0.0, diceDistribution.cumulativeProbability(0), Double.MIN_VALUE); Assert.assertEquals(1.0, diceDistribution.cumulativeProbability(7), Double.MIN_VALUE); } @Test public void testCumulativeProbabilitiesRangeArguments() { int lower = 0; int upper = 6; for (int i = 0; i < 2; i++) { // cum(0,6) = p(0 < X <= 6) = 1, cum(1,5) = 4/6, cum(2,4) = 2/6 Assert.assertEquals(1 - p * 2 * i, diceDistribution.cumulativeProbability(lower, upper), 1E-12); lower++; upper--; } for (int i = 0; i < 6; i++) { Assert.assertEquals(p, diceDistribution.cumulativeProbability(i, i+1), 1E-12); } } /** * Simple distribution modeling a 6-sided die */ class DiceDistribution extends AbstractIntegerDistribution { public static final long serialVersionUID = 23734213; private final double p = 1d/6d; public DiceDistribution() { super(null); } public double probability(int x) { if (x < 1 || x > 6) { return 0; } else { return p; } } public double cumulativeProbability(int x) { if (x < 1) { return 0; } else if (x >= 6) { return 1; } else { return p * x; } } public double getNumericalMean() { return 3.5; } public double getNumericalVariance() { return 12.5 - 3.5 * 3.5; // E(X^2) - E(X)^2 } public int getSupportLowerBound() { return 1; } public int getSupportUpperBound() { return 6; } public final boolean isSupportConnected() { return true; } } } ././@LongLink100644 0 0 156 12126630646 10263 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/distribution/HypergeometricDistributionTest.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/distribution/HypergeometricDistribution100644 1750 1750 31512 12126627667 32640 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.distribution; import org.apache.commons.math3.TestUtils; import org.apache.commons.math3.exception.NotPositiveException; import org.apache.commons.math3.exception.NotStrictlyPositiveException; import org.apache.commons.math3.exception.NumberIsTooLargeException; import org.apache.commons.math3.util.Precision; import org.junit.Assert; import org.junit.Test; /** * Test cases for HyperGeometriclDistribution. * Extends IntegerDistributionAbstractTest. See class javadoc for * IntegerDistributionAbstractTest for details. * * @version $Id: HypergeometricDistributionTest.java 1364028 2012-07-21 00:42:49Z erans $ */ public class HypergeometricDistributionTest extends IntegerDistributionAbstractTest { //-------------- Implementations for abstract methods ----------------------- /** Creates the default discrete distribution instance to use in tests. */ @Override public IntegerDistribution makeDistribution() { return new HypergeometricDistribution(10, 5, 5); } /** Creates the default probability density test input values */ @Override public int[] makeDensityTestPoints() { return new int[] {-1, 0, 1, 2, 3, 4, 5, 10}; } /** Creates the default probability density test expected values */ @Override public double[] makeDensityTestValues() { return new double[] {0d, 0.003968d, 0.099206d, 0.396825d, 0.396825d, 0.099206d, 0.003968d, 0d}; } /** Creates the default cumulative probability density test input values */ @Override public int[] makeCumulativeTestPoints() { return makeDensityTestPoints(); } /** Creates the default cumulative probability density test expected values */ @Override public double[] makeCumulativeTestValues() { return new double[] {0d, .003968d, .103175d, .50000d, .896825d, .996032d, 1.00000d, 1d}; } /** Creates the default inverse cumulative probability test input values */ @Override public double[] makeInverseCumulativeTestPoints() { return new double[] {0d, 0.001d, 0.010d, 0.025d, 0.050d, 0.100d, 0.999d, 0.990d, 0.975d, 0.950d, 0.900d, 1d}; } /** Creates the default inverse cumulative probability density test expected values */ @Override public int[] makeInverseCumulativeTestValues() { return new int[] {0, 0, 1, 1, 1, 1, 5, 4, 4, 4, 4, 5}; } //-------------------- Additional test cases ------------------------------ /** Verify that if there are no failures, mass is concentrated on sampleSize */ @Test public void testDegenerateNoFailures() { HypergeometricDistribution dist = new HypergeometricDistribution(5,5,3); setDistribution(dist); setCumulativeTestPoints(new int[] {-1, 0, 1, 3, 10 }); setCumulativeTestValues(new double[] {0d, 0d, 0d, 1d, 1d}); setDensityTestPoints(new int[] {-1, 0, 1, 3, 10}); setDensityTestValues(new double[] {0d, 0d, 0d, 1d, 0d}); setInverseCumulativeTestPoints(new double[] {0.1d, 0.5d}); setInverseCumulativeTestValues(new int[] {3, 3}); verifyDensities(); verifyCumulativeProbabilities(); verifyInverseCumulativeProbabilities(); Assert.assertEquals(dist.getSupportLowerBound(), 3); Assert.assertEquals(dist.getSupportUpperBound(), 3); } /** Verify that if there are no successes, mass is concentrated on 0 */ @Test public void testDegenerateNoSuccesses() { HypergeometricDistribution dist = new HypergeometricDistribution(5,0,3); setDistribution(dist); setCumulativeTestPoints(new int[] {-1, 0, 1, 3, 10 }); setCumulativeTestValues(new double[] {0d, 1d, 1d, 1d, 1d}); setDensityTestPoints(new int[] {-1, 0, 1, 3, 10}); setDensityTestValues(new double[] {0d, 1d, 0d, 0d, 0d}); setInverseCumulativeTestPoints(new double[] {0.1d, 0.5d}); setInverseCumulativeTestValues(new int[] {0, 0}); verifyDensities(); verifyCumulativeProbabilities(); verifyInverseCumulativeProbabilities(); Assert.assertEquals(dist.getSupportLowerBound(), 0); Assert.assertEquals(dist.getSupportUpperBound(), 0); } /** Verify that if sampleSize = populationSize, mass is concentrated on numberOfSuccesses */ @Test public void testDegenerateFullSample() { HypergeometricDistribution dist = new HypergeometricDistribution(5,3,5); setDistribution(dist); setCumulativeTestPoints(new int[] {-1, 0, 1, 3, 10 }); setCumulativeTestValues(new double[] {0d, 0d, 0d, 1d, 1d}); setDensityTestPoints(new int[] {-1, 0, 1, 3, 10}); setDensityTestValues(new double[] {0d, 0d, 0d, 1d, 0d}); setInverseCumulativeTestPoints(new double[] {0.1d, 0.5d}); setInverseCumulativeTestValues(new int[] {3, 3}); verifyDensities(); verifyCumulativeProbabilities(); verifyInverseCumulativeProbabilities(); Assert.assertEquals(dist.getSupportLowerBound(), 3); Assert.assertEquals(dist.getSupportUpperBound(), 3); } @Test public void testPreconditions() { try { new HypergeometricDistribution(0, 3, 5); Assert.fail("negative population size. NotStrictlyPositiveException expected"); } catch(NotStrictlyPositiveException ex) { // Expected. } try { new HypergeometricDistribution(5, -1, 5); Assert.fail("negative number of successes. NotPositiveException expected"); } catch(NotPositiveException ex) { // Expected. } try { new HypergeometricDistribution(5, 3, -1); Assert.fail("negative sample size. NotPositiveException expected"); } catch(NotPositiveException ex) { // Expected. } try { new HypergeometricDistribution(5, 6, 5); Assert.fail("numberOfSuccesses > populationSize. NumberIsTooLargeException expected"); } catch(NumberIsTooLargeException ex) { // Expected. } try { new HypergeometricDistribution(5, 3, 6); Assert.fail("sampleSize > populationSize. NumberIsTooLargeException expected"); } catch(NumberIsTooLargeException ex) { // Expected. } } @Test public void testAccessors() { HypergeometricDistribution dist = new HypergeometricDistribution(5, 3, 4); Assert.assertEquals(5, dist.getPopulationSize()); Assert.assertEquals(3, dist.getNumberOfSuccesses()); Assert.assertEquals(4, dist.getSampleSize()); } @Test public void testLargeValues() { int populationSize = 3456; int sampleSize = 789; int numberOfSucceses = 101; double[][] data = { {0.0, 2.75646034603961e-12, 2.75646034603961e-12, 1.0}, {1.0, 8.55705370142386e-11, 8.83269973602783e-11, 0.999999999997244}, {2.0, 1.31288129219665e-9, 1.40120828955693e-9, 0.999999999911673}, {3.0, 1.32724172984193e-8, 1.46736255879763e-8, 0.999999998598792}, {4.0, 9.94501711734089e-8, 1.14123796761385e-7, 0.999999985326375}, {5.0, 5.89080768883643e-7, 7.03204565645028e-7, 0.999999885876203}, {20.0, 0.0760051397707708, 0.27349758476299, 0.802507555007781}, {21.0, 0.087144222047629, 0.360641806810619, 0.72650241523701}, {22.0, 0.0940378846881819, 0.454679691498801, 0.639358193189381}, {23.0, 0.0956897500614809, 0.550369441560282, 0.545320308501199}, {24.0, 0.0919766921922999, 0.642346133752582, 0.449630558439718}, {25.0, 0.083641637261095, 0.725987771013677, 0.357653866247418}, {96.0, 5.93849188852098e-57, 1.0, 6.01900244560712e-57}, {97.0, 7.96593036832547e-59, 1.0, 8.05105570861321e-59}, {98.0, 8.44582921934367e-61, 1.0, 8.5125340287733e-61}, {99.0, 6.63604297068222e-63, 1.0, 6.670480942963e-63}, {100.0, 3.43501099007557e-65, 1.0, 3.4437972280786e-65}, {101.0, 8.78623800302957e-68, 1.0, 8.78623800302957e-68}, }; testHypergeometricDistributionProbabilities(populationSize, sampleSize, numberOfSucceses, data); } private void testHypergeometricDistributionProbabilities(int populationSize, int sampleSize, int numberOfSucceses, double[][] data) { HypergeometricDistribution dist = new HypergeometricDistribution(populationSize, numberOfSucceses, sampleSize); for (int i = 0; i < data.length; ++i) { int x = (int)data[i][0]; double pmf = data[i][1]; double actualPmf = dist.probability(x); TestUtils.assertRelativelyEquals("Expected equals for <"+x+"> pmf",pmf, actualPmf, 1.0e-9); double cdf = data[i][2]; double actualCdf = dist.cumulativeProbability(x); TestUtils.assertRelativelyEquals("Expected equals for <"+x+"> cdf",cdf, actualCdf, 1.0e-9); double cdf1 = data[i][3]; double actualCdf1 = dist.upperCumulativeProbability(x); TestUtils.assertRelativelyEquals("Expected equals for <"+x+"> cdf1",cdf1, actualCdf1, 1.0e-9); } } @Test public void testMoreLargeValues() { int populationSize = 26896; int sampleSize = 895; int numberOfSucceses = 55; double[][] data = { {0.0, 0.155168304750504, 0.155168304750504, 1.0}, {1.0, 0.29437545000746, 0.449543754757964, 0.844831695249496}, {2.0, 0.273841321577003, 0.723385076334967, 0.550456245242036}, {3.0, 0.166488572570786, 0.889873648905753, 0.276614923665033}, {4.0, 0.0743969744713231, 0.964270623377076, 0.110126351094247}, {5.0, 0.0260542785784855, 0.990324901955562, 0.0357293766229237}, {20.0, 3.57101101678792e-16, 1.0, 3.78252101622096e-16}, {21.0, 2.00551638598312e-17, 1.0, 2.11509999433041e-17}, {22.0, 1.04317070180562e-18, 1.0, 1.09583608347287e-18}, {23.0, 5.03153504903308e-20, 1.0, 5.266538166725e-20}, {24.0, 2.2525984149695e-21, 1.0, 2.35003117691919e-21}, {25.0, 9.3677424515947e-23, 1.0, 9.74327619496943e-23}, {50.0, 9.83633962945521e-69, 1.0, 9.8677629437617e-69}, {51.0, 3.13448949497553e-71, 1.0, 3.14233143064882e-71}, {52.0, 7.82755221928122e-74, 1.0, 7.84193567329055e-74}, {53.0, 1.43662126065532e-76, 1.0, 1.43834540093295e-76}, {54.0, 1.72312692517348e-79, 1.0, 1.7241402776278e-79}, {55.0, 1.01335245432581e-82, 1.0, 1.01335245432581e-82}, }; testHypergeometricDistributionProbabilities(populationSize, sampleSize, numberOfSucceses, data); } @Test public void testMoments() { final double tol = 1e-9; HypergeometricDistribution dist; dist = new HypergeometricDistribution(1500, 40, 100); Assert.assertEquals(dist.getNumericalMean(), 40d * 100d / 1500d, tol); Assert.assertEquals(dist.getNumericalVariance(), ( 100d * 40d * (1500d - 100d) * (1500d - 40d) ) / ( (1500d * 1500d * 1499d) ), tol); dist = new HypergeometricDistribution(3000, 55, 200); Assert.assertEquals(dist.getNumericalMean(), 55d * 200d / 3000d, tol); Assert.assertEquals(dist.getNumericalVariance(), ( 200d * 55d * (3000d - 200d) * (3000d - 55d) ) / ( (3000d * 3000d * 2999d) ), tol); } @Test public void testMath644() { int N = 14761461; // population int m = 1035; // successes in population int n = 1841; // number of trials int k = 0; final HypergeometricDistribution dist = new HypergeometricDistribution(N, m, n); Assert.assertTrue(Precision.compareTo(1.0, dist.upperCumulativeProbability(k), 1) == 0); Assert.assertTrue(Precision.compareTo(dist.cumulativeProbability(k), 0.0, 1) > 0); // another way to calculate the upper cumulative probability double upper = 1.0 - dist.cumulativeProbability(k) + dist.probability(k); Assert.assertTrue(Precision.compareTo(1.0, upper, 1) == 0); } } ././@LongLink100644 0 0 150 12126630646 10255 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/distribution/BinomialDistributionTest.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/distribution/BinomialDistributionTest.j100644 1750 1750 13370 12126627667 32476 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with this * work for additional information regarding copyright ownership. The ASF * licenses this file to You 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. */ package org.apache.commons.math3.distribution; import org.junit.Assert; import org.junit.Test; /** * Test cases for BinomialDistribution. Extends IntegerDistributionAbstractTest. * See class javadoc for IntegerDistributionAbstractTest for details. * * @version $Id: BinomialDistributionTest.java 1364028 2012-07-21 00:42:49Z erans $ */ public class BinomialDistributionTest extends IntegerDistributionAbstractTest { // -------------- Implementations for abstract methods // ----------------------- /** Creates the default discrete distribution instance to use in tests. */ @Override public IntegerDistribution makeDistribution() { return new BinomialDistribution(10, 0.70); } /** Creates the default probability density test input values */ @Override public int[] makeDensityTestPoints() { return new int[] { -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 }; } /** Creates the default probability density test expected values */ @Override public double[] makeDensityTestValues() { return new double[] { 0d, 0.0000059049d, 0.000137781d, 0.0014467d, 0.00900169d, 0.0367569d, 0.102919d, 0.200121d, 0.266828d, 0.233474d, 0.121061d, 0.0282475d, 0d }; } /** Creates the default cumulative probability density test input values */ @Override public int[] makeCumulativeTestPoints() { return makeDensityTestPoints(); } /** Creates the default cumulative probability density test expected values */ @Override public double[] makeCumulativeTestValues() { return new double[] { 0d, 0.0000d, 0.0001d, 0.0016d, 0.0106d, 0.0473d, 0.1503d, 0.3504d, 0.6172d, 0.8507d, 0.9718d, 1d, 1d }; } /** Creates the default inverse cumulative probability test input values */ @Override public double[] makeInverseCumulativeTestPoints() { return new double[] { 0, 0.001d, 0.010d, 0.025d, 0.050d, 0.100d, 0.999d, 0.990d, 0.975d, 0.950d, 0.900d, 1 }; } /** * Creates the default inverse cumulative probability density test expected * values */ @Override public int[] makeInverseCumulativeTestValues() { return new int[] { 0, 2, 3, 4, 5, 5, 10, 10, 10, 9, 9, 10 }; } // ----------------- Additional test cases --------------------------------- /** Test degenerate case p = 0 */ @Test public void testDegenerate0() { BinomialDistribution dist = new BinomialDistribution(5, 0.0d); setDistribution(dist); setCumulativeTestPoints(new int[] { -1, 0, 1, 5, 10 }); setCumulativeTestValues(new double[] { 0d, 1d, 1d, 1d, 1d }); setDensityTestPoints(new int[] { -1, 0, 1, 10, 11 }); setDensityTestValues(new double[] { 0d, 1d, 0d, 0d, 0d }); setInverseCumulativeTestPoints(new double[] { 0.1d, 0.5d }); setInverseCumulativeTestValues(new int[] { 0, 0 }); verifyDensities(); verifyCumulativeProbabilities(); verifyInverseCumulativeProbabilities(); Assert.assertEquals(dist.getSupportLowerBound(), 0); Assert.assertEquals(dist.getSupportUpperBound(), 0); } /** Test degenerate case p = 1 */ @Test public void testDegenerate1() { BinomialDistribution dist = new BinomialDistribution(5, 1.0d); setDistribution(dist); setCumulativeTestPoints(new int[] { -1, 0, 1, 2, 5, 10 }); setCumulativeTestValues(new double[] { 0d, 0d, 0d, 0d, 1d, 1d }); setDensityTestPoints(new int[] { -1, 0, 1, 2, 5, 10 }); setDensityTestValues(new double[] { 0d, 0d, 0d, 0d, 1d, 0d }); setInverseCumulativeTestPoints(new double[] { 0.1d, 0.5d }); setInverseCumulativeTestValues(new int[] { 5, 5 }); verifyDensities(); verifyCumulativeProbabilities(); verifyInverseCumulativeProbabilities(); Assert.assertEquals(dist.getSupportLowerBound(), 5); Assert.assertEquals(dist.getSupportUpperBound(), 5); } @Test public void testMoments() { final double tol = 1e-9; BinomialDistribution dist; dist = new BinomialDistribution(10, 0.5); Assert.assertEquals(dist.getNumericalMean(), 10d * 0.5d, tol); Assert.assertEquals(dist.getNumericalVariance(), 10d * 0.5d * 0.5d, tol); dist = new BinomialDistribution(30, 0.3); Assert.assertEquals(dist.getNumericalMean(), 30d * 0.3d, tol); Assert.assertEquals(dist.getNumericalVariance(), 30d * 0.3d * (1d - 0.3d), tol); } @Test public void testMath718() { // for large trials the evaluation of ContinuedFraction was inaccurate // do a sweep over several large trials to test if the current implementation is // numerically stable. for (int trials = 500000; trials < 20000000; trials += 100000) { BinomialDistribution dist = new BinomialDistribution(trials, 0.5); int p = dist.inverseCumulativeProbability(0.5); Assert.assertEquals(trials / 2, p); } } } commons-math3-3.2-src/src/test/java/org/apache/commons/math3/distribution/ZipfDistributionTest.java100644 1750 1750 7237 12126627667 32331 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.distribution; import org.apache.commons.math3.exception.NotStrictlyPositiveException; import org.apache.commons.math3.util.FastMath; import org.junit.Assert; import org.junit.Test; /** * Test cases for {@link ZipfDistribution}. * Extends IntegerDistributionAbstractTest. See class javadoc for * IntegerDistributionAbstractTest for details. * * @version $Id: ZipfDistributionTest.java 1244107 2012-02-14 16:17:55Z erans $ */ public class ZipfDistributionTest extends IntegerDistributionAbstractTest { @Test(expected=NotStrictlyPositiveException.class) public void testPreconditions1() { new ZipfDistribution(0, 1); } @Test(expected=NotStrictlyPositiveException.class) public void testPreconditions2() { new ZipfDistribution(1, 0); } //-------------- Implementations for abstract methods ----------------------- /** Creates the default discrete distribution instance to use in tests. */ @Override public IntegerDistribution makeDistribution() { return new ZipfDistribution(10, 1); } /** Creates the default probability density test input values */ @Override public int[] makeDensityTestPoints() { return new int[] {-1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}; } /** Creates the default probability density test expected values */ @Override public double[] makeDensityTestValues() { return new double[] {0d, 0d, 0.3414d, 0.1707d, 0.1138d, 0.0854d, 0.0683d, 0.0569d, 0.0488d, 0.0427d, 0.0379d, 0.0341d, 0d}; } /** Creates the default cumulative probability density test input values */ @Override public int[] makeCumulativeTestPoints() { return makeDensityTestPoints(); } /** Creates the default cumulative probability density test expected values */ @Override public double[] makeCumulativeTestValues() { return new double[] {0d, 0.0000d, 0.3414d, 0.5121d, 0.6259d, 0.7113d, 0.7796d, 0.8365d, 0.8852d, 0.9279d, 0.9659d, 1d, 1d}; } /** Creates the default inverse cumulative probability test input values */ @Override public double[] makeInverseCumulativeTestPoints() { return new double[] {0d, 0.001d, 0.010d, 0.025d, 0.050d, 0.3413d, 0.3415d, 0.999d, 0.990d, 0.975d, 0.950d, 0.900d, 1d}; } /** Creates the default inverse cumulative probability density test expected values */ @Override public int[] makeInverseCumulativeTestValues() { return new int[] {1, 1, 1, 1, 1, 1, 2, 10, 10, 10, 9, 8, 10}; } @Test public void testMoments() { final double tol = 1e-9; ZipfDistribution dist; dist = new ZipfDistribution(2, 0.5); Assert.assertEquals(dist.getNumericalMean(), FastMath.sqrt(2), tol); Assert.assertEquals(dist.getNumericalVariance(), 0.24264068711928521, tol); } } ././@LongLink100644 0 0 147 12126630646 10263 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/distribution/WeibullDistributionTest.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/distribution/WeibullDistributionTest.ja100644 1750 1750 11257 12126627667 32512 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.distribution; import org.apache.commons.math3.special.Gamma; import org.apache.commons.math3.util.FastMath; import org.apache.commons.math3.exception.NotStrictlyPositiveException; import org.junit.Assert; import org.junit.Test; /** * Test cases for WeibullDistribution. * Extends ContinuousDistributionAbstractTest. See class javadoc for * ContinuousDistributionAbstractTest for details. * * @version $Id: WeibullDistributionTest.java 1364028 2012-07-21 00:42:49Z erans $ */ public class WeibullDistributionTest extends RealDistributionAbstractTest { //-------------- Implementations for abstract methods ----------------------- /** Creates the default continuous distribution instance to use in tests. */ @Override public WeibullDistribution makeDistribution() { return new WeibullDistribution(1.2, 2.1); } /** Creates the default cumulative probability distribution test input values */ @Override public double[] makeCumulativeTestPoints() { // quantiles computed using R version 2.9.2 return new double[] {0.00664355180993, 0.0454328283309, 0.0981162737374, 0.176713524579, 0.321946865392, 10.5115496887, 7.4976304671, 6.23205600701, 5.23968436955, 4.2079028257}; } /** Creates the default cumulative probability density test expected values */ @Override public double[] makeCumulativeTestValues() { return new double[] {0.001, 0.01, 0.025, 0.05, 0.1, 0.999, 0.990, 0.975, 0.950, 0.900}; } /** Creates the default probability density test expected values */ @Override public double[] makeDensityTestValues() { return new double[] {0.180535929306, 0.262801138133, 0.301905425199, 0.330899152971, 0.353441418887, 0.000788590320203, 0.00737060094841, 0.0177576041516, 0.0343043442574, 0.065664589369}; } //---------------------------- Additional test cases ------------------------- @Test public void testInverseCumulativeProbabilityExtremes() { setInverseCumulativeTestPoints(new double[] {0.0, 1.0}); setInverseCumulativeTestValues( new double[] {0.0, Double.POSITIVE_INFINITY}); verifyInverseCumulativeProbabilities(); } @Test public void testAlpha() { WeibullDistribution dist = new WeibullDistribution(1, 2); Assert.assertEquals(1, dist.getShape(), 0); try { dist = new WeibullDistribution(0, 2); Assert.fail("NotStrictlyPositiveException expected"); } catch (NotStrictlyPositiveException e) { // Expected. } } @Test public void testBeta() { WeibullDistribution dist = new WeibullDistribution(1, 2); Assert.assertEquals(2, dist.getScale(), 0); try { dist = new WeibullDistribution(1, 0); Assert.fail("NotStrictlyPositiveException expected"); } catch (NotStrictlyPositiveException e) { // Expected. } } @Test public void testMoments() { final double tol = 1e-9; WeibullDistribution dist; dist = new WeibullDistribution(2.5, 3.5); // In R: 3.5*gamma(1+(1/2.5)) (or emperically: mean(rweibull(10000, 2.5, 3.5))) Assert.assertEquals(dist.getNumericalMean(), 3.5 * FastMath.exp(Gamma.logGamma(1 + (1 / 2.5))), tol); Assert.assertEquals(dist.getNumericalVariance(), (3.5 * 3.5) * FastMath.exp(Gamma.logGamma(1 + (2 / 2.5))) - (dist.getNumericalMean() * dist.getNumericalMean()), tol); dist = new WeibullDistribution(10.4, 2.222); Assert.assertEquals(dist.getNumericalMean(), 2.222 * FastMath.exp(Gamma.logGamma(1 + (1 / 10.4))), tol); Assert.assertEquals(dist.getNumericalVariance(), (2.222 * 2.222) * FastMath.exp(Gamma.logGamma(1 + (2 / 10.4))) - (dist.getNumericalMean() * dist.getNumericalMean()), tol); } } ././@LongLink100644 0 0 153 12126630646 10260 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/distribution/UniformRealDistributionTest.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/distribution/UniformRealDistributionTes100644 1750 1750 10734 12126627667 32554 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.distribution; import org.apache.commons.math3.exception.NumberIsTooLargeException; import org.junit.Assert; import org.junit.Test; /** * Test cases for UniformRealDistribution. See class javadoc for * {@link RealDistributionAbstractTest} for further details. */ public class UniformRealDistributionTest extends RealDistributionAbstractTest { // --- Override tolerance ------------------------------------------------- @Override public void setUp() { super.setUp(); setTolerance(1e-4); } //--- Implementations for abstract methods -------------------------------- /** Creates the default uniform real distribution instance to use in tests. */ @Override public UniformRealDistribution makeDistribution() { return new UniformRealDistribution(-0.5, 1.25); } /** Creates the default cumulative probability distribution test input values */ @Override public double[] makeCumulativeTestPoints() { return new double[] {-0.5001, -0.5, -0.4999, -0.25, -0.0001, 0.0, 0.0001, 0.25, 1.0, 1.2499, 1.25, 1.2501}; } /** Creates the default cumulative probability density test expected values */ @Override public double[] makeCumulativeTestValues() { return new double[] {0.0, 0.0, 0.0001, 0.25/1.75, 0.4999/1.75, 0.5/1.75, 0.5001/1.75, 0.75/1.75, 1.5/1.75, 1.7499/1.75, 1.0, 1.0}; } /** Creates the default probability density test expected values */ @Override public double[] makeDensityTestValues() { double d = 1 / 1.75; return new double[] {0, d, d, d, d, d, d, d, d, d, d, 0}; } //--- Additional test cases ----------------------------------------------- /** Test lower bound getter. */ @Test public void testGetLowerBound() { UniformRealDistribution distribution = makeDistribution(); Assert.assertEquals(-0.5, distribution.getSupportLowerBound(), 0); } /** Test upper bound getter. */ @Test public void testGetUpperBound() { UniformRealDistribution distribution = makeDistribution(); Assert.assertEquals(1.25, distribution.getSupportUpperBound(), 0); } /** Test pre-condition for equal lower/upper bound. */ @Test(expected=NumberIsTooLargeException.class) public void testPreconditions1() { new UniformRealDistribution(0, 0); } /** Test pre-condition for lower bound larger than upper bound. */ @Test(expected=NumberIsTooLargeException.class) public void testPreconditions2() { new UniformRealDistribution(1, 0); } /** Test mean/variance. */ @Test public void testMeanVariance() { UniformRealDistribution dist; dist = new UniformRealDistribution(0, 1); Assert.assertEquals(dist.getNumericalMean(), 0.5, 0); Assert.assertEquals(dist.getNumericalVariance(), 1/12.0, 0); dist = new UniformRealDistribution(-1.5, 0.6); Assert.assertEquals(dist.getNumericalMean(), -0.45, 0); Assert.assertEquals(dist.getNumericalVariance(), 0.3675, 0); dist = new UniformRealDistribution(-0.5, 1.25); Assert.assertEquals(dist.getNumericalMean(), 0.375, 0); Assert.assertEquals(dist.getNumericalVariance(), 0.2552083333333333, 0); } /** * Check accuracy of analytical inverse CDF. Fails if a solver is used * with the default accuracy. */ @Test public void testInverseCumulativeDistribution() { UniformRealDistribution dist = new UniformRealDistribution(0, 1e-9); Assert.assertEquals(2.5e-10, dist.inverseCumulativeProbability(0.25), 0); } } ././@LongLink100644 0 0 154 12126630646 10261 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/distribution/AbstractRealDistributionTest.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/distribution/AbstractRealDistributionTe100644 1750 1750 16370 12126627667 32517 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.distribution; import org.apache.commons.math3.analysis.UnivariateFunction; import org.apache.commons.math3.analysis.integration.RombergIntegrator; import org.apache.commons.math3.analysis.integration.UnivariateIntegrator; import org.apache.commons.math3.exception.OutOfRangeException; import org.junit.Assert; import org.junit.Test; /** Various tests related to MATH-699. */ public class AbstractRealDistributionTest { @Test public void testContinuous() { final double x0 = 0.0; final double x1 = 1.0; final double x2 = 2.0; final double x3 = 3.0; final double p12 = 0.5; final AbstractRealDistribution distribution; distribution = new AbstractRealDistribution(null) { private static final long serialVersionUID = 1L; public double cumulativeProbability(final double x) { if ((x < x0) || (x > x3)) { throw new OutOfRangeException(x, x0, x3); } if (x <= x1) { return p12 * (x - x0) / (x1 - x0); } else if (x <= x2) { return p12; } else if (x <= x3) { return p12 + (1.0 - p12) * (x - x2) / (x3 - x2); } return 0.0; } public double density(final double x) { if ((x < x0) || (x > x3)) { throw new OutOfRangeException(x, x0, x3); } if (x <= x1) { return p12 / (x1 - x0); } else if (x <= x2) { return 0.0; } else if (x <= x3) { return (1.0 - p12) / (x3 - x2); } return 0.0; } public double getNumericalMean() { return ((x0 + x1) * p12 + (x2 + x3) * (1.0 - p12)) / 2.0; } public double getNumericalVariance() { final double meanX = getNumericalMean(); final double meanX2; meanX2 = ((x0 * x0 + x0 * x1 + x1 * x1) * p12 + (x2 * x2 + x2 * x3 + x3 * x3) * (1.0 - p12)) / 3.0; return meanX2 - meanX * meanX; } public double getSupportLowerBound() { return x0; } public double getSupportUpperBound() { return x3; } public boolean isSupportConnected() { return false; } public boolean isSupportLowerBoundInclusive() { return true; } public boolean isSupportUpperBoundInclusive() { return true; } @Override public double probability(final double x) { throw new UnsupportedOperationException(); } }; final double expected = x1; final double actual = distribution.inverseCumulativeProbability(p12); Assert.assertEquals("", expected, actual, distribution.getSolverAbsoluteAccuracy()); } @Test public void testDiscontinuous() { final double x0 = 0.0; final double x1 = 0.25; final double x2 = 0.5; final double x3 = 0.75; final double x4 = 1.0; final double p12 = 1.0 / 3.0; final double p23 = 2.0 / 3.0; final AbstractRealDistribution distribution; distribution = new AbstractRealDistribution(null) { private static final long serialVersionUID = 1L; public double cumulativeProbability(final double x) { if ((x < x0) || (x > x4)) { throw new OutOfRangeException(x, x0, x4); } if (x <= x1) { return p12 * (x - x0) / (x1 - x0); } else if (x <= x2) { return p12; } else if (x <= x3) { return p23; } else { return (1.0 - p23) * (x - x3) / (x4 - x3) + p23; } } public double density(final double x) { if ((x < x0) || (x > x4)) { throw new OutOfRangeException(x, x0, x4); } if (x <= x1) { return p12 / (x1 - x0); } else if (x <= x2) { return 0.0; } else if (x <= x3) { return 0.0; } else { return (1.0 - p23) / (x4 - x3); } } public double getNumericalMean() { final UnivariateFunction f = new UnivariateFunction() { public double value(final double x) { return x * density(x); } }; final UnivariateIntegrator integrator = new RombergIntegrator(); return integrator.integrate(Integer.MAX_VALUE, f, x0, x4); } public double getNumericalVariance() { final double meanX = getNumericalMean(); final UnivariateFunction f = new UnivariateFunction() { public double value(final double x) { return x * x * density(x); } }; final UnivariateIntegrator integrator = new RombergIntegrator(); final double meanX2 = integrator.integrate(Integer.MAX_VALUE, f, x0, x4); return meanX2 - meanX * meanX; } public double getSupportLowerBound() { return x0; } public double getSupportUpperBound() { return x4; } public boolean isSupportConnected() { return false; } public boolean isSupportLowerBoundInclusive() { return true; } public boolean isSupportUpperBoundInclusive() { return true; } @Override public double probability(final double x) { throw new UnsupportedOperationException(); } }; final double expected = x2; final double actual = distribution.inverseCumulativeProbability(p23); Assert.assertEquals("", expected, actual, distribution.getSolverAbsoluteAccuracy()); } } ././@LongLink100644 0 0 146 12126630646 10262 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/distribution/NormalDistributionTest.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/distribution/NormalDistributionTest.jav100644 1750 1750 21373 12126627667 32525 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.distribution; import org.apache.commons.math3.exception.NotStrictlyPositiveException; import org.junit.Assert; import org.junit.Test; /** * Test cases for {@link NormalDistribution}. Extends * {@link RealDistributionAbstractTest}. See class javadoc of that class * for details. * * @version $Id: NormalDistributionTest.java 1364028 2012-07-21 00:42:49Z erans $ */ public class NormalDistributionTest extends RealDistributionAbstractTest { //-------------- Implementations for abstract methods ----------------------- /** Creates the default real distribution instance to use in tests. */ @Override public NormalDistribution makeDistribution() { return new NormalDistribution(2.1, 1.4); } /** Creates the default cumulative probability distribution test input values */ @Override public double[] makeCumulativeTestPoints() { // quantiles computed using R return new double[] {-2.226325228634938d, -1.156887023657177d, -0.643949578356075d, -0.2027950777320613d, 0.305827808237559d, 6.42632522863494d, 5.35688702365718d, 4.843949578356074d, 4.40279507773206d, 3.89417219176244d}; } /** Creates the default cumulative probability density test expected values */ @Override public double[] makeCumulativeTestValues() { return new double[] {0.001d, 0.01d, 0.025d, 0.05d, 0.1d, 0.999d, 0.990d, 0.975d, 0.950d, 0.900d}; } /** Creates the default probability density test expected values */ @Override public double[] makeDensityTestValues() { return new double[] {0.00240506434076, 0.0190372444310, 0.0417464784322, 0.0736683145538, 0.125355951380, 0.00240506434076, 0.0190372444310, 0.0417464784322, 0.0736683145538, 0.125355951380}; } // --------------------- Override tolerance -------------- protected double defaultTolerance = NormalDistribution.DEFAULT_INVERSE_ABSOLUTE_ACCURACY; @Override public void setUp() { super.setUp(); setTolerance(defaultTolerance); } //---------------------------- Additional test cases ------------------------- private void verifyQuantiles() { NormalDistribution distribution = (NormalDistribution) getDistribution(); double mu = distribution.getMean(); double sigma = distribution.getStandardDeviation(); setCumulativeTestPoints( new double[] {mu - 2 *sigma, mu - sigma, mu, mu + sigma, mu + 2 * sigma, mu + 3 * sigma, mu + 4 * sigma, mu + 5 * sigma}); // Quantiles computed using R (same as Mathematica) setCumulativeTestValues(new double[] {0.02275013194817921, 0.158655253931457, 0.5, 0.841344746068543, 0.977249868051821, 0.99865010196837, 0.999968328758167, 0.999999713348428}); verifyCumulativeProbabilities(); } @Test public void testQuantiles() { setDensityTestValues(new double[] {0.0385649760808, 0.172836231799, 0.284958771715, 0.172836231799, 0.0385649760808, 0.00316560600853, 9.55930184035e-05, 1.06194251052e-06}); verifyQuantiles(); verifyDensities(); setDistribution(new NormalDistribution(0, 1)); setDensityTestValues(new double[] {0.0539909665132, 0.241970724519, 0.398942280401, 0.241970724519, 0.0539909665132, 0.00443184841194, 0.000133830225765, 1.48671951473e-06}); verifyQuantiles(); verifyDensities(); setDistribution(new NormalDistribution(0, 0.1)); setDensityTestValues(new double[] {0.539909665132, 2.41970724519, 3.98942280401, 2.41970724519, 0.539909665132, 0.0443184841194, 0.00133830225765, 1.48671951473e-05}); verifyQuantiles(); verifyDensities(); } @Test public void testInverseCumulativeProbabilityExtremes() { setInverseCumulativeTestPoints(new double[] {0, 1}); setInverseCumulativeTestValues( new double[] {Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY}); verifyInverseCumulativeProbabilities(); } @Test public void testGetMean() { NormalDistribution distribution = (NormalDistribution) getDistribution(); Assert.assertEquals(2.1, distribution.getMean(), 0); } @Test public void testGetStandardDeviation() { NormalDistribution distribution = (NormalDistribution) getDistribution(); Assert.assertEquals(1.4, distribution.getStandardDeviation(), 0); } @Test(expected=NotStrictlyPositiveException.class) public void testPreconditions() { new NormalDistribution(1, 0); } @Test public void testDensity() { double [] x = new double[]{-2, -1, 0, 1, 2}; // R 2.5: print(dnorm(c(-2,-1,0,1,2)), digits=10) checkDensity(0, 1, x, new double[]{0.05399096651, 0.24197072452, 0.39894228040, 0.24197072452, 0.05399096651}); // R 2.5: print(dnorm(c(-2,-1,0,1,2), mean=1.1), digits=10) checkDensity(1.1, 1, x, new double[]{0.003266819056,0.043983595980,0.217852177033,0.396952547477,0.266085249899}); } private void checkDensity(double mean, double sd, double[] x, double[] expected) { NormalDistribution d = new NormalDistribution(mean, sd); for (int i = 0; i < x.length; i++) { Assert.assertEquals(expected[i], d.density(x[i]), 1e-9); } } /** * Check to make sure top-coding of extreme values works correctly. * Verifies fixes for JIRA MATH-167, MATH-414 */ @Test public void testExtremeValues() { NormalDistribution distribution = new NormalDistribution(0, 1); for (int i = 0; i < 100; i++) { // make sure no convergence exception double lowerTail = distribution.cumulativeProbability(-i); double upperTail = distribution.cumulativeProbability(i); if (i < 9) { // make sure not top-coded // For i = 10, due to bad tail precision in erf (MATH-364), 1 is returned // TODO: once MATH-364 is resolved, replace 9 with 30 Assert.assertTrue(lowerTail > 0.0d); Assert.assertTrue(upperTail < 1.0d); } else { // make sure top coding not reversed Assert.assertTrue(lowerTail < 0.00001); Assert.assertTrue(upperTail > 0.99999); } } Assert.assertEquals(distribution.cumulativeProbability(Double.MAX_VALUE), 1, 0); Assert.assertEquals(distribution.cumulativeProbability(-Double.MAX_VALUE), 0, 0); Assert.assertEquals(distribution.cumulativeProbability(Double.POSITIVE_INFINITY), 1, 0); Assert.assertEquals(distribution.cumulativeProbability(Double.NEGATIVE_INFINITY), 0, 0); } @Test public void testMath280() { NormalDistribution normal = new NormalDistribution(0,1); double result = normal.inverseCumulativeProbability(0.9986501019683698); Assert.assertEquals(3.0, result, defaultTolerance); result = normal.inverseCumulativeProbability(0.841344746068543); Assert.assertEquals(1.0, result, defaultTolerance); result = normal.inverseCumulativeProbability(0.9999683287581673); Assert.assertEquals(4.0, result, defaultTolerance); result = normal.inverseCumulativeProbability(0.9772498680518209); Assert.assertEquals(2.0, result, defaultTolerance); } @Test public void testMoments() { final double tol = 1e-9; NormalDistribution dist; dist = new NormalDistribution(0, 1); Assert.assertEquals(dist.getNumericalMean(), 0, tol); Assert.assertEquals(dist.getNumericalVariance(), 1, tol); dist = new NormalDistribution(2.2, 1.4); Assert.assertEquals(dist.getNumericalMean(), 2.2, tol); Assert.assertEquals(dist.getNumericalVariance(), 1.4 * 1.4, tol); dist = new NormalDistribution(-2000.9, 10.4); Assert.assertEquals(dist.getNumericalMean(), -2000.9, tol); Assert.assertEquals(dist.getNumericalVariance(), 10.4 * 10.4, tol); } } ././@LongLink100644 0 0 154 12126630646 10261 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/distribution/RealDistributionAbstractTest.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/distribution/RealDistributionAbstractTe100644 1750 1750 46670 12126627667 32525 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.distribution; import java.util.ArrayList; import java.util.Collections; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import org.apache.commons.math3.TestUtils; import org.apache.commons.math3.util.FastMath; import org.apache.commons.math3.analysis.UnivariateFunction; import org.apache.commons.math3.analysis.integration.BaseAbstractUnivariateIntegrator; import org.apache.commons.math3.analysis.integration.IterativeLegendreGaussIntegrator; import org.apache.commons.math3.exception.MathIllegalArgumentException; import org.apache.commons.math3.exception.NumberIsTooLargeException; import org.junit.After; import org.junit.Assert; import org.junit.Before; import org.junit.Test; /** * Abstract base class for {@link RealDistribution} tests. *

                          * To create a concrete test class for a continuous distribution * implementation, first implement makeDistribution() to return a distribution * instance to use in tests. Then implement each of the test data generation * methods below. In each case, the test points and test values arrays * returned represent parallel arrays of inputs and expected values for the * distribution returned by makeDistribution(). Default implementations * are provided for the makeInverseXxx methods that just invert the mapping * defined by the arrays returned by the makeCumulativeXxx methods. *

                          * makeCumulativeTestPoints() -- arguments used to test cumulative probabilities * makeCumulativeTestValues() -- expected cumulative probabilites * makeDensityTestValues() -- expected density values at cumulativeTestPoints * makeInverseCumulativeTestPoints() -- arguments used to test inverse cdf * makeInverseCumulativeTestValues() -- expected inverse cdf values *

                          * To implement additional test cases with different distribution instances and * test data, use the setXxx methods for the instance data in test cases and * call the verifyXxx methods to verify results. *

                          * Error tolerance can be overriden by implementing getTolerance(). *

                          * Test data should be validated against reference tables or other packages * where possible, and the source of the reference data and/or validation * should be documented in the test cases. A framework for validating * distribution data against R is included in the /src/test/R source tree. *

                          * See {@link NormalDistributionTest} and {@link ChiSquaredDistributionTest} * for examples. * * @version $Id: RealDistributionAbstractTest.java 1428822 2013-01-04 12:28:44Z erans $ */ public abstract class RealDistributionAbstractTest { //-------------------- Private test instance data ------------------------- /** Distribution instance used to perform tests */ private RealDistribution distribution; /** Tolerance used in comparing expected and returned values */ private double tolerance = 1E-4; /** Arguments used to test cumulative probability density calculations */ private double[] cumulativeTestPoints; /** Values used to test cumulative probability density calculations */ private double[] cumulativeTestValues; /** Arguments used to test inverse cumulative probability density calculations */ private double[] inverseCumulativeTestPoints; /** Values used to test inverse cumulative probability density calculations */ private double[] inverseCumulativeTestValues; /** Values used to test density calculations */ private double[] densityTestValues; //-------------------- Abstract methods ----------------------------------- /** Creates the default continuous distribution instance to use in tests. */ public abstract RealDistribution makeDistribution(); /** Creates the default cumulative probability test input values */ public abstract double[] makeCumulativeTestPoints(); /** Creates the default cumulative probability test expected values */ public abstract double[] makeCumulativeTestValues(); /** Creates the default density test expected values */ public abstract double[] makeDensityTestValues(); //---- Default implementations of inverse test data generation methods ---- /** Creates the default inverse cumulative probability test input values */ public double[] makeInverseCumulativeTestPoints() { return makeCumulativeTestValues(); } /** Creates the default inverse cumulative probability density test expected values */ public double[] makeInverseCumulativeTestValues() { return makeCumulativeTestPoints(); } //-------------------- Setup / tear down ---------------------------------- /** * Setup sets all test instance data to default values */ @Before public void setUp() { distribution = makeDistribution(); cumulativeTestPoints = makeCumulativeTestPoints(); cumulativeTestValues = makeCumulativeTestValues(); inverseCumulativeTestPoints = makeInverseCumulativeTestPoints(); inverseCumulativeTestValues = makeInverseCumulativeTestValues(); densityTestValues = makeDensityTestValues(); } /** * Cleans up test instance data */ @After public void tearDown() { distribution = null; cumulativeTestPoints = null; cumulativeTestValues = null; inverseCumulativeTestPoints = null; inverseCumulativeTestValues = null; densityTestValues = null; } //-------------------- Verification methods ------------------------------- /** * Verifies that cumulative probability density calculations match expected values * using current test instance data */ protected void verifyCumulativeProbabilities() { // verify cumulativeProbability(double) for (int i = 0; i < cumulativeTestPoints.length; i++) { TestUtils.assertEquals("Incorrect cumulative probability value returned for " + cumulativeTestPoints[i], cumulativeTestValues[i], distribution.cumulativeProbability(cumulativeTestPoints[i]), getTolerance()); } // verify cumulativeProbability(double, double) // XXX In 4.0, "cumulativeProbability(double,double)" must be replaced with "probability" (MATH-839). for (int i = 0; i < cumulativeTestPoints.length; i++) { for (int j = 0; j < cumulativeTestPoints.length; j++) { if (cumulativeTestPoints[i] <= cumulativeTestPoints[j]) { TestUtils.assertEquals(cumulativeTestValues[j] - cumulativeTestValues[i], distribution.cumulativeProbability(cumulativeTestPoints[i], cumulativeTestPoints[j]), getTolerance()); } else { try { distribution.cumulativeProbability(cumulativeTestPoints[i], cumulativeTestPoints[j]); } catch (NumberIsTooLargeException e) { continue; } Assert.fail("distribution.cumulativeProbability(double, double) should have thrown an exception that second argument is too large"); } } } } /** * Verifies that inverse cumulative probability density calculations match expected values * using current test instance data */ protected void verifyInverseCumulativeProbabilities() { for (int i = 0; i < inverseCumulativeTestPoints.length; i++) { TestUtils.assertEquals("Incorrect inverse cumulative probability value returned for " + inverseCumulativeTestPoints[i], inverseCumulativeTestValues[i], distribution.inverseCumulativeProbability(inverseCumulativeTestPoints[i]), getTolerance()); } } /** * Verifies that density calculations match expected values */ protected void verifyDensities() { for (int i = 0; i < cumulativeTestPoints.length; i++) { TestUtils.assertEquals("Incorrect probability density value returned for " + cumulativeTestPoints[i], densityTestValues[i], distribution.density(cumulativeTestPoints[i]), getTolerance()); } } //------------------------ Default test cases ----------------------------- /** * Verifies that cumulative probability density calculations match expected values * using default test instance data */ @Test public void testCumulativeProbabilities() { verifyCumulativeProbabilities(); } /** * Verifies that inverse cumulative probability density calculations match expected values * using default test instance data */ @Test public void testInverseCumulativeProbabilities() { verifyInverseCumulativeProbabilities(); } /** * Verifies that density calculations return expected values * for default test instance data */ @Test public void testDensities() { verifyDensities(); } /** * Verifies that probability computations are consistent */ @Test public void testConsistency() { for (int i=1; i < cumulativeTestPoints.length; i++) { // check that cdf(x, x) = 0 // XXX In 4.0, "cumulativeProbability(double,double)" must be replaced with "probability" (MATH-839). TestUtils.assertEquals(0d, distribution.cumulativeProbability (cumulativeTestPoints[i], cumulativeTestPoints[i]), tolerance); // check that P(a < X <= b) = P(X <= b) - P(X <= a) double upper = FastMath.max(cumulativeTestPoints[i], cumulativeTestPoints[i -1]); double lower = FastMath.min(cumulativeTestPoints[i], cumulativeTestPoints[i -1]); double diff = distribution.cumulativeProbability(upper) - distribution.cumulativeProbability(lower); // XXX In 4.0, "cumulativeProbability(double,double)" must be replaced with "probability" (MATH-839). double direct = distribution.cumulativeProbability(lower, upper); TestUtils.assertEquals("Inconsistent cumulative probabilities for (" + lower + "," + upper + ")", diff, direct, tolerance); } } /** * Verifies that illegal arguments are correctly handled */ @Test public void testIllegalArguments() { try { // XXX In 4.0, "cumulativeProbability(double,double)" must be replaced with "probability" (MATH-839). distribution.cumulativeProbability(1, 0); Assert.fail("Expecting MathIllegalArgumentException for bad cumulativeProbability interval"); } catch (MathIllegalArgumentException ex) { // expected } try { distribution.inverseCumulativeProbability(-1); Assert.fail("Expecting MathIllegalArgumentException for p = -1"); } catch (MathIllegalArgumentException ex) { // expected } try { distribution.inverseCumulativeProbability(2); Assert.fail("Expecting MathIllegalArgumentException for p = 2"); } catch (MathIllegalArgumentException ex) { // expected } } /** * Test sampling */ @Test public void testSampling() { final int sampleSize = 1000; distribution.reseedRandomGenerator(1000); // Use fixed seed double[] sample = distribution.sample(sampleSize); double[] quartiles = TestUtils.getDistributionQuartiles(distribution); double[] expected = {250, 250, 250, 250}; long[] counts = new long[4]; for (int i = 0; i < sampleSize; i++) { TestUtils.updateCounts(sample[i], counts, quartiles); } TestUtils.assertChiSquareAccept(expected, counts, 0.001); } /** * Verify that density integrals match the distribution. * The (filtered, sorted) cumulativeTestPoints array is used to source * integration limits. The integral of the density (estimated using a * Legendre-Gauss integrator) is compared with the cdf over the same * interval. Test points outside of the domain of the density function * are discarded. */ @Test public void testDensityIntegrals() { final double tol = 1.0e-9; final BaseAbstractUnivariateIntegrator integrator = new IterativeLegendreGaussIntegrator(5, 1.0e-12, 1.0e-10); final UnivariateFunction d = new UnivariateFunction() { public double value(double x) { return distribution.density(x); } }; final ArrayList integrationTestPoints = new ArrayList(); for (int i = 0; i < cumulativeTestPoints.length; i++) { if (Double.isNaN(cumulativeTestValues[i]) || cumulativeTestValues[i] < 1.0e-5 || cumulativeTestValues[i] > 1 - 1.0e-5) { continue; // exclude integrals outside domain. } integrationTestPoints.add(cumulativeTestPoints[i]); } Collections.sort(integrationTestPoints); for (int i = 1; i < integrationTestPoints.size(); i++) { Assert.assertEquals( distribution.cumulativeProbability( // FIXME @4.0 when rename happens integrationTestPoints.get(0), integrationTestPoints.get(i)), integrator.integrate( 1000000, // Triangle integrals are very slow to converge d, integrationTestPoints.get(0), integrationTestPoints.get(i)), tol); } } /** * Verify that isSupportLowerBoundInclusvie returns true iff the lower bound * is finite and density is non-NaN, non-infinite there. */ @Test public void testIsSupportLowerBoundInclusive() { final double lowerBound = distribution.getSupportLowerBound(); double result = Double.NaN; result = distribution.density(lowerBound); Assert.assertEquals( !Double.isInfinite(lowerBound) && !Double.isNaN(result) && !Double.isInfinite(result), distribution.isSupportLowerBoundInclusive()); } /** * Verify that isSupportUpperBoundInclusvie returns true iff the upper bound * is finite and density is non-NaN, non-infinite there. */ @Test public void testIsSupportUpperBoundInclusive() { final double upperBound = distribution.getSupportUpperBound(); double result = Double.NaN; result = distribution.density(upperBound); Assert.assertEquals( !Double.isInfinite(upperBound) && !Double.isNaN(result) && !Double.isInfinite(result), distribution.isSupportUpperBoundInclusive()); } @Test public void testDistributionClone() throws IOException, ClassNotFoundException { // Construct a distribution and initialize its internal random // generator, using a fixed seed for deterministic results. distribution.reseedRandomGenerator(123); distribution.sample(); // Clone the distribution. final RealDistribution cloned = deepClone(); // Make sure they still produce the same samples. final double s1 = distribution.sample(); final double s2 = cloned.sample(); Assert.assertEquals(s1, s2, 0d); } //------------------ Getters / Setters for test instance data ----------- /** * @return Returns the cumulativeTestPoints. */ protected double[] getCumulativeTestPoints() { return cumulativeTestPoints; } /** * @param cumulativeTestPoints The cumulativeTestPoints to set. */ protected void setCumulativeTestPoints(double[] cumulativeTestPoints) { this.cumulativeTestPoints = cumulativeTestPoints; } /** * @return Returns the cumulativeTestValues. */ protected double[] getCumulativeTestValues() { return cumulativeTestValues; } /** * @param cumulativeTestValues The cumulativeTestValues to set. */ protected void setCumulativeTestValues(double[] cumulativeTestValues) { this.cumulativeTestValues = cumulativeTestValues; } protected double[] getDensityTestValues() { return densityTestValues; } protected void setDensityTestValues(double[] densityTestValues) { this.densityTestValues = densityTestValues; } /** * @return Returns the distribution. */ protected RealDistribution getDistribution() { return distribution; } /** * @param distribution The distribution to set. */ protected void setDistribution(RealDistribution distribution) { this.distribution = distribution; } /** * @return Returns the inverseCumulativeTestPoints. */ protected double[] getInverseCumulativeTestPoints() { return inverseCumulativeTestPoints; } /** * @param inverseCumulativeTestPoints The inverseCumulativeTestPoints to set. */ protected void setInverseCumulativeTestPoints(double[] inverseCumulativeTestPoints) { this.inverseCumulativeTestPoints = inverseCumulativeTestPoints; } /** * @return Returns the inverseCumulativeTestValues. */ protected double[] getInverseCumulativeTestValues() { return inverseCumulativeTestValues; } /** * @param inverseCumulativeTestValues The inverseCumulativeTestValues to set. */ protected void setInverseCumulativeTestValues(double[] inverseCumulativeTestValues) { this.inverseCumulativeTestValues = inverseCumulativeTestValues; } /** * @return Returns the tolerance. */ protected double getTolerance() { return tolerance; } /** * @param tolerance The tolerance to set. */ protected void setTolerance(double tolerance) { this.tolerance = tolerance; } /** * Serialization and deserialization loop of the {@link #distribution}. */ private RealDistribution deepClone() throws IOException, ClassNotFoundException { // Serialize to byte array. final ByteArrayOutputStream bOut = new ByteArrayOutputStream(); final ObjectOutputStream oOut = new ObjectOutputStream(bOut); oOut.writeObject(distribution); final byte[] data = bOut.toByteArray(); // Deserialize from byte array. final ByteArrayInputStream bIn = new ByteArrayInputStream(data); final ObjectInputStream oIn = new ObjectInputStream(bIn); final Object clone = oIn.readObject(); oIn.close(); return (RealDistribution) clone; } } ././@LongLink100644 0 0 145 12126630646 10261 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/distribution/GammaDistributionTest.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/distribution/GammaDistributionTest.java100644 1750 1750 35254 12126627667 32463 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.distribution; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import org.apache.commons.math3.exception.NotStrictlyPositiveException; import org.apache.commons.math3.special.Gamma; import org.apache.commons.math3.stat.descriptive.SummaryStatistics; import org.apache.commons.math3.util.FastMath; import org.junit.Assert; import org.junit.Test; /** * Test cases for GammaDistribution. * Extends ContinuousDistributionAbstractTest. See class javadoc for * ContinuousDistributionAbstractTest for details. * * @version $Id: GammaDistributionTest.java 1454719 2013-03-09 14:34:05Z luc $ */ public class GammaDistributionTest extends RealDistributionAbstractTest { //-------------- Implementations for abstract methods ----------------------- /** Creates the default continuous distribution instance to use in tests. */ @Override public GammaDistribution makeDistribution() { return new GammaDistribution(4d, 2d); } /** Creates the default cumulative probability distribution test input values */ @Override public double[] makeCumulativeTestPoints() { // quantiles computed using R version 2.9.2 return new double[] {0.857104827257, 1.64649737269, 2.17973074725, 2.7326367935, 3.48953912565, 26.1244815584, 20.0902350297, 17.5345461395, 15.5073130559, 13.3615661365}; } /** Creates the default cumulative probability density test expected values */ @Override public double[] makeCumulativeTestValues() { return new double[] {0.001, 0.01, 0.025, 0.05, 0.1, 0.999, 0.990, 0.975, 0.950, 0.900}; } /** Creates the default probability density test expected values */ @Override public double[] makeDensityTestValues() { return new double[] {0.00427280075546, 0.0204117166709, 0.0362756163658, 0.0542113174239, 0.0773195272491, 0.000394468852816, 0.00366559696761, 0.00874649473311, 0.0166712508128, 0.0311798227954}; } // --------------------- Override tolerance -------------- @Override public void setUp() { super.setUp(); setTolerance(1e-9); } //---------------------------- Additional test cases ------------------------- @Test public void testParameterAccessors() { GammaDistribution distribution = (GammaDistribution) getDistribution(); Assert.assertEquals(4d, distribution.getAlpha(), 0); Assert.assertEquals(2d, distribution.getBeta(), 0); } @Test public void testPreconditions() { try { new GammaDistribution(0, 1); Assert.fail("Expecting NotStrictlyPositiveException for alpha = 0"); } catch (NotStrictlyPositiveException ex) { // Expected. } try { new GammaDistribution(1, 0); Assert.fail("Expecting NotStrictlyPositiveException for alpha = 0"); } catch (NotStrictlyPositiveException ex) { // Expected. } } @Test public void testProbabilities() { testProbability(-1.000, 4.0, 2.0, .0000); testProbability(15.501, 4.0, 2.0, .9499); testProbability(0.504, 4.0, 1.0, .0018); testProbability(10.011, 1.0, 2.0, .9933); testProbability(5.000, 2.0, 2.0, .7127); } @Test public void testValues() { testValue(15.501, 4.0, 2.0, .9499); testValue(0.504, 4.0, 1.0, .0018); testValue(10.011, 1.0, 2.0, .9933); testValue(5.000, 2.0, 2.0, .7127); } private void testProbability(double x, double a, double b, double expected) { GammaDistribution distribution = new GammaDistribution( a, b ); double actual = distribution.cumulativeProbability(x); Assert.assertEquals("probability for " + x, expected, actual, 10e-4); } private void testValue(double expected, double a, double b, double p) { GammaDistribution distribution = new GammaDistribution( a, b ); double actual = distribution.inverseCumulativeProbability(p); Assert.assertEquals("critical value for " + p, expected, actual, 10e-4); } @Test public void testDensity() { double[] x = new double[]{-0.1, 1e-6, 0.5, 1, 2, 5}; // R2.5: print(dgamma(x, shape=1, rate=1), digits=10) checkDensity(1, 1, x, new double[]{0.000000000000, 0.999999000001, 0.606530659713, 0.367879441171, 0.135335283237, 0.006737946999}); // R2.5: print(dgamma(x, shape=2, rate=1), digits=10) checkDensity(2, 1, x, new double[]{0.000000000000, 0.000000999999, 0.303265329856, 0.367879441171, 0.270670566473, 0.033689734995}); // R2.5: print(dgamma(x, shape=4, rate=1), digits=10) checkDensity(4, 1, x, new double[]{0.000000000e+00, 1.666665000e-19, 1.263605541e-02, 6.131324020e-02, 1.804470443e-01, 1.403738958e-01}); // R2.5: print(dgamma(x, shape=4, rate=10), digits=10) checkDensity(4, 10, x, new double[]{0.000000000e+00, 1.666650000e-15, 1.403738958e+00, 7.566654960e-02, 2.748204830e-05, 4.018228850e-17}); // R2.5: print(dgamma(x, shape=.1, rate=10), digits=10) checkDensity(0.1, 10, x, new double[]{0.000000000e+00, 3.323953832e+04, 1.663849010e-03, 6.007786726e-06, 1.461647647e-10, 5.996008322e-24}); // R2.5: print(dgamma(x, shape=.1, rate=20), digits=10) checkDensity(0.1, 20, x, new double[]{0.000000000e+00, 3.562489883e+04, 1.201557345e-05, 2.923295295e-10, 3.228910843e-19, 1.239484589e-45}); // R2.5: print(dgamma(x, shape=.1, rate=4), digits=10) checkDensity(0.1, 4, x, new double[]{0.000000000e+00, 3.032938388e+04, 3.049322494e-02, 2.211502311e-03, 2.170613371e-05, 5.846590589e-11}); // R2.5: print(dgamma(x, shape=.1, rate=1), digits=10) checkDensity(0.1, 1, x, new double[]{0.000000000e+00, 2.640334143e+04, 1.189704437e-01, 3.866916944e-02, 7.623306235e-03, 1.663849010e-04}); } private void checkDensity(double alpha, double rate, double[] x, double[] expected) { GammaDistribution d = new GammaDistribution(alpha, 1 / rate); for (int i = 0; i < x.length; i++) { Assert.assertEquals(expected[i], d.density(x[i]), 1e-5); } } @Test public void testInverseCumulativeProbabilityExtremes() { setInverseCumulativeTestPoints(new double[] {0, 1}); setInverseCumulativeTestValues(new double[] {0, Double.POSITIVE_INFINITY}); verifyInverseCumulativeProbabilities(); } @Test public void testMoments() { final double tol = 1e-9; GammaDistribution dist; dist = new GammaDistribution(1, 2); Assert.assertEquals(dist.getNumericalMean(), 2, tol); Assert.assertEquals(dist.getNumericalVariance(), 4, tol); dist = new GammaDistribution(1.1, 4.2); Assert.assertEquals(dist.getNumericalMean(), 1.1d * 4.2d, tol); Assert.assertEquals(dist.getNumericalVariance(), 1.1d * 4.2d * 4.2d, tol); } private static final double HALF_LOG_2_PI = 0.5 * FastMath.log(2.0 * FastMath.PI); public static double logGamma(double x) { /* * This is a copy of * double Gamma.logGamma(double) * prior to MATH-849 */ double ret; if (Double.isNaN(x) || (x <= 0.0)) { ret = Double.NaN; } else { double sum = Gamma.lanczos(x); double tmp = x + Gamma.LANCZOS_G + .5; ret = ((x + .5) * FastMath.log(tmp)) - tmp + HALF_LOG_2_PI + FastMath.log(sum / x); } return ret; } public static double density(final double x, final double shape, final double scale) { /* * This is a copy of * double GammaDistribution.density(double) * prior to MATH-753. */ if (x < 0) { return 0; } return FastMath.pow(x / scale, shape - 1) / scale * FastMath.exp(-x / scale) / FastMath.exp(logGamma(shape)); } /* * MATH-753: large values of x or shape parameter cause density(double) to * overflow. Reference data is generated with the Maxima script * gamma-distribution.mac, which can be found in * src/test/resources/org/apache/commons/math3/distribution. */ private void doTestMath753(final double shape, final double meanNoOF, final double sdNoOF, final double meanOF, final double sdOF, final String resourceName) throws IOException { final GammaDistribution distribution = new GammaDistribution(shape, 1.0); final SummaryStatistics statOld = new SummaryStatistics(); final SummaryStatistics statNewNoOF = new SummaryStatistics(); final SummaryStatistics statNewOF = new SummaryStatistics(); final InputStream resourceAsStream; resourceAsStream = this.getClass().getResourceAsStream(resourceName); Assert.assertNotNull("Could not find resource " + resourceName, resourceAsStream); final BufferedReader in; in = new BufferedReader(new InputStreamReader(resourceAsStream)); try { for (String line = in.readLine(); line != null; line = in.readLine()) { if (line.startsWith("#")) { continue; } final String[] tokens = line.split(", "); Assert.assertTrue("expected two floating-point values", tokens.length == 2); final double x = Double.parseDouble(tokens[0]); final String msg = "x = " + x + ", shape = " + shape + ", scale = 1.0"; final double expected = Double.parseDouble(tokens[1]); final double ulp = FastMath.ulp(expected); final double actualOld = density(x, shape, 1.0); final double actualNew = distribution.density(x); final double errOld, errNew; errOld = FastMath.abs((actualOld - expected) / ulp); errNew = FastMath.abs((actualNew - expected) / ulp); if (Double.isNaN(actualOld) || Double.isInfinite(actualOld)) { Assert.assertFalse(msg, Double.isNaN(actualNew)); Assert.assertFalse(msg, Double.isInfinite(actualNew)); statNewOF.addValue(errNew); } else { statOld.addValue(errOld); statNewNoOF.addValue(errNew); } } if (statOld.getN() != 0) { /* * If no overflow occurs, check that new implementation is * better than old one. */ final StringBuilder sb = new StringBuilder("shape = "); sb.append(shape); sb.append(", scale = 1.0\n"); sb.append("Old implementation\n"); sb.append("------------------\n"); sb.append(statOld.toString()); sb.append("New implementation\n"); sb.append("------------------\n"); sb.append(statNewNoOF.toString()); final String msg = sb.toString(); final double oldMin = statOld.getMin(); final double newMin = statNewNoOF.getMin(); Assert.assertTrue(msg, newMin <= oldMin); final double oldMax = statOld.getMax(); final double newMax = statNewNoOF.getMax(); Assert.assertTrue(msg, newMax <= oldMax); final double oldMean = statOld.getMean(); final double newMean = statNewNoOF.getMean(); Assert.assertTrue(msg, newMean <= oldMean); final double oldSd = statOld.getStandardDeviation(); final double newSd = statNewNoOF.getStandardDeviation(); Assert.assertTrue(msg, newSd <= oldSd); Assert.assertTrue(msg, newMean <= meanNoOF); Assert.assertTrue(msg, newSd <= sdNoOF); } if (statNewOF.getN() != 0) { final double newMean = statNewOF.getMean(); final double newSd = statNewOF.getStandardDeviation(); final StringBuilder sb = new StringBuilder("shape = "); sb.append(shape); sb.append(", scale = 1.0"); sb.append(", max. mean error (ulps) = "); sb.append(meanOF); sb.append(", actual mean error (ulps) = "); sb.append(newMean); sb.append(", max. sd of error (ulps) = "); sb.append(sdOF); sb.append(", actual sd of error (ulps) = "); sb.append(newSd); final String msg = sb.toString(); Assert.assertTrue(msg, newMean <= meanOF); Assert.assertTrue(msg, newSd <= sdOF); } } catch (IOException e) { Assert.fail(e.getMessage()); } finally { in.close(); } } @Test public void testMath753Shape1() throws IOException { doTestMath753(1.0, 1.5, 0.5, 0.0, 0.0, "gamma-distribution-shape-1.csv"); } @Test public void testMath753Shape8() throws IOException { doTestMath753(8.0, 1.5, 1.0, 0.0, 0.0, "gamma-distribution-shape-8.csv"); } @Test public void testMath753Shape10() throws IOException { doTestMath753(10.0, 1.0, 1.0, 0.0, 0.0, "gamma-distribution-shape-10.csv"); } @Test public void testMath753Shape100() throws IOException { doTestMath753(100.0, 1.5, 1.0, 0.0, 0.0, "gamma-distribution-shape-100.csv"); } @Test public void testMath753Shape142() throws IOException { doTestMath753(142.0, 0.5, 1.5, 40.0, 40.0, "gamma-distribution-shape-142.csv"); } @Test public void testMath753Shape1000() throws IOException { doTestMath753(1000.0, 1.0, 1.0, 160.0, 220.0, "gamma-distribution-shape-1000.csv"); } } ././@LongLink100644 0 0 146 12126630646 10262 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/distribution/PascalDistributionTest.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/distribution/PascalDistributionTest.jav100644 1750 1750 12535 12126627667 32500 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.distribution; import org.junit.Assert; import org.junit.Test; /** * Test cases for PascalDistribution. * Extends IntegerDistributionAbstractTest. See class javadoc for * IntegerDistributionAbstractTest for details. * * @version $Id: PascalDistributionTest.java 1364028 2012-07-21 00:42:49Z erans $ */ public class PascalDistributionTest extends IntegerDistributionAbstractTest { // --------------------- Override tolerance -------------- protected double defaultTolerance = NormalDistribution.DEFAULT_INVERSE_ABSOLUTE_ACCURACY; @Override public void setUp() { super.setUp(); setTolerance(defaultTolerance); } //-------------- Implementations for abstract methods ----------------------- /** Creates the default discrete distribution instance to use in tests. */ @Override public IntegerDistribution makeDistribution() { return new PascalDistribution(10,0.70); } /** Creates the default probability density test input values */ @Override public int[] makeDensityTestPoints() { return new int[] {-1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}; } /** Creates the default probability density test expected values */ @Override public double[] makeDensityTestValues() { return new double[] {0, 0.0282475249, 0.0847425747, 0.139825248255, 0.167790297906, 0.163595540458, 0.137420253985, 0.103065190489, 0.070673273478, 0.0450542118422, 0.0270325271053, 0.0154085404500, 0.0084046584273}; } /** Creates the default cumulative probability density test input values */ @Override public int[] makeCumulativeTestPoints() { return makeDensityTestPoints(); } /** Creates the default cumulative probability density test expected values */ @Override public double[] makeCumulativeTestValues() { return new double[] {0, 0.0282475249, 0.1129900996, 0.252815347855, 0.420605645761, 0.584201186219, 0.721621440204, 0.824686630693, 0.895359904171, 0.940414116013, 0.967446643119, 0.982855183569, 0.991259841996}; } /** Creates the default inverse cumulative probability test input values */ @Override public double[] makeInverseCumulativeTestPoints() { return new double[] {0.0, 0.001, 0.010, 0.025, 0.050, 0.100, 0.999, 0.990, 0.975, 0.950, 0.900, 1.0}; } /** Creates the default inverse cumulative probability density test expected values */ @Override public int[] makeInverseCumulativeTestValues() { return new int[] {0, 0, 0, 0, 1, 1, 14, 11, 10, 9, 8, Integer.MAX_VALUE}; } //----------------- Additional test cases --------------------------------- /** Test degenerate case p = 0 */ @Test public void testDegenerate0() { setDistribution(new PascalDistribution(5, 0.0d)); setCumulativeTestPoints(new int[] {-1, 0, 1, 5, 10 }); setCumulativeTestValues(new double[] {0d, 0d, 0d, 0d, 0d}); setDensityTestPoints(new int[] {-1, 0, 1, 10, 11}); setDensityTestValues(new double[] {0d, 0d, 0d, 0d, 0d}); setInverseCumulativeTestPoints(new double[] {0.1d, 0.5d}); setInverseCumulativeTestValues(new int[] {Integer.MAX_VALUE, Integer.MAX_VALUE}); verifyDensities(); verifyCumulativeProbabilities(); verifyInverseCumulativeProbabilities(); } /** Test degenerate case p = 1 */ @Test public void testDegenerate1() { setDistribution(new PascalDistribution(5, 1.0d)); setCumulativeTestPoints(new int[] {-1, 0, 1, 2, 5, 10 }); setCumulativeTestValues(new double[] {0d, 1d, 1d, 1d, 1d, 1d}); setDensityTestPoints(new int[] {-1, 0, 1, 2, 5, 10}); setDensityTestValues(new double[] {0d, 1d, 0d, 0d, 0d, 0d}); setInverseCumulativeTestPoints(new double[] {0.1d, 0.5d}); setInverseCumulativeTestValues(new int[] {0, 0}); verifyDensities(); verifyCumulativeProbabilities(); verifyInverseCumulativeProbabilities(); } @Test public void testMoments() { final double tol = 1e-9; PascalDistribution dist; dist = new PascalDistribution(10, 0.5); Assert.assertEquals(dist.getNumericalMean(), ( 10d * 0.5d ) / 0.5d, tol); Assert.assertEquals(dist.getNumericalVariance(), ( 10d * 0.5d ) / (0.5d * 0.5d), tol); dist = new PascalDistribution(25, 0.7); Assert.assertEquals(dist.getNumericalMean(), ( 25d * 0.3d ) / 0.7d, tol); Assert.assertEquals(dist.getNumericalVariance(), ( 25d * 0.3d ) / (0.7d * 0.7d), tol); } } commons-math3-3.2-src/src/test/java/org/apache/commons/math3/distribution/LevyDistributionTest.java100644 1750 1750 4765 12126627667 32343 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.distribution; import org.apache.commons.math3.random.Well19937a; import org.apache.commons.math3.util.Precision; import org.junit.Assert; import org.junit.Test; public class LevyDistributionTest extends RealDistributionAbstractTest { @Test public void testParameters() { LevyDistribution d = makeDistribution(); Assert.assertEquals(1.2, d.getLocation(), Precision.EPSILON); Assert.assertEquals(0.4, d.getScale(), Precision.EPSILON); } @Test public void testSupport() { LevyDistribution d = makeDistribution(); Assert.assertEquals(d.getLocation(), d.getSupportLowerBound(), Precision.EPSILON); Assert.assertTrue(Double.isInfinite(d.getSupportUpperBound())); Assert.assertTrue(d.isSupportConnected()); } public LevyDistribution makeDistribution() { return new LevyDistribution(new Well19937a(0xc5a5506bbb17e57al), 1.2, 0.4); } public double[] makeCumulativeTestPoints() { return new double[] { 1.2001, 1.21, 1.225, 1.25, 1.3, 1.9, 3.4, 5.6 }; } public double[] makeCumulativeTestValues() { // values computed with R and function plevy from rmutil package return new double[] { 0, 2.53962850749e-10, 6.33424836662e-05, 0.00467773498105, 0.0455002638964, 0.449691797969, 0.669815357599, 0.763024600553 }; } public double[] makeDensityTestValues() { // values computed with R and function dlevy from rmutil package return new double[] { 0, 5.20056373765e-07, 0.0214128361224, 0.413339707082, 1.07981933026, 0.323749319161, 0.0706032550094, 0.026122839884 }; } } ././@LongLink100644 0 0 156 12126630646 10263 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/distribution/EnumeratedRealDistributionTest.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/distribution/EnumeratedRealDistribution100644 1750 1750 17375 12126627667 32562 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.distribution; import java.util.ArrayList; import java.util.List; import org.apache.commons.math3.exception.DimensionMismatchException; import org.apache.commons.math3.exception.MathArithmeticException; import org.apache.commons.math3.exception.NotANumberException; import org.apache.commons.math3.exception.NotFiniteNumberException; import org.apache.commons.math3.exception.NotPositiveException; import org.apache.commons.math3.util.FastMath; import org.apache.commons.math3.util.Pair; import org.junit.Assert; import org.junit.Test; /** * Test class for {@link EnumeratedRealDistribution}. * * @version $Id: EnumeratedRealDistributionTest.java 1456769 2013-03-15 04:51:34Z psteitz $ */ public class EnumeratedRealDistributionTest { /** * The distribution object used for testing. */ private final EnumeratedRealDistribution testDistribution; /** * Creates the default distribution object used for testing. */ public EnumeratedRealDistributionTest() { // Non-sorted singleton array with duplicates should be allowed. // Values with zero-probability do not extend the support. testDistribution = new EnumeratedRealDistribution( new double[]{3.0, -1.0, 3.0, 7.0, -2.0, 8.0}, new double[]{0.2, 0.2, 0.3, 0.3, 0.0, 0.0}); } /** * Tests if the {@link EnumeratedRealDistribution} constructor throws * exceptions for invalid data. */ @Test public void testExceptions() { EnumeratedRealDistribution invalid = null; try { invalid = new EnumeratedRealDistribution(new double[]{1.0, 2.0}, new double[]{0.0}); Assert.fail("Expected DimensionMismatchException"); } catch (DimensionMismatchException e) { } try{ invalid = new EnumeratedRealDistribution(new double[]{1.0, 2.0}, new double[]{0.0, -1.0}); Assert.fail("Expected NotPositiveException"); } catch (NotPositiveException e) { } try { invalid = new EnumeratedRealDistribution(new double[]{1.0, 2.0}, new double[]{0.0, 0.0}); Assert.fail("Expected MathArithmeticException"); } catch (MathArithmeticException e) { } try { invalid = new EnumeratedRealDistribution(new double[]{1.0, 2.0}, new double[]{0.0, Double.NaN}); Assert.fail("Expected NotANumberException"); } catch (NotANumberException e) { } try { invalid = new EnumeratedRealDistribution(new double[]{1.0, 2.0}, new double[]{0.0, Double.POSITIVE_INFINITY}); Assert.fail("Expected NotFiniteNumberException"); } catch (NotFiniteNumberException e) { } Assert.assertNull("Expected non-initialized DiscreteRealDistribution", invalid); } /** * Tests if the distribution returns proper probability values. */ @Test public void testProbability() { double[] points = new double[]{-2.0, -1.0, 0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0}; double[] results = new double[]{0, 0.2, 0, 0, 0, 0.5, 0, 0, 0, 0.3, 0}; for (int p = 0; p < points.length; p++) { double density = testDistribution.probability(points[p]); Assert.assertEquals(results[p], density, 0.0); } } /** * Tests if the distribution returns proper density values. */ @Test public void testDensity() { double[] points = new double[]{-2.0, -1.0, 0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0}; double[] results = new double[]{0, 0.2, 0, 0, 0, 0.5, 0, 0, 0, 0.3, 0}; for (int p = 0; p < points.length; p++) { double density = testDistribution.density(points[p]); Assert.assertEquals(results[p], density, 0.0); } } /** * Tests if the distribution returns proper cumulative probability values. */ @Test public void testCumulativeProbability() { double[] points = new double[]{-2.0, -1.0, 0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0}; double[] results = new double[]{0, 0.2, 0.2, 0.2, 0.2, 0.7, 0.7, 0.7, 0.7, 1.0, 1.0}; for (int p = 0; p < points.length; p++) { double probability = testDistribution.cumulativeProbability(points[p]); Assert.assertEquals(results[p], probability, 1e-10); } } /** * Tests if the distribution returns proper mean value. */ @Test public void testGetNumericalMean() { Assert.assertEquals(3.4, testDistribution.getNumericalMean(), 1e-10); } /** * Tests if the distribution returns proper variance. */ @Test public void testGetNumericalVariance() { Assert.assertEquals(7.84, testDistribution.getNumericalVariance(), 1e-10); } /** * Tests if the distribution returns proper lower bound. */ @Test public void testGetSupportLowerBound() { Assert.assertEquals(-1, testDistribution.getSupportLowerBound(), 0); } /** * Tests if the distribution returns proper upper bound. */ @Test public void testGetSupportUpperBound() { Assert.assertEquals(7, testDistribution.getSupportUpperBound(), 0); } /** * Tests if the distribution returns properly that the support includes the * lower bound. */ @Test public void testIsSupportLowerBoundInclusive() { Assert.assertTrue(testDistribution.isSupportLowerBoundInclusive()); } /** * Tests if the distribution returns properly that the support includes the * upper bound. */ @Test public void testIsSupportUpperBoundInclusive() { Assert.assertTrue(testDistribution.isSupportUpperBoundInclusive()); } /** * Tests if the distribution returns properly that the support is connected. */ @Test public void testIsSupportConnected() { Assert.assertTrue(testDistribution.isSupportConnected()); } /** * Tests sampling. */ @Test public void testSample() { final int n = 1000000; testDistribution.reseedRandomGenerator(-334759360); // fixed seed final double[] samples = testDistribution.sample(n); Assert.assertEquals(n, samples.length); double sum = 0; double sumOfSquares = 0; for (int i = 0; i < samples.length; i++) { sum += samples[i]; sumOfSquares += samples[i] * samples[i]; } Assert.assertEquals(testDistribution.getNumericalMean(), sum / n, 1e-2); Assert.assertEquals(testDistribution.getNumericalVariance(), sumOfSquares / n - FastMath.pow(sum / n, 2), 1e-2); } @Test public void testIssue942() { List> list = new ArrayList>(); list.add(new Pair(new Object() {}, new Double(0))); list.add(new Pair(new Object() {}, new Double(1))); Assert.assertEquals(1, new EnumeratedDistribution(list).sample(1).length); } } ././@LongLink100644 0 0 161 12126630646 10257 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/distribution/EnumeratedIntegerDistributionTest.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/distribution/EnumeratedIntegerDistribut100644 1750 1750 14261 12126627667 32555 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.distribution; import org.apache.commons.math3.exception.DimensionMismatchException; import org.apache.commons.math3.exception.MathArithmeticException; import org.apache.commons.math3.exception.NotANumberException; import org.apache.commons.math3.exception.NotFiniteNumberException; import org.apache.commons.math3.exception.NotPositiveException; import org.apache.commons.math3.util.FastMath; import org.junit.Assert; import org.junit.Test; /** * Test class for {@link EnumeratedIntegerDistribution}. * * @version $Id: EnumeratedIntegerDistributionTest.java 1456769 2013-03-15 04:51:34Z psteitz $ */ public class EnumeratedIntegerDistributionTest { /** * The distribution object used for testing. */ private final EnumeratedIntegerDistribution testDistribution; /** * Creates the default distribution object used for testing. */ public EnumeratedIntegerDistributionTest() { // Non-sorted singleton array with duplicates should be allowed. // Values with zero-probability do not extend the support. testDistribution = new EnumeratedIntegerDistribution( new int[]{3, -1, 3, 7, -2, 8}, new double[]{0.2, 0.2, 0.3, 0.3, 0.0, 0.0}); } /** * Tests if the EnumeratedIntegerDistribution constructor throws * exceptions for invalid data. */ @Test public void testExceptions() { EnumeratedIntegerDistribution invalid = null; try { new EnumeratedIntegerDistribution(new int[]{1, 2}, new double[]{0.0}); Assert.fail("Expected DimensionMismatchException"); } catch (DimensionMismatchException e) { } try { new EnumeratedIntegerDistribution(new int[]{1, 2}, new double[]{0.0, -1.0}); Assert.fail("Expected NotPositiveException"); } catch (NotPositiveException e) { } try { new EnumeratedIntegerDistribution(new int[]{1, 2}, new double[]{0.0, 0.0}); Assert.fail("Expected MathArithmeticException"); } catch (MathArithmeticException e) { } try { new EnumeratedIntegerDistribution(new int[]{1, 2}, new double[]{0.0, Double.NaN}); Assert.fail("Expected NotANumberException"); } catch (NotANumberException e) { } try { new EnumeratedIntegerDistribution(new int[]{1, 2}, new double[]{0.0, Double.POSITIVE_INFINITY}); Assert.fail("Expected NotFiniteNumberException"); } catch (NotFiniteNumberException e) { } Assert.assertNull("Expected non-initialized DiscreteRealDistribution", invalid); } /** * Tests if the distribution returns proper probability values. */ @Test public void testProbability() { int[] points = new int[]{-2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8}; double[] results = new double[]{0, 0.2, 0, 0, 0, 0.5, 0, 0, 0, 0.3, 0}; for (int p = 0; p < points.length; p++) { double probability = testDistribution.probability(points[p]); Assert.assertEquals(results[p], probability, 0.0); } } /** * Tests if the distribution returns proper cumulative probability values. */ @Test public void testCumulativeProbability() { int[] points = new int[]{-2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8}; double[] results = new double[]{0, 0.2, 0.2, 0.2, 0.2, 0.7, 0.7, 0.7, 0.7, 1.0, 1.0}; for (int p = 0; p < points.length; p++) { double probability = testDistribution.cumulativeProbability(points[p]); Assert.assertEquals(results[p], probability, 1e-10); } } /** * Tests if the distribution returns proper mean value. */ @Test public void testGetNumericalMean() { Assert.assertEquals(3.4, testDistribution.getNumericalMean(), 1e-10); } /** * Tests if the distribution returns proper variance. */ @Test public void testGetNumericalVariance() { Assert.assertEquals(7.84, testDistribution.getNumericalVariance(), 1e-10); } /** * Tests if the distribution returns proper lower bound. */ @Test public void testGetSupportLowerBound() { Assert.assertEquals(-1, testDistribution.getSupportLowerBound()); } /** * Tests if the distribution returns proper upper bound. */ @Test public void testGetSupportUpperBound() { Assert.assertEquals(7, testDistribution.getSupportUpperBound()); } /** * Tests if the distribution returns properly that the support is connected. */ @Test public void testIsSupportConnected() { Assert.assertTrue(testDistribution.isSupportConnected()); } /** * Tests sampling. */ @Test public void testSample() { final int n = 1000000; testDistribution.reseedRandomGenerator(-334759360); // fixed seed final int[] samples = testDistribution.sample(n); Assert.assertEquals(n, samples.length); double sum = 0; double sumOfSquares = 0; for (int i = 0; i < samples.length; i++) { sum += samples[i]; sumOfSquares += samples[i] * samples[i]; } Assert.assertEquals(testDistribution.getNumericalMean(), sum / n, 1e-2); Assert.assertEquals(testDistribution.getNumericalVariance(), sumOfSquares / n - FastMath.pow(sum / n, 2), 1e-2); } } ././@LongLink100644 0 0 151 12126630646 10256 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/distribution/LogNormalDistributionTest.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/distribution/LogNormalDistributionTest.100644 1750 1750 24434 12126627667 32467 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.distribution; import org.apache.commons.math3.exception.NotStrictlyPositiveException; import org.junit.Assert; import org.junit.Test; /** * Test cases for {@link LogNormalDistribution}. Extends * {@link RealDistributionAbstractTest}. See class javadoc of that class * for details. * * @version $Id: LogNormalDistributionTest.java 1364028 2012-07-21 00:42:49Z erans $ * @since 3.0 */ public class LogNormalDistributionTest extends RealDistributionAbstractTest { //-------------- Implementations for abstract methods ----------------------- /** Creates the default real distribution instance to use in tests. */ @Override public LogNormalDistribution makeDistribution() { return new LogNormalDistribution(2.1, 1.4); } /** Creates the default cumulative probability distribution test input values */ @Override public double[] makeCumulativeTestPoints() { // quantiles computed using R return new double[] { -2.226325228634938, -1.156887023657177, -0.643949578356075, -0.2027950777320613, 0.305827808237559, 6.42632522863494, 5.35688702365718, 4.843949578356074, 4.40279507773206, 3.89417219176244 }; } /** Creates the default cumulative probability density test expected values */ @Override public double[] makeCumulativeTestValues() { return new double[] { 0, 0, 0, 0, 0.00948199951485, 0.432056525076, 0.381648158697, 0.354555726206, 0.329513316888, 0.298422824228 }; } /** Creates the default probability density test expected values */ @Override public double[] makeDensityTestValues() { return new double[] { 0, 0, 0, 0, 0.0594218160072, 0.0436977691036, 0.0508364857798, 0.054873528325, 0.0587182664085, 0.0636229042785 }; } /** * Creates the default inverse cumulative probability distribution test * input values. */ @Override public double[] makeInverseCumulativeTestPoints() { // Exclude the test points less than zero, as they have cumulative // probability of zero, meaning the inverse returns zero, and not the // points less than zero. double[] points = makeCumulativeTestValues(); double[] points2 = new double[points.length - 4]; System.arraycopy(points, 4, points2, 0, points2.length - 4); return points2; //return Arrays.copyOfRange(points, 4, points.length - 4); } /** * Creates the default inverse cumulative probability test expected * values. */ @Override public double[] makeInverseCumulativeTestValues() { // Exclude the test points less than zero, as they have cumulative // probability of zero, meaning the inverse returns zero, and not the // points less than zero. double[] points = makeCumulativeTestPoints(); double[] points2 = new double[points.length - 4]; System.arraycopy(points, 4, points2, 0, points2.length - 4); return points2; //return Arrays.copyOfRange(points, 1, points.length - 4); } // --------------------- Override tolerance -------------- @Override public void setUp() { super.setUp(); setTolerance(LogNormalDistribution.DEFAULT_INVERSE_ABSOLUTE_ACCURACY); } //---------------------------- Additional test cases ------------------------- private void verifyQuantiles() { LogNormalDistribution distribution = (LogNormalDistribution)getDistribution(); double mu = distribution.getScale(); double sigma = distribution.getShape(); setCumulativeTestPoints( new double[] { mu - 2 *sigma, mu - sigma, mu, mu + sigma, mu + 2 * sigma, mu + 3 * sigma,mu + 4 * sigma, mu + 5 * sigma }); verifyCumulativeProbabilities(); } @Test public void testQuantiles() { setCumulativeTestValues(new double[] {0, 0.0396495152787, 0.16601209243, 0.272533253269, 0.357618409638, 0.426488363093, 0.483255136841, 0.530823013877}); setDensityTestValues(new double[] {0, 0.0873055825147, 0.0847676303432, 0.0677935186237, 0.0544105523058, 0.0444614628804, 0.0369750288945, 0.0312206409653}); verifyQuantiles(); verifyDensities(); setDistribution(new LogNormalDistribution(0, 1)); setCumulativeTestValues(new double[] {0, 0, 0, 0.5, 0.755891404214, 0.864031392359, 0.917171480998, 0.946239689548}); setDensityTestValues(new double[] {0, 0, 0, 0.398942280401, 0.156874019279, 0.07272825614, 0.0381534565119, 0.0218507148303}); verifyQuantiles(); verifyDensities(); setDistribution(new LogNormalDistribution(0, 0.1)); setCumulativeTestValues(new double[] {0, 0, 0, 1.28417563064e-117, 1.39679883412e-58, 1.09839325447e-33, 2.52587961726e-20, 2.0824223487e-12}); setDensityTestValues(new double[] {0, 0, 0, 2.96247992535e-114, 1.1283370232e-55, 4.43812313223e-31, 5.85346445002e-18, 2.9446618076e-10}); verifyQuantiles(); verifyDensities(); } @Test public void testInverseCumulativeProbabilityExtremes() { setInverseCumulativeTestPoints(new double[] {0, 1}); setInverseCumulativeTestValues( new double[] {0, Double.POSITIVE_INFINITY}); verifyInverseCumulativeProbabilities(); } @Test public void testGetScale() { LogNormalDistribution distribution = (LogNormalDistribution)getDistribution(); Assert.assertEquals(2.1, distribution.getScale(), 0); } @Test public void testGetShape() { LogNormalDistribution distribution = (LogNormalDistribution)getDistribution(); Assert.assertEquals(1.4, distribution.getShape(), 0); } @Test(expected=NotStrictlyPositiveException.class) public void testPreconditions() { new LogNormalDistribution(1, 0); } @Test public void testDensity() { double [] x = new double[]{-2, -1, 0, 1, 2}; // R 2.13: print(dlnorm(c(-2,-1,0,1,2)), digits=10) checkDensity(0, 1, x, new double[] { 0.0000000000, 0.0000000000, 0.0000000000, 0.3989422804, 0.1568740193 }); // R 2.13: print(dlnorm(c(-2,-1,0,1,2), mean=1.1), digits=10) checkDensity(1.1, 1, x, new double[] { 0.0000000000, 0.0000000000, 0.0000000000, 0.2178521770, 0.1836267118}); } private void checkDensity(double scale, double shape, double[] x, double[] expected) { LogNormalDistribution d = new LogNormalDistribution(scale, shape); for (int i = 0; i < x.length; i++) { Assert.assertEquals(expected[i], d.density(x[i]), 1e-9); } } /** * Check to make sure top-coding of extreme values works correctly. * Verifies fixes for JIRA MATH-167, MATH-414 */ @Test public void testExtremeValues() { LogNormalDistribution d = new LogNormalDistribution(0, 1); for (int i = 0; i < 1e5; i++) { // make sure no convergence exception double upperTail = d.cumulativeProbability(i); if (i <= 72) { // make sure not top-coded Assert.assertTrue(upperTail < 1.0d); } else { // make sure top coding not reversed Assert.assertTrue(upperTail > 0.99999); } } Assert.assertEquals(d.cumulativeProbability(Double.MAX_VALUE), 1, 0); Assert.assertEquals(d.cumulativeProbability(-Double.MAX_VALUE), 0, 0); Assert.assertEquals(d.cumulativeProbability(Double.POSITIVE_INFINITY), 1, 0); Assert.assertEquals(d.cumulativeProbability(Double.NEGATIVE_INFINITY), 0, 0); } @Test public void testMeanVariance() { final double tol = 1e-9; LogNormalDistribution dist; dist = new LogNormalDistribution(0, 1); Assert.assertEquals(dist.getNumericalMean(), 1.6487212707001282, tol); Assert.assertEquals(dist.getNumericalVariance(), 4.670774270471604, tol); dist = new LogNormalDistribution(2.2, 1.4); Assert.assertEquals(dist.getNumericalMean(), 24.046753552064498, tol); Assert.assertEquals(dist.getNumericalVariance(), 3526.913651880464, tol); dist = new LogNormalDistribution(-2000.9, 10.4); Assert.assertEquals(dist.getNumericalMean(), 0.0, tol); Assert.assertEquals(dist.getNumericalVariance(), 0.0, tol); } } commons-math3-3.2-src/src/test/java/org/apache/commons/math3/distribution/FDistributionTest.java100644 1750 1750 13527 12126627667 31625 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.distribution; import org.apache.commons.math3.exception.NotStrictlyPositiveException; import org.junit.Assert; import org.junit.Test; /** * Test cases for FDistribution. * Extends ContinuousDistributionAbstractTest. See class javadoc for * ContinuousDistributionAbstractTest for details. * * @version $Id: FDistributionTest.java 1364028 2012-07-21 00:42:49Z erans $ */ public class FDistributionTest extends RealDistributionAbstractTest { //-------------- Implementations for abstract methods ----------------------- /** Creates the default continuous distribution instance to use in tests. */ @Override public FDistribution makeDistribution() { return new FDistribution(5.0, 6.0); } /** Creates the default cumulative probability distribution test input values */ @Override public double[] makeCumulativeTestPoints() { // quantiles computed using R version 2.9.2 return new double[] {0.0346808448626, 0.0937009113303, 0.143313661184, 0.202008445998, 0.293728320107, 20.8026639595, 8.74589525602, 5.98756512605, 4.38737418741, 3.10751166664}; } /** Creates the default cumulative probability density test expected values */ @Override public double[] makeCumulativeTestValues() { return new double[] {0.001, 0.01, 0.025, 0.05, 0.1, 0.999, 0.990, 0.975, 0.950, 0.900}; } /** Creates the default probability density test expected values */ @Override public double[] makeDensityTestValues() { return new double[] {0.0689156576706, 0.236735653193, 0.364074131941, 0.481570789649, 0.595880479994, 0.000133443915657, 0.00286681303403, 0.00969192007502, 0.0242883861471, 0.0605491314658}; } // --------------------- Override tolerance -------------- @Override public void setUp() { super.setUp(); setTolerance(1e-9); } //---------------------------- Additional test cases ------------------------- @Test public void testCumulativeProbabilityExtremes() { setCumulativeTestPoints(new double[] {-2, 0}); setCumulativeTestValues(new double[] {0, 0}); verifyCumulativeProbabilities(); } @Test public void testInverseCumulativeProbabilityExtremes() { setInverseCumulativeTestPoints(new double[] {0, 1}); setInverseCumulativeTestValues(new double[] {0, Double.POSITIVE_INFINITY}); verifyInverseCumulativeProbabilities(); } @Test public void testDfAccessors() { FDistribution dist = (FDistribution) getDistribution(); Assert.assertEquals(5d, dist.getNumeratorDegreesOfFreedom(), Double.MIN_VALUE); Assert.assertEquals(6d, dist.getDenominatorDegreesOfFreedom(), Double.MIN_VALUE); } @Test public void testPreconditions() { try { new FDistribution(0, 1); Assert.fail("Expecting NotStrictlyPositiveException for df = 0"); } catch (NotStrictlyPositiveException ex) { // Expected. } try { new FDistribution(1, 0); Assert.fail("Expecting NotStrictlyPositiveException for df = 0"); } catch (NotStrictlyPositiveException ex) { // Expected. } } @Test public void testLargeDegreesOfFreedom() { FDistribution fd = new FDistribution(100000, 100000); double p = fd.cumulativeProbability(.999); double x = fd.inverseCumulativeProbability(p); Assert.assertEquals(.999, x, 1.0e-5); } @Test public void testSmallDegreesOfFreedom() { FDistribution fd = new FDistribution(1, 1); double p = fd.cumulativeProbability(0.975); double x = fd.inverseCumulativeProbability(p); Assert.assertEquals(0.975, x, 1.0e-5); fd = new FDistribution(1, 2); p = fd.cumulativeProbability(0.975); x = fd.inverseCumulativeProbability(p); Assert.assertEquals(0.975, x, 1.0e-5); } @Test public void testMoments() { final double tol = 1e-9; FDistribution dist; dist = new FDistribution(1, 2); Assert.assertTrue(Double.isNaN(dist.getNumericalMean())); Assert.assertTrue(Double.isNaN(dist.getNumericalVariance())); dist = new FDistribution(1, 3); Assert.assertEquals(dist.getNumericalMean(), 3d / (3d - 2d), tol); Assert.assertTrue(Double.isNaN(dist.getNumericalVariance())); dist = new FDistribution(1, 5); Assert.assertEquals(dist.getNumericalMean(), 5d / (5d - 2d), tol); Assert.assertEquals(dist.getNumericalVariance(), (2d * 5d * 5d * 4d) / 9d, tol); } @Test public void testMath785() { // this test was failing due to inaccurate results from ContinuedFraction. try { double prob = 0.01; FDistribution f = new FDistribution(200000, 200000); double result = f.inverseCumulativeProbability(prob); Assert.assertTrue(result < 1.0); } catch (Exception e) { Assert.fail("Failing to calculate inverse cumulative probability"); } } } ././@LongLink100644 0 0 147 12126630646 10263 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/distribution/PoissonDistributionTest.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/distribution/PoissonDistributionTest.ja100644 1750 1750 20515 12126627667 32536 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.distribution; import org.apache.commons.math3.util.FastMath; import org.apache.commons.math3.exception.NotStrictlyPositiveException; import org.junit.Assert; import org.junit.Test; /** * PoissonDistributionTest * * @version $Id: PoissonDistributionTest.java 1370224 2012-08-07 12:43:41Z sebb $ */ public class PoissonDistributionTest extends IntegerDistributionAbstractTest { /** * Poisson parameter value for the test distribution. */ private static final double DEFAULT_TEST_POISSON_PARAMETER = 4.0; /** * Constructor. */ public PoissonDistributionTest() { setTolerance(1e-12); } /** * Creates the default discrete distribution instance to use in tests. */ @Override public IntegerDistribution makeDistribution() { return new PoissonDistribution(DEFAULT_TEST_POISSON_PARAMETER); } /** * Creates the default probability density test input values. */ @Override public int[] makeDensityTestPoints() { return new int[] { -1, 0, 1, 2, 3, 4, 5, 10, 20}; } /** * Creates the default probability density test expected values. * These and all other test values are generated by R, version 1.8.1 */ @Override public double[] makeDensityTestValues() { return new double[] { 0d, 0.0183156388887d, 0.073262555555d, 0.14652511111d, 0.195366814813d, 0.195366814813, 0.156293451851d, 0.00529247667642d, 8.27746364655e-09}; } /** * Creates the default cumulative probability density test input values. */ @Override public int[] makeCumulativeTestPoints() { return new int[] { -1, 0, 1, 2, 3, 4, 5, 10, 20 }; } /** * Creates the default cumulative probability density test expected values. */ @Override public double[] makeCumulativeTestValues() { return new double[] { 0d, 0.0183156388887d, 0.0915781944437d, 0.238103305554d, 0.433470120367d, 0.62883693518, 0.78513038703d, 0.99716023388d, 0.999999998077 }; } /** * Creates the default inverse cumulative probability test input values. */ @Override public double[] makeInverseCumulativeTestPoints() { IntegerDistribution dist = getDistribution(); return new double[] { 0d, 0.018315638886d, 0.018315638890d, 0.091578194441d, 0.091578194445d, 0.238103305552d, 0.238103305556d, dist.cumulativeProbability(3), dist.cumulativeProbability(4), dist.cumulativeProbability(5), dist.cumulativeProbability(10), dist.cumulativeProbability(20)}; } /** * Creates the default inverse cumulative probability density test expected values. */ @Override public int[] makeInverseCumulativeTestValues() { return new int[] { 0, 0, 1, 1, 2, 2, 3, 3, 4, 5, 10, 20}; } /** * Test the normal approximation of the Poisson distribution by * calculating P(90 ≤ X ≤ 110) for X = Po(100) and * P(9900 ≤ X ≤ 10200) for X = Po(10000) */ @Test public void testNormalApproximateProbability() { PoissonDistribution dist = new PoissonDistribution(100); double result = dist.normalApproximateProbability(110) - dist.normalApproximateProbability(89); Assert.assertEquals(0.706281887248, result, 1E-10); dist = new PoissonDistribution(10000); result = dist.normalApproximateProbability(10200) - dist.normalApproximateProbability(9899); Assert.assertEquals(0.820070051552, result, 1E-10); } /** * Test the degenerate cases of a 0.0 and 1.0 inverse cumulative probability. */ @Test public void testDegenerateInverseCumulativeProbability() { PoissonDistribution dist = new PoissonDistribution(DEFAULT_TEST_POISSON_PARAMETER); Assert.assertEquals(Integer.MAX_VALUE, dist.inverseCumulativeProbability(1.0d)); Assert.assertEquals(0, dist.inverseCumulativeProbability(0d)); } @Test(expected=NotStrictlyPositiveException.class) public void testNegativeMean() { new PoissonDistribution(-1); } @Test public void testMean() { PoissonDistribution dist = new PoissonDistribution(10.0); Assert.assertEquals(10.0, dist.getMean(), 0.0); } @Test public void testLargeMeanCumulativeProbability() { double mean = 1.0; while (mean <= 10000000.0) { PoissonDistribution dist = new PoissonDistribution(mean); double x = mean * 2.0; double dx = x / 10.0; double p = Double.NaN; double sigma = FastMath.sqrt(mean); while (x >= 0) { try { p = dist.cumulativeProbability((int) x); Assert.assertFalse("NaN cumulative probability returned for mean = " + mean + " x = " + x,Double.isNaN(p)); if (x > mean - 2 * sigma) { Assert.assertTrue("Zero cum probaility returned for mean = " + mean + " x = " + x, p > 0); } } catch (Exception ex) { Assert.fail("mean of " + mean + " and x of " + x + " caused " + ex.getMessage()); } x -= dx; } mean *= 10.0; } } /** * JIRA: MATH-282 */ @Test public void testCumulativeProbabilitySpecial() { PoissonDistribution dist; dist = new PoissonDistribution(9120); checkProbability(dist, 9075); checkProbability(dist, 9102); dist = new PoissonDistribution(5058); checkProbability(dist, 5044); dist = new PoissonDistribution(6986); checkProbability(dist, 6950); } private void checkProbability(PoissonDistribution dist, int x) { double p = dist.cumulativeProbability(x); Assert.assertFalse("NaN cumulative probability returned for mean = " + dist.getMean() + " x = " + x, Double.isNaN(p)); Assert.assertTrue("Zero cum probability returned for mean = " + dist.getMean() + " x = " + x, p > 0); } @Test public void testLargeMeanInverseCumulativeProbability() { double mean = 1.0; while (mean <= 100000.0) { // Extended test value: 1E7. Reduced to limit run time. PoissonDistribution dist = new PoissonDistribution(mean); double p = 0.1; double dp = p; while (p < .99) { try { int ret = dist.inverseCumulativeProbability(p); // Verify that returned value satisties definition Assert.assertTrue(p <= dist.cumulativeProbability(ret)); Assert.assertTrue(p > dist.cumulativeProbability(ret - 1)); } catch (Exception ex) { Assert.fail("mean of " + mean + " and p of " + p + " caused " + ex.getMessage()); } p += dp; } mean *= 10.0; } } @Test public void testMoments() { final double tol = 1e-9; PoissonDistribution dist; dist = new PoissonDistribution(1); Assert.assertEquals(dist.getNumericalMean(), 1, tol); Assert.assertEquals(dist.getNumericalVariance(), 1, tol); dist = new PoissonDistribution(11.23); Assert.assertEquals(dist.getNumericalMean(), 11.23, tol); Assert.assertEquals(dist.getNumericalVariance(), 11.23, tol); } } ././@LongLink100644 0 0 176 12126630646 10265 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/distribution/MultivariateNormalMixtureModelDistributionTest.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/distribution/MultivariateNormalMixtureM100644 1750 1750 32722 12126627667 32570 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.distribution; import java.util.List; import java.util.ArrayList; import org.apache.commons.math3.exception.NotPositiveException; import org.apache.commons.math3.exception.MathArithmeticException; import org.apache.commons.math3.util.Pair; import org.junit.Assert; import org.junit.Test; /** * Test that demonstrates the use of {@link MixtureMultivariateRealDistribution} * in order to create a mixture model composed of {@link MultivariateNormalDistribution * normal distributions}. */ public class MultivariateNormalMixtureModelDistributionTest { @Test public void testNonUnitWeightSum() { final double[] weights = { 1, 2 }; final double[][] means = { { -1.5, 2.0 }, { 4.0, 8.2 } }; final double[][][] covariances = { { { 2.0, -1.1 }, { -1.1, 2.0 } }, { { 3.5, 1.5 }, { 1.5, 3.5 } } }; final MultivariateNormalMixtureModelDistribution d = create(weights, means, covariances); final List> comp = d.getComponents(); Assert.assertEquals(1d / 3, comp.get(0).getFirst(), Math.ulp(1d)); Assert.assertEquals(2d / 3, comp.get(1).getFirst(), Math.ulp(1d)); } @Test(expected=MathArithmeticException.class) public void testWeightSumOverFlow() { final double[] weights = { 0.5 * Double.MAX_VALUE, 0.51 * Double.MAX_VALUE }; final double[][] means = { { -1.5, 2.0 }, { 4.0, 8.2 } }; final double[][][] covariances = { { { 2.0, -1.1 }, { -1.1, 2.0 } }, { { 3.5, 1.5 }, { 1.5, 3.5 } } }; final MultivariateNormalMixtureModelDistribution d = create(weights, means, covariances); } @Test(expected=NotPositiveException.class) public void testPreconditionPositiveWeights() { final double[] negativeWeights = { -0.5, 1.5 }; final double[][] means = { { -1.5, 2.0 }, { 4.0, 8.2 } }; final double[][][] covariances = { { { 2.0, -1.1 }, { -1.1, 2.0 } }, { { 3.5, 1.5 }, { 1.5, 3.5 } } }; final MultivariateNormalMixtureModelDistribution d = create(negativeWeights, means, covariances); } /** * Test the accuracy of the density calculation. */ @Test public void testDensities() { final double[] weights = { 0.3, 0.7 }; final double[][] means = { { -1.5, 2.0 }, { 4.0, 8.2 } }; final double[][][] covariances = { { { 2.0, -1.1 }, { -1.1, 2.0 } }, { { 3.5, 1.5 }, { 1.5, 3.5 } } }; final MultivariateNormalMixtureModelDistribution d = create(weights, means, covariances); // Test vectors final double[][] testValues = { { -1.5, 2 }, { 4, 8.2 }, { 1.5, -2 }, { 0, 0 } }; // Densities that we should get back. // Calculated by assigning weights to multivariate normal distribution // and summing // values from dmvnorm function in R 2.15 CRAN package Mixtools v0.4. // Like: .3*dmvnorm(val,mu1,sigma1)+.7*dmvnorm(val,mu2,sigma2) final double[] correctDensities = { 0.02862037278930575, 0.03523044847314091, 0.000416241365629767, 0.009932042831700297 }; for (int i = 0; i < testValues.length; i++) { Assert.assertEquals(correctDensities[i], d.density(testValues[i]), Math.ulp(1d)); } } /** * Test the accuracy of sampling from the distribution. */ @Test public void testSampling() { final double[] weights = { 0.3, 0.7 }; final double[][] means = { { -1.5, 2.0 }, { 4.0, 8.2 } }; final double[][][] covariances = { { { 2.0, -1.1 }, { -1.1, 2.0 } }, { { 3.5, 1.5 }, { 1.5, 3.5 } } }; final MultivariateNormalMixtureModelDistribution d = create(weights, means, covariances); d.reseedRandomGenerator(50); final double[][] correctSamples = getCorrectSamples(); final int n = correctSamples.length; final double[][] samples = d.sample(n); for (int i = 0; i < n; i++) { for (int j = 0; j < samples[i].length; j++) { Assert.assertEquals(correctSamples[i][j], samples[i][j], 1e-16); } } } /** * Creates a mixture of Gaussian distributions. * * @param weights Weights. * @param means Means. * @param covariances Covariances. * @return the mixture distribution. */ private MultivariateNormalMixtureModelDistribution create(double[] weights, double[][] means, double[][][] covariances) { final List> mvns = new ArrayList>(); for (int i = 0; i < weights.length; i++) { final MultivariateNormalDistribution dist = new MultivariateNormalDistribution(means[i], covariances[i]); mvns.add(new Pair(weights[i], dist)); } return new MultivariateNormalMixtureModelDistribution(mvns); } /** * Values used in {@link #testSampling()}. */ private double[][] getCorrectSamples() { // These were sampled from the MultivariateNormalMixtureModelDistribution class // with seed 50. // // They were then fit to a MVN mixture model in R using mixtools. // // The optimal parameters were: // - component weights: {0.3595186, 0.6404814} // - mean vectors: {-1.645879, 1.989797}, {3.474328, 7.782232} // - covariance matrices: // { 1.397738 -1.167732 // -1.167732 1.801782 } // and // { 3.934593 2.354787 // 2.354787 4.428024 } // // It is considered fairly close to the actual test parameters, // considering that the sample size is only 100. return new double[][] { { 6.259990922080121, 11.972954175355897 }, { -2.5296544304801847, 1.0031292519854365 }, { 0.49037886081440396, 0.9758251727325711 }, { 5.022970993312015, 9.289348879616787 }, { -1.686183146603914, 2.007244382745706 }, { -1.4729253946002685, 2.762166644212484 }, { 4.329788143963888, 11.514016497132253 }, { 3.008674596114442, 4.960246550446107 }, { 3.342379304090846, 5.937630105198625 }, { 2.6993068328674754, 7.42190871572571 }, { -2.446569340219571, 1.9687117791378763 }, { 1.922417883170056, 4.917616702617099 }, { -1.1969741543898518, 2.4576126277884387 }, { 2.4216948702967196, 8.227710158117134 }, { 6.701424725804463, 9.098666475042428 }, { 2.9890253545698964, 9.643807939324331 }, { 0.7162632354907799, 8.978811120287553 }, { -2.7548699149775877, 4.1354812280794215 }, { 8.304528180745018, 11.602319388898287 }, { -2.7633253389165926, 2.786173883989795 }, { 1.3322228389460813, 5.447481218602913 }, { -1.8120096092851508, 1.605624499560037 }, { 3.6546253437206504, 8.195304526564376 }, { -2.312349539658588, 1.868941220444169 }, { -1.882322136356522, 2.033795570464242 }, { 4.562770714939441, 7.414967958885031 }, { 4.731882017875329, 8.890676665580747 }, { 3.492186010427425, 8.9005225241848 }, { -1.619700190174894, 3.314060142479045 }, { 3.5466090064003315, 7.75182101001913 }, { 5.455682472787392, 8.143119287755635 }, { -2.3859602945473197, 1.8826732217294837 }, { 3.9095306088680015, 9.258129209626317 }, { 7.443020189508173, 7.837840713329312 }, { 2.136004873917428, 6.917636475958297 }, { -1.7203379410395119, 2.3212878757611524 }, { 4.618991257611526, 12.095065976419436 }, { -0.4837044029854387, 0.8255970441255125 }, { -4.438938966557163, 4.948666297280241 }, { -0.4539625134045906, 4.700922454655341 }, { 2.1285488271265356, 8.457941480487563 }, { 3.4873561871454393, 11.99809827845933 }, { 4.723049431412658, 7.813095742563365 }, { 1.1245583037967455, 5.20587873556688 }, { 1.3411933634409197, 6.069796875785409 }, { 4.585119332463686, 7.967669543767418 }, { 1.3076522817963823, -0.647431033653445 }, { -1.4449446442803178, 1.9400424267464862 }, { -2.069794456383682, 3.5824162107496544 }, { -0.15959481421417276, 1.5466782303315405 }, { -2.0823081278810136, 3.0914366458581437 }, { 3.521944615248141, 10.276112932926408 }, { 1.0164326704884257, 4.342329556442856 }, { 5.3718868590295275, 8.374761158360922 }, { 0.3673656866959396, 8.75168581694866 }, { -2.250268955954753, 1.4610850300996527 }, { -2.312739727403522, 1.5921126297576362 }, { 3.138993360831055, 6.7338392374947365 }, { 2.6978650950790115, 7.941857288979095 }, { 4.387985088655384, 8.253499976968 }, { -1.8928961721456705, 0.23631082388724223 }, { 4.43509029544109, 8.565290285488782 }, { 4.904728034106502, 5.79936660133754 }, { -1.7640371853739507, 2.7343727594167433 }, { 2.4553674733053463, 7.875871017408807 }, { -2.6478965122565006, 4.465127753193949 }, { 3.493873671142299, 10.443093773532448 }, { 1.1321916197409103, 7.127108479263268 }, { -1.7335075535240392, 2.550629648463023 }, { -0.9772679734368084, 4.377196298969238 }, { 3.6388366973980357, 6.947299283206256 }, { 0.27043799318823325, 6.587978599614367 }, { 5.356782352010253, 7.388957912116327 }, { -0.09187745751354681, 0.23612399246659743 }, { 2.903203580353435, 3.8076727621794415 }, { 5.297014824937293, 8.650985262326508 }, { 4.934508602170976, 9.164571423190052 }, { -1.0004911869654256, 4.797064194444461 }, { 6.782491700298046, 11.852373338280497 }, { 2.8983678524536014, 8.303837362117521 }, { 4.805003269830865, 6.790462904325329 }, { -0.8815799740744226, 1.3015810062131394 }, { 5.115138859802104, 6.376895810201089 }, { 4.301239328205988, 8.60546337560793 }, { 3.276423626317666, 9.889429652591947 }, { -4.001924973153122, 4.3353864592328515 }, { 3.9571892554119517, 4.500569057308562 }, { 4.783067027436208, 7.451125480601317 }, { 4.79065438272821, 9.614122776979698 }, { 2.677655270279617, 6.8875223698210135 }, { -1.3714746289327362, 2.3992153193382437 }, { 3.240136859745249, 7.748339397522042 }, { 5.107885374416291, 8.508324480583724 }, { -1.5830830226666048, 0.9139127045208315 }, { -1.1596156791652918, -0.04502759384531929 }, { -0.4670021307952068, 3.6193633227841624 }, { -0.7026065228267798, 0.4811423031997131 }, { -2.719979836732917, 2.5165041618080104 }, { 1.0336754331123372, -0.34966029029320644 }, { 4.743217291882213, 5.750060115251131 } }; } } /** * Class that implements a mixture of Gaussian ditributions. */ class MultivariateNormalMixtureModelDistribution extends MixtureMultivariateRealDistribution { public MultivariateNormalMixtureModelDistribution(List> components) { super(components); } } ././@LongLink100644 0 0 146 12126630646 10262 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/distribution/CauchyDistributionTest.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/distribution/CauchyDistributionTest.jav100644 1750 1750 10745 12126627667 32512 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.distribution; import org.apache.commons.math3.exception.NotStrictlyPositiveException; import org.junit.Assert; import org.junit.Test; /** * Test cases for CauchyDistribution. * Extends ContinuousDistributionAbstractTest. See class javadoc for * ContinuousDistributionAbstractTest for details. * * @version $Id: CauchyDistributionTest.java 1364028 2012-07-21 00:42:49Z erans $ */ public class CauchyDistributionTest extends RealDistributionAbstractTest { // --------------------- Override tolerance -------------- protected double defaultTolerance = NormalDistribution.DEFAULT_INVERSE_ABSOLUTE_ACCURACY; @Override public void setUp() { super.setUp(); setTolerance(defaultTolerance); } //-------------- Implementations for abstract methods ----------------------- /** Creates the default continuous distribution instance to use in tests. */ @Override public CauchyDistribution makeDistribution() { return new CauchyDistribution(1.2, 2.1); } /** Creates the default cumulative probability distribution test input values */ @Override public double[] makeCumulativeTestPoints() { // quantiles computed using R 2.9.2 return new double[] {-667.24856187, -65.6230835029, -25.4830299460, -12.0588781808, -5.26313542807, 669.64856187, 68.0230835029, 27.8830299460, 14.4588781808, 7.66313542807}; } /** Creates the default cumulative probability density test expected values */ @Override public double[] makeCumulativeTestValues() { return new double[] {0.001, 0.01, 0.025, 0.05, 0.1, 0.999, 0.990, 0.975, 0.950, 0.900}; } /** Creates the default probability density test expected values */ @Override public double[] makeDensityTestValues() { return new double[] {1.49599158008e-06, 0.000149550440335, 0.000933076881878, 0.00370933207799, 0.0144742330437, 1.49599158008e-06, 0.000149550440335, 0.000933076881878, 0.00370933207799, 0.0144742330437}; } //---------------------------- Additional test cases ------------------------- @Test public void testInverseCumulativeProbabilityExtremes() { setInverseCumulativeTestPoints(new double[] {0.0, 1.0}); setInverseCumulativeTestValues( new double[] {Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY}); verifyInverseCumulativeProbabilities(); } @Test public void testMedian() { CauchyDistribution distribution = (CauchyDistribution) getDistribution(); Assert.assertEquals(1.2, distribution.getMedian(), 0.0); } @Test public void testScale() { CauchyDistribution distribution = (CauchyDistribution) getDistribution(); Assert.assertEquals(2.1, distribution.getScale(), 0.0); } @Test public void testPreconditions() { try { new CauchyDistribution(0, 0); Assert.fail("Cannot have zero scale"); } catch (NotStrictlyPositiveException ex) { // Expected. } try { new CauchyDistribution(0, -1); Assert.fail("Cannot have negative scale"); } catch (NotStrictlyPositiveException ex) { // Expected. } } @Test public void testMoments() { CauchyDistribution dist; dist = new CauchyDistribution(10.2, 0.15); Assert.assertTrue(Double.isNaN(dist.getNumericalMean())); Assert.assertTrue(Double.isNaN(dist.getNumericalVariance())); dist = new CauchyDistribution(23.12, 2.12); Assert.assertTrue(Double.isNaN(dist.getNumericalMean())); Assert.assertTrue(Double.isNaN(dist.getNumericalVariance())); } } commons-math3-3.2-src/src/test/java/org/apache/commons/math3/distribution/TDistributionTest.java100644 1750 1750 15004 12126627667 31633 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.distribution; import org.apache.commons.math3.exception.NotStrictlyPositiveException; import org.junit.Assert; import org.junit.Test; import org.apache.commons.math3.TestUtils; /** * Test cases for TDistribution. * Extends ContinuousDistributionAbstractTest. See class javadoc for * ContinuousDistributionAbstractTest for details. * * @version $Id: TDistributionTest.java 1364028 2012-07-21 00:42:49Z erans $ */ public class TDistributionTest extends RealDistributionAbstractTest { //-------------- Implementations for abstract methods ----------------------- /** Creates the default continuous distribution instance to use in tests. */ @Override public TDistribution makeDistribution() { return new TDistribution(5.0); } /** Creates the default cumulative probability distribution test input values */ @Override public double[] makeCumulativeTestPoints() { // quantiles computed using R version 2.9.2 return new double[] {-5.89342953136, -3.36492999891, -2.57058183564, -2.01504837333, -1.47588404882, 5.89342953136, 3.36492999891, 2.57058183564, 2.01504837333, 1.47588404882}; } /** Creates the default cumulative probability density test expected values */ @Override public double[] makeCumulativeTestValues() { return new double[] {0.001, 0.01, 0.025, 0.05, 0.1, 0.999, 0.990, 0.975, 0.950, 0.900}; } /** Creates the default probability density test expected values */ @Override public double[] makeDensityTestValues() { return new double[] {0.000756494565517, 0.0109109752919, 0.0303377878006, 0.0637967988952, 0.128289492005, 0.000756494565517, 0.0109109752919, 0.0303377878006, 0.0637967988952, 0.128289492005}; } // --------------------- Override tolerance -------------- @Override public void setUp() { super.setUp(); setTolerance(1E-9); } //---------------------------- Additional test cases ------------------------- /** * @see * Bug report that prompted this unit test. */ @Test public void testCumulativeProbabilityAgainstStackOverflow() { TDistribution td = new TDistribution(5.); td.cumulativeProbability(.1); td.cumulativeProbability(.01); } @Test public void testSmallDf() { setDistribution(new TDistribution(1d)); // quantiles computed using R version 2.9.2 setCumulativeTestPoints(new double[] {-318.308838986, -31.8205159538, -12.7062047362, -6.31375151468, -3.07768353718, 318.308838986, 31.8205159538, 12.7062047362, 6.31375151468, 3.07768353718}); setDensityTestValues(new double[] {3.14158231817e-06, 0.000314055924703, 0.00195946145194, 0.00778959736375, 0.0303958893917, 3.14158231817e-06, 0.000314055924703, 0.00195946145194, 0.00778959736375, 0.0303958893917}); setInverseCumulativeTestValues(getCumulativeTestPoints()); verifyCumulativeProbabilities(); verifyInverseCumulativeProbabilities(); verifyDensities(); } @Test public void testInverseCumulativeProbabilityExtremes() { setInverseCumulativeTestPoints(new double[] {0, 1}); setInverseCumulativeTestValues( new double[] {Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY}); verifyInverseCumulativeProbabilities(); } @Test public void testDfAccessors() { TDistribution dist = (TDistribution) getDistribution(); Assert.assertEquals(5d, dist.getDegreesOfFreedom(), Double.MIN_VALUE); } @Test(expected=NotStrictlyPositiveException.class) public void testPreconditions() { new TDistribution(0); } @Test public void testMoments() { final double tol = 1e-9; TDistribution dist; dist = new TDistribution(1); Assert.assertTrue(Double.isNaN(dist.getNumericalMean())); Assert.assertTrue(Double.isNaN(dist.getNumericalVariance())); dist = new TDistribution(1.5); Assert.assertEquals(dist.getNumericalMean(), 0, tol); Assert.assertTrue(Double.isInfinite(dist.getNumericalVariance())); dist = new TDistribution(5); Assert.assertEquals(dist.getNumericalMean(), 0, tol); Assert.assertEquals(dist.getNumericalVariance(), 5d / (5d - 2d), tol); } /* * Adding this test to benchmark against tables published by NIST * http://itl.nist.gov/div898/handbook/eda/section3/eda3672.htm * Have chosen tabulated results for degrees of freedom 2,10,30,100 * Have chosen problevels from 0.10 to 0.001 */ @Test public void nistData(){ double[] prob = new double[]{ 0.10,0.05,0.025,0.01,0.005,0.001}; double[] args2 = new double[]{1.886,2.920,4.303,6.965,9.925,22.327}; double[] args10 = new double[]{1.372,1.812,2.228,2.764,3.169,4.143}; double[] args30 = new double[]{1.310,1.697,2.042,2.457,2.750,3.385}; double[] args100= new double[]{1.290,1.660,1.984,2.364,2.626,3.174}; TestUtils.assertEquals(prob, makeNistResults(args2, 2), 1.0e-4); TestUtils.assertEquals(prob, makeNistResults(args10, 10), 1.0e-4); TestUtils.assertEquals(prob, makeNistResults(args30, 30), 1.0e-4); TestUtils.assertEquals(prob, makeNistResults(args100, 100), 1.0e-4); return; } private double[] makeNistResults(double[] args, int df){ TDistribution td = new TDistribution(df); double[] res = new double[ args.length ]; for( int i = 0 ; i < res.length ; i++){ res[i] = 1.0 - td.cumulativeProbability(args[i]); } return res; } } ././@LongLink100644 0 0 152 12126630646 10257 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/exception/NumberIsTooLargeExceptionTest.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/exception/NumberIsTooLargeExceptionTest100644 1750 1750 2424 12126627672 32405 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.exception; import org.junit.Assert; import org.junit.Test; /** * Test for {@link NumberIsTooLargeException}. * * @version $Id$ */ public class NumberIsTooLargeExceptionTest { @Test public void testAccessors() { final NumberIsTooLargeException e = new NumberIsTooLargeException(1, 0, true); Assert.assertEquals(1, e.getArgument()); Assert.assertEquals(0, e.getMax()); Assert.assertTrue(e.getBoundIsAllowed()); } } ././@LongLink100644 0 0 153 12126630646 10260 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/exception/DimensionMismatchExceptionTest.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/exception/DimensionMismatchExceptionTes100644 1750 1750 2346 12126627672 32456 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.exception; import org.junit.Assert; import org.junit.Test; /** * Test for {@link DimensionMismatchException}. * * @version $Id$ */ public class DimensionMismatchExceptionTest { @Test public void testAccessors() { final DimensionMismatchException e = new DimensionMismatchException(1, 2); Assert.assertEquals(1, e.getArgument()); Assert.assertEquals(2, e.getDimension()); } } ././@LongLink100644 0 0 156 12126630646 10263 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/exception/NonMonotonicSequenceExceptionTest.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/exception/NonMonotonicSequenceException100644 1750 1750 3523 12126627672 32476 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.exception; import org.apache.commons.math3.util.MathArrays; import org.junit.Assert; import org.junit.Test; /** * Test for {@link NonMonotonicSequenceException}. * * @version $Id$ */ public class NonMonotonicSequenceExceptionTest { @Test public void testAccessors() { NonMonotonicSequenceException e = new NonMonotonicSequenceException(0, -1, 1, MathArrays.OrderDirection.DECREASING, false); Assert.assertEquals(0, e.getArgument()); Assert.assertEquals(-1, e.getPrevious()); Assert.assertEquals(1, e.getIndex()); Assert.assertTrue(e.getDirection() == MathArrays.OrderDirection.DECREASING); Assert.assertFalse(e.getStrict()); e = new NonMonotonicSequenceException(-1, 0, 1); Assert.assertEquals(-1, e.getArgument()); Assert.assertEquals(0, e.getPrevious()); Assert.assertEquals(1, e.getIndex()); Assert.assertTrue(e.getDirection() == MathArrays.OrderDirection.INCREASING); Assert.assertTrue(e.getStrict()); } } ././@LongLink100644 0 0 152 12126630646 10257 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/exception/NumberIsTooSmallExceptionTest.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/exception/NumberIsTooSmallExceptionTest100644 1750 1750 2426 12126627672 32425 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.exception; import org.junit.Assert; import org.junit.Test; /** * Test for {@link NumberIsTooSmallException}. * * @version $Id$ */ public class NumberIsTooSmallExceptionTest { @Test public void testAccessors() { final NumberIsTooSmallException e = new NumberIsTooSmallException(0, 0, false); Assert.assertEquals(0, e.getArgument()); Assert.assertEquals(0, e.getMin()); Assert.assertFalse(e.getBoundIsAllowed()); } } ././@LongLink100644 0 0 145 12126630646 10261 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/exception/NotPositiveExceptionTest.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/exception/NotPositiveExceptionTest.java100644 1750 1750 2371 12126627672 32430 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.exception; import org.junit.Assert; import org.junit.Test; /** * Test for {@link NotPositiveException}. * * @version $Id$ */ public class NotPositiveExceptionTest { @Test public void testAccessors() { final NotPositiveException e = new NotPositiveException(-1); Assert.assertEquals(-1, e.getArgument()); Assert.assertEquals(0, e.getMin()); Assert.assertTrue(e.getBoundIsAllowed()); } } ././@LongLink100644 0 0 155 12126630646 10262 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/exception/NotStrictlyPositiveExceptionTest.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/exception/NotStrictlyPositiveExceptionT100644 1750 1750 2430 12126627672 32526 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.exception; import org.junit.Assert; import org.junit.Test; /** * Test for {@link NotStrictlyPositiveException}. * * @version $Id$ */ public class NotStrictlyPositiveExceptionTest { @Test public void testAccessors() { final NotStrictlyPositiveException e = new NotStrictlyPositiveException(0); Assert.assertEquals(0, e.getArgument()); Assert.assertEquals(0, e.getMin()); Assert.assertFalse(e.getBoundIsAllowed()); } } commons-math3-3.2-src/src/test/java/org/apache/commons/math3/exception/OutOfRangeExceptionTest.java100644 1750 1750 2363 12126627672 32157 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.exception; import org.junit.Assert; import org.junit.Test; /** * Test for {@link OutOfRangeException}. * * @version $Id$ */ public class OutOfRangeExceptionTest { @Test public void testAccessors() { final OutOfRangeException e = new OutOfRangeException(-1, 0, 2); Assert.assertEquals(-1, e.getArgument()); Assert.assertEquals(0, e.getLo()); Assert.assertEquals(2, e.getHi()); } } ././@LongLink100644 0 0 154 12126630646 10261 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/exception/TooManyEvaluationsExceptionTest.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/exception/TooManyEvaluationsExceptionTe100644 1750 1750 2674 12126627672 32465 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.exception; import java.text.MessageFormat; import org.junit.Assert; import org.junit.Test; /** * Test for {@link TooManyEvaluationsException}. * * @version $Id$ */ public class TooManyEvaluationsExceptionTest { @Test public void testMessage() { final int max = 12345; final TooManyEvaluationsException e = new TooManyEvaluationsException(max); final String msg = e.getLocalizedMessage(); Assert.assertTrue(msg, msg.matches(".*?" + MessageFormat.format("{0}", max) + ".*")); } } ././@LongLink100644 0 0 146 12126630646 10262 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/exception/util/LocalizedFormatsTest.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/exception/util/LocalizedFormatsTest.jav100644 1750 1750 10361 12126627672 32362 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.exception.util; import java.text.MessageFormat; import java.util.Enumeration; import java.util.Locale; import java.util.ResourceBundle; import org.junit.Assert; import org.junit.Test; public class LocalizedFormatsTest { @Test public void testMessageNumber() { Assert.assertEquals(313, LocalizedFormats.values().length); } @Test public void testAllKeysPresentInPropertiesFiles() { final String path = LocalizedFormats.class.getName().replaceAll("\\.", "/"); for (final String language : new String[] { "fr" } ) { ResourceBundle bundle = ResourceBundle.getBundle("assets/" + path, new Locale(language)); for (LocalizedFormats message : LocalizedFormats.values()) { final String messageKey = message.toString(); boolean keyPresent = false; for (final Enumeration keys = bundle.getKeys(); keys.hasMoreElements();) { keyPresent |= messageKey.equals(keys.nextElement()); } Assert.assertTrue("missing key \"" + message.name() + "\" for language " + language, keyPresent); } Assert.assertEquals(language, bundle.getLocale().getLanguage()); } } @Test public void testAllPropertiesCorrespondToKeys() { final String path = LocalizedFormats.class.getName().replaceAll("\\.", "/"); for (final String language : new String[] { "fr" } ) { ResourceBundle bundle = ResourceBundle.getBundle("assets/" + path, new Locale(language)); for (final Enumeration keys = bundle.getKeys(); keys.hasMoreElements();) { final String propertyKey = keys.nextElement(); try { Assert.assertNotNull(LocalizedFormats.valueOf(propertyKey)); } catch (IllegalArgumentException iae) { Assert.fail("unknown key \"" + propertyKey + "\" in language " + language); } } Assert.assertEquals(language, bundle.getLocale().getLanguage()); } } @Test public void testNoMissingFrenchTranslation() { for (LocalizedFormats message : LocalizedFormats.values()) { String translated = message.getLocalizedString(Locale.FRENCH); Assert.assertFalse(message.name(), translated.toLowerCase().contains("missing translation")); } } @Test public void testNoOpEnglishTranslation() { for (LocalizedFormats message : LocalizedFormats.values()) { String translated = message.getLocalizedString(Locale.ENGLISH); Assert.assertEquals(message.getSourceString(), translated); } } @Test public void testVariablePartsConsistency() { for (final String language : new String[] { "fr" } ) { Locale locale = new Locale(language); for (LocalizedFormats message : LocalizedFormats.values()) { MessageFormat source = new MessageFormat(message.getSourceString()); MessageFormat translated = new MessageFormat(message.getLocalizedString(locale)); Assert.assertEquals(message.name() + " (" + language + ")", source.getFormatsByArgumentIndex().length, translated.getFormatsByArgumentIndex().length); } } } } commons-math3-3.2-src/src/test/java/org/apache/commons/math3/exception/util/ArgUtilsTest.java100644 1750 1750 4257 12126627672 31002 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.exception.util; import java.util.List; import java.util.ArrayList; import org.junit.Assert; import org.junit.Test; /** * Test for {@link ArgUtils}. * * @version $Id$ */ public class ArgUtilsTest { @Test public void testFlatten() { final List orig = new ArrayList(); final Object[] struct = new Object[] { new Object[] { new Object[] { create(orig), create(orig), }, create(orig), new Object[] { create(orig), } }, create(orig), new Object[] { create(orig), new Object[] { create(orig), create(orig), } }, create(orig), }; Object[] flat = ArgUtils.flatten(struct); Assert.assertEquals(flat.length, orig.size()); for (int i = 0, max = orig.size(); i < max; i++) { Assert.assertEquals(orig.get(i), flat[i]); } } /** * Create and store an {@code Object}. * * @param list List to store to. * @return the stored object. */ private Object create(List list) { final Object o = new Object(); list.add(o); return o; } } ././@LongLink100644 0 0 146 12126630646 10262 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/exception/util/ExceptionContextTest.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/exception/util/ExceptionContextTest.jav100644 1750 1750 11357 12126627672 32431 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.exception.util; import java.util.Locale; import java.util.Arrays; import java.io.IOException; import java.io.ObjectOutputStream; import java.io.ObjectInputStream; import java.io.ByteArrayOutputStream; import java.io.ByteArrayInputStream; import org.junit.Assert; import org.junit.Test; /** * Test for {@link ExceptionContext}. * * @version $Id$ */ public class ExceptionContextTest { @Test public void testMessageChain() { final ExceptionContext c = new ExceptionContext(new Exception("oops")); final String sep = " | "; // Non-default separator. final String m1 = "column index (0)"; c.addMessage(LocalizedFormats.COLUMN_INDEX, 0); final String m2 = "got 1x2 but expected 3x4"; c.addMessage(LocalizedFormats.DIMENSIONS_MISMATCH_2x2, 1, 2, 3, 4); final String m3 = "It didn't work out"; c.addMessage(LocalizedFormats.SIMPLE_MESSAGE, m3); Assert.assertEquals(c.getMessage(Locale.US, sep), m1 + sep + m2 + sep + m3); } @Test public void testNoArgAddMessage() { final ExceptionContext c = new ExceptionContext(new Exception("hello")); c.addMessage(LocalizedFormats.SIMPLE_MESSAGE); Assert.assertEquals(c.getMessage(), "{0}"); } @Test public void testContext() { final ExceptionContext c = new ExceptionContext(new Exception("bye")); final String[] keys = {"Key 1", "Key 2"}; final Object[] values = {"Value 1", Integer.valueOf(2)}; for (int i = 0; i < keys.length; i++) { c.setValue(keys[i], values[i]); } // Check that all keys are present. Assert.assertTrue(c.getKeys().containsAll(Arrays.asList(keys))); // Check that all values are correctly stored. for (int i = 0; i < keys.length; i++) { Assert.assertEquals(values[i], c.getValue(keys[i])); } // Check behaviour on missing key. Assert.assertNull(c.getValue("xyz")); } @Test public void testSerialize() throws IOException, ClassNotFoundException { final ExceptionContext cOut = new ExceptionContext(new Exception("Apache")); cOut.addMessage(LocalizedFormats.COLUMN_INDEX, 0); cOut.setValue("Key 1", Integer.valueOf(0)); ByteArrayOutputStream bos = new ByteArrayOutputStream(); ObjectOutputStream oos = new ObjectOutputStream(bos); oos.writeObject(cOut); ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray()); ObjectInputStream ois = new ObjectInputStream(bis); ExceptionContext cIn = (ExceptionContext) ois.readObject(); Assert.assertTrue(cOut.getMessage().equals(cIn.getMessage())); for (String key : cIn.getKeys()) { Assert.assertTrue(cOut.getValue(key).equals(cIn.getValue(key))); } } @Test public void testSerializeUnserializable() throws Exception { final ExceptionContext cOut = new ExceptionContext(new Exception("Apache Commons Math")); cOut.addMessage(LocalizedFormats.SIMPLE_MESSAGE, "OK"); cOut.addMessage(LocalizedFormats.SIMPLE_MESSAGE, new Unserializable()); String key = "Key 1"; cOut.setValue(key, new Unserializable()); { ByteArrayOutputStream bos = new ByteArrayOutputStream(); ObjectOutputStream oos = new ObjectOutputStream(bos); oos.writeObject(cOut); ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray()); ObjectInputStream ois = new ObjectInputStream(bis); ExceptionContext cIn = (ExceptionContext) ois.readObject(); String nsObjStr = (String) cIn.getValue(key); Assert.assertTrue(nsObjStr.matches(".*could not be serialized.*")); } } /** * Class used by {@link #testSerializeUnserializable()}. */ private static class Unserializable { Unserializable() {} } } ././@LongLink100644 0 0 152 12126630646 10257 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/exception/MaxCountExceededExceptionTest.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/exception/MaxCountExceededExceptionTest100644 1750 1750 2252 12126627672 32410 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.exception; import org.junit.Assert; import org.junit.Test; /** * Test for {@link MaxCountExceededException}. * * @version $Id$ */ public class MaxCountExceededExceptionTest { @Test public void testAccessors() { final MaxCountExceededException e = new MaxCountExceededException(10); Assert.assertEquals(10, e.getMax()); } } commons-math3-3.2-src/src/test/java/org/apache/commons/math3/Retry.java100644 1750 1750 2042 12126627675 24533 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3; import java.lang.annotation.*; /** * Annotation that enables test retries. * @version $Id$ */ @Retention(RetentionPolicy.RUNTIME) @Target({ElementType.METHOD}) public @interface Retry { int value() default 2; } commons-math3-3.2-src/src/test/java/org/apache/commons/math3/util/PairTest.java100644 1750 1750 6360 12126627675 26145 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with this * work for additional information regarding copyright ownership. The ASF * licenses this file to You 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. */ package org.apache.commons.math3.util; import org.junit.Assert; import org.junit.Test; /** * Test for {@link Pair}. */ public class PairTest { @Test public void testAccessor() { final Pair p = new Pair(new Integer(1), new Double(2)); Assert.assertEquals(new Integer(1), p.getKey()); Assert.assertEquals(new Double(2), p.getValue(), Math.ulp(1d)); } @Test public void testAccessor2() { final Pair p = new Pair(new Integer(1), new Double(2)); // Check that both APIs refer to the same data. Assert.assertTrue(p.getFirst() == p.getKey()); Assert.assertTrue(p.getSecond() == p.getValue()); } @Test public void testEquals() { Pair p1 = new Pair(null, null); Assert.assertFalse(p1.equals(null)); Pair p2 = new Pair(null, null); Assert.assertTrue(p1.equals(p2)); p1 = new Pair(new Integer(1), new Double(2)); Assert.assertFalse(p1.equals(p2)); p2 = new Pair(new Integer(1), new Double(2)); Assert.assertTrue(p1.equals(p2)); Pair p3 = new Pair(new Integer(1), new Float(2)); Assert.assertFalse(p1.equals(p3)); } @Test public void testHashCode() { final MyInteger m1 = new MyInteger(1); final MyInteger m2 = new MyInteger(1); final Pair p1 = new Pair(m1, m1); final Pair p2 = new Pair(m2, m2); // Same contents, same hash code. Assert.assertTrue(p1.hashCode() == p2.hashCode()); // Different contents, different hash codes. m2.set(2); Assert.assertFalse(p1.hashCode() == p2.hashCode()); } /** * A mutable integer. */ private static class MyInteger { private int i; public MyInteger(int i) { this.i = i; } public void set(int i) { this.i = i; } @Override public boolean equals(Object o) { if (!(o instanceof MyInteger)) { return false; } else { return i == ((MyInteger) o).i; } } @Override public int hashCode() { return i; } } } commons-math3-3.2-src/src/test/java/org/apache/commons/math3/util/ContinuedFractionTest.java100644 1750 1750 2717 12126627675 30672 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.util; import org.junit.Assert; import org.junit.Test; /** * @version $Id: ContinuedFractionTest.java 1244107 2012-02-14 16:17:55Z erans $ */ public class ContinuedFractionTest { @Test public void testGoldenRatio() throws Exception { ContinuedFraction cf = new ContinuedFraction() { @Override public double getA(int n, double x) { return 1.0; } @Override public double getB(int n, double x) { return 1.0; } }; double gr = cf.evaluate(0.0, 10e-9); Assert.assertEquals(1.61803399, gr, 10e-9); } } commons-math3-3.2-src/src/test/java/org/apache/commons/math3/util/MathUtilsTest.java100644 1750 1750 31021 12126627675 27174 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with this * work for additional information regarding copyright ownership. The ASF * licenses this file to You 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. */ package org.apache.commons.math3.util; import org.apache.commons.math3.distribution.RealDistribution; import org.apache.commons.math3.distribution.UniformRealDistribution; import org.apache.commons.math3.exception.MathArithmeticException; import org.apache.commons.math3.exception.NotFiniteNumberException; import org.apache.commons.math3.exception.NullArgumentException; import org.apache.commons.math3.exception.util.LocalizedFormats; import org.apache.commons.math3.random.RandomDataImpl; import org.junit.Assert; import org.junit.Test; /** * Test cases for the MathUtils class. * @version $Id: MathUtilsTest.java 1449529 2013-02-24 19:13:17Z luc $ * 2007) $ */ public final class MathUtilsTest { @Test public void testHash() { double[] testArray = { Double.NaN, Double.POSITIVE_INFINITY, Double.NEGATIVE_INFINITY, 1d, 0d, 1E-14, (1 + 1E-14), Double.MIN_VALUE, Double.MAX_VALUE }; for (int i = 0; i < testArray.length; i++) { for (int j = 0; j < testArray.length; j++) { if (i == j) { Assert.assertEquals(MathUtils.hash(testArray[i]), MathUtils.hash(testArray[j])); Assert.assertEquals(MathUtils.hash(testArray[j]), MathUtils.hash(testArray[i])); } else { Assert.assertTrue(MathUtils.hash(testArray[i]) != MathUtils.hash(testArray[j])); Assert.assertTrue(MathUtils.hash(testArray[j]) != MathUtils.hash(testArray[i])); } } } } @Test public void testArrayHash() { Assert.assertEquals(0, MathUtils.hash((double[]) null)); Assert.assertEquals(MathUtils.hash(new double[] { Double.NaN, Double.POSITIVE_INFINITY, Double.NEGATIVE_INFINITY, 1d, 0d }), MathUtils.hash(new double[] { Double.NaN, Double.POSITIVE_INFINITY, Double.NEGATIVE_INFINITY, 1d, 0d })); Assert.assertFalse(MathUtils.hash(new double[] { 1d }) == MathUtils.hash(new double[] { FastMath.nextAfter(1d, 2d) })); Assert.assertFalse(MathUtils.hash(new double[] { 1d }) == MathUtils.hash(new double[] { 1d, 1d })); } /** * Make sure that permuted arrays do not hash to the same value. */ @Test public void testPermutedArrayHash() { double[] original = new double[10]; double[] permuted = new double[10]; RandomDataImpl random = new RandomDataImpl(); // Generate 10 distinct random values for (int i = 0; i < 10; i++) { final RealDistribution u = new UniformRealDistribution(i + 0.5, i + 0.75); original[i] = u.sample(); } // Generate a random permutation, making sure it is not the identity boolean isIdentity = true; do { int[] permutation = random.nextPermutation(10, 10); for (int i = 0; i < 10; i++) { if (i != permutation[i]) { isIdentity = false; } permuted[i] = original[permutation[i]]; } } while (isIdentity); // Verify that permuted array has different hash Assert.assertFalse(MathUtils.hash(original) == MathUtils.hash(permuted)); } @Test public void testIndicatorByte() { Assert.assertEquals((byte)1, MathUtils.copySign((byte)1, (byte)2)); Assert.assertEquals((byte)1, MathUtils.copySign((byte)1, (byte)0)); Assert.assertEquals((byte)(-1), MathUtils.copySign((byte)1, (byte)(-2))); } @Test public void testIndicatorInt() { Assert.assertEquals(1, MathUtils.copySign(1, 2)); Assert.assertEquals(1, MathUtils.copySign(1, 0)); Assert.assertEquals((-1), MathUtils.copySign(1, -2)); } @Test public void testIndicatorLong() { Assert.assertEquals(1L, MathUtils.copySign(1L, 2L)); Assert.assertEquals(1L, MathUtils.copySign(1L, 0L)); Assert.assertEquals(-1L, MathUtils.copySign(1L, -2L)); } @Test public void testIndicatorShort() { Assert.assertEquals((short)1, MathUtils.copySign((short)1, (short)2)); Assert.assertEquals((short)1, MathUtils.copySign((short)1, (short)0)); Assert.assertEquals((short)(-1), MathUtils.copySign((short)1, (short)(-2))); } @Test public void testNormalizeAngle() { for (double a = -15.0; a <= 15.0; a += 0.1) { for (double b = -15.0; b <= 15.0; b += 0.2) { double c = MathUtils.normalizeAngle(a, b); Assert.assertTrue((b - FastMath.PI) <= c); Assert.assertTrue(c <= (b + FastMath.PI)); double twoK = FastMath.rint((a - c) / FastMath.PI); Assert.assertEquals(c, a - twoK * FastMath.PI, 1.0e-14); } } } @Test public void testReduce() { final double period = -12.222; final double offset = 13; final double delta = 1.5; double orig = offset + 122456789 * period + delta; double expected = delta; Assert.assertEquals(expected, MathUtils.reduce(orig, period, offset), 1e-7); Assert.assertEquals(expected, MathUtils.reduce(orig, -period, offset), 1e-7); orig = offset - 123356789 * period - delta; expected = Math.abs(period) - delta; Assert.assertEquals(expected, MathUtils.reduce(orig, period, offset), 1e-6); Assert.assertEquals(expected, MathUtils.reduce(orig, -period, offset), 1e-6); orig = offset - 123446789 * period + delta; expected = delta; Assert.assertEquals(expected, MathUtils.reduce(orig, period, offset), 1e-6); Assert.assertEquals(expected, MathUtils.reduce(orig, -period, offset), 1e-6); Assert.assertTrue(Double.isNaN(MathUtils.reduce(orig, Double.NaN, offset))); Assert.assertTrue(Double.isNaN(MathUtils.reduce(Double.NaN, period, offset))); Assert.assertTrue(Double.isNaN(MathUtils.reduce(orig, period, Double.NaN))); Assert.assertTrue(Double.isNaN(MathUtils.reduce(orig, period, Double.POSITIVE_INFINITY))); Assert.assertTrue(Double.isNaN(MathUtils.reduce(Double.POSITIVE_INFINITY, period, offset))); Assert.assertTrue(Double.isNaN(MathUtils.reduce(orig, Double.POSITIVE_INFINITY, offset))); Assert.assertTrue(Double.isNaN(MathUtils.reduce(orig, Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY))); Assert.assertTrue(Double.isNaN(MathUtils.reduce(Double.POSITIVE_INFINITY, period, Double.POSITIVE_INFINITY))); Assert.assertTrue(Double.isNaN(MathUtils.reduce(Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY, offset))); Assert.assertTrue(Double.isNaN(MathUtils.reduce(Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY))); } @Test public void testReduceComparedWithNormalizeAngle() { final double tol = Math.ulp(1d); final double period = 2 * Math.PI; for (double a = -15; a <= 15; a += 0.5) { for (double center = -15; center <= 15; center += 1) { final double nA = MathUtils.normalizeAngle(a, center); final double offset = center - Math.PI; final double r = MathUtils.reduce(a, period, offset); Assert.assertEquals(nA, r + offset, tol); } } } @Test public void testSignByte() { final byte one = (byte) 1; Assert.assertEquals((byte) 1, MathUtils.copySign(one, (byte) 2)); Assert.assertEquals((byte) (-1), MathUtils.copySign(one, (byte) (-2))); } @Test public void testSignInt() { final int one = 1; Assert.assertEquals(1, MathUtils.copySign(one, 2)); Assert.assertEquals((-1), MathUtils.copySign(one, -2)); } @Test public void testSignLong() { final long one = 1L; Assert.assertEquals(1L, MathUtils.copySign(one, 2L)); Assert.assertEquals(-1L, MathUtils.copySign(one, -2L)); } @Test public void testSignShort() { final short one = (short) 1; Assert.assertEquals((short) 1, MathUtils.copySign(one, (short) 2)); Assert.assertEquals((short) (-1), MathUtils.copySign(one, (short) (-2))); } @Test public void testCheckFinite() { try { MathUtils.checkFinite(Double.POSITIVE_INFINITY); Assert.fail("an exception should have been thrown"); } catch (NotFiniteNumberException e) { // Expected } try { MathUtils.checkFinite(Double.NEGATIVE_INFINITY); Assert.fail("an exception should have been thrown"); } catch (NotFiniteNumberException e) { // Expected } try { MathUtils.checkFinite(Double.NaN); Assert.fail("an exception should have been thrown"); } catch (NotFiniteNumberException e) { // Expected } try { MathUtils.checkFinite(new double[] {0, -1, Double.POSITIVE_INFINITY, -2, 3}); Assert.fail("an exception should have been thrown"); } catch (NotFiniteNumberException e) { // Expected } try { MathUtils.checkFinite(new double[] {1, Double.NEGATIVE_INFINITY, -2, 3}); Assert.fail("an exception should have been thrown"); } catch (NotFiniteNumberException e) { // Expected } try { MathUtils.checkFinite(new double[] {4, 3, -1, Double.NaN, -2, 1}); Assert.fail("an exception should have been thrown"); } catch (NotFiniteNumberException e) { // Expected } } @Test public void testCheckNotNull1() { try { Object obj = null; MathUtils.checkNotNull(obj); } catch (NullArgumentException e) { // Expected. } } @Test public void testCheckNotNull2() { try { double[] array = null; MathUtils.checkNotNull(array, LocalizedFormats.INPUT_ARRAY); } catch (NullArgumentException e) { // Expected. } } @Test public void testCopySignByte() { byte a = MathUtils.copySign(Byte.MIN_VALUE, (byte) -1); Assert.assertEquals(Byte.MIN_VALUE, a); final byte minValuePlusOne = Byte.MIN_VALUE + (byte) 1; a = MathUtils.copySign(minValuePlusOne, (byte) 1); Assert.assertEquals(Byte.MAX_VALUE, a); a = MathUtils.copySign(Byte.MAX_VALUE, (byte) -1); Assert.assertEquals(minValuePlusOne, a); final byte one = 1; byte val = -2; a = MathUtils.copySign(val, one); Assert.assertEquals(-val, a); final byte minusOne = -one; val = 2; a = MathUtils.copySign(val, minusOne); Assert.assertEquals(-val, a); val = 0; a = MathUtils.copySign(val, minusOne); Assert.assertEquals(val, a); val = 0; a = MathUtils.copySign(val, one); Assert.assertEquals(val, a); } @Test(expected=MathArithmeticException.class) public void testCopySignByte2() { MathUtils.copySign(Byte.MIN_VALUE, (byte) 1); } } commons-math3-3.2-src/src/test/java/org/apache/commons/math3/util/DoubleArrayAbstractTest.java100644 1750 1750 7477 12126627675 31161 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.util; import org.apache.commons.math3.stat.StatUtils; import org.junit.Assert; import org.junit.Test; /** * This class contains test cases for the ExpandableDoubleArray. * * @version $Id: DoubleArrayAbstractTest.java 1244107 2012-02-14 16:17:55Z erans $ */ public abstract class DoubleArrayAbstractTest { protected DoubleArray da = null; // Array used to test rolling protected DoubleArray ra = null; @Test public void testAdd1000() { for (int i = 0; i < 1000; i++) { da.addElement(i); } Assert.assertEquals( "Number of elements should be equal to 1000 after adding 1000 values", 1000, da.getNumElements()); Assert.assertEquals( "The element at the 56th index should be 56", 56.0, da.getElement(56), Double.MIN_VALUE); } @Test public void testGetValues() { double[] controlArray = { 2.0, 4.0, 6.0 }; da.addElement(2.0); da.addElement(4.0); da.addElement(6.0); double[] testArray = da.getElements(); for (int i = 0; i < da.getNumElements(); i++) { Assert.assertEquals( "The testArray values should equal the controlArray values, index i: " + i + " does not match", testArray[i], controlArray[i], Double.MIN_VALUE); } } @Test public void testAddElementRolling() { ra.addElement(0.5); ra.addElement(1.0); ra.addElement(1.0); ra.addElement(1.0); ra.addElement(1.0); ra.addElement(1.0); ra.addElementRolling(2.0); Assert.assertEquals( "There should be 6 elements in the eda", 6, ra.getNumElements()); Assert.assertEquals( "The max element should be 2.0", 2.0, StatUtils.max(ra.getElements()), Double.MIN_VALUE); Assert.assertEquals( "The min element should be 1.0", 1.0, StatUtils.min(ra.getElements()), Double.MIN_VALUE); for (int i = 0; i < 1024; i++) { ra.addElementRolling(i); } Assert.assertEquals( "We just inserted 1024 rolling elements, num elements should still be 6", 6, ra.getNumElements()); } @Test public void testMinMax() { da.addElement(2.0); da.addElement(22.0); da.addElement(-2.0); da.addElement(21.0); da.addElement(22.0); da.addElement(42.0); da.addElement(62.0); da.addElement(22.0); da.addElement(122.0); da.addElement(1212.0); Assert.assertEquals("Min should be -2.0", -2.0, StatUtils.min(da.getElements()), Double.MIN_VALUE); Assert.assertEquals( "Max should be 1212.0", 1212.0, StatUtils.max(da.getElements()), Double.MIN_VALUE); } } commons-math3-3.2-src/src/test/java/org/apache/commons/math3/util/FastMathStrictComparisonTest.java100644 1750 1750 24375 12126627675 32233 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.util; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.lang.reflect.Modifier; import java.lang.reflect.Type; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import org.junit.Assert; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.Parameterized; import org.junit.runners.Parameterized.Parameters; /** * Test to compare FastMath results against StrictMath results for boundary values. *

                          * Running all tests independently:
                          * {@code mvn test -Dtest=FastMathStrictComparisonTest}
                          * or just run tests against a single method (e.g. scalb):
                          * {@code mvn test -Dtest=FastMathStrictComparisonTest -DargLine="-DtestMethod=scalb"} */ @RunWith(Parameterized.class) public class FastMathStrictComparisonTest { // Values which often need special handling private static final Double[] DOUBLE_SPECIAL_VALUES = { -0.0, +0.0, // 1,2 Double.NaN, // 3 Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY, // 4,5 -Double.MAX_VALUE, Double.MAX_VALUE, // 6,7 // decreasing order of absolute value to help catch first failure -Precision.EPSILON, Precision.EPSILON, // 8,9 -Precision.SAFE_MIN, Precision.SAFE_MIN, // 10,11 -Double.MIN_VALUE, Double.MIN_VALUE, // 12,13 }; private static final Float [] FLOAT_SPECIAL_VALUES = { -0.0f, +0.0f, // 1,2 Float.NaN, // 3 Float.NEGATIVE_INFINITY, Float.POSITIVE_INFINITY, // 4,5 Float.MIN_VALUE, Float.MAX_VALUE, // 6,7 -Float.MIN_VALUE, -Float.MAX_VALUE, // 8,9 }; private static final Object [] LONG_SPECIAL_VALUES = { -1,0,1, // 1,2,3 Long.MIN_VALUE, Long.MAX_VALUE, // 4,5 }; private static final Object[] INT_SPECIAL_VALUES = { -1,0,1, // 1,2,3 Integer.MIN_VALUE, Integer.MAX_VALUE, // 4,5 }; private final Method mathMethod; private final Method fastMethod; private final Type[] types; private final Object[][] valueArrays; public FastMathStrictComparisonTest(Method m, Method f, Type[] types, Object[][] data) throws Exception{ this.mathMethod=m; this.fastMethod=f; this.types=types; this.valueArrays=data; } @Test public void test1() throws Exception{ setupMethodCall(mathMethod, fastMethod, types, valueArrays); } private static boolean isNumber(Double d) { return !(d.isInfinite() || d.isNaN()); } private static boolean isNumber(Float f) { return !(f.isInfinite() || f.isNaN()); } private static void reportFailedResults(Method mathMethod, Object[] params, Object expected, Object actual, int[] entries){ final String methodName = mathMethod.getName(); String format = null; long actL=0; long expL=0; if (expected instanceof Double) { Double exp = (Double) expected; Double act = (Double) actual; if (isNumber(exp) && isNumber(act) && exp != 0) { // show difference as hex actL = Double.doubleToLongBits(act); expL = Double.doubleToLongBits(exp); if (Math.abs(actL-expL)==1) { // Not 100% sure off-by-one errors are allowed everywhere, so only allow for these methods if (methodName.equals("toRadians") || methodName.equals("atan2")) { return; } } format = "%016x"; } } else if (expected instanceof Float ){ Float exp = (Float) expected; Float act = (Float) actual; if (isNumber(exp) && isNumber(act) && exp != 0) { // show difference as hex actL = Float.floatToIntBits(act); expL = Float.floatToIntBits(exp); format = "%08x"; } } StringBuilder sb = new StringBuilder(); sb.append(mathMethod.getReturnType().getSimpleName()); sb.append(" "); sb.append(methodName); sb.append("("); String sep = ""; for(Object o : params){ sb.append(sep); sb.append(o); sep=", "; } sb.append(") expected "); if (format != null){ sb.append(String.format(format, expL)); } else { sb.append(expected); } sb.append(" actual "); if (format != null){ sb.append(String.format(format, actL)); } else { sb.append(actual); } sb.append(" entries "); sb.append(Arrays.toString(entries)); String message = sb.toString(); final boolean fatal = true; if (fatal) { Assert.fail(message); } else { System.out.println(message); } } private static void callMethods(Method mathMethod, Method fastMethod, Object[] params, int[] entries) throws IllegalAccessException, InvocationTargetException { try { Object expected = mathMethod.invoke(mathMethod, params); Object actual = fastMethod.invoke(mathMethod, params); if (!expected.equals(actual)) { reportFailedResults(mathMethod, params, expected, actual, entries); } } catch (IllegalArgumentException e) { Assert.fail(mathMethod+" "+e); } } private static void setupMethodCall(Method mathMethod, Method fastMethod, Type[] types, Object[][] valueArrays) throws Exception { Object[] params = new Object[types.length]; int entry1 = 0; int[] entries = new int[types.length]; for(Object d : valueArrays[0]) { entry1++; params[0] = d; entries[0] = entry1; if (params.length > 1){ int entry2 = 0; for(Object d1 : valueArrays[1]) { entry2++; params[1] = d1; entries[1] = entry2; callMethods(mathMethod, fastMethod, params, entries); } } else { callMethods(mathMethod, fastMethod, params, entries); } } } @Parameters public static List data() throws Exception { String singleMethod = System.getProperty("testMethod"); List list = new ArrayList(); for(Method mathMethod : StrictMath.class.getDeclaredMethods()) { method: if (Modifier.isPublic(mathMethod.getModifiers())){// Only test public methods Type []types = mathMethod.getGenericParameterTypes(); if (types.length >=1) { // Only check methods with at least one parameter try { // Get the corresponding FastMath method Method fastMethod = FastMath.class.getDeclaredMethod(mathMethod.getName(), (Class[]) types); if (Modifier.isPublic(fastMethod.getModifiers())) { // It must be public too if (singleMethod != null && !fastMethod.getName().equals(singleMethod)) { break method; } Object [][] values = new Object[types.length][]; int index = 0; for(Type t : types) { if (t.equals(double.class)){ values[index]=DOUBLE_SPECIAL_VALUES; } else if (t.equals(float.class)) { values[index]=FLOAT_SPECIAL_VALUES; } else if (t.equals(long.class)) { values[index]=LONG_SPECIAL_VALUES; } else if (t.equals(int.class)) { values[index]=INT_SPECIAL_VALUES; } else { System.out.println("Cannot handle class "+t+" for "+mathMethod); break method; } index++; } // System.out.println(fastMethod); /* * The current implementation runs each method as a separate test. * Could be amended to run each value as a separate test */ list.add(new Object[]{mathMethod, fastMethod, types, values}); // setupMethodCall(mathMethod, fastMethod, params, data); } else { System.out.println("Cannot find public FastMath method corresponding to: "+mathMethod); } } catch (NoSuchMethodException e) { System.out.println("Cannot find FastMath method corresponding to: "+mathMethod); } } } } return list; } } commons-math3-3.2-src/src/test/java/org/apache/commons/math3/util/TestBean.java100644 1750 1750 3256 12126627675 26120 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.util; import org.apache.commons.math3.exception.MathUnsupportedOperationException; import org.apache.commons.math3.exception.util.LocalizedFormats; /** * @version $Id: TestBean.java 1244107 2012-02-14 16:17:55Z erans $ */ public class TestBean { private Double x = Double.valueOf(1.0); private String y = "1.0"; /** * */ public Double getX() { return x; } /** * */ public String getY() { return y; } /** * */ public void setX(Double double1) { x = double1; } /** * */ public void setY(String string) { y = string; } /** * */ public Double getZ() { throw new MathUnsupportedOperationException(LocalizedFormats.SIMPLE_MESSAGE, "?"); } /** * */ public void setZ(Double double1) { } } commons-math3-3.2-src/src/test/java/org/apache/commons/math3/util/ArithmeticUtilsTest.java100644 1750 1750 100337 12126627675 30423 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.util; import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; import java.util.List; import java.util.Map; import java.math.BigInteger; import org.apache.commons.math3.exception.MathArithmeticException; import org.apache.commons.math3.exception.MathIllegalArgumentException; import org.apache.commons.math3.exception.NotPositiveException; import org.apache.commons.math3.exception.NumberIsTooLargeException; import org.apache.commons.math3.random.RandomDataImpl; import org.junit.Assert; import org.junit.Test; /** * Test cases for the {@link ArithmeticUtils} class. * * @version $Id: ArithmeticUtilsTest.java 1371680 2012-08-10 12:15:23Z luc $ */ public class ArithmeticUtilsTest { /** cached binomial coefficients */ private static final List> binomialCache = new ArrayList>(); /** Verify that b(0,0) = 1 */ @Test public void test0Choose0() { Assert.assertEquals(ArithmeticUtils.binomialCoefficientDouble(0, 0), 1d, 0); Assert.assertEquals(ArithmeticUtils.binomialCoefficientLog(0, 0), 0d, 0); Assert.assertEquals(ArithmeticUtils.binomialCoefficient(0, 0), 1); } @Test public void testAddAndCheck() { int big = Integer.MAX_VALUE; int bigNeg = Integer.MIN_VALUE; Assert.assertEquals(big, ArithmeticUtils.addAndCheck(big, 0)); try { ArithmeticUtils.addAndCheck(big, 1); Assert.fail("Expecting MathArithmeticException"); } catch (MathArithmeticException ex) { } try { ArithmeticUtils.addAndCheck(bigNeg, -1); Assert.fail("Expecting MathArithmeticException"); } catch (MathArithmeticException ex) { } } @Test public void testAddAndCheckLong() { long max = Long.MAX_VALUE; long min = Long.MIN_VALUE; Assert.assertEquals(max, ArithmeticUtils.addAndCheck(max, 0L)); Assert.assertEquals(min, ArithmeticUtils.addAndCheck(min, 0L)); Assert.assertEquals(max, ArithmeticUtils.addAndCheck(0L, max)); Assert.assertEquals(min, ArithmeticUtils.addAndCheck(0L, min)); Assert.assertEquals(1, ArithmeticUtils.addAndCheck(-1L, 2L)); Assert.assertEquals(1, ArithmeticUtils.addAndCheck(2L, -1L)); Assert.assertEquals(-3, ArithmeticUtils.addAndCheck(-2L, -1L)); Assert.assertEquals(min, ArithmeticUtils.addAndCheck(min + 1, -1L)); testAddAndCheckLongFailure(max, 1L); testAddAndCheckLongFailure(min, -1L); testAddAndCheckLongFailure(1L, max); testAddAndCheckLongFailure(-1L, min); } @Test public void testBinomialCoefficient() { long[] bcoef5 = { 1, 5, 10, 10, 5, 1 }; long[] bcoef6 = { 1, 6, 15, 20, 15, 6, 1 }; for (int i = 0; i < 6; i++) { Assert.assertEquals("5 choose " + i, bcoef5[i], ArithmeticUtils.binomialCoefficient(5, i)); } for (int i = 0; i < 7; i++) { Assert.assertEquals("6 choose " + i, bcoef6[i], ArithmeticUtils.binomialCoefficient(6, i)); } for (int n = 1; n < 10; n++) { for (int k = 0; k <= n; k++) { Assert.assertEquals(n + " choose " + k, binomialCoefficient(n, k), ArithmeticUtils.binomialCoefficient(n, k)); Assert.assertEquals(n + " choose " + k, binomialCoefficient(n, k), ArithmeticUtils.binomialCoefficientDouble(n, k), Double.MIN_VALUE); Assert.assertEquals(n + " choose " + k, FastMath.log(binomialCoefficient(n, k)), ArithmeticUtils.binomialCoefficientLog(n, k), 10E-12); } } int[] n = { 34, 66, 100, 1500, 1500 }; int[] k = { 17, 33, 10, 1500 - 4, 4 }; for (int i = 0; i < n.length; i++) { long expected = binomialCoefficient(n[i], k[i]); Assert.assertEquals(n[i] + " choose " + k[i], expected, ArithmeticUtils.binomialCoefficient(n[i], k[i])); Assert.assertEquals(n[i] + " choose " + k[i], expected, ArithmeticUtils.binomialCoefficientDouble(n[i], k[i]), 0.0); Assert.assertEquals("log(" + n[i] + " choose " + k[i] + ")", FastMath.log(expected), ArithmeticUtils.binomialCoefficientLog(n[i], k[i]), 0.0); } } @Test public void testBinomialCoefficientFail() { try { ArithmeticUtils.binomialCoefficient(4, 5); Assert.fail("expecting MathIllegalArgumentException"); } catch (MathIllegalArgumentException ex) { // ignored } try { ArithmeticUtils.binomialCoefficientDouble(4, 5); Assert.fail("expecting MathIllegalArgumentException"); } catch (MathIllegalArgumentException ex) { // ignored } try { ArithmeticUtils.binomialCoefficientLog(4, 5); Assert.fail("expecting MathIllegalArgumentException"); } catch (MathIllegalArgumentException ex) { // ignored } try { ArithmeticUtils.binomialCoefficient(-1, -2); Assert.fail("expecting MathIllegalArgumentException"); } catch (MathIllegalArgumentException ex) { // ignored } try { ArithmeticUtils.binomialCoefficientDouble(-1, -2); Assert.fail("expecting MathIllegalArgumentException"); } catch (MathIllegalArgumentException ex) { // ignored } try { ArithmeticUtils.binomialCoefficientLog(-1, -2); Assert.fail("expecting MathIllegalArgumentException"); } catch (MathIllegalArgumentException ex) { // ignored } try { ArithmeticUtils.binomialCoefficient(67, 30); Assert.fail("expecting MathArithmeticException"); } catch (MathArithmeticException ex) { // ignored } try { ArithmeticUtils.binomialCoefficient(67, 34); Assert.fail("expecting MathArithmeticException"); } catch (MathArithmeticException ex) { // ignored } double x = ArithmeticUtils.binomialCoefficientDouble(1030, 515); Assert.assertTrue("expecting infinite binomial coefficient", Double .isInfinite(x)); } /** * Tests correctness for large n and sharpness of upper bound in API doc * JIRA: MATH-241 */ @Test public void testBinomialCoefficientLarge() throws Exception { // This tests all legal and illegal values for n <= 200. for (int n = 0; n <= 200; n++) { for (int k = 0; k <= n; k++) { long ourResult = -1; long exactResult = -1; boolean shouldThrow = false; boolean didThrow = false; try { ourResult = ArithmeticUtils.binomialCoefficient(n, k); } catch (MathArithmeticException ex) { didThrow = true; } try { exactResult = binomialCoefficient(n, k); } catch (MathArithmeticException ex) { shouldThrow = true; } Assert.assertEquals(n + " choose " + k, exactResult, ourResult); Assert.assertEquals(n + " choose " + k, shouldThrow, didThrow); Assert.assertTrue(n + " choose " + k, (n > 66 || !didThrow)); if (!shouldThrow && exactResult > 1) { Assert.assertEquals(n + " choose " + k, 1., ArithmeticUtils.binomialCoefficientDouble(n, k) / exactResult, 1e-10); Assert.assertEquals(n + " choose " + k, 1, ArithmeticUtils.binomialCoefficientLog(n, k) / FastMath.log(exactResult), 1e-10); } } } long ourResult = ArithmeticUtils.binomialCoefficient(300, 3); long exactResult = binomialCoefficient(300, 3); Assert.assertEquals(exactResult, ourResult); ourResult = ArithmeticUtils.binomialCoefficient(700, 697); exactResult = binomialCoefficient(700, 697); Assert.assertEquals(exactResult, ourResult); // This one should throw try { ArithmeticUtils.binomialCoefficient(700, 300); Assert.fail("Expecting MathArithmeticException"); } catch (MathArithmeticException ex) { // Expected } int n = 10000; ourResult = ArithmeticUtils.binomialCoefficient(n, 3); exactResult = binomialCoefficient(n, 3); Assert.assertEquals(exactResult, ourResult); Assert.assertEquals(1, ArithmeticUtils.binomialCoefficientDouble(n, 3) / exactResult, 1e-10); Assert.assertEquals(1, ArithmeticUtils.binomialCoefficientLog(n, 3) / FastMath.log(exactResult), 1e-10); } @Test public void testFactorial() { for (int i = 1; i < 21; i++) { Assert.assertEquals(i + "! ", factorial(i), ArithmeticUtils.factorial(i)); Assert.assertEquals(i + "! ", factorial(i), ArithmeticUtils.factorialDouble(i), Double.MIN_VALUE); Assert.assertEquals(i + "! ", FastMath.log(factorial(i)), ArithmeticUtils.factorialLog(i), 10E-12); } Assert.assertEquals("0", 1, ArithmeticUtils.factorial(0)); Assert.assertEquals("0", 1.0d, ArithmeticUtils.factorialDouble(0), 1E-14); Assert.assertEquals("0", 0.0d, ArithmeticUtils.factorialLog(0), 1E-14); } @Test public void testFactorialFail() { try { ArithmeticUtils.factorial(-1); Assert.fail("expecting MathIllegalArgumentException"); } catch (MathIllegalArgumentException ex) { // ignored } try { ArithmeticUtils.factorialDouble(-1); Assert.fail("expecting MathIllegalArgumentException"); } catch (MathIllegalArgumentException ex) { // ignored } try { ArithmeticUtils.factorialLog(-1); Assert.fail("expecting MathIllegalArgumentException"); } catch (MathIllegalArgumentException ex) { // ignored } try { ArithmeticUtils.factorial(21); Assert.fail("expecting MathArithmeticException"); } catch (MathArithmeticException ex) { // ignored } Assert.assertTrue("expecting infinite factorial value", Double.isInfinite(ArithmeticUtils.factorialDouble(171))); } @Test public void testGcd() { int a = 30; int b = 50; int c = 77; Assert.assertEquals(0, ArithmeticUtils.gcd(0, 0)); Assert.assertEquals(b, ArithmeticUtils.gcd(0, b)); Assert.assertEquals(a, ArithmeticUtils.gcd(a, 0)); Assert.assertEquals(b, ArithmeticUtils.gcd(0, -b)); Assert.assertEquals(a, ArithmeticUtils.gcd(-a, 0)); Assert.assertEquals(10, ArithmeticUtils.gcd(a, b)); Assert.assertEquals(10, ArithmeticUtils.gcd(-a, b)); Assert.assertEquals(10, ArithmeticUtils.gcd(a, -b)); Assert.assertEquals(10, ArithmeticUtils.gcd(-a, -b)); Assert.assertEquals(1, ArithmeticUtils.gcd(a, c)); Assert.assertEquals(1, ArithmeticUtils.gcd(-a, c)); Assert.assertEquals(1, ArithmeticUtils.gcd(a, -c)); Assert.assertEquals(1, ArithmeticUtils.gcd(-a, -c)); Assert.assertEquals(3 * (1<<15), ArithmeticUtils.gcd(3 * (1<<20), 9 * (1<<15))); Assert.assertEquals(Integer.MAX_VALUE, ArithmeticUtils.gcd(Integer.MAX_VALUE, 0)); Assert.assertEquals(Integer.MAX_VALUE, ArithmeticUtils.gcd(-Integer.MAX_VALUE, 0)); Assert.assertEquals(1<<30, ArithmeticUtils.gcd(1<<30, -Integer.MIN_VALUE)); try { // gcd(Integer.MIN_VALUE, 0) > Integer.MAX_VALUE ArithmeticUtils.gcd(Integer.MIN_VALUE, 0); Assert.fail("expecting MathArithmeticException"); } catch (MathArithmeticException expected) { // expected } try { // gcd(0, Integer.MIN_VALUE) > Integer.MAX_VALUE ArithmeticUtils.gcd(0, Integer.MIN_VALUE); Assert.fail("expecting MathArithmeticException"); } catch (MathArithmeticException expected) { // expected } try { // gcd(Integer.MIN_VALUE, Integer.MIN_VALUE) > Integer.MAX_VALUE ArithmeticUtils.gcd(Integer.MIN_VALUE, Integer.MIN_VALUE); Assert.fail("expecting MathArithmeticException"); } catch (MathArithmeticException expected) { // expected } } @Test public void testGcdConsistency() { int[] primeList = {19, 23, 53, 67, 73, 79, 101, 103, 111, 131}; ArrayList primes = new ArrayList(); for (int i = 0; i < primeList.length; i++) { primes.add(Integer.valueOf(primeList[i])); } RandomDataImpl randomData = new RandomDataImpl(); for (int i = 0; i < 20; i++) { Object[] sample = randomData.nextSample(primes, 4); int p1 = ((Integer) sample[0]).intValue(); int p2 = ((Integer) sample[1]).intValue(); int p3 = ((Integer) sample[2]).intValue(); int p4 = ((Integer) sample[3]).intValue(); int i1 = p1 * p2 * p3; int i2 = p1 * p2 * p4; int gcd = p1 * p2; Assert.assertEquals(gcd, ArithmeticUtils.gcd(i1, i2)); long l1 = i1; long l2 = i2; Assert.assertEquals(gcd, ArithmeticUtils.gcd(l1, l2)); } } @Test public void testGcdLong(){ long a = 30; long b = 50; long c = 77; Assert.assertEquals(0, ArithmeticUtils.gcd(0L, 0)); Assert.assertEquals(b, ArithmeticUtils.gcd(0, b)); Assert.assertEquals(a, ArithmeticUtils.gcd(a, 0)); Assert.assertEquals(b, ArithmeticUtils.gcd(0, -b)); Assert.assertEquals(a, ArithmeticUtils.gcd(-a, 0)); Assert.assertEquals(10, ArithmeticUtils.gcd(a, b)); Assert.assertEquals(10, ArithmeticUtils.gcd(-a, b)); Assert.assertEquals(10, ArithmeticUtils.gcd(a, -b)); Assert.assertEquals(10, ArithmeticUtils.gcd(-a, -b)); Assert.assertEquals(1, ArithmeticUtils.gcd(a, c)); Assert.assertEquals(1, ArithmeticUtils.gcd(-a, c)); Assert.assertEquals(1, ArithmeticUtils.gcd(a, -c)); Assert.assertEquals(1, ArithmeticUtils.gcd(-a, -c)); Assert.assertEquals(3L * (1L<<45), ArithmeticUtils.gcd(3L * (1L<<50), 9L * (1L<<45))); Assert.assertEquals(1L<<45, ArithmeticUtils.gcd(1L<<45, Long.MIN_VALUE)); Assert.assertEquals(Long.MAX_VALUE, ArithmeticUtils.gcd(Long.MAX_VALUE, 0L)); Assert.assertEquals(Long.MAX_VALUE, ArithmeticUtils.gcd(-Long.MAX_VALUE, 0L)); Assert.assertEquals(1, ArithmeticUtils.gcd(60247241209L, 153092023L)); try { // gcd(Long.MIN_VALUE, 0) > Long.MAX_VALUE ArithmeticUtils.gcd(Long.MIN_VALUE, 0); Assert.fail("expecting MathArithmeticException"); } catch (MathArithmeticException expected) { // expected } try { // gcd(0, Long.MIN_VALUE) > Long.MAX_VALUE ArithmeticUtils.gcd(0, Long.MIN_VALUE); Assert.fail("expecting MathArithmeticException"); } catch (MathArithmeticException expected) { // expected } try { // gcd(Long.MIN_VALUE, Long.MIN_VALUE) > Long.MAX_VALUE ArithmeticUtils.gcd(Long.MIN_VALUE, Long.MIN_VALUE); Assert.fail("expecting MathArithmeticException"); } catch (MathArithmeticException expected) { // expected } } @Test public void testLcm() { int a = 30; int b = 50; int c = 77; Assert.assertEquals(0, ArithmeticUtils.lcm(0, b)); Assert.assertEquals(0, ArithmeticUtils.lcm(a, 0)); Assert.assertEquals(b, ArithmeticUtils.lcm(1, b)); Assert.assertEquals(a, ArithmeticUtils.lcm(a, 1)); Assert.assertEquals(150, ArithmeticUtils.lcm(a, b)); Assert.assertEquals(150, ArithmeticUtils.lcm(-a, b)); Assert.assertEquals(150, ArithmeticUtils.lcm(a, -b)); Assert.assertEquals(150, ArithmeticUtils.lcm(-a, -b)); Assert.assertEquals(2310, ArithmeticUtils.lcm(a, c)); // Assert that no intermediate value overflows: // The naive implementation of lcm(a,b) would be (a*b)/gcd(a,b) Assert.assertEquals((1<<20)*15, ArithmeticUtils.lcm((1<<20)*3, (1<<20)*5)); // Special case Assert.assertEquals(0, ArithmeticUtils.lcm(0, 0)); try { // lcm == abs(MIN_VALUE) cannot be represented as a nonnegative int ArithmeticUtils.lcm(Integer.MIN_VALUE, 1); Assert.fail("Expecting MathArithmeticException"); } catch (MathArithmeticException expected) { // expected } try { // lcm == abs(MIN_VALUE) cannot be represented as a nonnegative int ArithmeticUtils.lcm(Integer.MIN_VALUE, 1<<20); Assert.fail("Expecting MathArithmeticException"); } catch (MathArithmeticException expected) { // expected } try { ArithmeticUtils.lcm(Integer.MAX_VALUE, Integer.MAX_VALUE - 1); Assert.fail("Expecting MathArithmeticException"); } catch (MathArithmeticException expected) { // expected } } @Test public void testLcmLong() { long a = 30; long b = 50; long c = 77; Assert.assertEquals(0, ArithmeticUtils.lcm(0, b)); Assert.assertEquals(0, ArithmeticUtils.lcm(a, 0)); Assert.assertEquals(b, ArithmeticUtils.lcm(1, b)); Assert.assertEquals(a, ArithmeticUtils.lcm(a, 1)); Assert.assertEquals(150, ArithmeticUtils.lcm(a, b)); Assert.assertEquals(150, ArithmeticUtils.lcm(-a, b)); Assert.assertEquals(150, ArithmeticUtils.lcm(a, -b)); Assert.assertEquals(150, ArithmeticUtils.lcm(-a, -b)); Assert.assertEquals(2310, ArithmeticUtils.lcm(a, c)); Assert.assertEquals(Long.MAX_VALUE, ArithmeticUtils.lcm(60247241209L, 153092023L)); // Assert that no intermediate value overflows: // The naive implementation of lcm(a,b) would be (a*b)/gcd(a,b) Assert.assertEquals((1L<<50)*15, ArithmeticUtils.lcm((1L<<45)*3, (1L<<50)*5)); // Special case Assert.assertEquals(0L, ArithmeticUtils.lcm(0L, 0L)); try { // lcm == abs(MIN_VALUE) cannot be represented as a nonnegative int ArithmeticUtils.lcm(Long.MIN_VALUE, 1); Assert.fail("Expecting MathArithmeticException"); } catch (MathArithmeticException expected) { // expected } try { // lcm == abs(MIN_VALUE) cannot be represented as a nonnegative int ArithmeticUtils.lcm(Long.MIN_VALUE, 1<<20); Assert.fail("Expecting MathArithmeticException"); } catch (MathArithmeticException expected) { // expected } Assert.assertEquals((long) Integer.MAX_VALUE * (Integer.MAX_VALUE - 1), ArithmeticUtils.lcm((long)Integer.MAX_VALUE, Integer.MAX_VALUE - 1)); try { ArithmeticUtils.lcm(Long.MAX_VALUE, Long.MAX_VALUE - 1); Assert.fail("Expecting MathArithmeticException"); } catch (MathArithmeticException expected) { // expected } } @Test public void testMulAndCheck() { int big = Integer.MAX_VALUE; int bigNeg = Integer.MIN_VALUE; Assert.assertEquals(big, ArithmeticUtils.mulAndCheck(big, 1)); try { ArithmeticUtils.mulAndCheck(big, 2); Assert.fail("Expecting MathArithmeticException"); } catch (MathArithmeticException ex) { } try { ArithmeticUtils.mulAndCheck(bigNeg, 2); Assert.fail("Expecting MathArithmeticException"); } catch (MathArithmeticException ex) { } } @Test public void testMulAndCheckLong() { long max = Long.MAX_VALUE; long min = Long.MIN_VALUE; Assert.assertEquals(max, ArithmeticUtils.mulAndCheck(max, 1L)); Assert.assertEquals(min, ArithmeticUtils.mulAndCheck(min, 1L)); Assert.assertEquals(0L, ArithmeticUtils.mulAndCheck(max, 0L)); Assert.assertEquals(0L, ArithmeticUtils.mulAndCheck(min, 0L)); Assert.assertEquals(max, ArithmeticUtils.mulAndCheck(1L, max)); Assert.assertEquals(min, ArithmeticUtils.mulAndCheck(1L, min)); Assert.assertEquals(0L, ArithmeticUtils.mulAndCheck(0L, max)); Assert.assertEquals(0L, ArithmeticUtils.mulAndCheck(0L, min)); Assert.assertEquals(1L, ArithmeticUtils.mulAndCheck(-1L, -1L)); Assert.assertEquals(min, ArithmeticUtils.mulAndCheck(min / 2, 2)); testMulAndCheckLongFailure(max, 2L); testMulAndCheckLongFailure(2L, max); testMulAndCheckLongFailure(min, 2L); testMulAndCheckLongFailure(2L, min); testMulAndCheckLongFailure(min, -1L); testMulAndCheckLongFailure(-1L, min); } @Test public void testSubAndCheck() { int big = Integer.MAX_VALUE; int bigNeg = Integer.MIN_VALUE; Assert.assertEquals(big, ArithmeticUtils.subAndCheck(big, 0)); Assert.assertEquals(bigNeg + 1, ArithmeticUtils.subAndCheck(bigNeg, -1)); Assert.assertEquals(-1, ArithmeticUtils.subAndCheck(bigNeg, -big)); try { ArithmeticUtils.subAndCheck(big, -1); Assert.fail("Expecting MathArithmeticException"); } catch (MathArithmeticException ex) { } try { ArithmeticUtils.subAndCheck(bigNeg, 1); Assert.fail("Expecting MathArithmeticException"); } catch (MathArithmeticException ex) { } } @Test public void testSubAndCheckErrorMessage() { int big = Integer.MAX_VALUE; try { ArithmeticUtils.subAndCheck(big, -1); Assert.fail("Expecting MathArithmeticException"); } catch (MathArithmeticException ex) { Assert.assertTrue(ex.getMessage().length() > 1); } } @Test public void testSubAndCheckLong() { long max = Long.MAX_VALUE; long min = Long.MIN_VALUE; Assert.assertEquals(max, ArithmeticUtils.subAndCheck(max, 0)); Assert.assertEquals(min, ArithmeticUtils.subAndCheck(min, 0)); Assert.assertEquals(-max, ArithmeticUtils.subAndCheck(0, max)); Assert.assertEquals(min + 1, ArithmeticUtils.subAndCheck(min, -1)); // min == -1-max Assert.assertEquals(-1, ArithmeticUtils.subAndCheck(-max - 1, -max)); Assert.assertEquals(max, ArithmeticUtils.subAndCheck(-1, -1 - max)); testSubAndCheckLongFailure(0L, min); testSubAndCheckLongFailure(max, -1L); testSubAndCheckLongFailure(min, 1L); } @Test public void testPow() { Assert.assertEquals(1801088541, ArithmeticUtils.pow(21, 7)); Assert.assertEquals(1, ArithmeticUtils.pow(21, 0)); try { ArithmeticUtils.pow(21, -7); Assert.fail("Expecting MathIllegalArgumentException"); } catch (MathIllegalArgumentException e) { // expected behavior } Assert.assertEquals(1801088541, ArithmeticUtils.pow(21, 7l)); Assert.assertEquals(1, ArithmeticUtils.pow(21, 0l)); try { ArithmeticUtils.pow(21, -7l); Assert.fail("Expecting MathIllegalArgumentException"); } catch (MathIllegalArgumentException e) { // expected behavior } Assert.assertEquals(1801088541l, ArithmeticUtils.pow(21l, 7)); Assert.assertEquals(1l, ArithmeticUtils.pow(21l, 0)); try { ArithmeticUtils.pow(21l, -7); Assert.fail("Expecting MathIllegalArgumentException"); } catch (MathIllegalArgumentException e) { // expected behavior } Assert.assertEquals(1801088541l, ArithmeticUtils.pow(21l, 7l)); Assert.assertEquals(1l, ArithmeticUtils.pow(21l, 0l)); try { ArithmeticUtils.pow(21l, -7l); Assert.fail("Expecting MathIllegalArgumentException"); } catch (MathIllegalArgumentException e) { // expected behavior } BigInteger twentyOne = BigInteger.valueOf(21l); Assert.assertEquals(BigInteger.valueOf(1801088541l), ArithmeticUtils.pow(twentyOne, 7)); Assert.assertEquals(BigInteger.ONE, ArithmeticUtils.pow(twentyOne, 0)); try { ArithmeticUtils.pow(twentyOne, -7); Assert.fail("Expecting MathIllegalArgumentException"); } catch (MathIllegalArgumentException e) { // expected behavior } Assert.assertEquals(BigInteger.valueOf(1801088541l), ArithmeticUtils.pow(twentyOne, 7l)); Assert.assertEquals(BigInteger.ONE, ArithmeticUtils.pow(twentyOne, 0l)); try { ArithmeticUtils.pow(twentyOne, -7l); Assert.fail("Expecting MathIllegalArgumentException"); } catch (MathIllegalArgumentException e) { // expected behavior } Assert.assertEquals(BigInteger.valueOf(1801088541l), ArithmeticUtils.pow(twentyOne, BigInteger.valueOf(7l))); Assert.assertEquals(BigInteger.ONE, ArithmeticUtils.pow(twentyOne, BigInteger.ZERO)); try { ArithmeticUtils.pow(twentyOne, BigInteger.valueOf(-7l)); Assert.fail("Expecting MathIllegalArgumentException"); } catch (MathIllegalArgumentException e) { // expected behavior } BigInteger bigOne = new BigInteger("1543786922199448028351389769265814882661837148" + "4763915343722775611762713982220306372888519211" + "560905579993523402015636025177602059044911261"); Assert.assertEquals(bigOne, ArithmeticUtils.pow(twentyOne, 103)); Assert.assertEquals(bigOne, ArithmeticUtils.pow(twentyOne, 103l)); Assert.assertEquals(bigOne, ArithmeticUtils.pow(twentyOne, BigInteger.valueOf(103l))); } @Test public void testIsPowerOfTwo() { final int n = 1025; final boolean[] expected = new boolean[n]; Arrays.fill(expected, false); for (int i = 1; i < expected.length; i *= 2) { expected[i] = true; } for (int i = 0; i < expected.length; i++) { final boolean actual = ArithmeticUtils.isPowerOfTwo(i); Assert.assertTrue(Integer.toString(i), actual == expected[i]); } } @Test public void testStirlingS2() { Assert.assertEquals(1, ArithmeticUtils.stirlingS2(0, 0)); for (int n = 1; n < 30; ++n) { Assert.assertEquals(0, ArithmeticUtils.stirlingS2(n, 0)); Assert.assertEquals(1, ArithmeticUtils.stirlingS2(n, 1)); if (n > 2) { Assert.assertEquals((1l << (n - 1)) - 1l, ArithmeticUtils.stirlingS2(n, 2)); Assert.assertEquals(ArithmeticUtils.binomialCoefficient(n, 2), ArithmeticUtils.stirlingS2(n, n - 1)); } Assert.assertEquals(1, ArithmeticUtils.stirlingS2(n, n)); } Assert.assertEquals(536870911l, ArithmeticUtils.stirlingS2(30, 2)); Assert.assertEquals(576460752303423487l, ArithmeticUtils.stirlingS2(60, 2)); Assert.assertEquals( 25, ArithmeticUtils.stirlingS2( 5, 3)); Assert.assertEquals( 90, ArithmeticUtils.stirlingS2( 6, 3)); Assert.assertEquals( 65, ArithmeticUtils.stirlingS2( 6, 4)); Assert.assertEquals( 301, ArithmeticUtils.stirlingS2( 7, 3)); Assert.assertEquals( 350, ArithmeticUtils.stirlingS2( 7, 4)); Assert.assertEquals( 140, ArithmeticUtils.stirlingS2( 7, 5)); Assert.assertEquals( 966, ArithmeticUtils.stirlingS2( 8, 3)); Assert.assertEquals( 1701, ArithmeticUtils.stirlingS2( 8, 4)); Assert.assertEquals( 1050, ArithmeticUtils.stirlingS2( 8, 5)); Assert.assertEquals( 266, ArithmeticUtils.stirlingS2( 8, 6)); Assert.assertEquals( 3025, ArithmeticUtils.stirlingS2( 9, 3)); Assert.assertEquals( 7770, ArithmeticUtils.stirlingS2( 9, 4)); Assert.assertEquals( 6951, ArithmeticUtils.stirlingS2( 9, 5)); Assert.assertEquals( 2646, ArithmeticUtils.stirlingS2( 9, 6)); Assert.assertEquals( 462, ArithmeticUtils.stirlingS2( 9, 7)); Assert.assertEquals( 9330, ArithmeticUtils.stirlingS2(10, 3)); Assert.assertEquals(34105, ArithmeticUtils.stirlingS2(10, 4)); Assert.assertEquals(42525, ArithmeticUtils.stirlingS2(10, 5)); Assert.assertEquals(22827, ArithmeticUtils.stirlingS2(10, 6)); Assert.assertEquals( 5880, ArithmeticUtils.stirlingS2(10, 7)); Assert.assertEquals( 750, ArithmeticUtils.stirlingS2(10, 8)); } @Test(expected=NotPositiveException.class) public void testStirlingS2NegativeN() { ArithmeticUtils.stirlingS2(3, -1); } @Test(expected=NumberIsTooLargeException.class) public void testStirlingS2LargeK() { ArithmeticUtils.stirlingS2(3, 4); } @Test(expected=MathArithmeticException.class) public void testStirlingS2Overflow() { ArithmeticUtils.stirlingS2(26, 9); } /** * Exact (caching) recursive implementation to test against */ private long binomialCoefficient(int n, int k) throws MathArithmeticException { if (binomialCache.size() > n) { Long cachedResult = binomialCache.get(n).get(Integer.valueOf(k)); if (cachedResult != null) { return cachedResult.longValue(); } } long result = -1; if ((n == k) || (k == 0)) { result = 1; } else if ((k == 1) || (k == n - 1)) { result = n; } else { // Reduce stack depth for larger values of n if (k < n - 100) { binomialCoefficient(n - 100, k); } if (k > 100) { binomialCoefficient(n - 100, k - 100); } result = ArithmeticUtils.addAndCheck(binomialCoefficient(n - 1, k - 1), binomialCoefficient(n - 1, k)); } if (result == -1) { throw new MathArithmeticException(); } for (int i = binomialCache.size(); i < n + 1; i++) { binomialCache.add(new HashMap()); } binomialCache.get(n).put(Integer.valueOf(k), Long.valueOf(result)); return result; } /** * Exact direct multiplication implementation to test against */ private long factorial(int n) { long result = 1; for (int i = 2; i <= n; i++) { result *= i; } return result; } private void testAddAndCheckLongFailure(long a, long b) { try { ArithmeticUtils.addAndCheck(a, b); Assert.fail("Expecting MathArithmeticException"); } catch (MathArithmeticException ex) { // success } } private void testMulAndCheckLongFailure(long a, long b) { try { ArithmeticUtils.mulAndCheck(a, b); Assert.fail("Expecting MathArithmeticException"); } catch (MathArithmeticException ex) { // success } } private void testSubAndCheckLongFailure(long a, long b) { try { ArithmeticUtils.subAndCheck(a, b); Assert.fail("Expecting MathArithmeticException"); } catch (MathArithmeticException ex) { // success } } } commons-math3-3.2-src/src/test/java/org/apache/commons/math3/util/BigRealTest.java100644 1750 1750 16205 12126627675 26576 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.util; import java.math.BigDecimal; import java.math.BigInteger; import java.math.MathContext; import org.apache.commons.math3.TestUtils; import org.apache.commons.math3.exception.MathArithmeticException; import org.junit.Assert; import org.junit.Test; public class BigRealTest { @Test public void testConstructor() { Assert.assertEquals(1.625, new BigReal(new BigDecimal("1.625")).doubleValue(), 1.0e-15); Assert.assertEquals(-5.0, new BigReal(new BigInteger("-5")).doubleValue(), 1.0e-15); Assert.assertEquals(-5.0, new BigReal(new BigInteger("-5"), MathContext.DECIMAL64) .doubleValue(), 1.0e-15); Assert .assertEquals(0.125, new BigReal(new BigInteger("125"), 3).doubleValue(), 1.0e-15); Assert.assertEquals(0.125, new BigReal(new BigInteger("125"), 3, MathContext.DECIMAL64) .doubleValue(), 1.0e-15); Assert.assertEquals(1.625, new BigReal(new char[] { '1', '.', '6', '2', '5' }).doubleValue(), 1.0e-15); Assert.assertEquals(1.625, new BigReal(new char[] { 'A', 'A', '1', '.', '6', '2', '5', '9' }, 2, 5).doubleValue(), 1.0e-15); Assert.assertEquals(1.625, new BigReal(new char[] { 'A', 'A', '1', '.', '6', '2', '5', '9' }, 2, 5, MathContext.DECIMAL64).doubleValue(), 1.0e-15); Assert.assertEquals(1.625, new BigReal(new char[] { '1', '.', '6', '2', '5' }, MathContext.DECIMAL64).doubleValue(), 1.0e-15); Assert.assertEquals(1.625, new BigReal(1.625).doubleValue(), 1.0e-15); Assert.assertEquals(1.625, new BigReal(1.625, MathContext.DECIMAL64) .doubleValue(), 1.0e-15); Assert.assertEquals(-5.0, new BigReal(-5).doubleValue(), 1.0e-15); Assert.assertEquals(-5.0, new BigReal(-5, MathContext.DECIMAL64) .doubleValue(), 1.0e-15); Assert.assertEquals(-5.0, new BigReal(-5l).doubleValue(), 1.0e-15); Assert.assertEquals(-5.0, new BigReal(-5l, MathContext.DECIMAL64) .doubleValue(), 1.0e-15); Assert.assertEquals(1.625, new BigReal("1.625").doubleValue(), 1.0e-15); Assert.assertEquals(1.625, new BigReal("1.625", MathContext.DECIMAL64) .doubleValue(), 1.0e-15); } @Test public void testCompareTo() { BigReal first = new BigReal(1.0 / 2.0); BigReal second = new BigReal(1.0 / 3.0); BigReal third = new BigReal(1.0 / 2.0); Assert.assertEquals(0, first.compareTo(first)); Assert.assertEquals(0, first.compareTo(third)); Assert.assertEquals(1, first.compareTo(second)); Assert.assertEquals(-1, second.compareTo(first)); } @Test public void testAdd() { BigReal a = new BigReal("1.2345678"); BigReal b = new BigReal("8.7654321"); Assert.assertEquals(9.9999999, a.add(b).doubleValue(), 1.0e-15); } @Test public void testSubtract() { BigReal a = new BigReal("1.2345678"); BigReal b = new BigReal("8.7654321"); Assert.assertEquals(-7.5308643, a.subtract(b).doubleValue(), 1.0e-15); } @Test public void testNegate() { BigReal a = new BigReal("1.2345678"); BigReal zero = new BigReal("0.0000000"); Assert.assertEquals(a.negate().add(a), zero); Assert.assertEquals(a.add(a.negate()), zero); Assert.assertEquals(zero, zero.negate()); } @Test public void testDivide() { BigReal a = new BigReal("1.0000000000"); BigReal b = new BigReal("0.0009765625"); Assert.assertEquals(1024.0, a.divide(b).doubleValue(), 1.0e-15); } @Test(expected = MathArithmeticException.class) public void testDivisionByZero() { final BigReal a = BigReal.ONE; final BigReal b = BigReal.ZERO; a.divide(b); } @Test public void testReciprocal() { BigReal a = new BigReal("1.2345678"); double eps = FastMath.pow(10., -a.getScale()); BigReal one = new BigReal("1.0000000"); BigReal b = a.reciprocal(); BigReal r = one.subtract(a.multiply(b)); Assert.assertTrue(FastMath.abs(r.doubleValue()) <= eps); r = one.subtract(b.multiply(a)); Assert.assertTrue(FastMath.abs(r.doubleValue()) <= eps); } @Test(expected = MathArithmeticException.class) public void testReciprocalOfZero() { BigReal.ZERO.reciprocal(); } @Test public void testMultiply() { BigReal a = new BigReal("1024.0"); BigReal b = new BigReal("0.0009765625"); Assert.assertEquals(1.0, a.multiply(b).doubleValue(), 1.0e-15); int n = 1024; Assert.assertEquals(1.0, b.multiply(n).doubleValue(), 1.0e-15); } @Test public void testDoubleValue() { Assert.assertEquals(0.5, new BigReal(0.5).doubleValue(), 1.0e-15); } @Test public void testBigDecimalValue() { BigDecimal pi = new BigDecimal( "3.1415926535897932384626433832795028841971693993751"); Assert.assertEquals(pi, new BigReal(pi).bigDecimalValue()); Assert.assertEquals(new BigDecimal(0.5), new BigReal(1.0 / 2.0).bigDecimalValue()); } @Test public void testEqualsAndHashCode() { BigReal zero = new BigReal(0.0); BigReal nullReal = null; Assert.assertTrue(zero.equals(zero)); Assert.assertFalse(zero.equals(nullReal)); Assert.assertFalse(zero.equals(Double.valueOf(0))); BigReal zero2 = new BigReal(0.0); Assert.assertTrue(zero.equals(zero2)); Assert.assertEquals(zero.hashCode(), zero2.hashCode()); BigReal one = new BigReal(1.0); Assert.assertFalse((one.equals(zero) || zero.equals(one))); Assert.assertTrue(one.equals(BigReal.ONE)); } @Test public void testSerial() { BigReal[] Reals = { new BigReal(3.0), BigReal.ONE, BigReal.ZERO, new BigReal(17), new BigReal(FastMath.PI), new BigReal(-2.5) }; for (BigReal Real : Reals) { Assert.assertEquals(Real, TestUtils.serializeAndRecover(Real)); } } } commons-math3-3.2-src/src/test/java/org/apache/commons/math3/util/MathArraysTest.java100644 1750 1750 113533 12126627675 27366 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with this * work for additional information regarding copyright ownership. The ASF * licenses this file to You 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. */ package org.apache.commons.math3.util; import java.util.Arrays; import org.apache.commons.math3.TestUtils; import org.apache.commons.math3.exception.DimensionMismatchException; import org.apache.commons.math3.exception.MathArithmeticException; import org.apache.commons.math3.exception.MathIllegalArgumentException; import org.apache.commons.math3.exception.NonMonotonicSequenceException; import org.apache.commons.math3.exception.NotPositiveException; import org.apache.commons.math3.exception.NotStrictlyPositiveException; import org.apache.commons.math3.exception.NullArgumentException; import org.apache.commons.math3.random.Well1024a; import org.junit.Assert; import org.junit.Test; /** * Test cases for the {@link MathArrays} class. * * @version $Id$ */ public class MathArraysTest { @Test public void testScale() { final double[] test = new double[] { -2.5, -1, 0, 1, 2.5 }; final double[] correctTest = MathArrays.copyOf(test); final double[] correctScaled = new double[]{5.25, 2.1, 0, -2.1, -5.25}; final double[] scaled = MathArrays.scale(-2.1, test); // Make sure test has not changed for (int i = 0; i < test.length; i++) { Assert.assertEquals(correctTest[i], test[i], 0); } // Test scaled values for (int i = 0; i < scaled.length; i++) { Assert.assertEquals(correctScaled[i], scaled[i], 0); } } @Test public void testScaleInPlace() { final double[] test = new double[] { -2.5, -1, 0, 1, 2.5 }; final double[] correctScaled = new double[]{5.25, 2.1, 0, -2.1, -5.25}; MathArrays.scaleInPlace(-2.1, test); // Make sure test has changed for (int i = 0; i < test.length; i++) { Assert.assertEquals(correctScaled[i], test[i], 0); } } @Test(expected=DimensionMismatchException.class) public void testEbeAddPrecondition() { MathArrays.ebeAdd(new double[3], new double[4]); } @Test(expected=DimensionMismatchException.class) public void testEbeSubtractPrecondition() { MathArrays.ebeSubtract(new double[3], new double[4]); } @Test(expected=DimensionMismatchException.class) public void testEbeMultiplyPrecondition() { MathArrays.ebeMultiply(new double[3], new double[4]); } @Test(expected=DimensionMismatchException.class) public void testEbeDividePrecondition() { MathArrays.ebeDivide(new double[3], new double[4]); } @Test public void testEbeAdd() { final double[] a = { 0, 1, 2 }; final double[] b = { 3, 5, 7 }; final double[] r = MathArrays.ebeAdd(a, b); for (int i = 0; i < a.length; i++) { Assert.assertEquals(a[i] + b[i], r[i], 0); } } @Test public void testEbeSubtract() { final double[] a = { 0, 1, 2 }; final double[] b = { 3, 5, 7 }; final double[] r = MathArrays.ebeSubtract(a, b); for (int i = 0; i < a.length; i++) { Assert.assertEquals(a[i] - b[i], r[i], 0); } } @Test public void testEbeMultiply() { final double[] a = { 0, 1, 2 }; final double[] b = { 3, 5, 7 }; final double[] r = MathArrays.ebeMultiply(a, b); for (int i = 0; i < a.length; i++) { Assert.assertEquals(a[i] * b[i], r[i], 0); } } @Test public void testEbeDivide() { final double[] a = { 0, 1, 2 }; final double[] b = { 3, 5, 7 }; final double[] r = MathArrays.ebeDivide(a, b); for (int i = 0; i < a.length; i++) { Assert.assertEquals(a[i] / b[i], r[i], 0); } } @Test public void testL1DistanceDouble() { double[] p1 = { 2.5, 0.0 }; double[] p2 = { -0.5, 4.0 }; Assert.assertTrue(Precision.equals(7.0, MathArrays.distance1(p1, p2), 1)); } @Test public void testL1DistanceInt() { int[] p1 = { 3, 0 }; int[] p2 = { 0, 4 }; Assert.assertEquals(7, MathArrays.distance1(p1, p2)); } @Test public void testL2DistanceDouble() { double[] p1 = { 2.5, 0.0 }; double[] p2 = { -0.5, 4.0 }; Assert.assertTrue(Precision.equals(5.0, MathArrays.distance(p1, p2), 1)); } @Test public void testL2DistanceInt() { int[] p1 = { 3, 0 }; int[] p2 = { 0, 4 }; Assert.assertTrue(Precision.equals(5, MathArrays.distance(p1, p2), 1)); } @Test public void testLInfDistanceDouble() { double[] p1 = { 2.5, 0.0 }; double[] p2 = { -0.5, 4.0 }; Assert.assertTrue(Precision.equals(4.0, MathArrays.distanceInf(p1, p2), 1)); } @Test public void testLInfDistanceInt() { int[] p1 = { 3, 0 }; int[] p2 = { 0, 4 }; Assert.assertEquals(4, MathArrays.distanceInf(p1, p2)); } @Test public void testCheckOrder() { MathArrays.checkOrder(new double[] {-15, -5.5, -1, 2, 15}, MathArrays.OrderDirection.INCREASING, true); MathArrays.checkOrder(new double[] {-15, -5.5, -1, 2, 2}, MathArrays.OrderDirection.INCREASING, false); MathArrays.checkOrder(new double[] {3, -5.5, -11, -27.5}, MathArrays.OrderDirection.DECREASING, true); MathArrays.checkOrder(new double[] {3, 0, 0, -5.5, -11, -27.5}, MathArrays.OrderDirection.DECREASING, false); try { MathArrays.checkOrder(new double[] {-15, -5.5, -1, -1, 2, 15}, MathArrays.OrderDirection.INCREASING, true); Assert.fail("an exception should have been thrown"); } catch (NonMonotonicSequenceException e) { // Expected } try { MathArrays.checkOrder(new double[] {-15, -5.5, -1, -2, 2}, MathArrays.OrderDirection.INCREASING, false); Assert.fail("an exception should have been thrown"); } catch (NonMonotonicSequenceException e) { // Expected } try { MathArrays.checkOrder(new double[] {3, 3, -5.5, -11, -27.5}, MathArrays.OrderDirection.DECREASING, true); Assert.fail("an exception should have been thrown"); } catch (NonMonotonicSequenceException e) { // Expected } try { MathArrays.checkOrder(new double[] {3, -1, 0, -5.5, -11, -27.5}, MathArrays.OrderDirection.DECREASING, false); Assert.fail("an exception should have been thrown"); } catch (NonMonotonicSequenceException e) { // Expected } try { MathArrays.checkOrder(new double[] {3, 0, -5.5, -11, -10}, MathArrays.OrderDirection.DECREASING, false); Assert.fail("an exception should have been thrown"); } catch (NonMonotonicSequenceException e) { // Expected } } @Test public void testIsMonotonic() { Assert.assertFalse(MathArrays.isMonotonic(new double[] { -15, -5.5, -1, -1, 2, 15 }, MathArrays.OrderDirection.INCREASING, true)); Assert.assertTrue(MathArrays.isMonotonic(new double[] { -15, -5.5, -1, 0, 2, 15 }, MathArrays.OrderDirection.INCREASING, true)); Assert.assertFalse(MathArrays.isMonotonic(new double[] { -15, -5.5, -1, -2, 2 }, MathArrays.OrderDirection.INCREASING, false)); Assert.assertTrue(MathArrays.isMonotonic(new double[] { -15, -5.5, -1, -1, 2 }, MathArrays.OrderDirection.INCREASING, false)); Assert.assertFalse(MathArrays.isMonotonic(new double[] { 3, 3, -5.5, -11, -27.5 }, MathArrays.OrderDirection.DECREASING, true)); Assert.assertTrue(MathArrays.isMonotonic(new double[] { 3, 2, -5.5, -11, -27.5 }, MathArrays.OrderDirection.DECREASING, true)); Assert.assertFalse(MathArrays.isMonotonic(new double[] { 3, -1, 0, -5.5, -11, -27.5 }, MathArrays.OrderDirection.DECREASING, false)); Assert.assertTrue(MathArrays.isMonotonic(new double[] { 3, 0, 0, -5.5, -11, -27.5 }, MathArrays.OrderDirection.DECREASING, false)); } @Test public void testIsMonotonicComparable() { Assert.assertFalse(MathArrays.isMonotonic(new Double[] { new Double(-15), new Double(-5.5), new Double(-1), new Double(-1), new Double(2), new Double(15) }, MathArrays.OrderDirection.INCREASING, true)); Assert.assertTrue(MathArrays.isMonotonic(new Double[] { new Double(-15), new Double(-5.5), new Double(-1), new Double(0), new Double(2), new Double(15) }, MathArrays.OrderDirection.INCREASING, true)); Assert.assertFalse(MathArrays.isMonotonic(new Double[] { new Double(-15), new Double(-5.5), new Double(-1), new Double(-2), new Double(2) }, MathArrays.OrderDirection.INCREASING, false)); Assert.assertTrue(MathArrays.isMonotonic(new Double[] { new Double(-15), new Double(-5.5), new Double(-1), new Double(-1), new Double(2) }, MathArrays.OrderDirection.INCREASING, false)); Assert.assertFalse(MathArrays.isMonotonic(new Double[] { new Double(3), new Double(3), new Double(-5.5), new Double(-11), new Double(-27.5) }, MathArrays.OrderDirection.DECREASING, true)); Assert.assertTrue(MathArrays.isMonotonic(new Double[] { new Double(3), new Double(2), new Double(-5.5), new Double(-11), new Double(-27.5) }, MathArrays.OrderDirection.DECREASING, true)); Assert.assertFalse(MathArrays.isMonotonic(new Double[] { new Double(3), new Double(-1), new Double(0), new Double(-5.5), new Double(-11), new Double(-27.5) }, MathArrays.OrderDirection.DECREASING, false)); Assert.assertTrue(MathArrays.isMonotonic(new Double[] { new Double(3), new Double(0), new Double(0), new Double(-5.5), new Double(-11), new Double(-27.5) }, MathArrays.OrderDirection.DECREASING, false)); } @Test public void testCheckRectangular() { final long[][] rect = new long[][] {{0, 1}, {2, 3}}; final long[][] ragged = new long[][] {{0, 1}, {2}}; final long[][] nullArray = null; final long[][] empty = new long[][] {}; MathArrays.checkRectangular(rect); MathArrays.checkRectangular(empty); try { MathArrays.checkRectangular(ragged); Assert.fail("Expecting DimensionMismatchException"); } catch (DimensionMismatchException ex) { // Expected } try { MathArrays.checkRectangular(nullArray); Assert.fail("Expecting NullArgumentException"); } catch (NullArgumentException ex) { // Expected } } @Test public void testCheckPositive() { final double[] positive = new double[] {1, 2, 3}; final double[] nonNegative = new double[] {0, 1, 2}; final double[] nullArray = null; final double[] empty = new double[] {}; MathArrays.checkPositive(positive); MathArrays.checkPositive(empty); try { MathArrays.checkPositive(nullArray); Assert.fail("Expecting NullPointerException"); } catch (NullPointerException ex) { // Expected } try { MathArrays.checkPositive(nonNegative); Assert.fail("Expecting NotStrictlyPositiveException"); } catch (NotStrictlyPositiveException ex) { // Expected } } @Test public void testCheckNonNegative() { final long[] nonNegative = new long[] {0, 1}; final long[] hasNegative = new long[] {-1}; final long[] nullArray = null; final long[] empty = new long[] {}; MathArrays.checkNonNegative(nonNegative); MathArrays.checkNonNegative(empty); try { MathArrays.checkNonNegative(nullArray); Assert.fail("Expecting NullPointerException"); } catch (NullPointerException ex) { // Expected } try { MathArrays.checkNonNegative(hasNegative); Assert.fail("Expecting NotPositiveException"); } catch (NotPositiveException ex) { // Expected } } @Test public void testCheckNonNegative2D() { final long[][] nonNegative = new long[][] {{0, 1}, {1, 0}}; final long[][] hasNegative = new long[][] {{-1}, {0}}; final long[][] nullArray = null; final long[][] empty = new long[][] {}; MathArrays.checkNonNegative(nonNegative); MathArrays.checkNonNegative(empty); try { MathArrays.checkNonNegative(nullArray); Assert.fail("Expecting NullPointerException"); } catch (NullPointerException ex) { // Expected } try { MathArrays.checkNonNegative(hasNegative); Assert.fail("Expecting NotPositiveException"); } catch (NotPositiveException ex) { // Expected } } @Test public void testSortInPlace() { final double[] x1 = {2, 5, -3, 1, 4}; final double[] x2 = {4, 25, 9, 1, 16}; final double[] x3 = {8, 125, -27, 1, 64}; MathArrays.sortInPlace(x1, x2, x3); Assert.assertEquals(-3, x1[0], Math.ulp(1d)); Assert.assertEquals(9, x2[0], Math.ulp(1d)); Assert.assertEquals(-27, x3[0], Math.ulp(1d)); Assert.assertEquals(1, x1[1], Math.ulp(1d)); Assert.assertEquals(1, x2[1], Math.ulp(1d)); Assert.assertEquals(1, x3[1], Math.ulp(1d)); Assert.assertEquals(2, x1[2], Math.ulp(1d)); Assert.assertEquals(4, x2[2], Math.ulp(1d)); Assert.assertEquals(8, x3[2], Math.ulp(1d)); Assert.assertEquals(4, x1[3], Math.ulp(1d)); Assert.assertEquals(16, x2[3], Math.ulp(1d)); Assert.assertEquals(64, x3[3], Math.ulp(1d)); Assert.assertEquals(5, x1[4], Math.ulp(1d)); Assert.assertEquals(25, x2[4], Math.ulp(1d)); Assert.assertEquals(125, x3[4], Math.ulp(1d)); } @Test /** Example in javadoc */ public void testSortInPlaceExample() { final double[] x = {3, 1, 2}; final double[] y = {1, 2, 3}; final double[] z = {0, 5, 7}; MathArrays.sortInPlace(x, y, z); final double[] sx = {1, 2, 3}; final double[] sy = {2, 3, 1}; final double[] sz = {5, 7, 0}; Assert.assertTrue(Arrays.equals(sx, x)); Assert.assertTrue(Arrays.equals(sy, y)); Assert.assertTrue(Arrays.equals(sz, z)); } @Test public void testSortInPlaceFailures() { final double[] nullArray = null; final double[] one = {1}; final double[] two = {1, 2}; final double[] onep = {2}; try { MathArrays.sortInPlace(one, two); Assert.fail("Expecting DimensionMismatchException"); } catch (DimensionMismatchException ex) { // expected } try { MathArrays.sortInPlace(one, nullArray); Assert.fail("Expecting NullArgumentException"); } catch (NullArgumentException ex) { // expected } try { MathArrays.sortInPlace(one, onep, nullArray); Assert.fail("Expecting NullArgumentException"); } catch (NullArgumentException ex) { // expected } } @Test public void testCopyOfInt() { final int[] source = { Integer.MIN_VALUE, -1, 0, 1, 3, 113, 4769, Integer.MAX_VALUE }; final int[] dest = MathArrays.copyOf(source); Assert.assertEquals(dest.length, source.length); for (int i = 0; i < source.length; i++) { Assert.assertEquals(source[i], dest[i]); } } @Test public void testCopyOfInt2() { final int[] source = { Integer.MIN_VALUE, -1, 0, 1, 3, 113, 4769, Integer.MAX_VALUE }; final int offset = 3; final int[] dest = MathArrays.copyOf(source, source.length - offset); Assert.assertEquals(dest.length, source.length - offset); for (int i = 0; i < source.length - offset; i++) { Assert.assertEquals(source[i], dest[i]); } } @Test public void testCopyOfInt3() { final int[] source = { Integer.MIN_VALUE, -1, 0, 1, 3, 113, 4769, Integer.MAX_VALUE }; final int offset = 3; final int[] dest = MathArrays.copyOf(source, source.length + offset); Assert.assertEquals(dest.length, source.length + offset); for (int i = 0; i < source.length; i++) { Assert.assertEquals(source[i], dest[i]); } for (int i = source.length; i < source.length + offset; i++) { Assert.assertEquals(0, dest[i], 0); } } @Test public void testCopyOfDouble() { final double[] source = { Double.NEGATIVE_INFINITY, -Double.MAX_VALUE, -1, 0, Double.MIN_VALUE, Math.ulp(1d), 1, 3, 113, 4769, Double.MAX_VALUE, Double.POSITIVE_INFINITY }; final double[] dest = MathArrays.copyOf(source); Assert.assertEquals(dest.length, source.length); for (int i = 0; i < source.length; i++) { Assert.assertEquals(source[i], dest[i], 0); } } @Test public void testCopyOfDouble2() { final double[] source = { Double.NEGATIVE_INFINITY, -Double.MAX_VALUE, -1, 0, Double.MIN_VALUE, Math.ulp(1d), 1, 3, 113, 4769, Double.MAX_VALUE, Double.POSITIVE_INFINITY }; final int offset = 3; final double[] dest = MathArrays.copyOf(source, source.length - offset); Assert.assertEquals(dest.length, source.length - offset); for (int i = 0; i < source.length - offset; i++) { Assert.assertEquals(source[i], dest[i], 0); } } @Test public void testCopyOfDouble3() { final double[] source = { Double.NEGATIVE_INFINITY, -Double.MAX_VALUE, -1, 0, Double.MIN_VALUE, Math.ulp(1d), 1, 3, 113, 4769, Double.MAX_VALUE, Double.POSITIVE_INFINITY }; final int offset = 3; final double[] dest = MathArrays.copyOf(source, source.length + offset); Assert.assertEquals(dest.length, source.length + offset); for (int i = 0; i < source.length; i++) { Assert.assertEquals(source[i], dest[i], 0); } for (int i = source.length; i < source.length + offset; i++) { Assert.assertEquals(0, dest[i], 0); } } @Test public void testLinearCombination1() { final double[] a = new double[] { -1321008684645961.0 / 268435456.0, -5774608829631843.0 / 268435456.0, -7645843051051357.0 / 8589934592.0 }; final double[] b = new double[] { -5712344449280879.0 / 2097152.0, -4550117129121957.0 / 2097152.0, 8846951984510141.0 / 131072.0 }; final double abSumInline = MathArrays.linearCombination(a[0], b[0], a[1], b[1], a[2], b[2]); final double abSumArray = MathArrays.linearCombination(a, b); Assert.assertEquals(abSumInline, abSumArray, 0); Assert.assertEquals(-1.8551294182586248737720779899, abSumInline, 1.0e-15); final double naive = a[0] * b[0] + a[1] * b[1] + a[2] * b[2]; Assert.assertTrue(FastMath.abs(naive - abSumInline) > 1.5); } @Test public void testLinearCombination2() { // we compare accurate versus naive dot product implementations // on regular vectors (i.e. not extreme cases like in the previous test) Well1024a random = new Well1024a(553267312521321234l); for (int i = 0; i < 10000; ++i) { final double ux = 1e17 * random.nextDouble(); final double uy = 1e17 * random.nextDouble(); final double uz = 1e17 * random.nextDouble(); final double vx = 1e17 * random.nextDouble(); final double vy = 1e17 * random.nextDouble(); final double vz = 1e17 * random.nextDouble(); final double sInline = MathArrays.linearCombination(ux, vx, uy, vy, uz, vz); final double sArray = MathArrays.linearCombination(new double[] {ux, uy, uz}, new double[] {vx, vy, vz}); Assert.assertEquals(sInline, sArray, 0); } } @Test public void testLinearCombinationInfinite() { final double[][] a = new double[][] { { 1, 2, 3, 4}, { 1, Double.POSITIVE_INFINITY, 3, 4}, { 1, 2, Double.POSITIVE_INFINITY, 4}, { 1, Double.POSITIVE_INFINITY, 3, Double.NEGATIVE_INFINITY}, { 1, 2, 3, 4}, { 1, 2, 3, 4}, { 1, 2, 3, 4}, { 1, 2, 3, 4} }; final double[][] b = new double[][] { { 1, -2, 3, 4}, { 1, -2, 3, 4}, { 1, -2, 3, 4}, { 1, -2, 3, 4}, { 1, Double.POSITIVE_INFINITY, 3, 4}, { 1, -2, Double.POSITIVE_INFINITY, 4}, { 1, Double.POSITIVE_INFINITY, 3, Double.NEGATIVE_INFINITY}, { Double.NaN, -2, 3, 4} }; Assert.assertEquals(-3, MathArrays.linearCombination(a[0][0], b[0][0], a[0][1], b[0][1]), 1.0e-10); Assert.assertEquals(6, MathArrays.linearCombination(a[0][0], b[0][0], a[0][1], b[0][1], a[0][2], b[0][2]), 1.0e-10); Assert.assertEquals(22, MathArrays.linearCombination(a[0][0], b[0][0], a[0][1], b[0][1], a[0][2], b[0][2], a[0][3], b[0][3]), 1.0e-10); Assert.assertEquals(22, MathArrays.linearCombination(a[0], b[0]), 1.0e-10); Assert.assertEquals(Double.NEGATIVE_INFINITY, MathArrays.linearCombination(a[1][0], b[1][0], a[1][1], b[1][1]), 1.0e-10); Assert.assertEquals(Double.NEGATIVE_INFINITY, MathArrays.linearCombination(a[1][0], b[1][0], a[1][1], b[1][1], a[1][2], b[1][2]), 1.0e-10); Assert.assertEquals(Double.NEGATIVE_INFINITY, MathArrays.linearCombination(a[1][0], b[1][0], a[1][1], b[1][1], a[1][2], b[1][2], a[1][3], b[1][3]), 1.0e-10); Assert.assertEquals(Double.NEGATIVE_INFINITY, MathArrays.linearCombination(a[1], b[1]), 1.0e-10); Assert.assertEquals(-3, MathArrays.linearCombination(a[2][0], b[2][0], a[2][1], b[2][1]), 1.0e-10); Assert.assertEquals(Double.POSITIVE_INFINITY, MathArrays.linearCombination(a[2][0], b[2][0], a[2][1], b[2][1], a[2][2], b[2][2]), 1.0e-10); Assert.assertEquals(Double.POSITIVE_INFINITY, MathArrays.linearCombination(a[2][0], b[2][0], a[2][1], b[2][1], a[2][2], b[2][2], a[2][3], b[2][3]), 1.0e-10); Assert.assertEquals(Double.POSITIVE_INFINITY, MathArrays.linearCombination(a[2], b[2]), 1.0e-10); Assert.assertEquals(Double.NEGATIVE_INFINITY, MathArrays.linearCombination(a[3][0], b[3][0], a[3][1], b[3][1]), 1.0e-10); Assert.assertEquals(Double.NEGATIVE_INFINITY, MathArrays.linearCombination(a[3][0], b[3][0], a[3][1], b[3][1], a[3][2], b[3][2]), 1.0e-10); Assert.assertEquals(Double.NEGATIVE_INFINITY, MathArrays.linearCombination(a[3][0], b[3][0], a[3][1], b[3][1], a[3][2], b[3][2], a[3][3], b[3][3]), 1.0e-10); Assert.assertEquals(Double.NEGATIVE_INFINITY, MathArrays.linearCombination(a[3], b[3]), 1.0e-10); Assert.assertEquals(Double.POSITIVE_INFINITY, MathArrays.linearCombination(a[4][0], b[4][0], a[4][1], b[4][1]), 1.0e-10); Assert.assertEquals(Double.POSITIVE_INFINITY, MathArrays.linearCombination(a[4][0], b[4][0], a[4][1], b[4][1], a[4][2], b[4][2]), 1.0e-10); Assert.assertEquals(Double.POSITIVE_INFINITY, MathArrays.linearCombination(a[4][0], b[4][0], a[4][1], b[4][1], a[4][2], b[4][2], a[4][3], b[4][3]), 1.0e-10); Assert.assertEquals(Double.POSITIVE_INFINITY, MathArrays.linearCombination(a[4], b[4]), 1.0e-10); Assert.assertEquals(-3, MathArrays.linearCombination(a[5][0], b[5][0], a[5][1], b[5][1]), 1.0e-10); Assert.assertEquals(Double.POSITIVE_INFINITY, MathArrays.linearCombination(a[5][0], b[5][0], a[5][1], b[5][1], a[5][2], b[5][2]), 1.0e-10); Assert.assertEquals(Double.POSITIVE_INFINITY, MathArrays.linearCombination(a[5][0], b[5][0], a[5][1], b[5][1], a[5][2], b[5][2], a[5][3], b[5][3]), 1.0e-10); Assert.assertEquals(Double.POSITIVE_INFINITY, MathArrays.linearCombination(a[5], b[5]), 1.0e-10); Assert.assertEquals(Double.POSITIVE_INFINITY, MathArrays.linearCombination(a[6][0], b[6][0], a[6][1], b[6][1]), 1.0e-10); Assert.assertEquals(Double.POSITIVE_INFINITY, MathArrays.linearCombination(a[6][0], b[6][0], a[6][1], b[6][1], a[6][2], b[6][2]), 1.0e-10); Assert.assertTrue(Double.isNaN(MathArrays.linearCombination(a[6][0], b[6][0], a[6][1], b[6][1], a[6][2], b[6][2], a[6][3], b[6][3]))); Assert.assertTrue(Double.isNaN(MathArrays.linearCombination(a[6], b[6]))); Assert.assertTrue(Double.isNaN(MathArrays.linearCombination(a[7][0], b[7][0], a[7][1], b[7][1]))); Assert.assertTrue(Double.isNaN(MathArrays.linearCombination(a[7][0], b[7][0], a[7][1], b[7][1], a[7][2], b[7][2]))); Assert.assertTrue(Double.isNaN(MathArrays.linearCombination(a[7][0], b[7][0], a[7][1], b[7][1], a[7][2], b[7][2], a[7][3], b[7][3]))); Assert.assertTrue(Double.isNaN(MathArrays.linearCombination(a[7], b[7]))); } @Test public void testArrayEquals() { Assert.assertFalse(MathArrays.equals(new double[] { 1d }, null)); Assert.assertFalse(MathArrays.equals(null, new double[] { 1d })); Assert.assertTrue(MathArrays.equals((double[]) null, (double[]) null)); Assert.assertFalse(MathArrays.equals(new double[] { 1d }, new double[0])); Assert.assertTrue(MathArrays.equals(new double[] { 1d }, new double[] { 1d })); Assert.assertTrue(MathArrays.equals(new double[] { Double.POSITIVE_INFINITY, Double.NEGATIVE_INFINITY, 1d, 0d }, new double[] { Double.POSITIVE_INFINITY, Double.NEGATIVE_INFINITY, 1d, 0d })); Assert.assertFalse(MathArrays.equals(new double[] { Double.NaN }, new double[] { Double.NaN })); Assert.assertFalse(MathArrays.equals(new double[] { Double.POSITIVE_INFINITY }, new double[] { Double.NEGATIVE_INFINITY })); Assert.assertFalse(MathArrays.equals(new double[] { 1d }, new double[] { FastMath.nextAfter(FastMath.nextAfter(1d, 2d), 2d) })); } @Test public void testArrayEqualsIncludingNaN() { Assert.assertFalse(MathArrays.equalsIncludingNaN(new double[] { 1d }, null)); Assert.assertFalse(MathArrays.equalsIncludingNaN(null, new double[] { 1d })); Assert.assertTrue(MathArrays.equalsIncludingNaN((double[]) null, (double[]) null)); Assert.assertFalse(MathArrays.equalsIncludingNaN(new double[] { 1d }, new double[0])); Assert.assertTrue(MathArrays.equalsIncludingNaN(new double[] { 1d }, new double[] { 1d })); Assert.assertTrue(MathArrays.equalsIncludingNaN(new double[] { Double.NaN, Double.POSITIVE_INFINITY, Double.NEGATIVE_INFINITY, 1d, 0d }, new double[] { Double.NaN, Double.POSITIVE_INFINITY, Double.NEGATIVE_INFINITY, 1d, 0d })); Assert.assertFalse(MathArrays.equalsIncludingNaN(new double[] { Double.POSITIVE_INFINITY }, new double[] { Double.NEGATIVE_INFINITY })); Assert.assertFalse(MathArrays.equalsIncludingNaN(new double[] { 1d }, new double[] { FastMath.nextAfter(FastMath.nextAfter(1d, 2d), 2d) })); } @Test public void testNormalizeArray() { double[] testValues1 = new double[] {1, 1, 2}; TestUtils.assertEquals( new double[] {.25, .25, .5}, MathArrays.normalizeArray(testValues1, 1), Double.MIN_VALUE); double[] testValues2 = new double[] {-1, -1, 1}; TestUtils.assertEquals( new double[] {1, 1, -1}, MathArrays.normalizeArray(testValues2, 1), Double.MIN_VALUE); // Ignore NaNs double[] testValues3 = new double[] {-1, -1, Double.NaN, 1, Double.NaN}; TestUtils.assertEquals( new double[] {1, 1,Double.NaN, -1, Double.NaN}, MathArrays.normalizeArray(testValues3, 1), Double.MIN_VALUE); // Zero sum -> MathArithmeticException double[] zeroSum = new double[] {-1, 1}; try { MathArrays.normalizeArray(zeroSum, 1); Assert.fail("expecting MathArithmeticException"); } catch (MathArithmeticException ex) {} // Infinite elements -> MathArithmeticException double[] hasInf = new double[] {1, 2, 1, Double.NEGATIVE_INFINITY}; try { MathArrays.normalizeArray(hasInf, 1); Assert.fail("expecting MathIllegalArgumentException"); } catch (MathIllegalArgumentException ex) {} // Infinite target -> MathIllegalArgumentException try { MathArrays.normalizeArray(testValues1, Double.POSITIVE_INFINITY); Assert.fail("expecting MathIllegalArgumentException"); } catch (MathIllegalArgumentException ex) {} // NaN target -> MathIllegalArgumentException try { MathArrays.normalizeArray(testValues1, Double.NaN); Assert.fail("expecting MathIllegalArgumentException"); } catch (MathIllegalArgumentException ex) {} } } commons-math3-3.2-src/src/test/java/org/apache/commons/math3/util/MultidimensionalCounterTest.java100644 1750 1750 13667 12126627675 32157 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.util; import org.apache.commons.math3.exception.DimensionMismatchException; import org.apache.commons.math3.exception.OutOfRangeException; import org.apache.commons.math3.exception.NotStrictlyPositiveException; import org.junit.Assert; import org.junit.Test; /** * */ public class MultidimensionalCounterTest { @Test public void testPreconditions() { MultidimensionalCounter c; try { c = new MultidimensionalCounter(0, 1); Assert.fail("NotStrictlyPositiveException expected"); } catch (NotStrictlyPositiveException e) { // Expected. } try { c = new MultidimensionalCounter(2, 0); Assert.fail("NotStrictlyPositiveException expected"); } catch (NotStrictlyPositiveException e) { // Expected. } try { c = new MultidimensionalCounter(-1, 1); Assert.fail("NotStrictlyPositiveException expected"); } catch (NotStrictlyPositiveException e) { // Expected. } c = new MultidimensionalCounter(2, 3); try { c.getCount(1, 1, 1); Assert.fail("DimensionMismatchException expected"); } catch (DimensionMismatchException e) { // Expected. } try { c.getCount(3, 1); Assert.fail("OutOfRangeException expected"); } catch (OutOfRangeException e) { // Expected. } try { c.getCount(0, -1); Assert.fail("OutOfRangeException expected"); } catch (OutOfRangeException e) { // Expected. } try { c.getCounts(-1); Assert.fail("OutOfRangeException expected"); } catch (OutOfRangeException e) { // Expected. } try { c.getCounts(6); Assert.fail("OutOfRangeException expected"); } catch (OutOfRangeException e) { // Expected. } } @Test public void testIteratorPreconditions() { MultidimensionalCounter.Iterator iter = (new MultidimensionalCounter(2, 3)).iterator(); try { iter.getCount(-1); Assert.fail("IndexOutOfBoundsException expected"); } catch (IndexOutOfBoundsException e) { // Expected. } try { iter.getCount(2); Assert.fail("IndexOutOfBoundsException expected"); } catch (IndexOutOfBoundsException e) { // Expected. } } @Test public void testMulti2UniConversion() { final MultidimensionalCounter c = new MultidimensionalCounter(2, 4, 5); Assert.assertEquals(c.getCount(1, 2, 3), 33); } @Test public void testAccessors() { final int[] originalSize = new int[] {2, 6, 5}; final MultidimensionalCounter c = new MultidimensionalCounter(originalSize); final int nDim = c.getDimension(); Assert.assertEquals(nDim, originalSize.length); final int[] size = c.getSizes(); for (int i = 0; i < nDim; i++) { Assert.assertEquals(originalSize[i], size[i]); } } @Test public void testIterationConsistency() { final MultidimensionalCounter c = new MultidimensionalCounter(2, 3, 4); final int[][] expected = new int[][] { { 0, 0, 0 }, { 0, 0, 1 }, { 0, 0, 2 }, { 0, 0, 3 }, { 0, 1, 0 }, { 0, 1, 1 }, { 0, 1, 2 }, { 0, 1, 3 }, { 0, 2, 0 }, { 0, 2, 1 }, { 0, 2, 2 }, { 0, 2, 3 }, { 1, 0, 0 }, { 1, 0, 1 }, { 1, 0, 2 }, { 1, 0, 3 }, { 1, 1, 0 }, { 1, 1, 1 }, { 1, 1, 2 }, { 1, 1, 3 }, { 1, 2, 0 }, { 1, 2, 1 }, { 1, 2, 2 }, { 1, 2, 3 } }; final int totalSize = c.getSize(); final int nDim = c.getDimension(); final MultidimensionalCounter.Iterator iter = c.iterator(); for (int i = 0; i < totalSize; i++) { if (!iter.hasNext()) { Assert.fail("Too short"); } final int uniDimIndex = iter.next(); Assert.assertEquals("Wrong iteration at " + i, i, uniDimIndex); for (int dimIndex = 0; dimIndex < nDim; dimIndex++) { Assert.assertEquals("Wrong multidimensional index for [" + i + "][" + dimIndex + "]", expected[i][dimIndex], iter.getCount(dimIndex)); } Assert.assertEquals("Wrong unidimensional index for [" + i + "]", c.getCount(expected[i]), uniDimIndex); final int[] indices = c.getCounts(uniDimIndex); for (int dimIndex = 0; dimIndex < nDim; dimIndex++) { Assert.assertEquals("Wrong multidimensional index for [" + i + "][" + dimIndex + "]", expected[i][dimIndex], indices[dimIndex]); } } if (iter.hasNext()) { Assert.fail("Too long"); } } } commons-math3-3.2-src/src/test/java/org/apache/commons/math3/util/OpenIntToDoubleHashMapTest.java100644 1750 1750 26303 12126627675 31545 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.util; import java.util.ConcurrentModificationException; import java.util.HashMap; import java.util.HashSet; import java.util.Map; import java.util.NoSuchElementException; import java.util.Random; import java.util.Set; import org.junit.Assert; import org.junit.Before; import org.junit.Test; /** * Test cases for the {@link OpenIntToDoubleHashMap}. */ public class OpenIntToDoubleHashMapTest { private Map javaMap = new HashMap(); @Before public void setUp() throws Exception { javaMap.put(50, 100.0); javaMap.put(75, 75.0); javaMap.put(25, 500.0); javaMap.put(Integer.MAX_VALUE, Double.MAX_VALUE); javaMap.put(0, -1.0); javaMap.put(1, 0.0); javaMap.put(33, -0.1); javaMap.put(23234234, -242343.0); javaMap.put(23321, Double.MIN_VALUE); javaMap.put(-4444, 332.0); javaMap.put(-1, -2323.0); javaMap.put(Integer.MIN_VALUE, 44.0); /* Add a few more to cause the table to rehash */ javaMap.putAll(generate()); } private Map generate() { Map map = new HashMap(); Random r = new Random(); for (int i = 0; i < 2000; ++i) map.put(r.nextInt(), r.nextDouble()); return map; } private OpenIntToDoubleHashMap createFromJavaMap() { OpenIntToDoubleHashMap map = new OpenIntToDoubleHashMap(); for (Map.Entry mapEntry : javaMap.entrySet()) { map.put(mapEntry.getKey(), mapEntry.getValue()); } return map; } @Test public void testPutAndGetWith0ExpectedSize() { OpenIntToDoubleHashMap map = new OpenIntToDoubleHashMap(0); assertPutAndGet(map); } @Test public void testPutAndGetWithExpectedSize() { OpenIntToDoubleHashMap map = new OpenIntToDoubleHashMap(500); assertPutAndGet(map); } @Test public void testPutAndGet() { OpenIntToDoubleHashMap map = new OpenIntToDoubleHashMap(); assertPutAndGet(map); } private void assertPutAndGet(OpenIntToDoubleHashMap map) { assertPutAndGet(map, 0, new HashSet()); } private void assertPutAndGet(OpenIntToDoubleHashMap map, int mapSize, Set keysInMap) { Assert.assertEquals(mapSize, map.size()); for (Map.Entry mapEntry : javaMap.entrySet()) { map.put(mapEntry.getKey(), mapEntry.getValue()); if (!keysInMap.contains(mapEntry.getKey())) ++mapSize; Assert.assertEquals(mapSize, map.size()); Assert.assertTrue(Precision.equals(mapEntry.getValue(), map.get(mapEntry.getKey()), 1)); } } @Test public void testPutAbsentOnExisting() { OpenIntToDoubleHashMap map = createFromJavaMap(); int size = javaMap.size(); for (Map.Entry mapEntry : generateAbsent().entrySet()) { map.put(mapEntry.getKey(), mapEntry.getValue()); Assert.assertEquals(++size, map.size()); Assert.assertTrue(Precision.equals(mapEntry.getValue(), map.get(mapEntry.getKey()), 1)); } } @Test public void testPutOnExisting() { OpenIntToDoubleHashMap map = createFromJavaMap(); for (Map.Entry mapEntry : javaMap.entrySet()) { map.put(mapEntry.getKey(), mapEntry.getValue()); Assert.assertEquals(javaMap.size(), map.size()); Assert.assertTrue(Precision.equals(mapEntry.getValue(), map.get(mapEntry.getKey()), 1)); } } @Test public void testGetAbsent() { Map generated = generateAbsent(); OpenIntToDoubleHashMap map = createFromJavaMap(); for (Map.Entry mapEntry : generated.entrySet()) Assert.assertTrue(Double.isNaN(map.get(mapEntry.getKey()))); } @Test public void testGetFromEmpty() { OpenIntToDoubleHashMap map = new OpenIntToDoubleHashMap(); Assert.assertTrue(Double.isNaN(map.get(5))); Assert.assertTrue(Double.isNaN(map.get(0))); Assert.assertTrue(Double.isNaN(map.get(50))); } @Test public void testRemove() { OpenIntToDoubleHashMap map = createFromJavaMap(); int mapSize = javaMap.size(); Assert.assertEquals(mapSize, map.size()); for (Map.Entry mapEntry : javaMap.entrySet()) { map.remove(mapEntry.getKey()); Assert.assertEquals(--mapSize, map.size()); Assert.assertTrue(Double.isNaN(map.get(mapEntry.getKey()))); } /* Ensure that put and get still work correctly after removals */ assertPutAndGet(map); } /* This time only remove some entries */ @Test public void testRemove2() { OpenIntToDoubleHashMap map = createFromJavaMap(); int mapSize = javaMap.size(); int count = 0; Set keysInMap = new HashSet(javaMap.keySet()); for (Map.Entry mapEntry : javaMap.entrySet()) { keysInMap.remove(mapEntry.getKey()); map.remove(mapEntry.getKey()); Assert.assertEquals(--mapSize, map.size()); Assert.assertTrue(Double.isNaN(map.get(mapEntry.getKey()))); if (count++ > 5) break; } /* Ensure that put and get still work correctly after removals */ assertPutAndGet(map, mapSize, keysInMap); } @Test public void testRemoveFromEmpty() { OpenIntToDoubleHashMap map = new OpenIntToDoubleHashMap(); Assert.assertTrue(Double.isNaN(map.remove(50))); } @Test public void testRemoveAbsent() { Map generated = generateAbsent(); OpenIntToDoubleHashMap map = createFromJavaMap(); int mapSize = map.size(); for (Map.Entry mapEntry : generated.entrySet()) { map.remove(mapEntry.getKey()); Assert.assertEquals(mapSize, map.size()); Assert.assertTrue(Double.isNaN(map.get(mapEntry.getKey()))); } } /** * Returns a map with at least 100 elements where each element is absent from javaMap. */ private Map generateAbsent() { Map generated = new HashMap(); do { generated.putAll(generate()); for (Integer key : javaMap.keySet()) generated.remove(key); } while (generated.size() < 100); return generated; } @Test public void testCopy() { OpenIntToDoubleHashMap copy = new OpenIntToDoubleHashMap(createFromJavaMap()); Assert.assertEquals(javaMap.size(), copy.size()); for (Map.Entry mapEntry : javaMap.entrySet()) Assert.assertTrue(Precision.equals(mapEntry.getValue(), copy.get(mapEntry.getKey()), 1)); } @Test public void testContainsKey() { OpenIntToDoubleHashMap map = createFromJavaMap(); for (Map.Entry mapEntry : javaMap.entrySet()) { Assert.assertTrue(map.containsKey(mapEntry.getKey())); } for (Map.Entry mapEntry : generateAbsent().entrySet()) { Assert.assertFalse(map.containsKey(mapEntry.getKey())); } for (Map.Entry mapEntry : javaMap.entrySet()) { int key = mapEntry.getKey(); Assert.assertTrue(map.containsKey(key)); map.remove(key); Assert.assertFalse(map.containsKey(key)); } } @Test public void testIterator() { OpenIntToDoubleHashMap map = createFromJavaMap(); OpenIntToDoubleHashMap.Iterator iterator = map.iterator(); for (int i = 0; i < map.size(); ++i) { Assert.assertTrue(iterator.hasNext()); iterator.advance(); int key = iterator.key(); Assert.assertTrue(map.containsKey(key)); Assert.assertEquals(javaMap.get(key), map.get(key), 0); Assert.assertEquals(javaMap.get(key), iterator.value(), 0); Assert.assertTrue(javaMap.containsKey(key)); } Assert.assertFalse(iterator.hasNext()); try { iterator.advance(); Assert.fail("an exception should have been thrown"); } catch (NoSuchElementException nsee) { // expected } } @Test public void testConcurrentModification() { OpenIntToDoubleHashMap map = createFromJavaMap(); OpenIntToDoubleHashMap.Iterator iterator = map.iterator(); map.put(3, 3); try { iterator.advance(); Assert.fail("an exception should have been thrown"); } catch (ConcurrentModificationException cme) { // expected } } /** * Regression test for a bug in findInsertionIndex where the hashing in the second probing * loop was inconsistent with the first causing duplicate keys after the right sequence * of puts and removes. */ @Test public void testPutKeysWithCollisions() { OpenIntToDoubleHashMap map = new OpenIntToDoubleHashMap(); int key1 = -1996012590; double value1 = 1.0; map.put(key1, value1); int key2 = 835099822; map.put(key2, value1); int key3 = 1008859686; map.put(key3, value1); Assert.assertTrue(Precision.equals(value1, map.get(key3), 1)); Assert.assertEquals(3, map.size()); map.remove(key2); double value2 = 2.0; map.put(key3, value2); Assert.assertTrue(Precision.equals(value2, map.get(key3), 1)); Assert.assertEquals(2, map.size()); } /** * Similar to testPutKeysWithCollisions() but exercises the codepaths in a slightly * different manner. */ @Test public void testPutKeysWithCollision2() { OpenIntToDoubleHashMap map = new OpenIntToDoubleHashMap(); int key1 = 837989881; double value1 = 1.0; map.put(key1, value1); int key2 = 476463321; map.put(key2, value1); Assert.assertEquals(2, map.size()); Assert.assertTrue(Precision.equals(value1, map.get(key2), 1)); map.remove(key1); double value2 = 2.0; map.put(key2, value2); Assert.assertEquals(1, map.size()); Assert.assertTrue(Precision.equals(value2, map.get(key2), 1)); } } commons-math3-3.2-src/src/test/java/org/apache/commons/math3/util/ResizableDoubleArrayTest.java100644 1750 1750 53765 12126627675 31357 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.util; import org.apache.commons.math3.distribution.IntegerDistribution; import org.apache.commons.math3.distribution.UniformIntegerDistribution; import org.junit.After; import org.junit.Assert; import org.junit.Before; import org.junit.Test; /** * This class contains test cases for the ResizableDoubleArray. * * @version $Id: ResizableDoubleArrayTest.java 1410121 2012-11-16 00:18:30Z erans $ */ public class ResizableDoubleArrayTest extends DoubleArrayAbstractTest { @After public void tearDown() throws Exception { da = null; ra = null; } @Before public void setUp() throws Exception { da = new ResizableDoubleArray(); ra = new ResizableDoubleArray(); } @Test public void testConstructors() { float defaultExpansionFactor = 2.0f; float defaultContractionCriteria = 2.5f; int defaultMode = ResizableDoubleArray.MULTIPLICATIVE_MODE; ResizableDoubleArray testDa = new ResizableDoubleArray(2); Assert.assertEquals(0, testDa.getNumElements()); Assert.assertEquals(2, testDa.getCapacity()); Assert.assertEquals(defaultExpansionFactor, testDa.getExpansionFactor(), 0); Assert.assertEquals(defaultContractionCriteria, testDa.getContractionCriteria(), 0); Assert.assertEquals(defaultMode, testDa.getExpansionMode()); try { da = new ResizableDoubleArray(-1); Assert.fail("Expecting IllegalArgumentException"); } catch (IllegalArgumentException ex) { // expected } testDa = new ResizableDoubleArray((double[]) null); Assert.assertEquals(0, testDa.getNumElements()); double[] initialArray = new double[] { 0, 1, 2 }; testDa = new ResizableDoubleArray(initialArray); Assert.assertEquals(3, testDa.getNumElements()); testDa = new ResizableDoubleArray(2, 2.0f); Assert.assertEquals(0, testDa.getNumElements()); Assert.assertEquals(2, testDa.getCapacity()); Assert.assertEquals(defaultExpansionFactor, testDa.getExpansionFactor(), 0); Assert.assertEquals(defaultContractionCriteria, testDa.getContractionCriteria(), 0); Assert.assertEquals(defaultMode, testDa.getExpansionMode()); try { da = new ResizableDoubleArray(2, 0.5f); Assert.fail("Expecting IllegalArgumentException"); } catch (IllegalArgumentException ex) { // expected } testDa = new ResizableDoubleArray(2, 3.0f); Assert.assertEquals(3.0f, testDa.getExpansionFactor(), 0); Assert.assertEquals(3.5f, testDa.getContractionCriteria(), 0); testDa = new ResizableDoubleArray(2, 2.0f, 3.0f); Assert.assertEquals(0, testDa.getNumElements()); Assert.assertEquals(2, testDa.getCapacity()); Assert.assertEquals(defaultExpansionFactor, testDa.getExpansionFactor(), 0); Assert.assertEquals(3.0f, testDa.getContractionCriteria(), 0); Assert.assertEquals(defaultMode, testDa.getExpansionMode()); try { da = new ResizableDoubleArray(2, 2.0f, 1.5f); Assert.fail("Expecting IllegalArgumentException"); } catch (IllegalArgumentException ex) { // expected } testDa = new ResizableDoubleArray(2, 2.0f, 3.0f, ResizableDoubleArray.ADDITIVE_MODE); Assert.assertEquals(0, testDa.getNumElements()); Assert.assertEquals(2, testDa.getCapacity()); Assert.assertEquals(defaultExpansionFactor, testDa.getExpansionFactor(), 0); Assert.assertEquals(3.0f, testDa.getContractionCriteria(), 0); Assert.assertEquals(ResizableDoubleArray.ADDITIVE_MODE, testDa.getExpansionMode()); try { da = new ResizableDoubleArray(2, 2.0f, 2.5f, -1); Assert.fail("Expecting IllegalArgumentException"); } catch (IllegalArgumentException ex) { // expected } // Copy constructor testDa = new ResizableDoubleArray(2, 2.0f, 3.0f, ResizableDoubleArray.ADDITIVE_MODE); testDa.addElement(2.0); testDa.addElement(3.2); ResizableDoubleArray copyDa = new ResizableDoubleArray(testDa); Assert.assertEquals(copyDa, testDa); Assert.assertEquals(testDa, copyDa); } @Test public void testSetElementArbitraryExpansion1() { // MULTIPLICATIVE_MODE da.addElement(2.0); da.addElement(4.0); da.addElement(6.0); da.setElement(1, 3.0); // Expand the array arbitrarily to 1000 items da.setElement(1000, 3.4); Assert.assertEquals( "The number of elements should now be 1001, it isn't", da.getNumElements(), 1001); Assert.assertEquals( "Uninitialized Elements are default value of 0.0, index 766 wasn't", 0.0, da.getElement( 760 ), Double.MIN_VALUE ); Assert.assertEquals( "The 1000th index should be 3.4, it isn't", 3.4, da.getElement(1000), Double.MIN_VALUE ); Assert.assertEquals( "The 0th index should be 2.0, it isn't", 2.0, da.getElement(0), Double.MIN_VALUE); } @Test public void testSetElementArbitraryExpansion2() { // Make sure numElements and expansion work correctly for expansion boundary cases da.addElement(2.0); da.addElement(4.0); da.addElement(6.0); Assert.assertEquals(16, ((ResizableDoubleArray) da).getCapacity()); Assert.assertEquals(3, da.getNumElements()); da.setElement(3, 7.0); Assert.assertEquals(16, ((ResizableDoubleArray) da).getCapacity()); Assert.assertEquals(4, da.getNumElements()); da.setElement(10, 10.0); Assert.assertEquals(16, ((ResizableDoubleArray) da).getCapacity()); Assert.assertEquals(11, da.getNumElements()); da.setElement(9, 10.0); Assert.assertEquals(16, ((ResizableDoubleArray) da).getCapacity()); Assert.assertEquals(11, da.getNumElements()); try { da.setElement(-2, 3); Assert.fail("Expecting ArrayIndexOutOfBoundsException for negative index"); } catch (ArrayIndexOutOfBoundsException ex) { // expected } // ADDITIVE_MODE ResizableDoubleArray testDa = new ResizableDoubleArray(2, 2.0f, 3.0f, ResizableDoubleArray.ADDITIVE_MODE); Assert.assertEquals(2, testDa.getCapacity()); testDa.addElement(1d); testDa.addElement(1d); Assert.assertEquals(2, testDa.getCapacity()); testDa.addElement(1d); Assert.assertEquals(4, testDa.getCapacity()); } @Override @Test public void testAdd1000() { super.testAdd1000(); Assert.assertEquals("Internal Storage length should be 1024 if we started out with initial capacity of " + "16 and an expansion factor of 2.0", 1024, ((ResizableDoubleArray) da).getCapacity()); } @Test public void testAddElements() { ResizableDoubleArray testDa = new ResizableDoubleArray(); // MULTIPLICATIVE_MODE testDa.addElements(new double[] {4, 5, 6}); Assert.assertEquals(3, testDa.getNumElements(), 0); Assert.assertEquals(4, testDa.getElement(0), 0); Assert.assertEquals(5, testDa.getElement(1), 0); Assert.assertEquals(6, testDa.getElement(2), 0); testDa.addElements(new double[] {4, 5, 6}); Assert.assertEquals(6, testDa.getNumElements()); // ADDITIVE_MODE (x's are occupied storage locations, 0's are open) testDa = new ResizableDoubleArray(2, 2.0f, 2.5f, ResizableDoubleArray.ADDITIVE_MODE); Assert.assertEquals(2, testDa.getCapacity()); testDa.addElements(new double[] { 1d }); // x,0 testDa.addElements(new double[] { 2d }); // x,x testDa.addElements(new double[] { 3d }); // x,x,x,0 -- expanded Assert.assertEquals(1d, testDa.getElement(0), 0); Assert.assertEquals(2d, testDa.getElement(1), 0); Assert.assertEquals(3d, testDa.getElement(2), 0); Assert.assertEquals(4, testDa.getCapacity()); // x,x,x,0 Assert.assertEquals(3, testDa.getNumElements()); } @Override @Test public void testAddElementRolling() { super.testAddElementRolling(); // MULTIPLICATIVE_MODE da.clear(); da.addElement(1); da.addElement(2); da.addElementRolling(3); Assert.assertEquals(3, da.getElement(1), 0); da.addElementRolling(4); Assert.assertEquals(3, da.getElement(0), 0); Assert.assertEquals(4, da.getElement(1), 0); da.addElement(5); Assert.assertEquals(5, da.getElement(2), 0); da.addElementRolling(6); Assert.assertEquals(4, da.getElement(0), 0); Assert.assertEquals(5, da.getElement(1), 0); Assert.assertEquals(6, da.getElement(2), 0); // ADDITIVE_MODE (x's are occupied storage locations, 0's are open) ResizableDoubleArray testDa = new ResizableDoubleArray(2, 2.0f, 2.5f, ResizableDoubleArray.ADDITIVE_MODE); Assert.assertEquals(2, testDa.getCapacity()); testDa.addElement(1d); // x,0 testDa.addElement(2d); // x,x testDa.addElement(3d); // x,x,x,0 -- expanded Assert.assertEquals(1d, testDa.getElement(0), 0); Assert.assertEquals(2d, testDa.getElement(1), 0); Assert.assertEquals(3d, testDa.getElement(2), 0); Assert.assertEquals(4, testDa.getCapacity()); // x,x,x,0 Assert.assertEquals(3, testDa.getNumElements()); testDa.addElementRolling(4d); Assert.assertEquals(2d, testDa.getElement(0), 0); Assert.assertEquals(3d, testDa.getElement(1), 0); Assert.assertEquals(4d, testDa.getElement(2), 0); Assert.assertEquals(4, testDa.getCapacity()); // 0,x,x,x Assert.assertEquals(3, testDa.getNumElements()); testDa.addElementRolling(5d); // 0,0,x,x,x,0 -- time to contract Assert.assertEquals(3d, testDa.getElement(0), 0); Assert.assertEquals(4d, testDa.getElement(1), 0); Assert.assertEquals(5d, testDa.getElement(2), 0); Assert.assertEquals(4, testDa.getCapacity()); // contracted -- x,x,x,0 Assert.assertEquals(3, testDa.getNumElements()); try { testDa.getElement(4); Assert.fail("Expecting ArrayIndexOutOfBoundsException"); } catch (ArrayIndexOutOfBoundsException ex) { // expected } try { testDa.getElement(-1); Assert.fail("Expecting ArrayIndexOutOfBoundsException"); } catch (ArrayIndexOutOfBoundsException ex) { // expected } } @Test public void testSetNumberOfElements() { da.addElement( 1.0 ); da.addElement( 1.0 ); da.addElement( 1.0 ); da.addElement( 1.0 ); da.addElement( 1.0 ); da.addElement( 1.0 ); Assert.assertEquals( "Number of elements should equal 6", da.getNumElements(), 6); ((ResizableDoubleArray) da).setNumElements( 3 ); Assert.assertEquals( "Number of elements should equal 3", da.getNumElements(), 3); try { ((ResizableDoubleArray) da).setNumElements( -3 ); Assert.fail( "Setting number of elements to negative should've thrown an exception"); } catch( IllegalArgumentException iae ) { } ((ResizableDoubleArray) da).setNumElements(1024); Assert.assertEquals( "Number of elements should now be 1024", da.getNumElements(), 1024); Assert.assertEquals( "Element 453 should be a default double", da.getElement( 453 ), 0.0, Double.MIN_VALUE); } @Test public void testWithInitialCapacity() { ResizableDoubleArray eDA2 = new ResizableDoubleArray(2); Assert.assertEquals("Initial number of elements should be 0", 0, eDA2.getNumElements()); final IntegerDistribution randomData = new UniformIntegerDistribution(100, 1000); final int iterations = randomData.sample(); for( int i = 0; i < iterations; i++) { eDA2.addElement( i ); } Assert.assertEquals("Number of elements should be equal to " + iterations, iterations, eDA2.getNumElements()); eDA2.addElement( 2.0 ); Assert.assertEquals("Number of elements should be equals to " + (iterations +1), iterations + 1 , eDA2.getNumElements() ); } @Test public void testWithInitialCapacityAndExpansionFactor() { ResizableDoubleArray eDA3 = new ResizableDoubleArray(3, 3.0f, 3.5f); Assert.assertEquals("Initial number of elements should be 0", 0, eDA3.getNumElements() ); final IntegerDistribution randomData = new UniformIntegerDistribution(100, 3000); final int iterations = randomData.sample(); for( int i = 0; i < iterations; i++) { eDA3.addElement( i ); } Assert.assertEquals("Number of elements should be equal to " + iterations, iterations,eDA3.getNumElements()); eDA3.addElement( 2.0 ); Assert.assertEquals("Number of elements should be equals to " + (iterations +1), iterations +1, eDA3.getNumElements() ); Assert.assertEquals("Expansion factor should equal 3.0", 3.0f, eDA3.getExpansionFactor(), Double.MIN_VALUE); } @Test public void testDiscard() { da.addElement(2.0); da.addElement(2.0); da.addElement(2.0); da.addElement(2.0); da.addElement(2.0); da.addElement(2.0); da.addElement(2.0); da.addElement(2.0); da.addElement(2.0); da.addElement(2.0); da.addElement(2.0); Assert.assertEquals( "Number of elements should be 11", 11, da.getNumElements()); ((ResizableDoubleArray)da).discardFrontElements(5); Assert.assertEquals( "Number of elements should be 6", 6, da.getNumElements()); da.addElement(2.0); da.addElement(2.0); da.addElement(2.0); da.addElement(2.0); Assert.assertEquals( "Number of elements should be 10", 10, da.getNumElements()); ((ResizableDoubleArray)da).discardMostRecentElements(2); Assert.assertEquals( "Number of elements should be 8", 8, da.getNumElements()); try { ((ResizableDoubleArray)da).discardFrontElements(-1); Assert.fail( "Trying to discard a negative number of element is not allowed"); } catch( Exception e ){ } try { ((ResizableDoubleArray)da).discardMostRecentElements(-1); Assert.fail( "Trying to discard a negative number of element is not allowed"); } catch( Exception e ){ } try { ((ResizableDoubleArray)da).discardFrontElements( 10000 ); Assert.fail( "You can't discard more elements than the array contains"); } catch( Exception e ){ } try { ((ResizableDoubleArray)da).discardMostRecentElements( 10000 ); Assert.fail( "You can't discard more elements than the array contains"); } catch( Exception e ){ } } @Test public void testSubstitute() { da.addElement(2.0); da.addElement(2.0); da.addElement(2.0); da.addElement(2.0); da.addElement(2.0); da.addElement(2.0); da.addElement(2.0); da.addElement(2.0); da.addElement(2.0); da.addElement(2.0); da.addElement(2.0); Assert.assertEquals( "Number of elements should be 11", 11, da.getNumElements()); ((ResizableDoubleArray)da).substituteMostRecentElement(24); Assert.assertEquals( "Number of elements should be 11", 11, da.getNumElements()); try { ((ResizableDoubleArray)da).discardMostRecentElements(10); } catch( Exception e ){ Assert.fail( "Trying to discard a negative number of element is not allowed"); } ((ResizableDoubleArray)da).substituteMostRecentElement(24); Assert.assertEquals( "Number of elements should be 1", 1, da.getNumElements()); } @Test public void testMutators() { ((ResizableDoubleArray)da).setContractionCriteria(10f); Assert.assertEquals(10f, ((ResizableDoubleArray)da).getContractionCriteria(), 0); ((ResizableDoubleArray)da).setExpansionFactor(8f); Assert.assertEquals(8f, ((ResizableDoubleArray)da).getExpansionFactor(), 0); try { ((ResizableDoubleArray)da).setExpansionFactor(11f); // greater than contractionCriteria Assert.fail("Expecting IllegalArgumentException"); } catch (IllegalArgumentException ex) { // expected } ((ResizableDoubleArray)da).setExpansionMode( ResizableDoubleArray.ADDITIVE_MODE); Assert.assertEquals(ResizableDoubleArray.ADDITIVE_MODE, ((ResizableDoubleArray)da).getExpansionMode()); try { ((ResizableDoubleArray)da).setExpansionMode(-1); Assert.fail("Expecting IllegalArgumentException"); } catch (IllegalArgumentException ex) { // expected } } @Test public void testEqualsAndHashCode() throws Exception { // Wrong type ResizableDoubleArray first = new ResizableDoubleArray(); Double other = new Double(2); Assert.assertFalse(first.equals(other)); // Null other = null; Assert.assertFalse(first.equals(other)); // Reflexive Assert.assertTrue(first.equals(first)); // Argumentless constructor ResizableDoubleArray second = new ResizableDoubleArray(); verifyEquality(first, second); // Equals iff same data, same properties ResizableDoubleArray third = new ResizableDoubleArray(3, 2.0f, 2.0f); verifyInequality(third, first); ResizableDoubleArray fourth = new ResizableDoubleArray(3, 2.0f, 2.0f); ResizableDoubleArray fifth = new ResizableDoubleArray(2, 2.0f, 2.0f); verifyEquality(third, fourth); verifyInequality(third, fifth); third.addElement(4.1); third.addElement(4.2); third.addElement(4.3); fourth.addElement(4.1); fourth.addElement(4.2); fourth.addElement(4.3); verifyEquality(third, fourth); // expand fourth.addElement(4.4); verifyInequality(third, fourth); third.addElement(4.4); verifyEquality(third, fourth); fourth.addElement(4.4); verifyInequality(third, fourth); third.addElement(4.4); verifyEquality(third, fourth); fourth.addElementRolling(4.5); third.addElementRolling(4.5); verifyEquality(third, fourth); // discard third.discardFrontElements(1); verifyInequality(third, fourth); fourth.discardFrontElements(1); verifyEquality(third, fourth); // discard recent third.discardMostRecentElements(2); fourth.discardMostRecentElements(2); verifyEquality(third, fourth); // wrong order third.addElement(18); fourth.addElement(17); third.addElement(17); fourth.addElement(18); verifyInequality(third, fourth); // copy ResizableDoubleArray.copy(fourth, fifth); verifyEquality(fourth, fifth); // Copy constructor verifyEquality(fourth, new ResizableDoubleArray(fourth)); // Instance copy verifyEquality(fourth, fourth.copy()); } @Test public void testGetArrayRef() { final ResizableDoubleArray a = new ResizableDoubleArray(); // Modify "a" through the public API. final int index = 20; final double v1 = 1.2; a.setElement(index, v1); // Modify the internal storage through the protected API. final double v2 = v1 + 3.4; final double[] aInternalArray = a.getArrayRef(); aInternalArray[a.getStartIndex() + index] = v2; Assert.assertEquals(v2, a.getElement(index), 0d); } @Test public void testCompute() { final ResizableDoubleArray a = new ResizableDoubleArray(); final int max = 20; for (int i = 1; i <= max; i++) { a.setElement(i, i); } final MathArrays.Function add = new MathArrays.Function() { public double evaluate(double[] a, int index, int num) { double sum = 0; final int max = index + num; for (int i = index; i < max; i++) { sum += a[i]; } return sum; } public double evaluate(double[] a) { return evaluate(a, 0, a.length); } }; final double sum = a.compute(add); Assert.assertEquals(0.5 * max * (max + 1), sum, 0); } private void verifyEquality(ResizableDoubleArray a, ResizableDoubleArray b) { Assert.assertTrue(b.equals(a)); Assert.assertTrue(a.equals(b)); Assert.assertEquals(a.hashCode(), b.hashCode()); } private void verifyInequality(ResizableDoubleArray a, ResizableDoubleArray b) { Assert.assertFalse(b.equals(a)); Assert.assertFalse(a.equals(b)); Assert.assertFalse(a.hashCode() == b.hashCode()); } } commons-math3-3.2-src/src/test/java/org/apache/commons/math3/util/TransformerMapTest.java100644 1750 1750 7631 12126627675 30214 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.util; import org.apache.commons.math3.TestUtils; import org.junit.Assert; import org.junit.Test; /** * @version $Id: TransformerMapTest.java 1244107 2012-02-14 16:17:55Z erans $ */ public class TransformerMapTest { /** * */ @Test public void testPutTransformer(){ NumberTransformer expected = new DefaultTransformer(); TransformerMap map = new TransformerMap(); map.putTransformer(TransformerMapTest.class, expected); Assert.assertEquals(expected, map.getTransformer(TransformerMapTest.class)); } /** * */ @Test public void testContainsClass(){ NumberTransformer expected = new DefaultTransformer(); TransformerMap map = new TransformerMap(); map.putTransformer(TransformerMapTest.class, expected); Assert.assertTrue(map.containsClass(TransformerMapTest.class)); } /** * */ @Test public void testContainsTransformer(){ NumberTransformer expected = new DefaultTransformer(); TransformerMap map = new TransformerMap(); map.putTransformer(TransformerMapTest.class, expected); Assert.assertTrue(map.containsTransformer(expected)); } /** * */ @Test public void testRemoveTransformer(){ NumberTransformer expected = new DefaultTransformer(); TransformerMap map = new TransformerMap(); map.putTransformer(TransformerMapTest.class, expected); Assert.assertTrue(map.containsClass(TransformerMapTest.class)); Assert.assertTrue(map.containsTransformer(expected)); map.removeTransformer(TransformerMapTest.class); Assert.assertFalse(map.containsClass(TransformerMapTest.class)); Assert.assertFalse(map.containsTransformer(expected)); } /** * */ @Test public void testClear(){ NumberTransformer expected = new DefaultTransformer(); TransformerMap map = new TransformerMap(); map.putTransformer(TransformerMapTest.class, expected); Assert.assertTrue(map.containsClass(TransformerMapTest.class)); map.clear(); Assert.assertFalse(map.containsClass(TransformerMapTest.class)); } /** * */ @Test public void testClasses(){ NumberTransformer expected = new DefaultTransformer(); TransformerMap map = new TransformerMap(); map.putTransformer(TransformerMapTest.class, expected); Assert.assertTrue(map.classes().contains(TransformerMapTest.class)); } /** * */ @Test public void testTransformers(){ NumberTransformer expected = new DefaultTransformer(); TransformerMap map = new TransformerMap(); map.putTransformer(TransformerMapTest.class, expected); Assert.assertTrue(map.transformers().contains(expected)); } @Test public void testSerial(){ NumberTransformer expected = new DefaultTransformer(); TransformerMap map = new TransformerMap(); map.putTransformer(TransformerMapTest.class, expected); Assert.assertEquals(map, TestUtils.serializeAndRecover(map)); } } commons-math3-3.2-src/src/test/java/org/apache/commons/math3/util/Decimal64Test.java100644 1750 1750 41434 12126627675 27003 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.util; import org.apache.commons.math3.ExtendedFieldElementAbstractTest; import org.junit.Assert; import org.junit.Test; public class Decimal64Test extends ExtendedFieldElementAbstractTest { public static final double X = 1.2345; public static final Decimal64 PLUS_X = new Decimal64(X); public static final Decimal64 MINUS_X = new Decimal64(-X); public static final double Y = 6.789; public static final Decimal64 PLUS_Y = new Decimal64(Y); public static final Decimal64 MINUS_Y = new Decimal64(-Y); public static final Decimal64 PLUS_ZERO = new Decimal64(0.0); public static final Decimal64 MINUS_ZERO = new Decimal64(-0.0); protected Decimal64 build(final double x) { return new Decimal64(x); } @Test public void testAdd() { Decimal64 expected, actual; expected = new Decimal64(X + Y); actual = PLUS_X.add(PLUS_Y); Assert.assertEquals(expected, actual); actual = PLUS_Y.add(PLUS_X); Assert.assertEquals(expected, actual); expected = new Decimal64(X + (-Y)); actual = PLUS_X.add(MINUS_Y); Assert.assertEquals(expected, actual); actual = MINUS_Y.add(PLUS_X); Assert.assertEquals(expected, actual); expected = new Decimal64((-X) + (-Y)); actual = MINUS_X.add(MINUS_Y); Assert.assertEquals(expected, actual); actual = MINUS_Y.add(MINUS_X); Assert.assertEquals(expected, actual); expected = Decimal64.POSITIVE_INFINITY; actual = PLUS_X.add(Decimal64.POSITIVE_INFINITY); Assert.assertEquals(expected, actual); actual = Decimal64.POSITIVE_INFINITY.add(PLUS_X); Assert.assertEquals(expected, actual); actual = MINUS_X.add(Decimal64.POSITIVE_INFINITY); Assert.assertEquals(expected, actual); actual = Decimal64.POSITIVE_INFINITY.add(MINUS_X); Assert.assertEquals(expected, actual); actual = Decimal64.POSITIVE_INFINITY.add(Decimal64.POSITIVE_INFINITY); Assert.assertEquals(expected, actual); expected = Decimal64.NEGATIVE_INFINITY; actual = PLUS_X.add(Decimal64.NEGATIVE_INFINITY); Assert.assertEquals(expected, actual); actual = Decimal64.NEGATIVE_INFINITY.add(PLUS_X); Assert.assertEquals(expected, actual); actual = Decimal64.NEGATIVE_INFINITY.add(Decimal64.NEGATIVE_INFINITY); Assert.assertEquals(expected, actual); actual = MINUS_X.add(Decimal64.NEGATIVE_INFINITY); Assert.assertEquals(expected, actual); actual = Decimal64.NEGATIVE_INFINITY.add(MINUS_X); Assert.assertEquals(expected, actual); expected = Decimal64.NAN; actual = Decimal64.POSITIVE_INFINITY.add(Decimal64.NEGATIVE_INFINITY); Assert.assertEquals(expected, actual); actual = Decimal64.NEGATIVE_INFINITY.add(Decimal64.POSITIVE_INFINITY); Assert.assertEquals(expected, actual); actual = PLUS_X.add(Decimal64.NAN); Assert.assertEquals(expected, actual); actual = Decimal64.NAN.add(PLUS_X); Assert.assertEquals(expected, actual); actual = MINUS_X.add(Decimal64.NAN); Assert.assertEquals(expected, actual); actual = Decimal64.NAN.add(MINUS_X); Assert.assertEquals(expected, actual); actual = Decimal64.POSITIVE_INFINITY.add(Decimal64.NAN); Assert.assertEquals(expected, actual); actual = Decimal64.NAN.add(Decimal64.POSITIVE_INFINITY); Assert.assertEquals(expected, actual); actual = Decimal64.NEGATIVE_INFINITY.add(Decimal64.NAN); Assert.assertEquals(expected, actual); actual = Decimal64.NAN.add(Decimal64.NEGATIVE_INFINITY); Assert.assertEquals(expected, actual); actual = Decimal64.NAN.add(Decimal64.NAN); Assert.assertEquals(expected, actual); } @Test public void testSubtract() { Decimal64 expected, actual; expected = new Decimal64(X - Y); actual = PLUS_X.subtract(PLUS_Y); Assert.assertEquals(expected, actual); expected = new Decimal64(X - (-Y)); actual = PLUS_X.subtract(MINUS_Y); Assert.assertEquals(expected, actual); expected = new Decimal64((-X) - Y); actual = MINUS_X.subtract(PLUS_Y); Assert.assertEquals(expected, actual); expected = new Decimal64((-X) - (-Y)); actual = MINUS_X.subtract(MINUS_Y); Assert.assertEquals(expected, actual); expected = Decimal64.NEGATIVE_INFINITY; actual = PLUS_X.subtract(Decimal64.POSITIVE_INFINITY); Assert.assertEquals(expected, actual); actual = MINUS_X.subtract(Decimal64.POSITIVE_INFINITY); Assert.assertEquals(expected, actual); actual = Decimal64.NEGATIVE_INFINITY .subtract(Decimal64.POSITIVE_INFINITY); Assert.assertEquals(expected, actual); expected = Decimal64.POSITIVE_INFINITY; actual = PLUS_X.subtract(Decimal64.NEGATIVE_INFINITY); Assert.assertEquals(expected, actual); actual = MINUS_X.subtract(Decimal64.NEGATIVE_INFINITY); Assert.assertEquals(expected, actual); actual = Decimal64.POSITIVE_INFINITY .subtract(Decimal64.NEGATIVE_INFINITY); Assert.assertEquals(expected, actual); expected = Decimal64.NAN; actual = Decimal64.POSITIVE_INFINITY .subtract(Decimal64.POSITIVE_INFINITY); Assert.assertEquals(expected, actual); actual = Decimal64.NEGATIVE_INFINITY .subtract(Decimal64.NEGATIVE_INFINITY); Assert.assertEquals(expected, actual); actual = PLUS_X.subtract(Decimal64.NAN); Assert.assertEquals(expected, actual); actual = Decimal64.NAN.subtract(PLUS_X); Assert.assertEquals(expected, actual); actual = MINUS_X.subtract(Decimal64.NAN); Assert.assertEquals(expected, actual); actual = Decimal64.NAN.subtract(MINUS_X); Assert.assertEquals(expected, actual); actual = Decimal64.POSITIVE_INFINITY.subtract(Decimal64.NAN); Assert.assertEquals(expected, actual); actual = Decimal64.NAN.subtract(Decimal64.POSITIVE_INFINITY); Assert.assertEquals(expected, actual); actual = Decimal64.NEGATIVE_INFINITY.subtract(Decimal64.NAN); Assert.assertEquals(expected, actual); actual = Decimal64.NAN.subtract(Decimal64.NEGATIVE_INFINITY); Assert.assertEquals(expected, actual); actual = Decimal64.NAN.subtract(Decimal64.NAN); Assert.assertEquals(expected, actual); } @Test public void testNegate() { Decimal64 expected, actual; expected = MINUS_X; actual = PLUS_X.negate(); Assert.assertEquals(expected, actual); expected = PLUS_X; actual = MINUS_X.negate(); Assert.assertEquals(expected, actual); expected = MINUS_ZERO; actual = PLUS_ZERO.negate(); Assert.assertEquals(expected, actual); expected = PLUS_ZERO; actual = MINUS_ZERO.negate(); Assert.assertEquals(expected, actual); expected = Decimal64.POSITIVE_INFINITY; actual = Decimal64.NEGATIVE_INFINITY.negate(); Assert.assertEquals(expected, actual); expected = Decimal64.NEGATIVE_INFINITY; actual = Decimal64.POSITIVE_INFINITY.negate(); Assert.assertEquals(expected, actual); expected = Decimal64.NAN; actual = Decimal64.NAN.negate(); Assert.assertEquals(expected, actual); } @Test public void testMultiply() { Decimal64 expected, actual; expected = new Decimal64(X * Y); actual = PLUS_X.multiply(PLUS_Y); Assert.assertEquals(expected, actual); actual = PLUS_Y.multiply(PLUS_X); Assert.assertEquals(expected, actual); expected = new Decimal64(X * (-Y)); actual = PLUS_X.multiply(MINUS_Y); Assert.assertEquals(expected, actual); actual = MINUS_Y.multiply(PLUS_X); Assert.assertEquals(expected, actual); expected = new Decimal64((-X) * (-Y)); actual = MINUS_X.multiply(MINUS_Y); Assert.assertEquals(expected, actual); actual = MINUS_Y.multiply(MINUS_X); Assert.assertEquals(expected, actual); expected = Decimal64.POSITIVE_INFINITY; actual = PLUS_X.multiply(Decimal64.POSITIVE_INFINITY); Assert.assertEquals(expected, actual); actual = Decimal64.POSITIVE_INFINITY.multiply(PLUS_X); Assert.assertEquals(expected, actual); actual = MINUS_X.multiply(Decimal64.NEGATIVE_INFINITY); Assert.assertEquals(expected, actual); actual = Decimal64.NEGATIVE_INFINITY.multiply(MINUS_X); Assert.assertEquals(expected, actual); actual = Decimal64.POSITIVE_INFINITY .multiply(Decimal64.POSITIVE_INFINITY); Assert.assertEquals(expected, actual); actual = Decimal64.NEGATIVE_INFINITY .multiply(Decimal64.NEGATIVE_INFINITY); Assert.assertEquals(expected, actual); expected = Decimal64.NEGATIVE_INFINITY; actual = PLUS_X.multiply(Decimal64.NEGATIVE_INFINITY); Assert.assertEquals(expected, actual); actual = Decimal64.NEGATIVE_INFINITY.multiply(PLUS_X); Assert.assertEquals(expected, actual); actual = MINUS_X.multiply(Decimal64.POSITIVE_INFINITY); Assert.assertEquals(expected, actual); actual = Decimal64.POSITIVE_INFINITY.multiply(MINUS_X); Assert.assertEquals(expected, actual); actual = Decimal64.POSITIVE_INFINITY .multiply(Decimal64.NEGATIVE_INFINITY); Assert.assertEquals(expected, actual); actual = Decimal64.NEGATIVE_INFINITY .multiply(Decimal64.POSITIVE_INFINITY); Assert.assertEquals(expected, actual); expected = Decimal64.NAN; actual = PLUS_X.multiply(Decimal64.NAN); Assert.assertEquals(expected, actual); actual = Decimal64.NAN.multiply(PLUS_X); Assert.assertEquals(expected, actual); actual = MINUS_X.multiply(Decimal64.NAN); Assert.assertEquals(expected, actual); actual = Decimal64.NAN.multiply(MINUS_X); Assert.assertEquals(expected, actual); actual = Decimal64.POSITIVE_INFINITY.multiply(Decimal64.NAN); Assert.assertEquals(expected, actual); actual = Decimal64.NAN.multiply(Decimal64.POSITIVE_INFINITY); Assert.assertEquals(expected, actual); actual = Decimal64.NEGATIVE_INFINITY.multiply(Decimal64.NAN); Assert.assertEquals(expected, actual); actual = Decimal64.NAN.multiply(Decimal64.NEGATIVE_INFINITY); Assert.assertEquals(expected, actual); actual = Decimal64.NAN.multiply(Decimal64.NAN); Assert.assertEquals(expected, actual); } @Test public void testDivide() { Decimal64 expected, actual; expected = new Decimal64(X / Y); actual = PLUS_X.divide(PLUS_Y); Assert.assertEquals(expected, actual); expected = new Decimal64(X / (-Y)); actual = PLUS_X.divide(MINUS_Y); Assert.assertEquals(expected, actual); expected = new Decimal64((-X) / Y); actual = MINUS_X.divide(PLUS_Y); Assert.assertEquals(expected, actual); expected = new Decimal64((-X) / (-Y)); actual = MINUS_X.divide(MINUS_Y); Assert.assertEquals(expected, actual); expected = PLUS_ZERO; actual = PLUS_X.divide(Decimal64.POSITIVE_INFINITY); Assert.assertEquals(expected, actual); actual = MINUS_X.divide(Decimal64.NEGATIVE_INFINITY); Assert.assertEquals(expected, actual); expected = MINUS_ZERO; actual = MINUS_X.divide(Decimal64.POSITIVE_INFINITY); Assert.assertEquals(expected, actual); actual = PLUS_X.divide(Decimal64.NEGATIVE_INFINITY); Assert.assertEquals(expected, actual); expected = Decimal64.POSITIVE_INFINITY; actual = Decimal64.POSITIVE_INFINITY.divide(PLUS_X); Assert.assertEquals(expected, actual); actual = Decimal64.NEGATIVE_INFINITY.divide(MINUS_X); Assert.assertEquals(expected, actual); actual = PLUS_X.divide(PLUS_ZERO); Assert.assertEquals(expected, actual); actual = MINUS_X.divide(MINUS_ZERO); Assert.assertEquals(expected, actual); expected = Decimal64.NEGATIVE_INFINITY; actual = Decimal64.POSITIVE_INFINITY.divide(MINUS_X); Assert.assertEquals(expected, actual); actual = Decimal64.NEGATIVE_INFINITY.divide(PLUS_X); Assert.assertEquals(expected, actual); actual = PLUS_X.divide(MINUS_ZERO); Assert.assertEquals(expected, actual); actual = MINUS_X.divide(PLUS_ZERO); Assert.assertEquals(expected, actual); expected = Decimal64.NAN; actual = Decimal64.POSITIVE_INFINITY .divide(Decimal64.POSITIVE_INFINITY); Assert.assertEquals(expected, actual); actual = Decimal64.POSITIVE_INFINITY .divide(Decimal64.NEGATIVE_INFINITY); Assert.assertEquals(expected, actual); actual = Decimal64.NEGATIVE_INFINITY .divide(Decimal64.POSITIVE_INFINITY); Assert.assertEquals(expected, actual); actual = Decimal64.NEGATIVE_INFINITY .divide(Decimal64.NEGATIVE_INFINITY); Assert.assertEquals(expected, actual); actual = PLUS_X.divide(Decimal64.NAN); Assert.assertEquals(expected, actual); actual = Decimal64.NAN.divide(PLUS_X); Assert.assertEquals(expected, actual); actual = MINUS_X.divide(Decimal64.NAN); Assert.assertEquals(expected, actual); actual = Decimal64.NAN.divide(MINUS_X); Assert.assertEquals(expected, actual); actual = Decimal64.POSITIVE_INFINITY.divide(Decimal64.NAN); Assert.assertEquals(expected, actual); actual = Decimal64.NAN.divide(Decimal64.POSITIVE_INFINITY); Assert.assertEquals(expected, actual); actual = Decimal64.NEGATIVE_INFINITY.divide(Decimal64.NAN); Assert.assertEquals(expected, actual); actual = Decimal64.NAN.divide(Decimal64.NEGATIVE_INFINITY); Assert.assertEquals(expected, actual); actual = Decimal64.NAN.divide(Decimal64.NAN); Assert.assertEquals(expected, actual); actual = PLUS_ZERO.divide(PLUS_ZERO); Assert.assertEquals(expected, actual); actual = PLUS_ZERO.divide(MINUS_ZERO); Assert.assertEquals(expected, actual); actual = MINUS_ZERO.divide(PLUS_ZERO); Assert.assertEquals(expected, actual); actual = MINUS_ZERO.divide(MINUS_ZERO); Assert.assertEquals(expected, actual); } @Test public void testReciprocal() { Decimal64 expected, actual; expected = new Decimal64(1.0 / X); actual = PLUS_X.reciprocal(); Assert.assertEquals(expected, actual); expected = new Decimal64(1.0 / (-X)); actual = MINUS_X.reciprocal(); Assert.assertEquals(expected, actual); expected = PLUS_ZERO; actual = Decimal64.POSITIVE_INFINITY.reciprocal(); Assert.assertEquals(expected, actual); expected = MINUS_ZERO; actual = Decimal64.NEGATIVE_INFINITY.reciprocal(); Assert.assertEquals(expected, actual); } @Test public void testIsInfinite() { Assert.assertFalse(MINUS_X.isInfinite()); Assert.assertFalse(PLUS_X.isInfinite()); Assert.assertFalse(MINUS_Y.isInfinite()); Assert.assertFalse(PLUS_Y.isInfinite()); Assert.assertFalse(Decimal64.NAN.isInfinite()); Assert.assertTrue(Decimal64.NEGATIVE_INFINITY.isInfinite()); Assert.assertTrue(Decimal64.POSITIVE_INFINITY.isInfinite()); } @Test public void testIsNaN() { Assert.assertFalse(MINUS_X.isNaN()); Assert.assertFalse(PLUS_X.isNaN()); Assert.assertFalse(MINUS_Y.isNaN()); Assert.assertFalse(PLUS_Y.isNaN()); Assert.assertFalse(Decimal64.NEGATIVE_INFINITY.isNaN()); Assert.assertFalse(Decimal64.POSITIVE_INFINITY.isNaN()); Assert.assertTrue(Decimal64.NAN.isNaN()); } } commons-math3-3.2-src/src/test/java/org/apache/commons/math3/util/IncrementorTest.java100644 1750 1750 10241 12126627675 27550 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with this * work for additional information regarding copyright ownership. The ASF * licenses this file to You 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. */ package org.apache.commons.math3.util; import org.apache.commons.math3.exception.MaxCountExceededException; import org.apache.commons.math3.exception.TooManyEvaluationsException; import org.junit.Assert; import org.junit.Test; /** * Test for {@link Incrementor}. */ public class IncrementorTest { @Test public void testConstructor1() { final Incrementor i = new Incrementor(); Assert.assertEquals(0, i.getMaximalCount()); Assert.assertEquals(0, i.getCount()); } @Test public void testConstructor2() { final Incrementor i = new Incrementor(10); Assert.assertEquals(10, i.getMaximalCount()); Assert.assertEquals(0, i.getCount()); } @Test public void testCanIncrement1() { final Incrementor i = new Incrementor(3); Assert.assertTrue(i.canIncrement()); i.incrementCount(); Assert.assertTrue(i.canIncrement()); i.incrementCount(); Assert.assertTrue(i.canIncrement()); i.incrementCount(); Assert.assertFalse(i.canIncrement()); } @Test public void testCanIncrement2() { final Incrementor i = new Incrementor(3); while (i.canIncrement()) { i.incrementCount(); } // Must keep try/catch because the exception must be generated here, // and not in the previous loop. try { i.incrementCount(); Assert.fail("MaxCountExceededException expected"); } catch (MaxCountExceededException e) { // Expected. } } @Test public void testAccessor() { final Incrementor i = new Incrementor(); i.setMaximalCount(10); Assert.assertEquals(10, i.getMaximalCount()); Assert.assertEquals(0, i.getCount()); } @Test public void testBelowMaxCount() { final Incrementor i = new Incrementor(); i.setMaximalCount(3); i.incrementCount(); i.incrementCount(); i.incrementCount(); Assert.assertEquals(3, i.getCount()); } @Test(expected=MaxCountExceededException.class) public void testAboveMaxCount() { final Incrementor i = new Incrementor(); i.setMaximalCount(3); i.incrementCount(); i.incrementCount(); i.incrementCount(); i.incrementCount(); } @Test(expected=TooManyEvaluationsException.class) public void testAlternateException() { final Incrementor.MaxCountExceededCallback cb = new Incrementor.MaxCountExceededCallback() { /** {@inheritDoc} */ public void trigger(int max) { throw new TooManyEvaluationsException(max); } }; final Incrementor i = new Incrementor(0, cb); i.incrementCount(); } @Test public void testReset() { final Incrementor i = new Incrementor(); i.setMaximalCount(3); i.incrementCount(); i.incrementCount(); i.incrementCount(); Assert.assertEquals(3, i.getCount()); i.resetCount(); Assert.assertEquals(0, i.getCount()); } @Test public void testBulkIncrement() { final Incrementor i = new Incrementor(); i.setMaximalCount(3); i.incrementCount(2); Assert.assertEquals(2, i.getCount()); i.incrementCount(1); Assert.assertEquals(3, i.getCount()); } }commons-math3-3.2-src/src/test/java/org/apache/commons/math3/util/FastMathTestPerformance.java100644 1750 1750 124521 12126627675 31203 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.util; import org.apache.commons.math3.PerfTestUtils; import org.junit.BeforeClass; import org.junit.Test; import org.junit.Assert; /** * Performance tests for FastMath. * Not enabled by default, as the class does not end in Test. * * Invoke by running
                          * {@code mvn test -Dtest=FastMathTestPerformance}
                          * or by running
                          * {@code mvn test -Dtest=FastMathTestPerformance -DargLine="-DtestRuns=1234 -server"}
                          */ public class FastMathTestPerformance { private static final int RUNS = Integer.parseInt(System.getProperty("testRuns","10000000")); private static final double F1 = 1d / RUNS; // Header format private static final String FMT_HDR = "%-5s %13s %13s %13s Runs=%d Java %s (%s) %s (%s)"; // Detail format private static final String FMT_DTL = "%-5s %6d %6.1f %6d %6.4f %6d %6.4f"; @BeforeClass public static void header() { System.out.println(String.format(FMT_HDR, "Name","StrictMath","FastMath","Math",RUNS, System.getProperty("java.version"), System.getProperty("java.runtime.version","?"), System.getProperty("java.vm.name"), System.getProperty("java.vm.version") )); } private static void report(String name, long strictMathTime, long fastMathTime, long mathTime) { long unitTime = strictMathTime; System.out.println(String.format(FMT_DTL, name, strictMathTime / RUNS, (double) strictMathTime / unitTime, fastMathTime / RUNS, (double) fastMathTime / unitTime, mathTime / RUNS, (double) mathTime / unitTime )); } @Test public void testLog() { double x = 0; long time = System.nanoTime(); for (int i = 0; i < RUNS; i++) { x += StrictMath.log(0.01 + i); } long strictMath = System.nanoTime() - time; x = 0; time = System.nanoTime(); for (int i = 0; i < RUNS; i++) { x += FastMath.log(0.01 + i); } long fastTime = System.nanoTime() - time; x = 0; time = System.nanoTime(); for (int i = 0; i < RUNS; i++) { x += Math.log(0.01 + i); } long mathTime = System.nanoTime() - time; report("log",strictMath,fastTime,mathTime); Assert.assertTrue(!Double.isNaN(x)); } @Test public void testLog10() { double x = 0; long time = System.nanoTime(); for (int i = 0; i < RUNS; i++) { x += StrictMath.log10(0.01 + i); } long strictMath = System.nanoTime() - time; x = 0; time = System.nanoTime(); for (int i = 0; i < RUNS; i++) { x += FastMath.log10(0.01 + i); } long fastTime = System.nanoTime() - time; x = 0; time = System.nanoTime(); for (int i = 0; i < RUNS; i++) { x += Math.log10(0.01 + i); } long mathTime = System.nanoTime() - time; report("log10",strictMath,fastTime,mathTime); Assert.assertTrue(!Double.isNaN(x)); } @Test public void testLog1p() { double x = 0; long time = System.nanoTime(); for (int i = 0; i < RUNS; i++) { x += StrictMath.log1p(-0.9 + i); } long strictMath = System.nanoTime() - time; x = 0; time = System.nanoTime(); for (int i = 0; i < RUNS; i++) { x += FastMath.log1p(-0.9 + i); } long fastTime = System.nanoTime() - time; x = 0; time = System.nanoTime(); for (int i = 0; i < RUNS; i++) { x += Math.log1p(-0.9 + i); } long mathTime = System.nanoTime() - time; report("log1p",strictMath,fastTime,mathTime); Assert.assertTrue(!Double.isNaN(x)); } @Test public void testPow() { double x = 0; long time = System.nanoTime(); for (int i = 0; i < RUNS; i++) { x += StrictMath.pow(0.01 + i * F1, i * F1); } long strictTime = System.nanoTime() - time; x = 0; time = System.nanoTime(); for (int i = 0; i < RUNS; i++) { x += FastMath.pow(0.01 + i * F1, i * F1); } long fastTime = System.nanoTime() - time; x = 0; time = System.nanoTime(); for (int i = 0; i < RUNS; i++) { x += Math.pow(0.01 + i * F1, i * F1); } long mathTime = System.nanoTime() - time; report("pow",strictTime,fastTime,mathTime); Assert.assertTrue(!Double.isNaN(x)); } @Test public void testExp() { double x = 0; long time = System.nanoTime(); for (int i = 0; i < RUNS; i++) { x += StrictMath.exp(100 * i * F1); } long strictTime = System.nanoTime() - time; x = 0; time = System.nanoTime(); for (int i = 0; i < RUNS; i++) { x += FastMath.exp(100 * i * F1); } long fastTime = System.nanoTime() - time; x = 0; time = System.nanoTime(); for (int i = 0; i < RUNS; i++) { x += Math.exp(100 * i * F1); } long mathTime = System.nanoTime() - time; report("exp",strictTime,fastTime,mathTime); Assert.assertTrue(!Double.isNaN(x)); } @Test public void testSin() { double x = 0; long time = System.nanoTime(); for (int i = 0; i < RUNS; i++) { x += StrictMath.sin(100 * (i - RUNS/2) * F1); } long strictTime = System.nanoTime() - time; x = 0; time = System.nanoTime(); for (int i = 0; i < RUNS; i++) { x += FastMath.sin(100 * (i - RUNS/2) * F1); } long fastTime = System.nanoTime() - time; x = 0; time = System.nanoTime(); for (int i = 0; i < RUNS; i++) { x += Math.sin(100 * (i - RUNS/2) * F1); } long mathTime = System.nanoTime() - time; report("sin",strictTime,fastTime,mathTime); Assert.assertTrue(!Double.isNaN(x)); } @Test public void testAsin() { double x = 0; long time = System.nanoTime(); for (int i = 0; i < RUNS; i++) { x += StrictMath.asin(0.999 * (i - RUNS/2) * F1); } long strictTime = System.nanoTime() - time; x = 0; time = System.nanoTime(); for (int i = 0; i < RUNS; i++) { x += FastMath.asin(0.999 * (i - RUNS/2) * F1); } long fastTime = System.nanoTime() - time; x = 0; time = System.nanoTime(); for (int i = 0; i < RUNS; i++) { x += Math.asin(0.999 * (i - RUNS/2) * F1); } long mathTime = System.nanoTime() - time; report("asin",strictTime,fastTime,mathTime); Assert.assertTrue(!Double.isNaN(x)); } @Test public void testCos() { double x = 0; long time = System.nanoTime(); for (int i = 0; i < RUNS; i++) { x += StrictMath.cos(100 * (i - RUNS/2) * F1); } long strictTime = System.nanoTime() - time; x = 0; time = System.nanoTime(); for (int i = 0; i < RUNS; i++) { x += FastMath.cos(100 * (i - RUNS/2) * F1); } long fastTime = System.nanoTime() - time; x = 0; time = System.nanoTime(); for (int i = 0; i < RUNS; i++) { x += Math.cos(100 * (i - RUNS/2) * F1); } long mathTime = System.nanoTime() - time; report("cos",strictTime,fastTime,mathTime); Assert.assertTrue(!Double.isNaN(x)); } @Test public void testAcos() { double x = 0; long time = System.nanoTime(); for (int i = 0; i < RUNS; i++) { x += StrictMath.acos(0.999 * (i - RUNS/2) * F1); } long strictTime = System.nanoTime() - time; x = 0; time = System.nanoTime(); for (int i = 0; i < RUNS; i++) { x += FastMath.acos(0.999 * (i - RUNS/2) * F1); } long fastTime = System.nanoTime() - time; x = 0; time = System.nanoTime(); for (int i = 0; i < RUNS; i++) { x += Math.acos(0.999 * (i - RUNS/2) * F1); } long mathTime = System.nanoTime() - time; report("acos",strictTime,fastTime,mathTime); Assert.assertTrue(!Double.isNaN(x)); } @Test public void testTan() { double x = 0; long time = System.nanoTime(); for (int i = 0; i < RUNS; i++) { x += StrictMath.tan(100 * (i - RUNS/2) * F1); } long strictTime = System.nanoTime() - time; x = 0; time = System.nanoTime(); for (int i = 0; i < RUNS; i++) { x += FastMath.tan(100 * (i - RUNS/2) * F1); } long fastTime = System.nanoTime() - time; x = 0; time = System.nanoTime(); for (int i = 0; i < RUNS; i++) { x += Math.tan(100 * (i - RUNS/2) * F1); } long mathTime = System.nanoTime() - time; report("tan",strictTime,fastTime,mathTime); Assert.assertTrue(!Double.isNaN(x)); } @Test public void testAtan() { double x = 0; long time = System.nanoTime(); for (int i = 0; i < RUNS; i++) { x += StrictMath.atan(100 * (i - RUNS/2) * F1); } long strictTime = System.nanoTime() - time; x = 0; time = System.nanoTime(); for (int i = 0; i < RUNS; i++) { x += FastMath.atan(100 * (i - RUNS/2) * F1); } long fastTime = System.nanoTime() - time; x = 0; time = System.nanoTime(); for (int i = 0; i < RUNS; i++) { x += Math.atan(100 * (i - RUNS/2) * F1); } long mathTime = System.nanoTime() - time; report("atan",strictTime,fastTime,mathTime); Assert.assertTrue(!Double.isNaN(x)); } @Test public void testAtan2() { double x = 0; long time = System.nanoTime(); int max = (int) FastMath.floor(FastMath.sqrt(RUNS)); for (int i = 0; i < max; i++) { for (int j = 0; j < max; j++) { x += StrictMath.atan2((i - max/2) * (100.0 / max), (j - max/2) * (100.0 / max)); } } long strictTime = System.nanoTime() - time; x = 0; time = System.nanoTime(); for (int i = 0; i < max; i++) { for (int j = 0; j < max; j++) { x += FastMath.atan2((i - max/2) * (100.0 / max), (j - max/2) * (100.0 / max)); } } long fastTime = System.nanoTime() - time; x = 0; time = System.nanoTime(); for (int i = 0; i < max; i++) { for (int j = 0; j < max; j++) { x += Math.atan2((i - max/2) * (100.0 / max), (j - max/2) * (100.0 / max)); } } long mathTime = System.nanoTime() - time; report("atan2",strictTime,fastTime,mathTime); Assert.assertTrue(!Double.isNaN(x)); } @Test public void testHypot() { double x = 0; long time = System.nanoTime(); int max = (int) FastMath.floor(FastMath.sqrt(RUNS)); for (int i = 0; i < max; i++) { for (int j = 0; j < max; j++) { x += StrictMath.atan2((i - max/2) * (100.0 / max), (j - max/2) * (100.0 / max)); } } long strictTime = System.nanoTime() - time; x = 0; time = System.nanoTime(); for (int i = 0; i < max; i++) { for (int j = 0; j < max; j++) { x += FastMath.atan2((i - max/2) * (100.0 / max), (j - max/2) * (100.0 / max)); } } long fastTime = System.nanoTime() - time; x = 0; time = System.nanoTime(); for (int i = 0; i < max; i++) { for (int j = 0; j < max; j++) { x += Math.atan2((i - max/2) * (100.0 / max), (j - max/2) * (100.0 / max)); } } long mathTime = System.nanoTime() - time; report("hypot",strictTime,fastTime,mathTime); Assert.assertTrue(!Double.isNaN(x)); } @Test public void testCbrt() { double x = 0; long time = System.nanoTime(); for (int i = 0; i < RUNS; i++) { x += StrictMath.cbrt(100 * i * F1); } long strictTime = System.nanoTime() - time; x = 0; time = System.nanoTime(); for (int i = 0; i < RUNS; i++) { x += FastMath.cbrt(100 * i * F1); } long fastTime = System.nanoTime() - time; x = 0; time = System.nanoTime(); for (int i = 0; i < RUNS; i++) { x += Math.cbrt(100 * i * F1); } long mathTime = System.nanoTime() - time; report("cbrt",strictTime,fastTime,mathTime); Assert.assertTrue(!Double.isNaN(x)); } @Test public void testSqrt() { double x = 0; long time = System.nanoTime(); for (int i = 0; i < RUNS; i++) { x += StrictMath.sqrt(100 * i * F1); } long strictTime = System.nanoTime() - time; x = 0; time = System.nanoTime(); for (int i = 0; i < RUNS; i++) { x += FastMath.sqrt(100 * i * F1); } long fastTime = System.nanoTime() - time; x = 0; time = System.nanoTime(); for (int i = 0; i < RUNS; i++) { x += Math.sqrt(100 * i * F1); } long mathTime = System.nanoTime() - time; report("sqrt",strictTime,fastTime,mathTime); Assert.assertTrue(!Double.isNaN(x)); } @Test public void testCosh() { double x = 0; long time = System.nanoTime(); for (int i = 0; i < RUNS; i++) { x += StrictMath.cosh(100 * (i - RUNS/2) * F1); } long strictTime = System.nanoTime() - time; x = 0; time = System.nanoTime(); for (int i = 0; i < RUNS; i++) { x += FastMath.cosh(100 * (i - RUNS/2) * F1); } long fastTime = System.nanoTime() - time; x = 0; time = System.nanoTime(); for (int i = 0; i < RUNS; i++) { x += Math.cosh(100 * (i - RUNS/2) * F1); } long mathTime = System.nanoTime() - time; report("cosh",strictTime,fastTime,mathTime); Assert.assertTrue(!Double.isNaN(x)); } @Test public void testSinh() { double x = 0; long time = System.nanoTime(); for (int i = 0; i < RUNS; i++) { x += StrictMath.sinh(100 * (i - RUNS/2) * F1); } long strictTime = System.nanoTime() - time; x = 0; time = System.nanoTime(); for (int i = 0; i < RUNS; i++) { x += FastMath.sinh(100 * (i - RUNS/2) * F1); } long fastTime = System.nanoTime() - time; x = 0; time = System.nanoTime(); for (int i = 0; i < RUNS; i++) { x += Math.sinh(100 * (i - RUNS/2) * F1); } long mathTime = System.nanoTime() - time; report("sinh",strictTime,fastTime,mathTime); Assert.assertTrue(!Double.isNaN(x)); } @Test public void testTanh() { double x = 0; long time = System.nanoTime(); for (int i = 0; i < RUNS; i++) { x += StrictMath.tanh(100 * (i - RUNS/2) * F1); } long strictTime = System.nanoTime() - time; x = 0; time = System.nanoTime(); for (int i = 0; i < RUNS; i++) { x += FastMath.tanh(100 * (i - RUNS/2) * F1); } long fastTime = System.nanoTime() - time; x = 0; time = System.nanoTime(); for (int i = 0; i < RUNS; i++) { x += Math.tanh(100 * (i - RUNS/2) * F1); } long mathTime = System.nanoTime() - time; report("tanh",strictTime,fastTime,mathTime); Assert.assertTrue(!Double.isNaN(x)); } @Test public void testExpm1() { double x = 0; long time = System.nanoTime(); for (int i = 0; i < RUNS; i++) { x += StrictMath.expm1(100 * (i - RUNS/2) * F1); } long strictTime = System.nanoTime() - time; x = 0; time = System.nanoTime(); for (int i = 0; i < RUNS; i++) { x += FastMath.expm1(100 * (i - RUNS/2) * F1); } long fastTime = System.nanoTime() - time; x = 0; time = System.nanoTime(); for (int i = 0; i < RUNS; i++) { x += Math.expm1(100 * (i - RUNS/2) * F1); } long mathTime = System.nanoTime() - time; report("expm1",strictTime,fastTime,mathTime); Assert.assertTrue(!Double.isNaN(x)); } @Test public void testAbs() { double x = 0; long time = System.nanoTime(); for (int i = 0; i < RUNS; i++) { x += StrictMath.abs(i * (1 - 0.5 * RUNS)); } long strictTime = System.nanoTime() - time; x = 0; time = System.nanoTime(); for (int i = 0; i < RUNS; i++) { x += FastMath.abs(i * (1 - 0.5 * RUNS)); } long fastTime = System.nanoTime() - time; x = 0; time = System.nanoTime(); for (int i = 0; i < RUNS; i++) { x += Math.abs(i * (1 - 0.5 * RUNS)); } long mathTime = System.nanoTime() - time; report("abs",strictTime,fastTime,mathTime); Assert.assertTrue(!Double.isNaN(x)); } @Test public void testSimpleBenchmark() { final String SM = "StrictMath"; final String M = "Math"; final String FM = "FastMath"; final int numStat = 100; final int numCall = RUNS / numStat; final double x = Math.random(); final double y = Math.random(); PerfTestUtils.timeAndReport("log", numCall, numStat, false, new PerfTestUtils.RunTest(SM) { @Override public Double call() throws Exception { return StrictMath.log(x); } }, new PerfTestUtils.RunTest(M) { @Override public Double call() throws Exception { return Math.log(x); } }, new PerfTestUtils.RunTest(FM) { @Override public Double call() throws Exception { return FastMath.log(x); } }); PerfTestUtils.timeAndReport("log10", numCall, numStat, false, new PerfTestUtils.RunTest(SM) { @Override public Double call() throws Exception { return StrictMath.log10(x); } }, new PerfTestUtils.RunTest(M) { @Override public Double call() throws Exception { return Math.log10(x); } }, new PerfTestUtils.RunTest(FM) { @Override public Double call() throws Exception { return FastMath.log10(x); } }); PerfTestUtils.timeAndReport("log1p", numCall, numStat, false, new PerfTestUtils.RunTest(SM) { @Override public Double call() throws Exception { return StrictMath.log1p(x); } }, new PerfTestUtils.RunTest(M) { @Override public Double call() throws Exception { return Math.log1p(x); } }, new PerfTestUtils.RunTest(FM) { @Override public Double call() throws Exception { return FastMath.log1p(x); } }); PerfTestUtils.timeAndReport("pow", numCall, numStat, false, new PerfTestUtils.RunTest(SM) { @Override public Double call() throws Exception { return StrictMath.pow(x, y); } }, new PerfTestUtils.RunTest(M) { @Override public Double call() throws Exception { return Math.pow(x, y); } }, new PerfTestUtils.RunTest(FM) { @Override public Double call() throws Exception { return FastMath.pow(x, y); } }); PerfTestUtils.timeAndReport("exp", numCall, numStat, false, new PerfTestUtils.RunTest(SM) { @Override public Double call() throws Exception { return StrictMath.exp(x); } }, new PerfTestUtils.RunTest(M) { @Override public Double call() throws Exception { return Math.exp(x); } }, new PerfTestUtils.RunTest(FM) { @Override public Double call() throws Exception { return FastMath.exp(x); } }); PerfTestUtils.timeAndReport("sin", numCall, numStat, false, new PerfTestUtils.RunTest(SM) { @Override public Double call() throws Exception { return StrictMath.sin(x); } }, new PerfTestUtils.RunTest(M) { @Override public Double call() throws Exception { return Math.sin(x); } }, new PerfTestUtils.RunTest(FM) { @Override public Double call() throws Exception { return FastMath.sin(x); } }); PerfTestUtils.timeAndReport("asin", numCall, numStat, false, new PerfTestUtils.RunTest(SM) { @Override public Double call() throws Exception { return StrictMath.asin(x); } }, new PerfTestUtils.RunTest(M) { @Override public Double call() throws Exception { return Math.asin(x); } }, new PerfTestUtils.RunTest(FM) { @Override public Double call() throws Exception { return FastMath.asin(x); } }); PerfTestUtils.timeAndReport("cos", numCall, numStat, false, new PerfTestUtils.RunTest(SM) { @Override public Double call() throws Exception { return StrictMath.cos(x); } }, new PerfTestUtils.RunTest(M) { @Override public Double call() throws Exception { return Math.cos(x); } }, new PerfTestUtils.RunTest(FM) { @Override public Double call() throws Exception { return FastMath.cos(x); } }); PerfTestUtils.timeAndReport("acos", numCall, numStat, false, new PerfTestUtils.RunTest(SM) { @Override public Double call() throws Exception { return StrictMath.acos(x); } }, new PerfTestUtils.RunTest(M) { @Override public Double call() throws Exception { return Math.acos(x); } }, new PerfTestUtils.RunTest(FM) { @Override public Double call() throws Exception { return FastMath.acos(x); } }); PerfTestUtils.timeAndReport("tan", numCall, numStat, false, new PerfTestUtils.RunTest(SM) { @Override public Double call() throws Exception { return StrictMath.tan(x); } }, new PerfTestUtils.RunTest(M) { @Override public Double call() throws Exception { return Math.tan(x); } }, new PerfTestUtils.RunTest(FM) { @Override public Double call() throws Exception { return FastMath.tan(x); } }); PerfTestUtils.timeAndReport("atan", numCall, numStat, false, new PerfTestUtils.RunTest(SM) { @Override public Double call() throws Exception { return StrictMath.atan(x); } }, new PerfTestUtils.RunTest(M) { @Override public Double call() throws Exception { return Math.atan(x); } }, new PerfTestUtils.RunTest(FM) { @Override public Double call() throws Exception { return FastMath.atan(x); } }); PerfTestUtils.timeAndReport("atan2", numCall, numStat, false, new PerfTestUtils.RunTest(SM) { @Override public Double call() throws Exception { return StrictMath.atan2(x, y); } }, new PerfTestUtils.RunTest(M) { @Override public Double call() throws Exception { return Math.atan2(x, y); } }, new PerfTestUtils.RunTest(FM) { @Override public Double call() throws Exception { return FastMath.atan2(x, y); } }); PerfTestUtils.timeAndReport("hypot", numCall, numStat, false, new PerfTestUtils.RunTest(SM) { @Override public Double call() throws Exception { return StrictMath.hypot(x, y); } }, new PerfTestUtils.RunTest(M) { @Override public Double call() throws Exception { return Math.hypot(x, y); } }, new PerfTestUtils.RunTest(FM) { @Override public Double call() throws Exception { return FastMath.hypot(x, y); } }); PerfTestUtils.timeAndReport("cbrt", numCall, numStat, false, new PerfTestUtils.RunTest(SM) { @Override public Double call() throws Exception { return StrictMath.cbrt(x); } }, new PerfTestUtils.RunTest(M) { @Override public Double call() throws Exception { return Math.cbrt(x); } }, new PerfTestUtils.RunTest(FM) { @Override public Double call() throws Exception { return FastMath.cbrt(x); } }); PerfTestUtils.timeAndReport("sqrt", numCall, numStat, false, new PerfTestUtils.RunTest(SM) { @Override public Double call() throws Exception { return StrictMath.sqrt(x); } }, new PerfTestUtils.RunTest(M) { @Override public Double call() throws Exception { return Math.sqrt(x); } }, new PerfTestUtils.RunTest(FM) { @Override public Double call() throws Exception { return FastMath.sqrt(x); } }); PerfTestUtils.timeAndReport("cosh", numCall, numStat, false, new PerfTestUtils.RunTest(SM) { @Override public Double call() throws Exception { return StrictMath.cosh(x); } }, new PerfTestUtils.RunTest(M) { @Override public Double call() throws Exception { return Math.cosh(x); } }, new PerfTestUtils.RunTest(FM) { @Override public Double call() throws Exception { return FastMath.cosh(x); } }); PerfTestUtils.timeAndReport("sinh", numCall, numStat, false, new PerfTestUtils.RunTest(SM) { @Override public Double call() throws Exception { return StrictMath.sinh(x); } }, new PerfTestUtils.RunTest(M) { @Override public Double call() throws Exception { return Math.sinh(x); } }, new PerfTestUtils.RunTest(FM) { @Override public Double call() throws Exception { return FastMath.sinh(x); } }); PerfTestUtils.timeAndReport("tanh", numCall, numStat, false, new PerfTestUtils.RunTest(SM) { @Override public Double call() throws Exception { return StrictMath.tanh(x); } }, new PerfTestUtils.RunTest(M) { @Override public Double call() throws Exception { return Math.tanh(x); } }, new PerfTestUtils.RunTest(FM) { @Override public Double call() throws Exception { return FastMath.tanh(x); } }); PerfTestUtils.timeAndReport("expm1", numCall, numStat, false, new PerfTestUtils.RunTest(SM) { @Override public Double call() throws Exception { return StrictMath.expm1(x); } }, new PerfTestUtils.RunTest(M) { @Override public Double call() throws Exception { return Math.expm1(x); } }, new PerfTestUtils.RunTest(FM) { @Override public Double call() throws Exception { return FastMath.expm1(x); } }); PerfTestUtils.timeAndReport("abs", numCall, numStat, false, new PerfTestUtils.RunTest(SM) { @Override public Double call() throws Exception { return StrictMath.abs(x); } }, new PerfTestUtils.RunTest(M) { @Override public Double call() throws Exception { return Math.abs(x); } }, new PerfTestUtils.RunTest(FM) { @Override public Double call() throws Exception { return FastMath.abs(x); } }); } } commons-math3-3.2-src/src/test/java/org/apache/commons/math3/util/OpenIntToFieldTest.java100644 1750 1750 30360 12126627675 30112 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.util; import java.util.ConcurrentModificationException; import java.util.HashMap; import java.util.HashSet; import java.util.Map; import java.util.NoSuchElementException; import java.util.Random; import java.util.Set; import java.util.Map.Entry; import org.apache.commons.math3.Field; import org.apache.commons.math3.fraction.Fraction; import org.apache.commons.math3.fraction.FractionConversionException; import org.apache.commons.math3.fraction.FractionField; import org.junit.Assert; import org.junit.Before; import org.junit.Test; public class OpenIntToFieldTest { private Map javaMap = new HashMap(); private FractionField field = FractionField.getInstance(); @Before public void setUp() throws FractionConversionException { javaMap.put(50, new Fraction(100.0)); javaMap.put(75, new Fraction(75.0)); javaMap.put(25, new Fraction(500.0)); javaMap.put(Integer.MAX_VALUE, new Fraction(Integer.MAX_VALUE)); javaMap.put(0, new Fraction(-1.0)); javaMap.put(1, new Fraction(0.0)); javaMap.put(33, new Fraction(-0.1)); javaMap.put(23234234, new Fraction(-242343.0)); javaMap.put(23321, new Fraction (Integer.MIN_VALUE)); javaMap.put(-4444, new Fraction(332.0)); javaMap.put(-1, new Fraction(-2323.0)); javaMap.put(Integer.MIN_VALUE, new Fraction(44.0)); /* Add a few more to cause the table to rehash */ javaMap.putAll(generate()); } private Map generate() { Map map = new HashMap(); Random r = new Random(); double dd=0; for (int i = 0; i < 2000; ++i) dd = r.nextDouble(); try { map.put(r.nextInt(), new Fraction(dd)); } catch (FractionConversionException e) { throw new IllegalStateException("Invalid :"+dd, e); } return map; } private OpenIntToFieldHashMap createFromJavaMap(Field field) { OpenIntToFieldHashMap map = new OpenIntToFieldHashMap(field); for (Map.Entry mapEntry : javaMap.entrySet()) { map.put(mapEntry.getKey(), mapEntry.getValue()); } return map; } @Test public void testPutAndGetWith0ExpectedSize() { OpenIntToFieldHashMap map = new OpenIntToFieldHashMap(field,0); assertPutAndGet(map); } @Test public void testPutAndGetWithExpectedSize() { OpenIntToFieldHashMap map = new OpenIntToFieldHashMap(field,500); assertPutAndGet(map); } @Test public void testPutAndGet() { OpenIntToFieldHashMap map = new OpenIntToFieldHashMap(field); assertPutAndGet(map); } private void assertPutAndGet(OpenIntToFieldHashMap map) { assertPutAndGet(map, 0, new HashSet()); } private void assertPutAndGet(OpenIntToFieldHashMap map, int mapSize, Set keysInMap) { Assert.assertEquals(mapSize, map.size()); for (Map.Entry mapEntry : javaMap.entrySet()) { map.put(mapEntry.getKey(), mapEntry.getValue()); if (!keysInMap.contains(mapEntry.getKey())) ++mapSize; Assert.assertEquals(mapSize, map.size()); Assert.assertEquals(mapEntry.getValue(), map.get(mapEntry.getKey())); } } @Test public void testPutAbsentOnExisting() { OpenIntToFieldHashMap map = createFromJavaMap(field); int size = javaMap.size(); for (Map.Entry mapEntry : generateAbsent().entrySet()) { map.put(mapEntry.getKey(), mapEntry.getValue()); Assert.assertEquals(++size, map.size()); Assert.assertEquals(mapEntry.getValue(), map.get(mapEntry.getKey())); } } @Test public void testPutOnExisting() { OpenIntToFieldHashMap map = createFromJavaMap(field); for (Map.Entry mapEntry : javaMap.entrySet()) { map.put(mapEntry.getKey(), mapEntry.getValue()); Assert.assertEquals(javaMap.size(), map.size()); Assert.assertEquals(mapEntry.getValue(), map.get(mapEntry.getKey())); } } @Test public void testGetAbsent() { Map generated = generateAbsent(); OpenIntToFieldHashMap map = createFromJavaMap(field); for (Map.Entry mapEntry : generated.entrySet()) Assert.assertTrue(field.getZero().equals(map.get(mapEntry.getKey()))); } @Test public void testGetFromEmpty() { OpenIntToFieldHashMap map = new OpenIntToFieldHashMap(field); Assert.assertTrue(field.getZero().equals(map.get(5))); Assert.assertTrue(field.getZero().equals(map.get(0))); Assert.assertTrue(field.getZero().equals(map.get(50))); } @Test public void testRemove() { OpenIntToFieldHashMap map = createFromJavaMap(field); int mapSize = javaMap.size(); Assert.assertEquals(mapSize, map.size()); for (Map.Entry mapEntry : javaMap.entrySet()) { map.remove(mapEntry.getKey()); Assert.assertEquals(--mapSize, map.size()); Assert.assertTrue(field.getZero().equals(map.get(mapEntry.getKey()))); } /* Ensure that put and get still work correctly after removals */ assertPutAndGet(map); } /* This time only remove some entries */ @Test public void testRemove2() { OpenIntToFieldHashMap map = createFromJavaMap(field); int mapSize = javaMap.size(); int count = 0; Set keysInMap = new HashSet(javaMap.keySet()); for (Map.Entry mapEntry : javaMap.entrySet()) { keysInMap.remove(mapEntry.getKey()); map.remove(mapEntry.getKey()); Assert.assertEquals(--mapSize, map.size()); Assert.assertTrue(field.getZero().equals(map.get(mapEntry.getKey()))); if (count++ > 5) break; } /* Ensure that put and get still work correctly after removals */ assertPutAndGet(map, mapSize, keysInMap); } @Test public void testRemoveFromEmpty() { OpenIntToFieldHashMap map = new OpenIntToFieldHashMap(field); Assert.assertTrue(field.getZero().equals(map.remove(50))); } @Test public void testRemoveAbsent() { Map generated = generateAbsent(); OpenIntToFieldHashMap map = createFromJavaMap(field); int mapSize = map.size(); for (Map.Entry mapEntry : generated.entrySet()) { map.remove(mapEntry.getKey()); Assert.assertEquals(mapSize, map.size()); Assert.assertTrue(field.getZero().equals(map.get(mapEntry.getKey()))); } } /** * Returns a map with at least 100 elements where each element is absent from javaMap. */ private Map generateAbsent() { Map generated = new HashMap(); do { generated.putAll(generate()); for (Integer key : javaMap.keySet()) generated.remove(key); } while (generated.size() < 100); return generated; } @Test public void testCopy() { OpenIntToFieldHashMap copy = new OpenIntToFieldHashMap(createFromJavaMap(field)); Assert.assertEquals(javaMap.size(), copy.size()); for (Map.Entry mapEntry : javaMap.entrySet()) Assert.assertEquals(mapEntry.getValue(), copy.get(mapEntry.getKey())); } @Test public void testContainsKey() { OpenIntToFieldHashMap map = createFromJavaMap(field); for (Entry mapEntry : javaMap.entrySet()) { Assert.assertTrue(map.containsKey(mapEntry.getKey())); } for (Map.Entry mapEntry : generateAbsent().entrySet()) { Assert.assertFalse(map.containsKey(mapEntry.getKey())); } for (Entry mapEntry : javaMap.entrySet()) { int key = mapEntry.getKey(); Assert.assertTrue(map.containsKey(key)); map.remove(key); Assert.assertFalse(map.containsKey(key)); } } @Test public void testIterator() { OpenIntToFieldHashMap map = createFromJavaMap(field); OpenIntToFieldHashMap.Iterator iterator = map.iterator(); for (int i = 0; i < map.size(); ++i) { Assert.assertTrue(iterator.hasNext()); iterator.advance(); int key = iterator.key(); Assert.assertTrue(map.containsKey(key)); Assert.assertEquals(javaMap.get(key), map.get(key)); Assert.assertEquals(javaMap.get(key), iterator.value()); Assert.assertTrue(javaMap.containsKey(key)); } Assert.assertFalse(iterator.hasNext()); try { iterator.advance(); Assert.fail("an exception should have been thrown"); } catch (NoSuchElementException nsee) { // expected } } @Test public void testConcurrentModification() { OpenIntToFieldHashMap map = createFromJavaMap(field); OpenIntToFieldHashMap.Iterator iterator = map.iterator(); map.put(3, new Fraction(3)); try { iterator.advance(); Assert.fail("an exception should have been thrown"); } catch (ConcurrentModificationException cme) { // expected } } /** * Regression test for a bug in findInsertionIndex where the hashing in the second probing * loop was inconsistent with the first causing duplicate keys after the right sequence * of puts and removes. */ @Test public void testPutKeysWithCollisions() { OpenIntToFieldHashMap map = new OpenIntToFieldHashMap(field); int key1 = -1996012590; Fraction value1 = new Fraction(1); map.put(key1, value1); int key2 = 835099822; map.put(key2, value1); int key3 = 1008859686; map.put(key3, value1); Assert.assertEquals(value1, map.get(key3)); Assert.assertEquals(3, map.size()); map.remove(key2); Fraction value2 = new Fraction(2); map.put(key3, value2); Assert.assertEquals(value2, map.get(key3)); Assert.assertEquals(2, map.size()); } /** * Similar to testPutKeysWithCollisions() but exercises the codepaths in a slightly * different manner. */ @Test public void testPutKeysWithCollision2() { OpenIntToFieldHashMapmap = new OpenIntToFieldHashMap(field); int key1 = 837989881; Fraction value1 = new Fraction(1); map.put(key1, value1); int key2 = 476463321; map.put(key2, value1); Assert.assertEquals(2, map.size()); Assert.assertEquals(value1, map.get(key2)); map.remove(key1); Fraction value2 = new Fraction(2); map.put(key2, value2); Assert.assertEquals(1, map.size()); Assert.assertEquals(value2, map.get(key2)); } } commons-math3-3.2-src/src/test/java/org/apache/commons/math3/util/DefaultTransformerTest.java100644 1750 1750 6132 12126627675 31056 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.util; import java.math.BigDecimal; import org.apache.commons.math3.TestUtils; import org.apache.commons.math3.exception.MathIllegalArgumentException; import org.apache.commons.math3.exception.NullArgumentException; import org.junit.Assert; import org.junit.Test; /** * @version $Id: DefaultTransformerTest.java 1244107 2012-02-14 16:17:55Z erans $ */ public class DefaultTransformerTest { /** * */ @Test public void testTransformDouble() throws Exception { double expected = 1.0; Double input = Double.valueOf(expected); DefaultTransformer t = new DefaultTransformer(); Assert.assertEquals(expected, t.transform(input), 1.0e-4); } /** * */ @Test public void testTransformNull() throws Exception { DefaultTransformer t = new DefaultTransformer(); try { t.transform(null); Assert.fail("Expecting NullArgumentException"); } catch (NullArgumentException e) { // expected } } /** * */ @Test public void testTransformInteger() throws Exception { double expected = 1.0; Integer input = Integer.valueOf(1); DefaultTransformer t = new DefaultTransformer(); Assert.assertEquals(expected, t.transform(input), 1.0e-4); } /** * */ @Test public void testTransformBigDecimal() throws Exception { double expected = 1.0; BigDecimal input = new BigDecimal("1.0"); DefaultTransformer t = new DefaultTransformer(); Assert.assertEquals(expected, t.transform(input), 1.0e-4); } /** * */ @Test public void testTransformString() throws Exception { double expected = 1.0; String input = "1.0"; DefaultTransformer t = new DefaultTransformer(); Assert.assertEquals(expected, t.transform(input), 1.0e-4); } /** * */ @Test(expected=MathIllegalArgumentException.class) public void testTransformObject(){ Boolean input = Boolean.TRUE; DefaultTransformer t = new DefaultTransformer(); t.transform(input); } @Test public void testSerial() { Assert.assertEquals(new DefaultTransformer(), TestUtils.serializeAndRecover(new DefaultTransformer())); } } commons-math3-3.2-src/src/test/java/org/apache/commons/math3/util/FastMathTest.java100644 1750 1750 154145 12126627675 27026 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.util; import java.lang.reflect.Method; import java.lang.reflect.Modifier; import java.lang.reflect.Type; import org.apache.commons.math3.TestUtils; import org.apache.commons.math3.dfp.Dfp; import org.apache.commons.math3.dfp.DfpField; import org.apache.commons.math3.dfp.DfpMath; import org.apache.commons.math3.random.MersenneTwister; import org.apache.commons.math3.random.RandomGenerator; import org.junit.Assert; import org.junit.Before; import org.junit.Ignore; import org.junit.Test; public class FastMathTest { private static final double MAX_ERROR_ULP = 0.51; private static final int NUMBER_OF_TRIALS = 1000; private DfpField field; private RandomGenerator generator; @Before public void setUp() { field = new DfpField(40); generator = new MersenneTwister(6176597458463500194l); } @Test public void testMinMaxDouble() { double[][] pairs = { { -50.0, 50.0 }, { Double.POSITIVE_INFINITY, 1.0 }, { Double.NEGATIVE_INFINITY, 1.0 }, { Double.NaN, 1.0 }, { Double.POSITIVE_INFINITY, 0.0 }, { Double.NEGATIVE_INFINITY, 0.0 }, { Double.NaN, 0.0 }, { Double.NaN, Double.NEGATIVE_INFINITY }, { Double.NaN, Double.POSITIVE_INFINITY }, { Precision.SAFE_MIN, Precision.EPSILON } }; for (double[] pair : pairs) { Assert.assertEquals("min(" + pair[0] + ", " + pair[1] + ")", Math.min(pair[0], pair[1]), FastMath.min(pair[0], pair[1]), Precision.EPSILON); Assert.assertEquals("min(" + pair[1] + ", " + pair[0] + ")", Math.min(pair[1], pair[0]), FastMath.min(pair[1], pair[0]), Precision.EPSILON); Assert.assertEquals("max(" + pair[0] + ", " + pair[1] + ")", Math.max(pair[0], pair[1]), FastMath.max(pair[0], pair[1]), Precision.EPSILON); Assert.assertEquals("max(" + pair[1] + ", " + pair[0] + ")", Math.max(pair[1], pair[0]), FastMath.max(pair[1], pair[0]), Precision.EPSILON); } } @Test public void testMinMaxFloat() { float[][] pairs = { { -50.0f, 50.0f }, { Float.POSITIVE_INFINITY, 1.0f }, { Float.NEGATIVE_INFINITY, 1.0f }, { Float.NaN, 1.0f }, { Float.POSITIVE_INFINITY, 0.0f }, { Float.NEGATIVE_INFINITY, 0.0f }, { Float.NaN, 0.0f }, { Float.NaN, Float.NEGATIVE_INFINITY }, { Float.NaN, Float.POSITIVE_INFINITY } }; for (float[] pair : pairs) { Assert.assertEquals("min(" + pair[0] + ", " + pair[1] + ")", Math.min(pair[0], pair[1]), FastMath.min(pair[0], pair[1]), Precision.EPSILON); Assert.assertEquals("min(" + pair[1] + ", " + pair[0] + ")", Math.min(pair[1], pair[0]), FastMath.min(pair[1], pair[0]), Precision.EPSILON); Assert.assertEquals("max(" + pair[0] + ", " + pair[1] + ")", Math.max(pair[0], pair[1]), FastMath.max(pair[0], pair[1]), Precision.EPSILON); Assert.assertEquals("max(" + pair[1] + ", " + pair[0] + ")", Math.max(pair[1], pair[0]), FastMath.max(pair[1], pair[0]), Precision.EPSILON); } } @Test public void testConstants() { Assert.assertEquals(Math.PI, FastMath.PI, 1.0e-20); Assert.assertEquals(Math.E, FastMath.E, 1.0e-20); } @Test public void testAtan2() { double y1 = 1.2713504628280707e10; double x1 = -5.674940885228782e-10; Assert.assertEquals(Math.atan2(y1, x1), FastMath.atan2(y1, x1), 2 * Precision.EPSILON); double y2 = 0.0; double x2 = Double.POSITIVE_INFINITY; Assert.assertEquals(Math.atan2(y2, x2), FastMath.atan2(y2, x2), Precision.SAFE_MIN); } @Test public void testHyperbolic() { double maxErr = 0; for (double x = -30; x < 30; x += 0.001) { double tst = FastMath.sinh(x); double ref = Math.sinh(x); maxErr = FastMath.max(maxErr, FastMath.abs(ref - tst) / FastMath.ulp(ref)); } Assert.assertEquals(0, maxErr, 2); maxErr = 0; for (double x = -30; x < 30; x += 0.001) { double tst = FastMath.cosh(x); double ref = Math.cosh(x); maxErr = FastMath.max(maxErr, FastMath.abs(ref - tst) / FastMath.ulp(ref)); } Assert.assertEquals(0, maxErr, 2); maxErr = 0; for (double x = -0.5; x < 0.5; x += 0.001) { double tst = FastMath.tanh(x); double ref = Math.tanh(x); maxErr = FastMath.max(maxErr, FastMath.abs(ref - tst) / FastMath.ulp(ref)); } Assert.assertEquals(0, maxErr, 4); } @Test public void testMath904() { final double x = -1; final double y = (5 + 1e-15) * 1e15; Assert.assertEquals(Math.pow(x, y), FastMath.pow(x, y), 0); Assert.assertEquals(Math.pow(x, -y), FastMath.pow(x, -y), 0); } @Test public void testMath905LargePositive() { final double start = StrictMath.log(Double.MAX_VALUE); final double endT = StrictMath.sqrt(2) * StrictMath.sqrt(Double.MAX_VALUE); final double end = 2 * StrictMath.log(endT); double maxErr = 0; for (double x = start; x < end; x += 1e-3) { final double tst = FastMath.cosh(x); final double ref = Math.cosh(x); maxErr = FastMath.max(maxErr, FastMath.abs(ref - tst) / FastMath.ulp(ref)); } Assert.assertEquals(0, maxErr, 3); for (double x = start; x < end; x += 1e-3) { final double tst = FastMath.sinh(x); final double ref = Math.sinh(x); maxErr = FastMath.max(maxErr, FastMath.abs(ref - tst) / FastMath.ulp(ref)); } Assert.assertEquals(0, maxErr, 3); } @Test public void testMath905LargeNegative() { final double start = -StrictMath.log(Double.MAX_VALUE); final double endT = StrictMath.sqrt(2) * StrictMath.sqrt(Double.MAX_VALUE); final double end = -2 * StrictMath.log(endT); double maxErr = 0; for (double x = start; x > end; x -= 1e-3) { final double tst = FastMath.cosh(x); final double ref = Math.cosh(x); maxErr = FastMath.max(maxErr, FastMath.abs(ref - tst) / FastMath.ulp(ref)); } Assert.assertEquals(0, maxErr, 3); for (double x = start; x > end; x -= 1e-3) { final double tst = FastMath.sinh(x); final double ref = Math.sinh(x); maxErr = FastMath.max(maxErr, FastMath.abs(ref - tst) / FastMath.ulp(ref)); } Assert.assertEquals(0, maxErr, 3); } @Test public void testHyperbolicInverses() { double maxErr = 0; for (double x = -30; x < 30; x += 0.01) { maxErr = FastMath.max(maxErr, FastMath.abs(x - FastMath.sinh(FastMath.asinh(x))) / (2 * FastMath.ulp(x))); } Assert.assertEquals(0, maxErr, 3); maxErr = 0; for (double x = 1; x < 30; x += 0.01) { maxErr = FastMath.max(maxErr, FastMath.abs(x - FastMath.cosh(FastMath.acosh(x))) / (2 * FastMath.ulp(x))); } Assert.assertEquals(0, maxErr, 2); maxErr = 0; for (double x = -1 + Precision.EPSILON; x < 1 - Precision.EPSILON; x += 0.0001) { maxErr = FastMath.max(maxErr, FastMath.abs(x - FastMath.tanh(FastMath.atanh(x))) / (2 * FastMath.ulp(x))); } Assert.assertEquals(0, maxErr, 2); } @Test public void testLogAccuracy() { double maxerrulp = 0.0; for (int i = 0; i < NUMBER_OF_TRIALS; i++) { double x = Math.exp(generator.nextDouble() * 1416.0 - 708.0) * generator.nextDouble(); // double x = generator.nextDouble()*2.0; double tst = FastMath.log(x); double ref = DfpMath.log(field.newDfp(x)).toDouble(); double err = (tst - ref) / ref; if (err != 0.0) { double ulp = Math.abs(ref - Double.longBitsToDouble((Double .doubleToLongBits(ref) ^ 1))); double errulp = field.newDfp(tst).subtract(DfpMath.log(field.newDfp(x))).divide(field.newDfp(ulp)).toDouble(); // System.out.println(x + "\t" + tst + "\t" + ref + "\t" + err + "\t" + errulp); maxerrulp = Math.max(maxerrulp, Math.abs(errulp)); } } Assert.assertTrue("log() had errors in excess of " + MAX_ERROR_ULP + " ULP", maxerrulp < MAX_ERROR_ULP); } @Test public void testLog10Accuracy() { double maxerrulp = 0.0; for (int i = 0; i < NUMBER_OF_TRIALS; i++) { double x = Math.exp(generator.nextDouble() * 1416.0 - 708.0) * generator.nextDouble(); // double x = generator.nextDouble()*2.0; double tst = FastMath.log10(x); double ref = DfpMath.log(field.newDfp(x)).divide(DfpMath.log(field.newDfp("10"))).toDouble(); double err = (tst - ref) / ref; if (err != 0.0) { double ulp = Math.abs(ref - Double.longBitsToDouble((Double.doubleToLongBits(ref) ^ 1))); double errulp = field.newDfp(tst).subtract(DfpMath.log(field.newDfp(x)).divide(DfpMath.log(field.newDfp("10")))).divide(field.newDfp(ulp)).toDouble(); // System.out.println(x + "\t" + tst + "\t" + ref + "\t" + err + "\t" + errulp); maxerrulp = Math.max(maxerrulp, Math.abs(errulp)); } } Assert.assertTrue("log10() had errors in excess of " + MAX_ERROR_ULP + " ULP", maxerrulp < MAX_ERROR_ULP); } @Test public void testLog1pAccuracy() { double maxerrulp = 0.0; for (int i = 0; i < NUMBER_OF_TRIALS; i++) { double x = Math.exp(generator.nextDouble() * 10.0 - 5.0) * generator.nextDouble(); // double x = generator.nextDouble()*2.0; double tst = FastMath.log1p(x); double ref = DfpMath.log(field.newDfp(x).add(field.getOne())).toDouble(); double err = (tst - ref) / ref; if (err != 0.0) { double ulp = Math.abs(ref - Double.longBitsToDouble((Double.doubleToLongBits(ref) ^ 1))); double errulp = field.newDfp(tst).subtract(DfpMath.log(field.newDfp(x).add(field.getOne()))).divide(field.newDfp(ulp)).toDouble(); // System.out.println(x + "\t" + tst + "\t" + ref + "\t" + err + "\t" + errulp); maxerrulp = Math.max(maxerrulp, Math.abs(errulp)); } } Assert.assertTrue("log1p() had errors in excess of " + MAX_ERROR_ULP + " ULP", maxerrulp < MAX_ERROR_ULP); } @Test public void testLog1pSpecialCases() { Assert.assertTrue("Logp of -1.0 should be -Inf", Double.isInfinite(FastMath.log1p(-1.0))); } @Test public void testLogSpecialCases() { Assert.assertTrue("Log of zero should be -Inf", Double.isInfinite(FastMath.log(0.0))); Assert.assertTrue("Log of -zero should be -Inf", Double.isInfinite(FastMath.log(-0.0))); Assert.assertTrue("Log of NaN should be NaN", Double.isNaN(FastMath.log(Double.NaN))); Assert.assertTrue("Log of negative number should be NaN", Double.isNaN(FastMath.log(-1.0))); Assert.assertEquals("Log of Double.MIN_VALUE should be -744.4400719213812", -744.4400719213812, FastMath.log(Double.MIN_VALUE), Precision.EPSILON); Assert.assertTrue("Log of infinity should be infinity", Double.isInfinite(FastMath.log(Double.POSITIVE_INFINITY))); } @Test public void testExpSpecialCases() { // Smallest value that will round up to Double.MIN_VALUE Assert.assertEquals(Double.MIN_VALUE, FastMath.exp(-745.1332191019411), Precision.EPSILON); Assert.assertEquals("exp(-745.1332191019412) should be 0.0", 0.0, FastMath.exp(-745.1332191019412), Precision.EPSILON); Assert.assertTrue("exp of NaN should be NaN", Double.isNaN(FastMath.exp(Double.NaN))); Assert.assertTrue("exp of infinity should be infinity", Double.isInfinite(FastMath.exp(Double.POSITIVE_INFINITY))); Assert.assertEquals("exp of -infinity should be 0.0", 0.0, FastMath.exp(Double.NEGATIVE_INFINITY), Precision.EPSILON); Assert.assertEquals("exp(1) should be Math.E", Math.E, FastMath.exp(1.0), Precision.EPSILON); } @Test public void testPowSpecialCases() { Assert.assertEquals("pow(-1, 0) should be 1.0", 1.0, FastMath.pow(-1.0, 0.0), Precision.EPSILON); Assert.assertEquals("pow(-1, -0) should be 1.0", 1.0, FastMath.pow(-1.0, -0.0), Precision.EPSILON); Assert.assertEquals("pow(PI, 1.0) should be PI", FastMath.PI, FastMath.pow(FastMath.PI, 1.0), Precision.EPSILON); Assert.assertEquals("pow(-PI, 1.0) should be -PI", -FastMath.PI, FastMath.pow(-FastMath.PI, 1.0), Precision.EPSILON); Assert.assertTrue("pow(PI, NaN) should be NaN", Double.isNaN(FastMath.pow(Math.PI, Double.NaN))); Assert.assertTrue("pow(NaN, PI) should be NaN", Double.isNaN(FastMath.pow(Double.NaN, Math.PI))); Assert.assertTrue("pow(2.0, Infinity) should be Infinity", Double.isInfinite(FastMath.pow(2.0, Double.POSITIVE_INFINITY))); Assert.assertTrue("pow(0.5, -Infinity) should be Infinity", Double.isInfinite(FastMath.pow(0.5, Double.NEGATIVE_INFINITY))); Assert.assertEquals("pow(0.5, Infinity) should be 0.0", 0.0, FastMath.pow(0.5, Double.POSITIVE_INFINITY), Precision.EPSILON); Assert.assertEquals("pow(2.0, -Infinity) should be 0.0", 0.0, FastMath.pow(2.0, Double.NEGATIVE_INFINITY), Precision.EPSILON); Assert.assertEquals("pow(0.0, 0.5) should be 0.0", 0.0, FastMath.pow(0.0, 0.5), Precision.EPSILON); Assert.assertEquals("pow(Infinity, -0.5) should be 0.0", 0.0, FastMath.pow(Double.POSITIVE_INFINITY, -0.5), Precision.EPSILON); Assert.assertTrue("pow(0.0, -0.5) should be Inf", Double.isInfinite(FastMath.pow(0.0, -0.5))); Assert.assertTrue("pow(Inf, 0.5) should be Inf", Double.isInfinite(FastMath.pow(Double.POSITIVE_INFINITY, 0.5))); Assert.assertTrue("pow(-0.0, -3.0) should be -Inf", Double.isInfinite(FastMath.pow(-0.0, -3.0))); Assert.assertTrue("pow(-Inf, -3.0) should be -Inf", Double.isInfinite(FastMath.pow(Double.NEGATIVE_INFINITY, 3.0))); Assert.assertTrue("pow(-0.0, -3.5) should be Inf", Double.isInfinite(FastMath.pow(-0.0, -3.5))); Assert.assertTrue("pow(Inf, 3.5) should be Inf", Double.isInfinite(FastMath.pow(Double.POSITIVE_INFINITY, 3.5))); Assert.assertEquals("pow(-2.0, 3.0) should be -8.0", -8.0, FastMath.pow(-2.0, 3.0), Precision.EPSILON); Assert.assertTrue("pow(-2.0, 3.5) should be NaN", Double.isNaN(FastMath.pow(-2.0, 3.5))); // Added tests for a 100% coverage Assert.assertTrue("pow(+Inf, NaN) should be NaN", Double.isNaN(FastMath.pow(Double.POSITIVE_INFINITY, Double.NaN))); Assert.assertTrue("pow(1.0, +Inf) should be NaN", Double.isNaN(FastMath.pow(1.0, Double.POSITIVE_INFINITY))); Assert.assertTrue("pow(-Inf, NaN) should be NaN", Double.isNaN(FastMath.pow(Double.NEGATIVE_INFINITY, Double.NaN))); Assert.assertEquals("pow(-Inf, -1.0) should be 0.0", 0.0, FastMath.pow(Double.NEGATIVE_INFINITY, -1.0), Precision.EPSILON); Assert.assertEquals("pow(-Inf, -2.0) should be 0.0", 0.0, FastMath.pow(Double.NEGATIVE_INFINITY, -2.0), Precision.EPSILON); Assert.assertTrue("pow(-Inf, 1.0) should be -Inf", Double.isInfinite(FastMath.pow(Double.NEGATIVE_INFINITY, 1.0))); Assert.assertTrue("pow(-Inf, 2.0) should be +Inf", Double.isInfinite(FastMath.pow(Double.NEGATIVE_INFINITY, 2.0))); Assert.assertTrue("pow(1.0, -Inf) should be NaN", Double.isNaN(FastMath.pow(1.0, Double.NEGATIVE_INFINITY))); } @Test public void testAtan2SpecialCases() { Assert.assertTrue("atan2(NaN, 0.0) should be NaN", Double.isNaN(FastMath.atan2(Double.NaN, 0.0))); Assert.assertTrue("atan2(0.0, NaN) should be NaN", Double.isNaN(FastMath.atan2(0.0, Double.NaN))); Assert.assertEquals("atan2(0.0, 0.0) should be 0.0", 0.0, FastMath.atan2(0.0, 0.0), Precision.EPSILON); Assert.assertEquals("atan2(0.0, 0.001) should be 0.0", 0.0, FastMath.atan2(0.0, 0.001), Precision.EPSILON); Assert.assertEquals("atan2(0.1, +Inf) should be 0.0", 0.0, FastMath.atan2(0.1, Double.POSITIVE_INFINITY), Precision.EPSILON); Assert.assertEquals("atan2(-0.0, 0.0) should be -0.0", -0.0, FastMath.atan2(-0.0, 0.0), Precision.EPSILON); Assert.assertEquals("atan2(-0.0, 0.001) should be -0.0", -0.0, FastMath.atan2(-0.0, 0.001), Precision.EPSILON); Assert.assertEquals("atan2(-0.0, +Inf) should be -0.0", -0.0, FastMath.atan2(-0.1, Double.POSITIVE_INFINITY), Precision.EPSILON); Assert.assertEquals("atan2(0.0, -0.0) should be PI", FastMath.PI, FastMath.atan2(0.0, -0.0), Precision.EPSILON); Assert.assertEquals("atan2(0.1, -Inf) should be PI", FastMath.PI, FastMath.atan2(0.1, Double.NEGATIVE_INFINITY), Precision.EPSILON); Assert.assertEquals("atan2(-0.0, -0.0) should be -PI", -FastMath.PI, FastMath.atan2(-0.0, -0.0), Precision.EPSILON); Assert.assertEquals("atan2(0.1, -Inf) should be -PI", -FastMath.PI, FastMath.atan2(-0.1, Double.NEGATIVE_INFINITY), Precision.EPSILON); Assert.assertEquals("atan2(0.1, 0.0) should be PI/2", FastMath.PI / 2.0, FastMath.atan2(0.1, 0.0), Precision.EPSILON); Assert.assertEquals("atan2(0.1, -0.0) should be PI/2", FastMath.PI / 2.0, FastMath.atan2(0.1, -0.0), Precision.EPSILON); Assert.assertEquals("atan2(Inf, 0.1) should be PI/2", FastMath.PI / 2.0, FastMath.atan2(Double.POSITIVE_INFINITY, 0.1), Precision.EPSILON); Assert.assertEquals("atan2(Inf, -0.1) should be PI/2", FastMath.PI / 2.0, FastMath.atan2(Double.POSITIVE_INFINITY, -0.1), Precision.EPSILON); Assert.assertEquals("atan2(-0.1, 0.0) should be -PI/2", -FastMath.PI / 2.0, FastMath.atan2(-0.1, 0.0), Precision.EPSILON); Assert.assertEquals("atan2(-0.1, -0.0) should be -PI/2", -FastMath.PI / 2.0, FastMath.atan2(-0.1, -0.0), Precision.EPSILON); Assert.assertEquals("atan2(-Inf, 0.1) should be -PI/2", -FastMath.PI / 2.0, FastMath.atan2(Double.NEGATIVE_INFINITY, 0.1), Precision.EPSILON); Assert.assertEquals("atan2(-Inf, -0.1) should be -PI/2", -FastMath.PI / 2.0, FastMath.atan2(Double.NEGATIVE_INFINITY, -0.1), Precision.EPSILON); Assert.assertEquals("atan2(Inf, Inf) should be PI/4", FastMath.PI / 4.0, FastMath.atan2(Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY), Precision.EPSILON); Assert.assertEquals("atan2(Inf, -Inf) should be PI * 3/4", FastMath.PI * 3.0 / 4.0, FastMath.atan2(Double.POSITIVE_INFINITY, Double.NEGATIVE_INFINITY), Precision.EPSILON); Assert.assertEquals("atan2(-Inf, Inf) should be -PI/4", -FastMath.PI / 4.0, FastMath.atan2(Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY), Precision.EPSILON); Assert.assertEquals("atan2(-Inf, -Inf) should be -PI * 3/4", - FastMath.PI * 3.0 / 4.0, FastMath.atan2(Double.NEGATIVE_INFINITY, Double.NEGATIVE_INFINITY), Precision.EPSILON); } @Test public void testPowAccuracy() { double maxerrulp = 0.0; for (int i = 0; i < NUMBER_OF_TRIALS; i++) { double x = (generator.nextDouble() * 2.0 + 0.25); double y = (generator.nextDouble() * 1200.0 - 600.0) * generator.nextDouble(); /* * double x = FastMath.floor(generator.nextDouble()*1024.0 - 512.0); double * y; if (x != 0) y = FastMath.floor(512.0 / FastMath.abs(x)); else * y = generator.nextDouble()*1200.0; y = y - y/2; x = FastMath.pow(2.0, x) * * generator.nextDouble(); y = y * generator.nextDouble(); */ // double x = generator.nextDouble()*2.0; double tst = FastMath.pow(x, y); double ref = DfpMath.pow(field.newDfp(x), field.newDfp(y)).toDouble(); double err = (tst - ref) / ref; if (err != 0) { double ulp = Math.abs(ref - Double.longBitsToDouble((Double .doubleToLongBits(ref) ^ 1))); double errulp = field.newDfp(tst).subtract(DfpMath.pow(field.newDfp(x), field.newDfp(y))).divide(field.newDfp(ulp)).toDouble(); // System.out.println(x + "\t" + y + "\t" + tst + "\t" + ref + "\t" + err + "\t" + errulp); maxerrulp = Math.max(maxerrulp, Math.abs(errulp)); } } Assert.assertTrue("pow() had errors in excess of " + MAX_ERROR_ULP + " ULP", maxerrulp < MAX_ERROR_ULP); } @Test public void testExpAccuracy() { double maxerrulp = 0.0; for (int i = 0; i < NUMBER_OF_TRIALS; i++) { /* double x = 1.0 + i/1024.0/2.0; */ double x = ((generator.nextDouble() * 1416.0) - 708.0) * generator.nextDouble(); // double x = (generator.nextDouble() * 20.0) - 10.0; // double x = ((generator.nextDouble() * 2.0) - 1.0) * generator.nextDouble(); /* double x = 3.0 / 512.0 * i - 3.0; */ double tst = FastMath.exp(x); double ref = DfpMath.exp(field.newDfp(x)).toDouble(); double err = (tst - ref) / ref; if (err != 0) { double ulp = Math.abs(ref - Double.longBitsToDouble((Double.doubleToLongBits(ref) ^ 1))); double errulp = field.newDfp(tst).subtract(DfpMath.exp(field.newDfp(x))).divide(field.newDfp(ulp)).toDouble(); // System.out.println(x + "\t" + tst + "\t" + ref + "\t" + err + "\t" + errulp); maxerrulp = Math.max(maxerrulp, Math.abs(errulp)); } } Assert.assertTrue("exp() had errors in excess of " + MAX_ERROR_ULP + " ULP", maxerrulp < MAX_ERROR_ULP); } @Test public void testSinAccuracy() { double maxerrulp = 0.0; for (int i = 0; i < NUMBER_OF_TRIALS; i++) { /* double x = 1.0 + i/1024.0/2.0; */ // double x = ((generator.nextDouble() * 1416.0) - 708.0) * generator.nextDouble(); double x = ((generator.nextDouble() * Math.PI) - Math.PI / 2.0) * Math.pow(2, 21) * generator.nextDouble(); // double x = (generator.nextDouble() * 20.0) - 10.0; // double x = ((generator.nextDouble() * 2.0) - 1.0) * generator.nextDouble(); /* double x = 3.0 / 512.0 * i - 3.0; */ double tst = FastMath.sin(x); double ref = DfpMath.sin(field.newDfp(x)).toDouble(); double err = (tst - ref) / ref; if (err != 0) { double ulp = Math.abs(ref - Double.longBitsToDouble((Double.doubleToLongBits(ref) ^ 1))); double errulp = field.newDfp(tst).subtract(DfpMath.sin(field.newDfp(x))).divide(field.newDfp(ulp)).toDouble(); // System.out.println(x + "\t" + tst + "\t" + ref + "\t" + err + "\t" + errulp); maxerrulp = Math.max(maxerrulp, Math.abs(errulp)); } } Assert.assertTrue("sin() had errors in excess of " + MAX_ERROR_ULP + " ULP", maxerrulp < MAX_ERROR_ULP); } @Test public void testCosAccuracy() { double maxerrulp = 0.0; for (int i = 0; i < NUMBER_OF_TRIALS; i++) { /* double x = 1.0 + i/1024.0/2.0; */ // double x = ((generator.nextDouble() * 1416.0) - 708.0) * generator.nextDouble(); double x = ((generator.nextDouble() * Math.PI) - Math.PI / 2.0) * Math.pow(2, 21) * generator.nextDouble(); // double x = (generator.nextDouble() * 20.0) - 10.0; // double x = ((generator.nextDouble() * 2.0) - 1.0) * generator.nextDouble(); /* double x = 3.0 / 512.0 * i - 3.0; */ double tst = FastMath.cos(x); double ref = DfpMath.cos(field.newDfp(x)).toDouble(); double err = (tst - ref) / ref; if (err != 0) { double ulp = Math.abs(ref - Double.longBitsToDouble((Double.doubleToLongBits(ref) ^ 1))); double errulp = field.newDfp(tst).subtract(DfpMath.cos(field.newDfp(x))).divide(field.newDfp(ulp)).toDouble(); // System.out.println(x + "\t" + tst + "\t" + ref + "\t" + err + "\t" + errulp); maxerrulp = Math.max(maxerrulp, Math.abs(errulp)); } } Assert.assertTrue("cos() had errors in excess of " + MAX_ERROR_ULP + " ULP", maxerrulp < MAX_ERROR_ULP); } @Test public void testTanAccuracy() { double maxerrulp = 0.0; for (int i = 0; i < NUMBER_OF_TRIALS; i++) { /* double x = 1.0 + i/1024.0/2.0; */ // double x = ((generator.nextDouble() * 1416.0) - 708.0) * generator.nextDouble(); double x = ((generator.nextDouble() * Math.PI) - Math.PI / 2.0) * Math.pow(2, 12) * generator.nextDouble(); // double x = (generator.nextDouble() * 20.0) - 10.0; // double x = ((generator.nextDouble() * 2.0) - 1.0) * generator.nextDouble(); /* double x = 3.0 / 512.0 * i - 3.0; */ double tst = FastMath.tan(x); double ref = DfpMath.tan(field.newDfp(x)).toDouble(); double err = (tst - ref) / ref; if (err != 0) { double ulp = Math.abs(ref - Double.longBitsToDouble((Double.doubleToLongBits(ref) ^ 1))); double errulp = field.newDfp(tst).subtract(DfpMath.tan(field.newDfp(x))).divide(field.newDfp(ulp)).toDouble(); // System.out.println(x + "\t" + tst + "\t" + ref + "\t" + err + "\t" + errulp); maxerrulp = Math.max(maxerrulp, Math.abs(errulp)); } } Assert.assertTrue("tan() had errors in excess of " + MAX_ERROR_ULP + " ULP", maxerrulp < MAX_ERROR_ULP); } @Test public void testAtanAccuracy() { double maxerrulp = 0.0; for (int i = 0; i < NUMBER_OF_TRIALS; i++) { /* double x = 1.0 + i/1024.0/2.0; */ // double x = ((generator.nextDouble() * 1416.0) - 708.0) * generator.nextDouble(); // double x = ((generator.nextDouble() * Math.PI) - Math.PI/2.0) * // generator.nextDouble(); double x = ((generator.nextDouble() * 16.0) - 8.0) * generator.nextDouble(); // double x = (generator.nextDouble() * 20.0) - 10.0; // double x = ((generator.nextDouble() * 2.0) - 1.0) * generator.nextDouble(); /* double x = 3.0 / 512.0 * i - 3.0; */ double tst = FastMath.atan(x); double ref = DfpMath.atan(field.newDfp(x)).toDouble(); double err = (tst - ref) / ref; if (err != 0) { double ulp = Math.abs(ref - Double.longBitsToDouble((Double.doubleToLongBits(ref) ^ 1))); double errulp = field.newDfp(tst).subtract(DfpMath.atan(field.newDfp(x))).divide(field.newDfp(ulp)).toDouble(); // System.out.println(x + "\t" + tst + "\t" + ref + "\t" + err + "\t" + errulp); maxerrulp = Math.max(maxerrulp, Math.abs(errulp)); } } Assert.assertTrue("atan() had errors in excess of " + MAX_ERROR_ULP + " ULP", maxerrulp < MAX_ERROR_ULP); } @Test public void testAtan2Accuracy() { double maxerrulp = 0.0; for (int i = 0; i < NUMBER_OF_TRIALS; i++) { /* double x = 1.0 + i/1024.0/2.0; */ // double x = ((generator.nextDouble() * 1416.0) - 708.0) * generator.nextDouble(); double x = generator.nextDouble() - 0.5; double y = generator.nextDouble() - 0.5; // double x = (generator.nextDouble() * 20.0) - 10.0; // double x = ((generator.nextDouble() * 2.0) - 1.0) * generator.nextDouble(); /* double x = 3.0 / 512.0 * i - 3.0; */ double tst = FastMath.atan2(y, x); Dfp refdfp = DfpMath.atan(field.newDfp(y) .divide(field.newDfp(x))); /* Make adjustments for sign */ if (x < 0.0) { if (y > 0.0) refdfp = field.getPi().add(refdfp); else refdfp = refdfp.subtract(field.getPi()); } double ref = refdfp.toDouble(); double err = (tst - ref) / ref; if (err != 0) { double ulp = Math.abs(ref - Double.longBitsToDouble((Double .doubleToLongBits(ref) ^ 1))); double errulp = field.newDfp(tst).subtract(refdfp).divide(field.newDfp(ulp)).toDouble(); // System.out.println(x + "\t" + y + "\t" + tst + "\t" + ref + "\t" + errulp); maxerrulp = Math.max(maxerrulp, Math.abs(errulp)); } } Assert.assertTrue("atan2() had errors in excess of " + MAX_ERROR_ULP + " ULP", maxerrulp < MAX_ERROR_ULP); } @Test public void testExpm1Accuracy() { double maxerrulp = 0.0; for (int i = 0; i < NUMBER_OF_TRIALS; i++) { /* double x = 1.0 + i/1024.0/2.0; */ // double x = (generator.nextDouble() * 20.0) - 10.0; double x = ((generator.nextDouble() * 16.0) - 8.0) * generator.nextDouble(); /* double x = 3.0 / 512.0 * i - 3.0; */ double tst = FastMath.expm1(x); double ref = DfpMath.exp(field.newDfp(x)).subtract(field.getOne()).toDouble(); double err = (tst - ref) / ref; if (err != 0) { double ulp = Math.abs(ref - Double.longBitsToDouble((Double .doubleToLongBits(ref) ^ 1))); double errulp = field.newDfp(tst).subtract(DfpMath.exp(field.newDfp(x)).subtract(field.getOne())).divide(field.newDfp(ulp)).toDouble(); // System.out.println(x + "\t" + tst + "\t" + ref + "\t" + err + "\t" + errulp); maxerrulp = Math.max(maxerrulp, Math.abs(errulp)); } } Assert.assertTrue("expm1() had errors in excess of " + MAX_ERROR_ULP + " ULP", maxerrulp < MAX_ERROR_ULP); } @Test public void testAsinAccuracy() { double maxerrulp = 0.0; for (int i=0; i<10000; i++) { double x = ((generator.nextDouble() * 2.0) - 1.0) * generator.nextDouble(); double tst = FastMath.asin(x); double ref = DfpMath.asin(field.newDfp(x)).toDouble(); double err = (tst - ref) / ref; if (err != 0) { double ulp = Math.abs(ref - Double.longBitsToDouble((Double.doubleToLongBits(ref) ^ 1))); double errulp = field.newDfp(tst).subtract(DfpMath.asin(field.newDfp(x))).divide(field.newDfp(ulp)).toDouble(); //System.out.println(x+"\t"+tst+"\t"+ref+"\t"+err+"\t"+errulp); maxerrulp = Math.max(maxerrulp, Math.abs(errulp)); } } Assert.assertTrue("asin() had errors in excess of " + MAX_ERROR_ULP + " ULP", maxerrulp < MAX_ERROR_ULP); } @Test public void testAcosAccuracy() { double maxerrulp = 0.0; for (int i=0; i<10000; i++) { double x = ((generator.nextDouble() * 2.0) - 1.0) * generator.nextDouble(); double tst = FastMath.acos(x); double ref = DfpMath.acos(field.newDfp(x)).toDouble(); double err = (tst - ref) / ref; if (err != 0) { double ulp = Math.abs(ref - Double.longBitsToDouble((Double.doubleToLongBits(ref) ^ 1))); double errulp = field.newDfp(tst).subtract(DfpMath.acos(field.newDfp(x))).divide(field.newDfp(ulp)).toDouble(); //System.out.println(x+"\t"+tst+"\t"+ref+"\t"+err+"\t"+errulp); maxerrulp = Math.max(maxerrulp, Math.abs(errulp)); } } Assert.assertTrue("acos() had errors in excess of " + MAX_ERROR_ULP + " ULP", maxerrulp < MAX_ERROR_ULP); } /** * Added tests for a 100% coverage of acos(). */ @Test public void testAcosSpecialCases() { Assert.assertTrue("acos(NaN) should be NaN", Double.isNaN(FastMath.acos(Double.NaN))); Assert.assertTrue("acos(-1.1) should be NaN", Double.isNaN(FastMath.acos(-1.1))); Assert.assertTrue("acos(-1.1) should be NaN", Double.isNaN(FastMath.acos(1.1))); Assert.assertEquals("acos(-1.0) should be PI", FastMath.acos(-1.0), FastMath.PI, Precision.EPSILON); Assert.assertEquals("acos(1.0) should be 0.0", FastMath.acos(1.0), 0.0, Precision.EPSILON); Assert.assertEquals("acos(0.0) should be PI/2", FastMath.acos(0.0), FastMath.PI / 2.0, Precision.EPSILON); } /** * Added tests for a 100% coverage of asin(). */ @Test public void testAsinSpecialCases() { Assert.assertTrue("asin(NaN) should be NaN", Double.isNaN(FastMath.asin(Double.NaN))); Assert.assertTrue("asin(1.1) should be NaN", Double.isNaN(FastMath.asin(1.1))); Assert.assertTrue("asin(-1.1) should be NaN", Double.isNaN(FastMath.asin(-1.1))); Assert.assertEquals("asin(1.0) should be PI/2", FastMath.asin(1.0), FastMath.PI / 2.0, Precision.EPSILON); Assert.assertEquals("asin(-1.0) should be -PI/2", FastMath.asin(-1.0), -FastMath.PI / 2.0, Precision.EPSILON); Assert.assertEquals("asin(0.0) should be 0.0", FastMath.asin(0.0), 0.0, Precision.EPSILON); } private Dfp cosh(Dfp x) { return DfpMath.exp(x).add(DfpMath.exp(x.negate())).divide(2); } private Dfp sinh(Dfp x) { return DfpMath.exp(x).subtract(DfpMath.exp(x.negate())).divide(2); } private Dfp tanh(Dfp x) { return sinh(x).divide(cosh(x)); } @Test public void testSinhAccuracy() { double maxerrulp = 0.0; for (int i=0; i<10000; i++) { double x = ((generator.nextDouble() * 16.0) - 8.0) * generator.nextDouble(); double tst = FastMath.sinh(x); double ref = sinh(field.newDfp(x)).toDouble(); double err = (tst - ref) / ref; if (err != 0) { double ulp = Math.abs(ref - Double.longBitsToDouble((Double.doubleToLongBits(ref) ^ 1))); double errulp = field.newDfp(tst).subtract(sinh(field.newDfp(x))).divide(field.newDfp(ulp)).toDouble(); //System.out.println(x+"\t"+tst+"\t"+ref+"\t"+err+"\t"+errulp); maxerrulp = Math.max(maxerrulp, Math.abs(errulp)); } } Assert.assertTrue("sinh() had errors in excess of " + MAX_ERROR_ULP + " ULP", maxerrulp < MAX_ERROR_ULP); } @Test public void testCoshAccuracy() { double maxerrulp = 0.0; for (int i=0; i<10000; i++) { double x = ((generator.nextDouble() * 16.0) - 8.0) * generator.nextDouble(); double tst = FastMath.cosh(x); double ref = cosh(field.newDfp(x)).toDouble(); double err = (tst - ref) / ref; if (err != 0) { double ulp = Math.abs(ref - Double.longBitsToDouble((Double.doubleToLongBits(ref) ^ 1))); double errulp = field.newDfp(tst).subtract(cosh(field.newDfp(x))).divide(field.newDfp(ulp)).toDouble(); //System.out.println(x+"\t"+tst+"\t"+ref+"\t"+err+"\t"+errulp); maxerrulp = Math.max(maxerrulp, Math.abs(errulp)); } } Assert.assertTrue("cosh() had errors in excess of " + MAX_ERROR_ULP + " ULP", maxerrulp < MAX_ERROR_ULP); } @Test public void testTanhAccuracy() { double maxerrulp = 0.0; for (int i=0; i<10000; i++) { double x = ((generator.nextDouble() * 16.0) - 8.0) * generator.nextDouble(); double tst = FastMath.tanh(x); double ref = tanh(field.newDfp(x)).toDouble(); double err = (tst - ref) / ref; if (err != 0) { double ulp = Math.abs(ref - Double.longBitsToDouble((Double.doubleToLongBits(ref) ^ 1))); double errulp = field.newDfp(tst).subtract(tanh(field.newDfp(x))).divide(field.newDfp(ulp)).toDouble(); //System.out.println(x+"\t"+tst+"\t"+ref+"\t"+err+"\t"+errulp); maxerrulp = Math.max(maxerrulp, Math.abs(errulp)); } } Assert.assertTrue("tanh() had errors in excess of " + MAX_ERROR_ULP + " ULP", maxerrulp < MAX_ERROR_ULP); } @Test public void testCbrtAccuracy() { double maxerrulp = 0.0; for (int i=0; i<10000; i++) { double x = ((generator.nextDouble() * 200.0) - 100.0) * generator.nextDouble(); double tst = FastMath.cbrt(x); double ref = cbrt(field.newDfp(x)).toDouble(); double err = (tst - ref) / ref; if (err != 0) { double ulp = Math.abs(ref - Double.longBitsToDouble((Double.doubleToLongBits(ref) ^ 1))); double errulp = field.newDfp(tst).subtract(cbrt(field.newDfp(x))).divide(field.newDfp(ulp)).toDouble(); //System.out.println(x+"\t"+tst+"\t"+ref+"\t"+err+"\t"+errulp); maxerrulp = Math.max(maxerrulp, Math.abs(errulp)); } } Assert.assertTrue("cbrt() had errors in excess of " + MAX_ERROR_ULP + " ULP", maxerrulp < MAX_ERROR_ULP); } private Dfp cbrt(Dfp x) { boolean negative=false; if (x.lessThan(field.getZero())) { negative = true; x = x.negate(); } Dfp y = DfpMath.pow(x, field.getOne().divide(3)); if (negative) { y = y.negate(); } return y; } @Test public void testToDegrees() { double maxerrulp = 0.0; for (int i = 0; i < NUMBER_OF_TRIALS; i++) { double x = generator.nextDouble(); double tst = field.newDfp(x).multiply(180).divide(field.getPi()).toDouble(); double ref = FastMath.toDegrees(x); double err = (tst - ref) / ref; if (err != 0) { double ulp = Math.abs(ref - Double.longBitsToDouble((Double.doubleToLongBits(ref) ^ 1))); double errulp = field.newDfp(tst).subtract(DfpMath.exp(field.newDfp(x)).subtract(field.getOne())).divide(field.newDfp(ulp)).toDouble(); // System.out.println(x + "\t" + tst + "\t" + ref + "\t" + err + "\t" + errulp); maxerrulp = Math.max(maxerrulp, Math.abs(errulp)); } } Assert.assertTrue("toDegrees() had errors in excess of " + MAX_ERROR_ULP + " ULP", maxerrulp < MAX_ERROR_ULP); } @Test public void testToRadians() { double maxerrulp = 0.0; for (int i = 0; i < NUMBER_OF_TRIALS; i++) { double x = generator.nextDouble(); double tst = field.newDfp(x).multiply(field.getPi()).divide(180).toDouble(); double ref = FastMath.toRadians(x); double err = (tst - ref) / ref; if (err != 0) { double ulp = Math.abs(ref - Double.longBitsToDouble((Double .doubleToLongBits(ref) ^ 1))); double errulp = field.newDfp(tst).subtract(DfpMath.exp(field.newDfp(x)).subtract(field.getOne())).divide(field.newDfp(ulp)).toDouble(); // System.out.println(x + "\t" + tst + "\t" + ref + "\t" + err + "\t" + errulp); maxerrulp = Math.max(maxerrulp, Math.abs(errulp)); } } Assert.assertTrue("toRadians() had errors in excess of " + MAX_ERROR_ULP + " ULP", maxerrulp < MAX_ERROR_ULP); } @Test public void testNextAfter() { // 0x402fffffffffffff 0x404123456789abcd -> 4030000000000000 Assert.assertEquals(16.0, FastMath.nextAfter(15.999999999999998, 34.27555555555555), 0.0); // 0xc02fffffffffffff 0x404123456789abcd -> c02ffffffffffffe Assert.assertEquals(-15.999999999999996, FastMath.nextAfter(-15.999999999999998, 34.27555555555555), 0.0); // 0x402fffffffffffff 0x400123456789abcd -> 402ffffffffffffe Assert.assertEquals(15.999999999999996, FastMath.nextAfter(15.999999999999998, 2.142222222222222), 0.0); // 0xc02fffffffffffff 0x400123456789abcd -> c02ffffffffffffe Assert.assertEquals(-15.999999999999996, FastMath.nextAfter(-15.999999999999998, 2.142222222222222), 0.0); // 0x4020000000000000 0x404123456789abcd -> 4020000000000001 Assert.assertEquals(8.000000000000002, FastMath.nextAfter(8.0, 34.27555555555555), 0.0); // 0xc020000000000000 0x404123456789abcd -> c01fffffffffffff Assert.assertEquals(-7.999999999999999, FastMath.nextAfter(-8.0, 34.27555555555555), 0.0); // 0x4020000000000000 0x400123456789abcd -> 401fffffffffffff Assert.assertEquals(7.999999999999999, FastMath.nextAfter(8.0, 2.142222222222222), 0.0); // 0xc020000000000000 0x400123456789abcd -> c01fffffffffffff Assert.assertEquals(-7.999999999999999, FastMath.nextAfter(-8.0, 2.142222222222222), 0.0); // 0x3f2e43753d36a223 0x3f2e43753d36a224 -> 3f2e43753d36a224 Assert.assertEquals(2.308922399667661E-4, FastMath.nextAfter(2.3089223996676606E-4, 2.308922399667661E-4), 0.0); // 0x3f2e43753d36a223 0x3f2e43753d36a223 -> 3f2e43753d36a223 Assert.assertEquals(2.3089223996676606E-4, FastMath.nextAfter(2.3089223996676606E-4, 2.3089223996676606E-4), 0.0); // 0x3f2e43753d36a223 0x3f2e43753d36a222 -> 3f2e43753d36a222 Assert.assertEquals(2.3089223996676603E-4, FastMath.nextAfter(2.3089223996676606E-4, 2.3089223996676603E-4), 0.0); // 0x3f2e43753d36a223 0xbf2e43753d36a224 -> 3f2e43753d36a222 Assert.assertEquals(2.3089223996676603E-4, FastMath.nextAfter(2.3089223996676606E-4, -2.308922399667661E-4), 0.0); // 0x3f2e43753d36a223 0xbf2e43753d36a223 -> 3f2e43753d36a222 Assert.assertEquals(2.3089223996676603E-4, FastMath.nextAfter(2.3089223996676606E-4, -2.3089223996676606E-4), 0.0); // 0x3f2e43753d36a223 0xbf2e43753d36a222 -> 3f2e43753d36a222 Assert.assertEquals(2.3089223996676603E-4, FastMath.nextAfter(2.3089223996676606E-4, -2.3089223996676603E-4), 0.0); // 0xbf2e43753d36a223 0x3f2e43753d36a224 -> bf2e43753d36a222 Assert.assertEquals(-2.3089223996676603E-4, FastMath.nextAfter(-2.3089223996676606E-4, 2.308922399667661E-4), 0.0); // 0xbf2e43753d36a223 0x3f2e43753d36a223 -> bf2e43753d36a222 Assert.assertEquals(-2.3089223996676603E-4, FastMath.nextAfter(-2.3089223996676606E-4, 2.3089223996676606E-4), 0.0); // 0xbf2e43753d36a223 0x3f2e43753d36a222 -> bf2e43753d36a222 Assert.assertEquals(-2.3089223996676603E-4, FastMath.nextAfter(-2.3089223996676606E-4, 2.3089223996676603E-4), 0.0); // 0xbf2e43753d36a223 0xbf2e43753d36a224 -> bf2e43753d36a224 Assert.assertEquals(-2.308922399667661E-4, FastMath.nextAfter(-2.3089223996676606E-4, -2.308922399667661E-4), 0.0); // 0xbf2e43753d36a223 0xbf2e43753d36a223 -> bf2e43753d36a223 Assert.assertEquals(-2.3089223996676606E-4, FastMath.nextAfter(-2.3089223996676606E-4, -2.3089223996676606E-4), 0.0); // 0xbf2e43753d36a223 0xbf2e43753d36a222 -> bf2e43753d36a222 Assert.assertEquals(-2.3089223996676603E-4, FastMath.nextAfter(-2.3089223996676606E-4, -2.3089223996676603E-4), 0.0); } @Test public void testDoubleNextAfterSpecialCases() { Assert.assertEquals(-Double.MAX_VALUE,FastMath.nextAfter(Double.NEGATIVE_INFINITY, 0D), 0D); Assert.assertEquals(Double.MAX_VALUE,FastMath.nextAfter(Double.POSITIVE_INFINITY, 0D), 0D); Assert.assertEquals(Double.NaN,FastMath.nextAfter(Double.NaN, 0D), 0D); Assert.assertEquals(Double.POSITIVE_INFINITY,FastMath.nextAfter(Double.MAX_VALUE, Double.POSITIVE_INFINITY), 0D); Assert.assertEquals(Double.NEGATIVE_INFINITY,FastMath.nextAfter(-Double.MAX_VALUE, Double.NEGATIVE_INFINITY), 0D); Assert.assertEquals(Double.MIN_VALUE, FastMath.nextAfter(0D, 1D), 0D); Assert.assertEquals(-Double.MIN_VALUE, FastMath.nextAfter(0D, -1D), 0D); Assert.assertEquals(0D, FastMath.nextAfter(Double.MIN_VALUE, -1), 0D); Assert.assertEquals(0D, FastMath.nextAfter(-Double.MIN_VALUE, 1), 0D); } @Test public void testFloatNextAfterSpecialCases() { Assert.assertEquals(-Float.MAX_VALUE,FastMath.nextAfter(Float.NEGATIVE_INFINITY, 0F), 0F); Assert.assertEquals(Float.MAX_VALUE,FastMath.nextAfter(Float.POSITIVE_INFINITY, 0F), 0F); Assert.assertEquals(Float.NaN,FastMath.nextAfter(Float.NaN, 0F), 0F); Assert.assertEquals(Float.POSITIVE_INFINITY,FastMath.nextAfter(Float.MAX_VALUE, Float.POSITIVE_INFINITY), 0F); Assert.assertEquals(Float.NEGATIVE_INFINITY,FastMath.nextAfter(-Float.MAX_VALUE, Float.NEGATIVE_INFINITY), 0F); Assert.assertEquals(Float.MIN_VALUE, FastMath.nextAfter(0F, 1F), 0F); Assert.assertEquals(-Float.MIN_VALUE, FastMath.nextAfter(0F, -1F), 0F); Assert.assertEquals(0F, FastMath.nextAfter(Float.MIN_VALUE, -1F), 0F); Assert.assertEquals(0F, FastMath.nextAfter(-Float.MIN_VALUE, 1F), 0F); } @Test public void testDoubleScalbSpecialCases() { Assert.assertEquals(2.5269841324701218E-175, FastMath.scalb(2.2250738585072014E-308, 442), 0D); Assert.assertEquals(1.307993905256674E297, FastMath.scalb(1.1102230246251565E-16, 1040), 0D); Assert.assertEquals(7.2520887996488946E-217, FastMath.scalb(Double.MIN_VALUE, 356), 0D); Assert.assertEquals(8.98846567431158E307, FastMath.scalb(Double.MIN_VALUE, 2097), 0D); Assert.assertEquals(Double.POSITIVE_INFINITY, FastMath.scalb(Double.MIN_VALUE, 2098), 0D); Assert.assertEquals(1.1125369292536007E-308, FastMath.scalb(2.225073858507201E-308, -1), 0D); Assert.assertEquals(1.0E-323, FastMath.scalb(Double.MAX_VALUE, -2097), 0D); Assert.assertEquals(Double.MIN_VALUE, FastMath.scalb(Double.MAX_VALUE, -2098), 0D); Assert.assertEquals(0, FastMath.scalb(Double.MAX_VALUE, -2099), 0D); Assert.assertEquals(Double.POSITIVE_INFINITY, FastMath.scalb(Double.POSITIVE_INFINITY, -1000000), 0D); Assert.assertEquals(Double.NEGATIVE_INFINITY, FastMath.scalb(-1.1102230246251565E-16, 1078), 0D); Assert.assertEquals(Double.NEGATIVE_INFINITY, FastMath.scalb(-1.1102230246251565E-16, 1079), 0D); Assert.assertEquals(Double.NEGATIVE_INFINITY, FastMath.scalb(-2.2250738585072014E-308, 2047), 0D); Assert.assertEquals(Double.NEGATIVE_INFINITY, FastMath.scalb(-2.2250738585072014E-308, 2048), 0D); Assert.assertEquals(Double.NEGATIVE_INFINITY, FastMath.scalb(-1.7976931348623157E308, 2147483647), 0D); Assert.assertEquals(Double.POSITIVE_INFINITY, FastMath.scalb( 1.7976931348623157E308, 2147483647), 0D); Assert.assertEquals(Double.NEGATIVE_INFINITY, FastMath.scalb(-1.1102230246251565E-16, 2147483647), 0D); Assert.assertEquals(Double.POSITIVE_INFINITY, FastMath.scalb( 1.1102230246251565E-16, 2147483647), 0D); Assert.assertEquals(Double.NEGATIVE_INFINITY, FastMath.scalb(-2.2250738585072014E-308, 2147483647), 0D); Assert.assertEquals(Double.POSITIVE_INFINITY, FastMath.scalb( 2.2250738585072014E-308, 2147483647), 0D); } @Test public void testFloatScalbSpecialCases() { Assert.assertEquals(0f, FastMath.scalb(Float.MIN_VALUE, -30), 0F); Assert.assertEquals(2 * Float.MIN_VALUE, FastMath.scalb(Float.MIN_VALUE, 1), 0F); Assert.assertEquals(7.555786e22f, FastMath.scalb(Float.MAX_VALUE, -52), 0F); Assert.assertEquals(1.7014118e38f, FastMath.scalb(Float.MIN_VALUE, 276), 0F); Assert.assertEquals(Float.POSITIVE_INFINITY, FastMath.scalb(Float.MIN_VALUE, 277), 0F); Assert.assertEquals(5.8774718e-39f, FastMath.scalb(1.1754944e-38f, -1), 0F); Assert.assertEquals(2 * Float.MIN_VALUE, FastMath.scalb(Float.MAX_VALUE, -276), 0F); Assert.assertEquals(Float.MIN_VALUE, FastMath.scalb(Float.MAX_VALUE, -277), 0F); Assert.assertEquals(0, FastMath.scalb(Float.MAX_VALUE, -278), 0F); Assert.assertEquals(Float.POSITIVE_INFINITY, FastMath.scalb(Float.POSITIVE_INFINITY, -1000000), 0F); Assert.assertEquals(-3.13994498e38f, FastMath.scalb(-1.1e-7f, 151), 0F); Assert.assertEquals(Float.NEGATIVE_INFINITY, FastMath.scalb(-1.1e-7f, 152), 0F); Assert.assertEquals(Float.POSITIVE_INFINITY, FastMath.scalb(3.4028235E38f, 2147483647), 0F); Assert.assertEquals(Float.NEGATIVE_INFINITY, FastMath.scalb(-3.4028235E38f, 2147483647), 0F); } private boolean compareClassMethods(Class class1, Class class2){ boolean allfound = true; for(Method method1 : class1.getDeclaredMethods()){ if (Modifier.isPublic(method1.getModifiers())){ Type []params = method1.getGenericParameterTypes(); try { class2.getDeclaredMethod(method1.getName(), (Class[]) params); } catch (NoSuchMethodException e) { allfound = false; System.out.println(class2.getSimpleName()+" does not implement: "+method1); } } } return allfound; } @Test public void checkMissingFastMathClasses() { boolean ok = compareClassMethods(StrictMath.class, FastMath.class); Assert.assertTrue("FastMath should implement all StrictMath methods", ok); } @Ignore @Test public void checkExtraFastMathClasses() { compareClassMethods( FastMath.class, StrictMath.class); } @Test public void testSignumDouble() { final double delta = 0.0; Assert.assertEquals(1.0, FastMath.signum(2.0), delta); Assert.assertEquals(0.0, FastMath.signum(0.0), delta); Assert.assertEquals(-1.0, FastMath.signum(-2.0), delta); TestUtils.assertSame(-0. / 0., FastMath.signum(Double.NaN)); } @Test public void testSignumFloat() { final float delta = 0.0F; Assert.assertEquals(1.0F, FastMath.signum(2.0F), delta); Assert.assertEquals(0.0F, FastMath.signum(0.0F), delta); Assert.assertEquals(-1.0F, FastMath.signum(-2.0F), delta); TestUtils.assertSame(Float.NaN, FastMath.signum(Float.NaN)); } @Test public void testLogWithBase() { Assert.assertEquals(2.0, FastMath.log(2, 4), 0); Assert.assertEquals(3.0, FastMath.log(2, 8), 0); Assert.assertTrue(Double.isNaN(FastMath.log(-1, 1))); Assert.assertTrue(Double.isNaN(FastMath.log(1, -1))); Assert.assertTrue(Double.isNaN(FastMath.log(0, 0))); Assert.assertEquals(0, FastMath.log(0, 10), 0); Assert.assertEquals(Double.NEGATIVE_INFINITY, FastMath.log(10, 0), 0); } @Test public void testIndicatorDouble() { double delta = 0.0; Assert.assertEquals(1.0, FastMath.copySign(1d, 2.0), delta); Assert.assertEquals(1.0, FastMath.copySign(1d, 0.0), delta); Assert.assertEquals(-1.0, FastMath.copySign(1d, -0.0), delta); Assert.assertEquals(1.0, FastMath.copySign(1d, Double.POSITIVE_INFINITY), delta); Assert.assertEquals(-1.0, FastMath.copySign(1d, Double.NEGATIVE_INFINITY), delta); Assert.assertEquals(1.0, FastMath.copySign(1d, Double.NaN), delta); Assert.assertEquals(-1.0, FastMath.copySign(1d, -2.0), delta); } @Test public void testIndicatorFloat() { float delta = 0.0F; Assert.assertEquals(1.0F, FastMath.copySign(1d, 2.0F), delta); Assert.assertEquals(1.0F, FastMath.copySign(1d, 0.0F), delta); Assert.assertEquals(-1.0F, FastMath.copySign(1d, -0.0F), delta); Assert.assertEquals(1.0F, FastMath.copySign(1d, Float.POSITIVE_INFINITY), delta); Assert.assertEquals(-1.0F, FastMath.copySign(1d, Float.NEGATIVE_INFINITY), delta); Assert.assertEquals(1.0F, FastMath.copySign(1d, Float.NaN), delta); Assert.assertEquals(-1.0F, FastMath.copySign(1d, -2.0F), delta); } @Test public void testIntPow() { final int maxExp = 300; DfpField field = new DfpField(40); final double base = 1.23456789; Dfp baseDfp = field.newDfp(base); Dfp dfpPower = field.getOne(); for (int i = 0; i < maxExp; i++) { Assert.assertEquals("exp=" + i, dfpPower.toDouble(), FastMath.pow(base, i), 0.6 * FastMath.ulp(dfpPower.toDouble())); dfpPower = dfpPower.multiply(baseDfp); } } } commons-math3-3.2-src/src/test/java/org/apache/commons/math3/util/PrecisionTest.java100644 1750 1750 72727 12126627675 27237 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with this * work for additional information regarding copyright ownership. The ASF * licenses this file to You 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. */ package org.apache.commons.math3.util; import java.math.BigDecimal; import org.apache.commons.math3.exception.MathArithmeticException; import org.apache.commons.math3.exception.MathIllegalArgumentException; import org.apache.commons.math3.TestUtils; import org.junit.Assert; import org.junit.Test; /** * Test cases for the {@link Precision} class. * * @version $Id$ */ public class PrecisionTest { @Test public void testEqualsWithRelativeTolerance() { Assert.assertTrue(Precision.equalsWithRelativeTolerance(0d, 0d, 0d)); Assert.assertTrue(Precision.equalsWithRelativeTolerance(0d, 1 / Double.NEGATIVE_INFINITY, 0d)); final double eps = 1e-14; Assert.assertFalse(Precision.equalsWithRelativeTolerance(1.987654687654968, 1.987654687654988, eps)); Assert.assertTrue(Precision.equalsWithRelativeTolerance(1.987654687654968, 1.987654687654987, eps)); Assert.assertFalse(Precision.equalsWithRelativeTolerance(1.987654687654968, 1.987654687654948, eps)); Assert.assertTrue(Precision.equalsWithRelativeTolerance(1.987654687654968, 1.987654687654949, eps)); Assert.assertFalse(Precision.equalsWithRelativeTolerance(Precision.SAFE_MIN, 0.0, eps)); Assert.assertFalse(Precision.equalsWithRelativeTolerance(1.0000000000001e-300, 1e-300, eps)); Assert.assertTrue(Precision.equalsWithRelativeTolerance(1.00000000000001e-300, 1e-300, eps)); Assert.assertFalse(Precision.equalsWithRelativeTolerance(Double.NEGATIVE_INFINITY, 1.23, eps)); Assert.assertFalse(Precision.equalsWithRelativeTolerance(Double.POSITIVE_INFINITY, 1.23, eps)); Assert.assertTrue(Precision.equalsWithRelativeTolerance(Double.NEGATIVE_INFINITY, Double.NEGATIVE_INFINITY, eps)); Assert.assertTrue(Precision.equalsWithRelativeTolerance(Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY, eps)); Assert.assertFalse(Precision.equalsWithRelativeTolerance(Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY, eps)); Assert.assertFalse(Precision.equalsWithRelativeTolerance(Double.NaN, 1.23, eps)); Assert.assertFalse(Precision.equalsWithRelativeTolerance(Double.NaN, Double.NaN, eps)); } @Test public void testEqualsIncludingNaN() { double[] testArray = { Double.NaN, Double.POSITIVE_INFINITY, Double.NEGATIVE_INFINITY, 1d, 0d }; for (int i = 0; i < testArray.length; i++) { for (int j = 0; j < testArray.length; j++) { if (i == j) { Assert.assertTrue(Precision.equalsIncludingNaN(testArray[i], testArray[j])); Assert.assertTrue(Precision.equalsIncludingNaN(testArray[j], testArray[i])); } else { Assert.assertTrue(!Precision.equalsIncludingNaN(testArray[i], testArray[j])); Assert.assertTrue(!Precision.equalsIncludingNaN(testArray[j], testArray[i])); } } } } @Test public void testEqualsWithAllowedDelta() { Assert.assertTrue(Precision.equals(153.0000, 153.0000, .0625)); Assert.assertTrue(Precision.equals(153.0000, 153.0625, .0625)); Assert.assertTrue(Precision.equals(152.9375, 153.0000, .0625)); Assert.assertFalse(Precision.equals(153.0000, 153.0625, .0624)); Assert.assertFalse(Precision.equals(152.9374, 153.0000, .0625)); Assert.assertFalse(Precision.equals(Double.NaN, Double.NaN, 1.0)); Assert.assertTrue(Precision.equals(Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY, 1.0)); Assert.assertTrue(Precision.equals(Double.NEGATIVE_INFINITY, Double.NEGATIVE_INFINITY, 1.0)); Assert.assertFalse(Precision.equals(Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY, 1.0)); } @Test public void testMath475() { final double a = 1.7976931348623182E16; final double b = FastMath.nextUp(a); double diff = FastMath.abs(a - b); // Because they are adjacent floating point numbers, "a" and "b" are // considered equal even though the allowed error is smaller than // their difference. Assert.assertTrue(Precision.equals(a, b, 0.5 * diff)); final double c = FastMath.nextUp(b); diff = FastMath.abs(a - c); // Because "a" and "c" are not adjacent, the tolerance is taken into // account for assessing equality. Assert.assertTrue(Precision.equals(a, c, diff)); Assert.assertFalse(Precision.equals(a, c, (1 - 1e-16) * diff)); } @Test public void testEqualsIncludingNaNWithAllowedDelta() { Assert.assertTrue(Precision.equalsIncludingNaN(153.0000, 153.0000, .0625)); Assert.assertTrue(Precision.equalsIncludingNaN(153.0000, 153.0625, .0625)); Assert.assertTrue(Precision.equalsIncludingNaN(152.9375, 153.0000, .0625)); Assert.assertTrue(Precision.equalsIncludingNaN(Double.NaN, Double.NaN, 1.0)); Assert.assertTrue(Precision.equalsIncludingNaN(Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY, 1.0)); Assert.assertTrue(Precision.equalsIncludingNaN(Double.NEGATIVE_INFINITY, Double.NEGATIVE_INFINITY, 1.0)); Assert.assertFalse(Precision.equalsIncludingNaN(Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY, 1.0)); Assert.assertFalse(Precision.equalsIncludingNaN(153.0000, 153.0625, .0624)); Assert.assertFalse(Precision.equalsIncludingNaN(152.9374, 153.0000, .0625)); } // Tests for floating point equality @Test public void testFloatEqualsWithAllowedUlps() { Assert.assertTrue("+0.0f == -0.0f",Precision.equals(0.0f, -0.0f)); Assert.assertTrue("+0.0f == -0.0f (1 ulp)",Precision.equals(0.0f, -0.0f, 1)); float oneFloat = 1.0f; Assert.assertTrue("1.0f == 1.0f + 1 ulp",Precision.equals(oneFloat, Float.intBitsToFloat(1 + Float.floatToIntBits(oneFloat)))); Assert.assertTrue("1.0f == 1.0f + 1 ulp (1 ulp)",Precision.equals(oneFloat, Float.intBitsToFloat(1 + Float.floatToIntBits(oneFloat)), 1)); Assert.assertFalse("1.0f != 1.0f + 2 ulp (1 ulp)",Precision.equals(oneFloat, Float.intBitsToFloat(2 + Float.floatToIntBits(oneFloat)), 1)); Assert.assertTrue(Precision.equals(153.0f, 153.0f, 1)); // These tests need adjusting for floating point precision // Assert.assertTrue(Precision.equals(153.0f, 153.00000000000003f, 1)); // Assert.assertFalse(Precision.equals(153.0f, 153.00000000000006f, 1)); // Assert.assertTrue(Precision.equals(153.0f, 152.99999999999997f, 1)); // Assert.assertFalse(Precision.equals(153f, 152.99999999999994f, 1)); // // Assert.assertTrue(Precision.equals(-128.0f, -127.99999999999999f, 1)); // Assert.assertFalse(Precision.equals(-128.0f, -127.99999999999997f, 1)); // Assert.assertTrue(Precision.equals(-128.0f, -128.00000000000003f, 1)); // Assert.assertFalse(Precision.equals(-128.0f, -128.00000000000006f, 1)); Assert.assertTrue(Precision.equals(Float.POSITIVE_INFINITY, Float.POSITIVE_INFINITY, 1)); Assert.assertTrue(Precision.equals(Double.MAX_VALUE, Float.POSITIVE_INFINITY, 1)); Assert.assertTrue(Precision.equals(Float.NEGATIVE_INFINITY, Float.NEGATIVE_INFINITY, 1)); Assert.assertTrue(Precision.equals(-Float.MAX_VALUE, Float.NEGATIVE_INFINITY, 1)); Assert.assertFalse(Precision.equals(Float.NaN, Float.NaN, 1)); Assert.assertFalse(Precision.equals(Float.NaN, Float.NaN, 0)); Assert.assertFalse(Precision.equals(Float.NaN, 0, 0)); Assert.assertFalse(Precision.equals(Float.NaN, Float.POSITIVE_INFINITY, 0)); Assert.assertFalse(Precision.equals(Float.NaN, Float.NEGATIVE_INFINITY, 0)); Assert.assertFalse(Precision.equals(Float.NEGATIVE_INFINITY, Float.POSITIVE_INFINITY, 100000)); } @Test public void testEqualsWithAllowedUlps() { Assert.assertTrue(Precision.equals(0.0, -0.0, 1)); Assert.assertTrue(Precision.equals(1.0, 1 + FastMath.ulp(1d), 1)); Assert.assertFalse(Precision.equals(1.0, 1 + 2 * FastMath.ulp(1d), 1)); final double nUp1 = FastMath.nextAfter(1d, Double.POSITIVE_INFINITY); final double nnUp1 = FastMath.nextAfter(nUp1, Double.POSITIVE_INFINITY); Assert.assertTrue(Precision.equals(1.0, nUp1, 1)); Assert.assertTrue(Precision.equals(nUp1, nnUp1, 1)); Assert.assertFalse(Precision.equals(1.0, nnUp1, 1)); Assert.assertTrue(Precision.equals(0.0, FastMath.ulp(0d), 1)); Assert.assertTrue(Precision.equals(0.0, -FastMath.ulp(0d), 1)); Assert.assertTrue(Precision.equals(153.0, 153.0, 1)); Assert.assertTrue(Precision.equals(153.0, 153.00000000000003, 1)); Assert.assertFalse(Precision.equals(153.0, 153.00000000000006, 1)); Assert.assertTrue(Precision.equals(153.0, 152.99999999999997, 1)); Assert.assertFalse(Precision.equals(153, 152.99999999999994, 1)); Assert.assertTrue(Precision.equals(-128.0, -127.99999999999999, 1)); Assert.assertFalse(Precision.equals(-128.0, -127.99999999999997, 1)); Assert.assertTrue(Precision.equals(-128.0, -128.00000000000003, 1)); Assert.assertFalse(Precision.equals(-128.0, -128.00000000000006, 1)); Assert.assertTrue(Precision.equals(Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY, 1)); Assert.assertTrue(Precision.equals(Double.MAX_VALUE, Double.POSITIVE_INFINITY, 1)); Assert.assertTrue(Precision.equals(Double.NEGATIVE_INFINITY, Double.NEGATIVE_INFINITY, 1)); Assert.assertTrue(Precision.equals(-Double.MAX_VALUE, Double.NEGATIVE_INFINITY, 1)); Assert.assertFalse(Precision.equals(Double.NaN, Double.NaN, 1)); Assert.assertFalse(Precision.equals(Double.NaN, Double.NaN, 0)); Assert.assertFalse(Precision.equals(Double.NaN, 0, 0)); Assert.assertFalse(Precision.equals(Double.NaN, Double.POSITIVE_INFINITY, 0)); Assert.assertFalse(Precision.equals(Double.NaN, Double.NEGATIVE_INFINITY, 0)); Assert.assertFalse(Precision.equals(Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY, 100000)); } @Test public void testEqualsIncludingNaNWithAllowedUlps() { Assert.assertTrue(Precision.equalsIncludingNaN(0.0, -0.0, 1)); Assert.assertTrue(Precision.equalsIncludingNaN(1.0, 1 + FastMath.ulp(1d), 1)); Assert.assertFalse(Precision.equalsIncludingNaN(1.0, 1 + 2 * FastMath.ulp(1d), 1)); final double nUp1 = FastMath.nextAfter(1d, Double.POSITIVE_INFINITY); final double nnUp1 = FastMath.nextAfter(nUp1, Double.POSITIVE_INFINITY); Assert.assertTrue(Precision.equalsIncludingNaN(1.0, nUp1, 1)); Assert.assertTrue(Precision.equalsIncludingNaN(nUp1, nnUp1, 1)); Assert.assertFalse(Precision.equalsIncludingNaN(1.0, nnUp1, 1)); Assert.assertTrue(Precision.equalsIncludingNaN(0.0, FastMath.ulp(0d), 1)); Assert.assertTrue(Precision.equalsIncludingNaN(0.0, -FastMath.ulp(0d), 1)); Assert.assertTrue(Precision.equalsIncludingNaN(153.0, 153.0, 1)); Assert.assertTrue(Precision.equalsIncludingNaN(153.0, 153.00000000000003, 1)); Assert.assertFalse(Precision.equalsIncludingNaN(153.0, 153.00000000000006, 1)); Assert.assertTrue(Precision.equalsIncludingNaN(153.0, 152.99999999999997, 1)); Assert.assertFalse(Precision.equalsIncludingNaN(153, 152.99999999999994, 1)); Assert.assertTrue(Precision.equalsIncludingNaN(-128.0, -127.99999999999999, 1)); Assert.assertFalse(Precision.equalsIncludingNaN(-128.0, -127.99999999999997, 1)); Assert.assertTrue(Precision.equalsIncludingNaN(-128.0, -128.00000000000003, 1)); Assert.assertFalse(Precision.equalsIncludingNaN(-128.0, -128.00000000000006, 1)); Assert.assertTrue(Precision.equalsIncludingNaN(Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY, 1)); Assert.assertTrue(Precision.equalsIncludingNaN(Double.MAX_VALUE, Double.POSITIVE_INFINITY, 1)); Assert.assertTrue(Precision.equalsIncludingNaN(Double.NEGATIVE_INFINITY, Double.NEGATIVE_INFINITY, 1)); Assert.assertTrue(Precision.equalsIncludingNaN(-Double.MAX_VALUE, Double.NEGATIVE_INFINITY, 1)); Assert.assertTrue(Precision.equalsIncludingNaN(Double.NaN, Double.NaN, 1)); Assert.assertFalse(Precision.equalsIncludingNaN(Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY, 100000)); } @Test public void testCompareToEpsilon() { Assert.assertEquals(0, Precision.compareTo(152.33, 152.32, .011)); Assert.assertTrue(Precision.compareTo(152.308, 152.32, .011) < 0); Assert.assertTrue(Precision.compareTo(152.33, 152.318, .011) > 0); Assert.assertEquals(0, Precision.compareTo(Double.MIN_VALUE, +0.0, Double.MIN_VALUE)); Assert.assertEquals(0, Precision.compareTo(Double.MIN_VALUE, -0.0, Double.MIN_VALUE)); } @Test public void testCompareToMaxUlps() { double a = 152.32; double delta = FastMath.ulp(a); for (int i = 0; i <= 10; ++i) { if (i <= 5) { Assert.assertEquals( 0, Precision.compareTo(a, a + i * delta, 5)); Assert.assertEquals( 0, Precision.compareTo(a, a - i * delta, 5)); } else { Assert.assertEquals(-1, Precision.compareTo(a, a + i * delta, 5)); Assert.assertEquals(+1, Precision.compareTo(a, a - i * delta, 5)); } } Assert.assertEquals( 0, Precision.compareTo(-0.0, 0.0, 0)); Assert.assertEquals(-1, Precision.compareTo(-Double.MIN_VALUE, -0.0, 0)); Assert.assertEquals( 0, Precision.compareTo(-Double.MIN_VALUE, -0.0, 1)); Assert.assertEquals(-1, Precision.compareTo(-Double.MIN_VALUE, +0.0, 0)); Assert.assertEquals( 0, Precision.compareTo(-Double.MIN_VALUE, +0.0, 1)); Assert.assertEquals(+1, Precision.compareTo( Double.MIN_VALUE, -0.0, 0)); Assert.assertEquals( 0, Precision.compareTo( Double.MIN_VALUE, -0.0, 1)); Assert.assertEquals(+1, Precision.compareTo( Double.MIN_VALUE, +0.0, 0)); Assert.assertEquals( 0, Precision.compareTo( Double.MIN_VALUE, +0.0, 1)); Assert.assertEquals(-1, Precision.compareTo(-Double.MIN_VALUE, Double.MIN_VALUE, 0)); Assert.assertEquals(-1, Precision.compareTo(-Double.MIN_VALUE, Double.MIN_VALUE, 1)); Assert.assertEquals( 0, Precision.compareTo(-Double.MIN_VALUE, Double.MIN_VALUE, 2)); Assert.assertEquals( 0, Precision.compareTo(Double.MAX_VALUE, Double.POSITIVE_INFINITY, 1)); Assert.assertEquals(-1, Precision.compareTo(Double.MAX_VALUE, Double.POSITIVE_INFINITY, 0)); Assert.assertEquals(+1, Precision.compareTo(Double.MAX_VALUE, Double.NaN, Integer.MAX_VALUE)); Assert.assertEquals(+1, Precision.compareTo(Double.NaN, Double.MAX_VALUE, Integer.MAX_VALUE)); } @Test public void testRoundDouble() { double x = 1.234567890; Assert.assertEquals(1.23, Precision.round(x, 2), 0.0); Assert.assertEquals(1.235, Precision.round(x, 3), 0.0); Assert.assertEquals(1.2346, Precision.round(x, 4), 0.0); // JIRA MATH-151 Assert.assertEquals(39.25, Precision.round(39.245, 2), 0.0); Assert.assertEquals(39.24, Precision.round(39.245, 2, BigDecimal.ROUND_DOWN), 0.0); double xx = 39.0; xx = xx + 245d / 1000d; Assert.assertEquals(39.25, Precision.round(xx, 2), 0.0); // BZ 35904 Assert.assertEquals(30.1d, Precision.round(30.095d, 2), 0.0d); Assert.assertEquals(30.1d, Precision.round(30.095d, 1), 0.0d); Assert.assertEquals(33.1d, Precision.round(33.095d, 1), 0.0d); Assert.assertEquals(33.1d, Precision.round(33.095d, 2), 0.0d); Assert.assertEquals(50.09d, Precision.round(50.085d, 2), 0.0d); Assert.assertEquals(50.19d, Precision.round(50.185d, 2), 0.0d); Assert.assertEquals(50.01d, Precision.round(50.005d, 2), 0.0d); Assert.assertEquals(30.01d, Precision.round(30.005d, 2), 0.0d); Assert.assertEquals(30.65d, Precision.round(30.645d, 2), 0.0d); Assert.assertEquals(1.24, Precision.round(x, 2, BigDecimal.ROUND_CEILING), 0.0); Assert.assertEquals(1.235, Precision.round(x, 3, BigDecimal.ROUND_CEILING), 0.0); Assert.assertEquals(1.2346, Precision.round(x, 4, BigDecimal.ROUND_CEILING), 0.0); Assert.assertEquals(-1.23, Precision.round(-x, 2, BigDecimal.ROUND_CEILING), 0.0); Assert.assertEquals(-1.234, Precision.round(-x, 3, BigDecimal.ROUND_CEILING), 0.0); Assert.assertEquals(-1.2345, Precision.round(-x, 4, BigDecimal.ROUND_CEILING), 0.0); Assert.assertEquals(1.23, Precision.round(x, 2, BigDecimal.ROUND_DOWN), 0.0); Assert.assertEquals(1.234, Precision.round(x, 3, BigDecimal.ROUND_DOWN), 0.0); Assert.assertEquals(1.2345, Precision.round(x, 4, BigDecimal.ROUND_DOWN), 0.0); Assert.assertEquals(-1.23, Precision.round(-x, 2, BigDecimal.ROUND_DOWN), 0.0); Assert.assertEquals(-1.234, Precision.round(-x, 3, BigDecimal.ROUND_DOWN), 0.0); Assert.assertEquals(-1.2345, Precision.round(-x, 4, BigDecimal.ROUND_DOWN), 0.0); Assert.assertEquals(1.23, Precision.round(x, 2, BigDecimal.ROUND_FLOOR), 0.0); Assert.assertEquals(1.234, Precision.round(x, 3, BigDecimal.ROUND_FLOOR), 0.0); Assert.assertEquals(1.2345, Precision.round(x, 4, BigDecimal.ROUND_FLOOR), 0.0); Assert.assertEquals(-1.24, Precision.round(-x, 2, BigDecimal.ROUND_FLOOR), 0.0); Assert.assertEquals(-1.235, Precision.round(-x, 3, BigDecimal.ROUND_FLOOR), 0.0); Assert.assertEquals(-1.2346, Precision.round(-x, 4, BigDecimal.ROUND_FLOOR), 0.0); Assert.assertEquals(1.23, Precision.round(x, 2, BigDecimal.ROUND_HALF_DOWN), 0.0); Assert.assertEquals(1.235, Precision.round(x, 3, BigDecimal.ROUND_HALF_DOWN), 0.0); Assert.assertEquals(1.2346, Precision.round(x, 4, BigDecimal.ROUND_HALF_DOWN), 0.0); Assert.assertEquals(-1.23, Precision.round(-x, 2, BigDecimal.ROUND_HALF_DOWN), 0.0); Assert.assertEquals(-1.235, Precision.round(-x, 3, BigDecimal.ROUND_HALF_DOWN), 0.0); Assert.assertEquals(-1.2346, Precision.round(-x, 4, BigDecimal.ROUND_HALF_DOWN), 0.0); Assert.assertEquals(1.234, Precision.round(1.2345, 3, BigDecimal.ROUND_HALF_DOWN), 0.0); Assert.assertEquals(-1.234, Precision.round(-1.2345, 3, BigDecimal.ROUND_HALF_DOWN), 0.0); Assert.assertEquals(1.23, Precision.round(x, 2, BigDecimal.ROUND_HALF_EVEN), 0.0); Assert.assertEquals(1.235, Precision.round(x, 3, BigDecimal.ROUND_HALF_EVEN), 0.0); Assert.assertEquals(1.2346, Precision.round(x, 4, BigDecimal.ROUND_HALF_EVEN), 0.0); Assert.assertEquals(-1.23, Precision.round(-x, 2, BigDecimal.ROUND_HALF_EVEN), 0.0); Assert.assertEquals(-1.235, Precision.round(-x, 3, BigDecimal.ROUND_HALF_EVEN), 0.0); Assert.assertEquals(-1.2346, Precision.round(-x, 4, BigDecimal.ROUND_HALF_EVEN), 0.0); Assert.assertEquals(1.234, Precision.round(1.2345, 3, BigDecimal.ROUND_HALF_EVEN), 0.0); Assert.assertEquals(-1.234, Precision.round(-1.2345, 3, BigDecimal.ROUND_HALF_EVEN), 0.0); Assert.assertEquals(1.236, Precision.round(1.2355, 3, BigDecimal.ROUND_HALF_EVEN), 0.0); Assert.assertEquals(-1.236, Precision.round(-1.2355, 3, BigDecimal.ROUND_HALF_EVEN), 0.0); Assert.assertEquals(1.23, Precision.round(x, 2, BigDecimal.ROUND_HALF_UP), 0.0); Assert.assertEquals(1.235, Precision.round(x, 3, BigDecimal.ROUND_HALF_UP), 0.0); Assert.assertEquals(1.2346, Precision.round(x, 4, BigDecimal.ROUND_HALF_UP), 0.0); Assert.assertEquals(-1.23, Precision.round(-x, 2, BigDecimal.ROUND_HALF_UP), 0.0); Assert.assertEquals(-1.235, Precision.round(-x, 3, BigDecimal.ROUND_HALF_UP), 0.0); Assert.assertEquals(-1.2346, Precision.round(-x, 4, BigDecimal.ROUND_HALF_UP), 0.0); Assert.assertEquals(1.235, Precision.round(1.2345, 3, BigDecimal.ROUND_HALF_UP), 0.0); Assert.assertEquals(-1.235, Precision.round(-1.2345, 3, BigDecimal.ROUND_HALF_UP), 0.0); Assert.assertEquals(-1.23, Precision.round(-1.23, 2, BigDecimal.ROUND_UNNECESSARY), 0.0); Assert.assertEquals(1.23, Precision.round(1.23, 2, BigDecimal.ROUND_UNNECESSARY), 0.0); try { Precision.round(1.234, 2, BigDecimal.ROUND_UNNECESSARY); Assert.fail(); } catch (ArithmeticException ex) { // expected } Assert.assertEquals(1.24, Precision.round(x, 2, BigDecimal.ROUND_UP), 0.0); Assert.assertEquals(1.235, Precision.round(x, 3, BigDecimal.ROUND_UP), 0.0); Assert.assertEquals(1.2346, Precision.round(x, 4, BigDecimal.ROUND_UP), 0.0); Assert.assertEquals(-1.24, Precision.round(-x, 2, BigDecimal.ROUND_UP), 0.0); Assert.assertEquals(-1.235, Precision.round(-x, 3, BigDecimal.ROUND_UP), 0.0); Assert.assertEquals(-1.2346, Precision.round(-x, 4, BigDecimal.ROUND_UP), 0.0); try { Precision.round(1.234, 2, 1923); Assert.fail(); } catch (IllegalArgumentException ex) { // expected } // MATH-151 Assert.assertEquals(39.25, Precision.round(39.245, 2, BigDecimal.ROUND_HALF_UP), 0.0); // special values TestUtils.assertEquals(Double.NaN, Precision.round(Double.NaN, 2), 0.0); Assert.assertEquals(0.0, Precision.round(0.0, 2), 0.0); Assert.assertEquals(Double.POSITIVE_INFINITY, Precision.round(Double.POSITIVE_INFINITY, 2), 0.0); Assert.assertEquals(Double.NEGATIVE_INFINITY, Precision.round(Double.NEGATIVE_INFINITY, 2), 0.0); } @Test public void testRoundFloat() { float x = 1.234567890f; Assert.assertEquals(1.23f, Precision.round(x, 2), 0.0); Assert.assertEquals(1.235f, Precision.round(x, 3), 0.0); Assert.assertEquals(1.2346f, Precision.round(x, 4), 0.0); // BZ 35904 Assert.assertEquals(30.1f, Precision.round(30.095f, 2), 0.0f); Assert.assertEquals(30.1f, Precision.round(30.095f, 1), 0.0f); Assert.assertEquals(50.09f, Precision.round(50.085f, 2), 0.0f); Assert.assertEquals(50.19f, Precision.round(50.185f, 2), 0.0f); Assert.assertEquals(50.01f, Precision.round(50.005f, 2), 0.0f); Assert.assertEquals(30.01f, Precision.round(30.005f, 2), 0.0f); Assert.assertEquals(30.65f, Precision.round(30.645f, 2), 0.0f); Assert.assertEquals(1.24f, Precision.round(x, 2, BigDecimal.ROUND_CEILING), 0.0); Assert.assertEquals(1.235f, Precision.round(x, 3, BigDecimal.ROUND_CEILING), 0.0); Assert.assertEquals(1.2346f, Precision.round(x, 4, BigDecimal.ROUND_CEILING), 0.0); Assert.assertEquals(-1.23f, Precision.round(-x, 2, BigDecimal.ROUND_CEILING), 0.0); Assert.assertEquals(-1.234f, Precision.round(-x, 3, BigDecimal.ROUND_CEILING), 0.0); Assert.assertEquals(-1.2345f, Precision.round(-x, 4, BigDecimal.ROUND_CEILING), 0.0); Assert.assertEquals(1.23f, Precision.round(x, 2, BigDecimal.ROUND_DOWN), 0.0); Assert.assertEquals(1.234f, Precision.round(x, 3, BigDecimal.ROUND_DOWN), 0.0); Assert.assertEquals(1.2345f, Precision.round(x, 4, BigDecimal.ROUND_DOWN), 0.0); Assert.assertEquals(-1.23f, Precision.round(-x, 2, BigDecimal.ROUND_DOWN), 0.0); Assert.assertEquals(-1.234f, Precision.round(-x, 3, BigDecimal.ROUND_DOWN), 0.0); Assert.assertEquals(-1.2345f, Precision.round(-x, 4, BigDecimal.ROUND_DOWN), 0.0); Assert.assertEquals(1.23f, Precision.round(x, 2, BigDecimal.ROUND_FLOOR), 0.0); Assert.assertEquals(1.234f, Precision.round(x, 3, BigDecimal.ROUND_FLOOR), 0.0); Assert.assertEquals(1.2345f, Precision.round(x, 4, BigDecimal.ROUND_FLOOR), 0.0); Assert.assertEquals(-1.24f, Precision.round(-x, 2, BigDecimal.ROUND_FLOOR), 0.0); Assert.assertEquals(-1.235f, Precision.round(-x, 3, BigDecimal.ROUND_FLOOR), 0.0); Assert.assertEquals(-1.2346f, Precision.round(-x, 4, BigDecimal.ROUND_FLOOR), 0.0); Assert.assertEquals(1.23f, Precision.round(x, 2, BigDecimal.ROUND_HALF_DOWN), 0.0); Assert.assertEquals(1.235f, Precision.round(x, 3, BigDecimal.ROUND_HALF_DOWN), 0.0); Assert.assertEquals(1.2346f, Precision.round(x, 4, BigDecimal.ROUND_HALF_DOWN), 0.0); Assert.assertEquals(-1.23f, Precision.round(-x, 2, BigDecimal.ROUND_HALF_DOWN), 0.0); Assert.assertEquals(-1.235f, Precision.round(-x, 3, BigDecimal.ROUND_HALF_DOWN), 0.0); Assert.assertEquals(-1.2346f, Precision.round(-x, 4, BigDecimal.ROUND_HALF_DOWN), 0.0); Assert.assertEquals(1.234f, Precision.round(1.2345f, 3, BigDecimal.ROUND_HALF_DOWN), 0.0); Assert.assertEquals(-1.234f, Precision.round(-1.2345f, 3, BigDecimal.ROUND_HALF_DOWN), 0.0); Assert.assertEquals(1.23f, Precision.round(x, 2, BigDecimal.ROUND_HALF_EVEN), 0.0); Assert.assertEquals(1.235f, Precision.round(x, 3, BigDecimal.ROUND_HALF_EVEN), 0.0); Assert.assertEquals(1.2346f, Precision.round(x, 4, BigDecimal.ROUND_HALF_EVEN), 0.0); Assert.assertEquals(-1.23f, Precision.round(-x, 2, BigDecimal.ROUND_HALF_EVEN), 0.0); Assert.assertEquals(-1.235f, Precision.round(-x, 3, BigDecimal.ROUND_HALF_EVEN), 0.0); Assert.assertEquals(-1.2346f, Precision.round(-x, 4, BigDecimal.ROUND_HALF_EVEN), 0.0); Assert.assertEquals(1.234f, Precision.round(1.2345f, 3, BigDecimal.ROUND_HALF_EVEN), 0.0); Assert.assertEquals(-1.234f, Precision.round(-1.2345f, 3, BigDecimal.ROUND_HALF_EVEN), 0.0); Assert.assertEquals(1.236f, Precision.round(1.2355f, 3, BigDecimal.ROUND_HALF_EVEN), 0.0); Assert.assertEquals(-1.236f, Precision.round(-1.2355f, 3, BigDecimal.ROUND_HALF_EVEN), 0.0); Assert.assertEquals(1.23f, Precision.round(x, 2, BigDecimal.ROUND_HALF_UP), 0.0); Assert.assertEquals(1.235f, Precision.round(x, 3, BigDecimal.ROUND_HALF_UP), 0.0); Assert.assertEquals(1.2346f, Precision.round(x, 4, BigDecimal.ROUND_HALF_UP), 0.0); Assert.assertEquals(-1.23f, Precision.round(-x, 2, BigDecimal.ROUND_HALF_UP), 0.0); Assert.assertEquals(-1.235f, Precision.round(-x, 3, BigDecimal.ROUND_HALF_UP), 0.0); Assert.assertEquals(-1.2346f, Precision.round(-x, 4, BigDecimal.ROUND_HALF_UP), 0.0); Assert.assertEquals(1.235f, Precision.round(1.2345f, 3, BigDecimal.ROUND_HALF_UP), 0.0); Assert.assertEquals(-1.235f, Precision.round(-1.2345f, 3, BigDecimal.ROUND_HALF_UP), 0.0); Assert.assertEquals(-1.23f, Precision.round(-1.23f, 2, BigDecimal.ROUND_UNNECESSARY), 0.0); Assert.assertEquals(1.23f, Precision.round(1.23f, 2, BigDecimal.ROUND_UNNECESSARY), 0.0); try { Precision.round(1.234f, 2, BigDecimal.ROUND_UNNECESSARY); Assert.fail(); } catch (MathArithmeticException ex) { // success } Assert.assertEquals(1.24f, Precision.round(x, 2, BigDecimal.ROUND_UP), 0.0); Assert.assertEquals(1.235f, Precision.round(x, 3, BigDecimal.ROUND_UP), 0.0); Assert.assertEquals(1.2346f, Precision.round(x, 4, BigDecimal.ROUND_UP), 0.0); Assert.assertEquals(-1.24f, Precision.round(-x, 2, BigDecimal.ROUND_UP), 0.0); Assert.assertEquals(-1.235f, Precision.round(-x, 3, BigDecimal.ROUND_UP), 0.0); Assert.assertEquals(-1.2346f, Precision.round(-x, 4, BigDecimal.ROUND_UP), 0.0); try { Precision.round(1.234f, 2, 1923); Assert.fail(); } catch (MathIllegalArgumentException ex) { // success } // special values TestUtils.assertEquals(Float.NaN, Precision.round(Float.NaN, 2), 0.0f); Assert.assertEquals(0.0f, Precision.round(0.0f, 2), 0.0f); Assert.assertEquals(Float.POSITIVE_INFINITY, Precision.round(Float.POSITIVE_INFINITY, 2), 0.0f); Assert.assertEquals(Float.NEGATIVE_INFINITY, Precision.round(Float.NEGATIVE_INFINITY, 2), 0.0f); } @Test public void testIssue721() { Assert.assertEquals(-53, FastMath.getExponent(Precision.EPSILON)); Assert.assertEquals(-1022, FastMath.getExponent(Precision.SAFE_MIN)); } @Test public void testRepresentableDelta() { int nonRepresentableCount = 0; final double x = 100; final int numTrials = 10000; for (int i = 0; i < numTrials; i++) { final double originalDelta = Math.random(); final double delta = Precision.representableDelta(x, originalDelta); if (delta != originalDelta) { ++nonRepresentableCount; } } Assert.assertTrue(nonRepresentableCount / (double) numTrials > 0.9); } @Test public void testMath843() { final double afterEpsilon = FastMath.nextAfter(Precision.EPSILON, Double.POSITIVE_INFINITY); // a) 1 + EPSILON is equal to 1. Assert.assertTrue(1 + Precision.EPSILON == 1); // b) 1 + "the number after EPSILON" is not equal to 1. Assert.assertFalse(1 + afterEpsilon == 1); } } commons-math3-3.2-src/src/test/java/org/apache/commons/math3/util/BigRealFieldTest.java100644 1750 1750 2706 12126627675 27523 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.util; import org.apache.commons.math3.TestUtils; import org.junit.Assert; import org.junit.Test; public class BigRealFieldTest { @Test public void testZero() { Assert.assertEquals(BigReal.ZERO, BigRealField.getInstance().getZero()); } @Test public void testOne() { Assert.assertEquals(BigReal.ONE, BigRealField.getInstance().getOne()); } @Test public void testSerial() { // deserializing the singleton should give the singleton itself back BigRealField field = BigRealField.getInstance(); Assert.assertTrue(field == TestUtils.serializeAndRecover(field)); } } commons-math3-3.2-src/src/test/java/org/apache/commons/math3/genetics/BinaryMutationTest.java100644 1750 1750 3217 12126627672 31036 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.genetics; import org.junit.Assert; import org.junit.Test; public class BinaryMutationTest { @Test public void testMutate() { BinaryMutation mutation = new BinaryMutation(); // stochastic testing :) for (int i=0; i<20; i++) { DummyBinaryChromosome original = new DummyBinaryChromosome(BinaryChromosome.randomBinaryRepresentation(10)); DummyBinaryChromosome mutated = (DummyBinaryChromosome) mutation.mutate(original); // one gene should be different int numDifferent = 0; for (int j=0; j(1); cp.crossover(p1c,p2c); } @Test(expected = NumberIsTooLargeException.class) public void testNumberIsTooLargeException() { final Integer[] p1 = new Integer[] {1,0,1,0,0,1,0,1,1}; final Integer[] p2 = new Integer[] {0,1,1,0,1,0,1,1,1}; final BinaryChromosome p1c = new DummyBinaryChromosome(p1); final BinaryChromosome p2c = new DummyBinaryChromosome(p2); final CrossoverPolicy cp = new NPointCrossover(15); cp.crossover(p1c,p2c); } @Test(expected = MathIllegalArgumentException.class) public void testCrossoverInvalidFixedLengthChromosomeFirst() { final Integer[] p1 = new Integer[] {1,0,1,0,0,1,0,1,1}; final BinaryChromosome p1c = new DummyBinaryChromosome(p1); final Chromosome p2c = new Chromosome() { public double fitness() { // Not important return 0; } }; final CrossoverPolicy cp = new NPointCrossover(1); cp.crossover(p1c,p2c); } @Test(expected = MathIllegalArgumentException.class) public void testCrossoverInvalidFixedLengthChromosomeSecond() { final Integer[] p1 = new Integer[] {1,0,1,0,0,1,0,1,1}; final BinaryChromosome p2c = new DummyBinaryChromosome(p1); final Chromosome p1c = new Chromosome() { public double fitness() { // Not important return 0; } }; final CrossoverPolicy cp = new NPointCrossover(1); cp.crossover(p1c,p2c); } @Test public void testCrossover() { Integer[] p1 = new Integer[] {1,0,1,0,1,0,1,0,1}; Integer[] p2 = new Integer[] {0,1,0,1,0,1,0,1,0}; BinaryChromosome p1c = new DummyBinaryChromosome(p1); BinaryChromosome p2c = new DummyBinaryChromosome(p2); final int order = 3; NPointCrossover npc = new NPointCrossover(order); // the two parent chromosomes are different at each position, so it is easy to detect // the number of crossovers that happened for each child for (int i=0; i<20; i++) { ChromosomePair pair = npc.crossover(p1c,p2c); Integer[] c1 = new Integer[p1.length]; Integer[] c2 = new Integer[p2.length]; c1 = ((BinaryChromosome) pair.getFirst()).getRepresentation().toArray(c1); c2 = ((BinaryChromosome) pair.getSecond()).getRepresentation().toArray(c2); Assert.assertEquals(order, detectCrossoverPoints(p1c, p2c, (BinaryChromosome) pair.getFirst())); Assert.assertEquals(order, detectCrossoverPoints(p2c, p1c, (BinaryChromosome) pair.getSecond())); } } private int detectCrossoverPoints(BinaryChromosome p1, BinaryChromosome p2, BinaryChromosome c) { int crossovers = 0; final int length = p1.getLength(); final List p1Rep = p1.getRepresentation(); final List p2Rep = p2.getRepresentation(); final List cRep = c.getRepresentation(); List rep = p1Rep; for (int i = 0; i < length; i++) { if (rep.get(i) != cRep.get(i)) { crossovers++; rep = rep == p1Rep ? p2Rep : p1Rep; } } return crossovers; } } ././@LongLink100644 0 0 146 12126630646 10262 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/genetics/GeneticAlgorithmTestBinary.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/genetics/GeneticAlgorithmTestBinary.jav100644 1750 1750 10057 12126627672 32342 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.genetics; import java.util.LinkedList; import java.util.List; import org.junit.Assert; import org.junit.Test; /** * This is also an example of usage. */ public class GeneticAlgorithmTestBinary { // parameters for the GA private static final int DIMENSION = 50; private static final int POPULATION_SIZE = 50; private static final int NUM_GENERATIONS = 50; private static final double ELITISM_RATE = 0.2; private static final double CROSSOVER_RATE = 1; private static final double MUTATION_RATE = 0.1; private static final int TOURNAMENT_ARITY = 2; @Test public void test() { // to test a stochastic algorithm is hard, so this will rather be an usage example // initialize a new genetic algorithm GeneticAlgorithm ga = new GeneticAlgorithm( new OnePointCrossover(), CROSSOVER_RATE, // all selected chromosomes will be recombined (=crosssover) new BinaryMutation(), MUTATION_RATE, new TournamentSelection(TOURNAMENT_ARITY) ); Assert.assertEquals(0, ga.getGenerationsEvolved()); // initial population Population initial = randomPopulation(); // stopping conditions StoppingCondition stopCond = new FixedGenerationCount(NUM_GENERATIONS); // best initial chromosome Chromosome bestInitial = initial.getFittestChromosome(); // run the algorithm Population finalPopulation = ga.evolve(initial, stopCond); // best chromosome from the final population Chromosome bestFinal = finalPopulation.getFittestChromosome(); // the only thing we can test is whether the final solution is not worse than the initial one // however, for some implementations of GA, this need not be true :) Assert.assertTrue(bestFinal.compareTo(bestInitial) > 0); Assert.assertEquals(NUM_GENERATIONS, ga.getGenerationsEvolved()); } /** * Initializes a random population. */ private static ElitisticListPopulation randomPopulation() { List popList = new LinkedList(); for (int i=0; i representation) { super(representation); } /** * Returns number of elements != 0 */ public double fitness() { int num = 0; for (int val : this.getRepresentation()) { if (val != 0) num++; } // number of elements >= 0 return num; } @Override public AbstractListChromosome newFixedLengthChromosome(List chromosomeRepresentation) { return new FindOnes(chromosomeRepresentation); } } } commons-math3-3.2-src/src/test/java/org/apache/commons/math3/genetics/FitnessCachingTest.java100644 1750 1750 6435 12126627672 30766 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.genetics; import java.util.LinkedList; import java.util.List; import org.junit.Assert; import org.junit.Test; public class FitnessCachingTest { // parameters for the GA private static final int DIMENSION = 50; private static final double CROSSOVER_RATE = 1; private static final double MUTATION_RATE = 0.1; private static final int TOURNAMENT_ARITY = 5; private static final int POPULATION_SIZE = 10; private static final int NUM_GENERATIONS = 50; private static final double ELITISM_RATE = 0.2; // how many times was the fitness computed private static int fitnessCalls = 0; @Test public void testFitnessCaching() { // initialize a new genetic algorithm GeneticAlgorithm ga = new GeneticAlgorithm( new OnePointCrossover(), CROSSOVER_RATE, // all selected chromosomes will be recombined (=crosssover) new BinaryMutation(), MUTATION_RATE, // no mutation new TournamentSelection(TOURNAMENT_ARITY) ); // initial population Population initial = randomPopulation(); // stopping conditions StoppingCondition stopCond = new FixedGenerationCount(NUM_GENERATIONS); // run the algorithm ga.evolve(initial, stopCond); int neededCalls = POPULATION_SIZE /*initial population*/ + (NUM_GENERATIONS - 1) /*for each population*/ * (int)(POPULATION_SIZE * (1.0 - ELITISM_RATE)) /*some chromosomes are copied*/ ; Assert.assertTrue(fitnessCalls <= neededCalls); // some chromosomes after crossover may be the same os old ones } /** * Initializes a random population. */ private static ElitisticListPopulation randomPopulation() { List popList = new LinkedList(); for (int i=0; i representation) { super(representation); } @Override public double fitness() { fitnessCalls++; return 0; } } } commons-math3-3.2-src/src/test/java/org/apache/commons/math3/genetics/FixedElapsedTimeTest.java100644 1750 1750 4700 12126627672 31243 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.genetics; import java.util.Iterator; import java.util.concurrent.TimeUnit; import org.apache.commons.math3.util.FastMath; import org.junit.Assert; import org.junit.Test; public class FixedElapsedTimeTest { @Test public void testIsSatisfied() { final Population pop = new Population() { public void addChromosome(final Chromosome chromosome) { // unimportant } public Chromosome getFittestChromosome() { // unimportant return null; } public int getPopulationLimit() { // unimportant return 0; } public int getPopulationSize() { // unimportant return 0; } public Population nextGeneration() { // unimportant return null; } public Iterator iterator() { // unimportant return null; } }; final long start = System.nanoTime(); final long duration = 3; final FixedElapsedTime tec = new FixedElapsedTime(duration, TimeUnit.SECONDS); while (!tec.isSatisfied(pop)) { try { Thread.sleep(50); } catch (InterruptedException e) { // ignore } } final long end = System.nanoTime(); final long elapsedTime = end - start; final long diff = FastMath.abs(elapsedTime - TimeUnit.SECONDS.toNanos(duration)); Assert.assertTrue(diff < TimeUnit.MILLISECONDS.toNanos(100)); } } commons-math3-3.2-src/src/test/java/org/apache/commons/math3/genetics/OrderedCrossoverTest.java100644 1750 1750 10152 12126627672 31377 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.genetics; import java.util.Arrays; import java.util.HashSet; import java.util.Set; import org.apache.commons.math3.exception.DimensionMismatchException; import org.apache.commons.math3.exception.MathIllegalArgumentException; import org.junit.Assert; import org.junit.Test; public class OrderedCrossoverTest { @Test public void testCrossover() { final Integer[] p1 = new Integer[] { 8, 4, 7, 3, 6, 2, 5, 1, 9, 0 }; final Integer[] p2 = new Integer[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; final DummyListChromosome p1c = new DummyListChromosome(p1); final DummyListChromosome p2c = new DummyListChromosome(p2); final CrossoverPolicy cp = new OrderedCrossover(); for (int i = 0; i < 20; i++) { final Set parentSet1 = new HashSet(Arrays.asList(p1)); final Set parentSet2 = new HashSet(Arrays.asList(p2)); final ChromosomePair pair = cp.crossover(p1c, p2c); final Integer[] c1 = ((DummyListChromosome) pair.getFirst()).getRepresentation().toArray(new Integer[p1.length]); final Integer[] c2 = ((DummyListChromosome) pair.getSecond()).getRepresentation().toArray(new Integer[p2.length]); Assert.assertNotSame(p1c, pair.getFirst()); Assert.assertNotSame(p2c, pair.getSecond()); // make sure that the children have exactly the same elements as their parents for (int j = 0; j < c1.length; j++) { Assert.assertTrue(parentSet1.contains(c1[j])); parentSet1.remove(c1[j]); Assert.assertTrue(parentSet2.contains(c2[j])); parentSet2.remove(c2[j]); } } } @Test(expected = DimensionMismatchException.class) public void testCrossoverDimensionMismatchException() { final Integer[] p1 = new Integer[] { 1, 0, 1, 0, 0, 1, 0, 1, 1 }; final Integer[] p2 = new Integer[] { 0, 1, 1, 0, 1 }; final BinaryChromosome p1c = new DummyBinaryChromosome(p1); final BinaryChromosome p2c = new DummyBinaryChromosome(p2); final CrossoverPolicy cp = new OrderedCrossover(); cp.crossover(p1c, p2c); } @Test(expected = MathIllegalArgumentException.class) public void testCrossoverInvalidFixedLengthChromosomeFirst() { final Integer[] p1 = new Integer[] { 1, 0, 1, 0, 0, 1, 0, 1, 1 }; final BinaryChromosome p1c = new DummyBinaryChromosome(p1); final Chromosome p2c = new Chromosome() { public double fitness() { // Not important return 0; } }; final CrossoverPolicy cp = new OrderedCrossover(); cp.crossover(p1c, p2c); } @Test(expected = MathIllegalArgumentException.class) public void testCrossoverInvalidFixedLengthChromosomeSecond() { final Integer[] p1 = new Integer[] { 1, 0, 1, 0, 0, 1, 0, 1, 1 }; final BinaryChromosome p2c = new DummyBinaryChromosome(p1); final Chromosome p1c = new Chromosome() { public double fitness() { // Not important return 0; } }; final CrossoverPolicy cp = new OrderedCrossover(); cp.crossover(p1c, p2c); } } commons-math3-3.2-src/src/test/java/org/apache/commons/math3/genetics/DummyRandomKey.java100644 1750 1750 2644 12126627672 30141 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.genetics; import java.util.List; /** * Implementation of RandomKey for testing purposes */ public class DummyRandomKey extends RandomKey { public DummyRandomKey(List representation) { super(representation); } public DummyRandomKey(Double[] representation) { super(representation); } @Override public AbstractListChromosome newFixedLengthChromosome(List chromosomeRepresentation) { return new DummyRandomKey(chromosomeRepresentation); } public double fitness() { // unimportant return 0; } } commons-math3-3.2-src/src/test/java/org/apache/commons/math3/genetics/ListPopulationTest.java100644 1750 1750 17227 12126627672 31105 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.genetics; import java.util.ArrayList; import java.util.Iterator; import org.apache.commons.math3.exception.NotPositiveException; import org.apache.commons.math3.exception.NumberIsTooLargeException; import org.apache.commons.math3.exception.NumberIsTooSmallException; import org.junit.Assert; import org.junit.Test; public class ListPopulationTest { @Test public void testGetFittestChromosome() { Chromosome c1 = new Chromosome() { public double fitness() { return 0; } }; Chromosome c2 = new Chromosome() { public double fitness() { return 10; } }; Chromosome c3 = new Chromosome() { public double fitness() { return 15; } }; ArrayList chromosomes = new ArrayList (); chromosomes.add(c1); chromosomes.add(c2); chromosomes.add(c3); ListPopulation population = new ListPopulation(chromosomes, 10) { public Population nextGeneration() { // not important return null; } }; Assert.assertEquals(c3, population.getFittestChromosome()); } @Test public void testChromosomes() { final ArrayList chromosomes = new ArrayList (); chromosomes.add(new DummyBinaryChromosome(BinaryChromosome.randomBinaryRepresentation(3))); chromosomes.add(new DummyBinaryChromosome(BinaryChromosome.randomBinaryRepresentation(3))); chromosomes.add(new DummyBinaryChromosome(BinaryChromosome.randomBinaryRepresentation(3))); final ListPopulation population = new ListPopulation(10) { public Population nextGeneration() { // not important return null; } }; population.addChromosomes(chromosomes); Assert.assertEquals(chromosomes, population.getChromosomes()); Assert.assertEquals(chromosomes.toString(), population.toString()); population.setPopulationLimit(50); Assert.assertEquals(50, population.getPopulationLimit()); } @Test(expected = NotPositiveException.class) public void testSetPopulationLimit() { final ListPopulation population = new ListPopulation(10) { public Population nextGeneration() { // not important return null; } }; population.setPopulationLimit(-50); } @Test(expected = NotPositiveException.class) public void testConstructorPopulationLimitNotPositive() { new ListPopulation(-10) { public Population nextGeneration() { // not important return null; } }; } @Test(expected = NotPositiveException.class) public void testChromosomeListConstructorPopulationLimitNotPositive() { final ArrayList chromosomes = new ArrayList (); chromosomes.add(new DummyBinaryChromosome(BinaryChromosome.randomBinaryRepresentation(3))); new ListPopulation(chromosomes, -10) { public Population nextGeneration() { // not important return null; } }; } @Test(expected = NumberIsTooLargeException.class) public void testConstructorListOfChromosomesBiggerThanPopulationSize() { final ArrayList chromosomes = new ArrayList (); chromosomes.add(new DummyBinaryChromosome(BinaryChromosome.randomBinaryRepresentation(3))); chromosomes.add(new DummyBinaryChromosome(BinaryChromosome.randomBinaryRepresentation(3))); chromosomes.add(new DummyBinaryChromosome(BinaryChromosome.randomBinaryRepresentation(3))); new ListPopulation(chromosomes, 1) { public Population nextGeneration() { // not important return null; } }; } @Test(expected=NumberIsTooLargeException.class) public void testAddTooManyChromosomes() { final ArrayList chromosomes = new ArrayList (); chromosomes.add(new DummyBinaryChromosome(BinaryChromosome.randomBinaryRepresentation(3))); chromosomes.add(new DummyBinaryChromosome(BinaryChromosome.randomBinaryRepresentation(3))); chromosomes.add(new DummyBinaryChromosome(BinaryChromosome.randomBinaryRepresentation(3))); final ListPopulation population = new ListPopulation(2) { public Population nextGeneration() { // not important return null; } }; population.addChromosomes(chromosomes); } @Test(expected=NumberIsTooLargeException.class) public void testAddTooManyChromosomesSingleCall() { final ListPopulation population = new ListPopulation(2) { public Population nextGeneration() { // not important return null; } }; for (int i = 0; i <= population.getPopulationLimit(); i++) { population.addChromosome(new DummyBinaryChromosome(BinaryChromosome.randomBinaryRepresentation(3))); } } @Test(expected = UnsupportedOperationException.class) public void testIterator() { final ArrayList chromosomes = new ArrayList(); chromosomes.add(new DummyBinaryChromosome(BinaryChromosome.randomBinaryRepresentation(3))); chromosomes.add(new DummyBinaryChromosome(BinaryChromosome.randomBinaryRepresentation(3))); chromosomes.add(new DummyBinaryChromosome(BinaryChromosome.randomBinaryRepresentation(3))); final ListPopulation population = new ListPopulation(10) { public Population nextGeneration() { // not important return null; } }; population.addChromosomes(chromosomes); final Iterator iter = population.iterator(); while (iter.hasNext()) { iter.next(); iter.remove(); } } @Test(expected=NumberIsTooSmallException.class) public void testSetPopulationLimitTooSmall() { final ArrayList chromosomes = new ArrayList (); chromosomes.add(new DummyBinaryChromosome(BinaryChromosome.randomBinaryRepresentation(3))); chromosomes.add(new DummyBinaryChromosome(BinaryChromosome.randomBinaryRepresentation(3))); chromosomes.add(new DummyBinaryChromosome(BinaryChromosome.randomBinaryRepresentation(3))); final ListPopulation population = new ListPopulation(chromosomes, 3) { public Population nextGeneration() { // not important return null; } }; population.setPopulationLimit(2); } } commons-math3-3.2-src/src/test/java/org/apache/commons/math3/genetics/RandomKeyMutationTest.java100644 1750 1750 3107 12126627672 31501 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.genetics; import org.junit.Assert; import org.junit.Test; public class RandomKeyMutationTest { @Test public void testMutate() { MutationPolicy mutation = new RandomKeyMutation(); int l=10; for (int i=0; i<20; i++) { DummyRandomKey origRk = new DummyRandomKey(RandomKey.randomPermutation(l)); Chromosome mutated = mutation.mutate(origRk); DummyRandomKey mutatedRk = (DummyRandomKey) mutated; int changes = 0; for (int j=0; j 0); Assert.assertEquals(0,c3.compareTo(c2)); Assert.assertEquals(0,c2.compareTo(c3)); } private abstract static class DummyChromosome extends Chromosome { private final int repr; public DummyChromosome(final int repr) { this.repr = repr; } @Override protected boolean isSame(Chromosome another) { return ((DummyChromosome) another).repr == repr; } } @Test public void testFindSameChromosome() { Chromosome c1 = new DummyChromosome(1) { public double fitness() { return 1; } }; Chromosome c2 = new DummyChromosome(2) { public double fitness() { return 2; } }; Chromosome c3 = new DummyChromosome(3) { public double fitness() { return 3; } }; Chromosome c4 = new DummyChromosome(1) { public double fitness() { return 5; } }; Chromosome c5 = new DummyChromosome(15) { public double fitness() { return 15; } }; List popChr = new ArrayList(); popChr.add(c1); popChr.add(c2); popChr.add(c3); Population pop = new ListPopulation(popChr,3) { public Population nextGeneration() { // not important return null; } }; Assert.assertNull(c5.findSameChromosome(pop)); Assert.assertEquals(c1, c4.findSameChromosome(pop)); c4.searchForFitnessUpdate(pop); Assert.assertEquals(1, c4.getFitness(),0); } } commons-math3-3.2-src/src/test/java/org/apache/commons/math3/genetics/DummyListChromosome.java100644 1750 1750 5213 12126627672 31212 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.genetics; import java.util.Arrays; import java.util.List; /** * Implementation of ListChromosome for testing purposes */ public class DummyListChromosome extends AbstractListChromosome { public DummyListChromosome(final Integer[] representation) { super(representation); } public DummyListChromosome(final List representation) { super(representation); } public double fitness() { // Not important. return 0; } @Override protected void checkValidity(final List chromosomeRepresentation) throws InvalidRepresentationException { // Not important. } @Override public AbstractListChromosome newFixedLengthChromosome(final List chromosomeRepresentation) { return new DummyListChromosome(chromosomeRepresentation); } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + (getRepresentation() == null ? 0 : getRepresentation().hashCode()); return result; } @Override public boolean equals(final Object obj) { if (this == obj) { return true; } if (obj == null) { return false; } if (!(obj instanceof DummyListChromosome)) { return false; } final DummyListChromosome other = (DummyListChromosome) obj; if (getRepresentation() == null) { if (other.getRepresentation() != null) { return false; } } final Integer[] rep = getRepresentation().toArray(new Integer[getRepresentation().size()]); final Integer[] otherRep = other.getRepresentation().toArray(new Integer[other.getRepresentation().size()]); return Arrays.equals(rep, otherRep); } } commons-math3-3.2-src/src/test/java/org/apache/commons/math3/genetics/FixedGenerationCountTest.java100644 1750 1750 3707 12126627672 32161 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.genetics; import java.util.Iterator; import org.junit.Assert; import org.junit.Test; public class FixedGenerationCountTest { @Test public void testIsSatisfied() { FixedGenerationCount fgc = new FixedGenerationCount(20); int cnt = 0; Population pop = new Population() { public void addChromosome(Chromosome chromosome) { // unimportant } public Chromosome getFittestChromosome() { // unimportant return null; } public int getPopulationLimit() { // unimportant return 0; } public int getPopulationSize() { // unimportant return 0; } public Population nextGeneration() { // unimportant return null; } public Iterator iterator() { // unimportant return null; } }; while (!fgc.isSatisfied(pop)) { cnt++; } Assert.assertEquals(20, cnt); } } commons-math3-3.2-src/src/test/java/org/apache/commons/math3/genetics/BinaryChromosomeTest.java100644 1750 1750 4441 12126627672 31351 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.genetics; import org.junit.Assert; import org.junit.Test; public class BinaryChromosomeTest { @Test public void testInvalidConstructor() { Integer[][] reprs = new Integer[][] { new Integer[] {0,1,0,1,2}, new Integer[] {0,1,0,1,-1} }; for (Integer[] repr : reprs) { try { new DummyBinaryChromosome(repr); Assert.fail("Exception not caught"); } catch (IllegalArgumentException e) { // Expected } } } @Test public void testRandomConstructor() { for (int i=0; i<20; i++) { new DummyBinaryChromosome(BinaryChromosome.randomBinaryRepresentation(10)); } } @Test public void testIsSame() { Chromosome c1 = new DummyBinaryChromosome(new Integer[] {0,1,0,1,0,1}); Chromosome c2 = new DummyBinaryChromosome(new Integer[] {0,1,1,0,1}); Chromosome c3 = new DummyBinaryChromosome(new Integer[] {0,1,0,1,0,1,1}); Chromosome c4 = new DummyBinaryChromosome(new Integer[] {1,1,0,1,0,1}); Chromosome c5 = new DummyBinaryChromosome(new Integer[] {0,1,0,1,0,0}); Chromosome c6 = new DummyBinaryChromosome(new Integer[] {0,1,0,1,0,1}); Assert.assertFalse(c1.isSame(c2)); Assert.assertFalse(c1.isSame(c3)); Assert.assertFalse(c1.isSame(c4)); Assert.assertFalse(c1.isSame(c5)); Assert.assertTrue(c1.isSame(c6)); } } commons-math3-3.2-src/src/test/java/org/apache/commons/math3/genetics/CycleCrossoverTest.java100644 1750 1750 14320 12126627672 31053 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.genetics; import org.apache.commons.math3.exception.DimensionMismatchException; import org.apache.commons.math3.exception.MathIllegalArgumentException; import org.junit.Assert; import org.junit.Test; public class CycleCrossoverTest { @Test public void testCrossoverExample() { // taken from http://www.rubicite.com/Tutorials/GeneticAlgorithms/CrossoverOperators/CycleCrossoverOperator.aspx final Integer[] p1 = new Integer[] { 8, 4, 7, 3, 6, 2, 5, 1, 9, 0 }; final Integer[] p2 = new Integer[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; final DummyListChromosome p1c = new DummyListChromosome(p1); final DummyListChromosome p2c = new DummyListChromosome(p2); final CrossoverPolicy cp = new CycleCrossover(); final ChromosomePair pair = cp.crossover(p1c, p2c); final Integer[] c1 = ((DummyListChromosome) pair.getFirst()).getRepresentation().toArray(new Integer[p1.length]); final Integer[] c2 = ((DummyListChromosome) pair.getSecond()).getRepresentation().toArray(new Integer[p2.length]); final Integer[] c1e = new Integer[] { 8, 1, 2, 3, 4, 5, 6, 7, 9, 0 }; final Integer[] c2e = new Integer[] { 0, 4, 7, 3, 6, 2, 5, 1, 8, 9 }; Assert.assertArrayEquals(c1e, c1); Assert.assertArrayEquals(c2e, c2); } @Test public void testCrossoverExample2() { // taken from http://www.scribd.com/doc/54206412/32/Cycle-crossover final Integer[] p1 = new Integer[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; final Integer[] p2 = new Integer[] { 9, 3, 7, 8, 2, 6, 5, 1, 4}; final DummyListChromosome p1c = new DummyListChromosome(p1); final DummyListChromosome p2c = new DummyListChromosome(p2); final CrossoverPolicy cp = new CycleCrossover(); final ChromosomePair pair = cp.crossover(p1c, p2c); final Integer[] c1 = ((DummyListChromosome) pair.getFirst()).getRepresentation().toArray(new Integer[p1.length]); final Integer[] c2 = ((DummyListChromosome) pair.getSecond()).getRepresentation().toArray(new Integer[p2.length]); final Integer[] c1e = new Integer[] { 1, 3, 7, 4, 2, 6, 5, 8, 9 }; final Integer[] c2e = new Integer[] { 9, 2, 3, 8, 5, 6, 7, 1, 4 }; Assert.assertArrayEquals(c1e, c1); Assert.assertArrayEquals(c2e, c2); } @Test public void testCrossover() { final Integer[] p1 = new Integer[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; final Integer[] p2 = new Integer[] { 10, 9, 8, 7, 6, 5, 4, 3, 2, 1 }; final DummyListChromosome p1c = new DummyListChromosome(p1); final DummyListChromosome p2c = new DummyListChromosome(p2); final CrossoverPolicy cp = new CycleCrossover(true); for (int i = 0; i < 20; i++) { final ChromosomePair pair = cp.crossover(p1c, p2c); final Integer[] c1 = ((DummyListChromosome) pair.getFirst()).getRepresentation().toArray(new Integer[p1.length]); final Integer[] c2 = ((DummyListChromosome) pair.getSecond()).getRepresentation().toArray(new Integer[p2.length]); int index = 0; // Determine if it is in the same spot as in the first parent, if // not it comes from the second parent. for (final Integer j : c1) { if (!p1[index].equals(j)) { Assert.assertEquals(j, p2[index]); } else { Assert.assertEquals(j, p1[index]); } index++; } // Same as above only for the second parent. index = 0; for (final Integer k : c2) { if (p2[index] != k) { Assert.assertEquals(k, p1[index]); } else { Assert.assertEquals(k, p2[index]); } index++; } } } @Test(expected = DimensionMismatchException.class) public void testCrossoverDimensionMismatchException() { final Integer[] p1 = new Integer[] { 1, 0, 1, 0, 0, 1, 0, 1, 1 }; final Integer[] p2 = new Integer[] { 0, 1, 1, 0, 1 }; final BinaryChromosome p1c = new DummyBinaryChromosome(p1); final BinaryChromosome p2c = new DummyBinaryChromosome(p2); final CrossoverPolicy cp = new CycleCrossover(); cp.crossover(p1c, p2c); } @Test(expected = MathIllegalArgumentException.class) public void testCrossoverInvalidFixedLengthChromosomeFirst() { final Integer[] p1 = new Integer[] { 1, 0, 1, 0, 0, 1, 0, 1, 1 }; final BinaryChromosome p1c = new DummyBinaryChromosome(p1); final Chromosome p2c = new Chromosome() { public double fitness() { // Not important return 0; } }; final CrossoverPolicy cp = new CycleCrossover(); cp.crossover(p1c, p2c); } @Test(expected = MathIllegalArgumentException.class) public void testCrossoverInvalidFixedLengthChromosomeSecond() { final Integer[] p1 = new Integer[] { 1, 0, 1, 0, 0, 1, 0, 1, 1 }; final BinaryChromosome p2c = new DummyBinaryChromosome(p1); final Chromosome p1c = new Chromosome() { public double fitness() { // Not important return 0; } }; final CrossoverPolicy cp = new CycleCrossover(); cp.crossover(p1c, p2c); } } ././@LongLink100644 0 0 147 12126630646 10263 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/genetics/ElitisticListPopulationTest.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/genetics/ElitisticListPopulationTest.ja100644 1750 1750 6634 12126627672 32410 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.genetics; import java.util.Collections; import java.util.List; import org.apache.commons.math3.exception.OutOfRangeException; import org.junit.Assert; import org.junit.Test; public class ElitisticListPopulationTest { private static int counter = 0; @Test public void testNextGeneration() { ElitisticListPopulation pop = new ElitisticListPopulation(100, 0.203); for (int i=0; i chromosomes = Collections.emptyList(); final double rate = -0.25; new ElitisticListPopulation(chromosomes, 100, rate); } @Test(expected = OutOfRangeException.class) public void testChromosomeListConstructorTooHigh() { final List chromosomes = Collections.emptyList(); final double rate = 1.25; new ElitisticListPopulation(chromosomes, 100, rate); } private static class DummyChromosome extends Chromosome { private final int fitness; public DummyChromosome() { this.fitness = counter; counter++; } public double fitness() { return this.fitness; } } } commons-math3-3.2-src/src/test/java/org/apache/commons/math3/genetics/OnePointCrossoverTest.java100644 1750 1750 4714 12126627672 31535 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.genetics; import org.junit.Assert; import org.junit.Test; public class OnePointCrossoverTest { @Test public void testCrossover() { Integer[] p1 = new Integer[] {1,0,1,0,0,1,0,1,1}; Integer[] p2 = new Integer[] {0,1,1,0,1,0,1,1,1}; BinaryChromosome p1c = new DummyBinaryChromosome(p1); BinaryChromosome p2c = new DummyBinaryChromosome(p2); OnePointCrossover opc = new OnePointCrossover(); // how to test a stochastic method? for (int i=0; i<20; i++) { ChromosomePair pair = opc.crossover(p1c,p2c); Integer[] c1 = new Integer[p1.length]; Integer[] c2 = new Integer[p2.length]; c1 = ((BinaryChromosome) pair.getFirst()).getRepresentation().toArray(c1); c2 = ((BinaryChromosome) pair.getSecond()).getRepresentation().toArray(c2); // first and last values will be the same Assert.assertEquals((int) p1[0], (int) c1[0]); Assert.assertEquals((int) p2[0], (int) c2[0]); Assert.assertEquals((int) p1[p1.length-1], (int) c1[c1.length-1]); Assert.assertEquals((int) p2[p2.length-1], (int) c2[c2.length-1]); // moreover, in the above setting, the 2nd, 3rd and 7th values will be the same Assert.assertEquals((int) p1[2], (int) c1[2]); Assert.assertEquals((int) p2[2], (int) c2[2]); Assert.assertEquals((int) p1[3], (int) c1[3]); Assert.assertEquals((int) p2[3], (int) c2[3]); Assert.assertEquals((int) p1[7], (int) c1[7]); Assert.assertEquals((int) p2[7], (int) c2[7]); } } } ././@LongLink100644 0 0 154 12126630646 10261 Lustar 0 0 commons-math3-3.2-src/src/test/java/org/apache/commons/math3/genetics/GeneticAlgorithmTestPermutations.javacommons-math3-3.2-src/src/test/java/org/apache/commons/math3/genetics/GeneticAlgorithmTestPermutatio100644 1750 1750 10746 12126627672 32475 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.genetics; import java.util.ArrayList; import java.util.List; import org.apache.commons.math3.util.FastMath; import org.junit.Assert; import org.junit.Test; /** * This is also an example of usage. * * This algorithm does "stochastic sorting" of a sequence 0,...,N. * */ public class GeneticAlgorithmTestPermutations { // parameters for the GA private static final int DIMENSION = 20; private static final int POPULATION_SIZE = 80; private static final int NUM_GENERATIONS = 200; private static final double ELITISM_RATE = 0.2; private static final double CROSSOVER_RATE = 1; private static final double MUTATION_RATE = 0.08; private static final int TOURNAMENT_ARITY = 2; // numbers from 0 to N-1 private static final List sequence = new ArrayList(); static { for (int i=0; i(), CROSSOVER_RATE, new RandomKeyMutation(), MUTATION_RATE, new TournamentSelection(TOURNAMENT_ARITY) ); // initial population Population initial = randomPopulation(); // stopping conditions StoppingCondition stopCond = new FixedGenerationCount(NUM_GENERATIONS); // best initial chromosome Chromosome bestInitial = initial.getFittestChromosome(); // run the algorithm Population finalPopulation = ga.evolve(initial, stopCond); // best chromosome from the final population Chromosome bestFinal = finalPopulation.getFittestChromosome(); // the only thing we can test is whether the final solution is not worse than the initial one // however, for some implementations of GA, this need not be true :) Assert.assertTrue(bestFinal.compareTo(bestInitial) > 0); //System.out.println(bestInitial); //System.out.println(bestFinal); } /** * Initializes a random population */ private static ElitisticListPopulation randomPopulation() { List popList = new ArrayList(); for (int i=0; i { public MinPermutations(List representation) { super(representation); } public double fitness() { int res = 0; List decoded = decode(sequence); for (int i=0; i newFixedLengthChromosome(List chromosomeRepresentation) { return new MinPermutations(chromosomeRepresentation); } } } commons-math3-3.2-src/src/test/java/org/apache/commons/math3/genetics/RandomKeyTest.java100644 1750 1750 15154 12126627672 30005 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.genetics; import java.util.Arrays; import java.util.Comparator; import java.util.List; import org.junit.Assert; import org.junit.Test; public class RandomKeyTest { @Test(expected=IllegalArgumentException.class) public void testConstructor1() { new DummyRandomKey(new Double[] {0.2, 0.3, 1.2}); } @Test(expected=IllegalArgumentException.class) public void testConstructor2() { new DummyRandomKey(new Double[] {0.2, 0.3, -0.2}); } @Test public void testIsSame() { DummyRandomKey drk1 = new DummyRandomKey(new Double[] {0.4, 0.1, 0.5, 0.8, 0.2}); DummyRandomKey drk2 = new DummyRandomKey(new Double[] {0.4, 0.1, 0.5, 0.8, 0.2}); DummyRandomKey drk3 = new DummyRandomKey(new Double[] {0.4, 0.15, 0.5, 0.8, 0.2}); DummyRandomKey drk4 = new DummyRandomKey(new Double[] {0.4, 0.25, 0.5, 0.8, 0.2}); DummyRandomKey drk5 = new DummyRandomKey(new Double[] {0.4, 0.25, 0.5, 0.8, 0.2, 0.5}); Assert.assertTrue(drk1.isSame(drk2)); Assert.assertTrue(drk2.isSame(drk3)); Assert.assertFalse(drk3.isSame(drk4)); Assert.assertFalse(drk4.isSame(drk5)); } @Test public void testDecode() { DummyRandomKey drk = new DummyRandomKey(new Double[] {0.4, 0.1, 0.5, 0.8, 0.2}); List decoded = drk.decode(Arrays.asList(new String[] {"a", "b", "c", "d", "e"})); Assert.assertEquals("b", decoded.get(0)); Assert.assertEquals("e", decoded.get(1)); Assert.assertEquals("a", decoded.get(2)); Assert.assertEquals("c", decoded.get(3)); Assert.assertEquals("d", decoded.get(4)); } @Test(expected=IllegalArgumentException.class) public void testInvalidRepresentation() { new DummyRandomKey(new Double[] {0.1, 0.1, 2d, 0.8, 0.2}); } @Test public void testRandomPermutation() { // never generate an invalid one for (int i=0; i<10; i++) { DummyRandomKey drk = new DummyRandomKey(RandomKey.randomPermutation(20)); Assert.assertNotNull(drk); } } @Test public void testIdentityPermutation() { DummyRandomKey drk = new DummyRandomKey(RandomKey.identityPermutation(5)); List decoded = drk.decode(Arrays.asList(new String[] {"a", "b", "c", "d", "e"})); Assert.assertEquals("a", decoded.get(0)); Assert.assertEquals("b", decoded.get(1)); Assert.assertEquals("c", decoded.get(2)); Assert.assertEquals("d", decoded.get(3)); Assert.assertEquals("e", decoded.get(4)); } @Test public void testComparatorPermutation() { List data = Arrays.asList(new String[] {"x", "b", "c", "z", "b"}); List permutation = RandomKey.comparatorPermutation(data, new Comparator() { public int compare(String o1, String o2) { return o1.compareTo(o2); } }); Double[] permArr = new Double[data.size()]; permArr = permutation.toArray(permArr); Assert.assertArrayEquals(new Double[] {0.6,0.0,0.4,0.8,0.2}, permArr); List decodedData = new DummyRandomKey(permutation).decode(data); Assert.assertEquals("b", decodedData.get(0)); Assert.assertEquals("b", decodedData.get(1)); Assert.assertEquals("c", decodedData.get(2)); Assert.assertEquals("x", decodedData.get(3)); Assert.assertEquals("z", decodedData.get(4)); permutation = RandomKey.comparatorPermutation(data, new Comparator() { public int compare(String o1, String o2) { return o2.compareTo(o1); } }); permArr = new Double[data.size()]; permArr = permutation.toArray(permArr); Assert.assertArrayEquals(new Double[] {0.2,0.6,0.4,0.0,0.8}, permArr); decodedData = new DummyRandomKey(permutation).decode(data); Assert.assertEquals("z", decodedData.get(0)); Assert.assertEquals("x", decodedData.get(1)); Assert.assertEquals("c", decodedData.get(2)); Assert.assertEquals("b", decodedData.get(3)); Assert.assertEquals("b", decodedData.get(4)); } @Test public void testInducedPermutation() { List origData = Arrays.asList(new String[] {"a", "b", "c", "d", "d"}); List permutedData = Arrays.asList(new String[] {"d", "b", "c", "a", "d"}); DummyRandomKey drk = new DummyRandomKey(RandomKey.inducedPermutation(origData, permutedData)); List decoded = drk.decode(origData); Assert.assertEquals("d", decoded.get(0)); Assert.assertEquals("b", decoded.get(1)); Assert.assertEquals("c", decoded.get(2)); Assert.assertEquals("a", decoded.get(3)); Assert.assertEquals("d", decoded.get(4)); try { RandomKey.inducedPermutation( Arrays.asList(new String[] {"a", "b", "c", "d", "d"}), Arrays.asList(new String[] {"a", "b", "c", "d"}) ); Assert.fail("Uncaught exception"); } catch (IllegalArgumentException e) { // no-op } try { RandomKey.inducedPermutation( Arrays.asList(new String[] {"a", "b", "c", "d", "d"}), Arrays.asList(new String[] {"a", "b", "c", "d", "f"}) ); Assert.fail("Uncaught exception"); } catch (IllegalArgumentException e) { // no-op } } @Test public void testEqualRepr() { DummyRandomKey drk = new DummyRandomKey(new Double[] {0.2, 0.2, 0.5}); List decodedData = drk.decode(Arrays.asList(new String[] {"a", "b", "c"})); Assert.assertEquals("a", decodedData.get(0)); Assert.assertEquals("b", decodedData.get(1)); Assert.assertEquals("c", decodedData.get(2)); } } commons-math3-3.2-src/src/test/java/org/apache/commons/math3/genetics/DummyBinaryChromosome.java100644 1750 1750 2714 12126627672 31526 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.genetics; import java.util.List; /** * Implementation of BinaryChromosome for testing purposes */ public class DummyBinaryChromosome extends BinaryChromosome { public DummyBinaryChromosome(List representation) { super(representation); } public DummyBinaryChromosome(Integer[] representation) { super(representation); } @Override public AbstractListChromosome newFixedLengthChromosome(List chromosomeRepresentation) { return new DummyBinaryChromosome(chromosomeRepresentation); } public double fitness() { // uninteresting return 0; } } commons-math3-3.2-src/src/test/java/org/apache/commons/math3/genetics/TournamentSelectionTest.java100644 1750 1750 3571 12126627672 32076 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.genetics; import org.junit.Assert; import org.junit.Test; public class TournamentSelectionTest { private static int counter = 0; @Test public void testSelect() { TournamentSelection ts = new TournamentSelection(2); ElitisticListPopulation pop = new ElitisticListPopulation(100, 0.203); for (int i=0; i 0); Assert.assertTrue(pair.getSecond().getFitness() > 0); } } private static class DummyChromosome extends Chromosome { private final int fitness; public DummyChromosome() { this.fitness = counter; counter++; } public double fitness() { return this.fitness; } } } commons-math3-3.2-src/src/test/java/org/apache/commons/math3/genetics/UniformCrossoverTest.java100644 1750 1750 12025 12126627672 31433 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.genetics; import java.util.ArrayList; import java.util.List; import org.junit.Assert; import org.apache.commons.math3.exception.DimensionMismatchException; import org.apache.commons.math3.exception.MathIllegalArgumentException; import org.apache.commons.math3.exception.OutOfRangeException; import org.junit.BeforeClass; import org.junit.Test; public class UniformCrossoverTest { private static final int LEN = 10000; private static final List p1 = new ArrayList(LEN); private static final List p2 = new ArrayList(LEN); @BeforeClass public static void setUpBeforeClass() { for (int i = 0; i < LEN; i++) { p1.add(0); p2.add(1); } } @Test(expected = OutOfRangeException.class) public void testRatioTooLow() { new UniformCrossover(-0.5d); } @Test(expected = OutOfRangeException.class) public void testRatioTooHigh() { new UniformCrossover(1.5d); } @Test public void testCrossover() { // test crossover with different ratios performCrossover(0.5); performCrossover(0.7); performCrossover(0.2); } private void performCrossover(double ratio) { final DummyBinaryChromosome p1c = new DummyBinaryChromosome(p1); final DummyBinaryChromosome p2c = new DummyBinaryChromosome(p2); final CrossoverPolicy cp = new UniformCrossover(ratio); // make a number of rounds for (int i = 0; i < 20; i++) { final ChromosomePair pair = cp.crossover(p1c, p2c); final List c1 = ((DummyBinaryChromosome) pair.getFirst()).getRepresentation(); final List c2 = ((DummyBinaryChromosome) pair.getSecond()).getRepresentation(); int from1 = 0; int from2 = 0; // check first child for (int val : c1) { if (val == 0) { from1++; } else { from2++; } } Assert.assertEquals(1.0 - ratio, Double.valueOf((double) from1 / LEN), 0.1); Assert.assertEquals(ratio, Double.valueOf((double) from2 / LEN), 0.1); from1 = 0; from2 = 0; // check second child for (int val : c2) { if (val == 0) { from1++; } else { from2++; } } Assert.assertEquals(ratio, Double.valueOf((double) from1 / LEN), 0.1); Assert.assertEquals(1.0 - ratio, Double.valueOf((double) from2 / LEN), 0.1); } } @Test(expected = DimensionMismatchException.class) public void testCrossoverDimensionMismatchException(){ final Integer[] p1 = new Integer[] {1,0,1,0,0,1,0,1,1}; final Integer[] p2 = new Integer[] {0,1,1,0,1}; final BinaryChromosome p1c = new DummyBinaryChromosome(p1); final BinaryChromosome p2c = new DummyBinaryChromosome(p2); final CrossoverPolicy cp = new UniformCrossover(0.5d); cp.crossover(p1c, p2c); } @Test(expected = MathIllegalArgumentException.class) public void testCrossoverInvalidFixedLengthChromosomeFirst() { final Integer[] p1 = new Integer[] {1,0,1,0,0,1,0,1,1}; final BinaryChromosome p1c = new DummyBinaryChromosome(p1); final Chromosome p2c = new Chromosome() { public double fitness() { // Not important return 0; } }; final CrossoverPolicy cp = new UniformCrossover(0.5d); cp.crossover(p1c, p2c); } @Test(expected = MathIllegalArgumentException.class) public void testCrossoverInvalidFixedLengthChromosomeSecond() { final Integer[] p1 = new Integer[] {1,0,1,0,0,1,0,1,1}; final BinaryChromosome p2c = new DummyBinaryChromosome(p1); final Chromosome p1c = new Chromosome() { public double fitness() { // Not important return 0; } }; final CrossoverPolicy cp = new UniformCrossover(0.5d); cp.crossover(p1c, p2c); } } commons-math3-3.2-src/src/test/java/org/apache/commons/math3/fraction/FractionTest.java100644 1750 1750 55622 12126627672 27671 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.fraction; import org.apache.commons.math3.exception.ConvergenceException; import org.apache.commons.math3.exception.MathIllegalArgumentException; import org.apache.commons.math3.exception.MathArithmeticException; import org.apache.commons.math3.TestUtils; import org.apache.commons.math3.util.FastMath; import org.junit.Assert; import org.junit.Test; /** * @version $Id: FractionTest.java 1368253 2012-08-01 21:24:27Z tn $ */ public class FractionTest { private void assertFraction(int expectedNumerator, int expectedDenominator, Fraction actual) { Assert.assertEquals(expectedNumerator, actual.getNumerator()); Assert.assertEquals(expectedDenominator, actual.getDenominator()); } @Test public void testConstructor() { assertFraction(0, 1, new Fraction(0, 1)); assertFraction(0, 1, new Fraction(0, 2)); assertFraction(0, 1, new Fraction(0, -1)); assertFraction(1, 2, new Fraction(1, 2)); assertFraction(1, 2, new Fraction(2, 4)); assertFraction(-1, 2, new Fraction(-1, 2)); assertFraction(-1, 2, new Fraction(1, -2)); assertFraction(-1, 2, new Fraction(-2, 4)); assertFraction(-1, 2, new Fraction(2, -4)); // overflow try { new Fraction(Integer.MIN_VALUE, -1); Assert.fail(); } catch (MathArithmeticException ex) { // success } try { new Fraction(1, Integer.MIN_VALUE); Assert.fail(); } catch (MathArithmeticException ex) { // success } assertFraction(0, 1, new Fraction(0.00000000000001)); assertFraction(2, 5, new Fraction(0.40000000000001)); assertFraction(15, 1, new Fraction(15.0000000000001)); } @Test(expected=ConvergenceException.class) public void testGoldenRatio() { // the golden ratio is notoriously a difficult number for continuous fraction new Fraction((1 + FastMath.sqrt(5)) / 2, 1.0e-12, 25); } // MATH-179 @Test public void testDoubleConstructor() throws ConvergenceException { assertFraction(1, 2, new Fraction((double)1 / (double)2)); assertFraction(1, 3, new Fraction((double)1 / (double)3)); assertFraction(2, 3, new Fraction((double)2 / (double)3)); assertFraction(1, 4, new Fraction((double)1 / (double)4)); assertFraction(3, 4, new Fraction((double)3 / (double)4)); assertFraction(1, 5, new Fraction((double)1 / (double)5)); assertFraction(2, 5, new Fraction((double)2 / (double)5)); assertFraction(3, 5, new Fraction((double)3 / (double)5)); assertFraction(4, 5, new Fraction((double)4 / (double)5)); assertFraction(1, 6, new Fraction((double)1 / (double)6)); assertFraction(5, 6, new Fraction((double)5 / (double)6)); assertFraction(1, 7, new Fraction((double)1 / (double)7)); assertFraction(2, 7, new Fraction((double)2 / (double)7)); assertFraction(3, 7, new Fraction((double)3 / (double)7)); assertFraction(4, 7, new Fraction((double)4 / (double)7)); assertFraction(5, 7, new Fraction((double)5 / (double)7)); assertFraction(6, 7, new Fraction((double)6 / (double)7)); assertFraction(1, 8, new Fraction((double)1 / (double)8)); assertFraction(3, 8, new Fraction((double)3 / (double)8)); assertFraction(5, 8, new Fraction((double)5 / (double)8)); assertFraction(7, 8, new Fraction((double)7 / (double)8)); assertFraction(1, 9, new Fraction((double)1 / (double)9)); assertFraction(2, 9, new Fraction((double)2 / (double)9)); assertFraction(4, 9, new Fraction((double)4 / (double)9)); assertFraction(5, 9, new Fraction((double)5 / (double)9)); assertFraction(7, 9, new Fraction((double)7 / (double)9)); assertFraction(8, 9, new Fraction((double)8 / (double)9)); assertFraction(1, 10, new Fraction((double)1 / (double)10)); assertFraction(3, 10, new Fraction((double)3 / (double)10)); assertFraction(7, 10, new Fraction((double)7 / (double)10)); assertFraction(9, 10, new Fraction((double)9 / (double)10)); assertFraction(1, 11, new Fraction((double)1 / (double)11)); assertFraction(2, 11, new Fraction((double)2 / (double)11)); assertFraction(3, 11, new Fraction((double)3 / (double)11)); assertFraction(4, 11, new Fraction((double)4 / (double)11)); assertFraction(5, 11, new Fraction((double)5 / (double)11)); assertFraction(6, 11, new Fraction((double)6 / (double)11)); assertFraction(7, 11, new Fraction((double)7 / (double)11)); assertFraction(8, 11, new Fraction((double)8 / (double)11)); assertFraction(9, 11, new Fraction((double)9 / (double)11)); assertFraction(10, 11, new Fraction((double)10 / (double)11)); } // MATH-181 @Test public void testDigitLimitConstructor() throws ConvergenceException { assertFraction(2, 5, new Fraction(0.4, 9)); assertFraction(2, 5, new Fraction(0.4, 99)); assertFraction(2, 5, new Fraction(0.4, 999)); assertFraction(3, 5, new Fraction(0.6152, 9)); assertFraction(8, 13, new Fraction(0.6152, 99)); assertFraction(510, 829, new Fraction(0.6152, 999)); assertFraction(769, 1250, new Fraction(0.6152, 9999)); } @Test public void testIntegerOverflow() { checkIntegerOverflow(0.75000000001455192); checkIntegerOverflow(1.0e10); checkIntegerOverflow(-1.0e10); checkIntegerOverflow(-43979.60679604749); } private void checkIntegerOverflow(double a) { try { new Fraction(a, 1.0e-12, 1000); Assert.fail("an exception should have been thrown"); } catch (ConvergenceException ce) { // expected behavior } } @Test public void testEpsilonLimitConstructor() throws ConvergenceException { assertFraction(2, 5, new Fraction(0.4, 1.0e-5, 100)); assertFraction(3, 5, new Fraction(0.6152, 0.02, 100)); assertFraction(8, 13, new Fraction(0.6152, 1.0e-3, 100)); assertFraction(251, 408, new Fraction(0.6152, 1.0e-4, 100)); assertFraction(251, 408, new Fraction(0.6152, 1.0e-5, 100)); assertFraction(510, 829, new Fraction(0.6152, 1.0e-6, 100)); assertFraction(769, 1250, new Fraction(0.6152, 1.0e-7, 100)); } @Test public void testCompareTo() { Fraction first = new Fraction(1, 2); Fraction second = new Fraction(1, 3); Fraction third = new Fraction(1, 2); Assert.assertEquals(0, first.compareTo(first)); Assert.assertEquals(0, first.compareTo(third)); Assert.assertEquals(1, first.compareTo(second)); Assert.assertEquals(-1, second.compareTo(first)); // these two values are different approximations of PI // the first one is approximately PI - 3.07e-18 // the second one is approximately PI + 1.936e-17 Fraction pi1 = new Fraction(1068966896, 340262731); Fraction pi2 = new Fraction( 411557987, 131002976); Assert.assertEquals(-1, pi1.compareTo(pi2)); Assert.assertEquals( 1, pi2.compareTo(pi1)); Assert.assertEquals(0.0, pi1.doubleValue() - pi2.doubleValue(), 1.0e-20); } @Test public void testDoubleValue() { Fraction first = new Fraction(1, 2); Fraction second = new Fraction(1, 3); Assert.assertEquals(0.5, first.doubleValue(), 0.0); Assert.assertEquals(1.0 / 3.0, second.doubleValue(), 0.0); } @Test public void testFloatValue() { Fraction first = new Fraction(1, 2); Fraction second = new Fraction(1, 3); Assert.assertEquals(0.5f, first.floatValue(), 0.0f); Assert.assertEquals((float)(1.0 / 3.0), second.floatValue(), 0.0f); } @Test public void testIntValue() { Fraction first = new Fraction(1, 2); Fraction second = new Fraction(3, 2); Assert.assertEquals(0, first.intValue()); Assert.assertEquals(1, second.intValue()); } @Test public void testLongValue() { Fraction first = new Fraction(1, 2); Fraction second = new Fraction(3, 2); Assert.assertEquals(0L, first.longValue()); Assert.assertEquals(1L, second.longValue()); } @Test public void testConstructorDouble() { assertFraction(1, 2, new Fraction(0.5)); assertFraction(1, 3, new Fraction(1.0 / 3.0)); assertFraction(17, 100, new Fraction(17.0 / 100.0)); assertFraction(317, 100, new Fraction(317.0 / 100.0)); assertFraction(-1, 2, new Fraction(-0.5)); assertFraction(-1, 3, new Fraction(-1.0 / 3.0)); assertFraction(-17, 100, new Fraction(17.0 / -100.0)); assertFraction(-317, 100, new Fraction(-317.0 / 100.0)); } @Test public void testAbs() { Fraction a = new Fraction(10, 21); Fraction b = new Fraction(-10, 21); Fraction c = new Fraction(10, -21); assertFraction(10, 21, a.abs()); assertFraction(10, 21, b.abs()); assertFraction(10, 21, c.abs()); } @Test public void testPercentage() { Assert.assertEquals(50.0, new Fraction(1, 2).percentageValue(), 1.0e-15); } @Test public void testMath835() { final int numer = Integer.MAX_VALUE / 99; final int denom = 1; final double percentage = 100 * ((double) numer) / denom; final Fraction frac = new Fraction(numer, denom); // With the implementation that preceded the fix suggested in MATH-835, // this test was failing, due to overflow. Assert.assertEquals(percentage, frac.percentageValue(), Math.ulp(percentage)); } @Test public void testReciprocal() { Fraction f = null; f = new Fraction(50, 75); f = f.reciprocal(); Assert.assertEquals(3, f.getNumerator()); Assert.assertEquals(2, f.getDenominator()); f = new Fraction(4, 3); f = f.reciprocal(); Assert.assertEquals(3, f.getNumerator()); Assert.assertEquals(4, f.getDenominator()); f = new Fraction(-15, 47); f = f.reciprocal(); Assert.assertEquals(-47, f.getNumerator()); Assert.assertEquals(15, f.getDenominator()); f = new Fraction(0, 3); try { f = f.reciprocal(); Assert.fail("expecting MathArithmeticException"); } catch (MathArithmeticException ex) {} // large values f = new Fraction(Integer.MAX_VALUE, 1); f = f.reciprocal(); Assert.assertEquals(1, f.getNumerator()); Assert.assertEquals(Integer.MAX_VALUE, f.getDenominator()); } @Test public void testNegate() { Fraction f = null; f = new Fraction(50, 75); f = f.negate(); Assert.assertEquals(-2, f.getNumerator()); Assert.assertEquals(3, f.getDenominator()); f = new Fraction(-50, 75); f = f.negate(); Assert.assertEquals(2, f.getNumerator()); Assert.assertEquals(3, f.getDenominator()); // large values f = new Fraction(Integer.MAX_VALUE-1, Integer.MAX_VALUE); f = f.negate(); Assert.assertEquals(Integer.MIN_VALUE+2, f.getNumerator()); Assert.assertEquals(Integer.MAX_VALUE, f.getDenominator()); f = new Fraction(Integer.MIN_VALUE, 1); try { f = f.negate(); Assert.fail("expecting MathArithmeticException"); } catch (MathArithmeticException ex) {} } @Test public void testAdd() { Fraction a = new Fraction(1, 2); Fraction b = new Fraction(2, 3); assertFraction(1, 1, a.add(a)); assertFraction(7, 6, a.add(b)); assertFraction(7, 6, b.add(a)); assertFraction(4, 3, b.add(b)); Fraction f1 = new Fraction(Integer.MAX_VALUE - 1, 1); Fraction f2 = Fraction.ONE; Fraction f = f1.add(f2); Assert.assertEquals(Integer.MAX_VALUE, f.getNumerator()); Assert.assertEquals(1, f.getDenominator()); f = f1.add(1); Assert.assertEquals(Integer.MAX_VALUE, f.getNumerator()); Assert.assertEquals(1, f.getDenominator()); f1 = new Fraction(-1, 13*13*2*2); f2 = new Fraction(-2, 13*17*2); f = f1.add(f2); Assert.assertEquals(13*13*17*2*2, f.getDenominator()); Assert.assertEquals(-17 - 2*13*2, f.getNumerator()); try { f.add(null); Assert.fail("expecting MathIllegalArgumentException"); } catch (MathIllegalArgumentException ex) {} // if this fraction is added naively, it will overflow. // check that it doesn't. f1 = new Fraction(1,32768*3); f2 = new Fraction(1,59049); f = f1.add(f2); Assert.assertEquals(52451, f.getNumerator()); Assert.assertEquals(1934917632, f.getDenominator()); f1 = new Fraction(Integer.MIN_VALUE, 3); f2 = new Fraction(1,3); f = f1.add(f2); Assert.assertEquals(Integer.MIN_VALUE+1, f.getNumerator()); Assert.assertEquals(3, f.getDenominator()); f1 = new Fraction(Integer.MAX_VALUE - 1, 1); f2 = Fraction.ONE; f = f1.add(f2); Assert.assertEquals(Integer.MAX_VALUE, f.getNumerator()); Assert.assertEquals(1, f.getDenominator()); try { f = f.add(Fraction.ONE); // should overflow Assert.fail("expecting MathArithmeticException but got: " + f.toString()); } catch (MathArithmeticException ex) {} // denominator should not be a multiple of 2 or 3 to trigger overflow f1 = new Fraction(Integer.MIN_VALUE, 5); f2 = new Fraction(-1,5); try { f = f1.add(f2); // should overflow Assert.fail("expecting MathArithmeticException but got: " + f.toString()); } catch (MathArithmeticException ex) {} try { f= new Fraction(-Integer.MAX_VALUE, 1); f = f.add(f); Assert.fail("expecting MathArithmeticException"); } catch (MathArithmeticException ex) {} try { f= new Fraction(-Integer.MAX_VALUE, 1); f = f.add(f); Assert.fail("expecting MathArithmeticException"); } catch (MathArithmeticException ex) {} f1 = new Fraction(3,327680); f2 = new Fraction(2,59049); try { f = f1.add(f2); // should overflow Assert.fail("expecting MathArithmeticException but got: " + f.toString()); } catch (MathArithmeticException ex) {} } @Test public void testDivide() { Fraction a = new Fraction(1, 2); Fraction b = new Fraction(2, 3); assertFraction(1, 1, a.divide(a)); assertFraction(3, 4, a.divide(b)); assertFraction(4, 3, b.divide(a)); assertFraction(1, 1, b.divide(b)); Fraction f1 = new Fraction(3, 5); Fraction f2 = Fraction.ZERO; try { f1.divide(f2); Assert.fail("expecting MathArithmeticException"); } catch (MathArithmeticException ex) {} f1 = new Fraction(0, 5); f2 = new Fraction(2, 7); Fraction f = f1.divide(f2); Assert.assertSame(Fraction.ZERO, f); f1 = new Fraction(2, 7); f2 = Fraction.ONE; f = f1.divide(f2); Assert.assertEquals(2, f.getNumerator()); Assert.assertEquals(7, f.getDenominator()); f1 = new Fraction(1, Integer.MAX_VALUE); f = f1.divide(f1); Assert.assertEquals(1, f.getNumerator()); Assert.assertEquals(1, f.getDenominator()); f1 = new Fraction(Integer.MIN_VALUE, Integer.MAX_VALUE); f2 = new Fraction(1, Integer.MAX_VALUE); f = f1.divide(f2); Assert.assertEquals(Integer.MIN_VALUE, f.getNumerator()); Assert.assertEquals(1, f.getDenominator()); try { f.divide(null); Assert.fail("MathIllegalArgumentException"); } catch (MathIllegalArgumentException ex) {} try { f1 = new Fraction(1, Integer.MAX_VALUE); f = f1.divide(f1.reciprocal()); // should overflow Assert.fail("expecting MathArithmeticException"); } catch (MathArithmeticException ex) {} try { f1 = new Fraction(1, -Integer.MAX_VALUE); f = f1.divide(f1.reciprocal()); // should overflow Assert.fail("expecting MathArithmeticException"); } catch (MathArithmeticException ex) {} f1 = new Fraction(6, 35); f = f1.divide(15); Assert.assertEquals(2, f.getNumerator()); Assert.assertEquals(175, f.getDenominator()); } @Test public void testMultiply() { Fraction a = new Fraction(1, 2); Fraction b = new Fraction(2, 3); assertFraction(1, 4, a.multiply(a)); assertFraction(1, 3, a.multiply(b)); assertFraction(1, 3, b.multiply(a)); assertFraction(4, 9, b.multiply(b)); Fraction f1 = new Fraction(Integer.MAX_VALUE, 1); Fraction f2 = new Fraction(Integer.MIN_VALUE, Integer.MAX_VALUE); Fraction f = f1.multiply(f2); Assert.assertEquals(Integer.MIN_VALUE, f.getNumerator()); Assert.assertEquals(1, f.getDenominator()); try { f.multiply(null); Assert.fail("expecting MathIllegalArgumentException"); } catch (MathIllegalArgumentException ex) {} f1 = new Fraction(6, 35); f = f1.multiply(15); Assert.assertEquals(18, f.getNumerator()); Assert.assertEquals(7, f.getDenominator()); } @Test public void testSubtract() { Fraction a = new Fraction(1, 2); Fraction b = new Fraction(2, 3); assertFraction(0, 1, a.subtract(a)); assertFraction(-1, 6, a.subtract(b)); assertFraction(1, 6, b.subtract(a)); assertFraction(0, 1, b.subtract(b)); Fraction f = new Fraction(1,1); try { f.subtract(null); Assert.fail("expecting MathIllegalArgumentException"); } catch (MathIllegalArgumentException ex) {} // if this fraction is subtracted naively, it will overflow. // check that it doesn't. Fraction f1 = new Fraction(1,32768*3); Fraction f2 = new Fraction(1,59049); f = f1.subtract(f2); Assert.assertEquals(-13085, f.getNumerator()); Assert.assertEquals(1934917632, f.getDenominator()); f1 = new Fraction(Integer.MIN_VALUE, 3); f2 = new Fraction(1,3).negate(); f = f1.subtract(f2); Assert.assertEquals(Integer.MIN_VALUE+1, f.getNumerator()); Assert.assertEquals(3, f.getDenominator()); f1 = new Fraction(Integer.MAX_VALUE, 1); f2 = Fraction.ONE; f = f1.subtract(f2); Assert.assertEquals(Integer.MAX_VALUE-1, f.getNumerator()); Assert.assertEquals(1, f.getDenominator()); f = f1.subtract(1); Assert.assertEquals(Integer.MAX_VALUE-1, f.getNumerator()); Assert.assertEquals(1, f.getDenominator()); try { f1 = new Fraction(1, Integer.MAX_VALUE); f2 = new Fraction(1, Integer.MAX_VALUE - 1); f = f1.subtract(f2); Assert.fail("expecting MathArithmeticException"); //should overflow } catch (MathArithmeticException ex) {} // denominator should not be a multiple of 2 or 3 to trigger overflow f1 = new Fraction(Integer.MIN_VALUE, 5); f2 = new Fraction(1,5); try { f = f1.subtract(f2); // should overflow Assert.fail("expecting MathArithmeticException but got: " + f.toString()); } catch (MathArithmeticException ex) {} try { f= new Fraction(Integer.MIN_VALUE, 1); f = f.subtract(Fraction.ONE); Assert.fail("expecting MathArithmeticException"); } catch (MathArithmeticException ex) {} try { f= new Fraction(Integer.MAX_VALUE, 1); f = f.subtract(Fraction.ONE.negate()); Assert.fail("expecting MathArithmeticException"); } catch (MathArithmeticException ex) {} f1 = new Fraction(3,327680); f2 = new Fraction(2,59049); try { f = f1.subtract(f2); // should overflow Assert.fail("expecting MathArithmeticException but got: " + f.toString()); } catch (MathArithmeticException ex) {} } @Test public void testEqualsAndHashCode() { Fraction zero = new Fraction(0,1); Fraction nullFraction = null; Assert.assertTrue( zero.equals(zero)); Assert.assertFalse(zero.equals(nullFraction)); Assert.assertFalse(zero.equals(Double.valueOf(0))); Fraction zero2 = new Fraction(0,2); Assert.assertTrue(zero.equals(zero2)); Assert.assertEquals(zero.hashCode(), zero2.hashCode()); Fraction one = new Fraction(1,1); Assert.assertFalse((one.equals(zero) ||zero.equals(one))); } @Test public void testGetReducedFraction() { Fraction threeFourths = new Fraction(3, 4); Assert.assertTrue(threeFourths.equals(Fraction.getReducedFraction(6, 8))); Assert.assertTrue(Fraction.ZERO.equals(Fraction.getReducedFraction(0, -1))); try { Fraction.getReducedFraction(1, 0); Assert.fail("expecting MathArithmeticException"); } catch (MathArithmeticException ex) { // expected } Assert.assertEquals(Fraction.getReducedFraction (2, Integer.MIN_VALUE).getNumerator(),-1); Assert.assertEquals(Fraction.getReducedFraction (1, -1).getNumerator(), -1); } @Test public void testToString() { Assert.assertEquals("0", new Fraction(0, 3).toString()); Assert.assertEquals("3", new Fraction(6, 2).toString()); Assert.assertEquals("2 / 3", new Fraction(18, 27).toString()); } @Test public void testSerial() throws FractionConversionException { Fraction[] fractions = { new Fraction(3, 4), Fraction.ONE, Fraction.ZERO, new Fraction(17), new Fraction(FastMath.PI, 1000), new Fraction(-5, 2) }; for (Fraction fraction : fractions) { Assert.assertEquals(fraction, TestUtils.serializeAndRecover(fraction)); } } } commons-math3-3.2-src/src/test/java/org/apache/commons/math3/fraction/BigFractionFieldTest.java100644 1750 1750 2746 12126627672 31236 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.fraction; import org.apache.commons.math3.TestUtils; import org.junit.Assert; import org.junit.Test; public class BigFractionFieldTest { @Test public void testZero() { Assert.assertEquals(BigFraction.ZERO, BigFractionField.getInstance().getZero()); } @Test public void testOne() { Assert.assertEquals(BigFraction.ONE, BigFractionField.getInstance().getOne()); } @Test public void testSerial() { // deserializing the singleton should give the singleton itself back BigFractionField field = BigFractionField.getInstance(); Assert.assertTrue(field == TestUtils.serializeAndRecover(field)); } } commons-math3-3.2-src/src/test/java/org/apache/commons/math3/fraction/FractionFieldTest.java100644 1750 1750 2721 12126627672 30605 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.fraction; import org.apache.commons.math3.TestUtils; import org.junit.Assert; import org.junit.Test; public class FractionFieldTest { @Test public void testZero() { Assert.assertEquals(Fraction.ZERO, FractionField.getInstance().getZero()); } @Test public void testOne() { Assert.assertEquals(Fraction.ONE, FractionField.getInstance().getOne()); } @Test public void testSerial() { // deserializing the singleton should give the singleton itself back FractionField field = FractionField.getInstance(); Assert.assertTrue(field == TestUtils.serializeAndRecover(field)); } } commons-math3-3.2-src/src/test/java/org/apache/commons/math3/fraction/BigFractionFormatTest.java100644 1750 1750 24704 12126627672 31461 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.fraction; import java.math.BigDecimal; import java.math.BigInteger; import java.text.NumberFormat; import java.util.Locale; import org.apache.commons.math3.exception.MathParseException; import org.apache.commons.math3.util.FastMath; import org.junit.Assert; import org.junit.Before; import org.junit.Test; public class BigFractionFormatTest { BigFractionFormat properFormat = null; BigFractionFormat improperFormat = null; protected Locale getLocale() { return Locale.getDefault(); } @Before public void setUp() { properFormat = BigFractionFormat.getProperInstance(getLocale()); improperFormat = BigFractionFormat.getImproperInstance(getLocale()); } @Test public void testFormat() { BigFraction c = new BigFraction(1, 2); String expected = "1 / 2"; String actual = properFormat.format(c); Assert.assertEquals(expected, actual); actual = improperFormat.format(c); Assert.assertEquals(expected, actual); } @Test public void testFormatNegative() { BigFraction c = new BigFraction(-1, 2); String expected = "-1 / 2"; String actual = properFormat.format(c); Assert.assertEquals(expected, actual); actual = improperFormat.format(c); Assert.assertEquals(expected, actual); } @Test public void testFormatZero() { BigFraction c = new BigFraction(0, 1); String expected = "0 / 1"; String actual = properFormat.format(c); Assert.assertEquals(expected, actual); actual = improperFormat.format(c); Assert.assertEquals(expected, actual); } @Test public void testFormatImproper() { BigFraction c = new BigFraction(5, 3); String actual = properFormat.format(c); Assert.assertEquals("1 2 / 3", actual); actual = improperFormat.format(c); Assert.assertEquals("5 / 3", actual); } @Test public void testFormatImproperNegative() { BigFraction c = new BigFraction(-5, 3); String actual = properFormat.format(c); Assert.assertEquals("-1 2 / 3", actual); actual = improperFormat.format(c); Assert.assertEquals("-5 / 3", actual); } @Test public void testParse() { String source = "1 / 2"; { BigFraction c = properFormat.parse(source); Assert.assertNotNull(c); Assert.assertEquals(BigInteger.ONE, c.getNumerator()); Assert.assertEquals(BigInteger.valueOf(2l), c.getDenominator()); c = improperFormat.parse(source); Assert.assertNotNull(c); Assert.assertEquals(BigInteger.ONE, c.getNumerator()); Assert.assertEquals(BigInteger.valueOf(2l), c.getDenominator()); } } @Test public void testParseInteger() { String source = "10"; { BigFraction c = properFormat.parse(source); Assert.assertNotNull(c); Assert.assertEquals(BigInteger.TEN, c.getNumerator()); Assert.assertEquals(BigInteger.ONE, c.getDenominator()); } { BigFraction c = improperFormat.parse(source); Assert.assertNotNull(c); Assert.assertEquals(BigInteger.TEN, c.getNumerator()); Assert.assertEquals(BigInteger.ONE, c.getDenominator()); } } @Test public void testParseInvalid() { String source = "a"; String msg = "should not be able to parse '10 / a'."; try { properFormat.parse(source); Assert.fail(msg); } catch (MathParseException ex) { // success } try { improperFormat.parse(source); Assert.fail(msg); } catch (MathParseException ex) { // success } } @Test public void testParseInvalidDenominator() { String source = "10 / a"; String msg = "should not be able to parse '10 / a'."; try { properFormat.parse(source); Assert.fail(msg); } catch (MathParseException ex) { // success } try { improperFormat.parse(source); Assert.fail(msg); } catch (MathParseException ex) { // success } } @Test public void testParseNegative() { { String source = "-1 / 2"; BigFraction c = properFormat.parse(source); Assert.assertNotNull(c); Assert.assertEquals(-1, c.getNumeratorAsInt()); Assert.assertEquals(2, c.getDenominatorAsInt()); c = improperFormat.parse(source); Assert.assertNotNull(c); Assert.assertEquals(-1, c.getNumeratorAsInt()); Assert.assertEquals(2, c.getDenominatorAsInt()); source = "1 / -2"; c = properFormat.parse(source); Assert.assertNotNull(c); Assert.assertEquals(-1, c.getNumeratorAsInt()); Assert.assertEquals(2, c.getDenominatorAsInt()); c = improperFormat.parse(source); Assert.assertNotNull(c); Assert.assertEquals(-1, c.getNumeratorAsInt()); Assert.assertEquals(2, c.getDenominatorAsInt()); } } @Test public void testParseProper() { String source = "1 2 / 3"; { BigFraction c = properFormat.parse(source); Assert.assertNotNull(c); Assert.assertEquals(5, c.getNumeratorAsInt()); Assert.assertEquals(3, c.getDenominatorAsInt()); } try { improperFormat.parse(source); Assert.fail("invalid improper fraction."); } catch (MathParseException ex) { // success } } @Test public void testParseProperNegative() { String source = "-1 2 / 3"; { BigFraction c = properFormat.parse(source); Assert.assertNotNull(c); Assert.assertEquals(-5, c.getNumeratorAsInt()); Assert.assertEquals(3, c.getDenominatorAsInt()); } try { improperFormat.parse(source); Assert.fail("invalid improper fraction."); } catch (MathParseException ex) { // success } } @Test public void testParseProperInvalidMinus() { String source = "2 -2 / 3"; try { properFormat.parse(source); Assert.fail("invalid minus in improper fraction."); } catch (MathParseException ex) { // expected } source = "2 2 / -3"; try { properFormat.parse(source); Assert.fail("invalid minus in improper fraction."); } catch (MathParseException ex) { // expected } } @Test public void testParseBig() { BigFraction f1 = improperFormat.parse("167213075789791382630275400487886041651764456874403" + " / " + "53225575123090058458126718248444563466137046489291"); Assert.assertEquals(FastMath.PI, f1.doubleValue(), 0.0); BigFraction f2 = properFormat.parse("3 " + "7536350420521207255895245742552351253353317406530" + " / " + "53225575123090058458126718248444563466137046489291"); Assert.assertEquals(FastMath.PI, f2.doubleValue(), 0.0); Assert.assertEquals(f1, f2); BigDecimal pi = new BigDecimal("3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117068"); Assert.assertEquals(pi, f1.bigDecimalValue(99, BigDecimal.ROUND_HALF_EVEN)); } @Test public void testNumeratorFormat() { NumberFormat old = properFormat.getNumeratorFormat(); NumberFormat nf = NumberFormat.getInstance(); nf.setParseIntegerOnly(true); properFormat.setNumeratorFormat(nf); Assert.assertEquals(nf, properFormat.getNumeratorFormat()); properFormat.setNumeratorFormat(old); old = improperFormat.getNumeratorFormat(); nf = NumberFormat.getInstance(); nf.setParseIntegerOnly(true); improperFormat.setNumeratorFormat(nf); Assert.assertEquals(nf, improperFormat.getNumeratorFormat()); improperFormat.setNumeratorFormat(old); } @Test public void testDenominatorFormat() { NumberFormat old = properFormat.getDenominatorFormat(); NumberFormat nf = NumberFormat.getInstance(); nf.setParseIntegerOnly(true); properFormat.setDenominatorFormat(nf); Assert.assertEquals(nf, properFormat.getDenominatorFormat()); properFormat.setDenominatorFormat(old); old = improperFormat.getDenominatorFormat(); nf = NumberFormat.getInstance(); nf.setParseIntegerOnly(true); improperFormat.setDenominatorFormat(nf); Assert.assertEquals(nf, improperFormat.getDenominatorFormat()); improperFormat.setDenominatorFormat(old); } @Test public void testWholeFormat() { ProperBigFractionFormat format = (ProperBigFractionFormat)properFormat; NumberFormat old = format.getWholeFormat(); NumberFormat nf = NumberFormat.getInstance(); nf.setParseIntegerOnly(true); format.setWholeFormat(nf); Assert.assertEquals(nf, format.getWholeFormat()); format.setWholeFormat(old); } @Test public void testLongFormat() { Assert.assertEquals("10 / 1", improperFormat.format(10l)); } @Test public void testDoubleFormat() { Assert.assertEquals("1 / 16", improperFormat.format(0.0625)); } } commons-math3-3.2-src/src/test/java/org/apache/commons/math3/fraction/BigFractionTest.java100644 1750 1750 64461 12126627672 30314 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.fraction; import java.math.BigDecimal; import java.math.BigInteger; import org.apache.commons.math3.TestUtils; import org.apache.commons.math3.exception.ConvergenceException; import org.apache.commons.math3.exception.MathArithmeticException; import org.apache.commons.math3.exception.NullArgumentException; import org.apache.commons.math3.exception.ZeroException; import org.apache.commons.math3.util.FastMath; import org.junit.Assert; import org.junit.Test; public class BigFractionTest { private void assertFraction(int expectedNumerator, int expectedDenominator, BigFraction actual) { Assert.assertEquals(expectedNumerator, actual.getNumeratorAsInt()); Assert.assertEquals(expectedDenominator, actual.getDenominatorAsInt()); } private void assertFraction(long expectedNumerator, long expectedDenominator, BigFraction actual) { Assert.assertEquals(expectedNumerator, actual.getNumeratorAsLong()); Assert.assertEquals(expectedDenominator, actual.getDenominatorAsLong()); } @Test public void testConstructor() { assertFraction(0, 1, new BigFraction(0, 1)); assertFraction(0, 1, new BigFraction(0l, 2l)); assertFraction(0, 1, new BigFraction(0, -1)); assertFraction(1, 2, new BigFraction(1, 2)); assertFraction(1, 2, new BigFraction(2, 4)); assertFraction(-1, 2, new BigFraction(-1, 2)); assertFraction(-1, 2, new BigFraction(1, -2)); assertFraction(-1, 2, new BigFraction(-2, 4)); assertFraction(-1, 2, new BigFraction(2, -4)); assertFraction(11, 1, new BigFraction(11)); assertFraction(11, 1, new BigFraction(11l)); assertFraction(11, 1, new BigFraction(new BigInteger("11"))); assertFraction(0, 1, new BigFraction(0.00000000000001, 1.0e-5, 100)); assertFraction(2, 5, new BigFraction(0.40000000000001, 1.0e-5, 100)); assertFraction(15, 1, new BigFraction(15.0000000000001, 1.0e-5, 100)); Assert.assertEquals(0.00000000000001, new BigFraction(0.00000000000001).doubleValue(), 0.0); Assert.assertEquals(0.40000000000001, new BigFraction(0.40000000000001).doubleValue(), 0.0); Assert.assertEquals(15.0000000000001, new BigFraction(15.0000000000001).doubleValue(), 0.0); assertFraction(3602879701896487l, 9007199254740992l, new BigFraction(0.40000000000001)); assertFraction(1055531162664967l, 70368744177664l, new BigFraction(15.0000000000001)); try { new BigFraction(null, BigInteger.ONE); Assert.fail("Expecting NullArgumentException"); } catch (NullArgumentException npe) { // expected } try { new BigFraction(BigInteger.ONE, null); Assert.fail("Expecting NullArgumentException"); } catch (NullArgumentException npe) { // expected } try { new BigFraction(BigInteger.ONE, BigInteger.ZERO); Assert.fail("Expecting ZeroException"); } catch (ZeroException npe) { // expected } try { new BigFraction(2.0 * Integer.MAX_VALUE, 1.0e-5, 100000); Assert.fail("Expecting FractionConversionException"); } catch (FractionConversionException fce) { // expected } } @Test(expected=ConvergenceException.class) public void testGoldenRatio() { // the golden ratio is notoriously a difficult number for continuous fraction new BigFraction((1 + FastMath.sqrt(5)) / 2, 1.0e-12, 25); } // MATH-179 @Test public void testDoubleConstructor() throws ConvergenceException { assertFraction(1, 2, new BigFraction((double) 1 / (double) 2, 1.0e-5, 100)); assertFraction(1, 3, new BigFraction((double) 1 / (double) 3, 1.0e-5, 100)); assertFraction(2, 3, new BigFraction((double) 2 / (double) 3, 1.0e-5, 100)); assertFraction(1, 4, new BigFraction((double) 1 / (double) 4, 1.0e-5, 100)); assertFraction(3, 4, new BigFraction((double) 3 / (double) 4, 1.0e-5, 100)); assertFraction(1, 5, new BigFraction((double) 1 / (double) 5, 1.0e-5, 100)); assertFraction(2, 5, new BigFraction((double) 2 / (double) 5, 1.0e-5, 100)); assertFraction(3, 5, new BigFraction((double) 3 / (double) 5, 1.0e-5, 100)); assertFraction(4, 5, new BigFraction((double) 4 / (double) 5, 1.0e-5, 100)); assertFraction(1, 6, new BigFraction((double) 1 / (double) 6, 1.0e-5, 100)); assertFraction(5, 6, new BigFraction((double) 5 / (double) 6, 1.0e-5, 100)); assertFraction(1, 7, new BigFraction((double) 1 / (double) 7, 1.0e-5, 100)); assertFraction(2, 7, new BigFraction((double) 2 / (double) 7, 1.0e-5, 100)); assertFraction(3, 7, new BigFraction((double) 3 / (double) 7, 1.0e-5, 100)); assertFraction(4, 7, new BigFraction((double) 4 / (double) 7, 1.0e-5, 100)); assertFraction(5, 7, new BigFraction((double) 5 / (double) 7, 1.0e-5, 100)); assertFraction(6, 7, new BigFraction((double) 6 / (double) 7, 1.0e-5, 100)); assertFraction(1, 8, new BigFraction((double) 1 / (double) 8, 1.0e-5, 100)); assertFraction(3, 8, new BigFraction((double) 3 / (double) 8, 1.0e-5, 100)); assertFraction(5, 8, new BigFraction((double) 5 / (double) 8, 1.0e-5, 100)); assertFraction(7, 8, new BigFraction((double) 7 / (double) 8, 1.0e-5, 100)); assertFraction(1, 9, new BigFraction((double) 1 / (double) 9, 1.0e-5, 100)); assertFraction(2, 9, new BigFraction((double) 2 / (double) 9, 1.0e-5, 100)); assertFraction(4, 9, new BigFraction((double) 4 / (double) 9, 1.0e-5, 100)); assertFraction(5, 9, new BigFraction((double) 5 / (double) 9, 1.0e-5, 100)); assertFraction(7, 9, new BigFraction((double) 7 / (double) 9, 1.0e-5, 100)); assertFraction(8, 9, new BigFraction((double) 8 / (double) 9, 1.0e-5, 100)); assertFraction(1, 10, new BigFraction((double) 1 / (double) 10, 1.0e-5, 100)); assertFraction(3, 10, new BigFraction((double) 3 / (double) 10, 1.0e-5, 100)); assertFraction(7, 10, new BigFraction((double) 7 / (double) 10, 1.0e-5, 100)); assertFraction(9, 10, new BigFraction((double) 9 / (double) 10, 1.0e-5, 100)); assertFraction(1, 11, new BigFraction((double) 1 / (double) 11, 1.0e-5, 100)); assertFraction(2, 11, new BigFraction((double) 2 / (double) 11, 1.0e-5, 100)); assertFraction(3, 11, new BigFraction((double) 3 / (double) 11, 1.0e-5, 100)); assertFraction(4, 11, new BigFraction((double) 4 / (double) 11, 1.0e-5, 100)); assertFraction(5, 11, new BigFraction((double) 5 / (double) 11, 1.0e-5, 100)); assertFraction(6, 11, new BigFraction((double) 6 / (double) 11, 1.0e-5, 100)); assertFraction(7, 11, new BigFraction((double) 7 / (double) 11, 1.0e-5, 100)); assertFraction(8, 11, new BigFraction((double) 8 / (double) 11, 1.0e-5, 100)); assertFraction(9, 11, new BigFraction((double) 9 / (double) 11, 1.0e-5, 100)); assertFraction(10, 11, new BigFraction((double) 10 / (double) 11, 1.0e-5, 100)); } // MATH-181 @Test public void testDigitLimitConstructor() throws ConvergenceException { assertFraction(2, 5, new BigFraction(0.4, 9)); assertFraction(2, 5, new BigFraction(0.4, 99)); assertFraction(2, 5, new BigFraction(0.4, 999)); assertFraction(3, 5, new BigFraction(0.6152, 9)); assertFraction(8, 13, new BigFraction(0.6152, 99)); assertFraction(510, 829, new BigFraction(0.6152, 999)); assertFraction(769, 1250, new BigFraction(0.6152, 9999)); } @Test public void testEpsilonLimitConstructor() throws ConvergenceException { assertFraction(2, 5, new BigFraction(0.4, 1.0e-5, 100)); assertFraction(3, 5, new BigFraction(0.6152, 0.02, 100)); assertFraction(8, 13, new BigFraction(0.6152, 1.0e-3, 100)); assertFraction(251, 408, new BigFraction(0.6152, 1.0e-4, 100)); assertFraction(251, 408, new BigFraction(0.6152, 1.0e-5, 100)); assertFraction(510, 829, new BigFraction(0.6152, 1.0e-6, 100)); assertFraction(769, 1250, new BigFraction(0.6152, 1.0e-7, 100)); } @Test public void testCompareTo() { BigFraction first = new BigFraction(1, 2); BigFraction second = new BigFraction(1, 3); BigFraction third = new BigFraction(1, 2); Assert.assertEquals(0, first.compareTo(first)); Assert.assertEquals(0, first.compareTo(third)); Assert.assertEquals(1, first.compareTo(second)); Assert.assertEquals(-1, second.compareTo(first)); // these two values are different approximations of PI // the first one is approximately PI - 3.07e-18 // the second one is approximately PI + 1.936e-17 BigFraction pi1 = new BigFraction(1068966896, 340262731); BigFraction pi2 = new BigFraction( 411557987, 131002976); Assert.assertEquals(-1, pi1.compareTo(pi2)); Assert.assertEquals( 1, pi2.compareTo(pi1)); Assert.assertEquals(0.0, pi1.doubleValue() - pi2.doubleValue(), 1.0e-20); } @Test public void testDoubleValue() { BigFraction first = new BigFraction(1, 2); BigFraction second = new BigFraction(1, 3); Assert.assertEquals(0.5, first.doubleValue(), 0.0); Assert.assertEquals(1.0 / 3.0, second.doubleValue(), 0.0); } // MATH-744 @Test public void testDoubleValueForLargeNumeratorAndDenominator() { final BigInteger pow400 = BigInteger.TEN.pow(400); final BigInteger pow401 = BigInteger.TEN.pow(401); final BigInteger two = new BigInteger("2"); final BigFraction large = new BigFraction(pow401.add(BigInteger.ONE), pow400.multiply(two)); Assert.assertEquals(5, large.doubleValue(), 1e-15); } // MATH-744 @Test public void testFloatValueForLargeNumeratorAndDenominator() { final BigInteger pow400 = BigInteger.TEN.pow(400); final BigInteger pow401 = BigInteger.TEN.pow(401); final BigInteger two = new BigInteger("2"); final BigFraction large = new BigFraction(pow401.add(BigInteger.ONE), pow400.multiply(two)); Assert.assertEquals(5, large.floatValue(), 1e-15); } @Test public void testFloatValue() { BigFraction first = new BigFraction(1, 2); BigFraction second = new BigFraction(1, 3); Assert.assertEquals(0.5f, first.floatValue(), 0.0f); Assert.assertEquals((float) (1.0 / 3.0), second.floatValue(), 0.0f); } @Test public void testIntValue() { BigFraction first = new BigFraction(1, 2); BigFraction second = new BigFraction(3, 2); Assert.assertEquals(0, first.intValue()); Assert.assertEquals(1, second.intValue()); } @Test public void testLongValue() { BigFraction first = new BigFraction(1, 2); BigFraction second = new BigFraction(3, 2); Assert.assertEquals(0L, first.longValue()); Assert.assertEquals(1L, second.longValue()); } @Test public void testConstructorDouble() { assertFraction(1, 2, new BigFraction(0.5)); assertFraction(6004799503160661l, 18014398509481984l, new BigFraction(1.0 / 3.0)); assertFraction(6124895493223875l, 36028797018963968l, new BigFraction(17.0 / 100.0)); assertFraction(1784551352345559l, 562949953421312l, new BigFraction(317.0 / 100.0)); assertFraction(-1, 2, new BigFraction(-0.5)); assertFraction(-6004799503160661l, 18014398509481984l, new BigFraction(-1.0 / 3.0)); assertFraction(-6124895493223875l, 36028797018963968l, new BigFraction(17.0 / -100.0)); assertFraction(-1784551352345559l, 562949953421312l, new BigFraction(-317.0 / 100.0)); for (double v : new double[] { Double.NaN, Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY}) { try { new BigFraction(v); Assert.fail("Expecting IllegalArgumentException"); } catch (IllegalArgumentException iae) { // expected } } Assert.assertEquals(1l, new BigFraction(Double.MAX_VALUE).getDenominatorAsLong()); Assert.assertEquals(1l, new BigFraction(Double.longBitsToDouble(0x0010000000000000L)).getNumeratorAsLong()); Assert.assertEquals(1l, new BigFraction(Double.MIN_VALUE).getNumeratorAsLong()); } @Test public void testAbs() { BigFraction a = new BigFraction(10, 21); BigFraction b = new BigFraction(-10, 21); BigFraction c = new BigFraction(10, -21); assertFraction(10, 21, a.abs()); assertFraction(10, 21, b.abs()); assertFraction(10, 21, c.abs()); } @Test public void testReciprocal() { BigFraction f = null; f = new BigFraction(50, 75); f = f.reciprocal(); Assert.assertEquals(3, f.getNumeratorAsInt()); Assert.assertEquals(2, f.getDenominatorAsInt()); f = new BigFraction(4, 3); f = f.reciprocal(); Assert.assertEquals(3, f.getNumeratorAsInt()); Assert.assertEquals(4, f.getDenominatorAsInt()); f = new BigFraction(-15, 47); f = f.reciprocal(); Assert.assertEquals(-47, f.getNumeratorAsInt()); Assert.assertEquals(15, f.getDenominatorAsInt()); f = new BigFraction(0, 3); try { f = f.reciprocal(); Assert.fail("expecting ZeroException"); } catch (ZeroException ex) { } // large values f = new BigFraction(Integer.MAX_VALUE, 1); f = f.reciprocal(); Assert.assertEquals(1, f.getNumeratorAsInt()); Assert.assertEquals(Integer.MAX_VALUE, f.getDenominatorAsInt()); } @Test public void testNegate() { BigFraction f = null; f = new BigFraction(50, 75); f = f.negate(); Assert.assertEquals(-2, f.getNumeratorAsInt()); Assert.assertEquals(3, f.getDenominatorAsInt()); f = new BigFraction(-50, 75); f = f.negate(); Assert.assertEquals(2, f.getNumeratorAsInt()); Assert.assertEquals(3, f.getDenominatorAsInt()); // large values f = new BigFraction(Integer.MAX_VALUE - 1, Integer.MAX_VALUE); f = f.negate(); Assert.assertEquals(Integer.MIN_VALUE + 2, f.getNumeratorAsInt()); Assert.assertEquals(Integer.MAX_VALUE, f.getDenominatorAsInt()); } @Test public void testAdd() { BigFraction a = new BigFraction(1, 2); BigFraction b = new BigFraction(2, 3); assertFraction(1, 1, a.add(a)); assertFraction(7, 6, a.add(b)); assertFraction(7, 6, b.add(a)); assertFraction(4, 3, b.add(b)); BigFraction f1 = new BigFraction(Integer.MAX_VALUE - 1, 1); BigFraction f2 = BigFraction.ONE; BigFraction f = f1.add(f2); Assert.assertEquals(Integer.MAX_VALUE, f.getNumeratorAsInt()); Assert.assertEquals(1, f.getDenominatorAsInt()); f1 = new BigFraction(-1, 13 * 13 * 2 * 2); f2 = new BigFraction(-2, 13 * 17 * 2); f = f1.add(f2); Assert.assertEquals(13 * 13 * 17 * 2 * 2, f.getDenominatorAsInt()); Assert.assertEquals(-17 - 2 * 13 * 2, f.getNumeratorAsInt()); try { f.add((BigFraction) null); Assert.fail("expecting NullArgumentException"); } catch (NullArgumentException ex) { } // if this fraction is added naively, it will overflow. // check that it doesn't. f1 = new BigFraction(1, 32768 * 3); f2 = new BigFraction(1, 59049); f = f1.add(f2); Assert.assertEquals(52451, f.getNumeratorAsInt()); Assert.assertEquals(1934917632, f.getDenominatorAsInt()); f1 = new BigFraction(Integer.MIN_VALUE, 3); f2 = new BigFraction(1, 3); f = f1.add(f2); Assert.assertEquals(Integer.MIN_VALUE + 1, f.getNumeratorAsInt()); Assert.assertEquals(3, f.getDenominatorAsInt()); f1 = new BigFraction(Integer.MAX_VALUE - 1, 1); f = f1.add(BigInteger.ONE); Assert.assertEquals(Integer.MAX_VALUE, f.getNumeratorAsInt()); Assert.assertEquals(1, f.getDenominatorAsInt()); f = f.add(BigInteger.ZERO); Assert.assertEquals(Integer.MAX_VALUE, f.getNumeratorAsInt()); Assert.assertEquals(1, f.getDenominatorAsInt()); f1 = new BigFraction(Integer.MAX_VALUE - 1, 1); f = f1.add(1); Assert.assertEquals(Integer.MAX_VALUE, f.getNumeratorAsInt()); Assert.assertEquals(1, f.getDenominatorAsInt()); f = f.add(0); Assert.assertEquals(Integer.MAX_VALUE, f.getNumeratorAsInt()); Assert.assertEquals(1, f.getDenominatorAsInt()); f1 = new BigFraction(Integer.MAX_VALUE - 1, 1); f = f1.add(1l); Assert.assertEquals(Integer.MAX_VALUE, f.getNumeratorAsInt()); Assert.assertEquals(1, f.getDenominatorAsInt()); f = f.add(0l); Assert.assertEquals(Integer.MAX_VALUE, f.getNumeratorAsInt()); Assert.assertEquals(1, f.getDenominatorAsInt()); } @Test public void testDivide() { BigFraction a = new BigFraction(1, 2); BigFraction b = new BigFraction(2, 3); assertFraction(1, 1, a.divide(a)); assertFraction(3, 4, a.divide(b)); assertFraction(4, 3, b.divide(a)); assertFraction(1, 1, b.divide(b)); BigFraction f1 = new BigFraction(3, 5); BigFraction f2 = BigFraction.ZERO; try { f1.divide(f2); Assert.fail("expecting MathArithmeticException"); } catch (MathArithmeticException ex) { } f1 = new BigFraction(0, 5); f2 = new BigFraction(2, 7); BigFraction f = f1.divide(f2); Assert.assertSame(BigFraction.ZERO, f); f1 = new BigFraction(2, 7); f2 = BigFraction.ONE; f = f1.divide(f2); Assert.assertEquals(2, f.getNumeratorAsInt()); Assert.assertEquals(7, f.getDenominatorAsInt()); f1 = new BigFraction(1, Integer.MAX_VALUE); f = f1.divide(f1); Assert.assertEquals(1, f.getNumeratorAsInt()); Assert.assertEquals(1, f.getDenominatorAsInt()); f1 = new BigFraction(Integer.MIN_VALUE, Integer.MAX_VALUE); f2 = new BigFraction(1, Integer.MAX_VALUE); f = f1.divide(f2); Assert.assertEquals(Integer.MIN_VALUE, f.getNumeratorAsInt()); Assert.assertEquals(1, f.getDenominatorAsInt()); try { f.divide((BigFraction) null); Assert.fail("expecting NullArgumentException"); } catch (NullArgumentException ex) { } f1 = new BigFraction(Integer.MIN_VALUE, Integer.MAX_VALUE); f = f1.divide(BigInteger.valueOf(Integer.MIN_VALUE)); Assert.assertEquals(Integer.MAX_VALUE, f.getDenominatorAsInt()); Assert.assertEquals(1, f.getNumeratorAsInt()); f1 = new BigFraction(Integer.MIN_VALUE, Integer.MAX_VALUE); f = f1.divide(Integer.MIN_VALUE); Assert.assertEquals(Integer.MAX_VALUE, f.getDenominatorAsInt()); Assert.assertEquals(1, f.getNumeratorAsInt()); f1 = new BigFraction(Integer.MIN_VALUE, Integer.MAX_VALUE); f = f1.divide((long) Integer.MIN_VALUE); Assert.assertEquals(Integer.MAX_VALUE, f.getDenominatorAsInt()); Assert.assertEquals(1, f.getNumeratorAsInt()); } @Test public void testMultiply() { BigFraction a = new BigFraction(1, 2); BigFraction b = new BigFraction(2, 3); assertFraction(1, 4, a.multiply(a)); assertFraction(1, 3, a.multiply(b)); assertFraction(1, 3, b.multiply(a)); assertFraction(4, 9, b.multiply(b)); BigFraction f1 = new BigFraction(Integer.MAX_VALUE, 1); BigFraction f2 = new BigFraction(Integer.MIN_VALUE, Integer.MAX_VALUE); BigFraction f = f1.multiply(f2); Assert.assertEquals(Integer.MIN_VALUE, f.getNumeratorAsInt()); Assert.assertEquals(1, f.getDenominatorAsInt()); f = f2.multiply(Integer.MAX_VALUE); Assert.assertEquals(Integer.MIN_VALUE, f.getNumeratorAsInt()); Assert.assertEquals(1, f.getDenominatorAsInt()); f = f2.multiply((long) Integer.MAX_VALUE); Assert.assertEquals(Integer.MIN_VALUE, f.getNumeratorAsInt()); Assert.assertEquals(1, f.getDenominatorAsInt()); try { f.multiply((BigFraction) null); Assert.fail("expecting NullArgumentException"); } catch (NullArgumentException ex) { } } @Test public void testSubtract() { BigFraction a = new BigFraction(1, 2); BigFraction b = new BigFraction(2, 3); assertFraction(0, 1, a.subtract(a)); assertFraction(-1, 6, a.subtract(b)); assertFraction(1, 6, b.subtract(a)); assertFraction(0, 1, b.subtract(b)); BigFraction f = new BigFraction(1, 1); try { f.subtract((BigFraction) null); Assert.fail("expecting NullArgumentException"); } catch (NullArgumentException ex) { } // if this fraction is subtracted naively, it will overflow. // check that it doesn't. BigFraction f1 = new BigFraction(1, 32768 * 3); BigFraction f2 = new BigFraction(1, 59049); f = f1.subtract(f2); Assert.assertEquals(-13085, f.getNumeratorAsInt()); Assert.assertEquals(1934917632, f.getDenominatorAsInt()); f1 = new BigFraction(Integer.MIN_VALUE, 3); f2 = new BigFraction(1, 3).negate(); f = f1.subtract(f2); Assert.assertEquals(Integer.MIN_VALUE + 1, f.getNumeratorAsInt()); Assert.assertEquals(3, f.getDenominatorAsInt()); f1 = new BigFraction(Integer.MAX_VALUE, 1); f2 = BigFraction.ONE; f = f1.subtract(f2); Assert.assertEquals(Integer.MAX_VALUE - 1, f.getNumeratorAsInt()); Assert.assertEquals(1, f.getDenominatorAsInt()); } @Test public void testBigDecimalValue() { Assert.assertEquals(new BigDecimal(0.5), new BigFraction(1, 2).bigDecimalValue()); Assert.assertEquals(new BigDecimal("0.0003"), new BigFraction(3, 10000).bigDecimalValue()); Assert.assertEquals(new BigDecimal("0"), new BigFraction(1, 3).bigDecimalValue(BigDecimal.ROUND_DOWN)); Assert.assertEquals(new BigDecimal("0.333"), new BigFraction(1, 3).bigDecimalValue(3, BigDecimal.ROUND_DOWN)); } @Test public void testEqualsAndHashCode() { BigFraction zero = new BigFraction(0, 1); BigFraction nullFraction = null; Assert.assertTrue(zero.equals(zero)); Assert.assertFalse(zero.equals(nullFraction)); Assert.assertFalse(zero.equals(Double.valueOf(0))); BigFraction zero2 = new BigFraction(0, 2); Assert.assertTrue(zero.equals(zero2)); Assert.assertEquals(zero.hashCode(), zero2.hashCode()); BigFraction one = new BigFraction(1, 1); Assert.assertFalse((one.equals(zero) || zero.equals(one))); Assert.assertTrue(one.equals(BigFraction.ONE)); } @Test public void testGetReducedFraction() { BigFraction threeFourths = new BigFraction(3, 4); Assert.assertTrue(threeFourths.equals(BigFraction.getReducedFraction(6, 8))); Assert.assertTrue(BigFraction.ZERO.equals(BigFraction.getReducedFraction(0, -1))); try { BigFraction.getReducedFraction(1, 0); Assert.fail("expecting ZeroException"); } catch (ZeroException ex) { // expected } Assert.assertEquals(BigFraction.getReducedFraction(2, Integer.MIN_VALUE).getNumeratorAsInt(), -1); Assert.assertEquals(BigFraction.getReducedFraction(1, -1).getNumeratorAsInt(), -1); } @Test public void testPercentage() { Assert.assertEquals(50.0, new BigFraction(1, 2).percentageValue(), 1.0e-15); } @Test public void testPow() { Assert.assertEquals(new BigFraction(8192, 1594323), new BigFraction(2, 3).pow(13)); Assert.assertEquals(new BigFraction(8192, 1594323), new BigFraction(2, 3).pow(13l)); Assert.assertEquals(new BigFraction(8192, 1594323), new BigFraction(2, 3).pow(BigInteger.valueOf(13l))); Assert.assertEquals(BigFraction.ONE, new BigFraction(2, 3).pow(0)); Assert.assertEquals(BigFraction.ONE, new BigFraction(2, 3).pow(0l)); Assert.assertEquals(BigFraction.ONE, new BigFraction(2, 3).pow(BigInteger.valueOf(0l))); Assert.assertEquals(new BigFraction(1594323, 8192), new BigFraction(2, 3).pow(-13)); Assert.assertEquals(new BigFraction(1594323, 8192), new BigFraction(2, 3).pow(-13l)); Assert.assertEquals(new BigFraction(1594323, 8192), new BigFraction(2, 3).pow(BigInteger.valueOf(-13l))); } @Test public void testMath340() { BigFraction fractionA = new BigFraction(0.00131); BigFraction fractionB = new BigFraction(.37).reciprocal(); BigFraction errorResult = fractionA.multiply(fractionB); BigFraction correctResult = new BigFraction(fractionA.getNumerator().multiply(fractionB.getNumerator()), fractionA.getDenominator().multiply(fractionB.getDenominator())); Assert.assertEquals(correctResult, errorResult); } @Test public void testSerial() throws FractionConversionException { BigFraction[] fractions = { new BigFraction(3, 4), BigFraction.ONE, BigFraction.ZERO, new BigFraction(17), new BigFraction(FastMath.PI, 1000), new BigFraction(-5, 2) }; for (BigFraction fraction : fractions) { Assert.assertEquals(fraction, TestUtils.serializeAndRecover(fraction)); } } } commons-math3-3.2-src/src/test/java/org/apache/commons/math3/fraction/FractionFormatTest.java100644 1750 1750 24744 12126627672 31043 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.fraction; import java.text.NumberFormat; import java.util.Locale; import org.apache.commons.math3.exception.MathParseException; import org.apache.commons.math3.util.FastMath; import org.junit.Assert; import org.junit.Before; import org.junit.Test; public class FractionFormatTest { FractionFormat properFormat = null; FractionFormat improperFormat = null; protected Locale getLocale() { return Locale.getDefault(); } @Before public void setUp() { properFormat = FractionFormat.getProperInstance(getLocale()); improperFormat = FractionFormat.getImproperInstance(getLocale()); } @Test public void testFormat() { Fraction c = new Fraction(1, 2); String expected = "1 / 2"; String actual = properFormat.format(c); Assert.assertEquals(expected, actual); actual = improperFormat.format(c); Assert.assertEquals(expected, actual); } @Test public void testFormatNegative() { Fraction c = new Fraction(-1, 2); String expected = "-1 / 2"; String actual = properFormat.format(c); Assert.assertEquals(expected, actual); actual = improperFormat.format(c); Assert.assertEquals(expected, actual); } @Test public void testFormatZero() { Fraction c = new Fraction(0, 1); String expected = "0 / 1"; String actual = properFormat.format(c); Assert.assertEquals(expected, actual); actual = improperFormat.format(c); Assert.assertEquals(expected, actual); } @Test public void testFormatImproper() { Fraction c = new Fraction(5, 3); String actual = properFormat.format(c); Assert.assertEquals("1 2 / 3", actual); actual = improperFormat.format(c); Assert.assertEquals("5 / 3", actual); } @Test public void testFormatImproperNegative() { Fraction c = new Fraction(-5, 3); String actual = properFormat.format(c); Assert.assertEquals("-1 2 / 3", actual); actual = improperFormat.format(c); Assert.assertEquals("-5 / 3", actual); } @Test public void testParse() { String source = "1 / 2"; try { Fraction c = properFormat.parse(source); Assert.assertNotNull(c); Assert.assertEquals(1, c.getNumerator()); Assert.assertEquals(2, c.getDenominator()); c = improperFormat.parse(source); Assert.assertNotNull(c); Assert.assertEquals(1, c.getNumerator()); Assert.assertEquals(2, c.getDenominator()); } catch (MathParseException ex) { Assert.fail(ex.getMessage()); } } @Test public void testParseInteger() { String source = "10"; { Fraction c = properFormat.parse(source); Assert.assertNotNull(c); Assert.assertEquals(10, c.getNumerator()); Assert.assertEquals(1, c.getDenominator()); } { Fraction c = improperFormat.parse(source); Assert.assertNotNull(c); Assert.assertEquals(10, c.getNumerator()); Assert.assertEquals(1, c.getDenominator()); } } @Test public void testParseOne1() { String source = "1 / 1"; Fraction c = properFormat.parse(source); Assert.assertNotNull(c); Assert.assertEquals(1, c.getNumerator()); Assert.assertEquals(1, c.getDenominator()); } @Test public void testParseOne2() { String source = "10 / 10"; Fraction c = properFormat.parse(source); Assert.assertNotNull(c); Assert.assertEquals(1, c.getNumerator()); Assert.assertEquals(1, c.getDenominator()); } @Test public void testParseZero1() { String source = "0 / 1"; Fraction c = properFormat.parse(source); Assert.assertNotNull(c); Assert.assertEquals(0, c.getNumerator()); Assert.assertEquals(1, c.getDenominator()); } @Test public void testParseZero2() { String source = "-0 / 1"; Fraction c = properFormat.parse(source); Assert.assertNotNull(c); Assert.assertEquals(0, c.getNumerator()); Assert.assertEquals(1, c.getDenominator()); // This test shows that the sign is not preserved. Assert.assertEquals(Double.POSITIVE_INFINITY, 1d / c.doubleValue(), 0); } @Test public void testParseInvalid() { String source = "a"; String msg = "should not be able to parse '10 / a'."; try { properFormat.parse(source); Assert.fail(msg); } catch (MathParseException ex) { // success } try { improperFormat.parse(source); Assert.fail(msg); } catch (MathParseException ex) { // success } } @Test public void testParseInvalidDenominator() { String source = "10 / a"; String msg = "should not be able to parse '10 / a'."; try { properFormat.parse(source); Assert.fail(msg); } catch (MathParseException ex) { // success } try { improperFormat.parse(source); Assert.fail(msg); } catch (MathParseException ex) { // success } } @Test public void testParseNegative() { { String source = "-1 / 2"; Fraction c = properFormat.parse(source); Assert.assertNotNull(c); Assert.assertEquals(-1, c.getNumerator()); Assert.assertEquals(2, c.getDenominator()); c = improperFormat.parse(source); Assert.assertNotNull(c); Assert.assertEquals(-1, c.getNumerator()); Assert.assertEquals(2, c.getDenominator()); source = "1 / -2"; c = properFormat.parse(source); Assert.assertNotNull(c); Assert.assertEquals(-1, c.getNumerator()); Assert.assertEquals(2, c.getDenominator()); c = improperFormat.parse(source); Assert.assertNotNull(c); Assert.assertEquals(-1, c.getNumerator()); Assert.assertEquals(2, c.getDenominator()); } } @Test public void testParseProper() { String source = "1 2 / 3"; { Fraction c = properFormat.parse(source); Assert.assertNotNull(c); Assert.assertEquals(5, c.getNumerator()); Assert.assertEquals(3, c.getDenominator()); } try { improperFormat.parse(source); Assert.fail("invalid improper fraction."); } catch (MathParseException ex) { // success } } @Test public void testParseProperNegative() { String source = "-1 2 / 3"; { Fraction c = properFormat.parse(source); Assert.assertNotNull(c); Assert.assertEquals(-5, c.getNumerator()); Assert.assertEquals(3, c.getDenominator()); } try { improperFormat.parse(source); Assert.fail("invalid improper fraction."); } catch (MathParseException ex) { // success } } @Test public void testParseProperInvalidMinus() { String source = "2 -2 / 3"; try { properFormat.parse(source); Assert.fail("invalid minus in improper fraction."); } catch (MathParseException ex) { // expected } source = "2 2 / -3"; try { properFormat.parse(source); Assert.fail("invalid minus in improper fraction."); } catch (MathParseException ex) { // expected } } @Test public void testNumeratorFormat() { NumberFormat old = properFormat.getNumeratorFormat(); NumberFormat nf = NumberFormat.getInstance(); nf.setParseIntegerOnly(true); properFormat.setNumeratorFormat(nf); Assert.assertEquals(nf, properFormat.getNumeratorFormat()); properFormat.setNumeratorFormat(old); old = improperFormat.getNumeratorFormat(); nf = NumberFormat.getInstance(); nf.setParseIntegerOnly(true); improperFormat.setNumeratorFormat(nf); Assert.assertEquals(nf, improperFormat.getNumeratorFormat()); improperFormat.setNumeratorFormat(old); } @Test public void testDenominatorFormat() { NumberFormat old = properFormat.getDenominatorFormat(); NumberFormat nf = NumberFormat.getInstance(); nf.setParseIntegerOnly(true); properFormat.setDenominatorFormat(nf); Assert.assertEquals(nf, properFormat.getDenominatorFormat()); properFormat.setDenominatorFormat(old); old = improperFormat.getDenominatorFormat(); nf = NumberFormat.getInstance(); nf.setParseIntegerOnly(true); improperFormat.setDenominatorFormat(nf); Assert.assertEquals(nf, improperFormat.getDenominatorFormat()); improperFormat.setDenominatorFormat(old); } @Test public void testWholeFormat() { ProperFractionFormat format = (ProperFractionFormat)properFormat; NumberFormat old = format.getWholeFormat(); NumberFormat nf = NumberFormat.getInstance(); nf.setParseIntegerOnly(true); format.setWholeFormat(nf); Assert.assertEquals(nf, format.getWholeFormat()); format.setWholeFormat(old); } @Test public void testLongFormat() { Assert.assertEquals("10 / 1", improperFormat.format(10l)); } @Test public void testDoubleFormat() { Assert.assertEquals("355 / 113", improperFormat.format(FastMath.PI)); } } commons-math3-3.2-src/src/test/java/org/apache/commons/math3/RetryRunnerTest.java100644 1750 1750 3422 12126627675 26570 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3; import java.util.Random; import org.apache.commons.math3.exception.MathIllegalStateException; import org.junit.Test; import org.junit.runner.RunWith; /** * Test for the "Retry" functionality (retrying Junit test methods). */ @RunWith(RetryRunner.class) public class RetryRunnerTest { final Random rng = new Random(); /** * Shows that an always failing test will fail even if it is retried. */ @Test(expected=MathIllegalStateException.class) @Retry public void testRetryFailAlways() { throw new MathIllegalStateException(); } /** * Shows that a test that sometimes fail might succeed if it is retried. * In this case the high number of retries makes it quite unlikely that * the exception will be thrown by all of the calls. */ @Test @Retry(100) public void testRetryFailSometimes() { if (rng.nextBoolean()) { throw new MathIllegalStateException(); } } } commons-math3-3.2-src/src/test/java/org/apache/commons/math3/ExtendedFieldElementAbstractTest.java100644 1750 1750 37356 12126627675 32030 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3; import org.apache.commons.math3.random.RandomGenerator; import org.apache.commons.math3.random.Well1024a; import org.apache.commons.math3.util.FastMath; import org.apache.commons.math3.util.MathArrays; import org.junit.Assert; import org.junit.Test; public abstract class ExtendedFieldElementAbstractTest> { protected abstract T build(double x); @Test public void testAddField() { for (double x = -3; x < 3; x += 0.2) { for (double y = -3; y < 3; y += 0.2) { checkRelative(x + y, build(x).add(build(y))); } } } @Test public void testAddDouble() { for (double x = -3; x < 3; x += 0.2) { for (double y = -3; y < 3; y += 0.2) { checkRelative(x + y, build(x).add(y)); } } } @Test public void testSubtractField() { for (double x = -3; x < 3; x += 0.2) { for (double y = -3; y < 3; y += 0.2) { checkRelative(x - y, build(x).subtract(build(y))); } } } @Test public void testSubtractDouble() { for (double x = -3; x < 3; x += 0.2) { for (double y = -3; y < 3; y += 0.2) { checkRelative(x - y, build(x).subtract(y)); } } } @Test public void testMultiplyField() { for (double x = -3; x < 3; x += 0.2) { for (double y = -3; y < 3; y += 0.2) { checkRelative(x * y, build(x).multiply(build(y))); } } } @Test public void testMultiplyDouble() { for (double x = -3; x < 3; x += 0.2) { for (double y = -3; y < 3; y += 0.2) { checkRelative(x * y, build(x).multiply(y)); } } } @Test public void testMultiplyInt() { for (double x = -3; x < 3; x += 0.2) { for (int y = -10; y < 10; y += 1) { checkRelative(x * y, build(x).multiply(y)); } } } @Test public void testDivideField() { for (double x = -3; x < 3; x += 0.2) { for (double y = -3; y < 3; y += 0.2) { checkRelative(x / y, build(x).divide(build(y))); } } } @Test public void testDivideDouble() { for (double x = -3; x < 3; x += 0.2) { for (double y = -3; y < 3; y += 0.2) { checkRelative(x / y, build(x).divide(y)); } } } @Test public void testRemainderField() { for (double x = -3; x < 3; x += 0.2) { for (double y = -3; y < 3; y += 0.2) { checkRelative(FastMath.IEEEremainder(x, y), build(x).remainder(build(y))); } } } @Test public void testRemainderDouble() { for (double x = -3; x < 3; x += 0.2) { for (double y = -3.2; y < 3.2; y += 0.25) { checkRelative(FastMath.IEEEremainder(x, y), build(x).remainder(y)); } } } @Test public void testCos() { for (double x = -0.9; x < 0.9; x += 0.05) { checkRelative(FastMath.cos(x), build(x).cos()); } } @Test public void testAcos() { for (double x = -0.9; x < 0.9; x += 0.05) { checkRelative(FastMath.acos(x), build(x).acos()); } } @Test public void testSin() { for (double x = -0.9; x < 0.9; x += 0.05) { checkRelative(FastMath.sin(x), build(x).sin()); } } @Test public void testAsin() { for (double x = -0.9; x < 0.9; x += 0.05) { checkRelative(FastMath.asin(x), build(x).asin()); } } @Test public void testTan() { for (double x = -0.9; x < 0.9; x += 0.05) { checkRelative(FastMath.tan(x), build(x).tan()); } } @Test public void testAtan() { for (double x = -0.9; x < 0.9; x += 0.05) { checkRelative(FastMath.atan(x), build(x).atan()); } } @Test public void testAtan2() { for (double x = -3; x < 3; x += 0.2) { for (double y = -3; y < 3; y += 0.2) { checkRelative(FastMath.atan2(x, y), build(x).atan2(build(y))); } } } @Test public void testCosh() { for (double x = -0.9; x < 0.9; x += 0.05) { checkRelative(FastMath.cosh(x), build(x).cosh()); } } @Test public void testAcosh() { for (double x = 1.1; x < 5.0; x += 0.05) { checkRelative(FastMath.acosh(x), build(x).acosh()); } } @Test public void testSinh() { for (double x = -0.9; x < 0.9; x += 0.05) { checkRelative(FastMath.sinh(x), build(x).sinh()); } } @Test public void testAsinh() { for (double x = -0.9; x < 0.9; x += 0.05) { checkRelative(FastMath.asinh(x), build(x).asinh()); } } @Test public void testTanh() { for (double x = -0.9; x < 0.9; x += 0.05) { checkRelative(FastMath.tanh(x), build(x).tanh()); } } @Test public void testAtanh() { for (double x = -0.9; x < 0.9; x += 0.05) { checkRelative(FastMath.atanh(x), build(x).atanh()); } } @Test public void testSqrt() { for (double x = 0.01; x < 0.9; x += 0.05) { checkRelative(FastMath.sqrt(x), build(x).sqrt()); } } @Test public void testCbrt() { for (double x = -0.9; x < 0.9; x += 0.05) { checkRelative(FastMath.cbrt(x), build(x).cbrt()); } } @Test public void testHypot() { for (double x = -3; x < 3; x += 0.2) { for (double y = -3; y < 3; y += 0.2) { checkRelative(FastMath.hypot(x, y), build(x).hypot(build(y))); } } } @Test public void testRootN() { for (double x = -0.9; x < 0.9; x += 0.05) { for (int n = 1; n < 5; ++n) { if (x < 0) { if (n % 2 == 1) { checkRelative(-FastMath.pow(-x, 1.0 / n), build(x).rootN(n)); } } else { checkRelative(FastMath.pow(x, 1.0 / n), build(x).rootN(n)); } } } } @Test public void testPowField() { for (double x = -0.9; x < 0.9; x += 0.05) { for (double y = 0.1; y < 4; y += 0.2) { checkRelative(FastMath.pow(x, y), build(x).pow(build(y))); } } } @Test public void testPowDouble() { for (double x = -0.9; x < 0.9; x += 0.05) { for (double y = 0.1; y < 4; y += 0.2) { checkRelative(FastMath.pow(x, y), build(x).pow(y)); } } } @Test public void testPowInt() { for (double x = -0.9; x < 0.9; x += 0.05) { for (int n = 0; n < 5; ++n) { checkRelative(FastMath.pow(x, n), build(x).pow(n)); } } } @Test public void testExp() { for (double x = -0.9; x < 0.9; x += 0.05) { checkRelative(FastMath.exp(x), build(x).exp()); } } @Test public void testExpm1() { for (double x = -0.9; x < 0.9; x += 0.05) { checkRelative(FastMath.expm1(x), build(x).expm1()); } } @Test public void testLog() { for (double x = 0.01; x < 0.9; x += 0.05) { checkRelative(FastMath.log(x), build(x).log()); } } @Test public void testLog1p() { for (double x = -0.9; x < 0.9; x += 0.05) { checkRelative(FastMath.log1p(x), build(x).log1p()); } } // TODO: add this test in 4.0, as it is not possible to do it in 3.2 // due to incompatibility of the return type in the Dfp class // @Test // public void testLog10() { // for (double x = -0.9; x < 0.9; x += 0.05) { // checkRelative(FastMath.log10(x), build(x).log10()); // } // } @Test public void testAbs() { for (double x = -0.9; x < 0.9; x += 0.05) { checkRelative(FastMath.abs(x), build(x).abs()); } } @Test public void testCeil() { for (double x = -0.9; x < 0.9; x += 0.05) { checkRelative(FastMath.ceil(x), build(x).ceil()); } } @Test public void testFloor() { for (double x = -0.9; x < 0.9; x += 0.05) { checkRelative(FastMath.floor(x), build(x).floor()); } } @Test public void testRint() { for (double x = -0.9; x < 0.9; x += 0.05) { checkRelative(FastMath.rint(x), build(x).rint()); } } @Test public void testRound() { for (double x = -0.9; x < 0.9; x += 0.05) { Assert.assertEquals(FastMath.round(x), build(x).round()); } } @Test public void testSignum() { for (double x = -0.9; x < 0.9; x += 0.05) { checkRelative(FastMath.signum(x), build(x).signum()); } } @Test public void testCopySignField() { for (double x = -3; x < 3; x += 0.2) { for (double y = -3; y < 3; y += 0.2) { checkRelative(FastMath.copySign(x, y), build(x).copySign(build(y))); } } } @Test public void testCopySignDouble() { for (double x = -3; x < 3; x += 0.2) { for (double y = -3; y < 3; y += 0.2) { checkRelative(FastMath.copySign(x, y), build(x).copySign(y)); } } } @Test public void testScalb() { for (double x = -0.9; x < 0.9; x += 0.05) { for (int n = -100; n < 100; ++n) { checkRelative(FastMath.scalb(x, n), build(x).scalb(n)); } } } @Test public void testLinearCombinationFaFa() { RandomGenerator r = new Well1024a(0xfafal); for (int i = 0; i < 50; ++i) { double[] aD = generateDouble(r, 10); double[] bD = generateDouble(r, 10); T[] aF = toFieldArray(aD); T[] bF = toFieldArray(bD); checkRelative(MathArrays.linearCombination(aD, bD), aF[0].linearCombination(aF, bF)); } } @Test public void testLinearCombinationDaFa() { RandomGenerator r = new Well1024a(0xdafal); for (int i = 0; i < 50; ++i) { double[] aD = generateDouble(r, 10); double[] bD = generateDouble(r, 10); T[] bF = toFieldArray(bD); checkRelative(MathArrays.linearCombination(aD, bD), bF[0].linearCombination(aD, bF)); } } @Test public void testLinearCombinationFF2() { RandomGenerator r = new Well1024a(0xff2l); for (int i = 0; i < 50; ++i) { double[] aD = generateDouble(r, 2); double[] bD = generateDouble(r, 2); T[] aF = toFieldArray(aD); T[] bF = toFieldArray(bD); checkRelative(MathArrays.linearCombination(aD[0], bD[0], aD[1], bD[1]), aF[0].linearCombination(aF[0], bF[0], aF[1], bF[1])); } } @Test public void testLinearCombinationDF2() { RandomGenerator r = new Well1024a(0xdf2l); for (int i = 0; i < 50; ++i) { double[] aD = generateDouble(r, 2); double[] bD = generateDouble(r, 2); T[] bF = toFieldArray(bD); checkRelative(MathArrays.linearCombination(aD[0], bD[0], aD[1], bD[1]), bF[0].linearCombination(aD[0], bF[0], aD[1], bF[1])); } } @Test public void testLinearCombinationFF3() { RandomGenerator r = new Well1024a(0xff3l); for (int i = 0; i < 50; ++i) { double[] aD = generateDouble(r, 3); double[] bD = generateDouble(r, 3); T[] aF = toFieldArray(aD); T[] bF = toFieldArray(bD); checkRelative(MathArrays.linearCombination(aD[0], bD[0], aD[1], bD[1], aD[2], bD[2]), aF[0].linearCombination(aF[0], bF[0], aF[1], bF[1], aF[2], bF[2])); } } @Test public void testLinearCombinationDF3() { RandomGenerator r = new Well1024a(0xdf3l); for (int i = 0; i < 50; ++i) { double[] aD = generateDouble(r, 3); double[] bD = generateDouble(r, 3); T[] bF = toFieldArray(bD); checkRelative(MathArrays.linearCombination(aD[0], bD[0], aD[1], bD[1], aD[2], bD[2]), bF[0].linearCombination(aD[0], bF[0], aD[1], bF[1], aD[2], bF[2])); } } @Test public void testLinearCombinationFF4() { RandomGenerator r = new Well1024a(0xff4l); for (int i = 0; i < 50; ++i) { double[] aD = generateDouble(r, 4); double[] bD = generateDouble(r, 4); T[] aF = toFieldArray(aD); T[] bF = toFieldArray(bD); checkRelative(MathArrays.linearCombination(aD[0], bD[0], aD[1], bD[1], aD[2], bD[2], aD[3], bD[3]), aF[0].linearCombination(aF[0], bF[0], aF[1], bF[1], aF[2], bF[2], aF[3], bF[3])); } } @Test public void testLinearCombinationDF4() { RandomGenerator r = new Well1024a(0xdf4l); for (int i = 0; i < 50; ++i) { double[] aD = generateDouble(r, 4); double[] bD = generateDouble(r, 4); T[] bF = toFieldArray(bD); checkRelative(MathArrays.linearCombination(aD[0], bD[0], aD[1], bD[1], aD[2], bD[2], aD[3], bD[3]), bF[0].linearCombination(aD[0], bF[0], aD[1], bF[1], aD[2], bF[2], aD[3], bF[3])); } } @Test public void testGetField() { checkRelative(1.0, build(-10).getField().getOne()); checkRelative(0.0, build(-10).getField().getZero()); } private void checkRelative(double expected, T obtained) { Assert.assertEquals(expected, obtained.getReal(), 1.0e-15 * (1 + FastMath.abs(expected))); } @Test public void testEquals() { T t1a = build(1.0); T t1b = build(1.0); T t2 = build(2.0); Assert.assertTrue(t1a.equals(t1a)); Assert.assertTrue(t1a.equals(t1b)); Assert.assertFalse(t1a.equals(t2)); Assert.assertFalse(t1a.equals(new Object())); } @Test public void testHash() { T t1a = build(1.0); T t1b = build(1.0); T t2 = build(2.0); Assert.assertEquals(t1a.hashCode(), t1b.hashCode()); Assert.assertTrue(t1a.hashCode() != t2.hashCode()); } private double[] generateDouble (final RandomGenerator r, int n) { double[] a = new double[n]; for (int i = 0; i < n; ++i) { a[i] = r.nextDouble(); } return a; } private T[] toFieldArray (double[] a) { T[] f = MathArrays.buildArray(build(0).getField(), a.length); for (int i = 0; i < a.length; ++i) { f[i] = build(a[i]); } return f; } } commons-math3-3.2-src/src/test/maxima/special/RealFunctionValidation/README.txt100644 1750 1750 12720 12126627677 26167 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ Validation of real functions ============================ This document details the procedure used in Commons-Math 3 to assess the accuracy of the implementations of special functions. It is a two-step process 1. reference values are computed with a multi-precision software (for example, the Maxima Computer Algebra System) [1], 2. these reference values are compared with the Commons-Math3 implementation. The accuracy is computed in ulps. This process relies on a small Java application, called RealFunctionValidation, which can be found in $CM3_SRC/src/test/maxima/special, where $CM3_SRC is the root directory to the source of Commons-Math 3 Compilation of RealFunctionValidation ------------------------------------- Change to the relevant directory cd $CM3_SRC/src/test/maxima/special/RealFunctionValidation Compile the source file. The jar file of Commons-Math3 should be included in your classpath. If it is installed in your local maven repository, the following command should work javac -classpath $HOME/.m2/repository/org/apache/commons/commons-math3/3.1-SNAPSHOT/commons-math3-3.1-SNAPSHOT.jar RealFunctionValidation.java Create a jar file jar cfm RealFunctionValidation.jar MANIFEST.txt RealFunctionValidation*.class Remove the unused *.class files rm *.class Invocation of the application RealFunctionValidation ---------------------------------------------------- The java application comes with a shell script, RealFunctionValidaton.sh. You should edit this file, and change the variables - CM3_JAR: full path to the Commons-Math 3 jar file, - APP_JAR: full path to the RealFunctionValidation application jar file. Invoking this application is then very simple. For example, to validate the implementation of Gamma.logGamma, change to directory reference cd $CM3_SRC/src/test/maxima/special/reference and run the application ../RealFunctionValidation/RealFunctionValidation.sh logGamma.properties Syntax of the *.properties files -------------------------------- Parameters of the RealFunctionValidation application are specified through a standard Java properties file. The following keys must be specified in this file - method: the fully qualified name to the function to be validated. This function should be static, take only primitive arguments, and return double. - signature: this key is necessary to discriminate functions with same name. The signature should be specified as in a plain java file. For example signature = double, int, float - inputFileMask: the name of the binary input file(s) containing the high-accuracy reference values. The format of this file is described in the next section. It is possible to specify multiple input files, which are indexed by an integer. Then this key should really be understood as a format string. In other words, the name of the file with index i is given by String.format(inputFileMask, i) - outputFileMask: the name of the binary output file(s) containing the reference values, the values computed through the specified method, and the error (in ulps). The format of this file is described in the next section. As for the input files, it is possible to specify multiple output files. - from: the first index - to: the last index (exclusive) - by: the increment As an example, here is the properties file for evaluation of double Gamma.logGamma(double) method=org.apache.commons.math3.special.Gamma.logGamma signature=double inputFileMask=logGamma-%02d.dat outputFileMask=logGamma-out-%02d.dat from=1 to=5 by=1 Format of the input and output binary files ------------------------------------------- The reference values are saved in a binary file - for a unary function f(x), the data is stored as follows x[0], f(x[0]), x[1], f(x[1]), ... - for a binary function f(x, y), the data is stored as follows x[0], y[0], f(x[0], y[0]), x[1], y[1], f(x[1], y[1]), ... - and similar storage pattern for a n-ary function. The parameters x[i], y[i], ... can be of arbitrary (primitive) type. The return value f(x[i], y[i], ...) must be of type double. The output files are also saved in a binary file - for a unary function f(x), the data is stored as follows x[0], reference value of f(x[0]), actual value of f(x[0], y[0]), error in ulps, x[1], y[1], reference value of f(x[1], y[1]), actual value of f(x[1], y[1]), error in ulps, ... - for a binary function f(x, y), the data is stored as follows x[0], y[0], reference value of f(x[0], y[0]), actual value of f(x[0], y[0]), error in ulps, x[1], y[1], reference value of f(x[1], y[1]), actual value of f(x[1], y[1]), error in ulps, ... The application also prints on the standard output some statistics about the error. References ---------- [1] http://maxima.sourceforge.net/ commons-math3-3.2-src/src/test/maxima/special/RealFunctionValidation/RealFunctionValidation.java100755 1750 1750 33372 12126627677 31751 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.EOFException; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.lang.reflect.Modifier; import java.util.ArrayList; import java.util.List; import java.util.Properties; import org.apache.commons.math3.stat.descriptive.SummaryStatistics; import org.apache.commons.math3.util.FastMath; /* * plot 'logGamma.dat' binary format="%double%double" endian=big u 1:2 w l */ public class RealFunctionValidation { public static class MissingRequiredPropertyException extends IllegalArgumentException { private static final long serialVersionUID = 20121017L; public MissingRequiredPropertyException(final String key) { super("missing required property " + key); } } public static class ApplicationProperties { private static final int DOT = '.'; private static final String METHOD_KEY = "method"; private static final String SIGNATURE_KEY = "signature"; private static final String INPUT_FILE_MASK = "inputFileMask"; private static final String OUTPUT_FILE_MASK = "outputFileMask"; private static final String FROM_KEY = "from"; private static final String TO_KEY = "to"; private static final String BY_KEY = "by"; final Method method; final String inputFileMask; final String outputFileMask; final int from; final int to; final int by; /** * Returns a {@link Method} with specified signature. * * @param className The fully qualified name of the class to which the * method belongs. * @param methodName The name of the method. * @param signature The signature of the method, as a list of parameter * types. * @return the method * @throws SecurityException * @throws ClassNotFoundException */ public static Method findStaticMethod(final String className, final String methodName, final List> signature) throws SecurityException, ClassNotFoundException { final int n = signature.size(); final Method[] methods = Class.forName(className).getMethods(); for (Method method : methods) { if (method.getName().equals(methodName)) { final Class[] parameters = method.getParameterTypes(); boolean sameSignature = true; if (parameters.length == n) { for (int i = 0; i < n; i++) { sameSignature &= signature.get(i) .equals(parameters[i]); } if (sameSignature) { final int modifiers = method.getModifiers(); if ((modifiers & Modifier.STATIC) != 0) { return method; } else { final String msg = "method must be static"; throw new IllegalArgumentException(msg); } } } } } throw new IllegalArgumentException("method not found"); } public static Class parsePrimitiveType(final String type) { if (type.equals("boolean")) { return Boolean.TYPE; } else if (type.equals("byte")) { return Byte.TYPE; } else if (type.equals("char")) { return Character.TYPE; } else if (type.equals("double")) { return Double.TYPE; } else if (type.equals("float")) { return Float.TYPE; } else if (type.equals("int")) { return Integer.TYPE; } else if (type.equals("long")) { return Long.TYPE; } else if (type.equals("short")) { return Short.TYPE; } else { final StringBuilder builder = new StringBuilder(); builder.append(type).append(" is not a primitive type"); throw new IllegalArgumentException(builder.toString()); } } private static String getPropertyAsString(final Properties properties, final String key) { final String value = properties.getProperty(key); if (value == null) { throw new MissingRequiredPropertyException(key); } else { return value; } } private static int getPropertyAsInteger(final Properties properties, final String key) { final String value = properties.getProperty(key); if (value == null) { throw new MissingRequiredPropertyException(key); } else { return Integer.parseInt(value); } } private ApplicationProperties(final String fullyQualifiedName, final String signature, final String inputFileMask, final String outputFileMask, final int from, final int to, final int by) { this.inputFileMask = inputFileMask; this.outputFileMask = outputFileMask; this.from = from; this.to = to; this.by = by; final String[] types = signature.split(","); final List> parameterTypes = new ArrayList>(); for (String type : types) { parameterTypes.add(parsePrimitiveType(type.trim())); } final int index = fullyQualifiedName.lastIndexOf(DOT); try { final String className, methodName; className = fullyQualifiedName.substring(0, index); methodName = fullyQualifiedName.substring(index + 1); this.method = findStaticMethod(className, methodName, parameterTypes); } catch (ClassNotFoundException e) { throw new IllegalArgumentException(e); } } public static final ApplicationProperties create(final Properties properties) { final String methodFullyQualifiedName; methodFullyQualifiedName = getPropertyAsString(properties, METHOD_KEY); final String signature; signature = getPropertyAsString(properties, SIGNATURE_KEY); final String inputFileMask; inputFileMask = getPropertyAsString(properties, INPUT_FILE_MASK); final String outputFileMask; outputFileMask = getPropertyAsString(properties, OUTPUT_FILE_MASK); final int from = getPropertyAsInteger(properties, FROM_KEY); final int to = getPropertyAsInteger(properties, TO_KEY); final int by = getPropertyAsInteger(properties, BY_KEY); return new ApplicationProperties(methodFullyQualifiedName, signature, inputFileMask, outputFileMask, from, to, by); } }; public static Object readAndWritePrimitiveValue(final DataInputStream in, final DataOutputStream out, final Class type) throws IOException { if (!type.isPrimitive()) { throw new IllegalArgumentException("type must be primitive"); } if (type.equals(Boolean.TYPE)) { final boolean x = in.readBoolean(); out.writeBoolean(x); return Boolean.valueOf(x); } else if (type.equals(Byte.TYPE)) { final byte x = in.readByte(); out.writeByte(x); return Byte.valueOf(x); } else if (type.equals(Character.TYPE)) { final char x = in.readChar(); out.writeChar(x); return Character.valueOf(x); } else if (type.equals(Double.TYPE)) { final double x = in.readDouble(); out.writeDouble(x); return Double.valueOf(x); } else if (type.equals(Float.TYPE)) { final float x = in.readFloat(); out.writeFloat(x); return Float.valueOf(x); } else if (type.equals(Integer.TYPE)) { final int x = in.readInt(); out.writeInt(x); return Integer.valueOf(x); } else if (type.equals(Long.TYPE)) { final long x = in.readLong(); out.writeLong(x); return Long.valueOf(x); } else if (type.equals(Short.TYPE)) { final short x = in.readShort(); out.writeShort(x); return Short.valueOf(x); } else { // This should never occur. throw new IllegalStateException(); } } public static SummaryStatistics assessAccuracy(final Method method, final DataInputStream in, final DataOutputStream out) throws IOException, IllegalAccessException, IllegalArgumentException, InvocationTargetException { if (method.getReturnType() != Double.TYPE) { throw new IllegalArgumentException("method must return a double"); } final Class[] types = method.getParameterTypes(); for (int i = 0; i < types.length; i++) { if (!types[i].isPrimitive()) { final StringBuilder builder = new StringBuilder(); builder.append("argument #").append(i + 1) .append(" of method ").append(method.getName()) .append("must be of primitive of type"); throw new IllegalArgumentException(builder.toString()); } } final SummaryStatistics stat = new SummaryStatistics(); final Object[] parameters = new Object[types.length]; while (true) { try { for (int i = 0; i < parameters.length; i++) { parameters[i] = readAndWritePrimitiveValue(in, out, types[i]); } final double expected = in.readDouble(); if (FastMath.abs(expected) > 1E-16) { final Object value = method.invoke(null, parameters); final double actual = ((Double) value).doubleValue(); final double err = FastMath.abs(actual - expected); final double ulps = err / FastMath.ulp(expected); out.writeDouble(expected); out.writeDouble(actual); out.writeDouble(ulps); stat.addValue(ulps); } } catch (EOFException e) { break; } } return stat; } public static void run(final ApplicationProperties properties) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException, IOException { for (int i = properties.from; i < properties.to; i += properties.by) { final String inputFileName; inputFileName = String.format(properties.inputFileMask, i); final String outputFileName; outputFileName = String.format(properties.outputFileMask, i); final DataInputStream in; in = new DataInputStream(new FileInputStream(inputFileName)); final DataOutputStream out; out = new DataOutputStream(new FileOutputStream(outputFileName)); final SummaryStatistics stats; stats = assessAccuracy(properties.method, in, out); System.out.println("input file name = " + inputFileName); System.out.println("output file name = " + outputFileName); System.out.println(stats); } } public static void main(final String[] args) throws IOException, IllegalAccessException, IllegalArgumentException, InvocationTargetException { if (args.length == 0) { final String msg = "missing required properties file"; throw new IllegalArgumentException(msg); } final FileInputStream in = new FileInputStream(args[0]); final Properties properties = new Properties(); properties.load(in); in.close(); final ApplicationProperties p; p = ApplicationProperties.create(properties); run(p); } } commons-math3-3.2-src/src/test/maxima/special/RealFunctionValidation/MANIFEST.txt100644 1750 1750 43 12126627677 26353 0ustarlucluc 0 0 Main-Class: RealFunctionValidation commons-math3-3.2-src/src/test/maxima/special/RealFunctionValidation/RealFunctionValidation.sh100755 1750 1750 2215 12126627677 31412 0ustarlucluc 0 0 #!/bin/bash # # Licensed to the Apache Software Foundation (ASF) under one or more # contributor license agreements. See the NOTICE file distributed with # this work for additional information regarding copyright ownership. # The ASF licenses this file to You 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. # Location of the Commons-Math3 jar file CM3_JAR=$HOME/.m2/repository/org/apache/commons/commons-math3/3.1-SNAPSHOT/commons-math3-3.1-SNAPSHOT.jar # Location of file RealFunctionValidation.jar APP_JAR=$HOME/Documents/workspace/commons-math3/src/test/maxima/special/RealFunctionValidation/RealFunctionValidation.jar java -cp $CM3_JAR:$APP_JAR RealFunctionValidation $1commons-math3-3.2-src/src/test/maxima/special/reference/logBeta.properties100755 1750 1750 1662 12126627677 25504 0ustarlucluc 0 0 # # Licensed to the Apache Software Foundation (ASF) under one or more # contributor license agreements. See the NOTICE file distributed with # this work for additional information regarding copyright ownership. # The ASF licenses this file to You 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. method=org.apache.commons.math3.special.Beta.logBeta signature=double, double inputFileMask=logBeta-%02d.dat outputFileMask=logBeta-out-%02d.dat from=1 to=7 by=1 commons-math3-3.2-src/src/test/maxima/special/reference/logGamma.mac100644 1750 1750 3122 12126627677 24205 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ /* * This script generates reference ("exact") values for the logGamma function. * The generated values are stored sequentially in a binary file, as follows * x[0], f(x[0]), x[1], f(x[1]), ... * where f is the function being sampled. */ kill(all); fpprec : 64; f(x) := log(gamma(x)); sample(x, name) := block( y : [], for i : 1 while i <= length(x) do y : endcons(float(f(x[i])), y), xy : join(float(x), y), stream : openw_binary(name), write_binary_data(xy, stream), close(stream) ); x : append(makelist(bfloat(i / 1024), i, 1, 8192)); sample(x, "logGamma-01.dat"); x : makelist(bfloat(i / 8), i, 65, 8192); sample(x, "logGamma-02.dat"); x : makelist(bfloat(i), i, 1025, 8192); sample(x, "logGamma-03.dat"); x : makelist(bfloat(2**(i / 8)), i, 105, 8112); sample(x, "logGamma-04.dat"); commons-math3-3.2-src/src/test/maxima/special/reference/logGamma.properties100755 1750 1750 1656 12126627677 25656 0ustarlucluc 0 0 # # Licensed to the Apache Software Foundation (ASF) under one or more # contributor license agreements. See the NOTICE file distributed with # this work for additional information regarding copyright ownership. # The ASF licenses this file to You 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. method=org.apache.commons.math3.special.Gamma.logGamma signature=double inputFileMask=logGamma-%02d.dat outputFileMask=logGamma-out-%02d.dat from=1 to=5 by=1 commons-math3-3.2-src/src/test/maxima/special/reference/gamma.mac100644 1750 1750 3446 12126627677 23554 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ /* * This script generates reference ("exact") values for the gamma function. * The generated values are stored sequentially in a binary file, as follows * x[0], f(x[0]), x[1], f(x[1]), ... * where f is the function being sampled. */ kill(all); fpprec : 64; f(x) := gamma(x); sample(x, name) := block( y : [], for i : 1 while i <= length(x) do y : endcons(float(f(x[i])), y), xy : join(float(x), y), stream : openw_binary(name), write_binary_data(xy, stream), close(stream) ); x : makelist(bfloat(i / 1024), i, -5119, -4097); sample(x, "gamma-01.dat"); x : makelist(bfloat(i / 1024), i, -4095, -3073); sample(x, "gamma-02.dat"); x : makelist(bfloat(i / 1024), i, -3071, -2049); sample(x, "gamma-03.dat"); x : makelist(bfloat(i / 1024), i, -2047, -1025); sample(x, "gamma-04.dat"); x : makelist(bfloat(i / 1024), i, -1023, -1); sample(x, "gamma-05.dat"); x : makelist(bfloat(i / 1024), i, 1, 8192); sample(x, "gamma-06.dat"); x : makelist(bfloat(i / 64), i, 513, 9024); sample(x, "gamma-07.dat"); commons-math3-3.2-src/src/test/maxima/special/reference/logBeta.mac100755 1750 1750 3723 12126627677 24050 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ /* * This script generates reference ("exact") values for the logGamma function. * The generated values are stored sequentially in a binary file, as follows * x[0], f(x[0]), x[1], f(x[1]), ... * where f is the function being sampled. */ kill(all); fpprec : 64; sample(x, y, name) := block( stream : openw_binary(name), for i : 1 while i <= length(x) do block( print(i), for j : 1 while j <= length(y) do write_binary_data([x[i], y[j], log(beta(x[i], y[j]))], stream) ), close(stream) ); x : makelist(bfloat(i / 32), i, 1, 256); y : makelist(bfloat(j / 32), j, 1, 256); sample(x, y, "logBeta-01.dat"); x : makelist(bfloat(i / 32), i, 1, 256); y : makelist(bfloat(j / 32), j, 257, 512); sample(x, y, "logBeta-02.dat"); x : makelist(bfloat(i / 32), i, 1, 256); y : makelist(bfloat(j), j, 17, 256); sample(x, y, "logBeta-03.dat"); x : makelist(bfloat(i / 32), i, 257, 512); y : makelist(bfloat(j / 32), j, 257, 512); sample(x, y, "logBeta-04.dat"); x : makelist(bfloat(i / 32), i, 257, 512); y : makelist(bfloat(j), j, 17, 256); sample(x, y, "logBeta-05.dat"); x : makelist(bfloat(i), i, 17, 256); y : makelist(bfloat(j), j, 17, 256); sample(x, y, "logBeta-06.dat"); commons-math3-3.2-src/src/test/maxima/special/reference/gamma.properties100755 1750 1750 1645 12126627677 25212 0ustarlucluc 0 0 # # Licensed to the Apache Software Foundation (ASF) under one or more # contributor license agreements. See the NOTICE file distributed with # this work for additional information regarding copyright ownership. # The ASF licenses this file to You 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. method=org.apache.commons.math3.special.Gamma.gamma signature=double inputFileMask=gamma-%02d.dat outputFileMask=gamma-out-%02d.dat from=1 to=8 by=1 commons-math3-3.2-src/src/test/R/cauchyTestCases.R100644 1750 1750 7450 12126627667 20573 0ustarlucluc 0 0 # Licensed to the Apache Software Foundation (ASF) under one or more # contributor license agreements. See the NOTICE file distributed with # this work for additional information regarding copyright ownership. # The ASF licenses this file to You 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. # #------------------------------------------------------------------------------ # R source file to validate Cauchy distribution tests in # org.apache.commons.math.distribution.CauchyDistributionTest # # To run the test, install R, put this file and testFunctions # into the same directory, launch R from this directory and then enter # source("") # #----------------------------------------------------------------------------- tol <- 1E-9 # Function definitions source("testFunctions") # utility test functions # function to verify distribution computations verifyDistribution <- function(points, expected, median, scale, tol) { rDistValues <- rep(0, length(points)) i <- 0 for (point in points) { i <- i + 1 rDistValues[i] <- pcauchy(point, median, scale, log = FALSE) } output <- c("Distribution test median = ",median,", scale = ", scale) if (assertEquals(expected, rDistValues, tol, "Distribution Values")) { displayPadded(output, SUCCEEDED, WIDTH) } else { displayPadded(output, FAILED, WIDTH) } } # function to verify density computations verifyDensity <- function(points, expected, median, scale, tol) { rDensityValues <- rep(0, length(points)) i <- 0 for (point in points) { i <- i + 1 rDensityValues[i] <- dcauchy(point, median, scale, log = FALSE) } output <- c("Density test median = ",median,", scale = ", scale) if (assertEquals(expected, rDensityValues, tol, "Density Values")) { displayPadded(output, SUCCEEDED, WIDTH) } else { displayPadded(output, FAILED, WIDTH) } } # function to verify quantiles verifyQuantiles <- function(points, expected, median, scale, tol) { rQuantileValues <- rep(0, length(points)) i <- 0 for (point in points) { i <- i + 1 rQuantileValues[i] <- qcauchy(point, median, scale, log = FALSE) } output <- c("Quantile test median = ",median,", scale = ", scale) if (assertEquals(expected, rQuantileValues, tol, "Quantile Values")) { displayPadded(output, SUCCEEDED, WIDTH) } else { displayPadded(output, FAILED, WIDTH) } } #-------------------------------------------------------------------------- cat("Cauchy test cases\n") median <- 1.2 scale <- 2.1 distributionValues <- c(0.001, 0.01, 0.025, 0.05, 0.1, 0.999, 0.990, 0.975, 0.950, 0.900) densityValues <- c(1.49599158008e-06, 0.000149550440335, 0.000933076881878, 0.00370933207799, 0.0144742330437, 1.49599158008e-06, 0.000149550440335, 0.000933076881878, 0.00370933207799, 0.0144742330437) distributionPoints <- c(-667.24856187, -65.6230835029, -25.4830299460, -12.0588781808, -5.26313542807, 669.64856187, 68.0230835029, 27.8830299460, 14.4588781808, 7.66313542807) verifyDistribution(distributionPoints, distributionValues, median, scale, tol) verifyDensity(distributionPoints, densityValues, median, scale, tol) verifyQuantiles(distributionValues, distributionPoints, median, scale, tol) displayDashes(WIDTH) commons-math3-3.2-src/src/test/R/logNormalTestCases100644 1750 1750 11526 12126627667 21070 0ustarlucluc 0 0 # Licensed to the Apache Software Foundation (ASF) under one or more # contributor license agreements. See the NOTICE file distributed with # this work for additional information regarding copyright ownership. # The ASF licenses this file to You 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. # #------------------------------------------------------------------------------ # R source file to validate LogNormal distribution tests in # org.apache.commons.math.distribution.LogNormalDistributionTest # # To run the test, install R, put this file and testFunctions # into the same directory, launch R from this directory and then enter # source("") # # R functions used # plnorm(q, mean=0, sd=1, lower.tail = TRUE, log.p = FALSE) <-- distribution #----------------------------------------------------------------------------- tol <- 1E-9 # Function definitions source("testFunctions") # utility test functions # function to verify distribution computations verifyDistribution <- function(points, expected, mu, sigma, tol) { rDistValues <- rep(0, length(points)) i <- 0 for (point in points) { i <- i + 1 rDistValues[i] <- plnorm(point, mu, sigma, log = FALSE) } output <- c("Distribution test mu = ",mu,", sigma = ", sigma) if (assertEquals(expected, rDistValues, tol, "Distribution Values")) { displayPadded(output, SUCCEEDED, WIDTH) } else { displayPadded(output, FAILED, WIDTH) } } # function to verify density computations verifyDensity <- function(points, expected, mu, sigma, tol) { rDensityValues <- rep(0, length(points)) i <- 0 for (point in points) { i <- i + 1 rDensityValues[i] <- dlnorm(point, mu, sigma, log = FALSE) } output <- c("Density test mu = ",mu,", sigma = ", sigma) if (assertEquals(expected, rDensityValues, tol, "Density Values")) { displayPadded(output, SUCCEEDED, WIDTH) } else { displayPadded(output, FAILED, WIDTH) } } #-------------------------------------------------------------------------- cat("LogNormal test cases\n") mu <- 2.1 sigma <- 1.4 distributionValues <- c(0, 0, 0, 0, 0.00948199951485, 0.432056525076, 0.381648158697, 0.354555726206, 0.329513316888, 0.298422824228) densityValues <- c(0, 0, 0, 0, 0.0594218160072, 0.0436977691036, 0.0508364857798, 0.054873528325, 0.0587182664085, 0.0636229042785) distributionPoints <- c(-2.226325228634938, -1.156887023657177, -0.643949578356075, -0.2027950777320613, 0.305827808237559, 6.42632522863494, 5.35688702365718, 4.843949578356074, 4.40279507773206, 3.89417219176244) verifyDistribution(distributionPoints, distributionValues, mu, sigma, tol) verifyDensity(distributionPoints, densityValues, mu, sigma, tol) distributionValues <- c(0, 0.0396495152787, 0.16601209243, 0.272533253269, 0.357618409638, 0.426488363093, 0.483255136841, 0.530823013877) densityValues <- c(0, 0.0873055825147, 0.0847676303432, 0.0677935186237, 0.0544105523058, 0.0444614628804, 0.0369750288945, 0.0312206409653) distributionPoints <- c(mu - 2 *sigma, mu - sigma, mu, mu + sigma, mu + 2 * sigma, mu + 3 * sigma, mu + 4 * sigma, mu + 5 * sigma) verifyDistribution(distributionPoints, distributionValues, mu, sigma, tol) verifyDensity(distributionPoints, densityValues, mu, sigma, tol) mu <- 0 sigma <- 1 distributionPoints <- c(mu - 2 *sigma, mu - sigma, mu, mu + sigma, mu + 2 * sigma, mu + 3 * sigma, mu + 4 * sigma, mu + 5 * sigma) distributionValues <- c(0, 0, 0, 0.5, 0.755891404214, 0.864031392359, 0.917171480998, 0.946239689548) densityValues <- c(0, 0, 0, 0.398942280401, 0.156874019279, 0.07272825614, 0.0381534565119, 0.0218507148303) verifyDistribution(distributionPoints, distributionValues, mu, sigma, tol) verifyDensity(distributionPoints, densityValues, mu, sigma, tol) mu <- 0 sigma <- 0.1 distributionPoints <- c(mu - 2 *sigma, mu - sigma, mu, mu + sigma, mu + 2 * sigma, mu + 3 * sigma, mu + 4 * sigma, mu + 5 * sigma) distributionValues <- c(0, 0, 0, 1.28417563064e-117, 1.39679883412e-58, 1.09839325447e-33, 2.52587961726e-20, 2.0824223487e-12) densityValues <- c(0, 0, 0, 2.96247992535e-114, 1.1283370232e-55, 4.43812313223e-31, 5.85346445002e-18, 2.9446618076e-10) verifyDistribution(distributionPoints, distributionValues, mu, sigma, tol) verifyDensity(distributionPoints, densityValues, mu, sigma, tol) displayDashes(WIDTH) commons-math3-3.2-src/src/test/R/README.txt100644 1750 1750 16352 12126627667 17074 0ustarlucluc 0 0 # Licensed to the Apache Software Foundation (ASF) under one or more # contributor license agreements. See the NOTICE file distributed with # this work for additional information regarding copyright ownership. # The ASF licenses this file to You 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. # #------------------------------------------------------------------------------ INTRODUCTION The purpose of the R programs included in this directory is to validate the target values used in Apache commons math unit tests. Success running the R and commons-math tests on a platform (OS and R version) means that R and commons-math give results for the test cases that are close in value. The tests include configurable tolerance levels; but care must be taken in changing these, since in most cases the pre-set tolerance is close to the number of decimal digits used in expressing the expected values (both here and in the corresponding commons-math unit tests). Of course it is always possible that both R and commons-math give incorrect values for test cases, so these tests should not be interpreted as definitive in any absolute sense. The value of developing and running the tests is really to generate questions (and answers!) when the two systems give different results. Contributions of additional test cases (both R and Junit code) or just R programs to validate commons-math tests that are not covered here would be greatly appreciated. SETUP 0) Download and install R. You can get R here http://www.r-project.org/ Follow the install instructions and make sure that you can launch R from this (i.e., either explitly add R to your OS path or let the install package do it for you). 1) Launch R from this directory and type > source("testAll") to an R prompt. This should produce output to the console similar to this: Binomial test cases Density test n = 10, p = 0.7...........................................SUCCEEDED Distribution test n = 10, p = 0.7......................................SUCCEEDED Inverse Distribution test n = 10, p = 0.7..............................SUCCEEDED Density test n = 5, p = 0..............................................SUCCEEDED Distribution test n = 5, p = 0.........................................SUCCEEDED Density test n = 5, p = 1..............................................SUCCEEDED Distribution test n = 5, p = 1.........................................SUCCEEDED -------------------------------------------------------------------------------- Normal test cases Distribution test mu = 2.1, sigma = 1.4................................SUCCEEDED Distribution test mu = 2.1, sigma = 1.4................................SUCCEEDED Distribution test mu = 0, sigma = 1....................................SUCCEEDED Distribution test mu = 0, sigma = 0.1..................................SUCCEEDED -------------------------------------------------------------------------------- ... WORKING WITH THE TESTS The R distribution comes with online manuals that you can view by launching a browser instance and then entering > help.start() at an R prompt. Poking about in the test case files and the online docs should bring you up to speed fairly quickly. Here are some basic things to get you started. I should note at this point that I am by no means an expert R programmer, so some things may not be implemented in the the nicest way. Comments / suggestions for improvement are welcome! All of the test cases use some basic functions and global constants (screen width and success / failure strings) defined in "testFunctions." The R "source" function is used to "import" these functions into each of the test programs. The "testAll" program pulls together and executes all of the individual test programs. You can execute any one of them by just entering > source(). The "assertEquals" function in the testFunctions file mimics the similarly named function used by Junit: assertEquals <- function(expected, observed, tol, message) { if(any(abs(expected - observed) > tol)) { cat("FAILURE: ",message,"\n") cat("EXPECTED: ",expected,"\n") cat("OBSERVED: ",observed,"\n") return(0) } else { return(1) } } The and arguments can be scalar values, vectors or matrices. If the arguments are vectors or matrices, corresponding entries are compared. The standard pattern used throughout the tests looks like this (from binomialTestCases): Start by defining a "verification function" -- in this example a function to verify computation of binomial probabilities. The argument is a vector of integer values to feed into the density function, is a vector of the computed probabilies from the commons-math Junit tests, and

                          are parameters of the distribution and is the error tolerance of the test. The function computes the probabilities using R and compares the values that R produces with those in the vector. verifyDensity <- function(points, expected, n, p, tol) { rDensityValues <- rep(0, length(points)) i <- 0 for (point in points) { i <- i + 1 rDensityValues[i] <- dbinom(point, n, p, log = FALSE) } output <- c("Density test n = ", n, ", p = ", p) if (assertEquals(expected,rDensityValues,tol,"Density Values")) { displayPadded(output, SUCCEEDED, WIDTH) } else { displayPadded(output, FAILED, WIDTH) } } The displayPadded function just displays its first and second arguments with enough dots in between to make the whole string WIDTH characters long. It is defined in testFunctions. Then call this function with different parameters corresponding to the different Junit test cases: size <- 10.0 probability <- 0.70 densityPoints <- c(-1,0,1,2,3,4,5,6,7,8,9,10,11) densityValues <- c(0, 0.0000, 0.0001, 0.0014, 0.0090, 0.0368, 0.1029, 0.2001, 0.2668, 0.2335, 0.1211, 0.0282, 0) ... verifyDensity(densityPoints, densityValues, size, probability, tol) If the values computed by R match the target values in densityValues, this will produce one line of output to the console: Density test n = 10, p = 0.7...........................................SUCCEEDED If you modify the value of tol set at the top of binomialTestCases to make the test more sensitive than the number of digits specified in the densityValues vector, it will fail, producing the following output, showing the failure and the expected and observed values: FAILURE: Density Values EXPECTED: 0 0 1e-04 0.0014 0.009 0.0368 0.1029 0.2001 0.2668 0.2335 0.1211 / 0.0282 0 OBSERVED: 0 5.9049e-06 0.000137781 0.0014467005 0.009001692 0.036756909 / 0.1029193452 0.200120949 0.266827932 0.2334744405 0.121060821 0.0282475249 0 Density test n = 10, p = 0.7..............................................FAILED commons-math3-3.2-src/src/test/R/KolmogorovSmirnovDistributionTestCases.R100644 1750 1750 3321 12126627667 25424 0ustarlucluc 0 0 # Licensed to the Apache Software Foundation (ASF) under one or more # contributor license agreements. See the NOTICE file distributed with # this work for additional information regarding copyright ownership. # The ASF licenses this file to You 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. # cat("/* ", version$version.string, " */\n\n\n", sep = "") ns <- c(200, 341, 389) ps <- c(0.005, 0.02, 0.031111, 0.04) for (n in ns) { for (p in ps) { res <- .C("pkolmogorov2x", p = as.double(p), n = as.integer(n), PACKAGE = "stats")$p cat("/* formatC(.C(\"pkolmogorov2x\", p = as.double(", p, "), n = as.integer(", n, "), PACKAGE = \"stats\")$p, 40) gives\n", sep = "") cat(" * ", formatC(res, digits = 40), "\n", sep = "") cat(" */\n") cat("dist = new KolmogorovSmirnovDistributionImpl(", n, ");\n", sep = "") #cat("Assert.assertEquals(", formatC(res, digits = 40), ", dist.cdf(", p, ", true), TOLERANCE);\n", sep = "") cat("Assert.assertEquals(", formatC(res, digits = 40), ", dist.cdf(", p, ", false), TOLERANCE);\n", sep = "") cat("\n") #cat("System.out.println(\"", formatC(res, digits = 20), " - \" + dist.cdf(", p, ", false) + \" = \" + (", res, " - dist.cdf(", p, ", false)));\n", sep = "") } } commons-math3-3.2-src/src/test/R/multipleOLSRegressionTestCases100644 1750 1750 30445 12126627667 23411 0ustarlucluc 0 0 # Licensed to the Apache Software Foundation (ASF) under one or more # contributor license agreements. See the NOTICE file distributed with # this work for additional information regarding copyright ownership. # The ASF licenses this file to You 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. # #------------------------------------------------------------------------------ # R source file to validate OLS multiple regression tests in # org.apache.commons.math.stat.regression.OLSMultipleLinearRegressionTest # # To run the test, install R, put this file and testFunctions # into the same directory, launch R from this directory and then enter # source("") # #------------------------------------------------------------------------------ tol <- 1E-8 # default error tolerance for tests #------------------------------------------------------------------------------ # Function definitions source("testFunctions") # utility test functions options(digits=16) # override number of digits displayed # function to verify OLS computations verifyRegression <- function(model, expectedBeta, expectedResiduals, expectedErrors, expectedStdError, expectedRSquare, expecteAdjRSquare, modelName) { betaHat <- as.vector(coefficients(model)) residuals <- as.vector(residuals(model)) errors <- as.vector(as.matrix(coefficients(summary(model)))[,2]) stdError <- summary(model)$sigma rSquare <- summary(model)$r.squared adjRSquare <- summary(model)$adj.r.squared output <- c("Parameter test dataset = ", modelName) if (assertEquals(expectedBeta,betaHat,tol,"Parameters")) { displayPadded(output, SUCCEEDED, WIDTH) } else { displayPadded(output, FAILED, WIDTH) } output <- c("Residuals test dataset = ", modelName) if (assertEquals(expectedResiduals,residuals,tol,"Residuals")) { displayPadded(output, SUCCEEDED, WIDTH) } else { displayPadded(output, FAILED, WIDTH) } output <- c("Errors test dataset = ", modelName) if (assertEquals(expectedErrors,errors,tol,"Errors")) { displayPadded(output, SUCCEEDED, WIDTH) } else { displayPadded(output, FAILED, WIDTH) } output <- c("Standard Error test dataset = ", modelName) if (assertEquals(expectedStdError,stdError,tol,"Regression Standard Error")) { displayPadded(output, SUCCEEDED, WIDTH) } else { displayPadded(output, FAILED, WIDTH) } output <- c("RSquared test dataset = ", modelName) if (assertEquals(expectedRSquare,rSquare,tol,"RSquared")) { displayPadded(output, SUCCEEDED, WIDTH) } else { displayPadded(output, FAILED, WIDTH) } output <- c("Adjusted RSquared test dataset = ", modelName) if (assertEquals(expecteAdjRSquare,adjRSquare,tol,"Adjusted RSquared")) { displayPadded(output, SUCCEEDED, WIDTH) } else { displayPadded(output, FAILED, WIDTH) } } #-------------------------------------------------------------------------- cat("Multiple regression OLS test cases\n") # Perfect fit x1 <- c(0,2,0,0,0,0) x2 <- c(0,0,3,0,0,0) x3 <- c(0,0,0,4,0,0) x4 <- c(0,0,0,0,5,0) x5 <- c(0,0,0,0,0,6) y <- c(11, 12, 13, 14, 15, 16) model <- lm(y ~ x1 + x2 + x3 + x4 + x5) expectedBeta <- c(11.0,0.5,0.666666666666667,0.75,0.8,0.8333333333333333) expectedResiduals <- c(0,0,0,0,0,0) expectedErrors <- c(NaN,NaN,NaN,NaN,NaN,NaN) expectedStdError <- NaN expectedRSquare <- 1 expectedAdjRSquare <- NaN verifyRegression(model, expectedBeta, expectedResiduals, expectedErrors, expectedStdError, expectedRSquare, expectedAdjRSquare, "perfect fit") # Longley # # Data Source: J. Longley (1967) "An Appraisal of Least Squares Programs for the # Electronic Computer from the Point of View of the User", # Journal of the American Statistical Association, # vol. 62. September, pp. 819-841. # # Certified values (and data) are from NIST: # http://www.itl.nist.gov/div898/strd/lls/data/LINKS/DATA/Longley.dat # design <- matrix(c(60323,83.0,234289,2356,1590,107608,1947, 61122,88.5,259426,2325,1456,108632,1948, 60171,88.2,258054,3682,1616,109773,1949, 61187,89.5,284599,3351,1650,110929,1950, 63221,96.2,328975,2099,3099,112075,1951, 63639,98.1,346999,1932,3594,113270,1952, 64989,99.0,365385,1870,3547,115094,1953, 63761,100.0,363112,3578,3350,116219,1954, 66019,101.2,397469,2904,3048,117388,1955, 67857,104.6,419180,2822,2857,118734,1956, 68169,108.4,442769,2936,2798,120445,1957, 66513,110.8,444546,4681,2637,121950,1958, 68655,112.6,482704,3813,2552,123366,1959, 69564,114.2,502601,3931,2514,125368,1960, 69331,115.7,518173,4806,2572,127852,1961, 70551,116.9,554894,4007,2827,130081,1962), nrow = 16, ncol = 7, byrow = TRUE) y <- design[,1] x1 <- design[,2] x2 <- design[,3] x3 <- design[,4] x4 <- design[,5] x5 <- design[,6] x6 <- design[,7] model <- lm(y ~ x1 + x2 + x3 + x4 + x5 + x6) estimates <- matrix(c(-3482258.63459582,890420.383607373, 15.0618722713733,84.9149257747669, -0.358191792925910E-01,0.334910077722432E-01, -2.02022980381683,0.488399681651699, -1.03322686717359,0.214274163161675, -0.511041056535807E-01,0.226073200069370, 1829.15146461355,455.478499142212), nrow = 7, ncol = 2, byrow = TRUE) expectedBeta <- estimates[,1] expectedErrors <- estimates[,2] expectedResiduals <- c(267.340029759711,-94.0139423988359,46.28716775752924, -410.114621930906,309.7145907602313,-249.3112153297231,-164.0489563956039, -13.18035686637081,14.30477260005235,455.394094551857,-17.26892711483297, -39.0550425226967,-155.5499735953195,-85.6713080421283,341.9315139607727, -206.7578251937366) expectedStdError <- 304.8540735619638 expectedRSquare <- 0.995479004577296 expectedAdjRSquare <- 0.992465007628826 verifyRegression(model, expectedBeta, expectedResiduals, expectedErrors, expectedStdError, expectedRSquare, expectedAdjRSquare, "Longley") # Model with no intercept model <- lm(y ~ 0 + x1 + x2 + x3 + x4 + x5 + x6) estimates <- matrix(c(-52.99357013868291, 129.54486693117232, 0.07107319907358, 0.03016640003786, -0.42346585566399, 0.41773654056612, -0.57256866841929, 0.27899087467676, -0.41420358884978, 0.32128496193363, 48.41786562001326, 17.68948737819961), nrow = 6, ncol = 2, byrow = TRUE) expectedBeta <- estimates[,1] expectedErrors <- estimates[,2] expectedResiduals <- c(279.90274927293092, -130.32465380836874, 90.73228661967445, -401.31252201634948, -440.46768772620027, -543.54512853774793, 201.32111639536299, 215.90889365977932, 73.09368242049943, 913.21694494481869, 424.82484953610174, -8.56475876776709, -361.32974610842876, 27.34560497213464, 151.28955976355002, -492.49937355336846) expectedStdError <- 475.1655079819517 expectedRSquare <- 0.9999670130706 expectedAdjRSquare <- 0.999947220913 verifyRegression(model, expectedBeta, expectedResiduals, expectedErrors, expectedStdError, expectedRSquare, expectedAdjRSquare, "Longley No Intercept") # Swiss Fertility (R dataset named "swiss") design <- matrix(c(80.2,17.0,15,12,9.96, 83.1,45.1,6,9,84.84, 92.5,39.7,5,5,93.40, 85.8,36.5,12,7,33.77, 76.9,43.5,17,15,5.16, 76.1,35.3,9,7,90.57, 83.8,70.2,16,7,92.85, 92.4,67.8,14,8,97.16, 82.4,53.3,12,7,97.67, 82.9,45.2,16,13,91.38, 87.1,64.5,14,6,98.61, 64.1,62.0,21,12,8.52, 66.9,67.5,14,7,2.27, 68.9,60.7,19,12,4.43, 61.7,69.3,22,5,2.82, 68.3,72.6,18,2,24.20, 71.7,34.0,17,8,3.30, 55.7,19.4,26,28,12.11, 54.3,15.2,31,20,2.15, 65.1,73.0,19,9,2.84, 65.5,59.8,22,10,5.23, 65.0,55.1,14,3,4.52, 56.6,50.9,22,12,15.14, 57.4,54.1,20,6,4.20, 72.5,71.2,12,1,2.40, 74.2,58.1,14,8,5.23, 72.0,63.5,6,3,2.56, 60.5,60.8,16,10,7.72, 58.3,26.8,25,19,18.46, 65.4,49.5,15,8,6.10, 75.5,85.9,3,2,99.71, 69.3,84.9,7,6,99.68, 77.3,89.7,5,2,100.00, 70.5,78.2,12,6,98.96, 79.4,64.9,7,3,98.22, 65.0,75.9,9,9,99.06, 92.2,84.6,3,3,99.46, 79.3,63.1,13,13,96.83, 70.4,38.4,26,12,5.62, 65.7,7.7,29,11,13.79, 72.7,16.7,22,13,11.22, 64.4,17.6,35,32,16.92, 77.6,37.6,15,7,4.97, 67.6,18.7,25,7,8.65, 35.0,1.2,37,53,42.34, 44.7,46.6,16,29,50.43, 42.8,27.7,22,29,58.33), nrow = 47, ncol = 5, byrow = TRUE) y <- design[,1] x1 <- design[,2] x2 <- design[,3] x3 <- design[,4] x4 <- design[,5] model <- lm(y ~ x1 + x2 + x3 + x4) estimates <- matrix(c(91.05542390271397,6.94881329475087, -0.22064551045715,0.07360008972340, -0.26058239824328,0.27410957467466, -0.96161238456030,0.19454551679325, 0.12441843147162,0.03726654773803), nrow = 5, ncol = 2, byrow = TRUE) expectedBeta <- estimates[,1] expectedErrors <- estimates[,2] expectedResiduals <- c(7.1044267859730512,1.6580347433531366, 4.6944952770029644,8.4548022690166160,13.6547432343186212, -9.3586864458500774,7.5822446330520386,15.5568995563859289, 0.8113090736598980,7.1186762732484308,7.4251378771228724, 2.6761316873234109,0.8351584810309354,7.1769991119615177, -3.8746753206299553,-3.1337779476387251,-0.1412575244091504, 1.1186809170469780,-6.3588097346816594,3.4039270429434074, 2.3374058329820175,-7.9272368576900503,-7.8361010968497959, -11.2597369269357070,0.9445333697827101,6.6544245101380328, -0.9146136301118665,-4.3152449403848570,-4.3536932047009183, -3.8907885169304661,-6.3027643926302188,-7.8308982189289091, -3.1792280015332750,-6.7167298771158226,-4.8469946718041754, -10.6335664353633685,11.1031134362036958,6.0084032641811733, 5.4326230830188482,-7.2375578629692230,2.1671550814448222, 15.0147574652763112,4.8625103516321015,-7.1597256413907706, -0.4515205619767598,-10.2916870903837587,-15.7812984571900063) expectedStdError <- 7.73642194433223 expectedRSquare <- 0.649789742860228 expectedAdjRSquare <- 0.6164363850373927 verifyRegression(model, expectedBeta, expectedResiduals, expectedErrors, expectedStdError, expectedRSquare, expectedAdjRSquare, "Swiss Fertility") # model with no intercept model <- lm(y ~ 0 + x1 + x2 + x3 + x4) estimates <- matrix(c(0.52191832900513, 0.10470063765677, 2.36588087917963, 0.41684100584290, -0.94770353802795, 0.43370143099691, 0.30851985863609, 0.07694953606522), nrow = 4, ncol = 2, byrow = TRUE) expectedBeta <- estimates[,1] expectedErrors <- estimates[,2] expectedResiduals <- c(44.138759883538249, 27.720705122356215, 35.873200836126799, 34.574619581211977, 26.600168342080213, 15.074636243026923, -12.704904871199814, 1.497443824078134, 2.691972687079431, 5.582798774291231, -4.422986561283165, -9.198581600334345, 4.481765170730647, 2.273520207553216, -22.649827853221336, -17.747900013943308, 20.298314638496436, 6.861405135329779, -8.684712790954924, -10.298639278062371, -9.896618896845819, 4.568568616351242, -15.313570491727944, -13.762961360873966, 7.156100301980509, 16.722282219843990, 26.716200609071898, -1.991466398777079, -2.523342564719335, 9.776486693095093, -5.297535127628603, -16.639070567471094, -10.302057295211819, -23.549487860816846, 1.506624392156384, -17.939174438345930, 13.105792202765040, -1.943329906928462, -1.516005841666695, -0.759066561832886, 20.793137744128977, -2.485236153005426, 27.588238710486976, 2.658333257106881, -15.998337823623046, -5.550742066720694, -14.219077806826615) expectedStdError <- 17.24710630547 expectedRSquare <- 0.946350722085 expectedAdjRSquare <- 0.9413600915813 verifyRegression(model, expectedBeta, expectedResiduals, expectedErrors, expectedStdError, expectedRSquare, expectedAdjRSquare, "Swiss Fertility No Intercept") displayDashes(WIDTH) commons-math3-3.2-src/src/test/R/testAll100644 1750 1750 4467 12126627667 16715 0ustarlucluc 0 0 # Licensed to the Apache Software Foundation (ASF) under one or more # contributor license agreements. See the NOTICE file distributed with # this work for additional information regarding copyright ownership. # The ASF licenses this file to You 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. # #------------------------------------------------------------------------------ # R source file to run all commons-math R verification tests # # To run the test, install R, put this file and all other o.a.c.math R # verification tests and the testfunctions utilities file into the same # directory, launch R from this directory and then enter # source("") # # To redirect output to a file, uncomment the following line, substituting # another file path if you like (default behavior is to write the file to the # current directory). # # sink("testResults") #------------------------------------------------------------------------------ # distribution source("binomialTestCases") source("normalTestCases") source("poissonTestCases") source("hypergeometricTestCases") source("exponentialTestCases") source("cauchyTestCases.R") source("pascalTestCases") source("TDistributionTestCases.R") source("FDistributionTestCases.R") source("GammaDistributionTestCases.R") source("WeibullDistributionTestCases.R") source("ChiSquareDistributionTestCases.R") source("LevyDistributionTestCases.R") # regression source("regressionTestCases") # inference source("chiSquareTestCases") source("anovaTestCases") # descriptive source("descriptiveTestCases") # multiple regression source("multipleOLSRegressionTestCases") # covariance source("covarianceTestCases") # correlation source("correlationTestCases") #------------------------------------------------------------------------------ # if output has been diverted, change it back if (sink.number()) { sink() } commons-math3-3.2-src/src/test/R/GammaDistributionTestCases.R100644 1750 1750 7460 12126627667 22742 0ustarlucluc 0 0 # Licensed to the Apache Software Foundation (ASF) under one or more # contributor license agreements. See the NOTICE file distributed with # this work for additional information regarding copyright ownership. # The ASF licenses this file to You 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. # #------------------------------------------------------------------------------ # R source file to validate Gamma distribution tests in # org.apache.commons.math.distribution.GammaDistributionTest # # To run the test, install R, put this file and testFunctions # into the same directory, launch R from this directory and then enter # source("") # #----------------------------------------------------------------------------- tol <- 1E-9 # Function definitions source("testFunctions") # utility test functions # function to verify distribution computations verifyDistribution <- function(points, expected, alpha, beta, tol) { rDistValues <- rep(0, length(points)) i <- 0 for (point in points) { i <- i + 1 rDistValues[i] <- pgamma(point, shape=alpha, scale=beta, log = FALSE) } output <- c("Distribution test shape = ", shape, " scale = ", scale) if (assertEquals(expected, rDistValues, tol, "Distribution Values")) { displayPadded(output, SUCCEEDED, WIDTH) } else { displayPadded(output, FAILED, WIDTH) } } # function to verify density computations verifyDensity <- function(points, expected, alpha, beta, tol) { rDensityValues <- rep(0, length(points)) i <- 0 for (point in points) { i <- i + 1 rDensityValues[i] <- dgamma(point, shape=alpha, scale=beta, log = FALSE) } output <- c("Density test shape = ", shape, " scale = ", scale) if (assertEquals(expected, rDensityValues, tol, "Density Values")) { displayPadded(output, SUCCEEDED, WIDTH) } else { displayPadded(output, FAILED, WIDTH) } } # function to verify quantiles verifyQuantiles <- function(points, expected, alpha, beta, tol) { rQuantileValues <- rep(0, length(points)) i <- 0 for (point in points) { i <- i + 1 rQuantileValues[i] <- qgamma(point, shape=alpha, scale=beta, log = FALSE) } output <- c("Quantile test shape = ", shape, " scale = ", scale) if (assertEquals(expected, rQuantileValues, tol, "Quantile Values")) { displayPadded(output, SUCCEEDED, WIDTH) } else { displayPadded(output, FAILED, WIDTH) } } #-------------------------------------------------------------------------- cat("Gamma Distribution test cases\n") shape <- 4 scale <- 2 distributionValues <- c(0.001, 0.01, 0.025, 0.05, 0.1, 0.999, 0.990, 0.975, 0.950, 0.900) densityValues <- c(0.00427280075546, 0.0204117166709, 0.0362756163658, 0.0542113174239, 0.0773195272491, 0.000394468852816, 0.00366559696761, 0.00874649473311, 0.0166712508128, 0.0311798227954) distributionPoints <- c(0.857104827257, 1.64649737269, 2.17973074725, 2.7326367935, 3.48953912565, 26.1244815584, 20.0902350297, 17.5345461395, 15.5073130559, 13.3615661365) verifyQuantiles(distributionValues, distributionPoints, shape, scale, tol) verifyDistribution(distributionPoints, distributionValues, shape, scale, tol) verifyDensity(distributionPoints, densityValues, shape, scale, tol) displayDashes(WIDTH) commons-math3-3.2-src/src/test/R/descriptiveTestCases100644 1750 1750 7164 12126627667 21442 0ustarlucluc 0 0 # Licensed to the Apache Software Foundation (ASF) under one or more # contributor license agreements. See the NOTICE file distributed with # this work for additional information regarding copyright ownership. # The ASF licenses this file to You 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. # #------------------------------------------------------------------------------ # R source file to validate summary statistics tests in # org.apache.commons.math.stat.CertifiedDataTest # # To run the test, install R, put this file and testFunctions # into the same directory, launch R from this directory and then enter # source("") # # R functions used # mean(x) # sd(x) #------------------------------------------------------------------------------ tol <- 1E-14 # error tolerance for tests #------------------------------------------------------------------------------ # Function definitions source("testFunctions") # utility test functions options(digits=15) # bump displayed digits to 15 verifyMean <- function(values, expectedMean, tol, desc) { results <- mean(values) if (assertEquals(expectedMean, results, tol, "mean")) { displayPadded(c(desc," mean test"), SUCCEEDED, WIDTH) } else { displayPadded(c(desc, " mean test"), FAILED, WIDTH) } } verifySigma <- function(values, expectedSigma, tol, desc) { results <- sd(values) if (assertEquals(expectedSigma, results, tol, "std")) { displayPadded(c(desc," std test"), SUCCEEDED, WIDTH) } else { displayPadded(c(desc, " std test"), FAILED, WIDTH) } } cat("Descriptive test cases\n") # Michelson data values <- c(299.85,299.74,299.90,300.07,299.93,299.85,299.95,299.98,299.98, 299.88,300.00,299.98,299.93,299.65,299.76,299.81,300.00,300.00,299.96,299.96, 299.96,299.94,299.96,299.94,299.88,299.80,299.85,299.88,299.90,299.84,299.83, 299.79,299.81,299.88,299.88,299.83,299.80,299.79,299.76,299.80,299.88,299.88, 299.88,299.86,299.72,299.72,299.62,299.86,299.97,299.95,299.88,299.91,299.85, 299.87,299.84,299.84,299.85,299.84,299.84,299.84,299.89,299.81,299.81,299.82, 299.80,299.77,299.76,299.74,299.75,299.76,299.91,299.92,299.89,299.86,299.88, 299.72,299.84,299.85,299.85,299.78,299.89,299.84,299.78,299.81,299.76,299.81, 299.79,299.81,299.82,299.85,299.87,299.87,299.81,299.74,299.81,299.94,299.95, 299.80,299.81,299.87) expectedMean <- 299.852400000000 expectedSigma <- 0.0790105478190518 verifyMean(values, expectedMean, tol, "Michelson") verifySigma(values, expectedSigma, tol, "Michelson") # Mavro data values <- c(2.00180,2.00170,2.00180,2.00190,2.00180,2.00170,2.00150,2.00140, 2.00150,2.00150,2.00170,2.00180,2.00180,2.00190,2.00190,2.00210,2.00200, 2.00160,2.00140,2.00130,2.00130,2.00150,2.00150,2.00160,2.00150,2.00140, 2.00130,2.00140,2.00150,2.00140,2.00150,2.00160,2.00150,2.00160,2.00190, 2.00200,2.00200,2.00210,2.00220,2.00230,2.00240,2.00250,2.00270,2.00260, 2.00260,2.00260,2.00270,2.00260,2.00250,2.00240) expectedMean <- 2.00185600000000 expectedSigma <- 0.000429123454003053 verifyMean(values, expectedMean, tol, "Mavro") verifySigma(values, expectedSigma, tol, "Mavro") displayDashes(WIDTH) commons-math3-3.2-src/src/test/R/TTestCases100644 1750 1750 10413 12126627667 17333 0ustarlucluc 0 0 # Licensed to the Apache Software Foundation (ASF) under one or more # contributor license agreements. See the NOTICE file distributed with # this work for additional information regarding copyright ownership. # The ASF licenses this file to You 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. # #------------------------------------------------------------------------------ # R source file to validate TTest tests in # org.apache.commons.math.inference.TTestImpl # # To run the test, install R, put this file and testFunctions # into the same directory, launch R from this directory and then enter # source("") # # R functions used # t.test(x, y = NULL, alternative = c("two.sided", "less", "greater"), # mu = 0, paired = FALSE, var.equal = FALSE, ... ) # Arguments # x a numeric vector of data values. # y an optional numeric vector data values. # alternative a character string specifying the alternative hypothesis, # must be one of "two.sided" (default), "greater" or "less". You can specify # just the initial letter. # mu a number indicating the true value of the mean (or difference in means # if you are performing a two sample test). # paired a logical indicating whether you want a paired t-test. # var.equal a logical variable indicating whether to treat the two # variances as being equal. # If TRUE then the pooled variance is used to estimate the variance, # otherwise the Welch (or Satterthwaite) approximation to the degrees # of freedom is used. #------------------------------------------------------------------------------ tol <- 1E-10 # error tolerance for tests #------------------------------------------------------------------------------ # Function definitions #------------------------------------------------------------------------------ source("testFunctions") # utility test functions #------------------------------------------------------------------------------ # Verification function # verifyTest <- function(out,expectedP, expectedT, tol) { if (assertEquals(expectedP, out$p.value, tol, "Ttest p value")) { displayPadded(output, SUCCEEDED, 80) } else { displayPadded(output, FAILED, 80) } output <- c("t test test statistic") if (assertEquals(expectedT, out$statistic, tol, "Ttest t statistic")) { displayPadded(output, SUCCEEDED, 80) } else { displayPadded(output, FAILED, 80) } displayDashes(WIDTH) } cat("One-sample, two-sided TTest test cases \n") sample1 <- c(93.0, 103.0, 95.0, 101.0, 91.0, 105.0, 96.0, 94.0, 101.0, 88.0, 98.0, 94.0, 101.0, 92.0, 95.0) out <- t.test(sample1, mu=100.0) expectedP <- 0.0136390585873 expectedT<- -2.81976445346 verifyTest(out,expectedP, expectedT, tol) cat("One-sample, one-sided TTest test cases \n") sample1 <- c(2, 0, 6, 6, 3, 3, 2, 3, -6, 6, 6, 6, 3, 0, 1, 1, 0, 2, 3, 3) out <- t.test(sample1, mu=0.0, alternative="g") expectedP <- 0.000521637019637 expectedT<- 3.86485535541 verifyTest(out,expectedP, expectedT, tol) cat("Homoscedastic TTest test cases \n") sample1 <- c(2, 4, 6, 8, 10, 97) sample2 <- c(4, 6, 8, 10, 16) out <- t.test(sample1,sample2,var.equal = TRUE) expectedP <- 0.4833963785 expectedT<- 0.73096310086 verifyTest(out,expectedP, expectedT, tol) cat("Heteroscedastic TTest test cases \n") sample1 <- c(7, -4, 18, 17, -3, -5, 1, 10, 11, -2) sample2 <- c(-1, 12, -1, -3, 3, -5, 5, 2, -11, -1, -3) out <- t.test(sample1,sample2,var.equal = FALSE) expectedP <- 0.128839369622 expectedT<- 1.60371728768 verifyTest(out,expectedP, expectedT, tol) cat("Small sample, heteroscedastic test cases \n") sample1 <- c(1,3) sample2 <- c(4,5) out <- t.test(sample1,sample2,var.equal = FALSE) expectedP <- 0.198727388935 expectedT<- -2.2360679775 verifyTest(out,expectedP, expectedT, tol) commons-math3-3.2-src/src/test/R/LevyDistributionTestCases.R100644 1750 1750 7303 12126627667 22633 0ustarlucluc 0 0 # Licensed to the Apache Software Foundation (ASF) under one or more # contributor license agreements. See the NOTICE file distributed with # this work for additional information regarding copyright ownership. # The ASF licenses this file to You 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. # #------------------------------------------------------------------------------ # R source file to validate Lévy distribution tests in # org.apache.commons.math3.distribution.LevyDistributionTest # # To run the test, install R, put this file and testFunctions # into the same directory, launch R from this directory and then enter # source("") # # R functions used # dlevy(q, m=0, s=1, log=FALSE) # plevy(q, m=0, s=1) # qlevy(p, m=0, s=1) #----------------------------------------------------------------------------- tol <- 1E-9 # Function definitions source("testFunctions") # utility test functions library(rmutil) # function to verify distribution computations verifyDistribution <- function(points, expected, m, s, tol) { rDistValues <- rep(0, length(points)) i <- 0 for (point in points) { i <- i + 1 rDistValues[i] <- plevy(point, m, s) } output <- c("Distribution test m = ",m,", s = ", s) if (assertEquals(expected, rDistValues, tol, "Distribution Values")) { displayPadded(output, SUCCEEDED, WIDTH) } else { displayPadded(output, FAILED, WIDTH) } } # function to verify density computations verifyDensity <- function(points, expected, m, s, tol) { rDensityValues <- rep(0, length(points)) i <- 0 for (point in points) { i <- i + 1 rDensityValues[i] <- dlevy(point, m, s, log=FALSE) } output <- c("Density test m = ",m,", s = ", s) if (assertEquals(expected, rDensityValues, tol, "Density Values")) { displayPadded(output, SUCCEEDED, WIDTH) } else { displayPadded(output, FAILED, WIDTH) } } #-------------------------------------------------------------------------- cat("Levy test cases\n") m <- 1.2 s <- 0.4 distributionPoints <- c(1.2001, 1.21, 1.225, 1.25, 1.3, 1.9, 3.4, 5.6) densityValues <- c(0.0, 5.200563737654472E-7, 0.021412836122382383, 0.4133397070818418, 1.0798193302637613, 0.3237493191610873, 0.07060325500936372, 0.026122839883975738) distributionValues <- c(0.0, 2.539628589470901E-10, 6.334248366624259E-5, 0.004677734981047284, 0.04550026389635843, 0.4496917979688907, 0.6698153575994166, 0.763024600552995) verifyDistribution(distributionPoints, distributionValues, m, s, tol) verifyDensity(distributionPoints, densityValues, m, s, tol) m <- 5 s <- 1.3 distributionPoints <- c(5.0001, 6, 7, 8, 9, 10, 11, 12, 13, 14) densityValues <- c(0.0, 0.23745992633364185, 0.1161959636020616, 0.07048597672583455, 0.04833023442399538, 0.03572468867742048, 0.02777194506550441, 0.022382435270909086, 0.018533623436073274, 0.0156730047506865) distributionValues <- c(0.0, 0.25421322360396437, 0.42011267955064, 0.5103578488686935, 0.5686182086635944, 0.6101201547975077, 0.6415915735304425, 0.6665077778509312, 0.6868651803414656, 0.7039020091632311) verifyDistribution(distributionPoints, distributionValues, m, s, tol) verifyDensity(distributionPoints, densityValues, m, s, tol) displayDashes(WIDTH) commons-math3-3.2-src/src/test/R/pascalTestCases100644 1750 1750 12227 12126627667 20400 0ustarlucluc 0 0 # Licensed to the Apache Software Foundation (ASF) under one or more # contributor license agreements. See the NOTICE file distributed with # this work for additional information regarding copyright ownership. # The ASF licenses this file to You 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. # #------------------------------------------------------------------------------ # R source file to validate Pascal distribution tests in # org.apache.commons.math.distribution.PascalDistributionTest # # To run the test, install R, put this file and testFunctions # into the same directory, launch R from this directory and then enter # source("") # # R functions used # dnbinom(x, size, prob, mu, log = FALSE) <- density # pnbinom(q, size, prob, mu, lower.tail = TRUE, log.p = FALSE) <- distribution # qnbinom(p, size, prob, mu, lower.tail = TRUE, log.p = FALSE) <- quantiles #------------------------------------------------------------------------------ tol <- 1E-9 # error tolerance for tests #------------------------------------------------------------------------------ # Function definitions source("testFunctions") # utility test functions # function to verify density computations verifyDensity <- function(points, expected, size, p, tol) { rDensityValues <- rep(0, length(points)) i <- 0 for (point in points) { i <- i + 1 rDensityValues[i] <- dnbinom(point, size, p) } output <- c("Density test size = ", size, ", p = ", p) if (assertEquals(expected,rDensityValues,tol,"Density Values")) { displayPadded(output, SUCCEEDED, WIDTH) } else { displayPadded(output, FAILED, WIDTH) } } # function to verify distribution computations verifyDistribution <- function(points, expected, size, p, tol) { rDistValues <- rep(0, length(points)) i <- 0 for (point in points) { i <- i + 1 rDistValues[i] <- pnbinom(point, size, p) } output <- c("Distribution test size = ", size, ", p = ", p) if (assertEquals(expected,rDistValues,tol,"Distribution Values")) { displayPadded(output, SUCCEEDED, WIDTH) } else { displayPadded(output, FAILED, WIDTH) } } #-------------------------------------------------------------------------- cat("Negative Binomial test cases\n") size <- 10.0 probability <- 0.70 densityPoints <- c(-1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11) densityValues <- c(0, 0.0282475249, 0.0847425747, 0.139825248255, 0.167790297906, 0.163595540458, 0.137420253985, 0.103065190489, 0.070673273478, 0.0450542118422, 0.0270325271053, 0.0154085404500, 0.0084046584273) distributionValues <- c(0, 0.0282475249, 0.1129900996, 0.252815347855, 0.420605645761, 0.584201186219, 0.721621440204, 0.824686630693, 0.895359904171, 0.940414116013, 0.967446643119, 0.982855183569, 0.991259841996) inverseCumPoints <- c( 0, 0.001, 0.010, 0.025, 0.050, 0.100, 0.999, 0.990, 0.975, 0.950, 0.900) inverseCumValues <- c(-1, -1, -1, -1, 0, 0, 13, 10, 9, 8, 7) verifyDensity(densityPoints,densityValues,size,probability,tol) verifyDistribution(densityPoints, distributionValues, size, probability, tol) i <- 0 rInverseCumValues <- rep(0,length(inverseCumPoints)) for (point in inverseCumPoints) { i <- i + 1 rInverseCumValues[i] <- qnbinom(point, size, probability) } output <- c("Inverse Distribution test n = ", size, ", p = ", probability) # R defines quantiles from the right, need to subtract one if (assertEquals(inverseCumValues, rInverseCumValues-1, tol, "Inverse Dist Values")) { displayPadded(output, SUCCEEDED, 80) } else { displayPadded(output, FAILED, 80) } # Degenerate cases size <- 5 probability <- 0.0 densityPoints <- c(-1, 0, 1, 10, 11) # Note: commons math returns 0's below densityValues <- c(NaN, NaN, NaN, NaN, NaN) distributionPoints <- c(-1, 0, 1, 5, 10) # Note: commons math returns 0's below distributionValues <- c(NaN, NaN, NaN, NaN, NaN) output <- c("Density test n = ", size, ", p = ", probability) verifyDensity(densityPoints,densityValues,size,probability,tol) output <- c("Distribution test n = ", size, ", p = ", probability) verifyDistribution(distributionPoints,distributionValues,size,probability,tol) size <- 5 probability <- 1.0 densityPoints <- c(-1, 0, 1, 2, 5, 10) densityValues <- c(0, 1, 0, 0, 0, 0) distributionPoints <- c(-1, 0, 1, 2, 5, 10) distributionValues <- c(0, 1, 1, 1, 1, 1) output <- c("Density test n = ", size, ", p = ", probability) verifyDensity(densityPoints,densityValues,size,probability,tol) output <- c("Distribution test n = ", size, ", p = ", probability) verifyDistribution(distributionPoints,distributionValues,size,probability,tol) displayDashes(WIDTH) commons-math3-3.2-src/src/test/R/normalTestCases100644 1750 1750 11544 12126627667 20426 0ustarlucluc 0 0 # Licensed to the Apache Software Foundation (ASF) under one or more # contributor license agreements. See the NOTICE file distributed with # this work for additional information regarding copyright ownership. # The ASF licenses this file to You 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. # #------------------------------------------------------------------------------ # R source file to validate Normal distribution tests in # org.apache.commons.math.distribution.NormalDistributionTest # # To run the test, install R, put this file and testFunctions # into the same directory, launch R from this directory and then enter # source("") # # R functions used # pnorm(q, mean=0, sd=1, lower.tail = TRUE, log.p = FALSE) <-- distribution #----------------------------------------------------------------------------- tol <- 1E-9 # Function definitions source("testFunctions") # utility test functions # function to verify distribution computations verifyDistribution <- function(points, expected, mu, sigma, tol) { rDistValues <- rep(0, length(points)) i <- 0 for (point in points) { i <- i + 1 rDistValues[i] <- pnorm(point, mu, sigma, log = FALSE) } output <- c("Distribution test mu = ",mu,", sigma = ", sigma) if (assertEquals(expected, rDistValues, tol, "Distribution Values")) { displayPadded(output, SUCCEEDED, WIDTH) } else { displayPadded(output, FAILED, WIDTH) } } # function to verify density computations verifyDensity <- function(points, expected, mu, sigma, tol) { rDensityValues <- rep(0, length(points)) i <- 0 for (point in points) { i <- i + 1 rDensityValues[i] <- dnorm(point, mu, sigma, log = FALSE) } output <- c("Density test mu = ",mu,", sigma = ", sigma) if (assertEquals(expected, rDensityValues, tol, "Density Values")) { displayPadded(output, SUCCEEDED, WIDTH) } else { displayPadded(output, FAILED, WIDTH) } } #-------------------------------------------------------------------------- cat("Normal test cases\n") mu <- 2.1 sigma <- 1.4 distributionValues <- c(0.001, 0.01, 0.025, 0.05, 0.1, 0.999, 0.990, 0.975, 0.950, 0.900) densityValues <- c(0.00240506434076, 0.0190372444310, 0.0417464784322, 0.0736683145538, 0.125355951380, 0.00240506434076, 0.0190372444310, 0.0417464784322, 0.0736683145538, 0.125355951380) distributionPoints <- c(-2.226325228634938, -1.156887023657177, -0.643949578356075, -0.2027950777320613, 0.305827808237559, 6.42632522863494, 5.35688702365718, 4.843949578356074, 4.40279507773206, 3.89417219176244) verifyDistribution(distributionPoints, distributionValues, mu, sigma, tol) verifyDensity(distributionPoints, densityValues, mu, sigma, tol) distributionValues <- c( 0.0227501319482, 0.158655253931, 0.5, 0.841344746069, 0.977249868052, 0.998650101968, 0.999968328758, 0.999999713348) densityValues <- c(0.0385649760808, 0.172836231799, 0.284958771715, 0.172836231799, 0.0385649760808, 0.00316560600853, 9.55930184035e-05, 1.06194251052e-06) distributionPoints <- c(mu - 2 *sigma, mu - sigma, mu, mu + sigma, mu + 2 * sigma, mu + 3 * sigma, mu + 4 * sigma, mu + 5 * sigma) verifyDistribution(distributionPoints, distributionValues, mu, sigma, tol) verifyDensity(distributionPoints, densityValues, mu, sigma, tol) mu <- 0 sigma <- 1 distributionPoints <- c(mu - 2 *sigma, mu - sigma, mu, mu + sigma, mu + 2 * sigma, mu + 3 * sigma, mu + 4 * sigma, mu + 5 * sigma) densityValues <- c(0.0539909665132, 0.241970724519, 0.398942280401, 0.241970724519, 0.0539909665132, 0.00443184841194, 0.000133830225765, 1.48671951473e-06) verifyDistribution(distributionPoints, distributionValues, mu, sigma, tol) verifyDensity(distributionPoints, densityValues, mu, sigma, tol) mu <- 0 sigma <- 0.1 distributionPoints <- c(mu - 2 *sigma, mu - sigma, mu, mu + sigma, mu + 2 * sigma, mu + 3 * sigma, mu + 4 * sigma, mu + 5 * sigma) densityValues <- c(0.539909665132, 2.41970724519, 3.98942280401, 2.41970724519, 0.539909665132, 0.0443184841194, 0.00133830225765, 1.48671951473e-05) verifyDistribution(distributionPoints, distributionValues, mu, sigma, tol) verifyDensity(distributionPoints, densityValues, mu, sigma, tol) displayDashes(WIDTH) commons-math3-3.2-src/src/test/R/covarianceTestCases100644 1750 1750 14166 12126627667 21253 0ustarlucluc 0 0 # Licensed to the Apache Software Foundation (ASF) under one or more # contributor license agreements. See the NOTICE file distributed with # this work for additional information regarding copyright ownership. # The ASF licenses this file to You 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. # #------------------------------------------------------------------------------ # R source file to validate covariance tests in # org.apache.commons.math.stat.correlation.CovarianceTest # # To run the test, install R, put this file and testFunctions # into the same directory, launch R from this directory and then enter # source("") # #------------------------------------------------------------------------------ tol <- 1E-9 # error tolerance for tests #------------------------------------------------------------------------------ # Function definitions source("testFunctions") # utility test functions options(digits=16) # override number of digits displayed # function to verify covariance computations verifyCovariance <- function(matrix, expectedCovariance, name) { covariance <- cov(matrix) output <- c("Covariance test dataset = ", name) if (assertEquals(expectedCovariance,covariance,tol,"Covariances")) { displayPadded(output, SUCCEEDED, WIDTH) } else { displayPadded(output, FAILED, WIDTH) } } #-------------------------------------------------------------------------- cat("Covariance test cases\n") # Longley longley <- matrix(c(60323,83.0,234289,2356,1590,107608,1947, 61122,88.5,259426,2325,1456,108632,1948, 60171,88.2,258054,3682,1616,109773,1949, 61187,89.5,284599,3351,1650,110929,1950, 63221,96.2,328975,2099,3099,112075,1951, 63639,98.1,346999,1932,3594,113270,1952, 64989,99.0,365385,1870,3547,115094,1953, 63761,100.0,363112,3578,3350,116219,1954, 66019,101.2,397469,2904,3048,117388,1955, 67857,104.6,419180,2822,2857,118734,1956, 68169,108.4,442769,2936,2798,120445,1957, 66513,110.8,444546,4681,2637,121950,1958, 68655,112.6,482704,3813,2552,123366,1959, 69564,114.2,502601,3931,2514,125368,1960, 69331,115.7,518173,4806,2572,127852,1961, 70551,116.9,554894,4007,2827,130081,1962), nrow = 16, ncol = 7, byrow = TRUE) expectedCovariance <- matrix(c( 12333921.73333333246, 3.679666000000000e+04, 343330206.333333313, 1649102.666666666744, 1117681.066666666651, 23461965.733333334, 16240.93333333333248, 36796.66000000000, 1.164576250000000e+02, 1063604.115416667, 6258.666250000000, 3490.253750000000, 73503.000000000, 50.92333333333334, 343330206.33333331347, 1.063604115416667e+06, 9879353659.329166412, 56124369.854166664183, 30880428.345833335072, 685240944.600000024, 470977.90000000002328, 1649102.66666666674, 6.258666250000000e+03, 56124369.854166664, 873223.429166666698, -115378.762499999997, 4462741.533333333, 2973.03333333333330, 1117681.06666666665, 3.490253750000000e+03, 30880428.345833335, -115378.762499999997, 484304.095833333326, 1764098.133333333, 1382.43333333333339, 23461965.73333333433, 7.350300000000000e+04, 685240944.600000024, 4462741.533333333209, 1764098.133333333302, 48387348.933333330, 32917.40000000000146, 16240.93333333333, 5.092333333333334e+01, 470977.900000000, 2973.033333333333, 1382.433333333333, 32917.40000000, 22.66666666666667), nrow = 7, ncol = 7, byrow = TRUE) verifyCovariance(longley, expectedCovariance, "longley") # Swiss Fertility fertility <- matrix(c(80.2,17.0,15,12,9.96, 83.1,45.1,6,9,84.84, 92.5,39.7,5,5,93.40, 85.8,36.5,12,7,33.77, 76.9,43.5,17,15,5.16, 76.1,35.3,9,7,90.57, 83.8,70.2,16,7,92.85, 92.4,67.8,14,8,97.16, 82.4,53.3,12,7,97.67, 82.9,45.2,16,13,91.38, 87.1,64.5,14,6,98.61, 64.1,62.0,21,12,8.52, 66.9,67.5,14,7,2.27, 68.9,60.7,19,12,4.43, 61.7,69.3,22,5,2.82, 68.3,72.6,18,2,24.20, 71.7,34.0,17,8,3.30, 55.7,19.4,26,28,12.11, 54.3,15.2,31,20,2.15, 65.1,73.0,19,9,2.84, 65.5,59.8,22,10,5.23, 65.0,55.1,14,3,4.52, 56.6,50.9,22,12,15.14, 57.4,54.1,20,6,4.20, 72.5,71.2,12,1,2.40, 74.2,58.1,14,8,5.23, 72.0,63.5,6,3,2.56, 60.5,60.8,16,10,7.72, 58.3,26.8,25,19,18.46, 65.4,49.5,15,8,6.10, 75.5,85.9,3,2,99.71, 69.3,84.9,7,6,99.68, 77.3,89.7,5,2,100.00, 70.5,78.2,12,6,98.96, 79.4,64.9,7,3,98.22, 65.0,75.9,9,9,99.06, 92.2,84.6,3,3,99.46, 79.3,63.1,13,13,96.83, 70.4,38.4,26,12,5.62, 65.7,7.7,29,11,13.79, 72.7,16.7,22,13,11.22, 64.4,17.6,35,32,16.92, 77.6,37.6,15,7,4.97, 67.6,18.7,25,7,8.65, 35.0,1.2,37,53,42.34, 44.7,46.6,16,29,50.43, 42.8,27.7,22,29,58.33), nrow = 47, ncol = 5, byrow = TRUE) expectedCovariance <- matrix(c( 156.0424976873265, 100.1691489361702, -64.36692876965772, -79.7295097132285, 241.5632030527289, 100.169148936170251, 515.7994172062905, -124.39283071230344, -139.6574005550416, 379.9043755781684, -64.3669287696577, -124.3928307123034, 63.64662349676226, 53.5758556891767, -190.5606105457909, -79.7295097132285, -139.6574005550416, 53.57585568917669, 92.4560592044403, -61.6988297872340, 241.5632030527289, 379.9043755781684, -190.56061054579092, -61.6988297872340, 1739.2945371877890), nrow = 5, ncol = 5, byrow = TRUE) verifyCovariance(fertility, expectedCovariance, "swiss fertility") displayDashes(WIDTH)commons-math3-3.2-src/src/test/R/correlationTestCases100644 1750 1750 24031 12126627667 21452 0ustarlucluc 0 0 # Licensed to the Apache Software Foundation (ASF) under one or more # contributor license agreements. See the NOTICE file distributed with # this work for additional information regarding copyright ownership. # The ASF licenses this file to You 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. # #------------------------------------------------------------------------------ # R source file to validate Pearson's correlation tests in # org.apache.commons.math.stat.correlation.PearsonsCorrelationTest # # To run the test, install R, put this file and testFunctions # into the same directory, launch R from this directory and then enter # source("") # #------------------------------------------------------------------------------ tol <- 1E-15 # error tolerance for tests #------------------------------------------------------------------------------ # Function definitions source("testFunctions") # utility test functions options(digits=16) # override number of digits displayed # Verify Pearson's correlation verifyPearsonsCorrelation <- function(matrix, expectedCorrelation, name) { correlation <- cor(matrix) output <- c("Pearson's Correlation matrix test dataset = ", name) if (assertEquals(expectedCorrelation, correlation,tol,"Pearson's Correlations")) { displayPadded(output, SUCCEEDED, WIDTH) } else { displayPadded(output, FAILED, WIDTH) } } # Verify Spearman's correlation verifySpearmansCorrelation <- function(matrix, expectedCorrelation, name) { correlation <- cor(matrix, method="spearman") output <- c("Spearman's Correlation matrix test dataset = ", name) if (assertEquals(expectedCorrelation, correlation,tol,"Spearman's Correlations")) { displayPadded(output, SUCCEEDED, WIDTH) } else { displayPadded(output, FAILED, WIDTH) } } # function to verify p-values verifyPValues <- function(matrix, pValues, name) { dimension <- dim(matrix)[2] corValues <- matrix(nrow=dimension,ncol=dimension) expectedValues <- matrix(nrow=dimension,ncol=dimension) for (i in 2:dimension) { for (j in 1:(i-1)) { corValues[i,j]<-cor.test(matrix[,i], matrix[,j])$p.value corValues[j,i]<-corValues[i,j] } } for (i in 1:dimension) { corValues[i,i] <- 1 expectedValues[i,i] <- 1 } ptr <- 1 for (i in 2:dimension) { for (j in 1:(i-1)) { expectedValues[i,j] <- pValues[ptr] expectedValues[j,i] <- expectedValues[i,j] ptr <- ptr + 1 } } output <- c("Correlation p-values test dataset = ", name) if (assertEquals(expectedValues, corValues,tol,"p-values")) { displayPadded(output, SUCCEEDED, WIDTH) } else { displayPadded(output, FAILED, WIDTH) } } #-------------------------------------------------------------------------- cat("Correlation test cases\n") # Longley ----------------------------------------------------------------- longley <- matrix(c(60323,83.0,234289,2356,1590,107608,1947, 61122,88.5,259426,2325,1456,108632,1948, 60171,88.2,258054,3682,1616,109773,1949, 61187,89.5,284599,3351,1650,110929,1950, 63221,96.2,328975,2099,3099,112075,1951, 63639,98.1,346999,1932,3594,113270,1952, 64989,99.0,365385,1870,3547,115094,1953, 63761,100.0,363112,3578,3350,116219,1954, 66019,101.2,397469,2904,3048,117388,1955, 67857,104.6,419180,2822,2857,118734,1956, 68169,108.4,442769,2936,2798,120445,1957, 66513,110.8,444546,4681,2637,121950,1958, 68655,112.6,482704,3813,2552,123366,1959, 69564,114.2,502601,3931,2514,125368,1960, 69331,115.7,518173,4806,2572,127852,1961, 70551,116.9,554894,4007,2827,130081,1962), nrow = 16, ncol = 7, byrow = TRUE) # Pearson's expectedCorrelation <- matrix(c( 1.000000000000000, 0.9708985250610560, 0.9835516111796693, 0.5024980838759942, 0.4573073999764817, 0.960390571594376, 0.9713294591921188, 0.970898525061056, 1.0000000000000000, 0.9915891780247822, 0.6206333925590966, 0.4647441876006747, 0.979163432977498, 0.9911491900672053, 0.983551611179669, 0.9915891780247822, 1.0000000000000000, 0.6042609398895580, 0.4464367918926265, 0.991090069458478, 0.9952734837647849, 0.502498083875994, 0.6206333925590966, 0.6042609398895580, 1.0000000000000000, -0.1774206295018783, 0.686551516365312, 0.6682566045621746, 0.457307399976482, 0.4647441876006747, 0.4464367918926265, -0.1774206295018783, 1.0000000000000000, 0.364416267189032, 0.4172451498349454, 0.960390571594376, 0.9791634329774981, 0.9910900694584777, 0.6865515163653120, 0.3644162671890320, 1.000000000000000, 0.9939528462329257, 0.971329459192119, 0.9911491900672053, 0.9952734837647849, 0.6682566045621746, 0.4172451498349454, 0.993952846232926, 1.0000000000000000), nrow = 7, ncol = 7, byrow = TRUE) verifyPearsonsCorrelation(longley, expectedCorrelation, "longley") expectedPValues <- c( 4.38904690369668e-10, 8.36353208910623e-12, 7.8159700933611e-14, 0.0472894097790304, 0.01030636128354301, 0.01316878049026582, 0.0749178049642416, 0.06971758330341182, 0.0830166169296545, 0.510948586323452, 3.693245043123738e-09, 4.327782576751815e-11, 1.167954621905665e-13, 0.00331028281967516, 0.1652293725106684, 3.95834476307755e-10, 1.114663916723657e-13, 1.332267629550188e-15, 0.00466039138541463, 0.1078477071581498, 7.771561172376096e-15) verifyPValues(longley, expectedPValues, "longley") # Spearman's expectedCorrelation <- matrix(c( 1, 0.982352941176471, 0.985294117647059, 0.564705882352941, 0.2264705882352941, 0.976470588235294, 0.976470588235294, 0.982352941176471, 1, 0.997058823529412, 0.664705882352941, 0.2205882352941176, 0.997058823529412, 0.997058823529412, 0.985294117647059, 0.997058823529412, 1, 0.638235294117647, 0.2235294117647059, 0.9941176470588236, 0.9941176470588236, 0.564705882352941, 0.664705882352941, 0.638235294117647, 1, -0.3411764705882353, 0.685294117647059, 0.685294117647059, 0.2264705882352941, 0.2205882352941176, 0.2235294117647059, -0.3411764705882353, 1, 0.2264705882352941, 0.2264705882352941, 0.976470588235294, 0.997058823529412, 0.9941176470588236, 0.685294117647059, 0.2264705882352941, 1, 1, 0.976470588235294, 0.997058823529412, 0.9941176470588236, 0.685294117647059, 0.2264705882352941, 1, 1), nrow = 7, ncol = 7, byrow = TRUE) verifySpearmansCorrelation(longley, expectedCorrelation, "longley") # Swiss Fertility --------------------------------------------------------- fertility <- matrix(c(80.2,17.0,15,12,9.96, 83.1,45.1,6,9,84.84, 92.5,39.7,5,5,93.40, 85.8,36.5,12,7,33.77, 76.9,43.5,17,15,5.16, 76.1,35.3,9,7,90.57, 83.8,70.2,16,7,92.85, 92.4,67.8,14,8,97.16, 82.4,53.3,12,7,97.67, 82.9,45.2,16,13,91.38, 87.1,64.5,14,6,98.61, 64.1,62.0,21,12,8.52, 66.9,67.5,14,7,2.27, 68.9,60.7,19,12,4.43, 61.7,69.3,22,5,2.82, 68.3,72.6,18,2,24.20, 71.7,34.0,17,8,3.30, 55.7,19.4,26,28,12.11, 54.3,15.2,31,20,2.15, 65.1,73.0,19,9,2.84, 65.5,59.8,22,10,5.23, 65.0,55.1,14,3,4.52, 56.6,50.9,22,12,15.14, 57.4,54.1,20,6,4.20, 72.5,71.2,12,1,2.40, 74.2,58.1,14,8,5.23, 72.0,63.5,6,3,2.56, 60.5,60.8,16,10,7.72, 58.3,26.8,25,19,18.46, 65.4,49.5,15,8,6.10, 75.5,85.9,3,2,99.71, 69.3,84.9,7,6,99.68, 77.3,89.7,5,2,100.00, 70.5,78.2,12,6,98.96, 79.4,64.9,7,3,98.22, 65.0,75.9,9,9,99.06, 92.2,84.6,3,3,99.46, 79.3,63.1,13,13,96.83, 70.4,38.4,26,12,5.62, 65.7,7.7,29,11,13.79, 72.7,16.7,22,13,11.22, 64.4,17.6,35,32,16.92, 77.6,37.6,15,7,4.97, 67.6,18.7,25,7,8.65, 35.0,1.2,37,53,42.34, 44.7,46.6,16,29,50.43, 42.8,27.7,22,29,58.33), nrow = 47, ncol = 5, byrow = TRUE) # Pearson's expectedCorrelation <- matrix(c( 1, 0.3530791836199747, -0.6458827064572875, -0.663788857035069, 0.463684700651794, 0.3530791836199747, 1, -0.6865422086171366, -0.63952251894832, 0.4010950530487398, -0.6458827064572875, -0.6865422086171366, 1, 0.698415296288483, -0.572741806064167, -0.663788857035069, -0.63952251894832, 0.698415296288483, 1, -0.1538589170909148, 0.463684700651794, 0.4010950530487398, -0.572741806064167, -0.1538589170909148, 1), nrow = 5, ncol = 5, byrow = TRUE) verifyPearsonsCorrelation(fertility, expectedCorrelation, "swiss fertility") expectedPValues <- c( 0.01491720061472623, 9.45043734069043e-07, 9.95151527133974e-08, 3.658616965962355e-07, 1.304590105694471e-06, 4.811397236181847e-08, 0.001028523190118147, 0.005204433539191644, 2.588307925380906e-05, 0.301807756132683) verifyPValues(fertility, expectedPValues, "swiss fertility") # Spearman's expectedCorrelation <- matrix(c( 1, 0.2426642769364176, -0.660902996352354, -0.443257690360988, 0.4136455623012432, 0.2426642769364176, 1, -0.598859938748963, -0.650463814145816, 0.2886878090882852, -0.660902996352354, -0.598859938748963, 1, 0.674603831406147, -0.4750575257171745, -0.443257690360988, -0.650463814145816, 0.674603831406147, 1, -0.1444163088302244, 0.4136455623012432, 0.2886878090882852, -0.4750575257171745, -0.1444163088302244, 1), nrow = 5, ncol = 5, byrow = TRUE) verifySpearmansCorrelation(fertility, expectedCorrelation, "swiss fertility") displayDashes(WIDTH) commons-math3-3.2-src/src/test/R/binomialTestCases100644 1750 1750 11107 12126627667 20723 0ustarlucluc 0 0 # Licensed to the Apache Software Foundation (ASF) under one or more # contributor license agreements. See the NOTICE file distributed with # this work for additional information regarding copyright ownership. # The ASF licenses this file to You 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. # #------------------------------------------------------------------------------ # R source file to validate Binomial distribution tests in # org.apache.commons.math.distribution.BinomialDistributionTest # # To run the test, install R, put this file and testFunctions # into the same directory, launch R from this directory and then enter # source("") # # R functions used # dbinom(x, size, prob, log = FALSE) <- density # pbinom(q, size, prob, lower.tail = TRUE, log.p = FALSE) <- distribution # qbinom(p, size, prob, lower.tail = TRUE, log.p = FALSE) <- quantiles #------------------------------------------------------------------------------ tol <- 1E-4 # error tolerance for tests #------------------------------------------------------------------------------ # Function definitions source("testFunctions") # utility test functions # function to verify density computations verifyDensity <- function(points, expected, n, p, tol) { rDensityValues <- rep(0, length(points)) i <- 0 for (point in points) { i <- i + 1 rDensityValues[i] <- dbinom(point, n, p, log = FALSE) } output <- c("Density test n = ", n, ", p = ", p) if (assertEquals(expected,rDensityValues,tol,"Density Values")) { displayPadded(output, SUCCEEDED, WIDTH) } else { displayPadded(output, FAILED, WIDTH) } } # function to verify distribution computations verifyDistribution <- function(points, expected, n, p, tol) { rDistValues <- rep(0, length(points)) i <- 0 for (point in points) { i <- i + 1 rDistValues[i] <- pbinom(point, n, p, log = FALSE) } output <- c("Distribution test n = ", n, ", p = ", p) if (assertEquals(expected,rDistValues,tol,"Distribution Values")) { displayPadded(output, SUCCEEDED, WIDTH) } else { displayPadded(output, FAILED, WIDTH) } } #-------------------------------------------------------------------------- cat("Binomial test cases\n") size <- 10.0 probability <- 0.70 densityPoints <- c(-1,0,1,2,3,4,5,6,7,8,9,10,11) densityValues <- c(0, 0.0000, 0.0001, 0.0014, 0.0090, 0.0368, 0.1029, 0.2001, 0.2668, 0.2335, 0.1211, 0.0282, 0) distributionValues <- c(0, 0.0000, 0.0001, 0.0016, 0.0106, 0.0473, 0.1503, 0.3504, 0.6172, 0.8507, 0.9718, 1, 1) inverseCumPoints <- c( 0.001, 0.010, 0.025, 0.050, 0.100, 0.999, 0.990, 0.975, 0.950, 0.900) inverseCumValues <- c(1, 2, 3, 4, 4, 9, 9, 9, 8, 8) verifyDensity(densityPoints,densityValues,size,probability,tol) verifyDistribution(densityPoints, distributionValues, size, probability, tol) i <- 0 rInverseCumValues <- rep(0,length(inverseCumPoints)) for (point in inverseCumPoints) { i <- i + 1 rInverseCumValues[i] <- qbinom(point, size, probability, log = FALSE) } output <- c("Inverse Distribution test n = ", size, ", p = ", probability) # R defines quantiles from the right, need to subtract one if (assertEquals(inverseCumValues, rInverseCumValues-1, tol, "Inverse Dist Values")) { displayPadded(output, SUCCEEDED, 80) } else { displayPadded(output, FAILED, 80) } # Degenerate cases size <- 5 probability <- 0.0 densityPoints <- c(-1, 0, 1, 10, 11) densityValues <- c(0, 1, 0, 0, 0) distributionPoints <- c(-1, 0, 1, 5, 10) distributionValues <- c(0, 1, 1, 1, 1) verifyDensity(densityPoints,densityValues,size,probability,tol) verifyDistribution(distributionPoints,distributionValues,size,probability,tol) size <- 5 probability <- 1.0 densityPoints <- c(-1, 0, 1, 2, 5, 10) densityValues <- c(0, 0, 0, 0, 1, 0) distributionPoints <- c(-1, 0, 1, 2, 5, 10) distributionValues <- c(0, 0, 0, 0, 1, 1) verifyDensity(densityPoints,densityValues,size,probability,tol) verifyDistribution(distributionPoints,distributionValues,size,probability,tol) displayDashes(WIDTH) commons-math3-3.2-src/src/test/R/chiSquareTestCases100644 1750 1750 7624 12126627667 21046 0ustarlucluc 0 0 # Licensed to the Apache Software Foundation (ASF) under one or more # contributor license agreements. See the NOTICE file distributed with # this work for additional information regarding copyright ownership. # The ASF licenses this file to You 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. # #------------------------------------------------------------------------------ # R source file to validate ChiSquare tests in # org.apache.commons.math.stat.inference.ChiSquareTestTest # # To run the test, install R, put this file and testFunctions # into the same directory, launch R from this directory and then enter # source("") # # R functions used #chisq.test(x, y = NULL, correct = TRUE, # p = rep(1/length(x), length(x)), # simulate.p.value = FALSE, B = 2000) #------------------------------------------------------------------------------ tol <- 1E-9 # error tolerance for tests #------------------------------------------------------------------------------ # Function definitions source("testFunctions") # utility test functions verifyTable <- function(counts, expectedP, expectedStat, tol, desc) { results <- chisq.test(counts) if (assertEquals(expectedP, results$p.value, tol, "p-value")) { displayPadded(c(desc," p-value test"), SUCCEEDED, WIDTH) } else { displayPadded(c(desc, " p-value test"), FAILED, WIDTH) } if (assertEquals(expectedStat, results$statistic, tol, "ChiSquare Statistic")) { displayPadded(c(desc, " chi-square statistic test"), SUCCEEDED, WIDTH) } else { displayPadded(c(desc, " chi-square statistic test"), FAILED, WIDTH) } } verifyHomogeneity <- function(obs, exp, expectedP, expectedStat, tol, desc) { results <- chisq.test(obs,p=exp,rescale.p=TRUE) chi <- results$statistic p <- results$p.value if (assertEquals(expectedP, p, tol, "p-value")) { displayPadded(c(desc, " p-value test"), SUCCEEDED, WIDTH) } else { displayPadded(c(desc, " p-value test"), FAILED, WIDTH) } if (assertEquals(expectedStat, chi, tol, "ChiSquare Statistic")) { displayPadded(c(desc, " chi-square statistic test"), SUCCEEDED, WIDTH) } else { displayPadded(c(desc, " chi-square statistic test"), FAILED, WIDTH) } } cat("ChiSquareTest test cases\n") observed <- c(10, 9, 11) expected <- c(10, 10, 10) verifyHomogeneity(observed, expected, 0.904837418036, 0.2, tol, "testChiSquare1") observed <- c(500, 623, 72, 70, 31) expected <- c(485, 541, 82, 61, 37) verifyHomogeneity(observed, expected, 0.06051952647453607, 9.023307936427388, tol, "testChiSquare2") observed <- c(2372383, 584222, 257170, 17750155, 7903832, 489265, 209628, 393899) expected <- c(3389119.5, 649136.6, 285745.4, 25357364.76, 11291189.78, 543628.0, 232921.0, 437665.75) verifyHomogeneity(observed, expected, 0, 114875.90421929007, tol, "testChiSquareLargeTestStatistic") counts <- matrix(c(40, 22, 43, 91, 21, 28, 60, 10, 22), nc = 3); verifyTable(counts, 0.000144751460134, 22.709027688, tol, "testChiSquareIndependence1") counts <- matrix(c(10, 15, 30, 40, 60, 90), nc = 3); verifyTable(counts, 0.918987499852, 0.168965517241, tol, "testChiSquareIndependence2") counts <- matrix(c(40, 0, 4, 91, 1, 2, 60, 2, 0), nc = 3); verifyTable(counts, 0.0462835770603, 9.67444662263, tol, "testChiSquareZeroCount") displayDashes(WIDTH) commons-math3-3.2-src/src/test/R/WeibullDistributionTestCases.R100644 1750 1750 7472 12126627667 23326 0ustarlucluc 0 0 # Licensed to the Apache Software Foundation (ASF) under one or more # contributor license agreements. See the NOTICE file distributed with # this work for additional information regarding copyright ownership. # The ASF licenses this file to You 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. # #------------------------------------------------------------------------------ # R source file to validate Weibull distribution tests in # org.apache.commons.math.distribution.GammaDistributionTest # # To run the test, install R, put this file and testFunctions # into the same directory, launch R from this directory and then enter # source("") # #----------------------------------------------------------------------------- tol <- 1E-9 # Function definitions source("testFunctions") # utility test functions # function to verify distribution computations verifyDistribution <- function(points, expected, alpha, beta, tol) { rDistValues <- rep(0, length(points)) i <- 0 for (point in points) { i <- i + 1 rDistValues[i] <- pweibull(point, shape=alpha, scale=beta, log = FALSE) } output <- c("Distribution test shape = ", shape, " scale = ", scale) if (assertEquals(expected, rDistValues, tol, "Distribution Values")) { displayPadded(output, SUCCEEDED, WIDTH) } else { displayPadded(output, FAILED, WIDTH) } } # function to verify density computations verifyDensity <- function(points, expected, alpha, beta, tol) { rDensityValues <- rep(0, length(points)) i <- 0 for (point in points) { i <- i + 1 rDensityValues[i] <- dweibull(point, shape=alpha, scale=beta, log = FALSE) } output <- c("Density test shape = ", shape, " scale = ", scale) if (assertEquals(expected, rDensityValues, tol, "Density Values")) { displayPadded(output, SUCCEEDED, WIDTH) } else { displayPadded(output, FAILED, WIDTH) } } # function to verify quantiles verifyQuantiles <- function(points, expected, alpha, beta, tol) { rQuantileValues <- rep(0, length(points)) i <- 0 for (point in points) { i <- i + 1 rQuantileValues[i] <- qweibull(point, shape=alpha, scale=beta, log = FALSE) } output <- c("Quantile test shape = ", shape, " scale = ", scale) if (assertEquals(expected, rQuantileValues, tol, "Quantile Values")) { displayPadded(output, SUCCEEDED, WIDTH) } else { displayPadded(output, FAILED, WIDTH) } } #-------------------------------------------------------------------------- cat("Weibull Distribution test cases\n") shape <- 1.2 scale <- 2.1 distributionValues <- c(0.001, 0.01, 0.025, 0.05, 0.1, 0.999, 0.990, 0.975, 0.950, 0.900) densityValues <- c(0.180535929306, 0.262801138133, 0.301905425199, 0.330899152971, 0.353441418887, 0.000788590320203, 0.00737060094841, 0.0177576041516, 0.0343043442574, 0.065664589369) distributionPoints <- c(0.00664355180993, 0.0454328283309, 0.0981162737374, 0.176713524579, 0.321946865392, 10.5115496887, 7.4976304671, 6.23205600701, 5.23968436955, 4.20790282578) verifyQuantiles(distributionValues, distributionPoints, shape, scale, tol) verifyDistribution(distributionPoints, distributionValues, shape, scale, tol) verifyDensity(distributionPoints, densityValues, shape, scale, tol) displayDashes(WIDTH) commons-math3-3.2-src/src/test/R/TDistributionTestCases.R100644 1750 1750 10244 12126627667 22135 0ustarlucluc 0 0 # Licensed to the Apache Software Foundation (ASF) under one or more # contributor license agreements. See the NOTICE file distributed with # this work for additional information regarding copyright ownership. # The ASF licenses this file to You 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. # #------------------------------------------------------------------------------ # R source file to validate T distribution tests in # org.apache.commons.math.distribution.TDistributionTest # # To run the test, install R, put this file and testFunctions # into the same directory, launch R from this directory and then enter # source("") # #----------------------------------------------------------------------------- tol <- 1E-9 # Function definitions source("testFunctions") # utility test functions # function to verify distribution computations verifyDistribution <- function(points, expected, df, tol) { rDistValues <- rep(0, length(points)) i <- 0 for (point in points) { i <- i + 1 rDistValues[i] <- pt(point, df, log = FALSE) } output <- c("Distribution test df = ", df) if (assertEquals(expected, rDistValues, tol, "Distribution Values")) { displayPadded(output, SUCCEEDED, WIDTH) } else { displayPadded(output, FAILED, WIDTH) } } # function to verify density computations verifyDensity <- function(points, expected, df, tol) { rDensityValues <- rep(0, length(points)) i <- 0 for (point in points) { i <- i + 1 rDensityValues[i] <- dt(point, df, log = FALSE) } output <- c("Density test df = ", df) if (assertEquals(expected, rDensityValues, tol, "Density Values")) { displayPadded(output, SUCCEEDED, WIDTH) } else { displayPadded(output, FAILED, WIDTH) } } # function to verify quantiles verifyQuantiles <- function(points, expected, df, tol) { rQuantileValues <- rep(0, length(points)) i <- 0 for (point in points) { i <- i + 1 rQuantileValues[i] <- qt(point, df, log = FALSE) } output <- c("Quantile test df = ", df) if (assertEquals(expected, rQuantileValues, tol, "Quantile Values")) { displayPadded(output, SUCCEEDED, WIDTH) } else { displayPadded(output, FAILED, WIDTH) } } #-------------------------------------------------------------------------- cat("T Distribution test cases\n") df <- 5 distributionValues <- c(0.001, 0.01, 0.025, 0.05, 0.1, 0.999, 0.990, 0.975, 0.950, 0.900) densityValues <- c(0.000756494565517, 0.0109109752919, 0.0303377878006, 0.0637967988952, 0.128289492005, 0.000756494565517, 0.0109109752919, 0.0303377878006, 0.0637967988952, 0.128289492005) distributionPoints <- c(-5.89342953136, -3.36492999891, -2.57058183564, -2.01504837333, -1.47588404882, 5.89342953136, 3.36492999891, 2.57058183564, 2.01504837333, 1.47588404882) verifyQuantiles(distributionValues, distributionPoints, df, tol) verifyDistribution(distributionPoints, distributionValues, df, tol) verifyDensity(distributionPoints, densityValues, df, tol) df <- 1 densityValues <- c(3.14158231817e-06, 0.000314055924703, 0.00195946145194, 0.00778959736375, 0.0303958893917, 3.14158231817e-06, 0.000314055924703, 0.00195946145194, 0.00778959736375, 0.0303958893917) distributionPoints <- c(-318.308838986, -31.8205159538, -12.7062047362, -6.31375151468, -3.07768353718, 318.308838986, 31.8205159538, 12.7062047362, 6.31375151468, 3.07768353718) verifyQuantiles(distributionValues, distributionPoints, df, tol) verifyDistribution(distributionPoints, distributionValues, df, tol) verifyDensity(distributionPoints, densityValues, df, tol) displayDashes(WIDTH) commons-math3-3.2-src/src/test/R/FDistributionTestCases.R100644 1750 1750 7735 12126627667 22112 0ustarlucluc 0 0 # Licensed to the Apache Software Foundation (ASF) under one or more # contributor license agreements. See the NOTICE file distributed with # this work for additional information regarding copyright ownership. # The ASF licenses this file to You 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. # #------------------------------------------------------------------------------ # R source file to validate F distribution tests in # org.apache.commons.math.distribution.TDistributionTest # # To run the test, install R, put this file and testFunctions # into the same directory, launch R from this directory and then enter # source("") # #----------------------------------------------------------------------------- tol <- 1E-9 # Function definitions source("testFunctions") # utility test functions # function to verify distribution computations verifyDistribution <- function(points, expected, numeratorDf, denominatorDf, tol) { rDistValues <- rep(0, length(points)) i <- 0 for (point in points) { i <- i + 1 rDistValues[i] <- pf(point, numeratorDf, denominatorDf, log = FALSE) } output <- c("Distribution test numerator df = ", numeratorDf, " denominator df = ", denominatorDf) if (assertEquals(expected, rDistValues, tol, "Distribution Values")) { displayPadded(output, SUCCEEDED, WIDTH) } else { displayPadded(output, FAILED, WIDTH) } } # function to verify density computations verifyDensity <- function(points, expected, numeratorDf, denominatorDf, tol) { rDensityValues <- rep(0, length(points)) i <- 0 for (point in points) { i <- i + 1 rDensityValues[i] <- df(point, numeratorDf, denominatorDf, log = FALSE) } output <- c("Density test numerator df = ", numeratorDf, " denominator df = ", denominatorDf) if (assertEquals(expected, rDensityValues, tol, "Density Values")) { displayPadded(output, SUCCEEDED, WIDTH) } else { displayPadded(output, FAILED, WIDTH) } } # function to verify quantiles verifyQuantiles <- function(points, expected, numeratorDf, denominatorDf, tol) { rQuantileValues <- rep(0, length(points)) i <- 0 for (point in points) { i <- i + 1 rQuantileValues[i] <- qf(point, numeratorDf, denominatorDf, log = FALSE) } output <- c("Quantile test numerator df = ", numeratorDf, " denominator df = ", denominatorDf) if (assertEquals(expected, rQuantileValues, tol, "Quantile Values")) { displayPadded(output, SUCCEEDED, WIDTH) } else { displayPadded(output, FAILED, WIDTH) } } #-------------------------------------------------------------------------- cat("F Distribution test cases\n") numeratorDf <- 5 denominatorDf <- 6 distributionValues <- c(0.001, 0.01, 0.025, 0.05, 0.1, 0.999, 0.990, 0.975, 0.950, 0.900) densityValues <- c(0.0689156576706, 0.236735653193, 0.364074131941, 0.481570789649, 0.595880479994, 0.000133443915657, 0.00286681303403, 0.00969192007502, 0.0242883861471, 0.0605491314658) distributionPoints <- c(0.0346808448626, 0.0937009113303, 0.143313661184, 0.202008445998, 0.293728320107, 20.8026639595, 8.74589525602, 5.98756512605, 4.38737418741, 3.10751166664) verifyQuantiles(distributionValues, distributionPoints, numeratorDf, denominatorDf, tol) verifyDistribution(distributionPoints, distributionValues, numeratorDf, denominatorDf, tol) verifyDensity(distributionPoints, densityValues, numeratorDf, denominatorDf, tol) displayDashes(WIDTH) commons-math3-3.2-src/src/test/R/regressionTestCases100644 1750 1750 13200 12126627667 21305 0ustarlucluc 0 0 # Licensed to the Apache Software Foundation (ASF) under one or more # contributor license agreements. See the NOTICE file distributed with # this work for additional information regarding copyright ownership. # The ASF licenses this file to You 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. # #----------------------------------------------------------------------- # R source file to validate Binomial distribution tests in # org.apache.commons.math.stat.regression.SimpleRegressionTest # # To run the test, install R, put this file and testFunctions # into the same directory, launch R from this directory and then enter # source("") # # Output will be written to a file named "regTestResults" # in the directory from which R was launched # #------------------------------------------------------------------------------ tol <- 1E-8 #------------------------------------------------------------------------------ # Function definitions source("testFunctions") # utility test functions #------------------------------------------------------------------------------ # infData example cat("Regresssion test cases\n") x <- c(15.6, 26.8,37.8,36.4,35.5,18.6,15.3,7.9,0.0) y <- c(5.2, 6.1, 8.7, 8.5, 8.8, 4.9, 4.5, 2.5, 1.1) model<-lm(y~x) coef <- coefficients(summary(model)) intercept <- coef[1, 1] interceptStd <- coef[1, 2] slope <- coef[2, 1] slopeStd <- coef[2, 2] significance <- coef[2, 4] output <- "InfData std error test" if (assertEquals(0.011448491, slopeStd, tol, "Slope Standard Error") && assertEquals(0.286036932, interceptStd, tol, "Intercept Standard Error")) { displayPadded(output, SUCCEEDED, WIDTH) } else { displayPadded(output, FAILED, WIDTH) } output <- "InfData significance test" if (assertEquals(4.596e-07, significance, tol, "Significance")) { displayPadded(output, SUCCEEDED, WIDTH) } else { displayPadded(output, FAILED, WIDTH) } output <- "InfData conf interval test" ci<-confint(model) # ci[1,1] = lower 2.5% bound for intercept, ci[1,2] = upper 97.5% for intercept # ci[2,1] = lower 2.5% bound for slope, ci[2,2] = upper 97.5% for slope halfWidth <- ci[2,2] - slope if (assertEquals(0.0270713794287, halfWidth, tol, "Slope conf. interval half-width")) { displayPadded(output, SUCCEEDED, WIDTH) } else { displayPadded(output, FAILED, WIDTH) } #------------------------------------------------------------------------------ # Norris dataset from NIST examples y <- c(0.1, 338.8, 118.1, 888.0, 9.2, 228.1, 668.5, 998.5, 449.1, 778.9, 559.2, 0.3, 0.1, 778.1, 668.8, 339.3, 448.9, 10.8, 557.7, 228.3, 998.0, 888.8, 119.6, 0.3, 0.6, 557.6, 339.3, 888.0, 998.5, 778.9, 10.2, 117.6, 228.9, 668.4, 449.2, 0.2) x <- c(0.2, 337.4, 118.2, 884.6, 10.1, 226.5, 666.3, 996.3, 448.6, 777.0, 558.2, 0.4, 0.6, 775.5, 666.9, 338.0, 447.5, 11.6, 556.0, 228.1, 995.8, 887.6, 120.2, 0.3, 0.3, 556.8, 339.1, 887.2, 999.0, 779.0, 11.1, 118.3, 229.2, 669.1, 448.9, 0.5) model<-lm(y~x) coef <- coefficients(summary(model)) intercept <- coef[1, 1] interceptStd <- coef[1, 2] slope <- coef[2, 1] slopeStd <- coef[2, 2] output <- "Norris std error test" if (assertEquals(0.429796848199937E-03, slopeStd, tol, "Slope Standard Error") && assertEquals(0.232818234301152, interceptStd, tol, "Intercept Standard Error")) { displayPadded(output, SUCCEEDED, WIDTH) } else { displayPadded(output, FAILED, WIDTH) } #------------------------------------------------------------------------------ # infData2 -- bad fit example # x <- c(1,2,3,4,5,6) y <- c(1,0,5,2,-1,12) model<-lm(y~x) coef <- coefficients(summary(model)) intercept <- coef[1, 1] interceptStd <- coef[1, 2] slope <- coef[2, 1] slopeStd <- coef[2, 2] significance <- coef[2, 4] output <- "InfData2 std error test" if (assertEquals(1.07260253, slopeStd, tol, "Slope Standard Error") && assertEquals(4.17718672, interceptStd, tol, "Intercept Standard Error")) { displayPadded(output, SUCCEEDED, WIDTH) } else { displayPadded(output, FAILED, WIDTH) } output <- "InfData2 significance test" if (assertEquals(0.261829133982, significance, tol, "Significance")) { displayPadded(output, SUCCEEDED, WIDTH) } else { displayPadded(output, FAILED, WIDTH) } output <- "InfData2 conf interval test" ci<-confint(model) # ci[1,1] = lower 2.5% bound for intercept, ci[1,2] = upper 97.5% for intercept # ci[2,1] = lower 2.5% bound for slope, ci[2,2] = upper 97.5% for slope halfWidth <- ci[2,2] - slope if (assertEquals(2.97802204827, halfWidth, tol, "Slope conf. interval half-width")) { displayPadded(output, SUCCEEDED, WIDTH) } else { displayPadded(output, FAILED, WIDTH) } #------------------------------------------------------------------------------ # Correlation example x <- c(101.0, 100.1, 100.0, 90.6, 86.5, 89.7, 90.6, 82.8, 70.1, 65.4, 61.3, 62.5, 63.6, 52.6, 59.7, 59.5, 61.3) y <- c(99.2, 99.0, 100.0, 111.6, 122.2, 117.6, 121.1, 136.0, 154.2, 153.6, 158.5, 140.6, 136.2, 168.0, 154.3, 149.0, 165.5) output <- "Correlation test" if (assertEquals(-0.94663767742, cor(x,y, method="pearson"), tol, "Correlation coefficient")) { displayPadded(output, SUCCEEDED, WIDTH) } else { displayPadded(output, FAILED, WIDTH) } displayDashes(WIDTH) commons-math3-3.2-src/src/test/R/testFunctions100644 1750 1750 6366 12126627667 20155 0ustarlucluc 0 0 # Licensed to the Apache Software Foundation (ASF) under one or more # contributor license agreements. See the NOTICE file distributed with # this work for additional information regarding copyright ownership. # The ASF licenses this file to You 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. # #------------------------------------------------------------------------------ # # Utility functions used in R comparison tests. # #------------------------------------------------------------------------------ # Global constants #------------------------------------------------------------------------------ WIDTH <- 80 # screen size constant for display functions SUCCEEDED <- "SUCCEEDED" FAILED <- "FAILED" options(digits=12) # display 12 digits throughout #------------------------------------------------------------------------------ # Comparison functions #------------------------------------------------------------------------------ # Tests to see if and are within of # one another in the sup norm. # # Returns 1 if no pair of corresponding non-NULL, non-NaN, non-na entries # differs by more than abs and NULLs, NaNs, na's correspond; # otherwise displays and returns 0. # Works for both vectors and scalar values. # assertEquals <- function(expected, observed, tol, message) { failed <- 0 if (any(is.na(observed) != is.na(expected))) { failed <- 1 } if (any(is.null(observed) != is.null(expected))) { failed <- 1 } if (any(is.nan(expected) != is.nan(observed))) { failed <- 1 } if (any(is.na(expected) != is.na(observed))) { failed <- 1 } if (!failed) { if(any(abs(observed - expected) > tol, na.rm = TRUE)) { failed <- 1 } } if (failed) { cat("FAILURE: ",message,"\n") cat("EXPECTED: ",expected,"\n") cat("OBSERVED: ",observed,"\n") cat("DIFF: ",observed - expected,"\n") cat("TOLERANCE: ",tol,"\n") } return(!failed) } #------------------------------------------------------------------------------ # Display functions #------------------------------------------------------------------------------ # Displays n-col dashed line. # displayDashes <- function(n) { cat(rep("-",n),"\n",sep='') return(1) } #------------------------------------------------------------------------------ # Displays ...... with enough dots in between to make cols, # followed by a new line character. Blows up if is longer than # cols by itself. # # Expects and to be strings (character vectors). # displayPadded <- function(start, end, n) { len = sum(nchar(start)) + sum(nchar(end)) cat(start, rep(".", n - len), end, "\n",sep='') return(1) } commons-math3-3.2-src/src/test/R/poissonTestCases100644 1750 1750 10212 12126627667 20617 0ustarlucluc 0 0 # Licensed to the Apache Software Foundation (ASF) under one or more # contributor license agreements. See the NOTICE file distributed with # this work for additional information regarding copyright ownership. # The ASF licenses this file to You 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. # #------------------------------------------------------------------------------ # R source file to validate Poisson distribution tests in # org.apache.commons.math.distribution.PoissonDistributionTest # # To run the test, install R, put this file and testFunctions # into the same directory, launch R from this directory and then enter # source("") # # R functions used # dpois(x, lambda, log = FALSE) <-- density # ppois(q, lambda, lower.tail = TRUE, log.p = FALSE) <-- distribution # pnorm(q, mean=0, sd=1, lower.tail = TRUE, log.p = FALSE) <-- normal dist. #------------------------------------------------------------------------------ tol <- 1E-10 #------------------------------------------------------------------------------ # Function definitions source("testFunctions") # utility test functions # function to verify density computations verifyDensity <- function(points, expected, lambda, tol) { rDensityValues <- rep(0, length(points)) i <- 0 for (point in points) { i <- i + 1 rDensityValues[i] <- dpois(point, lambda, log = FALSE) } output <- c("Density test lambda = ", lambda) if (assertEquals(expected, rDensityValues, tol, "Density Values")) { displayPadded(output, SUCCEEDED, WIDTH) } else { displayPadded(output, FAILED, WIDTH) } } # function to verify distribution computations verifyDistribution <- function(points, expected, lambda, tol) { rDistValues <- rep(0, length(points)) i <- 0 for (point in points) { i <- i + 1 rDistValues[i] <- ppois(point, lambda, log = FALSE) } output <- c("Distribution test lambda = ", lambda) if (assertEquals(expected, rDistValues, tol, "Distribution Values")) { displayPadded(output, SUCCEEDED, WIDTH) } else { displayPadded(output, FAILED, WIDTH) } } # function to verify normal approximation verifyNormalApproximation <- function(expected, lambda, lower, upper, tol) { rValue <- pnorm(upper, mean=lambda, sd=sqrt(lambda), lower.tail = TRUE, log.p = FALSE) - pnorm(lower, mean=lambda, sd=sqrt(lambda), lower.tail = TRUE, log.p = FALSE) output <- c("Normal approx. test lambda = ", lambda, " upper = ", upper, " lower = ", lower) if (assertEquals(expected, rValue, tol, "Distribution Values")) { displayPadded(output, SUCCEEDED, WIDTH) } else { displayPadded(output, FAILED, WIDTH) } } cat("Poisson distribution test cases\n") # stock tests lambda <- 4.0 densityPoints <- c(-1,0,1,2,3,4,5,10,20) densityValues <- c(0, 0.0183156388887, 0.073262555555, 0.14652511111, 0.195366814813, 0.195366814813, 0.156293451851, 0.00529247667642, 8.27746364655e-09) verifyDensity(densityPoints, densityValues, lambda, tol) distributionPoints <- c(-1, 0, 1, 2, 3, 4, 5, 10, 20) distributionValues <- c(0, 0.0183156388887, 0.0915781944437, 0.238103305554, 0.433470120367, 0.62883693518, 0.78513038703, 0.99716023388, 0.999999998077) verifyDistribution(distributionPoints, distributionValues, lambda, tol) # normal approximation tests lambda <- 100 verifyNormalApproximation(0.706281887248, lambda, 89.5, 110.5, tol) lambda <- 10000 verifyNormalApproximation(0.820070051552, lambda, 9899.5, 10200.5, tol) displayDashes(WIDTH) commons-math3-3.2-src/src/test/R/anovaTestCases100644 1750 1750 5631 12126627667 20222 0ustarlucluc 0 0 # Licensed to the Apache Software Foundation (ASF) under one or more # contributor license agreements. See the NOTICE file distributed with # this work for additional information regarding copyright ownership. # The ASF licenses this file to You 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. # #------------------------------------------------------------------------------ # R source file to validate Binomial distribution tests in # org.apache.commons.math.distribution.BinomialDistributionTest # # To run the test, install R, put this file and testFunctions # into the same directory, launch R from this directory and then enter # source("") # # R functions used # anova(model) <- anova # lm(frame) <- linear model #------------------------------------------------------------------------------ tol <- 1E-12 # error tolerance for tests #------------------------------------------------------------------------------ # Function definitions source("testFunctions") # utility test functions options(digits=16) # override number of digits displayed # function to verify anova computations verifyAnova <- function(frame, expectedP, expectedF, frameName) { a <- anova(lm(frame)) p <- a$"Pr(>F)"[1] f <- a$"F value"[1] output <- c("P-value test frame = ", frameName) if (assertEquals(expectedP,p,tol,"P value")) { displayPadded(output, SUCCEEDED, WIDTH) } else { displayPadded(output, FAILED, WIDTH) } output <- c("F-value test frame = ", frameName) if (assertEquals(expectedF,f,tol,"F value")) { displayPadded(output, SUCCEEDED, WIDTH) } else { displayPadded(output, FAILED, WIDTH) } } #-------------------------------------------------------------------------- cat("Anova test cases\n") classA <- c(93.0, 103.0, 95.0, 101.0, 91.0, 105.0, 96.0, 94.0, 101.0) classB <- c(99.0, 92.0, 102.0, 100.0, 102.0, 89.0) classC <- c(110.0, 115.0, 111.0, 117.0, 128.0, 117.0) threeClasses = data.frame(val = c(classA, classB, classC), class=c(rep("classA", length(classA)), rep("classB", length(classB)), rep("classC", length(classC)))) verifyAnova(threeClasses,6.959446e-06, 24.67361709460624, "Three classes") twoClasses = data.frame(val = c(classA, classB), class=c(rep("classA", length(classA)), rep("classB", length(classB)))) verifyAnova(twoClasses, 0.904212960464, 0.0150579150579, "Two classes") displayDashes(WIDTH)commons-math3-3.2-src/src/test/R/ChiSquareDistributionTestCases.R100644 1750 1750 7707 12126627667 23610 0ustarlucluc 0 0 # Licensed to the Apache Software Foundation (ASF) under one or more # contributor license agreements. See the NOTICE file distributed with # this work for additional information regarding copyright ownership. # The ASF licenses this file to You 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. # #------------------------------------------------------------------------------ # R source file to validate ChiSquare distribution tests in # org.apache.commons.math.distribution.ChiSquareDistributionTest # # To run the test, install R, put this file and testFunctions # into the same directory, launch R from this directory and then enter # source("") # #----------------------------------------------------------------------------- tol <- 1E-9 # Function definitions source("testFunctions") # utility test functions # function to verify distribution computations verifyDistribution <- function(points, expected, df, tol) { rDistValues <- rep(0, length(points)) i <- 0 for (point in points) { i <- i + 1 rDistValues[i] <- pchisq(point, df, log = FALSE) } output <- c("Distribution test df = ", df) if (assertEquals(expected, rDistValues, tol, "Distribution Values")) { displayPadded(output, SUCCEEDED, WIDTH) } else { displayPadded(output, FAILED, WIDTH) } } # function to verify density computations verifyDensity <- function(points, expected, df, tol) { rDensityValues <- rep(0, length(points)) i <- 0 for (point in points) { i <- i + 1 rDensityValues[i] <- dchisq(point, df, log = FALSE) } output <- c("Density test df = ", df) if (assertEquals(expected, rDensityValues, tol, "Density Values")) { displayPadded(output, SUCCEEDED, WIDTH) } else { displayPadded(output, FAILED, WIDTH) } } # function to verify quantiles verifyQuantiles <- function(points, expected, df, tol) { rQuantileValues <- rep(0, length(points)) i <- 0 for (point in points) { i <- i + 1 rQuantileValues[i] <- qchisq(point, df, log = FALSE) } output <- c("Quantile test df = ", df) if (assertEquals(expected, rQuantileValues, tol, "Quantile Values")) { displayPadded(output, SUCCEEDED, WIDTH) } else { displayPadded(output, FAILED, WIDTH) } } #-------------------------------------------------------------------------- cat("ChiSquare Distribution test cases\n") df <- 5 distributionValues <- c(0.001, 0.01, 0.025, 0.05, 0.1, 0.999, 0.990, 0.975, 0.950, 0.900) densityValues <- c(0.0115379817652, 0.0415948507811, 0.0665060119842, 0.0919455953114, 0.121472591024, 0.000433630076361, 0.00412780610309, 0.00999340341045, 0.0193246438937, 0.0368460089216) distributionPoints <- c(0.210212602629, 0.554298076728, 0.831211613487, 1.14547622606, 1.61030798696, 20.5150056524, 15.0862724694, 12.8325019940, 11.0704976935, 9.23635689978) verifyQuantiles(distributionValues, distributionPoints, df, tol) verifyDistribution(distributionPoints, distributionValues, df, tol) verifyDensity(distributionPoints, densityValues, df, tol) df <- .1 distributionPoints <- c(1.16892641146e-60, 1.16892641146e-40, 1.06313237798e-32, 1.11477509638e-26, 1.16892641146e-20, 5.47291719746, 2.17525480018, 1.13434752351, 0.531864604852, 0.152634227818) verifyQuantiles(distributionValues, distributionPoints, df, tol) verifyDistribution(distributionPoints, distributionValues, df, tol) displayDashes(WIDTH) commons-math3-3.2-src/src/test/R/hypergeometricTestCases100644 1750 1750 11756 12126627667 22171 0ustarlucluc 0 0 # Licensed to the Apache Software Foundation (ASF) under one or more # contributor license agreements. See the NOTICE file distributed with # this work for additional information regarding copyright ownership. # The ASF licenses this file to You 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. # #------------------------------------------------------------------------------ # R source file to validate Hypergeometric distribution tests in # org.apache.commons.math.distribution.HypergeometricDistributionTest # # To run the test, install R, put this file and testFunctions # into the same directory, launch R from this directory and then enter # source("") # # R functions used # dhyper(x, m, n, k, log = FALSE) <- density # phyper(q, m, n, k, lower.tail = TRUE, log.p = FALSE) <- distribution # qhyper(p, m, n, k, lower.tail = TRUE, log.p = FALSE) <- quantiles #------------------------------------------------------------------------------ tol <- 1E-6 # error tolerance for tests #------------------------------------------------------------------------------ # Function definitions source("testFunctions") # utility test functions # function to verify density computations verifyDensity <- function(points, expected, good, bad, selected, tol) { rDensityValues <- rep(0, length(points)) i <- 0 for (point in points) { i <- i + 1 rDensityValues[i] <- dhyper(point, good, bad, selected) } output <- c("Density test good = ", good, ", bad = ", bad, ", selected = ",selected) if (assertEquals(expected,rDensityValues,tol,"Density Values")) { displayPadded(output, SUCCEEDED, WIDTH) } else { displayPadded(output, FAILED, WIDTH) } } # function to verify distribution computations verifyDistribution <- function(points, expected, good, bad, selected, tol) { rDistValues <- rep(0, length(points)) i <- 0 for (point in points) { i <- i + 1 rDistValues[i] <- phyper(point, good, bad, selected) } output <- c("Distribution test good = ", good, ", bad = ", bad, ", selected = ",selected) if (assertEquals(expected,rDistValues,tol,"Distribution Values")) { displayPadded(output, SUCCEEDED, WIDTH) } else { displayPadded(output, FAILED, WIDTH) } } #-------------------------------------------------------------------------- cat("Hypergeometric test cases\n") good <- 5 bad <- 5 selected <- 5 densityPoints <- c(-1, 0, 1, 2, 3, 4, 5, 10) densityValues <- c(0, 0.003968, 0.099206, 0.396825, 0.396825, 0.099206, 0.003968, 0) distributionValues <- c(0, .003968, .103175, .50000, .896825, .996032, 1.00000, 1) #Eliminate p=1 case because it will mess up adjustement below inverseCumPoints <- c(0, 0.001, 0.010, 0.025, 0.050, 0.100, 0.999, 0.990, 0.975, 0.950, 0.900) inverseCumValues <- c(-1, -1, 0, 0, 0, 0, 4, 3, 3, 3, 3) verifyDensity(densityPoints, densityValues, good, bad, selected, tol) verifyDistribution(densityPoints, distributionValues, good, bad, selected, tol) i <- 0 rInverseCumValues <- rep(0,length(inverseCumPoints)) for (point in inverseCumPoints) { i <- i + 1 rInverseCumValues[i] <- qhyper(point, good, bad, selected) } output <- c("Inverse Distribution test good = ", good, ", bad = ", bad, ", selected = ", selected) # R defines quantiles from the right, need to subtract one if (assertEquals(inverseCumValues, rInverseCumValues-1, tol, "Inverse Dist Values")) { displayPadded(output, SUCCEEDED, 80) } else { displayPadded(output, FAILED, 80) } # Degenerate cases good <- 5 bad <- 0 selected <- 3 densityPoints <- c(-1, 0, 1, 3, 10) densityValues <- c(0, 0, 0, 1, 0) distributionValues <- c(0, 0, 0, 1, 1) verifyDensity(densityPoints, densityValues, good, bad, selected, tol) verifyDistribution(densityPoints, distributionValues, good, bad, selected, tol) good <- 0 bad <- 5 selected <- 3 densityPoints <- c(-1, 0, 1, 3, 10) densityValues <- c(0, 1, 0, 0, 0) distributionValues <- c(0, 1, 1, 1, 1) verifyDensity(densityPoints, densityValues, good, bad, selected, tol) verifyDistribution(densityPoints, distributionValues, good, bad, selected, tol) good <- 3 bad <- 2 selected <- 5 densityPoints <- c(-1, 0, 1, 3, 10) densityValues <- c(0, 0, 0, 1, 0) distributionValues <- c(0, 0, 0, 1, 1) verifyDensity(densityPoints, densityValues, good, bad, selected, tol) verifyDistribution(densityPoints, distributionValues, good, bad, selected, tol) displayDashes(WIDTH) commons-math3-3.2-src/src/test/R/exponentialTestCases100644 1750 1750 7516 12126627667 21450 0ustarlucluc 0 0 # Licensed to the Apache Software Foundation (ASF) under one or more # contributor license agreements. See the NOTICE file distributed with # this work for additional information regarding copyright ownership. # The ASF licenses this file to You 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. # #------------------------------------------------------------------------------ # R source file to validate exponential distribution tests in # org.apache.commons.math.distribution.ExponentialDistributionTest # # To run the test, install R, put this file and testFunctions # into the same directory, launch R from this directory and then enter # source("") # #------------------------------------------------------------------------------ tol <- 1E-9 # Function definitions source("testFunctions") # utility test functions # function to verify distribution computations verifyDistribution <- function(points, expected, mean, tol) { rDistValues <- rep(0, length(points)) i <- 0 for (point in points) { i <- i + 1 rDistValues[i] <- pexp(point, 1/mean) } output <- c("Distribution test mean = ", mean) if (assertEquals(expected, rDistValues, tol, "Distribution Values")) { displayPadded(output, SUCCEEDED, WIDTH) } else { displayPadded(output, FAILED, WIDTH) } } # function to verify density computations verifyDensity <- function(points, expected, mean, tol) { rDensityValues <- rep(0, length(points)) i <- 0 for (point in points) { i <- i + 1 rDensityValues[i] <- dexp(point, 1/mean) } output <- c("Density test mean = ", mean) if (assertEquals(expected, rDensityValues, tol, "Density Values")) { displayPadded(output, SUCCEEDED, WIDTH) } else { displayPadded(output, FAILED, WIDTH) } } # function to verify quantiles verifyQuantiles <- function(points, expected, mean, tol) { rQuantileValues <- rep(0, length(points)) i <- 0 for (point in points) { i <- i + 1 rQuantileValues[i] <- qexp(point, 1/mean, log = FALSE) } output <- c("Quantile test mean = ", mean) if (assertEquals(expected, rQuantileValues, tol, "Quantile Values")) { displayPadded(output, SUCCEEDED, WIDTH) } else { displayPadded(output, FAILED, WIDTH) } } #-------------------------------------------------------------------------- cat("Exponential test cases\n") mean <- 5 distributionValues <- c(0, 0, 0.001, 0.01, 0.025, 0.05, 0.1, 0.999, 0.990, 0.975, 0.950, 0.900) densityValues <- c(0.2, 0.2, 0.1998, 0.198, 0.195, 0.19, 0.18, 0.000200000000000, 0.00200000000002, 0.00499999999997, 0.00999999999994, 0.0199999999999) distributionPoints <- c(0, 0, 0.00500250166792, 0.0502516792675, 0.126589039921, 0.256466471938, 0.526802578289, 34.5387763949, 23.0258509299, 18.4443972706, 14.9786613678, 11.5129254650) verifyDistribution(distributionPoints, distributionValues, mean, tol) verifyQuantiles(distributionValues, distributionPoints, mean, tol) verifyDensity(distributionPoints, densityValues, mean, tol) output <- "Probability test P(.25 < X < .75)" if (assertEquals(0.0905214480757, pexp(.75, 1/mean) - pexp(.25, 1/mean), tol, "Probability value")) { displayPadded(output, SUCCEEDED, WIDTH) } else { displayPadded(output, FAILED, WIDTH) } displayDashes(WIDTH) commons-math3-3.2-src/src/test/resources/org/apache/commons/math3/optimization/general/MGH17.dat100644 1750 1750 6015 12126627677 31302 0ustarlucluc 0 0 NIST/ITL StRD Dataset Name: MGH17 (MGH17.dat) File Format: ASCII Starting Values (lines 41 to 45) Certified Values (lines 41 to 50) Data (lines 61 to 93) Procedure: Nonlinear Least Squares Regression Description: This problem was found to be difficult for some very good algorithms. See More, J. J., Garbow, B. S., and Hillstrom, K. E. (1981). Testing unconstrained optimization software. ACM Transactions on Mathematical Software. 7(1): pp. 17-41. Reference: Osborne, M. R. (1972). Some aspects of nonlinear least squares calculations. In Numerical Methods for Nonlinear Optimization, Lootsma (Ed). New York, NY: Academic Press, pp. 171-189. Data: 1 Response (y) 1 Predictor (x) 33 Observations Average Level of Difficulty Generated Data Model: Exponential Class 5 Parameters (b1 to b5) y = b1 + b2*exp[-x*b4] + b3*exp[-x*b5] + e Starting values Certified Values Start 1 Start 2 Parameter Standard Deviation b1 = 50 0.5 3.7541005211E-01 2.0723153551E-03 b2 = 150 1.5 1.9358469127E+00 2.2031669222E-01 b3 = -100 -1 -1.4646871366E+00 2.2175707739E-01 b4 = 1 0.01 1.2867534640E-02 4.4861358114E-04 b5 = 2 0.02 2.2122699662E-02 8.9471996575E-04 Residual Sum of Squares: 5.4648946975E-05 Residual Standard Deviation: 1.3970497866E-03 Degrees of Freedom: 28 Number of Observations: 33 Data: y x 8.440000E-01 0.000000E+00 9.080000E-01 1.000000E+01 9.320000E-01 2.000000E+01 9.360000E-01 3.000000E+01 9.250000E-01 4.000000E+01 9.080000E-01 5.000000E+01 8.810000E-01 6.000000E+01 8.500000E-01 7.000000E+01 8.180000E-01 8.000000E+01 7.840000E-01 9.000000E+01 7.510000E-01 1.000000E+02 7.180000E-01 1.100000E+02 6.850000E-01 1.200000E+02 6.580000E-01 1.300000E+02 6.280000E-01 1.400000E+02 6.030000E-01 1.500000E+02 5.800000E-01 1.600000E+02 5.580000E-01 1.700000E+02 5.380000E-01 1.800000E+02 5.220000E-01 1.900000E+02 5.060000E-01 2.000000E+02 4.900000E-01 2.100000E+02 4.780000E-01 2.200000E+02 4.670000E-01 2.300000E+02 4.570000E-01 2.400000E+02 4.480000E-01 2.500000E+02 4.380000E-01 2.600000E+02 4.310000E-01 2.700000E+02 4.240000E-01 2.800000E+02 4.200000E-01 2.900000E+02 4.140000E-01 3.000000E+02 4.110000E-01 3.100000E+02 4.060000E-01 3.200000E+02 commons-math3-3.2-src/src/test/resources/org/apache/commons/math3/optimization/general/Kirby2.dat100644 1750 1750 13346 12126627677 31706 0ustarlucluc 0 0 NIST/ITL StRD Dataset Name: Kirby2 (Kirby2.dat) File Format: ASCII Starting Values (lines 41 to 45) Certified Values (lines 41 to 50) Data (lines 61 to 211) Procedure: Nonlinear Least Squares Regression Description: These data are the result of a NIST study involving scanning electron microscope line with standards. Reference: Kirby, R., NIST (197?). Scanning electron microscope line width standards. Data: 1 Response (y) 1 Predictor (x) 151 Observations Average Level of Difficulty Observed Data Model: Rational Class (quadratic/quadratic) 5 Parameters (b1 to b5) y = (b1 + b2*x + b3*x**2) / (1 + b4*x + b5*x**2) + e Starting values Certified Values Start 1 Start 2 Parameter Standard Deviation b1 = 2 1.5 1.6745063063E+00 8.7989634338E-02 b2 = -0.1 -0.15 -1.3927397867E-01 4.1182041386E-03 b3 = 0.003 0.0025 2.5961181191E-03 4.1856520458E-05 b4 = -0.001 -0.0015 -1.7241811870E-03 5.8931897355E-05 b5 = 0.00001 0.00002 2.1664802578E-05 2.0129761919E-07 Residual Sum of Squares: 3.9050739624E+00 Residual Standard Deviation: 1.6354535131E-01 Degrees of Freedom: 146 Number of Observations: 151 Data: y x 0.0082E0 9.65E0 0.0112E0 10.74E0 0.0149E0 11.81E0 0.0198E0 12.88E0 0.0248E0 14.06E0 0.0324E0 15.28E0 0.0420E0 16.63E0 0.0549E0 18.19E0 0.0719E0 19.88E0 0.0963E0 21.84E0 0.1291E0 24.00E0 0.1710E0 26.25E0 0.2314E0 28.86E0 0.3227E0 31.85E0 0.4809E0 35.79E0 0.7084E0 40.18E0 1.0220E0 44.74E0 1.4580E0 49.53E0 1.9520E0 53.94E0 2.5410E0 58.29E0 3.2230E0 62.63E0 3.9990E0 67.03E0 4.8520E0 71.25E0 5.7320E0 75.22E0 6.7270E0 79.33E0 7.8350E0 83.56E0 9.0250E0 87.75E0 10.2670E0 91.93E0 11.5780E0 96.10E0 12.9440E0 100.28E0 14.3770E0 104.46E0 15.8560E0 108.66E0 17.3310E0 112.71E0 18.8850E0 116.88E0 20.5750E0 121.33E0 22.3200E0 125.79E0 22.3030E0 125.79E0 23.4600E0 128.74E0 24.0600E0 130.27E0 25.2720E0 133.33E0 25.8530E0 134.79E0 27.1100E0 137.93E0 27.6580E0 139.33E0 28.9240E0 142.46E0 29.5110E0 143.90E0 30.7100E0 146.91E0 31.3500E0 148.51E0 32.5200E0 151.41E0 33.2300E0 153.17E0 34.3300E0 155.97E0 35.0600E0 157.76E0 36.1700E0 160.56E0 36.8400E0 162.30E0 38.0100E0 165.21E0 38.6700E0 166.90E0 39.8700E0 169.92E0 40.0300E0 170.32E0 40.5000E0 171.54E0 41.3700E0 173.79E0 41.6700E0 174.57E0 42.3100E0 176.25E0 42.7300E0 177.34E0 43.4600E0 179.19E0 44.1400E0 181.02E0 44.5500E0 182.08E0 45.2200E0 183.88E0 45.9200E0 185.75E0 46.3000E0 186.80E0 47.0000E0 188.63E0 47.6800E0 190.45E0 48.0600E0 191.48E0 48.7400E0 193.35E0 49.4100E0 195.22E0 49.7600E0 196.23E0 50.4300E0 198.05E0 51.1100E0 199.97E0 51.5000E0 201.06E0 52.1200E0 202.83E0 52.7600E0 204.69E0 53.1800E0 205.86E0 53.7800E0 207.58E0 54.4600E0 209.50E0 54.8300E0 210.65E0 55.4000E0 212.33E0 56.4300E0 215.43E0 57.0300E0 217.16E0 58.0000E0 220.21E0 58.6100E0 221.98E0 59.5800E0 225.06E0 60.1100E0 226.79E0 61.1000E0 229.92E0 61.6500E0 231.69E0 62.5900E0 234.77E0 63.1200E0 236.60E0 64.0300E0 239.63E0 64.6200E0 241.50E0 65.4900E0 244.48E0 66.0300E0 246.40E0 66.8900E0 249.35E0 67.4200E0 251.32E0 68.2300E0 254.22E0 68.7700E0 256.24E0 69.5900E0 259.11E0 70.1100E0 261.18E0 70.8600E0 264.02E0 71.4300E0 266.13E0 72.1600E0 268.94E0 72.7000E0 271.09E0 73.4000E0 273.87E0 73.9300E0 276.08E0 74.6000E0 278.83E0 75.1600E0 281.08E0 75.8200E0 283.81E0 76.3400E0 286.11E0 76.9800E0 288.81E0 77.4800E0 291.08E0 78.0800E0 293.75E0 78.6000E0 295.99E0 79.1700E0 298.64E0 79.6200E0 300.84E0 79.8800E0 302.02E0 80.1900E0 303.48E0 80.6600E0 305.65E0 81.2200E0 308.27E0 81.6600E0 310.41E0 82.1600E0 313.01E0 82.5900E0 315.12E0 83.1400E0 317.71E0 83.5000E0 319.79E0 84.0000E0 322.36E0 84.4000E0 324.42E0 84.8900E0 326.98E0 85.2600E0 329.01E0 85.7400E0 331.56E0 86.0700E0 333.56E0 86.5400E0 336.10E0 86.8900E0 338.08E0 87.3200E0 340.60E0 87.6500E0 342.57E0 88.1000E0 345.08E0 88.4300E0 347.02E0 88.8300E0 349.52E0 89.1200E0 351.44E0 89.5400E0 353.93E0 89.8500E0 355.83E0 90.2500E0 358.32E0 90.5500E0 360.20E0 90.9300E0 362.67E0 91.2000E0 364.53E0 91.5500E0 367.00E0 92.2000E0 371.30E0 commons-math3-3.2-src/src/test/resources/org/apache/commons/math3/optimization/general/Hahn1.dat100644 1750 1750 23035 12126627677 31477 0ustarlucluc 0 0 NIST/ITL StRD Dataset Name: Hahn1 (Hahn1.dat) File Format: ASCII Starting Values (lines 41 to 47) Certified Values (lines 41 to 52) Data (lines 61 to 296) Procedure: Nonlinear Least Squares Regression Description: These data are the result of a NIST study involving the thermal expansion of copper. The response variable is the coefficient of thermal expansion, and the predictor variable is temperature in degrees kelvin. Reference: Hahn, T., NIST (197?). Copper Thermal Expansion Study. Data: 1 Response (y = coefficient of thermal expansion) 1 Predictor (x = temperature, degrees kelvin) 236 Observations Average Level of Difficulty Observed Data Model: Rational Class (cubic/cubic) 7 Parameters (b1 to b7) y = (b1+b2*x+b3*x**2+b4*x**3) / (1+b5*x+b6*x**2+b7*x**3) + e Starting values Certified Values Start 1 Start 2 Parameter Standard Deviation b1 = 10 1 1.0776351733E+00 1.7070154742E-01 b2 = -1 -0.1 -1.2269296921E-01 1.2000289189E-02 b3 = 0.05 0.005 4.0863750610E-03 2.2508314937E-04 b4 = -0.00001 -0.000001 -1.4262662514E-06 2.7578037666E-07 b5 = -0.05 -0.005 -5.7609940901E-03 2.4712888219E-04 b6 = 0.001 0.0001 2.4053735503E-04 1.0449373768E-05 b7 = -0.000001 -0.0000001 -1.2314450199E-07 1.3027335327E-08 Residual Sum of Squares: 1.5324382854E+00 Residual Standard Deviation: 8.1803852243E-02 Degrees of Freedom: 229 Number of Observations: 236 Data: y x .591E0 24.41E0 1.547E0 34.82E0 2.902E0 44.09E0 2.894E0 45.07E0 4.703E0 54.98E0 6.307E0 65.51E0 7.03E0 70.53E0 7.898E0 75.70E0 9.470E0 89.57E0 9.484E0 91.14E0 10.072E0 96.40E0 10.163E0 97.19E0 11.615E0 114.26E0 12.005E0 120.25E0 12.478E0 127.08E0 12.982E0 133.55E0 12.970E0 133.61E0 13.926E0 158.67E0 14.452E0 172.74E0 14.404E0 171.31E0 15.190E0 202.14E0 15.550E0 220.55E0 15.528E0 221.05E0 15.499E0 221.39E0 16.131E0 250.99E0 16.438E0 268.99E0 16.387E0 271.80E0 16.549E0 271.97E0 16.872E0 321.31E0 16.830E0 321.69E0 16.926E0 330.14E0 16.907E0 333.03E0 16.966E0 333.47E0 17.060E0 340.77E0 17.122E0 345.65E0 17.311E0 373.11E0 17.355E0 373.79E0 17.668E0 411.82E0 17.767E0 419.51E0 17.803E0 421.59E0 17.765E0 422.02E0 17.768E0 422.47E0 17.736E0 422.61E0 17.858E0 441.75E0 17.877E0 447.41E0 17.912E0 448.7E0 18.046E0 472.89E0 18.085E0 476.69E0 18.291E0 522.47E0 18.357E0 522.62E0 18.426E0 524.43E0 18.584E0 546.75E0 18.610E0 549.53E0 18.870E0 575.29E0 18.795E0 576.00E0 19.111E0 625.55E0 .367E0 20.15E0 .796E0 28.78E0 0.892E0 29.57E0 1.903E0 37.41E0 2.150E0 39.12E0 3.697E0 50.24E0 5.870E0 61.38E0 6.421E0 66.25E0 7.422E0 73.42E0 9.944E0 95.52E0 11.023E0 107.32E0 11.87E0 122.04E0 12.786E0 134.03E0 14.067E0 163.19E0 13.974E0 163.48E0 14.462E0 175.70E0 14.464E0 179.86E0 15.381E0 211.27E0 15.483E0 217.78E0 15.59E0 219.14E0 16.075E0 262.52E0 16.347E0 268.01E0 16.181E0 268.62E0 16.915E0 336.25E0 17.003E0 337.23E0 16.978E0 339.33E0 17.756E0 427.38E0 17.808E0 428.58E0 17.868E0 432.68E0 18.481E0 528.99E0 18.486E0 531.08E0 19.090E0 628.34E0 16.062E0 253.24E0 16.337E0 273.13E0 16.345E0 273.66E0 16.388E0 282.10E0 17.159E0 346.62E0 17.116E0 347.19E0 17.164E0 348.78E0 17.123E0 351.18E0 17.979E0 450.10E0 17.974E0 450.35E0 18.007E0 451.92E0 17.993E0 455.56E0 18.523E0 552.22E0 18.669E0 553.56E0 18.617E0 555.74E0 19.371E0 652.59E0 19.330E0 656.20E0 0.080E0 14.13E0 0.248E0 20.41E0 1.089E0 31.30E0 1.418E0 33.84E0 2.278E0 39.70E0 3.624E0 48.83E0 4.574E0 54.50E0 5.556E0 60.41E0 7.267E0 72.77E0 7.695E0 75.25E0 9.136E0 86.84E0 9.959E0 94.88E0 9.957E0 96.40E0 11.600E0 117.37E0 13.138E0 139.08E0 13.564E0 147.73E0 13.871E0 158.63E0 13.994E0 161.84E0 14.947E0 192.11E0 15.473E0 206.76E0 15.379E0 209.07E0 15.455E0 213.32E0 15.908E0 226.44E0 16.114E0 237.12E0 17.071E0 330.90E0 17.135E0 358.72E0 17.282E0 370.77E0 17.368E0 372.72E0 17.483E0 396.24E0 17.764E0 416.59E0 18.185E0 484.02E0 18.271E0 495.47E0 18.236E0 514.78E0 18.237E0 515.65E0 18.523E0 519.47E0 18.627E0 544.47E0 18.665E0 560.11E0 19.086E0 620.77E0 0.214E0 18.97E0 0.943E0 28.93E0 1.429E0 33.91E0 2.241E0 40.03E0 2.951E0 44.66E0 3.782E0 49.87E0 4.757E0 55.16E0 5.602E0 60.90E0 7.169E0 72.08E0 8.920E0 85.15E0 10.055E0 97.06E0 12.035E0 119.63E0 12.861E0 133.27E0 13.436E0 143.84E0 14.167E0 161.91E0 14.755E0 180.67E0 15.168E0 198.44E0 15.651E0 226.86E0 15.746E0 229.65E0 16.216E0 258.27E0 16.445E0 273.77E0 16.965E0 339.15E0 17.121E0 350.13E0 17.206E0 362.75E0 17.250E0 371.03E0 17.339E0 393.32E0 17.793E0 448.53E0 18.123E0 473.78E0 18.49E0 511.12E0 18.566E0 524.70E0 18.645E0 548.75E0 18.706E0 551.64E0 18.924E0 574.02E0 19.1E0 623.86E0 0.375E0 21.46E0 0.471E0 24.33E0 1.504E0 33.43E0 2.204E0 39.22E0 2.813E0 44.18E0 4.765E0 55.02E0 9.835E0 94.33E0 10.040E0 96.44E0 11.946E0 118.82E0 12.596E0 128.48E0 13.303E0 141.94E0 13.922E0 156.92E0 14.440E0 171.65E0 14.951E0 190.00E0 15.627E0 223.26E0 15.639E0 223.88E0 15.814E0 231.50E0 16.315E0 265.05E0 16.334E0 269.44E0 16.430E0 271.78E0 16.423E0 273.46E0 17.024E0 334.61E0 17.009E0 339.79E0 17.165E0 349.52E0 17.134E0 358.18E0 17.349E0 377.98E0 17.576E0 394.77E0 17.848E0 429.66E0 18.090E0 468.22E0 18.276E0 487.27E0 18.404E0 519.54E0 18.519E0 523.03E0 19.133E0 612.99E0 19.074E0 638.59E0 19.239E0 641.36E0 19.280E0 622.05E0 19.101E0 631.50E0 19.398E0 663.97E0 19.252E0 646.9E0 19.89E0 748.29E0 20.007E0 749.21E0 19.929E0 750.14E0 19.268E0 647.04E0 19.324E0 646.89E0 20.049E0 746.9E0 20.107E0 748.43E0 20.062E0 747.35E0 20.065E0 749.27E0 19.286E0 647.61E0 19.972E0 747.78E0 20.088E0 750.51E0 20.743E0 851.37E0 20.83E0 845.97E0 20.935E0 847.54E0 21.035E0 849.93E0 20.93E0 851.61E0 21.074E0 849.75E0 21.085E0 850.98E0 20.935E0 848.23E0 commons-math3-3.2-src/src/test/resources/org/apache/commons/math3/optimization/general/Lanczos1.dat100644 1750 1750 5604 12126627677 32214 0ustarlucluc 0 0 NIST/ITL StRD Dataset Name: Lanczos1 (Lanczos1.dat) File Format: ASCII Starting Values (lines 41 to 46) Certified Values (lines 41 to 51) Data (lines 61 to 84) Procedure: Nonlinear Least Squares Regression Description: These data are taken from an example discussed in Lanczos (1956). The data were generated to 14-digits of accuracy using f(x) = 0.0951*exp(-x) + 0.8607*exp(-3*x) + 1.5576*exp(-5*x). Reference: Lanczos, C. (1956). Applied Analysis. Englewood Cliffs, NJ: Prentice Hall, pp. 272-280. Data: 1 Response (y) 1 Predictor (x) 24 Observations Average Level of Difficulty Generated Data Model: Exponential Class 6 Parameters (b1 to b6) y = b1*exp(-b2*x) + b3*exp(-b4*x) + b5*exp(-b6*x) + e Starting values Certified Values Start 1 Start 2 Parameter Standard Deviation b1 = 1.2 0.5 9.5100000027E-02 5.3347304234E-11 b2 = 0.3 0.7 1.0000000001E+00 2.7473038179E-10 b3 = 5.6 3.6 8.6070000013E-01 1.3576062225E-10 b4 = 5.5 4.2 3.0000000002E+00 3.3308253069E-10 b5 = 6.5 4 1.5575999998E+00 1.8815731448E-10 b6 = 7.6 6.3 5.0000000001E+00 1.1057500538E-10 Residual Sum of Squares: 1.4307867721E-25 Residual Standard Deviation: 8.9156129349E-14 Degrees of Freedom: 18 Number of Observations: 24 Data: y x 2.513400000000E+00 0.000000000000E+00 2.044333373291E+00 5.000000000000E-02 1.668404436564E+00 1.000000000000E-01 1.366418021208E+00 1.500000000000E-01 1.123232487372E+00 2.000000000000E-01 9.268897180037E-01 2.500000000000E-01 7.679338563728E-01 3.000000000000E-01 6.388775523106E-01 3.500000000000E-01 5.337835317402E-01 4.000000000000E-01 4.479363617347E-01 4.500000000000E-01 3.775847884350E-01 5.000000000000E-01 3.197393199326E-01 5.500000000000E-01 2.720130773746E-01 6.000000000000E-01 2.324965529032E-01 6.500000000000E-01 1.996589546065E-01 7.000000000000E-01 1.722704126914E-01 7.500000000000E-01 1.493405660168E-01 8.000000000000E-01 1.300700206922E-01 8.500000000000E-01 1.138119324644E-01 9.000000000000E-01 1.000415587559E-01 9.500000000000E-01 8.833209084540E-02 1.000000000000E+00 7.833544019350E-02 1.050000000000E+00 6.976693743449E-02 1.100000000000E+00 6.239312536719E-02 1.150000000000E+00 ././@LongLink100644 0 0 154 12126630646 10261 Lustar 0 0 commons-math3-3.2-src/src/test/resources/org/apache/commons/math3/optim/nonlinear/vector/jacobian/MGH17.datcommons-math3-3.2-src/src/test/resources/org/apache/commons/math3/optim/nonlinear/vector/jacobian/MG100644 1750 1750 6015 12126627677 32275 0ustarlucluc 0 0 NIST/ITL StRD Dataset Name: MGH17 (MGH17.dat) File Format: ASCII Starting Values (lines 41 to 45) Certified Values (lines 41 to 50) Data (lines 61 to 93) Procedure: Nonlinear Least Squares Regression Description: This problem was found to be difficult for some very good algorithms. See More, J. J., Garbow, B. S., and Hillstrom, K. E. (1981). Testing unconstrained optimization software. ACM Transactions on Mathematical Software. 7(1): pp. 17-41. Reference: Osborne, M. R. (1972). Some aspects of nonlinear least squares calculations. In Numerical Methods for Nonlinear Optimization, Lootsma (Ed). New York, NY: Academic Press, pp. 171-189. Data: 1 Response (y) 1 Predictor (x) 33 Observations Average Level of Difficulty Generated Data Model: Exponential Class 5 Parameters (b1 to b5) y = b1 + b2*exp[-x*b4] + b3*exp[-x*b5] + e Starting values Certified Values Start 1 Start 2 Parameter Standard Deviation b1 = 50 0.5 3.7541005211E-01 2.0723153551E-03 b2 = 150 1.5 1.9358469127E+00 2.2031669222E-01 b3 = -100 -1 -1.4646871366E+00 2.2175707739E-01 b4 = 1 0.01 1.2867534640E-02 4.4861358114E-04 b5 = 2 0.02 2.2122699662E-02 8.9471996575E-04 Residual Sum of Squares: 5.4648946975E-05 Residual Standard Deviation: 1.3970497866E-03 Degrees of Freedom: 28 Number of Observations: 33 Data: y x 8.440000E-01 0.000000E+00 9.080000E-01 1.000000E+01 9.320000E-01 2.000000E+01 9.360000E-01 3.000000E+01 9.250000E-01 4.000000E+01 9.080000E-01 5.000000E+01 8.810000E-01 6.000000E+01 8.500000E-01 7.000000E+01 8.180000E-01 8.000000E+01 7.840000E-01 9.000000E+01 7.510000E-01 1.000000E+02 7.180000E-01 1.100000E+02 6.850000E-01 1.200000E+02 6.580000E-01 1.300000E+02 6.280000E-01 1.400000E+02 6.030000E-01 1.500000E+02 5.800000E-01 1.600000E+02 5.580000E-01 1.700000E+02 5.380000E-01 1.800000E+02 5.220000E-01 1.900000E+02 5.060000E-01 2.000000E+02 4.900000E-01 2.100000E+02 4.780000E-01 2.200000E+02 4.670000E-01 2.300000E+02 4.570000E-01 2.400000E+02 4.480000E-01 2.500000E+02 4.380000E-01 2.600000E+02 4.310000E-01 2.700000E+02 4.240000E-01 2.800000E+02 4.200000E-01 2.900000E+02 4.140000E-01 3.000000E+02 4.110000E-01 3.100000E+02 4.060000E-01 3.200000E+02 ././@LongLink100644 0 0 155 12126630646 10262 Lustar 0 0 commons-math3-3.2-src/src/test/resources/org/apache/commons/math3/optim/nonlinear/vector/jacobian/Kirby2.datcommons-math3-3.2-src/src/test/resources/org/apache/commons/math3/optim/nonlinear/vector/jacobian/Ki100644 1750 1750 13346 12126627677 32362 0ustarlucluc 0 0 NIST/ITL StRD Dataset Name: Kirby2 (Kirby2.dat) File Format: ASCII Starting Values (lines 41 to 45) Certified Values (lines 41 to 50) Data (lines 61 to 211) Procedure: Nonlinear Least Squares Regression Description: These data are the result of a NIST study involving scanning electron microscope line with standards. Reference: Kirby, R., NIST (197?). Scanning electron microscope line width standards. Data: 1 Response (y) 1 Predictor (x) 151 Observations Average Level of Difficulty Observed Data Model: Rational Class (quadratic/quadratic) 5 Parameters (b1 to b5) y = (b1 + b2*x + b3*x**2) / (1 + b4*x + b5*x**2) + e Starting values Certified Values Start 1 Start 2 Parameter Standard Deviation b1 = 2 1.5 1.6745063063E+00 8.7989634338E-02 b2 = -0.1 -0.15 -1.3927397867E-01 4.1182041386E-03 b3 = 0.003 0.0025 2.5961181191E-03 4.1856520458E-05 b4 = -0.001 -0.0015 -1.7241811870E-03 5.8931897355E-05 b5 = 0.00001 0.00002 2.1664802578E-05 2.0129761919E-07 Residual Sum of Squares: 3.9050739624E+00 Residual Standard Deviation: 1.6354535131E-01 Degrees of Freedom: 146 Number of Observations: 151 Data: y x 0.0082E0 9.65E0 0.0112E0 10.74E0 0.0149E0 11.81E0 0.0198E0 12.88E0 0.0248E0 14.06E0 0.0324E0 15.28E0 0.0420E0 16.63E0 0.0549E0 18.19E0 0.0719E0 19.88E0 0.0963E0 21.84E0 0.1291E0 24.00E0 0.1710E0 26.25E0 0.2314E0 28.86E0 0.3227E0 31.85E0 0.4809E0 35.79E0 0.7084E0 40.18E0 1.0220E0 44.74E0 1.4580E0 49.53E0 1.9520E0 53.94E0 2.5410E0 58.29E0 3.2230E0 62.63E0 3.9990E0 67.03E0 4.8520E0 71.25E0 5.7320E0 75.22E0 6.7270E0 79.33E0 7.8350E0 83.56E0 9.0250E0 87.75E0 10.2670E0 91.93E0 11.5780E0 96.10E0 12.9440E0 100.28E0 14.3770E0 104.46E0 15.8560E0 108.66E0 17.3310E0 112.71E0 18.8850E0 116.88E0 20.5750E0 121.33E0 22.3200E0 125.79E0 22.3030E0 125.79E0 23.4600E0 128.74E0 24.0600E0 130.27E0 25.2720E0 133.33E0 25.8530E0 134.79E0 27.1100E0 137.93E0 27.6580E0 139.33E0 28.9240E0 142.46E0 29.5110E0 143.90E0 30.7100E0 146.91E0 31.3500E0 148.51E0 32.5200E0 151.41E0 33.2300E0 153.17E0 34.3300E0 155.97E0 35.0600E0 157.76E0 36.1700E0 160.56E0 36.8400E0 162.30E0 38.0100E0 165.21E0 38.6700E0 166.90E0 39.8700E0 169.92E0 40.0300E0 170.32E0 40.5000E0 171.54E0 41.3700E0 173.79E0 41.6700E0 174.57E0 42.3100E0 176.25E0 42.7300E0 177.34E0 43.4600E0 179.19E0 44.1400E0 181.02E0 44.5500E0 182.08E0 45.2200E0 183.88E0 45.9200E0 185.75E0 46.3000E0 186.80E0 47.0000E0 188.63E0 47.6800E0 190.45E0 48.0600E0 191.48E0 48.7400E0 193.35E0 49.4100E0 195.22E0 49.7600E0 196.23E0 50.4300E0 198.05E0 51.1100E0 199.97E0 51.5000E0 201.06E0 52.1200E0 202.83E0 52.7600E0 204.69E0 53.1800E0 205.86E0 53.7800E0 207.58E0 54.4600E0 209.50E0 54.8300E0 210.65E0 55.4000E0 212.33E0 56.4300E0 215.43E0 57.0300E0 217.16E0 58.0000E0 220.21E0 58.6100E0 221.98E0 59.5800E0 225.06E0 60.1100E0 226.79E0 61.1000E0 229.92E0 61.6500E0 231.69E0 62.5900E0 234.77E0 63.1200E0 236.60E0 64.0300E0 239.63E0 64.6200E0 241.50E0 65.4900E0 244.48E0 66.0300E0 246.40E0 66.8900E0 249.35E0 67.4200E0 251.32E0 68.2300E0 254.22E0 68.7700E0 256.24E0 69.5900E0 259.11E0 70.1100E0 261.18E0 70.8600E0 264.02E0 71.4300E0 266.13E0 72.1600E0 268.94E0 72.7000E0 271.09E0 73.4000E0 273.87E0 73.9300E0 276.08E0 74.6000E0 278.83E0 75.1600E0 281.08E0 75.8200E0 283.81E0 76.3400E0 286.11E0 76.9800E0 288.81E0 77.4800E0 291.08E0 78.0800E0 293.75E0 78.6000E0 295.99E0 79.1700E0 298.64E0 79.6200E0 300.84E0 79.8800E0 302.02E0 80.1900E0 303.48E0 80.6600E0 305.65E0 81.2200E0 308.27E0 81.6600E0 310.41E0 82.1600E0 313.01E0 82.5900E0 315.12E0 83.1400E0 317.71E0 83.5000E0 319.79E0 84.0000E0 322.36E0 84.4000E0 324.42E0 84.8900E0 326.98E0 85.2600E0 329.01E0 85.7400E0 331.56E0 86.0700E0 333.56E0 86.5400E0 336.10E0 86.8900E0 338.08E0 87.3200E0 340.60E0 87.6500E0 342.57E0 88.1000E0 345.08E0 88.4300E0 347.02E0 88.8300E0 349.52E0 89.1200E0 351.44E0 89.5400E0 353.93E0 89.8500E0 355.83E0 90.2500E0 358.32E0 90.5500E0 360.20E0 90.9300E0 362.67E0 91.2000E0 364.53E0 91.5500E0 367.00E0 92.2000E0 371.30E0 ././@LongLink100644 0 0 154 12126630646 10261 Lustar 0 0 commons-math3-3.2-src/src/test/resources/org/apache/commons/math3/optim/nonlinear/vector/jacobian/Hahn1.datcommons-math3-3.2-src/src/test/resources/org/apache/commons/math3/optim/nonlinear/vector/jacobian/Ha100644 1750 1750 23035 12126627677 32343 0ustarlucluc 0 0 NIST/ITL StRD Dataset Name: Hahn1 (Hahn1.dat) File Format: ASCII Starting Values (lines 41 to 47) Certified Values (lines 41 to 52) Data (lines 61 to 296) Procedure: Nonlinear Least Squares Regression Description: These data are the result of a NIST study involving the thermal expansion of copper. The response variable is the coefficient of thermal expansion, and the predictor variable is temperature in degrees kelvin. Reference: Hahn, T., NIST (197?). Copper Thermal Expansion Study. Data: 1 Response (y = coefficient of thermal expansion) 1 Predictor (x = temperature, degrees kelvin) 236 Observations Average Level of Difficulty Observed Data Model: Rational Class (cubic/cubic) 7 Parameters (b1 to b7) y = (b1+b2*x+b3*x**2+b4*x**3) / (1+b5*x+b6*x**2+b7*x**3) + e Starting values Certified Values Start 1 Start 2 Parameter Standard Deviation b1 = 10 1 1.0776351733E+00 1.7070154742E-01 b2 = -1 -0.1 -1.2269296921E-01 1.2000289189E-02 b3 = 0.05 0.005 4.0863750610E-03 2.2508314937E-04 b4 = -0.00001 -0.000001 -1.4262662514E-06 2.7578037666E-07 b5 = -0.05 -0.005 -5.7609940901E-03 2.4712888219E-04 b6 = 0.001 0.0001 2.4053735503E-04 1.0449373768E-05 b7 = -0.000001 -0.0000001 -1.2314450199E-07 1.3027335327E-08 Residual Sum of Squares: 1.5324382854E+00 Residual Standard Deviation: 8.1803852243E-02 Degrees of Freedom: 229 Number of Observations: 236 Data: y x .591E0 24.41E0 1.547E0 34.82E0 2.902E0 44.09E0 2.894E0 45.07E0 4.703E0 54.98E0 6.307E0 65.51E0 7.03E0 70.53E0 7.898E0 75.70E0 9.470E0 89.57E0 9.484E0 91.14E0 10.072E0 96.40E0 10.163E0 97.19E0 11.615E0 114.26E0 12.005E0 120.25E0 12.478E0 127.08E0 12.982E0 133.55E0 12.970E0 133.61E0 13.926E0 158.67E0 14.452E0 172.74E0 14.404E0 171.31E0 15.190E0 202.14E0 15.550E0 220.55E0 15.528E0 221.05E0 15.499E0 221.39E0 16.131E0 250.99E0 16.438E0 268.99E0 16.387E0 271.80E0 16.549E0 271.97E0 16.872E0 321.31E0 16.830E0 321.69E0 16.926E0 330.14E0 16.907E0 333.03E0 16.966E0 333.47E0 17.060E0 340.77E0 17.122E0 345.65E0 17.311E0 373.11E0 17.355E0 373.79E0 17.668E0 411.82E0 17.767E0 419.51E0 17.803E0 421.59E0 17.765E0 422.02E0 17.768E0 422.47E0 17.736E0 422.61E0 17.858E0 441.75E0 17.877E0 447.41E0 17.912E0 448.7E0 18.046E0 472.89E0 18.085E0 476.69E0 18.291E0 522.47E0 18.357E0 522.62E0 18.426E0 524.43E0 18.584E0 546.75E0 18.610E0 549.53E0 18.870E0 575.29E0 18.795E0 576.00E0 19.111E0 625.55E0 .367E0 20.15E0 .796E0 28.78E0 0.892E0 29.57E0 1.903E0 37.41E0 2.150E0 39.12E0 3.697E0 50.24E0 5.870E0 61.38E0 6.421E0 66.25E0 7.422E0 73.42E0 9.944E0 95.52E0 11.023E0 107.32E0 11.87E0 122.04E0 12.786E0 134.03E0 14.067E0 163.19E0 13.974E0 163.48E0 14.462E0 175.70E0 14.464E0 179.86E0 15.381E0 211.27E0 15.483E0 217.78E0 15.59E0 219.14E0 16.075E0 262.52E0 16.347E0 268.01E0 16.181E0 268.62E0 16.915E0 336.25E0 17.003E0 337.23E0 16.978E0 339.33E0 17.756E0 427.38E0 17.808E0 428.58E0 17.868E0 432.68E0 18.481E0 528.99E0 18.486E0 531.08E0 19.090E0 628.34E0 16.062E0 253.24E0 16.337E0 273.13E0 16.345E0 273.66E0 16.388E0 282.10E0 17.159E0 346.62E0 17.116E0 347.19E0 17.164E0 348.78E0 17.123E0 351.18E0 17.979E0 450.10E0 17.974E0 450.35E0 18.007E0 451.92E0 17.993E0 455.56E0 18.523E0 552.22E0 18.669E0 553.56E0 18.617E0 555.74E0 19.371E0 652.59E0 19.330E0 656.20E0 0.080E0 14.13E0 0.248E0 20.41E0 1.089E0 31.30E0 1.418E0 33.84E0 2.278E0 39.70E0 3.624E0 48.83E0 4.574E0 54.50E0 5.556E0 60.41E0 7.267E0 72.77E0 7.695E0 75.25E0 9.136E0 86.84E0 9.959E0 94.88E0 9.957E0 96.40E0 11.600E0 117.37E0 13.138E0 139.08E0 13.564E0 147.73E0 13.871E0 158.63E0 13.994E0 161.84E0 14.947E0 192.11E0 15.473E0 206.76E0 15.379E0 209.07E0 15.455E0 213.32E0 15.908E0 226.44E0 16.114E0 237.12E0 17.071E0 330.90E0 17.135E0 358.72E0 17.282E0 370.77E0 17.368E0 372.72E0 17.483E0 396.24E0 17.764E0 416.59E0 18.185E0 484.02E0 18.271E0 495.47E0 18.236E0 514.78E0 18.237E0 515.65E0 18.523E0 519.47E0 18.627E0 544.47E0 18.665E0 560.11E0 19.086E0 620.77E0 0.214E0 18.97E0 0.943E0 28.93E0 1.429E0 33.91E0 2.241E0 40.03E0 2.951E0 44.66E0 3.782E0 49.87E0 4.757E0 55.16E0 5.602E0 60.90E0 7.169E0 72.08E0 8.920E0 85.15E0 10.055E0 97.06E0 12.035E0 119.63E0 12.861E0 133.27E0 13.436E0 143.84E0 14.167E0 161.91E0 14.755E0 180.67E0 15.168E0 198.44E0 15.651E0 226.86E0 15.746E0 229.65E0 16.216E0 258.27E0 16.445E0 273.77E0 16.965E0 339.15E0 17.121E0 350.13E0 17.206E0 362.75E0 17.250E0 371.03E0 17.339E0 393.32E0 17.793E0 448.53E0 18.123E0 473.78E0 18.49E0 511.12E0 18.566E0 524.70E0 18.645E0 548.75E0 18.706E0 551.64E0 18.924E0 574.02E0 19.1E0 623.86E0 0.375E0 21.46E0 0.471E0 24.33E0 1.504E0 33.43E0 2.204E0 39.22E0 2.813E0 44.18E0 4.765E0 55.02E0 9.835E0 94.33E0 10.040E0 96.44E0 11.946E0 118.82E0 12.596E0 128.48E0 13.303E0 141.94E0 13.922E0 156.92E0 14.440E0 171.65E0 14.951E0 190.00E0 15.627E0 223.26E0 15.639E0 223.88E0 15.814E0 231.50E0 16.315E0 265.05E0 16.334E0 269.44E0 16.430E0 271.78E0 16.423E0 273.46E0 17.024E0 334.61E0 17.009E0 339.79E0 17.165E0 349.52E0 17.134E0 358.18E0 17.349E0 377.98E0 17.576E0 394.77E0 17.848E0 429.66E0 18.090E0 468.22E0 18.276E0 487.27E0 18.404E0 519.54E0 18.519E0 523.03E0 19.133E0 612.99E0 19.074E0 638.59E0 19.239E0 641.36E0 19.280E0 622.05E0 19.101E0 631.50E0 19.398E0 663.97E0 19.252E0 646.9E0 19.89E0 748.29E0 20.007E0 749.21E0 19.929E0 750.14E0 19.268E0 647.04E0 19.324E0 646.89E0 20.049E0 746.9E0 20.107E0 748.43E0 20.062E0 747.35E0 20.065E0 749.27E0 19.286E0 647.61E0 19.972E0 747.78E0 20.088E0 750.51E0 20.743E0 851.37E0 20.83E0 845.97E0 20.935E0 847.54E0 21.035E0 849.93E0 20.93E0 851.61E0 21.074E0 849.75E0 21.085E0 850.98E0 20.935E0 848.23E0 ././@LongLink100644 0 0 157 12126630646 10264 Lustar 0 0 commons-math3-3.2-src/src/test/resources/org/apache/commons/math3/optim/nonlinear/vector/jacobian/Lanczos1.datcommons-math3-3.2-src/src/test/resources/org/apache/commons/math3/optim/nonlinear/vector/jacobian/La100644 1750 1750 5604 12126627677 32331 0ustarlucluc 0 0 NIST/ITL StRD Dataset Name: Lanczos1 (Lanczos1.dat) File Format: ASCII Starting Values (lines 41 to 46) Certified Values (lines 41 to 51) Data (lines 61 to 84) Procedure: Nonlinear Least Squares Regression Description: These data are taken from an example discussed in Lanczos (1956). The data were generated to 14-digits of accuracy using f(x) = 0.0951*exp(-x) + 0.8607*exp(-3*x) + 1.5576*exp(-5*x). Reference: Lanczos, C. (1956). Applied Analysis. Englewood Cliffs, NJ: Prentice Hall, pp. 272-280. Data: 1 Response (y) 1 Predictor (x) 24 Observations Average Level of Difficulty Generated Data Model: Exponential Class 6 Parameters (b1 to b6) y = b1*exp(-b2*x) + b3*exp(-b4*x) + b5*exp(-b6*x) + e Starting values Certified Values Start 1 Start 2 Parameter Standard Deviation b1 = 1.2 0.5 9.5100000027E-02 5.3347304234E-11 b2 = 0.3 0.7 1.0000000001E+00 2.7473038179E-10 b3 = 5.6 3.6 8.6070000013E-01 1.3576062225E-10 b4 = 5.5 4.2 3.0000000002E+00 3.3308253069E-10 b5 = 6.5 4 1.5575999998E+00 1.8815731448E-10 b6 = 7.6 6.3 5.0000000001E+00 1.1057500538E-10 Residual Sum of Squares: 1.4307867721E-25 Residual Standard Deviation: 8.9156129349E-14 Degrees of Freedom: 18 Number of Observations: 24 Data: y x 2.513400000000E+00 0.000000000000E+00 2.044333373291E+00 5.000000000000E-02 1.668404436564E+00 1.000000000000E-01 1.366418021208E+00 1.500000000000E-01 1.123232487372E+00 2.000000000000E-01 9.268897180037E-01 2.500000000000E-01 7.679338563728E-01 3.000000000000E-01 6.388775523106E-01 3.500000000000E-01 5.337835317402E-01 4.000000000000E-01 4.479363617347E-01 4.500000000000E-01 3.775847884350E-01 5.000000000000E-01 3.197393199326E-01 5.500000000000E-01 2.720130773746E-01 6.000000000000E-01 2.324965529032E-01 6.500000000000E-01 1.996589546065E-01 7.000000000000E-01 1.722704126914E-01 7.500000000000E-01 1.493405660168E-01 8.000000000000E-01 1.300700206922E-01 8.500000000000E-01 1.138119324644E-01 9.000000000000E-01 1.000415587559E-01 9.500000000000E-01 8.833209084540E-02 1.000000000000E+00 7.833544019350E-02 1.050000000000E+00 6.976693743449E-02 1.100000000000E+00 6.239312536719E-02 1.150000000000E+00 commons-math3-3.2-src/src/test/resources/org/apache/commons/math3/random/testData.txt100644 1750 1750 43270 12126627677 27500 0ustarlucluc 0 0 4.038625496201205 3.6485326248346936 3.6651209675932845 5.814896279561131 5.384126469824717 5.251190723365563 4.465213440111648 4.736608014129308 5.566383814840726 3.8872277480629114 5.246598498086048 3.7511487364188176 6.733371385175343 5.388632419618035 6.036263402962769 3.8105605069222905 5.738599503606028 4.994552792425298 2.945504336988336 4.095314381239862 5.760924710543879 3.889753944419315 3.808861160991701 5.084302950012555 6.370292933705698 5.9615431859588455 4.8790354385481445 4.068164663649243 4.26491661935213 5.911067976258105 4.1316140545022115 4.0479985648577115 5.560425919351912 5.7777862258090265 4.6491664757229145 5.322284766164775 3.9643060017297818 3.3374606422520423 4.070818520139152 5.162814971692577 4.68959876937858 5.729533112912969 7.160010937980058 5.154920628387343 5.992604820701763 5.317279162752973 6.3388993007264 4.38451874656454 5.024014917973479 3.7534872319471946 5.042363342784924 5.528064915562473 4.645749024871185 2.5572755520373756 3.953716919712825 3.479482401208564 4.676100783445583 4.602236051449888 7.136692300563229 3.2411466558895095 4.188618724984135 3.6403999184454445 3.4104206071160776 4.807963390662261 4.039073733966207 4.201017826594899 4.381868005163936 5.0635235098658935 5.9840760229548735 4.195400406346137 5.649256144660377 4.679088153774095 4.169683379901892 5.671299804360453 6.159089864893807 5.315685219074694 6.3327786025390305 5.57348047674858 6.124073677151904 4.599177837977919 4.792320274522308 5.670142645005109 5.479531549270221 4.7740747976996705 4.99464290442364 5.474337090086012 5.232353744280737 6.411298157619626 4.757268066271138 5.217779158748026 5.07395379944902 5.5759984176628965 4.521182520094989 5.738026445940142 4.742204968903384 4.670762511507285 4.884925361512115 3.2573282568729462 4.548387675110013 4.337950021352034 3.7875587274144618 3.6055586455442974 7.361861332197413 4.834945622163155 6.019473054836964 4.453304225895349 3.258695681592217 5.794572588445252 3.706438851580378 6.079300672323756 4.828008457182361 5.315261102210712 3.981847695058188 4.039767325290114 5.790863349525503 5.160671471128728 4.835236459763434 4.405281184174698 6.036293520404953 5.889067983920324 4.645514887430352 4.347244670972515 6.447181244432997 6.564267268399325 5.1013992003059885 4.123378901103389 2.7101740954226283 4.209200680057913 5.085704888132955 4.26810090240086 5.54254381015526 4.721081268239747 6.890088385585999 3.9983110493877954 5.321006894021748 4.316867375040024 3.694764624577479 5.453875921043777 3.7798857421649927 3.7228653199742623 4.807698651287013 3.953745662132547 4.821189486606762 4.453489509051613 6.4517275696030225 4.823034188588044 4.722822481625316 5.810689805246644 2.79248319144007 4.928162269110059 4.661918219482871 4.574123379557206 5.241478194631993 5.8345087395944155 7.024415739565572 3.5536052565954 6.095523994939967 5.714650096778455 4.846741263282074 6.002769586957791 5.982356840660745 5.045480762532407 6.461077219605347 4.806649266171423 6.350848113862498 6.402679936682352 3.8190196431210914 4.189064677946727 4.868517260374518 2.145198341547173 5.9469461091347 5.88772432287321 4.258280909990726 6.740134075161574 4.6047650031608445 3.9273659909763774 4.291244045368331 5.183827109845055 5.696810583954774 3.608472134466666 4.169004030425733 3.9965477474540467 3.7571221568428017 5.575534565152322 5.0764208309825065 5.3185446180363485 5.157450995663762 4.961815558094033 5.687338919107788 4.185906295178724 4.382007991332045 3.5280961455873676 4.531506809760329 4.5870808989545635 4.1932173503939625 7.213813469684956 3.1814836225682908 4.647297462243001 5.223975935315364 5.585001659776854 5.120918864744974 5.026571594328509 6.348097968923147 6.50023470519147 5.712556147497515 5.206026515338916 5.749621140061565 3.0714726650374033 6.576852312067237 7.873101351668455 6.565410149266118 6.42923283018875 4.576910183319347 4.891822273316748 6.059357175219146 3.5324494806223328 5.02539429500825 6.078049839679652 4.395054417468175 5.710022806162443 5.577091376894232 3.131753802875934 5.4869998928318 6.413992453090146 6.368380820674971 6.052068461844252 5.480278219624535 7.051236227914206 4.748415087916627 4.559239556696287 4.780665068784505 5.349223485326002 4.522305152002386 5.678473361027541 6.734219964637535 6.713281662687456 6.22214905332774 5.716102543806569 6.336616632829493 4.8399311234283635 5.811391244308217 4.3965755331585905 5.297963707368242 5.021726117260926 4.497125082388555 4.735667209277485 6.048804114181307 4.264048138073914 7.181013762432201 4.781684059171574 5.779533272721499 4.164421460389599 3.6986809242837757 4.8415576143236185 4.924528568365373 4.758045335351253 5.628351489493518 5.7967639104855415 4.491988822693669 2.776750089391839 4.704679957076673 4.039607278211126 5.660350110077993 4.955611684289963 3.0641653090331107 4.896101875253389 3.6528358436331603 5.552472713484715 4.857203367367906 6.698826102960048 4.485176970803183 3.359916665137426 4.036570806638963 3.48793689188148 4.19214761961293 3.9792199677002866 6.760873252923419 4.333561067615548 5.018626873497648 3.377671327382937 4.426183834007672 8.806961710969402 5.2068790380550265 5.483008251803699 4.287267636533901 5.931330465014387 5.257260104496106 4.623908313559696 4.365112456604631 5.600514050451817 6.184093404453588 4.9116883675838485 6.019780977080248 7.485280872899538 3.5982660410679284 4.232210941334369 5.446496617538108 6.487976163896015 3.3960660696641156 4.733884295853101 5.352545256764909 4.107747627715545 3.949506252011129 5.017847997679714 4.24906287118262 6.720303792581198 5.832137142236708 5.010377506040941 6.458070081692352 6.501223021355141 4.612768564431788 3.801740464538825 4.469721893125596 5.061713024524103 6.872685648715577 6.145993249521355 4.638532388190405 4.70471535512485 6.417576222531886 4.118577249932789 4.431328683019108 4.747884983644578 4.495605382683923 3.5972439735401767 5.210796056817244 2.9160129894156026 3.4596190721900797 3.972452277026154 5.5237190584690214 6.104711796147512 4.787324556447702 4.548676032231089 6.356843891618192 3.6148998030697816 4.760679260180754 4.698041709266617 4.244003753086054 5.595546833817678 3.2784025595193267 5.326657347561453 6.213858447402109 5.213011705084566 7.232075882741927 4.806572191866972 4.680144670146755 3.946663831660007 3.6143084085883554 7.789315918667734 7.099181638561095 3.672742516732736 5.953845998789752 6.28847712720666 6.946689084108734 6.325454389782429 4.334133006331358 3.039424552213366 4.847328734611504 4.249781519880862 6.126424188695286 3.3823135936253257 6.3280255743100025 6.150431997847958 5.4226742397784005 5.94864601826791 4.425906835511732 4.897545227095195 6.26027333638832 3.647858418615367 5.276322437292433 4.176876069581277 4.346107259567459 3.1384418250329995 4.212957347421948 4.637757894698186 6.535589923573854 5.193072556110316 5.017309492685374 5.1750466093509 4.6381038872450375 6.071604634493695 4.357240286904764 5.122391923621759 6.556903940099011 3.8006848201076036 4.522363890394659 6.2456602471550635 5.829300589393535 4.452647643620209 5.371890862621703 4.948677662633424 5.661113800822228 5.773629402623548 6.139823333391851 6.093004328053013 5.362399773949667 6.915042845179306 5.394739321037944 5.141451574018252 5.053294383161769 4.9834920876470665 6.812746808763125 3.5705971688428266 4.664119854301202 6.310596552569324 5.674835228932813 3.4639740645984807 4.788956793299906 5.1005488900135845 4.534989910256703 3.931742089464332 3.572625977535623 5.374511045697475 3.859408179493194 5.767053789854141 5.1414168750827285 4.7490168496463525 7.481142748403815 4.5189492261011575 5.40235395980428 6.700234279658992 3.5063554778412183 3.9690452319798735 3.00630763890251 7.23611608840341 5.018006325958164 4.523410620276403 4.076684362167451 5.916234395538267 7.047286572236027 3.8682363461132017 4.390658924201581 4.5292330092964255 5.07906584568947 4.671213490610071 4.095193931403399 4.054590162572947 3.2227278245030027 6.132646335444107 2.8407359953623814 4.7279370282096655 5.593872406613741 3.382542536766184 5.85844025043303 6.461000354065181 3.4994741020969773 4.132683344034104 5.647894883473891 5.011301190267978 4.401435886120444 3.3496957519609927 6.119687677370172 4.644762759286699 4.5205629205178735 3.0320051244977195 4.596487037061894 5.14520534308978 5.282269168918912 5.761372455401502 4.148416743583162 6.372742039103559 5.649143130777574 5.084494528193606 4.811163551671385 3.9806520282362476 4.411511792047385 4.670987670787611 5.768451736319585 3.984558689428816 5.3293696591099975 5.413539058295544 6.40970782426591 5.481145473625602 4.36515208836978 5.161811987273001 3.963554978392394 5.098946908474979 7.786683053797615 4.927631219070586 5.524180021898693 4.523736107490982 3.557364094609177 6.128701594561169 4.2509207146594115 3.944944115965259 4.966138264389299 5.394430219583224 6.77531735530901 4.128069102169693 5.2683457909620355 3.8872836447608496 4.486696800422189 6.5335585640393825 4.916400608546338 4.270979919569207 4.311416898242187 4.498167295277512 6.132808180917634 5.1041291367018875 5.498388642491546 5.584526454067219 6.142894025331306 4.944671156061267 4.1686843376349945 4.818977261651865 6.235820918635881 3.3212806573760028 5.68435151611855 6.189749316228399 3.591267557367338 5.043902793214377 7.818905185451641 4.768708643560666 5.669288800286096 6.398657810380692 5.3717200778027605 5.2573487416126525 4.822935237131512 6.182572962936934 5.6072955002277105 3.9675191626756288 6.350858167126948 6.283995295688788 4.445782391191543 6.877548745307459 5.3208290700871315 6.09847688940267 6.434994026138841 4.32779758193763 7.2924037238697 5.419935895280957 4.288818201810987 7.242433265647824 4.947890367713541 5.916218606455959 6.490437527083841 4.617582424838291 7.957708355752131 4.879357620439287 6.103294400805588 5.639488259504568 4.335236791293937 5.202542624850618 5.4406339076225505 5.782530244910674 4.055314639567904 5.552293301411749 5.290496801505254 4.022580394801182 4.625571974654451 5.5086593656510825 4.913637297182931 4.906396844626936 6.439485089212817 5.7942799739945325 7.158136207286507 4.280431104751667 3.9206066719991517 5.127791240556268 6.70098532482022 4.657147097255419 4.524267698037553 4.647534545829241 4.839690189371444 6.798322548455047 5.094754599613737 5.916399329150566 5.767837713902285 5.294550523894544 4.161295164684424 5.233358678928891 5.546871474458429 4.897048191655597 3.939430251326603 5.005888208270397 3.2926576330038655 4.0159694347757835 5.056229917378723 6.568879235955665 4.497327615853924 4.690014685240942 4.746884105330737 4.841384111334085 4.14796180246966 5.461902744235217 5.869304766250897 3.52354738655413 5.582741221891035 4.997825621424692 5.439611672191855 4.819402835865619 5.76136287301575 6.143090288547951 5.976125217642891 6.157007787875113 4.912778652906766 6.540414953620538 3.8210262932626495 4.727149320768898 4.955255599543759 5.7983414047818265 5.167409288825197 5.059246623397723 6.965380962189423 5.531311904089661 5.4022568784996885 4.344352255655229 5.745261070226892 5.118820012265567 4.960430609470355 4.487905086804239 3.8537512154805835 4.839114062528739 5.367538410451759 6.202050661574205 4.001800559371117 6.119617239220475 3.236283913097008 5.610134770285298 5.757041556538514 4.083399027093518 5.055588718117847 4.580930359877383 6.545516697552579 5.916270431823864 3.761559453909257 6.037777237143994 7.29718541816528 4.8965176227762734 3.941358569293476 3.9289815988008847 3.2604315357316436 4.639329221347256 6.570997662310685 3.851958625190621 5.859087244914328 4.647365626452129 6.076778087850363 4.627936340272149 4.422345848512504 6.2183675417422 5.243889853389288 5.90909311946919 6.09260484846961 6.0271781583360475 6.913810502971691 5.285845705409185 5.318460367681083 5.179580543035928 4.6834977896331615 5.382546996207003 4.606307320228796 4.038858683454586 6.271279252908354 6.0668723017439365 5.713564644555386 5.144428649779485 5.2496039700779615 3.8392027391676207 4.7050632415088876 7.137275712725372 4.208879180827962 4.81480733241727 4.699941077536472 4.423440083005291 5.742161495602944 4.592506326637019 6.224046541049566 4.611093653533141 6.1166037746546165 5.904004955760586 5.589433336321981 4.57489510266225 5.500028469975376 4.382479722617695 4.257470376496386 6.373209588018213 5.375461447759072 2.8662337475774584 4.699832117278568 3.102810935311515 6.501460955698313 4.550333534073201 7.944860661426514 5.69020177364993 4.006199611798767 5.11813341012065 4.896479097282944 4.816212778128475 4.940296064591277 5.419056166663748 3.4659391357743616 7.246324501631043 5.907112751874067 5.614502217435809 4.750614593197476 7.0951293897280205 4.3819944682346055 4.958360318480322 4.962367933857186 5.715499159595656 5.220101872658552 6.088622700649866 5.491586360474799 4.656477235994459 3.8695533953710326 3.7143742249025538 3.7411936672155304 6.603415889834424 5.62928670505705 5.5959396553858785 5.6577330557176095 6.003846653929077 4.508563176717718 5.549881486113916 4.953305865372426 6.203554032873964 5.612208234244517 4.854464793588011 5.263585016371758 3.897600440182904 5.981235398882815 5.531277293213279 4.8817070690071445 3.712544699529063 3.513432242611217 5.006035295792077 7.124520658535316 3.4782033127607037 4.829578059223972 5.742892584711905 4.361333503197903 4.601687049512891 6.035189727259647 4.711273209758127 4.272043208125593 4.447702393976457 5.17487393756583 4.741015989802225 4.953808452791662 4.6645084492292765 4.276788530554644 7.325515154525428 4.602597440231014 5.082884146093998 3.068409439905545 4.809983425115099 3.8747882947708083 4.893233436073575 5.376932606908371 6.239910432522629 6.041695571547008 5.317735375674715 5.160517819092331 5.283748111202239 6.5357867130743745 5.537247902605441 5.4185896683530235 5.287616337544387 5.981700012459223 5.992385624329782 5.758772999982491 4.599744432168506 5.7237660286844605 2.5862937961454855 4.319918124665613 7.566860260437548 3.202784785619934 6.67642720284947 5.215802050091852 5.452814592454087 4.192858032386887 5.299199379721475 3.291677765243241 4.632695766333648 5.115714853147839 4.996260485718097 3.5271286032511773 4.659715887897552 6.587392147323261 5.989132075359954 3.8378063660060056 4.975951043892332 3.90853196371359 5.708783809093124 6.591895462100242 5.653528117636727 3.665428787393319 4.324537690925271 6.234413976864244 4.053504794002944 5.713371183460703 4.670243561862966 3.352660528859447 4.020147292531281 5.121933145078237 4.282377411958472 4.088770874857499 4.275716553910016 4.284046155337823 6.449567142111275 3.3275914286077084 4.837717853228399 5.261182985672333 6.073443097165901 5.40483608136289 4.690566013556853 4.222184746341714 5.790245443382679 5.020060832906476 5.576527321711127 5.340393035828579 5.301460661931292 5.076040366457228 6.296482877500045 3.037720796600903 6.321850760102656 5.701339165316606 4.991940459105436 5.758970102557518 4.322111367356909 5.721255109646473 5.511881303620453 4.9563635195228954 6.861001584068987 3.8299029968884195 4.322974731453332 5.3047403550360634 6.0756269754391825 6.117153630436378 4.5085862451026495 4.832132638553977 4.699215334058029 7.982648077178181 3.303778194960711 6.845166964779691 5.175136241842978 5.611538016661082 4.293354218279116 6.2617605857039775 4.646868778200023 5.596211970851805 6.4731028962866635 5.9737535333484795 6.411386536458501 2.7695062051965302 3.5560570906765894 5.451690061978083 6.503535887841675 4.695530301460264 4.706120568510652 2.800841111510871 5.364729318170148 5.1911558656154835 5.947415408072919 4.777513714112934 4.596459418828304 5.043317097051506 5.174749896541634 5.258257882159918 3.887023257269741 5.131383317673293 5.843231353166214 6.472487193651527 5.763704927517821 6.024396779444038 4.926879229092987 6.558645082464584 5.447575064546803 4.286751335276036 3.9071252303818644 4.618489035299945 5.088217807208579 3.808752600228301 5.861810119867259 4.033532296400091 5.74542761207288 4.925806147050348 3.679404591586196 4.05604604887352 5.87881882930846 4.513573760688276 4.915009783906388 3.654483449601882 4.912095784340134 3.3774256594506396 4.188548007093734 5.4860540834510445 4.483111427918742 6.091604204270534 4.913639044459108 6.347957296069254 5.777137740280461 4.996625717628335 3.357832000765961 4.529640780144531 6.655383658310578 5.187418414545693 5.275067584707507 5.50723064248028 4.636201988408981 4.947416066568987 7.027581910469225 4.570962245627946 5.947355941474328 4.7057667163042245 4.786943520378938 5.615852784022176 4.645129057815488 5.263882354785195 3.844951724466573 5.554260404852657 4.684091248268045 5.13336102667963 3.417837773686996 4.392489033666552 6.270027300253521 6.102372796945901 4.219653651099504 5.076173402237902 4.383422445264855 3.0437995085361025 5.377941796580727 6.276975902314367 4.315133675763909 5.507204150696545 4.886780791403244 7.147240935203286 3.900457465197911 5.102470142455588 7.084247234995372 5.457300111792919 4.60867925423519 6.2840312118540815 7.236947706509271 7.133509547170027 4.3015318378968 5.043756433592529 5.108881706267525 5.5240023845728645 5.858632364389344 5.981971317600007 6.259948473084726 4.062783955426871 5.218852203995356 3.8038254404258813 4.758585778361602 4.376196481713867 4.458880802424765 3.96326498727664 3.6778134622710104 4.374934998721925 7.489468914416122 5.700987063590436 3.3100952240676955 5.1696122166092415 6.541584919841012 4.4595571152023465 4.366611842258099 5.382259676070623 4.7794428978336825 3.757838857759169 6.545307984939696 4.881890171568036 5.7063933726311165 4.7730257133517116 3.873677842944983 3.840259191338565 4.593661080441791 4.511107632929962 5.5385052402039605 5.441167937479936 5.984890322415174 5.403820054129332 5.148546201719365 4.838476271562129 6.2440438844133075 3.9741885421050913 6.327490860577795 4.633940514497735 5.232122748521683 4.456999940494487 5.576626928088951 3.2818610857426584 5.134684374559793 4.602466559265273 5.891324885962796 5.517816321593768 6.624687761337339 5.2683180340267874 4.662418552035468 4.622236368091395 5.536060664096081 3.272870360657461 3.9899131914173696 5.121549579739896 5.928806028927443 4.259133981719825 5.313734011651727 5.635277610987355 4.524627655490917 commons-math3-3.2-src/src/test/resources/org/apache/commons/math3/random/emptyFile.txt100644 1750 1750 0 12126627677 27545 0ustarlucluc 0 0 commons-math3-3.2-src/src/test/resources/org/apache/commons/math3/linear/matrix1.csv100644 1750 1750 543774 12126627677 27320 0ustarlucluc 0 0 # # Licensed to the Apache Software Foundation (ASF) under one or more # contributor license agreements. See the NOTICE file distributed with # this work for additional information regarding copyright ownership. # The ASF licenses this file to You 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. 0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0 0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0 0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0 0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0 0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0 0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0 0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0 0.0,0.0,0.0,0.0,0.0,0.0,0.0,15.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,-0.05369909107685089,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,15.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,-1.9101381320506334,0.0,0.0,0.0,0.0,-1.944289852399379,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,-10.825810492038727,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,9.700992196798325,0.0,0.0,0.0,0.6107797464355826,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,-1.3636776264756918,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,4.12471815943718,0.0,0.0,0.0,15.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,-9.14822806417942,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,15.0,0.0 0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,7.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,2.933939576148987,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,7.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,-0.7922147512435913,0.0,0.0,0.0,0.0,-0.4924217015504837,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,-1.9659620970487595,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,-0.5492931008338928,0.0,0.0,0.0,2.3254917040467262,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,1.5845542773604393,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,2.1992932707071304,0.0,0.0,0.0,7.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,-4.932329177856445,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,7.0,0.0 0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0 0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0 0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,14.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,-15.919750303030014,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,14.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,13.889551315456629,0.0,0.0,0.0,0.0,-4.3475807420909405,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,12.123515283688903,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,10.027344062924385,0.0,0.0,0.0,-5.674978591501713,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,-6.228649392724037,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,23.427826821804047,0.0,0.0,0.0,14.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,-24.709693431854248,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,14.0,0.0 0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0 0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0 0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0 0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0 0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0 0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,4.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.5924292057752609,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,4.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.1777516007423401,0.0,0.0,0.0,0.0,-1.7908689081668854,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,-0.006518632173538208,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,-0.05682873725891113,0.0,0.0,0.0,0.6916737286373973,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.7990173548460007,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,4.991209089756012,0.0,0.0,0.0,4.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,-2.956812411546707,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,4.0,0.0 0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0 0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,27.0,0.0,0.0,0.0,0.0,0.0,0.0,-15.525352537631989,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,27.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,14.746156910434365,0.0,0.0,0.0,0.0,-10.23025938437786,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,41.51351824775338,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,-25.738689684541896,0.0,0.0,0.0,36.28327626734972,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,41.01446334272623,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,38.101964892819524,0.0,0.0,0.0,27.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,-14.896937601268291,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,27.0,0.0 0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0 0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0 0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0 0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,4.0,0.0,0.0,1.3658225238323212,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,4.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,-1.5220678448677063,0.0,0.0,0.0,0.0,-0.6681850552558899,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.9971028361469507,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.1379072517156601,0.0,0.0,0.0,-0.5579372197389603,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,3.97811359167099,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,6.163206443190575,0.0,0.0,0.0,4.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,-4.34101939201355,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,4.0,0.0 0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0 0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,3.0,-4.5461119413375854,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,3.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,1.2333791255950928,0.0,0.0,0.0,0.0,-1.4135722815990448,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,3.343593806028366,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,-2.260468453168869,0.0,0.0,0.0,5.696155428886414,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.40279530361294746,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,3.0906842947006226,0.0,0.0,0.0,3.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,-3.8722375333309174,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,3.0,0.0 0.0,0.0,0.0,0.0,0.0,0.0,0.0,-0.05369909107685089,2.933939576148987,0.0,0.0,-15.919750303030014,0.0,0.0,0.0,0.0,0.0,0.5924292057752609,0.0,-15.525352537631989,0.0,0.0,0.0,1.3658225238323212,0.0,-4.5461119413375854,59.723969265034164,0.0,0.0,0.0,-0.7298467606306076,-11.378454633057117,0.0,0.0,-2.0424426179379225,-2.8975989669561386,0.0,-1.2547471076250076,0.0,0.0,-8.893463999032974,5.034359127283096,5.414208844304085,-2.6983630806207657,0.0,0.0,-1.8376648426055908,0.0,0.0,-5.941359521821141,0.0,0.0,0.0,-8.22452313452959,0.0,0.0,0.0,-36.22796697382849,0.0,0.0,0.0,1.8312844783067703,9.856234005058894,0.0,-3.220152974128723,0.16262386739253998,-2.0599038153886795,-2.9849311485886574,0.0,0.0,2.992463782429695,0.0,-0.12134154140949249,9.249121218919754,-10.133487362414598,0.0,0.0,-4.327284762635827,-5.96094835549593,0.0,0.0,0.07027912139892578,0.0,2.149657130241394,0.0,0.0,0.6494972407817841,-45.18152792327847,4.56357329711318,0.0,0.0,-0.3297060430049896,-1.9194390177726746,0.45474639534950256,0.0,0.0,0.0,-22.560335787944496,0.0,0.0,0.0,0.0,0.0,0.6824626699090004,1.5240850895643234,-0.9086112380027771,-0.5843207538127899,0.0,0.0,0.0,0.0,0.0,11.023676510974056,-2.5292555540800095,0.0,-1.0265597105026245,-23.483331195043327,0.0,0.22352806851267815,-2.7964515686035156,0.0,0.0,0.0,0.0,-9.433988235890865,4.976907283067703,0.0,1.0395476669073105,-9.758227080106735,-14.984199861959494,0.0,0.0,0.0,0.0,0.0,0.0,0.0,-4.759576991200447,0.0,0.0,0.0,0.0,-4.38206509500742,0.0,-0.5269257053732872,0.0,0.0,-2.501417610794306,-20.591913028908735,-0.8610515296459198,0.0,0.0,-5.941359521821141,0.0,0.0,0.0,-1.0466726273298264,0.0,0.0,0.3715929687023163,0.0,0.0,0.0,-1.4377858061343431,0.0,0.0,0.0,0.0,0.0,0.0,25.22303388623684,0.0,0.0,0.0,0.4247095314785838,0.0,-0.8339303731918335,0.0,0.0,0.0,0.0,0.0,0.3806012272834778,0.0,0.0,0.0,-4.30822491645813,3.8517103642225266,0.0,0.0,3.297197386622429,-4.745578117668629,0.0,0.0,0.0,0.0,0.0,0.0,0.0,-5.941359521821141,11.014317199587822 0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0 0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0 0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0 0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,-0.7298467606306076,0.0,0.0,0.0,2.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,2.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.3390001654624939,0.0,0.0,0.0,0.0,0.0036716521717607975,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,1.0194237008690834,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,-2.299489736557007,0.0,0.0,0.0,1.4863772690296173,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,1.9788296222686768,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,1.0450767874717712,0.0,0.0,0.0,2.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,-0.8483445346355438,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,2.0,0.0 0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,-11.378454633057117,0.0,0.0,0.0,0.0,15.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,15.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,8.822441661264747,0.0,0.0,0.0,0.0,-5.482512224465609,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,30.007759630680084,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,-2.1825039191171527,0.0,0.0,0.0,3.8669294118881226,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,23.274607360363007,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,38.5399112701416,0.0,0.0,0.0,15.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,-11.941730704158545,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,15.0,0.0 0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0 0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0 0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,-2.0424426179379225,0.0,0.0,0.0,0.0,0.0,0.0,0.0,12.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,12.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,2.743435960263014,0.0,0.0,0.0,0.0,-2.1083469102159142,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.4885908514261246,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,4.124842751771212,0.0,0.0,0.0,2.2382761612534523,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,8.651659648865461,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,8.893167037516832,0.0,0.0,0.0,12.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,-13.015602797269821,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,12.0,0.0 0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,-2.8975989669561386,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,5.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,5.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,-1.6660819351673126,0.0,0.0,0.0,0.0,-0.26680618710815907,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,2.2242048233747482,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,1.4843878149986267,0.0,0.0,0.0,4.429531574249268,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,3.932315766811371,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,6.9748256504535675,0.0,0.0,0.0,5.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,-6.963641285896301,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,5.0,0.0 0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0 0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,-1.2547471076250076,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,3.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,3.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,1.570149838924408,0.0,0.0,0.0,0.0,-0.3581736087799072,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,1.1439735740423203,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.6735544987022877,0.0,0.0,0.0,0.9498915374279022,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,2.074944794178009,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,1.6044365838170052,0.0,0.0,0.0,3.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,-4.129058361053467,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,3.0,0.0 0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0 0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0 0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,-8.893463999032974,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,13.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,13.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,4.560923157259822,0.0,0.0,0.0,0.0,-2.3036855403333902,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,8.071713298559189,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.766984548419714,0.0,0.0,0.0,5.462030299007893,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,4.670862816274166,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,15.634809285402298,0.0,0.0,0.0,13.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,-14.985589444637299,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,13.0,0.0 0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,5.034359127283096,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,6.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,6.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,-2.966256022453308,0.0,0.0,0.0,0.0,0.6641822177916765,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,-2.1657295301556587,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,3.0443245023489,0.0,0.0,0.0,-0.9783360548317432,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,5.691663771867752,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,4.577853083610535,0.0,0.0,0.0,6.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,-2.0713853552006185,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,6.0,0.0 0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,5.414208844304085,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,8.0,0.0,0.0,0.0,0.0,0.0,0.0,8.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,-3.3849349431693554,0.0,0.0,0.0,0.0,-1.54612004943192,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,3.5589938163757324,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,-4.28386144246906,0.0,0.0,0.0,3.6653946340084076,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,9.043274819850922,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,14.54409345984459,0.0,0.0,0.0,8.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,-7.893590062856674,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,8.0,0.0 0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,-2.6983630806207657,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,3.0,0.0,0.0,0.0,0.0,0.0,3.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.08702540397644043,0.0,0.0,0.0,0.0,0.6113622188568115,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,2.509317994117737,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,-3.712423324584961,0.0,0.0,0.0,3.3445043861865997,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,2.9976990818977356,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,3.1233449205756187,0.0,0.0,0.0,3.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,-4.547214984893799,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,3.0,0.0 0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0 0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0 0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,-1.8376648426055908,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,1.0,0.0,0.0,1.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,2.620814561843872,0.0,0.0,0.0,0.0,-0.5935000777244568,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,3.1800248622894287,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,-0.058661606162786484,0.0,0.0,0.0,0.9526339769363403,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,1.2820285558700562,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,2.5773181915283203,0.0,0.0,0.0,1.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,-1.4816099405288696,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,1.0,0.0 0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0 0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0 0.0,0.0,0.0,0.0,0.0,0.0,0.0,15.0,7.0,0.0,0.0,14.0,0.0,0.0,0.0,0.0,0.0,4.0,0.0,27.0,0.0,0.0,0.0,4.0,0.0,3.0,-5.941359521821141,0.0,0.0,0.0,2.0,15.0,0.0,0.0,12.0,5.0,0.0,3.0,0.0,0.0,13.0,6.0,8.0,3.0,0.0,0.0,1.0,0.0,0.0,49.0,0.0,0.0,0.0,16.0,0.0,0.0,0.0,15.487914606928825,0.0,0.0,0.0,4.0,-50.57884380221367,0.0,3.0,1.0,3.0,10.0,0.0,0.0,2.0,0.0,1.0,13.0,16.0,0.0,0.0,9.0,20.0,0.0,0.0,18.0,0.0,4.0,0.0,0.0,4.0,45.52266527339816,17.0,0.0,0.0,1.0,4.0,2.0,0.0,0.0,0.0,30.0,0.0,0.0,0.0,0.0,0.0,9.0,2.0,1.0,5.0,0.0,0.0,0.0,0.0,0.0,-5.863275927491486,11.0,0.0,1.0,10.865198890911415,0.0,8.0,3.0,0.0,0.0,0.0,0.0,11.0,14.0,0.0,16.0,9.0,32.41861438378692,0.0,0.0,0.0,0.0,0.0,0.0,0.0,14.0,0.0,0.0,0.0,0.0,13.0,0.0,5.0,0.0,0.0,4.0,30.408643363043666,2.0,0.0,0.0,49.0,0.0,0.0,0.0,13.0,0.0,0.0,4.0,0.0,0.0,0.0,6.0,0.0,0.0,0.0,0.0,0.0,0.0,-47.26256303489208,0.0,0.0,0.0,8.0,0.0,3.0,0.0,0.0,0.0,0.0,0.0,2.0,0.0,0.0,0.0,4.0,12.0,0.0,0.0,15.0,14.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,49.0,19.0 0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0 0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0 0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0 0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,-8.22452313452959,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,16.0,0.0,0.0,0.0,16.0,0.0,0.0,0.0,8.520381983369589,0.0,0.0,0.0,0.0,-3.2472858242690563,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,8.746096983551979,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,-1.6934340596199036,0.0,0.0,0.0,13.688669130206108,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,10.367527099326253,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,12.485619314014912,0.0,0.0,0.0,16.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,-13.077254951000214,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,16.0,0.0 0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0 0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0 0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0 0.0,0.0,0.0,0.0,0.0,0.0,0.0,-1.9101381320506334,-0.7922147512435913,0.0,0.0,13.889551315456629,0.0,0.0,0.0,0.0,0.0,0.1777516007423401,0.0,14.746156910434365,0.0,0.0,0.0,-1.5220678448677063,0.0,1.2333791255950928,-36.22796697382849,0.0,0.0,0.0,0.3390001654624939,8.822441661264747,0.0,0.0,2.743435960263014,-1.6660819351673126,0.0,1.570149838924408,0.0,0.0,4.560923157259822,-2.966256022453308,-3.3849349431693554,0.08702540397644043,0.0,0.0,2.620814561843872,0.0,0.0,15.487914606928825,0.0,0.0,0.0,8.520381983369589,0.0,0.0,0.0,68.37338485940037,0.0,0.0,0.0,-0.22938140854239464,-17.510697191229614,0.0,-2.588969349861145,-0.11737745255231857,2.956841230392456,0.17309942096471786,0.0,0.0,-1.8252867460250854,0.0,-0.70707106590271,-3.257279295474291,9.825382009148598,0.0,0.0,0.5782127380371094,-11.795425206422806,0.0,0.0,3.192576435394585,0.0,-0.7796851620078087,0.0,0.0,-1.0513367354869843,32.46211928404682,0.6567540988326073,0.0,0.0,-0.6989614963531494,0.5034151971340179,-0.022457540035247803,0.0,0.0,0.0,11.795906279236078,0.0,0.0,0.0,0.0,0.0,-1.4281827881932259,-1.0204152762889862,1.5221600532531738,0.4326392486691475,0.0,0.0,0.0,0.0,0.0,-8.12789240972587,-1.2468826696276665,0.0,0.2407173067331314,28.260723834754714,0.0,-0.3689092621207237,5.27638566493988,0.0,0.0,0.0,0.0,-1.1437940895557404,-3.276226717978716,0.0,-4.143434092402458,3.613796576857567,17.53931002792787,0.0,0.0,0.0,0.0,0.0,0.0,0.0,2.8112213611602783,0.0,0.0,0.0,0.0,3.115424363873899,0.0,4.429781533777714,0.0,0.0,-1.1793667078018188,27.775358993467307,0.8640256449580193,0.0,0.0,15.487914606928825,0.0,0.0,0.0,1.7705781161785126,0.0,0.0,-0.5751520097255707,0.0,0.0,0.0,-0.4276372417807579,0.0,0.0,0.0,0.0,0.0,0.0,-33.82405141120651,0.0,0.0,0.0,1.8073564060032368,0.0,-0.28548621013760567,0.0,0.0,0.0,0.0,0.0,1.0631259083747864,0.0,0.0,0.0,4.212456613779068,-4.067754611372948,0.0,0.0,-4.008007885597181,-2.5974843967705965,0.0,0.0,0.0,0.0,0.0,0.0,0.0,15.487914606928825,-3.1907826489768922 0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0 0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0 0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0 0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,1.8312844783067703,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,4.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,-0.22938140854239464,0.0,0.0,0.0,4.0,0.7897312976419926,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,-2.9249361753463745,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,1.3126036152243614,0.0,0.0,0.0,-0.2644241452217102,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,3.286569356918335,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.22610758617520332,0.0,0.0,0.0,4.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,-0.03645872324705124,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,4.0,0.0 0.0,0.0,0.0,0.0,0.0,0.0,0.0,-1.944289852399379,-0.4924217015504837,0.0,0.0,-4.3475807420909405,0.0,0.0,0.0,0.0,0.0,-1.7908689081668854,0.0,-10.23025938437786,0.0,0.0,0.0,-0.6681850552558899,0.0,-1.4135722815990448,9.856234005058894,0.0,0.0,0.0,0.0036716521717607975,-5.482512224465609,0.0,0.0,-2.1083469102159142,-0.26680618710815907,0.0,-0.3581736087799072,0.0,0.0,-2.3036855403333902,0.6641822177916765,-1.54612004943192,0.6113622188568115,0.0,0.0,-0.5935000777244568,0.0,0.0,-50.57884380221367,0.0,0.0,0.0,-3.2472858242690563,0.0,0.0,0.0,-17.510697191229614,0.0,0.0,0.0,0.7897312976419926,56.26130732671025,0.0,-1.1058396426960826,-0.2675591707229614,-1.9760533049702644,-2.1143492311239243,0.0,0.0,-0.35670218616724014,0.0,-0.26638340950012207,2.3442939072847366,-5.885182909201831,0.0,0.0,-5.773140370845795,-7.22975242882967,0.0,0.0,-4.925576898269355,0.0,1.0309927463531494,0.0,0.0,0.13987640291452408,-52.81648097639531,-5.027125038206577,0.0,0.0,-0.26849356293678284,-2.5127249658107758,-2.058899998664856,0.0,0.0,0.0,-1.8283812585286796,0.0,0.0,0.0,0.0,0.0,0.7085662446916103,-0.4603450745344162,-0.41806870698928833,-1.5882601365447044,0.0,0.0,0.0,0.0,0.0,7.6476147781791255,-0.22253571450710297,0.0,-0.2778942883014679,-13.648466504916327,0.0,-2.3833787329494953,-0.9711399525403976,0.0,0.0,0.0,0.0,-2.4042814150452614,-1.9924096018075943,0.0,-3.6081300228834152,-3.712091484107077,-36.39494682085166,0.0,0.0,0.0,0.0,0.0,0.0,0.0,-7.732258439064026,0.0,0.0,0.0,0.0,-0.7158599495887756,0.0,-2.2538879215717316,0.0,0.0,-2.8160181641578674,-25.78169407018379,-0.32570601254701614,0.0,0.0,-50.57884380221367,0.0,0.0,0.0,-4.594333034008741,0.0,0.0,-2.345126211643219,0.0,0.0,0.0,-0.6275079101324081,0.0,0.0,0.0,0.0,0.0,0.0,47.260019193315024,0.0,0.0,0.0,-0.5521116554737091,0.0,-1.2495318353176117,0.0,0.0,0.0,0.0,0.0,-0.07739302515983582,0.0,0.0,0.0,-0.767902597784996,-3.403123142197728,0.0,0.0,-3.7712617572396994,-1.7017017318867147,0.0,0.0,0.0,0.0,0.0,0.0,0.0,-50.57884380221367,-6.13785407692194 0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0 0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,-3.220152974128723,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,3.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,-2.588969349861145,0.0,0.0,0.0,0.0,-1.1058396426960826,0.0,3.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,6.297359347343445,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,-3.630152106285095,0.0,0.0,0.0,3.5228464603424072,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,2.6745598316192627,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,7.731954574584961,0.0,0.0,0.0,3.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,-3.0365793257951736,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,3.0,0.0 0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.16262386739253998,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,1.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,-0.11737745255231857,0.0,0.0,0.0,0.0,-0.2675591707229614,0.0,0.0,1.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,-0.3821831941604614,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.35446348786354065,0.0,0.0,0.0,0.18042966723442078,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.5979014039039612,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.10011700540781021,0.0,0.0,0.0,1.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,-0.8390291929244995,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,1.0,0.0 0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,-2.0599038153886795,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,3.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,2.956841230392456,0.0,0.0,0.0,0.0,-1.9760533049702644,0.0,0.0,0.0,3.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,4.637997388839722,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,-5.2728272676467896,0.0,0.0,0.0,6.03791868686676,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,2.478183249011636,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,3.27279531955719,0.0,0.0,0.0,3.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,-2.564838707447052,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,3.0,0.0 0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,-2.9849311485886574,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,10.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.17309942096471786,0.0,0.0,0.0,0.0,-2.1143492311239243,0.0,0.0,0.0,0.0,10.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,4.7538832277059555,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.05187414959073067,0.0,0.0,0.0,3.954907776787877,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,4.897982493042946,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,12.357213407754898,0.0,0.0,0.0,10.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,-5.884003698825836,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,10.0,0.0 0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0 0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0 0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,2.992463782429695,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,2.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,-1.8252867460250854,0.0,0.0,0.0,0.0,-0.35670218616724014,0.0,0.0,0.0,0.0,0.0,0.0,0.0,2.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,-0.13212478160858154,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,2.1189982891082764,0.0,0.0,0.0,-1.9141854643821716,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,2.468143582344055,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,2.719468206167221,0.0,0.0,0.0,2.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,-1.1132894158363342,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,2.0,0.0 0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0 0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,-0.12134154140949249,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,1.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,-0.70707106590271,0.0,0.0,0.0,0.0,-0.26638340950012207,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,1.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.3688853979110718,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,-1.1495740413665771,0.0,0.0,0.0,0.7993773221969604,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,1.4014883041381836,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.7976486682891846,0.0,0.0,0.0,1.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,-0.7888913750648499,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,1.0,0.0 0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,9.249121218919754,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,13.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,-3.257279295474291,0.0,0.0,0.0,0.0,2.3442939072847366,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,13.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,-6.737234711647034,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,6.132676690816879,0.0,0.0,0.0,-0.9964745342731476,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,11.029102437198162,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,7.31997487694025,0.0,0.0,0.0,13.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,-4.58983613178134,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,13.0,0.0 0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,-10.133487362414598,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,16.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,9.825382009148598,0.0,0.0,0.0,0.0,-5.885182909201831,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,16.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,42.26715350151062,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,-18.735623555257916,0.0,0.0,0.0,28.485295712947845,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,35.103896737098694,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,38.54084646701813,0.0,0.0,0.0,16.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,-17.998053796589375,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,16.0,0.0 0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0 0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0 0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,-4.327284762635827,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,9.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.5782127380371094,0.0,0.0,0.0,0.0,-5.773140370845795,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,9.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,7.641948837786913,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.38436487317085266,0.0,0.0,0.0,6.711456835269928,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,5.903665721416473,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,4.272793307900429,0.0,0.0,0.0,9.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,-7.9170845448970795,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,9.0,0.0 0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,-5.96094835549593,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,20.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,-11.795425206422806,0.0,0.0,0.0,0.0,-7.22975242882967,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,20.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,11.044744558632374,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,8.589418772608042,0.0,0.0,0.0,2.1531955674290657,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,6.641412604600191,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,37.63654890283942,0.0,0.0,0.0,20.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,-11.573262097314,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,20.0,0.0 0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0 0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0 0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.07027912139892578,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,18.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,3.192576435394585,0.0,0.0,0.0,0.0,-4.925576898269355,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,18.0,0.0,0.0,0.0,0.0,0.0,2.364184159785509,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,-1.3935693129897118,0.0,0.0,0.0,11.891992576187477,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,16.80736497398175,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,13.012511916458607,0.0,0.0,0.0,18.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,-14.686583906412125,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,18.0,0.0 0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0 0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,2.149657130241394,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,4.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,-0.7796851620078087,0.0,0.0,0.0,0.0,1.0309927463531494,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,4.0,0.0,0.0,0.0,-4.080883681774139,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,1.7328047156333923,0.0,0.0,0.0,0.16516364831477404,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,1.7275835871696472,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.48328394442796707,0.0,0.0,0.0,4.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,-0.6435953266918659,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,4.0,0.0 0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0 0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0 0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.6494972407817841,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,4.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,-1.0513367354869843,0.0,0.0,0.0,0.0,0.13987640291452408,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,4.0,0.5970048755407333,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,1.142452210187912,0.0,0.0,0.0,0.4583144783973694,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,1.4315535388886929,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,2.263509437441826,0.0,0.0,0.0,4.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,-4.699591875076294,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,4.0,0.0 0.0,0.0,0.0,0.0,0.0,0.0,0.0,-10.825810492038727,-1.9659620970487595,0.0,0.0,12.123515283688903,0.0,0.0,0.0,0.0,0.0,-0.006518632173538208,0.0,41.51351824775338,0.0,0.0,0.0,0.9971028361469507,0.0,3.343593806028366,-45.18152792327847,0.0,0.0,0.0,1.0194237008690834,30.007759630680084,0.0,0.0,0.4885908514261246,2.2242048233747482,0.0,1.1439735740423203,0.0,0.0,8.071713298559189,-2.1657295301556587,3.5589938163757324,2.509317994117737,0.0,0.0,3.1800248622894287,0.0,0.0,45.52266527339816,0.0,0.0,0.0,8.746096983551979,0.0,0.0,0.0,32.46211928404682,0.0,0.0,0.0,-2.9249361753463745,-52.81648097639531,0.0,6.297359347343445,-0.3821831941604614,4.637997388839722,4.7538832277059555,0.0,0.0,-0.13212478160858154,0.0,0.3688853979110718,-6.737234711647034,42.26715350151062,0.0,0.0,7.641948837786913,11.044744558632374,0.0,0.0,2.364184159785509,0.0,-4.080883681774139,0.0,0.0,0.5970048755407333,90.83568072618726,-3.72108406573534,0.0,0.0,0.49575042724609375,3.4608424603939056,-1.171668529510498,0.0,0.0,0.0,16.664411902427673,0.0,0.0,0.0,0.0,0.0,-3.269595831632614,0.4574817568063736,1.8502564430236816,-1.4143381481990218,0.0,0.0,0.0,0.0,0.0,-19.380850127662942,2.074189405888319,0.0,1.0210665464401245,35.93753066622956,0.0,-0.9316186606884003,3.8319568037986755,0.0,0.0,0.0,0.0,18.656118988990784,-10.648532122373581,0.0,-1.189279556274414,11.174888908863068,49.21415968223115,0.0,0.0,0.0,0.0,0.0,0.0,0.0,15.85558968782425,0.0,0.0,0.0,0.0,-0.16778476163744926,0.0,1.3584699630737305,0.0,0.0,4.048084359616041,44.34375490108518,0.27173732221126556,0.0,0.0,45.52266527339816,0.0,0.0,0.0,11.575125619769096,0.0,0.0,1.810744121670723,0.0,0.0,0.0,0.1019606702029705,0.0,0.0,0.0,0.0,0.0,0.0,-61.44789180375837,0.0,0.0,0.0,-4.915014298632741,0.0,-0.34424474835395813,0.0,0.0,0.0,0.0,0.0,0.8435365781188011,0.0,0.0,0.0,10.512348890304565,-4.059596322476864,0.0,0.0,-11.893787648528814,20.965521275997162,0.0,0.0,0.0,0.0,0.0,0.0,0.0,45.52266527339816,5.7895700968801975 0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,4.56357329711318,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,17.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.6567540988326073,0.0,0.0,0.0,0.0,-5.027125038206577,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,-3.72108406573534,17.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.4510270841419697,0.0,0.0,0.0,0.8267675284296274,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,13.428745549172163,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,9.983138713985682,0.0,0.0,0.0,17.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,-9.82754484936595,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,17.0,0.0 0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0 0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0 0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,-0.3297060430049896,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,1.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,-0.6989614963531494,0.0,0.0,0.0,0.0,-0.26849356293678284,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.49575042724609375,0.0,0.0,0.0,1.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,-0.821988046169281,0.0,0.0,0.0,1.5089184045791626,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,1.6102383136749268,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.8837286233901978,0.0,0.0,0.0,1.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,-1.5975936651229858,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,1.0,0.0 0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,-1.9194390177726746,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,4.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.5034151971340179,0.0,0.0,0.0,0.0,-2.5127249658107758,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,3.4608424603939056,0.0,0.0,0.0,0.0,4.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,-0.142767533659935,0.0,0.0,0.0,0.005823120241984725,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,4.235150992870331,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,5.1057329177856445,0.0,0.0,0.0,4.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,-6.161324858665466,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,4.0,0.0 0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.45474639534950256,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,2.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,-0.022457540035247803,0.0,0.0,0.0,0.0,-2.058899998664856,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,-1.171668529510498,0.0,0.0,0.0,0.0,0.0,2.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.4813680499792099,0.0,0.0,0.0,0.22524124383926392,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.773785786703229,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,-0.38275110721588135,0.0,0.0,0.0,2.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,-1.2861803770065308,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,2.0,0.0 0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0 0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0 0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0 0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,-22.560335787944496,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,30.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,11.795906279236078,0.0,0.0,0.0,0.0,-1.8283812585286796,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,16.664411902427673,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,30.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,31.934383779764175,0.0,0.0,0.0,-16.6393408998847,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,-8.421373564749956,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,36.4477561712265,0.0,0.0,0.0,30.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,-47.59305101679638,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,30.0,0.0 0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0 0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0 0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0 0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0 0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0 0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.6824626699090004,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,9.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,-1.4281827881932259,0.0,0.0,0.0,0.0,0.7085662446916103,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,-3.269595831632614,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,9.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,1.1005427315831184,0.0,0.0,0.0,2.4518962875008583,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,7.640195727348328,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,4.762723818421364,0.0,0.0,0.0,9.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,-7.565228156745434,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,9.0,0.0 0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,1.5240850895643234,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,2.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,-1.0204152762889862,0.0,0.0,0.0,0.0,-0.4603450745344162,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.4574817568063736,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,2.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,1.5130042433738708,0.0,0.0,0.0,-0.07700902223587036,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,1.9497647285461426,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,3.1533451080322266,0.0,0.0,0.0,2.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,-0.5266745984554291,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,2.0,0.0 0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,-0.9086112380027771,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,1.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,1.5221600532531738,0.0,0.0,0.0,0.0,-0.41806870698928833,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,1.8502564430236816,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,1.0,0.0,0.0,0.0,0.0,0.0,0.0,-2.370211601257324,0.0,0.0,0.0,1.7463539838790894,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,2.0139055252075195,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,1.3579655885696411,0.0,0.0,0.0,1.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,-2.424379348754883,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,1.0,0.0 0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,-0.5843207538127899,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,5.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.4326392486691475,0.0,0.0,0.0,0.0,-1.5882601365447044,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,-1.4143381481990218,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,5.0,0.0,0.0,0.0,0.0,0.0,2.3271981785073876,0.0,0.0,0.0,1.3716582208871841,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.1348637416958809,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,5.845506966114044,0.0,0.0,0.0,5.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,-1.284853145480156,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,5.0,0.0 0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0 0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0 0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0 0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0 0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0 0.0,0.0,0.0,0.0,0.0,0.0,0.0,9.700992196798325,-0.5492931008338928,0.0,0.0,10.027344062924385,0.0,0.0,0.0,0.0,0.0,-0.05682873725891113,0.0,-25.738689684541896,0.0,0.0,0.0,0.1379072517156601,0.0,-2.260468453168869,11.023676510974056,0.0,0.0,0.0,-2.299489736557007,-2.1825039191171527,0.0,0.0,4.124842751771212,1.4843878149986267,0.0,0.6735544987022877,0.0,0.0,0.766984548419714,3.0443245023489,-4.28386144246906,-3.712423324584961,0.0,0.0,-0.058661606162786484,0.0,0.0,-5.863275927491486,0.0,0.0,0.0,-1.6934340596199036,0.0,0.0,0.0,-8.12789240972587,0.0,0.0,0.0,1.3126036152243614,7.6476147781791255,0.0,-3.630152106285095,0.35446348786354065,-5.2728272676467896,0.05187414959073067,0.0,0.0,2.1189982891082764,0.0,-1.1495740413665771,6.132676690816879,-18.735623555257916,0.0,0.0,0.38436487317085266,8.589418772608042,0.0,0.0,-1.3935693129897118,0.0,1.7328047156333923,0.0,0.0,1.142452210187912,-19.380850127662942,0.4510270841419697,0.0,0.0,-0.821988046169281,-0.142767533659935,0.4813680499792099,0.0,0.0,0.0,31.934383779764175,0.0,0.0,0.0,0.0,0.0,1.1005427315831184,1.5130042433738708,-2.370211601257324,2.3271981785073876,0.0,0.0,0.0,0.0,0.0,54.76635141805805,-1.2060311064124107,0.0,-0.5206031799316406,-35.088405856935154,0.0,0.8018278703093529,-4.040727317333221,0.0,0.0,0.0,0.0,-13.946636438369751,5.43346145004034,0.0,9.126788832247257,7.87292093783617,-24.33448004927286,0.0,0.0,0.0,0.0,0.0,0.0,0.0,-14.726583674550056,0.0,0.0,0.0,0.0,-0.4232787051005289,0.0,-3.2726379726082087,0.0,0.0,-3.264012135565281,-10.298074807210874,-0.9557259976863861,0.0,0.0,-5.863275927491486,0.0,0.0,0.0,-5.378305966034532,0.0,0.0,0.6261225193738937,0.0,0.0,0.0,2.2149713337421417,0.0,0.0,0.0,0.0,0.0,0.0,-4.871554762800333,0.0,0.0,0.0,1.3413807079195976,0.0,3.9978439807891846,0.0,0.0,0.0,0.0,0.0,-0.9155250070616603,0.0,0.0,0.0,-3.9788946509361267,1.4030203446745872,0.0,0.0,9.181831791996956,-3.2411800660192966,0.0,0.0,0.0,0.0,0.0,0.0,0.0,-5.863275927491486,1.6856045834720135 0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,-2.5292555540800095,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,11.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,-1.2468826696276665,0.0,0.0,0.0,0.0,-0.22253571450710297,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,2.074189405888319,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,-1.2060311064124107,11.0,0.0,0.0,3.579925589263439,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,6.520438566803932,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,16.54778603836894,0.0,0.0,0.0,11.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,-8.531589768826962,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,11.0,0.0 0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0 0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,-1.0265597105026245,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,1.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.2407173067331314,0.0,0.0,0.0,0.0,-0.2778942883014679,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,1.0210665464401245,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,-0.5206031799316406,0.0,0.0,1.0,2.159616231918335,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,1.8060157299041748,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.1982285976409912,0.0,0.0,0.0,1.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,-1.1241397857666016,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,1.0,0.0 0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.6107797464355826,2.3254917040467262,0.0,0.0,-5.674978591501713,0.0,0.0,0.0,0.0,0.0,0.6916737286373973,0.0,36.28327626734972,0.0,0.0,0.0,-0.5579372197389603,0.0,5.696155428886414,-23.483331195043327,0.0,0.0,0.0,1.4863772690296173,3.8669294118881226,0.0,0.0,2.2382761612534523,4.429531574249268,0.0,0.9498915374279022,0.0,0.0,5.462030299007893,-0.9783360548317432,3.6653946340084076,3.3445043861865997,0.0,0.0,0.9526339769363403,0.0,0.0,10.865198890911415,0.0,0.0,0.0,13.688669130206108,0.0,0.0,0.0,28.260723834754714,0.0,0.0,0.0,-0.2644241452217102,-13.648466504916327,0.0,3.5228464603424072,0.18042966723442078,6.03791868686676,3.954907776787877,0.0,0.0,-1.9141854643821716,0.0,0.7993773221969604,-0.9964745342731476,28.485295712947845,0.0,0.0,6.711456835269928,2.1531955674290657,0.0,0.0,11.891992576187477,0.0,0.16516364831477404,0.0,0.0,0.4583144783973694,35.93753066622956,0.8267675284296274,0.0,0.0,1.5089184045791626,0.005823120241984725,0.22524124383926392,0.0,0.0,0.0,-16.6393408998847,0.0,0.0,0.0,0.0,0.0,2.4518962875008583,-0.07700902223587036,1.7463539838790894,1.3716582208871841,0.0,0.0,0.0,0.0,0.0,-35.088405856935154,3.579925589263439,0.0,2.159616231918335,53.585876904944534,0.0,1.6903417902067304,3.622964657843113,0.0,0.0,0.0,0.0,11.951535910367966,0.7938186123501509,0.0,-2.4219329692423344,-4.137203425168991,29.617043913039357,0.0,0.0,0.0,0.0,0.0,0.0,0.0,19.32557275891304,0.0,0.0,0.0,0.0,1.3356637642136775,0.0,6.152465641498566,0.0,0.0,4.78065986931324,20.0507878993469,1.6801990866661072,0.0,0.0,10.865198890911415,0.0,0.0,0.0,3.0518488800153136,0.0,0.0,-0.2091306895017624,0.0,0.0,0.0,-2.0621166694909334,0.0,0.0,0.0,0.0,0.0,0.0,-15.86158258064966,0.0,0.0,0.0,-0.13421922735869884,0.0,-1.8267025798559189,0.0,0.0,0.0,0.0,0.0,1.5654222965240479,0.0,0.0,0.0,7.133926153182983,0.45185646787285805,0.0,0.0,2.5932632237672806,13.739510668441653,0.0,0.0,0.0,0.0,0.0,0.0,0.0,10.865198890911415,-1.379431278910488 0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0 0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.22352806851267815,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,8.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,-0.3689092621207237,0.0,0.0,0.0,0.0,-2.3833787329494953,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,-0.9316186606884003,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.8018278703093529,0.0,0.0,0.0,1.6903417902067304,0.0,8.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,3.3032003045082092,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,3.8920627534389496,0.0,0.0,0.0,8.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,-8.448899753391743,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,8.0,0.0 0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,-2.7964515686035156,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,3.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,5.27638566493988,0.0,0.0,0.0,0.0,-0.9711399525403976,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,3.8319568037986755,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,-4.040727317333221,0.0,0.0,0.0,3.622964657843113,0.0,0.0,3.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,2.3435550779104233,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,3.749082386493683,0.0,0.0,0.0,3.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,-5.580236554145813,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,3.0,0.0 0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0 0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0 0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0 0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0 0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,-9.433988235890865,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,11.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,-1.1437940895557404,0.0,0.0,0.0,0.0,-2.4042814150452614,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,18.656118988990784,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,-13.946636438369751,0.0,0.0,0.0,11.951535910367966,0.0,0.0,0.0,0.0,0.0,0.0,0.0,11.0,0.0,0.0,0.0,0.0,16.96042151749134,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,19.19667559862137,0.0,0.0,0.0,11.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,-9.203946858644485,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,11.0,0.0 0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,4.976907283067703,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,14.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,-3.276226717978716,0.0,0.0,0.0,0.0,-1.9924096018075943,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,-10.648532122373581,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,5.43346145004034,0.0,0.0,0.0,0.7938186123501509,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,14.0,0.0,0.0,0.0,6.724108835682273,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,4.045049412176013,0.0,0.0,0.0,14.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,-6.233314499258995,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,14.0,0.0 0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0 0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,1.0395476669073105,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,16.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,-4.143434092402458,0.0,0.0,0.0,0.0,-3.6081300228834152,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,-1.189279556274414,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,9.126788832247257,0.0,0.0,0.0,-2.4219329692423344,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,16.0,0.0,7.923217952251434,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,13.762362701818347,0.0,0.0,0.0,16.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,-11.520488634705544,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,16.0,0.0 0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,-9.758227080106735,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,9.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,3.613796576857567,0.0,0.0,0.0,0.0,-3.712091484107077,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,11.174888908863068,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,7.87292093783617,0.0,0.0,0.0,-4.137203425168991,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,9.0,-1.100846916437149,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,16.31670969724655,0.0,0.0,0.0,9.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,-22.875793039798737,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,9.0,0.0 0.0,0.0,0.0,0.0,0.0,0.0,0.0,-1.3636776264756918,1.5845542773604393,0.0,0.0,-6.228649392724037,0.0,0.0,0.0,0.0,0.0,0.7990173548460007,0.0,41.01446334272623,0.0,0.0,0.0,3.97811359167099,0.0,0.40279530361294746,-14.984199861959494,0.0,0.0,0.0,1.9788296222686768,23.274607360363007,0.0,0.0,8.651659648865461,3.932315766811371,0.0,2.074944794178009,0.0,0.0,4.670862816274166,5.691663771867752,9.043274819850922,2.9976990818977356,0.0,0.0,1.2820285558700562,0.0,0.0,32.41861438378692,0.0,0.0,0.0,10.367527099326253,0.0,0.0,0.0,17.53931002792787,0.0,0.0,0.0,3.286569356918335,-36.39494682085166,0.0,2.6745598316192627,0.5979014039039612,2.478183249011636,4.897982493042946,0.0,0.0,2.468143582344055,0.0,1.4014883041381836,11.029102437198162,35.103896737098694,0.0,0.0,5.903665721416473,6.641412604600191,0.0,0.0,16.80736497398175,0.0,1.7275835871696472,0.0,0.0,1.4315535388886929,49.21415968223115,13.428745549172163,0.0,0.0,1.6102383136749268,4.235150992870331,0.773785786703229,0.0,0.0,0.0,-8.421373564749956,0.0,0.0,0.0,0.0,0.0,7.640195727348328,1.9497647285461426,2.0139055252075195,0.1348637416958809,0.0,0.0,0.0,0.0,0.0,-24.33448004927286,6.520438566803932,0.0,1.8060157299041748,29.617043913039357,0.0,3.3032003045082092,2.3435550779104233,0.0,0.0,0.0,0.0,16.96042151749134,6.724108835682273,0.0,7.923217952251434,-1.100846916437149,58.838128554349026,0.0,0.0,0.0,0.0,0.0,0.0,0.0,23.635957807302475,0.0,0.0,0.0,0.0,6.754010636359453,0.0,1.5689251348376274,0.0,0.0,6.298635542392731,26.811806090807096,0.668388033285737,0.0,0.0,32.41861438378692,0.0,0.0,0.0,14.332584224641323,0.0,0.0,3.412158966064453,0.0,0.0,0.0,-0.2764994502067566,0.0,0.0,0.0,0.0,0.0,0.0,-31.720790445466267,0.0,0.0,0.0,-0.1855148058384657,0.0,-0.28693845123052597,0.0,0.0,0.0,0.0,0.0,0.3339328467845917,0.0,0.0,0.0,9.448342680931091,2.9209076315164566,0.0,0.0,-1.2627365998923779,26.732697069644928,0.0,0.0,0.0,0.0,0.0,0.0,0.0,32.41861438378692,21.781525164842606 0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0 0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0 0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0 0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0 0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0 0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0 0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0 0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,-4.759576991200447,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,14.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,2.8112213611602783,0.0,0.0,0.0,0.0,-7.732258439064026,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,15.85558968782425,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,-14.726583674550056,0.0,0.0,0.0,19.32557275891304,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,23.635957807302475,0.0,0.0,0.0,0.0,0.0,0.0,0.0,14.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,29.234968304634094,0.0,0.0,0.0,14.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,-17.53032985329628,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,14.0,0.0 0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0 0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0 0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0 0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0 0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,-4.38206509500742,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,13.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,3.115424363873899,0.0,0.0,0.0,0.0,-0.7158599495887756,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,-0.16778476163744926,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,-0.4232787051005289,0.0,0.0,0.0,1.3356637642136775,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,6.754010636359453,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,13.0,0.0,0.0,0.0,0.0,0.0,3.568841114640236,0.0,0.0,0.0,13.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,-8.83885808289051,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,13.0,0.0 0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0 0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,-0.5269257053732872,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,5.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,4.429781533777714,0.0,0.0,0.0,0.0,-2.2538879215717316,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,1.3584699630737305,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,-3.2726379726082087,0.0,0.0,0.0,6.152465641498566,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,1.5689251348376274,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,5.0,0.0,0.0,0.0,6.140632331371307,0.0,0.0,0.0,5.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,-6.655707061290741,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,5.0,0.0 0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0 0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0 0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,-2.501417610794306,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,4.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,-1.1793667078018188,0.0,0.0,0.0,0.0,-2.8160181641578674,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,4.048084359616041,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,-3.264012135565281,0.0,0.0,0.0,4.78065986931324,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,6.298635542392731,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,4.0,4.159911751747131,0.0,0.0,0.0,4.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,-5.168218493461609,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,4.0,0.0 0.0,0.0,0.0,0.0,0.0,0.0,0.0,4.12471815943718,2.1992932707071304,0.0,0.0,23.427826821804047,0.0,0.0,0.0,0.0,0.0,4.991209089756012,0.0,38.101964892819524,0.0,0.0,0.0,6.163206443190575,0.0,3.0906842947006226,-20.591913028908735,0.0,0.0,0.0,1.0450767874717712,38.5399112701416,0.0,0.0,8.893167037516832,6.9748256504535675,0.0,1.6044365838170052,0.0,0.0,15.634809285402298,4.577853083610535,14.54409345984459,3.1233449205756187,0.0,0.0,2.5773181915283203,0.0,0.0,30.408643363043666,0.0,0.0,0.0,12.485619314014912,0.0,0.0,0.0,27.775358993467307,0.0,0.0,0.0,0.22610758617520332,-25.78169407018379,0.0,7.731954574584961,0.10011700540781021,3.27279531955719,12.357213407754898,0.0,0.0,2.719468206167221,0.0,0.7976486682891846,7.31997487694025,38.54084646701813,0.0,0.0,4.272793307900429,37.63654890283942,0.0,0.0,13.012511916458607,0.0,0.48328394442796707,0.0,0.0,2.263509437441826,44.34375490108518,9.983138713985682,0.0,0.0,0.8837286233901978,5.1057329177856445,-0.38275110721588135,0.0,0.0,0.0,36.4477561712265,0.0,0.0,0.0,0.0,0.0,4.762723818421364,3.1533451080322266,1.3579655885696411,5.845506966114044,0.0,0.0,0.0,0.0,0.0,-10.298074807210874,16.54778603836894,0.0,0.1982285976409912,20.0507878993469,0.0,3.8920627534389496,3.749082386493683,0.0,0.0,0.0,0.0,19.19667559862137,4.045049412176013,0.0,13.762362701818347,16.31670969724655,26.811806090807096,0.0,0.0,0.0,0.0,0.0,0.0,0.0,29.234968304634094,0.0,0.0,0.0,0.0,3.568841114640236,0.0,6.140632331371307,0.0,0.0,4.159911751747131,64.54448175677948,1.6949809789657593,0.0,0.0,30.408643363043666,0.0,0.0,0.0,19.458940282464027,0.0,0.0,3.864152878522873,0.0,0.0,0.0,8.367504209280014,0.0,0.0,0.0,0.0,0.0,0.0,-48.42422732698638,0.0,0.0,0.0,2.9277029260993004,0.0,1.002424418926239,0.0,0.0,0.0,0.0,0.0,3.3488584756851196,0.0,0.0,0.0,9.035545706748962,9.402471881359816,0.0,0.0,3.8424890618771315,25.351953595876694,0.0,0.0,0.0,0.0,0.0,0.0,0.0,30.408643363043666,26.3805573284626 0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,-0.8610515296459198,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,2.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.8640256449580193,0.0,0.0,0.0,0.0,-0.32570601254701614,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.27173732221126556,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,-0.9557259976863861,0.0,0.0,0.0,1.6801990866661072,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.668388033285737,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,1.6949809789657593,2.0,0.0,0.0,2.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,-2.161226987838745,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,2.0,0.0 0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0 0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0 0.0,0.0,0.0,0.0,0.0,0.0,0.0,15.0,7.0,0.0,0.0,14.0,0.0,0.0,0.0,0.0,0.0,4.0,0.0,27.0,0.0,0.0,0.0,4.0,0.0,3.0,-5.941359521821141,0.0,0.0,0.0,2.0,15.0,0.0,0.0,12.0,5.0,0.0,3.0,0.0,0.0,13.0,6.0,8.0,3.0,0.0,0.0,1.0,0.0,0.0,49.0,0.0,0.0,0.0,16.0,0.0,0.0,0.0,15.487914606928825,0.0,0.0,0.0,4.0,-50.57884380221367,0.0,3.0,1.0,3.0,10.0,0.0,0.0,2.0,0.0,1.0,13.0,16.0,0.0,0.0,9.0,20.0,0.0,0.0,18.0,0.0,4.0,0.0,0.0,4.0,45.52266527339816,17.0,0.0,0.0,1.0,4.0,2.0,0.0,0.0,0.0,30.0,0.0,0.0,0.0,0.0,0.0,9.0,2.0,1.0,5.0,0.0,0.0,0.0,0.0,0.0,-5.863275927491486,11.0,0.0,1.0,10.865198890911415,0.0,8.0,3.0,0.0,0.0,0.0,0.0,11.0,14.0,0.0,16.0,9.0,32.41861438378692,0.0,0.0,0.0,0.0,0.0,0.0,0.0,14.0,0.0,0.0,0.0,0.0,13.0,0.0,5.0,0.0,0.0,4.0,30.408643363043666,2.0,0.0,0.0,49.0,0.0,0.0,0.0,13.0,0.0,0.0,4.0,0.0,0.0,0.0,6.0,0.0,0.0,0.0,0.0,0.0,0.0,-47.26256303489208,0.0,0.0,0.0,8.0,0.0,3.0,0.0,0.0,0.0,0.0,0.0,2.0,0.0,0.0,0.0,4.0,12.0,0.0,0.0,15.0,14.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,49.0,19.0 0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0 0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0 0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0 0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,-1.0466726273298264,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,13.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,1.7705781161785126,0.0,0.0,0.0,0.0,-4.594333034008741,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,11.575125619769096,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,-5.378305966034532,0.0,0.0,0.0,3.0518488800153136,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,14.332584224641323,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,19.458940282464027,0.0,0.0,0.0,13.0,0.0,0.0,0.0,13.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,-16.31555461883545,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,13.0,0.0 0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0 0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0 0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.3715929687023163,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,4.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,-0.5751520097255707,0.0,0.0,0.0,0.0,-2.345126211643219,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,1.810744121670723,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.6261225193738937,0.0,0.0,0.0,-0.2091306895017624,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,3.412158966064453,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,3.864152878522873,0.0,0.0,0.0,4.0,0.0,0.0,0.0,0.0,0.0,0.0,4.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,-2.8239628076553345,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,4.0,0.0 0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0 0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0 0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0 0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,-1.4377858061343431,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,6.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,-0.4276372417807579,0.0,0.0,0.0,0.0,-0.6275079101324081,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.1019606702029705,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,2.2149713337421417,0.0,0.0,0.0,-2.0621166694909334,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,-0.2764994502067566,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,8.367504209280014,0.0,0.0,0.0,6.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,6.0,0.0,0.0,0.0,0.0,0.0,0.0,-5.022073924541473,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,6.0,0.0 0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0 0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0 0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0 0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0 0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0 0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0 0.0,0.0,0.0,0.0,0.0,0.0,0.0,-9.14822806417942,-4.932329177856445,0.0,0.0,-24.709693431854248,0.0,0.0,0.0,0.0,0.0,-2.956812411546707,0.0,-14.896937601268291,0.0,0.0,0.0,-4.34101939201355,0.0,-3.8722375333309174,25.22303388623684,0.0,0.0,0.0,-0.8483445346355438,-11.941730704158545,0.0,0.0,-13.015602797269821,-6.963641285896301,0.0,-4.129058361053467,0.0,0.0,-14.985589444637299,-2.0713853552006185,-7.893590062856674,-4.547214984893799,0.0,0.0,-1.4816099405288696,0.0,0.0,-47.26256303489208,0.0,0.0,0.0,-13.077254951000214,0.0,0.0,0.0,-33.82405141120651,0.0,0.0,0.0,-0.03645872324705124,47.260019193315024,0.0,-3.0365793257951736,-0.8390291929244995,-2.564838707447052,-5.884003698825836,0.0,0.0,-1.1132894158363342,0.0,-0.7888913750648499,-4.58983613178134,-17.998053796589375,0.0,0.0,-7.9170845448970795,-11.573262097314,0.0,0.0,-14.686583906412125,0.0,-0.6435953266918659,0.0,0.0,-4.699591875076294,-61.44789180375837,-9.82754484936595,0.0,0.0,-1.5975936651229858,-6.161324858665466,-1.2861803770065308,0.0,0.0,0.0,-47.59305101679638,0.0,0.0,0.0,0.0,0.0,-7.565228156745434,-0.5266745984554291,-2.424379348754883,-1.284853145480156,0.0,0.0,0.0,0.0,0.0,-4.871554762800333,-8.531589768826962,0.0,-1.1241397857666016,-15.86158258064966,0.0,-8.448899753391743,-5.580236554145813,0.0,0.0,0.0,0.0,-9.203946858644485,-6.233314499258995,0.0,-11.520488634705544,-22.875793039798737,-31.720790445466267,0.0,0.0,0.0,0.0,0.0,0.0,0.0,-17.53032985329628,0.0,0.0,0.0,0.0,-8.83885808289051,0.0,-6.655707061290741,0.0,0.0,-5.168218493461609,-48.42422732698638,-2.161226987838745,0.0,0.0,-47.26256303489208,0.0,0.0,0.0,-16.31555461883545,0.0,0.0,-2.8239628076553345,0.0,0.0,0.0,-5.022073924541473,0.0,0.0,0.0,0.0,0.0,0.0,92.38808948137304,0.0,0.0,0.0,-2.6613632291555405,0.0,-4.877313137054443,0.0,0.0,0.0,0.0,0.0,0.021797165274620056,0.0,0.0,0.0,0.2755004917271435,-5.899103596806526,0.0,0.0,-8.229397162795067,-3.5643845424056053,0.0,0.0,0.0,0.0,0.0,0.0,0.0,-47.26256303489208,-18.880743823945522 0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0 0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0 0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0 0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.4247095314785838,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,8.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,1.8073564060032368,0.0,0.0,0.0,0.0,-0.5521116554737091,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,-4.915014298632741,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,1.3413807079195976,0.0,0.0,0.0,-0.13421922735869884,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,-0.1855148058384657,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,2.9277029260993004,0.0,0.0,0.0,8.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,-2.6613632291555405,0.0,0.0,0.0,8.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,8.0,0.0 0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0 0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,-0.8339303731918335,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,3.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,-0.28548621013760567,0.0,0.0,0.0,0.0,-1.2495318353176117,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,-0.34424474835395813,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,3.9978439807891846,0.0,0.0,0.0,-1.8267025798559189,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,-0.28693845123052597,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,1.002424418926239,0.0,0.0,0.0,3.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,-4.877313137054443,0.0,0.0,0.0,0.0,0.0,3.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,3.0,0.0 0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0 0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0 0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0 0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0 0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0 0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.3806012272834778,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,2.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,1.0631259083747864,0.0,0.0,0.0,0.0,-0.07739302515983582,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.8435365781188011,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,-0.9155250070616603,0.0,0.0,0.0,1.5654222965240479,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.3339328467845917,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,3.3488584756851196,0.0,0.0,0.0,2.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.021797165274620056,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,2.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,2.0,0.0 0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0 0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0 0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0 0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,-4.30822491645813,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,4.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,4.212456613779068,0.0,0.0,0.0,0.0,-0.767902597784996,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,10.512348890304565,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,-3.9788946509361267,0.0,0.0,0.0,7.133926153182983,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,9.448342680931091,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,9.035545706748962,0.0,0.0,0.0,4.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.2755004917271435,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,4.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,4.0,0.0 0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,3.8517103642225266,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,12.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,-4.067754611372948,0.0,0.0,0.0,0.0,-3.403123142197728,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,-4.059596322476864,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,1.4030203446745872,0.0,0.0,0.0,0.45185646787285805,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,2.9209076315164566,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,9.402471881359816,0.0,0.0,0.0,12.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,-5.899103596806526,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,12.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,12.0,0.0 0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0 0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0 0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,3.297197386622429,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,15.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,-4.008007885597181,0.0,0.0,0.0,0.0,-3.7712617572396994,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,-11.893787648528814,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,9.181831791996956,0.0,0.0,0.0,2.5932632237672806,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,-1.2627365998923779,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,3.8424890618771315,0.0,0.0,0.0,15.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,-8.229397162795067,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,15.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,15.0,0.0 0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,-4.745578117668629,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,14.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,-2.5974843967705965,0.0,0.0,0.0,0.0,-1.7017017318867147,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,20.965521275997162,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,-3.2411800660192966,0.0,0.0,0.0,13.739510668441653,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,26.732697069644928,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,25.351953595876694,0.0,0.0,0.0,14.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,-3.5643845424056053,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,14.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,14.0,0.0 0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0 0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0 0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0 0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0 0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0 0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0 0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0 0.0,0.0,0.0,0.0,0.0,0.0,0.0,15.0,7.0,0.0,0.0,14.0,0.0,0.0,0.0,0.0,0.0,4.0,0.0,27.0,0.0,0.0,0.0,4.0,0.0,3.0,-5.941359521821141,0.0,0.0,0.0,2.0,15.0,0.0,0.0,12.0,5.0,0.0,3.0,0.0,0.0,13.0,6.0,8.0,3.0,0.0,0.0,1.0,0.0,0.0,49.0,0.0,0.0,0.0,16.0,0.0,0.0,0.0,15.487914606928825,0.0,0.0,0.0,4.0,-50.57884380221367,0.0,3.0,1.0,3.0,10.0,0.0,0.0,2.0,0.0,1.0,13.0,16.0,0.0,0.0,9.0,20.0,0.0,0.0,18.0,0.0,4.0,0.0,0.0,4.0,45.52266527339816,17.0,0.0,0.0,1.0,4.0,2.0,0.0,0.0,0.0,30.0,0.0,0.0,0.0,0.0,0.0,9.0,2.0,1.0,5.0,0.0,0.0,0.0,0.0,0.0,-5.863275927491486,11.0,0.0,1.0,10.865198890911415,0.0,8.0,3.0,0.0,0.0,0.0,0.0,11.0,14.0,0.0,16.0,9.0,32.41861438378692,0.0,0.0,0.0,0.0,0.0,0.0,0.0,14.0,0.0,0.0,0.0,0.0,13.0,0.0,5.0,0.0,0.0,4.0,30.408643363043666,2.0,0.0,0.0,49.0,0.0,0.0,0.0,13.0,0.0,0.0,4.0,0.0,0.0,0.0,6.0,0.0,0.0,0.0,0.0,0.0,0.0,-47.26256303489208,0.0,0.0,0.0,8.0,0.0,3.0,0.0,0.0,0.0,0.0,0.0,2.0,0.0,0.0,0.0,4.0,12.0,0.0,0.0,15.0,14.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,50.0,19.0 0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,11.014317199587822,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,19.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,-3.1907826489768922,0.0,0.0,0.0,0.0,-6.13785407692194,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,5.7895700968801975,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,1.6856045834720135,0.0,0.0,0.0,-1.379431278910488,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,21.781525164842606,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,26.3805573284626,0.0,0.0,0.0,19.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,-18.880743823945522,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,19.0,19.0 commons-math3-3.2-src/src/test/resources/org/apache/commons/math3/linear/matrix2.csv100644 1750 1750 31701 12126627677 27257 0ustarlucluc 0 0 # # Licensed to the Apache Software Foundation (ASF) under one or more # contributor license agreements. See the NOTICE file distributed with # this work for additional information regarding copyright ownership. # The ASF licenses this file to You 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. 0.003000302,-0.005529946,0.01538155,-0.001403205,-0.018213096,0.000076204,-0.000023713,0.000093626,0.000143789,-0.000092728,-0.00009325,-0.000471574,0.000256266,-0.000235424,-0.000559821,-0.000181869,0.000070384,0.000005498,0.000383238,0.000187495,0.000744314,-0.000299571,0.000655884,-0.000367504,-0.000296129,0.001865159,0.002139813,0.001156764,-0.000274115,0.00194246,-0.001067201,-0.000965123,-0.000838439,-0.000233388,-0.000400203,0.000097493,0.000236697,-0.000612503,0.000248345,0.000058795,0.000121681,0.000441167,0.000328212,0.000333702,0.000739353,0.000205686,0.00011173,0.001217128,0.000488424,0.000382149,0.000308471,-0.000490277,0.000345624,-0.000834169,0.000216899,0.000433887,-0.000369019,0.000646984,-0.000300974,0.000373483,0.000647662,-0.000253848,0.000223624,0.000634534,0.000747524,-0.000077809,-0.000353221,0.000240388,0.000414233,0.000207479,0.000041459,-0.000305439,-0.000105042,0.00019835,0.000413267,0.000026096,-0.00046518,-0.000128547,-0.000043406,0.00005626,0.00000624,-0.000126197,-0.000020413,-0.000151383,-0.000024204,-0.000030944,0.000022925,-0.000064255,0.000240003,0.000262614,0.000044858,-0.000320061,-0.000030874,0.000131975,0.000079968,-0.000113183,0.000073552,-0.000008934,0.00004173,0.000052111,-0.000061729,0.000044233,0.00031579,0.000136546,0.000069494,0.000086781,-0.000040722,0.000040594,0.000105801,0.000037761,0.000010309,-0.000152556,0.000021932,0.000034306,0.000012486,-0.000005934,-0.000046878,0.000023209,0.000008168,0.000008117,-0.000016579,-0.00000505,0.000004528,-0.00000555,-0.000000095,-0.00000136,-0.000005205,-0.000002595,0.000000285,-0.000001563,-0.000000953,-0.000000364,-0.000001132,-0.000000645,-0.000000775,-0.000000144,-0.000000641,-0.00000031,-0.000000023,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 0.002969656,-0.005304577,0.015172372,-0.001362703,-0.01832869,0.000054703,-0.000122829,-0.00003114,0.000041451,0.000025999,-0.000135931,-0.000186937,0.000239001,-0.000051824,-0.000383595,-0.00012191,-0.000021933,-0.000024977,0.000385048,0.000119015,0.000599602,-0.000358632,0.000726135,-0.000131523,-0.000116199,0.001494981,0.001850129,0.000724508,-0.000261562,0.001491656,-0.000748772,-0.000627043,-0.000597073,0.000000987,-0.000220861,-0.000078266,0.000303396,-0.000299759,0.000036112,0.000047285,0.000101568,0.000350496,0.000181929,0.000083279,0.00025771,0.000026141,-0.000010124,0.000382831,0.000080824,0.000021461,0.000143501,-0.000042618,0.000111455,-0.000339422,-0.000006761,0.000101824,-0.000082054,-0.000060832,-0.000124609,0.000138409,0.000029263,-0.000077077,0.000092533,-0.000011487,0.000023891,-0.000064685,0.000033676,0.000077876,0.000019453,0.000176876,-0.000211758,0.000035019,0.000043861,-0.000043324,0.000183336,-0.000029838,-0.000109527,0.000065992,-0.000221244,-0.000014366,-0.000032553,-0.000062429,0.00004144,0.000035076,0.000062257,0.000033082,0.000005334,0.000073079,-0.000159208,-0.000239372,-0.000056981,0.000281376,-0.000009754,-0.000031414,-0.000108677,0.000193121,-0.000082145,0.000036054,-0.000074536,-0.000005391,0.000091036,-0.00005061,-0.000360727,-0.000218139,-0.000109819,-0.000100537,0.000092986,-0.00010101,-0.000154437,-0.000115903,-0.000022312,0.000308967,-0.000020773,-0.000103365,-0.000013504,0.000011012,0.000171955,-0.000173067,-0.000026495,-0.000048153,0.000161432,0.000028197,-0.000036547,0.000067851,0.000013284,0.000040178,-0.000008893,0.000015076,-0.000015242,0.000013819,0.000003256,0.000000331,0.000014678,0.00000763,0.000002552,0.000004191,0.000004197,0.000000411,-0.000000075,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 0.002887098,-0.005162996,0.014815235,-0.001285135,-0.017864999,0.00000749,-0.000086847,-0.000083018,0.000105148,0.000081389,-0.000091836,-0.000139615,0.000144603,0.000009173,-0.000390802,-0.000178339,-0.000026705,0.000019374,0.000339385,0.000106817,0.000534724,-0.000295593,0.000630123,-0.00006547,-0.000158208,0.001252922,0.001515506,0.000467301,-0.000238628,0.001181597,-0.000586928,-0.000454072,-0.000436001,0.00011785,-0.000120203,-0.000050288,0.000131089,-0.00013403,-0.000094773,0.000109751,0.000052716,0.000224419,0.000031617,-0.000005773,0.000039057,-0.000067576,-0.000073733,-0.000022599,-0.00008645,-0.000176968,-0.000001081,0.000065436,-0.000057159,0.000019626,-0.000076696,-0.000085582,0.000027081,-0.000237917,0.000027559,-0.000027321,-0.000138684,-0.000010139,0.000026879,-0.000280673,-0.000163613,-0.00006582,0.00008998,-0.000072257,-0.000129705,0.000144026,-0.000195254,0.000131956,0.000008668,-0.00004325,-0.000036238,-0.000014767,0.000003249,0.000103041,-0.000106909,-0.000013922,-0.000025123,0.000014109,0.000077397,0.000074812,0.000097137,0.000008743,-0.000015867,0.000078784,-0.000160333,-0.000220779,-0.000096102,0.000276578,0.000044677,-0.000166,-0.000113381,0.000126259,-0.000094119,0.000046776,0.000006033,-0.000019026,0.000047752,-0.000077061,-0.000245975,-0.000149312,-0.000033577,-0.000036015,0.000006292,-0.000016404,-0.000042226,0.000005486,-0.000030862,-0.000047629,-0.000008657,0.000011517,-0.00001011,-0.000010266,0.000024103,0.000041587,-0.000072901,0.00008128,-0.000182947,-0.000016937,0.000031871,-0.000074912,0.000004791,-0.000132392,0.00002209,-0.000042783,0.000027412,-0.000036629,-0.000013631,0.000006481,-0.000035375,-0.00002774,-0.000009908,-0.000015946,-0.000012614,-0.000001499,0.000000198,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 0.002831307,-0.005036083,0.014495839,-0.001222631,-0.01743462,-0.000021877,-0.000089791,-0.00008631,0.000101769,0.000084557,-0.000093566,-0.000003847,0.000103268,0.000007839,-0.000342041,-0.000176391,-0.000013103,0.000043794,0.000266074,0.00011671,0.000488465,-0.00024847,0.000460824,-0.000028753,-0.00021999,0.001021342,0.001224627,0.000221835,-0.00029908,0.000917751,-0.000398617,-0.000299065,-0.000313377,0.000213555,-0.000069759,-0.000065952,0.000058733,-0.00002764,-0.000131624,0.000092763,0.000093374,0.000102217,-0.000039354,-0.000046958,-0.000102751,-0.000104948,-0.000110721,-0.000264731,-0.000183338,-0.000194462,-0.000097041,0.000096354,-0.000152521,0.000223908,-0.00014148,-0.00020514,0.000060268,-0.000291261,0.00009264,-0.000133008,-0.000194146,-0.000000404,0.000022513,-0.000420375,-0.000274199,-0.000004157,0.000118682,-0.000145176,-0.000187359,0.000118614,-0.000190098,0.000157552,-0.000000434,0.000005534,-0.000122158,-0.000035336,0.000091302,0.000070502,-0.000035075,0.000026897,-0.000026027,0.000068863,0.000030758,0.000118906,0.000052865,-0.000026329,-0.00000826,0.000101079,-0.000197935,-0.000140233,-0.000114663,0.000170086,0.000034608,-0.000173948,-0.000106214,0.000022543,-0.000095437,0.000028632,0.000052431,0.000005499,-0.000007454,-0.000044578,-0.000000059,0.000043577,0.000018142,0.000032244,-0.000026283,0.000068926,0.000042058,0.000008931,-0.000011774,-0.000111127,-0.000011769,0.000009934,0.000014911,-0.000079832,-0.000083897,0.000157805,0.000059901,-0.000004452,-0.000123879,-0.000055225,0.000008354,-0.000052262,-0.000087207,0.000107662,0.000039901,0.000035044,0.000016752,0.000021868,0.000026419,-0.000009502,0.000036966,0.000039395,0.000019642,0.000030458,0.000013378,0.000008617,-0.000000132,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 0.002756109,-0.004887643,0.014196414,-0.00116362,-0.01703753,0.000004375,-0.000097905,-0.000069192,0.000084888,0.000113941,-0.000104082,0.000006669,0.000086462,-0.000044821,-0.000238952,-0.000135702,0.000022235,0.000037076,0.000177186,0.000152045,0.000381545,-0.000224624,0.000260298,-0.000069067,-0.000207512,0.000794191,0.00090326,0.000111248,-0.00026504,0.00064074,-0.000259072,-0.000186556,-0.000222422,0.000225984,-0.000048346,-0.000070009,0.000011544,0.000062158,-0.000140825,0.000057386,0.000083756,0.000017278,-0.000134431,-0.000146456,-0.000251551,-0.000135834,-0.000087733,-0.000452141,-0.000243808,-0.000136996,-0.000212436,0.000144639,-0.000265376,0.000421396,-0.000148677,-0.000262259,0.000043509,-0.000298101,0.000123116,-0.000182754,-0.000189525,0.000046441,-0.000032655,-0.000384988,-0.000406727,0.000091405,0.000189811,-0.000144187,-0.000117633,0.000105068,-0.000075964,0.000148894,0.00004975,0.000094222,-0.000219824,-0.000073299,0.000100216,0.000046446,-0.00002118,0.000072675,0.00004038,0.000129741,-0.000046565,0.00011456,-0.000035221,-0.0000787,0.000024227,0.000092911,-0.000123184,-0.000023855,-0.000078062,0.000047717,0.000046576,-0.000088641,-0.00002932,-0.000090071,-0.000041614,0.000003208,0.000036034,0.000008077,0.000009485,-0.000014586,0.000140279,0.000173089,0.000056949,0.000011312,-0.000016935,0.0000546,0.000071702,0.000054964,0.000038482,-0.000020307,0.000019496,-0.000076559,0.000037942,-0.000004126,-0.000097085,0.000158935,0.000084689,-0.000032025,0.000027741,-0.000001712,0.000047726,0.000067935,0.000062807,0.000042019,-0.000107553,0.000003783,-0.000068377,0.000033799,-0.000008086,-0.000014471,-0.000014571,-0.000012693,-0.000021009,-0.000043842,-0.000001391,-0.00002082,0.000000039,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 0.002713439,-0.004752147,0.013965534,-0.001108472,-0.01672038,0.000009941,-0.000112401,-0.00006527,0.000078248,0.000152419,-0.000116667,0.000033126,0.000064384,-0.00007936,-0.000147899,-0.000068426,0.000033315,0.00002579,0.000120293,0.000215288,0.000323611,-0.000171217,0.000157969,-0.00005578,-0.00015296,0.000572407,0.000688925,0.000060766,-0.00020446,0.00048807,-0.00014862,-0.00009754,-0.000180141,0.000210196,-0.000036916,-0.000060428,0.00000925,0.000081098,-0.000136197,0.000013623,0.000080662,-0.000003224,-0.000165756,-0.00019215,-0.000308542,-0.000137013,-0.000061222,-0.000510196,-0.000248039,-0.000057964,-0.000224912,0.000125232,-0.000278473,0.000514189,-0.000154484,-0.000254711,-0.000009644,-0.000268256,0.000156188,-0.000188104,-0.000139626,0.000072911,-0.00008888,-0.000233338,-0.00046348,0.000144267,0.000185848,-0.000069423,-0.000046224,0.000076921,0.000062911,0.000085604,0.000134094,0.000122251,-0.000171917,-0.000080133,0.000124002,-0.000011948,-0.00006699,0.000122377,0.000084481,0.000108265,-0.000117191,0.000053756,-0.000096093,-0.000050405,0.000012092,0.000013554,-0.000033783,0.00004524,0.000030117,-0.000098499,0.000016659,0.000051857,0.000022419,-0.000134127,0.000007665,-0.000040693,-0.000026113,-0.000036273,0.000006864,0.000076699,0.000169231,0.000164427,0.000030649,0.000033798,-0.00002345,-0.00001429,0.000047296,0.00007971,0.000069608,0.000062877,0.000017838,-0.000017769,0.00002631,0.000071317,-0.000130968,0.000042289,0.000067647,-0.000064236,0.000128106,0.000063577,0.000000735,0.000037498,0.000047856,-0.000035523,0.000035664,0.000029723,0.000045477,-0.000025745,-0.000041613,0.000047659,-0.000017681,-0.000030828,0.00001873,0.000035826,-0.000004717,0.000033014,0.000000109,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 0.002654198,-0.004617799,0.013738668,-0.00106814,-0.016441538,0.000020623,-0.000085095,-0.00006464,0.000053609,0.000186066,-0.000107783,0.000031834,0.000022983,-0.000074228,-0.000068779,-0.000039889,0.000029619,0.000041417,0.000044411,0.000255179,0.000198631,-0.000132924,0.000082586,-0.00006463,-0.000063918,0.000359364,0.000438507,0.00003992,-0.000101309,0.000328214,-0.000061309,0.000003358,-0.000096576,0.000163887,-0.000022349,-0.000050092,-0.000012869,0.000115162,-0.000111409,-0.000009761,0.000077735,0.000014956,-0.000206665,-0.000207496,-0.000314351,-0.000161785,-0.000017807,-0.000524724,-0.000276115,0.000019489,-0.000205149,0.00009544,-0.000241665,0.000578938,-0.000166919,-0.00022578,-0.000108395,-0.000231887,0.000217214,-0.000205621,-0.000083071,0.000108178,-0.000157203,0.000010994,-0.000467108,0.000166157,0.000150765,-0.000021731,0.00002444,0.000040855,0.000246309,-0.000001378,0.000270913,0.000055249,-0.000110662,-0.000061757,0.000080507,-0.000087269,-0.000080525,0.000103341,0.000076934,0.000031422,-0.000094885,0.000004692,-0.000073083,0.000050076,-0.000016419,-0.000076807,0.00008026,0.000072597,0.000083693,-0.00015694,0.00000627,0.00009672,0.000092051,-0.000114397,0.000057658,-0.000030369,-0.000037034,-0.000059939,-0.000030038,0.000095397,0.000100554,0.000056162,0.000021134,0.00002121,0.00001418,-0.000050626,0.00000043,0.000054517,0.000023516,0.000065812,0.00000895,0.000094756,-0.000010199,0.000131197,-0.00010293,-0.000061066,0.000003383,0.00000012,0.000065144,0.000028177,-0.000085212,-0.00000508,0.000002609,-0.000083369,0.0000428,-0.000049929,0.000005855,-0.000043563,0.000045236,-0.000031818,0.000035129,0.000036617,-0.000019773,0.000008113,0.000001782,-0.000039179,-0.000000243,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 commons-math3-3.2-src/src/test/resources/org/apache/commons/math3/stat/data/NumAcc4.txt100644 1750 1750 34533 12126627677 27567 0ustarlucluc 0 0 File Name: NumAcc4.dat File Format: ASCII Header : lines 1 to 60 (= 60) Certified Values: lines 41 to 43 (= 3) Data : lines 61 to 1061 (= 1001) Dataset Name: NumAcc4 Description: This is a constructed/fabricated data set to test accuracy in summary statistic calculations. The numbers are 9-digit floating point values and differ only in the last decimal place. sample mean = 10000000.2 (exact) sample standard dev. = 0.1 (exact) sample autocorr. coef. = -0.999 (exact) Stat Category: Univariate Reference: Simon, Stephen D. and Lesage, James P. (1989). Assessing the Accuracy of ANOVA Caluclations in Statistical Software", Computational Statistics & data Analysis, 8, pp. 325-332. Data: Constructed 1 Response : y 0 Predictors 1001 Observations Model: Higher Level of Difficulty 2 Parameters : mu, sigma 1 Response Variable : y 0 Predictor Variables y = mu + e Certified Values Sample Mean ybar: 10000000.2 Sample Standard Deviation (denom. = n-1) s: 0.1 Sample Autocorrelation Coefficient (lag 1) r(1): -0.999 Number of Observations: 1001 Data: Y -------------- 10000000.2 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 10000000.1 10000000.3 commons-math3-3.2-src/src/test/resources/org/apache/commons/math3/stat/data/NumAcc1.txt100644 1750 1750 3334 12126627677 27537 0ustarlucluc 0 0 File Name: NumAcc1.dat File Format: ASCII Header : lines 1 to 60 (= 60) Certified Values: lines 41 to 43 (= 3) Data : lines 61 to 63 (= 3) Dataset Name: NumAcc1 Description: This is a constructed/fabricated data set to test accuracy in summary statistic calculations. The numbers are large (8-digit integers) and differ only in the last decimal place. Note--by construction, this data set has sample mean = 10000002 (exact) sample standard deviation = 1 (exact) sample autocorrelation coef. = -0.5 (exact) Stat Category: Univariate: Summary Statistics Reference: Simon, Stephen D. and Lesage, James P. (1989). Assessing the Accuracy of ANOVA Caluclations in Statistical Software", Computational Statistics & data Analysis, 8, pp. 325-332. Data: Constructed 1 Response : y 0 Predictors 3 Observations Model: Lower Level of Difficulty 2 Parameters : mu, sigma 1 Response Variable : y 0 Predictor Variables y = mu + e Certified Values Sample Mean ybar: 10000002 Sample Standard Deviation (denom. = n-1) s: 1 Sample Autocorrelation Coefficient (lag 1) r(1): -0.5 Number of Observations: 3 Data: Y --------- 10000001 10000003 10000002 commons-math3-3.2-src/src/test/resources/org/apache/commons/math3/stat/data/Lottery.txt100644 1750 1750 6777 12126627677 27770 0ustarlucluc 0 0 ##################################################################### # Dataset Name: Lottery # #Description: This is an observed/"real world" data set # consisting of 218 lottery values # from September 3, 1989 to April 14, 1990 (32 weeks). # One 3-digit random number (from 000 to 999) # is drawn per day, 7 days per week for most # weeks, but fewer days per week for some weeks. # We here use this data to test accuracy # in summary statistics calculations. # # Stat Category: Univariate: Summary Statistics # # Reference: http://www.itl.nist.gov/div898/strd/univ/lottery.html # # Data: "Real World" # 1 Response : y = 3-digit random number # 0 Predictors # 218 Observations # # Model: Lower Level of Difficulty # 2 Parameters : mu, sigma # 1 Response Variable : y # 0 Predictor Variables# # y = mu + e ##################################################################### ##################################################################### # # Certified Values # ##################################################################### mean = 518.958715596330 standardDeviation = 291.699727470969 autocorrelationCoefficient = -0.120948622967393 n = 218 ##################################################################### # # Data # ##################################################################### 162 671 933 414 788 730 817 33 536 875 670 236 473 167 877 980 316 950 456 92 517 557 956 954 104 178 794 278 147 773 437 435 502 610 582 780 689 562 964 791 28 97 848 281 858 538 660 972 671 613 867 448 738 966 139 636 847 659 754 243 122 455 195 968 793 59 730 361 574 522 97 762 431 158 429 414 22 629 788 999 187 215 810 782 47 34 108 986 25 644 829 630 315 567 919 331 207 412 242 607 668 944 749 168 864 442 533 805 372 63 458 777 416 340 436 140 919 350 510 572 905 900 85 389 473 758 444 169 625 692 140 897 672 288 312 860 724 226 884 508 976 741 476 417 831 15 318 432 241 114 799 955 833 358 935 146 630 830 440 642 356 373 271 715 367 393 190 669 8 861 108 795 269 590 326 866 64 523 862 840 219 382 998 4 628 305 747 247 34 747 729 645 856 974 24 568 24 694 608 480 410 729 947 293 53 930 223 203 677 227 62 455 387 318 562 242 428 968 commons-math3-3.2-src/src/test/resources/org/apache/commons/math3/stat/data/NumAcc2.txt100644 1750 1750 24727 12126627677 27571 0ustarlucluc 0 0 File Name: NumAcc2.dat File Format: ASCII Header : lines 1 to 60 (= 60) Certified Values: lines 41 to 43 (= 3) Data : lines 61 to 1061 (= 1001) Dataset Name: NumAcc2 Description: This is a constructed/fabricated data set to test accuracy in summary statistic calculations. The numbers are 2-digit floating point values and differ only in the last decimal place. Note--by construction, this data set has sample mean = 1.2 (exact) sample standard deviation = 0.1 (exact) sample autocorrelation coef. = -0.999 (exact) Stat Category: Univariate Reference: Simon, Stephen D. and Lesage, James P. (1989). Assessing the Accuracy of ANOVA Caluclations in Statistical Software", Computational Statistics & data Analysis, 8, pp. 325-332. Data: Constructed 1 Response : y 0 Predictors 1001 Observations Model: Average Level of Difficulty 2 Parameters : mu, sigma 1 Response Variable : y 0 Predictor Variables y = mu + e Certified Values Sample Mean ybar: 1.2 Sample Standard Deviation (denom. = n-1) s: 0.1 Sample Autocorrelation Coefficient (lag 1) r(1): -0.999 Number of Observations: 1001 Data: Y --------- 1.2 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 1.1 1.3 commons-math3-3.2-src/src/test/resources/org/apache/commons/math3/stat/data/PiDigits.txt100644 1750 1750 120645 12126627677 30071 0ustarlucluc 0 0 File Name: PiDigits.dat File Format: ASCII Header : lines 1 to 60 (= 60) Certified Values: lines 41 to 43 (= 3) Data : lines 61 to 5060 (= 5000) Dataset Name: PiDigits Description: This is a constructed/fabricated data set to test accuracy in summary statistic calculations. The numbers are the first 5000 digits of the mathematical constant pi (= 3.1415926535897932384...). Stat Category: Univariate Reference: Mathematics of Computation. January 1962, page 76. Data: Constructed Variable --> 1 Response : y = pi digits --> 0 Predictors --> 5000 Observations Model: Lower Level of Difficulty --> 2 Parameters : mu, sigma --> 1 Response Variable : y --> 0 Predictor Variables y = mu + e Certified Values Sample Mean ybar: 4.53480000000000 Sample Standard Deviation (denom. = n-1) s: 2.86733906028871 Sample Autocorrelation Coefficient (lag 1) r(1): -0.00355099287237972 Number of Observations: 5000 Data: Y --------- 3 1 4 1 5 9 2 6 5 3 5 8 9 7 9 3 2 3 8 4 6 2 6 4 3 3 8 3 2 7 9 5 0 2 8 8 4 1 9 7 1 6 9 3 9 9 3 7 5 1 0 5 8 2 0 9 7 4 9 4 4 5 9 2 3 0 7 8 1 6 4 0 6 2 8 6 2 0 8 9 9 8 6 2 8 0 3 4 8 2 5 3 4 2 1 1 7 0 6 7 9 8 2 1 4 8 0 8 6 5 1 3 2 8 2 3 0 6 6 4 7 0 9 3 8 4 4 6 0 9 5 5 0 5 8 2 2 3 1 7 2 5 3 5 9 4 0 8 1 2 8 4 8 1 1 1 7 4 5 0 2 8 4 1 0 2 7 0 1 9 3 8 5 2 1 1 0 5 5 5 9 6 4 4 6 2 2 9 4 8 9 5 4 9 3 0 3 8 1 9 6 4 4 2 8 8 1 0 9 7 5 6 6 5 9 3 3 4 4 6 1 2 8 4 7 5 6 4 8 2 3 3 7 8 6 7 8 3 1 6 5 2 7 1 2 0 1 9 0 9 1 4 5 6 4 8 5 6 6 9 2 3 4 6 0 3 4 8 6 1 0 4 5 4 3 2 6 6 4 8 2 1 3 3 9 3 6 0 7 2 6 0 2 4 9 1 4 1 2 7 3 7 2 4 5 8 7 0 0 6 6 0 6 3 1 5 5 8 8 1 7 4 8 8 1 5 2 0 9 2 0 9 6 2 8 2 9 2 5 4 0 9 1 7 1 5 3 6 4 3 6 7 8 9 2 5 9 0 3 6 0 0 1 1 3 3 0 5 3 0 5 4 8 8 2 0 4 6 6 5 2 1 3 8 4 1 4 6 9 5 1 9 4 1 5 1 1 6 0 9 4 3 3 0 5 7 2 7 0 3 6 5 7 5 9 5 9 1 9 5 3 0 9 2 1 8 6 1 1 7 3 8 1 9 3 2 6 1 1 7 9 3 1 0 5 1 1 8 5 4 8 0 7 4 4 6 2 3 7 9 9 6 2 7 4 9 5 6 7 3 5 1 8 8 5 7 5 2 7 2 4 8 9 1 2 2 7 9 3 8 1 8 3 0 1 1 9 4 9 1 2 9 8 3 3 6 7 3 3 6 2 4 4 0 6 5 6 6 4 3 0 8 6 0 2 1 3 9 4 9 4 6 3 9 5 2 2 4 7 3 7 1 9 0 7 0 2 1 7 9 8 6 0 9 4 3 7 0 2 7 7 0 5 3 9 2 1 7 1 7 6 2 9 3 1 7 6 7 5 2 3 8 4 6 7 4 8 1 8 4 6 7 6 6 9 4 0 5 1 3 2 0 0 0 5 6 8 1 2 7 1 4 5 2 6 3 5 6 0 8 2 7 7 8 5 7 7 1 3 4 2 7 5 7 7 8 9 6 0 9 1 7 3 6 3 7 1 7 8 7 2 1 4 6 8 4 4 0 9 0 1 2 2 4 9 5 3 4 3 0 1 4 6 5 4 9 5 8 5 3 7 1 0 5 0 7 9 2 2 7 9 6 8 9 2 5 8 9 2 3 5 4 2 0 1 9 9 5 6 1 1 2 1 2 9 0 2 1 9 6 0 8 6 4 0 3 4 4 1 8 1 5 9 8 1 3 6 2 9 7 7 4 7 7 1 3 0 9 9 6 0 5 1 8 7 0 7 2 1 1 3 4 9 9 9 9 9 9 8 3 7 2 9 7 8 0 4 9 9 5 1 0 5 9 7 3 1 7 3 2 8 1 6 0 9 6 3 1 8 5 9 5 0 2 4 4 5 9 4 5 5 3 4 6 9 0 8 3 0 2 6 4 2 5 2 2 3 0 8 2 5 3 3 4 4 6 8 5 0 3 5 2 6 1 9 3 1 1 8 8 1 7 1 0 1 0 0 0 3 1 3 7 8 3 8 7 5 2 8 8 6 5 8 7 5 3 3 2 0 8 3 8 1 4 2 0 6 1 7 1 7 7 6 6 9 1 4 7 3 0 3 5 9 8 2 5 3 4 9 0 4 2 8 7 5 5 4 6 8 7 3 1 1 5 9 5 6 2 8 6 3 8 8 2 3 5 3 7 8 7 5 9 3 7 5 1 9 5 7 7 8 1 8 5 7 7 3 0 5 3 2 1 7 1 2 2 6 8 0 6 6 1 3 0 0 1 9 2 7 8 7 6 6 1 1 1 9 5 9 0 9 2 1 6 4 2 0 1 9 8 9 3 8 0 9 5 2 5 7 2 0 1 0 6 5 4 8 5 8 6 3 2 7 8 8 6 5 9 3 6 1 5 3 3 8 1 8 2 7 9 6 8 2 3 0 3 0 1 9 5 2 0 3 5 3 0 1 8 5 2 9 6 8 9 9 5 7 7 3 6 2 2 5 9 9 4 1 3 8 9 1 2 4 9 7 2 1 7 7 5 2 8 3 4 7 9 1 3 1 5 1 5 5 7 4 8 5 7 2 4 2 4 5 4 1 5 0 6 9 5 9 5 0 8 2 9 5 3 3 1 1 6 8 6 1 7 2 7 8 5 5 8 8 9 0 7 5 0 9 8 3 8 1 7 5 4 6 3 7 4 6 4 9 3 9 3 1 9 2 5 5 0 6 0 4 0 0 9 2 7 7 0 1 6 7 1 1 3 9 0 0 9 8 4 8 8 2 4 0 1 2 8 5 8 3 6 1 6 0 3 5 6 3 7 0 7 6 6 0 1 0 4 7 1 0 1 8 1 9 4 2 9 5 5 5 9 6 1 9 8 9 4 6 7 6 7 8 3 7 4 4 9 4 4 8 2 5 5 3 7 9 7 7 4 7 2 6 8 4 7 1 0 4 0 4 7 5 3 4 6 4 6 2 0 8 0 4 6 6 8 4 2 5 9 0 6 9 4 9 1 2 9 3 3 1 3 6 7 7 0 2 8 9 8 9 1 5 2 1 0 4 7 5 2 1 6 2 0 5 6 9 6 6 0 2 4 0 5 8 0 3 8 1 5 0 1 9 3 5 1 1 2 5 3 3 8 2 4 3 0 0 3 5 5 8 7 6 4 0 2 4 7 4 9 6 4 7 3 2 6 3 9 1 4 1 9 9 2 7 2 6 0 4 2 6 9 9 2 2 7 9 6 7 8 2 3 5 4 7 8 1 6 3 6 0 0 9 3 4 1 7 2 1 6 4 1 2 1 9 9 2 4 5 8 6 3 1 5 0 3 0 2 8 6 1 8 2 9 7 4 5 5 5 7 0 6 7 4 9 8 3 8 5 0 5 4 9 4 5 8 8 5 8 6 9 2 6 9 9 5 6 9 0 9 2 7 2 1 0 7 9 7 5 0 9 3 0 2 9 5 5 3 2 1 1 6 5 3 4 4 9 8 7 2 0 2 7 5 5 9 6 0 2 3 6 4 8 0 6 6 5 4 9 9 1 1 9 8 8 1 8 3 4 7 9 7 7 5 3 5 6 6 3 6 9 8 0 7 4 2 6 5 4 2 5 2 7 8 6 2 5 5 1 8 1 8 4 1 7 5 7 4 6 7 2 8 9 0 9 7 7 7 7 2 7 9 3 8 0 0 0 8 1 6 4 7 0 6 0 0 1 6 1 4 5 2 4 9 1 9 2 1 7 3 2 1 7 2 1 4 7 7 2 3 5 0 1 4 1 4 4 1 9 7 3 5 6 8 5 4 8 1 6 1 3 6 1 1 5 7 3 5 2 5 5 2 1 3 3 4 7 5 7 4 1 8 4 9 4 6 8 4 3 8 5 2 3 3 2 3 9 0 7 3 9 4 1 4 3 3 3 4 5 4 7 7 6 2 4 1 6 8 6 2 5 1 8 9 8 3 5 6 9 4 8 5 5 6 2 0 9 9 2 1 9 2 2 2 1 8 4 2 7 2 5 5 0 2 5 4 2 5 6 8 8 7 6 7 1 7 9 0 4 9 4 6 0 1 6 5 3 4 6 6 8 0 4 9 8 8 6 2 7 2 3 2 7 9 1 7 8 6 0 8 5 7 8 4 3 8 3 8 2 7 9 6 7 9 7 6 6 8 1 4 5 4 1 0 0 9 5 3 8 8 3 7 8 6 3 6 0 9 5 0 6 8 0 0 6 4 2 2 5 1 2 5 2 0 5 1 1 7 3 9 2 9 8 4 8 9 6 0 8 4 1 2 8 4 8 8 6 2 6 9 4 5 6 0 4 2 4 1 9 6 5 2 8 5 0 2 2 2 1 0 6 6 1 1 8 6 3 0 6 7 4 4 2 7 8 6 2 2 0 3 9 1 9 4 9 4 5 0 4 7 1 2 3 7 1 3 7 8 6 9 6 0 9 5 6 3 6 4 3 7 1 9 1 7 2 8 7 4 6 7 7 6 4 6 5 7 5 7 3 9 6 2 4 1 3 8 9 0 8 6 5 8 3 2 6 4 5 9 9 5 8 1 3 3 9 0 4 7 8 0 2 7 5 9 0 0 9 9 4 6 5 7 6 4 0 7 8 9 5 1 2 6 9 4 6 8 3 9 8 3 5 2 5 9 5 7 0 9 8 2 5 8 2 2 6 2 0 5 2 2 4 8 9 4 0 7 7 2 6 7 1 9 4 7 8 2 6 8 4 8 2 6 0 1 4 7 6 9 9 0 9 0 2 6 4 0 1 3 6 3 9 4 4 3 7 4 5 5 3 0 5 0 6 8 2 0 3 4 9 6 2 5 2 4 5 1 7 4 9 3 9 9 6 5 1 4 3 1 4 2 9 8 0 9 1 9 0 6 5 9 2 5 0 9 3 7 2 2 1 6 9 6 4 6 1 5 1 5 7 0 9 8 5 8 3 8 7 4 1 0 5 9 7 8 8 5 9 5 9 7 7 2 9 7 5 4 9 8 9 3 0 1 6 1 7 5 3 9 2 8 4 6 8 1 3 8 2 6 8 6 8 3 8 6 8 9 4 2 7 7 4 1 5 5 9 9 1 8 5 5 9 2 5 2 4 5 9 5 3 9 5 9 4 3 1 0 4 9 9 7 2 5 2 4 6 8 0 8 4 5 9 8 7 2 7 3 6 4 4 6 9 5 8 4 8 6 5 3 8 3 6 7 3 6 2 2 2 6 2 6 0 9 9 1 2 4 6 0 8 0 5 1 2 4 3 8 8 4 3 9 0 4 5 1 2 4 4 1 3 6 5 4 9 7 6 2 7 8 0 7 9 7 7 1 5 6 9 1 4 3 5 9 9 7 7 0 0 1 2 9 6 1 6 0 8 9 4 4 1 6 9 4 8 6 8 5 5 5 8 4 8 4 0 6 3 5 3 4 2 2 0 7 2 2 2 5 8 2 8 4 8 8 6 4 8 1 5 8 4 5 6 0 2 8 5 0 6 0 1 6 8 4 2 7 3 9 4 5 2 2 6 7 4 6 7 6 7 8 8 9 5 2 5 2 1 3 8 5 2 2 5 4 9 9 5 4 6 6 6 7 2 7 8 2 3 9 8 6 4 5 6 5 9 6 1 1 6 3 5 4 8 8 6 2 3 0 5 7 7 4 5 6 4 9 8 0 3 5 5 9 3 6 3 4 5 6 8 1 7 4 3 2 4 1 1 2 5 1 5 0 7 6 0 6 9 4 7 9 4 5 1 0 9 6 5 9 6 0 9 4 0 2 5 2 2 8 8 7 9 7 1 0 8 9 3 1 4 5 6 6 9 1 3 6 8 6 7 2 2 8 7 4 8 9 4 0 5 6 0 1 0 1 5 0 3 3 0 8 6 1 7 9 2 8 6 8 0 9 2 0 8 7 4 7 6 0 9 1 7 8 2 4 9 3 8 5 8 9 0 0 9 7 1 4 9 0 9 6 7 5 9 8 5 2 6 1 3 6 5 5 4 9 7 8 1 8 9 3 1 2 9 7 8 4 8 2 1 6 8 2 9 9 8 9 4 8 7 2 2 6 5 8 8 0 4 8 5 7 5 6 4 0 1 4 2 7 0 4 7 7 5 5 5 1 3 2 3 7 9 6 4 1 4 5 1 5 2 3 7 4 6 2 3 4 3 6 4 5 4 2 8 5 8 4 4 4 7 9 5 2 6 5 8 6 7 8 2 1 0 5 1 1 4 1 3 5 4 7 3 5 7 3 9 5 2 3 1 1 3 4 2 7 1 6 6 1 0 2 1 3 5 9 6 9 5 3 6 2 3 1 4 4 2 9 5 2 4 8 4 9 3 7 1 8 7 1 1 0 1 4 5 7 6 5 4 0 3 5 9 0 2 7 9 9 3 4 4 0 3 7 4 2 0 0 7 3 1 0 5 7 8 5 3 9 0 6 2 1 9 8 3 8 7 4 4 7 8 0 8 4 7 8 4 8 9 6 8 3 3 2 1 4 4 5 7 1 3 8 6 8 7 5 1 9 4 3 5 0 6 4 3 0 2 1 8 4 5 3 1 9 1 0 4 8 4 8 1 0 0 5 3 7 0 6 1 4 6 8 0 6 7 4 9 1 9 2 7 8 1 9 1 1 9 7 9 3 9 9 5 2 0 6 1 4 1 9 6 6 3 4 2 8 7 5 4 4 4 0 6 4 3 7 4 5 1 2 3 7 1 8 1 9 2 1 7 9 9 9 8 3 9 1 0 1 5 9 1 9 5 6 1 8 1 4 6 7 5 1 4 2 6 9 1 2 3 9 7 4 8 9 4 0 9 0 7 1 8 6 4 9 4 2 3 1 9 6 1 5 6 7 9 4 5 2 0 8 0 9 5 1 4 6 5 5 0 2 2 5 2 3 1 6 0 3 8 8 1 9 3 0 1 4 2 0 9 3 7 6 2 1 3 7 8 5 5 9 5 6 6 3 8 9 3 7 7 8 7 0 8 3 0 3 9 0 6 9 7 9 2 0 7 7 3 4 6 7 2 2 1 8 2 5 6 2 5 9 9 6 6 1 5 0 1 4 2 1 5 0 3 0 6 8 0 3 8 4 4 7 7 3 4 5 4 9 2 0 2 6 0 5 4 1 4 6 6 5 9 2 5 2 0 1 4 9 7 4 4 2 8 5 0 7 3 2 5 1 8 6 6 6 0 0 2 1 3 2 4 3 4 0 8 8 1 9 0 7 1 0 4 8 6 3 3 1 7 3 4 6 4 9 6 5 1 4 5 3 9 0 5 7 9 6 2 6 8 5 6 1 0 0 5 5 0 8 1 0 6 6 5 8 7 9 6 9 9 8 1 6 3 5 7 4 7 3 6 3 8 4 0 5 2 5 7 1 4 5 9 1 0 2 8 9 7 0 6 4 1 4 0 1 1 0 9 7 1 2 0 6 2 8 0 4 3 9 0 3 9 7 5 9 5 1 5 6 7 7 1 5 7 7 0 0 4 2 0 3 3 7 8 6 9 9 3 6 0 0 7 2 3 0 5 5 8 7 6 3 1 7 6 3 5 9 4 2 1 8 7 3 1 2 5 1 4 7 1 2 0 5 3 2 9 2 8 1 9 1 8 2 6 1 8 6 1 2 5 8 6 7 3 2 1 5 7 9 1 9 8 4 1 4 8 4 8 8 2 9 1 6 4 4 7 0 6 0 9 5 7 5 2 7 0 6 9 5 7 2 2 0 9 1 7 5 6 7 1 1 6 7 2 2 9 1 0 9 8 1 6 9 0 9 1 5 2 8 0 1 7 3 5 0 6 7 1 2 7 4 8 5 8 3 2 2 2 8 7 1 8 3 5 2 0 9 3 5 3 9 6 5 7 2 5 1 2 1 0 8 3 5 7 9 1 5 1 3 6 9 8 8 2 0 9 1 4 4 4 2 1 0 0 6 7 5 1 0 3 3 4 6 7 1 1 0 3 1 4 1 2 6 7 1 1 1 3 6 9 9 0 8 6 5 8 5 1 6 3 9 8 3 1 5 0 1 9 7 0 1 6 5 1 5 1 1 6 8 5 1 7 1 4 3 7 6 5 7 6 1 8 3 5 1 5 5 6 5 0 8 8 4 9 0 9 9 8 9 8 5 9 9 8 2 3 8 7 3 4 5 5 2 8 3 3 1 6 3 5 5 0 7 6 4 7 9 1 8 5 3 5 8 9 3 2 2 6 1 8 5 4 8 9 6 3 2 1 3 2 9 3 3 0 8 9 8 5 7 0 6 4 2 0 4 6 7 5 2 5 9 0 7 0 9 1 5 4 8 1 4 1 6 5 4 9 8 5 9 4 6 1 6 3 7 1 8 0 2 7 0 9 8 1 9 9 4 3 0 9 9 2 4 4 8 8 9 5 7 5 7 1 2 8 2 8 9 0 5 9 2 3 2 3 3 2 6 0 9 7 2 9 9 7 1 2 0 8 4 4 3 3 5 7 3 2 6 5 4 8 9 3 8 2 3 9 1 1 9 3 2 5 9 7 4 6 3 6 6 7 3 0 5 8 3 6 0 4 1 4 2 8 1 3 8 8 3 0 3 2 0 3 8 2 4 9 0 3 7 5 8 9 8 5 2 4 3 7 4 4 1 7 0 2 9 1 3 2 7 6 5 6 1 8 0 9 3 7 7 3 4 4 4 0 3 0 7 0 7 4 6 9 2 1 1 2 0 1 9 1 3 0 2 0 3 3 0 3 8 0 1 9 7 6 2 1 1 0 1 1 0 0 4 4 9 2 9 3 2 1 5 1 6 0 8 4 2 4 4 4 8 5 9 6 3 7 6 6 9 8 3 8 9 5 2 2 8 6 8 4 7 8 3 1 2 3 5 5 2 6 5 8 2 1 3 1 4 4 9 5 7 6 8 5 7 2 6 2 4 3 3 4 4 1 8 9 3 0 3 9 6 8 6 4 2 6 2 4 3 4 1 0 7 7 3 2 2 6 9 7 8 0 2 8 0 7 3 1 8 9 1 5 4 4 1 1 0 1 0 4 4 6 8 2 3 2 5 2 7 1 6 2 0 1 0 5 2 6 5 2 2 7 2 1 1 1 6 6 0 3 9 6 6 6 5 5 7 3 0 9 2 5 4 7 1 1 0 5 5 7 8 5 3 7 6 3 4 6 6 8 2 0 6 5 3 1 0 9 8 9 6 5 2 6 9 1 8 6 2 0 5 6 4 7 6 9 3 1 2 5 7 0 5 8 6 3 5 6 6 2 0 1 8 5 5 8 1 0 0 7 2 9 3 6 0 6 5 9 8 7 6 4 8 6 1 1 7 9 1 0 4 5 3 3 4 8 8 5 0 3 4 6 1 1 3 6 5 7 6 8 6 7 5 3 2 4 9 4 4 1 6 6 8 0 3 9 6 2 6 5 7 9 7 8 7 7 1 8 5 5 6 0 8 4 5 5 2 9 6 5 4 1 2 6 6 5 4 0 8 5 3 0 6 1 4 3 4 4 4 3 1 8 5 8 6 7 6 9 7 5 1 4 5 6 6 1 4 0 6 8 0 0 7 0 0 2 3 7 8 7 7 6 5 9 1 3 4 4 0 1 7 1 2 7 4 9 4 7 0 4 2 0 5 6 2 2 3 0 5 3 8 9 9 4 5 6 1 3 1 4 0 7 1 1 2 7 0 0 0 4 0 7 8 5 4 7 3 3 2 6 9 9 3 9 0 8 1 4 5 4 6 6 4 6 4 5 8 8 0 7 9 7 2 7 0 8 2 6 6 8 3 0 6 3 4 3 2 8 5 8 7 8 5 6 9 8 3 0 5 2 3 5 8 0 8 9 3 3 0 6 5 7 5 7 4 0 6 7 9 5 4 5 7 1 6 3 7 7 5 2 5 4 2 0 2 1 1 4 9 5 5 7 6 1 5 8 1 4 0 0 2 5 0 1 2 6 2 2 8 5 9 4 1 3 0 2 1 6 4 7 1 5 5 0 9 7 9 2 5 9 2 3 0 9 9 0 7 9 6 5 4 7 3 7 6 1 2 5 5 1 7 6 5 6 7 5 1 3 5 7 5 1 7 8 2 9 6 6 6 4 5 4 7 7 9 1 7 4 5 0 1 1 2 9 9 6 1 4 8 9 0 3 0 4 6 3 9 9 4 7 1 3 2 9 6 2 1 0 7 3 4 0 4 3 7 5 1 8 9 5 7 3 5 9 6 1 4 5 8 9 0 1 9 3 8 9 7 1 3 1 1 1 7 9 0 4 2 9 7 8 2 8 5 6 4 7 5 0 3 2 0 3 1 9 8 6 9 1 5 1 4 0 2 8 7 0 8 0 8 5 9 9 0 4 8 0 1 0 9 4 1 2 1 4 7 2 2 1 3 1 7 9 4 7 6 4 7 7 7 2 6 2 2 4 1 4 2 5 4 8 5 4 5 4 0 3 3 2 1 5 7 1 8 5 3 0 6 1 4 2 2 8 8 1 3 7 5 8 5 0 4 3 0 6 3 3 2 1 7 5 1 8 2 9 7 9 8 6 6 2 2 3 7 1 7 2 1 5 9 1 6 0 7 7 1 6 6 9 2 5 4 7 4 8 7 3 8 9 8 6 6 5 4 9 4 9 4 5 0 1 1 4 6 5 4 0 6 2 8 4 3 3 6 6 3 9 3 7 9 0 0 3 9 7 6 9 2 6 5 6 7 2 1 4 6 3 8 5 3 0 6 7 3 6 0 9 6 5 7 1 2 0 9 1 8 0 7 6 3 8 3 2 7 1 6 6 4 1 6 2 7 4 8 8 8 8 0 0 7 8 6 9 2 5 6 0 2 9 0 2 2 8 4 7 2 1 0 4 0 3 1 7 2 1 1 8 6 0 8 2 0 4 1 9 0 0 0 4 2 2 9 6 6 1 7 1 1 9 6 3 7 7 9 2 1 3 3 7 5 7 5 1 1 4 9 5 9 5 0 1 5 6 6 0 4 9 6 3 1 8 6 2 9 4 7 2 6 5 4 7 3 6 4 2 5 2 3 0 8 1 7 7 0 3 6 7 5 1 5 9 0 6 7 3 5 0 2 3 5 0 7 2 8 3 5 4 0 5 6 7 0 4 0 3 8 6 7 4 3 5 1 3 6 2 2 2 2 4 7 7 1 5 8 9 1 5 0 4 9 5 3 0 9 8 4 4 4 8 9 3 3 3 0 9 6 3 4 0 8 7 8 0 7 6 9 3 2 5 9 9 3 9 7 8 0 5 4 1 9 3 4 1 4 4 7 3 7 7 4 4 1 8 4 2 6 3 1 2 9 8 6 0 8 0 9 9 8 8 8 6 8 7 4 1 3 2 6 0 4 7 2 commons-math3-3.2-src/src/test/resources/org/apache/commons/math3/stat/data/Mavro.txt100644 1750 1750 4035 12126627677 27373 0ustarlucluc 0 0 File Name: Mavro.dat File Format: ASCII Header : lines 1 to 60 (= 60) Certified Values: lines 41 to 43 (= 3) Data : lines 61 to 110 (= 50) Dataset Name: Mavro (Filter Transmittance Data) Description: This is an observed/"real world" data set consisting of 50 transmittance measurements (at a sampling rate of 10 observations per second) from a filter with a nominal value of 2. The experimenter was Radu Mavrodineaunu, a member of the chemistry staff at NIST. We here use this data to test accuracy in summary statistics calculations. Stat Category: Univariate: Summary Statistics Reference: None Data: "Real World" 1 Response : y = transmittance 0 Predictors 50 Observations Model: Lower Level of Difficulty 2 Parameters : mu, sigma 1 Response Variable : y 0 Predictor Variables y = mu + e Certified Values Sample Mean ybar: 2.00185600000000 Sample Standard Deviation (denom. = n-1) s: 0.000429123454003053 Sample Autocorrelation Coefficient (lag 1) r(1): 0.937989183438248 Number of Observations: 50 Data: Y ------------- 2.00180 2.00170 2.00180 2.00190 2.00180 2.00170 2.00150 2.00140 2.00150 2.00150 2.00170 2.00180 2.00180 2.00190 2.00190 2.00210 2.00200 2.00160 2.00140 2.00130 2.00130 2.00150 2.00150 2.00160 2.00150 2.00140 2.00130 2.00140 2.00150 2.00140 2.00150 2.00160 2.00150 2.00160 2.00190 2.00200 2.00200 2.00210 2.00220 2.00230 2.00240 2.00250 2.00270 2.00260 2.00260 2.00260 2.00270 2.00260 2.00250 2.00240 commons-math3-3.2-src/src/test/resources/org/apache/commons/math3/stat/data/Michelso.txt100644 1750 1750 4215 12126627677 30052 0ustarlucluc 0 0 File Name: Michelso.dat File Format: ASCII Header : lines 1 to 60 (= 60) Certified Values: lines 41 to 43 (= 3) Data : lines 61 to 160 (= 100) Dataset Name: Michelso (Speed of Light Data, in millions of meters per second) Description: This is an observed/"real world" data set consisting of 100 measurements of the speed of light in air. This classic experiment was carried out by Michelson is 1879. We here use this data to test accuracy in summary statistics calculations. Stat Category: Univariate: Summary Statistics Reference: Dorsey, Ernest N. (1944). The Velocity of Light. Transactions of the American Philiosophical Society, Volume 34, Part 1, Pages 1-110, Table 22. y = mu + e Certified Values Sample Mean ybar: 299.852400000000 Sample Standard Deviation (denom. = n-1) s: 0.0790105478190518 Sample Autocorrelation Coefficient (lag 1) r(1): 0.535199668621283 Number of Observations: 100 Data: Y ---------- 299.85 299.74 299.90 300.07 299.93 299.85 299.95 299.98 299.98 299.88 300.00 299.98 299.93 299.65 299.76 299.81 300.00 300.00 299.96 299.96 299.96 299.94 299.96 299.94 299.88 299.80 299.85 299.88 299.90 299.84 299.83 299.79 299.81 299.88 299.88 299.83 299.80 299.79 299.76 299.80 299.88 299.88 299.88 299.86 299.72 299.72 299.62 299.86 299.97 299.95 299.88 299.91 299.85 299.87 299.84 299.84 299.85 299.84 299.84 299.84 299.89 299.81 299.81 299.82 299.80 299.77 299.76 299.74 299.75 299.76 299.91 299.92 299.89 299.86 299.88 299.72 299.84 299.85 299.85 299.78 299.89 299.84 299.78 299.81 299.76 299.81 299.79 299.81 299.82 299.85 299.87 299.87 299.81 299.74 299.81 299.94 299.95 299.80 299.81 299.87 commons-math3-3.2-src/src/test/resources/org/apache/commons/math3/stat/data/Lew.txt100644 1750 1750 7025 12126627677 27040 0ustarlucluc 0 0 ##################################################################### # Dataset Name: Lew (Beam Deflection Data) # # Description: This is an observed/"real world" data set # consisting of 200 deflections of a steel-concrete # beam while subjected to periodic pressure. # The experimenter was H. S. Lew of the # Center for Building Technology at NIST. # We here use this data to test accuracy # in summary statistics calculations. # # Stat Category: Univariate: Summary Statistics # # Reference: http://www.itl.nist.gov/div898/strd/univ/lew.html # # Data: "Real World" # 1 Response : y = beam deflection # 0 Predictors # 200 Observations # # Model: Lower Level of Difficulty # 2 Parameters : mu, sigma # 1 Response Variable : y # 0 Predictor Variables # y = mu + e ##################################################################### ##################################################################### # # Certified Values # ##################################################################### n = 200 mean = -177.435000000000 standardDeviation = 277.332168044316 autocorrelationCoefficient = -0.307304800605679 ##################################################################### # # R Generated Values # ##################################################################### variance = 76913.13143 max = 300 min = -579 sum = -35487 ##################################################################### # # Data # ##################################################################### -213 -564 -35 -15 141 115 -420 -360 203 -338 -431 194 -220 -513 154 -125 -559 92 -21 -579 -52 99 -543 -175 162 -457 -346 204 -300 -474 164 -107 -572 -8 83 -541 -224 180 -420 -374 201 -236 -531 83 27 -564 -112 131 -507 -254 199 -311 -495 143 -46 -579 -90 136 -472 -338 202 -287 -477 169 -124 -568 17 48 -568 -135 162 -430 -422 172 -74 -577 -13 92 -534 -243 194 -355 -465 156 -81 -578 -64 139 -449 -384 193 -198 -538 110 -44 -577 -6 66 -552 -164 161 -460 -344 205 -281 -504 134 -28 -576 -118 156 -437 -381 200 -220 -540 83 11 -568 -160 172 -414 -408 188 -125 -572 -32 139 -492 -321 205 -262 -504 142 -83 -574 0 48 -571 -106 137 -501 -266 190 -391 -406 194 -186 -553 83 -13 -577 -49 103 -515 -280 201 300 -506 131 -45 -578 -80 138 -462 -361 201 -211 -554 32 74 -533 -235 187 -372 -442 182 -147 -566 25 68 -535 -244 194 -351 -463 174 -125 -570 15 72 -550 -190 172 -424 -385 198 -218 -536 96 commons-math3-3.2-src/src/test/resources/org/apache/commons/math3/stat/data/NumAcc3.txt100644 1750 1750 32664 12126627677 27571 0ustarlucluc 0 0 File Name: NumAcc3.dat File Format: ASCII Header : lines 1 to 60 (= 60) Certified Values: lines 41 to 43 (= 3) Data : lines 61 to 1061 (= 1001) Dataset Name: NumAcc3 Description: This is a constructed/fabricated data set to test accuracy in summary statistic calculations. The numbers are 8-digit floating point values and differ only in the last decimal place. Note--by construction, this data set has sample mean = 1000000.2 (exact) sample standard dev. = 0.1 (exact) sample autocorr. coef. = -0.999 (exact) Stat Category: Univariate: Summary Statistics Reference: Simon, Stephen D. and Lesage, James P. (1989). Assessing the Accuracy of ANOVA Caluclations in Statistical Software", Computational Statistics & data Analysis, 8, pp. 325-332. Data: Constructed 1 Response : y 0 Predictors 1001 Observations Model: Average Level of Difficulty 2 Parameters : mu, sigma 1 Response Variable : y 0 Predictor Variables y = mu + e Certified Values Sample Mean ybar: 1000000.2 Sample Standard Deviation (denom. = n-1) s: 0.1 Sample Autocorrelation Coefficient (lag 1) r(1): -0.999 Number of Observations: 1001 Data: Y ------------- 1000000.2 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 1000000.1 1000000.3 ././@LongLink100644 0 0 160 12126630646 10256 Lustar 0 0 commons-math3-3.2-src/src/test/resources/org/apache/commons/math3/distribution/gamma-distribution-shape-142.csvcommons-math3-3.2-src/src/test/resources/org/apache/commons/math3/distribution/gamma-distribution-sh100644 1750 1750 353367 12126627677 32612 0ustarlucluc 0 0 # # Licensed to the Apache Software Foundation (ASF) under one or more # contributor license agreements. See the NOTICE file distributed with # this work for additional information regarding copyright ownership. # The ASF licenses this file to You 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. 0.03125, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003033557651760714818735665800978035728386346661017724718604534692 0.0625, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008196150775626282628370564518003509754535754260418430834430871299 0.09375, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005356804087131708232170095277918593828134178145383293414681702802 0.125, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002146327166439540246398358002669112346471903105363000225485561847 0.15625, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000960364403129280786219117500792788862113356279461790022439367189 0.1875, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001359627826159167702689424668469409813856858299223455052233895885 0.21875, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003625314429647415686269710229488475280864917160280547545814011269 0.25, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005280055662820293479921044247452485193033930497996421361452481297 0.28125, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008347808225604055395552772844436397118175498947445178752830031553 0.3125, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002289849450465765697046081509376075699231725359632104839796953108 0.34375, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001522663140190406982028440599957663148888854448024647008119209476 0.375, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003142094319322777893822820759638913126973198807605710066428390617 0.40625, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002427194042808328587609778268791494485130017025071122616439403163 0.4375, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008120320119339370256765480323676918179450838149762092694252517995 0.46875, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001320735781658764038785116231539959464128005951135765103909968427 0.5, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001146289380649086251192366642512530248229429646573708458469054648 0.53125, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005729317978524440303661585614465927352456618862827214382201429896 0.5625, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001756533872566753164655565637025058413964567217445667196148257264 0.59375, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003482726265787536095327969710487749190390910320826079887842286997 0.625, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004670025940856888878036981654666381856494844505341292287265152492 0.65625, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004399859001778829755200950318799556871602405597605068627971141461 0.6875, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003009847523695871137155966271832221886880635267574938717556381889 0.71875, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001538153489785019351245227806007778554275108904637933195654778731 0.75, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000601988473101702607517706173135085324335133021669467403457903845 0.78125, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001844049668265386234882586879556672296571377630260000611580488412 0.8125, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004507147312601440301196460014271144816019658317794446256458944693 0.84375, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008940848436174156151597192738970983007662176427051447351997333187 0.875, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001461499607732898443402285654244909285713345943864592573630554624 0.90625, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001995548489369588752189161870328606539521123037000735268097269063 0.9375, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002303932728834128768996762781034540316524950031739503988166609962 0.96875, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002274049986657838699161555446592450941826846834830657157942672607 1.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001938101049577455202727075639432141456632441869307014325930687745 1.03125, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001439207103396520302957830107213680654287582685615092296128515438 1.0625, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000009388870987884926321208613344400111314360050974970011913169760016 1.09375, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005421364646458672817983471948627815237573880787153813230932289631 1.125, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002789942621540927035005667059586847893980666306338564415501188888 1.15625, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001287698098202811537673555436181916839533959712310258448088674657 1.1875, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005361500875610972303069979023862418197367833486824615163167283211 1.21875, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002024590275225225473149737015506573501482013601544113684468918953 1.25, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006968104103428521074887309294576974632141002929326770098973906475 1.28125, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002195876687657882481932874535395007039155900357364652460795053805 1.3125, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006363006899482337496866116378074003296591172546262283006753550852 1.34375, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001702131003319577076536215520640465553566210477367846495969272327 1.375, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004218872760922287869187073534075925700463131926730135780353228175 1.40625, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000009722138030276234999967148274402692267623338326043961409992449596 1.4375, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002089680571318010877428987137556677150362282595688332957085240892 1.46875, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004201959906157252500545494007663123961399359264658674199407587469 1.5, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000007926778132820965146879257325156297716926492844457887575744737405 1.53125, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001406558547228359964744980908369959351926478661061992683244431591 1.5625, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002353474185030691766648160034239693486376197232650666777326750706 1.59375, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000037218764710631680810291378957895044299024058306993829517533613 1.625, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005575282460741596068502411216802287983793894391140242455944987172 1.65625, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000007927227711779173914646595025005765997143334835634195149748159104 1.6875, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001071944134837427734148831528736806590429798098342595807102541858 1.71875, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000138108221760372438606897908219605037463847000728614744695961898 1.75, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001698323504027615535775493977499106103008870474964119336234801976 1.78125, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001996602265088979037508101711514879118315066110140433192234414829 1.8125, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002247565201838885801098874326029714649340575038762571277061052162 1.84375, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002426203059645715477666388686373013077426157362543234017305566191 1.875, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002515058586358477945242562545957311323172718010330339180458411349 1.90625, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002507014442882676241995203575254093447049547393440111571045348996 1.9375, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002406060902781161902201317113546172367599689929770992516074063544 1.96875, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002225994911613670690636440238190995406019476793372244336676352445 2.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001987519157465959491445347553267451003457990490531392275965754195 2.03125, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000171453587137175849954036308161585554228777378871958588259630981 2.0625, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001430495506796013044008857457938321109148650996310424378677368237 2.09375, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001155492324240659068289466063962728357122772450744805947202989304 2.125, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000009044922980021883592693068478137647216483263685308937373183116557 2.15625, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006867493750666057678732738145851191379632376782577075388002117646 2.1875, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005062073169861849151639861142509580998525899776359842235080539088 2.21875, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000362543600249358108048615563254247239159695775510296834704211439 2.25, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000252489526603295926854889138637603480058181758825607503414558784 2.28125, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001711247968940842037651268479869014802821611649688933171242705035 2.3125, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001129510956969341394482584774058228942310010081982015263548709781 2.34375, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000007265816104496300499898085992734152379766838403219111427628313591 2.375, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004558175816260007555105632285341725080639224806678437420596463138 2.40625, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002790584050989946527828296128587552010590980237784251322397109482 2.4375, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001668284578618286381690790296779862250618026482768832959903523649 2.46875, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000009744943495971065664868805223978856002757562039568746736065311578 2.5, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005565137725047395574049347236684956078968710486499751433053096287 2.53125, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003108878218450848872116486181048621292547452360719664918176573336 2.5625, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001699798852685296386784022068442245217886560103650873256545999535 2.59375, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000009100885870327262722464965223732881717518714282705775083344810535 2.625, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004773976482611128988798211844746120102308059567771123130352965033 2.65625, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002454696275760835444695690036249528912243300297923688668213413605 2.6875, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001237767743712980126635586939279788778464881607957430187558901314 2.71875, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006123510291695639565085664209717987140225049417169316601162655024 2.75, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002973520270830365285227422297179401263577767138187513753393353772 2.78125, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001417860046058175928579947574510220867900449105752381426257219265 2.8125, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000664147521303038603683361611790803353067898170593358323046435521 2.84375, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000305728087739129723233638886379847283002349977344670616284337749 2.875, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001383601345866491256388259703880761553003600032816461633662975225 2.90625, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006158165603165894766153050137123578901788359210810326725331742519 2.9375, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002696567354490352164922751914759148164548566444834386869162624497 2.96875, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000116209212486453951070292394541201310925330607163600309184486292 3.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004930425131321923963510886764900778982514745751009761362610987774 3.03125, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000206007645643820827303314518317195203657968524336608125987741934 3.0625, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008479568978137889855011902857824122896893352330369105194359940995 3.09375, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003439440813286467374108287315135203469007226896482626091143674007 3.125, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000137516148277974955023133604376843473580630998237473574478686017 3.15625, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005421203379476148213421988636417094025843024411009018713703061847 3.1875, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002107824814208896255395316141764944639324591290668333148079715839 3.21875, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008085134990542965334589253581905327037952765906130980166609647657 3.25, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003060326271241347797546462262688157278623489089392238281241333858 3.28125, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001143368848839448104018303666454175768464926380076825556040748176 3.3125, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004217455283370294519707512781532810480296863635335952438327502641 3.34375, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001536258982753454534656763557966934144314303438058646444410596781 3.375, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005527510837850865215976637913130799946312868911245521928224185736 3.40625, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001964918701656530953500470511779473718145278225187625311517613506 3.4375, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006902481976642110282867774172652102442911661216152222414590281317 3.46875, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002396652013332100510036731291118144945253806850133789101153921894 3.5, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008226866792627073576624811506236814537735144444052251936045499752 3.53125, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002792428729171867019261632828016033276093622823994937073116309008 3.5625, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000009374193247831870573401525780089781518374219773437712153248195981 3.59375, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003112960794284175181733134745769766048688030004098876592606281174 3.625, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001022781678276829485916565991737738962471395619764099977969733207 3.65625, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003325379860895122886890362143526848537880631351835625263563028836 3.6875, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000107010423716668494467801704623132402504569493415748568703926534 3.71875, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000340888937140304566485574925143235069269037264871300875742666981 3.75, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010751655516500057121080638788882342542498896812034085084486699 3.78125, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003358034552231975198678840222047938296020431436434536897616135351 3.8125, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001038753185016620960633311422824378609872886112455774706737689662 3.84375, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003182916559403998009440824039914963418731723416236261503014434234 3.875, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000009662520894131646017586093285001024077532297070658244747172094388 3.90625, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002906517684554548593909078547902429113847660001930359056667839338 3.9375, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008664356127701868520523335284923019038086901367224834615112611268 3.96875, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002560014264812279949870787664812232664883486562720634675869725085 4.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000007498108979417057501969410105785805389350218991421840011138800148 4.03125, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002177326094949818288347202184261203144450142589128935491950993049 4.0625, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006269245805777304062959647748195031272596426976759696928823559811 4.09375, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000179012603033255501316663682511198478183649007217295526070646623 4.125, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005069714673975477993740329273174984319966159049747174542021474877 4.15625, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001424193146376045621300533599430331840038299782840485465768830703 4.1875, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000396910311715937612060249857790569331523050987684370746126621097 4.21875, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001097502372668119018893797764965529385695652097551413500179560685 4.25, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003011330949110272144045228845675930846008796626607080758466525698 4.28125, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008199751179089182270769437231646418534684430377587720464056545446 4.3125, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002216053214150434174870849707962984102969408145854092717585333108 4.34375, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005944894276971024158504391166744192246344756485153025450007245157 4.375, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001583210377353177777333473799849462340999007461282477147169140736 4.40625, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004186092098624555386556386467552330430779725958036135029494429691 4.4375, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001099002676513170435713162205144203664065949773584055149817112979 4.46875, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000286517908393675782453454434457603529913583440230837720610375309 4.5, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000007418398729678948264093232621092495011052688220031294770631281509 4.53125, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001907723368316186218646756394762922274196828964932301754563956074 4.5625, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004873130564372278413572568167965620835391407327408598502160306408 4.59375, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001236596117972383474418618065923455754211611499889769196290907369 4.625, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003117553014251019176067861307360696368711492217264233091136034538 4.65625, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000007809156389985564416087564853726256469214340433469137404030263426 4.6875, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001943730719442780934678679858372061252199405445163249469868772566 4.71875, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004807800490111025108911568884934999392882694780835717333342337857 4.75, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001181873695171431439695945088987936055967942289494929550288380437 4.78125, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002887654362824212907589350003063923798362181637370243713611837908 4.8125, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000007012992901720668411651464264904343421484882878808334647435353143 4.84375, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000169308788005483889819778195348947755602057014872872843007673311 4.875, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004063560445223066083464629222389436716502305959187568800096141841 4.90625, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000009696558979485849976043909648212915905842791383838858857213633695 4.9375, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002300616401971812940984249955733735418359276617747026432669928184 4.96875, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005427724276350974799311405179138381682201180943921299018806591684 5.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001273412476145461672075056301194182361232686783977419708662262251 5.03125, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002971175901536008750962400943949868693742875555320731900504263877 5.0625, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006894855858850366700795231361900197527813107365021450247944717046 5.09375, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001591434099115486071170393818323723215391351119139507802570548869 5.125, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003653821361594311314574706064471335286211108802524525123637510255 5.15625, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008345054309138438898700485807574718650834969726679686966054554711 5.1875, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001896102209558316042776877472000039957325784928568868193789506405 5.21875, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004286196328211946561257298855380758740733499003062176371400127594 5.25, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000009640213425592401701042226950057700956732688467866826860393039754 5.28125, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002157404466474730346185520290143630707274173815691233592277968574 5.3125, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004804325621241812428345044635656940947080406967322304172469895096 5.34375, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001064668446646429457030972722867941251588495222606268144189274692 5.375, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002348021824397676043128942514749670887689136781433572527614873558 5.40625, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005153709724950940139653493250773597071009075655434957435254299825 5.4375, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001125878998267122968885755227852945047486709659371796425080143082 5.46875, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000244816605759779816004180567288698278511744647569700468628393531 5.5, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005298957873058011165104349035215339990730653743232994245786124398 5.53125, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001141729334458245387875885567892856386122119556582039777580259951 5.5625, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002448957371460512921600064708569122978658265686211010169637328029 5.59375, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000522957704396828875410780147126183684403517810610254312492269485 5.625, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001111835955208705990936833289311490643454681725453507908414697018 5.65625, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002353557693827852724067285371446488225403741799605315565539291925 5.6875, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004960663812563294773691731616946693129609512339452366736803176052 5.71875, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001041132585148871760810214106189583368411494721945393385226352451 5.75, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002175924032838341373227815023604310808951077720429327540940697089 5.78125, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004528690976979900655760131770976419259657575136030387076338350224 5.8125, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000009386688679153395792340988676276694113418700998038816778128957944 5.84375, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000193768018695629477573199431698945622462450783314835325762961207 5.875, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003983828374746924704154681470125385210358063226084027524948888291 5.90625, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000815805370965902701709785138214177903149493320270437986539405164 5.9375, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001664018704096287050872089658514876467239191720773564290055004104 5.96875, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003380909522265722750395407694134506238549942457017003971011108805 6.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006842744296214723537727945847460727928513924870705812601602431164 6.03125, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000137964049001107851622396404413536906611348188838440495038011827 6.0625, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002771134333588750978981033268036938465744063940438358720552228841 6.09375, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005545263126705103213400208522340569424112501012536187143288398533 6.125, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001105544720976625645024856553421425146740527858231014610730818966 6.15625, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002196020753551502100737924736366191791485623926602590372533217668 6.1875, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000043462897499030122394382008074978430762827686865137830272479296 6.21875, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008571146534483015484655815001620990693672450265429402676387192147 6.25, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001684274340253163378160635216915758506105388528091374365453164773 6.28125, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003298039382840138664381860281661933129470534060769098920043400455 6.3125, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006435512385969675609788067687924648245588209433225398451020581766 6.34375, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001251438989830819851415712766107847327820256458172967203317276102 6.375, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002425214970788023674463414661283310349951352977649260085600309237 6.40625, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004684026459773757828447058425930780252713579931047672401814148661 6.4375, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000009016361177077845246921244344478962271897618306178298649411697988 6.46875, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001729817295848649781993118854990487772917022923974342759178067018 6.5, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003307806169793130510982334866615011527785203501023656826483477103 6.53125, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006304700524662892306087761628664560637987386090053119166051807355 6.5625, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001197807355113915996089560628042636793403286547131106024342147946 6.59375, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002268406653574348337711735470759590234535088094244121787599163645 6.625, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004282322826889319671917993066379724135993887413502696970053707701 6.65625, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008058893105101947207141630965449937707349674105508014435223601649 6.6875, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001511895067084289961214791201476762110959758208993661039150779699 6.71875, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000282768321149007555510213646031528189288641392967010836054166041 6.75, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005272482111955957476310618050738049996113809379794652066147113435 6.78125, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000009801374259400904943593879018614884389897663396603615837451065519 6.8125, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001816596176738590046362779282146725551001583891168012131302563529 6.84375, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003356922287444331319401652433515568070270405108200030616560841174 6.875, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006185109438580475878947342997180768947983909247528181139598589571 6.90625, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001136287631742576597039449314550477460342784379234340484514154796 6.9375, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002081495061734270685218089722978990964163724234301594467329788179 6.96875, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003802068828281746104944863142920844263537323177737785614810544341 7.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006925213505823538062301172881109903609207566198325931757484703988 7.03125, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001257841540669633219049140099252106751234250072949226372464184205 7.0625, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002278290487613311599476275804319799159685458131215310791600370262 7.09375, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004115222664386450654500012336196226432901316119342402756067190012 7.125, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000007412918050559973389052201727180923494899643443855461521251167266 7.15625, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001331702083895200741102556009045789465363769564812986002183644396 7.1875, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002385927513729377306635589715693263875729456770424272035967029802 7.21875, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000426333862782329846914813032335347195061111785986525367655746213 7.25, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000759792200996601037634866991529745455282374167976311037875847981 7.28125, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001350523432370193299734786674454067302794900372527980116731210492 7.3125, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002394315910389921980661766500038664334659989560668585521962361671 7.34375, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004233917919772873337347814686927940300936042601972897362127972931 7.375, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000007467832533118624234061305065697220217811202399242360850898455582 7.40625, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001313854389561276542360909934343775265258348674100457512300950134 7.4375, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002305736589269961566168466259393035259769880425383845694413065436 7.46875, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004036371002452978610240489246408284502960388746591485972224620475 7.5, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000007048560351059796400285181889261238746243600418448156280920848613 7.53125, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001227853752301304056467276173274525321817246456141707672879709831 7.5625, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002133725460307847159962475075710536091069494892939321418646414774 7.59375, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003699004085178423116735407961688852470509413129667092898909470901 7.625, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006397260632565248001785169365305423189425549546916682595083947319 7.65625, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000110376018446916815423378362939326640165821909439302861269920657 7.6875, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001899919619407042009351418149210404736315074320726757613113617913 7.71875, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003262750260101144378275168028338904916508984929088513927203020413 7.75, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005590217652757415571250389409834998311213531198596359680280312335 7.78125, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000009556039994803832389547891888356525093319424715346225573281544967 7.8125, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001629819608107800248060806390015987297777206533074410563849997079 7.84375, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002773456393471583471518744399637437082532322777386734798381406521 7.875, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004709026785312308977827119718000547507531262567832787268101609912 7.90625, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000007977681503393607598068353520411929856003813625865845993101966936 7.9375, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001348545328252375845159875313726742833901141054670926931598580225 7.96875, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002274601095950038423180020783608654526026382291647209487491285674 8.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003828275722695871275414181078782527688315736455273579921024831237 8.03125, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006429345840110526376142521388953854103573762761281252949576150915 8.0625, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001077465159826240628298029795145612318598303260662362358338104024 8.09375, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001801854538890822622748496992109498149622261052398408032264386404 8.125, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000030069302487529963435785157079405622170924625468049237221815639 8.15625, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005007501893831298136412933794408212829115159222015750154726021779 8.1875, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008321851487003577695743100367223665786551248204349928487854160355 8.21875, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001380151365539535767693074815644042022613863149759156280615873753 8.25, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002284273852255941193239444404409641306288350657752316942091569727 8.28125, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003773036463723727157848267425911718960641614230519360366549595771 8.3125, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006219591189824386545510741542567717223257195419914415745836399268 8.34375, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001023215769088043283659624059235168874307462589254182958107715019 8.375, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001680016811650055606154274855341760151251427454287938101988517824 8.40625, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002753007834960247696597686243471489399857595617566623329616699859 8.4375, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004502513367815061239172107376162164313914160658954401389694257338 8.46875, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000007349580908200911558373754378522773430810367489994549209151062199 8.5, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001197391933743656087544768372834023274300731430443491754699533209 8.53125, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001947073695274732297730985287830338109400089171412829138014952089 8.5625, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000316014355698547785202114865486022226037526605774202205571229937 8.59375, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005119358950649590935192355402711251083310134773745651134553027059 8.625, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008277794112694305657297770538658544845048334957887062987220807851 8.65625, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001336010233239469759228932084856054863680676324724024014281868365 8.6875, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000215232013458648021542833076445898075446955282743018182448198709 8.71875, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003461079723115436449220480161062973977989465476899259580318390616 8.75, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000555558322814136613274167213447235151617395957165429181900439667 8.78125, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008901570646822747634490153362547626792598012833875666181002721963 8.8125, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001423731542313538141610151244797424802431159831058817867828383807 8.84375, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002273105224407963949072728556104209309678511636048321339419708845 8.875, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003622816818881237146362386147225631171220424625505080482182970732 8.90625, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005763867481567535513061612144128817652051154847423128202476238446 8.9375, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000009154353833237467973924845433855110224377681703744499120333767948 8.96875, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001451418854588386565041250108535516904058561990822755292626998915 9.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002297282437541898165579971054830991233322340086502987219280284537 9.03125, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003629925774042841802352704266926980766945049378066084285369481028 9.0625, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000572595482129406500152196841229693657743241575769776866769500755 9.09375, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000009017163776855774490705170052064929593227984800420245030894961418 9.125, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001417649478303937731879614418029258600981192764283064062593273312 9.15625, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002225100214225162980209891891187862813134526212714269583454891916 9.1875, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003486719343917734532381336588009862051832982400239241870784037603 9.21875, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005454763846860128433842617543369300271318096581495170515681195684 9.25, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008519836435887393037372799476007298426558838297528378940404312109 9.28125, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001328579812390231318118850576077916191697062955498112113421709686 9.3125, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002068472862796926932639844971783418116047020311149100671259371944 9.34375, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003215306985111608652552172352741451550612250523658895101256849161 9.375, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004990109816465268217512555533852438987099199856809497634809568338 9.40625, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000007732454334456354163202093766639503150744678325880996813584052309 9.4375, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001196323791021696680706877225232016259317830535095585447138309103 9.46875, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001848028645907580214223694398680119009434938885168119099780117975 9.5, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002850372808107008276052485040475492594517080453760521443420164284 9.53125, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004389671516792773240388877798247170881712713825597262232514888273 9.5625, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006750005182056601588246241693947369947696297617816730786118533444 9.59375, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001036387609295907794542130597361103274676836333515686403148029195 9.625, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001588878153852958335228027060547120418967220085875475351786587983 9.65625, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002432279411006132241593225730323880308088219849178226834259296345 9.6875, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003717876785972433039389193297183964200294903289152366578534999947 9.71875, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005674653031011688138036878104312988658698388308229352748513813643 9.75, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008648693641664818698549634186727001733495646440494843973812055027 9.78125, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001316232616424745500339742780691143898273059895876079464874194895 9.8125, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002000275106571635568703860263065097793311620029505305735746124515 9.84375, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003035468522478563963078085343743427325836661614206596654053281748 9.875, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004599859810613497642733108717675389730477678541886362246291390064 9.90625, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006960656732544005387764238730276305022587461367413392192245820811 9.9375, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001051832194155633424802898252941972192065789946527134540959847077 9.96875, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001587220083671825146295304840066497683504745765758149295889789254 10.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002391806708285417552455993203077117652240144682639512679358245999 11.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006036708497667833244105575688653964676515743656790548503830105057 12.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004728167010820066449834388048750264123567631360777777646311571828 13.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001386293890164672688980958434875665373468422710579518263196611968 14.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000001760358747269139460262117949098140092671629862308120788864014115 15.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000108672885069415685211010859188615827844919102581796787985945063 16.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000003579948200070280886257331795725912128843516256489402882147989447 17.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000006791451766317429483849925394400382425911703898856439825416123004 18.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000007903026604836526637508534299622724873220518890175830923055850538 19.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000594748666317073772831846021032273347343403750572404089816755731 20.0, 0.000000000000000000000000000000000000000000000000000000000000000000003026987651043487209321374217401707921122011443022829816141146971 21.0, 0.000000000000000000000000000000000000000000000000000000000000000001082448732327653872536572536914008068281167750926264132028543561 22.0, 0.0000000000000000000000000000000000000000000000000000000000000002810543378739649666251415485583921935287490527039287571301684864 23.0, 0.00000000000000000000000000000000000000000000000000000000000005451583813115210731306583761223194360672966759995473090084283673 24.0, 0.000000000000000000000000000000000000000000000000000000000008098198434366083739258477828573935799585126093108173453536184563 25.0, 0.0000000000000000000000000000000000000000000000000000000009415644956074781102749570400390382426136793627788644597005981355 26.0, 0.0000000000000000000000000000000000000000000000000000000873486974322276167680545194693103728073942329927962400629049062 27.0, 0.000000000000000000000000000000000000000000000000000006576739402696529361648349255994167876121321261342297286341115102 28.0, 0.000000000000000000000000000000000000000000000000000408044778296693854754388370383237798398041154398292495763582155 29.0, 0.00000000000000000000000000000000000000000000000002114699327644525053523794254087880550785807522557967427701874882 30.0, 0.0000000000000000000000000000000000000000000000009266876112108050086331645076337785406907702702592002909839913886 31.0, 0.00000000000000000000000000000000000000000000003471688815303758910565593355968383839840509352765376118505649967 32.0, 0.000000000000000000000000000000000000000000001123037920379460648137242805879442425300315247375983914263884276 33.0, 0.00000000000000000000000000000000000000000003165326536102020776606163889509630563738807028464123004633844247 34.0, 0.0000000000000000000000000000000000000000007837649120755275998724703651167789449999790138823415012865041409 35.0, 0.00000000000000000000000000000000000000001717743387639083029476212001814176006056705829558162996963861198 36.0, 0.0000000000000000000000000000000000000003355228934144175810153517012304713090917074487184197523934644415 37.0, 0.000000000000000000000000000000000000005877845436105469675821273206862313830251073819487847885947475163 38.0, 0.00000000000000000000000000000000000009288973187486104761161501637024182125270628021724255051023650176 39.0, 0.000000000000000000000000000000000001331360583964939210878965399472927740779921742679174756222519968 40.0, 0.00000000000000000000000000000000001739203496141217478485612057064067038042185068420595666660780217 41.0, 0.0000000000000000000000000000000002080275901423715352304226576902628316112411794681767497205392998 42.0, 0.000000000000000000000000000000002287982007165186973815812754191466158728434416552857336639883994 43.0, 0.00000000000000000000000000000002323059928459473302436633264709179897150340164603525217916122061 44.0, 0.0000000000000000000000000000002185451029385693942768145373245655580563926118128173538409653609 45.0, 0.00000000000000000000000000000191154116652465096958359638188034519139043719607673993045979308 46.0, 0.00000000000000000000000000001559477087138098014192863241431740245745100185232212061482938749 47.0, 0.0000000000000000000000000001190222670490762488805552185498036788459088966345087544651554457 48.0, 0.0000000000000000000000000008522170863510455654902673616845534562103728661396104062521052204 49.0, 0.000000000000000000000000005739691752098853486904128595345661475142038269914137934828993852 50.0, 0.00000000000000000000000003645166873669795540175992826706727436000871906834179454825214546 51.0, 0.0000000000000000000000002187999162320647316195079590597485016398702575852008187692179468 52.0, 0.000000000000000000000001244025527450833483775019847592652425843922246664095197754886549 53.0, 0.000000000000000000000006713685166943204001317699309021447086144067516164450553138331492 54.0, 0.00000000000000000000003445791623396263253984751256209484564162723867384725026474877629 55.0, 0.0000000000000000000001685053183769823577718922837934220281648047738583508331667585085 56.0, 0.0000000000000000000007864873105082683194620805860769329094944669868914845704780012798 57.0, 0.00000000000000000000350945824802570640834354618845355466309315485195550031695136544 58.0, 0.00000000000000000001499470895656341965385207931794417061222539453741003214890039999 59.0, 0.00000000000000000006143701516179819076510710218792767547866866542600126178906338309 60.0, 0.0000000000000000002417287401307223497382034100059345201116342479591610179127038565 61.0, 0.0000000000000000009145642881416924018479368806021767685845590937700431075393731512 62.0, 0.000000000000000003331510910537906535043201591774626808384363789546127939280261359 63.0, 0.00000000000000001169865710453245426775707351335730338966824816835238953069138433 64.0, 0.00000000000000003964609237788430735132968480113880752412757306097572126965057723 65.0, 0.0000000000000001298114083162922295047488290760870057169259904081713636847626519 66.0, 0.0000000000000004110834611391812946688258749239832831017573173420636890685323303 67.0, 0.000000000000001260340209129797618262692556398468779035454971387829506705556718 68.0, 0.000000000000003744577477357618415676321842846021096113553971516082131667427823 69.0, 0.00000000000001079129192714836394846901601742822049430355640566497827901631967 70.0, 0.00000000000003019123005256020398228417864229260577320405965196689294414042757 71.0, 0.00000000000008207102414327064510845257872058034151392719682501776774232860994 72.0, 0.0000000000002169452763246801559037310022871668337838788635571301430982802483 73.0, 0.0000000000005580806910598620894503152874318378203535160450611145490651192924 74.0, 0.000000000001398143409486399230207385500713236024605373215655577557006886836 75.0, 0.000000000003413680679285354936745428670083108512382270999232961401622765235 76.0, 0.000000000008128432162553662423179382588130228698479840263279920071637462011 77.0, 0.00000000001888808991165962698827240312458207081255796763867630811124225987 78.0, 0.00000000004285882787399796705098567844111603800643498367658991826368890515 79.0, 0.00000000009502255424288748117278702170272823349938826599406827929049439662 80.0, 0.0000000002059683605999324820156139057231272116156875013125239944350260166 81.0, 0.000000000436722458738719369832448089713588150242998064949989825968784123 82.0, 0.0000000009063095758844884811201511931708382096049312730485117322359355714 83.0, 0.000000001841789215718945263321521049540067409731512165820129058676562527 84.0, 0.000000003667024009729579500710808459591189094854498796374400367155242818 85.0, 0.000000007156628318382350160395996758757792601328234643400620306676300276 86.0, 0.00000001369705123598815762008841733420422185950504302102714193613917828 87.0, 0.00000002571968186100625219711150274755962453676135865671267534031490965 88.0, 0.00000004740381170098611526390735189007414926772105441361024634599746331 89.0, 0.00000008579321566851634393413523824162020863452259923802470170876706208 90.0, 0.0000001525321382154419379679097082931664892918752727735688125040598904 91.0, 0.0000002665074554266794212144843131788788164802750512506709386968075376 92.0, 0.0000004577856967521044725575655741686740965734429715832679986970445493 93.0, 0.0000007733566560961179024471297133235772580817772630830281690004543555 94.0, 0.000001285336845744877019322945401828252993728960425483723132737819618 95.0, 0.000002102439288040642838862000087587925857692677563108102690997726634 96.0, 0.000003385668980319961813404981405276920239982611908895745235060838514 97.0, 0.000005369337281658565731454465322565419619160672005104486289749844035 98.0, 0.000008388577954806452696511134426792857066258272262842532242941546939 99.0, 0.00001291456042118368999814533615777000204891119630418956411499422793 100.0, 0.00001959849436183591156264808893490049433826998729405420134263002904 101.0, 0.00002932526394874698717645770150186423367476026760725122534742853813 102.0, 0.00004327708126212291843300096180290663748560083145617320107245492488 103.0, 0.00006300687585992641494111922008932653435477375186737401862573427768 104.0, 0.00009052022438663507408749148640982945558006437874127592801574467594 105.0, 0.0001283634756071794630869250248866843984385926485103073347556906969 106.0, 0.0001797143759825711566110007986813361066202367975875381148031272864 107.0, 0.0002484700163784397363640742582291833195872742473055124931316268405 108.0, 0.000339325405501144837206221648870787420998546544154760512747085273 109.0, 0.0004578345686303526705032325915785107126079257265510557927942095465 110.0, 0.0006104449377465844681621183995100067314317049712976243208291469309 111.0, 0.0008044951241799450348135198495329401248634139757487423304387145332 112.0, 0.00104816612998882389586460249871677034249242972770893750010929153 113.0, 0.00135037682097395772141082609175547406356842846013786963777212924 114.0, 0.001720616170494761678716971007955900874156682895421681585314356281 115.0, 0.002168707441332401689243591383238770792242471239078885969789383474 116.0, 0.002704503070629297828645618822326347783062811540171184916885403712 117.0, 0.003337513431683449963430128085016128631691925331499951161838765151 118.0, 0.004076477637389240164509869204741105708241434267978443299145841491 119.0, 0.004928889802156858152239567009623800883314835886378656556608669449 120.0, 0.005900499297214603620530219708707575730249716197954455091699499713 121.0, 0.006994808078944280950055599495070637300980722739415188013434361533 122.0, 0.008212591695435863044555343892382916433584283963534571842585036311 123.0, 0.009551472673182273680049095456947198993979130155902380300051626102 124.0, 0.01100557532387277863768111805703045816842363144027218641846797892 125.0, 0.01256528937952923497771448248737461135668278496645566752294148244 126.0, 0.014217166200742981723913674133572988644844483465830471177355155 127.0, 0.0159439657118267076874336973273175379588776177268819402440786497 128.0, 0.01772486497070088108847251898880412311491721053014426808657056993 129.0, 0.01953583080601088773925880092216472954691771178316439939627837294 130.0, 0.02135014979792388456545854055413645079221908166553822668073350802 131.0, 0.0231390996703348014003392603111398300216663326736169969100201123 132.0, 0.02487273755604421064754583757475616375318755086944337434126291247 133.0, 0.02652077321934100736786423724487900601867576059571579528197793621 134.0, 0.02805348971613081209005453913447038689362430186351537607960489801 135.0, 0.02944267055483383312284721983215014114097758369260166070040529018 136.0, 0.0306624914423373607658009591403252701626037485273680981578760645 137.0, 0.03169033622545459422829776433570036003426818600199920579118046779 138.0, 0.03250750055040472491129951665370737190387293797183473154240387191 139.0, 0.03309975276886812067070456617792634607315191415981900772264480279 140.0, 0.03345772928277851309184977840958550593320987887143779186140314992 141.0, 0.03357715029987238329219029589023866694083146651416499809773375269 142.0, 0.0334588512685963154478629579251269044736695652140702632913761022 143.0, 0.03310863446570408042961815896819919034803540585377617430275315508 144.0, 0.03253695375216682991567091840182655297801160826743425788465160041 145.0, 0.03175845290108797599707934757277136313004603747137912765097514913 146.0, 0.03079138375364901332550206221691620575833710844316532925745034535 147.0, 0.02965693452458682815918242005973750218944029938853145412198834259 148.0, 0.0283785007445631997397084550617464496587993957647410600225341175 149.0, 0.02698093161495825618570987334297363446414106915276718461173620478 150.0, 0.02548978310378882425381958052646450617834925758475590901139609083 151.0, 0.02393060617116435099416912706024095655335035395806653297316574661 152.0, 0.0223282943921562321780165137615328676766889680381525485796668926 153.0, 0.02070651029941681476637557790819212685911753549785911496732232326 154.0, 0.01908720436527963709607115147678923327280791836036078158940618164 155.0, 0.01749023503731265291408546351012121079722770301432802099376748066 156.0, 0.01593309294997471009086670795447102125177394291454936518836705595 157.0, 0.01443072762273584116045562272831633055153314189319680566835468578 158.0, 0.01299547082281045267517960432232641052740702345058331773079521485 159.0, 0.01163704745154459861641730849919576868445366784358020195254306043 160.0, 0.01036266237353620950799626055742897242859969662219923294961137736 161.0, 0.009177150051430891912929602696013808456378681564850921374272304664 162.0, 0.008083173129396522514347845846841160533471403397456591233361250847 163.0, 0.007081456135839677035880096704682519774398108340742711585472221822 164.0, 0.006171041133444516400629812247156187562160684712911237901294578712 165.0, 0.005349553297839216071110580703751474127490857650504655917439450867 166.0, 0.00461346591533518825879755847876348406395862074173680089448002516 167.0, 0.003958356019473447634004305273379274662471110791560421815763881327 168.0, 0.003379143711373368417225956188831719747951290063017825101881991665 169.0, 0.002870310022924396269488437035260643915450483791600583582474382985 170.0, 0.002426089897760668986974360724844952750797027910357134879824006157 171.0, 0.002040638417360563597907339283933010212488336294098182959476890776 172.0, 0.001708169744417845570945758978189855897882671488725227197957700853 173.0, 0.001423069368370359011649941725588485158389808666066235951904056534 174.0, 0.001179981111277721004806161962539155145640347455337098937328602687 175.0, 0.0009738709927616347666747427606927921635391502594319941410226232372 176.0, 0.0008000704778264381160401282062741188655546645462555686507064515833 177.0, 0.0006543018658866236963400770035748552920570945268208216805731617332 178.0, 0.0005326886525200467111095604996538863469621461424759509866992810066 179.0, 0.0004317536384636381623307681230103002605145410540271118505755059965 180.0, 0.0003484074040080927399809401895726721208498414570258906612065478791 181.0, 0.0002799295401477848798173984622376664006301513366709074195130192621 182.0, 0.0002239447564660933145338789682581451161778826623926347156945964656 183.0, 0.0001783956919035431484631327824689429217855404260231946605053055835 184.0, 0.0001415139563344213266777761748108022702206226685476652283041748375 185.0, 0.0001117906422860137426523861280188819036219996574311085446683287243 186.0, 0.0000879472773800646676208610270537193936355294975108988115549121372 187.0, 0.00006890794594884972797061981107385284514141830330906687510100379259 188.0, 0.00005377309664391433056382121330810475865795774096235484453480465121 189.0, 0.00004179537318711845948746677872617607362181009385043999470400209869 190.0, 0.000032357657322072638299163605132779251378969035526174440806100261 191.0, 0.00002495339475498776326697322698673832386006039696328074259137118674 192.0, 0.0000191691837501686562171537329773231840922717230146149961427151677 193.0, 0.00001466953884296528313852050554066173266127803467515458429088569038 194.0, 0.00001118369538698364523893219407208791288673681461739184536644140811 195.0, 0.000008494290892955115834758283217655958656906740159184644622411881735 196.0, 0.000006427743041457964101820491833615766309172904718695257049984869826 197.0, 0.000004846138841600922819986563916768196033217534137364661351063222594 198.0, 0.000003640451995839176832005269019404512023990087992069850865513862547 199.0, 0.000002724913833400124359277496521424771825708326202327341785510684883 200.0, 0.000002032375292724669863181184346755065862845674312980101460375764666 201.0, 0.000001510511835080207220256831979761405004060226454871868511200662505 202.0, 0.000001118738660020585943453737393667497874445939421531873232566545147 203.0, 0.0000008257192664001246524413528644036801925578540835904202875303066829 204.0, 0.0000006073656094366751343710466686563045697103942079328823582860104439 205.0, 0.000000445242402327352288276869985395291261071769193157459406289279198 206.0, 0.000000325301224927588603233565259766889834906308302178606486801430125 207.0, 0.0000002368818870639679435094899813658856240847782923430931950186551552 208.0, 0.0000001719289031463975295361912351368541690403631309144486560674745002 209.0, 0.0000001243799914222556596787897267940347623383707113773678218171302918 210.0, 0.00000008969128712410559200889493476028355874331267304922255830888474017 211.0, 0.00000006447055576449738650545535135958532963232564648087777557794350561 212.0, 0.00000004619522918802623719393104676714088597728913186570476935491392362 213.0, 0.00000003299668707308886520548793102627919570387882898725399389117054272 214.0, 0.00000002349599351177112215917925824468089093495640796136390826898922057 215.0, 0.00000001667938900182790772215837119231022364672012861366996816826165254 216.0, 0.00000001180434026771586062622433301986205354842022264866382084469948632 217.0, 0.000000008328960420061087670861823039123533789519580582700390716549643563 218.0, 0.000000005859215101737564045789048230862250553184853923509220182641476485 219.0, 0.000000004109600014914016509576668243026645482500101677481017290650723241 220.0, 0.000000002873974271203576921459799809015978839229476633957330867257838973 221.0, 0.000000002004015091086440418923438302028547526306914042714549380789028765 222.0, 0.000000001393366335933577451015666326889224518470409067107375889060147981 223.0, 0.0000000009660222378848266164247338720756958120568372738482742596806044809 224.0, 0.0000000006678478580702497859390760613020707819144613156896559352965501106 225.0, 0.000000000460412943237255493110885260670255711603096824079126447656192966 226.0, 0.0000000003165249204387614167288834793375797743579390838131754660274720147 227.0, 0.0000000002170048152818689491473000369928682712420841346027551453445200035 228.0, 0.0000000001483687547757204874523009256286568824647912456382185796320340853 229.0, 0.000000000101166692764822924404060847686657570194225838621810230015751078 230.0, 0.00000000006879627746251973258132122762872331936622826689565333714874627833 231.0, 0.00000000004665892662868064402693141107291078516322040924509627581884819966 232.0, 0.00000000003156145140289820404455182608250547881873480272174880876807540092 233.0, 0.00000000002129322679752942940662315611181192813132040223203262873911791917 234.0, 0.0000000000143284109233757115779425535491294975615424538536585269037991149 235.0, 0.000000000009616925057430439008824512630505121357253921947104010399342806654 236.0, 0.000000000006438216670608200905534653098498714354351365720106748082374231193 237.0, 0.000000000004299277471550791372151619094083143123584708018630954641306980365 238.0, 0.000000000002863750319445241455780234563935790037433896109903725706927225026 239.0, 0.000000000001902802693267886160744467955882087687978286615760044271985807344 240.0, 0.00000000000126118943268596499670666947886588453647877791797551150997143561 241.0, 0.0000000000008338803941799766496808285044265916413687107934251539926005469872 242.0, 0.0000000000005500129072786021323753710418412787684891590428092693844514544215 243.0, 0.0000000000003619064918512893664071024301821507419276615179362871357537500102 244.0, 0.0000000000002375652028184923183596874458317321216690532524402664289833485673 245.0, 0.0000000000001555753288841661422947777549420868738870994127635686343989759946 246.0, 0.0000000000001016432287118288133669614207623958404060177210117995527650985906 247.0, 0.00000000000006625280623162343762904718493835592711388385474137337598717147147 248.0, 0.00000000000004308502835891840004275002717894547676194598003875576905211487219 249.0, 0.00000000000002795457186358654576589215431940328365644535867837058336322752339 250.0, 0.00000000000001809637742932401070865832726713692402666584882138143758787646894 251.0, 0.00000000000001168828169320333642591869168882971117762290038009940708579281122 252.0, 0.000000000000007532475178389598464837725200579505923415758853952208551714149722 253.0, 0.000000000000004843512924091507702000526902087037788586288685114787181503826699 254.0, 0.000000000000003107610202755990603359674574552059685064298727227713617218659629 255.0, 0.000000000000001989497760005081645262096443282837550899428438358298114498244105 256.0, 0.000000000000001270921247528713336368028605774974822386985977923611850850637065 257.0, 0.0000000000000008101388128283124455639210724587409159683376069084061016967011606 258.0, 0.0000000000000005153153846315609866090390766433459951228736747911413468053338365 259.0, 0.0000000000000003270896643627239599185220223650887039217774594050463993710683994 260.0, 0.0000000000000002071799231733687881316071967015647281480770176499315138998247951 261.0, 0.000000000000000130955172386965200057966928649795362006593940503271116178340505 262.0, 0.00000000000000008260355075357669682231156078699533820913564956711776968428176151 263.0, 0.00000000000000005199752772413675971966986716024246624697183586149426227839020193 264.0, 0.00000000000000003266490239574244702465574405014739382776169376463527888418942423 265.0, 0.00000000000000002047865451855816876260053493106717918327244083035707476182914804 266.0, 0.00000000000000001281296020688035373288093575078683762029709006301533976495921505 267.0, 0.000000000000000008000775472583484869097834577152573543114593238614706209904402136 268.0, 0.000000000000000004986039407252087991448450152802829703648825438686606403007467143 269.0, 0.000000000000000003101178375768868325369292991502067428702144585256055220508924728 270.0, 0.000000000000000001925092185935205592251104522989611636473998049200394329913002051 271.0, 0.000000000000000001192713997675085943581721967136729362762101705413410856558225839 272.0, 0.0000000000000000007375429054016329456417127631390435379025814344786328036289163802 273.0, 0.0000000000000000004552087189247248026742285796844864422297005940572255915466303833 274.0, 0.0000000000000000002804220854988206954166187723704417239209169041928827375120747948 275.0, 0.0000000000000000001724241943066050276260727307296665054180771424029467785746021939 276.0, 0.0000000000000000001058216343225295558123662128037080750361168400857165563533685641 277.0, 0.00000000000000000006482564543928450232647673810335944708849220215237221367961780858 278.0, 0.00000000000000000003963885983204890730718928265982608215343842977987498285392487949 279.0, 0.00000000000000000002419374557425353276858909411594530850357067731794423170920425654 280.0, 0.00000000000000000001474003059592235459716865620260544631708893851979449261472475406 281.0, 0.000000000000000000008964221733748932589897143683055309652679852632844303719370648958 282.0, 0.000000000000000000005441908996996891077517240946921999510409084472055571660069008198 283.0, 0.00000000000000000000329776670496488170040206592160196924259276786990904616850493615 284.0, 0.000000000000000000001994913111642523429305844753658623627095865964181354652307715694 285.0, 0.000000000000000000001204672003327388002512650728843507930314530254503140499721207461 286.0, 0.0000000000000000000007262058540692415839186079511933270836889386088802918378124095475 287.0, 0.0000000000000000000004370207237284066910790453425539804820166468199015716227884700322 288.0, 0.0000000000000000000002625432666420253645458230646876009721431302793940080673130189542 289.0, 0.0000000000000000000001574568131490285338747416539769704933532786489551271174972383962 290.0, 0.00000000000000000000009427333056413121214712519509660635328450022813977910675308102613 291.0, 0.0000000000000000000000563492463394957353877816567781262942616703119079244238690415857 292.0, 0.00000000000000000000003362515052230581067277738850738842543081166245425774702925482908 293.0, 0.00000000000000000000002003190126397487111528828552777957550823461131742541332672910609 294.0, 0.00000000000000000000001191425270018860597545286581193568401224695251471990458371791903 295.0, 0.000000000000000000000007074617924725253984415059636638448091730420691914131502114012045 296.0, 0.000000000000000000000004194068454174603993309506016775815039529506094988081769566452552 297.0, 0.000000000000000000000002482384935881002155249126832611026270407388625034222613811600496 298.0, 0.000000000000000000000001466927081181563080055730486025648891610541160715262483473766893 299.0, 0.0000000000000000000000008654826443342959121757917996621952791575715892937039504719015385 300.0, 0.0000000000000000000000005098274831148956798498583424017252517677343537167974941746695731 301.0, 0.000000000000000000000000299852534805628847931403812869510429344726156854381524928697071 302.0, 0.0000000000000000000000001760825507493826881203334547155810815899145929777614424911569355 303.0, 0.000000000000000000000000103241308630716242561303069678255941486003909471621608758518445 304.0, 0.00000000000000000000000006043989328958227832934513420278837692654990493195090125870502186 305.0, 0.00000000000000000000000003532899359830790063142830026136666728486443164073526999529975452 306.0, 0.00000000000000000000000002061961585840275095548140047399797751829605268531930070726017641 307.0, 0.00000000000000000000000001201644205681522640598118618403478842805170213195788462467407987 308.0, 0.000000000000000000000000006992323279006093148877133704101886864927464089287006118461647275 309.0, 0.000000000000000000000000004062763943074475602832215972969023266752400685621639286461854983 310.0, 0.000000000000000000000000002357112653570237794992160543140448831122713375563182708610402444 311.0, 0.000000000000000000000000001365531992546716384870531651116533400501181138651498094108874828 312.0, 0.000000000000000000000000000789933080531652634173269776174094301164021870620074462714125485 313.0, 0.0000000000000000000000000004562991470741501886787654957523235542670306354762251204955765352 314.0, 0.0000000000000000000000000002631988330161346696050448799953250119515152515389050870962436796 315.0, 0.0000000000000000000000000001515992956307916920403576243641973200627812459690836903847846443 316.0, 0.0000000000000000000000000000871953381848516739032611583023037791046272772945854086034692654 317.0, 0.0000000000000000000000000000500813593124964941253447032390466086188230439586274092691124023 318.0, 0.00000000000000000000000000002872430775935113265826604275433704177583101540857291558000036163 319.0, 0.00000000000000000000000000001645195384962570229406629061728485657924957673159795728058686817 320.0, 0.000000000000000000000000000009409870198159512749899374992488768719877000775224042388818508581 321.0, 0.000000000000000000000000000005374669661935694836687665905278789188966007655008283194887968455 322.0, 0.000000000000000000000000000003065671630161148559091789677428394934540732600029451042562671336 323.0, 0.000000000000000000000000000001746259945540017225215372779786006788657499867940015050075262521 324.0, 0.0000000000000000000000000000009933566445749813046953356399189703333007530533161568593384027595 325.0, 0.0000000000000000000000000000005643105610006143228899102048403356109619899940244676438382572735 326.0, 0.0000000000000000000000000000003201484531258372706715510936067737091665156574830650026337810091 327.0, 0.0000000000000000000000000000001813879536916960008307943300461623529325345560538556650902573114 328.0, 0.0000000000000000000000000000001026343645399915103034324715724651918486760181048620151451379979 329.0, 0.0000000000000000000000000000000579973250379728060487146080586808186082951053942396919678708261 330.0, 0.00000000000000000000000000000003273085825572625964036975048579088578363388113224500533693492115 331.0, 0.00000000000000000000000000000001844779700245430825463416301423332228880349657791248237825043211 332.0, 0.00000000000000000000000000000001038419198940776804956634139153862360448793095731328570598710153 333.0, 0.000000000000000000000000000000005837748074375289271022287303175926440946402500942398113489712515 334.0, 0.00000000000000000000000000000000327767405491363659948203515657677760778140245144040180459160735 335.0, 0.00000000000000000000000000000000183796506901573936159617308761511491955899685929393204808378154 336.0, 0.000000000000000000000000000000001029350052773835205170147197018370559709483691934020912370006344 337.0, 0.0000000000000000000000000000000005757666690644357791556051781106296425144520705687032163414082697 338.0, 0.0000000000000000000000000000000003216553316377924151508453659370551196188112651841349108612917918 339.0, 0.0000000000000000000000000000000001794729411990828378779622834086022116137224343257362641472195674 340.0, 0.0000000000000000000000000000000001000171222220563894478653390340332494182329971263802520651865122 341.0, 0.00000000000000000000000000000000005566984441888191210319266015560772608884506238942435691799424178 342.0, 0.00000000000000000000000000000000003094845987994935055806172150284848968892356984217995458172703168 343.0, 0.00000000000000000000000000000000001718440627896068673429353127230626376741965520678436485481471961 344.0, 0.000000000000000000000000000000000009530364839167104017682112000158349770956356462407090772416037574 345.0, 0.000000000000000000000000000000000005279187017656803987695624219160146459369487617678250043848573798 346.0, 0.000000000000000000000000000000000002920855619257306172896891439541235367479223528950366866031891061 347.0, 0.000000000000000000000000000000000001614141575268518005734226290062569058823197884782439365350200388 348.0, 0.0000000000000000000000000000000000008909731013163455374108686945074450840425886782630070462915362282 349.0, 0.0000000000000000000000000000000000004912266449844729970707956292430964678352600754496881848404998554 350.0, 0.0000000000000000000000000000000000002705181980603175596134575636985821986967588278555916844082179685 351.0, 0.0000000000000000000000000000000000001488028236232754435869016311232404586363062394844799738312265983 352.0, 0.00000000000000000000000000000000000008175772893259873618046076415305279307480458538233951565179943847 353.0, 0.00000000000000000000000000000000000004486960501569159519197048074163604450105424374900377987776965093 354.0, 0.00000000000000000000000000000000000002459711946975876719020634400535061533753745538691882890453732437 355.0, 0.00000000000000000000000000000000000001346875967358768165720579490934333716890552088344381263427292263 356.0, 0.000000000000000000000000000000000000007366904759526611977925648313360209843778851595410959381401990797 357.0, 0.000000000000000000000000000000000000004024939261410427242447457970343740444539515547772542278797462393 358.0, 0.000000000000000000000000000000000000002196610898724556925947283879241403779257963165765469359135182464 359.0, 0.00000000000000000000000000000000000000119748241608596091752329577161356603325995618537916902975416571 360.0, 0.0000000000000000000000000000000000000006520937258824266133033578001259486260166624527179793940305919198 361.0, 0.0000000000000000000000000000000000000003547140576189688390603642261345532218325666315101895768401488518 362.0, 0.0000000000000000000000000000000000000001927422368546325562187906806719203314197712026955685554357842409 363.0, 0.0000000000000000000000000000000000000001046184057998619833856380519524373353543678571731142368281460972 364.0, 0.00000000000000000000000000000000000000005672500990369849654548437374125915139061339865794007056273701582 365.0, 0.00000000000000000000000000000000000000003072408035785904516449560060701550613568582714316083040129368294 366.0, 0.0000000000000000000000000000000000000000166235419105907477471479357617964807843616691506157887797473149 367.0, 0.00000000000000000000000000000000000000000898485555772389707132979019346423348468815961804500674936662633 368.0, 0.00000000000000000000000000000000000000000485114204437097671176207048638329502097439436986604493313612282 369.0, 0.000000000000000000000000000000000000000002616523892315372480001719096248234478569106949862928884399478186 370.0, 0.000000000000000000000000000000000000000001409794097342349860475660139039090187175124957232456152211853008 371.0, 0.0000000000000000000000000000000000000000007588210471627858912318118286058220418884513700615449077415049776 372.0, 0.000000000000000000000000000000000000000000408016901982041000950623313964154468071162781510311510709867353 373.0, 0.0000000000000000000000000000000000000000002191666320061622000000965846136496420041016885565094077130209979 374.0, 0.0000000000000000000000000000000000000000001176062973479568034214698039882602268350594055638179866071670412 375.0, 0.00000000000000000000000000000000000000000006304475110902900159630490965619937298720229323770620005026834086 376.0, 0.000000000000000000000000000000000000000000033762285484405523672301048999221807750201249470943867352051286 377.0, 0.00000000000000000000000000000000000000000001806265555051044151071472129067666264436841699160227014067431372 378.0, 0.000000000000000000000000000000000000000000009653849327168043062419018560223678539276357203630280113250153853 379.0, 0.000000000000000000000000000000000000000000005154551259318421363253300693528473003299103489381502845087424348 380.0, 0.000000000000000000000000000000000000000000002749507434861845808810433359803128923774374516494256669875065395 381.0, 0.000000000000000000000000000000000000000000001465193096581893822485486876957825855330305994467417272900610616 382.0, 0.0000000000000000000000000000000000000000000007800330105205044099945029246188314581582651902490177851379869894 383.0, 0.0000000000000000000000000000000000000000000004148694535201386215165128182771295571232273073478254889739815429 384.0, 0.0000000000000000000000000000000000000000000002204410564161749322265387210174166190567065967338111273435006719 385.0, 0.0000000000000000000000000000000000000000000001170194968921555942890099119006560372405480300982893488321061479 386.0, 0.00000000000000000000000000000000000000000000006205986731350803273597732443086259125645058156140955810691604154 387.0, 0.00000000000000000000000000000000000000000000003288156425232339469504392520127347996733838067627613371229575249 388.0, 0.00000000000000000000000000000000000000000000001740544891658146635513852683352542759361398645127722448956355981 389.0, 0.000000000000000000000000000000000000000000000009204733629168704572291213759501054024350912562698646595223647054 390.0, 0.000000000000000000000000000000000000000000000004863316647339008972212314882193498205910740732977861835399513849 391.0, 0.000000000000000000000000000000000000000000000002567150128938471426431781684758541367337195701780937188875826217 392.0, 0.000000000000000000000000000000000000000000000001353846549594129952367206978076688212796439015567942676048114166 393.0, 0.0000000000000000000000000000000000000000000000007133277405660794693643125669925962505936539086580303378570635077 394.0, 0.0000000000000000000000000000000000000000000000003755020446885946789523687840745782106548732219462868594306603002 395.0, 0.0000000000000000000000000000000000000000000000001974881448535320859965632803928951190479301451525222395958837957 396.0, 0.0000000000000000000000000000000000000000000000001037713055061914697203881930487265774382112089185611533470464307 397.0, 0.00000000000000000000000000000000000000000000000005447823599208513232196106752988095893009209377526352014995542746 398.0, 0.00000000000000000000000000000000000000000000000002857460683494023790749111735693214420519871907102261934244505278 399.0, 0.00000000000000000000000000000000000000000000000001497445106987673472275190439151086453937481774764285116519283027 400.0, 0.000000000000000000000000000000000000000000000000007840376433202478990412303881957461767420518554466770228749377645 401.0, 0.000000000000000000000000000000000000000000000000004101476191636290565790051097911947870491550897768167392615835564 402.0, 0.000000000000000000000000000000000000000000000000002143693338806252375570909571505134751362163277943040824631131555 403.0, 0.00000000000000000000000000000000000000000000000000111945385835028586427152070520542294289659368203359258215550504 404.0, 0.000000000000000000000000000000000000000000000000000584080477298548261994918150698692322521424386391127789404577776 405.0, 0.0000000000000000000000000000000000000000000000000003044836668380627085638100452939603164908636069786960768022541919 406.0, 0.000000000000000000000000000000000000000000000000000158592257186176116251665681394024303905417062978180994309329633 407.0, 0.00000000000000000000000000000000000000000000000000008253315833553290315133868550668766080352253897612183536771991102 408.0, 0.00000000000000000000000000000000000000000000000000004291462064260579057749596149055243837200715572639966823881504165 409.0, 0.00000000000000000000000000000000000000000000000000002229534665340421399485863395577862974603792770276421046426742218 410.0, 0.00000000000000000000000000000000000000000000000000001157329746472668152157007879321368518079887126719131260008294791 411.0, 0.000000000000000000000000000000000000000000000000000006002549096216360923064650340135173872161617103509243027385553372 412.0, 0.000000000000000000000000000000000000000000000000000003110654775225322084499410202614393971603106198950305898878776459 413.0, 0.000000000000000000000000000000000000000000000000000001610672175070145914560970438145347176244263940003336208756260197 414.0, 0.0000000000000000000000000000000000000000000000000000008333040426553712166236667488314542627869044545917536608619989349 415.0, 0.0000000000000000000000000000000000000000000000000000004307671185639633114853010666497220328279192358233461920837527732 416.0, 0.0000000000000000000000000000000000000000000000000000002224979660521116978358239257930050521488233567197703976872852666 417.0, 0.0000000000000000000000000000000000000000000000000000001148300876417885647151145249400580690318238541458485051430588415 418.0, 0.00000000000000000000000000000000000000000000000000000005921519903407363086636551381348271922375740211013640951343872124 419.0, 0.00000000000000000000000000000000000000000000000000000003051126538822920981376590460519976269168668673490785688807278431 420.0, 0.00000000000000000000000000000000000000000000000000000001570863487972276175040466791929724786343290792073876927302285211 421.0, 0.000000000000000000000000000000000000000000000000000000008081082308248964610560593127007487182463606231794216598464594398 422.0, 0.000000000000000000000000000000000000000000000000000000004153891237288853636041819788384014292803017523133704577452510565 423.0, 0.00000000000000000000000000000000000000000000000000000000213352066361687350036106889991106737677983004028235650496309509 424.0, 0.000000000000000000000000000000000000000000000000000000001094955202084931419342645389523765886387994440734383095560493132 425.0, 0.0000000000000000000000000000000000000000000000000000000005615070673833054792707750032624546178861038473137832654346415451 426.0, 0.0000000000000000000000000000000000000000000000000000000002877233322058289549200545117350744653547206436264408597492656897 427.0, 0.0000000000000000000000000000000000000000000000000000000001473185733642853754454180522313488985596041958456314204537319729 428.0, 0.00000000000000000000000000000000000000000000000000000000007537096485882387459964191065979554995304288580700948639093855509 429.0, 0.0000000000000000000000000000000000000000000000000000000000385315394037369756401882171831471866937539768401769518921639399 430.0, 0.00000000000000000000000000000000000000000000000000000000001968320959143937542655148858040344514178573312950107312130992894 431.0, 0.00000000000000000000000000000000000000000000000000000000001004718250056949428091659275228945961461200135628223202681843177 432.0, 0.000000000000000000000000000000000000000000000000000000000005124635931726424164923753597806700833346032097084836484181278785 433.0, 0.000000000000000000000000000000000000000000000000000000000002611882408595847520787256549257288672518498558887356413669605387 434.0, 0.000000000000000000000000000000000000000000000000000000000001330202053376260566620358831177208622111794769695283821099631677 435.0, 0.0000000000000000000000000000000000000000000000000000000000006769498577830941548545382689267953531181227411254778367185480011 436.0, 0.0000000000000000000000000000000000000000000000000000000000003442482773624779805637194791069886181801959611534164601855639118 437.0, 0.000000000000000000000000000000000000000000000000000000000000174930251187043850833408870453127358014295475360818564253401699 438.0, 0.00000000000000000000000000000000000000000000000000000000000008882544790329280245561479860867419452899914758684104211645672428 439.0, 0.00000000000000000000000000000000000000000000000000000000000004507032577833474621547844321578032959372704893940034088175792568 440.0, 0.00000000000000000000000000000000000000000000000000000000000002285210683620370585583515246087095779552674372158851713234856187 441.0, 0.00000000000000000000000000000000000000000000000000000000000001157831855800454811875870322044445595837646631774736067930055655 442.0, 0.000000000000000000000000000000000000000000000000000000000000005862054821789233446257289690659100914752256154664791628677077938 443.0, 0.000000000000000000000000000000000000000000000000000000000000002965792843999259312104355616758537037922263128257231005144416185 444.0, 0.00000000000000000000000000000000000000000000000000000000000000149940764127480409705098998154885996594151768862324022277104687 445.0, 0.0000000000000000000000000000000000000000000000000000000000000007575093539634226195849101224288358174766729936493579359594844373 446.0, 0.0000000000000000000000000000000000000000000000000000000000000003824256805464221761195531586555100471239595594662879881583237541 447.0, 0.0000000000000000000000000000000000000000000000000000000000000001929293270675653979586389002958938194681615600417834415109092146 448.0, 0.00000000000000000000000000000000000000000000000000000000000000009726195162621776985067035507290659789264626739818797119402134712 449.0, 0.00000000000000000000000000000000000000000000000000000000000000004899847974376302916124570967750288893065056004144940826002528486 450.0, 0.00000000000000000000000000000000000000000000000000000000000000002466712215501703722771979224921675618276280358104526761395966075 ././@LongLink100644 0 0 160 12126630646 10256 Lustar 0 0 commons-math3-3.2-src/src/test/resources/org/apache/commons/math3/distribution/gamma-distribution-shape-100.csvcommons-math3-3.2-src/src/test/resources/org/apache/commons/math3/distribution/gamma-distribution-sh100644 1750 1750 140170 12126627677 32574 0ustarlucluc 0 0 # # Licensed to the Apache Software Foundation (ASF) under one or more # contributor license agreements. See the NOTICE file distributed with # this work for additional information regarding copyright ownership. # The ASF licenses this file to You 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. 0.03125, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000101525886200499695720910027704806535476492605455347010117696188 0.0625, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006236984391920056168198990734444763104564785445922383649647179826 0.09375, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001638466631798327740191622957231744173951348386338349176205440442 0.125, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003713648738044600765766739252470470205671394345505764588148317082 0.15625, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001413580052547067341777257822564647853284064146711289432599645271 0.1875, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000009455664713222542074175254187280525349010480602191101389605014157 0.21875, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000388913066960929789265799234352714909411592480022612064102823814 0.25, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002077225203404755562597817221849724686610900713631846698529208343 0.28125, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002333520406251065883643520078365355407423805446755208643375110699 0.3125, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000076635759531166442699548588812029824517675377693515568888652921 0.34375, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000009305411736632103878365603686370727225341233134192640242134594086 0.375, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004968570078153503414263779262094520354150227337102998034648654981 0.40625, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001330786040807018020284819640370266193510102612546149023483743045 0.4375, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001980706778834479076441980091969187947650791632179617138437945637 0.46875, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001776673397999419579843037504504497005015732945593108013273106942 0.5, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001025367466127659894769916406688109548924241493287232467139784294 0.53125, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004016731152570189731681988432450981347081222417701426257571636127 0.5625, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001116441189460572846604804970523006207932556562198944800093948646 0.59375, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002285033846395674284582206502977329561779474027418514315441470316 0.625, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003553726584630887789364740086377540446859637473884642868319755347 0.65625, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000431372958047472162775834129010516645515929755763969688274192727 0.6875, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004182312083727024312982418177830779956995196155832626175849788274 0.71875, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003304114573342625736676975881205581409644636442236570115661592488 0.75, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002164415122511238622814981006465116179848406354719167717035629689 0.78125, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001193759349959787129169676369284396679907825351289666246997751057 0.8125, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005618827174916347133880517143834679900448090689863313376972693348 0.84375, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002284152911860825047935089154214454977232616933225704452762646921 0.875, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008105614085932746230130666213223216537021677442289934736716156869 0.90625, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002534958824103317136303255136473313012466927591035717229109797328 0.9375, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000007046957172040349632057005969418325980682578821785357623447255697 0.96875, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001754823339048634175848814659671286121723195171704619326751468261 1.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003941866060050479209744717573598688470559588840463402689807157696 1.03125, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008038265656088681644587377259124334860038616154050836886064035241 1.0625, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000149666082705684603430459969859052895598427842965826865183449829 1.09375, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002557851296619231298459466912794561222269864984020543076623543516 1.125, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004031946635754342690906960708733381109213277395549539169039382455 1.15625, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005887978012507479073342070719454571839008356771980066246462800412 1.1875, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000007998339960983973048217796203097741324137292204804421182652237969 1.21875, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000101449097395421563502329183837067421062863103840416572971277372 1.25, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001205645225708401416475766802046183457017595217922008665555556957 1.28125, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001346822930548076792806326902141366881731610906444161431045774237 1.3125, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001418458824627842726662409195778503744819595075274103405348509647 1.34375, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001412350161499115191494517922962626829974316053676431085102531682 1.375, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001332933709041016305686346552323194901204592929507816736332860436 1.40625, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001195255470843576214435228567666655295723504578835247490933089974 1.4375, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001020646875381834445192261960540926629939116991339567929974941841 1.46875, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008316994359590198213212041736958244041642497300048580002500300659 1.5, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006480212819234045453899993819139151697446608451324761186671516627 1.53125, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004836675924771696287557783722184626840399491515807974383271745997 1.5625, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003464126899439031692554669678513898884817888167725702038364884528 1.59375, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002384729338856369505992159275868962051718496612791686220800319715 1.625, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001580341621810409018147795650151267711781967192656448913347394776 1.65625, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001009623257719484902519042720875711327958365293504841724731361234 1.6875, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006226711954488972705681183475035978776725906075986384148100128695 1.71875, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003712026865862262508725803398022267745955331620946434510802108539 1.75, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002141646668147303461415369705335563015647391299680926494699142689 1.78125, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001197215021470100168829968443976376307620902447951674794496248364 1.8125, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006491739824737971510655530093164342742218344351653375380181940119 1.84375, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003417961709879720764984829265973496270865672603276596767292175137 1.875, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001749122095706004767567251566050483126462109145877948782925695243 1.90625, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000870819812896122164307146359565513168554229277558193546988211676 1.9375, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004221630251411394464011862459199173495950321652254331206579512173 1.96875, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001994553207762686886712355995935573155884098320830575667135487572 2.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000009191300226354658942029129992622000127072574261681011996526464134 2.03125, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004134377541989686053371411296702290851926378914794025289118890971 2.0625, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001816626902285969890802956606769706873872374677442090813446121495 2.09375, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000007802789439451281481268593110509520295571609985976031180592769444 2.125, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003278348173624739295218157859446980855694300435596112438520993339 2.15625, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001348220722269384320415518042486781791680354125175771263000869964 2.1875, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005430443179139847360809462183781016317905669615653386771348157997 2.21875, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002143552541159123501455607805225013851797369321661014130909081137 2.25, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008296655436284505623172412420562960498620227867390326759535635726 2.28125, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003150484568309086415647318220931025223850126009971557071824960976 2.3125, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001174309990229598531271022976993780778586056994300498336791909276 2.34375, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004298687508930505578857353011690382594939047342116069834263850531 2.375, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000154612546410179503924813861065976288801578980319590367841578966 2.40625, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005466497912669052619674911950303280324533181496974994162988676313 2.4375, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001900734065483243461512407947918848354851991614681868917381848356 2.46875, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006502285391374336170041107088570022792586642780036776941052644767 2.5, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002189379256792221554678908065413375723918286281007625081582952733 2.53125, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000725867669727626849275463403280227003183856597086739736009375624 2.5625, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002370501681529090020927176607203377614434768234400115486680730547 2.59375, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000007628309837955751110077055684866828174157646505740563154335792686 2.625, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002419773948388999710112437226260854137642855329206054686848324201 2.65625, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000007568806802039747770558821973727915494318705354675325676301985448 2.6875, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002335225071090304812519892959748138032537087121947565392541942132 2.71875, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000007109128726838181063741827318604653247613502498200655183643411589 2.75, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002136108013452834824555611340745683726916751652714399555380998202 2.78125, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006336911204761581623923405679332855984177641581935800367218829555 2.8125, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001856537371052938790054060269633464045643714434023211064913447814 2.84375, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005373056389932544500338593817842707159195249327554108784091390053 2.875, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001536550234923824440282913343343030449160089299816283170811957022 2.90625, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004343022310936202279562661059578773031455345940591730692622559696 2.9375, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001213573144869126853003484165753289082111798155949514301258847482 2.96875, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003353309752913068298200618850578512871679613301969961854850476213 3.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000009164676019446674196459527165190615441837979440232191031936889725 4.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000788419866679760178086756223524856410273231365822270502870636571 5.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000001139080363163845923501700286733498088263501100130285438224780709 6.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000002892032984076451735243825582904203310247859721992496701567658374 7.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000045148251265064833861653768585185178771087340939844851348799647 8.0, 0.00000000000000000000000000000000000000000000000000000000000000000000009152699465290857596208216842307644797802625585943426579007616294 9.0, 0.000000000000000000000000000000000000000000000000000000000000000003902603616301374401423236581255655974513954805294301476973505249 10.0, 0.00000000000000000000000000000000000000000000000000000000000004864649182067610436458701223522755117545181424119392883621187348 11.0, 0.0000000000000000000000000000000000000000000000000000000002241985889945582183652952010323186985616135241303709367972963667 12.0, 0.0000000000000000000000000000000000000000000000000000004543660997030904445374367381193484247192188367117354635923258858 13.0, 0.0000000000000000000000000000000000000000000000000004619127594055862836556421500497525321895787708436322914278694354 14.0, 0.0000000000000000000000000000000000000000000000002609450907787229265618970459277574235573983004018931029746302937 15.0, 0.00000000000000000000000000000000000000000000008884106646231538598078298430314058288128779083747959015726543572 16.0, 0.00000000000000000000000000000000000000000001946089985387327763807635406514152452908861892700368352035010384 17.0, 0.000000000000000000000000000000000000000002893565844390712934996737540187734349642239818280885241257513303 18.0, 0.0000000000000000000000000000000000000003052626543717402514732050640821800393677208672407121300485474038 19.0, 0.00000000000000000000000000000000000002371415659909633737084897962496080762465186428360518973566054701 20.0, 0.000000000000000000000000000000000001399833162749540525535014869337029999863204212845335346018987116 21.0, 0.0000000000000000000000000000000000644944589802876268408619906921049273007782316313203171997633374 22.0, 0.000000000000000000000000000000002373357410793031225286511503896872647180222066185227180128871191 23.0, 0.00000000000000000000000000000007116706037693973302035434948521231337096245119407732460408323425 24.0, 0.00000000000000000000000000000176946390945000659087248744354099241334543140192905415229816946 25.0, 0.00000000000000000000000000003704206216383417687382434636591020469825248229201201692219634079 26.0, 0.0000000000000000000000000006617611461248020758834633361996919337276667806167226550394122683 27.0, 0.00000000000000000000000001021075805443667973242903651498627036730203121686726557976041755 28.0, 0.0000000000000000000000001375295287518427723294540574806251995094075866060996086197477638 29.0, 0.00000000000000000000000163251869023083428395520306160766295895987373790388529747579397 30.0, 0.00000000000000000000001722527267279472427156475038785242326062383109364919779201292777 31.0, 0.0000000000000000000001628077580513621760160948144738987076470640440109822231923241992 32.0, 0.000000000000000000001388100076169993784668797503219154984527155522802891342222188763 33.0, 0.00000000000000000001074381422989729494661881107915038201838879697295790056412559109 34.0, 0.00000000000000000007592708867747354045496188726767026699783573427304184970183809192 35.0, 0.0000000000000000004925222664969722919009683339673520663703437804309789954278535927 36.0, 0.000000000000000002946745118264090553841516829670970481631784017004856833563436159 37.0, 0.00000000000000001633319689590425667428978282797258542651674279601751129642527009 38.0, 0.00000000000000008421357379561955542158241239006740103345796957525136895116639197 39.0, 0.0000000000000004054223126683415182479447304921605542853525303154486745452238911 40.0, 0.000000000000001828757880581294976657522393232487468465444296668440327920599725 41.0, 0.000000000000007753975108250123733292655460540279380263578815038050033439238089 42.0, 0.00000000000003099615719621105638167879951373870499824926485606446032311188299 43.0, 0.0000000000001171414851497323124921212561450104699142870715256125364260957319 44.0, 0.0000000000004196180040463759259033674652807025188415025104712817236241236366 45.0, 0.000000000001428181895511647667570430387768373023103485507566357852178711018 46.0, 0.000000000004628875651567555278188643409630606218748375055805692096277179548 47.0, 0.00000000001431672330189130066783911858388009275925910024208673035807314911 48.0, 0.00000000004233930395846124558186670509616250065760581774301557725978997698 49.0, 0.0000000001199440350371566298677420826680101464338990597861000783890209152 50.0, 0.0000000003260638704295460052343810729435128246597284319136247760742048665 51.0, 0.000000000851971764533611603165195758861474199377427868552812842637326714 52.0, 0.000000002142961191566209888433567841461872576074366921734763502241526282 53.0, 0.000000005196367942944164736226510632249575393229196350019863465617046334 54.0, 0.0000000121640037394459980517407318705968635160461999347922529131753346 55.0, 0.00000002752365468731173013596453712442634490912404854103999560284217213 56.0, 0.00000006027261472938364219869380614879061920379623326543316256477711869 57.0, 0.0000001278855832092003330237821852786556863366737367878509194109216664 58.0, 0.0000002632011349248908591667277993603843206233354476520950938974368468 59.0, 0.0000005259822221839870283079853880842509523119172091969227391973644931 60.0, 0.000001021647835129563498573049448585331145778912969236996859406491534 61.0, 0.000001930574721109789137053135806912018656616224228224773699235633971 62.0, 0.000003552349827857868954086874692487244311584199128929243258938672447 63.0, 0.0000063702779391422723031515086374540323314438839776931406241927936 64.0, 0.00001114209681769054496133515159973000687448659880667266574757087221 65.0, 0.00001902292646998749609017433704551509224806908656859558035408253298 66.0, 0.00003172562293932525396550955555431396574618259461481459220929562539 67.0, 0.00005172154925395441038225818263557925080811590961998394043600306801 68.0, 0.00008248100305484238474674052374735154191563749323175896370557338474 69.0, 0.0001287469877968519167629710942026568464941952659749123713436764137 70.0, 0.00019682876770053729599878615244696506408659477133848505965643673 71.0, 0.0002948931288091571591255637612693555566759402425620199893188963706 72.0, 0.000433222291248379212056546027358763369287368010734309011941744586 73.0, 0.0006243991716954364751178091130645034127606932302712867024277725653 74.0, 0.0008833746613058414715922191377952007745533739369363557653370486603 75.0, 0.001227369362672396742341914191806926517339707286750019437030243001 76.0, 0.001675565294767155794630642060097037680439521900332610839471241583 77.0, 0.002248552493403038022180465455745483462289275055912737551940180727 78.0, 0.002967511585660843122688693253444429919666900862811127925513655899 79.0, 0.003853135774397894265614684331861256790084253214562802985925854783 80.0, 0.0049243226989990745962501136239049127618006228259751181909712082 81.0, 0.00619669584629917763659332210930455587487922348905392596708397322 82.0, 0.007681043332877395794027496857607125240600414802441966811896110291 83.0, 0.009381785355089319084141973151704910577939504747511217281941945465 84.0, 0.01129559689976202495057664399994768248989478872768970271690200815 85.0, 0.01341031654215249818891822930559276140194493876346094622358147568 86.0, 0.01570426353935595906422846735202922719647576362842741491955318553 87.0, 0.01814606361024470894286704439185217299564761402617237683579757936 88.0, 0.02069505003122633849517124083865580302834508487183640667729960321 89.0, 0.02330226375894734094000671653476702697970028724163383571358878334 90.0, 0.02591202825015755649778336991747192584990603031441663106816842058 91.0, 0.02846402629082596201222531384820293704405269267469359706044665508 92.0, 0.03089576245935197791600407523283074703065746590761194893900023843 93.0, 0.03314526038435500268671224297058853268756640829999116236063974945 94.0, 0.03515382227400321923527702021029750503280867962120688780556444832 95.0, 0.03686867143613371263505831470354228553932150576931836501742938089 96.0, 0.03824530718311153668632834045005824908374613083206359286952269869 97.0, 0.03924942448198831071876291553168182787786946409622015315045906508 98.0, 0.03985828537960633668280255543707815712656489716536962058892943296 99.0, 0.04006147193132940975326751038194383583076017753388605805719877344 100.0, 0.03986099680914713523392064945913874513228946442466768749690412737 101.0, 0.03927079358016447455068841910456870079794079518936107613849743738 102.0, 0.03831564983278456934970164269448233870993618041750198494181623239 103.0, 0.03702967966282289649009248543237145916839055818455251981623600685 104.0, 0.03545445533194645922260873177521631967371913729430097491035453234 105.0, 0.03363693015020804542397724106776370667524336427579716177958471022 106.0, 0.0316272859203348411859980944283802076804436414879771347866763242 107.0, 0.0294768297089253762736235242525582539716135256392292958611625071 108.0, 0.02723604812833601970108751735448048739234169639768866239343710345 109.0, 0.02495290504507706087353050108406763623070147775792946395538644433 110.0, 0.02267144318042336405237510096373540040643911235907111556582472294 111.0, 0.02043072385790325133559678402352615471510768476729050808052496336 112.0, 0.01826411430365845454243924394654114938027308660979828786487165959 113.0, 0.01619891009564484454252904598930382093871323219851485959683820709 114.0, 0.01425626273669950571007018001196057219657258434338014826557883788 115.0, 0.01245136950631700931934226177184417773013467289598533871650594197 116.0, 0.01079387484149336330696483992669838172806911726075774540076713643 117.0, 0.009288429208294644977285102544793638535018748211298936697596892152 118.0, 0.007935352143507592076708110689841553274733808801270627399345771783 119.0, 0.006731350064312851763170825622523907638603218212396900747068261934 120.0, 0.005670245669920250687466747981009866777025950637860943610148268716 121.0, 0.004743683404490132379135971024531972480753032626610674201423373693 122.0, 0.003941783705511029641005066046866403732028758628592463680048298186 123.0, 0.003253726943844653749385400657669092758006876625597976685275667962 124.0, 0.002668255543277657625183066203785781226098265043439638941512553989 125.0, 0.002174089382881061209429176486048643285407253526279726524531853048 126.0, 0.001760255022327741180227395288871422356230444513813649371495473123 127.0, 0.001416333469786730772175980920234811792846986808287900561548621055 128.0, 0.001132634162847308697006328893501915004604931474917243462669169543 129.0, 0.0009003046627377835507992605105826060129862417734974289688258141744 130.0, 0.0007113864294468024194526313525275759733520592877253429414167008002 131.0, 0.0005588271348245359133262012613087304160727809122481346958496768007 132.0, 0.0004364594724898465561156280495052324388845805685720513884780708531 133.0, 0.0003389555176307983346699148780674001272497308866969752274404921932 134.0, 0.000261764536370136543412944995717128235079133759411354740581928027 135.0, 0.0002010408766485025170469116196034621688390975538081105687453104226 136.0, 0.0001535672952139029039561330769546599940392332309880894567186547676 137.0, 0.0001166778648191423637878335196655248564456139878897526368037398109 138.0, 0.00008818351307496893831200303521437853341094524511385547974013144015 139.0, 0.00006630229873613613130322627297446429129011559609792585272130239721 140.0, 0.0000495957441543576357775196859782486330437125114639897356593883139 141.0, 0.00003691191268093349088070664094067839771704902016736664910479519192 142.0, 0.00002733543623090223075516238225569905755599717573646416323776453211 143.0, 0.0000201443446026833628646145519265734693436265234861440658240650202 144.0, 0.00001477330515532495315096552138323848704773277076216919905574745069 145.0, 0.00001078272900640586668178334510715524766968276262760447157723870056 146.0, 0.000007833118742358106561181516280597779487519478957740306709405659314 147.0, 0.000005664005228397486661745776802582323641718504634906221318387053395 148.0, 0.000004076832305019841259674355770246669555058921349754422426447517394 149.0, 0.000002921185353798683940134046137956323060555226880837278471738750784 150.0, 0.00000208381282358263862212340769960588729876863835283009660692632213 151.0, 0.000001479951079838258397100508043520731356288692401482257316088262683 152.0, 0.000001046526674857164118696576443205985785077187746462661738827963498 153.0, 0.0000007368723575197662945744147627482068834902863477056477434334847147 154.0, 0.0000005166512625184480766471716268082228047936401012468191941748515904 155.0, 0.0000003607362357462140887391107080579772199346717025301639915663500936 156.0, 0.0000002508374663783818439713450644841378772514432137871277791492054244 157.0, 0.0000001737113818233254904439257429677924906057979184965960051710274253 158.0, 0.0000001198173784132041195023253106916923723067770113412395659172678153 159.0, 0.00000008231690446795792520296377057522477937392043971340457338824278086 160.0, 0.00000005633230521797210576690702943004567655780792554786167199404485424 161.0, 0.00000003840134944729452979193298012433979054948668230455661031349035276 162.0, 0.00000002607814625563307409896104977635061811660077125479048242057832473 163.0, 0.00000001764284550259369161449338380479585068907161688593843391343413526 164.0, 0.00000001189165414798119358377749842222530914679494913684050199589025677 165.0, 0.000000007985779388898498357369319528233262622500891153106942647530111684 166.0, 0.000000005343343147525183567546827677151895462591051067242415490986227908 167.0, 0.000000003562447813444424686967324193137759361671501917826470175569215434 168.0, 0.000000002366694908066117360925124448921129989213544209590713015639537505 169.0, 0.000000001566796744107979252728872453736864807861536189652991881503930065 170.0, 0.000000001033659827843871116464646365235518013536740491977934239563637232 171.0, 0.0000000006796023448406316420512042759345911869815306585157539836376675994 172.0, 0.0000000004453092349753449524836040253826900713791166024578287343671738768 173.0, 0.0000000002908139168502099294717681338060236460442230653546286287632997602 174.0, 0.0000000001892919190267114570607070636207764093863998098050124414610462712 175.0, 0.0000000001228086146077000608704614380997640724064041355851483953948209492 176.0, 0.00000000007941849337138878176045739331488112147361462696565066623820940635 177.0, 0.00000000005119486690010761078091211616419508179948890316064179012345388403 178.0, 0.00000000003289718939075291795774437546155200060888892582294224420290285524 179.0, 0.00000000002107337772369361938516424626106445368545306599597573438057576764 180.0, 0.0000000000134575997612344672538075057056796967110550656617768928946490004 181.0, 0.000000000008567892973705953370207503114019094750680300832155095005263492252 182.0, 0.000000000005438361208010347985066525902574037790707148113424670096123245242 183.0, 0.000000000003441628903099740912856836116753296534748735011097813779109037109 184.0, 0.000000000002171581599228548270097204029344076217899347381257276990758229445 185.0, 0.000000000001366212658983260860759301626377545410349975254859458733295225351 186.0, 0.0000000000008570461115636667175736978968817983854062833087829149078629329177 187.0, 0.0000000000005361018136861249815859835202889032087800318817677332926975890447 188.0, 0.000000000000334395808084753826603800353927883945950037871223450984019245086 189.0, 0.0000000000002079973868164366743804436528338900588109379349472366508922941889 190.0, 0.0000000000001290182889635003601761803122984616225681213869048119684396563574 191.0, 0.00000000000007980932896542701440395142411445829053665762742048038989158992819 192.0, 0.00000000000004923539897182101655286246709393703555074202667751067754755347949 193.0, 0.00000000000003029248696803042865687381385433082145739535384231503265960320044 194.0, 0.00000000000001858823349243584317202059682857541657929535829961787433055471316 195.0, 0.00000000000001137624401878787151940868870932504895660534972621520257740532626 196.0, 0.000000000000006944307707945884870128370062084557987418836724096132505281434734 197.0, 0.000000000000004228046941157708713201906860333695304390323311686860521323242064 198.0, 0.00000000000000256769103237665918467600310018048541205380517701737033938255883 199.0, 0.000000000000001555424771157526687795553198968528314950939446085280607814603186 200.0, 0.0000000000000009398737827450507055439413086891342449561325068018791901479616681 201.0, 0.0000000000000005665198967912412585257132500168250195119783157143284952804408326 202.0, 0.0000000000000003406407309772735333523396582860616970478362501548397370157801859 203.0, 0.0000000000000002043263007477511763059737526284389094975696969245869434910797257 204.0, 0.0000000000000001222668186663148114457416594920295846958326681655357338381673182 205.0, 0.00000000000000007298940101413154795300529429189941238661840928437579021846457917 206.0, 0.00000000000000004346982479387689328454144045909903464279511684425011497456252527 207.0, 0.00000000000000002582871448531971287505389855119994445899894553089747754963923237 208.0, 0.00000000000000001531137773353573489010540526780497207384192308614448235916924356 209.0, 0.000000000000000009055906971566678003899181753983616382084431938422979629597085017 210.0, 0.000000000000000005343986154315020377640850944615037917737612368211837408817950481 211.0, 0.000000000000000003146471034911638258372843071125320538599193118215154348829427675 212.0, 0.000000000000000001848487055835540592177317924048364934440145236431066436064025637 213.0, 0.000000000000000001083558692136768921308517183733231932300899122314545943943102074 214.0, 0.0000000000000000006337832826084628200963176534626410730868251122368113149508171375 215.0, 0.0000000000000000003699050562345616697744704295756762903213270150529187435271715767 216.0, 0.0000000000000000002154317047563743311318419895328185015660108645276043201079180255 217.0, 0.0000000000000000001252008920705881199757458540671898269440905509270830857610446581 218.0, 0.00000000000000000007260928450656147141513543379793995173627741627644704063944506554 219.0, 0.00000000000000000004202156063985908040370671196962546586531473726124672875724082794 220.0, 0.0000000000000000000242692149375338286271880093652443833219063023781459435693288425 221.0, 0.00000000000000000001398784913122477830168257915828623248614716656597533576038735527 222.0, 0.000000000000000000008045737021814632076213080472874292985957813498174639415061198831 223.0, 0.000000000000000000004618578426494319022914728195785817754257816496810382740447058595 224.0, 0.000000000000000000002645977909314908236281091523270533735364661951683917468351085148 225.0, 0.000000000000000000001512889504698869978593986296856695841355166886433908507970240355 226.0, 0.0000000000000000000008633341312371826744398659578553469598567568310604397821610922211 227.0, 0.0000000000000000000004917097448641603229860356060973952024507687207123501984659200526 228.0, 0.0000000000000000000002795144885867555458810621771556235984845844475880081498188374484 229.0, 0.0000000000000000000001585888833147939413945634860114434181018209411314150795787849002 230.0, 0.00000000000000000000008980929211993700072061253194818074034879449114678193233557742212 231.0, 0.00000000000000000000005076414020992202462225281877370208922676120827499362503134565468 232.0, 0.00000000000000000000002864092552096289035938575966729003286809341317297905790600853269 233.0, 0.00000000000000000000001612940115500902266665087315640375702379027377663977635055227235 234.0, 0.000000000000000000000009066871558061758999026455144322180226255603152828512947910706486 235.0, 0.000000000000000000000005087582478123327334702987199000578436375669303388682228285199809 236.0, 0.000000000000000000000002849619737149774675305065301996488251555705717595530548883295123 237.0, 0.000000000000000000000001593273706855971641876720033029443473673464980479583664522182021 238.0, 0.0000000000000000000000008892592695114045060428228247035793370652947417954502365549610363 239.0, 0.0000000000000000000000004954585921577413819288929566415432055518578524126230944737977755 240.0, 0.0000000000000000000000002755710670965094139459109781886232158051431424676438910594314875 241.0, 0.000000000000000000000000153007747419420662897716975976650035259702720642222477324007104 242.0, 0.0000000000000000000000000848111509136179961652298809901341478700795870056398729105335872 243.0, 0.00000000000000000000000004693084015644312897825439051930048458249050426635234539249165161 244.0, 0.00000000000000000000000002592600308812300438040680569953195383552928949267130812790714073 245.0, 0.0000000000000000000000000142985049834959076526915138991240348295443618994025353931662144 246.0, 0.000000000000000000000000007872803290556434421788563248105278817178467420217772143785886927 247.0, 0.000000000000000000000000004327705385277240322357869549050509391727826928551359697616235293 248.0, 0.000000000000000000000000002375096365467313673612725353360901936577158126236388640814965362 249.0, 0.000000000000000000000000001301384751834282192848817278172603867038707802353626984983858466 250.0, 0.0000000000000000000000000007119290720969086625583781004067674755915105539212300629219301868 251.0, 0.0000000000000000000000000003888479504360105351305860334941329648240739630269296091265169657 252.0, 0.000000000000000000000000000212051057698766920263479165870539731762025720667751158569834188 253.0, 0.0000000000000000000000000001154579973218360356056964830327779317764373565515739526102811231 254.0, 0.000000000000000000000000000062767652618614337753309536956087843340350736217644049625954385 255.0, 0.00000000000000000000000000003407072090352256547731118478732563731806448176250187574452711963 256.0, 0.00000000000000000000000000001846569014999301753564641784200017690112960268217843429581440735 257.0, 0.00000000000000000000000000000999294980521790781074674545758542096824537201245403465440388076 258.0, 0.000000000000000000000000000005399715826623219067676328831864497813986968277351997655001382046 259.0, 0.000000000000000000000000000002913413822564400862304691226782344086327645157177543627954477859 260.0, 0.000000000000000000000000000001569612683917048250755715681145408155466951313366270705699112308 261.0, 0.000000000000000000000000000000844397220409153835050514078863761062102292638463131410816720199 262.0, 0.0000000000000000000000000000004535967427841018830359588069925359072683771623761091066413072274 263.0, 0.0000000000000000000000000000002433137796947836270710523423009569199567660919556971037946615458 264.0, 0.0000000000000000000000000000001303292460653885281685804357422783277128458221906735026896450826 265.0, 0.00000000000000000000000000000006971081507958502071338831436097634595144427726953261604006181889 266.0, 0.00000000000000000000000000000003723455651344991790634644445846982423836640166490101811216043813 267.0, 0.0000000000000000000000000000000198602427350860837990716745872714535992188453794224281724263548 268.0, 0.00000000000000000000000000000001057839559454630609513957738623169806334388682978985522613089313 269.0, 0.0000000000000000000000000000000056267346333307892720722873356704835968802390181138538044545874 270.0, 0.00000000000000000000000000000000298881396076170969495625334700190998722844837261980394299397287 271.0, 0.00000000000000000000000000000000158544633382966990721199645001162402746555565013920987608974462 272.0, 0.0000000000000000000000000000000008398829538837581388720250238443077523945536547305403208700009488 273.0, 0.0000000000000000000000000000000004443291877274093611011314614322736377686178959769324901214075654 274.0, 0.0000000000000000000000000000000002347545524431004350695685068381103606192882205038317363405123134 275.0, 0.0000000000000000000000000000000001238655430684634314318026348048860209226674517490926559147130336 276.0, 0.00000000000000000000000000000000006527072917185749153796253711208531841328948117774742908444902131 277.0, 0.00000000000000000000000000000000003434962462828752346280262950473129988372172744052942046759722405 278.0, 0.00000000000000000000000000000000001805365637620469441310118915198033109012459610580254465791947366 279.0, 0.000000000000000000000000000000000009476588404228570883264004409183921751187251375159932133104611814 280.0, 0.000000000000000000000000000000000004968056373879030069717483249416746372630724438333200949811065 281.0, 0.000000000000000000000000000000000002601193146466678489474744383341049825978930055692466008181404876 282.0, 0.000000000000000000000000000000000001360235710240181102084191508041117283093589928599308604068333636 283.0, 0.000000000000000000000000000000000000710419843308422945427383893201611515039903966792281071198680952 284.0, 0.0000000000000000000000000000000000003705775872717264175125942992783260810229728621951251533388577172 285.0, 0.0000000000000000000000000000000000001930679315249488699793151793654177444161407357998444555043137301 286.0, 0.0000000000000000000000000000000000001004643088254077483379169608552392885590447227350932692518212295 287.0, 0.00000000000000000000000000000000000005221410229941628000038760428749139897067541776774576774808739136 288.0, 0.00000000000000000000000000000000000002710452777914277696993580780281148685483565781199171805149421957 289.0, 0.0000000000000000000000000000000000000140532738141941824388873824677812305052768828220152155811876313 290.0, 0.000000000000000000000000000000000000007277770987314779944035540124492605803284471270401511086659220619 291.0, 0.00000000000000000000000000000000000000376450623726254940741679281975058194592023828835937400254867727 292.0, 0.000000000000000000000000000000000000001944956636089637896639972092482997900071525225696143343248958431 293.0, 0.000000000000000000000000000000000000001003708409078418771723923522024996312596684737389569261583597351 294.0, 0.0000000000000000000000000000000000000005173737337961358300839063658980698417328126646912736152013518944 295.0, 0.0000000000000000000000000000000000000002663813193950744612357084002480212173870832785291721528996017798 296.0, 0.0000000000000000000000000000000000000001369963816624701547248911259170935157571647953545349964756980543 297.0, 0.00000000000000000000000000000000000000007037585795965792278535022092947101177406768166318665817448222999 298.0, 0.0000000000000000000000000000000000000000361119451146544637430446582179882613819764176701938154704488672 299.0, 0.00000000000000000000000000000000000000001850946646188305088535822593948936874728178038286526057870947845 300.0, 0.000000000000000000000000000000000000000009476675102264851244077077022585830576142918820956902898519990507 ././@LongLink100644 0 0 146 12126630646 10262 Lustar 0 0 commons-math3-3.2-src/src/test/resources/org/apache/commons/math3/distribution/gamma-distribution.maccommons-math3-3.2-src/src/test/resources/org/apache/commons/math3/distribution/gamma-distribution.ma100644 1750 1750 4545 12126627677 32525 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ /* * This Maxima script allows the creation of reference data for the Gamma * distribution. */ /* * Set floating-point accuracy to four times the double precision. */ fpprec : 64; /* * Probability density function for Gamma distribution with shape parameter a * and scale parameter b. */ p(x, a, b) := (x / b)**a * exp(-x / b) / x / gamma(a); /* * Make sure x is a list of exactly representable doubles: use only power-of-two * fractions of unity. */ out : openw("gamma-distribution-shape-1.csv"); x : float(makelist(i / 32, i, 1, 3200)); y : p(bfloat(x), 1, 1); printf(out, "~{~h, ~h~%~}", join(x, y)); close(out); out : openw("gamma-distribution-shape-8.csv"); x : float(makelist(i / 32, i, 1, 3200)); y : p(bfloat(x), 8, 1); printf(out, "~{~h, ~h~%~}", join(x, y)); close(out); out : openw("gamma-distribution-shape-10.csv"); x : float(makelist(i / 4, i, 1, 400)); y : p(bfloat(x), 10, 1); printf(out, "~{~h, ~h~%~}", join(x, y)); close(out); out : openw("gamma-distribution-shape-100.csv"); x : float(append(makelist(i / 32, i, 1, 32 * 3), makelist(i + 3, i, 1, 297))); y : p(bfloat(x), 100, 1); printf(out, "~{~h, ~h~%~}", join(x, y)); close(out); out : openw("gamma-distribution-shape-142.csv"); x : float(append(makelist(i / 32, i, 1, 32 * 10), makelist(i + 10, i, 1, 440))); y : p(bfloat(x), 142, 1); printf(out, "~{~h, ~h~%~}", join(x, y)); close(out); out : openw("gamma-distribution-shape-1000.csv"); x : float(append(makelist(i / 32, i, 1, 32 * 10), makelist(i + 10, i, 1, 2990))); y : p(bfloat(x), 1000, 1); printf(out, "~{~h, ~h~%~}", join(x, y)); close(out); ././@LongLink100644 0 0 156 12126630646 10263 Lustar 0 0 commons-math3-3.2-src/src/test/resources/org/apache/commons/math3/distribution/gamma-distribution-shape-8.csvcommons-math3-3.2-src/src/test/resources/org/apache/commons/math3/distribution/gamma-distribution-sh100644 1750 1750 1062660 12126627677 32624 0ustarlucluc 0 0 # # Licensed to the Apache Software Foundation (ASF) under one or more # contributor license agreements. See the NOTICE file distributed with # this work for additional information regarding copyright ownership. # The ASF licenses this file to You 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. 0.03125, 0.000000000000005596904708180782148228953128471505726134670596981359963913580296 0.0625, 0.0000000000006943623748308400860911551749291645603377733398446501469280191284 0.09375, 0.00000000001149882039708756269340206341778109315907267642494151114935069288 0.125, 0.00000000008349351491101160909116193824619061759748201758054047745090716597 0.15625, 0.0000000003858789897680218019081493640394438819014188219069017029653664786 0.1875, 0.000000001340133774745126942389237952354556299276180189208517551288732448 0.21875, 0.000000003821237028587117444126434114369042829244368696273676940093280958 0.25, 0.000000009431394341743164888959252093824606363166594616434508723199237233 0.28125, 0.00000002084835811446620668807334462022263162650030753361074777549483593 0.3125, 0.00000004224765322293582855349833805490318103700823763718071818009388921 0.34375, 0.00000007979573571491588555412356156934981847605190181118723756865876005 0.375, 0.0000001422092699546526536315544745007606936145002857078165836090578558 0.40625, 0.0000002413742560266709313631472408747976065329735757305978046993444721 0.4375, 0.0000003930176271214412230480705357027361555305320141276386460041852999 0.46875, 0.000000617423123254783712225687935163889961643746658004062200704610924 0.5, 0.0000009401826942470136154572785450632137485148780622011206530302767838 0.53125, 0.00000139297435635502769736440670924090361148550987191479057642866019 0.5625, 0.000002014357291679739255148035666988356693491793074214994043693949978 0.59375, 0.000002850575008976495485723212176530365011710355826874419206072983668 0.625, 0.000003956357553179880234254838307624996221680076800493892247130033046 0.65625, 0.000005395714035869425504242274686365922327889631458039837635749653615 0.6875, 0.000007242707139531209231396108454733109629611529345216321034282686986 0.71875, 0.000009582201706518155995394378645572863323570736276129419779988101143 0.75, 0.00001251058004281905842748863349688002481207269611142445524184579908 0.78125, 0.0000161364171327106381789196757408990529575007634033151943915641541 0.8125, 0.00002058110956038075759191212562535712109025633075262136951623751429 0.84375, 0.00002597945255751030195714263487485639072227925329072499151632490814 0.875, 0.0000324801602318265935154475292925663941118773322369183105457074742 0.90625, 0.00004024632467230804723754879244257669910302626734707189708103250377 0.9375, 0.0000494558102646999319685368796687325136179578665846663009424006439 0.96875, 0.00006030158018001276310318363977108379986524770699611588565632217536 1.0, 0.00007299195261338141301498487503203588639797839901423964970393587335 1.03125, 0.00008775078494657983258338674746644138857536059190726386404844919605 1.0625, 0.0001048175845808948650404680000615362610075565701646650960381326024 1.09375, 0.0001244475457349318693214920044420652630262236018141233882334978437 1.125, 0.0001469115120218385447375124591133492230069756933534394307002870505 1.15625, 0.0001724958651105237500609695390143699903641299185493080372007373065 1.1875, 0.0002015023402343351555839972814820575709743812026359620734754717481 1.21875, 0.0002342477697373950947442897637600986518730299037051981432529428125 1.25, 0.0002710637562428108286525384080294934556532954340210519585234609967 1.28125, 0.0003122962773880409498245296699565865664670051347219902301997483219 1.3125, 0.0003583052244008733135361545034086424318365670915770738780252832698 1.34375, 0.0004094638770850647442943626284199890098255920233118241105865933429 1.375, 0.0004661583180482403839155245478041267391609154530056126726840923305 1.40625, 0.0005287867892368692098786241348345471425050978771404499615573166578 1.4375, 0.0005977589940448981734332662343214656169892560003746157285344166917 1.46875, 0.000673495348434948721302503050244831917464899848830420687626775285 1.5, 0.0007564261846549727738670082923500982517598731296699536395752229695 1.53125, 0.0008469909112501345090017041211051871434736061454300758048511643961 1.5625, 0.0009456371331607019644639965570296141767101121803221615701209275954 1.59375, 0.001052819735763222463626235421004865099126957777798560133136156146 1.625, 0.001168999936755578557798754368817024016568912238842536804132352844 1.65625, 0.00129464430980805994994293386447560900208663315084936922813736565 1.6875, 0.001430223783903736257349476044411940281283607133214692897446900631 1.71875, 0.001576212622273570720584838386082138740521199934202025894570661674 1.75, 0.001733087384796262843565174082848433063347362405953851299581123032 1.78125, 0.00190132587768111877775210835843410578149644074561675851786988727 1.8125, 0.002081406094185669008543563842706043464713514832630941981902270445 1.84375, 0.00227380515003960124416316050129733291493017418460583481599267797 1.875, 0.002478998217154135700103838010361767958982874740318873463264559267 1.90625, 0.002697457459092485007685165617179337649572506152461697213703680441 1.9375, 0.002929650971663714480217276565439577186376592574333859889283280378 1.96875, 0.00317604173188030826057087420020859717917351896243570144983303003 2.0, 0.003437086558390163603657130031047222943685880531036847783318320575 2.03125, 0.003713235086357640991183112608091933361166683747287856213347322926 2.0625, 0.00400492875962670359061786107325372144797482512617848862132247436 2.09375, 0.004312599842853046482351381399478214935447058287216410090105856955 2.125, 0.00463667045614235345442097018203564457931942938680943988847352424 2.15625, 0.004977551634579284914102835784895805741557209529507590179370374902 2.1875, 0.005335642414877301250646733303932690000372403624627564226718873472 2.21875, 0.005711328951223717678906644932984956530893743183625529596203011218 2.25, 0.006104983662238174983913965714347385680113020164129412732309488872 2.28125, 0.006516964410806652256808696510141759933452763549868239608668896002 2.3125, 0.006947613718397851111659309373326532645607402474639729865934494757 2.34375, 0.00739725801531480752035441095715554405444033280963768228085005737 2.375, 0.007866206928182453219872743108244822571745572033557484498650592654 2.40625, 0.008354752605822025386442624388234138716838711073486625766397044935 2.4375, 0.008863169084516140034875567799519555525952677435907781951008803159 2.46875, 0.009391711693524389415900499304855099135169653523610848299908952772 2.5, 0.009940616501568845133648666438409691200595930918791834373565965745 2.53125, 0.01051009980487215759693179063413351154703511516992492178397266218 2.5625, 0.01110035765719831347299863980283675822121314140534727652767382306 2.59375, 0.01171156544221778637003310003364949565438584470240282137964823845 2.625, 0.01234387748839499969809013658374842020321585695307202365141959259 2.65625, 0.01299742672647689123621112690762896546819746246331995629919064029 2.6875, 0.01367232438954707376805196060566822674229135954793759443557503054 2.71875, 0.01436865975550074502220367215092917464173854716447827892742892847 2.75, 0.01508649993169120687148304316907823530562245671384562198496642377 2.78125, 0.01582588968139967774540404127356602424889718587066443338070777943 2.8125, 0.01658685129168607013664189445049508821342351291232188779963190503 2.84375, 0.01736938448208958232290677015860819883434938992738273074996052146 2.875, 0.01817346635356432560278666600922326394999614547257701138009549901 2.90625, 0.01899905137695676276303470962473653957555592726480864734263094753 2.9375, 0.01984607142025844054359732147391482899033105108527965084939496747 2.96875, 0.02071443581379931336764356794820332649664181496248973579033221307 3.0, 0.02160403145248381811425036964815180664554107303890507389809560323 3.03125, 0.02251472293411369777334054853535453809586584532843577697572399271 3.0625, 0.02344635273278830116093955163217202751564613335051297413889842634 3.09375, 0.02439874140632461154165005128452640151344988831988551573332571215 3.125, 0.02537168783659547448108122516221510597866257660566158621703617559 3.15625, 0.02636496950164529147534967142871687660840869767808039211655659778 3.1875, 0.02737834277840770027479721228949805321412595789380780844017899441 3.21875, 0.02841154327481934812726058047460278412111855112563654904028774352 3.25, 0.02946428619009764749027194541550840522516141731433460326231204012 3.28125, 0.0305362667019282473049416722636055997074327769886846906731447978 3.3125, 0.0316271603792897148086826754421058064268947012262927450921243301 3.34375, 0.03273662361962845787474179945177910226099412016386308115749790369 3.375, 0.03386429410908607815232916063004438730333256854652564911265340606 3.40625, 0.03500979130447398100892701893456794745527781233761984574704280419 3.4375, 0.03617271693568602824325497631333169377714774788736506229706814867 3.46875, 0.03735265552723915174076345131365479077498865205390586012004063494 3.5, 0.0385491749376339984107543132626683067799022347917165709405002811 3.53125, 0.03976182691523269682330659580076328176681727758724515297607438471 3.5625, 0.04099014766935857257711932861386733944849379516318997844981092399 3.59375, 0.04223365845533294228947596778477178164524917113738420551861965282 3.625, 0.0434918661721768363942144453834153619544338821444827173815703976 3.65625, 0.04476426397172049167422174537424481691992009604312822522645844995 3.6875, 0.04605033187788057079514449446823281120969781843685818660630871444 3.71875, 0.04734953741488416563070716226735886099927679781358496638763946445 3.75, 0.04866133624323958415120616963425026869115182528629127706003183407 3.78125, 0.04998517280227657028163730582273526748581378077830505051177577415 3.8125, 0.0513204809581028287495326887502017109464173013083324046392687479 3.84375, 0.05266668465584939217075607831422049939813120754014028526120968725 3.875, 0.05402319857510434856974965470535705501168346635480467409440084421 3.90625, 0.05538942878746262091130139453378681059037256073190925036112480635 3.9375, 0.05676477341514873646928419801965263082988936293491831430247939603 3.96875, 0.05814862328969972722981805541593755397293634262825880843719082401 4.0, 0.05954036260972635117703890090094930801586653071352066363196928793 4.03125, 0.06093936959680261035549406117599890484904569625702657245296234425 4.0625, 0.06234501714856596116781214631412490050580485016693249935088918277 4.09375, 0.06375667348814356561195218224285057066633730012556390402742636425 4.125, 0.06517370280905332330724997455784717590262358654221805399868175592 4.15625, 0.06659546591476216148150555376787106462069330547378522413784838979 4.1875, 0.06802132085211805592477015967223958758644301349585034800690918457 4.21875, 0.06945062353790642662684440419753357577199402593277750575404276652 4.25, 0.07088272837781581777940559642458140843576777512559873434308981732 4.28125, 0.07231698887713205738438341877164543228777025103507015837732632201 4.3125, 0.07375275824251432513569787521586491691151237778126733703607657162 4.34375, 0.07518938997424067066997327078661445903803261612109088369396453851 4.375, 0.0766262384483444536612751566628250539571619803819784336465131277 4.40625, 0.07806265948809686225616571448352142896045167736124980894777706413 4.4375, 0.07949801092432405036989371817526847404394681961180234758493035789 4.46875, 0.08093165314408046434698025491317639109438184086971506603980656983 4.5, 0.08236294962723255589588174426033268149322823468937809021841585207 4.53125, 0.08379126747053925492959285537206351821293139103043588316148600994 4.5625, 0.0852159778988472602105005525501025152076668403129535430106351986 4.59375, 0.08663645676305035798187150993525848384757303518342628870079805348 4.625, 0.08805208502449256269397592333102482736096557696128307206648417548 4.65625, 0.08946224922552485617827439742799219986585171942917761572187051528 4.6875, 0.09086634194595465182308431369561868448112037040776697840123254071 4.71875, 0.09226376224515580095096348015018819907559769737694950182181754539 4.75, 0.09365391608963496494162107400576623009430565520931717166739246732 4.78125, 0.09503621676587747659169845752359584281680516169081528073624179718 4.8125, 0.09641008527832238821818124105363094780099335410670540021544740255 4.84375, 0.09777495073234223501564719038183047814136165165807739462027253859 4.875, 0.09913025070212811544519854032526519542384606197273808647685937194 4.90625, 0.1004754315834049934973811289131752183534112123725429229641609383 4.9375, 0.1018099489309256502228818954425957632621945326212750557111640091 4.96875, 0.1031332677807144457133355703081362577930580989384771000426191426 5.0, 0.1044448629570539914533117625116013183415424266393087399290912012 5.03125, 0.1057442193642289722344686992854558077209919706320235496331059093 5.0625, 0.1070308322630616939789231521325571225377218638593014006051773641 5.09375, 0.1083042075322934678978748890618356098075454737653697832877747166 5.125, 0.1095638619148846730415708452010092122218159178636244401609704622 5.15625, 0.11080932324932427060869390499518337812379143805399756508134816 5.1875, 0.1120401306860566779242284835641588926476230135233433082789808429 5.21875, 0.1132558348891502526433159561986837038202362904505028105599282475 5.25, 0.1144559982233471946221012098210069735777828819860310642828025583 5.28125, 0.1156401949266494513112917340367408750752988921622226880060894329 5.3125, 0.1168080112686092208608583671209845113222785912035053901148028059 5.34375, 0.1179590456945058947764481601401819871311521585589971611397123605 5.375, 0.119092908955603779282222741150236477261883411284175404353402077 5.40625, 0.1202092242256966927334178844153085573514058949965212287420010541 5.4375, 0.1213076272041565674983314003251882857315116806721967216574455497 5.46875, 0.1223877662057135014414543194086591883923459191138324855943235866 5.5, 0.123449302237204319904982861609177674270998513391493059497302087 5.53125, 0.124491909061535637931172833800329281148573263156319564801985392 5.5625, 0.1255152732491156689678340106050238455122186800150403005031265053 5.59375, 0.1265190942170166255201204570603403161222090317019434585949642464 5.625, 0.1275030842561365146566473183429906617263289434866826140615009384 5.65625, 0.1284669685466354628337966316303124676539919566831350979660769231 5.6875, 0.1294104851619274263893736728209290722746262089330116007664623643 5.71875, 0.1303333850615132727814651392963725454390575856155164643230408293 5.75, 0.1312354320729457699558080651790099994287252963326560416600219901 5.78125, 0.1321164028632210140562284957165015938635872961781230476494902488 5.8125, 0.1329760868998942761427358681255303031533768894107907033673493595 5.84375, 0.1338142864022211738599941231956665664392067918471793086872205838 5.875, 0.1346308162826274913912323145199820626961085246969484875204514626 5.90625, 0.1354255040788128978664788629988871137621790271187092188620825459 5.9375, 0.1361981898767952680040999349313570544185901185545536721157200405 5.96875, 0.1369487262252033064615517180095583448026294966988328946936170835 6.0, 0.1376769780411257364114230138716457823168170375507996908259441448 6.03125, 0.138382822507825450416376160068304248228501173324480029480558686 6.0625, 0.1390661489646267548169608503785732352186265865825340475883908212 6.09375, 0.1397268587892831845015994906457536955329344475160255175445903958 6.125, 0.1403648652731323398740486589025979890250273192370255263817456412 6.15625, 0.1409800934893428186681915386010695494610438532404778345686971643 6.1875, 0.1415724801545565983831828171146396903705638264167598923421856314 6.21875, 0.1421419734842281867075860124831022559109813980089447391734850277 6.25, 0.1426885330419595133197975755662326289714423377276454573230023868 6.28125, 0.1432121295831269025954391350983162399002229342982214982081044993 6.3125, 0.1437127448930935584590201984451709304348900821742252477609573982 6.34375, 0.1441903716202978250493031579056545494229580119112807716107220885 6.375, 0.1446450131045040748996839900724459883739218317588996387449461837 6.40625, 0.1450766832004994345421199119033079173544455765785646143703747838 6.4375, 0.145485406097515700093114708550521876298418955493965601495132085 6.46875, 0.14587121613465173642385907904393746093918338610979206872317556 6.5, 0.1462341576125674065806946230900897092043376385366968881939080161 6.53125, 0.1465742846017156565031322547877361012902903135443053937492208395 6.5625, 0.1468916607473747967453805148045540497396349459464931470780276994 6.59375, 0.1471863590717382904639950861024962362451493576504700626208044308 6.625, 0.1474584617733144876649961919877695377047492374178675194959150546 6.65625, 0.1477080600238837515377348465863239340424550727498295311177137235 6.6875, 0.1479352537632553152198704352776893278102619873636467140388692638 6.71875, 0.148140151492060997767412041589440808547358263735547183872304064 6.75, 0.1483228700628176073239189145624976235806858233018272735590864229 6.78125, 0.1484835344694844780202979471730167323155289577080655893917938459 6.8125, 0.1486222776357371351669553843157594469152004032054422386052887636 6.84375, 0.148739240202172570649367852468491705095786840132838971537801598 6.875, 0.1488345703126560465843226478562928871791001274057613694767997753 6.90625, 0.1489084234000137393690518578807648203193136820993856528084466127 6.9375, 0.1489609619712698970478062313573609692680552348875929746540607884 6.96875, 0.1489923553926215188783437434144551925928659313878014139088298932 7.0, 0.149002779674337885215779107214816238502787801289339303219690668 7.03125, 0.1489924172557665761217116542917476847890320594343698665911024603 7.0625, 0.1489614567906219259022013329948125681195329146338475387698971374 7.09375, 0.1489100929327261752044255712689372649737226103235836952443254791 7.125, 0.1488385261223679091647230072085536719234070754335524389525507906 7.15625, 0.1487469623734367158918770954187551949234962123992116500369156451 7.1875, 0.1486356130614873704715821180286845308490094680180548580712654737 7.21875, 0.1485046947128812515703058051285177373693468397353164602025004183 7.25, 0.1483544287951471361807945196927776511373460872768112527238491074 7.28125, 0.1481850415086979983771228115224368376369972232450912535356140255 7.3125, 0.1479967635800349651387270086508387038713913801890123881357855521 7.34375, 0.1477898300565641610851824199312335936900045779557286736948341009 7.375, 0.1475644801031468087884978526890452018431951786488194929414698882 7.40625, 0.1473209568004976463828079900641466295155327272091445079769356799 7.4375, 0.1470595069455414833979899022379948088023206256454343443992575633 7.46875, 0.146780380853832542776041079828230704844287860276009547261497493 7.5, 0.1464838321641361353125283611982910146093693210416171836329441538 7.53125, 0.1461701176452671854856447834288420779539292600497307014723747997 7.5625, 0.1458394970052751777448472251341277028645311017806701719746153109 7.59375, 0.1454922327030602225557755090211053050835003144716994852548830216 7.625, 0.145128589762500154344665092874937928707880206302958498304275093 7.65625, 0.1447488355891638712474285169713012428328522052559893006489699132 7.6875, 0.1443532397896815113335887532648216158554040736289726260182412444 7.71875, 0.1439420739938375336315394030474361310721136916517835376258171026 7.75, 0.1435156116794483365247482473914624220853475043803135552912932387 7.78125, 0.1430741280000817024280390965913763811463923865449259448835264875 7.8125, 0.1426178996156711074190491045057361016985004131051277216008398646 7.84375, 0.1421472045260737788494867261669621764637104193597704977009854287 7.875, 0.1416623219076173238845315002077622600479864286350828462179901495 7.90625, 0.1411635319526757882471114310613141048459221763905731779423087318 7.9375, 0.1406511157123121378535112696525941721793372533159443421959106352 7.96875, 0.1401253549420203870468004399490992933146008813685132060592053881 8.0, 0.1395865319505969261523717952201556048352961978634593626199956585 8.03125, 0.139034929452167028347360234482825094005454984475627880786668059 8.0625, 0.1384708304213890414751342249781640887207232135165388237754694186 8.09375, 0.1378945179518553944458534404893760209784770346777328266144149511 8.125, 0.137306275117706270124538649187209643845913007218244688257597012 8.15625, 0.1367063848384686168869038404421724142270253704739837253924750777 8.1875, 0.1360951297471300889809349018549205314364818786088753217098418186 8.21875, 0.135472792061454521027667211296520105380747996875093793063692204 8.25, 0.1348396534585426538900662492732683711213139773872726989025896501 8.28125, 0.134195994952639037105149286593053531166254001095648895689653526 8.3125, 0.1335420967761833363958288708101099953754046019027703454362920477 8.34375, 0.1328782382641016726581205859315136572403567504522437174534859629 8.375, 0.1322046977413311103821640892691300539233545731966601342404412936 8.40625, 0.1315217524135679977655052705057277764114337938180105910833817906 8.4375, 0.1308296782612285368000290847251082436367781289122245017714297755 8.46875, 0.1301287499366077282821264900177115144151343175418109771979675257 8.5, 0.1294192406642206928722233945489220067994836128911243537546777605 8.53125, 0.1287014221443083138226873992471819105675528528146455480181817349 8.5625, 0.1279755644594871785592272518338310880134218185679180109016567457 8.59375, 0.1272419359845219136498098214540689001581395616946718641068512452 8.625, 0.1265008032991962094929046934308475348369764359761717341024840736 8.65625, 0.1257524311042571159296681061600648840395330108327270795847968728 8.6875, 0.1249970821404055565222031584940638266537662789121884832976175432 8.71875, 0.1242350171103044559989256291165412529883861907556370383859125794 8.75, 0.1234664946035744008751557954021362056389488616175423534537103861 8.78125, 0.1226917710247453560124998000697102215182484981855384927062460262 8.8125, 0.1219111005241316383608735487772338622411062091258409940338695099 8.84375, 0.1211247349315961017878972967393393036253007106474480390906175052 8.875, 0.1203329236931683121796271338859893738816117590208196934874368039 8.90625, 0.1195359138104803883166872127362114055010679248721249537560162221 8.9375, 0.1187339497829831498006133671114615046083163829816594408728707471 8.96875, 0.1179272735529042469261638620216958057123505548330122740722752645 9.0, 0.1171161244529090472581670367111784045743053095436893911035315581 9.03125, 0.116300739156424218162210083385227185533605044339847852124075079 9.0625, 0.1154813516305831720397193881634775620922493157015178549475570141 9.09375, 0.1146581930917518299109321176590607018060567983412020806855422268 9.125, 0.1138314919635925076556667041706848420608352271803379539939177428 9.15625, 0.1130014738376231360458565376957212990823141589064789080116173296 9.1875, 0.1121683614362284890739190007137677652436395694523246660647205095 9.21875, 0.1113323745780796133914945012641619056077986973209133253888752737 9.25, 0.1104937301459172233257130101992574268888571646423888723107456074 9.28125, 0.1096526420566544493457524437613924141828713725194505127350597242 9.3125, 0.1088093212337540014323186697259645238954995013351097931174975041 9.34375, 0.1079639755818345309898787779655610084100681487363171436870377787 9.375, 0.1071168099634607441821723564042090037193203245757610115628138377 9.40625, 0.1062680261780716343261392051513118096427559753933825176508211987 9.4375, 0.1054178229430010597237636532811313712339610267446262576771971889 9.46875, 0.1045663958765447945377227189071435034687044837875471218789131249 9.5, 0.1037139374830281225348471064138184894358405720209232547560841715 9.53125, 0.1028606371398280252593165859252927006739301365365426685611875904 9.5625, 0.1020066810863040360024670690075651775314553757606669819658493808 9.59375, 0.1011522524145918873753207979934320576658078337719762942203305787 9.625, 0.1002975310622141719514067187849198747123084349474381459687816731 9.65625, 0.09944269380646236094043311244551347186132933931932763395238961378 9.6875, 0.09858791426050468380921095557578551296102506855325932845067711401 9.71875, 0.09773336287117456083874535795601281302988611106025759620624639309 9.75, 0.09687920691839449947249595123014577992202735721004505877045684474 9.78125, 0.09602561051619061267082966905759933316281161950185383386654675451 9.8125, 0.09517273461525319206491984563765188703915505596357131601874553098 9.84375, 0.09432073700699906924830257170489700348610541649798297846314796572 9.875, 0.0934697723290918238290632200512601609790448068908622816982339473 9.90625, 0.09261999207237624568809804174997119044476874229376716083851429583 9.9375, 0.09177154458918383007203469068826461831207499880459753640537030884 9.96875, 0.09092457510296647654139582190468269259280455276833745760153763756 10.0, 0.0900792257192159752690307848423623219006311287035019231330779874 10.03125, 0.08923563543762729563979716734600204632935976805884550301958272071 10.0625, 0.08839394016546414146459423696291650602396215891002094817810032835 10.09375, 0.0875542727320857033403975981446079045900950004170380141217798983 10.125, 0.08671676290459402073880955226978882040738016370274968214772002705 10.15625, 0.08588153740456186329032286367323554273714780196679598107134503884 10.1875, 0.08504871992580155147705950812258055922273494943380786172530972376 10.21875, 0.08421843115313566060574172054166647482048972395137041357727298345 10.25, 0.08339078878213108758302055413775607723132212504030552984467919505 10.28125, 0.08256590753975850676023276040171004069036098612758131236530350499 10.3125, 0.08174389920593979808250412322343169032284488043129175755402777055 10.34375, 0.08092487263594659712114048278923636920033358297768039227359054973 10.375, 0.08010893378361369146648178382789854656403551892204511091331458431 10.40625, 0.07929618572533157061342790577400283781264959560021938264500284631 10.4375, 0.07848672868478302611059314823253687943738975624995612483168087127 10.46875, 0.07768066005838929461750852576414987660165990817465531077261871691 10.5, 0.07687807444143183789729234703220624279380857338216579354887518411 10.53125, 0.07607906365481645996312680310428816692364016842017608293932628877 10.5625, 0.07528371677244707191735332262054653143448767903402410303253626836 10.59375, 0.07449212014917702881663938547103469773019925803122605043397312676 10.625, 0.07370435744930657953273530927323538552011916583148759884703871688 10.65625, 0.07292050967559558944542393848831886213771777720290243347547703543 10.6875, 0.07214065519876131631396157921297464812803513601942652338028874115 10.71875, 0.07136486978743164125886602479447493314344682213780748878337445234 10.75, 0.0705932266385247789018877058650651506121612742786201604507144401 10.78125, 0.06982579640802711283391643020037884671294585057571371909558535625 10.8125, 0.06906264724214142420453222565923372837428208273649133725449241974 10.84375, 0.06830384480877840186922864867183951077450874338278457748201476589 10.875, 0.06754945232936494172719141545968222766494875266081490789943738083 10.90625, 0.06679953061094336018954323087129961356563802422404758002923660697 10.9375, 0.06605413807853626170989169439229443426048007046861832278482418048 10.96875, 0.0653133308077524125792655593271689657287952723742815949379576175 11.0, 0.06457716255760958234783150545220401784168671597255123666736160942 11.03125, 0.06384568480355091991580646014974095445569419617921151651521111317 11.0625, 0.06311894677063203318290649937023367467323984603485566382390229352 11.09375, 0.06239699546685653882382648399621959603344520302605595275077738711 11.125, 0.06167987571663844194769248357791483216337325079191816121940298872 11.15625, 0.0609676301943702937995897381767567188529607835698861025890621261 11.1875, 0.06026029945807665898551800421785156257701253772225051353875611987 11.21875, 0.05955792198313300167740480983988375815312060358229032027879208004 11.25, 0.05886053419603067262623147394501590853935283971236069193541303953 11.28125, 0.05816817050816924533779857635785778716951948332299664882435651658 11.3125, 0.0574808633496580102204774535212360255396781394482704077840783975 11.34375, 0.05679864320310898968477670869263493212582523302622073950513070364 11.375, 0.05612153863740438486164394161535846674235555525922766863438587243 11.40625, 0.05544957634142190562432181498870901097330582514315999998588280346 11.4375, 0.0547827811577019697743597460142397745624021376810795707610064366 11.46875, 0.05412117611604128442562770613406518902698666372521448724479724358 11.5, 0.05346478246699784264260207004149530786172344904175906177569486209 11.53125, 0.0528136197152928811242811251868868144838942789008529933179484138 11.5625, 0.0521677056530958500477358085992101630263070350051249310071949207 11.59375, 0.05152705639317894398145946756000800638761012963773573842208917519 11.625, 0.05089168640192823294500119979566293503896678428072390811724372124 11.65625, 0.05026160853219891513485821963960316251879364044374241530953591884 11.6875, 0.04963683405600268747428463283275829189557781257028954713409880922 11.71875, 0.04901737269701569690324420033732735052135020653013622061296168467 11.75, 0.04840323266289599414023479513763303971326556773436670509411022052 11.78125, 0.04779442067739986246520183645872552865184363236066140246343015826 11.8125, 0.04719094201228683684599321613546618243415908251789683357860193099 11.84375, 0.04659280051900366342192623347299862353730256873652458178482910522 11.875, 0.04599999866013787593724569815644791373264054355998011366547189617 11.90625, 0.04541253754063208416252571154945252607614072917162635227679054418 11.9375, 0.04483041693875047963884709251768446716786316051519956067197537987 11.96875, 0.0442536353367894662204835816113971739140188055683013145126281588 12.0, 0.04368218995152471687635531739281537760189964883435067898333321298 12.03125, 0.04311607676438734404476547014127263252420545279051499674106103174 12.0625, 0.04255529055136224853236287308747029578868171152232971661549374741 12.09375, 0.04199982491260208152536743583929108018337480599025049091911324587 12.125, 0.04144967230175061576314945964008104380603768593021504686644246521 12.15625, 0.04090482405496967534110003947385222630048685740611084691528093907 12.1875, 0.04036527041966411899648465848096108337191779473078386067932884597 12.21875, 0.03983100058289970912779248348159746755531236781828933676775736839 12.25, 0.03930200269950902824993546378841277650347059020112867294596109835 12.28125, 0.03877826391988092614403431893049613289348872850639081869119198565 12.3125, 0.03825977041742929467530879194795290394691816004020008233248898725 12.34375, 0.037746507415737273183735783321366111817250504251481785415924853 12.375, 0.03723845921537328556151527703663072492151770914496108335033055671 12.40625, 0.03673560922037560068453835831193080739781669600184458029517656766 12.4375, 0.03623793996440239083101016903469320832974396818984132540245539339 12.46875, 0.03574543313654453817144717220144296839484240442264261014927766468 12.5, 0.03525806960679870742582945100797950445978870357757378206026414402 12.53125, 0.03477582945119846343402560974339524254654627649794884156156752345 12.5625, 0.03429869197660146575571085671751662921789853475712021504970444148 12.59375, 0.03382663574513101858939034234531708206459333298170791035266192892 12.625, 0.03335963859827049336270064119476638054383059942821784619709857119 12.65625, 0.03289767768060937338596393941607229039254919472314347121675936924 12.6875, 0.03244072946323989506811157554215359079173782010148374705451200468 12.71875, 0.03198876976680347846054609764789596236946716979852879152661544347 12.75, 0.03154177378418635141396187808316080638816052416332403260612468714 12.78125, 0.03109971610286397650085183859782612701298367634277324233588494615 12.8125, 0.03066257072689408816907787835047037918281954299789447234854200507 12.84375, 0.03023031109855833944745076832463048477533027565722711387022125042 12.875, 0.02980291011965274302188368817860432167519898954793369660785951768 12.90625, 0.02938034017242727074051228305981922526620887609501706167324990158 12.9375, 0.0289625731401751486892770735511776997583656427816909538412965764 12.96875, 0.02854958042747255200768959049621401227436162410810673893580897305 13.0, 0.0281413329800695646903692480982303363740925865703202581428511404 13.03125, 0.02773780130443342484651954905806200515303180417407344732301837546 13.0625, 0.02733895548694522537033722115277473581082515156055185600435491043 13.09375, 0.02694476521275138381429291375886109283666785114653183209362745783 13.125, 0.02655519978427133355841434536617153303457230349539182751402792695 13.15625, 0.02617022813936302123642556425539578180496820430430674621927914252 13.1875, 0.0257898188691479229181979250028229322606936071091667873311313849 13.21875, 0.02541394023549741386177664122660085378198896071964208894555595496 13.25, 0.02504256018818244384148378156398580956217209968617961472205210035 13.28125, 0.02467564638168858223530245245993062248488381663980076855582391007 13.3125, 0.02431316619169860431869539877810432437095491962896324428678848523 13.34375, 0.02395508673124489266664908109195037520870787465704923391069010152 13.375, 0.02360137486653402531410389886825693949918841596391167080373280115 13.40625, 0.02325199723244601546960677072424970354343687800780519979046093388 13.4375, 0.022906920247710756220046794561517314143025194214171416540692479 13.46875, 0.02256611012976430790716040534545965524698726022796661743168714562 13.5, 0.02222953290928774579992402554684276313564496766991144538849579827 13.53125, 0.02189715444443136143109309834353160324362993197658135926055940951 13.5625, 0.02156894043472708261034789158384430414024682748437226080282659365 13.59375, 0.02124485643469204476931959053144251339244174216719785762448238992 13.625, 0.02092486786712631003290033154832020515553225473976608821202936454 13.65625, 0.02060894003610779034350490623347431081786222321080332201080929914 13.6875, 0.02029703813968748718623816615417446383595538403698234152178129037 13.71875, 0.01998912728228821306815255087011842834710346711927050433149472271 13.75, 0.01968517248681000898787475094235064619344644494234684846226341983 13.78125, 0.0193851387064455177857246164964714859515789915629372555868373515 13.8125, 0.01908899083620861558086278002192362548089270044569431157095041703 13.84375, 0.01879669372417964257171167417742443238820891989332419590640361778 13.875, 0.01850821218247061038850265365148440799609697622212826075115095109 13.90625, 0.0182235109979137960307696701030660230211842790751616318185123049 13.9375, 0.01794255494247716228522977158061526911920574278350595874404072218 13.96875, 0.01766530878341007148686690000396960883946714985938783415648925456 14.0, 0.01739173729312278364306556231780631887969713759681244529381747078 14.03125, 0.01712180525880325137099877164771956192612344977062530661525080283 14.0625, 0.01685547749177474288459510565731319236025538991134036603996327899 14.09375, 0.01659271883659784049047526404455923484951974405871725046897261092 14.125, 0.01633349417992037579217716703127645247497261591390030491046018882 14.15625, 0.01607776845907887413742363245666721232464496711639717041597930042 14.1875, 0.01582550667045508985148744112449031202415508488877960933765199321 14.21875, 0.01557667387759122055694330099531444295444671912127151222754245053 14.25, 0.01533123521906739346103556447436122212875936556117825461179641207 14.28125, 0.01508915591614501897000297588447297878304086218143531280370179737 14.3125, 0.01485040128017960743714980831930667048525584103960935121913685029 14.34375, 0.01461493671980664333909084294585519649253217004273332018969980859 14.375, 0.01438272774790410777197066051002478221410586882600513543519777407 14.40625, 0.01415373998833523493480101806403369245715465723048837567240372514 14.4375, 0.0139279391824750812873004444592995936620103926189386089092036279 14.46875, 0.01370529119552447740037775082639886211364653274120826497060294389 14.5, 0.01348576202261492222299237093930202604133937188144469769807527935 14.53125, 0.01326931779470796763256594148836411112481413254753350314005290282 14.5625, 0.01305592478429262777913282815506138616404709313490776842976525673 14.59375, 0.01284554941088433293643440271100543855474440761669773381683868324 14.625, 0.01263815824632893139533163120468065884320815875783454305301287547 14.65625, 0.01243371801991522543410577230112460787092230542087111828432708092 14.6875, 0.01223219562329950863304235806129712906132887969621611228131906817 14.71875, 0.01203355811524555182249413297974228306924993254645822617743630436 14.75, 0.01183777272618346381848877085024065492976172115231605927589757706 14.78125, 0.01164480686259083086074077209705888018365671961778171405663285354 14.8125, 0.01145462811119951537626740330526992335794839429486266465411653182 14.84375, 0.01126720424303147039809983811802754868355663620864325356514383517 14.875, 0.01108250321726690072201867742453314475391217786068690100065815408 14.90625, 0.01090049318494807573282744118392403432849342166969474602418733066 14.9375, 0.01072114249252207182222421584360634264459803681289539641993976189 14.96875, 0.01054441968522569449848503026145836819610630416861577850817711493 15.0, 0.01037029351031580169841929658784268727219718885866488076505310045 15.03125, 0.010198732920148220497739802996526183473715500459371949628848851 15.0625, 0.01002970707510841941931854043822693556182834154799512699331026333 15.09375, 0.009863185346397067900869706918206110974273617042611023887557678326 15.125, 0.009699137318673583244404944129667268049096308158487118089171429876 15.15625, 0.00953753279256073356824802680610390499679334346380015378579852359 15.1875, 0.009378341787013332956308961515963939692755113826302515778764801244 15.21875, 0.009221534541554032185475292400412087148880984348868430777673792339 15.25, 0.00906708151837917514611360240403184806964588038864559326980478359 15.28125, 0.00891495340433765738749216329951545330068315973069517857297047596 15.3125, 0.008765121112785689153130642589552965142266130101551143640750628186 15.34375, 0.008617555785320330853353475513016838084655976108966557845253003893 15.375, 0.008472228793394634185389108618455863184430565329575241616138584996 15.40625, 0.008329111739817187085973320756207798483752415390233786350140594164 15.4375, 0.008188176460138825417389111683102168848232690928711474747895925408 15.46875, 0.008049395023929238774084573847594664941740698110351318914020875349 15.5, 0.007912739735946162081414694650635719822186900082414991230048984677 15.53125, 0.007778183137199808767714916509292135227341917077002320745436406903 15.5625, 0.007645698005915165252012212376521919074393611856841901395217601611 15.59375, 0.007515257358394730327525262704288231218462894546776649893548353995 15.625, 0.007386834449784246760160214419029787841342898081478173284630426354 15.65625, 0.007260402774743936085099103450784588726104248508786197067695648134 15.6875, 0.007135936068027711196112557729165154311724058993624128829251831655 15.71875, 0.007013408304972804903412752347775659601097175963379834073376279649 15.75, 0.006892793701902216207916316276142267970294995086846189346747353234 15.78125, 0.006774066716442339623159155910639778837428212133887633939171694213 15.8125, 0.006657202047758106490490607687830658108619514888437648195115582276 15.84375, 0.00654217463670793089752885855173170216247578706774598360181060695 15.875, 0.006428959665920716542416016107120865072700092313728051308217682283 15.90625, 0.006317532559797144704694922880922846593691217301236176194339329021 15.9375, 0.006207868984437427404474126321332152636348041416332165341685831046 15.96875, 0.006099944847497673871109001072127785931656801454320380313923265594 16.0, 0.005993736297976982616401083030255336132009925739194946945004034001 16.03125, 0.005889219725937335730153059269641196029126381998618573511386072621 16.0625, 0.005786371762158336502030987137848678837899784863378730942774399818 16.09375, 0.005685169277728796136678993186107051215546208807632687170722840925 16.125, 0.005585589383577140181903762102105242816264542980353392933224589745 16.15625, 0.005487609429942570344907926253035995272048304157877838824755031009 16.1875, 0.005391207005788882640841224926362481264119279087229071962943159304 16.21875, 0.005296359938162808312635262923185976329086733106557193482781387624 16.25, 0.005203046291498709691926131346029189874671384728454645996773921921 16.28125, 0.005111244366871429148052183032797754709076860797990415992349803486 16.3125, 0.005020932701199055505327508791431121714756779829305233326316614487 16.34375, 0.00493209006639733880721674120172691547609547161003624402579166358 16.375, 0.004844695468487451078364587361487328622263887898352861520547953491 16.40625, 0.004758728146658757789877196334106300435349644918475273826030174679 16.4375, 0.004674167572288232077560623045800659531506726288032460968923469296 16.46875, 0.004590993447918111404290794222749883906959691162919473078572949795 16.5, 0.004509185706193364303176601752709417049883966371357601625110752756 16.53125, 0.004428724508760503094113029599611806182602121750429661621549607512 16.5625, 0.004349590245129247038719595253812993475988279247440916518595722085 16.59375, 0.004271763531498509293132893365522644635752064506482206427160481127 16.625, 0.004195225209548150239891470497448172751804733123389589438645080414 16.65625, 0.004119956345197909334057767822635998020306230017300115626447364483 16.6875, 0.004045938227334897489238271236472876227360545527968910814926922811 16.71875, 0.003973152366511002260404999696277948826579645222266389863833250566 16.75, 0.003901580493611528656158574463971599104815789563734600543521014474 16.78125, 0.003831204558496369336739488508944586617457428700047330480075122825 16.8125, 0.003762006728614969228799170342472001742836049964835703924214608361 16.84375, 0.003693969387596321216480913269121976108222751984599390269114771514 16.875, 0.003627075133815201553223375693850552777481022240114496672638023673 16.90625, 0.003561306778935825982082547313163040502873558805233171611581552729 16.9375, 0.003496647346434080256183748780064138261472914222345448672623707917 16.96875, 0.003433080070099451816800447862074655696814613791052375903256265177 17.0, 0.003370588392517762815883026502544299200711223278838596233661236072 17.03125, 0.003309155963535778463743459765015690477610624461354912723741741669 17.0625, 0.003248766638708738841909275035594403568348544987038432709016552744 17.09375, 0.003189404477731836846521850727458194548832750778031905811135635887 17.125, 0.003131053742856639819470029742495838440568741485110239625039759577 17.15625, 0.003073698897293427682898799720659802554774508209785257653821424662 17.1875, 0.00301732460360039601778002863501709970892081697493949294727469142 17.21875, 0.002961915722060648518638588436002423939845982854018412061118718088 17.25, 0.002907457309047879613856251287310954342333581240549869330320520833 17.28125, 0.002853934615381624763601777673942980578528436192202620278165163658 17.3125, 0.002801333084672933034551127991356077014955167331024341529342475831 17.34375, 0.00274963835166129400118480101103245650359748319151202552069883895 17.375, 0.00269883624054362883643079589052827609921621261882340293980617437 17.40625, 0.002648912763296133628452271633075552086256395892735079991071928111 17.4375, 0.00259985411798974149399595168005853570526578754528481389879404129 17.46875, 0.002551646687099948950311335428308671715346356062762801910917175961 17.5, 0.002504277035811731255472322622425798620304510274846659292768413449 17.53125, 0.002457731910320251029098616931371664264660461168277586707009092537 17.5625, 0.002411998236128044419973322658666797106387634180372466853393749851 17.59375, 0.002367063116339349391752968200694377016249022152332026138467689678 17.625, 0.002322913829952221350618551206223002110068301200773738042720745057 17.65625, 0.002279537830149062336962873993643696400660441727157891984208221459 17.6875, 0.002236922742586171344587758334732264020308665538599993344795554231 17.71875, 0.002195056363682905012833007583865772337973540439485874023434187795 17.75, 0.002153926658911019956921773753242427137064590991780903822555155188 17.78125, 0.002113521761084750356840199533440992852494559849865732524690736381 17.8125, 0.002073829968652157112445087095317052102526991228183778681198540127 17.84375, 0.002034839743988267889305312015259509442031124133345099963342087203 17.875, 0.001996539711690510723050030854091612433141622831860723866376363451 17.90625, 0.001958918656876927516669126455616873802820625454928709588402192843 17.9375, 0.001921965523487637752173345149656588980945266655365679518597318731 17.96875, 0.001885669412590007042096894903779101680777942790481320624198471091 18.0, 0.001850019580687959764280888314626225715717956781557941459933869794 18.03125, 0.001815005438035859951926266863330096205004241000243064507348592252 18.0625, 0.001780616546957369846715326163650975361969020942281485580492146856 18.09375, 0.001746842620169681062492315338627707906184475639819233420519317417 18.125, 0.001713673519113499147145163906545967961403546430926717227158704095 18.15625, 0.00168109925228914846748382456434038335524266533443174418470995592 18.1875, 0.001649109973599150772573347893835558128586080613962869643083297856 18.21875, 0.001617695980697617511627999133501353014747553846858102199254128342 18.25, 0.001586847713346782989655113216873662880898123797406295921707869334 18.28125, 0.001556555751780992733977946858015464333956775854998526194350858827 18.3125, 0.001526810815078449013967870918847289188964839172250316322012258774 18.34375, 0.001497603759541003301161456811652166937831728839057666724189517362 18.375, 0.001468925577082273573795006991890770028544407830553966354349941436 18.40625, 0.001440767393624352755012274478739466170497612999297531620409026272 18.4375, 0.001413120467503363223934276269767293113136358468108810817784296206 18.46875, 0.001385976187884101249758856434562682727559537826353146843639884549 18.5, 0.001359326073184004367411895171036611365668741258164217336056419052 18.53125, 0.001333161769506664135328284090105479639842938485484143111675116657 18.5625, 0.001307475049085096388024318929460399757971594856167874223760831657 18.59375, 0.001282257808734971014560299749463811896466011088933693844395835749 18.625, 0.001257502068317993455112217543226695472689237716653104476908311135 18.65625, 0.001233199969215620508008809754962431808998232639308972257359451638 18.6875, 0.001209343772813283675086273803933166733856816405670035063723712956 18.71875, 0.001185925858995284140417572298516069609655627163677469628616728029 18.75, 0.001162938724650514572747074283129915510387674842729795156293559941 18.78125, 0.00114037498218915426167694697890068462581205760739453222502455343 18.8125, 0.001118227358070475638195700397967723476489800506226821719490756969 18.84375, 0.001096488691341891987913345233374607446248819177854991935550984684 18.875, 0.001075151932189368136790259021812978860085176453816293289900683301 18.90625, 0.001054210140499308070654720599013922403054573386384180043260775004 18.9375, 0.001033656484432025837853297561587348400283793163672063928254296187 18.96875, 0.001013484239006898675445725001357170756208692694834785884273831561 19.0, 0.0009936867846992940899404081413689659793979577326565040829668341012 19.03125, 0.0009742576060493556101901141872697483149061636701845924223023109669 19.0625, 0.0009551902902827251092758572114333191060591744423906900375053773939 19.09375, 0.000936478525943272960571689401978770002132318109883570156507658533 19.125, 0.0009181161015379008473015280169870517979996650037757186023400384769 19.15625, 0.0009000969041934757813957947267679558937640245682651625428133020165 19.1875, 0.0008824149183259478029830039988798147851599690208743741433578986254 19.21875, 0.0008650642243216979230907641786382571376197147742675519008990201795 19.25, 0.0008480389972311571357927441395902731047271676654814015402391419203 19.28125, 0.0008313335054747317588640660407804296152149821992043330960593961346 19.3125, 0.0008149421095610649607693022470235999432859057477736834305992581908 19.34375, 0.0007988592608176590933083380156289452744028823287011082703222275524 19.375, 0.0007830795001338783703215183683345013021983401714312557392995900712 19.40625, 0.0007675974567163465103751303933439837427839597517140168812951493325 19.4375, 0.0007524078468567491922129831859024408759705516959855127314560910429 19.46875, 0.0007375054727120465529049028799651900822281125970619888724416319326 19.5, 0.0007228852210970964870177165679061577572116710763298255144530350239 19.53125, 0.0007085420622896851777825937824498133159252306885743975881750969742 19.5625, 0.0006944710488479571051731201056818629052968980208585596871007492822 19.59375, 0.0006806673144402327281150220327322216650264434305204919957614992884 19.625, 0.000667126072687198125830315284092671791269370877757468671607112483 19.65625, 0.0006538426160164471037207773388457992418243678333847289836274414415 19.6875, 0.0006408123145293526193989373676807747981512693286412619129152044813 19.71875, 0.000628030614880240861696236955743803530956890843282728067871613038 19.75, 0.0006154930391678379169709282981111811239941098779864153982962546072 19.78125, 0.0006031951838389556800923474757281084711150368996857727660325087428 19.8125, 0.0005911327186043805094196641258952841669932526987784230176481463722 19.84375, 0.0005793013853669250832849167264029231186097465158157821031023092403 19.875, 0.0005676969971616009873316234904797261834287878658472296108923655925 19.90625, 0.0005563154371078667449877557047022803158579644911222804736788764456 19.9375, 0.0005451526573739032948383453696679272273620997343163617558924477559 19.96875, 0.0005342046781528663162181658445822197113949939550467898477302116523 20.0, 0.0005234675866510623055151594616268751686033796255489787156754907077 20.03125, 0.0005129375360879929080436206928059337555483132979126897190166079575 20.0625, 0.0005026107447082097115321846611711151666163617467718558417397676831 20.09375, 0.0004924834948049195049315156096264199926250853511902209314863920711 20.125, 0.0004825521317552778980727209089307171537323893103150175920475331532 20.15625, 0.0004728130630673071814286801275535003337780528432116134120788154404 20.1875, 0.0004632627574383723786115492811561827922269627529825433153673357133 20.21875, 0.0004538977438251476050815252527050272710743315490272200305453633532 20.25, 0.0004447146105250030926811884108766253172815885315280582408270234749 20.28125, 0.000435710004268741568918661043330348417984250806412656038030608365 20.3125, 0.0004268806293246110903091330931270274891479718253901884323025990031 20.34375, 0.0004182232466135199184909440008741825716700794782911614434326535002 20.375, 0.0004097346728353775942373007731893009651301200723566191134372097101 20.40625, 0.0004014117796064850059005581556213549942731819530544765597603109081 20.4375, 0.0003932514926078949633000148696791307391826063258100450088394950923 20.46875, 0.0003852507907446635736778928681021030569658248990766883708572621629 20.5, 0.0003774067053159115712170792662275906700342333916939355034628410085 20.53125, 0.000369716319195613673887578320724842817183112177268880779287264372 20.5625, 0.000362176766024033029249158977339776212838588163269564882568992823 20.59375, 0.0003547852294097168625012791139329883655638619869166763788067002189 20.625, 0.0003475389421419685537867830745965461571979799048327957867633916747 20.65625, 0.0003404351854137105458044388558844439794508414767225800901340241329 20.6875, 0.0003334712880546517154807017377228301347079306233265154863888633689 20.71875, 0.0003266446257746721331387042250621916388389543467319888551248457255 20.75, 0.000319952620417337477659543313458400670844463663390815293639440424 20.78125, 0.0003133927392234547749659266613169374601723043087624480576953359313 20.8125, 0.0003069624941045805782105739919509357964984646689326446005820957827 20.84375, 0.00030065944092639220979150000931790787881981345617549519498691471 20.875, 0.0002944811788018322362437663469830253922715370589688299639914093914 20.90625, 0.0002884253493939359457027585187108763335288425442217356549707343936 20.9375, 0.000282489636228251242557386733543537436404819377270103762353271613 20.96875, 0.0002766717640147600637019335939914153016007492191045174297072036423 21.0, 0.0002709694979792101540705690132423059691466472027722730038462955455 21.03125, 0.0002653806432037658145453431860720767237741399615532951453934048159 21.0625, 0.0002599030439768860515414411479595917757689245775230906176362924416 21.09375, 0.0002545345831523384132951344001453323146075016313181096478520496739 21.125, 0.0002492731815172566918401387850640471750425286461864149447736917883 21.15625, 0.0002441167971691506006140074280619626675880884422752313015333283424 21.1875, 0.000239063424901775504371494059434533352466595802922400916457199859 21.21875, 0.000234111095599770279406607489308502133779770264588500597768617791 21.25, 0.0002292578756419714168354195223365276381943999793871972965581951938 21.28125, 0.0002245018663133115487292818325698275029435195610559364970553045796 21.3125, 0.0002198412032252106750999000100110188866421817713529788969676355924 21.34375, 0.0002152740557443684980355386231836800139342224938604460862056911426 21.375, 0.0002107986264298664266078429852250025577803885378907693133932131265 21.40625, 0.0002064131504784880014718690278527835433172765721078608877298045818 21.4375, 0.0002021158951781667003522097085202251907533379806354288109283111954 21.46875, 0.0001979051593694703238533182170535478131900540144221312198778804315 21.5, 0.0001937792729150314242830419502544173692750302491638696214427979703 21.53125, 0.000189736596176833527488486081772531851705809300615907420027269357 21.5625, 0.000185775519501263208148453297827930443666541165106467760295994386 21.59375, 0.0001818944627118384116446728372323067800636343427441714904928947378 21.625, 0.0001780918746095237696642810462779493739987243521553820607584453208 21.65625, 0.0001743662324805440312092640674348561689731398244745199132870761252 21.6875, 0.0001707160416116071248664584399019208069228751937873434287785017145 21.71875, 0.0001671398348124487812064348693045742691753028871851855439944963413 21.75, 0.0001636361719456110752335937809465325844343405317596314541899867083 21.78125, 0.0001602036394633676971253842588000237383629077153214494082086658999 21.8125, 0.00015684084995170922431755922518948935412810915011288015431718109 21.84375, 0.0001535464416813021485758300164943904188367123878176220475832824296 21.875, 0.0001503190781653359073220677309000225238023966764424631341915724428 21.90625, 0.0001471574477241726784537205344569237958364908284642943441209240149 21.9375, 0.0001440602630567152215249655319895997789616919096514478305941190529 21.96875, 0.0001410262608184085847817376938381997305883569633304735341781380612 22.0, 0.0001380542012057920465121538493010630651744898446802968662906391646 22.03125, 0.0001351428675475182198581609553434769529726982135152205852196896735 22.0625, 0.0001322910659017568220195553267838284614807974891850474088381059511 22.09375, 0.0001294976246599011910704833169331281989862599555081067370453841167 22.125, 0.0001267613941564962258200396871132844581830721920756733437451268568 22.15625, 0.000124081246285307025717468232282320841436654584194649518806811742 22.1875, 0.0001214560741214481181792185651552438566082596388867492569041072517 22.21875, 0.000118884791549493779365535699055899413587652213390033777171238555 22.25, 0.0001163663328974905808392011820895901339119640180783317982474302161 22.28125, 0.0001138996525767939281940886425025039624879108451078358339232624733 22.3125, 0.0001114837247276509981563685485323636623017081133893849063404658862 22.34375, 0.0001091175428704531273606759278769554399528236306646202130433225262 22.375, 0.0001068001195625813585253868688862898153913850859154080368930748643 22.40625, 0.0001045304860607695076469651783470601265249921869649329008383771995 22.4375, 0.0001023076919889097786680749680551932375209593300060279162954087858 22.46875, 0.0001001308050112266194257730168155461304019973854575532857085751311 22.5, 0.00009799891051074518414531886282018932988443580110979543218851937232 22.53125, 0.00009591111127298144291518230050583958090797337305595407466765139615 22.5625, 0.00009386652717478165707512342291481054734710520447295688981171175146 22.59375, 0.00009186429487823962089916925133603486429886835987111995629086477861 22.625, 0.00008990356752962075399801920552506791490432212019864916882843725068 22.65625, 0.00008798351446322281515143755324887683492385624676539872526196693038 22.6875, 0.00008610332091010369647229533422453590878657399506323016282172852618 22.71875, 0.00008426218771160744657282390789995155768804525557211875640373061966 22.75, 0.00008245933103762036243376732419149813858897770871651127940011252377 22.78125, 0.00008069398210948968166237540435866692888723010977917791117141875305 22.8125, 0.00007896538692753809946970663721929402296146145644091046232557667111 22.84375, 0.00007727280600310802771565940218116879461020426475791374007535248997 22.875, 0.00007561551409507020648544977137068668406532831928429264235936658404 22.90625, 0.00007399279995073197160738616264832625144765338125115048973369362977 22.9375, 0.00007240396605108117404157003820822168350499427904121301403576290932 22.96875, 0.0000708483283603024389145074129962144914647615328317100674050695602 23.0, 0.00006932521607950314290637898975301268341054119934235426214238797386 23.03125, 0.00006783397140458717848540869385099518057667566354425092170572133459 23.0625, 0.00006637394928821526190539623271582815389986898119676309197775002697 23.09375, 0.00006494451720579122872432407271581860582348833818147354689967172914 23.125, 0.00006354505492541444565837786991228738094108945186296711416884094678 23.15625, 0.00006217495428173915065898013893028840165854841542417197492475884161 23.1875, 0.00006083361895368221400046802609493345010601704401991431562790884053 23.21875, 0.00005952046424592149171028580180601721840185718501141576954439150321 23.25, 0.00005823491687412761868676298579435896483160167781201448098581174918 23.28125, 0.00005697641475387276216359206277396211065374094892871894258390299004 23.3125, 0.00005574440679316052663383809188281764861820966987652414032315264852 23.34375, 0.00005453835268852186878531238500048799385672780022348057221822442141 23.375, 0.00005335772272462254527563024879395397927735439178845553063858215502 23.40625, 0.00005220199757732827714788489990293595417594306070136515629776283048 23.4375, 0.00005107066812017447222150523196423952685393469285267598463275615529 23.46875, 0.00004996323523418800075852212929662246409815454710913316134150548538 23.5, 0.00004887920962100916998008242555091766367841483179702915058181669015 23.53125, 0.0000478181116192626894743374655266654575406758120820936605950737021 23.5625, 0.00004677947102412706208313595233786499202342024827934006726292647601 23.59375, 0.00004576282691005247337508330230842902298102243511017340813981395371 23.625, 0.0000447677274565778872056317499690747362624231867898633817985470593 23.65625, 0.0000437937297771986850352554135425221336998827886619493439789302671 23.6875, 0.00004284039975123681253379670446043984501519344615900993762418392406 23.71875, 0.0000419073118586660184569947285608617578306706629993117154878233081 23.75, 0.00004099404901784538775903085586995301528728311762531688369813434673 23.78125, 0.00004010020242611498332628333661944350089804893613097429953106308809 23.8125, 0.00003922537140320801851049554364989556468101965758643195926342113358 23.84375, 0.00003836916323743458573671778343481058092050722152885304658234027891 23.875, 0.00003753119303459256479940370433645590117679110960812095556438343489 23.90625, 0.00003671108356956192797976483155492971857250021170358904521577154907 23.9375, 0.00003590846514053924776373781076711248694880505241223626426808428884 23.96875, 0.00003512297542586979666139851899311716008447625250801034312089627671 24.0, 0.00003435425934343520737782202742671887929322359545673893338585822963 24.03125, 0.00003360196891255523531833736777215345157742849628888979094690502462 24.0625, 0.00003286576311836273408749308665549759879265802360447725791455186413 24.09375, 0.00003214530777861151822388892268113286756648704179048323119263912845 24.125, 0.00003144027541287734586871285896801195766437426917451514554434418197 24.15625, 0.00003075034511411280736393656652116248239197285807216983836057906067 24.1875, 0.00003007520242251745388936249825485277379231864480077843288037134996 24.21875, 0.00002941453920168504315179123317512935654625549696766742525457864755 24.25, 0.00002876805351699031681311154918650178209163050620653243852008521525 24.28125, 0.00002813544951617825676855145680139156833070927093278479375383024601 24.3125, 0.00002751643731211929454583651295558635459090934075544231774790172113 24.34375, 0.00002691073286769446997738910130875101116500608532625523978557365882 24.375, 0.00002631805788277505189032391522539569734955574261962990829263325496 24.40625, 0.00002573813968326164485466700146922584894546013116703506595190770654 24.4375, 0.00002517071111214831202314185546624403353599889024803548978405608175 24.46875, 0.00002461551042257774478251425049737850997537596893587789632318664474 24.5, 0.00002407228117285400531556991147330043301983774871251851450665827823 24.53125, 0.00002354077212337985824515356794388942498591609480913700653682644949 24.5625, 0.00002302073713548619230022102608671819569966302451441613704668495191 24.59375, 0.00002251193507212151241342850509717582783073501555679145358914683137 24.625, 0.00002201412970036995683719757997820783791841338076163604324873481977 24.65625, 0.0000215270895957667627590816544447970832134824929731498906750215182 24.6875, 0.00002105058804838056751802354987744046237063533600583009594587653486 24.71875, 0.00002058440297063239088283906466356114347272725426194621027582817436 24.75, 0.00002012831680682159696673071837385105052741852026614327286143560002 24.78125, 0.00001968211644432958223214449079528789950621558029181019021773614603 24.8125, 0.00001924559312647237870565533512776565066737752912173161178832537556 24.84375, 0.00001881854236697379899107866368376159200966546455682293648277893206 24.875, 0.00001840076386603118196031840702000655034059182269287099528485900887 24.90625, 0.00001799206142794622513657269173524568327746841323330882391287776355 24.9375, 0.00001759224288029381178569583413993984491019241830879159114407031741 24.96875, 0.00001720111999460215762225055131600717609773881572106155553845033978 25.0, 0.00001681850840851801384173410051871025074598495287238839235281299405 25.03125, 0.00001644422754943106993539819247641126757136902439486920679635053265 25.0625, 0.00001607810055953210145584073183987079085132567157309570294740366882 25.09375, 0.00001571995422227980460797822870714285280948334543113188071293435637 25.125, 0.00001536961889025165126992845061038165744998245585888578365038246124 25.15625, 0.00001502692841435448483148113690230420003731006594235077955397247736 25.1875, 0.00001469172007437095910482290023033233689121076221002652880132646578 25.21875, 0.00001436383451081829954445420214969705785683046351782239706992450026 25.25, 0.00001404311565809623814302300216069149647199081177985915198768523871 25.28125, 0.00001372941067890134068007932042733714441051123445665040524492130513 25.3125, 0.00001342256989988530752521158717689139782130419083897296652533752259 25.34375, 0.00001312244674853518697001018175246576847907283881256500970380671417 25.375, 0.00001282889769125379311979563564182492148552098313902410119575607313 25.40625, 0.00001254178217261896875161997926563651203568040311789173224684259534 25.4375, 0.00001226096255580067727582689273740153378152888764445632313251505005 25.46875, 0.00001198630406411524706108793074938529421113230216171474252045442731 25.5, 0.00001171767472369642593445360772000620937205159580117590877498659913 25.53125, 0.00001145494530726323368615947798572596540479924381916640929685743639 25.5625, 0.00001119798927896492593172112924712447151609667204493008798750526629 25.59375, 0.00001094668274028370374964270372502647708926783173683824023095281046 25.625, 0.00001070090437697612016061767816659538341079751103488326889166035906 25.65625, 0.00001046053540703444678251779351610248881422105350977817626879219001 25.6875, 0.00001022545952964957192415096113029682743718001134145106631982150661 25.71875, 0.000009995562875157305009404414784013077997478320536666046880823933648 25.75, 0.000009770733955950261591909807753923497653950270099316962063062368097 25.78125, 0.000009550863618337798368933208959956071459191160980201358499041403441 25.8125, 0.000009335844995336758572167657583402805363600079503607120667439362077 25.84375, 0.000009125573460376074943029750480257668839471335826996985488572240837 25.875, 0.000008919946581898560231630386735555149501242308362645355485429848387 25.90625, 0.00000871886407884349383263187790613075958361834865758483261784609632 25.9375, 0.000008522227776993887828659183362328850013686859396487778017048386525 25.96875, 0.000008329941566172586393832809173033084629048828984984237515839123666 26.0, 0.000008141911358271619257436420022992605960045619004955622676669767312 26.03125, 0.000007958045046099492781876416438557447091373145729884332417420536163 26.0625, 0.000007778252463031361211119436408081905057087134386725835034629384741 26.09375, 0.000007602445343447275836907679055217064318996858067369808359245174697 26.125, 0.000007430537283943961251449474184400526565243615679842182017221641264 26.15625, 0.000007262443705305815548142965309452236086660592461182675814934381681 26.1875, 0.000007098081815221075337358403153792211024034668000962076005616039223 26.21875, 0.000006937370571729326803473133098068612004220261633929770832704268273 26.25, 0.000006780230647386780783251331024683573873950108100948787955523574663 26.28125, 0.00000662658439413596303523670458507941336921156156270850647810766972 26.3125, 0.000006476355808866700535936198699149474579715183450162611211587571363 26.34375, 0.000006329470499655510821965176338096337280079599211005745689568110189 26.375, 0.000006185855652670724138629374640225162090792329393770173120779718508 26.40625, 0.000006045439999730887495134649258686697840317653655458993901842078532 26.4375, 0.000005908153786504215705097670922780095775183816017614009923108820274 26.46875, 0.000005773928741337067148480803064915581630014793324292437587521895604 26.5, 0.000005642698044699631367529092963859412071734420370444223935688391037 26.53125, 0.00000551439629923722174460841523826757565867048301904576406619271739 26.5625, 0.000005388959500415769443708357572134804458269913113950718087789988393 26.59375, 0.00000526632500775031456926442936130681832714029059773686705262972888 26.625, 0.000005146431516605487145151652899142392680684316549398731757805797402 26.65625, 0.000005029219030557164082274178321326279003997141610169012633909107264 26.6875, 0.000004914628834304678823972342811246907254150534252175133498560680778 26.71875, 0.000004802603467123147873111451305178057145837148047995352808416983759 26.75, 0.000004693086696845662951592837993829923780727706159700942195715047718 26.78125, 0.00000458602349436527916028334274540348066985740796863387683202778438 26.8125, 0.000004481360008646908232892000323903794161496511690872748733691391578 26.84375, 0.000004379043542239401848775974153965141925007611997093955607030661912 26.875, 0.000004279022527278283024414830150484828633328262690704258965241554677 26.90625, 0.000004181246501969753878470598817831989106253653660500959841560313458 26.9375, 0.000004085666087546775597797111986609880263262303388992941023535823094 26.96875, 0.000003992232965688181258046204610351488778197884856613530277086458381 27.0, 0.000003900899856391944308930251405175145395189043308388262282299368303 27.03125, 0.000003811620496293885056744836105881277320626331785654038588022267089 27.0625, 0.000003724349617423254401147680792086044533592934880058008595078385769 27.09375, 0.000003639042926386788444852915008494142682766877324841794163199139462 27.125, 0.000003555657083972979428959479942279025074155541353989764120681944442 27.15625, 0.00000347414968516845778791530970074363191676750209792593569933278042 27.1875, 0.000003394479239578527001148460062167807696340266303008543629732842545 27.21875, 0.000003316605152244037377390492287073542857937553094529146431779006894 27.25, 0.000003240487704846926976585896694930275214097255504568896450963054228 27.28125, 0.000003166088037296897586623343552789799844806712929859706321729584945 27.3125, 0.000003093368129691831061226586260749136667479018381115004858257312585 27.34375, 0.000003022290784644686424176824015428381019505277696808770738072332588 27.375, 0.000002952819609969750986259850773590361514035518442441674336000079856 27.40625, 0.000002884919001721249337278127067380395520935077768881432779511370302 27.4375, 0.00000281855412757744249815859062050171218745053939576677986316737615 27.46875, 0.000002753690910563475779319827342670217214144834632136453798389208909 27.5, 0.000002690296013106358022414116470007252120137929963327158275507984765 27.53125, 0.000002628336821415576934385703840400492756081017268979480793092954276 27.5625, 0.000002567781430182975186218570082287038836515495954886167175045637867 27.59375, 0.000002508598627595629874193854093323235352966059164686604946002955731 27.625, 0.000002450757880655593859024332161485213460857081915233455903782498452 27.65625, 0.000002394229320800471437642583380561949102314353523444612702729288928 27.6875, 0.000002338983729818912793128577997584391127439356166948793386736616462 27.71875, 0.00000228499252605522173938563353144666558307318828426728023364778893 27.75, 0.0000022322277508973794575017929238461916859806717597546341075735085 27.78125, 0.000002180662055542893238734418951478931474883365179645089564827815712 27.8125, 0.000002130268688036983732874308792846319153142627973823550662623424698 27.84375, 0.000002081021480577726878205057381486099498195993669707500109646830601 27.875, 0.000002032894837082867587875598263947498065167023128542727210409457101 27.90625, 0.000001985863721013121414430217716501341125106330328284708944022117578 27.9375, 0.000001939903643446877836352714592328640047322231101318814541209802092 27.96875, 0.0000018949906514013145343230775463802280720842331618129644116659749 28.0, 0.000001851101316395026076681969601459241095043380348900286485683643637 28.03125, 0.000001808212723247362839260132847452079338910705057034870376492559786 28.0625, 0.000001766302459109766769851319233147214179782906572331017683971369861 28.09375, 0.000001725348602724479797469738137054704216530424921187449050074183458 28.125, 0.000001685329713906088306105418697434465036215119001427091838597583663 28.15625, 0.000001646224823241453166631885779054326952183842077073131437885678264 28.1875, 0.000001608013422003659373179756605461201826836413859641251324162552946 28.21875, 0.000001570675452275702385709608274780243213212397021089126040427962524 28.25, 0.000001534191297279709862434523668014636830240031572681631033560735766 28.28125, 0.000001498541771907577597590124553639916020666726865164564859619999825 28.3125, 0.000001463708113448977184958785582074587271110754292118086127581696312 28.34375, 0.000001429671972512770228356255741579023316715877339171626923804201579 28.375, 0.000001396415404137939839516252344074444936788452460891527048518320958 28.40625, 0.00000136392085909022472369890649829082454526539165859980417206763623 28.4375, 0.000001332171175340714375845476829640480918505368002364614817599210749 28.46875, 0.000001301149569722735816855948812779270941336519661571184855431355449 28.5, 0.000001270839629763432911939783791213187238932597742279851538403150196 28.53125, 0.000001241225305686508652057496195861240144465238986358420858324742958 28.5625, 0.000001212290902582668866021010965486170139845769145684156348885434566 28.59375, 0.00000118402107274437268536001317642340336109720133316951649701896126 28.625, 0.000001156400808161560726815211217034912898278955980986798819242210471 28.65625, 0.000001129415433175096408234721418667513654808718773638955283563487812 28.6875, 0.000001103050597284719092397797081539376682519034145448436055759226884 28.71875, 0.000001077292268108369879268427264193154892288817833539726492306673223 28.75, 0.000001052126724489811859516296012269782048407070460085012207369225989 28.78125, 0.000001027540549751526519691843990744393020173631618078873133126719207 28.8125, 0.00000100352062508992677079703405397087682197359917003788740606646092 28.84375, 0.0000009800541231099847754813818302186733074426185603786960642720711132 28.875, 0.0000009571285014964293927800200492108829120051819752721501114863789448 28.90625, 0.0000009347314968187236610043279024713544977951871655213858792092645397 28.9375, 0.0000009128511184670873166469553861410525529226204071972630302084551949 28.96875, 0.0000008914756427168829172690905589151662886624496093652846753692017444 29.0, 0.0000008705936069187367163444782444489071703848269587137146822636640833 29.03125, 0.0000008501938038118170447392122068954611943431315041499378431623610846 29.0625, 0.0000008302652759577436034597409218204437695998167011117505985768745971 29.09375, 0.0000008107973102926507818113033444895175131884788172048709148404921561 29.125, 0.0000007917794327949769002415662970804838357712710632907934420919374576 29.15625, 0.0000007732014032665991537275259156769982665942236501954151811272222518 29.1875, 0.0000007550532102249810151898255772727535788774620884365867679676700926 29.21875, 0.0000007373250659040449644462743828678362797085353923815618974449536159 29.25, 0.0000007200074013615286517735170519422339199951859630063943673740923558 29.28125, 0.0000007030908616906270011323231568895546247789425401158748959318983095 29.3125, 0.0000006865663013337663212020364433062469719080446343534452968571326474 29.34375, 0.0000006704247794963992370145145736694663360545597249233284123479003138 29.375, 0.0000006546575556587511954080736798967208104929654850979699478818198554 29.40625, 0.0000006392560851834904477502808528042424098908985207545766147266629159 29.4375, 0.0000006242120150173337872022837670456734256396939629654980174353468533 29.46875, 0.0000006095171794846399288012679789439093172312991686751368825466016834 29.5, 0.0000005951635961710812821958274467447163218995589779264985362343223077 29.53125, 0.0000005811434618955229921479879915283299260262021616922188676437006851 29.5625, 0.0000005674491487682755238765466157295488010727690622629565652948244727 29.59375, 0.0000005540732003339237617177433744572164115869942874111115758215397308 29.625, 0.0000005410083277969715829792766462855158934140320616038260506111076237 29.65625, 0.0000005282474063285761766227504020717277295996415012914630950176501385 29.6875, 0.0000005157834714526810106929532629669039014066570090351502678394241227 29.71875, 0.0000005036097155098903251922307945300359625033782143878160746842691348 29.75, 0.0000004917194841974613501565561925297202909121815611247370414538455109 29.78125, 0.0000004801062731838231336207100914843434115776531644555270345167293647 29.8125, 0.0000004687637247960629223716951170334496767548288475708751058658032173 29.84375, 0.0000004576856247788524811074547604883739120682875510913367319149005232 29.875, 0.0000004468658991233175738867207011409207205911327255312385573586939314 29.90625, 0.0000004362986109643840764415949493729198538678501326073143025317858784 29.9375, 0.0000004259779575451638497174935801482543762790883601496452614261180128 29.96875, 0.0000004158982672469725944218824490001403883726730796250363900736660895 30.0, 0.000000406053996683600433749026291121611739111584359537753382355077933 30.03125, 0.0000003964397278584839469789215108863471942010749267910541548516407262 30.0625, 0.0000003870501653834558103341163919030141517973749861555747364347736915 30.09375, 0.0000003778801337577751031631950474345108727988181458560530127331902444 30.125, 0.0000003689245747061677168870496832225946878100152755958389779373472541 30.15625, 0.0000003601785445746321707164434242328248576333106367402944845465438431 30.1875, 0.0000003516372117827915012917608827411675347866979170050982724376384693 30.21875, 0.0000003432958543315967623176756020406355917188685085834494239286487197 30.25, 0.0000003351498573652120540227630671812231674765546192287354071491439357 30.28125, 0.0000003271947107859349097716560130290512531660064236159352621185294807 30.3125, 0.0000003194260069210293071508865248891001971376466586925776424773355957 30.34375, 0.0000003118394382403715519478289306111813512888035241389350534958260047 30.375, 0.0000003044307951238318141090221127647799824156503708510974815062389607 30.40625, 0.0000002971959636773361833206950552972610719820681355490649277425303157 30.4375, 0.00000029013092359657576648090325933846104644087370049723109555813002 30.46875, 0.0000002832317460773505780709772745946307006957693398827121651480278649 30.5, 0.0000002764945917715567851889923902031291002233097480817827847843141148 30.53125, 0.0000002699157087878462695500279431255921060969186287440711675093719619 30.5625, 0.0000002634914307360074667247455698531763897214015276160684724872606509 30.59375, 0.0000002572181748141360457861991663072242958630427071396269540652278917 30.625, 0.000000251092439937683207742926063031226082987854918825968579846822269 30.65625, 0.0000002451108049094882159055170638210327980328559907441446292504421769 30.6875, 0.0000002392699266299202327903051349761556049755847456469985175153117498 30.71875, 0.0000002335665383462726333107322818767736368659220736982476123353544447 30.75, 0.0000002279974479405706997262920072432491913504202034169560797932922894 30.78125, 0.0000002225595362549709868732333282925136390695276122375744383798329842 30.8125, 0.0000002172497554539476832353909810416283613934857171200882880653462018 30.84375, 0.0000002120651274224779909567152250976897177238018187934632453227706712 30.875, 0.0000002070027421994549123644277844166354595740345154368295435389945283 30.90625, 0.0000002020597564455718682660691249690820353440322444511069022047751495 30.9375, 0.000000197233391944939290397327048917539917108606363553032348729736029 30.96875, 0.0000001925209341397087330139193146727538944348951030327387065778610715 31.0, 0.0000001879197306969951427162782839960667278117374883135338096003099005 31.03125, 0.0000001834271901074027170412220044238154099487056262844808354831639362 31.0625, 0.0000001790407803144742769172470567836040881722248918015401170057888835 31.09375, 0.0000001747580273743982814244128921109559486536375117513607044754547436 31.125, 0.0000001705765141453215309903003742500836598635401742745061342804520683 31.15625, 0.0000001664938790056292426555307101445879588682589362796593588757381747 31.1875, 0.000000162507814600567543723724176807061111076540711464117793384209858 31.21875, 0.0000001586160666165965232435985714900387427336316942303980016359519737 31.25, 0.0000001548164325828748095328619956571190877907809043951520226517186256 31.28125, 0.0000001511067606992892114295311436758788106145216894583196799606763594 31.3125, 0.0000001474849486904552761398652283663448541288458833765196853190295922 31.34375, 0.0000001439489426851266823469529690120238535991381886126959906488324305 31.375, 0.0000001404967361204632084654303539492299703511384425206150540182888752 31.40625, 0.0000001371263686706185973041777372265400529519598207488354443588923991 31.4375, 0.0000001338359251991209845729318722635906119960413015170464544165451573 31.46875, 0.0000001306235347345296741991817014914197883445813269376247575042799277 31.5, 0.0000001274873694688629327843479494607157769584856862110747603898751727 31.53125, 0.0000001244256437783021431175084180499919432113646570220578646500119037 31.5625, 0.0000001214366132656881067951771690165484381358756474071301317230331396 31.59375, 0.0000001185185738243355229024736168520486993525817512370551166505016388 31.625, 0.0000001156698607227016975525712754718865489517110708516508220985308449 31.65625, 0.0000001128888477094553619395729273963959234094763737362131610647628592 31.6875, 0.0000001101739461385010984419978096010809748657499084474851763699177984 31.71875, 0.0000001075236041135242991533168521585123570358310336466750614703081387 31.75, 0.000000104936305651630812873447861998856411760707194706390788369971413 31.78125, 0.0000001024105698656644788606368265669021705153638986190634588165699179 31.8125, 0.00000009994495016479460223651440698241433414249924835500333397014469972 31.84375, 0.00000009753803347297410050931640372642545461000805953785865799183059637 31.875, 0.00000009518843946487754681462362768579333132025778876803996907568908405 31.90625, 0.00000009289481981893665668632932295272893915725928901972336394039536961 31.9375, 0.00000009065585748709891491431966833041397664225111137890043238026493104 31.96875, 0.00000008847026598094302070672700873839995148428499623429965202062669901 32.0, 0.00000008633678867379264627758957841876281314089099171515324498246874557 32.03125, 0.00000008425419811847765938724153602659495254168897581641263619470048387 32.0625, 0.00000008222129538039945747365739344796505852655587663279702858236809635 32.09375, 0.00000008023690938556440296920770398662424523085301153074238913103423624 32.125, 0.00000007829989628325653928083268606491351985633615137188564808495018963 32.15625, 0.00000007640913882302780774657263361626000325302409360419881654393072129 32.1875, 0.00000007456354574569088063485801173258707666308904794798932348540600331 32.21875, 0.0000000727620511880064768361799274388835110352479218732943890969647408 32.25, 0.00000007100361410076363816600065372848250366330428589595756139302695111 32.28125, 0.00000006928721767995791795529551351110529587418460548811087181639761087 32.3125, 0.00000006761186881077877260016089946207593357921393610667961394710185547 32.34375, 0.00000006597659752412365367157476582335183775977825464381717541925019918 32.375, 0.00000006438045646536237569654386220643789924837058571559026089833051007 32.40625, 0.0000000628225203750812854080974462122230362759527439264119625405245078 32.4375, 0.00000006130188558154258467005111250009544004225675946272304929832003661 32.46875, 0.00000005981766950459986391078041170648928950147750195749030905445320883 32.5, 0.00000005836901017081648819833801664438696094889037325867888374835008112 32.53125, 0.00000005695506573953894646020438453051348895446586625733711394153064407 32.5625, 0.00000005557501403968262815185498537664285071387600237807347600841242583 32.59375, 0.00000005422805211699273322104227121595762845832804227990707571294434215 32.625, 0.00000005291339579154815276672940983903443697298705591383011520428170292 32.65625, 0.00000005163027922528118157688752151868164155777423105260759360156738902 32.6875, 0.00000005037795449929084192898220624743506535259781508142758521191437467 32.71875, 0.00000004915569120073241278999183106119369158915009524561912301647753384 32.75, 0.0000000479627760190704719569986304497545584377486847079253594302280525 32.78125, 0.00000004679851235148737279201335005304855477620512198158332417835396328 32.8125, 0.00000004566221991724359404316597893233573347857338826902632461092948469 32.84375, 0.00000004455323438079082278706202275986355402300299229340936374440429841 32.875, 0.00000004347090698344295871392897828666109762056105918972386649711918179 32.90625, 0.00000004241460418341446471044142931834758745095999070465205588559434386 32.9375, 0.00000004138370730403963584009817209312999671267394521499679729033613133 32.96875, 0.00000004037761218999041820669022687396701900484686479732301812273796132 33.0, 0.00000003939572887131438260604332754407070022986748288164020227351863179 33.03125, 0.00000003843748123511834708313300428750947280540172176279982705463628296 33.0625, 0.00000003750230670472694923977815391093751978336766073223959141365996874 33.09375, 0.00000003658965592614919507260710032860011552884839141139383863019521695 33.125, 0.00000003569899246168965791892939788552498688241724943139146773797582512 33.15625, 0.00000003482979249054457037410135889084370222676952972389660134120722873 33.1875, 0.00000003398154451622654541060092362077341579961338734724026583872351516 33.21875, 0.00000003315374908066508193766806280947370313068581486121668680216347386 33.25, 0.00000003234591848483335622162261237547305507476868939675777741830443996 33.28125, 0.00000003155757651575507544127646362679371379281083225068889502539016576 33.3125, 0.00000003078825817974837465103767107943254258845581767567979094526284255 33.34375, 0.00000003003750944176687500812937045870034726642980015076271425775686825 33.375, 0.0000000293048869707010907030695536960425755486324699857993914518908844 33.40625, 0.00000002858995789050637599944796474993735062755899743497271909144903094 33.4375, 0.00000002789229953702654349790195983596975017091876117828271976341874402 33.46875, 0.00000002721149922038516152089547055421437405141212898174608869823264139 33.5, 0.00000002654715399281935367386378393571733293632226790059912609581792534 33.53125, 0.00000002589887042183367845297594872998935483976505596013894696160731694 33.5625, 0.00000002526626436855436249320303499761075259433627229349744863845661775 33.59375, 0.00000002464896077116679891060302770132829247919630300628580288192142508 33.625, 0.00000002404659343332180339327377996132587852050663682471986432674792126 33.65625, 0.00000002345880481739864641577222598619965327572453825906046536700323453 33.6875, 0.00000002288524584251535134784995184978396815892013458774694141311896467 33.71875, 0.00000002232557568717916643286912479246370249137638329156705274789542221 33.75, 0.00000002177946159647248473429256198352705191921337106240086204258164154 33.78125, 0.00000002124657869367180127796964764618231212549852815335755953116493921 33.8125, 0.00000002072660979619956181950036875357609720477810910329696882961274658 33.84375, 0.00000002021924523581097398425598713407966603195547491142788403121270323 33.875, 0.00000001972418268292001998614742009543323689639888321366916343108818821 33.90625, 0.00000001924112697497103173282121296207839758668716205421890090294542495 33.9375, 0.00000001876978994876426485226197414116000076601353967051514190937232882 33.96875, 0.00000001830989027664593899158481994037650336796249069656368651374843187 34.0, 0.00000001786115330647519858644957413746695738940153443041613305470614484 34.03125, 0.00000001742331090528239210327426700006262080475343856986845399021087943 34.0625, 0.00000001699610130653496942180645493735204859503957680455224353861025133 34.09375, 0.00000001657926896092915743980877016524321373276554058182813025135852094 34.125, 0.0000000161725643906273940138099964813021403936859743467586494130893364 34.15625, 0.00000001577574404686328085159111242747184398456560753740786051880207656 34.1875, 0.00000001538857017083755777753113302861619574820417377812266075712490782 34.21875, 0.00000001501081065783030471836731456176342619862998780805179682296352162 34.25, 0.00000001464223892445624460491789313807670418917674352042266976645786892 34.28125, 0.00000001428263377899165093913831784803142187040273927274043742121120139 34.3125, 0.00000001393177929470295880379248234184279562688732374930194963283051836 34.34375, 0.00000001358946468610873834658228561387119362693028274142364476274645099 34.375, 0.00000001325548418810821598896647607070586734578586976756522602194447394 34.40625, 0.00000001292963693791102151419908031548691490376501504912464683014066437 34.4375, 0.00000001261172685970429948662106920772868446737799305167652939538444454 34.46875, 0.00000001230156255199475183773443258827861317491869263106196818090113707 34.5, 0.00000001199895717756457560264378538633283607493083787559635059787327947 34.53125, 0.00000001170372835598162636769436690928017156802141927845619244216450384 34.5625, 0.00000001141569805860547464752711736865679918113205411452713067297710873 34.59375, 0.00000001113469250603232978487345238598099510460470004279986012994309059 34.625, 0.00000001086054206792308468365200093610925929576851505777882271790164194 34.65625, 0.00000001059308116515998535685902158261124510861240533245181601780730064 34.6875, 0.00000001033214817427865249429420309555715989065413703623970190819359939 34.71875, 0.00000001007758533412337861789288155100724538576343119692480896059276685 34.75, 0.000000009829238654674794468775988681320939228816074852749655529239272354 34.78125, 0.000000009586957828000142622624946375111926038222992583451809870355227951 34.8125, 0.000000009350596141277515509533664011088519452936307623009031545622065991 34.84375, 0.000000009120010391846509560557281978522782561864285219288987628272470736 34.875, 0.000000008895060804238817644047900834095003745381370802330910068534984836 34.90625, 0.000000008675610949143328807851590268752371940670786509891718869481353677 34.9375, 0.000000008461527664261328115097156945248051651250199710148060934393308738 34.96875, 0.000000008252680977008390547656883443170013770822712124950809849481496859 35.0, 0.000000008048944029020542038097278691912038946777102430738397580492269133 35.03125, 0.00000000785019300242321815363788836899620124864746731946103270685262437 35.0625, 0.000000007656307047822487259954531662519857459415186756392720561737533247 35.09375, 0.000000007467168213978920594536804689831543588679004410021547201748917253 35.125, 0.000000007282661379125387025150786320143967635471252138734907121485369367 35.15625, 0.000000007102674183890925795845885085260183244431535459861625581715161627 35.1875, 0.000000006927096965793706698813346069436498564358388140403616994640847623 35.21875, 0.000000006755822695266924274225137949501046897668325389977396412955144085 35.25, 0.000000006588746913182291242146660226930605125546411794968597232119084365 35.28125, 0.00000000642576766983659681231173163200488816129096016275702062736770082 35.3125, 0.000000006266785465367578192129953938968253571715699022400617749095354448 35.34375, 0.000000006111703191566118905660947497020049327676661876151571696332944711 35.375, 0.000000005960426075052535823240175613802207025606519692268681132266665153 35.40625, 0.00000000581286162178544845185089618160478002264029642431616956443035695 35.4375, 0.000000005668919562872439411311399466411850595605549931625012856931520467 35.46875, 0.000000005528511801652414474380316004147718702912312120584580173244783773 35.5, 0.000000005391552362020254426013167965778396519727746242683289135599987841 35.53125, 0.000000005257957337965019636963367820091705910647619106171925075872235357 35.5625, 0.000000005127644844293621981287162626752558731341460935747682461185647672 35.59375, 0.000000005000534968512517880650700088488422812788208231738813760878335123 35.625, 0.000000004876549723840601148346991433969742429624247847849035235937061364 35.65625, 0.00000000475561300332708524358216864531269823588578590166700472985037078 35.6875, 0.000000004637650535048761836265166165462750629195784237155382886887381443 35.71875, 0.000000004522589838361606522158614314332001880670639432464141636710258162 35.75, 0.000000004410360181182273409423970592391177898629493370029770607383076171 35.78125, 0.00000000430089253827557840572977192714065859855328647128671288386728679 35.8125, 0.000000004194119550524616649530783339027049219001487081286842917237709551 35.84375, 0.000000004089975485160692923268062125879427753315404306710699680360486586 35.875, 0.000000003988396196930765327665840913365478768333860422337742076006121777 35.90625, 0.000000003889319090180612246890600797249505421414049985090663866265465439 35.9375, 0.000000003792683081832430950387681893253869632528065319532931831993785191 35.96875, 0.000000003698428565236063309550188625824307214042145460466434737690958001 36.0, 0.000000003606497374873520301477471627038178728642943972792971318145945264 36.03125, 0.000000003516832751896942468175072547915076697974576015513651803368359971 36.0625, 0.000000003429379310480588532727299777580062872990830809438339741874560078 36.09375, 0.000000003344083004967889174299996518322695427547070573564536135204119287 36.125, 0.000000003260891097795037756440942352457978092396740372992482508868361504 36.15625, 0.00000000317975212817301480835104956865308631448482899338162299929414392 36.1875, 0.000000003100615881510358492189425193594260428837663656071984622179068917 36.21875, 0.000000003023433359559399362011841313842114167547878321007657912258832999 36.25, 0.000000002948156751269074638057664476192896324605874836750477244650162689 36.28125, 0.000000002874739404327825185791582419710284927554389689144684499730081593 36.3125, 0.000000002803135797380457600026832777139539554174631790694915623590798561 36.34375, 0.00000000273330151290322444400638746027309170211782360138537055918897894 36.375, 0.000000002665193210721737970733626247907643533726700710357834776421027945 36.40625, 0.000000002598768602156686744282903176910736204891406653808977475267987287 36.4375, 0.000000002533986424782670663449168640454217459137329096444611201760277865 36.46875, 0.000000002470806417785808146171618397722708535934410838360051895463808051 36.5, 0.000000002409189297906099834119165963235674604987779545339205290174080675 36.53125, 0.000000002349096735950856292338258310463279566740230881321503372579820971 36.5625, 0.000000002290491333865812974950465045817140086869292426369848545655856367 36.59375, 0.00000000223333660235086436697097774391177933324142661226222337698700825 36.625, 0.000000002177596939007650853305953046738741036104345132881896156944776983 36.65625, 0.000000002123237607006526664340341911306991151788195061277915290906055938 36.6875, 0.000000002070224714260725355342168413992464252615694480462182963609937544 36.71875, 0.000000002018525193095820842978622590363535645186626350492602821989252209 36.75, 0.000000001968106780402857192128501881867850387783587042998224106684101482 36.78125, 0.000000001918937998263789262287431515377400054914787730674643138149131648 36.8125, 0.000000001870988135038139124504806199348647925936477425973219597270325017 36.84375, 0.000000001824227226900029983242661303234115842695987399917423275691748049 36.875, 0.000000001778626039815010316119070744700100715672412051344052333042956861 36.90625, 0.000000001734156051946326208601466390793359681118239116464632489898579571 36.9375, 0.000000001690789436480539537916087821129878141570134832348078610077405072 36.96875, 0.00000000164849904486262387552468757721892699197456101058239236445704328 37.0, 0.000000001607258390430898852552126746488711499653948542921251799930870919 37.03125, 0.000000001567041632442387386927219470441818089362575317579711915504278194 37.0625, 0.000000001527823560479398721514416291291117559985970323623827036482274197 37.09375, 0.000000001489579579228353783403008104327328628782500645996500294654758634 37.125, 0.000000001452285693622078057522148871083914547827579191115472577438199599 37.15625, 0.000000001415918494336991082157250405909230964578652215904053613767645577 37.1875, 0.000000001380455143636820926655809344659820973667865081591767713043205654 37.21875, 0.000000001345873361554666707185575354139878806560510488001464288522718267 37.25, 0.000000001312151412405422437109659498347901015775138295181938281695975253 37.28125, 0.000000001279268091620761394392522194668300917071575998960019588814379216 37.3125, 0.000000001247202712899061817272787257626159258444679325450998474866952273 37.34375, 0.000000001215935095662832206909631514642307966819028860708641463564949105 37.375, 0.000000001185445552816367915402280285959925251408771785365427654377627696 37.40625, 0.00000000115571487879654012101212190369231118036348815479019976042931978 37.4375, 0.000000001126724337909783829085465396164667325794003677294939656783402138 37.46875, 0.000000001098455652948513274612579559761278722806635066967069204177120806 37.5, 0.000000001070890994080351126167446410225575035711721815108346652691336478 37.53125, 0.000000001044012968003712284867594554892361432636717646497800968046016455 37.5625, 0.00000000101780460736343391784280331833345809290168985182415238873478716 37.59375, 0.0000000009922493604202907435668158089160749995729093195457278797108893464 37.625, 0.0000000009673310809683785745808237020662135572874237723907635827589062414 37.65625, 0.0000000009430340184944897981847122593571331082318711179909269490343373439 37.6875, 0.0000000009193428085737419124624241314774892816802555020015768520458417748 37.71875, 0.0000000008962424634958545067555509809457128365917651175439901402840562327 37.75, 0.0000000008737183631166012539980348633145314316357459072557532262878528849 37.78125, 0.0000000008517562459290916371826712448199675078802566698557899550899974894 37.8125, 0.0000000008303422003496623321035757679645303619183161806455564506057143342 37.84375, 0.0000000008094626562132804803466590246917186276237326755822513764606799194 37.875, 0.0000000007891043764734805757361455080829501950005671363839729369870995535 37.90625, 0.0000000007692544491019734180900487836589978913973153947260788364323729858 37.9375, 0.0000000007499002791831796227705815103580640717571142068851752546358046445 37.96875, 0.0000000007310295811990515743253714685834246355792434619813884361993165232 38.0, 0.0000000007126303714996565373301857926552574598225285985552735753739726076 38.03125, 0.0000000006946909609550999458607400487204854164036726755898611634154953356 38.0625, 0.000000000677199947784471742035077700300267402600242128754821201942101524 38.09375, 0.0000000006601462105576000797001113784661256434830290857567666921174722373 38.125, 0.0000000006435189013654958062612432854942980546476241503353802373480075255 38.15625, 0.0000000006273074391554679373285165815078911182640334129877605568740133191 38.1875, 0.000000000611501503226984897539979511191238877037975557129723371141867482 38.21875, 0.0000000005960910268844486677198263723411757683399953256100960541230455822 38.25, 0.0000000005810661912431392033912643720393313129358071471941339579574270804 38.28125, 0.0000000005664174191846746214315710340037294247026358933191220240081460676 38.3125, 0.0000000005521353694584187380772168284318333818362698424090494663939242588 38.34375, 0.0000000005382109309253516292399728671331701006043116719271949720708789455 38.375, 0.0000000005246352169410010188156573227845719790597349090272100069897012044 38.40625, 0.0000000005113995598741125269688031055878134559290480312544063789940687206 38.4375, 0.0000000004984955057578151718727675540438785447787791535830281920165944694 38.46875, 0.0000000004859148090701150577117542411286497646000996562877511043773578493 38.5, 0.0000000004736494276406249405887807320051886526758484294311670900269085282 38.53125, 0.0000000004616915176805103830764670054336728416008529283637231295176107077 38.5625, 0.0000000004500334289327045273255708266288129083982280312067388216320969948 38.59375, 0.0000000004386676999395131748447368550186634650848803827912120098475242953 38.625, 0.0000000004275870534247998963442239601211638672987060487241784507842543295 38.65625, 0.000000000416784391788007344600914188282891114948467494558266523432902106 38.6875, 0.0000000004062527927073358435192017521435596388938026864226769064307069121 38.71875, 0.0000000003959855048494637129813688699434278510968605658419823768361333211 38.75, 0.0000000003859759436832556964501958381428165693760465501473802412156521701 38.78125, 0.0000000003762176873949663205713370669961968350024125017234805410942566497 38.8125, 0.0000000003667044729025040664232818294386894791280935624191039016148142522 38.84375, 0.0000000003574301919663799030295981437434725430630886462394900553357558549 38.875, 0.0000000003483888873950200570004729805177870518073086063392111131117969582 38.90625, 0.0000000003395747493421778987110731855254918379961584531665078441758938534 38.9375, 0.0000000003309821116942335455554581532881372491208381017225715500206995726 38.96875, 0.0000000003226054485452222461543924930003826574851859017165744474075449145 39.0, 0.0000000003144393707574838448915110692537397540472258074519070612349075913 39.03125, 0.0000000003064786226058756620981362984953982879710729794878583961873901136 39.0625, 0.0000000002987180785035399892555741802201927701257376319375880698315483126 39.09375, 0.0000000002911527398072651177617227047345580337195061603581846040426435312 39.125, 0.0000000002837777317005254205308612727473634013434722730538875824609631917 39.15625, 0.0000000002765883001523315137774258101494955427696733231952544769581888095 39.1875, 0.0000000002695798089500659670068970617353572696690787162700440713558761379 39.21875, 0.0000000002627477368045234271577362955888871270786605277051257372876509963 39.25, 0.0000000002560876745254164021060195995081535019397415766488555891351038348 39.28125, 0.0000000002495953222656493329102464195786486778251518701475959564510214647 39.3125, 0.0000000002432664868327039962538705948576458160769722075238927245282426228 39.34375, 0.0000000002370970790655187410304149326344712963856305781399750413144473373 39.375, 0.0000000002310831112752825978920764275089280575126606239873024360729289038 39.40625, 0.0000000002252206947486029293288991624303515207032631823531065115390490551 39.4375, 0.0000000002195060373115420314543226062531541207472251788086135162368027363 39.46875, 0.0000000002139354409530539776585937552951488229380763566051796866595278945 39.5, 0.0000000002085052995063880287011728928410017543030501235950288313442400693 39.53125, 0.0000000002032120963870591432369327878461929827666805112828293834734422691 39.5625, 0.0000000001980524023860195263519845178173427797819464506035267253696775147 39.59375, 0.0000000001930228735166977701299470458787393556068567278716178960446076087 39.625, 0.0000000001881202489146039878580203826253968793582587459248246011437136428 39.65625, 0.0000000001833413487882304400765076013617911666876332676137301966141551123 39.6875, 0.0000000001786830724200075137295650219050554032295736411843613192059366947 39.71875, 0.0000000001741423962161045622441119916206779527366086663082933038632292142 39.75, 0.0000000001697163718038940611131765742169616990526705004353942140674096342 39.78125, 0.0000000001654021241759257967734169188647503741417823775575713698587026646 39.8125, 0.0000000001611968498792854021553712910088703110361495453087089206270713865 39.84375, 0.0000000001570978152492384957960559252061343515135192743259571314536573701 39.875, 0.0000000001531023546860879880276074460911289636178188481689510326424667779 39.90625, 0.0000000001492078689741978023353727712053883442559737161829169426135167403 39.9375, 0.0000000001454118236421613370164878004444229243681638495549001106045253441 39.96875, 0.0000000001417117473631174759352026050211553766884908456998635129163531018 40.0, 0.0000000001381052303942408613084805211951512955653547659513155872686996372 40.03125, 0.0000000001345899230544564795913488291229494662857145247501919630808052543 40.0625, 0.0000000001311635342394513968820502074662675334934409681488016189033881144 40.09375, 0.0000000001278238299730787257444944471652735732659912735355179612050938815 40.125, 0.0000000001245686319942706235652625677288516811124719862465224928802912931 40.15625, 0.0000000001213958163785983258497581852706869483403060682585443214321201761 40.1875, 0.0000000001183033121936379182587357211520848521993429475343382821780340909 40.21875, 0.0000000001152891001873207604568836603932494635998046478395177209909361407 40.25, 0.0000000001123512115084672044830092717922159674526079323884863273849096479 40.28125, 0.000000000109487726458721511585474031292936709349388389949910401830765136 40.3125, 0.0000000001066967732751246752668807956147055348743816502064506250664031929 40.34375, 0.000000000103976526942580215365685565432080282855503599077844060226633253 40.375, 0.0000000001013252080354859288392537411935814677594753571986314801978492526 40.40625, 0.00000000009874108158782207773114333501372465394459294486113525091323598731 40.4375, 0.00000000009622245599100357359717728695779749961599637201267199311467322173 40.46875, 0.00000000009376768191882039019148181736764020393570054456777770003942650161 40.5, 0.00000000009137515127880671201088799970587397451887509397272270613223883458 40.53125, 0.00000000008904329618939521467926196141935153822792746090653901769058505835 40.5625, 0.00000000008677058798222838322236587663474563662625554531391680908805425083 40.59375, 0.0000000000845555362290139149281504288146750222002753699128023786806866343 40.625, 0.00000000008239668779232603339059126022476215509379492474907750512799052585 40.65625, 0.00000000008029262589976896797989612085427792792282223060021641090273101368 40.6875, 0.0000000000782419692409329366542273642314339243539057710840464102298646226 40.71875, 0.00000000007624337108658671782209274926184352826730424773778505599056533636 40.75, 0.00000000007429551842956431678670123745968864405271792554835179576374724735 40.78125, 0.00000000007239713114681633187693862891099631415748837080597666969342174733 40.8125, 0.00000000007054696118210941223808352323862351481971506712226652212954299114 40.84375, 0.00000000006874379174886968078775106241581885293205405736442436672199770794 40.875, 0.00000000006698643655267817923651018895969206053830675865697644717738180052 40.90625, 0.0000000000652737390329382843586618662852456645755088060832512922000886026 40.9375, 0.00000000006360457162324665274390374918614087794286729571953896257448042613 40.96875, 0.00000000006197783503001058177250792886757902559485157674134619380140207607 41.0, 0.00000000006039245752886573408667274212856492214440553709590217747444497504 41.03125, 0.00000000005884739427845896777791842220934902051991530986978464653432590591 41.0625, 0.00000000005734162665117155112487952338085845376874480999063460403941475421 41.09375, 0.00000000005587416158036832510217270270463154506507385606806731434875452486 41.125, 0.00000000005444403092376841500155580551383254002869126876159969220323479043 41.15625, 0.00000000005305029084254289018477161518135050110361802657273177101648290768 41.1875, 0.00000000005169202119575433391095937873385566321553246876273100590890461155 41.21875, 0.0000000000503683249497626189053487549465699218212117527553737011508422394 41.25, 0.00000000004907832760223029428555393756381401917522828743901916350955610171 41.28125, 0.00000000004782117662036988093596519303268129496712990727117080897184828127 41.3125, 0.00000000004659604089308405059457844692694035900791793109919616220969804987 41.34375, 0.00000000004540211019665813384432328151005160414317529240457084426255697108 41.375, 0.00000000004423859467367266881866120191017321355941114024242405204667567926 41.40625, 0.00000000004310472432481177055966318226053019407608804314708295371251960921 41.4375, 0.00000000004199974851325097531394484100129588410351427862426307525876418778 41.46875, 0.00000000004092293548131589921561252402179483986432602445817497242198530738 41.5, 0.00000000003987357187911055127603003386329556932463612236470439671629715303 41.53125, 0.00000000003885096230482146076291054349224088313215156997517571572658665368 41.5625, 0.00000000003785442885641092318843135895531666100146306852281204906519959043 41.59375, 0.00000000003688331069441964141989130710799347300198979369225862200662521989 41.625, 0.00000000003593696361560584296099687353932627823872929944210841749346193577 41.65625, 0.00000000003501475963715459521553515745987188743566640366574606819533829421 41.6875, 0.00000000003411608659119752143278698020333607967321734941350998153043074841 41.71875, 0.0000000000332403477293894448489863697184142054746115642272665245100593878 41.75, 0.00000000003238696133729466099560516552482998747030421076263671033875383825 41.78125, 0.00000000003155536035834156187018667760263990285607659332030147390774276671 41.8125, 0.00000000003074499202711021420063066643720941731336152620472011369897474078 41.84375, 0.00000000002995531751172323083778195377747614915677139779505786825424494184 41.875, 0.0000000000291858115651158727611392327557400911059226292899757296183167827 41.90625, 0.00000000002843596218496678257630764892634844284057297394497919170969127299 41.9375, 0.00000000002770527028207608194072228193305126859540782399287376301347461225 41.96875, 0.00000000002699324935698276822064627672274659575661644326495921606921220139 42.0, 0.00000000002629942518461842292795274117297849508524195604695279681299760647 42.03125, 0.00000000002562333550679919910788499082811226406844663239219991557602645671 42.0625, 0.00000000002496452973236288977637964817487986853133659365904121244208951525 42.09375, 0.00000000002432256864476259759616799711426667816544471010272633366986754313 42.125, 0.00000000002369702411693313002589322033346319355521099533509039656490520977 42.15625, 0.00000000002308747883325073690122411266331544542503414073690465770782081402 42.1875, 0.00000000002249352601841119147244744607712582392292891610736825973917137384 42.21875, 0.00000000002191476917305549392752221544702545234896257118212853551222375017 42.25, 0.00000000002135082181597665090998369310710454009160274723036677289153505772 42.28125, 0.00000000002080130723274505797438801678062397121840689818029876864456458004 42.3125, 0.000000000020265858230593986726667974725792113416174400464182832475605068 42.34375, 0.0000000000197441168994105569341537788826641231057252907859626256175253915 42.375, 0.0000000000192357343786813584656031892196854103763186810057256719894751567 42.40625, 0.00000000001874037063024558078636656176960685370361035838556368583858435116 42.4375, 0.00000000001825769421671211108551727647695964150190958918618445332861459937 42.46875, 0.00000000001778738208540057809613121695754405782885978671215051952162988284 42.5, 0.00000000001732911935766974938183009268163209228104317025351578041290544032 42.53125, 0.00000000001688259912350003734753234651521919316707798974725167417053996926 42.5625, 0.0000000000164475222412001354869507040257426211611005427247318775310697443 42.59375, 0.0000000000160235971421109933532858494171984143484481005242125473524258834 42.625, 0.00000000001561053964018344833612586557221520717139316042150931096645061048 42.65625, 0.00000000001520807274630886640497162828875420951241622723087663915739806166 42.6875, 0.00000000001481592648728510435219140778656893209438381006787515915517366865 42.71875, 0.00000000001443383772930299450663813377754313353344749722522362827098875587 42.75, 0.00000000001406155000584137112267462347874081080488915640590580614545996124 42.78125, 0.00000000001369881334986140736592407617154371980802928418039309299340169076 42.8125, 0.00000000001334538413019371466457932394391520193255403983215979892146911416 42.84375, 0.0000000000130010248920142737822935325620646799630127443452418238004577032 42.875, 0.00000000001266550420130782086602857772508277064967107388102531696384679933 42.90625, 0.00000000001233859649321980346290544806932530121398035507446497397222251876 42.9375, 0.00000000001202008192420045258076877075724456678789793168472781435434129703 42.96875, 0.00000000001170974622784688874892027291174156703934909775195315235093617338 43.0, 0.00000000001140738057435149414459518061076306103010673839993595353635921212 43.03125, 0.00000000001111278143346704057959535594562918568152940889757060601104213585 43.0625, 0.00000000001082575044090126584923778377242781688582682310990078717603973432 43.09375, 0.00000000001054609426805573995923068166336285759231119973318664275359698033 43.125, 0.00000000001027362449502595936043643906096638648206435925871170935968594451 43.15625, 0.00000000001000815748678165280102932217140074637923037186858464053220923007 43.1875, 0.000000000009749514272448277984465352457745334121626484810358749591224250849 43.21875, 0.00000000000949752042761263510467081797014883746048566408894467077179545274 43.25, 0.000000000009252005959577422692896281480012467137721462179484692440207798779 43.28125, 0.000000000009012805195491414201676383162696903879126842863981372176092680933 43.3125, 0.000000000008779756673283741490775041729389327533915405536540988905559900925 43.34375, 0.000000000008552703035332534961610767490475265505518869554943524576664525381 43.375, 0.000000000008331490924799890578045657437528396273911884673223567467211645352 43.40625, 0.000000000008115970884566812454666473918853268723808120334843336641211504737 43.4375, 0.000000000007905997258703417105955480034075228584053699080236097546872534152 43.46875, 0.000000000007701428096411282823885080378078437984621805754283353452477369911 43.5, 0.000000000007502125058376385956564947226796069863800072475272337614623045255 43.53125, 0.000000000007307953325472586042523769714955769401467411171560850496937417798 43.5625, 0.000000000007118781509757104737277911043395239795839838127398087411622277681 43.59375, 0.000000000006934481567700890152176374969601014398763744932272892713268287051 43.625, 0.00000000000675492871559816948968091848553730922265444885069959019800966932 43.65625, 0.000000000006580001347100869562732804821870404103370912810307087224686977975 43.6875, 0.000000000006409580952824927766588603873986794008075331537298072083038249759 43.71875, 0.000000000006243552041976826147302736336127705782228272403760029139024702196 43.75, 0.000000000006081802065949959180106924998215624951165166323565211906727321636 43.78125, 0.000000000005924221343841692512350340710927456912592657838860803611153331652 43.8125, 0.000000000005770702989843185999787819295620448452822791831155259827294387134 43.84375, 0.000000000005621142842455240613953772561635176256688334154492040211688169623 43.875, 0.00000000000547543939548458594643360544008401819554009126771249458091945652 43.90625, 0.000000000005333493730776153789943047435562015320940114068885090166152313095 43.9375, 0.000000000005195209452637984326165166904919891401660764100050239077098563396 43.96875, 0.000000000005060492623916485469610265167867016414636714123864020690357459291 44.0, 0.000000000004929251703680813562503219690240425298950925539465746703168219537 44.03125, 0.000000000004801397486476165529211018193720162886933732055750928036090584872 44.0625, 0.000000000004676843043106769405917443548469407738611079700323369430094913645 44.09375, 0.000000000004555503662910332472988378531485681140248824064548854359815296264 44.125, 0.00000000000443729679748665462990326700603499617115540404260238587328891815 44.15625, 0.000000000004322142005844039747555808481917060500420639280042808399304107222 44.1875, 0.000000000004209960900928040077937964977108619326083140291472608170665011765 44.21875, 0.00000000000410067709749794895082530056935959800226999181011417024357702836 44.25, 0.000000000003994216161317315481835111794632747410140449108671879390348116897 44.28125, 0.000000000003890505559625592383853321221362978813347160618497136805209150674 44.3125, 0.000000000003789474612858844729318776660463139403300601902378599004342260813 44.34375, 0.000000000003691054447588244156790184091616414677045727293395417276804941907 44.375, 0.00000000000359517795064585004205189669803122312641017790980498226743877602 44.40625, 0.000000000003501779724407937040354504095089298314813294713473198659711295569 44.4375, 0.000000000003410796043206867619295050238999125836872249527792033313671024645 44.46875, 0.000000000003322164810843229197101955581302541233005425380753410300047912125 44.5, 0.000000000003235825519170658723474452239377923884637822887654967548499252939 44.53125, 0.000000000003151719207726463423662427294960062800463720233797315078975397921 44.5625, 0.000000000003069788424381815394697247773464443691127694650493839907824120714 44.59375, 0.000000000002989977186985950208895602645473333929431060020811443143120743418 44.625, 0.000000000002912230945979436047260458558471995704703466483077641041883977461 44.65625, 0.000000000002836496547952200547744230405727978619535671921151350383223629482 44.6875, 0.000000000002762722200122607894545351800892234611260215840642079650106787024 44.71875, 0.00000000000269085743571446906941228412596783701508906404290965387380045237 44.75, 0.000000000002620853080209443999988252765092109949129158964307769236886197075 44.78125, 0.000000000002552661218452855930350339567389537975916840874383352816543651667 44.8125, 0.00000000000248623516259148605323975380187229781909708835563373254762050838 44.84375, 0.000000000002421529420822450621771970433725685916428638229869867965312132585 44.875, 0.000000000002358499666932783732147939426424400608379791524044464476576655843 44.90625, 0.000000000002297102710609857061516178555285882582195084724001083924171229404 44.9375, 0.000000000002237296468503263372271828861978632908970823948602957301817060301 44.96875, 0.000000000002179039936019273863678446778707507867722564318901328816187660933 45.0, 0.000000000002122293159829450764245041725355944158954156979130456854880000587 45.03125, 0.000000000002067017211075456206975380910490182552514213673574141411157310232 45.0625, 0.00000000000201317415925254670050177669007887192362399147901230472277028048 45.09375, 0.000000000001960727046754679681347401953565273916421578832553261144235602143 45.125, 0.000000000001909639864064584978475491971153763905532311580605645448548393726 45.15625, 0.00000000000185987752557256980660920417836218009716386050027407062128642048 45.1875, 0.000000000001811405846008231388812824449942608693602435623275004521485762489 45.21875, 0.000000000001764191517469646744480861992024094282816259577711358601170383881 45.25, 0.000000000001718202087034994813002382068625887150109475757079362829024350822 45.28125, 0.000000000001673405934941942156766244491132947081996770599843040811231379584 45.3125, 0.000000000001629772253320490234802286742672806485542190511642708959638544212 45.34375, 0.000000000001587271025465339889449445444128174738762261170604646766387644374 45.375, 0.000000000001545873005634177466659527505140119404735483894415974705811525664 45.40625, 0.00000000000150554969935862711409403132725160915555907677638870621958937387 45.4375, 0.000000000001466273344254945482945502538047204705017308150513364458871187938 45.46875, 0.000000000001428016891321858507122090219330823611393743181742048030077460913 45.5, 0.000000000001390753986713255349720244094403714730874752829700000327208309116 45.53125, 0.000000000001354458953973762189282187338423207926919591702346583635263486635 45.5625, 0.00000000000131910677672551846007770178616239776212317122481493883907265648 45.59375, 0.000000000001284673081794770649745413729582811395142663282099854104613416403 45.625, 0.000000000001251134122767183977667983327980978317756562621846756155306808148 45.65625, 0.000000000001218466763961050407549278306082599616568550020272368185426909349 45.6875, 0.000000000001186648464807842662548907155712785741660786191557883817381117641 45.71875, 0.000000000001155657264629828381483395514248754906648758947461024143300245877 45.75, 0.000000000001125471767804716446333719794963882429823965894357421233559401578 45.78125, 0.00000000000109607112930755898685329062835088666403324665273837229827570064 45.8125, 0.000000000001067435040620377785731953661621266979519011747659531176666894676 45.84375, 0.000000000001039543716000222921954875330297927091693788418152333171857269901 45.875, 0.000000000001012377879096604651340181970603153053843859895192029402917774103 45.90625, 0.0000000000009859187499094668787022746608313473636281080373550766825800003105 45.9375, 0.0000000000009601480320790922690308247696883849621033388402302707755529385334 45.96875, 0.000000000000935047900499545215353177528871065724277964622837769075253633605 46.0, 0.0000000000009106009892474696649924309708121927481442792351083921436421443612 46.03125, 0.0000000000008867903798182643368374577739399677670921148222735444988209629024 46.0625, 0.0000000000008635995896618582698386177515064488394907032082225551088866906666 46.09375, 0.0000000000008410125610105050538882406844133379652729577192544336723343144186 46.125, 0.0000000000008190136499912046320899490265762100270777985314997614905144007168 46.15625, 0.0000000000007975876160155473486900129299462201150706641810275073173389642827 46.1875, 0.0000000000007767196114399560672077614096981414093675158007698079602781329048 46.21875, 0.0000000000007563951714894788132489586867220051070355479326741771117162161642 46.25, 0.0000000000007366002044384566179920596873810048684820713564385817711273200486 46.28125, 0.0000000000007173209820415591605344415098085624216524670966228012332628128295 46.3125, 0.0000000000006985441302088445366287516921077753533419803644376544130641823945 46.34375, 0.0000000000006802566199186591216707606120884376192141146991688178221801702339 46.375, 0.0000000000006624457583623491484130242335417141786109856522288998871365075331 46.40625, 0.0000000000006450991803149073835797988887199236736903020009431530856325409072 46.4375, 0.0000000000006282048397258262587280770690032505892540750018816262530693623067 46.46875, 0.000000000000611751001524573083349941731231613007054974384665743603540735343 46.5, 0.0000000000005957262336352436340453942720132295209883031163152422486628230435 46.53125, 0.0000000000005801193991950875620614432092449511438060168679154117654900947324 46.5625, 0.0000000000005649196489717327798427556137759143708372931294064165911802600512 46.59375, 0.0000000000005501164139740663605764702815287383690235780546263828807493108259 46.625, 0.0000000000005356993982518565960506766540831585796527707656206329077142345918 46.65625, 0.0000000000005216585718793247884523091508822124231558435891481475158914008369 46.6875, 0.0000000000005079841641179961799831827478209390646859509067771191062581083814 46.71875, 0.0000000000004946666567542772274062013522320221089333695695552855064284318809 46.75, 0.0000000000004816967776073212819845953085712765421498329545753956153321042426 46.78125, 0.0000000000004690654942028567120324069874783036091527146476575493317159029474 46.8125, 0.0000000000004567640076087606769363152242626521832456737507908231440541420646 46.84375, 0.0000000000004447837464282681977582288923096171489995896848476337777745969227 46.875, 0.0000000000004331163609468099383878044944161322066876904744571217038937233006 46.90625, 0.0000000000004217537174285732790109627788380913890709058334251288999509679513 46.9375, 0.0000000000004106878925589798950862200324896022891825551170851083743891722088 46.96875, 0.0000000000003999111680293692131715833107110893766742117725662857043794645587 47.0, 0.00000000000038941602526027086136097323594514902813570962859055883714395282 47.03125, 0.000000000000379195140259740626792462296954386264691039019213963686740448135 47.0625, 0.0000000000003692413786133235342217278830782000854158703992183184614736432536 47.09375, 0.0000000000003595477906022945251087516905532099944225169552949697741750717342 47.125, 0.0000000000003501076064469119017300748511702467354874195128668442778069084604 47.15625, 0.0000000000003409142316715012598138335202141607865678444113710891305248556797 47.1875, 0.0000000000003319612425882681190698566648602010977498958295288298152401226955 47.21875, 0.0000000000003232423818968159254131507340580576263322042429655716448500946281 47.25, 0.0000000000003147515543964225920403431118151645713673721391386484073729281797 47.28125, 0.0000000000003064828228082033179539576821356020518927315324758308822722656157 47.3125, 0.0000000000002984304037043601199627330068937134062103193106167640157179896643 47.34375, 0.0000000000002905886635417893843564403675627502004138967625812225934902906882 47.375, 0.0000000000002829521147973878329436203918934074782617608618449719032067619037 47.40625, 0.0000000000002755154122024646494054043136719634522144969492865817938830653873 47.4375, 0.0000000000002682733490737331693131034484439839893860735656951812715289271141 47.46875, 0.0000000000002612208537384195429634120324104993953535723129762398577193022728 47.5, 0.0000000000002543529860510881756379588493031846519289703246177424673300354537 47.53125, 0.0000000000002476649339998445752075398505379579306826712834673074243803597638 47.5625, 0.0000000000002411520103996355313935965962112028240289153096932718866883662064 47.59375, 0.0000000000002348096496704243527167043217621148485247457140119011409690751745 47.625, 0.0000000000002286334046980752335025858888153051749346792854277547360175387151 47.65625, 0.0000000000002226189437758357506545930612214217074635593961940301249330965928 47.6875, 0.0000000000002167620476243600337100553947274060924093515311408048498652029184 47.71875, 0.0000000000002110586064882673465691493538730755014903689859535107592580340832 47.75, 0.0000000000002055046173072816989537628824969916505759211298828891027363359809 47.78125, 0.0000000000002000961809600477030181093804323670954873107853114718494272374282 47.8125, 0.00000000000019482949957876623767415330088179170919744602111249158779962366 47.84375, 0.0000000000001897008739328406113985859037247189662867441866404100750943808946 47.875, 0.0000000000001847067008797698540628261997950314828394750455879597660430997409 47.90625, 0.0000000000001798434708815705494244757861230820162930648250107985973815032968 47.9375, 0.0000000000001751077655850522713500944968463183908041407874764476601728098061 47.96875, 0.0000000000001704962554643142368966554246960536217319905666478908447127649789 48.0, 0.000000000000166005697523872265651200703130019558771030547505350533077995597 48.03125, 0.0000000000001616329330608655641180348933185018402320257051005358417526996142 48.0625, 0.0000000000001573748854848322626844981844806183410854673560779474981890823983 48.09375, 0.0000000000001532285581935810463719301177826551242550614908074063419126166206 48.125, 0.0000000000001491910325037236641336531532868548736230053819667862311862008529 48.15625, 0.0000000000001452594656344695992219600979383983025662812372655729727705781891 48.1875, 0.00000000000014143108874331975883126740637958441962454002775310389106496571 48.21875, 0.0000000000001377032050123307179646144990682135581886428526881516617599609121 48.25, 0.0000000000001340731877836548528196576431022380668858664397252902271513949333 48.28125, 0.0000000000001305384787430946449408367280367968176958973195984871592266631739 48.3125, 0.000000000000127096586150441550381456913330766580981155914027160289067163383 48.34375, 0.0000000000001237450831154011290738798821109958687962656557819667437834684605 48.375, 0.0000000000001204816059179366389077900469373263174301719653367946491754863619 48.40625, 0.0000000000001173038523718930365474597107448669695682847633999600765730391394 48.4375, 0.0000000000001142095802307923121654485964521461391932424051159569680738912122 48.46875, 0.000000000000111196605634719336935342313688766069433261500880059108559365665 48.5, 0.0000000000001082628015972449387417524396512005196915954318681007115798219589 48.53125, 0.0000000000001054060965313597611039987359866023211718022018305023773049743505 48.5625, 0.0000000000001026244728134186202945098354231399653515795824417479129107612541 48.59375, 0.00000000000009991596538412057315064750268154471085354751486912266820494831743 48.625, 0.00000000000009727866038557475978963056713811216060551365574289881697433778953 48.65625, 0.00000000000009471069383352630758492367379042302316837116681445953805271497235 48.6875, 0.00000000000009221025032384019118773903553925561657109896796908332311727699007 48.71875, 0.00000000000008977556177236395352255345659945140275019206803654687258074873181 48.75, 0.00000000000008740490618731261960844090236785874016071143605895864354115041556 48.78125, 0.00000000000008509660647334099344008578580052462835784232276864972266650805615 48.8125, 0.0000000000000828490292664898323182667566598501791225434178435617447844578681 48.84375, 0.00000000000008066058379921315690633873478832444383332444636077727443485208206 48.875, 0.0000000000000785297207947141925148742716229393693244165055863108237606632736 48.90625, 0.00000000000007645493138983716094901434128133937177709641576391026453694520119 48.9375, 0.00000000000007443474608578136562829913207105950925021326192694600373418115612 48.96875, 0.00000000000007246773372592274821932248309593703048769998551316883412930454034 49.0, 0.00000000000007055250050004635500447336640737510174628008629247465110731386211 49.03125, 0.00000000000006868768897431094763459140488537266248043209708646756995700264107 49.0625, 0.00000000000006687197714628433746880861780853095756472439248281076203364410905 49.09375, 0.00000000000006510407752440492678780444969965689025197370878483061885326711191 49.125, 0.00000000000006338273623124141488845030264198450013278491516042327747789322446 49.15625, 0.00000000000006170673212993868326139194333383542891937992976409650401488083253 49.1875, 0.00000000000006007487597325352228019898194254311376024972499874617175728976471 49.21875, 0.00000000000005848600957459911238838552192391580856294879545915685412043887699 49.25, 0.00000000000005693900500053203569793679018439589457177338810224209938198953676 49.28125, 0.00000000000005543276378413007899741571241487497981449523536513560425505194695 49.3125, 0.00000000000005396621615872320595137102920383372538343016634459131670063195001 49.34375, 0.00000000000005253832031145383405847433648562138870368360807629911301842376537 49.375, 0.0000000000000511480616561559597931285081518435531623011939479719416160734219 49.40625, 0.00000000000004979445212505574212627190261562817366567483054629725261469054926 49.4375, 0.00000000000004847652947880888892597741039671510106132988401281258291886985871 49.46875, 0.00000000000004719335663440260098112896661915455544369999963681897659665696119 49.5, 0.00000000000004594402101046192276494300069094029499523999107628351990225952418 49.53125, 0.00000000000004472763388951213554670981913072446270777035219532012909478135464 49.5625, 0.00000000000004354332979676031485663368218539880801966079438169396720318063787 49.59375, 0.0000000000000423902658949703682012726931051194321615352776828262994196269323 49.625, 0.00000000000004126762139501677771636488791079363665457906148819111072183857566 49.65625, 0.00000000000004017459698171290334439413646992611615976324052220891118540955838 49.6875, 0.00000000000003911041425452006216942770490016597423048938017917140813372782347 49.71875, 0.00000000000003807431518275369558810636658857568028209472483130820301591594933 49.75, 0.00000000000003706556157491277472738720107941398945623950536535642464742110283 49.78125, 0.00000000000003608343456176818245286760640705997722550741593780549828780320413 49.8125, 0.00000000000003512723409285515379852848737936449374270458545072778253892895705 49.84375, 0.00000000000003419627844602396188201970894272480830259130110044559858552242229 49.875, 0.00000000000003328990374971190938587758514867572325171668876541860073074542824 49.90625, 0.00000000000003240746351760833236911926382587157259114202084567517219093174761 49.9375, 0.00000000000003154832819539274926220365045480203905119226885825793595254182433 49.96875, 0.00000000000003071188471923449898367819830438711272896997787919534955882792491 50.0, 0.00000000000002989753608575021365044244197245493202431846247566751784005780481 50.03125, 0.00000000000002910470093312326864966973426449747657412348330774476985765720329 50.0625, 0.00000000000002833281313309695107940686353808522216268101368949136184662910504 50.09375, 0.00000000000002758132139356049179658329078690917542858334952326492951312576757 50.125, 0.000000000000026849688871454321458234673091938022724184668796606546143667612 50.15625, 0.00000000000002613739279572794180274922985620594800502360687846926247886915215 50.1875, 0.0000000000000254439241000906546704968577305941381367226769940274214174260781 50.21875, 0.0000000000000247687870653020674669889264227263868668173893918128372094573443 50.25, 0.0000000000000241114989707557993714545377812848769399409258066742709030629537 50.28125, 0.00000000000002347158975511615192202173001338977072708115990147584158693795819 50.3125, 0.00000000000002284860168577368488888304997438380016214177920658967682423980466 50.34375, 0.00000000000002224208903689165769567264653108792729101845167365303466863484702 50.375, 0.00000000000002165161777582116207966906247133931069164154024417396131939887599 50.40625, 0.0000000000000210767652576684871049631768876665507073951274570076843417154154 50.4375, 0.00000000000002051711992780382687224749438153358978971521623399216680828993628 50.46875, 0.00000000000001997228103210586802097340598569524090914700040293763521307199509 50.5, 0.0000000000000194418583347420820170115675037727324166656351248289217592568502 50.53125, 0.00000000000001892547184328969979285694235049739337685310580355902896046982406 50.5625, 0.00000000000001842275154100736699985415877023196732843179496278221356950233562 50.59375, 0.00000000000001793333712607237029791805051815158383050344596060407122133402592 50.625, 0.00000000000001745687775760309201803547410281999803725306067011075801966817004 50.65625, 0.00000000000001699303180829099537404165678516527617663420473035407236038523927 50.6875, 0.00000000000001654146662347096827976300578596395820684408024076899388759384788 50.71875, 0.00000000000001610185828646326377403866522944787807179554872388577874588501333 50.75, 0.00000000000001567389139002457202121244955008272833675042139951763671636729335 50.78125, 0.00000000000001525725881374994571559297198631152403425530073416574360043819244 50.8125, 0.00000000000001485166150727138027948672720967339538732200959525101892756397429 50.84375, 0.00000000000001445680827910282523912526465590412463280339811250572404603127824 50.875, 0.00000000000001407241559098527625536002814235020121873492952377670678778580013 50.90625, 0.00000000000001369820735758937107317019271147207352718187126227135437119810408 50.9375, 0.00000000000001333391475143658966685511694871321237425668902634680688342629795 50.96875, 0.00000000000001297927601290374156320406511586406773208272604089490317768783135 51.0, 0.00000000000001263403626517891412740865296678766076126418853888743927000213457 51.03125, 0.00000000000001229794733404045683955341973372759492037012211725754109907762944 51.0625, 0.00000000000001197076757233389055735312837851878310812698881858002891691095235 51.09375, 0.00000000000001165226168902485967968275314961559091488157973781898068792130287 51.125, 0.00000000000001134220058270939116522011706184148903806611784579418302924775263 51.15625, 0.00000000000001104036117946578963603625699791428678330908272299899911259208714 51.1875, 0.00000000000001074652627493548436844616931632231645159948927821453626124758504 51.21875, 0.00000000000001046048438052305385182115923527423078499447646463297259646801534 51.25, 0.00000000000001018202957360848873835585945121120688047017966989582193674999356 51.28125, 0.00000000000000991096135166751632129775931781328264321119701403968722028009569 51.3125, 0.000000000000009647084490198501025783391098568743463030182867031684694033849048 51.34375, 0.000000000000009390208904357057587880932875210289577861482856195505431326751518 51.375, 0.000000000000009140149514202068400400596314016921545507162561663884761080813818 51.40625, 0.000000000000008896726113459285640337328934338103495034299083421618177013915783 51.4375, 0.000000000000008659763241711123940570435140092944073449274527804651046925799929 51.46875, 0.000000000000008429090059923612163159498446179021010737707897193820569062085581 51.5, 0.000000000000008204540229223774867200712665893205319175380453923718904138625804 51.53125, 0.000000000000007985951792842956894203204084586074019972480105884896197336369072 51.5625, 0.000000000000007773167061143789632299365008092154606370717068651066088273295467 51.59375, 0.000000000000007566032499650626442828507888022172648679625499111845465735089078 51.625, 0.00000000000000736439862000534887693614232781817775770944042304316342105590342 51.65625, 0.000000000000007168119873772466077260651317055972866077432544351066000147193501 51.6875, 0.000000000000006977054549019398516349016954519992162342661319259247395665450054 51.71875, 0.000000000000006791064669599755300232654723800598945583201682555133552257393478 51.75, 0.00000000000000661001589706928295982521404765269473223311143479090648125991485 51.78125, 0.000000000000006433777435165984228696461661370935528163798005350683379222399116 51.8125, 0.000000000000006262221936787678995382403457070039355834748240704224224894451358 51.84375, 0.000000000000006095225413402007622409297674104909525664275552753470249741624162 51.875, 0.000000000000005932667146825560312773905174917057747821824589202383701192587379 51.90625, 0.000000000000005774429603310456318080812666487442649811100390848016449471337135 51.9375, 0.00000000000000562039834987829463220082005658094965346272803664081064082640765 51.96875, 0.000000000000005470461972842954483191821395987382378820809638831078067100215648 52.0, 0.00000000000000532451199846524047973973355893587316635372911568668877765866757 52.03125, 0.000000000000005182442815683844715069075551065606492542782053977727872461386445 52.0625, 0.000000000000005044151600868537483471354784167267182151400319457594691624717813 52.09375, 0.000000000000004909538244542900499091805533214671580083833737691185612620283106 52.125, 0.000000000000004778505280025282575292061257206668601596025051508314220136337476 52.15625, 0.000000000000004650957813937988553392791936704146565771600726627576637068645186 52.1875, 0.0000000000000045268034585360087658695365705028044357012749936740542384109609 52.21875, 0.000000000000004405952265807859362047976593399662959973145753412855920517344209 52.25, 0.000000000000004288316663302334272474612922381102570302658037735664589982687109 52.28125, 0.000000000000004173811391636168277979320635539207850040978820300287506000786799 52.3125, 0.000000000000004062353443638778396207923760729613953369405790472902922103944276 52.34375, 0.000000000000003953862005091388396493480988801541378539873953456295115848757971 52.375, 0.000000000000003848258397018949477486951561173587481696900484949304005135786273 52.40625, 0.000000000000003745466019494349745352882542027163255610479326434316317657764313 52.4375, 0.000000000000003645410296915456848665536358583250089085976977929489872082871246 52.46875, 0.000000000000003548018624716562675756131000760951989604453105278417973977582519 52.5, 0.000000000000003453220317476797099208421141156440538864458577946978519222005128 52.53125, 0.000000000000003360946558389050040692985217763211425860921179462503871561588866 52.5625, 0.000000000000003271130350053888290207624770533128590456996755071646851029008382 52.59375, 0.000000000000003183706466563876192952779716631313622892551721253109702535436857 52.625, 0.000000000000003098611406844608143910655345812106201638966093475655184845631526 52.65625, 0.000000000000003015783349219636418021181871286783184181905975366508929623005866 52.6875, 0.000000000000002935162107167330810284461718466272653530033840108577716089185391 52.71875, 0.000000000000002856689086238537447514946508871515050443228418349900138342063435 52.75, 0.000000000000002780307242104713529281912075388772486407629554066002301727394133 52.78125, 0.000000000000002705961039707003212734774588177617847894233109134853944056984715 52.8125, 0.000000000000002633596413477487913326467686881380968828555378854099855467131752 52.84375, 0.000000000000002563160728604592475924286666944069162419493711826285474613514364 52.875, 0.000000000000002494602743315357490011389384728983569384140950343512766011489493 52.90625, 0.000000000000002427872572147997977116567305843534838764244985392587897545787754 52.9375, 0.000000000000002362921650188860253988003304348908788577988462420859591141207138 52.96875, 0.000000000000002299702698248562444636178213218726273786844867890935779228344865 53.0, 0.000000000000002238169688952760339380584862937362697703972759971455665162642014 53.03125, 0.000000000000002178277813723619528802636531883507678262530386693128334190043006 53.0625, 0.000000000000002119983450628697412881394565891482442787751095912155800201423833 53.09375, 0.00000000000000206324413307454522720088902361787391976856452691590442409227449 53.125, 0.000000000000002008018519322931054668005997559660034973177876897801007426109626 53.15625, 0.00000000000000195426636280816030771543252182172121519254512111636482074221884 53.1875, 0.000000000000001901948483234530767150724963625598642913096191130930318125998219 53.21875, 0.000000000000001851026738433505334198892916173425295798551747976180254979236634 53.25, 0.000000000000001801463996960717566561400156690612579141864970047050339726969434 53.28125, 0.000000000000001753224111413443192560608382030269721074271262004012617485102449 53.3125, 0.00000000000000170627189244967548537375655750272953748621788494544492503835025 53.34375, 0.000000000000001660573083490433978578500930603899702736753100089058869189207921 53.375, 0.0000000000000016160943360874148524350046699642048699183251108956066506111264 53.40625, 0.000000000000001572803185938557746554729750190408508638200142391969576238100022 53.4375, 0.000000000000001530668029534558079446628111691495976287305919863963228904145323 53.46875, 0.000000000000001489658101419796491251958752687669734882659084649747400008685156 53.5, 0.000000000000001449743452051588077125450752603657083633592429257060205665880218 53.53125, 0.000000000000001410894926242073941724969785012346186637248584419974207301526133 53.5625, 0.000000000000001373084142167486569051649388127716394095113595829496078312991936 53.59375, 0.000000000000001336283470929918847934862992912980403327823444553546822497238651 53.625, 0.000000000000001300466016657114596047957247374439600423224864541777726693889636 53.65625, 0.000000000000001265605597126176351691778861890813292054914841332658951672982916 53.6875, 0.000000000000001231676724897454313048021341311677538712542784967879209113217462 53.71875, 0.00000000000000119865458894523885284688007690367233754701516638469201245080057 53.75, 0.000000000000001166515036772228269557230246787807627729690428180334383486957365 53.78125, 0.000000000000001135234556995083595083138473644648995385008625982251585448500817 53.8125, 0.000000000000001104790262388713598137773594830531396516451405111465679682300435 53.84375, 0.00000000000000107515987337725583053190858404988322572700691249634562254788494 53.875, 0.000000000000001046321701960033883248072561710757787807230009813202312059943054 53.90625, 0.000000000000001018254636061077167333848743124666927306639559583954861246276999 53.9375, 0.0000000000000009909381242910877227181016591615997733388580126853643162835757794 53.96875, 0.0000000000000009643521611110289919725522505085283713343005096052688107137585072 54.0, 0.0000000000000009384772723867943764489532044292149196138963473941832590468314005 54.03125, 0.0000000000000009132945013246889145959528704294196805772630070332849288963103201 54.0625, 0.0000000000000008887853947777257770444823334977432467393521670217414189856306449 54.09375, 0.000000000000000864931989913000645787959613082603760644284537470813694088000064 54.125, 0.0000000000000008417168012306616162379895138000908922024657764706546107832471908 54.15625, 0.0000000000000008191228079252402072164896090808160494401543995333200418845740336 54.1875, 0.0000000000000007971334415803505566241730973277486224123543937193584652389802944 54.21875, 0.0000000000000007757325741879990867560619693471151244004174536981256812272296123 54.25, 0.0000000000000007549045064839760058635137793971459700037948766407916680212074887 54.28125, 0.0000000000000007346339565910231302391125197426704825357406541056116295025203456 54.3125, 0.0000000000000007149060489616898183868797219917451160662943615629166325908851427 54.34375, 0.0000000000000006957063036130004563127647840519398622231077315289035235596547445 54.375, 0.0000000000000006770206256452630673245279927263577544002093103739927899343586849 54.40625, 0.0000000000000006588352950375493838788017269028769119324196764558753649481130416 54.4375, 0.0000000000000006411369567125722521837627475531816127085823814655284013348894614 54.46875, 0.0000000000000006239126108638766780950297999472297930892777931376370126451096749 54.5, 0.0000000000000006071496035384462974678747897278307946248200766774000620409912283 54.53125, 0.0000000000000005908356174680076942793133827988106948860739451906681492278414317 54.5625, 0.0000000000000005749586631424909209162480204405152536518275700138028583695961586 54.59375, 0.000000000000000559507070119275919211346791019701744129471566600435679686704395 54.625, 0.0000000000000005444694785620214171142354117941839953615677004303761794413429002 54.65625, 0.0000000000000005298348310030354002569826965106726194283984757563541518378670628 54.6875, 0.0000000000000005155923643233045430614185158633377493881762153230149339392563843 54.71875, 0.0000000000000005017316019444541404765653244962420660086057377721721408028398846 54.75, 0.0000000000000004882423462270602161206991170778959242978039852515554138855130737 54.78125, 0.0000000000000004751146710698816999588613813786509806996070893376799303118574798 54.8125, 0.000000000000000462338914704722970399991300116374211855573195432973190710497879 54.84375, 0.000000000000000449905672681775740946908698530539993617821735909704632616863395 54.875, 0.0000000000000004378057910404243368150625803440271111848513598005157667308742895 54.90625, 0.0000000000000004260303596606299462952271801131435676514866843161078958878786349 54.9375, 0.0000000000000004145707057901375366833176710690918778550439506206052862937306461 54.96875, 0.000000000000000403418387742873884581023978290900818488265457501769487272971844 55.0, 0.0000000000000003925651887640266722204037677658029010523170207657344178505489617 55.03125, 0.0000000000000003820031110574129298722200807675219039588942519468363808448084442 55.0625, 0.0000000000000003717243699708603418596669215299987268481951878243571646875131346 55.09375, 0.0000000000000003617213883354371605802776328126032428385770491578665867653477416 55.125, 0.0000000000000003519867909544757675243063668351790890050966356726571441819535959 55.15625, 0.0000000000000003425133992384413588272297215257412238575946591746795908554897551 55.1875, 0.000000000000000333294225981800889693346518848891828420737331613649158005995784 55.21875, 0.0000000000000003243224702781483594402616939976358353847263614304131015375590699 55.25, 0.0000000000000003155915125699408274307130096790325307971401970558471690611151892 55.28125, 0.0000000000000003070949098292952884442369742172292076663846575027108614248406006 55.3125, 0.0000000000000002988263908663897709841787138433214979060832151318175052374326497 55.34375, 0.0000000000000002907798517621028187710309315466747835218921078549277184329283939 55.375, 0.0000000000000002829493514216139377090535740594897879794346718658480457852022785 55.40625, 0.0000000000000002753291072457736997535798243497094236203177067001439659083498498 55.4375, 0.0000000000000002679134909171360515737050707706775939663953974252996229816603178 55.46875, 0.0000000000000002606970242976270383611567432545401935288295742466933397343664425 55.5, 0.0000000000000002536743754349036787228689715456200246526340807322856948116205211 55.53125, 0.0000000000000002468403546745341709729800493121339437185406372835870936145803905 55.5625, 0.0000000000000002401899108752060285276869122997257496096158196965517818554786346 55.59375, 0.0000000000000002337181277242421853160982546328211256542557610913634943051105027 55.625, 0.0000000000000002274202201507766325950599919500589450627090252096002718290335283 55.65625, 0.0000000000000002212915308340107964050015636649879901618544993711638845582430238 55.6875, 0.0000000000000002153275268040396889366857593676658787763714502099111682382086484 55.71875, 0.0000000000000002095237961328029148387850234936598729755222255933512040112355708 55.75, 0.0000000000000002038760447127799312935011031444831813326750682065097747417531338 55.78125, 0.0000000000000001983800931211115936304734656979097528761691309768324473294258343 55.8125, 0.0000000000000001930318735668910102759151163583096620075462574106544935719100128 55.84375, 0.0000000000000001878274269194261247418706357469014379000212228234058918744689463 55.875, 0.0000000000000001827628998153342798364206559434132562752922181502044941082131688 55.90625, 0.0000000000000001778345418423853409240691739431832799938077180767411148366461803 55.9375, 0.0000000000000001730387027980648004367047147209794528801478177863038675195822659 55.96875, 0.0000000000000001683718300208816934526659125293793815578504905779302477116710813 56.0, 0.0000000000000001638304657924981615473251237820034847002435670912230883968763666 56.03125, 0.0000000000000001594112448088081458213562296896111944613057783642196695618698585 56.0625, 0.0000000000000001551108917181420056311554136914527850916586765875348397656796742 56.09375, 0.0000000000000001509262187248218817535113059439878030822621963594322177812157616 56.125, 0.0000000000000001468541232563393852863516189566083474035374812335314159378143547 56.15625, 0.000000000000000142891585692472729414302879016832162106771353137806752766070705 56.1875, 0.0000000000000001390356671547047622919564408140307091498499001247215899984091749 56.21875, 0.000000000000000135283507354346536926310318021736140125200566068908509304379398 56.25, 0.0000000000000001316323224978130984686722666790352214449586384499372597713615961 56.28125, 0.0000000000000001280794032475391103608205774396466071490705977797091366831033148 56.3125, 0.0000000000000001246221127370618071559650785602384570452898271959320087339595789 56.34375, 0.0000000000000001212578846388375816375785696531332983658541412667562275803024837 56.375, 0.0000000000000001179842212833963144434413039941848069312626874982593596107995691 56.40625, 0.0000000000000001147986918284743624115475791257716783811525876210283252308524564 56.4375, 0.0000000000000001116989304768029632489028552494564949569985236452787388550050789 56.46875, 0.0000000000000001086826347412637141584292507871887215178637910746165313575672078 56.5, 0.0000000000000001057475637561567653601187280726635051272505915727654508296193311 56.53125, 0.0000000000000001028915366333604599863696856325475429564480114649756033525029852 56.5625, 0.0000000000000001001124308621933729700751308361043186836409497569206503448524598 56.59375, 0.00000000000000009740818075182107602144807929151622261830231165275539180014668296 56.625, 0.00000000000000009477677591508050575746209398429221776378232027318206461260672951 56.65625, 0.0000000000000000922162597926245590809295094789280214370900636664288236239460663 56.6875, 0.00000000000000008972472821631850501745363505356811989422168784714311492578353428 56.71875, 0.00000000000000008730032801084800587319596003983781595093046666784550582603074855 56.75, 0.00000000000000008494125563252600270681243931277722404109872094249760770234247386 56.78125, 0.0000000000000000826457558443124601278715662586472626496502452746544872354891613 56.8125, 0.00000000000000008041212042608700224849616772469645540231613226838927830553849826 56.84375, 0.00000000000000007823868691923982363900393379914295242496413658549913335391066278 56.875, 0.00000000000000007612383740467094430608206465151763349817266309709414610706302964 56.90625, 0.00000000000000007406599731331191349008484353060750257587334677573636083623142573 56.9375, 0.00000000000000007206363426830747047752830921936811562822164633964078086528169895 56.96875, 0.00000000000000007011525695801745821330317662332993382896731150686579395781522997 57.0, 0.00000000000000006821941403902147340996881954697169935234928093679101713360608678 57.03125, 0.00000000000000006637469306833034080327934190707491771697964641854250861164066702 57.0625, 0.00000000000000006457971946402953449842392421624991690843923841296304328574577537 57.09375, 0.00000000000000006283315549360015089562228497849695240227505156423199403081724037 57.125, 0.00000000000000006113369928918297999304360314409247094311417926084755599626696778 57.15625, 0.00000000000000005948008388907063907701260243706568996698538590338647536391447009 57.1875, 0.00000000000000005787107630473163869262616575806464596083136898582542140029207605 57.21875, 0.00000000000000005630547661268865875130732751957351050802517445973246861370598137 57.25, 0.00000000000000005478211707059123573447114557864051832928153616711984482661997764 57.28125, 0.00000000000000005329986125684051291238646426446675120838946577696732556651881164 57.3125, 0.00000000000000005185760323314069670038524065781414460105557386710254452543492712 57.34375, 0.00000000000000005045426672936840578320400025491549752581528485746489414436180369 57.375, 0.0000000000000000490888043501672071997669340897513270746569204103588268566327549 57.40625, 0.00000000000000004776019680269031663557516560069463119000462703594294534348003424 57.4375, 0.00000000000000004646745214492970985958059298289325742762961525850900370610296088 57.46875, 0.00000000000000004520960505408475941738814905442437829492819759769175076907587708 57.5, 0.00000000000000004398571611443798591967592686080345224998295632649006133215730985 57.53125, 0.00000000000000004279487112421960683448897581392870984699376892100851524993758637 57.5625, 0.00000000000000004163618042095628762618737853490687677206230757597340868320322241 57.59375, 0.00000000000000004050877822481286014194644497721923048781279406499412158754368614 57.625, 0.00000000000000003941182199944878083326470579528834233456462732272659111463133347 57.65625, 0.00000000000000003834449182992376597083768914974856729204914408863142496338494925 57.6875, 0.00000000000000003730598981719937148023112518417334933177611277497276263131363846 57.71875, 0.00000000000000003629553948879529017109206692351517283388054807278486907318595977 57.75, 0.00000000000000003531238522517082750259181639638533016959652173009117634727067468 57.78125, 0.00000000000000003435579170141339694716699702918884187834108352848043365550685912 57.8125, 0.00000000000000003342504334382695559035088218804488614404982545097036804521687131 57.84375, 0.00000000000000003251944380102408773134590384654751682347536155032133466257907977 57.875, 0.00000000000000003163831542913594661512995987890814057006657682289421714937507084 57.90625, 0.00000000000000003078099879076448951921703414039431483607809517320234601717059901 57.9375, 0.00000000000000002994685216731139652760735689802832391123111552749716692556748471 57.96875, 0.00000000000000002913525108432775555096961774540365674296946856841734739184361132 58.0, 0.00000000000000002834558784953803240978386792855253351472066681364014321358042377 58.03125, 0.00000000000000002757727110320103181905183253594429335813251557490590707229524411 58.0625, 0.00000000000000002682972538047949945591198167899179145764868376521312079892690441 58.09375, 0.00000000000000002610239068549872333998608511047515850822516568052065717909568354 58.125, 0.00000000000000002539472207678297072816275008992635912992959339387863242526170986 58.15625, 0.00000000000000002470618926376685067550932918006064400979026169140798563429849023 58.1875, 0.00000000000000002403627621408672823815072887365479567746592492909512951852810032 58.21875, 0.00000000000000002338448077136513973379170171415870516207765820245877141257291227 58.25, 0.00000000000000002275031428320877512212493085114980021749724888837264769784304193 58.28125, 0.00000000000000002213330123914800886513143064901827189759648889377465425873135163 58.3125, 0.00000000000000002153297891825317987797230295124154329266604983760290718688237866 58.34375, 0.00000000000000002094889704616984954750612654515796449923669092737995996874646078 58.375, 0.00000000000000002038061746132210930477207800291363177064095965074792205097113324 58.40625, 0.0000000000000000198277137900396707855311431674728383153932947637917318875843205 58.4375, 0.00000000000000001928977113037095696622305327129162527656502302159864881537793835 58.46875, 0.0000000000000000187663857443507264634854103985983768521015751293074177070176604 58.5, 0.0000000000000000182571647584969099539144677045292926151835819380261886848784208 58.53125, 0.00000000000000001776172587231732180863609057749888340379010391719740911002277072 58.5625, 0.00000000000000001727969707461273583060221428495110382623708034187356461712657712 58.59375, 0.00000000000000001681071636736848560494201774291029009912025566299297890993754945 58.625, 0.00000000000000001635443149703227148823461602316346708668388729404974537978600273 58.65625, 0.00000000000000001591049969298123162859809002239511500297481149735108379497646279 58.6875, 0.00000000000000001547858741298656747847360586829976137123189746151985728541959568 58.71875, 0.0000000000000000150583700954891087882034450366053507509217175771056219032378007 58.75, 0.00000000000000001464953191850416270469824886847053621779827503466367366312304835 58.78125, 0.00000000000000001425176556497881990343852046782321479677141711221196668710623768 58.8125, 0.00000000000000001386477199442959111808127185139762557898691218483520169740081271 58.84375, 0.00000000000000001348826022069282337335763803166108378780967599415873775702406585 58.875, 0.00000000000000001312194709562479995843492212653504793734116226717907797625381639 58.90625, 0.00000000000000001276555709859276489784966744347956000145109720411964718499083005 58.9375, 0.00000000000000001241882213160233449981706879156108692219190459993319119067453588 58.96875, 0.00000000000000001208148131991086851965309077735794068151625684705354320306791796 59.0, 0.00000000000000001175328081798037452196453200660411530753636395258036554933307993 59.03125, 0.00000000000000001143397362062741403429942220995984636813029290231907099080107624 59.0625, 0.00000000000000001112331937923127085664292092971076925144185075528282607370744498 59.09375, 0.0000000000000000108210842228653331514155205117396561862630926225277641241607321 59.125, 0.00000000000000001052704058422023434171732039932468915929450940019298349619932923 59.15625, 0.00000000000000001024096703019079597589133337142926904251522981446585246909289899 59.1875, 0.000000000000000009962648097002221090495760582405135205898169665107969880437965493 59.21875, 0.000000000000000009691874129754301671724709119317922147990430521542509949257290349 59.25, 0.000000000000000009428441126265630962977072370686064519797037957166261555411904771 59.28125, 0.00000000000000000917215058510295291664469563537350687649574933064751663180419955 59.3125, 0.0000000000000000089228093576838393031411286109659268429205709256730541829551193 59.34375, 0.000000000000000008680229504343862072057995116975151517766111621371789748827322028 59.375, 0.000000000000000008444228154262326653483397181523578755566606162052689998412573291 59.40625, 0.000000000000000008214627369143453079860740215019255882502166269989688938713821751 59.4375, 0.000000000000000007991254010552638133250491239594690004642263266668352337341294959 59.46875, 0.000000000000000007773939610810105158895097128688226687962331320556026559245939771 59.5, 0.000000000000000007562520247346850660865487412848264884731174141282834075226858112 59.53125, 0.000000000000000007356836420430330185813577008960055836799325664567355336997943773 59.5625, 0.000000000000000007156732934169792133567968659886506304148698804168233378777899045 59.59375, 0.000000000000000006962058780713568787456835779357645046942867801908687387673607471 59.625, 0.000000000000000006772667027552970764920805313563286964330800552886983882945682557 59.65625, 0.000000000000000006588414707849705936648157215936723216481410717614683600267984228 59.6875, 0.000000000000000006409162713705958292141511355375544636930673768345089085239358146 59.71875, 0.000000000000000006234775692298417840050458719023546550168272409644132051528291857 59.75, 0.000000000000000006065121944799650979379567013417921810155491641205866348505539151 59.78125, 0.00000000000000000590007332801224337837341424664853526425060884642099078737192333 59.8125, 0.000000000000000005739505158643135727100868406735867134670008389149401130806888788 59.84375, 0.000000000000000005583296120147508224213420835092359448305153299005025249091347772 59.875, 0.000000000000000005431328172073453716854915180649409653554795981536600533668771271 59.90625, 0.000000000000000005283486461840513397179582627295301424105160743977554986204803636 59.9375, 0.000000000000000005139659238886934195397076932775839949308063902724972868022025862 59.96875, 0.00000000000000000499973777112224478873405197545617986979662844217335394195080729 60.0, 0.000000000000000004863616263623438725149170447039125364304861984166782660964072795 60.03125, 0.000000000000000004731191779514699763865154032833332362190558037034091675890372305 60.0625, 0.00000000000000000460236416297220735329737130208172864520858253704208236047573678 60.09375, 0.000000000000000004477035964297120360828868527342924334909541154112845991841119521 60.125, 0.000000000000000004355112367001355867554065210334360507780267513845378644310868203 60.15625, 0.000000000000000004236501116852258144221797412026542484954922104504493362669216239 60.1875, 0.000000000000000004121112452823691902757850896421738732109373418356083814589030552 60.21875, 0.000000000000000004008859039902494613276298332588452939984194536382190781589225024 60.25, 0.000000000000000003899655903700586104213685206912835124267438928732342348513400642 60.28125, 0.000000000000000003793420366824360811157177478671302668688132839710359093509387546 60.3125, 0.000000000000000003690071986954279870009757760941699431046584734130119825483286626 60.34375, 0.000000000000000003589532496588837698857184382867337082104824305300717988714872911 60.375, 0.000000000000000003491725744408301692048054124077772434548798013777305478717865476 60.40625, 0.000000000000000003396577638214815047256782610956202820463200496108305305288315401 60.4375, 0.000000000000000003304016089406612425901840730049461826942231437452482843262456744 60.46875, 0.000000000000000003213970958945226950632967543364797474381540738837135165698192302 60.5, 0.000000000000000003126374004775665789839799813112565312824231974411279066245818073 60.53125, 0.000000000000000003041158830660601065777886337776892129320010426457970816413834381 60.5625, 0.000000000000000002958260836390663826382830370733693238825994503142274922943847816 60.59375, 0.000000000000000002877617169333942097049766214878666662887806820751820170728596442 60.625, 0.000000000000000002799166677288770313508452852299880546574321086860301878172956582 60.65625, 0.00000000000000000272284986260485744687968109765889665656822482231917376219292227 60.6875, 0.000000000000000002648608837538735564565470331846094201971338051221040214053512494 60.71875, 0.000000000000000002576387280810420104865079450900850569276941831239176937086928544 60.75, 0.000000000000000002506130395329058440220928544843762530368307091395954580444917989 60.78125, 0.000000000000000002437784867056205007394456942434840886713082839419286381302066036 60.8125, 0.000000000000000002371298824976200019235455804691184631001173894390341547621014438 60.84375, 0.000000000000000002306621802143945152044348902202355283314294731182051067152582381 60.875, 0.000000000000000002243704697781164218698151724353339199358849320806643550390876843 60.90625, 0.000000000000000002182499740393010268863850834111835170205209720190307525896657514 60.9375, 0.000000000000000002122960451877633366601991624575683126523342361093528258633616404 60.96875, 0.00000000000000000206504161260205603041390010557371613277469749910629782147133318 61.0, 0.000000000000000002008699227418416514747867595333180159245478886401402008696920211 61.03125, 0.000000000000000001953890492595334284469113461082109875748114260419870028027411678 61.0625, 0.000000000000000001900573763639827690380849521687809168489832430008109711414583599 61.09375, 0.000000000000000001848708523985871486736557706545387591319358757244467494027430984 61.125, 0.000000000000000001798255354526321919948904500147888224705573892577219410224603066 61.15625, 0.000000000000000001749175903965560127829674118852718265511858705645067782820910957 61.1875, 0.000000000000000001701432859970810974782563467360514414665344316164949169641634431 61.21875, 0.000000000000000001654989921100684652481292165128024651382146079676127405799349886 61.25, 0.000000000000000001609811769490062828051164809546855564051700651509143179350866592 61.28125, 0.000000000000000001565864044271010241581325865580928346933253115846276183876275629 61.3125, 0.000000000000000001523113315709936849773254036329261271462642999702409248516580665 61.34375, 0.000000000000000001481527060041765279714878177564150417402270998298881655702577625 61.375, 0.000000000000000001441073634982373882671324904135162782406330630939609416845976269 61.40625, 0.000000000000000001401722255901087438668824130555207325437795610707337138687172798 61.4375, 0.000000000000000001363442972635475924808213230165739853848271444406758367038133245 61.46875, 0.000000000000000001326206646931197080257186233933846505677060759360404488023853748 61.5, 0.000000000000000001289984930490081125858744060026102544484460667418788434157072718 61.53125, 0.000000000000000001254750243610106264174235381822229793002981338543394679009742008 61.5625, 0.000000000000000001220475754401351825507379260907519783215615020815592870329690313 61.59375, 0.000000000000000001187135358562442457259413624868710581256153870146332608511490662 61.625, 0.000000000000000001154703659702411889578525050679618409838911903437342706736661788 61.65625, 0.000000000000000001123155950193318853151830428488634048644185635365702372530765225 61.6875, 0.000000000000000001092468192539340970556188515704512528051852875235519945448659169 61.71875, 0.000000000000000001062617001248455178406601012293616882438453338074217426999928812 61.75, 0.000000000000000001033579625193185743557539617366243336124569303512769933303115763 61.78125, 0.000000000000000001005333930447263485332656436181582709549111988894491375094099455 61.8125, 0.0000000000000000009778583835853926724577514117148069524710406151492608387788192599 61.84375, 0.0000000000000000009511320354336654862839357696300957500027502608697796418102608904 61.875, 0.0000000000000000009251345052584981823898615309561746436137961279305603843760012036 61.90625, 0.0000000000000000008998459653822883854461178118263410723548644640807561154178956538 61.9375, 0.0000000000000000008752471262143095555156517051600911067160813036303100341002031003 61.96875, 0.0000000000000000008513192216856667996292978871037613242984303291518675797443835188 62.0, 0.0000000000000000008280439950774380962342480355696917420075428839928994514994310246 62.03125, 0.0000000000000000008054036852314168716886991062511632265077159998034260466642777187 62.0625, 0.0000000000000000007833810131331559312539026342594686564218683657324612466851526932 62.09375, 0.0000000000000000007619591688572892102185848207489795533585518536448110393381407324 62.125, 0.0000000000000000007411217988653768765511116936682557496986145150677455716192122095 62.15625, 0.0000000000000000007208529936467811820968922948430528260699582729715726046529614031 62.1875, 0.0000000000000000007011372756933353168641668171367578342499541412806507344200382973 62.21875, 0.0000000000000000006819595877988155573079394347081067881462767811290472848689754096 62.25, 0.0000000000000000006633052816744683966975894295972756829433124465453817095311291316 62.28125, 0.0000000000000000006451601068720792807692203575793368465088527795041758497225051186 62.3125, 0.0000000000000000006275102000062982173402038719457050079995510988479604705350699956 62.34375, 0.0000000000000000006103420742681600522370452932186011443363370555229093019391979739 62.375, 0.0000000000000000005936426092219537691317943710188240083553126193040508866530006123 62.40625, 0.0000000000000000005773990408778059367249080727785109716644560179213402454883963081 62.4375, 0.0000000000000000005615989520325485479520506754158766917165113312678534776625409558 62.46875, 0.0000000000000000005462302628716411232093435462028985443985401916896392769138089007 62.5, 0.0000000000000000005312812218251112296579912773530701151085122926703987890178436145 62.53125, 0.0000000000000000005167403966706666442545933492723707516944007319700104712381441779 62.5625, 0.0000000000000000005025966658773163982284767129600553438065378610914334377133336346 62.59375, 0.0000000000000000004888392101830170205645442834257587584915960486488262831951864798 62.625, 0.0000000000000000004754575044000345793217908221116790510170783932935736104479725883 62.65625, 0.0000000000000000004624413094418827304914977726477781275653661786569134916619363008 62.6875, 0.0000000000000000004497806645658620493311795623374062499246455314936556580625289103 62.71875, 0.000000000000000000437465879825386560135395117907867189969024719464984983321770694 62.75, 0.0000000000000000004254875287264397154242219872800437129041068447976131374660713853 62.78125, 0.000000000000000000413836441082654219598833684654809280483780783524958093252972802 62.8125, 0.0000000000000000004025036960636581572217013924548586788360955139752548860800241999 62.84375, 0.0000000000000000003914806154314739812343954640719485572487839782761504129582926328 62.875, 0.0000000000000000003807587569598971477333347292059113657068580930503758318393287918 62.90625, 0.0000000000000000003703299080319176546613314436775658238440264655137098319095259791 62.9375, 0.0000000000000000003601860794103805524672996741879763548592628977240632437867866917 62.96875, 0.0000000000000000003503194991772107432866470271865442675792033591619652004165070668 63.0, 0.0000000000000000003407226068366531667420891439496060309640472261923584042905695929 63.03125, 0.0000000000000000003313880475781018777623949669538336976857590862181762060795838994 63.0625, 0.0000000000000000003223086666942106450978087582829188795418154448178281009769056098 63.09375, 0.0000000000000000003134775041500936263035705343238933677987789583884641528028877678 63.125, 0.0000000000000000003048877892995374913539597551032938786655163562160772744475178426 63.15625, 0.0000000000000000002965329357442561559466123716761809410445568660707460562451530753 63.1875, 0.0000000000000000002884065363323261279501458799236571602445459681831671341047173544 63.21875, 0.0000000000000000002805023582920444451675861992105577348334520995056800301841669094 63.25, 0.0000000000000000002728143384975523663618930938304206612242997274326932020681956915 63.28125, 0.0000000000000000002653365788626664450000071134313543295752837942631125586287908017 63.3125, 0.0000000000000000002580633418594544391084587791874818347165141258866387737542174371 63.34375, 0.0000000000000000002509890461581867617479848119777467395073352538471236599532640971 63.375, 0.000000000000000000244108262385384923770744479349588080166848058442531635422481801 63.40625, 0.0000000000000000002374157089967767307484141187662108129585080504350648146398213953 63.4375, 0.0000000000000000002309062482620539344911293573472852463195016639469897845048479012 63.46875, 0.0000000000000000002245748823584116699136384855166479925421198371566936212046497277 63.5, 0.0000000000000000002184167495699303919493383352220227585024594592101027116443117246 63.53125, 0.0000000000000000002124271205899402249188757123949434931749460853612215203721839003 63.5625, 0.0000000000000000002066013949235847067759500027582057030712186687845258263702102274 63.59375, 0.0000000000000000002009350973878759099642366204460877615130450033710962811864180062 63.625, 0.000000000000000000195423874706605904690085159060565821929524439469841873882804984 63.65625, 0.0000000000000000001900634921975505532291496974258102577470012589949976358350460427 63.6875, 0.0000000000000000001848498305494707379833012774686448466321259454959991400619949837 63.71875, 0.0000000000000000001797788826864833825259291687535034236888065951348826775452676613 63.75, 0.0000000000000000001748467507174400735922269576577315170945732340264642895258276461 63.78125, 0.000000000000000000170049642968014781330092447379957400083660209290434660346736466 63.8125, 0.0000000000000000001653838710932641522766836756840182926155900929065140733869846759 63.84375, 0.000000000000000000160845847268484160355775834496032040290575467002099145149261316 63.875, 0.0000000000000000001564320814562455903661683528874302388671103229223412491534810578 63.90625, 0.0000000000000000001521391787475479394224573885638267663782492960269196828740649987 63.9375, 0.000000000000000000147963836775086896925899219929575516674262043583087164648210452 63.96875, 0.0000000000000000001439028431966846440635840133167399639489406809557564735058880279 64.0, 0.0000000000000000001399530732469848396369259699137212113361609655517681831654183368 64.03125, 0.0000000000000000001361114873555653692125465705706736422937793591406442416509788323 64.0625, 0.0000000000000000001323751288296717671342924674895375900637087633522863815684599879 64.09375, 0.0000000000000000001287411215998227127837947830107813727543299144566371271306581336 64.125, 0.0000000000000000001252066680265861895901853820986060083295871570545801092549353914 64.15625, 0.0000000000000000001217690467668708126673409668087346587982547055384642837532490047 64.1875, 0.0000000000000000001184256106981215126634139373410913968963027867967696316242656142 64.21875, 0.0000000000000000001151737848988522425955430363163239691213251614056040819734289317 64.25, 0.0000000000000000001120110646839906833789377557264627156437117832231229974017077072 64.28125, 0.0000000000000000001089350136935510938473630374127872350269171891143599294521414218 64.3125, 0.0000000000000000001059432620331915128647316087606195597582931030899570469613700066 64.34375, 0.0000000000000000001030335044652505043907059763319896483690994382462804835670513788 64.375, 0.00000000000000000010020349864889657003673712276029760751706061465501068216049588 64.40625, 0.00000000000000000009745106342806026590801227360807036238465013640888864858117021731 64.4375, 0.00000000000000000009477407716585497879257182920832942544906914071399018726291225054 64.46875, 0.00000000000000000009217047612422726771874958703415542818220385365300128329640951861 64.5, 0.00000000000000000008963825288761168652917153962974268544245951921827427492074874258 64.53125, 0.00000000000000000008717545482939809669166938640614204617233798562513389462181772158 64.5625, 0.00000000000000000008478018262005168168579799677397003163139345313215758073119907873 64.59375, 0.0000000000000000000824505887757572089107390554969755632636308881441729159300260103 64.625, 0.00000000000000000008018487624648957545772378498155255229867880491380673799648302459 64.65625, 0.00000000000000000007798129704244234295527155008971218392058472587463860241132183638 64.6875, 0.00000000000000000007583815089777483609878438234521571789272826914456532902029593371 64.71875, 0.00000000000000000007375378397066647089654635808353868244229893081625046004266818088 64.75, 0.00000000000000000007172658757869431300583654089132914657608852690195706425225755855 64.78125, 0.00000000000000000006975499696857646413961776817606549293101855733255349756375336519 64.8125, 0.0000000000000000000678374901193497551950617536406360092340720137569058319844964454 64.84375, 0.00000000000000000006597258657807540775852494644550071408834398107551865140464977559 64.875, 0.00000000000000000006415884632719082972973597693156337137233552761601579963846645151 64.90625, 0.00000000000000000006239486868264955423064463929067559337915131217483918298069647758 64.9375, 0.00000000000000000006067929122201453148229938716543025830286979265574473866269801928 64.96875, 0.00000000000000000005901078874169255823094922529885737038879155553132604693573662587 65.0, 0.00000000000000000005738807224251959540370561065805938441602393453488188010309228186 65.03125, 0.00000000000000000005580988794292809834504204591829968655794508353790922203171345139 65.0625, 0.00000000000000000005427501631894828115978558450492257191610886444341680374947592949 65.09375, 0.00000000000000000005278227117031547287056541824301871165560954822398302191404036855 65.125, 0.00000000000000000005133049871197541337671174983594969700645711934648891349496956538 65.15625, 0.00000000000000000004991857669029849626151950835610925659140878145542730657294088761 65.1875, 0.00000000000000000004854541352333260762599088166112446858047050040052968556682642678 65.21875, 0.00000000000000000004720994746444234923676814396990324693506472550147675563402857162 65.25, 0.00000000000000000004591114578870008389806552092198226859944220348493420643996866424 65.28125, 0.00000000000000000004464800400141141426306250988496887181036224746210896461546018137 65.3125, 0.00000000000000000004341954506817441610715036814123391156701794880467411147227991999 65.34375, 0.00000000000000000004222481866588820586745903129190346017736990820670558911415526294 65.375, 0.00000000000000000004106290045414224214937262722499976946442744164298769522827232801 65.40625, 0.00000000000000000003993289136643315372466745768253116749536926145008691940022119661 65.4375, 0.00000000000000000003883391692067086379384988550985319208912989377882508280854411628 65.46875, 0.00000000000000000003776512654845035314506060902064877887682678619501496250876605394 65.5, 0.00000000000000000003672569294257958420108333757222879990326101397961315614485405003 65.53125, 0.00000000000000000003571481142236790439983102510857716870129246618917700148408662009 65.5625, 0.00000000000000000003473169931619267121302724679089150540547775401410201639841297632 65.59375, 0.00000000000000000003377559536087490240667612072306610642346117883832803264841903213 65.625, 0.0000000000000000000328457591174074636499287533743112292949609756484594480588179175 65.65625, 0.00000000000000000003194147040259167078851137001439599957481591299484533534697419084 65.6875, 0.00000000000000000003106202873615021526219922809615682979297874002786716673920372783 65.71875, 0.00000000000000000003020675280289602726176711111263586653515208563963781802004648101 65.75, 0.00000000000000000002937497992954808104658380605069484920255985475898595997311560092 65.78125, 0.00000000000000000002856606557579622890147458872518225728901842380173161763447543281 65.8125, 0.00000000000000000002777938283922793279364956529283801984069501912166337362029138196 65.84375, 0.00000000000000000002701432197374025396758199358340183726674767665486210655526979532 65.875, 0.0000000000000000000262702899210706683410800719075693052802505388403520614143946786 65.90625, 0.00000000000000000002554670985509020728176218734437157682660748796879409274294888825 65.9375, 0.00000000000000000002484302073851208658667968721014367393693067108856141726058549962 65.96875, 0.00000000000000000002415867689167838849602170148106185466189900241047061301514655077 66.0, 0.00000000000000000002349314757309650938725572375338679265119483580921613890150051284 66.03125, 0.00000000000000000002284591657140598627197346365673156251153420238178404081081686106 66.0625, 0.00000000000000000002221648180846497502316029256977804188528820803757061684964322336 66.09375, 0.00000000000000000002160435495325407888533064443367702366779296693885929857046828809 66.125, 0.00000000000000000002100906104630342357922456827391331669444268764161035359663572053 66.15625, 0.00000000000000000002043013813435685135198426358601700089648815074566074382629522604 66.1875, 0.00000000000000000001986713691499486662310645003274114593044494287135379305060506871 66.21875, 0.00000000000000000001931962039094551625535049937870776414519024626772018482258526308 66.25, 0.00000000000000000001878716353381973360101025068661976324275760592514235722129753583 66.28125, 0.00000000000000000001826935295701482284804625109558567820471895924160370168134554018 66.3125, 0.0000000000000000000177657865975367141798064630263578842647946029190366834730133961 66.34375, 0.00000000000000000001727607340649838608445914046578502537570143201807598026342381495 66.375, 0.00000000000000000001679983304805843388345294679998189204353723200636166038469042815 66.40625, 0.00000000000000000001633669560657016813331304318938231961612633000807240622756023211 66.4375, 0.0000000000000000000158863013017178577999247177399787260500013021555878942476465632 66.46875, 0.00000000000000000001544830021142279568780244774220924849507037971673382032743843627 66.5, 0.00000000000000000001502235200230776208142546794550291239475009436827904074641621428 66.53125, 0.00000000000000000001460812566751420135173224883162295306682524480320078844104494597 66.5625, 0.00000000000000000001420529927167200970924343020310330722074205019295563567230587308 66.59375, 0.00000000000000000001381355970282726454091022264857077987996462798797557832297911578 66.625, 0.00000000000000000001343260243113851093260931825196568172127355009819619654761236312 66.65625, 0.00000000000000000001306213127415736302526542338379068557903459809666353149528242752 66.6875, 0.00000000000000000001270185816851418064496177541152866313781132747744398022472731296 66.71875, 0.00000000000000000001235150294783444894709567494872662462197753144964257940654140766 66.75, 0.00000000000000000001201079312671622428135811425333592497662763047031966711695003967 66.78125, 0.00000000000000000001167946369060361667920149294579554869887923360721980676455364049 66.8125, 0.00000000000000000001135725689139576175372119675634545312904484498944263097811387991 66.84375, 0.00000000000000000001104392204863509575538216177477450086928575491007860413835151221 66.875, 0.00000000000000000001073921535612299032679699587949347648575773057631322002542800299 66.90625, 0.00000000000000000001044289969381493133843421974349154149518408508168039783932936171 66.9375, 0.00000000000000000001015474444485144217130476911022330944613490290894152545323273426 66.96875, 0.000000000000000000009874525317584858965228838113754458732250378076152304556349474494 67.0, 0.000000000000000000009602024172465866613645152320069094050579312865797586215349587594 67.03125, 0.000000000000000000009337028853657402520236468215292496245774697649606247946771888856 67.0625, 0.000000000000000000009079333025247133123908386274329096113977260524297554207706377214 67.09375, 0.000000000000000000008828736011933208656800843891606904202624043683364439042992668601 67.125, 0.000000000000000000008585042644061407161905573753566758263590258688716899488835940131 67.15625, 0.00000000000000000000834806310689509202828992789692259513469025802044968162279566691 67.1875, 0.000000000000000000008117612794002630699489276319210777028139194994023848363988164723 67.21875, 0.000000000000000000007893512164650058203607959348914782931064864169807936959251180772 67.25, 0.000000000000000000007675586605089820105833522017406010326845641514513609267152085134 67.28125, 0.000000000000000000007463666293639397695442918506441603391393730929513150931420914379 67.3125, 0.000000000000000000007257586069446505927950240533788993835886530733600929497922487845 67.34375, 0.000000000000000000007057185304840364026434745104355989986868696210115726846167291403 67.375, 0.000000000000000000006862307781171271823174302188675249326978008734901498681718847099 67.40625, 0.000000000000000000006672801568043383955208967743975011667363190487862061094319679059 67.4375, 0.000000000000000000006488518905848160921463337523046359910218824564310111738514635723 67.46875, 0.000000000000000000006309316091508492716527211131067141407491056719179319301647144845 67.5, 0.000000000000000000006135053367345939176422175429008789187902259559258574479248534156 67.53125, 0.000000000000000000005965594812985913152702234008964788851239384700718985564699012864 67.5625, 0.000000000000000000005800808240217949971208958875661820785244257886466373750014388081 67.59375, 0.000000000000000000005640565090730461080351586953484422996414787113691276740575156211 67.625, 0.00000000000000000000548474033664156305330836330168311793407453981366357778861334705 67.65625, 0.000000000000000000005333212383749706835489301916309400046858175399325490553105017095 67.6875, 0.000000000000000000005185862977429907934685212139095470315490275830780096425795598241 67.71875, 0.00000000000000000000504257711110339770476074586084684159306986428443534339487742461 67.75, 0.000000000000000000004903242937210480500393915167118596825581069756157323443153334899 67.78125, 0.000000000000000000004767751680618292764880790748314898424813720987947558478039269027 67.8125, 0.000000000000000000004635997554397019499977400856202324075407853339142559414785968313 67.84375, 0.000000000000000000004507877677899932461692091725567148616154579078615678328053999504 67.875, 0.000000000000000000004383291997084374196457774852934844778068893264597479542277709224 67.90625, 0.000000000000000000004262143207012524008850170830439936614466193414975638384470752299 67.9375, 0.000000000000000000004144336676472447429674603742077853316322184170937610077383901065 67.96875, 0.000000000000000000004029780374661550991541304890465770719985150291842850294553035627 68.0, 0.00000000000000000000391838479987614034367302032762678004232542457086996098994150202 68.03125, 0.000000000000000000003810062910152313141205723077137269068267324811983950912353354118 68.0625, 0.00000000000000000000370473005580490988699378350270877244867896799236510692220052839 68.09375, 0.000000000000000000003602303913812697114897454354199563251789260171386427368856816559 68.125, 0.000000000000000000003502704423999369081182475941404615139541228831094331587164607019 68.15625, 0.000000000000000000003405853726961327543777943771982266011348616534169891635874960149 68.1875, 0.000000000000000000003311676103694535297607952503888767439993160043311629545360066524 68.21875, 0.000000000000000000003220097916874038909812039067469337483814355144595965495731836828 68.25, 0.000000000000000000003131047553741020545822475112536804344558886980447160122379592441 68.28125, 0.000000000000000000003044455370553468853780115874020790963794176713666684232697790161 68.3125, 0.000000000000000000002960253638557755512553270261051049894524213217714818804474889167 68.34375, 0.000000000000000000002878376491439568154384083523253305831786266639071599775508981958 68.375, 0.000000000000000000002798759874213782829111646284731171026300625959715273692189661829 68.40625, 0.000000000000000000002721341493513960841338438294500086409235809035384917884935029149 68.4375, 0.000000000000000000002646060769243226499928815459489869863663832614143273643270122641 68.46875, 0.000000000000000000002572858787549324883377348179039531222923734316045809037011337282 68.5, 0.000000000000000000002501678255087672935402832609803006940259513691175364619185936682 68.53125, 0.000000000000000000002432463454537203831765231959028698614233678913425458490749599928 68.5625, 0.000000000000000000002365160201334764350111783766469297204656468612853896639658861503 68.59375, 0.000000000000000000002299715801594758657730265169047659377556680108301145070599125075 68.625, 0.000000000000000000002236079011181640215814679449896899387208181672239503272712962933 68.65625, 0.000000000000000000002174199995903737072453347106574957009666046298035489652129720735 68.6875, 0.000000000000000000002114030292797755350600065953292679462311006466791457123397808445 68.71875, 0.000000000000000000002055522772474141884204200602932179108421110953589682194218318915 68.75, 0.000000000000000000001998631602494300350213777390578371997569763266597333548304770215 68.78125, 0.000000000000000000001943312211751446503902079595084497966723772179519022610820085979 68.8125, 0.000000000000000000001889521255827657850758614380210585341111768482829713886886493548 68.84375, 0.000000000000000000001837216583300421864617389308350573792028045921284148886712585594 68.875, 0.000000000000000000001786357202972715257528155405314699881761306325283938593333828134 68.90625, 0.000000000000000000001736903252001355375467266061512134194163278318579181455991803292 68.9375, 0.000000000000000000001688815964899054073706262677436033007508370560212664380184722532 68.96875, 0.000000000000000000001642057643386274940299718687123716348690818012197184499394990308 69.0, 0.000000000000000000001596591627069646995323855582621040139986685291113254687821358293 69.03125, 0.000000000000000000001552382264924322492994914427237562708349556878961640001216806084 69.0625, 0.000000000000000000001509394887558283675991365399628448872516202439166624597609795109 69.09375, 0.000000000000000000001467595780237203745498967783230161402217945261449167037642868675 69.125, 0.000000000000000000001426952156649051373279786192525030057148588923741330307899859726 69.15625, 0.00000000000000000000138743213338819623765154787232898516269832906823595686375365544 69.1875, 0.000000000000000000001349004705139325745831203872330347949389044172693359684795329786 69.21875, 0.00000000000000000000131163972054202073111353161138563195669977931598635514424972746 69.25, 0.000000000000000000001275307858717360893894197565110633046036905630144311213561140883 69.28125, 0.000000000000000000001239980606438439488591805012648027200311844873619014166316416884 69.3125, 0.000000000000000000001205630235927161631272505577987026632659941874383524167694717327 69.34375, 0.000000000000000000001172229783260181991935298595170800794122931836498872845761862876 69.375, 0.000000000000000000001139753027367305907465600274889177283808334831332494410509735021 69.40625, 0.000000000000000000001108174469606133462762439777196488373672995917425995844009792329 69.4375, 0.000000000000000000001077469313897169185376781347493888698078887222458488036357640991 69.46875, 0.000000000000000000001047613447404051020645093346437712881155935888112634846930542224 69.5, 0.000000000000000000001018583421743971528091930302489321394921586532458220065317417343 69.53125, 0.0000000000000000000009903564347137720852324468720834841449177469653013922287096322673 69.5625, 0.0000000000000000000009629103125175876125916796149338239603893328495168320582091171789 69.59375, 0.0000000000000000000009362234924823052461044808401127203530502324884881436977204125929 69.625, 0.0000000000000000000009102750062474757741996618711681710022929240612535160751214594037 69.65625, 0.0000000000000000000008850444634166818129566856491717951289689038260640023549664291682 69.6875, 0.000000000000000000000860512035657721892142495946993119865496586095942365707890320023 69.71875, 0.0000000000000000000008366584412393151385260630640487737232651503302818119957652599131 69.75, 0.0000000000000000000008134649299923673341165559209260456425098681563291591078308978235 69.78125, 0.0000000000000000000007909132686841660522154930692099052200619196381505991115430247405 69.8125, 0.0000000000000000000007689857267941905827952422970281655279554839154819003975056865326 69.84375, 0.0000000000000000000007476650626805316933292606090041964596439083965864870921756040451 69.875, 0.0000000000000000000007269345101262171678320914100603137078115070602352906485949775734 69.90625, 0.0000000000000000000007067777652550317551413834049131912256275916701291921709720356003 69.9375, 0.0000000000000000000006871789738067048607789491798970910001554326164004041922936675408 69.96875, 0.0000000000000000000006681227187616162523876464027170765951840770575247876326377059874 70.0, 0.0000000000000000000006495940083054394281799719401863991939464728357930134123252874882 70.03125, 0.0000000000000000000006315782641244043267366335910475945772599277370618744269414982345 70.0625, 0.0000000000000000000006140613100221159349579780331209401899947309350459527338917768976 70.09375, 0.000000000000000000000597029360849113273663283022877789018873510541660831704348321387 70.125, 0.0000000000000000000005804690117365943966313168812947975131045317747820657989513697836 70.15625, 0.0000000000000000000005643672276259676130156480225856150297107954174045703588578577794 70.1875, 0.0000000000000000000005487113330861173142813670168878300241229393060606829717525159989 70.21875, 0.0000000000000000000005334890024104947294591377883321357461722831605859058087523081734 70.25, 0.0000000000000000000005186882499863598162242568422772572030818287819585864615087016299 70.28125, 0.0000000000000000000005042974209287104851008995732716291738007983219029013605728751593 70.3125, 0.0000000000000000000004903051819716396104998648212422750215187899295040654071633448735 70.34375, 0.0000000000000000000004767005126100589614949454930838387143407821498724672654425906647 70.375, 0.0000000000000000000004634726964849224391579758577944339462247174710010201456513195514 70.40625, 0.0000000000000000000004506113130052689837064412109873827798951327059787523058706597335 70.4375, 0.0000000000000000000004381062292005883574543802880049203906176623886457142616807142303 70.46875, 0.0000000000000000000004259475917971908584734350014176513252374140613376068506215083426 70.5, 0.0000000000000000000004141258195124350110406862253037641175920684291989816555615620169 70.53125, 0.0000000000000000000004026315955608355447489230842036683458633436974832006048811434813 70.5625, 0.0000000000000000000003914558603662376433602620561306169183129062975570790293082703879 70.59375, 0.0000000000000000000003805898044744026423716424183820440358596245903658699872701428251 70.625, 0.0000000000000000000003700248616605052027007392418410193039049469029538475502834446097 70.65625, 0.0000000000000000000003597527022261926054496677510504993543287420993987820011015407035 70.6875, 0.0000000000000000000003497652264810033146941375108580353070539491405033154166760133263 70.71875, 0.0000000000000000000003400545584030844538737455940681807586341126901954576983237294164 70.75, 0.0000000000000000000003306130394742864457699025084727430927981806785967385662152649332 70.78125, 0.0000000000000000000003214332226848478824279973481607652540479654482549810232538213797 70.8125, 0.0000000000000000000003125078667030148229984026993999822740145864733404202154167660285 70.84375, 0.0000000000000000000003038299302050662648157616715261864318092737692393159419140677415 70.875, 0.0000000000000000000002953925663613415938532821699855715311840501900561244263215235212 70.90625, 0.0000000000000000000002871891174739864900648776813590907960112858362679775248259158722 70.9375, 0.0000000000000000000002792131097622511335622905483290147641692855192868795434252125655 70.96875, 0.0000000000000000000002714582482912887190493348860642071863188902147959436403876173217 71.0, 0.0000000000000000000002639184120405133259851042583559108085000072734034570427570869368 71.03125, 0.0000000000000000000002565876491076841957243472430242228538332856461198656011242950403 71.0625, 0.0000000000000000000002494601720449885172207675177075100738902459803703614094133175223 71.09375, 0.00000000000000000000024253035332349700035781234513137962622290060712051088972806901 71.125, 0.0000000000000000000002357927209224658989783526229982639761155338070125115233167729403 71.15625, 0.000000000000000000000229241954040055810472577147300866613155827405629805996162385078 71.1875, 0.0000000000000000000002228728789221315995296786423548989815549252536993959012395365734 71.21875, 0.0000000000000000000002166804648058992425213298342767000765650454613795781457074058346 71.25, 0.0000000000000000000002106598199752243361567813658158827695099495900155106561775687447 71.28125, 0.0000000000000000000002048061879245635278126791670573663863178943918964830522217777431 71.3125, 0.0000000000000000000001991149436285242717180996225764407343837571017102603590164011274 71.34375, 0.0000000000000000000001935815899141501595807265447081619721769675516728871073465355687 71.375, 0.0000000000000000000001882017539331086791277580307791318729485067002887323731743619928 71.40625, 0.0000000000000000000001829711837310356805473753018701088657656668873442696019371597269 71.4375, 0.0000000000000000000001778857449113661384304085598221889365557958216921820504126504643 71.46875, 0.0000000000000000000001729414173910540433840621109705184932376223657055419363324042824 71.5, 0.0000000000000000000001681342922456554993009869105877623156627991195788550748114644335 71.53125, 0.0000000000000000000001634605686413183940651263096412808229444898485672083798766702113 71.5625, 0.0000000000000000000001589165508512894065165304179088262311951973923280378378608381267 71.59375, 0.0000000000000000000001544986453546146625856028078625833726248296019046231501303507299 71.625, 0.0000000000000000000001502033580147741090362634963323107893625368703713555339646912249 71.65625, 0.0000000000000000000001460272913360516832472949291896775947526124746032465442229048865 71.6875, 0.0000000000000000000001419671417955036695957817269189713166309231791772082869426234373 71.71875, 0.0000000000000000000001380196972484462936705652165750545529156295273812423050747790006 71.75, 0.0000000000000000000001341818344054406598572550706876381909662470995351491614926588538 71.78125, 0.0000000000000000000001304505163788086296899325587668706993081866804260934684286751102 71.8125, 0.0000000000000000000001268227902967672104521608986434750706161990031734728371045059886 71.84375, 0.0000000000000000000001232957849833215173612998197492905459872919168406349623656490676 71.875, 0.0000000000000000000001198667087021074286831628627345971469531907749618358584711722173 71.90625, 0.0000000000000000000001165328469624247105950582738815404936533608613365174783682063403 71.9375, 0.0000000000000000000001132915603857496857690199935418420496090454099473400466668024033 71.96875, 0.0000000000000000000001101402826310634936660142532520746467505416324605432297648699974 72.0, 0.0000000000000000000001070765183773776775845530364879060377775647403533351338286261391 72.03125, 0.0000000000000000000001040978413618832687755009230069504413850286617766308111171688427 72.0625, 0.0000000000000000000001012018924721927556413964347550597469201175264299204957346972476 72.09375, 0.00000000000000000000009838637789118635947232896162686511614224212838188736278922083076 72.125, 0.00000000000000000000009564906729301491971226049858063183317305201448551299155571013311 72.15625, 0.00000000000000000000009298779208885145289724853482679644267639686539438387494785956739 72.1875, 0.00000000000000000000009040044372102212079859154330552019756663934607158564154370908427 72.21875, 0.00000000000000000000008788497200418495474183666881438972796026382515920728370370574364 72.25, 0.00000000000000000000008543938351226126354604422221250394287512754621283213075431851516 72.28125, 0.00000000000000000000008306174000986023023484305427330261067106262746650697133843510159 72.3125, 0.00000000000000000000008075015692697180504028016532283928821506217132486112582050943729 72.34375, 0.00000000000000000000007850280187573665593235935215728957387298562424660592267588010449 72.375, 0.00000000000000000000007631789320813466891339694540240876824903198958915594561144973119 72.40625, 0.00000000000000000000007419369861346532386047149335756248219050197391452458565105884248 72.4375, 0.00000000000000000000007212853375452423233582474072058243996728318350521790842885298881 72.46875, 0.00000000000000000000007012076094141023539830633234430333320994487343912024449094999951 72.5, 0.00000000000000000000006816878784192674527102864397139137202626650282589965826721954551 72.53125, 0.00000000000000000000006627106622756949734921542578877127711145701637484320427210695024 72.5625, 0.00000000000000000000006442609075412058044868932419356110536705506234592041575360715999 72.59375, 0.00000000000000000000006263239777589555478025280147118369543043721305213037601588432445 72.625, 0.00000000000000000000006088856419271666968523182605146710886452413122179433733638818748 72.65625, 0.00000000000000000000005919320632871067691158846819429287312467908470728698673800346479 72.6875, 0.00000000000000000000005754497884205451982471648193818843919632937790795920938109173336 72.71875, 0.00000000000000000000005594257366481628357134815264513505922102336511645321675860710991 72.75, 0.00000000000000000000005438471897206223446518588683479023037461157822681995492149691592 72.78125, 0.00000000000000000000005287017817942357684666847031714146210101987371140867915612431843 72.8125, 0.00000000000000000000005139774896833872999976394895258402949285222912496940932873259356 72.84375, 0.00000000000000000000004996626233820849351773890053089316380514527436065801469068509758 72.875, 0.00000000000000000000004857458168472244346130881398330620161337012949673938372833633282 72.90625, 0.00000000000000000000004722160190363529995493751867185120740437242546430697193683532844 72.9375, 0.00000000000000000000004590624851929184528593777827957864994799257059113573250028760218 72.96875, 0.00000000000000000000004462747683721826544121866903054707708190983422390384631092595526 73.0, 0.00000000000000000000004338427112011655225373814322910869464171569915935477307849833266 73.03125, 0.00000000000000000000004217564378661685244300910604161398303679321636030924507569327358 73.0625, 0.00000000000000000000004100063463216039793289578163760392842310165037406065876761897479 73.09375, 0.00000000000000000000003985831007140291264122221339128584298082072169373683595108618763 73.125, 0.00000000000000000000003874776240154517780999031462286195749448580045678549508118231881 73.15625, 0.00000000000000000000003766810908601376386789351247277516764717962699532523674395645234 73.1875, 0.00000000000000000000003661849205793081441891940255046112422267584420981184347473432232 73.21875, 0.00000000000000000000003559807704282720951743341539144549141982575281590504560413959026 73.25, 0.00000000000000000000003460605290006845287058463051673841320341451956524992754655162634 73.28125, 0.00000000000000000000003364163098247723262578455516455955280631268892209485480661756076 73.3125, 0.00000000000000000000003270404451365080925915493665700444728426898893576154675419370145 73.34375, 0.00000000000000000000003179254798248519777583330829676019352197877825955577671509838588 73.375, 0.00000000000000000000003090641655443154565974929223248506095207129090776485735482829981 73.40625, 0.00000000000000000000003004494549902317317130361243594345276819523809764360153977748515 73.4375, 0.00000000000000000000002920744963322444880411952417429617315828040440444819448518700315 73.46875, 0.00000000000000000000002839326278016502981774384334903891440454785241059624327313414596 73.5, 0.00000000000000000000002760173724283501533368349889114620656515570141739335683389201995 73.53125, 0.00000000000000000000002683224329232824682743398705245558107904093571032388808529954585 73.5625, 0.00000000000000000000002608416867023235702442582536103650934656076940256820298115168795 73.59375, 0.00000000000000000000002535691810477522202055224921102878959131804033937717684930804041 73.625, 0.00000000000000000000002464991284034822146460889365126107544433021288702450069903204574 73.65625, 0.00000000000000000000002396259018003716619261167277068116602741346551186604658496812672 73.6875, 0.00000000000000000000002329440304080191989657551359173530026233402014276702967252466667 73.71875, 0.00000000000000000000002264481952095562912514876226776646202837049252643943397604781593 73.75, 0.00000000000000000000002201332247960409181697917064240083951552855515147010268828077343 73.78125, 0.00000000000000000000002139940912771514611646080383488895900565229299689438635455776012 73.8125, 0.00000000000000000000002080259063049705566808180489507749414814790525762671145133076649 73.84375, 0.00000000000000000000002022239172077371198392198767715992917499824637735143407652395624 73.875, 0.00000000000000000000001965835032305307568977757244743690174743504362506987261032700747 73.90625, 0.00000000000000000000001911001718799364315191384091748569869179113075427697778838913607 73.9375, 0.00000000000000000000001857695553698185965884740416023904556706420262861164210870320691 73.96875, 0.00000000000000000000001805874071654131129339873371356572305072788203952621803839359334 74.0, 0.0000000000000000000000175549598623022210192271065479200056033542304138020994816576607 74.03125, 0.00000000000000000000001706521157226725629471589124705679389120312220224967018342937805 74.0625, 0.00000000000000000000001658910558911693152341967139329238075410160903072397502037306421 74.09375, 0.00000000000000000000001612626249130496450324065850060810695944889194480723625685858154 74.125, 0.00000000000000000000001567631339270082724026331395197349668712013398248972969655793209 74.15625, 0.00000000000000000000001523889965054342339143363899394984158905231564943459526254216853 74.1875, 0.0000000000000000000000148136725814763323903248039283108499428477476717225675027251291 74.21875, 0.00000000000000000000001440029318544138904699773504419954045694454786151385286917222796 74.25, 0.00000000000000000000001399843187721352201286278722311881980525544768042967045589749064 74.28125, 0.00000000000000000000001360776822536575974618643829137600191262263941582128459901013687 74.3125, 0.00000000000000000000001322799069845913315414857465301559750483206820224541451178259778 74.34375, 0.00000000000000000000001285879641825786444638113580529902823128408672221257221441961441 74.375, 0.00000000000000000000001249989091977573631198829977789665375832301290420330479784745557 74.40625, 0.00000000000000000000001215098791796488860587322245245030709001903193369046383986116877 74.4375, 0.00000000000000000000001181180908086349546221549876097890996884252397123775850991870827 74.46875, 0.00000000000000000000001148208380902383819053080756756850632979163414034431479665214243 74.5, 0.00000000000000000000001116154902104721238932804452605300012410171344296855495519938384 74.53125, 0.00000000000000000000001084994894505689526247720283505337783836934563640595831726638414 74.5625, 0.00000000000000000000001054703491594505486757179724355972971943250192417671023818080738 74.59375, 0.00000000000000000000001025256517823401058528330134060147515430033898111861767638684533 74.625, 0.000000000000000000000009966304694396656996137361093740358343611487708232814012432348894 74.65625, 0.000000000000000000000009688024958485145011889454029025600154650752843920482165066877156 74.6875, 0.00000000000000000000000941750381492107786440213722116894506269317812865963879155614817 74.71875, 0.000000000000000000000009154525282304528645922288260943631974816236308217017486747516686 74.75, 0.000000000000000000000008898879382103123672354414478829952020528487156218481394602530875 74.78125, 0.000000000000000000000008650361972086265070521090567102995892783246204070735739603085196 74.8125, 0.00000000000000000000000840877458437328965242154543148939425980307550884798591455906721 74.84375, 0.000000000000000000000008173924267967982233304662499413702013324239619748425341476296369 74.875, 0.000000000000000000000007945623435655382895594190116853466362474540952097726685680654503 74.90625, 0.000000000000000000000007723689715140252039620420414025253440419938993865747415154766602 74.9375, 0.000000000000000000000007507945804309887061767740317690866414305685146319750312964716136 74.96875, 0.00000000000000000000000729821933050722275490980035624329290950534619786610649831157162 75.0, 0.000000000000000000000007094342713703296557570243768548608702136623875356548742245632521 75.03125, 0.00000000000000000000000689615303346122204059045395808593437236752363159262090371206833 75.0625, 0.000000000000000000000006703491899586791899352307961726560023019065765192783046030876122 75.09375, 0.000000000000000000000006516205326363727536455037101045141904908157749713365023871364257 75.125, 0.000000000000000000000006334143610274408331151708145587857511512399706633616765818886411 75.15625, 0.000000000000000000000006157161211109652092836680684906469289052908832809863562632030027 75.1875, 0.000000000000000000000005985116636373781121214825782343182079009844980250332424722409838 75.21875, 0.00000000000000000000000581787232889379782165073411709559653754096272017268677345022415 75.25, 0.000000000000000000000005655294557544011969771440328295089547906642468838500789951513003 75.28125, 0.000000000000000000000005497253310999910448425875478969892172789173725253299842470714181 75.3125, 0.000000000000000000000005343622194437441502420153479233464138560896631644573613056598339 75.34375, 0.000000000000000000000005194278329096201129444103499494329598023001570931088848485875821 75.375, 0.000000000000000000000005049102254627260955674476668421041648715579565716031008617657418 75.40625, 0.00000000000000000000000490797783414856658847380957897020869696046694521261624607048234 75.4375, 0.00000000000000000000000477079216193296470494660754073055270447599773756739077223834415 75.46875, 0.000000000000000000000004637435473655987685489732438566724805381732083293501453525077509 75.5, 0.00000000000000000000000450780105913253805186880297485758014438640439399952366314186607 75.53125, 0.000000000000000000000004381785177473572891368696196441994807686413076965797358603308388 75.5625, 0.000000000000000000000004259286974595792370636582852948999470318600727808744657587854017 75.59375, 0.000000000000000000000004140208403019187851407365164528565145800269531022529854085487936 75.625, 0.000000000000000000000004024454143889105460986184745980118880479594049741583224573132931 75.65625, 0.000000000000000000000003911931531161231649057777734508041808652523296439119989105490905 75.6875, 0.000000000000000000000003802550477889609646368311283308193391223969244345709631467999134 75.71875, 0.000000000000000000000003696223404559451159784898119874829455567052993353133231404477224 75.75, 0.000000000000000000000003592865169408117385346788314069057774520761562368317113394588974 75.78125, 0.000000000000000000000003492393000679208753810266609398239791669456342910895403208812707 75.8125, 0.000000000000000000000003394726430756224964925442140011896685838420457591552397475704356 75.84375, 0.000000000000000000000003299787232123737006723022626974740452687159297223619351225083724 75.875, 0.000000000000000000000003207499355105452151197013259514995541536727923474618831955954225 75.90625, 0.000000000000000000000003117788867329952492914481583598847744351929757278356133031223615 75.9375, 0.000000000000000000000003030583894876248546312804754475752153391916314372046799255002128 75.96875, 0.000000000000000000000002945814565052612804730423812059103919238393578949546377133964641 76.0, 0.000000000000000000000002863412950763445024281879554934433383589366651223671497758764881 76.03125, 0.000000000000000000000002783313016420172334807580947061084253965363938501010096771345763 76.0625, 0.000000000000000000000002705450565353404076912582281855195124479132299081972150079592746 76.09375, 0.000000000000000000000002629763188684744470258374563951241900291298212967554128421968211 76.125, 0.000000000000000000000002556190215617816759319929436741171054546608319310408235462153785 76.15625, 0.000000000000000000000002484672665109171258848512012688574132381078142445089795633070798 76.1875, 0.000000000000000000000002415153198880837607618019951921382745520140894935004844433723281 76.21875, 0.0000000000000000000000023475760757373393869344130347218095516386101689099544482456046 76.25, 0.000000000000000000000002281887107151017897697164283670808797638101000216396131567971376 76.28125, 0.000000000000000000000002218033614080512121591586667191493141040617227277880239205180865 76.3125, 0.000000000000000000000002155964384988214501189378052725320989848817748859727143951600695 76.34375, 0.000000000000000000000002095629635023467921732695832546195639308709418725611353202020733 76.375, 0.000000000000000000000002036980966339188904619581265536759118033591807538377912718142341 76.40625, 0.000000000000000000000001979971329510496249169851131845639718063003862483258965359218205 76.4375, 0.000000000000000000000001924554986024793885393389600897504573803614184271017663898791014 76.46875, 0.000000000000000000000001870687471813602207202282279207808167716912179452036339595348358 76.5, 0.000000000000000000000001818325561797254305061123783413950877686888698945856219503896148 76.53125, 0.000000000000000000000001767427235414372953488863655228189254130619246452697386410760323 76.5625, 0.000000000000000000000001717951643108821558420040385516933822062072924362607917555087191 76.59375, 0.000000000000000000000001669859073747578141275792334129114562369702502572233502653478601 76.625, 0.000000000000000000000001623110922943716422994937036729453398272188032989224416554549335 76.65625, 0.000000000000000000000001577669662259392748239130255994649079381784079696226892915169878 76.6875, 0.000000000000000000000001533498809264432517665067170362169745516432632943278472844726594 76.71875, 0.00000000000000000000000149056289842678551928278649781466669959667468146673112602109889 76.75, 0.000000000000000000000001448827452811776598228404888977289991483555079670089484326480774 76.78125, 0.000000000000000000000001408258956567716992924158582182609980648522719059037444207594555 76.8125, 0.000000000000000000000001368824828176062895547365908383086308278297649053412322387521313 76.84375, 0.000000000000000000000001330493394444911853158248936960462437817485067838945881812156111 76.875, 0.000000000000000000000001293233865225214986505386903428553726494945438120215665485898911 76.90625, 0.000000000000000000000001257016308829654127151989807497462835669609607874091142660231603 76.9375, 0.000000000000000000000001221811628134688308173920723745401173844926713893816474070908479 76.96875, 0.000000000000000000000001187591537346814024960563048771601143745098167794794387023204507 77.0, 0.00000000000000000000000115432853941460873429181307087231081406393396482495991303260183 77.03125, 0.000000000000000000000001121995904068637593888577966025346665017708580583166901728301602 77.0625, 0.000000000000000000000001090567646471799861711161221965076151322092863564532162527158812 77.09375, 0.000000000000000000000001060018506463174064043873920239605546295206101577372207645869443 77.125, 0.000000000000000000000001030323928378890382755659113490832242514361470525445558683012016 77.15625, 0.000000000000000000000001001460041434015073527576715508258163830966556728350897690850072 77.1875, 0.000000000000000000000000973403640649875466599367404095123831609458309649772235689380167 77.21875, 0.000000000000000000000000946132168311685568147400872885433934089910300588765710627042195 77.25, 0.0000000000000000000000009196236959417518126026923099834302295415412332542467560275388798 77.28125, 0.0000000000000000000000008938569067739464435511047576319767102374192473000172810606847134 77.3125, 0.0000000000000000000000008688110787155326437490662757294521154997352166079985097236481197 77.34375, 0.0000000000000000000000008444660677828112048275248110346052658797675539450556254158243917 77.375, 0.0000000000000000000000008208022919974335274473988373121588531475861555882817719669305553 77.40625, 0.0000000000000000000000007978007157305903676631739789189993817498611747545393747915723614 77.4375, 0.0000000000000000000000007754428344826402815777385112196408743967500356850438549936613323 77.46875, 0.0000000000000000000000007537106600860864466631295177306969656508152649302395299419048759 77.5, 0.0000000000000000000000007325867063201457253298354269239663728269945457901203538584996357 77.53125, 0.0000000000000000000000007120539749254797479334174395266155455309890523331933555045178188 77.5625, 0.000000000000000000000000692095942007974684629222600535376260264847211460074917879066793 77.59375, 0.0000000000000000000000006726965448207644974774893700202393574502363303419335846958009785 77.625, 0.0000000000000000000000006538401689139920582460733579941556661065272814765296902145908994 77.65625, 0.0000000000000000000000006355116356420938190595284635057169942262277375645720759260193871 77.6875, 0.0000000000000000000000006176961900186769615077033436657561032400399422054012685687155655 77.71875, 0.000000000000000000000000600379488909333347842649300880036061919287287669996770608404493 77.75, 0.0000000000000000000000005835475895530023721036284375980969248073735931445117441451858085 77.78125, 0.0000000000000000000000005671869384027551700854725103350487316663820176043459923681843211 77.8125, 0.0000000000000000000000005512843602771257998640235182975367673416553974777974871563215021 77.84375, 0.0000000000000000000000005358270478133611483218276054206790986106719211563127477187790799 77.875, 0.0000000000000000000000005208025512142006474905444401669240161965708780165306438169678184 77.90625, 0.0000000000000000000000005061987682800295859127988043721018506828538236416333229582774989 77.9375, 0.0000000000000000000000004920039347184760577979254624280283399712159970336686992965000401 77.96875, 0.0000000000000000000000004782066147237415846221754824827266521568533088167345305150385276 78.0, 0.0000000000000000000000004647956918181693432089957451616833347547929090785384489027700435 78.03125, 0.0000000000000000000000004517603599487619096444273341602511755040504303787409726323062566 78.0625, 0.0000000000000000000000004390901148315626434144973885590500420653613364214638818025549974 78.09375, 0.0000000000000000000000004267747455370114501542411671305786899395818703656704512954516455 78.125, 0.0000000000000000000000004148043263095768292352340021299243042269811772982497784012374376 78.15625, 0.0000000000000000000000004031692086151519846798469274183141387821277439539016979279814847 78.1875, 0.0000000000000000000000003918600134098835010092646661682111626073820915596912593306479924 78.21875, 0.0000000000000000000000003808676236242768020002990787586973077566722041164966817044382696 78.25, 0.0000000000000000000000003701831768565934584044953157177856690991763128182209558705766832 78.28125, 0.00000000000000000000000035979805826972152511116469828433003215073018428764674068581301 78.3125, 0.000000000000000000000000349703893685861599936197357957392261893962232094050907992207774 78.34375, 0.0000000000000000000000003398925428735283324988336917755945065016237405094693478156143179 78.375, 0.0000000000000000000000003303560930215197963022391449397657900180020254112838079579962589 78.40625, 0.0000000000000000000000003210868523946555905360254180184092182551925429327675280542786206 78.4375, 0.0000000000000000000000003120773441662288773213379449897077834035550943773973660338902164 78.46875, 0.000000000000000000000000303320300422257898941180292407509432645152328311691612861401905 78.5, 0.0000000000000000000000002948086563327589687165978523969059346681887637612278027079284753 78.53125, 0.0000000000000000000000002865355444853955962230835291676490464869119687877535311659678685 78.5625, 0.0000000000000000000000002784942893769873971387924078811900739597404742762102957504655894 78.59375, 0.0000000000000000000000002706784020584878519342929380445531527880003574673304252130721979 78.625, 0.0000000000000000000000002630815749291619147994834899075928886000533682366122697267922716 78.65625, 0.0000000000000000000000002556976766758130308707052407353840632317845668099234094933540073 78.6875, 0.0000000000000000000000002485207473530243895259719992789171670609243730015704846618618192 78.71875, 0.0000000000000000000000002415449936004913152301263310834370606928422537071781336261639182 78.75, 0.0000000000000000000000002347647839936306635929170793464006019676447566301263401826931932 78.78125, 0.0000000000000000000000002281746445237590349658607949676736418146694904486488693120439408 78.8125, 0.0000000000000000000000002217692542042346246861635567937074250205957535603951756095102005 78.84375, 0.0000000000000000000000002155434407990576793048237006019727519936641703776129002543472757 78.875, 0.0000000000000000000000002094921766705219008909469069856217765795012471939418067214201179 78.90625, 0.0000000000000000000000002036105747426038136796533746146384709581284814203077632429754588 78.9375, 0.000000000000000000000000197893884576869153696120701398394278276514142847896666058648459 78.96875, 0.000000000000000000000000192337488557764835246286651318180470135284188851999674964782896 79.0, 0.0000000000000000000000001869368981842520590091582699807782180623183925870867569978728361 79.03125, 0.0000000000000000000000001816877504648207236362188581724407131564690266706055030343212307 79.0625, 0.0000000000000000000000001765858044130075530997348205597293244389394016671967770436203078 79.09375, 0.0000000000000000000000001716269376406203205258661744768743200153296181993729302012867779 79.125, 0.0000000000000000000000001668071430459482990957643603622698222411694400094069065638783494 79.15625, 0.0000000000000000000000001621225255943146632466209072855895158092455851342302938273615188 79.1875, 0.000000000000000000000000157569299188400058604376439701261193097822885543615187825569142 79.21875, 0.0000000000000000000000001531437836258380149280025688149687259450417896900139673598910733 79.25, 0.0000000000000000000000001488424016416523493330355457714374399054598456506472447501050612 79.28125, 0.0000000000000000000000001446616760331742521184255383218767823972748385869433804440735405 79.3125, 0.0000000000000000000000001405982268651424180558865803293342346745984294869107653989381408 79.34375, 0.0000000000000000000000001366487687527534339478946770167709768927604192923739825032258875 79.375, 0.0000000000000000000000001328101082204917091161480782746468232469838935073468253205014396 79.40625, 0.0000000000000000000000001290791411346285883469420938917051666406306445320814742274189326 79.4375, 0.000000000000000000000000125452850207338964435748365066509875546670129183390233309152575 79.46875, 0.0000000000000000000000001219283025704407562620045897716440410253766517064646450854675973 79.5, 0.0000000000000000000000001185026474168180834243060072089849926332164326795739852221440488 79.53125, 0.0000000000000000000000001151731137076428937648010847814038785224326934240866153855197508 79.5625, 0.0000000000000000000000001119370079435622282844308929884657144548157124174447510422270498 79.59375, 0.0000000000000000000000001087917119980692804918854920403582617205111700392620913538275789 79.625, 0.0000000000000000000000001057346810113259644849262972954821358964788383375292388929860086 79.65625, 0.0000000000000000000000001027634413427528872628646221367977046657952775261752441521014865 79.6875, 0.00000000000000000000000009987558858074946405782671583808413290590010021491830548005574545 79.71875, 0.00000000000000000000000009706878560795245793879830055092766873627991824813467739924413618 79.75, 0.00000000000000000000000009434076072048550264879529729625366013657177324083496793508116797 79.78125, 0.00000000000000000000000009168930579969521564631591373394679158024371758516117359964919278 79.8125, 0.00000000000000000000000008911227453491136073152068598037917561904360856867352057052896102 79.84375, 0.00000000000000000000000008660758069580920959576274675104179672174394247199087990164668187 79.875, 0.00000000000000000000000008417319645299181137299466904000302496226243905995072436411856358 79.90625, 0.00000000000000000000000008180715074544834013384841342785951062623963988642665210027972248 79.9375, 0.00000000000000000000000007950752769358208272090412983987275152773035258348297089494798432 79.96875, 0.00000000000000000000000007727246505653798300633183122929885048105538654551013379076750111 80.0, 0.00000000000000000000000007510015273259500236993501208081993465007581753652346642608586864 80.03125, 0.00000000000000000000000007298883130142291795827036867356012771709955657962988441033242408 80.0625, 0.00000000000000000000000007093679060703658733002413788769219234244515248088319374363161733 80.09375, 0.00000000000000000000000006894236838031318690112694804477068653890636238830932951434744252 80.125, 0.00000000000000000000000006700394889996950792383490395593754947898546118571700807861392768 80.15625, 0.00000000000000000000000006511996169092709260282823137174181946847849214411377103461324645 80.1875, 0.00000000000000000000000006328888025902283871060641130334803969217030547796760892798466221 80.21875, 0.00000000000000000000000006150922086105171738227471623560862888048540502880110442621516445 80.25, 0.00000000000000000000000005977954130915645865890037490811409251977190347162838528865556748 80.28125, 0.00000000000000000000000005809843980860648518426297509910372136431327926899299341293030281 80.3125, 0.00000000000000000000000005646455382803503799756779411105570919223886577555658156962577023 80.34375, 0.00000000000000000000000005487655900122936075744185834088339879664083948805358084333203099 80.375, 0.00000000000000000000000005333316805959401054723761325568503878407282057167963911745764213 80.40625, 0.00000000000000000000000005183312979443186464551568597934295041505882957926876396512628492 80.4375, 0.00000000000000000000000005037522804821121274175030870229474031072955221259148435642375319 80.46875, 0.00000000000000000000000004895828073401048194029981611532552860378739428822480357853819413 80.5, 0.00000000000000000000000004758113888235465590622679953034993273967435447137358532655097446 80.53125, 0.00000000000000000000000004624268571467933753605656887804993981013047805123120657967537973 80.5625, 0.00000000000000000000000004494183574267968396123590941984207537339179530409576701419277545 80.59375, 0.00000000000000000000000004367753389282213040659075536390255382474771867286495168331336278 80.625, 0.00000000000000000000000004244875465531693185698170926555376698600475278004495977616535258 80.65625, 0.00000000000000000000000004125450125686910460387892482484665865503941702897504440868506007 80.6875, 0.00000000000000000000000004009380485654435907833385216853922063151138311397605833071139727 80.71875, 0.00000000000000000000000003896572376410509602602480809018585272072926757213998096409709157 80.75, 0.00000000000000000000000003786934268018950472341362821119153153208301808695452084385776087 80.78125, 0.0000000000000000000000000368037719577242688443757993085142170952206732017776812889237483 80.8125, 0.0000000000000000000000000357681468839783666411067371547418309320241216388947280917384521 80.84375, 0.00000000000000000000000003476162698268196079410741369885237388160597963300857530326745666 80.875, 0.00000000000000000000000003378339533565042273199954245327115394043239885807617926934963106 80.90625, 0.00000000000000000000000003283265792336913917749334783442318099228558085390576413632514983 80.9375, 0.00000000000000000000000003190864298400991754206819447088155217930936409205688556514715135 80.96875, 0.00000000000000000000000003101060039036455362601397076162012820100946386004930593435911777 81.0, 0.00000000000000000000000003013780104419546160526997052190288825128994128330007678856049106 81.03125, 0.00000000000000000000000002928953628751720389975854076803090312474837201389473897941046495 81.0625, 0.00000000000000000000000002846511733033630830133192508902436802893630703193808693923130675 81.09375, 0.0000000000000000000000000276638746943899324673902912525691052586767445662182605375812862 81.125, 0.00000000000000000000000002688515767243674203422192434467494684711672359031866964679512025 81.15625, 0.00000000000000000000000002612833380266581835714864779776381161535304089773890130416467717 81.1875, 0.00000000000000000000000002539278835780151514511167571504448582256597459726431438134813589 81.21875, 0.00000000000000000000000002467792384849394965320394680373907622878377628598955558465301056 81.25, 0.00000000000000000000000002398315954059625298856701453244370708410325631262998466000533946 81.28125, 0.00000000000000000000000002330793098594082457406597699637591692224873066039607367930073487 81.3125, 0.00000000000000000000000002265168956623764674878813506298811742061414879655566028035063795 81.34375, 0.00000000000000000000000002201390204972822546773735334456016283441279283217151993405440508 81.375, 0.00000000000000000000000002139405016023894045947697665215721324606211405710964283032366881 81.40625, 0.00000000000000000000000002079163015828752114218473560662103137169545278079047869909770112 81.4375, 0.00000000000000000000000002020615243390602099224164906996852622958933277787753066053369019 81.46875, 0.00000000000000000000000001963714111085305059229579841872601249678922732150256251144878118 81.5, 0.00000000000000000000000001908413366189715573160530817718444810315271557172782422702211045 81.53125, 0.00000000000000000000000001854668053486209895683913171397817839849879607216576517860810695 81.5625, 0.00000000000000000000000001802434478913342794119391779832964088272430231032608503773943201 81.59375, 0.00000000000000000000000001751670174233409882238088731130842267723153952273283875130572499 81.625, 0.00000000000000000000000001702333862688507393380782240558582013965881829313953935531843035 81.65625, 0.00000000000000000000000001654385425617473761081924071632969156825206037534771674387808907 81.6875, 0.00000000000000000000000001607785870006867730762224187358812260736900275074749661894958662 81.71875, 0.00000000000000000000000001562497296949886624773893691530069332994198384122099538372149151 81.75, 0.00000000000000000000000001518482870987856421833066977103500854989578824593308925324221928 81.78125, 0.00000000000000000000000001475706790309633070771761895671936828445762829665166534193896298 81.8125, 0.00000000000000000000000001434134257784942501598576882612743444227799987648656345887601751 81.84375, 0.00000000000000000000000001393731452808355672424470875495233038204296209022657568570898885 81.875, 0.00000000000000000000000001354465503931245232012524916710419821222523399993771276469149516 81.90625, 0.00000000000000000000000001316304462259702502869511015124073666497445044490667047167319504 81.9375, 0.00000000000000000000000001279217275597008002839623662246255506486014889666439838082105617 81.96875, 0.00000000000000000000000001243173763309846114019391010016206462988177048841881314397284705 82.0, 0.00000000000000000000000001208144591898035252813397952840439982506688633473962693065625303 82.03125, 0.00000000000000000000000001174101251248109457189393223113510135681823488360703729675176701 82.0625, 0.00000000000000000000000001141016031551636136902351890324609722524736943964890728576339232 82.09375, 0.00000000000000000000000001108862000869688267367883580850058443085572213075958040330136743 82.125, 0.0000000000000000000000000107761298332540797354459024139327584495820844199165816007616126 82.15625, 0.00000000000000000000000001047243537907102660379164593357269565501368455487988724407896672 82.1875, 0.00000000000000000000000001017728937864805003329620325657351359446303801840287457364658861 82.21875, 0.000000000000000000000000009890451506837046072881345767850172033729223912030316201247158268 82.25, 0.000000000000000000000000009611688186183223550872307748489192063818920468461120740070024587 82.28125, 0.000000000000000000000000009340772397717487673390666135388578623764657740297641379526877984 82.3125, 0.000000000000000000000000009077483497047054430040386070333997219497935774744048662549535482 82.34375, 0.000000000000000000000000008821607035596141942022945378507562346384840306441850859393465655 82.375, 0.000000000000000000000000008572934586852721690515071672555705897463517102606217698913335017 82.40625, 0.000000000000000000000000008331263577481334029781331974950279242394009094208748002409096309 82.4375, 0.000000000000000000000000008096397123165881730712566061281882514668984241105045351037394191 82.46875, 0.000000000000000000000000007868143869050115637686995076551756741116768076988880854104374503 82.5, 0.000000000000000000000000007646317834647220889494755872653871642485084714492475022285746018 82.53125, 0.000000000000000000000000007430738263093503503850827442378786202252602810802260306461420596 82.5625, 0.000000000000000000000000007221229474624668323039878015407055415118992498476844918262945988 82.59375, 0.000000000000000000000000007017620724156573154051113737022318737625764581620039664741012228 82.625, 0.000000000000000000000000006819746062855643121213709908296196130674296772075615906660227808 82.65625, 0.000000000000000000000000006627444203587336418507542114562234833143073806992470797030434863 82.6875, 0.00000000000000000000000000644055839013417036479693799109039901746996068292173888124453548 82.71875, 0.000000000000000000000000006258936270077847419371667219664886192902051645860866255829730315 82.75, 0.000000000000000000000000006082429771242967029268898100701587177915187897362620171503658454 82.78125, 0.000000000000000000000000005910894981602673208439490821221228899230324287952950300533619801 82.8125, 0.000000000000000000000000005744192032549371880985906218338186664431676371611625684502693702 82.84375, 0.000000000000000000000000005582184985436358481855954656706336117889288659098558483864879142 82.875, 0.000000000000000000000000005424741721298827262057239409069563167217142588343057760318285751 82.90625, 0.000000000000000000000000005271733833665291295022248462807867743925630056414124112264531904 82.9375, 0.000000000000000000000000005123036524372928371065180502020624473491646779164656391443564706 82.96875, 0.00000000000000000000000000497852850230278478591542487547758322125775739493570056088716392 83.0, 0.000000000000000000000000004838091884953118409780233957145663912649831619878393015612355666 83.03125, 0.000000000000000000000000004701612102771446244211549201162024394848702803256350657813072504 83.0625, 0.000000000000000000000000004568977806168081761893083350747513745736430129204168442802388035 83.09375, 0.000000000000000000000000004440080775136105455171995242050460681908030171725671356303901408 83.125, 0.000000000000000000000000004314815831404809919178977144852483736095256681317884738559204143 83.15625, 0.000000000000000000000000004193080753055700143123436944951955275637832973229936252343252159 83.1875, 0.000000000000000000000000004074776191532112110530943242529477654327247013781565697993476738 83.21875, 0.000000000000000000000000003959805590975439902121718105070165158398229005213290461457733559 83.25, 0.000000000000000000000000003848075109822834795885657177829406147643368630026287695849558328 83.28125, 0.000000000000000000000000003739493544603060866951719399142411105963420596193727708772363634 83.3125, 0.000000000000000000000000003633972255868961762616985023596153445467162275470350238338449262 83.34375, 0.000000000000000000000000003531425096206714082370624284692920184410166449047618535854643928 83.375, 0.000000000000000000000000003431768340263715506462790063695240802804650332358134595167787019 83.40625, 0.000000000000000000000000003334920616738581828731053947171025758314205708745044183304567131 83.4375, 0.000000000000000000000000003240802842278307661936447223209057105684315315211994147131522729 83.46875, 0.000000000000000000000000003149338157229182062468215232469718436020830102084775444944157795 83.5, 0.000000000000000000000000003060451863189543896410620655521334009028843469437287062217871411 83.53125, 0.000000000000000000000000002974071362313913636834022761533103343665188408852469933760662868 83.5625, 0.000000000000000000000000002890126098319449605683344451730761139684965532758232151021933365 83.59375, 0.000000000000000000000000002808547499147048583322615791577110423531236611723151200348723407 83.625, 0.00000000000000000000000000272926892123074430371246374956419639665396359427884289342899093 83.65625, 0.000000000000000000000000002652225595330353701806203069966043417850452595763364442232392916 83.6875, 0.000000000000000000000000002577354573883580920758257355437763980338855385224646348180876426 83.71875, 0.000000000000000000000000002504594679835014029732363703849028993000215706603915596490543502 83.75, 0.000000000000000000000000002433886456900640130144466757172661426576890786298334187522066866 83.78125, 0.000000000000000000000000002365172121227661993407909045953497434748360208092112398397446021 83.8125, 0.000000000000000000000000002298395514410524504421464616221267032908261195868666609529637959 83.84375, 0.000000000000000000000000002233502057825152884071335259170942884036554075335037116534057009 83.875, 0.000000000000000000000000002170438708244467807704649938194360474874460920527122463472340868 83.90625, 0.000000000000000000000000002109153914699275977251796524764995143330832006888835137249401614 83.9375, 0.000000000000000000000000002049597576549639271063978359976878664005355056796574322711726429 83.96875, 0.000000000000000000000000001991721002732802093145258283920350629028661503195056819089919482 84.0, 0.000000000000000000000000001935476872154705755410090073540566375863326215933675546901981824 84.03125, 0.000000000000000000000000001880819195193041414187612852337744062280450486843577792110815559 84.0625, 0.000000000000000000000000001827703276280689985513833542088081693318925506648271635870382305 84.09375, 0.000000000000000000000000001776085677539269302274551454427931486434702499391496746088209195 84.125, 0.000000000000000000000000001725924183433356249412588041568487533006459623300684235059705743 84.15625, 0.000000000000000000000000001677177766416775401132066510031000654436712384381383799857096185 84.1875, 0.000000000000000000000000001629806553543146447314138219826314354526104472093137024880733456 84.21875, 0.000000000000000000000000001583771794013661077778234589054132758596636684459573858283571079 84.25, 0.000000000000000000000000001539035827635816617250172703507282736470250023797493630343323424 84.28125, 0.000000000000000000000000001495562054167569178194962235884933377327219099249873358137952602 84.3125, 0.000000000000000000000000001453314903522084013376164028481870008613488289372306442252358966 84.34375, 0.000000000000000000000000001412259806808955679000754098917862222557278187380821054491559862 84.375, 0.000000000000000000000000001372363168188446120489483604368283998481764978966552959655140904 84.40625, 0.000000000000000000000000001333592337515945408618836638705858386242253713884287263314150243 84.4375, 0.000000000000000000000000001295915583754498111236050693304949482596048667878917881446660547 84.46875, 0.000000000000000000000000001259302069133858697481196758959347524051316483759487960805195664 84.5, 0.000000000000000000000000001223721824035142435700690880811965521101980234107131320151785641 84.53125, 0.000000000000000000000000001189145722580724447355906317155249472768980342279309735087555284 84.5625, 0.000000000000000000000000001155545458909609388066862078901143079399376768925813721962202585 84.59375, 0.000000000000000000000000001122893524119048101205230717443869349809673282651079522167980159 84.625, 0.00000000000000000000000000109116318385371597412236361478781938589922202205922829631211935 84.65625, 0.000000000000000000000000001060328456524291054719437066448143010441178225502459548776832578 84.6875, 0.000000000000000000000000001030364092137778677129855267194626422015391355399944564739131562 84.71875, 0.000000000000000000000000001001245551722423808555085412577978210441633315770562804966250375 84.75, 0.0000000000000000000000000009729489873305329621416746839519904686225305930172542614856273801 84.78125, 0.0000000000000000000000000009454512226029947094953647304670607783221998413866869118693025687 84.8125, 0.0000000000000000000000000009187297338797419465111124300315689735490619276215723843738171395 84.84375, 0.0000000000000000000000000008927626318408404826956383110292438814946697316820602466629644717 84.875, 0.0000000000000000000000000008675286436633175919307405375141013151852782753647691518117889579 84.90625, 0.0000000000000000000000000008430070956792612266321378622414227230812446049560876730953218356 84.9375, 0.0000000000000000000000000008191778965211259928390101854990355508905782405969435796563185357 84.96875, 0.0000000000000000000000000007960215207405760369085076361145922018390728194004928291850410588 85.0, 0.0000000000000000000000000007735189928875780220784916572109264701039792025933094449624659906 85.03125, 0.0000000000000000000000000007516518720368296832515083289029589289087087000066258904013557108 85.0625, 0.0000000000000000000000000007304022367489713404002308406631908466918638989022286350894298656 85.09375, 0.0000000000000000000000000007097526704543795161013954614984767003103706644001634142046768262 85.125, 0.0000000000000000000000000006896862472476837238516748259601839988513873044252570208045163187 85.15625, 0.0000000000000000000000000006701865180814798460893579864207545549291837611487405747394810181 85.1875, 0.0000000000000000000000000006512374973480365716487745954288470417868256686980192446296912352 85.21875, 0.0000000000000000000000000006328236498381053716568595290528853829237110352398495759657764976 85.25, 0.0000000000000000000000000006149298780662497133358676847116716211377662381901983023173123624 85.28125, 0.0000000000000000000000000005975415099524058884439133797475177013540443660351847197852868792 85.3125, 0.00000000000000000000000000058064428684967620595125077902060207880318133917363436217471842 85.34375, 0.000000000000000000000000000564224351908635599136215659036617577563861587423379002268307032 85.375, 0.0000000000000000000000000005482682387687051512330721820060898846287935753179723208959127684 85.40625, 0.0000000000000000000000000005327628605674108704191292228237363821778565882743849648327384854 85.4375, 0.0000000000000000000000000005176954992586034575123313411283126254513997747184639115681890129 85.46875, 0.0000000000000000000000000005030537952309650155330957417968426953073476484150174177545474882 85.5, 0.0000000000000000000000000004888257372183718507494396950193121600688354601065618099017664347 85.53125, 0.0000000000000000000000000004749996524939189058316540600244379014982814919930952396961474529 85.5625, 0.000000000000000000000000000461564197339641137683195574773720679038851226780486961196727763 85.59375, 0.000000000000000000000000000448508347784190490463821005119728975876316865357088809971378177 85.625, 0.0000000000000000000000000004358213906009441981876383031186517836844919643451999116584897309 85.65625, 0.0000000000000000000000000004234929145592311559477689603356224143654878535193350187139690468 85.6875, 0.0000000000000000000000000004115128019215681942925109855805206328022163132331788425858799003 85.71875, 0.0000000000000000000000000003998712201799974428099648791269893995104553694215011895533787674 85.75, 0.00000000000000000000000000038855861402480973720898776509886894766675940437798373298481861 85.78125, 0.0000000000000000000000000003775656975391273652679500112325359275884278393621107111533813644 85.8125, 0.0000000000000000000000000003668834466130025127519765955480366568570126339473798735691655256 85.84375, 0.0000000000000000000000000003565030915708657083276791964091219399759053274138161048228748857 85.875, 0.0000000000000000000000000003464161100063315200641890534729496134184635850731132128656226572 85.90625, 0.0000000000000000000000000003366142198185368647267283274613742206738479641890641684057269496 85.9375, 0.0000000000000000000000000003270893724443506902756618113113055314101283420062282696385203265 85.96875, 0.0000000000000000000000000003178337462809526135257334353294588885756660472180221691379607809 86.0, 0.0000000000000000000000000003088397402934324668625190935858586365763213075673889619522013105 86.03125, 0.000000000000000000000000000300099967802212754743903068723598797453578733114697487139918439 86.0625, 0.0000000000000000000000000002916072504452418634436672702054821269335702201487720838889972645 86.09375, 0.0000000000000000000000000002833546123100476237513172815715179301310729927798119837124579716 86.125, 0.0000000000000000000000000002753352742308786104698062608889420013838413223188053540379457822 86.15625, 0.0000000000000000000000000002675426482462944856987958890714309283788913793104689456918498675 86.1875, 0.0000000000000000000000000002599703322126968630977737488006180244584611200056359675150830632 86.21875, 0.0000000000000000000000000002526121045694186926139615560968632339928914237747506332409264492 86.25, 0.0000000000000000000000000002454619192511131416229391632334107405956372360320140196915635505 86.28125, 0.0000000000000000000000000002385139007433024783008127945871790692699253430066037143029831001 86.3125, 0.0000000000000000000000000002317623392770636427878148781544318892439668073783577707363880073 86.34375, 0.0000000000000000000000000002252016861589401150807738809242634886996377495121012484665623308 86.375, 0.0000000000000000000000000002188265492322794467516004668838030708365077518350097984679832722 86.40625, 0.000000000000000000000000000212631688466302505128930882956985725954211687674059482767028343 86.4375, 0.0000000000000000000000000002066120116693141696220438108966368645061852045549641410624660396 86.46875, 0.0000000000000000000000000002007625703225660041244992145565175662999737443501967159476061925 86.5, 0.0000000000000000000000000001950785555313793882838870377216371931392467938921487761510108899 86.53125, 0.0000000000000000000000000001895552940902328029630630589150663945872062454588689681985770054 86.5625, 0.0000000000000000000000000001841882446586095083354235813468952510531498314832303568116496899 86.59375, 0.0000000000000000000000000001789729940444918014906853089305541739360422298385817647096931637 86.625, 0.0000000000000000000000000001739052535924754668272760237308793867385923037589763942996621888 86.65625, 0.0000000000000000000000000001689808556735630074905543864598393786342923337924609906478093713 86.6875, 0.0000000000000000000000000001641957502737768383258862644460889480905303388998990992139515993 86.71875, 0.0000000000000000000000000001595460016788138969759361290099132803619366215614238254012095608 86.75, 0.0000000000000000000000000001550277852520411547204054705301245949558393728141221784419625186 86.78125, 0.000000000000000000000000000150637384303207345478178308894121583034289731717416891393216809 86.8125, 0.0000000000000000000000000001463711870453199413476666279412653034348627025055223139758247661 86.84375, 0.0000000000000000000000000001422256836372080457185064150975158009560434256516380522017106695 86.875, 0.0000000000000000000000000001381974633093615082480893834356692852601653720580155457005775932 86.90625, 0.0000000000000000000000000001342832115707042461417346355013043691812976366870503917386543662 86.9375, 0.000000000000000000000000000130479707494025537913401992929348420511758503248992005538946507 86.96875, 0.0000000000000000000000000001267838210778569923122704022258317450938845337684230250029506177 87.0, 0.0000000000000000000000000001231925106826450380692901588137650912273951177484726003369993703 87.03125, 0.0000000000000000000000000001197028205391291797910486137830000311140057145541159119332147152 87.0625, 0.0000000000000000000000000001163118783268949705444802316533625832301873818474535801394347587 87.09375, 0.0000000000000000000000000001130168928211277099073144703911593559269595347676244757752565723 87.125, 0.0000000000000000000000000001098151516056483336497650112356146485829293825227834963224979599 87.15625, 0.0000000000000000000000000001067040188503668626158662062334758397487568056226758476401104303 87.1875, 0.0000000000000000000000000001036809331513411673861396636519865893736668270638913256550551426 87.21875, 0.0000000000000000000000000001007434054316797243051083107431601042463145211544842239739809066 87.25, 0.00000000000000000000000000009788901690157652863993440679439510179861711557432147667335610215 87.28125, 0.00000000000000000000000000009511541707581443203982741246152042783169319128836809317069918084 87.3125, 0.00000000000000000000000000009242032184711992300906029433344761095196504634796967919803254725 87.34375, 0.0000000000000000000000000000898015116137978086194994905820199798016047190978665267997740919 87.375, 0.00000000000000000000000000008725682946011841994281970938019788144419616121860188248529654859 87.40625, 0.00000000000000000000000000008478417938797288842022738730868568638496137408703357603110521958 87.4375, 0.00000000000000000000000000008238152459835376035036735193588047725179507753677725239393688307 87.46875, 0.00000000000000000000000000008004688582125876563353052344432811201637129282826988836322314708 87.5, 0.00000000000000000000000000007777833969265496768647297593732223301519075187267763778565785911 87.53125, 0.00000000000000000000000000007557401717717882594243750977794692219769841558680183872969119694 87.5625, 0.00000000000000000000000000007343210203528493158754276818542834184180661635291365879403337464 87.59375, 0.00000000000000000000000000007135082933359236130160831210057250455505881912052162211950690081 87.625, 0.00000000000000000000000000006932848399721276206885445654199971058636831958909100007966298792 87.65625, 0.0000000000000000000000000000673633994028784610815835172538449208532521547859205770861858575 87.6875, 0.00000000000000000000000000006545395601172211606037111912266277104194905752741754254133620752 87.71875, 0.00000000000000000000000000006359858004059170986484843886916058274067336612272194365680437461 87.75, 0.00000000000000000000000000006179574217081607522380486173304981451475379328456629861385905918 87.78125, 0.00000000000000000000000000006004395629336663619370753235544984699783045791057531784020245982 87.8125, 0.00000000000000000000000000005834177828939069727057510492927540160887481600955426177230186372 87.84375, 0.00000000000000000000000000005668780484512042294925233497485277453052145956107057045847162618 87.875, 0.00000000000000000000000000005508067230018965329169397055093802544784424106736504828093923221 87.90625, 0.00000000000000000000000000005351905552841791742326364241777513617108186360692053033786866494 87.9375, 0.0000000000000000000000000000520016668501474588791525042257024798621746378130198185147596913 87.96875, 0.00000000000000000000000000005052725497524479580994690130898101568912554863332432902697109842 88.0, 0.00000000000000000000000000004909460397590332606378277712400695761335558755578593604079273911 88.03125, 0.00000000000000000000000000004770253228840777234640499871351360590034872635847543523216776479 88.0625, 0.00000000000000000000000000004634989174304486570644602714259177288446044454463041888349695409 88.09375, 0.00000000000000000000000000004503556662136760563651958652974157194973197272491058626119735319 88.125, 0.00000000000000000000000000004375847274004273072041250329562849048736061374402860440849093212 88.15625, 0.00000000000000000000000000004251755656053270307082080433959640009124635235515022022337465271 88.1875, 0.00000000000000000000000000004131179432388457036230820265386587107506462853253778031100153364 88.21875, 0.0000000000000000000000000000401401912099185381497542051398761913686851448657256284651947124 88.25, 0.00000000000000000000000000003900178052012897897426116968282714419606352137259681962196853669 88.28125, 0.00000000000000000000000000003789562288362993963191756064457186998911534937804824211541638112 88.3125, 0.00000000000000000000000000003682080548549599959945304742480956659100346120516023729740740204 88.34375, 0.00000000000000000000000000003577644131686759721867764147329434912514432028007361852238837058 88.375, 0.00000000000000000000000000003476166844620769065550023957239799781563604989835959253137147139 88.40625, 0.00000000000000000000000000003377564931111387227087510292481982975987002040476760907814029026 88.4375, 0.0000000000000000000000000000328175700301068218682262955889092611817264650569859729405117905 88.46875, 0.00000000000000000000000000003188663973383227992043860322308190148788702592531476293058633435 88.5, 0.00000000000000000000000000003098208991512955955373764503085151562351240334077218441513764395 88.53125, 0.0000000000000000000000000000301031737974350086294133288238881050552114609957945533062883651 88.5625, 0.00000000000000000000000000002924916572100379321078844144875752362262041843686487185674061837 88.59375, 0.00000000000000000000000000002841936054644791317531896945950345025141098692780443700602910099 88.625, 0.00000000000000000000000000002761307307510249153306037034073180322015484261231552866732688835 88.65625, 0.00000000000000000000000000002682963748574611261490353986219913348585618581114881709645967721 88.6875, 0.00000000000000000000000000002606840678721433184733048722068136181069547175480668975566794852 88.71875, 0.00000000000000000000000000002532875228645845217271676736790793148559183192643989172237984041 88.75, 0.00000000000000000000000000002461006307161426983932048033456784646451219022958608637390983132 88.78125, 0.00000000000000000000000000002391174550965774551160906189273128688185115803440461746589696973 88.8125, 0.00000000000000000000000000002323322275823646539097529103199410476962321731661127274797672475 88.84375, 0.00000000000000000000000000002257393429127733096163828154357817702324614716104632824438998035 88.875, 0.00000000000000000000000000002193333543798216448784755846160371341076586820414426816321791497 88.90625, 0.00000000000000000000000000002131089693483384962401579770000641771470507684365631196282360212 88.9375, 0.0000000000000000000000000000207061044902462513404613169242849694616165255414353891833428953 88.96875, 0.00000000000000000000000000002011845836150148544633956391398294626517702796442144878450890461 89.0, 0.0000000000000000000000000000195474729436281436982853166528786066007590049650810508560886834 89.03125, 0.00000000000000000000000000001899267636988383397316303683856146739457768748855768246275650114 89.0625, 0.00000000000000000000000000001845361012351487418229866190838683399936092351783887919991058079 89.09375, 0.00000000000000000000000000001792982866047519121649313187037810633143038855533281422838900734 89.125, 0.0000000000000000000000000000174208990427954297237584802042716389968586983789216880795142766 89.15625, 0.00000000000000000000000000001692640058230197721279795841417594591714228589005636283484415134 89.1875, 0.0000000000000000000000000000164459244943940689183468453790930557450209249637510058115464045 89.21875, 0.00000000000000000000000000001597907356159535493474163107385780270933028119580693851234026025 89.25, 0.00000000000000000000000000001552546180660430000375940399156858328332157199592455125332218415 89.28125, 0.00000000000000000000000000001508471417457554952666321600551012068921480810501053511814063658 89.3125, 0.00000000000000000000000000001465646622437194017108146682839182005340668399261528785740935444 89.34375, 0.00000000000000000000000000001424036382853416599630568569128280406699484487516339435455605985 89.375, 0.0000000000000000000000000000138360628817222372891270205056735197924689931970936743019309853 89.40625, 0.00000000000000000000000000001344322901738979508243630194138929957888308486961463290655247007 89.4375, 0.00000000000000000000000000001306153733245907525383699247056953707959349418960685811931698027 89.46875, 0.00000000000000000000000000001269067211977085764673568686631157669640116137485375972344561765 89.5, 0.00000000000000000000000000001233032660809009314340189424103355216008446660693764689631795392 89.53125, 0.00000000000000000000000000001198020270945408022060418570889155762662822190318958577497765432 89.5625, 0.00000000000000000000000000001164001077365606726075132605279114590130073875979652668824158185 89.59375, 0.00000000000000000000000000001130946934966299266102229515393003384328083178691719420013275383 89.625, 0.00000000000000000000000000001098830495377174632864991054153668813285429872998636414487487096 89.65625, 0.00000000000000000000000000001067625184431384808792886880451905940372288609829940650745311736 89.6875, 0.00000000000000000000000000001037305180272379533969446498067769997268722050497563685628653979 89.71875, 0.00000000000000000000000000001007845392079153836708296185969744349404496226471865489962962491 89.75, 0.000000000000000000000000000009792214393924601210064394400183569635920997411501617768850852872 89.78125, 0.000000000000000000000000000009514096320250283154296401381747192877881813692160628723331387892 89.8125, 0.00000000000000000000000000000924386950539315460041854987229980774712705060622352729823636935 89.84375, 0.00000000000000000000000000000898131027276770528873955514914810346540782223588253735582278901 89.875, 0.000000000000000000000000000008726201279230516332878704504034662774950360355506921037841966245 89.90625, 0.000000000000000000000000000008478331335940713939649241827489516532545251748085137910031428865 89.9375, 0.000000000000000000000000000008237495234281725633461707946888926205985415258737674652123038056 89.96875, 0.000000000000000000000000000008003493576701502733623824733987538391832912425150504467472912751 90.0, 0.000000000000000000000000000007776132612332399126475960196478416745271402961446526786412337749 90.03125, 0.000000000000000000000000000007555224077255809310927809370129796898816575410094730297189516153 90.0625, 0.000000000000000000000000000007340585039280471463155050573704515003341131237841163627262806556 90.09375, 0.000000000000000000000000000007132037747107036961961730371886562447741432025081308830829758682 90.125, 0.000000000000000000000000000006929409483755099451597837522338020690923150586602721155388375538 90.15625, 0.000000000000000000000000000006732532424132367020060517130596342087617111773832639605415127054 90.1875, 0.000000000000000000000000000006541243496629053282327825423114204226210961388746787321397824683 90.21875, 0.000000000000000000000000000006355384248623859843915050853362494315961063732820512745291202492 90.25, 0.00000000000000000000000000000617480071579112646729855248954085552098939268592771856472117935 90.28125, 0.000000000000000000000000000005999343295101838883345912148168858706244322784111900657031806078 90.3125, 0.000000000000000000000000000005828866621414210119798154284961276062649153751236343716595769957 90.34375, 0.000000000000000000000000000005663229447552491925650886338715696415815155780531506739816950053 90.375, 0.000000000000000000000000000005502294527775530751264434757793398873030675630458941172826930011 90.40625, 0.000000000000000000000000000005345928504539360129137414015419433920764939293654598069603689758 90.4375, 0.000000000000000000000000000005194001798460820453983326490869035787574142667598509848173071693 90.46875, 0.000000000000000000000000000005046388501391820283912717527370972752770375422423789058480228614 90.5, 0.000000000000000000000000000004902966272516402516163579503089326129068443174359292828758450107 90.53125, 0.000000000000000000000000000004763616237385256209857276196110593330316434669657615212172349478 90.5625, 0.000000000000000000000000000004628222889804722455187650324296323545054739228952838736830331434 90.59375, 0.000000000000000000000000000004496673996499682487002121088847703268896586362032719399495094532 90.625, 0.00000000000000000000000000000436886050447199011944729492245055225201595843848205564663643407 90.65625, 0.000000000000000000000000000004244676450978320392135298387410355241496961146791079731016472603 90.6875, 0.000000000000000000000000000004124018876053453869918960912160510772406785579637567276047667776 90.71875, 0.000000000000000000000000000004006787737507103079956356546849498539929261254655705998065711927 90.75, 0.000000000000000000000000000003892885828324415804166344001519019804526126508407525085389320871 90.78125, 0.000000000000000000000000000003782218696402261027433193188445177183714206722353591179238147918 90.8125, 0.000000000000000000000000000003674694566555318880498491580753686517633350487594574045443775271 90.84375, 0.000000000000000000000000000003570224264727857474658210455601376605993730963034460518688308306 90.875, 0.000000000000000000000000000003468721144348888622494807681063522673159996232816700517116896957 90.90625, 0.000000000000000000000000000003370101014770152551541713163125685243741138086542526897429607097 90.9375, 0.000000000000000000000000000003274282071728090281113129542964817236908525209030609949514979205 90.96875, 0.000000000000000000000000000003181184829772622741303338090820657636382403614110516925962318761 91.0, 0.000000000000000000000000000003090732056607169322921843743019994848410503891803108197994442422 91.03125, 0.000000000000000000000000000003002848709285906675327592219798750569427839853178322808316946167 91.0625, 0.000000000000000000000000000002917461872215792496159785616757079238874029337481776654038200079 91.09375, 0.000000000000000000000000000002834500696912360027236032254064686512007503765136735741157335465 91.125, 0.000000000000000000000000000002753896343459728193812038872776161635686361418583457734816048724 91.15625, 0.000000000000000000000000000002675581923626670975374553919900434853770447069718038840648226046 91.1875, 0.000000000000000000000000000002599492445591948817522019908372050901291700446120459029225118603 91.21875, 0.000000000000000000000000000002525564760233425796502317258128880582735584740666063304279852165 91.25, 0.000000000000000000000000000002453737508936779909628931414697038490530389870006714834830152818 91.28125, 0.000000000000000000000000000002383951072880861334745533824835017574391890603067819414572177356 91.3125, 0.000000000000000000000000000002316147523757965799321169338581734698724914809683047729304118702 91.34375, 0.000000000000000000000000000002250270575888468315136378092660443875036589334380993431350636512 91.375, 0.000000000000000000000000000002186265539690407430507764787431681487719632410512204266103141812 91.40625, 0.000000000000000000000000000002124079276465722764161186069729563046204503083618593767057263401 91.4375, 0.000000000000000000000000000002063660154465929822452216611004576840545427268107703667359867043 91.46875, 0.000000000000000000000000000002004958006201066848320459806292473606316176233149377465245527024 91.5, 0.000000000000000000000000000001947924086956769564966944800142561236389776278845929269543258795 91.53125, 0.00000000000000000000000000000189251103448532199441681151132680608845298930717058012095163063 91.5625, 0.000000000000000000000000000001838672829837495862047193789154795335389911383102050527958343455 91.59375, 0.000000000000000000000000000001786364759302928231175828210190530512552976814750361937741574695 91.625, 0.000000000000000000000000000001735543377427697713092531807783998700412522941351575830799743417 91.65625, 0.000000000000000000000000000001686166471078644612089563936112446152161494336751230866709406799 91.6875, 0.000000000000000000000000000001638193024524840415773191037835899140979466867945185779930923955 91.71875, 0.000000000000000000000000000001591583185507447831520351819830079778318697995106650431415356066 91.75, 0.000000000000000000000000000001546298232270024783893912888943212492009341486699821354106689308 91.78125, 0.000000000000000000000000000001502300541522115089426412275774352165445700282172892771011258324 91.8125, 0.00000000000000000000000000000145955355730973556001083131265650153908893198734741394663347947 91.84375, 0.000000000000000000000000000001418021760767114681615239862279347578020452667731559870267432422 91.875, 0.000000000000000000000000000001377670640724762380926194936598035868695113236323762716970069841 91.90625, 0.000000000000000000000000000001338466665149654321422555100450901086834841225749133309645800375 91.9375, 0.000000000000000000000000000001300377253393998238207240081215421488905597196903284090987860103 91.96875, 0.000000000000000000000000000001263370749229714587389906166502438659062083683180196341889441658 92.0, 0.000000000000000000000000000001227416394646409794874834746787529586852756482498876134728025916 92.03125, 0.000000000000000000000000000001192484304391248169710554540573531792914156600128953304388785326 92.0625, 0.000000000000000000000000000001158545441229738612464991060690987967194254425588610917899434982 92.09375, 0.000000000000000000000000000001125571591907045098703913813638770150311137848133705087823356111 92.125, 0.000000000000000000000000000001093535343790006036816898797824525837252184657810922406438584607 92.15625, 0.000000000000000000000000000001062410062170607459739013882954593265680166874875492112198484174 92.1875, 0.000000000000000000000000000001032169868212199069866444452309238050348532543683924091704294543 92.21875, 0.000000000000000000000000000001002789617520270861065498851202334390235883875820665915581098756 92.25, 0.0000000000000000000000000000009742448793201218239916044301854156808894662133772197982993927716 92.28125, 0.0000000000000000000000000000009465119162242515216449452742373065286698273245198736231791527425 92.3125, 0.0000000000000000000000000000009195676645727905100241408761099987445933814256458195023876023523 92.34375, 0.0000000000000000000000000000008933897153307570712186884984808333877860439834735348425795417194 92.375, 0.0000000000000000000000000000008679562955263859094364409122021632449358954320226638963465397592 92.40625, 0.0000000000000000000000000000008432462502152197095522590859286671911089633787079382836092317276 92.4375, 0.0000000000000000000000000000008192390249550871374785254575320380953193095117447892150849639065 92.46875, 0.0000000000000000000000000000007959146487775113264172846085047275406130485143454185113025866029 92.5, 0.0000000000000000000000000000007732537176415014872956318326440342092949765176370917337767346697 92.53125, 0.0000000000000000000000000000007512373783560773401479814303896891101511445935441156019129632602 92.5625, 0.0000000000000000000000000000007298473129582619112004285908085421096872644355930765200344210233 92.59375, 0.0000000000000000000000000000007090657235336531940802260772633279809003580701113136670994073635 92.625, 0.0000000000000000000000000000006888753174670495401590885265331893734189725358441019916733661617 92.65625, 0.0000000000000000000000000000006692592931109577211297968261551284052242486564289426036182956428 92.6875, 0.00000000000000000000000000000065020132586015668668759325520623606947757798732139498308563597 92.71875, 0.0000000000000000000000000000006316855546208244036429724894791443564613877452635779569861283654 92.75, 0.0000000000000000000000000000006136965686630600839881600231298065397577594531928896574939012173 92.78125, 0.0000000000000000000000000000005962193948459498547124704179737236169912300969676568470978438607 92.8125, 0.0000000000000000000000000000005792394852046307503449088902138875827852809268862894052536124589 92.84375, 0.0000000000000000000000000000005627427048891060718364321876762449772875039380983693617301591263 92.875, 0.0000000000000000000000000000005467153204448548969325929234750955350572838666641644530964794362 92.90625, 0.0000000000000000000000000000005311439884255600851954340772838151508331757963532666540939614414 92.9375, 0.0000000000000000000000000000005160157443285527261830954172950110576351241129319624160011690222 92.96875, 0.0000000000000000000000000000005013179918438368563526863442944667153585857973244928027965486696 93.0, 0.0000000000000000000000000000004870384924078166370601347194900160785468616526198947100965736352 93.03125, 0.0000000000000000000000000000004731653550530992544889685055575670875626710880985090730583947287 93.0625, 0.0000000000000000000000000000004596870265459907783757656225068866834080015312488756275623940357 93.09375, 0.000000000000000000000000000000446592281803539300137247942359225940111214869949792795467938988 93.125, 0.0000000000000000000000000000004338702145822100569264772847944298280303656151441057501728944337 93.15625, 0.000000000000000000000000000000421510228430501125255727765578796956669767679423330894383486135 93.1875, 0.0000000000000000000000000000004095020278980258197966680660214136933900999126117388796622426594 93.21875, 0.0000000000000000000000000000003978356099937993383026755613132500265486398322391797797427702048 93.25, 0.000000000000000000000000000000386501255886672625760233308708846252667463889675139116574435355 93.28125, 0.0000000000000000000000000000003754895228410560584435162151272523411987227483024994093650010254 93.3125, 0.0000000000000000000000000000003647912363812695353434798877339350114701262038323504122243625096 93.34375, 0.0000000000000000000000000000003543974826780440696767853691741030113024338111787494072197881204 93.375, 0.0000000000000000000000000000003442996011508831515692535247544888520650383010818077282020890822 93.40625, 0.0000000000000000000000000000003344891772801701549099163414341322063702638768795341556168726134 93.4375, 0.0000000000000000000000000000003249580356230810329027566179766319160989407140494513194158433547 93.46875, 0.0000000000000000000000000000003156982330275296300018738973815904683614048677105149215330705945 93.5, 0.0000000000000000000000000000003067020520385362706963988197730549251708115029148912272560870502 93.53125, 0.0000000000000000000000000000002979619944915690021174084528094132184880778846937910684300423395 93.5625, 0.0000000000000000000000000000002894707752875610979924487076364053610427926434305130616115919998 93.59375, 0.0000000000000000000000000000002812213163444583027212874398033301631511452331393541477253306299 93.625, 0.0000000000000000000000000000002732067407202949293651067823818944984233157202391781971673796067 93.65625, 0.0000000000000000000000000000002654203669029394437356496527824895447683599822810755409422688609 93.6875, 0.0000000000000000000000000000002578557032617876847730258238222825520497354609932894625051816511 93.71875, 0.0000000000000000000000000000002505064426568155019651367385614322608239739963326829644505805842 93.75, 0.000000000000000000000000000000243366457200532443456526577233064828028274473745495022366265355 93.78125, 0.0000000000000000000000000000002364297931685043103925305835949786092039948963107571587504721144 93.8125, 0.0000000000000000000000000000002296906660542350076097931714193425913827291066642499281218706246 93.84375, 0.0000000000000000000000000000002231434557643172687565519112596648707285269094140461834114418697 93.875, 0.0000000000000000000000000000002167827019498776132039485574421967292913356947055178834689942236 93.90625, 0.0000000000000000000000000000002106030994704533978299459258297241507758909056681045022779854914 93.9375, 0.000000000000000000000000000000204599493986549151373406127933243872762459600474605556099814895 93.96875, 0.0000000000000000000000000000001987668776772256124126374718798455429276234256048496795126119517 94.0, 0.0000000000000000000000000000001931003850791781214307563537901209376877480000249274337838376772 94.03125, 0.0000000000000000000000000000001875952890438613277368631921505105416537376392452491486271133396 94.0625, 0.0000000000000000000000000000001822469968093146456708472784998882843727017900521021182600823807 94.09375, 0.0000000000000000000000000000001770510461834376116578416067584135928592351059550338316181039985 94.125, 0.0000000000000000000000000000001720031018355563321624069938960369043139540469116976203529768223 94.15625, 0.0000000000000000000000000000001670989516932116480918425906763886228740794434318442256808232148 94.1875, 0.0000000000000000000000000000001623345034411865472468685404763579903294003219040792069436043406 94.21875, 0.0000000000000000000000000000001577057811198748044756164889257684570589803145749288368623025842 94.25, 0.0000000000000000000000000000001532089218201748886962257019257179097536347370050559488114681662 94.28125, 0.0000000000000000000000000000001488401724721729143975630363761244437879181812751221762999269133 94.3125, 0.0000000000000000000000000000001445958867249558981854408621842762176560866447638584991706221675 94.34375, 0.0000000000000000000000000000001404725219149718721412459371274060436668012095335281916743245959 94.375, 0.0000000000000000000000000000001364666361204265671306014842765914160417153013508958382607453581 94.40625, 0.0000000000000000000000000000001325748852992774709231913264594813165839168867500788715466726715 94.4375, 0.0000000000000000000000000000001287940205084551465442182513730049651916027219458609670134467261 94.46875, 0.0000000000000000000000000000001251208852020088225054858210245430381494751884869488279209284091 94.5, 0.0000000000000000000000000000001215524126059384936879552429107532990738005772520782127124047356 94.53125, 0.0000000000000000000000000000001180856231675391533370695561348833355458397841936574838951195947 94.5625, 0.0000000000000000000000000000001147176220771443650414033918223776821622472771149995369331813288 94.59375, 0.0000000000000000000000000000001114455968602162293762646683701586270102491307043673837229340159 94.625, 0.0000000000000000000000000000001082668150377869523579634892895141013525828527055796795623556252 94.65625, 0.0000000000000000000000000000001051786218533137298323772393250852454770722438529868117981762068 94.6875, 0.0000000000000000000000000000001021784380640635699227758590362481672458243538004661495221288927 94.71875, 0.00000000000000000000000000000009926375779519802988311925368762904435253253913212407678412845602 94.75, 0.00000000000000000000000000000009643214645477968806461649131504954301846337641029170020823975684 94.78125, 0.00000000000000000000000000000009368123870797254888557230645888142059211143261680947872586123753 94.8125, 0.00000000000000000000000000000009100873650875753017265365733780703931347920306309930404820728861 94.84375, 0.00000000000000000000000000000008841240718753174831977358862829672935961483985392002474541058138 94.875, 0.00000000000000000000000000000008589008159300653655480054111888822082447275411142479029255749954 94.90625, 0.0000000000000000000000000000000834396522868640432742508744857927227017679266841151732850678079 94.9375, 0.0000000000000000000000000000000810590717896758978872020517709957056320624242819477235944579117 94.96875, 0.0000000000000000000000000000000787463508766298368425808397559352594950276642569061301612436922 95.0, 0.00000000000000000000000000000007649955692165138742442076515309296964658877044005023367682585687 95.03125, 0.00000000000000000000000000000007431681228854774542694293307252202757536565895306375034282830839 95.0625, 0.00000000000000000000000000000007219629276783988794612832411301273619099159941035441025824037344 95.09375, 0.00000000000000000000000000000007013622605798676629603104936834973164881539928122826542414214037 95.125, 0.00000000000000000000000000000006813489028974215762040293210380600676990282220707698291008157801 95.15625, 0.00000000000000000000000000000006619061259242044738753073010934854584906807188290890742387063299 95.1875, 0.00000000000000000000000000000006430176770088229803791690742168335794180510437334427078211583298 95.21875, 0.000000000000000000000000000000062466776602084860179190046884967067401610859615574022047908973 95.25, 0.00000000000000000000000000000006068410522007392966171758826992751924549570559407586376801392039 95.28125, 0.00000000000000000000000000000005895226313832727360857621597052765812326738510310389669447993627 95.3125, 0.00000000000000000000000000000005726980235838926723917526948252272291100462878800724502809144318 95.34375, 0.00000000000000000000000000000005563531609376702660039807375962806571985640026406532608121425521 95.375, 0.00000000000000000000000000000005404743759808741486611544533544084262913358686536543523079984013 95.40625, 0.0000000000000000000000000000000525048390265426657491617656139212726536371210580991193047616244 95.4375, 0.00000000000000000000000000000005100623032967993017325601678164474010740798815636912084925445081 95.46875, 0.00000000000000000000000000000004955035817861683439907826445513185219389209226289747593571199425 95.5, 0.00000000000000000000000000000004813600492079116136987563537161007056018833027472849627249759104 95.53125, 0.00000000000000000000000000000004676198756537805359471180063677784755256294415799079071544056951 95.5625, 0.00000000000000000000000000000004542715679753270627258425361369279827139596505717494521365251907 95.59375, 0.00000000000000000000000000000004413039602064039383967613795852981846015708021055602882464340564 95.625, 0.00000000000000000000000000000004287062042577887138394629111421521309639988331735691509129865058 95.65625, 0.00000000000000000000000000000004164677608762073354861865348567698993322980974807588986295437362 95.6875, 0.00000000000000000000000000000004045783908602521623070802025201233322597104542607453701001294068 95.71875, 0.00000000000000000000000000000003930281465259020863884754248625074516671933292418278991398362825 95.75, 0.00000000000000000000000000000003818073634145592266206960575231727591328274828636330114781953304 95.78125, 0.00000000000000000000000000000003709066522367176007732797317375377086537579344965512330333062915 95.8125, 0.00000000000000000000000000000003603168910445744246572428467397135688399812316991711635662972897 95.84375, 0.00000000000000000000000000000003500292176270843992431625950445264240319406778583838416599928623 95.875, 0.00000000000000000000000000000003400350221211416840538014442122653118178810213585038196224210822 95.90625, 0.00000000000000000000000000000003303259398327533699891721579838671874859840073065618688929364291 95.9375, 0.00000000000000000000000000000003208938442622423047786869054672162708189546646088287046888582671 95.96875, 0.00000000000000000000000000000003117308403276862331190694905448192953786041832370631829565080518 96.0, 0.00000000000000000000000000000003028292577809645308171170985367089716730047830708681539482092981 96.03125, 0.00000000000000000000000000000002941816448109434735362949616913495336695668923921954932146540279 96.0625, 0.0000000000000000000000000000000285780761828486117834124351520923003578502008929151220645495121 96.09375, 0.00000000000000000000000000000002776195754281236131357375846171534852007299579917523798529898896 96.125, 0.00000000000000000000000000000002696912525213712325591839200429490055364256917229802802728581925 96.15625, 0.00000000000000000000000000000002619891546368147290127120336059508386259730727467192157958517462 96.1875, 0.00000000000000000000000000000002545068323822309082266215129689517597150658858986041493218641124 96.21875, 0.00000000000000000000000000000002472380200641406765429002404118998751402368754454011562318650753 96.25, 0.00000000000000000000000000000002401766304603233793163746323343253769556610742402852148163060697 96.28125, 0.00000000000000000000000000000002333167497409481034954266939599052702917677548484071180431654893 96.3125, 0.00000000000000000000000000000002266526325341008801133446017033039693801004899876230278463954414 96.34375, 0.00000000000000000000000000000002201786971316064908350121786078407683352795888747903639735131303 96.375, 0.00000000000000000000000000000002138895208311599562906217488330244022470034580441487092911596647 96.40625, 0.00000000000000000000000000000002077798354108958588132447136309135131418521038412643362593863837 96.4375, 0.00000000000000000000000000000002018445227326335217864066733848990643170281535676035041872614587 96.46875, 0.00000000000000000000000000000001960786104701428228652578130533078627162300926342433061744091559 96.5, 0.00000000000000000000000000000001904772679588791470570977511108633755213744413427258815186604214 96.53125, 0.00000000000000000000000000000001850358021637367737356655271167321174660900706845022211013279641 96.5625, 0.00000000000000000000000000000001797496537614679223961570370721293921886234479026837983562474628 96.59375, 0.00000000000000000000000000000001746143933345098362559639158720188989999014333370930101025368277 96.625, 0.00000000000000000000000000000001696257176730547393019527171869583829126286550039003503454176268 96.65625, 0.00000000000000000000000000000001647794461822873374864590616367875613891147470734594476945574404 96.6875, 0.00000000000000000000000000000001600715173918018227270862143757833969047748762655695742922499493 96.71875, 0.00000000000000000000000000000001554979855642951513152804657258548620865680620296607515353008409 96.75, 0.00000000000000000000000000000001510550174007157763896634930901056339092024443578384537948472845 96.78125, 0.00000000000000000000000000000001467388888391270854028295581298301192093986066268482040990007719 96.8125, 0.00000000000000000000000000000001425459819446225941978581006841027946038838396903117702686783379 96.84375, 0.00000000000000000000000000000001384727818877055437332246473843596422158287278976538774857847794 96.875, 0.00000000000000000000000000000001345158740086189961521102394813019606023569358064510992500459532 96.90625, 0.00000000000000000000000000000001306719409651838945156742577047495724073194366760442724862510291 96.9375, 0.00000000000000000000000000000001269377599617718941223911809550816140279386613603296328177123503 96.96875, 0.00000000000000000000000000000001233102000571071502597255868398173444034429316213639618190632181 97.0, 0.00000000000000000000000000000001197862195486567132000528532626525114680646152078455278739694616 97.03125, 0.00000000000000000000000000000001163628634314327904026784904062598582629565893581077081316959447 97.0625, 0.00000000000000000000000000000001130372609290919408285334766756251938350071130387041633554154819 97.09375, 0.00000000000000000000000000000001098066230952763181350298610115422543852086022120799623362604471 97.125, 0.00000000000000000000000000000001066682404832004279700901766663995118244277781860356805489239685 97.15625, 0.00000000000000000000000000000001036194808815435578949030529577103650373186229646629243727234841 97.1875, 0.00000000000000000000000000000001006577871147631235365955401768316901483956943989654869472970255 97.21875, 0.000000000000000000000000000000009778067490599769697917492206142615025400007357791605089526285519 97.25, 0.000000000000000000000000000000009498573080078048742808056683734049569060343332011670212510335081 97.28125, 0.000000000000000000000000000000009227061014983457286115867971293187584618433544241120316447456463 97.3125, 0.000000000000000000000000000000008963303514927027651929697683947142281311199960080625761718294326 97.34375, 0.000000000000000000000000000000008707079293655278432205721073628258683669843258236391509151578363 97.375, 0.000000000000000000000000000000008458173374065444809587984757061764817805200032796041661608801004 97.40625, 0.000000000000000000000000000000008216376908485125323553009002723920804146162088797938593255371621 97.4375, 0.000000000000000000000000000000007981487004066668535800686333277183262442038181202714008029482871 97.46875, 0.000000000000000000000000000000007753306553150874487133984401691827655733430361006388951089005042 97.5, 0.000000000000000000000000000000007531644068458716636219283890286083327188124695979928864313554493 97.53125, 0.000000000000000000000000000000007316313522973803550306613703558469853854170430479506820107081085 97.5625, 0.000000000000000000000000000000007107134194382199302087760145152137786046869071047262133656551489 97.59375, 0.000000000000000000000000000000006903930513940010544156581037435222673863773169202630814285149117 97.625, 0.000000000000000000000000000000006706531919642829720351136079017551998390719108436987850497759331 97.65625, 0.000000000000000000000000000000006514772713574700880094203111895059862369536282867107682457765092 97.6875, 0.000000000000000000000000000000006328491923317750049865053280580523690407401360489487154824698176 97.71875, 0.000000000000000000000000000000006147533167306998963396012849483371320464633196544079684921126166 97.75, 0.000000000000000000000000000000005971744524018161955838096853570666366742756231622156455274210467 97.78125, 0.000000000000000000000000000000005800978404879413704513688955931427117403997774177222832831617254 97.8125, 0.000000000000000000000000000000005635091430801212890531925143437662779526886800685730047425346126 97.84375, 0.000000000000000000000000000000005473944312221276327251228241730060550543291651698540304005217049 97.875, 0.000000000000000000000000000000005317401732564722146422763703473925309446730772086988720127715155 97.90625, 0.000000000000000000000000000000005165332235022241673306642138917153370701256871466189248567713675 97.9375, 0.000000000000000000000000000000005017608112551920011964776570193914578234296948952066789355442372 97.96875, 0.000000000000000000000000000000004874105301013007388464652767055858425431386604429198239851855383 98.0, 0.000000000000000000000000000000004734703275342549185247031349619044135893184194461534277752279366 98.03125, 0.000000000000000000000000000000004599284948688314503827432907863779820052379054288442409500272647 98.0625, 0.000000000000000000000000000000004467736574413923113547130338045068439015682492382940184278057949 98.09375, 0.000000000000000000000000000000004339947650894460820047976364845563937199588764260995488181129894 98.125, 0.000000000000000000000000000000004215810829023195599535610960795480507948357178815114532657831547 98.15625, 0.000000000000000000000000000000004095221822352263218606401601308030851072095928855806004922961784 98.1875, 0.000000000000000000000000000000003978079319792383364792626867688271978013167517163273668167026323 98.21875, 0.000000000000000000000000000000003864284900798797367378224707685839238802675933669131955247354651 98.25, 0.00000000000000000000000000000000375374295297268815730685936887810551350170113120768132400879687 98.28125, 0.000000000000000000000000000000003646360592009353914958452755200198084053437395987634875044646289 98.3125, 0.000000000000000000000000000000003542047583926360552399868654230629846777253988078019353703959327 98.34375, 0.000000000000000000000000000000003440716269506796392363611941617839497631495720133992211470354929 98.375, 0.000000000000000000000000000000003342281490894596713709488972332013723713978411315897800606751215 98.40625, 0.000000000000000000000000000000003246660520280697761902937834130114734342594257193111549451857463 98.4375, 0.000000000000000000000000000000003153772990620520859178369157552917973236691585650622417790393778 98.46875, 0.000000000000000000000000000000003063540828324978836505193148986505520575199701891487610976171282 98.5, 0.000000000000000000000000000000002975888187868840551270234000679567740102122330680938605215738112 98.53125, 0.000000000000000000000000000000002890741388261886113996875977211135699825751193825859604591460048 98.5625, 0.000000000000000000000000000000002808028851329836949060424677710153716356575734194434072787970694 98.59375, 0.000000000000000000000000000000002727681041753552245304795699581641288874189540970587909835578483 98.625, 0.000000000000000000000000000000002649630408816447963307085479171852250125943432061558311820943637 98.65625, 0.000000000000000000000000000000002573811329811517571911632630766181858259053929324982282713716061 98.6875, 0.000000000000000000000000000000002500160055060716268254030759144819846560725790103304420430212676 98.71875, 0.000000000000000000000000000000002428614654500813740036304329357567221304714286668228019675048992 98.75, 0.000000000000000000000000000000002359114965791125670991041274220185757664594302649985552628342792 98.78125, 0.000000000000000000000000000000002291602543899802253375565706834805857027001797634340819572068441 98.8125, 0.000000000000000000000000000000002226020612126584007350099625658544022462982872360502855221120555 98.84375, 0.000000000000000000000000000000002162314014521132238760891882615414918886593322128433134398606195 98.875, 0.000000000000000000000000000000002100429169657204487721915050416343952985559688473720226775148488 98.90625, 0.000000000000000000000000000000002040314025724075295846426741394771834104013152981138597571666588 98.9375, 0.000000000000000000000000000000001981918016897700488027968583842690388596323169099591218358649059 98.96875, 0.000000000000000000000000000000001925192020955189836716163930562225535362561967514684146112011647 99.0, 0.000000000000000000000000000000001870088318097189338237784543944787545735125113166192044815460831 99.03125, 0.000000000000000000000000000000001816560550943781242326812762187280984407834391780963066441340191 99.0625, 0.000000000000000000000000000000001764563685670488273695888993199985822435630249006921786503726003 99.09375, 0.000000000000000000000000000000001714053974251918980542585506328461566668107926656286999961700132 99.125, 0.000000000000000000000000000000001664988917781514628635811400167330569566552948691263795123279526 99.15625, 0.000000000000000000000000000000001617327230836755297982913310735734459412400061062611598687445813 99.1875, 0.000000000000000000000000000000001571028806860054577197674044762753853685659350141347892032116057 99.21875, 0.000000000000000000000000000000001526054684526419212599099074881064603197039774751034554010999274 99.25, 0.000000000000000000000000000000001482367015069772958259829007287302551159067397684021485284916112 99.28125, 0.000000000000000000000000000000001439929030540643373225871480470410831780402451508794072195604152 99.3125, 0.000000000000000000000000000000001398705012968687087091619591970012372861655278387704879183817091 99.34375, 0.000000000000000000000000000000001358660264404283750341614472405563773562745691683756015331875379 99.375, 0.000000000000000000000000000000001319761077814162128363173960263174677068698156616159369950960001 99.40625, 0.000000000000000000000000000000001281974708806734197004721088304653631670148291935197207841810792 99.4375, 0.000000000000000000000000000000001245269348163505244933268736525113800936644335949652876399909465 99.46875, 0.000000000000000000000000000000001209614095153600458967706902200312261536071334373264476028861817 99.5, 0.000000000000000000000000000000001174978931609101821851683109276813729177889261555187425876032039 99.53125, 0.000000000000000000000000000000001141334696739523930545543610600373358405436405737421506840168543 99.5625, 0.000000000000000000000000000000001108653062664374074620154264309950322091169292153860173547425779 99.59375, 0.000000000000000000000000000000001076906510643341111316794761529912767493168977505012639598313852 99.625, 0.000000000000000000000000000000001046068307984239834342559548572068759010286655209649903613338751 99.65625, 0.000000000000000000000000000000001016112485609403141413406433510772616478658936807214037223530401 99.6875, 0.0000000000000000000000000000000009870138162617638311191307561770914984641363991548873870821747541 99.71875, 0.0000000000000000000000000000000009587477933324017597056814425512654097981212636673196860589698939 99.75, 0.0000000000000000000000000000000009312906102918508067257889247586518556244796358573009315423220297 99.78125, 0.0000000000000000000000000000000009046191407079640664776183167361697934507659826426834720760677445 99.8125, 0.0000000000000000000000000000000008787109188336253187723414824063067614209274151950152449528567651 99.84375, 0.0000000000000000000000000000000008535441207480705449918566660011976422823130361834495194844113719 99.875, 0.0000000000000000000000000000000008290975460360454392087339874112350724981012217744377010515369604 99.90625, 0.0000000000000000000000000000000008053505999894739037054527516779590944497593488607015556365682242 99.9375, 0.0000000000000000000000000000000007822832763167487870033518088329221536718764753543437178962056912 99.96875, 0.0000000000000000000000000000000007598761403451799833475989993070011342646763742246374521052379919 100.0, 0.0000000000000000000000000000000007381103127025468180475586912426822097934310103922186442699630708 ././@LongLink100644 0 0 156 12126630646 10263 Lustar 0 0 commons-math3-3.2-src/src/test/resources/org/apache/commons/math3/distribution/gamma-distribution-shape-1.csvcommons-math3-3.2-src/src/test/resources/org/apache/commons/math3/distribution/gamma-distribution-sh100644 1750 1750 1140234 12126627677 32616 0ustarlucluc 0 0 # # Licensed to the Apache Software Foundation (ASF) under one or more # contributor license agreements. See the NOTICE file distributed with # this work for additional information regarding copyright ownership. # The ASF licenses this file to You 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. 0.03125, 0.9692332344763440818481091932463527836047257538965551706218187232 0.0625, 0.9394130628134757861197108246223050845246808905494418220094926621 0.09375, 0.910510361380034127843504886276252075818733291262414284628712251 0.125, 0.8824969025845954028648921432290507362220048249906507417703093192 0.15625, 0.8553453273074225376957316350710122527786143627149839873486535107 0.1875, 0.8290291181804003430146455093430818624253884092834511327569909388 0.21875, 0.8035225736890607339997845865872831280182965042603916162025275109 0.25, 0.7788007830714048682451702669783206472967722904261414742413173663 0.28125, 0.7548396019890073373273470959175298273476002094358293950872264578 0.3125, 0.7316156289466417911595594204914028252812811532198471928443930381 0.34375, 0.7091061824373984117214474100165018423431187898899713480387516576 0.375, 0.6872892787909721985452023391465135904346520237725210691826568897 0.40625, 0.6661436107034877744697913571301831862471733207796816315099744954 0.4375, 0.6456485264278920373483556800610319463609397772795757545173895435 0.46875, 0.625784009604591121679874456535199474230970817827008377497225613 0.5, 0.6065306597126334236037995349911804534419181354871869556828921587 0.53125, 0.5878696731223464940295448787316932511299614517963065908580042287 0.5625, 0.5697828247309230097666296898291228158846384743279959772910085157 0.59375, 0.5522524501630203650603977247307079991729571565907290748875716022 0.625, 0.5352614285189902419566225080220464054335441250360218912489004165 0.65625, 0.5187931656538893563426454573373251134367070087589234204556583319 0.6875, 0.5028315779709409596886366114376251798828429428010994391961524922 0.71875, 0.4873610767136191107148846960343577688855623931150161764618936127 0.75, 0.4723665527410147071380465509432679129702035791364766823956579441 0.78125, 0.4578333617716142609021468406541889706823290687715449060378795405 0.8125, 0.4437473100810798718472403499994589636605958914444523402526949191 0.84375, 0.4300946406400622513959465186060427862421483059922175839822179435 0.875, 0.416862019678508402585297182336392344618725829882351319044251078 0.90625, 0.4040365236633420953550290605718456381867923936722379918331324315 0.9375, 0.3916056266767989932268077284687155142918915495004473003052071024 0.96875, 0.3795571881830895836590761827218683599001339452984509501570991198 1.0, 0.3678794411714423215955237701614608674458111310317678345078368017 1.03125, 0.3565609806639469844401037358662226891081270463824920277712236537 1.0625, 0.345590752576974515890658664998971793213983210117895140527226858 1.09375, 0.3349580429252949537037634901608895070141722042599714768229239377 1.125, 0.3246524673583497297970681374724719919001621064047170561253314998 1.15625, 0.314663961018459026997774856494311223310353591433070038616351043 1.1875, 0.3049827687110592920355737223383435088209141015643642820305705718 1.21875, 0.295599435377370746126553344874182205699296982416652929281127462 1.25, 0.2865047968601901003248854266478376027931507923282510470883822112 1.28125, 0.2776899709537899611981194437002147828846120284498204238852415214 1.3125, 0.2691463487291838829011990129080059074542904083128644764337339049 1.34375, 0.2608655861262849553731805290249272384572228962381659658995463664 1.375, 0.2528395958047464778109874998193252779685338886322800884660856919 1.40625, 0.2450605392455259063573116572130182462651772663026172166218102426 1.4375, 0.2375208190954581318135909045279779320104319870788761070153321478 1.46875, 0.2302130717473614763011218428836210183363943216825455921103322034 1.5, 0.2231301601484298289332804707640125213421716293610793287438353188 1.53125, 0.2162651668298872943875955714762713215307644279129905399429701532 1.5625, 0.209611387151097822524110127972043884892551160856101795696587466 1.59375, 0.2031623227515317329327995471698127874208678000696276274149808164 1.625, 0.1969116752041940500612892180902017514452725360050508105084499613 1.65625, 0.190853339864316320634776151179227218855855860517957389672700467 1.6875, 0.1849813999073042875931379164202863890527953585098332670979164482 1.71875, 0.1792901205501186299837859108419336597096186522439345655325856282 1.75, 0.1737739434504451266807172586663710160147208533399050376839813873 1.78125, 0.1684274812781842384143188059534704964630387129343825509595135867 1.8125, 0.1632455124539583969735835198648102734686808476777614543028580832 1.84375, 0.1582229760494984069458670548147022193140743670636298503470587577 1.875, 0.1533549668449284632955644782100792542963062037780625081357711443 1.90625, 0.1486367305381225218572049326415189978973318478690864726010238934 1.9375, 0.1440636591014532790849812088278269379336723828229002277930480832 1.96875, 0.1396312862813989672098016595482470761550233620224010978211659623 2.0, 0.1353352832366126918939994949724844034076315459095758814681588727 2.03125, 0.1311714543101942678048647918633013800975226154991932766811987022 2.0625, 0.1271357329320355753208418768713503741979718733801207365843596391 2.09375, 0.1232241776472374968448670653979338864516928703573701117104088816 2.125, 0.1194329682667196179958769476118814108171492183144219507289770435 2.15625, 0.1157584021362632175327933302825342911924876461597301797677209604 2.1875, 0.1121968905203437367863572847404054490939802633246679118014154339 2.21875, 0.1087449550972210275982118015759633710138678548199629871195280889 2.25, 0.1053992245618643367832176892406980972684910733772778671488440914 2.28125, 0.1021564313333943010610871843323990812427799232249322372737366938 2.3125, 0.09901340836382630210299228473911766801908697048040957106948817286 2.34375, 0.095967086044998466499262948621638495138186402015077520283433108 2.375, 0.09301448921066348669986457340375418735074932736166132945145388687 2.40625, 0.09015273423081637996145008521913116682168405010052603521471707227 2.4375, 0.08737902619542038382348389451644838852747851510731708839071317726 2.46875, 0.08469065618478049660859598478340674811051228720749353585903893191 2.5, 0.08208499862389879516952867446715980783780412101543664884575841051 2.53125, 0.07955950871822768223387023359211768857574965248445227619807606718 2.5625, 0.07711171996831671233252919483736885810515927617409728327996150704 2.59375, 0.07473924176092569607834128697678686744012447048653098200056917835 2.625, 0.0724397570342514627383438533736588140933118544769412636804368982 2.65625, 0.07021102001498804355289613124765610630466392038532855768326429259 2.6875, 0.06805085402501019378876506472936150310385234113658660734066781241 2.71875, 0.06595714935553816858837466323667630187856432973209011509715836924 2.75, 0.0639278612067075727024300255579517493086340950787684482181901637 2.78125, 0.06196100769053198153368884987890151761938945734318480175223397675 2.8125, 0.06005466789530794296105561330779035134400525581922280768501375308 2.84375, 0.05820698000957197662131505038131479713379460100133873014470015258 2.875, 0.05641613950377734830626038887821314960169882535811926332555449239 2.90625, 0.05468039736791476868816284272671297494736592458531798234569272783 2.9375, 0.05299805840335580277129913664982808646009786096165100737936582067 2.96875, 0.05136747956725073264257141233972571980085424410978129029933342626 3.0, 0.04978706836786394297934241565006177663169959218842321556762772761 3.03125, 0.04825528130927964649545883495298295172562670161513542517399499271 3.0625, 0.04677062238395898365276137059832508093166339771695327388484855142 3.09375, 0.04533164161167626462607060228274503415072485708605403892559341259 3.125, 0.04393693362340741732674768170146271379851763162525379634224798802 3.15625, 0.04258513628878760750125461386167104630470212141933938823364301198 3.1875, 0.04127492938579754840342279753211213185223698672686759377149261064 3.21875, 0.04000503331137925984994428943034664833574615298217753378765368309 3.25, 0.03877420783172200988689983526759614326014406193602014570069586099 3.28125, 0.03758125087099791586064928714045584894448072183794777402166860564 3.3125, 0.03642499733736423323772595805588577874157512396170989949577846233 3.34375, 0.03530431798508575672991800888194128016944670772720554472386401553 3.375, 0.03421811831166603469855505944363081450784839756181186425689497358 3.40625, 0.03316533748891028888857726498472518873556900852888726679091727231 3.4375, 0.03214494732687607067068765507355672873434966617801554108645972633 3.46875, 0.03115595126969980451383975875968302952804390529840382836472636453 3.5, 0.03019738342231850073978629236361984507166053224765700667134022308 3.53125, 0.02926830760713609313057408023310541927278773018895089880007170508 3.5625, 0.02836781644971310213744077213749624467231022004485412104498576072 3.59375, 0.02749503049258666983761830833692773286485077892961080537740819868 3.625, 0.02664909733635548608738201053301189079810132921618120730838125207 3.65625, 0.02582919080719075335688241901461542835803520264839673655219847851 3.6875, 0.02503451014996014651091840491645328728450430590950487444238024608 3.71875, 0.02426427924617673852579119829026463360176522595301944080944200617 3.75, 0.02351774585600910823615118510043293947006765527307240394676540465 3.78125, 0.02279418088361234536609868230311684110897472367468368755671083469 3.8125, 0.02209287766506244426599278478536929262885164456499991139723175746 3.84375, 0.02141315127819865319461971149269400381183718427446499392243796903 3.875, 0.02075433787369974221535389761705478498013687000307133820023945717 3.90625, 0.0201157940267408907111667251678182724532674786895672028270897495 3.9375, 0.01949689610859799542033825380205486616607325892924923760012514382 3.96875, 0.0188970396775856813829097202628926688814309807853980291379544584 4.0, 0.01831563888873418029371802127324124221191206755347559476959992744 4.03125, 0.01775212592162854192233499372975969883192972116048677525865461803 4.0625, 0.01720595042585138235707948982322940047529341132585851978968192352 4.09375, 0.01667657898348756518324895682260691958111535986151284742846068108 4.125, 0.01616349458816587510560304740823871079494213811366044004860959064 4.15625, 0.01566619614012889424610048876769293504677575385308856266054061244 4.1875, 0.01518419795683794516341405893814046575269691108830998712146152334 4.21875, 0.01471702929863513683900037335974210223291459524246608140489966716 4.25, 0.01426423390899925527328694585600154399994991675424012667683653882 4.28125, 0.01382536956894649329676837613767217643003783845822635747928280964 4.3125, 0.01340000766514082864451674147191811176204210430334354228622923307 4.34375, 0.01298773277129224876057393115059744615353740989840303985272128857 4.375, 0.01258814224243399826757336796622576881843030886975970319595457323 4.40625, 0.01220084582168260323107615403580086525828111710466436134730941967 4.4375, 0.01182546525909661755189452909294639250843084293014711674793270227 4.46875, 0.01146163394226185293955498823828439237425344696414067596797318211 4.5, 0.01110899653824230649614313428693052777153926750577133022641468813 4.53125, 0.0107672086465471001571388735068687991328571956376546310277099526 4.5625, 0.01043593646277450493543211944248485485973121372784432653938656028 4.59375, 0.01011485645260455060390336023262613206432098786076504123484223477 4.625, 0.009803655035821828314879244321390670640100662503601143601731585542 4.65625, 0.00950202828005988956221732114478494195567290342580259606370062681 4.6875, 0.009209681603968139410830634641258015734158417894329567329273172993 4.71875, 0.008926329489511324321877855595748131535959913476387904654310882571 4.75, 0.008651695203120634177071503957250390848166331197708042545706586267 4.78125, 0.008385510525424082963534131487875892223089310615047011826504625087 4.8125, 0.008127515489292211464291280832583824030598636473037479059823140986 4.84375, 0.007877458125943276392000098054503780496517175327953552313161011211 4.875, 0.007635094218859961636273483672785768137198906071529716542544038036 4.90625, 0.007400187065277276355347935180263510842824457375129617949252361694 4.9375, 0.007172507245008698981719370307464823058773867865025775914754147631 4.96875, 0.00695183239638479305035695205011840214010007921362123075881921208 5.0, 0.006737946999085467096636048423148424248849585027355085430305531573 5.03125, 0.006530642163653783493293737192282643610674504746751262503902759223 5.0625, 0.006329715427485746576865116967905187540393454200016803051892859526 5.09375, 0.006134970557096825127264916839030658691480833418155633772249464267 5.125, 0.00594621735647209438641690704566897345954426935151435029030378917 5.15625, 0.005763271481312824320038095295859153803955380026251520485108213256 5.1875, 0.005585954258998099543591723607524682825316954885632630339058838564 5.21875, 0.005414092514085637873237315115703920663880659029111860199037855447 5.25, 0.005247518399181384276493529341379106806329911285485231513854577937 5.28125, 0.005086069231012700368741689865188038290511481988202499932730256759 5.3125, 0.004929587331545051651531164107447126950798927904544537068789732471 5.34375, 0.004777919873987020380245489513485961708625619105341678130726022967 5.375, 0.004630918733533246092687426313748240945109580264796832621306751167 5.40625, 0.004488440342699523089171400701265242243710121215323386473875365156 5.4375, 0.004350345551108769047948483985028086291301324701160883903760129469 5.46875, 0.004216499489590925867367123377085963304777517574095613831651517247 5.5, 0.004086771438464066993464702684720768408390656650938199420680381076 5.53125, 0.003961034699868069033446693119142766094868253406671148974219209268 5.5625, 0.003839166474026163359899948647179930944853965620646042204494596222 5.59375, 0.003721047739313519543060168680392316955514801610558551630843305328 5.625, 0.003606563136015730555538690380517047631473552097876093745572466032 5.65625, 0.003495600853663673406870032014333262941803109188574332258876271471 5.6875, 0.003388052521834711703606328158927367428351543332740652936192571295 5.71875, 0.003283813104313592005710537575666247468422318690945586779310733822 5.75, 0.003182780796509667067986418004781851345939727901057288002656106041 5.78125, 0.003084856926030259320804964811574293912054539621736957175767155352 5.8125, 0.002989945856313060363425103900754825679952326991605084845848480667 5.84375, 0.002897954893223449826180732125675816929535904887786868266272340015 5.875, 0.002808794194525512622515648388314197517982922391089438835732681408 5.90625, 0.00272237668213834018632021782255284570470723235221065691168614451 5.9375, 0.002638617957091921515277196742256435992125043561532871686964183121 5.96875, 0.002557436217099566373645182738544882718147780558370360198366651572 6.0, 0.002478752176666358423045167430816667891506479585533945050878624006 6.03125, 0.002402488989655612843094346412494115953917902977953673477679222241 6.0625, 0.002328572174237713594091962345533610656600547815524059472895722894 6.09375, 0.002256929540148032205921834983195362648762057591929041048269036814 6.125, 0.002187491118182885123279463768573825806083879312942809927133102267 6.15625, 0.002120189091864672399728363485653413483188650354914611245001219715 6.1875, 0.002054957731209459046647916273573107166349905572817314113467469009 6.21875, 0.001991733328532313676882760080850538985462636954614898586443564837 6.25, 0.001930454136227709242213511975650732143585407919243576211998565724 6.28125, 0.001871060306464219592313564396450257437552175454483135917989664483 6.3125, 0.001813493832734615164509378050152183509194283444917342181215981724 6.34375, 0.001757698493204273174334045279742289222040944071904965895584172695 6.375, 0.0017036197958025739862904270043299632776080172645777986209743189 6.40625, 0.001651204925003657617706382602198319650868043431634062393900109512 6.4375, 0.001600402690244564228582236158344265526832877938443435229165394819 6.46875, 0.001551163475930381588261266833905837775868201619922448173819899769 6.5, 0.001503439192977572447382903332167653830000512958302965096556049912 6.53125, 0.001457183231848156994639590235056917960336538733262224712475344933 6.5625, 0.001412350417028881609513421478289207778153440870914890039820541848 6.59375, 0.001368896962910916356439235399316295949556277434052950290238985209 6.625, 0.001326780431026991480997562486094290709950860226266980805409609041 6.65625, 0.001285959688604208900649307858698212238002983176682635216061156252 6.6875, 0.001246394868392049625949765651308944788719901705247374082833885321 6.71875, 0.001208047329726343458052187456838095023215797644340310114509055038 6.75, 0.001170879620791174400950220091799273421371832897758106014879662438 6.78125, 0.001134855442041865181211428726095543169982427982518539780513087483 6.8125, 0.001099939610753318226547311468625499331045671208090165450479575097 6.84375, 0.001066098026659089524920251332962115164132816306654638569338233662 6.875, 0.001033297638647637041402983828895914519059593687066794852999205039 6.90625, 0.001001506412483217851054633889735961973443197912248930675249293748 6.9375, 0.0009706932995199088610235252463403912008334680925399803101479719059 6.96875, 0.0009408282063781959212710105633446220691766745343727337616810629252 7.0, 0.0009118819655545162080031360844092826264737245274360538408161334219 7.03125, 0.0008838263069350499252242114258773038821312838014912041578206189774 7.0625, 0.0008566338301859404976667021148674448059212335235427046619836518091 7.09375, 0.0008302779779929783852050180570158720377852384516439895937985820523 7.125, 0.0008047330101246132706901273038690763164573324899262360792712440227 7.15625, 0.0007799739782929634701909835665035132846721735475241756466725398739 7.1875, 0.0007559767017882707722124483342609427417108699368399178689216450768 7.21875, 0.0007327177438630042917310443223090345182932092900640954687963308745 7.25, 0.0007101743888425490635846003705775444086763023873618958855644522888 7.28125, 0.0006883246199401247129070199651485647721907580134892609555972864646 7.3125, 0.0006671470977542673209530199216276155530851107969914025145944840415 7.34375, 0.0006466211394278742245251986783862759994530947228085273846643940838 7.375, 0.0006267266984484575973461493757061114218681137917704548519805484944 7.40625, 0.0006074443450698788931298055115451065619431603982686742996924096967 7.4375, 0.0005887552473364431944026792533143765119654461832521009036030513795 7.46875, 0.0005706411526908208010939339800391146893821319817614482738883168793 7.5, 0.0005530843701478335831020000885303571978113365824401972528887275428 7.53125, 0.0005360677530166962683263688589214700804598921367476956611788248067 7.5625, 0.0005195746821548384817648154077377751511011296804475912341693433195 7.59375, 0.0005035890497369525153511317658679789222543652740898643725806614548 7.625, 0.0004880952435234149992839225273580295054520480237108282363789757093 7.65625, 0.0004730781316127183551106542984242177413592699480926910084069795239 7.6875, 0.000458523047663020615100539178839276407743904933327279468421064005 7.71875, 0.0004444157765683803531278031880725606756002067566927224217526630583 7.75, 0.0004307425405756875368524022779915575296882599052168986655351139 7.78125, 0.0004174899858287315131203958398363662611368389675296285759980124156 7.8125, 0.0004046451693262644985597619222874584407154373696823266385409007843 7.84375, 0.0003921955462813232727354585300464376399998336397890094508362077356 7.875, 0.0003801289578694636568311470477163449699605164160677655248865785467 7.90625, 0.0003684336193539421893761270923520451107525686247565398712710177303 7.9375, 0.0003570981085762475529911934497159943116560852590903896462252558755 7.96875, 0.0003461113548007411220296898486203381973135434208805954866705311516 8.0, 0.0003354626279025118388213891257808610193109001337203193605445757479 8.03125, 0.0003251415278878858237467176289227121777896123750406957837272502385 8.0625, 0.0003151379747373560089452314870190118067605404911296618060388284102 8.09375, 0.0003054421985610119743910581210086984139633807862988455938478021289 8.125, 0.0002960447300568553658829835512835003838758631844924119150680640514 8.15625, 0.0002869363912626820852699215171641143729762525419634282179998785012 8.1875, 0.0002781082865924991528648619138170386175602080099947620047945011662 8.21875, 0.0002695517941487220305047253027340987160648527339548888513307943198 8.25, 0.0002612585573016675324874250850694064792219756685595328507642149398 8.28125, 0.0002532204765281185037084115243266293838846344484090737587696886096 8.3125, 0.0002454297015009894646814719880957666448340346669548870505757731606 8.34375, 0.0002378786234223676588728962081855444925150926189674791899369423683 8.375, 0.0002305598675924416284118058315172409949839717614575935556359394497 8.40625, 0.0002234662862070598219011440831922913422310912360465962589980848253 8.4375, 0.0002165909513768850277400186063407818107618834537046234001954973691 8.46875, 0.0002099271483613268461528687052620375139990780170761165537271196018 8.5, 0.0002034683690106441743689334304870663872973115267356780263658398019 8.53125, 0.0001972083054098129869683688307122355378438819190303223813160856672 8.5625, 0.0001911408437179517458678108499579280420951606037801895095800576118 8.59375, 0.0001852600581972877642079051009548185206955821847336826854862835836 8.625, 0.0001795602054258329620569716245961685824652263127008436806541025727 8.65625, 0.0001740357186881168701483041768869670085875340665883859433917445889 8.6875, 0.0001686812025384986360510657660838813742089157829846001462515371612 8.71875, 0.0001634914275317483350725995401184169644526808427558519502773417982 8.75, 0.0001584613251157512504141786187559691442411373456536354501727274038 8.78125, 0.0001535859826813471231991754367183116271116802167242622102265169292 8.8125, 0.0001488606387644698376000191297719821478921550265722770250552348751 8.84375, 0.000144280678395901749288105776103235561514032244370266263486885873 8.875, 0.0001398416285941010320876861017929387851379712412841071512443235659 8.90625, 0.0001355391539967001488298871209777827300106664462291816066773674721 8.9375, 0.0001313690526264089844437004220966159392950969621360721766010279784 8.96875, 0.000127327251787187444555606176301367287180387002688406556322889092 9.0, 0.0001234098040866795494976366907300338260721528322889390525344820451 9.03125, 0.0001196128835810243658999818888429185108258969652056335864811210863 9.0625, 0.0001159327820382786364006465934736575289057160061250221623108121509 9.09375, 0.0001123659053168018091678296196070452829167882997127534685852868165 9.125, 0.0001089087698550664460359498799265423289335084024396656413830584875 9.15625, 0.0001055579992694658107581288407532780515474748382865761525665897401 9.1875, 0.000102310321056795913457270280699597223185739202660922025412309757 9.21875, 0.00009916256339819151682555891702717186546597366454690376640824727006 9.25, 0.00009611165206139469381981833979593087119895727427219431164151538848 9.28125, 0.00009315460739833056229751232947016233824182291525740273140270765718 9.3125, 0.00009028854143505790302773519680683956834232111555918228389645630795 9.34375, 0.00008751065505125258470011779653213008153316193144248093340064553933 9.375, 0.00008481823524646916105206960352762039293004278786652155609070993855 9.40625, 0.00008220865249051075644022055030382473483700072365945384799617552838 9.4375, 0.00007967935815531949986690391308296426796681423730861280365529316217 9.46875, 0.000077227882025879383862004167833631555907410808994544202102439169 9.5, 0.00007485182988770059147118931935455516611298689892033605294265268263 9.53125, 0.00007254888118852912727784123833084539649705409903314256417286963298 9.5625, 0.00007031678677199807993355714793659429975826743470175514020132903918 9.59375, 0.00006815336668100710488920879483899164352092935367151110553312094012 9.625, 0.00006605650802868481552526689828881094589221541652261482631592120758 9.65625, 0.00006402416293485477518526396636453019642137748615388699383998795778 9.6875, 0.00006205434652598975618245348519219971968000860482231856571425179731 9.71875, 0.0000601451349967009371561978803011385062520908748761151923745861008 9.75, 0.00005829466373086880775836652252947021194344306593416814691930087258 9.78125, 0.00005650112548058079824489084897915161148029638010797033118813556966 9.8125, 0.00005476276860109710802162914916812398688335412240782379156968551962 9.84375, 0.00005307789534012092668460102720742305438486830204296956359359991494 9.875, 0.00005144486017970227704381268784654159588218795110431542530254772534 9.90625, 0.0000498620682291561138244431660074570758921308086135886143635749965 9.9375, 0.00004832797366742513440260230294326645510287196312615155677008419238 9.96875, 0.00004684107823336604771410811213916985477211989506650581752189108809 10.0, 0.00004539992976248485153559151556055061023791808886656496925907130565 10.03125, 0.00004400312076869203238853072341515867823568104895032006188494516111 10.0625, 0.00004264928706969257066298244855962790325822235544080677470684129238 10.09375, 0.00004133710645466824914014514567688180114132503936917583622810426884 10.125, 0.00004006529739295106753162709407675403788226058624825403330335790609 10.15625, 0.00003883261778242659928804804486446733823331529885284603172829791839 10.1875, 0.00003763786373644492885890546059828306632733946460140753182867903776 10.21875, 0.00003647986840805441570507797336937163732233188262401830307888942752 10.25, 0.00003535750085040998240458763976327742517935314399062311603903121235 10.28125, 0.00003426966491224295375091385758737628534487890029462593428543689659 10.3125, 0.0000332152981673137163255754019595779336923035418641869452844925794 10.34375, 0.00003219337087680165707561627992605006568276303205581351790234620451 10.375, 0.00003120288498361900735586429289694125271021296009202661681254478819 10.40625, 0.00003024287313766639712192828719310286775329505015756466990535313522 10.4375, 0.00002931239775107814293282539127211589186606795626877460112672851012 10.46875, 0.00002841055008253458265500049750329470509792900494680568353941950583 10.5, 0.00002753644934974715785741109710242551110158986173923072932051393179 10.53125, 0.00002668924186924945957530934184690544320994677460172602072985336809 10.5625, 0.00002586810022265412127035908731992101405025019639191270368287288637 10.59375, 0.00002507222244856129047201337293241817466703521398592655962966686766 10.625, 0.00002430083125932946299355438797434340908672824184176946039020883551 10.65625, 0.00002355317328194374524309716473171740728410198571175485565787858429 10.6875, 0.00002282851832224014470963609116121530661415288528253875866172038151 10.71875, 0.0000221261586517672991817087994496068148047063472397902413838409455 10.75, 0.00002144540831658916392913486505515413613217214562057114062390082297 10.78125, 0.00002078560246735360453881500237216378851843282733731863872875732472 10.8125, 0.00002014609670997261227168408907269590687518324275883611929109734365 10.84375, 0.00001952626647627998898364757363375566889996381261697922654781545696 10.875, 0.0000189255064140518594886702166687149912230057721263352896270619325 10.90625, 0.000018343229795794279792229454932371279435801680791230245455965807 10.9375, 0.00001777886794572053835692990837048942425627006939254003275226367834 10.96875, 0.00001723186968435851237981125047798864921604813388435887866556113703 11.0, 0.00001670170079024565931263551736058087907793804695928712447812473711 11.03125, 0.00001618784347818591235827934788723262562436078513543961606318264344 11.0625, 0.00001568979589355892372697302987028715129039131940322319375784573094 11.09375, 0.00001520707162218777683275496225151727657178061889913654754991587414 11.125, 0.00001473919921528648366435084220255094529912234858283760974456454338 11.15625, 0.00001428572172902331111586894886699274583174628757237904446776262593 11.1875, 0.00001384619627825025449477100212214554624394207704331337079434825134 11.21875, 0.00001342019360396281167854175859667327890312420359300517046936084385 11.25, 0.0000130072976540676209794295301465587490370648287288213976519861627 11.28125, 0.00001260710517704852279493063652605877692266167843051365148706460503 11.3125, 0.00001221922532813420226377706232986779938860062625259004096221678145 11.34375, 0.00001184327928758277971595601053414364498898717010672756919694438398 11.375, 0.00001147889989071054962511999344895169037263733540062576740931026561 11.40625, 0.00001112573126930353860013941661243510994374835937351832103773310448 11.4375, 0.00001078342850406166989142215218975998776609097180211361459039394392 11.46875, 0.00001045165728773609679424550718688557719682672235004321020401291826 11.5, 0.00001013009359863071072894135574908649729900677385468906416239442228 11.53125, 0.000009818423384148951865805481899030743267579258590992237080395150634 11.5625, 0.000009516342254076860826723418814838068852614463758104003833475731902 11.59375, 0.000009223555183302818817223660967073720636619151704610897363225172762 11.625, 0.000008939776223683639808699493540010510759698441461385303749424832621 11.65625, 0.000008664728224775611101875091698227729796062670701250653459023150083 11.6875, 0.000008398142563157736483290834156669248605544030529365745367666486941 11.71875, 0.000008139758880082827692296660509407107964477136371725528465973692184 11.75, 0.000007889324827200223242267497250908025229971315461862451098397701394 11.78125, 0.000007646595820101796729839074911371648979714479087445316883019229586 11.8125, 0.000007411334799450557318862530777708474300845144777946092034085514365 11.84375, 0.000007183311999458550563576716115734947813627748429275412849059295655 11.875, 0.000006962304723487945370698571878450321329363891325446644512398306253 11.90625, 0.000006748097126556149702714124755288108042358242993855089682829576027 11.9375, 0.000006540480004532540388921002749036702026631577622318669409933892938 11.96875, 0.000006339250589820927721994491271215685352770177990241786242519896056 12.0, 0.000006144212353328209758682308178805532311223989314888252975568505876 12.03125, 0.000005955174812525810599963949485718554858362750995386516757830623889 12.0625, 0.000005771953345416447394592132088367783301607124037198077163372459349 12.09375, 0.000005594369010224538201843926186662444142526469412332401088887697497 12.125, 0.000005422248370634152797224656382546796148767196099722095519287013954 12.15625, 0.000005255423326403826468166331522802012889288220854318379743916456411 12.1875, 0.00000509373094919280811685461814314811966227433911439640699980138163 12.21875, 0.000004937013323438403692814997370884871942945573065740697032837612456 12.25, 0.000004785117392129009089609771019433047616632788052290612016278131925 12.28125, 0.000004637894807322207975826985682423008053122232553234922572719520839 12.3125, 0.000004495201785261944260133393609982211354436168360335017209176963085 12.34375, 0.000004356898965953270539499408502442532022826863768330425375599353996 12.375, 0.000004222851277057527335517923455062189095885274590161536671026065095 12.40625, 0.000004092927801975027437798523247868633205707803177551606914113577989 12.4375, 0.000003967001651986409366678590653912429672324963630750937642272126852 12.46875, 0.000003844949842327787834119180822987166645991008265737791562758799407 12.5, 0.000003726653172078670992924851475950426180337481883969847014640452336 12.53125, 0.000003611996107745337972514703124111853839612830276205554047110188985 12.5625, 0.000003500866670425979341106325925033619926240350736293897680595659909 12.59375, 0.000003393156326447401234194383629460677900392197033522628166916894683 12.625, 0.000003288759881366484364002504240141483486472455659141172050772336982 12.65625, 0.000003187575377232875285440994877109384963703554897498963978707331939 12.6875, 0.000003089503993012572350416282863346077643119956450711977456920128133 12.71875, 0.000002994449948075155844770596022650031406835118437676372155567176917 12.75, 0.000002902320408650403885636985130628258815810820544004038528009453869 12.78125, 0.000002813025397162935683769706957674237495652594744867517908057768313 12.8125, 0.000002726477704356334577741105691535648512002773682197496792009998577 12.84375, 0.000002642592804120927569930994981351331955204564565286037263645304058 12.875, 0.000002561288770942038598661382929960881247932511123930337102729989435 12.90625, 0.000002482486199888092045475657681858859531407541294805976360291740902 12.9375, 0.00000240610812906042350091234163974281174211433307678353950696123884 12.96875, 0.000002332079964429059018763430088214607451848966670151166965112525814 13.0, 0.000002260329406981054325785277290538689469353142422703218644035360192 13.03125, 0.000002190786382110243996953225354948543102599024528925193653485585382 13.0625, 0.000002123382971179439661407705846028640263414588298450854962540225556 13.09375, 0.000002058053345188238009152436019915288001714384215324591641317203707 13.125, 0.000001994733700481655795480658131285123862189214798348424534877057005 13.15625, 0.000001933362196436802197415976441747244235535097855374081523744001904 13.1875, 0.000001873878895066730710761325559882400959816724986595752069506331702 13.21875, 0.000001816225702482485174363429366560172522321802304364052601342701881 13.25, 0.000001760346312156169298583277070545670846355256204940795463759798673 13.28125, 0.000001706186149929628030116116737054053465150696779907622037999424803 13.3125, 0.000001653692320715033923096682258398847366032601169859999788593182249 13.34375, 0.000001602813556835324071872976396958968588897222004727343295113182881 13.375, 0.000001553500167954034707723917976395643695080819469302538939657586887 13.40625, 0.000001505703992545632834270445886657108640048063502268890639983941261 13.4375, 0.000001459378350858948790400712018736336756218813570941140715163676229 13.46875, 0.000001414477999327771854569383983800152890375295567703005771877312151 13.5, 0.000001370959086384084364502599612723536382681291526099653792830575549 13.53125, 0.000001328779109630779701990638708409603216297727083542767700897683076 13.5625, 0.000001287896874332037221457964778207174921618859663096131917656633651 13.59375, 0.000001248272453180814080080821335227264065614009267604523362818044089 13.625, 0.000001209867147304161213196171197145304426019654434173875318393974842 13.65625, 0.000001172643448468279609764280236637968649964387864894814668760409787 13.6875, 0.000001136565002446404759383914508849162021384917435631750977281708851 13.71875, 0.000001101596573513742809161689961151011125410035795713618731254720284 13.75, 0.000001067704010034782694745456530497899430984764994748658965683200416 13.78125, 0.000001034854211109375370060936567200622023754525192027691692559723268 13.8125, 0.000001003015094245005296681653097343992149366022720162198405332817762 13.84375, 0.0000009721555640236815761978198592868346625299845160598220844614591222 13.875, 0.0000009422454817328474962428216997306633124349292369726189362373152284 13.90625, 0.0000009132556359306487616837955087787319155759524383063374151122560645 13.9375, 0.0000008851577139168132163948763711031687024661402915823345662230917514 13.96875, 0.0000008579242740812793192268235428525542600815901827190772655275263584 14.0, 0.0000008315287191035678840639851425652622946076583649845727951595276897 14.03125, 0.0000008059452699767224654415967213321538339664053731501267205561127827 14.0625, 0.0000007811489408304490795473003752540639777188068104912817749840444927 14.09375, 0.0000007571155145288664820484564403041198287145733851561357547120011069 14.125, 0.0000007338215190190347413712400609285392274431251658284135032734782109 14.15625, 0.0000007112442044071630876551075007117278249866643118196704882769549315 14.1875, 0.0000006893615207401086997315043187395830081913401587830214750979479301 14.21875, 0.0000006681520964704669092106642241606136959041569669319087592204616288 14.25, 0.0000006475952175842209248323503122835838007039376212704757086900347064 14.28125, 0.0000006276708073705662636751120150713137434353348256678879118450588217 14.3125, 0.0000006083594068141522505929193390630889319913680163493240919852469083 14.34375, 0.0000005896421555905908258831013258210064827870149447130380931129282259 14.375, 0.0000005715007736466720773651491360594751430180661650273363006852823132 14.40625, 0.0000005539175433472969621803297855132395049441095991499999168788500302 14.4375, 0.0000005368752921716911634177451252966820971679784823733834853347189781 14.46875, 0.0000005203573759420004776850631931507179520362513994642813892921597776 14.5, 0.0000005043476625678880758922222333462485722099130901169642339945154338 14.53125, 0.0000004888305162912579286051966255107613145393944498207321291520613855 14.5625, 0.0000004737907824157171315322522756910649622798248144288524426601834933 14.59375, 0.0000004592137725058632830621203036897494354532477945989026339913407963 14.625, 0.000000445085250041941916640468219651708427839152337723096039672947275 14.65625, 0.0000004313914165158637242809643538836527200033549874285530287120071485 14.6875, 0.0000004181188979550023580055277592518970676659874562850041432701480716 14.71875, 0.0000004052547318606113844770423187791368834799763531511822209173810827 14.75, 0.0000003927863545481039025566457391195863032792210996412992908581801363 14.78125, 0.000000380701588876830809462681059949024202795254940876073720205623922 14.8125, 0.0000003689886323573741019380024775808611668705209937712427360369985135 14.84375, 0.0000003576360456247402958618786903122562883233860445385506674476882963 14.875, 0.0000003466327412661964011572309055536416483538278668298875316931831333 14.90625, 0.0000003359679729928372476497237686972987161862461292961391776345590392 14.9375, 0.0000003256313251443086600008440723869955184905859592824416827581882565 14.96875, 0.000000315612702516436353823673364501701704370041493620233356343272174 15.0, 0.0000003059023205018257883714794977022896393708207808185591165592618274 15.03125, 0.0000002964906955338038717625369011595893568845071324320817401019705404 15.0625, 0.000000287368635824369671090682015078439046955057911645794167295261573 15.09375, 0.0000002785272323871084213064410670133865481263157593649517455095351744 15.125, 0.0000002699578503363014338726084839633428005857109164638257150001951953 15.15625, 0.0000002616521204537342507073202470438980950013641046517293360800729179 15.1875, 0.0000002536019310149668342707677039766524310491314554013313644534672512 15.21875, 0.000000245799419867082986167230116423054377723166495242326534461504394 15.25, 0.0000002382369667501817918046252454433717387565473839792923742756220843 15.28125, 0.000000230907185855111937347605486671185350276502418159282168657199692 15.3125, 0.0000002238029186101804699057090500607962128591456001910050841464082213 15.34375, 0.0000002169172266897911979506435835835358560035865708068373798918678793 15.375, 0.0000002102433852381846747590232072968232048892315579314972879574217136 15.40625, 0.0000002037748763016617849118476267354621194212624023647799472907564005 15.4375, 0.0000001975053824628765677199495878267682452266488107767171790511529571 15.46875, 0.0000001914287806709812607433653311757018689234225197080733925055446492 15.5, 0.000000185539136261597824071710864734925208710176169168731272302952739 15.53125, 0.0000001798306971607755985418094076094557528958380839595129272682033962 15.5625, 0.0000001742978882672744396501186455735147276605400932807955148355712165 15.59375, 0.000000168935306007686829062361490065744255275992974453816303830851889 15.625, 0.0000001637377130590812674232787623079998641101479600827616090934837723 15.65625, 0.0000001587000332340128604845205464368971888304115399261105453259549875 15.6875, 0.0000001538173465229055851851229272864292158762199356079251476460105079 15.71875, 0.000000149084884288964418107405095592983372235234576954371156431846824 15.75, 0.0000001444980246109244757972267552742775288194997848481932804611023742 15.78125, 0.0000001400522877690887002688571213083140741016090398737374624027834001 15.8125, 0.0000001357433318702455646225347870987886183716969437840369818627479093 15.84375, 0.0000001315669486071939097599906833827537093037571698638568077516248853 15.875, 0.0000001275190591487334861576310854394877149593188748511176241901019445 15.90625, 0.0000001235957101561071929409931918752942889051674032861963443335843898 15.9375, 0.0000001197930699220045045395367079214472202136203592612564396574311976 15.96875, 0.0000001161074246283552736011864255612188180277972990588442760025619111 16.0, 0.0000001125351747192591145137751790601271916379408006576543025777501291 16.03125, 0.0000001090728313855080181214246864963023606262919495925371045361552675 16.0625, 0.0000001057170131572688348576624982218873275029091327993189197580104915 16.09375, 0.0000001024644426015978969846278551835584466237838221570414252129318159 16.125, 0.00000009931194312156243409514613948799765723113976296694456839397451196 16.15625, 0.00000009625643585384266949374265739229851839041031273130850821587701023 16.1875, 0.00000009329493666178466543938344346344689434474582853956001520582632912 16.21875, 0.00000009042455322096720644065696361894413566465309392094949500035681616 16.25, 0.00000008764248219443636288699068669801797917295175738301912938849084908 16.28125, 0.00000008494600649484895051995360725661809512323880867091972810299067204 16.3125, 0.00000008233249263085098012441295989838538468899099296304093019734069685 16.34375, 0.00000007979938813509945924656342175224944522503168806641003009849921846 16.375, 0.00000007734421907141564407045271910225166086632896100885010321719891469 16.40625, 0.00000007496458761863512268043031436225934860220032460902478260156282208 16.4375, 0.00000007265816972879501628186110401578727126146240333383462724946280986 16.46875, 0.00000007042271285737118570217783636363996102837241740167856366722539321 16.5, 0.00000006825603376334869755383389689872243234910670785227114563978053275 16.53125, 0.00000006615601637697700653338619353802874999434383544633258523396756447 16.5625, 0.00000006412060973312741406581261674696082109820980252899575437007759238 16.59375, 0.00000006214782596823443343993216326797230795057097747307387210314261585 16.625, 0.00000006023573837886479029283790427826700257340974055690452782091283997 16.65625, 0.00000005838247954001797543678235083780414258860164930980398440385342962 16.6875, 0.00000005658623948132060336323100331196852378821358161639309311731596049 16.71875, 0.00000005484526391933337127954255141989547778341367913338054860961402847 16.75, 0.00000005315785254424421545476583056411120267842436605173139798370825074 16.78125, 0.00000005152235735927437749422065275672351524688566488235448374511525815 16.8125, 0.00000004993718107117557480301402468944624709074439096987866138487386443 16.84375, 0.00000004840077553024636721556610321756805922748354234856640005499033733 16.875, 0.00000004691164021834417429378634554468371936296012920885337994749502783 16.90625, 0.00000004546832078341627236362219418243580821194805100276029038054507631 16.9375, 0.00000004406940761911853274797008837900310623791312349099600363672116316 16.96875, 0.00000004271353448813469723456904094944250613840926876810363928133882148 17.0, 0.00000004139937718785166659651027718955280622936694374245959755699806974 17.03125, 0.00000004012565225708764464325618795159012136097809218194688149228088534 17.0625, 0.00000003889111572261007422198489572712026742266831303745103242415190115 17.09375, 0.0000000376945618842191619700897138913161199698698043735114076305021139 17.125, 0.00000003653482213721045339244497424564741486636786817187833910090314403 17.15625, 0.00000003541076383106642578659920140816163596461696310116461824029177528 17.1875, 0.00000003432128916326244931861610802892503007344086472572908650806826961 17.21875, 0.00000003326533410710676071816109803528294487355068216431099043689850905 17.25, 0.00000003224186737256733310674780475755638107075369281405911641494806478 17.28125, 0.00000003124988939907074186046253705302560011372101811754362759355198862 17.3125, 0.00000003028843137928935160185059479654272840045360662349404155915147859 17.34375, 0.00000002935655431296341391081177810313268842626524424083170957780795115 17.375, 0.00000002845334808983399869888939463701807282148150244157414593158648285 17.40625, 0.00000002757793060079111305343298781874371289151167020502116875577701941 17.4375, 0.00000002672944687636891749454318667948907116349267050777377905573802422 17.46875, 0.00000002590706825174665791094703242516839771388953555588156138937801344 17.5, 0.00000002510999155743981803547343740192733037123116164780246276122766355 17.53125, 0.00000002433743833489108746853588997128441772442558565078985482002204512 17.5625, 0.00000002358865407619505846307458436911384786418169120305165983209342185 17.59375, 0.00000002286290748721413469694264246994568194221293334451834355837055603 17.625, 0.00000002215948977336598009820460554032373614703414052529026916936359625 17.65625, 0.00000002147771394738497776647907048252608103249714945474736241470142545 17.6875, 0.00000002081691415838162977408603251316421201346222241206000538224628082 17.71875, 0.00000002017644504154462909381329684172616851799178706537044305163612577 17.75, 0.0000000195556810878504954002005627830503883782450235709322852089178013 17.78125, 0.00000001895401603316520671794509312243907483716995142744693667600249418 17.8125, 0.00000001837086226614119792816486960339354337975935916659020331961592124 17.84375, 0.00000001780565025433145348747243976999309590778886957383046518131521333 17.875, 0.00000001725782798796021329369155943745269411526761764060127223725058827 17.90625, 0.00000001672686044080705482159197139006624859347004492480704241627416402 17.9375, 0.00000001621222904767782829362457500013443633128403648260048402255557525 17.96875, 0.00000001571343119795212106755378921020937573002346547234651006059982503 18.0, 0.00000001522997974471262843613662923351743186217484333974231172832107487 18.03125, 0.00000001476140252897702597785750505381353274606219635190607909128266614 18.0625, 0.00000001430724191856768833467676051405007119078651938238519208632413038 18.09375, 0.00000001386705436116889522800173667488911759904210625988843072828716649 18.125, 0.0000000134404099511350216196521063651412284755240617213346804464627172 18.15625, 0.00000001302689200962663871291093102808350691241399459560480199837040694 18.1875, 0.00000001262609667766446908592433047791271040060632474462574518086615767 18.21875, 0.00000001223763252170375536829100612862410517216847783947801141798544328 18.25, 0.00000001186112015134382983309908947354505906820594032792997460525031301 18.28125, 0.00000001149619184879952402326229331232167755384351705327036593725924971 18.3125, 0.00000001114249120977254463769332999574707251458609467759248736084079569 18.34375, 0.00000001079967279537207558842059475715541884804788793097268899336134725 18.375, 0.00000001046740179474465727782886018971752831897915655033206309801251799 18.40625, 0.00000001014535369808385327486459675240744009564469310261685173863708418 18.4375, 0.000000009833213979700351905116576311676775760477357821911462841460482959 18.46875, 0.000000009530677790842975712710556364536051545816839265798474462877721231 18.5, 0.000000009237449661970594897883170384597758064645835854156804665664221919 18.53125, 0.000000008953243214184170983628714642159831946572502240860082175836922145 18.5625, 0.000000008677780879537103132494793119147332578307608301756563784884416149 18.59375, 0.000000008410793629950720457641637660995408169415762843689353809069991177 18.625, 0.000000008152020714470167819062765894965049018317002617446880295764135397 18.65625, 0.000000007901209404604078174233990640525956725160185970412859259782579776 18.6875, 0.000000007658114747499319517727401341809612803821550109608759987277415932 18.71875, 0.00000000742249932670975650705127745702493716025802987352684675919103554 18.75, 0.000000007194133030325383505481621086436094949487136238075081260786078116 18.78125, 0.000000006972792826235374220331708630516451330923994789832715663373675008 18.8125, 0.000000006758262544305560397502502229908609856738872092732223108855277081 18.84375, 0.000000006550332665257604954809374304322889940057398568270870528930352536 18.875, 0.000000006348800116043680092676125523181951890964953057590170147456025717 18.90625, 0.000000006153468071516804703599896749527545501980208406692282472977349453 18.9375, 0.000000005964145762203144006924866823004748619164697760089629860826694818 18.96875, 0.000000005780648287988533767904455083955243700480529521020239342364419098 19.0, 0.000000005602796437537267540012982816206463079783873749462819271605636779 19.03125, 0.000000005430416513266783738150554513100104229318636427331783696779533731 19.0625, 0.000000005263340161707315475371147705258743891147382626969917821548013083 19.09375, 0.000000005101404209080825276434974600079077965879836862126351369874352169 19.125, 0.000000004944450501938644154178699801554577448912060768358042586690268601 19.15625, 0.000000004792325752702175077719201916829883968721937112868894333146025206 19.1875, 0.000000004644881389955809399999949799743246343602825489624737088335910247 19.21875, 0.000000004501973413345846022841420211857355786498845394056125511315851899 19.25, 0.000000004363462252943701493269452779618645997615421142233358635022249607 19.28125, 0.000000004229212632936059238809530434654830336706769714329961310680394108 19.3125, 0.000000004099093439508832019823291077260896916111811618881521627973128816 19.34375, 0.000000003972977592795907530988523029226275270120781509510548339636417679 19.375, 0.000000003850741922767616921868439006922140160731288067535340235117409597 19.40625, 0.000000003732267048937713705290435545936717581599947793716984711301228389 19.4375, 0.000000003617437263771379839782959010498987953698647406249822302529141168 19.46875, 0.000000003506140419680390350809553543115153729267528692554520477514228011 19.5, 0.000000003398267819495071225140737876810867902387991960362445312088155848 19.53125, 0.000000003293714110306080894954855243083907243968278884593296961865235828 19.5625, 0.000000003192377180572336739426620580750818498967183149159123485794418418 19.59375, 0.000000003094158060394597885884064004490054804323987360690551332224895107 19.625, 0.000000002998960824857307305436565427828835805244018430291662557477742093 19.65625, 0.000000002906692500344292788795210341757050774579084855282261315618057792 19.6875, 0.0000000028172629737368307834181476468072352997970717650483836807785466 19.71875, 0.000000002730584904405392109672469241203058349590899124702048905415981656 19.75, 0.000000002646573638909117000693223559709276305618525603321195674246596366 19.78125, 0.000000002565147128319711392735845926640979315732139359812810691738222802 19.8125, 0.000000002486225848089019512772774914319378753500893115944315720995282754 19.84375, 0.000000002409732720382012071128861541613667195993598983492018735170211289 19.875, 0.000000002335593038799337195316980485091751691359647637535600944958119131 19.90625, 0.000000002263734395415914988511272198280655255538650453143139472718230541 19.9375, 0.000000002194086610064318541516231787990588076311042022760628312077569289 19.96875, 0.00000000212658166179387657976625879069180404059762326269027815840685192 20.0, 0.000000002061153622438557827965940380155820976375807275599103692972244662 20.03125, 0.00000000199773859222875669972384317992057066242860563783358888795315168 20.0625, 0.000000001936274637384096079360248749858040978704853945719828287711099884 20.09375, 0.000000001876701729626297707515562358423126861020645704223218480052223024 20.125, 0.000000001818961687553045900803691854779415968108581435695397311949701072 20.15625, 0.000000001762998119815587859960748373293101631994827051098661518544872205 20.1875, 0.000000001708756370044575425804394260626506527655946732294925525071355728 20.21875, 0.000000001656183463470360548300311297331116784431275593365563480979413767 20.25, 0.000000001605228055185611608653934309109539657171168160140150858203151324 20.28125, 0.000000001555840379999721693827811254248865694020711994109651342388448214 20.3125, 0.000000001507972203836034533721868081101059376028446232379522679641080217 20.34375, 0.000000001461576776624420591743673890650264723277935450006387249614427584 20.375, 0.000000001416608786643196221223593353898477087234041501984510406286813434 20.40625, 0.000000001373024316265794289404640137587008570606283660804528574166391129 20.4375, 0.000000001330780799068966609985148972856970547309074361604308042065896355 20.46875, 0.000000001289836978260628254307522616273118971291583313492699133643420249 20.5, 0.000000001250152866386742628937553119231222182271594642076565849338224677 20.53125, 0.000000001211689706277895372279581440909923571957242557098909147423716582 20.5625, 0.000000001174409933197415855009921853118431232805065714692999633942350747 20.59375, 0.00000000113827713815407855093636396779934398639787009035440971839812978 20.625, 0.000000001103256032343553922476783665789446829728879015411445235221685217 20.65625, 0.000000001069312412683880849105636375322693083505763353748406528121296867 20.6875, 0.000000001036413128411301094478011605912412882489877727655018282267522416 20.71875, 0.000000001004526048703831902017070442758281274430977926279272311153016452 20.75, 0.0000000009736200313009565409467121337463776649953328334477253542579116729 20.78125, 0.0000000009436648920887854753542167002513856183044607385965329069978040338 20.8125, 0.000000000914631375620983748004122305909538006175332392658718265903780255 20.84375, 0.000000000886491126546674079189127093742912442933649513864394760199218337 20.875, 0.000000000859216661917410971457821192718880349105361071112494032267767346 20.90625, 0.0000000008327813443461796487027921062736503879657905868425817814305930325 20.9375, 0.0000000008071593559922057813100061027195677393185297286881926094122123363 20.96875, 0.0000000007823256733461684705478123477914199818041530497685537975371119852 21.0, 0.0000000007582560427911906727941743241268126442980361518896898802629878868 21.03125, 0.0000000007349269569157389010120837131869161917643680442388073643263418063 21.0625, 0.0000000007123156315552983870450003201742386032022396141587106112543096637 21.09375, 0.0000000006903999835404016410098740103263760965771957641672944982575926956 21.125, 0.0000000006691586091292781984424095320766685233591086526061642795443229856 21.15625, 0.0000000006485707631040619756384741552999650204096042525986146299107945916 21.1875, 0.0000000006286163385101407118488394561787140866031486569646616261171994915 21.21875, 0.0000000006092758470188600965426459675405292240436636771864345821594170212 21.25, 0.0000000005905303998944039743060089194711978297627087223343364245253396631 21.28125, 0.0000000005723616895462620836601625498773471215998625899506718727587723779 21.3125, 0.0000000005547519716492686954494646351757524675287121215647157806240294188 21.34375, 0.0000000005376840478137498300141448901032536808349431914085236324943340618 21.375, 0.0000000005211412487888539914917754235340979681910935081149796745931924972 21.40625, 0.0000000005051074181826620869948856798517168495737139750244668455803185054 21.4375, 0.0000000004895668966831769066561314958437347211152719571916922756058964486 21.46875, 0.0000000004745045067647817205362144931935080135264780835533719940745004647 21.5, 0.0000000004599055378652316779070592536140742516339382273414831770280774728 21.53125, 0.0000000004457557320187012365083799919631331089587542387874754517228161657 21.5625, 0.0000000004320412699308562528377960572967875600328272916181235678864577067 21.59375, 0.0000000004187487574823510643728035782375265414729022687400295500157559764 21.625, 0.0000000004058652126475693124533616561412342268063892794474077673644163577 21.65625, 0.0000000003933780528158327990210221912430522517547184464896307164103420838 21.6875, 0.0000000003812750825026957375861722523180536404883234638548413139202578956 21.71875, 0.0000000003695444814393227325649697695646913866165233497346868249983475408 21.75, 0.0000000003581747930283180735669125811956356953889253299804844509248249415 21.78125, 0.0000000003471549131547318229501193109528811620243414896049131510145105107 21.8125, 0.0000000003364740793413150555264103747725091919941034789616220074698158069 21.84375, 0.0000000003261218602374328174702788653865750290088727042093047611000176103 21.875, 0.000000000316088145431369235619847408603570469776481710887416234166167763 21.90625, 0.0000000003063631355760750467065098875569011033779067237628761817065427193 21.9375, 0.0000000002969373328187139370741448926340441537635757320022344608476055657 21.96875, 0.0000000002878015315246607861192026638113560023595300812552385774465316257 22.0, 0.0000000002789468092868924807718913030644293207693172926068483265320511174 22.03125, 0.0000000002703645182119906947030474414476582846144861138054501944024293944 22.0625, 0.0000000002620462764742461767969384454018244649771623334381846744392608346 22.09375, 0.0000000002539839601296159326178729821236275630079064304522665169153802768 22.125, 0.0000000002461696951815384658412357904937687601314699167778584317117956307 22.15625, 0.0000000002385958498908582217727783460362304731676250670687162653996551116 22.1875, 0.0000000002312550273223487823733493132083786910592099621183621511018267063 22.21875, 0.0000000002241400581205554344783071864102583685108727082680539969670803734 22.25, 0.0000000002172439935079016958265025866427954139161324610052360113798719961 22.28125, 0.0000000002105600984982214558245434325695811208016120564462549037161457876 22.3125, 0.0000000002040818453190887816699931024992994555875944841135831449620230391 22.34375, 0.0000000001978029070365213610215165221448913485550088090655582796105374711 22.375, 0.0000000001917171513758311989921646674041166170827906984210452648821222554 22.40625, 0.0000000001858186347325877528839285779285222301712492430626940530723730962 22.4375, 0.0000000001801015963678443598684466085032263879248459396806626368482709644 22.46875, 0.0000000001745604527819587720851739000663952328220762010666891795991952147 22.5, 0.0000000001691897922615130361301943920641707939852013398692267781648297331 22.53125, 0.000000000163984369594007009994509912754407280534273606375572328998672844 22.5625, 0.0000000001589391009451636652873474286687896845017040851943310625500811005 22.59375, 0.0000000001540490588938431360752372177840318359736219124657927889333267258 22.625, 0.0000000001493094676197164029852572441807134985775478217666799218145577931 22.65625, 0.0000000001447156982389986926927919326868387555729717586606943946327908907 22.6875, 0.0000000001402632642836872742470707581216842422806706082003842589122484489 22.71875, 0.0000000001359478173198884861053650077774656537118054088710472169952196533 22.75, 0.0000000001317651427009546681277637488457027216928860955828509146750101454 22.78125, 0.0000000001277111554512833337963405215677610430798936474148178998146082817 22.8125, 0.0000000001237818962767585281509313260105493105159579346169698629934438497 22.84375, 0.0000000001199735276979380010081776361618664328040636764745745276939708609 22.875, 0.0000000001162823303022097037460042452156757580313473719706558782150041482 22.90625, 0.0000000001127046991112573083708862561097040857762702512746668372150009381 22.9375, 0.0000000001092371400602870632165686282041455400312312300699695497060202935 22.96875, 0.0000000001058762665855774504354880023504017872062961149530343934530775114 23.0, 0.0000000001026187963170189030392752784061249775983384339072303347942040296 23.03125, 0.00000000009946154787241337694387770569002926698944885021559412124519682572 23.0625, 0.00000000009640143775040295642065213360399128755441906614011256632224175955 23.09375, 0.00000000009343547731899299660880308109615789027451889963894655293038258109 23.125, 0.00000000009056076989672866838205679326464884099162076450376888924967580197 23.15625, 0.00000000008777450792367426006436955725125275613703346494831371246113844945 23.1875, 0.00000000008507397021943229563131815480223995362993197690238560348993438731 23.21875, 0.00000000008245651932552453577213542831517518851051508990879511156879267466 23.25, 0.00000000007991959892953931954328336519029480664254493025330021153562232889 23.28125, 0.00000000007746073136852956078485647651570678699020532994198171797642708772 23.3125, 0.00000000007507751520922311298713126530179569615962249778790525733677236809 23.34375, 0.0000000000727676229026822344778394739858206313630053172250954803884599646 23.375, 0.00000000007052879851112159591715026607595741365799858216374362753067662151 23.40625, 0.00000000006835885550466474514865125574507089339455319564009930580384645663 23.4375, 0.00000000006625567462588724928777016037819485145263446821747430568516805604 23.46875, 0.00000000006421720182006093724344499651861132231629723838658240232641142992 23.5, 0.00000000006224144622907783232136689302022997237838750643220946992160286443 23.53125, 0.00000000006032647824709455681543669928495257990996293247249621940852269112 23.5625, 0.00000000005847042763599826929776464155412857271401524541290913235291467396 23.59375, 0.00000000005667148169885361953741123782172877864901564507208390387664413075 23.625, 0.00000000005492788350954683267366302879765929035863916128218581191338245187 23.65625, 0.00000000005323793019689791874496626773419864303540576251634234079397065561 23.6875, 0.00000000005159997128156519953218794308170757063499751045831260393913246772 23.71875, 0.00000000005001240706411790386794378439677628430664376025467647286802403476 23.75, 0.00000000004847368706270255544719939628216547495248779512634412849893239563 23.78125, 0.00000000004698230849877731255382900488068339269597186295172971361255578254 23.8125, 0.00000000004553681482943536429733118181335927970942887420840431333030944503 23.84375, 0.00000000004413579432488398878208519056111761217980378531050967218268863378 23.875, 0.00000000004277787868968997954640400249723929260207060773823693847904607022 23.90625, 0.00000000004146174172644489068100517407718724253004393552519169505988187132 23.9375, 0.00000000004018609804054498001203393641635784898483003105327975573147131101 23.96875, 0.00000000003894970178482088407371050954245800424862074407897035061948796221 24.0, 0.00000000003775134544279097751644969547523406779168606482567375617965378112 24.03125, 0.00000000003658985864935009110776689593139100765040378409240920265647168613 24.0625, 0.00000000003546410704774182342585231695781743174203619373550464851848409614 24.09375, 0.00000000003437299118169811742631543759393293633921552889769605005083808293 24.125, 0.00000000003331544542166411888963861186873509980245246947376748978840293782 24.15625, 0.00000000003229043692405962287390830861517500256877781306954230639467042647 24.1875, 0.00000000003129696462256067921596398843487100942078436938271696952830010931 24.21875, 0.00000000003033405825041620035549178296323237503038409987538181923722592467 24.25, 0.00000000002940077739284472484256681695837677988851278913485508319509527564 24.28125, 0.00000000002849621056858586743181644288794507677838919723628460104039854084 24.3125, 0.00000000002761947433970946035707814608195729200300400566187003253033665014 24.34375, 0.00000000002676971244881298802776196111867754918174136578723484399507036772 24.375, 0.00000000002594609498276466594511872890858600291465444444369374272861213349 24.40625, 0.00000000002514781756217544022702595945666346179625839755109497002630738758 24.4375, 0.00000000002437410055580831207375228058641099674033904322688191091636254553 24.46875, 0.00000000002362418831915774634545416781306734767673743045194094613975303662 24.5, 0.00000000002289734845645552894085224694165523418863054340810247859727246808 24.53125, 0.00000000002219287110538231691976934719701139869171110483083766976227371828 24.5625, 0.00000000002151006824378630064482773483363256384364849134513225918982419859 24.59375, 0.00000000002084827301773189028695922677165790846625388881142824917384476088 24.625, 0.00000000002020683909022217083651288538281609157834629515186383110686552101 24.65625, 0.00000000001958514000995906063185823123406909065664870238955772970521520689 24.6875, 0.00000000001898256859952467807972865376628562883032599774103140216355877709 24.71875, 0.00000000001839853636238638880868364368975891403085304394756710417728729226 24.75, 0.00000000001783247290814638949351185043152702641342489919832682756869223951 24.78125, 0.00000000001728382539547450296873238633954367008418616714145034208244186391 24.8125, 0.00000000001675205799218012941575742658035263880817261668796963893371791519 24.84375, 0.0000000000162366513518960372275244244459667932427370618500586757531707112 24.875, 0.00000000001573710210686290097432467045951881454892809508418045713949900434 24.90625, 0.00000000001525292237631921856001000464495935007164204622402977931351235225 24.9375, 0.00000000001478363929001648052611957789322441633071839992747920448073621267 24.96875, 0.00000000001432879452639423641760543323473459550225175116582149503141916011 25.0, 0.00000000001388794386496402059466176374608685691039976038020505558354779996 25.03125, 0.0000000000134606567524649768437209917671582206358848977381221159254967967 25.0625, 0.00000000001304651588236747115980029006237783615214919687973777468374743811 25.09375, 0.00000000001264511678731401827791807314487600957571997452646131294517036829 25.125, 0.00000000001225606744409948265495685311326361683441078344398545282179898611 25.15625, 0.00000000001187898789080476098511880728367042710695647084316969097653632446 25.1875, 0.00000000001151350985571002293233392851630933607934635958029778185634691233 25.21875, 0.00000000001115927639762509117398752373513900910996664714692603062310721874 25.25, 0.00000000001081594155728569230782478384607772133398035272900120236054656546 25.28125, 0.00000000001048317001947511756787000017765370291764705664294093802455209593 25.3125, 0.00000000001016063678554130718062135461188325773240623311810199447385148359 25.34375, 0.0000000000098480268559895247998618551834828581504447982347057213268575167 25.375, 0.000000000009545034922840628702625160301045350246501538405750091614443965342 25.40625, 0.000000000009251365071454483920567265465083431470600683227933607473210460117 25.4375, 0.000000000008966730491527303534936063064909968662316005184686156791167572542 25.46875, 0.000000000008690853196980667007631730639614504954729571760675062181956067323 25.5, 0.000000000008423463754468647405876465262881571277968769169996380269556827686 25.53125, 0.000000000008164301020237896184906551572238922772629226431694896257807673555 25.5625, 0.000000000007913111885083692042070040779415970203070308968930987093994741028 25.59375, 0.000000000007669651027152867214057161509938544278999446942202599489728556863 25.625, 0.000000000007433680672352188178877733035852826474073210893255820824327521926 25.65625, 0.000000000007204970362128195530213873785214469960068194698406482175581659916 25.6875, 0.000000000006983296728391707068224331705727703722836450808167717265381194198 25.71875, 0.000000000006768443275367165928874380122581907648183843771024011684793881264 25.75, 0.000000000006560200168153778668203310983724905459866641830583007672967646166 25.78125, 0.000000000006358364027791943233741931312793610675529846695879693297883335517 25.8125, 0.000000000006162737732634820094458708652563040537227894773299127575907126865 25.84375, 0.000000000005973130225831057668083031279896499563394402926963900174947647834 25.875, 0.000000000005789356328730651594459299271083241355006263765132804457604079334 25.90625, 0.000000000005611236560031702184725785569555171989906850921398105954194485855 25.9375, 0.000000000005438596960491441178247482370862314593668991043809629824481877267 25.96875, 0.000000000005271268923030333238200790727641185284071011194411318083467876093 26.0, 0.000000000005109089028063324719874400193479215766594073423601830189769617789 26.03125, 0.000000000004951898883897417297189081970643818589646233645174686010546633907 26.0625, 0.000000000004799544972039692018456904936060991841172673598240843591497717412 26.09375, 0.000000000004651878497264705114407929665050019788979812219968558561367688172 26.125, 0.000000000004508755242294825083742455555354837546003401079014880390805727583 26.15625, 0.000000000004370035426951585773548798577676811218837046202368270818581191458 26.1875, 0.000000000004235583571640496753820056679459962763934021402304174799328643363 26.21875, 0.000000000004105268365035984521595542843949706412618148322450110498205355941 26.25, 0.000000000003978962535837240094323145250627027327091698719811554277988766866 26.28125, 0.000000000003856542728469724370096701516850069937788978021859078413709723482 26.3125, 0.000000000003737889382610936127418351637468862040808870051125180217001918879 26.34375, 0.000000000003622886616422782472554727127428203069610048627232715649811242819 26.375, 0.000000000003511422113376511566092447685534091371940424914038629745629366959 26.40625, 0.000000000003403387012559676107419669397885356492998953149377068913072042967 26.4375, 0.000000000003298675802357996753258415752296838167645122266580873782096391664 26.46875, 0.000000000003197186217408290715310949207266177281868136812986628081131557632 26.5, 0.000000000003098819138721825441641786081838463749528227731418362871414063524 26.53125, 0.00000000000300347849688055365663374304803548375540834823281988028182537468 26.5625, 0.000000000002911071178211687139276754751234973820139804636424084435785879075 26.59375, 0.000000000002821506933848975390177156990818467567123269277808606239966277294 26.625, 0.000000000002734698291591874614840181647567890890144885369155111264004956186 26.65625, 0.000000000002650560470476524987903977953747114412200097424787493759276757321 26.6875, 0.000000000002569011297975102628800016063278193497476760152761932307850513159 26.71875, 0.000000000002489971129742679700385110630225929818656576284665692020966805694 26.75, 0.000000000002413362771833214045455987755991730675290459182091108862072684673 26.78125, 0.000000000002339111405308701231606830200593541644620980840831422643312214184 26.8125, 0.000000000002267144513167859137752284748153175896229983499111910611124289804 26.84375, 0.000000000002197391809522980568482020801087130933793025305449422234425902415 26.875, 0.000000000002129785170955785037676766913988054748094566469768479851583937037 26.90625, 0.000000000002064258569985228964868916341241888443890139720677341375648699562 26.9375, 0.000000000002000748010582296155067076916784430756884591145007904798130876335 26.96875, 0.000000000001939191465668789599631683364981179444485820606390457174237526048 27.0, 0.000000000001879528816539083294758270418422192621228717249298110713402534449 27.03125, 0.000000000001821701794145670817595306586796189513154168420501191111045904602 27.0625, 0.000000000001765653922191167662170695891046245716151892600799538957598446296 27.09375, 0.000000000001711330461971188595869587628364692734681126476070633828656492791 27.125, 0.000000000001658678358914231275244965836943773357601563943641187801945697945 27.15625, 0.000000000001607646190766354727487950399822484502913387980563642508560792423 27.1875, 0.000000000001558184117370047679591544304313218126079015373384894861080701769 27.21875, 0.000000000001510243831988238669963650058696988594888539195652349461048145555 27.25, 0.000000000001463778514125908927567934838840494728306982753933927271260839556 27.28125, 0.000000000001418742783803231625396436859649354634039130517367213754624294691 27.3125, 0.000000000001375092657235578736663445880779793553162924874233779444704718412 27.34375, 0.000000000001332785503877110728064904498598318078821737378968159045750653218 27.375, 0.000000000001291780004785996056683253834236564309813562885118738973717182616 27.40625, 0.000000000001252036112270598196259888818401234456127492806183727514623095157 27.4375, 0.000000000001213515010777218965215753002150258573156923605784722587497578956 27.46875, 0.000000000001176179078981199484799770490523379181553880614247777847325112286 27.5, 0.000000000001139991853044355345317869569640344647415229313564944302982978006 27.53125, 0.000000000001104917991002861649340363550968440663924009003749519374768121732 27.5625, 0.000000000001070923238250807645586450037918863185375333911929676641239629272 27.59375, 0.000000000001037974394085710744098065655897837768796379959390463656395318322 27.625, 0.00000000000100603927928331685742331993441781572629606080401210151237162434 27.65625, 0.0000000000009750867046700192567604663556606828609229497005742404416073899437 27.6875, 0.0000000000009450864406622024481305618988019999263563271866913462197886210171 27.71875, 0.000000000000916009187742761913208636118741833369279409748363998833295431578 27.75, 0.0000000000008878265478459658447309919032692854118517297039185521506931272157 27.78125, 0.0000000000008605109966227121313162241295356142252797153292772606272943886037 27.8125, 0.0000000000008340358565590936774931037907833197876203249281403033135552141412 27.84375, 0.0000000000008083752709220185214666367772818030905893004412044324790629551062 27.875, 0.0000000000007835041785064389495850722208831585990391508811210106620151738884 27.90625, 0.0000000000007593982891595266914647092175310950216342011521376385739541478092 27.9375, 0.0000000000007360340600578900778843799863897468924961056514817290056968406479 27.96875, 0.000000000000713388672714664495951601201119798500687445075539289264321401908 28.0, 0.0000000000006914400106940203009412584658741409271181527296346090570501618862 28.03125, 0.0000000000006701666380113232377900390890042345453598340940757600548092303626 28.0625, 0.0000000000006495477781978520622513285212254326217167982480860928098748537069 28.09375, 0.0000000000006295632940096270861715595823331687885605545394719444972541354501 28.125, 0.0000000000006101936677605324371143444097035870402061804595206497946665724486 28.15625, 0.0000000000005914199822605245340051586711231695192194212198813824698284273247 28.1875, 0.0000000000005732239023403102330666774972492382990997661300308001296001472296 28.21875, 0.000000000000555587656944450869212194856882314271929814723265033414981511437 28.25, 0.0000000000005384940217754035666539933702275364106218183008343320580676262725 28.28125, 0.0000000000005219263024715492609464848839780345954239799954210155506357606684 28.3125, 0.0000000000005058683183027783885206601017051253347370321722902770787415824085 28.34375, 0.0000000000004903043863677106683091718887416209053200290879628612466400248802 28.375, 0.0000000000004752193062771153169720277055240248742314761737160697370688103812 28.40625, 0.0000000000004605983453085728829848013900184902384113568702899402178220467007 28.4375, 0.0000000000004464272240178801191968331265124911369291716905884977257230013525 28.46875, 0.0000000000004326921022931453878902493195200319745776524320623546695569495313 28.5, 0.0000000000004193795658379544425268072672185771476699172717181871136672587371 28.53125, 0.0000000000004064766130704054785098636083930962737352608596454456030838366859 28.5625, 0.000000000000393970642425218500673184044404547962303024182388730411290406747 28.59375, 0.0000000000003818494400465177145052517900727847506614165250185357405674069814 28.625, 0.0000000000003701011678592671957913870009821824866678929216040949050940023 28.65625, 0.000000000000358714352007709902041832784912873308540984113887351518424259478 28.6875, 0.000000000000347677871649518519942680811298194520489323789701485637850857252 28.71875, 0.0000000000003369809480947140461767326700731514194548863251582517183667090897 28.75, 0.0000000000003266131342787447136015818446803018660122273966522474050450870759 28.78125, 0.0000000000003165643045594442297920068090861372485388755079856186221586782238 28.8125, 0.0000000000003068246448279046090851995753821066584645932962019631363987094132 28.84375, 0.0000000000002973846429236054614360741921464725719776657563283707487506011626 28.875, 0.0000000000002882350793444387510169211198462044370705087698650933120508512609 28.90625, 0.0000000000002793670182425560447897948256334918477415501927956624147490574617 28.9375, 0.0000000000002707717986972444175212560197529383846894698321617015111128503383 28.96875, 0.000000000000262441026256307737523373176810635148888674333123802564667406642 29.0, 0.0000000000002543665647376922910303385614857681666602800144123121548934271248 29.03125, 0.0000000000002465405282833498686650378985039204672433581590597812818628443193 29.0625, 0.0000000000002389552736575777831431700344351582486022365023470224647818214634 29.09375, 0.0000000000002316033927823140537955879396485201628931317001156661287533031662 29.125, 0.0000000000002244777055020974138517460612913024571030310251880031685851195475 29.15625, 0.0000000000002175712525716260967333310734209941494581271075819892107467166293 29.1875, 0.0000000000002108772888590667569184176111006975625134791566789463533330428285 29.21875, 0.0000000000002043892767584755915741204355398786321574889953161490658336870876 29.25, 0.0000000000001981008798048979569085630396929471020125947047048098744992464703 29.28125, 0.0000000000001920059564859107175184459511895772557032504708487592590665102049 29.3125, 0.000000000000186098554243563421227312970963814904159288543435509811677763652 29.34375, 0.0000000000001803729036608603433946635627309679012729777514019091969818816887 29.375, 0.0000000000001748234128271056750353702769986877044201406665863973879794709982 29.40625, 0.0000000000001694446618766088143429656062408926715720280988607019795674478819 29.4375, 0.0000000000001642313976954160319642146762364022888634460629057607321613570079 29.46875, 0.0000000000001591785287908988819819777887827118164446543945133307586825897215 29.5, 0.0000000000001542811203191887832972102046746599557028600731621368063667752856 29.53125, 0.0000000000001495343892656013552966218739868910163908892100498629599255376223 29.5625, 0.0000000000001449336997733435079083684809432139235314097339433741835464527749 29.59375, 0.0000000000001404745586159411053103153832091660835036885001680360409996709514 29.625, 0.0000000000001361526108089653861003804147663390488932690018180235592944250667 29.65625, 0.0000000000001319636353567723677511499223486123185599443844369323299524862846 29.6875, 0.0000000000001279035411301013225186447594196980281873310801338069087503460495 29.71875, 0.0000000000001239683628705062144371630704627362604944947367367166653371892073 29.75, 0.0000000000001201542573177178574257935655130032575947131787750650772515588676 29.78125, 0.0000000000001164574994561546138345025805701820504863208673133412998836139031 29.8125, 0.0000000000001128744788769158182236047221795359881805160157451421495192372021 29.84375, 0.0000000000001094016962517048964473288007802328013292758261150401268064712016 29.875, 0.0000000000001060357599152384653509237021707857408205243737520329218936167377 29.90625, 0.0000000000001027733825528036503531635322126564251763150488617877261447930655 29.9375, 0.00000000000009961137798972855034934167182231626355990279969424516315758585973 29.96875, 0.00000000000009654665807963031202736645753195583649247795194522788686069126547 30.0, 0.0000000000000935762296884017460491583222337870674495832268893588041641331862 30.03125, 0.00000000000009069719177099091982820195038531505624400640274940219841093197011 30.0625, 0.00000000000008790673253811878715007745508458084949471000943526007356605391787 30.09375, 0.00000000000008520212671016775214476471428978312927766113258484513962559858263 30.125, 0.00000000000008258073285555919991304185839844014058823469735368047818326394869 30.15625, 0.00000000000008003999081102054158059541073638429398414732122389304833940782911 30.1875, 0.00000000000007757741918122229829059863817909520064743566219573624447377269316 30.21875, 0.00000000000007519061291534326475713482041018396907767522577904859818487763564 30.25, 0.00000000000007287724095819692419343177486977945948929949235942711623755343028 30.28125, 0.00000000000007063504397362510607670845202241965070838746857999775952206185879 30.3125, 0.00000000000006846183213793545743423845458085268334699421205004009870555067091 30.34375, 0.00000000000006635548300122770606364051959206056374865935512003933100209985853 30.375, 0.00000000000006431393941451999714427133928991949748908272906444170138979674864 30.40625, 0.00000000000006233520752065084781025732412063385893943738632244263907508248451 30.4375, 0.0000000000000604173548069945502012133038128592219219469047336787463984659903 30.46875, 0.00000000000005855850821808822311512658660940619300112898455968361813759145073 30.5, 0.00000000000005675685232632722461872788723806651277147710851207511617132619384 30.53125, 0.00000000000005501062755894234996942710926771080860064158948052651918547942972 30.5625, 0.00000000000005331812847952720635688032422997203085045183833154400733377141058 30.59375, 0.00000000000005167770212243743196470438416112707263544343835011785509972404668 30.625, 0.00000000000005008774637843506371533794637958044739004292333288103728547661217 30.65625, 0.00000000000004854670843000140622428965998860093849496053410531885382893537903 30.6875, 0.00000000000004705308323479026283315287890146343509630360819935784975940883709 30.71875, 0.00000000000004560541205574040548908559467309389433171995384029587358737286949 30.75, 0.00000000000004420228103641172960965413608134328763789392737619307621201699431 30.78125, 0.00000000000004284231982015370742098026972356734051096094778311351882522005538 30.8125, 0.00000000000004152420021175756171970067461402797684136716801186306046720927838 30.84375, 0.00000000000004024663488028507339388680344210137229540404084775767929862897459 30.875, 0.00000000000003900837610180715086700974167313790096888426102678664627766642413 30.90625, 0.00000000000003780821454082426717775880793588809789941083643536262769208129359 30.9375, 0.0000000000000366449780691786487441444891512771624812328311276315798578192818 30.96875, 0.00000000000003551753062130471587874420243100298454971148596041392366303254154 31.0, 0.00000000000003442477108469976458392389332851557284618351259439908190502232201 31.03125, 0.00000000000003336563222453127672188786689464883760081574742682430509214713618 31.0625, 0.00000000000003233907964133058491858033190960813098226609450201402781173388749 31.09375, 0.00000000000003134411076075493208330973962071492660316646538127621681336212758 31.125, 0.00000000000003037975385443128476623390383605324551711085590608983072603787567 31.15625, 0.00000000000002944506709092561532131244726758837502688188837815767282776061142 31.1875, 0.00000000000002853913761591078963969855015477889699011408032570901135078845422 31.21875, 0.00000000000002766108066063471380236658159991081762986715530081832506411185254 31.25, 0.00000000000002681003867781803222154873116390597902927467718703633772093127027 31.28125, 0.00000000000002598518050413745869190540711233896083456345504979813651406805532 31.3125, 0.0000000000000251857005484767864173976322895749104056694868675478862268120944 31.34375, 0.00000000000002441081800515278887672717595730894460714556684723506996023849669 31.375, 0.00000000000002365977609134761491693530573569489781939839159979259031526693162 31.40625, 0.00000000000002293184130800292254309267303935792626716345329260731214860622249 31.4375, 0.00000000000002222630272345390959084041136712890041529499987866472775368836677 31.46875, 0.0000000000000215424712791036082063556500687963761956640746203634047195181427 31.5, 0.0000000000000208796791164593355050988967622456613633950030798314832510108242 31.53125, 0.00000000000002023727892487405595911098915445354338169686393393796373270426668 31.5625, 0.00000000000001961464330935563234851933831217158428222692539159608421219058304 31.59375, 0.00000000000001901116417782654125524358008998805879558683673767931447314114668 31.625, 0.00000000000001842625214723562521700460207352515644968388951498025805315956558 31.65625, 0.00000000000001785933596794186535006602668820625770508538378375159376246222395 31.6875, 0.0000000000000173098619658080034713032174918301520086460462455913951782246162 31.71875, 0.00000000000001677729350145913893259476358722179991461910440182422512419472669 31.75, 0.00000000000001626111044617818941534293327338638741095039130088533385404956645 31.78125, 0.00000000000001576080867392635319261284729670397523401541315017739880785270671 31.8125, 0.00000000000001527589956899245871966753899790293282736557414092682971654588096 31.84375, 0.00000000000001480590954879035124107050793207955166055060491245069590322903612 31.875, 0.00000000000001435037960133826031132337455133011270673032653876563776089319378 31.90625, 0.00000000000001390886483696843116484910998078500051803540884991703036941764308 31.9375, 0.00000000000001348093405382920074412826829378359299931738508402274850585101743 31.96875, 0.00000000000001306616931675516947519282725323019767808212108759017055165579301 32.0, 0.00000000000001266416554909417572312090415596509638213583379530965917253757587 32.03125, 0.00000000000001227453013709243401754048325235314398822817036371392675748028511 32.0625, 0.00000000000001189688254645146296802261665478704348065092118738604612868112709 32.09375, 0.00000000000001153085395068231626997201672268733885470557940538224246625394659 32.125, 0.00000000000001117608687089415194301820536575819782967215206765483431961947329 32.15625, 0.00000000000001083223482666534219858970138282653684895183444692796326573280076 32.1875, 0.00000000000001049896199765615000772678117440715662772051029906481811399440231 32.21875, 0.00000000000001017594289563248910510986222526599671626409087589439456007962986 32.25, 0.000000000000009862862046580452066374245346140893650926785787713027173212154599 32.28125, 0.000000000000009559413682601146163446821076696597805486186912064829320397234151 32.3125, 0.00000000000000926530144328492856174915026421876168985297548473746539569456563 32.34375, 0.000000000000008980238086273390402410617856486404444329545346240554260942795881 32.375, 0.00000000000000870394520672641245385632388548046776564874384798250885845984231 32.40625, 0.000000000000008436152965420312083823384476972021891645327212182531614761072072 32.4375, 0.000000000000008176599825211530788936760245348483903717256077411900648285470143 32.46875, 0.000000000000007925032295608481657031544299599451096247262546248805022282676566 32.5, 0.000000000000007681204685202094906742597798918889636324893817601748181424203786 32.53125, 0.000000000000007444878861713274783297042976561374466886974156659783048016257963 32.5625, 0.00000000000000721582401942292008479884831418414196574587270622474606546817739 32.59375, 0.000000000000006993816453757370714822685696882520871062353642732605455661935729 32.625, 0.00000000000000677863934280913094654373717499274636431937212053442347622279962 32.65625, 0.000000000000006570082535579493365977953288042119167416536505965592361114822861 32.6875, 0.000000000000006367942346736252352265268206744637541223087088694311035052220507 32.71875, 0.000000000000006172021357686058862843947052354142597674687196812318769599446598 32.75, 0.000000000000005982128223767135435122487288777660319758547823199664046456058344 32.78125, 0.000000000000005798077487374047716947781969627202542288891733210783818886341612 32.8125, 0.000000000000005619689396832022333574998010724946448680690590611493935762273974 32.84375, 0.000000000000005446789730843916147136626333979053746717002894773382409683104367 32.875, 0.000000000000005279209628338384449872607217489492148100736157337955785703638262 32.90625, 0.000000000000005116785423553070669979883498086784641689519285457072596953114354 32.9375, 0.000000000000004959358486191752910691325158397801275098471639666089837692676214 32.96875, 0.000000000000004806775066499338082418905513785338517911073469370357785420229321 33.0, 0.000000000000004658886145103397364184245543610168411397994694119175058161438009 33.03125, 0.000000000000004515547287475591934899526717453791133253591743821163879900918183 33.0625, 0.000000000000004376618502870849893821266562634550034077703461145865999888302865 33.09375, 0.00000000000000424196410760652844926640405045644412632919776302884640458107808 33.125, 0.000000000000004111452592548034066465947386222916548790781481034628902227036337 33.15625, 0.000000000000003984956494671481468842282613106637853661332690625165858508219322 33.1875, 0.000000000000003862352272577954194276022288045168712713885090233882424274617363 33.21875, 0.000000000000003743520185837788707877116932359911701543708911678615135096858043 33.25, 0.000000000000003628344178047044634547183880785582824719966967563499987113021301 33.28125, 0.000000000000003516711763481949151405267063239420087787692897702622359409890662 33.3125, 0.000000000000003408513917240617512740336645558731942703997918404457892114395895 33.34375, 0.000000000000003303644968764757500404836682722308745844943590142597341494824337 33.375, 0.000000000000003202002498637366626741509953186425111874213965327351260324185978 33.40625, 0.000000000000003103487238555630389169112487937464837383073471667872052176103185 33.4375, 0.000000000000003008002974381330910180163052839615170814690710643254787754111168 33.46875, 0.000000000000002915456452174080922326139550202896547488215031846761170220785442 33.5, 0.000000000000002825757287115611210202875487541769782230750085059355987861740208 33.53125, 0.000000000000002738817875236163145608276500652446278444680081395812253869633543 33.5625, 0.000000000000002654553307856774605498082140069649689354373342933641056536448617 33.59375, 0.00000000000000257288128866390001784387677685331530480354595116505395350376381 33.625, 0.000000000000002493722053335376128500263841450136980956992758796903643761395253 33.65625, 0.000000000000002416998291639236833513131618456679779678523071220240214133447362 33.6875, 0.000000000000002342635071929295509497192864225550569105902316467023945698608301 33.71875, 0.000000000000002270559767963754058454981461088486829072358817051883736151395988 33.75, 0.000000000000002200701987975366648808897006250963100407940251205403841820864379 33.78125, 0.000000000000002132993505923885097244224157903895943511934251734443959626534697 33.8125, 0.000000000000002067368194863644143813716372491331112427961212211361206794106165 33.84375, 0.000000000000002003761962361210607158034875153471941101970002529731849406521418 33.875, 0.000000000000001942112687900022585134002089199827039822243164139276551574422514 33.90625, 0.000000000000001882360162210885444025768871401199207177530021440782149747884061 33.9375, 0.000000000000001824446028469072212092424056167194047872361092410398069756680151 33.96875, 0.000000000000001768313725300598997418403563976547172728507068471349013723735359 34.0, 0.000000000000001713908431542012966302720342576049241178116833744391523703773475 34.03125, 0.000000000000001661177012699742972394131737129343861755226284691177594744855484 34.0625, 0.000000000000001610067969056722791003980917401368224306250659780741020415888582 34.09375, 0.000000000000001560531385375605708599669032508252220983301414824238460328509116 34.125, 0.000000000000001512518882149448515616819583075405784122108367759750701737926829 34.15625, 0.000000000000001465983568352254274303197076036659409296887804978676070116342994 34.1875, 0.000000000000001420879995643228058344380186585273473468373574833600305141989806 34.21875, 0.000000000000001377164113980019618129683154099895593530124567477816985187940902 34.25, 0.000000000000001334793228597603001291255077878939695456095655211483103570245779 34.28125, 0.000000000000001293725958310776896523027863761058925487976485414443841707933068 34.3125, 0.000000000000001253922195099562172244109863820353976680672234799097302505539412 34.34375, 0.000000000000001215343064938026012921890228481470952329314363126074865537201919 34.375, 0.000000000000001177950889828276438478780404945605909786461075200143303501334113 34.40625, 0.000000000000001141709151002548012190203939018469819504478548896482145639121249 34.4375, 0.000000000000001106582453257440349367275775438279144402908964819823341696889614 34.46875, 0.000000000000001072536490385476747065478184922312596844728978575313530542352241 34.5, 0.000000000000001039538011670221943951336745344579022619195112821651531319005614 34.53125, 0.000000000000001007554789412236735948981743697443378997796652835463065636144589 34.5625, 0.0000000000000009765555874541539318316725521908005048018811167145703672082604339 34.59375, 0.0000000000000009465101306741359167625173871157786698218270070920042092073862904 34.625, 0.0000000000000009173890754179198539151946641603312963312343203405827398873609052 34.65625, 0.000000000000000889163980840573218390466518474258948510555104912387016581098822 34.6875, 0.0000000000000008618072811299708189402927421513598549899007766476881775747336507 34.71875, 0.0000000000000008352922585848655892269996566199940718828815865941472273678272105 34.75, 0.0000000000000008095930175212600624922416404648792650904806247811511470333426106 34.78125, 0.000000000000000784684458981594396726202268474945832777101717263769576694402722 34.8125, 0.0000000000000007605422562220508817692839194952310520564521171198220757262421729 34.84375, 0.0000000000000007371428309540348009965769337224321468335378425231969391781293251 34.875, 0.0000000000000007144633303166280805201743565920879551477593243426434680978368327 34.90625, 0.0000000000000006924816045575260575515975545967329894890422153101733109022656615 34.9375, 0.000000000000000671176185400659633919665196205475246087550941526353084767902475 34.96875, 0.0000000000000006505262650793757265102067175550545923405013114108663712497384679 35.0, 0.000000000000000630511676014698938563902119224654276139174799630129215733382794 35.03125, 0.0000000000000006111128711188273891663924239346947307552114851545915838420731489 35.0625, 0.0000000000000005923109047046262681881788117604687949861726197530153479474268309 35.09375, 0.0000000000000005740874139824745267475279700014919024853396105159212350448586278 35.125, 0.0000000000000005564246011263937469961632279450235308527585957037980962546046369 35.15625, 0.0000000000000005393052158919442198997214848337976554747822339405032911731312727 35.1875, 0.0000000000000005227125387689121387009586867398053256542471405258029826806971553 35.21875, 0.0000000000000005066303646523341152053583804835221081607173492816665516641052904 35.25, 0.0000000000000004910429870159114560157069256968877932341468337447956973659103248 35.28125, 0.0000000000000004759351825723572907697997248499100464997599443967297247827594558 35.3125, 0.0000000000000004612921964056952034979330902529760744903007481164209663002020173 35.34375, 0.0000000000000004470997275609889458651583388023090783611867334595146401423639248 35.375, 0.0000000000000004333439150774295574587173847511417690970292707600063680811748549 35.40625, 0.0000000000000004200113244511392197349246569188156443605555692970793749264376718 35.4375, 0.0000000000000004070889345144708496394038545340417222416910500090426398209969704 35.46875, 0.0000000000000003945641247189892061365477976654510433171406879042016753308177026 35.5, 0.0000000000000003824246628097135351942886256722714438017329513936660080311322421 35.53125, 0.0000000000000003706586928785839012120989270381902865434908272460379284381720833 35.5625, 0.000000000000000359254723785483718650408500972430702612740325586134902621627915 35.59375, 0.0000000000000003482016179355099684330738638792404553714764603951729556642315549 35.625, 0.0000000000000003374885804015305101371427001025037149233165211214297696957841013 35.65625, 0.0000000000000003271051483814051228554814322387791386923406212126498467732941064 35.6875, 0.000000000000000317041180979573754262915527431512182245081070756689820141842869 35.71875, 0.0000000000000003072868493030322480475221828938058188663404931911904007342218012 35.75, 0.0000000000000002978326268620228638766412995800721539609930037773826552137321939 35.78125, 0.0000000000000002886692802660645013240996076346552030578083684718617004760403094 35.8125, 0.0000000000000002797878602062359803370473456172767339244839785557012892475892687 35.84375, 0.0000000000000002711796927149052975714234137261678722279693865600144443918280321 35.875, 0.0000000000000002628363706943687433110170106560570782518036355699423673710129484 35.90625, 0.0000000000000002547497457061263923425512382801943548459655097390623859641092533 35.9375, 0.0000000000000002469119200127750305823478462298117582328327025067359998620333133 35.96875, 0.0000000000000002393152388647462960416170058016931569559834352338730716064713962 36.0, 0.0000000000000002319522830243569388312263609738080041136516077421054488658035201 36.03125, 0.0000000000000002248158615198698738835397763036661094658259502033296645104510431 36.0625, 0.000000000000000217899004622489338263889474011680130592371098279919117310949961 36.09375, 0.000000000000000211194957039431191748546125102072982390793905944982733292582876 36.125, 0.0000000000000002046971713164204274008161920594568866373442728010970893611959238 36.15625, 0.0000000000000001983993014431724942809806935942013204570009680447066736871284268 36.1875, 0.0000000000000001922951966556132769370556693442313134026800998668802541366350117 36.21875, 0.0000000000000001863788954287847195537597848312651849860651917326982651197142697 36.25, 0.000000000000000180644619654569314241762845934082691521384708575015682722863245 36.28125, 0.0000000000000001750867689985471748098627811085584773523762847214343834850079673 36.3125, 0.0000000000000001696999154304743657648915811535010498011922227325084429766746239 36.34375, 0.000000000000000164478797923040722089835840384830323710238911530567632960076891 36.375, 0.0000000000000001594183173137297441649583522073961046499646408985240653798582207 36.40625, 0.0000000000000001545135313247624445295743373375461712860521414327978328157671689 36.4375, 0.0000000000000001497596497362614146051479692013197265532714465040449105897639071 36.46875, 0.0000000000000001451520297079210207986762708720537406554490969056227273357877399 36.5, 0.0000000000000001406861712446146767248913728229635107640574543615877449205676576 36.53125, 0.0000000000000001363577128015107132761728331587194927879296022442151532050354559 36.5625, 0.0000000000000001321624270244046182216555161495682990339764876285157380756838138 36.59375, 0.0000000000000001280962166211074749992682565516834351662867417959177413142681327 36.625, 0.0000000000000001241551103598584253497128642483242442717741422796217784322843402 36.65625, 0.0000000000000001203352591908530374349672117848337900438821621296514488159022073 36.6875, 0.0000000000000001166329324870997012450374686766405488675195675909632012816304861 36.71875, 0.0000000000000001130445144009327138569815060391664063733905403138243794332722789 36.75, 0.0000000000000001095665003326236722882764496110723608321209803355667664464475113 36.78125, 0.0000000000000001061954935076422715991765372415096255851371749108704844598636925 36.8125, 0.0000000000000001029282016592237174684809255943367431676305117376487941129821743 36.84375, 0.00000000000000009976143381300280932711801152354170365248531524253698009825273135 36.875, 0.00000000000000009669209717057443272871369241620108258408361862747002111038061648 36.90625, 0.00000000000000009371719408893681532005205168552512817191621297527984170513487617 36.9375, 0.00000000000000009083381915286754390599044853627365070315014434728084593272554365 36.96875, 0.00000000000000008803915633737310213877000020577284787454845451393258504373722776 37.0, 0.00000000000000008533047625744065794278049822941244165796959767068708207939686596 37.03125, 0.00000000000000008270513350240609282748138024883887876737986737174340057735708123 37.0625, 0.00000000000000008016056405233490501481290788213067563480275358065693649969089812 37.09375, 0.00000000000000007769428277389271552252595015364329569563831618991273921738695207 37.125, 0.00000000000000007530388099325973922771711836773070215600005022767439245995698778 37.15625, 0.00000000000000007298702414371882730501051650695586674333374825704526375954615496 37.1875, 0.00000000000000007074144948561961677808837019703160660914493150321931303054591716 37.21875, 0.00000000000000006856496389649200846749912410591531819993940068885209280862707063 37.25, 0.00000000000000006645544172915070539633280106185789357558168142554952783972823624 37.28125, 0.00000000000000006441082273569894663895170725772106151537786165609015618070706255 37.3125, 0.00000000000000006242911005540393151838242513132921711964418188775921619060173525 37.34375, 0.00000000000000006050836826447880883181227536588830142249233218321808805220411005 37.375, 0.0000000000000000586467214858665663335956002641830916214558299559774319877843127 37.40625, 0.00000000000000005684235155717975607957327416694940546006862982599151197174201348 37.4375, 0.00000000000000005509349625500678866692819664371481014098951484379919190679991259 37.46875, 0.00000000000000005339844757385057936099481593703954403665235386888854318427308925 37.5, 0.00000000000000005175555005801868534851090705738829946024810467814550056195750367 37.53125, 0.00000000000000005016319918483578800569640869039793205397790647102700348777455989 37.5625, 0.00000000000000004861983979759949762596396358197155275095015211754136896837313473 37.59375, 0.00000000000000004712396458674903946877806659922302428169289520904943225956366873 37.625, 0.0000000000000000456741126177634667148351910898575562467960792003877672792615451 37.65625, 0.00000000000000004426886790435168393020674566917731800903964452367843191601375348 37.6875, 0.00000000000000004290685802554079852537374702037809314063858652614709147546417366 37.71875, 0.00000000000000004158675278531219064485383482961143975965790656597101814280123094 37.75, 0.00000000000000004030726291347624581090006089451246862349884395369796822583168843 37.78125, 0.00000000000000003906713880651697005380780352922551618449237529166765103368463295 37.8125, 0.00000000000000003786516930717674352635625391589184829517013260604693553860880126 37.84375, 0.00000000000000003670018052158930384528914701190611608852648041293033303916423432 37.875, 0.0000000000000000355710346728057215799945537172258415054717020750339845123650365 37.90625, 0.00000000000000003447662898959367323228660965858920052849461442550459160173894482 37.9375, 0.0000000000000000334158946294247664341342867317560204359678237332477161393946368 37.96875, 0.00000000000000003238779563459806157703712608077709340322011333348686422530719924 38.0, 0.0000000000000000313913279204802962870896465223191964910214748025654909203368062 38.03125, 0.00000000000000003042551829487468567988819215779597110326674418772903169694171931 38.0625, 0.00000000000000002948942350756057280317645217903254529340402219764096492776324315 38.09375, 0.00000000000000002858212932907566979385414700218997022674507576798813137302358776 38.125, 0.00000000000000002770274965784118981753583137720218789339711018678825032148171753 38.15625, 0.00000000000000002685042565475785071569050642720238083576144114919448209937315351 38.1875, 0.0000000000000000260243249044275604918044686046137901963550814129189097437735892 38.21875, 0.00000000000000002522364060218159852651870393431682415947132784977482489607978565 38.25, 0.00000000000000002444759076612131011866023980282410885636534362989946159173102073 38.28125, 0.00000000000000002369541747340176021951235585576807434264173967045287931650078699 38.3125, 0.00000000000000002296638612001246891922571538159319754513059764710082873005458457 38.34375, 0.00000000000000002225978470333229948061212331516276486910847864112588939197382329 38.375, 0.00000000000000002157492312675781190889561846065162787593155153512799450847791406 38.40625, 0.00000000000000002091113252572595291898160071974786506695343567647934123287353267 38.4375, 0.00000000000000002026776461447284776876733848940051434950952807179706203301557652 38.46875, 0.0000000000000000196441910528907111745067171436082568715898078686455798982502167 38.5, 0.00000000000000001903980283286452319096515104552386856180238153205647610962656255 38.53125, 0.00000000000000001845400968348914069523703264620541579713346487140394560021136287 38.5625, 0.00000000000000001788623949458595453902334841102199556789112559214509068864595869 38.59375, 0.00000000000000001733593775795607453859851432028681220672967699231553480667904479 38.625, 0.00000000000000001680256702582434670928045081277221936399102332777192925386020002 38.65625, 0.00000000000000001628560638594529643958076241963837319907098302479013640351484335 38.6875, 0.00000000000000001578455095285836403647636400646815223768360704362120637969138657 38.71875, 0.00000000000000001529891137479557115004729476505376443368044505830053192458104302 38.75, 0.00000000000000001482821335576004340674002904404904380866950641999930963533758987 38.78125, 0.00000000000000001437199719230863107549240374715971547759678830390627262512216334 38.8125, 0.0000000000000000139298173245862302303062587084245204549478596223254198872481602 38.84375, 0.00000000000000001350124190117332568178021261594051021101538707711984876966373689 38.875, 0.00000000000000001308585235732176752231071837118487800753285368702837107880488645 38.90625, 0.00000000000000001268324300616686864059373737718652591085565437671924114073071766 38.9375, 0.00000000000000001229302064251658378080486112719614666091002723790583446432569779 38.96875, 0.00000000000000001191480415883081402760085812124570480810861404558505485100341434 39.0, 0.0000000000000000115482241730157859862624420633238686554634107433268811999015873 39.03125, 0.00000000000000001119292266766999402518837095469633539557935515785288807311573401 39.0625, 0.00000000000000001084855264042937802512215456856225553657823293693860903937121952 39.09375, 0.00000000000000001051477776507024905878389186607902740618432411151545059105356705 39.125, 0.00000000000000001019127206303898189276986899631201586998580249099191460048722428 39.15625, 0.00000000000000000987771958508767642172937076947701730808569790870966984497475162 39.1875, 0.000000000000000009573814102704860058289981221641731323815958109513253316705596517 39.21875, 0.000000000000000009279258809039869350351252942886407312001201105764753011817096718 39.25, 0.000000000000000008993766029028821023001499686297860400877421889295224867239601041 39.28125, 0.000000000000000008717056938439069300785380651641677221214409785501086993970852339 39.3125, 0.000000000000000008448861291557756534108781397031695933028728358737219491709517721 39.34375, 0.000000000000000008188917157258506337927995029739248382771988435505178655730781405 39.375, 0.00000000000000000793697066318849089652397908658681256242170212154899445161341596 39.40625, 0.000000000000000007692775747826034786429395620499243911495557237973981439791960383 39.4375, 0.000000000000000007456093920166604365909056551451507185358217374942101314233083789 39.46875, 0.000000000000000007226694026802481980943584554223303722389901370505298542171847308 39.5, 0.00000000000000000700435202616864522061112011768362187536103749036183828932403071 39.53125, 0.00000000000000000678885076973437027141780601277934988887779068220261599360858016 39.5625, 0.000000000000000006579979789926861905832685857392307873264708982545160987441855621 39.59375, 0.000000000000000006377535094579787420073443019426365368518774207162125897244920283 39.625, 0.000000000000000006181318967705964331272002758987951804626168797652996115555663889 39.65625, 0.000000000000000005991139776399628078169610268248840414218475272173751515322207632 39.6875, 0.000000000000000005806811783679692374614303208840353252537641433958520993290263411 39.71875, 0.000000000000000005628154967091217087934244921499269681230491270961535272431110391 39.75, 0.000000000000000005454994842887922221432511600141532691875072409867449262194897642 39.78125, 0.000000000000000005287162295624037264141115302880131499114817798206523642146505764 39.8125, 0.000000000000000005124493412989058154950978718578690014887378528538644501697054427 39.84375, 0.000000000000000004966829325724104551884166559035278210844205241425781319197266595 39.875, 0.000000000000000004814016052463533001435123402155791440628678147105424875231044847 39.90625, 0.000000000000000004665904349350271814559491980372164738569781804247829310675173718 39.9375, 0.000000000000000004522349564278005672886899245631829007834535625736764606553965447 39.96875, 0.000000000000000004383211495617856764394718753377809063173600586839510980448493733 40.0, 0.000000000000000004248354255291588995329234782858658017879565554166446288050818919 40.03125, 0.000000000000000004117646136057606822044144770425261929689905887340032171239776544 40.0625, 0.000000000000000003990959482880134638486084153835191568381794457882217519840318483 40.09375, 0.000000000000000003868170568255950460580830603248109496218483321336526318265521288 40.125, 0.000000000000000003749159471376912762420307478902078700845761462631351620692165601 40.15625, 0.000000000000000003633809961010265515751686214104317164037705776883584130535650312 40.1875, 0.000000000000000003522009381982337422518633163359248682385214518701766343927242933 40.21875, 0.000000000000000003413648545154770556227762259503007670614231147478483444116970712 40.25, 0.000000000000000003308621620785824578734495752391217935395270383451505106513763934 40.28125, 0.000000000000000003206826035172608705939549465796004770378604631695339171131838127 40.3125, 0.000000000000000003108162370473297887645253149211449325312165133435989029797526754 40.34375, 0.000000000000000003012534267611495372885723052389297096173891688551327897524226661 40.375, 0.000000000000000002919848332167913985579429102259609497674385010379217687811822711 40.40625, 0.000000000000000002830014043167465976199237371201316664364933143063616432423112016 40.4375, 0.000000000000000002742943664672679092706415702181112784250325927114001245710233342 40.46875, 0.0000000000000000026585521600970972899776203303622724695833918898302825933919879 40.5, 0.000000000000000002576757109154980948124403947486451513360493624004595246976729028 40.53125, 0.000000000000000002497478627366196190865019751688722533874550264946345710434994842 40.5625, 0.000000000000000002420639288037678400038611110211989110260095548103903928384083334 40.59375, 0.000000000000000002346164046645273748667137415777100412881815895748031187785358675 40.625, 0.000000000000000002273980167542106884900240140642235723469797040415750012511330176 40.65625, 0.000000000000000002204017152921895082274179937135209041530922835415019991077651825 40.6875, 0.000000000000000002136206673967831447087363282377500777765460295179111026306199009 40.71875, 0.000000000000000002070482504119794292176394807803662574164640473230469810533830181 40.75, 0.000000000000000002006780454394708632628385410243889629177289408235403047039788028 40.78125, 0.000000000000000001945038310696890953509506758440482329695284858259590884777733507 40.8125, 0.00000000000000000188519577305715190088367074855321426279099653562506786581142323 40.84375, 0.000000000000000001827194396741315253343573106314203379515364575286140706285975471 40.875, 0.000000000000000001770977535170637281685472249758100805340949657815097915588558642 40.90625, 0.000000000000000001716490284598380182333463962190087718877340258783750301668736097 40.9375, 0.000000000000000001663679430488508403902787995024133474314529284584766296122195411 40.96875, 0.000000000000000001612493395544139050956511166363627673119611053904191479854778643 41.0, 0.000000000000000001562882189334988768090882995105834155020383128591943336625536296 41.03125, 0.000000000000000001514797359474621154565940509879598503678824784082465381688558963 41.0625, 0.000000000000000001468191944299812359949639009812810491904766398306253338427032693 41.09375, 0.000000000000000001423020427005819542911445087096988031101627854925042198668386744 41.125, 0.000000000000000001379238691192758771149653095521403817600626735441541547708157739 41.15625, 0.000000000000000001336803977779677089148846652129938095282028727671555851228490084 41.1875, 0.000000000000000001295674843244239227997803394644572567967273696432575794936729256 41.21875, 0.000000000000000001255811119147244082400844268705766816475249804208033584503842581 41.25, 0.000000000000000001217173872902440898697573216131742394565651429793063931232925138 41.28125, 0.00000000000000000117972536975333132967573564015788715175331412867636318770738663 41.3125, 0.00000000000000000114342903591982230502320516635593862801162148901585120925532783 41.34375, 0.000000000000000001108249422878737191676672011190670173845445145428658778611077712 41.375, 0.000000000000000001074152172743300091924727661726784488279100876995463430861448794 41.40625, 0.000000000000000001041103984707781430419259939988946488085794866420420376305874505 41.4375, 0.000000000000000001009072582524533262474441347100140064142485281404547598827754583 41.46875, 0.000000000000000000978026682981651011169422819952400473197590427123435967679160072 41.5, 0.0000000000000000009479359653504755945429561135510587538538451196765526360494568593 41.53125, 0.0000000000000000009187710417730970910026154526752514274517272350404040168763060139 41.5625, 0.0000000000000000008905034285609391360304939890085145857902375858075153654530716758 41.59375, 0.0000000000000000008631055183763930429524554612135919331084062813119880220421959863 41.625, 0.0000000000000000008365505532703330639649485959296700183540374282113887072087700038 41.65625, 0.0000000000000000008108125985491800970609605530388632067272319586525343274698082626 41.6875, 0.0000000000000000007858665174459913163296174374358040127187991042944260476792995154 41.71875, 0.000000000000000000761687946570838448570053937690149422446443149444868273929192666 41.75, 0.0000000000000000007382532721164985051631537456370909815564579001549960998672750693 41.78125, 0.0000000000000000007155396067962184479931449949290710062242330373317064073289500468 41.8125, 0.0000000000000000006935247674910302423441584828063113561804435324693034885363983814 41.84375, 0.0000000000000000006721872535847857263884706748847120601157580512118034564931448416 41.875, 0.0000000000000000006515062259657523829233834742993797403176788399818406644073857906 41.90625, 0.0000000000000000006314614866742620903700204150333331757993595058256907277086022344 41.9375, 0.0000000000000000006120334591765358925052285573286770266453894145602633745742883378 41.96875, 0.000000000000000000593203169245419376181688246309732366968212467131898887916115337 42.0, 0.0000000000000000005749522264293559806664380880573423424947557974323741891553019185 42.03125, 0.0000000000000000005572628060915000600835910182931983683537424293873225869878807634 42.0625, 0.0000000000000000005401176320014283428617697399260936051487162502073990065186172603 42.09375, 0.0000000000000000005234999594624481228768692037946110915330809875259128146198729508 42.125, 0.0000000000000000005073935589580236032308395555750085805856254021837743372949540135 42.15625, 0.000000000000000000491782700301348806185263683788676329741058386847255769146582599 42.1875, 0.000000000000000000476652137272586856824812172729108506824351299964451791563830694 42.21875, 0.0000000000000000004619870927287717234762569660250976174148791530856227804245222213 42.25, 0.0000000000000000004477732441718301199042103107780437992556463100742799703237452057 42.28125, 0.0000000000000000004339967097606286936845451591573122977149904712820285616500220223 42.3125, 0.0000000000000000004206440347533852788890940848911400029023266071673377524771734534 42.34375, 0.0000000000000000004077021783672033028251540324992487575147264573158798327546903281 42.375, 0.0000000000000000003951585010418958165545048828715900944592258527981543071785792864 42.40625, 0.0000000000000000003830007520956604651334704676519030056814203121800828554591668364 42.4375, 0.0000000000000000003712170577605494115914004902303788259930310800023341204354770162 42.46875, 0.0000000000000000003597959095860491523588356106313561927863631810598385783159828527 42.5, 0.0000000000000000003487261531994446734281848598801545102380250079975661551346429832 42.53125, 0.0000000000000000003379969774119908470930860176198578935411906988100092067256535572 42.5625, 0.0000000000000000003275979036602516989791821421694327113813681268831132474305603272 42.59375, 0.0000000000000000003175187757722955140899951318913466685087506803429436188691699406 42.625, 0.0000000000000000003077497500487510184399283134077388849997753558037012174771494273 42.65625, 0.0000000000000000002982812856490373793892827552445429707771598343808612172916314725 42.6875, 0.0000000000000000002891041352733788133573237693582250777919399967119129180917477006 42.71875, 0.0000000000000000002802093361315034652526905041045972604385948019307342446105058149 42.75, 0.0000000000000000002715882011892062118539010718050828636977616273846890370321374162 42.78125, 0.0000000000000000002632323106842264149442976616525503239368302199832156688670205069 42.8125, 0.0000000000000000002551335039031546742903216079354778257268291900132283154191929829 42.84375, 0.000000000000000000247283871211337562430064263450480519734882372193451075030377457 42.875, 0.0000000000000000002396757463279964116877787573643488238775163329071089324461112356 42.90625, 0.0000000000000000002323016988390157101664109824324162952728041509977800429826059473 42.9375, 0.0000000000000000002251545269400887815866241064719842498052902874605360056961351579 42.96875, 0.0000000000000000002182272504031334004335452907804147070783699825364524641571291074 43.0, 0.0000000000000000002115131037591080486631401007022651470196630163950520042536008126 43.03125, 0.0000000000000000002050055296905708661701808330090581048460533232143102767587398855 43.0625, 0.0000000000000000001986981726275281907385458311503033966679020344061658638567314108 43.09375, 0.0000000000000000001925848725403181243408466425886292978052700304494611806090094552 43.125, 0.0000000000000000001866596589234669953317458829626245144932248426238617028272505571 43.15625, 0.0000000000000000001809167449646430982256310729764028107907596663635078553285135453 43.1875, 0.0000000000000000001753505218930128665201395152858094813833124003968273065328142463 43.21875, 0.0000000000000000001699555535014798459736391324133791470875124082459499147194143955 43.25, 0.0000000000000000001647265708374566569865017517961857985235185731417524204130738058 43.28125, 0.0000000000000000001596584670569847311274115979888434255208348556441086648512860731 43.3125, 0.0000000000000000001547462924371761391387775892384022939052765968063830249326738566 43.34375, 0.0000000000000000001499852495421064517554970490942070329822455952960336436284427387 43.375, 0.0000000000000000001453706885374374413911396197246869378347138732567988647425087791 43.40625, 0.0000000000000000001408981026491936885512128020628951665774996900873351118024931315 43.4375, 0.000000000000000000136563123762257943587451567897298898429079116536631695402502657 43.46875, 0.000000000000000000132361518154286549608297708315794465501483804831363028941965497 43.5, 0.0000000000000000001282891823608784892767772841282260884379015216411903613269127991 43.53125, 0.0000000000000000001243421391679598060255024774748711599221364770118350169658647732 43.5625, 0.0000000000000000001205165337274693940931020825004018829087727691567528779566346477 43.59375, 0.0000000000000000001168086297925525730788059216913557965098539496193222862052106767 43.625, 0.0000000000000000001132148060685855790306615491842463271037073404036948417629075926 43.65625, 0.0000000000000000001097315526764672294180028169268808766429515792455814612502977749 43.6875, 0.0000000000000000001063554677247236641732700194089936957696138630680876812119424659 43.71875, 0.0000000000000000001030832539870783364058481419859445590921875198868387915843672361 43.75, 0.00000000000000000009991171568224242818037040257637069065317337082636412267181737174 43.78125, 0.00000000000000000009683775535278069950980024266089033760688888154532437851350411374 43.8125, 0.00000000000000000009385837084000453994041804755876697427837866996656927038524776829 43.84375, 0.00000000000000000009097065235193777630267485933362900255866688103448384598719203584 43.875, 0.00000000000000000008817177962149168896231745195980592969672651710431588218558086829 43.90625, 0.0000000000000000000854590191520737910057823314074138146635652575890022427517072093 43.9375, 0.00000000000000000008282972154794031627680909077959282792155360655888510466566152316 43.96875, 0.0000000000000000000802813189266851264444426899754740106876923727968716947629672327 44.0, 0.00000000000000000007781132241133796515713316729279898191826460674935183015615786578 44.03125, 0.00000000000000000007541731969962273716764348277275889287693093521013467045096617514 44.0625, 0.00000000000000000007309697270800184803268920391693937524610730174005474668492518532 44.09375, 0.00000000000000000007084801528820567919716933102886772952736781492505450938525022254 44.125, 0.00000000000000000006866825101401706529866009184406784641942658668670022192792972997 44.15625, 0.00000000000000000006655555103614925451199694874190619420260029502844796060257397767 44.1875, 0.0000000000000000000645078520031223357075613835419372652887088466701240986283377933 44.21875, 0.00000000000000000006252315404610757306989441847748124877780969125566720430902961368 44.25, 0.00000000000000000006059951882577156256676608051785944409046261939587808580324523505 44.28125, 0.00000000000000000005873506763921267628731939225107178119817782957165977905397278512 44.3125, 0.00000000000000000005692797958514094931864104042281711576170617822857396771382659256 44.34375, 0.00000000000000000005517648978550944682088842169188613462304054925485872195518220668 44.375, 0.00000000000000000005347888766186028184624077651045105854014899296525092658491463016 44.40625, 0.00000000000000000005183351526470189107153044953695691361414064276816331713101787249 44.4375, 0.00000000000000000005023876565428596816689955778987766214081772883125543870177271376 44.46875, 0.00000000000000000004869308133120265358599828237625909263288150963466462980169105986 44.5, 0.00000000000000000004719495271526123416360584691765583584108359215108059977170888907 44.53125, 0.0000000000000000000457429166711707635622390206445371003316218116514189214543523119 44.5625, 0.00000000000000000004433555507958072137646169097814226828076860801285714735605318279 44.59375, 0.00000000000000000004297149345208612922137004866391359701001827090648626493324967938 44.625, 0.00000000000000000004164939958884447966584639757830281436633347353090158728714572464 44.65625, 0.00000000000000000004036798227749345035624868347490886331726358871951643738696284656 44.6875, 0.00000000000000000003912599003209871175663872506844271423256108690547097251647346582 44.71875, 0.0000000000000000000379222098709002320013591056618116514810627694776181992906432706 44.75, 0.00000000000000000003675546613166337459663285285104768310965325156615232678969528956 44.78125, 0.0000000000000000000356246193234778111270343693813775559453484191982556249871024845 44.8125, 0.00000000000000000003452856501388286758872715348400510534586580628399128373609615795 44.84375, 0.00000000000000000003346623275023242424928658113599199814521659667393170705197561044 44.875, 0.00000000000000000003243658501424592871912064155146374104454579559817089573114207374 44.90625, 0.00000000000000000003143861620872449287066589181887985941270581795020303854948637173 44.9375, 0.00000000000000000003047135167544245801256539822933850431612657717421817386789291221 44.96875, 0.0000000000000000000295338467432552599969374549792815865794125949782409525362318587 45.0, 0.00000000000000000002862518580549393644470121629183937206836393358135167353625302566 45.03125, 0.0000000000000000000277444814257452208379377591771209822013628505963604749130971495 45.0625, 0.0000000000000000000268908734711438907838971309371708591160804875524595655127394954 45.09375, 0.00000000000000000002606352827233090737752512502107700655923029851537174710023191774 45.125, 0.00000000000000000002526163780925692552027883757927618378837679493243444805927146177 45.15625, 0.00000000000000000002448441892203599672714696107105571056822501078192672343182871079 45.1875, 0.00000000000000000002373111254607875102326777296115694092914254984790915612104456823 45.21875, 0.00000000000000000002300098297075805688995866036121944564495383628573051225930716259 45.25, 0.00000000000000000002229331712088314102582035610200812209158409834318216630471766601 45.28125, 0.00000000000000000002160742386028042538782975416629606577882284758130800996534776334 45.3125, 0.00000000000000000002094263331680092932537014332395336781296167436740111207713205861 45.34375, 0.0000000000000000000202982962280950107030071026471244487824318890410365240809228695 45.375, 0.00000000000000000001967378330751550216279304351593661777365921271164389985177315301 45.40625, 0.00000000000000000001906848462952995691247502479968367794669623077643873781299080935 45.4375, 0.00000000000000000001848180903404177184124705313385125647092099555930743014525209803 45.46875, 0.00000000000000000001791318354903842296799473357481731534943405071858737457294463368 45.5, 0.0000000000000000000173620528310029472541727757883161267527726780895743928059334086 45.53125, 0.00000000000000000001682787862254215314547576330974940826541620116821462399857855747 45.5625, 0.00000000000000000001631013922670185678641901292778164633774935400513398820361203553 45.59375, 0.0000000000000000000158083289974557381016638728322957955300023643418933771353426587 45.625, 0.00000000000000000001532195784587020677397859030302966656863176727719072267063291518 45.65625, 0.00000000000000000001485055076146297799799845272481289645325533741682855411448786434 45.6875, 0.0000000000000000000143936473482878967036787069570146847643276532122171834567411942 45.71875, 0.00000000000000000001395080137529293121573012256005135797982709684685564291486707689 45.75, 0.00000000000000000001352158034051219709172090868538791166006198834196590935671071968 45.78125, 0.00000000000000000001310556504866638277609077713751142675675069462004539080848658114 45.8125, 0.00000000000000000001270234920175904391536002038209669692845451351634223259676580055 45.84375, 0.00000000000000000001231153900226892549065838932038362395741510856084406430196819401 45.875, 0.00000000000000000001193275276855077273318884510876693198517018929134008833897954341 45.90625, 0.0000000000000000000115656205620690151108434443406697323659021482623461017835490256 45.9375, 0.00000000000000000001120978382610026415472003761184593769179277892962265231316241231 45.96875, 0.00000000000000000001086489503555176681929309327898834184579246960990413647577383811 46.0, 0.0000000000000000000105306173575538123787633244494281088055381286282993821433071057 46.03125, 0.00000000000000000001020662432249461315737574243381991821919925769436937828441926753 46.0625, 0.000000000000000000009892599505176377949769517844102475135344038644442912531922144748 46.09375, 0.000000000000000000009588236215781181769045700417496181592925138688456065665155371657 46.125, 0.000000000000000000009293237200344816219103081317936643296684636015964081870214724183 46.15625, 0.000000000000000000009007314350446090680771876354782703162531629798936031518309866913 46.1875, 0.000000000000000000008730188421828054697362328448042244686656599234151291142434270568 46.21875, 0.000000000000000000008461588761676335234410058985624218528121244223491380411158666507 46.25, 0.000000000000000000008201253044288237390172402697698021905407044599644826637555808683 46.28125, 0.000000000000000000007948927014874451905222008525972181510256078031897921261546408418 46.3125, 0.000000000000000000007704364241243155464653791012534577929939160753072028126239279662 46.34375, 0.000000000000000000007467325873123988062089541881906443041527830127235530932061038621 46.375, 0.000000000000000000007237580408896853119290315890232444061232629124392610115601527442 46.40625, 0.000000000000000000007014903469497717915910574859717027863660677132706004795496633403 46.4375, 0.000000000000000000006799077579280601243688752620551039954331331448007121524875890524 46.46875, 0.000000000000000000006589891953621728903801354632978071163012416424937875348089135553 46.5, 0.000000000000000000006387142293058422350228846869181368797906535889550594773378587856 46.53125, 0.000000000000000000006190630583761667876670326862152491578611734601851644293031278243 46.5625, 0.000000000000000000006000164904147499482821869974261185926812388720482571271310355771 46.59375, 0.000000000000000000005815559237438323978952157226875040401678689586966087004649433281 46.625, 0.000000000000000000005636633289991127851050395763354987343025765357787342751646321384 46.65625, 0.000000000000000000005463212315215137587616356189258469447154107886066629889780518924 46.6875, 0.000000000000000000005295126942906964064032796881263222095790497998793634972781256112 46.71875, 0.000000000000000000005132213013836552522792493908559446169780645838443414780593851116 46.75, 0.00000000000000000000497431141942238784500191751632954571297494557652767439431944264 46.78125, 0.000000000000000000004821267946339375188924821384288783589740407277481309865546469172 46.8125, 0.000000000000000000004672933125907633529143714270043557503686610059451579284656974195 46.84375, 0.000000000000000000004529162088115108870136881960558696399161519638394009934295040033 46.875, 0.000000000000000000004389814420131439490869312686902138702254367062158745602564588886 46.90625, 0.00000000000000000000425475402917489192225167781672752108128365210574403584420514547 46.9375, 0.00000000000000000000412384900959843775092132734035736001741663483140228187279141447 46.96875, 0.000000000000000000003996971514065161932830566843765811318300510662915573716723492756 47.0, 0.000000000000000000003873997628687187112931477497269127829318903454547759762227016886 47.03125, 0.000000000000000000003754807252006169383348449540364944810072514080933845833969807947 47.0625, 0.000000000000000000003639283977697172752348104207429690062742002621786862469787459902 47.09375, 0.000000000000000000003527314980881366004355728584070710537518739894075032315106862947 47.125, 0.00000000000000000000341879090793651015869637602409563680330430642657250515731043376 47.15625, 0.000000000000000000003313605769697620823858822294613664962045297149073137734526850547 47.1875, 0.000000000000000000003211656837943500731187641047341905907502297724522450370164391552 47.21875, 0.000000000000000000003112844545068046850651760468679142266439107732975227569811050682 47.25, 0.000000000000000000003017072386838346875877054742208869121930716984313451793950702086 47.28125, 0.000000000000000000002924246828144594553697910020302333603884565580894771961897786098 47.3125, 0.000000000000000000002834277211649775269349194434088390677191159498898238855942811025 47.34375, 0.000000000000000000002747075669249905335770872050589773411622052596412831982742282158 47.375, 0.000000000000000000002662557036258353340219184446732332032119983782280331831995366165 47.40625, 0.000000000000000000002580638768230432354260050510562338789972390939794735901972440408 47.4375, 0.000000000000000000002501240860347030412572063193162530545520404576479895942497176411 47.46875, 0.000000000000000000002424285769278545930176555977900709767844776658961424753419380728 47.5, 0.000000000000000000002349698337452817097626987583550420512792318964301314220995403503 47.53125, 0.000000000000000000002277405719653082135117489703862039067266297832700894671982146693 47.5625, 0.000000000000000000002207337311874282892411329291695569843025054160066496344691223009 47.59375, 0.000000000000000000002139424682368229874396535486344994100340633301572670727763906169 47.625, 0.000000000000000000002073601504810284506022644992316738590837973517779826016337096391 47.65625, 0.000000000000000000002009803493522286413160133234866027004085091540131176577542323948 47.6875, 0.000000000000000000001947968340688461711118451889070518624008362116813464494103042929 47.71875, 0.00000000000000000000188803565550299472146694094049436221657741565663019512575534177 47.75, 0.000000000000000000001829946905189832081389399401796366905706522500821260099791681107 47.78125, 0.000000000000000000001773645357837116710545438282318717278954188144999094408070615644 47.8125, 0.000000000000000000001719076026990421344100479491034060924204055684914670012840961061 47.84375, 0.000000000000000000001666185617950669058068982191383936177887569188986901464271080583 47.875, 0.000000000000000000001614922475724293081987169300483282067536977228039138384307226378 47.90625, 0.000000000000000000001565236534574801840173015043168697998943328450500712567921620896 47.9375, 0.00000000000000000000151707926912647916239719269271332266491890736011219988941967613 47.96875, 0.00000000000000000000147040364697246548514669553048706722984562407513921343128159799 48.0, 0.000000000000000000001425164082740935106285321028034060226297689848644563851957420727 48.03125, 0.000000000000000000001381316393574508593725188881470868264107859334574657577909567498 48.0625, 0.000000000000000000001338817755979419673496047660125917796096641212637869698787311609 48.09375, 0.000000000000000000001297626664002293682319760926054369057345799769750265961853023078 48.125, 0.000000000000000000001257702888693691270978347973605164833559768388444480492392454282 48.15625, 0.000000000000000000001219007438818827753720425591680197622717356973977510360476520511 48.1875, 0.000000000000000000001181502522777096543037573275421750650224959288860690175054959546 48.21875, 0.000000000000000000001145151511693205677952008101301519790330903485103702272172015289 48.25, 0.000000000000000000001109918903643880700483061646153668890282127219151045837924437164 48.28125, 0.000000000000000000001075770288985196176721487531006412447159893449479639702796838999 48.3125, 0.000000000000000000001042672316746673079074331244282761505405555684027704297907667295 48.34375, 0.000000000000000000001010592662059321094561086673318854109729081231068018871744647095 48.375, 0.0000000000000000000009794999945858147180569456485275642587352286262021169753239056681 48.40625, 0.0000000000000000000009493639479219707152983394647866905652373979538997752639041352617 48.4375, 0.0000000000000000000009201550899396431541544901263420084314280359943405033033059352859 48.46875, 0.0000000000000000000008918448940420716305822806554994312952368475549226397391646864683 48.5, 0.0000000000000000000008644057113036094557723120226181420040234309559977101294557389242 48.53125, 0.0000000000000000000008378107434666222935863575379832789839558529481534696996207919147 48.5625, 0.000000000000000000000812034016769184886028292719166921067748115057595080650248748748 48.59375, 0.000000000000000000000787050356578014896776473046201754228996864317808424928746664907 48.625, 0.0000000000000000000007628353628018693311752185994548910852852113406809943581067885786 48.65625, 0.0000000000000000000007393653860613912235954998595730411098530281602659468338199998383 48.6875, 0.0000000000000000000007166175045921330641583696298123930705198174399254189425681436073 48.71875, 0.0000000000000000000006945695018581994879937688824325296530185237098936202449030949564 48.75, 0.0000000000000000000006731998448546457708076873930661875728098856761077880131658855573 48.78125, 0.0000000000000000000006524876630774413423619334396317381189133170655180352147923078795 48.8125, 0.0000000000000000000006324127281404595014886001597816216851622663343444808232999692211 48.84375, 0.0000000000000000000006129554340195864292089488158142934291831228134154055902843670047 48.875, 0.0000000000000000000005940967779046550675565706947560857145037271117000730142125624214 48.90625, 0.0000000000000000000005758183416405030589822564552242700166109412625896014914685089362 48.9375, 0.0000000000000000000005581022737390293045043114639081669667404466880977619009652078984 48.96875, 0.0000000000000000000005409312719446813599870445841229256377171534080733304964219537123 49.0, 0.0000000000000000000005242885663363463937171805302832343671593431979202704955259207318 49.03125, 0.0000000000000000000005081579029491423026816885795649343695436816418234580110765780991 49.0625, 0.0000000000000000000004925235279001133412685496635736759462234176258367870052114519323 49.09375, 0.0000000000000000000004773701720023267504104331341597452508848109772300051423545553689 49.125, 0.0000000000000000000004626830358523438681091372312051459113300450424496520983207452301 49.15625, 0.0000000000000000000004484477753765015196672690713667061501310453991988385104159135075 49.1875, 0.0000000000000000000004346504878218875793315685323576244461727288529997575502956867876 49.21875, 0.0000000000000000000004212776981783289020463175321423793535695583137664289871381502818 49.25, 0.0000000000000000000004083163460181307687878625031649524624175033424790530181240012738 49.28125, 0.0000000000000000000003957537727407149826147475910967154767476696442328428571413749283 49.3125, 0.000000000000000000000383577709209699193586046726412260292319074148471977163722788883 49.34375, 0.0000000000000000000003717762637703433052774913060759768699276544643971324999103447722 49.375, 0.0000000000000000000003603379106356602980829568099362210729572634210156224118447404096 49.40625, 0.0000000000000000000003492514786298488576331578543806052274257610402224048813276139143 49.4375, 0.0000000000000000000003385061402780541721370210558728384190257961437894535233268172381 49.46875, 0.0000000000000000000003280914012318015010782545777447082240354188285714132985105884977 49.5, 0.000000000000000000000317997090019774949818153259078118271273987980982760305026019676 49.53125, 0.0000000000000000000003082133481139316304402766209732329170505613972519371097158665387 49.5625, 0.0000000000000000000002987306203012493589471845730160478034333259712856964275846737029 49.59375, 0.0000000000000000000002895396453517045334601212933204640399586287390738963268963401953 49.625, 0.0000000000000000000002806314469733661489016994239639204180886973142755782740449568125 49.65625, 0.000000000000000000000271997325045772313312354120551715052756512121258610840015281103 49.6875, 0.0000000000000000000002636288471230274133234643638453179771935939869531880018199274294 49.71875, 0.0000000000000000000002555178401983214968176500894408998584837362716246438742918319696 49.75, 0.0000000000000000000002476563827218287567173730449239895139567205665821240446730547845 49.78125, 0.0000000000000000000002400367968641894605088321530265354502176143146879991284682107496 49.8125, 0.0000000000000000000002326516410180195172097240977549836735730469098955919638738020596 49.84375, 0.0000000000000000000002254937025301243412714839504513572857370416693924250302419418911 49.875, 0.0000000000000000000002185559906573189884069227975687169901974232614945238843290928393 49.90625, 0.00000000000000000000021183172973897492160531483544793642315890499304079543973185276 49.9375, 0.0000000000000000000002053143525796254299212245858764134557387337487126996000396080914 49.96875, 0.0000000000000000000001989974940351668747209695152775659196654351849872209600059229427 50.0, 0.0000000000000000000001928749847963917783017342816527012574752832651230262910897809104 50.03125, 0.000000000000000000000186940845363782492358706207119080251518315661425521425598112596 50.0625, 0.0000000000000000000001811892802076809768861294367346661645104238346208183334301401611 50.09375, 0.0000000000000000000001756146721081312661888700620467681239740820551019325713677651539 50.125, 0.0000000000000000000001702115766688666745691579235473233904027733267275128951297312091 50.15625, 0.0000000000000000000001649747170000838713157408312754908430675092224326044592691309936 50.1875, 0.0000000000000000000001598989785648107989641519237487976556630895251020853493961130931 50.21875, 0.0000000000000000000001549794041838351814110541118053407988446234095211641726252856222 50.25, 0.0000000000000000000001502111891943152253933286421309577820391315060697434143505106618 50.28125, 0.0000000000000000000001455896767573442113234935487858758087526482309601179841556964871 50.3125, 0.0000000000000000000001411103533098861440937892041110989898676049936448848216418841568 50.34375, 0.0000000000000000000001367688441566406332984733210422315104061369460628443634137472706 50.375, 0.0000000000000000000001325609091975318330829095008112262087886909834703306787527778449 50.40625, 0.0000000000000000000001284824387866487279775454398542360743456568258319506391352526975 50.4375, 0.0000000000000000000001245294497185924319770947269558251484195395381005144640025432712 50.46875, 0.0000000000000000000001206980813383105991623377278916732656618599344113766748113529144 50.5, 0.0000000000000000000001169845917706196468585162518454188863053319692174116146739675599 50.53125, 0.0000000000000000000001133853542657323844659693334498924258959416760461815188387633678 50.5625, 0.0000000000000000000001098968536572219368472157038071066486063705909524104754447978471 50.59375, 0.0000000000000000000001065156829289626611595160449126140823396654633931444889816624621 50.625, 0.0000000000000000000001032385398876951875281049631158530931089080405845373680441731831 50.65625, 0.0000000000000000000001000622239379658709082452013795114082308759784078271722680764842 50.6875, 0.0000000000000000000000969836329562909246314352596036619235516207674407021458541260201 50.71875, 0.00000000000000000000009399976026149241312023467909414838515877259322692705697733480129 50.75, 0.00000000000000000000009110769167824720673069207291432906260636717426292470961970042384 50.78125, 0.00000000000000000000008830460269098103737283052915764317986412752412797156994598471132 50.8125, 0.00000000000000000000008558775568532802837734083209336904848069992063877726371452549838 50.84375, 0.0000000000000000000000829544972744615921943816942971029106446921409890896634471237657 50.875, 0.00000000000000000000008040225570768547845072460985721032422971082253010450525244369598 50.90625, 0.00000000000000000000007792853835875409360722745934838005336621777845864162288395686907 50.9375, 0.00000000000000000000007553092929146908041193767302046706202809965612045881336578668273 50.96875, 0.00000000000000000000007320708690017461658314769878333654925601219713637418207077259604 51.0, 0.00000000000000000000007095474162284704138983269387808073487688714545543760355576130238 51.03125, 0.00000000000000000000006877169372454531746470428781554922243124168260160567089916757545 51.0625, 0.00000000000000000000006665581114905755253022992980661877392864755875708245173705538096 51.09375, 0.00000000000000000000006460502743664540885263841772499215769937594161914959113568032495 51.125, 0.00000000000000000000006261733970585278221057572374596820782115314472353319368620498893 51.15625, 0.00000000000000000000006069080669740770001980957305274881857223914485751509753563383024 51.1875, 0.00000000000000000000005882354687830703109959964360794365171017411013390337625650598116 51.21875, 0.00000000000000000000005701373660423237662639991113120185520625649607795050233931393519 51.25, 0.00000000000000000000005525960833850248050058460195293129247588112040668673675736882373 51.28125, 0.00000000000000000000005355944892582271328989363547879047246716403032866049931734797455 51.3125, 0.00000000000000000000005191159791914570103548010638132483597820442487813047274780826257 51.34375, 0.00000000000000000000005031444595800904077987920694681773405104017280508026833956592955 51.375, 0.00000000000000000000004876643319676631935988433116848676578674481159310575394760546579 51.40625, 0.00000000000000000000004726604778117637990157830504760767677282877681784063024179740966 51.4375, 0.00000000000000000000004581182437186300914933266374029256327907162702535401237368129363 51.46875, 0.00000000000000000000004440234271320299438098120337913609573798733246839328008501380907 51.5, 0.00000000000000000000004303622624624486591398800182597112413577782862536457699775615197 51.53125, 0.00000000000000000000004171214076430364342318596632382098425386468902235671292717809993 51.5625, 0.00000000000000000000004042879310991858346732867140701318524405016746496099409482340458 51.59375, 0.00000000000000000000003918492991190132246490923226368594516164375745614726924454560151 51.625, 0.00000000000000000000003797933636124096332270797241061882063092757470214608482428815293 51.65625, 0.0000000000000000000000368108350246706032427394687520008361249057749206091929305124515 51.6875, 0.00000000000000000000003567828469473658197758020074282003861531139804822947141174202172 51.71875, 0.00000000000000000000003458057927524737989306591738839101027113140506655789383059709336 51.75, 0.00000000000000000000003351664670101364844842631969946901783200458784247910432234080663 51.78125, 0.00000000000000000000003248544789082434586322912838557669226904464964544703703674157397 51.8125, 0.00000000000000000000003148597573263641051593140815373319584557447618336334854988406139 51.84375, 0.00000000000000000000003051725409998686571196234539854738470991658431574407035800814726 51.875, 0.00000000000000000000002957833689866674259631266593132802719518648491758675198498461499 51.90625, 0.00000000000000000000002866830714272576294745914074778387602363138722049496373428053933 51.9375, 0.00000000000000000000002778627605890536924072448929862282007551667988852530841669751448 51.96875, 0.00000000000000000000002693138221862545368640889741447984190037853340198504971261103737 52.0, 0.00000000000000000000002610279069667704804702695315331864809315658855657738226659878118 52.03125, 0.0000000000000000000000252996922557993182002753128549486040546499018307676688868769212 52.0625, 0.00000000000000000000002452130255634448711287634590574728520089660955397499223408981825 52.09375, 0.00000000000000000000002376686139025881181440607134594057781676626328168832768860617851 52.125, 0.00000000000000000000002303563193863148803922831402543621303536826189958023465494034897 52.15625, 0.00000000000000000000002232690005208637363209197345434247111012363031567273704832997691 52.1875, 0.00000000000000000000002163997355331373106859041458786291410470783328585527878672026073 52.21875, 0.0000000000000000000000209741815610608123136867575951173170430274714598091669783279926 52.25, 0.00000000000000000000002032887383492106684768069275632003584221634392925479463833386508 52.28125, 0.00000000000000000000001970342014028206649740926932240826473046749238354480096788368366 52.3125, 0.00000000000000000000001909720963281192855947724253279262097688521862566055369120718882 52.34375, 0.00000000000000000000001850965026188310081988175373990338581893801097326090013152258529 52.375, 0.00000000000000000000001794016819235086709692836321527289316099401360425208119283626355 52.40625, 0.00000000000000000000001738820724412185792484609553462363656852884597953190848591556722 52.4375, 0.0000000000000000000000168532283489652254612729776103321828628215294117416050485281441 52.46875, 0.00000000000000000000001633470902403598161143311536801719048785608654241484851072226902 52.5, 0.00000000000000000000001583214286159632016192429324675848314196379412082772293519488853 52.53125, 0.00000000000000000000001534503903443656334813648163694314153950290866268232301971212311 52.5625, 0.00000000000000000000001487292181651270619154227076705215638791322933283374594297428149 52.59375, 0.00000000000000000000001441533011833239311120579592190550613590390036022995455019668487 52.625, 0.00000000000000000000001397181703663556525188775426705927794333833952022826877831621088 52.65625, 0.00000000000000000000001354194941792997774657113081021180615209133493920669533476141269 52.6875, 0.00000000000000000000001312530743545531737877456149408301540038755459990135200573118159 52.71875, 0.00000000000000000000001272148417916276604484219506739201511412998572693519699210770251 52.75, 0.0000000000000000000000123300852583095668470963245699615449193038106757761031833392343 52.78125, 0.00000000000000000000001195072841628046998982873106682932779186772776577511774514091383 52.8125, 0.00000000000000000000001158304315725987693415087666941019842537215526851193582707185782 52.84375, 0.00000000000000000000001122667038439007515708103918204015359714635948040340942542190961 52.875, 0.00000000000000000000001088126204906217365846436306525797897900138770160543091819982806 52.90625, 0.00000000000000000000001054648081099722202217222248862603460891718511247662902180617932 52.9375, 0.00000000000000000000001022199970878553398420917665752612308515813290486316812128968657 52.96875, 0.00000000000000000000000990750184056245038186504996243629580369353927686050412093292629 53.0, 0.000000000000000000000009602680054508676030230769670007490907627932879056113838935640218 53.03125, 0.000000000000000000000009307236648872920163684937077749673613999461814294215930960029603 53.0625, 0.000000000000000000000009020883081223869961322604727739196860878683160348390824790439787 53.09375, 0.000000000000000000000008743339686647540429392889661946522438459808338947737881930394447 53.125, 0.000000000000000000000008474335404614780343848476488324560550850852257071566262340261488 53.15625, 0.000000000000000000000008213607514252181594452043337965989549831587965862704551379741763 53.1875, 0.000000000000000000000007960901377757846388383216308773903120921948709323283134547237643 53.21875, 0.000000000000000000000007715970191711421381359293597692030409403986531642245600081874044 53.25, 0.000000000000000000000007478574746035517676729771539159164420901537642545910255183216747 53.28125, 0.000000000000000000000007248483190373108297613708068554223233799238380686536172366721457 53.3125, 0.000000000000000000000007025470807652737492038100304004066010222164662564206474465589912 53.34375, 0.000000000000000000000006809319794620396149783174595499543462746013041730249671520420058 53.375, 0.000000000000000000000006599819049123721548194102777378993813669062905277038590886691292 53.40625, 0.000000000000000000000006396763963940774247590157300146071948233337820966806126045749779 53.4375, 0.000000000000000000000006199956226952036665658164438831237686064480207002170903919563167 53.46875, 0.000000000000000000000006009203627460472916768374667936431423403743212873255154897376085 53.5, 0.000000000000000000000005824319868470493956849810784229682821697978241740648320068021564 53.53125, 0.000000000000000000000005645124384742491791511276103035902639363640588593003399681334524 53.5625, 0.000000000000000000000005471442166445247168273462446609155881781435739191461392857493956 53.59375, 0.000000000000000000000005303103588233982291995420904371574577733191852294180073298156401 53.625, 0.000000000000000000000005139944243587129015252183730105059740095770193988837482297709373 53.65625, 0.000000000000000000000004981804784240018837690094442250561640109459558590669351191165594 53.6875, 0.000000000000000000000004828530764558678916170620819162737267367699774220139946789712092 53.71875, 0.000000000000000000000004679972490701743002406718322537255598556324690238795531469215506 53.75, 0.000000000000000000000004535984874423162498817263892163627868590697755584420321769948966 53.78125, 0.00000000000000000000000439642729137293522352019007527064063947670703479617265564479884 53.8125, 0.000000000000000000000004261163443757462428285997151522069034208441284063035735527442898 53.84375, 0.000000000000000000000004130061227225402409222505613954436534253309440776653054882530308 53.875, 0.000000000000000000000004002992601849015847301279927752672158109436366036953771819965806 53.90625, 0.000000000000000000000003879833467074997844969612897377460813568789815602800083965910073 53.9375, 0.000000000000000000000003760463540522668392420334578483473458881180426910373429850100096 53.96875, 0.00000000000000000000000364476624051115048882870240550577918595886412412006810282838218 54.0, 0.000000000000000000000003532628572200807029735392810177208837390456582907219239618728044 54.03125, 0.000000000000000000000003423941017237737408575759278114291162036070068338905561732490062 54.0625, 0.000000000000000000000003318597426793556015514853537392155380036425030429261492201909306 54.09375, 0.000000000000000000000003216494917895990791567089872362688419014539195950390941864923393 54.125, 0.000000000000000000000003117533772949053948979065542049986177036347978737590271739970659 54.15625, 0.000000000000000000000003021617342344652038915339509859005453620644577999123188923915709 54.1875, 0.000000000000000000000002928651950070521776918095472605633787328364533194799472071890623 54.21875, 0.000000000000000000000002838546802222304374118769451724522119900743601068183487893185077 54.25, 0.000000000000000000000002751213898330407425746950789012023946162522101873320697349557762 54.28125, 0.000000000000000000000002666567945415052448162287138207137737725859308514800341350366273 54.3125, 0.000000000000000000000002584526274685570616256283030710318495014601685960630648211753178 54.34375, 0.000000000000000000000002505008760802591736856862367583466978998587963568277687284280931 54.375, 0.000000000000000000000002427937743624274522881434050136428389449127260732282632435835729 54.40625, 0.000000000000000000000002353237952360152251987254300862672386061784083630422901785850563 54.4375, 0.000000000000000000000002280836432058519271713110659591353912121138107939695508805041774 54.46875, 0.00000000000000000000000221066247235556284704845561182061257577031098429682641559515465 54.5, 0.000000000000000000000002142647538416653761805303642934845174503257114703419600359376805 54.53125, 0.00000000000000000000000207672520400235003942401233467389340955070371584473942428591509 54.5625, 0.000000000000000000000002012831086593743232862289661122413466430503411709285692899813803 54.59375, 0.000000000000000000000001950902784513787973611560674494396475317285725884526310828838705 54.625, 0.000000000000000000000001890879815983204830917979563122328278297735594316854560212982043 54.65625, 0.000000000000000000000001832703560051435917784697892588674592989456449336766231433497182 54.6875, 0.000000000000000000000001776317199344963935550683146909191159309149893244048932391199577 54.71875, 0.000000000000000000000001721665664577080262260137792132204022815639504468824680401974619 54.75, 0.000000000000000000000001668695580764907995110345530567873536534913444992483889300619965 54.78125, 0.000000000000000000000001617355215101153234116713824206416203432185080402458559880296343 54.8125, 0.000000000000000000000001567594426429673971192111659305816595808034832656758730113518045 54.84375, 0.000000000000000000000001519364616275522304432009622111959630442357473577594051144144614 54.875, 0.000000000000000000000001472618681381633861296037633014847982914502605445818752665104416 54.90625, 0.000000000000000000000001427310967705809769282973620196314126210415767705616614449408838 54.9375, 0.000000000000000000000001383397225833062695694490569282126768288441739642076527074613903 54.96875, 0.000000000000000000000001340834567759780782043400584784312865992925927387830377285150242 55.0, 0.000000000000000000000001299581425007503073600713406071485530284876939994987408094002696 55.03125, 0.000000000000000000000001259597508025398598973015902893887296329832999169493208355990098 55.0625, 0.000000000000000000000001220843766841799856680210128928298328717622331520462088064822515 55.09375, 0.000000000000000000000001183282352926361344667140814353035243435542801688322923410544972 55.125, 0.000000000000000000000001146876582225596115916099442151201818707398876767280220904969852 55.15625, 0.000000000000000000000001111590899335689313560549024399513821904468102828028807199537279 55.1875, 0.000000000000000000000001077390842777598351336303883159385464618789394660470791471626157 55.21875, 0.000000000000000000000001044243011340525944613209686814853155863816619317176719452724755 55.25, 0.000000000000000000000001012115031460895615021673610916279031904103823251238030043711272 55.28125, 0.0000000000000000000000009809755256049706068697861659800788870545167294004049014248584945 55.3125, 0.0000000000000000000000009507940816242373538311115169179817267372121589283787019798561835 55.34375, 0.0000000000000000000000009215412230536246770752440365033526265344135140254700285944425232 55.375, 0.0000000000000000000000008931883803235507089645068825869542725821985111674223917719842389 55.40625, 0.0000000000000000000000008657078628576820189552833187968315828795884014625758730089823548 55.4375, 0.0000000000000000000000008390728320291544420573099247284379893648934419076383284121281709 55.46875, 0.000000000000000000000000813257274948843519937292432240876223770613310247944223369268332 55.5, 0.0000000000000000000000007882359790600850793305675811804760580706720660880056144410456732 55.53125, 0.0000000000000000000000007639845075150340854797342229470394398104529818944121907661547499 55.5625, 0.0000000000000000000000007404791753086132890681840318202431931879148825219351376496356764 55.59375, 0.000000000000000000000000717697026146743079108959340220577334355599888727552045035230418 55.625, 0.0000000000000000000000006956158100262610840968832486983848114858330980191658251875092579 55.65625, 0.0000000000000000000000006742139615046351298135562058773358554385592201500958062401391785 55.6875, 0.0000000000000000000000006534705786382468433213047555817378225688192600482031843283613069 55.71875, 0.0000000000000000000000006333654025686761468387878386442160679023595668253825009382525424 55.75, 0.0000000000000000000000006138787977370497500601513126442185469762378909059378438013873019 55.78125, 0.0000000000000000000000005949917327071301431437682460269937457738251940026525907440731057 55.8125, 0.0000000000000000000000005766857615784161141217109326409612561666848913044204881746870158 55.84375, 0.0000000000000000000000005589430059711020445018471861825815949356642100224958988305153727 55.875, 0.0000000000000000000000005417461375653017381212640850478398761134351132590334873048438974 55.90625, 0.0000000000000000000000005250783611774838563183969869268808059058465806860588310027070947 55.9375, 0.0000000000000000000000005089233983575906958956696560546721402109083649875698749759456059 55.96875, 0.0000000000000000000000004932654714908205675529364895493592598066230301069343622399339926 56.0, 0.0000000000000000000000004780892883885469081277177042317962893889591856615410715093105308 56.03125, 0.0000000000000000000000004633800273533249714441869840640619393443504344227173179302014757 56.0625, 0.0000000000000000000000004491233227033999564021783080471510353667968758050898036250384822 56.09375, 0.000000000000000000000000435305250742579199328865392847126011150136707783728235262722466 56.125, 0.0000000000000000000000004219123161617660188552816755571147694406129423160604374089080347 56.15625, 0.0000000000000000000000004089314388588743804688952320504644306632275569373936056805292059 56.1875, 0.0000000000000000000000003963499411642521561636704803300225297042681521235286663841402254 56.21875, 0.0000000000000000000000003841555354591367913244251651847065121976253860598627501200241044 56.25, 0.0000000000000000000000003723363121750510429289070152190485523856370240070093056227296585 56.28125, 0.0000000000000000000000003608807281624184952147930279532121090244057340341414497354635108 56.3125, 0.0000000000000000000000003497775954170391544916516911842227504602701069083750620696385416 56.34375, 0.0000000000000000000000003390160701534149259525381167001906132530991744606347732659984852 56.375, 0.0000000000000000000000003285856422142535234938316570275016469295582346051416457987232155 56.40625, 0.0000000000000000000000003184761248058076895208413396726906745491223067576257578573631991 56.4375, 0.0000000000000000000000003086776445490248261575192586566713033983560642239546449120793265 56.46875, 0.0000000000000000000000002991806318367905729826824228389341867930185414598431077081906766 56.5, 0.0000000000000000000000002899758114878488106117515024620694726165873509789021333503931392 56.53125, 0.0000000000000000000000002810541936882703160936211630353843069295262899853202516937564979 56.5625, 0.0000000000000000000000002724070652116231281756224562862853854132977773162030742429869245 56.59375, 0.0000000000000000000000002640259809092698722780236990897784037393604492553399245859043965 56.625, 0.0000000000000000000000002559027554624811123470684203537291669766936165036068147151781194 56.65625, 0.0000000000000000000000002480294553883094972772432304043419583171817662522319529521456513 56.6875, 0.0000000000000000000000002403983912914173030371674977442193288229954527355878636086997021 56.71875, 0.0000000000000000000000002330021103542901800437445401369404350102883160172956851644981093 56.75, 0.0000000000000000000000002258333890585027333036269541643063206169668387931041740453571227 56.78125, 0.0000000000000000000000002188852261299272177594331698482411249023337064161876635857454879 56.8125, 0.0000000000000000000000002121508357009953435245735849059841776229097174131217759254956511 56.84375, 0.0000000000000000000000002056236406833351688685631059788036346615926339587914007494840433 56.875, 0.0000000000000000000000001992972663443105199559823431829173996569020776197800421687457865 56.90625, 0.0000000000000000000000001931655340811895161087769721126181460900349977972473141392894392 56.9375, 0.0000000000000000000000001872224553868617922416088651757092647613466918947050252994381734 56.96875, 0.0000000000000000000000001814622260012110846179739352524174042304079659273248175467167321 57.0, 0.0000000000000000000000001758792202424311648955875128803436317824026464120198324873778176 57.03125, 0.000000000000000000000000170467985512748847656679811475586103144424452541299143678201292 57.0625, 0.0000000000000000000000001652232369731881298876242246673711582490617950686266247811337565 57.09375, 0.0000000000000000000000001601398523821746135373599514374092683812806867523820768905505568 57.125, 0.0000000000000000000000001552128670929393755817942736311831564810757844123878066740137128 57.15625, 0.0000000000000000000000001504374692048365402274819032961023992886050157352074352842197686 57.1875, 0.0000000000000000000000001458089948638391264699844439593729453536701111715592449207378431 57.21875, 0.0000000000000000000000001413229237076234379878491443806545166709638373137483085601111095 57.25, 0.0000000000000000000000001369748744507934735927597489874655716586359893514934714402700393 57.28125, 0.0000000000000000000000001327606006059337030829273776609733050708665565904076287232361232 57.3125, 0.0000000000000000000000001286759863363112090298984912542481080086310090988912235632636025 57.34375, 0.0000000000000000000000001247170424361767693257685241272891218076135306507386167663431026 57.375, 0.0000000000000000000000001208799024347390737993589207579486848320786035002322165170626429 57.40625, 0.0000000000000000000000001171608188200070525838821170103475299956465565757077951568111779 57.4375, 0.0000000000000000000000001135561593788123621480730519508118378681634846537440161617596422 57.46875, 0.0000000000000000000000001100624036494375413214539592483172201130767753198704549850886421 57.5, 0.0000000000000000000000001066761394833853250683887626578971276933467013213436650787863263 57.53125, 0.0000000000000000000000001033940597129311956014897236346493048702683375604240491636781605 57.5625, 0.000000000000000000000000100212958921204562774823246810799547235531565089057828160246438 57.59375, 0.00000000000000000000000009712973031164409946040141989628411443873447319952727097039788404 57.625, 0.00000000000000000000000009414136267376981058247709251564920220149389441405036428324236769 57.65625, 0.00000000000000000000000009124493744230848144918843448567722026508547158379249062778644589 57.6875, 0.00000000000000000000000008843762584680032384987050742837944648624618566951754524819510145 57.71875, 0.00000000000000000000000008571668614890300612207803296035080272217423841073863461621499505 57.75, 0.00000000000000000000000008307946096469490233868523183337498730931950879181387320851401809 57.78125, 0.00000000000000000000000008052337466936240957070147187191680717317054755729009511559998259 57.8125, 0.00000000000000000000000007804593088173664192043226940218611044016884370361787412339363477 57.84375, 0.0000000000000000000000000756447100262227942726823710065247491223404062725210592781478239 57.875, 0.00000000000000000000000007331736696974105364153616913820887288363069051050221961399717669 57.90625, 0.00000000000000000000000007106162873137119541627576789830030661135478442149566213589542987 57.9375, 0.00000000000000000000000006887529226246400728043916732047535075560193082323778825070910505 57.96875, 0.00000000000000000000000006675622229505150443980319598119465644041989101817379347845212228 58.0, 0.00000000000000000000000006470234925645460326154039552926489376499135891786994488614968688 58.03125, 0.00000000000000000000000006271166724805157164335130391834208443764614716092400199479568804 58.0625, 0.00000000000000000000000006078223208623323653910534534682815008281038025373145892592635121 58.09375, 0.00000000000000000000000005891215940363166326487949496643670455753293639267366397290585013 58.125, 0.00000000000000000000000005709962280876788681183052821123123687820627915800324241309273088 58.15625, 0.00000000000000000000000005534285210232132989069234513880849655982424377801636181909990384 58.1875, 0.00000000000000000000000005364013154827884154867256167986317581455024838349962889084241974 58.21875, 0.00000000000000000000000005198979819827488794057015596632431130021907915809229556023231412 58.25, 0.00000000000000000000000005039024026748637554785214515953338160277486530519815592381334078 58.28125, 0.0000000000000000000000000488398955604959375575046741679222370525001379022191944319450909 58.3125, 0.00000000000000000000000004733724994558631541104429801600281405259863617547111029797648338 58.34375, 0.00000000000000000000000004588083587597576737453527456581239177341589040420478799811083779 58.375, 0.00000000000000000000000004446923095655028055781797900303520626868849312100222878476538327 58.40625, 0.00000000000000000000000004310105655469279689915070168590747669985832159036388537158263561 58.4375, 0.00000000000000000000000004177497645385273062629757532731288014126696115361627451886614807 58.46875, 0.0000000000000000000000000404896955485407966677941038385367671477357982873355450578861446 58.5, 0.00000000000000000000000003924395857947462718564164261574842593588213729374624270079873247 58.53125, 0.00000000000000000000000003803654890763986634570883643772766170948616888063908156165959867 58.5625, 0.0000000000000000000000000368662873260694399316595503842592134396532967217153792212736034 58.59375, 0.0000000000000000000000000357320309081805335610162569144535645354251653142212341912855996 58.625, 0.00000000000000000000000003463267189154451705471483856357337071829640450827218726097361767 58.65625, 0.00000000000000000000000003356713659599965781403400999368055819720411760616779035576738246 58.6875, 0.00000000000000000000000003253438437505000666808107650792722073258704061155042583214010286 58.71875, 0.00000000000000000000000003153340659952634832825245159842108585350699341449284157105115671 58.75, 0.00000000000000000000000003056322567251661707259710269230694668347713795738252405637284816 58.78125, 0.00000000000000000000000002962289407460371735530439020629423212481593516976827494888030493 58.8125, 0.00000000000000000000000002871149343847828852035450315207502892067852990812943916492815137 58.84375, 0.00000000000000000000000002782813365202264160180822183723576244465653083085942858757755172 58.875, 0.00000000000000000000000002697195198898990233496271808526774859833031504146234165834404219 58.90625, 0.00000000000000000000000002614211226642934513930674410690350425164233191298889021820165362 58.9375, 0.00000000000000000000000002533780402803502428699493541459638817421519318041983211329265653 58.96875, 0.00000000000000000000000002455824175262012625073431180616264325305035357602977207597116529 59.0, 0.00000000000000000000000002380266408694400605894324588802496330888042874348571148812732878 59.03125, 0.00000000000000000000000002307033310214265433945946173500540472415015946818920330100267278 59.0625, 0.0000000000000000000000000223605335730363938342486113167318526863007548919876556120723406 59.09375, 0.00000000000000000000000002167257227961094703014828299624011519614890786092480135712193855 59.125, 0.00000000000000000000000002100577732998967199565137522024278899832945497114060748417482913 59.15625, 0.00000000000000000000000002035949750423575269070417325237343810329378909824950380672286178 59.1875, 0.0000000000000000000000000197331016183434734243794578047094794185435111213955055834475278 59.21875, 0.00000000000000000000000001912597790779742464230961581569064674615522306490187316820159916 59.25, 0.00000000000000000000000001853753343009759808887402683517676127013173671753202556577642965 59.28125, 0.00000000000000000000000001796719348566685227280006307219058071313083991209747368887700495 59.3125, 0.000000000000000000000000017414401056575182159283560756942998626483890696575131777556856 59.34375, 0.00000000000000000000000001687861626253262765061055649951928513961256803186400300557144631 59.375, 0.00000000000000000000000001635931583361952069476176062284538787849663206638383577319222554 59.40625, 0.00000000000000000000000001585599259923911724894259375073439891225319326597897478996012469 59.4375, 0.00000000000000000000000001536815499279350378697278491803212743239073564531205144942984688 59.46875, 0.00000000000000000000000001489532657159902404938053611288928186474233937745467459675310343 59.5, 0.0000000000000000000000000144370455515723552902009539264283480094058999539509174266652798 59.53125, 0.00000000000000000000000001399286435623278891078154182531828399878148072512953750048246144 59.5625, 0.0000000000000000000000000135623491795802521770309726070029642200373059742066530081500409 59.59375, 0.00000000000000000000000001314507956242215934772988230828610423879914186731116059601623024 59.625, 0.00000000000000000000000001274064798173531523284589283947445366363998974824394302850042552 59.65625, 0.00000000000000000000000001234865945266182478015036194148185646099483793113154548178415186 59.6875, 0.00000000000000000000000001196873114275030118903964584556769823699714789380223518385648937 59.71875, 0.00000000000000000000000001160049199806562432300720424314099807622971866045715877614325247 59.75, 0.00000000000000000000000001124358238080209251662224067945239344552483278777244685201817111 59.78125, 0.00000000000000000000000001089765371804604556971972347496036830304447517318494825443683343 59.8125, 0.00000000000000000000000001056236816134492576307209705290396747162226428980006796617465711 59.84375, 0.00000000000000000000000001023739825675029775078983632846099087292883617684898756189193527 59.875, 0.000000000000000000000000009922426625012577492586113324015397421987149748129668401274750921 59.90625, 0.000000000000000000000000009617145651615134974211698719658698105070502422481221562603338459 59.9375, 0.000000000000000000000000009321257186345045009819858625594271537844086256075055316160582956 59.96875, 0.000000000000000000000000009034472252107074310842883459566671904436056554341797268707180102 60.0, 0.000000000000000000000000008756510762696520338488732800739166036557107481781758906056715424 60.03125, 0.000000000000000000000000008487101249255267047669969506161075012136655634148415857589728219 60.0625, 0.000000000000000000000000008225980595143903024275237418875813878928171889017144085243091777 60.09375, 0.000000000000000000000000007972893778970966997494854184673974919887338238640382054594636187 60.125, 0.000000000000000000000000007727593625528352301905634330594937332574117225995965937626332205 60.15625, 0.000000000000000000000000007489840564389623350714946637241939458411365000841511792870960677 60.1875, 0.000000000000000000000000007259402395935481103088263040892172839931091084279882992628073586 60.21875, 0.000000000000000000000000007036054064577868173949975365377521192985881060588326145188101141 60.25, 0.000000000000000000000000006819577438961234728288210582211392819245838040442943236493100954 60.28125, 0.000000000000000000000000006609761098926300490085962897979944935548157365947580145619113076 60.3125, 0.000000000000000000000000006406400129028252733565200604454472625466651448577569174551406236 60.34375, 0.000000000000000000000000006209295918407721461736311048163498448790834203630929469147016371 60.375, 0.000000000000000000000000006018255966819077366107030027424840940450279439145625115620827655 60.40625, 0.000000000000000000000000005833093696626611661289456817611217634546544942852719034838727288 60.4375, 0.000000000000000000000000005653628270584985372187289362553859512955313996759834500861935158 60.46875, 0.00000000000000000000000000547968441522598481179009842355547771005768733403285698514413651 60.5, 0.000000000000000000000000005311092249679095341554413295375012238126121884444085844302264863 60.53125, 0.000000000000000000000000005147687119758712401373115963765332885813854053416435985955860413 60.5625, 0.000000000000000000000000004989309437155952415161572797015883452396538814167887138977826877 60.59375, 0.000000000000000000000000004835804523578011544595320443060749414520291773583252052275313996 60.625, 0.000000000000000000000000004687022459682852246438158668500542195832582991739572866730494891 60.65625, 0.000000000000000000000000004542817938661680907318516149073444541924993167430326044571282062 60.6875, 0.000000000000000000000000004403050124326219057671298093982983045131860569820133203086671242 60.71875, 0.000000000000000000000000004267582513562170237062560610605846056677419821148569633269288746 60.75, 0.000000000000000000000000004136282803014548793060510261567091032948083405347291367535101794 60.78125, 0.000000000000000000000000004009022759874669909814923864072357214214582010147445320411643197 60.8125, 0.000000000000000000000000003885678096642606017033079370191342975637288838419966559911618052 60.84375, 0.000000000000000000000000003766128349742797337379361267336515304096654499757741517113728271 60.875, 0.000000000000000000000000003650256761874267482395154866400709895475670672787231600350593622 60.90625, 0.000000000000000000000000003537950167980542379087744115997330791696005050880482142105536088 60.9375, 0.000000000000000000000000003429098884727905963548323347472445256868425680695839079519919924 60.96875, 0.000000000000000000000000003323596603384052466909917143297821833078586714558538267943871592 61.0, 0.000000000000000000000000003221340285992516089001247775848943753444415064650467445206776075 61.03125, 0.000000000000000000000000003122230064741477649591141772239480599949752275274856710796898351 61.0625, 0.000000000000000000000000003026169144428667569759782812574098088133391486214873614933570542 61.09375, 0.00000000000000000000000000293306370792710831356914669384893898025292227487064280972382533 61.125, 0.000000000000000000000000002842822824559370165983459252384385990717424594209049201269554784 61.15625, 0.000000000000000000000000002755358361290854799200846585629314331851088265875242144957277426 61.1875, 0.000000000000000000000000002670584896655374260425460111656104560479127785593155367195427641 61.21875, 0.000000000000000000000000002588419637328961488507258747950151671150517245918454303208278992 61.25, 0.000000000000000000000000002508782337270434840948424174075981191480340319783307515499022011 61.28125, 0.000000000000000000000000002431595219349745912482555486318528321500863698246461095670183206 61.3125, 0.000000000000000000000000002356782899387569600021154616638360522308163670777235188613292462 61.34375, 0.000000000000000000000000002284272312531950289152686704900434729237596078593742951783932121 61.375, 0.00000000000000000000000000221399264190010050448678264654002580690443718076656524134658512 61.40625, 0.000000000000000000000000002145875249415660609113128836753716455373952246648350811786603654 61.4375, 0.000000000000000000000000002079853608773872317860612900750588224058017789903790888043273966 61.46875, 0.000000000000000000000000002015863240469196998992984515286991487108433942429166284484824547 61.5, 0.000000000000000000000000001953841648821924009129718924926113460392180384121285253867364956 61.53125, 0.0000000000000000000000000018937282609422666037557181884113574323495399760833423483431273 61.5625, 0.000000000000000000000000001835464367572335197376711812785134392816050507852284105940705394 61.59375, 0.000000000000000000000000001778993065748211761221623121224302083468096969288285219697665746 61.625, 0.000000000000000000000000001724259203226126733563654451088481562524019679188172950450704679 61.65625, 0.000000000000000000000000001671209324618462714294658804371604034389050832276752431461426079 61.6875, 0.00000000000000000000000000161979161918697910399561047120906444202226212299795486932185717 61.71875, 0.000000000000000000000000001569955870242270359282971550649090255441599857953687981445216126 61.75, 0.000000000000000000000000001521653406100039251382779965355347077352697774600085535289486926 61.78125, 0.000000000000000000000000001474837052546286965764442031643698427830338564372433763863612061 61.8125, 0.000000000000000000000000001429461086764995552190650070009754011906871635468538896876327673 61.84375, 0.000000000000000000000000001385481192683306565958031710268538113092548305551788253431343434 61.875, 0.000000000000000000000000001342854417690584127383490309605304488077468900770828700052532 61.90625, 0.000000000000000000000000001301539130689092419783466886332948310383183515117455137097824588 61.9375, 0.000000000000000000000000001261494981435318156749097117505501138452434653602508547234555429 61.96875, 0.000000000000000000000000001222682861132229047572650505088627959029548672622388549713843533 62.0, 0.000000000000000000000000001185064864233981006285030739097280989067371470800345712114149624 62.03125, 0.000000000000000000000000001148604251425770978100204526405046307586455152482883058928403778 62.0625, 0.00000000000000000000000000111326541374267995359964805050465605590257678345884739646119273 62.09375, 0.000000000000000000000000001079013837792463126616869106145341552332272132192747732953636774 62.125, 0.000000000000000000000000001045816072048322312901274994950454686551163537420448573125050279 62.15625, 0.000000000000000000000000001013639694178740736229516790958887341100652969543549311392510385 62.1875, 0.0000000000000000000000000009824532793824731272716459745673084833130443528689365147049585005 62.21875, 0.00000000000000000000000000095222636969776575738969909192966531352175648028462317307418192 62.25, 0.0000000000000000000000000009229294442558325033945178648071933723737656590338382266675557852 62.28125, 0.0000000000000000000000000008945338904495352393620147370883606391791371277259258264287990018 62.3125, 0.0000000000000000000000000008670119759891106785720105970155021153381019101892627011314183832 62.34375, 0.000000000000000000000000000840336821817652115430132152510327491635541579719456727559957345 62.375, 0.000000000000000000000000000814482375859894189957175917734211197005626969676776995290231828 62.40625, 0.0000000000000000000000000007894233875786626361404518096287454058012539716224069394595716239 62.4375, 0.000000000000000000000000000765135383314139774966957486658124594538790473628320903768946727 62.46875, 0.0000000000000000000000000007415946423818610436735314460131421040383398343688538723106196135 62.5, 0.0000000000000000000000000007187781739060988613271766074046810007195949732935031414245373197 62.53125, 0.0000000000000000000000000006966636943660083409896181348726158614391896992566392194612878627 62.5625, 0.0000000000000000000000000006752296058326054718580303499074469514706238920701024679816699986 62.59375, 0.0000000000000000000000000006544549748753230907620478744974750517481675572664608528056786686 62.625, 0.0000000000000000000000000006343195121175439001720883338667135006206597628223021307798225323 62.65625, 0.0000000000000000000000000006148035524211436080987383551543658662265547941464318603769929101 62.6875, 0.0000000000000000000000000005958880356806915829509742891038121448981850098362661059894104581 62.71875, 0.0000000000000000000000000005775544882085518335407444364104611431310620429072650501484973949 62.75, 0.0000000000000000000000000005597850046927042224822826314088145569025508001867172219901429188 62.78125, 0.0000000000000000000000000005425622307096651638546439915268729531770031736306174007058391187 62.8125, 0.0000000000000000000000000005258693457754291894528339478934301352720077611627190086160291255 62.84375, 0.0000000000000000000000000005096900469178782217168316140108084433573035372758676058144762613 62.875, 0.0000000000000000000000000004940085327546146786789901359719713736438101018982152498371308445 62.90625, 0.0000000000000000000000000004788094880606681544000369631379980974888244697420793758628320552 62.9375, 0.0000000000000000000000000004640780688110038494603813283932648515560552488878855360467944803 62.96875, 0.000000000000000000000000000449799887683224637392555817693113674022488066729100297136094252 63.0, 0.0000000000000000000000000004359610000063080973623124815888459642752411974918369426500077749 63.03125, 0.0000000000000000000000000004225478901416554598764493530920550566175788717913893131882942452 63.0625, 0.0000000000000000000000000004095474582831516262561908270040879795757485642496375869574855398 63.09375, 0.0000000000000000000000000003969470076633446464177618114115854613216913149230147324695729853 63.125, 0.0000000000000000000000000003847342321532496728118240649312008179136214467806504182651817487 63.15625, 0.0000000000000000000000000003728972042436668385594231176971403577681316158969798438114146724 63.1875, 0.0000000000000000000000000003614243633962751103346818968786424724764632899243079146693891349 63.21875, 0.0000000000000000000000000003503045047531253052829265887880803207412835698244158935832779011 63.25, 0.0000000000000000000000000003395267681935054889304922989752355975971433843593437002719449885 63.28125, 0.0000000000000000000000000003290806277274912294908434278990488837146345514444915835471167499 63.3125, 0.0000000000000000000000000003189558812158220045348975828525828248934023619250431472760890193 63.34375, 0.0000000000000000000000000003091426404060637598115267605958757464396733402206230657306859641 63.375, 0.0000000000000000000000000002996313212753265183366519364975449424020863888866261037075077733 63.40625, 0.0000000000000000000000000002904126346701053323992246936540361472218474732526836337740716107 63.4375, 0.0000000000000000000000000002814775772341030542609672169564797595669356661868488865994058588 63.46875, 0.0000000000000000000000000002728174226151746564590437978448951758549050510705109737509808063 63.5, 0.0000000000000000000000000002644237129428054344424309613160289726286717196546647951325331048 63.53125, 0.0000000000000000000000000002562882505677996390180209895484770707481569388212436870177173401 63.5625, 0.0000000000000000000000000002484030900561121717946457366476972493270908352693040173902310257 63.59375, 0.0000000000000000000000000002407605304290041835963000064291849791917889040921401873207961305 63.625, 0.0000000000000000000000000002333531076419439860790393584736212597259017703090140818440843714 63.65625, 0.0000000000000000000000000002261735872949078554803430979927084684453398150829916974742154735 63.6875, 0.0000000000000000000000000002192149575669613022775036213498344309972547185206246224475902544 63.71875, 0.0000000000000000000000000002124704223682204222488827986732011489472933119960848414262590604 63.75, 0.0000000000000000000000000002059333947025052469440001469899969398810969366160779434813684459 63.78125, 0.0000000000000000000000000001995974902342027822189310148604323732589487007406413224881346829 63.8125, 0.0000000000000000000000000001934565210530568632466504690363252627939212230413003423659106904 63.84375, 0.0000000000000000000000000001875044896307952580489945515342169108741143341777554173809181684 63.875, 0.0000000000000000000000000001817355829636918079062709975572377680196706692034775616484484326 63.90625, 0.0000000000000000000000000001761441668953429849622724570986411287202344154599856109001608751 63.9375, 0.0000000000000000000000000001707247806141142523068589662151529517081543757847297612485833259 63.96875, 0.0000000000000000000000000001654721313198822016792190013045193073154690838719438296778517809 64.0, 0.0000000000000000000000000001603810890548637852976087034142335380998363840219352921744094641 64.03125, 0.0000000000000000000000000001554466816934842126651218605147871284891245608478467123513579072 64.0625, 0.0000000000000000000000000001506640900863904070369778406447035584659786744724505837876500913 64.09375, 0.0000000000000000000000000001460286433538674612587088390160122038396825939576282572752301695 64.125, 0.0000000000000000000000000001415358143240614459250710672386655600519913576145824823036866315 64.15625, 0.0000000000000000000000000001371812151115533467714922631281177782215234599407080038311377698 64.1875, 0.0000000000000000000000000001329605928319659810139777463247992377047446946581276866758321617 64.21875, 0.000000000000000000000000000128869825448418597870683374044058922203949411620253097473778291 64.25, 0.0000000000000000000000000001249049177457726364811010140156307166934546382064229564736568755 64.28125, 0.0000000000000000000000000001210619974287369206472075828764555065400378957796706782580942952 64.3125, 0.0000000000000000000000000001173373113400215361459842230492121191771513680657258096236262117 64.34375, 0.0000000000000000000000000001137272217948468809452084742329141268836394724321573286122117871 64.375, 0.0000000000000000000000000001102282030282280160003399966511113878734062876943877243105977557 64.40625, 0.0000000000000000000000000001068368377515645854026615237371067550728769652657148815833134728 64.4375, 0.0000000000000000000000000001035498138151733270560152638793314918859974799929298063586578313 64.46875, 0.0000000000000000000000000001003639209735036630403727647509345604730039584634211318600001762 64.5, 0.00000000000000000000000000009727604774987714342626655911404843644135312981345564624561703043 64.53125, 0.000000000000000000000000000094283178397688716476950233763793471348554361947135791078176558 64.5625, 0.00000000000000000000000000009138238995510200684383207606978568897003868570482851071463574672 64.59375, 0.0000000000000000000000000000885708493903620935333864377750784691960276884865771033101259065 64.625, 0.00000000000000000000000000008584581083493778027773381737948028724041025366652583887811263887 64.65625, 0.00000000000000000000000000008320461290179112891004426701925421554007392353691789818856259693 64.6875, 0.00000000000000000000000000008064467608615516520422413568335202223021069956795213606550355214 64.71875, 0.0000000000000000000000000000781635002462812475828921796568818997675946320397491804891127226 64.75, 0.00000000000000000000000000007575866216169569082717056804473958670907383693444773308347747378 64.78125, 0.00000000000000000000000000007342781316658093571373668983172742554409137389409958846142534663 64.8125, 0.00000000000000000000000000007116867685596992528952060923129294918542718588381428201045441714 64.84375, 0.00000000000000000000000000006897904686251346092840221893329870489372864164414290339844190126 64.875, 0.00000000000000000000000000006685678470164923584864959679103494931290846181087527296105308759 64.90625, 0.00000000000000000000000000006479981768306804771923920140160890760280045150342184582482779896 64.9375, 0.00000000000000000000000000006280613688643744059115045474304146490844476989023864333579898131 64.96875, 0.00000000000000000000000000006087379519940578289244033311922883516079497801452082075264736548 65.0, 0.00000000000000000000000000005900090541597061391401260295558422532036153244409425403967219316 65.03125, 0.00000000000000000000000000005718563839335404549142148818041820740668564624498759696576786117 65.0625, 0.00000000000000000000000000005542622126558514603401378319650803409364916819836497798118518511 65.09375, 0.00000000000000000000000000005372093571204461647197475420519869045880663057084357513163048835 65.125, 0.00000000000000000000000000005206811627928074617319695429618144501958407709970329175168117622 65.15625, 0.00000000000000000000000000005046614875445766384999538259978198747318147568659411053105585038 65.1875, 0.00000000000000000000000000004891346858884732474226815080868051645627137755922343227543356524 65.21875, 0.00000000000000000000000000004740855936982555017715758444092953892023128403973013574666706806 65.25, 0.00000000000000000000000000004594995133987980669879684436488230220545566875125389898470575532 65.28125, 0.00000000000000000000000000004453621996118232559993007070574020642604269316114902550898445576 65.3125, 0.0000000000000000000000000000431659845243266647112560407468351610818213315657757296588819317 65.34375, 0.00000000000000000000000000004183790679986894617583444413138044444674570258875002075531563556 65.375, 0.00000000000000000000000000004055068973135680877920628840376946100067512433762022759622777258 65.40625, 0.00000000000000000000000000003930307616856963204938041157739010901643502684755821539738068746 65.4375, 0.00000000000000000000000000003809384763973286135673462669390066023390817744431027292893731797 65.46875, 0.00000000000000000000000000003692182316150732698687184122816955109173378945287197797816525762 65.5, 0.00000000000000000000000000003578585808559134280420773012013175384560147222858837622319312448 65.53125, 0.00000000000000000000000000003468484298080912770145262983236014684956651599449184088479441159 65.5625, 0.00000000000000000000000000003361770254959375046253727767991384970537714084379523798415324167 65.59375, 0.00000000000000000000000000003258339457780638980182307606681137464012698207674061087748347791 65.625, 0.00000000000000000000000000003158090891686625898714544334675173692267555084743317079876803441 65.65625, 0.00000000000000000000000000003060926649719710040552596520394574821598227936438801207748111009 65.6875, 0.00000000000000000000000000002966751837202674050712208490892724791319172835567106987394542551 65.71875, 0.00000000000000000000000000002875474479060583963583172727551364385587834021198495036125575452 65.75, 0.00000000000000000000000000002787005429994070327558040682798122090089626975507705992107493967 65.78125, 0.00000000000000000000000000002701258287416286927058739286127471431603512881462201557088238791 65.8125, 0.00000000000000000000000000002618149307068517681338971033848941329269242881690733903622752686 65.84375, 0.00000000000000000000000000002537597321232018379687168700935773974764020084247582182656182665 65.875, 0.00000000000000000000000000002459523659456215504574121628052945515703836076245414365579431179 65.90625, 0.0000000000000000000000000000238385207172584197424669025912314101897569733544871642826828935 65.9375, 0.0000000000000000000000000000231050865399197160444002345019003586042897575357157356294919861 65.96875, 0.00000000000000000000000000002239421775994222771595489013484452365953044561557179924714116643 66.0, 0.00000000000000000000000000002170522011303639411986569259572706328906757845744012219704285076 66.03125, 0.00000000000000000000000000002103742069517926297855144287608942970615479208592166372949865476 66.0625, 0.00000000000000000000000000002039016730542817611193546466170643985854201184043995854772320183 66.09375, 0.00000000000000000000000000001976282780895395241152627561846935014392347273713611029743420206 66.125, 0.0000000000000000000000000000191547895196714795202841609757691383332424451049018686317075771 66.15625, 0.00000000000000000000000000001856545860186476533975148136185366894157868189700608547511486354 66.1875, 0.00000000000000000000000000001799425949022205127269278968576081477361116186784093250086013975 66.21875, 0.00000000000000000000000000001744063432771456914855229683435527676170219892715264088900709342 66.25, 0.00000000000000000000000000001690404242076995063045178065965087310609584761576381416283684934 66.28125, 0.00000000000000000000000000001638395971120818858601626947537132029705121748722216124775998085 66.3125, 0.00000000000000000000000000001587987826442442091617831288476376141465678121778600310431810936 66.34375, 0.00000000000000000000000000001539130577331867466489174563692887473161290681881514129771165443 66.375, 0.00000000000000000000000000001491776507748808737508187920938903774645012966982235369228599694 66.40625, 0.00000000000000000000000000001445879369721202863208703127495161747368409754874105712762947127 66.4375, 0.00000000000000000000000000001401394338177499210310800139083562745016170782043996548738976006 66.46875, 0.0000000000000000000000000000135827796716861312496828246171706047437313177860094537257674981 66.5, 0.00000000000000000000000000001316488147436788393573303779792730910392289820654552496085184444 66.53125, 0.00000000000000000000000000001275984065289928563131413212122074267779380527346737113896150292 66.5625, 0.00000000000000000000000000001236726162741232066906181630744132405132018955459468769581933277 66.59375, 0.00000000000000000000000000001198676098875201849840490254424808808503817325034509634372299167 66.625, 0.00000000000000000000000000001161796712402297917075662131474141420309968124145513087252792674 66.65625, 0.00000000000000000000000000001126051985365662107462132206449413593543680588037961101935685874 66.6875, 0.00000000000000000000000000001091407007964469556049024737390018943804398240071552439125268923 66.71875, 0.00000000000000000000000000001057827944459551854035367757123341462014674886423190952346165273 66.75, 0.0000000000000000000000000000102528200012799390663425331576734438063878616133177431904510239 66.78125, 0.000000000000000000000000000009937373892344309610457736262242594680345021771713143331932866551 66.8125, 0.000000000000000000000000000009631633039877652287971353189568921525727729497189547877376257754 66.84375, 0.00000000000000000000000000000933529884452983928847640710992891457640402230703842961048398128 66.875, 0.000000000000000000000000000009048081893886933699945527940551794739460091120891800577388394059 66.90625, 0.000000000000000000000000000008769701679818877842571862056318237151080929623642345431341645398 66.9375, 0.000000000000000000000000000008499886324523479000362012967300744728232419859443328849232692626 66.96875, 0.00000000000000000000000000000823837231499913560752039709977439333869168959396623999297962804 67.0, 0.000000000000000000000000000007984904245686978808392694266474240819259136660240267839654831252 67.03125, 0.000000000000000000000000000007739234569031082903704750237977339959959193639984936345045092012 67.0625, 0.000000000000000000000000000007501123353713131314272123887202334067980386842534679292973359588 67.09375, 0.000000000000000000000000000007270338050325419888752532140730066468824555801059854386112077703 67.125, 0.000000000000000000000000000007046653264253343974491171378785445869293892293220577880841381065 67.15625, 0.000000000000000000000000000006829850535545556754973232241647023117571974989121729949957304403 67.1875, 0.000000000000000000000000000006619718125556810810467516562780392839695890276844667023624327216 67.21875, 0.000000000000000000000000000006416050810155109345167237587879464020287655568176235627363415194 67.25, 0.000000000000000000000000000006218649679291204504494216663053229663064757376894676452408382799 67.28125, 0.000000000000000000000000000006027321942734693941463304654289482029714540761668964113981440121 67.3125, 0.000000000000000000000000000005841880741786989349699605025055613320686622303060429049385756788 67.34375, 0.000000000000000000000000000005662144966787267944745579609455117607885063901925038125665167984 67.375, 0.000000000000000000000000000005487939080233175545605465303543079307853494317393407319463783045 67.40625, 0.000000000000000000000000000005319092945343533510568928481132649206942325109454668945675093947 67.4375, 0.000000000000000000000000000005155441659895616670750516057745236265086812410701732151414699494 67.46875, 0.000000000000000000000000000004996825395174720772220305431776989567601189863254948733038902075 67.5, 0.000000000000000000000000000004843089239878730814125881749478390059245657461564953627606108258 67.53125, 0.000000000000000000000000000004694083048825240932069172340303335094628831405365011027766364301 67.5625, 0.000000000000000000000000000004549661296313466849430010728056178452102819178075934794439231221 67.59375, 0.000000000000000000000000000004409682933997737985137156980484858178396637238298212893880530751 67.625, 0.000000000000000000000000000004274011253133762504455559385994996173105233454687522274210843632 67.65625, 0.000000000000000000000000000004142513751063129232963889939045234897814007882955252709038967792 67.6875, 0.000000000000000000000000000004015062001805649593918863513727368171016548366438836037900179757 67.71875, 0.000000000000000000000000000003891531530633154618198110308623360396143617557364986636374497233 67.75, 0.000000000000000000000000000003771801692502250532160606758790271582940879735525169720392677934 67.78125, 0.000000000000000000000000000003655755554227305249692374706911669720657926704780199155129239707 67.8125, 0.000000000000000000000000000003543279780278590961204049120392582709008239576447722674793089221 67.84375, 0.000000000000000000000000000003434264522094048491958744554140797652642632489901403755457427416 67.875, 0.000000000000000000000000000003328603310796570652614422016489052226037200205955624671207045138 67.90625, 0.000000000000000000000000000003226192953212027777660935700110490931914521059334208157904475033 67.9375, 0.000000000000000000000000000003126933431086482290802764936786237279811741861513829019613596819 67.96875, 0.00000000000000000000000000000303072780340416359862954054373215450523211586133215577285260741 68.0, 0.000000000000000000000000000002937482111710802946608880642392871963840654293470437352514713233 68.03125, 0.000000000000000000000000000002847105288349863032126582069459581939540054787979477373393741047 68.0625, 0.00000000000000000000000000000275950906752204202458900471825276039793433522065601604398833754 68.09375, 0.000000000000000000000000000002674607899081188970896627628371724956490600839136402805838081829 68.125, 0.00000000000000000000000000000259231886498244005881879952820963668795131086110226102878460227 68.15625, 0.000000000000000000000000000002512561598300975481017688238897485569651502759597639147900306243 68.1875, 0.000000000000000000000000000002435258204742307218450975500016085212082271968655650779314484553 68.21875, 0.000000000000000000000000000002360333186567441395561832499498613075767800006098046493139068463 68.25, 0.000000000000000000000000000002287713368858617327536957851197908523400952852757882718085926167 68.28125, 0.000000000000000000000000000002217327828053611285357685742096085662597923801569022113764610697 68.3125, 0.000000000000000000000000000002149107822678808579896586200435650527172008893408123374189934712 68.34375, 0.000000000000000000000000000002082986726213394975746641761465322335309718407000277211065325709 68.375, 0.000000000000000000000000000002018899962019099786062187590628246241491194483723856944372120847 68.40625, 0.000000000000000000000000000001956784940271940304164932813651483548702826478854648860409584777 68.4375, 0.000000000000000000000000000001896580996834372466208766091693100051213892424780042118678776618 68.46875, 0.000000000000000000000000000001838229334008147721371718918980725331094827658331444561771357586 68.5, 0.00000000000000000000000000000178167296311001286266921441429915843690907337758846599068823893 68.53125, 0.000000000000000000000000000001726856648814169836432719757349007195568490830959617611405833039 68.5625, 0.000000000000000000000000000001673726855207138040453624463622423651258669695491502823627088224 68.59375, 0.000000000000000000000000000001622231693502334024941456633302780559773856954481335548001852445 68.625, 0.000000000000000000000000000001572320871363304460128517258667504488789561630538400866777063015 68.65625, 0.000000000000000000000000000001523945643786119312657159104162574180724690705551391055550725497 68.6875, 0.000000000000000000000000000001477058765492954914192280126826800122760181330185941332493997467 68.71875, 0.000000000000000000000000000001431614444790372497183219403713308953050271338439479257588985833 68.75, 0.000000000000000000000000000001387568298847228255773708229822005857167550016971046330477305638 68.78125, 0.000000000000000000000000000001344877310348537461595656133166094871159872692075935428776293811 68.8125, 0.000000000000000000000000000001303499785482958978668837271833387329765473086578241511406343425 68.84375, 0.000000000000000000000000000001263395313222868991289621551316263259626057640771391002706668701 68.875, 0.000000000000000000000000000001224524725857255155674309584506281180175268362264823444046009882 68.90625, 0.000000000000000000000000000001186850060738885943136166350977418113997524967972255030009241384 68.9375, 0.000000000000000000000000000001150334523208395854697043867784478795056183795163281695604588139 68.96875, 0.00000000000000000000000000000111494245065907661247609569754419218317386974292253120054150731 69.0, 0.000000000000000000000000000001080639277707278494536649616247343147341085713163652653094177217 69.03125, 0.000000000000000000000000000001047391502434405765146885706578036573239003480398851146529132359 69.0625, 0.000000000000000000000000000001015166653667536716184189157537729784224592198706961126722981785 69.09375, 0.0000000000000000000000000000009839332592667131998050220250851189123741327484917844983055746356 69.125, 0.0000000000000000000000000000009536608153879276881827297425911181253895833219275027130442627911 69.15625, 0.0000000000000000000000000000009243197566917888032759723092507482497058750998850911028814928619 69.1875, 0.0000000000000000000000000000008958814274687698488795782566816717586884459835613997310530295675 69.21875, 0.000000000000000000000000000000868318053652840050643629514839639724219380881458214751713501614 69.25, 0.0000000000000000000000000000008416027156961458415894481270944616081318452143866411321178487248 69.28125, 0.0000000000000000000000000000008157093222782504682685483748959031499582263860083563847670379059 69.3125, 0.0000000000000000000000000000007906125848242552573975161427219381484793131892654672096676541942 69.34375, 0.0000000000000000000000000000007662879928069158705845672131577407372879207063754300164254427973 69.375, 0.000000000000000000000000000000742711789808632557177446392691005412760702422838386168039785597 69.40625, 0.0000000000000000000000000000007198609503199355401049452172340618941114176005176923406335063331 69.4375, 0.0000000000000000000000000000006977131572518059616459097929485244892945629521878705090496038526 69.46875, 0.0000000000000000000000000000006762467801398699778310020507965057643888584586231254795433162841 69.5, 0.0000000000000000000000000000006554408540191793025411169503433974338284011556879464398022906474 69.53125, 0.0000000000000000000000000000006352750589489464252412977349196244674379836054242270528807858617 69.5625, 0.0000000000000000000000000000006157297001672374993054015625575436854664757180972501783321925589 69.59375, 0.000000000000000000000000000000596785688856241140990621422328325509172932525572347751197310297 69.625, 0.0000000000000000000000000000005784245234993276931845075213693673168624310746299006465826362528 69.65625, 0.0000000000000000000000000000005606282718116914754556972937409010291393166956092690524674875547 69.6875, 0.0000000000000000000000000000005433795532269287271603894839863302866898524505627021984601614401 69.71875, 0.0000000000000000000000000000005266615219224469004900850797692317180639468019025277108664710037 69.75, 0.0000000000000000000000000000005104578503671272056601571473566712400320752039319052066565287949 69.78125, 0.0000000000000000000000000000004947527133751723648866659339321382925958809324132568178083172883 69.8125, 0.0000000000000000000000000000004795307726505658935210884795882737360767515870237964768626521933 69.84375, 0.0000000000000000000000000000004647771618070483785236924356802039392381820054003441606442938409 69.875, 0.0000000000000000000000000000004504774718489806343159231079855748333968587163413324539712425772 69.90625, 0.0000000000000000000000000000004366177370989137375225536679137009404458225022695727852176930696 69.9375, 0.0000000000000000000000000000004231844215581222148029930266570223251450575537910514753364973415 69.96875, 0.0000000000000000000000000000004101644056867795079602745169643337607638296457023043055491856724 70.0, 0.0000000000000000000000000000003975449735908646807789099753794825452332450269623793290841460543 70.03125, 0.0000000000000000000000000000003853138006032865628461918076241953484066222530493907964484774478 70.0625, 0.0000000000000000000000000000003734589412470965349079366902228621024395467792882554130208365813 70.09375, 0.0000000000000000000000000000003619688175690343241153285247426784441460746252394229570171905358 70.125, 0.0000000000000000000000000000003508322078320128602818714460357774985744196828906233531224446723 70.15625, 0.0000000000000000000000000000003400382355554987992230736022739077570466564852220264977923924439 70.1875, 0.0000000000000000000000000000003295763588930850887630314685943752374332277931638215235312542922 70.21875, 0.0000000000000000000000000000003194363603368812688947395836650102186033628775049992912883667044 70.25, 0.0000000000000000000000000000003096083367386663814984455245600936571879027716827147165389666576 70.28125, 0.0000000000000000000000000000003000826896380587286831163730964539113159355977094548203944415801 70.3125, 0.00000000000000000000000000000029085011588825656435761763904934368372934945901143186157714345 70.34375, 0.0000000000000000000000000000002819015985701944239022706058139842824159406240586604283786687569 70.375, 0.0000000000000000000000000000002732283981862414756299981533995035042301910724281496328142759903 70.40625, 0.0000000000000000000000000000002648220441248412901986500765442107398648721855280165707916263848 70.4375, 0.0000000000000000000000000000002566743263877570368977570999703923831422985997900984206959576275 70.46875, 0.0000000000000000000000000000002487772875718425872158284726228815709375186972897797268177746623 70.5, 0.0000000000000000000000000000002411232150975085867791968698541134533015570097100857540530896395 70.53125, 0.0000000000000000000000000000002337046336762934894146574946796395725099777559706948652977838875 70.5625, 0.0000000000000000000000000000002265142980101830670306959725973287906372553824812852735525427141 70.59375, 0.0000000000000000000000000000002195451857155482443013298750419629299706791791601029455303175502 70.625, 0.0000000000000000000000000000002127904904647904788210454714689523993833150877559831664844590581 70.65625, 0.0000000000000000000000000000002062436153389965297265898642630575107700563928253585711518193885 70.6875, 0.0000000000000000000000000000001998981663851105384073687415634733450436352099182414936614589535 70.71875, 0.0000000000000000000000000000001937479463713310851178912589414500489885412482680140045925935992 70.75, 0.0000000000000000000000000000001877869487346344801377514211414949668704459220451579340712719674 70.78125, 0.0000000000000000000000000000001820093517145131866698789873067221500896725744400636414483202742 70.8125, 0.0000000000000000000000000000001764095126672001381819118639684716716320892798229245826318819864 70.84375, 0.000000000000000000000000000000170981962554825982996031154351627051261282242806346816182618929 70.875, 0.000000000000000000000000000000165721400604127135772238654204198312038627377490243659679728579 70.90625, 0.0000000000000000000000000000001606226891294881059650395869007539793350109342165612102717505251 70.9375, 0.0000000000000000000000000000001556808485152620690863902908808590932103352389955675960609838849 70.96875, 0.0000000000000000000000000000001508910523524692044254705706279574323095753352230258086128161285 71.0, 0.0000000000000000000000000000001462486227251230946826378730831583441807808324481003728602428559 71.03125, 0.0000000000000000000000000000001417490256415816160208905869605426011376610095933773263786367071 71.0625, 0.000000000000000000000000000000137387866606460384042977277210229297789872172996156282390499499 71.09375, 0.0000000000000000000000000000001331608863287841004945817343882867640641915758248362320358971758 71.125, 0.0000000000000000000000000000001290639565621842011465095519722657449559649895871729746100038694 71.15625, 0.0000000000000000000000000000001250930760730801672692358415949598072968891780511848836920910209 71.1875, 0.000000000000000000000000000000121244366732906857331209452404029695850491038487116998268265111 71.21875, 0.0000000000000000000000000000001175140697305713641025466981544704245215005713414469924278734051 71.25, 0.0000000000000000000000000000001138985419014403235469465408009137230370686612591357407830762043 71.28125, 0.0000000000000000000000000000001103942521692724104152933305458619294551376571587057473611849914 71.3125, 0.0000000000000000000000000000001069977780976210624646707407478405277979222726896785884577601889 71.34375, 0.0000000000000000000000000000001037058025473393884468478653477079072235506511773952337966344664 71.375, 0.0000000000000000000000000000001005151104369228388566542781551607993377884108184115267644296258 71.40625, 0.00000000000000000000000000000009742258560252565410643319580107281171118663196476575847483323631 71.4375, 0.00000000000000000000000000000009442520775458445038764753103481359522121764762774197836418222442 71.46875, 0.00000000000000000000000000000009152004952807665406650666048464167744753768707219623832186714203 71.5, 0.00000000000000000000000000000008870427362353294318392465447405762931196665570279336455728483145 71.53125, 0.00000000000000000000000000000008597513003601148880289879940275350364652393924780522216088747993 71.5625, 0.00000000000000000000000000000008332995336932769613236407963319864289416388969569040328003746627 71.59375, 0.00000000000000000000000000000008076616023291640945629868349565358557656280559015850686323540798 71.625, 0.00000000000000000000000000000007828124671878424722954239068345945219819800542065350163918418043 71.65625, 0.00000000000000000000000000000007587278595608795308472950434704085019476380276569994440755577344 71.6875, 0.00000000000000000000000000000007353842574095046132026348316738701179399674055400354254887211604 71.71875, 0.00000000000000000000000000000007127588623919985574935929201432235102441847835136789624617117133 71.75, 0.00000000000000000000000000000006908295775978762034880693586549975249947952549335804388990379118 71.78125, 0.00000000000000000000000000000006695749859671160850927609883350359894173594138506410577987347416 71.8125, 0.00000000000000000000000000000006489743293733606227268424316533410493609165630923549136856923682 71.84375, 0.00000000000000000000000000000006290074883506585908821372552766308902827281665964298575183811821 71.875, 0.00000000000000000000000000000006096549624439501465845565104731508767886053744950884767465724197 71.90625, 0.00000000000000000000000000000005908978511641038776384306860432568178422170363152054408371743687 71.9375, 0.0000000000000000000000000000000572717835528905760414222198142550765567663738384686227840005666 71.96875, 0.00000000000000000000000000000005550971601719721821705587855286297494649125924716551214857305316 72.0, 0.00000000000000000000000000000005380186160021138413818187270454184208301634930276294865388867235 72.03125, 0.00000000000000000000000000000005214655233962149329753805398154684125391790873729894793934341011 72.0625, 0.00000000000000000000000000000005054217159092130788044350284659974008767297797774778509680280865 72.09375, 0.00000000000000000000000000000004898715244852704859755894033682099143845779537724364761539076657 72.125, 0.00000000000000000000000000000004747997621547163000413418735867298168037077664171944639911055017 72.15625, 0.00000000000000000000000000000004601917092018145446068730768295952020390586142704274417922074238 72.1875, 0.0000000000000000000000000000000446033098788871866931407262896084602765117222583357036229072388 72.21875, 0.00000000000000000000000000000004323101030226449897139976716430566600666801425019770104196594658 72.25, 0.00000000000000000000000000000004190093194494397377123780208764216742285248592367520745244441392 72.28125, 0.00000000000000000000000000000004061177579657121860300243647110250967952533245127185234622876145 72.3125, 0.00000000000000000000000000000003936228281313882737194799409439530483482618165550940611681829196 72.34375, 0.00000000000000000000000000000003815123268735115381075109856681782362560490395316811604596736667 72.375, 0.0000000000000000000000000000000369774426568209836074555680950647549845734470719956081240224672 72.40625, 0.00000000000000000000000000000003583976634893414007236964565408603552756962933750362460869326199 72.4375, 0.00000000000000000000000000000003473709266125386963048561930737436203074575048004195560863988898 72.46875, 0.00000000000000000000000000000003366834467637156306676762826126659608430715656364803871022883626 72.5, 0.00000000000000000000000000000003263247861014401018905003353116781033471980895691929570051405851 72.53125, 0.00000000000000000000000000000003162848279228999226323902630605799112959206893447851672423707811 72.5625, 0.00000000000000000000000000000003065537667835062006307623805633658134059855539625303739456749743 72.59375, 0.00000000000000000000000000000002971220989204845652719753169296219390504353263835607708391370829 72.625, 0.00000000000000000000000000000002879806129711015174527020219261512117420396899224158002602821718 72.65625, 0.00000000000000000000000000000002791203809764609329787648599760463196485228294159526440774469591 72.6875, 0.00000000000000000000000000000002705327496620846495469922454408737933461290340990838430021737459 72.71875, 0.00000000000000000000000000000002622093319867613863098180140168770807398581848783078610580295397 72.75, 0.00000000000000000000000000000002541419989514102471366281414180286315589416192294472468157024876 72.78125, 0.00000000000000000000000000000002463228716599589998425259768889812297165017943832147435885449652 72.8125, 0.00000000000000000000000000000002387443136244834518638297495484364402917032871168896668665830541 72.84375, 0.00000000000000000000000000000002313989233070927984993746319782397014897637466064951851926758181 72.875, 0.00000000000000000000000000000002242795268912770358910173755767850050377611296522745716533058123 72.90625, 0.00000000000000000000000000000002173791712756566332009385176718252850837908345196983536327998139 72.9375, 0.00000000000000000000000000000002106911172832918658251029415110424099532138545746966565458188563 72.96875, 0.00000000000000000000000000000002042088330799197360954205563681998090465025312368071198279533979 73.0, 0.00000000000000000000000000000001979259877946904553749191533601550752626140684018359343954556475 73.03125, 0.00000000000000000000000000000001918364453371732310219074649758848347287001282410019501544424674 73.0625, 0.00000000000000000000000000000001859342584045927865407332678864078368154720299600002660699382875 73.09375, 0.000000000000000000000000000000018021366267344383055579140894128424768921633124408296103885853 73.125, 0.00000000000000000000000000000001746690711698107614896041933507404325276143437703969212827606589 73.15625, 0.00000000000000000000000000000001692950688128944258602172768618931290307017294532790384824608728 73.1875, 0.00000000000000000000000000000001640864071264169093921622836634235529830247106944349468211807042 73.21875, 0.0000000000000000000000000000000159037999112739296868838528345873226537454182731276082293942171 73.25, 0.00000000000000000000000000000001541449142846862489693886455350639391003860734644287630375337478 73.28125, 0.00000000000000000000000000000001494023738502252674304240864505037691644506270631759183048449321 73.3125, 0.00000000000000000000000000000001448057460452978041777183207142464871207627675876619629497246574 73.34375, 0.00000000000000000000000000000001403505416102440613825464900851852675087420879218385931805063899 73.375, 0.0000000000000000000000000000000136032409405403569023316597962342507614364886021573678486773309 73.40625, 0.00000000000000000000000000000001318471321616095514394144241656252447803227016119532940912296443 73.4375, 0.00000000000000000000000000000001277906223614268373007412075509547289584791714859309742037313462 73.46875, 0.00000000000000000000000000000001238589182471107570489415494898342077147017476455156633030447697 73.5, 0.00000000000000000000000000000001200481799513882329018539059916184617084419921961700618549187549 73.53125, 0.00000000000000000000000000000001163546857472822198214896834249007573154091734711240051000891187 73.5625, 0.00000000000000000000000000000001127748284133169185792957744716206705592267790609789422815023923 73.59375, 0.00000000000000000000000000000001093051117105538677576450334201840902349345545880425924525727557 73.625, 0.00000000000000000000000000000001059421469680182402570194142144343697215908161873710851090133435 73.65625, 0.00000000000000000000000000000001026826497731805283017980118821413996694353213293011371140235036 73.6875, 0.000000000000000000000000000000009952343676426140243971685153184272776540280979452668449855287283 73.71875, 0.000000000000000000000000000000009646142252122697481586270710666600749957321190310325660536939917 73.75, 0.000000000000000000000000000000009349361655243808219345155165393431777506507850860706449076866924 73.78125, 0.000000000000000000000000000000009061712037401062392444716331167506798984923333383061371837343571 73.8125, 0.000000000000000000000000000000008782912467903453558092737555093816182209962958567285153368988163 73.84375, 0.000000000000000000000000000000008512690659388673867354812281799629148831923993369288571149034727 73.875, 0.00000000000000000000000000000000825078270189584665163106481599530896411321743914944724604293673 73.90625, 0.000000000000000000000000000000007996932805119980891991287018414724316239699732749231075654865853 73.9375, 0.000000000000000000000000000000007750893048596422452618821439209070596364840579753264195085017094 73.96875, 0.00000000000000000000000000000000751242313957132172731850691625749614493402212374729922644682523 74.0, 0.000000000000000000000000000000007281290178321643834296973716877553324763169176039320982529827144 74.03125, 0.000000000000000000000000000000007057268430695523030374094645608701167617170094096734006644596017 74.0625, 0.000000000000000000000000000000006840139107650814707026269327183422719949747881890466926766185465 74.09375, 0.000000000000000000000000000000006629690151576533064173105803743265315943550053821216092540847683 74.125, 0.000000000000000000000000000000006425716029188487008486604383532770214963235965347881743624524857 74.15625, 0.000000000000000000000000000000006228017530796847460942924869713239178414828518434521002346964272 74.1875, 0.000000000000000000000000000000006036401575749602354015752185939488804121342094986910293371199406 74.21875, 0.00000000000000000000000000000000585068102386188723006362426807977815013747508363125740504637236 74.25, 0.000000000000000000000000000000005670674492647025409835514681731389498326030613891300129395831889 74.28125, 0.00000000000000000000000000000000549620618017077789311449104838228156537352573123352738577557395 74.3125, 0.000000000000000000000000000000005327105693355795016180417099018604428992088446174311674467513526 74.34375, 0.000000000000000000000000000000005163207881568584786583029287287577199268406579897189027734866175 74.375, 0.000000000000000000000000000000005004352675326471943239777715270270404375956329751915475813197694 74.40625, 0.000000000000000000000000000000004850384929967022187729071355378732789537154952459016370102300107 74.4375, 0.00000000000000000000000000000000470115427412725258443744953979950498148603084278988708004726213 74.46875, 0.000000000000000000000000000000004556514962884646566284627656317125138105396245345176357739487903 74.5, 0.000000000000000000000000000000004416325735416544896809503334631776968629247559751738126278305998 74.53125, 0.000000000000000000000000000000004280449677038896775020040886602135637979280990084812978799056855 74.5625, 0.000000000000000000000000000000004148754085489632336350973451209470394907262265027881192279658105 74.59375, 0.000000000000000000000000000000004021110341326063278498580587873005752456174393233619712345547134 74.625, 0.000000000000000000000000000000003897393782309736273456722508874641779450298036061566622788131667 74.65625, 0.000000000000000000000000000000003777483581656058140844428395327331843249995030019980036605588671 74.6875, 0.000000000000000000000000000000003661262630029786255942934850607310807721644117948145740285869193 74.71875, 0.000000000000000000000000000000003548617421171136035082654496836580635972664010226374078017229968 74.75, 0.000000000000000000000000000000003439437941040803154055068471840397711902602904884167503371262931 74.78125, 0.000000000000000000000000000000003333617560375634875060826728370581874633056007914686149593136849 74.8125, 0.000000000000000000000000000000003231052930550015840788256545726712384060927040136868309931847684 74.84375, 0.000000000000000000000000000000003131643882641262193723920815517132568340995441138779810876170354 74.875, 0.000000000000000000000000000000003035293329600447047816970134374809585572138266782239998226176378 74.90625, 0.000000000000000000000000000000002941907171433113234229609826636520076588707901881237752654540398 74.9375, 0.000000000000000000000000000000002851394203297268825159238525126932271545090467061140195448843263 74.96875, 0.000000000000000000000000000000002763666026428910080533127646402367481506519514112867725753066344 75.0, 0.000000000000000000000000000000002678636961808077944344415201077540196973317264253141933006311449 75.03125, 0.000000000000000000000000000000002596223966481130737497887174633164654384122758465458281027993425 75.0625, 0.000000000000000000000000000000002516346552457509866267392693130266087776293609322001458307786288 75.09375, 0.000000000000000000000000000000002438926708101789723411280002039171293392835486353561728458405681 75.125, 0.000000000000000000000000000000002363888821944239958275458871504734772706948174146164437400264018 75.15625, 0.000000000000000000000000000000002291159608835490312911385522733926964492642815287321912912333718 75.1875, 0.000000000000000000000000000000002220668038373177570196988786356457830940334718193556164069452999 75.21875, 0.000000000000000000000000000000002152345265530673073046602441679938727291945375108981206286185654 75.25, 0.000000000000000000000000000000002086124563420139918115009966290399219999720732213324989387782735 75.28125, 0.000000000000000000000000000000002021941258124253403350260480249439072894199900064751386433276753 75.3125, 0.000000000000000000000000000000001959732665532938652116687640463835213749288864535956257773362108 75.34375, 0.000000000000000000000000000000001899438030123437520545693434550159655566448635307400558397092464 75.375, 0.000000000000000000000000000000001840998465623914831721973909976305667711039302088923841481043762 75.40625, 0.000000000000000000000000000000001784356897502653523881856087216904661392674583679224409189489661 75.4375, 0.000000000000000000000000000000001729458007226671246563749786035281339013007438272257260466530357 75.46875, 0.000000000000000000000000000000001676248178235319029909043901181774683261608515229222554949736518 75.5, 0.000000000000000000000000000000001624675443576097575791554081358047920088385166349754976861710292 75.53125, 0.000000000000000000000000000000001574689435151550110955433805205800041033583149753273192411698446 75.5625, 0.000000000000000000000000000000001526241334527664187337576756680993880189413411179977677751522475 75.59375, 0.000000000000000000000000000000001479283825255739849933394442920574753069716879481038415688749793 75.625, 0.000000000000000000000000000000001433771046661159707347822780921161991018171745252495884448511863 75.65625, 0.000000000000000000000000000000001389658549053929078145436481685693917760892773536068976094701591 75.6875, 0.000000000000000000000000000000001346903250317242946449009580580196968422665172061091956490323924 75.71875, 0.000000000000000000000000000000001305463393831682299061063450670160860342682262592007167030812948 75.75, 0.000000000000000000000000000000001265298507693946848100578970957682703715897370583008950313213887 75.78125, 0.000000000000000000000000000000001226369365190295441719886750491151131345971009238165477561628936 75.8125, 0.000000000000000000000000000000001188637946486090865661844162530421278354819368037949997307681642 75.84375, 0.000000000000000000000000000000001152067401494033437012027670595887429984215531370995508780247445 75.875, 0.000000000000000000000000000000001116622013884818948451115215052431091397723950652862241977054312 75.90625, 0.000000000000000000000000000000001082267166205072260887104408000177651493657023532397295257932231 75.9375, 0.00000000000000000000000000000000104896930606848925422361005111972788839788131040486819728651308 75.96875, 0.000000000000000000000000000000001016695913387167986351440477773133013369507016012863082572544378 76.0, 0.0000000000000000000000000000000009854154686111258028938097973614110920737696844845871780766343327 76.03125, 0.0000000000000000000000000000000009550974219449837769541048135285826873003013397801118949429200704 76.0625, 0.000000000000000000000000000000000925712163511754200746641111781818776006912750456156414168212321 76.09375, 0.0000000000000000000000000000000008972309944345918315863854630364856584070978661566467003598551339 76.125, 0.0000000000000000000000000000000008696260988082661166480673481821085327809241673451077758603752375 76.15625, 0.0000000000000000000000000000000008428705165329805597594041633527631636185345570416247134803502456 76.1875, 0.0000000000000000000000000000000008169381169840075979094817238768446795983985589178995769791942324 76.21875, 0.0000000000000000000000000000000007918035734914236476638489287477600363899533813675455119310840044 76.25, 0.0000000000000000000000000000000007674423386050201595081328908491214618524629416605658696665263538 76.28125, 0.0000000000000000000000000000000007438306201202333539894375436242359240481094240601057245931248374 76.3125, 0.0000000000000000000000000000000007209453578416785563135316059016859924894255587708615355458310877 76.34375, 0.0000000000000000000000000000000006987642010615954216679662575676896791223953916122519952352672659 76.375, 0.0000000000000000000000000000000006772654867312085555258498483852256580824818742216045690945337734 76.40625, 0.000000000000000000000000000000000656428218303684763445178338115324094013478683987123780689954918 76.4375, 0.000000000000000000000000000000000636232045228024074337554086564935738417144741953065375997869159 76.46875, 0.0000000000000000000000000000000006166572430738574104265317647032630359322425098877954041059595096 76.5, 0.0000000000000000000000000000000005976846942677399470295756019346260502593260858519482653514425902 76.53125, 0.0000000000000000000000000000000005792958694221264176560986911964920737569777501340485556823554121 76.5625, 0.0000000000000000000000000000000005614728092387934579799401962423040095912932707778887252944758449 76.59375, 0.0000000000000000000000000000000005441981069690351113354721740940661454567245896680905561074652551 76.625, 0.0000000000000000000000000000000005274548914135013864268738972477316885884820895551324896869789757 76.65625, 0.0000000000000000000000000000000005112268104450767959966611432798850748547761730304700969183010608 76.6875, 0.0000000000000000000000000000000004954980150387066279997995982392346096961570409223724833690471831 76.71875, 0.0000000000000000000000000000000004802531437925738072646540258861341315600343570953973773129046152 76.75, 0.0000000000000000000000000000000004654773079255090792333548465828346757137787785315788843842880445 76.78125, 0.0000000000000000000000000000000004511560767359823568266129815268109403868603097904485749645388721 76.8125, 0.0000000000000000000000000000000004372754635084738710169966890151438885403498485821048106259938731 76.84375, 0.0000000000000000000000000000000004238219118534606955899529322461407603214307016449164242269786161 76.875, 0.0000000000000000000000000000000004107822824676777035474866198812921680213927407125668582835085022 76.90625, 0.0000000000000000000000000000000003981438403017224702605603072433490023985635637680193885763003739 76.9375, 0.0000000000000000000000000000000003858942421224714676731158932491197727803992699072945448370816264 76.96875, 0.0000000000000000000000000000000003740215244581604831398411871530167279392163858176971845543731613 77.0, 0.0000000000000000000000000000000003625140919143559224240833197274627941555492785178428159004043095 77.03125, 0.0000000000000000000000000000000003513607058494058839880923320728306059179676561394912738391400651 77.0625, 0.000000000000000000000000000000000340550473398310974741746363486260680306345645627372349667216382 77.09375, 0.0000000000000000000000000000000003300728368342951187604925181586043830211612103599085724239033917 77.125, 0.0000000000000000000000000000000003199175632576864224714792997964895887940485147286639419481340493 77.15625, 0.0000000000000000000000000000000003100747346020378245469891860839771668081834824930503363665504631 77.1875, 0.0000000000000000000000000000000003005347379477270884343067550616870396564944091346662150939837912 77.21875, 0.0000000000000000000000000000000002912882561335759926837592928788812454737006447709326158241882982 77.25, 0.000000000000000000000000000000000282326258657319632294725476104743391933093042983501515214613673 77.28125, 0.0000000000000000000000000000000002736399928560388474424403239779995651593175884884106507493354364 77.3125, 0.0000000000000000000000000000000002652209753579422196902020477275455459393221951272316120073736535 77.34375, 0.0000000000000000000000000000000002570609837971490871692119889646559071340360503688015488482203891 77.375, 0.0000000000000000000000000000000002491520487833818880429878972267068098806406088574294916288345732 77.40625, 0.000000000000000000000000000000000241486446118725096723226447273132576546575922955946548049573913 77.4375, 0.0000000000000000000000000000000002340566892538493129092025704928109640331487023270981935350494411 77.46875, 0.0000000000000000000000000000000002268555219763329352426974123270701091387625096240601475279544668 77.5, 0.0000000000000000000000000000000002198759113239405276139820573482408185842900413173889140482663766 77.53125, 0.0000000000000000000000000000000002131110407159366883030848587538001239222629359526189562077652353 77.5625, 0.0000000000000000000000000000000002065543032957271747647767864837790154599622846432253849887190548 77.59375, 0.0000000000000000000000000000000002001992954783254279341268298715562553437061526921499890488715245 77.625, 0.0000000000000000000000000000000001940398106963426810122889306474004286857023264919508944270182103 77.65625, 0.0000000000000000000000000000000001880698333383937241579014657490067583037051739609634110201080216 77.6875, 0.0000000000000000000000000000000001822835328739983177157708394780142494961326840243849666370010724 77.71875, 0.0000000000000000000000000000000001766752581592403860931104210058972423250658000005804988973407418 77.75, 0.0000000000000000000000000000000001712395319176236600295750625062796521600311290575767709715293251 77.78125, 0.0000000000000000000000000000000001659710453907335392127820047858968186779995272201514607535283834 77.8125, 0.0000000000000000000000000000000001608646531534807870734903396964171793302446049813620323510497838 77.84375, 0.0000000000000000000000000000000001559153680888634071203137862892483868143500331571711339555010727 77.875, 0.0000000000000000000000000000000001511183565173388423257799812237218817784245726018433478394554779 77.90625, 0.000000000000000000000000000000000146468933476049638006878811617839768611168451708002862576455022 77.9375, 0.0000000000000000000000000000000001419625581432920618258571141632311430146828405099309040396916509 77.96875, 0.0000000000000000000000000000000001375948294037590249055787097454646940017154926388358423418506441 78.0, 0.0000000000000000000000000000000001333614815502261341453014079127396714793428961709544337630849251 78.03125, 0.0000000000000000000000000000000001292583801174829619119143312353448290112509931576557026107554862 78.0625, 0.0000000000000000000000000000000001252815178444407755120198644010382005553248163125914755102342226 78.09375, 0.000000000000000000000000000000000121427010760473151361130668243587351154631660175511337636351581 78.125, 0.0000000000000000000000000000000001176910943921672298161782380229860382112037094714237858455710896 78.15625, 0.0000000000000000000000000000000001140701200867809647234917109617650493849924776549978776761033209 78.1875, 0.0000000000000000000000000000000001105605514488157017076310231974842529016914234340182047817377891 78.21875, 0.0000000000000000000000000000000001071589608862238924045732931548452787233524923871039891788423581 78.25, 0.0000000000000000000000000000000001038620262628788261184983747685329162987273633395380327473502922 78.28125, 0.0000000000000000000000000000000001006665276540370403280310651451657316856115417223010814960009797 78.3125, 0.0000000000000000000000000000000000975693442016246584411255375937065746217556577405336088439048293 78.34375, 0.00000000000000000000000000000000009456745106627639543528253783042923607637313323971796213559010817 78.375, 0.00000000000000000000000000000000009165791647315046473648315615191014560478338870415398685462280332 78.40625, 0.00000000000000000000000000000000008883789884863420517165806811795107156889997603337185456997307825 78.4375, 0.00000000000000000000000000000000008610464404514401452191353179402916882565862855511208202492558106 78.46875, 0.00000000000000000000000000000000008345548265130921280135960459507173332189186810900221677473743274 78.5, 0.00000000000000000000000000000000008088782738491284791618466164072656027157792665007673110893030128 78.53125, 0.00000000000000000000000000000000007839917056604328026230847437583357505480418737102289811137129147 78.5625, 0.00000000000000000000000000000000007598708166798872003354080436762025008880379611721690203240682792 78.59375, 0.00000000000000000000000000000000007364920494348281804308471878151581987761281765344569550808011439 78.625, 0.00000000000000000000000000000000007138325712398300186297910100846503636325308945801387757729321136 78.65625, 0.00000000000000000000000000000000006918702518973457593073444163760748511336062608865359064356027043 78.6875, 0.00000000000000000000000000000000006705836420844273662077042218603385539757745174137576067622947059 78.71875, 0.00000000000000000000000000000000006499519524044165864786631085290334339755318711316806511192840244 78.75, 0.00000000000000000000000000000000006299550330831475300094231019060254961254838579304865370124908456 78.78125, 0.00000000000000000000000000000000006105733542898314232497323651940749540358278588238338690487333736 78.8125, 0.00000000000000000000000000000000005917879870634040875212709218288065642354211535116510549822560912 78.84375, 0.00000000000000000000000000000000005735805848257080121435184004374854442423651670224749236368902363 78.875, 0.00000000000000000000000000000000005559333654634540199942770066716745860400515536224431685927377872 78.90625, 0.00000000000000000000000000000000005388290939614630171497721069900960331962774631735424871201303186 78.9375, 0.00000000000000000000000000000000005222510655702267215195927401180198376478874543145813472238070151 78.96875, 0.00000000000000000000000000000000005061830894913481037348985843842163095641839383652058951143251943 79.0, 0.0000000000000000000000000000000000490609473064928056613538735192882439931658332555275883341790697 79.03125, 0.00000000000000000000000000000000004755150064434550312819730493540735220600872877240643645268381441 79.0625, 0.00000000000000000000000000000000004608849477372295172523032183584208871169107078629711274504016899 79.09375, 0.00000000000000000000000000000000004467050086168157644742130273645716609649721292342513886427517439 79.125, 0.00000000000000000000000000000000004329613403584594973700526130693884272608646845685024251237067356 79.15625, 0.00000000000000000000000000000000004196405203188429900427676818441262870786485912403777874542850275 79.1875, 0.00000000000000000000000000000000004067295388259681807349235928007735685069528899916112823382150312 79.21875, 0.00000000000000000000000000000000003942157864733649117273671836237649621944350388307025610139110824 79.25, 0.00000000000000000000000000000000003820870418052152851133258941744543848146738383347546966381428034 79.28125, 0.00000000000000000000000000000000003703314593803669099713863016764149924701782840612232535672386472 79.3125, 0.00000000000000000000000000000000003589375582035778552561648130590885147804871099840632726614452435 79.34375, 0.0000000000000000000000000000000000347894210512694776624002067486066852871828057540447454101506606 79.375, 0.00000000000000000000000000000000003371906309108133046891315363096657635455830527030212879787800187 79.40625, 0.00000000000000000000000000000000003268163658328067063630703541255217031557856421586578131597149185 79.4375, 0.00000000000000000000000000000000003167612833359353890207311320579248325702001991627031976663680331 79.46875, 0.00000000000000000000000000000000003070155632045663281912962417891607564196717677432734589080182554 79.5, 0.00000000000000000000000000000000002975696873593382724081147113106810077070472413689152621740082084 79.53125, 0.00000000000000000000000000000000002884144305614059133771028831058965949675682648755585348698605003 79.5625, 0.00000000000000000000000000000000002795408514026843961267314328430740044457899176698751073823799447 79.59375, 0.00000000000000000000000000000000002709402835732948637396994060725035238487914714526276294743000221 79.625, 0.00000000000000000000000000000000002626043273976824574324056728649243219844691620580812902467337498 79.65625, 0.00000000000000000000000000000000002545248416311405895458672026785271117066136052679707427554723084 79.6875, 0.00000000000000000000000000000000002466939355087296307085285233926089671456972394702768817862210175 79.71875, 0.00000000000000000000000000000000002391039610388246514106552901494629085281415908878454179569751548 79.75, 0.0000000000000000000000000000000000231747505533765773232957090325771169139038632705273449949884222 79.78125, 0.00000000000000000000000000000000002246173843703162493331608977146814151815738903911576518601995839 79.8125, 0.00000000000000000000000000000000002177066339728578336689320064497301132255423827311414015836977628 79.84375, 0.00000000000000000000000000000000002110085050124705330212163953631585274376762138081786104134853175 79.875, 0.0000000000000000000000000000000000204516455815254677632164780037473592625490591901128232427559992 79.90625, 0.00000000000000000000000000000000001982241459734576011031974137213429594766716153220497001712850647 79.9375, 0.00000000000000000000000000000000001921254301531652876929862289550367652669547179418013701070614183 79.96875, 0.00000000000000000000000000000000001862143520925113187531959935840803730262309357479953336260996846 80.0, 0.00000000000000000000000000000000001804851387845415172312128357350027421171103097839728692948688402 80.03125, 0.0000000000000000000000000000000000174932194839053031674826134001108740887445237306929502548051112 80.0625, 0.00000000000000000000000000000000001695500970179013951239794321667839948701727592774861450367249285 80.09375, 0.00000000000000000000000000000000001643335889384385103803764925941387395388291652894914273240411226 80.125, 0.00000000000000000000000000000000001592775759399087168657999633946003895602237106572657130386038512 80.15625, 0.000000000000000000000000000000000015437712010778924598271794790998951015747872938007872153510124 80.1875, 0.00000000000000000000000000000000001496274354512156270103513688722758403033151539143021597419977806 80.21875, 0.00000000000000000000000000000000001450238832287821147578771404775386574495874253319979876370608092 80.25, 0.00000000000000000000000000000000001405619674181521194707980056499102923542766139383760573047929245 80.28125, 0.00000000000000000000000000000000001362373303250540703711994304324614263443969469048604223066902201 80.3125, 0.000000000000000000000000000000000013204574832737427387786664915530940925344366786552477504472633 80.34375, 0.00000000000000000000000000000000001279831277501902689479960121004789351249161951988554174530910557 80.375, 0.00000000000000000000000000000000001240455008677160639680096010897851027853026876028844002162768386 80.40625, 0.0000000000000000000000000000000000120229022028254587091685858933839511565360891686531041617226782 80.4375, 0.00000000000000000000000000000000001165299638983728159317543657659176003496228803432758509600836464 80.46875, 0.00000000000000000000000000000000001129447138226314903841847172961884787556284763260501600842040551 80.5, 0.00000000000000000000000000000000001094697702953141678207421004086274843223183633312346058590816328 80.53125, 0.00000000000000000000000000000000001061017395407097631443614427058803598728990270713476474391198155 80.5625, 0.000000000000000000000000000000000010283733219860873409179006491536066052300345833639704232576622 80.59375, 0.000000000000000000000000000000000009967336011177582823028701123308128963107540343187118419792211614 80.625, 0.000000000000000000000000000000000009660673321226190268582938385455502605814421332266479445467526803 80.65625, 0.00000000000000000000000000000000000936344565035138580275198322185876607456067597700816321011513405 80.6875, 0.000000000000000000000000000000000009075362713533528819234293331849836932138929363835294637313705313 80.71875, 0.000000000000000000000000000000000008796143156884113024115349127211579404978653190902991297889554351 80.75, 0.000000000000000000000000000000000008525514282863748965455331342043125170018327058505208345762253234 80.78125, 0.000000000000000000000000000000000008263211783954300464506098668804370969971929857226782146139322729 80.8125, 0.000000000000000000000000000000000008008979484525067977765135905964623418172858048701412237135373085 80.84375, 0.000000000000000000000000000000000007762569090640914569255579123265819722893057981257497602346342615 80.875, 0.000000000000000000000000000000000007523739947567986606941160286659915194792150862230787969215731435 80.90625, 0.000000000000000000000000000000000007292258804740199091302481414782693593285972744461188633407712749 80.9375, 0.000000000000000000000000000000000007067899587956942020009725645610064202493585217358963030658858963 80.96875, 0.000000000000000000000000000000000006850443178589526506624497930527588156720871308404431994813842523 81.0, 0.000000000000000000000000000000000006639677199580734400702255270428292423919182138349527423631142371 81.03125, 0.000000000000000000000000000000000006435395808028469586689808533410979523832931793284594532707539447 81.0625, 0.00000000000000000000000000000000000623739949415093944908249859375491334600451725843700310809758949 81.09375, 0.000000000000000000000000000000000006045494886437027461533598424924938155755710517614360547596982655 81.125, 0.000000000000000000000000000000000005859494562791558574886305202494347307559016003564257848402505952 81.15625, 0.000000000000000000000000000000000005679216867491013943044417744117047205011816845713193858739545274 81.1875, 0.000000000000000000000000000000000005504485733770926254317334663994112303534263567105571491222684238 81.21875, 0.000000000000000000000000000000000005335130511871687071546624224679398383041979641789200863735664449 81.25, 0.000000000000000000000000000000000005170985802374828498608475581371711567219430431352372964777311443 81.28125, 0.000000000000000000000000000000000005011891294667008390183503183385658265894536756288663883950998252 81.3125, 0.000000000000000000000000000000000004857691610373936252203378039081881936610479942072404405293106987 81.34375, 0.000000000000000000000000000000000004708236151611330833077755365666128510547793115667087661237060662 81.375, 0.000000000000000000000000000000000004563378953904704921151241425551220606054337322040846075363647338 81.40625, 0.000000000000000000000000000000000004422978543634332636465854400317249937035164989095280217223648406 81.4375, 0.000000000000000000000000000000000004286897799866173988074929735145895500112727824925928457643022166 81.46875, 0.000000000000000000000000000000000004155003820433814978122473587427942656277778321870288856831971766 81.5, 0.000000000000000000000000000000000004027167792140633254123215878383787877219702117886558776118469376 81.53125, 0.000000000000000000000000000000000003903264864955423296097295357739520353061527163795629419030273398 81.5625, 0.000000000000000000000000000000000003783174030078615305422496189824697482429413537294190932309655554 81.59375, 0.000000000000000000000000000000000003666778001760002146545952758391010304645062857182066373787985745 81.625, 0.000000000000000000000000000000000003553963102752552572932888459567147712843146587135851243856056183 81.65625, 0.000000000000000000000000000000000003444619153290440123121486352288219190334449914382176500228079481 81.6875, 0.000000000000000000000000000000000003338639363482858969706942389787095238090132326659678703147703526 81.71875, 0.000000000000000000000000000000000003235920229018534004995165956986659491840455048058354798720287606 81.75, 0.000000000000000000000000000000000003136361430079065810025993662276018624317682382595963169386485512 81.78125, 0.00000000000000000000000000000000000303986573336238503650515878415997566808586589558052388294276889 81.8125, 0.000000000000000000000000000000000002946338897120628194585137520936287438004045713643793751011697692 81.84375, 0.000000000000000000000000000000000002855689579119690849911630416311491880670442237242320062672938362 81.875, 0.000000000000000000000000000000000002767829247430567666149040674628835882791680385744742844228222157 81.90625, 0.000000000000000000000000000000000002682672093965354371097123815390126448037768049676624921573492815 81.9375, 0.000000000000000000000000000000000002600134950673467276553972682786044237963089983367334253987300464 81.96875, 0.000000000000000000000000000000000002520137208316234062207310775165368372585296588429014579705766186 82.0, 0.000000000000000000000000000000000002442600737740527679440802607414812130872009992469495172662859705 82.03125, 0.000000000000000000000000000000000002367449813574555901351450237766647517620959444313471960436997744 82.0625, 0.000000000000000000000000000000000002294611040271284624149300191794964834815166680771941657100511725 82.09375, 0.000000000000000000000000000000000002224013280427265822775260506888736359139413789470655274791709715 82.125, 0.000000000000000000000000000000000002155587585306863319269263931009182529789130455344364808778763564 82.15625, 0.000000000000000000000000000000000002089267127504023406497503755407967494817728501544523776671599704 82.1875, 0.00000000000000000000000000000000000202498713567582498587761268913531434762509680765527776212459594 82.21875, 0.000000000000000000000000000000000001962684831284067264577896247677033439654771233905778111875013708 82.25, 0.000000000000000000000000000000000001902299367283114191435588144997202262300281215889938514642494914 82.28125, 0.000000000000000000000000000000000001843771768694115606865124278504671899069592823311122926541053626 82.3125, 0.00000000000000000000000000000000000178704487500756739670781986240449210297592877316885601312725444 82.34375, 0.00000000000000000000000000000000000173206328435795857259092994519578710806508810385413659374970539 82.375, 0.000000000000000000000000000000000001678773299415983895840643035894910195213752653382311012618300234 82.40625, 0.000000000000000000000000000000000001627122874945478108598973352506220670583828993338609005804404112 82.4375, 0.000000000000000000000000000000000001577061566973853672793545715058318032003462238604225385728817001 82.46875, 0.000000000000000000000000000000000001528540483526399732862566819794156318603233166643396546794366121 82.5, 0.000000000000000000000000000000000001481512236876327350652169472607215108660015195584495748098276425 82.53125, 0.000000000000000000000000000000000001435930897263926402336846837554519423249933038525801921631352411 82.5625, 0.000000000000000000000000000000000001391751948039634323329443849134441173887307465955559262913243336 82.59375, 0.000000000000000000000000000000000001348932242187207539227255494491337474767498645102384392933103832 82.625, 0.000000000000000000000000000000000001307429960184534287054391344764973028104498974199296971457326181 82.65625, 0.000000000000000000000000000000000001267204569160933927790424001126294535423127642804203736411441061 82.6875, 0.00000000000000000000000000000000000122821678331105405430332898660707971005561335818171679872282694 82.71875, 0.000000000000000000000000000000000001190428525526703944957969296519134441452290112806328843424117323 82.75, 0.000000000000000000000000000000000001153802890209152400929986783359922249400403001376738348673655086 82.78125, 0.000000000000000000000000000000000001118304107225570896322609134203727325137581427055807810732444317 82.8125, 0.00000000000000000000000000000000000108389750697442039052237518095451990932781949481531366245330285 82.84375, 0.000000000000000000000000000000000001050549486525663193158729457630735171178226317364460035075621505 82.875, 0.000000000000000000000000000000000001018227476802730991295071621693874273241122486449056521917157834 82.90625, 0.0000000000000000000000000000000000009868999107741975712750619123870817857843633315533269226772136186 82.9375, 0.0000000000000000000000000000000000009565361926240908876355897886362095514875324763722033667959079002 82.96875, 0.0000000000000000000000000000000000009271066678707349117629819953058547425937347618282180622382596648 83.0, 0.00000000000000000000000000000000000089858259440493806696688481727358348233256315837638934421683388 83.03125, 0.0000000000000000000000000000000000008709361144192429291125344669288182496878875016525189890941030203 83.0625, 0.0000000000000000000000000000000000008441402272008221197867685784709602565828957510779995678112971551 83.09375, 0.0000000000000000000000000000000000008181687627614487920966950018933739707172895820623117912891516299 83.125, 0.0000000000000000000000000000000000007929963562787876313836791338215901593646025383508720678033798844 83.15625, 0.0000000000000000000000000000000000007685984233240446628057183957287439254115282455932263443113739751 83.1875, 0.0000000000000000000000000000000000007449511358517821487598223632283813298708698716667375220138218637 83.21875, 0.0000000000000000000000000000000000007220313989284492215349749450757787020343231462053559875295354252 83.25, 0.0000000000000000000000000000000000006998168281769003573812042065557549950047401707743379425099495086 83.28125, 0.0000000000000000000000000000000000006782857279148730619601219156650921465400276207595494036280945285 83.3125, 0.0000000000000000000000000000000000006574170699660738868373779597778009970920653137945685280169547 83.34375, 0.0000000000000000000000000000000000006371904731231787942066054996747167692394056345578887711129128624 83.375, 0.0000000000000000000000000000000000006175861832426905739513116132417063066456241543919827547386957978 83.40625, 0.0000000000000000000000000000000000005985850539522131152709634744902687976027907837771274766069482824 83.4375, 0.0000000000000000000000000000000000005801685279513004471041585761827099104922638492837582129621139597 83.46875, 0.0000000000000000000000000000000000005623186188876181716165408380608243269949949840816209199476355175 83.5, 0.0000000000000000000000000000000000005450178937907167892531861948165256433218552835553827771747107886 83.53125, 0.0000000000000000000000000000000000005282494560462610010344913819219260424873358442130887121712210282 83.5625, 0.0000000000000000000000000000000000005119969288940869057678567376694514724822550747788617881838061564 83.59375, 0.0000000000000000000000000000000000004962444394339706021575385148711496874776242105969037318336864701 83.625, 0.0000000000000000000000000000000000004809766031234875580644827900952041891649090916176661492854431476 83.65625, 0.0000000000000000000000000000000000004661785087528227056668815243879103547866942727752483049530043706 83.6875, 0.0000000000000000000000000000000000004518357038818570313713257734313126702056066149102730625894529427 83.71875, 0.0000000000000000000000000000000000004379341807253079079534668687314845191063152494127348375679958074 83.75, 0.0000000000000000000000000000000000004244603624721380044990137282585403333724630548757689978951550046 83.78125, 0.0000000000000000000000000000000000004114010900258717346375918270899782350328995911573398178432689692 83.8125, 0.0000000000000000000000000000000000003987436091528692795316681287647201903497259838968999059955905181 83.84375, 0.0000000000000000000000000000000000003864755580260066505744535319997817167486794344152100302392614465 83.875, 0.0000000000000000000000000000000000003745849551515964268864193403975176133422638668885253790471663299 83.90625, 0.0000000000000000000000000000000000003630601876677580916297711277375696387003374042973578392037332728 83.9375, 0.0000000000000000000000000000000000003518900000028096644301918451673805993595773748396909307003929786 83.96875, 0.0000000000000000000000000000000000003410634828826039391049991203667676760219416355003908012480695553 84.0, 0.0000000000000000000000000000000000003305700626760734298455096425735447481813412386484862757646693545 84.03125, 0.0000000000000000000000000000000000003203994910685784378225415390456190339035072801366395948578455603 84.0625, 0.0000000000000000000000000000000000003105418350529727964710733118162308733124211685351053825902845151 84.09375, 0.0000000000000000000000000000000000003009874672286121501314174717117680389890258782622429966848439248 84.125, 0.0000000000000000000000000000000000002917270563988303703283683906796058346169133752404973865472895942 84.15625, 0.000000000000000000000000000000000000282751558457701210481334557921271414138494253129143366206260223 84.1875, 0.0000000000000000000000000000000000002740522075571848279451942468220230195148322347900717455792361297 84.21875, 0.0000000000000000000000000000000000002656205075460326379123288530789063154609047664168437170434340411 84.25, 0.0000000000000000000000000000000000002574482236720893743003611792080857737148042076135933324129744244 84.28125, 0.0000000000000000000000000000000000002495273745398884775186836167241193996824093287112367213869104203 84.3125, 0.0000000000000000000000000000000000002418502243156862591859424522403665655763769577895869802958916127 84.34375, 0.000000000000000000000000000000000000234409275172321952966568033969902723725363845432777317171006878 84.375, 0.0000000000000000000000000000000000002271972599665249847216231080235160343552406369318895979649418468 84.40625, 0.0000000000000000000000000000000000002202071351415178128806998741875157834755235140930955177111767729 84.4375, 0.0000000000000000000000000000000000002134320738479827230523563984959092432398035563916029493848473613 84.46875, 0.0000000000000000000000000000000000002068654592766742242942169113151436925506675256166492294425519031 84.5, 0.0000000000000000000000000000000000002005008781961653964423600303369609643552038505011266307585266864 84.53125, 0.0000000000000000000000000000000000001943321146894168803268126883403025228658171900596194949586369629 84.5625, 0.0000000000000000000000000000000000001883531440830513812386793761285671298742771127387890152863073101 84.59375, 0.0000000000000000000000000000000000001825581270634047603075778685598036414151926607474641619422771437 84.625, 0.0000000000000000000000000000000000001769414039736072023038365273950932367774352574841984720466398963 84.65625, 0.0000000000000000000000000000000000001714974892861247499513630304668109120782892794219187836958702125 84.6875, 0.0000000000000000000000000000000000001662210662453628567960559700765547790706537773511974958248152293 84.71875, 0.0000000000000000000000000000000000001611069816750997003803813112482615564202535155786998765853294485 84.75, 0.0000000000000000000000000000000000001561502409456779771374535280702049598211672328025391645659440396 84.78125, 0.0000000000000000000000000000000000001513460030960399272571421473513363963045540192049420264659916902 84.8125, 0.0000000000000000000000000000000000001466895761058415641747288635504929698305998690380752906125033601 84.84375, 0.0000000000000000000000000000000000001421764123130286569835164341593775243528690578891724100810791887 84.875, 0.0000000000000000000000000000000000001378021039723990781265181604553824699299024807324710078473018986 84.90625, 0.0000000000000000000000000000000000001335623789508138219246539772810061672330104571072267219336136677 84.9375, 0.0000000000000000000000000000000000001294530965548524563267071143978674453372775253090839977353703052 84.96875, 0.0000000000000000000000000000000000001254702434868381210591939177405690905634323045380245259071216555 85.0, 0.0000000000000000000000000000000000001216099299252825564416826336765759547098498959119546085616419732 85.03125, 0.000000000000000000000000000000000000117868385725923160957631209013205850854194977047960503950130018 85.0625, 0.0000000000000000000000000000000000001142419567396418509080010812014399898678099241922676834291358551 85.09375, 0.0000000000000000000000000000000000001107271012436696471489838319136214627382834972037331182489898688 85.125, 0.0000000000000000000000000000000000001073203864825915535114669268644046028923692335919952491541124273 85.15625, 0.0000000000000000000000000000000000001040184853157735270736374480632478959297237518465649103711267618 85.1875, 0.0000000000000000000000000000000000001008181729679372767401413491517375238616090443862633980536341941 85.21875, 0.00000000000000000000000000000000000009771632387970936508001612135029555994261705677511685378830914138 85.25, 0.00000000000000000000000000000000000009470990865506872747664331873723681029275679199112588305121737262 85.28125, 0.00000000000000000000000000000000000009179599110271135770466866648777204394445068154142846506880354081 85.3125, 0.00000000000000000000000000000000000008897172536844263249578507076987329258143177924196649836191642254 85.34375, 0.00000000000000000000000000000000000008623435315579664906843618591259457796775147664838824490322417415 85.375, 0.00000000000000000000000000000000000008358120103216811580072704410315679747604800869815732231932805353 85.40625, 0.00000000000000000000000000000000000008100967781782585137465553004391552464534369774714285956795627169 85.4375, 0.00000000000000000000000000000000000007851727205525789337760411811077452457839045166312587864473946238 85.46875, 0.00000000000000000000000000000000000007610154955637667256878244526586395421644041232688995050479324559 85.5, 0.00000000000000000000000000000000000007376015102518875042665072445113184967451093500891519517995262958 85.53125, 0.00000000000000000000000000000000000007149078975360731945351296680143666718827207756133343207519304299 85.5625, 0.00000000000000000000000000000000000006929124938815710000577777651632468077619794377412586196251947855 85.59375, 0.00000000000000000000000000000000000006715938176539050390857146472174568779696626206121721807866692929 85.625, 0.00000000000000000000000000000000000006509310481390104142071594735079902173755011815278287445441453747 85.65625, 0.00000000000000000000000000000000000006309040052088498977938890045630043561114441599213827127692848738 85.6875, 0.00000000000000000000000000000000000006114931296126538209548448098347858952712300568704885379173842969 85.71875, 0.0000000000000000000000000000000000000592679463874534763588287618149633901723313010017928324506507785 85.75, 0.00000000000000000000000000000000000005744446337788208542076463158856544576226476568283209728392675571 85.78125, 0.00000000000000000000000000000000000005567708304250254788802652982540670413935298732914736574662249121 85.8125, 0.00000000000000000000000000000000000005396407928349275294461362947584390845183874005835019815672640016 85.84375, 0.00000000000000000000000000000000000005230377910947755355114481659431719756808016671201900113337293308 85.875, 0.00000000000000000000000000000000000005069456100161516491587467453076973574980306432652503691571029137 85.90625, 0.00000000000000000000000000000000000004913485332995379962986388721472900307037250799627577693322156994 85.9375, 0.00000000000000000000000000000000000004762313281851188688203933166003368521109241924984514866335016629 85.96875, 0.00000000000000000000000000000000000004615792305758280866728575923902604128978966431034485718276730679 86.0, 0.00000000000000000000000000000000000004473779306181120734627655839169743997875489624858382665442086726 86.03125, 0.00000000000000000000000000000000000004336135587263262135361535588851228651298111554079905697223815705 86.0625, 0.00000000000000000000000000000000000004202726720371153293930037259639904363570004871488791815897948591 86.09375, 0.00000000000000000000000000000000000004073422412805490588259274745857377565380285431991534869750380494 86.125, 0.00000000000000000000000000000000000003948096380551899315022440334716464217964701225638616986173257031 86.15625, 0.00000000000000000000000000000000000003826626224946664423383422216376891678610692491789370081136933656 86.1875, 0.00000000000000000000000000000000000003708893313137057792286774556033245705310542508619361163083438958 86.21875, 0.00000000000000000000000000000000000003594782662219514589181874295125212035217553466627635171085961389 86.25, 0.00000000000000000000000000000000000003484182826942503189861385758046580971173123116998364484987468094 86.28125, 0.00000000000000000000000000000000000003376985790864414568455543138288122689394507646137578873244598248 86.3125, 0.00000000000000000000000000000000000003273086860860171383664300909057888925437656593671960263602365674 86.34375, 0.00000000000000000000000000000000000003172384564873527487528753404678744702641549982269266184543452911 86.375, 0.00000000000000000000000000000000000003074780552815198460549182176662482773561544379261628753845428572 86.40625, 0.00000000000000000000000000000000000002980179500510036127585082584722325037003126206677221921587499438 86.4375, 0.00000000000000000000000000000000000002888489016599437833309136026125698344901788285763785546364733472 86.46875, 0.00000000000000000000000000000000000002799619552308067462300573881741090821318873720480516406883095324 86.5, 0.00000000000000000000000000000000000002713484313986762595943959207576130471491776883920457360553445428 86.53125, 0.00000000000000000000000000000000000002629999178346213538212269732875136066271274775483046843191384852 86.5625, 0.00000000000000000000000000000000000002549082610298627863166669440750144113495282173552664234668453447 86.59375, 0.00000000000000000000000000000000000002470655583327141205137221924247030832397072515497722642606144677 86.625, 0.00000000000000000000000000000000000002394641502305203715632678732697154027747605521166045089522119541 86.65625, 0.000000000000000000000000000000000000023209661286905643601024266822502183881989298046364937858839873 86.6875, 0.00000000000000000000000000000000000002249557508020794359598882908925478222042910618780255269174807948 86.71875, 0.00000000000000000000000000000000000002180345899639538862125670046007073815544127226897570375690440365 86.75, 0.00000000000000000000000000000000000002113263708584864551281829065592685522537911898737342035420402016 86.78125, 0.00000000000000000000000000000000000002048245419573182493459633524874121997705903270401894646475234278 86.8125, 0.00000000000000000000000000000000000001985227533014272151594247247225631518337247993145863224200291947 86.84375, 0.00000000000000000000000000000000000001924148502994916152119100864594332294894593656413662518158472343 86.875, 0.00000000000000000000000000000000000001864948677170578019675759200877385852800880810707307166505937416 86.90625, 0.00000000000000000000000000000000000001807570238506418568980316506733885507606947637109779724388680254 86.9375, 0.00000000000000000000000000000000000001751957148810752785007751952723326739088121781308423352031551417 86.96875, 0.00000000000000000000000000000000000001698055094005799595275059382580621238942157711766119000839328869 87.0, 0.00000000000000000000000000000000000001645811431082273651166034277667247324155757911690455385118961206 87.03125, 0.00000000000000000000000000000000000001595175136686012746026651556704291726510419525501337160210852653 87.0625, 0.00000000000000000000000000000000000001546096757286428412268262963817401588730688215154213511139420319 87.09375, 0.00000000000000000000000000000000000001498528360878112114630196457300374884748978801806043089277160785 87.125, 0.00000000000000000000000000000000000001452423490168426800863440373824187915175322164528178662399724462 87.15625, 0.00000000000000000000000000000000000001407737117205364846772051820059648569752421516384334939115045555 87.1875, 0.00000000000000000000000000000000000001364425599401360057165483843353326449748951974885865361483313002 87.21875, 0.0000000000000000000000000000000000000132244663691010473160182367960165267144952730987040205576854376 87.25, 0.00000000000000000000000000000000000001281759231314744205341201233607893356713469149360082497011898157 87.28125, 0.00000000000000000000000000000000000001242323645587102022216572582944310750652322859618585468918902975 87.3125, 0.00000000000000000000000000000000000001204101365278830237996292415998587396247241273324133008342380662 87.34375, 0.00000000000000000000000000000000000001167055060906582502605935314334800050012193937411435560807956953 87.375, 0.00000000000000000000000000000000000001131148551494473702342299307541583971216296476066580572132237601 87.40625, 0.0000000000000000000000000000000000000109634676923822019784463387061515876296673287480155530073799906 87.4375, 0.00000000000000000000000000000000000001062615725256450173940995160157080434281532877925873935129126143 87.46875, 0.00000000000000000000000000000000000001029922476395735393453339677163744521992174892367306728683396219 87.5, 0.000000000000000000000000000000000000009982350930569247555985003322739095176400716307263856936733582469 87.53125, 0.000000000000000000000000000000000000009675226280113575058341567454343394927597389530911911570357189963 87.5625, 0.000000000000000000000000000000000000009377550861765007020171533942095666362539697590331453349494763682 87.59375, 0.00000000000000000000000000000000000000908903395321492555742495648041846966166495361562634008028833544 87.625, 0.000000000000000000000000000000000000008809393776739814528422697245410073729351455860172259756464323736 87.65625, 0.000000000000000000000000000000000000008538357224005307002162889746803900465224095917392603506376265703 87.6875, 0.000000000000000000000000000000000000008275659589337122071229160984048641559335725648850026904929120668 87.71875, 0.000000000000000000000000000000000000008021044311198392210120509845774708281980467957111597778034150048 87.75, 0.00000000000000000000000000000000000000777426272162089708529630685332019077899088412479209556166399867 87.78125, 0.000000000000000000000000000000000000007535073803345487842139402077110033987849584076645760921066608378 87.8125, 0.000000000000000000000000000000000000007303243954434515013056800650091310972313207871901554530324491588 87.84375, 0.000000000000000000000000000000000000007078546760126370663314458386633308212052078351257665087344854798 87.875, 0.000000000000000000000000000000000000006860762771709328343958301720604270284195423939196722501404105986 87.90625, 0.000000000000000000000000000000000000006649679292198719762050137926308120156011558067723787778176051741 87.9375, 0.000000000000000000000000000000000000006445090168608131502658349426745619022439505813694824804800472068 87.96875, 0.000000000000000000000000000000000000006246795590611745133812747285045885180688494033471225355068216451 88.0, 0.000000000000000000000000000000000000006054601895401185884531860533810599287768347010082648170864063143 88.03125, 0.000000000000000000000000000000000000005868321378546294903120665306636252914683829121221294569300469217 88.0625, 0.000000000000000000000000000000000000005687772110675103786722624399033877411856844830898874745952661761 88.09375, 0.000000000000000000000000000000000000005512777759793973350311629926262687450442236482305398443634617301 88.125, 0.000000000000000000000000000000000000005343167419074367024454092464540753242876813497718759663299061705 88.15625, 0.000000000000000000000000000000000000005178775439938068216014307764033512183150791772316722194445201918 88.1875, 0.000000000000000000000000000000000000005019441270277825648754994966048055562318726002272974717564497397 88.21875, 0.000000000000000000000000000000000000004865009297655426175311949604213678889173337744054471359309235714 88.25, 0.000000000000000000000000000000000000004715328697324055716620040420449908407683660457000459214887254963 88.28125, 0.000000000000000000000000000000000000004570253284926520587154697422828434544751775549299629434165363699 88.3125, 0.000000000000000000000000000000000000004429641373725468105877154669445778847667306974590199254855901745 88.34375, 0.000000000000000000000000000000000000004293355636226171533506824250014969801696028168492059940953387623 88.375, 0.000000000000000000000000000000000000004161262970056734339445119001147762650663475966385008972965409784 88.40625, 0.000000000000000000000000000000000000004033234367974726776099457472202161807131275004612778790831588509 88.4375, 0.000000000000000000000000000000000000003909144791873297785356431274811664308265875020754382421346901131 88.46875, 0.000000000000000000000000000000000000003788873050663711317442255664908240895441961031214143729011534451 88.5, 0.000000000000000000000000000000000000003672301681915042021204408140221412924184880376458033919609826907 88.53125, 0.000000000000000000000000000000000000003559316837135434664398932905275912558000642747533052841169183191 88.5625, 0.000000000000000000000000000000000000003449808170582888146562877034929335038523577614684757484097748877 88.59375, 0.000000000000000000000000000000000000003343668731496972048921814405816906782093269155287387192441928612 88.625, 0.000000000000000000000000000000000000003240794859646224692093253484977010442087206018963625214575828303 88.65625, 0.000000000000000000000000000000000000003141086084089219906084451104792193767910990401900238638061579276 88.6875, 0.0000000000000000000000000000000000000030444450250504283208427609842775687684349534984772169021767107 88.71875, 0.000000000000000000000000000000000000002950777298815041024690308905506980311592992185302539414183028502 88.75, 0.000000000000000000000000000000000000002859991425549871883345556409174610943252151829176999029290398921 88.78125, 0.000000000000000000000000000000000000002771998739960312543477703040548301981389462314178243811390457788 88.8125, 0.000000000000000000000000000000000000002686713304696083952836170394905569990486270487398954140567465931 88.84375, 0.000000000000000000000000000000000000002604051826421212819057460252172013829672924510502735933834674831 88.875, 0.000000000000000000000000000000000000002523933574466263423158823464916267976583433514394806734562497901 88.90625, 0.000000000000000000000000000000000000002446280301983377142698885545992349969835209594406050317323542847 88.9375, 0.000000000000000000000000000000000000002371016169527116386651107401622893845433103562778120385895233241 88.96875, 0.000000000000000000000000000000000000002298067670986478786448662327047356976740744501553079074902274246 89.0, 0.000000000000000000000000000000000000002227363561795743739222909281640826188721731726748842908310710762 89.03125, 0.000000000000000000000000000000000000002158834789354039002587800784565906236291489985330219438624692646 89.0625, 0.000000000000000000000000000000000000002092414425585672169036914447249607143552048506369898420457844856 89.09375, 0.000000000000000000000000000000000000002028037601575362608860789061636719898267362047371220596418359569 89.125, 0.00000000000000000000000000000000000000196564144421453590538467722630688740193646280049521375873928539 89.15625, 0.00000000000000000000000000000000000000190516501479680689437207317425592718483590613664707611038074201 89.1875, 0.000000000000000000000000000000000000001846549249502681078848265227503342776066980687174099119826027658 89.21875, 0.000000000000000000000000000000000000001789736901715349280564382821568253151693847559435070109327189485 89.25, 0.000000000000000000000000000000000000001734672486111238711838267803844126912129300039846916776439498852 89.28125, 0.000000000000000000000000000000000000001681302224470716953124833339182592024327080689784802743960897366 89.3125, 0.000000000000000000000000000000000000001629573993156025295200495874693489489797535989947324201744241282 89.34375, 0.000000000000000000000000000000000000001579437272205146191026445138558657720620571696187463110757899062 89.375, 0.000000000000000000000000000000000000001530843095991887751436585677717055012535041602916747738401979142 89.40625, 0.000000000000000000000000000000000000001483744005403997852103642739873428714562286867207006750134124211 89.4375, 0.00000000000000000000000000000000000000143809400149260299067527067794172440457045257101950392781427051 89.46875, 0.000000000000000000000000000000000000001393848500547703990482531786316204166932893528922655745649963093 89.5, 0.00000000000000000000000000000000000000135096429055585339419884928507263760903094460683775278651132317 89.53125, 0.000000000000000000000000000000000000001309399488997489287483637730447878225775837575866273714008597966 89.5625, 0.000000000000000000000000000000000000001269113501942708657347014309771002726053047439967244244676931042 89.59375, 0.000000000000000000000000000000000000001230066984405531500524182638009018416607546470542675870069760781 89.625, 0.000000000000000000000000000000000000001192221801917935992040251585161024369346870206162995626541563423 89.65625, 0.000000000000000000000000000000000000001155540993286136303764068830199816722608836560572348727816346738 89.6875, 0.000000000000000000000000000000000000001119988734492729290546791512529391654942476989730677916595745397 89.71875, 0.000000000000000000000000000000000000001085530303709455365175357243155539429628241974334026965936684433 89.75, 0.000000000000000000000000000000000000001052132047386403555806342114947437304465215575076679801942994924 89.78125, 0.000000000000000000000000000000000000001019761347384542040118528161364124784815923931717781950280040615 89.8125, 0.0000000000000000000000000000000000000009883865891194744058771191984992676017244919696669678017829386768 89.84375, 0.0000000000000000000000000000000000000009579771306853094930936092351491222342472044189195116494480649092 89.875, 0.0000000000000000000000000000000000000009285032729284898930297994058580750573677991583265321238862663708 89.90625, 0.0000000000000000000000000000000000000008999362304423519487936723950541484122233296712247241985059704577 89.9375, 0.0000000000000000000000000000000000000008722481034540893273074040470951657460819163756870482891386676734 89.96875, 0.000000000000000000000000000000000000000845411850576663791214632478349998347362679104350493107111742082 90.0, 0.000000000000000000000000000000000000000819401262399051543036110821338194339042713237858963647966737444 90.03125, 0.0000000000000000000000000000000000000007941909358890322675922376762854850959663521571745070502037041135 90.0625, 0.0000000000000000000000000000000000000007697562495835215620169295436888094072545708875985448114536340985 90.09375, 0.0000000000000000000000000000000000000007460733395422165906171067474129064941754781934773534181867392566 90.125, 0.0000000000000000000000000000000000000007231190760410702855678622091804529772335855058147813845501540914 90.15625, 0.0000000000000000000000000000000000000007008710409828319620458792951078259678853632703478279669068061806 90.1875, 0.0000000000000000000000000000000000000006793075060025925335631884464468508683071757360275968272982263562 90.21875, 0.0000000000000000000000000000000000000006584074112469512839290365462007864367330387236204728720609532794 90.25, 0.0000000000000000000000000000000000000006381503448060790393554117699533971098635640950893564650830717791 90.28125, 0.0000000000000000000000000000000000000006185165227785902302517650425120771113775765171990082233068093605 90.3125, 0.0000000000000000000000000000000000000005994867699497543599785755452102729454697380894719580284093813084 90.34375, 0.0000000000000000000000000000000000000005810425010641764108394393440011518600335903641546341855789719511 90.375, 0.0000000000000000000000000000000000000005631657026746563009085665713956478244354287225184625826071113906 90.40625, 0.0000000000000000000000000000000000000005458389155495002259369915237046643782819380443024670840485273759 90.4375, 0.0000000000000000000000000000000000000005290452176211021281330352721932588319288339978005211383416237173 90.46875, 0.0000000000000000000000000000000000000005127682074591421607385211712440915308181465244320658778894131527 90.5, 0.0000000000000000000000000000000000000004969919882522613803013705515382753000899818741663286561070804628 90.53125, 0.00000000000000000000000000000000000000048170115228256849777269099187778034170914973416212565185955212 90.5625, 0.0000000000000000000000000000000000000004668807658778158400319634059796595071030179242646893551264709345 90.59375, 0.0000000000000000000000000000000000000004525163548265481852452167696454626465129466182284926287064484112 90.625, 0.0000000000000000000000000000000000000004385938902419802942031578230000794360906178969838469962384670376 90.65625, 0.0000000000000000000000000000000000000004250997748607972070663045910769847223656195724188618970720705353 90.6875, 0.0000000000000000000000000000000000000004120208297634961387729464268723559313980257720393812980194273239 90.71875, 0.0000000000000000000000000000000000000003993442815033005015854043261284972473874424675189992225519729198 90.75, 0.0000000000000000000000000000000000000003870577496310756119394260945189611439895919805589509370012750957 90.78125, 0.0000000000000000000000000000000000000003751492346040623906287890028847612735717499005035278130873276763 90.8125, 0.0000000000000000000000000000000000000003636071060666202181204054718074040950736799254286994031828819559 90.84375, 0.0000000000000000000000000000000000000003524200914915334265513782587925724881741272920713181164800637868 90.875, 0.0000000000000000000000000000000000000003415772651707880515418549457569014951225498361134166832297982555 90.90625, 0.000000000000000000000000000000000000000331068037545066774282437916687634312767058475847810076142704055 90.9375, 0.0000000000000000000000000000000000000003208821448615407907573755063065805123162819379475306049511418317 90.96875, 0.0000000000000000000000000000000000000003110096391498579735242109885529304213387195918505603358308388964 91.0, 0.0000000000000000000000000000000000000003014408785065374553263082455636316893661597126377950080176410592 91.03125, 0.0000000000000000000000000000000000000002921665176782819664717322830596450773983851386717390150504024186 91.0625, 0.0000000000000000000000000000000000000002831774989350111935374972368716773272280336395749730819753513331 91.09375, 0.0000000000000000000000000000000000000002744650432237023806687901730368243871252996183101132618455857254 91.125, 0.0000000000000000000000000000000000000002660206415943986428829066965998674941221160912810431440916763541 91.15625, 0.0000000000000000000000000000000000000002578360468900112711995394888869543481888766641585916345147737955 91.1875, 0.000000000000000000000000000000000000000249903265691799941704349166901512475605471401196739364859636211 91.21875, 0.0000000000000000000000000000000000000002422145505126644464259223444236718742446626251746034453517419428 91.25, 0.0000000000000000000000000000000000000002347623922306235870404240669405708792472981906150271266659625053 91.28125, 0.0000000000000000000000000000000000000002275395127550914492834327828372343202195089050543868040208653914 91.3125, 0.0000000000000000000000000000000000000002205388579187886356422187058052160213082735458486695350493060071 91.34375, 0.0000000000000000000000000000000000000002137535905883463984731112813100964634724197092837436973659188973 91.375, 0.0000000000000000000000000000000000000002071770839868752003548888089837961999101574853881557476197764811 91.40625, 0.0000000000000000000000000000000000000002008029152219762418460635009757540281123782145267072149690994373 91.4375, 0.0000000000000000000000000000000000000001946248590128751410394622395765151375539407583703010779844356475 91.46875, 0.0000000000000000000000000000000000000001886368816105514203591898164749112707198763726417516124887345162 91.5, 0.0000000000000000000000000000000000000001828331349049259438514995771553202200972862439424360634273585181 91.53125, 0.0000000000000000000000000000000000000001772079507133511368673897845558020294660896155834102064022992033 91.5625, 0.0000000000000000000000000000000000000001717558352448258879422930638613249392255604400008402056308602291 91.59375, 0.0000000000000000000000000000000000000001664714637345286527789820121238038382693046621027734091371924249 91.625, 0.0000000000000000000000000000000000000001613496752434286201451706807211844556535292480241171466681482846 91.65625, 0.0000000000000000000000000000000000000001563854676178960216617829286107897439742381592466326002475045468 91.6875, 0.0000000000000000000000000000000000000001515739926043889293378178144723323753492587349204339800270746733 91.71875, 0.0000000000000000000000000000000000000001469105511144453389150803765206644151819196071306859358894332766 91.75, 0.0000000000000000000000000000000000000001423905886353561315372995844803480481881887756887445907366071874 91.78125, 0.000000000000000000000000000000000000000138009690782036784318946795730999213837135712491130120495079113 91.8125, 0.000000000000000000000000000000000000000133763578985753601014124061144229511912841383707539683866961711 91.84375, 0.0000000000000000000000000000000000000001296481063154938918547509756869011792762643329721720627502613671 91.875, 0.0000000000000000000000000000000000000001256592534278990772758265312594862403230432131822366330430165388 91.90625, 0.0000000000000000000000000000000000000001217931246418052501934967126900099914165226160409644332910147491 91.9375, 0.0000000000000000000000000000000000000001180459441335574283761349893762174273399879349169348154125296859 91.96875, 0.0000000000000000000000000000000000000001144140522493816811039172890993356253640445423932805910563112788 92.0, 0.0000000000000000000000000000000000000001108939019312136379459597534352117145641980176873064965893702127 92.03125, 0.0000000000000000000000000000000000000001074820552524926937492459123407095474093111193338162931413519245 92.0625, 0.0000000000000000000000000000000000000001041751800605386210483495750111169062802223909117251190428399964 92.09375, 0.0000000000000000000000000000000000000001009700467222313939578910506762707065345516898064324292310069352 92.125, 0.00000000000000000000000000000000000000009786352496981591786219680335847852394607403973021673414550738076 92.15625, 0.00000000000000000000000000000000000000009485258084375114540242184540723470126707870156142837095038592315 92.1875, 0.00000000000000000000000000000000000000009193427372961783688253374642244438597397190501627642324521349711 92.21875, 0.00000000000000000000000000000000000000008910575348619108483585021487469569287260483624058094843373173406 92.25, 0.00000000000000000000000000000000000000008636425766187275782918311010190422563737211838837093719212225894 92.28125, 0.00000000000000000000000000000000000000008370710879676531458771627384025553252514031134339507542597083605 92.3125, 0.000000000000000000000000000000000000000081131711807752080480834578829191382751880730552784666814211771 92.34375, 0.00000000000000000000000000000000000000007863555145403014600449663795809449423807819345169685689291033451 92.375, 0.00000000000000000000000000000000000000007621618988062062030342019269921842672024391591312942143903706865 92.40625, 0.00000000000000000000000000000000000000007387126423745712873441539503081328900813647001728072877254980595 92.4375, 0.00000000000000000000000000000000000000007159848437172725635776234050551440557007743045392087097691267664 92.46875, 0.00000000000000000000000000000000000000006939563059121318114534766942212583278132249795495934764323375861 92.5, 0.00000000000000000000000000000000000000006726055149644708148317514741167282579625300553829005393384590219 92.53125, 0.00000000000000000000000000000000000000006519116187956410994297191657543406558411340471245088867593132381 92.5625, 0.00000000000000000000000000000000000000006318544068780086494049064861342358902074623343379342975053957201 92.59375, 0.00000000000000000000000000000000000000006124142904965042740486763295007536440743250620285428454909544799 92.625, 0.00000000000000000000000000000000000000005935722836174622261482393842410817096935333973630554967155726851 92.65625, 0.00000000000000000000000000000000000000005753099843460627767727429320531507140651788491142608867976498667 92.6875, 0.00000000000000000000000000000000000000005576095569542693065698410611449490547162200194198971393256185665 92.71875, 0.00000000000000000000000000000000000000005404537144617076425507368619189704636749000545584678017982351348 92.75, 0.00000000000000000000000000000000000000005238257017524753959486289982013560099145119926080877708396192371 92.78125, 0.00000000000000000000000000000000000000005077092792113924684704445570661377855937224960784560290246085492 92.8125, 0.00000000000000000000000000000000000000004920887068637112023105680687824470030541458588596153074729507406 92.84375, 0.00000000000000000000000000000000000000004769487290027963491163180227810012660088979653175165929524213464 92.875, 0.00000000000000000000000000000000000000004622745592907616049028616345169181548962131765529764288537990719 92.90625, 0.00000000000000000000000000000000000000004480518663175113671501279898932984321079666720825810457996312165 92.9375, 0.00000000000000000000000000000000000000004342667596040840680960266284254495324858676533783297337985882793 92.96875, 0.00000000000000000000000000000000000000004209057760366273618093490920243855658964633718193601897645216502 93.0, 0.00000000000000000000000000000000000000004079558667177560157700966530454417991730661765485682561885933399 93.03125, 0.0000000000000000000000000000000000000000395404384222450991154024876240150378106865916062007958915650826 93.0625, 0.00000000000000000000000000000000000000003832390702460532879238813331724676340349971001750561447105749931 93.09375, 0.00000000000000000000000000000000000000003714480436322890670354759263829745134964845190481102021300754206 93.125, 0.00000000000000000000000000000000000000003600197887696337165639362630385942137058204155450309708432290741 93.15625, 0.00000000000000000000000000000000000000003489431443446822638293246980241704249002882413382029288999741497 93.1875, 0.00000000000000000000000000000000000000003382072924415422029837975099418334287709862288731209352620796109 93.21875, 0.00000000000000000000000000000000000000003278017479766027475378718910077325141967647807073651350725434697 93.25, 0.00000000000000000000000000000000000000003177163484583620599975372316983035145401498804462904827209332797 93.28125, 0.00000000000000000000000000000000000000003079412440623114760488474413213463102537033433742214245152524498 93.3125, 0.00000000000000000000000000000000000000002984668880111834386121642143877206733944618763638079843219263074 93.34375, 0.0000000000000000000000000000000000000000289284027251168088105057997640125426058100444909774556516533038 93.375, 0.00000000000000000000000000000000000000002803836934149925106659433568862781190773505184890460868868812537 93.40625, 0.00000000000000000000000000000000000000002717571940630368082034781205923770135829212436289506282375790076 93.4375, 0.00000000000000000000000000000000000000002633961041939326965676705279585452947362060661081748475524106257 93.46875, 0.00000000000000000000000000000000000000002552922580163535260872517792142756841449908674154738341798602926 93.5, 0.00000000000000000000000000000000000000002474377409739597092245809224835372031044791840838412389494116057 93.53125, 0.00000000000000000000000000000000000000002398248820157107822962513052911572765175960273983691550981773294 93.5625, 0.00000000000000000000000000000000000000002324462461039949635617704660655147886426662708276828685405291787 93.59375, 0.00000000000000000000000000000000000000002252946269532593325321340571932285362505030815780809593010552234 93.625, 0.00000000000000000000000000000000000000002183630399920488719321642921926934372483417948452417771198121558 93.65625, 0.00000000000000000000000000000000000000002116447155415808042234378944313363433897099092858068310147214918 93.6875, 0.00000000000000000000000000000000000000002051330922041921320524621234457130761519426836285073346416210284 93.71875, 0.00000000000000000000000000000000000000001988218104552032629692323483971835108208031162939264662762376149 93.75, 0.0000000000000000000000000000000000000000192704706431939263447684106766638913411822122043511630979615806 93.78125, 0.00000000000000000000000000000000000000001867758059138428396561083872134019537633163258715243881475829119 93.8125, 0.00000000000000000000000000000000000000001810293184877997706271034906993157474979593212533270507980250432 93.84375, 0.00000000000000000000000000000000000000001754596318929784057320256450511900498393058899746931865180123619 93.875, 0.00000000000000000000000000000000000000001700613065396601593352833692072238624936235409680187459371644893 93.90625, 0.00000000000000000000000000000000000000001648290701967078624150162563857846042638652249487190994862688152 93.9375, 0.0000000000000000000000000000000000000000159757812842483529746461602034998879577715421467125667557216845 93.96875, 0.00000000000000000000000000000000000000001548425816741867328044130630965578844880466093128615296148849496 94.0, 0.00000000000000000000000000000000000000001500785762707394887544965003009105458417081168445378468976407202 94.03125, 0.00000000000000000000000000000000000000001454611439044935358757841042340420638889568842886996265434298152 94.0625, 0.00000000000000000000000000000000000000001409857749971812119467429081552762312534286472471997903607948795 94.09375, 0.00000000000000000000000000000000000000001366480987156720264838574585252120003677242076435045105473421027 94.125, 0.00000000000000000000000000000000000000001324438787032335578312893938346694183950043792704988134472282666 94.15625, 0.00000000000000000000000000000000000000001283690089421276453114407775271994184758737744317880453545098725 94.1875, 0.00000000000000000000000000000000000000001244195097435011142083021940044759549389590974350106974400946537 94.21875, 0.00000000000000000000000000000000000000001205915238606545925394759991469508509799222123778125361306438424 94.25, 0.00000000000000000000000000000000000000001168813127218934747960564592740346340821399355669699641298208778 94.28125, 0.00000000000000000000000000000000000000001132852527792818767737913641385258022181052520792079499699075143 94.3125, 0.00000000000000000000000000000000000000001097998319697336213451292760783677765062876791443809305823035173 94.34375, 0.00000000000000000000000000000000000000001064216462849840080815705069863987025733840114395154223297235002 94.375, 0.00000000000000000000000000000000000000001031473964470924571795676662894655886058148498431498053123176724 94.40625, 0.000000000000000000000000000000000000000009997388468622918402477206474300982384336925961334305698434265404 94.4375, 0.000000000000000000000000000000000000000009689801161759895560716154913032645712848896454917446705006183641 94.46875, 0.000000000000000000000000000000000000000009391677321445180143504421421946693438282058216596211102690073436 94.5, 0.000000000000000000000000000000000000000009102725787422439414898888271750273174569163509246908124095912242 94.53125, 0.000000000000000000000000000000000000000008822664357494677035796365948414993229483004411154440573072213253 94.5625, 0.000000000000000000000000000000000000000008551219511913721914015285679156127068641250857238567973422109593 94.59375, 0.000000000000000000000000000000000000000008288126146249361026781883562185169014541094800694020309495376314 94.625, 0.000000000000000000000000000000000000000008033127312477224997797825072147352687863632113683183071496959344 94.65625, 0.000000000000000000000000000000000000000007785973968032561989992161572080195376229104288818208025785160195 94.6875, 0.000000000000000000000000000000000000000007546424732584815295961328615021359580263917285279420911394043278 94.71875, 0.000000000000000000000000000000000000000007314245652295460469076242461030166659565067436781812236306037865 94.75, 0.0000000000000000000000000000000000000000070892099713288663036161291100602853894452235860965079672168332 94.78125, 0.00000000000000000000000000000000000000000687109791039302757982859602961088888904397052852902611658298723 94.8125, 0.000000000000000000000000000000000000000006659696452093883157663091672280559342989112868053931218999832525 94.84375, 0.000000000000000000000000000000000000000006454799132893587436379701307672299361603270001241743075873123569 94.875, 0.0000000000000000000000000000000000000000062562058414695528952962344137303244384611651941653936436965889 94.90625, 0.000000000000000000000000000000000000000006063722623277332692649624221224572527878567125124083433966604871 94.9375, 0.0000000000000000000000000000000000000000058771614911264712301573724645310611685247690238283874355051169 94.96875, 0.000000000000000000000000000000000000000005696340241584323107777091726460047050792827648249747543526995089 95.0, 0.000000000000000000000000000000000000000005521082277028532731723642066679472073387404362974854801802151705 95.03125, 0.000000000000000000000000000000000000000005351216433174383557902384465234911505860253335314795849911905994 95.0625, 0.000000000000000000000000000000000000000005186576811908572940413119852207667395878665029821599926748026169 95.09375, 0.000000000000000000000000000000000000000005027002619266151032764779959365415067315363147921374327118941041 95.125, 0.000000000000000000000000000000000000000004872338008392385219341422385296787525416250868723727186632012371 95.15625, 0.000000000000000000000000000000000000000004722431927336180042177401850887579781178427652518019387304046962 95.1875, 0.000000000000000000000000000000000000000004577137971526401288003008989948796991330203173920652861321056654 95.21875, 0.000000000000000000000000000000000000000004436314240787026421291782386238659499659150185858326828569117018 95.25, 0.000000000000000000000000000000000000000004299823200751476357369468408952476981498922458454428082272038078 95.28125, 0.000000000000000000000000000000000000000004167531548540779994798909699605515845330265776197459952135176104 95.3125, 0.000000000000000000000000000000000000000004039310082574387164304136984929938314391254705118912508573168191 95.34375, 0.000000000000000000000000000000000000000003915033576386481769410294632805325655534881797118449568927351904 95.375, 0.000000000000000000000000000000000000000003794580656324558833595920067121271315352680350724720812403563277 95.40625, 0.000000000000000000000000000000000000000003677833683010820750644959010324764782633311851403979124492832177 95.4375, 0.000000000000000000000000000000000000000003564678636450622962065760488688783880158898147256448136744739832 95.46875, 0.00000000000000000000000000000000000000000345500500467576114700129817840660969069481786315216085817999546 95.5, 0.000000000000000000000000000000000000000003348705675813844284652515289376857677402106207400418032586890935 95.53125, 0.000000000000000000000000000000000000000003245676833478344008511271612141044470476832372150642480052823661 95.5625, 0.000000000000000000000000000000000000000003145817855377153783552823481003711293128433717939530307738066239 95.59375, 0.000000000000000000000000000000000000000003049031215040634769329257383170396870046619822064679942653062382 95.625, 0.000000000000000000000000000000000000000002955222386573171853544596095132472569564067788586228002615258959 95.65625, 0.000000000000000000000000000000000000000002864299752335216227637978796480919343730261000997533047679082244 95.6875, 0.00000000000000000000000000000000000000000277617451346565291206672350019587740269268563942178170259453843 95.71875, 0.00000000000000000000000000000000000000000269076060315710561955086573026848794849702817712531738935171692 95.75, 0.00000000000000000000000000000000000000000260797460259947997870079519857543349546567038144067658961209125 95.78125, 0.000000000000000000000000000000000000000002527735659509652054032859586436691932473142356483819650279960087 95.8125, 0.000000000000000000000000000000000000000002449965409167734836428861208464750796669610458127204738942918827 95.84375, 0.000000000000000000000000000000000000000002374587897882803407356037972178547995928284964244240701843675561 95.875, 0.000000000000000000000000000000000000000002301529508813332191482527734348033792124186993249432297766951507 95.90625, 0.000000000000000000000000000000000000000002230718890069897422988934068708648128660585430539773877005463673 95.9375, 0.00000000000000000000000000000000000000000216208688502992690688340902556177352665064713292389942766603331 95.96875, 0.000000000000000000000000000000000000000002095566464796439534867642344537827680807787755214189374473556718 96.0, 0.000000000000000000000000000000000000000002031092662734810925690368777712955382670817683531436185415229678 96.03125, 0.000000000000000000000000000000000000000001968602511023631047464254629782699873633211930553126134700473627 96.0625, 0.000000000000000000000000000000000000000001908034979157686726194020802258182707743322745582978742868424942 96.09375, 0.000000000000000000000000000000000000000001849330914343008471868847738170009632965398269357820333468581269 96.125, 0.000000000000000000000000000000000000000001792432983725768922904885956236673165561252277160724758214168423 96.15625, 0.000000000000000000000000000000000000000001737285618398611226190917535139755822366423581695055872386587439 96.1875, 0.000000000000000000000000000000000000000001683834959129721582673739564485547188879945223753541200047375917 96.21875, 0.000000000000000000000000000000000000000001632028803761642692684862660792180026924590939762594540310250099 96.25, 0.000000000000000000000000000000000000000001581816556228455574260925243175882963349671346875618900309501585 96.28125, 0.000000000000000000000000000000000000000001533149177141537794195025393212884657044929027335322190432075731 96.3125, 0.000000000000000000000000000000000000000001485979135895638089122540827830261585202148307118468308636210669 96.34375, 0.000000000000000000000000000000000000000001440260364248492158747474872451996494801445599894635200041326847 96.375, 0.000000000000000000000000000000000000000001395948211328643535476627768542144987672128879894018699857409897 96.40625, 0.000000000000000000000000000000000000000001352999400027528279756058668725136857370235314061471415791650499 96.4375, 0.000000000000000000000000000000000000000001311371984733234180561577037324256079638403271637530073274373179 96.46875, 0.000000000000000000000000000000000000000001271025310364655476134122349941291604403398167840543353955361068 96.5, 0.000000000000000000000000000000000000000001231919972666034130900645061215066830224833826443989728356641473 96.53125, 0.000000000000000000000000000000000000000001194017779723109650937161347527967847089762007291531641480269447 96.5625, 0.000000000000000000000000000000000000000001157281714663292494506958580664990985934908922631238042755720335 96.59375, 0.000000000000000000000000000000000000000001121675899503432501349798249753934653151875434713248722166942781 96.625, 0.00000000000000000000000000000000000000000108716556010987455386408887626927991450826102226856767450708643 96.65625, 0.000000000000000000000000000000000000000001053716992236579989723421617505579402240064043343838121243468057 96.6875, 0.000000000000000000000000000000000000000001021297528608145169734003597863975685976512928445601386111164502 96.71875, 0.0000000000000000000000000000000000000000009898755070155690951613571699692175066827905478387087716090359841 96.75, 0.00000000000000000000000000000000000000000095942023939361106198695879267428932260178892370914251031132719 96.78125, 0.0000000000000000000000000000000000000000009299019818495380015882373202583870026372660957050866712548461142 96.8125, 0.0000000000000000000000000000000000000000009012919056139903244381196735392518605760855091285942262514653622 96.84375, 0.0000000000000000000000000000000000000000008735620688855956630566968768505905373841875280533846728654628028 96.875, 0.0000000000000000000000000000000000000000008466853895418327821614604210571029204984234844756132856575361508 96.90625, 0.0000000000000000000000000000000000000000008206356186894939402370828043963493633628336796612566686225566237 96.9375, 0.0000000000000000000000000000000000000000007953873150289139738358925210641335340492878641243798468307064266 96.96875, 0.0000000000000000000000000000000000000000007709158200069291346598343028298744154931520193973314237250564492 97.0, 0.000000000000000000000000000000000000000000747197233734299016058555557684525731124226786546069523612403334 97.03125, 0.0000000000000000000000000000000000000000007242083916440715123200634426251661488087377317997669966317371307 97.0625, 0.0000000000000000000000000000000000000000007019268418680943901976370519946362213756761080365909374101997419 97.09375, 0.0000000000000000000000000000000000000000006803308233095784242427140121576460225008449721642046361222400742 97.125, 0.000000000000000000000000000000000000000000659399244390296840687759041631707837931896764189531861870894185 97.15625, 0.0000000000000000000000000000000000000000006391116624516646927602123952151640832859241388887209566721512174 97.1875, 0.0000000000000000000000000000000000000000006194482637895803968965539477705070182101058912898329434128281555 97.21875, 0.0000000000000000000000000000000000000000006003898443035306180543035650488148057013756376130260382213701441 97.25, 0.0000000000000000000000000000000000000000005819177907410596076904859557680020907062366486957495977931178533 97.28125, 0.000000000000000000000000000000000000000000564014062519285555890251149773246688559129675247622671755997163 97.3125, 0.0000000000000000000000000000000000000000005466611741057100874651670054068873294663597238456244664756767065 97.34375, 0.0000000000000000000000000000000000000000005298421779411132610016805148165352698159806168165695549045423334 97.375, 0.0000000000000000000000000000000000000000005135406478878558532967069753207021052414121603554969044231244947 97.40625, 0.0000000000000000000000000000000000000000004977406631874238464264206953189117434403159427139746934218846769 97.4375, 0.0000000000000000000000000000000000000000004824267929115473820050814487537466347174554262317301954389787175 97.46875, 0.0000000000000000000000000000000000000000004675840808917084927333974471148033342734206608772169281559851741 97.5, 0.0000000000000000000000000000000000000000004531980311123191358964092075853144399657553608023206351029196731 97.53125, 0.0000000000000000000000000000000000000000004392545935533038933505306683700852719343346239888052441446552333 97.5625, 0.0000000000000000000000000000000000000000004257401504682606100006388618805469583660440894818280950039777696 97.59375, 0.0000000000000000000000000000000000000000004126415030847976464664114016520329274428396838281120540496292285 97.625, 0.0000000000000000000000000000000000000000003999458587140587370395802474558885788564757682066747260230838742 97.65625, 0.0000000000000000000000000000000000000000003876408182568460738249298596955308220394822085477577556604262717 97.6875, 0.0000000000000000000000000000000000000000003757143640941395724329466243716297479113996976466009575673165483 97.71875, 0.0000000000000000000000000000000000000000003641548483501856920381312232283499805674689671015314846420533504 97.75, 0.0000000000000000000000000000000000000000003529509815166930496825526211102661358641903427825609190475252641 97.78125, 0.0000000000000000000000000000000000000000003420918214270247407571012594336337026688795646824869632637296853 97.8125, 0.0000000000000000000000000000000000000000003315667625696190990674202895331877793895070672893651072394920453 97.84375, 0.000000000000000000000000000000000000000000321365525730201934624857379037010871942700463383435388954967164 97.875, 0.0000000000000000000000000000000000000000003114781479526743988610950455303372113995949546465312485060162278 97.90625, 0.0000000000000000000000000000000000000000003018949728088718589594112665524725420222021072226827138845496806 97.9375, 0.0000000000000000000000000000000000000000002926066409676908193880240127535332911175781030977401574676031009 97.96875, 0.0000000000000000000000000000000000000000002836040810543733041219784310139230578458728114884526915643092599 98.0, 0.0000000000000000000000000000000000000000002748785007910214929956328761371856588953132860316523297892513631 98.03125, 0.0000000000000000000000000000000000000000002664213784096900668987494900940587471196232022145648579146079267 98.0625, 0.0000000000000000000000000000000000000000002582244543296699273612227955580523837654631724143229772625834804 98.09375, 0.0000000000000000000000000000000000000000002502797230908349764572586825534864054143811615314778221668210153 98.125, 0.0000000000000000000000000000000000000000002425794255351737248928854960855731536576137911607294009576138605 98.15625, 0.0000000000000000000000000000000000000000002351160412288698838615518150109626711937938517959522232661502065 98.1875, 0.0000000000000000000000000000000000000000002278822811175310264872644854641457825403984689167351885659540218 98.21875, 0.0000000000000000000000000000000000000000002208710804073921068659740444545504787055214351680545766235661728 98.25, 0.0000000000000000000000000000000000000000002140755916655413212472407202342335593323719242246144417509930735 98.28125, 0.0000000000000000000000000000000000000000002074891781324297023111090542273559786685856245327086313114374011 98.3125, 0.0000000000000000000000000000000000000000002011054072401331626995645050731941635579830920939594942636414226 98.34375, 0.0000000000000000000000000000000000000000001949180443300366504404383980393574898257735372188032267423727596 98.375, 0.0000000000000000000000000000000000000000001889210465638048429069339871857388051580836923126209752442344466 98.40625, 0.000000000000000000000000000000000000000000183108557021692577702769091570886193486124756197481367187852161 98.4375, 0.000000000000000000000000000000000000000000177474898982431182713681475941010277931768967963384400752407192 98.46875, 0.0000000000000000000000000000000000000000001720145703791042022108985032451579406245167978846982613457733994 98.5, 0.000000000000000000000000000000000000000000166722238425597894523590457662292578578327944035252693144497004 98.53125, 0.0000000000000000000000000000000000000000001615927344083784672791934476412984535570443157944083887061528649 98.5625, 0.0000000000000000000000000000000000000000001566210486385094812420602494788433595927456378258487858291124725 98.59375, 0.0000000000000000000000000000000000000000001518023255589793510557032436834661482443724557902391358182478004 98.625, 0.0000000000000000000000000000000000000000001471318590025605535537403471589674854001778204930482625224333831 98.65625, 0.0000000000000000000000000000000000000000001426050875955691698889021152420742567856362896854185831078535334 98.6875, 0.0000000000000000000000000000000000000000001382175903030358801196965995616819142919515453726653171982218044 98.71875, 0.0000000000000000000000000000000000000000001339650821109376372546075891219776648381844561437789636837157397 98.75, 0.0000000000000000000000000000000000000000001298434098412731069664433415842837745120127696710208367398574483 98.78125, 0.0000000000000000000000000000000000000000001258485480958946999871827930182848760185794597909501167527435422 98.8125, 0.0000000000000000000000000000000000000000001219765953251357732866935083612642515912285166335176882956069524 98.84375, 0.0000000000000000000000000000000000000000001182237700173934563388629262584063067579512623144760143309869284 98.875, 0.0000000000000000000000000000000000000000001145864070059456891193112200872818646296000436586268643866008325 98.90625, 0.0000000000000000000000000000000000000000001110609538893955543309895717199427913399491710451785138629419558 98.9375, 0.0000000000000000000000000000000000000000001076439675622469595390670459959815412888177753904589422449871695 98.96875, 0.0000000000000000000000000000000000000000001043321108522232837956432790572468530586040854613450650446178937 99.0, 0.0000000000000000000000000000000000000000001011221492610448529945285797620258654961994482959553064992885338 99.03125, 0.00000000000000000000000000000000000000000009801094780548215043120138369675611392605204374688250233580298768 99.0625, 0.00000000000000000000000000000000000000000009499546795559960253517315920735403784650835524721337628062793335 99.09375, 0.00000000000000000000000000000000000000000009207276466719970013732205101200629608134538702896106577133009964 99.125, 0.00000000000000000000000000000000000000000008923998350556921563654116576912811449288685645134866388281744247 99.15625, 0.00000000000000000000000000000000000000000008649435785771844588934893949304730822181368572985639426503229732 99.1875, 0.00000000000000000000000000000000000000000008383320623039083664956436070629763712274561319728936845970074892 99.21875, 0.00000000000000000000000000000000000000000008125392963120411134002170027647712570485371889196365766971850207 99.25, 0.00000000000000000000000000000000000000000007875400903036521665491210223644937224253343496689950087770790337 99.28125, 0.00000000000000000000000000000000000000000007633100290048008926297813812188148294862987707628102317409033805 99.3125, 0.00000000000000000000000000000000000000000007398254483205551856213952515155234726704111141363607449882235213 99.34375, 0.00000000000000000000000000000000000000000007170634122236430451434158366415094740048887444543232348963020445 99.375, 0.0000000000000000000000000000000000000000000695001690354165592628504410013218662196285576041019803277669795 99.40625, 0.00000000000000000000000000000000000000000006736187363084944647894907865539420802831192476686609641509306791 99.4375, 0.00000000000000000000000000000000000000000006528936665961496102415706555948427354559773031444186342570573657 99.46875, 0.00000000000000000000000000000000000000000006328062402441058928414654528432324551563964494006088195874288963 99.5, 0.00000000000000000000000000000000000000000006133368390286092114540236372454593779037485662580265974523263977 99.53125, 0.00000000000000000000000000000000000000000005944664483151956979904775715148829077179039042099345185506257293 99.5625, 0.00000000000000000000000000000000000000000005761766384882015522184062308308891226949551846533968462439761365 99.59375, 0.00000000000000000000000000000000000000000005584495469516267931434580472038474760077500399871455669605237684 99.625, 0.00000000000000000000000000000000000000000005412678606837742149806480800035427810355077137486310514232810816 99.65625, 0.00000000000000000000000000000000000000000005246147993286256758428074946988727653907697026699730943588936025 99.6875, 0.00000000000000000000000000000000000000000005084740988074420474826842325024703229494305178558686799589811131 99.71875, 0.00000000000000000000000000000000000000000004928299954345812266891993334643207551628052336431317937285653172 99.75, 0.00000000000000000000000000000000000000000004776672105220210494621532227270137679441826082103247717141348343 99.78125, 0.00000000000000000000000000000000000000000004629709354575512388113363282919978025653176140914996697530511473 99.8125, 0.00000000000000000000000000000000000000000004487268172420611220867052912615493077659265869224045577557460125 99.84375, 0.00000000000000000000000000000000000000000004349209444717982259455600965597318541155512820264630343940873562 99.875, 0.00000000000000000000000000000000000000000004215398337519074342996091280273279493473931998599909700509830365 99.90625, 0.00000000000000000000000000000000000000000004085704165279816012858096172068941852335487150352458681818298508 99.9375, 0.00000000000000000000000000000000000000000003960000263227627588380647465890102958946535387047210678260873866 99.96875, 0.00000000000000000000000000000000000000000003838163863655287455139230752277322872024739529867563072657639638 100.0, 0.00000000000000000000000000000000000000000003720075976020835962959695803863118337358892292376781967120613877 ././@LongLink100644 0 0 157 12126630646 10264 Lustar 0 0 commons-math3-3.2-src/src/test/resources/org/apache/commons/math3/distribution/gamma-distribution-shape-10.csvcommons-math3-3.2-src/src/test/resources/org/apache/commons/math3/distribution/gamma-distribution-sh100644 1750 1750 104562 12126627677 32601 0ustarlucluc 0 0 # # Licensed to the Apache Software Foundation (ASF) under one or more # contributor license agreements. See the NOTICE file distributed with # this work for additional information regarding copyright ownership. # The ASF licenses this file to You 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. 0.25, 0.000000000008186974254985386188332684109222748579137668937877177711110448987 0.5, 0.000000003264523243913241720337772725913936626787771049309446711910683277 0.75, 0.00000009773890658452389396475494919437519384431793837050355657692030534 1.0, 0.000001013777119630297402985901042111609533305255541864439579221331574 1.25, 0.000005882459987908221107910989757584493395253807161915190072123719547 1.5, 0.0000236383182704678991833440091359405703674960353021860512367257178 1.75, 0.00007371639049914659664469924484337953134029579678102318895787762898 2.0, 0.0001909492532438979779809516683915123857603266961687137657399066986 2.25, 0.0004292566637511216785564507142900505556329467302903493327405109363 2.5, 0.0008629007379834066956292245172230634722739523367006800671498234154 2.75, 0.001584606329630760443966534916196585479149580956923021059184841385 3.0, 0.002700503931560477264281296206018975830692634129863134237261950404 3.25, 0.004322451706707033355777741992379271252649548199759156207752373941 3.5, 0.006558713791472451118496393575940093861858366336090666583626783936 3.75, 0.009504167235007731279532455006689505603740590876228765050787467591 4.0, 0.01323119169105030026156420020021095733685922904744903636265984176 4.25, 0.01778221224061525289778491090859724569265354775286287693155638646 4.5, 0.02316457958265915634571674057321856666997044100638758787392945839 4.75, 0.02934814558017206800687952058687639675698293535639192619091031311 5.0, 0.03626557741564374703239991753875045775748000924975997914204555598 5.25, 0.04381518681987509794127311938460423207274500951027751679576035436 5.5, 0.05186585267604764829341293838441145342635701430684257013601927961 5.75, 0.06026349267933013221060977993029191814044764041664500524145107011 6.0, 0.0688384890205628682057115069358228911584085187753998454129720724 6.25, 0.07741348363821588179242489993827725096106897663175209273166362133 6.5, 0.08581101609904129066714371979939291963726757261354782675267519001 6.75, 0.09386056621162676713466743812158052742215274755818757154910937699 7.0, 0.1014046695005910607718496701878610512032861425441336924689561491 7.25, 0.1083038842159016853542084991854392401098160237845471037680183154 7.5, 0.1144404938782313557129127821861648551635697820637634247132376202 7.75, 0.1197209225902342460071901612354126628680720066922580960372680576 8.0, 0.1240769172894194899132193735290272042980410647675194334399961409 8.25, 0.1274656099100286025054532512661365070756171192489062231813542786 8.5, 0.1298686130276381258335852813355502082119818198803296466496592805 8.75, 0.1312903262928634037083904942427229617254447530221262005041625199 9.0, 0.1317556400095226781654379163000757051460934732366505649914730029 9.25, 0.1313072192445839225111988810440828276135811270793666373206690422 9.5, 0.1300025396922678897051382132478766482164529392345600519685638399 9.75, 0.1279108278844427375847798106085518500533017450663876166578688028 10.0, 0.1251100357211332989847649789477254470842099009770860043514972047 10.25, 0.1216839548114256581832096801263610814460525105839874962403695546 10.5, 0.1177195514884425017802289063930658092780193779914413713717151257 10.75, 0.1133045799085349966923527501254387703835817674836533651678567706 11.0, 0.1085255092982049925567723911071761966506123976760930505104271492 11.25, 0.1034657827664601667257975127939732767293311635568840287927182335 11.5, 0.09820440946195089846505727448594103423212397410795327666438396544 11.75, 0.09281487929890386376369675560679807701962121451841671141743878224 12.0, 0.08736437990304943375271063478563075520379929766870135796666642596 12.25, 0.08191328861243157710772139631595405936183410336190099283268454613 12.5, 0.07651490800086525049008127388884441072002756852772088120717045143 12.75, 0.07121541112210824655183580285963650817326868346250504236851589519 13.0, 0.06605396213377439489822781845279065065585621014422393925197003788 13.25, 0.06106297879219139301278467223371192626748387848826261957833710927 13.5, 0.05626850517663460655605768966544574418710132441446334613962998937 13.75, 0.05169066560121551839263986250053012563817317356822848663050941404 14.0, 0.0473441737423897999172340307540283125058422079024338788553920038 14.25, 0.04323887432877600812057686543159688428501664818426054620983206842 14.5, 0.03938029812853871385255758319428126354432781858435760681972677061 14.75, 0.03577021428805958120847171122368725677994839525278837008628425846 15.0, 0.03240716721973688030756030183700839772561621518332775239079093892 15.25, 0.02928698813358412388775062026510634259301416747061653867790243033 15.5, 0.02640327391057035333416500541410044010111670478889168948637873012 15.75, 0.02374782830108497927883668342014640761640697526014976173371549044 16.0, 0.02131106239280704930275940632979675069159084707269314469334767645 16.25, 0.01908235293540108372255894525778934654556128513691048553504324663 16.5, 0.01705035845154365877138652537743248321987374784169593114495003386 16.75, 0.0152032941283178403971318062228893301922899994371567897915502031 17.0, 0.01352916729774490908041937026715697873618810454978269877122357257 17.25, 0.0120159759100494399666402885233398034929255037207100068417152775 17.5, 0.01065187280857420412483887226552640038150355932877485289458786971 17.75, 0.009425298860738239238578699210151974998214065268721819591580327521 18.0, 0.008325088113095818939263997415818015720730805517010736569702414073 18.25, 0.007340548146202262631833418691597004767626824406578256047553155974 18.5, 0.006461518729822576315926682253990003331946204105648658100907075287 18.75, 0.005678411741457590687241573648095290578064818568016577911589648148 19.0, 0.004982235128839516200951213042141621091148093631791638527097598757 19.25, 0.004364603484881537029613871530929452463478625944999331364650930942 19.5, 0.00381773757391904082206231562425439565527413787186689099820509122 19.75, 0.003334454910977844131784343323361008155249340059473279948582400177 20.0, 0.00290815325917256836397310812014930649224099791971654842041939282 20.25, 0.002532788680255681676285830871320780127330297183468394199710157135 20.5, 0.002202849554291831080610799467113124709470646984158005490698040748 20.75, 0.001913327779561664829510932193068508872784297028732054303717105105 21.0, 0.001659688175122662193682235206109124061023214116980172148558560216 21.25, 0.001437836937077468304371446222987336966974426954055989989264722461 21.5, 0.001244089845902406609372724187570894846491426842722204618221296691 21.75, 0.001075140785986397767745721638875264871166253025076953538857647045 22.0, 0.0009280310192167132015539230980793683825618484003508844900648521617 22.25, 0.0008001195511119990371764866001142738287471067597208907759703947413 22.5, 0.0006890548395286770760217732542044562257499392265532491325755268366 22.75, 0.0005927480211133109560017597323175313243535802134387412367294546956 23.0, 0.0005093477681396828138538122997131070767246707562792417315739338635 23.25, 0.0004372168368440362621717127292842106656497594716980149705262897732 23.5, 0.0003749103265722543627986183265346427745333970952765187279001148214 23.75, 0.0003211556357517835282337269741547968428539324102156986765413736799 24.0, 0.000274834074747481659022576219413751034345788763653911467086865837 24.25, 0.0002349640759907655302904223665762111698786036743899858628780223874 24.5, 0.0002006859274167446762593172133590081238910778981206838657308559932 24.75, 0.0001712479453330368679435136899150296408153028794517970636414325658 25.0, 0.0001459939966017188701539418447804709266144527159061492391737239067 25.25, 0.0001243522767606247615425153168760537808253283602138395915159523612 25.5, 0.0001058252498483833467205341447213060783913409745793699261240977234 25.75, 0.00008998065671760097676091245699772082171072779121844934941582349231 26.0, 0.00007644350108599464747259749910476391151376164510208334624206614866 26.25, 0.00006488892611756880046471000394716701559053814393486144723059671064 26.5, 0.00005503589863736550177565702130375377954688189868256189248384961953 26.75, 0.00004664162290988367633054375190222117826870790609584729791557428934 27.0, 0.00003949661104596843612791879547739834712628906349743115560828110407 27.25, 0.00003342034237958883629237590159068281234261240681404779403983684661 27.5, 0.00002825744944321782297848160528393033910908763242730782563684602053 27.75, 0.00002387437336701962872906214463082372201646515343050073510365729013 28.0, 0.0000201564365563013950572036689936672919238056971324697861774441196 28.25, 0.00001700528530812900627901600062229070979629771106907269676001478733 28.5, 0.00001433665957326872753782068589462376853920836828009457516761053815 28.75, 0.00001207845132932097382126997809224641283870096079394469309241147023 29.0, 0.00001016901699192579970063480838307681847630054822608658399699640964 29.25, 0.000008555712948991289682402495593782326502442795701036920568562456822 29.5, 0.00000719362666066504841431831716013318582129293334084076876608220817 29.75, 0.000006044478833090494947540791877094938402436981846430035802107557534 30.0, 0.000005075674958545005421862828639020146738894804494221917279438474162 30.25, 0.000004259487032711866044225064293923861454014093906361037409783521147 30.5, 0.00000357234852771514860308416904147862285392685962712608938257789174 30.75, 0.000002994247734282026142499194251374233520781690327686743516660346394 31.0, 0.000002508206405552949057643658762780835075376107309295916542026358533 31.25, 0.000002099832256169634460894938092137574432926173290949870098900263477 31.5, 0.000001756935310492767292434295178505489301209130863096374041622967223 31.75, 0.000001469199369665931754197778269253085994173998561995987001405615382 32.0, 0.000001227900994471717635947940670844626675781560771059957261972888826 32.25, 0.00000102566939431493724194480631831221991619882519235644946106020963 32.5, 0.000000856281486012846050826312917786579548642575788256660707766594072 32.75, 0.0000007144871521382537927552547718300676886720531061395026850539441051 33.0, 0.0000005958603991786300369164053291040693409767456785848080593869693059 33.25, 0.0000004966727014567857970523284638105406868208188773778197263475583667 33.5, 0.0000004137853273394655508401893266924830123303842382145467952921064821 33.75, 0.000000344557888537943606147987797005017813565680284385638637783029876 34.0, 0.0000002867707391984073550824403847626639269742801916883479140450042143 34.25, 0.0000002385591860877771310674513336011819973321686624434141396238261647 34.5, 0.0000001983577608416143904312050771678146963637004136309521708210926513 34.75, 0.0000001648530555963296041069625671109391205207946026301875820142638725 35.0, 0.0000001369438393826411666204050888554478848583604233007574588347643012 35.25, 0.0000001137073588376537605773591596975758337682189348053564944199301356 35.5, 0.00000009437088700327813389420965178989200297204016947696687684916506496 35.75, 0.00000007828772165364262929627671410052708059815495653102324579025739985 36.0, 0.00000006491695274772336542659448928668721711557299151027348372662701475 36.25, 0.00000005380641987450719988295346841315594203545010281482533339302922789 36.5, 0.00000004457836725188057644451748409056565961798568471219661455325581916 36.75, 0.00000003691737796677546967422291420597428735209681632999012562616037233 37.0, 0.00000003056023245138750734922029883254230615314243826748880158479669844 37.25, 0.0000000252873901968860968109996098288383250444651434820609477360523842 37.5, 0.00000002091583972813185793295793769971826241624456670133489556037766558 37.75, 0.00000001729310103942849409063297996391895067076965488831460877828935211 38.0, 0.00000001429219800618755610978872617491933016644071244880298670611133952 38.25, 0.00000001180744658924535209391155180995547456989089367071830019255677997 38.5, 0.000000009750928668337726641496114444648484450399671312838158599894238415 38.75, 0.000000008049541707456786551402738725157263957690554138273272825699690439 39.0, 0.000000006642531707251846223333171337985252304247645182421536668587422867 39.25, 0.000000005479431501195302860686872489823329574056154620501424840790950021 39.5, 0.000000004518338799372804469180625084099624821546304935265885195900702335 39.75, 0.000000003724478815602643888022757788870667286241808091586112088401200488 40.0, 0.000000003069005119872019140188456026558917679230105910029235272637769716 40.25, 0.000000002527999786033835423093822338651067788488758867571140704986322034 40.5, 0.00000000208164016507031540799804224329944148200812323456608914907506595 40.75, 0.000000001713504886419352719362730189293808666524880697824790678513021367 41.0, 0.000000001409996126472545819440234437751633807288134831364049449090861153 41.25, 0.00000000115985891403708312667031766508232350003957476174244507512818131 41.5, 0.0000000009537813773444187074325378586258443648521466908696131561755940528 41.75, 0.0000000007840624693887246532173892893419995838188490746173395960395015582 42.0, 0.0000000006443359170231513617348421587379731295884279231503435219184413585 42.25, 0.0000000005293409912205808390972606413472333069064809580264803824263305413 42.5, 0.0000000004347322477748747891795917348083050928143642537557344218168118275 42.75, 0.0000000003569216872576454279497644662689757367584755403342809700515579225 43.0, 0.0000000002929478705829987871299512354069569422870466569650219178990025446 43.25, 0.0000000002403674360800283713330666739716086188931125360848935417039750084 43.5, 0.0000000001971652241904543934209725193042342111079956547407511229343119081 43.75, 0.0000000001616798509285742186551862656860707587929107800860977557669481121 44.0, 0.0000000001325421013656396535695310183375758802606804422834123002407454587 44.25, 0.0000000001086239566995748061895939387274733885523351824006782199828265228 44.5, 0.00000000008899643728246801301611505620872408017436178851776040955438396712 44.75, 0.00000000007289475133940173194758993649853845041670779720085523795058215321 45.0, 0.00000000005968949512020330274439179852563592947058566503804409904350001651 45.25, 0.00000000004886286334492488287219708242209434802494490909520573406394683792 45.5, 0.00000000003998900612490440121886576856165681141102023674564480107504169371 45.75, 0.00000000003271781600001054606881071372703598844824200853831222195370729119 46.0, 0.00000000002676155129510619182116644353109166465379576196512996911037190928 46.25, 0.00000000002188380381675883485310611354220042675676987167891533083058379224 46.5, 0.00000000001789040345385841038492574673139729905217997796184211878015540453 46.75, 0.00000000001462192240985279332440912616790709149517144842755816777130933443 47.0, 0.00000000001194749999722136573258874830837269726599698013272978432293044138 47.25, 0.000000000009759757182807822404750951646831157685602343908135121319243374573 47.5, 0.000000000007970610066357884670599231301948755151593601318316408915463784616 47.75, 0.00000000000650782460415533303778838864268468090682307182227228937233009802 48.0, 0.000000000005312182320763912500838422500160625880672977520171217058495859103 48.25, 0.000000000004335149454647013552673114190898668188742742199068867770106171761 48.5, 0.000000000003536960764682213988267877446799116978396602147385276372411613929 48.75, 0.000000000002885044755010904826919240722689087321710982948039845851260628951 49.0, 0.000000000002352729912508490255079729899223716934622478988725439407064730318 49.25, 0.000000000001918182157175041469966344522800538430933987342760341696975374941 49.5, 0.000000000001563532465012282309094466492263561914056760946314773536673769432 49.75, 0.000000000001274160854104271520815330338494681767757413760393645635818162407 50.0, 0.000000000001038108891866326862862584790710240695288835502627344369446451556 50.25, 0.0000000000008455978037478342451442142195327172858077719995793812662800749935 50.5, 0.0000000000006886333224746665925539409725902279284111324441263188571742330864 50.75, 0.0000000000005606817624058355802275554110324298194697466279971547668791491248 51.0, 0.0000000000004564045600795882728526375884252042450006688109673087436288271114 51.25, 0.0000000000003714407923879311972477473901364157718331243929057047889686792357 51.5, 0.0000000000003022290530966494012712929190016007473303180945683219925486343095 51.75, 0.0000000000002458616069214598138416237818818318720325144026643711387287379266 52.0, 0.0000000000001999650061645834757946699936578139033586178267891223118720699599 52.25, 0.0000000000001626023959806504022187183741862230393866236160992461501345779129 52.5, 0.0000000000001321935902784086389540723718098949893784050549370327714389673838 52.75, 0.0000000000001074497037549860686086458398510273783570781904310525073569490574 53.0, 0.00000000000008731970355928199712944531777765349747014527059388637449224807525 53.25, 0.00000000000007094671881780450978934389210841701571448547901568109970752853841 53.5, 0.00000000000005763233605048136074656001967555302066153124903668084130093285631 53.75, 0.00000000000004680742845034396854191229874806111770121956600923260145545538557 54.0, 0.00000000000003800832953166517224618260477938320424436280206946442199139667172 54.25, 0.00000000000003085737700158328657995399336634740154644852165463388789535645784 54.5, 0.00000000000002504702930430652937574937630818179746853294212154232686496186383 54.75, 0.00000000000002032690205503128040396254371021958891080466747973077187965546226 55.0, 0.00000000000001649319022237750949259335274293824688448970816411592519441542513 55.25, 0.00000000000001338003911929555551436063724456044083043673989965679825009889493 55.5, 0.00000000000001085250687407447300536273818893605667967050176632809738115964042 55.75, 0.000000000000008800826239168257988970934338777781358066491724688823427198473605 56.0, 0.000000000000007135726954517697702950160946949485111388386477751050072397281745 56.25, 0.000000000000005784623547267177178799074219293540004905408916257008486046163892 56.5, 0.000000000000004688509172230436586400541801249445475589801402057784866817393189 56.75, 0.000000000000003799425313792042008228592174781334702789736103685728495914935187 57.0, 0.000000000000003078401058510843987624842982057097933274761302272694648153974666 57.25, 0.000000000000002493775174738606764888369223341568942452997428940913005688175562 57.5, 0.000000000000002019830193102230429818451849771269638909807629923024517770070912 57.75, 0.000000000000001635680718750295595803647495423192363871388885450863941898186173 58.0, 0.000000000000001324368854525638069812679607106260038104448932792853357923396466 58.25, 0.000000000000001072128309132917701914097544251798180562246653559776624017535506 58.5, 0.0000000000000008677858624273062512469970430809079396154446289918072809281274384 58.75, 0.0000000000000007022746529508614456310423556955602106145049815879352239982381467 59.0, 0.0000000000000005682384795470789404299796654859572970213067072073923955170618229 59.25, 0.0000000000000004597101646017484596870305364488416302191665147708956122447320131 59.5, 0.0000000000000003718501709120790007240144696296676355301324896343965740947483949 59.75, 0.0000000000000003007342279591153329801564647338933261440033316296183087612716162 60.0, 0.0000000000000002431808131811719362574585223519562682152430992083391330482036398 60.25, 0.0000000000000001966110369295431784017665368947086604623065281421034513402837002 60.5, 0.0000000000000001589348673747240376008489898047953775870124317269289472531424389 60.75, 0.0000000000000001284587619043277533303868139274994215761442408331942031429617729 61.0, 0.0000000000000001038106920169989979357891016977050468410059296708280121439061126 61.25, 0.00000000000000008387929814552171983834302751132812954184316979760097164977063123 61.5, 0.0000000000000000677645208798070741427671489032461992899493244353432299318137262 61.75, 0.00000000000000005473755152205821964297042926727182265071514638716196255285572013 62.0, 0.00000000000000004420834884830100058228402012124854244829159508428757627171962304 62.25, 0.00000000000000003569929754262041238163823109762212640465999565821198903874959834 62.5, 0.00000000000000002882385101047695473404900593278375190475869643394090652223543916 62.75, 0.00000000000000002326921857404030252729289012207468225405523900095002193880205151 63.0, 0.00000000000000001878233370187050581665766406022203245689310334385375703651764881 63.25, 0.0000000000000000151584835007724213701895963914855868960991330323257458083186962 63.5, 0.00000000000000001223209636740766420746832641943057316627141881111023137538577711 63.75, 0.000000000000000009869279483855504153936248195914923523502278248759410092375818302 64.0, 0.000000000000000007961774833606248654900677399536140022679379373611701086743798715 64.25, 0.000000000000000006422064940375781811193975545119041584680138515715321923077597529 64.5, 0.000000000000000005179410299662312762263693024231069543222114094830910447764513282 64.75, 0.000000000000000004176641685213882995437952256537543820721002146797032836029244171 65.0, 0.000000000000000003367563961453406813620225069865290266079182269581610325493956817 65.25, 0.000000000000000002714855175583366679878577561394404617415453423261461004250959528 65.5, 0.000000000000000002188366724262528626648580403045202899791188405917157543756390113 65.75, 0.000000000000000001763748252384471543325655625625444465704741834916926966476070313 66.0, 0.000000000000000001421335428172338817928971287079900955397287566457576403540781027 66.25, 0.000000000000000001145250485384106590391445186170028536217580621608612878355572443 66.5, 0.0000000000000000009226749464195208453414413280833368658011611780572498325116542166 66.75, 0.0000000000000000007432616590368688479112314296927645448427145387140787752606348768 67.0, 0.0000000000000000005986595348638788226201817883998633776812574368689633961208930377 67.25, 0.0000000000000000004821294464677990214220655266506217997056228000275340973788125281 67.5, 0.000000000000000000388233845902360213507965788866962440796939862734331666264946302 67.75, 0.0000000000000000003125859935344400159977686838440597825238709583003038116220695037 68.0, 0.0000000000000000002516473793698232354047784165964754293848994891069819391317986853 68.25, 0.0000000000000000002025640999416358682809055969290412248225948678522104138548859766 68.5, 0.0000000000000000001630347193393768518214436293520577682698986544085778421441001583 68.75, 0.0000000000000000001312035719953398124869070444118831443717129748580063798529064649 69.0, 0.0000000000000000001055746213399804075657899504008162792566195648748639662321873171 69.25, 0.00000000000000000008494192421139269446840962237272028037271330911054067283449025941 69.5, 0.00000000000000000006833350795665025657730619921665339816416657428342107597915909891 69.75, 0.00000000000000000005496607952737488338104759500069819345365507565696013377835496309 70.0, 0.0000000000000000000442084811207868499733592014849077229213571791025800794499153985 70.25, 0.00000000000000000003555220738469874778548918795404036980255580073978467498887898385 70.5, 0.00000000000000000002858762297821777935590237099050046674252747375314220241048407798 70.75, 0.00000000000000000002298478100560427704450149479259854302006379545844982207431801496 71.0, 0.00000000000000000001847795437633649550404042453294647757845189812815037434081215623 71.25, 0.00000000000000000001485316308809687213917931114444017183458824257726549743751998375 71.5, 0.00000000000000000001193811854906739342085375463405955414232145554259724800284595903 71.75, 0.000000000000000000009594117637275817458144308088081658117794103595148178197038903757 72.0, 0.000000000000000000007709509323171192786087818627129234719984661305440129635661082017 72.25, 0.000000000000000000006194429470770462667212812051571650879230492870869750340912705473 72.5, 0.000000000000000000004976558209640659094872837637147581933514768131647709427320454668 72.75, 0.000000000000000000003997701725063543469397921947097978731208752652626163717627847515 73.0, 0.000000000000000000003211038622209737596669035628721114357579207789169466468546077982 73.25, 0.00000000000000000000257890194046699358549203120245787850269091414076140714404853348 73.5, 0.000000000000000000002070992847501464744255440026176313811341838721973795304942960622 73.75, 0.000000000000000000001662942177766967092337337087808101613575410166715872991708015892 74.0, 0.000000000000000000001335152225082874476406772714672360426166218968694170788354963194 74.25, 0.000000000000000000001071864303348201009750532637295215257119598771217900001080088326 74.5, 0.0000000000000000000008604081590842679244981663768156342213721602088449475297235469464 74.75, 0.0000000000000000000006905978434369803467293029590467678303708917363308059506587333878 75.0, 0.0000000000000000000005542455245080700435601752944178600548544237402622303704879400406 75.25, 0.0000000000000000000004447702623333724205497068274165480973349737103465599045749974215 75.5, 0.0000000000000000000003568832359350034726411825577421100169172028006513303439003391954 75.75, 0.0000000000000000000002863345124463453550618952155610817059209866304509312724510797041 76.0, 0.0000000000000000000002297093500501341452812796709625178781057247469092767579313142493 76.25, 0.000000000000000000000184264364707225208275415544694858496875246827730147786579089008 76.5, 0.0000000000000000000001477957745698330764832494675206151947769874220611928758415510588 76.75, 0.000000000000000000000118533453645882927609747337136476219103588179430404742889137575 77.0, 0.00000000000000000000009505574875262798868911332912780459467479256218676649061625411457 77.25, 0.00000000000000000000007622099836129722640548408372354852582192352299683049870856374981 77.5, 0.00000000000000000000006111248478938021198281005601336212537211299987016542188003629774 77.75, 0.00000000000000000000004899418959128120002798181086191452488219963654733534757418968454 78.0, 0.00000000000000000000003927523595863530950116014046616224178678000081713649893228406868 78.25, 0.00000000000000000000003148131567140937892919270970968380574303576752646552849451781864 78.5, 0.0000000000000000000000252317311456464438190813210546226888320700862428836948227351701 78.75, 0.00000000000000000000002022095112132639114149930312339114559916627688943080391026712856 79.0, 0.00000000000000000000001620379418844329305939106615208384526287401511300011736699617181 79.25, 0.00000000000000000000001298352786334028032302726472137671605786437016457282202924766276 79.5, 0.00000000000000000000001040231051855756238558986169531371388458452998115385389028133228 79.75, 0.000000000000000000000008333515756664344822086856118197976310032708521493582614619613484 80.0, 0.000000000000000000000006675569131786222432883112184961771968895628225468752571207632768 80.25, 0.000000000000000000000005346999753504158556138674940023423791084910334739635806014199938 80.5, 0.000000000000000000000004282467711699704985226753023007640994948260216154426059894193781 80.75, 0.000000000000000000000003429575219167960797125885779206077511467612147564130386387878762 81.0, 0.000000000000000000000002746307120152311438780226063808400691898795899440719497357574748 81.25, 0.000000000000000000000002198976759093297935692179609365769583991672263907588654264812483 81.5, 0.000000000000000000000001760577594663005314697991086665325005738418402865404728776911294 81.75, 0.000000000000000000000001409460546109900167796770996481772942041498907421336198576334431 82.0, 0.000000000000000000000001128272810544776255544067754847099783663190884927628492801842297 82.25, 0.0000000000000000000000009031065459727936635155984921911620123088748007176216753697218154 82.5, 0.0000000000000000000000007228159828064950997100511410868113037036681644168667794504494283 82.75, 0.000000000000000000000000578469694589540547477195959037297389582956511478251760946278059 83.0, 0.0000000000000000000000004629113193811393434024448851496733151978429170741979095076877526 83.25, 0.0000000000000000000000003704072924072433400008373600627821339462026944574522710981438137 83.5, 0.0000000000000000000000002963643819878235754409576370202586256173799149970017322159521382 83.75, 0.0000000000000000000000002371032184250645300394642203330745907965204587607036060717569045 84.0, 0.0000000000000000000000001896767334711611640301888272069755048346059691615002035963942188 84.25, 0.0000000000000000000000001517246179763646331636153331290091953977342230491819046045667517 84.5, 0.000000000000000000000000121356663250929524673775806412744955722894643980325616787691492 84.75, 0.00000000000000000000000009705926265018418276239878155767942417063354017372905988210918762 85.0, 0.0000000000000000000000000776204822723993223544041975465131075902951352602313991646363442 85.25, 0.00000000000000000000000006207001836060901294827085958850511091798662842273823679840225591 85.5, 0.00000000000000000000000004963108813195281697140404903492953775198895030894435376158872332 85.75, 0.00000000000000000000000003968188574774725761536475831260141738198487601186233350862059431 86.0, 0.0000000000000000000000000317247044334753684015998780022362566127565609828945661472011235 86.25, 0.00000000000000000000000002536120220387477576533883151376450815919767536346394851891349965 86.5, 0.00000000000000000000000002027259058506476983315435816656555400508512949443791917160973932 86.75, 0.00000000000000000000000001620376787709463836695251935856056627954658250102051856251585495 87.0, 0.0000000000000000000000000129506126855130596270341279452970552152799117533081821104270588 87.25, 0.00000000000000000000000001034980915592788434450750927253829669633104452610063383705854739 87.5, 0.000000000000000000000000008270700184331799949299426694723935368368808250349835615228374775 87.75, 0.000000000000000000000000006608764957627353544833318377060303991347380283378344232227473915 88.0, 0.00000000000000000000000000528039740540826884774908091733763721885868986155564289860970794 88.25, 0.000000000000000000000000004218726448639541615454609455735596884659096644720414146071074122 88.5, 0.00000000000000000000000000337027046858018740020502319851231643392020862591337418590919183 88.75, 0.00000000000000000000000000269225972100710794835101001229506323844630969937811331186804383 89.0, 0.000000000000000000000000002150493516478868419918305461214603373397389976783430608764978628 89.25, 0.000000000000000000000000001717625505025964784009663436910958655274349851830371642955433202 89.5, 0.000000000000000000000000001371791648784078730582430879781097377643286092197531750711540144 89.75, 0.000000000000000000000000001095512485506420597703393415529564790145081655721996527488946848 90.0, 0.0000000000000000000000000008748149188873949017285455221038218838430328331627342634713879968 90.25, 0.0000000000000000000000000006985296910439369725215405025950120072490092397767241415564486233 90.5, 0.0000000000000000000000000005577294376871870237223438475719076851194835653999471957047068887 90.75, 0.0000000000000000000000000004452792297851388420999956447362497574848985797624576154311331787 91.0, 0.0000000000000000000000000003554771133439440161543859449437302408289914267780769303832219124 91.25, 0.0000000000000000000000000002837666489827278675870784615651154105042631861385803679429228378 91.5, 0.0000000000000000000000000002265070477364418609763125500415771987689486729245532128740770617 91.75, 0.0000000000000000000000000001807893772623414653801104453983266903960470447066946513570103092 92.0, 0.0000000000000000000000000001442896161706557292197305735668007003211351509426456611713612688 92.25, 0.0000000000000000000000000001151511779627665865247576830021496393451307370483121744411836972 92.5, 0.00000000000000000000000000009189100168847357084268402594528496810111309484767175204343313913 92.75, 0.00000000000000000000000000007332448733277105296893605359688342182187306327857858103048442487 93.0, 0.00000000000000000000000000005850549890048897352684868317873818143544175602096485205035090792 93.25, 0.00000000000000000000000000004667841426237576019912803820082731795778783290504551228288595183 93.5, 0.00000000000000000000000000003723980575602630156243878586334707527186842862996816432610454187 93.75, 0.00000000000000000000000000002970781948248687053912677944739560889017022384588562284744450134 94.0, 0.00000000000000000000000000002369770836888358168003004364013206396401307400305915006824985717 94.25, 0.00000000000000000000000000001890228372341982357248772811545170207931801348593020568457045052 94.5, 0.00000000000000000000000000001507629767603030879523419872227437012574732784729682582048544987 94.75, 0.000000000000000000000000000012023966969540806574036091518216173097842966884505824747926881 95.0, 0.000000000000000000000000000009589006961359774604241630632037000709173106294742407762963241087 95.25, 0.000000000000000000000000000007646671352301346964951898290669226233676564340053512552455441578 95.5, 0.000000000000000000000000000006097394428872855409494559215248982583702189155390174557350641038 95.75, 0.000000000000000000000000000004861713570479017221680840623438948686148900228627036703187933578 96.0, 0.000000000000000000000000000003876214499596345994459098861269874837414461223307112370537079015 96.25, 0.00000000000000000000000000000309029349392199938360847481577737664924069988969325312206136868 96.5, 0.000000000000000000000000000002463572136875100468308970184419635373192943251935832069683535429 96.75, 0.000000000000000000000000000001963833237941649402265929834775349885846123341061554460625041923 97.0, 0.000000000000000000000000000001565372971851820853471246244928190945004194395125859127453025923 97.25, 0.000000000000000000000000000001247685396745217373099355855416076488576198101860536413391732999 97.5, 0.0000000000000000000000000000009944123809136899308758273261393344392928070887660999828663989917 97.75, 0.0000000000000000000000000000007925054466809206753216007682905703514010410724363080738183824396 98.0, 0.0000000000000000000000000000006315568091165255885432290150241847205710852917167857667157345977 98.25, 0.0000000000000000000000000000005032654907497211052151797939790403806031460399426173375102731491 98.5, 0.0000000000000000000000000000004010112662604230310911337199040741125889696719840157858674221541 98.75, 0.0000000000000000000000000000003195146810221878323058829989671913913538353524921562463965600557 99.0, 0.0000000000000000000000000000002545657723009798986676184210444842046631939060297478921005046057 99.25, 0.0000000000000000000000000000002028076240261561164742824566055073505083589005916501235019707843 99.5, 0.0000000000000000000000000000001615636828842084765525989680919135433651860855793297821254053638 99.75, 0.0000000000000000000000000000001286999969175980384388475022660608802667298459271859076410322999 100.0, 0.0000000000000000000000000000001025153212086870580621609293392614180268654181100303672597170932 ././@LongLink100644 0 0 161 12126630646 10257 Lustar 0 0 commons-math3-3.2-src/src/test/resources/org/apache/commons/math3/distribution/gamma-distribution-shape-1000.csvcommons-math3-3.2-src/src/test/resources/org/apache/commons/math3/distribution/gamma-distribution-sh100644 1750 1750 5240611 12126627677 32622 0ustarlucluc 0 0 # # Licensed to the Apache Software Foundation (ASF) under one or more # contributor license agreements. See the NOTICE file distributed with # this work for additional information regarding copyright ownership. # The ASF licenses this file to You 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. 0.03125, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005457022365340747531144703069166332243737626465083128193302885601 0.0625, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002833672787037245923796699676380369862430887877883756225743140728 0.09375, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002259153776402399107644181694804673261411648001988503456720517562 0.125, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000142617218709928525301419106191463276140979827980209555799791213 0.15625, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000898883182542818202004274823379549223847776452605214055626427197 0.1875, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001102037452538991574030852892271340649169536140084921902544370485 0.21875, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008099673125173748881767618802988805731909195366372584342876789319 0.25, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006742963685255220037508832830445998931159947197274131714816438902 0.28125, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008253706913018735974132017653288672699894592286976030986748093185 0.3125, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004119176590298921254732436975011040155739995341746082199514143453 0.34375, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008964605471326193428571226397921642498594843921962811951478060085 0.375, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004894764562586577721173971401412778169222625704803487284895715164 0.40625, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002532236459542001882465939848632291112821853399854552453954917648 0.4375, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003486833766228765238436349273257863640860241102147717548209184263 0.46875, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002898142225359822154952929091275541148091077491048233336828375442 0.5, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000281347375713687423166424051507029492690129609188899634931201216 0.53125, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005473699477565016933799072825275029353623339042858802323524086425 0.5625, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003337869771002700169870633947396939390969171236929468680590594344 0.59375, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000009279240683405450486335843674795195273290919691821067417139178955 0.625, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001614578084961229217838430018985951504570403361653742595303823516 0.65625, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002304608300334450002938681598108086118538352744797190662299711229 0.6875, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003405713582312759218419253237853027282229140028353919672228818027 0.71875, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006375092036142686248935722092048609037514357433002973720405698088 0.75, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001802341342453249432301967555886732787257087503236704524738351872 0.78125, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008980562417002141304247715067587147169349837870042025923446322635 0.8125, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000009037281132954115520864718630022639868957216223706290014520050311 0.84375, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002072487224063305680676834281863217444017581303456609915347546277 0.875, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001206127099798037667082432662539358225440488265174142017696304283 0.90625, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001961317769782772928373877185178577673620122401169102002180705941 0.9375, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000009716499316124293917123199015229564020325450094702204188384956749 0.96875, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001585392492793593776772178546746343169485430561496287738890198837 1.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000009142422677620557252249857062822988618813064170025445013983874489 1.03125, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001986493689769196014080938756921469519067199337298682152168302568 1.0625, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001723961943253982546521846698869866913699015022016287341942894295 1.09375, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006302228605633058324261216076614032002704685469223176892251850375 1.125, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001018930144173041075198421614256428537342542195732794594064532817 1.15625, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000007618979139798264068062188110314552044361429945522877936525441406 1.1875, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002745464036882610433137749317892127124938784331196035912788892862 1.21875, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004951918130547047274425025002916362532903851457256794361930415813 1.25, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004630103194196369492188988896575362142650491631818459101774403727 1.28125, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002318251205864842405601516602595427165074779035489756745864908438 1.3125, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006405559008291155034584240281010312242667623597728747648139962684 1.34375, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001004457001266036928366752493930122788750959390959977308506015118 1.375, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000009174794242263094972288649473889207436864393491589692277188629295 1.40625, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005001520839297170012608706977703209596637063484161922124417500037 1.4375, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001664573469533679163896561913702258190135062179036864819558389664 1.46875, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003454756906931854050101421340701698904156627684717488743028686204 1.5, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004561228734934971050221278684474626985766575292567621859629269424 1.53125, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003902994680340485179524156334999818704400694640911190372002684827 1.5625, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002202807974326775272653487309571990906068620530688181962697252938 1.59375, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008336361276784269091859339526945418516012532601076680393579675769 1.625, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002148518978821122376538295659924799586085707980387951015859499465 1.65625, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003826669296596240186460919137624087332896791834319180223708728386 1.6875, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004775529819185559851777552144515990452982693481947781734690151942 1.71875, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004230684119040612501715724699015437331175162031903349055170168494 1.75, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002693711448953571844926453162215506769609465149437447518360318614 1.78125, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001247157225592318582934873519556444548015951038170295428717810086 1.8125, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004245552905904032592123598652679843703712068530937495589570603212 1.84375, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001073882130590924785723801289407576510417041831714926343752465049 1.875, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002038564307209420691902855253667172928038594385761602218984765581 1.90625, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002931964826161997497157621740926499020655860296646040124018463343 1.9375, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003223886074635620813806182122257576573375411931864343803879862548 1.96875, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002733500005882492068235288286738140247257120474380177502966678369 2.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001801907456218155222569191949386248085032442210464998061339607336 2.03125, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000009307007333724074804585424149293279475299388330256318061911315875 2.0625, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003794780617521757661894745784769090315214557319208339232840728386 2.09375, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001230130716235148575911515870411115797327075496228377711116873057 2.125, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003191945413766026740622745603965439349218570125442021710126526917 2.15625, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006673007395934453070077418458834748214496025174664873545526478786 2.1875, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001130967776926889181963154060925709940493648282635706897834116968 2.21875, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001563244555963562738279171477865013874912523237999447755650629727 2.25, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001772265514336214640280685203752586574606114535185891767029646423 2.28125, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000165703014697523894827911567653948026173121149842165449596473083 2.3125, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001284427094078169416738375375181341026536772335705856785977118653 2.34375, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008295677736999867847967595147707734390565321425566987307239813397 2.375, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004485973773991128606245291023190298507491186059387334283861215286 2.40625, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002040519674286159792086694805095872779262450648505140322065638802 2.4375, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000007842286865164152868108226506088092132380648222342124830428044449 2.46875, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002557567668913334049507182983530073230482667235460439871209901873 2.5, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000007107031436747112528719275065830626556182549742786299091030183689 2.53125, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001689481298363867981025346368970204551004565459259151867063441863 2.5625, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003448945706533915912961254652150867811102759652026335929532853354 2.59375, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006068626476803889025177431864508262747333825809793703645424257762 2.625, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000009236580021280183447998107703392428463853028251728230131572805952 2.65625, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001220223844439802070248158956642061668058373807638159867191451615 2.6875, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001403827703013644919532486128691176632693118462285555405987523739 2.71875, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001410985918651231725878809730947969756593692256896619872189155636 2.75, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001242816724992433214652592775586947147992945147723660306066442901 2.78125, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000009621937836128556051554974512915273701078939482218943842635859517 2.8125, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006566608671040102148589627765623807582353253582136543036039808324 2.84375, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003961443189409482803308638147812044757431408699830156251467122382 2.875, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002118216365073297027539166041941248985615658207763357020589706781 2.90625, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001006525318196881350577543262987058196708858210491277276323265401 2.9375, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004261015772589298636601456336457114564694709804758362256271105553 2.96875, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001611006811594798494571575059920801345276140808355722615968325043 3.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005452627092288982287835591779402559177515974652959017841437662828 3.03125, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001655902721789098381124240104018847545103725073570732267674790952 3.0625, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004522204546201788821294033419441754704519966234585656043999842539 3.09375, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001112982372103614450128286843328539583305269135878138546497037847 3.125, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002473757821325286661660408143401536763001512805739392919352797511 3.15625, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004975513244418270077940552172567592930431324860343063109341911582 3.1875, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000009073719829578760104888112541539388152881912464413389512836505174 3.21875, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001503244256243987725471485465164353028299008702921203857029346776 3.25, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002266607542283682932315225907654420169232516297675218722647783621 3.28125, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003116078848376914611556632730981349139497967550162242934901525682 3.3125, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003912788419755095932011707324428276214612336298477378248071373417 3.34375, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004495201561433684930347779340605470933248074268049169897082714701 3.375, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004732768821894098834577232526135462242506800381845907339984890224 3.40625, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004573864557124008350258486166103543377173003873401360656607277269 3.4375, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000040638028241623118294779104639048700318746329278111044686817337 3.46875, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000332448409890132958188270564383839473096876594090604758875496782 3.5, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000250784907780405513445219893889189536625862306239014420665547699 3.53125, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001746988664359133320067590247272397950477838508442745762721182329 3.5625, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001125381650477774096298457763384900451176470556369144125705833083 3.59375, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006713116054287076114868227348443289733950306214423285228705028955 3.625, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003713138742546544398159799369297909931286104405605684037489510172 3.65625, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001906839418862675143580826533206837840357568235540331710685845204 3.6875, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000009103152810187798719745161427341518438872984434954303459651319343 3.71875, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004044913724745374952760844511899686365951802823537448533737352012 3.75, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001674896329103377636836680619134616831572312446090218904047782814 3.78125, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006470478116208379069606254570110538322706838626704028166760684422 3.8125, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002334804749554217052690929893541196002204386518991603429005836577 3.84375, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000007877970485765795201992318018593112982291069516934604659599553114 3.875, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002488283134743383031494999745338804109590817969522524755977719073 3.90625, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000007364912837290876501660848425386498827731788683484618843936033952 3.9375, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002044878393972600613466878608908836199173760778233794689428339292 3.96875, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005331365364394838877201245582122643846285306354244066499205185376 4.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001306499316584080620645304606962713114502759304439910751968511611 4.03125, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003012301201753345997235550486283358413378205247977758618164486316 4.0625, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006540561868243553505804363671234915503563130481407963656705543928 4.09375, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001338624474539954221965758301254142905360466737696975836938773938 4.125, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002584758505312115182225048564175638325101852894734855297769944717 4.15625, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004712814240188193120794949306563605676391423798882593804998743339 4.1875, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008121062629109688847790371511909315001647553370966948459815158163 4.21875, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001323677897812473876796173703763905880308075253174554707940476134 4.25, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002042421389589956630048011404572459575987502766484280905939520374 4.28125, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002985731670820068879559973088357070746998319225209039269865314306 4.3125, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004138469872187430586000983013058326513426069520476548564531259725 4.34375, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005443099011841785542228527840292689827110514729781755977482424782 4.375, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006798243568811370543860093721491018317096556146503490715881256947 4.40625, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008068836550824116753874761501234771427757712777434858147505593564 4.4375, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000009107553089708625104092247526148428154890061145258384372259797476 4.46875, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000009783075335687096167168169147952896735501786669745726807694472724 4.5, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001000764452965367673683580152664975442643585147392438478036803587 4.53125, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000009755842893640667307743466134425951624243507993563573392004767312 4.5625, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000090690498949443629540247745551402306913004109256520257167186247 4.59375, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008044602201127862050780864965276000830906572926727483188015398536 4.625, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006813482378295768571549353568000803040050951640874844365248046042 4.65625, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000551348120933953592249727254336045827856864571719681311242676341 4.6875, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004265204404223213813850029837795146277866838307722854395023363985 4.71875, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003156244914282811028535610270428740717510042730184200009626434192 4.75, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000223549140311193883239491563166408741069549552514962985667183247 4.78125, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001516339092034802715304162806278546695937365796677923010565590881 4.8125, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000009855651942358325471192568562059269376556213371096118470081863133 4.84375, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006141578562634066087697254955558018774113081302360074542097300722 4.875, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003671263809266428862651006534386559858729072723632082158204252791 4.90625, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002106312917906509365722626322450669113516516891867537507446727599 4.9375, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001160454987330584659109703925677801290853586623037497861719205347 4.96875, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006142623903727619005631045365126038543535756552835705608326969942 5.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003125487022846911159978195340780803690022722526405083723026612662 5.03125, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001529443647606231071840818097960490228238726962483735756412471087 5.0625, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000007201304080728995872699739112294201499889675710930922145575659016 5.09375, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003264049367278901011645362076343346498967520804969810323542166543 5.125, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001424860726568943523875826193516169252514115015384297947035087571 5.15625, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005993172577180875523030386428772243357109575057482632677378077232 5.1875, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002429991550158323395213666083738689097316370650677529868486430688 5.21875, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000009501841578694973564707284967676673314770189209549968430015217063 5.25, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003584708539491030988455605841160363635252285950054351592909152476 5.28125, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001305351921486097919506156393916797033656614109058753458743682347 5.3125, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004589976458212975356611635306415322675284537227121966869996409869 5.34375, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001559123725617697989852267765725951581512982761703597167255102369 5.375, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005118150552534231204455860789501235477307642835226957720823756261 5.40625, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000162435114595757060900735544820194000347789001937321676136529267 5.4375, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004985976477059013004910059826282694983323663881262143648056719586 5.46875, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001480778588244310461473978272575630525014030387083565751386414826 5.5, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004256600896331688272672954217840198298352660721523746503321651358 5.53125, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001184756714489058560311297524147159597542853449645953549627366484 5.5625, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003194086603409826582316665018746106795947601555295930973565475342 5.59375, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008343929520740220227576004613546999771185025968508753550710721919 5.625, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002112776578445410733917178576092983120643543908541212627257667613 5.65625, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005187350187588847990426220309741410349521419111180693582864010958 5.6875, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001235361798978224730348508097536327134933102051548532862307376435 5.71875, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002854595051482937962251444230577085372427745396551515938970549292 5.75, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006402349110255556698780880805347608196908409309232962318317228914 5.78125, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001394180927913352652792783393352232630998560576473458770709499888 5.8125, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002948641909353637768792710667766029230603598580689349305764267072 5.84375, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006058763321490537123694830821684661951929473015178311306281599167 5.875, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001209870171171410619822125433429754959149835707870207458656801202 5.90625, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002348648482241441446747332184413920787880127619029338497648117592 5.9375, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004433546847675180286290580616000804881138157101443774099004341968 5.96875, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008140782053752896594205880420961005146349044759931062923297831565 6.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001454413910742854175155233310834354804611646812271239557946262666 6.03125, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002528951735857387393659942053501362013990091330934011028401521592 6.0625, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004281001106037391998328513657306283535690378845351954226464974908 6.09375, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000007057033650917769189730093752076721192049537604406681315057424426 6.125, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001133154218058147548227268813656205626231675383324685424501240088 6.15625, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001772809080267501829359090253950506703205913684211739251625432037 6.1875, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002703058225565038924315732035855628999338789141443876446884249874 6.21875, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004017741215777451961797362149528634421963686186679490664604901246 6.25, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005823077801440043496539230282631577830077455613941290156620950213 6.28125, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008231454556134458121563027779250498956080190297931123699238103636 6.3125, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001135171806868677598431058912188974119159625766496806959850147665 6.34375, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000152761421858960304146638704929846787829548519105900385338334874 6.375, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002006491723276591851726242127533774414505623207039306933316203759 6.40625, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002572975235947793420319648585816220190432591616068778402011119688 6.4375, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003221883686124471582939940918371527555629169229017843013729951101 6.46875, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003940579719944436102108870472873712230254432712377086745797761912 6.5, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000470852555349842087267540444767510827632125616586139716846700177 6.53125, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005497704922293393112747910269755648598515866862984615423010703836 6.5625, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006274011898292979430958486585896498975015203534264007728805859849 6.59375, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006999564802578117564878143102936890255818459243571905137552740935 6.625, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000007635747550077534934097431557515813442056024395544520696977532308 6.65625, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008146641897384564940429284487404635829833529950710823466662977961 6.6875, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008502422155190947995618621590422897299560716672807024917817625521 6.71875, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008682261090568929313105375395263759555801028315789289550141879414 6.75, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008676350405101427772485232813086018169259354663156221190850382249 6.78125, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008486763415229226021191693082483343069087802162922025326937390103 6.8125, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000812705856405671777114275103200312515627300135785390898469099418 6.84375, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000007620707645509843225395291751922763276577201816319968514287876068 6.875, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006998597456258666935939252313000063200307043229987536914261849079 6.90625, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006295969015640460634485665304877084615982375092102937593712323098 6.9375, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005549207240521207853077905331586249318566798278742338256106920439 6.96875, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000479287332314399090952586302822031064727488567940534972328150717 7.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004057293066305022569562346625424257236726992541197718616121204149 7.03125, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003366897953417543553057169994420128088896992176464512008641885374 7.0625, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002739386846379386083009552017819236193832623017657358264975560895 7.09375, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002185658424468407784003463222552824178766864233049810288467372376 7.125, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001710375260734465829733556833489956415442873223621313138778746617 7.15625, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001312968804870572077265137552468933481986313382377581602248314309 7.1875, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000009888809922159183051632035670948291056820704546433215748969941252 7.21875, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000007308564049260783711234692440900088059163887136496195834594081906 7.25, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005301385043392977018840545866962409662483222939971344738162956593 7.28125, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003774729584396736629382246789613438215692901176472697815053200781 7.3125, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002638703481630838580161962743243963835501995307723691833291691702 7.34375, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001811222248446250846138977679006415641697083941098432672749382256 7.375, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001220946401986517929982997472329861236638246611541639350159756763 7.40625, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008084098449364792237698541186695584843684192017981226411571105068 7.4375, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005258263270849668074042110630590431903807419852457045419895636791 7.46875, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003360420084713971991405934455465883870867152831243068756648017112 7.5, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002110324747587916894982208600620388618903456699884662179379970593 7.53125, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001302484763882040910585650818072623022683756979254899927757590137 7.5625, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000007901799966276107181490468529757748698265352980997632620767845394 7.59375, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004712713505916160648981806082680283728885465472955769474538247408 7.625, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002763557620859414474620186303060568024246839550268532531632560925 7.65625, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001593597315093836609102894091462546834735400344725123888792845262 7.6875, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000009037755808344803355074985791761905516651201321259420499686222371 7.71875, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005041656008479808232204232261417618404587569873426074960635174415 7.75, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002766778211429592701664665604435349893800728430333766643061908518 7.78125, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001493899038044334756117466744117359341049550846974570677064448955 7.8125, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000007937257315218708359512495773198493373168578008502506867129274981 7.84375, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004150284356652700407423490253759329866476541626915298136728690651 7.875, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002135987132228652594370065870032060090628991454987013357521251167 7.90625, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001082149699981116821159171329655245492994482725317588881348315322 7.9375, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005397565105358697351685322928476408695785759532430208437526682429 7.96875, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002650840133405083701955647607399869779184265450076298122237918294 8.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001282026279418792249860918301531752512152481669105449572579917003 8.03125, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006106468012243643537412637741040494597793624061705648029812372777 8.0625, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002864932757861167191570047142450340061424180653160700301301974842 8.09375, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001324100015196015754874815379353839341610592047062584348568155451 8.125, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006029195879439735032800113317689116503436517270716352380603469927 8.15625, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002705078665497648991938849635466780497563218206163983253272535492 8.1875, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001196000590631514595764804463719011090321588342889206684775497464 8.21875, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005211495947680518094605849321474530280227293082394293390818929924 8.25, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002238313445633581873431109801933335236069051679350787027520906122 8.28125, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000009476638647015776725323502285375876739105904541026408983403235028 8.3125, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003955574330498028959592918121153768979793101647477006533325200053 8.34375, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000162791961516747113049454055845244118290106963378087646346156987 8.375, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006606483963129814172956214845608499002336057682415751398346171936 8.40625, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002644034819977312501955779197235198125903913753287292844009632116 8.4375, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001043681871758014696334882391646849769489334010518621632757135005 8.46875, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000406366245273299505678902739744006175197838663758325290627125795 8.5, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000156084389577830492542356968318299178854931891405270661193835733 8.53125, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005914758693490297846114679162666464432192664845369584135092882342 8.5625, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002211531820398138440733649316011647293835077528039072305634569528 8.59375, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008159628174239737917638058166502544198095792873024872135106944837 8.625, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002971053778899379972980037205578013897288414491853149392549926041 8.65625, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001067714455820090434487961087272512356438367884677997417331490923 8.6875, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003787435511890265985008851723357344767102256595932025578064764757 8.71875, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001326238076788371659482493556865510417322351143965932382903506045 8.75, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004584838375043466478283060308488383542950714075496971222783049361 8.78125, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001564921662176897947770944916052244840214773163254447307767074381 8.8125, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005274320608085653395011219826516516071261897455614479904989827304 8.84375, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001755434788241585517022878144587727114963576182060691021458119827 8.875, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005770130246288816361053618425907410410791047929563088744758182874 8.90625, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001873300095806137769980377827884428020506447064706870797131905802 8.9375, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006007414523832595406571069095594652400573647095263286354007028366 8.96875, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001903108894319411703131022024700862339448504213314467134689670911 9.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005956242493101042383164967096269957541897955650510647249532480506 9.03125, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001841833256386768729766417761625380593693966345720274908148923876 9.0625, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005627734429808407596835924761598887520066647943129449697518178188 9.09375, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001699252777215645094744217889226075116660277776263716820508824457 9.125, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005070594556828571910673863365178890897892641801209522556287609973 9.15625, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001495448013255547222993724931421072657787968962736044998797774187 9.1875, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004359432579897267073277542006761484732593956475792669371131001836 9.21875, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001256229950397492954833443574461067915773911162329569856385497046 9.25, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003578679673412781400565152155656128833744716236641273727193246078 9.28125, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001007916706959226580543564427348306420903016249398691372827525726 9.3125, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002806776849222178102776280200288728466071101460372202403276120812 9.34375, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000007728683222112120940135541388331607066161198086325442606945241618 9.375, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002104505830932213568006605189611206916158068600960553177286060905 9.40625, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005667272092362290387592531422967491653042269804776545692504572972 9.4375, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001509417140875379871987747020369200548273504433414829394220403012 9.46875, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000397637591463922496132150229517674505260244777042048663428940118 9.5, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001036191161075152832332514042998261701400164082594748300756620756 9.53125, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002671146225480302964072695092022504920423050878241567825866166597 9.5625, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006812264597200720998951427916594205096875639547831911967642838933 9.59375, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001718904910379183081567629662167713307050886881501405612300275032 9.625, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004291497184207116059265793728106613774260418298659741619888117214 9.65625, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001060210963299186602105101439671373679000676888527212301060823104 9.6875, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000259198081844941681502394679855319057969172456643852930134230685 9.71875, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006271285581322541007642767188186729821721629307089506098324848426 9.75, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001501743158647688850913122885486654939757927298369516138512305523 9.78125, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003559407769336751815053446628461599226792475328108461963919899974 9.8125, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008350861054413012797748373314128071646900629477243606383499870715 9.84375, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001939475803747144337250236391347272753581210502232907542553074695 9.875, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004459282264471123104370310059820592933481454749993472232217016507 9.90625, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001015080957019318031067769042827810091402054881419170733131261407 9.9375, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002287804378425687159873954755735319542036703775759767606666558786 9.96875, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005105598907655299621457881242368437701269976722770993488745089363 10.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001128264591522769235884060681451006551539177881174425706963397584 11.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000009319850588394543553569197998866094942402458850158754393199872382 12.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001931464794914881930671437967263287954615954813676317517051524993 13.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003792594995623697769962288543403443324652792694880384196194633412 14.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001982168972828213442172154764627900371091959689995757270982928203 15.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006253257979235280820456319055923051119919932478040183488219569312 16.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002304128738156873913563144586671011839800453086430408959531475853 17.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001701461170551009879675717969239192384529524668587165147927963398 18.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003938108721870315518024540393435779209445627088297621058940289278 19.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004155353481170164725429715387277870098237250639056772125383402553 20.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002744301406004916137736428069327757868715006561902234451735179712 21.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001486781323852525269278398299842619727588013738025306118198064526 22.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008339409832926201760415444766094724682482923265510326041112455648 23.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005925034393442893048149752531817426493337594478598044312054504954 24.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000635797304086895767303968270684713627482444746819766913992745031 25.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001202437172379286494231083173685925452060705918781891257696728702 26.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000459276126670733389400564382737748843720333345092774093153291191 27.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003997653617234423527551918285542146699728062538974047306869902829 28.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008830465445905487941517705156700735727057748370262328746598326268 29.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005450240966069061076995488334581221180677110890927284989711161657 30.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001024836974579885378729995341820404228603408129937283499609839318 31.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006346860885892350864401116151183757272989115851408713041297913663 32.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000138918696166476308508592584773146013929456006828779838866683004 33.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001145681269781620249956434402154449393328895804045700517691201301 34.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003773822924925360877022599481030745184665105170677280226490973251 35.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005236308401705713325379907839488550030539275836874909023139288384 36.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003213310520062567843459324651856347257365391162258228042870098814 37.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000091197365021011379442534922369296162192934149614860351333932039 38.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001247321722363271493119573631112765225794554202334137291024577631 39.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008539125630940018075183575031875215891534506843388350809882303233 40.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003030454992514395853518128328184306671899913968523346685436559618 41.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005759103490794243321545923061600080968096756818800320067076417568 42.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006039884662644090774315795919679245333724040913220269364362622077 43.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003594845087044629205004597927631927201589141357347460357686729764 44.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001246299393927610666219719673124016506214042041933597874664328263 45.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002578726918768106707732730516639827646811117448259416174637206467 46.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003257492768051960595262672489233126994452680660371821421769024659 47.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002566109628342659057175109894870325793250884098485442751457484054 48.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001285928390558858847871676054887672055422368944350120057522390545 49.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004176477182898822046500125232120824950266957270745870537545435716 50.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008946764676165290397272871385353213413971075787960827124480240566 51.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001285118343447227145310320523402569423498425316484666019870624447 52.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001257138161770511848862440646248826938684744224744162225515390995 53.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008498493922697574551216533769972411852288611145939891639427839181 54.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004025499881900671314165417137898684610200076623381719232048979756 55.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001353586698409042614617991281581025116020420064992931389772023752 56.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003271174939907404252350935072378243766865040128264293407566786545 57.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005748454870686439835471162251061415642906242214484243323392400129 58.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000007427478388984509455413020242222581149533964955974281253066609064 59.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000713084466492183691029949169077630298943451919141773355368371905 60.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005137904136896520941404978992054778674401948883269354389554675396 61.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002804771963249395344375532678729859251355854041020598796700612553 62.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001170565469238450498547780102173183708292356527098797936617529017 63.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003767144872249816790329028843739519061698064379109482479140287505 64.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000942546849162588734892281538586938965752172665828316820578019965 65.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001847811817918898715011613181098425244881440226140162034683539925 66.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000028596414178507101540388232182562872880644825961698857224081044 67.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003518466241507636800436142037599322139409975172919356494710517205 68.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003465251833425996000101563022505783276856242908486017412285789574 69.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000274965479021396292001310303943526770720080099329398159417644229 70.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001768821542504997744138657630508764017156295301255583207166716265 71.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000009279785401060658795710135968084453197936401707665199736801017317 72.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003993162791399039307739974675719232060573666421466450267920890377 73.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001417084863803921622583965731790703810893732954441605705814422161 74.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004169194232143809000021736460032526040182046193752245087402463944 75.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001022049107769283890403255369100383041438352855337595197649425433 76.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000209775054083010355201772830295262703624692625444199038110186403 77.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003621722061775956503184953985349665680632656050020555703115025146 78.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005283166249735260333195272767036108923191849755299970579775150983 79.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006539671096709259330284966608293204236145562268284802597784822825 80.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000689754004531237890863711998353879585839562779353829186190479639 81.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006223527364751850198370323226565709419630333293279840494571140627 82.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004822217481666590363069334830504513869903540570797008451121180613 83.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003220535298620738359564393774386499494924730074440786132612837834 84.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001860484575479978538056482872495824146661695704707369137401598288 85.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000009328925066479243331675988487072789889831519910294202829192311588 86.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004073644503111892967071864955672702473808224207912998431414915924 87.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001554065138584944368472924730799884023203589556574611190431864421 88.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000519554186187436484238040979553119979610644673764454140704184672 89.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001526734525244779647298364829521643080733211083013762997579680522 90.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003954752747661690082930139415517991149820165470870887105915173348 91.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000009055425322960176434219355935294585704125658517610257800120666405 92.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001837819947399764450407925229104981799137376009635212287988162672 93.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003314626256407442220268225433282587316515395218232047200293423557 94.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005325988508528007513159280195830954133748158432001497050691150534 95.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000007642961653186895581154738346916129837089599788601280683561235154 96.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000009818549222676154718590095171321784868127730815795039415217157673 97.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001131757979613391556897708316758525969617555942548624224271043146 98.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001173130073403227731879598854468755682831543024111269279297001564 99.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001095876142660211757561012773422456386349476551750297236093980455 100.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000009245014306139073398183433523646896786367819126375472281715702093 101.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000007057737711028563231811384063174298244190489071035479321750575362 102.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004885288476171575109291692872486896285089030241744312968319612336 103.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003071932172134242319247272987180031609591204636271592216285076028 104.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000175806779900677240226559498455677431435899050859278228936649038 105.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000009173708771265270823343344478166104507907904131150197510427954497 106.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004372200762295638842863507356811309310689654881211954192817899579 107.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000190651453992098215843007770960679170455722486712776797325172141 108.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000007618745453187425512797903395350157986215764008830687088632575328 109.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002794658160796170988091153535194829617389260164110587021490451666 110.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000009424433222902697307894382306711709497878643807023105420770297608 111.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002926335355929796202282488486094654265627822121063160074876828746 112.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008378734948025752098912338215864509922115533094921499469733079373 113.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002215360269359463998153833735234311733949459864156948077511225507 114.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005416656881346379200377271938460924500320015696831369651540093365 115.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001226402062585214026202051943373571081796540588070368103117365405 116.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002574702856142607569528590594218106651684769884247188098497249496 117.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005018538724052462832766112389736727926629519572613944087765776853 118.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000009093523890255765548792782812711050916503415122261854594866586215 119.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001533652131406995851250788768433280639055592421370492280354150537 120.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002410364659054915692566192695952794887378546016634061697079483135 121.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003534340714761274884076304267804728176275434772615723027072990239 122.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004840607058447353033463160889740094556952815071082932286209425432 123.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006199273995546654433112784973669597048879815336474103127924835889 124.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000007431962316847059717327938586935709837855383395784714762281931825 125.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008349270566083302125983637224491587063304551474333798161373555251 126.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008798844255327635550483065150776365276324404341065649760237670666 127.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008707105590557252997581917150940372415776867096372558119903577478 128.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008098820480989585734329949633771405916346165406502739840077530486 129.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000007087420514280610450145577569121563972920902913168410733495150433 130.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005840931122462352371065943961771598170010907477417936404145808407 131.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004537357602595086062743325527445903129090379002061112015507655238 132.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003325381231422719678411725098265313000657649116264588806035639635 133.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000230133059303304559137983697047928737656837748988788615601354895 134.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001505181144593377080970471543521019408497420984084653486178316571 135.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000009311838762881012111529029863621201281506993734459486582049618624 136.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005453504814420673767247007377695596396971787060855396723606206187 137.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003025926302158490794737866219930552548459655514090851886768218932 138.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001591932283402798686802343314429721876182854088009999771258439009 139.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000007947092118454339840175258812682956462370703782541173490342364114 140.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003767349864821053923683516841248068632133914355272044323362117009 141.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000169717791687439012986581847276044959121113932483687225003940952 142.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000007271020787601528517543474102541405293790238215022731910712160295 143.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002964464768545456783020218450841431105031105505690394508454720481 144.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001151012359158110054320199802260047746553557903352418006459051386 145.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004258828332705536999592623916577733479202802887629377426207482597 146.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001502672534836030325399119856240219253399692993088466278405484399 147.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005059229161214365099406022543294528086837906873434023577690547135 148.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001626395394192646360769014114117790714045268654056580391360675292 149.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004995283237012163432285620859875382017241757915819450528669783026 150.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001466733762344947208706574767181922058134851757020698745154051173 151.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004119640854092136650807046929346012699398522730138772019545663422 152.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001107487633310228168069556153056991315992132593846968021764254376 153.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002851277148511034228588024082608158711494005322341059715875442683 154.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000007034053714086634463739962131841730671712128194763210721003029866 155.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001663709724053597787746179263820625329618671878086167532627844344 156.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003774768802575026194466545066992230267909820778083892395672382421 157.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008220057886188140327534969700104478916898315712718061800197083093 158.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000171892728226146096769965102403500033461273559934790951103371763 159.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003453507085529265844384408391621504294756002603642094940775007006 160.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006669623928696218378724395354066107243778617093589564800671455815 161.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001238780462568590097345677222519714942797363870682190189348590342 162.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000221385533119692213960189662028703304857261180309659856184264185 163.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003808658442254687020919115673912969003584563658906111633498828545 164.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006310517063311119284236996441712002444004102617178243940258020269 165.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001007457034218183786441833447317031670222645871882073447631199744 166.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001550428467708089358309264265193704737423511803013336920372972434 167.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002301081370829567376365700894020373528417326503517929746846228394 168.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003294998811325479375869337181743817277474943760411108314590122744 169.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004554138616162757862328230789577165381249441990482016738194422931 170.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006078078589585777236687720749971058852418801029171940392106956938 171.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000007836346918826965836208467236110176490102584824368411569653496651 172.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000009763900637201150691991626986698037183403363646280458164730075184 173.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001176162786386056601822504049386478822308544905906750106832265662 174.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001370297801724892575797312983272109728590427294840988164221639931 175.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001544657090821926310879207315404742259864716019532519525370559821 176.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001685318969949338003021961610171756995495393877447953623426779176 177.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001780432758871996970283557446624584676263898486754828060427509438 178.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001821882254190763461527210922456017463585440437146102318045013973 179.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001806431368382481849793122151433437868715319454592499979207449731 180.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000173612756870738866168309893386837538689758685037463552213301366 181.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001617896917966987814174459199036822316159829155791069939164360213 182.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001462435323998460590735052747118139530381136103734252225802285063 183.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001282638377735147925194267634767652968321026610500804686865270656 184.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001091883477366447652527123379037089085659746235054199227408124063 185.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000009024710939093385070268035782997141416634805108673116228874377879 186.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000007244583419242465562308925791310369388067046022241748446505937325 187.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005650053913391724622420133245054744500098180806807227222630031814 188.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004282374008515182148711924197294626504820260077029095721716352598 189.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003155302977167849823031154606467896319581405629503060749128916159 190.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002260744943379217511422985777715844212833454455851474390837161033 191.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001575591447808647245472715018562721281274861179547408253566239104 192.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001068421766877722593179442166619273335507283291131883905091575179 193.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000007051353206715266826744585189141673803924097897552749508361411462 194.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004530587150606517881462291907437950861433312008907431370201534378 195.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002834709491645512070175032247292343369714524227998757885670492879 196.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001727637451072609368304728390390181668081508645137424251718688558 197.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001025894681569813961310691196169771168062950872274618210242332757 198.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005937087264123422002410689525194151931419799706807977669974854102 199.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003349478638654387388805677349550934854051048554001691665971596967 200.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001842574538231519230155852099796088539135023001700186238733142014 201.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000009886129120453532461747327743125614902413081771488803444858183821 202.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005174739721572825859189109752067400485868344424746192916280369034 203.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002643125756588336194880008147691536359153653259879397962918341331 204.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001317706620881584871815971174595291052419208006205461255089366982 205.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006413486462889446948073339505623291233045548437122775916411055456 206.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003048215412037936702447497777010419649430879549205199867389990397 207.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001415054397603162355731109906413523766802564466303150009572157529 208.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006417637314456401735381180339436525597622008421218789649258011822 209.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000284412634384161162052432037792206098940371184040378777072478209 210.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001231941307018406041126505363404094725756485182533440699317660152 211.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005216664883070218371328826332623178135587087845330336699138114852 212.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002159984736371524970544275091160497357528658329859486882704734654 213.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000874691624017802182822601760640346111431606403754022132187515789 214.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003464944051240945401771642251382491803998950595449549267041507543 215.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001342961691283104541354500951665329643948213997363477857254062568 216.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005093837502949111571083683332747990270151735111407145194847683535 217.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001891155845861663029745103204783770711933496903097014102875042076 218.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006873783257686178364363762165891401797158706076956353839712694279 219.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000244644223842245043422571764533838582952758511103855218900795667 220.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008527621198858639963717829334605101871441349192142044010810545349 221.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000291176746226494100394107480519004172949614743492540450624590674 222.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000009740971473948588340201996869265266025396067454618524589487632781 223.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003193335128885151862152079358929315284060950251946108242477746571 224.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001026034933212075950640805032799727097519295021406809756577800371 225.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003231714172982149783192291057248184766698873296390132659352182055 226.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000009980069737003234445841347798698059975137331824220443897060818281 227.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000302231496636244649301543653195504000705453385541655848183325573 228.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008976893180535797850799616318052877374499104346770518023170657536 229.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002615569591135937972538993439862461701451428317350976999395792993 230.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000007477098840994286970838447116605732834442874848870234382618764774 231.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002097482617924238206667348080412795644334015324339690126645191476 232.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005774745941684697765378244747685330748301564987247780730451794478 233.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001560653890728306748995346985387040675395490611539747352858719706 234.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000414084109968053878125865864994236110559139321842968687615609915 235.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001078814909955930375765691926185415328029149179517664945141791814 236.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002760253796168104475456094924679871690942421803285297237876954166 237.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006936832866438917675151947185680361245309548281577712234060412602 238.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001712573361161999371672416625266380509294357977254503006576269859 239.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000415410672676736315763135782014660257181580785362660126139181193 240.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000009901718854427980597199342924208436231118719618773380828708471744 241.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002319589699227227384178756633941788756507916558868130442189302752 242.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005341236052153593058554052089743455761201551207665113255092989704 243.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001209104952628525743851003735272063659252863902853318254725213786 244.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000269115496106734973229493317243581960234013537231203655178807098 245.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005890145076259614148462209747648646478433604835321142312233584568 246.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001267900654252282354958422109076315961622047119851814526475913517 247.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002684571987465946859704211546027568066858989772777763515948371235 248.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000559182293789033511392324906570782572805103297247122248227594536 249.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001145981304432145045830956862885022347936109341756816261351329464 250.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002311021391305303603840346743221805242568960852747228038360557376 251.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004586575846907874447672065316700827469474822520273766798908642612 252.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008959557871648654224595964722365121742913423940682896704757468672 253.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001722870008747997687159011304415872998616546042868653324598627084 254.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003261671942519104755926027387553799304756904142366215077802870855 255.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006079993935135285702835765017668644056797877214831123040257322634 256.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001116075969079357846379830800997392761540122162469314010389118006 257.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002017735043118975267543584424183118323766484974848217308433317147 258.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003593070214328386335234414906808876898015797822461383725644233481 259.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006303028663185996251366546622935479093800407655357139073622690037 260.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001089344150928322482844281000434432148476395321525888515507590202 261.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001855080806595376940512377612094058288050939074558602890659351505 262.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003113089077058278165067442416425530785637159360980035736789717984 263.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005148725976387010421347758196698334020473364888313450961600832971 264.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008393353187673235907964392439415360152202029082768054047537526317 265.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001348795587066978866034677234221150648105277950852722258057528792 266.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000213687238187782559963247643070058768271015678628093843917651945 267.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003337945280274591735409080175059390581736797708777007747264024474 268.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000514154762055289098280521513325519162852066327880858656546390599 269.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000007810302742446037294223490857250790213513118697679045431495258419 270.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001170162183628291760890903387640298512554294910178896485709271537 271.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001729309652615971851444518191049780622930544203084082084384469946 272.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002521110204948419450381196658090111448321660780731697155956257355 273.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003626157070018300617683209156644326092125306961035879251676103258 274.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005146121005283670800999863351863443271943040179304823151166073971 275.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000007206665936659384443188051258633275074128396828153801235195226197 276.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000009959826101754906371893437442494810639428229199265703119068066199 277.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001358543616194270946792929750111516399898922155234758637213050788 278.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001829114661598717593398397253747935494496556149029327675033184572 279.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002431052832481337007795159291861018782412757539406466713028965887 280.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003189878605779885563461508493560237679320195827828269383088058073 281.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004132567426762137218908438336835300534733232796366037856723239891 282.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005286534760833068849300194716780786441586946979211738827129431825 283.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006678308667422838640573321758662575080792702786802728228854017942 284.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008331911040108926850661857020579377317131056414123069055555214227 285.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001026700051820284475821559914129860084969114879425820169257834083 286.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001249686417012859387673430493807375787704021093630500531907312795 287.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001502637797377714825811551546201451475781247192503379325374790632 288.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001785008387752225213781107774531332553516278245706829799766665591 289.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002095054965943465266299047965166154167247411392222477405154899936 290.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002429718312819026152848757002574339327449024162100036795660551204 291.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002784566159733907741445498065178200026190169415734949023336472706 292.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003153810967293495490794052134885558270782565056530549814843600387 293.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000353041137686734202108770001558091458327725030424111990334604178 294.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003906260301996058515499027656276656024066232812709801926568454135 295.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004272455800983309285918352985156478725896507749495745189584879234 296.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004619643715281072921641607909437449726105335893453250207930603804 297.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004938414288216306162231404231327585937484266880463678764157991309 298.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005219729326504256595011501084609492523735313358363731652635975906 299.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000545535258717850641909574529214309198191656857351511257400703485 300.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005638254456326060218170585279751611613329000129556658658866369965 301.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005762962893206499958669728315170041055501999785682316892406532505 302.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005825836032289136394750465658155870938371692729721151025567101983 303.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005825237478025715384137063143593694348250653922146337993289528805 304.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005761602658573995066467768019352854956193254326234928843907642043 305.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005637392908293936368451748624527696132558479457843273538728005661 306.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005456942411289057855751704388427410779162696970924184788178551399 307.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005226210946814485814704989153391539512541649167068679683919176308 308.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004952461814197292082178930549268386602149542051704579364809258729 309.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004643888835898818992167987851733519093091889124183363130081568469 310.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004309218625425074516663014517228796574629511668039968919817489501 311.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003957314294119636227901878780795668268951950466831892743793264981 312.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003596804628976780003288909351457335695312314839282540907405630502 313.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003235758875198583382243973256518312207204762080393836675138829726 314.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002881422117095016864061376326515240383369716962969737654073494903 315.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002540020455640093885539923248911668395106950595759244348608200219 316.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000221663931743292316593162649145590884211826048125491058559608276 317.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001915172821945617368192675247061888459950551159138559276226117246 318.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001638337594767897269363048653015446230401828020572412654520619194 319.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001387741017244902270302795994635696892827132030074508830505699135 320.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001163991772052018059280431982222121840122339393323758629848942977 321.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000009668396653949162675030619779945747901293905051670212273563996144 322.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000007953319498443785197644915417930998787100503568221928362746292444 323.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006479745248366919785888762893157138753299060737403497360761474224 324.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000522888195650523305781624914395580075872831175073537917675225964 325.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000417952355529564019185646465799141423031744541007703699972335696 326.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003309307665444278694895030534254339818893298568154498394919140485 327.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000259576339102185064773523307388540493661380573112065327904364805 328.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002017137863537324830868687394426153592656230569585697561975425115 329.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001553006536760927257325092348925384334590468525353778252329186101 330.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001184684449448348004399127220379639519760954894000455761810005525 331.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000895463833140892014054517216646272270972328429578957423816419077 332.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006707078761675387069686121011312715902384482828139117374505263548 333.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004978317338725198435831695843415401756516217216036207992535709772 334.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003662006973058208829682446844463213300961506499720404918535621795 335.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002669725256527871515065573009969802308983075955143582132053267894 336.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001929069927561351577646105454503461482649305530585652085016621178 337.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001381612796259287677166889651186843777923327512881416301684869495 338.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000009808542358684721762167354644954076071760491294604691885744377414 339.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006902794073229214680902604849953029884458149251037254989497188686 340.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004815817892729107477187391614361842734013403352005216696719692108 341.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003330903588101469516935164983562798192822948521955906484437631588 342.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002284141040175729714912551203756005109163933554175864737833167492 343.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001553010556318070557739470443371535192558066088520048063996623918 344.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001046979388838969939798011241601437757798437847193763630913612731 345.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006998992037718201213066644307136786190245962689656469305515401342 346.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004639676850014703720392884547953892062092488224154034230298169882 347.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000305011260823713333740255701127758134993506760067498459876387549 348.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001988569518775742265590310980945177191448933661105015622963733329 349.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001285828731869323766669897806834976740510431486753823705559410946 350.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008246381406622284950788436718387607847713998353147782811370062046 351.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005245682237014537321129405945977480167950168043792860117668602383 352.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003309931168910712086160750860871842798985890714452735740771607698 353.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002071735693383261884847417781077592444778621467935743120737773829 354.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001286376056450315225049788095625587867506373695031957991640565125 355.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000007923907629667855075459516267453789588168140892564471940529809628 356.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004842484008541867161210687620957373786032060999007181674470231886 357.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002936118871985054340429302268214662292378332926240452629633994232 358.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001766342317403715562977052602450030718374999060486738244501402182 359.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001054364814039109485885449861706851530439984783780640088851274128 360.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000624511671356121639135034711515387819885542478066425226884901763 361.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003670646099098749532320163821631413371898289735917618150283813934 362.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002140993217682668655034177635027020910935503725635789529218516799 363.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001239302306752831795374563982996001986265004542315614829354831949 364.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000007119453052045435776048161164649805346852983692160294040962647963 365.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000405920958332955524709849618246098305511403397767781602817038982 366.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002297098986997655414387873059050113874318302758267357999051184956 367.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001290265566086893962643745320910129599268174675561480782366894412 368.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000007193782322513105555217458953242972345161747855948856583786220608 369.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003981362746648885760609762220401329598558582184082614369375258329 370.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002187357736731928072846678971763357470619266579614709674108249889 371.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001192995202460330997782896266934444657874404722263853409355868936 372.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006459597412108325279209045779747406261908184195147398907704503971 373.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003472458026074385695648081812352444137087078091518694533080671609 374.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001853318855904150832600670836057273060992217840318688865919359876 375.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000009821131456425435216756413305796038209509233900319247576198763832 376.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005167585428889866904195887668452513385189427629848637396576256079 377.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000269988307214246124698583811135867158255625468897550834050466907 378.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001400714601262932879689586976594362103285548532150073873579267946 379.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000007216355082734923452590231089579179538022945882806920001781704331 380.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003692033855714720445544446650769771492720820988978952726166138885 381.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001875896498127119650781453080830045966075393109982738101829658707 382.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000009465928933308917300858741334065948766914127806020663327709513615 383.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004743996976600454926315488956335636872214383032366626380747808724 384.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002361390817888456910841135337121846972615117457103609360512460577 385.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000116747885661131716420694990514323079313796905032148461512297237 386.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000573327971055637631392296022734966540517412605319066223432817326 387.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002796696476155931512483166813659280829284021955609606721374042899 388.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001355160292785370381906895536676570126008230386500467616161556823 389.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006523100082878845392842794066303744035986823887130868217754355157 390.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003119250512208602550268998964451765427831054502845413552020686697 391.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001481815018616102213997960166732000772834720685175336943746528984 392.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006993584477726048946897233857824020215925315118521865230285682416 393.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003279308048617477496730436862174486295738756953570927519242790717 394.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000152776132496293440044230352986118891958195040792973673068746475 395.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000007071864925048455156803501737498451763773514465942363651889897443 396.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003252607684885237512150089928002918794338081521126359773955661253 397.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000148649248728728309916514130331029957275010098906825836349904042 398.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006750578346199595061103388639907455379474241708416309768173595186 399.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003046353443272115201341630098216612762208702217328704852990420998 400.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000136613731273579727583724343261850205220150313856158565740450534 401.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006088310347937359404023499888924680523351494535769021388338379024 402.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002696504094918761135207539284020404278832502730742669947554712579 403.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000011869178625747073864446661482593473559477377064370010411927388 404.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000519240828788320805257557090854297820950560577139905459152933954 405.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002257661351847772780784825216164284077399625572682727566326040765 406.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000975671611459553007232582756808738099430714457043212691289812539 407.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004190988404459562312319318773190142873581238519575323777206397983 408.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001789410939793542885443216508601573708731108531217436428751170434 409.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000007594468389356296710571109287195824903992961036413554524371884154 410.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003203989325814518629418640724970223171766675015260665775002300807 411.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001343704494511974182384875768139534182749643183174484851389538358 412.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005602063803623967855625077048123800687536462237068097139815689989 413.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002321861747208372415747915228259802431241280119179539746953903573 414.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000009567116219012840378092351303001169294682662162134022689906117735 415.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000039191727192104001035649546668498704001722970887515420203691333 416.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001596204762418139121319660977647763449735526559712243832073277283 417.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006463618912116470755682578202522599315966123485653734887516066065 418.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002602362760211805564973645031825857308328272620458344291898734978 419.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001041781890495106692438729530430147190203426980676398698222006213 420.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004146813330873520058263880616714049112472609642541510769492528822 421.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001641317637523979077717962906234174719889559797150316132761071722 422.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006459857314709688246304331856365661857218868052927410144188959409 423.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000252823209627630740314172705300955970466370984896092073908534726 424.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000009839798092515133506701599214078298832381437395152743854006164947 425.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003808395902710181296146310439265540514821279082494836936792857075 426.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001465871802033536581947686335300089654751540448969373164458248151 427.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005611244014866243712408150771628305586816692964064106998791374272 428.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002136204213944691203981726283292399172590350760391024290849933194 429.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008088312716135229235269782501321262803233744504621691338011761618 430.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003045900298576389688199888469421726917964311110796894305204036805 431.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001140845853176214116426000230271932878112689817135909440175905206 432.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000425013440002017894778476984641519070228663683345416855344958291 433.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001574902131469816472239604755688742639911573793586028247108750453 434.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005804842154123372480666270112838318815109304158548460117792259305 435.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002128255907880283009950091169673220463836068771391620313173253828 436.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000007761835733404768466866902910491668537689423346012207430586226741 437.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002815935608167864977944767246429932695383043652809937215936390812 438.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001016269982026999519168991373741789771950606913673836679674974684 439.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003648664609441889565193297651974591191048388933656696891249985911 440.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001303189433987695377897575691920176746301091697080085127906793446 441.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004630630595393188839487734477721778076033444828569650741829872196 442.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001636974281903048453949331800556488791496415438060534246331384696 443.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005757352117735428724275564054833380765044448089866002688118955901 444.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002014619147679482539682735649051937284445452389921651153857715445 445.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000007013944109177268054882726732797717221365853029364991033035553997 446.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002429633138625228684081752372868565701379447273894396020319063514 447.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008374096783149840138389216340794023864717871133134293447949391251 448.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000287186399834502573717091274732043218322854843983075390467154365 449.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000009800044903637917887962817799620602033956004807969482537902285991 450.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003327669475480442489608138504609599331794532236975141325594190632 451.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001124371362228619465771731734557697108499044288288902109618134422 452.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003780474684165127162221371560089515463619393952722318448105411862 453.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001264909011249709431274049499317351325139157449426187665945622068 454.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004211705520777811976611549298500156751465120050530573368992694841 455.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001395570473394731189503167610501387179833004498202261862268203369 456.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000460203430546164494063391462042003938993776299402555317094344547 457.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001510293724577539015581311996184321175142479420248535471528025198 458.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004932823678032800524917701440482976757201313479527228443649123297 459.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001603472204413651294905522649414842075293258771046343499865362661 460.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005187617567171467485973403192033630362221359638630244544590452401 461.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001670413859608884086679402165263576951191173281363731517965240381 462.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005353511243649124590193085210405977377891433797592376176003204632 463.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001707735803061089231093481134844286279391212847782883659875194635 464.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005422240157399341060603410077777121085811493550934607480320123055 465.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001713648045337781737667660742080544416012757566878971367351860809 466.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005390859141814455598532286000101550644184671704760230960458355962 467.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001688093177254810338505111998663520738677491073831948802514477895 468.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005261934869095085125012465570275142385658374489401952851587346298 469.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001632727236775857795394303422047759489845617422111317389146950685 470.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005043236807335200416297950249403406267965323545594772968505283375 471.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001550747262555791547418487073686340947971412613378094441815973561 472.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004746975114534618231891869089111435657603601070039670989596619635 473.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000144658992047821066445870589271405989775744081010979476724293824 474.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004388687807974288522336608214574556275779287603741523330262331436 475.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001325540156764442724765089514884113255026966650825767617048792104 476.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003985916161905555656023509190663138154359334514269366426280699824 477.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001193297061271556432135014992682247758755155388066828057413770358 478.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003556822098409781004723190526553312431507499047545714729400996514 479.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001055545229620299830420975475624356916799151400589954529053418327 480.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003118893911084492058103672151453461600858844286082436941637279063 481.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000009175743819579797350456016177700033074930698532099832750641568575 482.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000026878603516731777023244443053399310291490818808342166304344548 483.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000007839793785756401391460924167344944656595580256164080172358690463 484.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002276894035664004110525618304434983683157356666788064886601511302 485.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006584592734397799882540383467914830404961660527332675176980858997 486.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001896140724505751896138154380289778160581438068804994897311975088 487.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005437200452914626316483339063147343289688935068860688220596359204 488.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001552568568834041010892417071686326475063059682727149362682102562 489.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004414732479494580355594534930482954537063640074226030684081311937 490.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001250096656907220344487887829439769866879068967322876293716488375 491.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003525135329972851452033030283172362073371349168655487164585985592 492.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000009899387990603522772823211783260612667152574853863952617907416902 493.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000002768525688076612911107103633016389050970923261165856173465402074 494.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000007710875523662724977444431382876869799052811226859403988971253987 495.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000002138853163366448770199799251014248145429416297006081892460168394 496.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000005908640508166611792328670877961181322656113571445486067251389282 497.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000001625663356443765840409615251322516466254933715771968338819264449 498.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000004454687127454854523975459861511461171932106955038466041586557718 499.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000001215778295417706531443134694865228374445294228263383310886319634 500.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000003304830255502683609433215647787167126102877085577401094260670424 501.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000008947639657504875735808485402540686278002454881277996489524760317 502.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000002412899816026375796172492489548502524209081016774137521115718397 503.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000006481095955198343598405264796574270375073454298485388073555018211 504.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000001733974904446146157053339986367223195634662537720502871819150529 505.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000004620927527183953869101033478326879317099187605279203857325404671 506.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000001226631908320104774043886842997764533202061503165686300329850569 507.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000003243432222085233919410758695919595967129215182556646436920349921 508.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000008542943668143561442413836813514785162349112954600335467348727908 509.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000002241449938201833827911435868126694898281972921534511186638919828 510.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000005858358289622862055311783082002639602707029142807685882168579872 511.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000001525298207489035852740334788202752760410895005647957022270797855 512.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000003956143700529126673045780606121376778999663194477528346614686212 513.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000102219630842251079408248152277545875544440445348961177621165765 514.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000263116422686812134093693330355308903989387278430808919467753466 515.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000006747135155702946367350394714955595396775538785123349931769922646 516.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000001723673645261747621471846214815556741279104796218534713709864727 517.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000004386934849519765000534094155962679398014432434988204266150372504 518.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000001112356859962492806759896710459223569934284094989951072054134271 519.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000002810024850995253168223105352486778793481472181600101957837310167 520.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000007072378241300652790385963157769435575705352254337846150950401121 521.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000001773439112786203903198452237936399203562599719741504491153129615 522.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000004430663052538552702679859662330823990930283999191686757680782476 523.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000001102881675352128080817932048072951551994897375774248617981227578 524.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000002735287352042974200837679154746748697264663232008218453742931116 525.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000675922465451006727426757761824088978769556973165802708052553131 526.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000001664242875104189614767793256933936945744057290176009634059731289 527.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000004082896888630985028914296213326849820141011152120487832065836083 528.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000009980629733955597109819252311193417037128219322008397018642717882 529.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000002431035015124847833805077699254758380305406005156332212239034309 530.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000005900300088049627083353647865267392556708918701087954317029070712 531.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000001426962130186180851443571747688862695571136519664366412978772671 532.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000003438840778800106940268988417249446881945671238924105758611424882 533.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000008258073443666470092916366376066791520030683346948265616990838534 534.0, 0.000000000000000000000000000000000000000000000000000000000000000000000001976142293522009946432599133259769769850024495370956125021980848 535.0, 0.000000000000000000000000000000000000000000000000000000000000000000000004712335455221326617829143371343896237465626518914519404758474465 536.0, 0.00000000000000000000000000000000000000000000000000000000000000000000001119794630789742487536367336676179684171914515506625108964570741 537.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000265173676342093793055202406737052905329510625767683103297437456 538.0, 0.00000000000000000000000000000000000000000000000000000000000000000000006257745616043422267597742612349835797519316014554852818073764999 539.0, 0.0000000000000000000000000000000000000000000000000000000000000000000001471656502050697978258941409810420484001302150704884135619931243 540.0, 0.0000000000000000000000000000000000000000000000000000000000000000000003449066860233991332904676414793368677314537185496590312463438435 541.0, 0.0000000000000000000000000000000000000000000000000000000000000000000008055804343248886778721874662557157257726153026575912093352650573 542.0, 0.000000000000000000000000000000000000000000000000000000000000000000001875140604381870596329121838540247451332638385588228767280043898 543.0, 0.000000000000000000000000000000000000000000000000000000000000000000004349925947573824027669377120285419834050161870431572018114130846 544.0, 0.00000000000000000000000000000000000000000000000000000000000000000001005676775670526108017493490929196578137839269687681742273889513 545.0, 0.00000000000000000000000000000000000000000000000000000000000000000002317228786185282275085350392968263421565832219754246295286787471 546.0, 0.00000000000000000000000000000000000000000000000000000000000000000005321311968519041993751170761872967029402983598768280682827734154 547.0, 0.000000000000000000000000000000000000000000000000000000000000000000121790435627550551407931978916567940995755229690677134252264148 548.0, 0.0000000000000000000000000000000000000000000000000000000000000000002778162345938741307319529044674149079609737411353987903783527755 549.0, 0.0000000000000000000000000000000000000000000000000000000000000000006316221019955411032975299332131700810267281190783696971354384483 550.0, 0.000000000000000000000000000000000000000000000000000000000000000001431256710281756199491776961186703564584506324991782414434517596 551.0, 0.000000000000000000000000000000000000000000000000000000000000000003232536981703760091627148631639052297187335935063860027374071542 552.0, 0.000000000000000000000000000000000000000000000000000000000000000007276799666067606927220806491602056599667302617302863073485062606 553.0, 0.00000000000000000000000000000000000000000000000000000000000000001632726564398968419348029047317727227729259937862857111886633319 554.0, 0.00000000000000000000000000000000000000000000000000000000000000003651470254601423544243071936767490125007116535157096723823378795 555.0, 0.00000000000000000000000000000000000000000000000000000000000000008139701170663159434046758852414646268592638813458481400072767582 556.0, 0.000000000000000000000000000000000000000000000000000000000000000180859210266634453036193079365019535524393967886918782964241608 557.0, 0.0000000000000000000000000000000000000000000000000000000000000004005616213743682949642715571567185085914798688136270971570648473 558.0, 0.0000000000000000000000000000000000000000000000000000000000000008842999785767195058739384710558839742644757681353515688572401997 559.0, 0.000000000000000000000000000000000000000000000000000000000000001945971486685029726781908422039630253330364527459548113735694866 560.0, 0.00000000000000000000000000000000000000000000000000000000000000426859440934165414982973249878322315567348282840805771091582693 561.0, 0.000000000000000000000000000000000000000000000000000000000000009333613758574469178268691872873688666178245566825043614282735674 562.0, 0.00000000000000000000000000000000000000000000000000000000000002034399318722746834486821574765969105771858673611024491011751555 563.0, 0.00000000000000000000000000000000000000000000000000000000000004420271272169478598567641237744330507140422329868628675372874124 564.0, 0.00000000000000000000000000000000000000000000000000000000000009573987669080255117752487784302051730947066887224742536147161688 565.0, 0.0000000000000000000000000000000000000000000000000000000000002067154199921706996230638785945062321582702331200489916728239193 566.0, 0.0000000000000000000000000000000000000000000000000000000000004449321392292218643034584109410408097209723577936619396991652043 567.0, 0.0000000000000000000000000000000000000000000000000000000000009546856017661523156886540256083424140235851983421240460649153239 568.0, 0.000000000000000000000000000000000000000000000000000000000002042102006878537122279417028607151355271537202830818050762277794 569.0, 0.000000000000000000000000000000000000000000000000000000000004354614392107585555884964333145459056908854082209662497154173609 570.0, 0.000000000000000000000000000000000000000000000000000000000009257248240370590672162938393638603092362532187175105444192925392 571.0, 0.00000000000000000000000000000000000000000000000000000000001961908499708934526396512548434536469268277453389172375886477791 572.0, 0.0000000000000000000000000000000000000000000000000000000000414519431095591319178639582806077878645255330547318882931448954 573.0, 0.00000000000000000000000000000000000000000000000000000000008731422282089903303255761005555325578530468185935262509479135574 574.0, 0.0000000000000000000000000000000000000000000000000000000001833596076433924873412888210862154262904206550315376216022604781 575.0, 0.0000000000000000000000000000000000000000000000000000000003838888766616701476907204337956922019761319756323122131461012618 576.0, 0.0000000000000000000000000000000000000000000000000000000008012999974942166269153559650184693498385417070546874728055426282 577.0, 0.000000000000000000000000000000000000000000000000000000001667543079190363763964330825950311303498905983871840438727872833 578.0, 0.000000000000000000000000000000000000000000000000000000003459838442929279961100388339349479426913860896749919904047794888 579.0, 0.000000000000000000000000000000000000000000000000000000007157080789711033421034833796754289306137628468013915617509173873 580.0, 0.00000000000000000000000000000000000000000000000000000001476120607753189391514928977276776798786664594846998953503328346 581.0, 0.00000000000000000000000000000000000000000000000000000003035414818861569520205366462378579766501908455011220875711487582 582.0, 0.00000000000000000000000000000000000000000000000000000006223418054052310326888504584248214937592524570262125493820745752 583.0, 0.0000000000000000000000000000000000000000000000000000001272210666641019003887682112783399468823542878511879654100547554 584.0, 0.0000000000000000000000000000000000000000000000000000002593060273257537658616772882977271474594464253529286362670545292 585.0, 0.0000000000000000000000000000000000000000000000000000005269799362074665628132876843747430744038507719620638732081109473 586.0, 0.000000000000000000000000000000000000000000000000000001067843933237828173771513307192381447115918714047027495539417925 587.0, 0.000000000000000000000000000000000000000000000000000002157535980474790946943070440324764675322015031316816000346831246 588.0, 0.000000000000000000000000000000000000000000000000000004346594927242941922453502887641455660663647296006661241238209565 589.0, 0.000000000000000000000000000000000000000000000000000008731431056650397839198642769597638365115128810989271378255363644 590.0, 0.00000000000000000000000000000000000000000000000000001748924595820354285649075785808253158313499181090865164685978127 591.0, 0.00000000000000000000000000000000000000000000000000003493094809587229054133726867455191685753990985356567276406183652 592.0, 0.00000000000000000000000000000000000000000000000000006956767790926680566912181435315175062360976162746330831506152444 593.0, 0.0000000000000000000000000000000000000000000000000001381550232053661102226887641323805247493437423834866140598560002 594.0, 0.0000000000000000000000000000000000000000000000000002735848621393131168975793611079445835541698150777584925954019297 595.0, 0.0000000000000000000000000000000000000000000000000005402413329437913058659741875049900884785309115815149736741008179 596.0, 0.000000000000000000000000000000000000000000000000001063795243200689037319927859641446729265541537812693913543837072 597.0, 0.000000000000000000000000000000000000000000000000002088848198199487403750843073586361641930389037204229723593312716 598.0, 0.000000000000000000000000000000000000000000000000004090142171449720529991635512144480774554392488228894358408420719 599.0, 0.000000000000000000000000000000000000000000000000007986503470942784882377295058873762823000615800214641697567928204 600.0, 0.00000000000000000000000000000000000000000000000001555126668959143389451783039945308058945322838311014954196946391 601.0, 0.00000000000000000000000000000000000000000000000003019740924729348388559863315748551729941334670400591448441398754 602.0, 0.00000000000000000000000000000000000000000000000005847529732023184206731748556145016915136063610499036687681801803 603.0, 0.0000000000000000000000000000000000000000000000001129218587449047027209561313601227207846381056938840891530025521 604.0, 0.000000000000000000000000000000000000000000000000217465511555629682219656770975688487180326417903644757075323636 605.0, 0.0000000000000000000000000000000000000000000000004176509794421135646196632621203583680955373325185983034092893068 606.0, 0.0000000000000000000000000000000000000000000000007999287144500696068732844395808135647822555439658928546879491381 607.0, 0.000000000000000000000000000000000000000000000001527944736642874160993710634610118176237087338459468688836743496 608.0, 0.000000000000000000000000000000000000000000000002910626466895167701518750037179125980101647781134587628126497538 609.0, 0.000000000000000000000000000000000000000000000005529573549401722789781214576386686661205504159514965944328562528 610.0, 0.00000000000000000000000000000000000000000000001047675993645744074963520738952907211821369297137326565126058357 611.0, 0.00000000000000000000000000000000000000000000001979686252787847260471392164002339179462243318816907378060387938 612.0, 0.00000000000000000000000000000000000000000000003730813816976677735204028843367985855827237359932582831598086566 613.0, 0.00000000000000000000000000000000000000000000007012169655675389193066304042638196561341437401481499360023039095 614.0, 0.0000000000000000000000000000000000000000000001314457834608531608472303094516953531403164307102971841005925451 615.0, 0.0000000000000000000000000000000000000000000002457480424166386265102852415835254764807997258559520674952708002 616.0, 0.000000000000000000000000000000000000000000000458233016561309913549008326672904834438202519492083753453073635 617.0, 0.0000000000000000000000000000000000000000000008521956494871501151923652172664253469159482126383520232523465805 618.0, 0.000000000000000000000000000000000000000000001580711363507748318654335388675635346920669676409216240867429693 619.0, 0.00000000000000000000000000000000000000000000292435337381216854420883301928915781654740585762391532334575514 620.0, 0.000000000000000000000000000000000000000000005396035604562808789706945293380352418191038047728924736457070062 621.0, 0.000000000000000000000000000000000000000000009930956792050013278681277458386045493762478640090124118597636836 622.0, 0.00000000000000000000000000000000000000000001822981830797421597853816324409855567194749593409509089773862207 623.0, 0.00000000000000000000000000000000000000000003337737396460910454143380929082990730430304769043662138343403611 624.0, 0.00000000000000000000000000000000000000000006095427400540209854380858960986712999253714791627181299682820025 625.0, 0.0000000000000000000000000000000000000000001110304027231689493977993914753861348680668230078587475333630825 626.0, 0.0000000000000000000000000000000000000000002017293009871957249589285104009265295573776625893870445189194978 627.0, 0.0000000000000000000000000000000000000000003655854597405004915365432334783195564062638627644042936343195989 628.0, 0.000000000000000000000000000000000000000000660853563364026679714207092744528511316741342630246175066571243 629.0, 0.000000000000000000000000000000000000000001191575213132969366662260401829228948518726011501680954766026215 630.0, 0.000000000000000000000000000000000000000002143093321688630586822718967112801405117551346906705102846272343 631.0, 0.000000000000000000000000000000000000000003844745362860414406275387999897375451459737918311071455615183188 632.0, 0.000000000000000000000000000000000000000006880253170316022703411115690617290416511169727698341534131108197 633.0, 0.00000000000000000000000000000000000000001228160259399122311095429600745503698970737110626874919120793714 634.0, 0.00000000000000000000000000000000000000002186869537681933499600502992878177528527629524123136116497917734 635.0, 0.00000000000000000000000000000000000000003884287220551998704795413260052568566862522907416274227692532042 636.0, 0.000000000000000000000000000000000000000068821450257860165567961955593943361632896864521091898777407902 637.0, 0.0000000000000000000000000000000000000001216364421246958574278746309519668608561515830658841349971922999 638.0, 0.000000000000000000000000000000000000000214454106124377228352709122137858315122227597494998403872756052 639.0, 0.0000000000000000000000000000000000000003771717370731277347901619080107795740809999788859907060526142519 640.0, 0.0000000000000000000000000000000000000006617308312497886111569807883338407367506649134844476827493475771 641.0, 0.00000000000000000000000000000000000000115814881509171510795347455459362507538474878727639181612296503 642.0, 0.000000000000000000000000000000000000002022047888117786430852443602553334314763117479067196955209161452 643.0, 0.000000000000000000000000000000000000003521809538539845904660132902227845317697531847061328703183433059 644.0, 0.000000000000000000000000000000000000006119147539162538389308973354341234984593155823007337063674051896 645.0, 0.00000000000000000000000000000000000001060644565066216044544178745349816138525663370650851070960521978 646.0, 0.00000000000000000000000000000000000001834027931257823045084415085980413047094163525639902336916485014 647.0, 0.00000000000000000000000000000000000003163751575911696851512597295070812459916795647141228910283563575 648.0, 0.00000000000000000000000000000000000005444554748593614928588779997078586342347606664850110984463469815 649.0, 0.00000000000000000000000000000000000009347363378960076338600156658975113677719266846321532974909176208 650.0, 0.0000000000000000000000000000000000001600979706456214301221090126631302687093928744367137559538532818 651.0, 0.0000000000000000000000000000000000002735619170378746701597101711757714574425401882125861711656475759 652.0, 0.0000000000000000000000000000000000004663389742825779954581199359193161319639560379313922396936698106 653.0, 0.0000000000000000000000000000000000007930986102139604165592040511558823448206747895888519937916412841 654.0, 0.000000000000000000000000000000000001345659522734051335827775105752269980077467655398462229145248624 655.0, 0.000000000000000000000000000000000002277869413283832129325994719763586135564078211722763305034397514 656.0, 0.000000000000000000000000000000000003846902556644213447612801364530927351703020960542299494058972682 657.0, 0.000000000000000000000000000000000006481646740697564340453553577100757491153128064568076556234376784 658.0, 0.00000000000000000000000000000000001089568165827957915494070957431102512229298889420469577732162609 659.0, 0.00000000000000000000000000000000001827348508308054134192343285566788544522179760250402169744809992 660.0, 0.00000000000000000000000000000000003057660958625169196913370134183276771747181028013268095406623635 661.0, 0.00000000000000000000000000000000005104594708842857265452267109114647153798436423440497633850660116 662.0, 0.00000000000000000000000000000000008502374068554750175429462921487695280224653948551073896659359421 663.0, 0.0000000000000000000000000000000001412957673640467356381147087581347197416617040370092683675487731 664.0, 0.0000000000000000000000000000000002342777706818449890259436027660139014929076101500045243610593245 665.0, 0.0000000000000000000000000000000003875689443236986499209797877602953849629462051227949529095568777 666.0, 0.0000000000000000000000000000000006397138765210415665775102355026381773659517997002520247340984568 667.0, 0.000000000000000000000000000000001053523993807665772211371608786199316364342697425529285636211493 668.0, 0.000000000000000000000000000000001731122812812593455903702774380935706303439655281894592443513272 669.0, 0.000000000000000000000000000000002838174113759279840793585468394487983985958596822471417603300113 670.0, 0.000000000000000000000000000000004642808820105915481047481680470450408589953657598516838234146292 671.0, 0.000000000000000000000000000000007578025626838722218112116946344616845823744175569035989902861149 672.0, 0.00000000000000000000000000000001234149352371378209843250855097847258211341651282903366618717817 673.0, 0.00000000000000000000000000000002005481299769056560542458034957038411604222996294496978135898961 674.0, 0.00000000000000000000000000000003251708556909560083685600167426120897180908461513054697072326073 675.0, 0.00000000000000000000000000000005260772864052762801316243012307653644609059265527632704699991855 676.0, 0.00000000000000000000000000000008492494161380868571484430669907219724617804348907899733750664861 677.0, 0.0000000000000000000000000000001367954164281594165641810713489227012024429486558884129209441102 678.0, 0.0000000000000000000000000000002198675923205036367760036294864449287230612992146521474654480997 679.0, 0.0000000000000000000000000000003526201061058820669854151849959008413350031000238216411964206916 680.0, 0.0000000000000000000000000000005643023738059813753123288942850990058706459007685907579581067791 681.0, 0.000000000000000000000000000000901111231149851850044894797524713124110916756999352311897520485 682.0, 0.00000000000000000000000000000143585108825018132892829198889884995342778553566130464937188187 683.0, 0.00000000000000000000000000000228300895922135836520698203125250024171025586961696512039026451 684.0, 0.000000000000000000000000000003622227925530162202462721362791644853167028478098235603482317399 685.0, 0.000000000000000000000000000005734777796120316307199944633985400086934762439950035181159107922 686.0, 0.000000000000000000000000000009060095662731207072514838358901558335555580621606757781973702677 687.0, 0.00000000000000000000000000001428324983880220281213119962062369371580401324824717971648419692 688.0, 0.00000000000000000000000000002246994584406752814741890338110568777363435741295871797125710015 689.0, 0.00000000000000000000000000003527446472520320632839418332187921297801424295503556375763254439 690.0, 0.00000000000000000000000000005525924044870904609877955181873484832360616448558513053606248169 691.0, 0.00000000000000000000000000008638495464059947167140881838274571158126080530979171755819769294 692.0, 0.0000000000000000000000000001347605153439383624018673211994942132335444718116230012961305591 693.0, 0.000000000000000000000000000209788267345167895222513328186780441338381625621360070558470087 694.0, 0.0000000000000000000000000003259089757662460855052602928110797672507262816910530048034095842 695.0, 0.0000000000000000000000000005052550240206771060046932912939627477088302000364326598722970643 696.0, 0.0000000000000000000000000007816759392527501852921650717072779171338104506248904898161225111 697.0, 0.000000000000000000000000001206833094673319548423105652498358887743630033909611711722350134 698.0, 0.000000000000000000000000001859407635136951084780844344912004920541082902489264224615423194 699.0, 0.000000000000000000000000002858982502671327440058232458890599258296819502275200078987541414 700.0, 0.000000000000000000000000004386927123038403968276438224964236762712075834304384091465355301 701.0, 0.000000000000000000000000006717751156610196572802821137713987493493713170748656525868924195 702.0, 0.00000000000000000000000001026607657751298288074723220987087338635467954912336409862661679 703.0, 0.00000000000000000000000001565686095366370111959333738777779703727092431621871228664201263 704.0, 0.00000000000000000000000002383016234080107050289161819459078862065230887300068931715237621 705.0, 0.00000000000000000000000003619711107591378349908381465442012657624177204809232428791131571 706.0, 0.00000000000000000000000005487163549802878409903434591522650349086167425454513657539365725 707.0, 0.00000000000000000000000008301402192091128666428084584056069417185015906108623334619774488 708.0, 0.000000000000000000000000125339230024648449870707606307344529033748681543490872237253695 709.0, 0.0000000000000000000000001888674237449554472484821567942637400025679877828291737000106483 710.0, 0.0000000000000000000000002840298581550558386750259388979343364237888457718332707609840495 711.0, 0.0000000000000000000000004262950341985539627459901449620899022834547586776751615387676244 712.0, 0.0000000000000000000000006385549942975820323880264446222041643138966791707239389632343946 713.0, 0.0000000000000000000000009546199427262358300635198126894750905643459049438771340615206945 714.0, 0.000000000000000000000001424325544995853148336235349237406401114090389322391508100717237 715.0, 0.000000000000000000000002120981968197867480157421843500441016958135234266634742926210012 716.0, 0.000000000000000000000003152216374465536477025329484762268499734448546668640460513338985 717.0, 0.000000000000000000000004675722899504713505324468984222170728769345791396691380377038631 718.0, 0.000000000000000000000006922095224107995017172347981375718028935223253894606845859677231 719.0, 0.00000000000000000000001022786016959625487024709018864924467906403166249875248717445812 720.0, 0.00000000000000000000001508317404344948641629928751624649281825169179351135701440688137 721.0, 0.0000000000000000000000222005523753233875759842107733060622754882757343727837488388678 722.0, 0.00000000000000000000003261371079892681640804303052873125158499275007170849504665899143 723.0, 0.00000000000000000000004781942603201745893479965514703493147391538076499281986995981141 724.0, 0.00000000000000000000006998073682798714408566012579207698395135590968870192239597719658 725.0, 0.0000000000000000000001022174311573899889230301740001065195240059834956583610525268757 726.0, 0.0000000000000000000001490204928876833525967150553431135565507419806817025015788199117 727.0, 0.0000000000000000000002168422387261752846289270276542664335460032082262005670205382858 728.0, 0.0000000000000000000003149349694005423947703734065461351018251900083481633590260825747 729.0, 0.0000000000000000000004565404473678231147593359167163060857669668010285147516055485033 730.0, 0.0000000000000000000006605736544851246293308798004950423410292156862546966034477776698 731.0, 0.000000000000000000000954001565759817317904183046070935411066811051354600531166566891 732.0, 0.000000000000000000001375197271547450387139150893784259430041410656454538400782554609 733.0, 0.000000000000000000001978660159360356584754312805530407871902481802135454803664591937 734.0, 0.000000000000000000002841645624468155267251288223972358867311676834069799085258096814 735.0, 0.000000000000000000004073458762331198266450603087572522368143372626823019530950817433 736.0, 0.000000000000000000005828457314922992100557192212142877522273006615312728078531691297 737.0, 0.00000000000000000000832420920397421095643464677094681055851456959546254008271546541 738.0, 0.00000000000000000001186679922253431433777825088781858162155784691767702870921686366 739.0, 0.00000000000000000001688603199393470867764198379475357863279533107798114266560244949 740.0, 0.00000000000000000002398430734135704645874897258085233917190321352190949043327014919 741.0, 0.00000000000000000003400434853358591544733625518909087611524205125707915165046768867 742.0, 0.00000000000000000004812287654560673633311935830041163704331940113627856145848514254 743.0, 0.00000000000000000006797992335036233217623944300082478499296921725699089410984586078 744.0, 0.00000000000000000009585700425127999565941556643331487374669957796227360979756341848 745.0, 0.0000000000000000001349221445273031287696173353179420336889050624608700485650035436 746.0, 0.0000000000000000001895662082203683651600917930325002316447074648764109334260070902 747.0, 0.0000000000000000002658636787139959590277501989079356390416503822257472734039292551 748.0, 0.0000000000000000003722027527970485452976253239642489454538563173028267618751179377 749.0, 0.0000000000000000005201453425348668149352898978396373370099398689289529995826631737 750.0, 0.0000000000000000007255986712234249922226643351392857410221390590506724513450081746 751.0, 0.000000000000000001010408371227045561248732982653876004900445764127078811192514893 752.0, 0.000000000000000001404520678661110832995361724970180710466102251422537005519154562 753.0, 0.000000000000000001948911549136597914578056844256664021042919573959759790392751618 754.0, 0.000000000000000002699547333403489195918922790302877219114522169540575846431572215 755.0, 0.000000000000000003732730354822313615916963711035328983349799404540975587511952914 756.0, 0.000000000000000005152300263512578254804382087256564889445191772604885563744153539 757.0, 0.000000000000000007099317472005295338908125734555272832715272898290260985259295346 758.0, 0.000000000000000009765060124386254534160171663585216742437734655491883807827516987 759.0, 0.00000000000000001340843629277472782830565301515412893561576815936693740985456369 760.0, 0.00000000000000001837926839646365352679529555369275506793831493735283177389666969 761.0, 0.00000000000000002514937306836293324596719974910200091056449073875953047979397105 762.0, 0.00000000000000003435397029948215630661377240545040653769895382085755715536035027 763.0, 0.00000000000000004684675383847310977989964186829354534994649428584091816628333249 764.0, 0.00000000000000006377299609583820296008959199035073229076253768501870690411287786 765.0, 0.00000000000000008666641664006348947315031194837490832015622771087383185988997717 766.0, 0.0000000000000001175773054015984271602930793776390340322526037244793139017532397 767.0, 0.0000000000000001592416771991044279726006332142256379323173916753067241287022918 768.0, 0.0000000000000002153041929794328164734599806519955333094991234049009885040690549 769.0, 0.0000000000000002906114061906616194054316188080678211441414079918475652116835545 770.0, 0.0000000000000003915968229374401060484926332226533415695652562685262797590436912 771.0, 0.000000000000000526785610448363075333358202972533705408754576260033369127176269 772.0, 0.0000000000000007074549407214498953373467505278515725407973614994473279698609302 773.0, 0.0000000000000009484964091480677407128708563330570053584926786733775233020027722 774.0, 0.000000000000001269540344975637893927741204389139785684754995600084126920309338 775.0, 0.00000000000000169641891683969332775470153372746689168280392656214993064276185 776.0, 0.000000000000002263066719006591335657692104817370672918387307321115031068789761 777.0, 0.00000000000000301398523989030051734704932352191412216886977311255486179293213 778.0, 0.000000000000004007432812585979719778676443043311867882848681897271663059102123 779.0, 0.000000000000005319546261938837482345572284856554940988144069366551052380883639 780.0, 0.000000000000007049656905005305214065560737024731823013158933978933367340230691 781.0, 0.000000000000009327134830510524511532080282782536519204637360423666548222035044 782.0, 0.00000000000001232018520043518650127075799142263245991565500089609627932657327 783.0, 0.00000000000001624713329408927621727958116736335287749370722024210738297371592 784.0, 0.00000000000002139087685166682964137759868767612726071739773923113117231717092 785.0, 0.00000000000002811736200132043550998064262148376669224158779597761078868212229 786.0, 0.00000000000003689916132641713886232717987882694553695865105542616836901587522 787.0, 0.00000000000004834551008013964424223647831681345768849742091260325154448261563 788.0, 0.00000000000006324050221672768460240055386783736985513051454404018703652211927 789.0, 0.00000000000008259157772648904784823762836348093717591873976107686704743305117 790.0, 0.0000000000001076909661984538188140819937510907624260784465654937043293626175 791.0, 0.0000000000001401934122991074358588036130272143000817772380559576367488372203 792.0, 0.000000000000182214325767604873714916205207621142673459696011131979064514834 793.0, 0.0000000000002364535065446776431945948961052745278949803527917812562108475352 794.0, 0.0000000000003063508370860972565325737219108414817120917907547354397053588691 795.0, 0.0000000000003962818598661650956219283246308432196381191643749377339652059467 796.0, 0.0000000000005118030301425786613871691593605282822157719097447667456514506491 797.0, 0.0000000000006599587262989235264499427572518137449118050783857182783271882696 798.0, 0.0000000000008496649013844014358113598173379658117334448311309789838640839402 799.0, 0.000000000001092187676252466740950040443725529918535805269683647904645074833 800.0, 0.000000000001401739334103887114287695225134729957539401084125494587072158465 801.0, 0.000000000001796219229831232508045444205225588586959254014939074842663964835 802.0, 0.000000000002298133255619635650710203086264821172812805812473312543823011085 803.0, 0.000000000002935732920734541002561933031422580506252622046375430086529018187 804.0, 0.000000000003744424061663929828869855266476523700799744291062894223865743485 805.0, 0.000000000004768505998086834739000579512468908415975214554603803724453163177 806.0, 0.000000000006063314943314139817566234077941739285970776424948280762339766187 807.0, 0.00000000000769786107979403910608364039690451914781032365958883082898414096 808.0, 0.000000000009758067407794000872478549471103686116129196752315474359211635666 809.0, 0.00000000001235074083828527647519492842927609458622048104563882091863763386 810.0, 0.00000000001560843269478963012517781632538807963179894989669906376891890627 811.0, 0.0000000000196953775887142891743713396895258205393746344273405107559497165 812.0, 0.00000000002481473743943770734873191009484293053005810011597796560901978958 813.0, 0.00000000003121742226861399676104372300208298814749110202253093577539695451 814.0, 0.00000000003921281251578469914197382105221495625726316769763529169275638722 815.0, 0.00000000004918177039290500016704079414064961602572378498098072887975314392 816.0, 0.00000000006159240182225200912614807642091880900049088438086295664736858876 817.0, 0.00000000007701911762569050728091131714094814600466435080249516310257956897 818.0, 0.00000000009616564496007637020474754932801480218586967946904950368729708527 819.0, 0.0000000001198927599288355807450758488770325687513762015625726577809572018 820.0, 0.0000000001492516525820312731468368910487672569812574728817669239546035199 821.0, 0.0000000001855239992561617323280518979209677233370166977189496697770744007 822.0, 0.000000000230270007921643812939176326528018145045333620726247468175027702 823.0, 0.0000000002853859238761149912979951191651651197931765458545451863954557917 824.0, 0.0000000003531727402211721773738994022701859699053366761138137502504481083 825.0, 0.0000000004364181551128277147560045228478422441616378278928890018334774899 826.0, 0.0000000005384941614035495710062361800890885659798931291165810289414551679 827.0, 0.0000000006634730502672603351384353915270051872602725293390149796354135957 828.0, 0.0000000008162650656906920116758657939177618749349840137820253963781177556 829.0, 0.000000001002781469051842445525807823275608007649296451304319295700799429 830.0, 0.000000001230127370922022819660658738587945219391667012317863659353285054 831.0, 0.000000001506829370111874257357753051767454946449260452905321284094183917 832.0, 0.000000001843103818127787630700533723678923556988775134218401308795865816 833.0, 0.000000002251172411855005475049002985450059510059504955042627645129592767 834.0, 0.00000000274563282066394994406551223861055768112944969781367737870325488 835.0, 0.000000003343893189488257547450535360786109750820807007157399540423042208 836.0, 0.000000004066680641022155783555708063081883571820075631219281115813921317 837.0, 0.000000004938635343348378251970298260970668176129807784686706850698637564 838.0, 0.000000005989003330386992176582646450031937591407349887160243691806879336 839.0, 0.000000007252443078910025394775531146256698940873482496391617864101354351 840.0, 0.000000008769962875818303616625111209154265978099586547517748207979369012 841.0, 0.00000001059000827213806073519873744712957409726623940413769366850380009 842.0, 0.0000000127697214357701252701591973515244419272008517185383063711824862 843.0, 0.00000001537639700407909991179696933193964817224377337461726383211735793 844.0, 0.00000001848916212110312572466147784727323063389064068690022464986535477 845.0, 0.00000002220091174394271588510186501407711534137994454540090897866751724 846.0, 0.00000002662053404023179017883343534315463959218457292103055695367630669 847.0, 0.00000003187546479472395365480875523826469919846752545852358872035049807 848.0, 0.00000003811461421854549114894133529085141624208294932611489756941221162 849.0, 0.00000004551171442915355434484236963171072781946554593999495168010398817 850.0, 0.00000005426914116058281989304274671202693958433608012185662000824980039 851.0, 0.00000006462226898823439685909275330783031218798180008785245524747059285 852.0, 0.00000007684442552370769608535251025759707111028426136453900438526730057 853.0, 0.00000009125251666316988542333975562108184900355431858980218528864534885 854.0, 0.0000001082134020636576386405346284090177590133548197835901605370482787 855.0, 0.000000128151107576860829167579757935342634389241029199741528877098349 856.0, 0.000000151554969385019979844603251280084788455903684986469422649821465 857.0, 0.0000001789888130476449700599381706474362117319377837122622125466056048 858.0, 0.0000002111012795623027642513047650163193625659095365263342711508048489 859.0, 0.0000002486374198405599643920997602622564973323346963596092748858128084 860.0, 0.0000002924516886644094206181995225726083356786242649762471585265368146 861.0, 0.0000003435224791714183707646440150704691320975539671067725308605821224 862.0, 0.0000004029683491586749689719815429335835678638338545394561218061032011 863.0, 0.0000004720661009235018659009773656202750353096237781108232581000439617 864.0, 0.0000005522708868856843473952209209758867046850279013844240801661114702 865.0, 0.0000006452385237590636400876410277105037122868468520814752527197354264 866.0, 0.0000007528502084407560915646751657304425978476038422932633036010331591 867.0, 0.0000008772398389275265763208061140759259036221896882210341627020335421 868.0, 0.000001020824153296217869275121093243575700535682131088228161299468173 869.0, 0.000001186335908924830830605623728089523040170489830276235344660398888 870.0, 0.000001376860332489482115951119458532239463786044656908573746341817869 871.0, 0.000001595875078636706434462130278415379971913670795095109345603349721 872.0, 0.000001847293941367027734775142944298046642296545869131750630273635023 873.0, 0.000002135514566821114625883213517339838442045425351219829494875434201 874.0, 0.00000246547041906147239723620771422759752814729133649130635464559342 875.0, 0.000002842687251299261693320447802924530306572020574443994403395835828 876.0, 0.000003273344333518910323399221020764714780218556792657002993210245149 877.0, 0.00000376434068327849553739474324150280619946244064677332968209784228 878.0, 0.000004323366539273713288023530067061899761113158738351055445960748608 879.0, 0.000004958980306699049032018185985340156156530780400759080204730316793 880.0, 0.000005680691189165251559673190160646443728429344482069091843911651341 881.0, 0.000006499047703577125935164551029923541152345668881527740003805016465 882.0, 0.000007425732251580021822676898663512200731778961347885436201749164121 883.0, 0.000008473661893592335924950011246271985959946787071704158686506641929 884.0, 0.000009657095438710534942650828005420267219467521858876998041836496184 885.0, 0.00001099174692557479261656687786310357431583283956463529307704804294 886.0, 0.00001249490552531246737974341994281619920297760030454609756435288584 887.0, 0.00001418556184765846779140359591231485925866634587677992800680789776 888.0, 0.00001608454057504865876487920916028939631030826058602282784710553814 889.0, 0.00001821463928670276331279604565583892783212438487554890353364165897 890.0, 0.00002060077326531811759315860492133785003829624971772106871456513058 891.0, 0.0000232701260029084599207483357546855811623332786101861217878666616 892.0, 0.0000262523050395364612347911815542906637384697936125930846291082338 893.0, 0.00002957950267927775576639291632893329726188205082342314920029773814 894.0, 0.00003328666103187812150008007420793872151670429141498156715611614945 895.0, 0.00003741164072648020265552625903803360748337712702462055774415311335 896.0, 0.00004199539253586126045352946672168932726299993985550537058005877573 897.0, 0.00004708213103630908280308562929433830351111952849411386216156559532 898.0, 0.00005271950931015673401903427056092338451660529469929799383071713805 899.0, 0.00005895879357580831534828599357689531372986951510869249840681364678 900.0, 0.00006585503650465444263350385298137230843925843648962393084061008139 901.0, 0.00007346724785656499231591616359869356930500600855284856134875850352 902.0, 0.00008185856093675682405353047600837168036357055650293658218428853577 903.0, 0.00009109639324799629958824407321531633915848521225056682617619183742 904.0, 0.0001012525995846708074373493234054370380441779760417017854231075051 905.0, 0.0001124036156907362030583228042412251530184431402846796571454586667 906.0, 0.0001246305904835236855371933904379535360896518867654741197874003431 907.0, 0.0001380195047315869014010488131196113071296219517990984840488066847 908.0, 0.0001526612739690050770244578725221647604396120328704894111851890053 909.0, 0.0001686518333327348885880812965998218152596391480271523428846275162 910.0, 0.000186092201925698054328687202627678502505727252542145613697220955 911.0, 0.0002050885242383318188862522011500264476317358370781384679695166827 912.0, 0.0002257520861073765063502381711714558998948284195887523374500962271 913.0, 0.0002481993026547983660288284990203246286094375437300233566623196068 914.0, 0.0002725516756340012455504004491153402021366824926123836292763863795 915.0, 0.0002989357176168780620012092732406307697650365682990263427948648225 916.0, 0.0003274828404857309060285558352500595368565598900973087478114121773 917.0, 0.0003583292057504818880620049633247610028198654062325429802916234965 918.0, 0.0003916155342956051811079883716098078477608574140229516698436173655 919.0, 0.0004274868732743656015284496902366399104082245305757171639832083889 920.0, 0.0004660923180115804521390892103963997522766414423340349267155778395 921.0, 0.0005075846869513243417005246867769629608600966329984458039598368563 922.0, 0.0005521201478935987210742010048327247736983332542249815154153888064 923.0, 0.0005998577940045168610912909016906053961738335842619628273360126502 924.0, 0.0006509591683582090865238884004400907760050636835522337904969103787 925.0, 0.000705587736075272496862404584160975325645537301397234007637098178 926.0, 0.0007639083034616309926638448084021585456541512578871377316811613253 927.0, 0.000826086383922186464562829756905933000670438322778246017414049081 928.0, 0.0008922875108242578419507421593707997049090832008625772155927932622 929.0, 0.0009626764979147107729217070599835747200359001765769562641251931987 930.0, 0.001037416648349619421915415287209345405140486280687935853221737909 931.0, 0.001116668913873564847672497065174133635547520421833071668867177224 932.0, 0.001200591006184104512777066912467246020154840741316833198500976608 933.0, 0.00128933646303194690092187971798151907743745507299405708900344428 934.0, 0.001383053672134910130921329732901903061761615635157418787086518217 935.0, 0.001481884856519405109028817897861219510916466863671833147124699089 936.0, 0.001585965025442156406052932935640317577326973624978174062305682051 937.0, 0.001695420895582009584366097915595227374123544589996721905194378489 938.0, 0.001810369787721522988234536131567766026503775443545480909318281551 939.0, 0.001930918504654902630340947618861312768730093420483162359951838645 940.0, 0.002057162196556808993525400635406282014878988751921694332165463793 941.0, 0.0021891832205196028791592109974725352701582165986294184115458981 942.0, 0.002327050001408587637055037778675703367643455827187524199112622924 943.0, 0.002470815901589625632148308926440690586448189299228725357638782949 944.0, 0.002620518107445104015172542991076537456519284892301359010384659631 945.0, 0.002776176540906689374385206061054151085482223811111288629427835995 946.0, 0.002937792804490955840338131053547004682583740071674734886093420409 947.0, 0.003105349168521411876936252793330308554121102429879668233594550869 948.0, 0.003278807609352683901886178496396467380149397087487308683290572815 949.0, 0.003458108907475096303497249911450943007784915833444464991845793904 950.0, 0.00364317181436660931014258463472794517982505370549741330301407268 951.0, 0.003833892296870638713769480890082757405788650776378299011673308855 952.0, 0.004030142867709961010658434471178824000419020953071165510456454864 953.0, 0.004231772010496718793503750064974978722706960358086248125167026496 954.0, 0.004438603707265292510291780174045438359118797740709616689224262809 955.0, 0.004650437076138145250106305328132921162306545393508434168121959149 956.0, 0.004867046126235205659333302139596718440132078997407947795175414054 957.0, 0.005088179636356367094125123647870692777826276858706818520182646584 958.0, 0.005313561163306611450029407112050403810798560655282010005080295602 959.0, 0.005542889184997409564405491234459936502983654564956018063555184456 960.0, 0.005775837382650630684523050815702697691269346222155578454059249941 961.0, 0.006012055065557346705557733540775371758071989653362748455679331143 962.0, 0.00625116774090966066691269544601165266145012101044143342552624909 963.0, 0.006492777830235882674599849201167120598263962254452989146752822835 964.0, 0.006736465532935667582453541414886077789374768673802431859471611791 965.0, 0.006981789836340488583544292199594429646166192495346597763397469044 966.0, 0.00722828967062506834901812351350931494884873535922489431736333261 967.0, 0.007475485205776705327119971064798442680306180421861099515209376594 968.0, 0.007722879286701864473278895759318693255297564245358924443175231704 969.0, 0.007969959001423358812473323962722321170394067384462417840903943757 970.0, 0.008216197376207592327680285143438311489287593266587022161969739716 971.0, 0.008461055190370462239325467058956404658035621959106326708692934357 972.0, 0.008703982902453440339097172596074467707736090037998191587852925545 973.0, 0.008944422678448769486310511179789902212052739291434199731049620227 974.0, 0.009181810511795088846054430538329763363337336169830417731317595931 975.0, 0.009415578423972247097394938236429559190420836658870245522303701538 976.0, 0.009645156733706202611109790274590093848837170559855163925836470559 977.0, 0.009869976382060769940098602820584913052659844256276855607151925321 978.0, 0.0100894713000508670980444203050136822983685653629347195638784454 979.0, 0.0103030808048693444070345565236673968381226528406719153202905241 980.0, 0.01051025201038301478081969980618573468403974302921995405798648765 981.0, 0.01071044223722873712723661822496928793370865629401041878109308163 982.0, 0.01090312140763183184828957011300137541353854615545638167759311604 983.0, 0.01108777440998009265277359363360504964606247038097435206273912991 984.0, 0.01126390341821937459254602027409858269633735598086876385323458708 985.0, 0.01143103015129213149409753973480697428819137431034389145015600605 986.0, 0.01158869805811804776887618413488284220628575683235834468810045959 987.0, 0.01173647441401450924574969830026164098965676051790686795641864498 988.0, 0.01187395231497129249381874187178950485242463964720767876549499405 989.0, 0.01200075255682451235664729017351824640020501571102180815441108988 990.0, 0.01211652538711436616967069245350224138144536499026081572009995897 991.0, 0.01222095211825324055638013792482715264374847884173715240879203971 992.0, 0.0123137465915679381134887910787498096729755384639606738722959712 993.0, 0.01239465648280379804068019590584970209025374821078701728269165262 994.0, 0.0124634644407801068573487834133147501724554459527189915669207421 995.0, 0.0125199890520554246358887233562132418821265006275115017302225886 996.0, 0.01256408562568762470017244247272498610909892360229987178634082084 997.0, 0.01259564679344535065886474029907017705720980942337546091199323907 998.0, 0.01261460292213360427701213858897658750677097867599648658056652257 999.0, 0.01262092233602437184817848703258225203996559071900930290435271335 1000.0, 0.01261461134872149971803693647457875764715489427807484086809413053 1001.0, 0.01259571410512534309837488244749920404176917266642600228205415814 1002.0, 0.01256431223548504834060414473220701635645728692364486494356112011 1003.0, 0.01252052432482294025514805106463128441051655666157545104914538711 1004.0, 0.01246450520227499270591118733968148514869200958324889217236891821 1005.0, 0.0123964450561028697821070193123475333837809047407276913373615914 1006.0, 0.0123165683812862440010486663548382988176462121205519641527222745 1007.0, 0.01222513276768943974238308976646384510915175890775966592399534864 1008.0, 0.01212242753780512640326799588129177961274035822120092469436781226 1009.0, 0.01200877224400189160683225495742783279797641572113010086705271546 1010.0, 0.01188451503603511009187516705417279973835066861023813508212742402 1011.0, 0.01175003091031565274483878954008156950524367341535189023005525308 1012.0, 0.01160571985306377712673489640150493515733312240681641778678935385 1013.0, 0.01145200489000222362420658111994202350563160362517889035803480748 1014.0, 0.01128933005566044049024220736082428398588782957595518919856701275 1015.0, 0.01111815829566942552347027505705374874597141597711925749449189822 1016.0, 0.01093896931562346207722504412882771692328889874312956104253981516 1017.0, 0.01075225739017169349246473764763235141297490266331709484052732212 1018.0, 0.01055852914598073229224752882379516279401449226110456063625055139 1019.0, 0.01035830133208206225174029863065589388905972231206204924593276916 1020.0, 0.01015209859088854600930326562946969446587791791039919523726022504 1021.0, 0.009940451242837476530573966138732091230536327930745748735358123979 1022.0, 0.00972389309719870777385415329602534140134406142702826206995535375 1023.0, 0.00950295930108161004416239473407630999982555542096589071823858835 1024.0, 0.00927818423809071511561433924736865206542583894386104802810385759 1025.0, 0.00905009948742430451557622475686217752178349453239249906793326251 1026.0, 0.008819231853490678230566771488488159872459840844955867410168183666 1027.0, 0.008586101475341617229992290699499692374637430808993767255173301898 1028.0, 0.008351220024400089509836349063220859232960435864840972427227965686 1029.0, 0.008115088998098186458003178076061352445116925668728966217596126174 1030.0, 0.007878198116150330582239546066304069543386546868780063536736107323 1031.0, 0.007641023825274664700946351646373440765033152399646970253125481908 1032.0, 0.007404027917250804890994037931514728352435226083995195243643928279 1033.0, 0.007167656264273207733554822506536499477004197953858192204682830653 1034.0, 0.006932337674634383746796695739805690236782233312337486709701186353 1035.0, 0.00669848287085885037185248458330734463217139502035999547748633427 1036.0, 0.006466483591514409331359440874168224675285771602063240908961744771 1037.0, 0.006236711817058927626650686730574253454199381875067632124968951039 1038.0, 0.006009519119244643362214657569635878770748227491168085014121125286 1039.0, 0.005785236132803879443984966921414908991635270532210803896223687515 1040.0, 0.005564172147385095336277059220289659160978790454409325373958662937 1041.0, 0.005346614817000971061154536811920368831618019841076217685347098987 1042.0, 0.005132829983594578029088667932139253941917777076742059688631813149 1043.0, 0.004923061610728865549125680415464744810931391794781569596096866804 1044.0, 0.004717531822861233206752029251972062253066563378973320717464699787 1045.0, 0.004516441045180762174461350764421917189382407832082877614886973896 1046.0, 0.004319968238561991204964911325426113738382827644743799104390035788 1047.0, 0.004128271223826566841696948069926193296235821715210222531989082304 1048.0, 0.003941487089202691829724855611302321045525902024314761369003464032 1049.0, 0.003759732674631489335249663528340667751874496525564184769979998011 1050.0, 0.003583105126388106287009207276839597745981232901982955076059289772 1051.0, 0.003411682515362013753266520648895310733674376668207097535205641616 1052.0, 0.00324552451227348939061114979585939504872716223908094875153788135 1053.0, 0.003084673113089242657729561123365185746460217691606942695623007877 1054.0, 0.002929153407936763623486271134636671998518137421125449297015224216 1055.0, 0.002778974386901125528102244766336116925349874559739585676052533077 1056.0, 0.002634129776216272726336596255197375622869695126533778529109899305 1057.0, 0.002494598898531689895310127084286446318821703929985642654549433339 1058.0, 0.002360347551141022633421488395222900861074483489995348687147724078 1059.0, 0.002231328896297835306539319691267880418267324438832464778549926576 1060.0, 0.002107484358011311082839149120432447512449707685544913225676890468 1061.0, 0.001988744520007357777411052324285632007890458311140252769327073456 1062.0, 0.001875030019854333526615858191311384363067515820234203867717106039 1063.0, 0.001766252434583555141154142825589999866246400606071047842499209336 1064.0, 0.001662315153479096153128946619732698299651340118036959444852105166 1065.0, 0.00156311423406544047856009629276181513050550477175341563463754804 1066.0, 0.001468539237681801987325270424202977312065039842221767057200788521 1067.0, 0.001378474041394997400946596201926242074255601292137193057850769689 1068.0, 0.001292797623365515301353171879413658998455328830338219995086588531 1069.0, 0.001211384819140919265812530125220147488171727179035585744985025513 1070.0, 0.001134107046704250419046017019717980463118390763927838797343476193 1071.0, 0.001060832998450187596825098220992931184242364223481148109619125115 1072.0, 0.0009914292985961643215164192948523798683559746793450561607841472937 1073.0, 0.0009257611248574665180813305835778375799451209785820388241273677497 1074.0, 0.0008636927935228333188042686381394793942379125206102382911085151329 1075.0, 0.0008050883073587981107667278503278511861572491089366838327928854087 1076.0, 0.0007498118660457293605415120518706196970063932321855551169059768415 1077.0, 0.0006977283391052938014210601591402063428395070501445647003275475059 1078.0, 0.0006487037015171346034451000402553164458752396098945717920457243349 1079.0, 0.0006026054324414231700832721850691186230945525249360582519811563026 1080.0, 0.0005593028776633048125922230281078645107828267864257022024516055091 1081.0, 0.0005186675765550125196188507185720017830672283034256769536626913142 1082.0, 0.0004805735545116488628246614685959077790585653252708050711757656302 1083.0, 0.0004448975819575807199521574796349275275714727655036973884247499651 1084.0, 0.000411519401142453563646168060587942513450112102612918949719098438 1085.0, 0.0003803219220495456692033881684262396956273876359251249748731583206 1086.0, 0.0003511913888252010904224130228277318434232625778153314861558058563 1087.0, 0.0003240175182071600534229031194862413530086108961931259668101461871 1088.0, 0.0002986936114825900761612548664194565644401990374625598041309674324 1089.0, 0.0002751166415444258265397530647128726229551080254072963905288632222 1090.0, 0.0002531873166382223730241680137933045128421140624141678810534574955 1091.0, 0.0002328101224021293905583982476264184005866550192738334776330119683 1092.0, 0.0002138933438008463695266008338192159134223233545486687698972620757 1093.0, 0.0001963490685415806259441491426565663636025730621680374674491681081 1094.0, 0.0001800931735371653408363189692783494075912970569053608278726343334 1095.0, 0.0001650452959496624348355258424641705289877790675243551355916219039 1096.0, 0.0001511287903080176845006806172368741126216807584958422674743521417 1097.0, 0.0001382706731466717754790513482071443005394573791166109698510580575 1098.0, 0.0001264015565594478473031393257563940831148830689565249193052490662 1099.0, 0.0001154555720054820697507206654539847596106062286325638617209556237 1100.0, 0.0001053702856423435885021251985039649067037232960510752659951545707 1101.0, 0.00009608660639666004457022930684391702313870566158661237528654257374 1102.0, 0.00008754868791532900338491671139960695548758228824460744516076222857 1103.0, 0.00007970382547150341699663370497101455642421969131297559487378426897 1104.0, 0.00007250234882968336183983333066992158746785589834575080938327205586 1105.0, 0.00006589751200406156423086478236959063089313396812497492233583553028 1106.0, 0.00005984538077433318011516421350648812786456867414630238531963940285 1107.0, 0.0000543047187540093980851379333430319006793956305329993903317468812 1108.0, 0.00004923687273833087244071569093257202478443952791746082641411580552 1109.0, 0.00004460565799256598241987508946482185616543556025095733301520806091 1110.0, 0.00004037724407715145200240992619811416674125898275312750254820778763 1111.0, 0.00003652004174408786305328157439980958290392933881482905279216174647 1112.0, 0.00003300459137948932952827998522864403039408389576519849203179937853 1113.0, 0.00002980345341040742396083525455614697876223371929366891885633943201 1114.0, 0.00002689110104016269873876056947604346685636180240365003689574698968 1115.0, 0.00002424381562554021065232639439954366920337717038018134367036694377 1116.0, 0.00002183958496141793169675897848920546289786782943186335892902731058 1117.0, 0.00001965800469374382586746912377194406596401879232423141990281107727 1118.0, 0.00001768018304027234962649078377029532148434155594692999515536204946 1119.0, 0.00001588864896009969780924148095350231795087447809161290883282009307 1120.0, 0.00001426726387775976167974073759318196593842914641571183295178178575 1121.0, 0.00001280113703539803423265414445740756258390663217574070589766114079 1122.0, 0.00001147654451724812856326067373636012760593815269120795098188142094 1123.0, 0.00001028085196419852161832317694819920398071329936453762287649988703 1124.0, 0.000009202440972545437565094424238746753834370397188173004631156880891 1125.0, 0.000008230639149960280391211003502319930703864759134543140212600691667 1126.0, 0.00000735565378312689353291739407272734775005665564851775454196952045 1127.0, 0.000006568509055288853329012853157855734058813189481647324542757962898 1128.0, 0.000005860986737949177480320840315912795214799542167654447630010269368 1129.0, 0.000005225570269040737912704374574114192002590519046514882656192103864 1130.0, 0.000004655392119890729633741838441211415128691218066215808761690183936 1131.0, 0.000004144184345092566472813213649784128312834896474408975979503729915 1132.0, 0.000003686232202831016701305006232032278893865520179126897205217473986 1133.0, 0.000003276330728141498734771393836043586753779255377084490038722980157 1134.0, 0.000002909744137886204177602846675054792105557106792150148731499757393 1135.0, 0.000002582167943766615683195745556391549743139351807472809301680672367 1136.0, 0.000002289693648337757703194363077703176016759069514583239594411666557 1137.0, 0.000002028775898623614700003337908044316766807475431408689035867530901 1138.0, 0.00000179620197244118088437914011752080033242674988126758270421342663 1139.0, 0.000001589063473814629117531535118218549714728863584312382142204233515 1140.0, 0.000001404730115799821217156645837707795231192950011277057674611110998 1141.0, 0.000001240825471548311612710327817323494820980897083303839690463086863 1142.0, 0.000001095204577431403102543259775106653065213100331333400434273610061 1143.0, 0.0000009659332754377371089053753024358941630929862397490791930156338982 1144.0, 0.0000008512691857780369168827686250030872280644106082162814639354319558 1145.0, 0.00000074964420461016764709353192306638198429885097056209720641035106 1146.0, 0.0000006596484259751296302137345142211498492514734138901158549799321084 1147.0, 0.000000580015391354526446790883930878978922181692199533256577141974528 1148.0, 0.000000509608574672808860593008353071065374117681723822087964411071083 1149.0, 0.000000447409015029065644931686956290620959194594232958809015190142719 1150.0, 0.000000392504013914391104323042902531946659057579198046452994931722621 1151.0, 0.0000003440768181178733910871827424663220627916178279066925703055409469 1152.0, 0.0000003013972139175469243670086584003693642806515415686538759156790559 1153.0, 0.000000263812962467001995641863032327567299985753755816722175789547059 1154.0, 0.0000002307420105024215158367326403913906294438286049511215093978881381 1155.0, 0.0000002016654145908854361827586405234269102872166179410242902050428983 1156.0, 0.000000176120921104390936991255193169959376221133887569426585830813728 1157.0, 0.0000001536971479236972346997846129600069744398718227906368668179025626 1158.0, 0.0000001340283175430147905242268373335188268000338636778520596698422784 1159.0, 0.0000001167894947543185051083119860257625920883947907656150962048813159 1160.0, 0.0000001016922854344087405739401001313719418865732944538635701391722086 1161.0, 0.000000088480956136395491117205162047769652020885391726839538520931945 1162.0, 0.00000007692893719931560545461132527510967366640042482607647331838525977 1163.0, 0.00000006683567493582153213446915006759422155037165633320136784891603876 1164.0, 0.00000005802380114024642930473696960471952787803147485084687914712743894 1165.0, 0.00000005033659068084526861005489861034255891990152436982863999866496155 1166.0, 0.00000004363568030450170469034347772541693573305130481191629105122119009 1167.0, 0.00000003779902399426401175864220060713426219639048982672358665322173694 1168.0, 0.00000003271906228489894706442666141254614314863041833203107187738418294 1169.0, 0.00000002830108486485285874056852090235036607248995005533279381940196493 1170.0, 0.00000002446176758054887436263028229125536530848396544624523673960644725 1171.0, 0.00000002112786661703279747459843284554663149053975515829221710045180058 1172.0, 0.00000001823505416396634749469686277796492048197382892632494500935863653 1173.0, 0.0000000157268812942881008129381951843293978619498421017079398406384731 1174.0, 0.00000001355385509096212367046900152162532420054413362899222601627323163 1175.0, 0.00000001167261826150540127883306928405520996121733925425889099639886208 1176.0, 0.00000001004522058672412094562283305487752929389310530591704102464745578 1177.0, 0.000000008638472565454184604325415291206855901774859002008005788201313023 1178.0, 0.000000007423372547080297468553223119702767972475609873639891696951196442 1179.0, 0.000000006374599493989758858270243390671521018413350926886307709371369763 1180.0, 0.000000005470064292471608435171175915864639326940995536326911374463427718 1181.0, 0.000000004690513238233877044581087312524144688754362981784762678652634281 1182.0, 0.000000004019177966769722442963684802837523296511243064017142606101678429 1183.0, 0.000000003441466684091775879123756293858353153341609219255966376178210762 1184.0, 0.000000002944692084450024831430302868582952801507127663930079366973710985 1185.0, 0.000000002517831822870450299852684750064711435204946619537514480309865945 1186.0, 0.000000002151317845761058542180344631507048188409187092785830557397575132 1187.0, 0.000000001836851276237567766156171290336262860064265225767677724833339597 1188.0, 0.00000000156723990578416101325190861698009249400084961086658171275622931 1189.0, 0.000000001336255663706466089826598900077955020107240379281333224565877122 1190.0, 0.00000000113850972364331528017861966458143268503259215362426223169363831 1191.0, 0.000000000969343165047024032687012703361745003576398260957235932716410997 1192.0, 0.0000000008247313396719714004365209253679825727363367571607978271115367978 1193.0, 0.0000000007012003011784045958107406030455379849779025017198369891274026999 1194.0, 0.0000000005957538422205959794733333890940231367827888053673121948746235292 1195.0, 0.0000000005058098499221482813921815244716004973887296442392356850276375933 1196.0, 0.000000000429144839351894935100606421300718903747443407159739966526855188 1197.0, 0.0000000003638456572467286970294288024359859662332998203482979064855130134 1198.0, 0.0000000003082674663782068298295662693192844624896082532151298892780415257 1199.0, 0.0000000002609972260836091373170376899403915912401061148048316518925835045 1200.0, 0.0000000002208219779050657174087856546083985706316064658919876873460438689 1201.0, 0.0000000001867013282077950940420458387234816466289210257249598370495429484 1202.0, 0.0000000001577435931743927689557698907775476521579109095422660482568561498 1203.0, 0.000000000133185136687720427875434377577338998674728654737674917412655733 1204.0, 0.0000000001123724892169203343981694673590528308414907669676176795278415617 1205.0, 0.00000000009474688671920957331357494637530229217810171193063514801474693461 1206.0, 0.00000000007983091349458786656015999738215602460366468196620760909380866126 1207.0, 0.000000000067216972538823394449571224722895443680598484761963075192307815 1208.0, 0.00000000005655734182303637149571869341116431518612138306706485919625413421 1209.0, 0.00000000004755560561634699141538466078731851507462557332191696003379645016 1210.0, 0.00000000003995927693685411516456205963041532043000301962287266939747337746 1211.0, 0.0000000000335534508912124085494412790727862002133690220844944528729998279 1212.0, 0.00000000002815534942461750246923524338307044525246890674067531832897494644 1213.0, 0.00000000002360963619055207389220806555126998993617383288590032185172608219 1214.0, 0.00000000001978439616575334656333743261119849837800374763057109398355464202 1215.0, 0.00000000001656768854984721817830343677708599101572393705931914616083582431 1216.0, 0.00000000001386459364035527865561594509209214686298239641866654486324056292 1217.0, 0.00000000001159468497484869677871550734500947746548245810807113074213251483 1218.0, 0.000000000009689867271331238699966861373708957024219129931257775742352740861 1219.0, 0.000000000008092528742339699674949700435620470264984238583088324441446887617 1220.0, 0.000000000006753963355319423641793688410322846331690474274268462629301780778 1221.0, 0.000000000005633024691908179639941131683700192818974332564867105808176587096 1222.0, 0.000000000004694978336832645591979627545690814803678967809825390681622350636 1223.0, 0.000000000003910524304509506577218825220503512305168212782229399220038444618 1224.0, 0.000000000003254964977319099054987581046652946398895338259239983696535215605 1225.0, 0.000000000002707497462269182240784642936279571441279212064492046645645065729 1226.0, 0.000000000002250612241213524976845533845975178749712386009990969783132651246 1227.0, 0.000000000001869582554292919697657790454942336122986684965125309166016920324 1228.0, 0.000000000001552031169698569942233696655277152401689326928453595995250357285 1229.0, 0.000000000001287563101484002931389895488957986230456018457423737592996497416 1230.0, 0.000000000001067454481407732423753679903527932757152162910898109203945555736 1231.0, 0.0000000000008843892059773479856502019295196636602738342599459433042871909251 1232.0, 0.0000000000007322361967733090223472654728916591772614927244709193097152436544 1233.0, 0.0000000000006058611575768339418831060294563734886336629877376964418542847246 1234.0, 0.000000000000500967609148830088427505247282608233236040751777265837940096106 1235.0, 0.0000000000004139627519924573517363888581126040422416844534651354205208612156 1236.0, 0.0000000000003418443666962030156890583441040153337608525402554331584070098393 1237.0, 0.0000000000002821055257772759645145995718104904875630201826492454085761673479 1238.0, 0.0000000000002326543735669660181349192378832025465635123718939864604653223105 1239.0, 0.0000000000001917466430612554104979817559158935280849218277990241060648344176 1240.0, 0.0000000000001579289307118610109034545573899712296535858608700880607724488382 1241.0, 0.0000000000001299910504160161461910664240249484629097074902226444299767540435 1242.0, 0.0000000000001069260438654495527348068239193137966541113047662864678777103077 1243.0, 0.0000000000000878966423051175467802462040652384768137236885486461411583778476 1244.0, 0.00000000000007220716011539929204146544164278726366111539757818983167142208665 1245.0, 0.00000000000005927995818691556162822227949638663917217455524018225793566533353 1246.0, 0.00000000000004863574886118976863063741363004961602678499158061967712976397525 1247.0, 0.00000000000003987712774612922766430731847866882095952691046338208663595065634 1248.0, 0.00000000000003267481397073620617197750948517714935520541132604789234151690915 1249.0, 0.00000000000002675616197931856742613012502956028482899388037633989947612855326 1250.0, 0.00000000000002189557697427635385774763280939825635959347242864018161069356924 1251.0, 0.00000000000001790652447373575133456818945060173285575385735024858849027979419 1252.0, 0.00000000000001463487375858688421366230127400653307551444112126761902967472978 1253.0, 0.00000000000001195335661119161831514084238784751360648204237647879480148544984 1254.0, 0.00000000000000975695786230872164416137877880787230107638423626449935118006439 1255.0, 0.000000000000007959083858249685839385813027714561755601543062159847012523888938 1256.0, 0.000000000000006488379883729928974212625419682201796745580552191966062027900635 1257.0, 0.000000000000005286088547608893979175195695961394973607672555202388580831103867 1258.0, 0.000000000000004303858770841526426745604963040723876541781251710964057435706449 1259.0, 0.000000000000003501929828217598987618102018315222771830009442025084941725457128 1260.0, 0.00000000000000284762732871933569301448834190011964339469596666696876324021478 1261.0, 0.000000000000002314118447233208378360252321161645876959604906132768750151309624 1262.0, 0.000000000000001879382459303277124865543950101600489097815889371338157512950904 1263.0, 0.000000000000001525359948284255132582232786435879269510839183018495481529486316 1264.0, 0.000000000000001237250176896504650496276072622859439909110971753357928392885767 1265.0, 0.000000000000001002931233877715391718064228895170587675077802754690494916228217 1266.0, 0.0000000000000008124818423920326045791462755881198591791053738073757451974531879 1267.0, 0.0000000000000006577872860161797979998311750290918414958775222663473054475446585 1268.0, 0.000000000000000532214884959809681826364121796425413258381273365501111859717122 1269.0, 0.0000000000000004303469360434506934434080272154680572094863537740398140900363462 1270.0, 0.0000000000000003477610958683920627916087801173416873731527408966285635819604841 1271.0, 0.0000000000000002808499056337880153175685924789257480731549852607887050311632883 1272.0, 0.0000000000000002266725853227147299089570330094880700455421924610597331356597262 1273.0, 0.0000000000000001828334124112442003703019778430226825391869195905810708941466411 1274.0, 0.0000000000000001473819860103615247482389607060602004318978330225102311736740789 1275.0, 0.0000000000000001187314950537620499877583735139351397727498492058230043978881343 1276.0, 0.00000000000000009559178692515447853198638922232864628783096807378502033652314597 1277.0, 0.00000000000000007691459429064699966556578439449017675190443448895178896976230366 1278.0, 0.00000000000000006184874250429743542069449030799398195434105715605910149058389676 1279.0, 0.00000000000000004970354417734602105443670727375038848662930002202121884716915272 1280.0, 0.00000000000000003991890501011781651940259599288097444349886030019292191436420201 1281.0, 0.00000000000000003204092716607557463460563763832447223468241664316681269117172083 1282.0, 0.00000000000000002570201279860695992179849675135361635387163203940242230891117387 1283.0, 0.00000000000000002060464870714671929211330645804677566498900047673247077098814434 1284.0, 0.00000000000000001650820005304455009299863410162299502256881684908340292375016536 1285.0, 0.00000000000000001321816204910257554572048829530075325494362306860046383358422259 1286.0, 0.00000000000000001057741806559190206585528499385693906590702177624854319811794595 1287.0, 0.000000000000000008459134413903467969784444541141245800878887238236805633333666028 1288.0, 0.000000000000000006760989278669555859622896591714920925746574012131173328569452482 1289.0, 0.000000000000000005400488437992380574372506371693870529179464905154493459505826176 1290.0, 0.000000000000000004311165663310757071901800317446210012890144645388664008576191154 1291.0, 0.000000000000000003439502780917825883145389288555068502675442780580193638143204895 1292.0, 0.000000000000000002742434756012650132631168934968509183979498859242933508469395096 1293.0, 0.000000000000000002185330023831546420015454579181181133743091742592777365206278249 1294.0, 0.000000000000000001740356627392023708169118290122523201300400360219299131211435583 1295.0, 0.000000000000000001385161340535520109084800912659849023608683111933496811908350745 1296.0, 0.000000000000000001101802527491238320855878088053812878197283740541664681641680248 1297.0, 0.0000000000000000008758885666472334404666738520564983322014755722635732028361914789 1298.0, 0.0000000000000000006958826991766922163005358045945374057582510690169801272368282409 1299.0, 0.0000000000000000005525425243739118470683601317481603580801716794686350481365742575 1300.0, 0.0000000000000000004384683581309868278682579707079696714676301260413242138225615446 1301.0, 0.0000000000000000003477395491855915381053609048171724771814455695551432299689368693 1302.0, 0.0000000000000000002756218146860656217688591861861024016754708206480009019342916421 1303.0, 0.0000000000000000002183318802090707071237725462148452761837233334697724724860880847 1304.0, 0.0000000000000000001728483270686157346014636799926307555787462871466735454764005252 1305.0, 0.0000000000000000001367596739590287117749086101607312562320683890434800438828504144 1306.0, 0.0000000000000000001081424425078900494391524101980626369883934272405709415095456159 1307.0, 0.00000000000000000008546335215524245506455364872703563494812301776207680419725361559 1308.0, 0.00000000000000000006750092009486464036683802620260107163240903265017112720150951237 1309.0, 0.0000000000000000000532826566843760759268576498806115503011813477345533906796649857 1310.0, 0.0000000000000000000420347863832343561853469205248405863467525281811491789600667594 1311.0, 0.00000000000000000003314202198714875716188018838035779399159880839546181367326632386 1312.0, 0.000000000000000000026115402600712777349384635090832263604724082297803594998910863 1313.0, 0.00000000000000000002056659529930348810626201931992799438055029282450805299792624326 1314.0, 0.00000000000000000001618737454323750123744919739901467055668840195853689554194389408 1315.0, 0.00000000000000000001273324654325952760769849263332565523734984272659250904474381927 1316.0, 0.00000000000000000001001038963425906723452518253744848063760614481150819779103004876 1317.0, 0.00000000000000000000786524577503282064732954640114031503539767951949056622739066074 1318.0, 0.000000000000000000006176230235451062361408859447868969378945216072975741775157207817 1319.0, 0.000000000000000000004847132574183548962324079589517428744045516447423302225884082078 1320.0, 0.000000000000000000003801867177410856154784226268408436645221997393815119336384268033 1321.0, 0.000000000000000000002980299984816824763409270085795239045951857527579851940760681835 1322.0, 0.000000000000000000002334932855221725277163301875684741708806546354273556659807791204 1323.0, 0.000000000000000000001828270968987822040477686478506546278243180927633814764659176799 1324.0, 0.000000000000000000001430733863868207333615857658778998896192952603675931207460460048 1325.0, 0.000000000000000000001118998876822464000288481991574719918083423793242052689395018322 1326.0, 0.0000000000000000000008746883029880908753498673729270750850971166081274682620022102277 1327.0, 0.0000000000000000000006833296003528720502283207362679738033441535903267689258911922027 1328.0, 0.0000000000000000000005335323621722108967421593078685046276188325994467814642010450975 1329.0, 0.0000000000000000000004163372700960709760068233140808032385095318078811339911928181781 1330.0, 0.000000000000000000000324701408600598689916717016569327432334626157617943603929890302 1331.0, 0.0000000000000000000002530916305874211009881058247753844307207455913690632024864324453 1332.0, 0.0000000000000000000001971634878056066933668499583825066693583048778253922597506598385 1333.0, 0.0000000000000000000001535078769285114287697299680892645151884696422594603212906082328 1334.0, 0.0000000000000000000001194512420472616841490897267951542255638399027017828717336133884 1335.0, 0.00000000000000000000009289810860977999518579669666519264616422118178520628904212717423 1336.0, 0.00000000000000000000007220705567613795486143477939750428882615653783707519831914630873 1337.0, 0.00000000000000000000005609308525903466387039440271498770511392593669532034134355134233 1338.0, 0.00000000000000000000004355081730455093894593072902831252701346402047285800718422607131 1339.0, 0.00000000000000000000003379410463377879029325087662608296057813936089809001453869097704 1340.0, 0.00000000000000000000002620858620134573111924543480395407930696082271241570428479364458 1341.0, 0.00000000000000000000002031442898655036851151096813378789915906185984448246937229021745 1342.0, 0.0000000000000000000000157370882714825394700695623281944109736162080829663757965805814 1343.0, 0.00000000000000000000001218437446634140024739846968996224782100820559964751945094213498 1344.0, 0.00000000000000000000000942847706522806734723560365904179401171877900450696585432709206 1345.0, 0.00000000000000000000000729188267093579275838432248913864042198421014428598167906953235 1346.0, 0.000000000000000000000005636350136711755312141170822016312689266423847064427043377944231 1347.0, 0.000000000000000000000004354284298561237307595826256277583462637603121387458281558286204 1348.0, 0.000000000000000000000003361990480408110400346659980238144998721536606979850837993299379 1349.0, 0.000000000000000000000002594402849205472603619404514845354833198850519762382196946636433 1350.0, 0.000000000000000000000002000967040033465857657441978997890692153401828304402141532441369 1351.0, 0.000000000000000000000001542426221085189888635923506840255127972607193859785181791319007 1352.0, 0.000000000000000000000001188313849323320993146409718000082258560609889558964552993915651 1353.0, 0.0000000000000000000000009149988827854547850024786634284485500429263330592614378541875969 1354.0, 0.0000000000000000000000007041626176093393996536313849302630066095702567942197415121758565 1355.0, 0.0000000000000000000000005416125438977492034363393439120350511760684556704962516301945471 1356.0, 0.0000000000000000000000004163591934495451190601573051855759956108320332581450341538864388 1357.0, 0.0000000000000000000000003198980901185104842014244361997005919320163600569520418404497275 1358.0, 0.0000000000000000000000002456515607584852910877559761325998548527037450228492308293506109 1359.0, 0.0000000000000000000000001885350702492247816722306750092058999099753519517588140607987921 1360.0, 0.0000000000000000000000001446204975339774272751490357273764104159312899883060072774964563 1361.0, 0.0000000000000000000000001108748347789528448245917024064386699943954933276048662129620522 1362.0, 0.00000000000000000000000008495753338470234016483686691080415714160337785942599460277910882 1363.0, 0.0000000000000000000000000650634252030651655504235749168993944410444404447974629755273077 1364.0, 0.00000000000000000000000004980103996590308230730690756874272845179649896796056444754649116 1365.0, 0.00000000000000000000000003809839720353463646175009546384545327156146175487572573394483053 1366.0, 0.00000000000000000000000002913011131442871256993644107695889415763120583439456220243412969 1367.0, 0.00000000000000000000000002226102048937755049969925840117404523688399479573875194295767611 1368.0, 0.00000000000000000000000001700261891426247986652875904669038215159292681207573552168642977 1369.0, 0.00000000000000000000000001297940362868255745417676541695578735699085895018759310558221997 1370.0, 0.00000000000000000000000000990289466697671068794106074315965557721526259196541916356825297 1371.0, 0.000000000000000000000000007551590357151738263686070791135218328503383707742797410287636469 1372.0, 0.000000000000000000000000005755510704260054545757341920808747629277308465850966829943690418 1373.0, 0.000000000000000000000000004384285470014227561353014371710040428575232336655650042881944818 1374.0, 0.000000000000000000000000003337979257501234193007198870769099801479890472262678030132443902 1375.0, 0.000000000000000000000000002540028730606862959162929180520912562086792855034569460078627003 1376.0, 0.000000000000000000000000001931808820021322269888701941697474410680884519726443465319052271 1377.0, 0.000000000000000000000000001468454565206672132704520326106521884330249671958827859741205997 1378.0, 0.000000000000000000000000001115650255472635921228318780574946330939043527920370190966087769 1379.0, 0.000000000000000000000000000847163318611162791489715341517480719455235183546584499792383805 1380.0, 0.0000000000000000000000000006429512814153093462977850719261454713572603278596985792968776451 1381.0, 0.0000000000000000000000000004877094486971915007116664460967144954915836998814744269108572287 1382.0, 0.0000000000000000000000000003697573219865426959510450555828620630579304385832263988393436828 1383.0, 0.0000000000000000000000000002801852281979525293335809974673889402644487238594606705778382855 1384.0, 0.0000000000000000000000000002122007206106807503047785592518243314624473055389284044481236538 1385.0, 0.0000000000000000000000000001606282651873646147091460864218827097501237317187348530507825183 1386.0, 0.0000000000000000000000000001215264762712527737788734242773473174366435957601166961349023355 1387.0, 0.00000000000000000000000000009189544608141959659103001556262285792362803172019509699597080423 1388.0, 0.00000000000000000000000000006945308622675414482329650103918981837771131266530822563727599056 1389.0, 0.00000000000000000000000000005246430282976002098933101167028707617512812078727988803222295716 1390.0, 0.00000000000000000000000000003961059683288647678889451492526931539058854969056925577629332702 1391.0, 0.00000000000000000000000000002989057795455595118536044616400070531922311186244170756059005855 1392.0, 0.00000000000000000000000000002254410548259534428076538448802485444585794819032669948775749015 1393.0, 0.00000000000000000000000000001699447662184711550447005208069188480795038065738450990715103291 1394.0, 0.0000000000000000000000000000128043929310999764564790784239347595478300199672493384908228922 1395.0, 0.000000000000000000000000000009642439548950707667377225568489655831170447914286167212549040508 1396.0, 0.000000000000000000000000000007257581304188374497483025597370994299116728996109722765343763476 1397.0, 0.000000000000000000000000000005459768984578902479689636967873226958660287756399788826377004378 1398.0, 0.000000000000000000000000000004105199954073268162855063055854236959360401183136780802415040161 1399.0, 0.000000000000000000000000000003085122211914519404076253375254198709366259149363995428873450399 1400.0, 0.00000000000000000000000000000231733465210912973547219812079365788256324456903909494172668892 1401.0, 0.000000000000000000000000000001739737728833793229004068283114172497770861219321999098980594896 1402.0, 0.000000000000000000000000000001305442557206297612445730658456006518823374053539709361709775985 1403.0, 0.0000000000000000000000000000009790638715052567302416007954246668720775762909381537948186357964 1404.0, 0.0000000000000000000000000000007339118021183641469326076017762742851123224232073302608820232349 1405.0, 0.0000000000000000000000000000005498656885130244885977405658428128304337767010043054568896402774 1406.0, 0.0000000000000000000000000000004117651480687465659711582534575114453849126817882201610573553349 1407.0, 0.0000000000000000000000000000003081932178287984812231419231989846133854275098820567242836394568 1408.0, 0.000000000000000000000000000000230556520562479032231191999257179235156205671458444730584267305 1409.0, 0.0000000000000000000000000000001723903265926377484561198756129170285754935423285041384365612318 1410.0, 0.0000000000000000000000000000001288338060371864270248257371659253535905007757778606228742477887 1411.0, 0.00000000000000000000000000000009623400336820830050588407504040776604110788089517022874677376026 1412.0, 0.00000000000000000000000000000007184711759614864009355058319945660808256414810805312690016114354 1413.0, 0.00000000000000000000000000000005361329945530628911430107041303530560951969366381489775995684099 1414.0, 0.0000000000000000000000000000000399869627478823234043159984533466776771435706544728918526069991 1415.0, 0.00000000000000000000000000000002980899301115118567035755758630466954317512653135220175162593141 1416.0, 0.00000000000000000000000000000002221055972409158307897264481947735504608274159964384691428146322 1417.0, 0.00000000000000000000000000000001654075458882544101712247460761458504473808895912469321088991101 1418.0, 0.00000000000000000000000000000001231218281075136422321769526745716058810015089058898360083582155 1419.0, 0.000000000000000000000000000000009160074780886126153765958530906535435291246730621484883197242547 1420.0, 0.000000000000000000000000000000006811574546209272935405473127597961152487161776413271863502584259 1421.0, 0.000000000000000000000000000000005062684259081092526330099127082936446694425626903303896294416866 1422.0, 0.0000000000000000000000000000000037609651468131335580343434585531278201698895366659966062167635 1423.0, 0.000000000000000000000000000000002792564505885407203405392749822608246184232161606002802157376316 1424.0, 0.000000000000000000000000000000002072491982640732730380159023234794871777951571317929259511971551 1425.0, 0.000000000000000000000000000000001537335110386759290095205520936364047479906882035927454930852432 1426.0, 0.0000000000000000000000000000000011398050430226761484850197031087808105203609758699583773642262 1427.0, 0.0000000000000000000000000000000008446547787013679039158582175325624783793854201530571428072082692 1428.0, 0.0000000000000000000000000000000006256260918198769337459507162316312339284849275487930998252317815 1429.0, 0.0000000000000000000000000000000004631670974246147701064122393626420475507227644338467062082654684 1430.0, 0.0000000000000000000000000000000003427268140418785662468599016281183164999915560471072289040649277 1431.0, 0.0000000000000000000000000000000002534815186142741751862638617761780973201055129963009563582785625 1432.0, 0.0000000000000000000000000000000001873840607844108396741871854389734544687986883659539496608137158 1433.0, 0.0000000000000000000000000000000001384546089846109383424993960173730534822557751678867202214269674 1434.0, 0.000000000000000000000000000000000102251787559084618208040782834143515684030053188773848903395952 1435.0, 0.00000000000000000000000000000000007547852691890633561116436581172880126523588463629526678485806533 1436.0, 0.00000000000000000000000000000000005568846295294075204697293495121211722066862412226328168922002539 1437.0, 0.0000000000000000000000000000000000410673471107917775018700116447687914665573878417131660325373906 1438.0, 0.00000000000000000000000000000000003027038619565402428573713472996675625466789970667725492362910748 1439.0, 0.00000000000000000000000000000000002230126312157109315658003186873267123791562539573142133815440723 1440.0, 0.00000000000000000000000000000000001642220390985856153469168058953394830449134766391766478240850294 1441.0, 0.00000000000000000000000000000000001208715770313566412918442187518511219304744906008639510740186704 1442.0, 0.000000000000000000000000000000000008892174903376278771129421809510845705564245704847831257758259091 1443.0, 0.00000000000000000000000000000000000653857566616338117962710092434136807377315190020816437948231997 1444.0, 0.000000000000000000000000000000000004805625793499540480392534659423829497339758457108544998021858873 1445.0, 0.000000000000000000000000000000000003530276104965108913219362323491098405795277347254929921957149188 1446.0, 0.000000000000000000000000000000000002592146908629015774648132858349693580897848221310606861529346958 1447.0, 0.000000000000000000000000000000000001902405320073661395044820802536757664424494992305035870549794292 1448.0, 0.000000000000000000000000000000000001395530334768418222731616779175671539705867122611831881634453148 1449.0, 0.000000000000000000000000000000000001023218976119455541537318653826423158565207465154270169157180539 1450.0, 0.0000000000000000000000000000000000007498791024414532997801929268669056128643735868771786508991298619 1451.0, 0.0000000000000000000000000000000000005492974229879754287704455655007201425693501371213706794505771866 1452.0, 0.0000000000000000000000000000000000004021775295547225142938792593634344606947319376120305351853431738 1453.0, 0.0000000000000000000000000000000000002943216816029102688100890088168104306428823289308823145232832281 1454.0, 0.0000000000000000000000000000000000002152886859781673362090232614103437504152703431491453441040762607 1455.0, 0.0000000000000000000000000000000000001574037001382449909033245231587029294348846067263280911157781943 1456.0, 0.0000000000000000000000000000000000001150280423847337572458122695771178452449909294515245110354785687 1457.0, 0.0000000000000000000000000000000000000840210032766238659408994655951925906335449787524476392080360243 1458.0, 0.0000000000000000000000000000000000000613433685746241316905609998908459460990583232423411330653984348 1459.0, 0.00000000000000000000000000000000000004476548381213907045747658228577745473405229515728901114186652378 1460.0, 0.00000000000000000000000000000000000003265240148092019219218740967915014875880768708443486810202058058 1461.0, 0.00000000000000000000000000000000000002380583590080033804871122538359272531332334717260125973619637221 1462.0, 0.00000000000000000000000000000000000001734796289314543835434713279657172744126764252963274809736710797 1463.0, 0.00000000000000000000000000000000000001263602721482588101237202938979056815860284682864954663500120662 1464.0, 0.000000000000000000000000000000000000009199620602281302190551804788580119256438066652142198562330835071 1465.0, 0.000000000000000000000000000000000000006694634249821737958830612324948272205918642918754493790143717866 1466.0, 0.000000000000000000000000000000000000004869469416313378223448285831609362647888417081895188639574615149 1467.0, 0.000000000000000000000000000000000000003540255084879857353306602353998090616617251258354804050711284304 1468.0, 0.000000000000000000000000000000000000002572680580780764822935496940847971055394168413933674104732658806 1469.0, 0.000000000000000000000000000000000000001868683954184682136553240211762981655851426571954730631559663191 1470.0, 0.000000000000000000000000000000000000001356703025561928491288292376906025658490405302642957528546088362 1471.0, 0.0000000000000000000000000000000000000009845390650710929980617491142992791059197007628458811341055627266 1472.0, 0.0000000000000000000000000000000000000007141354756170251502205372861609520492589713574745203725927345873 1473.0, 0.0000000000000000000000000000000000000005177594451201540593321312356697533598218351608096575007871594405 1474.0, 0.000000000000000000000000000000000000000375210939479144336297884759972731608213541394706066027463513421 1475.0, 0.0000000000000000000000000000000000000002717836109535756898120484411185585622675750900593504247452391214 1476.0, 0.0000000000000000000000000000000000000001967757689583714154606255294790639319056758044168084743256511384 1477.0, 0.000000000000000000000000000000000000000142403552417259460592891993742771425549969192414148726443636709 1478.0, 0.0000000000000000000000000000000000000001030080461569933881699492002158511279365444913596832578583274861 1479.0, 0.00000000000000000000000000000000000000007447711855032841764738687130731450426594089254569412901731368298 1480.0, 0.00000000000000000000000000000000000000005382403356577697143485990577629641352524224167702491671771570065 1481.0, 0.00000000000000000000000000000000000000003888047319220923405854132916723885090884218576420083373227972299 1482.0, 0.00000000000000000000000000000000000000002807301365510087164878972101464122015944350641890494270741786235 1483.0, 0.0000000000000000000000000000000000000000202604455940677393220847178854116397877607720600909508907614948 1484.0, 0.00000000000000000000000000000000000000001461543263384473274706738234715519914553808461289440119827735007 1485.0, 0.00000000000000000000000000000000000000001053846484506572538912610739257457718985586843467525664376918761 1486.0, 0.000000000000000000000000000000000000000007595323652233928181240865158843480395823616647612808853423965508 1487.0, 0.000000000000000000000000000000000000000005471655438698788667344226926807809087474095065497646913089329446 1488.0, 0.00000000000000000000000000000000000000000393998877457382012668486085771961257285672094138407576868553634 1489.0, 0.000000000000000000000000000000000000000002835797911639274089321524796819520558761722734131424671547033032 1490.0, 0.000000000000000000000000000000000000000002040139600503246555830373383645188539271708251046454322818071559 1491.0, 0.000000000000000000000000000000000000000001467064032106128318647416100402826542014987984636270080825185266 1492.0, 0.000000000000000000000000000000000000000001054491520350871500573010211543235250067339355967864762317041561 1493.0, 0.0000000000000000000000000000000000000000007576039208402830624661679884736627512604284314396353138079914213 1494.0, 0.0000000000000000000000000000000000000000005440598736306850638586031541568238133505786412780150276699022147 1495.0, 0.0000000000000000000000000000000000000000003905321571375078842027251399117091490765556705551693246827785347 1496.0, 0.0000000000000000000000000000000000000000002802030030696987929920160580322493715886987459021256509029549937 1497.0, 0.000000000000000000000000000000000000000000200953193093612421592032817223501548491178998374159952669301852 1498.0, 0.0000000000000000000000000000000000000000001440533747144269902883496674621456329007329608520661336354001467 1499.0, 0.0000000000000000000000000000000000000000001032187559304218002449255842444787907275142715185962011613845848 1500.0, 0.00000000000000000000000000000000000000000007392659776002362977062383463597476942527673033767645934351926317 1501.0, 0.00000000000000000000000000000000000000000005292367479356012427828342539304784008059550131466003283491755425 1502.0, 0.00000000000000000000000000000000000000000003787099316866029389100975045106056456338783760333578308690336291 1503.0, 0.000000000000000000000000000000000000000000027087634676943731279455021494829940478426069249010802961278663 1504.0, 0.00000000000000000000000000000000000000000001936615550257161596635873683392689932742600008046755992584012618 1505.0, 0.00000000000000000000000000000000000000000001383961291659071700157508433299822002965487993944205502294952017 1506.0, 0.000000000000000000000000000000000000000000009885825139413160160982287389790650528476373642850145649005430243 1507.0, 0.000000000000000000000000000000000000000000007058470630190981953438613550365443037682481864847071757871902095 1508.0, 0.00000000000000000000000000000000000000000000503752552815773183215579867243785684337148142561265895400295871 1509.0, 0.000000000000000000000000000000000000000000003593628026846905295189403240087356540239298002435247649538413063 1510.0, 0.000000000000000000000000000000000000000000002562467995208726865933932648770027355601625334668358247250981012 1511.0, 0.00000000000000000000000000000000000000000000182638989964183698432193023991268911047093718448368647783760816 1512.0, 0.000000000000000000000000000000000000000000001301183400271489454440694179810463933489728624468221025069978407 1513.0, 0.0000000000000000000000000000000000000000000009266031097841531748052153799967179289096782020596856358899255592 1514.0, 0.0000000000000000000000000000000000000000000006595678302748027577706594402363688162066270723993969668720778809 1515.0, 0.0000000000000000000000000000000000000000000004692841647986505016980796265282727349046122812126363171123351813 1516.0, 0.0000000000000000000000000000000000000000000003337515622651023832890190320606037607581164454452881855560296953 1517.0, 0.0000000000000000000000000000000000000000000002372585853376095745104821610095243985597958154547097663290587367 1518.0, 0.0000000000000000000000000000000000000000000001685900877216030465887575925268899480391774820079127237617702271 1519.0, 0.0000000000000000000000000000000000000000000001197440259914486446700303646334282639595618300447000784322137086 1520.0, 0.00000000000000000000000000000000000000000000008501345014861887835393553049524187322671349354336624233919331445 1521.0, 0.00000000000000000000000000000000000000000000006033004398745597466854396754590121504322776334572835415855895535 1522.0, 0.0000000000000000000000000000000000000000000000427949086074774322007343507435671658035384466949961785889467222 1523.0, 0.00000000000000000000000000000000000000000000003034333220912930748005063899349160644381612225576258034349018243 1524.0, 0.00000000000000000000000000000000000000000000002150539351590247460335976976035636653983223715818919633751260821 1525.0, 0.00000000000000000000000000000000000000000000001523507913941542793107624301935500369477469354975786524484583644 1526.0, 0.0000000000000000000000000000000000000000000000107883612078499809065366761461252550514007330594856536160061413 1527.0, 0.000000000000000000000000000000000000000000000007636246357241916022175357836118529817064630189497380900190039904 1528.0, 0.000000000000000000000000000000000000000000000005402792833557102062162870342978814305963744567481053502009472802 1529.0, 0.000000000000000000000000000000000000000000000003820945777343499725873387175133475012740691008221926056394638513 1530.0, 0.000000000000000000000000000000000000000000000002701082521718759651587509686114201695628971998145195565371788871 1531.0, 0.000000000000000000000000000000000000000000000001908620093003030667819388317662727829094780200658798728005164776 1532.0, 0.000000000000000000000000000000000000000000000001348081141634033771153392101574277218061840762915630883338298665 1533.0, 0.0000000000000000000000000000000000000000000000009517605941736353886276970507658019579527132241629228842411354664 1534.0, 0.0000000000000000000000000000000000000000000000006716682068516422316411638723770245215921394614918225135940746887 1535.0, 0.0000000000000000000000000000000000000000000000004738026547404225708365262409916398458655002742281008699023202457 1536.0, 0.0000000000000000000000000000000000000000000000003340842903399552542000874553679697567572586842812995484505340978 1537.0, 0.0000000000000000000000000000000000000000000000002354673655211720244147366699990558824988455177590394600010798558 1538.0, 0.0000000000000000000000000000000000000000000000001658905857477797296775134551580676292524597751698169214682443714 1539.0, 0.0000000000000000000000000000000000000000000000001168232651580067130338773047158083210822345508664365247982394759 1540.0, 0.00000000000000000000000000000000000000000000000008223444438842278360914899791142955407272230408880340753191527505 1541.0, 0.00000000000000000000000000000000000000000000000005786224031098426537703552345961630914718385747150474592849760192 1542.0, 0.00000000000000000000000000000000000000000000000004069621549183941128898941537432791727441769772660803937395666224 1543.0, 0.00000000000000000000000000000000000000000000000002861082218455835633520004952880446228794966306893205659967340067 1544.0, 0.0000000000000000000000000000000000000000000000000201059418870818262913983904775928381871690278137106267088901968 1545.0, 0.00000000000000000000000000000000000000000000000001412331073684783404939275181220597572567733545338620000513694325 1546.0, 0.000000000000000000000000000000000000000000000000009916692533447705321623651018895386386673512901726634429873914002 1547.0, 0.00000000000000000000000000000000000000000000000000696010288112681680655952754169004939323803716152946123691469807 1548.0, 0.000000000000000000000000000000000000000000000000004882960174211021583360209763494553656592814427267654121855753338 1549.0, 0.000000000000000000000000000000000000000000000000003424283011073116030696473141771213696339556955571278503743491964 1550.0, 0.000000000000000000000000000000000000000000000000002400354025005173775673641620654851452521749689676022188138632939 1551.0, 0.000000000000000000000000000000000000000000000000001681900740248336950159208467727062377188227873151681273580593527 1552.0, 0.000000000000000000000000000000000000000000000000001177999400052549746259213631198626653947243793688193033063384981 1553.0, 0.0000000000000000000000000000000000000000000000000008247259406836810698791153824436908931209141343408992600465108394 1554.0, 0.0000000000000000000000000000000000000000000000000005771575094060784966419498305062807265459141594342387703217864766 1555.0, 0.0000000000000000000000000000000000000000000000000004037377773326923860221338752842002619534505514950999279608114062 1556.0, 0.0000000000000000000000000000000000000000000000000002823091779583025775593403434502588025572895181160267156692064417 1557.0, 0.0000000000000000000000000000000000000000000000000001973201377878244979258633107760233005060682035850151483875575629 1558.0, 0.0000000000000000000000000000000000000000000000000001378601846363840858231947197493309223854762416202531589236909432 1559.0, 0.00000000000000000000000000000000000000000000000000009627811180414791195384524126679759697352905377462120922345199856 1560.0, 0.00000000000000000000000000000000000000000000000000006721059883399059708127382700404755377634332688397938872452114815 1561.0, 0.00000000000000000000000000000000000000000000000000004689965913024025950140241775581252084723285417943301368263691481 1562.0, 0.00000000000000000000000000000000000000000000000000003271323976467299331887577062389705727633056364092278500981390167 1563.0, 0.00000000000000000000000000000000000000000000000000002280865122199030841149798424148561026251604342128319423088025534 1564.0, 0.00000000000000000000000000000000000000000000000000001589637342366258834626764303471407436072567576889401077290806079 1565.0, 0.00000000000000000000000000000000000000000000000000001107437281664727253716770079303448845567778185817482636741112108 1566.0, 0.000000000000000000000000000000000000000000000000000007711930047747277541507788226079687943780590345763712269424430258 1567.0, 0.00000000000000000000000000000000000000000000000000000536821755062096689203786583591281247229787279014460046219278254 1568.0, 0.000000000000000000000000000000000000000000000000000003735256615847049568056188540602913817980163303598668358767272384 1569.0, 0.000000000000000000000000000000000000000000000000000002597971096237545203655972324075696703261298761556253351009755516 1570.0, 0.000000000000000000000000000000000000000000000000000001806225405599198976520146532950786156554203260839681875185277196 1571.0, 0.00000000000000000000000000000000000000000000000000000125525963273773498767756465807024419117102239396403074126766908 1572.0, 0.0000000000000000000000000000000000000000000000000000008720058282347534926278933898897510292963020286193043110739648109 1573.0, 0.0000000000000000000000000000000000000000000000000000006055216068693610897306916389679272129746754367740144617975796309 1574.0, 0.0000000000000000000000000000000000000000000000000000004203050006916943587262057266085421981497149409983228291043782813 1575.0, 0.0000000000000000000000000000000000000000000000000000002916247286755804259428948224060914501622784396676769521523516945 1576.0, 0.0000000000000000000000000000000000000000000000000000002022596441060398647917801710483000991965316268571290402523792513 1577.0, 0.0000000000000000000000000000000000000000000000000000001402230638090505447866036498377497471827708904868179742879038949 1578.0, 0.00000000000000000000000000000000000000000000000000000009717514762398348039565852302608406101597623969605208630385695068 1579.0, 0.00000000000000000000000000000000000000000000000000000006731575651947900187968739880228947224531939520375672120989945011 1580.0, 0.00000000000000000000000000000000000000000000000000000004661269768244350513368084803779344822448221191177127091930102532 1581.0, 0.00000000000000000000000000000000000000000000000000000003226398084070266088738511097107702924680258385833836850312504152 1582.0, 0.00000000000000000000000000000000000000000000000000000002232328425547481293556911310429850617800741020166577549044757195 1583.0, 0.0000000000000000000000000000000000000000000000000000000154392028227146882948386198796323583661222934033337614657064046 1584.0, 0.00000000000000000000000000000000000000000000000000000001067378668826621612588639869764900020690538950376586152537043752 1585.0, 0.000000000000000000000000000000000000000000000000000000007376311497231024468257938890109849741151622588490473245596206932 1586.0, 0.000000000000000000000000000000000000000000000000000000005095505537747985819929454020533047838920365064589488996626345308 1587.0, 0.000000000000000000000000000000000000000000000000000000003518542697036971209243332917135778042584567084958839647958128799 1588.0, 0.000000000000000000000000000000000000000000000000000000002428656580034411699176138287068320381285149852938545330155042442 1589.0, 0.000000000000000000000000000000000000000000000000000000001675704144403531399326564794933040941118787405992355302263992977 1590.0, 0.000000000000000000000000000000000000000000000000000000001155730961089636169134258252812147511080022694196927977041883387 1591.0, 0.000000000000000000000000000000000000000000000000000000000796791214244966195634344578234555156866135142172308097807209483 1592.0, 0.0000000000000000000000000000000000000000000000000000000005491119892355796734792761338736259347698307469194510142825017007 1593.0, 0.0000000000000000000000000000000000000000000000000000000003782736859741806931721501526148564586265485743399689034774295424 1594.0, 0.0000000000000000000000000000000000000000000000000000000002604835890373462144257030155152457349205861198856479739097089212 1595.0, 0.0000000000000000000000000000000000000000000000000000000001793014691142748575775339018920140014376562931037110256784514052 1596.0, 0.0000000000000000000000000000000000000000000000000000000001233720519220138802274832292580987530363561281295943050540369025 1597.0, 0.00000000000000000000000000000000000000000000000000000000008485538352813271358585919054354350472499613666474530378285672922 1598.0, 0.0000000000000000000000000000000000000000000000000000000000583407354337579410021606266636114650057989628574569283060876749 1599.0, 0.00000000000000000000000000000000000000000000000000000000004009539507092461620612091555327367275688949424792578608403271245 1600.0, 0.00000000000000000000000000000000000000000000000000000000002754529360872150040312661142421604745574847781425555466562492306 1601.0, 0.00000000000000000000000000000000000000000000000000000000001891606676220794885533900243957021138299040474722676214667380331 1602.0, 0.00000000000000000000000000000000000000000000000000000000001298509123322972212069205516155782254180778716795885989114432641 1603.0, 0.000000000000000000000000000000000000000000000000000000000008910254704207367714981173134641374695364374954633420503316169867 1604.0, 0.000000000000000000000000000000000000000000000000000000000006111761371585020679576361353758297485287862014251576178499513193 1605.0, 0.00000000000000000000000000000000000000000000000000000000000419057895167808012507457009452492615551573957525770764282578681 1606.0, 0.000000000000000000000000000000000000000000000000000000000002872190510316920689617907552668928777947875801317615216319165214 1607.0, 0.000000000000000000000000000000000000000000000000000000000001967814905722459931596450261973363535802400268722826910595530672 1608.0, 0.000000000000000000000000000000000000000000000000000000000001347681433998831811846589011409388174167850935164073516902108682 1609.0, 0.0000000000000000000000000000000000000000000000000000000000009226191198936753640576148328343221989522875343334409380819538047 1610.0, 0.0000000000000000000000000000000000000000000000000000000000006313789077091641330242242029448504657322220916603954435160467027 1611.0, 0.0000000000000000000000000000000000000000000000000000000000004319070673560424059210336172150167771781778240275735774234867236 1612.0, 0.0000000000000000000000000000000000000000000000000000000000002953407554526482649918621117132261485321925014508474569000806255 1613.0, 0.0000000000000000000000000000000000000000000000000000000000002018782305407099569589387932628238721186277459157208478148421013 1614.0, 0.0000000000000000000000000000000000000000000000000000000000001379395616969993627761245630250379627078825896530595367964229457 1615.0, 0.00000000000000000000000000000000000000000000000000000000000009421534538993732978040431082180006025405056715532923483329763531 1616.0, 0.00000000000000000000000000000000000000000000000000000000000006432623075714808613744765283176843649789139058778456024468697628 1617.0, 0.00000000000000000000000000000000000000000000000000000000000004390241662508430680087814935797774106083441568853044259979300659 1618.0, 0.0000000000000000000000000000000000000000000000000000000000000299517924323722052021025787412440843944313787484987766663472457 1619.0, 0.00000000000000000000000000000000000000000000000000000000000002042638348633338324902613748470553123329913769954707635662760213 1620.0, 0.00000000000000000000000000000000000000000000000000000000000001392498136604601335511089293796785355529321984770609339441946351 1621.0, 0.000000000000000000000000000000000000000000000000000000000000009489262188416327471497948909012782478444968927319780418818245279 1622.0, 0.000000000000000000000000000000000000000000000000000000000000006464056682468181097205729031332293269949868365126478402376944688 1623.0, 0.000000000000000000000000000000000000000000000000000000000000004401624144304056986652783277316472836076264117342857813583351371 1624.0, 0.000000000000000000000000000000000000000000000000000000000000002996098224542351986928497316079709752818252436832878564964553874 1625.0, 0.000000000000000000000000000000000000000000000000000000000000002038612273969523913727986109643396606812981551233732653489895562 1626.0, 0.000000000000000000000000000000000000000000000000000000000000001386592733632642594087053018402185720673294534149786553258587357 1627.0, 0.0000000000000000000000000000000000000000000000000000000000000009427555661124549224725847980378119110853937654766908663667950038 1628.0, 0.0000000000000000000000000000000000000000000000000000000000000006407452450790079364252172209543978370035500768970616635406790332 1629.0, 0.000000000000000000000000000000000000000000000000000000000000000435319359662610789253512117400843990968804776232633065618278749 1630.0, 0.0000000000000000000000000000000000000000000000000000000000000002956426422991348049513430557880818634227044012273956173122142465 1631.0, 0.0000000000000000000000000000000000000000000000000000000000000002007071633148171697309115131370477340680977863679778287281884391 1632.0, 0.0000000000000000000000000000000000000000000000000000000000000001362057917882471083265738197732115078310954290768032517078699736 1633.0, 0.00000000000000000000000000000000000000000000000000000000000000009239859805064354103895393874332476220001210794739875215945650226 1634.0, 0.00000000000000000000000000000000000000000000000000000000000000006265741767357646714707380522124906098608816512540299213400684396 1635.0, 0.00000000000000000000000000000000000000000000000000000000000000004247340759496280577300537243142303569336704362443688275808820385 1636.0, 0.00000000000000000000000000000000000000000000000000000000000000002878057193612506318818442128604446268389234170316519321196196166 1637.0, 0.00000000000000000000000000000000000000000000000000000000000000001949483818739548380139087040996348170576073318590109850234105739 1638.0, 0.0000000000000000000000000000000000000000000000000000000000000000132001220667089986435328814389629017579436003066990757470504766 1639.0, 0.000000000000000000000000000000000000000000000000000000000000000008934588507159633184909524134286686783585679346791919231472495011 1640.0, 0.000000000000000000000000000000000000000000000000000000000000000006045185836946245640573897989198410259991252982888354341603007265 1641.0, 0.000000000000000000000000000000000000000000000000000000000000000004088683057718225779201668480382412286613701534862719180841637524 1642.0, 0.000000000000000000000000000000000000000000000000000000000000000002764369695574463576726143505225004036437603249462618202397944144 1643.0, 0.000000000000000000000000000000000000000000000000000000000000000001868305457635413644739292895194382350672827143785121683661030809 1644.0, 0.000000000000000000000000000000000000000000000000000000000000000001262231225657823662627539249642473098212420292113492158899016594 1645.0, 0.0000000000000000000000000000000000000000000000000000000000000000008524510120884813477645697379103414861601246758880386192689515396 1646.0, 0.0000000000000000000000000000000000000000000000000000000000000000005754924236062464990082384184338889111693103479414351388879359973 1647.0, 0.0000000000000000000000000000000000000000000000000000000000000000003883735580274366056532084230800649597673410128946580307641752992 1648.0, 0.0000000000000000000000000000000000000000000000000000000000000000002619990732013811882954431081125526797558857424172143662672921575 1649.0, 0.0000000000000000000000000000000000000000000000000000000000000000001766811056172377711169202126589049718849428633868331976184837276 1650.0, 0.0000000000000000000000000000000000000000000000000000000000000000001191025081269108812080573963398352855007053863275782084466951084 1651.0, 0.00000000000000000000000000000000000000000000000000000000000000000008025874143491404258339561897427032737443076992662661044303596032 1652.0, 0.00000000000000000000000000000000000000000000000000000000000000000005406355690832625245299684803179312148539863960593349724733129028 1653.0, 0.00000000000000000000000000000000000000000000000000000000000000000003640473798558161500685448161442734406596803003431353713533419542 1654.0, 0.00000000000000000000000000000000000000000000000000000000000000000002450487103296057999669921371807173170914730031206473473196943137 1655.0, 0.0000000000000000000000000000000000000000000000000000000000000000000164887730938141473776411024095203899744767573918471869715946807 1656.0, 0.00000000000000000000000000000000000000000000000000000000000000000001109087632544911233994762081838657467629976689408251451431984827 1657.0, 0.000000000000000000000000000000000000000000000000000000000000000000007457361136210962300546356522020699906520804941477569903538278694 1658.0, 0.000000000000000000000000000000000000000000000000000000000000000000005012408644612995847938494862074169960327451689168039381249884702 1659.0, 0.000000000000000000000000000000000000000000000000000000000000000000003367828274375532217308074808043819477824384517211867164321303748 1660.0, 0.000000000000000000000000000000000000000000000000000000000000000000002262016510290446133224387411310979584214185669077467108529369177 1661.0, 0.000000000000000000000000000000000000000000000000000000000000000000001518742532298482095654233586164969505202421136467260760753943104 1662.0, 0.000000000000000000000000000000000000000000000000000000000000000000001019331119901604076421655039092974961134162878973967978150018732 1663.0, 0.000000000000000000000000000000000000000000000000000000000000000000000683894865312283690200318647958731054726137187656097228195911177 1664.0, 0.0000000000000000000000000000000000000000000000000000000000000000000004586765355997049981532897949882216085451534344726462958867374586 1665.0, 0.0000000000000000000000000000000000000000000000000000000000000000000003075155018572283409905391438444468344100523767775822989852729088 1666.0, 0.000000000000000000000000000000000000000000000000000000000000000000000206096682343262904311173157701395569753314335172344743718299608 1667.0, 0.0000000000000000000000000000000000000000000000000000000000000000000001380761513633515666098736483214859378749676524785546231063939134 1668.0, 0.00000000000000000000000000000000000000000000000000000000000000000000009247199317292996273011821787832857936345763443398732236137773561 1669.0, 0.00000000000000000000000000000000000000000000000000000000000000000000006190786457719088081685127002364340504199532304842815383522546053 1670.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000414310248123427502422727699896639976755408819569613756816530841 1671.0, 0.00000000000000000000000000000000000000000000000000000000000000000000002771723863973197589045730122079488861547865502751149119252967082 1672.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000185361214160594184740740630935814349161687487813481577031944764 1673.0, 0.00000000000000000000000000000000000000000000000000000000000000000000001239174806220512622971188552399158693698641240717051195821707718 1674.0, 0.000000000000000000000000000000000000000000000000000000000000000000000008281161901009853277308844675378523381603154564683655523074397712 1675.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000553216534009427737834539305968852520783811305008833501438253664 1676.0, 0.000000000000000000000000000000000000000000000000000000000000000000000003694404018616467936432201307780392256351200639975297365457161336 1677.0, 0.000000000000000000000000000000000000000000000000000000000000000000000002466261762612669202699774067823056720323562357442158394543219108 1678.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000164580993790518553863973325741343200548055729772545893502276109 1679.0, 0.000000000000000000000000000000000000000000000000000000000000000000000001097908390836902268522095759732838327345748233919858156417336634 1680.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000732147568737609305638772973784271174329712865768503234442211707 1681.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000004880647300389475966794591994865378679289485687461251182391736387 1682.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000003252390523798554645187513414001667917328389060066233837528026909 1683.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000002166579325253134263223291791910761354610117604623860762864125705 1684.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000001442757462275698140194106896322706536126407867911499175416728239 1685.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000960415306684061275226492793096486665670490809601304344824508322 1686.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000006391046941741382266627045602566281268395643839443042548971490435 1687.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000004251403381325180836680772448924332627752348358399141942105702509 1688.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000002827093502912489658030865905051155483914997031741835618037067776 1689.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000001879298489588449086503125197832946432413412391469121761706535597 1690.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000124881834670653600116932263574522138099508006886256461632035343 1691.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000008295658577900965968756476519198647415978688791681486304817328117 1692.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000005508720551406128717704996364363793242992570132351774055086626789 1693.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000003656781965946513812003817743990503480186908065217806060185133259 1694.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000002426587870729136249630250279703034239056720637095693796473014586 1695.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000001609688303766314970933350913192337686543741541320464967548782117 1696.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000106742296637321080111384427736365109198073773132100217856099049 1697.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000007075880070322637363090348893503893100273303671731503916103656614 1698.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000004688929771873109885603769637879365451837811478061104968014833698 1699.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000003106107730061192586856923639327960371153760148620951869192968164 1700.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000002056880207837065201870728157062634867427404199076754526603903424 1701.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000001361605695578160045401836553064517345415283792276442367435544395 1702.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000009010393780979746602342603273599374441419092154244431703785637742 1703.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000005960551041231615180707115332480616112703760716164421803206288377 1704.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000394166267581868728094805592819084371080657056221698259138637408 1705.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000002605691990455699787051956110643600717533711594679953683209006516 1706.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000001721937783417257763989362283805983717990700852779801674076707145 1707.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000001137529747337453739197941972625781609517631078139902673819048292 1708.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000007512062174189530590297506207920453326353076101020191829400580058 1709.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000495914564460977028392347131547431039297729053337714711739805527 1710.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000003272698557634665651084381480619705212093517462512508807245516843 1711.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000002159020534612136073021200236469833349022842209784747923120263691 1712.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000001423833937606403906845335488529365257505705380545382396162805111 1713.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000009386720363457683260209194850746261375898591050574329910727429746 1714.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000006186151692804668005159626069100223456663386251194003102853915418 1715.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000004075487525876515773374913862072048522033853860712634028840270052 1716.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000002684052838008847544733591400141174269087001310748391722512627179 1717.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000001767075948332715100106260034182539924010668268431353262519133867 1718.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000001162979886633617051560404007148393800019115509900857495031355118 1719.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000007651422726054544345457958291186473545114334629370002756303023624 1720.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000005032286573693905436048954680899377715013192246594382316327713716 1721.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000003308581824188142026060975654869753522236884902046747246759280913 1722.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000002174562582956898151739139749799100036606916271687657648296937934 1723.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000001428747979202077176452975379738301378903882371819796513290909771 1724.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000009384112462284268552759569610128757518559386434074753382819162761 1725.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000006161476616752106698706407308516155049582889953753685886174609405 1726.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000004044181150621731024535937217308968896505354798134958173585384207 1727.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000002653571296741742978887432611573952647659183100311475419670059853 1728.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000001740545792642312351769971412114579706915639686340395168292943796 1729.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000001141286940596854729890215685283628856727615266867851159255369019 1730.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000007480990632096222392321075770938242283361763943156303784039980861 1731.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000004902057609905557423409111771090016679575517885658601772125611087 1732.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000003211093231072720945869736217598914957025820282552778325369553666 1733.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000002102726513171918416828814074274119840983504119735126832474341691 1734.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000001376474609145436921338502476648107271522453365685708094612226799 1735.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000009007604796234270172468802073935761692532750170235856698862667779 1736.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000005892591208189123613519424879928337640321203175698284015034301038 1737.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000003853535290331547282416695477043169781697215098187458647767501155 1738.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000251923435716427527024931570213525794990962141181157422273268943 1739.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000001646395494065677570194742942784376819339155565178509267642510725 1740.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000001075613638993645369867281861781875373013523632195343804074133596 1741.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000007024818866360904148577985733836749173275819648596320234062855144 1742.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000004586388326444696585043890370785394353518471080734645686031661386 1743.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000002993391653534217710693190882173083460636598017937624007749622697 1744.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000001953050417111582208702835667772059249901025969034849242293832996 1745.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000001273857125166078427911873692955132141940160245486633895848549801 1746.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000008305877182179786303664727764542398163957952282098397613794709686 1747.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000005413871841542068255773264077447570760347253501317823435286703033 1748.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000003527672674762525579003434212675903545195241086047694946794357544 1749.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000002297876048004131396553582388829479229478373462148261234674807454 1750.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000001496315190137056806444522002536132003810858545667582654345183619 1751.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000009740424909154070967314957642658893729515424667823615474256471506 1752.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000006338568872933624422146650299390707676579750394989268010477745965 1753.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000004123473227556839977870589131953581192642302497794867293059868693 1754.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000002681599775274719687063045794362576982457999553522928066048156162 1755.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000001743346518150804169574064698199080156260375377972622163964387182 1756.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000113300705535771324086811688063686197194091958194678604391759296 1757.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000007361067626806141334155414437714594454420334302274625823489326896 1758.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000478088678025157236892629738233195830024133098270802519832068308 1759.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000003104100173284824619348002568329181860347919043584992560504522123 1760.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000002014757469182781991063161674093917126853572105303070333688740308 1761.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000001307283433247699162010748824560303680309197060453106132324959444 1762.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000008479628703863791451340885279399387578144604565592735061906213579 1763.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000005498499241197778472950860726109871122881835704853997235622181305 1764.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000003564280835931282326872031658069027269249403404365829001827167743 1765.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000002309724774794037196294109721631521035418991007425162092443476107 1766.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000001496267624651273494738750770992637963039795386124933605818092339 1767.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000009689898146647611708642970441177056474925158472883295325343788654 1768.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000006273215246415584314513578213656160969244558106126382355459659421 1769.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000004059965730905483716966919886351634441059235753038660232036567424 1770.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000002626732844775562593857338843872658032661287506147184872024197335 1771.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000001698912282299418895435956462009570870514121711155115900138410249 1772.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000001098468680352341422344910940618180557414760024541113663645947973 1773.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000007100128826190844378055492984278781521243725324590917974518304497 1774.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004587824134645465533721601577240593547226110827790013945613562816 1775.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000296353071874460575670975200280229427857863081156097047754827904 1776.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001913702388131211773848340870569604274037106165268910501582580514 1777.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001235383547669953201697504769639727535632710052520092489409019359 1778.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000007972450500104278647416846382022946581597355808648719352166452998 1779.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005143332581065826070503241266522389131001235173725154558111035832 1780.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003317113231564510860657483705884904366269935063074946674919697499 1781.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002138646729580318243372223685592057097671240077886536336909877175 1782.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000137841829117126210379796488085390655678289599384674968672406493 1783.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008881501153622155254520454388279567745701067295954476380088102758 1784.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005720780076397176796814496647584589485085808987379173157888452003 1785.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003683730147318844738670194189033047977822526173553901998751529864 1786.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002371287416276964054890658201135866241833862056965616737547830826 1787.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001525964990417220762589187130551850873824853359728419678702510986 1788.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000009816780497089105134475036503045902401754086756923450988871281573 1789.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006313321119479916604216272348362378778060537050512375321125300942 1790.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004058925872678579508213064077979949154475181836463731662579611275 1791.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002608728917489881325400414911926127829821850960085254393193019059 1792.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001676144776695890284181012545154695962251642242641832044361999878 1793.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001076611459012486486451111747958487035450641239527462461958667231 1794.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006913078887587172387881477256719862982816229193425545586245015235 1795.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004437610934069333933407435213501553939398146223357394837663641968 1796.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000028476871700557463111798078580190261534728892036678576039820856 1797.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001826841334933894113516130112874221478254407685581576953578973811 1798.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001171588299080284263903417784716604076675823189822491096306988465 1799.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000751129841143690285309858595493103650949365792471268606264296085 1800.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000481416476805482835031813048739795324078505095782003108113600497 1801.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003084558255545099920051795055129845823429305281356447153324877377 1802.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001975746606188227831973381992326319046761248045767113045235691653 1803.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001265132178137387781143531291481798638473300570626314687981435459 1804.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008098546816343029433107468320455968201066994151281858152161939348 1805.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005182567627576585843110821165731897769258829699715992961058918634 1806.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003315505037488944705917173826657042175688243224071471313279823086 1807.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002120417535254486960624443676749919850325466863419280892969096047 1808.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001355689430073522790474861206301324299396593431693208393544714104 1809.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008664954924557522750825634104876825565612435138486950013517478306 1810.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005536557661268835048313864942516604960847530979641301350891018761 1811.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003536559057487209984018224999305318902552435059145095146629778777 1812.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002258342026725820864568989268089978764666889484209758851779428362 1813.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001441671717730118389820391397775918059202226067373791343319681609 1814.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000009200491887350489287731903074668338532063643475071856254821444905 1815.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005869807600093805359233040686600671565527440406622060172259791753 1816.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003743733989629068864737444904181490733479367249039288875127953135 1817.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002387011659696586451186036405472473246148981940125039026959509896 1818.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001521502550898974090376464043873286616967107082735789022418500671 1819.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000009695262235252400159416656252744791756421170473945654409225697542 1820.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006176114104364985231899311495341705464991798844445432601144095812 1821.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003933146120193004136002062834406811581052187066214597631831460933 1822.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000250399821233290269476921910657220088852898295033006166857353456 1823.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001593665806465919058069415908280531052457615665701286733645121125 1824.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001013981297655055975451232939014330804383830348884081429252483949 1825.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000644959179235957294567023277551201921382948986684342966907268818 1826.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004101136719662930428691025074816772064273287153714161331993360777 1827.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000260703070256091573422455272433556647332296644241303340705583123 1828.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001656754142407654882839382254873708711188274177763703581182454038 1829.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001052543728203649254394286548074181284765179160443215995839145267 1830.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006684863375335909378052450156464545906377232023886323015824739898 1831.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004244390857726337941963078330833645447281144350149632177678922972 1832.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002694069490122762392886303403225998533277644109159334391864935953 1833.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001709515120569570092391090989709853262907763620977264381012521146 1834.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001084446096837961317033076480594124121758427696266558481768282935 1835.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000687723721971252852752955341674792546522800150286097235578382731 1836.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000436004721079707454478128035258453156887551488073933051747604283 1837.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002763374025492765936742501059282513808384249566028446925376327639 1838.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001750892904453678561873737045919330128354696829365732968084332046 1839.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001109049847964290155258245828236058828604246886738338714925526852 1840.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000007022864067336211769570956689583284120031041188342851268244297076 1841.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004445793734874575471147641329002617682421289962570083834735105575 1842.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000281356107542090175365226009244580962800875742394290244534294916 1843.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001780063577493220122106465586945964626888000172161111764200704082 1844.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001125866635349877709403754587757771189652128342423141751159903909 1845.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000007118865553478182299324936494723723835937078066622756432734231698 1846.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004499944709550378779771852563164280799377815749859196284773281103 1847.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002843650690940490862007431885899967147822377830463774007107926207 1848.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000179646242489946104177262569428617411033755876136033516735602314 1849.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001134574417358176094658431731013603213315726596826918716473371134 1850.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000007163428516796113870372585942919415072807289989493605206894587834 1851.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004521495564045316666891254290249623159637453585178859657801017424 1852.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002853097785057621778460972020699717566430582978698503442004421127 1853.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001799801919758224322104384906457890108795061113505001383340893858 1854.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001135027562600753362102621730513474008828132346595884982804105766 1855.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000007155860810249127155681790040078712416276659296449604365937254016 1856.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004510153015497944471306157187641259523691486677912199639671513436 1857.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002841807915330218355345898439838263547891878807853986921768332637 1858.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001790079625884543609516628233974682761537391572498950466251029979 1859.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001127260532612325826510559014040980573013012340890558077367559945 1860.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000007096606284822247348827659031748673166450412801098826311087063966 1861.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004466339309969417702647422686983644642691983446103858284687549799 1862.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002810136673325667825891412028206794304653399689836186979109341027 1863.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001767575744972020298532902435046501776992745681618532552180526793 1864.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001111485036032381894452557604901010412034203541424773390859303538 1865.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006987218699620774421410219789405877096715666120763579319942964142 1866.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004391170674803756675302002709142795386011378048539457036203356528 1867.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002758872915445083069932699738108921703197592741373148044455615317 1868.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001732840559968815082083585275203482879112056965845092929233096371 1869.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001088080878233934526648633871791111677141874134978309232972867793 1870.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006830296376322377870414389892947697722864964549704831748385074409 1871.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004286411381166173355854644630428463479384075772375170130136509085 1872.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000268920687375684521028661208340147695470713343308947489112650101 1873.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001686672526465384426226783334605636621682857255516141660816067336 1874.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001057581040176683202279666646125718452376493904724224290620338301 1875.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006629381331009042379342969226449005240385840178020887404415626343 1876.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004154405995716060906974648148130372103803594488870688657163554185 1877.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002602684910495938979408549780455988860073027460663807064137059575 1878.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001630088207856406279704915942681112587108902326072253116437497074 1879.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001020651791220079047110585079314068101228095804285987425060736379 1880.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006388828264126076540900715180592937766248443855511772286347575648 1881.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003997993398543861133870287526140570931852154191541684688460918314 1882.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002501153282088441889655178947169252707063895802939789104731184621 1883.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001564285611740409123545254176412300280094612608140651772613487959 1884.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000009780688568289836342176017240095367705175478497981826125831419017 1885.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006113650074687831735028334510599224643278390796543985210512267265 1886.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003820406904167639251930722467855665809423789869795191876803592062 1887.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002386693724529558380228523009008575777556121650533948540072606543 1888.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001490602744833613538095676707377045577471812347119955703274552611 1889.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000009306908121288072441923018533336212114702547409780006488822285432 1890.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005809347422106344630949963678966290692385291466002145428584117249 1891.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003625165306083019945850129310953685161787636511426452842897907168 1892.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002261553940634023336392072142016783098849666892085929474745017741 1893.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000141047334998204890280325655468609449993778677766730157388186849 1894.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008794309498482593575607114586445007633885847689136372599141807882 1895.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005481730255340615926346179477957961344069230459047675996688334624 1896.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003415959856653514589329560088262177252873592712851215970747613629 1897.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002128076153416866466969105891619297229131591294589335026818137435 1898.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001325381818514557294649504051100862981340146307012269621050793841 1899.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008252288754073412315467047787617879443900985412384615353469111537 1900.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005136739165307880845787864012420614353839410316335405139994282688 1901.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003196542097703149028087004814393834410465591241084153306224595188 1902.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001988626794180153937656179196276993178599452173043343816940422211 1903.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001236819189269515288235961103971732393746526310008874598196030932 1904.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000007690230169526661211336141083586135752813389745239275050640158499 1905.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004780273925104491386301728473391834441589460691479166181125048708 1906.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002970617097183227225447092953219739969917323562332959413888412223 1907.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001845530136262303244325685804650158895891847448798052860723643395 1908.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000114624196454418041581273827872085634066777318641504465472331801 1909.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000007117251126543256252873333999300646266692882058528207172655273688 1910.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004418035723028079278399209184011093292961864977888459412787868254 1911.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002741746064211697718636039649155294919888202871036542764772027991 1912.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001701008293149877126366785424643393767616450061084077540062137434 1913.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001055035211219679957848456125743007372253159932485612252914647781 1914.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006541975636895890035685281440576657667333703611132439837662442381 1915.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004055388445758357148446693692929194488337579063321449815079985948 1916.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002513261561197341461109928835687911701419468261315911536215990009 1917.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001557129507090681417174400735751188772813206599272728507683342833 1918.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000009644810947243730568799963721692192747047976524370875938321611776 1919.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005972343334926353620947358438474514965924867687841325897338757705 1920.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003697243026441771689309170799015054623885377891848327344336260363 1921.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002288197667441251970488148497891975408283474624815966548573877773 1922.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001415766099725900987045696686924521006232985161454662804920431231 1923.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008757336386957445145347805856288948944258655832277204713313371182 1924.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005415458475387390356580942703103565616791538860726061260348193913 1925.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003347967432956296147394820375954179754549720264402108605976324176 1926.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002069236535503783658224889316868721762833832690498376937191667648 1927.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001278562878568923786381707723406284259051296057842298522347910942 1928.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000789800138652791249746780826251617445925266721159251699747104619 1929.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004877481184624171847801385226183951209762722439260251669225356587 1930.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003011323430779653773349647300128411388267459863129130439297404059 1931.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001858671879107865501197280230557705020069956777070853890802933871 1932.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001146916229209523982149897879390611362006778307520676282645887135 1933.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000707529306449538041481831056833465741696678393294400498708087255 1934.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004363561027017915996750886426604692668932687176321223448575150616 1935.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002690429898760883760194967139108543072354805819527062019766049505 1936.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001658389092139946171933049373295778291686398035363925498845963662 1937.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001021963605081577752144523937899862870439962259794888259245325955 1938.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006296059033034273574111463765648241594639459081093198449291623919 1939.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000387781098962629034165180543679830167374238876975399379911519143 1940.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000238775127618141578657493106564048884091148122459067265514890644 1941.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001469860965356861517639018499603010575855639143169390770598445351 1942.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000009045826801910259692558620392122592599526142036323792579472787607 1943.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005565513480725659292160437882691976804534717558447676369209291151 1944.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003423318346601948935962380157550420510158532737583315304431386813 1945.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002105108729089664981521462959042452189877749071371334552721021448 1946.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000129415737188151901672978857361922044651851049720633472461061566 1947.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000795399073452192767802500237438803001448422905244016406198829259 1948.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000488729574606822790332764782537841636929635220598164245136268442 1949.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003002187600590780731224114804761989768370008632045135410188850536 1950.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001843710876414747541676203281473023044985731040438216210991538803 1951.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000113196685301409048006043336136625674212771830637204399577628849 1952.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006948012963273969359756501766593809715530529124132361180445338278 1953.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000426357262793768709685450914812808527273200220913086974247566015 1954.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002615609838568824249256189361005882673595822859581946704209078251 1955.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001604200425377006248881755205893987270370392939559948260079472968 1956.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000009836277584458325368081974960234435973014304412985617990023075832 1957.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006029614206933856599443186861154365301396327388042174143956248879 1958.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003695174829468118120064410385514207908271871389271855958967669145 1959.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002263952382508411253281161151120744499929925700188111217155564974 1960.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001386712826511892942997582900802887575334072469346404254472300437 1961.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008491664745562225949303757197260733243775947773149860893238437101 1962.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005198599067738675262684784903590989006805636563850077539044094088 1963.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000318175768823478317758869976545437200541213046468003670517001039 1964.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001946862533342160915375224070385584934078336446114857131051035008 1965.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001190943051006973167651554867157582755480131981937826012043566035 1966.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000007283402966571340980356975208444066520813082374794439210774085039 1967.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004453130614555702840064898051266442788171607415008570222413686512 1968.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002721976638284668346767128133867261756425103816463675219433946454 1969.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001663379452287512542595099474286007351382830958497659250993711042 1970.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001016216784892203239758454272334883252728705275274248487323623556 1971.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006206826443414061022356830992487723130110640276369536055862099761 1972.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003790017010954933410767202408930807314736634830715961481885488672 1973.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002313668584577097332453876737028396992468503883011291133965522737 1974.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001412048735183913514597686089447838942767758936776039820828945182 1975.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008615626858354063669397724296482662992121776144626982527043937105 1976.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005255485575053504511929649434320203458745476553693415385814207094 1977.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003204997532511297950633741841800409932323774019149396058892944881 1978.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001954031439903752165385825028514640748323306574003821569113837016 1979.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001191034944010486095915630636627480100243016104031613265605123934 1980.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000007257828148085150045189522953409555762901146615883716202142104297 1981.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000442158716123219955888941911326197865142356081475309165653074494 1982.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002693017308631047253420121845133567041229973292011900301854256021 1983.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001639795366143379022883526616640640675732442706478345224302196684 1984.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000009982281945099904900387515976829010803236257524270632034322196758 1985.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006075188923103771768607528416323482708593081233228790293130145737 1986.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003696405716516220154099152250663032814806900532136123624323079796 1987.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002248482329250200626874571434024135309382055830223250379594938606 1988.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001367380667605922054608790403427966910784940446207108262096118119 1989.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008313417926547250499301933134204828918740608519649467640646613502 1990.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005053125955329332455768720953598018188670226443096160883050597415 1991.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003070655363141793429376180733472558288921805913669223668320228559 1992.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001865488517399364920474724168489688101935919647527888114637137537 1993.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001133038706838199416305201921055085727485328501612969332324563283 1994.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006879988058611490730406269338032362149739830205810069632296164742 1995.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000417658672358821211621016209349416340991863379607574126805085482 1996.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002534815262293740562930746340084857627602184976527715839378445467 1997.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001538020849778253108553708723178520003508261194097361697021196577 1998.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000009329735707609323615902089633169845575888234731372725309627375428 1999.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005658062979714232421633555208177659405381236641294377938943998745 2000.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003430501706332743014704522544888360542897579284336389389097676089 2001.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002079404775549169925424961507057900944467461406804870337986079771 2002.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001260120502505668813987677085305116464449409411079985739647736959 2003.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000007634434439120037074990888250740977384919780798449005227902336964 2004.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000462416712447722077008069345889291132670088716142436325132864566 2005.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002800155445616905118238658951181449022490225013654678046079615156 2006.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001695207384968505540344858656229994434294416208719403032420031027 2007.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001026019732012715875948019365534496267022778625285759999047569999 2008.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006208417017312257341247013321169718793640035603184686909091242147 2009.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003755765305298994605410401330560926092353352303453470746096726502 2010.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002271477898067453464721919066834321504686480716947268065665367679 2011.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000137344475938493403835029193033515794748299285311453718651441498 2012.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008302456255343397434064170822038704306976862888850888974831047071 2013.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000501758594777394455951892717075294355280866698023713797890995937 2014.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003031628464627880530787862091409933988593652753842919671559161594 2015.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001831260675505292181200725430143158248725546272706597552129394873 2016.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001105904197960061687892654030931219134602296642525468213944095196 2017.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006676949531198282270669557604774268920748973187618996751055291725 2018.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004030250458851745858901750651325927701397993814392538210817370387 2019.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002432088867890826430764700999487706319795265446091998410336341127 2020.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001467305044012492804902136091421266250059953047837645149687331992 2021.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008850240105321544867507538210659018533836355618261150497225734753 2022.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005336831281223841927055984964591002852757134472599764796279226076 2023.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003217405320241954550391335994370211616130919066925403879445770565 2024.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001939197601279301355355975478257362527818323278829324977577085098 2025.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001168510036384634880719642653145737096881435043468098690688113882 2026.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000007039422389045556091115306269029192145435470444663351611349758892 2027.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004239707586940013268490238304567099530653055175843257893675964286 2028.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002552872874172854718022278259328025939460234224703150090602273671 2029.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001536798692936778077840962313544233956218379735244910025511930578 2030.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000009249098180435435652299614900984498295080331316586813276974938037 2031.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005565145376639319571167486384735452720307007789447445472090818105 2032.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003347714849639511829742461564818911947404572325055531391958826059 2033.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000201333168322009035528629854730053766028920230590712734809604579 2034.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001210534649204023349470526053034306912728073695250721430887107593 2035.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000007276696347423113356887950455201615620858390361911784726656768745 2036.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004373070853116142201911476085786549147562753396772953394456588983 2037.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002627447893963545716161086610841775722858771865353108596659554411 2038.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001578254912442114526026076646235260636789558622791794664683839136 2039.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000009477978724669478746627372574295262629273381768950123659908259801 2040.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005690493789615993212073503842902521282492154060497986857236873763 2041.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003415701611987625884083015707295244852758088488364135046926088461 2042.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002049772888348611441918213818777768110237991844412243592680548221 2043.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001229780253533705673363691329483787589665610577823463285572976337 2044.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000007376414940928740729134602360450531838748635917066594754180670275 2045.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004423431606288762290659433213451239435915794518688866972948656565 2046.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002651975762650190259104276588209334389697644771664198843599860172 2047.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001589557131167873444110696565303115916588075714792728354497675001 2048.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000009525311598038465945230721606328056543185802278052750096769594177 2049.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005706617986490263793077032138999312065460908737268648467762008723 2050.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003418023709283662595058093380512463153389807612992428884254259542 2051.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002046765567593270053179783047659174761986104531886868225993648138 2052.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001225343907377843902477977206831125418502036033495614906806818766 2053.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000007334066638515935056265873244808526021188251493410660263428580109 2054.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004388628009595782497325373182851692175898388097244528041213991912 2055.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002625486881056248680179069190224016204366550474410147567951546908 2056.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001570320129034399955440011929262308274359231384589122463856613757 2057.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000009389963538847599462163891404957731604266613071884084977560479354 2058.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005613543300549402739747833825191268654089571907060522878312632312 2059.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000335511808891772988049641446157961695482157056443606758457558709 2060.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002004823784316567483410394200681016765758750422802563937546149952 2061.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001197684316854577027839051922196825620261722006269172491619048478 2062.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000007153299024022632288014787393380311315764514310205468522425929808 2063.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004271381444012848553952299090058756307669177669774513583600517425 2064.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002549930782737807742981552756672475434415884594235928981944022365 2065.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001521901636659781531453566002461565630627803436847457526551443149 2066.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000009081195644227307151605383160572642043276017653306556425357800544 2067.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005417486459397168129131640454057010571864156793568033788011383275 2068.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003231105134472311995613898907616460312277791790603062854839847919 2069.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001926650302980079820695561479795037840284033988674430374468717173 2070.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001148559106693712139880907494564058239733368196609927964059920137 2071.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006845458639686749450112687994797277653828222256015644745305533547 2072.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000407897080087920575166100899159176135138034257429827287566076174 2073.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002429951380815743082484323625235338940000136475559363479837478325 2074.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001447250177461691512242537590739810192747514138993295723365072811 2075.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008617648494874782993439342642700961615741812977382674443725256749 2076.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005130186741650094117888866273615715106170930254541638079432054506 2077.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003053352206429843079584843331918011734089132317472759087648190696 2078.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001816854131657667281457759887261579068868329937312220704836489251 2079.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001080843320844036389146284066033841099958413951336560537079914406 2080.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006428431890582993009982053239054015256543144542539308390232841162 2081.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003822496255180897417474248369268570193761221428811400350804165918 2082.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002272421578677119110290890632758238583457766493598251030314466424 2083.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001350612146613917609341173875527414879818869850038585397605440082 2084.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008025505641198268052983125454351713594904621135941719350787934537 2085.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004767758620396822863339072351080470916433144823740137439639360977 2086.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002831759160596432490702992455101546000817120947405279435584264204 2087.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001681506932538189039473812719074893092996277806197345444199634643 2088.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000009982547830878858150377263279341887816836758402025912276295011821 2089.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005924949080045603851203964920974835643163333061614448224485307354 2090.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000351583451268290404621509779009120785862007323361921747492103532 2091.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002085801152175132803736725165024191676542225540057647191918693466 2092.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001237138012500407806606002349063713845751926343887675813449883808 2093.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000007336083480366150103922636250958750422495611977436097092658787273 2094.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004349219614041097283986916730037354258295463982929700861336328239 2095.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002577860818178719807836711760212434253709416138259904318374757361 2096.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001527596807329396352603339365216178728645428820512027642397370921 2097.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000090502226072547513429932213955232801385558386399626318211162521 2098.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005360571814295953770717275707393168050813470389034004583236573971 2099.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003174420146653656855585040018687906958612425449890123253552381599 2100.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001879399990065545882421670309721692266892898125502999482890773438 2101.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001112437582685911795928779867636120092451684633772580929611651758 2102.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006583150711912417403263800998536546464779224661356984835690313303 2103.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003894877015549970596315069837337140973417181990810225463374101941 2104.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002303857427779471073362535943532847707612304211424365616650582846 2105.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001362446454369048630775236880182291455199493109841138670818208887 2106.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008055367755102691186679363782083131850194095844518667413178418752 2107.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004761606256910125570534091393344749151164664273261328990227192669 2108.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002813998488222783022574906338752251496306709634943664741170872496 2109.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001662633794429307863773106109722082367336647420974499991652494385 2110.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000098213639369263213451041003127793322411958105362032702407242902 2111.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005800287952672099269861575456320549009068229250194913960709048903 2112.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003424758451734240033170605164075841170694511308305622270075341125 2113.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002021683057542955187250991845614975299115330783290383131365030475 2114.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001193160926865014847231772882881773734077314087848672432476763335 2115.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000007040246929389162159322483347219944578992007683137511236926705275 2116.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004153170593865059788572114178316534791169363629831252171075723354 2117.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002449484806993943227775361099276472563705043638189547838100033637 2118.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001444351585423914174635348177484391537084826781775156038986549278 2119.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008514798578050826512631363897676913691967373932045778076627740688 2120.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005018561040229111381693105384502722323932656670173869879016128064 2121.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002957246381872756658357331879219879123902645351805894214817734809 2122.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001742205437763293627936560713194061478483132551632273714633629776 2123.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001026159497583583932676785029730873485677174284110169162280501656 2124.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006042742839143817186427657350020028717110100193824672158569736827 2125.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003557600564899315481854066926827906346157963605912332597071332794 2126.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002094036168687134858484079371312758821906887577135140052811227934 2127.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001232296407748816420212199142682630672419010863299548563360967602 2128.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000007250205011461820787566116959243843245111932858422593985069823469 2129.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004264710768079154985394856567764749282069397406223751975187888192 2130.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002508032495947629951578518397464600500957797073405534304591680734 2131.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001474623350153575309192135686174503819690801446018765526575791283 2132.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008668291636348025093058762964183386644623616767871050043398905688 2133.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005094369858978426043652021793214022233737380692979666135050274368 2134.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002993312571709242467210331870345564725960644056919984508782558435 2135.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001758402919342688293142993083933367341032203757774201523805817837 2136.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001032736533586522916410641725820860613578466643494296801569021819 2137.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006064089361659364003881002007495497520479030540449218885733840257 2138.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003559972479049417996647358677890348596494366514068169928526835536 2139.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002089453804260422570207305219048525958334539461752383151044499471 2140.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001226094885272578935042639900287110342937123162957778373820362463 2141.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000007193175396440017906773848147668833734463923272156850586979190523 2142.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004219126729536300052249825426986051207126506394377975424415285896 2143.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002474172240807734233658633081087267709190484143555967086955577409 2144.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001450583780022850264936986002969642840336753874650323032937161056 2145.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008502787386538383404629017898504732770586639445679514202006416747 2146.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004982938485077166484896592185630038806329648915917185868173465502 2147.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000291954735167654927953857431534342189522529496516786641177190097 2148.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001710217712974042955649237097511992795099307688910863554913273199 2149.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001001597527369918877842560995892714526147631311237646280362901234 2150.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005864637074987797011167710876466849403728479562302996525040219515 2151.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003433168989441581218335611243937931457348005100753228648158837057 2152.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002009349343973638151694444878591408503347551820661017619454561661 2153.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001175769076654067056462684112683382985362327997413889532156095248 2154.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006878520267524791387624484081161081569403249920607653426085274551 2155.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004023226642665261276759497068350580782046093821113913787568261919 2156.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002352667497333459933750014475728934536333272098432704737269321167 2157.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001375476801267556794126805853118822595820818368439956850163972421 2158.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008039938694153637749427670910331085911816828636596353546370645263 2159.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004698497938515765940734603273095738554210825045250001096709763205 2160.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002745189105077142934786664564817662922765028871614708900564114814 2161.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001603586905677287915649669198464893602963306036738436303507380402 2162.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000009365259824732092022211128582077387254464381943287290949612887509 2163.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000546832528476980350486784962692501908247853299713067864839593669 2164.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003192244279083899992586135564159589195745790442082046698825814471 2165.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001863138953912266892332228917458762420666393485393886296883499316 2166.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001087180896225257114731966500745800822330676857902082289478577852 2167.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006342579244413907140905210386507282553888946215575954299923592381 2168.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000369945373203591282732413553291795479660766698447004722177305675 2169.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002157332031905479480972706068088171568835122763545157794272127901 2170.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001257778487560403886139863811915431789807917445762084721276514872 2171.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000007331607332151913149574221080607270153169125908596332142360704687 2172.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004272697827915486169876179671142082857624408040645822517503546596 2173.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002489506108866798477991929843899633505306748734303363441862968236 2174.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001450214792543289870436627303024102228706775183574406427531713576 2175.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008446167067930679197408884081245456209829463711430554223163239102 2176.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004918076632057828036798738913131142456517113498316680611161960096 2177.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002863118258564962853089014801099713268202025363147826560269606797 2178.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001666447886723509850045953940856704261450557825038014547579668018 2179.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000009697342320980657366708317780878449751911200026610136257235224487 2180.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005641860449883515535934477323969235655692868593465387838815939014 2181.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003281713472655120089942592223704007778642837909706402033458913952 2182.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001908480694064412406885536889055224369843683177688756624929860545 2183.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001109644223035351552708429055568246348511787682011988156159796972 2184.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006450430493055153817947528974820893469200649654791146301114857984 2185.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003748889886611382451231642226388049456938883126485619768043156874 2186.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002178340637511224717239691185615783103257429271747373214423329734 2187.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001265488229445555382205839922795155612606891723234796765204797403 2188.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000007350209491643007532507918847040019177056005038576386134490441329 2189.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004268258344111820031746069556117696984407241106091414452497178596 2190.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002478056105731879320859513627333446311047737831177988885698579985 2191.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001438404758075122529114887675137926350330083887993054990109718722 2192.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008347582279622669890787977254628575786842081329067322583400017627 2193.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004843396346978587272225595381319030356105190045360720036361439424 2194.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002809629780874714316905586462607163040855700914126633345412028056 2195.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001629513841380474275063959262380830252941179283474111817372196139 2196.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000009448806616216350440169109962487473599830255832338004549332700214 2197.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005477796573458194551398627517773556859291648111345397894194629076 2198.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003175008950336051123796617458118325676667791227815150035729871162 2199.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001839899934921944746640801476043457522965346343236250851641262228 2200.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001065991476360138206521174037147012890201013993013713521587882599 2201.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006174810295529001446129655352645067014963797508726730882000636732 2202.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003576053010181844354831072031336004984804555340455351076038847008 2203.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002070593272478319988948152671632901400395792367132165328020867761 2204.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001198660661752640481982216186923240889851363365864632891476960965 2205.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006937586183169304424527455492974335050269327307073125366651075014 2206.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004014498457311764791630571439769042667430024726783200598720939007 2207.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002322549858810783995953912069083632400213580239486986624992085525 2208.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001343413547541901723473111577355623915421120070620114043091067284 2209.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000007769005146591034552504309156903394810929874814897497722142778696 2210.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004491921759625968552059178589619796516110802033384463452382850957 2211.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002596630332017377928239793882404053256073098418681153333272267882 2212.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001500718781958171910303572695521200266199218737105341362496356906 2213.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008671612032231640906195489000026041733324618384290548813684928896 2214.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005009700585967037392274086050253320218510209451346873955973344603 2215.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002893577979261923348903787697429760726625022530435359920418174047 2216.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001670975878305282459024882798056400197932501396899150171168202636 2217.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000009647545150181855636309676500433041872393238682155926751419574561 2218.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005568973967802564939082807280516867305018382722767749562755113057 2219.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003213996239806329483821848964929199938700319742107822921242501272 2220.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001854502524388994645578234065527957868776489512608227610111136033 2221.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000106984647646924664335977195346232858025646139278713239932966357 2222.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006170602049621586511946039421740990958980242382331101558918071349 2223.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003558326076088355272981672846450236820426851397424095418491768434 2224.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002051521875071873262914770960820186184698859296556640635619357352 2225.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001182548182016956929878846482546358465523515296995239392809466374 2226.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006815126161848455526650271688173084321975097330558108386993881682 2227.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003926823635375894143190776608409516905600450749515934848240693624 2228.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000022621500708826080137572839919142112408270596459946823364206478 2229.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001302908829725390517623884839740864038554648774652901196977900477 2230.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000007502730015705945758274933954427057617965850722762006258008365685 2231.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004319538659596929252202099976434145115623140615059561636592961523 2232.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002486384244236831953943140356691747796465523253797270947891517803 2233.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001430909069188254958546001721004424174289549331485618693090489561 2234.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008233202870930774740456461917773032909196822218645015756400825945 2235.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004736294127649469315591613193557685151525286323259321283985540638 2236.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002724091288530862230901573544777917444456289331678550625304332065 2237.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001566454817961823067788188195242768349300360130012810029900612725 2238.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000009005905561785204384821999903218313267033121104050669407105015661 2239.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005176667631313224441603583397374574105095032265875126899259220946 2240.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002974997673314014778127339464739198790940562067641063040560828426 2241.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001709371706344168156064108074875030680803565974950217816906000443 2242.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000098197402819367391884147324872944018642301235976419977637743411 2243.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005639974991470321095278768678453199734707765587500503173339618841 2244.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003238680590204212860773538179112768135498786865712541796717262501 2245.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001859400310174446391099523015935236095296966269088279018491492898 2246.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001067312502241719396907812968509695281576701099460784849904755841 2247.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006125256614594208523572994672601393273903688855875199669056476558 2248.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003514560708605211688076646373005068615595165713492534492429810162 2249.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002016192327140226694227702666039058405829389276804904882701414621 2250.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000115639735274543890428159671668761817893520135956419407051584303 2251.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006631267069582306864911325605232133278708620632384503979366372999 2252.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000380189680720784407043489370546626261783964753180671636890680564 2253.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002179307843655251722175036268014165243132177038662741909718455762 2254.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001248968157952879741080928408382337984674581353744378857136841812 2255.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000007156468331537810661142438826791938629656977659756596073140297132 2256.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004099782526511755816485166173186739627325628135209443236677432601 2257.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002348213826622432248799839455373941834479509439070088010822160859 2258.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001344712034725836313086470511654018834484379962307942451457926494 2259.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000007699027091990210910421313709593100192810881626162204572447828771 2260.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004407145459212829764448537747663875241889551064520323841773569322 2261.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002522283957374328700358033362921728003516113479603768506159465139 2262.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000144326374281104807772707355734665993553224559798850564989580405 2263.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008256816474644662433345076676026256796996037720957616512899311942 2264.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004722748759529460808324872901445238709061822618552669021661078124 2265.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002700799903969134517954956635192044518564574469698390179998608605 2266.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001544206609370633922550941195607958454815735251321808866660754398 2267.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008827422422866376541794856394835812164480841700954269364621080101 2268.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005045195409899204840629525833225959832533420731152958451214128291 2269.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002882954103136383578860185048144647171452754159324397294064184881 2270.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001647074310062522387690653175972189816300902529512081764053116937 2271.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000940815494097416024106235861702344558033498748262522819321522103 2272.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005372935234721814131551694933722658160864549259643628145887866199 2273.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003067854094971839565473258628021826278077419684164520382281398666 2274.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001751353526690001853782788331892139884554970203585202888295369826 2275.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000009996064277694965037510461959509118652761610412034388733458756445 2276.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005704274696944385868998511934200723510119794104769280327075435716 2277.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003254528422562710278945472829051104695080570619927453953047842097 2278.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001856487483729438725363963763230625599498839369579762254581005364 2279.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001058796203886600824823277396070080662103024997282862750564481255 2280.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006037389455481309249920240671356419043165145332651152289360387875 2281.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003441934075785954116163695777964339753789534001896996107974289389 2282.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001961880347307162120729124240604696748640453039086316754778449472 2283.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001118044711005077736422104698786502767163111819615833757842000662 2284.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006370339601121503744050736708641402549761013532854968080016493299 2285.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003628965424365872454126676044160904437141075170116754293661777331 2286.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002066902450893385337439443399142098470046212933429201958287432791 2287.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001176993616082581560414656462318706988693625118617924500337578266 2288.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006701087427229775863012301991318055278914703415904479156114814394 2289.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000038144645507804953813971981257398273703691666726561095678268608 2290.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002170896292615121006382267622092765773644415455669449291304035615 2291.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001235269839396377446960091116690859359160172834637884318842932707 2292.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000007027517530831171776069989957384391237261540557385324187252705278 2293.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003997232859697843560777572829329253790090636354421937998771503119 2294.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002273183230187999029385113882343692678902693907068813937004826982 2295.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001292489409756634627935209259015616793537558117127950731160091072 2296.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000007347458981261502571771746818719029540131899987714802074475253446 2297.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004176043808252803238296001055075481589981949205777913728333641011 2298.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002373070781563177081464505240386508961361046609386719055935064323 2299.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001348261645938963016144117591459199955710828475545584257576064941 2300.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000007658709239155643033278860318410417249235905543389191993914377735 2301.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004349656571583604768548959175784800131987624999620550936220061903 2302.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002469860423004963550655760021140664084145057751072878223713050882 2303.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001402193601307637828642476577180808785911976919171432330433902115 2304.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000007959059459751100545594316370755042360952903222527009629357751134 2305.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004516830431533475638185452256875199074277576432304574800917276409 2306.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002562855761954819003724412545325780621998782197519206206843021763 2307.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001453894700850805233035848409964707142588367837882885598442204216 2308.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008246320772816926436186577939270471079738047547349484113050446587 2309.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004676339654115838037090330872647906202320855243159926348905802087 2310.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002651370950150890294839869171031043744642388220210692399446937874 2311.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001502981493548381959096700143876652294453488569241869409301676729 2312.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008518351098319188766675496060562788505927730849019856984392986442 2313.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004826988603967119713241941892485878986870947275205702468941105848 2314.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002734739193451168709524651889004758793612403101672181414345522998 2315.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001549082438352932387844523827866709801046543671524688827576249664 2316.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008773082033329901197680458709600156729008467484626851120787797646 2317.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004967626832168797873948043224677006883353421821102898892101485311 2318.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000281232120896952668090055274992514883735517724623808423036225036 2319.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001591842639249970528602849953195392085745139709450487832251179754 2320.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000009008545332544467253594048545763832062836971687711008012329152596 2321.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005097163867958350660084534218387613944477139000693650873971493128 2322.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002883513477706770496103449678015731827590426843818308258435470722 2323.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001630928443992799443051694823397047148965235296941092612539639077 2324.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000009222898502669560634946547818219258445125810213673750925445631341 2325.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005214583445248020275563412666612996195361794552137553535699535671 2326.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000294775614197647800170124552729208235068381164743007569136410009 2327.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001666031822238817881993390362795682285673481684842647706728415232 2328.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000009414449040139373139109656011170040536022802017297327213857864517 2329.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005318956901631363242146456268213661597877214122815178857593584578 2330.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003004540401618420930999720194020024495177946577823184331659520549 2331.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001696874441951747453764378466843287408285777238216274604452346037 2332.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000009581676862044833828342056236590498348321534693218488134704786822 2333.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005409455500595309631665007488798831904663018904401878063365041467 2334.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003053415271186696281399916245893163879529652830344575348748038564 2335.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001723211368021096539704561323452036724487466690258147366262084908 2336.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000097232545114185632436598473768922093805096030835964904629737691 2337.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005485361446699527139338033615596539072403572847621748490463495849 2338.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003093993571817737183415564170417284229815730906613933340626203104 2339.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001744834313972331158147331904562412848771234199952041455436014769 2340.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000009838064759387981664940713761329718033548317365235116919329535859 2341.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005546077388086922004996961020748791325301783285988290622241950036 2342.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003125957046049027251124255570380708568271096175370644783787837081 2343.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001761574386230209375484334904855565940572960209512543713092291989 2344.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000009925215277170490817746571721317421946587011336817644192838077746 2345.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005591134230236154812693747333690757943806755341175046187492475108 2346.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003149060501106345301593988171029144314518655899769794034974446409 2347.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001773304270435870185632718607266144409198271591656926732381038355 2348.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000009984050109150378102223458993456411262807967526433577869968072236 2349.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005620197118600770643844706847992544552774587483458200434165892734 2350.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003163134905660868676680566336508370756872753391578143120410641037 2351.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000177993982054554329166768862059493719393459555344696221338046686 2352.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001001415774283248935967372034766999697419426894509663934129297845 2353.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005633069484820471318954869239917953310633689593569867152491039121 2354.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003168089386268286461859406571393043667302228672329605430027573826 2355.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001781441023561192411208831498456052501126048216243722175440148545 2356.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001001537564062004939856070289832415710333154583389308577252842474 2357.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005629695090565950579601628791026384869799882194164289769592078107 2358.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000031639120920831498784372616397474870217127277493411335231674795 2359.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001777812325442579335227329039963290437351593866732886801579159412 2360.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000009987791170297037654129799561064450799861217888267919677080667325 2361.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005610158043755939031911970063599274098562925485201088653860729431 2362.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003150669919406024483869740218373993554120096306693598554181238852 2363.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001769102316693486297244157763623681453474853026456050366184735193 2364.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000009931738943927942932717716033376483511321486935041504584214608617 2365.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005574680802801847231569186913733751456691231014452866534852034223 2366.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003128507110570511740707243349025514944344712442695565118556993654 2367.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001755402788965936575037997874766433535935449627251608166158985112 2368.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000984779464673316411351431424964521872872609954706492174730659702 2369.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005523620224635417375578301884958419066219369898140521861653731811 2370.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003097642764021632400247671382253465561364403664972654302250628504 2371.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001736847186456029923356485746951351848271624727359324818806058123 2372.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000009736765506524697936566050160862643814834433356439253707430958231 2373.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005457461750540512221255097284226961265436655573904158756225050375 2374.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003058367313612408894897458049175255925122516292427071977401490964 2375.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001713608487561530046464097944575306720727477665151107066721910345 2376.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000009599677618772683862791211781926828111961255778513783960997196808 2377.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005376811859293808861259840011341969782726239529489305006068010795 2378.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003011038054631140981552820164896216780532091908725914013867819842 2379.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001685896562949339975231824181173023076829757470899280793601221926 2380.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000009437760400759755204941325016295475398753930540414832154086667703 2381.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005282388948970898477160187731288721192244684757177211628466316916 2382.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002956073811405936505077408029573804609654405486649246255569947816 2383.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001653955065590180100631106113104627253740832396845840207578482884 2384.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000009252428499215016314212677734218838636768523348039561757334706457 2385.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005175012836266401085476590137027102574935184794012869361886901108 2386.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002893948856125293402984645859781373435555892476455544661798755763 2387.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001618057916249726089680611563089921858362134567772217836432927471 2388.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000009045261518194731104597170065430267904526805832267141172109032758 2389.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005055593084722967784698540025472986388691936700290144008220774406 2390.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002825186200455525906747504784906186721374067304195597030876232715 2391.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001578505454218942749195901337258802000157960397363411397284267203 2392.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008817981966955685948129733395960559725039911404668223967462935381 2393.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004925116390433807361788774453682127061570917096753409012967231863 2394.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002750350390408146410111105501712946731929494853234836156565636365 2395.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001535620327610886030261172295266609767366187464287895292700663656 2396.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008572431850601684761034498520552126544117256285957363563934909116 2397.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004784633265309936828573295524485939711642420838943124499783294951 2398.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002670039940586381534563874766939192017733706783611323469775224499 2399.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001489743200288845749293525917680784186892350305629448919855318056 2400.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008310548339121737415222018704408168156812166445499871587283210578 2401.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004635244263786571575950538835745979445006973412842832126356593914 2402.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002584879546386768965502941971225210047373210592368100104348879976 2403.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001441228353415778733648536937793624990037594005039462346758536077 2404.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008034338953121232781938197870228227765805534796761999660779307011 2405.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000447808599894629652811961087280227410890975596349498875638503187 2406.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000249551221200723906588653564608142260439755952541915476831894741 2407.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001390439258772528193138542550052223883914721817286978136873201279 2408.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000007745856697396176880161554198715844403910363087352371272648375639 2409.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004314317188678370638209988145242580824283789627879875452353897062 2410.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002402591428360837070497968339862451154166986422483696645900786791 2411.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001337744198474478850829082039724314467005464378482278739106951404 2412.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000007447175557095863614727328481469282715845399409422229141903782726 2413.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004145104962034130183620822097246294083531189389343284516725005068 2414.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002306773528435481804861677754126156139430892393081204476510882418 2415.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001283512001657497152320091998899572101838989918507014703473683091 2416.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000714036674637153557877014549893128510984653387041595711385114433 2417.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003971611640863044504261834722601525242148460701499334726335309266 2418.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002208710338563440141425663400290619148857920762629754675702082088 2419.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001228107963274969099875526422296740068544376224603115935287390081 2420.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006827476067122782916329366846557621685376593224286745105637821477 2421.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003794982192711692812997327404630064414417235126966698250352277858 2422.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002109042232813481890892541544147286842080771935234203101924652293 2423.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000117189000354854924781410133047812363896666677269709978426930436 2424.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000651050269688947146160919093889960530523578691733762915151970604 2425.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003616332528509033835853875260731228930342777808357351713746934958 2426.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002008391684680160567728644983930815117968802413427975653463499751 2427.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001115205119067774237487523836381665159362371176427876071545499365 2428.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006191379681366557842039381211986771995884690022509776392570094137 2429.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003436738793472784626617675150504452330724186076007210232592103772 2430.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001907357395832917357572492192698192149068227555046764122964974565 2431.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001058386168273862893681502736560996854344309177101735885416634636 2432.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005871956359781481311633939376069905517780317435092075832294823369 2433.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003257227772712236350579786236853917355160559942349825670284364233 2434.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001806509066331684502245461797307782514286603354782183183854510178 2435.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100174902533285045714749190524245575420523594890892438596312303 2436.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005553982901831817400266323484009739707404624358485078166146879508 2437.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003078768504938398457000807680007614936730104656930391333688356806 2438.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001706382854845054789396481698249583894976604784417146787914167188 2439.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000009455901274432675675815799979748595191302390889209959744843552444 2440.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005239097084379032237440408901454097493734293879072307822326052967 2441.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000290226516927295817703662871020277806585461705916538154552694551 2442.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000016074775614394587272240301484495248732341813555559691927534727 2443.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008901844316659379752747719498047821952878944300770881107452112307 2444.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004928813385919744820981385595841802229383334347613970341767059822 2445.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002728551282085863477057870156107179520449615393702654284980407826 2446.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001510251549836980632029295825161276253491532136380201235394473635 2447.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008357837886253377707320096923482921102540795544887159429855302345 2448.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000462451442820444064681595733764500831000862731498137320869412043 2449.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002558385213744111933923066362806361984586950155229206949791908671 2450.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001415120411025442787409199499182444159753620386870612134481791474 2451.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000782615732110243635830134554732551191416571261228753476166721709 2452.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004327444748319751122341245137683124508948753026785155171286462934 2453.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002392447009706711042346438539524829661248863430881540114512892461 2454.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001322455356065038334404664510943703457512993462998663180379003578 2455.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000007308826758683069029069396666165092846285081304167334993687019952 2456.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004038706842024744919359833503757438327727841613527745266143287748 2457.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002231336477054145851362750463017631664038539820465882402993562084 2458.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000123258231314042897983978747620328935763363051087592192623004624 2459.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006807615018889327074971806790116566797779215998411226545756208798 2460.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003759259380903604218976463045247621298739465768892704014974247836 2461.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002075572476704019451382525298767246655422320897359331206353340209 2462.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001145781692572313696171886006227407632305356394374216105661546339 2463.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006324035183154702857510679340848284537995995071810038544772846101 2464.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003489917472564391927341767762686535543795279380403238199557641174 2465.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001925593343549359302106583784901113561432357340616250219075769393 2466.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001062288773789303687778355929919169223004094953299106004567034734 2467.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005859347599040948411587518202644563259947813993497458969275449919 2468.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003231354805098296833990720507492418553878000003716681662641717108 2469.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001781758341774981638765330296101969534490573056779846761828519657 2470.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000009822946602752175224941891230574369747807447146945596743081352581 2471.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005414565997046024035219239716667839630447406485605933260827469556 2472.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000298410749456352560017932841105667995162806035778079529281308035 2473.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001644350050775621319497919226842946304629423084510088852805333069 2474.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000009059477423001828072420727573961058511962131550834625766887015461 2475.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004990466373992489981314791645572849589211000976594096274508929833 2476.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002748579437453522343521610853069058745325434383928998957554209265 2477.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001513577568440050199321180610829360998424914951577981824429019244 2478.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008333556028165797810248448510694876355929205227597398307696421138 2479.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004587598274931234683396237021957353119097480535851665615367067567 2480.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002525048958875690877159810718213744285852528443041678715690110527 2481.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001389580413015544360956025111062425690547528130587947126352723455 2482.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000007645872992064699695250611325814174551802277950007456943684173809 2483.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004206298092711257802679982946728082673072168281128664096071514465 2484.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002313676541289769665041834729603410880873225666597233704847330328 2485.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001272433002188130009802570771798428884745868112469142564631420638 2486.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006996759525179509350094998287173962884159229859875857046199753655 2487.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003846704000677956874214439473608663477933731254384224738179399345 2488.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002114513417798827452615137682642994698702494272335169210891920377 2489.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001162149588202051302553339337643459414110677965773257035830812955 2490.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006386215763010002680609040147996297528278517369161871096803865429 2491.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000350877213863560597784787371711316442301833935803473391112854528 2492.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001927510817714440786397751149551123299747768318438388778205561542 2493.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001058689530539431745597113223934834994669457392557828137261777936 2494.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005813940789976262235554396763621209380951667753802937273136576379 2495.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000319229368436056578655661852808189432242680777210282100788744406 2496.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001752529659916383035175548284314972153667249914358772375293334634 2497.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000009619627925896787787426380454378321558476416760060854676783232701 2498.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005279363768840207336200226599937317124304693220040135702149361244 2499.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002896912461586351398535968886137475368664846353597332625504830762 2500.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001589350500825198852259200049150639563431720081101510530405310709 2501.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008718355555307869103072841325356672907639238165146924969527850082 2502.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004781675586170785881247669570424321824249532913639451908915519136 2503.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002622142759435221013164283358170316541134607011408451962527665685 2504.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001437683557997948805081826484626152567166029729183992554136228113 2505.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000007881358509778774551368370563247564964747792995090898174673920702 2506.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004319860473096801180689032100494913895171415477388797519239404018 2507.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002367387066645078776751287234450178516242264702712399504344686361 2508.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001297178646823924147441874092152689612332692616959126300104173523 2509.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000007106591234795973371086547314815186817228511478293173322064992729 2510.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003892727115528028777467762900076805422807961117196033383201402578 2511.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002131953455537828563326473138866491276578799466657990224069107213 2512.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001167434885924089420800307486157345917995528851941738753132110698 2513.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006391736443172615600340440453449084023456694195103229048438218789 2514.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003498938827264607134644764098342356922412028987145287352540910388 2515.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001915072384430356144190792308385585853616151097975157944251226853 2516.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001048010046053144112656825359692236676570754378644229954718057948 2517.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000573425708956201123042496925267064735868266780350430939035485927 2518.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003137042421135862993496155233007692217392187886767988340753522181 2519.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001715912722095001217526165669074358398248053396490988283312562238 2520.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000009384294370103452272824379553122388849644136720484178804109934742 2521.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000513144561607148076857838049128603392564033956605160574380376397 2522.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002805495475825929483602851958240737952995701853503011815798162825 2523.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001533596833006010175041977770428651599280489599321756615647224755 2524.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008381942467763781990546936725289369622043214208533922607631254216 2525.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004580470013994224873756886664607349276662892314166387631461694524 2526.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002502691757834286251128491433986479997251908759594508484172784382 2527.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001367214596632685728770766592087585232216531100581442039123447002 2528.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000007467892663570808616810376836091500960514245027724639854579141541 2529.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004078416354018400923173433098967776528827045366404137276112353682 2530.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002226984617754291813435260292167550958667987447641308465608146507 2531.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001215836267238859246023689142181785187112077662561538121345630745 2532.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006636899522420108841877179928439604517390892054412369646172070959 2533.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003622327539422449507727430525750481626552437906732766740073493561 2534.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001976708236606674181317234418288102729255157773740482452089639884 2535.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001078524111918792756802031012950351789631898068051500254768320943 2536.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005883687956543170261543458813052637801903820468007236862546196719 2537.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003209238132005666818928329573346130546074357023189857746408071068 2538.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001750196650424911862056966577019801357103832436561877821503685649 2539.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000009543427934583052818527576765822652924483973042358948294099580811 2540.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005203009836231387688277220537150762840645401798873733794795435697 2541.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002836205187493383928515836518441426600451179895850454710389646842 2542.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001545800528954783333750539371336721565194840775328118649488791388 2543.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008423684906921721197672347983649713803022679874710355966856925332 2544.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004589693511621316298475430306189944615490678380657367578263379909 2545.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002500335114926822746020481535441269143426120014141388807268143225 2546.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001361901726706076033179494577072393794490781532657670268320296701 2547.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000007416967720842752699044919820737501946926042082800831825509034759 2548.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004038686363623635134564549433384171920523357388373511735378109376 2549.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002198806642479369487735065234501754129996217068854771816737882971 2550.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001196925661379300163822896662477725509092116888490173389639841313 2551.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000651449258142098403893645517261781899385526679630577128161199126 2552.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003545090609382864655980172033566960255477232528590436751816587867 2553.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001928890029937878383548920809492917149373112682066388493993115379 2554.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001049351605760614210704757601161003320480932301277247279329606901 2555.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005707791249457542176319822312998475219560776015329833078345604189 2556.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003104192676884889264626023491520092857281183893295835461540065914 2557.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001687962715744269248073078388987650750039856934742047064727789248 2558.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000009177210045685745402214581767046198099889736765871819260444415768 2559.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000498875514563601100491874895255847109741026310718931497621633833 2560.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002711486534818759150389799119733945841480463623896510945056506957 2561.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001473521624256423372540699029012397480421906712713572406055460356 2562.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008006439476178818689625851957217444124923365623331331470601210207 2563.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004349669285550855519544957211518920760273051713408605854007571688 2564.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002362691418278229368634443283821068019179387803852798197404128744 2565.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001283192387218110110055742916036871533213790594351059594155467609 2566.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006968039453229124383295685336906683050789836200576439245207351532 2567.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003783236883009127972966153200081840410618138068205779746931959399 2568.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002053764429410471333662421771622490131925207765636462058220640306 2569.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001114735752956821918381310639792485580276395048588214299441121432 2570.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006049611623730978413787940359313548017084489882336109728339539687 2571.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003282595507729667553169671556694504326145198956077166586238674881 2572.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001780908515988575119517736345610638639591014777882206551326663882 2573.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000009660514683184031176144274270632493339249227777258967178314287679 2574.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005239542708168696259588454963805709626962281904549612769357591227 2575.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002841325708187486483538675855824814483911282814532052620763746639 2576.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001540576341585946953200996165573834267167568347306689222368937552 2577.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008351799226529225247809424977093422638720956592850067532476798651 2578.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004527010852812771485293400610824746117749659272509749133249103009 2579.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002453453013680098220437632424818188364687485985984860421895119209 2580.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001329470560165061920997050454271969149482683511419179804790891325 2581.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000007203018411982888009852817756034226805535656079756673633442676162 2582.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003901981411477345670595543582970787545864051448123187487521976833 2583.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002113444214657367197596640547703103614782893609568108883418750155 2584.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001144540997116801661558836369383119359776189756314572328861305743 2585.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006197363079012741659909650491304220977644302138789100255918540792 2586.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003355193717269744409619574602372422932898985554634325149723428838 2587.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001816198789915463041401703036505760891457415053264176859512672061 2588.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000009829792197597737955761894264122631332217188581656478229026180045 2589.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000531937385999736801921955364914534996250369368271389661615747924 2590.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002878140333481730846731441424219268224020415756416468152322191834 2591.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001557036306311973374928739692669904508835914677975269349999491793 2592.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008422109546500526687924901604357896614944025645022338362233502958 2593.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004554895894657869290583223867651220314509005374683683876142594299 2594.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002463040182990608141441076624907455340523251467087563832198070355 2595.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001331680563991903142836648429002878379983844135582142984618207519 2596.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000007198867783006150246268956306923681034411295823070946689007230233 2597.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003891024405296037875014597408921359270584621808798894520486119133 2598.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002102806851965655239634599323659150021681996403771403017445131403 2599.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001136241200321961929059571711079872073486633190253355815296714798 2600.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000613871473498524611123737998943264898333719754283395726408881583 2601.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003316043287557303461296838158205087535401863610016390007981586461 2602.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001791013250643013525772955788023421527927813478447967725944928098 2603.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000009671934011903279739396525243279034830950357467082956901501915051 2604.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000522232406639421320891205851611935865816814712005495774136950415 2605.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002819358662409391360558059162133064243712276575417987758748046195 2606.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001521853695336658154140853092375399771958430999240431720339898416 2607.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008213563011675062152605722295326325357793507834529970316412264005 2608.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004432272435319484147711462284615988274835817358410357349769645721 2609.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002391429120757702560623092514181722478230539675932534367003043475 2610.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000129010435512075033835612277167244881065526505191517519383186406 2611.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006958705860997435703329669717226588281365468036461574125940114669 2612.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003752912829662258705307260672810912140739101079314734057176751572 2613.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002023694178050638528974491688780384487863175091100423908179719106 2614.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001091082886688999992566171614852597156903416059202190517071840741 2615.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005881757446040006474669393270827826397409302864541005828963930166 2616.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003170246498426754769539796050906112701767821457054113296117516827 2617.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001708502246939355562537809733267573088883447589387246118116665102 2618.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000009206079642226460511546880204319185370084836298539326057934423678 2619.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000495987349964678204713774610125028262411245131951244077935366933 2620.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002671795535206216584669064669065117094908723346041572168024162046 2621.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001439039235275171463275968243679623023767748470570473733469812448 2622.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000007749592996607957791963779447186340095834798413963172580781893081 2623.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004172746921464690044649520047716846391841114521847697566691184571 2624.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002246477828245299275897829587431476641116470883303668500031523216 2625.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001209258687374420888890891789592996706127830130836222728082749293 2626.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006508386440984770484884422373638914074622895683378262799349325357 2627.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000350239021837049628688077334418180910835499835426848192322276745 2628.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000188448577257548552529930621177674530304890531738356167653356876 2629.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001013814210724035488427223380423387049424912599259787205395435569 2630.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005453321642588110633292031135257826961439476770051822676718252985 2631.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002932926151929327923488742643477227929671684998772708691214915257 2632.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001577169847106346467233235846244509081785704094319522001936962811 2633.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008479947696279490856117001367165522764746828048548064129602295435 2634.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004558744998427163007069825047538304715399297339870392174564977601 2635.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002450388196527437868102984579332059383530263218678590831925316032 2636.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001316927895031280742734513877551564958617083689397216051463904543 2637.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000007076632825848198216003893885778205005149970919312390186990203196 2638.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003802147284692939446483402433948324245063893906395784799899811428 2639.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002042531979964723843308458141493523697032135293433542742545689326 2640.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001097100710322398760415035090146233166494190263722573944762451069 2641.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005891988317960388084998047452280882025926066149126611135505280723 2642.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003163843950151449687061356984422712374398876948070822135109883746 2643.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000169865849805544707001305545333982731706570226183631754831673248 2644.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000009118743264698906255706482743513265981238280387210563178681148512 2645.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004894426810398087014514013353501420205033154395553467368513306481 2646.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002626677013354723616409712327826648960571909323078309837383991706 2647.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001409449565277473916201906517031662213249125138232782002538270833 2648.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000007561892253077498650450438784933199602804189295998914187271497066 2649.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004056482017579265300577952063671841643945059826340119280567127748 2650.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002175739003039811178331509612636425320217284612197276572637667299 2651.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001166815682809126440192858175378241495196826766786684906795306107 2652.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006256565325550905311807689679980107216220666044361202812964265257 2653.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003354347258438267354105076745573481041284762730652912908171288627 2654.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001798118943902446841615514757091060421623447369834886272010211861 2655.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000009637562706772410922089969827106833020825659367958437735250850188 2656.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005164811409670982936384239872282944420554267037853004981948633492 2657.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002767452763322291510522728631735008177033822509060163089687981577 2658.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001482670030369184551121394122770464030747535973299273763721828869 2659.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000007942320189027472714744549523052869814763152949907918368498232086 2660.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004253915998067322003702227632298494133225849298240927273571599662 2661.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002278080728567845848264360263505250327604198600483174554854890296 2662.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001219798342610212377993470776166629956577549257189446929331045352 2663.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006530489597675097932425134847148572177072041616741179524993347198 2664.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003495765285945400873192355172277638874172403137023804755361262924 2665.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001871016661489175882703144023052158820955894760397232810683071696 2666.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001001271734917725552530921960812625399224531395073199668713328979 2667.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000535753745705575553041204543781777889610493669718878164177277751 2668.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002866272515386153169794103705465847215064565875139725488399214281 2669.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001533235237360803096669880477521808617731086463375856870929095845 2670.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008200478595906792304129602739345764709448011660874130451533977448 2671.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004385395308235082489980098694221076894312699693113738438564900248 2672.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002344863044279531470690715356746657106961582219325871936708642691 2673.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001253618660903798875089703152130849965693770923800959193532134877 2674.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006701201724915833615894155568520616956681677079892917115636438367 2675.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003581617946893961153054276825952619035462143624619637399742741767 2676.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001914014358603979107602685148610650139102324562965702406491802923 2677.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001022705375342667327421954302992916304097149725381952758866571021 2678.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005463806925359323704431741208771728178745139858428768721358093148 2679.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002918634109355952825495580890219333694991963128544194256077343566 2680.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001558847073234246822747426744331373538605325258113184651666230507 2681.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008324668803618979756488454393135559459868156267830442961227836931 2682.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004444982499265188018213348527177038132295000720774489086464006339 2683.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002373082467330011998114946558966352597557349352439578341339721474 2684.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001266762898253101401780344218693510699125021350192141240067134319 2685.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006761103950567812182439888826813114828793927579622520818535795593 2686.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003608109507694917032827827382522114737922575466493927583275520992 2687.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001925225789046254748471241763094788803524626730153280138856574509 2688.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001027125566074087666800914032603198879874894643385030346686343925 2689.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005479051229055724711150064175117266886698693109153704722911871186 2690.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002922316031246512088419115872200660114961470790114878840558040739 2691.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001558436252333222411818733903133705857022403228448689494070086437 2692.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008309808024359083241806505298059517715316305007872885360485476206 2693.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004430299335090581231280018687426712731045595110129935212941274803 2694.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002361648864134551903493824579793901523519315394872963478659519254 2695.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001258745125737532067667476417512675644723589883244776712387091445 2696.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006708115661563354640642192157238209388752195321351876119546485484 2697.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003574403619712337510228964094747598661367299266812373830469443061 2698.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001904350978659586804125593183617292791188250115477400079434308396 2699.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000101445034213207753461734672108822769791169609037802698091483226 2700.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005403249648891862420198996211741167062230452797697143102275334685 2701.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000287752933966330245443974913200149295745187683692811831259354934 2702.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001532233723321482428210618675728900536832958499184204285020983878 2703.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008157758536912180705034843342712896503674469280416269281071826236 2704.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004342674652460082622492035933449743064158915441407862809789822307 2705.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002311449463816882039046900525496407019291163266898188487589116653 2706.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001230133418675370603158668754236043784339412505282788213635532092 2707.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006545770590082269443735308743635097479678668144503081827697109544 2708.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003482652454728865530798738128650743752786523949397281532009457893 2709.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001852679645232859179607593533206263947911167885715135337561151707 2710.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000009854427610858179069521222546146794888824079987150620374740661115 2711.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005240870167897506048126327341335987032213306392318014487932958484 2712.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002786867789754450310002173234163053671195272239037626439223100684 2713.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001481734326934631549099670230264090551548998830470924172493088175 2714.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000007877082115377015202615481978008608460241233356923145918396710016 2715.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004186985929944697459992431227507416735843093166045085865285675219 2716.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002225249804178872991918689866779020027962368829681885768954110059 2717.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001182489317918622615213969199535144404421989930574246358703073779 2718.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006282853148530782716803791111440643495110593302910972837325386135 2719.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003337781189728417784425624444704091661954554761812218306285280791 2720.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001772964872969986786306101541983620705941649266576201217224763852 2721.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000009416375248657716810153849395272819744830980142400828527378338415 2722.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005000446483449029286561498633243077655986831684458186279618647246 2723.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002655065588149040672290147439646951359261208153421332405387615136 2724.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001409558844327818532346298724307189806461787686807783109427390571 2725.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000007482258314915650976078075360531387085981326224360701435275651839 2726.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003971218260114251591811742300031708872134605845075979133865495691 2727.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002107445883846858179629086727529747775743266430266695890291302527 2728.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001118229034115906130074661606664468560810395514784030650088045018 2729.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005932623641871554250196150093966494452573352610650507446937473657 2730.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003147056758763620893131704032256366695822554862466874628260443985 2731.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001669183717736745213123175181203350468678020900013989248912619667 2732.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008852084127334155041862614932218844504276836508131752073394785717 2733.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004693845572899735678918328836211949322334406470108196175740875508 2734.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002488593577357095594509039519537323909962437731931607952699566189 2735.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001319231799543077757817200078704762534625213915912226471758186283 2736.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006992464104219730693907108007166397886416499019199985290781505149 2737.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000370579525911982906798696628514157942698869338976439401706546358 2738.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001963697923751953219925773566326929877275789471204449840870471718 2739.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001040423292877569079011871163758294402868661599487718330484763894 2740.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005511726010415483574135018938501614186740223615750191810593835597 2741.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002919492641844537280070354627919035212973391257927242739067982188 2742.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001546213291409809209393046496734720225224096765889725582803216552 2743.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008187922327101483082959409834182325962308262479985353560829165029 2744.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004335312051462776239937287273050463291658032352607730089412648379 2745.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002295141103407808123941659092536636524140284006896770706859777961 2746.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001214900859677371546921086364595658246333142029748558975869004569 2747.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006430055933685493381245590890351081035390341334531783307309504289 2748.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003402758808244886665373364546998875387709299531372741568175470797 2749.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001800487578157877351319214123137899649748217217422637349710985601 2750.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000009525585518156758644633696439812454035084642738523289671319952492 2751.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005038901412128723857593228249117269522910594478817873409789741858 2752.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002665156486605988124949311281637591082909517002778797386852042321 2753.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001409458458111793927173134291568074310329656814683645838034668773 2754.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000007452888111154237885497703041115357195499335133302543205543521305 2755.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003940394634408633804016313279165645996786577754843238654788599997 2756.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002083040312731662616128645513636452219384480793252857399603926217 2757.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001101028371942112039112513855978547181359754639352540573846148378 2758.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005818918395480782027633632313193699182621058498224872814295022844 2759.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003074885761837656207753169900086948513192442606087988032868781386 2760.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001624645859413272404571292406115884085274203577373102716724912061 2761.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008582849072361199627993195398147697236470954942466631651435147896 2762.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004533643111416239322502633879011086264892044009879175124339780977 2763.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002394452966649689808280420296563864785829519785845093967430860885 2764.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001264469798305221202178528318529125874730172887597834615955484865 2765.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000667657636092003324761536791752315702571836852283602896713931211 2766.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003524864533611321306033882361583966582873948365193169544137522493 2767.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001860691327285033962169353346905440542365003204689582877640697417 2768.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000009820861097735620167607133165930366351875551961346823248743781581 2769.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005182844483531962838370900022140118567667499332756748348821933845 2770.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002734829155872206651515319558090146902696862247891949671087161122 2771.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001442898162690487691436958841931779640549664194288958273294141608 2772.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000007611752453442647861738874985639199806795173598245340983798375838 2773.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004014922517741497728870702001961275314293935577618851427641572901 2774.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002117450462115692429752837830987237906626856580610406035302645934 2775.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001116588029346280887485344191954058226700544596329344997242580027 2776.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005887302286785604509828679816053664678903189868401122507814268359 2777.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003103726205193136624274945533195407645673693835250564644650891183 2778.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001636041104430906081491124795875966120527162021313066298234170259 2779.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008622809654834089012202165465872630008144335323062317004257043595 2780.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000045440929776490339024047635554547074635212383821255613789123249 2781.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002394360174315478394191097130206780831909605051231910398378953701 2782.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001261466301556314071149773791356889015965762814689074275841186166 2783.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006645164997635536712460546462863575236720277657260161932498179079 2784.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003500095278984904988390551808034740194735382952151984384977339053 2785.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001843308337889677140784800923331658898063462081024020423023064454 2786.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000009706444401890326531863819108153133252367659431016158665031452575 2787.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005110536072639154580544729287326622681468790458127537196257421631 2788.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002690400209636547311250269018598057416546077195172634143491732055 2789.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001416157319853291497008950525260451556905760796912181626394425723 2790.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000007453329791519156326240577699981579998931799207776501684425456582 2791.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003922233439820916184734095414941342987622130161469543034484465079 2792.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000206376784316206128824148682498398820938028625037280533317970098 2793.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001085756873495551915666832093506853842075088697919242332801851138 2794.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005711480739235224297323377267821265564512277648659307041078276571 2795.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003004064606300585265151789472270787024289149422077311204798725579 2796.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001579844283073617156632020319700228496453654461998276072049169114 2797.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008307374701608546461029994961400742785527932074097010170940981281 2798.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004367750863245799320855778169973939125391543742559771567131873648 2799.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002296130134752368691482832917615566575856622105925458461472210143 2800.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001206923544799904043753735823672414266300522381661947018529773775 2801.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006343189436542046479326983724548462382971223262522351645393197135 2802.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003333345295140307369393132642385283563748035582248681229429728771 2803.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001751449684986922626773389330854036968163557912670611742294665379 2804.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000009201524916622762773476301344865421780132181006518059955417259604 2805.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004833556125563096526834240162167452472138361662183658553496768889 2806.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000253874208833220566736873439075701343697369842553285417054668827 2807.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001333261373767357169671255606784794422572152797878303913814397392 2808.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000007000949546462234623475544144709507993676795145591837948021881813 2809.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003675729755708667494547474565268701609998443673335510805870277363 2810.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001929635209167532692474075235672482197715188451205718757913439675 2811.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000101286580579138977999606010713164712529434337636432254748088724 2812.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005315861990788840526986866426765281663573550258292708702437323684 2813.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000278959153853131447221914503953665362356121945868085158341707793 2814.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001463702139844069418740925171813455435816544635576285010259646489 2815.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000007679094434242468282801255420819235482389526451701328762074583679 2816.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004028214217077047465122389420425185952876024858778652003781273599 2817.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002112809763858409753436026574637400402080576864231015011393542803 2818.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001108035204626905479915222417783124152542517188906382677118152157 2819.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005810213462804621511611356764440139393258053304056830563004291405 2820.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000030463235070403752765081323744116285411200149641149328470411423 2821.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001597001771932966531561119851844697010537967116202524309520039643 2822.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008371056164903441294142775092429611997392718232690307887714054425 2823.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004387333353692853162651220906461605296580541524655995810041573151 2824.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002299146104963641602862789352725185543390692616129826189146958862 2825.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001204697764609676762501523509669668508384240912105391055199689659 2826.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006311539060618756809070973042782528725367917580992786614792595016 2827.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003306268492029728379312534882877440667852684214183008402314172655 2828.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001731755897122527739602161985380717788720655054828732453586353354 2829.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000009069450673013058203083624323708102532878609222967360423488760483 2830.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004749206794187204152001888764610968995623382092034329928509178979 2831.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002486606161826296225135504780859885932905674061489919078970918571 2832.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001301783601848377819957663427855363598958256079903665697322509432 2833.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006814225350882415267897913636269307318665520463072812515086365721 2834.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003566482764544518215623762756290755701469017462304450899766223156 2835.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001866421582648343140710989141565557700232691816682405164361594981 2836.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000009766194848032165661849279499520680208923848962847589360514662904 2837.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005109602061846344648365264585701781007680359601912944113731709582 2838.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002672974814732775418200191545844908621529242916008378154755608259 2839.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001398133970374513136891843964915545513958181171000942233768640571 2840.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000007312213774456225333306403616702108338247542774442753004758506383 2841.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003823800097538688256372778672838102105296825407745071786340120733 2842.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000199934493444052710696355036654740341350283870760110191104065772 2843.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001045265358843850210692656977549304505613250800016589711395333455 2844.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005464012833173374147752740593135037167867536197042643084820632144 2845.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002855901492913292644193948478808639193542548111754845404297430282 2846.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001492523348252697120848508112438644497160198537397433851830640442 2847.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000007799117782479095330913413928889409744018645799706748352916921531 2848.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004074893824693091960651612806668832469060709453140093452537201108 2849.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002128793942418523977797042881792842843121452223505604103332663356 2850.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001111981346727800088946024489559121906188818164051644570597962458 2851.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000580775067348561866607795902011421130381773734298224038056740764 2852.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003032948587048212140570639424242682251618051317110874025350394891 2853.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001583684963621948700605780384684615201947436837955837120477464936 2854.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008268357306383223772196739526945349493509780572670580949958011973 2855.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004316347617480024599864999625773401816573575007220261612339011146 2856.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002252995701916205580610298200664826573560027205729845559080953786 2857.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001175847831662296129170440475821272947049826043788985339177776398 2858.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006136047725802826651715132907716677316713698407151176404969451311 2859.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003201645321700132805994007205018610409878917230333778515850272388 2860.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001670339035002275569894908460266937710713948867677462933036775289 2861.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008713306681354135972490786070177362812559146344781478066678232987 2862.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000454473290890346071401736686248293662309066367566878707657844635 2863.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000237017690713474906349340639466030214226560319611810067048432691 2864.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001235948085537038842520515844383429615664761176888348037589358296 2865.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006444167230899924992034209785793157900259990101259845943943375039 2866.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003359545430993745844420941574389407101574632917264740285635405506 2867.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001751222847983651086948084937945573059825879069303994242641509534 2868.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000009127451377361142267956947282148717776437490231920610849584875913 2869.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004756690508356914812907751777366006127423005258021056032849981289 2870.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000247860632033293283903599540912860777900532518726155100404739942 2871.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001291390352536459822977344414733439436371929047661705851659647036 2872.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006727518278742610082432538646236242141586862544920952682489505141 2873.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003504286743783056895100146379181741584821886399136253318804620283 2874.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000182512167312652186020396543949294950545200637417372156613576874 2875.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000009504548313743932378244188769213409567315583156552808680917369207 2876.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004949013668205725075795428484111065168572640929481878763753270091 2877.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002576637781741618749668002387757613213228807540220921075323397167 2878.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001341330102625463528845711741769655532299400862852807833478337277 2879.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006981770865384535275345004429139679706121073959154594576428030707 2880.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003633650627910856512814146155668428743222938583513363447415898257 2881.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001890899455258548514232992037843573335643453555837482535668778896 2882.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000009838784180887190339397463690210795062696833390149075135703610356 2883.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005118729698124744864895456002733191081138121112314960827353205661 2884.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002662752249393620570986266047823019182418614012738430150854868375 2885.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001384991669648536834971988267838918500588888121230066460523497498 2886.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000007202967177721444966741812761033906253939502084755945358739056887 2887.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003745619178198959551992809584907651855478274049877118303723570344 2888.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001947528177811481907976900490577652790732197448029910670510623268 2889.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001012492609216177929518757097728302910548059127523289995431491954 2890.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005263177190598988328352006055076575037902735272807653997285227281 2891.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002735597351243676623289858972505238331756239891776584147973708472 2892.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001421688490495824426883208574840772873446622364248342912525796923 2893.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000007387625064623523011025182087097517968549730483415245971700623533 2894.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003838428065661471450860114919114475740806758103652625088577160482 2895.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000199411483018966926503073884339683135521698263797867148583406386 2896.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001035845905337619693167643907956154358578676650146109366578214087 2897.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005380076025601322665025073815540410164028755329781014619587576996 2898.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000279402299627299838113830856040275880499604457259792799126486878 2899.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000145084120033476505578284207302051136900905514463925093841637358 2900.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000007532829879562593305657485604494354523139730574103205114639764043 2901.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003910613096668587067408936973948838591080239963174361566675807402 2902.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002029924981264110235068071461605118443114447930040527984315294062 2903.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001053570516257142004664429920008036793744218006155258295250917352 2904.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000546758756165726078682993740974980320915737190581178311448077241 2905.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000028371117247703841716333619131842916542992762397884028764190386 2906.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000147199291296244659604838157038929007441840846299155581425604803 2907.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000007636311288504437078547044985455999607023891919694477499099942245 2908.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000396104875523660609457499987412487429539117697465493659758111034 2909.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002054402084848345642948735266420837928240254689785637002395367312 2910.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001065392012192908978588431659642667490895900862913259239590124727 2911.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005524362783776192108359740229361517366966348039446399457962496072 2912.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002864202674646178851957980140129572243148897658085439012056842981 2913.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001484821121500034017480969312802182974092276515540209028817299901 2914.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000007696502208454739351054226900554552685640491150197735464801246616 2915.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000398897730864036052255604685205579170271830865546920690574784492 2916.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002067181807541260343503377242500896770890111004115886262505378624 2917.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010711363523380283159965291113396223679539477236974602420163438 2918.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005549576648270250871277549430319606308209000569747886615225383171 2919.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000287490829074829545132674556800358882480274226514502737120220841 2920.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000148914578764130270972381594172259475262813155075432858283861166 2921.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000007712577747987290884716251588217610772531813515873861082442716456 2922.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003994027430663753817320169039770501729665679802574687351252844903 2923.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002068100867282696040737287946048423588970099794225039262912124525 2924.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010707340415430262633869083175095483679461298909307083937350232 2925.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005542947416308710571537968853387726098587653297384190669425345317 2926.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002869123194888820228148712756861787019463153422511168420261411218 2927.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001484933340643798558157343957282588431275821240054068052357010167 2928.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000007684472824948186330663112573457161379208578275111746111328602861 2929.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003976221718690044261119465306269986276543631021387427501746976291 2930.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002057200092111393080110243651668883271888189360497952587526840234 2931.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001064221281651560308405225866174545591708051122875391076464077215 2932.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005504740373514820844352437715328488547612223975186596500794867883 2933.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002847024950544311556790887523173275209781275577531302218589980034 2934.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000147229647225681008676430697297124879026165216474508899254617809 2935.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000007612877996490002686842581701615138758845722094821934806480650117 2936.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003935972928528154114220715604557178746054600364968647910909870983 2937.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002034721641431910997711385351009753900542589064812409621569937319 2938.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001051738104964357712007779753577358846926255039199129122130728655 2939.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005435755960517395804052235189599323944430853616494647921505276305 2940.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002809066781528564107942481507768651087319113783124483347873176525 2941.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001451489782248881276337733691342756339339623706074427546584396049 2942.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000007499213908053189952077054397450209396710098151574279317944054586 2943.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003874069201192638166739466987770448534536138313833885578248349161 2944.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000200110056455194413256954727999834877949507921898604102005319103 2945.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001033523597172146038055089831711104166027912315208986385091957164 2946.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005337302956803127415453698606116828872289108583017847837780099131 2947.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002755962621638906556503106030454778592134986329512645279957809635 2948.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001422901492495659972328634835750324355255465763647416717528819343 2949.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000734558560634244976863877343518273435715984645072607432653285901 2950.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003791648985858671629534206327814671517451209654359266919854981275 2951.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001956951091538557179349687243880366571130931725124904850113967083 2952.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001009908429820478549466840254828568757670839220116157140582098862 2953.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000521115793634884462226843030830434157753074973536900973187047298 2954.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000268866516639353792015704437043732192523912384949013050885619442 2955.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001387041597552500635553074858054742636640481470755125252290811688 2956.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000007154718725021539853911958413257000415683245499314452695586920429 2957.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003690166751607697318525245072555748129990261832668182928694852263 2958.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001903048248711474291821018733621103388331249268433947031432962433 2959.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000009813050224273149206243009210313018907283669835408447106943972975 2960.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005059512720300025302797969851830836734756902909507769981264737253 2961.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002608337854282806424767511652752321627066804113408543072108585833 2962.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000134452694906217416036086281254744976899811912960507418351068634 2963.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006929880204358248007131452985852108499750028339715202005667759526 2964.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003571350908990745045915655938476015869655143500349187170675573536 2965.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001840305556089991929346077917322441311113215607435999521060369167 2966.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000009481957371166540659287886151111849039019461207702822986662303893 2967.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004884911966071622334578073135704456348530229019341270796367041452 2968.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002516321911774581242795271081457688368308709004193613335566323515 2969.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001296063874405666880580191314066811042086847458464457776466305853 2970.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006674786718351788078341216927425728009666613783214625589193687387 2971.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003437155616439702393702766255789474633269942068762314866489037781 2972.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001769749689692760439965077400932253585981323965736245558193919882 2973.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000091111957053097650244729441350656065991987164158110225256001316 2974.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004690183336323774598571119323236127817474142699938830768138734157 2975.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002414099745135781551019176897696314920386966676405355379306698434 2976.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001242429002540992426813968808304881944988205470075144656904898052 2977.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006393504334794698254842424394259216087504245902346179602571800037 2978.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003289708316403527688143140619813345823914001560632831218820193678 2979.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001692493073074103011999729727465273676801654106391495675716224501 2980.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008706578466082339669958715843460061835804125099325748879191151574 2981.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004478362874741317421735246005415739623442458710574430063467792999 2982.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002303256048007774654903420224324879345323723263275685205676256708 2983.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000118444900969874146300713736082174890982346426626679675717773063 2984.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006090343114800239198641397415417763097833399182414232583303469009 2985.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003131254927092528506282793882840233959672477233869625822449710928 2986.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001609705397821078476597487752867868830300398932888099712938452219 2987.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008274194286930565064189333362853067365095414045438600996061369261 2988.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004252618274972857136691969742627771968704166917072403855607593646 2989.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002185438015341922686635290516159066743225333872045475619946248165 2990.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001122980004131798151126883470676430857378489999587041341628781466 2991.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000057697503679817034631250631247456099367670695301206728783921659 2992.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002964104608295297406235183215897705138459841183256670455748709399 2993.0, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001522585062516070995610384813275571494019768055761769837622163805 2994.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000007820259608881268150488340966056100352855367417443764561565073088 2995.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004016172667975206827469849551528382144654643603788671059968187782 2996.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002062316014077122078390144422584709350064379910077036286310951826 2997.0, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001058887243901404075079324435752726161972732621333944023339240754 2998.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005436206126505799552538751889828989125720456048353775897166452792 2999.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002790575929105113311243482528306938346063859340162384200539364853 3000.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001432331466924773106999560775247991727510226470312026404633833917 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/primes/SmallPrimes.java100644 1750 1750 21102 12126627715 27133 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.primes; import java.math.BigInteger; import java.util.ArrayList; import java.util.List; import org.apache.commons.math3.util.FastMath; /** * Utility methods to work on primes within the int range. * @version $Id: SmallPrimes.java 1462702 2013-03-30 04:45:52Z psteitz $ * @since 3.2 */ class SmallPrimes { /** * The first 512 prime numbers. *

                          * It contains all primes smaller or equal to the cubic square of Integer.MAX_VALUE. * As a result, int numbers which are not reduced by those primes are guaranteed * to be either prime or semi prime. */ public static final int[] PRIMES = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313, 317, 331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397, 401, 409, 419, 421, 431, 433, 439, 443, 449, 457, 461, 463, 467, 479, 487, 491, 499, 503, 509, 521, 523, 541, 547, 557, 563, 569, 571, 577, 587, 593, 599, 601, 607, 613, 617, 619, 631, 641, 643, 647, 653, 659, 661, 673, 677, 683, 691, 701, 709, 719, 727, 733, 739, 743, 751, 757, 761, 769, 773, 787, 797, 809, 811, 821, 823, 827, 829, 839, 853, 857, 859, 863, 877, 881, 883, 887, 907, 911, 919, 929, 937, 941, 947, 953, 967, 971, 977, 983, 991, 997, 1009, 1013, 1019, 1021, 1031, 1033, 1039, 1049, 1051, 1061, 1063, 1069, 1087, 1091, 1093, 1097, 1103, 1109, 1117, 1123, 1129, 1151, 1153, 1163, 1171, 1181, 1187, 1193, 1201, 1213, 1217, 1223, 1229, 1231, 1237, 1249, 1259, 1277, 1279, 1283, 1289, 1291, 1297, 1301, 1303, 1307, 1319, 1321, 1327, 1361, 1367, 1373, 1381, 1399, 1409, 1423, 1427, 1429, 1433, 1439, 1447, 1451, 1453, 1459, 1471, 1481, 1483, 1487, 1489, 1493, 1499, 1511, 1523, 1531, 1543, 1549, 1553, 1559, 1567, 1571, 1579, 1583, 1597, 1601, 1607, 1609, 1613, 1619, 1621, 1627, 1637, 1657, 1663, 1667, 1669, 1693, 1697, 1699, 1709, 1721, 1723, 1733, 1741, 1747, 1753, 1759, 1777, 1783, 1787, 1789, 1801, 1811, 1823, 1831, 1847, 1861, 1867, 1871, 1873, 1877, 1879, 1889, 1901, 1907, 1913, 1931, 1933, 1949, 1951, 1973, 1979, 1987, 1993, 1997, 1999, 2003, 2011, 2017, 2027, 2029, 2039, 2053, 2063, 2069, 2081, 2083, 2087, 2089, 2099, 2111, 2113, 2129, 2131, 2137, 2141, 2143, 2153, 2161, 2179, 2203, 2207, 2213, 2221, 2237, 2239, 2243, 2251, 2267, 2269, 2273, 2281, 2287, 2293, 2297, 2309, 2311, 2333, 2339, 2341, 2347, 2351, 2357, 2371, 2377, 2381, 2383, 2389, 2393, 2399, 2411, 2417, 2423, 2437, 2441, 2447, 2459, 2467, 2473, 2477, 2503, 2521, 2531, 2539, 2543, 2549, 2551, 2557, 2579, 2591, 2593, 2609, 2617, 2621, 2633, 2647, 2657, 2659, 2663, 2671, 2677, 2683, 2687, 2689, 2693, 2699, 2707, 2711, 2713, 2719, 2729, 2731, 2741, 2749, 2753, 2767, 2777, 2789, 2791, 2797, 2801, 2803, 2819, 2833, 2837, 2843, 2851, 2857, 2861, 2879, 2887, 2897, 2903, 2909, 2917, 2927, 2939, 2953, 2957, 2963, 2969, 2971, 2999, 3001, 3011, 3019, 3023, 3037, 3041, 3049, 3061, 3067, 3079, 3083, 3089, 3109, 3119, 3121, 3137, 3163, 3167, 3169, 3181, 3187, 3191, 3203, 3209, 3217, 3221, 3229, 3251, 3253, 3257, 3259, 3271, 3299, 3301, 3307, 3313, 3319, 3323, 3329, 3331, 3343, 3347, 3359, 3361, 3371, 3373, 3389, 3391, 3407, 3413, 3433, 3449, 3457, 3461, 3463, 3467, 3469, 3491, 3499, 3511, 3517, 3527, 3529, 3533, 3539, 3541, 3547, 3557, 3559, 3571, 3581, 3583, 3593, 3607, 3613, 3617, 3623, 3631, 3637, 3643, 3659, 3671}; /** The last number in PRIMES. */ public static final int PRIMES_LAST = PRIMES[PRIMES.length - 1]; /** * Hide utility class. */ private SmallPrimes() { } /** * Extract small factors. * @param n the number to factor, must be > 0. * @param factors the list where to add the factors. * @return the part of n which remains to be factored, it is either a prime or a semi-prime */ public static int smallTrialDivision(int n, final List factors) { for (int p : PRIMES) { while (0 == n % p) { n = n / p; factors.add(p); } } return n; } /** * Extract factors in the range PRIME_LAST+2 to maxFactors. * @param n the number to factorize, must be >= PRIME_LAST+2 and must not contain any factor below PRIME_LAST+2 * @param maxFactor the upper bound of trial division: if it is reached, the method gives up and returns n. * @param factors the list where to add the factors. * @return n or 1 if factorization is completed. */ public static int boundedTrialDivision(int n, int maxFactor, List factors) { int f = PRIMES_LAST + 2; // no check is done about n >= f while (f <= maxFactor) { if (0 == n % f) { n = n / f; factors.add(f); break; } f += 4; if (0 == n % f) { n = n / f; factors.add(f); break; } f += 2; } if (n != 1) { factors.add(n); } return n; } /** * Factorization by trial division. * @param n the number to factor * @return the list of prime factors of n */ public static List trialDivision(int n){ final List factors = new ArrayList(32); n = smallTrialDivision(n, factors); if (1 == n) { return factors; } // here we are sure that n is either a prime or a semi prime final int bound = (int) FastMath.sqrt(n); boundedTrialDivision(n, bound, factors); return factors; } /** * Miller-Rabin probabilistic primality test for int type, used in such a way that a result is always guaranteed. *

                          * It uses the prime numbers as successive base therefore it is guaranteed to be always correct. * (see Handbook of applied cryptography by Menezes, table 4.1) * * @param n number to test: an odd integer ≥ 3 * @return true if n is prime. false if n is definitely composite. */ public static boolean millerRabinPrimeTest(final int n) { final int nMinus1 = n - 1; final int s = Integer.numberOfTrailingZeros(nMinus1); final int r = nMinus1 >> s; //r must be odd, it is not checked here int t = 1; if (n >= 2047) { t = 2; } if (n >= 1373653) { t = 3; } if (n >= 25326001) { t = 4; } // works up to 3.2 billion, int range stops at 2.7 so we are safe :-) BigInteger br = BigInteger.valueOf(r); BigInteger bn = BigInteger.valueOf(n); for (int i = 0; i < t; i++) { BigInteger a = BigInteger.valueOf(SmallPrimes.PRIMES[i]); BigInteger bPow = a.modPow(br, bn); int y = bPow.intValue(); if ((1 != y) && (y != nMinus1)) { int j = 1; while ((j <= s - 1) && (nMinus1 != y)) { long square = ((long) y) * y; y = (int) (square % n); if (1 == y) { return false; } // definitely composite j++; } if (nMinus1 != y) { return false; } // definitely composite } } return true; // definitely prime } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/primes/package-info.java100644 1750 1750 1643 12126627715 27217 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ /** * Methods related to prime numbers like primality test, factor decomposition. */ package org.apache.commons.math3.primes; commons-math3-3.2-src/src/main/java/org/apache/commons/math3/primes/Primes.java100644 1750 1750 7560 12126627715 26136 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.primes; import org.apache.commons.math3.exception.MathIllegalArgumentException; import org.apache.commons.math3.exception.util.LocalizedFormats; import java.util.List; /** * Methods related to prime numbers in the range of int: *

                            *
                          • primality test
                          • *
                          • prime number generation
                          • *
                          • factorization
                          • *
                          * * @version $Id: Primes.java 1462702 2013-03-30 04:45:52Z psteitz $ * @since 3.2 */ public class Primes { /** * Hide utility class. */ private Primes() { } /** * Primality test: tells if the argument is a (provable) prime or not. *

                          * It uses the Miller-Rabin probabilistic test in such a way that a result is guaranteed: * it uses the firsts prime numbers as successive base (see Handbook of applied cryptography * by Menezes, table 4.1). * * @param n number to test. * @return true if n is prime. (All numbers < 2 return false). */ public static boolean isPrime(int n) { if (n < 2) { return false; } for (int p : SmallPrimes.PRIMES) { if (0 == (n % p)) { return n == p; } } return SmallPrimes.millerRabinPrimeTest(n); } /** * Return the smallest prime greater than or equal to n. * * @param n a positive number. * @return the smallest prime greater than or equal to n. * @throws MathIllegalArgumentException if n < 0. */ public static int nextPrime(int n) { if (n < 0) { throw new MathIllegalArgumentException(LocalizedFormats.NUMBER_TOO_SMALL, n, 0); } if (n == 2) { return 2; } n = n | 1;//make sure n is odd if (n == 1) { return 2; } if (isPrime(n)) { return n; } // prepare entry in the +2, +4 loop: // n should not be a multiple of 3 final int rem = n % 3; if (0 == rem) { // if n % 3 == 0 n += 2; // n % 3 == 2 } else if (1 == rem) { // if n % 3 == 1 // if (isPrime(n)) return n; n += 4; // n % 3 == 2 } while (true) { // this loop skips all multiple of 3 if (isPrime(n)) { return n; } n += 2; // n % 3 == 1 if (isPrime(n)) { return n; } n += 4; // n % 3 == 2 } } /** * Prime factors decomposition * * @param n number to factorize: must be ≥ 2 * @return list of prime factors of n * @throws MathIllegalArgumentException if n < 2. */ public static List primeFactors(int n) { if (n < 2) { throw new MathIllegalArgumentException(LocalizedFormats.NUMBER_TOO_SMALL, n, 2); } // slower than trial div unless we do an awful lot of computation // (then it finally gets JIT-compiled efficiently // List out = PollardRho.primeFactors(n); return SmallPrimes.trialDivision(n); } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/primes/PollardRho.java100644 1750 1750 13147 12126627715 26763 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.primes; import java.util.ArrayList; import java.util.List; import org.apache.commons.math3.util.FastMath; /** * Implementation of the Pollard's rho factorization algorithm. * @version $Id: PollardRho.java 1462702 2013-03-30 04:45:52Z psteitz $ * @since 3.2 */ class PollardRho { /** * Hide utility class. */ private PollardRho() { } /** * Factorization using Pollard's rho algorithm. * @param n number to factors, must be > 0 * @return the list of prime factors of n. */ public static List primeFactors(int n) { final List factors = new ArrayList(); n = SmallPrimes.smallTrialDivision(n, factors); if (1 == n) { return factors; } if (SmallPrimes.millerRabinPrimeTest(n)) { factors.add(n); return factors; } int divisor = rhoBrent(n); factors.add(divisor); factors.add(n / divisor); return factors; } /** * Implementation of the Pollard's rho factorization algorithm. *

                          * This implementation follows the paper "An improved Monte Carlo factorization algorithm" * by Richard P. Brent. This avoids the triple computation of f(x) typically found in Pollard's * rho implementations. It also batches several gcd computation into 1. *

                          * The backtracking is not implemented as we deal only with semi-primes. * * @param n number to factor, must be semi-prime. * @return a prime factor of n. */ static int rhoBrent(final int n) { final int x0 = 2; final int m = 25; int cst = SmallPrimes.PRIMES_LAST; int y = x0; int r = 1; do { int x = y; for (int i = 0; i < r; i++) { final long y2 = ((long) y) * y; y = (int) ((y2 + cst) % n); } int k = 0; do { final int bound = FastMath.min(m, r - k); int q = 1; for (int i = -3; i < bound; i++) { //start at -3 to ensure we enter this loop at least 3 times final long y2 = ((long) y) * y; y = (int) ((y2 + cst) % n); final long divisor = FastMath.abs(x - y); if (0 == divisor) { cst += SmallPrimes.PRIMES_LAST; k = -m; y = x0; r = 1; break; } final long prod = divisor * q; q = (int) (prod % n); if (0 == q) { return gcdPositive(FastMath.abs((int) divisor), n); } } final int out = gcdPositive(FastMath.abs(q), n); if (1 != out) { return out; } k = k + m; } while (k < r); r = 2 * r; } while (true); } /** * Gcd between two positive numbers. *

                          * Gets the greatest common divisor of two numbers, using the "binary gcd" method, * which avoids division and modulo operations. See Knuth 4.5.2 algorithm B. * This algorithm is due to Josef Stein (1961). *

                          * Special cases: *
                            *
                          • The result of {@code gcd(x, x)}, {@code gcd(0, x)} and {@code gcd(x, 0)} is the value of {@code x}.
                          • *
                          • The invocation {@code gcd(0, 0)} is the only one which returns {@code 0}.
                          • *
                          * * @param a first number, must be ≥ 0 * @param b second number, must be ≥ 0 * @return gcd(a,b) */ static int gcdPositive(int a, int b){ // both a and b must be positive, it is not checked here // gdc(a,0) = a if (a == 0) { return b; } else if (b == 0) { return a; } // make a and b odd, keep in mind the common power of twos final int aTwos = Integer.numberOfTrailingZeros(a); a >>= aTwos; final int bTwos = Integer.numberOfTrailingZeros(b); b >>= bTwos; final int shift = FastMath.min(aTwos, bTwos); // a and b >0 // if a > b then gdc(a,b) = gcd(a-b,b) // if a < b then gcd(a,b) = gcd(b-a,a) // so next a is the absolute difference and next b is the minimum of current values while (a != b) { final int delta = a - b; b = FastMath.min(a, b); a = FastMath.abs(delta); // for speed optimization: // remove any power of two in a as b is guaranteed to be odd throughout all iterations a >>= Integer.numberOfTrailingZeros(a); } // gcd(a,a) = a, just "add" the common power of twos return a << shift; } } ././@LongLink100644 0 0 150 12126630646 10255 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/optimization/SimpleVectorValueChecker.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/optimization/SimpleVectorValueChecker.j100644 1750 1750 13774 12126627713 32356 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.optimization; import org.apache.commons.math3.util.FastMath; import org.apache.commons.math3.exception.NotStrictlyPositiveException; /** * Simple implementation of the {@link ConvergenceChecker} interface using * only objective function values. * * Convergence is considered to have been reached if either the relative * difference between the objective function values is smaller than a * threshold or if either the absolute difference between the objective * function values is smaller than another threshold for all vectors elements. *
                          * The {@link #converged(int,PointVectorValuePair,PointVectorValuePair) converged} * method will also return {@code true} if the number of iterations has been set * (see {@link #SimpleVectorValueChecker(double,double,int) this constructor}). * * @version $Id: SimpleVectorValueChecker.java 1462503 2013-03-29 15:48:27Z luc $ * @deprecated As of 3.1 (to be removed in 4.0). * @since 3.0 */ @Deprecated public class SimpleVectorValueChecker extends AbstractConvergenceChecker { /** * If {@link #maxIterationCount} is set to this value, the number of * iterations will never cause * {@link #converged(int,PointVectorValuePair,PointVectorValuePair)} * to return {@code true}. */ private static final int ITERATION_CHECK_DISABLED = -1; /** * Number of iterations after which the * {@link #converged(int,PointVectorValuePair,PointVectorValuePair)} method * will return true (unless the check is disabled). */ private final int maxIterationCount; /** * Build an instance with default thresholds. * @deprecated See {@link AbstractConvergenceChecker#AbstractConvergenceChecker()} */ @Deprecated public SimpleVectorValueChecker() { maxIterationCount = ITERATION_CHECK_DISABLED; } /** * Build an instance with specified thresholds. * * In order to perform only relative checks, the absolute tolerance * must be set to a negative value. In order to perform only absolute * checks, the relative tolerance must be set to a negative value. * * @param relativeThreshold relative tolerance threshold * @param absoluteThreshold absolute tolerance threshold */ public SimpleVectorValueChecker(final double relativeThreshold, final double absoluteThreshold) { super(relativeThreshold, absoluteThreshold); maxIterationCount = ITERATION_CHECK_DISABLED; } /** * Builds an instance with specified tolerance thresholds and * iteration count. * * In order to perform only relative checks, the absolute tolerance * must be set to a negative value. In order to perform only absolute * checks, the relative tolerance must be set to a negative value. * * @param relativeThreshold Relative tolerance threshold. * @param absoluteThreshold Absolute tolerance threshold. * @param maxIter Maximum iteration count. * @throws NotStrictlyPositiveException if {@code maxIter <= 0}. * * @since 3.1 */ public SimpleVectorValueChecker(final double relativeThreshold, final double absoluteThreshold, final int maxIter) { super(relativeThreshold, absoluteThreshold); if (maxIter <= 0) { throw new NotStrictlyPositiveException(maxIter); } maxIterationCount = maxIter; } /** * Check if the optimization algorithm has converged considering the * last two points. * This method may be called several times from the same algorithm * iteration with different points. This can be detected by checking the * iteration number at each call if needed. Each time this method is * called, the previous and current point correspond to points with the * same role at each iteration, so they can be compared. As an example, * simplex-based algorithms call this method for all points of the simplex, * not only for the best or worst ones. * * @param iteration Index of current iteration * @param previous Best point in the previous iteration. * @param current Best point in the current iteration. * @return {@code true} if the arguments satify the convergence criterion. */ @Override public boolean converged(final int iteration, final PointVectorValuePair previous, final PointVectorValuePair current) { if (maxIterationCount != ITERATION_CHECK_DISABLED && iteration >= maxIterationCount) { return true; } final double[] p = previous.getValueRef(); final double[] c = current.getValueRef(); for (int i = 0; i < p.length; ++i) { final double pi = p[i]; final double ci = c[i]; final double difference = FastMath.abs(pi - ci); final double size = FastMath.max(FastMath.abs(pi), FastMath.abs(ci)); if (difference > size * getRelativeThreshold() && difference > getAbsoluteThreshold()) { return false; } } return true; } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/optimization/package-info.java100644 1750 1750 10203 12126627713 30454 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ /** *

                          All classes and sub-packages of this package are deprecated.

                          *

                          Please use their replacements, to be found under *
                            *
                          • {@link org.apache.commons.math3.optim}
                          • *
                          • {@link org.apache.commons.math3.fitting}
                          • *
                          *

                          * *

                          * This package provides common interfaces for the optimization algorithms * provided in sub-packages. The main interfaces defines optimizers and convergence * checkers. The functions that are optimized by the algorithms provided by this * package and its sub-packages are a subset of the one defined in the analysis * package, namely the real and vector valued functions. These functions are called * objective function here. When the goal is to minimize, the functions are often called * cost function, this name is not used in this package. *

                          * *

                          * Optimizers are the algorithms that will either minimize or maximize, the objective function * by changing its input variables set until an optimal set is found. There are only four * interfaces defining the common behavior of optimizers, one for each supported type of objective * function: *

                            *
                          • {@link org.apache.commons.math3.optimization.univariate.UnivariateOptimizer * UnivariateOptimizer} for {@link org.apache.commons.math3.analysis.UnivariateFunction * univariate real functions}
                          • *
                          • {@link org.apache.commons.math3.optimization.MultivariateOptimizer * MultivariateOptimizer} for {@link org.apache.commons.math3.analysis.MultivariateFunction * multivariate real functions}
                          • *
                          • {@link org.apache.commons.math3.optimization.MultivariateDifferentiableOptimizer * MultivariateDifferentiableOptimizer} for {@link * org.apache.commons.math3.analysis.differentiation.MultivariateDifferentiableFunction * multivariate differentiable real functions}
                          • *
                          • {@link org.apache.commons.math3.optimization.MultivariateDifferentiableVectorOptimizer * MultivariateDifferentiableVectorOptimizer} for {@link * org.apache.commons.math3.analysis.differentiation.MultivariateDifferentiableVectorFunction * multivariate differentiable vectorial functions}
                          • *
                          *

                          * *

                          * Despite there are only four types of supported optimizers, it is possible to optimize a * transform a {@link org.apache.commons.math3.analysis.MultivariateVectorFunction * non-differentiable multivariate vectorial function} by converting it to a {@link * org.apache.commons.math3.analysis.MultivariateFunction non-differentiable multivariate * real function} thanks to the {@link * org.apache.commons.math3.optimization.LeastSquaresConverter LeastSquaresConverter} helper class. * The transformed function can be optimized using any implementation of the {@link * org.apache.commons.math3.optimization.MultivariateOptimizer MultivariateOptimizer} interface. *

                          * *

                          * For each of the four types of supported optimizers, there is a special implementation which * wraps a classical optimizer in order to add it a multi-start feature. This feature call the * underlying optimizer several times in sequence with different starting points and returns * the best optimum found or all optima if desired. This is a classical way to prevent being * trapped into a local extremum when looking for a global one. *

                          * */ package org.apache.commons.math3.optimization; ././@LongLink100644 0 0 157 12126630646 10264 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/optimization/MultivariateMultiStartOptimizer.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/optimization/MultivariateMultiStartOpti100644 1750 1750 4366 12126627713 32540 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.optimization; import org.apache.commons.math3.analysis.MultivariateFunction; import org.apache.commons.math3.random.RandomVectorGenerator; /** * Special implementation of the {@link MultivariateOptimizer} interface adding * multi-start features to an existing optimizer. * * This class wraps a classical optimizer to use it several times in * turn with different starting points in order to avoid being trapped * into a local extremum when looking for a global one. * * @version $Id: MultivariateMultiStartOptimizer.java 1422230 2012-12-15 12:11:13Z erans $ * @deprecated As of 3.1 (to be removed in 4.0). * @since 2.0 */ @Deprecated public class MultivariateMultiStartOptimizer extends BaseMultivariateMultiStartOptimizer implements MultivariateOptimizer { /** * Create a multi-start optimizer from a single-start optimizer. * * @param optimizer Single-start optimizer to wrap. * @param starts Number of starts to perform (including the * first one), multi-start is disabled if value is less than or * equal to 1. * @param generator Random vector generator to use for restarts. */ public MultivariateMultiStartOptimizer(final MultivariateOptimizer optimizer, final int starts, final RandomVectorGenerator generator) { super(optimizer, starts, generator); } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/optimization/direct/package-info.java100644 1750 1750 1707 12126627712 31716 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ /** * *

                          * This package provides optimization algorithms that don't require derivatives. *

                          * */ package org.apache.commons.math3.optimization.direct; ././@LongLink100644 0 0 156 12126630646 10263 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/optimization/direct/MultiDirectionalSimplex.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/optimization/direct/MultiDirectionalSim100644 1750 1750 20572 12126627712 32414 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.optimization.direct; import java.util.Comparator; import org.apache.commons.math3.analysis.MultivariateFunction; import org.apache.commons.math3.optimization.PointValuePair; /** * This class implements the multi-directional direct search method. * * @version $Id: MultiDirectionalSimplex.java 1422230 2012-12-15 12:11:13Z erans $ * @deprecated As of 3.1 (to be removed in 4.0). * @since 3.0 */ @Deprecated public class MultiDirectionalSimplex extends AbstractSimplex { /** Default value for {@link #khi}: {@value}. */ private static final double DEFAULT_KHI = 2; /** Default value for {@link #gamma}: {@value}. */ private static final double DEFAULT_GAMMA = 0.5; /** Expansion coefficient. */ private final double khi; /** Contraction coefficient. */ private final double gamma; /** * Build a multi-directional simplex with default coefficients. * The default values are 2.0 for khi and 0.5 for gamma. * * @param n Dimension of the simplex. */ public MultiDirectionalSimplex(final int n) { this(n, 1d); } /** * Build a multi-directional simplex with default coefficients. * The default values are 2.0 for khi and 0.5 for gamma. * * @param n Dimension of the simplex. * @param sideLength Length of the sides of the default (hypercube) * simplex. See {@link AbstractSimplex#AbstractSimplex(int,double)}. */ public MultiDirectionalSimplex(final int n, double sideLength) { this(n, sideLength, DEFAULT_KHI, DEFAULT_GAMMA); } /** * Build a multi-directional simplex with specified coefficients. * * @param n Dimension of the simplex. See * {@link AbstractSimplex#AbstractSimplex(int,double)}. * @param khi Expansion coefficient. * @param gamma Contraction coefficient. */ public MultiDirectionalSimplex(final int n, final double khi, final double gamma) { this(n, 1d, khi, gamma); } /** * Build a multi-directional simplex with specified coefficients. * * @param n Dimension of the simplex. See * {@link AbstractSimplex#AbstractSimplex(int,double)}. * @param sideLength Length of the sides of the default (hypercube) * simplex. See {@link AbstractSimplex#AbstractSimplex(int,double)}. * @param khi Expansion coefficient. * @param gamma Contraction coefficient. */ public MultiDirectionalSimplex(final int n, double sideLength, final double khi, final double gamma) { super(n, sideLength); this.khi = khi; this.gamma = gamma; } /** * Build a multi-directional simplex with default coefficients. * The default values are 2.0 for khi and 0.5 for gamma. * * @param steps Steps along the canonical axes representing box edges. * They may be negative but not zero. See */ public MultiDirectionalSimplex(final double[] steps) { this(steps, DEFAULT_KHI, DEFAULT_GAMMA); } /** * Build a multi-directional simplex with specified coefficients. * * @param steps Steps along the canonical axes representing box edges. * They may be negative but not zero. See * {@link AbstractSimplex#AbstractSimplex(double[])}. * @param khi Expansion coefficient. * @param gamma Contraction coefficient. */ public MultiDirectionalSimplex(final double[] steps, final double khi, final double gamma) { super(steps); this.khi = khi; this.gamma = gamma; } /** * Build a multi-directional simplex with default coefficients. * The default values are 2.0 for khi and 0.5 for gamma. * * @param referenceSimplex Reference simplex. See * {@link AbstractSimplex#AbstractSimplex(double[][])}. */ public MultiDirectionalSimplex(final double[][] referenceSimplex) { this(referenceSimplex, DEFAULT_KHI, DEFAULT_GAMMA); } /** * Build a multi-directional simplex with specified coefficients. * * @param referenceSimplex Reference simplex. See * {@link AbstractSimplex#AbstractSimplex(double[][])}. * @param khi Expansion coefficient. * @param gamma Contraction coefficient. * @throws org.apache.commons.math3.exception.NotStrictlyPositiveException * if the reference simplex does not contain at least one point. * @throws org.apache.commons.math3.exception.DimensionMismatchException * if there is a dimension mismatch in the reference simplex. */ public MultiDirectionalSimplex(final double[][] referenceSimplex, final double khi, final double gamma) { super(referenceSimplex); this.khi = khi; this.gamma = gamma; } /** {@inheritDoc} */ @Override public void iterate(final MultivariateFunction evaluationFunction, final Comparator comparator) { // Save the original simplex. final PointValuePair[] original = getPoints(); final PointValuePair best = original[0]; // Perform a reflection step. final PointValuePair reflected = evaluateNewSimplex(evaluationFunction, original, 1, comparator); if (comparator.compare(reflected, best) < 0) { // Compute the expanded simplex. final PointValuePair[] reflectedSimplex = getPoints(); final PointValuePair expanded = evaluateNewSimplex(evaluationFunction, original, khi, comparator); if (comparator.compare(reflected, expanded) <= 0) { // Keep the reflected simplex. setPoints(reflectedSimplex); } // Keep the expanded simplex. return; } // Compute the contracted simplex. evaluateNewSimplex(evaluationFunction, original, gamma, comparator); } /** * Compute and evaluate a new simplex. * * @param evaluationFunction Evaluation function. * @param original Original simplex (to be preserved). * @param coeff Linear coefficient. * @param comparator Comparator to use to sort simplex vertices from best * to poorest. * @return the best point in the transformed simplex. * @throws org.apache.commons.math3.exception.TooManyEvaluationsException * if the maximal number of evaluations is exceeded. */ private PointValuePair evaluateNewSimplex(final MultivariateFunction evaluationFunction, final PointValuePair[] original, final double coeff, final Comparator comparator) { final double[] xSmallest = original[0].getPointRef(); // Perform a linear transformation on all the simplex points, // except the first one. setPoint(0, original[0]); final int dim = getDimension(); for (int i = 1; i < getSize(); i++) { final double[] xOriginal = original[i].getPointRef(); final double[] xTransformed = new double[dim]; for (int j = 0; j < dim; j++) { xTransformed[j] = xSmallest[j] + coeff * (xSmallest[j] - xOriginal[j]); } setPoint(i, new PointValuePair(xTransformed, Double.NaN, false)); } // Evaluate the simplex. evaluate(evaluationFunction, comparator); return getPoint(0); } } ././@LongLink100644 0 0 146 12126630646 10262 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/optimization/direct/BOBYQAOptimizer.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/optimization/direct/BOBYQAOptimizer.jav100644 1750 1750 335505 12126627712 32157 0ustarlucluc 0 0 // CHECKSTYLE: stop all /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.optimization.direct; import org.apache.commons.math3.analysis.MultivariateFunction; import org.apache.commons.math3.exception.MathIllegalStateException; import org.apache.commons.math3.exception.NumberIsTooSmallException; import org.apache.commons.math3.exception.OutOfRangeException; import org.apache.commons.math3.exception.util.LocalizedFormats; import org.apache.commons.math3.linear.Array2DRowRealMatrix; import org.apache.commons.math3.linear.ArrayRealVector; import org.apache.commons.math3.linear.RealVector; import org.apache.commons.math3.optimization.GoalType; import org.apache.commons.math3.optimization.PointValuePair; import org.apache.commons.math3.optimization.MultivariateOptimizer; /** * Powell's BOBYQA algorithm. This implementation is translated and * adapted from the Fortran version available * here. * See * this paper for an introduction. *
                          * BOBYQA is particularly well suited for high dimensional problems * where derivatives are not available. In most cases it outperforms the * {@link PowellOptimizer} significantly. Stochastic algorithms like * {@link CMAESOptimizer} succeed more often than BOBYQA, but are more * expensive. BOBYQA could also be considered as a replacement of any * derivative-based optimizer when the derivatives are approximated by * finite differences. * * @version $Id: BOBYQAOptimizer.java 1462507 2013-03-29 15:50:22Z luc $ * @deprecated As of 3.1 (to be removed in 4.0). * @since 3.0 */ @Deprecated public class BOBYQAOptimizer extends BaseAbstractMultivariateSimpleBoundsOptimizer implements MultivariateOptimizer { /** Minimum dimension of the problem: {@value} */ public static final int MINIMUM_PROBLEM_DIMENSION = 2; /** Default value for {@link #initialTrustRegionRadius}: {@value} . */ public static final double DEFAULT_INITIAL_RADIUS = 10.0; /** Default value for {@link #stoppingTrustRegionRadius}: {@value} . */ public static final double DEFAULT_STOPPING_RADIUS = 1E-8; private static final double ZERO = 0d; private static final double ONE = 1d; private static final double TWO = 2d; private static final double TEN = 10d; private static final double SIXTEEN = 16d; private static final double TWO_HUNDRED_FIFTY = 250d; private static final double MINUS_ONE = -ONE; private static final double HALF = ONE / 2; private static final double ONE_OVER_FOUR = ONE / 4; private static final double ONE_OVER_EIGHT = ONE / 8; private static final double ONE_OVER_TEN = ONE / 10; private static final double ONE_OVER_A_THOUSAND = ONE / 1000; /** * numberOfInterpolationPoints XXX */ private final int numberOfInterpolationPoints; /** * initialTrustRegionRadius XXX */ private double initialTrustRegionRadius; /** * stoppingTrustRegionRadius XXX */ private final double stoppingTrustRegionRadius; /** Goal type (minimize or maximize). */ private boolean isMinimize; /** * Current best values for the variables to be optimized. * The vector will be changed in-place to contain the values of the least * calculated objective function values. */ private ArrayRealVector currentBest; /** Differences between the upper and lower bounds. */ private double[] boundDifference; /** * Index of the interpolation point at the trust region center. */ private int trustRegionCenterInterpolationPointIndex; /** * Last n columns of matrix H (where n is the dimension * of the problem). * XXX "bmat" in the original code. */ private Array2DRowRealMatrix bMatrix; /** * Factorization of the leading npt square submatrix of H, this * factorization being Z ZT, which provides both the correct * rank and positive semi-definiteness. * XXX "zmat" in the original code. */ private Array2DRowRealMatrix zMatrix; /** * Coordinates of the interpolation points relative to {@link #originShift}. * XXX "xpt" in the original code. */ private Array2DRowRealMatrix interpolationPoints; /** * Shift of origin that should reduce the contributions from rounding * errors to values of the model and Lagrange functions. * XXX "xbase" in the original code. */ private ArrayRealVector originShift; /** * Values of the objective function at the interpolation points. * XXX "fval" in the original code. */ private ArrayRealVector fAtInterpolationPoints; /** * Displacement from {@link #originShift} of the trust region center. * XXX "xopt" in the original code. */ private ArrayRealVector trustRegionCenterOffset; /** * Gradient of the quadratic model at {@link #originShift} + * {@link #trustRegionCenterOffset}. * XXX "gopt" in the original code. */ private ArrayRealVector gradientAtTrustRegionCenter; /** * Differences {@link #getLowerBound()} - {@link #originShift}. * All the components of every {@link #trustRegionCenterOffset} are going * to satisfy the bounds
                          * {@link #getLowerBound() lowerBound}i ≤ * {@link #trustRegionCenterOffset}i,
                          * with appropriate equalities when {@link #trustRegionCenterOffset} is * on a constraint boundary. * XXX "sl" in the original code. */ private ArrayRealVector lowerDifference; /** * Differences {@link #getUpperBound()} - {@link #originShift} * All the components of every {@link #trustRegionCenterOffset} are going * to satisfy the bounds
                          * {@link #trustRegionCenterOffset}i ≤ * {@link #getUpperBound() upperBound}i,
                          * with appropriate equalities when {@link #trustRegionCenterOffset} is * on a constraint boundary. * XXX "su" in the original code. */ private ArrayRealVector upperDifference; /** * Parameters of the implicit second derivatives of the quadratic model. * XXX "pq" in the original code. */ private ArrayRealVector modelSecondDerivativesParameters; /** * Point chosen by function {@link #trsbox(double,ArrayRealVector, * ArrayRealVector, ArrayRealVector,ArrayRealVector,ArrayRealVector) trsbox} * or {@link #altmov(int,double) altmov}. * Usually {@link #originShift} + {@link #newPoint} is the vector of * variables for the next evaluation of the objective function. * It also satisfies the constraints indicated in {@link #lowerDifference} * and {@link #upperDifference}. * XXX "xnew" in the original code. */ private ArrayRealVector newPoint; /** * Alternative to {@link #newPoint}, chosen by * {@link #altmov(int,double) altmov}. * It may replace {@link #newPoint} in order to increase the denominator * in the {@link #update(double, double, int) updating procedure}. * XXX "xalt" in the original code. */ private ArrayRealVector alternativeNewPoint; /** * Trial step from {@link #trustRegionCenterOffset} which is usually * {@link #newPoint} - {@link #trustRegionCenterOffset}. * XXX "d__" in the original code. */ private ArrayRealVector trialStepPoint; /** * Values of the Lagrange functions at a new point. * XXX "vlag" in the original code. */ private ArrayRealVector lagrangeValuesAtNewPoint; /** * Explicit second derivatives of the quadratic model. * XXX "hq" in the original code. */ private ArrayRealVector modelSecondDerivativesValues; /** * @param numberOfInterpolationPoints Number of interpolation conditions. * For a problem of dimension {@code n}, its value must be in the interval * {@code [n+2, (n+1)(n+2)/2]}. * Choices that exceed {@code 2n+1} are not recommended. */ public BOBYQAOptimizer(int numberOfInterpolationPoints) { this(numberOfInterpolationPoints, DEFAULT_INITIAL_RADIUS, DEFAULT_STOPPING_RADIUS); } /** * @param numberOfInterpolationPoints Number of interpolation conditions. * For a problem of dimension {@code n}, its value must be in the interval * {@code [n+2, (n+1)(n+2)/2]}. * Choices that exceed {@code 2n+1} are not recommended. * @param initialTrustRegionRadius Initial trust region radius. * @param stoppingTrustRegionRadius Stopping trust region radius. */ public BOBYQAOptimizer(int numberOfInterpolationPoints, double initialTrustRegionRadius, double stoppingTrustRegionRadius) { super(null); // No custom convergence criterion. this.numberOfInterpolationPoints = numberOfInterpolationPoints; this.initialTrustRegionRadius = initialTrustRegionRadius; this.stoppingTrustRegionRadius = stoppingTrustRegionRadius; } /** {@inheritDoc} */ @Override protected PointValuePair doOptimize() { final double[] lowerBound = getLowerBound(); final double[] upperBound = getUpperBound(); // Validity checks. setup(lowerBound, upperBound); isMinimize = (getGoalType() == GoalType.MINIMIZE); currentBest = new ArrayRealVector(getStartPoint()); final double value = bobyqa(lowerBound, upperBound); return new PointValuePair(currentBest.getDataRef(), isMinimize ? value : -value); } /** * This subroutine seeks the least value of a function of many variables, * by applying a trust region method that forms quadratic models by * interpolation. There is usually some freedom in the interpolation * conditions, which is taken up by minimizing the Frobenius norm of * the change to the second derivative of the model, beginning with the * zero matrix. The values of the variables are constrained by upper and * lower bounds. The arguments of the subroutine are as follows. * * N must be set to the number of variables and must be at least two. * NPT is the number of interpolation conditions. Its value must be in * the interval [N+2,(N+1)(N+2)/2]. Choices that exceed 2*N+1 are not * recommended. * Initial values of the variables must be set in X(1),X(2),...,X(N). They * will be changed to the values that give the least calculated F. * For I=1,2,...,N, XL(I) and XU(I) must provide the lower and upper * bounds, respectively, on X(I). The construction of quadratic models * requires XL(I) to be strictly less than XU(I) for each I. Further, * the contribution to a model from changes to the I-th variable is * damaged severely by rounding errors if XU(I)-XL(I) is too small. * RHOBEG and RHOEND must be set to the initial and final values of a trust * region radius, so both must be positive with RHOEND no greater than * RHOBEG. Typically, RHOBEG should be about one tenth of the greatest * expected change to a variable, while RHOEND should indicate the * accuracy that is required in the final values of the variables. An * error return occurs if any of the differences XU(I)-XL(I), I=1,...,N, * is less than 2*RHOBEG. * MAXFUN must be set to an upper bound on the number of calls of CALFUN. * The array W will be used for working space. Its length must be at least * (NPT+5)*(NPT+N)+3*N*(N+5)/2. * * @param lowerBound Lower bounds. * @param upperBound Upper bounds. * @return the value of the objective at the optimum. */ private double bobyqa(double[] lowerBound, double[] upperBound) { printMethod(); // XXX final int n = currentBest.getDimension(); // Return if there is insufficient space between the bounds. Modify the // initial X if necessary in order to avoid conflicts between the bounds // and the construction of the first quadratic model. The lower and upper // bounds on moves from the updated X are set now, in the ISL and ISU // partitions of W, in order to provide useful and exact information about // components of X that become within distance RHOBEG from their bounds. for (int j = 0; j < n; j++) { final double boundDiff = boundDifference[j]; lowerDifference.setEntry(j, lowerBound[j] - currentBest.getEntry(j)); upperDifference.setEntry(j, upperBound[j] - currentBest.getEntry(j)); if (lowerDifference.getEntry(j) >= -initialTrustRegionRadius) { if (lowerDifference.getEntry(j) >= ZERO) { currentBest.setEntry(j, lowerBound[j]); lowerDifference.setEntry(j, ZERO); upperDifference.setEntry(j, boundDiff); } else { currentBest.setEntry(j, lowerBound[j] + initialTrustRegionRadius); lowerDifference.setEntry(j, -initialTrustRegionRadius); // Computing MAX final double deltaOne = upperBound[j] - currentBest.getEntry(j); upperDifference.setEntry(j, Math.max(deltaOne, initialTrustRegionRadius)); } } else if (upperDifference.getEntry(j) <= initialTrustRegionRadius) { if (upperDifference.getEntry(j) <= ZERO) { currentBest.setEntry(j, upperBound[j]); lowerDifference.setEntry(j, -boundDiff); upperDifference.setEntry(j, ZERO); } else { currentBest.setEntry(j, upperBound[j] - initialTrustRegionRadius); // Computing MIN final double deltaOne = lowerBound[j] - currentBest.getEntry(j); final double deltaTwo = -initialTrustRegionRadius; lowerDifference.setEntry(j, Math.min(deltaOne, deltaTwo)); upperDifference.setEntry(j, initialTrustRegionRadius); } } } // Make the call of BOBYQB. return bobyqb(lowerBound, upperBound); } // bobyqa // ---------------------------------------------------------------------------------------- /** * The arguments N, NPT, X, XL, XU, RHOBEG, RHOEND, IPRINT and MAXFUN * are identical to the corresponding arguments in SUBROUTINE BOBYQA. * XBASE holds a shift of origin that should reduce the contributions * from rounding errors to values of the model and Lagrange functions. * XPT is a two-dimensional array that holds the coordinates of the * interpolation points relative to XBASE. * FVAL holds the values of F at the interpolation points. * XOPT is set to the displacement from XBASE of the trust region centre. * GOPT holds the gradient of the quadratic model at XBASE+XOPT. * HQ holds the explicit second derivatives of the quadratic model. * PQ contains the parameters of the implicit second derivatives of the * quadratic model. * BMAT holds the last N columns of H. * ZMAT holds the factorization of the leading NPT by NPT submatrix of H, * this factorization being ZMAT times ZMAT^T, which provides both the * correct rank and positive semi-definiteness. * NDIM is the first dimension of BMAT and has the value NPT+N. * SL and SU hold the differences XL-XBASE and XU-XBASE, respectively. * All the components of every XOPT are going to satisfy the bounds * SL(I) .LEQ. XOPT(I) .LEQ. SU(I), with appropriate equalities when * XOPT is on a constraint boundary. * XNEW is chosen by SUBROUTINE TRSBOX or ALTMOV. Usually XBASE+XNEW is the * vector of variables for the next call of CALFUN. XNEW also satisfies * the SL and SU constraints in the way that has just been mentioned. * XALT is an alternative to XNEW, chosen by ALTMOV, that may replace XNEW * in order to increase the denominator in the updating of UPDATE. * D is reserved for a trial step from XOPT, which is usually XNEW-XOPT. * VLAG contains the values of the Lagrange functions at a new point X. * They are part of a product that requires VLAG to be of length NDIM. * W is a one-dimensional array that is used for working space. Its length * must be at least 3*NDIM = 3*(NPT+N). * * @param lowerBound Lower bounds. * @param upperBound Upper bounds. * @return the value of the objective at the optimum. */ private double bobyqb(double[] lowerBound, double[] upperBound) { printMethod(); // XXX final int n = currentBest.getDimension(); final int npt = numberOfInterpolationPoints; final int np = n + 1; final int nptm = npt - np; final int nh = n * np / 2; final ArrayRealVector work1 = new ArrayRealVector(n); final ArrayRealVector work2 = new ArrayRealVector(npt); final ArrayRealVector work3 = new ArrayRealVector(npt); double cauchy = Double.NaN; double alpha = Double.NaN; double dsq = Double.NaN; double crvmin = Double.NaN; // Set some constants. // Parameter adjustments // Function Body // The call of PRELIM sets the elements of XBASE, XPT, FVAL, GOPT, HQ, PQ, // BMAT and ZMAT for the first iteration, with the corresponding values of // of NF and KOPT, which are the number of calls of CALFUN so far and the // index of the interpolation point at the trust region centre. Then the // initial XOPT is set too. The branch to label 720 occurs if MAXFUN is // less than NPT. GOPT will be updated if KOPT is different from KBASE. trustRegionCenterInterpolationPointIndex = 0; prelim(lowerBound, upperBound); double xoptsq = ZERO; for (int i = 0; i < n; i++) { trustRegionCenterOffset.setEntry(i, interpolationPoints.getEntry(trustRegionCenterInterpolationPointIndex, i)); // Computing 2nd power final double deltaOne = trustRegionCenterOffset.getEntry(i); xoptsq += deltaOne * deltaOne; } double fsave = fAtInterpolationPoints.getEntry(0); final int kbase = 0; // Complete the settings that are required for the iterative procedure. int ntrits = 0; int itest = 0; int knew = 0; int nfsav = getEvaluations(); double rho = initialTrustRegionRadius; double delta = rho; double diffa = ZERO; double diffb = ZERO; double diffc = ZERO; double f = ZERO; double beta = ZERO; double adelt = ZERO; double denom = ZERO; double ratio = ZERO; double dnorm = ZERO; double scaden = ZERO; double biglsq = ZERO; double distsq = ZERO; // Update GOPT if necessary before the first iteration and after each // call of RESCUE that makes a call of CALFUN. int state = 20; for(;;) switch (state) { case 20: { printState(20); // XXX if (trustRegionCenterInterpolationPointIndex != kbase) { int ih = 0; for (int j = 0; j < n; j++) { for (int i = 0; i <= j; i++) { if (i < j) { gradientAtTrustRegionCenter.setEntry(j, gradientAtTrustRegionCenter.getEntry(j) + modelSecondDerivativesValues.getEntry(ih) * trustRegionCenterOffset.getEntry(i)); } gradientAtTrustRegionCenter.setEntry(i, gradientAtTrustRegionCenter.getEntry(i) + modelSecondDerivativesValues.getEntry(ih) * trustRegionCenterOffset.getEntry(j)); ih++; } } if (getEvaluations() > npt) { for (int k = 0; k < npt; k++) { double temp = ZERO; for (int j = 0; j < n; j++) { temp += interpolationPoints.getEntry(k, j) * trustRegionCenterOffset.getEntry(j); } temp *= modelSecondDerivativesParameters.getEntry(k); for (int i = 0; i < n; i++) { gradientAtTrustRegionCenter.setEntry(i, gradientAtTrustRegionCenter.getEntry(i) + temp * interpolationPoints.getEntry(k, i)); } } // throw new PathIsExploredException(); // XXX } } // Generate the next point in the trust region that provides a small value // of the quadratic model subject to the constraints on the variables. // The int NTRITS is set to the number "trust region" iterations that // have occurred since the last "alternative" iteration. If the length // of XNEW-XOPT is less than HALF*RHO, however, then there is a branch to // label 650 or 680 with NTRITS=-1, instead of calculating F at XNEW. } case 60: { printState(60); // XXX final ArrayRealVector gnew = new ArrayRealVector(n); final ArrayRealVector xbdi = new ArrayRealVector(n); final ArrayRealVector s = new ArrayRealVector(n); final ArrayRealVector hs = new ArrayRealVector(n); final ArrayRealVector hred = new ArrayRealVector(n); final double[] dsqCrvmin = trsbox(delta, gnew, xbdi, s, hs, hred); dsq = dsqCrvmin[0]; crvmin = dsqCrvmin[1]; // Computing MIN double deltaOne = delta; double deltaTwo = Math.sqrt(dsq); dnorm = Math.min(deltaOne, deltaTwo); if (dnorm < HALF * rho) { ntrits = -1; // Computing 2nd power deltaOne = TEN * rho; distsq = deltaOne * deltaOne; if (getEvaluations() <= nfsav + 2) { state = 650; break; } // The following choice between labels 650 and 680 depends on whether or // not our work with the current RHO seems to be complete. Either RHO is // decreased or termination occurs if the errors in the quadratic model at // the last three interpolation points compare favourably with predictions // of likely improvements to the model within distance HALF*RHO of XOPT. // Computing MAX deltaOne = Math.max(diffa, diffb); final double errbig = Math.max(deltaOne, diffc); final double frhosq = rho * ONE_OVER_EIGHT * rho; if (crvmin > ZERO && errbig > frhosq * crvmin) { state = 650; break; } final double bdtol = errbig / rho; for (int j = 0; j < n; j++) { double bdtest = bdtol; if (newPoint.getEntry(j) == lowerDifference.getEntry(j)) { bdtest = work1.getEntry(j); } if (newPoint.getEntry(j) == upperDifference.getEntry(j)) { bdtest = -work1.getEntry(j); } if (bdtest < bdtol) { double curv = modelSecondDerivativesValues.getEntry((j + j * j) / 2); for (int k = 0; k < npt; k++) { // Computing 2nd power final double d1 = interpolationPoints.getEntry(k, j); curv += modelSecondDerivativesParameters.getEntry(k) * (d1 * d1); } bdtest += HALF * curv * rho; if (bdtest < bdtol) { state = 650; break; } // throw new PathIsExploredException(); // XXX } } state = 680; break; } ++ntrits; // Severe cancellation is likely to occur if XOPT is too far from XBASE. // If the following test holds, then XBASE is shifted so that XOPT becomes // zero. The appropriate changes are made to BMAT and to the second // derivatives of the current model, beginning with the changes to BMAT // that do not depend on ZMAT. VLAG is used temporarily for working space. } case 90: { printState(90); // XXX if (dsq <= xoptsq * ONE_OVER_A_THOUSAND) { final double fracsq = xoptsq * ONE_OVER_FOUR; double sumpq = ZERO; // final RealVector sumVector // = new ArrayRealVector(npt, -HALF * xoptsq).add(interpolationPoints.operate(trustRegionCenter)); for (int k = 0; k < npt; k++) { sumpq += modelSecondDerivativesParameters.getEntry(k); double sum = -HALF * xoptsq; for (int i = 0; i < n; i++) { sum += interpolationPoints.getEntry(k, i) * trustRegionCenterOffset.getEntry(i); } // sum = sumVector.getEntry(k); // XXX "testAckley" and "testDiffPow" fail. work2.setEntry(k, sum); final double temp = fracsq - HALF * sum; for (int i = 0; i < n; i++) { work1.setEntry(i, bMatrix.getEntry(k, i)); lagrangeValuesAtNewPoint.setEntry(i, sum * interpolationPoints.getEntry(k, i) + temp * trustRegionCenterOffset.getEntry(i)); final int ip = npt + i; for (int j = 0; j <= i; j++) { bMatrix.setEntry(ip, j, bMatrix.getEntry(ip, j) + work1.getEntry(i) * lagrangeValuesAtNewPoint.getEntry(j) + lagrangeValuesAtNewPoint.getEntry(i) * work1.getEntry(j)); } } } // Then the revisions of BMAT that depend on ZMAT are calculated. for (int m = 0; m < nptm; m++) { double sumz = ZERO; double sumw = ZERO; for (int k = 0; k < npt; k++) { sumz += zMatrix.getEntry(k, m); lagrangeValuesAtNewPoint.setEntry(k, work2.getEntry(k) * zMatrix.getEntry(k, m)); sumw += lagrangeValuesAtNewPoint.getEntry(k); } for (int j = 0; j < n; j++) { double sum = (fracsq * sumz - HALF * sumw) * trustRegionCenterOffset.getEntry(j); for (int k = 0; k < npt; k++) { sum += lagrangeValuesAtNewPoint.getEntry(k) * interpolationPoints.getEntry(k, j); } work1.setEntry(j, sum); for (int k = 0; k < npt; k++) { bMatrix.setEntry(k, j, bMatrix.getEntry(k, j) + sum * zMatrix.getEntry(k, m)); } } for (int i = 0; i < n; i++) { final int ip = i + npt; final double temp = work1.getEntry(i); for (int j = 0; j <= i; j++) { bMatrix.setEntry(ip, j, bMatrix.getEntry(ip, j) + temp * work1.getEntry(j)); } } } // The following instructions complete the shift, including the changes // to the second derivative parameters of the quadratic model. int ih = 0; for (int j = 0; j < n; j++) { work1.setEntry(j, -HALF * sumpq * trustRegionCenterOffset.getEntry(j)); for (int k = 0; k < npt; k++) { work1.setEntry(j, work1.getEntry(j) + modelSecondDerivativesParameters.getEntry(k) * interpolationPoints.getEntry(k, j)); interpolationPoints.setEntry(k, j, interpolationPoints.getEntry(k, j) - trustRegionCenterOffset.getEntry(j)); } for (int i = 0; i <= j; i++) { modelSecondDerivativesValues.setEntry(ih, modelSecondDerivativesValues.getEntry(ih) + work1.getEntry(i) * trustRegionCenterOffset.getEntry(j) + trustRegionCenterOffset.getEntry(i) * work1.getEntry(j)); bMatrix.setEntry(npt + i, j, bMatrix.getEntry(npt + j, i)); ih++; } } for (int i = 0; i < n; i++) { originShift.setEntry(i, originShift.getEntry(i) + trustRegionCenterOffset.getEntry(i)); newPoint.setEntry(i, newPoint.getEntry(i) - trustRegionCenterOffset.getEntry(i)); lowerDifference.setEntry(i, lowerDifference.getEntry(i) - trustRegionCenterOffset.getEntry(i)); upperDifference.setEntry(i, upperDifference.getEntry(i) - trustRegionCenterOffset.getEntry(i)); trustRegionCenterOffset.setEntry(i, ZERO); } xoptsq = ZERO; } if (ntrits == 0) { state = 210; break; } state = 230; break; // XBASE is also moved to XOPT by a call of RESCUE. This calculation is // more expensive than the previous shift, because new matrices BMAT and // ZMAT are generated from scratch, which may include the replacement of // interpolation points whose positions seem to be causing near linear // dependence in the interpolation conditions. Therefore RESCUE is called // only if rounding errors have reduced by at least a factor of two the // denominator of the formula for updating the H matrix. It provides a // useful safeguard, but is not invoked in most applications of BOBYQA. } case 210: { printState(210); // XXX // Pick two alternative vectors of variables, relative to XBASE, that // are suitable as new positions of the KNEW-th interpolation point. // Firstly, XNEW is set to the point on a line through XOPT and another // interpolation point that minimizes the predicted value of the next // denominator, subject to ||XNEW - XOPT|| .LEQ. ADELT and to the SL // and SU bounds. Secondly, XALT is set to the best feasible point on // a constrained version of the Cauchy step of the KNEW-th Lagrange // function, the corresponding value of the square of this function // being returned in CAUCHY. The choice between these alternatives is // going to be made when the denominator is calculated. final double[] alphaCauchy = altmov(knew, adelt); alpha = alphaCauchy[0]; cauchy = alphaCauchy[1]; for (int i = 0; i < n; i++) { trialStepPoint.setEntry(i, newPoint.getEntry(i) - trustRegionCenterOffset.getEntry(i)); } // Calculate VLAG and BETA for the current choice of D. The scalar // product of D with XPT(K,.) is going to be held in W(NPT+K) for // use when VQUAD is calculated. } case 230: { printState(230); // XXX for (int k = 0; k < npt; k++) { double suma = ZERO; double sumb = ZERO; double sum = ZERO; for (int j = 0; j < n; j++) { suma += interpolationPoints.getEntry(k, j) * trialStepPoint.getEntry(j); sumb += interpolationPoints.getEntry(k, j) * trustRegionCenterOffset.getEntry(j); sum += bMatrix.getEntry(k, j) * trialStepPoint.getEntry(j); } work3.setEntry(k, suma * (HALF * suma + sumb)); lagrangeValuesAtNewPoint.setEntry(k, sum); work2.setEntry(k, suma); } beta = ZERO; for (int m = 0; m < nptm; m++) { double sum = ZERO; for (int k = 0; k < npt; k++) { sum += zMatrix.getEntry(k, m) * work3.getEntry(k); } beta -= sum * sum; for (int k = 0; k < npt; k++) { lagrangeValuesAtNewPoint.setEntry(k, lagrangeValuesAtNewPoint.getEntry(k) + sum * zMatrix.getEntry(k, m)); } } dsq = ZERO; double bsum = ZERO; double dx = ZERO; for (int j = 0; j < n; j++) { // Computing 2nd power final double d1 = trialStepPoint.getEntry(j); dsq += d1 * d1; double sum = ZERO; for (int k = 0; k < npt; k++) { sum += work3.getEntry(k) * bMatrix.getEntry(k, j); } bsum += sum * trialStepPoint.getEntry(j); final int jp = npt + j; for (int i = 0; i < n; i++) { sum += bMatrix.getEntry(jp, i) * trialStepPoint.getEntry(i); } lagrangeValuesAtNewPoint.setEntry(jp, sum); bsum += sum * trialStepPoint.getEntry(j); dx += trialStepPoint.getEntry(j) * trustRegionCenterOffset.getEntry(j); } beta = dx * dx + dsq * (xoptsq + dx + dx + HALF * dsq) + beta - bsum; // Original // beta += dx * dx + dsq * (xoptsq + dx + dx + HALF * dsq) - bsum; // XXX "testAckley" and "testDiffPow" fail. // beta = dx * dx + dsq * (xoptsq + 2 * dx + HALF * dsq) + beta - bsum; // XXX "testDiffPow" fails. lagrangeValuesAtNewPoint.setEntry(trustRegionCenterInterpolationPointIndex, lagrangeValuesAtNewPoint.getEntry(trustRegionCenterInterpolationPointIndex) + ONE); // If NTRITS is zero, the denominator may be increased by replacing // the step D of ALTMOV by a Cauchy step. Then RESCUE may be called if // rounding errors have damaged the chosen denominator. if (ntrits == 0) { // Computing 2nd power final double d1 = lagrangeValuesAtNewPoint.getEntry(knew); denom = d1 * d1 + alpha * beta; if (denom < cauchy && cauchy > ZERO) { for (int i = 0; i < n; i++) { newPoint.setEntry(i, alternativeNewPoint.getEntry(i)); trialStepPoint.setEntry(i, newPoint.getEntry(i) - trustRegionCenterOffset.getEntry(i)); } cauchy = ZERO; // XXX Useful statement? state = 230; break; } // Alternatively, if NTRITS is positive, then set KNEW to the index of // the next interpolation point to be deleted to make room for a trust // region step. Again RESCUE may be called if rounding errors have damaged_ // the chosen denominator, which is the reason for attempting to select // KNEW before calculating the next value of the objective function. } else { final double delsq = delta * delta; scaden = ZERO; biglsq = ZERO; knew = 0; for (int k = 0; k < npt; k++) { if (k == trustRegionCenterInterpolationPointIndex) { continue; } double hdiag = ZERO; for (int m = 0; m < nptm; m++) { // Computing 2nd power final double d1 = zMatrix.getEntry(k, m); hdiag += d1 * d1; } // Computing 2nd power final double d2 = lagrangeValuesAtNewPoint.getEntry(k); final double den = beta * hdiag + d2 * d2; distsq = ZERO; for (int j = 0; j < n; j++) { // Computing 2nd power final double d3 = interpolationPoints.getEntry(k, j) - trustRegionCenterOffset.getEntry(j); distsq += d3 * d3; } // Computing MAX // Computing 2nd power final double d4 = distsq / delsq; final double temp = Math.max(ONE, d4 * d4); if (temp * den > scaden) { scaden = temp * den; knew = k; denom = den; } // Computing MAX // Computing 2nd power final double d5 = lagrangeValuesAtNewPoint.getEntry(k); biglsq = Math.max(biglsq, temp * (d5 * d5)); } } // Put the variables for the next calculation of the objective function // in XNEW, with any adjustments for the bounds. // Calculate the value of the objective function at XBASE+XNEW, unless // the limit on the number of calculations of F has been reached. } case 360: { printState(360); // XXX for (int i = 0; i < n; i++) { // Computing MIN // Computing MAX final double d3 = lowerBound[i]; final double d4 = originShift.getEntry(i) + newPoint.getEntry(i); final double d1 = Math.max(d3, d4); final double d2 = upperBound[i]; currentBest.setEntry(i, Math.min(d1, d2)); if (newPoint.getEntry(i) == lowerDifference.getEntry(i)) { currentBest.setEntry(i, lowerBound[i]); } if (newPoint.getEntry(i) == upperDifference.getEntry(i)) { currentBest.setEntry(i, upperBound[i]); } } f = computeObjectiveValue(currentBest.toArray()); if (!isMinimize) f = -f; if (ntrits == -1) { fsave = f; state = 720; break; } // Use the quadratic model to predict the change in F due to the step D, // and set DIFF to the error of this prediction. final double fopt = fAtInterpolationPoints.getEntry(trustRegionCenterInterpolationPointIndex); double vquad = ZERO; int ih = 0; for (int j = 0; j < n; j++) { vquad += trialStepPoint.getEntry(j) * gradientAtTrustRegionCenter.getEntry(j); for (int i = 0; i <= j; i++) { double temp = trialStepPoint.getEntry(i) * trialStepPoint.getEntry(j); if (i == j) { temp *= HALF; } vquad += modelSecondDerivativesValues.getEntry(ih) * temp; ih++; } } for (int k = 0; k < npt; k++) { // Computing 2nd power final double d1 = work2.getEntry(k); final double d2 = d1 * d1; // "d1" must be squared first to prevent test failures. vquad += HALF * modelSecondDerivativesParameters.getEntry(k) * d2; } final double diff = f - fopt - vquad; diffc = diffb; diffb = diffa; diffa = Math.abs(diff); if (dnorm > rho) { nfsav = getEvaluations(); } // Pick the next value of DELTA after a trust region step. if (ntrits > 0) { if (vquad >= ZERO) { throw new MathIllegalStateException(LocalizedFormats.TRUST_REGION_STEP_FAILED, vquad); } ratio = (f - fopt) / vquad; final double hDelta = HALF * delta; if (ratio <= ONE_OVER_TEN) { // Computing MIN delta = Math.min(hDelta, dnorm); } else if (ratio <= .7) { // Computing MAX delta = Math.max(hDelta, dnorm); } else { // Computing MAX delta = Math.max(hDelta, 2 * dnorm); } if (delta <= rho * 1.5) { delta = rho; } // Recalculate KNEW and DENOM if the new F is less than FOPT. if (f < fopt) { final int ksav = knew; final double densav = denom; final double delsq = delta * delta; scaden = ZERO; biglsq = ZERO; knew = 0; for (int k = 0; k < npt; k++) { double hdiag = ZERO; for (int m = 0; m < nptm; m++) { // Computing 2nd power final double d1 = zMatrix.getEntry(k, m); hdiag += d1 * d1; } // Computing 2nd power final double d1 = lagrangeValuesAtNewPoint.getEntry(k); final double den = beta * hdiag + d1 * d1; distsq = ZERO; for (int j = 0; j < n; j++) { // Computing 2nd power final double d2 = interpolationPoints.getEntry(k, j) - newPoint.getEntry(j); distsq += d2 * d2; } // Computing MAX // Computing 2nd power final double d3 = distsq / delsq; final double temp = Math.max(ONE, d3 * d3); if (temp * den > scaden) { scaden = temp * den; knew = k; denom = den; } // Computing MAX // Computing 2nd power final double d4 = lagrangeValuesAtNewPoint.getEntry(k); final double d5 = temp * (d4 * d4); biglsq = Math.max(biglsq, d5); } if (scaden <= HALF * biglsq) { knew = ksav; denom = densav; } } } // Update BMAT and ZMAT, so that the KNEW-th interpolation point can be // moved. Also update the second derivative terms of the model. update(beta, denom, knew); ih = 0; final double pqold = modelSecondDerivativesParameters.getEntry(knew); modelSecondDerivativesParameters.setEntry(knew, ZERO); for (int i = 0; i < n; i++) { final double temp = pqold * interpolationPoints.getEntry(knew, i); for (int j = 0; j <= i; j++) { modelSecondDerivativesValues.setEntry(ih, modelSecondDerivativesValues.getEntry(ih) + temp * interpolationPoints.getEntry(knew, j)); ih++; } } for (int m = 0; m < nptm; m++) { final double temp = diff * zMatrix.getEntry(knew, m); for (int k = 0; k < npt; k++) { modelSecondDerivativesParameters.setEntry(k, modelSecondDerivativesParameters.getEntry(k) + temp * zMatrix.getEntry(k, m)); } } // Include the new interpolation point, and make the changes to GOPT at // the old XOPT that are caused by the updating of the quadratic model. fAtInterpolationPoints.setEntry(knew, f); for (int i = 0; i < n; i++) { interpolationPoints.setEntry(knew, i, newPoint.getEntry(i)); work1.setEntry(i, bMatrix.getEntry(knew, i)); } for (int k = 0; k < npt; k++) { double suma = ZERO; for (int m = 0; m < nptm; m++) { suma += zMatrix.getEntry(knew, m) * zMatrix.getEntry(k, m); } double sumb = ZERO; for (int j = 0; j < n; j++) { sumb += interpolationPoints.getEntry(k, j) * trustRegionCenterOffset.getEntry(j); } final double temp = suma * sumb; for (int i = 0; i < n; i++) { work1.setEntry(i, work1.getEntry(i) + temp * interpolationPoints.getEntry(k, i)); } } for (int i = 0; i < n; i++) { gradientAtTrustRegionCenter.setEntry(i, gradientAtTrustRegionCenter.getEntry(i) + diff * work1.getEntry(i)); } // Update XOPT, GOPT and KOPT if the new calculated F is less than FOPT. if (f < fopt) { trustRegionCenterInterpolationPointIndex = knew; xoptsq = ZERO; ih = 0; for (int j = 0; j < n; j++) { trustRegionCenterOffset.setEntry(j, newPoint.getEntry(j)); // Computing 2nd power final double d1 = trustRegionCenterOffset.getEntry(j); xoptsq += d1 * d1; for (int i = 0; i <= j; i++) { if (i < j) { gradientAtTrustRegionCenter.setEntry(j, gradientAtTrustRegionCenter.getEntry(j) + modelSecondDerivativesValues.getEntry(ih) * trialStepPoint.getEntry(i)); } gradientAtTrustRegionCenter.setEntry(i, gradientAtTrustRegionCenter.getEntry(i) + modelSecondDerivativesValues.getEntry(ih) * trialStepPoint.getEntry(j)); ih++; } } for (int k = 0; k < npt; k++) { double temp = ZERO; for (int j = 0; j < n; j++) { temp += interpolationPoints.getEntry(k, j) * trialStepPoint.getEntry(j); } temp *= modelSecondDerivativesParameters.getEntry(k); for (int i = 0; i < n; i++) { gradientAtTrustRegionCenter.setEntry(i, gradientAtTrustRegionCenter.getEntry(i) + temp * interpolationPoints.getEntry(k, i)); } } } // Calculate the parameters of the least Frobenius norm interpolant to // the current data, the gradient of this interpolant at XOPT being put // into VLAG(NPT+I), I=1,2,...,N. if (ntrits > 0) { for (int k = 0; k < npt; k++) { lagrangeValuesAtNewPoint.setEntry(k, fAtInterpolationPoints.getEntry(k) - fAtInterpolationPoints.getEntry(trustRegionCenterInterpolationPointIndex)); work3.setEntry(k, ZERO); } for (int j = 0; j < nptm; j++) { double sum = ZERO; for (int k = 0; k < npt; k++) { sum += zMatrix.getEntry(k, j) * lagrangeValuesAtNewPoint.getEntry(k); } for (int k = 0; k < npt; k++) { work3.setEntry(k, work3.getEntry(k) + sum * zMatrix.getEntry(k, j)); } } for (int k = 0; k < npt; k++) { double sum = ZERO; for (int j = 0; j < n; j++) { sum += interpolationPoints.getEntry(k, j) * trustRegionCenterOffset.getEntry(j); } work2.setEntry(k, work3.getEntry(k)); work3.setEntry(k, sum * work3.getEntry(k)); } double gqsq = ZERO; double gisq = ZERO; for (int i = 0; i < n; i++) { double sum = ZERO; for (int k = 0; k < npt; k++) { sum += bMatrix.getEntry(k, i) * lagrangeValuesAtNewPoint.getEntry(k) + interpolationPoints.getEntry(k, i) * work3.getEntry(k); } if (trustRegionCenterOffset.getEntry(i) == lowerDifference.getEntry(i)) { // Computing MIN // Computing 2nd power final double d1 = Math.min(ZERO, gradientAtTrustRegionCenter.getEntry(i)); gqsq += d1 * d1; // Computing 2nd power final double d2 = Math.min(ZERO, sum); gisq += d2 * d2; } else if (trustRegionCenterOffset.getEntry(i) == upperDifference.getEntry(i)) { // Computing MAX // Computing 2nd power final double d1 = Math.max(ZERO, gradientAtTrustRegionCenter.getEntry(i)); gqsq += d1 * d1; // Computing 2nd power final double d2 = Math.max(ZERO, sum); gisq += d2 * d2; } else { // Computing 2nd power final double d1 = gradientAtTrustRegionCenter.getEntry(i); gqsq += d1 * d1; gisq += sum * sum; } lagrangeValuesAtNewPoint.setEntry(npt + i, sum); } // Test whether to replace the new quadratic model by the least Frobenius // norm interpolant, making the replacement if the test is satisfied. ++itest; if (gqsq < TEN * gisq) { itest = 0; } if (itest >= 3) { for (int i = 0, max = Math.max(npt, nh); i < max; i++) { if (i < n) { gradientAtTrustRegionCenter.setEntry(i, lagrangeValuesAtNewPoint.getEntry(npt + i)); } if (i < npt) { modelSecondDerivativesParameters.setEntry(i, work2.getEntry(i)); } if (i < nh) { modelSecondDerivativesValues.setEntry(i, ZERO); } itest = 0; } } } // If a trust region step has provided a sufficient decrease in F, then // branch for another trust region calculation. The case NTRITS=0 occurs // when the new interpolation point was reached by an alternative step. if (ntrits == 0) { state = 60; break; } if (f <= fopt + ONE_OVER_TEN * vquad) { state = 60; break; } // Alternatively, find out if the interpolation points are close enough // to the best point so far. // Computing MAX // Computing 2nd power final double d1 = TWO * delta; // Computing 2nd power final double d2 = TEN * rho; distsq = Math.max(d1 * d1, d2 * d2); } case 650: { printState(650); // XXX knew = -1; for (int k = 0; k < npt; k++) { double sum = ZERO; for (int j = 0; j < n; j++) { // Computing 2nd power final double d1 = interpolationPoints.getEntry(k, j) - trustRegionCenterOffset.getEntry(j); sum += d1 * d1; } if (sum > distsq) { knew = k; distsq = sum; } } // If KNEW is positive, then ALTMOV finds alternative new positions for // the KNEW-th interpolation point within distance ADELT of XOPT. It is // reached via label 90. Otherwise, there is a branch to label 60 for // another trust region iteration, unless the calculations with the // current RHO are complete. if (knew >= 0) { final double dist = Math.sqrt(distsq); if (ntrits == -1) { // Computing MIN delta = Math.min(ONE_OVER_TEN * delta, HALF * dist); if (delta <= rho * 1.5) { delta = rho; } } ntrits = 0; // Computing MAX // Computing MIN final double d1 = Math.min(ONE_OVER_TEN * dist, delta); adelt = Math.max(d1, rho); dsq = adelt * adelt; state = 90; break; } if (ntrits == -1) { state = 680; break; } if (ratio > ZERO) { state = 60; break; } if (Math.max(delta, dnorm) > rho) { state = 60; break; } // The calculations with the current value of RHO are complete. Pick the // next values of RHO and DELTA. } case 680: { printState(680); // XXX if (rho > stoppingTrustRegionRadius) { delta = HALF * rho; ratio = rho / stoppingTrustRegionRadius; if (ratio <= SIXTEEN) { rho = stoppingTrustRegionRadius; } else if (ratio <= TWO_HUNDRED_FIFTY) { rho = Math.sqrt(ratio) * stoppingTrustRegionRadius; } else { rho *= ONE_OVER_TEN; } delta = Math.max(delta, rho); ntrits = 0; nfsav = getEvaluations(); state = 60; break; } // Return from the calculation, after another Newton-Raphson step, if // it is too short to have been tried before. if (ntrits == -1) { state = 360; break; } } case 720: { printState(720); // XXX if (fAtInterpolationPoints.getEntry(trustRegionCenterInterpolationPointIndex) <= fsave) { for (int i = 0; i < n; i++) { // Computing MIN // Computing MAX final double d3 = lowerBound[i]; final double d4 = originShift.getEntry(i) + trustRegionCenterOffset.getEntry(i); final double d1 = Math.max(d3, d4); final double d2 = upperBound[i]; currentBest.setEntry(i, Math.min(d1, d2)); if (trustRegionCenterOffset.getEntry(i) == lowerDifference.getEntry(i)) { currentBest.setEntry(i, lowerBound[i]); } if (trustRegionCenterOffset.getEntry(i) == upperDifference.getEntry(i)) { currentBest.setEntry(i, upperBound[i]); } } f = fAtInterpolationPoints.getEntry(trustRegionCenterInterpolationPointIndex); } return f; } default: { throw new MathIllegalStateException(LocalizedFormats.SIMPLE_MESSAGE, "bobyqb"); }} } // bobyqb // ---------------------------------------------------------------------------------------- /** * The arguments N, NPT, XPT, XOPT, BMAT, ZMAT, NDIM, SL and SU all have * the same meanings as the corresponding arguments of BOBYQB. * KOPT is the index of the optimal interpolation point. * KNEW is the index of the interpolation point that is going to be moved. * ADELT is the current trust region bound. * XNEW will be set to a suitable new position for the interpolation point * XPT(KNEW,.). Specifically, it satisfies the SL, SU and trust region * bounds and it should provide a large denominator in the next call of * UPDATE. The step XNEW-XOPT from XOPT is restricted to moves along the * straight lines through XOPT and another interpolation point. * XALT also provides a large value of the modulus of the KNEW-th Lagrange * function subject to the constraints that have been mentioned, its main * difference from XNEW being that XALT-XOPT is a constrained version of * the Cauchy step within the trust region. An exception is that XALT is * not calculated if all components of GLAG (see below) are zero. * ALPHA will be set to the KNEW-th diagonal element of the H matrix. * CAUCHY will be set to the square of the KNEW-th Lagrange function at * the step XALT-XOPT from XOPT for the vector XALT that is returned, * except that CAUCHY is set to zero if XALT is not calculated. * GLAG is a working space vector of length N for the gradient of the * KNEW-th Lagrange function at XOPT. * HCOL is a working space vector of length NPT for the second derivative * coefficients of the KNEW-th Lagrange function. * W is a working space vector of length 2N that is going to hold the * constrained Cauchy step from XOPT of the Lagrange function, followed * by the downhill version of XALT when the uphill step is calculated. * * Set the first NPT components of W to the leading elements of the * KNEW-th column of the H matrix. * @param knew * @param adelt */ private double[] altmov( int knew, double adelt ) { printMethod(); // XXX final int n = currentBest.getDimension(); final int npt = numberOfInterpolationPoints; final ArrayRealVector glag = new ArrayRealVector(n); final ArrayRealVector hcol = new ArrayRealVector(npt); final ArrayRealVector work1 = new ArrayRealVector(n); final ArrayRealVector work2 = new ArrayRealVector(n); for (int k = 0; k < npt; k++) { hcol.setEntry(k, ZERO); } for (int j = 0, max = npt - n - 1; j < max; j++) { final double tmp = zMatrix.getEntry(knew, j); for (int k = 0; k < npt; k++) { hcol.setEntry(k, hcol.getEntry(k) + tmp * zMatrix.getEntry(k, j)); } } final double alpha = hcol.getEntry(knew); final double ha = HALF * alpha; // Calculate the gradient of the KNEW-th Lagrange function at XOPT. for (int i = 0; i < n; i++) { glag.setEntry(i, bMatrix.getEntry(knew, i)); } for (int k = 0; k < npt; k++) { double tmp = ZERO; for (int j = 0; j < n; j++) { tmp += interpolationPoints.getEntry(k, j) * trustRegionCenterOffset.getEntry(j); } tmp *= hcol.getEntry(k); for (int i = 0; i < n; i++) { glag.setEntry(i, glag.getEntry(i) + tmp * interpolationPoints.getEntry(k, i)); } } // Search for a large denominator along the straight lines through XOPT // and another interpolation point. SLBD and SUBD will be lower and upper // bounds on the step along each of these lines in turn. PREDSQ will be // set to the square of the predicted denominator for each line. PRESAV // will be set to the largest admissible value of PREDSQ that occurs. double presav = ZERO; double step = Double.NaN; int ksav = 0; int ibdsav = 0; double stpsav = 0; for (int k = 0; k < npt; k++) { if (k == trustRegionCenterInterpolationPointIndex) { continue; } double dderiv = ZERO; double distsq = ZERO; for (int i = 0; i < n; i++) { final double tmp = interpolationPoints.getEntry(k, i) - trustRegionCenterOffset.getEntry(i); dderiv += glag.getEntry(i) * tmp; distsq += tmp * tmp; } double subd = adelt / Math.sqrt(distsq); double slbd = -subd; int ilbd = 0; int iubd = 0; final double sumin = Math.min(ONE, subd); // Revise SLBD and SUBD if necessary because of the bounds in SL and SU. for (int i = 0; i < n; i++) { final double tmp = interpolationPoints.getEntry(k, i) - trustRegionCenterOffset.getEntry(i); if (tmp > ZERO) { if (slbd * tmp < lowerDifference.getEntry(i) - trustRegionCenterOffset.getEntry(i)) { slbd = (lowerDifference.getEntry(i) - trustRegionCenterOffset.getEntry(i)) / tmp; ilbd = -i - 1; } if (subd * tmp > upperDifference.getEntry(i) - trustRegionCenterOffset.getEntry(i)) { // Computing MAX subd = Math.max(sumin, (upperDifference.getEntry(i) - trustRegionCenterOffset.getEntry(i)) / tmp); iubd = i + 1; } } else if (tmp < ZERO) { if (slbd * tmp > upperDifference.getEntry(i) - trustRegionCenterOffset.getEntry(i)) { slbd = (upperDifference.getEntry(i) - trustRegionCenterOffset.getEntry(i)) / tmp; ilbd = i + 1; } if (subd * tmp < lowerDifference.getEntry(i) - trustRegionCenterOffset.getEntry(i)) { // Computing MAX subd = Math.max(sumin, (lowerDifference.getEntry(i) - trustRegionCenterOffset.getEntry(i)) / tmp); iubd = -i - 1; } } } // Seek a large modulus of the KNEW-th Lagrange function when the index // of the other interpolation point on the line through XOPT is KNEW. step = slbd; int isbd = ilbd; double vlag = Double.NaN; if (k == knew) { final double diff = dderiv - ONE; vlag = slbd * (dderiv - slbd * diff); final double d1 = subd * (dderiv - subd * diff); if (Math.abs(d1) > Math.abs(vlag)) { step = subd; vlag = d1; isbd = iubd; } final double d2 = HALF * dderiv; final double d3 = d2 - diff * slbd; final double d4 = d2 - diff * subd; if (d3 * d4 < ZERO) { final double d5 = d2 * d2 / diff; if (Math.abs(d5) > Math.abs(vlag)) { step = d2 / diff; vlag = d5; isbd = 0; } } // Search along each of the other lines through XOPT and another point. } else { vlag = slbd * (ONE - slbd); final double tmp = subd * (ONE - subd); if (Math.abs(tmp) > Math.abs(vlag)) { step = subd; vlag = tmp; isbd = iubd; } if (subd > HALF && Math.abs(vlag) < ONE_OVER_FOUR) { step = HALF; vlag = ONE_OVER_FOUR; isbd = 0; } vlag *= dderiv; } // Calculate PREDSQ for the current line search and maintain PRESAV. final double tmp = step * (ONE - step) * distsq; final double predsq = vlag * vlag * (vlag * vlag + ha * tmp * tmp); if (predsq > presav) { presav = predsq; ksav = k; stpsav = step; ibdsav = isbd; } } // Construct XNEW in a way that satisfies the bound constraints exactly. for (int i = 0; i < n; i++) { final double tmp = trustRegionCenterOffset.getEntry(i) + stpsav * (interpolationPoints.getEntry(ksav, i) - trustRegionCenterOffset.getEntry(i)); newPoint.setEntry(i, Math.max(lowerDifference.getEntry(i), Math.min(upperDifference.getEntry(i), tmp))); } if (ibdsav < 0) { newPoint.setEntry(-ibdsav - 1, lowerDifference.getEntry(-ibdsav - 1)); } if (ibdsav > 0) { newPoint.setEntry(ibdsav - 1, upperDifference.getEntry(ibdsav - 1)); } // Prepare for the iterative method that assembles the constrained Cauchy // step in W. The sum of squares of the fixed components of W is formed in // WFIXSQ, and the free components of W are set to BIGSTP. final double bigstp = adelt + adelt; int iflag = 0; double cauchy = Double.NaN; double csave = ZERO; while (true) { double wfixsq = ZERO; double ggfree = ZERO; for (int i = 0; i < n; i++) { final double glagValue = glag.getEntry(i); work1.setEntry(i, ZERO); if (Math.min(trustRegionCenterOffset.getEntry(i) - lowerDifference.getEntry(i), glagValue) > ZERO || Math.max(trustRegionCenterOffset.getEntry(i) - upperDifference.getEntry(i), glagValue) < ZERO) { work1.setEntry(i, bigstp); // Computing 2nd power ggfree += glagValue * glagValue; } } if (ggfree == ZERO) { return new double[] { alpha, ZERO }; } // Investigate whether more components of W can be fixed. final double tmp1 = adelt * adelt - wfixsq; if (tmp1 > ZERO) { step = Math.sqrt(tmp1 / ggfree); ggfree = ZERO; for (int i = 0; i < n; i++) { if (work1.getEntry(i) == bigstp) { final double tmp2 = trustRegionCenterOffset.getEntry(i) - step * glag.getEntry(i); if (tmp2 <= lowerDifference.getEntry(i)) { work1.setEntry(i, lowerDifference.getEntry(i) - trustRegionCenterOffset.getEntry(i)); // Computing 2nd power final double d1 = work1.getEntry(i); wfixsq += d1 * d1; } else if (tmp2 >= upperDifference.getEntry(i)) { work1.setEntry(i, upperDifference.getEntry(i) - trustRegionCenterOffset.getEntry(i)); // Computing 2nd power final double d1 = work1.getEntry(i); wfixsq += d1 * d1; } else { // Computing 2nd power final double d1 = glag.getEntry(i); ggfree += d1 * d1; } } } } // Set the remaining free components of W and all components of XALT, // except that W may be scaled later. double gw = ZERO; for (int i = 0; i < n; i++) { final double glagValue = glag.getEntry(i); if (work1.getEntry(i) == bigstp) { work1.setEntry(i, -step * glagValue); final double min = Math.min(upperDifference.getEntry(i), trustRegionCenterOffset.getEntry(i) + work1.getEntry(i)); alternativeNewPoint.setEntry(i, Math.max(lowerDifference.getEntry(i), min)); } else if (work1.getEntry(i) == ZERO) { alternativeNewPoint.setEntry(i, trustRegionCenterOffset.getEntry(i)); } else if (glagValue > ZERO) { alternativeNewPoint.setEntry(i, lowerDifference.getEntry(i)); } else { alternativeNewPoint.setEntry(i, upperDifference.getEntry(i)); } gw += glagValue * work1.getEntry(i); } // Set CURV to the curvature of the KNEW-th Lagrange function along W. // Scale W by a factor less than one if that can reduce the modulus of // the Lagrange function at XOPT+W. Set CAUCHY to the final value of // the square of this function. double curv = ZERO; for (int k = 0; k < npt; k++) { double tmp = ZERO; for (int j = 0; j < n; j++) { tmp += interpolationPoints.getEntry(k, j) * work1.getEntry(j); } curv += hcol.getEntry(k) * tmp * tmp; } if (iflag == 1) { curv = -curv; } if (curv > -gw && curv < -gw * (ONE + Math.sqrt(TWO))) { final double scale = -gw / curv; for (int i = 0; i < n; i++) { final double tmp = trustRegionCenterOffset.getEntry(i) + scale * work1.getEntry(i); alternativeNewPoint.setEntry(i, Math.max(lowerDifference.getEntry(i), Math.min(upperDifference.getEntry(i), tmp))); } // Computing 2nd power final double d1 = HALF * gw * scale; cauchy = d1 * d1; } else { // Computing 2nd power final double d1 = gw + HALF * curv; cauchy = d1 * d1; } // If IFLAG is zero, then XALT is calculated as before after reversing // the sign of GLAG. Thus two XALT vectors become available. The one that // is chosen is the one that gives the larger value of CAUCHY. if (iflag == 0) { for (int i = 0; i < n; i++) { glag.setEntry(i, -glag.getEntry(i)); work2.setEntry(i, alternativeNewPoint.getEntry(i)); } csave = cauchy; iflag = 1; } else { break; } } if (csave > cauchy) { for (int i = 0; i < n; i++) { alternativeNewPoint.setEntry(i, work2.getEntry(i)); } cauchy = csave; } return new double[] { alpha, cauchy }; } // altmov // ---------------------------------------------------------------------------------------- /** * SUBROUTINE PRELIM sets the elements of XBASE, XPT, FVAL, GOPT, HQ, PQ, * BMAT and ZMAT for the first iteration, and it maintains the values of * NF and KOPT. The vector X is also changed by PRELIM. * * The arguments N, NPT, X, XL, XU, RHOBEG, IPRINT and MAXFUN are the * same as the corresponding arguments in SUBROUTINE BOBYQA. * The arguments XBASE, XPT, FVAL, HQ, PQ, BMAT, ZMAT, NDIM, SL and SU * are the same as the corresponding arguments in BOBYQB, the elements * of SL and SU being set in BOBYQA. * GOPT is usually the gradient of the quadratic model at XOPT+XBASE, but * it is set by PRELIM to the gradient of the quadratic model at XBASE. * If XOPT is nonzero, BOBYQB will change it to its usual value later. * NF is maintaned as the number of calls of CALFUN so far. * KOPT will be such that the least calculated value of F so far is at * the point XPT(KOPT,.)+XBASE in the space of the variables. * * @param lowerBound Lower bounds. * @param upperBound Upper bounds. */ private void prelim(double[] lowerBound, double[] upperBound) { printMethod(); // XXX final int n = currentBest.getDimension(); final int npt = numberOfInterpolationPoints; final int ndim = bMatrix.getRowDimension(); final double rhosq = initialTrustRegionRadius * initialTrustRegionRadius; final double recip = 1d / rhosq; final int np = n + 1; // Set XBASE to the initial vector of variables, and set the initial // elements of XPT, BMAT, HQ, PQ and ZMAT to zero. for (int j = 0; j < n; j++) { originShift.setEntry(j, currentBest.getEntry(j)); for (int k = 0; k < npt; k++) { interpolationPoints.setEntry(k, j, ZERO); } for (int i = 0; i < ndim; i++) { bMatrix.setEntry(i, j, ZERO); } } for (int i = 0, max = n * np / 2; i < max; i++) { modelSecondDerivativesValues.setEntry(i, ZERO); } for (int k = 0; k < npt; k++) { modelSecondDerivativesParameters.setEntry(k, ZERO); for (int j = 0, max = npt - np; j < max; j++) { zMatrix.setEntry(k, j, ZERO); } } // Begin the initialization procedure. NF becomes one more than the number // of function values so far. The coordinates of the displacement of the // next initial interpolation point from XBASE are set in XPT(NF+1,.). int ipt = 0; int jpt = 0; double fbeg = Double.NaN; do { final int nfm = getEvaluations(); final int nfx = nfm - n; final int nfmm = nfm - 1; final int nfxm = nfx - 1; double stepa = 0; double stepb = 0; if (nfm <= 2 * n) { if (nfm >= 1 && nfm <= n) { stepa = initialTrustRegionRadius; if (upperDifference.getEntry(nfmm) == ZERO) { stepa = -stepa; // throw new PathIsExploredException(); // XXX } interpolationPoints.setEntry(nfm, nfmm, stepa); } else if (nfm > n) { stepa = interpolationPoints.getEntry(nfx, nfxm); stepb = -initialTrustRegionRadius; if (lowerDifference.getEntry(nfxm) == ZERO) { stepb = Math.min(TWO * initialTrustRegionRadius, upperDifference.getEntry(nfxm)); // throw new PathIsExploredException(); // XXX } if (upperDifference.getEntry(nfxm) == ZERO) { stepb = Math.max(-TWO * initialTrustRegionRadius, lowerDifference.getEntry(nfxm)); // throw new PathIsExploredException(); // XXX } interpolationPoints.setEntry(nfm, nfxm, stepb); } } else { final int tmp1 = (nfm - np) / n; jpt = nfm - tmp1 * n - n; ipt = jpt + tmp1; if (ipt > n) { final int tmp2 = jpt; jpt = ipt - n; ipt = tmp2; // throw new PathIsExploredException(); // XXX } final int iptMinus1 = ipt - 1; final int jptMinus1 = jpt - 1; interpolationPoints.setEntry(nfm, iptMinus1, interpolationPoints.getEntry(ipt, iptMinus1)); interpolationPoints.setEntry(nfm, jptMinus1, interpolationPoints.getEntry(jpt, jptMinus1)); } // Calculate the next value of F. The least function value so far and // its index are required. for (int j = 0; j < n; j++) { currentBest.setEntry(j, Math.min(Math.max(lowerBound[j], originShift.getEntry(j) + interpolationPoints.getEntry(nfm, j)), upperBound[j])); if (interpolationPoints.getEntry(nfm, j) == lowerDifference.getEntry(j)) { currentBest.setEntry(j, lowerBound[j]); } if (interpolationPoints.getEntry(nfm, j) == upperDifference.getEntry(j)) { currentBest.setEntry(j, upperBound[j]); } } final double objectiveValue = computeObjectiveValue(currentBest.toArray()); final double f = isMinimize ? objectiveValue : -objectiveValue; final int numEval = getEvaluations(); // nfm + 1 fAtInterpolationPoints.setEntry(nfm, f); if (numEval == 1) { fbeg = f; trustRegionCenterInterpolationPointIndex = 0; } else if (f < fAtInterpolationPoints.getEntry(trustRegionCenterInterpolationPointIndex)) { trustRegionCenterInterpolationPointIndex = nfm; } // Set the nonzero initial elements of BMAT and the quadratic model in the // cases when NF is at most 2*N+1. If NF exceeds N+1, then the positions // of the NF-th and (NF-N)-th interpolation points may be switched, in // order that the function value at the first of them contributes to the // off-diagonal second derivative terms of the initial quadratic model. if (numEval <= 2 * n + 1) { if (numEval >= 2 && numEval <= n + 1) { gradientAtTrustRegionCenter.setEntry(nfmm, (f - fbeg) / stepa); if (npt < numEval + n) { final double oneOverStepA = ONE / stepa; bMatrix.setEntry(0, nfmm, -oneOverStepA); bMatrix.setEntry(nfm, nfmm, oneOverStepA); bMatrix.setEntry(npt + nfmm, nfmm, -HALF * rhosq); // throw new PathIsExploredException(); // XXX } } else if (numEval >= n + 2) { final int ih = nfx * (nfx + 1) / 2 - 1; final double tmp = (f - fbeg) / stepb; final double diff = stepb - stepa; modelSecondDerivativesValues.setEntry(ih, TWO * (tmp - gradientAtTrustRegionCenter.getEntry(nfxm)) / diff); gradientAtTrustRegionCenter.setEntry(nfxm, (gradientAtTrustRegionCenter.getEntry(nfxm) * stepb - tmp * stepa) / diff); if (stepa * stepb < ZERO && f < fAtInterpolationPoints.getEntry(nfm - n)) { fAtInterpolationPoints.setEntry(nfm, fAtInterpolationPoints.getEntry(nfm - n)); fAtInterpolationPoints.setEntry(nfm - n, f); if (trustRegionCenterInterpolationPointIndex == nfm) { trustRegionCenterInterpolationPointIndex = nfm - n; } interpolationPoints.setEntry(nfm - n, nfxm, stepb); interpolationPoints.setEntry(nfm, nfxm, stepa); } bMatrix.setEntry(0, nfxm, -(stepa + stepb) / (stepa * stepb)); bMatrix.setEntry(nfm, nfxm, -HALF / interpolationPoints.getEntry(nfm - n, nfxm)); bMatrix.setEntry(nfm - n, nfxm, -bMatrix.getEntry(0, nfxm) - bMatrix.getEntry(nfm, nfxm)); zMatrix.setEntry(0, nfxm, Math.sqrt(TWO) / (stepa * stepb)); zMatrix.setEntry(nfm, nfxm, Math.sqrt(HALF) / rhosq); // zMatrix.setEntry(nfm, nfxm, Math.sqrt(HALF) * recip); // XXX "testAckley" and "testDiffPow" fail. zMatrix.setEntry(nfm - n, nfxm, -zMatrix.getEntry(0, nfxm) - zMatrix.getEntry(nfm, nfxm)); } // Set the off-diagonal second derivatives of the Lagrange functions and // the initial quadratic model. } else { zMatrix.setEntry(0, nfxm, recip); zMatrix.setEntry(nfm, nfxm, recip); zMatrix.setEntry(ipt, nfxm, -recip); zMatrix.setEntry(jpt, nfxm, -recip); final int ih = ipt * (ipt - 1) / 2 + jpt - 1; final double tmp = interpolationPoints.getEntry(nfm, ipt - 1) * interpolationPoints.getEntry(nfm, jpt - 1); modelSecondDerivativesValues.setEntry(ih, (fbeg - fAtInterpolationPoints.getEntry(ipt) - fAtInterpolationPoints.getEntry(jpt) + f) / tmp); // throw new PathIsExploredException(); // XXX } } while (getEvaluations() < npt); } // prelim // ---------------------------------------------------------------------------------------- /** * A version of the truncated conjugate gradient is applied. If a line * search is restricted by a constraint, then the procedure is restarted, * the values of the variables that are at their bounds being fixed. If * the trust region boundary is reached, then further changes may be made * to D, each one being in the two dimensional space that is spanned * by the current D and the gradient of Q at XOPT+D, staying on the trust * region boundary. Termination occurs when the reduction in Q seems to * be close to the greatest reduction that can be achieved. * The arguments N, NPT, XPT, XOPT, GOPT, HQ, PQ, SL and SU have the same * meanings as the corresponding arguments of BOBYQB. * DELTA is the trust region radius for the present calculation, which * seeks a small value of the quadratic model within distance DELTA of * XOPT subject to the bounds on the variables. * XNEW will be set to a new vector of variables that is approximately * the one that minimizes the quadratic model within the trust region * subject to the SL and SU constraints on the variables. It satisfies * as equations the bounds that become active during the calculation. * D is the calculated trial step from XOPT, generated iteratively from an * initial value of zero. Thus XNEW is XOPT+D after the final iteration. * GNEW holds the gradient of the quadratic model at XOPT+D. It is updated * when D is updated. * xbdi.get( is a working space vector. For I=1,2,...,N, the element xbdi.get((I) is * set to -1.0, 0.0, or 1.0, the value being nonzero if and only if the * I-th variable has become fixed at a bound, the bound being SL(I) or * SU(I) in the case xbdi.get((I)=-1.0 or xbdi.get((I)=1.0, respectively. This * information is accumulated during the construction of XNEW. * The arrays S, HS and HRED are also used for working space. They hold the * current search direction, and the changes in the gradient of Q along S * and the reduced D, respectively, where the reduced D is the same as D, * except that the components of the fixed variables are zero. * DSQ will be set to the square of the length of XNEW-XOPT. * CRVMIN is set to zero if D reaches the trust region boundary. Otherwise * it is set to the least curvature of H that occurs in the conjugate * gradient searches that are not restricted by any constraints. The * value CRVMIN=-1.0D0 is set, however, if all of these searches are * constrained. * @param delta * @param gnew * @param xbdi * @param s * @param hs * @param hred */ private double[] trsbox( double delta, ArrayRealVector gnew, ArrayRealVector xbdi, ArrayRealVector s, ArrayRealVector hs, ArrayRealVector hred ) { printMethod(); // XXX final int n = currentBest.getDimension(); final int npt = numberOfInterpolationPoints; double dsq = Double.NaN; double crvmin = Double.NaN; // Local variables double ds; int iu; double dhd, dhs, cth, shs, sth, ssq, beta=0, sdec, blen; int iact = -1; int nact = 0; double angt = 0, qred; int isav; double temp = 0, xsav = 0, xsum = 0, angbd = 0, dredg = 0, sredg = 0; int iterc; double resid = 0, delsq = 0, ggsav = 0, tempa = 0, tempb = 0, redmax = 0, dredsq = 0, redsav = 0, gredsq = 0, rednew = 0; int itcsav = 0; double rdprev = 0, rdnext = 0, stplen = 0, stepsq = 0; int itermax = 0; // Set some constants. // Function Body // The sign of GOPT(I) gives the sign of the change to the I-th variable // that will reduce Q from its value at XOPT. Thus xbdi.get((I) shows whether // or not to fix the I-th variable at one of its bounds initially, with // NACT being set to the number of fixed variables. D and GNEW are also // set for the first iteration. DELSQ is the upper bound on the sum of // squares of the free variables. QRED is the reduction in Q so far. iterc = 0; nact = 0; for (int i = 0; i < n; i++) { xbdi.setEntry(i, ZERO); if (trustRegionCenterOffset.getEntry(i) <= lowerDifference.getEntry(i)) { if (gradientAtTrustRegionCenter.getEntry(i) >= ZERO) { xbdi.setEntry(i, MINUS_ONE); } } else if (trustRegionCenterOffset.getEntry(i) >= upperDifference.getEntry(i) && gradientAtTrustRegionCenter.getEntry(i) <= ZERO) { xbdi.setEntry(i, ONE); } if (xbdi.getEntry(i) != ZERO) { ++nact; } trialStepPoint.setEntry(i, ZERO); gnew.setEntry(i, gradientAtTrustRegionCenter.getEntry(i)); } delsq = delta * delta; qred = ZERO; crvmin = MINUS_ONE; // Set the next search direction of the conjugate gradient method. It is // the steepest descent direction initially and when the iterations are // restarted because a variable has just been fixed by a bound, and of // course the components of the fixed variables are zero. ITERMAX is an // upper bound on the indices of the conjugate gradient iterations. int state = 20; for(;;) { switch (state) { case 20: { printState(20); // XXX beta = ZERO; } case 30: { printState(30); // XXX stepsq = ZERO; for (int i = 0; i < n; i++) { if (xbdi.getEntry(i) != ZERO) { s.setEntry(i, ZERO); } else if (beta == ZERO) { s.setEntry(i, -gnew.getEntry(i)); } else { s.setEntry(i, beta * s.getEntry(i) - gnew.getEntry(i)); } // Computing 2nd power final double d1 = s.getEntry(i); stepsq += d1 * d1; } if (stepsq == ZERO) { state = 190; break; } if (beta == ZERO) { gredsq = stepsq; itermax = iterc + n - nact; } if (gredsq * delsq <= qred * 1e-4 * qred) { state = 190; break; } // Multiply the search direction by the second derivative matrix of Q and // calculate some scalars for the choice of steplength. Then set BLEN to // the length of the the step to the trust region boundary and STPLEN to // the steplength, ignoring the simple bounds. state = 210; break; } case 50: { printState(50); // XXX resid = delsq; ds = ZERO; shs = ZERO; for (int i = 0; i < n; i++) { if (xbdi.getEntry(i) == ZERO) { // Computing 2nd power final double d1 = trialStepPoint.getEntry(i); resid -= d1 * d1; ds += s.getEntry(i) * trialStepPoint.getEntry(i); shs += s.getEntry(i) * hs.getEntry(i); } } if (resid <= ZERO) { state = 90; break; } temp = Math.sqrt(stepsq * resid + ds * ds); if (ds < ZERO) { blen = (temp - ds) / stepsq; } else { blen = resid / (temp + ds); } stplen = blen; if (shs > ZERO) { // Computing MIN stplen = Math.min(blen, gredsq / shs); } // Reduce STPLEN if necessary in order to preserve the simple bounds, // letting IACT be the index of the new constrained variable. iact = -1; for (int i = 0; i < n; i++) { if (s.getEntry(i) != ZERO) { xsum = trustRegionCenterOffset.getEntry(i) + trialStepPoint.getEntry(i); if (s.getEntry(i) > ZERO) { temp = (upperDifference.getEntry(i) - xsum) / s.getEntry(i); } else { temp = (lowerDifference.getEntry(i) - xsum) / s.getEntry(i); } if (temp < stplen) { stplen = temp; iact = i; } } } // Update CRVMIN, GNEW and D. Set SDEC to the decrease that occurs in Q. sdec = ZERO; if (stplen > ZERO) { ++iterc; temp = shs / stepsq; if (iact == -1 && temp > ZERO) { crvmin = Math.min(crvmin,temp); if (crvmin == MINUS_ONE) { crvmin = temp; } } ggsav = gredsq; gredsq = ZERO; for (int i = 0; i < n; i++) { gnew.setEntry(i, gnew.getEntry(i) + stplen * hs.getEntry(i)); if (xbdi.getEntry(i) == ZERO) { // Computing 2nd power final double d1 = gnew.getEntry(i); gredsq += d1 * d1; } trialStepPoint.setEntry(i, trialStepPoint.getEntry(i) + stplen * s.getEntry(i)); } // Computing MAX final double d1 = stplen * (ggsav - HALF * stplen * shs); sdec = Math.max(d1, ZERO); qred += sdec; } // Restart the conjugate gradient method if it has hit a new bound. if (iact >= 0) { ++nact; xbdi.setEntry(iact, ONE); if (s.getEntry(iact) < ZERO) { xbdi.setEntry(iact, MINUS_ONE); } // Computing 2nd power final double d1 = trialStepPoint.getEntry(iact); delsq -= d1 * d1; if (delsq <= ZERO) { state = 190; break; } state = 20; break; } // If STPLEN is less than BLEN, then either apply another conjugate // gradient iteration or RETURN. if (stplen < blen) { if (iterc == itermax) { state = 190; break; } if (sdec <= qred * .01) { state = 190; break; } beta = gredsq / ggsav; state = 30; break; } } case 90: { printState(90); // XXX crvmin = ZERO; // Prepare for the alternative iteration by calculating some scalars // and by multiplying the reduced D by the second derivative matrix of // Q, where S holds the reduced D in the call of GGMULT. } case 100: { printState(100); // XXX if (nact >= n - 1) { state = 190; break; } dredsq = ZERO; dredg = ZERO; gredsq = ZERO; for (int i = 0; i < n; i++) { if (xbdi.getEntry(i) == ZERO) { // Computing 2nd power double d1 = trialStepPoint.getEntry(i); dredsq += d1 * d1; dredg += trialStepPoint.getEntry(i) * gnew.getEntry(i); // Computing 2nd power d1 = gnew.getEntry(i); gredsq += d1 * d1; s.setEntry(i, trialStepPoint.getEntry(i)); } else { s.setEntry(i, ZERO); } } itcsav = iterc; state = 210; break; // Let the search direction S be a linear combination of the reduced D // and the reduced G that is orthogonal to the reduced D. } case 120: { printState(120); // XXX ++iterc; temp = gredsq * dredsq - dredg * dredg; if (temp <= qred * 1e-4 * qred) { state = 190; break; } temp = Math.sqrt(temp); for (int i = 0; i < n; i++) { if (xbdi.getEntry(i) == ZERO) { s.setEntry(i, (dredg * trialStepPoint.getEntry(i) - dredsq * gnew.getEntry(i)) / temp); } else { s.setEntry(i, ZERO); } } sredg = -temp; // By considering the simple bounds on the variables, calculate an upper // bound on the tangent of half the angle of the alternative iteration, // namely ANGBD, except that, if already a free variable has reached a // bound, there is a branch back to label 100 after fixing that variable. angbd = ONE; iact = -1; for (int i = 0; i < n; i++) { if (xbdi.getEntry(i) == ZERO) { tempa = trustRegionCenterOffset.getEntry(i) + trialStepPoint.getEntry(i) - lowerDifference.getEntry(i); tempb = upperDifference.getEntry(i) - trustRegionCenterOffset.getEntry(i) - trialStepPoint.getEntry(i); if (tempa <= ZERO) { ++nact; xbdi.setEntry(i, MINUS_ONE); state = 100; break; } else if (tempb <= ZERO) { ++nact; xbdi.setEntry(i, ONE); state = 100; break; } // Computing 2nd power double d1 = trialStepPoint.getEntry(i); // Computing 2nd power double d2 = s.getEntry(i); ssq = d1 * d1 + d2 * d2; // Computing 2nd power d1 = trustRegionCenterOffset.getEntry(i) - lowerDifference.getEntry(i); temp = ssq - d1 * d1; if (temp > ZERO) { temp = Math.sqrt(temp) - s.getEntry(i); if (angbd * temp > tempa) { angbd = tempa / temp; iact = i; xsav = MINUS_ONE; } } // Computing 2nd power d1 = upperDifference.getEntry(i) - trustRegionCenterOffset.getEntry(i); temp = ssq - d1 * d1; if (temp > ZERO) { temp = Math.sqrt(temp) + s.getEntry(i); if (angbd * temp > tempb) { angbd = tempb / temp; iact = i; xsav = ONE; } } } } // Calculate HHD and some curvatures for the alternative iteration. state = 210; break; } case 150: { printState(150); // XXX shs = ZERO; dhs = ZERO; dhd = ZERO; for (int i = 0; i < n; i++) { if (xbdi.getEntry(i) == ZERO) { shs += s.getEntry(i) * hs.getEntry(i); dhs += trialStepPoint.getEntry(i) * hs.getEntry(i); dhd += trialStepPoint.getEntry(i) * hred.getEntry(i); } } // Seek the greatest reduction in Q for a range of equally spaced values // of ANGT in [0,ANGBD], where ANGT is the tangent of half the angle of // the alternative iteration. redmax = ZERO; isav = -1; redsav = ZERO; iu = (int) (angbd * 17. + 3.1); for (int i = 0; i < iu; i++) { angt = angbd * i / iu; sth = (angt + angt) / (ONE + angt * angt); temp = shs + angt * (angt * dhd - dhs - dhs); rednew = sth * (angt * dredg - sredg - HALF * sth * temp); if (rednew > redmax) { redmax = rednew; isav = i; rdprev = redsav; } else if (i == isav + 1) { rdnext = rednew; } redsav = rednew; } // Return if the reduction is zero. Otherwise, set the sine and cosine // of the angle of the alternative iteration, and calculate SDEC. if (isav < 0) { state = 190; break; } if (isav < iu) { temp = (rdnext - rdprev) / (redmax + redmax - rdprev - rdnext); angt = angbd * (isav + HALF * temp) / iu; } cth = (ONE - angt * angt) / (ONE + angt * angt); sth = (angt + angt) / (ONE + angt * angt); temp = shs + angt * (angt * dhd - dhs - dhs); sdec = sth * (angt * dredg - sredg - HALF * sth * temp); if (sdec <= ZERO) { state = 190; break; } // Update GNEW, D and HRED. If the angle of the alternative iteration // is restricted by a bound on a free variable, that variable is fixed // at the bound. dredg = ZERO; gredsq = ZERO; for (int i = 0; i < n; i++) { gnew.setEntry(i, gnew.getEntry(i) + (cth - ONE) * hred.getEntry(i) + sth * hs.getEntry(i)); if (xbdi.getEntry(i) == ZERO) { trialStepPoint.setEntry(i, cth * trialStepPoint.getEntry(i) + sth * s.getEntry(i)); dredg += trialStepPoint.getEntry(i) * gnew.getEntry(i); // Computing 2nd power final double d1 = gnew.getEntry(i); gredsq += d1 * d1; } hred.setEntry(i, cth * hred.getEntry(i) + sth * hs.getEntry(i)); } qred += sdec; if (iact >= 0 && isav == iu) { ++nact; xbdi.setEntry(iact, xsav); state = 100; break; } // If SDEC is sufficiently small, then RETURN after setting XNEW to // XOPT+D, giving careful attention to the bounds. if (sdec > qred * .01) { state = 120; break; } } case 190: { printState(190); // XXX dsq = ZERO; for (int i = 0; i < n; i++) { // Computing MAX // Computing MIN final double min = Math.min(trustRegionCenterOffset.getEntry(i) + trialStepPoint.getEntry(i), upperDifference.getEntry(i)); newPoint.setEntry(i, Math.max(min, lowerDifference.getEntry(i))); if (xbdi.getEntry(i) == MINUS_ONE) { newPoint.setEntry(i, lowerDifference.getEntry(i)); } if (xbdi.getEntry(i) == ONE) { newPoint.setEntry(i, upperDifference.getEntry(i)); } trialStepPoint.setEntry(i, newPoint.getEntry(i) - trustRegionCenterOffset.getEntry(i)); // Computing 2nd power final double d1 = trialStepPoint.getEntry(i); dsq += d1 * d1; } return new double[] { dsq, crvmin }; // The following instructions multiply the current S-vector by the second // derivative matrix of the quadratic model, putting the product in HS. // They are reached from three different parts of the software above and // they can be regarded as an external subroutine. } case 210: { printState(210); // XXX int ih = 0; for (int j = 0; j < n; j++) { hs.setEntry(j, ZERO); for (int i = 0; i <= j; i++) { if (i < j) { hs.setEntry(j, hs.getEntry(j) + modelSecondDerivativesValues.getEntry(ih) * s.getEntry(i)); } hs.setEntry(i, hs.getEntry(i) + modelSecondDerivativesValues.getEntry(ih) * s.getEntry(j)); ih++; } } final RealVector tmp = interpolationPoints.operate(s).ebeMultiply(modelSecondDerivativesParameters); for (int k = 0; k < npt; k++) { if (modelSecondDerivativesParameters.getEntry(k) != ZERO) { for (int i = 0; i < n; i++) { hs.setEntry(i, hs.getEntry(i) + tmp.getEntry(k) * interpolationPoints.getEntry(k, i)); } } } if (crvmin != ZERO) { state = 50; break; } if (iterc > itcsav) { state = 150; break; } for (int i = 0; i < n; i++) { hred.setEntry(i, hs.getEntry(i)); } state = 120; break; } default: { throw new MathIllegalStateException(LocalizedFormats.SIMPLE_MESSAGE, "trsbox"); }} } } // trsbox // ---------------------------------------------------------------------------------------- /** * The arrays BMAT and ZMAT are updated, as required by the new position * of the interpolation point that has the index KNEW. The vector VLAG has * N+NPT components, set on entry to the first NPT and last N components * of the product Hw in equation (4.11) of the Powell (2006) paper on * NEWUOA. Further, BETA is set on entry to the value of the parameter * with that name, and DENOM is set to the denominator of the updating * formula. Elements of ZMAT may be treated as zero if their moduli are * at most ZTEST. The first NDIM elements of W are used for working space. * @param beta * @param denom * @param knew */ private void update( double beta, double denom, int knew ) { printMethod(); // XXX final int n = currentBest.getDimension(); final int npt = numberOfInterpolationPoints; final int nptm = npt - n - 1; // XXX Should probably be split into two arrays. final ArrayRealVector work = new ArrayRealVector(npt + n); double ztest = ZERO; for (int k = 0; k < npt; k++) { for (int j = 0; j < nptm; j++) { // Computing MAX ztest = Math.max(ztest, Math.abs(zMatrix.getEntry(k, j))); } } ztest *= 1e-20; // Apply the rotations that put zeros in the KNEW-th row of ZMAT. for (int j = 1; j < nptm; j++) { final double d1 = zMatrix.getEntry(knew, j); if (Math.abs(d1) > ztest) { // Computing 2nd power final double d2 = zMatrix.getEntry(knew, 0); // Computing 2nd power final double d3 = zMatrix.getEntry(knew, j); final double d4 = Math.sqrt(d2 * d2 + d3 * d3); final double d5 = zMatrix.getEntry(knew, 0) / d4; final double d6 = zMatrix.getEntry(knew, j) / d4; for (int i = 0; i < npt; i++) { final double d7 = d5 * zMatrix.getEntry(i, 0) + d6 * zMatrix.getEntry(i, j); zMatrix.setEntry(i, j, d5 * zMatrix.getEntry(i, j) - d6 * zMatrix.getEntry(i, 0)); zMatrix.setEntry(i, 0, d7); } } zMatrix.setEntry(knew, j, ZERO); } // Put the first NPT components of the KNEW-th column of HLAG into W, // and calculate the parameters of the updating formula. for (int i = 0; i < npt; i++) { work.setEntry(i, zMatrix.getEntry(knew, 0) * zMatrix.getEntry(i, 0)); } final double alpha = work.getEntry(knew); final double tau = lagrangeValuesAtNewPoint.getEntry(knew); lagrangeValuesAtNewPoint.setEntry(knew, lagrangeValuesAtNewPoint.getEntry(knew) - ONE); // Complete the updating of ZMAT. final double sqrtDenom = Math.sqrt(denom); final double d1 = tau / sqrtDenom; final double d2 = zMatrix.getEntry(knew, 0) / sqrtDenom; for (int i = 0; i < npt; i++) { zMatrix.setEntry(i, 0, d1 * zMatrix.getEntry(i, 0) - d2 * lagrangeValuesAtNewPoint.getEntry(i)); } // Finally, update the matrix BMAT. for (int j = 0; j < n; j++) { final int jp = npt + j; work.setEntry(jp, bMatrix.getEntry(knew, j)); final double d3 = (alpha * lagrangeValuesAtNewPoint.getEntry(jp) - tau * work.getEntry(jp)) / denom; final double d4 = (-beta * work.getEntry(jp) - tau * lagrangeValuesAtNewPoint.getEntry(jp)) / denom; for (int i = 0; i <= jp; i++) { bMatrix.setEntry(i, j, bMatrix.getEntry(i, j) + d3 * lagrangeValuesAtNewPoint.getEntry(i) + d4 * work.getEntry(i)); if (i >= npt) { bMatrix.setEntry(jp, (i - npt), bMatrix.getEntry(i, j)); } } } } // update /** * Performs validity checks. * * @param lowerBound Lower bounds (constraints) of the objective variables. * @param upperBound Upperer bounds (constraints) of the objective variables. */ private void setup(double[] lowerBound, double[] upperBound) { printMethod(); // XXX double[] init = getStartPoint(); final int dimension = init.length; // Check problem dimension. if (dimension < MINIMUM_PROBLEM_DIMENSION) { throw new NumberIsTooSmallException(dimension, MINIMUM_PROBLEM_DIMENSION, true); } // Check number of interpolation points. final int[] nPointsInterval = { dimension + 2, (dimension + 2) * (dimension + 1) / 2 }; if (numberOfInterpolationPoints < nPointsInterval[0] || numberOfInterpolationPoints > nPointsInterval[1]) { throw new OutOfRangeException(LocalizedFormats.NUMBER_OF_INTERPOLATION_POINTS, numberOfInterpolationPoints, nPointsInterval[0], nPointsInterval[1]); } // Initialize bound differences. boundDifference = new double[dimension]; double requiredMinDiff = 2 * initialTrustRegionRadius; double minDiff = Double.POSITIVE_INFINITY; for (int i = 0; i < dimension; i++) { boundDifference[i] = upperBound[i] - lowerBound[i]; minDiff = Math.min(minDiff, boundDifference[i]); } if (minDiff < requiredMinDiff) { initialTrustRegionRadius = minDiff / 3.0; } // Initialize the data structures used by the "bobyqa" method. bMatrix = new Array2DRowRealMatrix(dimension + numberOfInterpolationPoints, dimension); zMatrix = new Array2DRowRealMatrix(numberOfInterpolationPoints, numberOfInterpolationPoints - dimension - 1); interpolationPoints = new Array2DRowRealMatrix(numberOfInterpolationPoints, dimension); originShift = new ArrayRealVector(dimension); fAtInterpolationPoints = new ArrayRealVector(numberOfInterpolationPoints); trustRegionCenterOffset = new ArrayRealVector(dimension); gradientAtTrustRegionCenter = new ArrayRealVector(dimension); lowerDifference = new ArrayRealVector(dimension); upperDifference = new ArrayRealVector(dimension); modelSecondDerivativesParameters = new ArrayRealVector(numberOfInterpolationPoints); newPoint = new ArrayRealVector(dimension); alternativeNewPoint = new ArrayRealVector(dimension); trialStepPoint = new ArrayRealVector(dimension); lagrangeValuesAtNewPoint = new ArrayRealVector(dimension + numberOfInterpolationPoints); modelSecondDerivativesValues = new ArrayRealVector(dimension * (dimension + 1) / 2); } // XXX utility for figuring out call sequence. private static String caller(int n) { final Throwable t = new Throwable(); final StackTraceElement[] elements = t.getStackTrace(); final StackTraceElement e = elements[n]; return e.getMethodName() + " (at line " + e.getLineNumber() + ")"; } // XXX utility for figuring out call sequence. private static void printState(int s) { // System.out.println(caller(2) + ": state " + s); } // XXX utility for figuring out call sequence. private static void printMethod() { // System.out.println(caller(2)); } /** * Marker for code paths that are not explored with the current unit tests. * If the path becomes explored, it should just be removed from the code. */ private static class PathIsExploredException extends RuntimeException { private static final long serialVersionUID = 745350979634801853L; private static final String PATH_IS_EXPLORED = "If this exception is thrown, just remove it from the code"; PathIsExploredException() { super(PATH_IS_EXPLORED + " " + BOBYQAOptimizer.caller(3)); } } } //CHECKSTYLE: resume all ././@LongLink100644 0 0 147 12126630646 10263 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/optimization/direct/SimplexOptimizer.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/optimization/direct/SimplexOptimizer.ja100644 1750 1750 21333 12126627712 32404 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.optimization.direct; import java.util.Comparator; import org.apache.commons.math3.analysis.MultivariateFunction; import org.apache.commons.math3.exception.NullArgumentException; import org.apache.commons.math3.optimization.GoalType; import org.apache.commons.math3.optimization.ConvergenceChecker; import org.apache.commons.math3.optimization.PointValuePair; import org.apache.commons.math3.optimization.SimpleValueChecker; import org.apache.commons.math3.optimization.MultivariateOptimizer; import org.apache.commons.math3.optimization.OptimizationData; /** * This class implements simplex-based direct search optimization. * *

                          * Direct search methods only use objective function values, they do * not need derivatives and don't either try to compute approximation * of the derivatives. According to a 1996 paper by Margaret H. Wright * (Direct * Search Methods: Once Scorned, Now Respectable), they are used * when either the computation of the derivative is impossible (noisy * functions, unpredictable discontinuities) or difficult (complexity, * computation cost). In the first cases, rather than an optimum, a * not too bad point is desired. In the latter cases, an * optimum is desired but cannot be reasonably found. In all cases * direct search methods can be useful. *

                          *

                          * Simplex-based direct search methods are based on comparison of * the objective function values at the vertices of a simplex (which is a * set of n+1 points in dimension n) that is updated by the algorithms * steps. *

                          *

                          * The {@link #setSimplex(AbstractSimplex) setSimplex} method must * be called prior to calling the {@code optimize} method. *

                          *

                          * Each call to {@link #optimize(int,MultivariateFunction,GoalType,double[]) * optimize} will re-use the start configuration of the current simplex and * move it such that its first vertex is at the provided start point of the * optimization. If the {@code optimize} method is called to solve a different * problem and the number of parameters change, the simplex must be * re-initialized to one with the appropriate dimensions. *

                          *

                          * Convergence is checked by providing the worst points of * previous and current simplex to the convergence checker, not the best * ones. *

                          *

                          * This simplex optimizer implementation does not directly support constrained * optimization with simple bounds, so for such optimizations, either a more * dedicated method must be used like {@link CMAESOptimizer} or {@link * BOBYQAOptimizer}, or the optimized method must be wrapped in an adapter like * {@link MultivariateFunctionMappingAdapter} or {@link * MultivariateFunctionPenaltyAdapter}. *

                          * * @see AbstractSimplex * @see MultivariateFunctionMappingAdapter * @see MultivariateFunctionPenaltyAdapter * @see CMAESOptimizer * @see BOBYQAOptimizer * @version $Id: SimplexOptimizer.java 1422230 2012-12-15 12:11:13Z erans $ * @deprecated As of 3.1 (to be removed in 4.0). * @since 3.0 */ @Deprecated public class SimplexOptimizer extends BaseAbstractMultivariateOptimizer implements MultivariateOptimizer { /** Simplex. */ private AbstractSimplex simplex; /** * Constructor using a default {@link SimpleValueChecker convergence * checker}. * @deprecated See {@link SimpleValueChecker#SimpleValueChecker()} */ @Deprecated public SimplexOptimizer() { this(new SimpleValueChecker()); } /** * @param checker Convergence checker. */ public SimplexOptimizer(ConvergenceChecker checker) { super(checker); } /** * @param rel Relative threshold. * @param abs Absolute threshold. */ public SimplexOptimizer(double rel, double abs) { this(new SimpleValueChecker(rel, abs)); } /** * Set the simplex algorithm. * * @param simplex Simplex. * @deprecated As of 3.1. The initial simplex can now be passed as an * argument of the {@link #optimize(int,MultivariateFunction,GoalType,OptimizationData[])} * method. */ @Deprecated public void setSimplex(AbstractSimplex simplex) { parseOptimizationData(simplex); } /** * Optimize an objective function. * * @param maxEval Allowed number of evaluations of the objective function. * @param f Objective function. * @param goalType Optimization type. * @param optData Optimization data. The following data will be looked for: *
                            *
                          • {@link org.apache.commons.math3.optimization.InitialGuess InitialGuess}
                          • *
                          • {@link AbstractSimplex}
                          • *
                          * @return the point/value pair giving the optimal value for objective * function. */ @Override protected PointValuePair optimizeInternal(int maxEval, MultivariateFunction f, GoalType goalType, OptimizationData... optData) { // Scan "optData" for the input specific to this optimizer. parseOptimizationData(optData); // The parent's method will retrieve the common parameters from // "optData" and call "doOptimize". return super.optimizeInternal(maxEval, f, goalType, optData); } /** * Scans the list of (required and optional) optimization data that * characterize the problem. * * @param optData Optimization data. The following data will be looked for: *
                            *
                          • {@link AbstractSimplex}
                          • *
                          */ private void parseOptimizationData(OptimizationData... optData) { // The existing values (as set by the previous call) are reused if // not provided in the argument list. for (OptimizationData data : optData) { if (data instanceof AbstractSimplex) { simplex = (AbstractSimplex) data; continue; } } } /** {@inheritDoc} */ @Override protected PointValuePair doOptimize() { if (simplex == null) { throw new NullArgumentException(); } // Indirect call to "computeObjectiveValue" in order to update the // evaluations counter. final MultivariateFunction evalFunc = new MultivariateFunction() { public double value(double[] point) { return computeObjectiveValue(point); } }; final boolean isMinim = getGoalType() == GoalType.MINIMIZE; final Comparator comparator = new Comparator() { public int compare(final PointValuePair o1, final PointValuePair o2) { final double v1 = o1.getValue(); final double v2 = o2.getValue(); return isMinim ? Double.compare(v1, v2) : Double.compare(v2, v1); } }; // Initialize search. simplex.build(getStartPoint()); simplex.evaluate(evalFunc, comparator); PointValuePair[] previous = null; int iteration = 0; final ConvergenceChecker checker = getConvergenceChecker(); while (true) { if (iteration > 0) { boolean converged = true; for (int i = 0; i < simplex.getSize(); i++) { PointValuePair prev = previous[i]; converged = converged && checker.converged(iteration, prev, simplex.getPoint(i)); } if (converged) { // We have found an optimum. return simplex.getPoint(0); } } // We still need to search. previous = simplex.getPoints(); simplex.iterate(evalFunc, comparator); ++iteration; } } } ././@LongLink100644 0 0 171 12126630646 10260 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/optimization/direct/MultivariateFunctionMappingAdapter.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/optimization/direct/MultivariateFunctio100644 1750 1750 25533 12126627712 32473 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.optimization.direct; import org.apache.commons.math3.analysis.MultivariateFunction; import org.apache.commons.math3.analysis.UnivariateFunction; import org.apache.commons.math3.analysis.function.Logit; import org.apache.commons.math3.analysis.function.Sigmoid; import org.apache.commons.math3.exception.DimensionMismatchException; import org.apache.commons.math3.exception.NumberIsTooSmallException; import org.apache.commons.math3.util.FastMath; import org.apache.commons.math3.util.MathUtils; /** *

                          Adapter for mapping bounded {@link MultivariateFunction} to unbounded ones.

                          * *

                          * This adapter can be used to wrap functions subject to simple bounds on * parameters so they can be used by optimizers that do not directly * support simple bounds. *

                          *

                          * The principle is that the user function that will be wrapped will see its * parameters bounded as required, i.e when its {@code value} method is called * with argument array {@code point}, the elements array will fulfill requirement * {@code lower[i] <= point[i] <= upper[i]} for all i. Some of the components * may be unbounded or bounded only on one side if the corresponding bound is * set to an infinite value. The optimizer will not manage the user function by * itself, but it will handle this adapter and it is this adapter that will take * care the bounds are fulfilled. The adapter {@link #value(double[])} method will * be called by the optimizer with unbound parameters, and the adapter will map * the unbounded value to the bounded range using appropriate functions like * {@link Sigmoid} for double bounded elements for example. *

                          *

                          * As the optimizer sees only unbounded parameters, it should be noted that the * start point or simplex expected by the optimizer should be unbounded, so the * user is responsible for converting his bounded point to unbounded by calling * {@link #boundedToUnbounded(double[])} before providing them to the optimizer. * For the same reason, the point returned by the {@link * org.apache.commons.math3.optimization.BaseMultivariateOptimizer#optimize(int, * MultivariateFunction, org.apache.commons.math3.optimization.GoalType, double[])} * method is unbounded. So to convert this point to bounded, users must call * {@link #unboundedToBounded(double[])} by themselves!

                          *

                          * This adapter is only a poor man solution to simple bounds optimization constraints * that can be used with simple optimizers like {@link SimplexOptimizer} with {@link * NelderMeadSimplex} or {@link MultiDirectionalSimplex}. A better solution is to use * an optimizer that directly supports simple bounds like {@link CMAESOptimizer} or * {@link BOBYQAOptimizer}. One caveat of this poor man solution is that behavior near * the bounds may be numerically unstable as bounds are mapped from infinite values. * Another caveat is that convergence values are evaluated by the optimizer with respect * to unbounded variables, so there will be scales differences when converted to bounded * variables. *

                          * * @see MultivariateFunctionPenaltyAdapter * * @version $Id: MultivariateFunctionMappingAdapter.java 1422230 2012-12-15 12:11:13Z erans $ * @deprecated As of 3.1 (to be removed in 4.0). * @since 3.0 */ @Deprecated public class MultivariateFunctionMappingAdapter implements MultivariateFunction { /** Underlying bounded function. */ private final MultivariateFunction bounded; /** Mapping functions. */ private final Mapper[] mappers; /** Simple constructor. * @param bounded bounded function * @param lower lower bounds for each element of the input parameters array * (some elements may be set to {@code Double.NEGATIVE_INFINITY} for * unbounded values) * @param upper upper bounds for each element of the input parameters array * (some elements may be set to {@code Double.POSITIVE_INFINITY} for * unbounded values) * @exception DimensionMismatchException if lower and upper bounds are not * consistent, either according to dimension or to values */ public MultivariateFunctionMappingAdapter(final MultivariateFunction bounded, final double[] lower, final double[] upper) { // safety checks MathUtils.checkNotNull(lower); MathUtils.checkNotNull(upper); if (lower.length != upper.length) { throw new DimensionMismatchException(lower.length, upper.length); } for (int i = 0; i < lower.length; ++i) { // note the following test is written in such a way it also fails for NaN if (!(upper[i] >= lower[i])) { throw new NumberIsTooSmallException(upper[i], lower[i], true); } } this.bounded = bounded; this.mappers = new Mapper[lower.length]; for (int i = 0; i < mappers.length; ++i) { if (Double.isInfinite(lower[i])) { if (Double.isInfinite(upper[i])) { // element is unbounded, no transformation is needed mappers[i] = new NoBoundsMapper(); } else { // element is simple-bounded on the upper side mappers[i] = new UpperBoundMapper(upper[i]); } } else { if (Double.isInfinite(upper[i])) { // element is simple-bounded on the lower side mappers[i] = new LowerBoundMapper(lower[i]); } else { // element is double-bounded mappers[i] = new LowerUpperBoundMapper(lower[i], upper[i]); } } } } /** Map an array from unbounded to bounded. * @param point unbounded value * @return bounded value */ public double[] unboundedToBounded(double[] point) { // map unbounded input point to bounded point final double[] mapped = new double[mappers.length]; for (int i = 0; i < mappers.length; ++i) { mapped[i] = mappers[i].unboundedToBounded(point[i]); } return mapped; } /** Map an array from bounded to unbounded. * @param point bounded value * @return unbounded value */ public double[] boundedToUnbounded(double[] point) { // map bounded input point to unbounded point final double[] mapped = new double[mappers.length]; for (int i = 0; i < mappers.length; ++i) { mapped[i] = mappers[i].boundedToUnbounded(point[i]); } return mapped; } /** Compute the underlying function value from an unbounded point. *

                          * This method simply bounds the unbounded point using the mappings * set up at construction and calls the underlying function using * the bounded point. *

                          * @param point unbounded value * @return underlying function value * @see #unboundedToBounded(double[]) */ public double value(double[] point) { return bounded.value(unboundedToBounded(point)); } /** Mapping interface. */ private interface Mapper { /** Map a value from unbounded to bounded. * @param y unbounded value * @return bounded value */ double unboundedToBounded(double y); /** Map a value from bounded to unbounded. * @param x bounded value * @return unbounded value */ double boundedToUnbounded(double x); } /** Local class for no bounds mapping. */ private static class NoBoundsMapper implements Mapper { /** Simple constructor. */ public NoBoundsMapper() { } /** {@inheritDoc} */ public double unboundedToBounded(final double y) { return y; } /** {@inheritDoc} */ public double boundedToUnbounded(final double x) { return x; } } /** Local class for lower bounds mapping. */ private static class LowerBoundMapper implements Mapper { /** Low bound. */ private final double lower; /** Simple constructor. * @param lower lower bound */ public LowerBoundMapper(final double lower) { this.lower = lower; } /** {@inheritDoc} */ public double unboundedToBounded(final double y) { return lower + FastMath.exp(y); } /** {@inheritDoc} */ public double boundedToUnbounded(final double x) { return FastMath.log(x - lower); } } /** Local class for upper bounds mapping. */ private static class UpperBoundMapper implements Mapper { /** Upper bound. */ private final double upper; /** Simple constructor. * @param upper upper bound */ public UpperBoundMapper(final double upper) { this.upper = upper; } /** {@inheritDoc} */ public double unboundedToBounded(final double y) { return upper - FastMath.exp(-y); } /** {@inheritDoc} */ public double boundedToUnbounded(final double x) { return -FastMath.log(upper - x); } } /** Local class for lower and bounds mapping. */ private static class LowerUpperBoundMapper implements Mapper { /** Function from unbounded to bounded. */ private final UnivariateFunction boundingFunction; /** Function from bounded to unbounded. */ private final UnivariateFunction unboundingFunction; /** Simple constructor. * @param lower lower bound * @param upper upper bound */ public LowerUpperBoundMapper(final double lower, final double upper) { boundingFunction = new Sigmoid(lower, upper); unboundingFunction = new Logit(lower, upper); } /** {@inheritDoc} */ public double unboundedToBounded(final double y) { return boundingFunction.value(y); } /** {@inheritDoc} */ public double boundedToUnbounded(final double x) { return unboundingFunction.value(x); } } } ././@LongLink100644 0 0 145 12126630646 10261 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/optimization/direct/CMAESOptimizer.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/optimization/direct/CMAESOptimizer.java100644 1750 1750 157446 12126627712 32201 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.optimization.direct; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import org.apache.commons.math3.analysis.MultivariateFunction; import org.apache.commons.math3.exception.DimensionMismatchException; import org.apache.commons.math3.exception.NotPositiveException; import org.apache.commons.math3.exception.NotStrictlyPositiveException; import org.apache.commons.math3.exception.OutOfRangeException; import org.apache.commons.math3.exception.TooManyEvaluationsException; import org.apache.commons.math3.linear.Array2DRowRealMatrix; import org.apache.commons.math3.linear.EigenDecomposition; import org.apache.commons.math3.linear.MatrixUtils; import org.apache.commons.math3.linear.RealMatrix; import org.apache.commons.math3.optimization.ConvergenceChecker; import org.apache.commons.math3.optimization.OptimizationData; import org.apache.commons.math3.optimization.GoalType; import org.apache.commons.math3.optimization.MultivariateOptimizer; import org.apache.commons.math3.optimization.PointValuePair; import org.apache.commons.math3.optimization.SimpleValueChecker; import org.apache.commons.math3.random.MersenneTwister; import org.apache.commons.math3.random.RandomGenerator; import org.apache.commons.math3.util.MathArrays; /** *

                          An implementation of the active Covariance Matrix Adaptation Evolution Strategy (CMA-ES) * for non-linear, non-convex, non-smooth, global function minimization. * The CMA-Evolution Strategy (CMA-ES) is a reliable stochastic optimization method * which should be applied if derivative-based methods, e.g. quasi-Newton BFGS or * conjugate gradient, fail due to a rugged search landscape (e.g. noise, local * optima, outlier, etc.) of the objective function. Like a * quasi-Newton method, the CMA-ES learns and applies a variable metric * on the underlying search space. Unlike a quasi-Newton method, the * CMA-ES neither estimates nor uses gradients, making it considerably more * reliable in terms of finding a good, or even close to optimal, solution.

                          * *

                          In general, on smooth objective functions the CMA-ES is roughly ten times * slower than BFGS (counting objective function evaluations, no gradients provided). * For up to N=10 variables also the derivative-free simplex * direct search method (Nelder and Mead) can be faster, but it is * far less reliable than CMA-ES.

                          * *

                          The CMA-ES is particularly well suited for non-separable * and/or badly conditioned problems. To observe the advantage of CMA compared * to a conventional evolution strategy, it will usually take about * 30 N function evaluations. On difficult problems the complete * optimization (a single run) is expected to take roughly between * 30 N and 300 N2 * function evaluations.

                          * *

                          This implementation is translated and adapted from the Matlab version * of the CMA-ES algorithm as implemented in module {@code cmaes.m} version 3.51.

                          * * For more information, please refer to the following links: * * * @version $Id: CMAESOptimizer.java 1462503 2013-03-29 15:48:27Z luc $ * @deprecated As of 3.1 (to be removed in 4.0). * @since 3.0 */ @Deprecated public class CMAESOptimizer extends BaseAbstractMultivariateSimpleBoundsOptimizer implements MultivariateOptimizer { /** Default value for {@link #checkFeasableCount}: {@value}. */ public static final int DEFAULT_CHECKFEASABLECOUNT = 0; /** Default value for {@link #stopFitness}: {@value}. */ public static final double DEFAULT_STOPFITNESS = 0; /** Default value for {@link #isActiveCMA}: {@value}. */ public static final boolean DEFAULT_ISACTIVECMA = true; /** Default value for {@link #maxIterations}: {@value}. */ public static final int DEFAULT_MAXITERATIONS = 30000; /** Default value for {@link #diagonalOnly}: {@value}. */ public static final int DEFAULT_DIAGONALONLY = 0; /** Default value for {@link #random}. */ public static final RandomGenerator DEFAULT_RANDOMGENERATOR = new MersenneTwister(); // global search parameters /** * Population size, offspring number. The primary strategy parameter to play * with, which can be increased from its default value. Increasing the * population size improves global search properties in exchange to speed. * Speed decreases, as a rule, at most linearly with increasing population * size. It is advisable to begin with the default small population size. */ private int lambda; // population size /** * Covariance update mechanism, default is active CMA. isActiveCMA = true * turns on "active CMA" with a negative update of the covariance matrix and * checks for positive definiteness. OPTS.CMA.active = 2 does not check for * pos. def. and is numerically faster. Active CMA usually speeds up the * adaptation. */ private boolean isActiveCMA; /** * Determines how often a new random offspring is generated in case it is * not feasible / beyond the defined limits, default is 0. */ private int checkFeasableCount; /** * @see Sigma */ private double[] inputSigma; /** Number of objective variables/problem dimension */ private int dimension; /** * Defines the number of initial iterations, where the covariance matrix * remains diagonal and the algorithm has internally linear time complexity. * diagonalOnly = 1 means keeping the covariance matrix always diagonal and * this setting also exhibits linear space complexity. This can be * particularly useful for dimension > 100. * @see A Simple Modification in CMA-ES */ private int diagonalOnly = 0; /** Number of objective variables/problem dimension */ private boolean isMinimize = true; /** Indicates whether statistic data is collected. */ private boolean generateStatistics = false; // termination criteria /** Maximal number of iterations allowed. */ private int maxIterations; /** Limit for fitness value. */ private double stopFitness; /** Stop if x-changes larger stopTolUpX. */ private double stopTolUpX; /** Stop if x-change smaller stopTolX. */ private double stopTolX; /** Stop if fun-changes smaller stopTolFun. */ private double stopTolFun; /** Stop if back fun-changes smaller stopTolHistFun. */ private double stopTolHistFun; // selection strategy parameters /** Number of parents/points for recombination. */ private int mu; // /** log(mu + 0.5), stored for efficiency. */ private double logMu2; /** Array for weighted recombination. */ private RealMatrix weights; /** Variance-effectiveness of sum w_i x_i. */ private double mueff; // // dynamic strategy parameters and constants /** Overall standard deviation - search volume. */ private double sigma; /** Cumulation constant. */ private double cc; /** Cumulation constant for step-size. */ private double cs; /** Damping for step-size. */ private double damps; /** Learning rate for rank-one update. */ private double ccov1; /** Learning rate for rank-mu update' */ private double ccovmu; /** Expectation of ||N(0,I)|| == norm(randn(N,1)). */ private double chiN; /** Learning rate for rank-one update - diagonalOnly */ private double ccov1Sep; /** Learning rate for rank-mu update - diagonalOnly */ private double ccovmuSep; // CMA internal values - updated each generation /** Objective variables. */ private RealMatrix xmean; /** Evolution path. */ private RealMatrix pc; /** Evolution path for sigma. */ private RealMatrix ps; /** Norm of ps, stored for efficiency. */ private double normps; /** Coordinate system. */ private RealMatrix B; /** Scaling. */ private RealMatrix D; /** B*D, stored for efficiency. */ private RealMatrix BD; /** Diagonal of sqrt(D), stored for efficiency. */ private RealMatrix diagD; /** Covariance matrix. */ private RealMatrix C; /** Diagonal of C, used for diagonalOnly. */ private RealMatrix diagC; /** Number of iterations already performed. */ private int iterations; /** History queue of best values. */ private double[] fitnessHistory; /** Size of history queue of best values. */ private int historySize; /** Random generator. */ private RandomGenerator random; /** History of sigma values. */ private List statisticsSigmaHistory = new ArrayList(); /** History of mean matrix. */ private List statisticsMeanHistory = new ArrayList(); /** History of fitness values. */ private List statisticsFitnessHistory = new ArrayList(); /** History of D matrix. */ private List statisticsDHistory = new ArrayList(); /** * Default constructor, uses default parameters * * @deprecated As of version 3.1: Parameter {@code lambda} must be * passed with the call to {@link #optimize(int,MultivariateFunction,GoalType,OptimizationData[]) * optimize} (whereas in the current code it is set to an undocumented value). */ public CMAESOptimizer() { this(0); } /** * @param lambda Population size. * @deprecated As of version 3.1: Parameter {@code lambda} must be * passed with the call to {@link #optimize(int,MultivariateFunction,GoalType,OptimizationData[]) * optimize} (whereas in the current code it is set to an undocumented value).. */ public CMAESOptimizer(int lambda) { this(lambda, null, DEFAULT_MAXITERATIONS, DEFAULT_STOPFITNESS, DEFAULT_ISACTIVECMA, DEFAULT_DIAGONALONLY, DEFAULT_CHECKFEASABLECOUNT, DEFAULT_RANDOMGENERATOR, false, null); } /** * @param lambda Population size. * @param inputSigma Initial standard deviations to sample new points * around the initial guess. * @deprecated As of version 3.1: Parameters {@code lambda} and {@code inputSigma} must be * passed with the call to {@link #optimize(int,MultivariateFunction,GoalType,OptimizationData[]) * optimize}. */ @Deprecated public CMAESOptimizer(int lambda, double[] inputSigma) { this(lambda, inputSigma, DEFAULT_MAXITERATIONS, DEFAULT_STOPFITNESS, DEFAULT_ISACTIVECMA, DEFAULT_DIAGONALONLY, DEFAULT_CHECKFEASABLECOUNT, DEFAULT_RANDOMGENERATOR, false); } /** * @param lambda Population size. * @param inputSigma Initial standard deviations to sample new points * around the initial guess. * @param maxIterations Maximal number of iterations. * @param stopFitness Whether to stop if objective function value is smaller than * {@code stopFitness}. * @param isActiveCMA Chooses the covariance matrix update method. * @param diagonalOnly Number of initial iterations, where the covariance matrix * remains diagonal. * @param checkFeasableCount Determines how often new random objective variables are * generated in case they are out of bounds. * @param random Random generator. * @param generateStatistics Whether statistic data is collected. * @deprecated See {@link SimpleValueChecker#SimpleValueChecker()} */ @Deprecated public CMAESOptimizer(int lambda, double[] inputSigma, int maxIterations, double stopFitness, boolean isActiveCMA, int diagonalOnly, int checkFeasableCount, RandomGenerator random, boolean generateStatistics) { this(lambda, inputSigma, maxIterations, stopFitness, isActiveCMA, diagonalOnly, checkFeasableCount, random, generateStatistics, new SimpleValueChecker()); } /** * @param lambda Population size. * @param inputSigma Initial standard deviations to sample new points * around the initial guess. * @param maxIterations Maximal number of iterations. * @param stopFitness Whether to stop if objective function value is smaller than * {@code stopFitness}. * @param isActiveCMA Chooses the covariance matrix update method. * @param diagonalOnly Number of initial iterations, where the covariance matrix * remains diagonal. * @param checkFeasableCount Determines how often new random objective variables are * generated in case they are out of bounds. * @param random Random generator. * @param generateStatistics Whether statistic data is collected. * @param checker Convergence checker. * @deprecated As of version 3.1: Parameters {@code lambda} and {@code inputSigma} must be * passed with the call to {@link #optimize(int,MultivariateFunction,GoalType,OptimizationData[]) * optimize}. */ @Deprecated public CMAESOptimizer(int lambda, double[] inputSigma, int maxIterations, double stopFitness, boolean isActiveCMA, int diagonalOnly, int checkFeasableCount, RandomGenerator random, boolean generateStatistics, ConvergenceChecker checker) { super(checker); this.lambda = lambda; this.inputSigma = inputSigma == null ? null : (double[]) inputSigma.clone(); this.maxIterations = maxIterations; this.stopFitness = stopFitness; this.isActiveCMA = isActiveCMA; this.diagonalOnly = diagonalOnly; this.checkFeasableCount = checkFeasableCount; this.random = random; this.generateStatistics = generateStatistics; } /** * @param maxIterations Maximal number of iterations. * @param stopFitness Whether to stop if objective function value is smaller than * {@code stopFitness}. * @param isActiveCMA Chooses the covariance matrix update method. * @param diagonalOnly Number of initial iterations, where the covariance matrix * remains diagonal. * @param checkFeasableCount Determines how often new random objective variables are * generated in case they are out of bounds. * @param random Random generator. * @param generateStatistics Whether statistic data is collected. * @param checker Convergence checker. * * @since 3.1 */ public CMAESOptimizer(int maxIterations, double stopFitness, boolean isActiveCMA, int diagonalOnly, int checkFeasableCount, RandomGenerator random, boolean generateStatistics, ConvergenceChecker checker) { super(checker); this.maxIterations = maxIterations; this.stopFitness = stopFitness; this.isActiveCMA = isActiveCMA; this.diagonalOnly = diagonalOnly; this.checkFeasableCount = checkFeasableCount; this.random = random; this.generateStatistics = generateStatistics; } /** * @return History of sigma values. */ public List getStatisticsSigmaHistory() { return statisticsSigmaHistory; } /** * @return History of mean matrix. */ public List getStatisticsMeanHistory() { return statisticsMeanHistory; } /** * @return History of fitness values. */ public List getStatisticsFitnessHistory() { return statisticsFitnessHistory; } /** * @return History of D matrix. */ public List getStatisticsDHistory() { return statisticsDHistory; } /** * Input sigma values. * They define the initial coordinate-wise standard deviations for * sampling new search points around the initial guess. * It is suggested to set them to the estimated distance from the * initial to the desired optimum. * Small values induce the search to be more local (and very small * values are more likely to find a local optimum close to the initial * guess). * Too small values might however lead to early termination. * @since 3.1 */ public static class Sigma implements OptimizationData { /** Sigma values. */ private final double[] sigma; /** * @param s Sigma values. * @throws NotPositiveException if any of the array entries is smaller * than zero. */ public Sigma(double[] s) throws NotPositiveException { for (int i = 0; i < s.length; i++) { if (s[i] < 0) { throw new NotPositiveException(s[i]); } } sigma = s.clone(); } /** * @return the sigma values. */ public double[] getSigma() { return sigma.clone(); } } /** * Population size. * The number of offspring is the primary strategy parameter. * In the absence of better clues, a good default could be an * integer close to {@code 4 + 3 ln(n)}, where {@code n} is the * number of optimized parameters. * Increasing the population size improves global search properties * at the expense of speed (which in general decreases at most * linearly with increasing population size). * @since 3.1 */ public static class PopulationSize implements OptimizationData { /** Population size. */ private final int lambda; /** * @param size Population size. * @throws NotStrictlyPositiveException if {@code size <= 0}. */ public PopulationSize(int size) throws NotStrictlyPositiveException { if (size <= 0) { throw new NotStrictlyPositiveException(size); } lambda = size; } /** * @return the population size. */ public int getPopulationSize() { return lambda; } } /** * Optimize an objective function. * * @param maxEval Allowed number of evaluations of the objective function. * @param f Objective function. * @param goalType Optimization type. * @param optData Optimization data. The following data will be looked for: *
                            *
                          • {@link org.apache.commons.math3.optimization.InitialGuess InitialGuess}
                          • *
                          • {@link Sigma}
                          • *
                          • {@link PopulationSize}
                          • *
                          * @return the point/value pair giving the optimal value for objective * function. */ @Override protected PointValuePair optimizeInternal(int maxEval, MultivariateFunction f, GoalType goalType, OptimizationData... optData) { // Scan "optData" for the input specific to this optimizer. parseOptimizationData(optData); // The parent's method will retrieve the common parameters from // "optData" and call "doOptimize". return super.optimizeInternal(maxEval, f, goalType, optData); } /** {@inheritDoc} */ @Override protected PointValuePair doOptimize() { checkParameters(); // -------------------- Initialization -------------------------------- isMinimize = getGoalType().equals(GoalType.MINIMIZE); final FitnessFunction fitfun = new FitnessFunction(); final double[] guess = getStartPoint(); // number of objective variables/problem dimension dimension = guess.length; initializeCMA(guess); iterations = 0; double bestValue = fitfun.value(guess); push(fitnessHistory, bestValue); PointValuePair optimum = new PointValuePair(getStartPoint(), isMinimize ? bestValue : -bestValue); PointValuePair lastResult = null; // -------------------- Generation Loop -------------------------------- generationLoop: for (iterations = 1; iterations <= maxIterations; iterations++) { // Generate and evaluate lambda offspring final RealMatrix arz = randn1(dimension, lambda); final RealMatrix arx = zeros(dimension, lambda); final double[] fitness = new double[lambda]; // generate random offspring for (int k = 0; k < lambda; k++) { RealMatrix arxk = null; for (int i = 0; i < checkFeasableCount + 1; i++) { if (diagonalOnly <= 0) { arxk = xmean.add(BD.multiply(arz.getColumnMatrix(k)) .scalarMultiply(sigma)); // m + sig * Normal(0,C) } else { arxk = xmean.add(times(diagD,arz.getColumnMatrix(k)) .scalarMultiply(sigma)); } if (i >= checkFeasableCount || fitfun.isFeasible(arxk.getColumn(0))) { break; } // regenerate random arguments for row arz.setColumn(k, randn(dimension)); } copyColumn(arxk, 0, arx, k); try { fitness[k] = fitfun.value(arx.getColumn(k)); // compute fitness } catch (TooManyEvaluationsException e) { break generationLoop; } } // Sort by fitness and compute weighted mean into xmean final int[] arindex = sortedIndices(fitness); // Calculate new xmean, this is selection and recombination final RealMatrix xold = xmean; // for speed up of Eq. (2) and (3) final RealMatrix bestArx = selectColumns(arx, MathArrays.copyOf(arindex, mu)); xmean = bestArx.multiply(weights); final RealMatrix bestArz = selectColumns(arz, MathArrays.copyOf(arindex, mu)); final RealMatrix zmean = bestArz.multiply(weights); final boolean hsig = updateEvolutionPaths(zmean, xold); if (diagonalOnly <= 0) { updateCovariance(hsig, bestArx, arz, arindex, xold); } else { updateCovarianceDiagonalOnly(hsig, bestArz); } // Adapt step size sigma - Eq. (5) sigma *= Math.exp(Math.min(1, (normps/chiN - 1) * cs / damps)); final double bestFitness = fitness[arindex[0]]; final double worstFitness = fitness[arindex[arindex.length - 1]]; if (bestValue > bestFitness) { bestValue = bestFitness; lastResult = optimum; optimum = new PointValuePair(fitfun.repair(bestArx.getColumn(0)), isMinimize ? bestFitness : -bestFitness); if (getConvergenceChecker() != null && lastResult != null && getConvergenceChecker().converged(iterations, optimum, lastResult)) { break generationLoop; } } // handle termination criteria // Break, if fitness is good enough if (stopFitness != 0 && bestFitness < (isMinimize ? stopFitness : -stopFitness)) { break generationLoop; } final double[] sqrtDiagC = sqrt(diagC).getColumn(0); final double[] pcCol = pc.getColumn(0); for (int i = 0; i < dimension; i++) { if (sigma * Math.max(Math.abs(pcCol[i]), sqrtDiagC[i]) > stopTolX) { break; } if (i >= dimension - 1) { break generationLoop; } } for (int i = 0; i < dimension; i++) { if (sigma * sqrtDiagC[i] > stopTolUpX) { break generationLoop; } } final double historyBest = min(fitnessHistory); final double historyWorst = max(fitnessHistory); if (iterations > 2 && Math.max(historyWorst, worstFitness) - Math.min(historyBest, bestFitness) < stopTolFun) { break generationLoop; } if (iterations > fitnessHistory.length && historyWorst-historyBest < stopTolHistFun) { break generationLoop; } // condition number of the covariance matrix exceeds 1e14 if (max(diagD)/min(diagD) > 1e7) { break generationLoop; } // user defined termination if (getConvergenceChecker() != null) { final PointValuePair current = new PointValuePair(bestArx.getColumn(0), isMinimize ? bestFitness : -bestFitness); if (lastResult != null && getConvergenceChecker().converged(iterations, current, lastResult)) { break generationLoop; } lastResult = current; } // Adjust step size in case of equal function values (flat fitness) if (bestValue == fitness[arindex[(int)(0.1+lambda/4.)]]) { sigma = sigma * Math.exp(0.2 + cs / damps); } if (iterations > 2 && Math.max(historyWorst, bestFitness) - Math.min(historyBest, bestFitness) == 0) { sigma = sigma * Math.exp(0.2 + cs / damps); } // store best in history push(fitnessHistory,bestFitness); fitfun.setValueRange(worstFitness-bestFitness); if (generateStatistics) { statisticsSigmaHistory.add(sigma); statisticsFitnessHistory.add(bestFitness); statisticsMeanHistory.add(xmean.transpose()); statisticsDHistory.add(diagD.transpose().scalarMultiply(1E5)); } } return optimum; } /** * Scans the list of (required and optional) optimization data that * characterize the problem. * * @param optData Optimization data. The following data will be looked for: *
                            *
                          • {@link Sigma}
                          • *
                          • {@link PopulationSize}
                          • *
                          */ private void parseOptimizationData(OptimizationData... optData) { // The existing values (as set by the previous call) are reused if // not provided in the argument list. for (OptimizationData data : optData) { if (data instanceof Sigma) { inputSigma = ((Sigma) data).getSigma(); continue; } if (data instanceof PopulationSize) { lambda = ((PopulationSize) data).getPopulationSize(); continue; } } } /** * Checks dimensions and values of boundaries and inputSigma if defined. */ private void checkParameters() { final double[] init = getStartPoint(); final double[] lB = getLowerBound(); final double[] uB = getUpperBound(); if (inputSigma != null) { if (inputSigma.length != init.length) { throw new DimensionMismatchException(inputSigma.length, init.length); } for (int i = 0; i < init.length; i++) { if (inputSigma[i] < 0) { // XXX Remove this block in 4.0 (check performed in "Sigma" class). throw new NotPositiveException(inputSigma[i]); } if (inputSigma[i] > uB[i] - lB[i]) { throw new OutOfRangeException(inputSigma[i], 0, uB[i] - lB[i]); } } } } /** * Initialization of the dynamic search parameters * * @param guess Initial guess for the arguments of the fitness function. */ private void initializeCMA(double[] guess) { if (lambda <= 0) { // XXX Line below to replace the current one in 4.0 (MATH-879). // throw new NotStrictlyPositiveException(lambda); lambda = 4 + (int) (3 * Math.log(dimension)); } // initialize sigma final double[][] sigmaArray = new double[guess.length][1]; for (int i = 0; i < guess.length; i++) { // XXX Line below to replace the current one in 4.0 (MATH-868). // sigmaArray[i][0] = inputSigma[i]; sigmaArray[i][0] = inputSigma == null ? 0.3 : inputSigma[i]; } final RealMatrix insigma = new Array2DRowRealMatrix(sigmaArray, false); sigma = max(insigma); // overall standard deviation // initialize termination criteria stopTolUpX = 1e3 * max(insigma); stopTolX = 1e-11 * max(insigma); stopTolFun = 1e-12; stopTolHistFun = 1e-13; // initialize selection strategy parameters mu = lambda / 2; // number of parents/points for recombination logMu2 = Math.log(mu + 0.5); weights = log(sequence(1, mu, 1)).scalarMultiply(-1).scalarAdd(logMu2); double sumw = 0; double sumwq = 0; for (int i = 0; i < mu; i++) { double w = weights.getEntry(i, 0); sumw += w; sumwq += w * w; } weights = weights.scalarMultiply(1 / sumw); mueff = sumw * sumw / sumwq; // variance-effectiveness of sum w_i x_i // initialize dynamic strategy parameters and constants cc = (4 + mueff / dimension) / (dimension + 4 + 2 * mueff / dimension); cs = (mueff + 2) / (dimension + mueff + 3.); damps = (1 + 2 * Math.max(0, Math.sqrt((mueff - 1) / (dimension + 1)) - 1)) * Math.max(0.3, 1 - dimension / (1e-6 + maxIterations)) + cs; // minor increment ccov1 = 2 / ((dimension + 1.3) * (dimension + 1.3) + mueff); ccovmu = Math.min(1 - ccov1, 2 * (mueff - 2 + 1 / mueff) / ((dimension + 2) * (dimension + 2) + mueff)); ccov1Sep = Math.min(1, ccov1 * (dimension + 1.5) / 3); ccovmuSep = Math.min(1 - ccov1, ccovmu * (dimension + 1.5) / 3); chiN = Math.sqrt(dimension) * (1 - 1 / ((double) 4 * dimension) + 1 / ((double) 21 * dimension * dimension)); // intialize CMA internal values - updated each generation xmean = MatrixUtils.createColumnRealMatrix(guess); // objective variables diagD = insigma.scalarMultiply(1 / sigma); diagC = square(diagD); pc = zeros(dimension, 1); // evolution paths for C and sigma ps = zeros(dimension, 1); // B defines the coordinate system normps = ps.getFrobeniusNorm(); B = eye(dimension, dimension); D = ones(dimension, 1); // diagonal D defines the scaling BD = times(B, repmat(diagD.transpose(), dimension, 1)); C = B.multiply(diag(square(D)).multiply(B.transpose())); // covariance historySize = 10 + (int) (3 * 10 * dimension / (double) lambda); fitnessHistory = new double[historySize]; // history of fitness values for (int i = 0; i < historySize; i++) { fitnessHistory[i] = Double.MAX_VALUE; } } /** * Update of the evolution paths ps and pc. * * @param zmean Weighted row matrix of the gaussian random numbers generating * the current offspring. * @param xold xmean matrix of the previous generation. * @return hsig flag indicating a small correction. */ private boolean updateEvolutionPaths(RealMatrix zmean, RealMatrix xold) { ps = ps.scalarMultiply(1 - cs).add( B.multiply(zmean).scalarMultiply( Math.sqrt(cs * (2 - cs) * mueff))); normps = ps.getFrobeniusNorm(); final boolean hsig = normps / Math.sqrt(1 - Math.pow(1 - cs, 2 * iterations)) / chiN < 1.4 + 2 / ((double) dimension + 1); pc = pc.scalarMultiply(1 - cc); if (hsig) { pc = pc.add(xmean.subtract(xold).scalarMultiply(Math.sqrt(cc * (2 - cc) * mueff) / sigma)); } return hsig; } /** * Update of the covariance matrix C for diagonalOnly > 0 * * @param hsig Flag indicating a small correction. * @param bestArz Fitness-sorted matrix of the gaussian random values of the * current offspring. */ private void updateCovarianceDiagonalOnly(boolean hsig, final RealMatrix bestArz) { // minor correction if hsig==false double oldFac = hsig ? 0 : ccov1Sep * cc * (2 - cc); oldFac += 1 - ccov1Sep - ccovmuSep; diagC = diagC.scalarMultiply(oldFac) // regard old matrix .add(square(pc).scalarMultiply(ccov1Sep)) // plus rank one update .add((times(diagC, square(bestArz).multiply(weights))) // plus rank mu update .scalarMultiply(ccovmuSep)); diagD = sqrt(diagC); // replaces eig(C) if (diagonalOnly > 1 && iterations > diagonalOnly) { // full covariance matrix from now on diagonalOnly = 0; B = eye(dimension, dimension); BD = diag(diagD); C = diag(diagC); } } /** * Update of the covariance matrix C. * * @param hsig Flag indicating a small correction. * @param bestArx Fitness-sorted matrix of the argument vectors producing the * current offspring. * @param arz Unsorted matrix containing the gaussian random values of the * current offspring. * @param arindex Indices indicating the fitness-order of the current offspring. * @param xold xmean matrix of the previous generation. */ private void updateCovariance(boolean hsig, final RealMatrix bestArx, final RealMatrix arz, final int[] arindex, final RealMatrix xold) { double negccov = 0; if (ccov1 + ccovmu > 0) { final RealMatrix arpos = bestArx.subtract(repmat(xold, 1, mu)) .scalarMultiply(1 / sigma); // mu difference vectors final RealMatrix roneu = pc.multiply(pc.transpose()) .scalarMultiply(ccov1); // rank one update // minor correction if hsig==false double oldFac = hsig ? 0 : ccov1 * cc * (2 - cc); oldFac += 1 - ccov1 - ccovmu; if (isActiveCMA) { // Adapt covariance matrix C active CMA negccov = (1 - ccovmu) * 0.25 * mueff / (Math.pow(dimension + 2, 1.5) + 2 * mueff); // keep at least 0.66 in all directions, small popsize are most // critical final double negminresidualvariance = 0.66; // where to make up for the variance loss final double negalphaold = 0.5; // prepare vectors, compute negative updating matrix Cneg final int[] arReverseIndex = reverse(arindex); RealMatrix arzneg = selectColumns(arz, MathArrays.copyOf(arReverseIndex, mu)); RealMatrix arnorms = sqrt(sumRows(square(arzneg))); final int[] idxnorms = sortedIndices(arnorms.getRow(0)); final RealMatrix arnormsSorted = selectColumns(arnorms, idxnorms); final int[] idxReverse = reverse(idxnorms); final RealMatrix arnormsReverse = selectColumns(arnorms, idxReverse); arnorms = divide(arnormsReverse, arnormsSorted); final int[] idxInv = inverse(idxnorms); final RealMatrix arnormsInv = selectColumns(arnorms, idxInv); // check and set learning rate negccov final double negcovMax = (1 - negminresidualvariance) / square(arnormsInv).multiply(weights).getEntry(0, 0); if (negccov > negcovMax) { negccov = negcovMax; } arzneg = times(arzneg, repmat(arnormsInv, dimension, 1)); final RealMatrix artmp = BD.multiply(arzneg); final RealMatrix Cneg = artmp.multiply(diag(weights)).multiply(artmp.transpose()); oldFac += negalphaold * negccov; C = C.scalarMultiply(oldFac) .add(roneu) // regard old matrix .add(arpos.scalarMultiply( // plus rank one update ccovmu + (1 - negalphaold) * negccov) // plus rank mu update .multiply(times(repmat(weights, 1, dimension), arpos.transpose()))) .subtract(Cneg.scalarMultiply(negccov)); } else { // Adapt covariance matrix C - nonactive C = C.scalarMultiply(oldFac) // regard old matrix .add(roneu) // plus rank one update .add(arpos.scalarMultiply(ccovmu) // plus rank mu update .multiply(times(repmat(weights, 1, dimension), arpos.transpose()))); } } updateBD(negccov); } /** * Update B and D from C. * * @param negccov Negative covariance factor. */ private void updateBD(double negccov) { if (ccov1 + ccovmu + negccov > 0 && (iterations % 1. / (ccov1 + ccovmu + negccov) / dimension / 10.) < 1) { // to achieve O(N^2) C = triu(C, 0).add(triu(C, 1).transpose()); // enforce symmetry to prevent complex numbers final EigenDecomposition eig = new EigenDecomposition(C); B = eig.getV(); // eigen decomposition, B==normalized eigenvectors D = eig.getD(); diagD = diag(D); if (min(diagD) <= 0) { for (int i = 0; i < dimension; i++) { if (diagD.getEntry(i, 0) < 0) { diagD.setEntry(i, 0, 0); } } final double tfac = max(diagD) / 1e14; C = C.add(eye(dimension, dimension).scalarMultiply(tfac)); diagD = diagD.add(ones(dimension, 1).scalarMultiply(tfac)); } if (max(diagD) > 1e14 * min(diagD)) { final double tfac = max(diagD) / 1e14 - min(diagD); C = C.add(eye(dimension, dimension).scalarMultiply(tfac)); diagD = diagD.add(ones(dimension, 1).scalarMultiply(tfac)); } diagC = diag(C); diagD = sqrt(diagD); // D contains standard deviations now BD = times(B, repmat(diagD.transpose(), dimension, 1)); // O(n^2) } } /** * Pushes the current best fitness value in a history queue. * * @param vals History queue. * @param val Current best fitness value. */ private static void push(double[] vals, double val) { for (int i = vals.length-1; i > 0; i--) { vals[i] = vals[i-1]; } vals[0] = val; } /** * Sorts fitness values. * * @param doubles Array of values to be sorted. * @return a sorted array of indices pointing into doubles. */ private int[] sortedIndices(final double[] doubles) { final DoubleIndex[] dis = new DoubleIndex[doubles.length]; for (int i = 0; i < doubles.length; i++) { dis[i] = new DoubleIndex(doubles[i], i); } Arrays.sort(dis); final int[] indices = new int[doubles.length]; for (int i = 0; i < doubles.length; i++) { indices[i] = dis[i].index; } return indices; } /** * Used to sort fitness values. Sorting is always in lower value first * order. */ private static class DoubleIndex implements Comparable { /** Value to compare. */ private final double value; /** Index into sorted array. */ private final int index; /** * @param value Value to compare. * @param index Index into sorted array. */ DoubleIndex(double value, int index) { this.value = value; this.index = index; } /** {@inheritDoc} */ public int compareTo(DoubleIndex o) { return Double.compare(value, o.value); } /** {@inheritDoc} */ @Override public boolean equals(Object other) { if (this == other) { return true; } if (other instanceof DoubleIndex) { return Double.compare(value, ((DoubleIndex) other).value) == 0; } return false; } /** {@inheritDoc} */ @Override public int hashCode() { long bits = Double.doubleToLongBits(value); return (int) ((1438542 ^ (bits >>> 32) ^ bits) & 0xffffffff); } } /** * Normalizes fitness values to the range [0,1]. Adds a penalty to the * fitness value if out of range. The penalty is adjusted by calling * setValueRange(). */ private class FitnessFunction { /** Determines the penalty for boundary violations */ private double valueRange; /** * Flag indicating whether the objective variables are forced into their * bounds if defined */ private final boolean isRepairMode; /** Simple constructor. */ public FitnessFunction() { valueRange = 1; isRepairMode = true; } /** * @param point Normalized objective variables. * @return the objective value + penalty for violated bounds. */ public double value(final double[] point) { double value; if (isRepairMode) { double[] repaired = repair(point); value = CMAESOptimizer.this.computeObjectiveValue(repaired) + penalty(point, repaired); } else { value = CMAESOptimizer.this.computeObjectiveValue(point); } return isMinimize ? value : -value; } /** * @param x Normalized objective variables. * @return {@code true} if in bounds. */ public boolean isFeasible(final double[] x) { final double[] lB = CMAESOptimizer.this.getLowerBound(); final double[] uB = CMAESOptimizer.this.getUpperBound(); for (int i = 0; i < x.length; i++) { if (x[i] < lB[i]) { return false; } if (x[i] > uB[i]) { return false; } } return true; } /** * @param valueRange Adjusts the penalty computation. */ public void setValueRange(double valueRange) { this.valueRange = valueRange; } /** * @param x Normalized objective variables. * @return the repaired (i.e. all in bounds) objective variables. */ private double[] repair(final double[] x) { final double[] lB = CMAESOptimizer.this.getLowerBound(); final double[] uB = CMAESOptimizer.this.getUpperBound(); final double[] repaired = new double[x.length]; for (int i = 0; i < x.length; i++) { if (x[i] < lB[i]) { repaired[i] = lB[i]; } else if (x[i] > uB[i]) { repaired[i] = uB[i]; } else { repaired[i] = x[i]; } } return repaired; } /** * @param x Normalized objective variables. * @param repaired Repaired objective variables. * @return Penalty value according to the violation of the bounds. */ private double penalty(final double[] x, final double[] repaired) { double penalty = 0; for (int i = 0; i < x.length; i++) { double diff = Math.abs(x[i] - repaired[i]); penalty += diff * valueRange; } return isMinimize ? penalty : -penalty; } } // -----Matrix utility functions similar to the Matlab build in functions------ /** * @param m Input matrix * @return Matrix representing the element-wise logarithm of m. */ private static RealMatrix log(final RealMatrix m) { final double[][] d = new double[m.getRowDimension()][m.getColumnDimension()]; for (int r = 0; r < m.getRowDimension(); r++) { for (int c = 0; c < m.getColumnDimension(); c++) { d[r][c] = Math.log(m.getEntry(r, c)); } } return new Array2DRowRealMatrix(d, false); } /** * @param m Input matrix. * @return Matrix representing the element-wise square root of m. */ private static RealMatrix sqrt(final RealMatrix m) { final double[][] d = new double[m.getRowDimension()][m.getColumnDimension()]; for (int r = 0; r < m.getRowDimension(); r++) { for (int c = 0; c < m.getColumnDimension(); c++) { d[r][c] = Math.sqrt(m.getEntry(r, c)); } } return new Array2DRowRealMatrix(d, false); } /** * @param m Input matrix. * @return Matrix representing the element-wise square of m. */ private static RealMatrix square(final RealMatrix m) { final double[][] d = new double[m.getRowDimension()][m.getColumnDimension()]; for (int r = 0; r < m.getRowDimension(); r++) { for (int c = 0; c < m.getColumnDimension(); c++) { double e = m.getEntry(r, c); d[r][c] = e * e; } } return new Array2DRowRealMatrix(d, false); } /** * @param m Input matrix 1. * @param n Input matrix 2. * @return the matrix where the elements of m and n are element-wise multiplied. */ private static RealMatrix times(final RealMatrix m, final RealMatrix n) { final double[][] d = new double[m.getRowDimension()][m.getColumnDimension()]; for (int r = 0; r < m.getRowDimension(); r++) { for (int c = 0; c < m.getColumnDimension(); c++) { d[r][c] = m.getEntry(r, c) * n.getEntry(r, c); } } return new Array2DRowRealMatrix(d, false); } /** * @param m Input matrix 1. * @param n Input matrix 2. * @return Matrix where the elements of m and n are element-wise divided. */ private static RealMatrix divide(final RealMatrix m, final RealMatrix n) { final double[][] d = new double[m.getRowDimension()][m.getColumnDimension()]; for (int r = 0; r < m.getRowDimension(); r++) { for (int c = 0; c < m.getColumnDimension(); c++) { d[r][c] = m.getEntry(r, c) / n.getEntry(r, c); } } return new Array2DRowRealMatrix(d, false); } /** * @param m Input matrix. * @param cols Columns to select. * @return Matrix representing the selected columns. */ private static RealMatrix selectColumns(final RealMatrix m, final int[] cols) { final double[][] d = new double[m.getRowDimension()][cols.length]; for (int r = 0; r < m.getRowDimension(); r++) { for (int c = 0; c < cols.length; c++) { d[r][c] = m.getEntry(r, cols[c]); } } return new Array2DRowRealMatrix(d, false); } /** * @param m Input matrix. * @param k Diagonal position. * @return Upper triangular part of matrix. */ private static RealMatrix triu(final RealMatrix m, int k) { final double[][] d = new double[m.getRowDimension()][m.getColumnDimension()]; for (int r = 0; r < m.getRowDimension(); r++) { for (int c = 0; c < m.getColumnDimension(); c++) { d[r][c] = r <= c - k ? m.getEntry(r, c) : 0; } } return new Array2DRowRealMatrix(d, false); } /** * @param m Input matrix. * @return Row matrix representing the sums of the rows. */ private static RealMatrix sumRows(final RealMatrix m) { final double[][] d = new double[1][m.getColumnDimension()]; for (int c = 0; c < m.getColumnDimension(); c++) { double sum = 0; for (int r = 0; r < m.getRowDimension(); r++) { sum += m.getEntry(r, c); } d[0][c] = sum; } return new Array2DRowRealMatrix(d, false); } /** * @param m Input matrix. * @return the diagonal n-by-n matrix if m is a column matrix or the column * matrix representing the diagonal if m is a n-by-n matrix. */ private static RealMatrix diag(final RealMatrix m) { if (m.getColumnDimension() == 1) { final double[][] d = new double[m.getRowDimension()][m.getRowDimension()]; for (int i = 0; i < m.getRowDimension(); i++) { d[i][i] = m.getEntry(i, 0); } return new Array2DRowRealMatrix(d, false); } else { final double[][] d = new double[m.getRowDimension()][1]; for (int i = 0; i < m.getColumnDimension(); i++) { d[i][0] = m.getEntry(i, i); } return new Array2DRowRealMatrix(d, false); } } /** * Copies a column from m1 to m2. * * @param m1 Source matrix. * @param col1 Source column. * @param m2 Target matrix. * @param col2 Target column. */ private static void copyColumn(final RealMatrix m1, int col1, RealMatrix m2, int col2) { for (int i = 0; i < m1.getRowDimension(); i++) { m2.setEntry(i, col2, m1.getEntry(i, col1)); } } /** * @param n Number of rows. * @param m Number of columns. * @return n-by-m matrix filled with 1. */ private static RealMatrix ones(int n, int m) { final double[][] d = new double[n][m]; for (int r = 0; r < n; r++) { Arrays.fill(d[r], 1); } return new Array2DRowRealMatrix(d, false); } /** * @param n Number of rows. * @param m Number of columns. * @return n-by-m matrix of 0 values out of diagonal, and 1 values on * the diagonal. */ private static RealMatrix eye(int n, int m) { final double[][] d = new double[n][m]; for (int r = 0; r < n; r++) { if (r < m) { d[r][r] = 1; } } return new Array2DRowRealMatrix(d, false); } /** * @param n Number of rows. * @param m Number of columns. * @return n-by-m matrix of zero values. */ private static RealMatrix zeros(int n, int m) { return new Array2DRowRealMatrix(n, m); } /** * @param mat Input matrix. * @param n Number of row replicates. * @param m Number of column replicates. * @return a matrix which replicates the input matrix in both directions. */ private static RealMatrix repmat(final RealMatrix mat, int n, int m) { final int rd = mat.getRowDimension(); final int cd = mat.getColumnDimension(); final double[][] d = new double[n * rd][m * cd]; for (int r = 0; r < n * rd; r++) { for (int c = 0; c < m * cd; c++) { d[r][c] = mat.getEntry(r % rd, c % cd); } } return new Array2DRowRealMatrix(d, false); } /** * @param start Start value. * @param end End value. * @param step Step size. * @return a sequence as column matrix. */ private static RealMatrix sequence(double start, double end, double step) { final int size = (int) ((end - start) / step + 1); final double[][] d = new double[size][1]; double value = start; for (int r = 0; r < size; r++) { d[r][0] = value; value += step; } return new Array2DRowRealMatrix(d, false); } /** * @param m Input matrix. * @return the maximum of the matrix element values. */ private static double max(final RealMatrix m) { double max = -Double.MAX_VALUE; for (int r = 0; r < m.getRowDimension(); r++) { for (int c = 0; c < m.getColumnDimension(); c++) { double e = m.getEntry(r, c); if (max < e) { max = e; } } } return max; } /** * @param m Input matrix. * @return the minimum of the matrix element values. */ private static double min(final RealMatrix m) { double min = Double.MAX_VALUE; for (int r = 0; r < m.getRowDimension(); r++) { for (int c = 0; c < m.getColumnDimension(); c++) { double e = m.getEntry(r, c); if (min > e) { min = e; } } } return min; } /** * @param m Input array. * @return the maximum of the array values. */ private static double max(final double[] m) { double max = -Double.MAX_VALUE; for (int r = 0; r < m.length; r++) { if (max < m[r]) { max = m[r]; } } return max; } /** * @param m Input array. * @return the minimum of the array values. */ private static double min(final double[] m) { double min = Double.MAX_VALUE; for (int r = 0; r < m.length; r++) { if (min > m[r]) { min = m[r]; } } return min; } /** * @param indices Input index array. * @return the inverse of the mapping defined by indices. */ private static int[] inverse(final int[] indices) { final int[] inverse = new int[indices.length]; for (int i = 0; i < indices.length; i++) { inverse[indices[i]] = i; } return inverse; } /** * @param indices Input index array. * @return the indices in inverse order (last is first). */ private static int[] reverse(final int[] indices) { final int[] reverse = new int[indices.length]; for (int i = 0; i < indices.length; i++) { reverse[i] = indices[indices.length - i - 1]; } return reverse; } /** * @param size Length of random array. * @return an array of Gaussian random numbers. */ private double[] randn(int size) { final double[] randn = new double[size]; for (int i = 0; i < size; i++) { randn[i] = random.nextGaussian(); } return randn; } /** * @param size Number of rows. * @param popSize Population size. * @return a 2-dimensional matrix of Gaussian random numbers. */ private RealMatrix randn1(int size, int popSize) { final double[][] d = new double[size][popSize]; for (int r = 0; r < size; r++) { for (int c = 0; c < popSize; c++) { d[r][c] = random.nextGaussian(); } } return new Array2DRowRealMatrix(d, false); } } ././@LongLink100644 0 0 150 12126630646 10255 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/optimization/direct/NelderMeadSimplex.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/optimization/direct/NelderMeadSimplex.j100644 1750 1750 25460 12126627712 32266 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.optimization.direct; import java.util.Comparator; import org.apache.commons.math3.optimization.PointValuePair; import org.apache.commons.math3.analysis.MultivariateFunction; /** * This class implements the Nelder-Mead simplex algorithm. * * @version $Id: NelderMeadSimplex.java 1422230 2012-12-15 12:11:13Z erans $ * @deprecated As of 3.1 (to be removed in 4.0). * @since 3.0 */ @Deprecated public class NelderMeadSimplex extends AbstractSimplex { /** Default value for {@link #rho}: {@value}. */ private static final double DEFAULT_RHO = 1; /** Default value for {@link #khi}: {@value}. */ private static final double DEFAULT_KHI = 2; /** Default value for {@link #gamma}: {@value}. */ private static final double DEFAULT_GAMMA = 0.5; /** Default value for {@link #sigma}: {@value}. */ private static final double DEFAULT_SIGMA = 0.5; /** Reflection coefficient. */ private final double rho; /** Expansion coefficient. */ private final double khi; /** Contraction coefficient. */ private final double gamma; /** Shrinkage coefficient. */ private final double sigma; /** * Build a Nelder-Mead simplex with default coefficients. * The default coefficients are 1.0 for rho, 2.0 for khi and 0.5 * for both gamma and sigma. * * @param n Dimension of the simplex. */ public NelderMeadSimplex(final int n) { this(n, 1d); } /** * Build a Nelder-Mead simplex with default coefficients. * The default coefficients are 1.0 for rho, 2.0 for khi and 0.5 * for both gamma and sigma. * * @param n Dimension of the simplex. * @param sideLength Length of the sides of the default (hypercube) * simplex. See {@link AbstractSimplex#AbstractSimplex(int,double)}. */ public NelderMeadSimplex(final int n, double sideLength) { this(n, sideLength, DEFAULT_RHO, DEFAULT_KHI, DEFAULT_GAMMA, DEFAULT_SIGMA); } /** * Build a Nelder-Mead simplex with specified coefficients. * * @param n Dimension of the simplex. See * {@link AbstractSimplex#AbstractSimplex(int,double)}. * @param sideLength Length of the sides of the default (hypercube) * simplex. See {@link AbstractSimplex#AbstractSimplex(int,double)}. * @param rho Reflection coefficient. * @param khi Expansion coefficient. * @param gamma Contraction coefficient. * @param sigma Shrinkage coefficient. */ public NelderMeadSimplex(final int n, double sideLength, final double rho, final double khi, final double gamma, final double sigma) { super(n, sideLength); this.rho = rho; this.khi = khi; this.gamma = gamma; this.sigma = sigma; } /** * Build a Nelder-Mead simplex with specified coefficients. * * @param n Dimension of the simplex. See * {@link AbstractSimplex#AbstractSimplex(int)}. * @param rho Reflection coefficient. * @param khi Expansion coefficient. * @param gamma Contraction coefficient. * @param sigma Shrinkage coefficient. */ public NelderMeadSimplex(final int n, final double rho, final double khi, final double gamma, final double sigma) { this(n, 1d, rho, khi, gamma, sigma); } /** * Build a Nelder-Mead simplex with default coefficients. * The default coefficients are 1.0 for rho, 2.0 for khi and 0.5 * for both gamma and sigma. * * @param steps Steps along the canonical axes representing box edges. * They may be negative but not zero. See */ public NelderMeadSimplex(final double[] steps) { this(steps, DEFAULT_RHO, DEFAULT_KHI, DEFAULT_GAMMA, DEFAULT_SIGMA); } /** * Build a Nelder-Mead simplex with specified coefficients. * * @param steps Steps along the canonical axes representing box edges. * They may be negative but not zero. See * {@link AbstractSimplex#AbstractSimplex(double[])}. * @param rho Reflection coefficient. * @param khi Expansion coefficient. * @param gamma Contraction coefficient. * @param sigma Shrinkage coefficient. * @throws IllegalArgumentException if one of the steps is zero. */ public NelderMeadSimplex(final double[] steps, final double rho, final double khi, final double gamma, final double sigma) { super(steps); this.rho = rho; this.khi = khi; this.gamma = gamma; this.sigma = sigma; } /** * Build a Nelder-Mead simplex with default coefficients. * The default coefficients are 1.0 for rho, 2.0 for khi and 0.5 * for both gamma and sigma. * * @param referenceSimplex Reference simplex. See * {@link AbstractSimplex#AbstractSimplex(double[][])}. */ public NelderMeadSimplex(final double[][] referenceSimplex) { this(referenceSimplex, DEFAULT_RHO, DEFAULT_KHI, DEFAULT_GAMMA, DEFAULT_SIGMA); } /** * Build a Nelder-Mead simplex with specified coefficients. * * @param referenceSimplex Reference simplex. See * {@link AbstractSimplex#AbstractSimplex(double[][])}. * @param rho Reflection coefficient. * @param khi Expansion coefficient. * @param gamma Contraction coefficient. * @param sigma Shrinkage coefficient. * @throws org.apache.commons.math3.exception.NotStrictlyPositiveException * if the reference simplex does not contain at least one point. * @throws org.apache.commons.math3.exception.DimensionMismatchException * if there is a dimension mismatch in the reference simplex. */ public NelderMeadSimplex(final double[][] referenceSimplex, final double rho, final double khi, final double gamma, final double sigma) { super(referenceSimplex); this.rho = rho; this.khi = khi; this.gamma = gamma; this.sigma = sigma; } /** {@inheritDoc} */ @Override public void iterate(final MultivariateFunction evaluationFunction, final Comparator comparator) { // The simplex has n + 1 points if dimension is n. final int n = getDimension(); // Interesting values. final PointValuePair best = getPoint(0); final PointValuePair secondBest = getPoint(n - 1); final PointValuePair worst = getPoint(n); final double[] xWorst = worst.getPointRef(); // Compute the centroid of the best vertices (dismissing the worst // point at index n). final double[] centroid = new double[n]; for (int i = 0; i < n; i++) { final double[] x = getPoint(i).getPointRef(); for (int j = 0; j < n; j++) { centroid[j] += x[j]; } } final double scaling = 1.0 / n; for (int j = 0; j < n; j++) { centroid[j] *= scaling; } // compute the reflection point final double[] xR = new double[n]; for (int j = 0; j < n; j++) { xR[j] = centroid[j] + rho * (centroid[j] - xWorst[j]); } final PointValuePair reflected = new PointValuePair(xR, evaluationFunction.value(xR), false); if (comparator.compare(best, reflected) <= 0 && comparator.compare(reflected, secondBest) < 0) { // Accept the reflected point. replaceWorstPoint(reflected, comparator); } else if (comparator.compare(reflected, best) < 0) { // Compute the expansion point. final double[] xE = new double[n]; for (int j = 0; j < n; j++) { xE[j] = centroid[j] + khi * (xR[j] - centroid[j]); } final PointValuePair expanded = new PointValuePair(xE, evaluationFunction.value(xE), false); if (comparator.compare(expanded, reflected) < 0) { // Accept the expansion point. replaceWorstPoint(expanded, comparator); } else { // Accept the reflected point. replaceWorstPoint(reflected, comparator); } } else { if (comparator.compare(reflected, worst) < 0) { // Perform an outside contraction. final double[] xC = new double[n]; for (int j = 0; j < n; j++) { xC[j] = centroid[j] + gamma * (xR[j] - centroid[j]); } final PointValuePair outContracted = new PointValuePair(xC, evaluationFunction.value(xC), false); if (comparator.compare(outContracted, reflected) <= 0) { // Accept the contraction point. replaceWorstPoint(outContracted, comparator); return; } } else { // Perform an inside contraction. final double[] xC = new double[n]; for (int j = 0; j < n; j++) { xC[j] = centroid[j] - gamma * (centroid[j] - xWorst[j]); } final PointValuePair inContracted = new PointValuePair(xC, evaluationFunction.value(xC), false); if (comparator.compare(inContracted, worst) < 0) { // Accept the contraction point. replaceWorstPoint(inContracted, comparator); return; } } // Perform a shrink. final double[] xSmallest = getPoint(0).getPointRef(); for (int i = 1; i <= n; i++) { final double[] x = getPoint(i).getPoint(); for (int j = 0; j < n; j++) { x[j] = xSmallest[j] + sigma * (x[j] - xSmallest[j]); } setPoint(i, new PointValuePair(x, Double.NaN, false)); } evaluate(evaluationFunction, comparator); } } } ././@LongLink100644 0 0 176 12126630646 10265 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/optimization/direct/BaseAbstractMultivariateVectorOptimizer.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/optimization/direct/BaseAbstractMultiva100644 1750 1750 31666 12126627712 32401 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.optimization.direct; import org.apache.commons.math3.util.Incrementor; import org.apache.commons.math3.exception.MaxCountExceededException; import org.apache.commons.math3.exception.TooManyEvaluationsException; import org.apache.commons.math3.exception.DimensionMismatchException; import org.apache.commons.math3.exception.NullArgumentException; import org.apache.commons.math3.analysis.MultivariateVectorFunction; import org.apache.commons.math3.optimization.OptimizationData; import org.apache.commons.math3.optimization.InitialGuess; import org.apache.commons.math3.optimization.Target; import org.apache.commons.math3.optimization.Weight; import org.apache.commons.math3.optimization.BaseMultivariateVectorOptimizer; import org.apache.commons.math3.optimization.ConvergenceChecker; import org.apache.commons.math3.optimization.PointVectorValuePair; import org.apache.commons.math3.optimization.SimpleVectorValueChecker; import org.apache.commons.math3.linear.RealMatrix; /** * Base class for implementing optimizers for multivariate scalar functions. * This base class handles the boiler-plate methods associated to thresholds * settings, iterations and evaluations counting. * * @param the type of the objective function to be optimized * * @version $Id: BaseAbstractMultivariateVectorOptimizer.java 1422230 2012-12-15 12:11:13Z erans $ * @deprecated As of 3.1 (to be removed in 4.0). * @since 3.0 */ @Deprecated public abstract class BaseAbstractMultivariateVectorOptimizer implements BaseMultivariateVectorOptimizer { /** Evaluations counter. */ protected final Incrementor evaluations = new Incrementor(); /** Convergence checker. */ private ConvergenceChecker checker; /** Target value for the objective functions at optimum. */ private double[] target; /** Weight matrix. */ private RealMatrix weightMatrix; /** Weight for the least squares cost computation. * @deprecated */ private double[] weight; /** Initial guess. */ private double[] start; /** Objective function. */ private FUNC function; /** * Simple constructor with default settings. * The convergence check is set to a {@link SimpleVectorValueChecker}. * @deprecated See {@link SimpleVectorValueChecker#SimpleVectorValueChecker()} */ @Deprecated protected BaseAbstractMultivariateVectorOptimizer() { this(new SimpleVectorValueChecker()); } /** * @param checker Convergence checker. */ protected BaseAbstractMultivariateVectorOptimizer(ConvergenceChecker checker) { this.checker = checker; } /** {@inheritDoc} */ public int getMaxEvaluations() { return evaluations.getMaximalCount(); } /** {@inheritDoc} */ public int getEvaluations() { return evaluations.getCount(); } /** {@inheritDoc} */ public ConvergenceChecker getConvergenceChecker() { return checker; } /** * Compute the objective function value. * * @param point Point at which the objective function must be evaluated. * @return the objective function value at the specified point. * @throws TooManyEvaluationsException if the maximal number of evaluations is * exceeded. */ protected double[] computeObjectiveValue(double[] point) { try { evaluations.incrementCount(); } catch (MaxCountExceededException e) { throw new TooManyEvaluationsException(e.getMax()); } return function.value(point); } /** {@inheritDoc} * * @deprecated As of 3.1. Please use * {@link #optimize(int,MultivariateVectorFunction,OptimizationData[])} * instead. */ @Deprecated public PointVectorValuePair optimize(int maxEval, FUNC f, double[] t, double[] w, double[] startPoint) { return optimizeInternal(maxEval, f, t, w, startPoint); } /** * Optimize an objective function. * * @param maxEval Allowed number of evaluations of the objective function. * @param f Objective function. * @param optData Optimization data. The following data will be looked for: *
                            *
                          • {@link Target}
                          • *
                          • {@link Weight}
                          • *
                          • {@link InitialGuess}
                          • *
                          * @return the point/value pair giving the optimal value of the objective * function. * @throws TooManyEvaluationsException if the maximal number of * evaluations is exceeded. * @throws DimensionMismatchException if the initial guess, target, and weight * arguments have inconsistent dimensions. * * @since 3.1 */ protected PointVectorValuePair optimize(int maxEval, FUNC f, OptimizationData... optData) throws TooManyEvaluationsException, DimensionMismatchException { return optimizeInternal(maxEval, f, optData); } /** * Optimize an objective function. * Optimization is considered to be a weighted least-squares minimization. * The cost function to be minimized is * ∑weighti(objectivei - targeti)2 * * @param f Objective function. * @param t Target value for the objective functions at optimum. * @param w Weights for the least squares cost computation. * @param startPoint Start point for optimization. * @return the point/value pair giving the optimal value for objective * function. * @param maxEval Maximum number of function evaluations. * @throws org.apache.commons.math3.exception.DimensionMismatchException * if the start point dimension is wrong. * @throws org.apache.commons.math3.exception.TooManyEvaluationsException * if the maximal number of evaluations is exceeded. * @throws org.apache.commons.math3.exception.NullArgumentException if * any argument is {@code null}. * @deprecated As of 3.1. Please use * {@link #optimizeInternal(int,MultivariateVectorFunction,OptimizationData[])} * instead. */ @Deprecated protected PointVectorValuePair optimizeInternal(final int maxEval, final FUNC f, final double[] t, final double[] w, final double[] startPoint) { // Checks. if (f == null) { throw new NullArgumentException(); } if (t == null) { throw new NullArgumentException(); } if (w == null) { throw new NullArgumentException(); } if (startPoint == null) { throw new NullArgumentException(); } if (t.length != w.length) { throw new DimensionMismatchException(t.length, w.length); } return optimizeInternal(maxEval, f, new Target(t), new Weight(w), new InitialGuess(startPoint)); } /** * Optimize an objective function. * * @param maxEval Allowed number of evaluations of the objective function. * @param f Objective function. * @param optData Optimization data. The following data will be looked for: *
                            *
                          • {@link Target}
                          • *
                          • {@link Weight}
                          • *
                          • {@link InitialGuess}
                          • *
                          * @return the point/value pair giving the optimal value of the objective * function. * @throws TooManyEvaluationsException if the maximal number of * evaluations is exceeded. * @throws DimensionMismatchException if the initial guess, target, and weight * arguments have inconsistent dimensions. * * @since 3.1 */ protected PointVectorValuePair optimizeInternal(int maxEval, FUNC f, OptimizationData... optData) throws TooManyEvaluationsException, DimensionMismatchException { // Set internal state. evaluations.setMaximalCount(maxEval); evaluations.resetCount(); function = f; // Retrieve other settings. parseOptimizationData(optData); // Check input consistency. checkParameters(); // Allow subclasses to reset their own internal state. setUp(); // Perform computation. return doOptimize(); } /** * Gets the initial values of the optimized parameters. * * @return the initial guess. */ public double[] getStartPoint() { return start.clone(); } /** * Gets the weight matrix of the observations. * * @return the weight matrix. * @since 3.1 */ public RealMatrix getWeight() { return weightMatrix.copy(); } /** * Gets the observed values to be matched by the objective vector * function. * * @return the target values. * @since 3.1 */ public double[] getTarget() { return target.clone(); } /** * Gets the objective vector function. * Note that this access bypasses the evaluation counter. * * @return the objective vector function. * @since 3.1 */ protected FUNC getObjectiveFunction() { return function; } /** * Perform the bulk of the optimization algorithm. * * @return the point/value pair giving the optimal value for the * objective function. */ protected abstract PointVectorValuePair doOptimize(); /** * @return a reference to the {@link #target array}. * @deprecated As of 3.1. */ @Deprecated protected double[] getTargetRef() { return target; } /** * @return a reference to the {@link #weight array}. * @deprecated As of 3.1. */ @Deprecated protected double[] getWeightRef() { return weight; } /** * Method which a subclass must override whenever its internal * state depend on the {@link OptimizationData input} parsed by this base * class. * It will be called after the parsing step performed in the * {@link #optimize(int,MultivariateVectorFunction,OptimizationData[]) * optimize} method and just before {@link #doOptimize()}. * * @since 3.1 */ protected void setUp() { // XXX Temporary code until the new internal data is used everywhere. final int dim = target.length; weight = new double[dim]; for (int i = 0; i < dim; i++) { weight[i] = weightMatrix.getEntry(i, i); } } /** * Scans the list of (required and optional) optimization data that * characterize the problem. * * @param optData Optimization data. The following data will be looked for: *
                            *
                          • {@link Target}
                          • *
                          • {@link Weight}
                          • *
                          • {@link InitialGuess}
                          • *
                          */ private void parseOptimizationData(OptimizationData... optData) { // The existing values (as set by the previous call) are reused if // not provided in the argument list. for (OptimizationData data : optData) { if (data instanceof Target) { target = ((Target) data).getTarget(); continue; } if (data instanceof Weight) { weightMatrix = ((Weight) data).getWeight(); continue; } if (data instanceof InitialGuess) { start = ((InitialGuess) data).getInitialGuess(); continue; } } } /** * Check parameters consistency. * * @throws DimensionMismatchException if {@link #target} and * {@link #weightMatrix} have inconsistent dimensions. */ private void checkParameters() { if (target.length != weightMatrix.getColumnDimension()) { throw new DimensionMismatchException(target.length, weightMatrix.getColumnDimension()); } } } ././@LongLink100644 0 0 171 12126630646 10260 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/optimization/direct/MultivariateFunctionPenaltyAdapter.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/optimization/direct/MultivariateFunctio100644 1750 1750 20472 12126627712 32470 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.optimization.direct; import org.apache.commons.math3.analysis.MultivariateFunction; import org.apache.commons.math3.exception.DimensionMismatchException; import org.apache.commons.math3.exception.NumberIsTooSmallException; import org.apache.commons.math3.util.FastMath; import org.apache.commons.math3.util.MathUtils; /** *

                          Adapter extending bounded {@link MultivariateFunction} to an unbouded * domain using a penalty function.

                          * *

                          * This adapter can be used to wrap functions subject to simple bounds on * parameters so they can be used by optimizers that do not directly * support simple bounds. *

                          *

                          * The principle is that the user function that will be wrapped will see its * parameters bounded as required, i.e when its {@code value} method is called * with argument array {@code point}, the elements array will fulfill requirement * {@code lower[i] <= point[i] <= upper[i]} for all i. Some of the components * may be unbounded or bounded only on one side if the corresponding bound is * set to an infinite value. The optimizer will not manage the user function by * itself, but it will handle this adapter and it is this adapter that will take * care the bounds are fulfilled. The adapter {@link #value(double[])} method will * be called by the optimizer with unbound parameters, and the adapter will check * if the parameters is within range or not. If it is in range, then the underlying * user function will be called, and if it is not the value of a penalty function * will be returned instead. *

                          *

                          * This adapter is only a poor man solution to simple bounds optimization constraints * that can be used with simple optimizers like {@link SimplexOptimizer} with {@link * NelderMeadSimplex} or {@link MultiDirectionalSimplex}. A better solution is to use * an optimizer that directly supports simple bounds like {@link CMAESOptimizer} or * {@link BOBYQAOptimizer}. One caveat of this poor man solution is that if start point * or start simplex is completely outside of the allowed range, only the penalty function * is used, and the optimizer may converge without ever entering the range. *

                          * * @see MultivariateFunctionMappingAdapter * * @version $Id: MultivariateFunctionPenaltyAdapter.java 1422230 2012-12-15 12:11:13Z erans $ * @deprecated As of 3.1 (to be removed in 4.0). * @since 3.0 */ @Deprecated public class MultivariateFunctionPenaltyAdapter implements MultivariateFunction { /** Underlying bounded function. */ private final MultivariateFunction bounded; /** Lower bounds. */ private final double[] lower; /** Upper bounds. */ private final double[] upper; /** Penalty offset. */ private final double offset; /** Penalty scales. */ private final double[] scale; /** Simple constructor. *

                          * When the optimizer provided points are out of range, the value of the * penalty function will be used instead of the value of the underlying * function. In order for this penalty to be effective in rejecting this * point during the optimization process, the penalty function value should * be defined with care. This value is computed as: *

                               *   penalty(point) = offset + ∑i[scale[i] * √|point[i]-boundary[i]|]
                               * 
                          * where indices i correspond to all the components that violates their boundaries. *

                          *

                          * So when attempting a function minimization, offset should be larger than * the maximum expected value of the underlying function and scale components * should all be positive. When attempting a function maximization, offset * should be lesser than the minimum expected value of the underlying function * and scale components should all be negative. * minimization, and lesser than the minimum expected value of the underlying * function when attempting maximization. *

                          *

                          * These choices for the penalty function have two properties. First, all out * of range points will return a function value that is worse than the value * returned by any in range point. Second, the penalty is worse for large * boundaries violation than for small violations, so the optimizer has an hint * about the direction in which it should search for acceptable points. *

                          * @param bounded bounded function * @param lower lower bounds for each element of the input parameters array * (some elements may be set to {@code Double.NEGATIVE_INFINITY} for * unbounded values) * @param upper upper bounds for each element of the input parameters array * (some elements may be set to {@code Double.POSITIVE_INFINITY} for * unbounded values) * @param offset base offset of the penalty function * @param scale scale of the penalty function * @exception DimensionMismatchException if lower bounds, upper bounds and * scales are not consistent, either according to dimension or to bounadary * values */ public MultivariateFunctionPenaltyAdapter(final MultivariateFunction bounded, final double[] lower, final double[] upper, final double offset, final double[] scale) { // safety checks MathUtils.checkNotNull(lower); MathUtils.checkNotNull(upper); MathUtils.checkNotNull(scale); if (lower.length != upper.length) { throw new DimensionMismatchException(lower.length, upper.length); } if (lower.length != scale.length) { throw new DimensionMismatchException(lower.length, scale.length); } for (int i = 0; i < lower.length; ++i) { // note the following test is written in such a way it also fails for NaN if (!(upper[i] >= lower[i])) { throw new NumberIsTooSmallException(upper[i], lower[i], true); } } this.bounded = bounded; this.lower = lower.clone(); this.upper = upper.clone(); this.offset = offset; this.scale = scale.clone(); } /** Compute the underlying function value from an unbounded point. *

                          * This method simply returns the value of the underlying function * if the unbounded point already fulfills the bounds, and compute * a replacement value using the offset and scale if bounds are * violated, without calling the function at all. *

                          * @param point unbounded point * @return either underlying function value or penalty function value */ public double value(double[] point) { for (int i = 0; i < scale.length; ++i) { if ((point[i] < lower[i]) || (point[i] > upper[i])) { // bound violation starting at this component double sum = 0; for (int j = i; j < scale.length; ++j) { final double overshoot; if (point[j] < lower[j]) { overshoot = scale[j] * (lower[j] - point[j]); } else if (point[j] > upper[j]) { overshoot = scale[j] * (point[j] - upper[j]); } else { overshoot = 0; } sum += FastMath.sqrt(overshoot); } return offset + sum; } } // all boundaries are fulfilled, we are in the expected // domain of the underlying function return bounded.value(point); } } ././@LongLink100644 0 0 204 12126630646 10255 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/optimization/direct/BaseAbstractMultivariateSimpleBoundsOptimizer.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/optimization/direct/BaseAbstractMultiva100644 1750 1750 7274 12126627712 32357 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.optimization.direct; import org.apache.commons.math3.analysis.MultivariateFunction; import org.apache.commons.math3.optimization.BaseMultivariateOptimizer; import org.apache.commons.math3.optimization.BaseMultivariateSimpleBoundsOptimizer; import org.apache.commons.math3.optimization.GoalType; import org.apache.commons.math3.optimization.InitialGuess; import org.apache.commons.math3.optimization.SimpleBounds; import org.apache.commons.math3.optimization.PointValuePair; import org.apache.commons.math3.optimization.ConvergenceChecker; /** * Base class for implementing optimizers for multivariate scalar functions, * subject to simple bounds: The valid range of the parameters is an interval. * The interval can possibly be infinite (in one or both directions). * This base class handles the boiler-plate methods associated to thresholds * settings, iterations and evaluations counting. * * @param Type of the objective function to be optimized. * * @version $Id: BaseAbstractMultivariateSimpleBoundsOptimizer.java 1462503 2013-03-29 15:48:27Z luc $ * @deprecated As of 3.1 (to be removed in 4.0). * @since 3.0 * @deprecated As of 3.1 since the {@link BaseAbstractMultivariateOptimizer * base class} contains similar functionality. */ @Deprecated public abstract class BaseAbstractMultivariateSimpleBoundsOptimizer extends BaseAbstractMultivariateOptimizer implements BaseMultivariateOptimizer, BaseMultivariateSimpleBoundsOptimizer { /** * Simple constructor with default settings. * The convergence checker is set to a * {@link org.apache.commons.math3.optimization.SimpleValueChecker}. * * @see BaseAbstractMultivariateOptimizer#BaseAbstractMultivariateOptimizer() * @deprecated See {@link org.apache.commons.math3.optimization.SimpleValueChecker#SimpleValueChecker()} */ @Deprecated protected BaseAbstractMultivariateSimpleBoundsOptimizer() {} /** * @param checker Convergence checker. */ protected BaseAbstractMultivariateSimpleBoundsOptimizer(ConvergenceChecker checker) { super(checker); } /** {@inheritDoc} */ @Override public PointValuePair optimize(int maxEval, FUNC f, GoalType goalType, double[] startPoint) { return super.optimizeInternal(maxEval, f, goalType, new InitialGuess(startPoint)); } /** {@inheritDoc} */ public PointValuePair optimize(int maxEval, FUNC f, GoalType goalType, double[] startPoint, double[] lower, double[] upper) { return super.optimizeInternal(maxEval, f, goalType, new InitialGuess(startPoint), new SimpleBounds(lower, upper)); } } ././@LongLink100644 0 0 170 12126630646 10257 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/optimization/direct/BaseAbstractMultivariateOptimizer.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/optimization/direct/BaseAbstractMultiva100644 1750 1750 26533 12126627712 32376 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.optimization.direct; import org.apache.commons.math3.util.Incrementor; import org.apache.commons.math3.exception.MaxCountExceededException; import org.apache.commons.math3.exception.TooManyEvaluationsException; import org.apache.commons.math3.analysis.MultivariateFunction; import org.apache.commons.math3.optimization.BaseMultivariateOptimizer; import org.apache.commons.math3.optimization.OptimizationData; import org.apache.commons.math3.optimization.GoalType; import org.apache.commons.math3.optimization.InitialGuess; import org.apache.commons.math3.optimization.SimpleBounds; import org.apache.commons.math3.optimization.ConvergenceChecker; import org.apache.commons.math3.optimization.PointValuePair; import org.apache.commons.math3.optimization.SimpleValueChecker; import org.apache.commons.math3.exception.DimensionMismatchException; import org.apache.commons.math3.exception.NumberIsTooSmallException; import org.apache.commons.math3.exception.NumberIsTooLargeException; /** * Base class for implementing optimizers for multivariate scalar functions. * This base class handles the boiler-plate methods associated to thresholds, * evaluations counting, initial guess and simple bounds settings. * * @param Type of the objective function to be optimized. * * @version $Id: BaseAbstractMultivariateOptimizer.java 1422313 2012-12-15 18:53:41Z psteitz $ * @deprecated As of 3.1 (to be removed in 4.0). * @since 2.2 */ @Deprecated public abstract class BaseAbstractMultivariateOptimizer implements BaseMultivariateOptimizer { /** Evaluations counter. */ protected final Incrementor evaluations = new Incrementor(); /** Convergence checker. */ private ConvergenceChecker checker; /** Type of optimization. */ private GoalType goal; /** Initial guess. */ private double[] start; /** Lower bounds. */ private double[] lowerBound; /** Upper bounds. */ private double[] upperBound; /** Objective function. */ private MultivariateFunction function; /** * Simple constructor with default settings. * The convergence check is set to a {@link SimpleValueChecker}. * @deprecated See {@link SimpleValueChecker#SimpleValueChecker()} */ @Deprecated protected BaseAbstractMultivariateOptimizer() { this(new SimpleValueChecker()); } /** * @param checker Convergence checker. */ protected BaseAbstractMultivariateOptimizer(ConvergenceChecker checker) { this.checker = checker; } /** {@inheritDoc} */ public int getMaxEvaluations() { return evaluations.getMaximalCount(); } /** {@inheritDoc} */ public int getEvaluations() { return evaluations.getCount(); } /** {@inheritDoc} */ public ConvergenceChecker getConvergenceChecker() { return checker; } /** * Compute the objective function value. * * @param point Point at which the objective function must be evaluated. * @return the objective function value at the specified point. * @throws TooManyEvaluationsException if the maximal number of * evaluations is exceeded. */ protected double computeObjectiveValue(double[] point) { try { evaluations.incrementCount(); } catch (MaxCountExceededException e) { throw new TooManyEvaluationsException(e.getMax()); } return function.value(point); } /** * {@inheritDoc} * * @deprecated As of 3.1. Please use * {@link #optimize(int,MultivariateFunction,GoalType,OptimizationData[])} * instead. */ @Deprecated public PointValuePair optimize(int maxEval, FUNC f, GoalType goalType, double[] startPoint) { return optimizeInternal(maxEval, f, goalType, new InitialGuess(startPoint)); } /** * Optimize an objective function. * * @param maxEval Allowed number of evaluations of the objective function. * @param f Objective function. * @param goalType Optimization type. * @param optData Optimization data. The following data will be looked for: *
                            *
                          • {@link InitialGuess}
                          • *
                          • {@link SimpleBounds}
                          • *
                          * @return the point/value pair giving the optimal value of the objective * function. * @since 3.1 */ public PointValuePair optimize(int maxEval, FUNC f, GoalType goalType, OptimizationData... optData) { return optimizeInternal(maxEval, f, goalType, optData); } /** * Optimize an objective function. * * @param f Objective function. * @param goalType Type of optimization goal: either * {@link GoalType#MAXIMIZE} or {@link GoalType#MINIMIZE}. * @param startPoint Start point for optimization. * @param maxEval Maximum number of function evaluations. * @return the point/value pair giving the optimal value for objective * function. * @throws org.apache.commons.math3.exception.DimensionMismatchException * if the start point dimension is wrong. * @throws org.apache.commons.math3.exception.TooManyEvaluationsException * if the maximal number of evaluations is exceeded. * @throws org.apache.commons.math3.exception.NullArgumentException if * any argument is {@code null}. * @deprecated As of 3.1. Please use * {@link #optimize(int,MultivariateFunction,GoalType,OptimizationData[])} * instead. */ @Deprecated protected PointValuePair optimizeInternal(int maxEval, FUNC f, GoalType goalType, double[] startPoint) { return optimizeInternal(maxEval, f, goalType, new InitialGuess(startPoint)); } /** * Optimize an objective function. * * @param maxEval Allowed number of evaluations of the objective function. * @param f Objective function. * @param goalType Optimization type. * @param optData Optimization data. The following data will be looked for: *
                            *
                          • {@link InitialGuess}
                          • *
                          • {@link SimpleBounds}
                          • *
                          * @return the point/value pair giving the optimal value of the objective * function. * @throws TooManyEvaluationsException if the maximal number of * evaluations is exceeded. * @since 3.1 */ protected PointValuePair optimizeInternal(int maxEval, FUNC f, GoalType goalType, OptimizationData... optData) throws TooManyEvaluationsException { // Set internal state. evaluations.setMaximalCount(maxEval); evaluations.resetCount(); function = f; goal = goalType; // Retrieve other settings. parseOptimizationData(optData); // Check input consistency. checkParameters(); // Perform computation. return doOptimize(); } /** * Scans the list of (required and optional) optimization data that * characterize the problem. * * @param optData Optimization data. The following data will be looked for: *
                            *
                          • {@link InitialGuess}
                          • *
                          • {@link SimpleBounds}
                          • *
                          */ private void parseOptimizationData(OptimizationData... optData) { // The existing values (as set by the previous call) are reused if // not provided in the argument list. for (OptimizationData data : optData) { if (data instanceof InitialGuess) { start = ((InitialGuess) data).getInitialGuess(); continue; } if (data instanceof SimpleBounds) { final SimpleBounds bounds = (SimpleBounds) data; lowerBound = bounds.getLower(); upperBound = bounds.getUpper(); continue; } } } /** * @return the optimization type. */ public GoalType getGoalType() { return goal; } /** * @return the initial guess. */ public double[] getStartPoint() { return start == null ? null : start.clone(); } /** * @return the lower bounds. * @since 3.1 */ public double[] getLowerBound() { return lowerBound == null ? null : lowerBound.clone(); } /** * @return the upper bounds. * @since 3.1 */ public double[] getUpperBound() { return upperBound == null ? null : upperBound.clone(); } /** * Perform the bulk of the optimization algorithm. * * @return the point/value pair giving the optimal value of the * objective function. */ protected abstract PointValuePair doOptimize(); /** * Check parameters consistency. */ private void checkParameters() { if (start != null) { final int dim = start.length; if (lowerBound != null) { if (lowerBound.length != dim) { throw new DimensionMismatchException(lowerBound.length, dim); } for (int i = 0; i < dim; i++) { final double v = start[i]; final double lo = lowerBound[i]; if (v < lo) { throw new NumberIsTooSmallException(v, lo, true); } } } if (upperBound != null) { if (upperBound.length != dim) { throw new DimensionMismatchException(upperBound.length, dim); } for (int i = 0; i < dim; i++) { final double v = start[i]; final double hi = upperBound[i]; if (v > hi) { throw new NumberIsTooLargeException(v, hi, true); } } } // If the bounds were not specified, the allowed interval is // assumed to be [-inf, +inf]. if (lowerBound == null) { lowerBound = new double[dim]; for (int i = 0; i < dim; i++) { lowerBound[i] = Double.NEGATIVE_INFINITY; } } if (upperBound == null) { upperBound = new double[dim]; for (int i = 0; i < dim; i++) { upperBound[i] = Double.POSITIVE_INFINITY; } } } } } ././@LongLink100644 0 0 146 12126630646 10262 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/optimization/direct/PowellOptimizer.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/optimization/direct/PowellOptimizer.jav100644 1750 1750 32266 12126627712 32422 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.optimization.direct; import org.apache.commons.math3.util.FastMath; import org.apache.commons.math3.util.MathArrays; import org.apache.commons.math3.analysis.UnivariateFunction; import org.apache.commons.math3.analysis.MultivariateFunction; import org.apache.commons.math3.exception.NumberIsTooSmallException; import org.apache.commons.math3.exception.NotStrictlyPositiveException; import org.apache.commons.math3.optimization.GoalType; import org.apache.commons.math3.optimization.PointValuePair; import org.apache.commons.math3.optimization.ConvergenceChecker; import org.apache.commons.math3.optimization.MultivariateOptimizer; import org.apache.commons.math3.optimization.univariate.BracketFinder; import org.apache.commons.math3.optimization.univariate.BrentOptimizer; import org.apache.commons.math3.optimization.univariate.UnivariatePointValuePair; import org.apache.commons.math3.optimization.univariate.SimpleUnivariateValueChecker; /** * Powell algorithm. * This code is translated and adapted from the Python version of this * algorithm (as implemented in module {@code optimize.py} v0.5 of * SciPy). *
                          * The default stopping criterion is based on the differences of the * function value between two successive iterations. It is however possible * to define a custom convergence checker that might terminate the algorithm * earlier. *
                          * The internal line search optimizer is a {@link BrentOptimizer} with a * convergence checker set to {@link SimpleUnivariateValueChecker}. * * @version $Id: PowellOptimizer.java 1462503 2013-03-29 15:48:27Z luc $ * @deprecated As of 3.1 (to be removed in 4.0). * @since 2.2 */ @Deprecated public class PowellOptimizer extends BaseAbstractMultivariateOptimizer implements MultivariateOptimizer { /** * Minimum relative tolerance. */ private static final double MIN_RELATIVE_TOLERANCE = 2 * FastMath.ulp(1d); /** * Relative threshold. */ private final double relativeThreshold; /** * Absolute threshold. */ private final double absoluteThreshold; /** * Line search. */ private final LineSearch line; /** * This constructor allows to specify a user-defined convergence checker, * in addition to the parameters that control the default convergence * checking procedure. *
                          * The internal line search tolerances are set to the square-root of their * corresponding value in the multivariate optimizer. * * @param rel Relative threshold. * @param abs Absolute threshold. * @param checker Convergence checker. * @throws NotStrictlyPositiveException if {@code abs <= 0}. * @throws NumberIsTooSmallException if {@code rel < 2 * Math.ulp(1d)}. */ public PowellOptimizer(double rel, double abs, ConvergenceChecker checker) { this(rel, abs, FastMath.sqrt(rel), FastMath.sqrt(abs), checker); } /** * This constructor allows to specify a user-defined convergence checker, * in addition to the parameters that control the default convergence * checking procedure and the line search tolerances. * * @param rel Relative threshold for this optimizer. * @param abs Absolute threshold for this optimizer. * @param lineRel Relative threshold for the internal line search optimizer. * @param lineAbs Absolute threshold for the internal line search optimizer. * @param checker Convergence checker. * @throws NotStrictlyPositiveException if {@code abs <= 0}. * @throws NumberIsTooSmallException if {@code rel < 2 * Math.ulp(1d)}. */ public PowellOptimizer(double rel, double abs, double lineRel, double lineAbs, ConvergenceChecker checker) { super(checker); if (rel < MIN_RELATIVE_TOLERANCE) { throw new NumberIsTooSmallException(rel, MIN_RELATIVE_TOLERANCE, true); } if (abs <= 0) { throw new NotStrictlyPositiveException(abs); } relativeThreshold = rel; absoluteThreshold = abs; // Create the line search optimizer. line = new LineSearch(lineRel, lineAbs); } /** * The parameters control the default convergence checking procedure. *
                          * The internal line search tolerances are set to the square-root of their * corresponding value in the multivariate optimizer. * * @param rel Relative threshold. * @param abs Absolute threshold. * @throws NotStrictlyPositiveException if {@code abs <= 0}. * @throws NumberIsTooSmallException if {@code rel < 2 * Math.ulp(1d)}. */ public PowellOptimizer(double rel, double abs) { this(rel, abs, null); } /** * Builds an instance with the default convergence checking procedure. * * @param rel Relative threshold. * @param abs Absolute threshold. * @param lineRel Relative threshold for the internal line search optimizer. * @param lineAbs Absolute threshold for the internal line search optimizer. * @throws NotStrictlyPositiveException if {@code abs <= 0}. * @throws NumberIsTooSmallException if {@code rel < 2 * Math.ulp(1d)}. * @since 3.1 */ public PowellOptimizer(double rel, double abs, double lineRel, double lineAbs) { this(rel, abs, lineRel, lineAbs, null); } /** {@inheritDoc} */ @Override protected PointValuePair doOptimize() { final GoalType goal = getGoalType(); final double[] guess = getStartPoint(); final int n = guess.length; final double[][] direc = new double[n][n]; for (int i = 0; i < n; i++) { direc[i][i] = 1; } final ConvergenceChecker checker = getConvergenceChecker(); double[] x = guess; double fVal = computeObjectiveValue(x); double[] x1 = x.clone(); int iter = 0; while (true) { ++iter; double fX = fVal; double fX2 = 0; double delta = 0; int bigInd = 0; double alphaMin = 0; for (int i = 0; i < n; i++) { final double[] d = MathArrays.copyOf(direc[i]); fX2 = fVal; final UnivariatePointValuePair optimum = line.search(x, d); fVal = optimum.getValue(); alphaMin = optimum.getPoint(); final double[][] result = newPointAndDirection(x, d, alphaMin); x = result[0]; if ((fX2 - fVal) > delta) { delta = fX2 - fVal; bigInd = i; } } // Default convergence check. boolean stop = 2 * (fX - fVal) <= (relativeThreshold * (FastMath.abs(fX) + FastMath.abs(fVal)) + absoluteThreshold); final PointValuePair previous = new PointValuePair(x1, fX); final PointValuePair current = new PointValuePair(x, fVal); if (!stop && checker != null) { stop = checker.converged(iter, previous, current); } if (stop) { if (goal == GoalType.MINIMIZE) { return (fVal < fX) ? current : previous; } else { return (fVal > fX) ? current : previous; } } final double[] d = new double[n]; final double[] x2 = new double[n]; for (int i = 0; i < n; i++) { d[i] = x[i] - x1[i]; x2[i] = 2 * x[i] - x1[i]; } x1 = x.clone(); fX2 = computeObjectiveValue(x2); if (fX > fX2) { double t = 2 * (fX + fX2 - 2 * fVal); double temp = fX - fVal - delta; t *= temp * temp; temp = fX - fX2; t -= delta * temp * temp; if (t < 0.0) { final UnivariatePointValuePair optimum = line.search(x, d); fVal = optimum.getValue(); alphaMin = optimum.getPoint(); final double[][] result = newPointAndDirection(x, d, alphaMin); x = result[0]; final int lastInd = n - 1; direc[bigInd] = direc[lastInd]; direc[lastInd] = result[1]; } } } } /** * Compute a new point (in the original space) and a new direction * vector, resulting from the line search. * * @param p Point used in the line search. * @param d Direction used in the line search. * @param optimum Optimum found by the line search. * @return a 2-element array containing the new point (at index 0) and * the new direction (at index 1). */ private double[][] newPointAndDirection(double[] p, double[] d, double optimum) { final int n = p.length; final double[] nP = new double[n]; final double[] nD = new double[n]; for (int i = 0; i < n; i++) { nD[i] = d[i] * optimum; nP[i] = p[i] + nD[i]; } final double[][] result = new double[2][]; result[0] = nP; result[1] = nD; return result; } /** * Class for finding the minimum of the objective function along a given * direction. */ private class LineSearch extends BrentOptimizer { /** * Value that will pass the precondition check for {@link BrentOptimizer} * but will not pass the convergence check, so that the custom checker * will always decide when to stop the line search. */ private static final double REL_TOL_UNUSED = 1e-15; /** * Value that will pass the precondition check for {@link BrentOptimizer} * but will not pass the convergence check, so that the custom checker * will always decide when to stop the line search. */ private static final double ABS_TOL_UNUSED = Double.MIN_VALUE; /** * Automatic bracketing. */ private final BracketFinder bracket = new BracketFinder(); /** * The "BrentOptimizer" default stopping criterion uses the tolerances * to check the domain (point) values, not the function values. * We thus create a custom checker to use function values. * * @param rel Relative threshold. * @param abs Absolute threshold. */ LineSearch(double rel, double abs) { super(REL_TOL_UNUSED, ABS_TOL_UNUSED, new SimpleUnivariateValueChecker(rel, abs)); } /** * Find the minimum of the function {@code f(p + alpha * d)}. * * @param p Starting point. * @param d Search direction. * @return the optimum. * @throws org.apache.commons.math3.exception.TooManyEvaluationsException * if the number of evaluations is exceeded. */ public UnivariatePointValuePair search(final double[] p, final double[] d) { final int n = p.length; final UnivariateFunction f = new UnivariateFunction() { public double value(double alpha) { final double[] x = new double[n]; for (int i = 0; i < n; i++) { x[i] = p[i] + alpha * d[i]; } final double obj = PowellOptimizer.this.computeObjectiveValue(x); return obj; } }; final GoalType goal = PowellOptimizer.this.getGoalType(); bracket.search(f, goal, 0, 1); // Passing "MAX_VALUE" as a dummy value because it is the enclosing // class that counts the number of evaluations (and will eventually // generate the exception). return optimize(Integer.MAX_VALUE, f, goal, bracket.getLo(), bracket.getHi(), bracket.getMid()); } } } ././@LongLink100644 0 0 146 12126630646 10262 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/optimization/direct/AbstractSimplex.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/optimization/direct/AbstractSimplex.jav100644 1750 1750 31447 12126627712 32362 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.optimization.direct; import java.util.Arrays; import java.util.Comparator; import org.apache.commons.math3.analysis.MultivariateFunction; import org.apache.commons.math3.exception.NotStrictlyPositiveException; import org.apache.commons.math3.exception.DimensionMismatchException; import org.apache.commons.math3.exception.ZeroException; import org.apache.commons.math3.exception.OutOfRangeException; import org.apache.commons.math3.exception.NullArgumentException; import org.apache.commons.math3.exception.MathIllegalArgumentException; import org.apache.commons.math3.exception.util.LocalizedFormats; import org.apache.commons.math3.optimization.PointValuePair; import org.apache.commons.math3.optimization.OptimizationData; /** * This class implements the simplex concept. * It is intended to be used in conjunction with {@link SimplexOptimizer}. *
                          * The initial configuration of the simplex is set by the constructors * {@link #AbstractSimplex(double[])} or {@link #AbstractSimplex(double[][])}. * The other {@link #AbstractSimplex(int) constructor} will set all steps * to 1, thus building a default configuration from a unit hypercube. *
                          * Users must call the {@link #build(double[]) build} method in order * to create the data structure that will be acted on by the other methods of * this class. * * @see SimplexOptimizer * @version $Id: AbstractSimplex.java 1422230 2012-12-15 12:11:13Z erans $ * @deprecated As of 3.1 (to be removed in 4.0). * @since 3.0 */ @Deprecated public abstract class AbstractSimplex implements OptimizationData { /** Simplex. */ private PointValuePair[] simplex; /** Start simplex configuration. */ private double[][] startConfiguration; /** Simplex dimension (must be equal to {@code simplex.length - 1}). */ private final int dimension; /** * Build a unit hypercube simplex. * * @param n Dimension of the simplex. */ protected AbstractSimplex(int n) { this(n, 1d); } /** * Build a hypercube simplex with the given side length. * * @param n Dimension of the simplex. * @param sideLength Length of the sides of the hypercube. */ protected AbstractSimplex(int n, double sideLength) { this(createHypercubeSteps(n, sideLength)); } /** * The start configuration for simplex is built from a box parallel to * the canonical axes of the space. The simplex is the subset of vertices * of a box parallel to the canonical axes. It is built as the path followed * while traveling from one vertex of the box to the diagonally opposite * vertex moving only along the box edges. The first vertex of the box will * be located at the start point of the optimization. * As an example, in dimension 3 a simplex has 4 vertices. Setting the * steps to (1, 10, 2) and the start point to (1, 1, 1) would imply the * start simplex would be: { (1, 1, 1), (2, 1, 1), (2, 11, 1), (2, 11, 3) }. * The first vertex would be set to the start point at (1, 1, 1) and the * last vertex would be set to the diagonally opposite vertex at (2, 11, 3). * * @param steps Steps along the canonical axes representing box edges. They * may be negative but not zero. * @throws NullArgumentException if {@code steps} is {@code null}. * @throws ZeroException if one of the steps is zero. */ protected AbstractSimplex(final double[] steps) { if (steps == null) { throw new NullArgumentException(); } if (steps.length == 0) { throw new ZeroException(); } dimension = steps.length; // Only the relative position of the n final vertices with respect // to the first one are stored. startConfiguration = new double[dimension][dimension]; for (int i = 0; i < dimension; i++) { final double[] vertexI = startConfiguration[i]; for (int j = 0; j < i + 1; j++) { if (steps[j] == 0) { throw new ZeroException(LocalizedFormats.EQUAL_VERTICES_IN_SIMPLEX); } System.arraycopy(steps, 0, vertexI, 0, j + 1); } } } /** * The real initial simplex will be set up by moving the reference * simplex such that its first point is located at the start point of the * optimization. * * @param referenceSimplex Reference simplex. * @throws NotStrictlyPositiveException if the reference simplex does not * contain at least one point. * @throws DimensionMismatchException if there is a dimension mismatch * in the reference simplex. * @throws IllegalArgumentException if one of its vertices is duplicated. */ protected AbstractSimplex(final double[][] referenceSimplex) { if (referenceSimplex.length <= 0) { throw new NotStrictlyPositiveException(LocalizedFormats.SIMPLEX_NEED_ONE_POINT, referenceSimplex.length); } dimension = referenceSimplex.length - 1; // Only the relative position of the n final vertices with respect // to the first one are stored. startConfiguration = new double[dimension][dimension]; final double[] ref0 = referenceSimplex[0]; // Loop over vertices. for (int i = 0; i < referenceSimplex.length; i++) { final double[] refI = referenceSimplex[i]; // Safety checks. if (refI.length != dimension) { throw new DimensionMismatchException(refI.length, dimension); } for (int j = 0; j < i; j++) { final double[] refJ = referenceSimplex[j]; boolean allEquals = true; for (int k = 0; k < dimension; k++) { if (refI[k] != refJ[k]) { allEquals = false; break; } } if (allEquals) { throw new MathIllegalArgumentException(LocalizedFormats.EQUAL_VERTICES_IN_SIMPLEX, i, j); } } // Store vertex i position relative to vertex 0 position. if (i > 0) { final double[] confI = startConfiguration[i - 1]; for (int k = 0; k < dimension; k++) { confI[k] = refI[k] - ref0[k]; } } } } /** * Get simplex dimension. * * @return the dimension of the simplex. */ public int getDimension() { return dimension; } /** * Get simplex size. * After calling the {@link #build(double[]) build} method, this method will * will be equivalent to {@code getDimension() + 1}. * * @return the size of the simplex. */ public int getSize() { return simplex.length; } /** * Compute the next simplex of the algorithm. * * @param evaluationFunction Evaluation function. * @param comparator Comparator to use to sort simplex vertices from best * to worst. * @throws org.apache.commons.math3.exception.TooManyEvaluationsException * if the algorithm fails to converge. */ public abstract void iterate(final MultivariateFunction evaluationFunction, final Comparator comparator); /** * Build an initial simplex. * * @param startPoint First point of the simplex. * @throws DimensionMismatchException if the start point does not match * simplex dimension. */ public void build(final double[] startPoint) { if (dimension != startPoint.length) { throw new DimensionMismatchException(dimension, startPoint.length); } // Set first vertex. simplex = new PointValuePair[dimension + 1]; simplex[0] = new PointValuePair(startPoint, Double.NaN); // Set remaining vertices. for (int i = 0; i < dimension; i++) { final double[] confI = startConfiguration[i]; final double[] vertexI = new double[dimension]; for (int k = 0; k < dimension; k++) { vertexI[k] = startPoint[k] + confI[k]; } simplex[i + 1] = new PointValuePair(vertexI, Double.NaN); } } /** * Evaluate all the non-evaluated points of the simplex. * * @param evaluationFunction Evaluation function. * @param comparator Comparator to use to sort simplex vertices from best to worst. * @throws org.apache.commons.math3.exception.TooManyEvaluationsException * if the maximal number of evaluations is exceeded. */ public void evaluate(final MultivariateFunction evaluationFunction, final Comparator comparator) { // Evaluate the objective function at all non-evaluated simplex points. for (int i = 0; i < simplex.length; i++) { final PointValuePair vertex = simplex[i]; final double[] point = vertex.getPointRef(); if (Double.isNaN(vertex.getValue())) { simplex[i] = new PointValuePair(point, evaluationFunction.value(point), false); } } // Sort the simplex from best to worst. Arrays.sort(simplex, comparator); } /** * Replace the worst point of the simplex by a new point. * * @param pointValuePair Point to insert. * @param comparator Comparator to use for sorting the simplex vertices * from best to worst. */ protected void replaceWorstPoint(PointValuePair pointValuePair, final Comparator comparator) { for (int i = 0; i < dimension; i++) { if (comparator.compare(simplex[i], pointValuePair) > 0) { PointValuePair tmp = simplex[i]; simplex[i] = pointValuePair; pointValuePair = tmp; } } simplex[dimension] = pointValuePair; } /** * Get the points of the simplex. * * @return all the simplex points. */ public PointValuePair[] getPoints() { final PointValuePair[] copy = new PointValuePair[simplex.length]; System.arraycopy(simplex, 0, copy, 0, simplex.length); return copy; } /** * Get the simplex point stored at the requested {@code index}. * * @param index Location. * @return the point at location {@code index}. */ public PointValuePair getPoint(int index) { if (index < 0 || index >= simplex.length) { throw new OutOfRangeException(index, 0, simplex.length - 1); } return simplex[index]; } /** * Store a new point at location {@code index}. * Note that no deep-copy of {@code point} is performed. * * @param index Location. * @param point New value. */ protected void setPoint(int index, PointValuePair point) { if (index < 0 || index >= simplex.length) { throw new OutOfRangeException(index, 0, simplex.length - 1); } simplex[index] = point; } /** * Replace all points. * Note that no deep-copy of {@code points} is performed. * * @param points New Points. */ protected void setPoints(PointValuePair[] points) { if (points.length != simplex.length) { throw new DimensionMismatchException(points.length, simplex.length); } simplex = points; } /** * Create steps for a unit hypercube. * * @param n Dimension of the hypercube. * @param sideLength Length of the sides of the hypercube. * @return the steps. */ private static double[] createHypercubeSteps(int n, double sideLength) { final double[] steps = new double[n]; for (int i = 0; i < n; i++) { steps[i] = sideLength; } return steps; } } ././@LongLink100644 0 0 163 12126630646 10261 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/optimization/MultivariateDifferentiableOptimizer.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/optimization/MultivariateDifferentiable100644 1750 1750 3123 12126627713 32465 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.optimization; import org.apache.commons.math3.analysis.differentiation.MultivariateDifferentiableFunction; /** * This interface represents an optimization algorithm for * {@link MultivariateDifferentiableFunction scalar differentiable objective * functions}. * Optimization algorithms find the input point set that either {@link GoalType * maximize or minimize} an objective function. * * @see MultivariateOptimizer * @see MultivariateDifferentiableVectorOptimizer * * @version $Id: MultivariateDifferentiableOptimizer.java 1422230 2012-12-15 12:11:13Z erans $ * @deprecated As of 3.1 (to be removed in 4.0). * @since 3.1 */ @Deprecated public interface MultivariateDifferentiableOptimizer extends BaseMultivariateOptimizer {} commons-math3-3.2-src/src/main/java/org/apache/commons/math3/optimization/fitting/package-info.java100644 1750 1750 2446 12126627711 32110 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ /** * * This package provides classes to perform curve fitting. * *

                          Curve fitting is a special case of a least squares problem * were the parameters are the coefficients of a function f * whose graph y=f(x) should pass through sample points, and * were the objective function is the squared sum of residuals * f(xi)-yi for observed points * (xi, yi).

                          * * */ package org.apache.commons.math3.optimization.fitting; ././@LongLink100644 0 0 146 12126630646 10262 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/optimization/fitting/GaussianFitter.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/optimization/fitting/GaussianFitter.jav100644 1750 1750 34366 12126627711 32401 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.optimization.fitting; import java.util.Arrays; import java.util.Comparator; import org.apache.commons.math3.analysis.function.Gaussian; import org.apache.commons.math3.exception.NullArgumentException; import org.apache.commons.math3.exception.NumberIsTooSmallException; import org.apache.commons.math3.exception.OutOfRangeException; import org.apache.commons.math3.exception.ZeroException; import org.apache.commons.math3.exception.NotStrictlyPositiveException; import org.apache.commons.math3.exception.util.LocalizedFormats; import org.apache.commons.math3.optimization.DifferentiableMultivariateVectorOptimizer; import org.apache.commons.math3.util.FastMath; /** * Fits points to a {@link * org.apache.commons.math3.analysis.function.Gaussian.Parametric Gaussian} function. *

                          * Usage example: *

                           *   GaussianFitter fitter = new GaussianFitter(
                           *     new LevenbergMarquardtOptimizer());
                           *   fitter.addObservedPoint(4.0254623,  531026.0);
                           *   fitter.addObservedPoint(4.03128248, 984167.0);
                           *   fitter.addObservedPoint(4.03839603, 1887233.0);
                           *   fitter.addObservedPoint(4.04421621, 2687152.0);
                           *   fitter.addObservedPoint(4.05132976, 3461228.0);
                           *   fitter.addObservedPoint(4.05326982, 3580526.0);
                           *   fitter.addObservedPoint(4.05779662, 3439750.0);
                           *   fitter.addObservedPoint(4.0636168,  2877648.0);
                           *   fitter.addObservedPoint(4.06943698, 2175960.0);
                           *   fitter.addObservedPoint(4.07525716, 1447024.0);
                           *   fitter.addObservedPoint(4.08237071, 717104.0);
                           *   fitter.addObservedPoint(4.08366408, 620014.0);
                           *   double[] parameters = fitter.fit();
                           * 
                          * * @since 2.2 * @version $Id: GaussianFitter.java 1422230 2012-12-15 12:11:13Z erans $ * @deprecated As of 3.1 (to be removed in 4.0). */ @Deprecated public class GaussianFitter extends CurveFitter { /** * Constructs an instance using the specified optimizer. * * @param optimizer Optimizer to use for the fitting. */ public GaussianFitter(DifferentiableMultivariateVectorOptimizer optimizer) { super(optimizer); } /** * Fits a Gaussian function to the observed points. * * @param initialGuess First guess values in the following order: *
                            *
                          • Norm
                          • *
                          • Mean
                          • *
                          • Sigma
                          • *
                          * @return the parameters of the Gaussian function that best fits the * observed points (in the same order as above). * @since 3.0 */ public double[] fit(double[] initialGuess) { final Gaussian.Parametric f = new Gaussian.Parametric() { @Override public double value(double x, double ... p) { double v = Double.POSITIVE_INFINITY; try { v = super.value(x, p); } catch (NotStrictlyPositiveException e) { // NOPMD // Do nothing. } return v; } @Override public double[] gradient(double x, double ... p) { double[] v = { Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY }; try { v = super.gradient(x, p); } catch (NotStrictlyPositiveException e) { // NOPMD // Do nothing. } return v; } }; return fit(f, initialGuess); } /** * Fits a Gaussian function to the observed points. * * @return the parameters of the Gaussian function that best fits the * observed points (in the same order as above). */ public double[] fit() { final double[] guess = (new ParameterGuesser(getObservations())).guess(); return fit(guess); } /** * Guesses the parameters {@code norm}, {@code mean}, and {@code sigma} * of a {@link org.apache.commons.math3.analysis.function.Gaussian.Parametric} * based on the specified observed points. */ public static class ParameterGuesser { /** Normalization factor. */ private final double norm; /** Mean. */ private final double mean; /** Standard deviation. */ private final double sigma; /** * Constructs instance with the specified observed points. * * @param observations Observed points from which to guess the * parameters of the Gaussian. * @throws NullArgumentException if {@code observations} is * {@code null}. * @throws NumberIsTooSmallException if there are less than 3 * observations. */ public ParameterGuesser(WeightedObservedPoint[] observations) { if (observations == null) { throw new NullArgumentException(LocalizedFormats.INPUT_ARRAY); } if (observations.length < 3) { throw new NumberIsTooSmallException(observations.length, 3, true); } final WeightedObservedPoint[] sorted = sortObservations(observations); final double[] params = basicGuess(sorted); norm = params[0]; mean = params[1]; sigma = params[2]; } /** * Gets an estimation of the parameters. * * @return the guessed parameters, in the following order: *
                            *
                          • Normalization factor
                          • *
                          • Mean
                          • *
                          • Standard deviation
                          • *
                          */ public double[] guess() { return new double[] { norm, mean, sigma }; } /** * Sort the observations. * * @param unsorted Input observations. * @return the input observations, sorted. */ private WeightedObservedPoint[] sortObservations(WeightedObservedPoint[] unsorted) { final WeightedObservedPoint[] observations = unsorted.clone(); final Comparator cmp = new Comparator() { public int compare(WeightedObservedPoint p1, WeightedObservedPoint p2) { if (p1 == null && p2 == null) { return 0; } if (p1 == null) { return -1; } if (p2 == null) { return 1; } if (p1.getX() < p2.getX()) { return -1; } if (p1.getX() > p2.getX()) { return 1; } if (p1.getY() < p2.getY()) { return -1; } if (p1.getY() > p2.getY()) { return 1; } if (p1.getWeight() < p2.getWeight()) { return -1; } if (p1.getWeight() > p2.getWeight()) { return 1; } return 0; } }; Arrays.sort(observations, cmp); return observations; } /** * Guesses the parameters based on the specified observed points. * * @param points Observed points, sorted. * @return the guessed parameters (normalization factor, mean and * sigma). */ private double[] basicGuess(WeightedObservedPoint[] points) { final int maxYIdx = findMaxY(points); final double n = points[maxYIdx].getY(); final double m = points[maxYIdx].getX(); double fwhmApprox; try { final double halfY = n + ((m - n) / 2); final double fwhmX1 = interpolateXAtY(points, maxYIdx, -1, halfY); final double fwhmX2 = interpolateXAtY(points, maxYIdx, 1, halfY); fwhmApprox = fwhmX2 - fwhmX1; } catch (OutOfRangeException e) { // TODO: Exceptions should not be used for flow control. fwhmApprox = points[points.length - 1].getX() - points[0].getX(); } final double s = fwhmApprox / (2 * FastMath.sqrt(2 * FastMath.log(2))); return new double[] { n, m, s }; } /** * Finds index of point in specified points with the largest Y. * * @param points Points to search. * @return the index in specified points array. */ private int findMaxY(WeightedObservedPoint[] points) { int maxYIdx = 0; for (int i = 1; i < points.length; i++) { if (points[i].getY() > points[maxYIdx].getY()) { maxYIdx = i; } } return maxYIdx; } /** * Interpolates using the specified points to determine X at the * specified Y. * * @param points Points to use for interpolation. * @param startIdx Index within points from which to start the search for * interpolation bounds points. * @param idxStep Index step for searching interpolation bounds points. * @param y Y value for which X should be determined. * @return the value of X for the specified Y. * @throws ZeroException if {@code idxStep} is 0. * @throws OutOfRangeException if specified {@code y} is not within the * range of the specified {@code points}. */ private double interpolateXAtY(WeightedObservedPoint[] points, int startIdx, int idxStep, double y) throws OutOfRangeException { if (idxStep == 0) { throw new ZeroException(); } final WeightedObservedPoint[] twoPoints = getInterpolationPointsForY(points, startIdx, idxStep, y); final WeightedObservedPoint p1 = twoPoints[0]; final WeightedObservedPoint p2 = twoPoints[1]; if (p1.getY() == y) { return p1.getX(); } if (p2.getY() == y) { return p2.getX(); } return p1.getX() + (((y - p1.getY()) * (p2.getX() - p1.getX())) / (p2.getY() - p1.getY())); } /** * Gets the two bounding interpolation points from the specified points * suitable for determining X at the specified Y. * * @param points Points to use for interpolation. * @param startIdx Index within points from which to start search for * interpolation bounds points. * @param idxStep Index step for search for interpolation bounds points. * @param y Y value for which X should be determined. * @return the array containing two points suitable for determining X at * the specified Y. * @throws ZeroException if {@code idxStep} is 0. * @throws OutOfRangeException if specified {@code y} is not within the * range of the specified {@code points}. */ private WeightedObservedPoint[] getInterpolationPointsForY(WeightedObservedPoint[] points, int startIdx, int idxStep, double y) throws OutOfRangeException { if (idxStep == 0) { throw new ZeroException(); } for (int i = startIdx; idxStep < 0 ? i + idxStep >= 0 : i + idxStep < points.length; i += idxStep) { final WeightedObservedPoint p1 = points[i]; final WeightedObservedPoint p2 = points[i + idxStep]; if (isBetween(y, p1.getY(), p2.getY())) { if (idxStep < 0) { return new WeightedObservedPoint[] { p2, p1 }; } else { return new WeightedObservedPoint[] { p1, p2 }; } } } // Boundaries are replaced by dummy values because the raised // exception is caught and the message never displayed. // TODO: Exceptions should not be used for flow control. throw new OutOfRangeException(y, Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY); } /** * Determines whether a value is between two other values. * * @param value Value to test whether it is between {@code boundary1} * and {@code boundary2}. * @param boundary1 One end of the range. * @param boundary2 Other end of the range. * @return {@code true} if {@code value} is between {@code boundary1} and * {@code boundary2} (inclusive), {@code false} otherwise. */ private boolean isBetween(double value, double boundary1, double boundary2) { return (value >= boundary1 && value <= boundary2) || (value >= boundary2 && value <= boundary1); } } } ././@LongLink100644 0 0 155 12126630646 10262 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/optimization/fitting/WeightedObservedPoint.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/optimization/fitting/WeightedObservedPo100644 1750 1750 4703 12126627711 32373 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.optimization.fitting; import java.io.Serializable; /** This class is a simple container for weighted observed point in * {@link CurveFitter curve fitting}. *

                          Instances of this class are guaranteed to be immutable.

                          * @version $Id: WeightedObservedPoint.java 1422230 2012-12-15 12:11:13Z erans $ * @deprecated As of 3.1 (to be removed in 4.0). * @since 2.0 */ @Deprecated public class WeightedObservedPoint implements Serializable { /** Serializable version id. */ private static final long serialVersionUID = 5306874947404636157L; /** Weight of the measurement in the fitting process. */ private final double weight; /** Abscissa of the point. */ private final double x; /** Observed value of the function at x. */ private final double y; /** Simple constructor. * @param weight weight of the measurement in the fitting process * @param x abscissa of the measurement * @param y ordinate of the measurement */ public WeightedObservedPoint(final double weight, final double x, final double y) { this.weight = weight; this.x = x; this.y = y; } /** Get the weight of the measurement in the fitting process. * @return weight of the measurement in the fitting process */ public double getWeight() { return weight; } /** Get the abscissa of the point. * @return abscissa of the point */ public double getX() { return x; } /** Get the observed value of the function at x. * @return observed value of the function at x */ public double getY() { return y; } } ././@LongLink100644 0 0 150 12126630646 10255 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/optimization/fitting/PolynomialFitter.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/optimization/fitting/PolynomialFitter.j100644 1750 1750 10674 12126627711 32417 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.optimization.fitting; import org.apache.commons.math3.analysis.polynomials.PolynomialFunction; import org.apache.commons.math3.optimization.DifferentiableMultivariateVectorOptimizer; /** * Polynomial fitting is a very simple case of {@link CurveFitter curve fitting}. * The estimated coefficients are the polynomial coefficients (see the * {@link #fit(double[]) fit} method). * * @version $Id: PolynomialFitter.java 1422313 2012-12-15 18:53:41Z psteitz $ * @deprecated As of 3.1 (to be removed in 4.0). * @since 2.0 */ @Deprecated public class PolynomialFitter extends CurveFitter { /** Polynomial degree. * @deprecated */ @Deprecated private final int degree; /** * Simple constructor. *

                          The polynomial fitter built this way are complete polynomials, * ie. a n-degree polynomial has n+1 coefficients.

                          * * @param degree Maximal degree of the polynomial. * @param optimizer Optimizer to use for the fitting. * @deprecated Since 3.1 (to be removed in 4.0). Please use * {@link #PolynomialFitter(DifferentiableMultivariateVectorOptimizer)} instead. */ @Deprecated public PolynomialFitter(int degree, final DifferentiableMultivariateVectorOptimizer optimizer) { super(optimizer); this.degree = degree; } /** * Simple constructor. * * @param optimizer Optimizer to use for the fitting. * @since 3.1 */ public PolynomialFitter(DifferentiableMultivariateVectorOptimizer optimizer) { super(optimizer); degree = -1; // To avoid compilation error until the instance variable is removed. } /** * Get the polynomial fitting the weighted (x, y) points. * * @return the coefficients of the polynomial that best fits the observed points. * @throws org.apache.commons.math3.exception.ConvergenceException * if the algorithm failed to converge. * @deprecated Since 3.1 (to be removed in 4.0). Please use {@link #fit(double[])} instead. */ @Deprecated public double[] fit() { return fit(new PolynomialFunction.Parametric(), new double[degree + 1]); } /** * Get the coefficients of the polynomial fitting the weighted data points. * The degree of the fitting polynomial is {@code guess.length - 1}. * * @param guess First guess for the coefficients. They must be sorted in * increasing order of the polynomial's degree. * @param maxEval Maximum number of evaluations of the polynomial. * @return the coefficients of the polynomial that best fits the observed points. * @throws org.apache.commons.math3.exception.TooManyEvaluationsException if * the number of evaluations exceeds {@code maxEval}. * @throws org.apache.commons.math3.exception.ConvergenceException * if the algorithm failed to converge. * @since 3.1 */ public double[] fit(int maxEval, double[] guess) { return fit(maxEval, new PolynomialFunction.Parametric(), guess); } /** * Get the coefficients of the polynomial fitting the weighted data points. * The degree of the fitting polynomial is {@code guess.length - 1}. * * @param guess First guess for the coefficients. They must be sorted in * increasing order of the polynomial's degree. * @return the coefficients of the polynomial that best fits the observed points. * @throws org.apache.commons.math3.exception.ConvergenceException * if the algorithm failed to converge. * @since 3.1 */ public double[] fit(double[] guess) { return fit(new PolynomialFunction.Parametric(), guess); } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/optimization/fitting/CurveFitter.java100644 1750 1750 26272 12126627711 32051 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.optimization.fitting; import java.util.ArrayList; import java.util.List; import org.apache.commons.math3.analysis.DifferentiableMultivariateVectorFunction; import org.apache.commons.math3.analysis.MultivariateMatrixFunction; import org.apache.commons.math3.analysis.ParametricUnivariateFunction; import org.apache.commons.math3.analysis.differentiation.DerivativeStructure; import org.apache.commons.math3.analysis.differentiation.MultivariateDifferentiableVectorFunction; import org.apache.commons.math3.optimization.DifferentiableMultivariateVectorOptimizer; import org.apache.commons.math3.optimization.MultivariateDifferentiableVectorOptimizer; import org.apache.commons.math3.optimization.PointVectorValuePair; /** Fitter for parametric univariate real functions y = f(x). *
                          * When a univariate real function y = f(x) does depend on some * unknown parameters p0, p1 ... pn-1, * this class can be used to find these parameters. It does this * by fitting the curve so it remains very close to a set of * observed points (x0, y0), (x1, * y1) ... (xk-1, yk-1). This fitting * is done by finding the parameters values that minimizes the objective * function ∑(yi-f(xi))2. This is * really a least squares problem. * * @param Function to use for the fit. * * @version $Id: CurveFitter.java 1422230 2012-12-15 12:11:13Z erans $ * @deprecated As of 3.1 (to be removed in 4.0). * @since 2.0 */ @Deprecated public class CurveFitter { /** Optimizer to use for the fitting. * @deprecated as of 3.1 replaced by {@link #optimizer} */ @Deprecated private final DifferentiableMultivariateVectorOptimizer oldOptimizer; /** Optimizer to use for the fitting. */ private final MultivariateDifferentiableVectorOptimizer optimizer; /** Observed points. */ private final List observations; /** Simple constructor. * @param optimizer optimizer to use for the fitting * @deprecated as of 3.1 replaced by {@link #CurveFitter(MultivariateDifferentiableVectorOptimizer)} */ public CurveFitter(final DifferentiableMultivariateVectorOptimizer optimizer) { this.oldOptimizer = optimizer; this.optimizer = null; observations = new ArrayList(); } /** Simple constructor. * @param optimizer optimizer to use for the fitting * @since 3.1 */ public CurveFitter(final MultivariateDifferentiableVectorOptimizer optimizer) { this.oldOptimizer = null; this.optimizer = optimizer; observations = new ArrayList(); } /** Add an observed (x,y) point to the sample with unit weight. *

                          Calling this method is equivalent to call * {@code addObservedPoint(1.0, x, y)}.

                          * @param x abscissa of the point * @param y observed value of the point at x, after fitting we should * have f(x) as close as possible to this value * @see #addObservedPoint(double, double, double) * @see #addObservedPoint(WeightedObservedPoint) * @see #getObservations() */ public void addObservedPoint(double x, double y) { addObservedPoint(1.0, x, y); } /** Add an observed weighted (x,y) point to the sample. * @param weight weight of the observed point in the fit * @param x abscissa of the point * @param y observed value of the point at x, after fitting we should * have f(x) as close as possible to this value * @see #addObservedPoint(double, double) * @see #addObservedPoint(WeightedObservedPoint) * @see #getObservations() */ public void addObservedPoint(double weight, double x, double y) { observations.add(new WeightedObservedPoint(weight, x, y)); } /** Add an observed weighted (x,y) point to the sample. * @param observed observed point to add * @see #addObservedPoint(double, double) * @see #addObservedPoint(double, double, double) * @see #getObservations() */ public void addObservedPoint(WeightedObservedPoint observed) { observations.add(observed); } /** Get the observed points. * @return observed points * @see #addObservedPoint(double, double) * @see #addObservedPoint(double, double, double) * @see #addObservedPoint(WeightedObservedPoint) */ public WeightedObservedPoint[] getObservations() { return observations.toArray(new WeightedObservedPoint[observations.size()]); } /** * Remove all observations. */ public void clearObservations() { observations.clear(); } /** * Fit a curve. * This method compute the coefficients of the curve that best * fit the sample of observed points previously given through calls * to the {@link #addObservedPoint(WeightedObservedPoint) * addObservedPoint} method. * * @param f parametric function to fit. * @param initialGuess first guess of the function parameters. * @return the fitted parameters. * @throws org.apache.commons.math3.exception.DimensionMismatchException * if the start point dimension is wrong. */ public double[] fit(T f, final double[] initialGuess) { return fit(Integer.MAX_VALUE, f, initialGuess); } /** * Fit a curve. * This method compute the coefficients of the curve that best * fit the sample of observed points previously given through calls * to the {@link #addObservedPoint(WeightedObservedPoint) * addObservedPoint} method. * * @param f parametric function to fit. * @param initialGuess first guess of the function parameters. * @param maxEval Maximum number of function evaluations. * @return the fitted parameters. * @throws org.apache.commons.math3.exception.TooManyEvaluationsException * if the number of allowed evaluations is exceeded. * @throws org.apache.commons.math3.exception.DimensionMismatchException * if the start point dimension is wrong. * @since 3.0 */ public double[] fit(int maxEval, T f, final double[] initialGuess) { // prepare least squares problem double[] target = new double[observations.size()]; double[] weights = new double[observations.size()]; int i = 0; for (WeightedObservedPoint point : observations) { target[i] = point.getY(); weights[i] = point.getWeight(); ++i; } // perform the fit final PointVectorValuePair optimum; if (optimizer == null) { // to be removed in 4.0 optimum = oldOptimizer.optimize(maxEval, new OldTheoreticalValuesFunction(f), target, weights, initialGuess); } else { optimum = optimizer.optimize(maxEval, new TheoreticalValuesFunction(f), target, weights, initialGuess); } // extract the coefficients return optimum.getPointRef(); } /** Vectorial function computing function theoretical values. */ @Deprecated private class OldTheoreticalValuesFunction implements DifferentiableMultivariateVectorFunction { /** Function to fit. */ private final ParametricUnivariateFunction f; /** Simple constructor. * @param f function to fit. */ public OldTheoreticalValuesFunction(final ParametricUnivariateFunction f) { this.f = f; } /** {@inheritDoc} */ public MultivariateMatrixFunction jacobian() { return new MultivariateMatrixFunction() { public double[][] value(double[] point) { final double[][] jacobian = new double[observations.size()][]; int i = 0; for (WeightedObservedPoint observed : observations) { jacobian[i++] = f.gradient(observed.getX(), point); } return jacobian; } }; } /** {@inheritDoc} */ public double[] value(double[] point) { // compute the residuals final double[] values = new double[observations.size()]; int i = 0; for (WeightedObservedPoint observed : observations) { values[i++] = f.value(observed.getX(), point); } return values; } } /** Vectorial function computing function theoretical values. */ private class TheoreticalValuesFunction implements MultivariateDifferentiableVectorFunction { /** Function to fit. */ private final ParametricUnivariateFunction f; /** Simple constructor. * @param f function to fit. */ public TheoreticalValuesFunction(final ParametricUnivariateFunction f) { this.f = f; } /** {@inheritDoc} */ public double[] value(double[] point) { // compute the residuals final double[] values = new double[observations.size()]; int i = 0; for (WeightedObservedPoint observed : observations) { values[i++] = f.value(observed.getX(), point); } return values; } /** {@inheritDoc} */ public DerivativeStructure[] value(DerivativeStructure[] point) { // extract parameters final double[] parameters = new double[point.length]; for (int k = 0; k < point.length; ++k) { parameters[k] = point[k].getValue(); } // compute the residuals final DerivativeStructure[] values = new DerivativeStructure[observations.size()]; int i = 0; for (WeightedObservedPoint observed : observations) { // build the DerivativeStructure by adding first the value as a constant // and then adding derivatives DerivativeStructure vi = new DerivativeStructure(point.length, 1, f.value(observed.getX(), parameters)); for (int k = 0; k < point.length; ++k) { vi = vi.add(new DerivativeStructure(point.length, 1, k, 0.0)); } values[i++] = vi; } return values; } } } ././@LongLink100644 0 0 146 12126630646 10262 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/optimization/fitting/HarmonicFitter.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/optimization/fitting/HarmonicFitter.jav100644 1750 1750 40255 12126627711 32361 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.optimization.fitting; import org.apache.commons.math3.optimization.DifferentiableMultivariateVectorOptimizer; import org.apache.commons.math3.analysis.function.HarmonicOscillator; import org.apache.commons.math3.exception.ZeroException; import org.apache.commons.math3.exception.NumberIsTooSmallException; import org.apache.commons.math3.exception.MathIllegalStateException; import org.apache.commons.math3.exception.util.LocalizedFormats; import org.apache.commons.math3.util.FastMath; /** * Class that implements a curve fitting specialized for sinusoids. * * Harmonic fitting is a very simple case of curve fitting. The * estimated coefficients are the amplitude a, the pulsation ω and * the phase φ: f (t) = a cos (ω t + φ). They are * searched by a least square estimator initialized with a rough guess * based on integrals. * * @version $Id: HarmonicFitter.java 1422230 2012-12-15 12:11:13Z erans $ * @deprecated As of 3.1 (to be removed in 4.0). * @since 2.0 */ @Deprecated public class HarmonicFitter extends CurveFitter { /** * Simple constructor. * @param optimizer Optimizer to use for the fitting. */ public HarmonicFitter(final DifferentiableMultivariateVectorOptimizer optimizer) { super(optimizer); } /** * Fit an harmonic function to the observed points. * * @param initialGuess First guess values in the following order: *
                            *
                          • Amplitude
                          • *
                          • Angular frequency
                          • *
                          • Phase
                          • *
                          * @return the parameters of the harmonic function that best fits the * observed points (in the same order as above). */ public double[] fit(double[] initialGuess) { return fit(new HarmonicOscillator.Parametric(), initialGuess); } /** * Fit an harmonic function to the observed points. * An initial guess will be automatically computed. * * @return the parameters of the harmonic function that best fits the * observed points (see the other {@link #fit(double[]) fit} method. * @throws NumberIsTooSmallException if the sample is too short for the * the first guess to be computed. * @throws ZeroException if the first guess cannot be computed because * the abscissa range is zero. */ public double[] fit() { return fit((new ParameterGuesser(getObservations())).guess()); } /** * This class guesses harmonic coefficients from a sample. *

                          The algorithm used to guess the coefficients is as follows:

                          * *

                          We know f (t) at some sampling points ti and want to find a, * ω and φ such that f (t) = a cos (ω t + φ). *

                          * *

                          From the analytical expression, we can compute two primitives : *

                               *     If2  (t) = ∫ f2  = a2 × [t + S (t)] / 2
                               *     If'2 (t) = ∫ f'2 = a2 ω2 × [t - S (t)] / 2
                               *     where S (t) = sin (2 (ω t + φ)) / (2 ω)
                               * 
                          *

                          * *

                          We can remove S between these expressions : *

                               *     If'2 (t) = a2 ω2 t - ω2 If2 (t)
                               * 
                          *

                          * *

                          The preceding expression shows that If'2 (t) is a linear * combination of both t and If2 (t): If'2 (t) = A × t + B × If2 (t) *

                          * *

                          From the primitive, we can deduce the same form for definite * integrals between t1 and ti for each ti : *

                               *   If2 (ti) - If2 (t1) = A × (ti - t1) + B × (If2 (ti) - If2 (t1))
                               * 
                          *

                          * *

                          We can find the coefficients A and B that best fit the sample * to this linear expression by computing the definite integrals for * each sample points. *

                          * *

                          For a bilinear expression z (xi, yi) = A × xi + B × yi, the * coefficients A and B that minimize a least square criterion * ∑ (zi - z (xi, yi))2 are given by these expressions:

                          *
                               *
                               *         ∑yiyi ∑xizi - ∑xiyi ∑yizi
                               *     A = ------------------------
                               *         ∑xixi ∑yiyi - ∑xiyi ∑xiyi
                               *
                               *         ∑xixi ∑yizi - ∑xiyi ∑xizi
                               *     B = ------------------------
                               *         ∑xixi ∑yiyi - ∑xiyi ∑xiyi
                               * 
                          *

                          * * *

                          In fact, we can assume both a and ω are positive and * compute them directly, knowing that A = a2 ω2 and that * B = - ω2. The complete algorithm is therefore:

                          *
                               *
                               * for each ti from t1 to tn-1, compute:
                               *   f  (ti)
                               *   f' (ti) = (f (ti+1) - f(ti-1)) / (ti+1 - ti-1)
                               *   xi = ti - t1
                               *   yi = ∫ f2 from t1 to ti
                               *   zi = ∫ f'2 from t1 to ti
                               *   update the sums ∑xixi, ∑yiyi, ∑xiyi, ∑xizi and ∑yizi
                               * end for
                               *
                               *            |--------------------------
                               *         \  | ∑yiyi ∑xizi - ∑xiyi ∑yizi
                               * a     =  \ | ------------------------
                               *           \| ∑xiyi ∑xizi - ∑xixi ∑yizi
                               *
                               *
                               *            |--------------------------
                               *         \  | ∑xiyi ∑xizi - ∑xixi ∑yizi
                               * ω     =  \ | ------------------------
                               *           \| ∑xixi ∑yiyi - ∑xiyi ∑xiyi
                               *
                               * 
                          *

                          * *

                          Once we know ω, we can compute: *

                               *    fc = ω f (t) cos (ω t) - f' (t) sin (ω t)
                               *    fs = ω f (t) sin (ω t) + f' (t) cos (ω t)
                               * 
                          *

                          * *

                          It appears that fc = a ω cos (φ) and * fs = -a ω sin (φ), so we can use these * expressions to compute φ. The best estimate over the sample is * given by averaging these expressions. *

                          * *

                          Since integrals and means are involved in the preceding * estimations, these operations run in O(n) time, where n is the * number of measurements.

                          */ public static class ParameterGuesser { /** Amplitude. */ private final double a; /** Angular frequency. */ private final double omega; /** Phase. */ private final double phi; /** * Simple constructor. * * @param observations Sampled observations. * @throws NumberIsTooSmallException if the sample is too short. * @throws ZeroException if the abscissa range is zero. * @throws MathIllegalStateException when the guessing procedure cannot * produce sensible results. */ public ParameterGuesser(WeightedObservedPoint[] observations) { if (observations.length < 4) { throw new NumberIsTooSmallException(LocalizedFormats.INSUFFICIENT_OBSERVED_POINTS_IN_SAMPLE, observations.length, 4, true); } final WeightedObservedPoint[] sorted = sortObservations(observations); final double aOmega[] = guessAOmega(sorted); a = aOmega[0]; omega = aOmega[1]; phi = guessPhi(sorted); } /** * Gets an estimation of the parameters. * * @return the guessed parameters, in the following order: *
                            *
                          • Amplitude
                          • *
                          • Angular frequency
                          • *
                          • Phase
                          • *
                          */ public double[] guess() { return new double[] { a, omega, phi }; } /** * Sort the observations with respect to the abscissa. * * @param unsorted Input observations. * @return the input observations, sorted. */ private WeightedObservedPoint[] sortObservations(WeightedObservedPoint[] unsorted) { final WeightedObservedPoint[] observations = unsorted.clone(); // Since the samples are almost always already sorted, this // method is implemented as an insertion sort that reorders the // elements in place. Insertion sort is very efficient in this case. WeightedObservedPoint curr = observations[0]; for (int j = 1; j < observations.length; ++j) { WeightedObservedPoint prec = curr; curr = observations[j]; if (curr.getX() < prec.getX()) { // the current element should be inserted closer to the beginning int i = j - 1; WeightedObservedPoint mI = observations[i]; while ((i >= 0) && (curr.getX() < mI.getX())) { observations[i + 1] = mI; if (i-- != 0) { mI = observations[i]; } } observations[i + 1] = curr; curr = observations[j]; } } return observations; } /** * Estimate a first guess of the amplitude and angular frequency. * This method assumes that the {@link #sortObservations()} method * has been called previously. * * @param observations Observations, sorted w.r.t. abscissa. * @throws ZeroException if the abscissa range is zero. * @throws MathIllegalStateException when the guessing procedure cannot * produce sensible results. * @return the guessed amplitude (at index 0) and circular frequency * (at index 1). */ private double[] guessAOmega(WeightedObservedPoint[] observations) { final double[] aOmega = new double[2]; // initialize the sums for the linear model between the two integrals double sx2 = 0; double sy2 = 0; double sxy = 0; double sxz = 0; double syz = 0; double currentX = observations[0].getX(); double currentY = observations[0].getY(); double f2Integral = 0; double fPrime2Integral = 0; final double startX = currentX; for (int i = 1; i < observations.length; ++i) { // one step forward final double previousX = currentX; final double previousY = currentY; currentX = observations[i].getX(); currentY = observations[i].getY(); // update the integrals of f2 and f'2 // considering a linear model for f (and therefore constant f') final double dx = currentX - previousX; final double dy = currentY - previousY; final double f2StepIntegral = dx * (previousY * previousY + previousY * currentY + currentY * currentY) / 3; final double fPrime2StepIntegral = dy * dy / dx; final double x = currentX - startX; f2Integral += f2StepIntegral; fPrime2Integral += fPrime2StepIntegral; sx2 += x * x; sy2 += f2Integral * f2Integral; sxy += x * f2Integral; sxz += x * fPrime2Integral; syz += f2Integral * fPrime2Integral; } // compute the amplitude and pulsation coefficients double c1 = sy2 * sxz - sxy * syz; double c2 = sxy * sxz - sx2 * syz; double c3 = sx2 * sy2 - sxy * sxy; if ((c1 / c2 < 0) || (c2 / c3 < 0)) { final int last = observations.length - 1; // Range of the observations, assuming that the // observations are sorted. final double xRange = observations[last].getX() - observations[0].getX(); if (xRange == 0) { throw new ZeroException(); } aOmega[1] = 2 * Math.PI / xRange; double yMin = Double.POSITIVE_INFINITY; double yMax = Double.NEGATIVE_INFINITY; for (int i = 1; i < observations.length; ++i) { final double y = observations[i].getY(); if (y < yMin) { yMin = y; } if (y > yMax) { yMax = y; } } aOmega[0] = 0.5 * (yMax - yMin); } else { if (c2 == 0) { // In some ill-conditioned cases (cf. MATH-844), the guesser // procedure cannot produce sensible results. throw new MathIllegalStateException(LocalizedFormats.ZERO_DENOMINATOR); } aOmega[0] = FastMath.sqrt(c1 / c2); aOmega[1] = FastMath.sqrt(c2 / c3); } return aOmega; } /** * Estimate a first guess of the phase. * * @param observations Observations, sorted w.r.t. abscissa. * @return the guessed phase. */ private double guessPhi(WeightedObservedPoint[] observations) { // initialize the means double fcMean = 0; double fsMean = 0; double currentX = observations[0].getX(); double currentY = observations[0].getY(); for (int i = 1; i < observations.length; ++i) { // one step forward final double previousX = currentX; final double previousY = currentY; currentX = observations[i].getX(); currentY = observations[i].getY(); final double currentYPrime = (currentY - previousY) / (currentX - previousX); double omegaX = omega * currentX; double cosine = FastMath.cos(omegaX); double sine = FastMath.sin(omegaX); fcMean += omega * currentY * cosine - currentYPrime * sine; fsMean += omega * currentY * sine + currentYPrime * cosine; } return FastMath.atan2(-fsMean, fcMean); } } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/optimization/PointValuePair.java100644 1750 1750 10074 12126627713 31040 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.optimization; import java.io.Serializable; import org.apache.commons.math3.util.Pair; /** * This class holds a point and the value of an objective function at * that point. * * @see PointVectorValuePair * @see org.apache.commons.math3.analysis.MultivariateFunction * @version $Id: PointValuePair.java 1422230 2012-12-15 12:11:13Z erans $ * @deprecated As of 3.1 (to be removed in 4.0). * @since 3.0 */ @Deprecated public class PointValuePair extends Pair implements Serializable { /** Serializable UID. */ private static final long serialVersionUID = 20120513L; /** * Builds a point/objective function value pair. * * @param point Point coordinates. This instance will store * a copy of the array, not the array passed as argument. * @param value Value of the objective function at the point. */ public PointValuePair(final double[] point, final double value) { this(point, value, true); } /** * Builds a point/objective function value pair. * * @param point Point coordinates. * @param value Value of the objective function at the point. * @param copyArray if {@code true}, the input array will be copied, * otherwise it will be referenced. */ public PointValuePair(final double[] point, final double value, final boolean copyArray) { super(copyArray ? ((point == null) ? null : point.clone()) : point, value); } /** * Gets the point. * * @return a copy of the stored point. */ public double[] getPoint() { final double[] p = getKey(); return p == null ? null : p.clone(); } /** * Gets a reference to the point. * * @return a reference to the internal array storing the point. */ public double[] getPointRef() { return getKey(); } /** * Replace the instance with a data transfer object for serialization. * @return data transfer object that will be serialized */ private Object writeReplace() { return new DataTransferObject(getKey(), getValue()); } /** Internal class used only for serialization. */ private static class DataTransferObject implements Serializable { /** Serializable UID. */ private static final long serialVersionUID = 20120513L; /** * Point coordinates. * @Serial */ private final double[] point; /** * Value of the objective function at the point. * @Serial */ private final double value; /** Simple constructor. * @param point Point coordinates. * @param value Value of the objective function at the point. */ public DataTransferObject(final double[] point, final double value) { this.point = point.clone(); this.value = value; } /** Replace the deserialized data transfer object with a {@link PointValuePair}. * @return replacement {@link PointValuePair} */ private Object readResolve() { return new PointValuePair(point, value, false); } } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/optimization/InitialGuess.java100644 1750 1750 2770 12126627713 30522 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.optimization; /** * Starting point (first guess) of the optimization procedure. *
                          * Immutable class. * * @version $Id: InitialGuess.java 1422230 2012-12-15 12:11:13Z erans $ * @deprecated As of 3.1 (to be removed in 4.0). * @since 3.1 */ @Deprecated public class InitialGuess implements OptimizationData { /** Initial guess. */ private final double[] init; /** * @param startPoint Initial guess. */ public InitialGuess(double[] startPoint) { init = startPoint.clone(); } /** * Gets the initial guess. * * @return the initial guess. */ public double[] getInitialGuess() { return init.clone(); } } ././@LongLink100644 0 0 175 12126630646 10264 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/optimization/MultivariateDifferentiableMultiStartOptimizer.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/optimization/MultivariateDifferentiable100644 1750 1750 4612 12126627713 32471 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.optimization; import org.apache.commons.math3.analysis.differentiation.MultivariateDifferentiableFunction; import org.apache.commons.math3.random.RandomVectorGenerator; /** * Special implementation of the {@link MultivariateDifferentiableOptimizer} * interface adding multi-start features to an existing optimizer. * * This class wraps a classical optimizer to use it several times in * turn with different starting points in order to avoid being trapped * into a local extremum when looking for a global one. * * @version $Id: MultivariateDifferentiableMultiStartOptimizer.java 1422230 2012-12-15 12:11:13Z erans $ * @deprecated As of 3.1 (to be removed in 4.0). * @since 3.1 */ @Deprecated public class MultivariateDifferentiableMultiStartOptimizer extends BaseMultivariateMultiStartOptimizer implements MultivariateDifferentiableOptimizer { /** * Create a multi-start optimizer from a single-start optimizer. * * @param optimizer Single-start optimizer to wrap. * @param starts Number of starts to perform (including the * first one), multi-start is disabled if value is less than or * equal to 1. * @param generator Random vector generator to use for restarts. */ public MultivariateDifferentiableMultiStartOptimizer(final MultivariateDifferentiableOptimizer optimizer, final int starts, final RandomVectorGenerator generator) { super(optimizer, starts, generator); } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/optimization/ConvergenceChecker.java100644 1750 1750 5002 12126627713 31634 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.optimization; /** * This interface specifies how to check if an optimization algorithm has * converged. *
                          * Deciding if convergence has been reached is a problem-dependent issue. The * user should provide a class implementing this interface to allow the * optimization algorithm to stop its search according to the problem at hand. *
                          * For convenience, three implementations that fit simple needs are already * provided: {@link SimpleValueChecker}, {@link SimpleVectorValueChecker} and * {@link SimplePointChecker}. The first two consider that convergence is * reached when the objective function value does not change much anymore, it * does not use the point set at all. * The third one considers that convergence is reached when the input point * set does not change much anymore, it does not use objective function value * at all. * * @param Type of the (point, objective value) pair. * * @see org.apache.commons.math3.optimization.SimplePointChecker * @see org.apache.commons.math3.optimization.SimpleValueChecker * @see org.apache.commons.math3.optimization.SimpleVectorValueChecker * * @version $Id: ConvergenceChecker.java 1422230 2012-12-15 12:11:13Z erans $ * @deprecated As of 3.1 (to be removed in 4.0). * @since 3.0 */ @Deprecated public interface ConvergenceChecker { /** * Check if the optimization algorithm has converged. * * @param iteration Current iteration. * @param previous Best point in the previous iteration. * @param current Best point in the current iteration. * @return {@code true} if the algorithm is considered to have converged. */ boolean converged(int iteration, PAIR previous, PAIR current); } ././@LongLink100644 0 0 163 12126630646 10261 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/optimization/BaseMultivariateMultiStartOptimizer.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/optimization/BaseMultivariateMultiStart100644 1750 1750 17105 12126627713 32512 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.optimization; import java.util.Arrays; import java.util.Comparator; import org.apache.commons.math3.analysis.MultivariateFunction; import org.apache.commons.math3.exception.MathIllegalStateException; import org.apache.commons.math3.exception.NotStrictlyPositiveException; import org.apache.commons.math3.exception.NullArgumentException; import org.apache.commons.math3.exception.util.LocalizedFormats; import org.apache.commons.math3.random.RandomVectorGenerator; /** * Base class for all implementations of a multi-start optimizer. * * This interface is mainly intended to enforce the internal coherence of * Commons-Math. Users of the API are advised to base their code on * {@link MultivariateMultiStartOptimizer} or on * {@link DifferentiableMultivariateMultiStartOptimizer}. * * @param Type of the objective function to be optimized. * * @version $Id: BaseMultivariateMultiStartOptimizer.java 1422230 2012-12-15 12:11:13Z erans $ * @deprecated As of 3.1 (to be removed in 4.0). * @since 3.0 */ @Deprecated public class BaseMultivariateMultiStartOptimizer implements BaseMultivariateOptimizer { /** Underlying classical optimizer. */ private final BaseMultivariateOptimizer optimizer; /** Maximal number of evaluations allowed. */ private int maxEvaluations; /** Number of evaluations already performed for all starts. */ private int totalEvaluations; /** Number of starts to go. */ private int starts; /** Random generator for multi-start. */ private RandomVectorGenerator generator; /** Found optima. */ private PointValuePair[] optima; /** * Create a multi-start optimizer from a single-start optimizer. * * @param optimizer Single-start optimizer to wrap. * @param starts Number of starts to perform. If {@code starts == 1}, * the {@link #optimize(int,MultivariateFunction,GoalType,double[]) * optimize} will return the same solution as {@code optimizer} would. * @param generator Random vector generator to use for restarts. * @throws NullArgumentException if {@code optimizer} or {@code generator} * is {@code null}. * @throws NotStrictlyPositiveException if {@code starts < 1}. */ protected BaseMultivariateMultiStartOptimizer(final BaseMultivariateOptimizer optimizer, final int starts, final RandomVectorGenerator generator) { if (optimizer == null || generator == null) { throw new NullArgumentException(); } if (starts < 1) { throw new NotStrictlyPositiveException(starts); } this.optimizer = optimizer; this.starts = starts; this.generator = generator; } /** * Get all the optima found during the last call to {@link * #optimize(int,MultivariateFunction,GoalType,double[]) optimize}. * The optimizer stores all the optima found during a set of * restarts. The {@link #optimize(int,MultivariateFunction,GoalType,double[]) * optimize} method returns the best point only. This method * returns all the points found at the end of each starts, * including the best one already returned by the {@link * #optimize(int,MultivariateFunction,GoalType,double[]) optimize} method. *
                          * The returned array as one element for each start as specified * in the constructor. It is ordered with the results from the * runs that did converge first, sorted from best to worst * objective value (i.e in ascending order if minimizing and in * descending order if maximizing), followed by and null elements * corresponding to the runs that did not converge. This means all * elements will be null if the {@link #optimize(int,MultivariateFunction,GoalType,double[]) * optimize} method did throw an exception. * This also means that if the first element is not {@code null}, it * is the best point found across all starts. * * @return an array containing the optima. * @throws MathIllegalStateException if {@link * #optimize(int,MultivariateFunction,GoalType,double[]) optimize} * has not been called. */ public PointValuePair[] getOptima() { if (optima == null) { throw new MathIllegalStateException(LocalizedFormats.NO_OPTIMUM_COMPUTED_YET); } return optima.clone(); } /** {@inheritDoc} */ public int getMaxEvaluations() { return maxEvaluations; } /** {@inheritDoc} */ public int getEvaluations() { return totalEvaluations; } /** {@inheritDoc} */ public ConvergenceChecker getConvergenceChecker() { return optimizer.getConvergenceChecker(); } /** * {@inheritDoc} */ public PointValuePair optimize(int maxEval, final FUNC f, final GoalType goal, double[] startPoint) { maxEvaluations = maxEval; RuntimeException lastException = null; optima = new PointValuePair[starts]; totalEvaluations = 0; // Multi-start loop. for (int i = 0; i < starts; ++i) { // CHECKSTYLE: stop IllegalCatch try { optima[i] = optimizer.optimize(maxEval - totalEvaluations, f, goal, i == 0 ? startPoint : generator.nextVector()); } catch (RuntimeException mue) { lastException = mue; optima[i] = null; } // CHECKSTYLE: resume IllegalCatch totalEvaluations += optimizer.getEvaluations(); } sortPairs(goal); if (optima[0] == null) { throw lastException; // cannot be null if starts >=1 } // Return the found point given the best objective function value. return optima[0]; } /** * Sort the optima from best to worst, followed by {@code null} elements. * * @param goal Goal type. */ private void sortPairs(final GoalType goal) { Arrays.sort(optima, new Comparator() { public int compare(final PointValuePair o1, final PointValuePair o2) { if (o1 == null) { return (o2 == null) ? 0 : 1; } else if (o2 == null) { return -1; } final double v1 = o1.getValue(); final double v2 = o2.getValue(); return (goal == GoalType.MINIMIZE) ? Double.compare(v1, v2) : Double.compare(v2, v1); } }); } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/optimization/PointVectorValuePair.java100644 1750 1750 11421 12126627713 32220 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.optimization; import java.io.Serializable; import org.apache.commons.math3.util.Pair; /** * This class holds a point and the vectorial value of an objective function at * that point. * * @see PointValuePair * @see org.apache.commons.math3.analysis.MultivariateVectorFunction * @version $Id: PointVectorValuePair.java 1422230 2012-12-15 12:11:13Z erans $ * @deprecated As of 3.1 (to be removed in 4.0). * @since 3.0 */ @Deprecated public class PointVectorValuePair extends Pair implements Serializable { /** Serializable UID. */ private static final long serialVersionUID = 20120513L; /** * Builds a point/objective function value pair. * * @param point Point coordinates. This instance will store * a copy of the array, not the array passed as argument. * @param value Value of the objective function at the point. */ public PointVectorValuePair(final double[] point, final double[] value) { this(point, value, true); } /** * Build a point/objective function value pair. * * @param point Point coordinates. * @param value Value of the objective function at the point. * @param copyArray if {@code true}, the input arrays will be copied, * otherwise they will be referenced. */ public PointVectorValuePair(final double[] point, final double[] value, final boolean copyArray) { super(copyArray ? ((point == null) ? null : point.clone()) : point, copyArray ? ((value == null) ? null : value.clone()) : value); } /** * Gets the point. * * @return a copy of the stored point. */ public double[] getPoint() { final double[] p = getKey(); return p == null ? null : p.clone(); } /** * Gets a reference to the point. * * @return a reference to the internal array storing the point. */ public double[] getPointRef() { return getKey(); } /** * Gets the value of the objective function. * * @return a copy of the stored value of the objective function. */ @Override public double[] getValue() { final double[] v = super.getValue(); return v == null ? null : v.clone(); } /** * Gets a reference to the value of the objective function. * * @return a reference to the internal array storing the value of * the objective function. */ public double[] getValueRef() { return super.getValue(); } /** * Replace the instance with a data transfer object for serialization. * @return data transfer object that will be serialized */ private Object writeReplace() { return new DataTransferObject(getKey(), getValue()); } /** Internal class used only for serialization. */ private static class DataTransferObject implements Serializable { /** Serializable UID. */ private static final long serialVersionUID = 20120513L; /** * Point coordinates. * @Serial */ private final double[] point; /** * Value of the objective function at the point. * @Serial */ private final double[] value; /** Simple constructor. * @param point Point coordinates. * @param value Value of the objective function at the point. */ public DataTransferObject(final double[] point, final double[] value) { this.point = point.clone(); this.value = value.clone(); } /** Replace the deserialized data transfer object with a {@link PointValuePair}. * @return replacement {@link PointValuePair} */ private Object readResolve() { return new PointVectorValuePair(point, value, false); } } } ././@LongLink100644 0 0 151 12126630646 10256 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/optimization/BaseMultivariateOptimizer.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/optimization/BaseMultivariateOptimizer.100644 1750 1750 5432 12126627713 32422 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.optimization; import org.apache.commons.math3.analysis.MultivariateFunction; /** * This interface is mainly intended to enforce the internal coherence of * Commons-FastMath. Users of the API are advised to base their code on * the following interfaces: *
                            *
                          • {@link org.apache.commons.math3.optimization.MultivariateOptimizer}
                          • *
                          • {@link org.apache.commons.math3.optimization.MultivariateDifferentiableOptimizer}
                          • *
                          * * @param Type of the objective function to be optimized. * * @version $Id: BaseMultivariateOptimizer.java 1422230 2012-12-15 12:11:13Z erans $ * @deprecated As of 3.1 (to be removed in 4.0). * @since 3.0 */ @Deprecated public interface BaseMultivariateOptimizer extends BaseOptimizer { /** * Optimize an objective function. * * @param f Objective function. * @param goalType Type of optimization goal: either * {@link GoalType#MAXIMIZE} or {@link GoalType#MINIMIZE}. * @param startPoint Start point for optimization. * @param maxEval Maximum number of function evaluations. * @return the point/value pair giving the optimal value for objective * function. * @throws org.apache.commons.math3.exception.DimensionMismatchException * if the start point dimension is wrong. * @throws org.apache.commons.math3.exception.TooManyEvaluationsException * if the maximal number of evaluations is exceeded. * @throws org.apache.commons.math3.exception.NullArgumentException if * any argument is {@code null}. * @deprecated As of 3.1. In 4.0, it will be replaced by the declaration * corresponding to this {@link org.apache.commons.math3.optimization.direct.BaseAbstractMultivariateOptimizer#optimize(int,MultivariateFunction,GoalType,OptimizationData[]) method}. */ @Deprecated PointValuePair optimize(int maxEval, FUNC f, GoalType goalType, double[] startPoint); } ././@LongLink100644 0 0 171 12126630646 10260 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/optimization/BaseMultivariateVectorMultiStartOptimizer.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/optimization/BaseMultivariateVectorMult100644 1750 1750 20564 12126627713 32511 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.optimization; import java.util.Arrays; import java.util.Comparator; import org.apache.commons.math3.analysis.MultivariateVectorFunction; import org.apache.commons.math3.exception.ConvergenceException; import org.apache.commons.math3.exception.MathIllegalStateException; import org.apache.commons.math3.exception.NotStrictlyPositiveException; import org.apache.commons.math3.exception.NullArgumentException; import org.apache.commons.math3.exception.util.LocalizedFormats; import org.apache.commons.math3.random.RandomVectorGenerator; /** * Base class for all implementations of a multi-start optimizer. * * This interface is mainly intended to enforce the internal coherence of * Commons-Math. Users of the API are advised to base their code on * {@link DifferentiableMultivariateVectorMultiStartOptimizer}. * * @param Type of the objective function to be optimized. * * @version $Id: BaseMultivariateVectorMultiStartOptimizer.java 1422230 2012-12-15 12:11:13Z erans $ * @deprecated As of 3.1 (to be removed in 4.0). * @since 3.0 */ @Deprecated public class BaseMultivariateVectorMultiStartOptimizer implements BaseMultivariateVectorOptimizer { /** Underlying classical optimizer. */ private final BaseMultivariateVectorOptimizer optimizer; /** Maximal number of evaluations allowed. */ private int maxEvaluations; /** Number of evaluations already performed for all starts. */ private int totalEvaluations; /** Number of starts to go. */ private int starts; /** Random generator for multi-start. */ private RandomVectorGenerator generator; /** Found optima. */ private PointVectorValuePair[] optima; /** * Create a multi-start optimizer from a single-start optimizer. * * @param optimizer Single-start optimizer to wrap. * @param starts Number of starts to perform. If {@code starts == 1}, * the {@link #optimize(int,MultivariateVectorFunction,double[],double[],double[]) * optimize} will return the same solution as {@code optimizer} would. * @param generator Random vector generator to use for restarts. * @throws NullArgumentException if {@code optimizer} or {@code generator} * is {@code null}. * @throws NotStrictlyPositiveException if {@code starts < 1}. */ protected BaseMultivariateVectorMultiStartOptimizer(final BaseMultivariateVectorOptimizer optimizer, final int starts, final RandomVectorGenerator generator) { if (optimizer == null || generator == null) { throw new NullArgumentException(); } if (starts < 1) { throw new NotStrictlyPositiveException(starts); } this.optimizer = optimizer; this.starts = starts; this.generator = generator; } /** * Get all the optima found during the last call to {@link * #optimize(int,MultivariateVectorFunction,double[],double[],double[]) optimize}. * The optimizer stores all the optima found during a set of * restarts. The {@link #optimize(int,MultivariateVectorFunction,double[],double[],double[]) * optimize} method returns the best point only. This method * returns all the points found at the end of each starts, including * the best one already returned by the {@link * #optimize(int,MultivariateVectorFunction,double[],double[],double[]) optimize} method. *
                          * The returned array as one element for each start as specified * in the constructor. It is ordered with the results from the * runs that did converge first, sorted from best to worst * objective value (i.e. in ascending order if minimizing and in * descending order if maximizing), followed by and null elements * corresponding to the runs that did not converge. This means all * elements will be null if the {@link * #optimize(int,MultivariateVectorFunction,double[],double[],double[]) optimize} method did * throw a {@link ConvergenceException}). This also means that if * the first element is not {@code null}, it is the best point found * across all starts. * * @return array containing the optima * @throws MathIllegalStateException if {@link * #optimize(int,MultivariateVectorFunction,double[],double[],double[]) optimize} has not been * called. */ public PointVectorValuePair[] getOptima() { if (optima == null) { throw new MathIllegalStateException(LocalizedFormats.NO_OPTIMUM_COMPUTED_YET); } return optima.clone(); } /** {@inheritDoc} */ public int getMaxEvaluations() { return maxEvaluations; } /** {@inheritDoc} */ public int getEvaluations() { return totalEvaluations; } /** {@inheritDoc} */ public ConvergenceChecker getConvergenceChecker() { return optimizer.getConvergenceChecker(); } /** * {@inheritDoc} */ public PointVectorValuePair optimize(int maxEval, final FUNC f, double[] target, double[] weights, double[] startPoint) { maxEvaluations = maxEval; RuntimeException lastException = null; optima = new PointVectorValuePair[starts]; totalEvaluations = 0; // Multi-start loop. for (int i = 0; i < starts; ++i) { // CHECKSTYLE: stop IllegalCatch try { optima[i] = optimizer.optimize(maxEval - totalEvaluations, f, target, weights, i == 0 ? startPoint : generator.nextVector()); } catch (ConvergenceException oe) { optima[i] = null; } catch (RuntimeException mue) { lastException = mue; optima[i] = null; } // CHECKSTYLE: resume IllegalCatch totalEvaluations += optimizer.getEvaluations(); } sortPairs(target, weights); if (optima[0] == null) { throw lastException; // cannot be null if starts >=1 } // Return the found point given the best objective function value. return optima[0]; } /** * Sort the optima from best to worst, followed by {@code null} elements. * * @param target Target value for the objective functions at optimum. * @param weights Weights for the least-squares cost computation. */ private void sortPairs(final double[] target, final double[] weights) { Arrays.sort(optima, new Comparator() { public int compare(final PointVectorValuePair o1, final PointVectorValuePair o2) { if (o1 == null) { return (o2 == null) ? 0 : 1; } else if (o2 == null) { return -1; } return Double.compare(weightedResidual(o1), weightedResidual(o2)); } private double weightedResidual(final PointVectorValuePair pv) { final double[] value = pv.getValueRef(); double sum = 0; for (int i = 0; i < value.length; ++i) { final double ri = value[i] - target[i]; sum += weights[i] * ri * ri; } return sum; } }); } } ././@LongLink100644 0 0 163 12126630646 10261 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/optimization/DifferentiableMultivariateOptimizer.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/optimization/DifferentiableMultivariate100644 1750 1750 3103 12126627713 32463 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.optimization; import org.apache.commons.math3.analysis.DifferentiableMultivariateFunction; /** * This interface represents an optimization algorithm for * {@link DifferentiableMultivariateFunction scalar differentiable objective * functions}. * Optimization algorithms find the input point set that either {@link GoalType * maximize or minimize} an objective function. * * @see MultivariateOptimizer * @see DifferentiableMultivariateVectorOptimizer * * @version $Id: DifferentiableMultivariateOptimizer.java 1422230 2012-12-15 12:11:13Z erans $ * @deprecated As of 3.1 (to be removed in 4.0). * @since 2.0 */ @Deprecated public interface DifferentiableMultivariateOptimizer extends BaseMultivariateOptimizer {} commons-math3-3.2-src/src/main/java/org/apache/commons/math3/optimization/linear/package-info.java100644 1750 1750 1671 12126627713 31717 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ /** * * This package provides optimization algorithms for linear constrained problems. * */ package org.apache.commons.math3.optimization.linear; ././@LongLink100644 0 0 146 12126630646 10262 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/optimization/linear/LinearOptimizer.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/optimization/linear/LinearOptimizer.jav100644 1750 1750 10166 12126627713 32366 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.optimization.linear; import java.util.Collection; import org.apache.commons.math3.exception.MathIllegalStateException; import org.apache.commons.math3.optimization.GoalType; import org.apache.commons.math3.optimization.PointValuePair; /** * This interface represents an optimization algorithm for linear problems. *

                          Optimization algorithms find the input point set that either {@link GoalType * maximize or minimize} an objective function. In the linear case the form of * the function is restricted to *

                           * c1x1 + ... cnxn = v
                           * 
                          * and there may be linear constraints too, of one of the forms: *
                            *
                          • c1x1 + ... cnxn = v
                          • *
                          • c1x1 + ... cnxn <= v
                          • *
                          • c1x1 + ... cnxn >= v
                          • *
                          • l1x1 + ... lnxn + lcst = * r1x1 + ... rnxn + rcst
                          • *
                          • l1x1 + ... lnxn + lcst <= * r1x1 + ... rnxn + rcst
                          • *
                          • l1x1 + ... lnxn + lcst >= * r1x1 + ... rnxn + rcst
                          • *
                          * where the ci, li or ri are the coefficients of * the constraints, the xi are the coordinates of the current point and * v is the value of the constraint. *

                          * @version $Id: LinearOptimizer.java 1422230 2012-12-15 12:11:13Z erans $ * @deprecated As of 3.1 (to be removed in 4.0). * @since 2.0 */ @Deprecated public interface LinearOptimizer { /** * Set the maximal number of iterations of the algorithm. * @param maxIterations maximal number of function calls */ void setMaxIterations(int maxIterations); /** * Get the maximal number of iterations of the algorithm. * @return maximal number of iterations */ int getMaxIterations(); /** * Get the number of iterations realized by the algorithm. *

                          * The number of evaluations corresponds to the last call to the * {@link #optimize(LinearObjectiveFunction, Collection, GoalType, boolean) optimize} * method. It is 0 if the method has not been called yet. *

                          * @return number of iterations */ int getIterations(); /** * Optimizes an objective function. * @param f linear objective function * @param constraints linear constraints * @param goalType type of optimization goal: either {@link GoalType#MAXIMIZE} or {@link GoalType#MINIMIZE} * @param restrictToNonNegative whether to restrict the variables to non-negative values * @return point/value pair giving the optimal value for objective function * @exception MathIllegalStateException if no solution fulfilling the constraints * can be found in the allowed number of iterations */ PointValuePair optimize(LinearObjectiveFunction f, Collection constraints, GoalType goalType, boolean restrictToNonNegative) throws MathIllegalStateException; } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/optimization/linear/Relationship.java100644 1750 1750 3776 12126627713 32044 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.optimization.linear; /** * Types of relationships between two cells in a Solver {@link LinearConstraint}. * @version $Id: Relationship.java 1422230 2012-12-15 12:11:13Z erans $ * @deprecated As of 3.1 (to be removed in 4.0). * @since 2.0 */ @Deprecated public enum Relationship { /** Equality relationship. */ EQ("="), /** Lesser than or equal relationship. */ LEQ("<="), /** Greater than or equal relationship. */ GEQ(">="); /** Display string for the relationship. */ private final String stringValue; /** Simple constructor. * @param stringValue display string for the relationship */ private Relationship(String stringValue) { this.stringValue = stringValue; } @Override public String toString() { return stringValue; } /** * Get the relationship obtained when multiplying all coefficients by -1. * @return relationship obtained when multiplying all coefficients by -1 */ public Relationship oppositeRelationship() { switch (this) { case LEQ : return GEQ; case GEQ : return LEQ; default : return EQ; } } } ././@LongLink100644 0 0 147 12126630646 10263 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/optimization/linear/LinearConstraint.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/optimization/linear/LinearConstraint.ja100644 1750 1750 24201 12126627713 32335 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.optimization.linear; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.io.Serializable; import org.apache.commons.math3.linear.MatrixUtils; import org.apache.commons.math3.linear.RealVector; import org.apache.commons.math3.linear.ArrayRealVector; /** * A linear constraint for a linear optimization problem. *

                          * A linear constraint has one of the forms: *

                            *
                          • c1x1 + ... cnxn = v
                          • *
                          • c1x1 + ... cnxn <= v
                          • *
                          • c1x1 + ... cnxn >= v
                          • *
                          • l1x1 + ... lnxn + lcst = * r1x1 + ... rnxn + rcst
                          • *
                          • l1x1 + ... lnxn + lcst <= * r1x1 + ... rnxn + rcst
                          • *
                          • l1x1 + ... lnxn + lcst >= * r1x1 + ... rnxn + rcst
                          • *
                          * The ci, li or ri are the coefficients of the constraints, the xi * are the coordinates of the current point and v is the value of the constraint. *

                          * @version $Id: LinearConstraint.java 1422230 2012-12-15 12:11:13Z erans $ * @deprecated As of 3.1 (to be removed in 4.0). * @since 2.0 */ @Deprecated public class LinearConstraint implements Serializable { /** Serializable version identifier. */ private static final long serialVersionUID = -764632794033034092L; /** Coefficients of the constraint (left hand side). */ private final transient RealVector coefficients; /** Relationship between left and right hand sides (=, <=, >=). */ private final Relationship relationship; /** Value of the constraint (right hand side). */ private final double value; /** * Build a constraint involving a single linear equation. *

                          * A linear constraint with a single linear equation has one of the forms: *

                            *
                          • c1x1 + ... cnxn = v
                          • *
                          • c1x1 + ... cnxn <= v
                          • *
                          • c1x1 + ... cnxn >= v
                          • *
                          *

                          * @param coefficients The coefficients of the constraint (left hand side) * @param relationship The type of (in)equality used in the constraint * @param value The value of the constraint (right hand side) */ public LinearConstraint(final double[] coefficients, final Relationship relationship, final double value) { this(new ArrayRealVector(coefficients), relationship, value); } /** * Build a constraint involving a single linear equation. *

                          * A linear constraint with a single linear equation has one of the forms: *

                            *
                          • c1x1 + ... cnxn = v
                          • *
                          • c1x1 + ... cnxn <= v
                          • *
                          • c1x1 + ... cnxn >= v
                          • *
                          *

                          * @param coefficients The coefficients of the constraint (left hand side) * @param relationship The type of (in)equality used in the constraint * @param value The value of the constraint (right hand side) */ public LinearConstraint(final RealVector coefficients, final Relationship relationship, final double value) { this.coefficients = coefficients; this.relationship = relationship; this.value = value; } /** * Build a constraint involving two linear equations. *

                          * A linear constraint with two linear equation has one of the forms: *

                            *
                          • l1x1 + ... lnxn + lcst = * r1x1 + ... rnxn + rcst
                          • *
                          • l1x1 + ... lnxn + lcst <= * r1x1 + ... rnxn + rcst
                          • *
                          • l1x1 + ... lnxn + lcst >= * r1x1 + ... rnxn + rcst
                          • *
                          *

                          * @param lhsCoefficients The coefficients of the linear expression on the left hand side of the constraint * @param lhsConstant The constant term of the linear expression on the left hand side of the constraint * @param relationship The type of (in)equality used in the constraint * @param rhsCoefficients The coefficients of the linear expression on the right hand side of the constraint * @param rhsConstant The constant term of the linear expression on the right hand side of the constraint */ public LinearConstraint(final double[] lhsCoefficients, final double lhsConstant, final Relationship relationship, final double[] rhsCoefficients, final double rhsConstant) { double[] sub = new double[lhsCoefficients.length]; for (int i = 0; i < sub.length; ++i) { sub[i] = lhsCoefficients[i] - rhsCoefficients[i]; } this.coefficients = new ArrayRealVector(sub, false); this.relationship = relationship; this.value = rhsConstant - lhsConstant; } /** * Build a constraint involving two linear equations. *

                          * A linear constraint with two linear equation has one of the forms: *

                            *
                          • l1x1 + ... lnxn + lcst = * r1x1 + ... rnxn + rcst
                          • *
                          • l1x1 + ... lnxn + lcst <= * r1x1 + ... rnxn + rcst
                          • *
                          • l1x1 + ... lnxn + lcst >= * r1x1 + ... rnxn + rcst
                          • *
                          *

                          * @param lhsCoefficients The coefficients of the linear expression on the left hand side of the constraint * @param lhsConstant The constant term of the linear expression on the left hand side of the constraint * @param relationship The type of (in)equality used in the constraint * @param rhsCoefficients The coefficients of the linear expression on the right hand side of the constraint * @param rhsConstant The constant term of the linear expression on the right hand side of the constraint */ public LinearConstraint(final RealVector lhsCoefficients, final double lhsConstant, final Relationship relationship, final RealVector rhsCoefficients, final double rhsConstant) { this.coefficients = lhsCoefficients.subtract(rhsCoefficients); this.relationship = relationship; this.value = rhsConstant - lhsConstant; } /** * Get the coefficients of the constraint (left hand side). * @return coefficients of the constraint (left hand side) */ public RealVector getCoefficients() { return coefficients; } /** * Get the relationship between left and right hand sides. * @return relationship between left and right hand sides */ public Relationship getRelationship() { return relationship; } /** * Get the value of the constraint (right hand side). * @return value of the constraint (right hand side) */ public double getValue() { return value; } @Override public boolean equals(Object other) { if (this == other) { return true; } if (other instanceof LinearConstraint) { LinearConstraint rhs = (LinearConstraint) other; return (relationship == rhs.relationship) && (value == rhs.value) && coefficients.equals(rhs.coefficients); } return false; } @Override public int hashCode() { return relationship.hashCode() ^ Double.valueOf(value).hashCode() ^ coefficients.hashCode(); } /** * Serialize the instance. * @param oos stream where object should be written * @throws IOException if object cannot be written to stream */ private void writeObject(ObjectOutputStream oos) throws IOException { oos.defaultWriteObject(); MatrixUtils.serializeRealVector(coefficients, oos); } /** * Deserialize the instance. * @param ois stream from which the object should be read * @throws ClassNotFoundException if a class in the stream cannot be found * @throws IOException if object cannot be read from the stream */ private void readObject(ObjectInputStream ois) throws ClassNotFoundException, IOException { ois.defaultReadObject(); MatrixUtils.deserializeRealVector(this, "coefficients", ois); } } ././@LongLink100644 0 0 162 12126630646 10260 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/optimization/linear/NoFeasibleSolutionException.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/optimization/linear/NoFeasibleSolutionE100644 1750 1750 3143 12126627713 32320 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.optimization.linear; import org.apache.commons.math3.exception.MathIllegalStateException; import org.apache.commons.math3.exception.util.LocalizedFormats; /** * This class represents exceptions thrown by optimizers when no solution fulfills the constraints. * * @version $Id: NoFeasibleSolutionException.java 1422230 2012-12-15 12:11:13Z erans $ * @deprecated As of 3.1 (to be removed in 4.0). * @since 2.0 */ @Deprecated public class NoFeasibleSolutionException extends MathIllegalStateException { /** Serializable version identifier. */ private static final long serialVersionUID = -3044253632189082760L; /** * Simple constructor using a default message. */ public NoFeasibleSolutionException() { super(LocalizedFormats.NO_FEASIBLE_SOLUTION); } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/optimization/linear/SimplexSolver.java100644 1750 1750 23031 12126627713 32221 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.optimization.linear; import java.util.ArrayList; import java.util.List; import org.apache.commons.math3.exception.MaxCountExceededException; import org.apache.commons.math3.optimization.PointValuePair; import org.apache.commons.math3.util.Precision; /** * Solves a linear problem using the Two-Phase Simplex Method. * * @version $Id: SimplexSolver.java 1462503 2013-03-29 15:48:27Z luc $ * @deprecated As of 3.1 (to be removed in 4.0). * @since 2.0 */ @Deprecated public class SimplexSolver extends AbstractLinearOptimizer { /** Default amount of error to accept for algorithm convergence. */ private static final double DEFAULT_EPSILON = 1.0e-6; /** Default amount of error to accept in floating point comparisons (as ulps). */ private static final int DEFAULT_ULPS = 10; /** Amount of error to accept for algorithm convergence. */ private final double epsilon; /** Amount of error to accept in floating point comparisons (as ulps). */ private final int maxUlps; /** * Build a simplex solver with default settings. */ public SimplexSolver() { this(DEFAULT_EPSILON, DEFAULT_ULPS); } /** * Build a simplex solver with a specified accepted amount of error * @param epsilon the amount of error to accept for algorithm convergence * @param maxUlps amount of error to accept in floating point comparisons */ public SimplexSolver(final double epsilon, final int maxUlps) { this.epsilon = epsilon; this.maxUlps = maxUlps; } /** * Returns the column with the most negative coefficient in the objective function row. * @param tableau simple tableau for the problem * @return column with the most negative coefficient */ private Integer getPivotColumn(SimplexTableau tableau) { double minValue = 0; Integer minPos = null; for (int i = tableau.getNumObjectiveFunctions(); i < tableau.getWidth() - 1; i++) { final double entry = tableau.getEntry(0, i); // check if the entry is strictly smaller than the current minimum // do not use a ulp/epsilon check if (entry < minValue) { minValue = entry; minPos = i; } } return minPos; } /** * Returns the row with the minimum ratio as given by the minimum ratio test (MRT). * @param tableau simple tableau for the problem * @param col the column to test the ratio of. See {@link #getPivotColumn(SimplexTableau)} * @return row with the minimum ratio */ private Integer getPivotRow(SimplexTableau tableau, final int col) { // create a list of all the rows that tie for the lowest score in the minimum ratio test List minRatioPositions = new ArrayList(); double minRatio = Double.MAX_VALUE; for (int i = tableau.getNumObjectiveFunctions(); i < tableau.getHeight(); i++) { final double rhs = tableau.getEntry(i, tableau.getWidth() - 1); final double entry = tableau.getEntry(i, col); if (Precision.compareTo(entry, 0d, maxUlps) > 0) { final double ratio = rhs / entry; // check if the entry is strictly equal to the current min ratio // do not use a ulp/epsilon check final int cmp = Double.compare(ratio, minRatio); if (cmp == 0) { minRatioPositions.add(i); } else if (cmp < 0) { minRatio = ratio; minRatioPositions = new ArrayList(); minRatioPositions.add(i); } } } if (minRatioPositions.size() == 0) { return null; } else if (minRatioPositions.size() > 1) { // there's a degeneracy as indicated by a tie in the minimum ratio test // 1. check if there's an artificial variable that can be forced out of the basis if (tableau.getNumArtificialVariables() > 0) { for (Integer row : minRatioPositions) { for (int i = 0; i < tableau.getNumArtificialVariables(); i++) { int column = i + tableau.getArtificialVariableOffset(); final double entry = tableau.getEntry(row, column); if (Precision.equals(entry, 1d, maxUlps) && row.equals(tableau.getBasicRow(column))) { return row; } } } } // 2. apply Bland's rule to prevent cycling: // take the row for which the corresponding basic variable has the smallest index // // see http://www.stanford.edu/class/msande310/blandrule.pdf // see http://en.wikipedia.org/wiki/Bland%27s_rule (not equivalent to the above paper) // // Additional heuristic: if we did not get a solution after half of maxIterations // revert to the simple case of just returning the top-most row // This heuristic is based on empirical data gathered while investigating MATH-828. if (getIterations() < getMaxIterations() / 2) { Integer minRow = null; int minIndex = tableau.getWidth(); final int varStart = tableau.getNumObjectiveFunctions(); final int varEnd = tableau.getWidth() - 1; for (Integer row : minRatioPositions) { for (int i = varStart; i < varEnd && !row.equals(minRow); i++) { final Integer basicRow = tableau.getBasicRow(i); if (basicRow != null && basicRow.equals(row) && i < minIndex) { minIndex = i; minRow = row; } } } return minRow; } } return minRatioPositions.get(0); } /** * Runs one iteration of the Simplex method on the given model. * @param tableau simple tableau for the problem * @throws MaxCountExceededException if the maximal iteration count has been exceeded * @throws UnboundedSolutionException if the model is found not to have a bounded solution */ protected void doIteration(final SimplexTableau tableau) throws MaxCountExceededException, UnboundedSolutionException { incrementIterationsCounter(); Integer pivotCol = getPivotColumn(tableau); Integer pivotRow = getPivotRow(tableau, pivotCol); if (pivotRow == null) { throw new UnboundedSolutionException(); } // set the pivot element to 1 double pivotVal = tableau.getEntry(pivotRow, pivotCol); tableau.divideRow(pivotRow, pivotVal); // set the rest of the pivot column to 0 for (int i = 0; i < tableau.getHeight(); i++) { if (i != pivotRow) { final double multiplier = tableau.getEntry(i, pivotCol); tableau.subtractRow(i, pivotRow, multiplier); } } } /** * Solves Phase 1 of the Simplex method. * @param tableau simple tableau for the problem * @throws MaxCountExceededException if the maximal iteration count has been exceeded * @throws UnboundedSolutionException if the model is found not to have a bounded solution * @throws NoFeasibleSolutionException if there is no feasible solution */ protected void solvePhase1(final SimplexTableau tableau) throws MaxCountExceededException, UnboundedSolutionException, NoFeasibleSolutionException { // make sure we're in Phase 1 if (tableau.getNumArtificialVariables() == 0) { return; } while (!tableau.isOptimal()) { doIteration(tableau); } // if W is not zero then we have no feasible solution if (!Precision.equals(tableau.getEntry(0, tableau.getRhsOffset()), 0d, epsilon)) { throw new NoFeasibleSolutionException(); } } /** {@inheritDoc} */ @Override public PointValuePair doOptimize() throws MaxCountExceededException, UnboundedSolutionException, NoFeasibleSolutionException { final SimplexTableau tableau = new SimplexTableau(getFunction(), getConstraints(), getGoalType(), restrictToNonNegative(), epsilon, maxUlps); solvePhase1(tableau); tableau.dropPhase1Objective(); while (!tableau.isOptimal()) { doIteration(tableau); } return tableau.getSolution(); } } ././@LongLink100644 0 0 156 12126630646 10263 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/optimization/linear/LinearObjectiveFunction.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/optimization/linear/LinearObjectiveFunc100644 1750 1750 12065 12126627713 32353 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.optimization.linear; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.io.Serializable; import org.apache.commons.math3.linear.MatrixUtils; import org.apache.commons.math3.linear.RealVector; import org.apache.commons.math3.linear.ArrayRealVector; /** * An objective function for a linear optimization problem. *

                          * A linear objective function has one the form: *

                           * c1x1 + ... cnxn + d
                           * 
                          * The ci and d are the coefficients of the equation, * the xi are the coordinates of the current point. *

                          * @version $Id: LinearObjectiveFunction.java 1422230 2012-12-15 12:11:13Z erans $ * @deprecated As of 3.1 (to be removed in 4.0). * @since 2.0 */ @Deprecated public class LinearObjectiveFunction implements Serializable { /** Serializable version identifier. */ private static final long serialVersionUID = -4531815507568396090L; /** Coefficients of the constraint (ci). */ private final transient RealVector coefficients; /** Constant term of the linear equation. */ private final double constantTerm; /** * @param coefficients The coefficients for the linear equation being optimized * @param constantTerm The constant term of the linear equation */ public LinearObjectiveFunction(double[] coefficients, double constantTerm) { this(new ArrayRealVector(coefficients), constantTerm); } /** * @param coefficients The coefficients for the linear equation being optimized * @param constantTerm The constant term of the linear equation */ public LinearObjectiveFunction(RealVector coefficients, double constantTerm) { this.coefficients = coefficients; this.constantTerm = constantTerm; } /** * Get the coefficients of the linear equation being optimized. * @return coefficients of the linear equation being optimized */ public RealVector getCoefficients() { return coefficients; } /** * Get the constant of the linear equation being optimized. * @return constant of the linear equation being optimized */ public double getConstantTerm() { return constantTerm; } /** * Compute the value of the linear equation at the current point * @param point point at which linear equation must be evaluated * @return value of the linear equation at the current point */ public double getValue(final double[] point) { return coefficients.dotProduct(new ArrayRealVector(point, false)) + constantTerm; } /** * Compute the value of the linear equation at the current point * @param point point at which linear equation must be evaluated * @return value of the linear equation at the current point */ public double getValue(final RealVector point) { return coefficients.dotProduct(point) + constantTerm; } @Override public boolean equals(Object other) { if (this == other) { return true; } if (other instanceof LinearObjectiveFunction) { LinearObjectiveFunction rhs = (LinearObjectiveFunction) other; return (constantTerm == rhs.constantTerm) && coefficients.equals(rhs.coefficients); } return false; } @Override public int hashCode() { return Double.valueOf(constantTerm).hashCode() ^ coefficients.hashCode(); } /** * Serialize the instance. * @param oos stream where object should be written * @throws IOException if object cannot be written to stream */ private void writeObject(ObjectOutputStream oos) throws IOException { oos.defaultWriteObject(); MatrixUtils.serializeRealVector(coefficients, oos); } /** * Deserialize the instance. * @param ois stream from which the object should be read * @throws ClassNotFoundException if a class in the stream cannot be found * @throws IOException if object cannot be read from the stream */ private void readObject(ObjectInputStream ois) throws ClassNotFoundException, IOException { ois.defaultReadObject(); MatrixUtils.deserializeRealVector(this, "coefficients", ois); } } ././@LongLink100644 0 0 145 12126630646 10261 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/optimization/linear/SimplexTableau.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/optimization/linear/SimplexTableau.java100644 1750 1750 56174 12126627713 32342 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.optimization.linear; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.io.Serializable; import java.util.ArrayList; import java.util.Collection; import java.util.HashSet; import java.util.List; import java.util.Set; import java.util.TreeSet; import org.apache.commons.math3.linear.Array2DRowRealMatrix; import org.apache.commons.math3.linear.MatrixUtils; import org.apache.commons.math3.linear.RealMatrix; import org.apache.commons.math3.linear.RealVector; import org.apache.commons.math3.optimization.GoalType; import org.apache.commons.math3.optimization.PointValuePair; import org.apache.commons.math3.util.FastMath; import org.apache.commons.math3.util.Precision; /** * A tableau for use in the Simplex method. * *

                          * Example: *

                           *   W |  Z |  x1 |  x2 |  x- | s1 |  s2 |  a1 |  RHS
                           * ---------------------------------------------------
                           *  -1    0    0     0     0     0     0     1     0   <= phase 1 objective
                           *   0    1   -15   -10    0     0     0     0     0   <= phase 2 objective
                           *   0    0    1     0     0     1     0     0     2   <= constraint 1
                           *   0    0    0     1     0     0     1     0     3   <= constraint 2
                           *   0    0    1     1     0     0     0     1     4   <= constraint 3
                           * 
                          * W: Phase 1 objective function
                          * Z: Phase 2 objective function
                          * x1 & x2: Decision variables
                          * x-: Extra decision variable to allow for negative values
                          * s1 & s2: Slack/Surplus variables
                          * a1: Artificial variable
                          * RHS: Right hand side
                          *

                          * @version $Id: SimplexTableau.java 1422230 2012-12-15 12:11:13Z erans $ * @deprecated As of 3.1 (to be removed in 4.0). * @since 2.0 */ class SimplexTableau implements Serializable { /** Column label for negative vars. */ private static final String NEGATIVE_VAR_COLUMN_LABEL = "x-"; /** Default amount of error to accept in floating point comparisons (as ulps). */ private static final int DEFAULT_ULPS = 10; /** The cut-off threshold to zero-out entries. */ private static final double CUTOFF_THRESHOLD = 1e-12; /** Serializable version identifier. */ private static final long serialVersionUID = -1369660067587938365L; /** Linear objective function. */ private final LinearObjectiveFunction f; /** Linear constraints. */ private final List constraints; /** Whether to restrict the variables to non-negative values. */ private final boolean restrictToNonNegative; /** The variables each column represents */ private final List columnLabels = new ArrayList(); /** Simple tableau. */ private transient RealMatrix tableau; /** Number of decision variables. */ private final int numDecisionVariables; /** Number of slack variables. */ private final int numSlackVariables; /** Number of artificial variables. */ private int numArtificialVariables; /** Amount of error to accept when checking for optimality. */ private final double epsilon; /** Amount of error to accept in floating point comparisons. */ private final int maxUlps; /** * Build a tableau for a linear problem. * @param f linear objective function * @param constraints linear constraints * @param goalType type of optimization goal: either {@link GoalType#MAXIMIZE} or {@link GoalType#MINIMIZE} * @param restrictToNonNegative whether to restrict the variables to non-negative values * @param epsilon amount of error to accept when checking for optimality */ SimplexTableau(final LinearObjectiveFunction f, final Collection constraints, final GoalType goalType, final boolean restrictToNonNegative, final double epsilon) { this(f, constraints, goalType, restrictToNonNegative, epsilon, DEFAULT_ULPS); } /** * Build a tableau for a linear problem. * @param f linear objective function * @param constraints linear constraints * @param goalType type of optimization goal: either {@link GoalType#MAXIMIZE} or {@link GoalType#MINIMIZE} * @param restrictToNonNegative whether to restrict the variables to non-negative values * @param epsilon amount of error to accept when checking for optimality * @param maxUlps amount of error to accept in floating point comparisons */ SimplexTableau(final LinearObjectiveFunction f, final Collection constraints, final GoalType goalType, final boolean restrictToNonNegative, final double epsilon, final int maxUlps) { this.f = f; this.constraints = normalizeConstraints(constraints); this.restrictToNonNegative = restrictToNonNegative; this.epsilon = epsilon; this.maxUlps = maxUlps; this.numDecisionVariables = f.getCoefficients().getDimension() + (restrictToNonNegative ? 0 : 1); this.numSlackVariables = getConstraintTypeCounts(Relationship.LEQ) + getConstraintTypeCounts(Relationship.GEQ); this.numArtificialVariables = getConstraintTypeCounts(Relationship.EQ) + getConstraintTypeCounts(Relationship.GEQ); this.tableau = createTableau(goalType == GoalType.MAXIMIZE); initializeColumnLabels(); } /** * Initialize the labels for the columns. */ protected void initializeColumnLabels() { if (getNumObjectiveFunctions() == 2) { columnLabels.add("W"); } columnLabels.add("Z"); for (int i = 0; i < getOriginalNumDecisionVariables(); i++) { columnLabels.add("x" + i); } if (!restrictToNonNegative) { columnLabels.add(NEGATIVE_VAR_COLUMN_LABEL); } for (int i = 0; i < getNumSlackVariables(); i++) { columnLabels.add("s" + i); } for (int i = 0; i < getNumArtificialVariables(); i++) { columnLabels.add("a" + i); } columnLabels.add("RHS"); } /** * Create the tableau by itself. * @param maximize if true, goal is to maximize the objective function * @return created tableau */ protected RealMatrix createTableau(final boolean maximize) { // create a matrix of the correct size int width = numDecisionVariables + numSlackVariables + numArtificialVariables + getNumObjectiveFunctions() + 1; // + 1 is for RHS int height = constraints.size() + getNumObjectiveFunctions(); Array2DRowRealMatrix matrix = new Array2DRowRealMatrix(height, width); // initialize the objective function rows if (getNumObjectiveFunctions() == 2) { matrix.setEntry(0, 0, -1); } int zIndex = (getNumObjectiveFunctions() == 1) ? 0 : 1; matrix.setEntry(zIndex, zIndex, maximize ? 1 : -1); RealVector objectiveCoefficients = maximize ? f.getCoefficients().mapMultiply(-1) : f.getCoefficients(); copyArray(objectiveCoefficients.toArray(), matrix.getDataRef()[zIndex]); matrix.setEntry(zIndex, width - 1, maximize ? f.getConstantTerm() : -1 * f.getConstantTerm()); if (!restrictToNonNegative) { matrix.setEntry(zIndex, getSlackVariableOffset() - 1, getInvertedCoefficientSum(objectiveCoefficients)); } // initialize the constraint rows int slackVar = 0; int artificialVar = 0; for (int i = 0; i < constraints.size(); i++) { LinearConstraint constraint = constraints.get(i); int row = getNumObjectiveFunctions() + i; // decision variable coefficients copyArray(constraint.getCoefficients().toArray(), matrix.getDataRef()[row]); // x- if (!restrictToNonNegative) { matrix.setEntry(row, getSlackVariableOffset() - 1, getInvertedCoefficientSum(constraint.getCoefficients())); } // RHS matrix.setEntry(row, width - 1, constraint.getValue()); // slack variables if (constraint.getRelationship() == Relationship.LEQ) { matrix.setEntry(row, getSlackVariableOffset() + slackVar++, 1); // slack } else if (constraint.getRelationship() == Relationship.GEQ) { matrix.setEntry(row, getSlackVariableOffset() + slackVar++, -1); // excess } // artificial variables if ((constraint.getRelationship() == Relationship.EQ) || (constraint.getRelationship() == Relationship.GEQ)) { matrix.setEntry(0, getArtificialVariableOffset() + artificialVar, 1); matrix.setEntry(row, getArtificialVariableOffset() + artificialVar++, 1); matrix.setRowVector(0, matrix.getRowVector(0).subtract(matrix.getRowVector(row))); } } return matrix; } /** * Get new versions of the constraints which have positive right hand sides. * @param originalConstraints original (not normalized) constraints * @return new versions of the constraints */ public List normalizeConstraints(Collection originalConstraints) { List normalized = new ArrayList(); for (LinearConstraint constraint : originalConstraints) { normalized.add(normalize(constraint)); } return normalized; } /** * Get a new equation equivalent to this one with a positive right hand side. * @param constraint reference constraint * @return new equation */ private LinearConstraint normalize(final LinearConstraint constraint) { if (constraint.getValue() < 0) { return new LinearConstraint(constraint.getCoefficients().mapMultiply(-1), constraint.getRelationship().oppositeRelationship(), -1 * constraint.getValue()); } return new LinearConstraint(constraint.getCoefficients(), constraint.getRelationship(), constraint.getValue()); } /** * Get the number of objective functions in this tableau. * @return 2 for Phase 1. 1 for Phase 2. */ protected final int getNumObjectiveFunctions() { return this.numArtificialVariables > 0 ? 2 : 1; } /** * Get a count of constraints corresponding to a specified relationship. * @param relationship relationship to count * @return number of constraint with the specified relationship */ private int getConstraintTypeCounts(final Relationship relationship) { int count = 0; for (final LinearConstraint constraint : constraints) { if (constraint.getRelationship() == relationship) { ++count; } } return count; } /** * Get the -1 times the sum of all coefficients in the given array. * @param coefficients coefficients to sum * @return the -1 times the sum of all coefficients in the given array. */ protected static double getInvertedCoefficientSum(final RealVector coefficients) { double sum = 0; for (double coefficient : coefficients.toArray()) { sum -= coefficient; } return sum; } /** * Checks whether the given column is basic. * @param col index of the column to check * @return the row that the variable is basic in. null if the column is not basic */ protected Integer getBasicRow(final int col) { Integer row = null; for (int i = 0; i < getHeight(); i++) { final double entry = getEntry(i, col); if (Precision.equals(entry, 1d, maxUlps) && (row == null)) { row = i; } else if (!Precision.equals(entry, 0d, maxUlps)) { return null; } } return row; } /** * Removes the phase 1 objective function, positive cost non-artificial variables, * and the non-basic artificial variables from this tableau. */ protected void dropPhase1Objective() { if (getNumObjectiveFunctions() == 1) { return; } Set columnsToDrop = new TreeSet(); columnsToDrop.add(0); // positive cost non-artificial variables for (int i = getNumObjectiveFunctions(); i < getArtificialVariableOffset(); i++) { final double entry = tableau.getEntry(0, i); if (Precision.compareTo(entry, 0d, epsilon) > 0) { columnsToDrop.add(i); } } // non-basic artificial variables for (int i = 0; i < getNumArtificialVariables(); i++) { int col = i + getArtificialVariableOffset(); if (getBasicRow(col) == null) { columnsToDrop.add(col); } } double[][] matrix = new double[getHeight() - 1][getWidth() - columnsToDrop.size()]; for (int i = 1; i < getHeight(); i++) { int col = 0; for (int j = 0; j < getWidth(); j++) { if (!columnsToDrop.contains(j)) { matrix[i - 1][col++] = tableau.getEntry(i, j); } } } // remove the columns in reverse order so the indices are correct Integer[] drop = columnsToDrop.toArray(new Integer[columnsToDrop.size()]); for (int i = drop.length - 1; i >= 0; i--) { columnLabels.remove((int) drop[i]); } this.tableau = new Array2DRowRealMatrix(matrix); this.numArtificialVariables = 0; } /** * @param src the source array * @param dest the destination array */ private void copyArray(final double[] src, final double[] dest) { System.arraycopy(src, 0, dest, getNumObjectiveFunctions(), src.length); } /** * Returns whether the problem is at an optimal state. * @return whether the model has been solved */ boolean isOptimal() { for (int i = getNumObjectiveFunctions(); i < getWidth() - 1; i++) { final double entry = tableau.getEntry(0, i); if (Precision.compareTo(entry, 0d, epsilon) < 0) { return false; } } return true; } /** * Get the current solution. * @return current solution */ protected PointValuePair getSolution() { int negativeVarColumn = columnLabels.indexOf(NEGATIVE_VAR_COLUMN_LABEL); Integer negativeVarBasicRow = negativeVarColumn > 0 ? getBasicRow(negativeVarColumn) : null; double mostNegative = negativeVarBasicRow == null ? 0 : getEntry(negativeVarBasicRow, getRhsOffset()); Set basicRows = new HashSet(); double[] coefficients = new double[getOriginalNumDecisionVariables()]; for (int i = 0; i < coefficients.length; i++) { int colIndex = columnLabels.indexOf("x" + i); if (colIndex < 0) { coefficients[i] = 0; continue; } Integer basicRow = getBasicRow(colIndex); if (basicRow != null && basicRow == 0) { // if the basic row is found to be the objective function row // set the coefficient to 0 -> this case handles unconstrained // variables that are still part of the objective function coefficients[i] = 0; } else if (basicRows.contains(basicRow)) { // if multiple variables can take a given value // then we choose the first and set the rest equal to 0 coefficients[i] = 0 - (restrictToNonNegative ? 0 : mostNegative); } else { basicRows.add(basicRow); coefficients[i] = (basicRow == null ? 0 : getEntry(basicRow, getRhsOffset())) - (restrictToNonNegative ? 0 : mostNegative); } } return new PointValuePair(coefficients, f.getValue(coefficients)); } /** * Subtracts a multiple of one row from another. *

                          * After application of this operation, the following will hold: *

                          minuendRow = minuendRow - multiple * subtrahendRow
                          * * @param dividendRow index of the row * @param divisor value of the divisor */ protected void divideRow(final int dividendRow, final double divisor) { for (int j = 0; j < getWidth(); j++) { tableau.setEntry(dividendRow, j, tableau.getEntry(dividendRow, j) / divisor); } } /** * Subtracts a multiple of one row from another. *

                          * After application of this operation, the following will hold: *

                          minuendRow = minuendRow - multiple * subtrahendRow
                          * * @param minuendRow row index * @param subtrahendRow row index * @param multiple multiplication factor */ protected void subtractRow(final int minuendRow, final int subtrahendRow, final double multiple) { for (int i = 0; i < getWidth(); i++) { double result = tableau.getEntry(minuendRow, i) - tableau.getEntry(subtrahendRow, i) * multiple; // cut-off values smaller than the CUTOFF_THRESHOLD, otherwise may lead to numerical instabilities if (FastMath.abs(result) < CUTOFF_THRESHOLD) { result = 0.0; } tableau.setEntry(minuendRow, i, result); } } /** * Get the width of the tableau. * @return width of the tableau */ protected final int getWidth() { return tableau.getColumnDimension(); } /** * Get the height of the tableau. * @return height of the tableau */ protected final int getHeight() { return tableau.getRowDimension(); } /** * Get an entry of the tableau. * @param row row index * @param column column index * @return entry at (row, column) */ protected final double getEntry(final int row, final int column) { return tableau.getEntry(row, column); } /** * Set an entry of the tableau. * @param row row index * @param column column index * @param value for the entry */ protected final void setEntry(final int row, final int column, final double value) { tableau.setEntry(row, column, value); } /** * Get the offset of the first slack variable. * @return offset of the first slack variable */ protected final int getSlackVariableOffset() { return getNumObjectiveFunctions() + numDecisionVariables; } /** * Get the offset of the first artificial variable. * @return offset of the first artificial variable */ protected final int getArtificialVariableOffset() { return getNumObjectiveFunctions() + numDecisionVariables + numSlackVariables; } /** * Get the offset of the right hand side. * @return offset of the right hand side */ protected final int getRhsOffset() { return getWidth() - 1; } /** * Get the number of decision variables. *

                          * If variables are not restricted to positive values, this will include 1 extra decision variable to represent * the absolute value of the most negative variable. * * @return number of decision variables * @see #getOriginalNumDecisionVariables() */ protected final int getNumDecisionVariables() { return numDecisionVariables; } /** * Get the original number of decision variables. * @return original number of decision variables * @see #getNumDecisionVariables() */ protected final int getOriginalNumDecisionVariables() { return f.getCoefficients().getDimension(); } /** * Get the number of slack variables. * @return number of slack variables */ protected final int getNumSlackVariables() { return numSlackVariables; } /** * Get the number of artificial variables. * @return number of artificial variables */ protected final int getNumArtificialVariables() { return numArtificialVariables; } /** * Get the tableau data. * @return tableau data */ protected final double[][] getData() { return tableau.getData(); } @Override public boolean equals(Object other) { if (this == other) { return true; } if (other instanceof SimplexTableau) { SimplexTableau rhs = (SimplexTableau) other; return (restrictToNonNegative == rhs.restrictToNonNegative) && (numDecisionVariables == rhs.numDecisionVariables) && (numSlackVariables == rhs.numSlackVariables) && (numArtificialVariables == rhs.numArtificialVariables) && (epsilon == rhs.epsilon) && (maxUlps == rhs.maxUlps) && f.equals(rhs.f) && constraints.equals(rhs.constraints) && tableau.equals(rhs.tableau); } return false; } @Override public int hashCode() { return Boolean.valueOf(restrictToNonNegative).hashCode() ^ numDecisionVariables ^ numSlackVariables ^ numArtificialVariables ^ Double.valueOf(epsilon).hashCode() ^ maxUlps ^ f.hashCode() ^ constraints.hashCode() ^ tableau.hashCode(); } /** * Serialize the instance. * @param oos stream where object should be written * @throws IOException if object cannot be written to stream */ private void writeObject(ObjectOutputStream oos) throws IOException { oos.defaultWriteObject(); MatrixUtils.serializeRealMatrix(tableau, oos); } /** * Deserialize the instance. * @param ois stream from which the object should be read * @throws ClassNotFoundException if a class in the stream cannot be found * @throws IOException if object cannot be read from the stream */ private void readObject(ObjectInputStream ois) throws ClassNotFoundException, IOException { ois.defaultReadObject(); MatrixUtils.deserializeRealMatrix(this, "tableau", ois); } } ././@LongLink100644 0 0 161 12126630646 10257 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/optimization/linear/UnboundedSolutionException.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/optimization/linear/UnboundedSolutionEx100644 1750 1750 3126 12126627713 32425 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.optimization.linear; import org.apache.commons.math3.exception.MathIllegalStateException; import org.apache.commons.math3.exception.util.LocalizedFormats; /** * This class represents exceptions thrown by optimizers when a solution escapes to infinity. * * @version $Id: UnboundedSolutionException.java 1422230 2012-12-15 12:11:13Z erans $ * @deprecated As of 3.1 (to be removed in 4.0). * @since 2.0 */ @Deprecated public class UnboundedSolutionException extends MathIllegalStateException { /** Serializable version identifier. */ private static final long serialVersionUID = 940539497277290619L; /** * Simple constructor using a default message. */ public UnboundedSolutionException() { super(LocalizedFormats.UNBOUNDED_SOLUTION); } } ././@LongLink100644 0 0 156 12126630646 10263 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/optimization/linear/AbstractLinearOptimizer.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/optimization/linear/AbstractLinearOptim100644 1750 1750 11566 12126627713 32406 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.optimization.linear; import java.util.Collection; import java.util.Collections; import org.apache.commons.math3.exception.MathIllegalStateException; import org.apache.commons.math3.exception.MaxCountExceededException; import org.apache.commons.math3.optimization.GoalType; import org.apache.commons.math3.optimization.PointValuePair; /** * Base class for implementing linear optimizers. *

                          * This base class handles the boilerplate methods associated to thresholds * settings and iterations counters. * * @version $Id: AbstractLinearOptimizer.java 1422230 2012-12-15 12:11:13Z erans $ * @deprecated As of 3.1 (to be removed in 4.0). * @since 2.0 */ @Deprecated public abstract class AbstractLinearOptimizer implements LinearOptimizer { /** Default maximal number of iterations allowed. */ public static final int DEFAULT_MAX_ITERATIONS = 100; /** * Linear objective function. * @since 2.1 */ private LinearObjectiveFunction function; /** * Linear constraints. * @since 2.1 */ private Collection linearConstraints; /** * Type of optimization goal: either {@link GoalType#MAXIMIZE} or {@link GoalType#MINIMIZE}. * @since 2.1 */ private GoalType goal; /** * Whether to restrict the variables to non-negative values. * @since 2.1 */ private boolean nonNegative; /** Maximal number of iterations allowed. */ private int maxIterations; /** Number of iterations already performed. */ private int iterations; /** * Simple constructor with default settings. *

                          The maximal number of evaluation is set to its default value.

                          */ protected AbstractLinearOptimizer() { setMaxIterations(DEFAULT_MAX_ITERATIONS); } /** * @return {@code true} if the variables are restricted to non-negative values. */ protected boolean restrictToNonNegative() { return nonNegative; } /** * @return the optimization type. */ protected GoalType getGoalType() { return goal; } /** * @return the optimization type. */ protected LinearObjectiveFunction getFunction() { return function; } /** * @return the optimization type. */ protected Collection getConstraints() { return Collections.unmodifiableCollection(linearConstraints); } /** {@inheritDoc} */ public void setMaxIterations(int maxIterations) { this.maxIterations = maxIterations; } /** {@inheritDoc} */ public int getMaxIterations() { return maxIterations; } /** {@inheritDoc} */ public int getIterations() { return iterations; } /** * Increment the iterations counter by 1. * @exception MaxCountExceededException if the maximal number of iterations is exceeded */ protected void incrementIterationsCounter() throws MaxCountExceededException { if (++iterations > maxIterations) { throw new MaxCountExceededException(maxIterations); } } /** {@inheritDoc} */ public PointValuePair optimize(final LinearObjectiveFunction f, final Collection constraints, final GoalType goalType, final boolean restrictToNonNegative) throws MathIllegalStateException { // store linear problem characteristics this.function = f; this.linearConstraints = constraints; this.goal = goalType; this.nonNegative = restrictToNonNegative; iterations = 0; // solve the problem return doOptimize(); } /** * Perform the bulk of optimization algorithm. * @return the point/value pair giving the optimal value for objective function * @exception MathIllegalStateException if no solution fulfilling the constraints * can be found in the allowed number of iterations */ protected abstract PointValuePair doOptimize() throws MathIllegalStateException; } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/optimization/general/package-info.java100644 1750 1750 1663 12126627713 32063 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ /** * * This package provides optimization algorithms that require derivatives. * */ package org.apache.commons.math3.optimization.general; ././@LongLink100644 0 0 165 12126630646 10263 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/optimization/general/AbstractLeastSquaresOptimizer.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/optimization/general/AbstractLeastSquar100644 1750 1750 56226 12126627713 32414 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.optimization.general; import org.apache.commons.math3.analysis.DifferentiableMultivariateVectorFunction; import org.apache.commons.math3.analysis.FunctionUtils; import org.apache.commons.math3.analysis.differentiation.DerivativeStructure; import org.apache.commons.math3.analysis.differentiation.MultivariateDifferentiableVectorFunction; import org.apache.commons.math3.exception.DimensionMismatchException; import org.apache.commons.math3.exception.NumberIsTooSmallException; import org.apache.commons.math3.exception.util.LocalizedFormats; import org.apache.commons.math3.linear.ArrayRealVector; import org.apache.commons.math3.linear.RealMatrix; import org.apache.commons.math3.linear.DiagonalMatrix; import org.apache.commons.math3.linear.DecompositionSolver; import org.apache.commons.math3.linear.MatrixUtils; import org.apache.commons.math3.linear.QRDecomposition; import org.apache.commons.math3.linear.EigenDecomposition; import org.apache.commons.math3.optimization.OptimizationData; import org.apache.commons.math3.optimization.InitialGuess; import org.apache.commons.math3.optimization.Target; import org.apache.commons.math3.optimization.Weight; import org.apache.commons.math3.optimization.ConvergenceChecker; import org.apache.commons.math3.optimization.DifferentiableMultivariateVectorOptimizer; import org.apache.commons.math3.optimization.PointVectorValuePair; import org.apache.commons.math3.optimization.direct.BaseAbstractMultivariateVectorOptimizer; import org.apache.commons.math3.util.FastMath; /** * Base class for implementing least squares optimizers. * It handles the boilerplate methods associated to thresholds settings, * Jacobian and error estimation. *
                          * This class constructs the Jacobian matrix of the function argument in method * {@link BaseAbstractMultivariateVectorOptimizer#optimize(int,MultivariateVectorFunction,OptimizationData[]) * optimize} and assumes that the rows of that matrix iterate on the model * functions while the columns iterate on the parameters; thus, the numbers * of rows is equal to the dimension of the * {@link org.apache.commons.math3.optimization.Target Target} while * the number of columns is equal to the dimension of the * {@link org.apache.commons.math3.optimization.InitialGuess InitialGuess}. * * @version $Id: AbstractLeastSquaresOptimizer.java 1426759 2012-12-29 13:26:44Z erans $ * @deprecated As of 3.1 (to be removed in 4.0). * @since 1.2 */ @Deprecated public abstract class AbstractLeastSquaresOptimizer extends BaseAbstractMultivariateVectorOptimizer implements DifferentiableMultivariateVectorOptimizer { /** * Singularity threshold (cf. {@link #getCovariances(double)}). * @deprecated As of 3.1. */ @Deprecated private static final double DEFAULT_SINGULARITY_THRESHOLD = 1e-14; /** * Jacobian matrix of the weighted residuals. * This matrix is in canonical form just after the calls to * {@link #updateJacobian()}, but may be modified by the solver * in the derived class (the {@link LevenbergMarquardtOptimizer * Levenberg-Marquardt optimizer} does this). * @deprecated As of 3.1. To be removed in 4.0. Please use * {@link #computeWeightedJacobian(double[])} instead. */ @Deprecated protected double[][] weightedResidualJacobian; /** Number of columns of the jacobian matrix. * @deprecated As of 3.1. */ @Deprecated protected int cols; /** Number of rows of the jacobian matrix. * @deprecated As of 3.1. */ @Deprecated protected int rows; /** Current point. * @deprecated As of 3.1. */ @Deprecated protected double[] point; /** Current objective function value. * @deprecated As of 3.1. */ @Deprecated protected double[] objective; /** Weighted residuals * @deprecated As of 3.1. */ @Deprecated protected double[] weightedResiduals; /** Cost value (square root of the sum of the residuals). * @deprecated As of 3.1. Field to become "private" in 4.0. * Please use {@link #setCost(double)}. */ @Deprecated protected double cost; /** Objective function derivatives. */ private MultivariateDifferentiableVectorFunction jF; /** Number of evaluations of the Jacobian. */ private int jacobianEvaluations; /** Square-root of the weight matrix. */ private RealMatrix weightMatrixSqrt; /** * Simple constructor with default settings. * The convergence check is set to a {@link * org.apache.commons.math3.optimization.SimpleVectorValueChecker}. * @deprecated See {@link org.apache.commons.math3.optimization.SimpleValueChecker#SimpleValueChecker()} */ @Deprecated protected AbstractLeastSquaresOptimizer() {} /** * @param checker Convergence checker. */ protected AbstractLeastSquaresOptimizer(ConvergenceChecker checker) { super(checker); } /** * @return the number of evaluations of the Jacobian function. */ public int getJacobianEvaluations() { return jacobianEvaluations; } /** * Update the jacobian matrix. * * @throws DimensionMismatchException if the Jacobian dimension does not * match problem dimension. * @deprecated As of 3.1. Please use {@link #computeWeightedJacobian(double[])} * instead. */ @Deprecated protected void updateJacobian() { final RealMatrix weightedJacobian = computeWeightedJacobian(point); weightedResidualJacobian = weightedJacobian.scalarMultiply(-1).getData(); } /** * Computes the Jacobian matrix. * * @param params Model parameters at which to compute the Jacobian. * @return the weighted Jacobian: W1/2 J. * @throws DimensionMismatchException if the Jacobian dimension does not * match problem dimension. * @since 3.1 */ protected RealMatrix computeWeightedJacobian(double[] params) { ++jacobianEvaluations; final DerivativeStructure[] dsPoint = new DerivativeStructure[params.length]; final int nC = params.length; for (int i = 0; i < nC; ++i) { dsPoint[i] = new DerivativeStructure(nC, 1, i, params[i]); } final DerivativeStructure[] dsValue = jF.value(dsPoint); final int nR = getTarget().length; if (dsValue.length != nR) { throw new DimensionMismatchException(dsValue.length, nR); } final double[][] jacobianData = new double[nR][nC]; for (int i = 0; i < nR; ++i) { int[] orders = new int[nC]; for (int j = 0; j < nC; ++j) { orders[j] = 1; jacobianData[i][j] = dsValue[i].getPartialDerivative(orders); orders[j] = 0; } } return weightMatrixSqrt.multiply(MatrixUtils.createRealMatrix(jacobianData)); } /** * Update the residuals array and cost function value. * @throws DimensionMismatchException if the dimension does not match the * problem dimension. * @throws org.apache.commons.math3.exception.TooManyEvaluationsException * if the maximal number of evaluations is exceeded. * @deprecated As of 3.1. Please use {@link #computeResiduals(double[])}, * {@link #computeObjectiveValue(double[])}, {@link #computeCost(double[])} * and {@link #setCost(double)} instead. */ @Deprecated protected void updateResidualsAndCost() { objective = computeObjectiveValue(point); final double[] res = computeResiduals(objective); // Compute cost. cost = computeCost(res); // Compute weighted residuals. final ArrayRealVector residuals = new ArrayRealVector(res); weightedResiduals = weightMatrixSqrt.operate(residuals).toArray(); } /** * Computes the cost. * * @param residuals Residuals. * @return the cost. * @see #computeResiduals(double[]) * @since 3.1 */ protected double computeCost(double[] residuals) { final ArrayRealVector r = new ArrayRealVector(residuals); return FastMath.sqrt(r.dotProduct(getWeight().operate(r))); } /** * Get the Root Mean Square value. * Get the Root Mean Square value, i.e. the root of the arithmetic * mean of the square of all weighted residuals. This is related to the * criterion that is minimized by the optimizer as follows: if * c if the criterion, and n is the number of * measurements, then the RMS is sqrt (c/n). * * @return RMS value */ public double getRMS() { return FastMath.sqrt(getChiSquare() / rows); } /** * Get a Chi-Square-like value assuming the N residuals follow N * distinct normal distributions centered on 0 and whose variances are * the reciprocal of the weights. * @return chi-square value */ public double getChiSquare() { return cost * cost; } /** * Gets the square-root of the weight matrix. * * @return the square-root of the weight matrix. * @since 3.1 */ public RealMatrix getWeightSquareRoot() { return weightMatrixSqrt.copy(); } /** * Sets the cost. * * @param cost Cost value. * @since 3.1 */ protected void setCost(double cost) { this.cost = cost; } /** * Get the covariance matrix of the optimized parameters. * * @return the covariance matrix. * @throws org.apache.commons.math3.linear.SingularMatrixException * if the covariance matrix cannot be computed (singular problem). * @see #getCovariances(double) * @deprecated As of 3.1. Please use {@link #computeCovariances(double[],double)} * instead. */ @Deprecated public double[][] getCovariances() { return getCovariances(DEFAULT_SINGULARITY_THRESHOLD); } /** * Get the covariance matrix of the optimized parameters. *
                          * Note that this operation involves the inversion of the * JTJ matrix, where {@code J} is the * Jacobian matrix. * The {@code threshold} parameter is a way for the caller to specify * that the result of this computation should be considered meaningless, * and thus trigger an exception. * * @param threshold Singularity threshold. * @return the covariance matrix. * @throws org.apache.commons.math3.linear.SingularMatrixException * if the covariance matrix cannot be computed (singular problem). * @deprecated As of 3.1. Please use {@link #computeCovariances(double[],double)} * instead. */ @Deprecated public double[][] getCovariances(double threshold) { return computeCovariances(point, threshold); } /** * Get the covariance matrix of the optimized parameters. *
                          * Note that this operation involves the inversion of the * JTJ matrix, where {@code J} is the * Jacobian matrix. * The {@code threshold} parameter is a way for the caller to specify * that the result of this computation should be considered meaningless, * and thus trigger an exception. * * @param params Model parameters. * @param threshold Singularity threshold. * @return the covariance matrix. * @throws org.apache.commons.math3.linear.SingularMatrixException * if the covariance matrix cannot be computed (singular problem). * @since 3.1 */ public double[][] computeCovariances(double[] params, double threshold) { // Set up the Jacobian. final RealMatrix j = computeWeightedJacobian(params); // Compute transpose(J)J. final RealMatrix jTj = j.transpose().multiply(j); // Compute the covariances matrix. final DecompositionSolver solver = new QRDecomposition(jTj, threshold).getSolver(); return solver.getInverse().getData(); } /** *

                          * Returns an estimate of the standard deviation of each parameter. The * returned values are the so-called (asymptotic) standard errors on the * parameters, defined as {@code sd(a[i]) = sqrt(S / (n - m) * C[i][i])}, * where {@code a[i]} is the optimized value of the {@code i}-th parameter, * {@code S} is the minimized value of the sum of squares objective function * (as returned by {@link #getChiSquare()}), {@code n} is the number of * observations, {@code m} is the number of parameters and {@code C} is the * covariance matrix. *

                          *

                          * See also * Wikipedia, * or * MathWorld, * equations (34) and (35) for a particular case. *

                          * * @return an estimate of the standard deviation of the optimized parameters * @throws org.apache.commons.math3.linear.SingularMatrixException * if the covariance matrix cannot be computed. * @throws NumberIsTooSmallException if the number of degrees of freedom is not * positive, i.e. the number of measurements is less or equal to the number of * parameters. * @deprecated as of version 3.1, {@link #computeSigma(double[],double)} should be used * instead. It should be emphasized that {@code guessParametersErrors} and * {@code computeSigma} are not strictly equivalent. */ @Deprecated public double[] guessParametersErrors() { if (rows <= cols) { throw new NumberIsTooSmallException(LocalizedFormats.NO_DEGREES_OF_FREEDOM, rows, cols, false); } double[] errors = new double[cols]; final double c = FastMath.sqrt(getChiSquare() / (rows - cols)); double[][] covar = computeCovariances(point, 1e-14); for (int i = 0; i < errors.length; ++i) { errors[i] = FastMath.sqrt(covar[i][i]) * c; } return errors; } /** * Computes an estimate of the standard deviation of the parameters. The * returned values are the square root of the diagonal coefficients of the * covariance matrix, {@code sd(a[i]) ~= sqrt(C[i][i])}, where {@code a[i]} * is the optimized value of the {@code i}-th parameter, and {@code C} is * the covariance matrix. * * @param params Model parameters. * @param covarianceSingularityThreshold Singularity threshold (see * {@link #computeCovariances(double[],double) computeCovariances}). * @return an estimate of the standard deviation of the optimized parameters * @throws org.apache.commons.math3.linear.SingularMatrixException * if the covariance matrix cannot be computed. * @since 3.1 */ public double[] computeSigma(double[] params, double covarianceSingularityThreshold) { final int nC = params.length; final double[] sig = new double[nC]; final double[][] cov = computeCovariances(params, covarianceSingularityThreshold); for (int i = 0; i < nC; ++i) { sig[i] = FastMath.sqrt(cov[i][i]); } return sig; } /** {@inheritDoc} * @deprecated As of 3.1. Please use * {@link BaseAbstractMultivariateVectorOptimizer#optimize(int,MultivariateVectorFunction,OptimizationData[]) * optimize(int,MultivariateDifferentiableVectorFunction,OptimizationData...)} * instead. */ @Override @Deprecated public PointVectorValuePair optimize(int maxEval, final DifferentiableMultivariateVectorFunction f, final double[] target, final double[] weights, final double[] startPoint) { return optimizeInternal(maxEval, FunctionUtils.toMultivariateDifferentiableVectorFunction(f), new Target(target), new Weight(weights), new InitialGuess(startPoint)); } /** * Optimize an objective function. * Optimization is considered to be a weighted least-squares minimization. * The cost function to be minimized is * ∑weighti(objectivei - targeti)2 * * @param f Objective function. * @param target Target value for the objective functions at optimum. * @param weights Weights for the least squares cost computation. * @param startPoint Start point for optimization. * @return the point/value pair giving the optimal value for objective * function. * @param maxEval Maximum number of function evaluations. * @throws org.apache.commons.math3.exception.DimensionMismatchException * if the start point dimension is wrong. * @throws org.apache.commons.math3.exception.TooManyEvaluationsException * if the maximal number of evaluations is exceeded. * @throws org.apache.commons.math3.exception.NullArgumentException if * any argument is {@code null}. * @deprecated As of 3.1. Please use * {@link BaseAbstractMultivariateVectorOptimizer#optimize(int,MultivariateVectorFunction,OptimizationData[]) * optimize(int,MultivariateDifferentiableVectorFunction,OptimizationData...)} * instead. */ @Deprecated public PointVectorValuePair optimize(final int maxEval, final MultivariateDifferentiableVectorFunction f, final double[] target, final double[] weights, final double[] startPoint) { return optimizeInternal(maxEval, f, new Target(target), new Weight(weights), new InitialGuess(startPoint)); } /** * Optimize an objective function. * Optimization is considered to be a weighted least-squares minimization. * The cost function to be minimized is * ∑weighti(objectivei - targeti)2 * * @param maxEval Allowed number of evaluations of the objective function. * @param f Objective function. * @param optData Optimization data. The following data will be looked for: *
                            *
                          • {@link Target}
                          • *
                          • {@link Weight}
                          • *
                          • {@link InitialGuess}
                          • *
                          * @return the point/value pair giving the optimal value of the objective * function. * @throws org.apache.commons.math3.exception.TooManyEvaluationsException if * the maximal number of evaluations is exceeded. * @throws DimensionMismatchException if the target, and weight arguments * have inconsistent dimensions. * @see BaseAbstractMultivariateVectorOptimizer#optimizeInternal(int,MultivariateVectorFunction,OptimizationData[]) * @since 3.1 * @deprecated As of 3.1. Override is necessary only until this class's generic * argument is changed to {@code MultivariateDifferentiableVectorFunction}. */ @Deprecated protected PointVectorValuePair optimizeInternal(final int maxEval, final MultivariateDifferentiableVectorFunction f, OptimizationData... optData) { // XXX Conversion will be removed when the generic argument of the // base class becomes "MultivariateDifferentiableVectorFunction". return super.optimizeInternal(maxEval, FunctionUtils.toDifferentiableMultivariateVectorFunction(f), optData); } /** {@inheritDoc} */ @Override protected void setUp() { super.setUp(); // Reset counter. jacobianEvaluations = 0; // Square-root of the weight matrix. weightMatrixSqrt = squareRoot(getWeight()); // Store least squares problem characteristics. // XXX The conversion won't be necessary when the generic argument of // the base class becomes "MultivariateDifferentiableVectorFunction". // XXX "jF" is not strictly necessary anymore but is currently more // efficient than converting the value returned from "getObjectiveFunction()" // every time it is used. jF = FunctionUtils.toMultivariateDifferentiableVectorFunction((DifferentiableMultivariateVectorFunction) getObjectiveFunction()); // Arrays shared with "private" and "protected" methods. point = getStartPoint(); rows = getTarget().length; cols = point.length; } /** * Computes the residuals. * The residual is the difference between the observed (target) * values and the model (objective function) value. * There is one residual for each element of the vector-valued * function. * * @param objectiveValue Value of the the objective function. This is * the value returned from a call to * {@link #computeObjectiveValue(double[]) computeObjectiveValue} * (whose array argument contains the model parameters). * @return the residuals. * @throws DimensionMismatchException if {@code params} has a wrong * length. * @since 3.1 */ protected double[] computeResiduals(double[] objectiveValue) { final double[] target = getTarget(); if (objectiveValue.length != target.length) { throw new DimensionMismatchException(target.length, objectiveValue.length); } final double[] residuals = new double[target.length]; for (int i = 0; i < target.length; i++) { residuals[i] = target[i] - objectiveValue[i]; } return residuals; } /** * Computes the square-root of the weight matrix. * * @param m Symmetric, positive-definite (weight) matrix. * @return the square-root of the weight matrix. */ private RealMatrix squareRoot(RealMatrix m) { if (m instanceof DiagonalMatrix) { final int dim = m.getRowDimension(); final RealMatrix sqrtM = new DiagonalMatrix(dim); for (int i = 0; i < dim; i++) { sqrtM.setEntry(i, i, FastMath.sqrt(m.getEntry(i, i))); } return sqrtM; } else { final EigenDecomposition dec = new EigenDecomposition(m); return dec.getSquareRoot(); } } } ././@LongLink100644 0 0 160 12126630646 10256 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/optimization/general/ConjugateGradientFormula.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/optimization/general/ConjugateGradientF100644 1750 1750 3656 12126627713 32326 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.optimization.general; /** * Available choices of update formulas for the β parameter * in {@link NonLinearConjugateGradientOptimizer}. *

                          * The β parameter is used to compute the successive conjugate * search directions. For non-linear conjugate gradients, there are * two formulas to compute β: *

                            *
                          • Fletcher-Reeves formula
                          • *
                          • Polak-Ribière formula
                          • *
                          * On the one hand, the Fletcher-Reeves formula is guaranteed to converge * if the start point is close enough of the optimum whether the * Polak-Ribière formula may not converge in rare cases. On the * other hand, the Polak-Ribière formula is often faster when it * does converge. Polak-Ribière is often used. *

                          * @see NonLinearConjugateGradientOptimizer * @version $Id: ConjugateGradientFormula.java 1422230 2012-12-15 12:11:13Z erans $ * @deprecated As of 3.1 (to be removed in 4.0). * @since 2.0 */ @Deprecated public enum ConjugateGradientFormula { /** Fletcher-Reeves formula. */ FLETCHER_REEVES, /** Polak-Ribière formula. */ POLAK_RIBIERE } ././@LongLink100644 0 0 163 12126630646 10261 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/optimization/general/LevenbergMarquardtOptimizer.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/optimization/general/LevenbergMarquardt100644 1750 1750 116337 12126627713 32456 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.optimization.general; import java.util.Arrays; import org.apache.commons.math3.exception.ConvergenceException; import org.apache.commons.math3.exception.util.LocalizedFormats; import org.apache.commons.math3.optimization.PointVectorValuePair; import org.apache.commons.math3.optimization.ConvergenceChecker; import org.apache.commons.math3.linear.RealMatrix; import org.apache.commons.math3.util.Precision; import org.apache.commons.math3.util.FastMath; /** * This class solves a least squares problem using the Levenberg-Marquardt algorithm. * *

                          This implementation should work even for over-determined systems * (i.e. systems having more point than equations). Over-determined systems * are solved by ignoring the point which have the smallest impact according * to their jacobian column norm. Only the rank of the matrix and some loop bounds * are changed to implement this.

                          * *

                          The resolution engine is a simple translation of the MINPACK lmder routine with minor * changes. The changes include the over-determined resolution, the use of * inherited convergence checker and the Q.R. decomposition which has been * rewritten following the algorithm described in the * P. Lascaux and R. Theodor book Analyse numérique matricielle * appliquée à l'art de l'ingénieur, Masson 1986.

                          *

                          The authors of the original fortran version are: *

                            *
                          • Argonne National Laboratory. MINPACK project. March 1980
                          • *
                          • Burton S. Garbow
                          • *
                          • Kenneth E. Hillstrom
                          • *
                          • Jorge J. More
                          • *
                          * The redistribution policy for MINPACK is available here, for convenience, it * is reproduced below.

                          * * * * *
                          * Minpack Copyright Notice (1999) University of Chicago. * All rights reserved *
                          * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: *
                            *
                          1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer.
                          2. *
                          3. Redistributions in binary form must reproduce the above * copyright notice, this list of conditions and the following * disclaimer in the documentation and/or other materials provided * with the distribution.
                          4. *
                          5. The end-user documentation included with the redistribution, if any, * must include the following acknowledgment: * This product includes software developed by the University of * Chicago, as Operator of Argonne National Laboratory. * Alternately, this acknowledgment may appear in the software itself, * if and wherever such third-party acknowledgments normally appear.
                          6. *
                          7. WARRANTY DISCLAIMER. THE SOFTWARE IS SUPPLIED "AS IS" * WITHOUT WARRANTY OF ANY KIND. THE COPYRIGHT HOLDER, THE * UNITED STATES, THE UNITED STATES DEPARTMENT OF ENERGY, AND * THEIR EMPLOYEES: (1) DISCLAIM ANY WARRANTIES, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO ANY IMPLIED WARRANTIES * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE * OR NON-INFRINGEMENT, (2) DO NOT ASSUME ANY LEGAL LIABILITY * OR RESPONSIBILITY FOR THE ACCURACY, COMPLETENESS, OR * USEFULNESS OF THE SOFTWARE, (3) DO NOT REPRESENT THAT USE OF * THE SOFTWARE WOULD NOT INFRINGE PRIVATELY OWNED RIGHTS, (4) * DO NOT WARRANT THAT THE SOFTWARE WILL FUNCTION * UNINTERRUPTED, THAT IT IS ERROR-FREE OR THAT ANY ERRORS WILL * BE CORRECTED.
                          8. *
                          9. LIMITATION OF LIABILITY. IN NO EVENT WILL THE COPYRIGHT * HOLDER, THE UNITED STATES, THE UNITED STATES DEPARTMENT OF * ENERGY, OR THEIR EMPLOYEES: BE LIABLE FOR ANY INDIRECT, * INCIDENTAL, CONSEQUENTIAL, SPECIAL OR PUNITIVE DAMAGES OF * ANY KIND OR NATURE, INCLUDING BUT NOT LIMITED TO LOSS OF * PROFITS OR LOSS OF DATA, FOR ANY REASON WHATSOEVER, WHETHER * SUCH LIABILITY IS ASSERTED ON THE BASIS OF CONTRACT, TORT * (INCLUDING NEGLIGENCE OR STRICT LIABILITY), OR OTHERWISE, * EVEN IF ANY OF SAID PARTIES HAS BEEN WARNED OF THE * POSSIBILITY OF SUCH LOSS OR DAMAGES.
                          10. *
                            * @version $Id: LevenbergMarquardtOptimizer.java 1462503 2013-03-29 15:48:27Z luc $ * @deprecated As of 3.1 (to be removed in 4.0). * @since 2.0 * */ @Deprecated public class LevenbergMarquardtOptimizer extends AbstractLeastSquaresOptimizer { /** Number of solved point. */ private int solvedCols; /** Diagonal elements of the R matrix in the Q.R. decomposition. */ private double[] diagR; /** Norms of the columns of the jacobian matrix. */ private double[] jacNorm; /** Coefficients of the Householder transforms vectors. */ private double[] beta; /** Columns permutation array. */ private int[] permutation; /** Rank of the jacobian matrix. */ private int rank; /** Levenberg-Marquardt parameter. */ private double lmPar; /** Parameters evolution direction associated with lmPar. */ private double[] lmDir; /** Positive input variable used in determining the initial step bound. */ private final double initialStepBoundFactor; /** Desired relative error in the sum of squares. */ private final double costRelativeTolerance; /** Desired relative error in the approximate solution parameters. */ private final double parRelativeTolerance; /** Desired max cosine on the orthogonality between the function vector * and the columns of the jacobian. */ private final double orthoTolerance; /** Threshold for QR ranking. */ private final double qrRankingThreshold; /** Weighted residuals. */ private double[] weightedResidual; /** Weighted Jacobian. */ private double[][] weightedJacobian; /** * Build an optimizer for least squares problems with default values * for all the tuning parameters (see the {@link * #LevenbergMarquardtOptimizer(double,double,double,double,double) * other contructor}. * The default values for the algorithm settings are: *
                              *
                            • Initial step bound factor: 100
                            • *
                            • Cost relative tolerance: 1e-10
                            • *
                            • Parameters relative tolerance: 1e-10
                            • *
                            • Orthogonality tolerance: 1e-10
                            • *
                            • QR ranking threshold: {@link Precision#SAFE_MIN}
                            • *
                            */ public LevenbergMarquardtOptimizer() { this(100, 1e-10, 1e-10, 1e-10, Precision.SAFE_MIN); } /** * Constructor that allows the specification of a custom convergence * checker. * Note that all the usual convergence checks will be disabled. * The default values for the algorithm settings are: *
                              *
                            • Initial step bound factor: 100
                            • *
                            • Cost relative tolerance: 1e-10
                            • *
                            • Parameters relative tolerance: 1e-10
                            • *
                            • Orthogonality tolerance: 1e-10
                            • *
                            • QR ranking threshold: {@link Precision#SAFE_MIN}
                            • *
                            * * @param checker Convergence checker. */ public LevenbergMarquardtOptimizer(ConvergenceChecker checker) { this(100, checker, 1e-10, 1e-10, 1e-10, Precision.SAFE_MIN); } /** * Constructor that allows the specification of a custom convergence * checker, in addition to the standard ones. * * @param initialStepBoundFactor Positive input variable used in * determining the initial step bound. This bound is set to the * product of initialStepBoundFactor and the euclidean norm of * {@code diag * x} if non-zero, or else to {@code initialStepBoundFactor} * itself. In most cases factor should lie in the interval * {@code (0.1, 100.0)}. {@code 100} is a generally recommended value. * @param checker Convergence checker. * @param costRelativeTolerance Desired relative error in the sum of * squares. * @param parRelativeTolerance Desired relative error in the approximate * solution parameters. * @param orthoTolerance Desired max cosine on the orthogonality between * the function vector and the columns of the Jacobian. * @param threshold Desired threshold for QR ranking. If the squared norm * of a column vector is smaller or equal to this threshold during QR * decomposition, it is considered to be a zero vector and hence the rank * of the matrix is reduced. */ public LevenbergMarquardtOptimizer(double initialStepBoundFactor, ConvergenceChecker checker, double costRelativeTolerance, double parRelativeTolerance, double orthoTolerance, double threshold) { super(checker); this.initialStepBoundFactor = initialStepBoundFactor; this.costRelativeTolerance = costRelativeTolerance; this.parRelativeTolerance = parRelativeTolerance; this.orthoTolerance = orthoTolerance; this.qrRankingThreshold = threshold; } /** * Build an optimizer for least squares problems with default values * for some of the tuning parameters (see the {@link * #LevenbergMarquardtOptimizer(double,double,double,double,double) * other contructor}. * The default values for the algorithm settings are: *
                              *
                            • Initial step bound factor}: 100
                            • *
                            • QR ranking threshold}: {@link Precision#SAFE_MIN}
                            • *
                            * * @param costRelativeTolerance Desired relative error in the sum of * squares. * @param parRelativeTolerance Desired relative error in the approximate * solution parameters. * @param orthoTolerance Desired max cosine on the orthogonality between * the function vector and the columns of the Jacobian. */ public LevenbergMarquardtOptimizer(double costRelativeTolerance, double parRelativeTolerance, double orthoTolerance) { this(100, costRelativeTolerance, parRelativeTolerance, orthoTolerance, Precision.SAFE_MIN); } /** * The arguments control the behaviour of the default convergence checking * procedure. * Additional criteria can defined through the setting of a {@link * ConvergenceChecker}. * * @param initialStepBoundFactor Positive input variable used in * determining the initial step bound. This bound is set to the * product of initialStepBoundFactor and the euclidean norm of * {@code diag * x} if non-zero, or else to {@code initialStepBoundFactor} * itself. In most cases factor should lie in the interval * {@code (0.1, 100.0)}. {@code 100} is a generally recommended value. * @param costRelativeTolerance Desired relative error in the sum of * squares. * @param parRelativeTolerance Desired relative error in the approximate * solution parameters. * @param orthoTolerance Desired max cosine on the orthogonality between * the function vector and the columns of the Jacobian. * @param threshold Desired threshold for QR ranking. If the squared norm * of a column vector is smaller or equal to this threshold during QR * decomposition, it is considered to be a zero vector and hence the rank * of the matrix is reduced. */ public LevenbergMarquardtOptimizer(double initialStepBoundFactor, double costRelativeTolerance, double parRelativeTolerance, double orthoTolerance, double threshold) { super(null); // No custom convergence criterion. this.initialStepBoundFactor = initialStepBoundFactor; this.costRelativeTolerance = costRelativeTolerance; this.parRelativeTolerance = parRelativeTolerance; this.orthoTolerance = orthoTolerance; this.qrRankingThreshold = threshold; } /** {@inheritDoc} */ @Override protected PointVectorValuePair doOptimize() { final int nR = getTarget().length; // Number of observed data. final double[] currentPoint = getStartPoint(); final int nC = currentPoint.length; // Number of parameters. // arrays shared with the other private methods solvedCols = FastMath.min(nR, nC); diagR = new double[nC]; jacNorm = new double[nC]; beta = new double[nC]; permutation = new int[nC]; lmDir = new double[nC]; // local point double delta = 0; double xNorm = 0; double[] diag = new double[nC]; double[] oldX = new double[nC]; double[] oldRes = new double[nR]; double[] oldObj = new double[nR]; double[] qtf = new double[nR]; double[] work1 = new double[nC]; double[] work2 = new double[nC]; double[] work3 = new double[nC]; final RealMatrix weightMatrixSqrt = getWeightSquareRoot(); // Evaluate the function at the starting point and calculate its norm. double[] currentObjective = computeObjectiveValue(currentPoint); double[] currentResiduals = computeResiduals(currentObjective); PointVectorValuePair current = new PointVectorValuePair(currentPoint, currentObjective); double currentCost = computeCost(currentResiduals); // Outer loop. lmPar = 0; boolean firstIteration = true; int iter = 0; final ConvergenceChecker checker = getConvergenceChecker(); while (true) { ++iter; final PointVectorValuePair previous = current; // QR decomposition of the jacobian matrix qrDecomposition(computeWeightedJacobian(currentPoint)); weightedResidual = weightMatrixSqrt.operate(currentResiduals); for (int i = 0; i < nR; i++) { qtf[i] = weightedResidual[i]; } // compute Qt.res qTy(qtf); // now we don't need Q anymore, // so let jacobian contain the R matrix with its diagonal elements for (int k = 0; k < solvedCols; ++k) { int pk = permutation[k]; weightedJacobian[k][pk] = diagR[pk]; } if (firstIteration) { // scale the point according to the norms of the columns // of the initial jacobian xNorm = 0; for (int k = 0; k < nC; ++k) { double dk = jacNorm[k]; if (dk == 0) { dk = 1.0; } double xk = dk * currentPoint[k]; xNorm += xk * xk; diag[k] = dk; } xNorm = FastMath.sqrt(xNorm); // initialize the step bound delta delta = (xNorm == 0) ? initialStepBoundFactor : (initialStepBoundFactor * xNorm); } // check orthogonality between function vector and jacobian columns double maxCosine = 0; if (currentCost != 0) { for (int j = 0; j < solvedCols; ++j) { int pj = permutation[j]; double s = jacNorm[pj]; if (s != 0) { double sum = 0; for (int i = 0; i <= j; ++i) { sum += weightedJacobian[i][pj] * qtf[i]; } maxCosine = FastMath.max(maxCosine, FastMath.abs(sum) / (s * currentCost)); } } } if (maxCosine <= orthoTolerance) { // Convergence has been reached. setCost(currentCost); // Update (deprecated) "point" field. point = current.getPoint(); return current; } // rescale if necessary for (int j = 0; j < nC; ++j) { diag[j] = FastMath.max(diag[j], jacNorm[j]); } // Inner loop. for (double ratio = 0; ratio < 1.0e-4;) { // save the state for (int j = 0; j < solvedCols; ++j) { int pj = permutation[j]; oldX[pj] = currentPoint[pj]; } final double previousCost = currentCost; double[] tmpVec = weightedResidual; weightedResidual = oldRes; oldRes = tmpVec; tmpVec = currentObjective; currentObjective = oldObj; oldObj = tmpVec; // determine the Levenberg-Marquardt parameter determineLMParameter(qtf, delta, diag, work1, work2, work3); // compute the new point and the norm of the evolution direction double lmNorm = 0; for (int j = 0; j < solvedCols; ++j) { int pj = permutation[j]; lmDir[pj] = -lmDir[pj]; currentPoint[pj] = oldX[pj] + lmDir[pj]; double s = diag[pj] * lmDir[pj]; lmNorm += s * s; } lmNorm = FastMath.sqrt(lmNorm); // on the first iteration, adjust the initial step bound. if (firstIteration) { delta = FastMath.min(delta, lmNorm); } // Evaluate the function at x + p and calculate its norm. currentObjective = computeObjectiveValue(currentPoint); currentResiduals = computeResiduals(currentObjective); current = new PointVectorValuePair(currentPoint, currentObjective); currentCost = computeCost(currentResiduals); // compute the scaled actual reduction double actRed = -1.0; if (0.1 * currentCost < previousCost) { double r = currentCost / previousCost; actRed = 1.0 - r * r; } // compute the scaled predicted reduction // and the scaled directional derivative for (int j = 0; j < solvedCols; ++j) { int pj = permutation[j]; double dirJ = lmDir[pj]; work1[j] = 0; for (int i = 0; i <= j; ++i) { work1[i] += weightedJacobian[i][pj] * dirJ; } } double coeff1 = 0; for (int j = 0; j < solvedCols; ++j) { coeff1 += work1[j] * work1[j]; } double pc2 = previousCost * previousCost; coeff1 = coeff1 / pc2; double coeff2 = lmPar * lmNorm * lmNorm / pc2; double preRed = coeff1 + 2 * coeff2; double dirDer = -(coeff1 + coeff2); // ratio of the actual to the predicted reduction ratio = (preRed == 0) ? 0 : (actRed / preRed); // update the step bound if (ratio <= 0.25) { double tmp = (actRed < 0) ? (0.5 * dirDer / (dirDer + 0.5 * actRed)) : 0.5; if ((0.1 * currentCost >= previousCost) || (tmp < 0.1)) { tmp = 0.1; } delta = tmp * FastMath.min(delta, 10.0 * lmNorm); lmPar /= tmp; } else if ((lmPar == 0) || (ratio >= 0.75)) { delta = 2 * lmNorm; lmPar *= 0.5; } // test for successful iteration. if (ratio >= 1.0e-4) { // successful iteration, update the norm firstIteration = false; xNorm = 0; for (int k = 0; k < nC; ++k) { double xK = diag[k] * currentPoint[k]; xNorm += xK * xK; } xNorm = FastMath.sqrt(xNorm); // tests for convergence. if (checker != null && checker.converged(iter, previous, current)) { setCost(currentCost); // Update (deprecated) "point" field. point = current.getPoint(); return current; } } else { // failed iteration, reset the previous values currentCost = previousCost; for (int j = 0; j < solvedCols; ++j) { int pj = permutation[j]; currentPoint[pj] = oldX[pj]; } tmpVec = weightedResidual; weightedResidual = oldRes; oldRes = tmpVec; tmpVec = currentObjective; currentObjective = oldObj; oldObj = tmpVec; // Reset "current" to previous values. current = new PointVectorValuePair(currentPoint, currentObjective); } // Default convergence criteria. if ((FastMath.abs(actRed) <= costRelativeTolerance && preRed <= costRelativeTolerance && ratio <= 2.0) || delta <= parRelativeTolerance * xNorm) { setCost(currentCost); // Update (deprecated) "point" field. point = current.getPoint(); return current; } // tests for termination and stringent tolerances // (2.2204e-16 is the machine epsilon for IEEE754) if ((FastMath.abs(actRed) <= 2.2204e-16) && (preRed <= 2.2204e-16) && (ratio <= 2.0)) { throw new ConvergenceException(LocalizedFormats.TOO_SMALL_COST_RELATIVE_TOLERANCE, costRelativeTolerance); } else if (delta <= 2.2204e-16 * xNorm) { throw new ConvergenceException(LocalizedFormats.TOO_SMALL_PARAMETERS_RELATIVE_TOLERANCE, parRelativeTolerance); } else if (maxCosine <= 2.2204e-16) { throw new ConvergenceException(LocalizedFormats.TOO_SMALL_ORTHOGONALITY_TOLERANCE, orthoTolerance); } } } } /** * Determine the Levenberg-Marquardt parameter. *

                            This implementation is a translation in Java of the MINPACK * lmpar * routine.

                            *

                            This method sets the lmPar and lmDir attributes.

                            *

                            The authors of the original fortran function are:

                            *
                              *
                            • Argonne National Laboratory. MINPACK project. March 1980
                            • *
                            • Burton S. Garbow
                            • *
                            • Kenneth E. Hillstrom
                            • *
                            • Jorge J. More
                            • *
                            *

                            Luc Maisonobe did the Java translation.

                            * * @param qy array containing qTy * @param delta upper bound on the euclidean norm of diagR * lmDir * @param diag diagonal matrix * @param work1 work array * @param work2 work array * @param work3 work array */ private void determineLMParameter(double[] qy, double delta, double[] diag, double[] work1, double[] work2, double[] work3) { final int nC = weightedJacobian[0].length; // compute and store in x the gauss-newton direction, if the // jacobian is rank-deficient, obtain a least squares solution for (int j = 0; j < rank; ++j) { lmDir[permutation[j]] = qy[j]; } for (int j = rank; j < nC; ++j) { lmDir[permutation[j]] = 0; } for (int k = rank - 1; k >= 0; --k) { int pk = permutation[k]; double ypk = lmDir[pk] / diagR[pk]; for (int i = 0; i < k; ++i) { lmDir[permutation[i]] -= ypk * weightedJacobian[i][pk]; } lmDir[pk] = ypk; } // evaluate the function at the origin, and test // for acceptance of the Gauss-Newton direction double dxNorm = 0; for (int j = 0; j < solvedCols; ++j) { int pj = permutation[j]; double s = diag[pj] * lmDir[pj]; work1[pj] = s; dxNorm += s * s; } dxNorm = FastMath.sqrt(dxNorm); double fp = dxNorm - delta; if (fp <= 0.1 * delta) { lmPar = 0; return; } // if the jacobian is not rank deficient, the Newton step provides // a lower bound, parl, for the zero of the function, // otherwise set this bound to zero double sum2; double parl = 0; if (rank == solvedCols) { for (int j = 0; j < solvedCols; ++j) { int pj = permutation[j]; work1[pj] *= diag[pj] / dxNorm; } sum2 = 0; for (int j = 0; j < solvedCols; ++j) { int pj = permutation[j]; double sum = 0; for (int i = 0; i < j; ++i) { sum += weightedJacobian[i][pj] * work1[permutation[i]]; } double s = (work1[pj] - sum) / diagR[pj]; work1[pj] = s; sum2 += s * s; } parl = fp / (delta * sum2); } // calculate an upper bound, paru, for the zero of the function sum2 = 0; for (int j = 0; j < solvedCols; ++j) { int pj = permutation[j]; double sum = 0; for (int i = 0; i <= j; ++i) { sum += weightedJacobian[i][pj] * qy[i]; } sum /= diag[pj]; sum2 += sum * sum; } double gNorm = FastMath.sqrt(sum2); double paru = gNorm / delta; if (paru == 0) { // 2.2251e-308 is the smallest positive real for IEE754 paru = 2.2251e-308 / FastMath.min(delta, 0.1); } // if the input par lies outside of the interval (parl,paru), // set par to the closer endpoint lmPar = FastMath.min(paru, FastMath.max(lmPar, parl)); if (lmPar == 0) { lmPar = gNorm / dxNorm; } for (int countdown = 10; countdown >= 0; --countdown) { // evaluate the function at the current value of lmPar if (lmPar == 0) { lmPar = FastMath.max(2.2251e-308, 0.001 * paru); } double sPar = FastMath.sqrt(lmPar); for (int j = 0; j < solvedCols; ++j) { int pj = permutation[j]; work1[pj] = sPar * diag[pj]; } determineLMDirection(qy, work1, work2, work3); dxNorm = 0; for (int j = 0; j < solvedCols; ++j) { int pj = permutation[j]; double s = diag[pj] * lmDir[pj]; work3[pj] = s; dxNorm += s * s; } dxNorm = FastMath.sqrt(dxNorm); double previousFP = fp; fp = dxNorm - delta; // if the function is small enough, accept the current value // of lmPar, also test for the exceptional cases where parl is zero if ((FastMath.abs(fp) <= 0.1 * delta) || ((parl == 0) && (fp <= previousFP) && (previousFP < 0))) { return; } // compute the Newton correction for (int j = 0; j < solvedCols; ++j) { int pj = permutation[j]; work1[pj] = work3[pj] * diag[pj] / dxNorm; } for (int j = 0; j < solvedCols; ++j) { int pj = permutation[j]; work1[pj] /= work2[j]; double tmp = work1[pj]; for (int i = j + 1; i < solvedCols; ++i) { work1[permutation[i]] -= weightedJacobian[i][pj] * tmp; } } sum2 = 0; for (int j = 0; j < solvedCols; ++j) { double s = work1[permutation[j]]; sum2 += s * s; } double correction = fp / (delta * sum2); // depending on the sign of the function, update parl or paru. if (fp > 0) { parl = FastMath.max(parl, lmPar); } else if (fp < 0) { paru = FastMath.min(paru, lmPar); } // compute an improved estimate for lmPar lmPar = FastMath.max(parl, lmPar + correction); } } /** * Solve a*x = b and d*x = 0 in the least squares sense. *

                            This implementation is a translation in Java of the MINPACK * qrsolv * routine.

                            *

                            This method sets the lmDir and lmDiag attributes.

                            *

                            The authors of the original fortran function are:

                            *
                              *
                            • Argonne National Laboratory. MINPACK project. March 1980
                            • *
                            • Burton S. Garbow
                            • *
                            • Kenneth E. Hillstrom
                            • *
                            • Jorge J. More
                            • *
                            *

                            Luc Maisonobe did the Java translation.

                            * * @param qy array containing qTy * @param diag diagonal matrix * @param lmDiag diagonal elements associated with lmDir * @param work work array */ private void determineLMDirection(double[] qy, double[] diag, double[] lmDiag, double[] work) { // copy R and Qty to preserve input and initialize s // in particular, save the diagonal elements of R in lmDir for (int j = 0; j < solvedCols; ++j) { int pj = permutation[j]; for (int i = j + 1; i < solvedCols; ++i) { weightedJacobian[i][pj] = weightedJacobian[j][permutation[i]]; } lmDir[j] = diagR[pj]; work[j] = qy[j]; } // eliminate the diagonal matrix d using a Givens rotation for (int j = 0; j < solvedCols; ++j) { // prepare the row of d to be eliminated, locating the // diagonal element using p from the Q.R. factorization int pj = permutation[j]; double dpj = diag[pj]; if (dpj != 0) { Arrays.fill(lmDiag, j + 1, lmDiag.length, 0); } lmDiag[j] = dpj; // the transformations to eliminate the row of d // modify only a single element of Qty // beyond the first n, which is initially zero. double qtbpj = 0; for (int k = j; k < solvedCols; ++k) { int pk = permutation[k]; // determine a Givens rotation which eliminates the // appropriate element in the current row of d if (lmDiag[k] != 0) { final double sin; final double cos; double rkk = weightedJacobian[k][pk]; if (FastMath.abs(rkk) < FastMath.abs(lmDiag[k])) { final double cotan = rkk / lmDiag[k]; sin = 1.0 / FastMath.sqrt(1.0 + cotan * cotan); cos = sin * cotan; } else { final double tan = lmDiag[k] / rkk; cos = 1.0 / FastMath.sqrt(1.0 + tan * tan); sin = cos * tan; } // compute the modified diagonal element of R and // the modified element of (Qty,0) weightedJacobian[k][pk] = cos * rkk + sin * lmDiag[k]; final double temp = cos * work[k] + sin * qtbpj; qtbpj = -sin * work[k] + cos * qtbpj; work[k] = temp; // accumulate the tranformation in the row of s for (int i = k + 1; i < solvedCols; ++i) { double rik = weightedJacobian[i][pk]; final double temp2 = cos * rik + sin * lmDiag[i]; lmDiag[i] = -sin * rik + cos * lmDiag[i]; weightedJacobian[i][pk] = temp2; } } } // store the diagonal element of s and restore // the corresponding diagonal element of R lmDiag[j] = weightedJacobian[j][permutation[j]]; weightedJacobian[j][permutation[j]] = lmDir[j]; } // solve the triangular system for z, if the system is // singular, then obtain a least squares solution int nSing = solvedCols; for (int j = 0; j < solvedCols; ++j) { if ((lmDiag[j] == 0) && (nSing == solvedCols)) { nSing = j; } if (nSing < solvedCols) { work[j] = 0; } } if (nSing > 0) { for (int j = nSing - 1; j >= 0; --j) { int pj = permutation[j]; double sum = 0; for (int i = j + 1; i < nSing; ++i) { sum += weightedJacobian[i][pj] * work[i]; } work[j] = (work[j] - sum) / lmDiag[j]; } } // permute the components of z back to components of lmDir for (int j = 0; j < lmDir.length; ++j) { lmDir[permutation[j]] = work[j]; } } /** * Decompose a matrix A as A.P = Q.R using Householder transforms. *

                            As suggested in the P. Lascaux and R. Theodor book * Analyse numérique matricielle appliquée à * l'art de l'ingénieur (Masson, 1986), instead of representing * the Householder transforms with uk unit vectors such that: *

                                 * Hk = I - 2uk.ukt
                                 * 
                            * we use k non-unit vectors such that: *
                                 * Hk = I - betakvk.vkt
                                 * 
                            * where vk = ak - alphak ek. * The betak coefficients are provided upon exit as recomputing * them from the vk vectors would be costly.

                            *

                            This decomposition handles rank deficient cases since the tranformations * are performed in non-increasing columns norms order thanks to columns * pivoting. The diagonal elements of the R matrix are therefore also in * non-increasing absolute values order.

                            * * @param jacobian Weighted Jacobian matrix at the current point. * @exception ConvergenceException if the decomposition cannot be performed */ private void qrDecomposition(RealMatrix jacobian) throws ConvergenceException { // Code in this class assumes that the weighted Jacobian is -(W^(1/2) J), // hence the multiplication by -1. weightedJacobian = jacobian.scalarMultiply(-1).getData(); final int nR = weightedJacobian.length; final int nC = weightedJacobian[0].length; // initializations for (int k = 0; k < nC; ++k) { permutation[k] = k; double norm2 = 0; for (int i = 0; i < nR; ++i) { double akk = weightedJacobian[i][k]; norm2 += akk * akk; } jacNorm[k] = FastMath.sqrt(norm2); } // transform the matrix column after column for (int k = 0; k < nC; ++k) { // select the column with the greatest norm on active components int nextColumn = -1; double ak2 = Double.NEGATIVE_INFINITY; for (int i = k; i < nC; ++i) { double norm2 = 0; for (int j = k; j < nR; ++j) { double aki = weightedJacobian[j][permutation[i]]; norm2 += aki * aki; } if (Double.isInfinite(norm2) || Double.isNaN(norm2)) { throw new ConvergenceException(LocalizedFormats.UNABLE_TO_PERFORM_QR_DECOMPOSITION_ON_JACOBIAN, nR, nC); } if (norm2 > ak2) { nextColumn = i; ak2 = norm2; } } if (ak2 <= qrRankingThreshold) { rank = k; return; } int pk = permutation[nextColumn]; permutation[nextColumn] = permutation[k]; permutation[k] = pk; // choose alpha such that Hk.u = alpha ek double akk = weightedJacobian[k][pk]; double alpha = (akk > 0) ? -FastMath.sqrt(ak2) : FastMath.sqrt(ak2); double betak = 1.0 / (ak2 - akk * alpha); beta[pk] = betak; // transform the current column diagR[pk] = alpha; weightedJacobian[k][pk] -= alpha; // transform the remaining columns for (int dk = nC - 1 - k; dk > 0; --dk) { double gamma = 0; for (int j = k; j < nR; ++j) { gamma += weightedJacobian[j][pk] * weightedJacobian[j][permutation[k + dk]]; } gamma *= betak; for (int j = k; j < nR; ++j) { weightedJacobian[j][permutation[k + dk]] -= gamma * weightedJacobian[j][pk]; } } } rank = solvedCols; } /** * Compute the product Qt.y for some Q.R. decomposition. * * @param y vector to multiply (will be overwritten with the result) */ private void qTy(double[] y) { final int nR = weightedJacobian.length; final int nC = weightedJacobian[0].length; for (int k = 0; k < nC; ++k) { int pk = permutation[k]; double gamma = 0; for (int i = k; i < nR; ++i) { gamma += weightedJacobian[i][pk] * y[i]; } gamma *= beta[pk]; for (int i = k; i < nR; ++i) { y[i] -= gamma * weightedJacobian[i][pk]; } } } } ././@LongLink100644 0 0 173 12126630646 10262 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/optimization/general/NonLinearConjugateGradientOptimizer.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/optimization/general/NonLinearConjugate100644 1750 1750 30040 12126627713 32353 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.optimization.general; import org.apache.commons.math3.exception.MathIllegalStateException; import org.apache.commons.math3.analysis.UnivariateFunction; import org.apache.commons.math3.analysis.solvers.BrentSolver; import org.apache.commons.math3.analysis.solvers.UnivariateSolver; import org.apache.commons.math3.exception.util.LocalizedFormats; import org.apache.commons.math3.optimization.GoalType; import org.apache.commons.math3.optimization.PointValuePair; import org.apache.commons.math3.optimization.SimpleValueChecker; import org.apache.commons.math3.optimization.ConvergenceChecker; import org.apache.commons.math3.util.FastMath; /** * Non-linear conjugate gradient optimizer. *

                            * This class supports both the Fletcher-Reeves and the Polak-Ribière * update formulas for the conjugate search directions. It also supports * optional preconditioning. *

                            * * @version $Id: NonLinearConjugateGradientOptimizer.java 1462503 2013-03-29 15:48:27Z luc $ * @deprecated As of 3.1 (to be removed in 4.0). * @since 2.0 * */ @Deprecated public class NonLinearConjugateGradientOptimizer extends AbstractScalarDifferentiableOptimizer { /** Update formula for the beta parameter. */ private final ConjugateGradientFormula updateFormula; /** Preconditioner (may be null). */ private final Preconditioner preconditioner; /** solver to use in the line search (may be null). */ private final UnivariateSolver solver; /** Initial step used to bracket the optimum in line search. */ private double initialStep; /** Current point. */ private double[] point; /** * Constructor with default {@link SimpleValueChecker checker}, * {@link BrentSolver line search solver} and * {@link IdentityPreconditioner preconditioner}. * * @param updateFormula formula to use for updating the β parameter, * must be one of {@link ConjugateGradientFormula#FLETCHER_REEVES} or {@link * ConjugateGradientFormula#POLAK_RIBIERE}. * @deprecated See {@link SimpleValueChecker#SimpleValueChecker()} */ @Deprecated public NonLinearConjugateGradientOptimizer(final ConjugateGradientFormula updateFormula) { this(updateFormula, new SimpleValueChecker()); } /** * Constructor with default {@link BrentSolver line search solver} and * {@link IdentityPreconditioner preconditioner}. * * @param updateFormula formula to use for updating the β parameter, * must be one of {@link ConjugateGradientFormula#FLETCHER_REEVES} or {@link * ConjugateGradientFormula#POLAK_RIBIERE}. * @param checker Convergence checker. */ public NonLinearConjugateGradientOptimizer(final ConjugateGradientFormula updateFormula, ConvergenceChecker checker) { this(updateFormula, checker, new BrentSolver(), new IdentityPreconditioner()); } /** * Constructor with default {@link IdentityPreconditioner preconditioner}. * * @param updateFormula formula to use for updating the β parameter, * must be one of {@link ConjugateGradientFormula#FLETCHER_REEVES} or {@link * ConjugateGradientFormula#POLAK_RIBIERE}. * @param checker Convergence checker. * @param lineSearchSolver Solver to use during line search. */ public NonLinearConjugateGradientOptimizer(final ConjugateGradientFormula updateFormula, ConvergenceChecker checker, final UnivariateSolver lineSearchSolver) { this(updateFormula, checker, lineSearchSolver, new IdentityPreconditioner()); } /** * @param updateFormula formula to use for updating the β parameter, * must be one of {@link ConjugateGradientFormula#FLETCHER_REEVES} or {@link * ConjugateGradientFormula#POLAK_RIBIERE}. * @param checker Convergence checker. * @param lineSearchSolver Solver to use during line search. * @param preconditioner Preconditioner. */ public NonLinearConjugateGradientOptimizer(final ConjugateGradientFormula updateFormula, ConvergenceChecker checker, final UnivariateSolver lineSearchSolver, final Preconditioner preconditioner) { super(checker); this.updateFormula = updateFormula; solver = lineSearchSolver; this.preconditioner = preconditioner; initialStep = 1.0; } /** * Set the initial step used to bracket the optimum in line search. *

                            * The initial step is a factor with respect to the search direction, * which itself is roughly related to the gradient of the function *

                            * @param initialStep initial step used to bracket the optimum in line search, * if a non-positive value is used, the initial step is reset to its * default value of 1.0 */ public void setInitialStep(final double initialStep) { if (initialStep <= 0) { this.initialStep = 1.0; } else { this.initialStep = initialStep; } } /** {@inheritDoc} */ @Override protected PointValuePair doOptimize() { final ConvergenceChecker checker = getConvergenceChecker(); point = getStartPoint(); final GoalType goal = getGoalType(); final int n = point.length; double[] r = computeObjectiveGradient(point); if (goal == GoalType.MINIMIZE) { for (int i = 0; i < n; ++i) { r[i] = -r[i]; } } // Initial search direction. double[] steepestDescent = preconditioner.precondition(point, r); double[] searchDirection = steepestDescent.clone(); double delta = 0; for (int i = 0; i < n; ++i) { delta += r[i] * searchDirection[i]; } PointValuePair current = null; int iter = 0; int maxEval = getMaxEvaluations(); while (true) { ++iter; final double objective = computeObjectiveValue(point); PointValuePair previous = current; current = new PointValuePair(point, objective); if (previous != null && checker.converged(iter, previous, current)) { // We have found an optimum. return current; } // Find the optimal step in the search direction. final UnivariateFunction lsf = new LineSearchFunction(searchDirection); final double uB = findUpperBound(lsf, 0, initialStep); // XXX Last parameters is set to a value close to zero in order to // work around the divergence problem in the "testCircleFitting" // unit test (see MATH-439). final double step = solver.solve(maxEval, lsf, 0, uB, 1e-15); maxEval -= solver.getEvaluations(); // Subtract used up evaluations. // Validate new point. for (int i = 0; i < point.length; ++i) { point[i] += step * searchDirection[i]; } r = computeObjectiveGradient(point); if (goal == GoalType.MINIMIZE) { for (int i = 0; i < n; ++i) { r[i] = -r[i]; } } // Compute beta. final double deltaOld = delta; final double[] newSteepestDescent = preconditioner.precondition(point, r); delta = 0; for (int i = 0; i < n; ++i) { delta += r[i] * newSteepestDescent[i]; } final double beta; if (updateFormula == ConjugateGradientFormula.FLETCHER_REEVES) { beta = delta / deltaOld; } else { double deltaMid = 0; for (int i = 0; i < r.length; ++i) { deltaMid += r[i] * steepestDescent[i]; } beta = (delta - deltaMid) / deltaOld; } steepestDescent = newSteepestDescent; // Compute conjugate search direction. if (iter % n == 0 || beta < 0) { // Break conjugation: reset search direction. searchDirection = steepestDescent.clone(); } else { // Compute new conjugate search direction. for (int i = 0; i < n; ++i) { searchDirection[i] = steepestDescent[i] + beta * searchDirection[i]; } } } } /** * Find the upper bound b ensuring bracketing of a root between a and b. * * @param f function whose root must be bracketed. * @param a lower bound of the interval. * @param h initial step to try. * @return b such that f(a) and f(b) have opposite signs. * @throws MathIllegalStateException if no bracket can be found. */ private double findUpperBound(final UnivariateFunction f, final double a, final double h) { final double yA = f.value(a); double yB = yA; for (double step = h; step < Double.MAX_VALUE; step *= FastMath.max(2, yA / yB)) { final double b = a + step; yB = f.value(b); if (yA * yB <= 0) { return b; } } throw new MathIllegalStateException(LocalizedFormats.UNABLE_TO_BRACKET_OPTIMUM_IN_LINE_SEARCH); } /** Default identity preconditioner. */ public static class IdentityPreconditioner implements Preconditioner { /** {@inheritDoc} */ public double[] precondition(double[] variables, double[] r) { return r.clone(); } } /** Internal class for line search. *

                            * The function represented by this class is the dot product of * the objective function gradient and the search direction. Its * value is zero when the gradient is orthogonal to the search * direction, i.e. when the objective function value is a local * extremum along the search direction. *

                            */ private class LineSearchFunction implements UnivariateFunction { /** Search direction. */ private final double[] searchDirection; /** Simple constructor. * @param searchDirection search direction */ public LineSearchFunction(final double[] searchDirection) { this.searchDirection = searchDirection; } /** {@inheritDoc} */ public double value(double x) { // current point in the search direction final double[] shiftedPoint = point.clone(); for (int i = 0; i < shiftedPoint.length; ++i) { shiftedPoint[i] += x * searchDirection[i]; } // gradient of the objective function final double[] gradient = computeObjectiveGradient(shiftedPoint); // dot product with the search direction double dotProduct = 0; for (int i = 0; i < gradient.length; ++i) { dotProduct += gradient[i] * searchDirection[i]; } return dotProduct; } } } ././@LongLink100644 0 0 175 12126630646 10264 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/optimization/general/AbstractScalarDifferentiableOptimizer.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/optimization/general/AbstractScalarDiff100644 1750 1750 11550 12126627713 32315 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.optimization.general; import org.apache.commons.math3.analysis.DifferentiableMultivariateFunction; import org.apache.commons.math3.analysis.MultivariateVectorFunction; import org.apache.commons.math3.analysis.FunctionUtils; import org.apache.commons.math3.analysis.differentiation.MultivariateDifferentiableFunction; import org.apache.commons.math3.optimization.DifferentiableMultivariateOptimizer; import org.apache.commons.math3.optimization.GoalType; import org.apache.commons.math3.optimization.ConvergenceChecker; import org.apache.commons.math3.optimization.PointValuePair; import org.apache.commons.math3.optimization.direct.BaseAbstractMultivariateOptimizer; /** * Base class for implementing optimizers for multivariate scalar * differentiable functions. * It contains boiler-plate code for dealing with gradient evaluation. * * @version $Id: AbstractScalarDifferentiableOptimizer.java 1422230 2012-12-15 12:11:13Z erans $ * @deprecated As of 3.1 (to be removed in 4.0). * @since 2.0 */ @Deprecated public abstract class AbstractScalarDifferentiableOptimizer extends BaseAbstractMultivariateOptimizer implements DifferentiableMultivariateOptimizer { /** * Objective function gradient. */ private MultivariateVectorFunction gradient; /** * Simple constructor with default settings. * The convergence check is set to a * {@link org.apache.commons.math3.optimization.SimpleValueChecker * SimpleValueChecker}. * @deprecated See {@link org.apache.commons.math3.optimization.SimpleValueChecker#SimpleValueChecker()} */ @Deprecated protected AbstractScalarDifferentiableOptimizer() {} /** * @param checker Convergence checker. */ protected AbstractScalarDifferentiableOptimizer(ConvergenceChecker checker) { super(checker); } /** * Compute the gradient vector. * * @param evaluationPoint Point at which the gradient must be evaluated. * @return the gradient at the specified point. * @throws org.apache.commons.math3.exception.TooManyEvaluationsException * if the allowed number of evaluations is exceeded. */ protected double[] computeObjectiveGradient(final double[] evaluationPoint) { return gradient.value(evaluationPoint); } /** {@inheritDoc} */ @Override protected PointValuePair optimizeInternal(int maxEval, final DifferentiableMultivariateFunction f, final GoalType goalType, final double[] startPoint) { // Store optimization problem characteristics. gradient = f.gradient(); return super.optimizeInternal(maxEval, f, goalType, startPoint); } /** * Optimize an objective function. * * @param f Objective function. * @param goalType Type of optimization goal: either * {@link GoalType#MAXIMIZE} or {@link GoalType#MINIMIZE}. * @param startPoint Start point for optimization. * @param maxEval Maximum number of function evaluations. * @return the point/value pair giving the optimal value for objective * function. * @throws org.apache.commons.math3.exception.DimensionMismatchException * if the start point dimension is wrong. * @throws org.apache.commons.math3.exception.TooManyEvaluationsException * if the maximal number of evaluations is exceeded. * @throws org.apache.commons.math3.exception.NullArgumentException if * any argument is {@code null}. */ public PointValuePair optimize(final int maxEval, final MultivariateDifferentiableFunction f, final GoalType goalType, final double[] startPoint) { return optimizeInternal(maxEval, FunctionUtils.toDifferentiableMultivariateFunction(f), goalType, startPoint); } } ././@LongLink100644 0 0 167 12126630646 10265 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/optimization/general/AbstractDifferentiableOptimizer.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/optimization/general/AbstractDifferenti100644 1750 1750 7252 12126627713 32362 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.optimization.general; import org.apache.commons.math3.analysis.MultivariateVectorFunction; import org.apache.commons.math3.analysis.differentiation.GradientFunction; import org.apache.commons.math3.analysis.differentiation.MultivariateDifferentiableFunction; import org.apache.commons.math3.optimization.ConvergenceChecker; import org.apache.commons.math3.optimization.GoalType; import org.apache.commons.math3.optimization.OptimizationData; import org.apache.commons.math3.optimization.InitialGuess; import org.apache.commons.math3.optimization.PointValuePair; import org.apache.commons.math3.optimization.direct.BaseAbstractMultivariateOptimizer; /** * Base class for implementing optimizers for multivariate scalar * differentiable functions. * It contains boiler-plate code for dealing with gradient evaluation. * * @version $Id: AbstractDifferentiableOptimizer.java 1422230 2012-12-15 12:11:13Z erans $ * @deprecated As of 3.1 (to be removed in 4.0). * @since 3.1 */ @Deprecated public abstract class AbstractDifferentiableOptimizer extends BaseAbstractMultivariateOptimizer { /** * Objective function gradient. */ private MultivariateVectorFunction gradient; /** * @param checker Convergence checker. */ protected AbstractDifferentiableOptimizer(ConvergenceChecker checker) { super(checker); } /** * Compute the gradient vector. * * @param evaluationPoint Point at which the gradient must be evaluated. * @return the gradient at the specified point. */ protected double[] computeObjectiveGradient(final double[] evaluationPoint) { return gradient.value(evaluationPoint); } /** * {@inheritDoc} * * @deprecated In 3.1. Please use * {@link #optimizeInternal(int,MultivariateDifferentiableFunction,GoalType,OptimizationData[])} * instead. */ @Override@Deprecated protected PointValuePair optimizeInternal(final int maxEval, final MultivariateDifferentiableFunction f, final GoalType goalType, final double[] startPoint) { return optimizeInternal(maxEval, f, goalType, new InitialGuess(startPoint)); } /** {@inheritDoc} */ @Override protected PointValuePair optimizeInternal(final int maxEval, final MultivariateDifferentiableFunction f, final GoalType goalType, final OptimizationData... optData) { // Store optimization problem characteristics. gradient = new GradientFunction(f); // Perform optimization. return super.optimizeInternal(maxEval, f, goalType, optData); } } ././@LongLink100644 0 0 154 12126630646 10261 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/optimization/general/GaussNewtonOptimizer.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/optimization/general/GaussNewtonOptimiz100644 1750 1750 17167 12126627713 32476 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.optimization.general; import org.apache.commons.math3.exception.ConvergenceException; import org.apache.commons.math3.exception.NullArgumentException; import org.apache.commons.math3.exception.MathInternalError; import org.apache.commons.math3.exception.util.LocalizedFormats; import org.apache.commons.math3.linear.ArrayRealVector; import org.apache.commons.math3.linear.BlockRealMatrix; import org.apache.commons.math3.linear.DecompositionSolver; import org.apache.commons.math3.linear.LUDecomposition; import org.apache.commons.math3.linear.QRDecomposition; import org.apache.commons.math3.linear.RealMatrix; import org.apache.commons.math3.linear.SingularMatrixException; import org.apache.commons.math3.optimization.ConvergenceChecker; import org.apache.commons.math3.optimization.SimpleVectorValueChecker; import org.apache.commons.math3.optimization.PointVectorValuePair; /** * Gauss-Newton least-squares solver. *

                            * This class solve a least-square problem by solving the normal equations * of the linearized problem at each iteration. Either LU decomposition or * QR decomposition can be used to solve the normal equations. LU decomposition * is faster but QR decomposition is more robust for difficult problems. *

                            * * @version $Id: GaussNewtonOptimizer.java 1423687 2012-12-18 21:56:18Z erans $ * @deprecated As of 3.1 (to be removed in 4.0). * @since 2.0 * */ @Deprecated public class GaussNewtonOptimizer extends AbstractLeastSquaresOptimizer { /** Indicator for using LU decomposition. */ private final boolean useLU; /** * Simple constructor with default settings. * The normal equations will be solved using LU decomposition and the * convergence check is set to a {@link SimpleVectorValueChecker} * with default tolerances. * @deprecated See {@link SimpleVectorValueChecker#SimpleVectorValueChecker()} */ @Deprecated public GaussNewtonOptimizer() { this(true); } /** * Simple constructor with default settings. * The normal equations will be solved using LU decomposition. * * @param checker Convergence checker. */ public GaussNewtonOptimizer(ConvergenceChecker checker) { this(true, checker); } /** * Simple constructor with default settings. * The convergence check is set to a {@link SimpleVectorValueChecker} * with default tolerances. * * @param useLU If {@code true}, the normal equations will be solved * using LU decomposition, otherwise they will be solved using QR * decomposition. * @deprecated See {@link SimpleVectorValueChecker#SimpleVectorValueChecker()} */ @Deprecated public GaussNewtonOptimizer(final boolean useLU) { this(useLU, new SimpleVectorValueChecker()); } /** * @param useLU If {@code true}, the normal equations will be solved * using LU decomposition, otherwise they will be solved using QR * decomposition. * @param checker Convergence checker. */ public GaussNewtonOptimizer(final boolean useLU, ConvergenceChecker checker) { super(checker); this.useLU = useLU; } /** {@inheritDoc} */ @Override public PointVectorValuePair doOptimize() { final ConvergenceChecker checker = getConvergenceChecker(); // Computation will be useless without a checker (see "for-loop"). if (checker == null) { throw new NullArgumentException(); } final double[] targetValues = getTarget(); final int nR = targetValues.length; // Number of observed data. final RealMatrix weightMatrix = getWeight(); // Diagonal of the weight matrix. final double[] residualsWeights = new double[nR]; for (int i = 0; i < nR; i++) { residualsWeights[i] = weightMatrix.getEntry(i, i); } final double[] currentPoint = getStartPoint(); final int nC = currentPoint.length; // iterate until convergence is reached PointVectorValuePair current = null; int iter = 0; for (boolean converged = false; !converged;) { ++iter; // evaluate the objective function and its jacobian PointVectorValuePair previous = current; // Value of the objective function at "currentPoint". final double[] currentObjective = computeObjectiveValue(currentPoint); final double[] currentResiduals = computeResiduals(currentObjective); final RealMatrix weightedJacobian = computeWeightedJacobian(currentPoint); current = new PointVectorValuePair(currentPoint, currentObjective); // build the linear problem final double[] b = new double[nC]; final double[][] a = new double[nC][nC]; for (int i = 0; i < nR; ++i) { final double[] grad = weightedJacobian.getRow(i); final double weight = residualsWeights[i]; final double residual = currentResiduals[i]; // compute the normal equation final double wr = weight * residual; for (int j = 0; j < nC; ++j) { b[j] += wr * grad[j]; } // build the contribution matrix for measurement i for (int k = 0; k < nC; ++k) { double[] ak = a[k]; double wgk = weight * grad[k]; for (int l = 0; l < nC; ++l) { ak[l] += wgk * grad[l]; } } } try { // solve the linearized least squares problem RealMatrix mA = new BlockRealMatrix(a); DecompositionSolver solver = useLU ? new LUDecomposition(mA).getSolver() : new QRDecomposition(mA).getSolver(); final double[] dX = solver.solve(new ArrayRealVector(b, false)).toArray(); // update the estimated parameters for (int i = 0; i < nC; ++i) { currentPoint[i] += dX[i]; } } catch (SingularMatrixException e) { throw new ConvergenceException(LocalizedFormats.UNABLE_TO_SOLVE_SINGULAR_PROBLEM); } // Check convergence. if (previous != null) { converged = checker.converged(iter, previous, current); if (converged) { cost = computeCost(currentResiduals); // Update (deprecated) "point" field. point = current.getPoint(); return current; } } } // Must never happen. throw new MathInternalError(); } } ././@LongLink100644 0 0 146 12126630646 10262 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/optimization/general/Preconditioner.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/optimization/general/Preconditioner.jav100644 1750 1750 4226 12126627713 32360 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.optimization.general; /** * This interface represents a preconditioner for differentiable scalar * objective function optimizers. * @version $Id: Preconditioner.java 1422230 2012-12-15 12:11:13Z erans $ * @deprecated As of 3.1 (to be removed in 4.0). * @since 2.0 */ @Deprecated public interface Preconditioner { /** * Precondition a search direction. *

                            * The returned preconditioned search direction must be computed fast or * the algorithm performances will drop drastically. A classical approach * is to compute only the diagonal elements of the hessian and to divide * the raw search direction by these elements if they are all positive. * If at least one of them is negative, it is safer to return a clone of * the raw search direction as if the hessian was the identity matrix. The * rationale for this simplified choice is that a negative diagonal element * means the current point is far from the optimum and preconditioning will * not be efficient anyway in this case. *

                            * @param point current point at which the search direction was computed * @param r raw search direction (i.e. opposite of the gradient) * @return approximation of H-1r where H is the objective function hessian */ double[] precondition(double[] point, double[] r); } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/optimization/SimplePointChecker.java100644 1750 1750 13605 12126627713 31671 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.optimization; import org.apache.commons.math3.util.FastMath; import org.apache.commons.math3.util.Pair; import org.apache.commons.math3.exception.NotStrictlyPositiveException; /** * Simple implementation of the {@link ConvergenceChecker} interface using * only point coordinates. * * Convergence is considered to have been reached if either the relative * difference between each point coordinate are smaller than a threshold * or if either the absolute difference between the point coordinates are * smaller than another threshold. *
                            * The {@link #converged(int,Pair,Pair) converged} method will also return * {@code true} if the number of iterations has been set (see * {@link #SimplePointChecker(double,double,int) this constructor}). * * @param Type of the (point, value) pair. * The type of the "value" part of the pair (not used by this class). * * @version $Id: SimplePointChecker.java 1462503 2013-03-29 15:48:27Z luc $ * @deprecated As of 3.1 (to be removed in 4.0). * @since 3.0 */ @Deprecated public class SimplePointChecker> extends AbstractConvergenceChecker { /** * If {@link #maxIterationCount} is set to this value, the number of * iterations will never cause {@link #converged(int, Pair, Pair)} * to return {@code true}. */ private static final int ITERATION_CHECK_DISABLED = -1; /** * Number of iterations after which the * {@link #converged(int, Pair, Pair)} method * will return true (unless the check is disabled). */ private final int maxIterationCount; /** * Build an instance with default threshold. * @deprecated See {@link AbstractConvergenceChecker#AbstractConvergenceChecker()} */ @Deprecated public SimplePointChecker() { maxIterationCount = ITERATION_CHECK_DISABLED; } /** * Build an instance with specified thresholds. * In order to perform only relative checks, the absolute tolerance * must be set to a negative value. In order to perform only absolute * checks, the relative tolerance must be set to a negative value. * * @param relativeThreshold relative tolerance threshold * @param absoluteThreshold absolute tolerance threshold */ public SimplePointChecker(final double relativeThreshold, final double absoluteThreshold) { super(relativeThreshold, absoluteThreshold); maxIterationCount = ITERATION_CHECK_DISABLED; } /** * Builds an instance with specified thresholds. * In order to perform only relative checks, the absolute tolerance * must be set to a negative value. In order to perform only absolute * checks, the relative tolerance must be set to a negative value. * * @param relativeThreshold Relative tolerance threshold. * @param absoluteThreshold Absolute tolerance threshold. * @param maxIter Maximum iteration count. * @throws NotStrictlyPositiveException if {@code maxIter <= 0}. * * @since 3.1 */ public SimplePointChecker(final double relativeThreshold, final double absoluteThreshold, final int maxIter) { super(relativeThreshold, absoluteThreshold); if (maxIter <= 0) { throw new NotStrictlyPositiveException(maxIter); } maxIterationCount = maxIter; } /** * Check if the optimization algorithm has converged considering the * last two points. * This method may be called several times from the same algorithm * iteration with different points. This can be detected by checking the * iteration number at each call if needed. Each time this method is * called, the previous and current point correspond to points with the * same role at each iteration, so they can be compared. As an example, * simplex-based algorithms call this method for all points of the simplex, * not only for the best or worst ones. * * @param iteration Index of current iteration * @param previous Best point in the previous iteration. * @param current Best point in the current iteration. * @return {@code true} if the arguments satify the convergence criterion. */ @Override public boolean converged(final int iteration, final PAIR previous, final PAIR current) { if (maxIterationCount != ITERATION_CHECK_DISABLED && iteration >= maxIterationCount) { return true; } final double[] p = previous.getKey(); final double[] c = current.getKey(); for (int i = 0; i < p.length; ++i) { final double pi = p[i]; final double ci = c[i]; final double difference = FastMath.abs(pi - ci); final double size = FastMath.max(FastMath.abs(pi), FastMath.abs(ci)); if (difference > size * getRelativeThreshold() && difference > getAbsoluteThreshold()) { return false; } } return true; } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/optimization/BaseOptimizer.java100644 1750 1750 4437 12126627713 30701 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.optimization; /** * This interface is mainly intended to enforce the internal coherence of * Commons-Math. Users of the API are advised to base their code on * the following interfaces: *
                              *
                            • {@link org.apache.commons.math3.optimization.MultivariateOptimizer}
                            • *
                            • {@link org.apache.commons.math3.optimization.MultivariateDifferentiableOptimizer}
                            • *
                            • {@link org.apache.commons.math3.optimization.MultivariateDifferentiableVectorOptimizer}
                            • *
                            • {@link org.apache.commons.math3.optimization.univariate.UnivariateOptimizer}
                            • *
                            * * @param Type of the point/objective pair. * * @version $Id: BaseOptimizer.java 1422230 2012-12-15 12:11:13Z erans $ * @deprecated As of 3.1 (to be removed in 4.0). * @since 3.0 */ @Deprecated public interface BaseOptimizer { /** * Get the maximal number of function evaluations. * * @return the maximal number of function evaluations. */ int getMaxEvaluations(); /** * Get the number of evaluations of the objective function. * The number of evaluations corresponds to the last call to the * {@code optimize} method. It is 0 if the method has not been * called yet. * * @return the number of evaluations of the objective function. */ int getEvaluations(); /** * Get the convergence checker. * * @return the object used to check for convergence. */ ConvergenceChecker getConvergenceChecker(); } ././@LongLink100644 0 0 171 12126630646 10260 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/optimization/MultivariateDifferentiableVectorOptimizer.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/optimization/MultivariateDifferentiable100644 1750 1750 2647 12126627713 32477 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.optimization; import org.apache.commons.math3.analysis.differentiation.MultivariateDifferentiableVectorFunction; /** * This interface represents an optimization algorithm for * {@link MultivariateDifferentiableVectorFunction differentiable vectorial * objective functions}. * * @version $Id: MultivariateDifferentiableVectorOptimizer.java 1422230 2012-12-15 12:11:13Z erans $ * @deprecated As of 3.1 (to be removed in 4.0). * @since 3.1 */ @Deprecated public interface MultivariateDifferentiableVectorOptimizer extends BaseMultivariateVectorOptimizer {} commons-math3-3.2-src/src/main/java/org/apache/commons/math3/optimization/GoalType.java100644 1750 1750 2301 12126627713 27634 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.optimization; import java.io.Serializable; /** * Goal type for an optimization problem. * * @version $Id: GoalType.java 1422230 2012-12-15 12:11:13Z erans $ * @deprecated As of 3.1 (to be removed in 4.0). * @since 2.0 */ @Deprecated public enum GoalType implements Serializable { /** Maximization goal. */ MAXIMIZE, /** Minimization goal. */ MINIMIZE } ././@LongLink100644 0 0 145 12126630646 10261 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/optimization/LeastSquaresConverter.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/optimization/LeastSquaresConverter.java100644 1750 1750 20066 12126627713 32444 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.optimization; import org.apache.commons.math3.analysis.MultivariateFunction; import org.apache.commons.math3.analysis.MultivariateVectorFunction; import org.apache.commons.math3.exception.DimensionMismatchException; import org.apache.commons.math3.linear.RealMatrix; /** This class converts {@link MultivariateVectorFunction vectorial * objective functions} to {@link MultivariateFunction scalar objective functions} * when the goal is to minimize them. *

                            * This class is mostly used when the vectorial objective function represents * a theoretical result computed from a point set applied to a model and * the models point must be adjusted to fit the theoretical result to some * reference observations. The observations may be obtained for example from * physical measurements whether the model is built from theoretical * considerations. *

                            *

                            * This class computes a possibly weighted squared sum of the residuals, which is * a scalar value. The residuals are the difference between the theoretical model * (i.e. the output of the vectorial objective function) and the observations. The * class implements the {@link MultivariateFunction} interface and can therefore be * minimized by any optimizer supporting scalar objectives functions.This is one way * to perform a least square estimation. There are other ways to do this without using * this converter, as some optimization algorithms directly support vectorial objective * functions. *

                            *

                            * This class support combination of residuals with or without weights and correlations. *

                            * * @see MultivariateFunction * @see MultivariateVectorFunction * @version $Id: LeastSquaresConverter.java 1422230 2012-12-15 12:11:13Z erans $ * @deprecated As of 3.1 (to be removed in 4.0). * @since 2.0 */ @Deprecated public class LeastSquaresConverter implements MultivariateFunction { /** Underlying vectorial function. */ private final MultivariateVectorFunction function; /** Observations to be compared to objective function to compute residuals. */ private final double[] observations; /** Optional weights for the residuals. */ private final double[] weights; /** Optional scaling matrix (weight and correlations) for the residuals. */ private final RealMatrix scale; /** Build a simple converter for uncorrelated residuals with the same weight. * @param function vectorial residuals function to wrap * @param observations observations to be compared to objective function to compute residuals */ public LeastSquaresConverter(final MultivariateVectorFunction function, final double[] observations) { this.function = function; this.observations = observations.clone(); this.weights = null; this.scale = null; } /** Build a simple converter for uncorrelated residuals with the specific weights. *

                            * The scalar objective function value is computed as: *

                                 * objective = ∑weighti(observationi-objectivei)2
                                 * 
                            *

                            *

                            * Weights can be used for example to combine residuals with different standard * deviations. As an example, consider a residuals array in which even elements * are angular measurements in degrees with a 0.01° standard deviation and * odd elements are distance measurements in meters with a 15m standard deviation. * In this case, the weights array should be initialized with value * 1.0/(0.012) in the even elements and 1.0/(15.02) in the * odd elements (i.e. reciprocals of variances). *

                            *

                            * The array computed by the objective function, the observations array and the * weights array must have consistent sizes or a {@link DimensionMismatchException} * will be triggered while computing the scalar objective. *

                            * @param function vectorial residuals function to wrap * @param observations observations to be compared to objective function to compute residuals * @param weights weights to apply to the residuals * @exception DimensionMismatchException if the observations vector and the weights * vector dimensions do not match (objective function dimension is checked only when * the {@link #value(double[])} method is called) */ public LeastSquaresConverter(final MultivariateVectorFunction function, final double[] observations, final double[] weights) { if (observations.length != weights.length) { throw new DimensionMismatchException(observations.length, weights.length); } this.function = function; this.observations = observations.clone(); this.weights = weights.clone(); this.scale = null; } /** Build a simple converter for correlated residuals with the specific weights. *

                            * The scalar objective function value is computed as: *

                                 * objective = yTy with y = scale×(observation-objective)
                                 * 
                            *

                            *

                            * The array computed by the objective function, the observations array and the * the scaling matrix must have consistent sizes or a {@link DimensionMismatchException} * will be triggered while computing the scalar objective. *

                            * @param function vectorial residuals function to wrap * @param observations observations to be compared to objective function to compute residuals * @param scale scaling matrix * @throws DimensionMismatchException if the observations vector and the scale * matrix dimensions do not match (objective function dimension is checked only when * the {@link #value(double[])} method is called) */ public LeastSquaresConverter(final MultivariateVectorFunction function, final double[] observations, final RealMatrix scale) { if (observations.length != scale.getColumnDimension()) { throw new DimensionMismatchException(observations.length, scale.getColumnDimension()); } this.function = function; this.observations = observations.clone(); this.weights = null; this.scale = scale.copy(); } /** {@inheritDoc} */ public double value(final double[] point) { // compute residuals final double[] residuals = function.value(point); if (residuals.length != observations.length) { throw new DimensionMismatchException(residuals.length, observations.length); } for (int i = 0; i < residuals.length; ++i) { residuals[i] -= observations[i]; } // compute sum of squares double sumSquares = 0; if (weights != null) { for (int i = 0; i < residuals.length; ++i) { final double ri = residuals[i]; sumSquares += weights[i] * ri * ri; } } else if (scale != null) { for (final double yi : scale.operate(residuals)) { sumSquares += yi * yi; } } else { for (final double ri : residuals) { sumSquares += ri * ri; } } return sumSquares; } } ././@LongLink100644 0 0 147 12126630646 10263 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/optimization/univariate/package-info.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/optimization/univariate/package-info.ja100644 1750 1750 1650 12126627713 32262 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ /** * * Univariate real functions minimum finding algorithms. * */ package org.apache.commons.math3.optimization.univariate; ././@LongLink100644 0 0 150 12126630646 10255 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/optimization/univariate/BracketFinder.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/optimization/univariate/BracketFinder.j100644 1750 1750 17442 12126627713 32326 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.optimization.univariate; import org.apache.commons.math3.util.Incrementor; import org.apache.commons.math3.exception.NotStrictlyPositiveException; import org.apache.commons.math3.exception.TooManyEvaluationsException; import org.apache.commons.math3.exception.MaxCountExceededException; import org.apache.commons.math3.analysis.UnivariateFunction; import org.apache.commons.math3.optimization.GoalType; /** * Provide an interval that brackets a local optimum of a function. * This code is based on a Python implementation (from SciPy, * module {@code optimize.py} v0.5). * * @version $Id: BracketFinder.java 1422230 2012-12-15 12:11:13Z erans $ * @deprecated As of 3.1 (to be removed in 4.0). * @since 2.2 */ @Deprecated public class BracketFinder { /** Tolerance to avoid division by zero. */ private static final double EPS_MIN = 1e-21; /** * Golden section. */ private static final double GOLD = 1.618034; /** * Factor for expanding the interval. */ private final double growLimit; /** * Counter for function evaluations. */ private final Incrementor evaluations = new Incrementor(); /** * Lower bound of the bracket. */ private double lo; /** * Higher bound of the bracket. */ private double hi; /** * Point inside the bracket. */ private double mid; /** * Function value at {@link #lo}. */ private double fLo; /** * Function value at {@link #hi}. */ private double fHi; /** * Function value at {@link #mid}. */ private double fMid; /** * Constructor with default values {@code 100, 50} (see the * {@link #BracketFinder(double,int) other constructor}). */ public BracketFinder() { this(100, 50); } /** * Create a bracketing interval finder. * * @param growLimit Expanding factor. * @param maxEvaluations Maximum number of evaluations allowed for finding * a bracketing interval. */ public BracketFinder(double growLimit, int maxEvaluations) { if (growLimit <= 0) { throw new NotStrictlyPositiveException(growLimit); } if (maxEvaluations <= 0) { throw new NotStrictlyPositiveException(maxEvaluations); } this.growLimit = growLimit; evaluations.setMaximalCount(maxEvaluations); } /** * Search new points that bracket a local optimum of the function. * * @param func Function whose optimum should be bracketed. * @param goal {@link GoalType Goal type}. * @param xA Initial point. * @param xB Initial point. * @throws TooManyEvaluationsException if the maximum number of evaluations * is exceeded. */ public void search(UnivariateFunction func, GoalType goal, double xA, double xB) { evaluations.resetCount(); final boolean isMinim = goal == GoalType.MINIMIZE; double fA = eval(func, xA); double fB = eval(func, xB); if (isMinim ? fA < fB : fA > fB) { double tmp = xA; xA = xB; xB = tmp; tmp = fA; fA = fB; fB = tmp; } double xC = xB + GOLD * (xB - xA); double fC = eval(func, xC); while (isMinim ? fC < fB : fC > fB) { double tmp1 = (xB - xA) * (fB - fC); double tmp2 = (xB - xC) * (fB - fA); double val = tmp2 - tmp1; double denom = Math.abs(val) < EPS_MIN ? 2 * EPS_MIN : 2 * val; double w = xB - ((xB - xC) * tmp2 - (xB - xA) * tmp1) / denom; double wLim = xB + growLimit * (xC - xB); double fW; if ((w - xC) * (xB - w) > 0) { fW = eval(func, w); if (isMinim ? fW < fC : fW > fC) { xA = xB; xB = w; fA = fB; fB = fW; break; } else if (isMinim ? fW > fB : fW < fB) { xC = w; fC = fW; break; } w = xC + GOLD * (xC - xB); fW = eval(func, w); } else if ((w - wLim) * (wLim - xC) >= 0) { w = wLim; fW = eval(func, w); } else if ((w - wLim) * (xC - w) > 0) { fW = eval(func, w); if (isMinim ? fW < fC : fW > fC) { xB = xC; xC = w; w = xC + GOLD * (xC - xB); fB = fC; fC =fW; fW = eval(func, w); } } else { w = xC + GOLD * (xC - xB); fW = eval(func, w); } xA = xB; fA = fB; xB = xC; fB = fC; xC = w; fC = fW; } lo = xA; fLo = fA; mid = xB; fMid = fB; hi = xC; fHi = fC; if (lo > hi) { double tmp = lo; lo = hi; hi = tmp; tmp = fLo; fLo = fHi; fHi = tmp; } } /** * @return the number of evalutations. */ public int getMaxEvaluations() { return evaluations.getMaximalCount(); } /** * @return the number of evalutations. */ public int getEvaluations() { return evaluations.getCount(); } /** * @return the lower bound of the bracket. * @see #getFLo() */ public double getLo() { return lo; } /** * Get function value at {@link #getLo()}. * @return function value at {@link #getLo()} */ public double getFLo() { return fLo; } /** * @return the higher bound of the bracket. * @see #getFHi() */ public double getHi() { return hi; } /** * Get function value at {@link #getHi()}. * @return function value at {@link #getHi()} */ public double getFHi() { return fHi; } /** * @return a point in the middle of the bracket. * @see #getFMid() */ public double getMid() { return mid; } /** * Get function value at {@link #getMid()}. * @return function value at {@link #getMid()} */ public double getFMid() { return fMid; } /** * @param f Function. * @param x Argument. * @return {@code f(x)} * @throws TooManyEvaluationsException if the maximal number of evaluations is * exceeded. */ private double eval(UnivariateFunction f, double x) { try { evaluations.incrementCount(); } catch (MaxCountExceededException e) { throw new TooManyEvaluationsException(e.getMax()); } return f.value(x); } } ././@LongLink100644 0 0 170 12126630646 10257 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/optimization/univariate/UnivariateMultiStartOptimizer.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/optimization/univariate/UnivariateMulti100644 1750 1750 20177 12126627713 32514 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.optimization.univariate; import java.util.Arrays; import java.util.Comparator; import org.apache.commons.math3.analysis.UnivariateFunction; import org.apache.commons.math3.exception.MathIllegalStateException; import org.apache.commons.math3.exception.NotStrictlyPositiveException; import org.apache.commons.math3.exception.NullArgumentException; import org.apache.commons.math3.exception.util.LocalizedFormats; import org.apache.commons.math3.random.RandomGenerator; import org.apache.commons.math3.optimization.GoalType; import org.apache.commons.math3.optimization.ConvergenceChecker; /** * Special implementation of the {@link UnivariateOptimizer} interface * adding multi-start features to an existing optimizer. * * This class wraps a classical optimizer to use it several times in * turn with different starting points in order to avoid being trapped * into a local extremum when looking for a global one. * * @param Type of the objective function to be optimized. * * @version $Id: UnivariateMultiStartOptimizer.java 1422230 2012-12-15 12:11:13Z erans $ * @deprecated As of 3.1 (to be removed in 4.0). * @since 3.0 */ @Deprecated public class UnivariateMultiStartOptimizer implements BaseUnivariateOptimizer { /** Underlying classical optimizer. */ private final BaseUnivariateOptimizer optimizer; /** Maximal number of evaluations allowed. */ private int maxEvaluations; /** Number of evaluations already performed for all starts. */ private int totalEvaluations; /** Number of starts to go. */ private int starts; /** Random generator for multi-start. */ private RandomGenerator generator; /** Found optima. */ private UnivariatePointValuePair[] optima; /** * Create a multi-start optimizer from a single-start optimizer. * * @param optimizer Single-start optimizer to wrap. * @param starts Number of starts to perform. If {@code starts == 1}, * the {@code optimize} methods will return the same solution as * {@code optimizer} would. * @param generator Random generator to use for restarts. * @throws NullArgumentException if {@code optimizer} or {@code generator} * is {@code null}. * @throws NotStrictlyPositiveException if {@code starts < 1}. */ public UnivariateMultiStartOptimizer(final BaseUnivariateOptimizer optimizer, final int starts, final RandomGenerator generator) { if (optimizer == null || generator == null) { throw new NullArgumentException(); } if (starts < 1) { throw new NotStrictlyPositiveException(starts); } this.optimizer = optimizer; this.starts = starts; this.generator = generator; } /** * {@inheritDoc} */ public ConvergenceChecker getConvergenceChecker() { return optimizer.getConvergenceChecker(); } /** {@inheritDoc} */ public int getMaxEvaluations() { return maxEvaluations; } /** {@inheritDoc} */ public int getEvaluations() { return totalEvaluations; } /** * Get all the optima found during the last call to {@link * #optimize(int,UnivariateFunction,GoalType,double,double) optimize}. * The optimizer stores all the optima found during a set of * restarts. The {@link #optimize(int,UnivariateFunction,GoalType,double,double) optimize} * method returns the best point only. This method returns all the points * found at the end of each starts, including the best one already * returned by the {@link #optimize(int,UnivariateFunction,GoalType,double,double) optimize} * method. *
                            * The returned array as one element for each start as specified * in the constructor. It is ordered with the results from the * runs that did converge first, sorted from best to worst * objective value (i.e in ascending order if minimizing and in * descending order if maximizing), followed by {@code null} elements * corresponding to the runs that did not converge. This means all * elements will be {@code null} if the {@link * #optimize(int,UnivariateFunction,GoalType,double,double) optimize} * method did throw an exception. * This also means that if the first element is not {@code null}, it is * the best point found across all starts. * * @return an array containing the optima. * @throws MathIllegalStateException if {@link * #optimize(int,UnivariateFunction,GoalType,double,double) optimize} * has not been called. */ public UnivariatePointValuePair[] getOptima() { if (optima == null) { throw new MathIllegalStateException(LocalizedFormats.NO_OPTIMUM_COMPUTED_YET); } return optima.clone(); } /** {@inheritDoc} */ public UnivariatePointValuePair optimize(int maxEval, final FUNC f, final GoalType goal, final double min, final double max) { return optimize(maxEval, f, goal, min, max, min + 0.5 * (max - min)); } /** {@inheritDoc} */ public UnivariatePointValuePair optimize(int maxEval, final FUNC f, final GoalType goal, final double min, final double max, final double startValue) { RuntimeException lastException = null; optima = new UnivariatePointValuePair[starts]; totalEvaluations = 0; // Multi-start loop. for (int i = 0; i < starts; ++i) { // CHECKSTYLE: stop IllegalCatch try { final double s = (i == 0) ? startValue : min + generator.nextDouble() * (max - min); optima[i] = optimizer.optimize(maxEval - totalEvaluations, f, goal, min, max, s); } catch (RuntimeException mue) { lastException = mue; optima[i] = null; } // CHECKSTYLE: resume IllegalCatch totalEvaluations += optimizer.getEvaluations(); } sortPairs(goal); if (optima[0] == null) { throw lastException; // cannot be null if starts >=1 } // Return the point with the best objective function value. return optima[0]; } /** * Sort the optima from best to worst, followed by {@code null} elements. * * @param goal Goal type. */ private void sortPairs(final GoalType goal) { Arrays.sort(optima, new Comparator() { public int compare(final UnivariatePointValuePair o1, final UnivariatePointValuePair o2) { if (o1 == null) { return (o2 == null) ? 0 : 1; } else if (o2 == null) { return -1; } final double v1 = o1.getValue(); final double v2 = o2.getValue(); return (goal == GoalType.MINIMIZE) ? Double.compare(v1, v2) : Double.compare(v2, v1); } }); } } ././@LongLink100644 0 0 167 12126630646 10265 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/optimization/univariate/SimpleUnivariateValueChecker.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/optimization/univariate/SimpleUnivariat100644 1750 1750 13574 12126627713 32511 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.optimization.univariate; import org.apache.commons.math3.util.FastMath; import org.apache.commons.math3.exception.NotStrictlyPositiveException; import org.apache.commons.math3.optimization.AbstractConvergenceChecker; /** * Simple implementation of the * {@link org.apache.commons.math3.optimization.ConvergenceChecker} interface * that uses only objective function values. * * Convergence is considered to have been reached if either the relative * difference between the objective function values is smaller than a * threshold or if either the absolute difference between the objective * function values is smaller than another threshold. *
                            * The {@link #converged(int,UnivariatePointValuePair,UnivariatePointValuePair) * converged} method will also return {@code true} if the number of iterations * has been set (see {@link #SimpleUnivariateValueChecker(double,double,int) * this constructor}). * * @version $Id: SimpleUnivariateValueChecker.java 1462503 2013-03-29 15:48:27Z luc $ * @deprecated As of 3.1 (to be removed in 4.0). * @since 3.1 */ @Deprecated public class SimpleUnivariateValueChecker extends AbstractConvergenceChecker { /** * If {@link #maxIterationCount} is set to this value, the number of * iterations will never cause * {@link #converged(int,UnivariatePointValuePair,UnivariatePointValuePair)} * to return {@code true}. */ private static final int ITERATION_CHECK_DISABLED = -1; /** * Number of iterations after which the * {@link #converged(int,UnivariatePointValuePair,UnivariatePointValuePair)} * method will return true (unless the check is disabled). */ private final int maxIterationCount; /** * Build an instance with default thresholds. * @deprecated See {@link AbstractConvergenceChecker#AbstractConvergenceChecker()} */ @Deprecated public SimpleUnivariateValueChecker() { maxIterationCount = ITERATION_CHECK_DISABLED; } /** Build an instance with specified thresholds. * * In order to perform only relative checks, the absolute tolerance * must be set to a negative value. In order to perform only absolute * checks, the relative tolerance must be set to a negative value. * * @param relativeThreshold relative tolerance threshold * @param absoluteThreshold absolute tolerance threshold */ public SimpleUnivariateValueChecker(final double relativeThreshold, final double absoluteThreshold) { super(relativeThreshold, absoluteThreshold); maxIterationCount = ITERATION_CHECK_DISABLED; } /** * Builds an instance with specified thresholds. * * In order to perform only relative checks, the absolute tolerance * must be set to a negative value. In order to perform only absolute * checks, the relative tolerance must be set to a negative value. * * @param relativeThreshold relative tolerance threshold * @param absoluteThreshold absolute tolerance threshold * @param maxIter Maximum iteration count. * @throws NotStrictlyPositiveException if {@code maxIter <= 0}. * * @since 3.1 */ public SimpleUnivariateValueChecker(final double relativeThreshold, final double absoluteThreshold, final int maxIter) { super(relativeThreshold, absoluteThreshold); if (maxIter <= 0) { throw new NotStrictlyPositiveException(maxIter); } maxIterationCount = maxIter; } /** * Check if the optimization algorithm has converged considering the * last two points. * This method may be called several time from the same algorithm * iteration with different points. This can be detected by checking the * iteration number at each call if needed. Each time this method is * called, the previous and current point correspond to points with the * same role at each iteration, so they can be compared. As an example, * simplex-based algorithms call this method for all points of the simplex, * not only for the best or worst ones. * * @param iteration Index of current iteration * @param previous Best point in the previous iteration. * @param current Best point in the current iteration. * @return {@code true} if the algorithm has converged. */ @Override public boolean converged(final int iteration, final UnivariatePointValuePair previous, final UnivariatePointValuePair current) { if (maxIterationCount != ITERATION_CHECK_DISABLED && iteration >= maxIterationCount) { return true; } final double p = previous.getValue(); final double c = current.getValue(); final double difference = FastMath.abs(p - c); final double size = FastMath.max(FastMath.abs(p), FastMath.abs(c)); return difference <= size * getRelativeThreshold() || difference <= getAbsoluteThreshold(); } } ././@LongLink100644 0 0 172 12126630646 10261 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/optimization/univariate/BaseAbstractUnivariateOptimizer.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/optimization/univariate/BaseAbstractUni100644 1750 1750 12370 12126627713 32400 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.optimization.univariate; import org.apache.commons.math3.util.Incrementor; import org.apache.commons.math3.exception.MaxCountExceededException; import org.apache.commons.math3.exception.TooManyEvaluationsException; import org.apache.commons.math3.exception.NullArgumentException; import org.apache.commons.math3.analysis.UnivariateFunction; import org.apache.commons.math3.optimization.GoalType; import org.apache.commons.math3.optimization.ConvergenceChecker; /** * Provide a default implementation for several functions useful to generic * optimizers. * * @version $Id: BaseAbstractUnivariateOptimizer.java 1422230 2012-12-15 12:11:13Z erans $ * @deprecated As of 3.1 (to be removed in 4.0). * @since 2.0 */ @Deprecated public abstract class BaseAbstractUnivariateOptimizer implements UnivariateOptimizer { /** Convergence checker. */ private final ConvergenceChecker checker; /** Evaluations counter. */ private final Incrementor evaluations = new Incrementor(); /** Optimization type */ private GoalType goal; /** Lower end of search interval. */ private double searchMin; /** Higher end of search interval. */ private double searchMax; /** Initial guess . */ private double searchStart; /** Function to optimize. */ private UnivariateFunction function; /** * @param checker Convergence checking procedure. */ protected BaseAbstractUnivariateOptimizer(ConvergenceChecker checker) { this.checker = checker; } /** {@inheritDoc} */ public int getMaxEvaluations() { return evaluations.getMaximalCount(); } /** {@inheritDoc} */ public int getEvaluations() { return evaluations.getCount(); } /** * @return the optimization type. */ public GoalType getGoalType() { return goal; } /** * @return the lower end of the search interval. */ public double getMin() { return searchMin; } /** * @return the higher end of the search interval. */ public double getMax() { return searchMax; } /** * @return the initial guess. */ public double getStartValue() { return searchStart; } /** * Compute the objective function value. * * @param point Point at which the objective function must be evaluated. * @return the objective function value at specified point. * @throws TooManyEvaluationsException if the maximal number of evaluations * is exceeded. */ protected double computeObjectiveValue(double point) { try { evaluations.incrementCount(); } catch (MaxCountExceededException e) { throw new TooManyEvaluationsException(e.getMax()); } return function.value(point); } /** {@inheritDoc} */ public UnivariatePointValuePair optimize(int maxEval, UnivariateFunction f, GoalType goalType, double min, double max, double startValue) { // Checks. if (f == null) { throw new NullArgumentException(); } if (goalType == null) { throw new NullArgumentException(); } // Reset. searchMin = min; searchMax = max; searchStart = startValue; goal = goalType; function = f; evaluations.setMaximalCount(maxEval); evaluations.resetCount(); // Perform computation. return doOptimize(); } /** {@inheritDoc} */ public UnivariatePointValuePair optimize(int maxEval, UnivariateFunction f, GoalType goalType, double min, double max){ return optimize(maxEval, f, goalType, min, max, min + 0.5 * (max - min)); } /** * {@inheritDoc} */ public ConvergenceChecker getConvergenceChecker() { return checker; } /** * Method for implementing actual optimization algorithms in derived * classes. * * @return the optimum and its corresponding function value. * @throws TooManyEvaluationsException if the maximal number of evaluations * is exceeded. */ protected abstract UnivariatePointValuePair doOptimize(); } ././@LongLink100644 0 0 163 12126630646 10261 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/optimization/univariate/UnivariatePointValuePair.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/optimization/univariate/UnivariatePoint100644 1750 1750 4175 12126627713 32473 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.optimization.univariate; import java.io.Serializable; /** * This class holds a point and the value of an objective function at this * point. * This is a simple immutable container. * * @version $Id: UnivariatePointValuePair.java 1422230 2012-12-15 12:11:13Z erans $ * @deprecated As of 3.1 (to be removed in 4.0). * @since 3.0 */ @Deprecated public class UnivariatePointValuePair implements Serializable { /** Serializable version identifier. */ private static final long serialVersionUID = 1003888396256744753L; /** Point. */ private final double point; /** Value of the objective function at the point. */ private final double value; /** * Build a point/objective function value pair. * * @param point Point. * @param value Value of an objective function at the point */ public UnivariatePointValuePair(final double point, final double value) { this.point = point; this.value = value; } /** * Get the point. * * @return the point. */ public double getPoint() { return point; } /** * Get the value of the objective function. * * @return the stored value of the objective function. */ public double getValue() { return value; } } ././@LongLink100644 0 0 156 12126630646 10263 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/optimization/univariate/UnivariateOptimizer.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/optimization/univariate/UnivariateOptim100644 1750 1750 2326 12126627713 32466 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.optimization.univariate; import org.apache.commons.math3.analysis.UnivariateFunction; /** * Interface for univariate optimization algorithms. * * @version $Id: UnivariateOptimizer.java 1422230 2012-12-15 12:11:13Z erans $ * @deprecated As of 3.1 (to be removed in 4.0). * @since 3.0 */ @Deprecated public interface UnivariateOptimizer extends BaseUnivariateOptimizer {} ././@LongLink100644 0 0 151 12126630646 10256 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/optimization/univariate/BrentOptimizer.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/optimization/univariate/BrentOptimizer.100644 1750 1750 25506 12126627713 32426 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.optimization.univariate; import org.apache.commons.math3.util.Precision; import org.apache.commons.math3.util.FastMath; import org.apache.commons.math3.exception.NumberIsTooSmallException; import org.apache.commons.math3.exception.NotStrictlyPositiveException; import org.apache.commons.math3.optimization.ConvergenceChecker; import org.apache.commons.math3.optimization.GoalType; /** * For a function defined on some interval {@code (lo, hi)}, this class * finds an approximation {@code x} to the point at which the function * attains its minimum. * It implements Richard Brent's algorithm (from his book "Algorithms for * Minimization without Derivatives", p. 79) for finding minima of real * univariate functions. *
                            * This code is an adaptation, partly based on the Python code from SciPy * (module "optimize.py" v0.5); the original algorithm is also modified *
                              *
                            • to use an initial guess provided by the user,
                            • *
                            • to ensure that the best point encountered is the one returned.
                            • *
                            * * @version $Id: BrentOptimizer.java 1462503 2013-03-29 15:48:27Z luc $ * @deprecated As of 3.1 (to be removed in 4.0). * @since 2.0 */ @Deprecated public class BrentOptimizer extends BaseAbstractUnivariateOptimizer { /** * Golden section. */ private static final double GOLDEN_SECTION = 0.5 * (3 - FastMath.sqrt(5)); /** * Minimum relative tolerance. */ private static final double MIN_RELATIVE_TOLERANCE = 2 * FastMath.ulp(1d); /** * Relative threshold. */ private final double relativeThreshold; /** * Absolute threshold. */ private final double absoluteThreshold; /** * The arguments are used implement the original stopping criterion * of Brent's algorithm. * {@code abs} and {@code rel} define a tolerance * {@code tol = rel |x| + abs}. {@code rel} should be no smaller than * 2 macheps and preferably not much less than sqrt(macheps), * where macheps is the relative machine precision. {@code abs} must * be positive. * * @param rel Relative threshold. * @param abs Absolute threshold. * @param checker Additional, user-defined, convergence checking * procedure. * @throws NotStrictlyPositiveException if {@code abs <= 0}. * @throws NumberIsTooSmallException if {@code rel < 2 * Math.ulp(1d)}. */ public BrentOptimizer(double rel, double abs, ConvergenceChecker checker) { super(checker); if (rel < MIN_RELATIVE_TOLERANCE) { throw new NumberIsTooSmallException(rel, MIN_RELATIVE_TOLERANCE, true); } if (abs <= 0) { throw new NotStrictlyPositiveException(abs); } relativeThreshold = rel; absoluteThreshold = abs; } /** * The arguments are used for implementing the original stopping criterion * of Brent's algorithm. * {@code abs} and {@code rel} define a tolerance * {@code tol = rel |x| + abs}. {@code rel} should be no smaller than * 2 macheps and preferably not much less than sqrt(macheps), * where macheps is the relative machine precision. {@code abs} must * be positive. * * @param rel Relative threshold. * @param abs Absolute threshold. * @throws NotStrictlyPositiveException if {@code abs <= 0}. * @throws NumberIsTooSmallException if {@code rel < 2 * Math.ulp(1d)}. */ public BrentOptimizer(double rel, double abs) { this(rel, abs, null); } /** {@inheritDoc} */ @Override protected UnivariatePointValuePair doOptimize() { final boolean isMinim = getGoalType() == GoalType.MINIMIZE; final double lo = getMin(); final double mid = getStartValue(); final double hi = getMax(); // Optional additional convergence criteria. final ConvergenceChecker checker = getConvergenceChecker(); double a; double b; if (lo < hi) { a = lo; b = hi; } else { a = hi; b = lo; } double x = mid; double v = x; double w = x; double d = 0; double e = 0; double fx = computeObjectiveValue(x); if (!isMinim) { fx = -fx; } double fv = fx; double fw = fx; UnivariatePointValuePair previous = null; UnivariatePointValuePair current = new UnivariatePointValuePair(x, isMinim ? fx : -fx); // Best point encountered so far (which is the initial guess). UnivariatePointValuePair best = current; int iter = 0; while (true) { final double m = 0.5 * (a + b); final double tol1 = relativeThreshold * FastMath.abs(x) + absoluteThreshold; final double tol2 = 2 * tol1; // Default stopping criterion. final boolean stop = FastMath.abs(x - m) <= tol2 - 0.5 * (b - a); if (!stop) { double p = 0; double q = 0; double r = 0; double u = 0; if (FastMath.abs(e) > tol1) { // Fit parabola. r = (x - w) * (fx - fv); q = (x - v) * (fx - fw); p = (x - v) * q - (x - w) * r; q = 2 * (q - r); if (q > 0) { p = -p; } else { q = -q; } r = e; e = d; if (p > q * (a - x) && p < q * (b - x) && FastMath.abs(p) < FastMath.abs(0.5 * q * r)) { // Parabolic interpolation step. d = p / q; u = x + d; // f must not be evaluated too close to a or b. if (u - a < tol2 || b - u < tol2) { if (x <= m) { d = tol1; } else { d = -tol1; } } } else { // Golden section step. if (x < m) { e = b - x; } else { e = a - x; } d = GOLDEN_SECTION * e; } } else { // Golden section step. if (x < m) { e = b - x; } else { e = a - x; } d = GOLDEN_SECTION * e; } // Update by at least "tol1". if (FastMath.abs(d) < tol1) { if (d >= 0) { u = x + tol1; } else { u = x - tol1; } } else { u = x + d; } double fu = computeObjectiveValue(u); if (!isMinim) { fu = -fu; } // User-defined convergence checker. previous = current; current = new UnivariatePointValuePair(u, isMinim ? fu : -fu); best = best(best, best(previous, current, isMinim), isMinim); if (checker != null && checker.converged(iter, previous, current)) { return best; } // Update a, b, v, w and x. if (fu <= fx) { if (u < x) { b = x; } else { a = x; } v = w; fv = fw; w = x; fw = fx; x = u; fx = fu; } else { if (u < x) { a = u; } else { b = u; } if (fu <= fw || Precision.equals(w, x)) { v = w; fv = fw; w = u; fw = fu; } else if (fu <= fv || Precision.equals(v, x) || Precision.equals(v, w)) { v = u; fv = fu; } } } else { // Default termination (Brent's criterion). return best(best, best(previous, current, isMinim), isMinim); } ++iter; } } /** * Selects the best of two points. * * @param a Point and value. * @param b Point and value. * @param isMinim {@code true} if the selected point must be the one with * the lowest value. * @return the best point, or {@code null} if {@code a} and {@code b} are * both {@code null}. When {@code a} and {@code b} have the same function * value, {@code a} is returned. */ private UnivariatePointValuePair best(UnivariatePointValuePair a, UnivariatePointValuePair b, boolean isMinim) { if (a == null) { return b; } if (b == null) { return a; } if (isMinim) { return a.getValue() <= b.getValue() ? a : b; } else { return a.getValue() >= b.getValue() ? a : b; } } } ././@LongLink100644 0 0 162 12126630646 10260 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/optimization/univariate/BaseUnivariateOptimizer.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/optimization/univariate/BaseUnivariateO100644 1750 1750 7764 12126627713 32402 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.optimization.univariate; import org.apache.commons.math3.analysis.UnivariateFunction; import org.apache.commons.math3.optimization.BaseOptimizer; import org.apache.commons.math3.optimization.GoalType; /** * This interface is mainly intended to enforce the internal coherence of * Commons-Math. Users of the API are advised to base their code on * the following interfaces: *
                              *
                            • {@link org.apache.commons.math3.optimization.univariate.UnivariateOptimizer}
                            • *
                            * * @param Type of the objective function to be optimized. * * @version $Id: BaseUnivariateOptimizer.java 1422230 2012-12-15 12:11:13Z erans $ * @deprecated As of 3.1 (to be removed in 4.0). * @since 3.0 */ @Deprecated public interface BaseUnivariateOptimizer extends BaseOptimizer { /** * Find an optimum in the given interval. * * An optimizer may require that the interval brackets a single optimum. * * @param f Function to optimize. * @param goalType Type of optimization goal: either * {@link GoalType#MAXIMIZE} or {@link GoalType#MINIMIZE}. * @param min Lower bound for the interval. * @param max Upper bound for the interval. * @param maxEval Maximum number of function evaluations. * @return a (point, value) pair where the function is optimum. * @throws org.apache.commons.math3.exception.TooManyEvaluationsException * if the maximum evaluation count is exceeded. * @throws org.apache.commons.math3.exception.ConvergenceException * if the optimizer detects a convergence problem. * @throws IllegalArgumentException if {@code min > max} or the endpoints * do not satisfy the requirements specified by the optimizer. */ UnivariatePointValuePair optimize(int maxEval, FUNC f, GoalType goalType, double min, double max); /** * Find an optimum in the given interval, start at startValue. * An optimizer may require that the interval brackets a single optimum. * * @param f Function to optimize. * @param goalType Type of optimization goal: either * {@link GoalType#MAXIMIZE} or {@link GoalType#MINIMIZE}. * @param min Lower bound for the interval. * @param max Upper bound for the interval. * @param startValue Start value to use. * @param maxEval Maximum number of function evaluations. * @return a (point, value) pair where the function is optimum. * @throws org.apache.commons.math3.exception.TooManyEvaluationsException * if the maximum evaluation count is exceeded. * @throws org.apache.commons.math3.exception.ConvergenceException if the * optimizer detects a convergence problem. * @throws IllegalArgumentException if {@code min > max} or the endpoints * do not satisfy the requirements specified by the optimizer. * @throws org.apache.commons.math3.exception.NullArgumentException if any * argument is {@code null}. */ UnivariatePointValuePair optimize(int maxEval, FUNC f, GoalType goalType, double min, double max, double startValue); } ././@LongLink100644 0 0 203 12126630646 10254 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/optimization/DifferentiableMultivariateVectorMultiStartOptimizer.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/optimization/DifferentiableMultivariate100644 1750 1750 4557 12126627713 32501 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.optimization; import org.apache.commons.math3.analysis.DifferentiableMultivariateVectorFunction; import org.apache.commons.math3.random.RandomVectorGenerator; /** * Special implementation of the {@link DifferentiableMultivariateVectorOptimizer} * interface addind multi-start features to an existing optimizer. * * This class wraps a classical optimizer to use it several times in * turn with different starting points in order to avoid being trapped * into a local extremum when looking for a global one. * * @version $Id: DifferentiableMultivariateVectorMultiStartOptimizer.java 1422230 2012-12-15 12:11:13Z erans $ * @deprecated As of 3.1 (to be removed in 4.0). * @since 2.0 */ @Deprecated public class DifferentiableMultivariateVectorMultiStartOptimizer extends BaseMultivariateVectorMultiStartOptimizer implements DifferentiableMultivariateVectorOptimizer { /** * Create a multi-start optimizer from a single-start optimizer. * * @param optimizer Single-start optimizer to wrap. * @param starts Number of starts to perform (including the * first one), multi-start is disabled if value is less than or * equal to 1. * @param generator Random vector generator to use for restarts. */ public DifferentiableMultivariateVectorMultiStartOptimizer( final DifferentiableMultivariateVectorOptimizer optimizer, final int starts, final RandomVectorGenerator generator) { super(optimizer, starts, generator); } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/optimization/OptimizationData.java100644 1750 1750 2435 12126627713 31400 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.optimization; /** * Marker interface. * Implementations will provide functionality (optional or required) needed * by the optimizers, and those will need to check the actual type of the * arguments and perform the appropriate cast in order to access the data * they need. * * @version $Id: OptimizationData.java 1422230 2012-12-15 12:11:13Z erans $ * @deprecated As of 3.1 (to be removed in 4.0). * @since 3.1 */ @Deprecated public interface OptimizationData {} ././@LongLink100644 0 0 157 12126630646 10264 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/optimization/BaseMultivariateVectorOptimizer.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/optimization/BaseMultivariateVectorOpti100644 1750 1750 6002 12126627713 32452 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.optimization; import org.apache.commons.math3.analysis.MultivariateVectorFunction; /** * This interface is mainly intended to enforce the internal coherence of * Commons-Math. Users of the API are advised to base their code on * the following interfaces: *
                              *
                            • {@link org.apache.commons.math3.optimization.DifferentiableMultivariateVectorOptimizer}
                            • *
                            * * @param Type of the objective function to be optimized. * * @version $Id: BaseMultivariateVectorOptimizer.java 1422230 2012-12-15 12:11:13Z erans $ * @deprecated As of 3.1 (to be removed in 4.0). * @since 3.0 */ @Deprecated public interface BaseMultivariateVectorOptimizer extends BaseOptimizer { /** * Optimize an objective function. * Optimization is considered to be a weighted least-squares minimization. * The cost function to be minimized is * ∑weighti(objectivei - targeti)2 * * @param f Objective function. * @param target Target value for the objective functions at optimum. * @param weight Weights for the least squares cost computation. * @param startPoint Start point for optimization. * @return the point/value pair giving the optimal value for objective * function. * @param maxEval Maximum number of function evaluations. * @throws org.apache.commons.math3.exception.DimensionMismatchException * if the start point dimension is wrong. * @throws org.apache.commons.math3.exception.TooManyEvaluationsException * if the maximal number of evaluations is exceeded. * @throws org.apache.commons.math3.exception.NullArgumentException if * any argument is {@code null}. * @deprecated As of 3.1. In 4.0, this will be replaced by the declaration * corresponding to this {@link org.apache.commons.math3.optimization.direct.BaseAbstractMultivariateVectorOptimizer#optimize(int,MultivariateVectorFunction,OptimizationData[]) method}. */ @Deprecated PointVectorValuePair optimize(int maxEval, FUNC f, double[] target, double[] weight, double[] startPoint); } ././@LongLink100644 0 0 203 12126630646 10254 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/optimization/MultivariateDifferentiableVectorMultiStartOptimizer.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/optimization/MultivariateDifferentiable100644 1750 1750 4577 12126627713 32503 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.optimization; import org.apache.commons.math3.analysis.differentiation.MultivariateDifferentiableVectorFunction; import org.apache.commons.math3.random.RandomVectorGenerator; /** * Special implementation of the {@link MultivariateDifferentiableVectorOptimizer} * interface adding multi-start features to an existing optimizer. * * This class wraps a classical optimizer to use it several times in * turn with different starting points in order to avoid being trapped * into a local extremum when looking for a global one. * * @version $Id: MultivariateDifferentiableVectorMultiStartOptimizer.java 1422230 2012-12-15 12:11:13Z erans $ * @deprecated As of 3.1 (to be removed in 4.0). * @since 3.1 */ @Deprecated public class MultivariateDifferentiableVectorMultiStartOptimizer extends BaseMultivariateVectorMultiStartOptimizer implements MultivariateDifferentiableVectorOptimizer { /** * Create a multi-start optimizer from a single-start optimizer. * * @param optimizer Single-start optimizer to wrap. * @param starts Number of starts to perform (including the * first one), multi-start is disabled if value is less than or * equal to 1. * @param generator Random vector generator to use for restarts. */ public MultivariateDifferentiableVectorMultiStartOptimizer( final MultivariateDifferentiableVectorOptimizer optimizer, final int starts, final RandomVectorGenerator generator) { super(optimizer, starts, generator); } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/optimization/Target.java100644 1750 1750 3174 12126627713 27347 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.optimization; /** * Target of the optimization procedure. * They are the values which the objective vector function must reproduce * When the parameters of the model have been optimized. *
                            * Immutable class. * * @version $Id: Target.java 1422230 2012-12-15 12:11:13Z erans $ * @deprecated As of 3.1 (to be removed in 4.0). * @since 3.1 */ @Deprecated public class Target implements OptimizationData { /** Target values (of the objective vector function). */ private final double[] target; /** * @param observations Target values. */ public Target(double[] observations) { target = observations.clone(); } /** * Gets the initial guess. * * @return the initial guess. */ public double[] getTarget() { return target.clone(); } } ././@LongLink100644 0 0 165 12126630646 10263 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/optimization/BaseMultivariateSimpleBoundsOptimizer.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/optimization/BaseMultivariateSimpleBoun100644 1750 1750 6047 12126627713 32442 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.optimization; import org.apache.commons.math3.analysis.MultivariateFunction; /** * This interface is mainly intended to enforce the internal coherence of * Commons-FastMath. Users of the API are advised to base their code on * the following interfaces: *
                              *
                            • {@link org.apache.commons.math3.optimization.MultivariateOptimizer}
                            • *
                            • {@link org.apache.commons.math3.optimization.MultivariateDifferentiableOptimizer}
                            • *
                            * * @param Type of the objective function to be optimized. * * @version $Id: BaseMultivariateSimpleBoundsOptimizer.java 1422230 2012-12-15 12:11:13Z erans $ * @deprecated As of 3.1 (to be removed in 4.0). * @since 3.0 */ @Deprecated public interface BaseMultivariateSimpleBoundsOptimizer extends BaseMultivariateOptimizer { /** * Optimize an objective function. * * @param f Objective function. * @param goalType Type of optimization goal: either * {@link GoalType#MAXIMIZE} or {@link GoalType#MINIMIZE}. * @param startPoint Start point for optimization. * @param maxEval Maximum number of function evaluations. * @param lowerBound Lower bound for each of the parameters. * @param upperBound Upper bound for each of the parameters. * @return the point/value pair giving the optimal value for objective * function. * @throws org.apache.commons.math3.exception.DimensionMismatchException * if the array sizes are wrong. * @throws org.apache.commons.math3.exception.TooManyEvaluationsException * if the maximal number of evaluations is exceeded. * @throws org.apache.commons.math3.exception.NullArgumentException if * {@code f}, {@code goalType} or {@code startPoint} is {@code null}. * @throws org.apache.commons.math3.exception.NumberIsTooSmallException if any * of the initial values is less than its lower bound. * @throws org.apache.commons.math3.exception.NumberIsTooLargeException if any * of the initial values is greater than its upper bound. */ PointValuePair optimize(int maxEval, FUNC f, GoalType goalType, double[] startPoint, double[] lowerBound, double[] upperBound); } ././@LongLink100644 0 0 145 12126630646 10261 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/optimization/MultivariateOptimizer.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/optimization/MultivariateOptimizer.java100644 1750 1750 2775 12126627713 32500 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.optimization; import org.apache.commons.math3.analysis.MultivariateFunction; /** * This interface represents an optimization algorithm for {@link MultivariateFunction * scalar objective functions}. *

                            Optimization algorithms find the input point set that either {@link GoalType * maximize or minimize} an objective function.

                            * * @see MultivariateDifferentiableOptimizer * @see MultivariateDifferentiableVectorOptimizer * @version $Id: MultivariateOptimizer.java 1422230 2012-12-15 12:11:13Z erans $ * @deprecated As of 3.1 (to be removed in 4.0). * @since 2.0 */ @Deprecated public interface MultivariateOptimizer extends BaseMultivariateOptimizer {} commons-math3-3.2-src/src/main/java/org/apache/commons/math3/optimization/Weight.java100644 1750 1750 4266 12126627713 27353 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.optimization; import org.apache.commons.math3.linear.RealMatrix; import org.apache.commons.math3.linear.DiagonalMatrix; import org.apache.commons.math3.linear.NonSquareMatrixException; /** * Weight matrix of the residuals between model and observations. *
                            * Immutable class. * * @version $Id: Weight.java 1426759 2012-12-29 13:26:44Z erans $ * @deprecated As of 3.1 (to be removed in 4.0). * @since 3.1 */ @Deprecated public class Weight implements OptimizationData { /** Weight matrix. */ private final RealMatrix weightMatrix; /** * Creates a diagonal weight matrix. * * @param weight List of the values of the diagonal. */ public Weight(double[] weight) { weightMatrix = new DiagonalMatrix(weight); } /** * @param weight Weight matrix. * @throws NonSquareMatrixException if the argument is not * a square matrix. */ public Weight(RealMatrix weight) { if (weight.getColumnDimension() != weight.getRowDimension()) { throw new NonSquareMatrixException(weight.getColumnDimension(), weight.getRowDimension()); } weightMatrix = weight.copy(); } /** * Gets the initial guess. * * @return the initial guess. */ public RealMatrix getWeight() { return weightMatrix.copy(); } } ././@LongLink100644 0 0 171 12126630646 10260 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/optimization/DifferentiableMultivariateVectorOptimizer.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/optimization/DifferentiableMultivariate100644 1750 1750 2627 12126627713 32475 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.optimization; import org.apache.commons.math3.analysis.DifferentiableMultivariateVectorFunction; /** * This interface represents an optimization algorithm for * {@link DifferentiableMultivariateVectorFunction vectorial differentiable * objective functions}. * * @version $Id: DifferentiableMultivariateVectorOptimizer.java 1422230 2012-12-15 12:11:13Z erans $ * @deprecated As of 3.1 (to be removed in 4.0). * @since 3.0 */ @Deprecated public interface DifferentiableMultivariateVectorOptimizer extends BaseMultivariateVectorOptimizer {} ././@LongLink100644 0 0 152 12126630646 10257 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/optimization/AbstractConvergenceChecker.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/optimization/AbstractConvergenceChecker100644 1750 1750 6737 12126627713 32420 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.optimization; import org.apache.commons.math3.util.Precision; /** * Base class for all convergence checker implementations. * * @param Type of (point, value) pair. * * @version $Id: AbstractConvergenceChecker.java 1422230 2012-12-15 12:11:13Z erans $ * @deprecated As of 3.1 (to be removed in 4.0). * @since 3.0 */ @Deprecated public abstract class AbstractConvergenceChecker implements ConvergenceChecker { /** * Default relative threshold. * @deprecated in 3.1 (to be removed in 4.0) because this value is too small * to be useful as a default (cf. MATH-798). */ @Deprecated private static final double DEFAULT_RELATIVE_THRESHOLD = 100 * Precision.EPSILON; /** * Default absolute threshold. * @deprecated in 3.1 (to be removed in 4.0) because this value is too small * to be useful as a default (cf. MATH-798). */ @Deprecated private static final double DEFAULT_ABSOLUTE_THRESHOLD = 100 * Precision.SAFE_MIN; /** * Relative tolerance threshold. */ private final double relativeThreshold; /** * Absolute tolerance threshold. */ private final double absoluteThreshold; /** * Build an instance with default thresholds. * @deprecated in 3.1 (to be removed in 4.0). Convergence thresholds are * problem-dependent. As this class is intended for users who want to set * their own convergence criterion instead of relying on an algorithm's * default procedure, they should also set the thresholds appropriately * (cf. MATH-798). */ @Deprecated public AbstractConvergenceChecker() { this.relativeThreshold = DEFAULT_RELATIVE_THRESHOLD; this.absoluteThreshold = DEFAULT_ABSOLUTE_THRESHOLD; } /** * Build an instance with a specified thresholds. * * @param relativeThreshold relative tolerance threshold * @param absoluteThreshold absolute tolerance threshold */ public AbstractConvergenceChecker(final double relativeThreshold, final double absoluteThreshold) { this.relativeThreshold = relativeThreshold; this.absoluteThreshold = absoluteThreshold; } /** * @return the relative threshold. */ public double getRelativeThreshold() { return relativeThreshold; } /** * @return the absolute threshold. */ public double getAbsoluteThreshold() { return absoluteThreshold; } /** * {@inheritDoc} */ public abstract boolean converged(int iteration, PAIR previous, PAIR current); } ././@LongLink100644 0 0 175 12126630646 10264 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/optimization/DifferentiableMultivariateMultiStartOptimizer.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/optimization/DifferentiableMultivariate100644 1750 1750 4572 12126627713 32476 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.optimization; import org.apache.commons.math3.analysis.DifferentiableMultivariateFunction; import org.apache.commons.math3.random.RandomVectorGenerator; /** * Special implementation of the {@link DifferentiableMultivariateOptimizer} * interface adding multi-start features to an existing optimizer. * * This class wraps a classical optimizer to use it several times in * turn with different starting points in order to avoid being trapped * into a local extremum when looking for a global one. * * @version $Id: DifferentiableMultivariateMultiStartOptimizer.java 1422230 2012-12-15 12:11:13Z erans $ * @deprecated As of 3.1 (to be removed in 4.0). * @since 2.0 */ @Deprecated public class DifferentiableMultivariateMultiStartOptimizer extends BaseMultivariateMultiStartOptimizer implements DifferentiableMultivariateOptimizer { /** * Create a multi-start optimizer from a single-start optimizer. * * @param optimizer Single-start optimizer to wrap. * @param starts Number of starts to perform (including the * first one), multi-start is disabled if value is less than or * equal to 1. * @param generator Random vector generator to use for restarts. */ public DifferentiableMultivariateMultiStartOptimizer(final DifferentiableMultivariateOptimizer optimizer, final int starts, final RandomVectorGenerator generator) { super(optimizer, starts, generator); } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/optimization/SimpleValueChecker.java100644 1750 1750 13104 12126627713 31646 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.optimization; import org.apache.commons.math3.util.FastMath; import org.apache.commons.math3.exception.NotStrictlyPositiveException; /** * Simple implementation of the {@link ConvergenceChecker} interface using * only objective function values. * * Convergence is considered to have been reached if either the relative * difference between the objective function values is smaller than a * threshold or if either the absolute difference between the objective * function values is smaller than another threshold. *
                            * The {@link #converged(int,PointValuePair,PointValuePair) converged} * method will also return {@code true} if the number of iterations has been set * (see {@link #SimpleValueChecker(double,double,int) this constructor}). * * @version $Id: SimpleValueChecker.java 1462503 2013-03-29 15:48:27Z luc $ * @deprecated As of 3.1 (to be removed in 4.0). * @since 3.0 */ @Deprecated public class SimpleValueChecker extends AbstractConvergenceChecker { /** * If {@link #maxIterationCount} is set to this value, the number of * iterations will never cause * {@link #converged(int,PointValuePair,PointValuePair)} * to return {@code true}. */ private static final int ITERATION_CHECK_DISABLED = -1; /** * Number of iterations after which the * {@link #converged(int,PointValuePair,PointValuePair)} method * will return true (unless the check is disabled). */ private final int maxIterationCount; /** * Build an instance with default thresholds. * @deprecated See {@link AbstractConvergenceChecker#AbstractConvergenceChecker()} */ @Deprecated public SimpleValueChecker() { maxIterationCount = ITERATION_CHECK_DISABLED; } /** Build an instance with specified thresholds. * * In order to perform only relative checks, the absolute tolerance * must be set to a negative value. In order to perform only absolute * checks, the relative tolerance must be set to a negative value. * * @param relativeThreshold relative tolerance threshold * @param absoluteThreshold absolute tolerance threshold */ public SimpleValueChecker(final double relativeThreshold, final double absoluteThreshold) { super(relativeThreshold, absoluteThreshold); maxIterationCount = ITERATION_CHECK_DISABLED; } /** * Builds an instance with specified thresholds. * * In order to perform only relative checks, the absolute tolerance * must be set to a negative value. In order to perform only absolute * checks, the relative tolerance must be set to a negative value. * * @param relativeThreshold relative tolerance threshold * @param absoluteThreshold absolute tolerance threshold * @param maxIter Maximum iteration count. * @throws NotStrictlyPositiveException if {@code maxIter <= 0}. * * @since 3.1 */ public SimpleValueChecker(final double relativeThreshold, final double absoluteThreshold, final int maxIter) { super(relativeThreshold, absoluteThreshold); if (maxIter <= 0) { throw new NotStrictlyPositiveException(maxIter); } maxIterationCount = maxIter; } /** * Check if the optimization algorithm has converged considering the * last two points. * This method may be called several time from the same algorithm * iteration with different points. This can be detected by checking the * iteration number at each call if needed. Each time this method is * called, the previous and current point correspond to points with the * same role at each iteration, so they can be compared. As an example, * simplex-based algorithms call this method for all points of the simplex, * not only for the best or worst ones. * * @param iteration Index of current iteration * @param previous Best point in the previous iteration. * @param current Best point in the current iteration. * @return {@code true} if the algorithm has converged. */ @Override public boolean converged(final int iteration, final PointValuePair previous, final PointValuePair current) { if (maxIterationCount != ITERATION_CHECK_DISABLED && iteration >= maxIterationCount) { return true; } final double p = previous.getValue(); final double c = current.getValue(); final double difference = FastMath.abs(p - c); final double size = FastMath.max(FastMath.abs(p), FastMath.abs(c)); return difference <= size * getRelativeThreshold() || difference <= getAbsoluteThreshold(); } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/optimization/SimpleBounds.java100644 1750 1750 3566 12126627713 30532 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.optimization; /** * Simple optimization constraints: lower and upper bounds. * The valid range of the parameters is an interval that can be infinite * (in one or both directions). *
                            * Immutable class. * * @version $Id: SimpleBounds.java 1422230 2012-12-15 12:11:13Z erans $ * @deprecated As of 3.1 (to be removed in 4.0). * @since 3.1 */ @Deprecated public class SimpleBounds implements OptimizationData { /** Lower bounds. */ private final double[] lower; /** Upper bounds. */ private final double[] upper; /** * @param lB Lower bounds. * @param uB Upper bounds. */ public SimpleBounds(double[] lB, double[] uB) { lower = lB.clone(); upper = uB.clone(); } /** * Gets the lower bounds. * * @return the initial guess. */ public double[] getLower() { return lower.clone(); } /** * Gets the lower bounds. * * @return the initial guess. */ public double[] getUpper() { return upper.clone(); } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/ode/package-info.java100644 1750 1750 21307 12126627716 26507 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ /** * *

                            * This package provides classes to solve Ordinary Differential Equations problems. *

                            * *

                            * This package solves Initial Value Problems of the form * y'=f(t,y) with t0 and * y(t0)=y0 known. The provided * integrators compute an estimate of y(t) from * t=t0 to t=t1. * It is also possible to get thederivatives with respect to the initial state * dy(t)/dy(t0) or the derivatives with * respect to some ODE parameters dy(t)/dp. *

                            * *

                            * All integrators provide dense output. This means that besides * computing the state vector at discrete times, they also provide a * cheap mean to get the state between the time steps. They do so through * classes extending the {@link * org.apache.commons.math3.ode.sampling.StepInterpolator StepInterpolator} * abstract class, which are made available to the user at the end of * each step. *

                            * *

                            * All integrators handle multiple discrete events detection based on switching * functions. This means that the integrator can be driven by user specified * discrete events. The steps are shortened as needed to ensure the events occur * at step boundaries (even if the integrator is a fixed-step * integrator). When the events are triggered, integration can be stopped * (this is called a G-stop facility), the state vector can be changed, * or integration can simply go on. The latter case is useful to handle * discontinuities in the differential equations gracefully and get * accurate dense output even close to the discontinuity. *

                            * *

                            * The user should describe his problem in his own classes * (UserProblem in the diagram below) which should implement * the {@link org.apache.commons.math3.ode.FirstOrderDifferentialEquations * FirstOrderDifferentialEquations} interface. Then he should pass it to * the integrator he prefers among all the classes that implement the * {@link org.apache.commons.math3.ode.FirstOrderIntegrator * FirstOrderIntegrator} interface. *

                            * *

                            * The solution of the integration problem is provided by two means. The * first one is aimed towards simple use: the state vector at the end of * the integration process is copied in the y array of the * {@link org.apache.commons.math3.ode.FirstOrderIntegrator#integrate * FirstOrderIntegrator.integrate} method. The second one should be used * when more in-depth information is needed throughout the integration * process. The user can register an object implementing the {@link * org.apache.commons.math3.ode.sampling.StepHandler StepHandler} interface or a * {@link org.apache.commons.math3.ode.sampling.StepNormalizer StepNormalizer} * object wrapping a user-specified object implementing the {@link * org.apache.commons.math3.ode.sampling.FixedStepHandler FixedStepHandler} * interface into the integrator before calling the {@link * org.apache.commons.math3.ode.FirstOrderIntegrator#integrate * FirstOrderIntegrator.integrate} method. The user object will be called * appropriately during the integration process, allowing the user to * process intermediate results. The default step handler does nothing. *

                            * *

                            * {@link org.apache.commons.math3.ode.ContinuousOutputModel * ContinuousOutputModel} is a special-purpose step handler that is able * to store all steps and to provide transparent access to any * intermediate result once the integration is over. An important feature * of this class is that it implements the Serializable * interface. This means that a complete continuous model of the * integrated function throughout the integration range can be serialized * and reused later (if stored into a persistent medium like a filesystem * or a database) or elsewhere (if sent to another application). Only the * result of the integration is stored, there is no reference to the * integrated problem by itself. *

                            * *

                            * Other default implementations of the {@link * org.apache.commons.math3.ode.sampling.StepHandler StepHandler} interface are * available for general needs ({@link * org.apache.commons.math3.ode.sampling.DummyStepHandler DummyStepHandler}, {@link * org.apache.commons.math3.ode.sampling.StepNormalizer StepNormalizer}) and custom * implementations can be developed for specific needs. As an example, * if an application is to be completely driven by the integration * process, then most of the application code will be run inside a step * handler specific to this application. *

                            * *

                            * Some integrators (the simple ones) use fixed steps that are set at * creation time. The more efficient integrators use variable steps that * are handled internally in order to control the integration error with * respect to a specified accuracy (these integrators extend the {@link * org.apache.commons.math3.ode.nonstiff.AdaptiveStepsizeIntegrator * AdaptiveStepsizeIntegrator} abstract class). In this case, the step * handler which is called after each successful step shows up the * variable stepsize. The {@link * org.apache.commons.math3.ode.sampling.StepNormalizer StepNormalizer} class can * be used to convert the variable stepsize into a fixed stepsize that * can be handled by classes implementing the {@link * org.apache.commons.math3.ode.sampling.FixedStepHandler FixedStepHandler} * interface. Adaptive stepsize integrators can automatically compute the * initial stepsize by themselves, however the user can specify it if he * prefers to retain full control over the integration or if the * automatic guess is wrong. *

                            * *

                            * * * * * * * * *
                            Fixed Step Integrators
                            NameOrder
                            {@link org.apache.commons.math3.ode.nonstiff.EulerIntegrator Euler}1
                            {@link org.apache.commons.math3.ode.nonstiff.MidpointIntegrator Midpoint}2
                            {@link org.apache.commons.math3.ode.nonstiff.ClassicalRungeKuttaIntegrator Classical Runge-Kutta}4
                            {@link org.apache.commons.math3.ode.nonstiff.GillIntegrator Gill}4
                            {@link org.apache.commons.math3.ode.nonstiff.ThreeEighthesIntegrator 3/8}4
                            *

                            * * * * * * * * * * *
                            Adaptive Stepsize Integrators
                            NameIntegration OrderError Estimation Order
                            {@link org.apache.commons.math3.ode.nonstiff.HighamHall54Integrator Higham and Hall}54
                            {@link org.apache.commons.math3.ode.nonstiff.DormandPrince54Integrator Dormand-Prince 5(4)}54
                            {@link org.apache.commons.math3.ode.nonstiff.DormandPrince853Integrator Dormand-Prince 8(5,3)}85 and 3
                            {@link org.apache.commons.math3.ode.nonstiff.GraggBulirschStoerIntegrator Gragg-Bulirsch-Stoer}variable (up to 18 by default)variable
                            {@link org.apache.commons.math3.ode.nonstiff.AdamsBashforthIntegrator Adams-Bashforth}variablevariable
                            {@link org.apache.commons.math3.ode.nonstiff.AdamsMoultonIntegrator Adams-Moulton}variablevariable
                            *

                            * *

                            * In the table above, the {@link org.apache.commons.math3.ode.nonstiff.AdamsBashforthIntegrator * Adams-Bashforth} and {@link org.apache.commons.math3.ode.nonstiff.AdamsMoultonIntegrator * Adams-Moulton} integrators appear as variable-step ones. This is an experimental extension * to the classical algorithms using the Nordsieck vector representation. *

                            * * */ package org.apache.commons.math3.ode; commons-math3-3.2-src/src/main/java/org/apache/commons/math3/ode/events/package-info.java100644 1750 1750 7137 12126627715 27777 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ /** * *

                            * This package provides classes to handle discrete events occurring during * Ordinary Differential Equations integration. *

                            * *

                            * Discrete events detection is based on switching functions. The user provides * a simple {@link org.apache.commons.math3.ode.events.EventHandler#g g(t, y)} * function depending on the current time and state. The integrator will monitor * the value of the function throughout integration range and will trigger the * event when its sign changes. The magnitude of the value is almost irrelevant, * it should however be continuous (but not necessarily smooth) for the sake of * root finding. The steps are shortened as needed to ensure the events occur * at step boundaries (even if the integrator is a fixed-step integrator). *

                            * *

                            * When an event is triggered, several different options are available: *

                            *
                              *
                            • integration can be stopped (this is called a G-stop facility),
                            • *
                            • the state vector or the derivatives can be changed,
                            • *
                            • or integration can simply go on.
                            • *
                            * *

                            * The first case, G-stop, is the most common one. A typical use case is when an * ODE must be solved up to some target state is reached, with a known value of * the state but an unknown occurrence time. As an example, if we want to monitor * a chemical reaction up to some predefined concentration for the first substance, * we can use the following switching function setting: *

                             *  public double g(double t, double[] y) {
                             *    return y[0] - targetConcentration;
                             *  }
                             *
                             *  public int eventOccurred(double t, double[] y) {
                             *    return STOP;
                             *  }
                             * 
                            *

                            * *

                            * The second case, change state vector or derivatives is encountered when dealing * with discontinuous dynamical models. A typical case would be the motion of a * spacecraft when thrusters are fired for orbital maneuvers. The acceleration is * smooth as long as no maneuver are performed, depending only on gravity, drag, * third body attraction, radiation pressure. Firing a thruster introduces a * discontinuity that must be handled appropriately by the integrator. In such a case, * we would use a switching function setting similar to this: *

                             *  public double g(double t, double[] y) {
                             *    return (t - tManeuverStart) ∗ (t - tManeuverStop);
                             *  }
                             *
                             *  public int eventOccurred(double t, double[] y) {
                             *    return RESET_DERIVATIVES;
                             *  }
                             * 
                            *

                            * *

                            * The third case is useful mainly for monitoring purposes, a simple example is: *

                             *  public double g(double t, double[] y) {
                             *    return y[0] - y[1];
                             *  }
                             *
                             *  public int eventOccurred(double t, double[] y) {
                             *    logger.log("y0(t) and y1(t) curves cross at t = " + t);
                             *    return CONTINUE;
                             *  }
                             * 
                            *

                            * * */ package org.apache.commons.math3.ode.events; commons-math3-3.2-src/src/main/java/org/apache/commons/math3/ode/events/Transformer.java100644 1750 1750 6436 12126627715 27756 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.ode.events; import org.apache.commons.math3.util.FastMath; import org.apache.commons.math3.util.Precision; /** Transformer for {@link EventHandler#g(double, double[]) g functions}. * @see EventFilter * @see FilterType * @version $Id: Transformer.java 1458298 2013-03-19 14:09:58Z luc $ * @since 3.2 */ enum Transformer { /** Transformer computing transformed = 0. *

                            * This transformer is used when we initialize the filter, until we get at * least one non-zero value to select the proper transformer. *

                            */ UNINITIALIZED { /** {@inheritDoc} */ protected double transformed(final double g) { return 0; } }, /** Transformer computing transformed = g. *

                            * When this transformer is applied, the roots of the original function * are preserved, with the same {@code increasing/decreasing} status. *

                            */ PLUS { /** {@inheritDoc} */ protected double transformed(final double g) { return g; } }, /** Transformer computing transformed = -g. *

                            * When this transformer is applied, the roots of the original function * are preserved, with reversed {@code increasing/decreasing} status. *

                            */ MINUS { /** {@inheritDoc} */ protected double transformed(final double g) { return -g; } }, /** Transformer computing transformed = min(-{@link Precision#SAFE_MIN}, -g, +g). *

                            * When this transformer is applied, the transformed function is * guaranteed to be always strictly negative (i.e. there are no roots). *

                            */ MIN { /** {@inheritDoc} */ protected double transformed(final double g) { return FastMath.min(-Precision.SAFE_MIN, FastMath.min(-g, +g)); } }, /** Transformer computing transformed = max(+{@link Precision#SAFE_MIN}, -g, +g). *

                            * When this transformer is applied, the transformed function is * guaranteed to be always strictly positive (i.e. there are no roots). *

                            */ MAX { /** {@inheritDoc} */ protected double transformed(final double g) { return FastMath.max(+Precision.SAFE_MIN, FastMath.max(-g, +g)); } }; /** Transform value of function g. * @param g raw value of function g * @return transformed value of function g */ protected abstract double transformed(double g); } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/ode/events/EventFilter.java100644 1750 1750 17757 12126627715 27733 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.ode.events; import java.util.Arrays; /** Wrapper used to detect only increasing or decreasing events. * *

                            General {@link EventHandler events} are defined implicitely * by a {@link EventHandler#g(double, double[]) g function} crossing * zero. This function needs to be continuous in the event neighborhood, * and its sign must remain consistent between events. This implies that * during an ODE integration, events triggered are alternately events * for which the function increases from negative to positive values, * and events for which the function decreases from positive to * negative values. *

                            * *

                            Sometimes, users are only interested in one type of event (say * increasing events for example) and not in the other type. In these * cases, looking precisely for all events location and triggering * events that will later be ignored is a waste of computing time.

                            * *

                            Users can wrap a regular {@link EventHandler event handler} in * an instance of this class and provide this wrapping instance to * the {@link org.apache.commons.math3.ode.FirstOrderIntegrator ODE solver} * in order to avoid wasting time looking for uninteresting events. * The wrapper will intercept the calls to the {@link * EventHandler#g(double, double[]) g function} and to the {@link * EventHandler#eventOccurred(double, double[], boolean) * eventOccurred} method in order to ignore uninteresting events. The * wrapped regular {@link EventHandler event handler} will the see only * the interesting events, i.e. either only {@code increasing} events or * {@code decreasing} events. the number of calls to the {@link * EventHandler#g(double, double[]) g function} will also be reduced.

                            * * @version $Id: EventFilter.java 1458491 2013-03-19 20:13:11Z luc $ * @since 3.2 */ public class EventFilter implements EventHandler { /** Number of past transformers updates stored. */ private static final int HISTORY_SIZE = 100; /** Wrapped event handler. */ private final EventHandler rawHandler; /** Filter to use. */ private final FilterType filter; /** Transformers of the g function. */ private final Transformer[] transformers; /** Update time of the transformers. */ private final double[] updates; /** Indicator for forward integration. */ private boolean forward; /** Extreme time encountered so far. */ private double extremeT; /** Wrap an {@link EventHandler event handler}. * @param rawHandler event handler to wrap * @param filter filter to use */ public EventFilter(final EventHandler rawHandler, final FilterType filter) { this.rawHandler = rawHandler; this.filter = filter; this.transformers = new Transformer[HISTORY_SIZE]; this.updates = new double[HISTORY_SIZE]; } /** {@inheritDoc} */ public void init(double t0, double[] y0, double t) { // delegate to raw handler rawHandler.init(t0, y0, t); // initialize events triggering logic forward = t >= t0; extremeT = forward ? Double.NEGATIVE_INFINITY : Double.POSITIVE_INFINITY; Arrays.fill(transformers, Transformer.UNINITIALIZED); Arrays.fill(updates, extremeT); } /** {@inheritDoc} */ public double g(double t, double[] y) { final double rawG = rawHandler.g(t, y); // search which transformer should be applied to g if (forward) { final int last = transformers.length - 1; if (extremeT < t) { // we are at the forward end of the history // check if a new rough root has been crossed final Transformer previous = transformers[last]; final Transformer next = filter.selectTransformer(previous, rawG, forward); if (next != previous) { // there is a root somewhere between extremeT end t // the new transformer, which is valid on both sides of the root, // so it is valid for t (this is how we have just computed it above), // but it was already valid before, so we store the switch at extremeT // for safety, to ensure the previous transformer is not applied too // close of the root System.arraycopy(updates, 1, updates, 0, last); System.arraycopy(transformers, 1, transformers, 0, last); updates[last] = extremeT; transformers[last] = next; } extremeT = t; // apply the transform return next.transformed(rawG); } else { // we are in the middle of the history // select the transformer for (int i = last; i > 0; --i) { if (updates[i] <= t) { // apply the transform return transformers[i].transformed(rawG); } } return transformers[0].transformed(rawG); } } else { if (t < extremeT) { // we are at the backward end of the history // check if a new rough root has been crossed final Transformer previous = transformers[0]; final Transformer next = filter.selectTransformer(previous, rawG, forward); if (next != previous) { // there is a root somewhere between extremeT end t // the new transformer, which is valid on both sides of the root, // so it is valid for t (this is how we have just computed it above), // but it was already valid before, so we store the switch at extremeT // for safety, to ensure the previous transformer is not applied too // close of the root System.arraycopy(updates, 0, updates, 1, updates.length - 1); System.arraycopy(transformers, 0, transformers, 1, transformers.length - 1); updates[0] = extremeT; transformers[0] = next; } extremeT = t; // apply the transform return next.transformed(rawG); } else { // we are in the middle of the history // select the transformer for (int i = 0; i < updates.length - 1; ++i) { if (t <= updates[i]) { // apply the transform return transformers[i].transformed(rawG); } } return transformers[updates.length - 1].transformed(rawG); } } } /** {@inheritDoc} */ public Action eventOccurred(double t, double[] y, boolean increasing) { // delegate to raw handler, fixing increasing status on the fly return rawHandler.eventOccurred(t, y, filter.getTriggeredIncreasing()); } /** {@inheritDoc} */ public void resetState(double t, double[] y) { // delegate to raw handler rawHandler.resetState(t, y); } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/ode/events/EventState.java100644 1750 1750 37064 12126627715 27557 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.ode.events; import org.apache.commons.math3.analysis.UnivariateFunction; import org.apache.commons.math3.analysis.solvers.AllowedSolution; import org.apache.commons.math3.analysis.solvers.BracketedUnivariateSolver; import org.apache.commons.math3.analysis.solvers.PegasusSolver; import org.apache.commons.math3.analysis.solvers.UnivariateSolver; import org.apache.commons.math3.analysis.solvers.UnivariateSolverUtils; import org.apache.commons.math3.exception.MaxCountExceededException; import org.apache.commons.math3.exception.NoBracketingException; import org.apache.commons.math3.ode.sampling.StepInterpolator; import org.apache.commons.math3.util.FastMath; /** This class handles the state for one {@link EventHandler * event handler} during integration steps. * *

                            Each time the integrator proposes a step, the event handler * switching function should be checked. This class handles the state * of one handler during one integration step, with references to the * state at the end of the preceding step. This information is used to * decide if the handler should trigger an event or not during the * proposed step.

                            * * @version $Id: EventState.java 1416643 2012-12-03 19:37:14Z tn $ * @since 1.2 */ public class EventState { /** Event handler. */ private final EventHandler handler; /** Maximal time interval between events handler checks. */ private final double maxCheckInterval; /** Convergence threshold for event localization. */ private final double convergence; /** Upper limit in the iteration count for event localization. */ private final int maxIterationCount; /** Time at the beginning of the step. */ private double t0; /** Value of the events handler at the beginning of the step. */ private double g0; /** Simulated sign of g0 (we cheat when crossing events). */ private boolean g0Positive; /** Indicator of event expected during the step. */ private boolean pendingEvent; /** Occurrence time of the pending event. */ private double pendingEventTime; /** Occurrence time of the previous event. */ private double previousEventTime; /** Integration direction. */ private boolean forward; /** Variation direction around pending event. * (this is considered with respect to the integration direction) */ private boolean increasing; /** Next action indicator. */ private EventHandler.Action nextAction; /** Root-finding algorithm to use to detect state events. */ private final UnivariateSolver solver; /** Simple constructor. * @param handler event handler * @param maxCheckInterval maximal time interval between switching * function checks (this interval prevents missing sign changes in * case the integration steps becomes very large) * @param convergence convergence threshold in the event time search * @param maxIterationCount upper limit of the iteration count in * the event time search * @param solver Root-finding algorithm to use to detect state events */ public EventState(final EventHandler handler, final double maxCheckInterval, final double convergence, final int maxIterationCount, final UnivariateSolver solver) { this.handler = handler; this.maxCheckInterval = maxCheckInterval; this.convergence = FastMath.abs(convergence); this.maxIterationCount = maxIterationCount; this.solver = solver; // some dummy values ... t0 = Double.NaN; g0 = Double.NaN; g0Positive = true; pendingEvent = false; pendingEventTime = Double.NaN; previousEventTime = Double.NaN; increasing = true; nextAction = EventHandler.Action.CONTINUE; } /** Get the underlying event handler. * @return underlying event handler */ public EventHandler getEventHandler() { return handler; } /** Get the maximal time interval between events handler checks. * @return maximal time interval between events handler checks */ public double getMaxCheckInterval() { return maxCheckInterval; } /** Get the convergence threshold for event localization. * @return convergence threshold for event localization */ public double getConvergence() { return convergence; } /** Get the upper limit in the iteration count for event localization. * @return upper limit in the iteration count for event localization */ public int getMaxIterationCount() { return maxIterationCount; } /** Reinitialize the beginning of the step. * @param interpolator valid for the current step * @exception MaxCountExceededException if the interpolator throws one because * the number of functions evaluations is exceeded */ public void reinitializeBegin(final StepInterpolator interpolator) throws MaxCountExceededException { t0 = interpolator.getPreviousTime(); interpolator.setInterpolatedTime(t0); g0 = handler.g(t0, interpolator.getInterpolatedState()); if (g0 == 0) { // excerpt from MATH-421 issue: // If an ODE solver is setup with an EventHandler that return STOP // when the even is triggered, the integrator stops (which is exactly // the expected behavior). If however the user wants to restart the // solver from the final state reached at the event with the same // configuration (expecting the event to be triggered again at a // later time), then the integrator may fail to start. It can get stuck // at the previous event. The use case for the bug MATH-421 is fairly // general, so events occurring exactly at start in the first step should // be ignored. // extremely rare case: there is a zero EXACTLY at interval start // we will use the sign slightly after step beginning to force ignoring this zero final double epsilon = FastMath.max(solver.getAbsoluteAccuracy(), FastMath.abs(solver.getRelativeAccuracy() * t0)); final double tStart = t0 + 0.5 * epsilon; interpolator.setInterpolatedTime(tStart); g0 = handler.g(tStart, interpolator.getInterpolatedState()); } g0Positive = g0 >= 0; } /** Evaluate the impact of the proposed step on the event handler. * @param interpolator step interpolator for the proposed step * @return true if the event handler triggers an event before * the end of the proposed step * @exception MaxCountExceededException if the interpolator throws one because * the number of functions evaluations is exceeded * @exception NoBracketingException if the event cannot be bracketed */ public boolean evaluateStep(final StepInterpolator interpolator) throws MaxCountExceededException, NoBracketingException { try { forward = interpolator.isForward(); final double t1 = interpolator.getCurrentTime(); final double dt = t1 - t0; if (FastMath.abs(dt) < convergence) { // we cannot do anything on such a small step, don't trigger any events return false; } final int n = FastMath.max(1, (int) FastMath.ceil(FastMath.abs(dt) / maxCheckInterval)); final double h = dt / n; final UnivariateFunction f = new UnivariateFunction() { public double value(final double t) throws LocalMaxCountExceededException { try { interpolator.setInterpolatedTime(t); return handler.g(t, interpolator.getInterpolatedState()); } catch (MaxCountExceededException mcee) { throw new LocalMaxCountExceededException(mcee); } } }; double ta = t0; double ga = g0; for (int i = 0; i < n; ++i) { // evaluate handler value at the end of the substep final double tb = t0 + (i + 1) * h; interpolator.setInterpolatedTime(tb); final double gb = handler.g(tb, interpolator.getInterpolatedState()); // check events occurrence if (g0Positive ^ (gb >= 0)) { // there is a sign change: an event is expected during this step // variation direction, with respect to the integration direction increasing = gb >= ga; // find the event time making sure we select a solution just at or past the exact root final double root; if (solver instanceof BracketedUnivariateSolver) { @SuppressWarnings("unchecked") BracketedUnivariateSolver bracketing = (BracketedUnivariateSolver) solver; root = forward ? bracketing.solve(maxIterationCount, f, ta, tb, AllowedSolution.RIGHT_SIDE) : bracketing.solve(maxIterationCount, f, tb, ta, AllowedSolution.LEFT_SIDE); } else { final double baseRoot = forward ? solver.solve(maxIterationCount, f, ta, tb) : solver.solve(maxIterationCount, f, tb, ta); final int remainingEval = maxIterationCount - solver.getEvaluations(); BracketedUnivariateSolver bracketing = new PegasusSolver(solver.getRelativeAccuracy(), solver.getAbsoluteAccuracy()); root = forward ? UnivariateSolverUtils.forceSide(remainingEval, f, bracketing, baseRoot, ta, tb, AllowedSolution.RIGHT_SIDE) : UnivariateSolverUtils.forceSide(remainingEval, f, bracketing, baseRoot, tb, ta, AllowedSolution.LEFT_SIDE); } if ((!Double.isNaN(previousEventTime)) && (FastMath.abs(root - ta) <= convergence) && (FastMath.abs(root - previousEventTime) <= convergence)) { // we have either found nothing or found (again ?) a past event, // retry the substep excluding this value ta = forward ? ta + convergence : ta - convergence; ga = f.value(ta); --i; } else if (Double.isNaN(previousEventTime) || (FastMath.abs(previousEventTime - root) > convergence)) { pendingEventTime = root; pendingEvent = true; return true; } else { // no sign change: there is no event for now ta = tb; ga = gb; } } else { // no sign change: there is no event for now ta = tb; ga = gb; } } // no event during the whole step pendingEvent = false; pendingEventTime = Double.NaN; return false; } catch (LocalMaxCountExceededException lmcee) { throw lmcee.getException(); } } /** Get the occurrence time of the event triggered in the current step. * @return occurrence time of the event triggered in the current * step or infinity if no events are triggered */ public double getEventTime() { return pendingEvent ? pendingEventTime : (forward ? Double.POSITIVE_INFINITY : Double.NEGATIVE_INFINITY); } /** Acknowledge the fact the step has been accepted by the integrator. * @param t value of the independent time variable at the * end of the step * @param y array containing the current value of the state vector * at the end of the step */ public void stepAccepted(final double t, final double[] y) { t0 = t; g0 = handler.g(t, y); if (pendingEvent && (FastMath.abs(pendingEventTime - t) <= convergence)) { // force the sign to its value "just after the event" previousEventTime = t; g0Positive = increasing; nextAction = handler.eventOccurred(t, y, !(increasing ^ forward)); } else { g0Positive = g0 >= 0; nextAction = EventHandler.Action.CONTINUE; } } /** Check if the integration should be stopped at the end of the * current step. * @return true if the integration should be stopped */ public boolean stop() { return nextAction == EventHandler.Action.STOP; } /** Let the event handler reset the state if it wants. * @param t value of the independent time variable at the * beginning of the next step * @param y array were to put the desired state vector at the beginning * of the next step * @return true if the integrator should reset the derivatives too */ public boolean reset(final double t, final double[] y) { if (!(pendingEvent && (FastMath.abs(pendingEventTime - t) <= convergence))) { return false; } if (nextAction == EventHandler.Action.RESET_STATE) { handler.resetState(t, y); } pendingEvent = false; pendingEventTime = Double.NaN; return (nextAction == EventHandler.Action.RESET_STATE) || (nextAction == EventHandler.Action.RESET_DERIVATIVES); } /** Local wrapper to propagate exceptions. */ private static class LocalMaxCountExceededException extends RuntimeException { /** Serializable UID. */ private static final long serialVersionUID = 20120901L; /** Wrapped exception. */ private final MaxCountExceededException wrapped; /** Simple constructor. * @param exception exception to wrap */ public LocalMaxCountExceededException(final MaxCountExceededException exception) { wrapped = exception; } /** Get the wrapped exception. * @return wrapped exception */ public MaxCountExceededException getException() { return wrapped; } } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/ode/events/FilterType.java100644 1750 1750 46156 12126627715 27566 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.ode.events; import org.apache.commons.math3.exception.MathInternalError; /** Enumerate for {@link EventFilter filtering events}. * * @version $Id: FilterType.java 1458491 2013-03-19 20:13:11Z luc $ * @since 3.2 */ public enum FilterType { /** Constant for triggering only decreasing events. *

                            When this filter is used, the wrapped {@link EventHandler * event handler} {@link EventHandler#eventOccurred(double, double[], * boolean) eventOccurred} method will be called only with * its {@code increasing} argument set to false.

                            */ TRIGGER_ONLY_DECREASING_EVENTS { /** {@inheritDoc} */ protected boolean getTriggeredIncreasing() { return false; } /** {@inheritDoc} *

                            * states scheduling for computing h(t,y) as an altered version of g(t, y) *

                              *
                            • 0 are triggered events for which a zero is produced (here decreasing events)
                            • *
                            • X are ignored events for which zero is masked (here increasing events)
                            • *
                            *

                            *
                                     *  g(t)
                                     *             ___                     ___                     ___
                                     *            /   \                   /   \                   /   \
                                     *           /     \                 /     \                 /     \
                                     *          /  g>0  \               /  g>0  \               /  g>0  \
                                     *         /         \             /         \             /         \
                                     *  ----- X --------- 0 --------- X --------- 0 --------- X --------- 0 ---
                                     *       /             \         /             \         /             \
                                     *      /               \ g<0   /               \  g<0  /               \ g<0
                                     *     /                 \     /                 \     /                 \     /
                                     * ___/                   \___/                   \___/                   \___/
                                     * 
                            *
                                     *  h(t,y)) as an alteration of g(t,y)
                                     *             ___                                 ___         ___
                                     *    \       /   \                               /   \       /   \
                                     *     \     /     \ h=+g                        /     \     /     \
                                     *      \   /       \      h=min(-s,-g,+g)      /       \   /       \
                                     *       \_/         \                         /         \_/         \
                                     *  ------ ---------- 0 ----------_---------- 0 --------------------- 0 ---
                                     *                     \         / \         /                         \
                                     *   h=max(+s,-g,+g)    \       /   \       /       h=max(+s,-g,+g)     \
                                     *                       \     /     \     / h=-g                        \     /
                                     *                        \___/       \___/                               \___/
                                     * 
                            *

                            * As shown by the figure above, several expressions are used to compute h, * depending on the current state: *

                              *
                            • h = max(+s,-g,+g)
                            • *
                            • h = +g
                            • *
                            • h = min(-s,-g,+g)
                            • *
                            • h = -g
                            • *
                            * where s is a tiny positive value: {@link org.apache.commons.math3.util.Precision#SAFE_MIN}. *

                            */ protected Transformer selectTransformer(final Transformer previous, final double g, final boolean forward) { if (forward) { switch (previous) { case UNINITIALIZED : // we are initializing the first point if (g > 0) { // initialize as if previous root (i.e. backward one) was an ignored increasing event return Transformer.MAX; } else if (g < 0) { // initialize as if previous root (i.e. backward one) was a triggered decreasing event return Transformer.PLUS; } else { // we are exactly at a root, we don't know if it is an increasing // or a decreasing event, we remain in uninitialized state return Transformer.UNINITIALIZED; } case PLUS : if (g >= 0) { // we have crossed the zero line on an ignored increasing event, // we must change the transformer return Transformer.MIN; } else { // we are still in the same status return previous; } case MINUS : if (g >= 0) { // we have crossed the zero line on an ignored increasing event, // we must change the transformer return Transformer.MAX; } else { // we are still in the same status return previous; } case MIN : if (g <= 0) { // we have crossed the zero line on a triggered decreasing event, // we must change the transformer return Transformer.MINUS; } else { // we are still in the same status return previous; } case MAX : if (g <= 0) { // we have crossed the zero line on a triggered decreasing event, // we must change the transformer return Transformer.PLUS; } else { // we are still in the same status return previous; } default : // this should never happen throw new MathInternalError(); } } else { switch (previous) { case UNINITIALIZED : // we are initializing the first point if (g > 0) { // initialize as if previous root (i.e. forward one) was a triggered decreasing event return Transformer.MINUS; } else if (g < 0) { // initialize as if previous root (i.e. forward one) was an ignored increasing event return Transformer.MIN; } else { // we are exactly at a root, we don't know if it is an increasing // or a decreasing event, we remain in uninitialized state return Transformer.UNINITIALIZED; } case PLUS : if (g <= 0) { // we have crossed the zero line on an ignored increasing event, // we must change the transformer return Transformer.MAX; } else { // we are still in the same status return previous; } case MINUS : if (g <= 0) { // we have crossed the zero line on an ignored increasing event, // we must change the transformer return Transformer.MIN; } else { // we are still in the same status return previous; } case MIN : if (g >= 0) { // we have crossed the zero line on a triggered decreasing event, // we must change the transformer return Transformer.PLUS; } else { // we are still in the same status return previous; } case MAX : if (g >= 0) { // we have crossed the zero line on a triggered decreasing event, // we must change the transformer return Transformer.MINUS; } else { // we are still in the same status return previous; } default : // this should never happen throw new MathInternalError(); } } } }, /** Constant for triggering only increasing events. *

                            When this filter is used, the wrapped {@link EventHandler * event handler} {@link EventHandler#eventOccurred(double, double[], * boolean) eventOccurred} method will be called only with * its {@code increasing} argument set to true.

                            */ TRIGGER_ONLY_INCREASING_EVENTS { /** {@inheritDoc} */ protected boolean getTriggeredIncreasing() { return true; } /** {@inheritDoc} *

                            * states scheduling for computing h(t,y) as an altered version of g(t, y) *

                              *
                            • 0 are triggered events for which a zero is produced (here increasing events)
                            • *
                            • X are ignored events for which zero is masked (here decreasing events)
                            • *
                            *

                            *
                                     *  g(t)
                                     *             ___                     ___                     ___
                                     *            /   \                   /   \                   /   \
                                     *           /     \                 /     \                 /     \
                                     *          /  g>0  \               /  g>0  \               /  g>0  \
                                     *         /         \             /         \             /         \
                                     *  ----- 0 --------- X --------- 0 --------- X --------- 0 --------- X ---
                                     *       /             \         /             \         /             \
                                     *      /               \ g<0   /               \  g<0  /               \ g<0
                                     *     /                 \     /                 \     /                 \     /
                                     * ___/                   \___/                   \___/                   \___/
                                     * 
                            *
                                     *  h(t,y)) as an alteration of g(t,y)
                                     *                                     ___         ___
                                     *    \                               /   \       /   \
                                     *     \ h=-g                        /     \     /     \ h=-g
                                     *      \      h=min(-s,-g,+g)      /       \   /       \      h=min(-s,-g,+g)
                                     *       \                         /         \_/         \
                                     *  ------0 ----------_---------- 0 --------------------- 0 --------- _ ---
                                     *         \         / \         /                         \         / \
                                     *          \       /   \       /       h=max(+s,-g,+g)     \       /   \
                                     *           \     /     \     / h=+g                        \     /     \     /
                                     *            \___/       \___/                               \___/       \___/
                                     * 
                            *

                            * As shown by the figure above, several expressions are used to compute h, * depending on the current state: *

                              *
                            • h = max(+s,-g,+g)
                            • *
                            • h = +g
                            • *
                            • h = min(-s,-g,+g)
                            • *
                            • h = -g
                            • *
                            * where s is a tiny positive value: {@link org.apache.commons.math3.util.Precision#SAFE_MIN}. *

                            */ protected Transformer selectTransformer(final Transformer previous, final double g, final boolean forward) { if (forward) { switch (previous) { case UNINITIALIZED : // we are initializing the first point if (g > 0) { // initialize as if previous root (i.e. backward one) was a triggered increasing event return Transformer.PLUS; } else if (g < 0) { // initialize as if previous root (i.e. backward one) was an ignored decreasing event return Transformer.MIN; } else { // we are exactly at a root, we don't know if it is an increasing // or a decreasing event, we remain in uninitialized state return Transformer.UNINITIALIZED; } case PLUS : if (g <= 0) { // we have crossed the zero line on an ignored decreasing event, // we must change the transformer return Transformer.MAX; } else { // we are still in the same status return previous; } case MINUS : if (g <= 0) { // we have crossed the zero line on an ignored decreasing event, // we must change the transformer return Transformer.MIN; } else { // we are still in the same status return previous; } case MIN : if (g >= 0) { // we have crossed the zero line on a triggered increasing event, // we must change the transformer return Transformer.PLUS; } else { // we are still in the same status return previous; } case MAX : if (g >= 0) { // we have crossed the zero line on a triggered increasing event, // we must change the transformer return Transformer.MINUS; } else { // we are still in the same status return previous; } default : // this should never happen throw new MathInternalError(); } } else { switch (previous) { case UNINITIALIZED : // we are initializing the first point if (g > 0) { // initialize as if previous root (i.e. forward one) was an ignored decreasing event return Transformer.MAX; } else if (g < 0) { // initialize as if previous root (i.e. forward one) was a triggered increasing event return Transformer.MINUS; } else { // we are exactly at a root, we don't know if it is an increasing // or a decreasing event, we remain in uninitialized state return Transformer.UNINITIALIZED; } case PLUS : if (g >= 0) { // we have crossed the zero line on an ignored decreasing event, // we must change the transformer return Transformer.MIN; } else { // we are still in the same status return previous; } case MINUS : if (g >= 0) { // we have crossed the zero line on an ignored decreasing event, // we must change the transformer return Transformer.MAX; } else { // we are still in the same status return previous; } case MIN : if (g <= 0) { // we have crossed the zero line on a triggered increasing event, // we must change the transformer return Transformer.MINUS; } else { // we are still in the same status return previous; } case MAX : if (g <= 0) { // we have crossed the zero line on a triggered increasing event, // we must change the transformer return Transformer.PLUS; } else { // we are still in the same status return previous; } default : // this should never happen throw new MathInternalError(); } } } }; /** Get the increasing status of triggered events. * @return true if triggered events are increasing events */ protected abstract boolean getTriggeredIncreasing(); /** Get next function transformer in the specified direction. * @param previous transformer active on the previous point with respect * to integration direction (may be null if no previous point is known) * @param g current value of the g function * @param forward true if integration goes forward * @return next transformer transformer */ protected abstract Transformer selectTransformer(Transformer previous, double g, boolean forward); } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/ode/events/EventHandler.java100644 1750 1750 26315 12126627715 30051 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.ode.events; /** This interface represents a handler for discrete events triggered * during ODE integration. * *

                            Some events can be triggered at discrete times as an ODE problem * is solved. This occurs for example when the integration process * should be stopped as some state is reached (G-stop facility) when the * precise date is unknown a priori, or when the derivatives have * discontinuities, or simply when the user wants to monitor some * states boundaries crossings. *

                            * *

                            These events are defined as occurring when a g * switching function sign changes.

                            * *

                            Since events are only problem-dependent and are triggered by the * independent time variable and the state vector, they can * occur at virtually any time, unknown in advance. The integrators will * take care to avoid sign changes inside the steps, they will reduce * the step size when such an event is detected in order to put this * event exactly at the end of the current step. This guarantees that * step interpolation (which always has a one step scope) is relevant * even in presence of discontinuities. This is independent from the * stepsize control provided by integrators that monitor the local * error (this event handling feature is available for all integrators, * including fixed step ones).

                            * * @version $Id: EventHandler.java 1451658 2013-03-01 17:36:46Z luc $ * @since 1.2 */ public interface EventHandler { /** Enumerate for actions to be performed when an event occurs. */ public enum Action { /** Stop indicator. *

                            This value should be used as the return value of the {@link * #eventOccurred eventOccurred} method when the integration should be * stopped after the event ending the current step.

                            */ STOP, /** Reset state indicator. *

                            This value should be used as the return value of the {@link * #eventOccurred eventOccurred} method when the integration should * go on after the event ending the current step, with a new state * vector (which will be retrieved thanks to the {@link #resetState * resetState} method).

                            */ RESET_STATE, /** Reset derivatives indicator. *

                            This value should be used as the return value of the {@link * #eventOccurred eventOccurred} method when the integration should * go on after the event ending the current step, with a new derivatives * vector (which will be retrieved thanks to the {@link * org.apache.commons.math3.ode.FirstOrderDifferentialEquations#computeDerivatives} * method).

                            */ RESET_DERIVATIVES, /** Continue indicator. *

                            This value should be used as the return value of the {@link * #eventOccurred eventOccurred} method when the integration should go * on after the event ending the current step.

                            */ CONTINUE; } /** Initialize event handler at the start of an ODE integration. *

                            * This method is called once at the start of the integration. It * may be used by the event handler to initialize some internal data * if needed. *

                            * @param t0 start value of the independent time variable * @param y0 array containing the start value of the state vector * @param t target time for the integration */ void init(double t0, double[] y0, double t); /** Compute the value of the switching function. *

                            The discrete events are generated when the sign of this * switching function changes. The integrator will take care to change * the stepsize in such a way these events occur exactly at step boundaries. * The switching function must be continuous in its roots neighborhood * (but not necessarily smooth), as the integrator will need to find its * roots to locate precisely the events.

                            *

                            Also note that the integrator expect that once an event has occurred, * the sign of the switching function at the start of the next step (i.e. * just after the event) is the opposite of the sign just before the event. * This consistency between the steps must be preserved, * otherwise {@link org.apache.commons.math3.exception.NoBracketingException * exceptions} related to root not being bracketed will occur.

                            *

                            This need for consistency is sometimes tricky to achieve. A typical * example is using an event to model a ball bouncing on the floor. The first * idea to represent this would be to have {@code g(t) = h(t)} where h is the * height above the floor at time {@code t}. When {@code g(t)} reaches 0, the * ball is on the floor, so it should bounce and the typical way to do this is * to reverse its vertical velocity. However, this would mean that before the * event {@code g(t)} was decreasing from positive values to 0, and after the * event {@code g(t)} would be increasing from 0 to positive values again. * Consistency is broken here! The solution here is to have {@code g(t) = sign * * h(t)}, where sign is a variable with initial value set to {@code +1}. Each * time {@link #eventOccurred(double, double[], boolean) eventOccurred} is called, * {@code sign} is reset to {@code -sign}. This allows the {@code g(t)} * function to remain continuous (and even smooth) even across events, despite * {@code h(t)} is not. Basically, the event is used to fold {@code h(t)} * at bounce points, and {@code sign} is used to unfold it back, so the * solvers sees a {@code g(t)} function which behaves smoothly even across events.

                            * @param t current value of the independent time variable * @param y array containing the current value of the state vector * @return value of the g switching function */ double g(double t, double[] y); /** Handle an event and choose what to do next. *

                            This method is called when the integrator has accepted a step * ending exactly on a sign change of the function, just before * the step handler itself is called (see below for scheduling). It * allows the user to update his internal data to acknowledge the fact * the event has been handled (for example setting a flag in the {@link * org.apache.commons.math3.ode.FirstOrderDifferentialEquations * differential equations} to switch the derivatives computation in * case of discontinuity), or to direct the integrator to either stop * or continue integration, possibly with a reset state or derivatives.

                            *
                              *
                            • if {@link Action#STOP} is returned, the step handler will be called * with the isLast flag of the {@link * org.apache.commons.math3.ode.sampling.StepHandler#handleStep handleStep} * method set to true and the integration will be stopped,
                            • *
                            • if {@link Action#RESET_STATE} is returned, the {@link #resetState * resetState} method will be called once the step handler has * finished its task, and the integrator will also recompute the * derivatives,
                            • *
                            • if {@link Action#RESET_DERIVATIVES} is returned, the integrator * will recompute the derivatives, *
                            • if {@link Action#CONTINUE} is returned, no specific action will * be taken (apart from having called this method) and integration * will continue.
                            • *
                            *

                            The scheduling between this method and the {@link * org.apache.commons.math3.ode.sampling.StepHandler StepHandler} method {@link * org.apache.commons.math3.ode.sampling.StepHandler#handleStep( * org.apache.commons.math3.ode.sampling.StepInterpolator, boolean) * handleStep(interpolator, isLast)} is to call this method first and * handleStep afterwards. This scheduling allows the integrator to * pass true as the isLast parameter to the step * handler to make it aware the step will be the last one if this method * returns {@link Action#STOP}. As the interpolator may be used to navigate back * throughout the last step (as {@link * org.apache.commons.math3.ode.sampling.StepNormalizer StepNormalizer} * does for example), user code called by this method and user * code called by step handlers may experience apparently out of order values * of the independent time variable. As an example, if the same user object * implements both this {@link EventHandler EventHandler} interface and the * {@link org.apache.commons.math3.ode.sampling.FixedStepHandler FixedStepHandler} * interface, a forward integration may call its * eventOccurred method with t = 10 first and call its * handleStep method with t = 9 afterwards. Such out of order * calls are limited to the size of the integration step for {@link * org.apache.commons.math3.ode.sampling.StepHandler variable step handlers} and * to the size of the fixed step for {@link * org.apache.commons.math3.ode.sampling.FixedStepHandler fixed step handlers}.

                            * @param t current value of the independent time variable * @param y array containing the current value of the state vector * @param increasing if true, the value of the switching function increases * when times increases around event (note that increase is measured with respect * to physical time, not with respect to integration which may go backward in time) * @return indication of what the integrator should do next, this * value must be one of {@link Action#STOP}, {@link Action#RESET_STATE}, * {@link Action#RESET_DERIVATIVES} or {@link Action#CONTINUE} */ Action eventOccurred(double t, double[] y, boolean increasing); /** Reset the state prior to continue the integration. *

                            This method is called after the step handler has returned and * before the next step is started, but only when {@link * #eventOccurred} has itself returned the {@link Action#RESET_STATE} * indicator. It allows the user to reset the state vector for the * next step, without perturbing the step handler of the finishing * step. If the {@link #eventOccurred} never returns the {@link * Action#RESET_STATE} indicator, this function will never be called, and it is * safe to leave its body empty.

                            * @param t current value of the independent time variable * @param y array containing the current value of the state vector * the new state should be put in the same array */ void resetState(double t, double[] y); } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/ode/FirstOrderConverter.java100644 1750 1750 10756 12126627716 30144 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.ode; /** This class converts second order differential equations to first * order ones. * *

                            This class is a wrapper around a {@link * SecondOrderDifferentialEquations} which allow to use a {@link * FirstOrderIntegrator} to integrate it.

                            * *

                            The transformation is done by changing the n dimension state * vector to a 2n dimension vector, where the first n components are * the initial state variables and the n last components are their * first time derivative. The first time derivative of this state * vector then really contains both the first and second time * derivative of the initial state vector, which can be handled by the * underlying second order equations set.

                            * *

                            One should be aware that the data is duplicated during the * transformation process and that for each call to {@link * #computeDerivatives computeDerivatives}, this wrapper does copy 4n * scalars : 2n before the call to {@link * SecondOrderDifferentialEquations#computeSecondDerivatives * computeSecondDerivatives} in order to dispatch the y state vector * into z and zDot, and 2n after the call to gather zDot and zDDot * into yDot. Since the underlying problem by itself perhaps also * needs to copy data and dispatch the arrays into domain objects, * this has an impact on both memory and CPU usage. The only way to * avoid this duplication is to perform the transformation at the * problem level, i.e. to implement the problem as a first order one * and then avoid using this class.

                            * * @see FirstOrderIntegrator * @see FirstOrderDifferentialEquations * @see SecondOrderDifferentialEquations * @version $Id: FirstOrderConverter.java 1416643 2012-12-03 19:37:14Z tn $ * @since 1.2 */ public class FirstOrderConverter implements FirstOrderDifferentialEquations { /** Underlying second order equations set. */ private final SecondOrderDifferentialEquations equations; /** second order problem dimension. */ private final int dimension; /** state vector. */ private final double[] z; /** first time derivative of the state vector. */ private final double[] zDot; /** second time derivative of the state vector. */ private final double[] zDDot; /** Simple constructor. * Build a converter around a second order equations set. * @param equations second order equations set to convert */ public FirstOrderConverter (final SecondOrderDifferentialEquations equations) { this.equations = equations; dimension = equations.getDimension(); z = new double[dimension]; zDot = new double[dimension]; zDDot = new double[dimension]; } /** Get the dimension of the problem. *

                            The dimension of the first order problem is twice the * dimension of the underlying second order problem.

                            * @return dimension of the problem */ public int getDimension() { return 2 * dimension; } /** Get the current time derivative of the state vector. * @param t current value of the independent time variable * @param y array containing the current value of the state vector * @param yDot placeholder array where to put the time derivative of the state vector */ public void computeDerivatives(final double t, final double[] y, final double[] yDot) { // split the state vector in two System.arraycopy(y, 0, z, 0, dimension); System.arraycopy(y, dimension, zDot, 0, dimension); // apply the underlying equations set equations.computeSecondDerivatives(t, z, zDot, zDDot); // build the result state derivative System.arraycopy(zDot, 0, yDot, 0, dimension); System.arraycopy(zDDot, 0, yDot, dimension, dimension); } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/ode/SecondaryEquations.java100644 1750 1750 5501 12126627716 27761 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.ode; import org.apache.commons.math3.exception.DimensionMismatchException; import org.apache.commons.math3.exception.MaxCountExceededException; /** * This interface allows users to add secondary differential equations to a primary * set of differential equations. *

                            * In some cases users may need to integrate some problem-specific equations along * with a primary set of differential equations. One example is optimal control where * adjoined parameters linked to the minimized hamiltonian must be integrated. *

                            *

                            * This interface allows users to add such equations to a primary set of {@link * FirstOrderDifferentialEquations first order differential equations} * thanks to the {@link * ExpandableStatefulODE#addSecondaryEquations(SecondaryEquations)} * method. *

                            * @see ExpandableStatefulODE * @version $Id: SecondaryEquations.java 1416643 2012-12-03 19:37:14Z tn $ * @since 3.0 */ public interface SecondaryEquations { /** Get the dimension of the secondary state parameters. * @return dimension of the secondary state parameters */ int getDimension(); /** Compute the derivatives related to the secondary state parameters. * @param t current value of the independent time variable * @param primary array containing the current value of the primary state vector * @param primaryDot array containing the derivative of the primary state vector * @param secondary array containing the current value of the secondary state vector * @param secondaryDot placeholder array where to put the derivative of the secondary state vector * @exception MaxCountExceededException if the number of functions evaluations is exceeded * @exception DimensionMismatchException if arrays dimensions do not match equations settings */ void computeDerivatives(double t, double[] primary, double[] primaryDot, double[] secondary, double[] secondaryDot) throws MaxCountExceededException, DimensionMismatchException; } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/ode/ExpandableStatefulODE.java100644 1750 1750 30206 12126627715 30263 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.ode; import java.util.ArrayList; import java.util.List; import org.apache.commons.math3.exception.DimensionMismatchException; import org.apache.commons.math3.exception.MaxCountExceededException; /** * This class represents a combined set of first order differential equations, * with at least a primary set of equations expandable by some sets of secondary * equations. *

                            * One typical use case is the computation of the Jacobian matrix for some ODE. * In this case, the primary set of equations corresponds to the raw ODE, and we * add to this set another bunch of secondary equations which represent the Jacobian * matrix of the primary set. *

                            *

                            * We want the integrator to use only the primary set to estimate the * errors and hence the step sizes. It should not use the secondary * equations in this computation. The {@link AbstractIntegrator integrator} will * be able to know where the primary set ends and so where the secondary sets begin. *

                            * * @see FirstOrderDifferentialEquations * @see JacobianMatrices * * @version $Id: ExpandableStatefulODE.java 1463680 2013-04-02 19:02:55Z luc $ * @since 3.0 */ public class ExpandableStatefulODE { /** Primary differential equation. */ private final FirstOrderDifferentialEquations primary; /** Mapper for primary equation. */ private final EquationsMapper primaryMapper; /** Time. */ private double time; /** State. */ private final double[] primaryState; /** State derivative. */ private final double[] primaryStateDot; /** Components of the expandable ODE. */ private List components; /** Build an expandable set from its primary ODE set. * @param primary the primary set of differential equations to be integrated. */ public ExpandableStatefulODE(final FirstOrderDifferentialEquations primary) { final int n = primary.getDimension(); this.primary = primary; this.primaryMapper = new EquationsMapper(0, n); this.time = Double.NaN; this.primaryState = new double[n]; this.primaryStateDot = new double[n]; this.components = new ArrayList(); } /** Get the primary set of differential equations. * @return primary set of differential equations */ public FirstOrderDifferentialEquations getPrimary() { return primary; } /** Return the dimension of the complete set of equations. *

                            * The complete set of equations correspond to the primary set plus all secondary sets. *

                            * @return dimension of the complete set of equations */ public int getTotalDimension() { if (components.isEmpty()) { // there are no secondary equations, the complete set is limited to the primary set return primaryMapper.getDimension(); } else { // there are secondary equations, the complete set ends after the last set final EquationsMapper lastMapper = components.get(components.size() - 1).mapper; return lastMapper.getFirstIndex() + lastMapper.getDimension(); } } /** Get the current time derivative of the complete state vector. * @param t current value of the independent time variable * @param y array containing the current value of the complete state vector * @param yDot placeholder array where to put the time derivative of the complete state vector * @exception MaxCountExceededException if the number of functions evaluations is exceeded * @exception DimensionMismatchException if arrays dimensions do not match equations settings */ public void computeDerivatives(final double t, final double[] y, final double[] yDot) throws MaxCountExceededException, DimensionMismatchException { // compute derivatives of the primary equations primaryMapper.extractEquationData(y, primaryState); primary.computeDerivatives(t, primaryState, primaryStateDot); // Add contribution for secondary equations for (final SecondaryComponent component : components) { component.mapper.extractEquationData(y, component.state); component.equation.computeDerivatives(t, primaryState, primaryStateDot, component.state, component.stateDot); component.mapper.insertEquationData(component.stateDot, yDot); } primaryMapper.insertEquationData(primaryStateDot, yDot); } /** Add a set of secondary equations to be integrated along with the primary set. * @param secondary secondary equations set * @return index of the secondary equation in the expanded state */ public int addSecondaryEquations(final SecondaryEquations secondary) { final int firstIndex; if (components.isEmpty()) { // lazy creation of the components list components = new ArrayList(); firstIndex = primary.getDimension(); } else { final SecondaryComponent last = components.get(components.size() - 1); firstIndex = last.mapper.getFirstIndex() + last.mapper.getDimension(); } components.add(new SecondaryComponent(secondary, firstIndex)); return components.size() - 1; } /** Get an equations mapper for the primary equations set. * @return mapper for the primary set * @see #getSecondaryMappers() */ public EquationsMapper getPrimaryMapper() { return primaryMapper; } /** Get the equations mappers for the secondary equations sets. * @return equations mappers for the secondary equations sets * @see #getPrimaryMapper() */ public EquationsMapper[] getSecondaryMappers() { final EquationsMapper[] mappers = new EquationsMapper[components.size()]; for (int i = 0; i < mappers.length; ++i) { mappers[i] = components.get(i).mapper; } return mappers; } /** Set current time. * @param time current time */ public void setTime(final double time) { this.time = time; } /** Get current time. * @return current time */ public double getTime() { return time; } /** Set primary part of the current state. * @param primaryState primary part of the current state * @throws DimensionMismatchException if the dimension of the array does not * match the primary set */ public void setPrimaryState(final double[] primaryState) throws DimensionMismatchException { // safety checks if (primaryState.length != this.primaryState.length) { throw new DimensionMismatchException(primaryState.length, this.primaryState.length); } // set the data System.arraycopy(primaryState, 0, this.primaryState, 0, primaryState.length); } /** Get primary part of the current state. * @return primary part of the current state */ public double[] getPrimaryState() { return primaryState.clone(); } /** Get primary part of the current state derivative. * @return primary part of the current state derivative */ public double[] getPrimaryStateDot() { return primaryStateDot.clone(); } /** Set secondary part of the current state. * @param index index of the part to set as returned by {@link * #addSecondaryEquations(SecondaryEquations)} * @param secondaryState secondary part of the current state * @throws DimensionMismatchException if the dimension of the partial state does not * match the selected equations set dimension */ public void setSecondaryState(final int index, final double[] secondaryState) throws DimensionMismatchException { // get either the secondary state double[] localArray = components.get(index).state; // safety checks if (secondaryState.length != localArray.length) { throw new DimensionMismatchException(secondaryState.length, localArray.length); } // set the data System.arraycopy(secondaryState, 0, localArray, 0, secondaryState.length); } /** Get secondary part of the current state. * @param index index of the part to set as returned by {@link * #addSecondaryEquations(SecondaryEquations)} * @return secondary part of the current state */ public double[] getSecondaryState(final int index) { return components.get(index).state.clone(); } /** Get secondary part of the current state derivative. * @param index index of the part to set as returned by {@link * #addSecondaryEquations(SecondaryEquations)} * @return secondary part of the current state derivative */ public double[] getSecondaryStateDot(final int index) { return components.get(index).stateDot.clone(); } /** Set the complete current state. * @param completeState complete current state to copy data from * @throws DimensionMismatchException if the dimension of the complete state does not * match the complete equations sets dimension */ public void setCompleteState(final double[] completeState) throws DimensionMismatchException { // safety checks if (completeState.length != getTotalDimension()) { throw new DimensionMismatchException(completeState.length, getTotalDimension()); } // set the data primaryMapper.extractEquationData(completeState, primaryState); for (final SecondaryComponent component : components) { component.mapper.extractEquationData(completeState, component.state); } } /** Get the complete current state. * @return complete current state * @throws DimensionMismatchException if the dimension of the complete state does not * match the complete equations sets dimension */ public double[] getCompleteState() throws DimensionMismatchException { // allocate complete array double[] completeState = new double[getTotalDimension()]; // set the data primaryMapper.insertEquationData(primaryState, completeState); for (final SecondaryComponent component : components) { component.mapper.insertEquationData(component.state, completeState); } return completeState; } /** Components of the compound stateful ODE. */ private static class SecondaryComponent { /** Secondary differential equation. */ private final SecondaryEquations equation; /** Mapper between local and complete arrays. */ private final EquationsMapper mapper; /** State. */ private final double[] state; /** State derivative. */ private final double[] stateDot; /** Simple constructor. * @param equation secondary differential equation * @param firstIndex index to use for the first element in the complete arrays */ public SecondaryComponent(final SecondaryEquations equation, final int firstIndex) { final int n = equation.getDimension(); this.equation = equation; mapper = new EquationsMapper(firstIndex, n); state = new double[n]; stateDot = new double[n]; } } } ././@LongLink100644 0 0 147 12126630646 10263 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/ode/SecondOrderDifferentialEquations.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/ode/SecondOrderDifferentialEquations.ja100644 1750 1750 5252 12126627716 32232 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.ode; /** This interface represents a second order differential equations set. *

                            This interface should be implemented by all real second order * differential equation problems before they can be handled by the * integrators {@link SecondOrderIntegrator#integrate} method.

                            * *

                            A second order differential equations problem, as seen by an * integrator is the second time derivative d2Y/dt^2 of a * state vector Y, both being one dimensional * arrays. From the integrator point of view, this derivative depends * only on the current time t, on the state vector * Y and on the first time derivative of the state * vector.

                            * *

                            For real problems, the derivative depends also on parameters * that do not belong to the state vector (dynamical model constants * for example). These constants are completely outside of the scope * of this interface, the classes that implement it are allowed to * handle them as they want.

                            * * @see SecondOrderIntegrator * @see FirstOrderConverter * @see FirstOrderDifferentialEquations * @version $Id: SecondOrderDifferentialEquations.java 1416643 2012-12-03 19:37:14Z tn $ * @since 1.2 */ public interface SecondOrderDifferentialEquations { /** Get the dimension of the problem. * @return dimension of the problem */ int getDimension(); /** Get the current time derivative of the state vector. * @param t current value of the independent time variable * @param y array containing the current value of the state vector * @param yDot array containing the current value of the first derivative * of the state vector * @param yDDot placeholder array where to put the second time derivative * of the state vector */ void computeSecondDerivatives(double t, double[] y, double[] yDot, double[] yDDot); } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/ode/ParameterConfiguration.java100644 1750 1750 4121 12126627716 30606 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.ode; import java.io.Serializable; /** Simple container pairing a parameter name with a step in order to compute * the associated Jacobian matrix by finite difference. * * @version $Id: ParameterConfiguration.java 1416643 2012-12-03 19:37:14Z tn $ * @since 3.0 */ class ParameterConfiguration implements Serializable { /** Serializable UID. */ private static final long serialVersionUID = 2247518849090889379L; /** Parameter name. */ private String parameterName; /** Parameter step for finite difference computation. */ private double hP; /** Parameter name and step pair constructor. * @param parameterName parameter name * @param hP parameter step */ public ParameterConfiguration(final String parameterName, final double hP) { this.parameterName = parameterName; this.hP = hP; } /** Get parameter name. * @return parameterName parameter name */ public String getParameterName() { return parameterName; } /** Get parameter step. * @return hP parameter step */ public double getHP() { return hP; } /** Set parameter step. * @param hParam parameter step */ public void setHP(final double hParam) { this.hP = hParam; } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/ode/SecondOrderIntegrator.java100644 1750 1750 5173 12126627715 30413 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.ode; import org.apache.commons.math3.exception.MathIllegalArgumentException; import org.apache.commons.math3.exception.MathIllegalStateException; /** This interface represents a second order integrator for * differential equations. * *

                            The classes which are devoted to solve second order differential * equations should implement this interface. The problems which can * be handled should implement the {@link * SecondOrderDifferentialEquations} interface.

                            * * @see SecondOrderDifferentialEquations * @version $Id: SecondOrderIntegrator.java 1416643 2012-12-03 19:37:14Z tn $ * @since 1.2 */ public interface SecondOrderIntegrator extends ODEIntegrator { /** Integrate the differential equations up to the given time * @param equations differential equations to integrate * @param t0 initial time * @param y0 initial value of the state vector at t0 * @param yDot0 initial value of the first derivative of the state * vector at t0 * @param t target time for the integration * (can be set to a value smaller thant t0 for backward integration) * @param y placeholder where to put the state vector at each * successful step (and hence at the end of integration), can be the * same object as y0 * @param yDot placeholder where to put the first derivative of * the state vector at time t, can be the same object as yDot0 * @throws MathIllegalStateException if the integrator cannot perform integration * @throws MathIllegalArgumentException if integration parameters are wrong (typically * too small integration span) */ void integrate(SecondOrderDifferentialEquations equations, double t0, double[] y0, double[] yDot0, double t, double[] y, double[] yDot) throws MathIllegalStateException, MathIllegalArgumentException; } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/ode/sampling/package-info.java100644 1750 1750 5727 12126627715 30310 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ /** * *

                            * This package provides classes to handle sampling steps during * Ordinary Differential Equations integration. *

                            * *

                            * In addition to computing the evolution of the state vector at some grid points, all * ODE integrators also build up interpolation models of this evolution inside the * last computed step. If users are interested in these interpolators, they can register a * {@link org.apache.commons.math3.ode.sampling.StepHandler StepHandler} instance using the * {@link org.apache.commons.math3.ode.FirstOrderIntegrator#addStepHandler addStepHandler} * method which is supported by all integrators. The integrator will call this instance * at the end of each accepted step and provide it the interpolator. The user can do * whatever he wants with this interpolator, which computes both the state and its * time-derivative. A typical use of step handler is to provide some output to monitor * the integration process. *

                            * *

                            * In a sense, this is a kind of Inversion Of Control: rather than having the master * application driving the slave integrator by providing the target end value for * the free variable, we get a master integrator scheduling the free variable * evolution and calling the slave application callbacks that were registered at * configuration time. *

                            * *

                            * Since some integrators may use variable step size, the generic {@link * org.apache.commons.math3.ode.sampling.StepHandler StepHandler} interface can be called * either at regular or irregular rate. This interface allows to navigate to any location * within the last computed step, thanks to the provided {@link * org.apache.commons.math3.ode.sampling.StepInterpolator StepInterpolator} object. * If regular output is desired (for example in order to write an ephemeris file), then * the simpler {@link org.apache.commons.math3.ode.sampling.FixedStepHandler FixedStepHandler} * interface can be used. Objects implementing this interface should be wrapped within a * {@link org.apache.commons.math3.ode.sampling.StepNormalizer StepNormalizer} instance * in order to be registered to the integrator. *

                            * * */ package org.apache.commons.math3.ode.sampling; commons-math3-3.2-src/src/main/java/org/apache/commons/math3/ode/sampling/DummyStepHandler.java100644 1750 1750 6306 12126627715 31203 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.ode.sampling; /** * This class is a step handler that does nothing. *

                            This class is provided as a convenience for users who are only * interested in the final state of an integration and not in the * intermediate steps. Its handleStep method does nothing.

                            * *

                            Since this class has no internal state, it is implemented using * the Singleton design pattern. This means that only one instance is * ever created, which can be retrieved using the getInstance * method. This explains why there is no public constructor.

                            * * @see StepHandler * @version $Id: DummyStepHandler.java 1416643 2012-12-03 19:37:14Z tn $ * @since 1.2 */ public class DummyStepHandler implements StepHandler { /** Private constructor. * The constructor is private to prevent users from creating * instances (Singleton design-pattern). */ private DummyStepHandler() { } /** Get the only instance. * @return the only instance */ public static DummyStepHandler getInstance() { return LazyHolder.INSTANCE; } /** {@inheritDoc} */ public void init(double t0, double[] y0, double t) { } /** * Handle the last accepted step. * This method does nothing in this class. * @param interpolator interpolator for the last accepted step. For * efficiency purposes, the various integrators reuse the same * object on each call, so if the instance wants to keep it across * all calls (for example to provide at the end of the integration a * continuous model valid throughout the integration range), it * should build a local copy using the clone method and store this * copy. * @param isLast true if the step is the last one */ public void handleStep(final StepInterpolator interpolator, final boolean isLast) { } // CHECKSTYLE: stop HideUtilityClassConstructor /** Holder for the instance. *

                            We use here the Initialization On Demand Holder Idiom.

                            */ private static class LazyHolder { /** Cached field instance. */ private static final DummyStepHandler INSTANCE = new DummyStepHandler(); } // CHECKSTYLE: resume HideUtilityClassConstructor /** Handle deserialization of the singleton. * @return the singleton instance */ private Object readResolve() { // return the singleton instance return LazyHolder.INSTANCE; } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/ode/sampling/StepNormalizerMode.java100644 1750 1750 5627 12126627715 31546 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.ode.sampling; /** {@link StepNormalizer Step normalizer} modes. Determines how the step size * is interpreted. * @see StepNormalizer * @see StepNormalizerBounds * @version $Id: StepNormalizerMode.java 1416643 2012-12-03 19:37:14Z tn $ * @since 3.0 */ public enum StepNormalizerMode { /** * Steps are fixed increments of the start value. In other words, they * are relative to the start value. * *

                            If the integration start time is t0, then the points handled by * the underlying fixed step size step handler are t0 (depending on * the {@link StepNormalizerBounds bounds settings}), t0+h, t0+2h, ...

                            * *

                            If the integration range is an integer multiple of the step size * (h), then the last point handled will be the end point of the * integration (tend). If not, the last point may be the end point * tend, or it may be a point belonging to the interval [tend - h ; * tend], depending on the {@link StepNormalizerBounds bounds settings}. *

                            * * @see StepNormalizer * @see StepNormalizerBounds */ INCREMENT, /** Steps are multiples of a fixed value. In other words, they are * relative to the first multiple of the step size that is encountered * after the start value. * *

                            If the integration start time is t0, and the first multiple of * the fixed step size that is encountered is t1, then the points * handled by the underlying fixed step size step handler are t0 * (depending on the {@link StepNormalizerBounds bounds settings}), t1, * t1+h, t1+2h, ...

                            * *

                            If the end point of the integration range (tend) is an integer * multiple of the step size (h) added to t1, then the last point * handled will be the end point of the integration (tend). If not, * the last point may be the end point tend, or it may be a point * belonging to the interval [tend - h ; tend], depending on the * {@link StepNormalizerBounds bounds settings}.

                            * * @see StepNormalizer * @see StepNormalizerBounds */ MULTIPLES; } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/ode/sampling/FixedStepHandler.java100644 1750 1750 5751 12126627715 31152 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.ode.sampling; /** * This interface represents a handler that should be called after * each successful fixed step. *

                            This interface should be implemented by anyone who is interested * in getting the solution of an ordinary differential equation at * fixed time steps. Objects implementing this interface should be * wrapped within an instance of {@link StepNormalizer} that itself * is used as the general {@link StepHandler} by the integrator. The * {@link StepNormalizer} object is called according to the integrator * internal algorithms and it calls objects implementing this * interface as necessary at fixed time steps.

                            * * @see StepHandler * @see StepNormalizer * @version $Id: FixedStepHandler.java 1416643 2012-12-03 19:37:14Z tn $ * @since 1.2 */ public interface FixedStepHandler { /** Initialize step handler at the start of an ODE integration. *

                            * This method is called once at the start of the integration. It * may be used by the step handler to initialize some internal data * if needed. *

                            * @param t0 start value of the independent time variable * @param y0 array containing the start value of the state vector * @param t target time for the integration */ void init(double t0, double[] y0, double t); /** * Handle the last accepted step * @param t time of the current step * @param y state vector at t. For efficiency purposes, the {@link * StepNormalizer} class reuses the same array on each call, so if * the instance wants to keep it across all calls (for example to * provide at the end of the integration a complete array of all * steps), it should build a local copy store this copy. * @param yDot derivatives of the state vector state vector at t. * For efficiency purposes, the {@link StepNormalizer} class reuses * the same array on each call, so if * the instance wants to keep it across all calls (for example to * provide at the end of the integration a complete array of all * steps), it should build a local copy store this copy. * @param isLast true if the step is the last one */ void handleStep(double t, double[] y, double[] yDot, boolean isLast); } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/ode/sampling/StepInterpolator.java100644 1750 1750 16205 12126627715 31313 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.ode.sampling; import java.io.Externalizable; import org.apache.commons.math3.exception.MaxCountExceededException; /** This interface represents an interpolator over the last step * during an ODE integration. * *

                            The various ODE integrators provide objects implementing this * interface to the step handlers. These objects are often custom * objects tightly bound to the integrator internal algorithms. The * handlers can use these objects to retrieve the state vector at * intermediate times between the previous and the current grid points * (this feature is often called dense output).

                            *

                            One important thing to note is that the step handlers may be so * tightly bound to the integrators that they often share some internal * state arrays. This imply that one should never use a direct * reference to a step interpolator outside of the step handler, either * for future use or for use in another thread. If such a need arise, the * step interpolator must be copied using the dedicated * {@link #copy()} method. *

                            * * @see org.apache.commons.math3.ode.FirstOrderIntegrator * @see org.apache.commons.math3.ode.SecondOrderIntegrator * @see StepHandler * @version $Id: StepInterpolator.java 1416643 2012-12-03 19:37:14Z tn $ * @since 1.2 */ public interface StepInterpolator extends Externalizable { /** * Get the previous grid point time. * @return previous grid point time */ double getPreviousTime(); /** * Get the current grid point time. * @return current grid point time */ double getCurrentTime(); /** * Get the time of the interpolated point. * If {@link #setInterpolatedTime} has not been called, it returns * the current grid point time. * @return interpolation point time */ double getInterpolatedTime(); /** * Set the time of the interpolated point. *

                            Setting the time outside of the current step is now allowed, but * should be used with care since the accuracy of the interpolator will * probably be very poor far from this step. This allowance has been * added to simplify implementation of search algorithms near the * step endpoints.

                            *

                            Setting the time changes the instance internal state. If a * specific state must be preserved, a copy of the instance must be * created using {@link #copy()}.

                            * @param time time of the interpolated point */ void setInterpolatedTime(double time); /** * Get the state vector of the interpolated point. *

                            The returned vector is a reference to a reused array, so * it should not be modified and it should be copied if it needs * to be preserved across several calls.

                            * @return state vector at time {@link #getInterpolatedTime} * @see #getInterpolatedDerivatives() * @exception MaxCountExceededException if the number of functions evaluations is exceeded */ double[] getInterpolatedState() throws MaxCountExceededException; /** * Get the derivatives of the state vector of the interpolated point. *

                            The returned vector is a reference to a reused array, so * it should not be modified and it should be copied if it needs * to be preserved across several calls.

                            * @return derivatives of the state vector at time {@link #getInterpolatedTime} * @see #getInterpolatedState() * @since 2.0 * @exception MaxCountExceededException if the number of functions evaluations is exceeded */ double[] getInterpolatedDerivatives() throws MaxCountExceededException; /** Get the interpolated secondary state corresponding to the secondary equations. *

                            The returned vector is a reference to a reused array, so * it should not be modified and it should be copied if it needs * to be preserved across several calls.

                            * @param index index of the secondary set, as returned by {@link * org.apache.commons.math3.ode.ExpandableStatefulODE#addSecondaryEquations( * org.apache.commons.math3.ode.SecondaryEquations) * ExpandableStatefulODE.addSecondaryEquations(SecondaryEquations)} * @return interpolated secondary state at the current interpolation date * @see #getInterpolatedState() * @see #getInterpolatedDerivatives() * @see #getInterpolatedSecondaryDerivatives(int) * @see #setInterpolatedTime(double) * @since 3.0 * @exception MaxCountExceededException if the number of functions evaluations is exceeded */ double[] getInterpolatedSecondaryState(int index) throws MaxCountExceededException; /** Get the interpolated secondary derivatives corresponding to the secondary equations. *

                            The returned vector is a reference to a reused array, so * it should not be modified and it should be copied if it needs * to be preserved across several calls.

                            * @param index index of the secondary set, as returned by {@link * org.apache.commons.math3.ode.ExpandableStatefulODE#addSecondaryEquations( * org.apache.commons.math3.ode.SecondaryEquations) * ExpandableStatefulODE.addSecondaryEquations(SecondaryEquations)} * @return interpolated secondary derivatives at the current interpolation date * @see #getInterpolatedState() * @see #getInterpolatedDerivatives() * @see #getInterpolatedSecondaryState(int) * @see #setInterpolatedTime(double) * @since 3.0 * @exception MaxCountExceededException if the number of functions evaluations is exceeded */ double[] getInterpolatedSecondaryDerivatives(int index) throws MaxCountExceededException; /** Check if the natural integration direction is forward. *

                            This method provides the integration direction as specified by * the integrator itself, it avoid some nasty problems in * degenerated cases like null steps due to cancellation at step * initialization, step control or discrete events * triggering.

                            * @return true if the integration variable (time) increases during * integration */ boolean isForward(); /** Copy the instance. *

                            The copied instance is guaranteed to be independent from the * original one. Both can be used with different settings for * interpolated time without any side effect.

                            * @return a deep copy of the instance, which can be used independently. * @see #setInterpolatedTime(double) * @exception MaxCountExceededException if the number of functions evaluations is exceeded * during step finalization */ StepInterpolator copy() throws MaxCountExceededException; } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/ode/sampling/StepHandler.java100644 1750 1750 6466 12126627715 30176 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.ode.sampling; import org.apache.commons.math3.exception.MaxCountExceededException; /** * This interface represents a handler that should be called after * each successful step. * *

                            The ODE integrators compute the evolution of the state vector at * some grid points that depend on their own internal algorithm. Once * they have found a new grid point (possibly after having computed * several evaluation of the derivative at intermediate points), they * provide it to objects implementing this interface. These objects * typically either ignore the intermediate steps and wait for the * last one, store the points in an ephemeris, or forward them to * specialized processing or output methods.

                            * * @see org.apache.commons.math3.ode.FirstOrderIntegrator * @see org.apache.commons.math3.ode.SecondOrderIntegrator * @see StepInterpolator * @version $Id: StepHandler.java 1416643 2012-12-03 19:37:14Z tn $ * @since 1.2 */ public interface StepHandler { /** Initialize step handler at the start of an ODE integration. *

                            * This method is called once at the start of the integration. It * may be used by the step handler to initialize some internal data * if needed. *

                            * @param t0 start value of the independent time variable * @param y0 array containing the start value of the state vector * @param t target time for the integration */ void init(double t0, double[] y0, double t); /** * Handle the last accepted step * @param interpolator interpolator for the last accepted step. For * efficiency purposes, the various integrators reuse the same * object on each call, so if the instance wants to keep it across * all calls (for example to provide at the end of the integration a * continuous model valid throughout the integration range, as the * {@link org.apache.commons.math3.ode.ContinuousOutputModel * ContinuousOutputModel} class does), it should build a local copy * using the clone method of the interpolator and store this copy. * Keeping only a reference to the interpolator and reusing it will * result in unpredictable behavior (potentially crashing the application). * @param isLast true if the step is the last one * @exception MaxCountExceededException if the interpolator throws one because * the number of functions evaluations is exceeded */ void handleStep(StepInterpolator interpolator, boolean isLast) throws MaxCountExceededException; } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/ode/sampling/StepNormalizer.java100644 1750 1750 40625 12126627715 30756 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.ode.sampling; import org.apache.commons.math3.exception.MaxCountExceededException; import org.apache.commons.math3.util.FastMath; import org.apache.commons.math3.util.Precision; /** * This class wraps an object implementing {@link FixedStepHandler} * into a {@link StepHandler}. *

                            This wrapper allows to use fixed step handlers with general * integrators which cannot guaranty their integration steps will * remain constant and therefore only accept general step * handlers.

                            * *

                            The stepsize used is selected at construction time. The {@link * FixedStepHandler#handleStep handleStep} method of the underlying * {@link FixedStepHandler} object is called at normalized times. The * normalized times can be influenced by the {@link StepNormalizerMode} and * {@link StepNormalizerBounds}.

                            * *

                            There is no constraint on the integrator, it can use any time step * it needs (time steps longer or shorter than the fixed time step and * non-integer ratios are all allowed).

                            * *

                            * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
                            Examples (step size = 0.5)
                            Start timeEnd timeDirection{@link StepNormalizerMode Mode}{@link StepNormalizerBounds Bounds}Output
                            0.33.1forward{@link StepNormalizerMode#INCREMENT INCREMENT}{@link StepNormalizerBounds#NEITHER NEITHER}0.8, 1.3, 1.8, 2.3, 2.8
                            0.33.1forward{@link StepNormalizerMode#INCREMENT INCREMENT}{@link StepNormalizerBounds#FIRST FIRST}0.3, 0.8, 1.3, 1.8, 2.3, 2.8
                            0.33.1forward{@link StepNormalizerMode#INCREMENT INCREMENT}{@link StepNormalizerBounds#LAST LAST}0.8, 1.3, 1.8, 2.3, 2.8, 3.1
                            0.33.1forward{@link StepNormalizerMode#INCREMENT INCREMENT}{@link StepNormalizerBounds#BOTH BOTH}0.3, 0.8, 1.3, 1.8, 2.3, 2.8, 3.1
                            0.33.1forward{@link StepNormalizerMode#MULTIPLES MULTIPLES}{@link StepNormalizerBounds#NEITHER NEITHER}0.5, 1.0, 1.5, 2.0, 2.5, 3.0
                            0.33.1forward{@link StepNormalizerMode#MULTIPLES MULTIPLES}{@link StepNormalizerBounds#FIRST FIRST}0.3, 0.5, 1.0, 1.5, 2.0, 2.5, 3.0
                            0.33.1forward{@link StepNormalizerMode#MULTIPLES MULTIPLES}{@link StepNormalizerBounds#LAST LAST}0.5, 1.0, 1.5, 2.0, 2.5, 3.0, 3.1
                            0.33.1forward{@link StepNormalizerMode#MULTIPLES MULTIPLES}{@link StepNormalizerBounds#BOTH BOTH}0.3, 0.5, 1.0, 1.5, 2.0, 2.5, 3.0, 3.1
                            0.03.0forward{@link StepNormalizerMode#INCREMENT INCREMENT}{@link StepNormalizerBounds#NEITHER NEITHER}0.5, 1.0, 1.5, 2.0, 2.5, 3.0
                            0.03.0forward{@link StepNormalizerMode#INCREMENT INCREMENT}{@link StepNormalizerBounds#FIRST FIRST}0.0, 0.5, 1.0, 1.5, 2.0, 2.5, 3.0
                            0.03.0forward{@link StepNormalizerMode#INCREMENT INCREMENT}{@link StepNormalizerBounds#LAST LAST}0.5, 1.0, 1.5, 2.0, 2.5, 3.0
                            0.03.0forward{@link StepNormalizerMode#INCREMENT INCREMENT}{@link StepNormalizerBounds#BOTH BOTH}0.0, 0.5, 1.0, 1.5, 2.0, 2.5, 3.0
                            0.03.0forward{@link StepNormalizerMode#MULTIPLES MULTIPLES}{@link StepNormalizerBounds#NEITHER NEITHER}0.5, 1.0, 1.5, 2.0, 2.5, 3.0
                            0.03.0forward{@link StepNormalizerMode#MULTIPLES MULTIPLES}{@link StepNormalizerBounds#FIRST FIRST}0.0, 0.5, 1.0, 1.5, 2.0, 2.5, 3.0
                            0.03.0forward{@link StepNormalizerMode#MULTIPLES MULTIPLES}{@link StepNormalizerBounds#LAST LAST}0.5, 1.0, 1.5, 2.0, 2.5, 3.0
                            0.03.0forward{@link StepNormalizerMode#MULTIPLES MULTIPLES}{@link StepNormalizerBounds#BOTH BOTH}0.0, 0.5, 1.0, 1.5, 2.0, 2.5, 3.0
                            3.10.3backward{@link StepNormalizerMode#INCREMENT INCREMENT}{@link StepNormalizerBounds#NEITHER NEITHER}2.6, 2.1, 1.6, 1.1, 0.6
                            3.10.3backward{@link StepNormalizerMode#INCREMENT INCREMENT}{@link StepNormalizerBounds#FIRST FIRST}3.1, 2.6, 2.1, 1.6, 1.1, 0.6
                            3.10.3backward{@link StepNormalizerMode#INCREMENT INCREMENT}{@link StepNormalizerBounds#LAST LAST}2.6, 2.1, 1.6, 1.1, 0.6, 0.3
                            3.10.3backward{@link StepNormalizerMode#INCREMENT INCREMENT}{@link StepNormalizerBounds#BOTH BOTH}3.1, 2.6, 2.1, 1.6, 1.1, 0.6, 0.3
                            3.10.3backward{@link StepNormalizerMode#MULTIPLES MULTIPLES}{@link StepNormalizerBounds#NEITHER NEITHER}3.0, 2.5, 2.0, 1.5, 1.0, 0.5
                            3.10.3backward{@link StepNormalizerMode#MULTIPLES MULTIPLES}{@link StepNormalizerBounds#FIRST FIRST}3.1, 3.0, 2.5, 2.0, 1.5, 1.0, 0.5
                            3.10.3backward{@link StepNormalizerMode#MULTIPLES MULTIPLES}{@link StepNormalizerBounds#LAST LAST}3.0, 2.5, 2.0, 1.5, 1.0, 0.5, 0.3
                            3.10.3backward{@link StepNormalizerMode#MULTIPLES MULTIPLES}{@link StepNormalizerBounds#BOTH BOTH}3.1, 3.0, 2.5, 2.0, 1.5, 1.0, 0.5, 0.3
                            3.00.0backward{@link StepNormalizerMode#INCREMENT INCREMENT}{@link StepNormalizerBounds#NEITHER NEITHER}2.5, 2.0, 1.5, 1.0, 0.5, 0.0
                            3.00.0backward{@link StepNormalizerMode#INCREMENT INCREMENT}{@link StepNormalizerBounds#FIRST FIRST}3.0, 2.5, 2.0, 1.5, 1.0, 0.5, 0.0
                            3.00.0backward{@link StepNormalizerMode#INCREMENT INCREMENT}{@link StepNormalizerBounds#LAST LAST}2.5, 2.0, 1.5, 1.0, 0.5, 0.0
                            3.00.0backward{@link StepNormalizerMode#INCREMENT INCREMENT}{@link StepNormalizerBounds#BOTH BOTH}3.0, 2.5, 2.0, 1.5, 1.0, 0.5, 0.0
                            3.00.0backward{@link StepNormalizerMode#MULTIPLES MULTIPLES}{@link StepNormalizerBounds#NEITHER NEITHER}2.5, 2.0, 1.5, 1.0, 0.5, 0.0
                            3.00.0backward{@link StepNormalizerMode#MULTIPLES MULTIPLES}{@link StepNormalizerBounds#FIRST FIRST}3.0, 2.5, 2.0, 1.5, 1.0, 0.5, 0.0
                            3.00.0backward{@link StepNormalizerMode#MULTIPLES MULTIPLES}{@link StepNormalizerBounds#LAST LAST}2.5, 2.0, 1.5, 1.0, 0.5, 0.0
                            3.00.0backward{@link StepNormalizerMode#MULTIPLES MULTIPLES}{@link StepNormalizerBounds#BOTH BOTH}3.0, 2.5, 2.0, 1.5, 1.0, 0.5, 0.0
                            *

                            * * @see StepHandler * @see FixedStepHandler * @see StepNormalizerMode * @see StepNormalizerBounds * @version $Id: StepNormalizer.java 1416643 2012-12-03 19:37:14Z tn $ * @since 1.2 */ public class StepNormalizer implements StepHandler { /** Fixed time step. */ private double h; /** Underlying step handler. */ private final FixedStepHandler handler; /** First step time. */ private double firstTime; /** Last step time. */ private double lastTime; /** Last state vector. */ private double[] lastState; /** Last derivatives vector. */ private double[] lastDerivatives; /** Integration direction indicator. */ private boolean forward; /** The step normalizer bounds settings to use. */ private final StepNormalizerBounds bounds; /** The step normalizer mode to use. */ private final StepNormalizerMode mode; /** Simple constructor. Uses {@link StepNormalizerMode#INCREMENT INCREMENT} * mode, and {@link StepNormalizerBounds#FIRST FIRST} bounds setting, for * backwards compatibility. * @param h fixed time step (sign is not used) * @param handler fixed time step handler to wrap */ public StepNormalizer(final double h, final FixedStepHandler handler) { this(h, handler, StepNormalizerMode.INCREMENT, StepNormalizerBounds.FIRST); } /** Simple constructor. Uses {@link StepNormalizerBounds#FIRST FIRST} * bounds setting. * @param h fixed time step (sign is not used) * @param handler fixed time step handler to wrap * @param mode step normalizer mode to use * @since 3.0 */ public StepNormalizer(final double h, final FixedStepHandler handler, final StepNormalizerMode mode) { this(h, handler, mode, StepNormalizerBounds.FIRST); } /** Simple constructor. Uses {@link StepNormalizerMode#INCREMENT INCREMENT} * mode. * @param h fixed time step (sign is not used) * @param handler fixed time step handler to wrap * @param bounds step normalizer bounds setting to use * @since 3.0 */ public StepNormalizer(final double h, final FixedStepHandler handler, final StepNormalizerBounds bounds) { this(h, handler, StepNormalizerMode.INCREMENT, bounds); } /** Simple constructor. * @param h fixed time step (sign is not used) * @param handler fixed time step handler to wrap * @param mode step normalizer mode to use * @param bounds step normalizer bounds setting to use * @since 3.0 */ public StepNormalizer(final double h, final FixedStepHandler handler, final StepNormalizerMode mode, final StepNormalizerBounds bounds) { this.h = FastMath.abs(h); this.handler = handler; this.mode = mode; this.bounds = bounds; firstTime = Double.NaN; lastTime = Double.NaN; lastState = null; lastDerivatives = null; forward = true; } /** {@inheritDoc} */ public void init(double t0, double[] y0, double t) { firstTime = Double.NaN; lastTime = Double.NaN; lastState = null; lastDerivatives = null; forward = true; // initialize the underlying handler handler.init(t0, y0, t); } /** * Handle the last accepted step * @param interpolator interpolator for the last accepted step. For * efficiency purposes, the various integrators reuse the same * object on each call, so if the instance wants to keep it across * all calls (for example to provide at the end of the integration a * continuous model valid throughout the integration range), it * should build a local copy using the clone method and store this * copy. * @param isLast true if the step is the last one * @exception MaxCountExceededException if the interpolator throws one because * the number of functions evaluations is exceeded */ public void handleStep(final StepInterpolator interpolator, final boolean isLast) throws MaxCountExceededException { // The first time, update the last state with the start information. if (lastState == null) { firstTime = interpolator.getPreviousTime(); lastTime = interpolator.getPreviousTime(); interpolator.setInterpolatedTime(lastTime); lastState = interpolator.getInterpolatedState().clone(); lastDerivatives = interpolator.getInterpolatedDerivatives().clone(); // Take the integration direction into account. forward = interpolator.getCurrentTime() >= lastTime; if (!forward) { h = -h; } } // Calculate next normalized step time. double nextTime = (mode == StepNormalizerMode.INCREMENT) ? lastTime + h : (FastMath.floor(lastTime / h) + 1) * h; if (mode == StepNormalizerMode.MULTIPLES && Precision.equals(nextTime, lastTime, 1)) { nextTime += h; } // Process normalized steps as long as they are in the current step. boolean nextInStep = isNextInStep(nextTime, interpolator); while (nextInStep) { // Output the stored previous step. doNormalizedStep(false); // Store the next step as last step. storeStep(interpolator, nextTime); // Move on to the next step. nextTime += h; nextInStep = isNextInStep(nextTime, interpolator); } if (isLast) { // There will be no more steps. The stored one should be given to // the handler. We may have to output one more step. Only the last // one of those should be flagged as being the last. boolean addLast = bounds.lastIncluded() && lastTime != interpolator.getCurrentTime(); doNormalizedStep(!addLast); if (addLast) { storeStep(interpolator, interpolator.getCurrentTime()); doNormalizedStep(true); } } } /** * Returns a value indicating whether the next normalized time is in the * current step. * @param nextTime the next normalized time * @param interpolator interpolator for the last accepted step, to use to * get the end time of the current step * @return value indicating whether the next normalized time is in the * current step */ private boolean isNextInStep(double nextTime, StepInterpolator interpolator) { return forward ? nextTime <= interpolator.getCurrentTime() : nextTime >= interpolator.getCurrentTime(); } /** * Invokes the underlying step handler for the current normalized step. * @param isLast true if the step is the last one */ private void doNormalizedStep(boolean isLast) { if (!bounds.firstIncluded() && firstTime == lastTime) { return; } handler.handleStep(lastTime, lastState, lastDerivatives, isLast); } /** Stores the interpolated information for the given time in the current * state. * @param interpolator interpolator for the last accepted step, to use to * get the interpolated information * @param t the time for which to store the interpolated information * @exception MaxCountExceededException if the interpolator throws one because * the number of functions evaluations is exceeded */ private void storeStep(StepInterpolator interpolator, double t) throws MaxCountExceededException { lastTime = t; interpolator.setInterpolatedTime(lastTime); System.arraycopy(interpolator.getInterpolatedState(), 0, lastState, 0, lastState.length); System.arraycopy(interpolator.getInterpolatedDerivatives(), 0, lastDerivatives, 0, lastDerivatives.length); } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/ode/sampling/StepNormalizerBounds.java100644 1750 1750 6001 12126627715 32077 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.ode.sampling; /** {@link StepNormalizer Step normalizer} bounds settings. They influence * whether the underlying fixed step size step handler is called for the first * and last points. Note that if the last point coincides with a normalized * point, then the underlying fixed step size step handler is always called, * regardless of these settings. * @see StepNormalizer * @see StepNormalizerMode * @version $Id: StepNormalizerBounds.java 1416643 2012-12-03 19:37:14Z tn $ * @since 3.0 */ public enum StepNormalizerBounds { /** Do not include the first and last points. */ NEITHER(false, false), /** Include the first point, but not the last point. */ FIRST(true, false), /** Include the last point, but not the first point. */ LAST(false, true), /** Include both the first and last points. */ BOTH(true, true); /** Whether the first point should be passed to the underlying fixed * step size step handler. */ private final boolean first; /** Whether the last point should be passed to the underlying fixed * step size step handler. */ private final boolean last; /** * Simple constructor. * @param first Whether the first point should be passed to the * underlying fixed step size step handler. * @param last Whether the last point should be passed to the * underlying fixed step size step handler. */ private StepNormalizerBounds(final boolean first, final boolean last) { this.first = first; this.last = last; } /** * Returns a value indicating whether the first point should be passed * to the underlying fixed step size step handler. * @return value indicating whether the first point should be passed * to the underlying fixed step size step handler. */ public boolean firstIncluded() { return first; } /** * Returns a value indicating whether the last point should be passed * to the underlying fixed step size step handler. * @return value indicating whether the last point should be passed * to the underlying fixed step size step handler. */ public boolean lastIncluded() { return last; } } ././@LongLink100644 0 0 150 12126630646 10255 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/ode/sampling/AbstractStepInterpolator.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/ode/sampling/AbstractStepInterpolator.j100644 1750 1750 53641 12126627715 32314 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.ode.sampling; import java.io.IOException; import java.io.ObjectInput; import java.io.ObjectOutput; import org.apache.commons.math3.exception.MaxCountExceededException; import org.apache.commons.math3.ode.EquationsMapper; /** This abstract class represents an interpolator over the last step * during an ODE integration. * *

                            The various ODE integrators provide objects extending this class * to the step handlers. The handlers can use these objects to * retrieve the state vector at intermediate times between the * previous and the current grid points (dense output).

                            * * @see org.apache.commons.math3.ode.FirstOrderIntegrator * @see org.apache.commons.math3.ode.SecondOrderIntegrator * @see StepHandler * * @version $Id: AbstractStepInterpolator.java 1416643 2012-12-03 19:37:14Z tn $ * @since 1.2 * */ public abstract class AbstractStepInterpolator implements StepInterpolator { /** current time step */ protected double h; /** current state */ protected double[] currentState; /** interpolated time */ protected double interpolatedTime; /** interpolated state */ protected double[] interpolatedState; /** interpolated derivatives */ protected double[] interpolatedDerivatives; /** interpolated primary state */ protected double[] interpolatedPrimaryState; /** interpolated primary derivatives */ protected double[] interpolatedPrimaryDerivatives; /** interpolated secondary state */ protected double[][] interpolatedSecondaryState; /** interpolated secondary derivatives */ protected double[][] interpolatedSecondaryDerivatives; /** global previous time */ private double globalPreviousTime; /** global current time */ private double globalCurrentTime; /** soft previous time */ private double softPreviousTime; /** soft current time */ private double softCurrentTime; /** indicate if the step has been finalized or not. */ private boolean finalized; /** integration direction. */ private boolean forward; /** indicator for dirty state. */ private boolean dirtyState; /** Equations mapper for the primary equations set. */ private EquationsMapper primaryMapper; /** Equations mappers for the secondary equations sets. */ private EquationsMapper[] secondaryMappers; /** Simple constructor. * This constructor builds an instance that is not usable yet, the * {@link #reinitialize} method should be called before using the * instance in order to initialize the internal arrays. This * constructor is used only in order to delay the initialization in * some cases. As an example, the {@link * org.apache.commons.math3.ode.nonstiff.EmbeddedRungeKuttaIntegrator} * class uses the prototyping design pattern to create the step * interpolators by cloning an uninitialized model and latter * initializing the copy. */ protected AbstractStepInterpolator() { globalPreviousTime = Double.NaN; globalCurrentTime = Double.NaN; softPreviousTime = Double.NaN; softCurrentTime = Double.NaN; h = Double.NaN; interpolatedTime = Double.NaN; currentState = null; finalized = false; this.forward = true; this.dirtyState = true; primaryMapper = null; secondaryMappers = null; allocateInterpolatedArrays(-1); } /** Simple constructor. * @param y reference to the integrator array holding the state at * the end of the step * @param forward integration direction indicator * @param primaryMapper equations mapper for the primary equations set * @param secondaryMappers equations mappers for the secondary equations sets */ protected AbstractStepInterpolator(final double[] y, final boolean forward, final EquationsMapper primaryMapper, final EquationsMapper[] secondaryMappers) { globalPreviousTime = Double.NaN; globalCurrentTime = Double.NaN; softPreviousTime = Double.NaN; softCurrentTime = Double.NaN; h = Double.NaN; interpolatedTime = Double.NaN; currentState = y; finalized = false; this.forward = forward; this.dirtyState = true; this.primaryMapper = primaryMapper; this.secondaryMappers = (secondaryMappers == null) ? null : secondaryMappers.clone(); allocateInterpolatedArrays(y.length); } /** Copy constructor. *

                            The copied interpolator should have been finalized before the * copy, otherwise the copy will not be able to perform correctly * any derivative computation and will throw a {@link * NullPointerException} later. Since we don't want this constructor * to throw the exceptions finalization may involve and since we * don't want this method to modify the state of the copied * interpolator, finalization is not done * automatically, it remains under user control.

                            *

                            The copy is a deep copy: its arrays are separated from the * original arrays of the instance.

                            * @param interpolator interpolator to copy from. */ protected AbstractStepInterpolator(final AbstractStepInterpolator interpolator) { globalPreviousTime = interpolator.globalPreviousTime; globalCurrentTime = interpolator.globalCurrentTime; softPreviousTime = interpolator.softPreviousTime; softCurrentTime = interpolator.softCurrentTime; h = interpolator.h; interpolatedTime = interpolator.interpolatedTime; if (interpolator.currentState == null) { currentState = null; primaryMapper = null; secondaryMappers = null; allocateInterpolatedArrays(-1); } else { currentState = interpolator.currentState.clone(); interpolatedState = interpolator.interpolatedState.clone(); interpolatedDerivatives = interpolator.interpolatedDerivatives.clone(); interpolatedPrimaryState = interpolator.interpolatedPrimaryState.clone(); interpolatedPrimaryDerivatives = interpolator.interpolatedPrimaryDerivatives.clone(); interpolatedSecondaryState = new double[interpolator.interpolatedSecondaryState.length][]; interpolatedSecondaryDerivatives = new double[interpolator.interpolatedSecondaryDerivatives.length][]; for (int i = 0; i < interpolatedSecondaryState.length; ++i) { interpolatedSecondaryState[i] = interpolator.interpolatedSecondaryState[i].clone(); interpolatedSecondaryDerivatives[i] = interpolator.interpolatedSecondaryDerivatives[i].clone(); } } finalized = interpolator.finalized; forward = interpolator.forward; dirtyState = interpolator.dirtyState; primaryMapper = interpolator.primaryMapper; secondaryMappers = (interpolator.secondaryMappers == null) ? null : interpolator.secondaryMappers.clone(); } /** Allocate the various interpolated states arrays. * @param dimension total dimension (negative if arrays should be set to null) */ private void allocateInterpolatedArrays(final int dimension) { if (dimension < 0) { interpolatedState = null; interpolatedDerivatives = null; interpolatedPrimaryState = null; interpolatedPrimaryDerivatives = null; interpolatedSecondaryState = null; interpolatedSecondaryDerivatives = null; } else { interpolatedState = new double[dimension]; interpolatedDerivatives = new double[dimension]; interpolatedPrimaryState = new double[primaryMapper.getDimension()]; interpolatedPrimaryDerivatives = new double[primaryMapper.getDimension()]; if (secondaryMappers == null) { interpolatedSecondaryState = null; interpolatedSecondaryDerivatives = null; } else { interpolatedSecondaryState = new double[secondaryMappers.length][]; interpolatedSecondaryDerivatives = new double[secondaryMappers.length][]; for (int i = 0; i < secondaryMappers.length; ++i) { interpolatedSecondaryState[i] = new double[secondaryMappers[i].getDimension()]; interpolatedSecondaryDerivatives[i] = new double[secondaryMappers[i].getDimension()]; } } } } /** Reinitialize the instance * @param y reference to the integrator array holding the state at the end of the step * @param isForward integration direction indicator * @param primary equations mapper for the primary equations set * @param secondary equations mappers for the secondary equations sets */ protected void reinitialize(final double[] y, final boolean isForward, final EquationsMapper primary, final EquationsMapper[] secondary) { globalPreviousTime = Double.NaN; globalCurrentTime = Double.NaN; softPreviousTime = Double.NaN; softCurrentTime = Double.NaN; h = Double.NaN; interpolatedTime = Double.NaN; currentState = y; finalized = false; this.forward = isForward; this.dirtyState = true; this.primaryMapper = primary; this.secondaryMappers = secondary.clone(); allocateInterpolatedArrays(y.length); } /** {@inheritDoc} */ public StepInterpolator copy() throws MaxCountExceededException { // finalize the step before performing copy finalizeStep(); // create the new independent instance return doCopy(); } /** Really copy the finalized instance. *

                            This method is called by {@link #copy()} after the * step has been finalized. It must perform a deep copy * to have an new instance completely independent for the * original instance. * @return a copy of the finalized instance */ protected abstract StepInterpolator doCopy(); /** Shift one step forward. * Copy the current time into the previous time, hence preparing the * interpolator for future calls to {@link #storeTime storeTime} */ public void shift() { globalPreviousTime = globalCurrentTime; softPreviousTime = globalPreviousTime; softCurrentTime = globalCurrentTime; } /** Store the current step time. * @param t current time */ public void storeTime(final double t) { globalCurrentTime = t; softCurrentTime = globalCurrentTime; h = globalCurrentTime - globalPreviousTime; setInterpolatedTime(t); // the step is not finalized anymore finalized = false; } /** Restrict step range to a limited part of the global step. *

                            * This method can be used to restrict a step and make it appear * as if the original step was smaller. Calling this method * only changes the value returned by {@link #getPreviousTime()}, * it does not change any other property *

                            * @param softPreviousTime start of the restricted step * @since 2.2 */ public void setSoftPreviousTime(final double softPreviousTime) { this.softPreviousTime = softPreviousTime; } /** Restrict step range to a limited part of the global step. *

                            * This method can be used to restrict a step and make it appear * as if the original step was smaller. Calling this method * only changes the value returned by {@link #getCurrentTime()}, * it does not change any other property *

                            * @param softCurrentTime end of the restricted step * @since 2.2 */ public void setSoftCurrentTime(final double softCurrentTime) { this.softCurrentTime = softCurrentTime; } /** * Get the previous global grid point time. * @return previous global grid point time */ public double getGlobalPreviousTime() { return globalPreviousTime; } /** * Get the current global grid point time. * @return current global grid point time */ public double getGlobalCurrentTime() { return globalCurrentTime; } /** * Get the previous soft grid point time. * @return previous soft grid point time * @see #setSoftPreviousTime(double) */ public double getPreviousTime() { return softPreviousTime; } /** * Get the current soft grid point time. * @return current soft grid point time * @see #setSoftCurrentTime(double) */ public double getCurrentTime() { return softCurrentTime; } /** {@inheritDoc} */ public double getInterpolatedTime() { return interpolatedTime; } /** {@inheritDoc} */ public void setInterpolatedTime(final double time) { interpolatedTime = time; dirtyState = true; } /** {@inheritDoc} */ public boolean isForward() { return forward; } /** Compute the state and derivatives at the interpolated time. * This is the main processing method that should be implemented by * the derived classes to perform the interpolation. * @param theta normalized interpolation abscissa within the step * (theta is zero at the previous time step and one at the current time step) * @param oneMinusThetaH time gap between the interpolated time and * the current time * @exception MaxCountExceededException if the number of functions evaluations is exceeded */ protected abstract void computeInterpolatedStateAndDerivatives(double theta, double oneMinusThetaH) throws MaxCountExceededException; /** Lazy evaluation of complete interpolated state. * @exception MaxCountExceededException if the number of functions evaluations is exceeded */ private void evaluateCompleteInterpolatedState() throws MaxCountExceededException { // lazy evaluation of the state if (dirtyState) { final double oneMinusThetaH = globalCurrentTime - interpolatedTime; final double theta = (h == 0) ? 0 : (h - oneMinusThetaH) / h; computeInterpolatedStateAndDerivatives(theta, oneMinusThetaH); dirtyState = false; } } /** {@inheritDoc} */ public double[] getInterpolatedState() throws MaxCountExceededException { evaluateCompleteInterpolatedState(); primaryMapper.extractEquationData(interpolatedState, interpolatedPrimaryState); return interpolatedPrimaryState; } /** {@inheritDoc} */ public double[] getInterpolatedDerivatives() throws MaxCountExceededException { evaluateCompleteInterpolatedState(); primaryMapper.extractEquationData(interpolatedDerivatives, interpolatedPrimaryDerivatives); return interpolatedPrimaryDerivatives; } /** {@inheritDoc} */ public double[] getInterpolatedSecondaryState(final int index) throws MaxCountExceededException { evaluateCompleteInterpolatedState(); secondaryMappers[index].extractEquationData(interpolatedState, interpolatedSecondaryState[index]); return interpolatedSecondaryState[index]; } /** {@inheritDoc} */ public double[] getInterpolatedSecondaryDerivatives(final int index) throws MaxCountExceededException { evaluateCompleteInterpolatedState(); secondaryMappers[index].extractEquationData(interpolatedDerivatives, interpolatedSecondaryDerivatives[index]); return interpolatedSecondaryDerivatives[index]; } /** * Finalize the step. *

                            Some embedded Runge-Kutta integrators need fewer functions * evaluations than their counterpart step interpolators. These * interpolators should perform the last evaluations they need by * themselves only if they need them. This method triggers these * extra evaluations. It can be called directly by the user step * handler and it is called automatically if {@link * #setInterpolatedTime} is called.

                            *

                            Once this method has been called, no other * evaluation will be performed on this step. If there is a need to * have some side effects between the step handler and the * differential equations (for example update some data in the * equations once the step has been done), it is advised to call * this method explicitly from the step handler before these side * effects are set up. If the step handler induces no side effect, * then this method can safely be ignored, it will be called * transparently as needed.

                            *

                            Warning: since the step interpolator provided * to the step handler as a parameter of the {@link * StepHandler#handleStep handleStep} is valid only for the duration * of the {@link StepHandler#handleStep handleStep} call, one cannot * simply store a reference and reuse it later. One should first * finalize the instance, then copy this finalized instance into a * new object that can be kept.

                            *

                            This method calls the protected doFinalize method * if it has never been called during this step and set a flag * indicating that it has been called once. It is the * doFinalize method which should perform the evaluations. * This wrapping prevents from calling doFinalize several * times and hence evaluating the differential equations too often. * Therefore, subclasses are not allowed not reimplement it, they * should rather reimplement doFinalize.

                            * @exception MaxCountExceededException if the number of functions evaluations is exceeded */ public final void finalizeStep() throws MaxCountExceededException { if (! finalized) { doFinalize(); finalized = true; } } /** * Really finalize the step. * The default implementation of this method does nothing. * @exception MaxCountExceededException if the number of functions evaluations is exceeded */ protected void doFinalize() throws MaxCountExceededException { } /** {@inheritDoc} */ public abstract void writeExternal(ObjectOutput out) throws IOException; /** {@inheritDoc} */ public abstract void readExternal(ObjectInput in) throws IOException, ClassNotFoundException; /** Save the base state of the instance. * This method performs step finalization if it has not been done * before. * @param out stream where to save the state * @exception IOException in case of write error */ protected void writeBaseExternal(final ObjectOutput out) throws IOException { if (currentState == null) { out.writeInt(-1); } else { out.writeInt(currentState.length); } out.writeDouble(globalPreviousTime); out.writeDouble(globalCurrentTime); out.writeDouble(softPreviousTime); out.writeDouble(softCurrentTime); out.writeDouble(h); out.writeBoolean(forward); out.writeObject(primaryMapper); out.write(secondaryMappers.length); for (final EquationsMapper mapper : secondaryMappers) { out.writeObject(mapper); } if (currentState != null) { for (int i = 0; i < currentState.length; ++i) { out.writeDouble(currentState[i]); } } out.writeDouble(interpolatedTime); // we do not store the interpolated state, // it will be recomputed as needed after reading try { // finalize the step (and don't bother saving the now true flag) finalizeStep(); } catch (MaxCountExceededException mcee) { final IOException ioe = new IOException(mcee.getLocalizedMessage()); ioe.initCause(mcee); throw ioe; } } /** Read the base state of the instance. * This method does neither set the interpolated * time nor state. It is up to the derived class to reset it * properly calling the {@link #setInterpolatedTime} method later, * once all rest of the object state has been set up properly. * @param in stream where to read the state from * @return interpolated time to be set later by the caller * @exception IOException in case of read error * @exception ClassNotFoundException if an equation mapper class * cannot be found */ protected double readBaseExternal(final ObjectInput in) throws IOException, ClassNotFoundException { final int dimension = in.readInt(); globalPreviousTime = in.readDouble(); globalCurrentTime = in.readDouble(); softPreviousTime = in.readDouble(); softCurrentTime = in.readDouble(); h = in.readDouble(); forward = in.readBoolean(); primaryMapper = (EquationsMapper) in.readObject(); secondaryMappers = new EquationsMapper[in.read()]; for (int i = 0; i < secondaryMappers.length; ++i) { secondaryMappers[i] = (EquationsMapper) in.readObject(); } dirtyState = true; if (dimension < 0) { currentState = null; } else { currentState = new double[dimension]; for (int i = 0; i < currentState.length; ++i) { currentState[i] = in.readDouble(); } } // we do NOT handle the interpolated time and state here interpolatedTime = Double.NaN; allocateInterpolatedArrays(dimension); finalized = true; return in.readDouble(); } } ././@LongLink100644 0 0 151 12126630646 10256 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/ode/sampling/NordsieckStepInterpolator.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/ode/sampling/NordsieckStepInterpolator.100644 1750 1750 25047 12126627715 32317 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.ode.sampling; import java.io.IOException; import java.io.ObjectInput; import java.io.ObjectOutput; import java.util.Arrays; import org.apache.commons.math3.exception.MaxCountExceededException; import org.apache.commons.math3.linear.Array2DRowRealMatrix; import org.apache.commons.math3.ode.EquationsMapper; import org.apache.commons.math3.util.FastMath; /** * This class implements an interpolator for integrators using Nordsieck representation. * *

                            This interpolator computes dense output around the current point. * The interpolation equation is based on Taylor series formulas. * * @see org.apache.commons.math3.ode.nonstiff.AdamsBashforthIntegrator * @see org.apache.commons.math3.ode.nonstiff.AdamsMoultonIntegrator * @version $Id: NordsieckStepInterpolator.java 1416643 2012-12-03 19:37:14Z tn $ * @since 2.0 */ public class NordsieckStepInterpolator extends AbstractStepInterpolator { /** Serializable version identifier */ private static final long serialVersionUID = -7179861704951334960L; /** State variation. */ protected double[] stateVariation; /** Step size used in the first scaled derivative and Nordsieck vector. */ private double scalingH; /** Reference time for all arrays. *

                            Sometimes, the reference time is the same as previousTime, * sometimes it is the same as currentTime, so we use a separate * field to avoid any confusion. *

                            */ private double referenceTime; /** First scaled derivative. */ private double[] scaled; /** Nordsieck vector. */ private Array2DRowRealMatrix nordsieck; /** Simple constructor. * This constructor builds an instance that is not usable yet, the * {@link AbstractStepInterpolator#reinitialize} method should be called * before using the instance in order to initialize the internal arrays. This * constructor is used only in order to delay the initialization in * some cases. */ public NordsieckStepInterpolator() { } /** Copy constructor. * @param interpolator interpolator to copy from. The copy is a deep * copy: its arrays are separated from the original arrays of the * instance */ public NordsieckStepInterpolator(final NordsieckStepInterpolator interpolator) { super(interpolator); scalingH = interpolator.scalingH; referenceTime = interpolator.referenceTime; if (interpolator.scaled != null) { scaled = interpolator.scaled.clone(); } if (interpolator.nordsieck != null) { nordsieck = new Array2DRowRealMatrix(interpolator.nordsieck.getDataRef(), true); } if (interpolator.stateVariation != null) { stateVariation = interpolator.stateVariation.clone(); } } /** {@inheritDoc} */ @Override protected StepInterpolator doCopy() { return new NordsieckStepInterpolator(this); } /** Reinitialize the instance. *

                            Beware that all arrays must be references to integrator * arrays, in order to ensure proper update without copy.

                            * @param y reference to the integrator array holding the state at * the end of the step * @param forward integration direction indicator * @param primaryMapper equations mapper for the primary equations set * @param secondaryMappers equations mappers for the secondary equations sets */ @Override public void reinitialize(final double[] y, final boolean forward, final EquationsMapper primaryMapper, final EquationsMapper[] secondaryMappers) { super.reinitialize(y, forward, primaryMapper, secondaryMappers); stateVariation = new double[y.length]; } /** Reinitialize the instance. *

                            Beware that all arrays must be references to integrator * arrays, in order to ensure proper update without copy.

                            * @param time time at which all arrays are defined * @param stepSize step size used in the scaled and nordsieck arrays * @param scaledDerivative reference to the integrator array holding the first * scaled derivative * @param nordsieckVector reference to the integrator matrix holding the * nordsieck vector */ public void reinitialize(final double time, final double stepSize, final double[] scaledDerivative, final Array2DRowRealMatrix nordsieckVector) { this.referenceTime = time; this.scalingH = stepSize; this.scaled = scaledDerivative; this.nordsieck = nordsieckVector; // make sure the state and derivatives will depend on the new arrays setInterpolatedTime(getInterpolatedTime()); } /** Rescale the instance. *

                            Since the scaled and Nordiseck arrays are shared with the caller, * this method has the side effect of rescaling this arrays in the caller too.

                            * @param stepSize new step size to use in the scaled and nordsieck arrays */ public void rescale(final double stepSize) { final double ratio = stepSize / scalingH; for (int i = 0; i < scaled.length; ++i) { scaled[i] *= ratio; } final double[][] nData = nordsieck.getDataRef(); double power = ratio; for (int i = 0; i < nData.length; ++i) { power *= ratio; final double[] nDataI = nData[i]; for (int j = 0; j < nDataI.length; ++j) { nDataI[j] *= power; } } scalingH = stepSize; } /** * Get the state vector variation from current to interpolated state. *

                            This method is aimed at computing y(tinterpolation) * -y(tcurrent) accurately by avoiding the cancellation errors * that would occur if the subtraction were performed explicitly.

                            *

                            The returned vector is a reference to a reused array, so * it should not be modified and it should be copied if it needs * to be preserved across several calls.

                            * @return state vector at time {@link #getInterpolatedTime} * @see #getInterpolatedDerivatives() * @exception MaxCountExceededException if the number of functions evaluations is exceeded */ public double[] getInterpolatedStateVariation() throws MaxCountExceededException { // compute and ignore interpolated state // to make sure state variation is computed as a side effect getInterpolatedState(); return stateVariation; } /** {@inheritDoc} */ @Override protected void computeInterpolatedStateAndDerivatives(final double theta, final double oneMinusThetaH) { final double x = interpolatedTime - referenceTime; final double normalizedAbscissa = x / scalingH; Arrays.fill(stateVariation, 0.0); Arrays.fill(interpolatedDerivatives, 0.0); // apply Taylor formula from high order to low order, // for the sake of numerical accuracy final double[][] nData = nordsieck.getDataRef(); for (int i = nData.length - 1; i >= 0; --i) { final int order = i + 2; final double[] nDataI = nData[i]; final double power = FastMath.pow(normalizedAbscissa, order); for (int j = 0; j < nDataI.length; ++j) { final double d = nDataI[j] * power; stateVariation[j] += d; interpolatedDerivatives[j] += order * d; } } for (int j = 0; j < currentState.length; ++j) { stateVariation[j] += scaled[j] * normalizedAbscissa; interpolatedState[j] = currentState[j] + stateVariation[j]; interpolatedDerivatives[j] = (interpolatedDerivatives[j] + scaled[j] * normalizedAbscissa) / x; } } /** {@inheritDoc} */ @Override public void writeExternal(final ObjectOutput out) throws IOException { // save the state of the base class writeBaseExternal(out); // save the local attributes out.writeDouble(scalingH); out.writeDouble(referenceTime); final int n = (currentState == null) ? -1 : currentState.length; if (scaled == null) { out.writeBoolean(false); } else { out.writeBoolean(true); for (int j = 0; j < n; ++j) { out.writeDouble(scaled[j]); } } if (nordsieck == null) { out.writeBoolean(false); } else { out.writeBoolean(true); out.writeObject(nordsieck); } // we don't save state variation, it will be recomputed } /** {@inheritDoc} */ @Override public void readExternal(final ObjectInput in) throws IOException, ClassNotFoundException { // read the base class final double t = readBaseExternal(in); // read the local attributes scalingH = in.readDouble(); referenceTime = in.readDouble(); final int n = (currentState == null) ? -1 : currentState.length; final boolean hasScaled = in.readBoolean(); if (hasScaled) { scaled = new double[n]; for (int j = 0; j < n; ++j) { scaled[j] = in.readDouble(); } } else { scaled = null; } final boolean hasNordsieck = in.readBoolean(); if (hasNordsieck) { nordsieck = (Array2DRowRealMatrix) in.readObject(); } else { nordsieck = null; } if (hasScaled && hasNordsieck) { // we can now set the interpolated time and state stateVariation = new double[n]; setInterpolatedTime(t); } else { stateVariation = null; } } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/ode/MultistepIntegrator.java100644 1750 1750 44600 12126627716 30211 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.ode; import org.apache.commons.math3.exception.DimensionMismatchException; import org.apache.commons.math3.exception.MaxCountExceededException; import org.apache.commons.math3.exception.NoBracketingException; import org.apache.commons.math3.exception.NumberIsTooSmallException; import org.apache.commons.math3.exception.util.LocalizedFormats; import org.apache.commons.math3.linear.Array2DRowRealMatrix; import org.apache.commons.math3.ode.nonstiff.AdaptiveStepsizeIntegrator; import org.apache.commons.math3.ode.nonstiff.DormandPrince853Integrator; import org.apache.commons.math3.ode.sampling.StepHandler; import org.apache.commons.math3.ode.sampling.StepInterpolator; import org.apache.commons.math3.util.FastMath; /** * This class is the base class for multistep integrators for Ordinary * Differential Equations. *

                            We define scaled derivatives si(n) at step n as: *

                             * s1(n) = h y'n for first derivative
                             * s2(n) = h2/2 y''n for second derivative
                             * s3(n) = h3/6 y'''n for third derivative
                             * ...
                             * sk(n) = hk/k! y(k)n for kth derivative
                             * 

                            *

                            Rather than storing several previous steps separately, this implementation uses * the Nordsieck vector with higher degrees scaled derivatives all taken at the same * step (yn, s1(n) and rn) where rn is defined as: *

                             * rn = [ s2(n), s3(n) ... sk(n) ]T
                             * 
                            * (we omit the k index in the notation for clarity)

                            *

                            * Multistep integrators with Nordsieck representation are highly sensitive to * large step changes because when the step is multiplied by factor a, the * kth component of the Nordsieck vector is multiplied by ak * and the last components are the least accurate ones. The default max growth * factor is therefore set to a quite low value: 21/order. *

                            * * @see org.apache.commons.math3.ode.nonstiff.AdamsBashforthIntegrator * @see org.apache.commons.math3.ode.nonstiff.AdamsMoultonIntegrator * @version $Id: MultistepIntegrator.java 1463684 2013-04-02 19:04:13Z luc $ * @since 2.0 */ public abstract class MultistepIntegrator extends AdaptiveStepsizeIntegrator { /** First scaled derivative (h y'). */ protected double[] scaled; /** Nordsieck matrix of the higher scaled derivatives. *

                            (h2/2 y'', h3/6 y''' ..., hk/k! y(k))

                            */ protected Array2DRowRealMatrix nordsieck; /** Starter integrator. */ private FirstOrderIntegrator starter; /** Number of steps of the multistep method (excluding the one being computed). */ private final int nSteps; /** Stepsize control exponent. */ private double exp; /** Safety factor for stepsize control. */ private double safety; /** Minimal reduction factor for stepsize control. */ private double minReduction; /** Maximal growth factor for stepsize control. */ private double maxGrowth; /** * Build a multistep integrator with the given stepsize bounds. *

                            The default starter integrator is set to the {@link * DormandPrince853Integrator Dormand-Prince 8(5,3)} integrator with * some defaults settings.

                            *

                            * The default max growth factor is set to a quite low value: 21/order. *

                            * @param name name of the method * @param nSteps number of steps of the multistep method * (excluding the one being computed) * @param order order of the method * @param minStep minimal step (must be positive even for backward * integration), the last step can be smaller than this * @param maxStep maximal step (must be positive even for backward * integration) * @param scalAbsoluteTolerance allowed absolute error * @param scalRelativeTolerance allowed relative error * @exception NumberIsTooSmallException if number of steps is smaller than 2 */ protected MultistepIntegrator(final String name, final int nSteps, final int order, final double minStep, final double maxStep, final double scalAbsoluteTolerance, final double scalRelativeTolerance) throws NumberIsTooSmallException { super(name, minStep, maxStep, scalAbsoluteTolerance, scalRelativeTolerance); if (nSteps < 2) { throw new NumberIsTooSmallException( LocalizedFormats.INTEGRATION_METHOD_NEEDS_AT_LEAST_TWO_PREVIOUS_POINTS, nSteps, 2, true); } starter = new DormandPrince853Integrator(minStep, maxStep, scalAbsoluteTolerance, scalRelativeTolerance); this.nSteps = nSteps; exp = -1.0 / order; // set the default values of the algorithm control parameters setSafety(0.9); setMinReduction(0.2); setMaxGrowth(FastMath.pow(2.0, -exp)); } /** * Build a multistep integrator with the given stepsize bounds. *

                            The default starter integrator is set to the {@link * DormandPrince853Integrator Dormand-Prince 8(5,3)} integrator with * some defaults settings.

                            *

                            * The default max growth factor is set to a quite low value: 21/order. *

                            * @param name name of the method * @param nSteps number of steps of the multistep method * (excluding the one being computed) * @param order order of the method * @param minStep minimal step (must be positive even for backward * integration), the last step can be smaller than this * @param maxStep maximal step (must be positive even for backward * integration) * @param vecAbsoluteTolerance allowed absolute error * @param vecRelativeTolerance allowed relative error */ protected MultistepIntegrator(final String name, final int nSteps, final int order, final double minStep, final double maxStep, final double[] vecAbsoluteTolerance, final double[] vecRelativeTolerance) { super(name, minStep, maxStep, vecAbsoluteTolerance, vecRelativeTolerance); starter = new DormandPrince853Integrator(minStep, maxStep, vecAbsoluteTolerance, vecRelativeTolerance); this.nSteps = nSteps; exp = -1.0 / order; // set the default values of the algorithm control parameters setSafety(0.9); setMinReduction(0.2); setMaxGrowth(FastMath.pow(2.0, -exp)); } /** * Get the starter integrator. * @return starter integrator */ public ODEIntegrator getStarterIntegrator() { return starter; } /** * Set the starter integrator. *

                            The various step and event handlers for this starter integrator * will be managed automatically by the multi-step integrator. Any * user configuration for these elements will be cleared before use.

                            * @param starterIntegrator starter integrator */ public void setStarterIntegrator(FirstOrderIntegrator starterIntegrator) { this.starter = starterIntegrator; } /** Start the integration. *

                            This method computes one step using the underlying starter integrator, * and initializes the Nordsieck vector at step start. The starter integrator * purpose is only to establish initial conditions, it does not really change * time by itself. The top level multistep integrator remains in charge of * handling time propagation and events handling as it will starts its own * computation right from the beginning. In a sense, the starter integrator * can be seen as a dummy one and so it will never trigger any user event nor * call any user step handler.

                            * @param t0 initial time * @param y0 initial value of the state vector at t0 * @param t target time for the integration * (can be set to a value smaller than t0 for backward integration) * @exception DimensionMismatchException if arrays dimension do not match equations settings * @exception NumberIsTooSmallException if integration step is too small * @exception MaxCountExceededException if the number of functions evaluations is exceeded * @exception NoBracketingException if the location of an event cannot be bracketed */ protected void start(final double t0, final double[] y0, final double t) throws DimensionMismatchException, NumberIsTooSmallException, MaxCountExceededException, NoBracketingException { // make sure NO user event nor user step handler is triggered, // this is the task of the top level integrator, not the task // of the starter integrator starter.clearEventHandlers(); starter.clearStepHandlers(); // set up one specific step handler to extract initial Nordsieck vector starter.addStepHandler(new NordsieckInitializer(nSteps, y0.length)); // start integration, expecting a InitializationCompletedMarkerException try { if (starter instanceof AbstractIntegrator) { ((AbstractIntegrator) starter).integrate(getExpandable(), t); } else { starter.integrate(new FirstOrderDifferentialEquations() { /** {@inheritDoc} */ public int getDimension() { return getExpandable().getTotalDimension(); } /** {@inheritDoc} */ public void computeDerivatives(double t, double[] y, double[] yDot) { getExpandable().computeDerivatives(t, y, yDot); } }, t0, y0, t, new double[y0.length]); } } catch (InitializationCompletedMarkerException icme) { // NOPMD // this is the expected nominal interruption of the start integrator // count the evaluations used by the starter getEvaluationsCounter().incrementCount(starter.getEvaluations()); } // remove the specific step handler starter.clearStepHandlers(); } /** Initialize the high order scaled derivatives at step start. * @param h step size to use for scaling * @param t first steps times * @param y first steps states * @param yDot first steps derivatives * @return Nordieck vector at first step (h2/2 y''n, * h3/6 y'''n ... hk/k! y(k)n) */ protected abstract Array2DRowRealMatrix initializeHighOrderDerivatives(final double h, final double[] t, final double[][] y, final double[][] yDot); /** Get the minimal reduction factor for stepsize control. * @return minimal reduction factor */ public double getMinReduction() { return minReduction; } /** Set the minimal reduction factor for stepsize control. * @param minReduction minimal reduction factor */ public void setMinReduction(final double minReduction) { this.minReduction = minReduction; } /** Get the maximal growth factor for stepsize control. * @return maximal growth factor */ public double getMaxGrowth() { return maxGrowth; } /** Set the maximal growth factor for stepsize control. * @param maxGrowth maximal growth factor */ public void setMaxGrowth(final double maxGrowth) { this.maxGrowth = maxGrowth; } /** Get the safety factor for stepsize control. * @return safety factor */ public double getSafety() { return safety; } /** Set the safety factor for stepsize control. * @param safety safety factor */ public void setSafety(final double safety) { this.safety = safety; } /** Compute step grow/shrink factor according to normalized error. * @param error normalized error of the current step * @return grow/shrink factor for next step */ protected double computeStepGrowShrinkFactor(final double error) { return FastMath.min(maxGrowth, FastMath.max(minReduction, safety * FastMath.pow(error, exp))); } /** Transformer used to convert the first step to Nordsieck representation. */ public interface NordsieckTransformer { /** Initialize the high order scaled derivatives at step start. * @param h step size to use for scaling * @param t first steps times * @param y first steps states * @param yDot first steps derivatives * @return Nordieck vector at first step (h2/2 y''n, * h3/6 y'''n ... hk/k! y(k)n) */ Array2DRowRealMatrix initializeHighOrderDerivatives(final double h, final double[] t, final double[][] y, final double[][] yDot); } /** Specialized step handler storing the first step. */ private class NordsieckInitializer implements StepHandler { /** Steps counter. */ private int count; /** First steps times. */ private final double[] t; /** First steps states. */ private final double[][] y; /** First steps derivatives. */ private final double[][] yDot; /** Simple constructor. * @param nSteps number of steps of the multistep method (excluding the one being computed) * @param n problem dimension */ public NordsieckInitializer(final int nSteps, final int n) { this.count = 0; this.t = new double[nSteps]; this.y = new double[nSteps][n]; this.yDot = new double[nSteps][n]; } /** {@inheritDoc} */ public void handleStep(StepInterpolator interpolator, boolean isLast) throws MaxCountExceededException { final double prev = interpolator.getPreviousTime(); final double curr = interpolator.getCurrentTime(); if (count == 0) { // first step, we need to store also the beginning of the step interpolator.setInterpolatedTime(prev); t[0] = prev; final ExpandableStatefulODE expandable = getExpandable(); final EquationsMapper primary = expandable.getPrimaryMapper(); primary.insertEquationData(interpolator.getInterpolatedState(), y[count]); primary.insertEquationData(interpolator.getInterpolatedDerivatives(), yDot[count]); int index = 0; for (final EquationsMapper secondary : expandable.getSecondaryMappers()) { secondary.insertEquationData(interpolator.getInterpolatedSecondaryState(index), y[count]); secondary.insertEquationData(interpolator.getInterpolatedSecondaryDerivatives(index), yDot[count]); ++index; } } // store the end of the step ++count; interpolator.setInterpolatedTime(curr); t[count] = curr; final ExpandableStatefulODE expandable = getExpandable(); final EquationsMapper primary = expandable.getPrimaryMapper(); primary.insertEquationData(interpolator.getInterpolatedState(), y[count]); primary.insertEquationData(interpolator.getInterpolatedDerivatives(), yDot[count]); int index = 0; for (final EquationsMapper secondary : expandable.getSecondaryMappers()) { secondary.insertEquationData(interpolator.getInterpolatedSecondaryState(index), y[count]); secondary.insertEquationData(interpolator.getInterpolatedSecondaryDerivatives(index), yDot[count]); ++index; } if (count == t.length - 1) { // this was the last step we needed, we can compute the derivatives stepStart = t[0]; stepSize = (t[t.length - 1] - t[0]) / (t.length - 1); // first scaled derivative scaled = yDot[0].clone(); for (int j = 0; j < scaled.length; ++j) { scaled[j] *= stepSize; } // higher order derivatives nordsieck = initializeHighOrderDerivatives(stepSize, t, y, yDot); // stop the integrator now that all needed steps have been handled throw new InitializationCompletedMarkerException(); } } /** {@inheritDoc} */ public void init(double t0, double[] y0, double time) { // nothing to do } } /** Marker exception used ONLY to stop the starter integrator after first step. */ private static class InitializationCompletedMarkerException extends RuntimeException { /** Serializable version identifier. */ private static final long serialVersionUID = -1914085471038046418L; /** Simple constructor. */ public InitializationCompletedMarkerException() { super((Throwable) null); } } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/ode/FirstOrderIntegrator.java100644 1750 1750 6414 12126627716 30267 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.ode; import org.apache.commons.math3.exception.DimensionMismatchException; import org.apache.commons.math3.exception.MaxCountExceededException; import org.apache.commons.math3.exception.NoBracketingException; import org.apache.commons.math3.exception.NumberIsTooSmallException; /** This interface represents a first order integrator for * differential equations. *

                            The classes which are devoted to solve first order differential * equations should implement this interface. The problems which can * be handled should implement the {@link * FirstOrderDifferentialEquations} interface.

                            * * @see FirstOrderDifferentialEquations * @see org.apache.commons.math3.ode.sampling.StepHandler * @see org.apache.commons.math3.ode.events.EventHandler * @version $Id: FirstOrderIntegrator.java 1416643 2012-12-03 19:37:14Z tn $ * @since 1.2 */ public interface FirstOrderIntegrator extends ODEIntegrator { /** Integrate the differential equations up to the given time. *

                            This method solves an Initial Value Problem (IVP).

                            *

                            Since this method stores some internal state variables made * available in its public interface during integration ({@link * #getCurrentSignedStepsize()}), it is not thread-safe.

                            * @param equations differential equations to integrate * @param t0 initial time * @param y0 initial value of the state vector at t0 * @param t target time for the integration * (can be set to a value smaller than t0 for backward integration) * @param y placeholder where to put the state vector at each successful * step (and hence at the end of integration), can be the same object as y0 * @return stop time, will be the same as target time if integration reached its * target, but may be different if some {@link * org.apache.commons.math3.ode.events.EventHandler} stops it at some point. * @exception DimensionMismatchException if arrays dimension do not match equations settings * @exception NumberIsTooSmallException if integration step is too small * @exception MaxCountExceededException if the number of functions evaluations is exceeded * @exception NoBracketingException if the location of an event cannot be bracketed */ double integrate (FirstOrderDifferentialEquations equations, double t0, double[] y0, double t, double[] y) throws DimensionMismatchException, NumberIsTooSmallException, MaxCountExceededException, NoBracketingException; } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/ode/Parameterizable.java100644 1750 1750 3007 12126627715 27246 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.ode; import java.util.Collection; /** This interface enables to process any parameterizable object. * * @version $Id: Parameterizable.java 1416643 2012-12-03 19:37:14Z tn $ * @since 3.0 */ public interface Parameterizable { /** Get the names of the supported parameters. * @return parameters names * @see #isSupported(String) */ Collection getParametersNames(); /** Check if a parameter is supported. *

                            Supported parameters are those listed by {@link #getParametersNames()}.

                            * @param name parameter name to check * @return true if the parameter is supported * @see #getParametersNames() */ boolean isSupported(String name); } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/ode/AbstractIntegrator.java100644 1750 1750 43457 12126627715 27776 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.ode; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; import java.util.Comparator; import java.util.Iterator; import java.util.List; import java.util.SortedSet; import java.util.TreeSet; import org.apache.commons.math3.analysis.solvers.BracketingNthOrderBrentSolver; import org.apache.commons.math3.analysis.solvers.UnivariateSolver; import org.apache.commons.math3.exception.DimensionMismatchException; import org.apache.commons.math3.exception.MaxCountExceededException; import org.apache.commons.math3.exception.NoBracketingException; import org.apache.commons.math3.exception.NumberIsTooSmallException; import org.apache.commons.math3.exception.util.LocalizedFormats; import org.apache.commons.math3.ode.events.EventHandler; import org.apache.commons.math3.ode.events.EventState; import org.apache.commons.math3.ode.sampling.AbstractStepInterpolator; import org.apache.commons.math3.ode.sampling.StepHandler; import org.apache.commons.math3.util.FastMath; import org.apache.commons.math3.util.Incrementor; import org.apache.commons.math3.util.Precision; /** * Base class managing common boilerplate for all integrators. * @version $Id: AbstractIntegrator.java 1463684 2013-04-02 19:04:13Z luc $ * @since 2.0 */ public abstract class AbstractIntegrator implements FirstOrderIntegrator { /** Step handler. */ protected Collection stepHandlers; /** Current step start time. */ protected double stepStart; /** Current stepsize. */ protected double stepSize; /** Indicator for last step. */ protected boolean isLastStep; /** Indicator that a state or derivative reset was triggered by some event. */ protected boolean resetOccurred; /** Events states. */ private Collection eventsStates; /** Initialization indicator of events states. */ private boolean statesInitialized; /** Name of the method. */ private final String name; /** Counter for number of evaluations. */ private Incrementor evaluations; /** Differential equations to integrate. */ private transient ExpandableStatefulODE expandable; /** Build an instance. * @param name name of the method */ public AbstractIntegrator(final String name) { this.name = name; stepHandlers = new ArrayList(); stepStart = Double.NaN; stepSize = Double.NaN; eventsStates = new ArrayList(); statesInitialized = false; evaluations = new Incrementor(); setMaxEvaluations(-1); evaluations.resetCount(); } /** Build an instance with a null name. */ protected AbstractIntegrator() { this(null); } /** {@inheritDoc} */ public String getName() { return name; } /** {@inheritDoc} */ public void addStepHandler(final StepHandler handler) { stepHandlers.add(handler); } /** {@inheritDoc} */ public Collection getStepHandlers() { return Collections.unmodifiableCollection(stepHandlers); } /** {@inheritDoc} */ public void clearStepHandlers() { stepHandlers.clear(); } /** {@inheritDoc} */ public void addEventHandler(final EventHandler handler, final double maxCheckInterval, final double convergence, final int maxIterationCount) { addEventHandler(handler, maxCheckInterval, convergence, maxIterationCount, new BracketingNthOrderBrentSolver(convergence, 5)); } /** {@inheritDoc} */ public void addEventHandler(final EventHandler handler, final double maxCheckInterval, final double convergence, final int maxIterationCount, final UnivariateSolver solver) { eventsStates.add(new EventState(handler, maxCheckInterval, convergence, maxIterationCount, solver)); } /** {@inheritDoc} */ public Collection getEventHandlers() { final List list = new ArrayList(); for (EventState state : eventsStates) { list.add(state.getEventHandler()); } return Collections.unmodifiableCollection(list); } /** {@inheritDoc} */ public void clearEventHandlers() { eventsStates.clear(); } /** {@inheritDoc} */ public double getCurrentStepStart() { return stepStart; } /** {@inheritDoc} */ public double getCurrentSignedStepsize() { return stepSize; } /** {@inheritDoc} */ public void setMaxEvaluations(int maxEvaluations) { evaluations.setMaximalCount((maxEvaluations < 0) ? Integer.MAX_VALUE : maxEvaluations); } /** {@inheritDoc} */ public int getMaxEvaluations() { return evaluations.getMaximalCount(); } /** {@inheritDoc} */ public int getEvaluations() { return evaluations.getCount(); } /** Prepare the start of an integration. * @param t0 start value of the independent time variable * @param y0 array containing the start value of the state vector * @param t target time for the integration */ protected void initIntegration(final double t0, final double[] y0, final double t) { evaluations.resetCount(); for (final EventState state : eventsStates) { state.getEventHandler().init(t0, y0, t); } for (StepHandler handler : stepHandlers) { handler.init(t0, y0, t); } setStateInitialized(false); } /** Set the equations. * @param equations equations to set */ protected void setEquations(final ExpandableStatefulODE equations) { this.expandable = equations; } /** Get the differential equations to integrate. * @return differential equations to integrate * @since 3.2 */ protected ExpandableStatefulODE getExpandable() { return expandable; } /** Get the evaluations counter. * @return evaluations counter * @since 3.2 */ protected Incrementor getEvaluationsCounter() { return evaluations; } /** {@inheritDoc} */ public double integrate(final FirstOrderDifferentialEquations equations, final double t0, final double[] y0, final double t, final double[] y) throws DimensionMismatchException, NumberIsTooSmallException, MaxCountExceededException, NoBracketingException { if (y0.length != equations.getDimension()) { throw new DimensionMismatchException(y0.length, equations.getDimension()); } if (y.length != equations.getDimension()) { throw new DimensionMismatchException(y.length, equations.getDimension()); } // prepare expandable stateful equations final ExpandableStatefulODE expandableODE = new ExpandableStatefulODE(equations); expandableODE.setTime(t0); expandableODE.setPrimaryState(y0); // perform integration integrate(expandableODE, t); // extract results back from the stateful equations System.arraycopy(expandableODE.getPrimaryState(), 0, y, 0, y.length); return expandableODE.getTime(); } /** Integrate a set of differential equations up to the given time. *

                            This method solves an Initial Value Problem (IVP).

                            *

                            The set of differential equations is composed of a main set, which * can be extended by some sets of secondary equations. The set of * equations must be already set up with initial time and partial states. * At integration completion, the final time and partial states will be * available in the same object.

                            *

                            Since this method stores some internal state variables made * available in its public interface during integration ({@link * #getCurrentSignedStepsize()}), it is not thread-safe.

                            * @param equations complete set of differential equations to integrate * @param t target time for the integration * (can be set to a value smaller than t0 for backward integration) * @exception NumberIsTooSmallException if integration step is too small * @throws DimensionMismatchException if the dimension of the complete state does not * match the complete equations sets dimension * @exception MaxCountExceededException if the number of functions evaluations is exceeded * @exception NoBracketingException if the location of an event cannot be bracketed */ public abstract void integrate(ExpandableStatefulODE equations, double t) throws NumberIsTooSmallException, DimensionMismatchException, MaxCountExceededException, NoBracketingException; /** Compute the derivatives and check the number of evaluations. * @param t current value of the independent time variable * @param y array containing the current value of the state vector * @param yDot placeholder array where to put the time derivative of the state vector * @exception MaxCountExceededException if the number of functions evaluations is exceeded * @exception DimensionMismatchException if arrays dimensions do not match equations settings */ public void computeDerivatives(final double t, final double[] y, final double[] yDot) throws MaxCountExceededException, DimensionMismatchException { evaluations.incrementCount(); expandable.computeDerivatives(t, y, yDot); } /** Set the stateInitialized flag. *

                            This method must be called by integrators with the value * {@code false} before they start integration, so a proper lazy * initialization is done automatically on the first step.

                            * @param stateInitialized new value for the flag * @since 2.2 */ protected void setStateInitialized(final boolean stateInitialized) { this.statesInitialized = stateInitialized; } /** Accept a step, triggering events and step handlers. * @param interpolator step interpolator * @param y state vector at step end time, must be reset if an event * asks for resetting or if an events stops integration during the step * @param yDot placeholder array where to put the time derivative of the state vector * @param tEnd final integration time * @return time at end of step * @exception MaxCountExceededException if the interpolator throws one because * the number of functions evaluations is exceeded * @exception NoBracketingException if the location of an event cannot be bracketed * @exception DimensionMismatchException if arrays dimensions do not match equations settings * @since 2.2 */ protected double acceptStep(final AbstractStepInterpolator interpolator, final double[] y, final double[] yDot, final double tEnd) throws MaxCountExceededException, DimensionMismatchException, NoBracketingException { double previousT = interpolator.getGlobalPreviousTime(); final double currentT = interpolator.getGlobalCurrentTime(); // initialize the events states if needed if (! statesInitialized) { for (EventState state : eventsStates) { state.reinitializeBegin(interpolator); } statesInitialized = true; } // search for next events that may occur during the step final int orderingSign = interpolator.isForward() ? +1 : -1; SortedSet occuringEvents = new TreeSet(new Comparator() { /** {@inheritDoc} */ public int compare(EventState es0, EventState es1) { return orderingSign * Double.compare(es0.getEventTime(), es1.getEventTime()); } }); for (final EventState state : eventsStates) { if (state.evaluateStep(interpolator)) { // the event occurs during the current step occuringEvents.add(state); } } while (!occuringEvents.isEmpty()) { // handle the chronologically first event final Iterator iterator = occuringEvents.iterator(); final EventState currentEvent = iterator.next(); iterator.remove(); // restrict the interpolator to the first part of the step, up to the event final double eventT = currentEvent.getEventTime(); interpolator.setSoftPreviousTime(previousT); interpolator.setSoftCurrentTime(eventT); // get state at event time interpolator.setInterpolatedTime(eventT); final double[] eventYPrimary = interpolator.getInterpolatedState().clone(); final double[] eventYComplete = new double[y.length]; expandable.getPrimaryMapper().insertEquationData(interpolator.getInterpolatedState(), eventYComplete); int index = 0; for (EquationsMapper secondary : expandable.getSecondaryMappers()) { secondary.insertEquationData(interpolator.getInterpolatedSecondaryState(index++), eventYComplete); } // advance all event states to current time for (final EventState state : eventsStates) { state.stepAccepted(eventT, eventYPrimary); isLastStep = isLastStep || state.stop(); } // handle the first part of the step, up to the event for (final StepHandler handler : stepHandlers) { handler.handleStep(interpolator, isLastStep); } if (isLastStep) { // the event asked to stop integration System.arraycopy(eventYComplete, 0, y, 0, y.length); return eventT; } boolean needReset = false; for (final EventState state : eventsStates) { needReset = needReset || state.reset(eventT, eventYComplete); } if (needReset) { // some event handler has triggered changes that // invalidate the derivatives, we need to recompute them interpolator.setInterpolatedTime(eventT); System.arraycopy(eventYComplete, 0, y, 0, y.length); computeDerivatives(eventT, y, yDot); resetOccurred = true; return eventT; } // prepare handling of the remaining part of the step previousT = eventT; interpolator.setSoftPreviousTime(eventT); interpolator.setSoftCurrentTime(currentT); // check if the same event occurs again in the remaining part of the step if (currentEvent.evaluateStep(interpolator)) { // the event occurs during the current step occuringEvents.add(currentEvent); } } // last part of the step, after the last event interpolator.setInterpolatedTime(currentT); final double[] currentY = interpolator.getInterpolatedState(); for (final EventState state : eventsStates) { state.stepAccepted(currentT, currentY); isLastStep = isLastStep || state.stop(); } isLastStep = isLastStep || Precision.equals(currentT, tEnd, 1); // handle the remaining part of the step, after all events if any for (StepHandler handler : stepHandlers) { handler.handleStep(interpolator, isLastStep); } return currentT; } /** Check the integration span. * @param equations set of differential equations * @param t target time for the integration * @exception NumberIsTooSmallException if integration span is too small * @exception DimensionMismatchException if adaptive step size integrators * tolerance arrays dimensions are not compatible with equations settings */ protected void sanityChecks(final ExpandableStatefulODE equations, final double t) throws NumberIsTooSmallException, DimensionMismatchException { final double threshold = 1000 * FastMath.ulp(FastMath.max(FastMath.abs(equations.getTime()), FastMath.abs(t))); final double dt = FastMath.abs(equations.getTime() - t); if (dt <= threshold) { throw new NumberIsTooSmallException(LocalizedFormats.TOO_SMALL_INTEGRATION_INTERVAL, dt, threshold, false); } } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/ode/ParameterJacobianWrapper.java100644 1750 1750 7474 12126627716 31064 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.ode; import java.util.Arrays; import java.util.Collection; import java.util.HashMap; import java.util.Map; import org.apache.commons.math3.exception.DimensionMismatchException; import org.apache.commons.math3.exception.MaxCountExceededException; /** Wrapper class to compute Jacobian matrices by finite differences for ODE * which do not compute them by themselves. * * @version $Id: ParameterJacobianWrapper.java 1416643 2012-12-03 19:37:14Z tn $ * @since 3.0 */ class ParameterJacobianWrapper implements ParameterJacobianProvider { /** Main ODE set. */ private final FirstOrderDifferentialEquations fode; /** Raw ODE without Jacobian computation skill to be wrapped into a ParameterJacobianProvider. */ private final ParameterizedODE pode; /** Steps for finite difference computation of the Jacobian df/dp w.r.t. parameters. */ private final Map hParam; /** Wrap a {@link ParameterizedODE} into a {@link ParameterJacobianProvider}. * @param fode main first order differential equations set * @param pode secondary problem, without parameter Jacobian computation skill * @param paramsAndSteps parameters and steps to compute the Jacobians df/dp * @see JacobianMatrices#setParameterStep(String, double) */ public ParameterJacobianWrapper(final FirstOrderDifferentialEquations fode, final ParameterizedODE pode, final ParameterConfiguration[] paramsAndSteps) { this.fode = fode; this.pode = pode; this.hParam = new HashMap(); // set up parameters for jacobian computation for (final ParameterConfiguration param : paramsAndSteps) { final String name = param.getParameterName(); if (pode.isSupported(name)) { hParam.put(name, param.getHP()); } } } /** {@inheritDoc} */ public Collection getParametersNames() { return pode.getParametersNames(); } /** {@inheritDoc} */ public boolean isSupported(String name) { return pode.isSupported(name); } /** {@inheritDoc} */ public void computeParameterJacobian(double t, double[] y, double[] yDot, String paramName, double[] dFdP) throws DimensionMismatchException, MaxCountExceededException { final int n = fode.getDimension(); if (pode.isSupported(paramName)) { final double[] tmpDot = new double[n]; // compute the jacobian df/dp w.r.t. parameter final double p = pode.getParameter(paramName); final double hP = hParam.get(paramName); pode.setParameter(paramName, p + hP); fode.computeDerivatives(t, y, tmpDot); for (int i = 0; i < n; ++i) { dFdP[i] = (tmpDot[i] - yDot[i]) / hP; } pode.setParameter(paramName, p); } else { Arrays.fill(dFdP, 0, n, 0.0); } } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/ode/ODEIntegrator.java100644 1750 1750 14754 12126627715 26640 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.ode; import java.util.Collection; import org.apache.commons.math3.analysis.solvers.UnivariateSolver; import org.apache.commons.math3.ode.events.EventHandler; import org.apache.commons.math3.ode.sampling.StepHandler; /** * This interface defines the common parts shared by integrators * for first and second order differential equations. * @see FirstOrderIntegrator * @see SecondOrderIntegrator * @version $Id: ODEIntegrator.java 1416643 2012-12-03 19:37:14Z tn $ * @since 2.0 */ public interface ODEIntegrator { /** Get the name of the method. * @return name of the method */ String getName(); /** Add a step handler to this integrator. *

                            The handler will be called by the integrator for each accepted * step.

                            * @param handler handler for the accepted steps * @see #getStepHandlers() * @see #clearStepHandlers() * @since 2.0 */ void addStepHandler(StepHandler handler); /** Get all the step handlers that have been added to the integrator. * @return an unmodifiable collection of the added events handlers * @see #addStepHandler(StepHandler) * @see #clearStepHandlers() * @since 2.0 */ Collection getStepHandlers(); /** Remove all the step handlers that have been added to the integrator. * @see #addStepHandler(StepHandler) * @see #getStepHandlers() * @since 2.0 */ void clearStepHandlers(); /** Add an event handler to the integrator. * Uses a default {@link UnivariateSolver} * with an absolute accuracy equal to the given convergence threshold, * as root-finding algorithm to detect the state events. * @param handler event handler * @param maxCheckInterval maximal time interval between switching * function checks (this interval prevents missing sign changes in * case the integration steps becomes very large) * @param convergence convergence threshold in the event time search * @param maxIterationCount upper limit of the iteration count in * the event time search * @see #getEventHandlers() * @see #clearEventHandlers() */ void addEventHandler(EventHandler handler, double maxCheckInterval, double convergence, int maxIterationCount); /** Add an event handler to the integrator. * @param handler event handler * @param maxCheckInterval maximal time interval between switching * function checks (this interval prevents missing sign changes in * case the integration steps becomes very large) * @param convergence convergence threshold in the event time search * @param maxIterationCount upper limit of the iteration count in * the event time search * @param solver The root-finding algorithm to use to detect the state * events. * @see #getEventHandlers() * @see #clearEventHandlers() */ void addEventHandler(EventHandler handler, double maxCheckInterval, double convergence, int maxIterationCount, UnivariateSolver solver); /** Get all the event handlers that have been added to the integrator. * @return an unmodifiable collection of the added events handlers * @see #addEventHandler(EventHandler, double, double, int) * @see #clearEventHandlers() */ Collection getEventHandlers(); /** Remove all the event handlers that have been added to the integrator. * @see #addEventHandler(EventHandler, double, double, int) * @see #getEventHandlers() */ void clearEventHandlers(); /** Get the current value of the step start time ti. *

                            This method can be called during integration (typically by * the object implementing the {@link FirstOrderDifferentialEquations * differential equations} problem) if the value of the current step that * is attempted is needed.

                            *

                            The result is undefined if the method is called outside of * calls to integrate.

                            * @return current value of the step start time ti */ double getCurrentStepStart(); /** Get the current signed value of the integration stepsize. *

                            This method can be called during integration (typically by * the object implementing the {@link FirstOrderDifferentialEquations * differential equations} problem) if the signed value of the current stepsize * that is tried is needed.

                            *

                            The result is undefined if the method is called outside of * calls to integrate.

                            * @return current signed value of the stepsize */ double getCurrentSignedStepsize(); /** Set the maximal number of differential equations function evaluations. *

                            The purpose of this method is to avoid infinite loops which can occur * for example when stringent error constraints are set or when lots of * discrete events are triggered, thus leading to many rejected steps.

                            * @param maxEvaluations maximal number of function evaluations (negative * values are silently converted to maximal integer value, thus representing * almost unlimited evaluations) */ void setMaxEvaluations(int maxEvaluations); /** Get the maximal number of functions evaluations. * @return maximal number of functions evaluations */ int getMaxEvaluations(); /** Get the number of evaluations of the differential equations function. *

                            * The number of evaluations corresponds to the last call to the * integrate method. It is 0 if the method has not been called yet. *

                            * @return number of evaluations of the differential equations function */ int getEvaluations(); } ././@LongLink100644 0 0 146 12126630646 10262 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/ode/FirstOrderDifferentialEquations.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/ode/FirstOrderDifferentialEquations.jav100644 1750 1750 5557 12126627716 32304 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.ode; import org.apache.commons.math3.exception.DimensionMismatchException; import org.apache.commons.math3.exception.MaxCountExceededException; /** This interface represents a first order differential equations set. * *

                            This interface should be implemented by all real first order * differential equation problems before they can be handled by the * integrators {@link FirstOrderIntegrator#integrate} method.

                            * *

                            A first order differential equations problem, as seen by an * integrator is the time derivative dY/dt of a state * vector Y, both being one dimensional arrays. From the * integrator point of view, this derivative depends only on the * current time t and on the state vector * Y.

                            * *

                            For real problems, the derivative depends also on parameters * that do not belong to the state vector (dynamical model constants * for example). These constants are completely outside of the scope * of this interface, the classes that implement it are allowed to * handle them as they want.

                            * * @see FirstOrderIntegrator * @see FirstOrderConverter * @see SecondOrderDifferentialEquations * * @version $Id: FirstOrderDifferentialEquations.java 1416643 2012-12-03 19:37:14Z tn $ * @since 1.2 */ public interface FirstOrderDifferentialEquations { /** Get the dimension of the problem. * @return dimension of the problem */ int getDimension(); /** Get the current time derivative of the state vector. * @param t current value of the independent time variable * @param y array containing the current value of the state vector * @param yDot placeholder array where to put the time derivative of the state vector * @exception MaxCountExceededException if the number of functions evaluations is exceeded * @exception DimensionMismatchException if arrays dimensions do not match equations settings */ void computeDerivatives(double t, double[] y, double[] yDot) throws MaxCountExceededException, DimensionMismatchException; } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/ode/ContinuousOutputModel.java100644 1750 1750 34174 12126627716 30541 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.ode; import java.io.Serializable; import java.util.ArrayList; import java.util.List; import org.apache.commons.math3.exception.DimensionMismatchException; import org.apache.commons.math3.exception.MathIllegalArgumentException; import org.apache.commons.math3.exception.MaxCountExceededException; import org.apache.commons.math3.exception.util.LocalizedFormats; import org.apache.commons.math3.ode.sampling.StepHandler; import org.apache.commons.math3.ode.sampling.StepInterpolator; import org.apache.commons.math3.util.FastMath; /** * This class stores all information provided by an ODE integrator * during the integration process and build a continuous model of the * solution from this. * *

                            This class act as a step handler from the integrator point of * view. It is called iteratively during the integration process and * stores a copy of all steps information in a sorted collection for * later use. Once the integration process is over, the user can use * the {@link #setInterpolatedTime setInterpolatedTime} and {@link * #getInterpolatedState getInterpolatedState} to retrieve this * information at any time. It is important to wait for the * integration to be over before attempting to call {@link * #setInterpolatedTime setInterpolatedTime} because some internal * variables are set only once the last step has been handled.

                            * *

                            This is useful for example if the main loop of the user * application should remain independent from the integration process * or if one needs to mimic the behaviour of an analytical model * despite a numerical model is used (i.e. one needs the ability to * get the model value at any time or to navigate through the * data).

                            * *

                            If problem modeling is done with several separate * integration phases for contiguous intervals, the same * ContinuousOutputModel can be used as step handler for all * integration phases as long as they are performed in order and in * the same direction. As an example, one can extrapolate the * trajectory of a satellite with one model (i.e. one set of * differential equations) up to the beginning of a maneuver, use * another more complex model including thrusters modeling and * accurate attitude control during the maneuver, and revert to the * first model after the end of the maneuver. If the same continuous * output model handles the steps of all integration phases, the user * do not need to bother when the maneuver begins or ends, he has all * the data available in a transparent manner.

                            * *

                            An important feature of this class is that it implements the * Serializable interface. This means that the result of * an integration can be serialized and reused later (if stored into a * persistent medium like a filesystem or a database) or elsewhere (if * sent to another application). Only the result of the integration is * stored, there is no reference to the integrated problem by * itself.

                            * *

                            One should be aware that the amount of data stored in a * ContinuousOutputModel instance can be important if the state vector * is large, if the integration interval is long or if the steps are * small (which can result from small tolerance settings in {@link * org.apache.commons.math3.ode.nonstiff.AdaptiveStepsizeIntegrator adaptive * step size integrators}).

                            * * @see StepHandler * @see StepInterpolator * @version $Id: ContinuousOutputModel.java 1463684 2013-04-02 19:04:13Z luc $ * @since 1.2 */ public class ContinuousOutputModel implements StepHandler, Serializable { /** Serializable version identifier */ private static final long serialVersionUID = -1417964919405031606L; /** Initial integration time. */ private double initialTime; /** Final integration time. */ private double finalTime; /** Integration direction indicator. */ private boolean forward; /** Current interpolator index. */ private int index; /** Steps table. */ private List steps; /** Simple constructor. * Build an empty continuous output model. */ public ContinuousOutputModel() { steps = new ArrayList(); initialTime = Double.NaN; finalTime = Double.NaN; forward = true; index = 0; } /** Append another model at the end of the instance. * @param model model to add at the end of the instance * @exception MathIllegalArgumentException if the model to append is not * compatible with the instance (dimension of the state vector, * propagation direction, hole between the dates) * @exception MaxCountExceededException if the number of functions evaluations is exceeded * during step finalization */ public void append(final ContinuousOutputModel model) throws MathIllegalArgumentException, MaxCountExceededException { if (model.steps.size() == 0) { return; } if (steps.size() == 0) { initialTime = model.initialTime; forward = model.forward; } else { if (getInterpolatedState().length != model.getInterpolatedState().length) { throw new DimensionMismatchException(model.getInterpolatedState().length, getInterpolatedState().length); } if (forward ^ model.forward) { throw new MathIllegalArgumentException(LocalizedFormats.PROPAGATION_DIRECTION_MISMATCH); } final StepInterpolator lastInterpolator = steps.get(index); final double current = lastInterpolator.getCurrentTime(); final double previous = lastInterpolator.getPreviousTime(); final double step = current - previous; final double gap = model.getInitialTime() - current; if (FastMath.abs(gap) > 1.0e-3 * FastMath.abs(step)) { throw new MathIllegalArgumentException(LocalizedFormats.HOLE_BETWEEN_MODELS_TIME_RANGES, FastMath.abs(gap)); } } for (StepInterpolator interpolator : model.steps) { steps.add(interpolator.copy()); } index = steps.size() - 1; finalTime = (steps.get(index)).getCurrentTime(); } /** {@inheritDoc} */ public void init(double t0, double[] y0, double t) { initialTime = Double.NaN; finalTime = Double.NaN; forward = true; index = 0; steps.clear(); } /** Handle the last accepted step. * A copy of the information provided by the last step is stored in * the instance for later use. * @param interpolator interpolator for the last accepted step. * @param isLast true if the step is the last one * @exception MaxCountExceededException if the number of functions evaluations is exceeded * during step finalization */ public void handleStep(final StepInterpolator interpolator, final boolean isLast) throws MaxCountExceededException { if (steps.size() == 0) { initialTime = interpolator.getPreviousTime(); forward = interpolator.isForward(); } steps.add(interpolator.copy()); if (isLast) { finalTime = interpolator.getCurrentTime(); index = steps.size() - 1; } } /** * Get the initial integration time. * @return initial integration time */ public double getInitialTime() { return initialTime; } /** * Get the final integration time. * @return final integration time */ public double getFinalTime() { return finalTime; } /** * Get the time of the interpolated point. * If {@link #setInterpolatedTime} has not been called, it returns * the final integration time. * @return interpolation point time */ public double getInterpolatedTime() { return steps.get(index).getInterpolatedTime(); } /** Set the time of the interpolated point. *

                            This method should not be called before the * integration is over because some internal variables are set only * once the last step has been handled.

                            *

                            Setting the time outside of the integration interval is now * allowed (it was not allowed up to version 5.9 of Mantissa), but * should be used with care since the accuracy of the interpolator * will probably be very poor far from this interval. This allowance * has been added to simplify implementation of search algorithms * near the interval endpoints.

                            * @param time time of the interpolated point */ public void setInterpolatedTime(final double time) { // initialize the search with the complete steps table int iMin = 0; final StepInterpolator sMin = steps.get(iMin); double tMin = 0.5 * (sMin.getPreviousTime() + sMin.getCurrentTime()); int iMax = steps.size() - 1; final StepInterpolator sMax = steps.get(iMax); double tMax = 0.5 * (sMax.getPreviousTime() + sMax.getCurrentTime()); // handle points outside of the integration interval // or in the first and last step if (locatePoint(time, sMin) <= 0) { index = iMin; sMin.setInterpolatedTime(time); return; } if (locatePoint(time, sMax) >= 0) { index = iMax; sMax.setInterpolatedTime(time); return; } // reduction of the table slice size while (iMax - iMin > 5) { // use the last estimated index as the splitting index final StepInterpolator si = steps.get(index); final int location = locatePoint(time, si); if (location < 0) { iMax = index; tMax = 0.5 * (si.getPreviousTime() + si.getCurrentTime()); } else if (location > 0) { iMin = index; tMin = 0.5 * (si.getPreviousTime() + si.getCurrentTime()); } else { // we have found the target step, no need to continue searching si.setInterpolatedTime(time); return; } // compute a new estimate of the index in the reduced table slice final int iMed = (iMin + iMax) / 2; final StepInterpolator sMed = steps.get(iMed); final double tMed = 0.5 * (sMed.getPreviousTime() + sMed.getCurrentTime()); if ((FastMath.abs(tMed - tMin) < 1e-6) || (FastMath.abs(tMax - tMed) < 1e-6)) { // too close to the bounds, we estimate using a simple dichotomy index = iMed; } else { // estimate the index using a reverse quadratic polynom // (reverse means we have i = P(t), thus allowing to simply // compute index = P(time) rather than solving a quadratic equation) final double d12 = tMax - tMed; final double d23 = tMed - tMin; final double d13 = tMax - tMin; final double dt1 = time - tMax; final double dt2 = time - tMed; final double dt3 = time - tMin; final double iLagrange = ((dt2 * dt3 * d23) * iMax - (dt1 * dt3 * d13) * iMed + (dt1 * dt2 * d12) * iMin) / (d12 * d23 * d13); index = (int) FastMath.rint(iLagrange); } // force the next size reduction to be at least one tenth final int low = FastMath.max(iMin + 1, (9 * iMin + iMax) / 10); final int high = FastMath.min(iMax - 1, (iMin + 9 * iMax) / 10); if (index < low) { index = low; } else if (index > high) { index = high; } } // now the table slice is very small, we perform an iterative search index = iMin; while ((index <= iMax) && (locatePoint(time, steps.get(index)) > 0)) { ++index; } steps.get(index).setInterpolatedTime(time); } /** * Get the state vector of the interpolated point. * @return state vector at time {@link #getInterpolatedTime} * @exception MaxCountExceededException if the number of functions evaluations is exceeded * @see #getInterpolatedSecondaryState(int) */ public double[] getInterpolatedState() throws MaxCountExceededException { return steps.get(index).getInterpolatedState(); } /** Get the interpolated secondary state corresponding to the secondary equations. * @param secondaryStateIndex index of the secondary set, as returned by {@link * org.apache.commons.math3.ode.ExpandableStatefulODE#addSecondaryEquations( * org.apache.commons.math3.ode.SecondaryEquations) * ExpandableStatefulODE.addSecondaryEquations(SecondaryEquations)} * @return interpolated secondary state at the current interpolation date * @see #getInterpolatedState() * @since 3.2 * @exception MaxCountExceededException if the number of functions evaluations is exceeded */ public double[] getInterpolatedSecondaryState(final int secondaryStateIndex) throws MaxCountExceededException { return steps.get(index).getInterpolatedSecondaryState(secondaryStateIndex); } /** Compare a step interval and a double. * @param time point to locate * @param interval step interval * @return -1 if the double is before the interval, 0 if it is in * the interval, and +1 if it is after the interval, according to * the interval direction */ private int locatePoint(final double time, final StepInterpolator interval) { if (forward) { if (time < interval.getPreviousTime()) { return -1; } else if (time > interval.getCurrentTime()) { return +1; } else { return 0; } } if (time > interval.getPreviousTime()) { return -1; } else if (time < interval.getCurrentTime()) { return +1; } else { return 0; } } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/ode/JacobianMatrices.java100644 1750 1750 46424 12126627716 27370 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.ode; import java.lang.reflect.Array; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import org.apache.commons.math3.exception.DimensionMismatchException; import org.apache.commons.math3.exception.MathIllegalArgumentException; import org.apache.commons.math3.exception.MaxCountExceededException; import org.apache.commons.math3.exception.util.LocalizedFormats; /** * This class defines a set of {@link SecondaryEquations secondary equations} to * compute the Jacobian matrices with respect to the initial state vector and, if * any, to some parameters of the primary ODE set. *

                            * It is intended to be packed into an {@link ExpandableStatefulODE} * in conjunction with a primary set of ODE, which may be: *

                              *
                            • a {@link FirstOrderDifferentialEquations}
                            • *
                            • a {@link MainStateJacobianProvider}
                            • *
                            * In order to compute Jacobian matrices with respect to some parameters of the * primary ODE set, the following parameter Jacobian providers may be set: *
                              *
                            • a {@link ParameterJacobianProvider}
                            • *
                            • a {@link ParameterizedODE}
                            • *
                            *

                            * * @see ExpandableStatefulODE * @see FirstOrderDifferentialEquations * @see MainStateJacobianProvider * @see ParameterJacobianProvider * @see ParameterizedODE * * @version $Id: JacobianMatrices.java 1422447 2012-12-16 01:38:40Z psteitz $ * @since 3.0 */ public class JacobianMatrices { /** Expandable first order differential equation. */ private ExpandableStatefulODE efode; /** Index of the instance in the expandable set. */ private int index; /** FODE with exact primary Jacobian computation skill. */ private MainStateJacobianProvider jode; /** FODE without exact parameter Jacobian computation skill. */ private ParameterizedODE pode; /** Main state vector dimension. */ private int stateDim; /** Selected parameters for parameter Jacobian computation. */ private ParameterConfiguration[] selectedParameters; /** FODE with exact parameter Jacobian computation skill. */ private List jacobianProviders; /** Parameters dimension. */ private int paramDim; /** Boolean for selected parameters consistency. */ private boolean dirtyParameter; /** State and parameters Jacobian matrices in a row. */ private double[] matricesData; /** Simple constructor for a secondary equations set computing Jacobian matrices. *

                            * Parameters must belong to the supported ones given by {@link * Parameterizable#getParametersNames()}, so the primary set of differential * equations must be {@link Parameterizable}. *

                            *

                            Note that each selection clears the previous selected parameters.

                            * * @param fode the primary first order differential equations set to extend * @param hY step used for finite difference computation with respect to state vector * @param parameters parameters to consider for Jacobian matrices processing * (may be null if parameters Jacobians is not desired) * @exception DimensionMismatchException if there is a dimension mismatch between * the steps array {@code hY} and the equation dimension */ public JacobianMatrices(final FirstOrderDifferentialEquations fode, final double[] hY, final String... parameters) throws DimensionMismatchException { this(new MainStateJacobianWrapper(fode, hY), parameters); } /** Simple constructor for a secondary equations set computing Jacobian matrices. *

                            * Parameters must belong to the supported ones given by {@link * Parameterizable#getParametersNames()}, so the primary set of differential * equations must be {@link Parameterizable}. *

                            *

                            Note that each selection clears the previous selected parameters.

                            * * @param jode the primary first order differential equations set to extend * @param parameters parameters to consider for Jacobian matrices processing * (may be null if parameters Jacobians is not desired) */ public JacobianMatrices(final MainStateJacobianProvider jode, final String... parameters) { this.efode = null; this.index = -1; this.jode = jode; this.pode = null; this.stateDim = jode.getDimension(); if (parameters == null) { selectedParameters = null; paramDim = 0; } else { this.selectedParameters = new ParameterConfiguration[parameters.length]; for (int i = 0; i < parameters.length; ++i) { selectedParameters[i] = new ParameterConfiguration(parameters[i], Double.NaN); } paramDim = parameters.length; } this.dirtyParameter = false; this.jacobianProviders = new ArrayList(); // set the default initial state Jacobian to the identity // and the default initial parameters Jacobian to the null matrix matricesData = new double[(stateDim + paramDim) * stateDim]; for (int i = 0; i < stateDim; ++i) { matricesData[i * (stateDim + 1)] = 1.0; } } /** Register the variational equations for the Jacobians matrices to the expandable set. * @param expandable expandable set into which variational equations should be registered * @throws DimensionMismatchException if the dimension of the partial state does not * match the selected equations set dimension * @exception MismatchedEquations if the primary set of the expandable set does * not match the one used to build the instance * @see ExpandableStatefulODE#addSecondaryEquations(SecondaryEquations) */ public void registerVariationalEquations(final ExpandableStatefulODE expandable) throws DimensionMismatchException, MismatchedEquations { // safety checks final FirstOrderDifferentialEquations ode = (jode instanceof MainStateJacobianWrapper) ? ((MainStateJacobianWrapper) jode).ode : jode; if (expandable.getPrimary() != ode) { throw new MismatchedEquations(); } efode = expandable; index = efode.addSecondaryEquations(new JacobiansSecondaryEquations()); efode.setSecondaryState(index, matricesData); } /** Add a parameter Jacobian provider. * @param provider the parameter Jacobian provider to compute exactly the parameter Jacobian matrix */ public void addParameterJacobianProvider(final ParameterJacobianProvider provider) { jacobianProviders.add(provider); } /** Set a parameter Jacobian provider. * @param parameterizedOde the parameterized ODE to compute the parameter Jacobian matrix using finite differences */ public void setParameterizedODE(final ParameterizedODE parameterizedOde) { this.pode = parameterizedOde; dirtyParameter = true; } /** Set the step associated to a parameter in order to compute by finite * difference the Jacobian matrix. *

                            * Needed if and only if the primary ODE set is a {@link ParameterizedODE}. *

                            *

                            * Given a non zero parameter value pval for the parameter, a reasonable value * for such a step is {@code pval * FastMath.sqrt(Precision.EPSILON)}. *

                            *

                            * A zero value for such a step doesn't enable to compute the parameter Jacobian matrix. *

                            * @param parameter parameter to consider for Jacobian processing * @param hP step for Jacobian finite difference computation w.r.t. the specified parameter * @see ParameterizedODE * @exception UnknownParameterException if the parameter is not supported */ public void setParameterStep(final String parameter, final double hP) throws UnknownParameterException { for (ParameterConfiguration param: selectedParameters) { if (parameter.equals(param.getParameterName())) { param.setHP(hP); dirtyParameter = true; return; } } throw new UnknownParameterException(parameter); } /** Set the initial value of the Jacobian matrix with respect to state. *

                            * If this method is not called, the initial value of the Jacobian * matrix with respect to state is set to identity. *

                            * @param dYdY0 initial Jacobian matrix w.r.t. state * @exception DimensionMismatchException if matrix dimensions are incorrect */ public void setInitialMainStateJacobian(final double[][] dYdY0) throws DimensionMismatchException { // Check dimensions checkDimension(stateDim, dYdY0); checkDimension(stateDim, dYdY0[0]); // store the matrix in row major order as a single dimension array int i = 0; for (final double[] row : dYdY0) { System.arraycopy(row, 0, matricesData, i, stateDim); i += stateDim; } if (efode != null) { efode.setSecondaryState(index, matricesData); } } /** Set the initial value of a column of the Jacobian matrix with respect to one parameter. *

                            * If this method is not called for some parameter, the initial value of * the column of the Jacobian matrix with respect to this parameter is set to zero. *

                            * @param pName parameter name * @param dYdP initial Jacobian column vector with respect to the parameter * @exception UnknownParameterException if a parameter is not supported * @throws DimensionMismatchException if the column vector does not match state dimension */ public void setInitialParameterJacobian(final String pName, final double[] dYdP) throws UnknownParameterException, DimensionMismatchException { // Check dimensions checkDimension(stateDim, dYdP); // store the column in a global single dimension array int i = stateDim * stateDim; for (ParameterConfiguration param: selectedParameters) { if (pName.equals(param.getParameterName())) { System.arraycopy(dYdP, 0, matricesData, i, stateDim); if (efode != null) { efode.setSecondaryState(index, matricesData); } return; } i += stateDim; } throw new UnknownParameterException(pName); } /** Get the current value of the Jacobian matrix with respect to state. * @param dYdY0 current Jacobian matrix with respect to state. */ public void getCurrentMainSetJacobian(final double[][] dYdY0) { // get current state for this set of equations from the expandable fode double[] p = efode.getSecondaryState(index); int j = 0; for (int i = 0; i < stateDim; i++) { System.arraycopy(p, j, dYdY0[i], 0, stateDim); j += stateDim; } } /** Get the current value of the Jacobian matrix with respect to one parameter. * @param pName name of the parameter for the computed Jacobian matrix * @param dYdP current Jacobian matrix with respect to the named parameter */ public void getCurrentParameterJacobian(String pName, final double[] dYdP) { // get current state for this set of equations from the expandable fode double[] p = efode.getSecondaryState(index); int i = stateDim * stateDim; for (ParameterConfiguration param: selectedParameters) { if (param.getParameterName().equals(pName)) { System.arraycopy(p, i, dYdP, 0, stateDim); return; } i += stateDim; } } /** Check array dimensions. * @param expected expected dimension * @param array (may be null if expected is 0) * @throws DimensionMismatchException if the array dimension does not match the expected one */ private void checkDimension(final int expected, final Object array) throws DimensionMismatchException { int arrayDimension = (array == null) ? 0 : Array.getLength(array); if (arrayDimension != expected) { throw new DimensionMismatchException(arrayDimension, expected); } } /** Local implementation of secondary equations. *

                            * This class is an inner class to ensure proper scheduling of calls * by forcing the use of {@link JacobianMatrices#registerVariationalEquations(ExpandableStatefulODE)}. *

                            */ private class JacobiansSecondaryEquations implements SecondaryEquations { /** {@inheritDoc} */ public int getDimension() { return stateDim * (stateDim + paramDim); } /** {@inheritDoc} */ public void computeDerivatives(final double t, final double[] y, final double[] yDot, final double[] z, final double[] zDot) throws MaxCountExceededException, DimensionMismatchException { // Lazy initialization if (dirtyParameter && (paramDim != 0)) { jacobianProviders.add(new ParameterJacobianWrapper(jode, pode, selectedParameters)); dirtyParameter = false; } // variational equations: // from d[dy/dt]/dy0 and d[dy/dt]/dp to d[dy/dy0]/dt and d[dy/dp]/dt // compute Jacobian matrix with respect to primary state double[][] dFdY = new double[stateDim][stateDim]; jode.computeMainStateJacobian(t, y, yDot, dFdY); // Dispatch Jacobian matrix in the compound secondary state vector for (int i = 0; i < stateDim; ++i) { final double[] dFdYi = dFdY[i]; for (int j = 0; j < stateDim; ++j) { double s = 0; final int startIndex = j; int zIndex = startIndex; for (int l = 0; l < stateDim; ++l) { s += dFdYi[l] * z[zIndex]; zIndex += stateDim; } zDot[startIndex + i * stateDim] = s; } } if (paramDim != 0) { // compute Jacobian matrices with respect to parameters double[] dFdP = new double[stateDim]; int startIndex = stateDim * stateDim; for (ParameterConfiguration param: selectedParameters) { boolean found = false; for (int k = 0 ; (!found) && (k < jacobianProviders.size()); ++k) { final ParameterJacobianProvider provider = jacobianProviders.get(k); if (provider.isSupported(param.getParameterName())) { provider.computeParameterJacobian(t, y, yDot, param.getParameterName(), dFdP); for (int i = 0; i < stateDim; ++i) { final double[] dFdYi = dFdY[i]; int zIndex = startIndex; double s = dFdP[i]; for (int l = 0; l < stateDim; ++l) { s += dFdYi[l] * z[zIndex]; zIndex++; } zDot[startIndex + i] = s; } found = true; } } if (! found) { Arrays.fill(zDot, startIndex, startIndex + stateDim, 0.0); } startIndex += stateDim; } } } } /** Wrapper class to compute jacobian matrices by finite differences for ODE * which do not compute them by themselves. */ private static class MainStateJacobianWrapper implements MainStateJacobianProvider { /** Raw ODE without jacobians computation skill to be wrapped into a MainStateJacobianProvider. */ private final FirstOrderDifferentialEquations ode; /** Steps for finite difference computation of the jacobian df/dy w.r.t. state. */ private final double[] hY; /** Wrap a {@link FirstOrderDifferentialEquations} into a {@link MainStateJacobianProvider}. * @param ode original ODE problem, without jacobians computation skill * @param hY step sizes to compute the jacobian df/dy * @see JacobianMatrices#setMainStateSteps(double[]) * @exception DimensionMismatchException if there is a dimension mismatch between * the steps array {@code hY} and the equation dimension */ public MainStateJacobianWrapper(final FirstOrderDifferentialEquations ode, final double[] hY) throws DimensionMismatchException { this.ode = ode; this.hY = hY.clone(); if (hY.length != ode.getDimension()) { throw new DimensionMismatchException(ode.getDimension(), hY.length); } } /** {@inheritDoc} */ public int getDimension() { return ode.getDimension(); } /** {@inheritDoc} */ public void computeDerivatives(double t, double[] y, double[] yDot) throws MaxCountExceededException, DimensionMismatchException { ode.computeDerivatives(t, y, yDot); } /** {@inheritDoc} */ public void computeMainStateJacobian(double t, double[] y, double[] yDot, double[][] dFdY) throws MaxCountExceededException, DimensionMismatchException { final int n = ode.getDimension(); final double[] tmpDot = new double[n]; for (int j = 0; j < n; ++j) { final double savedYj = y[j]; y[j] += hY[j]; ode.computeDerivatives(t, y, tmpDot); for (int i = 0; i < n; ++i) { dFdY[i][j] = (tmpDot[i] - yDot[i]) / hY[j]; } y[j] = savedYj; } } } /** * Special exception for equations mismatch. * @since 3.1 */ public static class MismatchedEquations extends MathIllegalArgumentException { /** Serializable UID. */ private static final long serialVersionUID = 20120902L; /** Simple constructor. */ public MismatchedEquations() { super(LocalizedFormats.UNMATCHED_ODE_IN_EXPANDED_SET); } } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/ode/ParameterJacobianProvider.java100644 1750 1750 4712 12126627716 31226 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.ode; import org.apache.commons.math3.exception.DimensionMismatchException; import org.apache.commons.math3.exception.MaxCountExceededException; /** Interface to compute exactly Jacobian matrix for some parameter * when computing {@link JacobianMatrices partial derivatives equations}. * * @version $Id: ParameterJacobianProvider.java 1416643 2012-12-03 19:37:14Z tn $ * @since 3.0 */ public interface ParameterJacobianProvider extends Parameterizable { /** Compute the Jacobian matrix of ODE with respect to one parameter. *

                            If the parameter does not belong to the collection returned by * {@link #getParametersNames()}, the Jacobian will be set to 0, * but no errors will be triggered.

                            * @param t current value of the independent time variable * @param y array containing the current value of the main state vector * @param yDot array containing the current value of the time derivative * of the main state vector * @param paramName name of the parameter to consider * @param dFdP placeholder array where to put the Jacobian matrix of the * ODE with respect to the parameter * @exception MaxCountExceededException if the number of functions evaluations is exceeded * @exception DimensionMismatchException if arrays dimensions do not match equations settings * @exception UnknownParameterException if the parameter is not supported */ void computeParameterJacobian(double t, double[] y, double[] yDot, String paramName, double[] dFdP) throws DimensionMismatchException, MaxCountExceededException, UnknownParameterException; } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/ode/UnknownParameterException.java100644 1750 1750 3341 12126627716 31320 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.ode; import org.apache.commons.math3.exception.MathIllegalArgumentException; import org.apache.commons.math3.exception.util.LocalizedFormats; /** * Exception to be thrown when a parameter is unknown. * * @since 3.1 * @version $Id: UnknownParameterException.java 1416643 2012-12-03 19:37:14Z tn $ */ public class UnknownParameterException extends MathIllegalArgumentException { /** Serializable version Id. */ private static final long serialVersionUID = 20120902L; /** Parameter name. */ private final String name; /** * Construct an exception from the unknown parameter. * * @param name parameter name. */ public UnknownParameterException(final String name) { super(LocalizedFormats.UNKNOWN_PARAMETER); this.name = name; } /** * @return the name of the unknown parameter. */ public String getName() { return name; } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/ode/ParameterizedODE.java100644 1750 1750 3244 12126627716 27267 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.ode; /** Interface to compute by finite difference Jacobian matrix for some parameter * when computing {@link JacobianMatrices partial derivatives equations}. * * @version $Id: ParameterizedODE.java 1416643 2012-12-03 19:37:14Z tn $ * @since 3.0 */ public interface ParameterizedODE extends Parameterizable { /** Get parameter value from its name. * @param name parameter name * @return parameter value * @exception UnknownParameterException if parameter is not supported */ double getParameter(String name) throws UnknownParameterException; /** Set the value for a given parameter. * @param name parameter name * @param value parameter value * @exception UnknownParameterException if parameter is not supported */ void setParameter(String name, double value) throws UnknownParameterException; } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/ode/EquationsMapper.java100644 1750 1750 7414 12126627716 27263 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.ode; import java.io.Serializable; import org.apache.commons.math3.exception.DimensionMismatchException; /** * Class mapping the part of a complete state or derivative that pertains * to a specific differential equation. *

                            * Instances of this class are guaranteed to be immutable. *

                            * @see SecondaryEquations * @version $Id: EquationsMapper.java 1416643 2012-12-03 19:37:14Z tn $ * @since 3.0 */ public class EquationsMapper implements Serializable { /** Serializable UID. */ private static final long serialVersionUID = 20110925L; /** Index of the first equation element in complete state arrays. */ private final int firstIndex; /** Dimension of the secondary state parameters. */ private final int dimension; /** simple constructor. * @param firstIndex index of the first equation element in complete state arrays * @param dimension dimension of the secondary state parameters */ public EquationsMapper(final int firstIndex, final int dimension) { this.firstIndex = firstIndex; this.dimension = dimension; } /** Get the index of the first equation element in complete state arrays. * @return index of the first equation element in complete state arrays */ public int getFirstIndex() { return firstIndex; } /** Get the dimension of the secondary state parameters. * @return dimension of the secondary state parameters */ public int getDimension() { return dimension; } /** Extract equation data from a complete state or derivative array. * @param complete complete state or derivative array from which * equation data should be retrieved * @param equationData placeholder where to put equation data * @throws DimensionMismatchException if the dimension of the equation data does not * match the mapper dimension */ public void extractEquationData(double[] complete, double[] equationData) throws DimensionMismatchException { if (equationData.length != dimension) { throw new DimensionMismatchException(equationData.length, dimension); } System.arraycopy(complete, firstIndex, equationData, 0, dimension); } /** Insert equation data into a complete state or derivative array. * @param equationData equation data to be inserted into the complete array * @param complete placeholder where to put equation data (only the * part corresponding to the equation will be overwritten) * @throws DimensionMismatchException if the dimension of the equation data does not * match the mapper dimension */ public void insertEquationData(double[] equationData, double[] complete) throws DimensionMismatchException { if (equationData.length != dimension) { throw new DimensionMismatchException(equationData.length, dimension); } System.arraycopy(equationData, 0, complete, firstIndex, dimension); } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/ode/MainStateJacobianProvider.java100644 1750 1750 4214 12126627716 31170 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.ode; import org.apache.commons.math3.exception.DimensionMismatchException; import org.apache.commons.math3.exception.MaxCountExceededException; /** Interface expanding {@link FirstOrderDifferentialEquations first order * differential equations} in order to compute exactly the main state jacobian * matrix for {@link JacobianMatrices partial derivatives equations}. * * @version $Id: MainStateJacobianProvider.java 1416643 2012-12-03 19:37:14Z tn $ * @since 3.0 */ public interface MainStateJacobianProvider extends FirstOrderDifferentialEquations { /** Compute the jacobian matrix of ODE with respect to main state. * @param t current value of the independent time variable * @param y array containing the current value of the main state vector * @param yDot array containing the current value of the time derivative of the main state vector * @param dFdY placeholder array where to put the jacobian matrix of the ODE w.r.t. the main state vector * @exception MaxCountExceededException if the number of functions evaluations is exceeded * @exception DimensionMismatchException if arrays dimensions do not match equations settings */ void computeMainStateJacobian(double t, double[] y, double[] yDot, double[][] dFdY) throws MaxCountExceededException, DimensionMismatchException; } ././@LongLink100644 0 0 152 12126630646 10257 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/ode/nonstiff/AdaptiveStepsizeIntegrator.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/ode/nonstiff/AdaptiveStepsizeIntegrator100644 1750 1750 33370 12126627715 32416 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.ode.nonstiff; import org.apache.commons.math3.exception.DimensionMismatchException; import org.apache.commons.math3.exception.MaxCountExceededException; import org.apache.commons.math3.exception.NoBracketingException; import org.apache.commons.math3.exception.NumberIsTooSmallException; import org.apache.commons.math3.exception.util.LocalizedFormats; import org.apache.commons.math3.ode.AbstractIntegrator; import org.apache.commons.math3.ode.ExpandableStatefulODE; import org.apache.commons.math3.util.FastMath; /** * This abstract class holds the common part of all adaptive * stepsize integrators for Ordinary Differential Equations. * *

                            These algorithms perform integration with stepsize control, which * means the user does not specify the integration step but rather a * tolerance on error. The error threshold is computed as *

                             * threshold_i = absTol_i + relTol_i * max (abs (ym), abs (ym+1))
                             * 
                            * where absTol_i is the absolute tolerance for component i of the * state vector and relTol_i is the relative tolerance for the same * component. The user can also use only two scalar values absTol and * relTol which will be used for all components. *

                            *

                            * If the Ordinary Differential Equations is an {@link ExpandableStatefulODE * extended ODE} rather than a {@link * org.apache.commons.math3.ode.FirstOrderDifferentialEquations basic ODE}, then * only the {@link ExpandableStatefulODE#getPrimaryState() primary part} * of the state vector is used for stepsize control, not the complete state vector. *

                            * *

                            If the estimated error for ym+1 is such that *

                             * sqrt((sum (errEst_i / threshold_i)^2 ) / n) < 1
                             * 
                            * * (where n is the main set dimension) then the step is accepted, * otherwise the step is rejected and a new attempt is made with a new * stepsize.

                            * * @version $Id: AdaptiveStepsizeIntegrator.java 1416643 2012-12-03 19:37:14Z tn $ * @since 1.2 * */ public abstract class AdaptiveStepsizeIntegrator extends AbstractIntegrator { /** Allowed absolute scalar error. */ protected double scalAbsoluteTolerance; /** Allowed relative scalar error. */ protected double scalRelativeTolerance; /** Allowed absolute vectorial error. */ protected double[] vecAbsoluteTolerance; /** Allowed relative vectorial error. */ protected double[] vecRelativeTolerance; /** Main set dimension. */ protected int mainSetDimension; /** User supplied initial step. */ private double initialStep; /** Minimal step. */ private double minStep; /** Maximal step. */ private double maxStep; /** Build an integrator with the given stepsize bounds. * The default step handler does nothing. * @param name name of the method * @param minStep minimal step (sign is irrelevant, regardless of * integration direction, forward or backward), the last step can * be smaller than this * @param maxStep maximal step (sign is irrelevant, regardless of * integration direction, forward or backward), the last step can * be smaller than this * @param scalAbsoluteTolerance allowed absolute error * @param scalRelativeTolerance allowed relative error */ public AdaptiveStepsizeIntegrator(final String name, final double minStep, final double maxStep, final double scalAbsoluteTolerance, final double scalRelativeTolerance) { super(name); setStepSizeControl(minStep, maxStep, scalAbsoluteTolerance, scalRelativeTolerance); resetInternalState(); } /** Build an integrator with the given stepsize bounds. * The default step handler does nothing. * @param name name of the method * @param minStep minimal step (sign is irrelevant, regardless of * integration direction, forward or backward), the last step can * be smaller than this * @param maxStep maximal step (sign is irrelevant, regardless of * integration direction, forward or backward), the last step can * be smaller than this * @param vecAbsoluteTolerance allowed absolute error * @param vecRelativeTolerance allowed relative error */ public AdaptiveStepsizeIntegrator(final String name, final double minStep, final double maxStep, final double[] vecAbsoluteTolerance, final double[] vecRelativeTolerance) { super(name); setStepSizeControl(minStep, maxStep, vecAbsoluteTolerance, vecRelativeTolerance); resetInternalState(); } /** Set the adaptive step size control parameters. *

                            * A side effect of this method is to also reset the initial * step so it will be automatically computed by the integrator * if {@link #setInitialStepSize(double) setInitialStepSize} * is not called by the user. *

                            * @param minimalStep minimal step (must be positive even for backward * integration), the last step can be smaller than this * @param maximalStep maximal step (must be positive even for backward * integration) * @param absoluteTolerance allowed absolute error * @param relativeTolerance allowed relative error */ public void setStepSizeControl(final double minimalStep, final double maximalStep, final double absoluteTolerance, final double relativeTolerance) { minStep = FastMath.abs(minimalStep); maxStep = FastMath.abs(maximalStep); initialStep = -1; scalAbsoluteTolerance = absoluteTolerance; scalRelativeTolerance = relativeTolerance; vecAbsoluteTolerance = null; vecRelativeTolerance = null; } /** Set the adaptive step size control parameters. *

                            * A side effect of this method is to also reset the initial * step so it will be automatically computed by the integrator * if {@link #setInitialStepSize(double) setInitialStepSize} * is not called by the user. *

                            * @param minimalStep minimal step (must be positive even for backward * integration), the last step can be smaller than this * @param maximalStep maximal step (must be positive even for backward * integration) * @param absoluteTolerance allowed absolute error * @param relativeTolerance allowed relative error */ public void setStepSizeControl(final double minimalStep, final double maximalStep, final double[] absoluteTolerance, final double[] relativeTolerance) { minStep = FastMath.abs(minimalStep); maxStep = FastMath.abs(maximalStep); initialStep = -1; scalAbsoluteTolerance = 0; scalRelativeTolerance = 0; vecAbsoluteTolerance = absoluteTolerance.clone(); vecRelativeTolerance = relativeTolerance.clone(); } /** Set the initial step size. *

                            This method allows the user to specify an initial positive * step size instead of letting the integrator guess it by * itself. If this method is not called before integration is * started, the initial step size will be estimated by the * integrator.

                            * @param initialStepSize initial step size to use (must be positive even * for backward integration ; providing a negative value or a value * outside of the min/max step interval will lead the integrator to * ignore the value and compute the initial step size by itself) */ public void setInitialStepSize(final double initialStepSize) { if ((initialStepSize < minStep) || (initialStepSize > maxStep)) { initialStep = -1.0; } else { initialStep = initialStepSize; } } /** {@inheritDoc} */ @Override protected void sanityChecks(final ExpandableStatefulODE equations, final double t) throws DimensionMismatchException, NumberIsTooSmallException { super.sanityChecks(equations, t); mainSetDimension = equations.getPrimaryMapper().getDimension(); if ((vecAbsoluteTolerance != null) && (vecAbsoluteTolerance.length != mainSetDimension)) { throw new DimensionMismatchException(mainSetDimension, vecAbsoluteTolerance.length); } if ((vecRelativeTolerance != null) && (vecRelativeTolerance.length != mainSetDimension)) { throw new DimensionMismatchException(mainSetDimension, vecRelativeTolerance.length); } } /** Initialize the integration step. * @param forward forward integration indicator * @param order order of the method * @param scale scaling vector for the state vector (can be shorter than state vector) * @param t0 start time * @param y0 state vector at t0 * @param yDot0 first time derivative of y0 * @param y1 work array for a state vector * @param yDot1 work array for the first time derivative of y1 * @return first integration step * @exception MaxCountExceededException if the number of functions evaluations is exceeded * @exception DimensionMismatchException if arrays dimensions do not match equations settings */ public double initializeStep(final boolean forward, final int order, final double[] scale, final double t0, final double[] y0, final double[] yDot0, final double[] y1, final double[] yDot1) throws MaxCountExceededException, DimensionMismatchException { if (initialStep > 0) { // use the user provided value return forward ? initialStep : -initialStep; } // very rough first guess : h = 0.01 * ||y/scale|| / ||y'/scale|| // this guess will be used to perform an Euler step double ratio; double yOnScale2 = 0; double yDotOnScale2 = 0; for (int j = 0; j < scale.length; ++j) { ratio = y0[j] / scale[j]; yOnScale2 += ratio * ratio; ratio = yDot0[j] / scale[j]; yDotOnScale2 += ratio * ratio; } double h = ((yOnScale2 < 1.0e-10) || (yDotOnScale2 < 1.0e-10)) ? 1.0e-6 : (0.01 * FastMath.sqrt(yOnScale2 / yDotOnScale2)); if (! forward) { h = -h; } // perform an Euler step using the preceding rough guess for (int j = 0; j < y0.length; ++j) { y1[j] = y0[j] + h * yDot0[j]; } computeDerivatives(t0 + h, y1, yDot1); // estimate the second derivative of the solution double yDDotOnScale = 0; for (int j = 0; j < scale.length; ++j) { ratio = (yDot1[j] - yDot0[j]) / scale[j]; yDDotOnScale += ratio * ratio; } yDDotOnScale = FastMath.sqrt(yDDotOnScale) / h; // step size is computed such that // h^order * max (||y'/tol||, ||y''/tol||) = 0.01 final double maxInv2 = FastMath.max(FastMath.sqrt(yDotOnScale2), yDDotOnScale); final double h1 = (maxInv2 < 1.0e-15) ? FastMath.max(1.0e-6, 0.001 * FastMath.abs(h)) : FastMath.pow(0.01 / maxInv2, 1.0 / order); h = FastMath.min(100.0 * FastMath.abs(h), h1); h = FastMath.max(h, 1.0e-12 * FastMath.abs(t0)); // avoids cancellation when computing t1 - t0 if (h < getMinStep()) { h = getMinStep(); } if (h > getMaxStep()) { h = getMaxStep(); } if (! forward) { h = -h; } return h; } /** Filter the integration step. * @param h signed step * @param forward forward integration indicator * @param acceptSmall if true, steps smaller than the minimal value * are silently increased up to this value, if false such small * steps generate an exception * @return a bounded integration step (h if no bound is reach, or a bounded value) * @exception NumberIsTooSmallException if the step is too small and acceptSmall is false */ protected double filterStep(final double h, final boolean forward, final boolean acceptSmall) throws NumberIsTooSmallException { double filteredH = h; if (FastMath.abs(h) < minStep) { if (acceptSmall) { filteredH = forward ? minStep : -minStep; } else { throw new NumberIsTooSmallException(LocalizedFormats.MINIMAL_STEPSIZE_REACHED_DURING_INTEGRATION, FastMath.abs(h), minStep, true); } } if (filteredH > maxStep) { filteredH = maxStep; } else if (filteredH < -maxStep) { filteredH = -maxStep; } return filteredH; } /** {@inheritDoc} */ @Override public abstract void integrate (ExpandableStatefulODE equations, double t) throws NumberIsTooSmallException, DimensionMismatchException, MaxCountExceededException, NoBracketingException; /** {@inheritDoc} */ @Override public double getCurrentStepStart() { return stepStart; } /** Reset internal state to dummy values. */ protected void resetInternalState() { stepStart = Double.NaN; stepSize = FastMath.sqrt(minStep * maxStep); } /** Get the minimal step. * @return minimal step */ public double getMinStep() { return minStep; } /** Get the maximal step. * @return maximal step */ public double getMaxStep() { return maxStep; } } ././@LongLink100644 0 0 151 12126630646 10256 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/ode/nonstiff/DormandPrince54Integrator.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/ode/nonstiff/DormandPrince54Integrator.100644 1750 1750 14367 12126627715 32113 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.ode.nonstiff; import org.apache.commons.math3.util.FastMath; /** * This class implements the 5(4) Dormand-Prince integrator for Ordinary * Differential Equations. *

                            This integrator is an embedded Runge-Kutta integrator * of order 5(4) used in local extrapolation mode (i.e. the solution * is computed using the high order formula) with stepsize control * (and automatic step initialization) and continuous output. This * method uses 7 functions evaluations per step. However, since this * is an fsal, the last evaluation of one step is the same as * the first evaluation of the next step and hence can be avoided. So * the cost is really 6 functions evaluations per step.

                            * *

                            This method has been published (whithout the continuous output * that was added by Shampine in 1986) in the following article : *

                             *  A family of embedded Runge-Kutta formulae
                             *  J. R. Dormand and P. J. Prince
                             *  Journal of Computational and Applied Mathematics
                             *  volume 6, no 1, 1980, pp. 19-26
                             * 

                            * * @version $Id: DormandPrince54Integrator.java 1416643 2012-12-03 19:37:14Z tn $ * @since 1.2 */ public class DormandPrince54Integrator extends EmbeddedRungeKuttaIntegrator { /** Integrator method name. */ private static final String METHOD_NAME = "Dormand-Prince 5(4)"; /** Time steps Butcher array. */ private static final double[] STATIC_C = { 1.0/5.0, 3.0/10.0, 4.0/5.0, 8.0/9.0, 1.0, 1.0 }; /** Internal weights Butcher array. */ private static final double[][] STATIC_A = { {1.0/5.0}, {3.0/40.0, 9.0/40.0}, {44.0/45.0, -56.0/15.0, 32.0/9.0}, {19372.0/6561.0, -25360.0/2187.0, 64448.0/6561.0, -212.0/729.0}, {9017.0/3168.0, -355.0/33.0, 46732.0/5247.0, 49.0/176.0, -5103.0/18656.0}, {35.0/384.0, 0.0, 500.0/1113.0, 125.0/192.0, -2187.0/6784.0, 11.0/84.0} }; /** Propagation weights Butcher array. */ private static final double[] STATIC_B = { 35.0/384.0, 0.0, 500.0/1113.0, 125.0/192.0, -2187.0/6784.0, 11.0/84.0, 0.0 }; /** Error array, element 1. */ private static final double E1 = 71.0 / 57600.0; // element 2 is zero, so it is neither stored nor used /** Error array, element 3. */ private static final double E3 = -71.0 / 16695.0; /** Error array, element 4. */ private static final double E4 = 71.0 / 1920.0; /** Error array, element 5. */ private static final double E5 = -17253.0 / 339200.0; /** Error array, element 6. */ private static final double E6 = 22.0 / 525.0; /** Error array, element 7. */ private static final double E7 = -1.0 / 40.0; /** Simple constructor. * Build a fifth order Dormand-Prince integrator with the given step bounds * @param minStep minimal step (sign is irrelevant, regardless of * integration direction, forward or backward), the last step can * be smaller than this * @param maxStep maximal step (sign is irrelevant, regardless of * integration direction, forward or backward), the last step can * be smaller than this * @param scalAbsoluteTolerance allowed absolute error * @param scalRelativeTolerance allowed relative error */ public DormandPrince54Integrator(final double minStep, final double maxStep, final double scalAbsoluteTolerance, final double scalRelativeTolerance) { super(METHOD_NAME, true, STATIC_C, STATIC_A, STATIC_B, new DormandPrince54StepInterpolator(), minStep, maxStep, scalAbsoluteTolerance, scalRelativeTolerance); } /** Simple constructor. * Build a fifth order Dormand-Prince integrator with the given step bounds * @param minStep minimal step (sign is irrelevant, regardless of * integration direction, forward or backward), the last step can * be smaller than this * @param maxStep maximal step (sign is irrelevant, regardless of * integration direction, forward or backward), the last step can * be smaller than this * @param vecAbsoluteTolerance allowed absolute error * @param vecRelativeTolerance allowed relative error */ public DormandPrince54Integrator(final double minStep, final double maxStep, final double[] vecAbsoluteTolerance, final double[] vecRelativeTolerance) { super(METHOD_NAME, true, STATIC_C, STATIC_A, STATIC_B, new DormandPrince54StepInterpolator(), minStep, maxStep, vecAbsoluteTolerance, vecRelativeTolerance); } /** {@inheritDoc} */ @Override public int getOrder() { return 5; } /** {@inheritDoc} */ @Override protected double estimateError(final double[][] yDotK, final double[] y0, final double[] y1, final double h) { double error = 0; for (int j = 0; j < mainSetDimension; ++j) { final double errSum = E1 * yDotK[0][j] + E3 * yDotK[2][j] + E4 * yDotK[3][j] + E5 * yDotK[4][j] + E6 * yDotK[5][j] + E7 * yDotK[6][j]; final double yScale = FastMath.max(FastMath.abs(y0[j]), FastMath.abs(y1[j])); final double tol = (vecAbsoluteTolerance == null) ? (scalAbsoluteTolerance + scalRelativeTolerance * yScale) : (vecAbsoluteTolerance[j] + vecRelativeTolerance[j] * yScale); final double ratio = h * errSum / tol; error += ratio * ratio; } return FastMath.sqrt(error / mainSetDimension); } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/ode/nonstiff/GillIntegrator.java100644 1750 1750 4711 12126627715 30716 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.ode.nonstiff; import org.apache.commons.math3.util.FastMath; /** * This class implements the Gill fourth order Runge-Kutta * integrator for Ordinary Differential Equations . *

                            This method is an explicit Runge-Kutta method, its Butcher-array * is the following one : *

                             *    0  |    0        0       0      0
                             *   1/2 |   1/2       0       0      0
                             *   1/2 | (q-1)/2  (2-q)/2    0      0
                             *    1  |    0       -q/2  (2+q)/2   0
                             *       |-------------------------------
                             *       |   1/6    (2-q)/6 (2+q)/6  1/6
                             * 
                            * where q = sqrt(2)

                            * * @see EulerIntegrator * @see ClassicalRungeKuttaIntegrator * @see MidpointIntegrator * @see ThreeEighthesIntegrator * @version $Id: GillIntegrator.java 1416643 2012-12-03 19:37:14Z tn $ * @since 1.2 */ public class GillIntegrator extends RungeKuttaIntegrator { /** Time steps Butcher array. */ private static final double[] STATIC_C = { 1.0 / 2.0, 1.0 / 2.0, 1.0 }; /** Internal weights Butcher array. */ private static final double[][] STATIC_A = { { 1.0 / 2.0 }, { (FastMath.sqrt(2.0) - 1.0) / 2.0, (2.0 - FastMath.sqrt(2.0)) / 2.0 }, { 0.0, -FastMath.sqrt(2.0) / 2.0, (2.0 + FastMath.sqrt(2.0)) / 2.0 } }; /** Propagation weights Butcher array. */ private static final double[] STATIC_B = { 1.0 / 6.0, (2.0 - FastMath.sqrt(2.0)) / 6.0, (2.0 + FastMath.sqrt(2.0)) / 6.0, 1.0 / 6.0 }; /** Simple constructor. * Build a fourth-order Gill integrator with the given step. * @param step integration step */ public GillIntegrator(final double step) { super("Gill", STATIC_C, STATIC_A, STATIC_B, new GillStepInterpolator(), step); } } ././@LongLink100644 0 0 155 12126630646 10262 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/ode/nonstiff/ThreeEighthesStepInterpolator.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/ode/nonstiff/ThreeEighthesStepInterpola100644 1750 1750 14152 12126627715 32332 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.ode.nonstiff; import org.apache.commons.math3.ode.sampling.StepInterpolator; /** * This class implements a step interpolator for the 3/8 fourth * order Runge-Kutta integrator. * *

                            This interpolator allows to compute dense output inside the last * step computed. The interpolation equation is consistent with the * integration scheme : *

                              *
                            • Using reference point at step start:
                              * y(tn + θ h) = y (tn) * + θ (h/8) [ (8 - 15 θ + 8 θ2) y'1 * + 3 * (15 θ - 12 θ2) y'2 * + 3 θ y'3 * + (-3 θ + 4 θ2) y'4 * ] *
                            • *
                            • Using reference point at step end:
                              * y(tn + θ h) = y (tn + h) * - (1 - θ) (h/8) [(1 - 7 θ + 8 θ2) y'1 * + 3 (1 + θ - 4 θ2) y'2 * + 3 (1 + θ) y'3 * + (1 + θ + 4 θ2) y'4 * ] *
                            • *
                            *

                            * * where θ belongs to [0 ; 1] and where y'1 to y'4 are the four * evaluations of the derivatives already computed during the * step.

                            * * @see ThreeEighthesIntegrator * @version $Id: ThreeEighthesStepInterpolator.java 1416643 2012-12-03 19:37:14Z tn $ * @since 1.2 */ class ThreeEighthesStepInterpolator extends RungeKuttaStepInterpolator { /** Serializable version identifier */ private static final long serialVersionUID = 20111120L; /** Simple constructor. * This constructor builds an instance that is not usable yet, the * {@link * org.apache.commons.math3.ode.sampling.AbstractStepInterpolator#reinitialize} * method should be called before using the instance in order to * initialize the internal arrays. This constructor is used only * in order to delay the initialization in some cases. The {@link * RungeKuttaIntegrator} class uses the prototyping design pattern * to create the step interpolators by cloning an uninitialized model * and later initializing the copy. */ public ThreeEighthesStepInterpolator() { } /** Copy constructor. * @param interpolator interpolator to copy from. The copy is a deep * copy: its arrays are separated from the original arrays of the * instance */ public ThreeEighthesStepInterpolator(final ThreeEighthesStepInterpolator interpolator) { super(interpolator); } /** {@inheritDoc} */ @Override protected StepInterpolator doCopy() { return new ThreeEighthesStepInterpolator(this); } /** {@inheritDoc} */ @Override protected void computeInterpolatedStateAndDerivatives(final double theta, final double oneMinusThetaH) { final double coeffDot3 = 0.75 * theta; final double coeffDot1 = coeffDot3 * (4 * theta - 5) + 1; final double coeffDot2 = coeffDot3 * (5 - 6 * theta); final double coeffDot4 = coeffDot3 * (2 * theta - 1); if ((previousState != null) && (theta <= 0.5)) { final double s = theta * h / 8.0; final double fourTheta2 = 4 * theta * theta; final double coeff1 = s * (8 - 15 * theta + 2 * fourTheta2); final double coeff2 = 3 * s * (5 * theta - fourTheta2); final double coeff3 = 3 * s * theta; final double coeff4 = s * (-3 * theta + fourTheta2); for (int i = 0; i < interpolatedState.length; ++i) { final double yDot1 = yDotK[0][i]; final double yDot2 = yDotK[1][i]; final double yDot3 = yDotK[2][i]; final double yDot4 = yDotK[3][i]; interpolatedState[i] = previousState[i] + coeff1 * yDot1 + coeff2 * yDot2 + coeff3 * yDot3 + coeff4 * yDot4; interpolatedDerivatives[i] = coeffDot1 * yDot1 + coeffDot2 * yDot2 + coeffDot3 * yDot3 + coeffDot4 * yDot4; } } else { final double s = oneMinusThetaH / 8.0; final double fourTheta2 = 4 * theta * theta; final double coeff1 = s * (1 - 7 * theta + 2 * fourTheta2); final double coeff2 = 3 * s * (1 + theta - fourTheta2); final double coeff3 = 3 * s * (1 + theta); final double coeff4 = s * (1 + theta + fourTheta2); for (int i = 0; i < interpolatedState.length; ++i) { final double yDot1 = yDotK[0][i]; final double yDot2 = yDotK[1][i]; final double yDot3 = yDotK[2][i]; final double yDot4 = yDotK[3][i]; interpolatedState[i] = currentState[i] - coeff1 * yDot1 - coeff2 * yDot2 - coeff3 * yDot3 - coeff4 * yDot4; interpolatedDerivatives[i] = coeffDot1 * yDot1 + coeffDot2 * yDot2 + coeffDot3 * yDot3 + coeffDot4 * yDot4; } } } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/ode/nonstiff/package-info.java100644 1750 1750 1720 12126627715 30311 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ /** * *

                            * This package provides classes to solve non-stiff Ordinary Differential Equations problems. *

                            * * */ package org.apache.commons.math3.ode.nonstiff; commons-math3-3.2-src/src/main/java/org/apache/commons/math3/ode/nonstiff/RungeKuttaIntegrator.java100644 1750 1750 14431 12126627715 32140 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.ode.nonstiff; import org.apache.commons.math3.exception.DimensionMismatchException; import org.apache.commons.math3.exception.MaxCountExceededException; import org.apache.commons.math3.exception.NoBracketingException; import org.apache.commons.math3.exception.NumberIsTooSmallException; import org.apache.commons.math3.ode.AbstractIntegrator; import org.apache.commons.math3.ode.ExpandableStatefulODE; import org.apache.commons.math3.util.FastMath; /** * This class implements the common part of all fixed step Runge-Kutta * integrators for Ordinary Differential Equations. * *

                            These methods are explicit Runge-Kutta methods, their Butcher * arrays are as follows : *

                             *    0  |
                             *   c2  | a21
                             *   c3  | a31  a32
                             *   ... |        ...
                             *   cs  | as1  as2  ...  ass-1
                             *       |--------------------------
                             *       |  b1   b2  ...   bs-1  bs
                             * 
                            *

                            * * @see EulerIntegrator * @see ClassicalRungeKuttaIntegrator * @see GillIntegrator * @see MidpointIntegrator * @version $Id: RungeKuttaIntegrator.java 1416643 2012-12-03 19:37:14Z tn $ * @since 1.2 */ public abstract class RungeKuttaIntegrator extends AbstractIntegrator { /** Time steps from Butcher array (without the first zero). */ private final double[] c; /** Internal weights from Butcher array (without the first empty row). */ private final double[][] a; /** External weights for the high order method from Butcher array. */ private final double[] b; /** Prototype of the step interpolator. */ private final RungeKuttaStepInterpolator prototype; /** Integration step. */ private final double step; /** Simple constructor. * Build a Runge-Kutta integrator with the given * step. The default step handler does nothing. * @param name name of the method * @param c time steps from Butcher array (without the first zero) * @param a internal weights from Butcher array (without the first empty row) * @param b propagation weights for the high order method from Butcher array * @param prototype prototype of the step interpolator to use * @param step integration step */ protected RungeKuttaIntegrator(final String name, final double[] c, final double[][] a, final double[] b, final RungeKuttaStepInterpolator prototype, final double step) { super(name); this.c = c; this.a = a; this.b = b; this.prototype = prototype; this.step = FastMath.abs(step); } /** {@inheritDoc} */ @Override public void integrate(final ExpandableStatefulODE equations, final double t) throws NumberIsTooSmallException, DimensionMismatchException, MaxCountExceededException, NoBracketingException { sanityChecks(equations, t); setEquations(equations); final boolean forward = t > equations.getTime(); // create some internal working arrays final double[] y0 = equations.getCompleteState(); final double[] y = y0.clone(); final int stages = c.length + 1; final double[][] yDotK = new double[stages][]; for (int i = 0; i < stages; ++i) { yDotK [i] = new double[y0.length]; } final double[] yTmp = y0.clone(); final double[] yDotTmp = new double[y0.length]; // set up an interpolator sharing the integrator arrays final RungeKuttaStepInterpolator interpolator = (RungeKuttaStepInterpolator) prototype.copy(); interpolator.reinitialize(this, yTmp, yDotK, forward, equations.getPrimaryMapper(), equations.getSecondaryMappers()); interpolator.storeTime(equations.getTime()); // set up integration control objects stepStart = equations.getTime(); stepSize = forward ? step : -step; initIntegration(equations.getTime(), y0, t); // main integration loop isLastStep = false; do { interpolator.shift(); // first stage computeDerivatives(stepStart, y, yDotK[0]); // next stages for (int k = 1; k < stages; ++k) { for (int j = 0; j < y0.length; ++j) { double sum = a[k-1][0] * yDotK[0][j]; for (int l = 1; l < k; ++l) { sum += a[k-1][l] * yDotK[l][j]; } yTmp[j] = y[j] + stepSize * sum; } computeDerivatives(stepStart + c[k-1] * stepSize, yTmp, yDotK[k]); } // estimate the state at the end of the step for (int j = 0; j < y0.length; ++j) { double sum = b[0] * yDotK[0][j]; for (int l = 1; l < stages; ++l) { sum += b[l] * yDotK[l][j]; } yTmp[j] = y[j] + stepSize * sum; } // discrete events handling interpolator.storeTime(stepStart + stepSize); System.arraycopy(yTmp, 0, y, 0, y0.length); System.arraycopy(yDotK[stages - 1], 0, yDotTmp, 0, y0.length); stepStart = acceptStep(interpolator, y, yDotTmp, t); if (!isLastStep) { // prepare next step interpolator.storeTime(stepStart); // stepsize control for next step final double nextT = stepStart + stepSize; final boolean nextIsLast = forward ? (nextT >= t) : (nextT <= t); if (nextIsLast) { stepSize = t - stepStart; } } } while (!isLastStep); // dispatch results equations.setTime(stepStart); equations.setCompleteState(y); stepStart = Double.NaN; stepSize = Double.NaN; } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/ode/nonstiff/GillStepInterpolator.java100644 1750 1750 14425 12126627715 32141 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.ode.nonstiff; import org.apache.commons.math3.ode.sampling.StepInterpolator; import org.apache.commons.math3.util.FastMath; /** * This class implements a step interpolator for the Gill fourth * order Runge-Kutta integrator. * *

                            This interpolator allows to compute dense output inside the last * step computed. The interpolation equation is consistent with the * integration scheme : *

                              *
                            • Using reference point at step start:
                              * y(tn + θ h) = y (tn) * + θ (h/6) [ (6 - 9 θ + 4 θ2) y'1 * + ( 6 θ - 4 θ2) ((1-1/√2) y'2 + (1+1/√2)) y'3) * + ( - 3 θ + 4 θ2) y'4 * ] *
                            • *
                            • Using reference point at step start:
                              * y(tn + θ h) = y (tn + h) * - (1 - θ) (h/6) [ (1 - 5 θ + 4 θ2) y'1 * + (2 + 2 θ - 4 θ2) ((1-1/√2) y'2 + (1+1/√2)) y'3) * + (1 + θ + 4 θ2) y'4 * ] *
                            • *
                            *

                            * where θ belongs to [0 ; 1] and where y'1 to y'4 * are the four evaluations of the derivatives already computed during * the step.

                            * * @see GillIntegrator * @version $Id: GillStepInterpolator.java 1416643 2012-12-03 19:37:14Z tn $ * @since 1.2 */ class GillStepInterpolator extends RungeKuttaStepInterpolator { /** First Gill coefficient. */ private static final double ONE_MINUS_INV_SQRT_2 = 1 - FastMath.sqrt(0.5); /** Second Gill coefficient. */ private static final double ONE_PLUS_INV_SQRT_2 = 1 + FastMath.sqrt(0.5); /** Serializable version identifier. */ private static final long serialVersionUID = 20111120L; /** Simple constructor. * This constructor builds an instance that is not usable yet, the * {@link * org.apache.commons.math3.ode.sampling.AbstractStepInterpolator#reinitialize} * method should be called before using the instance in order to * initialize the internal arrays. This constructor is used only * in order to delay the initialization in some cases. The {@link * RungeKuttaIntegrator} class uses the prototyping design pattern * to create the step interpolators by cloning an uninitialized model * and later initializing the copy. */ public GillStepInterpolator() { } /** Copy constructor. * @param interpolator interpolator to copy from. The copy is a deep * copy: its arrays are separated from the original arrays of the * instance */ public GillStepInterpolator(final GillStepInterpolator interpolator) { super(interpolator); } /** {@inheritDoc} */ @Override protected StepInterpolator doCopy() { return new GillStepInterpolator(this); } /** {@inheritDoc} */ @Override protected void computeInterpolatedStateAndDerivatives(final double theta, final double oneMinusThetaH) { final double twoTheta = 2 * theta; final double fourTheta2 = twoTheta * twoTheta; final double coeffDot1 = theta * (twoTheta - 3) + 1; final double cDot23 = twoTheta * (1 - theta); final double coeffDot2 = cDot23 * ONE_MINUS_INV_SQRT_2; final double coeffDot3 = cDot23 * ONE_PLUS_INV_SQRT_2; final double coeffDot4 = theta * (twoTheta - 1); if ((previousState != null) && (theta <= 0.5)) { final double s = theta * h / 6.0; final double c23 = s * (6 * theta - fourTheta2); final double coeff1 = s * (6 - 9 * theta + fourTheta2); final double coeff2 = c23 * ONE_MINUS_INV_SQRT_2; final double coeff3 = c23 * ONE_PLUS_INV_SQRT_2; final double coeff4 = s * (-3 * theta + fourTheta2); for (int i = 0; i < interpolatedState.length; ++i) { final double yDot1 = yDotK[0][i]; final double yDot2 = yDotK[1][i]; final double yDot3 = yDotK[2][i]; final double yDot4 = yDotK[3][i]; interpolatedState[i] = previousState[i] + coeff1 * yDot1 + coeff2 * yDot2 + coeff3 * yDot3 + coeff4 * yDot4; interpolatedDerivatives[i] = coeffDot1 * yDot1 + coeffDot2 * yDot2 + coeffDot3 * yDot3 + coeffDot4 * yDot4; } } else { final double s = oneMinusThetaH / 6.0; final double c23 = s * (2 + twoTheta - fourTheta2); final double coeff1 = s * (1 - 5 * theta + fourTheta2); final double coeff2 = c23 * ONE_MINUS_INV_SQRT_2; final double coeff3 = c23 * ONE_PLUS_INV_SQRT_2; final double coeff4 = s * (1 + theta + fourTheta2); for (int i = 0; i < interpolatedState.length; ++i) { final double yDot1 = yDotK[0][i]; final double yDot2 = yDotK[1][i]; final double yDot3 = yDotK[2][i]; final double yDot4 = yDotK[3][i]; interpolatedState[i] = currentState[i] - coeff1 * yDot1 - coeff2 * yDot2 - coeff3 * yDot3 - coeff4 * yDot4; interpolatedDerivatives[i] = coeffDot1 * yDot1 + coeffDot2 * yDot2 + coeffDot3 * yDot3 + coeffDot4 * yDot4; } } } } ././@LongLink100644 0 0 150 12126630646 10255 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/ode/nonstiff/MidpointStepInterpolator.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/ode/nonstiff/MidpointStepInterpolator.j100644 1750 1750 10331 12126627715 32335 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.ode.nonstiff; import org.apache.commons.math3.ode.sampling.StepInterpolator; /** * This class implements a step interpolator for second order * Runge-Kutta integrator. * *

                            This interpolator computes dense output inside the last * step computed. The interpolation equation is consistent with the * integration scheme : *

                              *
                            • Using reference point at step start:
                              * y(tn + θ h) = y (tn) + θ h [(1 - θ) y'1 + θ y'2] *
                            • *
                            • Using reference point at step end:
                              * y(tn + θ h) = y (tn + h) + (1-θ) h [θ y'1 - (1+θ) y'2] *
                            • *
                            *

                            * * where θ belongs to [0 ; 1] and where y'1 and y'2 are the two * evaluations of the derivatives already computed during the * step.

                            * * @see MidpointIntegrator * @version $Id: MidpointStepInterpolator.java 1416643 2012-12-03 19:37:14Z tn $ * @since 1.2 */ class MidpointStepInterpolator extends RungeKuttaStepInterpolator { /** Serializable version identifier */ private static final long serialVersionUID = 20111120L; /** Simple constructor. * This constructor builds an instance that is not usable yet, the * {@link * org.apache.commons.math3.ode.sampling.AbstractStepInterpolator#reinitialize} * method should be called before using the instance in order to * initialize the internal arrays. This constructor is used only * in order to delay the initialization in some cases. The {@link * RungeKuttaIntegrator} class uses the prototyping design pattern * to create the step interpolators by cloning an uninitialized model * and later initializing the copy. */ public MidpointStepInterpolator() { } /** Copy constructor. * @param interpolator interpolator to copy from. The copy is a deep * copy: its arrays are separated from the original arrays of the * instance */ public MidpointStepInterpolator(final MidpointStepInterpolator interpolator) { super(interpolator); } /** {@inheritDoc} */ @Override protected StepInterpolator doCopy() { return new MidpointStepInterpolator(this); } /** {@inheritDoc} */ @Override protected void computeInterpolatedStateAndDerivatives(final double theta, final double oneMinusThetaH) { final double coeffDot2 = 2 * theta; final double coeffDot1 = 1 - coeffDot2; if ((previousState != null) && (theta <= 0.5)) { final double coeff1 = theta * oneMinusThetaH; final double coeff2 = theta * theta * h; for (int i = 0; i < interpolatedState.length; ++i) { final double yDot1 = yDotK[0][i]; final double yDot2 = yDotK[1][i]; interpolatedState[i] = previousState[i] + coeff1 * yDot1 + coeff2 * yDot2; interpolatedDerivatives[i] = coeffDot1 * yDot1 + coeffDot2 * yDot2; } } else { final double coeff1 = oneMinusThetaH * theta; final double coeff2 = oneMinusThetaH * (1.0 + theta); for (int i = 0; i < interpolatedState.length; ++i) { final double yDot1 = yDotK[0][i]; final double yDot2 = yDotK[1][i]; interpolatedState[i] = currentState[i] + coeff1 * yDot1 - coeff2 * yDot2; interpolatedDerivatives[i] = coeffDot1 * yDot1 + coeffDot2 * yDot2; } } } } ././@LongLink100644 0 0 146 12126630646 10262 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/ode/nonstiff/AdamsMoultonIntegrator.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/ode/nonstiff/AdamsMoultonIntegrator.jav100644 1750 1750 44714 12126627715 32320 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.ode.nonstiff; import java.util.Arrays; import org.apache.commons.math3.exception.DimensionMismatchException; import org.apache.commons.math3.exception.MaxCountExceededException; import org.apache.commons.math3.exception.NoBracketingException; import org.apache.commons.math3.exception.NumberIsTooSmallException; import org.apache.commons.math3.linear.Array2DRowRealMatrix; import org.apache.commons.math3.linear.RealMatrixPreservingVisitor; import org.apache.commons.math3.ode.EquationsMapper; import org.apache.commons.math3.ode.ExpandableStatefulODE; import org.apache.commons.math3.ode.sampling.NordsieckStepInterpolator; import org.apache.commons.math3.util.FastMath; /** * This class implements implicit Adams-Moulton integrators for Ordinary * Differential Equations. * *

                            Adams-Moulton methods (in fact due to Adams alone) are implicit * multistep ODE solvers. This implementation is a variation of the classical * one: it uses adaptive stepsize to implement error control, whereas * classical implementations are fixed step size. The value of state vector * at step n+1 is a simple combination of the value at step n and of the * derivatives at steps n+1, n, n-1 ... Since y'n+1 is needed to * compute yn+1, another method must be used to compute a first * estimate of yn+1, then compute y'n+1, then compute * a final estimate of yn+1 using the following formulas. Depending * on the number k of previous steps one wants to use for computing the next * value, different formulas are available for the final estimate:

                            *
                              *
                            • k = 1: yn+1 = yn + h y'n+1
                            • *
                            • k = 2: yn+1 = yn + h (y'n+1+y'n)/2
                            • *
                            • k = 3: yn+1 = yn + h (5y'n+1+8y'n-y'n-1)/12
                            • *
                            • k = 4: yn+1 = yn + h (9y'n+1+19y'n-5y'n-1+y'n-2)/24
                            • *
                            • ...
                            • *
                            * *

                            A k-steps Adams-Moulton method is of order k+1.

                            * *

                            Implementation details

                            * *

                            We define scaled derivatives si(n) at step n as: *

                             * s1(n) = h y'n for first derivative
                             * s2(n) = h2/2 y''n for second derivative
                             * s3(n) = h3/6 y'''n for third derivative
                             * ...
                             * sk(n) = hk/k! y(k)n for kth derivative
                             * 

                            * *

                            The definitions above use the classical representation with several previous first * derivatives. Lets define *

                             *   qn = [ s1(n-1) s1(n-2) ... s1(n-(k-1)) ]T
                             * 
                            * (we omit the k index in the notation for clarity). With these definitions, * Adams-Moulton methods can be written: *
                              *
                            • k = 1: yn+1 = yn + s1(n+1)
                            • *
                            • k = 2: yn+1 = yn + 1/2 s1(n+1) + [ 1/2 ] qn+1
                            • *
                            • k = 3: yn+1 = yn + 5/12 s1(n+1) + [ 8/12 -1/12 ] qn+1
                            • *
                            • k = 4: yn+1 = yn + 9/24 s1(n+1) + [ 19/24 -5/24 1/24 ] qn+1
                            • *
                            • ...
                            • *

                            * *

                            Instead of using the classical representation with first derivatives only (yn, * s1(n+1) and qn+1), our implementation uses the Nordsieck vector with * higher degrees scaled derivatives all taken at the same step (yn, s1(n) * and rn) where rn is defined as: *

                             * rn = [ s2(n), s3(n) ... sk(n) ]T
                             * 
                            * (here again we omit the k index in the notation for clarity) *

                            * *

                            Taylor series formulas show that for any index offset i, s1(n-i) can be * computed from s1(n), s2(n) ... sk(n), the formula being exact * for degree k polynomials. *

                             * s1(n-i) = s1(n) + ∑j j (-i)j-1 sj(n)
                             * 
                            * The previous formula can be used with several values for i to compute the transform between * classical representation and Nordsieck vector. The transform between rn * and qn resulting from the Taylor series formulas above is: *
                             * qn = s1(n) u + P rn
                             * 
                            * where u is the [ 1 1 ... 1 ]T vector and P is the (k-1)×(k-1) matrix built * with the j (-i)j-1 terms: *
                             *        [  -2   3   -4    5  ... ]
                             *        [  -4  12  -32   80  ... ]
                             *   P =  [  -6  27 -108  405  ... ]
                             *        [  -8  48 -256 1280  ... ]
                             *        [          ...           ]
                             * 

                            * *

                            Using the Nordsieck vector has several advantages: *

                              *
                            • it greatly simplifies step interpolation as the interpolator mainly applies * Taylor series formulas,
                            • *
                            • it simplifies step changes that occur when discrete events that truncate * the step are triggered,
                            • *
                            • it allows to extend the methods in order to support adaptive stepsize.
                            • *

                            * *

                            The predicted Nordsieck vector at step n+1 is computed from the Nordsieck vector at step * n as follows: *

                              *
                            • Yn+1 = yn + s1(n) + uT rn
                            • *
                            • S1(n+1) = h f(tn+1, Yn+1)
                            • *
                            • Rn+1 = (s1(n) - S1(n+1)) P-1 u + P-1 A P rn
                            • *
                            * where A is a rows shifting matrix (the lower left part is an identity matrix): *
                             *        [ 0 0   ...  0 0 | 0 ]
                             *        [ ---------------+---]
                             *        [ 1 0   ...  0 0 | 0 ]
                             *    A = [ 0 1   ...  0 0 | 0 ]
                             *        [       ...      | 0 ]
                             *        [ 0 0   ...  1 0 | 0 ]
                             *        [ 0 0   ...  0 1 | 0 ]
                             * 
                            * From this predicted vector, the corrected vector is computed as follows: *
                              *
                            • yn+1 = yn + S1(n+1) + [ -1 +1 -1 +1 ... ±1 ] rn+1
                            • *
                            • s1(n+1) = h f(tn+1, yn+1)
                            • *
                            • rn+1 = Rn+1 + (s1(n+1) - S1(n+1)) P-1 u
                            • *
                            * where the upper case Yn+1, S1(n+1) and Rn+1 represent the * predicted states whereas the lower case yn+1, sn+1 and rn+1 * represent the corrected states.

                            * *

                            The P-1u vector and the P-1 A P matrix do not depend on the state, * they only depend on k and therefore are precomputed once for all.

                            * * @version $Id: AdamsMoultonIntegrator.java 1463684 2013-04-02 19:04:13Z luc $ * @since 2.0 */ public class AdamsMoultonIntegrator extends AdamsIntegrator { /** Integrator method name. */ private static final String METHOD_NAME = "Adams-Moulton"; /** * Build an Adams-Moulton integrator with the given order and error control parameters. * @param nSteps number of steps of the method excluding the one being computed * @param minStep minimal step (sign is irrelevant, regardless of * integration direction, forward or backward), the last step can * be smaller than this * @param maxStep maximal step (sign is irrelevant, regardless of * integration direction, forward or backward), the last step can * be smaller than this * @param scalAbsoluteTolerance allowed absolute error * @param scalRelativeTolerance allowed relative error * @exception NumberIsTooSmallException if order is 1 or less */ public AdamsMoultonIntegrator(final int nSteps, final double minStep, final double maxStep, final double scalAbsoluteTolerance, final double scalRelativeTolerance) throws NumberIsTooSmallException { super(METHOD_NAME, nSteps, nSteps + 1, minStep, maxStep, scalAbsoluteTolerance, scalRelativeTolerance); } /** * Build an Adams-Moulton integrator with the given order and error control parameters. * @param nSteps number of steps of the method excluding the one being computed * @param minStep minimal step (sign is irrelevant, regardless of * integration direction, forward or backward), the last step can * be smaller than this * @param maxStep maximal step (sign is irrelevant, regardless of * integration direction, forward or backward), the last step can * be smaller than this * @param vecAbsoluteTolerance allowed absolute error * @param vecRelativeTolerance allowed relative error * @exception IllegalArgumentException if order is 1 or less */ public AdamsMoultonIntegrator(final int nSteps, final double minStep, final double maxStep, final double[] vecAbsoluteTolerance, final double[] vecRelativeTolerance) throws IllegalArgumentException { super(METHOD_NAME, nSteps, nSteps + 1, minStep, maxStep, vecAbsoluteTolerance, vecRelativeTolerance); } /** {@inheritDoc} */ @Override public void integrate(final ExpandableStatefulODE equations,final double t) throws NumberIsTooSmallException, DimensionMismatchException, MaxCountExceededException, NoBracketingException { sanityChecks(equations, t); setEquations(equations); final boolean forward = t > equations.getTime(); // initialize working arrays final double[] y0 = equations.getCompleteState(); final double[] y = y0.clone(); final double[] yDot = new double[y.length]; final double[] yTmp = new double[y.length]; final double[] predictedScaled = new double[y.length]; Array2DRowRealMatrix nordsieckTmp = null; // set up two interpolators sharing the integrator arrays final NordsieckStepInterpolator interpolator = new NordsieckStepInterpolator(); interpolator.reinitialize(y, forward, equations.getPrimaryMapper(), equations.getSecondaryMappers()); // set up integration control objects initIntegration(equations.getTime(), y0, t); // compute the initial Nordsieck vector using the configured starter integrator start(equations.getTime(), y, t); interpolator.reinitialize(stepStart, stepSize, scaled, nordsieck); interpolator.storeTime(stepStart); double hNew = stepSize; interpolator.rescale(hNew); isLastStep = false; do { double error = 10; while (error >= 1.0) { stepSize = hNew; // predict a first estimate of the state at step end (P in the PECE sequence) final double stepEnd = stepStart + stepSize; interpolator.setInterpolatedTime(stepEnd); final ExpandableStatefulODE expandable = getExpandable(); final EquationsMapper primary = expandable.getPrimaryMapper(); primary.insertEquationData(interpolator.getInterpolatedState(), yTmp); int index = 0; for (final EquationsMapper secondary : expandable.getSecondaryMappers()) { secondary.insertEquationData(interpolator.getInterpolatedSecondaryState(index), yTmp); ++index; } // evaluate a first estimate of the derivative (first E in the PECE sequence) computeDerivatives(stepEnd, yTmp, yDot); // update Nordsieck vector for (int j = 0; j < y0.length; ++j) { predictedScaled[j] = stepSize * yDot[j]; } nordsieckTmp = updateHighOrderDerivativesPhase1(nordsieck); updateHighOrderDerivativesPhase2(scaled, predictedScaled, nordsieckTmp); // apply correction (C in the PECE sequence) error = nordsieckTmp.walkInOptimizedOrder(new Corrector(y, predictedScaled, yTmp)); if (error >= 1.0) { // reject the step and attempt to reduce error by stepsize control final double factor = computeStepGrowShrinkFactor(error); hNew = filterStep(stepSize * factor, forward, false); interpolator.rescale(hNew); } } // evaluate a final estimate of the derivative (second E in the PECE sequence) final double stepEnd = stepStart + stepSize; computeDerivatives(stepEnd, yTmp, yDot); // update Nordsieck vector final double[] correctedScaled = new double[y0.length]; for (int j = 0; j < y0.length; ++j) { correctedScaled[j] = stepSize * yDot[j]; } updateHighOrderDerivativesPhase2(predictedScaled, correctedScaled, nordsieckTmp); // discrete events handling System.arraycopy(yTmp, 0, y, 0, y.length); interpolator.reinitialize(stepEnd, stepSize, correctedScaled, nordsieckTmp); interpolator.storeTime(stepStart); interpolator.shift(); interpolator.storeTime(stepEnd); stepStart = acceptStep(interpolator, y, yDot, t); scaled = correctedScaled; nordsieck = nordsieckTmp; if (!isLastStep) { // prepare next step interpolator.storeTime(stepStart); if (resetOccurred) { // some events handler has triggered changes that // invalidate the derivatives, we need to restart from scratch start(stepStart, y, t); interpolator.reinitialize(stepStart, stepSize, scaled, nordsieck); } // stepsize control for next step final double factor = computeStepGrowShrinkFactor(error); final double scaledH = stepSize * factor; final double nextT = stepStart + scaledH; final boolean nextIsLast = forward ? (nextT >= t) : (nextT <= t); hNew = filterStep(scaledH, forward, nextIsLast); final double filteredNextT = stepStart + hNew; final boolean filteredNextIsLast = forward ? (filteredNextT >= t) : (filteredNextT <= t); if (filteredNextIsLast) { hNew = t - stepStart; } interpolator.rescale(hNew); } } while (!isLastStep); // dispatch results equations.setTime(stepStart); equations.setCompleteState(y); resetInternalState(); } /** Corrector for current state in Adams-Moulton method. *

                            * This visitor implements the Taylor series formula: *

                                 * Yn+1 = yn + s1(n+1) + [ -1 +1 -1 +1 ... ±1 ] rn+1
                                 * 
                            *

                            */ private class Corrector implements RealMatrixPreservingVisitor { /** Previous state. */ private final double[] previous; /** Current scaled first derivative. */ private final double[] scaled; /** Current state before correction. */ private final double[] before; /** Current state after correction. */ private final double[] after; /** Simple constructor. * @param previous previous state * @param scaled current scaled first derivative * @param state state to correct (will be overwritten after visit) */ public Corrector(final double[] previous, final double[] scaled, final double[] state) { this.previous = previous; this.scaled = scaled; this.after = state; this.before = state.clone(); } /** {@inheritDoc} */ public void start(int rows, int columns, int startRow, int endRow, int startColumn, int endColumn) { Arrays.fill(after, 0.0); } /** {@inheritDoc} */ public void visit(int row, int column, double value) { if ((row & 0x1) == 0) { after[column] -= value; } else { after[column] += value; } } /** * End visiting the Nordsieck vector. *

                            The correction is used to control stepsize. So its amplitude is * considered to be an error, which must be normalized according to * error control settings. If the normalized value is greater than 1, * the correction was too large and the step must be rejected.

                            * @return the normalized correction, if greater than 1, the step * must be rejected */ public double end() { double error = 0; for (int i = 0; i < after.length; ++i) { after[i] += previous[i] + scaled[i]; if (i < mainSetDimension) { final double yScale = FastMath.max(FastMath.abs(previous[i]), FastMath.abs(after[i])); final double tol = (vecAbsoluteTolerance == null) ? (scalAbsoluteTolerance + scalRelativeTolerance * yScale) : (vecAbsoluteTolerance[i] + vecRelativeTolerance[i] * yScale); final double ratio = (after[i] - before[i]) / tol; error += ratio * ratio; } } return FastMath.sqrt(error / mainSetDimension); } } } ././@LongLink100644 0 0 147 12126630646 10263 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/ode/nonstiff/ThreeEighthesIntegrator.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/ode/nonstiff/ThreeEighthesIntegrator.ja100644 1750 1750 4301 12126627715 32223 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.ode.nonstiff; /** * This class implements the 3/8 fourth order Runge-Kutta * integrator for Ordinary Differential Equations. * *

                            This method is an explicit Runge-Kutta method, its Butcher-array * is the following one : *

                             *    0  |  0    0    0    0
                             *   1/3 | 1/3   0    0    0
                             *   2/3 |-1/3   1    0    0
                             *    1  |  1   -1    1    0
                             *       |--------------------
                             *       | 1/8  3/8  3/8  1/8
                             * 
                            *

                            * * @see EulerIntegrator * @see ClassicalRungeKuttaIntegrator * @see GillIntegrator * @see MidpointIntegrator * @version $Id: ThreeEighthesIntegrator.java 1416643 2012-12-03 19:37:14Z tn $ * @since 1.2 */ public class ThreeEighthesIntegrator extends RungeKuttaIntegrator { /** Time steps Butcher array. */ private static final double[] STATIC_C = { 1.0 / 3.0, 2.0 / 3.0, 1.0 }; /** Internal weights Butcher array. */ private static final double[][] STATIC_A = { { 1.0 / 3.0 }, { -1.0 / 3.0, 1.0 }, { 1.0, -1.0, 1.0 } }; /** Propagation weights Butcher array. */ private static final double[] STATIC_B = { 1.0 / 8.0, 3.0 / 8.0, 3.0 / 8.0, 1.0 / 8.0 }; /** Simple constructor. * Build a 3/8 integrator with the given step. * @param step integration step */ public ThreeEighthesIntegrator(final double step) { super("3/8", STATIC_C, STATIC_A, STATIC_B, new ThreeEighthesStepInterpolator(), step); } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/ode/nonstiff/MidpointIntegrator.java100644 1750 1750 3721 12126627715 31612 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.ode.nonstiff; /** * This class implements a second order Runge-Kutta integrator for * Ordinary Differential Equations. * *

                            This method is an explicit Runge-Kutta method, its Butcher-array * is the following one : *

                             *    0  |  0    0
                             *   1/2 | 1/2   0
                             *       |----------
                             *       |  0    1
                             * 
                            *

                            * * @see EulerIntegrator * @see ClassicalRungeKuttaIntegrator * @see GillIntegrator * * @version $Id: MidpointIntegrator.java 1416643 2012-12-03 19:37:14Z tn $ * @since 1.2 */ public class MidpointIntegrator extends RungeKuttaIntegrator { /** Time steps Butcher array. */ private static final double[] STATIC_C = { 1.0 / 2.0 }; /** Internal weights Butcher array. */ private static final double[][] STATIC_A = { { 1.0 / 2.0 } }; /** Propagation weights Butcher array. */ private static final double[] STATIC_B = { 0.0, 1.0 }; /** Simple constructor. * Build a midpoint integrator with the given step. * @param step integration step */ public MidpointIntegrator(final double step) { super("midpoint", STATIC_C, STATIC_A, STATIC_B, new MidpointStepInterpolator(), step); } } ././@LongLink100644 0 0 163 12126630646 10261 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/ode/nonstiff/ClassicalRungeKuttaStepInterpolator.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/ode/nonstiff/ClassicalRungeKuttaStepInt100644 1750 1750 13616 12126627715 32313 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.ode.nonstiff; import org.apache.commons.math3.ode.sampling.StepInterpolator; /** * This class implements a step interpolator for the classical fourth * order Runge-Kutta integrator. * *

                            This interpolator allows to compute dense output inside the last * step computed. The interpolation equation is consistent with the * integration scheme : *

                              *
                            • Using reference point at step start:
                              * y(tn + θ h) = y (tn) * + θ (h/6) [ (6 - 9 θ + 4 θ2) y'1 * + ( 6 θ - 4 θ2) (y'2 + y'3) * + ( -3 θ + 4 θ2) y'4 * ] *
                            • *
                            • Using reference point at step end:
                              * y(tn + θ h) = y (tn + h) * + (1 - θ) (h/6) [ (-4 θ^2 + 5 θ - 1) y'1 * +(4 θ^2 - 2 θ - 2) (y'2 + y'3) * -(4 θ^2 + θ + 1) y'4 * ] *
                            • *
                            *

                            * * where θ belongs to [0 ; 1] and where y'1 to y'4 are the four * evaluations of the derivatives already computed during the * step.

                            * * @see ClassicalRungeKuttaIntegrator * @version $Id: ClassicalRungeKuttaStepInterpolator.java 1416643 2012-12-03 19:37:14Z tn $ * @since 1.2 */ class ClassicalRungeKuttaStepInterpolator extends RungeKuttaStepInterpolator { /** Serializable version identifier. */ private static final long serialVersionUID = 20111120L; /** Simple constructor. * This constructor builds an instance that is not usable yet, the * {@link RungeKuttaStepInterpolator#reinitialize} method should be * called before using the instance in order to initialize the * internal arrays. This constructor is used only in order to delay * the initialization in some cases. The {@link RungeKuttaIntegrator} * class uses the prototyping design pattern to create the step * interpolators by cloning an uninitialized model and latter initializing * the copy. */ public ClassicalRungeKuttaStepInterpolator() { } /** Copy constructor. * @param interpolator interpolator to copy from. The copy is a deep * copy: its arrays are separated from the original arrays of the * instance */ public ClassicalRungeKuttaStepInterpolator(final ClassicalRungeKuttaStepInterpolator interpolator) { super(interpolator); } /** {@inheritDoc} */ @Override protected StepInterpolator doCopy() { return new ClassicalRungeKuttaStepInterpolator(this); } /** {@inheritDoc} */ @Override protected void computeInterpolatedStateAndDerivatives(final double theta, final double oneMinusThetaH) { final double oneMinusTheta = 1 - theta; final double oneMinus2Theta = 1 - 2 * theta; final double coeffDot1 = oneMinusTheta * oneMinus2Theta; final double coeffDot23 = 2 * theta * oneMinusTheta; final double coeffDot4 = -theta * oneMinus2Theta; if ((previousState != null) && (theta <= 0.5)) { final double fourTheta2 = 4 * theta * theta; final double s = theta * h / 6.0; final double coeff1 = s * ( 6 - 9 * theta + fourTheta2); final double coeff23 = s * ( 6 * theta - fourTheta2); final double coeff4 = s * (-3 * theta + fourTheta2); for (int i = 0; i < interpolatedState.length; ++i) { final double yDot1 = yDotK[0][i]; final double yDot23 = yDotK[1][i] + yDotK[2][i]; final double yDot4 = yDotK[3][i]; interpolatedState[i] = previousState[i] + coeff1 * yDot1 + coeff23 * yDot23 + coeff4 * yDot4; interpolatedDerivatives[i] = coeffDot1 * yDot1 + coeffDot23 * yDot23 + coeffDot4 * yDot4; } } else { final double fourTheta = 4 * theta; final double s = oneMinusThetaH / 6.0; final double coeff1 = s * ((-fourTheta + 5) * theta - 1); final double coeff23 = s * (( fourTheta - 2) * theta - 2); final double coeff4 = s * ((-fourTheta - 1) * theta - 1); for (int i = 0; i < interpolatedState.length; ++i) { final double yDot1 = yDotK[0][i]; final double yDot23 = yDotK[1][i] + yDotK[2][i]; final double yDot4 = yDotK[3][i]; interpolatedState[i] = currentState[i] + coeff1 * yDot1 + coeff23 * yDot23 + coeff4 * yDot4; interpolatedDerivatives[i] = coeffDot1 * yDot1 + coeffDot23 * yDot23 + coeffDot4 * yDot4; } } } } ././@LongLink100644 0 0 160 12126630646 10256 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/ode/nonstiff/DormandPrince853StepInterpolator.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/ode/nonstiff/DormandPrince853StepInterp100644 1750 1750 46707 12126627715 32106 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.ode.nonstiff; import java.io.IOException; import java.io.ObjectInput; import java.io.ObjectOutput; import org.apache.commons.math3.exception.MaxCountExceededException; import org.apache.commons.math3.ode.AbstractIntegrator; import org.apache.commons.math3.ode.EquationsMapper; import org.apache.commons.math3.ode.sampling.StepInterpolator; /** * This class represents an interpolator over the last step during an * ODE integration for the 8(5,3) Dormand-Prince integrator. * * @see DormandPrince853Integrator * * @version $Id: DormandPrince853StepInterpolator.java 1416643 2012-12-03 19:37:14Z tn $ * @since 1.2 */ class DormandPrince853StepInterpolator extends RungeKuttaStepInterpolator { /** Serializable version identifier. */ private static final long serialVersionUID = 20111120L; /** Propagation weights, element 1. */ private static final double B_01 = 104257.0 / 1920240.0; // elements 2 to 5 are zero, so they are neither stored nor used /** Propagation weights, element 6. */ private static final double B_06 = 3399327.0 / 763840.0; /** Propagation weights, element 7. */ private static final double B_07 = 66578432.0 / 35198415.0; /** Propagation weights, element 8. */ private static final double B_08 = -1674902723.0 / 288716400.0; /** Propagation weights, element 9. */ private static final double B_09 = 54980371265625.0 / 176692375811392.0; /** Propagation weights, element 10. */ private static final double B_10 = -734375.0 / 4826304.0; /** Propagation weights, element 11. */ private static final double B_11 = 171414593.0 / 851261400.0; /** Propagation weights, element 12. */ private static final double B_12 = 137909.0 / 3084480.0; /** Time step for stage 14 (interpolation only). */ private static final double C14 = 1.0 / 10.0; /** Internal weights for stage 14, element 1. */ private static final double K14_01 = 13481885573.0 / 240030000000.0 - B_01; // elements 2 to 5 are zero, so they are neither stored nor used /** Internal weights for stage 14, element 6. */ private static final double K14_06 = 0.0 - B_06; /** Internal weights for stage 14, element 7. */ private static final double K14_07 = 139418837528.0 / 549975234375.0 - B_07; /** Internal weights for stage 14, element 8. */ private static final double K14_08 = -11108320068443.0 / 45111937500000.0 - B_08; /** Internal weights for stage 14, element 9. */ private static final double K14_09 = -1769651421925959.0 / 14249385146080000.0 - B_09; /** Internal weights for stage 14, element 10. */ private static final double K14_10 = 57799439.0 / 377055000.0 - B_10; /** Internal weights for stage 14, element 11. */ private static final double K14_11 = 793322643029.0 / 96734250000000.0 - B_11; /** Internal weights for stage 14, element 12. */ private static final double K14_12 = 1458939311.0 / 192780000000.0 - B_12; /** Internal weights for stage 14, element 13. */ private static final double K14_13 = -4149.0 / 500000.0; /** Time step for stage 15 (interpolation only). */ private static final double C15 = 1.0 / 5.0; /** Internal weights for stage 15, element 1. */ private static final double K15_01 = 1595561272731.0 / 50120273500000.0 - B_01; // elements 2 to 5 are zero, so they are neither stored nor used /** Internal weights for stage 15, element 6. */ private static final double K15_06 = 975183916491.0 / 34457688031250.0 - B_06; /** Internal weights for stage 15, element 7. */ private static final double K15_07 = 38492013932672.0 / 718912673015625.0 - B_07; /** Internal weights for stage 15, element 8. */ private static final double K15_08 = -1114881286517557.0 / 20298710767500000.0 - B_08; /** Internal weights for stage 15, element 9. */ private static final double K15_09 = 0.0 - B_09; /** Internal weights for stage 15, element 10. */ private static final double K15_10 = 0.0 - B_10; /** Internal weights for stage 15, element 11. */ private static final double K15_11 = -2538710946863.0 / 23431227861250000.0 - B_11; /** Internal weights for stage 15, element 12. */ private static final double K15_12 = 8824659001.0 / 23066716781250.0 - B_12; /** Internal weights for stage 15, element 13. */ private static final double K15_13 = -11518334563.0 / 33831184612500.0; /** Internal weights for stage 15, element 14. */ private static final double K15_14 = 1912306948.0 / 13532473845.0; /** Time step for stage 16 (interpolation only). */ private static final double C16 = 7.0 / 9.0; /** Internal weights for stage 16, element 1. */ private static final double K16_01 = -13613986967.0 / 31741908048.0 - B_01; // elements 2 to 5 are zero, so they are neither stored nor used /** Internal weights for stage 16, element 6. */ private static final double K16_06 = -4755612631.0 / 1012344804.0 - B_06; /** Internal weights for stage 16, element 7. */ private static final double K16_07 = 42939257944576.0 / 5588559685701.0 - B_07; /** Internal weights for stage 16, element 8. */ private static final double K16_08 = 77881972900277.0 / 19140370552944.0 - B_08; /** Internal weights for stage 16, element 9. */ private static final double K16_09 = 22719829234375.0 / 63689648654052.0 - B_09; /** Internal weights for stage 16, element 10. */ private static final double K16_10 = 0.0 - B_10; /** Internal weights for stage 16, element 11. */ private static final double K16_11 = 0.0 - B_11; /** Internal weights for stage 16, element 12. */ private static final double K16_12 = 0.0 - B_12; /** Internal weights for stage 16, element 13. */ private static final double K16_13 = -1199007803.0 / 857031517296.0; /** Internal weights for stage 16, element 14. */ private static final double K16_14 = 157882067000.0 / 53564469831.0; /** Internal weights for stage 16, element 15. */ private static final double K16_15 = -290468882375.0 / 31741908048.0; /** Interpolation weights. * (beware that only the non-null values are in the table) */ private static final double[][] D = { { -17751989329.0 / 2106076560.0, 4272954039.0 / 7539864640.0, -118476319744.0 / 38604839385.0, 755123450731.0 / 316657731600.0, 3692384461234828125.0 / 1744130441634250432.0, -4612609375.0 / 5293382976.0, 2091772278379.0 / 933644586600.0, 2136624137.0 / 3382989120.0, -126493.0 / 1421424.0, 98350000.0 / 5419179.0, -18878125.0 / 2053168.0, -1944542619.0 / 438351368.0}, { 32941697297.0 / 3159114840.0, 456696183123.0 / 1884966160.0, 19132610714624.0 / 115814518155.0, -177904688592943.0 / 474986597400.0, -4821139941836765625.0 / 218016305204281304.0, 30702015625.0 / 3970037232.0, -85916079474274.0 / 2800933759800.0, -5919468007.0 / 634310460.0, 2479159.0 / 157936.0, -18750000.0 / 602131.0, -19203125.0 / 2053168.0, 15700361463.0 / 438351368.0}, { 12627015655.0 / 631822968.0, -72955222965.0 / 188496616.0, -13145744952320.0 / 69488710893.0, 30084216194513.0 / 56998391688.0, -296858761006640625.0 / 25648977082856624.0, 569140625.0 / 82709109.0, -18684190637.0 / 18672891732.0, 69644045.0 / 89549712.0, -11847025.0 / 4264272.0, -978650000.0 / 16257537.0, 519371875.0 / 6159504.0, 5256837225.0 / 438351368.0}, { -450944925.0 / 17550638.0, -14532122925.0 / 94248308.0, -595876966400.0 / 2573655959.0, 188748653015.0 / 527762886.0, 2545485458115234375.0 / 27252038150535163.0, -1376953125.0 / 36759604.0, 53995596795.0 / 518691437.0, 210311225.0 / 7047894.0, -1718875.0 / 39484.0, 58000000.0 / 602131.0, -1546875.0 / 39484.0, -1262172375.0 / 8429834.0} }; /** Last evaluations. */ private double[][] yDotKLast; /** Vectors for interpolation. */ private double[][] v; /** Initialization indicator for the interpolation vectors. */ private boolean vectorsInitialized; /** Simple constructor. * This constructor builds an instance that is not usable yet, the * {@link #reinitialize} method should be called before using the * instance in order to initialize the internal arrays. This * constructor is used only in order to delay the initialization in * some cases. The {@link EmbeddedRungeKuttaIntegrator} uses the * prototyping design pattern to create the step interpolators by * cloning an uninitialized model and latter initializing the copy. */ public DormandPrince853StepInterpolator() { super(); yDotKLast = null; v = null; vectorsInitialized = false; } /** Copy constructor. * @param interpolator interpolator to copy from. The copy is a deep * copy: its arrays are separated from the original arrays of the * instance */ public DormandPrince853StepInterpolator(final DormandPrince853StepInterpolator interpolator) { super(interpolator); if (interpolator.currentState == null) { yDotKLast = null; v = null; vectorsInitialized = false; } else { final int dimension = interpolator.currentState.length; yDotKLast = new double[3][]; for (int k = 0; k < yDotKLast.length; ++k) { yDotKLast[k] = new double[dimension]; System.arraycopy(interpolator.yDotKLast[k], 0, yDotKLast[k], 0, dimension); } v = new double[7][]; for (int k = 0; k < v.length; ++k) { v[k] = new double[dimension]; System.arraycopy(interpolator.v[k], 0, v[k], 0, dimension); } vectorsInitialized = interpolator.vectorsInitialized; } } /** {@inheritDoc} */ @Override protected StepInterpolator doCopy() { return new DormandPrince853StepInterpolator(this); } /** {@inheritDoc} */ @Override public void reinitialize(final AbstractIntegrator integrator, final double[] y, final double[][] yDotK, final boolean forward, final EquationsMapper primaryMapper, final EquationsMapper[] secondaryMappers) { super.reinitialize(integrator, y, yDotK, forward, primaryMapper, secondaryMappers); final int dimension = currentState.length; yDotKLast = new double[3][]; for (int k = 0; k < yDotKLast.length; ++k) { yDotKLast[k] = new double[dimension]; } v = new double[7][]; for (int k = 0; k < v.length; ++k) { v[k] = new double[dimension]; } vectorsInitialized = false; } /** {@inheritDoc} */ @Override public void storeTime(final double t) { super.storeTime(t); vectorsInitialized = false; } /** {@inheritDoc} */ @Override protected void computeInterpolatedStateAndDerivatives(final double theta, final double oneMinusThetaH) throws MaxCountExceededException { if (! vectorsInitialized) { if (v == null) { v = new double[7][]; for (int k = 0; k < 7; ++k) { v[k] = new double[interpolatedState.length]; } } // perform the last evaluations if they have not been done yet finalizeStep(); // compute the interpolation vectors for this time step for (int i = 0; i < interpolatedState.length; ++i) { final double yDot1 = yDotK[0][i]; final double yDot6 = yDotK[5][i]; final double yDot7 = yDotK[6][i]; final double yDot8 = yDotK[7][i]; final double yDot9 = yDotK[8][i]; final double yDot10 = yDotK[9][i]; final double yDot11 = yDotK[10][i]; final double yDot12 = yDotK[11][i]; final double yDot13 = yDotK[12][i]; final double yDot14 = yDotKLast[0][i]; final double yDot15 = yDotKLast[1][i]; final double yDot16 = yDotKLast[2][i]; v[0][i] = B_01 * yDot1 + B_06 * yDot6 + B_07 * yDot7 + B_08 * yDot8 + B_09 * yDot9 + B_10 * yDot10 + B_11 * yDot11 + B_12 * yDot12; v[1][i] = yDot1 - v[0][i]; v[2][i] = v[0][i] - v[1][i] - yDotK[12][i]; for (int k = 0; k < D.length; ++k) { v[k+3][i] = D[k][0] * yDot1 + D[k][1] * yDot6 + D[k][2] * yDot7 + D[k][3] * yDot8 + D[k][4] * yDot9 + D[k][5] * yDot10 + D[k][6] * yDot11 + D[k][7] * yDot12 + D[k][8] * yDot13 + D[k][9] * yDot14 + D[k][10] * yDot15 + D[k][11] * yDot16; } } vectorsInitialized = true; } final double eta = 1 - theta; final double twoTheta = 2 * theta; final double theta2 = theta * theta; final double dot1 = 1 - twoTheta; final double dot2 = theta * (2 - 3 * theta); final double dot3 = twoTheta * (1 + theta * (twoTheta -3)); final double dot4 = theta2 * (3 + theta * (5 * theta - 8)); final double dot5 = theta2 * (3 + theta * (-12 + theta * (15 - 6 * theta))); final double dot6 = theta2 * theta * (4 + theta * (-15 + theta * (18 - 7 * theta))); if ((previousState != null) && (theta <= 0.5)) { for (int i = 0; i < interpolatedState.length; ++i) { interpolatedState[i] = previousState[i] + theta * h * (v[0][i] + eta * (v[1][i] + theta * (v[2][i] + eta * (v[3][i] + theta * (v[4][i] + eta * (v[5][i] + theta * (v[6][i]))))))); interpolatedDerivatives[i] = v[0][i] + dot1 * v[1][i] + dot2 * v[2][i] + dot3 * v[3][i] + dot4 * v[4][i] + dot5 * v[5][i] + dot6 * v[6][i]; } } else { for (int i = 0; i < interpolatedState.length; ++i) { interpolatedState[i] = currentState[i] - oneMinusThetaH * (v[0][i] - theta * (v[1][i] + theta * (v[2][i] + eta * (v[3][i] + theta * (v[4][i] + eta * (v[5][i] + theta * (v[6][i]))))))); interpolatedDerivatives[i] = v[0][i] + dot1 * v[1][i] + dot2 * v[2][i] + dot3 * v[3][i] + dot4 * v[4][i] + dot5 * v[5][i] + dot6 * v[6][i]; } } } /** {@inheritDoc} */ @Override protected void doFinalize() throws MaxCountExceededException { if (currentState == null) { // we are finalizing an uninitialized instance return; } double s; final double[] yTmp = new double[currentState.length]; final double pT = getGlobalPreviousTime(); // k14 for (int j = 0; j < currentState.length; ++j) { s = K14_01 * yDotK[0][j] + K14_06 * yDotK[5][j] + K14_07 * yDotK[6][j] + K14_08 * yDotK[7][j] + K14_09 * yDotK[8][j] + K14_10 * yDotK[9][j] + K14_11 * yDotK[10][j] + K14_12 * yDotK[11][j] + K14_13 * yDotK[12][j]; yTmp[j] = currentState[j] + h * s; } integrator.computeDerivatives(pT + C14 * h, yTmp, yDotKLast[0]); // k15 for (int j = 0; j < currentState.length; ++j) { s = K15_01 * yDotK[0][j] + K15_06 * yDotK[5][j] + K15_07 * yDotK[6][j] + K15_08 * yDotK[7][j] + K15_09 * yDotK[8][j] + K15_10 * yDotK[9][j] + K15_11 * yDotK[10][j] + K15_12 * yDotK[11][j] + K15_13 * yDotK[12][j] + K15_14 * yDotKLast[0][j]; yTmp[j] = currentState[j] + h * s; } integrator.computeDerivatives(pT + C15 * h, yTmp, yDotKLast[1]); // k16 for (int j = 0; j < currentState.length; ++j) { s = K16_01 * yDotK[0][j] + K16_06 * yDotK[5][j] + K16_07 * yDotK[6][j] + K16_08 * yDotK[7][j] + K16_09 * yDotK[8][j] + K16_10 * yDotK[9][j] + K16_11 * yDotK[10][j] + K16_12 * yDotK[11][j] + K16_13 * yDotK[12][j] + K16_14 * yDotKLast[0][j] + K16_15 * yDotKLast[1][j]; yTmp[j] = currentState[j] + h * s; } integrator.computeDerivatives(pT + C16 * h, yTmp, yDotKLast[2]); } /** {@inheritDoc} */ @Override public void writeExternal(final ObjectOutput out) throws IOException { try { // save the local attributes finalizeStep(); } catch (MaxCountExceededException mcee) { final IOException ioe = new IOException(mcee.getLocalizedMessage()); ioe.initCause(mcee); throw ioe; } final int dimension = (currentState == null) ? -1 : currentState.length; out.writeInt(dimension); for (int i = 0; i < dimension; ++i) { out.writeDouble(yDotKLast[0][i]); out.writeDouble(yDotKLast[1][i]); out.writeDouble(yDotKLast[2][i]); } // save the state of the base class super.writeExternal(out); } /** {@inheritDoc} */ @Override public void readExternal(final ObjectInput in) throws IOException, ClassNotFoundException { // read the local attributes yDotKLast = new double[3][]; final int dimension = in.readInt(); yDotKLast[0] = (dimension < 0) ? null : new double[dimension]; yDotKLast[1] = (dimension < 0) ? null : new double[dimension]; yDotKLast[2] = (dimension < 0) ? null : new double[dimension]; for (int i = 0; i < dimension; ++i) { yDotKLast[0][i] = in.readDouble(); yDotKLast[1][i] = in.readDouble(); yDotKLast[2][i] = in.readDouble(); } // read the base state super.readExternal(in); } } ././@LongLink100644 0 0 155 12126630646 10262 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/ode/nonstiff/ClassicalRungeKuttaIntegrator.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/ode/nonstiff/ClassicalRungeKuttaIntegra100644 1750 1750 4475 12126627715 32301 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.ode.nonstiff; /** * This class implements the classical fourth order Runge-Kutta * integrator for Ordinary Differential Equations (it is the most * often used Runge-Kutta method). * *

                            This method is an explicit Runge-Kutta method, its Butcher-array * is the following one : *

                             *    0  |  0    0    0    0
                             *   1/2 | 1/2   0    0    0
                             *   1/2 |  0   1/2   0    0
                             *    1  |  0    0    1    0
                             *       |--------------------
                             *       | 1/6  1/3  1/3  1/6
                             * 
                            *

                            * * @see EulerIntegrator * @see GillIntegrator * @see MidpointIntegrator * @see ThreeEighthesIntegrator * @version $Id: ClassicalRungeKuttaIntegrator.java 1416643 2012-12-03 19:37:14Z tn $ * @since 1.2 */ public class ClassicalRungeKuttaIntegrator extends RungeKuttaIntegrator { /** Time steps Butcher array. */ private static final double[] STATIC_C = { 1.0 / 2.0, 1.0 / 2.0, 1.0 }; /** Internal weights Butcher array. */ private static final double[][] STATIC_A = { { 1.0 / 2.0 }, { 0.0, 1.0 / 2.0 }, { 0.0, 0.0, 1.0 } }; /** Propagation weights Butcher array. */ private static final double[] STATIC_B = { 1.0 / 6.0, 1.0 / 3.0, 1.0 / 3.0, 1.0 / 6.0 }; /** Simple constructor. * Build a fourth-order Runge-Kutta integrator with the given * step. * @param step integration step */ public ClassicalRungeKuttaIntegrator(final double step) { super("classical Runge-Kutta", STATIC_C, STATIC_A, STATIC_B, new ClassicalRungeKuttaStepInterpolator(), step); } } ././@LongLink100644 0 0 162 12126630646 10260 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/ode/nonstiff/GraggBulirschStoerStepInterpolator.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/ode/nonstiff/GraggBulirschStoerStepInte100644 1750 1750 32716 12126627715 32312 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.ode.nonstiff; import java.io.IOException; import java.io.ObjectInput; import java.io.ObjectOutput; import org.apache.commons.math3.ode.EquationsMapper; import org.apache.commons.math3.ode.sampling.AbstractStepInterpolator; import org.apache.commons.math3.ode.sampling.StepInterpolator; import org.apache.commons.math3.util.FastMath; /** * This class implements an interpolator for the Gragg-Bulirsch-Stoer * integrator. * *

                            This interpolator compute dense output inside the last step * produced by a Gragg-Bulirsch-Stoer integrator.

                            * *

                            * This implementation is basically a reimplementation in Java of the * odex * fortran code by E. Hairer and G. Wanner. The redistribution policy * for this code is available here, for * convenience, it is reproduced below.

                            *

                            * * * * * * * *
                            Copyright (c) 2004, Ernst Hairer
                            Redistribution and use in source and binary forms, with or * without modification, are permitted provided that the following * conditions are met: *
                              *
                            • Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer.
                            • *
                            • Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution.
                            • *
                            THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
                            * * @see GraggBulirschStoerIntegrator * @version $Id: GraggBulirschStoerStepInterpolator.java 1416643 2012-12-03 19:37:14Z tn $ * @since 1.2 */ class GraggBulirschStoerStepInterpolator extends AbstractStepInterpolator { /** Serializable version identifier. */ private static final long serialVersionUID = 20110928L; /** Slope at the beginning of the step. */ private double[] y0Dot; /** State at the end of the step. */ private double[] y1; /** Slope at the end of the step. */ private double[] y1Dot; /** Derivatives at the middle of the step. * element 0 is state at midpoint, element 1 is first derivative ... */ private double[][] yMidDots; /** Interpolation polynomials. */ private double[][] polynomials; /** Error coefficients for the interpolation. */ private double[] errfac; /** Degree of the interpolation polynomials. */ private int currentDegree; /** Simple constructor. * This constructor should not be used directly, it is only intended * for the serialization process. */ public GraggBulirschStoerStepInterpolator() { y0Dot = null; y1 = null; y1Dot = null; yMidDots = null; resetTables(-1); } /** Simple constructor. * @param y reference to the integrator array holding the current state * @param y0Dot reference to the integrator array holding the slope * at the beginning of the step * @param y1 reference to the integrator array holding the state at * the end of the step * @param y1Dot reference to the integrator array holding the slope * at the end of the step * @param yMidDots reference to the integrator array holding the * derivatives at the middle point of the step * @param forward integration direction indicator * @param primaryMapper equations mapper for the primary equations set * @param secondaryMappers equations mappers for the secondary equations sets */ public GraggBulirschStoerStepInterpolator(final double[] y, final double[] y0Dot, final double[] y1, final double[] y1Dot, final double[][] yMidDots, final boolean forward, final EquationsMapper primaryMapper, final EquationsMapper[] secondaryMappers) { super(y, forward, primaryMapper, secondaryMappers); this.y0Dot = y0Dot; this.y1 = y1; this.y1Dot = y1Dot; this.yMidDots = yMidDots; resetTables(yMidDots.length + 4); } /** Copy constructor. * @param interpolator interpolator to copy from. The copy is a deep * copy: its arrays are separated from the original arrays of the * instance */ public GraggBulirschStoerStepInterpolator (final GraggBulirschStoerStepInterpolator interpolator) { super(interpolator); final int dimension = currentState.length; // the interpolator has been finalized, // the following arrays are not needed anymore y0Dot = null; y1 = null; y1Dot = null; yMidDots = null; // copy the interpolation polynomials (up to the current degree only) if (interpolator.polynomials == null) { polynomials = null; currentDegree = -1; } else { resetTables(interpolator.currentDegree); for (int i = 0; i < polynomials.length; ++i) { polynomials[i] = new double[dimension]; System.arraycopy(interpolator.polynomials[i], 0, polynomials[i], 0, dimension); } currentDegree = interpolator.currentDegree; } } /** Reallocate the internal tables. * Reallocate the internal tables in order to be able to handle * interpolation polynomials up to the given degree * @param maxDegree maximal degree to handle */ private void resetTables(final int maxDegree) { if (maxDegree < 0) { polynomials = null; errfac = null; currentDegree = -1; } else { final double[][] newPols = new double[maxDegree + 1][]; if (polynomials != null) { System.arraycopy(polynomials, 0, newPols, 0, polynomials.length); for (int i = polynomials.length; i < newPols.length; ++i) { newPols[i] = new double[currentState.length]; } } else { for (int i = 0; i < newPols.length; ++i) { newPols[i] = new double[currentState.length]; } } polynomials = newPols; // initialize the error factors array for interpolation if (maxDegree <= 4) { errfac = null; } else { errfac = new double[maxDegree - 4]; for (int i = 0; i < errfac.length; ++i) { final int ip5 = i + 5; errfac[i] = 1.0 / (ip5 * ip5); final double e = 0.5 * FastMath.sqrt (((double) (i + 1)) / ip5); for (int j = 0; j <= i; ++j) { errfac[i] *= e / (j + 1); } } } currentDegree = 0; } } /** {@inheritDoc} */ @Override protected StepInterpolator doCopy() { return new GraggBulirschStoerStepInterpolator(this); } /** Compute the interpolation coefficients for dense output. * @param mu degree of the interpolation polynomial * @param h current step */ public void computeCoefficients(final int mu, final double h) { if ((polynomials == null) || (polynomials.length <= (mu + 4))) { resetTables(mu + 4); } currentDegree = mu + 4; for (int i = 0; i < currentState.length; ++i) { final double yp0 = h * y0Dot[i]; final double yp1 = h * y1Dot[i]; final double ydiff = y1[i] - currentState[i]; final double aspl = ydiff - yp1; final double bspl = yp0 - ydiff; polynomials[0][i] = currentState[i]; polynomials[1][i] = ydiff; polynomials[2][i] = aspl; polynomials[3][i] = bspl; if (mu < 0) { return; } // compute the remaining coefficients final double ph0 = 0.5 * (currentState[i] + y1[i]) + 0.125 * (aspl + bspl); polynomials[4][i] = 16 * (yMidDots[0][i] - ph0); if (mu > 0) { final double ph1 = ydiff + 0.25 * (aspl - bspl); polynomials[5][i] = 16 * (yMidDots[1][i] - ph1); if (mu > 1) { final double ph2 = yp1 - yp0; polynomials[6][i] = 16 * (yMidDots[2][i] - ph2 + polynomials[4][i]); if (mu > 2) { final double ph3 = 6 * (bspl - aspl); polynomials[7][i] = 16 * (yMidDots[3][i] - ph3 + 3 * polynomials[5][i]); for (int j = 4; j <= mu; ++j) { final double fac1 = 0.5 * j * (j - 1); final double fac2 = 2 * fac1 * (j - 2) * (j - 3); polynomials[j+4][i] = 16 * (yMidDots[j][i] + fac1 * polynomials[j+2][i] - fac2 * polynomials[j][i]); } } } } } } /** Estimate interpolation error. * @param scale scaling array * @return estimate of the interpolation error */ public double estimateError(final double[] scale) { double error = 0; if (currentDegree >= 5) { for (int i = 0; i < scale.length; ++i) { final double e = polynomials[currentDegree][i] / scale[i]; error += e * e; } error = FastMath.sqrt(error / scale.length) * errfac[currentDegree - 5]; } return error; } /** {@inheritDoc} */ @Override protected void computeInterpolatedStateAndDerivatives(final double theta, final double oneMinusThetaH) { final int dimension = currentState.length; final double oneMinusTheta = 1.0 - theta; final double theta05 = theta - 0.5; final double tOmT = theta * oneMinusTheta; final double t4 = tOmT * tOmT; final double t4Dot = 2 * tOmT * (1 - 2 * theta); final double dot1 = 1.0 / h; final double dot2 = theta * (2 - 3 * theta) / h; final double dot3 = ((3 * theta - 4) * theta + 1) / h; for (int i = 0; i < dimension; ++i) { final double p0 = polynomials[0][i]; final double p1 = polynomials[1][i]; final double p2 = polynomials[2][i]; final double p3 = polynomials[3][i]; interpolatedState[i] = p0 + theta * (p1 + oneMinusTheta * (p2 * theta + p3 * oneMinusTheta)); interpolatedDerivatives[i] = dot1 * p1 + dot2 * p2 + dot3 * p3; if (currentDegree > 3) { double cDot = 0; double c = polynomials[currentDegree][i]; for (int j = currentDegree - 1; j > 3; --j) { final double d = 1.0 / (j - 3); cDot = d * (theta05 * cDot + c); c = polynomials[j][i] + c * d * theta05; } interpolatedState[i] += t4 * c; interpolatedDerivatives[i] += (t4 * cDot + t4Dot * c) / h; } } if (h == 0) { // in this degenerated case, the previous computation leads to NaN for derivatives // we fix this by using the derivatives at midpoint System.arraycopy(yMidDots[1], 0, interpolatedDerivatives, 0, dimension); } } /** {@inheritDoc} */ @Override public void writeExternal(final ObjectOutput out) throws IOException { final int dimension = (currentState == null) ? -1 : currentState.length; // save the state of the base class writeBaseExternal(out); // save the local attributes (but not the temporary vectors) out.writeInt(currentDegree); for (int k = 0; k <= currentDegree; ++k) { for (int l = 0; l < dimension; ++l) { out.writeDouble(polynomials[k][l]); } } } /** {@inheritDoc} */ @Override public void readExternal(final ObjectInput in) throws IOException, ClassNotFoundException { // read the base class final double t = readBaseExternal(in); final int dimension = (currentState == null) ? -1 : currentState.length; // read the local attributes final int degree = in.readInt(); resetTables(degree); currentDegree = degree; for (int k = 0; k <= currentDegree; ++k) { for (int l = 0; l < dimension; ++l) { polynomials[k][l] = in.readDouble(); } } // we can now set the interpolated time and state setInterpolatedTime(t); } } ././@LongLink100644 0 0 157 12126630646 10264 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/ode/nonstiff/DormandPrince54StepInterpolator.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/ode/nonstiff/DormandPrince54StepInterpo100644 1750 1750 16752 12126627715 32173 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.ode.nonstiff; import org.apache.commons.math3.ode.AbstractIntegrator; import org.apache.commons.math3.ode.EquationsMapper; import org.apache.commons.math3.ode.sampling.StepInterpolator; /** * This class represents an interpolator over the last step during an * ODE integration for the 5(4) Dormand-Prince integrator. * * @see DormandPrince54Integrator * * @version $Id: DormandPrince54StepInterpolator.java 1416643 2012-12-03 19:37:14Z tn $ * @since 1.2 */ class DormandPrince54StepInterpolator extends RungeKuttaStepInterpolator { /** Last row of the Butcher-array internal weights, element 0. */ private static final double A70 = 35.0 / 384.0; // element 1 is zero, so it is neither stored nor used /** Last row of the Butcher-array internal weights, element 2. */ private static final double A72 = 500.0 / 1113.0; /** Last row of the Butcher-array internal weights, element 3. */ private static final double A73 = 125.0 / 192.0; /** Last row of the Butcher-array internal weights, element 4. */ private static final double A74 = -2187.0 / 6784.0; /** Last row of the Butcher-array internal weights, element 5. */ private static final double A75 = 11.0 / 84.0; /** Shampine (1986) Dense output, element 0. */ private static final double D0 = -12715105075.0 / 11282082432.0; // element 1 is zero, so it is neither stored nor used /** Shampine (1986) Dense output, element 2. */ private static final double D2 = 87487479700.0 / 32700410799.0; /** Shampine (1986) Dense output, element 3. */ private static final double D3 = -10690763975.0 / 1880347072.0; /** Shampine (1986) Dense output, element 4. */ private static final double D4 = 701980252875.0 / 199316789632.0; /** Shampine (1986) Dense output, element 5. */ private static final double D5 = -1453857185.0 / 822651844.0; /** Shampine (1986) Dense output, element 6. */ private static final double D6 = 69997945.0 / 29380423.0; /** Serializable version identifier. */ private static final long serialVersionUID = 20111120L; /** First vector for interpolation. */ private double[] v1; /** Second vector for interpolation. */ private double[] v2; /** Third vector for interpolation. */ private double[] v3; /** Fourth vector for interpolation. */ private double[] v4; /** Initialization indicator for the interpolation vectors. */ private boolean vectorsInitialized; /** Simple constructor. * This constructor builds an instance that is not usable yet, the * {@link #reinitialize} method should be called before using the * instance in order to initialize the internal arrays. This * constructor is used only in order to delay the initialization in * some cases. The {@link EmbeddedRungeKuttaIntegrator} uses the * prototyping design pattern to create the step interpolators by * cloning an uninitialized model and latter initializing the copy. */ public DormandPrince54StepInterpolator() { super(); v1 = null; v2 = null; v3 = null; v4 = null; vectorsInitialized = false; } /** Copy constructor. * @param interpolator interpolator to copy from. The copy is a deep * copy: its arrays are separated from the original arrays of the * instance */ public DormandPrince54StepInterpolator(final DormandPrince54StepInterpolator interpolator) { super(interpolator); if (interpolator.v1 == null) { v1 = null; v2 = null; v3 = null; v4 = null; vectorsInitialized = false; } else { v1 = interpolator.v1.clone(); v2 = interpolator.v2.clone(); v3 = interpolator.v3.clone(); v4 = interpolator.v4.clone(); vectorsInitialized = interpolator.vectorsInitialized; } } /** {@inheritDoc} */ @Override protected StepInterpolator doCopy() { return new DormandPrince54StepInterpolator(this); } /** {@inheritDoc} */ @Override public void reinitialize(final AbstractIntegrator integrator, final double[] y, final double[][] yDotK, final boolean forward, final EquationsMapper primaryMapper, final EquationsMapper[] secondaryMappers) { super.reinitialize(integrator, y, yDotK, forward, primaryMapper, secondaryMappers); v1 = null; v2 = null; v3 = null; v4 = null; vectorsInitialized = false; } /** {@inheritDoc} */ @Override public void storeTime(final double t) { super.storeTime(t); vectorsInitialized = false; } /** {@inheritDoc} */ @Override protected void computeInterpolatedStateAndDerivatives(final double theta, final double oneMinusThetaH) { if (! vectorsInitialized) { if (v1 == null) { v1 = new double[interpolatedState.length]; v2 = new double[interpolatedState.length]; v3 = new double[interpolatedState.length]; v4 = new double[interpolatedState.length]; } // no step finalization is needed for this interpolator // we need to compute the interpolation vectors for this time step for (int i = 0; i < interpolatedState.length; ++i) { final double yDot0 = yDotK[0][i]; final double yDot2 = yDotK[2][i]; final double yDot3 = yDotK[3][i]; final double yDot4 = yDotK[4][i]; final double yDot5 = yDotK[5][i]; final double yDot6 = yDotK[6][i]; v1[i] = A70 * yDot0 + A72 * yDot2 + A73 * yDot3 + A74 * yDot4 + A75 * yDot5; v2[i] = yDot0 - v1[i]; v3[i] = v1[i] - v2[i] - yDot6; v4[i] = D0 * yDot0 + D2 * yDot2 + D3 * yDot3 + D4 * yDot4 + D5 * yDot5 + D6 * yDot6; } vectorsInitialized = true; } // interpolate final double eta = 1 - theta; final double twoTheta = 2 * theta; final double dot2 = 1 - twoTheta; final double dot3 = theta * (2 - 3 * theta); final double dot4 = twoTheta * (1 + theta * (twoTheta - 3)); if ((previousState != null) && (theta <= 0.5)) { for (int i = 0; i < interpolatedState.length; ++i) { interpolatedState[i] = previousState[i] + theta * h * (v1[i] + eta * (v2[i] + theta * (v3[i] + eta * v4[i]))); interpolatedDerivatives[i] = v1[i] + dot2 * v2[i] + dot3 * v3[i] + dot4 * v4[i]; } } else { for (int i = 0; i < interpolatedState.length; ++i) { interpolatedState[i] = currentState[i] - oneMinusThetaH * (v1[i] - theta * (v2[i] + theta * (v3[i] + eta * v4[i]))); interpolatedDerivatives[i] = v1[i] + dot2 * v2[i] + dot3 * v3[i] + dot4 * v4[i]; } } } } ././@LongLink100644 0 0 146 12126630646 10262 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/ode/nonstiff/HighamHall54Integrator.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/ode/nonstiff/HighamHall54Integrator.jav100644 1750 1750 12176 12126627715 32061 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.ode.nonstiff; import org.apache.commons.math3.util.FastMath; /** * This class implements the 5(4) Higham and Hall integrator for * Ordinary Differential Equations. * *

                            This integrator is an embedded Runge-Kutta integrator * of order 5(4) used in local extrapolation mode (i.e. the solution * is computed using the high order formula) with stepsize control * (and automatic step initialization) and continuous output. This * method uses 7 functions evaluations per step.

                            * * @version $Id: HighamHall54Integrator.java 1416643 2012-12-03 19:37:14Z tn $ * @since 1.2 */ public class HighamHall54Integrator extends EmbeddedRungeKuttaIntegrator { /** Integrator method name. */ private static final String METHOD_NAME = "Higham-Hall 5(4)"; /** Time steps Butcher array. */ private static final double[] STATIC_C = { 2.0/9.0, 1.0/3.0, 1.0/2.0, 3.0/5.0, 1.0, 1.0 }; /** Internal weights Butcher array. */ private static final double[][] STATIC_A = { {2.0/9.0}, {1.0/12.0, 1.0/4.0}, {1.0/8.0, 0.0, 3.0/8.0}, {91.0/500.0, -27.0/100.0, 78.0/125.0, 8.0/125.0}, {-11.0/20.0, 27.0/20.0, 12.0/5.0, -36.0/5.0, 5.0}, {1.0/12.0, 0.0, 27.0/32.0, -4.0/3.0, 125.0/96.0, 5.0/48.0} }; /** Propagation weights Butcher array. */ private static final double[] STATIC_B = { 1.0/12.0, 0.0, 27.0/32.0, -4.0/3.0, 125.0/96.0, 5.0/48.0, 0.0 }; /** Error weights Butcher array. */ private static final double[] STATIC_E = { -1.0/20.0, 0.0, 81.0/160.0, -6.0/5.0, 25.0/32.0, 1.0/16.0, -1.0/10.0 }; /** Simple constructor. * Build a fifth order Higham and Hall integrator with the given step bounds * @param minStep minimal step (sign is irrelevant, regardless of * integration direction, forward or backward), the last step can * be smaller than this * @param maxStep maximal step (sign is irrelevant, regardless of * integration direction, forward or backward), the last step can * be smaller than this * @param scalAbsoluteTolerance allowed absolute error * @param scalRelativeTolerance allowed relative error */ public HighamHall54Integrator(final double minStep, final double maxStep, final double scalAbsoluteTolerance, final double scalRelativeTolerance) { super(METHOD_NAME, false, STATIC_C, STATIC_A, STATIC_B, new HighamHall54StepInterpolator(), minStep, maxStep, scalAbsoluteTolerance, scalRelativeTolerance); } /** Simple constructor. * Build a fifth order Higham and Hall integrator with the given step bounds * @param minStep minimal step (sign is irrelevant, regardless of * integration direction, forward or backward), the last step can * be smaller than this * @param maxStep maximal step (sign is irrelevant, regardless of * integration direction, forward or backward), the last step can * be smaller than this * @param vecAbsoluteTolerance allowed absolute error * @param vecRelativeTolerance allowed relative error */ public HighamHall54Integrator(final double minStep, final double maxStep, final double[] vecAbsoluteTolerance, final double[] vecRelativeTolerance) { super(METHOD_NAME, false, STATIC_C, STATIC_A, STATIC_B, new HighamHall54StepInterpolator(), minStep, maxStep, vecAbsoluteTolerance, vecRelativeTolerance); } /** {@inheritDoc} */ @Override public int getOrder() { return 5; } /** {@inheritDoc} */ @Override protected double estimateError(final double[][] yDotK, final double[] y0, final double[] y1, final double h) { double error = 0; for (int j = 0; j < mainSetDimension; ++j) { double errSum = STATIC_E[0] * yDotK[0][j]; for (int l = 1; l < STATIC_E.length; ++l) { errSum += STATIC_E[l] * yDotK[l][j]; } final double yScale = FastMath.max(FastMath.abs(y0[j]), FastMath.abs(y1[j])); final double tol = (vecAbsoluteTolerance == null) ? (scalAbsoluteTolerance + scalRelativeTolerance * yScale) : (vecAbsoluteTolerance[j] + vecRelativeTolerance[j] * yScale); final double ratio = h * errSum / tol; error += ratio * ratio; } return FastMath.sqrt(error / mainSetDimension); } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/ode/nonstiff/AdamsIntegrator.java100644 1750 1750 15303 12126627715 31073 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.ode.nonstiff; import org.apache.commons.math3.exception.DimensionMismatchException; import org.apache.commons.math3.exception.MaxCountExceededException; import org.apache.commons.math3.exception.NoBracketingException; import org.apache.commons.math3.exception.NumberIsTooSmallException; import org.apache.commons.math3.linear.Array2DRowRealMatrix; import org.apache.commons.math3.ode.ExpandableStatefulODE; import org.apache.commons.math3.ode.MultistepIntegrator; /** Base class for {@link AdamsBashforthIntegrator Adams-Bashforth} and * {@link AdamsMoultonIntegrator Adams-Moulton} integrators. * @version $Id: AdamsIntegrator.java 1416643 2012-12-03 19:37:14Z tn $ * @since 2.0 */ public abstract class AdamsIntegrator extends MultistepIntegrator { /** Transformer. */ private final AdamsNordsieckTransformer transformer; /** * Build an Adams integrator with the given order and step control parameters. * @param name name of the method * @param nSteps number of steps of the method excluding the one being computed * @param order order of the method * @param minStep minimal step (sign is irrelevant, regardless of * integration direction, forward or backward), the last step can * be smaller than this * @param maxStep maximal step (sign is irrelevant, regardless of * integration direction, forward or backward), the last step can * be smaller than this * @param scalAbsoluteTolerance allowed absolute error * @param scalRelativeTolerance allowed relative error * @exception NumberIsTooSmallException if order is 1 or less */ public AdamsIntegrator(final String name, final int nSteps, final int order, final double minStep, final double maxStep, final double scalAbsoluteTolerance, final double scalRelativeTolerance) throws NumberIsTooSmallException { super(name, nSteps, order, minStep, maxStep, scalAbsoluteTolerance, scalRelativeTolerance); transformer = AdamsNordsieckTransformer.getInstance(nSteps); } /** * Build an Adams integrator with the given order and step control parameters. * @param name name of the method * @param nSteps number of steps of the method excluding the one being computed * @param order order of the method * @param minStep minimal step (sign is irrelevant, regardless of * integration direction, forward or backward), the last step can * be smaller than this * @param maxStep maximal step (sign is irrelevant, regardless of * integration direction, forward or backward), the last step can * be smaller than this * @param vecAbsoluteTolerance allowed absolute error * @param vecRelativeTolerance allowed relative error * @exception IllegalArgumentException if order is 1 or less */ public AdamsIntegrator(final String name, final int nSteps, final int order, final double minStep, final double maxStep, final double[] vecAbsoluteTolerance, final double[] vecRelativeTolerance) throws IllegalArgumentException { super(name, nSteps, order, minStep, maxStep, vecAbsoluteTolerance, vecRelativeTolerance); transformer = AdamsNordsieckTransformer.getInstance(nSteps); } /** {@inheritDoc} */ @Override public abstract void integrate(final ExpandableStatefulODE equations, final double t) throws NumberIsTooSmallException, DimensionMismatchException, MaxCountExceededException, NoBracketingException; /** {@inheritDoc} */ @Override protected Array2DRowRealMatrix initializeHighOrderDerivatives(final double h, final double[] t, final double[][] y, final double[][] yDot) { return transformer.initializeHighOrderDerivatives(h, t, y, yDot); } /** Update the high order scaled derivatives for Adams integrators (phase 1). *

                            The complete update of high order derivatives has a form similar to: *

                                 * rn+1 = (s1(n) - s1(n+1)) P-1 u + P-1 A P rn
                                 * 
                            * this method computes the P-1 A P rn part.

                            * @param highOrder high order scaled derivatives * (h2/2 y'', ... hk/k! y(k)) * @return updated high order derivatives * @see #updateHighOrderDerivativesPhase2(double[], double[], Array2DRowRealMatrix) */ public Array2DRowRealMatrix updateHighOrderDerivativesPhase1(final Array2DRowRealMatrix highOrder) { return transformer.updateHighOrderDerivativesPhase1(highOrder); } /** Update the high order scaled derivatives Adams integrators (phase 2). *

                            The complete update of high order derivatives has a form similar to: *

                                 * rn+1 = (s1(n) - s1(n+1)) P-1 u + P-1 A P rn
                                 * 
                            * this method computes the (s1(n) - s1(n+1)) P-1 u part.

                            *

                            Phase 1 of the update must already have been performed.

                            * @param start first order scaled derivatives at step start * @param end first order scaled derivatives at step end * @param highOrder high order scaled derivatives, will be modified * (h2/2 y'', ... hk/k! y(k)) * @see #updateHighOrderDerivativesPhase1(Array2DRowRealMatrix) */ public void updateHighOrderDerivativesPhase2(final double[] start, final double[] end, final Array2DRowRealMatrix highOrder) { transformer.updateHighOrderDerivativesPhase2(start, end, highOrder); } } ././@LongLink100644 0 0 152 12126630646 10257 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/ode/nonstiff/DormandPrince853Integrator.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/ode/nonstiff/DormandPrince853Integrator100644 1750 1750 27322 12126627715 32117 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.ode.nonstiff; import org.apache.commons.math3.util.FastMath; /** * This class implements the 8(5,3) Dormand-Prince integrator for Ordinary * Differential Equations. * *

                            This integrator is an embedded Runge-Kutta integrator * of order 8(5,3) used in local extrapolation mode (i.e. the solution * is computed using the high order formula) with stepsize control * (and automatic step initialization) and continuous output. This * method uses 12 functions evaluations per step for integration and 4 * evaluations for interpolation. However, since the first * interpolation evaluation is the same as the first integration * evaluation of the next step, we have included it in the integrator * rather than in the interpolator and specified the method was an * fsal. Hence, despite we have 13 stages here, the cost is * really 12 evaluations per step even if no interpolation is done, * and the overcost of interpolation is only 3 evaluations.

                            * *

                            This method is based on an 8(6) method by Dormand and Prince * (i.e. order 8 for the integration and order 6 for error estimation) * modified by Hairer and Wanner to use a 5th order error estimator * with 3rd order correction. This modification was introduced because * the original method failed in some cases (wrong steps can be * accepted when step size is too large, for example in the * Brusselator problem) and also had severe difficulties when * applied to problems with discontinuities. This modification is * explained in the second edition of the first volume (Nonstiff * Problems) of the reference book by Hairer, Norsett and Wanner: * Solving Ordinary Differential Equations (Springer-Verlag, * ISBN 3-540-56670-8).

                            * * @version $Id: DormandPrince853Integrator.java 1416643 2012-12-03 19:37:14Z tn $ * @since 1.2 */ public class DormandPrince853Integrator extends EmbeddedRungeKuttaIntegrator { /** Integrator method name. */ private static final String METHOD_NAME = "Dormand-Prince 8 (5, 3)"; /** Time steps Butcher array. */ private static final double[] STATIC_C = { (12.0 - 2.0 * FastMath.sqrt(6.0)) / 135.0, (6.0 - FastMath.sqrt(6.0)) / 45.0, (6.0 - FastMath.sqrt(6.0)) / 30.0, (6.0 + FastMath.sqrt(6.0)) / 30.0, 1.0/3.0, 1.0/4.0, 4.0/13.0, 127.0/195.0, 3.0/5.0, 6.0/7.0, 1.0, 1.0 }; /** Internal weights Butcher array. */ private static final double[][] STATIC_A = { // k2 {(12.0 - 2.0 * FastMath.sqrt(6.0)) / 135.0}, // k3 {(6.0 - FastMath.sqrt(6.0)) / 180.0, (6.0 - FastMath.sqrt(6.0)) / 60.0}, // k4 {(6.0 - FastMath.sqrt(6.0)) / 120.0, 0.0, (6.0 - FastMath.sqrt(6.0)) / 40.0}, // k5 {(462.0 + 107.0 * FastMath.sqrt(6.0)) / 3000.0, 0.0, (-402.0 - 197.0 * FastMath.sqrt(6.0)) / 1000.0, (168.0 + 73.0 * FastMath.sqrt(6.0)) / 375.0}, // k6 {1.0 / 27.0, 0.0, 0.0, (16.0 + FastMath.sqrt(6.0)) / 108.0, (16.0 - FastMath.sqrt(6.0)) / 108.0}, // k7 {19.0 / 512.0, 0.0, 0.0, (118.0 + 23.0 * FastMath.sqrt(6.0)) / 1024.0, (118.0 - 23.0 * FastMath.sqrt(6.0)) / 1024.0, -9.0 / 512.0}, // k8 {13772.0 / 371293.0, 0.0, 0.0, (51544.0 + 4784.0 * FastMath.sqrt(6.0)) / 371293.0, (51544.0 - 4784.0 * FastMath.sqrt(6.0)) / 371293.0, -5688.0 / 371293.0, 3072.0 / 371293.0}, // k9 {58656157643.0 / 93983540625.0, 0.0, 0.0, (-1324889724104.0 - 318801444819.0 * FastMath.sqrt(6.0)) / 626556937500.0, (-1324889724104.0 + 318801444819.0 * FastMath.sqrt(6.0)) / 626556937500.0, 96044563816.0 / 3480871875.0, 5682451879168.0 / 281950621875.0, -165125654.0 / 3796875.0}, // k10 {8909899.0 / 18653125.0, 0.0, 0.0, (-4521408.0 - 1137963.0 * FastMath.sqrt(6.0)) / 2937500.0, (-4521408.0 + 1137963.0 * FastMath.sqrt(6.0)) / 2937500.0, 96663078.0 / 4553125.0, 2107245056.0 / 137915625.0, -4913652016.0 / 147609375.0, -78894270.0 / 3880452869.0}, // k11 {-20401265806.0 / 21769653311.0, 0.0, 0.0, (354216.0 + 94326.0 * FastMath.sqrt(6.0)) / 112847.0, (354216.0 - 94326.0 * FastMath.sqrt(6.0)) / 112847.0, -43306765128.0 / 5313852383.0, -20866708358144.0 / 1126708119789.0, 14886003438020.0 / 654632330667.0, 35290686222309375.0 / 14152473387134411.0, -1477884375.0 / 485066827.0}, // k12 {39815761.0 / 17514443.0, 0.0, 0.0, (-3457480.0 - 960905.0 * FastMath.sqrt(6.0)) / 551636.0, (-3457480.0 + 960905.0 * FastMath.sqrt(6.0)) / 551636.0, -844554132.0 / 47026969.0, 8444996352.0 / 302158619.0, -2509602342.0 / 877790785.0, -28388795297996250.0 / 3199510091356783.0, 226716250.0 / 18341897.0, 1371316744.0 / 2131383595.0}, // k13 should be for interpolation only, but since it is the same // stage as the first evaluation of the next step, we perform it // here at no cost by specifying this is an fsal method {104257.0/1920240.0, 0.0, 0.0, 0.0, 0.0, 3399327.0/763840.0, 66578432.0/35198415.0, -1674902723.0/288716400.0, 54980371265625.0/176692375811392.0, -734375.0/4826304.0, 171414593.0/851261400.0, 137909.0/3084480.0} }; /** Propagation weights Butcher array. */ private static final double[] STATIC_B = { 104257.0/1920240.0, 0.0, 0.0, 0.0, 0.0, 3399327.0/763840.0, 66578432.0/35198415.0, -1674902723.0/288716400.0, 54980371265625.0/176692375811392.0, -734375.0/4826304.0, 171414593.0/851261400.0, 137909.0/3084480.0, 0.0 }; /** First error weights array, element 1. */ private static final double E1_01 = 116092271.0 / 8848465920.0; // elements 2 to 5 are zero, so they are neither stored nor used /** First error weights array, element 6. */ private static final double E1_06 = -1871647.0 / 1527680.0; /** First error weights array, element 7. */ private static final double E1_07 = -69799717.0 / 140793660.0; /** First error weights array, element 8. */ private static final double E1_08 = 1230164450203.0 / 739113984000.0; /** First error weights array, element 9. */ private static final double E1_09 = -1980813971228885.0 / 5654156025964544.0; /** First error weights array, element 10. */ private static final double E1_10 = 464500805.0 / 1389975552.0; /** First error weights array, element 11. */ private static final double E1_11 = 1606764981773.0 / 19613062656000.0; /** First error weights array, element 12. */ private static final double E1_12 = -137909.0 / 6168960.0; /** Second error weights array, element 1. */ private static final double E2_01 = -364463.0 / 1920240.0; // elements 2 to 5 are zero, so they are neither stored nor used /** Second error weights array, element 6. */ private static final double E2_06 = 3399327.0 / 763840.0; /** Second error weights array, element 7. */ private static final double E2_07 = 66578432.0 / 35198415.0; /** Second error weights array, element 8. */ private static final double E2_08 = -1674902723.0 / 288716400.0; /** Second error weights array, element 9. */ private static final double E2_09 = -74684743568175.0 / 176692375811392.0; /** Second error weights array, element 10. */ private static final double E2_10 = -734375.0 / 4826304.0; /** Second error weights array, element 11. */ private static final double E2_11 = 171414593.0 / 851261400.0; /** Second error weights array, element 12. */ private static final double E2_12 = 69869.0 / 3084480.0; /** Simple constructor. * Build an eighth order Dormand-Prince integrator with the given step bounds * @param minStep minimal step (sign is irrelevant, regardless of * integration direction, forward or backward), the last step can * be smaller than this * @param maxStep maximal step (sign is irrelevant, regardless of * integration direction, forward or backward), the last step can * be smaller than this * @param scalAbsoluteTolerance allowed absolute error * @param scalRelativeTolerance allowed relative error */ public DormandPrince853Integrator(final double minStep, final double maxStep, final double scalAbsoluteTolerance, final double scalRelativeTolerance) { super(METHOD_NAME, true, STATIC_C, STATIC_A, STATIC_B, new DormandPrince853StepInterpolator(), minStep, maxStep, scalAbsoluteTolerance, scalRelativeTolerance); } /** Simple constructor. * Build an eighth order Dormand-Prince integrator with the given step bounds * @param minStep minimal step (sign is irrelevant, regardless of * integration direction, forward or backward), the last step can * be smaller than this * @param maxStep maximal step (sign is irrelevant, regardless of * integration direction, forward or backward), the last step can * be smaller than this * @param vecAbsoluteTolerance allowed absolute error * @param vecRelativeTolerance allowed relative error */ public DormandPrince853Integrator(final double minStep, final double maxStep, final double[] vecAbsoluteTolerance, final double[] vecRelativeTolerance) { super(METHOD_NAME, true, STATIC_C, STATIC_A, STATIC_B, new DormandPrince853StepInterpolator(), minStep, maxStep, vecAbsoluteTolerance, vecRelativeTolerance); } /** {@inheritDoc} */ @Override public int getOrder() { return 8; } /** {@inheritDoc} */ @Override protected double estimateError(final double[][] yDotK, final double[] y0, final double[] y1, final double h) { double error1 = 0; double error2 = 0; for (int j = 0; j < mainSetDimension; ++j) { final double errSum1 = E1_01 * yDotK[0][j] + E1_06 * yDotK[5][j] + E1_07 * yDotK[6][j] + E1_08 * yDotK[7][j] + E1_09 * yDotK[8][j] + E1_10 * yDotK[9][j] + E1_11 * yDotK[10][j] + E1_12 * yDotK[11][j]; final double errSum2 = E2_01 * yDotK[0][j] + E2_06 * yDotK[5][j] + E2_07 * yDotK[6][j] + E2_08 * yDotK[7][j] + E2_09 * yDotK[8][j] + E2_10 * yDotK[9][j] + E2_11 * yDotK[10][j] + E2_12 * yDotK[11][j]; final double yScale = FastMath.max(FastMath.abs(y0[j]), FastMath.abs(y1[j])); final double tol = (vecAbsoluteTolerance == null) ? (scalAbsoluteTolerance + scalRelativeTolerance * yScale) : (vecAbsoluteTolerance[j] + vecRelativeTolerance[j] * yScale); final double ratio1 = errSum1 / tol; error1 += ratio1 * ratio1; final double ratio2 = errSum2 / tol; error2 += ratio2 * ratio2; } double den = error1 + 0.01 * error2; if (den <= 0.0) { den = 1.0; } return FastMath.abs(h) * error1 / FastMath.sqrt(mainSetDimension * den); } } ././@LongLink100644 0 0 150 12126630646 10255 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/ode/nonstiff/AdamsBashforthIntegrator.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/ode/nonstiff/AdamsBashforthIntegrator.j100644 1750 1750 35066 12126627715 32254 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.ode.nonstiff; import org.apache.commons.math3.exception.DimensionMismatchException; import org.apache.commons.math3.exception.MaxCountExceededException; import org.apache.commons.math3.exception.NoBracketingException; import org.apache.commons.math3.exception.NumberIsTooSmallException; import org.apache.commons.math3.linear.Array2DRowRealMatrix; import org.apache.commons.math3.ode.EquationsMapper; import org.apache.commons.math3.ode.ExpandableStatefulODE; import org.apache.commons.math3.ode.sampling.NordsieckStepInterpolator; import org.apache.commons.math3.util.FastMath; /** * This class implements explicit Adams-Bashforth integrators for Ordinary * Differential Equations. * *

                            Adams-Bashforth methods (in fact due to Adams alone) are explicit * multistep ODE solvers. This implementation is a variation of the classical * one: it uses adaptive stepsize to implement error control, whereas * classical implementations are fixed step size. The value of state vector * at step n+1 is a simple combination of the value at step n and of the * derivatives at steps n, n-1, n-2 ... Depending on the number k of previous * steps one wants to use for computing the next value, different formulas * are available:

                            *
                              *
                            • k = 1: yn+1 = yn + h y'n
                            • *
                            • k = 2: yn+1 = yn + h (3y'n-y'n-1)/2
                            • *
                            • k = 3: yn+1 = yn + h (23y'n-16y'n-1+5y'n-2)/12
                            • *
                            • k = 4: yn+1 = yn + h (55y'n-59y'n-1+37y'n-2-9y'n-3)/24
                            • *
                            • ...
                            • *
                            * *

                            A k-steps Adams-Bashforth method is of order k.

                            * *

                            Implementation details

                            * *

                            We define scaled derivatives si(n) at step n as: *

                             * s1(n) = h y'n for first derivative
                             * s2(n) = h2/2 y''n for second derivative
                             * s3(n) = h3/6 y'''n for third derivative
                             * ...
                             * sk(n) = hk/k! y(k)n for kth derivative
                             * 

                            * *

                            The definitions above use the classical representation with several previous first * derivatives. Lets define *

                             *   qn = [ s1(n-1) s1(n-2) ... s1(n-(k-1)) ]T
                             * 
                            * (we omit the k index in the notation for clarity). With these definitions, * Adams-Bashforth methods can be written: *
                              *
                            • k = 1: yn+1 = yn + s1(n)
                            • *
                            • k = 2: yn+1 = yn + 3/2 s1(n) + [ -1/2 ] qn
                            • *
                            • k = 3: yn+1 = yn + 23/12 s1(n) + [ -16/12 5/12 ] qn
                            • *
                            • k = 4: yn+1 = yn + 55/24 s1(n) + [ -59/24 37/24 -9/24 ] qn
                            • *
                            • ...
                            • *

                            * *

                            Instead of using the classical representation with first derivatives only (yn, * s1(n) and qn), our implementation uses the Nordsieck vector with * higher degrees scaled derivatives all taken at the same step (yn, s1(n) * and rn) where rn is defined as: *

                             * rn = [ s2(n), s3(n) ... sk(n) ]T
                             * 
                            * (here again we omit the k index in the notation for clarity) *

                            * *

                            Taylor series formulas show that for any index offset i, s1(n-i) can be * computed from s1(n), s2(n) ... sk(n), the formula being exact * for degree k polynomials. *

                             * s1(n-i) = s1(n) + ∑j j (-i)j-1 sj(n)
                             * 
                            * The previous formula can be used with several values for i to compute the transform between * classical representation and Nordsieck vector. The transform between rn * and qn resulting from the Taylor series formulas above is: *
                             * qn = s1(n) u + P rn
                             * 
                            * where u is the [ 1 1 ... 1 ]T vector and P is the (k-1)×(k-1) matrix built * with the j (-i)j-1 terms: *
                             *        [  -2   3   -4    5  ... ]
                             *        [  -4  12  -32   80  ... ]
                             *   P =  [  -6  27 -108  405  ... ]
                             *        [  -8  48 -256 1280  ... ]
                             *        [          ...           ]
                             * 

                            * *

                            Using the Nordsieck vector has several advantages: *

                              *
                            • it greatly simplifies step interpolation as the interpolator mainly applies * Taylor series formulas,
                            • *
                            • it simplifies step changes that occur when discrete events that truncate * the step are triggered,
                            • *
                            • it allows to extend the methods in order to support adaptive stepsize.
                            • *

                            * *

                            The Nordsieck vector at step n+1 is computed from the Nordsieck vector at step n as follows: *

                              *
                            • yn+1 = yn + s1(n) + uT rn
                            • *
                            • s1(n+1) = h f(tn+1, yn+1)
                            • *
                            • rn+1 = (s1(n) - s1(n+1)) P-1 u + P-1 A P rn
                            • *
                            * where A is a rows shifting matrix (the lower left part is an identity matrix): *
                             *        [ 0 0   ...  0 0 | 0 ]
                             *        [ ---------------+---]
                             *        [ 1 0   ...  0 0 | 0 ]
                             *    A = [ 0 1   ...  0 0 | 0 ]
                             *        [       ...      | 0 ]
                             *        [ 0 0   ...  1 0 | 0 ]
                             *        [ 0 0   ...  0 1 | 0 ]
                             * 

                            * *

                            The P-1u vector and the P-1 A P matrix do not depend on the state, * they only depend on k and therefore are precomputed once for all.

                            * * @version $Id: AdamsBashforthIntegrator.java 1463684 2013-04-02 19:04:13Z luc $ * @since 2.0 */ public class AdamsBashforthIntegrator extends AdamsIntegrator { /** Integrator method name. */ private static final String METHOD_NAME = "Adams-Bashforth"; /** * Build an Adams-Bashforth integrator with the given order and step control parameters. * @param nSteps number of steps of the method excluding the one being computed * @param minStep minimal step (sign is irrelevant, regardless of * integration direction, forward or backward), the last step can * be smaller than this * @param maxStep maximal step (sign is irrelevant, regardless of * integration direction, forward or backward), the last step can * be smaller than this * @param scalAbsoluteTolerance allowed absolute error * @param scalRelativeTolerance allowed relative error * @exception NumberIsTooSmallException if order is 1 or less */ public AdamsBashforthIntegrator(final int nSteps, final double minStep, final double maxStep, final double scalAbsoluteTolerance, final double scalRelativeTolerance) throws NumberIsTooSmallException { super(METHOD_NAME, nSteps, nSteps, minStep, maxStep, scalAbsoluteTolerance, scalRelativeTolerance); } /** * Build an Adams-Bashforth integrator with the given order and step control parameters. * @param nSteps number of steps of the method excluding the one being computed * @param minStep minimal step (sign is irrelevant, regardless of * integration direction, forward or backward), the last step can * be smaller than this * @param maxStep maximal step (sign is irrelevant, regardless of * integration direction, forward or backward), the last step can * be smaller than this * @param vecAbsoluteTolerance allowed absolute error * @param vecRelativeTolerance allowed relative error * @exception IllegalArgumentException if order is 1 or less */ public AdamsBashforthIntegrator(final int nSteps, final double minStep, final double maxStep, final double[] vecAbsoluteTolerance, final double[] vecRelativeTolerance) throws IllegalArgumentException { super(METHOD_NAME, nSteps, nSteps, minStep, maxStep, vecAbsoluteTolerance, vecRelativeTolerance); } /** {@inheritDoc} */ @Override public void integrate(final ExpandableStatefulODE equations, final double t) throws NumberIsTooSmallException, DimensionMismatchException, MaxCountExceededException, NoBracketingException { sanityChecks(equations, t); setEquations(equations); final boolean forward = t > equations.getTime(); // initialize working arrays final double[] y0 = equations.getCompleteState(); final double[] y = y0.clone(); final double[] yDot = new double[y.length]; // set up an interpolator sharing the integrator arrays final NordsieckStepInterpolator interpolator = new NordsieckStepInterpolator(); interpolator.reinitialize(y, forward, equations.getPrimaryMapper(), equations.getSecondaryMappers()); // set up integration control objects initIntegration(equations.getTime(), y0, t); // compute the initial Nordsieck vector using the configured starter integrator start(equations.getTime(), y, t); interpolator.reinitialize(stepStart, stepSize, scaled, nordsieck); interpolator.storeTime(stepStart); final int lastRow = nordsieck.getRowDimension() - 1; // reuse the step that was chosen by the starter integrator double hNew = stepSize; interpolator.rescale(hNew); // main integration loop isLastStep = false; do { double error = 10; while (error >= 1.0) { stepSize = hNew; // evaluate error using the last term of the Taylor expansion error = 0; for (int i = 0; i < mainSetDimension; ++i) { final double yScale = FastMath.abs(y[i]); final double tol = (vecAbsoluteTolerance == null) ? (scalAbsoluteTolerance + scalRelativeTolerance * yScale) : (vecAbsoluteTolerance[i] + vecRelativeTolerance[i] * yScale); final double ratio = nordsieck.getEntry(lastRow, i) / tol; error += ratio * ratio; } error = FastMath.sqrt(error / mainSetDimension); if (error >= 1.0) { // reject the step and attempt to reduce error by stepsize control final double factor = computeStepGrowShrinkFactor(error); hNew = filterStep(stepSize * factor, forward, false); interpolator.rescale(hNew); } } // predict a first estimate of the state at step end final double stepEnd = stepStart + stepSize; interpolator.shift(); interpolator.setInterpolatedTime(stepEnd); final ExpandableStatefulODE expandable = getExpandable(); final EquationsMapper primary = expandable.getPrimaryMapper(); primary.insertEquationData(interpolator.getInterpolatedState(), y); int index = 0; for (final EquationsMapper secondary : expandable.getSecondaryMappers()) { secondary.insertEquationData(interpolator.getInterpolatedSecondaryState(index), y); ++index; } // evaluate the derivative computeDerivatives(stepEnd, y, yDot); // update Nordsieck vector final double[] predictedScaled = new double[y0.length]; for (int j = 0; j < y0.length; ++j) { predictedScaled[j] = stepSize * yDot[j]; } final Array2DRowRealMatrix nordsieckTmp = updateHighOrderDerivativesPhase1(nordsieck); updateHighOrderDerivativesPhase2(scaled, predictedScaled, nordsieckTmp); interpolator.reinitialize(stepEnd, stepSize, predictedScaled, nordsieckTmp); // discrete events handling interpolator.storeTime(stepEnd); stepStart = acceptStep(interpolator, y, yDot, t); scaled = predictedScaled; nordsieck = nordsieckTmp; interpolator.reinitialize(stepEnd, stepSize, scaled, nordsieck); if (!isLastStep) { // prepare next step interpolator.storeTime(stepStart); if (resetOccurred) { // some events handler has triggered changes that // invalidate the derivatives, we need to restart from scratch start(stepStart, y, t); interpolator.reinitialize(stepStart, stepSize, scaled, nordsieck); } // stepsize control for next step final double factor = computeStepGrowShrinkFactor(error); final double scaledH = stepSize * factor; final double nextT = stepStart + scaledH; final boolean nextIsLast = forward ? (nextT >= t) : (nextT <= t); hNew = filterStep(scaledH, forward, nextIsLast); final double filteredNextT = stepStart + hNew; final boolean filteredNextIsLast = forward ? (filteredNextT >= t) : (filteredNextT <= t); if (filteredNextIsLast) { hNew = t - stepStart; } interpolator.rescale(hNew); } } while (!isLastStep); // dispatch results equations.setTime(stepStart); equations.setCompleteState(y); resetInternalState(); } } ././@LongLink100644 0 0 152 12126630646 10257 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/ode/nonstiff/RungeKuttaStepInterpolator.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/ode/nonstiff/RungeKuttaStepInterpolator100644 1750 1750 16117 12126627715 32423 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.ode.nonstiff; import java.io.IOException; import java.io.ObjectInput; import java.io.ObjectOutput; import org.apache.commons.math3.ode.AbstractIntegrator; import org.apache.commons.math3.ode.EquationsMapper; import org.apache.commons.math3.ode.sampling.AbstractStepInterpolator; /** This class represents an interpolator over the last step during an * ODE integration for Runge-Kutta and embedded Runge-Kutta integrators. * * @see RungeKuttaIntegrator * @see EmbeddedRungeKuttaIntegrator * * @version $Id: RungeKuttaStepInterpolator.java 1416643 2012-12-03 19:37:14Z tn $ * @since 1.2 */ abstract class RungeKuttaStepInterpolator extends AbstractStepInterpolator { /** Previous state. */ protected double[] previousState; /** Slopes at the intermediate points */ protected double[][] yDotK; /** Reference to the integrator. */ protected AbstractIntegrator integrator; /** Simple constructor. * This constructor builds an instance that is not usable yet, the * {@link #reinitialize} method should be called before using the * instance in order to initialize the internal arrays. This * constructor is used only in order to delay the initialization in * some cases. The {@link RungeKuttaIntegrator} and {@link * EmbeddedRungeKuttaIntegrator} classes use the prototyping design * pattern to create the step interpolators by cloning an * uninitialized model and latter initializing the copy. */ protected RungeKuttaStepInterpolator() { previousState = null; yDotK = null; integrator = null; } /** Copy constructor. *

                            The copied interpolator should have been finalized before the * copy, otherwise the copy will not be able to perform correctly any * interpolation and will throw a {@link NullPointerException} * later. Since we don't want this constructor to throw the * exceptions finalization may involve and since we don't want this * method to modify the state of the copied interpolator, * finalization is not done automatically, it * remains under user control.

                            *

                            The copy is a deep copy: its arrays are separated from the * original arrays of the instance.

                            * @param interpolator interpolator to copy from. */ public RungeKuttaStepInterpolator(final RungeKuttaStepInterpolator interpolator) { super(interpolator); if (interpolator.currentState != null) { previousState = interpolator.previousState.clone(); yDotK = new double[interpolator.yDotK.length][]; for (int k = 0; k < interpolator.yDotK.length; ++k) { yDotK[k] = interpolator.yDotK[k].clone(); } } else { previousState = null; yDotK = null; } // we cannot keep any reference to the equations in the copy // the interpolator should have been finalized before integrator = null; } /** Reinitialize the instance *

                            Some Runge-Kutta integrators need fewer functions evaluations * than their counterpart step interpolators. So the interpolator * should perform the last evaluations they need by themselves. The * {@link RungeKuttaIntegrator RungeKuttaIntegrator} and {@link * EmbeddedRungeKuttaIntegrator EmbeddedRungeKuttaIntegrator} * abstract classes call this method in order to let the step * interpolator perform the evaluations it needs. These evaluations * will be performed during the call to doFinalize if * any, i.e. only if the step handler either calls the {@link * AbstractStepInterpolator#finalizeStep finalizeStep} method or the * {@link AbstractStepInterpolator#getInterpolatedState * getInterpolatedState} method (for an interpolator which needs a * finalization) or if it clones the step interpolator.

                            * @param rkIntegrator integrator being used * @param y reference to the integrator array holding the state at * the end of the step * @param yDotArray reference to the integrator array holding all the * intermediate slopes * @param forward integration direction indicator * @param primaryMapper equations mapper for the primary equations set * @param secondaryMappers equations mappers for the secondary equations sets */ public void reinitialize(final AbstractIntegrator rkIntegrator, final double[] y, final double[][] yDotArray, final boolean forward, final EquationsMapper primaryMapper, final EquationsMapper[] secondaryMappers) { reinitialize(y, forward, primaryMapper, secondaryMappers); this.previousState = null; this.yDotK = yDotArray; this.integrator = rkIntegrator; } /** {@inheritDoc} */ @Override public void shift() { previousState = currentState.clone(); super.shift(); } /** {@inheritDoc} */ @Override public void writeExternal(final ObjectOutput out) throws IOException { // save the state of the base class writeBaseExternal(out); // save the local attributes final int n = (currentState == null) ? -1 : currentState.length; for (int i = 0; i < n; ++i) { out.writeDouble(previousState[i]); } final int kMax = (yDotK == null) ? -1 : yDotK.length; out.writeInt(kMax); for (int k = 0; k < kMax; ++k) { for (int i = 0; i < n; ++i) { out.writeDouble(yDotK[k][i]); } } // we do not save any reference to the equations } /** {@inheritDoc} */ @Override public void readExternal(final ObjectInput in) throws IOException, ClassNotFoundException { // read the base class final double t = readBaseExternal(in); // read the local attributes final int n = (currentState == null) ? -1 : currentState.length; if (n < 0) { previousState = null; } else { previousState = new double[n]; for (int i = 0; i < n; ++i) { previousState[i] = in.readDouble(); } } final int kMax = in.readInt(); yDotK = (kMax < 0) ? null : new double[kMax][]; for (int k = 0; k < kMax; ++k) { yDotK[k] = (n < 0) ? null : new double[n]; for (int i = 0; i < n; ++i) { yDotK[k][i] = in.readDouble(); } } integrator = null; if (currentState != null) { // we can now set the interpolated time and state setInterpolatedTime(t); } else { interpolatedTime = t; } } } ././@LongLink100644 0 0 154 12126630646 10261 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/ode/nonstiff/GraggBulirschStoerIntegrator.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/ode/nonstiff/GraggBulirschStoerIntegrat100644 1750 1750 104345 12126627715 32352 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.ode.nonstiff; import org.apache.commons.math3.analysis.solvers.UnivariateSolver; import org.apache.commons.math3.exception.DimensionMismatchException; import org.apache.commons.math3.exception.MaxCountExceededException; import org.apache.commons.math3.exception.NoBracketingException; import org.apache.commons.math3.exception.NumberIsTooSmallException; import org.apache.commons.math3.ode.ExpandableStatefulODE; import org.apache.commons.math3.ode.events.EventHandler; import org.apache.commons.math3.ode.sampling.AbstractStepInterpolator; import org.apache.commons.math3.ode.sampling.StepHandler; import org.apache.commons.math3.util.FastMath; /** * This class implements a Gragg-Bulirsch-Stoer integrator for * Ordinary Differential Equations. * *

                            The Gragg-Bulirsch-Stoer algorithm is one of the most efficient * ones currently available for smooth problems. It uses Richardson * extrapolation to estimate what would be the solution if the step * size could be decreased down to zero.

                            * *

                            * This method changes both the step size and the order during * integration, in order to minimize computation cost. It is * particularly well suited when a very high precision is needed. The * limit where this method becomes more efficient than high-order * embedded Runge-Kutta methods like {@link DormandPrince853Integrator * Dormand-Prince 8(5,3)} depends on the problem. Results given in the * Hairer, Norsett and Wanner book show for example that this limit * occurs for accuracy around 1e-6 when integrating Saltzam-Lorenz * equations (the authors note this problem is extremely sensitive * to the errors in the first integration steps), and around 1e-11 * for a two dimensional celestial mechanics problems with seven * bodies (pleiades problem, involving quasi-collisions for which * automatic step size control is essential). *

                            * *

                            * This implementation is basically a reimplementation in Java of the * odex * fortran code by E. Hairer and G. Wanner. The redistribution policy * for this code is available here, for * convenience, it is reproduced below.

                            *

                            * * * * * * * *
                            Copyright (c) 2004, Ernst Hairer
                            Redistribution and use in source and binary forms, with or * without modification, are permitted provided that the following * conditions are met: *
                              *
                            • Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer.
                            • *
                            • Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution.
                            • *
                            THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
                            * * @version $Id: GraggBulirschStoerIntegrator.java 1416643 2012-12-03 19:37:14Z tn $ * @since 1.2 */ public class GraggBulirschStoerIntegrator extends AdaptiveStepsizeIntegrator { /** Integrator method name. */ private static final String METHOD_NAME = "Gragg-Bulirsch-Stoer"; /** maximal order. */ private int maxOrder; /** step size sequence. */ private int[] sequence; /** overall cost of applying step reduction up to iteration k+1, in number of calls. */ private int[] costPerStep; /** cost per unit step. */ private double[] costPerTimeUnit; /** optimal steps for each order. */ private double[] optimalStep; /** extrapolation coefficients. */ private double[][] coeff; /** stability check enabling parameter. */ private boolean performTest; /** maximal number of checks for each iteration. */ private int maxChecks; /** maximal number of iterations for which checks are performed. */ private int maxIter; /** stepsize reduction factor in case of stability check failure. */ private double stabilityReduction; /** first stepsize control factor. */ private double stepControl1; /** second stepsize control factor. */ private double stepControl2; /** third stepsize control factor. */ private double stepControl3; /** fourth stepsize control factor. */ private double stepControl4; /** first order control factor. */ private double orderControl1; /** second order control factor. */ private double orderControl2; /** use interpolation error in stepsize control. */ private boolean useInterpolationError; /** interpolation order control parameter. */ private int mudif; /** Simple constructor. * Build a Gragg-Bulirsch-Stoer integrator with the given step * bounds. All tuning parameters are set to their default * values. The default step handler does nothing. * @param minStep minimal step (sign is irrelevant, regardless of * integration direction, forward or backward), the last step can * be smaller than this * @param maxStep maximal step (sign is irrelevant, regardless of * integration direction, forward or backward), the last step can * be smaller than this * @param scalAbsoluteTolerance allowed absolute error * @param scalRelativeTolerance allowed relative error */ public GraggBulirschStoerIntegrator(final double minStep, final double maxStep, final double scalAbsoluteTolerance, final double scalRelativeTolerance) { super(METHOD_NAME, minStep, maxStep, scalAbsoluteTolerance, scalRelativeTolerance); setStabilityCheck(true, -1, -1, -1); setControlFactors(-1, -1, -1, -1); setOrderControl(-1, -1, -1); setInterpolationControl(true, -1); } /** Simple constructor. * Build a Gragg-Bulirsch-Stoer integrator with the given step * bounds. All tuning parameters are set to their default * values. The default step handler does nothing. * @param minStep minimal step (must be positive even for backward * integration), the last step can be smaller than this * @param maxStep maximal step (must be positive even for backward * integration) * @param vecAbsoluteTolerance allowed absolute error * @param vecRelativeTolerance allowed relative error */ public GraggBulirschStoerIntegrator(final double minStep, final double maxStep, final double[] vecAbsoluteTolerance, final double[] vecRelativeTolerance) { super(METHOD_NAME, minStep, maxStep, vecAbsoluteTolerance, vecRelativeTolerance); setStabilityCheck(true, -1, -1, -1); setControlFactors(-1, -1, -1, -1); setOrderControl(-1, -1, -1); setInterpolationControl(true, -1); } /** Set the stability check controls. *

                            The stability check is performed on the first few iterations of * the extrapolation scheme. If this test fails, the step is rejected * and the stepsize is reduced.

                            *

                            By default, the test is performed, at most during two * iterations at each step, and at most once for each of these * iterations. The default stepsize reduction factor is 0.5.

                            * @param performStabilityCheck if true, stability check will be performed, if false, the check will be skipped * @param maxNumIter maximal number of iterations for which checks are * performed (the number of iterations is reset to default if negative * or null) * @param maxNumChecks maximal number of checks for each iteration * (the number of checks is reset to default if negative or null) * @param stepsizeReductionFactor stepsize reduction factor in case of * failure (the factor is reset to default if lower than 0.0001 or * greater than 0.9999) */ public void setStabilityCheck(final boolean performStabilityCheck, final int maxNumIter, final int maxNumChecks, final double stepsizeReductionFactor) { this.performTest = performStabilityCheck; this.maxIter = (maxNumIter <= 0) ? 2 : maxNumIter; this.maxChecks = (maxNumChecks <= 0) ? 1 : maxNumChecks; if ((stepsizeReductionFactor < 0.0001) || (stepsizeReductionFactor > 0.9999)) { this.stabilityReduction = 0.5; } else { this.stabilityReduction = stepsizeReductionFactor; } } /** Set the step size control factors. *

                            The new step size hNew is computed from the old one h by: *

                               * hNew = h * stepControl2 / (err/stepControl1)^(1/(2k+1))
                               * 
                            * where err is the scaled error and k the iteration number of the * extrapolation scheme (counting from 0). The default values are * 0.65 for stepControl1 and 0.94 for stepControl2.

                            *

                            The step size is subject to the restriction: *

                               * stepControl3^(1/(2k+1))/stepControl4 <= hNew/h <= 1/stepControl3^(1/(2k+1))
                               * 
                            * The default values are 0.02 for stepControl3 and 4.0 for * stepControl4.

                            * @param control1 first stepsize control factor (the factor is * reset to default if lower than 0.0001 or greater than 0.9999) * @param control2 second stepsize control factor (the factor * is reset to default if lower than 0.0001 or greater than 0.9999) * @param control3 third stepsize control factor (the factor is * reset to default if lower than 0.0001 or greater than 0.9999) * @param control4 fourth stepsize control factor (the factor * is reset to default if lower than 1.0001 or greater than 999.9) */ public void setControlFactors(final double control1, final double control2, final double control3, final double control4) { if ((control1 < 0.0001) || (control1 > 0.9999)) { this.stepControl1 = 0.65; } else { this.stepControl1 = control1; } if ((control2 < 0.0001) || (control2 > 0.9999)) { this.stepControl2 = 0.94; } else { this.stepControl2 = control2; } if ((control3 < 0.0001) || (control3 > 0.9999)) { this.stepControl3 = 0.02; } else { this.stepControl3 = control3; } if ((control4 < 1.0001) || (control4 > 999.9)) { this.stepControl4 = 4.0; } else { this.stepControl4 = control4; } } /** Set the order control parameters. *

                            The Gragg-Bulirsch-Stoer method changes both the step size and * the order during integration, in order to minimize computation * cost. Each extrapolation step increases the order by 2, so the * maximal order that will be used is always even, it is twice the * maximal number of columns in the extrapolation table.

                            *
                               * order is decreased if w(k-1) <= w(k)   * orderControl1
                               * order is increased if w(k)   <= w(k-1) * orderControl2
                               * 
                            *

                            where w is the table of work per unit step for each order * (number of function calls divided by the step length), and k is * the current order.

                            *

                            The default maximal order after construction is 18 (i.e. the * maximal number of columns is 9). The default values are 0.8 for * orderControl1 and 0.9 for orderControl2.

                            * @param maximalOrder maximal order in the extrapolation table (the * maximal order is reset to default if order <= 6 or odd) * @param control1 first order control factor (the factor is * reset to default if lower than 0.0001 or greater than 0.9999) * @param control2 second order control factor (the factor * is reset to default if lower than 0.0001 or greater than 0.9999) */ public void setOrderControl(final int maximalOrder, final double control1, final double control2) { if ((maximalOrder <= 6) || (maximalOrder % 2 != 0)) { this.maxOrder = 18; } if ((control1 < 0.0001) || (control1 > 0.9999)) { this.orderControl1 = 0.8; } else { this.orderControl1 = control1; } if ((control2 < 0.0001) || (control2 > 0.9999)) { this.orderControl2 = 0.9; } else { this.orderControl2 = control2; } // reinitialize the arrays initializeArrays(); } /** {@inheritDoc} */ @Override public void addStepHandler (final StepHandler handler) { super.addStepHandler(handler); // reinitialize the arrays initializeArrays(); } /** {@inheritDoc} */ @Override public void addEventHandler(final EventHandler function, final double maxCheckInterval, final double convergence, final int maxIterationCount, final UnivariateSolver solver) { super.addEventHandler(function, maxCheckInterval, convergence, maxIterationCount, solver); // reinitialize the arrays initializeArrays(); } /** Initialize the integrator internal arrays. */ private void initializeArrays() { final int size = maxOrder / 2; if ((sequence == null) || (sequence.length != size)) { // all arrays should be reallocated with the right size sequence = new int[size]; costPerStep = new int[size]; coeff = new double[size][]; costPerTimeUnit = new double[size]; optimalStep = new double[size]; } // step size sequence: 2, 6, 10, 14, ... for (int k = 0; k < size; ++k) { sequence[k] = 4 * k + 2; } // initialize the order selection cost array // (number of function calls for each column of the extrapolation table) costPerStep[0] = sequence[0] + 1; for (int k = 1; k < size; ++k) { costPerStep[k] = costPerStep[k-1] + sequence[k]; } // initialize the extrapolation tables for (int k = 0; k < size; ++k) { coeff[k] = (k > 0) ? new double[k] : null; for (int l = 0; l < k; ++l) { final double ratio = ((double) sequence[k]) / sequence[k-l-1]; coeff[k][l] = 1.0 / (ratio * ratio - 1.0); } } } /** Set the interpolation order control parameter. * The interpolation order for dense output is 2k - mudif + 1. The * default value for mudif is 4 and the interpolation error is used * in stepsize control by default. * @param useInterpolationErrorForControl if true, interpolation error is used * for stepsize control * @param mudifControlParameter interpolation order control parameter (the parameter * is reset to default if <= 0 or >= 7) */ public void setInterpolationControl(final boolean useInterpolationErrorForControl, final int mudifControlParameter) { this.useInterpolationError = useInterpolationErrorForControl; if ((mudifControlParameter <= 0) || (mudifControlParameter >= 7)) { this.mudif = 4; } else { this.mudif = mudifControlParameter; } } /** Update scaling array. * @param y1 first state vector to use for scaling * @param y2 second state vector to use for scaling * @param scale scaling array to update (can be shorter than state) */ private void rescale(final double[] y1, final double[] y2, final double[] scale) { if (vecAbsoluteTolerance == null) { for (int i = 0; i < scale.length; ++i) { final double yi = FastMath.max(FastMath.abs(y1[i]), FastMath.abs(y2[i])); scale[i] = scalAbsoluteTolerance + scalRelativeTolerance * yi; } } else { for (int i = 0; i < scale.length; ++i) { final double yi = FastMath.max(FastMath.abs(y1[i]), FastMath.abs(y2[i])); scale[i] = vecAbsoluteTolerance[i] + vecRelativeTolerance[i] * yi; } } } /** Perform integration over one step using substeps of a modified * midpoint method. * @param t0 initial time * @param y0 initial value of the state vector at t0 * @param step global step * @param k iteration number (from 0 to sequence.length - 1) * @param scale scaling array (can be shorter than state) * @param f placeholder where to put the state vector derivatives at each substep * (element 0 already contains initial derivative) * @param yMiddle placeholder where to put the state vector at the middle of the step * @param yEnd placeholder where to put the state vector at the end * @param yTmp placeholder for one state vector * @return true if computation was done properly, * false if stability check failed before end of computation * @exception MaxCountExceededException if the number of functions evaluations is exceeded * @exception DimensionMismatchException if arrays dimensions do not match equations settings */ private boolean tryStep(final double t0, final double[] y0, final double step, final int k, final double[] scale, final double[][] f, final double[] yMiddle, final double[] yEnd, final double[] yTmp) throws MaxCountExceededException, DimensionMismatchException { final int n = sequence[k]; final double subStep = step / n; final double subStep2 = 2 * subStep; // first substep double t = t0 + subStep; for (int i = 0; i < y0.length; ++i) { yTmp[i] = y0[i]; yEnd[i] = y0[i] + subStep * f[0][i]; } computeDerivatives(t, yEnd, f[1]); // other substeps for (int j = 1; j < n; ++j) { if (2 * j == n) { // save the point at the middle of the step System.arraycopy(yEnd, 0, yMiddle, 0, y0.length); } t += subStep; for (int i = 0; i < y0.length; ++i) { final double middle = yEnd[i]; yEnd[i] = yTmp[i] + subStep2 * f[j][i]; yTmp[i] = middle; } computeDerivatives(t, yEnd, f[j+1]); // stability check if (performTest && (j <= maxChecks) && (k < maxIter)) { double initialNorm = 0.0; for (int l = 0; l < scale.length; ++l) { final double ratio = f[0][l] / scale[l]; initialNorm += ratio * ratio; } double deltaNorm = 0.0; for (int l = 0; l < scale.length; ++l) { final double ratio = (f[j+1][l] - f[0][l]) / scale[l]; deltaNorm += ratio * ratio; } if (deltaNorm > 4 * FastMath.max(1.0e-15, initialNorm)) { return false; } } } // correction of the last substep (at t0 + step) for (int i = 0; i < y0.length; ++i) { yEnd[i] = 0.5 * (yTmp[i] + yEnd[i] + subStep * f[n][i]); } return true; } /** Extrapolate a vector. * @param offset offset to use in the coefficients table * @param k index of the last updated point * @param diag working diagonal of the Aitken-Neville's * triangle, without the last element * @param last last element */ private void extrapolate(final int offset, final int k, final double[][] diag, final double[] last) { // update the diagonal for (int j = 1; j < k; ++j) { for (int i = 0; i < last.length; ++i) { // Aitken-Neville's recursive formula diag[k-j-1][i] = diag[k-j][i] + coeff[k+offset][j-1] * (diag[k-j][i] - diag[k-j-1][i]); } } // update the last element for (int i = 0; i < last.length; ++i) { // Aitken-Neville's recursive formula last[i] = diag[0][i] + coeff[k+offset][k-1] * (diag[0][i] - last[i]); } } /** {@inheritDoc} */ @Override public void integrate(final ExpandableStatefulODE equations, final double t) throws NumberIsTooSmallException, DimensionMismatchException, MaxCountExceededException, NoBracketingException { sanityChecks(equations, t); setEquations(equations); final boolean forward = t > equations.getTime(); // create some internal working arrays final double[] y0 = equations.getCompleteState(); final double[] y = y0.clone(); final double[] yDot0 = new double[y.length]; final double[] y1 = new double[y.length]; final double[] yTmp = new double[y.length]; final double[] yTmpDot = new double[y.length]; final double[][] diagonal = new double[sequence.length-1][]; final double[][] y1Diag = new double[sequence.length-1][]; for (int k = 0; k < sequence.length-1; ++k) { diagonal[k] = new double[y.length]; y1Diag[k] = new double[y.length]; } final double[][][] fk = new double[sequence.length][][]; for (int k = 0; k < sequence.length; ++k) { fk[k] = new double[sequence[k] + 1][]; // all substeps start at the same point, so share the first array fk[k][0] = yDot0; for (int l = 0; l < sequence[k]; ++l) { fk[k][l+1] = new double[y0.length]; } } if (y != y0) { System.arraycopy(y0, 0, y, 0, y0.length); } final double[] yDot1 = new double[y0.length]; final double[][] yMidDots = new double[1 + 2 * sequence.length][y0.length]; // initial scaling final double[] scale = new double[mainSetDimension]; rescale(y, y, scale); // initial order selection final double tol = (vecRelativeTolerance == null) ? scalRelativeTolerance : vecRelativeTolerance[0]; final double log10R = FastMath.log10(FastMath.max(1.0e-10, tol)); int targetIter = FastMath.max(1, FastMath.min(sequence.length - 2, (int) FastMath.floor(0.5 - 0.6 * log10R))); // set up an interpolator sharing the integrator arrays final AbstractStepInterpolator interpolator = new GraggBulirschStoerStepInterpolator(y, yDot0, y1, yDot1, yMidDots, forward, equations.getPrimaryMapper(), equations.getSecondaryMappers()); interpolator.storeTime(equations.getTime()); stepStart = equations.getTime(); double hNew = 0; double maxError = Double.MAX_VALUE; boolean previousRejected = false; boolean firstTime = true; boolean newStep = true; boolean firstStepAlreadyComputed = false; initIntegration(equations.getTime(), y0, t); costPerTimeUnit[0] = 0; isLastStep = false; do { double error; boolean reject = false; if (newStep) { interpolator.shift(); // first evaluation, at the beginning of the step if (! firstStepAlreadyComputed) { computeDerivatives(stepStart, y, yDot0); } if (firstTime) { hNew = initializeStep(forward, 2 * targetIter + 1, scale, stepStart, y, yDot0, yTmp, yTmpDot); } newStep = false; } stepSize = hNew; // step adjustment near bounds if ((forward && (stepStart + stepSize > t)) || ((! forward) && (stepStart + stepSize < t))) { stepSize = t - stepStart; } final double nextT = stepStart + stepSize; isLastStep = forward ? (nextT >= t) : (nextT <= t); // iterate over several substep sizes int k = -1; for (boolean loop = true; loop; ) { ++k; // modified midpoint integration with the current substep if ( ! tryStep(stepStart, y, stepSize, k, scale, fk[k], (k == 0) ? yMidDots[0] : diagonal[k-1], (k == 0) ? y1 : y1Diag[k-1], yTmp)) { // the stability check failed, we reduce the global step hNew = FastMath.abs(filterStep(stepSize * stabilityReduction, forward, false)); reject = true; loop = false; } else { // the substep was computed successfully if (k > 0) { // extrapolate the state at the end of the step // using last iteration data extrapolate(0, k, y1Diag, y1); rescale(y, y1, scale); // estimate the error at the end of the step. error = 0; for (int j = 0; j < mainSetDimension; ++j) { final double e = FastMath.abs(y1[j] - y1Diag[0][j]) / scale[j]; error += e * e; } error = FastMath.sqrt(error / mainSetDimension); if ((error > 1.0e15) || ((k > 1) && (error > maxError))) { // error is too big, we reduce the global step hNew = FastMath.abs(filterStep(stepSize * stabilityReduction, forward, false)); reject = true; loop = false; } else { maxError = FastMath.max(4 * error, 1.0); // compute optimal stepsize for this order final double exp = 1.0 / (2 * k + 1); double fac = stepControl2 / FastMath.pow(error / stepControl1, exp); final double pow = FastMath.pow(stepControl3, exp); fac = FastMath.max(pow / stepControl4, FastMath.min(1 / pow, fac)); optimalStep[k] = FastMath.abs(filterStep(stepSize * fac, forward, true)); costPerTimeUnit[k] = costPerStep[k] / optimalStep[k]; // check convergence switch (k - targetIter) { case -1 : if ((targetIter > 1) && ! previousRejected) { // check if we can stop iterations now if (error <= 1.0) { // convergence have been reached just before targetIter loop = false; } else { // estimate if there is a chance convergence will // be reached on next iteration, using the // asymptotic evolution of error final double ratio = ((double) sequence [targetIter] * sequence[targetIter + 1]) / (sequence[0] * sequence[0]); if (error > ratio * ratio) { // we don't expect to converge on next iteration // we reject the step immediately and reduce order reject = true; loop = false; targetIter = k; if ((targetIter > 1) && (costPerTimeUnit[targetIter-1] < orderControl1 * costPerTimeUnit[targetIter])) { --targetIter; } hNew = optimalStep[targetIter]; } } } break; case 0: if (error <= 1.0) { // convergence has been reached exactly at targetIter loop = false; } else { // estimate if there is a chance convergence will // be reached on next iteration, using the // asymptotic evolution of error final double ratio = ((double) sequence[k+1]) / sequence[0]; if (error > ratio * ratio) { // we don't expect to converge on next iteration // we reject the step immediately reject = true; loop = false; if ((targetIter > 1) && (costPerTimeUnit[targetIter-1] < orderControl1 * costPerTimeUnit[targetIter])) { --targetIter; } hNew = optimalStep[targetIter]; } } break; case 1 : if (error > 1.0) { reject = true; if ((targetIter > 1) && (costPerTimeUnit[targetIter-1] < orderControl1 * costPerTimeUnit[targetIter])) { --targetIter; } hNew = optimalStep[targetIter]; } loop = false; break; default : if ((firstTime || isLastStep) && (error <= 1.0)) { loop = false; } break; } } } } } if (! reject) { // derivatives at end of step computeDerivatives(stepStart + stepSize, y1, yDot1); } // dense output handling double hInt = getMaxStep(); if (! reject) { // extrapolate state at middle point of the step for (int j = 1; j <= k; ++j) { extrapolate(0, j, diagonal, yMidDots[0]); } final int mu = 2 * k - mudif + 3; for (int l = 0; l < mu; ++l) { // derivative at middle point of the step final int l2 = l / 2; double factor = FastMath.pow(0.5 * sequence[l2], l); int middleIndex = fk[l2].length / 2; for (int i = 0; i < y0.length; ++i) { yMidDots[l+1][i] = factor * fk[l2][middleIndex + l][i]; } for (int j = 1; j <= k - l2; ++j) { factor = FastMath.pow(0.5 * sequence[j + l2], l); middleIndex = fk[l2+j].length / 2; for (int i = 0; i < y0.length; ++i) { diagonal[j-1][i] = factor * fk[l2+j][middleIndex+l][i]; } extrapolate(l2, j, diagonal, yMidDots[l+1]); } for (int i = 0; i < y0.length; ++i) { yMidDots[l+1][i] *= stepSize; } // compute centered differences to evaluate next derivatives for (int j = (l + 1) / 2; j <= k; ++j) { for (int m = fk[j].length - 1; m >= 2 * (l + 1); --m) { for (int i = 0; i < y0.length; ++i) { fk[j][m][i] -= fk[j][m-2][i]; } } } } if (mu >= 0) { // estimate the dense output coefficients final GraggBulirschStoerStepInterpolator gbsInterpolator = (GraggBulirschStoerStepInterpolator) interpolator; gbsInterpolator.computeCoefficients(mu, stepSize); if (useInterpolationError) { // use the interpolation error to limit stepsize final double interpError = gbsInterpolator.estimateError(scale); hInt = FastMath.abs(stepSize / FastMath.max(FastMath.pow(interpError, 1.0 / (mu+4)), 0.01)); if (interpError > 10.0) { hNew = hInt; reject = true; } } } } if (! reject) { // Discrete events handling interpolator.storeTime(stepStart + stepSize); stepStart = acceptStep(interpolator, y1, yDot1, t); // prepare next step interpolator.storeTime(stepStart); System.arraycopy(y1, 0, y, 0, y0.length); System.arraycopy(yDot1, 0, yDot0, 0, y0.length); firstStepAlreadyComputed = true; int optimalIter; if (k == 1) { optimalIter = 2; if (previousRejected) { optimalIter = 1; } } else if (k <= targetIter) { optimalIter = k; if (costPerTimeUnit[k-1] < orderControl1 * costPerTimeUnit[k]) { optimalIter = k-1; } else if (costPerTimeUnit[k] < orderControl2 * costPerTimeUnit[k-1]) { optimalIter = FastMath.min(k+1, sequence.length - 2); } } else { optimalIter = k - 1; if ((k > 2) && (costPerTimeUnit[k-2] < orderControl1 * costPerTimeUnit[k-1])) { optimalIter = k - 2; } if (costPerTimeUnit[k] < orderControl2 * costPerTimeUnit[optimalIter]) { optimalIter = FastMath.min(k, sequence.length - 2); } } if (previousRejected) { // after a rejected step neither order nor stepsize // should increase targetIter = FastMath.min(optimalIter, k); hNew = FastMath.min(FastMath.abs(stepSize), optimalStep[targetIter]); } else { // stepsize control if (optimalIter <= k) { hNew = optimalStep[optimalIter]; } else { if ((k < targetIter) && (costPerTimeUnit[k] < orderControl2 * costPerTimeUnit[k-1])) { hNew = filterStep(optimalStep[k] * costPerStep[optimalIter+1] / costPerStep[k], forward, false); } else { hNew = filterStep(optimalStep[k] * costPerStep[optimalIter] / costPerStep[k], forward, false); } } targetIter = optimalIter; } newStep = true; } hNew = FastMath.min(hNew, hInt); if (! forward) { hNew = -hNew; } firstTime = false; if (reject) { isLastStep = false; previousRejected = true; } else { previousRejected = false; } } while (!isLastStep); // dispatch results equations.setTime(stepStart); equations.setCompleteState(y); resetInternalState(); } } ././@LongLink100644 0 0 151 12126630646 10256 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/ode/nonstiff/AdamsNordsieckTransformer.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/ode/nonstiff/AdamsNordsieckTransformer.100644 1750 1750 36062 12126627715 32264 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.ode.nonstiff; import java.util.Arrays; import java.util.HashMap; import java.util.Map; import org.apache.commons.math3.fraction.BigFraction; import org.apache.commons.math3.linear.Array2DRowFieldMatrix; import org.apache.commons.math3.linear.Array2DRowRealMatrix; import org.apache.commons.math3.linear.ArrayFieldVector; import org.apache.commons.math3.linear.FieldDecompositionSolver; import org.apache.commons.math3.linear.FieldLUDecomposition; import org.apache.commons.math3.linear.FieldMatrix; import org.apache.commons.math3.linear.MatrixUtils; import org.apache.commons.math3.linear.QRDecomposition; import org.apache.commons.math3.linear.RealMatrix; /** Transformer to Nordsieck vectors for Adams integrators. *

                            This class is used by {@link AdamsBashforthIntegrator Adams-Bashforth} and * {@link AdamsMoultonIntegrator Adams-Moulton} integrators to convert between * classical representation with several previous first derivatives and Nordsieck * representation with higher order scaled derivatives.

                            * *

                            We define scaled derivatives si(n) at step n as: *

                             * s1(n) = h y'n for first derivative
                             * s2(n) = h2/2 y''n for second derivative
                             * s3(n) = h3/6 y'''n for third derivative
                             * ...
                             * sk(n) = hk/k! y(k)n for kth derivative
                             * 

                            * *

                            With the previous definition, the classical representation of multistep methods * uses first derivatives only, i.e. it handles yn, s1(n) and * qn where qn is defined as: *

                             *   qn = [ s1(n-1) s1(n-2) ... s1(n-(k-1)) ]T
                             * 
                            * (we omit the k index in the notation for clarity).

                            * *

                            Another possible representation uses the Nordsieck vector with * higher degrees scaled derivatives all taken at the same step, i.e it handles yn, * s1(n) and rn) where rn is defined as: *

                             * rn = [ s2(n), s3(n) ... sk(n) ]T
                             * 
                            * (here again we omit the k index in the notation for clarity) *

                            * *

                            Taylor series formulas show that for any index offset i, s1(n-i) can be * computed from s1(n), s2(n) ... sk(n), the formula being exact * for degree k polynomials. *

                             * s1(n-i) = s1(n) + ∑j>1 j (-i)j-1 sj(n)
                             * 
                            * The previous formula can be used with several values for i to compute the transform between * classical representation and Nordsieck vector at step end. The transform between rn * and qn resulting from the Taylor series formulas above is: *
                             * qn = s1(n) u + P rn
                             * 
                            * where u is the [ 1 1 ... 1 ]T vector and P is the (k-1)×(k-1) matrix built * with the j (-i)j-1 terms: *
                             *        [  -2   3   -4    5  ... ]
                             *        [  -4  12  -32   80  ... ]
                             *   P =  [  -6  27 -108  405  ... ]
                             *        [  -8  48 -256 1280  ... ]
                             *        [          ...           ]
                             * 

                            * *

                            Changing -i into +i in the formula above can be used to compute a similar transform between * classical representation and Nordsieck vector at step start. The resulting matrix is simply * the absolute value of matrix P.

                            * *

                            For {@link AdamsBashforthIntegrator Adams-Bashforth} method, the Nordsieck vector * at step n+1 is computed from the Nordsieck vector at step n as follows: *

                              *
                            • yn+1 = yn + s1(n) + uT rn
                            • *
                            • s1(n+1) = h f(tn+1, yn+1)
                            • *
                            • rn+1 = (s1(n) - s1(n+1)) P-1 u + P-1 A P rn
                            • *
                            * where A is a rows shifting matrix (the lower left part is an identity matrix): *
                             *        [ 0 0   ...  0 0 | 0 ]
                             *        [ ---------------+---]
                             *        [ 1 0   ...  0 0 | 0 ]
                             *    A = [ 0 1   ...  0 0 | 0 ]
                             *        [       ...      | 0 ]
                             *        [ 0 0   ...  1 0 | 0 ]
                             *        [ 0 0   ...  0 1 | 0 ]
                             * 

                            * *

                            For {@link AdamsMoultonIntegrator Adams-Moulton} method, the predicted Nordsieck vector * at step n+1 is computed from the Nordsieck vector at step n as follows: *

                              *
                            • Yn+1 = yn + s1(n) + uT rn
                            • *
                            • S1(n+1) = h f(tn+1, Yn+1)
                            • *
                            • Rn+1 = (s1(n) - s1(n+1)) P-1 u + P-1 A P rn
                            • *
                            * From this predicted vector, the corrected vector is computed as follows: *
                              *
                            • yn+1 = yn + S1(n+1) + [ -1 +1 -1 +1 ... ±1 ] rn+1
                            • *
                            • s1(n+1) = h f(tn+1, yn+1)
                            • *
                            • rn+1 = Rn+1 + (s1(n+1) - S1(n+1)) P-1 u
                            • *
                            * where the upper case Yn+1, S1(n+1) and Rn+1 represent the * predicted states whereas the lower case yn+1, sn+1 and rn+1 * represent the corrected states.

                            * *

                            We observe that both methods use similar update formulas. In both cases a P-1u * vector and a P-1 A P matrix are used that do not depend on the state, * they only depend on k. This class handles these transformations.

                            * * @version $Id: AdamsNordsieckTransformer.java 1416643 2012-12-03 19:37:14Z tn $ * @since 2.0 */ public class AdamsNordsieckTransformer { /** Cache for already computed coefficients. */ private static final Map CACHE = new HashMap(); /** Update matrix for the higher order derivatives h2/2y'', h3/6 y''' ... */ private final Array2DRowRealMatrix update; /** Update coefficients of the higher order derivatives wrt y'. */ private final double[] c1; /** Simple constructor. * @param nSteps number of steps of the multistep method * (excluding the one being computed) */ private AdamsNordsieckTransformer(final int nSteps) { // compute exact coefficients FieldMatrix bigP = buildP(nSteps); FieldDecompositionSolver pSolver = new FieldLUDecomposition(bigP).getSolver(); BigFraction[] u = new BigFraction[nSteps]; Arrays.fill(u, BigFraction.ONE); BigFraction[] bigC1 = pSolver .solve(new ArrayFieldVector(u, false)).toArray(); // update coefficients are computed by combining transform from // Nordsieck to multistep, then shifting rows to represent step advance // then applying inverse transform BigFraction[][] shiftedP = bigP.getData(); for (int i = shiftedP.length - 1; i > 0; --i) { // shift rows shiftedP[i] = shiftedP[i - 1]; } shiftedP[0] = new BigFraction[nSteps]; Arrays.fill(shiftedP[0], BigFraction.ZERO); FieldMatrix bigMSupdate = pSolver.solve(new Array2DRowFieldMatrix(shiftedP, false)); // convert coefficients to double update = MatrixUtils.bigFractionMatrixToRealMatrix(bigMSupdate); c1 = new double[nSteps]; for (int i = 0; i < nSteps; ++i) { c1[i] = bigC1[i].doubleValue(); } } /** Get the Nordsieck transformer for a given number of steps. * @param nSteps number of steps of the multistep method * (excluding the one being computed) * @return Nordsieck transformer for the specified number of steps */ public static AdamsNordsieckTransformer getInstance(final int nSteps) { synchronized(CACHE) { AdamsNordsieckTransformer t = CACHE.get(nSteps); if (t == null) { t = new AdamsNordsieckTransformer(nSteps); CACHE.put(nSteps, t); } return t; } } /** Get the number of steps of the method * (excluding the one being computed). * @return number of steps of the method * (excluding the one being computed) */ public int getNSteps() { return c1.length; } /** Build the P matrix. *

                            The P matrix general terms are shifted j (-i)j-1 terms: *

                                 *        [  -2   3   -4    5  ... ]
                                 *        [  -4  12  -32   80  ... ]
                                 *   P =  [  -6  27 -108  405  ... ]
                                 *        [  -8  48 -256 1280  ... ]
                                 *        [          ...           ]
                                 * 

                            * @param nSteps number of steps of the multistep method * (excluding the one being computed) * @return P matrix */ private FieldMatrix buildP(final int nSteps) { final BigFraction[][] pData = new BigFraction[nSteps][nSteps]; for (int i = 0; i < pData.length; ++i) { // build the P matrix elements from Taylor series formulas final BigFraction[] pI = pData[i]; final int factor = -(i + 1); int aj = factor; for (int j = 0; j < pI.length; ++j) { pI[j] = new BigFraction(aj * (j + 2)); aj *= factor; } } return new Array2DRowFieldMatrix(pData, false); } /** Initialize the high order scaled derivatives at step start. * @param h step size to use for scaling * @param t first steps times * @param y first steps states * @param yDot first steps derivatives * @return Nordieck vector at first step (h2/2 y''n, * h3/6 y'''n ... hk/k! y(k)n) */ public Array2DRowRealMatrix initializeHighOrderDerivatives(final double h, final double[] t, final double[][] y, final double[][] yDot) { // using Taylor series with di = ti - t0, we get: // y(ti) - y(t0) - di y'(t0) = di^2 / h^2 s2 + ... + di^k / h^k sk + O(h^(k+1)) // y'(ti) - y'(t0) = 2 di / h^2 s2 + ... + k di^(k-1) / h^k sk + O(h^k) // we write these relations for i = 1 to i= n-1 as a set of 2(n-1) linear // equations depending on the Nordsieck vector [s2 ... sk] final double[][] a = new double[2 * (y.length - 1)][c1.length]; final double[][] b = new double[2 * (y.length - 1)][y[0].length]; final double[] y0 = y[0]; final double[] yDot0 = yDot[0]; for (int i = 1; i < y.length; ++i) { final double di = t[i] - t[0]; final double ratio = di / h; double dikM1Ohk = 1 / h; // linear coefficients of equations // y(ti) - y(t0) - di y'(t0) and y'(ti) - y'(t0) final double[] aI = a[2 * i - 2]; final double[] aDotI = a[2 * i - 1]; for (int j = 0; j < aI.length; ++j) { dikM1Ohk *= ratio; aI[j] = di * dikM1Ohk; aDotI[j] = (j + 2) * dikM1Ohk; } // expected value of the previous equations final double[] yI = y[i]; final double[] yDotI = yDot[i]; final double[] bI = b[2 * i - 2]; final double[] bDotI = b[2 * i - 1]; for (int j = 0; j < yI.length; ++j) { bI[j] = yI[j] - y0[j] - di * yDot0[j]; bDotI[j] = yDotI[j] - yDot0[j]; } } // solve the rectangular system in the least square sense // to get the best estimate of the Nordsieck vector [s2 ... sk] QRDecomposition decomposition; decomposition = new QRDecomposition(new Array2DRowRealMatrix(a, false)); RealMatrix x = decomposition.getSolver().solve(new Array2DRowRealMatrix(b, false)); return new Array2DRowRealMatrix(x.getData(), false); } /** Update the high order scaled derivatives for Adams integrators (phase 1). *

                            The complete update of high order derivatives has a form similar to: *

                                 * rn+1 = (s1(n) - s1(n+1)) P-1 u + P-1 A P rn
                                 * 
                            * this method computes the P-1 A P rn part.

                            * @param highOrder high order scaled derivatives * (h2/2 y'', ... hk/k! y(k)) * @return updated high order derivatives * @see #updateHighOrderDerivativesPhase2(double[], double[], Array2DRowRealMatrix) */ public Array2DRowRealMatrix updateHighOrderDerivativesPhase1(final Array2DRowRealMatrix highOrder) { return update.multiply(highOrder); } /** Update the high order scaled derivatives Adams integrators (phase 2). *

                            The complete update of high order derivatives has a form similar to: *

                                 * rn+1 = (s1(n) - s1(n+1)) P-1 u + P-1 A P rn
                                 * 
                            * this method computes the (s1(n) - s1(n+1)) P-1 u part.

                            *

                            Phase 1 of the update must already have been performed.

                            * @param start first order scaled derivatives at step start * @param end first order scaled derivatives at step end * @param highOrder high order scaled derivatives, will be modified * (h2/2 y'', ... hk/k! y(k)) * @see #updateHighOrderDerivativesPhase1(Array2DRowRealMatrix) */ public void updateHighOrderDerivativesPhase2(final double[] start, final double[] end, final Array2DRowRealMatrix highOrder) { final double[][] data = highOrder.getDataRef(); for (int i = 0; i < data.length; ++i) { final double[] dataI = data[i]; final double c1I = c1[i]; for (int j = 0; j < dataI.length; ++j) { dataI[j] += c1I * (start[j] - end[j]); } } } } ././@LongLink100644 0 0 154 12126630646 10261 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/ode/nonstiff/EmbeddedRungeKuttaIntegrator.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/ode/nonstiff/EmbeddedRungeKuttaIntegrat100644 1750 1750 33021 12126627715 32265 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.ode.nonstiff; import org.apache.commons.math3.exception.DimensionMismatchException; import org.apache.commons.math3.exception.MaxCountExceededException; import org.apache.commons.math3.exception.NoBracketingException; import org.apache.commons.math3.exception.NumberIsTooSmallException; import org.apache.commons.math3.ode.ExpandableStatefulODE; import org.apache.commons.math3.util.FastMath; /** * This class implements the common part of all embedded Runge-Kutta * integrators for Ordinary Differential Equations. * *

                            These methods are embedded explicit Runge-Kutta methods with two * sets of coefficients allowing to estimate the error, their Butcher * arrays are as follows : *

                             *    0  |
                             *   c2  | a21
                             *   c3  | a31  a32
                             *   ... |        ...
                             *   cs  | as1  as2  ...  ass-1
                             *       |--------------------------
                             *       |  b1   b2  ...   bs-1  bs
                             *       |  b'1  b'2 ...   b's-1 b's
                             * 
                            *

                            * *

                            In fact, we rather use the array defined by ej = bj - b'j to * compute directly the error rather than computing two estimates and * then comparing them.

                            * *

                            Some methods are qualified as fsal (first same as last) * methods. This means the last evaluation of the derivatives in one * step is the same as the first in the next step. Then, this * evaluation can be reused from one step to the next one and the cost * of such a method is really s-1 evaluations despite the method still * has s stages. This behaviour is true only for successful steps, if * the step is rejected after the error estimation phase, no * evaluation is saved. For an fsal method, we have cs = 1 and * asi = bi for all i.

                            * * @version $Id: EmbeddedRungeKuttaIntegrator.java 1416643 2012-12-03 19:37:14Z tn $ * @since 1.2 */ public abstract class EmbeddedRungeKuttaIntegrator extends AdaptiveStepsizeIntegrator { /** Indicator for fsal methods. */ private final boolean fsal; /** Time steps from Butcher array (without the first zero). */ private final double[] c; /** Internal weights from Butcher array (without the first empty row). */ private final double[][] a; /** External weights for the high order method from Butcher array. */ private final double[] b; /** Prototype of the step interpolator. */ private final RungeKuttaStepInterpolator prototype; /** Stepsize control exponent. */ private final double exp; /** Safety factor for stepsize control. */ private double safety; /** Minimal reduction factor for stepsize control. */ private double minReduction; /** Maximal growth factor for stepsize control. */ private double maxGrowth; /** Build a Runge-Kutta integrator with the given Butcher array. * @param name name of the method * @param fsal indicate that the method is an fsal * @param c time steps from Butcher array (without the first zero) * @param a internal weights from Butcher array (without the first empty row) * @param b propagation weights for the high order method from Butcher array * @param prototype prototype of the step interpolator to use * @param minStep minimal step (sign is irrelevant, regardless of * integration direction, forward or backward), the last step can * be smaller than this * @param maxStep maximal step (sign is irrelevant, regardless of * integration direction, forward or backward), the last step can * be smaller than this * @param scalAbsoluteTolerance allowed absolute error * @param scalRelativeTolerance allowed relative error */ protected EmbeddedRungeKuttaIntegrator(final String name, final boolean fsal, final double[] c, final double[][] a, final double[] b, final RungeKuttaStepInterpolator prototype, final double minStep, final double maxStep, final double scalAbsoluteTolerance, final double scalRelativeTolerance) { super(name, minStep, maxStep, scalAbsoluteTolerance, scalRelativeTolerance); this.fsal = fsal; this.c = c; this.a = a; this.b = b; this.prototype = prototype; exp = -1.0 / getOrder(); // set the default values of the algorithm control parameters setSafety(0.9); setMinReduction(0.2); setMaxGrowth(10.0); } /** Build a Runge-Kutta integrator with the given Butcher array. * @param name name of the method * @param fsal indicate that the method is an fsal * @param c time steps from Butcher array (without the first zero) * @param a internal weights from Butcher array (without the first empty row) * @param b propagation weights for the high order method from Butcher array * @param prototype prototype of the step interpolator to use * @param minStep minimal step (must be positive even for backward * integration), the last step can be smaller than this * @param maxStep maximal step (must be positive even for backward * integration) * @param vecAbsoluteTolerance allowed absolute error * @param vecRelativeTolerance allowed relative error */ protected EmbeddedRungeKuttaIntegrator(final String name, final boolean fsal, final double[] c, final double[][] a, final double[] b, final RungeKuttaStepInterpolator prototype, final double minStep, final double maxStep, final double[] vecAbsoluteTolerance, final double[] vecRelativeTolerance) { super(name, minStep, maxStep, vecAbsoluteTolerance, vecRelativeTolerance); this.fsal = fsal; this.c = c; this.a = a; this.b = b; this.prototype = prototype; exp = -1.0 / getOrder(); // set the default values of the algorithm control parameters setSafety(0.9); setMinReduction(0.2); setMaxGrowth(10.0); } /** Get the order of the method. * @return order of the method */ public abstract int getOrder(); /** Get the safety factor for stepsize control. * @return safety factor */ public double getSafety() { return safety; } /** Set the safety factor for stepsize control. * @param safety safety factor */ public void setSafety(final double safety) { this.safety = safety; } /** {@inheritDoc} */ @Override public void integrate(final ExpandableStatefulODE equations, final double t) throws NumberIsTooSmallException, DimensionMismatchException, MaxCountExceededException, NoBracketingException { sanityChecks(equations, t); setEquations(equations); final boolean forward = t > equations.getTime(); // create some internal working arrays final double[] y0 = equations.getCompleteState(); final double[] y = y0.clone(); final int stages = c.length + 1; final double[][] yDotK = new double[stages][y.length]; final double[] yTmp = y0.clone(); final double[] yDotTmp = new double[y.length]; // set up an interpolator sharing the integrator arrays final RungeKuttaStepInterpolator interpolator = (RungeKuttaStepInterpolator) prototype.copy(); interpolator.reinitialize(this, yTmp, yDotK, forward, equations.getPrimaryMapper(), equations.getSecondaryMappers()); interpolator.storeTime(equations.getTime()); // set up integration control objects stepStart = equations.getTime(); double hNew = 0; boolean firstTime = true; initIntegration(equations.getTime(), y0, t); // main integration loop isLastStep = false; do { interpolator.shift(); // iterate over step size, ensuring local normalized error is smaller than 1 double error = 10; while (error >= 1.0) { if (firstTime || !fsal) { // first stage computeDerivatives(stepStart, y, yDotK[0]); } if (firstTime) { final double[] scale = new double[mainSetDimension]; if (vecAbsoluteTolerance == null) { for (int i = 0; i < scale.length; ++i) { scale[i] = scalAbsoluteTolerance + scalRelativeTolerance * FastMath.abs(y[i]); } } else { for (int i = 0; i < scale.length; ++i) { scale[i] = vecAbsoluteTolerance[i] + vecRelativeTolerance[i] * FastMath.abs(y[i]); } } hNew = initializeStep(forward, getOrder(), scale, stepStart, y, yDotK[0], yTmp, yDotK[1]); firstTime = false; } stepSize = hNew; if (forward) { if (stepStart + stepSize >= t) { stepSize = t - stepStart; } } else { if (stepStart + stepSize <= t) { stepSize = t - stepStart; } } // next stages for (int k = 1; k < stages; ++k) { for (int j = 0; j < y0.length; ++j) { double sum = a[k-1][0] * yDotK[0][j]; for (int l = 1; l < k; ++l) { sum += a[k-1][l] * yDotK[l][j]; } yTmp[j] = y[j] + stepSize * sum; } computeDerivatives(stepStart + c[k-1] * stepSize, yTmp, yDotK[k]); } // estimate the state at the end of the step for (int j = 0; j < y0.length; ++j) { double sum = b[0] * yDotK[0][j]; for (int l = 1; l < stages; ++l) { sum += b[l] * yDotK[l][j]; } yTmp[j] = y[j] + stepSize * sum; } // estimate the error at the end of the step error = estimateError(yDotK, y, yTmp, stepSize); if (error >= 1.0) { // reject the step and attempt to reduce error by stepsize control final double factor = FastMath.min(maxGrowth, FastMath.max(minReduction, safety * FastMath.pow(error, exp))); hNew = filterStep(stepSize * factor, forward, false); } } // local error is small enough: accept the step, trigger events and step handlers interpolator.storeTime(stepStart + stepSize); System.arraycopy(yTmp, 0, y, 0, y0.length); System.arraycopy(yDotK[stages - 1], 0, yDotTmp, 0, y0.length); stepStart = acceptStep(interpolator, y, yDotTmp, t); System.arraycopy(y, 0, yTmp, 0, y.length); if (!isLastStep) { // prepare next step interpolator.storeTime(stepStart); if (fsal) { // save the last evaluation for the next step System.arraycopy(yDotTmp, 0, yDotK[0], 0, y0.length); } // stepsize control for next step final double factor = FastMath.min(maxGrowth, FastMath.max(minReduction, safety * FastMath.pow(error, exp))); final double scaledH = stepSize * factor; final double nextT = stepStart + scaledH; final boolean nextIsLast = forward ? (nextT >= t) : (nextT <= t); hNew = filterStep(scaledH, forward, nextIsLast); final double filteredNextT = stepStart + hNew; final boolean filteredNextIsLast = forward ? (filteredNextT >= t) : (filteredNextT <= t); if (filteredNextIsLast) { hNew = t - stepStart; } } } while (!isLastStep); // dispatch results equations.setTime(stepStart); equations.setCompleteState(y); resetInternalState(); } /** Get the minimal reduction factor for stepsize control. * @return minimal reduction factor */ public double getMinReduction() { return minReduction; } /** Set the minimal reduction factor for stepsize control. * @param minReduction minimal reduction factor */ public void setMinReduction(final double minReduction) { this.minReduction = minReduction; } /** Get the maximal growth factor for stepsize control. * @return maximal growth factor */ public double getMaxGrowth() { return maxGrowth; } /** Set the maximal growth factor for stepsize control. * @param maxGrowth maximal growth factor */ public void setMaxGrowth(final double maxGrowth) { this.maxGrowth = maxGrowth; } /** Compute the error ratio. * @param yDotK derivatives computed during the first stages * @param y0 estimate of the step at the start of the step * @param y1 estimate of the step at the end of the step * @param h current step * @return error ratio, greater than 1 if step should be rejected */ protected abstract double estimateError(double[][] yDotK, double[] y0, double[] y1, double h); } ././@LongLink100644 0 0 154 12126630646 10261 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/ode/nonstiff/HighamHall54StepInterpolator.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/ode/nonstiff/HighamHall54StepInterpolat100644 1750 1750 12410 12126627715 32130 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.ode.nonstiff; import org.apache.commons.math3.ode.sampling.StepInterpolator; /** * This class represents an interpolator over the last step during an * ODE integration for the 5(4) Higham and Hall integrator. * * @see HighamHall54Integrator * * @version $Id: HighamHall54StepInterpolator.java 1416643 2012-12-03 19:37:14Z tn $ * @since 1.2 */ class HighamHall54StepInterpolator extends RungeKuttaStepInterpolator { /** Serializable version identifier */ private static final long serialVersionUID = 20111120L; /** Simple constructor. * This constructor builds an instance that is not usable yet, the * {@link * org.apache.commons.math3.ode.sampling.AbstractStepInterpolator#reinitialize} * method should be called before using the instance in order to * initialize the internal arrays. This constructor is used only * in order to delay the initialization in some cases. The {@link * EmbeddedRungeKuttaIntegrator} uses the prototyping design pattern * to create the step interpolators by cloning an uninitialized model * and later initializing the copy. */ public HighamHall54StepInterpolator() { super(); } /** Copy constructor. * @param interpolator interpolator to copy from. The copy is a deep * copy: its arrays are separated from the original arrays of the * instance */ public HighamHall54StepInterpolator(final HighamHall54StepInterpolator interpolator) { super(interpolator); } /** {@inheritDoc} */ @Override protected StepInterpolator doCopy() { return new HighamHall54StepInterpolator(this); } /** {@inheritDoc} */ @Override protected void computeInterpolatedStateAndDerivatives(final double theta, final double oneMinusThetaH) { final double bDot0 = 1 + theta * (-15.0/2.0 + theta * (16.0 - 10.0 * theta)); final double bDot2 = theta * (459.0/16.0 + theta * (-729.0/8.0 + 135.0/2.0 * theta)); final double bDot3 = theta * (-44.0 + theta * (152.0 - 120.0 * theta)); final double bDot4 = theta * (375.0/16.0 + theta * (-625.0/8.0 + 125.0/2.0 * theta)); final double bDot5 = theta * 5.0/8.0 * (2 * theta - 1); if ((previousState != null) && (theta <= 0.5)) { final double hTheta = h * theta; final double b0 = hTheta * (1.0 + theta * (-15.0/4.0 + theta * (16.0/3.0 - 5.0/2.0 * theta))); final double b2 = hTheta * ( theta * (459.0/32.0 + theta * (-243.0/8.0 + theta * 135.0/8.0))); final double b3 = hTheta * ( theta * (-22.0 + theta * (152.0/3.0 + theta * -30.0))); final double b4 = hTheta * ( theta * (375.0/32.0 + theta * (-625.0/24.0 + theta * 125.0/8.0))); final double b5 = hTheta * ( theta * (-5.0/16.0 + theta * 5.0/12.0)); for (int i = 0; i < interpolatedState.length; ++i) { final double yDot0 = yDotK[0][i]; final double yDot2 = yDotK[2][i]; final double yDot3 = yDotK[3][i]; final double yDot4 = yDotK[4][i]; final double yDot5 = yDotK[5][i]; interpolatedState[i] = previousState[i] + b0 * yDot0 + b2 * yDot2 + b3 * yDot3 + b4 * yDot4 + b5 * yDot5; interpolatedDerivatives[i] = bDot0 * yDot0 + bDot2 * yDot2 + bDot3 * yDot3 + bDot4 * yDot4 + bDot5 * yDot5; } } else { final double theta2 = theta * theta; final double b0 = h * (-1.0/12.0 + theta * (1.0 + theta * (-15.0/4.0 + theta * (16.0/3.0 + theta * -5.0/2.0)))); final double b2 = h * (-27.0/32.0 + theta2 * (459.0/32.0 + theta * (-243.0/8.0 + theta * 135.0/8.0))); final double b3 = h * (4.0/3.0 + theta2 * (-22.0 + theta * (152.0/3.0 + theta * -30.0))); final double b4 = h * (-125.0/96.0 + theta2 * (375.0/32.0 + theta * (-625.0/24.0 + theta * 125.0/8.0))); final double b5 = h * (-5.0/48.0 + theta2 * (-5.0/16.0 + theta * 5.0/12.0)); for (int i = 0; i < interpolatedState.length; ++i) { final double yDot0 = yDotK[0][i]; final double yDot2 = yDotK[2][i]; final double yDot3 = yDotK[3][i]; final double yDot4 = yDotK[4][i]; final double yDot5 = yDotK[5][i]; interpolatedState[i] = currentState[i] + b0 * yDot0 + b2 * yDot2 + b3 * yDot3 + b4 * yDot4 + b5 * yDot5; interpolatedDerivatives[i] = bDot0 * yDot0 + bDot2 * yDot2 + bDot3 * yDot3 + bDot4 * yDot4 + bDot5 * yDot5; } } } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/ode/nonstiff/EulerIntegrator.java100644 1750 1750 4754 12126627715 31112 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.ode.nonstiff; /** * This class implements a simple Euler integrator for Ordinary * Differential Equations. * *

                            The Euler algorithm is the simplest one that can be used to * integrate ordinary differential equations. It is a simple inversion * of the forward difference expression : * f'=(f(t+h)-f(t))/h which leads to * f(t+h)=f(t)+hf'. The interpolation scheme used for * dense output is the linear scheme already used for integration.

                            * *

                            This algorithm looks cheap because it needs only one function * evaluation per step. However, as it uses linear estimates, it needs * very small steps to achieve high accuracy, and small steps lead to * numerical errors and instabilities.

                            * *

                            This algorithm is almost never used and has been included in * this package only as a comparison reference for more useful * integrators.

                            * * @see MidpointIntegrator * @see ClassicalRungeKuttaIntegrator * @see GillIntegrator * @see ThreeEighthesIntegrator * @version $Id: EulerIntegrator.java 1416643 2012-12-03 19:37:14Z tn $ * @since 1.2 */ public class EulerIntegrator extends RungeKuttaIntegrator { /** Time steps Butcher array. */ private static final double[] STATIC_C = { }; /** Internal weights Butcher array. */ private static final double[][] STATIC_A = { }; /** Propagation weights Butcher array. */ private static final double[] STATIC_B = { 1.0 }; /** Simple constructor. * Build an Euler integrator with the given step. * @param step integration step */ public EulerIntegrator(final double step) { super("Euler", STATIC_C, STATIC_A, STATIC_B, new EulerStepInterpolator(), step); } } ././@LongLink100644 0 0 145 12126630646 10261 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/ode/nonstiff/EulerStepInterpolator.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/ode/nonstiff/EulerStepInterpolator.java100644 1750 1750 7125 12126627715 32305 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.ode.nonstiff; import org.apache.commons.math3.ode.sampling.StepInterpolator; /** * This class implements a linear interpolator for step. * *

                            This interpolator computes dense output inside the last * step computed. The interpolation equation is consistent with the * integration scheme : *

                              *
                            • Using reference point at step start:
                              * y(tn + θ h) = y (tn) + θ h y' *
                            • *
                            • Using reference point at step end:
                              * y(tn + θ h) = y (tn + h) - (1-θ) h y' *
                            • *
                            *

                            * * where θ belongs to [0 ; 1] and where y' is the evaluation of * the derivatives already computed during the step.

                            * * @see EulerIntegrator * @version $Id: EulerStepInterpolator.java 1416643 2012-12-03 19:37:14Z tn $ * @since 1.2 */ class EulerStepInterpolator extends RungeKuttaStepInterpolator { /** Serializable version identifier. */ private static final long serialVersionUID = 20111120L; /** Simple constructor. * This constructor builds an instance that is not usable yet, the * {@link * org.apache.commons.math3.ode.sampling.AbstractStepInterpolator#reinitialize} * method should be called before using the instance in order to * initialize the internal arrays. This constructor is used only * in order to delay the initialization in some cases. The {@link * RungeKuttaIntegrator} class uses the prototyping design pattern * to create the step interpolators by cloning an uninitialized model * and later initializing the copy. */ public EulerStepInterpolator() { } /** Copy constructor. * @param interpolator interpolator to copy from. The copy is a deep * copy: its arrays are separated from the original arrays of the * instance */ public EulerStepInterpolator(final EulerStepInterpolator interpolator) { super(interpolator); } /** {@inheritDoc} */ @Override protected StepInterpolator doCopy() { return new EulerStepInterpolator(this); } /** {@inheritDoc} */ @Override protected void computeInterpolatedStateAndDerivatives(final double theta, final double oneMinusThetaH) { if ((previousState != null) && (theta <= 0.5)) { for (int i = 0; i < interpolatedState.length; ++i) { interpolatedState[i] = previousState[i] + theta * h * yDotK[0][i]; } System.arraycopy(yDotK[0], 0, interpolatedDerivatives, 0, interpolatedDerivatives.length); } else { for (int i = 0; i < interpolatedState.length; ++i) { interpolatedState[i] = currentState[i] - oneMinusThetaH * yDotK[0][i]; } System.arraycopy(yDotK[0], 0, interpolatedDerivatives, 0, interpolatedDerivatives.length); } } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/ode/ParameterizedWrapper.java100644 1750 1750 6042 12126627716 30277 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.ode; import java.util.ArrayList; import java.util.Collection; import org.apache.commons.math3.exception.DimensionMismatchException; import org.apache.commons.math3.exception.MaxCountExceededException; /** Wrapper class enabling {@link FirstOrderDifferentialEquations basic simple} * ODE instances to be used when processing {@link JacobianMatrices}. * * @version $Id: ParameterizedWrapper.java 1416643 2012-12-03 19:37:14Z tn $ * @since 3.0 */ class ParameterizedWrapper implements ParameterizedODE { /** Basic FODE without parameter. */ private final FirstOrderDifferentialEquations fode; /** Simple constructor. * @param ode original first order differential equations */ public ParameterizedWrapper(final FirstOrderDifferentialEquations ode) { this.fode = ode; } /** Get the dimension of the underlying FODE. * @return dimension of the underlying FODE */ public int getDimension() { return fode.getDimension(); } /** Get the current time derivative of the state vector of the underlying FODE. * @param t current value of the independent time variable * @param y array containing the current value of the state vector * @param yDot placeholder array where to put the time derivative of the state vector * @exception MaxCountExceededException if the number of functions evaluations is exceeded * @exception DimensionMismatchException if arrays dimensions do not match equations settings */ public void computeDerivatives(double t, double[] y, double[] yDot) throws MaxCountExceededException, DimensionMismatchException { fode.computeDerivatives(t, y, yDot); } /** {@inheritDoc} */ public Collection getParametersNames() { return new ArrayList(); } /** {@inheritDoc} */ public boolean isSupported(String name) { return false; } /** {@inheritDoc} */ public double getParameter(String name) throws UnknownParameterException { if (!isSupported(name)) { throw new UnknownParameterException(name); } return Double.NaN; } /** {@inheritDoc} */ public void setParameter(String name, double value) { } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/ode/AbstractParameterizable.java100644 1750 1750 5126 12126627716 30737 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.ode; import java.util.ArrayList; import java.util.Collection; /** This abstract class provides boilerplate parameters list. * * @version $Id: AbstractParameterizable.java 1416643 2012-12-03 19:37:14Z tn $ * @since 3.0 */ public abstract class AbstractParameterizable implements Parameterizable { /** List of the parameters names. */ private final Collection parametersNames; /** Simple constructor. * @param names names of the supported parameters */ protected AbstractParameterizable(final String ... names) { parametersNames = new ArrayList(); for (final String name : names) { parametersNames.add(name); } } /** Simple constructor. * @param names names of the supported parameters */ protected AbstractParameterizable(final Collection names) { parametersNames = new ArrayList(); parametersNames.addAll(names); } /** {@inheritDoc} */ public Collection getParametersNames() { return parametersNames; } /** {@inheritDoc} */ public boolean isSupported(final String name) { for (final String supportedName : parametersNames) { if (supportedName.equals(name)) { return true; } } return false; } /** Check if a parameter is supported and throw an IllegalArgumentException if not. * @param name name of the parameter to check * @exception UnknownParameterException if the parameter is not supported * @see #isSupported(String) */ public void complainIfNotSupported(final String name) throws UnknownParameterException { if (!isSupported(name)) { throw new UnknownParameterException(name); } } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/package-info.java100644 1750 1750 1611 12126627720 25707 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ /** * Common classes used throughout the commons-math library. */ package org.apache.commons.math3; commons-math3-3.2-src/src/main/java/org/apache/commons/math3/complex/package-info.java100644 1750 1750 1666 12126627716 27375 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ /** * * Complex number type and implementations of complex transcendental * functions. * */ package org.apache.commons.math3.complex; commons-math3-3.2-src/src/main/java/org/apache/commons/math3/complex/Quaternion.java100644 1750 1750 33455 12126627716 27217 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.complex; import java.io.Serializable; import org.apache.commons.math3.util.FastMath; import org.apache.commons.math3.util.MathUtils; import org.apache.commons.math3.util.Precision; import org.apache.commons.math3.exception.DimensionMismatchException; import org.apache.commons.math3.exception.ZeroException; import org.apache.commons.math3.exception.util.LocalizedFormats; /** * This class implements * quaternions (Hamilton's hypercomplex numbers). *
                            * Instance of this class are guaranteed to be immutable. * * @since 3.1 * @version $Id: Quaternion.java 1421249 2012-12-13 12:32:03Z erans $ */ public final class Quaternion implements Serializable { /** Identity quaternion. */ public static final Quaternion IDENTITY = new Quaternion(1, 0, 0, 0); /** Zero quaternion. */ public static final Quaternion ZERO = new Quaternion(0, 0, 0, 0); /** i */ public static final Quaternion I = new Quaternion(0, 1, 0, 0); /** j */ public static final Quaternion J = new Quaternion(0, 0, 1, 0); /** k */ public static final Quaternion K = new Quaternion(0, 0, 0, 1); /** Serializable version identifier. */ private static final long serialVersionUID = 20092012L; /** First component (scalar part). */ private final double q0; /** Second component (first vector part). */ private final double q1; /** Third component (second vector part). */ private final double q2; /** Fourth component (third vector part). */ private final double q3; /** * Builds a quaternion from its components. * * @param a Scalar component. * @param b First vector component. * @param c Second vector component. * @param d Third vector component. */ public Quaternion(final double a, final double b, final double c, final double d) { this.q0 = a; this.q1 = b; this.q2 = c; this.q3 = d; } /** * Builds a quaternion from scalar and vector parts. * * @param scalar Scalar part of the quaternion. * @param v Components of the vector part of the quaternion. * * @throws DimensionMismatchException if the array length is not 3. */ public Quaternion(final double scalar, final double[] v) throws DimensionMismatchException { if (v.length != 3) { throw new DimensionMismatchException(v.length, 3); } this.q0 = scalar; this.q1 = v[0]; this.q2 = v[1]; this.q3 = v[2]; } /** * Builds a pure quaternion from a vector (assuming that the scalar * part is zero). * * @param v Components of the vector part of the pure quaternion. */ public Quaternion(final double[] v) { this(0, v); } /** * Returns the conjugate quaternion of the instance. * * @return the conjugate quaternion */ public Quaternion getConjugate() { return new Quaternion(q0, -q1, -q2, -q3); } /** * Returns the Hamilton product of two quaternions. * * @param q1 First quaternion. * @param q2 Second quaternion. * @return the product {@code q1} and {@code q2}, in that order. */ public static Quaternion multiply(final Quaternion q1, final Quaternion q2) { // Components of the first quaternion. final double q1a = q1.getQ0(); final double q1b = q1.getQ1(); final double q1c = q1.getQ2(); final double q1d = q1.getQ3(); // Components of the second quaternion. final double q2a = q2.getQ0(); final double q2b = q2.getQ1(); final double q2c = q2.getQ2(); final double q2d = q2.getQ3(); // Components of the product. final double w = q1a * q2a - q1b * q2b - q1c * q2c - q1d * q2d; final double x = q1a * q2b + q1b * q2a + q1c * q2d - q1d * q2c; final double y = q1a * q2c - q1b * q2d + q1c * q2a + q1d * q2b; final double z = q1a * q2d + q1b * q2c - q1c * q2b + q1d * q2a; return new Quaternion(w, x, y, z); } /** * Returns the Hamilton product of the instance by a quaternion. * * @param q Quaternion. * @return the product of this instance with {@code q}, in that order. */ public Quaternion multiply(final Quaternion q) { return multiply(this, q); } /** * Computes the sum of two quaternions. * * @param q1 Quaternion. * @param q2 Quaternion. * @return the sum of {@code q1} and {@code q2}. */ public static Quaternion add(final Quaternion q1, final Quaternion q2) { return new Quaternion(q1.getQ0() + q2.getQ0(), q1.getQ1() + q2.getQ1(), q1.getQ2() + q2.getQ2(), q1.getQ3() + q2.getQ3()); } /** * Computes the sum of the instance and another quaternion. * * @param q Quaternion. * @return the sum of this instance and {@code q} */ public Quaternion add(final Quaternion q) { return add(this, q); } /** * Subtracts two quaternions. * * @param q1 First Quaternion. * @param q2 Second quaternion. * @return the difference between {@code q1} and {@code q2}. */ public static Quaternion subtract(final Quaternion q1, final Quaternion q2) { return new Quaternion(q1.getQ0() - q2.getQ0(), q1.getQ1() - q2.getQ1(), q1.getQ2() - q2.getQ2(), q1.getQ3() - q2.getQ3()); } /** * Subtracts a quaternion from the instance. * * @param q Quaternion. * @return the difference between this instance and {@code q}. */ public Quaternion subtract(final Quaternion q) { return subtract(this, q); } /** * Computes the dot-product of two quaternions. * * @param q1 Quaternion. * @param q2 Quaternion. * @return the dot product of {@code q1} and {@code q2}. */ public static double dotProduct(final Quaternion q1, final Quaternion q2) { return q1.getQ0() * q2.getQ0() + q1.getQ1() * q2.getQ1() + q1.getQ2() * q2.getQ2() + q1.getQ3() * q2.getQ3(); } /** * Computes the dot-product of the instance by a quaternion. * * @param q Quaternion. * @return the dot product of this instance and {@code q}. */ public double dotProduct(final Quaternion q) { return dotProduct(this, q); } /** * Computes the norm of the quaternion. * * @return the norm. */ public double getNorm() { return FastMath.sqrt(q0 * q0 + q1 * q1 + q2 * q2 + q3 * q3); } /** * Computes the normalized quaternion (the versor of the instance). * The norm of the quaternion must not be zero. * * @return a normalized quaternion. * @throws ZeroException if the norm of the quaternion is zero. */ public Quaternion normalize() { final double norm = getNorm(); if (norm < Precision.SAFE_MIN) { throw new ZeroException(LocalizedFormats.NORM, norm); } return new Quaternion(q0 / norm, q1 / norm, q2 / norm, q3 / norm); } /** * {@inheritDoc} */ @Override public boolean equals(Object other) { if (this == other) { return true; } if (other instanceof Quaternion) { final Quaternion q = (Quaternion) other; return q0 == q.getQ0() && q1 == q.getQ1() && q2 == q.getQ2() && q3 == q.getQ3(); } return false; } /** * {@inheritDoc} */ @Override public int hashCode() { // "Effective Java" (second edition, p. 47). int result = 17; for (double comp : new double[] { q0, q1, q2, q3 }) { final int c = MathUtils.hash(comp); result = 31 * result + c; } return result; } /** * Checks whether this instance is equal to another quaternion * within a given tolerance. * * @param q Quaternion with which to compare the current quaternion. * @param eps Tolerance. * @return {@code true} if the each of the components are equal * within the allowed absolute error. */ public boolean equals(final Quaternion q, final double eps) { return Precision.equals(q0, q.getQ0(), eps) && Precision.equals(q1, q.getQ1(), eps) && Precision.equals(q2, q.getQ2(), eps) && Precision.equals(q3, q.getQ3(), eps); } /** * Checks whether the instance is a unit quaternion within a given * tolerance. * * @param eps Tolerance (absolute error). * @return {@code true} if the norm is 1 within the given tolerance, * {@code false} otherwise */ public boolean isUnitQuaternion(double eps) { return Precision.equals(getNorm(), 1d, eps); } /** * Checks whether the instance is a pure quaternion within a given * tolerance. * * @param eps Tolerance (absolute error). * @return {@code true} if the scalar part of the quaternion is zero. */ public boolean isPureQuaternion(double eps) { return FastMath.abs(getQ0()) <= eps; } /** * Returns the polar form of the quaternion. * * @return the unit quaternion with positive scalar part. */ public Quaternion getPositivePolarForm() { if (getQ0() < 0) { final Quaternion unitQ = normalize(); // The quaternion of rotation (normalized quaternion) q and -q // are equivalent (i.e. represent the same rotation). return new Quaternion(-unitQ.getQ0(), -unitQ.getQ1(), -unitQ.getQ2(), -unitQ.getQ3()); } else { return this.normalize(); } } /** * Returns the inverse of this instance. * The norm of the quaternion must not be zero. * * @return the inverse. * @throws ZeroException if the norm (squared) of the quaternion is zero. */ public Quaternion getInverse() { final double squareNorm = q0 * q0 + q1 * q1 + q2 * q2 + q3 * q3; if (squareNorm < Precision.SAFE_MIN) { throw new ZeroException(LocalizedFormats.NORM, squareNorm); } return new Quaternion(q0 / squareNorm, -q1 / squareNorm, -q2 / squareNorm, -q3 / squareNorm); } /** * Gets the first component of the quaternion (scalar part). * * @return the scalar part. */ public double getQ0() { return q0; } /** * Gets the second component of the quaternion (first component * of the vector part). * * @return the first component of the vector part. */ public double getQ1() { return q1; } /** * Gets the third component of the quaternion (second component * of the vector part). * * @return the second component of the vector part. */ public double getQ2() { return q2; } /** * Gets the fourth component of the quaternion (third component * of the vector part). * * @return the third component of the vector part. */ public double getQ3() { return q3; } /** * Gets the scalar part of the quaternion. * * @return the scalar part. * @see #getQ0() */ public double getScalarPart() { return getQ0(); } /** * Gets the three components of the vector part of the quaternion. * * @return the vector part. * @see #getQ1() * @see #getQ2() * @see #getQ3() */ public double[] getVectorPart() { return new double[] { getQ1(), getQ2(), getQ3() }; } /** * Multiplies the instance by a scalar. * * @param alpha Scalar factor. * @return a scaled quaternion. */ public Quaternion multiply(final double alpha) { return new Quaternion(alpha * q0, alpha * q1, alpha * q2, alpha * q3); } /** * {@inheritDoc} */ @Override public String toString() { final String sp = " "; final StringBuilder s = new StringBuilder(); s.append("[") .append(q0).append(sp) .append(q1).append(sp) .append(q2).append(sp) .append(q3) .append("]"); return s.toString(); } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/complex/Complex.java100644 1750 1750 121053 12126627716 26511 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.complex; import java.io.Serializable; import java.util.ArrayList; import java.util.List; import org.apache.commons.math3.FieldElement; import org.apache.commons.math3.exception.NotPositiveException; import org.apache.commons.math3.exception.NullArgumentException; import org.apache.commons.math3.exception.util.LocalizedFormats; import org.apache.commons.math3.util.FastMath; import org.apache.commons.math3.util.MathUtils; /** * Representation of a Complex number, i.e. a number which has both a * real and imaginary part. *
                            * Implementations of arithmetic operations handle {@code NaN} and * infinite values according to the rules for {@link java.lang.Double}, i.e. * {@link #equals} is an equivalence relation for all instances that have * a {@code NaN} in either real or imaginary part, e.g. the following are * considered equal: *
                              *
                            • {@code 1 + NaNi}
                            • *
                            • {@code NaN + i}
                            • *
                            • {@code NaN + NaNi}
                            • *
                            * Note that this is in contradiction with the IEEE-754 standard for floating * point numbers (according to which the test {@code x == x} must fail if * {@code x} is {@code NaN}). The method * {@link org.apache.commons.math3.util.Precision#equals(double,double,int) * equals for primitive double} in {@link org.apache.commons.math3.util.Precision} * conforms with IEEE-754 while this class conforms with the standard behavior * for Java object types. *
                            * Implements Serializable since 2.0 * * @version $Id: Complex.java 1459927 2013-03-22 18:55:58Z luc $ */ public class Complex implements FieldElement, Serializable { /** The square root of -1. A number representing "0.0 + 1.0i" */ public static final Complex I = new Complex(0.0, 1.0); // CHECKSTYLE: stop ConstantName /** A complex number representing "NaN + NaNi" */ public static final Complex NaN = new Complex(Double.NaN, Double.NaN); // CHECKSTYLE: resume ConstantName /** A complex number representing "+INF + INFi" */ public static final Complex INF = new Complex(Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY); /** A complex number representing "1.0 + 0.0i" */ public static final Complex ONE = new Complex(1.0, 0.0); /** A complex number representing "0.0 + 0.0i" */ public static final Complex ZERO = new Complex(0.0, 0.0); /** Serializable version identifier */ private static final long serialVersionUID = -6195664516687396620L; /** The imaginary part. */ private final double imaginary; /** The real part. */ private final double real; /** Record whether this complex number is equal to NaN. */ private final transient boolean isNaN; /** Record whether this complex number is infinite. */ private final transient boolean isInfinite; /** * Create a complex number given only the real part. * * @param real Real part. */ public Complex(double real) { this(real, 0.0); } /** * Create a complex number given the real and imaginary parts. * * @param real Real part. * @param imaginary Imaginary part. */ public Complex(double real, double imaginary) { this.real = real; this.imaginary = imaginary; isNaN = Double.isNaN(real) || Double.isNaN(imaginary); isInfinite = !isNaN && (Double.isInfinite(real) || Double.isInfinite(imaginary)); } /** * Return the absolute value of this complex number. * Returns {@code NaN} if either real or imaginary part is {@code NaN} * and {@code Double.POSITIVE_INFINITY} if neither part is {@code NaN}, * but at least one part is infinite. * * @return the absolute value. */ public double abs() { if (isNaN) { return Double.NaN; } if (isInfinite()) { return Double.POSITIVE_INFINITY; } if (FastMath.abs(real) < FastMath.abs(imaginary)) { if (imaginary == 0.0) { return FastMath.abs(real); } double q = real / imaginary; return FastMath.abs(imaginary) * FastMath.sqrt(1 + q * q); } else { if (real == 0.0) { return FastMath.abs(imaginary); } double q = imaginary / real; return FastMath.abs(real) * FastMath.sqrt(1 + q * q); } } /** * Returns a {@code Complex} whose value is * {@code (this + addend)}. * Uses the definitional formula *
                                 *  
                                 *   (a + bi) + (c + di) = (a+c) + (b+d)i
                                 *  
                                 * 
                            *
                            * If either {@code this} or {@code addend} has a {@code NaN} value in * either part, {@link #NaN} is returned; otherwise {@code Infinite} * and {@code NaN} values are returned in the parts of the result * according to the rules for {@link java.lang.Double} arithmetic. * * @param addend Value to be added to this {@code Complex}. * @return {@code this + addend}. * @throws NullArgumentException if {@code addend} is {@code null}. */ public Complex add(Complex addend) throws NullArgumentException { MathUtils.checkNotNull(addend); if (isNaN || addend.isNaN) { return NaN; } return createComplex(real + addend.getReal(), imaginary + addend.getImaginary()); } /** * Returns a {@code Complex} whose value is {@code (this + addend)}, * with {@code addend} interpreted as a real number. * * @param addend Value to be added to this {@code Complex}. * @return {@code this + addend}. * @see #add(Complex) */ public Complex add(double addend) { if (isNaN || Double.isNaN(addend)) { return NaN; } return createComplex(real + addend, imaginary); } /** * Return the conjugate of this complex number. * The conjugate of {@code a + bi} is {@code a - bi}. *
                            * {@link #NaN} is returned if either the real or imaginary * part of this Complex number equals {@code Double.NaN}. *
                            * If the imaginary part is infinite, and the real part is not * {@code NaN}, the returned value has infinite imaginary part * of the opposite sign, e.g. the conjugate of * {@code 1 + POSITIVE_INFINITY i} is {@code 1 - NEGATIVE_INFINITY i}. * * @return the conjugate of this Complex object. */ public Complex conjugate() { if (isNaN) { return NaN; } return createComplex(real, -imaginary); } /** * Returns a {@code Complex} whose value is * {@code (this / divisor)}. * Implements the definitional formula *
                                 *  
                                 *    a + bi          ac + bd + (bc - ad)i
                                 *    ----------- = -------------------------
                                 *    c + di         c2 + d2
                                 *  
                                 * 
                            * but uses * * prescaling of operands to limit the effects of overflows and * underflows in the computation. *
                            * {@code Infinite} and {@code NaN} values are handled according to the * following rules, applied in the order presented: *
                              *
                            • If either {@code this} or {@code divisor} has a {@code NaN} value * in either part, {@link #NaN} is returned. *
                            • *
                            • If {@code divisor} equals {@link #ZERO}, {@link #NaN} is returned. *
                            • *
                            • If {@code this} and {@code divisor} are both infinite, * {@link #NaN} is returned. *
                            • *
                            • If {@code this} is finite (i.e., has no {@code Infinite} or * {@code NaN} parts) and {@code divisor} is infinite (one or both parts * infinite), {@link #ZERO} is returned. *
                            • *
                            • If {@code this} is infinite and {@code divisor} is finite, * {@code NaN} values are returned in the parts of the result if the * {@link java.lang.Double} rules applied to the definitional formula * force {@code NaN} results. *
                            • *
                            * * @param divisor Value by which this {@code Complex} is to be divided. * @return {@code this / divisor}. * @throws NullArgumentException if {@code divisor} is {@code null}. */ public Complex divide(Complex divisor) throws NullArgumentException { MathUtils.checkNotNull(divisor); if (isNaN || divisor.isNaN) { return NaN; } final double c = divisor.getReal(); final double d = divisor.getImaginary(); if (c == 0.0 && d == 0.0) { return NaN; } if (divisor.isInfinite() && !isInfinite()) { return ZERO; } if (FastMath.abs(c) < FastMath.abs(d)) { double q = c / d; double denominator = c * q + d; return createComplex((real * q + imaginary) / denominator, (imaginary * q - real) / denominator); } else { double q = d / c; double denominator = d * q + c; return createComplex((imaginary * q + real) / denominator, (imaginary - real * q) / denominator); } } /** * Returns a {@code Complex} whose value is {@code (this / divisor)}, * with {@code divisor} interpreted as a real number. * * @param divisor Value by which this {@code Complex} is to be divided. * @return {@code this / divisor}. * @see #divide(Complex) */ public Complex divide(double divisor) { if (isNaN || Double.isNaN(divisor)) { return NaN; } if (divisor == 0d) { return NaN; } if (Double.isInfinite(divisor)) { return !isInfinite() ? ZERO : NaN; } return createComplex(real / divisor, imaginary / divisor); } /** {@inheritDoc} */ public Complex reciprocal() { if (isNaN) { return NaN; } if (real == 0.0 && imaginary == 0.0) { return INF; } if (isInfinite) { return ZERO; } if (FastMath.abs(real) < FastMath.abs(imaginary)) { double q = real / imaginary; double scale = 1. / (real * q + imaginary); return createComplex(scale * q, -scale); } else { double q = imaginary / real; double scale = 1. / (imaginary * q + real); return createComplex(scale, -scale * q); } } /** * Test for the equality of two Complex objects. * If both the real and imaginary parts of two complex numbers * are exactly the same, and neither is {@code Double.NaN}, the two * Complex objects are considered to be equal. * All {@code NaN} values are considered to be equal - i.e, if either * (or both) real and imaginary parts of the complex number are equal * to {@code Double.NaN}, the complex number is equal to * {@code NaN}. * * @param other Object to test for equality to this * @return true if two Complex objects are equal, false if object is * {@code null}, not an instance of Complex, or not equal to this Complex * instance. */ @Override public boolean equals(Object other) { if (this == other) { return true; } if (other instanceof Complex){ Complex c = (Complex)other; if (c.isNaN) { return isNaN; } else { return (real == c.real) && (imaginary == c.imaginary); } } return false; } /** * Get a hashCode for the complex number. * Any {@code Double.NaN} value in real or imaginary part produces * the same hash code {@code 7}. * * @return a hash code value for this object. */ @Override public int hashCode() { if (isNaN) { return 7; } return 37 * (17 * MathUtils.hash(imaginary) + MathUtils.hash(real)); } /** * Access the imaginary part. * * @return the imaginary part. */ public double getImaginary() { return imaginary; } /** * Access the real part. * * @return the real part. */ public double getReal() { return real; } /** * Checks whether either or both parts of this complex number is * {@code NaN}. * * @return true if either or both parts of this complex number is * {@code NaN}; false otherwise. */ public boolean isNaN() { return isNaN; } /** * Checks whether either the real or imaginary part of this complex number * takes an infinite value (either {@code Double.POSITIVE_INFINITY} or * {@code Double.NEGATIVE_INFINITY}) and neither part * is {@code NaN}. * * @return true if one or both parts of this complex number are infinite * and neither part is {@code NaN}. */ public boolean isInfinite() { return isInfinite; } /** * Returns a {@code Complex} whose value is {@code this * factor}. * Implements preliminary checks for {@code NaN} and infinity followed by * the definitional formula: *
                                 *  
                                 *   (a + bi)(c + di) = (ac - bd) + (ad + bc)i
                                 *  
                                 * 
                            * Returns {@link #NaN} if either {@code this} or {@code factor} has one or * more {@code NaN} parts. *
                            * Returns {@link #INF} if neither {@code this} nor {@code factor} has one * or more {@code NaN} parts and if either {@code this} or {@code factor} * has one or more infinite parts (same result is returned regardless of * the sign of the components). *
                            * Returns finite values in components of the result per the definitional * formula in all remaining cases. * * @param factor value to be multiplied by this {@code Complex}. * @return {@code this * factor}. * @throws NullArgumentException if {@code factor} is {@code null}. */ public Complex multiply(Complex factor) throws NullArgumentException { MathUtils.checkNotNull(factor); if (isNaN || factor.isNaN) { return NaN; } if (Double.isInfinite(real) || Double.isInfinite(imaginary) || Double.isInfinite(factor.real) || Double.isInfinite(factor.imaginary)) { // we don't use isInfinite() to avoid testing for NaN again return INF; } return createComplex(real * factor.real - imaginary * factor.imaginary, real * factor.imaginary + imaginary * factor.real); } /** * Returns a {@code Complex} whose value is {@code this * factor}, with {@code factor} * interpreted as a integer number. * * @param factor value to be multiplied by this {@code Complex}. * @return {@code this * factor}. * @see #multiply(Complex) */ public Complex multiply(final int factor) { if (isNaN) { return NaN; } if (Double.isInfinite(real) || Double.isInfinite(imaginary)) { return INF; } return createComplex(real * factor, imaginary * factor); } /** * Returns a {@code Complex} whose value is {@code this * factor}, with {@code factor} * interpreted as a real number. * * @param factor value to be multiplied by this {@code Complex}. * @return {@code this * factor}. * @see #multiply(Complex) */ public Complex multiply(double factor) { if (isNaN || Double.isNaN(factor)) { return NaN; } if (Double.isInfinite(real) || Double.isInfinite(imaginary) || Double.isInfinite(factor)) { // we don't use isInfinite() to avoid testing for NaN again return INF; } return createComplex(real * factor, imaginary * factor); } /** * Returns a {@code Complex} whose value is {@code (-this)}. * Returns {@code NaN} if either real or imaginary * part of this Complex number equals {@code Double.NaN}. * * @return {@code -this}. */ public Complex negate() { if (isNaN) { return NaN; } return createComplex(-real, -imaginary); } /** * Returns a {@code Complex} whose value is * {@code (this - subtrahend)}. * Uses the definitional formula *
                                 *  
                                 *   (a + bi) - (c + di) = (a-c) + (b-d)i
                                 *  
                                 * 
                            * If either {@code this} or {@code subtrahend} has a {@code NaN]} value in either part, * {@link #NaN} is returned; otherwise infinite and {@code NaN} values are * returned in the parts of the result according to the rules for * {@link java.lang.Double} arithmetic. * * @param subtrahend value to be subtracted from this {@code Complex}. * @return {@code this - subtrahend}. * @throws NullArgumentException if {@code subtrahend} is {@code null}. */ public Complex subtract(Complex subtrahend) throws NullArgumentException { MathUtils.checkNotNull(subtrahend); if (isNaN || subtrahend.isNaN) { return NaN; } return createComplex(real - subtrahend.getReal(), imaginary - subtrahend.getImaginary()); } /** * Returns a {@code Complex} whose value is * {@code (this - subtrahend)}. * * @param subtrahend value to be subtracted from this {@code Complex}. * @return {@code this - subtrahend}. * @see #subtract(Complex) */ public Complex subtract(double subtrahend) { if (isNaN || Double.isNaN(subtrahend)) { return NaN; } return createComplex(real - subtrahend, imaginary); } /** * Compute the * * inverse cosine of this complex number. * Implements the formula: *
                                 *  
                                 *   acos(z) = -i (log(z + i (sqrt(1 - z2))))
                                 *  
                                 * 
                            * Returns {@link Complex#NaN} if either real or imaginary part of the * input argument is {@code NaN} or infinite. * * @return the inverse cosine of this complex number. * @since 1.2 */ public Complex acos() { if (isNaN) { return NaN; } return this.add(this.sqrt1z().multiply(I)).log().multiply(I.negate()); } /** * Compute the * * inverse sine of this complex number. * Implements the formula: *
                                 *  
                                 *   asin(z) = -i (log(sqrt(1 - z2) + iz))
                                 *  
                                 * 
                            * Returns {@link Complex#NaN} if either real or imaginary part of the * input argument is {@code NaN} or infinite. * * @return the inverse sine of this complex number. * @since 1.2 */ public Complex asin() { if (isNaN) { return NaN; } return sqrt1z().add(this.multiply(I)).log().multiply(I.negate()); } /** * Compute the * * inverse tangent of this complex number. * Implements the formula: *
                                 *  
                                 *   atan(z) = (i/2) log((i + z)/(i - z))
                                 *  
                                 * 
                            * Returns {@link Complex#NaN} if either real or imaginary part of the * input argument is {@code NaN} or infinite. * * @return the inverse tangent of this complex number * @since 1.2 */ public Complex atan() { if (isNaN) { return NaN; } return this.add(I).divide(I.subtract(this)).log() .multiply(I.divide(createComplex(2.0, 0.0))); } /** * Compute the * * cosine * of this complex number. * Implements the formula: *
                                 *  
                                 *   cos(a + bi) = cos(a)cosh(b) - sin(a)sinh(b)i
                                 *  
                                 * 
                            * where the (real) functions on the right-hand side are * {@link java.lang.Math#sin}, {@link java.lang.Math#cos}, * {@link FastMath#cosh} and {@link FastMath#sinh}. *
                            * Returns {@link Complex#NaN} if either real or imaginary part of the * input argument is {@code NaN}. *
                            * Infinite values in real or imaginary parts of the input may result in * infinite or NaN values returned in parts of the result. *
                                 *  Examples:
                                 *  
                                 *   cos(1 ± INFINITY i) = 1 ∓ INFINITY i
                                 *   cos(±INFINITY + i) = NaN + NaN i
                                 *   cos(±INFINITY ± INFINITY i) = NaN + NaN i
                                 *  
                                 * 
                            * * @return the cosine of this complex number. * @since 1.2 */ public Complex cos() { if (isNaN) { return NaN; } return createComplex(FastMath.cos(real) * FastMath.cosh(imaginary), -FastMath.sin(real) * FastMath.sinh(imaginary)); } /** * Compute the * * hyperbolic cosine of this complex number. * Implements the formula: *
                                 *  
                                 *   cosh(a + bi) = cosh(a)cos(b) + sinh(a)sin(b)i}
                                 *  
                                 * 
                            * where the (real) functions on the right-hand side are * {@link java.lang.Math#sin}, {@link java.lang.Math#cos}, * {@link FastMath#cosh} and {@link FastMath#sinh}. *
                            * Returns {@link Complex#NaN} if either real or imaginary part of the * input argument is {@code NaN}. *
                            * Infinite values in real or imaginary parts of the input may result in * infinite or NaN values returned in parts of the result. *
                                 *  Examples:
                                 *  
                                 *   cosh(1 ± INFINITY i) = NaN + NaN i
                                 *   cosh(±INFINITY + i) = INFINITY ± INFINITY i
                                 *   cosh(±INFINITY ± INFINITY i) = NaN + NaN i
                                 *  
                                 * 
                            * * @return the hyperbolic cosine of this complex number. * @since 1.2 */ public Complex cosh() { if (isNaN) { return NaN; } return createComplex(FastMath.cosh(real) * FastMath.cos(imaginary), FastMath.sinh(real) * FastMath.sin(imaginary)); } /** * Compute the * * exponential function of this complex number. * Implements the formula: *
                                 *  
                                 *   exp(a + bi) = exp(a)cos(b) + exp(a)sin(b)i
                                 *  
                                 * 
                            * where the (real) functions on the right-hand side are * {@link java.lang.Math#exp}, {@link java.lang.Math#cos}, and * {@link java.lang.Math#sin}. *
                            * Returns {@link Complex#NaN} if either real or imaginary part of the * input argument is {@code NaN}. *
                            * Infinite values in real or imaginary parts of the input may result in * infinite or NaN values returned in parts of the result. *
                                 *  Examples:
                                 *  
                                 *   exp(1 ± INFINITY i) = NaN + NaN i
                                 *   exp(INFINITY + i) = INFINITY + INFINITY i
                                 *   exp(-INFINITY + i) = 0 + 0i
                                 *   exp(±INFINITY ± INFINITY i) = NaN + NaN i
                                 *  
                                 * 
                            * * @return ethis. * @since 1.2 */ public Complex exp() { if (isNaN) { return NaN; } double expReal = FastMath.exp(real); return createComplex(expReal * FastMath.cos(imaginary), expReal * FastMath.sin(imaginary)); } /** * Compute the * * natural logarithm of this complex number. * Implements the formula: *
                                 *  
                                 *   log(a + bi) = ln(|a + bi|) + arg(a + bi)i
                                 *  
                                 * 
                            * where ln on the right hand side is {@link java.lang.Math#log}, * {@code |a + bi|} is the modulus, {@link Complex#abs}, and * {@code arg(a + bi) = }{@link java.lang.Math#atan2}(b, a). *
                            * Returns {@link Complex#NaN} if either real or imaginary part of the * input argument is {@code NaN}. *
                            * Infinite (or critical) values in real or imaginary parts of the input may * result in infinite or NaN values returned in parts of the result. *
                                 *  Examples:
                                 *  
                                 *   log(1 ± INFINITY i) = INFINITY ± (π/2)i
                                 *   log(INFINITY + i) = INFINITY + 0i
                                 *   log(-INFINITY + i) = INFINITY + πi
                                 *   log(INFINITY ± INFINITY i) = INFINITY ± (π/4)i
                                 *   log(-INFINITY ± INFINITY i) = INFINITY ± (3π/4)i
                                 *   log(0 + 0i) = -INFINITY + 0i
                                 *  
                                 * 
                            * * @return the value ln   this, the natural logarithm * of {@code this}. * @since 1.2 */ public Complex log() { if (isNaN) { return NaN; } return createComplex(FastMath.log(abs()), FastMath.atan2(imaginary, real)); } /** * Returns of value of this complex number raised to the power of {@code x}. * Implements the formula: *
                                 *  
                                 *   yx = exp(x·log(y))
                                 *  
                                 * 
                            * where {@code exp} and {@code log} are {@link #exp} and * {@link #log}, respectively. *
                            * Returns {@link Complex#NaN} if either real or imaginary part of the * input argument is {@code NaN} or infinite, or if {@code y} * equals {@link Complex#ZERO}. * * @param x exponent to which this {@code Complex} is to be raised. * @return this{@code x}. * @throws NullArgumentException if x is {@code null}. * @since 1.2 */ public Complex pow(Complex x) throws NullArgumentException { MathUtils.checkNotNull(x); return this.log().multiply(x).exp(); } /** * Returns of value of this complex number raised to the power of {@code x}. * * @param x exponent to which this {@code Complex} is to be raised. * @return thisx. * @see #pow(Complex) */ public Complex pow(double x) { return this.log().multiply(x).exp(); } /** * Compute the * * sine * of this complex number. * Implements the formula: *
                                 *  
                                 *   sin(a + bi) = sin(a)cosh(b) - cos(a)sinh(b)i
                                 *  
                                 * 
                            * where the (real) functions on the right-hand side are * {@link java.lang.Math#sin}, {@link java.lang.Math#cos}, * {@link FastMath#cosh} and {@link FastMath#sinh}. *
                            * Returns {@link Complex#NaN} if either real or imaginary part of the * input argument is {@code NaN}. *
                            * Infinite values in real or imaginary parts of the input may result in * infinite or {@code NaN} values returned in parts of the result. *
                                 *  Examples:
                                 *  
                                 *   sin(1 ± INFINITY i) = 1 ± INFINITY i
                                 *   sin(±INFINITY + i) = NaN + NaN i
                                 *   sin(±INFINITY ± INFINITY i) = NaN + NaN i
                                 *  
                                 * 
                            * * @return the sine of this complex number. * @since 1.2 */ public Complex sin() { if (isNaN) { return NaN; } return createComplex(FastMath.sin(real) * FastMath.cosh(imaginary), FastMath.cos(real) * FastMath.sinh(imaginary)); } /** * Compute the * * hyperbolic sine of this complex number. * Implements the formula: *
                                 *  
                                 *   sinh(a + bi) = sinh(a)cos(b)) + cosh(a)sin(b)i
                                 *  
                                 * 
                            * where the (real) functions on the right-hand side are * {@link java.lang.Math#sin}, {@link java.lang.Math#cos}, * {@link FastMath#cosh} and {@link FastMath#sinh}. *
                            * Returns {@link Complex#NaN} if either real or imaginary part of the * input argument is {@code NaN}. *
                            * Infinite values in real or imaginary parts of the input may result in * infinite or NaN values returned in parts of the result. *
                                 *  Examples:
                                 *  
                                 *   sinh(1 ± INFINITY i) = NaN + NaN i
                                 *   sinh(±INFINITY + i) = ± INFINITY + INFINITY i
                                 *   sinh(±INFINITY ± INFINITY i) = NaN + NaN i
                                 *  
                                 * 
                            * * @return the hyperbolic sine of {@code this}. * @since 1.2 */ public Complex sinh() { if (isNaN) { return NaN; } return createComplex(FastMath.sinh(real) * FastMath.cos(imaginary), FastMath.cosh(real) * FastMath.sin(imaginary)); } /** * Compute the * * square root of this complex number. * Implements the following algorithm to compute {@code sqrt(a + bi)}: *
                            1. Let {@code t = sqrt((|a| + |a + bi|) / 2)}
                            2. *
                            3. if {@code  a ≥ 0} return {@code t + (b/2t)i}
                                   *  else return {@code |b|/2t + sign(b)t i }
                            4. *
                            * where
                              *
                            • {@code |a| = }{@link Math#abs}(a)
                            • *
                            • {@code |a + bi| = }{@link Complex#abs}(a + bi)
                            • *
                            • {@code sign(b) = }{@link FastMath#copySign(double,double) copySign(1d, b)} *
                            *
                            * Returns {@link Complex#NaN} if either real or imaginary part of the * input argument is {@code NaN}. *
                            * Infinite values in real or imaginary parts of the input may result in * infinite or NaN values returned in parts of the result. *
                                 *  Examples:
                                 *  
                                 *   sqrt(1 ± INFINITY i) = INFINITY + NaN i
                                 *   sqrt(INFINITY + i) = INFINITY + 0i
                                 *   sqrt(-INFINITY + i) = 0 + INFINITY i
                                 *   sqrt(INFINITY ± INFINITY i) = INFINITY + NaN i
                                 *   sqrt(-INFINITY ± INFINITY i) = NaN ± INFINITY i
                                 *  
                                 * 
                            * * @return the square root of {@code this}. * @since 1.2 */ public Complex sqrt() { if (isNaN) { return NaN; } if (real == 0.0 && imaginary == 0.0) { return createComplex(0.0, 0.0); } double t = FastMath.sqrt((FastMath.abs(real) + abs()) / 2.0); if (real >= 0.0) { return createComplex(t, imaginary / (2.0 * t)); } else { return createComplex(FastMath.abs(imaginary) / (2.0 * t), FastMath.copySign(1d, imaginary) * t); } } /** * Compute the * * square root of 1 - this2 for this complex * number. * Computes the result directly as * {@code sqrt(ONE.subtract(z.multiply(z)))}. *
                            * Returns {@link Complex#NaN} if either real or imaginary part of the * input argument is {@code NaN}. *
                            * Infinite values in real or imaginary parts of the input may result in * infinite or NaN values returned in parts of the result. * * @return the square root of 1 - this2. * @since 1.2 */ public Complex sqrt1z() { return createComplex(1.0, 0.0).subtract(this.multiply(this)).sqrt(); } /** * Compute the * * tangent of this complex number. * Implements the formula: *
                                 *  
                                 *   tan(a + bi) = sin(2a)/(cos(2a)+cosh(2b)) + [sinh(2b)/(cos(2a)+cosh(2b))]i
                                 *  
                                 * 
                            * where the (real) functions on the right-hand side are * {@link FastMath#sin}, {@link FastMath#cos}, {@link FastMath#cosh} and * {@link FastMath#sinh}. *
                            * Returns {@link Complex#NaN} if either real or imaginary part of the * input argument is {@code NaN}. *
                            * Infinite (or critical) values in real or imaginary parts of the input may * result in infinite or NaN values returned in parts of the result. *
                                 *  Examples:
                                 *  
                                 *   tan(a ± INFINITY i) = 0 ± i
                                 *   tan(±INFINITY + bi) = NaN + NaN i
                                 *   tan(±INFINITY ± INFINITY i) = NaN + NaN i
                                 *   tan(±π/2 + 0 i) = ±INFINITY + NaN i
                                 *  
                                 * 
                            * * @return the tangent of {@code this}. * @since 1.2 */ public Complex tan() { if (isNaN || Double.isInfinite(real)) { return NaN; } if (imaginary > 20.0) { return createComplex(0.0, 1.0); } if (imaginary < -20.0) { return createComplex(0.0, -1.0); } double real2 = 2.0 * real; double imaginary2 = 2.0 * imaginary; double d = FastMath.cos(real2) + FastMath.cosh(imaginary2); return createComplex(FastMath.sin(real2) / d, FastMath.sinh(imaginary2) / d); } /** * Compute the * * hyperbolic tangent of this complex number. * Implements the formula: *
                                 *  
                                 *   tan(a + bi) = sinh(2a)/(cosh(2a)+cos(2b)) + [sin(2b)/(cosh(2a)+cos(2b))]i
                                 *  
                                 * 
                            * where the (real) functions on the right-hand side are * {@link FastMath#sin}, {@link FastMath#cos}, {@link FastMath#cosh} and * {@link FastMath#sinh}. *
                            * Returns {@link Complex#NaN} if either real or imaginary part of the * input argument is {@code NaN}. *
                            * Infinite values in real or imaginary parts of the input may result in * infinite or NaN values returned in parts of the result. *
                                 *  Examples:
                                 *  
                                 *   tanh(a ± INFINITY i) = NaN + NaN i
                                 *   tanh(±INFINITY + bi) = ±1 + 0 i
                                 *   tanh(±INFINITY ± INFINITY i) = NaN + NaN i
                                 *   tanh(0 + (π/2)i) = NaN + INFINITY i
                                 *  
                                 * 
                            * * @return the hyperbolic tangent of {@code this}. * @since 1.2 */ public Complex tanh() { if (isNaN || Double.isInfinite(imaginary)) { return NaN; } if (real > 20.0) { return createComplex(1.0, 0.0); } if (real < -20.0) { return createComplex(-1.0, 0.0); } double real2 = 2.0 * real; double imaginary2 = 2.0 * imaginary; double d = FastMath.cosh(real2) + FastMath.cos(imaginary2); return createComplex(FastMath.sinh(real2) / d, FastMath.sin(imaginary2) / d); } /** * Compute the argument of this complex number. * The argument is the angle phi between the positive real axis and * the point representing this number in the complex plane. * The value returned is between -PI (not inclusive) * and PI (inclusive), with negative values returned for numbers with * negative imaginary parts. *
                            * If either real or imaginary part (or both) is NaN, NaN is returned. * Infinite parts are handled as {@code Math.atan2} handles them, * essentially treating finite parts as zero in the presence of an * infinite coordinate and returning a multiple of pi/4 depending on * the signs of the infinite parts. * See the javadoc for {@code Math.atan2} for full details. * * @return the argument of {@code this}. */ public double getArgument() { return FastMath.atan2(getImaginary(), getReal()); } /** * Computes the n-th roots of this complex number. * The nth roots are defined by the formula: *
                                 *  
                                 *   zk = abs1/n (cos(phi + 2πk/n) + i (sin(phi + 2πk/n))
                                 *  
                                 * 
                            * for {@code k=0, 1, ..., n-1}, where {@code abs} and {@code phi} * are respectively the {@link #abs() modulus} and * {@link #getArgument() argument} of this complex number. *
                            * If one or both parts of this complex number is NaN, a list with just * one element, {@link #NaN} is returned. * if neither part is NaN, but at least one part is infinite, the result * is a one-element list containing {@link #INF}. * * @param n Degree of root. * @return a List of all {@code n}-th roots of {@code this}. * @throws NotPositiveException if {@code n <= 0}. * @since 2.0 */ public List nthRoot(int n) throws NotPositiveException { if (n <= 0) { throw new NotPositiveException(LocalizedFormats.CANNOT_COMPUTE_NTH_ROOT_FOR_NEGATIVE_N, n); } final List result = new ArrayList(); if (isNaN) { result.add(NaN); return result; } if (isInfinite()) { result.add(INF); return result; } // nth root of abs -- faster / more accurate to use a solver here? final double nthRootOfAbs = FastMath.pow(abs(), 1.0 / n); // Compute nth roots of complex number with k = 0, 1, ... n-1 final double nthPhi = getArgument() / n; final double slice = 2 * FastMath.PI / n; double innerPart = nthPhi; for (int k = 0; k < n ; k++) { // inner part final double realPart = nthRootOfAbs * FastMath.cos(innerPart); final double imaginaryPart = nthRootOfAbs * FastMath.sin(innerPart); result.add(createComplex(realPart, imaginaryPart)); innerPart += slice; } return result; } /** * Create a complex number given the real and imaginary parts. * * @param realPart Real part. * @param imaginaryPart Imaginary part. * @return a new complex number instance. * @since 1.2 * @see #valueOf(double, double) */ protected Complex createComplex(double realPart, double imaginaryPart) { return new Complex(realPart, imaginaryPart); } /** * Create a complex number given the real and imaginary parts. * * @param realPart Real part. * @param imaginaryPart Imaginary part. * @return a Complex instance. */ public static Complex valueOf(double realPart, double imaginaryPart) { if (Double.isNaN(realPart) || Double.isNaN(imaginaryPart)) { return NaN; } return new Complex(realPart, imaginaryPart); } /** * Create a complex number given only the real part. * * @param realPart Real part. * @return a Complex instance. */ public static Complex valueOf(double realPart) { if (Double.isNaN(realPart)) { return NaN; } return new Complex(realPart); } /** * Resolve the transient fields in a deserialized Complex Object. * Subclasses will need to override {@link #createComplex} to * deserialize properly. * * @return A Complex instance with all fields resolved. * @since 2.0 */ protected final Object readResolve() { return createComplex(real, imaginary); } /** {@inheritDoc} */ public ComplexField getField() { return ComplexField.getInstance(); } /** {@inheritDoc} */ @Override public String toString() { return "(" + real + ", " + imaginary + ")"; } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/complex/ComplexField.java100644 1750 1750 4715 12126627716 27422 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.complex; import java.io.Serializable; import org.apache.commons.math3.Field; import org.apache.commons.math3.FieldElement; /** * Representation of the complex numbers field. *

                            * This class is a singleton. *

                            * @see Complex * @version $Id: ComplexField.java 1416643 2012-12-03 19:37:14Z tn $ * @since 2.0 */ public class ComplexField implements Field, Serializable { /** Serializable version identifier. */ private static final long serialVersionUID = -6130362688700788798L; /** Private constructor for the singleton. */ private ComplexField() { } /** Get the unique instance. * @return the unique instance */ public static ComplexField getInstance() { return LazyHolder.INSTANCE; } /** {@inheritDoc} */ public Complex getOne() { return Complex.ONE; } /** {@inheritDoc} */ public Complex getZero() { return Complex.ZERO; } /** {@inheritDoc} */ public Class> getRuntimeClass() { return Complex.class; } // CHECKSTYLE: stop HideUtilityClassConstructor /** Holder for the instance. *

                            We use here the Initialization On Demand Holder Idiom.

                            */ private static class LazyHolder { /** Cached field instance. */ private static final ComplexField INSTANCE = new ComplexField(); } // CHECKSTYLE: resume HideUtilityClassConstructor /** Handle deserialization of the singleton. * @return the singleton instance */ private Object readResolve() { // return the singleton instance return LazyHolder.INSTANCE; } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/complex/ComplexFormat.java100644 1750 1750 37345 12126627716 27654 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.complex; import java.text.FieldPosition; import java.text.NumberFormat; import java.text.ParsePosition; import java.util.Locale; import org.apache.commons.math3.exception.MathIllegalArgumentException; import org.apache.commons.math3.exception.MathParseException; import org.apache.commons.math3.exception.NoDataException; import org.apache.commons.math3.exception.NullArgumentException; import org.apache.commons.math3.exception.util.LocalizedFormats; import org.apache.commons.math3.util.CompositeFormat; /** * Formats a Complex number in cartesian format "Re(c) + Im(c)i". 'i' can * be replaced with 'j' (or anything else), and the number format for both real * and imaginary parts can be configured. * * @version $Id: ComplexFormat.java 1416643 2012-12-03 19:37:14Z tn $ */ public class ComplexFormat { /** The default imaginary character. */ private static final String DEFAULT_IMAGINARY_CHARACTER = "i"; /** The notation used to signify the imaginary part of the complex number. */ private final String imaginaryCharacter; /** The format used for the imaginary part. */ private final NumberFormat imaginaryFormat; /** The format used for the real part. */ private final NumberFormat realFormat; /** * Create an instance with the default imaginary character, 'i', and the * default number format for both real and imaginary parts. */ public ComplexFormat() { this.imaginaryCharacter = DEFAULT_IMAGINARY_CHARACTER; this.imaginaryFormat = CompositeFormat.getDefaultNumberFormat(); this.realFormat = imaginaryFormat; } /** * Create an instance with a custom number format for both real and * imaginary parts. * @param format the custom format for both real and imaginary parts. * @throws NullArgumentException if {@code realFormat} is {@code null}. */ public ComplexFormat(NumberFormat format) throws NullArgumentException { if (format == null) { throw new NullArgumentException(LocalizedFormats.IMAGINARY_FORMAT); } this.imaginaryCharacter = DEFAULT_IMAGINARY_CHARACTER; this.imaginaryFormat = format; this.realFormat = format; } /** * Create an instance with a custom number format for the real part and a * custom number format for the imaginary part. * @param realFormat the custom format for the real part. * @param imaginaryFormat the custom format for the imaginary part. * @throws NullArgumentException if {@code imaginaryFormat} is {@code null}. * @throws NullArgumentException if {@code realFormat} is {@code null}. */ public ComplexFormat(NumberFormat realFormat, NumberFormat imaginaryFormat) throws NullArgumentException { if (imaginaryFormat == null) { throw new NullArgumentException(LocalizedFormats.IMAGINARY_FORMAT); } if (realFormat == null) { throw new NullArgumentException(LocalizedFormats.REAL_FORMAT); } this.imaginaryCharacter = DEFAULT_IMAGINARY_CHARACTER; this.imaginaryFormat = imaginaryFormat; this.realFormat = realFormat; } /** * Create an instance with a custom imaginary character, and the default * number format for both real and imaginary parts. * @param imaginaryCharacter The custom imaginary character. * @throws NullArgumentException if {@code imaginaryCharacter} is * {@code null}. * @throws NoDataException if {@code imaginaryCharacter} is an * empty string. */ public ComplexFormat(String imaginaryCharacter) throws NullArgumentException, NoDataException { this(imaginaryCharacter, CompositeFormat.getDefaultNumberFormat()); } /** * Create an instance with a custom imaginary character, and a custom number * format for both real and imaginary parts. * @param imaginaryCharacter The custom imaginary character. * @param format the custom format for both real and imaginary parts. * @throws NullArgumentException if {@code imaginaryCharacter} is * {@code null}. * @throws NoDataException if {@code imaginaryCharacter} is an * empty string. * @throws NullArgumentException if {@code format} is {@code null}. */ public ComplexFormat(String imaginaryCharacter, NumberFormat format) throws NullArgumentException, NoDataException { this(imaginaryCharacter, format, format); } /** * Create an instance with a custom imaginary character, a custom number * format for the real part, and a custom number format for the imaginary * part. * * @param imaginaryCharacter The custom imaginary character. * @param realFormat the custom format for the real part. * @param imaginaryFormat the custom format for the imaginary part. * @throws NullArgumentException if {@code imaginaryCharacter} is * {@code null}. * @throws NoDataException if {@code imaginaryCharacter} is an * empty string. * @throws NullArgumentException if {@code imaginaryFormat} is {@code null}. * @throws NullArgumentException if {@code realFormat} is {@code null}. */ public ComplexFormat(String imaginaryCharacter, NumberFormat realFormat, NumberFormat imaginaryFormat) throws NullArgumentException, NoDataException { if (imaginaryCharacter == null) { throw new NullArgumentException(); } if (imaginaryCharacter.length() == 0) { throw new NoDataException(); } if (imaginaryFormat == null) { throw new NullArgumentException(LocalizedFormats.IMAGINARY_FORMAT); } if (realFormat == null) { throw new NullArgumentException(LocalizedFormats.REAL_FORMAT); } this.imaginaryCharacter = imaginaryCharacter; this.imaginaryFormat = imaginaryFormat; this.realFormat = realFormat; } /** * Get the set of locales for which complex formats are available. *

                            This is the same set as the {@link NumberFormat} set.

                            * @return available complex format locales. */ public static Locale[] getAvailableLocales() { return NumberFormat.getAvailableLocales(); } /** * This method calls {@link #format(Object,StringBuffer,FieldPosition)}. * * @param c Complex object to format. * @return A formatted number in the form "Re(c) + Im(c)i". */ public String format(Complex c) { return format(c, new StringBuffer(), new FieldPosition(0)).toString(); } /** * This method calls {@link #format(Object,StringBuffer,FieldPosition)}. * * @param c Double object to format. * @return A formatted number. */ public String format(Double c) { return format(new Complex(c, 0), new StringBuffer(), new FieldPosition(0)).toString(); } /** * Formats a {@link Complex} object to produce a string. * * @param complex the object to format. * @param toAppendTo where the text is to be appended * @param pos On input: an alignment field, if desired. On output: the * offsets of the alignment field * @return the value passed in as toAppendTo. */ public StringBuffer format(Complex complex, StringBuffer toAppendTo, FieldPosition pos) { pos.setBeginIndex(0); pos.setEndIndex(0); // format real double re = complex.getReal(); CompositeFormat.formatDouble(re, getRealFormat(), toAppendTo, pos); // format sign and imaginary double im = complex.getImaginary(); StringBuffer imAppendTo; if (im < 0.0) { toAppendTo.append(" - "); imAppendTo = formatImaginary(-im, new StringBuffer(), pos); toAppendTo.append(imAppendTo); toAppendTo.append(getImaginaryCharacter()); } else if (im > 0.0 || Double.isNaN(im)) { toAppendTo.append(" + "); imAppendTo = formatImaginary(im, new StringBuffer(), pos); toAppendTo.append(imAppendTo); toAppendTo.append(getImaginaryCharacter()); } return toAppendTo; } /** * Format the absolute value of the imaginary part. * * @param absIm Absolute value of the imaginary part of a complex number. * @param toAppendTo where the text is to be appended. * @param pos On input: an alignment field, if desired. On output: the * offsets of the alignment field. * @return the value passed in as toAppendTo. */ private StringBuffer formatImaginary(double absIm, StringBuffer toAppendTo, FieldPosition pos) { pos.setBeginIndex(0); pos.setEndIndex(0); CompositeFormat.formatDouble(absIm, getImaginaryFormat(), toAppendTo, pos); if (toAppendTo.toString().equals("1")) { // Remove the character "1" if it is the only one. toAppendTo.setLength(0); } return toAppendTo; } /** * Formats a object to produce a string. {@code obj} must be either a * {@link Complex} object or a {@link Number} object. Any other type of * object will result in an {@link IllegalArgumentException} being thrown. * * @param obj the object to format. * @param toAppendTo where the text is to be appended * @param pos On input: an alignment field, if desired. On output: the * offsets of the alignment field * @return the value passed in as toAppendTo. * @see java.text.Format#format(java.lang.Object, java.lang.StringBuffer, java.text.FieldPosition) * @throws MathIllegalArgumentException is {@code obj} is not a valid type. */ public StringBuffer format(Object obj, StringBuffer toAppendTo, FieldPosition pos) throws MathIllegalArgumentException { StringBuffer ret = null; if (obj instanceof Complex) { ret = format( (Complex)obj, toAppendTo, pos); } else if (obj instanceof Number) { ret = format(new Complex(((Number)obj).doubleValue(), 0.0), toAppendTo, pos); } else { throw new MathIllegalArgumentException(LocalizedFormats.CANNOT_FORMAT_INSTANCE_AS_COMPLEX, obj.getClass().getName()); } return ret; } /** * Access the imaginaryCharacter. * @return the imaginaryCharacter. */ public String getImaginaryCharacter() { return imaginaryCharacter; } /** * Access the imaginaryFormat. * @return the imaginaryFormat. */ public NumberFormat getImaginaryFormat() { return imaginaryFormat; } /** * Returns the default complex format for the current locale. * @return the default complex format. */ public static ComplexFormat getInstance() { return getInstance(Locale.getDefault()); } /** * Returns the default complex format for the given locale. * @param locale the specific locale used by the format. * @return the complex format specific to the given locale. */ public static ComplexFormat getInstance(Locale locale) { NumberFormat f = CompositeFormat.getDefaultNumberFormat(locale); return new ComplexFormat(f); } /** * Returns the default complex format for the given locale. * @param locale the specific locale used by the format. * @param imaginaryCharacter Imaginary character. * @return the complex format specific to the given locale. * @throws NullArgumentException if {@code imaginaryCharacter} is * {@code null}. * @throws NoDataException if {@code imaginaryCharacter} is an * empty string. */ public static ComplexFormat getInstance(String imaginaryCharacter, Locale locale) throws NullArgumentException, NoDataException { NumberFormat f = CompositeFormat.getDefaultNumberFormat(locale); return new ComplexFormat(imaginaryCharacter, f); } /** * Access the realFormat. * @return the realFormat. */ public NumberFormat getRealFormat() { return realFormat; } /** * Parses a string to produce a {@link Complex} object. * * @param source the string to parse. * @return the parsed {@link Complex} object. * @throws MathParseException if the beginning of the specified string * cannot be parsed. */ public Complex parse(String source) throws MathParseException { ParsePosition parsePosition = new ParsePosition(0); Complex result = parse(source, parsePosition); if (parsePosition.getIndex() == 0) { throw new MathParseException(source, parsePosition.getErrorIndex(), Complex.class); } return result; } /** * Parses a string to produce a {@link Complex} object. * * @param source the string to parse * @param pos input/ouput parsing parameter. * @return the parsed {@link Complex} object. */ public Complex parse(String source, ParsePosition pos) { int initialIndex = pos.getIndex(); // parse whitespace CompositeFormat.parseAndIgnoreWhitespace(source, pos); // parse real Number re = CompositeFormat.parseNumber(source, getRealFormat(), pos); if (re == null) { // invalid real number // set index back to initial, error index should already be set pos.setIndex(initialIndex); return null; } // parse sign int startIndex = pos.getIndex(); char c = CompositeFormat.parseNextCharacter(source, pos); int sign = 0; switch (c) { case 0 : // no sign // return real only complex number return new Complex(re.doubleValue(), 0.0); case '-' : sign = -1; break; case '+' : sign = 1; break; default : // invalid sign // set index back to initial, error index should be the last // character examined. pos.setIndex(initialIndex); pos.setErrorIndex(startIndex); return null; } // parse whitespace CompositeFormat.parseAndIgnoreWhitespace(source, pos); // parse imaginary Number im = CompositeFormat.parseNumber(source, getRealFormat(), pos); if (im == null) { // invalid imaginary number // set index back to initial, error index should already be set pos.setIndex(initialIndex); return null; } // parse imaginary character if (!CompositeFormat.parseFixedstring(source, getImaginaryCharacter(), pos)) { return null; } return new Complex(re.doubleValue(), im.doubleValue() * sign); } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/complex/RootsOfUnity.java100644 1750 1750 17501 12126627716 27510 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.complex; import java.io.Serializable; import org.apache.commons.math3.exception.MathIllegalArgumentException; import org.apache.commons.math3.exception.MathIllegalStateException; import org.apache.commons.math3.exception.OutOfRangeException; import org.apache.commons.math3.exception.ZeroException; import org.apache.commons.math3.exception.util.LocalizedFormats; import org.apache.commons.math3.util.FastMath; /** * A helper class for the computation and caching of the {@code n}-th roots of * unity. * * @version $Id: RootsOfUnity.java 1416643 2012-12-03 19:37:14Z tn $ * @since 3.0 */ public class RootsOfUnity implements Serializable { /** Serializable version id. */ private static final long serialVersionUID = 20120201L; /** Number of roots of unity. */ private int omegaCount; /** Real part of the roots. */ private double[] omegaReal; /** * Imaginary part of the {@code n}-th roots of unity, for positive values * of {@code n}. In this array, the roots are stored in counter-clockwise * order. */ private double[] omegaImaginaryCounterClockwise; /** * Imaginary part of the {@code n}-th roots of unity, for negative values * of {@code n}. In this array, the roots are stored in clockwise order. */ private double[] omegaImaginaryClockwise; /** * {@code true} if {@link #computeRoots(int)} was called with a positive * value of its argument {@code n}. In this case, counter-clockwise ordering * of the roots of unity should be used. */ private boolean isCounterClockWise; /** * Build an engine for computing the {@code n}-th roots of unity. */ public RootsOfUnity() { omegaCount = 0; omegaReal = null; omegaImaginaryCounterClockwise = null; omegaImaginaryClockwise = null; isCounterClockWise = true; } /** * Returns {@code true} if {@link #computeRoots(int)} was called with a * positive value of its argument {@code n}. If {@code true}, then * counter-clockwise ordering of the roots of unity should be used. * * @return {@code true} if the roots of unity are stored in * counter-clockwise order * @throws MathIllegalStateException if no roots of unity have been computed * yet */ public synchronized boolean isCounterClockWise() throws MathIllegalStateException { if (omegaCount == 0) { throw new MathIllegalStateException( LocalizedFormats.ROOTS_OF_UNITY_NOT_COMPUTED_YET); } return isCounterClockWise; } /** *

                            * Computes the {@code n}-th roots of unity. The roots are stored in * {@code omega[]}, such that {@code omega[k] = w ^ k}, where * {@code k = 0, ..., n - 1}, {@code w = exp(2 * pi * i / n)} and * {@code i = sqrt(-1)}. *

                            *

                            * Note that {@code n} can be positive of negative *

                            *
                              *
                            • {@code abs(n)} is always the number of roots of unity.
                            • *
                            • If {@code n > 0}, then the roots are stored in counter-clockwise order.
                            • *
                            • If {@code n < 0}, then the roots are stored in clockwise order.

                              *
                            * * @param n the (signed) number of roots of unity to be computed * @throws ZeroException if {@code n = 0} */ public synchronized void computeRoots(int n) throws ZeroException { if (n == 0) { throw new ZeroException( LocalizedFormats.CANNOT_COMPUTE_0TH_ROOT_OF_UNITY); } isCounterClockWise = n > 0; // avoid repetitive calculations final int absN = FastMath.abs(n); if (absN == omegaCount) { return; } // calculate everything from scratch final double t = 2.0 * FastMath.PI / absN; final double cosT = FastMath.cos(t); final double sinT = FastMath.sin(t); omegaReal = new double[absN]; omegaImaginaryCounterClockwise = new double[absN]; omegaImaginaryClockwise = new double[absN]; omegaReal[0] = 1.0; omegaImaginaryCounterClockwise[0] = 0.0; omegaImaginaryClockwise[0] = 0.0; for (int i = 1; i < absN; i++) { omegaReal[i] = omegaReal[i - 1] * cosT - omegaImaginaryCounterClockwise[i - 1] * sinT; omegaImaginaryCounterClockwise[i] = omegaReal[i - 1] * sinT + omegaImaginaryCounterClockwise[i - 1] * cosT; omegaImaginaryClockwise[i] = -omegaImaginaryCounterClockwise[i]; } omegaCount = absN; } /** * Get the real part of the {@code k}-th {@code n}-th root of unity. * * @param k index of the {@code n}-th root of unity * @return real part of the {@code k}-th {@code n}-th root of unity * @throws MathIllegalStateException if no roots of unity have been * computed yet * @throws MathIllegalArgumentException if {@code k} is out of range */ public synchronized double getReal(int k) throws MathIllegalStateException, MathIllegalArgumentException { if (omegaCount == 0) { throw new MathIllegalStateException( LocalizedFormats.ROOTS_OF_UNITY_NOT_COMPUTED_YET); } if ((k < 0) || (k >= omegaCount)) { throw new OutOfRangeException( LocalizedFormats.OUT_OF_RANGE_ROOT_OF_UNITY_INDEX, Integer.valueOf(k), Integer.valueOf(0), Integer.valueOf(omegaCount - 1)); } return omegaReal[k]; } /** * Get the imaginary part of the {@code k}-th {@code n}-th root of unity. * * @param k index of the {@code n}-th root of unity * @return imaginary part of the {@code k}-th {@code n}-th root of unity * @throws MathIllegalStateException if no roots of unity have been * computed yet * @throws OutOfRangeException if {@code k} is out of range */ public synchronized double getImaginary(int k) throws MathIllegalStateException, OutOfRangeException { if (omegaCount == 0) { throw new MathIllegalStateException( LocalizedFormats.ROOTS_OF_UNITY_NOT_COMPUTED_YET); } if ((k < 0) || (k >= omegaCount)) { throw new OutOfRangeException( LocalizedFormats.OUT_OF_RANGE_ROOT_OF_UNITY_INDEX, Integer.valueOf(k), Integer.valueOf(0), Integer.valueOf(omegaCount - 1)); } return isCounterClockWise ? omegaImaginaryCounterClockwise[k] : omegaImaginaryClockwise[k]; } /** * Returns the number of roots of unity currently stored. If * {@link #computeRoots(int)} was called with {@code n}, then this method * returns {@code abs(n)}. If no roots of unity have been computed yet, this * method returns 0. * * @return the number of roots of unity currently stored */ public synchronized int getNumberOfRoots() { return omegaCount; } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/complex/ComplexUtils.java100644 1750 1750 6461 12126627716 27477 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.complex; import org.apache.commons.math3.exception.MathIllegalArgumentException; import org.apache.commons.math3.exception.util.LocalizedFormats; import org.apache.commons.math3.util.FastMath; /** * Static implementations of common * {@link org.apache.commons.math3.complex.Complex} utilities functions. * * @version $Id: ComplexUtils.java 1416643 2012-12-03 19:37:14Z tn $ */ public class ComplexUtils { /** * Default constructor. */ private ComplexUtils() {} /** * Creates a complex number from the given polar representation. *

                            * The value returned is r·ei·theta, * computed as r·cos(theta) + r·sin(theta)i

                            *

                            * If either r or theta is NaN, or * theta is infinite, {@link Complex#NaN} is returned.

                            *

                            * If r is infinite and theta is finite, * infinite or NaN values may be returned in parts of the result, following * the rules for double arithmetic.

                                 * Examples:
                                 * 
                                 * polar2Complex(INFINITY, π/4) = INFINITY + INFINITY i
                                 * polar2Complex(INFINITY, 0) = INFINITY + NaN i
                                 * polar2Complex(INFINITY, -π/4) = INFINITY - INFINITY i
                                 * polar2Complex(INFINITY, 5π/4) = -INFINITY - INFINITY i 

                            * * @param r the modulus of the complex number to create * @param theta the argument of the complex number to create * @return r·ei·theta * @throws MathIllegalArgumentException if {@code r} is negative. * @since 1.1 */ public static Complex polar2Complex(double r, double theta) throws MathIllegalArgumentException { if (r < 0) { throw new MathIllegalArgumentException( LocalizedFormats.NEGATIVE_COMPLEX_MODULE, r); } return new Complex(r * FastMath.cos(theta), r * FastMath.sin(theta)); } /** * Convert an array of primitive doubles to an array of {@code Complex} objects. * * @param real Array of numbers to be converted to their {@code Complex} * equivalent. * @return an array of {@code Complex} objects. * * @since 3.1 */ public static Complex[] convertToComplex(double[] real) { final Complex c[] = new Complex[real.length]; for (int i = 0; i < real.length; i++) { c[i] = new Complex(real[i], 0); } return c; } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/fitting/package-info.java100644 1750 1750 2376 12126627715 27370 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ /** * Classes to perform curve fitting. * * Curve fitting is a special case of a least squares problem * were the parameters are the coefficients of a function {@code f} * whose graph {@code y = f(x)} should pass through sample points, and * were the objective function is the squared sum of the residuals * f(xi) - yi for observed points * (xi, yi). */ package org.apache.commons.math3.fitting; commons-math3-3.2-src/src/main/java/org/apache/commons/math3/fitting/GaussianFitter.java100644 1750 1750 34225 12126627715 30012 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.fitting; import java.util.Arrays; import java.util.Comparator; import org.apache.commons.math3.analysis.function.Gaussian; import org.apache.commons.math3.exception.NullArgumentException; import org.apache.commons.math3.exception.NumberIsTooSmallException; import org.apache.commons.math3.exception.OutOfRangeException; import org.apache.commons.math3.exception.ZeroException; import org.apache.commons.math3.exception.NotStrictlyPositiveException; import org.apache.commons.math3.exception.util.LocalizedFormats; import org.apache.commons.math3.optim.nonlinear.vector.MultivariateVectorOptimizer; import org.apache.commons.math3.util.FastMath; /** * Fits points to a {@link * org.apache.commons.math3.analysis.function.Gaussian.Parametric Gaussian} function. *

                            * Usage example: *

                             *   GaussianFitter fitter = new GaussianFitter(
                             *     new LevenbergMarquardtOptimizer());
                             *   fitter.addObservedPoint(4.0254623,  531026.0);
                             *   fitter.addObservedPoint(4.03128248, 984167.0);
                             *   fitter.addObservedPoint(4.03839603, 1887233.0);
                             *   fitter.addObservedPoint(4.04421621, 2687152.0);
                             *   fitter.addObservedPoint(4.05132976, 3461228.0);
                             *   fitter.addObservedPoint(4.05326982, 3580526.0);
                             *   fitter.addObservedPoint(4.05779662, 3439750.0);
                             *   fitter.addObservedPoint(4.0636168,  2877648.0);
                             *   fitter.addObservedPoint(4.06943698, 2175960.0);
                             *   fitter.addObservedPoint(4.07525716, 1447024.0);
                             *   fitter.addObservedPoint(4.08237071, 717104.0);
                             *   fitter.addObservedPoint(4.08366408, 620014.0);
                             *   double[] parameters = fitter.fit();
                             * 
                            * * @since 2.2 * @version $Id: GaussianFitter.java 1416643 2012-12-03 19:37:14Z tn $ */ public class GaussianFitter extends CurveFitter { /** * Constructs an instance using the specified optimizer. * * @param optimizer Optimizer to use for the fitting. */ public GaussianFitter(MultivariateVectorOptimizer optimizer) { super(optimizer); } /** * Fits a Gaussian function to the observed points. * * @param initialGuess First guess values in the following order: *
                              *
                            • Norm
                            • *
                            • Mean
                            • *
                            • Sigma
                            • *
                            * @return the parameters of the Gaussian function that best fits the * observed points (in the same order as above). * @since 3.0 */ public double[] fit(double[] initialGuess) { final Gaussian.Parametric f = new Gaussian.Parametric() { @Override public double value(double x, double ... p) { double v = Double.POSITIVE_INFINITY; try { v = super.value(x, p); } catch (NotStrictlyPositiveException e) { // NOPMD // Do nothing. } return v; } @Override public double[] gradient(double x, double ... p) { double[] v = { Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY }; try { v = super.gradient(x, p); } catch (NotStrictlyPositiveException e) { // NOPMD // Do nothing. } return v; } }; return fit(f, initialGuess); } /** * Fits a Gaussian function to the observed points. * * @return the parameters of the Gaussian function that best fits the * observed points (in the same order as above). */ public double[] fit() { final double[] guess = (new ParameterGuesser(getObservations())).guess(); return fit(guess); } /** * Guesses the parameters {@code norm}, {@code mean}, and {@code sigma} * of a {@link org.apache.commons.math3.analysis.function.Gaussian.Parametric} * based on the specified observed points. */ public static class ParameterGuesser { /** Normalization factor. */ private final double norm; /** Mean. */ private final double mean; /** Standard deviation. */ private final double sigma; /** * Constructs instance with the specified observed points. * * @param observations Observed points from which to guess the * parameters of the Gaussian. * @throws NullArgumentException if {@code observations} is * {@code null}. * @throws NumberIsTooSmallException if there are less than 3 * observations. */ public ParameterGuesser(WeightedObservedPoint[] observations) { if (observations == null) { throw new NullArgumentException(LocalizedFormats.INPUT_ARRAY); } if (observations.length < 3) { throw new NumberIsTooSmallException(observations.length, 3, true); } final WeightedObservedPoint[] sorted = sortObservations(observations); final double[] params = basicGuess(sorted); norm = params[0]; mean = params[1]; sigma = params[2]; } /** * Gets an estimation of the parameters. * * @return the guessed parameters, in the following order: *
                              *
                            • Normalization factor
                            • *
                            • Mean
                            • *
                            • Standard deviation
                            • *
                            */ public double[] guess() { return new double[] { norm, mean, sigma }; } /** * Sort the observations. * * @param unsorted Input observations. * @return the input observations, sorted. */ private WeightedObservedPoint[] sortObservations(WeightedObservedPoint[] unsorted) { final WeightedObservedPoint[] observations = unsorted.clone(); final Comparator cmp = new Comparator() { public int compare(WeightedObservedPoint p1, WeightedObservedPoint p2) { if (p1 == null && p2 == null) { return 0; } if (p1 == null) { return -1; } if (p2 == null) { return 1; } if (p1.getX() < p2.getX()) { return -1; } if (p1.getX() > p2.getX()) { return 1; } if (p1.getY() < p2.getY()) { return -1; } if (p1.getY() > p2.getY()) { return 1; } if (p1.getWeight() < p2.getWeight()) { return -1; } if (p1.getWeight() > p2.getWeight()) { return 1; } return 0; } }; Arrays.sort(observations, cmp); return observations; } /** * Guesses the parameters based on the specified observed points. * * @param points Observed points, sorted. * @return the guessed parameters (normalization factor, mean and * sigma). */ private double[] basicGuess(WeightedObservedPoint[] points) { final int maxYIdx = findMaxY(points); final double n = points[maxYIdx].getY(); final double m = points[maxYIdx].getX(); double fwhmApprox; try { final double halfY = n + ((m - n) / 2); final double fwhmX1 = interpolateXAtY(points, maxYIdx, -1, halfY); final double fwhmX2 = interpolateXAtY(points, maxYIdx, 1, halfY); fwhmApprox = fwhmX2 - fwhmX1; } catch (OutOfRangeException e) { // TODO: Exceptions should not be used for flow control. fwhmApprox = points[points.length - 1].getX() - points[0].getX(); } final double s = fwhmApprox / (2 * FastMath.sqrt(2 * FastMath.log(2))); return new double[] { n, m, s }; } /** * Finds index of point in specified points with the largest Y. * * @param points Points to search. * @return the index in specified points array. */ private int findMaxY(WeightedObservedPoint[] points) { int maxYIdx = 0; for (int i = 1; i < points.length; i++) { if (points[i].getY() > points[maxYIdx].getY()) { maxYIdx = i; } } return maxYIdx; } /** * Interpolates using the specified points to determine X at the * specified Y. * * @param points Points to use for interpolation. * @param startIdx Index within points from which to start the search for * interpolation bounds points. * @param idxStep Index step for searching interpolation bounds points. * @param y Y value for which X should be determined. * @return the value of X for the specified Y. * @throws ZeroException if {@code idxStep} is 0. * @throws OutOfRangeException if specified {@code y} is not within the * range of the specified {@code points}. */ private double interpolateXAtY(WeightedObservedPoint[] points, int startIdx, int idxStep, double y) throws OutOfRangeException { if (idxStep == 0) { throw new ZeroException(); } final WeightedObservedPoint[] twoPoints = getInterpolationPointsForY(points, startIdx, idxStep, y); final WeightedObservedPoint p1 = twoPoints[0]; final WeightedObservedPoint p2 = twoPoints[1]; if (p1.getY() == y) { return p1.getX(); } if (p2.getY() == y) { return p2.getX(); } return p1.getX() + (((y - p1.getY()) * (p2.getX() - p1.getX())) / (p2.getY() - p1.getY())); } /** * Gets the two bounding interpolation points from the specified points * suitable for determining X at the specified Y. * * @param points Points to use for interpolation. * @param startIdx Index within points from which to start search for * interpolation bounds points. * @param idxStep Index step for search for interpolation bounds points. * @param y Y value for which X should be determined. * @return the array containing two points suitable for determining X at * the specified Y. * @throws ZeroException if {@code idxStep} is 0. * @throws OutOfRangeException if specified {@code y} is not within the * range of the specified {@code points}. */ private WeightedObservedPoint[] getInterpolationPointsForY(WeightedObservedPoint[] points, int startIdx, int idxStep, double y) throws OutOfRangeException { if (idxStep == 0) { throw new ZeroException(); } for (int i = startIdx; idxStep < 0 ? i + idxStep >= 0 : i + idxStep < points.length; i += idxStep) { final WeightedObservedPoint p1 = points[i]; final WeightedObservedPoint p2 = points[i + idxStep]; if (isBetween(y, p1.getY(), p2.getY())) { if (idxStep < 0) { return new WeightedObservedPoint[] { p2, p1 }; } else { return new WeightedObservedPoint[] { p1, p2 }; } } } // Boundaries are replaced by dummy values because the raised // exception is caught and the message never displayed. // TODO: Exceptions should not be used for flow control. throw new OutOfRangeException(y, Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY); } /** * Determines whether a value is between two other values. * * @param value Value to test whether it is between {@code boundary1} * and {@code boundary2}. * @param boundary1 One end of the range. * @param boundary2 Other end of the range. * @return {@code true} if {@code value} is between {@code boundary1} and * {@code boundary2} (inclusive), {@code false} otherwise. */ private boolean isBetween(double value, double boundary1, double boundary2) { return (value >= boundary1 && value <= boundary2) || (value >= boundary2 && value <= boundary1); } } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/fitting/WeightedObservedPoint.java100644 1750 1750 4701 12126627715 31302 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.fitting; import java.io.Serializable; /** * This class is a simple container for weighted observed point in * {@link CurveFitter curve fitting}. *

                            Instances of this class are guaranteed to be immutable.

                            * @version $Id: WeightedObservedPoint.java 1416643 2012-12-03 19:37:14Z tn $ * @since 2.0 */ public class WeightedObservedPoint implements Serializable { /** Serializable version id. */ private static final long serialVersionUID = 5306874947404636157L; /** Weight of the measurement in the fitting process. */ private final double weight; /** Abscissa of the point. */ private final double x; /** Observed value of the function at x. */ private final double y; /** * Simple constructor. * * @param weight Weight of the measurement in the fitting process. * @param x Abscissa of the measurement. * @param y Ordinate of the measurement. */ public WeightedObservedPoint(final double weight, final double x, final double y) { this.weight = weight; this.x = x; this.y = y; } /** * Gets the weight of the measurement in the fitting process. * * @return the weight of the measurement in the fitting process. */ public double getWeight() { return weight; } /** * Gets the abscissa of the point. * * @return the abscissa of the point. */ public double getX() { return x; } /** * Gets the observed value of the function at x. * * @return the observed value of the function at x. */ public double getY() { return y; } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/fitting/PolynomialFitter.java100644 1750 1750 5762 12126627715 30347 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.fitting; import org.apache.commons.math3.analysis.polynomials.PolynomialFunction; import org.apache.commons.math3.optim.nonlinear.vector.MultivariateVectorOptimizer; /** * Polynomial fitting is a very simple case of {@link CurveFitter curve fitting}. * The estimated coefficients are the polynomial coefficients (see the * {@link #fit(double[]) fit} method). * * @version $Id: PolynomialFitter.java 1416643 2012-12-03 19:37:14Z tn $ * @since 2.0 */ public class PolynomialFitter extends CurveFitter { /** * Simple constructor. * * @param optimizer Optimizer to use for the fitting. */ public PolynomialFitter(MultivariateVectorOptimizer optimizer) { super(optimizer); } /** * Get the coefficients of the polynomial fitting the weighted data points. * The degree of the fitting polynomial is {@code guess.length - 1}. * * @param guess First guess for the coefficients. They must be sorted in * increasing order of the polynomial's degree. * @param maxEval Maximum number of evaluations of the polynomial. * @return the coefficients of the polynomial that best fits the observed points. * @throws org.apache.commons.math3.exception.TooManyEvaluationsException if * the number of evaluations exceeds {@code maxEval}. * @throws org.apache.commons.math3.exception.ConvergenceException * if the algorithm failed to converge. */ public double[] fit(int maxEval, double[] guess) { return fit(maxEval, new PolynomialFunction.Parametric(), guess); } /** * Get the coefficients of the polynomial fitting the weighted data points. * The degree of the fitting polynomial is {@code guess.length - 1}. * * @param guess First guess for the coefficients. They must be sorted in * increasing order of the polynomial's degree. * @return the coefficients of the polynomial that best fits the observed points. * @throws org.apache.commons.math3.exception.ConvergenceException * if the algorithm failed to converge. */ public double[] fit(double[] guess) { return fit(new PolynomialFunction.Parametric(), guess); } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/fitting/CurveFitter.java100644 1750 1750 22133 12126627715 27317 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.fitting; import java.util.ArrayList; import java.util.List; import org.apache.commons.math3.analysis.MultivariateVectorFunction; import org.apache.commons.math3.analysis.MultivariateMatrixFunction; import org.apache.commons.math3.analysis.ParametricUnivariateFunction; import org.apache.commons.math3.optim.MaxEval; import org.apache.commons.math3.optim.InitialGuess; import org.apache.commons.math3.optim.PointVectorValuePair; import org.apache.commons.math3.optim.nonlinear.vector.MultivariateVectorOptimizer; import org.apache.commons.math3.optim.nonlinear.vector.ModelFunction; import org.apache.commons.math3.optim.nonlinear.vector.ModelFunctionJacobian; import org.apache.commons.math3.optim.nonlinear.vector.Target; import org.apache.commons.math3.optim.nonlinear.vector.Weight; /** * Fitter for parametric univariate real functions y = f(x). *
                            * When a univariate real function y = f(x) does depend on some * unknown parameters p0, p1 ... pn-1, * this class can be used to find these parameters. It does this * by fitting the curve so it remains very close to a set of * observed points (x0, y0), (x1, * y1) ... (xk-1, yk-1). This fitting * is done by finding the parameters values that minimizes the objective * function ∑(yi-f(xi))2. This is * really a least squares problem. * * @param Function to use for the fit. * * @version $Id: CurveFitter.java 1416643 2012-12-03 19:37:14Z tn $ * @since 2.0 */ public class CurveFitter { /** Optimizer to use for the fitting. */ private final MultivariateVectorOptimizer optimizer; /** Observed points. */ private final List observations; /** * Simple constructor. * * @param optimizer Optimizer to use for the fitting. * @since 3.1 */ public CurveFitter(final MultivariateVectorOptimizer optimizer) { this.optimizer = optimizer; observations = new ArrayList(); } /** Add an observed (x,y) point to the sample with unit weight. *

                            Calling this method is equivalent to call * {@code addObservedPoint(1.0, x, y)}.

                            * @param x abscissa of the point * @param y observed value of the point at x, after fitting we should * have f(x) as close as possible to this value * @see #addObservedPoint(double, double, double) * @see #addObservedPoint(WeightedObservedPoint) * @see #getObservations() */ public void addObservedPoint(double x, double y) { addObservedPoint(1.0, x, y); } /** Add an observed weighted (x,y) point to the sample. * @param weight weight of the observed point in the fit * @param x abscissa of the point * @param y observed value of the point at x, after fitting we should * have f(x) as close as possible to this value * @see #addObservedPoint(double, double) * @see #addObservedPoint(WeightedObservedPoint) * @see #getObservations() */ public void addObservedPoint(double weight, double x, double y) { observations.add(new WeightedObservedPoint(weight, x, y)); } /** Add an observed weighted (x,y) point to the sample. * @param observed observed point to add * @see #addObservedPoint(double, double) * @see #addObservedPoint(double, double, double) * @see #getObservations() */ public void addObservedPoint(WeightedObservedPoint observed) { observations.add(observed); } /** Get the observed points. * @return observed points * @see #addObservedPoint(double, double) * @see #addObservedPoint(double, double, double) * @see #addObservedPoint(WeightedObservedPoint) */ public WeightedObservedPoint[] getObservations() { return observations.toArray(new WeightedObservedPoint[observations.size()]); } /** * Remove all observations. */ public void clearObservations() { observations.clear(); } /** * Fit a curve. * This method compute the coefficients of the curve that best * fit the sample of observed points previously given through calls * to the {@link #addObservedPoint(WeightedObservedPoint) * addObservedPoint} method. * * @param f parametric function to fit. * @param initialGuess first guess of the function parameters. * @return the fitted parameters. * @throws org.apache.commons.math3.exception.DimensionMismatchException * if the start point dimension is wrong. */ public double[] fit(T f, final double[] initialGuess) { return fit(Integer.MAX_VALUE, f, initialGuess); } /** * Fit a curve. * This method compute the coefficients of the curve that best * fit the sample of observed points previously given through calls * to the {@link #addObservedPoint(WeightedObservedPoint) * addObservedPoint} method. * * @param f parametric function to fit. * @param initialGuess first guess of the function parameters. * @param maxEval Maximum number of function evaluations. * @return the fitted parameters. * @throws org.apache.commons.math3.exception.TooManyEvaluationsException * if the number of allowed evaluations is exceeded. * @throws org.apache.commons.math3.exception.DimensionMismatchException * if the start point dimension is wrong. * @since 3.0 */ public double[] fit(int maxEval, T f, final double[] initialGuess) { // Prepare least squares problem. double[] target = new double[observations.size()]; double[] weights = new double[observations.size()]; int i = 0; for (WeightedObservedPoint point : observations) { target[i] = point.getY(); weights[i] = point.getWeight(); ++i; } // Input to the optimizer: the model and its Jacobian. final TheoreticalValuesFunction model = new TheoreticalValuesFunction(f); // Perform the fit. final PointVectorValuePair optimum = optimizer.optimize(new MaxEval(maxEval), model.getModelFunction(), model.getModelFunctionJacobian(), new Target(target), new Weight(weights), new InitialGuess(initialGuess)); // Extract the coefficients. return optimum.getPointRef(); } /** Vectorial function computing function theoretical values. */ private class TheoreticalValuesFunction { /** Function to fit. */ private final ParametricUnivariateFunction f; /** * @param f function to fit. */ public TheoreticalValuesFunction(final ParametricUnivariateFunction f) { this.f = f; } /** * @return the model function values. */ public ModelFunction getModelFunction() { return new ModelFunction(new MultivariateVectorFunction() { /** {@inheritDoc} */ public double[] value(double[] point) { // compute the residuals final double[] values = new double[observations.size()]; int i = 0; for (WeightedObservedPoint observed : observations) { values[i++] = f.value(observed.getX(), point); } return values; } }); } /** * @return the model function Jacobian. */ public ModelFunctionJacobian getModelFunctionJacobian() { return new ModelFunctionJacobian(new MultivariateMatrixFunction() { public double[][] value(double[] point) { final double[][] jacobian = new double[observations.size()][]; int i = 0; for (WeightedObservedPoint observed : observations) { jacobian[i++] = f.gradient(observed.getX(), point); } return jacobian; } }); } } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/fitting/HarmonicFitter.java100644 1750 1750 40115 12126627715 27773 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.fitting; import org.apache.commons.math3.optim.nonlinear.vector.MultivariateVectorOptimizer; import org.apache.commons.math3.analysis.function.HarmonicOscillator; import org.apache.commons.math3.exception.ZeroException; import org.apache.commons.math3.exception.NumberIsTooSmallException; import org.apache.commons.math3.exception.MathIllegalStateException; import org.apache.commons.math3.exception.util.LocalizedFormats; import org.apache.commons.math3.util.FastMath; /** * Class that implements a curve fitting specialized for sinusoids. * * Harmonic fitting is a very simple case of curve fitting. The * estimated coefficients are the amplitude a, the pulsation ω and * the phase φ: f (t) = a cos (ω t + φ). They are * searched by a least square estimator initialized with a rough guess * based on integrals. * * @version $Id: HarmonicFitter.java 1416643 2012-12-03 19:37:14Z tn $ * @since 2.0 */ public class HarmonicFitter extends CurveFitter { /** * Simple constructor. * @param optimizer Optimizer to use for the fitting. */ public HarmonicFitter(final MultivariateVectorOptimizer optimizer) { super(optimizer); } /** * Fit an harmonic function to the observed points. * * @param initialGuess First guess values in the following order: *
                              *
                            • Amplitude
                            • *
                            • Angular frequency
                            • *
                            • Phase
                            • *
                            * @return the parameters of the harmonic function that best fits the * observed points (in the same order as above). */ public double[] fit(double[] initialGuess) { return fit(new HarmonicOscillator.Parametric(), initialGuess); } /** * Fit an harmonic function to the observed points. * An initial guess will be automatically computed. * * @return the parameters of the harmonic function that best fits the * observed points (see the other {@link #fit(double[]) fit} method. * @throws NumberIsTooSmallException if the sample is too short for the * the first guess to be computed. * @throws ZeroException if the first guess cannot be computed because * the abscissa range is zero. */ public double[] fit() { return fit((new ParameterGuesser(getObservations())).guess()); } /** * This class guesses harmonic coefficients from a sample. *

                            The algorithm used to guess the coefficients is as follows:

                            * *

                            We know f (t) at some sampling points ti and want to find a, * ω and φ such that f (t) = a cos (ω t + φ). *

                            * *

                            From the analytical expression, we can compute two primitives : *

                                 *     If2  (t) = ∫ f2  = a2 × [t + S (t)] / 2
                                 *     If'2 (t) = ∫ f'2 = a2 ω2 × [t - S (t)] / 2
                                 *     where S (t) = sin (2 (ω t + φ)) / (2 ω)
                                 * 
                            *

                            * *

                            We can remove S between these expressions : *

                                 *     If'2 (t) = a2 ω2 t - ω2 If2 (t)
                                 * 
                            *

                            * *

                            The preceding expression shows that If'2 (t) is a linear * combination of both t and If2 (t): If'2 (t) = A × t + B × If2 (t) *

                            * *

                            From the primitive, we can deduce the same form for definite * integrals between t1 and ti for each ti : *

                                 *   If2 (ti) - If2 (t1) = A × (ti - t1) + B × (If2 (ti) - If2 (t1))
                                 * 
                            *

                            * *

                            We can find the coefficients A and B that best fit the sample * to this linear expression by computing the definite integrals for * each sample points. *

                            * *

                            For a bilinear expression z (xi, yi) = A × xi + B × yi, the * coefficients A and B that minimize a least square criterion * ∑ (zi - z (xi, yi))2 are given by these expressions:

                            *
                                 *
                                 *         ∑yiyi ∑xizi - ∑xiyi ∑yizi
                                 *     A = ------------------------
                                 *         ∑xixi ∑yiyi - ∑xiyi ∑xiyi
                                 *
                                 *         ∑xixi ∑yizi - ∑xiyi ∑xizi
                                 *     B = ------------------------
                                 *         ∑xixi ∑yiyi - ∑xiyi ∑xiyi
                                 * 
                            *

                            * * *

                            In fact, we can assume both a and ω are positive and * compute them directly, knowing that A = a2 ω2 and that * B = - ω2. The complete algorithm is therefore:

                            *
                                 *
                                 * for each ti from t1 to tn-1, compute:
                                 *   f  (ti)
                                 *   f' (ti) = (f (ti+1) - f(ti-1)) / (ti+1 - ti-1)
                                 *   xi = ti - t1
                                 *   yi = ∫ f2 from t1 to ti
                                 *   zi = ∫ f'2 from t1 to ti
                                 *   update the sums ∑xixi, ∑yiyi, ∑xiyi, ∑xizi and ∑yizi
                                 * end for
                                 *
                                 *            |--------------------------
                                 *         \  | ∑yiyi ∑xizi - ∑xiyi ∑yizi
                                 * a     =  \ | ------------------------
                                 *           \| ∑xiyi ∑xizi - ∑xixi ∑yizi
                                 *
                                 *
                                 *            |--------------------------
                                 *         \  | ∑xiyi ∑xizi - ∑xixi ∑yizi
                                 * ω     =  \ | ------------------------
                                 *           \| ∑xixi ∑yiyi - ∑xiyi ∑xiyi
                                 *
                                 * 
                            *

                            * *

                            Once we know ω, we can compute: *

                                 *    fc = ω f (t) cos (ω t) - f' (t) sin (ω t)
                                 *    fs = ω f (t) sin (ω t) + f' (t) cos (ω t)
                                 * 
                            *

                            * *

                            It appears that fc = a ω cos (φ) and * fs = -a ω sin (φ), so we can use these * expressions to compute φ. The best estimate over the sample is * given by averaging these expressions. *

                            * *

                            Since integrals and means are involved in the preceding * estimations, these operations run in O(n) time, where n is the * number of measurements.

                            */ public static class ParameterGuesser { /** Amplitude. */ private final double a; /** Angular frequency. */ private final double omega; /** Phase. */ private final double phi; /** * Simple constructor. * * @param observations Sampled observations. * @throws NumberIsTooSmallException if the sample is too short. * @throws ZeroException if the abscissa range is zero. * @throws MathIllegalStateException when the guessing procedure cannot * produce sensible results. */ public ParameterGuesser(WeightedObservedPoint[] observations) { if (observations.length < 4) { throw new NumberIsTooSmallException(LocalizedFormats.INSUFFICIENT_OBSERVED_POINTS_IN_SAMPLE, observations.length, 4, true); } final WeightedObservedPoint[] sorted = sortObservations(observations); final double aOmega[] = guessAOmega(sorted); a = aOmega[0]; omega = aOmega[1]; phi = guessPhi(sorted); } /** * Gets an estimation of the parameters. * * @return the guessed parameters, in the following order: *
                              *
                            • Amplitude
                            • *
                            • Angular frequency
                            • *
                            • Phase
                            • *
                            */ public double[] guess() { return new double[] { a, omega, phi }; } /** * Sort the observations with respect to the abscissa. * * @param unsorted Input observations. * @return the input observations, sorted. */ private WeightedObservedPoint[] sortObservations(WeightedObservedPoint[] unsorted) { final WeightedObservedPoint[] observations = unsorted.clone(); // Since the samples are almost always already sorted, this // method is implemented as an insertion sort that reorders the // elements in place. Insertion sort is very efficient in this case. WeightedObservedPoint curr = observations[0]; for (int j = 1; j < observations.length; ++j) { WeightedObservedPoint prec = curr; curr = observations[j]; if (curr.getX() < prec.getX()) { // the current element should be inserted closer to the beginning int i = j - 1; WeightedObservedPoint mI = observations[i]; while ((i >= 0) && (curr.getX() < mI.getX())) { observations[i + 1] = mI; if (i-- != 0) { mI = observations[i]; } } observations[i + 1] = curr; curr = observations[j]; } } return observations; } /** * Estimate a first guess of the amplitude and angular frequency. * This method assumes that the {@link #sortObservations()} method * has been called previously. * * @param observations Observations, sorted w.r.t. abscissa. * @throws ZeroException if the abscissa range is zero. * @throws MathIllegalStateException when the guessing procedure cannot * produce sensible results. * @return the guessed amplitude (at index 0) and circular frequency * (at index 1). */ private double[] guessAOmega(WeightedObservedPoint[] observations) { final double[] aOmega = new double[2]; // initialize the sums for the linear model between the two integrals double sx2 = 0; double sy2 = 0; double sxy = 0; double sxz = 0; double syz = 0; double currentX = observations[0].getX(); double currentY = observations[0].getY(); double f2Integral = 0; double fPrime2Integral = 0; final double startX = currentX; for (int i = 1; i < observations.length; ++i) { // one step forward final double previousX = currentX; final double previousY = currentY; currentX = observations[i].getX(); currentY = observations[i].getY(); // update the integrals of f2 and f'2 // considering a linear model for f (and therefore constant f') final double dx = currentX - previousX; final double dy = currentY - previousY; final double f2StepIntegral = dx * (previousY * previousY + previousY * currentY + currentY * currentY) / 3; final double fPrime2StepIntegral = dy * dy / dx; final double x = currentX - startX; f2Integral += f2StepIntegral; fPrime2Integral += fPrime2StepIntegral; sx2 += x * x; sy2 += f2Integral * f2Integral; sxy += x * f2Integral; sxz += x * fPrime2Integral; syz += f2Integral * fPrime2Integral; } // compute the amplitude and pulsation coefficients double c1 = sy2 * sxz - sxy * syz; double c2 = sxy * sxz - sx2 * syz; double c3 = sx2 * sy2 - sxy * sxy; if ((c1 / c2 < 0) || (c2 / c3 < 0)) { final int last = observations.length - 1; // Range of the observations, assuming that the // observations are sorted. final double xRange = observations[last].getX() - observations[0].getX(); if (xRange == 0) { throw new ZeroException(); } aOmega[1] = 2 * Math.PI / xRange; double yMin = Double.POSITIVE_INFINITY; double yMax = Double.NEGATIVE_INFINITY; for (int i = 1; i < observations.length; ++i) { final double y = observations[i].getY(); if (y < yMin) { yMin = y; } if (y > yMax) { yMax = y; } } aOmega[0] = 0.5 * (yMax - yMin); } else { if (c2 == 0) { // In some ill-conditioned cases (cf. MATH-844), the guesser // procedure cannot produce sensible results. throw new MathIllegalStateException(LocalizedFormats.ZERO_DENOMINATOR); } aOmega[0] = FastMath.sqrt(c1 / c2); aOmega[1] = FastMath.sqrt(c2 / c3); } return aOmega; } /** * Estimate a first guess of the phase. * * @param observations Observations, sorted w.r.t. abscissa. * @return the guessed phase. */ private double guessPhi(WeightedObservedPoint[] observations) { // initialize the means double fcMean = 0; double fsMean = 0; double currentX = observations[0].getX(); double currentY = observations[0].getY(); for (int i = 1; i < observations.length; ++i) { // one step forward final double previousX = currentX; final double previousY = currentY; currentX = observations[i].getX(); currentY = observations[i].getY(); final double currentYPrime = (currentY - previousY) / (currentX - previousX); double omegaX = omega * currentX; double cosine = FastMath.cos(omegaX); double sine = FastMath.sin(omegaX); fcMean += omega * currentY * cosine - currentYPrime * sine; fsMean += omega * currentY * sine + currentYPrime * cosine; } return FastMath.atan2(-fsMean, fcMean); } } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/transform/DstNormalization.java100644 1750 1750 4627 12126627720 30711 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.transform; /** * This enumeration defines the various types of normalizations that can be * applied to discrete sine transforms (DST). The exact definition of these * normalizations is detailed below. * * @see FastSineTransformer * @version $Id: DstNormalization.java 1385310 2012-09-16 16:32:10Z tn $ * @since 3.0 */ public enum DstNormalization { /** * Should be passed to the constructor of {@link FastSineTransformer} to * use the standard normalization convention. The standard DST-I * normalization convention is defined as follows *
                              *
                            • forward transform: yn = ∑k=0N-1 * xk sin(π nk / N),
                            • *
                            • inverse transform: xk = (2 / N) * ∑n=0N-1 yn sin(π nk / N),
                            • *
                            * where N is the size of the data sample, and x0 = 0. */ STANDARD_DST_I, /** * Should be passed to the constructor of {@link FastSineTransformer} to * use the orthogonal normalization convention. The orthogonal * DCT-I normalization convention is defined as follows *
                              *
                            • Forward transform: yn = √(2 / N) * ∑k=0N-1 xk sin(π nk / N),
                            • *
                            • Inverse transform: xk = √(2 / N) * ∑n=0N-1 yn sin(π nk / N),
                            • *
                            * which makes the transform orthogonal. N is the size of the data sample, * and x0 = 0. */ ORTHOGONAL_DST_I } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/transform/package-info.java100644 1750 1750 1655 12126627720 27732 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ /** * * Implementations of transform methods, including Fast Fourier transforms. * */ package org.apache.commons.math3.transform; commons-math3-3.2-src/src/main/java/org/apache/commons/math3/transform/DctNormalization.java100644 1750 1750 5411 12126627720 30661 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.transform; /** * This enumeration defines the various types of normalizations that can be * applied to discrete cosine transforms (DCT). The exact definition of these * normalizations is detailed below. * * @see FastCosineTransformer * @version $Id: DctNormalization.java 1385310 2012-09-16 16:32:10Z tn $ * @since 3.0 */ public enum DctNormalization { /** * Should be passed to the constructor of {@link FastCosineTransformer} * to use the standard normalization convention. The standard * DCT-I normalization convention is defined as follows *
                              *
                            • forward transform: * yn = (1/2) [x0 + (-1)nxN-1] * + ∑k=1N-2 * xk cos[π nk / (N - 1)],
                            • *
                            • inverse transform: * xk = [1 / (N - 1)] [y0 * + (-1)kyN-1] * + [2 / (N - 1)] ∑n=1N-2 * yn cos[π nk / (N - 1)],
                            • *
                            * where N is the size of the data sample. */ STANDARD_DCT_I, /** * Should be passed to the constructor of {@link FastCosineTransformer} * to use the orthogonal normalization convention. The orthogonal * DCT-I normalization convention is defined as follows *
                              *
                            • forward transform: * yn = [2(N - 1)]-1/2 [x0 * + (-1)nxN-1] * + [2 / (N - 1)]1/2k=1N-2 * xk cos[π nk / (N - 1)],
                            • *
                            • inverse transform: * xk = [2(N - 1)]-1/2 [y0 * + (-1)kyN-1] * + [2 / (N - 1)]1/2n=1N-2 * yn cos[π nk / (N - 1)],
                            • *
                            * which makes the transform orthogonal. N is the size of the data sample. */ ORTHOGONAL_DCT_I; } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/transform/FastCosineTransformer.java100644 1750 1750 16311 12126627720 31702 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.transform; import java.io.Serializable; import org.apache.commons.math3.analysis.FunctionUtils; import org.apache.commons.math3.analysis.UnivariateFunction; import org.apache.commons.math3.complex.Complex; import org.apache.commons.math3.exception.MathIllegalArgumentException; import org.apache.commons.math3.exception.util.LocalizedFormats; import org.apache.commons.math3.util.ArithmeticUtils; import org.apache.commons.math3.util.FastMath; /** * Implements the Fast Cosine Transform for transformation of one-dimensional * real data sets. For reference, see James S. Walker, Fast Fourier * Transforms, chapter 3 (ISBN 0849371635). *

                            * There are several variants of the discrete cosine transform. The present * implementation corresponds to DCT-I, with various normalization conventions, * which are specified by the parameter {@link DctNormalization}. *

                            * DCT-I is equivalent to DFT of an even extension of the data series. * More precisely, if x0, …, xN-1 is the data set * to be cosine transformed, the extended data set * x0#, …, x2N-3# * is defined as follows *

                              *
                            • xk# = xk if 0 ≤ k < N,
                            • *
                            • xk# = x2N-2-k * if N ≤ k < 2N - 2.
                            • *
                            *

                            * Then, the standard DCT-I y0, …, yN-1 of the real * data set x0, …, xN-1 is equal to half * of the N first elements of the DFT of the extended data set * x0#, …, x2N-3# *
                            * yn = (1 / 2) ∑k=02N-3 * xk# exp[-2πi nk / (2N - 2)] *     k = 0, …, N-1. *

                            * The present implementation of the discrete cosine transform as a fast cosine * transform requires the length of the data set to be a power of two plus one * (N = 2n + 1). Besides, it implicitly assumes * that the sampled function is even. * * @version $Id: FastCosineTransformer.java 1385310 2012-09-16 16:32:10Z tn $ * @since 1.2 */ public class FastCosineTransformer implements RealTransformer, Serializable { /** Serializable version identifier. */ static final long serialVersionUID = 20120212L; /** The type of DCT to be performed. */ private final DctNormalization normalization; /** * Creates a new instance of this class, with various normalization * conventions. * * @param normalization the type of normalization to be applied to the * transformed data */ public FastCosineTransformer(final DctNormalization normalization) { this.normalization = normalization; } /** * {@inheritDoc} * * @throws MathIllegalArgumentException if the length of the data array is * not a power of two plus one */ public double[] transform(final double[] f, final TransformType type) throws MathIllegalArgumentException { if (type == TransformType.FORWARD) { if (normalization == DctNormalization.ORTHOGONAL_DCT_I) { final double s = FastMath.sqrt(2.0 / (f.length - 1)); return TransformUtils.scaleArray(fct(f), s); } return fct(f); } final double s2 = 2.0 / (f.length - 1); final double s1; if (normalization == DctNormalization.ORTHOGONAL_DCT_I) { s1 = FastMath.sqrt(s2); } else { s1 = s2; } return TransformUtils.scaleArray(fct(f), s1); } /** * {@inheritDoc} * * @throws org.apache.commons.math3.exception.NonMonotonicSequenceException * if the lower bound is greater than, or equal to the upper bound * @throws org.apache.commons.math3.exception.NotStrictlyPositiveException * if the number of sample points is negative * @throws MathIllegalArgumentException if the number of sample points is * not a power of two plus one */ public double[] transform(final UnivariateFunction f, final double min, final double max, final int n, final TransformType type) throws MathIllegalArgumentException { final double[] data = FunctionUtils.sample(f, min, max, n); return transform(data, type); } /** * Perform the FCT algorithm (including inverse). * * @param f the real data array to be transformed * @return the real transformed array * @throws MathIllegalArgumentException if the length of the data array is * not a power of two plus one */ protected double[] fct(double[] f) throws MathIllegalArgumentException { final double[] transformed = new double[f.length]; final int n = f.length - 1; if (!ArithmeticUtils.isPowerOfTwo(n)) { throw new MathIllegalArgumentException( LocalizedFormats.NOT_POWER_OF_TWO_PLUS_ONE, Integer.valueOf(f.length)); } if (n == 1) { // trivial case transformed[0] = 0.5 * (f[0] + f[1]); transformed[1] = 0.5 * (f[0] - f[1]); return transformed; } // construct a new array and perform FFT on it final double[] x = new double[n]; x[0] = 0.5 * (f[0] + f[n]); x[n >> 1] = f[n >> 1]; // temporary variable for transformed[1] double t1 = 0.5 * (f[0] - f[n]); for (int i = 1; i < (n >> 1); i++) { final double a = 0.5 * (f[i] + f[n - i]); final double b = FastMath.sin(i * FastMath.PI / n) * (f[i] - f[n - i]); final double c = FastMath.cos(i * FastMath.PI / n) * (f[i] - f[n - i]); x[i] = a - b; x[n - i] = a + b; t1 += c; } FastFourierTransformer transformer; transformer = new FastFourierTransformer(DftNormalization.STANDARD); Complex[] y = transformer.transform(x, TransformType.FORWARD); // reconstruct the FCT result for the original array transformed[0] = y[0].getReal(); transformed[1] = t1; for (int i = 1; i < (n >> 1); i++) { transformed[2 * i] = y[i].getReal(); transformed[2 * i + 1] = transformed[2 * i - 1] - y[i].getImaginary(); } transformed[n] = y[n >> 1].getReal(); return transformed; } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/transform/TransformUtils.java100644 1750 1750 13637 12126627720 30425 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.transform; import java.util.Arrays; import org.apache.commons.math3.complex.Complex; import org.apache.commons.math3.exception.DimensionMismatchException; import org.apache.commons.math3.exception.MathIllegalArgumentException; import org.apache.commons.math3.exception.util.LocalizedFormats; /** * Useful functions for the implementation of various transforms. * * @version $Id: TransformUtils.java 1385310 2012-09-16 16:32:10Z tn $ * @since 3.0 */ public class TransformUtils { /** * Table of the powers of 2 to facilitate binary search lookup. * * @see #exactLog2(int) */ private static final int[] POWERS_OF_TWO = { 0x00000001, 0x00000002, 0x00000004, 0x00000008, 0x00000010, 0x00000020, 0x00000040, 0x00000080, 0x00000100, 0x00000200, 0x00000400, 0x00000800, 0x00001000, 0x00002000, 0x00004000, 0x00008000, 0x00010000, 0x00020000, 0x00040000, 0x00080000, 0x00100000, 0x00200000, 0x00400000, 0x00800000, 0x01000000, 0x02000000, 0x04000000, 0x08000000, 0x10000000, 0x20000000, 0x40000000 }; /** Private constructor. */ private TransformUtils() { super(); } /** * Multiply every component in the given real array by the * given real number. The change is made in place. * * @param f the real array to be scaled * @param d the real scaling coefficient * @return a reference to the scaled array */ public static double[] scaleArray(double[] f, double d) { for (int i = 0; i < f.length; i++) { f[i] *= d; } return f; } /** * Multiply every component in the given complex array by the * given real number. The change is made in place. * * @param f the complex array to be scaled * @param d the real scaling coefficient * @return a reference to the scaled array */ public static Complex[] scaleArray(Complex[] f, double d) { for (int i = 0; i < f.length; i++) { f[i] = new Complex(d * f[i].getReal(), d * f[i].getImaginary()); } return f; } /** * Builds a new two dimensional array of {@code double} filled with the real * and imaginary parts of the specified {@link Complex} numbers. In the * returned array {@code dataRI}, the data is laid out as follows *

                              *
                            • {@code dataRI[0][i] = dataC[i].getReal()},
                            • *
                            • {@code dataRI[1][i] = dataC[i].getImaginary()}.
                            • *
                            * * @param dataC the array of {@link Complex} data to be transformed * @return a two dimensional array filled with the real and imaginary parts * of the specified complex input */ public static double[][] createRealImaginaryArray(final Complex[] dataC) { final double[][] dataRI = new double[2][dataC.length]; final double[] dataR = dataRI[0]; final double[] dataI = dataRI[1]; for (int i = 0; i < dataC.length; i++) { final Complex c = dataC[i]; dataR[i] = c.getReal(); dataI[i] = c.getImaginary(); } return dataRI; } /** * Builds a new array of {@link Complex} from the specified two dimensional * array of real and imaginary parts. In the returned array {@code dataC}, * the data is laid out as follows *
                              *
                            • {@code dataC[i].getReal() = dataRI[0][i]},
                            • *
                            • {@code dataC[i].getImaginary() = dataRI[1][i]}.
                            • *
                            * * @param dataRI the array of real and imaginary parts to be transformed * @return an array of {@link Complex} with specified real and imaginary parts. * @throws DimensionMismatchException if the number of rows of the specified * array is not two, or the array is not rectangular */ public static Complex[] createComplexArray(final double[][] dataRI) throws DimensionMismatchException{ if (dataRI.length != 2) { throw new DimensionMismatchException(dataRI.length, 2); } final double[] dataR = dataRI[0]; final double[] dataI = dataRI[1]; if (dataR.length != dataI.length) { throw new DimensionMismatchException(dataI.length, dataR.length); } final int n = dataR.length; final Complex[] c = new Complex[n]; for (int i = 0; i < n; i++) { c[i] = new Complex(dataR[i], dataI[i]); } return c; } /** * Returns the base-2 logarithm of the specified {@code int}. Throws an * exception if {@code n} is not a power of two. * * @param n the {@code int} whose base-2 logarithm is to be evaluated * @return the base-2 logarithm of {@code n} * @throws MathIllegalArgumentException if {@code n} is not a power of two */ public static int exactLog2(final int n) throws MathIllegalArgumentException { int index = Arrays.binarySearch(TransformUtils.POWERS_OF_TWO, n); if (index < 0) { throw new MathIllegalArgumentException( LocalizedFormats.NOT_POWER_OF_TWO_CONSIDER_PADDING, Integer.valueOf(n)); } return index; } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/transform/RealTransformer.java100644 1750 1750 6600 12126627720 30507 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.transform; import org.apache.commons.math3.analysis.UnivariateFunction; import org.apache.commons.math3.exception.MathIllegalArgumentException; import org.apache.commons.math3.exception.NonMonotonicSequenceException; import org.apache.commons.math3.exception.NotStrictlyPositiveException; /** * Interface for one-dimensional data sets transformations producing real results. *

                            * Such transforms include {@link FastSineTransformer sine transform}, * {@link FastCosineTransformer cosine transform} or {@link * FastHadamardTransformer Hadamard transform}. {@link FastFourierTransformer * Fourier transform} is of a different kind and does not implement this * interface since it produces {@link org.apache.commons.math3.complex.Complex} * results instead of real ones. * * @version $Id: RealTransformer.java 1385310 2012-09-16 16:32:10Z tn $ * @since 2.0 */ public interface RealTransformer { /** * Returns the (forward, inverse) transform of the specified real data set. * * @param f the real data array to be transformed (signal) * @param type the type of transform (forward, inverse) to be performed * @return the real transformed array (spectrum) * @throws MathIllegalArgumentException if the array cannot be transformed * with the given type (this may be for example due to array size, which is * constrained in some transforms) */ double[] transform(double[] f, TransformType type) throws MathIllegalArgumentException; /** * Returns the (forward, inverse) transform of the specified real function, * sampled on the specified interval. * * @param f the function to be sampled and transformed * @param min the (inclusive) lower bound for the interval * @param max the (exclusive) upper bound for the interval * @param n the number of sample points * @param type the type of transform (forward, inverse) to be performed * @return the real transformed array * @throws NonMonotonicSequenceException if the lower bound is greater than, or equal to the upper bound * @throws NotStrictlyPositiveException if the number of sample points is negative * @throws MathIllegalArgumentException if the sample cannot be transformed * with the given type (this may be for example due to sample size, which is * constrained in some transforms) */ double[] transform(UnivariateFunction f, double min, double max, int n, TransformType type) throws NonMonotonicSequenceException, NotStrictlyPositiveException, MathIllegalArgumentException; } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/transform/TransformType.java100644 1750 1750 2253 12126627720 30216 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.transform; /** * This enumeration defines the type of transform which is to be computed. * * @since 3.0 * @version $Id: TransformType.java 1364391 2012-07-22 18:21:57Z tn $ */ public enum TransformType { /** The type to be specified for forward transforms. */ FORWARD, /** The type to be specified for inverse transforms. */ INVERSE; } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/transform/DftNormalization.java100644 1750 1750 4530 12126627720 30665 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.transform; /** * This enumeration defines the various types of normalizations that can be * applied to discrete Fourier transforms (DFT). The exact definition of these * normalizations is detailed below. * * @see FastFourierTransformer * @version $Id: DftNormalization.java 1385310 2012-09-16 16:32:10Z tn $ * @since 3.0 */ public enum DftNormalization { /** * Should be passed to the constructor of {@link FastFourierTransformer} * to use the standard normalization convention. This normalization * convention is defined as follows *

                              *
                            • forward transform: yn = ∑k=0N-1 * xk exp(-2πi n k / N),
                            • *
                            • inverse transform: xk = N-1 * ∑n=0N-1 yn exp(2πi n k / N),
                            • *
                            * where N is the size of the data sample. */ STANDARD, /** * Should be passed to the constructor of {@link FastFourierTransformer} * to use the unitary normalization convention. This normalization * convention is defined as follows *
                              *
                            • forward transform: yn = (1 / √N) * ∑k=0N-1 xk * exp(-2πi n k / N),
                            • *
                            • inverse transform: xk = (1 / √N) * ∑n=0N-1 yn exp(2πi n k / N),
                            • *
                            * which makes the transform unitary. N is the size of the data sample. */ UNITARY; } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/transform/FastSineTransformer.java100644 1750 1750 17020 12126627720 31356 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.transform; import java.io.Serializable; import org.apache.commons.math3.analysis.FunctionUtils; import org.apache.commons.math3.analysis.UnivariateFunction; import org.apache.commons.math3.complex.Complex; import org.apache.commons.math3.exception.MathIllegalArgumentException; import org.apache.commons.math3.exception.util.LocalizedFormats; import org.apache.commons.math3.util.ArithmeticUtils; import org.apache.commons.math3.util.FastMath; /** * Implements the Fast Sine Transform for transformation of one-dimensional real * data sets. For reference, see James S. Walker, Fast Fourier * Transforms, chapter 3 (ISBN 0849371635). *

                            * There are several variants of the discrete sine transform. The present * implementation corresponds to DST-I, with various normalization conventions, * which are specified by the parameter {@link DstNormalization}. * It should be noted that regardless to the convention, the first * element of the dataset to be transformed must be zero. *

                            * DST-I is equivalent to DFT of an odd extension of the data series. * More precisely, if x0, …, xN-1 is the data set * to be sine transformed, the extended data set x0#, * …, x2N-1# is defined as follows *

                              *
                            • x0# = x0 = 0,
                            • *
                            • xk# = xk if 1 ≤ k < N,
                            • *
                            • xN# = 0,
                            • *
                            • xk# = -x2N-k if N + 1 ≤ k < * 2N.
                            • *
                            *

                            * Then, the standard DST-I y0, …, yN-1 of the real * data set x0, …, xN-1 is equal to half * of i (the pure imaginary number) times the N first elements of the DFT of the * extended data set x0#, …, * x2N-1#
                            * yn = (i / 2) ∑k=02N-1 * xk# exp[-2πi nk / (2N)] *     k = 0, …, N-1. *

                            * The present implementation of the discrete sine transform as a fast sine * transform requires the length of the data to be a power of two. Besides, * it implicitly assumes that the sampled function is odd. In particular, the * first element of the data set must be 0, which is enforced in * {@link #transform(UnivariateFunction, double, double, int, TransformType)}, * after sampling. * * @version $Id: FastSineTransformer.java 1385310 2012-09-16 16:32:10Z tn $ * @since 1.2 */ public class FastSineTransformer implements RealTransformer, Serializable { /** Serializable version identifier. */ static final long serialVersionUID = 20120211L; /** The type of DST to be performed. */ private final DstNormalization normalization; /** * Creates a new instance of this class, with various normalization conventions. * * @param normalization the type of normalization to be applied to the transformed data */ public FastSineTransformer(final DstNormalization normalization) { this.normalization = normalization; } /** * {@inheritDoc} * * The first element of the specified data set is required to be {@code 0}. * * @throws MathIllegalArgumentException if the length of the data array is * not a power of two, or the first element of the data array is not zero */ public double[] transform(final double[] f, final TransformType type) { if (normalization == DstNormalization.ORTHOGONAL_DST_I) { final double s = FastMath.sqrt(2.0 / f.length); return TransformUtils.scaleArray(fst(f), s); } if (type == TransformType.FORWARD) { return fst(f); } final double s = 2.0 / f.length; return TransformUtils.scaleArray(fst(f), s); } /** * {@inheritDoc} * * This implementation enforces {@code f(x) = 0.0} at {@code x = 0.0}. * * @throws org.apache.commons.math3.exception.NonMonotonicSequenceException * if the lower bound is greater than, or equal to the upper bound * @throws org.apache.commons.math3.exception.NotStrictlyPositiveException * if the number of sample points is negative * @throws MathIllegalArgumentException if the number of sample points is not a power of two */ public double[] transform(final UnivariateFunction f, final double min, final double max, final int n, final TransformType type) { final double[] data = FunctionUtils.sample(f, min, max, n); data[0] = 0.0; return transform(data, type); } /** * Perform the FST algorithm (including inverse). The first element of the * data set is required to be {@code 0}. * * @param f the real data array to be transformed * @return the real transformed array * @throws MathIllegalArgumentException if the length of the data array is * not a power of two, or the first element of the data array is not zero */ protected double[] fst(double[] f) throws MathIllegalArgumentException { final double[] transformed = new double[f.length]; if (!ArithmeticUtils.isPowerOfTwo(f.length)) { throw new MathIllegalArgumentException( LocalizedFormats.NOT_POWER_OF_TWO_CONSIDER_PADDING, Integer.valueOf(f.length)); } if (f[0] != 0.0) { throw new MathIllegalArgumentException( LocalizedFormats.FIRST_ELEMENT_NOT_ZERO, Double.valueOf(f[0])); } final int n = f.length; if (n == 1) { // trivial case transformed[0] = 0.0; return transformed; } // construct a new array and perform FFT on it final double[] x = new double[n]; x[0] = 0.0; x[n >> 1] = 2.0 * f[n >> 1]; for (int i = 1; i < (n >> 1); i++) { final double a = FastMath.sin(i * FastMath.PI / n) * (f[i] + f[n - i]); final double b = 0.5 * (f[i] - f[n - i]); x[i] = a + b; x[n - i] = a - b; } FastFourierTransformer transformer; transformer = new FastFourierTransformer(DftNormalization.STANDARD); Complex[] y = transformer.transform(x, TransformType.FORWARD); // reconstruct the FST result for the original array transformed[0] = 0.0; transformed[1] = 0.5 * y[0].getReal(); for (int i = 1; i < (n >> 1); i++) { transformed[2 * i] = -y[i].getImaginary(); transformed[2 * i + 1] = y[i].getReal() + transformed[2 * i - 1]; } return transformed; } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/transform/FastHadamardTransformer.java100644 1750 1750 30416 12126627720 32165 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.transform; import java.io.Serializable; import org.apache.commons.math3.analysis.FunctionUtils; import org.apache.commons.math3.analysis.UnivariateFunction; import org.apache.commons.math3.exception.MathIllegalArgumentException; import org.apache.commons.math3.exception.util.LocalizedFormats; import org.apache.commons.math3.util.ArithmeticUtils; /** * Implements the Fast Hadamard Transform (FHT). * Transformation of an input vector x to the output vector y. *

                            * In addition to transformation of real vectors, the Hadamard transform can * transform integer vectors into integer vectors. However, this integer transform * cannot be inverted directly. Due to a scaling factor it may lead to rational results. * As an example, the inverse transform of integer vector (0, 1, 0, 1) is rational * vector (1/2, -1/2, 0, 0). * * @version $Id: FastHadamardTransformer.java 1385310 2012-09-16 16:32:10Z tn $ * @since 2.0 */ public class FastHadamardTransformer implements RealTransformer, Serializable { /** Serializable version identifier. */ static final long serialVersionUID = 20120211L; /** * {@inheritDoc} * * @throws MathIllegalArgumentException if the length of the data array is * not a power of two */ public double[] transform(final double[] f, final TransformType type) { if (type == TransformType.FORWARD) { return fht(f); } return TransformUtils.scaleArray(fht(f), 1.0 / f.length); } /** * {@inheritDoc} * * @throws org.apache.commons.math3.exception.NonMonotonicSequenceException * if the lower bound is greater than, or equal to the upper bound * @throws org.apache.commons.math3.exception.NotStrictlyPositiveException * if the number of sample points is negative * @throws MathIllegalArgumentException if the number of sample points is not a power of two */ public double[] transform(final UnivariateFunction f, final double min, final double max, final int n, final TransformType type) { return transform(FunctionUtils.sample(f, min, max, n), type); } /** * Returns the forward transform of the specified integer data set.The * integer transform cannot be inverted directly, due to a scaling factor * which may lead to double results. * * @param f the integer data array to be transformed (signal) * @return the integer transformed array (spectrum) * @throws MathIllegalArgumentException if the length of the data array is not a power of two */ public int[] transform(final int[] f) { return fht(f); } /** * The FHT (Fast Hadamard Transformation) which uses only subtraction and * addition. Requires {@code N * log2(N) = n * 2^n} additions. * *

                            Short Table of manual calculation for N=8

                            *
                              *
                            1. x is the input vector to be transformed,
                            2. *
                            3. y is the output vector (Fast Hadamard transform of x),
                            4. *
                            5. a and b are helper rows.
                            6. *
                            * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
                            xaby
                            x0a0 = x0 + x1b0 = a0 + a1y0 = b0+ b1
                            x1a1 = x2 + x3b0 = a2 + a3y0 = b2 + b3
                            x2a2 = x4 + x5b0 = a4 + a5y0 = b4 + b5
                            x3a3 = x6 + x7b0 = a6 + a7y0 = b6 + b7
                            x4a0 = x0 - x1b0 = a0 - a1y0 = b0 - b1
                            x5a1 = x2 - x3b0 = a2 - a3y0 = b2 - b3
                            x6a2 = x4 - x5b0 = a4 - a5y0 = b4 - b5
                            x7a3 = x6 - x7b0 = a6 - a7y0 = b6 - b7
                            * *

                            How it works

                            *
                              *
                            1. Construct a matrix with {@code N} rows and {@code n + 1} columns, * {@code hadm[n+1][N]}.
                              * (If I use [x][y] it always means [row-offset][column-offset] of a * Matrix with n rows and m columns. Its entries go from M[0][0] * to M[n][N])
                            2. *
                            3. Place the input vector {@code x[N]} in the first column of the * matrix {@code hadm}.
                            4. *
                            5. The entries of the submatrix {@code D_top} are calculated as follows *
                                *
                              • {@code D_top} goes from entry {@code [0][1]} to * {@code [N / 2 - 1][n + 1]},
                              • *
                              • the columns of {@code D_top} are the pairwise mutually * exclusive sums of the previous column.
                              • *
                              *
                            6. *
                            7. The entries of the submatrix {@code D_bottom} are calculated as * follows *
                                *
                              • {@code D_bottom} goes from entry {@code [N / 2][1]} to * {@code [N][n + 1]},
                              • *
                              • the columns of {@code D_bottom} are the pairwise differences * of the previous column.
                              • *
                              *
                            8. *
                            9. The consputation of {@code D_top} and {@code D_bottom} are best * understood with the above example (for {@code N = 8}). *
                            10. The output vector {@code y} is now in the last column of * {@code hadm}.
                            11. *
                            12. Algorithm from chipcenter.
                            13. *
                            *

                            Visually

                            * * * * * * * * * * * * * * * * * * * * * * * * * * *
                            0123n + 1
                            0x0 * ↑
                            * ← Dtop
                            * ↓ *
                            1x1
                            2x2
                            N / 2 - 1xN/2-1
                            N / 2xN/2 * ↑
                            * ← Dbottom
                            * ↓ *
                            N / 2 + 1xN/2+1
                            N / 2 + 2xN/2+2
                            NxN
                            * * @param x the real data array to be transformed * @return the real transformed array, {@code y} * @throws MathIllegalArgumentException if the length of the data array is not a power of two */ protected double[] fht(double[] x) throws MathIllegalArgumentException { final int n = x.length; final int halfN = n / 2; if (!ArithmeticUtils.isPowerOfTwo(n)) { throw new MathIllegalArgumentException( LocalizedFormats.NOT_POWER_OF_TWO, Integer.valueOf(n)); } /* * Instead of creating a matrix with p+1 columns and n rows, we use two * one dimension arrays which we are used in an alternating way. */ double[] yPrevious = new double[n]; double[] yCurrent = x.clone(); // iterate from left to right (column) for (int j = 1; j < n; j <<= 1) { // switch columns final double[] yTmp = yCurrent; yCurrent = yPrevious; yPrevious = yTmp; // iterate from top to bottom (row) for (int i = 0; i < halfN; ++i) { // Dtop: the top part works with addition final int twoI = 2 * i; yCurrent[i] = yPrevious[twoI] + yPrevious[twoI + 1]; } for (int i = halfN; i < n; ++i) { // Dbottom: the bottom part works with subtraction final int twoI = 2 * i; yCurrent[i] = yPrevious[twoI - n] - yPrevious[twoI - n + 1]; } } return yCurrent; } /** * Returns the forward transform of the specified integer data set. The FHT * (Fast Hadamard Transform) uses only subtraction and addition. * * @param x the integer data array to be transformed * @return the integer transformed array, {@code y} * @throws MathIllegalArgumentException if the length of the data array is not a power of two */ protected int[] fht(int[] x) throws MathIllegalArgumentException { final int n = x.length; final int halfN = n / 2; if (!ArithmeticUtils.isPowerOfTwo(n)) { throw new MathIllegalArgumentException( LocalizedFormats.NOT_POWER_OF_TWO, Integer.valueOf(n)); } /* * Instead of creating a matrix with p+1 columns and n rows, we use two * one dimension arrays which we are used in an alternating way. */ int[] yPrevious = new int[n]; int[] yCurrent = x.clone(); // iterate from left to right (column) for (int j = 1; j < n; j <<= 1) { // switch columns final int[] yTmp = yCurrent; yCurrent = yPrevious; yPrevious = yTmp; // iterate from top to bottom (row) for (int i = 0; i < halfN; ++i) { // Dtop: the top part works with addition final int twoI = 2 * i; yCurrent[i] = yPrevious[twoI] + yPrevious[twoI + 1]; } for (int i = halfN; i < n; ++i) { // Dbottom: the bottom part works with subtraction final int twoI = 2 * i; yCurrent[i] = yPrevious[twoI - n] - yPrevious[twoI - n + 1]; } } // return the last computed output vector y return yCurrent; } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/transform/FastFourierTransformer.java100644 1750 1750 66155 12126627720 32110 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.transform; import java.io.Serializable; import java.lang.reflect.Array; import org.apache.commons.math3.analysis.FunctionUtils; import org.apache.commons.math3.analysis.UnivariateFunction; import org.apache.commons.math3.complex.Complex; import org.apache.commons.math3.exception.DimensionMismatchException; import org.apache.commons.math3.exception.MathIllegalArgumentException; import org.apache.commons.math3.exception.MathIllegalStateException; import org.apache.commons.math3.exception.util.LocalizedFormats; import org.apache.commons.math3.util.ArithmeticUtils; import org.apache.commons.math3.util.FastMath; import org.apache.commons.math3.util.MathArrays; /** * Implements the Fast Fourier Transform for transformation of one-dimensional * real or complex data sets. For reference, see Applied Numerical Linear * Algebra, ISBN 0898713897, chapter 6. *

                            * There are several variants of the discrete Fourier transform, with various * normalization conventions, which are specified by the parameter * {@link DftNormalization}. *

                            * The current implementation of the discrete Fourier transform as a fast * Fourier transform requires the length of the data set to be a power of 2. * This greatly simplifies and speeds up the code. Users can pad the data with * zeros to meet this requirement. There are other flavors of FFT, for * reference, see S. Winograd, * On computing the discrete Fourier transform, Mathematics of * Computation, 32 (1978), 175 - 199. * * @see DftNormalization * @version $Id: FastFourierTransformer.java 1385310 2012-09-16 16:32:10Z tn $ * @since 1.2 */ public class FastFourierTransformer implements Serializable { /** Serializable version identifier. */ static final long serialVersionUID = 20120210L; /** * {@code W_SUB_N_R[i]} is the real part of * {@code exp(- 2 * i * pi / n)}: * {@code W_SUB_N_R[i] = cos(2 * pi/ n)}, where {@code n = 2^i}. */ private static final double[] W_SUB_N_R = { 0x1.0p0, -0x1.0p0, 0x1.1a62633145c07p-54, 0x1.6a09e667f3bcdp-1 , 0x1.d906bcf328d46p-1, 0x1.f6297cff75cbp-1, 0x1.fd88da3d12526p-1, 0x1.ff621e3796d7ep-1 , 0x1.ffd886084cd0dp-1, 0x1.fff62169b92dbp-1, 0x1.fffd8858e8a92p-1, 0x1.ffff621621d02p-1 , 0x1.ffffd88586ee6p-1, 0x1.fffff62161a34p-1, 0x1.fffffd8858675p-1, 0x1.ffffff621619cp-1 , 0x1.ffffffd885867p-1, 0x1.fffffff62161ap-1, 0x1.fffffffd88586p-1, 0x1.ffffffff62162p-1 , 0x1.ffffffffd8858p-1, 0x1.fffffffff6216p-1, 0x1.fffffffffd886p-1, 0x1.ffffffffff621p-1 , 0x1.ffffffffffd88p-1, 0x1.fffffffffff62p-1, 0x1.fffffffffffd9p-1, 0x1.ffffffffffff6p-1 , 0x1.ffffffffffffep-1, 0x1.fffffffffffffp-1, 0x1.0p0, 0x1.0p0 , 0x1.0p0, 0x1.0p0, 0x1.0p0, 0x1.0p0 , 0x1.0p0, 0x1.0p0, 0x1.0p0, 0x1.0p0 , 0x1.0p0, 0x1.0p0, 0x1.0p0, 0x1.0p0 , 0x1.0p0, 0x1.0p0, 0x1.0p0, 0x1.0p0 , 0x1.0p0, 0x1.0p0, 0x1.0p0, 0x1.0p0 , 0x1.0p0, 0x1.0p0, 0x1.0p0, 0x1.0p0 , 0x1.0p0, 0x1.0p0, 0x1.0p0, 0x1.0p0 , 0x1.0p0, 0x1.0p0, 0x1.0p0 }; /** * {@code W_SUB_N_I[i]} is the imaginary part of * {@code exp(- 2 * i * pi / n)}: * {@code W_SUB_N_I[i] = -sin(2 * pi/ n)}, where {@code n = 2^i}. */ private static final double[] W_SUB_N_I = { 0x1.1a62633145c07p-52, -0x1.1a62633145c07p-53, -0x1.0p0, -0x1.6a09e667f3bccp-1 , -0x1.87de2a6aea963p-2, -0x1.8f8b83c69a60ap-3, -0x1.917a6bc29b42cp-4, -0x1.91f65f10dd814p-5 , -0x1.92155f7a3667ep-6, -0x1.921d1fcdec784p-7, -0x1.921f0fe670071p-8, -0x1.921f8becca4bap-9 , -0x1.921faaee6472dp-10, -0x1.921fb2aecb36p-11, -0x1.921fb49ee4ea6p-12, -0x1.921fb51aeb57bp-13 , -0x1.921fb539ecf31p-14, -0x1.921fb541ad59ep-15, -0x1.921fb5439d73ap-16, -0x1.921fb544197ap-17 , -0x1.921fb544387bap-18, -0x1.921fb544403c1p-19, -0x1.921fb544422c2p-20, -0x1.921fb54442a83p-21 , -0x1.921fb54442c73p-22, -0x1.921fb54442cefp-23, -0x1.921fb54442d0ep-24, -0x1.921fb54442d15p-25 , -0x1.921fb54442d17p-26, -0x1.921fb54442d18p-27, -0x1.921fb54442d18p-28, -0x1.921fb54442d18p-29 , -0x1.921fb54442d18p-30, -0x1.921fb54442d18p-31, -0x1.921fb54442d18p-32, -0x1.921fb54442d18p-33 , -0x1.921fb54442d18p-34, -0x1.921fb54442d18p-35, -0x1.921fb54442d18p-36, -0x1.921fb54442d18p-37 , -0x1.921fb54442d18p-38, -0x1.921fb54442d18p-39, -0x1.921fb54442d18p-40, -0x1.921fb54442d18p-41 , -0x1.921fb54442d18p-42, -0x1.921fb54442d18p-43, -0x1.921fb54442d18p-44, -0x1.921fb54442d18p-45 , -0x1.921fb54442d18p-46, -0x1.921fb54442d18p-47, -0x1.921fb54442d18p-48, -0x1.921fb54442d18p-49 , -0x1.921fb54442d18p-50, -0x1.921fb54442d18p-51, -0x1.921fb54442d18p-52, -0x1.921fb54442d18p-53 , -0x1.921fb54442d18p-54, -0x1.921fb54442d18p-55, -0x1.921fb54442d18p-56, -0x1.921fb54442d18p-57 , -0x1.921fb54442d18p-58, -0x1.921fb54442d18p-59, -0x1.921fb54442d18p-60 }; /** The type of DFT to be performed. */ private final DftNormalization normalization; /** * Creates a new instance of this class, with various normalization * conventions. * * @param normalization the type of normalization to be applied to the * transformed data */ public FastFourierTransformer(final DftNormalization normalization) { this.normalization = normalization; } /** * Performs identical index bit reversal shuffles on two arrays of identical * size. Each element in the array is swapped with another element based on * the bit-reversal of the index. For example, in an array with length 16, * item at binary index 0011 (decimal 3) would be swapped with the item at * binary index 1100 (decimal 12). * * @param a the first array to be shuffled * @param b the second array to be shuffled */ private static void bitReversalShuffle2(double[] a, double[] b) { final int n = a.length; assert b.length == n; final int halfOfN = n >> 1; int j = 0; for (int i = 0; i < n; i++) { if (i < j) { // swap indices i & j double temp = a[i]; a[i] = a[j]; a[j] = temp; temp = b[i]; b[i] = b[j]; b[j] = temp; } int k = halfOfN; while (k <= j && k > 0) { j -= k; k >>= 1; } j += k; } } /** * Applies the proper normalization to the specified transformed data. * * @param dataRI the unscaled transformed data * @param normalization the normalization to be applied * @param type the type of transform (forward, inverse) which resulted in the specified data */ private static void normalizeTransformedData(final double[][] dataRI, final DftNormalization normalization, final TransformType type) { final double[] dataR = dataRI[0]; final double[] dataI = dataRI[1]; final int n = dataR.length; assert dataI.length == n; switch (normalization) { case STANDARD: if (type == TransformType.INVERSE) { final double scaleFactor = 1.0 / ((double) n); for (int i = 0; i < n; i++) { dataR[i] *= scaleFactor; dataI[i] *= scaleFactor; } } break; case UNITARY: final double scaleFactor = 1.0 / FastMath.sqrt(n); for (int i = 0; i < n; i++) { dataR[i] *= scaleFactor; dataI[i] *= scaleFactor; } break; default: /* * This should never occur in normal conditions. However this * clause has been added as a safeguard if other types of * normalizations are ever implemented, and the corresponding * test is forgotten in the present switch. */ throw new MathIllegalStateException(); } } /** * Computes the standard transform of the specified complex data. The * computation is done in place. The input data is laid out as follows *

                              *
                            • {@code dataRI[0][i]} is the real part of the {@code i}-th data point,
                            • *
                            • {@code dataRI[1][i]} is the imaginary part of the {@code i}-th data point.
                            • *
                            * * @param dataRI the two dimensional array of real and imaginary parts of the data * @param normalization the normalization to be applied to the transformed data * @param type the type of transform (forward, inverse) to be performed * @throws DimensionMismatchException if the number of rows of the specified * array is not two, or the array is not rectangular * @throws MathIllegalArgumentException if the number of data points is not * a power of two */ public static void transformInPlace(final double[][] dataRI, final DftNormalization normalization, final TransformType type) { if (dataRI.length != 2) { throw new DimensionMismatchException(dataRI.length, 2); } final double[] dataR = dataRI[0]; final double[] dataI = dataRI[1]; if (dataR.length != dataI.length) { throw new DimensionMismatchException(dataI.length, dataR.length); } final int n = dataR.length; if (!ArithmeticUtils.isPowerOfTwo(n)) { throw new MathIllegalArgumentException( LocalizedFormats.NOT_POWER_OF_TWO_CONSIDER_PADDING, Integer.valueOf(n)); } if (n == 1) { return; } else if (n == 2) { final double srcR0 = dataR[0]; final double srcI0 = dataI[0]; final double srcR1 = dataR[1]; final double srcI1 = dataI[1]; // X_0 = x_0 + x_1 dataR[0] = srcR0 + srcR1; dataI[0] = srcI0 + srcI1; // X_1 = x_0 - x_1 dataR[1] = srcR0 - srcR1; dataI[1] = srcI0 - srcI1; normalizeTransformedData(dataRI, normalization, type); return; } bitReversalShuffle2(dataR, dataI); // Do 4-term DFT. if (type == TransformType.INVERSE) { for (int i0 = 0; i0 < n; i0 += 4) { final int i1 = i0 + 1; final int i2 = i0 + 2; final int i3 = i0 + 3; final double srcR0 = dataR[i0]; final double srcI0 = dataI[i0]; final double srcR1 = dataR[i2]; final double srcI1 = dataI[i2]; final double srcR2 = dataR[i1]; final double srcI2 = dataI[i1]; final double srcR3 = dataR[i3]; final double srcI3 = dataI[i3]; // 4-term DFT // X_0 = x_0 + x_1 + x_2 + x_3 dataR[i0] = srcR0 + srcR1 + srcR2 + srcR3; dataI[i0] = srcI0 + srcI1 + srcI2 + srcI3; // X_1 = x_0 - x_2 + j * (x_3 - x_1) dataR[i1] = srcR0 - srcR2 + (srcI3 - srcI1); dataI[i1] = srcI0 - srcI2 + (srcR1 - srcR3); // X_2 = x_0 - x_1 + x_2 - x_3 dataR[i2] = srcR0 - srcR1 + srcR2 - srcR3; dataI[i2] = srcI0 - srcI1 + srcI2 - srcI3; // X_3 = x_0 - x_2 + j * (x_1 - x_3) dataR[i3] = srcR0 - srcR2 + (srcI1 - srcI3); dataI[i3] = srcI0 - srcI2 + (srcR3 - srcR1); } } else { for (int i0 = 0; i0 < n; i0 += 4) { final int i1 = i0 + 1; final int i2 = i0 + 2; final int i3 = i0 + 3; final double srcR0 = dataR[i0]; final double srcI0 = dataI[i0]; final double srcR1 = dataR[i2]; final double srcI1 = dataI[i2]; final double srcR2 = dataR[i1]; final double srcI2 = dataI[i1]; final double srcR3 = dataR[i3]; final double srcI3 = dataI[i3]; // 4-term DFT // X_0 = x_0 + x_1 + x_2 + x_3 dataR[i0] = srcR0 + srcR1 + srcR2 + srcR3; dataI[i0] = srcI0 + srcI1 + srcI2 + srcI3; // X_1 = x_0 - x_2 + j * (x_3 - x_1) dataR[i1] = srcR0 - srcR2 + (srcI1 - srcI3); dataI[i1] = srcI0 - srcI2 + (srcR3 - srcR1); // X_2 = x_0 - x_1 + x_2 - x_3 dataR[i2] = srcR0 - srcR1 + srcR2 - srcR3; dataI[i2] = srcI0 - srcI1 + srcI2 - srcI3; // X_3 = x_0 - x_2 + j * (x_1 - x_3) dataR[i3] = srcR0 - srcR2 + (srcI3 - srcI1); dataI[i3] = srcI0 - srcI2 + (srcR1 - srcR3); } } int lastN0 = 4; int lastLogN0 = 2; while (lastN0 < n) { int n0 = lastN0 << 1; int logN0 = lastLogN0 + 1; double wSubN0R = W_SUB_N_R[logN0]; double wSubN0I = W_SUB_N_I[logN0]; if (type == TransformType.INVERSE) { wSubN0I = -wSubN0I; } // Combine even/odd transforms of size lastN0 into a transform of size N0 (lastN0 * 2). for (int destEvenStartIndex = 0; destEvenStartIndex < n; destEvenStartIndex += n0) { int destOddStartIndex = destEvenStartIndex + lastN0; double wSubN0ToRR = 1; double wSubN0ToRI = 0; for (int r = 0; r < lastN0; r++) { double grR = dataR[destEvenStartIndex + r]; double grI = dataI[destEvenStartIndex + r]; double hrR = dataR[destOddStartIndex + r]; double hrI = dataI[destOddStartIndex + r]; // dest[destEvenStartIndex + r] = Gr + WsubN0ToR * Hr dataR[destEvenStartIndex + r] = grR + wSubN0ToRR * hrR - wSubN0ToRI * hrI; dataI[destEvenStartIndex + r] = grI + wSubN0ToRR * hrI + wSubN0ToRI * hrR; // dest[destOddStartIndex + r] = Gr - WsubN0ToR * Hr dataR[destOddStartIndex + r] = grR - (wSubN0ToRR * hrR - wSubN0ToRI * hrI); dataI[destOddStartIndex + r] = grI - (wSubN0ToRR * hrI + wSubN0ToRI * hrR); // WsubN0ToR *= WsubN0R double nextWsubN0ToRR = wSubN0ToRR * wSubN0R - wSubN0ToRI * wSubN0I; double nextWsubN0ToRI = wSubN0ToRR * wSubN0I + wSubN0ToRI * wSubN0R; wSubN0ToRR = nextWsubN0ToRR; wSubN0ToRI = nextWsubN0ToRI; } } lastN0 = n0; lastLogN0 = logN0; } normalizeTransformedData(dataRI, normalization, type); } /** * Returns the (forward, inverse) transform of the specified real data set. * * @param f the real data array to be transformed * @param type the type of transform (forward, inverse) to be performed * @return the complex transformed array * @throws MathIllegalArgumentException if the length of the data array is not a power of two */ public Complex[] transform(final double[] f, final TransformType type) { final double[][] dataRI = new double[][] { MathArrays.copyOf(f, f.length), new double[f.length] }; transformInPlace(dataRI, normalization, type); return TransformUtils.createComplexArray(dataRI); } /** * Returns the (forward, inverse) transform of the specified real function, * sampled on the specified interval. * * @param f the function to be sampled and transformed * @param min the (inclusive) lower bound for the interval * @param max the (exclusive) upper bound for the interval * @param n the number of sample points * @param type the type of transform (forward, inverse) to be performed * @return the complex transformed array * @throws org.apache.commons.math3.exception.NumberIsTooLargeException * if the lower bound is greater than, or equal to the upper bound * @throws org.apache.commons.math3.exception.NotStrictlyPositiveException * if the number of sample points {@code n} is negative * @throws MathIllegalArgumentException if the number of sample points * {@code n} is not a power of two */ public Complex[] transform(final UnivariateFunction f, final double min, final double max, final int n, final TransformType type) { final double[] data = FunctionUtils.sample(f, min, max, n); return transform(data, type); } /** * Returns the (forward, inverse) transform of the specified complex data set. * * @param f the complex data array to be transformed * @param type the type of transform (forward, inverse) to be performed * @return the complex transformed array * @throws MathIllegalArgumentException if the length of the data array is not a power of two */ public Complex[] transform(final Complex[] f, final TransformType type) { final double[][] dataRI = TransformUtils.createRealImaginaryArray(f); transformInPlace(dataRI, normalization, type); return TransformUtils.createComplexArray(dataRI); } /** * Performs a multi-dimensional Fourier transform on a given array. Use * {@link #transform(Complex[], TransformType)} in a row-column * implementation in any number of dimensions with * O(N×log(N)) complexity with * N = n1 × n2 ×n3 × ... * × nd, where nk is the number of elements in * dimension k, and d is the total number of dimensions. * * @param mdca Multi-Dimensional Complex Array, i.e. {@code Complex[][][][]} * @param type the type of transform (forward, inverse) to be performed * @return transform of {@code mdca} as a Multi-Dimensional Complex Array, i.e. {@code Complex[][][][]} * @throws IllegalArgumentException if any dimension is not a power of two * @deprecated see MATH-736 */ @Deprecated public Object mdfft(Object mdca, TransformType type) { MultiDimensionalComplexMatrix mdcm = (MultiDimensionalComplexMatrix) new MultiDimensionalComplexMatrix(mdca).clone(); int[] dimensionSize = mdcm.getDimensionSizes(); //cycle through each dimension for (int i = 0; i < dimensionSize.length; i++) { mdfft(mdcm, type, i, new int[0]); } return mdcm.getArray(); } /** * Performs one dimension of a multi-dimensional Fourier transform. * * @param mdcm input matrix * @param type the type of transform (forward, inverse) to be performed * @param d index of the dimension to process * @param subVector recursion subvector * @throws IllegalArgumentException if any dimension is not a power of two * @deprecated see MATH-736 */ @Deprecated private void mdfft(MultiDimensionalComplexMatrix mdcm, TransformType type, int d, int[] subVector) { int[] dimensionSize = mdcm.getDimensionSizes(); //if done if (subVector.length == dimensionSize.length) { Complex[] temp = new Complex[dimensionSize[d]]; for (int i = 0; i < dimensionSize[d]; i++) { //fft along dimension d subVector[d] = i; temp[i] = mdcm.get(subVector); } temp = transform(temp, type); for (int i = 0; i < dimensionSize[d]; i++) { subVector[d] = i; mdcm.set(temp[i], subVector); } } else { int[] vector = new int[subVector.length + 1]; System.arraycopy(subVector, 0, vector, 0, subVector.length); if (subVector.length == d) { //value is not important once the recursion is done. //then an fft will be applied along the dimension d. vector[d] = 0; mdfft(mdcm, type, d, vector); } else { for (int i = 0; i < dimensionSize[subVector.length]; i++) { vector[subVector.length] = i; //further split along the next dimension mdfft(mdcm, type, d, vector); } } } } /** * Complex matrix implementation. Not designed for synchronized access may * eventually be replaced by jsr-83 of the java community process * http://jcp.org/en/jsr/detail?id=83 * may require additional exception throws for other basic requirements. * * @deprecated see MATH-736 */ @Deprecated private static class MultiDimensionalComplexMatrix implements Cloneable { /** Size in all dimensions. */ protected int[] dimensionSize; /** Storage array. */ protected Object multiDimensionalComplexArray; /** * Simple constructor. * * @param multiDimensionalComplexArray array containing the matrix * elements */ public MultiDimensionalComplexMatrix( Object multiDimensionalComplexArray) { this.multiDimensionalComplexArray = multiDimensionalComplexArray; // count dimensions int numOfDimensions = 0; for (Object lastDimension = multiDimensionalComplexArray; lastDimension instanceof Object[];) { final Object[] array = (Object[]) lastDimension; numOfDimensions++; lastDimension = array[0]; } // allocate array with exact count dimensionSize = new int[numOfDimensions]; // fill array numOfDimensions = 0; for (Object lastDimension = multiDimensionalComplexArray; lastDimension instanceof Object[];) { final Object[] array = (Object[]) lastDimension; dimensionSize[numOfDimensions++] = array.length; lastDimension = array[0]; } } /** * Get a matrix element. * * @param vector indices of the element * @return matrix element * @exception DimensionMismatchException if dimensions do not match */ public Complex get(int... vector) throws DimensionMismatchException { if (vector == null) { if (dimensionSize.length > 0) { throw new DimensionMismatchException( 0, dimensionSize.length); } return null; } if (vector.length != dimensionSize.length) { throw new DimensionMismatchException( vector.length, dimensionSize.length); } Object lastDimension = multiDimensionalComplexArray; for (int i = 0; i < dimensionSize.length; i++) { lastDimension = ((Object[]) lastDimension)[vector[i]]; } return (Complex) lastDimension; } /** * Set a matrix element. * * @param magnitude magnitude of the element * @param vector indices of the element * @return the previous value * @exception DimensionMismatchException if dimensions do not match */ public Complex set(Complex magnitude, int... vector) throws DimensionMismatchException { if (vector == null) { if (dimensionSize.length > 0) { throw new DimensionMismatchException( 0, dimensionSize.length); } return null; } if (vector.length != dimensionSize.length) { throw new DimensionMismatchException( vector.length, dimensionSize.length); } Object[] lastDimension = (Object[]) multiDimensionalComplexArray; for (int i = 0; i < dimensionSize.length - 1; i++) { lastDimension = (Object[]) lastDimension[vector[i]]; } Complex lastValue = (Complex) lastDimension[vector[dimensionSize.length - 1]]; lastDimension[vector[dimensionSize.length - 1]] = magnitude; return lastValue; } /** * Get the size in all dimensions. * * @return size in all dimensions */ public int[] getDimensionSizes() { return dimensionSize.clone(); } /** * Get the underlying storage array. * * @return underlying storage array */ public Object getArray() { return multiDimensionalComplexArray; } /** {@inheritDoc} */ @Override public Object clone() { MultiDimensionalComplexMatrix mdcm = new MultiDimensionalComplexMatrix(Array.newInstance( Complex.class, dimensionSize)); clone(mdcm); return mdcm; } /** * Copy contents of current array into mdcm. * * @param mdcm array where to copy data */ private void clone(MultiDimensionalComplexMatrix mdcm) { int[] vector = new int[dimensionSize.length]; int size = 1; for (int i = 0; i < dimensionSize.length; i++) { size *= dimensionSize[i]; } int[][] vectorList = new int[size][dimensionSize.length]; for (int[] nextVector : vectorList) { System.arraycopy(vector, 0, nextVector, 0, dimensionSize.length); for (int i = 0; i < dimensionSize.length; i++) { vector[i]++; if (vector[i] < dimensionSize[i]) { break; } else { vector[i] = 0; } } } for (int[] nextVector : vectorList) { mdcm.set(get(nextVector), nextVector); } } } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/filter/package-info.java100644 1750 1750 1617 12126627710 27201 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ /** * Implementations of common discrete-time linear filters. */ package org.apache.commons.math3.filter; commons-math3-3.2-src/src/main/java/org/apache/commons/math3/filter/ProcessModel.java100644 1750 1750 4746 12126627710 27262 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.filter; import org.apache.commons.math3.linear.RealMatrix; import org.apache.commons.math3.linear.RealVector; /** * Defines the process dynamics model for the use with a {@link KalmanFilter}. * * @since 3.0 * @version $Id: ProcessModel.java 1416643 2012-12-03 19:37:14Z tn $ */ public interface ProcessModel { /** * Returns the state transition matrix. * * @return the state transition matrix */ RealMatrix getStateTransitionMatrix(); /** * Returns the control matrix. * * @return the control matrix */ RealMatrix getControlMatrix(); /** * Returns the process noise matrix. This method is called by the {@link KalmanFilter} every * prediction step, so implementations of this interface may return a modified process noise * depending on the current iteration step. * * @return the process noise matrix * @see KalmanFilter#predict() * @see KalmanFilter#predict(double[]) * @see KalmanFilter#predict(RealVector) */ RealMatrix getProcessNoise(); /** * Returns the initial state estimation vector. *

                            * Note: if the return value is zero, the Kalman filter will initialize the * state estimation with a zero vector. * * @return the initial state estimation vector */ RealVector getInitialStateEstimate(); /** * Returns the initial error covariance matrix. *

                            * Note: if the return value is zero, the Kalman filter will initialize the * error covariance with the process noise matrix. * * @return the initial error covariance matrix */ RealMatrix getInitialErrorCovariance(); } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/filter/DefaultProcessModel.java100644 1750 1750 14671 12126627710 30605 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.filter; import org.apache.commons.math3.exception.DimensionMismatchException; import org.apache.commons.math3.exception.NoDataException; import org.apache.commons.math3.exception.NullArgumentException; import org.apache.commons.math3.linear.Array2DRowRealMatrix; import org.apache.commons.math3.linear.ArrayRealVector; import org.apache.commons.math3.linear.RealMatrix; import org.apache.commons.math3.linear.RealVector; /** * Default implementation of a {@link ProcessModel} for the use with a {@link KalmanFilter}. * * @since 3.0 * @version $Id: DefaultProcessModel.java 1416643 2012-12-03 19:37:14Z tn $ */ public class DefaultProcessModel implements ProcessModel { /** * The state transition matrix, used to advance the internal state estimation each time-step. */ private RealMatrix stateTransitionMatrix; /** * The control matrix, used to integrate a control input into the state estimation. */ private RealMatrix controlMatrix; /** The process noise covariance matrix. */ private RealMatrix processNoiseCovMatrix; /** The initial state estimation of the observed process. */ private RealVector initialStateEstimateVector; /** The initial error covariance matrix of the observed process. */ private RealMatrix initialErrorCovMatrix; /** * Create a new {@link ProcessModel}, taking double arrays as input parameters. * * @param stateTransition * the state transition matrix * @param control * the control matrix * @param processNoise * the process noise matrix * @param initialStateEstimate * the initial state estimate vector * @param initialErrorCovariance * the initial error covariance matrix * @throws NullArgumentException * if any of the input arrays is {@code null} * @throws NoDataException * if any row / column dimension of the input matrices is zero * @throws DimensionMismatchException * if any of the input matrices is non-rectangular */ public DefaultProcessModel(final double[][] stateTransition, final double[][] control, final double[][] processNoise, final double[] initialStateEstimate, final double[][] initialErrorCovariance) throws NullArgumentException, NoDataException, DimensionMismatchException { this(new Array2DRowRealMatrix(stateTransition), new Array2DRowRealMatrix(control), new Array2DRowRealMatrix(processNoise), new ArrayRealVector(initialStateEstimate), new Array2DRowRealMatrix(initialErrorCovariance)); } /** * Create a new {@link ProcessModel}, taking double arrays as input parameters. *

                            * The initial state estimate and error covariance are omitted and will be initialized by the * {@link KalmanFilter} to default values. * * @param stateTransition * the state transition matrix * @param control * the control matrix * @param processNoise * the process noise matrix * @throws NullArgumentException * if any of the input arrays is {@code null} * @throws NoDataException * if any row / column dimension of the input matrices is zero * @throws DimensionMismatchException * if any of the input matrices is non-rectangular */ public DefaultProcessModel(final double[][] stateTransition, final double[][] control, final double[][] processNoise) throws NullArgumentException, NoDataException, DimensionMismatchException { this(new Array2DRowRealMatrix(stateTransition), new Array2DRowRealMatrix(control), new Array2DRowRealMatrix(processNoise), null, null); } /** * Create a new {@link ProcessModel}, taking double arrays as input parameters. * * @param stateTransition * the state transition matrix * @param control * the control matrix * @param processNoise * the process noise matrix * @param initialStateEstimate * the initial state estimate vector * @param initialErrorCovariance * the initial error covariance matrix */ public DefaultProcessModel(final RealMatrix stateTransition, final RealMatrix control, final RealMatrix processNoise, final RealVector initialStateEstimate, final RealMatrix initialErrorCovariance) { this.stateTransitionMatrix = stateTransition; this.controlMatrix = control; this.processNoiseCovMatrix = processNoise; this.initialStateEstimateVector = initialStateEstimate; this.initialErrorCovMatrix = initialErrorCovariance; } /** {@inheritDoc} */ public RealMatrix getStateTransitionMatrix() { return stateTransitionMatrix; } /** {@inheritDoc} */ public RealMatrix getControlMatrix() { return controlMatrix; } /** {@inheritDoc} */ public RealMatrix getProcessNoise() { return processNoiseCovMatrix; } /** {@inheritDoc} */ public RealVector getInitialStateEstimate() { return initialStateEstimateVector; } /** {@inheritDoc} */ public RealMatrix getInitialErrorCovariance() { return initialErrorCovMatrix; } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/filter/MeasurementModel.java100644 1750 1750 3272 12126627710 30122 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.filter; import org.apache.commons.math3.linear.RealMatrix; /** * Defines the measurement model for the use with a {@link KalmanFilter}. * * @since 3.0 * @version $Id: MeasurementModel.java 1416643 2012-12-03 19:37:14Z tn $ */ public interface MeasurementModel { /** * Returns the measurement matrix. * * @return the measurement matrix */ RealMatrix getMeasurementMatrix(); /** * Returns the measurement noise matrix. This method is called by the {@link KalmanFilter} every * correction step, so implementations of this interface may return a modified measurement noise * depending on the current iteration step. * * @return the measurement noise matrix * @see KalmanFilter#correct(double[]) * @see KalmanFilter#correct(org.apache.commons.math3.linear.RealVector) */ RealMatrix getMeasurementNoise(); } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/filter/DefaultMeasurementModel.java100644 1750 1750 6344 12126627710 31432 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.filter; import org.apache.commons.math3.exception.DimensionMismatchException; import org.apache.commons.math3.exception.NoDataException; import org.apache.commons.math3.exception.NullArgumentException; import org.apache.commons.math3.linear.Array2DRowRealMatrix; import org.apache.commons.math3.linear.RealMatrix; /** * Default implementation of a {@link MeasurementModel} for the use with a {@link KalmanFilter}. * * @since 3.0 * @version $Id: DefaultMeasurementModel.java 1416643 2012-12-03 19:37:14Z tn $ */ public class DefaultMeasurementModel implements MeasurementModel { /** * The measurement matrix, used to associate the measurement vector to the * internal state estimation vector. */ private RealMatrix measurementMatrix; /** * The measurement noise covariance matrix. */ private RealMatrix measurementNoise; /** * Create a new {@link MeasurementModel}, taking double arrays as input parameters for the * respective measurement matrix and noise. * * @param measMatrix * the measurement matrix * @param measNoise * the measurement noise matrix * @throws NullArgumentException * if any of the input matrices is {@code null} * @throws NoDataException * if any row / column dimension of the input matrices is zero * @throws DimensionMismatchException * if any of the input matrices is non-rectangular */ public DefaultMeasurementModel(final double[][] measMatrix, final double[][] measNoise) throws NullArgumentException, NoDataException, DimensionMismatchException { this(new Array2DRowRealMatrix(measMatrix), new Array2DRowRealMatrix(measNoise)); } /** * Create a new {@link MeasurementModel}, taking {@link RealMatrix} objects * as input parameters for the respective measurement matrix and noise. * * @param measMatrix the measurement matrix * @param measNoise the measurement noise matrix */ public DefaultMeasurementModel(final RealMatrix measMatrix, final RealMatrix measNoise) { this.measurementMatrix = measMatrix; this.measurementNoise = measNoise; } /** {@inheritDoc} */ public RealMatrix getMeasurementMatrix() { return measurementMatrix; } /** {@inheritDoc} */ public RealMatrix getMeasurementNoise() { return measurementNoise; } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/filter/KalmanFilter.java100644 1750 1750 36712 12126627710 27252 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.filter; import org.apache.commons.math3.exception.DimensionMismatchException; import org.apache.commons.math3.exception.NullArgumentException; import org.apache.commons.math3.linear.Array2DRowRealMatrix; import org.apache.commons.math3.linear.ArrayRealVector; import org.apache.commons.math3.linear.CholeskyDecomposition; import org.apache.commons.math3.linear.DecompositionSolver; import org.apache.commons.math3.linear.MatrixDimensionMismatchException; import org.apache.commons.math3.linear.MatrixUtils; import org.apache.commons.math3.linear.NonSquareMatrixException; import org.apache.commons.math3.linear.RealMatrix; import org.apache.commons.math3.linear.RealVector; import org.apache.commons.math3.linear.SingularMatrixException; import org.apache.commons.math3.util.MathUtils; /** * Implementation of a Kalman filter to estimate the state xk * of a discrete-time controlled process that is governed by the linear * stochastic difference equation: * *

                             * xk = Axk-1 + Buk-1 + wk-1
                             * 
                            * * with a measurement xk that is * *
                             * zk = Hxk + vk.
                             * 
                            * *

                            * The random variables wk and vk represent * the process and measurement noise and are assumed to be independent of each * other and distributed with normal probability (white noise). *

                            * The Kalman filter cycle involves the following steps: *

                              *
                            1. predict: project the current state estimate ahead in time
                            2. *
                            3. correct: adjust the projected estimate by an actual measurement
                            4. *
                            *

                            * The Kalman filter is initialized with a {@link ProcessModel} and a * {@link MeasurementModel}, which contain the corresponding transformation and * noise covariance matrices. The parameter names used in the respective models * correspond to the following names commonly used in the mathematical * literature: *

                              *
                            • A - state transition matrix
                            • *
                            • B - control input matrix
                            • *
                            • H - measurement matrix
                            • *
                            • Q - process noise covariance matrix
                            • *
                            • R - measurement noise covariance matrix
                            • *
                            • P - error covariance matrix
                            • *
                            * * @see Kalman filter * resources * @see An * introduction to the Kalman filter by Greg Welch and Gary Bishop * @see * Kalman filter example by Dan Simon * @see ProcessModel * @see MeasurementModel * @since 3.0 * @version $Id: KalmanFilter.java 1416643 2012-12-03 19:37:14Z tn $ */ public class KalmanFilter { /** The process model used by this filter instance. */ private final ProcessModel processModel; /** The measurement model used by this filter instance. */ private final MeasurementModel measurementModel; /** The transition matrix, equivalent to A. */ private RealMatrix transitionMatrix; /** The transposed transition matrix. */ private RealMatrix transitionMatrixT; /** The control matrix, equivalent to B. */ private RealMatrix controlMatrix; /** The measurement matrix, equivalent to H. */ private RealMatrix measurementMatrix; /** The transposed measurement matrix. */ private RealMatrix measurementMatrixT; /** The internal state estimation vector, equivalent to x hat. */ private RealVector stateEstimation; /** The error covariance matrix, equivalent to P. */ private RealMatrix errorCovariance; /** * Creates a new Kalman filter with the given process and measurement models. * * @param process * the model defining the underlying process dynamics * @param measurement * the model defining the given measurement characteristics * @throws NullArgumentException * if any of the given inputs is null (except for the control matrix) * @throws NonSquareMatrixException * if the transition matrix is non square * @throws DimensionMismatchException * if the column dimension of the transition matrix does not match the dimension of the * initial state estimation vector * @throws MatrixDimensionMismatchException * if the matrix dimensions do not fit together */ public KalmanFilter(final ProcessModel process, final MeasurementModel measurement) throws NullArgumentException, NonSquareMatrixException, DimensionMismatchException, MatrixDimensionMismatchException { MathUtils.checkNotNull(process); MathUtils.checkNotNull(measurement); this.processModel = process; this.measurementModel = measurement; transitionMatrix = processModel.getStateTransitionMatrix(); MathUtils.checkNotNull(transitionMatrix); transitionMatrixT = transitionMatrix.transpose(); // create an empty matrix if no control matrix was given if (processModel.getControlMatrix() == null) { controlMatrix = new Array2DRowRealMatrix(); } else { controlMatrix = processModel.getControlMatrix(); } measurementMatrix = measurementModel.getMeasurementMatrix(); MathUtils.checkNotNull(measurementMatrix); measurementMatrixT = measurementMatrix.transpose(); // check that the process and measurement noise matrices are not null // they will be directly accessed from the model as they may change // over time RealMatrix processNoise = processModel.getProcessNoise(); MathUtils.checkNotNull(processNoise); RealMatrix measNoise = measurementModel.getMeasurementNoise(); MathUtils.checkNotNull(measNoise); // set the initial state estimate to a zero vector if it is not // available from the process model if (processModel.getInitialStateEstimate() == null) { stateEstimation = new ArrayRealVector(transitionMatrix.getColumnDimension()); } else { stateEstimation = processModel.getInitialStateEstimate(); } if (transitionMatrix.getColumnDimension() != stateEstimation.getDimension()) { throw new DimensionMismatchException(transitionMatrix.getColumnDimension(), stateEstimation.getDimension()); } // initialize the error covariance to the process noise if it is not // available from the process model if (processModel.getInitialErrorCovariance() == null) { errorCovariance = processNoise.copy(); } else { errorCovariance = processModel.getInitialErrorCovariance(); } // sanity checks, the control matrix B may be null // A must be a square matrix if (!transitionMatrix.isSquare()) { throw new NonSquareMatrixException( transitionMatrix.getRowDimension(), transitionMatrix.getColumnDimension()); } // row dimension of B must be equal to A if (controlMatrix != null && controlMatrix.getRowDimension() > 0 && controlMatrix.getColumnDimension() > 0 && (controlMatrix.getRowDimension() != transitionMatrix.getRowDimension() || controlMatrix.getColumnDimension() != 1)) { throw new MatrixDimensionMismatchException(controlMatrix.getRowDimension(), controlMatrix.getColumnDimension(), transitionMatrix.getRowDimension(), 1); } // Q must be equal to A MatrixUtils.checkAdditionCompatible(transitionMatrix, processNoise); // column dimension of H must be equal to row dimension of A if (measurementMatrix.getColumnDimension() != transitionMatrix.getRowDimension()) { throw new MatrixDimensionMismatchException(measurementMatrix.getRowDimension(), measurementMatrix.getColumnDimension(), measurementMatrix.getRowDimension(), transitionMatrix.getRowDimension()); } // row dimension of R must be equal to row dimension of H if (measNoise.getRowDimension() != measurementMatrix.getRowDimension() || measNoise.getColumnDimension() != 1) { throw new MatrixDimensionMismatchException(measNoise.getRowDimension(), measNoise.getColumnDimension(), measurementMatrix.getRowDimension(), 1); } } /** * Returns the dimension of the state estimation vector. * * @return the state dimension */ public int getStateDimension() { return stateEstimation.getDimension(); } /** * Returns the dimension of the measurement vector. * * @return the measurement vector dimension */ public int getMeasurementDimension() { return measurementMatrix.getRowDimension(); } /** * Returns the current state estimation vector. * * @return the state estimation vector */ public double[] getStateEstimation() { return stateEstimation.toArray(); } /** * Returns a copy of the current state estimation vector. * * @return the state estimation vector */ public RealVector getStateEstimationVector() { return stateEstimation.copy(); } /** * Returns the current error covariance matrix. * * @return the error covariance matrix */ public double[][] getErrorCovariance() { return errorCovariance.getData(); } /** * Returns a copy of the current error covariance matrix. * * @return the error covariance matrix */ public RealMatrix getErrorCovarianceMatrix() { return errorCovariance.copy(); } /** * Predict the internal state estimation one time step ahead. */ public void predict() { predict((RealVector) null); } /** * Predict the internal state estimation one time step ahead. * * @param u * the control vector * @throws DimensionMismatchException * if the dimension of the control vector does not fit */ public void predict(final double[] u) throws DimensionMismatchException { predict(new ArrayRealVector(u)); } /** * Predict the internal state estimation one time step ahead. * * @param u * the control vector * @throws DimensionMismatchException * if the dimension of the control vector does not match */ public void predict(final RealVector u) throws DimensionMismatchException { // sanity checks if (u != null && u.getDimension() != controlMatrix.getColumnDimension()) { throw new DimensionMismatchException(u.getDimension(), controlMatrix.getColumnDimension()); } // project the state estimation ahead (a priori state) // xHat(k)- = A * xHat(k-1) + B * u(k-1) stateEstimation = transitionMatrix.operate(stateEstimation); // add control input if it is available if (u != null) { stateEstimation = stateEstimation.add(controlMatrix.operate(u)); } // project the error covariance ahead // P(k)- = A * P(k-1) * A' + Q errorCovariance = transitionMatrix.multiply(errorCovariance) .multiply(transitionMatrixT) .add(processModel.getProcessNoise()); } /** * Correct the current state estimate with an actual measurement. * * @param z * the measurement vector * @throws NullArgumentException * if the measurement vector is {@code null} * @throws DimensionMismatchException * if the dimension of the measurement vector does not fit * @throws SingularMatrixException * if the covariance matrix could not be inverted */ public void correct(final double[] z) throws NullArgumentException, DimensionMismatchException, SingularMatrixException { correct(new ArrayRealVector(z)); } /** * Correct the current state estimate with an actual measurement. * * @param z * the measurement vector * @throws NullArgumentException * if the measurement vector is {@code null} * @throws DimensionMismatchException * if the dimension of the measurement vector does not fit * @throws SingularMatrixException * if the covariance matrix could not be inverted */ public void correct(final RealVector z) throws NullArgumentException, DimensionMismatchException, SingularMatrixException { // sanity checks MathUtils.checkNotNull(z); if (z.getDimension() != measurementMatrix.getRowDimension()) { throw new DimensionMismatchException(z.getDimension(), measurementMatrix.getRowDimension()); } // S = H * P(k) - * H' + R RealMatrix s = measurementMatrix.multiply(errorCovariance) .multiply(measurementMatrixT) .add(measurementModel.getMeasurementNoise()); // invert S // as the error covariance matrix is a symmetric positive // semi-definite matrix, we can use the cholesky decomposition DecompositionSolver solver = new CholeskyDecomposition(s).getSolver(); RealMatrix invertedS = solver.getInverse(); // Inn = z(k) - H * xHat(k)- RealVector innovation = z.subtract(measurementMatrix.operate(stateEstimation)); // calculate gain matrix // K(k) = P(k)- * H' * (H * P(k)- * H' + R)^-1 // K(k) = P(k)- * H' * S^-1 RealMatrix kalmanGain = errorCovariance.multiply(measurementMatrixT).multiply(invertedS); // update estimate with measurement z(k) // xHat(k) = xHat(k)- + K * Inn stateEstimation = stateEstimation.add(kalmanGain.operate(innovation)); // update covariance of prediction error // P(k) = (I - K * H) * P(k)- RealMatrix identity = MatrixUtils.createRealIdentityMatrix(kalmanGain.getRowDimension()); errorCovariance = identity.subtract(kalmanGain.multiply(measurementMatrix)).multiply(errorCovariance); } } ././@LongLink100644 0 0 146 12126630646 10262 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/analysis/interpolation/package-info.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/analysis/interpolation/package-info.jav100644 1750 1750 1645 12126627702 32267 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ /** * * Univariate real functions interpolation algorithms. * */ package org.apache.commons.math3.analysis.interpolation; ././@LongLink100644 0 0 154 12126630646 10261 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/analysis/interpolation/LinearInterpolator.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/analysis/interpolation/LinearInterpolat100644 1750 1750 6453 12126627702 32442 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.analysis.interpolation; import org.apache.commons.math3.analysis.polynomials.PolynomialFunction; import org.apache.commons.math3.analysis.polynomials.PolynomialSplineFunction; import org.apache.commons.math3.util.MathArrays; import org.apache.commons.math3.exception.DimensionMismatchException; import org.apache.commons.math3.exception.NumberIsTooSmallException; import org.apache.commons.math3.exception.NonMonotonicSequenceException; import org.apache.commons.math3.exception.util.LocalizedFormats; /** * Implements a linear function for interpolation of real univariate functions. * * @version $Id: LinearInterpolator.java 1379904 2012-09-01 23:54:52Z erans $ */ public class LinearInterpolator implements UnivariateInterpolator { /** * Computes a linear interpolating function for the data set. * * @param x the arguments for the interpolation points * @param y the values for the interpolation points * @return a function which interpolates the data set * @throws DimensionMismatchException if {@code x} and {@code y} * have different sizes. * @throws NonMonotonicSequenceException if {@code x} is not sorted in * strict increasing order. * @throws NumberIsTooSmallException if the size of {@code x} is smaller * than 2. */ public PolynomialSplineFunction interpolate(double x[], double y[]) throws DimensionMismatchException, NumberIsTooSmallException, NonMonotonicSequenceException { if (x.length != y.length) { throw new DimensionMismatchException(x.length, y.length); } if (x.length < 2) { throw new NumberIsTooSmallException(LocalizedFormats.NUMBER_OF_POINTS, x.length, 2, true); } // Number of intervals. The number of data points is n + 1. int n = x.length - 1; MathArrays.checkOrder(x); // Slope of the lines between the datapoints. final double m[] = new double[n]; for (int i = 0; i < n; i++) { m[i] = (y[i + 1] - y[i]) / (x[i + 1] - x[i]); } final PolynomialFunction polynomials[] = new PolynomialFunction[n]; final double coefficients[] = new double[2]; for (int i = 0; i < n; i++) { coefficients[0] = y[i]; coefficients[1] = m[i]; polynomials[i] = new PolynomialFunction(coefficients); } return new PolynomialSplineFunction(x, polynomials); } } ././@LongLink100644 0 0 162 12126630646 10260 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/analysis/interpolation/FieldHermiteInterpolator.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/analysis/interpolation/FieldHermiteInte100644 1750 1750 20650 12126627702 32362 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.analysis.interpolation; import java.util.ArrayList; import java.util.List; import org.apache.commons.math3.FieldElement; import org.apache.commons.math3.exception.DimensionMismatchException; import org.apache.commons.math3.exception.MathArithmeticException; import org.apache.commons.math3.exception.NoDataException; import org.apache.commons.math3.exception.NullArgumentException; import org.apache.commons.math3.exception.ZeroException; import org.apache.commons.math3.exception.util.LocalizedFormats; import org.apache.commons.math3.util.MathArrays; import org.apache.commons.math3.util.MathUtils; /** Polynomial interpolator using both sample values and sample derivatives. *

                            * The interpolation polynomials match all sample points, including both values * and provided derivatives. There is one polynomial for each component of * the values vector. All polynomials have the same degree. The degree of the * polynomials depends on the number of points and number of derivatives at each * point. For example the interpolation polynomials for n sample points without * any derivatives all have degree n-1. The interpolation polynomials for n * sample points with the two extreme points having value and first derivative * and the remaining points having value only all have degree n+1. The * interpolation polynomial for n sample points with value, first and second * derivative for all points all have degree 3n-1. *

                            * * @param Type of the field elements. * * @version $Id: FieldHermiteInterpolator.java 1455194 2013-03-11 15:45:54Z luc $ * @since 3.2 */ public class FieldHermiteInterpolator> { /** Sample abscissae. */ private final List abscissae; /** Top diagonal of the divided differences array. */ private final List topDiagonal; /** Bottom diagonal of the divided differences array. */ private final List bottomDiagonal; /** Create an empty interpolator. */ public FieldHermiteInterpolator() { this.abscissae = new ArrayList(); this.topDiagonal = new ArrayList(); this.bottomDiagonal = new ArrayList(); } /** Add a sample point. *

                            * This method must be called once for each sample point. It is allowed to * mix some calls with values only with calls with values and first * derivatives. *

                            *

                            * The point abscissae for all calls must be different. *

                            * @param x abscissa of the sample point * @param value value and derivatives of the sample point * (if only one row is passed, it is the value, if two rows are * passed the first one is the value and the second the derivative * and so on) * @exception ZeroException if the abscissa difference between added point * and a previous point is zero (i.e. the two points are at same abscissa) * @exception MathArithmeticException if the number of derivatives is larger * than 20, which prevents computation of a factorial * @throws DimensionMismatchException if derivative structures are inconsistent * @throws NullArgumentException if x is null */ public void addSamplePoint(final T x, final T[] ... value) throws ZeroException, MathArithmeticException, DimensionMismatchException, NullArgumentException { MathUtils.checkNotNull(x); T factorial = x.getField().getOne(); for (int i = 0; i < value.length; ++i) { final T[] y = value[i].clone(); if (i > 1) { factorial = factorial.multiply(i); final T inv = factorial.reciprocal(); for (int j = 0; j < y.length; ++j) { y[j] = y[j].multiply(inv); } } // update the bottom diagonal of the divided differences array final int n = abscissae.size(); bottomDiagonal.add(n - i, y); T[] bottom0 = y; for (int j = i; j < n; ++j) { final T[] bottom1 = bottomDiagonal.get(n - (j + 1)); if (x.equals(abscissae.get(n - (j + 1)))) { throw new ZeroException(LocalizedFormats.DUPLICATED_ABSCISSA_DIVISION_BY_ZERO, x); } final T inv = x.subtract(abscissae.get(n - (j + 1))).reciprocal(); for (int k = 0; k < y.length; ++k) { bottom1[k] = inv.multiply(bottom0[k].subtract(bottom1[k])); } bottom0 = bottom1; } // update the top diagonal of the divided differences array topDiagonal.add(bottom0.clone()); // update the abscissae array abscissae.add(x); } } /** Interpolate value at a specified abscissa. * @param x interpolation abscissa * @return interpolated value * @exception NoDataException if sample is empty * @throws NullArgumentException if x is null */ public T[] value(T x) throws NoDataException, NullArgumentException { // safety check MathUtils.checkNotNull(x); if (abscissae.isEmpty()) { throw new NoDataException(LocalizedFormats.EMPTY_INTERPOLATION_SAMPLE); } final T[] value = MathArrays.buildArray(x.getField(), topDiagonal.get(0).length); T valueCoeff = x.getField().getOne(); for (int i = 0; i < topDiagonal.size(); ++i) { T[] dividedDifference = topDiagonal.get(i); for (int k = 0; k < value.length; ++k) { value[k] = value[k].add(dividedDifference[k].multiply(valueCoeff)); } final T deltaX = x.subtract(abscissae.get(i)); valueCoeff = valueCoeff.multiply(deltaX); } return value; } /** Interpolate value and first derivatives at a specified abscissa. * @param x interpolation abscissa * @param order maximum derivation order * @return interpolated value and derivatives (value in row 0, * 1st derivative in row 1, ... nth derivative in row n) * @exception NoDataException if sample is empty * @throws NullArgumentException if x is null */ public T[][] derivatives(T x, int order) throws NoDataException, NullArgumentException { // safety check MathUtils.checkNotNull(x); if (abscissae.isEmpty()) { throw new NoDataException(LocalizedFormats.EMPTY_INTERPOLATION_SAMPLE); } final T zero = x.getField().getZero(); final T one = x.getField().getOne(); final T[] tj = MathArrays.buildArray(x.getField(), order + 1); tj[0] = zero; for (int i = 0; i < order; ++i) { tj[i + 1] = tj[i].add(one); } final T[][] derivatives = MathArrays.buildArray(x.getField(), order + 1, topDiagonal.get(0).length); final T[] valueCoeff = MathArrays.buildArray(x.getField(), order + 1); valueCoeff[0] = x.getField().getOne(); for (int i = 0; i < topDiagonal.size(); ++i) { T[] dividedDifference = topDiagonal.get(i); final T deltaX = x.subtract(abscissae.get(i)); for (int j = order; j >= 0; --j) { for (int k = 0; k < derivatives[j].length; ++k) { derivatives[j][k] = derivatives[j][k].add(dividedDifference[k].multiply(valueCoeff[j])); } valueCoeff[j] = valueCoeff[j].multiply(deltaX); if (j > 0) { valueCoeff[j] = valueCoeff[j].add(tj[j].multiply(valueCoeff[j - 1])); } } } return derivatives; } } ././@LongLink100644 0 0 155 12126630646 10262 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/analysis/interpolation/NevilleInterpolator.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/analysis/interpolation/NevilleInterpola100644 1750 1750 5103 12126627702 32431 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.analysis.interpolation; import java.io.Serializable; import org.apache.commons.math3.analysis.polynomials.PolynomialFunctionLagrangeForm; import org.apache.commons.math3.exception.DimensionMismatchException; import org.apache.commons.math3.exception.NumberIsTooSmallException; import org.apache.commons.math3.exception.NonMonotonicSequenceException; /** * Implements the * Neville's Algorithm for interpolation of real univariate functions. For * reference, see Introduction to Numerical Analysis, ISBN 038795452X, * chapter 2. *

                            * The actual code of Neville's algorithm is in PolynomialFunctionLagrangeForm, * this class provides an easy-to-use interface to it.

                            * * @version $Id: NevilleInterpolator.java 1379904 2012-09-01 23:54:52Z erans $ * @since 1.2 */ public class NevilleInterpolator implements UnivariateInterpolator, Serializable { /** serializable version identifier */ static final long serialVersionUID = 3003707660147873733L; /** * Computes an interpolating function for the data set. * * @param x Interpolating points. * @param y Interpolating values. * @return a function which interpolates the data set * @throws DimensionMismatchException if the array lengths are different. * @throws NumberIsTooSmallException if the number of points is less than 2. * @throws NonMonotonicSequenceException if two abscissae have the same * value. */ public PolynomialFunctionLagrangeForm interpolate(double x[], double y[]) throws DimensionMismatchException, NumberIsTooSmallException, NonMonotonicSequenceException { return new PolynomialFunctionLagrangeForm(x, y); } } ././@LongLink100644 0 0 206 12126630646 10257 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/analysis/interpolation/SmoothingPolynomialBicubicSplineInterpolator.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/analysis/interpolation/SmoothingPolynom100644 1750 1750 15271 12126627702 32531 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.analysis.interpolation; import org.apache.commons.math3.exception.DimensionMismatchException; import org.apache.commons.math3.exception.NoDataException; import org.apache.commons.math3.exception.NonMonotonicSequenceException; import org.apache.commons.math3.exception.NotPositiveException; import org.apache.commons.math3.exception.NullArgumentException; import org.apache.commons.math3.util.MathArrays; import org.apache.commons.math3.util.Precision; import org.apache.commons.math3.optim.nonlinear.vector.jacobian.GaussNewtonOptimizer; import org.apache.commons.math3.fitting.PolynomialFitter; import org.apache.commons.math3.analysis.polynomials.PolynomialFunction; import org.apache.commons.math3.optim.SimpleVectorValueChecker; /** * Generates a bicubic interpolation function. * Prior to generating the interpolating function, the input is smoothed using * polynomial fitting. * * @version $Id: SmoothingPolynomialBicubicSplineInterpolator.java 1455194 2013-03-11 15:45:54Z luc $ * @since 2.2 */ public class SmoothingPolynomialBicubicSplineInterpolator extends BicubicSplineInterpolator { /** Fitter for x. */ private final PolynomialFitter xFitter; /** Degree of the fitting polynomial. */ private final int xDegree; /** Fitter for y. */ private final PolynomialFitter yFitter; /** Degree of the fitting polynomial. */ private final int yDegree; /** * Default constructor. The degree of the fitting polynomials is set to 3. */ public SmoothingPolynomialBicubicSplineInterpolator() { this(3); } /** * @param degree Degree of the polynomial fitting functions. * @exception NotPositiveException if degree is not positive */ public SmoothingPolynomialBicubicSplineInterpolator(int degree) throws NotPositiveException { this(degree, degree); } /** * @param xDegree Degree of the polynomial fitting functions along the * x-dimension. * @param yDegree Degree of the polynomial fitting functions along the * y-dimension. * @exception NotPositiveException if degrees are not positive */ public SmoothingPolynomialBicubicSplineInterpolator(int xDegree, int yDegree) throws NotPositiveException { if (xDegree < 0) { throw new NotPositiveException(xDegree); } if (yDegree < 0) { throw new NotPositiveException(yDegree); } this.xDegree = xDegree; this.yDegree = yDegree; final double safeFactor = 1e2; final SimpleVectorValueChecker checker = new SimpleVectorValueChecker(safeFactor * Precision.EPSILON, safeFactor * Precision.SAFE_MIN); xFitter = new PolynomialFitter(new GaussNewtonOptimizer(false, checker)); yFitter = new PolynomialFitter(new GaussNewtonOptimizer(false, checker)); } /** * {@inheritDoc} */ @Override public BicubicSplineInterpolatingFunction interpolate(final double[] xval, final double[] yval, final double[][] fval) throws NoDataException, NullArgumentException, DimensionMismatchException, NonMonotonicSequenceException { if (xval.length == 0 || yval.length == 0 || fval.length == 0) { throw new NoDataException(); } if (xval.length != fval.length) { throw new DimensionMismatchException(xval.length, fval.length); } final int xLen = xval.length; final int yLen = yval.length; for (int i = 0; i < xLen; i++) { if (fval[i].length != yLen) { throw new DimensionMismatchException(fval[i].length, yLen); } } MathArrays.checkOrder(xval); MathArrays.checkOrder(yval); // For each line y[j] (0 <= j < yLen), construct a polynomial, with // respect to variable x, fitting array fval[][j] final PolynomialFunction[] yPolyX = new PolynomialFunction[yLen]; for (int j = 0; j < yLen; j++) { xFitter.clearObservations(); for (int i = 0; i < xLen; i++) { xFitter.addObservedPoint(1, xval[i], fval[i][j]); } // Initial guess for the fit is zero for each coefficients (of which // there are "xDegree" + 1). yPolyX[j] = new PolynomialFunction(xFitter.fit(new double[xDegree + 1])); } // For every knot (xval[i], yval[j]) of the grid, calculate corrected // values fval_1 final double[][] fval_1 = new double[xLen][yLen]; for (int j = 0; j < yLen; j++) { final PolynomialFunction f = yPolyX[j]; for (int i = 0; i < xLen; i++) { fval_1[i][j] = f.value(xval[i]); } } // For each line x[i] (0 <= i < xLen), construct a polynomial, with // respect to variable y, fitting array fval_1[i][] final PolynomialFunction[] xPolyY = new PolynomialFunction[xLen]; for (int i = 0; i < xLen; i++) { yFitter.clearObservations(); for (int j = 0; j < yLen; j++) { yFitter.addObservedPoint(1, yval[j], fval_1[i][j]); } // Initial guess for the fit is zero for each coefficients (of which // there are "yDegree" + 1). xPolyY[i] = new PolynomialFunction(yFitter.fit(new double[yDegree + 1])); } // For every knot (xval[i], yval[j]) of the grid, calculate corrected // values fval_2 final double[][] fval_2 = new double[xLen][yLen]; for (int i = 0; i < xLen; i++) { final PolynomialFunction f = xPolyY[i]; for (int j = 0; j < yLen; j++) { fval_2[i][j] = f.value(yval[j]); } } return super.interpolate(xval, yval, fval_2); } } ././@LongLink100644 0 0 164 12126630646 10262 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/analysis/interpolation/TricubicSplineInterpolator.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/analysis/interpolation/TricubicSplineIn100644 1750 1750 20445 12126627702 32411 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.analysis.interpolation; import org.apache.commons.math3.exception.DimensionMismatchException; import org.apache.commons.math3.exception.NoDataException; import org.apache.commons.math3.exception.NonMonotonicSequenceException; import org.apache.commons.math3.exception.NumberIsTooSmallException; import org.apache.commons.math3.util.MathArrays; /** * Generates a tricubic interpolating function. * * @since 2.2 * @version $Id: TricubicSplineInterpolator.java 1455194 2013-03-11 15:45:54Z luc $ */ public class TricubicSplineInterpolator implements TrivariateGridInterpolator { /** * {@inheritDoc} */ public TricubicSplineInterpolatingFunction interpolate(final double[] xval, final double[] yval, final double[] zval, final double[][][] fval) throws NoDataException, NumberIsTooSmallException, DimensionMismatchException, NonMonotonicSequenceException { if (xval.length == 0 || yval.length == 0 || zval.length == 0 || fval.length == 0) { throw new NoDataException(); } if (xval.length != fval.length) { throw new DimensionMismatchException(xval.length, fval.length); } MathArrays.checkOrder(xval); MathArrays.checkOrder(yval); MathArrays.checkOrder(zval); final int xLen = xval.length; final int yLen = yval.length; final int zLen = zval.length; // Samples, re-ordered as (z, x, y) and (y, z, x) tuplets // fvalXY[k][i][j] = f(xval[i], yval[j], zval[k]) // fvalZX[j][k][i] = f(xval[i], yval[j], zval[k]) final double[][][] fvalXY = new double[zLen][xLen][yLen]; final double[][][] fvalZX = new double[yLen][zLen][xLen]; for (int i = 0; i < xLen; i++) { if (fval[i].length != yLen) { throw new DimensionMismatchException(fval[i].length, yLen); } for (int j = 0; j < yLen; j++) { if (fval[i][j].length != zLen) { throw new DimensionMismatchException(fval[i][j].length, zLen); } for (int k = 0; k < zLen; k++) { final double v = fval[i][j][k]; fvalXY[k][i][j] = v; fvalZX[j][k][i] = v; } } } final BicubicSplineInterpolator bsi = new BicubicSplineInterpolator(); // For each line x[i] (0 <= i < xLen), construct a 2D spline in y and z final BicubicSplineInterpolatingFunction[] xSplineYZ = new BicubicSplineInterpolatingFunction[xLen]; for (int i = 0; i < xLen; i++) { xSplineYZ[i] = bsi.interpolate(yval, zval, fval[i]); } // For each line y[j] (0 <= j < yLen), construct a 2D spline in z and x final BicubicSplineInterpolatingFunction[] ySplineZX = new BicubicSplineInterpolatingFunction[yLen]; for (int j = 0; j < yLen; j++) { ySplineZX[j] = bsi.interpolate(zval, xval, fvalZX[j]); } // For each line z[k] (0 <= k < zLen), construct a 2D spline in x and y final BicubicSplineInterpolatingFunction[] zSplineXY = new BicubicSplineInterpolatingFunction[zLen]; for (int k = 0; k < zLen; k++) { zSplineXY[k] = bsi.interpolate(xval, yval, fvalXY[k]); } // Partial derivatives wrt x and wrt y final double[][][] dFdX = new double[xLen][yLen][zLen]; final double[][][] dFdY = new double[xLen][yLen][zLen]; final double[][][] d2FdXdY = new double[xLen][yLen][zLen]; for (int k = 0; k < zLen; k++) { final BicubicSplineInterpolatingFunction f = zSplineXY[k]; for (int i = 0; i < xLen; i++) { final double x = xval[i]; for (int j = 0; j < yLen; j++) { final double y = yval[j]; dFdX[i][j][k] = f.partialDerivativeX(x, y); dFdY[i][j][k] = f.partialDerivativeY(x, y); d2FdXdY[i][j][k] = f.partialDerivativeXY(x, y); } } } // Partial derivatives wrt y and wrt z final double[][][] dFdZ = new double[xLen][yLen][zLen]; final double[][][] d2FdYdZ = new double[xLen][yLen][zLen]; for (int i = 0; i < xLen; i++) { final BicubicSplineInterpolatingFunction f = xSplineYZ[i]; for (int j = 0; j < yLen; j++) { final double y = yval[j]; for (int k = 0; k < zLen; k++) { final double z = zval[k]; dFdZ[i][j][k] = f.partialDerivativeY(y, z); d2FdYdZ[i][j][k] = f.partialDerivativeXY(y, z); } } } // Partial derivatives wrt x and wrt z final double[][][] d2FdZdX = new double[xLen][yLen][zLen]; for (int j = 0; j < yLen; j++) { final BicubicSplineInterpolatingFunction f = ySplineZX[j]; for (int k = 0; k < zLen; k++) { final double z = zval[k]; for (int i = 0; i < xLen; i++) { final double x = xval[i]; d2FdZdX[i][j][k] = f.partialDerivativeXY(z, x); } } } // Third partial cross-derivatives final double[][][] d3FdXdYdZ = new double[xLen][yLen][zLen]; for (int i = 0; i < xLen ; i++) { final int nI = nextIndex(i, xLen); final int pI = previousIndex(i); for (int j = 0; j < yLen; j++) { final int nJ = nextIndex(j, yLen); final int pJ = previousIndex(j); for (int k = 0; k < zLen; k++) { final int nK = nextIndex(k, zLen); final int pK = previousIndex(k); // XXX Not sure about this formula d3FdXdYdZ[i][j][k] = (fval[nI][nJ][nK] - fval[nI][pJ][nK] - fval[pI][nJ][nK] + fval[pI][pJ][nK] - fval[nI][nJ][pK] + fval[nI][pJ][pK] + fval[pI][nJ][pK] - fval[pI][pJ][pK]) / ((xval[nI] - xval[pI]) * (yval[nJ] - yval[pJ]) * (zval[nK] - zval[pK])) ; } } } // Create the interpolating splines return new TricubicSplineInterpolatingFunction(xval, yval, zval, fval, dFdX, dFdY, dFdZ, d2FdXdY, d2FdZdX, d2FdYdZ, d3FdXdYdZ); } /** * Compute the next index of an array, clipping if necessary. * It is assumed (but not checked) that {@code i} is larger than or equal to 0}. * * @param i Index * @param max Upper limit of the array * @return the next index */ private int nextIndex(int i, int max) { final int index = i + 1; return index < max ? index : index - 1; } /** * Compute the previous index of an array, clipping if necessary. * It is assumed (but not checked) that {@code i} is smaller than the size of the array. * * @param i Index * @return the previous index */ private int previousIndex(int i) { final int index = i - 1; return index >= 0 ? index : 0; } } ././@LongLink100644 0 0 155 12126630646 10262 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/analysis/interpolation/HermiteInterpolator.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/analysis/interpolation/HermiteInterpola100644 1750 1750 22426 12126627702 32457 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.analysis.interpolation; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import org.apache.commons.math3.analysis.differentiation.DerivativeStructure; import org.apache.commons.math3.analysis.differentiation.UnivariateDifferentiableVectorFunction; import org.apache.commons.math3.analysis.polynomials.PolynomialFunction; import org.apache.commons.math3.exception.MathArithmeticException; import org.apache.commons.math3.exception.NoDataException; import org.apache.commons.math3.exception.ZeroException; import org.apache.commons.math3.exception.util.LocalizedFormats; import org.apache.commons.math3.util.ArithmeticUtils; /** Polynomial interpolator using both sample values and sample derivatives. *

                            * The interpolation polynomials match all sample points, including both values * and provided derivatives. There is one polynomial for each component of * the values vector. All polynomials have the same degree. The degree of the * polynomials depends on the number of points and number of derivatives at each * point. For example the interpolation polynomials for n sample points without * any derivatives all have degree n-1. The interpolation polynomials for n * sample points with the two extreme points having value and first derivative * and the remaining points having value only all have degree n+1. The * interpolation polynomial for n sample points with value, first and second * derivative for all points all have degree 3n-1. *

                            * * @version $Id: HermiteInterpolator.java 1410460 2012-11-16 16:49:38Z erans $ * @since 3.1 */ public class HermiteInterpolator implements UnivariateDifferentiableVectorFunction { /** Sample abscissae. */ private final List abscissae; /** Top diagonal of the divided differences array. */ private final List topDiagonal; /** Bottom diagonal of the divided differences array. */ private final List bottomDiagonal; /** Create an empty interpolator. */ public HermiteInterpolator() { this.abscissae = new ArrayList(); this.topDiagonal = new ArrayList(); this.bottomDiagonal = new ArrayList(); } /** Add a sample point. *

                            * This method must be called once for each sample point. It is allowed to * mix some calls with values only with calls with values and first * derivatives. *

                            *

                            * The point abscissae for all calls must be different. *

                            * @param x abscissa of the sample point * @param value value and derivatives of the sample point * (if only one row is passed, it is the value, if two rows are * passed the first one is the value and the second the derivative * and so on) * @exception ZeroException if the abscissa difference between added point * and a previous point is zero (i.e. the two points are at same abscissa) * @exception MathArithmeticException if the number of derivatives is larger * than 20, which prevents computation of a factorial */ public void addSamplePoint(final double x, final double[] ... value) throws ZeroException, MathArithmeticException { for (int i = 0; i < value.length; ++i) { final double[] y = value[i].clone(); if (i > 1) { double inv = 1.0 / ArithmeticUtils.factorial(i); for (int j = 0; j < y.length; ++j) { y[j] *= inv; } } // update the bottom diagonal of the divided differences array final int n = abscissae.size(); bottomDiagonal.add(n - i, y); double[] bottom0 = y; for (int j = i; j < n; ++j) { final double[] bottom1 = bottomDiagonal.get(n - (j + 1)); final double inv = 1.0 / (x - abscissae.get(n - (j + 1))); if (Double.isInfinite(inv)) { throw new ZeroException(LocalizedFormats.DUPLICATED_ABSCISSA_DIVISION_BY_ZERO, x); } for (int k = 0; k < y.length; ++k) { bottom1[k] = inv * (bottom0[k] - bottom1[k]); } bottom0 = bottom1; } // update the top diagonal of the divided differences array topDiagonal.add(bottom0.clone()); // update the abscissae array abscissae.add(x); } } /** Compute the interpolation polynomials. * @return interpolation polynomials array * @exception NoDataException if sample is empty */ public PolynomialFunction[] getPolynomials() throws NoDataException { // safety check checkInterpolation(); // iteration initialization final PolynomialFunction zero = polynomial(0); PolynomialFunction[] polynomials = new PolynomialFunction[topDiagonal.get(0).length]; for (int i = 0; i < polynomials.length; ++i) { polynomials[i] = zero; } PolynomialFunction coeff = polynomial(1); // build the polynomials by iterating on the top diagonal of the divided differences array for (int i = 0; i < topDiagonal.size(); ++i) { double[] tdi = topDiagonal.get(i); for (int k = 0; k < polynomials.length; ++k) { polynomials[k] = polynomials[k].add(coeff.multiply(polynomial(tdi[k]))); } coeff = coeff.multiply(polynomial(-abscissae.get(i), 1.0)); } return polynomials; } /** Interpolate value at a specified abscissa. *

                            * Calling this method is equivalent to call the {@link PolynomialFunction#value(double) * value} methods of all polynomials returned by {@link #getPolynomials() getPolynomials}, * except it does not build the intermediate polynomials, so this method is faster and * numerically more stable. *

                            * @param x interpolation abscissa * @return interpolated value * @exception NoDataException if sample is empty */ public double[] value(double x) throws NoDataException { // safety check checkInterpolation(); final double[] value = new double[topDiagonal.get(0).length]; double valueCoeff = 1; for (int i = 0; i < topDiagonal.size(); ++i) { double[] dividedDifference = topDiagonal.get(i); for (int k = 0; k < value.length; ++k) { value[k] += dividedDifference[k] * valueCoeff; } final double deltaX = x - abscissae.get(i); valueCoeff *= deltaX; } return value; } /** Interpolate value at a specified abscissa. *

                            * Calling this method is equivalent to call the {@link * PolynomialFunction#value(DerivativeStructure) value} methods of all polynomials * returned by {@link #getPolynomials() getPolynomials}, except it does not build the * intermediate polynomials, so this method is faster and numerically more stable. *

                            * @param x interpolation abscissa * @return interpolated value * @exception NoDataException if sample is empty */ public DerivativeStructure[] value(final DerivativeStructure x) throws NoDataException { // safety check checkInterpolation(); final DerivativeStructure[] value = new DerivativeStructure[topDiagonal.get(0).length]; Arrays.fill(value, x.getField().getZero()); DerivativeStructure valueCoeff = x.getField().getOne(); for (int i = 0; i < topDiagonal.size(); ++i) { double[] dividedDifference = topDiagonal.get(i); for (int k = 0; k < value.length; ++k) { value[k] = value[k].add(valueCoeff.multiply(dividedDifference[k])); } final DerivativeStructure deltaX = x.subtract(abscissae.get(i)); valueCoeff = valueCoeff.multiply(deltaX); } return value; } /** Check interpolation can be performed. * @exception NoDataException if interpolation cannot be performed * because sample is empty */ private void checkInterpolation() throws NoDataException { if (abscissae.isEmpty()) { throw new NoDataException(LocalizedFormats.EMPTY_INTERPOLATION_SAMPLE); } } /** Create a polynomial from its coefficients. * @param c polynomials coefficients * @return polynomial */ private PolynomialFunction polynomial(double ... c) { return new PolynomialFunction(c); } } ././@LongLink100644 0 0 153 12126630646 10260 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/analysis/interpolation/LoessInterpolator.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/analysis/interpolation/LoessInterpolato100644 1750 1750 46111 12126627702 32507 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.analysis.interpolation; import java.io.Serializable; import java.util.Arrays; import org.apache.commons.math3.analysis.polynomials.PolynomialSplineFunction; import org.apache.commons.math3.exception.NotPositiveException; import org.apache.commons.math3.exception.OutOfRangeException; import org.apache.commons.math3.exception.DimensionMismatchException; import org.apache.commons.math3.exception.NoDataException; import org.apache.commons.math3.exception.NumberIsTooSmallException; import org.apache.commons.math3.exception.NonMonotonicSequenceException; import org.apache.commons.math3.exception.NotFiniteNumberException; import org.apache.commons.math3.exception.util.LocalizedFormats; import org.apache.commons.math3.util.FastMath; import org.apache.commons.math3.util.MathUtils; import org.apache.commons.math3.util.MathArrays; /** * Implements the * Local Regression Algorithm (also Loess, Lowess) for interpolation of * real univariate functions. *

                            * For reference, see * * William S. Cleveland - Robust Locally Weighted Regression and Smoothing * Scatterplots *

                            * This class implements both the loess method and serves as an interpolation * adapter to it, allowing one to build a spline on the obtained loess fit. * * @version $Id: LoessInterpolator.java 1379904 2012-09-01 23:54:52Z erans $ * @since 2.0 */ public class LoessInterpolator implements UnivariateInterpolator, Serializable { /** Default value of the bandwidth parameter. */ public static final double DEFAULT_BANDWIDTH = 0.3; /** Default value of the number of robustness iterations. */ public static final int DEFAULT_ROBUSTNESS_ITERS = 2; /** * Default value for accuracy. * @since 2.1 */ public static final double DEFAULT_ACCURACY = 1e-12; /** serializable version identifier. */ private static final long serialVersionUID = 5204927143605193821L; /** * The bandwidth parameter: when computing the loess fit at * a particular point, this fraction of source points closest * to the current point is taken into account for computing * a least-squares regression. *

                            * A sensible value is usually 0.25 to 0.5. */ private final double bandwidth; /** * The number of robustness iterations parameter: this many * robustness iterations are done. *

                            * A sensible value is usually 0 (just the initial fit without any * robustness iterations) to 4. */ private final int robustnessIters; /** * If the median residual at a certain robustness iteration * is less than this amount, no more iterations are done. */ private final double accuracy; /** * Constructs a new {@link LoessInterpolator} * with a bandwidth of {@link #DEFAULT_BANDWIDTH}, * {@link #DEFAULT_ROBUSTNESS_ITERS} robustness iterations * and an accuracy of {#link #DEFAULT_ACCURACY}. * See {@link #LoessInterpolator(double, int, double)} for an explanation of * the parameters. */ public LoessInterpolator() { this.bandwidth = DEFAULT_BANDWIDTH; this.robustnessIters = DEFAULT_ROBUSTNESS_ITERS; this.accuracy = DEFAULT_ACCURACY; } /** * Construct a new {@link LoessInterpolator} * with given bandwidth and number of robustness iterations. *

                            * Calling this constructor is equivalent to calling {link {@link * #LoessInterpolator(double, int, double) LoessInterpolator(bandwidth, * robustnessIters, LoessInterpolator.DEFAULT_ACCURACY)} *

                            * * @param bandwidth when computing the loess fit at * a particular point, this fraction of source points closest * to the current point is taken into account for computing * a least-squares regression.
                            * A sensible value is usually 0.25 to 0.5, the default value is * {@link #DEFAULT_BANDWIDTH}. * @param robustnessIters This many robustness iterations are done.
                            * A sensible value is usually 0 (just the initial fit without any * robustness iterations) to 4, the default value is * {@link #DEFAULT_ROBUSTNESS_ITERS}. * @see #LoessInterpolator(double, int, double) */ public LoessInterpolator(double bandwidth, int robustnessIters) { this(bandwidth, robustnessIters, DEFAULT_ACCURACY); } /** * Construct a new {@link LoessInterpolator} * with given bandwidth, number of robustness iterations and accuracy. * * @param bandwidth when computing the loess fit at * a particular point, this fraction of source points closest * to the current point is taken into account for computing * a least-squares regression.
                            * A sensible value is usually 0.25 to 0.5, the default value is * {@link #DEFAULT_BANDWIDTH}. * @param robustnessIters This many robustness iterations are done.
                            * A sensible value is usually 0 (just the initial fit without any * robustness iterations) to 4, the default value is * {@link #DEFAULT_ROBUSTNESS_ITERS}. * @param accuracy If the median residual at a certain robustness iteration * is less than this amount, no more iterations are done. * @throws OutOfRangeException if bandwidth does not lie in the interval [0,1]. * @throws NotPositiveException if {@code robustnessIters} is negative. * @see #LoessInterpolator(double, int) * @since 2.1 */ public LoessInterpolator(double bandwidth, int robustnessIters, double accuracy) throws OutOfRangeException, NotPositiveException { if (bandwidth < 0 || bandwidth > 1) { throw new OutOfRangeException(LocalizedFormats.BANDWIDTH, bandwidth, 0, 1); } this.bandwidth = bandwidth; if (robustnessIters < 0) { throw new NotPositiveException(LocalizedFormats.ROBUSTNESS_ITERATIONS, robustnessIters); } this.robustnessIters = robustnessIters; this.accuracy = accuracy; } /** * Compute an interpolating function by performing a loess fit * on the data at the original abscissae and then building a cubic spline * with a * {@link org.apache.commons.math3.analysis.interpolation.SplineInterpolator} * on the resulting fit. * * @param xval the arguments for the interpolation points * @param yval the values for the interpolation points * @return A cubic spline built upon a loess fit to the data at the original abscissae * @throws NonMonotonicSequenceException if {@code xval} not sorted in * strictly increasing order. * @throws DimensionMismatchException if {@code xval} and {@code yval} have * different sizes. * @throws NoDataException if {@code xval} or {@code yval} has zero size. * @throws NotFiniteNumberException if any of the arguments and values are * not finite real numbers. * @throws NumberIsTooSmallException if the bandwidth is too small to * accomodate the size of the input data (i.e. the bandwidth must be * larger than 2/n). */ public final PolynomialSplineFunction interpolate(final double[] xval, final double[] yval) throws NonMonotonicSequenceException, DimensionMismatchException, NoDataException, NotFiniteNumberException, NumberIsTooSmallException { return new SplineInterpolator().interpolate(xval, smooth(xval, yval)); } /** * Compute a weighted loess fit on the data at the original abscissae. * * @param xval Arguments for the interpolation points. * @param yval Values for the interpolation points. * @param weights point weights: coefficients by which the robustness weight * of a point is multiplied. * @return the values of the loess fit at corresponding original abscissae. * @throws NonMonotonicSequenceException if {@code xval} not sorted in * strictly increasing order. * @throws DimensionMismatchException if {@code xval} and {@code yval} have * different sizes. * @throws NoDataException if {@code xval} or {@code yval} has zero size. * @throws NotFiniteNumberException if any of the arguments and values are not finite real numbers. * @throws NumberIsTooSmallException if the bandwidth is too small to * accomodate the size of the input data (i.e. the bandwidth must be * larger than 2/n). * @since 2.1 */ public final double[] smooth(final double[] xval, final double[] yval, final double[] weights) throws NonMonotonicSequenceException, DimensionMismatchException, NoDataException, NotFiniteNumberException, NumberIsTooSmallException { if (xval.length != yval.length) { throw new DimensionMismatchException(xval.length, yval.length); } final int n = xval.length; if (n == 0) { throw new NoDataException(); } checkAllFiniteReal(xval); checkAllFiniteReal(yval); checkAllFiniteReal(weights); MathArrays.checkOrder(xval); if (n == 1) { return new double[]{yval[0]}; } if (n == 2) { return new double[]{yval[0], yval[1]}; } int bandwidthInPoints = (int) (bandwidth * n); if (bandwidthInPoints < 2) { throw new NumberIsTooSmallException(LocalizedFormats.BANDWIDTH, bandwidthInPoints, 2, true); } final double[] res = new double[n]; final double[] residuals = new double[n]; final double[] sortedResiduals = new double[n]; final double[] robustnessWeights = new double[n]; // Do an initial fit and 'robustnessIters' robustness iterations. // This is equivalent to doing 'robustnessIters+1' robustness iterations // starting with all robustness weights set to 1. Arrays.fill(robustnessWeights, 1); for (int iter = 0; iter <= robustnessIters; ++iter) { final int[] bandwidthInterval = {0, bandwidthInPoints - 1}; // At each x, compute a local weighted linear regression for (int i = 0; i < n; ++i) { final double x = xval[i]; // Find out the interval of source points on which // a regression is to be made. if (i > 0) { updateBandwidthInterval(xval, weights, i, bandwidthInterval); } final int ileft = bandwidthInterval[0]; final int iright = bandwidthInterval[1]; // Compute the point of the bandwidth interval that is // farthest from x final int edge; if (xval[i] - xval[ileft] > xval[iright] - xval[i]) { edge = ileft; } else { edge = iright; } // Compute a least-squares linear fit weighted by // the product of robustness weights and the tricube // weight function. // See http://en.wikipedia.org/wiki/Linear_regression // (section "Univariate linear case") // and http://en.wikipedia.org/wiki/Weighted_least_squares // (section "Weighted least squares") double sumWeights = 0; double sumX = 0; double sumXSquared = 0; double sumY = 0; double sumXY = 0; double denom = FastMath.abs(1.0 / (xval[edge] - x)); for (int k = ileft; k <= iright; ++k) { final double xk = xval[k]; final double yk = yval[k]; final double dist = (k < i) ? x - xk : xk - x; final double w = tricube(dist * denom) * robustnessWeights[k] * weights[k]; final double xkw = xk * w; sumWeights += w; sumX += xkw; sumXSquared += xk * xkw; sumY += yk * w; sumXY += yk * xkw; } final double meanX = sumX / sumWeights; final double meanY = sumY / sumWeights; final double meanXY = sumXY / sumWeights; final double meanXSquared = sumXSquared / sumWeights; final double beta; if (FastMath.sqrt(FastMath.abs(meanXSquared - meanX * meanX)) < accuracy) { beta = 0; } else { beta = (meanXY - meanX * meanY) / (meanXSquared - meanX * meanX); } final double alpha = meanY - beta * meanX; res[i] = beta * x + alpha; residuals[i] = FastMath.abs(yval[i] - res[i]); } // No need to recompute the robustness weights at the last // iteration, they won't be needed anymore if (iter == robustnessIters) { break; } // Recompute the robustness weights. // Find the median residual. // An arraycopy and a sort are completely tractable here, // because the preceding loop is a lot more expensive System.arraycopy(residuals, 0, sortedResiduals, 0, n); Arrays.sort(sortedResiduals); final double medianResidual = sortedResiduals[n / 2]; if (FastMath.abs(medianResidual) < accuracy) { break; } for (int i = 0; i < n; ++i) { final double arg = residuals[i] / (6 * medianResidual); if (arg >= 1) { robustnessWeights[i] = 0; } else { final double w = 1 - arg * arg; robustnessWeights[i] = w * w; } } } return res; } /** * Compute a loess fit on the data at the original abscissae. * * @param xval the arguments for the interpolation points * @param yval the values for the interpolation points * @return values of the loess fit at corresponding original abscissae * @throws NonMonotonicSequenceException if {@code xval} not sorted in * strictly increasing order. * @throws DimensionMismatchException if {@code xval} and {@code yval} have * different sizes. * @throws NoDataException if {@code xval} or {@code yval} has zero size. * @throws NotFiniteNumberException if any of the arguments and values are * not finite real numbers. * @throws NumberIsTooSmallException if the bandwidth is too small to * accomodate the size of the input data (i.e. the bandwidth must be * larger than 2/n). */ public final double[] smooth(final double[] xval, final double[] yval) throws NonMonotonicSequenceException, DimensionMismatchException, NoDataException, NotFiniteNumberException, NumberIsTooSmallException { if (xval.length != yval.length) { throw new DimensionMismatchException(xval.length, yval.length); } final double[] unitWeights = new double[xval.length]; Arrays.fill(unitWeights, 1.0); return smooth(xval, yval, unitWeights); } /** * Given an index interval into xval that embraces a certain number of * points closest to {@code xval[i-1]}, update the interval so that it * embraces the same number of points closest to {@code xval[i]}, * ignoring zero weights. * * @param xval Arguments array. * @param weights Weights array. * @param i Index around which the new interval should be computed. * @param bandwidthInterval a two-element array {left, right} such that: * {@code (left==0 or xval[i] - xval[left-1] > xval[right] - xval[i])} * and * {@code (right==xval.length-1 or xval[right+1] - xval[i] > xval[i] - xval[left])}. * The array will be updated. */ private static void updateBandwidthInterval(final double[] xval, final double[] weights, final int i, final int[] bandwidthInterval) { final int left = bandwidthInterval[0]; final int right = bandwidthInterval[1]; // The right edge should be adjusted if the next point to the right // is closer to xval[i] than the leftmost point of the current interval int nextRight = nextNonzero(weights, right); if (nextRight < xval.length && xval[nextRight] - xval[i] < xval[i] - xval[left]) { int nextLeft = nextNonzero(weights, bandwidthInterval[0]); bandwidthInterval[0] = nextLeft; bandwidthInterval[1] = nextRight; } } /** * Return the smallest index {@code j} such that * {@code j > i && (j == weights.length || weights[j] != 0)}. * * @param weights Weights array. * @param i Index from which to start search. * @return the smallest compliant index. */ private static int nextNonzero(final double[] weights, final int i) { int j = i + 1; while(j < weights.length && weights[j] == 0) { ++j; } return j; } /** * Compute the * tricube * weight function * * @param x Argument. * @return (1 - |x|3)3 for |x| < 1, 0 otherwise. */ private static double tricube(final double x) { final double absX = FastMath.abs(x); if (absX >= 1.0) { return 0.0; } final double tmp = 1 - absX * absX * absX; return tmp * tmp * tmp; } /** * Check that all elements of an array are finite real numbers. * * @param values Values array. * @throws org.apache.commons.math3.exception.NotFiniteNumberException * if one of the values is not a finite real number. */ private static void checkAllFiniteReal(final double[] values) { for (int i = 0; i < values.length; i++) { MathUtils.checkFinite(values[i]); } } } ././@LongLink100644 0 0 163 12126630646 10261 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/analysis/interpolation/BivariateGridInterpolator.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/analysis/interpolation/BivariateGridInt100644 1750 1750 4746 12126627702 32360 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.analysis.interpolation; import org.apache.commons.math3.analysis.BivariateFunction; import org.apache.commons.math3.exception.DimensionMismatchException; import org.apache.commons.math3.exception.NoDataException; import org.apache.commons.math3.exception.NonMonotonicSequenceException; import org.apache.commons.math3.exception.NumberIsTooSmallException; /** * Interface representing a bivariate real interpolating function where the * sample points must be specified on a regular grid. * * @version $Id: BivariateGridInterpolator.java 1455194 2013-03-11 15:45:54Z luc $ */ public interface BivariateGridInterpolator { /** * Compute an interpolating function for the dataset. * * @param xval All the x-coordinates of the interpolation points, sorted * in increasing order. * @param yval All the y-coordinates of the interpolation points, sorted * in increasing order. * @param fval The values of the interpolation points on all the grid knots: * {@code fval[i][j] = f(xval[i], yval[j])}. * @return a function which interpolates the dataset. * @throws NoDataException if any of the arrays has zero length. * @throws DimensionMismatchException if the array lengths are inconsistent. * @throws NonMonotonicSequenceException if the array is not sorted. * @throws NumberIsTooSmallException if the number of points is too small for * the order of the interpolation */ BivariateFunction interpolate(double[] xval, double[] yval, double[][] fval) throws NoDataException, DimensionMismatchException, NonMonotonicSequenceException, NumberIsTooSmallException; } ././@LongLink100644 0 0 163 12126630646 10261 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/analysis/interpolation/BicubicSplineInterpolator.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/analysis/interpolation/BicubicSplineInt100644 1750 1750 13355 12126627702 32373 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.analysis.interpolation; import org.apache.commons.math3.analysis.UnivariateFunction; import org.apache.commons.math3.analysis.polynomials.PolynomialSplineFunction; import org.apache.commons.math3.exception.DimensionMismatchException; import org.apache.commons.math3.exception.NoDataException; import org.apache.commons.math3.exception.NonMonotonicSequenceException; import org.apache.commons.math3.exception.NumberIsTooSmallException; import org.apache.commons.math3.util.MathArrays; /** * Generates a bicubic interpolating function. * * @version $Id: BicubicSplineInterpolator.java 1455194 2013-03-11 15:45:54Z luc $ * @since 2.2 */ public class BicubicSplineInterpolator implements BivariateGridInterpolator { /** * {@inheritDoc} */ public BicubicSplineInterpolatingFunction interpolate(final double[] xval, final double[] yval, final double[][] fval) throws NoDataException, DimensionMismatchException, NonMonotonicSequenceException, NumberIsTooSmallException { if (xval.length == 0 || yval.length == 0 || fval.length == 0) { throw new NoDataException(); } if (xval.length != fval.length) { throw new DimensionMismatchException(xval.length, fval.length); } MathArrays.checkOrder(xval); MathArrays.checkOrder(yval); final int xLen = xval.length; final int yLen = yval.length; // Samples (first index is y-coordinate, i.e. subarray variable is x) // 0 <= i < xval.length // 0 <= j < yval.length // fX[j][i] = f(xval[i], yval[j]) final double[][] fX = new double[yLen][xLen]; for (int i = 0; i < xLen; i++) { if (fval[i].length != yLen) { throw new DimensionMismatchException(fval[i].length, yLen); } for (int j = 0; j < yLen; j++) { fX[j][i] = fval[i][j]; } } final SplineInterpolator spInterpolator = new SplineInterpolator(); // For each line y[j] (0 <= j < yLen), construct a 1D spline with // respect to variable x final PolynomialSplineFunction[] ySplineX = new PolynomialSplineFunction[yLen]; for (int j = 0; j < yLen; j++) { ySplineX[j] = spInterpolator.interpolate(xval, fX[j]); } // For each line x[i] (0 <= i < xLen), construct a 1D spline with // respect to variable y generated by array fY_1[i] final PolynomialSplineFunction[] xSplineY = new PolynomialSplineFunction[xLen]; for (int i = 0; i < xLen; i++) { xSplineY[i] = spInterpolator.interpolate(yval, fval[i]); } // Partial derivatives with respect to x at the grid knots final double[][] dFdX = new double[xLen][yLen]; for (int j = 0; j < yLen; j++) { final UnivariateFunction f = ySplineX[j].derivative(); for (int i = 0; i < xLen; i++) { dFdX[i][j] = f.value(xval[i]); } } // Partial derivatives with respect to y at the grid knots final double[][] dFdY = new double[xLen][yLen]; for (int i = 0; i < xLen; i++) { final UnivariateFunction f = xSplineY[i].derivative(); for (int j = 0; j < yLen; j++) { dFdY[i][j] = f.value(yval[j]); } } // Cross partial derivatives final double[][] d2FdXdY = new double[xLen][yLen]; for (int i = 0; i < xLen ; i++) { final int nI = nextIndex(i, xLen); final int pI = previousIndex(i); for (int j = 0; j < yLen; j++) { final int nJ = nextIndex(j, yLen); final int pJ = previousIndex(j); d2FdXdY[i][j] = (fval[nI][nJ] - fval[nI][pJ] - fval[pI][nJ] + fval[pI][pJ]) / ((xval[nI] - xval[pI]) * (yval[nJ] - yval[pJ])); } } // Create the interpolating splines return new BicubicSplineInterpolatingFunction(xval, yval, fval, dFdX, dFdY, d2FdXdY); } /** * Computes the next index of an array, clipping if necessary. * It is assumed (but not checked) that {@code i >= 0}. * * @param i Index. * @param max Upper limit of the array. * @return the next index. */ private int nextIndex(int i, int max) { final int index = i + 1; return index < max ? index : index - 1; } /** * Computes the previous index of an array, clipping if necessary. * It is assumed (but not checked) that {@code i} is smaller than the size * of the array. * * @param i Index. * @return the previous index. */ private int previousIndex(int i) { final int index = i - 1; return index >= 0 ? index : 0; } } ././@LongLink100644 0 0 160 12126630646 10256 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/analysis/interpolation/UnivariateInterpolator.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/analysis/interpolation/UnivariateInterp100644 1750 1750 3512 12126627702 32450 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.analysis.interpolation; import org.apache.commons.math3.analysis.UnivariateFunction; import org.apache.commons.math3.exception.DimensionMismatchException; import org.apache.commons.math3.exception.MathIllegalArgumentException; /** * Interface representing a univariate real interpolating function. * * @version $Id: UnivariateInterpolator.java 1455194 2013-03-11 15:45:54Z luc $ */ public interface UnivariateInterpolator { /** * Compute an interpolating function for the dataset. * * @param xval Arguments for the interpolation points. * @param yval Values for the interpolation points. * @return a function which interpolates the dataset. * @throws MathIllegalArgumentException * if the arguments violate assumptions made by the interpolation * algorithm. * @throws DimensionMismatchException if arrays lengthes do not match */ UnivariateFunction interpolate(double xval[], double yval[]) throws MathIllegalArgumentException, DimensionMismatchException; } ././@LongLink100644 0 0 164 12126630646 10262 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/analysis/interpolation/TrivariateGridInterpolator.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/analysis/interpolation/TrivariateGridIn100644 1750 1750 5175 12126627702 32375 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.analysis.interpolation; import org.apache.commons.math3.analysis.TrivariateFunction; import org.apache.commons.math3.exception.DimensionMismatchException; import org.apache.commons.math3.exception.NoDataException; import org.apache.commons.math3.exception.NonMonotonicSequenceException; import org.apache.commons.math3.exception.NumberIsTooSmallException; /** * Interface representing a trivariate real interpolating function where the * sample points must be specified on a regular grid. * * @since 2.2 * @version $Id: TrivariateGridInterpolator.java 1455194 2013-03-11 15:45:54Z luc $ */ public interface TrivariateGridInterpolator { /** * Compute an interpolating function for the dataset. * * @param xval All the x-coordinates of the interpolation points, sorted * in increasing order. * @param yval All the y-coordinates of the interpolation points, sorted * in increasing order. * @param zval All the z-coordinates of the interpolation points, sorted * in increasing order. * @param fval the values of the interpolation points on all the grid knots: * {@code fval[i][j][k] = f(xval[i], yval[j], zval[k])}. * @return a function that interpolates the data set. * @throws NoDataException if any of the arrays has zero length. * @throws DimensionMismatchException if the array lengths are inconsistent. * @throws NonMonotonicSequenceException if arrays are not sorted * @throws NumberIsTooSmallException if the number of points is too small for * the order of the interpolation */ TrivariateFunction interpolate(double[] xval, double[] yval, double[] zval, double[][][] fval) throws NoDataException, NumberIsTooSmallException, DimensionMismatchException, NonMonotonicSequenceException; } ././@LongLink100644 0 0 174 12126630646 10263 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/analysis/interpolation/BicubicSplineInterpolatingFunction.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/analysis/interpolation/BicubicSplineInt100644 1750 1750 46540 12126627702 32375 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.analysis.interpolation; import org.apache.commons.math3.analysis.BivariateFunction; import org.apache.commons.math3.exception.DimensionMismatchException; import org.apache.commons.math3.exception.NoDataException; import org.apache.commons.math3.exception.OutOfRangeException; import org.apache.commons.math3.exception.NonMonotonicSequenceException; import org.apache.commons.math3.util.MathArrays; /** * Function that implements the * * bicubic spline interpolation. * * @since 2.1 * @version $Id: BicubicSplineInterpolatingFunction.java 1379904 2012-09-01 23:54:52Z erans $ */ public class BicubicSplineInterpolatingFunction implements BivariateFunction { /** * Matrix to compute the spline coefficients from the function values * and function derivatives values */ private static final double[][] AINV = { { 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 }, { 0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0 }, { -3,3,0,0,-2,-1,0,0,0,0,0,0,0,0,0,0 }, { 2,-2,0,0,1,1,0,0,0,0,0,0,0,0,0,0 }, { 0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0 }, { 0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0 }, { 0,0,0,0,0,0,0,0,-3,3,0,0,-2,-1,0,0 }, { 0,0,0,0,0,0,0,0,2,-2,0,0,1,1,0,0 }, { -3,0,3,0,0,0,0,0,-2,0,-1,0,0,0,0,0 }, { 0,0,0,0,-3,0,3,0,0,0,0,0,-2,0,-1,0 }, { 9,-9,-9,9,6,3,-6,-3,6,-6,3,-3,4,2,2,1 }, { -6,6,6,-6,-3,-3,3,3,-4,4,-2,2,-2,-2,-1,-1 }, { 2,0,-2,0,0,0,0,0,1,0,1,0,0,0,0,0 }, { 0,0,0,0,2,0,-2,0,0,0,0,0,1,0,1,0 }, { -6,6,6,-6,-4,-2,4,2,-3,3,-3,3,-2,-1,-2,-1 }, { 4,-4,-4,4,2,2,-2,-2,2,-2,2,-2,1,1,1,1 } }; /** Samples x-coordinates */ private final double[] xval; /** Samples y-coordinates */ private final double[] yval; /** Set of cubic splines patching the whole data grid */ private final BicubicSplineFunction[][] splines; /** * Partial derivatives * The value of the first index determines the kind of derivatives: * 0 = first partial derivatives wrt x * 1 = first partial derivatives wrt y * 2 = second partial derivatives wrt x * 3 = second partial derivatives wrt y * 4 = cross partial derivatives */ private BivariateFunction[][][] partialDerivatives = null; /** * @param x Sample values of the x-coordinate, in increasing order. * @param y Sample values of the y-coordinate, in increasing order. * @param f Values of the function on every grid point. * @param dFdX Values of the partial derivative of function with respect * to x on every grid point. * @param dFdY Values of the partial derivative of function with respect * to y on every grid point. * @param d2FdXdY Values of the cross partial derivative of function on * every grid point. * @throws DimensionMismatchException if the various arrays do not contain * the expected number of elements. * @throws NonMonotonicSequenceException if {@code x} or {@code y} are * not strictly increasing. * @throws NoDataException if any of the arrays has zero length. */ public BicubicSplineInterpolatingFunction(double[] x, double[] y, double[][] f, double[][] dFdX, double[][] dFdY, double[][] d2FdXdY) throws DimensionMismatchException, NoDataException, NonMonotonicSequenceException { final int xLen = x.length; final int yLen = y.length; if (xLen == 0 || yLen == 0 || f.length == 0 || f[0].length == 0) { throw new NoDataException(); } if (xLen != f.length) { throw new DimensionMismatchException(xLen, f.length); } if (xLen != dFdX.length) { throw new DimensionMismatchException(xLen, dFdX.length); } if (xLen != dFdY.length) { throw new DimensionMismatchException(xLen, dFdY.length); } if (xLen != d2FdXdY.length) { throw new DimensionMismatchException(xLen, d2FdXdY.length); } MathArrays.checkOrder(x); MathArrays.checkOrder(y); xval = x.clone(); yval = y.clone(); final int lastI = xLen - 1; final int lastJ = yLen - 1; splines = new BicubicSplineFunction[lastI][lastJ]; for (int i = 0; i < lastI; i++) { if (f[i].length != yLen) { throw new DimensionMismatchException(f[i].length, yLen); } if (dFdX[i].length != yLen) { throw new DimensionMismatchException(dFdX[i].length, yLen); } if (dFdY[i].length != yLen) { throw new DimensionMismatchException(dFdY[i].length, yLen); } if (d2FdXdY[i].length != yLen) { throw new DimensionMismatchException(d2FdXdY[i].length, yLen); } final int ip1 = i + 1; for (int j = 0; j < lastJ; j++) { final int jp1 = j + 1; final double[] beta = new double[] { f[i][j], f[ip1][j], f[i][jp1], f[ip1][jp1], dFdX[i][j], dFdX[ip1][j], dFdX[i][jp1], dFdX[ip1][jp1], dFdY[i][j], dFdY[ip1][j], dFdY[i][jp1], dFdY[ip1][jp1], d2FdXdY[i][j], d2FdXdY[ip1][j], d2FdXdY[i][jp1], d2FdXdY[ip1][jp1] }; splines[i][j] = new BicubicSplineFunction(computeSplineCoefficients(beta)); } } } /** * {@inheritDoc} */ public double value(double x, double y) throws OutOfRangeException { final int i = searchIndex(x, xval); if (i == -1) { throw new OutOfRangeException(x, xval[0], xval[xval.length - 1]); } final int j = searchIndex(y, yval); if (j == -1) { throw new OutOfRangeException(y, yval[0], yval[yval.length - 1]); } final double xN = (x - xval[i]) / (xval[i + 1] - xval[i]); final double yN = (y - yval[j]) / (yval[j + 1] - yval[j]); return splines[i][j].value(xN, yN); } /** * @param x x-coordinate. * @param y y-coordinate. * @return the value at point (x, y) of the first partial derivative with * respect to x. * @throws OutOfRangeException if {@code x} (resp. {@code y}) is outside * the range defined by the boundary values of {@code xval} (resp. * {@code yval}). */ public double partialDerivativeX(double x, double y) throws OutOfRangeException { return partialDerivative(0, x, y); } /** * @param x x-coordinate. * @param y y-coordinate. * @return the value at point (x, y) of the first partial derivative with * respect to y. * @throws OutOfRangeException if {@code x} (resp. {@code y}) is outside * the range defined by the boundary values of {@code xval} (resp. * {@code yval}). */ public double partialDerivativeY(double x, double y) throws OutOfRangeException { return partialDerivative(1, x, y); } /** * @param x x-coordinate. * @param y y-coordinate. * @return the value at point (x, y) of the second partial derivative with * respect to x. * @throws OutOfRangeException if {@code x} (resp. {@code y}) is outside * the range defined by the boundary values of {@code xval} (resp. * {@code yval}). */ public double partialDerivativeXX(double x, double y) throws OutOfRangeException { return partialDerivative(2, x, y); } /** * @param x x-coordinate. * @param y y-coordinate. * @return the value at point (x, y) of the second partial derivative with * respect to y. * @throws OutOfRangeException if {@code x} (resp. {@code y}) is outside * the range defined by the boundary values of {@code xval} (resp. * {@code yval}). */ public double partialDerivativeYY(double x, double y) throws OutOfRangeException { return partialDerivative(3, x, y); } /** * @param x x-coordinate. * @param y y-coordinate. * @return the value at point (x, y) of the second partial cross-derivative. * @throws OutOfRangeException if {@code x} (resp. {@code y}) is outside * the range defined by the boundary values of {@code xval} (resp. * {@code yval}). */ public double partialDerivativeXY(double x, double y) throws OutOfRangeException { return partialDerivative(4, x, y); } /** * @param which First index in {@link #partialDerivatives}. * @param x x-coordinate. * @param y y-coordinate. * @return the value at point (x, y) of the selected partial derivative. * @throws OutOfRangeException if {@code x} (resp. {@code y}) is outside * the range defined by the boundary values of {@code xval} (resp. * {@code yval}). */ private double partialDerivative(int which, double x, double y) throws OutOfRangeException { if (partialDerivatives == null) { computePartialDerivatives(); } final int i = searchIndex(x, xval); if (i == -1) { throw new OutOfRangeException(x, xval[0], xval[xval.length - 1]); } final int j = searchIndex(y, yval); if (j == -1) { throw new OutOfRangeException(y, yval[0], yval[yval.length - 1]); } final double xN = (x - xval[i]) / (xval[i + 1] - xval[i]); final double yN = (y - yval[j]) / (yval[j + 1] - yval[j]); return partialDerivatives[which][i][j].value(xN, yN); } /** * Compute all partial derivatives. */ private void computePartialDerivatives() { final int lastI = xval.length - 1; final int lastJ = yval.length - 1; partialDerivatives = new BivariateFunction[5][lastI][lastJ]; for (int i = 0; i < lastI; i++) { for (int j = 0; j < lastJ; j++) { final BicubicSplineFunction f = splines[i][j]; partialDerivatives[0][i][j] = f.partialDerivativeX(); partialDerivatives[1][i][j] = f.partialDerivativeY(); partialDerivatives[2][i][j] = f.partialDerivativeXX(); partialDerivatives[3][i][j] = f.partialDerivativeYY(); partialDerivatives[4][i][j] = f.partialDerivativeXY(); } } } /** * @param c Coordinate. * @param val Coordinate samples. * @return the index in {@code val} corresponding to the interval * containing {@code c}, or {@code -1} if {@code c} is out of the * range defined by the boundary values of {@code val}. */ private int searchIndex(double c, double[] val) { if (c < val[0]) { return -1; } final int max = val.length; for (int i = 1; i < max; i++) { if (c <= val[i]) { return i - 1; } } return -1; } /** * Compute the spline coefficients from the list of function values and * function partial derivatives values at the four corners of a grid * element. They must be specified in the following order: *
                              *
                            • f(0,0)
                            • *
                            • f(1,0)
                            • *
                            • f(0,1)
                            • *
                            • f(1,1)
                            • *
                            • fx(0,0)
                            • *
                            • fx(1,0)
                            • *
                            • fx(0,1)
                            • *
                            • fx(1,1)
                            • *
                            • fy(0,0)
                            • *
                            • fy(1,0)
                            • *
                            • fy(0,1)
                            • *
                            • fy(1,1)
                            • *
                            • fxy(0,0)
                            • *
                            • fxy(1,0)
                            • *
                            • fxy(0,1)
                            • *
                            • fxy(1,1)
                            • *
                            * where the subscripts indicate the partial derivative with respect to * the corresponding variable(s). * * @param beta List of function values and function partial derivatives * values. * @return the spline coefficients. */ private double[] computeSplineCoefficients(double[] beta) { final double[] a = new double[16]; for (int i = 0; i < 16; i++) { double result = 0; final double[] row = AINV[i]; for (int j = 0; j < 16; j++) { result += row[j] * beta[j]; } a[i] = result; } return a; } } /** * 2D-spline function. * * @version $Id: BicubicSplineInterpolatingFunction.java 1379904 2012-09-01 23:54:52Z erans $ */ class BicubicSplineFunction implements BivariateFunction { /** Number of points. */ private static final short N = 4; /** Coefficients */ private final double[][] a; /** First partial derivative along x. */ private BivariateFunction partialDerivativeX; /** First partial derivative along y. */ private BivariateFunction partialDerivativeY; /** Second partial derivative along x. */ private BivariateFunction partialDerivativeXX; /** Second partial derivative along y. */ private BivariateFunction partialDerivativeYY; /** Second crossed partial derivative. */ private BivariateFunction partialDerivativeXY; /** * Simple constructor. * @param a Spline coefficients */ public BicubicSplineFunction(double[] a) { this.a = new double[N][N]; for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { this.a[i][j] = a[i + N * j]; } } } /** * {@inheritDoc} */ public double value(double x, double y) { if (x < 0 || x > 1) { throw new OutOfRangeException(x, 0, 1); } if (y < 0 || y > 1) { throw new OutOfRangeException(y, 0, 1); } final double x2 = x * x; final double x3 = x2 * x; final double[] pX = {1, x, x2, x3}; final double y2 = y * y; final double y3 = y2 * y; final double[] pY = {1, y, y2, y3}; return apply(pX, pY, a); } /** * Compute the value of the bicubic polynomial. * * @param pX Powers of the x-coordinate. * @param pY Powers of the y-coordinate. * @param coeff Spline coefficients. * @return the interpolated value. */ private double apply(double[] pX, double[] pY, double[][] coeff) { double result = 0; for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { result += coeff[i][j] * pX[i] * pY[j]; } } return result; } /** * @return the partial derivative wrt {@code x}. */ public BivariateFunction partialDerivativeX() { if (partialDerivativeX == null) { computePartialDerivatives(); } return partialDerivativeX; } /** * @return the partial derivative wrt {@code y}. */ public BivariateFunction partialDerivativeY() { if (partialDerivativeY == null) { computePartialDerivatives(); } return partialDerivativeY; } /** * @return the second partial derivative wrt {@code x}. */ public BivariateFunction partialDerivativeXX() { if (partialDerivativeXX == null) { computePartialDerivatives(); } return partialDerivativeXX; } /** * @return the second partial derivative wrt {@code y}. */ public BivariateFunction partialDerivativeYY() { if (partialDerivativeYY == null) { computePartialDerivatives(); } return partialDerivativeYY; } /** * @return the second partial cross-derivative. */ public BivariateFunction partialDerivativeXY() { if (partialDerivativeXY == null) { computePartialDerivatives(); } return partialDerivativeXY; } /** * Compute all partial derivatives functions. */ private void computePartialDerivatives() { final double[][] aX = new double[N][N]; final double[][] aY = new double[N][N]; final double[][] aXX = new double[N][N]; final double[][] aYY = new double[N][N]; final double[][] aXY = new double[N][N]; for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { final double c = a[i][j]; aX[i][j] = i * c; aY[i][j] = j * c; aXX[i][j] = (i - 1) * aX[i][j]; aYY[i][j] = (j - 1) * aY[i][j]; aXY[i][j] = j * aX[i][j]; } } partialDerivativeX = new BivariateFunction() { public double value(double x, double y) { final double x2 = x * x; final double[] pX = {0, 1, x, x2}; final double y2 = y * y; final double y3 = y2 * y; final double[] pY = {1, y, y2, y3}; return apply(pX, pY, aX); } }; partialDerivativeY = new BivariateFunction() { public double value(double x, double y) { final double x2 = x * x; final double x3 = x2 * x; final double[] pX = {1, x, x2, x3}; final double y2 = y * y; final double[] pY = {0, 1, y, y2}; return apply(pX, pY, aY); } }; partialDerivativeXX = new BivariateFunction() { public double value(double x, double y) { final double[] pX = {0, 0, 1, x}; final double y2 = y * y; final double y3 = y2 * y; final double[] pY = {1, y, y2, y3}; return apply(pX, pY, aXX); } }; partialDerivativeYY = new BivariateFunction() { public double value(double x, double y) { final double x2 = x * x; final double x3 = x2 * x; final double[] pX = {1, x, x2, x3}; final double[] pY = {0, 0, 1, y}; return apply(pX, pY, aYY); } }; partialDerivativeXY = new BivariateFunction() { public double value(double x, double y) { final double x2 = x * x; final double[] pX = {0, 1, x, x2}; final double y2 = y * y; final double[] pY = {0, 1, y, y2}; return apply(pX, pY, aXY); } }; } } ././@LongLink100644 0 0 172 12126630646 10261 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/analysis/interpolation/MicrosphereInterpolatingFunction.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/analysis/interpolation/MicrosphereInter100644 1750 1750 22130 12126627702 32456 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.analysis.interpolation; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import org.apache.commons.math3.analysis.MultivariateFunction; import org.apache.commons.math3.exception.DimensionMismatchException; import org.apache.commons.math3.exception.NoDataException; import org.apache.commons.math3.exception.NullArgumentException; import org.apache.commons.math3.linear.ArrayRealVector; import org.apache.commons.math3.linear.RealVector; import org.apache.commons.math3.random.UnitSphereRandomVectorGenerator; import org.apache.commons.math3.util.FastMath; /** * Interpolating function that implements the * Microsphere Projection. * * @version $Id: MicrosphereInterpolatingFunction.java 1455194 2013-03-11 15:45:54Z luc $ */ public class MicrosphereInterpolatingFunction implements MultivariateFunction { /** * Space dimension. */ private final int dimension; /** * Internal accounting data for the interpolation algorithm. * Each element of the list corresponds to one surface element of * the microsphere. */ private final List microsphere; /** * Exponent used in the power law that computes the weights of the * sample data. */ private final double brightnessExponent; /** * Sample data. */ private final Map samples; /** * Class for storing the accounting data needed to perform the * microsphere projection. */ private static class MicrosphereSurfaceElement { /** Normal vector characterizing a surface element. */ private final RealVector normal; /** Illumination received from the brightest sample. */ private double brightestIllumination; /** Brightest sample. */ private Map.Entry brightestSample; /** * @param n Normal vector characterizing a surface element * of the microsphere. */ MicrosphereSurfaceElement(double[] n) { normal = new ArrayRealVector(n); } /** * Return the normal vector. * @return the normal vector */ RealVector normal() { return normal; } /** * Reset "illumination" and "sampleIndex". */ void reset() { brightestIllumination = 0; brightestSample = null; } /** * Store the illumination and index of the brightest sample. * @param illuminationFromSample illumination received from sample * @param sample current sample illuminating the element */ void store(final double illuminationFromSample, final Map.Entry sample) { if (illuminationFromSample > this.brightestIllumination) { this.brightestIllumination = illuminationFromSample; this.brightestSample = sample; } } /** * Get the illumination of the element. * @return the illumination. */ double illumination() { return brightestIllumination; } /** * Get the sample illuminating the element the most. * @return the sample. */ Map.Entry sample() { return brightestSample; } } /** * @param xval Arguments for the interpolation points. * {@code xval[i][0]} is the first component of interpolation point * {@code i}, {@code xval[i][1]} is the second component, and so on * until {@code xval[i][d-1]}, the last component of that interpolation * point (where {@code dimension} is thus the dimension of the sampled * space). * @param yval Values for the interpolation points. * @param brightnessExponent Brightness dimming factor. * @param microsphereElements Number of surface elements of the * microsphere. * @param rand Unit vector generator for creating the microsphere. * @throws DimensionMismatchException if the lengths of {@code yval} and * {@code xval} (equal to {@code n}, the number of interpolation points) * do not match, or the the arrays {@code xval[0]} ... {@code xval[n]}, * have lengths different from {@code dimension}. * @throws NoDataException if there an array has zero-length. * @throws NullArgumentException if an argument is {@code null}. */ public MicrosphereInterpolatingFunction(double[][] xval, double[] yval, int brightnessExponent, int microsphereElements, UnitSphereRandomVectorGenerator rand) throws DimensionMismatchException, NoDataException, NullArgumentException { if (xval == null || yval == null) { throw new NullArgumentException(); } if (xval.length == 0) { throw new NoDataException(); } if (xval.length != yval.length) { throw new DimensionMismatchException(xval.length, yval.length); } if (xval[0] == null) { throw new NullArgumentException(); } dimension = xval[0].length; this.brightnessExponent = brightnessExponent; // Copy data samples. samples = new HashMap(yval.length); for (int i = 0; i < xval.length; ++i) { final double[] xvalI = xval[i]; if (xvalI == null) { throw new NullArgumentException(); } if (xvalI.length != dimension) { throw new DimensionMismatchException(xvalI.length, dimension); } samples.put(new ArrayRealVector(xvalI), yval[i]); } microsphere = new ArrayList(microsphereElements); // Generate the microsphere, assuming that a fairly large number of // randomly generated normals will represent a sphere. for (int i = 0; i < microsphereElements; i++) { microsphere.add(new MicrosphereSurfaceElement(rand.nextVector())); } } /** * @param point Interpolation point. * @return the interpolated value. * @throws DimensionMismatchException if point dimension does not math sample */ public double value(double[] point) throws DimensionMismatchException { final RealVector p = new ArrayRealVector(point); // Reset. for (MicrosphereSurfaceElement md : microsphere) { md.reset(); } // Compute contribution of each sample points to the microsphere elements illumination for (Map.Entry sd : samples.entrySet()) { // Vector between interpolation point and current sample point. final RealVector diff = sd.getKey().subtract(p); final double diffNorm = diff.getNorm(); if (FastMath.abs(diffNorm) < FastMath.ulp(1d)) { // No need to interpolate, as the interpolation point is // actually (very close to) one of the sampled points. return sd.getValue(); } for (MicrosphereSurfaceElement md : microsphere) { final double w = FastMath.pow(diffNorm, -brightnessExponent); md.store(cosAngle(diff, md.normal()) * w, sd); } } // Interpolation calculation. double value = 0; double totalWeight = 0; for (MicrosphereSurfaceElement md : microsphere) { final double iV = md.illumination(); final Map.Entry sd = md.sample(); if (sd != null) { value += iV * sd.getValue(); totalWeight += iV; } } return value / totalWeight; } /** * Compute the cosine of the angle between 2 vectors. * * @param v Vector. * @param w Vector. * @return the cosine of the angle between {@code v} and {@code w}. */ private double cosAngle(final RealVector v, final RealVector w) { return v.dotProduct(w) / (v.getNorm() * w.getNorm()); } } ././@LongLink100644 0 0 167 12126630646 10265 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/analysis/interpolation/DividedDifferenceInterpolator.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/analysis/interpolation/DividedDifferenc100644 1750 1750 12035 12126627702 32355 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.analysis.interpolation; import java.io.Serializable; import org.apache.commons.math3.analysis.polynomials.PolynomialFunctionLagrangeForm; import org.apache.commons.math3.analysis.polynomials.PolynomialFunctionNewtonForm; import org.apache.commons.math3.exception.DimensionMismatchException; import org.apache.commons.math3.exception.NumberIsTooSmallException; import org.apache.commons.math3.exception.NonMonotonicSequenceException; /** * Implements the * Divided Difference Algorithm for interpolation of real univariate * functions. For reference, see Introduction to Numerical Analysis, * ISBN 038795452X, chapter 2. *

                            * The actual code of Neville's evaluation is in PolynomialFunctionLagrangeForm, * this class provides an easy-to-use interface to it.

                            * * @version $Id: DividedDifferenceInterpolator.java 1385313 2012-09-16 16:35:23Z tn $ * @since 1.2 */ public class DividedDifferenceInterpolator implements UnivariateInterpolator, Serializable { /** serializable version identifier */ private static final long serialVersionUID = 107049519551235069L; /** * Compute an interpolating function for the dataset. * * @param x Interpolating points array. * @param y Interpolating values array. * @return a function which interpolates the dataset. * @throws DimensionMismatchException if the array lengths are different. * @throws NumberIsTooSmallException if the number of points is less than 2. * @throws NonMonotonicSequenceException if {@code x} is not sorted in * strictly increasing order. */ public PolynomialFunctionNewtonForm interpolate(double x[], double y[]) throws DimensionMismatchException, NumberIsTooSmallException, NonMonotonicSequenceException { /** * a[] and c[] are defined in the general formula of Newton form: * p(x) = a[0] + a[1](x-c[0]) + a[2](x-c[0])(x-c[1]) + ... + * a[n](x-c[0])(x-c[1])...(x-c[n-1]) */ PolynomialFunctionLagrangeForm.verifyInterpolationArray(x, y, true); /** * When used for interpolation, the Newton form formula becomes * p(x) = f[x0] + f[x0,x1](x-x0) + f[x0,x1,x2](x-x0)(x-x1) + ... + * f[x0,x1,...,x[n-1]](x-x0)(x-x1)...(x-x[n-2]) * Therefore, a[k] = f[x0,x1,...,xk], c[k] = x[k]. *

                            * Note x[], y[], a[] have the same length but c[]'s size is one less.

                            */ final double[] c = new double[x.length-1]; System.arraycopy(x, 0, c, 0, c.length); final double[] a = computeDividedDifference(x, y); return new PolynomialFunctionNewtonForm(a, c); } /** * Return a copy of the divided difference array. *

                            * The divided difference array is defined recursively by

                                 * f[x0] = f(x0)
                                 * f[x0,x1,...,xk] = (f[x1,...,xk] - f[x0,...,x[k-1]]) / (xk - x0)
                                 * 

                            *

                            * The computational complexity is O(N^2).

                            * * @param x Interpolating points array. * @param y Interpolating values array. * @return a fresh copy of the divided difference array. * @throws DimensionMismatchException if the array lengths are different. * @throws NumberIsTooSmallException if the number of points is less than 2. * @throws NonMonotonicSequenceException * if {@code x} is not sorted in strictly increasing order. */ protected static double[] computeDividedDifference(final double x[], final double y[]) throws DimensionMismatchException, NumberIsTooSmallException, NonMonotonicSequenceException { PolynomialFunctionLagrangeForm.verifyInterpolationArray(x, y, true); final double[] divdiff = y.clone(); // initialization final int n = x.length; final double[] a = new double [n]; a[0] = divdiff[0]; for (int i = 1; i < n; i++) { for (int j = 0; j < n-i; j++) { final double denominator = x[j+i] - x[j]; divdiff[j] = (divdiff[j+1] - divdiff[j]) / denominator; } a[i] = divdiff[0]; } return a; } } ././@LongLink100644 0 0 175 12126630646 10264 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/analysis/interpolation/TricubicSplineInterpolatingFunction.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/analysis/interpolation/TricubicSplineIn100644 1750 1750 62556 12126627702 32422 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.analysis.interpolation; import org.apache.commons.math3.analysis.TrivariateFunction; import org.apache.commons.math3.exception.DimensionMismatchException; import org.apache.commons.math3.exception.NoDataException; import org.apache.commons.math3.exception.OutOfRangeException; import org.apache.commons.math3.exception.NonMonotonicSequenceException; import org.apache.commons.math3.util.MathArrays; /** * Function that implements the * * tricubic spline interpolation, as proposed in * * Tricubic interpolation in three dimensions
                            * F. Lekien and J. Marsden
                            * Int. J. Numer. Meth. Engng 2005; 63:455-471 *
                            * * @since 2.2 * @version $Id: TricubicSplineInterpolatingFunction.java 1385314 2012-09-16 16:35:49Z tn $ */ public class TricubicSplineInterpolatingFunction implements TrivariateFunction { /** * Matrix to compute the spline coefficients from the function values * and function derivatives values */ private static final double[][] AINV = { { 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 }, { 0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 }, { -3,3,0,0,0,0,0,0,-2,-1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 }, { 2,-2,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 }, { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 }, { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 }, { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,-3,3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,-2,-1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 }, { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,-2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 }, { -3,0,3,0,0,0,0,0,0,0,0,0,0,0,0,0,-2,0,-1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 }, { 0,0,0,0,0,0,0,0,-3,0,3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,-2,0,-1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 }, { 9,-9,-9,9,0,0,0,0,6,3,-6,-3,0,0,0,0,6,-6,3,-3,0,0,0,0,0,0,0,0,0,0,0,0,4,2,2,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 }, { -6,6,6,-6,0,0,0,0,-3,-3,3,3,0,0,0,0,-4,4,-2,2,0,0,0,0,0,0,0,0,0,0,0,0,-2,-2,-1,-1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 }, { 2,0,-2,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 }, { 0,0,0,0,0,0,0,0,2,0,-2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 }, { -6,6,6,-6,0,0,0,0,-4,-2,4,2,0,0,0,0,-3,3,-3,3,0,0,0,0,0,0,0,0,0,0,0,0,-2,-1,-2,-1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 }, { 4,-4,-4,4,0,0,0,0,2,2,-2,-2,0,0,0,0,2,-2,2,-2,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 }, { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 }, { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 }, { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,-3,3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,-2,-1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 }, { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,-2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 }, { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 }, { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0 }, { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,-3,3,0,0,0,0,0,0,-2,-1,0,0,0,0,0,0 }, { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,-2,0,0,0,0,0,0,1,1,0,0,0,0,0,0 }, { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,-3,0,3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,-2,0,-1,0,0,0,0,0,0,0,0,0,0,0,0,0 }, { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,-3,0,3,0,0,0,0,0,0,0,0,0,0,0,0,0,-2,0,-1,0,0,0,0,0 }, { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,9,-9,-9,9,0,0,0,0,0,0,0,0,0,0,0,0,6,3,-6,-3,0,0,0,0,6,-6,3,-3,0,0,0,0,4,2,2,1,0,0,0,0 }, { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,-6,6,6,-6,0,0,0,0,0,0,0,0,0,0,0,0,-3,-3,3,3,0,0,0,0,-4,4,-2,2,0,0,0,0,-2,-2,-1,-1,0,0,0,0 }, { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,0,-2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0 }, { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,0,-2,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,1,0,0,0,0,0 }, { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,-6,6,6,-6,0,0,0,0,0,0,0,0,0,0,0,0,-4,-2,4,2,0,0,0,0,-3,3,-3,3,0,0,0,0,-2,-1,-2,-1,0,0,0,0 }, { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,4,-4,-4,4,0,0,0,0,0,0,0,0,0,0,0,0,2,2,-2,-2,0,0,0,0,2,-2,2,-2,0,0,0,0,1,1,1,1,0,0,0,0 }, {-3,0,0,0,3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,-2,0,0,0,-1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 }, { 0,0,0,0,0,0,0,0,-3,0,0,0,3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,-2,0,0,0,-1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 }, { 9,-9,0,0,-9,9,0,0,6,3,0,0,-6,-3,0,0,0,0,0,0,0,0,0,0,6,-6,0,0,3,-3,0,0,0,0,0,0,0,0,0,0,4,2,0,0,2,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 }, { -6,6,0,0,6,-6,0,0,-3,-3,0,0,3,3,0,0,0,0,0,0,0,0,0,0,-4,4,0,0,-2,2,0,0,0,0,0,0,0,0,0,0,-2,-2,0,0,-1,-1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 }, { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,-3,0,0,0,3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,-2,0,0,0,-1,0,0,0,0,0,0,0,0,0,0,0 }, { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,-3,0,0,0,3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,-2,0,0,0,-1,0,0,0 }, { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,9,-9,0,0,-9,9,0,0,0,0,0,0,0,0,0,0,6,3,0,0,-6,-3,0,0,0,0,0,0,0,0,0,0,6,-6,0,0,3,-3,0,0,4,2,0,0,2,1,0,0 }, { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,-6,6,0,0,6,-6,0,0,0,0,0,0,0,0,0,0,-3,-3,0,0,3,3,0,0,0,0,0,0,0,0,0,0,-4,4,0,0,-2,2,0,0,-2,-2,0,0,-1,-1,0,0 }, { 9,0,-9,0,-9,0,9,0,0,0,0,0,0,0,0,0,6,0,3,0,-6,0,-3,0,6,0,-6,0,3,0,-3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,4,0,2,0,2,0,1,0,0,0,0,0,0,0,0,0 }, { 0,0,0,0,0,0,0,0,9,0,-9,0,-9,0,9,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,6,0,3,0,-6,0,-3,0,6,0,-6,0,3,0,-3,0,0,0,0,0,0,0,0,0,4,0,2,0,2,0,1,0 }, { -27,27,27,-27,27,-27,-27,27,-18,-9,18,9,18,9,-18,-9,-18,18,-9,9,18,-18,9,-9,-18,18,18,-18,-9,9,9,-9,-12,-6,-6,-3,12,6,6,3,-12,-6,12,6,-6,-3,6,3,-12,12,-6,6,-6,6,-3,3,-8,-4,-4,-2,-4,-2,-2,-1 }, { 18,-18,-18,18,-18,18,18,-18,9,9,-9,-9,-9,-9,9,9,12,-12,6,-6,-12,12,-6,6,12,-12,-12,12,6,-6,-6,6,6,6,3,3,-6,-6,-3,-3,6,6,-6,-6,3,3,-3,-3,8,-8,4,-4,4,-4,2,-2,4,4,2,2,2,2,1,1 }, { -6,0,6,0,6,0,-6,0,0,0,0,0,0,0,0,0,-3,0,-3,0,3,0,3,0,-4,0,4,0,-2,0,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,-2,0,-2,0,-1,0,-1,0,0,0,0,0,0,0,0,0 }, { 0,0,0,0,0,0,0,0,-6,0,6,0,6,0,-6,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,-3,0,-3,0,3,0,3,0,-4,0,4,0,-2,0,2,0,0,0,0,0,0,0,0,0,-2,0,-2,0,-1,0,-1,0 }, { 18,-18,-18,18,-18,18,18,-18,12,6,-12,-6,-12,-6,12,6,9,-9,9,-9,-9,9,-9,9,12,-12,-12,12,6,-6,-6,6,6,3,6,3,-6,-3,-6,-3,8,4,-8,-4,4,2,-4,-2,6,-6,6,-6,3,-3,3,-3,4,2,4,2,2,1,2,1 }, { -12,12,12,-12,12,-12,-12,12,-6,-6,6,6,6,6,-6,-6,-6,6,-6,6,6,-6,6,-6,-8,8,8,-8,-4,4,4,-4,-3,-3,-3,-3,3,3,3,3,-4,-4,4,4,-2,-2,2,2,-4,4,-4,4,-2,2,-2,2,-2,-2,-2,-2,-1,-1,-1,-1 }, { 2,0,0,0,-2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 }, { 0,0,0,0,0,0,0,0,2,0,0,0,-2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 }, { -6,6,0,0,6,-6,0,0,-4,-2,0,0,4,2,0,0,0,0,0,0,0,0,0,0,-3,3,0,0,-3,3,0,0,0,0,0,0,0,0,0,0,-2,-1,0,0,-2,-1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 }, { 4,-4,0,0,-4,4,0,0,2,2,0,0,-2,-2,0,0,0,0,0,0,0,0,0,0,2,-2,0,0,2,-2,0,0,0,0,0,0,0,0,0,0,1,1,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 }, { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,0,0,0,-2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0 }, { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,0,0,0,-2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,1,0,0,0 }, { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,-6,6,0,0,6,-6,0,0,0,0,0,0,0,0,0,0,-4,-2,0,0,4,2,0,0,0,0,0,0,0,0,0,0,-3,3,0,0,-3,3,0,0,-2,-1,0,0,-2,-1,0,0 }, { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,4,-4,0,0,-4,4,0,0,0,0,0,0,0,0,0,0,2,2,0,0,-2,-2,0,0,0,0,0,0,0,0,0,0,2,-2,0,0,2,-2,0,0,1,1,0,0,1,1,0,0 }, { -6,0,6,0,6,0,-6,0,0,0,0,0,0,0,0,0,-4,0,-2,0,4,0,2,0,-3,0,3,0,-3,0,3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,-2,0,-1,0,-2,0,-1,0,0,0,0,0,0,0,0,0 }, { 0,0,0,0,0,0,0,0,-6,0,6,0,6,0,-6,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,-4,0,-2,0,4,0,2,0,-3,0,3,0,-3,0,3,0,0,0,0,0,0,0,0,0,-2,0,-1,0,-2,0,-1,0 }, { 18,-18,-18,18,-18,18,18,-18,12,6,-12,-6,-12,-6,12,6,12,-12,6,-6,-12,12,-6,6,9,-9,-9,9,9,-9,-9,9,8,4,4,2,-8,-4,-4,-2,6,3,-6,-3,6,3,-6,-3,6,-6,3,-3,6,-6,3,-3,4,2,2,1,4,2,2,1 }, { -12,12,12,-12,12,-12,-12,12,-6,-6,6,6,6,6,-6,-6,-8,8,-4,4,8,-8,4,-4,-6,6,6,-6,-6,6,6,-6,-4,-4,-2,-2,4,4,2,2,-3,-3,3,3,-3,-3,3,3,-4,4,-2,2,-4,4,-2,2,-2,-2,-1,-1,-2,-2,-1,-1 }, { 4,0,-4,0,-4,0,4,0,0,0,0,0,0,0,0,0,2,0,2,0,-2,0,-2,0,2,0,-2,0,2,0,-2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,1,0,1,0,1,0,0,0,0,0,0,0,0,0 }, { 0,0,0,0,0,0,0,0,4,0,-4,0,-4,0,4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,0,2,0,-2,0,-2,0,2,0,-2,0,2,0,-2,0,0,0,0,0,0,0,0,0,1,0,1,0,1,0,1,0 }, { -12,12,12,-12,12,-12,-12,12,-8,-4,8,4,8,4,-8,-4,-6,6,-6,6,6,-6,6,-6,-6,6,6,-6,-6,6,6,-6,-4,-2,-4,-2,4,2,4,2,-4,-2,4,2,-4,-2,4,2,-3,3,-3,3,-3,3,-3,3,-2,-1,-2,-1,-2,-1,-2,-1 }, { 8,-8,-8,8,-8,8,8,-8,4,4,-4,-4,-4,-4,4,4,4,-4,4,-4,-4,4,-4,4,4,-4,-4,4,4,-4,-4,4,2,2,2,2,-2,-2,-2,-2,2,2,-2,-2,2,2,-2,-2,2,-2,2,-2,2,-2,2,-2,1,1,1,1,1,1,1,1 } }; /** Samples x-coordinates */ private final double[] xval; /** Samples y-coordinates */ private final double[] yval; /** Samples z-coordinates */ private final double[] zval; /** Set of cubic splines pacthing the whole data grid */ private final TricubicSplineFunction[][][] splines; /** * @param x Sample values of the x-coordinate, in increasing order. * @param y Sample values of the y-coordinate, in increasing order. * @param z Sample values of the y-coordinate, in increasing order. * @param f Values of the function on every grid point. * @param dFdX Values of the partial derivative of function with respect to x on every grid point. * @param dFdY Values of the partial derivative of function with respect to y on every grid point. * @param dFdZ Values of the partial derivative of function with respect to z on every grid point. * @param d2FdXdY Values of the cross partial derivative of function on every grid point. * @param d2FdXdZ Values of the cross partial derivative of function on every grid point. * @param d2FdYdZ Values of the cross partial derivative of function on every grid point. * @param d3FdXdYdZ Values of the cross partial derivative of function on every grid point. * @throws NoDataException if any of the arrays has zero length. * @throws DimensionMismatchException if the various arrays do not contain the expected number of elements. * @throws NonMonotonicSequenceException if {@code x}, {@code y} or {@code z} are not strictly increasing. */ public TricubicSplineInterpolatingFunction(double[] x, double[] y, double[] z, double[][][] f, double[][][] dFdX, double[][][] dFdY, double[][][] dFdZ, double[][][] d2FdXdY, double[][][] d2FdXdZ, double[][][] d2FdYdZ, double[][][] d3FdXdYdZ) throws NoDataException, DimensionMismatchException, NonMonotonicSequenceException { final int xLen = x.length; final int yLen = y.length; final int zLen = z.length; if (xLen == 0 || yLen == 0 || z.length == 0 || f.length == 0 || f[0].length == 0) { throw new NoDataException(); } if (xLen != f.length) { throw new DimensionMismatchException(xLen, f.length); } if (xLen != dFdX.length) { throw new DimensionMismatchException(xLen, dFdX.length); } if (xLen != dFdY.length) { throw new DimensionMismatchException(xLen, dFdY.length); } if (xLen != dFdZ.length) { throw new DimensionMismatchException(xLen, dFdZ.length); } if (xLen != d2FdXdY.length) { throw new DimensionMismatchException(xLen, d2FdXdY.length); } if (xLen != d2FdXdZ.length) { throw new DimensionMismatchException(xLen, d2FdXdZ.length); } if (xLen != d2FdYdZ.length) { throw new DimensionMismatchException(xLen, d2FdYdZ.length); } if (xLen != d3FdXdYdZ.length) { throw new DimensionMismatchException(xLen, d3FdXdYdZ.length); } MathArrays.checkOrder(x); MathArrays.checkOrder(y); MathArrays.checkOrder(z); xval = x.clone(); yval = y.clone(); zval = z.clone(); final int lastI = xLen - 1; final int lastJ = yLen - 1; final int lastK = zLen - 1; splines = new TricubicSplineFunction[lastI][lastJ][lastK]; for (int i = 0; i < lastI; i++) { if (f[i].length != yLen) { throw new DimensionMismatchException(f[i].length, yLen); } if (dFdX[i].length != yLen) { throw new DimensionMismatchException(dFdX[i].length, yLen); } if (dFdY[i].length != yLen) { throw new DimensionMismatchException(dFdY[i].length, yLen); } if (dFdZ[i].length != yLen) { throw new DimensionMismatchException(dFdZ[i].length, yLen); } if (d2FdXdY[i].length != yLen) { throw new DimensionMismatchException(d2FdXdY[i].length, yLen); } if (d2FdXdZ[i].length != yLen) { throw new DimensionMismatchException(d2FdXdZ[i].length, yLen); } if (d2FdYdZ[i].length != yLen) { throw new DimensionMismatchException(d2FdYdZ[i].length, yLen); } if (d3FdXdYdZ[i].length != yLen) { throw new DimensionMismatchException(d3FdXdYdZ[i].length, yLen); } final int ip1 = i + 1; for (int j = 0; j < lastJ; j++) { if (f[i][j].length != zLen) { throw new DimensionMismatchException(f[i][j].length, zLen); } if (dFdX[i][j].length != zLen) { throw new DimensionMismatchException(dFdX[i][j].length, zLen); } if (dFdY[i][j].length != zLen) { throw new DimensionMismatchException(dFdY[i][j].length, zLen); } if (dFdZ[i][j].length != zLen) { throw new DimensionMismatchException(dFdZ[i][j].length, zLen); } if (d2FdXdY[i][j].length != zLen) { throw new DimensionMismatchException(d2FdXdY[i][j].length, zLen); } if (d2FdXdZ[i][j].length != zLen) { throw new DimensionMismatchException(d2FdXdZ[i][j].length, zLen); } if (d2FdYdZ[i][j].length != zLen) { throw new DimensionMismatchException(d2FdYdZ[i][j].length, zLen); } if (d3FdXdYdZ[i][j].length != zLen) { throw new DimensionMismatchException(d3FdXdYdZ[i][j].length, zLen); } final int jp1 = j + 1; for (int k = 0; k < lastK; k++) { final int kp1 = k + 1; final double[] beta = new double[] { f[i][j][k], f[ip1][j][k], f[i][jp1][k], f[ip1][jp1][k], f[i][j][kp1], f[ip1][j][kp1], f[i][jp1][kp1], f[ip1][jp1][kp1], dFdX[i][j][k], dFdX[ip1][j][k], dFdX[i][jp1][k], dFdX[ip1][jp1][k], dFdX[i][j][kp1], dFdX[ip1][j][kp1], dFdX[i][jp1][kp1], dFdX[ip1][jp1][kp1], dFdY[i][j][k], dFdY[ip1][j][k], dFdY[i][jp1][k], dFdY[ip1][jp1][k], dFdY[i][j][kp1], dFdY[ip1][j][kp1], dFdY[i][jp1][kp1], dFdY[ip1][jp1][kp1], dFdZ[i][j][k], dFdZ[ip1][j][k], dFdZ[i][jp1][k], dFdZ[ip1][jp1][k], dFdZ[i][j][kp1], dFdZ[ip1][j][kp1], dFdZ[i][jp1][kp1], dFdZ[ip1][jp1][kp1], d2FdXdY[i][j][k], d2FdXdY[ip1][j][k], d2FdXdY[i][jp1][k], d2FdXdY[ip1][jp1][k], d2FdXdY[i][j][kp1], d2FdXdY[ip1][j][kp1], d2FdXdY[i][jp1][kp1], d2FdXdY[ip1][jp1][kp1], d2FdXdZ[i][j][k], d2FdXdZ[ip1][j][k], d2FdXdZ[i][jp1][k], d2FdXdZ[ip1][jp1][k], d2FdXdZ[i][j][kp1], d2FdXdZ[ip1][j][kp1], d2FdXdZ[i][jp1][kp1], d2FdXdZ[ip1][jp1][kp1], d2FdYdZ[i][j][k], d2FdYdZ[ip1][j][k], d2FdYdZ[i][jp1][k], d2FdYdZ[ip1][jp1][k], d2FdYdZ[i][j][kp1], d2FdYdZ[ip1][j][kp1], d2FdYdZ[i][jp1][kp1], d2FdYdZ[ip1][jp1][kp1], d3FdXdYdZ[i][j][k], d3FdXdYdZ[ip1][j][k], d3FdXdYdZ[i][jp1][k], d3FdXdYdZ[ip1][jp1][k], d3FdXdYdZ[i][j][kp1], d3FdXdYdZ[ip1][j][kp1], d3FdXdYdZ[i][jp1][kp1], d3FdXdYdZ[ip1][jp1][kp1], }; splines[i][j][k] = new TricubicSplineFunction(computeSplineCoefficients(beta)); } } } } /** * {@inheritDoc} * * @throws OutOfRangeException if any of the variables is outside its interpolation range. */ public double value(double x, double y, double z) throws OutOfRangeException { final int i = searchIndex(x, xval); if (i == -1) { throw new OutOfRangeException(x, xval[0], xval[xval.length - 1]); } final int j = searchIndex(y, yval); if (j == -1) { throw new OutOfRangeException(y, yval[0], yval[yval.length - 1]); } final int k = searchIndex(z, zval); if (k == -1) { throw new OutOfRangeException(z, zval[0], zval[zval.length - 1]); } final double xN = (x - xval[i]) / (xval[i + 1] - xval[i]); final double yN = (y - yval[j]) / (yval[j + 1] - yval[j]); final double zN = (z - zval[k]) / (zval[k + 1] - zval[k]); return splines[i][j][k].value(xN, yN, zN); } /** * @param c Coordinate. * @param val Coordinate samples. * @return the index in {@code val} corresponding to the interval containing {@code c}, or {@code -1} * if {@code c} is out of the range defined by the end values of {@code val}. */ private int searchIndex(double c, double[] val) { if (c < val[0]) { return -1; } final int max = val.length; for (int i = 1; i < max; i++) { if (c <= val[i]) { return i - 1; } } return -1; } /** * Compute the spline coefficients from the list of function values and * function partial derivatives values at the four corners of a grid * element. They must be specified in the following order: *
                              *
                            • f(0,0,0)
                            • *
                            • f(1,0,0)
                            • *
                            • f(0,1,0)
                            • *
                            • f(1,1,0)
                            • *
                            • f(0,0,1)
                            • *
                            • f(1,0,1)
                            • *
                            • f(0,1,1)
                            • *
                            • f(1,1,1)
                            • * *
                            • fx(0,0,0)
                            • *
                            • ... (same order as above)
                            • *
                            • fx(1,1,1)
                            • * *
                            • fy(0,0,0)
                            • *
                            • ... (same order as above)
                            • *
                            • fy(1,1,1)
                            • * *
                            • fz(0,0,0)
                            • *
                            • ... (same order as above)
                            • *
                            • fz(1,1,1)
                            • * *
                            • fxy(0,0,0)
                            • *
                            • ... (same order as above)
                            • *
                            • fxy(1,1,1)
                            • * *
                            • fxz(0,0,0)
                            • *
                            • ... (same order as above)
                            • *
                            • fxz(1,1,1)
                            • * *
                            • fyz(0,0,0)
                            • *
                            • ... (same order as above)
                            • *
                            • fyz(1,1,1)
                            • * *
                            • fxyz(0,0,0)
                            • *
                            • ... (same order as above)
                            • *
                            • fxyz(1,1,1)
                            • *
                            * where the subscripts indicate the partial derivative with respect to * the corresponding variable(s). * * @param beta List of function values and function partial derivatives values. * @return the spline coefficients. */ private double[] computeSplineCoefficients(double[] beta) { final int sz = 64; final double[] a = new double[sz]; for (int i = 0; i < sz; i++) { double result = 0; final double[] row = AINV[i]; for (int j = 0; j < sz; j++) { result += row[j] * beta[j]; } a[i] = result; } return a; } } /** * 3D-spline function. * * @version $Id: TricubicSplineInterpolatingFunction.java 1385314 2012-09-16 16:35:49Z tn $ */ class TricubicSplineFunction implements TrivariateFunction { /** Number of points. */ private static final short N = 4; /** Coefficients */ private final double[][][] a = new double[N][N][N]; /** * @param aV List of spline coefficients. */ public TricubicSplineFunction(double[] aV) { for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { for (int k = 0; k < N; k++) { a[i][j][k] = aV[i + N * (j + N * k)]; } } } } /** * @param x x-coordinate of the interpolation point. * @param y y-coordinate of the interpolation point. * @param z z-coordinate of the interpolation point. * @return the interpolated value. * @throws OutOfRangeException if {@code x}, {@code y} or * {@code z} are not in the interval {@code [0, 1]}. */ public double value(double x, double y, double z) throws OutOfRangeException { if (x < 0 || x > 1) { throw new OutOfRangeException(x, 0, 1); } if (y < 0 || y > 1) { throw new OutOfRangeException(y, 0, 1); } if (z < 0 || z > 1) { throw new OutOfRangeException(z, 0, 1); } final double x2 = x * x; final double x3 = x2 * x; final double[] pX = { 1, x, x2, x3 }; final double y2 = y * y; final double y3 = y2 * y; final double[] pY = { 1, y, y2, y3 }; final double z2 = z * z; final double z3 = z2 * z; final double[] pZ = { 1, z, z2, z3 }; double result = 0; for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { for (int k = 0; k < N; k++) { result += a[i][j][k] * pX[i] * pY[j] * pZ[k]; } } } return result; } } ././@LongLink100644 0 0 161 12126630646 10257 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/analysis/interpolation/MicrosphereInterpolator.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/analysis/interpolation/MicrosphereInter100644 1750 1750 10152 12126627702 32457 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.analysis.interpolation; import org.apache.commons.math3.analysis.MultivariateFunction; import org.apache.commons.math3.exception.NotPositiveException; import org.apache.commons.math3.exception.NotStrictlyPositiveException; import org.apache.commons.math3.exception.NoDataException; import org.apache.commons.math3.exception.DimensionMismatchException; import org.apache.commons.math3.exception.NullArgumentException; import org.apache.commons.math3.random.UnitSphereRandomVectorGenerator; /** * Interpolator that implements the algorithm described in * William Dudziak's * MS thesis. * * @since 2.1 * @version $Id: MicrosphereInterpolator.java 1379904 2012-09-01 23:54:52Z erans $ */ public class MicrosphereInterpolator implements MultivariateInterpolator { /** * Default number of surface elements that composes the microsphere. */ public static final int DEFAULT_MICROSPHERE_ELEMENTS = 2000; /** * Default exponent used the weights calculation. */ public static final int DEFAULT_BRIGHTNESS_EXPONENT = 2; /** * Number of surface elements of the microsphere. */ private final int microsphereElements; /** * Exponent used in the power law that computes the weights of the * sample data. */ private final int brightnessExponent; /** * Create a microsphere interpolator with default settings. * Calling this constructor is equivalent to call {@link * #MicrosphereInterpolator(int, int) * MicrosphereInterpolator(MicrosphereInterpolator.DEFAULT_MICROSPHERE_ELEMENTS, * MicrosphereInterpolator.DEFAULT_BRIGHTNESS_EXPONENT)}. */ public MicrosphereInterpolator() { this(DEFAULT_MICROSPHERE_ELEMENTS, DEFAULT_BRIGHTNESS_EXPONENT); } /** Create a microsphere interpolator. * @param elements Number of surface elements of the microsphere. * @param exponent Exponent used in the power law that computes the * weights (distance dimming factor) of the sample data. * @throws NotPositiveException if {@code exponent < 0}. * @throws NotStrictlyPositiveException if {@code elements <= 0}. */ public MicrosphereInterpolator(final int elements, final int exponent) throws NotPositiveException, NotStrictlyPositiveException { if (exponent < 0) { throw new NotPositiveException(exponent); } if (elements <= 0) { throw new NotStrictlyPositiveException(elements); } microsphereElements = elements; brightnessExponent = exponent; } /** * {@inheritDoc} */ public MultivariateFunction interpolate(final double[][] xval, final double[] yval) throws DimensionMismatchException, NoDataException, NullArgumentException { final UnitSphereRandomVectorGenerator rand = new UnitSphereRandomVectorGenerator(xval[0].length); return new MicrosphereInterpolatingFunction(xval, yval, brightnessExponent, microsphereElements, rand); } } ././@LongLink100644 0 0 154 12126630646 10261 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/analysis/interpolation/SplineInterpolator.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/analysis/interpolation/SplineInterpolat100644 1750 1750 13071 12126627702 32474 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.analysis.interpolation; import org.apache.commons.math3.exception.DimensionMismatchException; import org.apache.commons.math3.exception.util.LocalizedFormats; import org.apache.commons.math3.exception.NumberIsTooSmallException; import org.apache.commons.math3.exception.NonMonotonicSequenceException; import org.apache.commons.math3.analysis.polynomials.PolynomialFunction; import org.apache.commons.math3.analysis.polynomials.PolynomialSplineFunction; import org.apache.commons.math3.util.MathArrays; /** * Computes a natural (also known as "free", "unclamped") cubic spline interpolation for the data set. *

                            * The {@link #interpolate(double[], double[])} method returns a {@link PolynomialSplineFunction} * consisting of n cubic polynomials, defined over the subintervals determined by the x values, * x[0] < x[i] ... < x[n]. The x values are referred to as "knot points."

                            *

                            * The value of the PolynomialSplineFunction at a point x that is greater than or equal to the smallest * knot point and strictly less than the largest knot point is computed by finding the subinterval to which * x belongs and computing the value of the corresponding polynomial at x - x[i] where * i is the index of the subinterval. See {@link PolynomialSplineFunction} for more details. *

                            *

                            * The interpolating polynomials satisfy:

                              *
                            1. The value of the PolynomialSplineFunction at each of the input x values equals the * corresponding y value.
                            2. *
                            3. Adjacent polynomials are equal through two derivatives at the knot points (i.e., adjacent polynomials * "match up" at the knot points, as do their first and second derivatives).
                            4. *

                            *

                            * The cubic spline interpolation algorithm implemented is as described in R.L. Burden, J.D. Faires, * Numerical Analysis, 4th Ed., 1989, PWS-Kent, ISBN 0-53491-585-X, pp 126-131. *

                            * * @version $Id: SplineInterpolator.java 1379905 2012-09-01 23:56:50Z erans $ */ public class SplineInterpolator implements UnivariateInterpolator { /** * Computes an interpolating function for the data set. * @param x the arguments for the interpolation points * @param y the values for the interpolation points * @return a function which interpolates the data set * @throws DimensionMismatchException if {@code x} and {@code y} * have different sizes. * @throws NonMonotonicSequenceException if {@code x} is not sorted in * strict increasing order. * @throws NumberIsTooSmallException if the size of {@code x} is smaller * than 3. */ public PolynomialSplineFunction interpolate(double x[], double y[]) throws DimensionMismatchException, NumberIsTooSmallException, NonMonotonicSequenceException { if (x.length != y.length) { throw new DimensionMismatchException(x.length, y.length); } if (x.length < 3) { throw new NumberIsTooSmallException(LocalizedFormats.NUMBER_OF_POINTS, x.length, 3, true); } // Number of intervals. The number of data points is n + 1. final int n = x.length - 1; MathArrays.checkOrder(x); // Differences between knot points final double h[] = new double[n]; for (int i = 0; i < n; i++) { h[i] = x[i + 1] - x[i]; } final double mu[] = new double[n]; final double z[] = new double[n + 1]; mu[0] = 0d; z[0] = 0d; double g = 0; for (int i = 1; i < n; i++) { g = 2d * (x[i+1] - x[i - 1]) - h[i - 1] * mu[i -1]; mu[i] = h[i] / g; z[i] = (3d * (y[i + 1] * h[i - 1] - y[i] * (x[i + 1] - x[i - 1])+ y[i - 1] * h[i]) / (h[i - 1] * h[i]) - h[i - 1] * z[i - 1]) / g; } // cubic spline coefficients -- b is linear, c quadratic, d is cubic (original y's are constants) final double b[] = new double[n]; final double c[] = new double[n + 1]; final double d[] = new double[n]; z[n] = 0d; c[n] = 0d; for (int j = n -1; j >=0; j--) { c[j] = z[j] - mu[j] * c[j + 1]; b[j] = (y[j + 1] - y[j]) / h[j] - h[j] * (c[j + 1] + 2d * c[j]) / 3d; d[j] = (c[j + 1] - c[j]) / (3d * h[j]); } final PolynomialFunction polynomials[] = new PolynomialFunction[n]; final double coefficients[] = new double[4]; for (int i = 0; i < n; i++) { coefficients[0] = y[i]; coefficients[1] = b[i]; coefficients[2] = c[i]; coefficients[3] = d[i]; polynomials[i] = new PolynomialFunction(coefficients); } return new PolynomialSplineFunction(x, polynomials); } } ././@LongLink100644 0 0 170 12126630646 10257 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/analysis/interpolation/UnivariatePeriodicInterpolator.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/analysis/interpolation/UnivariatePeriod100644 1750 1750 11536 12126627702 32456 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.analysis.interpolation; import org.apache.commons.math3.analysis.UnivariateFunction; import org.apache.commons.math3.util.MathUtils; import org.apache.commons.math3.util.MathArrays; import org.apache.commons.math3.exception.MathIllegalArgumentException; import org.apache.commons.math3.exception.NonMonotonicSequenceException; import org.apache.commons.math3.exception.NumberIsTooSmallException; /** * Adapter for classes implementing the {@link UnivariateInterpolator} * interface. * The data to be interpolated is assumed to be periodic. Thus values that are * outside of the range can be passed to the interpolation function: They will * be wrapped into the initial range before being passed to the class that * actually computes the interpolation. * * @version $Id: UnivariatePeriodicInterpolator.java 1459739 2013-03-22 11:58:11Z erans $ */ public class UnivariatePeriodicInterpolator implements UnivariateInterpolator { /** Default number of extension points of the samples array. */ public static final int DEFAULT_EXTEND = 5; /** Interpolator. */ private final UnivariateInterpolator interpolator; /** Period. */ private final double period; /** Number of extension points. */ private final int extend; /** * Builds an interpolator. * * @param interpolator Interpolator. * @param period Period. * @param extend Number of points to be appended at the beginning and * end of the sample arrays in order to avoid interpolation failure at * the (periodic) boundaries of the orginal interval. The value is the * number of sample points which the original {@code interpolator} needs * on each side of the interpolated point. */ public UnivariatePeriodicInterpolator(UnivariateInterpolator interpolator, double period, int extend) { this.interpolator = interpolator; this.period = period; this.extend = extend; } /** * Builds an interpolator. * Uses {@link #DEFAULT_EXTEND} as the number of extension points on each side * of the original abscissae range. * * @param interpolator Interpolator. * @param period Period. */ public UnivariatePeriodicInterpolator(UnivariateInterpolator interpolator, double period) { this(interpolator, period, DEFAULT_EXTEND); } /** * {@inheritDoc} * * @throws NumberIsTooSmallException if the number of extension points * is larger than the size of {@code xval}. */ public UnivariateFunction interpolate(double[] xval, double[] yval) throws NumberIsTooSmallException, NonMonotonicSequenceException { if (xval.length < extend) { throw new NumberIsTooSmallException(xval.length, extend, true); } MathArrays.checkOrder(xval); final double offset = xval[0]; final int len = xval.length + extend * 2; final double[] x = new double[len]; final double[] y = new double[len]; for (int i = 0; i < xval.length; i++) { final int index = i + extend; x[index] = MathUtils.reduce(xval[i], period, offset); y[index] = yval[i]; } // Wrap to enable interpolation at the boundaries. for (int i = 0; i < extend; i++) { int index = xval.length - extend + i; x[i] = MathUtils.reduce(xval[index], period, offset) - period; y[i] = yval[index]; index = len - extend + i; x[index] = MathUtils.reduce(xval[i], period, offset) + period; y[index] = yval[i]; } MathArrays.sortInPlace(x, y); final UnivariateFunction f = interpolator.interpolate(x, y); return new UnivariateFunction() { public double value(final double x) throws MathIllegalArgumentException { return f.value(MathUtils.reduce(x, period, offset)); } }; } } ././@LongLink100644 0 0 162 12126630646 10260 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/analysis/interpolation/MultivariateInterpolator.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/analysis/interpolation/MultivariateInte100644 1750 1750 4711 12126627702 32447 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.analysis.interpolation; import org.apache.commons.math3.analysis.MultivariateFunction; import org.apache.commons.math3.exception.DimensionMismatchException; import org.apache.commons.math3.exception.MathIllegalArgumentException; import org.apache.commons.math3.exception.NoDataException; import org.apache.commons.math3.exception.NullArgumentException; /** * Interface representing a univariate real interpolating function. * * @since 2.1 * @version $Id: MultivariateInterpolator.java 1455194 2013-03-11 15:45:54Z luc $ */ public interface MultivariateInterpolator { /** * Computes an interpolating function for the data set. * * @param xval the arguments for the interpolation points. * {@code xval[i][0]} is the first component of interpolation point * {@code i}, {@code xval[i][1]} is the second component, and so on * until {@code xval[i][d-1]}, the last component of that interpolation * point (where {@code d} is thus the dimension of the space). * @param yval the values for the interpolation points * @return a function which interpolates the data set * @throws MathIllegalArgumentException if the arguments violate assumptions * made by the interpolation algorithm. * @throws DimensionMismatchException when the array dimensions are not consistent. * @throws NoDataException if an array has zero-length. * @throws NullArgumentException if the arguments are {@code null}. */ MultivariateFunction interpolate(double[][] xval, double[] yval) throws MathIllegalArgumentException, DimensionMismatchException, NoDataException, NullArgumentException; } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/analysis/package-info.java100644 1750 1750 2730 12126627707 27542 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ /** * *

                            * Parent package for common numerical analysis procedures, including root finding, * function interpolation and integration. Note that optimization (i.e. minimization * and maximization) is a separate top-level package. *

                            *

                            * Function interfaces are intended to be implemented by user code to represent * domain problems. The algorithms provided by the library operate on these * functions to find their roots, or integrate them, or ... Functions can be multivariate * or univariate, real vectorial or matrix-valued, and they can be differentiable or not. *

                            * */ package org.apache.commons.math3.analysis; ././@LongLink100644 0 0 164 12126630646 10262 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/analysis/DifferentiableMultivariateVectorFunction.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/analysis/DifferentiableMultivariateVect100644 1750 1750 2700 12126627707 32407 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.analysis; /** * Extension of {@link MultivariateVectorFunction} representing a differentiable * multivariate vectorial function. * @version $Id: DifferentiableMultivariateVectorFunction.java 1415149 2012-11-29 13:12:55Z erans $ * @since 2.0 * @deprecated as of 3.1 replaced by {@link org.apache.commons.math3.analysis.differentiation.MultivariateDifferentiableVectorFunction} */ @Deprecated public interface DifferentiableMultivariateVectorFunction extends MultivariateVectorFunction { /** * Returns the jacobian function. * @return the jacobian function */ MultivariateMatrixFunction jacobian(); } ././@LongLink100644 0 0 153 12126630646 10260 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/analysis/integration/TrapezoidIntegrator.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/analysis/integration/TrapezoidIntegrato100644 1750 1750 17035 12126627701 32455 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.analysis.integration; import org.apache.commons.math3.exception.MathIllegalArgumentException; import org.apache.commons.math3.exception.MaxCountExceededException; import org.apache.commons.math3.exception.NotStrictlyPositiveException; import org.apache.commons.math3.exception.NumberIsTooLargeException; import org.apache.commons.math3.exception.NumberIsTooSmallException; import org.apache.commons.math3.exception.TooManyEvaluationsException; import org.apache.commons.math3.util.FastMath; /** * Implements the * Trapezoid Rule for integration of real univariate functions. For * reference, see Introduction to Numerical Analysis, ISBN 038795452X, * chapter 3. *

                            * The function should be integrable.

                            * * @version $Id: TrapezoidIntegrator.java 1455194 2013-03-11 15:45:54Z luc $ * @since 1.2 */ public class TrapezoidIntegrator extends BaseAbstractUnivariateIntegrator { /** Maximum number of iterations for trapezoid. */ public static final int TRAPEZOID_MAX_ITERATIONS_COUNT = 64; /** Intermediate result. */ private double s; /** * Build a trapezoid integrator with given accuracies and iterations counts. * @param relativeAccuracy relative accuracy of the result * @param absoluteAccuracy absolute accuracy of the result * @param minimalIterationCount minimum number of iterations * @param maximalIterationCount maximum number of iterations * (must be less than or equal to {@link #TRAPEZOID_MAX_ITERATIONS_COUNT} * @exception NotStrictlyPositiveException if minimal number of iterations * is not strictly positive * @exception NumberIsTooSmallException if maximal number of iterations * is lesser than or equal to the minimal number of iterations * @exception NumberIsTooLargeException if maximal number of iterations * is greater than {@link #TRAPEZOID_MAX_ITERATIONS_COUNT} */ public TrapezoidIntegrator(final double relativeAccuracy, final double absoluteAccuracy, final int minimalIterationCount, final int maximalIterationCount) throws NotStrictlyPositiveException, NumberIsTooSmallException, NumberIsTooLargeException { super(relativeAccuracy, absoluteAccuracy, minimalIterationCount, maximalIterationCount); if (maximalIterationCount > TRAPEZOID_MAX_ITERATIONS_COUNT) { throw new NumberIsTooLargeException(maximalIterationCount, TRAPEZOID_MAX_ITERATIONS_COUNT, false); } } /** * Build a trapezoid integrator with given iteration counts. * @param minimalIterationCount minimum number of iterations * @param maximalIterationCount maximum number of iterations * (must be less than or equal to {@link #TRAPEZOID_MAX_ITERATIONS_COUNT} * @exception NotStrictlyPositiveException if minimal number of iterations * is not strictly positive * @exception NumberIsTooSmallException if maximal number of iterations * is lesser than or equal to the minimal number of iterations * @exception NumberIsTooLargeException if maximal number of iterations * is greater than {@link #TRAPEZOID_MAX_ITERATIONS_COUNT} */ public TrapezoidIntegrator(final int minimalIterationCount, final int maximalIterationCount) throws NotStrictlyPositiveException, NumberIsTooSmallException, NumberIsTooLargeException { super(minimalIterationCount, maximalIterationCount); if (maximalIterationCount > TRAPEZOID_MAX_ITERATIONS_COUNT) { throw new NumberIsTooLargeException(maximalIterationCount, TRAPEZOID_MAX_ITERATIONS_COUNT, false); } } /** * Construct a trapezoid integrator with default settings. * (max iteration count set to {@link #TRAPEZOID_MAX_ITERATIONS_COUNT}) */ public TrapezoidIntegrator() { super(DEFAULT_MIN_ITERATIONS_COUNT, TRAPEZOID_MAX_ITERATIONS_COUNT); } /** * Compute the n-th stage integral of trapezoid rule. This function * should only be called by API integrate() in the package. * To save time it does not verify arguments - caller does. *

                            * The interval is divided equally into 2^n sections rather than an * arbitrary m sections because this configuration can best utilize the * already computed values.

                            * * @param baseIntegrator integrator holding integration parameters * @param n the stage of 1/2 refinement, n = 0 is no refinement * @return the value of n-th stage integral * @throws TooManyEvaluationsException if the maximal number of evaluations * is exceeded. */ double stage(final BaseAbstractUnivariateIntegrator baseIntegrator, final int n) throws TooManyEvaluationsException { if (n == 0) { final double max = baseIntegrator.getMax(); final double min = baseIntegrator.getMin(); s = 0.5 * (max - min) * (baseIntegrator.computeObjectiveValue(min) + baseIntegrator.computeObjectiveValue(max)); return s; } else { final long np = 1L << (n-1); // number of new points in this stage double sum = 0; final double max = baseIntegrator.getMax(); final double min = baseIntegrator.getMin(); // spacing between adjacent new points final double spacing = (max - min) / np; double x = min + 0.5 * spacing; // the first new point for (long i = 0; i < np; i++) { sum += baseIntegrator.computeObjectiveValue(x); x += spacing; } // add the new sum to previously calculated result s = 0.5 * (s + sum * spacing); return s; } } /** {@inheritDoc} */ @Override protected double doIntegrate() throws MathIllegalArgumentException, TooManyEvaluationsException, MaxCountExceededException { double oldt = stage(this, 0); iterations.incrementCount(); while (true) { final int i = iterations.getCount(); final double t = stage(this, i); if (i >= getMinimalIterationCount()) { final double delta = FastMath.abs(t - oldt); final double rLimit = getRelativeAccuracy() * (FastMath.abs(oldt) + FastMath.abs(t)) * 0.5; if ((delta <= rLimit) || (delta <= getAbsoluteAccuracy())) { return t; } } oldt = t; iterations.incrementCount(); } } } ././@LongLink100644 0 0 154 12126630646 10261 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/analysis/integration/UnivariateIntegrator.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/analysis/integration/UnivariateIntegrat100644 1750 1750 6323 12126627701 32422 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.analysis.integration; import org.apache.commons.math3.analysis.UnivariateFunction; import org.apache.commons.math3.exception.MathIllegalArgumentException; import org.apache.commons.math3.exception.MaxCountExceededException; import org.apache.commons.math3.exception.NullArgumentException; import org.apache.commons.math3.exception.TooManyEvaluationsException; /** * Interface for univariate real integration algorithms. * * @version $Id: UnivariateIntegrator.java 1364387 2012-07-22 18:14:11Z tn $ * @since 1.2 */ public interface UnivariateIntegrator { /** * Get the actual relative accuracy. * @return the accuracy */ double getRelativeAccuracy(); /** * Get the actual absolute accuracy. * * @return the accuracy */ double getAbsoluteAccuracy(); /** * Get the min limit for the number of iterations. * * @return the actual min limit */ int getMinimalIterationCount(); /** * Get the upper limit for the number of iterations. * * @return the actual upper limit */ int getMaximalIterationCount(); /** * Integrate the function in the given interval. * * @param maxEval Maximum number of evaluations. * @param f the integrand function * @param min the min bound for the interval * @param max the upper bound for the interval * @return the value of integral * @throws TooManyEvaluationsException if the maximum number of function * evaluations is exceeded. * @throws MaxCountExceededException if the maximum iteration count is exceeded * or the integrator detects convergence problems otherwise * @throws MathIllegalArgumentException if min > max or the endpoints do not * satisfy the requirements specified by the integrator * @throws NullArgumentException if {@code f} is {@code null}. */ double integrate(int maxEval, UnivariateFunction f, double min, double max) throws TooManyEvaluationsException, MaxCountExceededException, MathIllegalArgumentException, NullArgumentException; /** * Get the number of function evaluations of the last run of the integrator. * @return number of function evaluations */ int getEvaluations(); /** * Get the number of iterations of the last run of the integrator. * @return number of iterations */ int getIterations(); } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/analysis/integration/package-info.java100644 1750 1750 1674 12126627701 32065 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ /** * * Numerical integration (quadrature) algorithms for univariate real functions. * */ package org.apache.commons.math3.analysis.integration; ././@LongLink100644 0 0 170 12126630646 10257 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/analysis/integration/IterativeLegendreGaussIntegrator.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/analysis/integration/IterativeLegendreG100644 1750 1750 16641 12126627701 32352 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.analysis.integration; import org.apache.commons.math3.analysis.UnivariateFunction; import org.apache.commons.math3.analysis.integration.gauss.GaussIntegratorFactory; import org.apache.commons.math3.analysis.integration.gauss.GaussIntegrator; import org.apache.commons.math3.exception.MathIllegalArgumentException; import org.apache.commons.math3.exception.MaxCountExceededException; import org.apache.commons.math3.exception.NotStrictlyPositiveException; import org.apache.commons.math3.exception.NumberIsTooSmallException; import org.apache.commons.math3.exception.TooManyEvaluationsException; import org.apache.commons.math3.exception.util.LocalizedFormats; import org.apache.commons.math3.util.FastMath; /** * This algorithm divides the integration interval into equally-sized * sub-interval and on each of them performs a * * Legendre-Gauss quadrature. * * @version $Id: IterativeLegendreGaussIntegrator.java 1455194 2013-03-11 15:45:54Z luc $ * @since 3.1 */ public class IterativeLegendreGaussIntegrator extends BaseAbstractUnivariateIntegrator { /** Factory that computes the points and weights. */ private static final GaussIntegratorFactory FACTORY = new GaussIntegratorFactory(); /** Number of integration points (per interval). */ private final int numberOfPoints; /** * Builds an integrator with given accuracies and iterations counts. * * @param n Number of integration points. * @param relativeAccuracy Relative accuracy of the result. * @param absoluteAccuracy Absolute accuracy of the result. * @param minimalIterationCount Minimum number of iterations. * @param maximalIterationCount Maximum number of iterations. * @throws NotStrictlyPositiveException if minimal number of iterations * or number of points are not strictly positive. * @throws NumberIsTooSmallException if maximal number of iterations * is smaller than or equal to the minimal number of iterations. */ public IterativeLegendreGaussIntegrator(final int n, final double relativeAccuracy, final double absoluteAccuracy, final int minimalIterationCount, final int maximalIterationCount) throws NotStrictlyPositiveException, NumberIsTooSmallException { super(relativeAccuracy, absoluteAccuracy, minimalIterationCount, maximalIterationCount); if (n <= 0) { throw new NotStrictlyPositiveException(LocalizedFormats.NUMBER_OF_POINTS, n); } numberOfPoints = n; } /** * Builds an integrator with given accuracies. * * @param n Number of integration points. * @param relativeAccuracy Relative accuracy of the result. * @param absoluteAccuracy Absolute accuracy of the result. * @throws NotStrictlyPositiveException if {@code n < 1}. */ public IterativeLegendreGaussIntegrator(final int n, final double relativeAccuracy, final double absoluteAccuracy) throws NotStrictlyPositiveException { this(n, relativeAccuracy, absoluteAccuracy, DEFAULT_MIN_ITERATIONS_COUNT, DEFAULT_MAX_ITERATIONS_COUNT); } /** * Builds an integrator with given iteration counts. * * @param n Number of integration points. * @param minimalIterationCount Minimum number of iterations. * @param maximalIterationCount Maximum number of iterations. * @throws NotStrictlyPositiveException if minimal number of iterations * is not strictly positive. * @throws NumberIsTooSmallException if maximal number of iterations * is smaller than or equal to the minimal number of iterations. * @throws NotStrictlyPositiveException if {@code n < 1}. */ public IterativeLegendreGaussIntegrator(final int n, final int minimalIterationCount, final int maximalIterationCount) throws NotStrictlyPositiveException, NumberIsTooSmallException { this(n, DEFAULT_RELATIVE_ACCURACY, DEFAULT_ABSOLUTE_ACCURACY, minimalIterationCount, maximalIterationCount); } /** {@inheritDoc} */ @Override protected double doIntegrate() throws MathIllegalArgumentException, TooManyEvaluationsException, MaxCountExceededException { // Compute first estimate with a single step. double oldt = stage(1); int n = 2; while (true) { // Improve integral with a larger number of steps. final double t = stage(n); // Estimate the error. final double delta = FastMath.abs(t - oldt); final double limit = FastMath.max(getAbsoluteAccuracy(), getRelativeAccuracy() * (FastMath.abs(oldt) + FastMath.abs(t)) * 0.5); // check convergence if (iterations.getCount() + 1 >= getMinimalIterationCount() && delta <= limit) { return t; } // Prepare next iteration. final double ratio = FastMath.min(4, FastMath.pow(delta / limit, 0.5 / numberOfPoints)); n = FastMath.max((int) (ratio * n), n + 1); oldt = t; iterations.incrementCount(); } } /** * Compute the n-th stage integral. * * @param n Number of steps. * @return the value of n-th stage integral. * @throws TooManyEvaluationsException if the maximum number of evaluations * is exceeded. */ private double stage(final int n) throws TooManyEvaluationsException { // Function to be integrated is stored in the base class. final UnivariateFunction f = new UnivariateFunction() { public double value(double x) throws MathIllegalArgumentException, TooManyEvaluationsException { return computeObjectiveValue(x); } }; final double min = getMin(); final double max = getMax(); final double step = (max - min) / n; double sum = 0; for (int i = 0; i < n; i++) { // Integrate over each sub-interval [a, b]. final double a = min + i * step; final double b = a + step; final GaussIntegrator g = FACTORY.legendreHighPrecision(numberOfPoints, a, b); sum += g.integrate(f); } return sum; } } ././@LongLink100644 0 0 157 12126630646 10264 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/analysis/integration/LegendreGaussIntegrator.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/analysis/integration/LegendreGaussInteg100644 1750 1750 24465 12126627701 32363 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.analysis.integration; import org.apache.commons.math3.exception.MathIllegalArgumentException; import org.apache.commons.math3.exception.MaxCountExceededException; import org.apache.commons.math3.exception.NotStrictlyPositiveException; import org.apache.commons.math3.exception.NumberIsTooSmallException; import org.apache.commons.math3.exception.TooManyEvaluationsException; import org.apache.commons.math3.exception.util.LocalizedFormats; import org.apache.commons.math3.util.FastMath; /** * Implements the * Legendre-Gauss quadrature formula. *

                            * Legendre-Gauss integrators are efficient integrators that can * accurately integrate functions with few function evaluations. A * Legendre-Gauss integrator using an n-points quadrature formula can * integrate 2n-1 degree polynomials exactly. *

                            *

                            * These integrators evaluate the function on n carefully chosen * abscissas in each step interval (mapped to the canonical [-1,1] interval). * The evaluation abscissas are not evenly spaced and none of them are * at the interval endpoints. This implies the function integrated can be * undefined at integration interval endpoints. *

                            *

                            * The evaluation abscissas xi are the roots of the degree n * Legendre polynomial. The weights ai of the quadrature formula * integrals from -1 to +1 ∫ Li2 where Li (x) = * ∏ (x-xk)/(xi-xk) for k != i. *

                            *

                            * @version $Id: LegendreGaussIntegrator.java 1455194 2013-03-11 15:45:54Z luc $ * @since 1.2 * @deprecated As of 3.1 (to be removed in 4.0). Please use * {@link IterativeLegendreGaussIntegrator} instead. */ @Deprecated public class LegendreGaussIntegrator extends BaseAbstractUnivariateIntegrator { /** Abscissas for the 2 points method. */ private static final double[] ABSCISSAS_2 = { -1.0 / FastMath.sqrt(3.0), 1.0 / FastMath.sqrt(3.0) }; /** Weights for the 2 points method. */ private static final double[] WEIGHTS_2 = { 1.0, 1.0 }; /** Abscissas for the 3 points method. */ private static final double[] ABSCISSAS_3 = { -FastMath.sqrt(0.6), 0.0, FastMath.sqrt(0.6) }; /** Weights for the 3 points method. */ private static final double[] WEIGHTS_3 = { 5.0 / 9.0, 8.0 / 9.0, 5.0 / 9.0 }; /** Abscissas for the 4 points method. */ private static final double[] ABSCISSAS_4 = { -FastMath.sqrt((15.0 + 2.0 * FastMath.sqrt(30.0)) / 35.0), -FastMath.sqrt((15.0 - 2.0 * FastMath.sqrt(30.0)) / 35.0), FastMath.sqrt((15.0 - 2.0 * FastMath.sqrt(30.0)) / 35.0), FastMath.sqrt((15.0 + 2.0 * FastMath.sqrt(30.0)) / 35.0) }; /** Weights for the 4 points method. */ private static final double[] WEIGHTS_4 = { (90.0 - 5.0 * FastMath.sqrt(30.0)) / 180.0, (90.0 + 5.0 * FastMath.sqrt(30.0)) / 180.0, (90.0 + 5.0 * FastMath.sqrt(30.0)) / 180.0, (90.0 - 5.0 * FastMath.sqrt(30.0)) / 180.0 }; /** Abscissas for the 5 points method. */ private static final double[] ABSCISSAS_5 = { -FastMath.sqrt((35.0 + 2.0 * FastMath.sqrt(70.0)) / 63.0), -FastMath.sqrt((35.0 - 2.0 * FastMath.sqrt(70.0)) / 63.0), 0.0, FastMath.sqrt((35.0 - 2.0 * FastMath.sqrt(70.0)) / 63.0), FastMath.sqrt((35.0 + 2.0 * FastMath.sqrt(70.0)) / 63.0) }; /** Weights for the 5 points method. */ private static final double[] WEIGHTS_5 = { (322.0 - 13.0 * FastMath.sqrt(70.0)) / 900.0, (322.0 + 13.0 * FastMath.sqrt(70.0)) / 900.0, 128.0 / 225.0, (322.0 + 13.0 * FastMath.sqrt(70.0)) / 900.0, (322.0 - 13.0 * FastMath.sqrt(70.0)) / 900.0 }; /** Abscissas for the current method. */ private final double[] abscissas; /** Weights for the current method. */ private final double[] weights; /** * Build a Legendre-Gauss integrator with given accuracies and iterations counts. * @param n number of points desired (must be between 2 and 5 inclusive) * @param relativeAccuracy relative accuracy of the result * @param absoluteAccuracy absolute accuracy of the result * @param minimalIterationCount minimum number of iterations * @param maximalIterationCount maximum number of iterations * @exception MathIllegalArgumentException if number of points is out of [2; 5] * @exception NotStrictlyPositiveException if minimal number of iterations * is not strictly positive * @exception NumberIsTooSmallException if maximal number of iterations * is lesser than or equal to the minimal number of iterations */ public LegendreGaussIntegrator(final int n, final double relativeAccuracy, final double absoluteAccuracy, final int minimalIterationCount, final int maximalIterationCount) throws MathIllegalArgumentException, NotStrictlyPositiveException, NumberIsTooSmallException { super(relativeAccuracy, absoluteAccuracy, minimalIterationCount, maximalIterationCount); switch(n) { case 2 : abscissas = ABSCISSAS_2; weights = WEIGHTS_2; break; case 3 : abscissas = ABSCISSAS_3; weights = WEIGHTS_3; break; case 4 : abscissas = ABSCISSAS_4; weights = WEIGHTS_4; break; case 5 : abscissas = ABSCISSAS_5; weights = WEIGHTS_5; break; default : throw new MathIllegalArgumentException( LocalizedFormats.N_POINTS_GAUSS_LEGENDRE_INTEGRATOR_NOT_SUPPORTED, n, 2, 5); } } /** * Build a Legendre-Gauss integrator with given accuracies. * @param n number of points desired (must be between 2 and 5 inclusive) * @param relativeAccuracy relative accuracy of the result * @param absoluteAccuracy absolute accuracy of the result * @exception MathIllegalArgumentException if number of points is out of [2; 5] */ public LegendreGaussIntegrator(final int n, final double relativeAccuracy, final double absoluteAccuracy) throws MathIllegalArgumentException { this(n, relativeAccuracy, absoluteAccuracy, DEFAULT_MIN_ITERATIONS_COUNT, DEFAULT_MAX_ITERATIONS_COUNT); } /** * Build a Legendre-Gauss integrator with given iteration counts. * @param n number of points desired (must be between 2 and 5 inclusive) * @param minimalIterationCount minimum number of iterations * @param maximalIterationCount maximum number of iterations * @exception MathIllegalArgumentException if number of points is out of [2; 5] * @exception NotStrictlyPositiveException if minimal number of iterations * is not strictly positive * @exception NumberIsTooSmallException if maximal number of iterations * is lesser than or equal to the minimal number of iterations */ public LegendreGaussIntegrator(final int n, final int minimalIterationCount, final int maximalIterationCount) throws MathIllegalArgumentException { this(n, DEFAULT_RELATIVE_ACCURACY, DEFAULT_ABSOLUTE_ACCURACY, minimalIterationCount, maximalIterationCount); } /** {@inheritDoc} */ @Override protected double doIntegrate() throws MathIllegalArgumentException, TooManyEvaluationsException, MaxCountExceededException { // compute first estimate with a single step double oldt = stage(1); int n = 2; while (true) { // improve integral with a larger number of steps final double t = stage(n); // estimate error final double delta = FastMath.abs(t - oldt); final double limit = FastMath.max(getAbsoluteAccuracy(), getRelativeAccuracy() * (FastMath.abs(oldt) + FastMath.abs(t)) * 0.5); // check convergence if ((iterations.getCount() + 1 >= getMinimalIterationCount()) && (delta <= limit)) { return t; } // prepare next iteration double ratio = FastMath.min(4, FastMath.pow(delta / limit, 0.5 / abscissas.length)); n = FastMath.max((int) (ratio * n), n + 1); oldt = t; iterations.incrementCount(); } } /** * Compute the n-th stage integral. * @param n number of steps * @return the value of n-th stage integral * @throws TooManyEvaluationsException if the maximum number of evaluations * is exceeded. */ private double stage(final int n) throws TooManyEvaluationsException { // set up the step for the current stage final double step = (getMax() - getMin()) / n; final double halfStep = step / 2.0; // integrate over all elementary steps double midPoint = getMin() + halfStep; double sum = 0.0; for (int i = 0; i < n; ++i) { for (int j = 0; j < abscissas.length; ++j) { sum += weights[j] * computeObjectiveValue(midPoint + halfStep * abscissas[j]); } midPoint += step; } return halfStep * sum; } } ././@LongLink100644 0 0 151 12126630646 10256 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/analysis/integration/SimpsonIntegrator.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/analysis/integration/SimpsonIntegrator.100644 1750 1750 13530 12126627701 32400 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.analysis.integration; import org.apache.commons.math3.exception.MaxCountExceededException; import org.apache.commons.math3.exception.NotStrictlyPositiveException; import org.apache.commons.math3.exception.NumberIsTooLargeException; import org.apache.commons.math3.exception.NumberIsTooSmallException; import org.apache.commons.math3.exception.TooManyEvaluationsException; import org.apache.commons.math3.util.FastMath; /** * Implements * Simpson's Rule for integration of real univariate functions. For * reference, see Introduction to Numerical Analysis, ISBN 038795452X, * chapter 3. *

                            * This implementation employs the basic trapezoid rule to calculate Simpson's * rule.

                            * * @version $Id: SimpsonIntegrator.java 1364387 2012-07-22 18:14:11Z tn $ * @since 1.2 */ public class SimpsonIntegrator extends BaseAbstractUnivariateIntegrator { /** Maximal number of iterations for Simpson. */ public static final int SIMPSON_MAX_ITERATIONS_COUNT = 64; /** * Build a Simpson integrator with given accuracies and iterations counts. * @param relativeAccuracy relative accuracy of the result * @param absoluteAccuracy absolute accuracy of the result * @param minimalIterationCount minimum number of iterations * @param maximalIterationCount maximum number of iterations * (must be less than or equal to {@link #SIMPSON_MAX_ITERATIONS_COUNT}) * @exception NotStrictlyPositiveException if minimal number of iterations * is not strictly positive * @exception NumberIsTooSmallException if maximal number of iterations * is lesser than or equal to the minimal number of iterations * @exception NumberIsTooLargeException if maximal number of iterations * is greater than {@link #SIMPSON_MAX_ITERATIONS_COUNT} */ public SimpsonIntegrator(final double relativeAccuracy, final double absoluteAccuracy, final int minimalIterationCount, final int maximalIterationCount) throws NotStrictlyPositiveException, NumberIsTooSmallException, NumberIsTooLargeException { super(relativeAccuracy, absoluteAccuracy, minimalIterationCount, maximalIterationCount); if (maximalIterationCount > SIMPSON_MAX_ITERATIONS_COUNT) { throw new NumberIsTooLargeException(maximalIterationCount, SIMPSON_MAX_ITERATIONS_COUNT, false); } } /** * Build a Simpson integrator with given iteration counts. * @param minimalIterationCount minimum number of iterations * @param maximalIterationCount maximum number of iterations * (must be less than or equal to {@link #SIMPSON_MAX_ITERATIONS_COUNT}) * @exception NotStrictlyPositiveException if minimal number of iterations * is not strictly positive * @exception NumberIsTooSmallException if maximal number of iterations * is lesser than or equal to the minimal number of iterations * @exception NumberIsTooLargeException if maximal number of iterations * is greater than {@link #SIMPSON_MAX_ITERATIONS_COUNT} */ public SimpsonIntegrator(final int minimalIterationCount, final int maximalIterationCount) throws NotStrictlyPositiveException, NumberIsTooSmallException, NumberIsTooLargeException { super(minimalIterationCount, maximalIterationCount); if (maximalIterationCount > SIMPSON_MAX_ITERATIONS_COUNT) { throw new NumberIsTooLargeException(maximalIterationCount, SIMPSON_MAX_ITERATIONS_COUNT, false); } } /** * Construct an integrator with default settings. * (max iteration count set to {@link #SIMPSON_MAX_ITERATIONS_COUNT}) */ public SimpsonIntegrator() { super(DEFAULT_MIN_ITERATIONS_COUNT, SIMPSON_MAX_ITERATIONS_COUNT); } /** {@inheritDoc} */ @Override protected double doIntegrate() throws TooManyEvaluationsException, MaxCountExceededException { TrapezoidIntegrator qtrap = new TrapezoidIntegrator(); if (getMinimalIterationCount() == 1) { return (4 * qtrap.stage(this, 1) - qtrap.stage(this, 0)) / 3.0; } // Simpson's rule requires at least two trapezoid stages. double olds = 0; double oldt = qtrap.stage(this, 0); while (true) { final double t = qtrap.stage(this, iterations.getCount()); iterations.incrementCount(); final double s = (4 * t - oldt) / 3.0; if (iterations.getCount() >= getMinimalIterationCount()) { final double delta = FastMath.abs(s - olds); final double rLimit = getRelativeAccuracy() * (FastMath.abs(olds) + FastMath.abs(s)) * 0.5; if ((delta <= rLimit) || (delta <= getAbsoluteAccuracy())) { return s; } } olds = s; oldt = t; } } } ././@LongLink100644 0 0 151 12126630646 10256 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/analysis/integration/RombergIntegrator.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/analysis/integration/RombergIntegrator.100644 1750 1750 14533 12126627701 32351 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.analysis.integration; import org.apache.commons.math3.exception.MaxCountExceededException; import org.apache.commons.math3.exception.NotStrictlyPositiveException; import org.apache.commons.math3.exception.NumberIsTooLargeException; import org.apache.commons.math3.exception.NumberIsTooSmallException; import org.apache.commons.math3.exception.TooManyEvaluationsException; import org.apache.commons.math3.util.FastMath; /** * Implements the * Romberg Algorithm for integration of real univariate functions. For * reference, see Introduction to Numerical Analysis, ISBN 038795452X, * chapter 3. *

                            * Romberg integration employs k successive refinements of the trapezoid * rule to remove error terms less than order O(N^(-2k)). Simpson's rule * is a special case of k = 2.

                            * * @version $Id: RombergIntegrator.java 1364387 2012-07-22 18:14:11Z tn $ * @since 1.2 */ public class RombergIntegrator extends BaseAbstractUnivariateIntegrator { /** Maximal number of iterations for Romberg. */ public static final int ROMBERG_MAX_ITERATIONS_COUNT = 32; /** * Build a Romberg integrator with given accuracies and iterations counts. * @param relativeAccuracy relative accuracy of the result * @param absoluteAccuracy absolute accuracy of the result * @param minimalIterationCount minimum number of iterations * @param maximalIterationCount maximum number of iterations * (must be less than or equal to {@link #ROMBERG_MAX_ITERATIONS_COUNT}) * @exception NotStrictlyPositiveException if minimal number of iterations * is not strictly positive * @exception NumberIsTooSmallException if maximal number of iterations * is lesser than or equal to the minimal number of iterations * @exception NumberIsTooLargeException if maximal number of iterations * is greater than {@link #ROMBERG_MAX_ITERATIONS_COUNT} */ public RombergIntegrator(final double relativeAccuracy, final double absoluteAccuracy, final int minimalIterationCount, final int maximalIterationCount) throws NotStrictlyPositiveException, NumberIsTooSmallException, NumberIsTooLargeException { super(relativeAccuracy, absoluteAccuracy, minimalIterationCount, maximalIterationCount); if (maximalIterationCount > ROMBERG_MAX_ITERATIONS_COUNT) { throw new NumberIsTooLargeException(maximalIterationCount, ROMBERG_MAX_ITERATIONS_COUNT, false); } } /** * Build a Romberg integrator with given iteration counts. * @param minimalIterationCount minimum number of iterations * @param maximalIterationCount maximum number of iterations * (must be less than or equal to {@link #ROMBERG_MAX_ITERATIONS_COUNT}) * @exception NotStrictlyPositiveException if minimal number of iterations * is not strictly positive * @exception NumberIsTooSmallException if maximal number of iterations * is lesser than or equal to the minimal number of iterations * @exception NumberIsTooLargeException if maximal number of iterations * is greater than {@link #ROMBERG_MAX_ITERATIONS_COUNT} */ public RombergIntegrator(final int minimalIterationCount, final int maximalIterationCount) throws NotStrictlyPositiveException, NumberIsTooSmallException, NumberIsTooLargeException { super(minimalIterationCount, maximalIterationCount); if (maximalIterationCount > ROMBERG_MAX_ITERATIONS_COUNT) { throw new NumberIsTooLargeException(maximalIterationCount, ROMBERG_MAX_ITERATIONS_COUNT, false); } } /** * Construct a Romberg integrator with default settings * (max iteration count set to {@link #ROMBERG_MAX_ITERATIONS_COUNT}) */ public RombergIntegrator() { super(DEFAULT_MIN_ITERATIONS_COUNT, ROMBERG_MAX_ITERATIONS_COUNT); } /** {@inheritDoc} */ @Override protected double doIntegrate() throws TooManyEvaluationsException, MaxCountExceededException { final int m = iterations.getMaximalCount() + 1; double previousRow[] = new double[m]; double currentRow[] = new double[m]; TrapezoidIntegrator qtrap = new TrapezoidIntegrator(); currentRow[0] = qtrap.stage(this, 0); iterations.incrementCount(); double olds = currentRow[0]; while (true) { final int i = iterations.getCount(); // switch rows final double[] tmpRow = previousRow; previousRow = currentRow; currentRow = tmpRow; currentRow[0] = qtrap.stage(this, i); iterations.incrementCount(); for (int j = 1; j <= i; j++) { // Richardson extrapolation coefficient final double r = (1L << (2 * j)) - 1; final double tIJm1 = currentRow[j - 1]; currentRow[j] = tIJm1 + (tIJm1 - previousRow[j - 1]) / r; } final double s = currentRow[i]; if (i >= getMinimalIterationCount()) { final double delta = FastMath.abs(s - olds); final double rLimit = getRelativeAccuracy() * (FastMath.abs(olds) + FastMath.abs(s)) * 0.5; if ((delta <= rLimit) || (delta <= getAbsoluteAccuracy())) { return s; } } olds = s; } } } ././@LongLink100644 0 0 152 12126630646 10257 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/analysis/integration/gauss/package-info.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/analysis/integration/gauss/package-info100644 1750 1750 1625 12126627701 32263 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ /** * * Gauss family of quadrature schemes. * */ package org.apache.commons.math3.analysis.integration.gauss; ././@LongLink100644 0 0 176 12126630646 10265 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/analysis/integration/gauss/LegendreHighPrecisionRuleFactory.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/analysis/integration/gauss/LegendreHigh100644 1750 1750 21214 12126627701 32300 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.analysis.integration.gauss; import java.math.BigDecimal; import java.math.MathContext; import org.apache.commons.math3.exception.DimensionMismatchException; import org.apache.commons.math3.util.Pair; /** * Factory that creates Gauss-type quadrature rule using Legendre polynomials. * In this implementation, the lower and upper bounds of the natural interval * of integration are -1 and 1, respectively. * The Legendre polynomials are evaluated using the recurrence relation * presented in . * * @since 3.1 * @version $Id: LegendreHighPrecisionRuleFactory.java 1455194 2013-03-11 15:45:54Z luc $ */ public class LegendreHighPrecisionRuleFactory extends BaseRuleFactory { /** Settings for enhanced precision computations. */ private final MathContext mContext; /** The number {@code 2}. */ private final BigDecimal two; /** The number {@code -1}. */ private final BigDecimal minusOne; /** The number {@code 0.5}. */ private final BigDecimal oneHalf; /** * Default precision is {@link MathContext#DECIMAL128 DECIMAL128}. */ public LegendreHighPrecisionRuleFactory() { this(MathContext.DECIMAL128); } /** * @param mContext Precision setting for computing the quadrature rules. */ public LegendreHighPrecisionRuleFactory(MathContext mContext) { this.mContext = mContext; two = new BigDecimal("2", mContext); minusOne = new BigDecimal("-1", mContext); oneHalf = new BigDecimal("0.5", mContext); } /** {@inheritDoc} */ @Override protected Pair computeRule(int numberOfPoints) throws DimensionMismatchException { if (numberOfPoints == 1) { // Break recursion. return new Pair(new BigDecimal[] { BigDecimal.ZERO }, new BigDecimal[] { two }); } // Get previous rule. // If it has not been computed yet it will trigger a recursive call // to this method. final BigDecimal[] previousPoints = getRuleInternal(numberOfPoints - 1).getFirst(); // Compute next rule. final BigDecimal[] points = new BigDecimal[numberOfPoints]; final BigDecimal[] weights = new BigDecimal[numberOfPoints]; // Find i-th root of P[n+1] by bracketing. final int iMax = numberOfPoints / 2; for (int i = 0; i < iMax; i++) { // Lower-bound of the interval. BigDecimal a = (i == 0) ? minusOne : previousPoints[i - 1]; // Upper-bound of the interval. BigDecimal b = (iMax == 1) ? BigDecimal.ONE : previousPoints[i]; // P[j-1](a) BigDecimal pma = BigDecimal.ONE; // P[j](a) BigDecimal pa = a; // P[j-1](b) BigDecimal pmb = BigDecimal.ONE; // P[j](b) BigDecimal pb = b; for (int j = 1; j < numberOfPoints; j++) { final BigDecimal b_two_j_p_1 = new BigDecimal(2 * j + 1, mContext); final BigDecimal b_j = new BigDecimal(j, mContext); final BigDecimal b_j_p_1 = new BigDecimal(j + 1, mContext); // Compute P[j+1](a) // ppa = ((2 * j + 1) * a * pa - j * pma) / (j + 1); BigDecimal tmp1 = a.multiply(b_two_j_p_1, mContext); tmp1 = pa.multiply(tmp1, mContext); BigDecimal tmp2 = pma.multiply(b_j, mContext); // P[j+1](a) BigDecimal ppa = tmp1.subtract(tmp2, mContext); ppa = ppa.divide(b_j_p_1, mContext); // Compute P[j+1](b) // ppb = ((2 * j + 1) * b * pb - j * pmb) / (j + 1); tmp1 = b.multiply(b_two_j_p_1, mContext); tmp1 = pb.multiply(tmp1, mContext); tmp2 = pmb.multiply(b_j, mContext); // P[j+1](b) BigDecimal ppb = tmp1.subtract(tmp2, mContext); ppb = ppb.divide(b_j_p_1, mContext); pma = pa; pa = ppa; pmb = pb; pb = ppb; } // Now pa = P[n+1](a), and pma = P[n](a). Same holds for b. // Middle of the interval. BigDecimal c = a.add(b, mContext).multiply(oneHalf, mContext); // P[j-1](c) BigDecimal pmc = BigDecimal.ONE; // P[j](c) BigDecimal pc = c; boolean done = false; while (!done) { BigDecimal tmp1 = b.subtract(a, mContext); BigDecimal tmp2 = c.ulp().multiply(BigDecimal.TEN, mContext); done = tmp1.compareTo(tmp2) <= 0; pmc = BigDecimal.ONE; pc = c; for (int j = 1; j < numberOfPoints; j++) { final BigDecimal b_two_j_p_1 = new BigDecimal(2 * j + 1, mContext); final BigDecimal b_j = new BigDecimal(j, mContext); final BigDecimal b_j_p_1 = new BigDecimal(j + 1, mContext); // Compute P[j+1](c) tmp1 = c.multiply(b_two_j_p_1, mContext); tmp1 = pc.multiply(tmp1, mContext); tmp2 = pmc.multiply(b_j, mContext); // P[j+1](c) BigDecimal ppc = tmp1.subtract(tmp2, mContext); ppc = ppc.divide(b_j_p_1, mContext); pmc = pc; pc = ppc; } // Now pc = P[n+1](c) and pmc = P[n](c). if (!done) { if (pa.signum() * pc.signum() <= 0) { b = c; pmb = pmc; pb = pc; } else { a = c; pma = pmc; pa = pc; } c = a.add(b, mContext).multiply(oneHalf, mContext); } } final BigDecimal nP = new BigDecimal(numberOfPoints, mContext); BigDecimal tmp1 = pmc.subtract(c.multiply(pc, mContext), mContext); tmp1 = tmp1.multiply(nP); tmp1 = tmp1.pow(2, mContext); BigDecimal tmp2 = c.pow(2, mContext); tmp2 = BigDecimal.ONE.subtract(tmp2, mContext); tmp2 = tmp2.multiply(two, mContext); tmp2 = tmp2.divide(tmp1, mContext); points[i] = c; weights[i] = tmp2; final int idx = numberOfPoints - i - 1; points[idx] = c.negate(mContext); weights[idx] = tmp2; } // If "numberOfPoints" is odd, 0 is a root. // Note: as written, the test for oddness will work for negative // integers too (although it is not necessary here), preventing // a FindBugs warning. if (numberOfPoints % 2 != 0) { BigDecimal pmc = BigDecimal.ONE; for (int j = 1; j < numberOfPoints; j += 2) { final BigDecimal b_j = new BigDecimal(j, mContext); final BigDecimal b_j_p_1 = new BigDecimal(j + 1, mContext); // pmc = -j * pmc / (j + 1); pmc = pmc.multiply(b_j, mContext); pmc = pmc.divide(b_j_p_1, mContext); pmc = pmc.negate(mContext); } // 2 / pow(numberOfPoints * pmc, 2); final BigDecimal nP = new BigDecimal(numberOfPoints, mContext); BigDecimal tmp1 = pmc.multiply(nP, mContext); tmp1 = tmp1.pow(2, mContext); BigDecimal tmp2 = two.divide(tmp1, mContext); points[iMax] = BigDecimal.ZERO; weights[iMax] = tmp2; } return new Pair(points, weights); } } ././@LongLink100644 0 0 161 12126630646 10257 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/analysis/integration/gauss/LegendreRuleFactory.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/analysis/integration/gauss/LegendreRule100644 1750 1750 12411 12126627701 32327 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.analysis.integration.gauss; import org.apache.commons.math3.exception.DimensionMismatchException; import org.apache.commons.math3.util.Pair; /** * Factory that creates Gauss-type quadrature rule using Legendre polynomials. * In this implementation, the lower and upper bounds of the natural interval * of integration are -1 and 1, respectively. * The Legendre polynomials are evaluated using the recurrence relation * presented in . * * @since 3.1 * @version $Id: LegendreRuleFactory.java 1455194 2013-03-11 15:45:54Z luc $ */ public class LegendreRuleFactory extends BaseRuleFactory { /** {@inheritDoc} */ @Override protected Pair computeRule(int numberOfPoints) throws DimensionMismatchException { if (numberOfPoints == 1) { // Break recursion. return new Pair(new Double[] { 0d }, new Double[] { 2d }); } // Get previous rule. // If it has not been computed yet it will trigger a recursive call // to this method. final Double[] previousPoints = getRuleInternal(numberOfPoints - 1).getFirst(); // Compute next rule. final Double[] points = new Double[numberOfPoints]; final Double[] weights = new Double[numberOfPoints]; // Find i-th root of P[n+1] by bracketing. final int iMax = numberOfPoints / 2; for (int i = 0; i < iMax; i++) { // Lower-bound of the interval. double a = (i == 0) ? -1 : previousPoints[i - 1].doubleValue(); // Upper-bound of the interval. double b = (iMax == 1) ? 1 : previousPoints[i].doubleValue(); // P[j-1](a) double pma = 1; // P[j](a) double pa = a; // P[j-1](b) double pmb = 1; // P[j](b) double pb = b; for (int j = 1; j < numberOfPoints; j++) { final int two_j_p_1 = 2 * j + 1; final int j_p_1 = j + 1; // P[j+1](a) final double ppa = (two_j_p_1 * a * pa - j * pma) / j_p_1; // P[j+1](b) final double ppb = (two_j_p_1 * b * pb - j * pmb) / j_p_1; pma = pa; pa = ppa; pmb = pb; pb = ppb; } // Now pa = P[n+1](a), and pma = P[n](a) (same holds for b). // Middle of the interval. double c = 0.5 * (a + b); // P[j-1](c) double pmc = 1; // P[j](c) double pc = c; boolean done = false; while (!done) { done = b - a <= Math.ulp(c); pmc = 1; pc = c; for (int j = 1; j < numberOfPoints; j++) { // P[j+1](c) final double ppc = ((2 * j + 1) * c * pc - j * pmc) / (j + 1); pmc = pc; pc = ppc; } // Now pc = P[n+1](c) and pmc = P[n](c). if (!done) { if (pa * pc <= 0) { b = c; pmb = pmc; pb = pc; } else { a = c; pma = pmc; pa = pc; } c = 0.5 * (a + b); } } final double d = numberOfPoints * (pmc - c * pc); final double w = 2 * (1 - c * c) / (d * d); points[i] = c; weights[i] = w; final int idx = numberOfPoints - i - 1; points[idx] = -c; weights[idx] = w; } // If "numberOfPoints" is odd, 0 is a root. // Note: as written, the test for oddness will work for negative // integers too (although it is not necessary here), preventing // a FindBugs warning. if (numberOfPoints % 2 != 0) { double pmc = 1; for (int j = 1; j < numberOfPoints; j += 2) { pmc = -j * pmc / (j + 1); } final double d = numberOfPoints * pmc; final double w = 2 / (d * d); points[iMax] = 0d; weights[iMax] = w; } return new Pair(points, weights); } } ././@LongLink100644 0 0 155 12126630646 10262 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/analysis/integration/gauss/BaseRuleFactory.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/analysis/integration/gauss/BaseRuleFact100644 1750 1750 13507 12126627701 32261 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.analysis.integration.gauss; import java.util.Map; import java.util.TreeMap; import org.apache.commons.math3.util.Pair; import org.apache.commons.math3.exception.DimensionMismatchException; import org.apache.commons.math3.exception.NotStrictlyPositiveException; import org.apache.commons.math3.exception.util.LocalizedFormats; /** * Base class for rules that determines the integration nodes and their * weights. * Subclasses must implement the {@link #computeRule(int) computeRule} method. * * @param Type of the number used to represent the points and weights of * the quadrature rules. * * @since 3.1 * @version $Id: BaseRuleFactory.java 1455194 2013-03-11 15:45:54Z luc $ */ public abstract class BaseRuleFactory { /** List of points and weights, indexed by the order of the rule. */ private final Map> pointsAndWeights = new TreeMap>(); /** Cache for double-precision rules. */ private final Map> pointsAndWeightsDouble = new TreeMap>(); /** * Gets a copy of the quadrature rule with the given number of integration * points. * * @param numberOfPoints Number of integration points. * @return a copy of the integration rule. * @throws NotStrictlyPositiveException if {@code numberOfPoints < 1}. * @throws DimensionMismatchException if the elements of the rule pair do not * have the same length. */ public Pair getRule(int numberOfPoints) throws NotStrictlyPositiveException, DimensionMismatchException { if (numberOfPoints <= 0) { throw new NotStrictlyPositiveException(LocalizedFormats.NUMBER_OF_POINTS, numberOfPoints); } // Try to obtain the rule from the cache. Pair cached = pointsAndWeightsDouble.get(numberOfPoints); if (cached == null) { // Rule not computed yet. // Compute the rule. final Pair rule = getRuleInternal(numberOfPoints); cached = convertToDouble(rule); // Cache it. pointsAndWeightsDouble.put(numberOfPoints, cached); } // Return a copy. return new Pair(cached.getFirst().clone(), cached.getSecond().clone()); } /** * Gets a rule. * Synchronization ensures that rules will be computed and added to the * cache at most once. * The returned rule is a reference into the cache. * * @param numberOfPoints Order of the rule to be retrieved. * @return the points and weights corresponding to the given order. * @throws DimensionMismatchException if the elements of the rule pair do not * have the same length. */ protected synchronized Pair getRuleInternal(int numberOfPoints) throws DimensionMismatchException { final Pair rule = pointsAndWeights.get(numberOfPoints); if (rule == null) { addRule(computeRule(numberOfPoints)); // The rule should be available now. return getRuleInternal(numberOfPoints); } return rule; } /** * Stores a rule. * * @param rule Rule to be stored. * @throws DimensionMismatchException if the elements of the pair do not * have the same length. */ protected void addRule(Pair rule) throws DimensionMismatchException { if (rule.getFirst().length != rule.getSecond().length) { throw new DimensionMismatchException(rule.getFirst().length, rule.getSecond().length); } pointsAndWeights.put(rule.getFirst().length, rule); } /** * Computes the rule for the given order. * * @param numberOfPoints Order of the rule to be computed. * @return the computed rule. * @throws DimensionMismatchException if the elements of the pair do not * have the same length. */ protected abstract Pair computeRule(int numberOfPoints) throws DimensionMismatchException; /** * Converts the from the actual {@code Number} type to {@code double} * * @param Type of the number used to represent the points and * weights of the quadrature rules. * @param rule Points and weights. * @return points and weights as {@code double}s. */ private static Pair convertToDouble(Pair rule) { final T[] pT = rule.getFirst(); final T[] wT = rule.getSecond(); final int len = pT.length; final double[] pD = new double[len]; final double[] wD = new double[len]; for (int i = 0; i < len; i++) { pD[i] = pT[i].doubleValue(); wD[i] = wT[i].doubleValue(); } return new Pair(pD, wD); } } ././@LongLink100644 0 0 164 12126630646 10262 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/analysis/integration/gauss/GaussIntegratorFactory.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/analysis/integration/gauss/GaussIntegra100644 1750 1750 14643 12126627701 32357 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.analysis.integration.gauss; import java.math.BigDecimal; import org.apache.commons.math3.exception.DimensionMismatchException; import org.apache.commons.math3.exception.NotStrictlyPositiveException; import org.apache.commons.math3.util.Pair; /** * Class that provides different ways to compute the nodes and weights to be * used by the {@link GaussIntegrator Gaussian integration rule}. * * @since 3.1 * @version $Id: GaussIntegratorFactory.java 1455194 2013-03-11 15:45:54Z luc $ */ public class GaussIntegratorFactory { /** Generator of Gauss-Legendre integrators. */ private final BaseRuleFactory legendre = new LegendreRuleFactory(); /** Generator of Gauss-Legendre integrators. */ private final BaseRuleFactory legendreHighPrecision = new LegendreHighPrecisionRuleFactory(); /** * Creates an integrator of the given order, and whose call to the * {@link GaussIntegrator#integrate(org.apache.commons.math3.analysis.UnivariateFunction) * integrate} method will perform an integration on the natural interval * {@code [-1 , 1]}. * * @param numberOfPoints Order of the integration rule. * @return a Gauss-Legendre integrator. */ public GaussIntegrator legendre(int numberOfPoints) { return new GaussIntegrator(getRule(legendre, numberOfPoints)); } /** * Creates an integrator of the given order, and whose call to the * {@link GaussIntegrator#integrate(org.apache.commons.math3.analysis.UnivariateFunction) * integrate} method will perform an integration on the given interval. * * @param numberOfPoints Order of the integration rule. * @param lowerBound Lower bound of the integration interval. * @param upperBound Upper bound of the integration interval. * @return a Gauss-Legendre integrator. * @throws NotStrictlyPositiveException if number of points is not positive */ public GaussIntegrator legendre(int numberOfPoints, double lowerBound, double upperBound) throws NotStrictlyPositiveException { return new GaussIntegrator(transform(getRule(legendre, numberOfPoints), lowerBound, upperBound)); } /** * Creates an integrator of the given order, and whose call to the * {@link GaussIntegrator#integrate(org.apache.commons.math3.analysis.UnivariateFunction) * integrate} method will perform an integration on the natural interval * {@code [-1 , 1]}. * * @param numberOfPoints Order of the integration rule. * @return a Gauss-Legendre integrator. * @throws NotStrictlyPositiveException if number of points is not positive */ public GaussIntegrator legendreHighPrecision(int numberOfPoints) throws NotStrictlyPositiveException { return new GaussIntegrator(getRule(legendreHighPrecision, numberOfPoints)); } /** * Creates an integrator of the given order, and whose call to the * {@link GaussIntegrator#integrate(org.apache.commons.math3.analysis.UnivariateFunction) * integrate} method will perform an integration on the given interval. * * @param numberOfPoints Order of the integration rule. * @param lowerBound Lower bound of the integration interval. * @param upperBound Upper bound of the integration interval. * @return a Gauss-Legendre integrator. * @throws NotStrictlyPositiveException if number of points is not positive */ public GaussIntegrator legendreHighPrecision(int numberOfPoints, double lowerBound, double upperBound) throws NotStrictlyPositiveException { return new GaussIntegrator(transform(getRule(legendreHighPrecision, numberOfPoints), lowerBound, upperBound)); } /** * @param factory Integration rule factory. * @param numberOfPoints Order of the integration rule. * @return the integration nodes and weights. * @throws NotStrictlyPositiveException if number of points is not positive * @throws DimensionMismatchException if the elements of the rule pair do not * have the same length. */ private static Pair getRule(BaseRuleFactory factory, int numberOfPoints) throws NotStrictlyPositiveException, DimensionMismatchException { return factory.getRule(numberOfPoints); } /** * Performs a change of variable so that the integration can be performed * on an arbitrary interval {@code [a, b]}. * It is assumed that the natural interval is {@code [-1, 1]}. * * @param rule Original points and weights. * @param a Lower bound of the integration interval. * @param b Lower bound of the integration interval. * @return the points and weights adapted to the new interval. */ private static Pair transform(Pair rule, double a, double b) { final double[] points = rule.getFirst(); final double[] weights = rule.getSecond(); // Scaling final double scale = (b - a) / 2; final double shift = a + scale; for (int i = 0; i < points.length; i++) { points[i] = points[i] * scale + shift; weights[i] *= scale; } return new Pair(points, weights); } } ././@LongLink100644 0 0 155 12126630646 10262 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/analysis/integration/gauss/GaussIntegrator.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/analysis/integration/gauss/GaussIntegra100644 1750 1750 10207 12126627701 32347 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.analysis.integration.gauss; import org.apache.commons.math3.analysis.UnivariateFunction; import org.apache.commons.math3.exception.DimensionMismatchException; import org.apache.commons.math3.exception.NonMonotonicSequenceException; import org.apache.commons.math3.util.MathArrays; import org.apache.commons.math3.util.Pair; /** * Class that implements the Gaussian rule for * {@link #integrate(UnivariateFunction) integrating} a weighted * function. * * @since 3.1 * @version $Id: GaussIntegrator.java 1455194 2013-03-11 15:45:54Z luc $ */ public class GaussIntegrator { /** Nodes. */ private final double[] points; /** Nodes weights. */ private final double[] weights; /** * Creates an integrator from the given {@code points} and {@code weights}. * The integration interval is defined by the first and last value of * {@code points} which must be sorted in increasing order. * * @param points Integration points. * @param weights Weights of the corresponding integration nodes. * @throws NonMonotonicSequenceException if the {@code points} are not * sorted in increasing order. * @throws DimensionMismatchException if points and weights don't have the same length */ public GaussIntegrator(double[] points, double[] weights) throws NonMonotonicSequenceException, DimensionMismatchException { if (points.length != weights.length) { throw new DimensionMismatchException(points.length, weights.length); } MathArrays.checkOrder(points, MathArrays.OrderDirection.INCREASING, true, true); this.points = points.clone(); this.weights = weights.clone(); } /** * Creates an integrator from the given pair of points (first element of * the pair) and weights (second element of the pair. * * @param pointsAndWeights Integration points and corresponding weights. * @throws NonMonotonicSequenceException if the {@code points} are not * sorted in increasing order. * * @see #GaussIntegrator(double[], double[]) */ public GaussIntegrator(Pair pointsAndWeights) throws NonMonotonicSequenceException { this(pointsAndWeights.getFirst(), pointsAndWeights.getSecond()); } /** * Returns an estimate of the integral of {@code f(x) * w(x)}, * where {@code w} is a weight function that depends on the actual * flavor of the Gauss integration scheme. * The algorithm uses the points and associated weights, as passed * to the {@link #GaussIntegrator(double[],double[]) constructor}. * * @param f Function to integrate. * @return the integral of the weighted function. */ public double integrate(UnivariateFunction f) { double s = 0; double c = 0; for (int i = 0; i < points.length; i++) { final double x = points[i]; final double w = weights[i]; final double y = w * f.value(x) - c; final double t = s + y; c = (t - s) - y; s = t; } return s; } /** * @return the order of the integration rule (the number of integration * points). */ public int getNumberOfPoints() { return points.length; } } ././@LongLink100644 0 0 170 12126630646 10257 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/analysis/integration/BaseAbstractUnivariateIntegrator.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/analysis/integration/BaseAbstractUnivar100644 1750 1750 25136 12126627701 32363 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.analysis.integration; import org.apache.commons.math3.analysis.UnivariateFunction; import org.apache.commons.math3.analysis.solvers.UnivariateSolverUtils; import org.apache.commons.math3.exception.MathIllegalArgumentException; import org.apache.commons.math3.exception.MaxCountExceededException; import org.apache.commons.math3.exception.NotStrictlyPositiveException; import org.apache.commons.math3.exception.NullArgumentException; import org.apache.commons.math3.exception.NumberIsTooSmallException; import org.apache.commons.math3.exception.TooManyEvaluationsException; import org.apache.commons.math3.util.Incrementor; import org.apache.commons.math3.util.MathUtils; /** * Provide a default implementation for several generic functions. * * @version $Id: BaseAbstractUnivariateIntegrator.java 1455194 2013-03-11 15:45:54Z luc $ * @since 1.2 */ public abstract class BaseAbstractUnivariateIntegrator implements UnivariateIntegrator { /** Default absolute accuracy. */ public static final double DEFAULT_ABSOLUTE_ACCURACY = 1.0e-15; /** Default relative accuracy. */ public static final double DEFAULT_RELATIVE_ACCURACY = 1.0e-6; /** Default minimal iteration count. */ public static final int DEFAULT_MIN_ITERATIONS_COUNT = 3; /** Default maximal iteration count. */ public static final int DEFAULT_MAX_ITERATIONS_COUNT = Integer.MAX_VALUE; /** The iteration count. */ protected final Incrementor iterations; /** Maximum absolute error. */ private final double absoluteAccuracy; /** Maximum relative error. */ private final double relativeAccuracy; /** minimum number of iterations */ private final int minimalIterationCount; /** The functions evaluation count. */ private final Incrementor evaluations; /** Function to integrate. */ private UnivariateFunction function; /** Lower bound for the interval. */ private double min; /** Upper bound for the interval. */ private double max; /** * Construct an integrator with given accuracies and iteration counts. *

                            * The meanings of the various parameters are: *

                              *
                            • relative accuracy: * this is used to stop iterations if the absolute accuracy can't be * achieved due to large values or short mantissa length. If this * should be the primary criterion for convergence rather then a * safety measure, set the absolute accuracy to a ridiculously small value, * like {@link org.apache.commons.math3.util.Precision#SAFE_MIN Precision.SAFE_MIN}.
                            • *
                            • absolute accuracy: * The default is usually chosen so that results in the interval * -10..-0.1 and +0.1..+10 can be found with a reasonable accuracy. If the * expected absolute value of your results is of much smaller magnitude, set * this to a smaller value.
                            • *
                            • minimum number of iterations: * minimal iteration is needed to avoid false early convergence, e.g. * the sample points happen to be zeroes of the function. Users can * use the default value or choose one that they see as appropriate.
                            • *
                            • maximum number of iterations: * usually a high iteration count indicates convergence problems. However, * the "reasonable value" varies widely for different algorithms. Users are * advised to use the default value supplied by the algorithm.
                            • *
                            *

                            * @param relativeAccuracy relative accuracy of the result * @param absoluteAccuracy absolute accuracy of the result * @param minimalIterationCount minimum number of iterations * @param maximalIterationCount maximum number of iterations * @exception NotStrictlyPositiveException if minimal number of iterations * is not strictly positive * @exception NumberIsTooSmallException if maximal number of iterations * is lesser than or equal to the minimal number of iterations */ protected BaseAbstractUnivariateIntegrator(final double relativeAccuracy, final double absoluteAccuracy, final int minimalIterationCount, final int maximalIterationCount) throws NotStrictlyPositiveException, NumberIsTooSmallException { // accuracy settings this.relativeAccuracy = relativeAccuracy; this.absoluteAccuracy = absoluteAccuracy; // iterations count settings if (minimalIterationCount <= 0) { throw new NotStrictlyPositiveException(minimalIterationCount); } if (maximalIterationCount <= minimalIterationCount) { throw new NumberIsTooSmallException(maximalIterationCount, minimalIterationCount, false); } this.minimalIterationCount = minimalIterationCount; this.iterations = new Incrementor(); iterations.setMaximalCount(maximalIterationCount); // prepare evaluations counter, but do not set it yet evaluations = new Incrementor(); } /** * Construct an integrator with given accuracies. * @param relativeAccuracy relative accuracy of the result * @param absoluteAccuracy absolute accuracy of the result */ protected BaseAbstractUnivariateIntegrator(final double relativeAccuracy, final double absoluteAccuracy) { this(relativeAccuracy, absoluteAccuracy, DEFAULT_MIN_ITERATIONS_COUNT, DEFAULT_MAX_ITERATIONS_COUNT); } /** * Construct an integrator with given iteration counts. * @param minimalIterationCount minimum number of iterations * @param maximalIterationCount maximum number of iterations * @exception NotStrictlyPositiveException if minimal number of iterations * is not strictly positive * @exception NumberIsTooSmallException if maximal number of iterations * is lesser than or equal to the minimal number of iterations */ protected BaseAbstractUnivariateIntegrator(final int minimalIterationCount, final int maximalIterationCount) throws NotStrictlyPositiveException, NumberIsTooSmallException { this(DEFAULT_RELATIVE_ACCURACY, DEFAULT_ABSOLUTE_ACCURACY, minimalIterationCount, maximalIterationCount); } /** {@inheritDoc} */ public double getRelativeAccuracy() { return relativeAccuracy; } /** {@inheritDoc} */ public double getAbsoluteAccuracy() { return absoluteAccuracy; } /** {@inheritDoc} */ public int getMinimalIterationCount() { return minimalIterationCount; } /** {@inheritDoc} */ public int getMaximalIterationCount() { return iterations.getMaximalCount(); } /** {@inheritDoc} */ public int getEvaluations() { return evaluations.getCount(); } /** {@inheritDoc} */ public int getIterations() { return iterations.getCount(); } /** * @return the lower bound. */ protected double getMin() { return min; } /** * @return the upper bound. */ protected double getMax() { return max; } /** * Compute the objective function value. * * @param point Point at which the objective function must be evaluated. * @return the objective function value at specified point. * @throws TooManyEvaluationsException if the maximal number of function * evaluations is exceeded. */ protected double computeObjectiveValue(final double point) throws TooManyEvaluationsException { try { evaluations.incrementCount(); } catch (MaxCountExceededException e) { throw new TooManyEvaluationsException(e.getMax()); } return function.value(point); } /** * Prepare for computation. * Subclasses must call this method if they override any of the * {@code solve} methods. * * @param maxEval Maximum number of evaluations. * @param f the integrand function * @param lower the min bound for the interval * @param upper the upper bound for the interval * @throws NullArgumentException if {@code f} is {@code null}. * @throws MathIllegalArgumentException if {@code min >= max}. */ protected void setup(final int maxEval, final UnivariateFunction f, final double lower, final double upper) throws NullArgumentException, MathIllegalArgumentException { // Checks. MathUtils.checkNotNull(f); UnivariateSolverUtils.verifyInterval(lower, upper); // Reset. min = lower; max = upper; function = f; evaluations.setMaximalCount(maxEval); evaluations.resetCount(); iterations.resetCount(); } /** {@inheritDoc} */ public double integrate(final int maxEval, final UnivariateFunction f, final double lower, final double upper) throws TooManyEvaluationsException, MaxCountExceededException, MathIllegalArgumentException, NullArgumentException { // Initialization. setup(maxEval, f, lower, upper); // Perform computation. return doIntegrate(); } /** * Method for implementing actual integration algorithms in derived * classes. * * @return the root. * @throws TooManyEvaluationsException if the maximal number of evaluations * is exceeded. * @throws MaxCountExceededException if the maximum iteration count is exceeded * or the integrator detects convergence problems otherwise */ protected abstract double doIntegrate() throws TooManyEvaluationsException, MaxCountExceededException; } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/analysis/UnivariateFunction.java100644 1750 1750 5531 12126627707 31035 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.analysis; /** * An interface representing a univariate real function. *
                            * When a user-defined function encounters an error during * evaluation, the {@link #value(double) value} method should throw a * user-defined unchecked exception. *
                            * The following code excerpt shows the recommended way to do that using * a root solver as an example, but the same construct is applicable to * ODE integrators or optimizers. * *
                             * private static class LocalException extends RuntimeException {
                             *     // The x value that caused the problem.
                             *     private final double x;
                             *
                             *     public LocalException(double x) {
                             *         this.x = x;
                             *     }
                             *
                             *     public double getX() {
                             *         return x;
                             *     }
                             * }
                             *
                             * private static class MyFunction implements UnivariateFunction {
                             *     public double value(double x) {
                             *         double y = hugeFormula(x);
                             *         if (somethingBadHappens) {
                             *           throw new LocalException(x);
                             *         }
                             *         return y;
                             *     }
                             * }
                             *
                             * public void compute() {
                             *     try {
                             *         solver.solve(maxEval, new MyFunction(a, b, c), min, max);
                             *     } catch (LocalException le) {
                             *         // Retrieve the x value.
                             *     }
                             * }
                             * 
                            * * As shown, the exception is local to the user's code and it is guaranteed * that Apache Commons Math will not catch it. * * @version $Id: UnivariateFunction.java 1364387 2012-07-22 18:14:11Z tn $ */ public interface UnivariateFunction { /** * Compute the value of the function. * * @param x Point at which the function value should be computed. * @return the value of the function. * @throws IllegalArgumentException when the activated method itself can * ascertain that a precondition, specified in the API expressed at the * level of the activated method, has been violated. * When Commons Math throws an {@code IllegalArgumentException}, it is * usually the consequence of checking the actual parameters passed to * the method. */ double value(double x); } ././@LongLink100644 0 0 162 12126630646 10260 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/analysis/DifferentiableUnivariateMatrixFunction.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/analysis/DifferentiableUnivariateMatrix100644 1750 1750 2665 12126627707 32425 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.analysis; /** * Extension of {@link UnivariateMatrixFunction} representing a differentiable univariate matrix function. * * @version $Id: DifferentiableUnivariateMatrixFunction.java 1383854 2012-09-12 08:55:32Z luc $ * @since 2.0 * @deprecated as of 3.1 replaced by {@link org.apache.commons.math3.analysis.differentiation.UnivariateDifferentiableMatrixFunction} */ public interface DifferentiableUnivariateMatrixFunction extends UnivariateMatrixFunction { /** * Returns the derivative of the function * * @return the derivative function */ UnivariateMatrixFunction derivative(); } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/analysis/BivariateFunction.java100644 1750 1750 2451 12126627707 30632 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.analysis; /** * An interface representing a bivariate real function. * * @since 2.1 * @version $Id: BivariateFunction.java 1364387 2012-07-22 18:14:11Z tn $ */ public interface BivariateFunction { /** * Compute the value for the function. * * @param x Abscissa for which the function value should be computed. * @param y Ordinate for which the function value should be computed. * @return the value. */ double value(double x, double y); } ././@LongLink100644 0 0 146 12126630646 10262 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/analysis/MultivariateMatrixFunction.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/analysis/MultivariateMatrixFunction.jav100644 1750 1750 2606 12126627707 32420 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.analysis; /** * An interface representing a multivariate matrix function. * @version $Id: MultivariateMatrixFunction.java 1462485 2013-03-29 14:36:19Z psteitz $ * @since 2.0 */ public interface MultivariateMatrixFunction { /** * Compute the value for the function at the given point. * @param point point at which the function must be evaluated * @return function value for the given point * @exception IllegalArgumentException if point's dimension is wrong */ double[][] value(double[] point) throws IllegalArgumentException; } ././@LongLink100644 0 0 146 12126630646 10262 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/analysis/MultivariateVectorFunction.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/analysis/MultivariateVectorFunction.jav100644 1750 1750 2607 12126627707 32417 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.analysis; /** * An interface representing a multivariate vectorial function. * @version $Id: MultivariateVectorFunction.java 1462485 2013-03-29 14:36:19Z psteitz $ * @since 2.0 */ public interface MultivariateVectorFunction { /** * Compute the value for the function at the given point. * @param point point at which the function must be evaluated * @return function value for the given point * @exception IllegalArgumentException if point's dimension is wrong */ double[] value(double[] point) throws IllegalArgumentException; } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/analysis/FunctionUtils.java100644 1750 1750 74721 12126627707 30055 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.analysis; import org.apache.commons.math3.analysis.differentiation.DerivativeStructure; import org.apache.commons.math3.analysis.differentiation.MultivariateDifferentiableFunction; import org.apache.commons.math3.analysis.differentiation.MultivariateDifferentiableVectorFunction; import org.apache.commons.math3.analysis.differentiation.UnivariateDifferentiableFunction; import org.apache.commons.math3.analysis.function.Identity; import org.apache.commons.math3.exception.DimensionMismatchException; import org.apache.commons.math3.exception.NotStrictlyPositiveException; import org.apache.commons.math3.exception.NumberIsTooLargeException; import org.apache.commons.math3.exception.util.LocalizedFormats; /** * Utilities for manipulating function objects. * * @version $Id: FunctionUtils.java 1455194 2013-03-11 15:45:54Z luc $ * @since 3.0 */ public class FunctionUtils { /** * Class only contains static methods. */ private FunctionUtils() {} /** * Composes functions. *
                            * The functions in the argument list are composed sequentially, in the * given order. For example, compose(f1,f2,f3) acts like f1(f2(f3(x))). * * @param f List of functions. * @return the composite function. */ public static UnivariateFunction compose(final UnivariateFunction ... f) { return new UnivariateFunction() { /** {@inheritDoc} */ public double value(double x) { double r = x; for (int i = f.length - 1; i >= 0; i--) { r = f[i].value(r); } return r; } }; } /** * Composes functions. *
                            * The functions in the argument list are composed sequentially, in the * given order. For example, compose(f1,f2,f3) acts like f1(f2(f3(x))). * * @param f List of functions. * @return the composite function. * @since 3.1 */ public static UnivariateDifferentiableFunction compose(final UnivariateDifferentiableFunction ... f) { return new UnivariateDifferentiableFunction() { /** {@inheritDoc} */ public double value(final double t) { double r = t; for (int i = f.length - 1; i >= 0; i--) { r = f[i].value(r); } return r; } /** {@inheritDoc} */ public DerivativeStructure value(final DerivativeStructure t) { DerivativeStructure r = t; for (int i = f.length - 1; i >= 0; i--) { r = f[i].value(r); } return r; } }; } /** * Composes functions. *
                            * The functions in the argument list are composed sequentially, in the * given order. For example, compose(f1,f2,f3) acts like f1(f2(f3(x))). * * @param f List of functions. * @return the composite function. * @deprecated as of 3.1 replaced by {@link #compose(UnivariateDifferentiableFunction...)} */ public static DifferentiableUnivariateFunction compose(final DifferentiableUnivariateFunction ... f) { return new DifferentiableUnivariateFunction() { /** {@inheritDoc} */ public double value(double x) { double r = x; for (int i = f.length - 1; i >= 0; i--) { r = f[i].value(r); } return r; } /** {@inheritDoc} */ public UnivariateFunction derivative() { return new UnivariateFunction() { /** {@inheritDoc} */ public double value(double x) { double p = 1; double r = x; for (int i = f.length - 1; i >= 0; i--) { p *= f[i].derivative().value(r); r = f[i].value(r); } return p; } }; } }; } /** * Adds functions. * * @param f List of functions. * @return a function that computes the sum of the functions. */ public static UnivariateFunction add(final UnivariateFunction ... f) { return new UnivariateFunction() { /** {@inheritDoc} */ public double value(double x) { double r = f[0].value(x); for (int i = 1; i < f.length; i++) { r += f[i].value(x); } return r; } }; } /** * Adds functions. * * @param f List of functions. * @return a function that computes the sum of the functions. * @since 3.1 */ public static UnivariateDifferentiableFunction add(final UnivariateDifferentiableFunction ... f) { return new UnivariateDifferentiableFunction() { /** {@inheritDoc} */ public double value(final double t) { double r = f[0].value(t); for (int i = 1; i < f.length; i++) { r += f[i].value(t); } return r; } /** {@inheritDoc} * @throws DimensionMismatchException if functions are not consistent with each other */ public DerivativeStructure value(final DerivativeStructure t) throws DimensionMismatchException { DerivativeStructure r = f[0].value(t); for (int i = 1; i < f.length; i++) { r = r.add(f[i].value(t)); } return r; } }; } /** * Adds functions. * * @param f List of functions. * @return a function that computes the sum of the functions. * @deprecated as of 3.1 replaced by {@link #add(UnivariateDifferentiableFunction...)} */ @Deprecated public static DifferentiableUnivariateFunction add(final DifferentiableUnivariateFunction ... f) { return new DifferentiableUnivariateFunction() { /** {@inheritDoc} */ public double value(double x) { double r = f[0].value(x); for (int i = 1; i < f.length; i++) { r += f[i].value(x); } return r; } /** {@inheritDoc} */ public UnivariateFunction derivative() { return new UnivariateFunction() { /** {@inheritDoc} */ public double value(double x) { double r = f[0].derivative().value(x); for (int i = 1; i < f.length; i++) { r += f[i].derivative().value(x); } return r; } }; } }; } /** * Multiplies functions. * * @param f List of functions. * @return a function that computes the product of the functions. */ public static UnivariateFunction multiply(final UnivariateFunction ... f) { return new UnivariateFunction() { /** {@inheritDoc} */ public double value(double x) { double r = f[0].value(x); for (int i = 1; i < f.length; i++) { r *= f[i].value(x); } return r; } }; } /** * Multiplies functions. * * @param f List of functions. * @return a function that computes the product of the functions. * @since 3.1 */ public static UnivariateDifferentiableFunction multiply(final UnivariateDifferentiableFunction ... f) { return new UnivariateDifferentiableFunction() { /** {@inheritDoc} */ public double value(final double t) { double r = f[0].value(t); for (int i = 1; i < f.length; i++) { r *= f[i].value(t); } return r; } /** {@inheritDoc} */ public DerivativeStructure value(final DerivativeStructure t) { DerivativeStructure r = f[0].value(t); for (int i = 1; i < f.length; i++) { r = r.multiply(f[i].value(t)); } return r; } }; } /** * Multiplies functions. * * @param f List of functions. * @return a function that computes the product of the functions. * @deprecated as of 3.1 replaced by {@link #multiply(UnivariateDifferentiableFunction...)} */ public static DifferentiableUnivariateFunction multiply(final DifferentiableUnivariateFunction ... f) { return new DifferentiableUnivariateFunction() { /** {@inheritDoc} */ public double value(double x) { double r = f[0].value(x); for (int i = 1; i < f.length; i++) { r *= f[i].value(x); } return r; } /** {@inheritDoc} */ public UnivariateFunction derivative() { return new UnivariateFunction() { /** {@inheritDoc} */ public double value(double x) { double sum = 0; for (int i = 0; i < f.length; i++) { double prod = f[i].derivative().value(x); for (int j = 0; j < f.length; j++) { if (i != j) { prod *= f[j].value(x); } } sum += prod; } return sum; } }; } }; } /** * Returns the univariate function
                            * {@code h(x) = combiner(f(x), g(x))}. * * @param combiner Combiner function. * @param f Function. * @param g Function. * @return the composite function. */ public static UnivariateFunction combine(final BivariateFunction combiner, final UnivariateFunction f, final UnivariateFunction g) { return new UnivariateFunction() { /** {@inheritDoc} */ public double value(double x) { return combiner.value(f.value(x), g.value(x)); } }; } /** * Returns a MultivariateFunction h(x[]) defined by
                             
                                 * h(x[]) = combiner(...combiner(combiner(initialValue,f(x[0])),f(x[1]))...),f(x[x.length-1]))
                                 * 
                            * * @param combiner Combiner function. * @param f Function. * @param initialValue Initial value. * @return a collector function. */ public static MultivariateFunction collector(final BivariateFunction combiner, final UnivariateFunction f, final double initialValue) { return new MultivariateFunction() { /** {@inheritDoc} */ public double value(double[] point) { double result = combiner.value(initialValue, f.value(point[0])); for (int i = 1; i < point.length; i++) { result = combiner.value(result, f.value(point[i])); } return result; } }; } /** * Returns a MultivariateFunction h(x[]) defined by
                             
                                 * h(x[]) = combiner(...combiner(combiner(initialValue,x[0]),x[1])...),x[x.length-1])
                                 * 
                            * * @param combiner Combiner function. * @param initialValue Initial value. * @return a collector function. */ public static MultivariateFunction collector(final BivariateFunction combiner, final double initialValue) { return collector(combiner, new Identity(), initialValue); } /** * Creates a unary function by fixing the first argument of a binary function. * * @param f Binary function. * @param fixed Value to which the first argument of {@code f} is set. * @return the unary function h(x) = f(fixed, x) */ public static UnivariateFunction fix1stArgument(final BivariateFunction f, final double fixed) { return new UnivariateFunction() { /** {@inheritDoc} */ public double value(double x) { return f.value(fixed, x); } }; } /** * Creates a unary function by fixing the second argument of a binary function. * * @param f Binary function. * @param fixed Value to which the second argument of {@code f} is set. * @return the unary function h(x) = f(x, fixed) */ public static UnivariateFunction fix2ndArgument(final BivariateFunction f, final double fixed) { return new UnivariateFunction() { /** {@inheritDoc} */ public double value(double x) { return f.value(x, fixed); } }; } /** * Samples the specified univariate real function on the specified interval. *
                            * The interval is divided equally into {@code n} sections and sample points * are taken from {@code min} to {@code max - (max - min) / n}; therefore * {@code f} is not sampled at the upper bound {@code max}. * * @param f Function to be sampled * @param min Lower bound of the interval (included). * @param max Upper bound of the interval (excluded). * @param n Number of sample points. * @return the array of samples. * @throws NumberIsTooLargeException if the lower bound {@code min} is * greater than, or equal to the upper bound {@code max}. * @throws NotStrictlyPositiveException if the number of sample points * {@code n} is negative. */ public static double[] sample(UnivariateFunction f, double min, double max, int n) throws NumberIsTooLargeException, NotStrictlyPositiveException { if (n <= 0) { throw new NotStrictlyPositiveException( LocalizedFormats.NOT_POSITIVE_NUMBER_OF_SAMPLES, Integer.valueOf(n)); } if (min >= max) { throw new NumberIsTooLargeException(min, max, false); } final double[] s = new double[n]; final double h = (max - min) / n; for (int i = 0; i < n; i++) { s[i] = f.value(min + i * h); } return s; } /** Convert a {@link UnivariateDifferentiableFunction} into a {@link DifferentiableUnivariateFunction}. * @param f function to convert * @return converted function * @deprecated this conversion method is temporary in version 3.1, as the {@link * DifferentiableUnivariateFunction} interface itself is deprecated */ @Deprecated public static DifferentiableUnivariateFunction toDifferentiableUnivariateFunction(final UnivariateDifferentiableFunction f) { return new DifferentiableUnivariateFunction() { /** {@inheritDoc} */ public double value(final double x) { return f.value(x); } /** {@inheritDoc} */ public UnivariateFunction derivative() { return new UnivariateFunction() { /** {@inheritDoc} */ public double value(final double x) { return f.value(new DerivativeStructure(1, 1, 0, x)).getPartialDerivative(1); } }; } }; } /** Convert a {@link DifferentiableUnivariateFunction} into a {@link UnivariateDifferentiableFunction}. *

                            * Note that the converted function is able to handle {@link DerivativeStructure} up to order one. * If the function is called with higher order, a {@link NumberIsTooLargeException} will be thrown. *

                            * @param f function to convert * @return converted function * @deprecated this conversion method is temporary in version 3.1, as the {@link * DifferentiableUnivariateFunction} interface itself is deprecated */ @Deprecated public static UnivariateDifferentiableFunction toUnivariateDifferential(final DifferentiableUnivariateFunction f) { return new UnivariateDifferentiableFunction() { /** {@inheritDoc} */ public double value(final double x) { return f.value(x); } /** {@inheritDoc} * @exception NumberIsTooLargeException if derivation order is greater than 1 */ public DerivativeStructure value(final DerivativeStructure t) throws NumberIsTooLargeException { switch (t.getOrder()) { case 0 : return new DerivativeStructure(t.getFreeParameters(), 0, f.value(t.getValue())); case 1 : { final int parameters = t.getFreeParameters(); final double[] derivatives = new double[parameters + 1]; derivatives[0] = f.value(t.getValue()); final double fPrime = f.derivative().value(t.getValue()); int[] orders = new int[parameters]; for (int i = 0; i < parameters; ++i) { orders[i] = 1; derivatives[i + 1] = fPrime * t.getPartialDerivative(orders); orders[i] = 0; } return new DerivativeStructure(parameters, 1, derivatives); } default : throw new NumberIsTooLargeException(t.getOrder(), 1, true); } } }; } /** Convert a {@link MultivariateDifferentiableFunction} into a {@link DifferentiableMultivariateFunction}. * @param f function to convert * @return converted function * @deprecated this conversion method is temporary in version 3.1, as the {@link * DifferentiableMultivariateFunction} interface itself is deprecated */ @Deprecated public static DifferentiableMultivariateFunction toDifferentiableMultivariateFunction(final MultivariateDifferentiableFunction f) { return new DifferentiableMultivariateFunction() { /** {@inheritDoc} */ public double value(final double[] x) { return f.value(x); } /** {@inheritDoc} */ public MultivariateFunction partialDerivative(final int k) { return new MultivariateFunction() { /** {@inheritDoc} */ public double value(final double[] x) { final int n = x.length; // delegate computation to underlying function final DerivativeStructure[] dsX = new DerivativeStructure[n]; for (int i = 0; i < n; ++i) { if (i == k) { dsX[i] = new DerivativeStructure(1, 1, 0, x[i]); } else { dsX[i] = new DerivativeStructure(1, 1, x[i]); } } final DerivativeStructure y = f.value(dsX); // extract partial derivative return y.getPartialDerivative(1); } }; } public MultivariateVectorFunction gradient() { return new MultivariateVectorFunction() { /** {@inheritDoc} */ public double[] value(final double[] x) { final int n = x.length; // delegate computation to underlying function final DerivativeStructure[] dsX = new DerivativeStructure[n]; for (int i = 0; i < n; ++i) { dsX[i] = new DerivativeStructure(n, 1, i, x[i]); } final DerivativeStructure y = f.value(dsX); // extract gradient final double[] gradient = new double[n]; final int[] orders = new int[n]; for (int i = 0; i < n; ++i) { orders[i] = 1; gradient[i] = y.getPartialDerivative(orders); orders[i] = 0; } return gradient; } }; } }; } /** Convert a {@link DifferentiableMultivariateFunction} into a {@link MultivariateDifferentiableFunction}. *

                            * Note that the converted function is able to handle {@link DerivativeStructure} elements * that all have the same number of free parameters and order, and with order at most 1. * If the function is called with inconsistent numbers of free parameters or higher order, a * {@link DimensionMismatchException} or a {@link NumberIsTooLargeException} will be thrown. *

                            * @param f function to convert * @return converted function * @deprecated this conversion method is temporary in version 3.1, as the {@link * DifferentiableMultivariateFunction} interface itself is deprecated */ @Deprecated public static MultivariateDifferentiableFunction toMultivariateDifferentiableFunction(final DifferentiableMultivariateFunction f) { return new MultivariateDifferentiableFunction() { /** {@inheritDoc} */ public double value(final double[] x) { return f.value(x); } /** {@inheritDoc} * @exception NumberIsTooLargeException if derivation order is higher than 1 * @exception DimensionMismatchException if numbers of free parameters are inconsistent */ public DerivativeStructure value(final DerivativeStructure[] t) throws DimensionMismatchException, NumberIsTooLargeException { // check parameters and orders limits final int parameters = t[0].getFreeParameters(); final int order = t[0].getOrder(); final int n = t.length; if (order > 1) { throw new NumberIsTooLargeException(order, 1, true); } // check all elements in the array are consistent for (int i = 0; i < n; ++i) { if (t[i].getFreeParameters() != parameters) { throw new DimensionMismatchException(t[i].getFreeParameters(), parameters); } if (t[i].getOrder() != order) { throw new DimensionMismatchException(t[i].getOrder(), order); } } // delegate computation to underlying function final double[] point = new double[n]; for (int i = 0; i < n; ++i) { point[i] = t[i].getValue(); } final double value = f.value(point); final double[] gradient = f.gradient().value(point); // merge value and gradient into one DerivativeStructure final double[] derivatives = new double[parameters + 1]; derivatives[0] = value; final int[] orders = new int[parameters]; for (int i = 0; i < parameters; ++i) { orders[i] = 1; for (int j = 0; j < n; ++j) { derivatives[i + 1] += gradient[j] * t[j].getPartialDerivative(orders); } orders[i] = 0; } return new DerivativeStructure(parameters, order, derivatives); } }; } /** Convert a {@link MultivariateDifferentiableVectorFunction} into a {@link DifferentiableMultivariateVectorFunction}. * @param f function to convert * @return converted function * @deprecated this conversion method is temporary in version 3.1, as the {@link * DifferentiableMultivariateVectorFunction} interface itself is deprecated */ @Deprecated public static DifferentiableMultivariateVectorFunction toDifferentiableMultivariateVectorFunction(final MultivariateDifferentiableVectorFunction f) { return new DifferentiableMultivariateVectorFunction() { /** {@inheritDoc} */ public double[] value(final double[] x) { return f.value(x); } public MultivariateMatrixFunction jacobian() { return new MultivariateMatrixFunction() { /** {@inheritDoc} */ public double[][] value(final double[] x) { final int n = x.length; // delegate computation to underlying function final DerivativeStructure[] dsX = new DerivativeStructure[n]; for (int i = 0; i < n; ++i) { dsX[i] = new DerivativeStructure(n, 1, i, x[i]); } final DerivativeStructure[] y = f.value(dsX); // extract Jacobian final double[][] jacobian = new double[y.length][n]; final int[] orders = new int[n]; for (int i = 0; i < y.length; ++i) { for (int j = 0; j < n; ++j) { orders[j] = 1; jacobian[i][j] = y[i].getPartialDerivative(orders); orders[j] = 0; } } return jacobian; } }; } }; } /** Convert a {@link DifferentiableMultivariateVectorFunction} into a {@link MultivariateDifferentiableVectorFunction}. *

                            * Note that the converted function is able to handle {@link DerivativeStructure} elements * that all have the same number of free parameters and order, and with order at most 1. * If the function is called with inconsistent numbers of free parameters or higher order, a * {@link DimensionMismatchException} or a {@link NumberIsTooLargeException} will be thrown. *

                            * @param f function to convert * @return converted function * @deprecated this conversion method is temporary in version 3.1, as the {@link * DifferentiableMultivariateFunction} interface itself is deprecated */ @Deprecated public static MultivariateDifferentiableVectorFunction toMultivariateDifferentiableVectorFunction(final DifferentiableMultivariateVectorFunction f) { return new MultivariateDifferentiableVectorFunction() { /** {@inheritDoc} */ public double[] value(final double[] x) { return f.value(x); } /** {@inheritDoc} * @exception NumberIsTooLargeException if derivation order is higher than 1 * @exception DimensionMismatchException if numbers of free parameters are inconsistent */ public DerivativeStructure[] value(final DerivativeStructure[] t) throws DimensionMismatchException, NumberIsTooLargeException { // check parameters and orders limits final int parameters = t[0].getFreeParameters(); final int order = t[0].getOrder(); final int n = t.length; if (order > 1) { throw new NumberIsTooLargeException(order, 1, true); } // check all elements in the array are consistent for (int i = 0; i < n; ++i) { if (t[i].getFreeParameters() != parameters) { throw new DimensionMismatchException(t[i].getFreeParameters(), parameters); } if (t[i].getOrder() != order) { throw new DimensionMismatchException(t[i].getOrder(), order); } } // delegate computation to underlying function final double[] point = new double[n]; for (int i = 0; i < n; ++i) { point[i] = t[i].getValue(); } final double[] value = f.value(point); final double[][] jacobian = f.jacobian().value(point); // merge value and Jacobian into a DerivativeStructure array final DerivativeStructure[] merged = new DerivativeStructure[value.length]; for (int k = 0; k < merged.length; ++k) { final double[] derivatives = new double[parameters + 1]; derivatives[0] = value[k]; final int[] orders = new int[parameters]; for (int i = 0; i < parameters; ++i) { orders[i] = 1; for (int j = 0; j < n; ++j) { derivatives[i + 1] += jacobian[k][j] * t[j].getPartialDerivative(orders); } orders[i] = 0; } merged[k] = new DerivativeStructure(parameters, order, derivatives); } return merged; } }; } } ././@LongLink100644 0 0 150 12126630646 10255 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/analysis/ParametricUnivariateFunction.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/analysis/ParametricUnivariateFunction.j100644 1750 1750 3223 12126627707 32351 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.analysis; /** * An interface representing a real function that depends on one independent * variable plus some extra parameters. * * @since 3.0 * @version $Id: ParametricUnivariateFunction.java 1364387 2012-07-22 18:14:11Z tn $ */ public interface ParametricUnivariateFunction { /** * Compute the value of the function. * * @param x Point for which the function value should be computed. * @param parameters Function parameters. * @return the value. */ double value(double x, double ... parameters); /** * Compute the gradient of the function with respect to its parameters. * * @param x Point for which the function value should be computed. * @param parameters Function parameters. * @return the value. */ double[] gradient(double x, double ... parameters); } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/analysis/UnivariateVectorFunction.java100644 1750 1750 2343 12126627707 32216 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.analysis; /** * An interface representing a univariate vectorial function. * * @version $Id: UnivariateVectorFunction.java 1364387 2012-07-22 18:14:11Z tn $ * @since 2.0 */ public interface UnivariateVectorFunction { /** * Compute the value for the function. * @param x the point for which the function value should be computed * @return the value */ double[] value(double x); } ././@LongLink100644 0 0 162 12126630646 10260 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/analysis/DifferentiableUnivariateVectorFunction.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/analysis/DifferentiableUnivariateVector100644 1750 1750 2667 12126627707 32425 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.analysis; /** * Extension of {@link UnivariateVectorFunction} representing a differentiable univariate vectorial function. * * @version $Id: DifferentiableUnivariateVectorFunction.java 1383852 2012-09-12 08:54:40Z luc $ * @since 2.0 * @deprecated as of 3.1 replaced by {@link org.apache.commons.math3.analysis.differentiation.UnivariateDifferentiableVectorFunction} */ public interface DifferentiableUnivariateVectorFunction extends UnivariateVectorFunction { /** * Returns the derivative of the function * * @return the derivative function */ UnivariateVectorFunction derivative(); } ././@LongLink100644 0 0 156 12126630646 10263 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/analysis/DifferentiableMultivariateFunction.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/analysis/DifferentiableMultivariateFunc100644 1750 1750 4426 12126627707 32410 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.analysis; /** * Extension of {@link MultivariateFunction} representing a differentiable * multivariate real function. * @version $Id: DifferentiableMultivariateFunction.java 1415149 2012-11-29 13:12:55Z erans $ * @since 2.0 * @deprecated as of 3.1 replaced by {@link org.apache.commons.math3.analysis.differentiation.MultivariateDifferentiableFunction} */ @Deprecated public interface DifferentiableMultivariateFunction extends MultivariateFunction { /** * Returns the partial derivative of the function with respect to a point coordinate. *

                            * The partial derivative is defined with respect to point coordinate * xk. If the partial derivatives with respect to all coordinates are * needed, it may be more efficient to use the {@link #gradient()} method which will * compute them all at once. *

                            * @param k index of the coordinate with respect to which the partial * derivative is computed * @return the partial derivative function with respect to kth point coordinate */ MultivariateFunction partialDerivative(int k); /** * Returns the gradient function. *

                            If only one partial derivative with respect to a specific coordinate is * needed, it may be more efficient to use the {@link #partialDerivative(int)} method * which will compute only the specified component.

                            * @return the gradient function */ MultivariateVectorFunction gradient(); } ././@LongLink100644 0 0 154 12126630646 10261 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/analysis/DifferentiableUnivariateFunction.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/analysis/DifferentiableUnivariateFuncti100644 1750 1750 2614 12126627707 32403 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.analysis; /** * Extension of {@link UnivariateFunction} representing a differentiable univariate real function. * * @version $Id: DifferentiableUnivariateFunction.java 1383845 2012-09-12 08:34:10Z luc $ * @deprecated as of 3.1 replaced by {@link org.apache.commons.math3.analysis.differentiation.UnivariateDifferentiableFunction} */ @Deprecated public interface DifferentiableUnivariateFunction extends UnivariateFunction { /** * Returns the derivative of the function * * @return the derivative function */ UnivariateFunction derivative(); } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/analysis/TrivariateFunction.java100644 1750 1750 2613 12126627707 31036 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.analysis; /** * An interface representing a trivariate real function. * * @since 2.2 * @version $Id: TrivariateFunction.java 1364387 2012-07-22 18:14:11Z tn $ */ public interface TrivariateFunction { /** * Compute the value for the function. * * @param x x-coordinate for which the function value should be computed. * @param y y-coordinate for which the function value should be computed. * @param z z-coordinate for which the function value should be computed. * @return the value. */ double value(double x, double y, double z); } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/analysis/function/Acos.java100644 1750 1750 3702 12126627701 27722 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.analysis.function; import org.apache.commons.math3.analysis.FunctionUtils; import org.apache.commons.math3.analysis.UnivariateFunction; import org.apache.commons.math3.analysis.DifferentiableUnivariateFunction; import org.apache.commons.math3.analysis.differentiation.DerivativeStructure; import org.apache.commons.math3.analysis.differentiation.UnivariateDifferentiableFunction; import org.apache.commons.math3.util.FastMath; /** * Arc-cosine function. * * @since 3.0 * @version $Id: Acos.java 1383441 2012-09-11 14:56:39Z luc $ */ public class Acos implements UnivariateDifferentiableFunction, DifferentiableUnivariateFunction { /** {@inheritDoc} */ public double value(double x) { return FastMath.acos(x); } /** {@inheritDoc} * @deprecated as of 3.1, replaced by {@link #value(DerivativeStructure)} */ @Deprecated public UnivariateFunction derivative() { return FunctionUtils.toDifferentiableUnivariateFunction(this).derivative(); } /** {@inheritDoc} * @since 3.1 */ public DerivativeStructure value(final DerivativeStructure t) { return t.acos(); } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/analysis/function/Ulp.java100644 1750 1750 2312 12126627701 27571 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.analysis.function; import org.apache.commons.math3.analysis.UnivariateFunction; import org.apache.commons.math3.util.FastMath; /** * {@code ulp} function. * * @since 3.0 * @version $Id: Ulp.java 1364377 2012-07-22 17:39:16Z tn $ */ public class Ulp implements UnivariateFunction { /** {@inheritDoc} */ public double value(double x) { return FastMath.ulp(x); } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/analysis/function/package-info.java100644 1750 1750 2126 12126627701 31360 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ /** * *

                            * The {@code function} package contains function objects that wrap the * methods contained in {@link java.lang.Math}, as well as common * mathematical functions such as the gaussian and sinc functions. *

                            * */ package org.apache.commons.math3.analysis.function; commons-math3-3.2-src/src/main/java/org/apache/commons/math3/analysis/function/Cos.java100644 1750 1750 3672 12126627701 27567 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.analysis.function; import org.apache.commons.math3.analysis.FunctionUtils; import org.apache.commons.math3.analysis.UnivariateFunction; import org.apache.commons.math3.analysis.DifferentiableUnivariateFunction; import org.apache.commons.math3.analysis.differentiation.DerivativeStructure; import org.apache.commons.math3.analysis.differentiation.UnivariateDifferentiableFunction; import org.apache.commons.math3.util.FastMath; /** * Cosine function. * * @since 3.0 * @version $Id: Cos.java 1383441 2012-09-11 14:56:39Z luc $ */ public class Cos implements UnivariateDifferentiableFunction, DifferentiableUnivariateFunction { /** {@inheritDoc} */ public double value(double x) { return FastMath.cos(x); } /** {@inheritDoc} * @deprecated as of 3.1, replaced by {@link #value(DerivativeStructure)} */ @Deprecated public UnivariateFunction derivative() { return FunctionUtils.toDifferentiableUnivariateFunction(this).derivative(); } /** {@inheritDoc} * @since 3.1 */ public DerivativeStructure value(final DerivativeStructure t) { return t.cos(); } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/analysis/function/Sinc.java100644 1750 1750 16767 12126627701 27770 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.analysis.function; import org.apache.commons.math3.analysis.DifferentiableUnivariateFunction; import org.apache.commons.math3.analysis.FunctionUtils; import org.apache.commons.math3.analysis.UnivariateFunction; import org.apache.commons.math3.analysis.differentiation.DerivativeStructure; import org.apache.commons.math3.analysis.differentiation.UnivariateDifferentiableFunction; import org.apache.commons.math3.exception.DimensionMismatchException; import org.apache.commons.math3.util.FastMath; /** *
                            Sinc function, * defined by *
                            
                             *   sinc(x) = 1            if x = 0,
                             *             sin(x) / x   otherwise.
                             * 
                            * * @since 3.0 * @version $Id: Sinc.java 1455194 2013-03-11 15:45:54Z luc $ */ public class Sinc implements UnivariateDifferentiableFunction, DifferentiableUnivariateFunction { /** * Value below which the computations are done using Taylor series. *

                            * The Taylor series for sinc even order derivatives are: *

                                 * d^(2n)sinc/dx^(2n)     = Sum_(k>=0) (-1)^(n+k) / ((2k)!(2n+2k+1)) x^(2k)
                                 *                        = (-1)^n     [ 1/(2n+1) - x^2/(4n+6) + x^4/(48n+120) - x^6/(1440n+5040) + O(x^8) ]
                                 * 
                            *

                            *

                            * The Taylor series for sinc odd order derivatives are: *

                                 * d^(2n+1)sinc/dx^(2n+1) = Sum_(k>=0) (-1)^(n+k+1) / ((2k+1)!(2n+2k+3)) x^(2k+1)
                                 *                        = (-1)^(n+1) [ x/(2n+3) - x^3/(12n+30) + x^5/(240n+840) - x^7/(10080n+45360) + O(x^9) ]
                                 * 
                            *

                            *

                            * So the ratio of the fourth term with respect to the first term * is always smaller than x^6/720, for all derivative orders. * This implies that neglecting this term and using only the first three terms induces * a relative error bounded by x^6/720. The SHORTCUT value is chosen such that this * relative error is below double precision accuracy when |x| <= SHORTCUT. *

                            */ private static final double SHORTCUT = 6.0e-3; /** For normalized sinc function. */ private final boolean normalized; /** * The sinc function, {@code sin(x) / x}. */ public Sinc() { this(false); } /** * Instantiates the sinc function. * * @param normalized If {@code true}, the function is * sin(πx) / πx, otherwise {@code sin(x) / x}. */ public Sinc(boolean normalized) { this.normalized = normalized; } /** {@inheritDoc} */ public double value(final double x) { final double scaledX = normalized ? FastMath.PI * x : x; if (FastMath.abs(scaledX) <= SHORTCUT) { // use Taylor series final double scaledX2 = scaledX * scaledX; return ((scaledX2 - 20) * scaledX2 + 120) / 120; } else { // use definition expression return FastMath.sin(scaledX) / scaledX; } } /** {@inheritDoc} * @deprecated as of 3.1, replaced by {@link #value(DerivativeStructure)} */ @Deprecated public UnivariateFunction derivative() { return FunctionUtils.toDifferentiableUnivariateFunction(this).derivative(); } /** {@inheritDoc} * @since 3.1 */ public DerivativeStructure value(final DerivativeStructure t) throws DimensionMismatchException { final double scaledX = (normalized ? FastMath.PI : 1) * t.getValue(); final double scaledX2 = scaledX * scaledX; double[] f = new double[t.getOrder() + 1]; if (FastMath.abs(scaledX) <= SHORTCUT) { for (int i = 0; i < f.length; ++i) { final int k = i / 2; if ((i & 0x1) == 0) { // even derivation order f[i] = (((k & 0x1) == 0) ? 1 : -1) * (1.0 / (i + 1) - scaledX2 * (1.0 / (2 * i + 6) - scaledX2 / (24 * i + 120))); } else { // odd derivation order f[i] = (((k & 0x1) == 0) ? -scaledX : scaledX) * (1.0 / (i + 2) - scaledX2 * (1.0 / (6 * i + 24) - scaledX2 / (120 * i + 720))); } } } else { final double inv = 1 / scaledX; final double cos = FastMath.cos(scaledX); final double sin = FastMath.sin(scaledX); f[0] = inv * sin; // the nth order derivative of sinc has the form: // dn(sinc(x)/dxn = [S_n(x) sin(x) + C_n(x) cos(x)] / x^(n+1) // where S_n(x) is an even polynomial with degree n-1 or n (depending on parity) // and C_n(x) is an odd polynomial with degree n-1 or n (depending on parity) // S_0(x) = 1, S_1(x) = -1, S_2(x) = -x^2 + 2, S_3(x) = 3x^2 - 6... // C_0(x) = 0, C_1(x) = x, C_2(x) = -2x, C_3(x) = -x^3 + 6x... // the general recurrence relations for S_n and C_n are: // S_n(x) = x S_(n-1)'(x) - n S_(n-1)(x) - x C_(n-1)(x) // C_n(x) = x C_(n-1)'(x) - n C_(n-1)(x) + x S_(n-1)(x) // as per polynomials parity, we can store both S_n and C_n in the same array final double[] sc = new double[f.length]; sc[0] = 1; double coeff = inv; for (int n = 1; n < f.length; ++n) { double s = 0; double c = 0; // update and evaluate polynomials S_n(x) and C_n(x) final int kStart; if ((n & 0x1) == 0) { // even derivation order, S_n is degree n and C_n is degree n-1 sc[n] = 0; kStart = n; } else { // odd derivation order, S_n is degree n-1 and C_n is degree n sc[n] = sc[n - 1]; c = sc[n]; kStart = n - 1; } // in this loop, k is always even for (int k = kStart; k > 1; k -= 2) { // sine part sc[k] = (k - n) * sc[k] - sc[k - 1]; s = s * scaledX2 + sc[k]; // cosine part sc[k - 1] = (k - 1 - n) * sc[k - 1] + sc[k -2]; c = c * scaledX2 + sc[k - 1]; } sc[0] *= -n; s = s * scaledX2 + sc[0]; coeff *= inv; f[n] = coeff * (s * sin + c * scaledX * cos); } } if (normalized) { double scale = FastMath.PI; for (int i = 1; i < f.length; ++i) { f[i] *= scale; scale *= FastMath.PI; } } return t.compose(f); } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/analysis/function/Floor.java100644 1750 1750 2322 12126627701 30113 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.analysis.function; import org.apache.commons.math3.analysis.UnivariateFunction; import org.apache.commons.math3.util.FastMath; /** * {@code floor} function. * * @since 3.0 * @version $Id: Floor.java 1364377 2012-07-22 17:39:16Z tn $ */ public class Floor implements UnivariateFunction { /** {@inheritDoc} */ public double value(double x) { return FastMath.floor(x); } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/analysis/function/Identity.java100644 1750 1750 3350 12126627701 30625 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.analysis.function; import org.apache.commons.math3.analysis.DifferentiableUnivariateFunction; import org.apache.commons.math3.analysis.differentiation.DerivativeStructure; import org.apache.commons.math3.analysis.differentiation.UnivariateDifferentiableFunction; /** * Identity function. * * @since 3.0 * @version $Id: Identity.java 1424087 2012-12-19 20:32:50Z luc $ */ public class Identity implements UnivariateDifferentiableFunction, DifferentiableUnivariateFunction { /** {@inheritDoc} */ public double value(double x) { return x; } /** {@inheritDoc} * @deprecated as of 3.1, replaced by {@link #value(DerivativeStructure)} */ @Deprecated public DifferentiableUnivariateFunction derivative() { return new Constant(1); } /** {@inheritDoc} * @since 3.1 */ public DerivativeStructure value(final DerivativeStructure t) { return t; } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/analysis/function/Min.java100644 1750 1750 2321 12126627701 27554 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.analysis.function; import org.apache.commons.math3.analysis.BivariateFunction; import org.apache.commons.math3.util.FastMath; /** * Minimum function. * * @since 3.0 * @version $Id: Min.java 1364377 2012-07-22 17:39:16Z tn $ */ public class Min implements BivariateFunction { /** {@inheritDoc} */ public double value(double x, double y) { return FastMath.min(x, y); } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/analysis/function/Atanh.java100644 1750 1750 3722 12126627701 30072 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.analysis.function; import org.apache.commons.math3.analysis.FunctionUtils; import org.apache.commons.math3.analysis.UnivariateFunction; import org.apache.commons.math3.analysis.DifferentiableUnivariateFunction; import org.apache.commons.math3.analysis.differentiation.DerivativeStructure; import org.apache.commons.math3.analysis.differentiation.UnivariateDifferentiableFunction; import org.apache.commons.math3.util.FastMath; /** * Hyperbolic arc-tangent function. * * @since 3.0 * @version $Id: Atanh.java 1383441 2012-09-11 14:56:39Z luc $ */ public class Atanh implements UnivariateDifferentiableFunction, DifferentiableUnivariateFunction { /** {@inheritDoc} */ public double value(double x) { return FastMath.atanh(x); } /** {@inheritDoc} * @deprecated as of 3.1, replaced by {@link #value(DerivativeStructure)} */ @Deprecated public UnivariateFunction derivative() { return FunctionUtils.toDifferentiableUnivariateFunction(this).derivative(); } /** {@inheritDoc} * @since 3.1 */ public DerivativeStructure value(final DerivativeStructure t) { return t.atanh(); } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/analysis/function/Sin.java100644 1750 1750 3427 12126627701 27572 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.analysis.function; import org.apache.commons.math3.analysis.DifferentiableUnivariateFunction; import org.apache.commons.math3.analysis.differentiation.DerivativeStructure; import org.apache.commons.math3.analysis.differentiation.UnivariateDifferentiableFunction; import org.apache.commons.math3.util.FastMath; /** * Sine function. * * @since 3.0 * @version $Id: Sin.java 1424087 2012-12-19 20:32:50Z luc $ */ public class Sin implements UnivariateDifferentiableFunction, DifferentiableUnivariateFunction { /** {@inheritDoc} */ public double value(double x) { return FastMath.sin(x); } /** {@inheritDoc} * @deprecated as of 3.1, replaced by {@link #value(DerivativeStructure)} */ @Deprecated public DifferentiableUnivariateFunction derivative() { return new Cos(); } /** {@inheritDoc} * @since 3.1 */ public DerivativeStructure value(final DerivativeStructure t) { return t.sin(); } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/analysis/function/Subtract.java100644 1750 1750 2271 12126627701 30624 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.analysis.function; import org.apache.commons.math3.analysis.BivariateFunction; /** * Subtract the second operand from the first. * * @since 3.0 * @version $Id: Subtract.java 1364377 2012-07-22 17:39:16Z tn $ */ public class Subtract implements BivariateFunction { /** {@inheritDoc} */ public double value(double x, double y) { return x - y; } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/analysis/function/Ceil.java100644 1750 1750 2316 12126627701 27711 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.analysis.function; import org.apache.commons.math3.analysis.UnivariateFunction; import org.apache.commons.math3.util.FastMath; /** * {@code ceil} function. * * @since 3.0 * @version $Id: Ceil.java 1364377 2012-07-22 17:39:16Z tn $ */ public class Ceil implements UnivariateFunction { /** {@inheritDoc} */ public double value(double x) { return FastMath.ceil(x); } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/analysis/function/Exp.java100644 1750 1750 3677 12126627701 27604 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.analysis.function; import org.apache.commons.math3.analysis.FunctionUtils; import org.apache.commons.math3.analysis.UnivariateFunction; import org.apache.commons.math3.analysis.DifferentiableUnivariateFunction; import org.apache.commons.math3.analysis.differentiation.DerivativeStructure; import org.apache.commons.math3.analysis.differentiation.UnivariateDifferentiableFunction; import org.apache.commons.math3.util.FastMath; /** * Exponential function. * * @since 3.0 * @version $Id: Exp.java 1383441 2012-09-11 14:56:39Z luc $ */ public class Exp implements UnivariateDifferentiableFunction, DifferentiableUnivariateFunction { /** {@inheritDoc} */ public double value(double x) { return FastMath.exp(x); } /** {@inheritDoc} * @deprecated as of 3.1, replaced by {@link #value(DerivativeStructure)} */ @Deprecated public UnivariateFunction derivative() { return FunctionUtils.toDifferentiableUnivariateFunction(this).derivative(); } /** {@inheritDoc} * @since 3.1 */ public DerivativeStructure value(final DerivativeStructure t) { return t.exp(); } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/analysis/function/Expm1.java100644 1750 1750 3730 12126627701 30030 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.analysis.function; import org.apache.commons.math3.analysis.FunctionUtils; import org.apache.commons.math3.analysis.UnivariateFunction; import org.apache.commons.math3.analysis.DifferentiableUnivariateFunction; import org.apache.commons.math3.analysis.differentiation.DerivativeStructure; import org.apache.commons.math3.analysis.differentiation.UnivariateDifferentiableFunction; import org.apache.commons.math3.util.FastMath; /** * ex-1 function. * * @since 3.0 * @version $Id: Expm1.java 1383441 2012-09-11 14:56:39Z luc $ */ public class Expm1 implements UnivariateDifferentiableFunction, DifferentiableUnivariateFunction { /** {@inheritDoc} */ public double value(double x) { return FastMath.expm1(x); } /** {@inheritDoc} * @deprecated as of 3.1, replaced by {@link #value(DerivativeStructure)} */ @Deprecated public UnivariateFunction derivative() { return FunctionUtils.toDifferentiableUnivariateFunction(this).derivative(); } /** {@inheritDoc} * @since 3.1 */ public DerivativeStructure value(final DerivativeStructure t) { return t.expm1(); } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/analysis/function/Tanh.java100644 1750 1750 3712 12126627701 27730 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.analysis.function; import org.apache.commons.math3.analysis.FunctionUtils; import org.apache.commons.math3.analysis.UnivariateFunction; import org.apache.commons.math3.analysis.DifferentiableUnivariateFunction; import org.apache.commons.math3.analysis.differentiation.DerivativeStructure; import org.apache.commons.math3.analysis.differentiation.UnivariateDifferentiableFunction; import org.apache.commons.math3.util.FastMath; /** * Hyperbolic tangent function. * * @since 3.0 * @version $Id: Tanh.java 1383441 2012-09-11 14:56:39Z luc $ */ public class Tanh implements UnivariateDifferentiableFunction, DifferentiableUnivariateFunction { /** {@inheritDoc} */ public double value(double x) { return FastMath.tanh(x); } /** {@inheritDoc} * @deprecated as of 3.1, replaced by {@link #value(DerivativeStructure)} */ @Deprecated public UnivariateFunction derivative() { return FunctionUtils.toDifferentiableUnivariateFunction(this).derivative(); } /** {@inheritDoc} * @since 3.1 */ public DerivativeStructure value(final DerivativeStructure t) { return t.tanh(); } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/analysis/function/Tan.java100644 1750 1750 3673 12126627701 27566 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.analysis.function; import org.apache.commons.math3.analysis.FunctionUtils; import org.apache.commons.math3.analysis.UnivariateFunction; import org.apache.commons.math3.analysis.DifferentiableUnivariateFunction; import org.apache.commons.math3.analysis.differentiation.DerivativeStructure; import org.apache.commons.math3.analysis.differentiation.UnivariateDifferentiableFunction; import org.apache.commons.math3.util.FastMath; /** * Tangent function. * * @since 3.0 * @version $Id: Tan.java 1383441 2012-09-11 14:56:39Z luc $ */ public class Tan implements UnivariateDifferentiableFunction, DifferentiableUnivariateFunction { /** {@inheritDoc} */ public double value(double x) { return FastMath.tan(x); } /** {@inheritDoc} * @deprecated as of 3.1, replaced by {@link #value(DerivativeStructure)} */ @Deprecated public UnivariateFunction derivative() { return FunctionUtils.toDifferentiableUnivariateFunction(this).derivative(); } /** {@inheritDoc} * @since 3.1 */ public DerivativeStructure value(final DerivativeStructure t) { return t.tan(); } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/analysis/function/Inverse.java100644 1750 1750 3621 12126627701 30450 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.analysis.function; import org.apache.commons.math3.analysis.FunctionUtils; import org.apache.commons.math3.analysis.UnivariateFunction; import org.apache.commons.math3.analysis.DifferentiableUnivariateFunction; import org.apache.commons.math3.analysis.differentiation.DerivativeStructure; import org.apache.commons.math3.analysis.differentiation.UnivariateDifferentiableFunction; /** * Inverse function. * * @since 3.0 * @version $Id: Inverse.java 1383441 2012-09-11 14:56:39Z luc $ */ public class Inverse implements UnivariateDifferentiableFunction, DifferentiableUnivariateFunction { /** {@inheritDoc} */ public double value(double x) { return 1 / x; } /** {@inheritDoc} * @deprecated as of 3.1, replaced by {@link #value(DerivativeStructure)} */ @Deprecated public UnivariateFunction derivative() { return FunctionUtils.toDifferentiableUnivariateFunction(this).derivative(); } /** {@inheritDoc} * @since 3.1 */ public DerivativeStructure value(final DerivativeStructure t) { return t.reciprocal(); } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/analysis/function/Add.java100644 1750 1750 2231 12126627701 27521 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.analysis.function; import org.apache.commons.math3.analysis.BivariateFunction; /** * Add the two operands. * * @since 3.0 * @version $Id: Add.java 1364377 2012-07-22 17:39:16Z tn $ */ public class Add implements BivariateFunction { /** {@inheritDoc} */ public double value(double x, double y) { return x + y; } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/analysis/function/Log10.java100644 1750 1750 3716 12126627701 27724 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.analysis.function; import org.apache.commons.math3.analysis.FunctionUtils; import org.apache.commons.math3.analysis.UnivariateFunction; import org.apache.commons.math3.analysis.DifferentiableUnivariateFunction; import org.apache.commons.math3.analysis.differentiation.DerivativeStructure; import org.apache.commons.math3.analysis.differentiation.UnivariateDifferentiableFunction; import org.apache.commons.math3.util.FastMath; /** * Base 10 logarithm function. * * @since 3.0 * @version $Id: Log10.java 1383441 2012-09-11 14:56:39Z luc $ */ public class Log10 implements UnivariateDifferentiableFunction, DifferentiableUnivariateFunction { /** {@inheritDoc} */ public double value(double x) { return FastMath.log10(x); } /** {@inheritDoc} * @deprecated as of 3.1, replaced by {@link #value(DerivativeStructure)} */ @Deprecated public UnivariateFunction derivative() { return FunctionUtils.toDifferentiableUnivariateFunction(this).derivative(); } /** {@inheritDoc} * @since 3.1 */ public DerivativeStructure value(final DerivativeStructure t) { return t.log10(); } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/analysis/function/Atan.java100644 1750 1750 3703 12126627701 27721 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.analysis.function; import org.apache.commons.math3.analysis.FunctionUtils; import org.apache.commons.math3.analysis.UnivariateFunction; import org.apache.commons.math3.analysis.DifferentiableUnivariateFunction; import org.apache.commons.math3.analysis.differentiation.DerivativeStructure; import org.apache.commons.math3.analysis.differentiation.UnivariateDifferentiableFunction; import org.apache.commons.math3.util.FastMath; /** * Arc-tangent function. * * @since 3.0 * @version $Id: Atan.java 1383441 2012-09-11 14:56:39Z luc $ */ public class Atan implements UnivariateDifferentiableFunction, DifferentiableUnivariateFunction { /** {@inheritDoc} */ public double value(double x) { return FastMath.atan(x); } /** {@inheritDoc} * @deprecated as of 3.1, replaced by {@link #value(DerivativeStructure)} */ @Deprecated public UnivariateFunction derivative() { return FunctionUtils.toDifferentiableUnivariateFunction(this).derivative(); } /** {@inheritDoc} * @since 3.1 */ public DerivativeStructure value(final DerivativeStructure t) { return t.atan(); } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/analysis/function/Rint.java100644 1750 1750 2316 12126627701 27751 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.analysis.function; import org.apache.commons.math3.analysis.UnivariateFunction; import org.apache.commons.math3.util.FastMath; /** * {@code rint} function. * * @since 3.0 * @version $Id: Rint.java 1364377 2012-07-22 17:39:16Z tn $ */ public class Rint implements UnivariateFunction { /** {@inheritDoc} */ public double value(double x) { return FastMath.rint(x); } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/analysis/function/Asin.java100644 1750 1750 3700 12126627701 27725 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.analysis.function; import org.apache.commons.math3.analysis.FunctionUtils; import org.apache.commons.math3.analysis.UnivariateFunction; import org.apache.commons.math3.analysis.DifferentiableUnivariateFunction; import org.apache.commons.math3.analysis.differentiation.DerivativeStructure; import org.apache.commons.math3.analysis.differentiation.UnivariateDifferentiableFunction; import org.apache.commons.math3.util.FastMath; /** * Arc-sine function. * * @since 3.0 * @version $Id: Asin.java 1383441 2012-09-11 14:56:39Z luc $ */ public class Asin implements UnivariateDifferentiableFunction, DifferentiableUnivariateFunction { /** {@inheritDoc} */ public double value(double x) { return FastMath.asin(x); } /** {@inheritDoc} * @deprecated as of 3.1, replaced by {@link #value(DerivativeStructure)} */ @Deprecated public UnivariateFunction derivative() { return FunctionUtils.toDifferentiableUnivariateFunction(this).derivative(); } /** {@inheritDoc} * @since 3.1 */ public DerivativeStructure value(final DerivativeStructure t) { return t.asin(); } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/analysis/function/Gaussian.java100644 1750 1750 22660 12126627701 30633 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.analysis.function; import java.util.Arrays; import org.apache.commons.math3.analysis.FunctionUtils; import org.apache.commons.math3.analysis.UnivariateFunction; import org.apache.commons.math3.analysis.DifferentiableUnivariateFunction; import org.apache.commons.math3.analysis.ParametricUnivariateFunction; import org.apache.commons.math3.analysis.differentiation.DerivativeStructure; import org.apache.commons.math3.analysis.differentiation.UnivariateDifferentiableFunction; import org.apache.commons.math3.exception.NotStrictlyPositiveException; import org.apache.commons.math3.exception.NullArgumentException; import org.apache.commons.math3.exception.DimensionMismatchException; import org.apache.commons.math3.util.FastMath; import org.apache.commons.math3.util.Precision; /** * * Gaussian function. * * @since 3.0 * @version $Id: Gaussian.java 1455194 2013-03-11 15:45:54Z luc $ */ public class Gaussian implements UnivariateDifferentiableFunction, DifferentiableUnivariateFunction { /** Mean. */ private final double mean; /** Inverse of the standard deviation. */ private final double is; /** Inverse of twice the square of the standard deviation. */ private final double i2s2; /** Normalization factor. */ private final double norm; /** * Gaussian with given normalization factor, mean and standard deviation. * * @param norm Normalization factor. * @param mean Mean. * @param sigma Standard deviation. * @throws NotStrictlyPositiveException if {@code sigma <= 0}. */ public Gaussian(double norm, double mean, double sigma) throws NotStrictlyPositiveException { if (sigma <= 0) { throw new NotStrictlyPositiveException(sigma); } this.norm = norm; this.mean = mean; this.is = 1 / sigma; this.i2s2 = 0.5 * is * is; } /** * Normalized gaussian with given mean and standard deviation. * * @param mean Mean. * @param sigma Standard deviation. * @throws NotStrictlyPositiveException if {@code sigma <= 0}. */ public Gaussian(double mean, double sigma) throws NotStrictlyPositiveException { this(1 / (sigma * FastMath.sqrt(2 * Math.PI)), mean, sigma); } /** * Normalized gaussian with zero mean and unit standard deviation. */ public Gaussian() { this(0, 1); } /** {@inheritDoc} */ public double value(double x) { return value(x - mean, norm, i2s2); } /** {@inheritDoc} * @deprecated as of 3.1, replaced by {@link #value(DerivativeStructure)} */ @Deprecated public UnivariateFunction derivative() { return FunctionUtils.toDifferentiableUnivariateFunction(this).derivative(); } /** * Parametric function where the input array contains the parameters of * the Gaussian, ordered as follows: *
                              *
                            • Norm
                            • *
                            • Mean
                            • *
                            • Standard deviation
                            • *
                            */ public static class Parametric implements ParametricUnivariateFunction { /** * Computes the value of the Gaussian at {@code x}. * * @param x Value for which the function must be computed. * @param param Values of norm, mean and standard deviation. * @return the value of the function. * @throws NullArgumentException if {@code param} is {@code null}. * @throws DimensionMismatchException if the size of {@code param} is * not 3. * @throws NotStrictlyPositiveException if {@code param[2]} is negative. */ public double value(double x, double ... param) throws NullArgumentException, DimensionMismatchException, NotStrictlyPositiveException { validateParameters(param); final double diff = x - param[1]; final double i2s2 = 1 / (2 * param[2] * param[2]); return Gaussian.value(diff, param[0], i2s2); } /** * Computes the value of the gradient at {@code x}. * The components of the gradient vector are the partial * derivatives of the function with respect to each of the * parameters (norm, mean and standard deviation). * * @param x Value at which the gradient must be computed. * @param param Values of norm, mean and standard deviation. * @return the gradient vector at {@code x}. * @throws NullArgumentException if {@code param} is {@code null}. * @throws DimensionMismatchException if the size of {@code param} is * not 3. * @throws NotStrictlyPositiveException if {@code param[2]} is negative. */ public double[] gradient(double x, double ... param) throws NullArgumentException, DimensionMismatchException, NotStrictlyPositiveException { validateParameters(param); final double norm = param[0]; final double diff = x - param[1]; final double sigma = param[2]; final double i2s2 = 1 / (2 * sigma * sigma); final double n = Gaussian.value(diff, 1, i2s2); final double m = norm * n * 2 * i2s2 * diff; final double s = m * diff / sigma; return new double[] { n, m, s }; } /** * Validates parameters to ensure they are appropriate for the evaluation of * the {@link #value(double,double[])} and {@link #gradient(double,double[])} * methods. * * @param param Values of norm, mean and standard deviation. * @throws NullArgumentException if {@code param} is {@code null}. * @throws DimensionMismatchException if the size of {@code param} is * not 3. * @throws NotStrictlyPositiveException if {@code param[2]} is negative. */ private void validateParameters(double[] param) throws NullArgumentException, DimensionMismatchException, NotStrictlyPositiveException { if (param == null) { throw new NullArgumentException(); } if (param.length != 3) { throw new DimensionMismatchException(param.length, 3); } if (param[2] <= 0) { throw new NotStrictlyPositiveException(param[2]); } } } /** * @param xMinusMean {@code x - mean}. * @param norm Normalization factor. * @param i2s2 Inverse of twice the square of the standard deviation. * @return the value of the Gaussian at {@code x}. */ private static double value(double xMinusMean, double norm, double i2s2) { return norm * FastMath.exp(-xMinusMean * xMinusMean * i2s2); } /** {@inheritDoc} * @since 3.1 */ public DerivativeStructure value(final DerivativeStructure t) throws DimensionMismatchException { final double u = is * (t.getValue() - mean); double[] f = new double[t.getOrder() + 1]; // the nth order derivative of the Gaussian has the form: // dn(g(x)/dxn = (norm / s^n) P_n(u) exp(-u^2/2) with u=(x-m)/s // where P_n(u) is a degree n polynomial with same parity as n // P_0(u) = 1, P_1(u) = -u, P_2(u) = u^2 - 1, P_3(u) = -u^3 + 3 u... // the general recurrence relation for P_n is: // P_n(u) = P_(n-1)'(u) - u P_(n-1)(u) // as per polynomial parity, we can store coefficients of both P_(n-1) and P_n in the same array final double[] p = new double[f.length]; p[0] = 1; final double u2 = u * u; double coeff = norm * FastMath.exp(-0.5 * u2); if (coeff <= Precision.SAFE_MIN) { Arrays.fill(f, 0.0); } else { f[0] = coeff; for (int n = 1; n < f.length; ++n) { // update and evaluate polynomial P_n(x) double v = 0; p[n] = -p[n - 1]; for (int k = n; k >= 0; k -= 2) { v = v * u2 + p[k]; if (k > 2) { p[k - 2] = (k - 1) * p[k - 1] - p[k - 3]; } else if (k == 2) { p[0] = p[1]; } } if ((n & 0x1) == 1) { v *= u; } coeff *= is; f[n] = coeff * v; } } return t.compose(f); } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/analysis/function/Cosh.java100644 1750 1750 3451 12126627701 27732 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.analysis.function; import org.apache.commons.math3.analysis.DifferentiableUnivariateFunction; import org.apache.commons.math3.analysis.differentiation.DerivativeStructure; import org.apache.commons.math3.analysis.differentiation.UnivariateDifferentiableFunction; import org.apache.commons.math3.util.FastMath; /** * Hyperbolic cosine function. * * @since 3.0 * @version $Id: Cosh.java 1424087 2012-12-19 20:32:50Z luc $ */ public class Cosh implements UnivariateDifferentiableFunction, DifferentiableUnivariateFunction { /** {@inheritDoc} */ public double value(double x) { return FastMath.cosh(x); } /** {@inheritDoc} * @deprecated as of 3.1, replaced by {@link #value(DerivativeStructure)} */ @Deprecated public DifferentiableUnivariateFunction derivative() { return new Sinh(); } /** {@inheritDoc} * @since 3.1 */ public DerivativeStructure value(final DerivativeStructure t) { return t.cosh(); } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/analysis/function/Cbrt.java100644 1750 1750 3701 12126627701 27726 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.analysis.function; import org.apache.commons.math3.analysis.FunctionUtils; import org.apache.commons.math3.analysis.UnivariateFunction; import org.apache.commons.math3.analysis.DifferentiableUnivariateFunction; import org.apache.commons.math3.analysis.differentiation.DerivativeStructure; import org.apache.commons.math3.analysis.differentiation.UnivariateDifferentiableFunction; import org.apache.commons.math3.util.FastMath; /** * Cube root function. * * @since 3.0 * @version $Id: Cbrt.java 1383441 2012-09-11 14:56:39Z luc $ */ public class Cbrt implements UnivariateDifferentiableFunction, DifferentiableUnivariateFunction { /** {@inheritDoc} */ public double value(double x) { return FastMath.cbrt(x); } /** {@inheritDoc} * @deprecated as of 3.1, replaced by {@link #value(DerivativeStructure)} */ @Deprecated public UnivariateFunction derivative() { return FunctionUtils.toDifferentiableUnivariateFunction(this).derivative(); } /** {@inheritDoc} * @since 3.1 */ public DerivativeStructure value(final DerivativeStructure t) { return t.cbrt(); } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/analysis/function/Acosh.java100644 1750 1750 3721 12126627701 30073 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.analysis.function; import org.apache.commons.math3.analysis.FunctionUtils; import org.apache.commons.math3.analysis.UnivariateFunction; import org.apache.commons.math3.analysis.DifferentiableUnivariateFunction; import org.apache.commons.math3.analysis.differentiation.DerivativeStructure; import org.apache.commons.math3.analysis.differentiation.UnivariateDifferentiableFunction; import org.apache.commons.math3.util.FastMath; /** * Hyperbolic arc-cosine function. * * @since 3.0 * @version $Id: Acosh.java 1383441 2012-09-11 14:56:39Z luc $ */ public class Acosh implements UnivariateDifferentiableFunction, DifferentiableUnivariateFunction { /** {@inheritDoc} */ public double value(double x) { return FastMath.acosh(x); } /** {@inheritDoc} * @deprecated as of 3.1, replaced by {@link #value(DerivativeStructure)} */ @Deprecated public UnivariateFunction derivative() { return FunctionUtils.toDifferentiableUnivariateFunction(this).derivative(); } /** {@inheritDoc} * @since 3.1 */ public DerivativeStructure value(final DerivativeStructure t) { return t.acosh(); } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/analysis/function/Power.java100644 1750 1750 4117 12126627701 30132 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.analysis.function; import org.apache.commons.math3.analysis.FunctionUtils; import org.apache.commons.math3.analysis.UnivariateFunction; import org.apache.commons.math3.analysis.DifferentiableUnivariateFunction; import org.apache.commons.math3.analysis.differentiation.DerivativeStructure; import org.apache.commons.math3.analysis.differentiation.UnivariateDifferentiableFunction; import org.apache.commons.math3.util.FastMath; /** * Power function. * * @since 3.0 * @version $Id: Power.java 1383441 2012-09-11 14:56:39Z luc $ */ public class Power implements UnivariateDifferentiableFunction, DifferentiableUnivariateFunction { /** Power. */ private final double p; /** * @param p Power. */ public Power(double p) { this.p = p; } /** {@inheritDoc} */ public double value(double x) { return FastMath.pow(x, p); } /** {@inheritDoc} * @deprecated as of 3.1, replaced by {@link #value(DerivativeStructure)} */ @Deprecated public UnivariateFunction derivative() { return FunctionUtils.toDifferentiableUnivariateFunction(this).derivative(); } /** {@inheritDoc} * @since 3.1 */ public DerivativeStructure value(final DerivativeStructure t) { return t.pow(p); } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/analysis/function/Abs.java100644 1750 1750 2315 12126627701 27541 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.analysis.function; import org.apache.commons.math3.analysis.UnivariateFunction; import org.apache.commons.math3.util.FastMath; /** * Absolute value function. * * @since 3.0 * @version $Id: Abs.java 1364377 2012-07-22 17:39:16Z tn $ */ public class Abs implements UnivariateFunction { /** {@inheritDoc} */ public double value(double x) { return FastMath.abs(x); } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/analysis/function/Minus.java100644 1750 1750 3352 12126627701 30131 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.analysis.function; import org.apache.commons.math3.analysis.DifferentiableUnivariateFunction; import org.apache.commons.math3.analysis.differentiation.DerivativeStructure; import org.apache.commons.math3.analysis.differentiation.UnivariateDifferentiableFunction; /** * Minus function. * * @since 3.0 * @version $Id: Minus.java 1424087 2012-12-19 20:32:50Z luc $ */ public class Minus implements UnivariateDifferentiableFunction, DifferentiableUnivariateFunction { /** {@inheritDoc} */ public double value(double x) { return -x; } /** {@inheritDoc} * @deprecated as of 3.1, replaced by {@link #value(DerivativeStructure)} */ @Deprecated public DifferentiableUnivariateFunction derivative() { return new Constant(-1); } /** {@inheritDoc} * @since 3.1 */ public DerivativeStructure value(final DerivativeStructure t) { return t.negate(); } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/analysis/function/Signum.java100644 1750 1750 2326 12126627701 30300 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.analysis.function; import org.apache.commons.math3.analysis.UnivariateFunction; import org.apache.commons.math3.util.FastMath; /** * {@code signum} function. * * @since 3.0 * @version $Id: Signum.java 1364377 2012-07-22 17:39:16Z tn $ */ public class Signum implements UnivariateFunction { /** {@inheritDoc} */ public double value(double x) { return FastMath.signum(x); } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/analysis/function/Max.java100644 1750 1750 2321 12126627701 27556 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.analysis.function; import org.apache.commons.math3.analysis.BivariateFunction; import org.apache.commons.math3.util.FastMath; /** * Maximum function. * * @since 3.0 * @version $Id: Max.java 1364377 2012-07-22 17:39:16Z tn $ */ public class Max implements BivariateFunction { /** {@inheritDoc} */ public double value(double x, double y) { return FastMath.max(x, y); } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/analysis/function/Divide.java100644 1750 1750 2261 12126627701 30240 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.analysis.function; import org.apache.commons.math3.analysis.BivariateFunction; /** * Divide the first operand by the second. * * @since 3.0 * @version $Id: Divide.java 1364377 2012-07-22 17:39:16Z tn $ */ public class Divide implements BivariateFunction { /** {@inheritDoc} */ public double value(double x, double y) { return x / y; } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/analysis/function/Sigmoid.java100644 1750 1750 17051 12126627701 30452 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.analysis.function; import java.util.Arrays; import org.apache.commons.math3.analysis.FunctionUtils; import org.apache.commons.math3.analysis.UnivariateFunction; import org.apache.commons.math3.analysis.DifferentiableUnivariateFunction; import org.apache.commons.math3.analysis.ParametricUnivariateFunction; import org.apache.commons.math3.analysis.differentiation.DerivativeStructure; import org.apache.commons.math3.analysis.differentiation.UnivariateDifferentiableFunction; import org.apache.commons.math3.exception.NullArgumentException; import org.apache.commons.math3.exception.DimensionMismatchException; import org.apache.commons.math3.util.FastMath; /** * * Sigmoid function. * It is the inverse of the {@link Logit logit} function. * A more flexible version, the generalised logistic, is implemented * by the {@link Logistic} class. * * @since 3.0 * @version $Id: Sigmoid.java 1455194 2013-03-11 15:45:54Z luc $ */ public class Sigmoid implements UnivariateDifferentiableFunction, DifferentiableUnivariateFunction { /** Lower asymptote. */ private final double lo; /** Higher asymptote. */ private final double hi; /** * Usual sigmoid function, where the lower asymptote is 0 and the higher * asymptote is 1. */ public Sigmoid() { this(0, 1); } /** * Sigmoid function. * * @param lo Lower asymptote. * @param hi Higher asymptote. */ public Sigmoid(double lo, double hi) { this.lo = lo; this.hi = hi; } /** {@inheritDoc} * @deprecated as of 3.1, replaced by {@link #value(DerivativeStructure)} */ @Deprecated public UnivariateFunction derivative() { return FunctionUtils.toDifferentiableUnivariateFunction(this).derivative(); } /** {@inheritDoc} */ public double value(double x) { return value(x, lo, hi); } /** * Parametric function where the input array contains the parameters of * the logit function, ordered as follows: *
                              *
                            • Lower asymptote
                            • *
                            • Higher asymptote
                            • *
                            */ public static class Parametric implements ParametricUnivariateFunction { /** * Computes the value of the sigmoid at {@code x}. * * @param x Value for which the function must be computed. * @param param Values of lower asymptote and higher asymptote. * @return the value of the function. * @throws NullArgumentException if {@code param} is {@code null}. * @throws DimensionMismatchException if the size of {@code param} is * not 2. */ public double value(double x, double ... param) throws NullArgumentException, DimensionMismatchException { validateParameters(param); return Sigmoid.value(x, param[0], param[1]); } /** * Computes the value of the gradient at {@code x}. * The components of the gradient vector are the partial * derivatives of the function with respect to each of the * parameters (lower asymptote and higher asymptote). * * @param x Value at which the gradient must be computed. * @param param Values for lower asymptote and higher asymptote. * @return the gradient vector at {@code x}. * @throws NullArgumentException if {@code param} is {@code null}. * @throws DimensionMismatchException if the size of {@code param} is * not 2. */ public double[] gradient(double x, double ... param) throws NullArgumentException, DimensionMismatchException { validateParameters(param); final double invExp1 = 1 / (1 + FastMath.exp(-x)); return new double[] { 1 - invExp1, invExp1 }; } /** * Validates parameters to ensure they are appropriate for the evaluation of * the {@link #value(double,double[])} and {@link #gradient(double,double[])} * methods. * * @param param Values for lower and higher asymptotes. * @throws NullArgumentException if {@code param} is {@code null}. * @throws DimensionMismatchException if the size of {@code param} is * not 2. */ private void validateParameters(double[] param) throws NullArgumentException, DimensionMismatchException { if (param == null) { throw new NullArgumentException(); } if (param.length != 2) { throw new DimensionMismatchException(param.length, 2); } } } /** * @param x Value at which to compute the sigmoid. * @param lo Lower asymptote. * @param hi Higher asymptote. * @return the value of the sigmoid function at {@code x}. */ private static double value(double x, double lo, double hi) { return lo + (hi - lo) / (1 + FastMath.exp(-x)); } /** {@inheritDoc} * @since 3.1 */ public DerivativeStructure value(final DerivativeStructure t) throws DimensionMismatchException { double[] f = new double[t.getOrder() + 1]; final double exp = FastMath.exp(-t.getValue()); if (Double.isInfinite(exp)) { // special handling near lower boundary, to avoid NaN f[0] = lo; Arrays.fill(f, 1, f.length, 0.0); } else { // the nth order derivative of sigmoid has the form: // dn(sigmoid(x)/dxn = P_n(exp(-x)) / (1+exp(-x))^(n+1) // where P_n(t) is a degree n polynomial with normalized higher term // P_0(t) = 1, P_1(t) = t, P_2(t) = t^2 - t, P_3(t) = t^3 - 4 t^2 + t... // the general recurrence relation for P_n is: // P_n(x) = n t P_(n-1)(t) - t (1 + t) P_(n-1)'(t) final double[] p = new double[f.length]; final double inv = 1 / (1 + exp); double coeff = hi - lo; for (int n = 0; n < f.length; ++n) { // update and evaluate polynomial P_n(t) double v = 0; p[n] = 1; for (int k = n; k >= 0; --k) { v = v * exp + p[k]; if (k > 1) { p[k - 1] = (n - k + 2) * p[k - 2] - (k - 1) * p[k - 1]; } else { p[0] = 0; } } coeff *= inv; f[n] = coeff * v; } // fix function value f[0] += lo; } return t.compose(f); } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/analysis/function/Log1p.java100644 1750 1750 3723 12126627701 30022 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.analysis.function; import org.apache.commons.math3.analysis.FunctionUtils; import org.apache.commons.math3.analysis.UnivariateFunction; import org.apache.commons.math3.analysis.DifferentiableUnivariateFunction; import org.apache.commons.math3.analysis.differentiation.DerivativeStructure; import org.apache.commons.math3.analysis.differentiation.UnivariateDifferentiableFunction; import org.apache.commons.math3.util.FastMath; /** * log(1 + p) function. * * @since 3.0 * @version $Id: Log1p.java 1383441 2012-09-11 14:56:39Z luc $ */ public class Log1p implements UnivariateDifferentiableFunction, DifferentiableUnivariateFunction { /** {@inheritDoc} */ public double value(double x) { return FastMath.log1p(x); } /** {@inheritDoc} * @deprecated as of 3.1, replaced by {@link #value(DerivativeStructure)} */ @Deprecated public UnivariateFunction derivative() { return FunctionUtils.toDifferentiableUnivariateFunction(this).derivative(); } /** {@inheritDoc} * @since 3.1 */ public DerivativeStructure value(final DerivativeStructure t) { return t.log1p(); } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/analysis/function/Logistic.java100644 1750 1750 21472 12126627701 30636 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.analysis.function; import org.apache.commons.math3.analysis.FunctionUtils; import org.apache.commons.math3.analysis.UnivariateFunction; import org.apache.commons.math3.analysis.DifferentiableUnivariateFunction; import org.apache.commons.math3.analysis.ParametricUnivariateFunction; import org.apache.commons.math3.analysis.differentiation.DerivativeStructure; import org.apache.commons.math3.analysis.differentiation.UnivariateDifferentiableFunction; import org.apache.commons.math3.exception.NotStrictlyPositiveException; import org.apache.commons.math3.exception.NullArgumentException; import org.apache.commons.math3.exception.DimensionMismatchException; import org.apache.commons.math3.util.FastMath; /** * * Generalised logistic function. * * @since 3.0 * @version $Id: Logistic.java 1391927 2012-09-30 00:03:30Z erans $ */ public class Logistic implements UnivariateDifferentiableFunction, DifferentiableUnivariateFunction { /** Lower asymptote. */ private final double a; /** Upper asymptote. */ private final double k; /** Growth rate. */ private final double b; /** Parameter that affects near which asymptote maximum growth occurs. */ private final double oneOverN; /** Parameter that affects the position of the curve along the ordinate axis. */ private final double q; /** Abscissa of maximum growth. */ private final double m; /** * @param k If {@code b > 0}, value of the function for x going towards +∞. * If {@code b < 0}, value of the function for x going towards -∞. * @param m Abscissa of maximum growth. * @param b Growth rate. * @param q Parameter that affects the position of the curve along the * ordinate axis. * @param a If {@code b > 0}, value of the function for x going towards -∞. * If {@code b < 0}, value of the function for x going towards +∞. * @param n Parameter that affects near which asymptote the maximum * growth occurs. * @throws NotStrictlyPositiveException if {@code n <= 0}. */ public Logistic(double k, double m, double b, double q, double a, double n) throws NotStrictlyPositiveException { if (n <= 0) { throw new NotStrictlyPositiveException(n); } this.k = k; this.m = m; this.b = b; this.q = q; this.a = a; oneOverN = 1 / n; } /** {@inheritDoc} */ public double value(double x) { return value(m - x, k, b, q, a, oneOverN); } /** {@inheritDoc} * @deprecated as of 3.1, replaced by {@link #value(DerivativeStructure)} */ @Deprecated public UnivariateFunction derivative() { return FunctionUtils.toDifferentiableUnivariateFunction(this).derivative(); } /** * Parametric function where the input array contains the parameters of * the logit function, ordered as follows: *
                              *
                            • Lower asymptote
                            • *
                            • Higher asymptote
                            • *
                            */ public static class Parametric implements ParametricUnivariateFunction { /** * Computes the value of the sigmoid at {@code x}. * * @param x Value for which the function must be computed. * @param param Values for {@code k}, {@code m}, {@code b}, {@code q}, * {@code a} and {@code n}. * @return the value of the function. * @throws NullArgumentException if {@code param} is {@code null}. * @throws DimensionMismatchException if the size of {@code param} is * not 6. * @throws NotStrictlyPositiveException if {@code param[5] <= 0}. */ public double value(double x, double ... param) throws NullArgumentException, DimensionMismatchException, NotStrictlyPositiveException { validateParameters(param); return Logistic.value(param[1] - x, param[0], param[2], param[3], param[4], 1 / param[5]); } /** * Computes the value of the gradient at {@code x}. * The components of the gradient vector are the partial * derivatives of the function with respect to each of the * parameters. * * @param x Value at which the gradient must be computed. * @param param Values for {@code k}, {@code m}, {@code b}, {@code q}, * {@code a} and {@code n}. * @return the gradient vector at {@code x}. * @throws NullArgumentException if {@code param} is {@code null}. * @throws DimensionMismatchException if the size of {@code param} is * not 6. * @throws NotStrictlyPositiveException if {@code param[5] <= 0}. */ public double[] gradient(double x, double ... param) throws NullArgumentException, DimensionMismatchException, NotStrictlyPositiveException { validateParameters(param); final double b = param[2]; final double q = param[3]; final double mMinusX = param[1] - x; final double oneOverN = 1 / param[5]; final double exp = FastMath.exp(b * mMinusX); final double qExp = q * exp; final double qExp1 = qExp + 1; final double factor1 = (param[0] - param[4]) * oneOverN / FastMath.pow(qExp1, oneOverN); final double factor2 = -factor1 / qExp1; // Components of the gradient. final double gk = Logistic.value(mMinusX, 1, b, q, 0, oneOverN); final double gm = factor2 * b * qExp; final double gb = factor2 * mMinusX * qExp; final double gq = factor2 * exp; final double ga = Logistic.value(mMinusX, 0, b, q, 1, oneOverN); final double gn = factor1 * Math.log(qExp1) * oneOverN; return new double[] { gk, gm, gb, gq, ga, gn }; } /** * Validates parameters to ensure they are appropriate for the evaluation of * the {@link #value(double,double[])} and {@link #gradient(double,double[])} * methods. * * @param param Values for {@code k}, {@code m}, {@code b}, {@code q}, * {@code a} and {@code n}. * @throws NullArgumentException if {@code param} is {@code null}. * @throws DimensionMismatchException if the size of {@code param} is * not 6. * @throws NotStrictlyPositiveException if {@code param[5] <= 0}. */ private void validateParameters(double[] param) throws NullArgumentException, DimensionMismatchException, NotStrictlyPositiveException { if (param == null) { throw new NullArgumentException(); } if (param.length != 6) { throw new DimensionMismatchException(param.length, 6); } if (param[5] <= 0) { throw new NotStrictlyPositiveException(param[5]); } } } /** * @param mMinusX {@code m - x}. * @param k {@code k}. * @param b {@code b}. * @param q {@code q}. * @param a {@code a}. * @param oneOverN {@code 1 / n}. * @return the value of the function. */ private static double value(double mMinusX, double k, double b, double q, double a, double oneOverN) { return a + (k - a) / FastMath.pow(1 + q * FastMath.exp(b * mMinusX), oneOverN); } /** {@inheritDoc} * @since 3.1 */ public DerivativeStructure value(final DerivativeStructure t) { return t.negate().add(m).multiply(b).exp().multiply(q).add(1).pow(oneOverN).reciprocal().multiply(k - a).add(a); } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/analysis/function/Pow.java100644 1750 1750 2317 12126627701 27603 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.analysis.function; import org.apache.commons.math3.analysis.BivariateFunction; import org.apache.commons.math3.util.FastMath; /** * Power function. * * @since 3.0 * @version $Id: Pow.java 1364377 2012-07-22 17:39:16Z tn $ */ public class Pow implements BivariateFunction { /** {@inheritDoc} */ public double value(double x, double y) { return FastMath.pow(x, y); } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/analysis/function/Asinh.java100644 1750 1750 3717 12126627701 30105 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.analysis.function; import org.apache.commons.math3.analysis.FunctionUtils; import org.apache.commons.math3.analysis.UnivariateFunction; import org.apache.commons.math3.analysis.DifferentiableUnivariateFunction; import org.apache.commons.math3.analysis.differentiation.DerivativeStructure; import org.apache.commons.math3.analysis.differentiation.UnivariateDifferentiableFunction; import org.apache.commons.math3.util.FastMath; /** * Hyperbolic arc-sine function. * * @since 3.0 * @version $Id: Asinh.java 1383441 2012-09-11 14:56:39Z luc $ */ public class Asinh implements UnivariateDifferentiableFunction, DifferentiableUnivariateFunction { /** {@inheritDoc} */ public double value(double x) { return FastMath.asinh(x); } /** {@inheritDoc} * @deprecated as of 3.1, replaced by {@link #value(DerivativeStructure)} */ @Deprecated public UnivariateFunction derivative() { return FunctionUtils.toDifferentiableUnivariateFunction(this).derivative(); } /** {@inheritDoc} * @since 3.1 */ public DerivativeStructure value(final DerivativeStructure t) { return t.asinh(); } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/analysis/function/Multiply.java100644 1750 1750 2250 12126627701 30651 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.analysis.function; import org.apache.commons.math3.analysis.BivariateFunction; /** * Multiply the two operands. * * @since 3.0 * @version $Id: Multiply.java 1364377 2012-07-22 17:39:16Z tn $ */ public class Multiply implements BivariateFunction { /** {@inheritDoc} */ public double value(double x, double y) { return x * y; } } ././@LongLink100644 0 0 147 12126630646 10263 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/analysis/function/HarmonicOscillator.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/analysis/function/HarmonicOscillator.ja100644 1750 1750 15502 12126627701 32323 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.analysis.function; import org.apache.commons.math3.analysis.DifferentiableUnivariateFunction; import org.apache.commons.math3.analysis.FunctionUtils; import org.apache.commons.math3.analysis.ParametricUnivariateFunction; import org.apache.commons.math3.analysis.UnivariateFunction; import org.apache.commons.math3.analysis.differentiation.DerivativeStructure; import org.apache.commons.math3.analysis.differentiation.UnivariateDifferentiableFunction; import org.apache.commons.math3.exception.DimensionMismatchException; import org.apache.commons.math3.exception.NullArgumentException; import org.apache.commons.math3.util.FastMath; /** * * simple harmonic oscillator function. * * @since 3.0 * @version $Id: HarmonicOscillator.java 1455194 2013-03-11 15:45:54Z luc $ */ public class HarmonicOscillator implements UnivariateDifferentiableFunction, DifferentiableUnivariateFunction { /** Amplitude. */ private final double amplitude; /** Angular frequency. */ private final double omega; /** Phase. */ private final double phase; /** * Harmonic oscillator function. * * @param amplitude Amplitude. * @param omega Angular frequency. * @param phase Phase. */ public HarmonicOscillator(double amplitude, double omega, double phase) { this.amplitude = amplitude; this.omega = omega; this.phase = phase; } /** {@inheritDoc} */ public double value(double x) { return value(omega * x + phase, amplitude); } /** {@inheritDoc} * @deprecated as of 3.1, replaced by {@link #value(DerivativeStructure)} */ @Deprecated public UnivariateFunction derivative() { return FunctionUtils.toDifferentiableUnivariateFunction(this).derivative(); } /** * Parametric function where the input array contains the parameters of * the harmonic oscillator function, ordered as follows: *
                              *
                            • Amplitude
                            • *
                            • Angular frequency
                            • *
                            • Phase
                            • *
                            */ public static class Parametric implements ParametricUnivariateFunction { /** * Computes the value of the harmonic oscillator at {@code x}. * * @param x Value for which the function must be computed. * @param param Values of norm, mean and standard deviation. * @return the value of the function. * @throws NullArgumentException if {@code param} is {@code null}. * @throws DimensionMismatchException if the size of {@code param} is * not 3. */ public double value(double x, double ... param) throws NullArgumentException, DimensionMismatchException { validateParameters(param); return HarmonicOscillator.value(x * param[1] + param[2], param[0]); } /** * Computes the value of the gradient at {@code x}. * The components of the gradient vector are the partial * derivatives of the function with respect to each of the * parameters (amplitude, angular frequency and phase). * * @param x Value at which the gradient must be computed. * @param param Values of amplitude, angular frequency and phase. * @return the gradient vector at {@code x}. * @throws NullArgumentException if {@code param} is {@code null}. * @throws DimensionMismatchException if the size of {@code param} is * not 3. */ public double[] gradient(double x, double ... param) throws NullArgumentException, DimensionMismatchException { validateParameters(param); final double amplitude = param[0]; final double omega = param[1]; final double phase = param[2]; final double xTimesOmegaPlusPhase = omega * x + phase; final double a = HarmonicOscillator.value(xTimesOmegaPlusPhase, 1); final double p = -amplitude * FastMath.sin(xTimesOmegaPlusPhase); final double w = p * x; return new double[] { a, w, p }; } /** * Validates parameters to ensure they are appropriate for the evaluation of * the {@link #value(double,double[])} and {@link #gradient(double,double[])} * methods. * * @param param Values of norm, mean and standard deviation. * @throws NullArgumentException if {@code param} is {@code null}. * @throws DimensionMismatchException if the size of {@code param} is * not 3. */ private void validateParameters(double[] param) throws NullArgumentException, DimensionMismatchException { if (param == null) { throw new NullArgumentException(); } if (param.length != 3) { throw new DimensionMismatchException(param.length, 3); } } } /** * @param xTimesOmegaPlusPhase {@code omega * x + phase}. * @param amplitude Amplitude. * @return the value of the harmonic oscillator function at {@code x}. */ private static double value(double xTimesOmegaPlusPhase, double amplitude) { return amplitude * FastMath.cos(xTimesOmegaPlusPhase); } /** {@inheritDoc} * @since 3.1 */ public DerivativeStructure value(final DerivativeStructure t) throws DimensionMismatchException { final double x = t.getValue(); double[] f = new double[t.getOrder() + 1]; final double alpha = omega * x + phase; f[0] = amplitude * FastMath.cos(alpha); if (f.length > 1) { f[1] = -amplitude * omega * FastMath.sin(alpha); final double mo2 = - omega * omega; for (int i = 2; i < f.length; ++i) { f[i] = mo2 * f[i - 2]; } } return t.compose(f); } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/analysis/function/Log.java100644 1750 1750 3705 12126627701 27561 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.analysis.function; import org.apache.commons.math3.analysis.FunctionUtils; import org.apache.commons.math3.analysis.UnivariateFunction; import org.apache.commons.math3.analysis.DifferentiableUnivariateFunction; import org.apache.commons.math3.analysis.differentiation.DerivativeStructure; import org.apache.commons.math3.analysis.differentiation.UnivariateDifferentiableFunction; import org.apache.commons.math3.util.FastMath; /** * Natural logarithm function. * * @since 3.0 * @version $Id: Log.java 1383441 2012-09-11 14:56:39Z luc $ */ public class Log implements UnivariateDifferentiableFunction, DifferentiableUnivariateFunction { /** {@inheritDoc} */ public double value(double x) { return FastMath.log(x); } /** {@inheritDoc} * @deprecated as of 3.1, replaced by {@link #value(DerivativeStructure)} */ @Deprecated public UnivariateFunction derivative() { return FunctionUtils.toDifferentiableUnivariateFunction(this).derivative(); } /** {@inheritDoc} * @since 3.1 */ public DerivativeStructure value(final DerivativeStructure t) { return t.log(); } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/analysis/function/Atan2.java100644 1750 1750 2333 12126627701 30001 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.analysis.function; import org.apache.commons.math3.analysis.BivariateFunction; import org.apache.commons.math3.util.FastMath; /** * Arc-tangent function. * * @since 3.0 * @version $Id: Atan2.java 1364377 2012-07-22 17:39:16Z tn $ */ public class Atan2 implements BivariateFunction { /** {@inheritDoc} */ public double value(double x, double y) { return FastMath.atan2(x, y); } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/analysis/function/StepFunction.java100644 1750 1750 7360 12126627701 31462 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.analysis.function; import java.util.Arrays; import org.apache.commons.math3.analysis.UnivariateFunction; import org.apache.commons.math3.exception.DimensionMismatchException; import org.apache.commons.math3.exception.NonMonotonicSequenceException; import org.apache.commons.math3.exception.NullArgumentException; import org.apache.commons.math3.exception.NoDataException; import org.apache.commons.math3.util.MathArrays; /** * * Step function. * * @since 3.0 * @version $Id: StepFunction.java 1455194 2013-03-11 15:45:54Z luc $ */ public class StepFunction implements UnivariateFunction { /** Abscissae. */ private final double[] abscissa; /** Ordinates. */ private final double[] ordinate; /** * Builds a step function from a list of arguments and the corresponding * values. Specifically, returns the function h(x) defined by
                            
                                 * h(x) = y[0] for all x < x[1]
                                 *        y[1] for x[1] <= x < x[2]
                                 *        ...
                                 *        y[y.length - 1] for x >= x[x.length - 1]
                                 * 
                            * The value of {@code x[0]} is ignored, but it must be strictly less than * {@code x[1]}. * * @param x Domain values where the function changes value. * @param y Values of the function. * @throws NonMonotonicSequenceException * if the {@code x} array is not sorted in strictly increasing order. * @throws NullArgumentException if {@code x} or {@code y} are {@code null}. * @throws NoDataException if {@code x} or {@code y} are zero-length. * @throws DimensionMismatchException if {@code x} and {@code y} do not * have the same length. */ public StepFunction(double[] x, double[] y) throws NullArgumentException, NoDataException, DimensionMismatchException, NonMonotonicSequenceException { if (x == null || y == null) { throw new NullArgumentException(); } if (x.length == 0 || y.length == 0) { throw new NoDataException(); } if (y.length != x.length) { throw new DimensionMismatchException(y.length, x.length); } MathArrays.checkOrder(x); abscissa = MathArrays.copyOf(x); ordinate = MathArrays.copyOf(y); } /** {@inheritDoc} */ public double value(double x) { int index = Arrays.binarySearch(abscissa, x); double fx = 0; if (index < -1) { // "x" is between "abscissa[-index-2]" and "abscissa[-index-1]". fx = ordinate[-index-2]; } else if (index >= 0) { // "x" is exactly "abscissa[index]". fx = ordinate[index]; } else { // Otherwise, "x" is smaller than the first value in "abscissa" // (hence the returned value should be "ordinate[0]"). fx = ordinate[0]; } return fx; } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/analysis/function/Constant.java100644 1750 1750 3675 12126627701 30637 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.analysis.function; import org.apache.commons.math3.analysis.DifferentiableUnivariateFunction; import org.apache.commons.math3.analysis.differentiation.DerivativeStructure; import org.apache.commons.math3.analysis.differentiation.UnivariateDifferentiableFunction; /** * Constant function. * * @since 3.0 * @version $Id: Constant.java 1424087 2012-12-19 20:32:50Z luc $ */ public class Constant implements UnivariateDifferentiableFunction, DifferentiableUnivariateFunction { /** Constant. */ private final double c; /** * @param c Constant. */ public Constant(double c) { this.c = c; } /** {@inheritDoc} */ public double value(double x) { return c; } /** {@inheritDoc} * @deprecated as of 3.1, replaced by {@link #value(DerivativeStructure)} */ @Deprecated public DifferentiableUnivariateFunction derivative() { return new Constant(0); } /** {@inheritDoc} * @since 3.1 */ public DerivativeStructure value(final DerivativeStructure t) { return new DerivativeStructure(t.getFreeParameters(), t.getOrder(), c); } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/analysis/function/Sqrt.java100644 1750 1750 3703 12126627701 27767 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.analysis.function; import org.apache.commons.math3.analysis.FunctionUtils; import org.apache.commons.math3.analysis.UnivariateFunction; import org.apache.commons.math3.analysis.DifferentiableUnivariateFunction; import org.apache.commons.math3.analysis.differentiation.DerivativeStructure; import org.apache.commons.math3.analysis.differentiation.UnivariateDifferentiableFunction; import org.apache.commons.math3.util.FastMath; /** * Square-root function. * * @since 3.0 * @version $Id: Sqrt.java 1383441 2012-09-11 14:56:39Z luc $ */ public class Sqrt implements UnivariateDifferentiableFunction, DifferentiableUnivariateFunction { /** {@inheritDoc} */ public double value(double x) { return FastMath.sqrt(x); } /** {@inheritDoc} * @deprecated as of 3.1, replaced by {@link #value(DerivativeStructure)} */ @Deprecated public UnivariateFunction derivative() { return FunctionUtils.toDifferentiableUnivariateFunction(this).derivative(); } /** {@inheritDoc} * @since 3.1 */ public DerivativeStructure value(final DerivativeStructure t) { return t.sqrt(); } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/analysis/function/Logit.java100644 1750 1750 16764 12126627701 30147 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.analysis.function; import org.apache.commons.math3.analysis.DifferentiableUnivariateFunction; import org.apache.commons.math3.analysis.FunctionUtils; import org.apache.commons.math3.analysis.ParametricUnivariateFunction; import org.apache.commons.math3.analysis.UnivariateFunction; import org.apache.commons.math3.analysis.differentiation.DerivativeStructure; import org.apache.commons.math3.analysis.differentiation.UnivariateDifferentiableFunction; import org.apache.commons.math3.exception.DimensionMismatchException; import org.apache.commons.math3.exception.NullArgumentException; import org.apache.commons.math3.exception.OutOfRangeException; import org.apache.commons.math3.util.FastMath; /** * * Logit function. * It is the inverse of the {@link Sigmoid sigmoid} function. * * @since 3.0 * @version $Id: Logit.java 1391927 2012-09-30 00:03:30Z erans $ */ public class Logit implements UnivariateDifferentiableFunction, DifferentiableUnivariateFunction { /** Lower bound. */ private final double lo; /** Higher bound. */ private final double hi; /** * Usual logit function, where the lower bound is 0 and the higher * bound is 1. */ public Logit() { this(0, 1); } /** * Logit function. * * @param lo Lower bound of the function domain. * @param hi Higher bound of the function domain. */ public Logit(double lo, double hi) { this.lo = lo; this.hi = hi; } /** {@inheritDoc} */ public double value(double x) throws OutOfRangeException { return value(x, lo, hi); } /** {@inheritDoc} * @deprecated as of 3.1, replaced by {@link #value(DerivativeStructure)} */ @Deprecated public UnivariateFunction derivative() { return FunctionUtils.toDifferentiableUnivariateFunction(this).derivative(); } /** * Parametric function where the input array contains the parameters of * the logit function, ordered as follows: *
                              *
                            • Lower bound
                            • *
                            • Higher bound
                            • *
                            */ public static class Parametric implements ParametricUnivariateFunction { /** * Computes the value of the logit at {@code x}. * * @param x Value for which the function must be computed. * @param param Values of lower bound and higher bounds. * @return the value of the function. * @throws NullArgumentException if {@code param} is {@code null}. * @throws DimensionMismatchException if the size of {@code param} is * not 2. */ public double value(double x, double ... param) throws NullArgumentException, DimensionMismatchException { validateParameters(param); return Logit.value(x, param[0], param[1]); } /** * Computes the value of the gradient at {@code x}. * The components of the gradient vector are the partial * derivatives of the function with respect to each of the * parameters (lower bound and higher bound). * * @param x Value at which the gradient must be computed. * @param param Values for lower and higher bounds. * @return the gradient vector at {@code x}. * @throws NullArgumentException if {@code param} is {@code null}. * @throws DimensionMismatchException if the size of {@code param} is * not 2. */ public double[] gradient(double x, double ... param) throws NullArgumentException, DimensionMismatchException { validateParameters(param); final double lo = param[0]; final double hi = param[1]; return new double[] { 1 / (lo - x), 1 / (hi - x) }; } /** * Validates parameters to ensure they are appropriate for the evaluation of * the {@link #value(double,double[])} and {@link #gradient(double,double[])} * methods. * * @param param Values for lower and higher bounds. * @throws NullArgumentException if {@code param} is {@code null}. * @throws DimensionMismatchException if the size of {@code param} is * not 2. */ private void validateParameters(double[] param) throws NullArgumentException, DimensionMismatchException { if (param == null) { throw new NullArgumentException(); } if (param.length != 2) { throw new DimensionMismatchException(param.length, 2); } } } /** * @param x Value at which to compute the logit. * @param lo Lower bound. * @param hi Higher bound. * @return the value of the logit function at {@code x}. * @throws OutOfRangeException if {@code x < lo} or {@code x > hi}. */ private static double value(double x, double lo, double hi) throws OutOfRangeException { if (x < lo || x > hi) { throw new OutOfRangeException(x, lo, hi); } return FastMath.log((x - lo) / (hi - x)); } /** {@inheritDoc} * @since 3.1 * @exception OutOfRangeException if parameter is outside of function domain */ public DerivativeStructure value(final DerivativeStructure t) throws OutOfRangeException { final double x = t.getValue(); if (x < lo || x > hi) { throw new OutOfRangeException(x, lo, hi); } double[] f = new double[t.getOrder() + 1]; // function value f[0] = FastMath.log((x - lo) / (hi - x)); if (Double.isInfinite(f[0])) { if (f.length > 1) { f[1] = Double.POSITIVE_INFINITY; } // fill the array with infinities // (for x close to lo the signs will flip between -inf and +inf, // for x close to hi the signs will always be +inf) // this is probably overkill, since the call to compose at the end // of the method will transform most infinities into NaN ... for (int i = 2; i < f.length; ++i) { f[i] = f[i - 2]; } } else { // function derivatives final double invL = 1.0 / (x - lo); double xL = invL; final double invH = 1.0 / (hi - x); double xH = invH; for (int i = 1; i < f.length; ++i) { f[i] = xL + xH; xL *= -i * invL; xH *= i * invH; } } return t.compose(f); } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/analysis/function/Sinh.java100644 1750 1750 3447 12126627701 27744 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.analysis.function; import org.apache.commons.math3.analysis.DifferentiableUnivariateFunction; import org.apache.commons.math3.analysis.differentiation.DerivativeStructure; import org.apache.commons.math3.analysis.differentiation.UnivariateDifferentiableFunction; import org.apache.commons.math3.util.FastMath; /** * Hyperbolic sine function. * * @since 3.0 * @version $Id: Sinh.java 1424087 2012-12-19 20:32:50Z luc $ */ public class Sinh implements UnivariateDifferentiableFunction, DifferentiableUnivariateFunction { /** {@inheritDoc} */ public double value(double x) { return FastMath.sinh(x); } /** {@inheritDoc} * @deprecated as of 3.1, replaced by {@link #value(DerivativeStructure)} */ @Deprecated public DifferentiableUnivariateFunction derivative() { return new Cosh(); } /** {@inheritDoc} * @since 3.1 */ public DerivativeStructure value(final DerivativeStructure t) { return t.sinh(); } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/analysis/polynomials/package-info.java100644 1750 1750 1725 12126627705 32111 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ /** * * Univariate real polynomials implementations, seen as differentiable * univariate real functions. * */ package org.apache.commons.math3.analysis.polynomials; ././@LongLink100644 0 0 152 12126630646 10257 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/analysis/polynomials/PolynomialFunction.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/analysis/polynomials/PolynomialFunction100644 1750 1750 34705 12126627705 32522 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.analysis.polynomials; import java.io.Serializable; import java.util.Arrays; import org.apache.commons.math3.exception.util.LocalizedFormats; import org.apache.commons.math3.exception.NoDataException; import org.apache.commons.math3.exception.NullArgumentException; import org.apache.commons.math3.analysis.DifferentiableUnivariateFunction; import org.apache.commons.math3.analysis.UnivariateFunction; import org.apache.commons.math3.analysis.ParametricUnivariateFunction; import org.apache.commons.math3.analysis.differentiation.DerivativeStructure; import org.apache.commons.math3.analysis.differentiation.UnivariateDifferentiableFunction; import org.apache.commons.math3.util.FastMath; import org.apache.commons.math3.util.MathUtils; /** * Immutable representation of a real polynomial function with real coefficients. *

                            * Horner's Method * is used to evaluate the function.

                            * * @version $Id: PolynomialFunction.java 1455194 2013-03-11 15:45:54Z luc $ */ public class PolynomialFunction implements UnivariateDifferentiableFunction, DifferentiableUnivariateFunction, Serializable { /** * Serialization identifier */ private static final long serialVersionUID = -7726511984200295583L; /** * The coefficients of the polynomial, ordered by degree -- i.e., * coefficients[0] is the constant term and coefficients[n] is the * coefficient of x^n where n is the degree of the polynomial. */ private final double coefficients[]; /** * Construct a polynomial with the given coefficients. The first element * of the coefficients array is the constant term. Higher degree * coefficients follow in sequence. The degree of the resulting polynomial * is the index of the last non-null element of the array, or 0 if all elements * are null. *

                            * The constructor makes a copy of the input array and assigns the copy to * the coefficients property.

                            * * @param c Polynomial coefficients. * @throws NullArgumentException if {@code c} is {@code null}. * @throws NoDataException if {@code c} is empty. */ public PolynomialFunction(double c[]) throws NullArgumentException, NoDataException { super(); MathUtils.checkNotNull(c); int n = c.length; if (n == 0) { throw new NoDataException(LocalizedFormats.EMPTY_POLYNOMIALS_COEFFICIENTS_ARRAY); } while ((n > 1) && (c[n - 1] == 0)) { --n; } this.coefficients = new double[n]; System.arraycopy(c, 0, this.coefficients, 0, n); } /** * Compute the value of the function for the given argument. *

                            * The value returned is
                            * coefficients[n] * x^n + ... + coefficients[1] * x + coefficients[0] *

                            * * @param x Argument for which the function value should be computed. * @return the value of the polynomial at the given point. * @see UnivariateFunction#value(double) */ public double value(double x) { return evaluate(coefficients, x); } /** * Returns the degree of the polynomial. * * @return the degree of the polynomial. */ public int degree() { return coefficients.length - 1; } /** * Returns a copy of the coefficients array. *

                            * Changes made to the returned copy will not affect the coefficients of * the polynomial.

                            * * @return a fresh copy of the coefficients array. */ public double[] getCoefficients() { return coefficients.clone(); } /** * Uses Horner's Method to evaluate the polynomial with the given coefficients at * the argument. * * @param coefficients Coefficients of the polynomial to evaluate. * @param argument Input value. * @return the value of the polynomial. * @throws NoDataException if {@code coefficients} is empty. * @throws NullArgumentException if {@code coefficients} is {@code null}. */ protected static double evaluate(double[] coefficients, double argument) throws NullArgumentException, NoDataException { MathUtils.checkNotNull(coefficients); int n = coefficients.length; if (n == 0) { throw new NoDataException(LocalizedFormats.EMPTY_POLYNOMIALS_COEFFICIENTS_ARRAY); } double result = coefficients[n - 1]; for (int j = n - 2; j >= 0; j--) { result = argument * result + coefficients[j]; } return result; } /** {@inheritDoc} * @since 3.1 * @throws NoDataException if {@code coefficients} is empty. * @throws NullArgumentException if {@code coefficients} is {@code null}. */ public DerivativeStructure value(final DerivativeStructure t) throws NullArgumentException, NoDataException { MathUtils.checkNotNull(coefficients); int n = coefficients.length; if (n == 0) { throw new NoDataException(LocalizedFormats.EMPTY_POLYNOMIALS_COEFFICIENTS_ARRAY); } DerivativeStructure result = new DerivativeStructure(t.getFreeParameters(), t.getOrder(), coefficients[n - 1]); for (int j = n - 2; j >= 0; j--) { result = result.multiply(t).add(coefficients[j]); } return result; } /** * Add a polynomial to the instance. * * @param p Polynomial to add. * @return a new polynomial which is the sum of the instance and {@code p}. */ public PolynomialFunction add(final PolynomialFunction p) { // identify the lowest degree polynomial final int lowLength = FastMath.min(coefficients.length, p.coefficients.length); final int highLength = FastMath.max(coefficients.length, p.coefficients.length); // build the coefficients array double[] newCoefficients = new double[highLength]; for (int i = 0; i < lowLength; ++i) { newCoefficients[i] = coefficients[i] + p.coefficients[i]; } System.arraycopy((coefficients.length < p.coefficients.length) ? p.coefficients : coefficients, lowLength, newCoefficients, lowLength, highLength - lowLength); return new PolynomialFunction(newCoefficients); } /** * Subtract a polynomial from the instance. * * @param p Polynomial to subtract. * @return a new polynomial which is the difference the instance minus {@code p}. */ public PolynomialFunction subtract(final PolynomialFunction p) { // identify the lowest degree polynomial int lowLength = FastMath.min(coefficients.length, p.coefficients.length); int highLength = FastMath.max(coefficients.length, p.coefficients.length); // build the coefficients array double[] newCoefficients = new double[highLength]; for (int i = 0; i < lowLength; ++i) { newCoefficients[i] = coefficients[i] - p.coefficients[i]; } if (coefficients.length < p.coefficients.length) { for (int i = lowLength; i < highLength; ++i) { newCoefficients[i] = -p.coefficients[i]; } } else { System.arraycopy(coefficients, lowLength, newCoefficients, lowLength, highLength - lowLength); } return new PolynomialFunction(newCoefficients); } /** * Negate the instance. * * @return a new polynomial. */ public PolynomialFunction negate() { double[] newCoefficients = new double[coefficients.length]; for (int i = 0; i < coefficients.length; ++i) { newCoefficients[i] = -coefficients[i]; } return new PolynomialFunction(newCoefficients); } /** * Multiply the instance by a polynomial. * * @param p Polynomial to multiply by. * @return a new polynomial. */ public PolynomialFunction multiply(final PolynomialFunction p) { double[] newCoefficients = new double[coefficients.length + p.coefficients.length - 1]; for (int i = 0; i < newCoefficients.length; ++i) { newCoefficients[i] = 0.0; for (int j = FastMath.max(0, i + 1 - p.coefficients.length); j < FastMath.min(coefficients.length, i + 1); ++j) { newCoefficients[i] += coefficients[j] * p.coefficients[i-j]; } } return new PolynomialFunction(newCoefficients); } /** * Returns the coefficients of the derivative of the polynomial with the given coefficients. * * @param coefficients Coefficients of the polynomial to differentiate. * @return the coefficients of the derivative or {@code null} if coefficients has length 1. * @throws NoDataException if {@code coefficients} is empty. * @throws NullArgumentException if {@code coefficients} is {@code null}. */ protected static double[] differentiate(double[] coefficients) throws NullArgumentException, NoDataException { MathUtils.checkNotNull(coefficients); int n = coefficients.length; if (n == 0) { throw new NoDataException(LocalizedFormats.EMPTY_POLYNOMIALS_COEFFICIENTS_ARRAY); } if (n == 1) { return new double[]{0}; } double[] result = new double[n - 1]; for (int i = n - 1; i > 0; i--) { result[i - 1] = i * coefficients[i]; } return result; } /** * Returns the derivative as a {@link PolynomialFunction}. * * @return the derivative polynomial. */ public PolynomialFunction polynomialDerivative() { return new PolynomialFunction(differentiate(coefficients)); } /** * Returns the derivative as a {@link UnivariateFunction}. * * @return the derivative function. */ public UnivariateFunction derivative() { return polynomialDerivative(); } /** * Returns a string representation of the polynomial. * *

                            The representation is user oriented. Terms are displayed lowest * degrees first. The multiplications signs, coefficients equals to * one and null terms are not displayed (except if the polynomial is 0, * in which case the 0 constant term is displayed). Addition of terms * with negative coefficients are replaced by subtraction of terms * with positive coefficients except for the first displayed term * (i.e. we display -3 for a constant negative polynomial, * but 1 - 3 x + x^2 if the negative coefficient is not * the first one displayed).

                            * * @return a string representation of the polynomial. */ @Override public String toString() { StringBuilder s = new StringBuilder(); if (coefficients[0] == 0.0) { if (coefficients.length == 1) { return "0"; } } else { s.append(toString(coefficients[0])); } for (int i = 1; i < coefficients.length; ++i) { if (coefficients[i] != 0) { if (s.length() > 0) { if (coefficients[i] < 0) { s.append(" - "); } else { s.append(" + "); } } else { if (coefficients[i] < 0) { s.append("-"); } } double absAi = FastMath.abs(coefficients[i]); if ((absAi - 1) != 0) { s.append(toString(absAi)); s.append(' '); } s.append("x"); if (i > 1) { s.append('^'); s.append(Integer.toString(i)); } } } return s.toString(); } /** * Creates a string representing a coefficient, removing ".0" endings. * * @param coeff Coefficient. * @return a string representation of {@code coeff}. */ private static String toString(double coeff) { final String c = Double.toString(coeff); if (c.endsWith(".0")) { return c.substring(0, c.length() - 2); } else { return c; } } /** {@inheritDoc} */ @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + Arrays.hashCode(coefficients); return result; } /** {@inheritDoc} */ @Override public boolean equals(Object obj) { if (this == obj) { return true; } if (!(obj instanceof PolynomialFunction)) { return false; } PolynomialFunction other = (PolynomialFunction) obj; if (!Arrays.equals(coefficients, other.coefficients)) { return false; } return true; } /** * Dedicated parametric polynomial class. * * @since 3.0 */ public static class Parametric implements ParametricUnivariateFunction { /** {@inheritDoc} */ public double[] gradient(double x, double ... parameters) { final double[] gradient = new double[parameters.length]; double xn = 1.0; for (int i = 0; i < parameters.length; ++i) { gradient[i] = xn; xn *= x; } return gradient; } /** {@inheritDoc} */ public double value(final double x, final double ... parameters) throws NoDataException { return PolynomialFunction.evaluate(parameters, x); } } } ././@LongLink100644 0 0 166 12126630646 10264 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/analysis/polynomials/PolynomialFunctionLagrangeForm.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/analysis/polynomials/PolynomialFunction100644 1750 1750 30532 12126627705 32514 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.analysis.polynomials; import org.apache.commons.math3.analysis.UnivariateFunction; import org.apache.commons.math3.util.FastMath; import org.apache.commons.math3.util.MathArrays; import org.apache.commons.math3.exception.DimensionMismatchException; import org.apache.commons.math3.exception.NonMonotonicSequenceException; import org.apache.commons.math3.exception.NumberIsTooSmallException; import org.apache.commons.math3.exception.util.LocalizedFormats; /** * Implements the representation of a real polynomial function in * * Lagrange Form. For reference, see Introduction to Numerical * Analysis, ISBN 038795452X, chapter 2. *

                            * The approximated function should be smooth enough for Lagrange polynomial * to work well. Otherwise, consider using splines instead.

                            * * @version $Id: PolynomialFunctionLagrangeForm.java 1455194 2013-03-11 15:45:54Z luc $ * @since 1.2 */ public class PolynomialFunctionLagrangeForm implements UnivariateFunction { /** * The coefficients of the polynomial, ordered by degree -- i.e. * coefficients[0] is the constant term and coefficients[n] is the * coefficient of x^n where n is the degree of the polynomial. */ private double coefficients[]; /** * Interpolating points (abscissas). */ private final double x[]; /** * Function values at interpolating points. */ private final double y[]; /** * Whether the polynomial coefficients are available. */ private boolean coefficientsComputed; /** * Construct a Lagrange polynomial with the given abscissas and function * values. The order of interpolating points are not important. *

                            * The constructor makes copy of the input arrays and assigns them.

                            * * @param x interpolating points * @param y function values at interpolating points * @throws DimensionMismatchException if the array lengths are different. * @throws NumberIsTooSmallException if the number of points is less than 2. * @throws NonMonotonicSequenceException * if two abscissae have the same value. */ public PolynomialFunctionLagrangeForm(double x[], double y[]) throws DimensionMismatchException, NumberIsTooSmallException, NonMonotonicSequenceException { this.x = new double[x.length]; this.y = new double[y.length]; System.arraycopy(x, 0, this.x, 0, x.length); System.arraycopy(y, 0, this.y, 0, y.length); coefficientsComputed = false; if (!verifyInterpolationArray(x, y, false)) { MathArrays.sortInPlace(this.x, this.y); // Second check in case some abscissa is duplicated. verifyInterpolationArray(this.x, this.y, true); } } /** * Calculate the function value at the given point. * * @param z Point at which the function value is to be computed. * @return the function value. * @throws DimensionMismatchException if {@code x} and {@code y} have * different lengths. * @throws org.apache.commons.math3.exception.NonMonotonicSequenceException * if {@code x} is not sorted in strictly increasing order. * @throws NumberIsTooSmallException if the size of {@code x} is less * than 2. */ public double value(double z) { return evaluateInternal(x, y, z); } /** * Returns the degree of the polynomial. * * @return the degree of the polynomial */ public int degree() { return x.length - 1; } /** * Returns a copy of the interpolating points array. *

                            * Changes made to the returned copy will not affect the polynomial.

                            * * @return a fresh copy of the interpolating points array */ public double[] getInterpolatingPoints() { double[] out = new double[x.length]; System.arraycopy(x, 0, out, 0, x.length); return out; } /** * Returns a copy of the interpolating values array. *

                            * Changes made to the returned copy will not affect the polynomial.

                            * * @return a fresh copy of the interpolating values array */ public double[] getInterpolatingValues() { double[] out = new double[y.length]; System.arraycopy(y, 0, out, 0, y.length); return out; } /** * Returns a copy of the coefficients array. *

                            * Changes made to the returned copy will not affect the polynomial.

                            *

                            * Note that coefficients computation can be ill-conditioned. Use with caution * and only when it is necessary.

                            * * @return a fresh copy of the coefficients array */ public double[] getCoefficients() { if (!coefficientsComputed) { computeCoefficients(); } double[] out = new double[coefficients.length]; System.arraycopy(coefficients, 0, out, 0, coefficients.length); return out; } /** * Evaluate the Lagrange polynomial using * * Neville's Algorithm. It takes O(n^2) time. * * @param x Interpolating points array. * @param y Interpolating values array. * @param z Point at which the function value is to be computed. * @return the function value. * @throws DimensionMismatchException if {@code x} and {@code y} have * different lengths. * @throws NonMonotonicSequenceException * if {@code x} is not sorted in strictly increasing order. * @throws NumberIsTooSmallException if the size of {@code x} is less * than 2. */ public static double evaluate(double x[], double y[], double z) throws DimensionMismatchException, NumberIsTooSmallException, NonMonotonicSequenceException { if (verifyInterpolationArray(x, y, false)) { return evaluateInternal(x, y, z); } // Array is not sorted. final double[] xNew = new double[x.length]; final double[] yNew = new double[y.length]; System.arraycopy(x, 0, xNew, 0, x.length); System.arraycopy(y, 0, yNew, 0, y.length); MathArrays.sortInPlace(xNew, yNew); // Second check in case some abscissa is duplicated. verifyInterpolationArray(xNew, yNew, true); return evaluateInternal(xNew, yNew, z); } /** * Evaluate the Lagrange polynomial using * * Neville's Algorithm. It takes O(n^2) time. * * @param x Interpolating points array. * @param y Interpolating values array. * @param z Point at which the function value is to be computed. * @return the function value. * @throws DimensionMismatchException if {@code x} and {@code y} have * different lengths. * @throws org.apache.commons.math3.exception.NonMonotonicSequenceException * if {@code x} is not sorted in strictly increasing order. * @throws NumberIsTooSmallException if the size of {@code x} is less * than 2. */ private static double evaluateInternal(double x[], double y[], double z) { int nearest = 0; final int n = x.length; final double[] c = new double[n]; final double[] d = new double[n]; double min_dist = Double.POSITIVE_INFINITY; for (int i = 0; i < n; i++) { // initialize the difference arrays c[i] = y[i]; d[i] = y[i]; // find out the abscissa closest to z final double dist = FastMath.abs(z - x[i]); if (dist < min_dist) { nearest = i; min_dist = dist; } } // initial approximation to the function value at z double value = y[nearest]; for (int i = 1; i < n; i++) { for (int j = 0; j < n-i; j++) { final double tc = x[j] - z; final double td = x[i+j] - z; final double divider = x[j] - x[i+j]; // update the difference arrays final double w = (c[j+1] - d[j]) / divider; c[j] = tc * w; d[j] = td * w; } // sum up the difference terms to get the final value if (nearest < 0.5*(n-i+1)) { value += c[nearest]; // fork down } else { nearest--; value += d[nearest]; // fork up } } return value; } /** * Calculate the coefficients of Lagrange polynomial from the * interpolation data. It takes O(n^2) time. * Note that this computation can be ill-conditioned: Use with caution * and only when it is necessary. */ protected void computeCoefficients() { final int n = degree() + 1; coefficients = new double[n]; for (int i = 0; i < n; i++) { coefficients[i] = 0.0; } // c[] are the coefficients of P(x) = (x-x[0])(x-x[1])...(x-x[n-1]) final double[] c = new double[n+1]; c[0] = 1.0; for (int i = 0; i < n; i++) { for (int j = i; j > 0; j--) { c[j] = c[j-1] - c[j] * x[i]; } c[0] *= -x[i]; c[i+1] = 1; } final double[] tc = new double[n]; for (int i = 0; i < n; i++) { // d = (x[i]-x[0])...(x[i]-x[i-1])(x[i]-x[i+1])...(x[i]-x[n-1]) double d = 1; for (int j = 0; j < n; j++) { if (i != j) { d *= x[i] - x[j]; } } final double t = y[i] / d; // Lagrange polynomial is the sum of n terms, each of which is a // polynomial of degree n-1. tc[] are the coefficients of the i-th // numerator Pi(x) = (x-x[0])...(x-x[i-1])(x-x[i+1])...(x-x[n-1]). tc[n-1] = c[n]; // actually c[n] = 1 coefficients[n-1] += t * tc[n-1]; for (int j = n-2; j >= 0; j--) { tc[j] = c[j+1] + tc[j+1] * x[i]; coefficients[j] += t * tc[j]; } } coefficientsComputed = true; } /** * Check that the interpolation arrays are valid. * The arrays features checked by this method are that both arrays have the * same length and this length is at least 2. * * @param x Interpolating points array. * @param y Interpolating values array. * @param abort Whether to throw an exception if {@code x} is not sorted. * @throws DimensionMismatchException if the array lengths are different. * @throws NumberIsTooSmallException if the number of points is less than 2. * @throws org.apache.commons.math3.exception.NonMonotonicSequenceException * if {@code x} is not sorted in strictly increasing order and {@code abort} * is {@code true}. * @return {@code false} if the {@code x} is not sorted in increasing order, * {@code true} otherwise. * @see #evaluate(double[], double[], double) * @see #computeCoefficients() */ public static boolean verifyInterpolationArray(double x[], double y[], boolean abort) throws DimensionMismatchException, NumberIsTooSmallException, NonMonotonicSequenceException { if (x.length != y.length) { throw new DimensionMismatchException(x.length, y.length); } if (x.length < 2) { throw new NumberIsTooSmallException(LocalizedFormats.WRONG_NUMBER_OF_POINTS, 2, x.length, true); } return MathArrays.checkOrder(x, MathArrays.OrderDirection.INCREASING, true, abort); } } ././@LongLink100644 0 0 164 12126630646 10262 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/analysis/polynomials/PolynomialFunctionNewtonForm.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/analysis/polynomials/PolynomialFunction100644 1750 1750 21101 12126627705 32504 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.analysis.polynomials; import org.apache.commons.math3.analysis.differentiation.DerivativeStructure; import org.apache.commons.math3.analysis.differentiation.UnivariateDifferentiableFunction; import org.apache.commons.math3.exception.DimensionMismatchException; import org.apache.commons.math3.exception.NoDataException; import org.apache.commons.math3.exception.NullArgumentException; import org.apache.commons.math3.exception.util.LocalizedFormats; import org.apache.commons.math3.util.MathUtils; /** * Implements the representation of a real polynomial function in * Newton Form. For reference, see Elementary Numerical Analysis, * ISBN 0070124477, chapter 2. *

                            * The formula of polynomial in Newton form is * p(x) = a[0] + a[1](x-c[0]) + a[2](x-c[0])(x-c[1]) + ... + * a[n](x-c[0])(x-c[1])...(x-c[n-1]) * Note that the length of a[] is one more than the length of c[]

                            * * @version $Id: PolynomialFunctionNewtonForm.java 1455194 2013-03-11 15:45:54Z luc $ * @since 1.2 */ public class PolynomialFunctionNewtonForm implements UnivariateDifferentiableFunction { /** * The coefficients of the polynomial, ordered by degree -- i.e. * coefficients[0] is the constant term and coefficients[n] is the * coefficient of x^n where n is the degree of the polynomial. */ private double coefficients[]; /** * Centers of the Newton polynomial. */ private final double c[]; /** * When all c[i] = 0, a[] becomes normal polynomial coefficients, * i.e. a[i] = coefficients[i]. */ private final double a[]; /** * Whether the polynomial coefficients are available. */ private boolean coefficientsComputed; /** * Construct a Newton polynomial with the given a[] and c[]. The order of * centers are important in that if c[] shuffle, then values of a[] would * completely change, not just a permutation of old a[]. *

                            * The constructor makes copy of the input arrays and assigns them.

                            * * @param a Coefficients in Newton form formula. * @param c Centers. * @throws NullArgumentException if any argument is {@code null}. * @throws NoDataException if any array has zero length. * @throws DimensionMismatchException if the size difference between * {@code a} and {@code c} is not equal to 1. */ public PolynomialFunctionNewtonForm(double a[], double c[]) throws NullArgumentException, NoDataException, DimensionMismatchException { verifyInputArray(a, c); this.a = new double[a.length]; this.c = new double[c.length]; System.arraycopy(a, 0, this.a, 0, a.length); System.arraycopy(c, 0, this.c, 0, c.length); coefficientsComputed = false; } /** * Calculate the function value at the given point. * * @param z Point at which the function value is to be computed. * @return the function value. */ public double value(double z) { return evaluate(a, c, z); } /** * {@inheritDoc} * @since 3.1 */ public DerivativeStructure value(final DerivativeStructure t) { verifyInputArray(a, c); final int n = c.length; DerivativeStructure value = new DerivativeStructure(t.getFreeParameters(), t.getOrder(), a[n]); for (int i = n - 1; i >= 0; i--) { value = t.subtract(c[i]).multiply(value).add(a[i]); } return value; } /** * Returns the degree of the polynomial. * * @return the degree of the polynomial */ public int degree() { return c.length; } /** * Returns a copy of coefficients in Newton form formula. *

                            * Changes made to the returned copy will not affect the polynomial.

                            * * @return a fresh copy of coefficients in Newton form formula */ public double[] getNewtonCoefficients() { double[] out = new double[a.length]; System.arraycopy(a, 0, out, 0, a.length); return out; } /** * Returns a copy of the centers array. *

                            * Changes made to the returned copy will not affect the polynomial.

                            * * @return a fresh copy of the centers array. */ public double[] getCenters() { double[] out = new double[c.length]; System.arraycopy(c, 0, out, 0, c.length); return out; } /** * Returns a copy of the coefficients array. *

                            * Changes made to the returned copy will not affect the polynomial.

                            * * @return a fresh copy of the coefficients array. */ public double[] getCoefficients() { if (!coefficientsComputed) { computeCoefficients(); } double[] out = new double[coefficients.length]; System.arraycopy(coefficients, 0, out, 0, coefficients.length); return out; } /** * Evaluate the Newton polynomial using nested multiplication. It is * also called * Horner's Rule and takes O(N) time. * * @param a Coefficients in Newton form formula. * @param c Centers. * @param z Point at which the function value is to be computed. * @return the function value. * @throws NullArgumentException if any argument is {@code null}. * @throws NoDataException if any array has zero length. * @throws DimensionMismatchException if the size difference between * {@code a} and {@code c} is not equal to 1. */ public static double evaluate(double a[], double c[], double z) throws NullArgumentException, DimensionMismatchException, NoDataException { verifyInputArray(a, c); final int n = c.length; double value = a[n]; for (int i = n - 1; i >= 0; i--) { value = a[i] + (z - c[i]) * value; } return value; } /** * Calculate the normal polynomial coefficients given the Newton form. * It also uses nested multiplication but takes O(N^2) time. */ protected void computeCoefficients() { final int n = degree(); coefficients = new double[n+1]; for (int i = 0; i <= n; i++) { coefficients[i] = 0.0; } coefficients[0] = a[n]; for (int i = n-1; i >= 0; i--) { for (int j = n-i; j > 0; j--) { coefficients[j] = coefficients[j-1] - c[i] * coefficients[j]; } coefficients[0] = a[i] - c[i] * coefficients[0]; } coefficientsComputed = true; } /** * Verifies that the input arrays are valid. *

                            * The centers must be distinct for interpolation purposes, but not * for general use. Thus it is not verified here.

                            * * @param a the coefficients in Newton form formula * @param c the centers * @throws NullArgumentException if any argument is {@code null}. * @throws NoDataException if any array has zero length. * @throws DimensionMismatchException if the size difference between * {@code a} and {@code c} is not equal to 1. * @see org.apache.commons.math3.analysis.interpolation.DividedDifferenceInterpolator#computeDividedDifference(double[], * double[]) */ protected static void verifyInputArray(double a[], double c[]) throws NullArgumentException, NoDataException, DimensionMismatchException { MathUtils.checkNotNull(a); MathUtils.checkNotNull(c); if (a.length == 0 || c.length == 0) { throw new NoDataException(LocalizedFormats.EMPTY_POLYNOMIALS_COEFFICIENTS_ARRAY); } if (a.length != c.length + 1) { throw new DimensionMismatchException(LocalizedFormats.ARRAY_SIZES_SHOULD_HAVE_DIFFERENCE_1, a.length, c.length); } } } ././@LongLink100644 0 0 150 12126630646 10255 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/analysis/polynomials/PolynomialsUtils.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/analysis/polynomials/PolynomialsUtils.j100644 1750 1750 41066 12126627705 32446 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.analysis.polynomials; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import org.apache.commons.math3.fraction.BigFraction; import org.apache.commons.math3.util.ArithmeticUtils; import org.apache.commons.math3.util.FastMath; /** * A collection of static methods that operate on or return polynomials. * * @version $Id: PolynomialsUtils.java 1364387 2012-07-22 18:14:11Z tn $ * @since 2.0 */ public class PolynomialsUtils { /** Coefficients for Chebyshev polynomials. */ private static final List CHEBYSHEV_COEFFICIENTS; /** Coefficients for Hermite polynomials. */ private static final List HERMITE_COEFFICIENTS; /** Coefficients for Laguerre polynomials. */ private static final List LAGUERRE_COEFFICIENTS; /** Coefficients for Legendre polynomials. */ private static final List LEGENDRE_COEFFICIENTS; /** Coefficients for Jacobi polynomials. */ private static final Map> JACOBI_COEFFICIENTS; static { // initialize recurrence for Chebyshev polynomials // T0(X) = 1, T1(X) = 0 + 1 * X CHEBYSHEV_COEFFICIENTS = new ArrayList(); CHEBYSHEV_COEFFICIENTS.add(BigFraction.ONE); CHEBYSHEV_COEFFICIENTS.add(BigFraction.ZERO); CHEBYSHEV_COEFFICIENTS.add(BigFraction.ONE); // initialize recurrence for Hermite polynomials // H0(X) = 1, H1(X) = 0 + 2 * X HERMITE_COEFFICIENTS = new ArrayList(); HERMITE_COEFFICIENTS.add(BigFraction.ONE); HERMITE_COEFFICIENTS.add(BigFraction.ZERO); HERMITE_COEFFICIENTS.add(BigFraction.TWO); // initialize recurrence for Laguerre polynomials // L0(X) = 1, L1(X) = 1 - 1 * X LAGUERRE_COEFFICIENTS = new ArrayList(); LAGUERRE_COEFFICIENTS.add(BigFraction.ONE); LAGUERRE_COEFFICIENTS.add(BigFraction.ONE); LAGUERRE_COEFFICIENTS.add(BigFraction.MINUS_ONE); // initialize recurrence for Legendre polynomials // P0(X) = 1, P1(X) = 0 + 1 * X LEGENDRE_COEFFICIENTS = new ArrayList(); LEGENDRE_COEFFICIENTS.add(BigFraction.ONE); LEGENDRE_COEFFICIENTS.add(BigFraction.ZERO); LEGENDRE_COEFFICIENTS.add(BigFraction.ONE); // initialize map for Jacobi polynomials JACOBI_COEFFICIENTS = new HashMap>(); } /** * Private constructor, to prevent instantiation. */ private PolynomialsUtils() { } /** * Create a Chebyshev polynomial of the first kind. *

                            Chebyshev * polynomials of the first kind are orthogonal polynomials. * They can be defined by the following recurrence relations: *

                                 *  T0(X)   = 1
                                 *  T1(X)   = X
                                 *  Tk+1(X) = 2X Tk(X) - Tk-1(X)
                                 * 

                            * @param degree degree of the polynomial * @return Chebyshev polynomial of specified degree */ public static PolynomialFunction createChebyshevPolynomial(final int degree) { return buildPolynomial(degree, CHEBYSHEV_COEFFICIENTS, new RecurrenceCoefficientsGenerator() { private final BigFraction[] coeffs = { BigFraction.ZERO, BigFraction.TWO, BigFraction.ONE }; /** {@inheritDoc} */ public BigFraction[] generate(int k) { return coeffs; } }); } /** * Create a Hermite polynomial. *

                            Hermite * polynomials are orthogonal polynomials. * They can be defined by the following recurrence relations: *

                                 *  H0(X)   = 1
                                 *  H1(X)   = 2X
                                 *  Hk+1(X) = 2X Hk(X) - 2k Hk-1(X)
                                 * 

                            * @param degree degree of the polynomial * @return Hermite polynomial of specified degree */ public static PolynomialFunction createHermitePolynomial(final int degree) { return buildPolynomial(degree, HERMITE_COEFFICIENTS, new RecurrenceCoefficientsGenerator() { /** {@inheritDoc} */ public BigFraction[] generate(int k) { return new BigFraction[] { BigFraction.ZERO, BigFraction.TWO, new BigFraction(2 * k)}; } }); } /** * Create a Laguerre polynomial. *

                            Laguerre * polynomials are orthogonal polynomials. * They can be defined by the following recurrence relations: *

                                 *        L0(X)   = 1
                                 *        L1(X)   = 1 - X
                                 *  (k+1) Lk+1(X) = (2k + 1 - X) Lk(X) - k Lk-1(X)
                                 * 

                            * @param degree degree of the polynomial * @return Laguerre polynomial of specified degree */ public static PolynomialFunction createLaguerrePolynomial(final int degree) { return buildPolynomial(degree, LAGUERRE_COEFFICIENTS, new RecurrenceCoefficientsGenerator() { /** {@inheritDoc} */ public BigFraction[] generate(int k) { final int kP1 = k + 1; return new BigFraction[] { new BigFraction(2 * k + 1, kP1), new BigFraction(-1, kP1), new BigFraction(k, kP1)}; } }); } /** * Create a Legendre polynomial. *

                            Legendre * polynomials are orthogonal polynomials. * They can be defined by the following recurrence relations: *

                                 *        P0(X)   = 1
                                 *        P1(X)   = X
                                 *  (k+1) Pk+1(X) = (2k+1) X Pk(X) - k Pk-1(X)
                                 * 

                            * @param degree degree of the polynomial * @return Legendre polynomial of specified degree */ public static PolynomialFunction createLegendrePolynomial(final int degree) { return buildPolynomial(degree, LEGENDRE_COEFFICIENTS, new RecurrenceCoefficientsGenerator() { /** {@inheritDoc} */ public BigFraction[] generate(int k) { final int kP1 = k + 1; return new BigFraction[] { BigFraction.ZERO, new BigFraction(k + kP1, kP1), new BigFraction(k, kP1)}; } }); } /** * Create a Jacobi polynomial. *

                            Jacobi * polynomials are orthogonal polynomials. * They can be defined by the following recurrence relations: *

                                 *        P0vw(X)   = 1
                                 *        P-1vw(X)  = 0
                                 *  2k(k + v + w)(2k + v + w - 2) Pkvw(X) =
                                 *  (2k + v + w - 1)[(2k + v + w)(2k + v + w - 2) X + v2 - w2] Pk-1vw(X)
                                 *  - 2(k + v - 1)(k + w - 1)(2k + v + w) Pk-2vw(X)
                                 * 

                            * @param degree degree of the polynomial * @param v first exponent * @param w second exponent * @return Jacobi polynomial of specified degree */ public static PolynomialFunction createJacobiPolynomial(final int degree, final int v, final int w) { // select the appropriate list final JacobiKey key = new JacobiKey(v, w); if (!JACOBI_COEFFICIENTS.containsKey(key)) { // allocate a new list for v, w final List list = new ArrayList(); JACOBI_COEFFICIENTS.put(key, list); // Pv,w,0(x) = 1; list.add(BigFraction.ONE); // P1(x) = (v - w) / 2 + (2 + v + w) * X / 2 list.add(new BigFraction(v - w, 2)); list.add(new BigFraction(2 + v + w, 2)); } return buildPolynomial(degree, JACOBI_COEFFICIENTS.get(key), new RecurrenceCoefficientsGenerator() { /** {@inheritDoc} */ public BigFraction[] generate(int k) { k++; final int kvw = k + v + w; final int twoKvw = kvw + k; final int twoKvwM1 = twoKvw - 1; final int twoKvwM2 = twoKvw - 2; final int den = 2 * k * kvw * twoKvwM2; return new BigFraction[] { new BigFraction(twoKvwM1 * (v * v - w * w), den), new BigFraction(twoKvwM1 * twoKvw * twoKvwM2, den), new BigFraction(2 * (k + v - 1) * (k + w - 1) * twoKvw, den) }; } }); } /** Inner class for Jacobi polynomials keys. */ private static class JacobiKey { /** First exponent. */ private final int v; /** Second exponent. */ private final int w; /** Simple constructor. * @param v first exponent * @param w second exponent */ public JacobiKey(final int v, final int w) { this.v = v; this.w = w; } /** Get hash code. * @return hash code */ @Override public int hashCode() { return (v << 16) ^ w; } /** Check if the instance represent the same key as another instance. * @param key other key * @return true if the instance and the other key refer to the same polynomial */ @Override public boolean equals(final Object key) { if ((key == null) || !(key instanceof JacobiKey)) { return false; } final JacobiKey otherK = (JacobiKey) key; return (v == otherK.v) && (w == otherK.w); } } /** * Compute the coefficients of the polynomial Ps(x) * whose values at point {@code x} will be the same as the those from the * original polynomial P(x) when computed at {@code x + shift}. * Thus, if P(x) = Σi ai xi, * then *
                                 *  
                                 *   
                                 *    
                                 *    
                                 *   
                                 *   
                                 *    
                                 *    
                                 *   
                                 *  
                            Ps(x)= Σi bi xi
                            = Σi ai (x + shift)i
                            *
                            * * @param coefficients Coefficients of the original polynomial. * @param shift Shift value. * @return the coefficients bi of the shifted * polynomial. */ public static double[] shift(final double[] coefficients, final double shift) { final int dp1 = coefficients.length; final double[] newCoefficients = new double[dp1]; // Pascal triangle. final int[][] coeff = new int[dp1][dp1]; for (int i = 0; i < dp1; i++){ for(int j = 0; j <= i; j++){ coeff[i][j] = (int) ArithmeticUtils.binomialCoefficient(i, j); } } // First polynomial coefficient. for (int i = 0; i < dp1; i++){ newCoefficients[0] += coefficients[i] * FastMath.pow(shift, i); } // Superior order. final int d = dp1 - 1; for (int i = 0; i < d; i++) { for (int j = i; j < d; j++){ newCoefficients[i + 1] += coeff[j + 1][j - i] * coefficients[j + 1] * FastMath.pow(shift, j - i); } } return newCoefficients; } /** Get the coefficients array for a given degree. * @param degree degree of the polynomial * @param coefficients list where the computed coefficients are stored * @param generator recurrence coefficients generator * @return coefficients array */ private static PolynomialFunction buildPolynomial(final int degree, final List coefficients, final RecurrenceCoefficientsGenerator generator) { final int maxDegree = (int) FastMath.floor(FastMath.sqrt(2 * coefficients.size())) - 1; synchronized (PolynomialsUtils.class) { if (degree > maxDegree) { computeUpToDegree(degree, maxDegree, generator, coefficients); } } // coefficient for polynomial 0 is l [0] // coefficients for polynomial 1 are l [1] ... l [2] (degrees 0 ... 1) // coefficients for polynomial 2 are l [3] ... l [5] (degrees 0 ... 2) // coefficients for polynomial 3 are l [6] ... l [9] (degrees 0 ... 3) // coefficients for polynomial 4 are l[10] ... l[14] (degrees 0 ... 4) // coefficients for polynomial 5 are l[15] ... l[20] (degrees 0 ... 5) // coefficients for polynomial 6 are l[21] ... l[27] (degrees 0 ... 6) // ... final int start = degree * (degree + 1) / 2; final double[] a = new double[degree + 1]; for (int i = 0; i <= degree; ++i) { a[i] = coefficients.get(start + i).doubleValue(); } // build the polynomial return new PolynomialFunction(a); } /** Compute polynomial coefficients up to a given degree. * @param degree maximal degree * @param maxDegree current maximal degree * @param generator recurrence coefficients generator * @param coefficients list where the computed coefficients should be appended */ private static void computeUpToDegree(final int degree, final int maxDegree, final RecurrenceCoefficientsGenerator generator, final List coefficients) { int startK = (maxDegree - 1) * maxDegree / 2; for (int k = maxDegree; k < degree; ++k) { // start indices of two previous polynomials Pk(X) and Pk-1(X) int startKm1 = startK; startK += k; // Pk+1(X) = (a[0] + a[1] X) Pk(X) - a[2] Pk-1(X) BigFraction[] ai = generator.generate(k); BigFraction ck = coefficients.get(startK); BigFraction ckm1 = coefficients.get(startKm1); // degree 0 coefficient coefficients.add(ck.multiply(ai[0]).subtract(ckm1.multiply(ai[2]))); // degree 1 to degree k-1 coefficients for (int i = 1; i < k; ++i) { final BigFraction ckPrev = ck; ck = coefficients.get(startK + i); ckm1 = coefficients.get(startKm1 + i); coefficients.add(ck.multiply(ai[0]).add(ckPrev.multiply(ai[1])).subtract(ckm1.multiply(ai[2]))); } // degree k coefficient final BigFraction ckPrev = ck; ck = coefficients.get(startK + k); coefficients.add(ck.multiply(ai[0]).add(ckPrev.multiply(ai[1]))); // degree k+1 coefficient coefficients.add(ck.multiply(ai[1])); } } /** Interface for recurrence coefficients generation. */ private interface RecurrenceCoefficientsGenerator { /** * Generate recurrence coefficients. * @param k highest degree of the polynomials used in the recurrence * @return an array of three coefficients such that * Pk+1(X) = (a[0] + a[1] X) Pk(X) - a[2] Pk-1(X) */ BigFraction[] generate(int k); } } ././@LongLink100644 0 0 160 12126630646 10256 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/analysis/polynomials/PolynomialSplineFunction.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/analysis/polynomials/PolynomialSplineFu100644 1750 1750 22365 12126627705 32461 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.analysis.polynomials; import java.util.Arrays; import org.apache.commons.math3.util.MathArrays; import org.apache.commons.math3.analysis.DifferentiableUnivariateFunction; import org.apache.commons.math3.analysis.UnivariateFunction; import org.apache.commons.math3.analysis.differentiation.DerivativeStructure; import org.apache.commons.math3.analysis.differentiation.UnivariateDifferentiableFunction; import org.apache.commons.math3.exception.NonMonotonicSequenceException; import org.apache.commons.math3.exception.OutOfRangeException; import org.apache.commons.math3.exception.NumberIsTooSmallException; import org.apache.commons.math3.exception.DimensionMismatchException; import org.apache.commons.math3.exception.NullArgumentException; import org.apache.commons.math3.exception.util.LocalizedFormats; /** * Represents a polynomial spline function. *

                            * A polynomial spline function consists of a set of * interpolating polynomials and an ascending array of domain * knot points, determining the intervals over which the spline function * is defined by the constituent polynomials. The polynomials are assumed to * have been computed to match the values of another function at the knot * points. The value consistency constraints are not currently enforced by * PolynomialSplineFunction itself, but are assumed to hold among * the polynomials and knot points passed to the constructor.

                            *

                            * N.B.: The polynomials in the polynomials property must be * centered on the knot points to compute the spline function values. * See below.

                            *

                            * The domain of the polynomial spline function is * [smallest knot, largest knot]. Attempts to evaluate the * function at values outside of this range generate IllegalArgumentExceptions. *

                            *

                            * The value of the polynomial spline function for an argument x * is computed as follows: *

                              *
                            1. The knot array is searched to find the segment to which x * belongs. If x is less than the smallest knot point or greater * than the largest one, an IllegalArgumentException * is thrown.
                            2. *
                            3. Let j be the index of the largest knot point that is less * than or equal to x. The value returned is
                              * polynomials[j](x - knot[j])

                            * * @version $Id: PolynomialSplineFunction.java 1455194 2013-03-11 15:45:54Z luc $ */ public class PolynomialSplineFunction implements UnivariateDifferentiableFunction, DifferentiableUnivariateFunction { /** * Spline segment interval delimiters (knots). * Size is n + 1 for n segments. */ private final double knots[]; /** * The polynomial functions that make up the spline. The first element * determines the value of the spline over the first subinterval, the * second over the second, etc. Spline function values are determined by * evaluating these functions at {@code (x - knot[i])} where i is the * knot segment to which x belongs. */ private final PolynomialFunction polynomials[]; /** * Number of spline segments. It is equal to the number of polynomials and * to the number of partition points - 1. */ private final int n; /** * Construct a polynomial spline function with the given segment delimiters * and interpolating polynomials. * The constructor copies both arrays and assigns the copies to the knots * and polynomials properties, respectively. * * @param knots Spline segment interval delimiters. * @param polynomials Polynomial functions that make up the spline. * @throws NullArgumentException if either of the input arrays is {@code null}. * @throws NumberIsTooSmallException if knots has length less than 2. * @throws DimensionMismatchException if {@code polynomials.length != knots.length - 1}. * @throws NonMonotonicSequenceException if the {@code knots} array is not strictly increasing. * */ public PolynomialSplineFunction(double knots[], PolynomialFunction polynomials[]) throws NullArgumentException, NumberIsTooSmallException, DimensionMismatchException, NonMonotonicSequenceException{ if (knots == null || polynomials == null) { throw new NullArgumentException(); } if (knots.length < 2) { throw new NumberIsTooSmallException(LocalizedFormats.NOT_ENOUGH_POINTS_IN_SPLINE_PARTITION, 2, knots.length, false); } if (knots.length - 1 != polynomials.length) { throw new DimensionMismatchException(polynomials.length, knots.length); } MathArrays.checkOrder(knots); this.n = knots.length -1; this.knots = new double[n + 1]; System.arraycopy(knots, 0, this.knots, 0, n + 1); this.polynomials = new PolynomialFunction[n]; System.arraycopy(polynomials, 0, this.polynomials, 0, n); } /** * Compute the value for the function. * See {@link PolynomialSplineFunction} for details on the algorithm for * computing the value of the function. * * @param v Point for which the function value should be computed. * @return the value. * @throws OutOfRangeException if {@code v} is outside of the domain of the * spline function (smaller than the smallest knot point or larger than the * largest knot point). */ public double value(double v) { if (v < knots[0] || v > knots[n]) { throw new OutOfRangeException(v, knots[0], knots[n]); } int i = Arrays.binarySearch(knots, v); if (i < 0) { i = -i - 2; } // This will handle the case where v is the last knot value // There are only n-1 polynomials, so if v is the last knot // then we will use the last polynomial to calculate the value. if ( i >= polynomials.length ) { i--; } return polynomials[i].value(v - knots[i]); } /** * Get the derivative of the polynomial spline function. * * @return the derivative function. */ public UnivariateFunction derivative() { return polynomialSplineDerivative(); } /** * Get the derivative of the polynomial spline function. * * @return the derivative function. */ public PolynomialSplineFunction polynomialSplineDerivative() { PolynomialFunction derivativePolynomials[] = new PolynomialFunction[n]; for (int i = 0; i < n; i++) { derivativePolynomials[i] = polynomials[i].polynomialDerivative(); } return new PolynomialSplineFunction(knots, derivativePolynomials); } /** {@inheritDoc} * @since 3.1 */ public DerivativeStructure value(final DerivativeStructure t) { final double t0 = t.getValue(); if (t0 < knots[0] || t0 > knots[n]) { throw new OutOfRangeException(t0, knots[0], knots[n]); } int i = Arrays.binarySearch(knots, t0); if (i < 0) { i = -i - 2; } // This will handle the case where t is the last knot value // There are only n-1 polynomials, so if t is the last knot // then we will use the last polynomial to calculate the value. if ( i >= polynomials.length ) { i--; } return polynomials[i].value(t.subtract(knots[i])); } /** * Get the number of spline segments. * It is also the number of polynomials and the number of knot points - 1. * * @return the number of spline segments. */ public int getN() { return n; } /** * Get a copy of the interpolating polynomials array. * It returns a fresh copy of the array. Changes made to the copy will * not affect the polynomials property. * * @return the interpolating polynomials. */ public PolynomialFunction[] getPolynomials() { PolynomialFunction p[] = new PolynomialFunction[n]; System.arraycopy(polynomials, 0, p, 0, n); return p; } /** * Get an array copy of the knot points. * It returns a fresh copy of the array. Changes made to the copy * will not affect the knots property. * * @return the knot points. */ public double[] getKnots() { double out[] = new double[n + 1]; System.arraycopy(knots, 0, out, 0, n + 1); return out; } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/analysis/MultivariateFunction.java100644 1750 1750 3534 12126627707 31375 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.analysis; /** * An interface representing a multivariate real function. * * @version $Id: MultivariateFunction.java 1364387 2012-07-22 18:14:11Z tn $ * @since 2.0 */ public interface MultivariateFunction { /** * Compute the value for the function at the given point. * * @param point Point at which the function must be evaluated. * @return the function value for the given point. * @throws org.apache.commons.math3.exception.DimensionMismatchException * if the parameter's dimension is wrong for the function being evaluated. * @throws org.apache.commons.math3.exception.MathIllegalArgumentException * when the activated method itself can ascertain that preconditions, * specified in the API expressed at the level of the activated method, * have been violated. In the vast majority of cases where Commons Math * throws this exception, it is the result of argument checking of actual * parameters immediately passed to a method. */ double value(double[] point); } ././@LongLink100644 0 0 150 12126630646 10255 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/analysis/differentiation/package-info.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/analysis/differentiation/package-info.j100644 1750 1750 4104 12126627706 32220 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ /** * *

                            * This package holds the main interfaces and basic building block classes * dealing with differentiation. * The core class is {@link org.apache.commons.math3.analysis.differentiation.DerivativeStructure * DerivativeStructure} which holds the value and the differentials of a function. This class * handles some arbitrary number of free parameters and arbitrary differentiation order. It is used * both as the input and the output type for the {@link * org.apache.commons.math3.analysis.differentiation.UnivariateDifferentiableFunction * UnivariateDifferentiableFunction} interface. Any differentiable function should implement this * interface. *

                            *

                            * The {@link org.apache.commons.math3.analysis.differentiation.UnivariateFunctionDifferentiator * UnivariateFunctionDifferentiator} interface defines a way to differentiate a simple {@link * org.apache.commons.math3.analysis.UnivariateFunction UnivariateFunction} and get a {@link * org.apache.commons.math3.analysis.differentiation.UnivariateDifferentiableFunction * UnivariateDifferentiableFunction}. *

                            *

                            * Similar interfaces also exist for multivariate functions and for vector or matrix valued functions. *

                            * */ package org.apache.commons.math3.analysis.differentiation; ././@LongLink100644 0 0 202 12126630646 10253 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/analysis/differentiation/UnivariateMatrixFunctionDifferentiator.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/analysis/differentiation/UnivariateMatr100644 1750 1750 2766 12126627706 32413 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.analysis.differentiation; import org.apache.commons.math3.analysis.UnivariateMatrixFunction; /** Interface defining the function differentiation operation. * @version $Id: UnivariateMatrixFunctionDifferentiator.java 1416643 2012-12-03 19:37:14Z tn $ * @since 3.1 */ public interface UnivariateMatrixFunctionDifferentiator { /** Create an implementation of a {@link UnivariateDifferentiableMatrixFunction * differential} from a regular {@link UnivariateMatrixFunction matrix function}. * @param function function to differentiate * @return differential function */ UnivariateDifferentiableMatrixFunction differentiate(UnivariateMatrixFunction function); } ././@LongLink100644 0 0 202 12126630646 10253 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/analysis/differentiation/UnivariateVectorFunctionDifferentiator.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/analysis/differentiation/UnivariateVect100644 1750 1750 2766 12126627706 32411 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.analysis.differentiation; import org.apache.commons.math3.analysis.UnivariateVectorFunction; /** Interface defining the function differentiation operation. * @version $Id: UnivariateVectorFunctionDifferentiator.java 1416643 2012-12-03 19:37:14Z tn $ * @since 3.1 */ public interface UnivariateVectorFunctionDifferentiator { /** Create an implementation of a {@link UnivariateDifferentiableVectorFunction * differential} from a regular {@link UnivariateVectorFunction vector function}. * @param function function to differentiate * @return differential function */ UnivariateDifferentiableVectorFunction differentiate(UnivariateVectorFunction function); } ././@LongLink100644 0 0 202 12126630646 10253 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/analysis/differentiation/UnivariateDifferentiableMatrixFunction.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/analysis/differentiation/UnivariateDiff100644 1750 1750 3355 12126627706 32353 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.analysis.differentiation; import org.apache.commons.math3.analysis.UnivariateMatrixFunction; import org.apache.commons.math3.exception.MathIllegalArgumentException; /** * Extension of {@link UnivariateMatrixFunction} representing a univariate differentiable matrix function. * * @version $Id: UnivariateDifferentiableMatrixFunction.java 1462496 2013-03-29 14:56:08Z psteitz $ * @since 3.1 */ public interface UnivariateDifferentiableMatrixFunction extends UnivariateMatrixFunction { /** * Compute the value for the function. * @param x the point for which the function value should be computed * @return the value * @exception MathIllegalArgumentException if {@code x} does not * satisfy the function's constraints (argument out of bound, or unsupported * derivative order for example) */ DerivativeStructure[][] value(DerivativeStructure x) throws MathIllegalArgumentException; } ././@LongLink100644 0 0 174 12126630646 10263 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/analysis/differentiation/UnivariateDifferentiableFunction.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/analysis/differentiation/UnivariateDiff100644 1750 1750 3711 12126627706 32347 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.analysis.differentiation; import org.apache.commons.math3.analysis.UnivariateFunction; import org.apache.commons.math3.exception.DimensionMismatchException; /** Interface for univariate functions derivatives. *

                            This interface represents a simple function which computes * both the value and the first derivative of a mathematical function. * The derivative is computed with respect to the input variable.

                            * @see UnivariateDifferentiableFunction * @see UnivariateFunctionDifferentiator * @since 3.1 * @version $Id: UnivariateDifferentiableFunction.java 1462496 2013-03-29 14:56:08Z psteitz $ */ public interface UnivariateDifferentiableFunction extends UnivariateFunction { /** Simple mathematical function. *

                            {@link UnivariateDifferentiableFunction} classes compute both the * value and the first derivative of the function.

                            * @param t function input value * @return function result * @exception DimensionMismatchException if t is inconsistent with the * function's free parameters or order */ DerivativeStructure value(DerivativeStructure t) throws DimensionMismatchException; } ././@LongLink100644 0 0 146 12126630646 10262 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/analysis/differentiation/DSCompiler.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/analysis/differentiation/DSCompiler.jav100644 1750 1750 225713 12126627706 32277 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.analysis.differentiation; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.concurrent.atomic.AtomicReference; import org.apache.commons.math3.exception.DimensionMismatchException; import org.apache.commons.math3.exception.MathArithmeticException; import org.apache.commons.math3.exception.MathInternalError; import org.apache.commons.math3.exception.NotPositiveException; import org.apache.commons.math3.exception.NumberIsTooLargeException; import org.apache.commons.math3.util.ArithmeticUtils; import org.apache.commons.math3.util.FastMath; import org.apache.commons.math3.util.MathArrays; /** Class holding "compiled" computation rules for derivative structures. *

                            This class implements the computation rules described in Dan Kalman's paper Doubly * Recursive Multivariate Automatic Differentiation, Mathematics Magazine, vol. 75, * no. 3, June 2002. However, in order to avoid performances bottlenecks, the recursive * rules are "compiled" once in an unfold form. This class does this recursion unrolling * and stores the computation rules as simple loops with pre-computed indirection arrays.

                            *

                            * This class maps all derivative computation into single dimension arrays that hold the * value and partial derivatives. The class does not hold these arrays, which remains under * the responsibility of the caller. For each combination of number of free parameters and * derivation order, only one compiler is necessary, and this compiler will be used to * perform computations on all arrays provided to it, which can represent hundreds or * thousands of different parameters kept together with all theur partial derivatives. *

                            *

                            * The arrays on which compilers operate contain only the partial derivatives together * with the 0th derivative, i.e. the value. The partial derivatives are stored in * a compiler-specific order, which can be retrieved using methods {@link * #getPartialDerivativeIndex(int...) getPartialDerivativeIndex} and {@link * #getPartialDerivativeOrders(int)}. The value is guaranteed to be stored as the first element * (i.e. the {@link #getPartialDerivativeIndex(int...) getPartialDerivativeIndex} method returns * 0 when called with 0 for all derivation orders and {@link #getPartialDerivativeOrders(int) * getPartialDerivativeOrders} returns an array filled with 0 when called with 0 as the index). *

                            *

                            * Note that the ordering changes with number of parameters and derivation order. For example * given 2 parameters x and y, df/dy is stored at index 2 when derivation order is set to 1 (in * this case the array has three elements: f, df/dx and df/dy). If derivation order is set to * 2, then df/dy will be stored at index 3 (in this case the array has six elements: f, df/dx, * df/dxdx, df/dy, df/dxdy and df/dydy). *

                            *

                            * Given this structure, users can perform some simple operations like adding, subtracting * or multiplying constants and negating the elements by themselves, knowing if they want to * mutate their array or create a new array. These simple operations are not provided by * the compiler. The compiler provides only the more complex operations between several arrays. *

                            *

                            This class is mainly used as the engine for scalar variable {@link DerivativeStructure}. * It can also be used directly to hold several variables in arrays for more complex data * structures. User can for example store a vector of n variables depending on three x, y * and z free parameters in one array as follows: *

                             *   // parameter 0 is x, parameter 1 is y, parameter 2 is z
                             *   int parameters = 3;
                             *   DSCompiler compiler = DSCompiler.getCompiler(parameters, order);
                             *   int size = compiler.getSize();
                             *
                             *   // pack all elements in a single array
                             *   double[] array = new double[n * size];
                             *   for (int i = 0; i < n; ++i) {
                             *
                             *     // we know value is guaranteed to be the first element
                             *     array[i * size] = v[i];
                             *
                             *     // we don't know where first derivatives are stored, so we ask the compiler
                             *     array[i * size + compiler.getPartialDerivativeIndex(1, 0, 0) = dvOnDx[i][0];
                             *     array[i * size + compiler.getPartialDerivativeIndex(0, 1, 0) = dvOnDy[i][0];
                             *     array[i * size + compiler.getPartialDerivativeIndex(0, 0, 1) = dvOnDz[i][0];
                             *
                             *     // we let all higher order derivatives set to 0
                             *
                             *   }
                             * 
                            * Then in another function, user can perform some operations on all elements stored * in the single array, such as a simple product of all variables: *
                             *   // compute the product of all elements
                             *   double[] product = new double[size];
                             *   prod[0] = 1.0;
                             *   for (int i = 0; i < n; ++i) {
                             *     double[] tmp = product.clone();
                             *     compiler.multiply(tmp, 0, array, i * size, product, 0);
                             *   }
                             *
                             *   // value
                             *   double p = product[0];
                             *
                             *   // first derivatives
                             *   double dPdX = product[compiler.getPartialDerivativeIndex(1, 0, 0)];
                             *   double dPdY = product[compiler.getPartialDerivativeIndex(0, 1, 0)];
                             *   double dPdZ = product[compiler.getPartialDerivativeIndex(0, 0, 1)];
                             *
                             *   // cross derivatives (assuming order was at least 2)
                             *   double dPdXdX = product[compiler.getPartialDerivativeIndex(2, 0, 0)];
                             *   double dPdXdY = product[compiler.getPartialDerivativeIndex(1, 1, 0)];
                             *   double dPdXdZ = product[compiler.getPartialDerivativeIndex(1, 0, 1)];
                             *   double dPdYdY = product[compiler.getPartialDerivativeIndex(0, 2, 0)];
                             *   double dPdYdZ = product[compiler.getPartialDerivativeIndex(0, 1, 1)];
                             *   double dPdZdZ = product[compiler.getPartialDerivativeIndex(0, 0, 2)];
                             * 

                            * @see DerivativeStructure * @version $Id: DSCompiler.java 1462503 2013-03-29 15:48:27Z luc $ * @since 3.1 */ public class DSCompiler { /** Array of all compilers created so far. */ private static AtomicReference compilers = new AtomicReference(null); /** Number of free parameters. */ private final int parameters; /** Derivation order. */ private final int order; /** Number of partial derivatives (including the single 0 order derivative element). */ private final int[][] sizes; /** Indirection array for partial derivatives. */ private final int[][] derivativesIndirection; /** Indirection array of the lower derivative elements. */ private final int[] lowerIndirection; /** Indirection arrays for multiplication. */ private final int[][][] multIndirection; /** Indirection arrays for function composition. */ private final int[][][] compIndirection; /** Private constructor, reserved for the factory method {@link #getCompiler(int, int)}. * @param parameters number of free parameters * @param order derivation order * @param valueCompiler compiler for the value part * @param derivativeCompiler compiler for the derivative part * @throws NumberIsTooLargeException if order is too large */ private DSCompiler(final int parameters, final int order, final DSCompiler valueCompiler, final DSCompiler derivativeCompiler) throws NumberIsTooLargeException { this.parameters = parameters; this.order = order; this.sizes = compileSizes(parameters, order, valueCompiler); this.derivativesIndirection = compileDerivativesIndirection(parameters, order, valueCompiler, derivativeCompiler); this.lowerIndirection = compileLowerIndirection(parameters, order, valueCompiler, derivativeCompiler); this.multIndirection = compileMultiplicationIndirection(parameters, order, valueCompiler, derivativeCompiler, lowerIndirection); this.compIndirection = compileCompositionIndirection(parameters, order, valueCompiler, derivativeCompiler, sizes, derivativesIndirection); } /** Get the compiler for number of free parameters and order. * @param parameters number of free parameters * @param order derivation order * @return cached rules set * @throws NumberIsTooLargeException if order is too large */ public static DSCompiler getCompiler(int parameters, int order) throws NumberIsTooLargeException { // get the cached compilers final DSCompiler[][] cache = compilers.get(); if (cache != null && cache.length > parameters && cache[parameters].length > order && cache[parameters][order] != null) { // the compiler has already been created return cache[parameters][order]; } // we need to create more compilers final int maxParameters = FastMath.max(parameters, cache == null ? 0 : cache.length); final int maxOrder = FastMath.max(order, cache == null ? 0 : cache[0].length); final DSCompiler[][] newCache = new DSCompiler[maxParameters + 1][maxOrder + 1]; if (cache != null) { // preserve the already created compilers for (int i = 0; i < cache.length; ++i) { System.arraycopy(cache[i], 0, newCache[i], 0, cache[i].length); } } // create the array in increasing diagonal order for (int diag = 0; diag <= parameters + order; ++diag) { for (int o = FastMath.max(0, diag - parameters); o <= FastMath.min(order, diag); ++o) { final int p = diag - o; if (newCache[p][o] == null) { final DSCompiler valueCompiler = (p == 0) ? null : newCache[p - 1][o]; final DSCompiler derivativeCompiler = (o == 0) ? null : newCache[p][o - 1]; newCache[p][o] = new DSCompiler(p, o, valueCompiler, derivativeCompiler); } } } // atomically reset the cached compilers array compilers.compareAndSet(cache, newCache); return newCache[parameters][order]; } /** Compile the sizes array. * @param parameters number of free parameters * @param order derivation order * @param valueCompiler compiler for the value part * @return sizes array */ private static int[][] compileSizes(final int parameters, final int order, final DSCompiler valueCompiler) { final int[][] sizes = new int[parameters + 1][order + 1]; if (parameters == 0) { Arrays.fill(sizes[0], 1); } else { System.arraycopy(valueCompiler.sizes, 0, sizes, 0, parameters); sizes[parameters][0] = 1; for (int i = 0; i < order; ++i) { sizes[parameters][i + 1] = sizes[parameters][i] + sizes[parameters - 1][i + 1]; } } return sizes; } /** Compile the derivatives indirection array. * @param parameters number of free parameters * @param order derivation order * @param valueCompiler compiler for the value part * @param derivativeCompiler compiler for the derivative part * @return derivatives indirection array */ private static int[][] compileDerivativesIndirection(final int parameters, final int order, final DSCompiler valueCompiler, final DSCompiler derivativeCompiler) { if (parameters == 0 || order == 0) { return new int[1][parameters]; } final int vSize = valueCompiler.derivativesIndirection.length; final int dSize = derivativeCompiler.derivativesIndirection.length; final int[][] derivativesIndirection = new int[vSize + dSize][parameters]; // set up the indices for the value part for (int i = 0; i < vSize; ++i) { // copy the first indices, the last one remaining set to 0 System.arraycopy(valueCompiler.derivativesIndirection[i], 0, derivativesIndirection[i], 0, parameters - 1); } // set up the indices for the derivative part for (int i = 0; i < dSize; ++i) { // copy the indices System.arraycopy(derivativeCompiler.derivativesIndirection[i], 0, derivativesIndirection[vSize + i], 0, parameters); // increment the derivation order for the last parameter derivativesIndirection[vSize + i][parameters - 1]++; } return derivativesIndirection; } /** Compile the lower derivatives indirection array. *

                            * This indirection array contains the indices of all elements * except derivatives for last derivation order. *

                            * @param parameters number of free parameters * @param order derivation order * @param valueCompiler compiler for the value part * @param derivativeCompiler compiler for the derivative part * @return lower derivatives indirection array */ private static int[] compileLowerIndirection(final int parameters, final int order, final DSCompiler valueCompiler, final DSCompiler derivativeCompiler) { if (parameters == 0 || order <= 1) { return new int[] { 0 }; } // this is an implementation of definition 6 in Dan Kalman's paper. final int vSize = valueCompiler.lowerIndirection.length; final int dSize = derivativeCompiler.lowerIndirection.length; final int[] lowerIndirection = new int[vSize + dSize]; System.arraycopy(valueCompiler.lowerIndirection, 0, lowerIndirection, 0, vSize); for (int i = 0; i < dSize; ++i) { lowerIndirection[vSize + i] = valueCompiler.getSize() + derivativeCompiler.lowerIndirection[i]; } return lowerIndirection; } /** Compile the multiplication indirection array. *

                            * This indirection array contains the indices of all pairs of elements * involved when computing a multiplication. This allows a straightforward * loop-based multiplication (see {@link #multiply(double[], int, double[], int, double[], int)}). *

                            * @param parameters number of free parameters * @param order derivation order * @param valueCompiler compiler for the value part * @param derivativeCompiler compiler for the derivative part * @param lowerIndirection lower derivatives indirection array * @return multiplication indirection array */ private static int[][][] compileMultiplicationIndirection(final int parameters, final int order, final DSCompiler valueCompiler, final DSCompiler derivativeCompiler, final int[] lowerIndirection) { if ((parameters == 0) || (order == 0)) { return new int[][][] { { { 1, 0, 0 } } }; } // this is an implementation of definition 3 in Dan Kalman's paper. final int vSize = valueCompiler.multIndirection.length; final int dSize = derivativeCompiler.multIndirection.length; final int[][][] multIndirection = new int[vSize + dSize][][]; System.arraycopy(valueCompiler.multIndirection, 0, multIndirection, 0, vSize); for (int i = 0; i < dSize; ++i) { final int[][] dRow = derivativeCompiler.multIndirection[i]; List row = new ArrayList(); for (int j = 0; j < dRow.length; ++j) { row.add(new int[] { dRow[j][0], lowerIndirection[dRow[j][1]], vSize + dRow[j][2] }); row.add(new int[] { dRow[j][0], vSize + dRow[j][1], lowerIndirection[dRow[j][2]] }); } // combine terms with similar derivation orders final List combined = new ArrayList(row.size()); for (int j = 0; j < row.size(); ++j) { final int[] termJ = row.get(j); if (termJ[0] > 0) { for (int k = j + 1; k < row.size(); ++k) { final int[] termK = row.get(k); if (termJ[1] == termK[1] && termJ[2] == termK[2]) { // combine termJ and termK termJ[0] += termK[0]; // make sure we will skip termK later on in the outer loop termK[0] = 0; } } combined.add(termJ); } } multIndirection[vSize + i] = combined.toArray(new int[combined.size()][]); } return multIndirection; } /** Compile the function composition indirection array. *

                            * This indirection array contains the indices of all sets of elements * involved when computing a composition. This allows a straightforward * loop-based composition (see {@link #compose(double[], int, double[], double[], int)}). *

                            * @param parameters number of free parameters * @param order derivation order * @param valueCompiler compiler for the value part * @param derivativeCompiler compiler for the derivative part * @param sizes sizes array * @param derivativesIndirection derivatives indirection array * @return multiplication indirection array * @throws NumberIsTooLargeException if order is too large */ private static int[][][] compileCompositionIndirection(final int parameters, final int order, final DSCompiler valueCompiler, final DSCompiler derivativeCompiler, final int[][] sizes, final int[][] derivativesIndirection) throws NumberIsTooLargeException { if ((parameters == 0) || (order == 0)) { return new int[][][] { { { 1, 0 } } }; } final int vSize = valueCompiler.compIndirection.length; final int dSize = derivativeCompiler.compIndirection.length; final int[][][] compIndirection = new int[vSize + dSize][][]; // the composition rules from the value part can be reused as is System.arraycopy(valueCompiler.compIndirection, 0, compIndirection, 0, vSize); // the composition rules for the derivative part are deduced by // differentiation the rules from the underlying compiler once // with respect to the parameter this compiler handles and the // underlying one did not handle for (int i = 0; i < dSize; ++i) { List row = new ArrayList(); for (int[] term : derivativeCompiler.compIndirection[i]) { // handle term p * f_k(g(x)) * g_l1(x) * g_l2(x) * ... * g_lp(x) // derive the first factor in the term: f_k with respect to new parameter int[] derivedTermF = new int[term.length + 1]; derivedTermF[0] = term[0]; // p derivedTermF[1] = term[1] + 1; // f_(k+1) int[] orders = new int[parameters]; orders[parameters - 1] = 1; derivedTermF[term.length] = getPartialDerivativeIndex(parameters, order, sizes, orders); // g_1 for (int j = 2; j < term.length; ++j) { // convert the indices as the mapping for the current order // is different from the mapping with one less order derivedTermF[j] = convertIndex(term[j], parameters, derivativeCompiler.derivativesIndirection, parameters, order, sizes); } Arrays.sort(derivedTermF, 2, derivedTermF.length); row.add(derivedTermF); // derive the various g_l for (int l = 2; l < term.length; ++l) { int[] derivedTermG = new int[term.length]; derivedTermG[0] = term[0]; derivedTermG[1] = term[1]; for (int j = 2; j < term.length; ++j) { // convert the indices as the mapping for the current order // is different from the mapping with one less order derivedTermG[j] = convertIndex(term[j], parameters, derivativeCompiler.derivativesIndirection, parameters, order, sizes); if (j == l) { // derive this term System.arraycopy(derivativesIndirection[derivedTermG[j]], 0, orders, 0, parameters); orders[parameters - 1]++; derivedTermG[j] = getPartialDerivativeIndex(parameters, order, sizes, orders); } } Arrays.sort(derivedTermG, 2, derivedTermG.length); row.add(derivedTermG); } } // combine terms with similar derivation orders final List combined = new ArrayList(row.size()); for (int j = 0; j < row.size(); ++j) { final int[] termJ = row.get(j); if (termJ[0] > 0) { for (int k = j + 1; k < row.size(); ++k) { final int[] termK = row.get(k); boolean equals = termJ.length == termK.length; for (int l = 1; equals && l < termJ.length; ++l) { equals &= termJ[l] == termK[l]; } if (equals) { // combine termJ and termK termJ[0] += termK[0]; // make sure we will skip termK later on in the outer loop termK[0] = 0; } } combined.add(termJ); } } compIndirection[vSize + i] = combined.toArray(new int[combined.size()][]); } return compIndirection; } /** Get the index of a partial derivative in the array. *

                            * If all orders are set to 0, then the 0th order derivative * is returned, which is the value of the function. *

                            *

                            The indices of derivatives are between 0 and {@link #getSize() getSize()} - 1. * Their specific order is fixed for a given compiler, but otherwise not * publicly specified. There are however some simple cases which have guaranteed * indices: *

                            *
                              *
                            • the index of 0th order derivative is always 0
                            • *
                            • if there is only 1 {@link #getFreeParameters() free parameter}, then the * derivatives are sorted in increasing derivation order (i.e. f at index 0, df/dp * at index 1, d2f/dp2 at index 2 ... * dkf/dpk at index k),
                            • *
                            • if the {@link #getOrder() derivation order} is 1, then the derivatives * are sorted in increasing free parameter order (i.e. f at index 0, df/dx1 * at index 1, df/dx2 at index 2 ... df/dxk at index k),
                            • *
                            • all other cases are not publicly specified
                            • *
                            *

                            * This method is the inverse of method {@link #getPartialDerivativeOrders(int)} *

                            * @param orders derivation orders with respect to each parameter * @return index of the partial derivative * @exception DimensionMismatchException if the numbers of parameters does not * match the instance * @exception NumberIsTooLargeException if sum of derivation orders is larger * than the instance limits * @see #getPartialDerivativeOrders(int) */ public int getPartialDerivativeIndex(final int ... orders) throws DimensionMismatchException, NumberIsTooLargeException { // safety check if (orders.length != getFreeParameters()) { throw new DimensionMismatchException(orders.length, getFreeParameters()); } return getPartialDerivativeIndex(parameters, order, sizes, orders); } /** Get the index of a partial derivative in an array. * @param parameters number of free parameters * @param order derivation order * @param sizes sizes array * @param orders derivation orders with respect to each parameter * (the lenght of this array must match the number of parameters) * @return index of the partial derivative * @exception NumberIsTooLargeException if sum of derivation orders is larger * than the instance limits */ private static int getPartialDerivativeIndex(final int parameters, final int order, final int[][] sizes, final int ... orders) throws NumberIsTooLargeException { // the value is obtained by diving into the recursive Dan Kalman's structure // this is theorem 2 of his paper, with recursion replaced by iteration int index = 0; int m = order; int ordersSum = 0; for (int i = parameters - 1; i >= 0; --i) { // derivative order for current free parameter int derivativeOrder = orders[i]; // safety check ordersSum += derivativeOrder; if (ordersSum > order) { throw new NumberIsTooLargeException(ordersSum, order, true); } while (derivativeOrder-- > 0) { // as long as we differentiate according to current free parameter, // we have to skip the value part and dive into the derivative part // so we add the size of the value part to the base index index += sizes[i][m--]; } } return index; } /** Convert an index from one (parameters, order) structure to another. * @param index index of a partial derivative in source derivative structure * @param srcP number of free parameters in source derivative structure * @param srcDerivativesIndirection derivatives indirection array for the source * derivative structure * @param destP number of free parameters in destination derivative structure * @param destO derivation order in destination derivative structure * @param destSizes sizes array for the destination derivative structure * @return index of the partial derivative with the same characteristics * in destination derivative structure * @throws NumberIsTooLargeException if order is too large */ private static int convertIndex(final int index, final int srcP, final int[][] srcDerivativesIndirection, final int destP, final int destO, final int[][] destSizes) throws NumberIsTooLargeException { int[] orders = new int[destP]; System.arraycopy(srcDerivativesIndirection[index], 0, orders, 0, FastMath.min(srcP, destP)); return getPartialDerivativeIndex(destP, destO, destSizes, orders); } /** Get the derivation orders for a specific index in the array. *

                            * This method is the inverse of {@link #getPartialDerivativeIndex(int...)}. *

                            * @param index of the partial derivative * @return orders derivation orders with respect to each parameter * @see #getPartialDerivativeIndex(int...) */ public int[] getPartialDerivativeOrders(final int index) { return derivativesIndirection[index]; } /** Get the number of free parameters. * @return number of free parameters */ public int getFreeParameters() { return parameters; } /** Get the derivation order. * @return derivation order */ public int getOrder() { return order; } /** Get the array size required for holding partial derivatives data. *

                            * This number includes the single 0 order derivative element, which is * guaranteed to be stored in the first element of the array. *

                            * @return array size required for holding partial derivatives data */ public int getSize() { return sizes[parameters][order]; } /** Compute linear combination. * The derivative structure built will be a1 * ds1 + a2 * ds2 * @param a1 first scale factor * @param c1 first base (unscaled) component * @param offset1 offset of first operand in its array * @param a2 second scale factor * @param c2 second base (unscaled) component * @param offset2 offset of second operand in its array * @param result array where result must be stored (it may be * one of the input arrays) * @param resultOffset offset of the result in its array */ public void linearCombination(final double a1, final double[] c1, final int offset1, final double a2, final double[] c2, final int offset2, final double[] result, final int resultOffset) { for (int i = 0; i < getSize(); ++i) { result[resultOffset + i] = MathArrays.linearCombination(a1, c1[offset1 + i], a2, c2[offset2 + i]); } } /** Compute linear combination. * The derivative structure built will be a1 * ds1 + a2 * ds2 + a3 * ds3 + a4 * ds4 * @param a1 first scale factor * @param c1 first base (unscaled) component * @param offset1 offset of first operand in its array * @param a2 second scale factor * @param c2 second base (unscaled) component * @param offset2 offset of second operand in its array * @param a3 third scale factor * @param c3 third base (unscaled) component * @param offset3 offset of third operand in its array * @param result array where result must be stored (it may be * one of the input arrays) * @param resultOffset offset of the result in its array */ public void linearCombination(final double a1, final double[] c1, final int offset1, final double a2, final double[] c2, final int offset2, final double a3, final double[] c3, final int offset3, final double[] result, final int resultOffset) { for (int i = 0; i < getSize(); ++i) { result[resultOffset + i] = MathArrays.linearCombination(a1, c1[offset1 + i], a2, c2[offset2 + i], a3, c3[offset3 + i]); } } /** Compute linear combination. * The derivative structure built will be a1 * ds1 + a2 * ds2 + a3 * ds3 + a4 * ds4 * @param a1 first scale factor * @param c1 first base (unscaled) component * @param offset1 offset of first operand in its array * @param a2 second scale factor * @param c2 second base (unscaled) component * @param offset2 offset of second operand in its array * @param a3 third scale factor * @param c3 third base (unscaled) component * @param offset3 offset of third operand in its array * @param a4 fourth scale factor * @param c4 fourth base (unscaled) component * @param offset4 offset of fourth operand in its array * @param result array where result must be stored (it may be * one of the input arrays) * @param resultOffset offset of the result in its array */ public void linearCombination(final double a1, final double[] c1, final int offset1, final double a2, final double[] c2, final int offset2, final double a3, final double[] c3, final int offset3, final double a4, final double[] c4, final int offset4, final double[] result, final int resultOffset) { for (int i = 0; i < getSize(); ++i) { result[resultOffset + i] = MathArrays.linearCombination(a1, c1[offset1 + i], a2, c2[offset2 + i], a3, c3[offset3 + i], a4, c4[offset4 + i]); } } /** Perform addition of two derivative structures. * @param lhs array holding left hand side of addition * @param lhsOffset offset of the left hand side in its array * @param rhs array right hand side of addition * @param rhsOffset offset of the right hand side in its array * @param result array where result must be stored (it may be * one of the input arrays) * @param resultOffset offset of the result in its array */ public void add(final double[] lhs, final int lhsOffset, final double[] rhs, final int rhsOffset, final double[] result, final int resultOffset) { for (int i = 0; i < getSize(); ++i) { result[resultOffset + i] = lhs[lhsOffset + i] + rhs[rhsOffset + i]; } } /** Perform subtraction of two derivative structures. * @param lhs array holding left hand side of subtraction * @param lhsOffset offset of the left hand side in its array * @param rhs array right hand side of subtraction * @param rhsOffset offset of the right hand side in its array * @param result array where result must be stored (it may be * one of the input arrays) * @param resultOffset offset of the result in its array */ public void subtract(final double[] lhs, final int lhsOffset, final double[] rhs, final int rhsOffset, final double[] result, final int resultOffset) { for (int i = 0; i < getSize(); ++i) { result[resultOffset + i] = lhs[lhsOffset + i] - rhs[rhsOffset + i]; } } /** Perform multiplication of two derivative structures. * @param lhs array holding left hand side of multiplication * @param lhsOffset offset of the left hand side in its array * @param rhs array right hand side of multiplication * @param rhsOffset offset of the right hand side in its array * @param result array where result must be stored (for * multiplication the result array cannot be one of * the input arrays) * @param resultOffset offset of the result in its array */ public void multiply(final double[] lhs, final int lhsOffset, final double[] rhs, final int rhsOffset, final double[] result, final int resultOffset) { for (int i = 0; i < multIndirection.length; ++i) { final int[][] mappingI = multIndirection[i]; double r = 0; for (int j = 0; j < mappingI.length; ++j) { r += mappingI[j][0] * lhs[lhsOffset + mappingI[j][1]] * rhs[rhsOffset + mappingI[j][2]]; } result[resultOffset + i] = r; } } /** Perform division of two derivative structures. * @param lhs array holding left hand side of division * @param lhsOffset offset of the left hand side in its array * @param rhs array right hand side of division * @param rhsOffset offset of the right hand side in its array * @param result array where result must be stored (for * division the result array cannot be one of * the input arrays) * @param resultOffset offset of the result in its array */ public void divide(final double[] lhs, final int lhsOffset, final double[] rhs, final int rhsOffset, final double[] result, final int resultOffset) { final double[] reciprocal = new double[getSize()]; pow(rhs, lhsOffset, -1, reciprocal, 0); multiply(lhs, lhsOffset, reciprocal, 0, result, resultOffset); } /** Perform remainder of two derivative structures. * @param lhs array holding left hand side of remainder * @param lhsOffset offset of the left hand side in its array * @param rhs array right hand side of remainder * @param rhsOffset offset of the right hand side in its array * @param result array where result must be stored (it may be * one of the input arrays) * @param resultOffset offset of the result in its array */ public void remainder(final double[] lhs, final int lhsOffset, final double[] rhs, final int rhsOffset, final double[] result, final int resultOffset) { // compute k such that lhs % rhs = lhs - k rhs final double rem = FastMath.IEEEremainder(lhs[lhsOffset], rhs[rhsOffset]); final double k = FastMath.rint((lhs[lhsOffset] - rem) / rhs[rhsOffset]); // set up value result[resultOffset] = rem; // set up partial derivatives for (int i = 1; i < getSize(); ++i) { result[resultOffset + i] = lhs[lhsOffset + i] - k * rhs[rhsOffset + i]; } } /** Compute power of a derivative structure. * @param operand array holding the operand * @param operandOffset offset of the operand in its array * @param p power to apply * @param result array where result must be stored (for * power the result array cannot be the input * array) * @param resultOffset offset of the result in its array */ public void pow(final double[] operand, final int operandOffset, final double p, final double[] result, final int resultOffset) { // create the function value and derivatives // [x^p, px^(p-1), p(p-1)x^(p-2), ... ] double[] function = new double[1 + order]; double xk = FastMath.pow(operand[operandOffset], p - order); for (int i = order; i > 0; --i) { function[i] = xk; xk *= operand[operandOffset]; } function[0] = xk; double coefficient = p; for (int i = 1; i <= order; ++i) { function[i] *= coefficient; coefficient *= p - i; } // apply function composition compose(operand, operandOffset, function, result, resultOffset); } /** Compute integer power of a derivative structure. * @param operand array holding the operand * @param operandOffset offset of the operand in its array * @param n power to apply * @param result array where result must be stored (for * power the result array cannot be the input * array) * @param resultOffset offset of the result in its array */ public void pow(final double[] operand, final int operandOffset, final int n, final double[] result, final int resultOffset) { if (n == 0) { // special case, x^0 = 1 for all x result[resultOffset] = 1.0; Arrays.fill(result, resultOffset + 1, resultOffset + getSize(), 0); return; } // create the power function value and derivatives // [x^n, nx^(n-1), n(n-1)x^(n-2), ... ] double[] function = new double[1 + order]; if (n > 0) { // strictly positive power final int maxOrder = FastMath.min(order, n); double xk = FastMath.pow(operand[operandOffset], n - maxOrder); for (int i = maxOrder; i > 0; --i) { function[i] = xk; xk *= operand[operandOffset]; } function[0] = xk; } else { // strictly negative power final double inv = 1.0 / operand[operandOffset]; double xk = FastMath.pow(inv, -n); for (int i = 0; i <= order; ++i) { function[i] = xk; xk *= inv; } } double coefficient = n; for (int i = 1; i <= order; ++i) { function[i] *= coefficient; coefficient *= n - i; } // apply function composition compose(operand, operandOffset, function, result, resultOffset); } /** Compute power of a derivative structure. * @param x array holding the base * @param xOffset offset of the base in its array * @param y array holding the exponent * @param yOffset offset of the exponent in its array * @param result array where result must be stored (for * power the result array cannot be the input * array) * @param resultOffset offset of the result in its array */ public void pow(final double[] x, final int xOffset, final double[] y, final int yOffset, final double[] result, final int resultOffset) { final double[] logX = new double[getSize()]; log(x, xOffset, logX, 0); final double[] yLogX = new double[getSize()]; multiply(logX, 0, y, yOffset, yLogX, 0); exp(yLogX, 0, result, resultOffset); } /** Compute nth root of a derivative structure. * @param operand array holding the operand * @param operandOffset offset of the operand in its array * @param n order of the root * @param result array where result must be stored (for * nth root the result array cannot be the input * array) * @param resultOffset offset of the result in its array */ public void rootN(final double[] operand, final int operandOffset, final int n, final double[] result, final int resultOffset) { // create the function value and derivatives // [x^(1/n), (1/n)x^((1/n)-1), (1-n)/n^2x^((1/n)-2), ... ] double[] function = new double[1 + order]; double xk; if (n == 2) { function[0] = FastMath.sqrt(operand[operandOffset]); xk = 0.5 / function[0]; } else if (n == 3) { function[0] = FastMath.cbrt(operand[operandOffset]); xk = 1.0 / (3.0 * function[0] * function[0]); } else { function[0] = FastMath.pow(operand[operandOffset], 1.0 / n); xk = 1.0 / (n * FastMath.pow(function[0], n - 1)); } final double nReciprocal = 1.0 / n; final double xReciprocal = 1.0 / operand[operandOffset]; for (int i = 1; i <= order; ++i) { function[i] = xk; xk *= xReciprocal * (nReciprocal - i); } // apply function composition compose(operand, operandOffset, function, result, resultOffset); } /** Compute exponential of a derivative structure. * @param operand array holding the operand * @param operandOffset offset of the operand in its array * @param result array where result must be stored (for * exponential the result array cannot be the input * array) * @param resultOffset offset of the result in its array */ public void exp(final double[] operand, final int operandOffset, final double[] result, final int resultOffset) { // create the function value and derivatives double[] function = new double[1 + order]; Arrays.fill(function, FastMath.exp(operand[operandOffset])); // apply function composition compose(operand, operandOffset, function, result, resultOffset); } /** Compute exp(x) - 1 of a derivative structure. * @param operand array holding the operand * @param operandOffset offset of the operand in its array * @param result array where result must be stored (for * exponential the result array cannot be the input * array) * @param resultOffset offset of the result in its array */ public void expm1(final double[] operand, final int operandOffset, final double[] result, final int resultOffset) { // create the function value and derivatives double[] function = new double[1 + order]; function[0] = FastMath.expm1(operand[operandOffset]); Arrays.fill(function, 1, 1 + order, FastMath.exp(operand[operandOffset])); // apply function composition compose(operand, operandOffset, function, result, resultOffset); } /** Compute natural logarithm of a derivative structure. * @param operand array holding the operand * @param operandOffset offset of the operand in its array * @param result array where result must be stored (for * logarithm the result array cannot be the input * array) * @param resultOffset offset of the result in its array */ public void log(final double[] operand, final int operandOffset, final double[] result, final int resultOffset) { // create the function value and derivatives double[] function = new double[1 + order]; function[0] = FastMath.log(operand[operandOffset]); if (order > 0) { double inv = 1.0 / operand[operandOffset]; double xk = inv; for (int i = 1; i <= order; ++i) { function[i] = xk; xk *= -i * inv; } } // apply function composition compose(operand, operandOffset, function, result, resultOffset); } /** Computes shifted logarithm of a derivative structure. * @param operand array holding the operand * @param operandOffset offset of the operand in its array * @param result array where result must be stored (for * shifted logarithm the result array cannot be the input array) * @param resultOffset offset of the result in its array */ public void log1p(final double[] operand, final int operandOffset, final double[] result, final int resultOffset) { // create the function value and derivatives double[] function = new double[1 + order]; function[0] = FastMath.log1p(operand[operandOffset]); if (order > 0) { double inv = 1.0 / (1.0 + operand[operandOffset]); double xk = inv; for (int i = 1; i <= order; ++i) { function[i] = xk; xk *= -i * inv; } } // apply function composition compose(operand, operandOffset, function, result, resultOffset); } /** Computes base 10 logarithm of a derivative structure. * @param operand array holding the operand * @param operandOffset offset of the operand in its array * @param result array where result must be stored (for * base 10 logarithm the result array cannot be the input array) * @param resultOffset offset of the result in its array */ public void log10(final double[] operand, final int operandOffset, final double[] result, final int resultOffset) { // create the function value and derivatives double[] function = new double[1 + order]; function[0] = FastMath.log10(operand[operandOffset]); if (order > 0) { double inv = 1.0 / operand[operandOffset]; double xk = inv / FastMath.log(10.0); for (int i = 1; i <= order; ++i) { function[i] = xk; xk *= -i * inv; } } // apply function composition compose(operand, operandOffset, function, result, resultOffset); } /** Compute cosine of a derivative structure. * @param operand array holding the operand * @param operandOffset offset of the operand in its array * @param result array where result must be stored (for * cosine the result array cannot be the input * array) * @param resultOffset offset of the result in its array */ public void cos(final double[] operand, final int operandOffset, final double[] result, final int resultOffset) { // create the function value and derivatives double[] function = new double[1 + order]; function[0] = FastMath.cos(operand[operandOffset]); if (order > 0) { function[1] = -FastMath.sin(operand[operandOffset]); for (int i = 2; i <= order; ++i) { function[i] = -function[i - 2]; } } // apply function composition compose(operand, operandOffset, function, result, resultOffset); } /** Compute sine of a derivative structure. * @param operand array holding the operand * @param operandOffset offset of the operand in its array * @param result array where result must be stored (for * sine the result array cannot be the input * array) * @param resultOffset offset of the result in its array */ public void sin(final double[] operand, final int operandOffset, final double[] result, final int resultOffset) { // create the function value and derivatives double[] function = new double[1 + order]; function[0] = FastMath.sin(operand[operandOffset]); if (order > 0) { function[1] = FastMath.cos(operand[operandOffset]); for (int i = 2; i <= order; ++i) { function[i] = -function[i - 2]; } } // apply function composition compose(operand, operandOffset, function, result, resultOffset); } /** Compute tangent of a derivative structure. * @param operand array holding the operand * @param operandOffset offset of the operand in its array * @param result array where result must be stored (for * tangent the result array cannot be the input * array) * @param resultOffset offset of the result in its array */ public void tan(final double[] operand, final int operandOffset, final double[] result, final int resultOffset) { // create the function value and derivatives final double[] function = new double[1 + order]; final double t = FastMath.tan(operand[operandOffset]); function[0] = t; if (order > 0) { // the nth order derivative of tan has the form: // dn(tan(x)/dxn = P_n(tan(x)) // where P_n(t) is a degree n+1 polynomial with same parity as n+1 // P_0(t) = t, P_1(t) = 1 + t^2, P_2(t) = 2 t (1 + t^2) ... // the general recurrence relation for P_n is: // P_n(x) = (1+t^2) P_(n-1)'(t) // as per polynomial parity, we can store coefficients of both P_(n-1) and P_n in the same array final double[] p = new double[order + 2]; p[1] = 1; final double t2 = t * t; for (int n = 1; n <= order; ++n) { // update and evaluate polynomial P_n(t) double v = 0; p[n + 1] = n * p[n]; for (int k = n + 1; k >= 0; k -= 2) { v = v * t2 + p[k]; if (k > 2) { p[k - 2] = (k - 1) * p[k - 1] + (k - 3) * p[k - 3]; } else if (k == 2) { p[0] = p[1]; } } if ((n & 0x1) == 0) { v *= t; } function[n] = v; } } // apply function composition compose(operand, operandOffset, function, result, resultOffset); } /** Compute arc cosine of a derivative structure. * @param operand array holding the operand * @param operandOffset offset of the operand in its array * @param result array where result must be stored (for * arc cosine the result array cannot be the input * array) * @param resultOffset offset of the result in its array */ public void acos(final double[] operand, final int operandOffset, final double[] result, final int resultOffset) { // create the function value and derivatives double[] function = new double[1 + order]; final double x = operand[operandOffset]; function[0] = FastMath.acos(x); if (order > 0) { // the nth order derivative of acos has the form: // dn(acos(x)/dxn = P_n(x) / [1 - x^2]^((2n-1)/2) // where P_n(x) is a degree n-1 polynomial with same parity as n-1 // P_1(x) = -1, P_2(x) = -x, P_3(x) = -2x^2 - 1 ... // the general recurrence relation for P_n is: // P_n(x) = (1-x^2) P_(n-1)'(x) + (2n-3) x P_(n-1)(x) // as per polynomial parity, we can store coefficients of both P_(n-1) and P_n in the same array final double[] p = new double[order]; p[0] = -1; final double x2 = x * x; final double f = 1.0 / (1 - x2); double coeff = FastMath.sqrt(f); function[1] = coeff * p[0]; for (int n = 2; n <= order; ++n) { // update and evaluate polynomial P_n(x) double v = 0; p[n - 1] = (n - 1) * p[n - 2]; for (int k = n - 1; k >= 0; k -= 2) { v = v * x2 + p[k]; if (k > 2) { p[k - 2] = (k - 1) * p[k - 1] + (2 * n - k) * p[k - 3]; } else if (k == 2) { p[0] = p[1]; } } if ((n & 0x1) == 0) { v *= x; } coeff *= f; function[n] = coeff * v; } } // apply function composition compose(operand, operandOffset, function, result, resultOffset); } /** Compute arc sine of a derivative structure. * @param operand array holding the operand * @param operandOffset offset of the operand in its array * @param result array where result must be stored (for * arc sine the result array cannot be the input * array) * @param resultOffset offset of the result in its array */ public void asin(final double[] operand, final int operandOffset, final double[] result, final int resultOffset) { // create the function value and derivatives double[] function = new double[1 + order]; final double x = operand[operandOffset]; function[0] = FastMath.asin(x); if (order > 0) { // the nth order derivative of asin has the form: // dn(asin(x)/dxn = P_n(x) / [1 - x^2]^((2n-1)/2) // where P_n(x) is a degree n-1 polynomial with same parity as n-1 // P_1(x) = 1, P_2(x) = x, P_3(x) = 2x^2 + 1 ... // the general recurrence relation for P_n is: // P_n(x) = (1-x^2) P_(n-1)'(x) + (2n-3) x P_(n-1)(x) // as per polynomial parity, we can store coefficients of both P_(n-1) and P_n in the same array final double[] p = new double[order]; p[0] = 1; final double x2 = x * x; final double f = 1.0 / (1 - x2); double coeff = FastMath.sqrt(f); function[1] = coeff * p[0]; for (int n = 2; n <= order; ++n) { // update and evaluate polynomial P_n(x) double v = 0; p[n - 1] = (n - 1) * p[n - 2]; for (int k = n - 1; k >= 0; k -= 2) { v = v * x2 + p[k]; if (k > 2) { p[k - 2] = (k - 1) * p[k - 1] + (2 * n - k) * p[k - 3]; } else if (k == 2) { p[0] = p[1]; } } if ((n & 0x1) == 0) { v *= x; } coeff *= f; function[n] = coeff * v; } } // apply function composition compose(operand, operandOffset, function, result, resultOffset); } /** Compute arc tangent of a derivative structure. * @param operand array holding the operand * @param operandOffset offset of the operand in its array * @param result array where result must be stored (for * arc tangent the result array cannot be the input * array) * @param resultOffset offset of the result in its array */ public void atan(final double[] operand, final int operandOffset, final double[] result, final int resultOffset) { // create the function value and derivatives double[] function = new double[1 + order]; final double x = operand[operandOffset]; function[0] = FastMath.atan(x); if (order > 0) { // the nth order derivative of atan has the form: // dn(atan(x)/dxn = Q_n(x) / (1 + x^2)^n // where Q_n(x) is a degree n-1 polynomial with same parity as n-1 // Q_1(x) = 1, Q_2(x) = -2x, Q_3(x) = 6x^2 - 2 ... // the general recurrence relation for Q_n is: // Q_n(x) = (1+x^2) Q_(n-1)'(x) - 2(n-1) x Q_(n-1)(x) // as per polynomial parity, we can store coefficients of both Q_(n-1) and Q_n in the same array final double[] q = new double[order]; q[0] = 1; final double x2 = x * x; final double f = 1.0 / (1 + x2); double coeff = f; function[1] = coeff * q[0]; for (int n = 2; n <= order; ++n) { // update and evaluate polynomial Q_n(x) double v = 0; q[n - 1] = -n * q[n - 2]; for (int k = n - 1; k >= 0; k -= 2) { v = v * x2 + q[k]; if (k > 2) { q[k - 2] = (k - 1) * q[k - 1] + (k - 1 - 2 * n) * q[k - 3]; } else if (k == 2) { q[0] = q[1]; } } if ((n & 0x1) == 0) { v *= x; } coeff *= f; function[n] = coeff * v; } } // apply function composition compose(operand, operandOffset, function, result, resultOffset); } /** Compute two arguments arc tangent of a derivative structure. * @param y array holding the first operand * @param yOffset offset of the first operand in its array * @param x array holding the second operand * @param xOffset offset of the second operand in its array * @param result array where result must be stored (for * two arguments arc tangent the result array cannot * be the input array) * @param resultOffset offset of the result in its array */ public void atan2(final double[] y, final int yOffset, final double[] x, final int xOffset, final double[] result, final int resultOffset) { // compute r = sqrt(x^2+y^2) double[] tmp1 = new double[getSize()]; multiply(x, xOffset, x, xOffset, tmp1, 0); // x^2 double[] tmp2 = new double[getSize()]; multiply(y, yOffset, y, yOffset, tmp2, 0); // y^2 add(tmp1, 0, tmp2, 0, tmp2, 0); // x^2 + y^2 rootN(tmp2, 0, 2, tmp1, 0); // r = sqrt(x^2 + y^2) if (x[xOffset] >= 0) { // compute atan2(y, x) = 2 atan(y / (r + x)) add(tmp1, 0, x, xOffset, tmp2, 0); // r + x divide(y, yOffset, tmp2, 0, tmp1, 0); // y /(r + x) atan(tmp1, 0, tmp2, 0); // atan(y / (r + x)) for (int i = 0; i < tmp2.length; ++i) { result[resultOffset + i] = 2 * tmp2[i]; // 2 * atan(y / (r + x)) } } else { // compute atan2(y, x) = +/- pi - 2 atan(y / (r - x)) subtract(tmp1, 0, x, xOffset, tmp2, 0); // r - x divide(y, yOffset, tmp2, 0, tmp1, 0); // y /(r - x) atan(tmp1, 0, tmp2, 0); // atan(y / (r - x)) result[resultOffset] = ((tmp2[0] <= 0) ? -FastMath.PI : FastMath.PI) - 2 * tmp2[0]; // +/-pi - 2 * atan(y / (r - x)) for (int i = 1; i < tmp2.length; ++i) { result[resultOffset + i] = -2 * tmp2[i]; // +/-pi - 2 * atan(y / (r - x)) } } // fix value to take special cases (+0/+0, +0/-0, -0/+0, -0/-0, +/-infinity) correctly result[resultOffset] = FastMath.atan2(y[yOffset], x[xOffset]); } /** Compute hyperbolic cosine of a derivative structure. * @param operand array holding the operand * @param operandOffset offset of the operand in its array * @param result array where result must be stored (for * hyperbolic cosine the result array cannot be the input * array) * @param resultOffset offset of the result in its array */ public void cosh(final double[] operand, final int operandOffset, final double[] result, final int resultOffset) { // create the function value and derivatives double[] function = new double[1 + order]; function[0] = FastMath.cosh(operand[operandOffset]); if (order > 0) { function[1] = FastMath.sinh(operand[operandOffset]); for (int i = 2; i <= order; ++i) { function[i] = function[i - 2]; } } // apply function composition compose(operand, operandOffset, function, result, resultOffset); } /** Compute hyperbolic sine of a derivative structure. * @param operand array holding the operand * @param operandOffset offset of the operand in its array * @param result array where result must be stored (for * hyperbolic sine the result array cannot be the input * array) * @param resultOffset offset of the result in its array */ public void sinh(final double[] operand, final int operandOffset, final double[] result, final int resultOffset) { // create the function value and derivatives double[] function = new double[1 + order]; function[0] = FastMath.sinh(operand[operandOffset]); if (order > 0) { function[1] = FastMath.cosh(operand[operandOffset]); for (int i = 2; i <= order; ++i) { function[i] = function[i - 2]; } } // apply function composition compose(operand, operandOffset, function, result, resultOffset); } /** Compute hyperbolic tangent of a derivative structure. * @param operand array holding the operand * @param operandOffset offset of the operand in its array * @param result array where result must be stored (for * hyperbolic tangent the result array cannot be the input * array) * @param resultOffset offset of the result in its array */ public void tanh(final double[] operand, final int operandOffset, final double[] result, final int resultOffset) { // create the function value and derivatives final double[] function = new double[1 + order]; final double t = FastMath.tanh(operand[operandOffset]); function[0] = t; if (order > 0) { // the nth order derivative of tanh has the form: // dn(tanh(x)/dxn = P_n(tanh(x)) // where P_n(t) is a degree n+1 polynomial with same parity as n+1 // P_0(t) = t, P_1(t) = 1 - t^2, P_2(t) = -2 t (1 - t^2) ... // the general recurrence relation for P_n is: // P_n(x) = (1-t^2) P_(n-1)'(t) // as per polynomial parity, we can store coefficients of both P_(n-1) and P_n in the same array final double[] p = new double[order + 2]; p[1] = 1; final double t2 = t * t; for (int n = 1; n <= order; ++n) { // update and evaluate polynomial P_n(t) double v = 0; p[n + 1] = -n * p[n]; for (int k = n + 1; k >= 0; k -= 2) { v = v * t2 + p[k]; if (k > 2) { p[k - 2] = (k - 1) * p[k - 1] - (k - 3) * p[k - 3]; } else if (k == 2) { p[0] = p[1]; } } if ((n & 0x1) == 0) { v *= t; } function[n] = v; } } // apply function composition compose(operand, operandOffset, function, result, resultOffset); } /** Compute inverse hyperbolic cosine of a derivative structure. * @param operand array holding the operand * @param operandOffset offset of the operand in its array * @param result array where result must be stored (for * inverse hyperbolic cosine the result array cannot be the input * array) * @param resultOffset offset of the result in its array */ public void acosh(final double[] operand, final int operandOffset, final double[] result, final int resultOffset) { // create the function value and derivatives double[] function = new double[1 + order]; final double x = operand[operandOffset]; function[0] = FastMath.acosh(x); if (order > 0) { // the nth order derivative of acosh has the form: // dn(acosh(x)/dxn = P_n(x) / [x^2 - 1]^((2n-1)/2) // where P_n(x) is a degree n-1 polynomial with same parity as n-1 // P_1(x) = 1, P_2(x) = -x, P_3(x) = 2x^2 + 1 ... // the general recurrence relation for P_n is: // P_n(x) = (x^2-1) P_(n-1)'(x) - (2n-3) x P_(n-1)(x) // as per polynomial parity, we can store coefficients of both P_(n-1) and P_n in the same array final double[] p = new double[order]; p[0] = 1; final double x2 = x * x; final double f = 1.0 / (x2 - 1); double coeff = FastMath.sqrt(f); function[1] = coeff * p[0]; for (int n = 2; n <= order; ++n) { // update and evaluate polynomial P_n(x) double v = 0; p[n - 1] = (1 - n) * p[n - 2]; for (int k = n - 1; k >= 0; k -= 2) { v = v * x2 + p[k]; if (k > 2) { p[k - 2] = (1 - k) * p[k - 1] + (k - 2 * n) * p[k - 3]; } else if (k == 2) { p[0] = -p[1]; } } if ((n & 0x1) == 0) { v *= x; } coeff *= f; function[n] = coeff * v; } } // apply function composition compose(operand, operandOffset, function, result, resultOffset); } /** Compute inverse hyperbolic sine of a derivative structure. * @param operand array holding the operand * @param operandOffset offset of the operand in its array * @param result array where result must be stored (for * inverse hyperbolic sine the result array cannot be the input * array) * @param resultOffset offset of the result in its array */ public void asinh(final double[] operand, final int operandOffset, final double[] result, final int resultOffset) { // create the function value and derivatives double[] function = new double[1 + order]; final double x = operand[operandOffset]; function[0] = FastMath.asinh(x); if (order > 0) { // the nth order derivative of asinh has the form: // dn(asinh(x)/dxn = P_n(x) / [x^2 + 1]^((2n-1)/2) // where P_n(x) is a degree n-1 polynomial with same parity as n-1 // P_1(x) = 1, P_2(x) = -x, P_3(x) = 2x^2 - 1 ... // the general recurrence relation for P_n is: // P_n(x) = (x^2+1) P_(n-1)'(x) - (2n-3) x P_(n-1)(x) // as per polynomial parity, we can store coefficients of both P_(n-1) and P_n in the same array final double[] p = new double[order]; p[0] = 1; final double x2 = x * x; final double f = 1.0 / (1 + x2); double coeff = FastMath.sqrt(f); function[1] = coeff * p[0]; for (int n = 2; n <= order; ++n) { // update and evaluate polynomial P_n(x) double v = 0; p[n - 1] = (1 - n) * p[n - 2]; for (int k = n - 1; k >= 0; k -= 2) { v = v * x2 + p[k]; if (k > 2) { p[k - 2] = (k - 1) * p[k - 1] + (k - 2 * n) * p[k - 3]; } else if (k == 2) { p[0] = p[1]; } } if ((n & 0x1) == 0) { v *= x; } coeff *= f; function[n] = coeff * v; } } // apply function composition compose(operand, operandOffset, function, result, resultOffset); } /** Compute inverse hyperbolic tangent of a derivative structure. * @param operand array holding the operand * @param operandOffset offset of the operand in its array * @param result array where result must be stored (for * inverse hyperbolic tangent the result array cannot be the input * array) * @param resultOffset offset of the result in its array */ public void atanh(final double[] operand, final int operandOffset, final double[] result, final int resultOffset) { // create the function value and derivatives double[] function = new double[1 + order]; final double x = operand[operandOffset]; function[0] = FastMath.atanh(x); if (order > 0) { // the nth order derivative of atanh has the form: // dn(atanh(x)/dxn = Q_n(x) / (1 - x^2)^n // where Q_n(x) is a degree n-1 polynomial with same parity as n-1 // Q_1(x) = 1, Q_2(x) = 2x, Q_3(x) = 6x^2 + 2 ... // the general recurrence relation for Q_n is: // Q_n(x) = (1-x^2) Q_(n-1)'(x) + 2(n-1) x Q_(n-1)(x) // as per polynomial parity, we can store coefficients of both Q_(n-1) and Q_n in the same array final double[] q = new double[order]; q[0] = 1; final double x2 = x * x; final double f = 1.0 / (1 - x2); double coeff = f; function[1] = coeff * q[0]; for (int n = 2; n <= order; ++n) { // update and evaluate polynomial Q_n(x) double v = 0; q[n - 1] = n * q[n - 2]; for (int k = n - 1; k >= 0; k -= 2) { v = v * x2 + q[k]; if (k > 2) { q[k - 2] = (k - 1) * q[k - 1] + (2 * n - k + 1) * q[k - 3]; } else if (k == 2) { q[0] = q[1]; } } if ((n & 0x1) == 0) { v *= x; } coeff *= f; function[n] = coeff * v; } } // apply function composition compose(operand, operandOffset, function, result, resultOffset); } /** Compute composition of a derivative structure by a function. * @param operand array holding the operand * @param operandOffset offset of the operand in its array * @param f array of value and derivatives of the function at * the current point (i.e. at {@code operand[operandOffset]}). * @param result array where result must be stored (for * composition the result array cannot be the input * array) * @param resultOffset offset of the result in its array */ public void compose(final double[] operand, final int operandOffset, final double[] f, final double[] result, final int resultOffset) { for (int i = 0; i < compIndirection.length; ++i) { final int[][] mappingI = compIndirection[i]; double r = 0; for (int j = 0; j < mappingI.length; ++j) { final int[] mappingIJ = mappingI[j]; double product = mappingIJ[0] * f[mappingIJ[1]]; for (int k = 2; k < mappingIJ.length; ++k) { product *= operand[operandOffset + mappingIJ[k]]; } r += product; } result[resultOffset + i] = r; } } /** Evaluate Taylor expansion of a derivative structure. * @param ds array holding the derivative structure * @param dsOffset offset of the derivative structure in its array * @param delta parameters offsets (Δx, Δy, ...) * @return value of the Taylor expansion at x + Δx, y + Δy, ... * @throws MathArithmeticException if factorials becomes too large */ public double taylor(final double[] ds, final int dsOffset, final double ... delta) throws MathArithmeticException { double value = 0; for (int i = getSize() - 1; i >= 0; --i) { final int[] orders = getPartialDerivativeOrders(i); double term = ds[dsOffset + i]; for (int k = 0; k < orders.length; ++k) { if (orders[k] > 0) { try { term *= FastMath.pow(delta[k], orders[k]) / ArithmeticUtils.factorial(orders[k]); } catch (NotPositiveException e) { // this cannot happen throw new MathInternalError(e); } } } value += term; } return value; } /** Check rules set compatibility. * @param compiler other compiler to check against instance * @exception DimensionMismatchException if number of free parameters or orders are inconsistent */ public void checkCompatibility(final DSCompiler compiler) throws DimensionMismatchException { if (parameters != compiler.parameters) { throw new DimensionMismatchException(parameters, compiler.parameters); } if (order != compiler.order) { throw new DimensionMismatchException(order, compiler.order); } } } ././@LongLink100644 0 0 154 12126630646 10261 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/analysis/differentiation/GradientFunction.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/analysis/differentiation/GradientFuncti100644 1750 1750 4412 12126627706 32354 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.analysis.differentiation; import org.apache.commons.math3.analysis.MultivariateVectorFunction; /** Class representing the gradient of a multivariate function. *

                            * The vectorial components of the function represent the derivatives * with respect to each function parameters. *

                            * @version $Id: GradientFunction.java 1455194 2013-03-11 15:45:54Z luc $ * @since 3.1 */ public class GradientFunction implements MultivariateVectorFunction { /** Underlying real-valued function. */ private final MultivariateDifferentiableFunction f; /** Simple constructor. * @param f underlying real-valued function */ public GradientFunction(final MultivariateDifferentiableFunction f) { this.f = f; } /** {@inheritDoc} */ public double[] value(double[] point) { // set up parameters final DerivativeStructure[] dsX = new DerivativeStructure[point.length]; for (int i = 0; i < point.length; ++i) { dsX[i] = new DerivativeStructure(point.length, 1, i, point[i]); } // compute the derivatives final DerivativeStructure dsY = f.value(dsX); // extract the gradient final double[] y = new double[point.length]; final int[] orders = new int[point.length]; for (int i = 0; i < point.length; ++i) { orders[i] = 1; y[i] = dsY.getPartialDerivative(orders); orders[i] = 0; } return y; } } ././@LongLink100644 0 0 176 12126630646 10265 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/analysis/differentiation/MultivariateDifferentiableFunction.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/analysis/differentiation/MultivariateDi100644 1750 1750 3446 12126627706 32377 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.analysis.differentiation; import org.apache.commons.math3.analysis.MultivariateFunction; import org.apache.commons.math3.exception.MathIllegalArgumentException; /** * Extension of {@link MultivariateFunction} representing a * multivariate differentiable real function. * @version $Id: MultivariateDifferentiableFunction.java 1462496 2013-03-29 14:56:08Z psteitz $ * @since 3.1 */ public interface MultivariateDifferentiableFunction extends MultivariateFunction { /** * Compute the value for the function at the given point. * * @param point Point at which the function must be evaluated. * @return the function value for the given point. * @exception MathIllegalArgumentException if {@code point} does not * satisfy the function's constraints (wrong dimension, argument out of bound, * or unsupported derivative order for example) */ DerivativeStructure value(DerivativeStructure[] point) throws MathIllegalArgumentException; } ././@LongLink100644 0 0 202 12126630646 10253 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/analysis/differentiation/UnivariateDifferentiableVectorFunction.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/analysis/differentiation/UnivariateDiff100644 1750 1750 3356 12126627706 32354 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.analysis.differentiation; import org.apache.commons.math3.analysis.UnivariateVectorFunction; import org.apache.commons.math3.exception.MathIllegalArgumentException; /** * Extension of {@link UnivariateVectorFunction} representing a univariate differentiable vectorial function. * * @version $Id: UnivariateDifferentiableVectorFunction.java 1462699 2013-03-30 04:09:17Z psteitz $ * @since 3.1 */ public interface UnivariateDifferentiableVectorFunction extends UnivariateVectorFunction { /** * Compute the value for the function. * @param x the point for which the function value should be computed * @return the value * @exception MathIllegalArgumentException if {@code x} does not * satisfy the function's constraints (argument out of bound, or unsupported * derivative order for example) */ DerivativeStructure[] value(DerivativeStructure x) throws MathIllegalArgumentException; } ././@LongLink100644 0 0 154 12126630646 10261 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/analysis/differentiation/JacobianFunction.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/analysis/differentiation/JacobianFuncti100644 1750 1750 5060 12126627706 32325 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.analysis.differentiation; import org.apache.commons.math3.analysis.MultivariateMatrixFunction; /** Class representing the Jacobian of a multivariate vector function. *

                            * The rows iterate on the model functions while the columns iterate on the parameters; thus, * the numbers of rows is equal to the dimension of the underlying function vector * value and the number of columns is equal to the number of free parameters of * the underlying function. *

                            * @version $Id: JacobianFunction.java 1455194 2013-03-11 15:45:54Z luc $ * @since 3.1 */ public class JacobianFunction implements MultivariateMatrixFunction { /** Underlying vector-valued function. */ private final MultivariateDifferentiableVectorFunction f; /** Simple constructor. * @param f underlying vector-valued function */ public JacobianFunction(final MultivariateDifferentiableVectorFunction f) { this.f = f; } /** {@inheritDoc} */ public double[][] value(double[] point) { // set up parameters final DerivativeStructure[] dsX = new DerivativeStructure[point.length]; for (int i = 0; i < point.length; ++i) { dsX[i] = new DerivativeStructure(point.length, 1, i, point[i]); } // compute the derivatives final DerivativeStructure[] dsY = f.value(dsX); // extract the Jacobian final double[][] y = new double[dsY.length][point.length]; final int[] orders = new int[point.length]; for (int i = 0; i < dsY.length; ++i) { for (int j = 0; j < point.length; ++j) { orders[j] = 1; y[i][j] = dsY[i].getPartialDerivative(orders); orders[j] = 0; } } return y; } } ././@LongLink100644 0 0 157 12126630646 10264 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/analysis/differentiation/DerivativeStructure.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/analysis/differentiation/DerivativeStru100644 1750 1750 126230 12126627706 32471 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.analysis.differentiation; import java.io.Serializable; import org.apache.commons.math3.RealFieldElement; import org.apache.commons.math3.Field; import org.apache.commons.math3.FieldElement; import org.apache.commons.math3.exception.DimensionMismatchException; import org.apache.commons.math3.exception.MathArithmeticException; import org.apache.commons.math3.exception.NumberIsTooLargeException; import org.apache.commons.math3.util.FastMath; import org.apache.commons.math3.util.MathArrays; import org.apache.commons.math3.util.MathUtils; /** Class representing both the value and the differentials of a function. *

                            This class is the workhorse of the differentiation package.

                            *

                            This class is an implementation of the extension to Rall's * numbers described in Dan Kalman's paper Doubly * Recursive Multivariate Automatic Differentiation, Mathematics Magazine, vol. 75, * no. 3, June 2002.

                            . Rall's numbers are an extension to the real numbers used * throughout mathematical expressions; they hold the derivative together with the * value of a function. Dan Kalman's derivative structures hold all partial derivatives * up to any specified order, with respect to any number of free parameters. Rall's * numbers therefore can be seen as derivative structures for order one derivative and * one free parameter, and real numbers can be seen as derivative structures with zero * order derivative and no free parameters.

                            *

                            {@link DerivativeStructure} instances can be used directly thanks to * the arithmetic operators to the mathematical functions provided as static * methods by this class (+, -, *, /, %, sin, cos ...).

                            *

                            Implementing complex expressions by hand using these classes is * a tedious and error-prone task but has the advantage of having no limitation * on the derivation order despite no requiring users to compute the derivatives by * themselves. Implementing complex expression can also be done by developing computation * code using standard primitive double values and to use {@link * UnivariateFunctionDifferentiator differentiators} to create the {@link * DerivativeStructure}-based instances. This method is simpler but may be limited in * the accuracy and derivation orders and may be computationally intensive (this is * typically the case for {@link FiniteDifferencesDifferentiator finite differences * differentiator}.

                            *

                            Instances of this class are guaranteed to be immutable.

                            * @see DSCompiler * @version $Id: DerivativeStructure.java 1462423 2013-03-29 07:25:18Z luc $ * @since 3.1 */ public class DerivativeStructure implements RealFieldElement, Serializable { /** Serializable UID. */ private static final long serialVersionUID = 20120730L; /** Compiler for the current dimensions. */ private transient DSCompiler compiler; /** Combined array holding all values. */ private final double[] data; /** Build an instance with all values and derivatives set to 0. * @param compiler compiler to use for computation */ private DerivativeStructure(final DSCompiler compiler) { this.compiler = compiler; this.data = new double[compiler.getSize()]; } /** Build an instance with all values and derivatives set to 0. * @param parameters number of free parameters * @param order derivation order * @throws NumberIsTooLargeException if order is too large */ public DerivativeStructure(final int parameters, final int order) throws NumberIsTooLargeException { this(DSCompiler.getCompiler(parameters, order)); } /** Build an instance representing a constant value. * @param parameters number of free parameters * @param order derivation order * @param value value of the constant * @throws NumberIsTooLargeException if order is too large * @see #DerivativeStructure(int, int, int, double) */ public DerivativeStructure(final int parameters, final int order, final double value) throws NumberIsTooLargeException { this(parameters, order); this.data[0] = value; } /** Build an instance representing a variable. *

                            Instances built using this constructor are considered * to be the free variables with respect to which differentials * are computed. As such, their differential with respect to * themselves is +1.

                            * @param parameters number of free parameters * @param order derivation order * @param index index of the variable (from 0 to {@code parameters - 1}) * @param value value of the variable * @exception NumberIsTooLargeException if {@code index >= parameters}. * @see #DerivativeStructure(int, int, double) */ public DerivativeStructure(final int parameters, final int order, final int index, final double value) throws NumberIsTooLargeException { this(parameters, order, value); if (index >= parameters) { throw new NumberIsTooLargeException(index, parameters, false); } if (order > 0) { // the derivative of the variable with respect to itself is 1. data[DSCompiler.getCompiler(index, order).getSize()] = 1.0; } } /** Linear combination constructor. * The derivative structure built will be a1 * ds1 + a2 * ds2 * @param a1 first scale factor * @param ds1 first base (unscaled) derivative structure * @param a2 second scale factor * @param ds2 second base (unscaled) derivative structure * @exception DimensionMismatchException if number of free parameters or orders are inconsistent */ public DerivativeStructure(final double a1, final DerivativeStructure ds1, final double a2, final DerivativeStructure ds2) throws DimensionMismatchException { this(ds1.compiler); compiler.checkCompatibility(ds2.compiler); compiler.linearCombination(a1, ds1.data, 0, a2, ds2.data, 0, data, 0); } /** Linear combination constructor. * The derivative structure built will be a1 * ds1 + a2 * ds2 + a3 * ds3 * @param a1 first scale factor * @param ds1 first base (unscaled) derivative structure * @param a2 second scale factor * @param ds2 second base (unscaled) derivative structure * @param a3 third scale factor * @param ds3 third base (unscaled) derivative structure * @exception DimensionMismatchException if number of free parameters or orders are inconsistent */ public DerivativeStructure(final double a1, final DerivativeStructure ds1, final double a2, final DerivativeStructure ds2, final double a3, final DerivativeStructure ds3) throws DimensionMismatchException { this(ds1.compiler); compiler.checkCompatibility(ds2.compiler); compiler.checkCompatibility(ds3.compiler); compiler.linearCombination(a1, ds1.data, 0, a2, ds2.data, 0, a3, ds3.data, 0, data, 0); } /** Linear combination constructor. * The derivative structure built will be a1 * ds1 + a2 * ds2 + a3 * ds3 + a4 * ds4 * @param a1 first scale factor * @param ds1 first base (unscaled) derivative structure * @param a2 second scale factor * @param ds2 second base (unscaled) derivative structure * @param a3 third scale factor * @param ds3 third base (unscaled) derivative structure * @param a4 fourth scale factor * @param ds4 fourth base (unscaled) derivative structure * @exception DimensionMismatchException if number of free parameters or orders are inconsistent */ public DerivativeStructure(final double a1, final DerivativeStructure ds1, final double a2, final DerivativeStructure ds2, final double a3, final DerivativeStructure ds3, final double a4, final DerivativeStructure ds4) throws DimensionMismatchException { this(ds1.compiler); compiler.checkCompatibility(ds2.compiler); compiler.checkCompatibility(ds3.compiler); compiler.checkCompatibility(ds4.compiler); compiler.linearCombination(a1, ds1.data, 0, a2, ds2.data, 0, a3, ds3.data, 0, a4, ds4.data, 0, data, 0); } /** Build an instance from all its derivatives. * @param parameters number of free parameters * @param order derivation order * @param derivatives derivatives sorted according to * {@link DSCompiler#getPartialDerivativeIndex(int...)} * @exception DimensionMismatchException if derivatives array does not match the * {@link DSCompiler#getSize() size} expected by the compiler * @throws NumberIsTooLargeException if order is too large * @see #getAllDerivatives() */ public DerivativeStructure(final int parameters, final int order, final double ... derivatives) throws DimensionMismatchException, NumberIsTooLargeException { this(parameters, order); if (derivatives.length != data.length) { throw new DimensionMismatchException(derivatives.length, data.length); } System.arraycopy(derivatives, 0, data, 0, data.length); } /** Copy constructor. * @param ds instance to copy */ private DerivativeStructure(final DerivativeStructure ds) { this.compiler = ds.compiler; this.data = ds.data.clone(); } /** Get the number of free parameters. * @return number of free parameters */ public int getFreeParameters() { return compiler.getFreeParameters(); } /** Get the derivation order. * @return derivation order */ public int getOrder() { return compiler.getOrder(); } /** {@inheritDoc} * @since 3.2 */ public double getReal() { return data[0]; } /** Get the value part of the derivative structure. * @return value part of the derivative structure * @see #getPartialDerivative(int...) */ public double getValue() { return data[0]; } /** Get a partial derivative. * @param orders derivation orders with respect to each variable (if all orders are 0, * the value is returned) * @return partial derivative * @see #getValue() * @exception DimensionMismatchException if the numbers of variables does not * match the instance * @exception NumberIsTooLargeException if sum of derivation orders is larger * than the instance limits */ public double getPartialDerivative(final int ... orders) throws DimensionMismatchException, NumberIsTooLargeException { return data[compiler.getPartialDerivativeIndex(orders)]; } /** Get all partial derivatives. * @return a fresh copy of partial derivatives, in an array sorted according to * {@link DSCompiler#getPartialDerivativeIndex(int...)} */ public double[] getAllDerivatives() { return data.clone(); } /** {@inheritDoc} * @since 3.2 */ public DerivativeStructure add(final double a) { final DerivativeStructure ds = new DerivativeStructure(this); ds.data[0] += a; return ds; } /** {@inheritDoc} * @exception DimensionMismatchException if number of free parameters * or orders do not match */ public DerivativeStructure add(final DerivativeStructure a) throws DimensionMismatchException { compiler.checkCompatibility(a.compiler); final DerivativeStructure ds = new DerivativeStructure(this); compiler.add(data, 0, a.data, 0, ds.data, 0); return ds; } /** {@inheritDoc} * @since 3.2 */ public DerivativeStructure subtract(final double a) { return add(-a); } /** {@inheritDoc} * @exception DimensionMismatchException if number of free parameters * or orders do not match */ public DerivativeStructure subtract(final DerivativeStructure a) throws DimensionMismatchException { compiler.checkCompatibility(a.compiler); final DerivativeStructure ds = new DerivativeStructure(this); compiler.subtract(data, 0, a.data, 0, ds.data, 0); return ds; } /** {@inheritDoc} */ public DerivativeStructure multiply(final int n) { return multiply((double) n); } /** {@inheritDoc} * @since 3.2 */ public DerivativeStructure multiply(final double a) { final DerivativeStructure ds = new DerivativeStructure(this); for (int i = 0; i < ds.data.length; ++i) { ds.data[i] *= a; } return ds; } /** {@inheritDoc} * @exception DimensionMismatchException if number of free parameters * or orders do not match */ public DerivativeStructure multiply(final DerivativeStructure a) throws DimensionMismatchException { compiler.checkCompatibility(a.compiler); final DerivativeStructure result = new DerivativeStructure(compiler); compiler.multiply(data, 0, a.data, 0, result.data, 0); return result; } /** {@inheritDoc} * @since 3.2 */ public DerivativeStructure divide(final double a) { final DerivativeStructure ds = new DerivativeStructure(this); for (int i = 0; i < ds.data.length; ++i) { ds.data[i] /= a; } return ds; } /** {@inheritDoc} * @exception DimensionMismatchException if number of free parameters * or orders do not match */ public DerivativeStructure divide(final DerivativeStructure a) throws DimensionMismatchException { compiler.checkCompatibility(a.compiler); final DerivativeStructure result = new DerivativeStructure(compiler); compiler.divide(data, 0, a.data, 0, result.data, 0); return result; } /** {@inheritDoc} */ public DerivativeStructure remainder(final double a) { final DerivativeStructure ds = new DerivativeStructure(this); ds.data[0] = FastMath.IEEEremainder(ds.data[0], a); return ds; } /** {@inheritDoc} * @exception DimensionMismatchException if number of free parameters * or orders do not match * @since 3.2 */ public DerivativeStructure remainder(final DerivativeStructure a) throws DimensionMismatchException { compiler.checkCompatibility(a.compiler); final DerivativeStructure result = new DerivativeStructure(compiler); compiler.remainder(data, 0, a.data, 0, result.data, 0); return result; } /** {@inheritDoc} */ public DerivativeStructure negate() { final DerivativeStructure ds = new DerivativeStructure(compiler); for (int i = 0; i < ds.data.length; ++i) { ds.data[i] = -data[i]; } return ds; } /** {@inheritDoc} * @since 3.2 */ public DerivativeStructure abs() { if (Double.doubleToLongBits(data[0]) < 0) { // we use the bits representation to also handle -0.0 return negate(); } else { return this; } } /** {@inheritDoc} * @since 3.2 */ public DerivativeStructure ceil() { return new DerivativeStructure(compiler.getFreeParameters(), compiler.getOrder(), FastMath.ceil(data[0])); } /** {@inheritDoc} * @since 3.2 */ public DerivativeStructure floor() { return new DerivativeStructure(compiler.getFreeParameters(), compiler.getOrder(), FastMath.floor(data[0])); } /** {@inheritDoc} * @since 3.2 */ public DerivativeStructure rint() { return new DerivativeStructure(compiler.getFreeParameters(), compiler.getOrder(), FastMath.rint(data[0])); } /** {@inheritDoc} */ public long round() { return FastMath.round(data[0]); } /** {@inheritDoc} * @since 3.2 */ public DerivativeStructure signum() { return new DerivativeStructure(compiler.getFreeParameters(), compiler.getOrder(), FastMath.signum(data[0])); } /** {@inheritDoc} * @since 3.2 */ public DerivativeStructure copySign(final DerivativeStructure sign){ long m = Double.doubleToLongBits(data[0]); long s = Double.doubleToLongBits(sign.data[0]); if ((m >= 0 && s >= 0) || (m < 0 && s < 0)) { // Sign is currently OK return this; } return negate(); // flip sign } /** {@inheritDoc} * @since 3.2 */ public DerivativeStructure copySign(final double sign) { long m = Double.doubleToLongBits(data[0]); long s = Double.doubleToLongBits(sign); if ((m >= 0 && s >= 0) || (m < 0 && s < 0)) { // Sign is currently OK return this; } return negate(); // flip sign } /** * Return the exponent of the instance value, removing the bias. *

                            * For double numbers of the form 2x, the unbiased * exponent is exactly x. *

                            * @return exponent for instance in IEEE754 representation, without bias */ public int getExponent() { return FastMath.getExponent(data[0]); } /** {@inheritDoc} * @since 3.2 */ public DerivativeStructure scalb(final int n) { final DerivativeStructure ds = new DerivativeStructure(compiler); for (int i = 0; i < ds.data.length; ++i) { ds.data[i] = FastMath.scalb(data[i], n); } return ds; } /** {@inheritDoc} * @exception DimensionMismatchException if number of free parameters * or orders do not match * @since 3.2 */ public DerivativeStructure hypot(final DerivativeStructure y) throws DimensionMismatchException { compiler.checkCompatibility(y.compiler); if (Double.isInfinite(data[0]) || Double.isInfinite(y.data[0])) { return new DerivativeStructure(compiler.getFreeParameters(), compiler.getFreeParameters(), Double.POSITIVE_INFINITY); } else if (Double.isNaN(data[0]) || Double.isNaN(y.data[0])) { return new DerivativeStructure(compiler.getFreeParameters(), compiler.getFreeParameters(), Double.NaN); } else { final int expX = getExponent(); final int expY = y.getExponent(); if (expX > expY + 27) { // y is neglectible with respect to x return abs(); } else if (expY > expX + 27) { // x is neglectible with respect to y return y.abs(); } else { // find an intermediate scale to avoid both overflow and underflow final int middleExp = (expX + expY) / 2; // scale parameters without losing precision final DerivativeStructure scaledX = scalb(-middleExp); final DerivativeStructure scaledY = y.scalb(-middleExp); // compute scaled hypotenuse final DerivativeStructure scaledH = scaledX.multiply(scaledX).add(scaledY.multiply(scaledY)).sqrt(); // remove scaling return scaledH.scalb(middleExp); } } } /** * Returns the hypotenuse of a triangle with sides {@code x} and {@code y} * - sqrt(x2 +y2)
                            * avoiding intermediate overflow or underflow. * *
                              *
                            • If either argument is infinite, then the result is positive infinity.
                            • *
                            • else, if either argument is NaN then the result is NaN.
                            • *
                            * * @param x a value * @param y a value * @return sqrt(x2 +y2) * @exception DimensionMismatchException if number of free parameters * or orders do not match * @since 3.2 */ public static DerivativeStructure hypot(final DerivativeStructure x, final DerivativeStructure y) throws DimensionMismatchException { return x.hypot(y); } /** Compute composition of the instance by a univariate function. * @param f array of value and derivatives of the function at * the current point (i.e. [f({@link #getValue()}), * f'({@link #getValue()}), f''({@link #getValue()})...]). * @return f(this) * @exception DimensionMismatchException if the number of derivatives * in the array is not equal to {@link #getOrder() order} + 1 */ public DerivativeStructure compose(final double ... f) throws DimensionMismatchException { if (f.length != getOrder() + 1) { throw new DimensionMismatchException(f.length, getOrder() + 1); } final DerivativeStructure result = new DerivativeStructure(compiler); compiler.compose(data, 0, f, result.data, 0); return result; } /** {@inheritDoc} */ public DerivativeStructure reciprocal() { final DerivativeStructure result = new DerivativeStructure(compiler); compiler.pow(data, 0, -1, result.data, 0); return result; } /** {@inheritDoc} * @since 3.2 */ public DerivativeStructure sqrt() { return rootN(2); } /** {@inheritDoc} * @since 3.2 */ public DerivativeStructure cbrt() { return rootN(3); } /** {@inheritDoc} * @since 3.2 */ public DerivativeStructure rootN(final int n) { final DerivativeStructure result = new DerivativeStructure(compiler); compiler.rootN(data, 0, n, result.data, 0); return result; } /** {@inheritDoc} */ public Field getField() { return new Field() { /** {@inheritDoc} */ public DerivativeStructure getZero() { return new DerivativeStructure(compiler.getFreeParameters(), compiler.getOrder(), 0.0); } /** {@inheritDoc} */ public DerivativeStructure getOne() { return new DerivativeStructure(compiler.getFreeParameters(), compiler.getOrder(), 1.0); } /** {@inheritDoc} */ public Class> getRuntimeClass() { return DerivativeStructure.class; } }; } /** {@inheritDoc} * @since 3.2 */ public DerivativeStructure pow(final double p) { final DerivativeStructure result = new DerivativeStructure(compiler); compiler.pow(data, 0, p, result.data, 0); return result; } /** {@inheritDoc} * @since 3.2 */ public DerivativeStructure pow(final int n) { final DerivativeStructure result = new DerivativeStructure(compiler); compiler.pow(data, 0, n, result.data, 0); return result; } /** {@inheritDoc} * @exception DimensionMismatchException if number of free parameters * or orders do not match * @since 3.2 */ public DerivativeStructure pow(final DerivativeStructure e) throws DimensionMismatchException { compiler.checkCompatibility(e.compiler); final DerivativeStructure result = new DerivativeStructure(compiler); compiler.pow(data, 0, e.data, 0, result.data, 0); return result; } /** {@inheritDoc} * @since 3.2 */ public DerivativeStructure exp() { final DerivativeStructure result = new DerivativeStructure(compiler); compiler.exp(data, 0, result.data, 0); return result; } /** {@inheritDoc} * @since 3.2 */ public DerivativeStructure expm1() { final DerivativeStructure result = new DerivativeStructure(compiler); compiler.expm1(data, 0, result.data, 0); return result; } /** {@inheritDoc} * @since 3.2 */ public DerivativeStructure log() { final DerivativeStructure result = new DerivativeStructure(compiler); compiler.log(data, 0, result.data, 0); return result; } /** {@inheritDoc} * @since 3.2 */ public DerivativeStructure log1p() { final DerivativeStructure result = new DerivativeStructure(compiler); compiler.log1p(data, 0, result.data, 0); return result; } /** Base 10 logarithm. * @return base 10 logarithm of the instance */ public DerivativeStructure log10() { final DerivativeStructure result = new DerivativeStructure(compiler); compiler.log10(data, 0, result.data, 0); return result; } /** {@inheritDoc} * @since 3.2 */ public DerivativeStructure cos() { final DerivativeStructure result = new DerivativeStructure(compiler); compiler.cos(data, 0, result.data, 0); return result; } /** {@inheritDoc} * @since 3.2 */ public DerivativeStructure sin() { final DerivativeStructure result = new DerivativeStructure(compiler); compiler.sin(data, 0, result.data, 0); return result; } /** {@inheritDoc} * @since 3.2 */ public DerivativeStructure tan() { final DerivativeStructure result = new DerivativeStructure(compiler); compiler.tan(data, 0, result.data, 0); return result; } /** {@inheritDoc} * @since 3.2 */ public DerivativeStructure acos() { final DerivativeStructure result = new DerivativeStructure(compiler); compiler.acos(data, 0, result.data, 0); return result; } /** {@inheritDoc} * @since 3.2 */ public DerivativeStructure asin() { final DerivativeStructure result = new DerivativeStructure(compiler); compiler.asin(data, 0, result.data, 0); return result; } /** {@inheritDoc} * @since 3.2 */ public DerivativeStructure atan() { final DerivativeStructure result = new DerivativeStructure(compiler); compiler.atan(data, 0, result.data, 0); return result; } /** {@inheritDoc} * @since 3.2 */ public DerivativeStructure atan2(final DerivativeStructure x) throws DimensionMismatchException { compiler.checkCompatibility(x.compiler); final DerivativeStructure result = new DerivativeStructure(compiler); compiler.atan2(data, 0, x.data, 0, result.data, 0); return result; } /** Two arguments arc tangent operation. * @param y first argument of the arc tangent * @param x second argument of the arc tangent * @return atan2(y, x) * @exception DimensionMismatchException if number of free parameters * or orders do not match * @since 3.2 */ public static DerivativeStructure atan2(final DerivativeStructure y, final DerivativeStructure x) throws DimensionMismatchException { return y.atan2(x); } /** {@inheritDoc} * @since 3.2 */ public DerivativeStructure cosh() { final DerivativeStructure result = new DerivativeStructure(compiler); compiler.cosh(data, 0, result.data, 0); return result; } /** {@inheritDoc} * @since 3.2 */ public DerivativeStructure sinh() { final DerivativeStructure result = new DerivativeStructure(compiler); compiler.sinh(data, 0, result.data, 0); return result; } /** {@inheritDoc} * @since 3.2 */ public DerivativeStructure tanh() { final DerivativeStructure result = new DerivativeStructure(compiler); compiler.tanh(data, 0, result.data, 0); return result; } /** {@inheritDoc} * @since 3.2 */ public DerivativeStructure acosh() { final DerivativeStructure result = new DerivativeStructure(compiler); compiler.acosh(data, 0, result.data, 0); return result; } /** {@inheritDoc} * @since 3.2 */ public DerivativeStructure asinh() { final DerivativeStructure result = new DerivativeStructure(compiler); compiler.asinh(data, 0, result.data, 0); return result; } /** {@inheritDoc} * @since 3.2 */ public DerivativeStructure atanh() { final DerivativeStructure result = new DerivativeStructure(compiler); compiler.atanh(data, 0, result.data, 0); return result; } /** Convert radians to degrees, with error of less than 0.5 ULP * @return instance converted into degrees */ public DerivativeStructure toDegrees() { final DerivativeStructure ds = new DerivativeStructure(compiler); for (int i = 0; i < ds.data.length; ++i) { ds.data[i] = FastMath.toDegrees(data[i]); } return ds; } /** Convert degrees to radians, with error of less than 0.5 ULP * @return instance converted into radians */ public DerivativeStructure toRadians() { final DerivativeStructure ds = new DerivativeStructure(compiler); for (int i = 0; i < ds.data.length; ++i) { ds.data[i] = FastMath.toRadians(data[i]); } return ds; } /** Evaluate Taylor expansion a derivative structure. * @param delta parameters offsets (Δx, Δy, ...) * @return value of the Taylor expansion at x + Δx, y + Δy, ... * @throws MathArithmeticException if factorials becomes too large */ public double taylor(final double ... delta) throws MathArithmeticException { return compiler.taylor(data, 0, delta); } /** {@inheritDoc} * @exception DimensionMismatchException if number of free parameters * or orders do not match * @since 3.2 */ public DerivativeStructure linearCombination(final DerivativeStructure[] a, final DerivativeStructure[] b) throws DimensionMismatchException { // compute an accurate value, taking care of cancellations final double[] aDouble = new double[a.length]; for (int i = 0; i < a.length; ++i) { aDouble[i] = a[i].getValue(); } final double[] bDouble = new double[b.length]; for (int i = 0; i < b.length; ++i) { bDouble[i] = b[i].getValue(); } final double accurateValue = MathArrays.linearCombination(aDouble, bDouble); // compute a simple value, with all partial derivatives DerivativeStructure simpleValue = a[0].getField().getZero(); for (int i = 0; i < a.length; ++i) { simpleValue = simpleValue.add(a[i].multiply(b[i])); } // create a result with accurate value and all derivatives (not necessarily as accurate as the value) final double[] all = simpleValue.getAllDerivatives(); all[0] = accurateValue; return new DerivativeStructure(simpleValue.getFreeParameters(), simpleValue.getOrder(), all); } /** {@inheritDoc} * @exception DimensionMismatchException if number of free parameters * or orders do not match * @since 3.2 */ public DerivativeStructure linearCombination(final double[] a, final DerivativeStructure[] b) throws DimensionMismatchException { // compute an accurate value, taking care of cancellations final double[] bDouble = new double[b.length]; for (int i = 0; i < b.length; ++i) { bDouble[i] = b[i].getValue(); } final double accurateValue = MathArrays.linearCombination(a, bDouble); // compute a simple value, with all partial derivatives DerivativeStructure simpleValue = b[0].getField().getZero(); for (int i = 0; i < a.length; ++i) { simpleValue = simpleValue.add(b[i].multiply(a[i])); } // create a result with accurate value and all derivatives (not necessarily as accurate as the value) final double[] all = simpleValue.getAllDerivatives(); all[0] = accurateValue; return new DerivativeStructure(simpleValue.getFreeParameters(), simpleValue.getOrder(), all); } /** {@inheritDoc} * @exception DimensionMismatchException if number of free parameters * or orders do not match * @since 3.2 */ public DerivativeStructure linearCombination(final DerivativeStructure a1, final DerivativeStructure b1, final DerivativeStructure a2, final DerivativeStructure b2) throws DimensionMismatchException { // compute an accurate value, taking care of cancellations final double accurateValue = MathArrays.linearCombination(a1.getValue(), b1.getValue(), a2.getValue(), b2.getValue()); // compute a simple value, with all partial derivatives final DerivativeStructure simpleValue = a1.multiply(b1).add(a2.multiply(b2)); // create a result with accurate value and all derivatives (not necessarily as accurate as the value) final double[] all = simpleValue.getAllDerivatives(); all[0] = accurateValue; return new DerivativeStructure(getFreeParameters(), getOrder(), all); } /** {@inheritDoc} * @exception DimensionMismatchException if number of free parameters * or orders do not match * @since 3.2 */ public DerivativeStructure linearCombination(final double a1, final DerivativeStructure b1, final double a2, final DerivativeStructure b2) throws DimensionMismatchException { // compute an accurate value, taking care of cancellations final double accurateValue = MathArrays.linearCombination(a1, b1.getValue(), a2, b2.getValue()); // compute a simple value, with all partial derivatives final DerivativeStructure simpleValue = b1.multiply(a1).add(b2.multiply(a2)); // create a result with accurate value and all derivatives (not necessarily as accurate as the value) final double[] all = simpleValue.getAllDerivatives(); all[0] = accurateValue; return new DerivativeStructure(getFreeParameters(), getOrder(), all); } /** {@inheritDoc} * @exception DimensionMismatchException if number of free parameters * or orders do not match * @since 3.2 */ public DerivativeStructure linearCombination(final DerivativeStructure a1, final DerivativeStructure b1, final DerivativeStructure a2, final DerivativeStructure b2, final DerivativeStructure a3, final DerivativeStructure b3) throws DimensionMismatchException { // compute an accurate value, taking care of cancellations final double accurateValue = MathArrays.linearCombination(a1.getValue(), b1.getValue(), a2.getValue(), b2.getValue(), a3.getValue(), b3.getValue()); // compute a simple value, with all partial derivatives final DerivativeStructure simpleValue = a1.multiply(b1).add(a2.multiply(b2)).add(a3.multiply(b3)); // create a result with accurate value and all derivatives (not necessarily as accurate as the value) final double[] all = simpleValue.getAllDerivatives(); all[0] = accurateValue; return new DerivativeStructure(getFreeParameters(), getOrder(), all); } /** {@inheritDoc} * @exception DimensionMismatchException if number of free parameters * or orders do not match * @since 3.2 */ public DerivativeStructure linearCombination(final double a1, final DerivativeStructure b1, final double a2, final DerivativeStructure b2, final double a3, final DerivativeStructure b3) throws DimensionMismatchException { // compute an accurate value, taking care of cancellations final double accurateValue = MathArrays.linearCombination(a1, b1.getValue(), a2, b2.getValue(), a3, b3.getValue()); // compute a simple value, with all partial derivatives final DerivativeStructure simpleValue = b1.multiply(a1).add(b2.multiply(a2)).add(b3.multiply(a3)); // create a result with accurate value and all derivatives (not necessarily as accurate as the value) final double[] all = simpleValue.getAllDerivatives(); all[0] = accurateValue; return new DerivativeStructure(getFreeParameters(), getOrder(), all); } /** {@inheritDoc} * @exception DimensionMismatchException if number of free parameters * or orders do not match * @since 3.2 */ public DerivativeStructure linearCombination(final DerivativeStructure a1, final DerivativeStructure b1, final DerivativeStructure a2, final DerivativeStructure b2, final DerivativeStructure a3, final DerivativeStructure b3, final DerivativeStructure a4, final DerivativeStructure b4) throws DimensionMismatchException { // compute an accurate value, taking care of cancellations final double accurateValue = MathArrays.linearCombination(a1.getValue(), b1.getValue(), a2.getValue(), b2.getValue(), a3.getValue(), b3.getValue(), a4.getValue(), b4.getValue()); // compute a simple value, with all partial derivatives final DerivativeStructure simpleValue = a1.multiply(b1).add(a2.multiply(b2)).add(a3.multiply(b3)).add(a4.multiply(b4)); // create a result with accurate value and all derivatives (not necessarily as accurate as the value) final double[] all = simpleValue.getAllDerivatives(); all[0] = accurateValue; return new DerivativeStructure(getFreeParameters(), getOrder(), all); } /** {@inheritDoc} * @exception DimensionMismatchException if number of free parameters * or orders do not match * @since 3.2 */ public DerivativeStructure linearCombination(final double a1, final DerivativeStructure b1, final double a2, final DerivativeStructure b2, final double a3, final DerivativeStructure b3, final double a4, final DerivativeStructure b4) throws DimensionMismatchException { // compute an accurate value, taking care of cancellations final double accurateValue = MathArrays.linearCombination(a1, b1.getValue(), a2, b2.getValue(), a3, b3.getValue(), a4, b4.getValue()); // compute a simple value, with all partial derivatives final DerivativeStructure simpleValue = b1.multiply(a1).add(b2.multiply(a2)).add(b3.multiply(a3)).add(b4.multiply(a4)); // create a result with accurate value and all derivatives (not necessarily as accurate as the value) final double[] all = simpleValue.getAllDerivatives(); all[0] = accurateValue; return new DerivativeStructure(getFreeParameters(), getOrder(), all); } /** * Test for the equality of two derivative structures. *

                            * Derivative structures are considered equal if they have the same number * of free parameters, the same derivation order, and the same derivatives. *

                            * @param other Object to test for equality to this * @return true if two derivative structures are equal * @since 3.2 */ @Override public boolean equals(Object other) { if (this == other) { return true; } if (other instanceof DerivativeStructure) { final DerivativeStructure rhs = (DerivativeStructure)other; return (getFreeParameters() == rhs.getFreeParameters()) && (getOrder() == rhs.getOrder()) && MathArrays.equals(data, rhs.data); } return false; } /** * Get a hashCode for the derivative structure. * @return a hash code value for this object * @since 3.2 */ @Override public int hashCode() { return 227 + 229 * getFreeParameters() + 233 * getOrder() + 239 * MathUtils.hash(data); } /** * Replace the instance with a data transfer object for serialization. * @return data transfer object that will be serialized */ private Object writeReplace() { return new DataTransferObject(compiler.getFreeParameters(), compiler.getOrder(), data); } /** Internal class used only for serialization. */ private static class DataTransferObject implements Serializable { /** Serializable UID. */ private static final long serialVersionUID = 20120730L; /** Number of variables. * @serial */ private final int variables; /** Derivation order. * @serial */ private final int order; /** Partial derivatives. * @serial */ private final double[] data; /** Simple constructor. * @param variables number of variables * @param order derivation order * @param data partial derivatives */ public DataTransferObject(final int variables, final int order, final double[] data) { this.variables = variables; this.order = order; this.data = data; } /** Replace the deserialized data transfer object with a {@link DerivativeStructure}. * @return replacement {@link DerivativeStructure} */ private Object readResolve() { return new DerivativeStructure(variables, order, data); } } } ././@LongLink100644 0 0 204 12126630646 10255 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/analysis/differentiation/MultivariateDifferentiableVectorFunction.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/analysis/differentiation/MultivariateDi100644 1750 1750 3503 12126627706 32371 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.analysis.differentiation; import org.apache.commons.math3.analysis.MultivariateVectorFunction; import org.apache.commons.math3.exception.MathIllegalArgumentException; /** * Extension of {@link MultivariateVectorFunction} representing a * multivariate differentiable vectorial function. * @version $Id: MultivariateDifferentiableVectorFunction.java 1462496 2013-03-29 14:56:08Z psteitz $ * @since 3.1 */ public interface MultivariateDifferentiableVectorFunction extends MultivariateVectorFunction { /** * Compute the value for the function at the given point. * @param point point at which the function must be evaluated * @return function value for the given point * @exception MathIllegalArgumentException if {@code point} does not * satisfy the function's constraints (wrong dimension, argument out of bound, * or unsupported derivative order for example) */ DerivativeStructure[] value(DerivativeStructure[] point) throws MathIllegalArgumentException; } ././@LongLink100644 0 0 174 12126630646 10263 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/analysis/differentiation/UnivariateFunctionDifferentiator.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/analysis/differentiation/UnivariateFunc100644 1750 1750 2705 12126627706 32374 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.analysis.differentiation; import org.apache.commons.math3.analysis.UnivariateFunction; /** Interface defining the function differentiation operation. * @version $Id: UnivariateFunctionDifferentiator.java 1416643 2012-12-03 19:37:14Z tn $ * @since 3.1 */ public interface UnivariateFunctionDifferentiator { /** Create an implementation of a {@link UnivariateDifferentiableFunction * differential} from a regular {@link UnivariateFunction function}. * @param function function to differentiate * @return differential function */ UnivariateDifferentiableFunction differentiate(UnivariateFunction function); } ././@LongLink100644 0 0 173 12126630646 10262 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/analysis/differentiation/FiniteDifferencesDifferentiator.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/analysis/differentiation/FiniteDifferen100644 1750 1750 37370 12126627706 32360 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.analysis.differentiation; import java.io.Serializable; import org.apache.commons.math3.analysis.UnivariateFunction; import org.apache.commons.math3.analysis.UnivariateMatrixFunction; import org.apache.commons.math3.analysis.UnivariateVectorFunction; import org.apache.commons.math3.exception.MathIllegalArgumentException; import org.apache.commons.math3.exception.NotPositiveException; import org.apache.commons.math3.exception.NumberIsTooLargeException; import org.apache.commons.math3.exception.NumberIsTooSmallException; import org.apache.commons.math3.util.FastMath; /** Univariate functions differentiator using finite differences. *

                            * This class creates some wrapper objects around regular * {@link UnivariateFunction univariate functions} (or {@link * UnivariateVectorFunction univariate vector functions} or {@link * UnivariateMatrixFunction univariate matrix functions}). These * wrapper objects compute derivatives in addition to function * value. *

                            *

                            * The wrapper objects work by calling the underlying function on * a sampling grid around the current point and performing polynomial * interpolation. A finite differences scheme with n points is * theoretically able to compute derivatives up to order n-1, but * it is generally better to have a slight margin. The step size must * also be small enough in order for the polynomial approximation to * be good in the current point neighborhood, but it should not be too * small because numerical instability appears quickly (there are several * differences of close points). Choosing the number of points and * the step size is highly problem dependent. *

                            *

                            * As an example of good and bad settings, lets consider the quintic * polynomial function {@code f(x) = (x-1)*(x-0.5)*x*(x+0.5)*(x+1)}. * Since it is a polynomial, finite differences with at least 6 points * should theoretically recover the exact same polynomial and hence * compute accurate derivatives for any order. However, due to numerical * errors, we get the following results for a 7 points finite differences * for abscissae in the [-10, 10] range: *

                              *
                            • step size = 0.25, second order derivative error about 9.97e-10
                            • *
                            • step size = 0.25, fourth order derivative error about 5.43e-8
                            • *
                            • step size = 1.0e-6, second order derivative error about 148
                            • *
                            • step size = 1.0e-6, fourth order derivative error about 6.35e+14
                            • *
                            * This example shows that the small step size is really bad, even simply * for second order derivative! *

                            * @version $Id: FiniteDifferencesDifferentiator.java 1420666 2012-12-12 13:33:20Z erans $ * @since 3.1 */ public class FiniteDifferencesDifferentiator implements UnivariateFunctionDifferentiator, UnivariateVectorFunctionDifferentiator, UnivariateMatrixFunctionDifferentiator, Serializable { /** Serializable UID. */ private static final long serialVersionUID = 20120917L; /** Number of points to use. */ private final int nbPoints; /** Step size. */ private final double stepSize; /** Half sample span. */ private final double halfSampleSpan; /** Lower bound for independent variable. */ private final double tMin; /** Upper bound for independent variable. */ private final double tMax; /** * Build a differentiator with number of points and step size when independent variable is unbounded. *

                            * Beware that wrong settings for the finite differences differentiator * can lead to highly unstable and inaccurate results, especially for * high derivation orders. Using very small step sizes is often a * bad idea. *

                            * @param nbPoints number of points to use * @param stepSize step size (gap between each point) * @exception NotPositiveException if {@code stepsize <= 0} (note that * {@link NotPositiveException} extends {@link NumberIsTooSmallException}) * @exception NumberIsTooSmallException {@code nbPoint <= 1} */ public FiniteDifferencesDifferentiator(final int nbPoints, final double stepSize) throws NotPositiveException, NumberIsTooSmallException { this(nbPoints, stepSize, Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY); } /** * Build a differentiator with number of points and step size when independent variable is bounded. *

                            * When the independent variable is bounded (tLower < t < tUpper), the sampling * points used for differentiation will be adapted to ensure the constraint holds * even near the boundaries. This means the sample will not be centered anymore in * these cases. At an extreme case, computing derivatives exactly at the lower bound * will lead the sample to be entirely on the right side of the derivation point. *

                            *

                            * Note that the boundaries are considered to be excluded for function evaluation. *

                            *

                            * Beware that wrong settings for the finite differences differentiator * can lead to highly unstable and inaccurate results, especially for * high derivation orders. Using very small step sizes is often a * bad idea. *

                            * @param nbPoints number of points to use * @param stepSize step size (gap between each point) * @param tLower lower bound for independent variable (may be {@code Double.NEGATIVE_INFINITY} * if there are no lower bounds) * @param tUpper upper bound for independent variable (may be {@code Double.POSITIVE_INFINITY} * if there are no upper bounds) * @exception NotPositiveException if {@code stepsize <= 0} (note that * {@link NotPositiveException} extends {@link NumberIsTooSmallException}) * @exception NumberIsTooSmallException {@code nbPoint <= 1} * @exception NumberIsTooLargeException {@code stepSize * (nbPoints - 1) >= tUpper - tLower} */ public FiniteDifferencesDifferentiator(final int nbPoints, final double stepSize, final double tLower, final double tUpper) throws NotPositiveException, NumberIsTooSmallException, NumberIsTooLargeException { if (nbPoints <= 1) { throw new NumberIsTooSmallException(stepSize, 1, false); } this.nbPoints = nbPoints; if (stepSize <= 0) { throw new NotPositiveException(stepSize); } this.stepSize = stepSize; halfSampleSpan = 0.5 * stepSize * (nbPoints - 1); if (2 * halfSampleSpan >= tUpper - tLower) { throw new NumberIsTooLargeException(2 * halfSampleSpan, tUpper - tLower, false); } final double safety = FastMath.ulp(halfSampleSpan); this.tMin = tLower + halfSampleSpan + safety; this.tMax = tUpper - halfSampleSpan - safety; } /** * Get the number of points to use. * @return number of points to use */ public int getNbPoints() { return nbPoints; } /** * Get the step size. * @return step size */ public double getStepSize() { return stepSize; } /** * Evaluate derivatives from a sample. *

                            * Evaluation is done using divided differences. *

                            * @param t evaluation abscissa value and derivatives * @param t0 first sample point abscissa * @param y function values sample {@code y[i] = f(t[i]) = f(t0 + i * stepSize)} * @return value and derivatives at {@code t} * @exception NumberIsTooLargeException if the requested derivation order * is larger or equal to the number of points */ private DerivativeStructure evaluate(final DerivativeStructure t, final double t0, final double[] y) throws NumberIsTooLargeException { // create divided differences diagonal arrays final double[] top = new double[nbPoints]; final double[] bottom = new double[nbPoints]; for (int i = 0; i < nbPoints; ++i) { // update the bottom diagonal of the divided differences array bottom[i] = y[i]; for (int j = 1; j <= i; ++j) { bottom[i - j] = (bottom[i - j + 1] - bottom[i - j]) / (j * stepSize); } // update the top diagonal of the divided differences array top[i] = bottom[0]; } // evaluate interpolation polynomial (represented by top diagonal) at t final int order = t.getOrder(); final int parameters = t.getFreeParameters(); final double[] derivatives = t.getAllDerivatives(); final double dt0 = t.getValue() - t0; DerivativeStructure interpolation = new DerivativeStructure(parameters, order, 0.0); DerivativeStructure monomial = null; for (int i = 0; i < nbPoints; ++i) { if (i == 0) { // start with monomial(t) = 1 monomial = new DerivativeStructure(parameters, order, 1.0); } else { // monomial(t) = (t - t0) * (t - t1) * ... * (t - t(i-1)) derivatives[0] = dt0 - (i - 1) * stepSize; final DerivativeStructure deltaX = new DerivativeStructure(parameters, order, derivatives); monomial = monomial.multiply(deltaX); } interpolation = interpolation.add(monomial.multiply(top[i])); } return interpolation; } /** {@inheritDoc} *

                            The returned object cannot compute derivatives to arbitrary orders. The * value function will throw a {@link NumberIsTooLargeException} if the requested * derivation order is larger or equal to the number of points. *

                            */ public UnivariateDifferentiableFunction differentiate(final UnivariateFunction function) { return new UnivariateDifferentiableFunction() { /** {@inheritDoc} */ public double value(final double x) throws MathIllegalArgumentException { return function.value(x); } /** {@inheritDoc} */ public DerivativeStructure value(final DerivativeStructure t) throws MathIllegalArgumentException { // check we can achieve the requested derivation order with the sample if (t.getOrder() >= nbPoints) { throw new NumberIsTooLargeException(t.getOrder(), nbPoints, false); } // compute sample position, trying to be centered if possible final double t0 = FastMath.max(FastMath.min(t.getValue(), tMax), tMin) - halfSampleSpan; // compute sample points final double[] y = new double[nbPoints]; for (int i = 0; i < nbPoints; ++i) { y[i] = function.value(t0 + i * stepSize); } // evaluate derivatives return evaluate(t, t0, y); } }; } /** {@inheritDoc} *

                            The returned object cannot compute derivatives to arbitrary orders. The * value function will throw a {@link NumberIsTooLargeException} if the requested * derivation order is larger or equal to the number of points. *

                            */ public UnivariateDifferentiableVectorFunction differentiate(final UnivariateVectorFunction function) { return new UnivariateDifferentiableVectorFunction() { /** {@inheritDoc} */ public double[]value(final double x) throws MathIllegalArgumentException { return function.value(x); } /** {@inheritDoc} */ public DerivativeStructure[] value(final DerivativeStructure t) throws MathIllegalArgumentException { // check we can achieve the requested derivation order with the sample if (t.getOrder() >= nbPoints) { throw new NumberIsTooLargeException(t.getOrder(), nbPoints, false); } // compute sample position, trying to be centered if possible final double t0 = FastMath.max(FastMath.min(t.getValue(), tMax), tMin) - halfSampleSpan; // compute sample points double[][] y = null; for (int i = 0; i < nbPoints; ++i) { final double[] v = function.value(t0 + i * stepSize); if (i == 0) { y = new double[v.length][nbPoints]; } for (int j = 0; j < v.length; ++j) { y[j][i] = v[j]; } } // evaluate derivatives final DerivativeStructure[] value = new DerivativeStructure[y.length]; for (int j = 0; j < value.length; ++j) { value[j] = evaluate(t, t0, y[j]); } return value; } }; } /** {@inheritDoc} *

                            The returned object cannot compute derivatives to arbitrary orders. The * value function will throw a {@link NumberIsTooLargeException} if the requested * derivation order is larger or equal to the number of points. *

                            */ public UnivariateDifferentiableMatrixFunction differentiate(final UnivariateMatrixFunction function) { return new UnivariateDifferentiableMatrixFunction() { /** {@inheritDoc} */ public double[][] value(final double x) throws MathIllegalArgumentException { return function.value(x); } /** {@inheritDoc} */ public DerivativeStructure[][] value(final DerivativeStructure t) throws MathIllegalArgumentException { // check we can achieve the requested derivation order with the sample if (t.getOrder() >= nbPoints) { throw new NumberIsTooLargeException(t.getOrder(), nbPoints, false); } // compute sample position, trying to be centered if possible final double t0 = FastMath.max(FastMath.min(t.getValue(), tMax), tMin) - halfSampleSpan; // compute sample points double[][][] y = null; for (int i = 0; i < nbPoints; ++i) { final double[][] v = function.value(t0 + i * stepSize); if (i == 0) { y = new double[v.length][v[0].length][nbPoints]; } for (int j = 0; j < v.length; ++j) { for (int k = 0; k < v[j].length; ++k) { y[j][k][i] = v[j][k]; } } } // evaluate derivatives final DerivativeStructure[][] value = new DerivativeStructure[y.length][y[0].length]; for (int j = 0; j < value.length; ++j) { for (int k = 0; k < y[j].length; ++k) { value[j][k] = evaluate(t, t0, y[j][k]); } } return value; } }; } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/analysis/solvers/package-info.java100644 1750 1750 1643 12126627704 31236 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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 algorithms, for univariate real functions. * */ package org.apache.commons.math3.analysis.solvers; commons-math3-3.2-src/src/main/java/org/apache/commons/math3/analysis/solvers/PolynomialSolver.java100644 1750 1750 2334 12126627704 32226 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.analysis.solvers; import org.apache.commons.math3.analysis.polynomials.PolynomialFunction; /** * Interface for (polynomial) root-finding algorithms. * Implementations will search for only one zero in the given interval. * * @since 3.0 * @version $Id: PolynomialSolver.java 1364387 2012-07-22 18:14:11Z tn $ */ public interface PolynomialSolver extends BaseUnivariateSolver {} ././@LongLink100644 0 0 151 12126630646 10256 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/analysis/solvers/UnivariateSolverUtils.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/analysis/solvers/UnivariateSolverUtils.100644 1750 1750 40466 12126627704 32421 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.analysis.solvers; import org.apache.commons.math3.analysis.UnivariateFunction; import org.apache.commons.math3.exception.NoBracketingException; import org.apache.commons.math3.exception.NotStrictlyPositiveException; import org.apache.commons.math3.exception.NullArgumentException; import org.apache.commons.math3.exception.NumberIsTooLargeException; import org.apache.commons.math3.exception.util.LocalizedFormats; import org.apache.commons.math3.util.FastMath; /** * Utility routines for {@link UnivariateSolver} objects. * * @version $Id: UnivariateSolverUtils.java 1400850 2012-10-22 11:57:17Z erans $ */ public class UnivariateSolverUtils { /** * Class contains only static methods. */ private UnivariateSolverUtils() {} /** * Convenience method to find a zero of a univariate real function. A default * solver is used. * * @param function Function. * @param x0 Lower bound for the interval. * @param x1 Upper bound for the interval. * @return a value where the function is zero. * @throws NoBracketingException if the function has the same sign at the * endpoints. * @throws NullArgumentException if {@code function} is {@code null}. */ public static double solve(UnivariateFunction function, double x0, double x1) throws NullArgumentException, NoBracketingException { if (function == null) { throw new NullArgumentException(LocalizedFormats.FUNCTION); } final UnivariateSolver solver = new BrentSolver(); return solver.solve(Integer.MAX_VALUE, function, x0, x1); } /** * Convenience method to find a zero of a univariate real function. A default * solver is used. * * @param function Function. * @param x0 Lower bound for the interval. * @param x1 Upper bound for the interval. * @param absoluteAccuracy Accuracy to be used by the solver. * @return a value where the function is zero. * @throws NoBracketingException if the function has the same sign at the * endpoints. * @throws NullArgumentException if {@code function} is {@code null}. */ public static double solve(UnivariateFunction function, double x0, double x1, double absoluteAccuracy) throws NullArgumentException, NoBracketingException { if (function == null) { throw new NullArgumentException(LocalizedFormats.FUNCTION); } final UnivariateSolver solver = new BrentSolver(absoluteAccuracy); return solver.solve(Integer.MAX_VALUE, function, x0, x1); } /** Force a root found by a non-bracketing solver to lie on a specified side, * as if the solver was a bracketing one. * @param maxEval maximal number of new evaluations of the function * (evaluations already done for finding the root should have already been subtracted * from this number) * @param f function to solve * @param bracketing bracketing solver to use for shifting the root * @param baseRoot original root found by a previous non-bracketing solver * @param min minimal bound of the search interval * @param max maximal bound of the search interval * @param allowedSolution the kind of solutions that the root-finding algorithm may * accept as solutions. * @return a root approximation, on the specified side of the exact root * @throws NoBracketingException if the function has the same sign at the * endpoints. */ public static double forceSide(final int maxEval, final UnivariateFunction f, final BracketedUnivariateSolver bracketing, final double baseRoot, final double min, final double max, final AllowedSolution allowedSolution) throws NoBracketingException { if (allowedSolution == AllowedSolution.ANY_SIDE) { // no further bracketing required return baseRoot; } // find a very small interval bracketing the root final double step = FastMath.max(bracketing.getAbsoluteAccuracy(), FastMath.abs(baseRoot * bracketing.getRelativeAccuracy())); double xLo = FastMath.max(min, baseRoot - step); double fLo = f.value(xLo); double xHi = FastMath.min(max, baseRoot + step); double fHi = f.value(xHi); int remainingEval = maxEval - 2; while (remainingEval > 0) { if ((fLo >= 0 && fHi <= 0) || (fLo <= 0 && fHi >= 0)) { // compute the root on the selected side return bracketing.solve(remainingEval, f, xLo, xHi, baseRoot, allowedSolution); } // try increasing the interval boolean changeLo = false; boolean changeHi = false; if (fLo < fHi) { // increasing function if (fLo >= 0) { changeLo = true; } else { changeHi = true; } } else if (fLo > fHi) { // decreasing function if (fLo <= 0) { changeLo = true; } else { changeHi = true; } } else { // unknown variation changeLo = true; changeHi = true; } // update the lower bound if (changeLo) { xLo = FastMath.max(min, xLo - step); fLo = f.value(xLo); remainingEval--; } // update the higher bound if (changeHi) { xHi = FastMath.min(max, xHi + step); fHi = f.value(xHi); remainingEval--; } } throw new NoBracketingException(LocalizedFormats.FAILED_BRACKETING, xLo, xHi, fLo, fHi, maxEval - remainingEval, maxEval, baseRoot, min, max); } /** * This method attempts to find two values a and b satisfying
                              *
                            • lowerBound <= a < initial < b <= upperBound
                            • *
                            • f(a) * f(b) < 0
                            • *
                            * If f is continuous on [a,b], this means that a * and b bracket a root of f. *

                            * The algorithm starts by setting * a := initial -1; b := initial +1, examines the value of the * function at a and b and keeps moving * the endpoints out by one unit each time through a loop that terminates * when one of the following happens:

                              *
                            • f(a) * f(b) < 0 -- success!
                            • *
                            • a = lower and b = upper * -- NoBracketingException
                            • *
                            • Integer.MAX_VALUE iterations elapse * -- NoBracketingException
                            • *

                            *

                            * Note: this method can take * Integer.MAX_VALUE iterations to throw a * ConvergenceException. Unless you are confident that there * is a root between lowerBound and upperBound * near initial, it is better to use * {@link #bracket(UnivariateFunction, double, double, double, int)}, * explicitly specifying the maximum number of iterations.

                            * * @param function Function. * @param initial Initial midpoint of interval being expanded to * bracket a root. * @param lowerBound Lower bound (a is never lower than this value) * @param upperBound Upper bound (b never is greater than this * value). * @return a two-element array holding a and b. * @throws NoBracketingException if a root cannot be bracketted. * @throws NotStrictlyPositiveException if {@code maximumIterations <= 0}. * @throws NullArgumentException if {@code function} is {@code null}. */ public static double[] bracket(UnivariateFunction function, double initial, double lowerBound, double upperBound) throws NullArgumentException, NotStrictlyPositiveException, NoBracketingException { return bracket(function, initial, lowerBound, upperBound, Integer.MAX_VALUE); } /** * This method attempts to find two values a and b satisfying
                              *
                            • lowerBound <= a < initial < b <= upperBound
                            • *
                            • f(a) * f(b) <= 0
                            • *
                            * If f is continuous on [a,b], this means that a * and b bracket a root of f. *

                            * The algorithm starts by setting * a := initial -1; b := initial +1, examines the value of the * function at a and b and keeps moving * the endpoints out by one unit each time through a loop that terminates * when one of the following happens:

                              *
                            • f(a) * f(b) <= 0 -- success!
                            • *
                            • a = lower and b = upper * -- NoBracketingException
                            • *
                            • maximumIterations iterations elapse * -- NoBracketingException

                            * * @param function Function. * @param initial Initial midpoint of interval being expanded to * bracket a root. * @param lowerBound Lower bound (a is never lower than this value). * @param upperBound Upper bound (b never is greater than this * value). * @param maximumIterations Maximum number of iterations to perform * @return a two element array holding a and b. * @throws NoBracketingException if the algorithm fails to find a and b * satisfying the desired conditions. * @throws NotStrictlyPositiveException if {@code maximumIterations <= 0}. * @throws NullArgumentException if {@code function} is {@code null}. */ public static double[] bracket(UnivariateFunction function, double initial, double lowerBound, double upperBound, int maximumIterations) throws NullArgumentException, NotStrictlyPositiveException, NoBracketingException { if (function == null) { throw new NullArgumentException(LocalizedFormats.FUNCTION); } if (maximumIterations <= 0) { throw new NotStrictlyPositiveException(LocalizedFormats.INVALID_MAX_ITERATIONS, maximumIterations); } verifySequence(lowerBound, initial, upperBound); double a = initial; double b = initial; double fa; double fb; int numIterations = 0; do { a = FastMath.max(a - 1.0, lowerBound); b = FastMath.min(b + 1.0, upperBound); fa = function.value(a); fb = function.value(b); ++numIterations; } while ((fa * fb > 0.0) && (numIterations < maximumIterations) && ((a > lowerBound) || (b < upperBound))); if (fa * fb > 0.0) { throw new NoBracketingException(LocalizedFormats.FAILED_BRACKETING, a, b, fa, fb, numIterations, maximumIterations, initial, lowerBound, upperBound); } return new double[] {a, b}; } /** * Compute the midpoint of two values. * * @param a first value. * @param b second value. * @return the midpoint. */ public static double midpoint(double a, double b) { return (a + b) * 0.5; } /** * Check whether the interval bounds bracket a root. That is, if the * values at the endpoints are not equal to zero, then the function takes * opposite signs at the endpoints. * * @param function Function. * @param lower Lower endpoint. * @param upper Upper endpoint. * @return {@code true} if the function values have opposite signs at the * given points. * @throws NullArgumentException if {@code function} is {@code null}. */ public static boolean isBracketing(UnivariateFunction function, final double lower, final double upper) throws NullArgumentException { if (function == null) { throw new NullArgumentException(LocalizedFormats.FUNCTION); } final double fLo = function.value(lower); final double fHi = function.value(upper); return (fLo >= 0 && fHi <= 0) || (fLo <= 0 && fHi >= 0); } /** * Check whether the arguments form a (strictly) increasing sequence. * * @param start First number. * @param mid Second number. * @param end Third number. * @return {@code true} if the arguments form an increasing sequence. */ public static boolean isSequence(final double start, final double mid, final double end) { return (start < mid) && (mid < end); } /** * Check that the endpoints specify an interval. * * @param lower Lower endpoint. * @param upper Upper endpoint. * @throws NumberIsTooLargeException if {@code lower >= upper}. */ public static void verifyInterval(final double lower, final double upper) throws NumberIsTooLargeException { if (lower >= upper) { throw new NumberIsTooLargeException(LocalizedFormats.ENDPOINTS_NOT_AN_INTERVAL, lower, upper, false); } } /** * Check that {@code lower < initial < upper}. * * @param lower Lower endpoint. * @param initial Initial value. * @param upper Upper endpoint. * @throws NumberIsTooLargeException if {@code lower >= initial} or * {@code initial >= upper}. */ public static void verifySequence(final double lower, final double initial, final double upper) throws NumberIsTooLargeException { verifyInterval(lower, initial); verifyInterval(initial, upper); } /** * Check that the endpoints specify an interval and the end points * bracket a root. * * @param function Function. * @param lower Lower endpoint. * @param upper Upper endpoint. * @throws NoBracketingException if the function has the same sign at the * endpoints. * @throws NullArgumentException if {@code function} is {@code null}. */ public static void verifyBracketing(UnivariateFunction function, final double lower, final double upper) throws NullArgumentException, NoBracketingException { if (function == null) { throw new NullArgumentException(LocalizedFormats.FUNCTION); } verifyInterval(lower, upper); if (!isBracketing(function, lower, upper)) { throw new NoBracketingException(lower, upper, function.value(lower), function.value(upper)); } } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/analysis/solvers/BrentSolver.java100644 1750 1750 17463 12126627704 31206 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.analysis.solvers; import org.apache.commons.math3.exception.NoBracketingException; import org.apache.commons.math3.exception.TooManyEvaluationsException; import org.apache.commons.math3.exception.NumberIsTooLargeException; import org.apache.commons.math3.util.FastMath; import org.apache.commons.math3.util.Precision; /** * This class implements the * Brent algorithm for finding zeros of real univariate functions. * The function should be continuous but not necessarily smooth. * The {@code solve} method returns a zero {@code x} of the function {@code f} * in the given interval {@code [a, b]} to within a tolerance * {@code 6 eps abs(x) + t} where {@code eps} is the relative accuracy and * {@code t} is the absolute accuracy. * The given interval must bracket the root. * * @version $Id: BrentSolver.java 1379560 2012-08-31 19:40:30Z erans $ */ public class BrentSolver extends AbstractUnivariateSolver { /** Default absolute accuracy. */ private static final double DEFAULT_ABSOLUTE_ACCURACY = 1e-6; /** * Construct a solver with default accuracy (1e-6). */ public BrentSolver() { this(DEFAULT_ABSOLUTE_ACCURACY); } /** * Construct a solver. * * @param absoluteAccuracy Absolute accuracy. */ public BrentSolver(double absoluteAccuracy) { super(absoluteAccuracy); } /** * Construct a solver. * * @param relativeAccuracy Relative accuracy. * @param absoluteAccuracy Absolute accuracy. */ public BrentSolver(double relativeAccuracy, double absoluteAccuracy) { super(relativeAccuracy, absoluteAccuracy); } /** * Construct a solver. * * @param relativeAccuracy Relative accuracy. * @param absoluteAccuracy Absolute accuracy. * @param functionValueAccuracy Function value accuracy. */ public BrentSolver(double relativeAccuracy, double absoluteAccuracy, double functionValueAccuracy) { super(relativeAccuracy, absoluteAccuracy, functionValueAccuracy); } /** * {@inheritDoc} */ @Override protected double doSolve() throws NoBracketingException, TooManyEvaluationsException, NumberIsTooLargeException { double min = getMin(); double max = getMax(); final double initial = getStartValue(); final double functionValueAccuracy = getFunctionValueAccuracy(); verifySequence(min, initial, max); // Return the initial guess if it is good enough. double yInitial = computeObjectiveValue(initial); if (FastMath.abs(yInitial) <= functionValueAccuracy) { return initial; } // Return the first endpoint if it is good enough. double yMin = computeObjectiveValue(min); if (FastMath.abs(yMin) <= functionValueAccuracy) { return min; } // Reduce interval if min and initial bracket the root. if (yInitial * yMin < 0) { return brent(min, initial, yMin, yInitial); } // Return the second endpoint if it is good enough. double yMax = computeObjectiveValue(max); if (FastMath.abs(yMax) <= functionValueAccuracy) { return max; } // Reduce interval if initial and max bracket the root. if (yInitial * yMax < 0) { return brent(initial, max, yInitial, yMax); } throw new NoBracketingException(min, max, yMin, yMax); } /** * Search for a zero inside the provided interval. * This implementation is based on the algorithm described at page 58 of * the book * * Algorithms for Minimization Without Derivatives * Richard P. Brent * Dover 0-486-41998-3 * * * @param lo Lower bound of the search interval. * @param hi Higher bound of the search interval. * @param fLo Function value at the lower bound of the search interval. * @param fHi Function value at the higher bound of the search interval. * @return the value where the function is zero. */ private double brent(double lo, double hi, double fLo, double fHi) { double a = lo; double fa = fLo; double b = hi; double fb = fHi; double c = a; double fc = fa; double d = b - a; double e = d; final double t = getAbsoluteAccuracy(); final double eps = getRelativeAccuracy(); while (true) { if (FastMath.abs(fc) < FastMath.abs(fb)) { a = b; b = c; c = a; fa = fb; fb = fc; fc = fa; } final double tol = 2 * eps * FastMath.abs(b) + t; final double m = 0.5 * (c - b); if (FastMath.abs(m) <= tol || Precision.equals(fb, 0)) { return b; } if (FastMath.abs(e) < tol || FastMath.abs(fa) <= FastMath.abs(fb)) { // Force bisection. d = m; e = d; } else { double s = fb / fa; double p; double q; // The equality test (a == c) is intentional, // it is part of the original Brent's method and // it should NOT be replaced by proximity test. if (a == c) { // Linear interpolation. p = 2 * m * s; q = 1 - s; } else { // Inverse quadratic interpolation. q = fa / fc; final double r = fb / fc; p = s * (2 * m * q * (q - r) - (b - a) * (r - 1)); q = (q - 1) * (r - 1) * (s - 1); } if (p > 0) { q = -q; } else { p = -p; } s = e; e = d; if (p >= 1.5 * m * q - FastMath.abs(tol * q) || p >= FastMath.abs(0.5 * s * q)) { // Inverse quadratic interpolation gives a value // in the wrong direction, or progress is slow. // Fall back to bisection. d = m; e = d; } else { d = p / q; } } a = b; fa = fb; if (FastMath.abs(d) > tol) { b += d; } else if (m > 0) { b += tol; } else { b -= tol; } fb = computeObjectiveValue(b); if ((fb > 0 && fc > 0) || (fb <= 0 && fc <= 0)) { c = a; fc = fa; d = b - a; e = d; } } } } ././@LongLink100644 0 0 172 12126630646 10261 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/analysis/solvers/AbstractDifferentiableUnivariateSolver.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/analysis/solvers/AbstractDifferentiable100644 1750 1750 6356 12126627704 32367 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.analysis.solvers; import org.apache.commons.math3.analysis.DifferentiableUnivariateFunction; import org.apache.commons.math3.analysis.UnivariateFunction; import org.apache.commons.math3.exception.TooManyEvaluationsException; /** * Provide a default implementation for several functions useful to generic * solvers. * * @since 3.0 * @version $Id: AbstractDifferentiableUnivariateSolver.java 1455194 2013-03-11 15:45:54Z luc $ * @deprecated as of 3.1, replaced by {@link AbstractUnivariateDifferentiableSolver} */ @Deprecated public abstract class AbstractDifferentiableUnivariateSolver extends BaseAbstractUnivariateSolver implements DifferentiableUnivariateSolver { /** Derivative of the function to solve. */ private UnivariateFunction functionDerivative; /** * Construct a solver with given absolute accuracy. * * @param absoluteAccuracy Maximum absolute error. */ protected AbstractDifferentiableUnivariateSolver(final double absoluteAccuracy) { super(absoluteAccuracy); } /** * Construct a solver with given accuracies. * * @param relativeAccuracy Maximum relative error. * @param absoluteAccuracy Maximum absolute error. * @param functionValueAccuracy Maximum function value error. */ protected AbstractDifferentiableUnivariateSolver(final double relativeAccuracy, final double absoluteAccuracy, final double functionValueAccuracy) { super(relativeAccuracy, absoluteAccuracy, functionValueAccuracy); } /** * Compute the objective function value. * * @param point Point at which the objective function must be evaluated. * @return the objective function value at specified point. * @throws TooManyEvaluationsException if the maximal number of evaluations is exceeded. */ protected double computeDerivativeObjectiveValue(double point) throws TooManyEvaluationsException { incrementEvaluationCount(); return functionDerivative.value(point); } /** * {@inheritDoc} */ @Override protected void setup(int maxEval, DifferentiableUnivariateFunction f, double min, double max, double startValue) { super.setup(maxEval, f, min, max, startValue); functionDerivative = f.derivative(); } } ././@LongLink100644 0 0 147 12126630646 10263 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/analysis/solvers/NewtonRaphsonSolver.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/analysis/solvers/NewtonRaphsonSolver.ja100644 1750 1750 6463 12126627704 32370 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.analysis.solvers; import org.apache.commons.math3.analysis.differentiation.DerivativeStructure; import org.apache.commons.math3.analysis.differentiation.UnivariateDifferentiableFunction; import org.apache.commons.math3.util.FastMath; import org.apache.commons.math3.exception.TooManyEvaluationsException; /** * Implements * Newton's Method for finding zeros of real univariate differentiable * functions. * * @since 3.1 * @version $Id: NewtonRaphsonSolver.java 1383441 2012-09-11 14:56:39Z luc $ */ public class NewtonRaphsonSolver extends AbstractUnivariateDifferentiableSolver { /** Default absolute accuracy. */ private static final double DEFAULT_ABSOLUTE_ACCURACY = 1e-6; /** * Construct a solver. */ public NewtonRaphsonSolver() { this(DEFAULT_ABSOLUTE_ACCURACY); } /** * Construct a solver. * * @param absoluteAccuracy Absolute accuracy. */ public NewtonRaphsonSolver(double absoluteAccuracy) { super(absoluteAccuracy); } /** * Find a zero near the midpoint of {@code min} and {@code max}. * * @param f Function to solve. * @param min Lower bound for the interval. * @param max Upper bound for the interval. * @param maxEval Maximum number of evaluations. * @return the value where the function is zero. * @throws org.apache.commons.math3.exception.TooManyEvaluationsException * if the maximum evaluation count is exceeded. * @throws org.apache.commons.math3.exception.NumberIsTooLargeException * if {@code min >= max}. */ @Override public double solve(int maxEval, final UnivariateDifferentiableFunction f, final double min, final double max) throws TooManyEvaluationsException { return super.solve(maxEval, f, UnivariateSolverUtils.midpoint(min, max)); } /** * {@inheritDoc} */ @Override protected double doSolve() throws TooManyEvaluationsException { final double startValue = getStartValue(); final double absoluteAccuracy = getAbsoluteAccuracy(); double x0 = startValue; double x1; while (true) { final DerivativeStructure y0 = computeObjectiveValueAndDerivative(x0); x1 = x0 - (y0.getValue() / y0.getPartialDerivative(1)); if (FastMath.abs(x1 - x0) <= absoluteAccuracy) { return x1; } x0 = x1; } } } ././@LongLink100644 0 0 162 12126630646 10260 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/analysis/solvers/DifferentiableUnivariateSolver.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/analysis/solvers/DifferentiableUnivaria100644 1750 1750 2515 12126627704 32373 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.analysis.solvers; import org.apache.commons.math3.analysis.DifferentiableUnivariateFunction; /** * Interface for (univariate real) rootfinding algorithms. * Implementations will search for only one zero in the given interval. * * @version $Id: DifferentiableUnivariateSolver.java 1377245 2012-08-25 10:06:00Z luc $ * @deprecated as of 3.1, replaced by {@link UnivariateDifferentiableSolver} */ public interface DifferentiableUnivariateSolver extends BaseUnivariateSolver {} commons-math3-3.2-src/src/main/java/org/apache/commons/math3/analysis/solvers/RiddersSolver.java100644 1750 1750 12170 12126627704 31516 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.analysis.solvers; import org.apache.commons.math3.util.FastMath; import org.apache.commons.math3.exception.NoBracketingException; import org.apache.commons.math3.exception.TooManyEvaluationsException; /** * Implements the * Ridders' Method for root finding of real univariate functions. For * reference, see C. Ridders, A new algorithm for computing a single root * of a real continuous function , IEEE Transactions on Circuits and * Systems, 26 (1979), 979 - 980. *

                            * The function should be continuous but not necessarily smooth.

                            * * @version $Id: RiddersSolver.java 1379560 2012-08-31 19:40:30Z erans $ * @since 1.2 */ public class RiddersSolver extends AbstractUnivariateSolver { /** Default absolute accuracy. */ private static final double DEFAULT_ABSOLUTE_ACCURACY = 1e-6; /** * Construct a solver with default accuracy (1e-6). */ public RiddersSolver() { this(DEFAULT_ABSOLUTE_ACCURACY); } /** * Construct a solver. * * @param absoluteAccuracy Absolute accuracy. */ public RiddersSolver(double absoluteAccuracy) { super(absoluteAccuracy); } /** * Construct a solver. * * @param relativeAccuracy Relative accuracy. * @param absoluteAccuracy Absolute accuracy. */ public RiddersSolver(double relativeAccuracy, double absoluteAccuracy) { super(relativeAccuracy, absoluteAccuracy); } /** * {@inheritDoc} */ @Override protected double doSolve() throws TooManyEvaluationsException, NoBracketingException { double min = getMin(); double max = getMax(); // [x1, x2] is the bracketing interval in each iteration // x3 is the midpoint of [x1, x2] // x is the new root approximation and an endpoint of the new interval double x1 = min; double y1 = computeObjectiveValue(x1); double x2 = max; double y2 = computeObjectiveValue(x2); // check for zeros before verifying bracketing if (y1 == 0) { return min; } if (y2 == 0) { return max; } verifyBracketing(min, max); final double absoluteAccuracy = getAbsoluteAccuracy(); final double functionValueAccuracy = getFunctionValueAccuracy(); final double relativeAccuracy = getRelativeAccuracy(); double oldx = Double.POSITIVE_INFINITY; while (true) { // calculate the new root approximation final double x3 = 0.5 * (x1 + x2); final double y3 = computeObjectiveValue(x3); if (FastMath.abs(y3) <= functionValueAccuracy) { return x3; } final double delta = 1 - (y1 * y2) / (y3 * y3); // delta > 1 due to bracketing final double correction = (FastMath.signum(y2) * FastMath.signum(y3)) * (x3 - x1) / FastMath.sqrt(delta); final double x = x3 - correction; // correction != 0 final double y = computeObjectiveValue(x); // check for convergence final double tolerance = FastMath.max(relativeAccuracy * FastMath.abs(x), absoluteAccuracy); if (FastMath.abs(x - oldx) <= tolerance) { return x; } if (FastMath.abs(y) <= functionValueAccuracy) { return x; } // prepare the new interval for next iteration // Ridders' method guarantees x1 < x < x2 if (correction > 0.0) { // x1 < x < x3 if (FastMath.signum(y1) + FastMath.signum(y) == 0.0) { x2 = x; y2 = y; } else { x1 = x; x2 = x3; y1 = y; y2 = y3; } } else { // x3 < x < x2 if (FastMath.signum(y2) + FastMath.signum(y) == 0.0) { x1 = x; y1 = y; } else { x1 = x3; x2 = x; y1 = y3; y2 = y; } } oldx = x; } } } ././@LongLink100644 0 0 150 12126630646 10255 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/analysis/solvers/BaseUnivariateSolver.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/analysis/solvers/BaseUnivariateSolver.j100644 1750 1750 13735 12126627704 32344 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.analysis.solvers; import org.apache.commons.math3.analysis.UnivariateFunction; import org.apache.commons.math3.exception.MathIllegalArgumentException; import org.apache.commons.math3.exception.TooManyEvaluationsException; /** * Interface for (univariate real) rootfinding algorithms. * Implementations will search for only one zero in the given interval. * * This class is not intended for use outside of the Apache Commons Math * library, regular user should rely on more specific interfaces like * {@link UnivariateSolver}, {@link PolynomialSolver} or {@link * DifferentiableUnivariateSolver}. * @param Type of function to solve. * * @since 3.0 * @version $Id: BaseUnivariateSolver.java 1455194 2013-03-11 15:45:54Z luc $ * @see UnivariateSolver * @see PolynomialSolver * @see DifferentiableUnivariateSolver */ public interface BaseUnivariateSolver { /** * Get the maximum number of function evaluations. * * @return the maximum number of function evaluations. */ int getMaxEvaluations(); /** * Get the number of evaluations of the objective function. * The number of evaluations corresponds to the last call to the * {@code optimize} method. It is 0 if the method has not been * called yet. * * @return the number of evaluations of the objective function. */ int getEvaluations(); /** * Get the absolute accuracy of the solver. Solutions returned by the * solver should be accurate to this tolerance, i.e., if ε is the * absolute accuracy of the solver and {@code v} is a value returned by * one of the {@code solve} methods, then a root of the function should * exist somewhere in the interval ({@code v} - ε, {@code v} + ε). * * @return the absolute accuracy. */ double getAbsoluteAccuracy(); /** * Get the relative accuracy of the solver. The contract for relative * accuracy is the same as {@link #getAbsoluteAccuracy()}, but using * relative, rather than absolute error. If ρ is the relative accuracy * configured for a solver and {@code v} is a value returned, then a root * of the function should exist somewhere in the interval * ({@code v} - ρ {@code v}, {@code v} + ρ {@code v}). * * @return the relative accuracy. */ double getRelativeAccuracy(); /** * Get the function value accuracy of the solver. If {@code v} is * a value returned by the solver for a function {@code f}, * then by contract, {@code |f(v)|} should be less than or equal to * the function value accuracy configured for the solver. * * @return the function value accuracy. */ double getFunctionValueAccuracy(); /** * Solve for a zero root in the given interval. * A solver may require that the interval brackets a single zero root. * Solvers that do require bracketing should be able to handle the case * where one of the endpoints is itself a root. * * @param maxEval Maximum number of evaluations. * @param f Function to solve. * @param min Lower bound for the interval. * @param max Upper bound for the interval. * @return a value where the function is zero. * @throws MathIllegalArgumentException * if the arguments do not satisfy the requirements specified by the solver. * @throws TooManyEvaluationsException if * the allowed number of evaluations is exceeded. */ double solve(int maxEval, FUNC f, double min, double max) throws MathIllegalArgumentException, TooManyEvaluationsException; /** * Solve for a zero in the given interval, start at {@code startValue}. * A solver may require that the interval brackets a single zero root. * Solvers that do require bracketing should be able to handle the case * where one of the endpoints is itself a root. * * @param maxEval Maximum number of evaluations. * @param f Function to solve. * @param min Lower bound for the interval. * @param max Upper bound for the interval. * @param startValue Start value to use. * @return a value where the function is zero. * @throws MathIllegalArgumentException * if the arguments do not satisfy the requirements specified by the solver. * @throws TooManyEvaluationsException if * the allowed number of evaluations is exceeded. */ double solve(int maxEval, FUNC f, double min, double max, double startValue) throws MathIllegalArgumentException, TooManyEvaluationsException; /** * Solve for a zero in the vicinity of {@code startValue}. * * @param f Function to solve. * @param startValue Start value to use. * @return a value where the function is zero. * @param maxEval Maximum number of evaluations. * @throws org.apache.commons.math3.exception.MathIllegalArgumentException * if the arguments do not satisfy the requirements specified by the solver. * @throws org.apache.commons.math3.exception.TooManyEvaluationsException if * the allowed number of evaluations is exceeded. */ double solve(int maxEval, FUNC f, double startValue); } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/analysis/solvers/LaguerreSolver.java100644 1750 1750 37250 12126627704 31676 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.analysis.solvers; import org.apache.commons.math3.complex.Complex; import org.apache.commons.math3.complex.ComplexUtils; import org.apache.commons.math3.analysis.polynomials.PolynomialFunction; import org.apache.commons.math3.exception.NoBracketingException; import org.apache.commons.math3.exception.NullArgumentException; import org.apache.commons.math3.exception.NoDataException; import org.apache.commons.math3.exception.TooManyEvaluationsException; import org.apache.commons.math3.exception.NumberIsTooLargeException; import org.apache.commons.math3.exception.util.LocalizedFormats; import org.apache.commons.math3.util.FastMath; /** * Implements the * Laguerre's Method for root finding of real coefficient polynomials. * For reference, see * * A First Course in Numerical Analysis * ISBN 048641454X, chapter 8. * * Laguerre's method is global in the sense that it can start with any initial * approximation and be able to solve all roots from that point. * The algorithm requires a bracketing condition. * * @version $Id: LaguerreSolver.java 1422195 2012-12-15 06:45:18Z psteitz $ * @since 1.2 */ public class LaguerreSolver extends AbstractPolynomialSolver { /** Default absolute accuracy. */ private static final double DEFAULT_ABSOLUTE_ACCURACY = 1e-6; /** Complex solver. */ private final ComplexSolver complexSolver = new ComplexSolver(); /** * Construct a solver with default accuracy (1e-6). */ public LaguerreSolver() { this(DEFAULT_ABSOLUTE_ACCURACY); } /** * Construct a solver. * * @param absoluteAccuracy Absolute accuracy. */ public LaguerreSolver(double absoluteAccuracy) { super(absoluteAccuracy); } /** * Construct a solver. * * @param relativeAccuracy Relative accuracy. * @param absoluteAccuracy Absolute accuracy. */ public LaguerreSolver(double relativeAccuracy, double absoluteAccuracy) { super(relativeAccuracy, absoluteAccuracy); } /** * Construct a solver. * * @param relativeAccuracy Relative accuracy. * @param absoluteAccuracy Absolute accuracy. * @param functionValueAccuracy Function value accuracy. */ public LaguerreSolver(double relativeAccuracy, double absoluteAccuracy, double functionValueAccuracy) { super(relativeAccuracy, absoluteAccuracy, functionValueAccuracy); } /** * {@inheritDoc} */ @Override public double doSolve() throws TooManyEvaluationsException, NumberIsTooLargeException, NoBracketingException { final double min = getMin(); final double max = getMax(); final double initial = getStartValue(); final double functionValueAccuracy = getFunctionValueAccuracy(); verifySequence(min, initial, max); // Return the initial guess if it is good enough. final double yInitial = computeObjectiveValue(initial); if (FastMath.abs(yInitial) <= functionValueAccuracy) { return initial; } // Return the first endpoint if it is good enough. final double yMin = computeObjectiveValue(min); if (FastMath.abs(yMin) <= functionValueAccuracy) { return min; } // Reduce interval if min and initial bracket the root. if (yInitial * yMin < 0) { return laguerre(min, initial, yMin, yInitial); } // Return the second endpoint if it is good enough. final double yMax = computeObjectiveValue(max); if (FastMath.abs(yMax) <= functionValueAccuracy) { return max; } // Reduce interval if initial and max bracket the root. if (yInitial * yMax < 0) { return laguerre(initial, max, yInitial, yMax); } throw new NoBracketingException(min, max, yMin, yMax); } /** * Find a real root in the given interval. * * Despite the bracketing condition, the root returned by * {@link LaguerreSolver.ComplexSolver#solve(Complex[],Complex)} may * not be a real zero inside {@code [min, max]}. * For example, p(x) = x3 + 1, * with {@code min = -2}, {@code max = 2}, {@code initial = 0}. * When it occurs, this code calls * {@link LaguerreSolver.ComplexSolver#solveAll(Complex[],Complex)} * in order to obtain all roots and picks up one real root. * * @param lo Lower bound of the search interval. * @param hi Higher bound of the search interval. * @param fLo Function value at the lower bound of the search interval. * @param fHi Function value at the higher bound of the search interval. * @return the point at which the function value is zero. * @deprecated This method should not be part of the public API: It will * be made private in version 4.0. */ @Deprecated public double laguerre(double lo, double hi, double fLo, double fHi) { final Complex c[] = ComplexUtils.convertToComplex(getCoefficients()); final Complex initial = new Complex(0.5 * (lo + hi), 0); final Complex z = complexSolver.solve(c, initial); if (complexSolver.isRoot(lo, hi, z)) { return z.getReal(); } else { double r = Double.NaN; // Solve all roots and select the one we are seeking. Complex[] root = complexSolver.solveAll(c, initial); for (int i = 0; i < root.length; i++) { if (complexSolver.isRoot(lo, hi, root[i])) { r = root[i].getReal(); break; } } return r; } } /** * Find all complex roots for the polynomial with the given * coefficients, starting from the given initial value. *
                            * Note: This method is not part of the API of {@link BaseUnivariateSolver}. * * @param coefficients Polynomial coefficients. * @param initial Start value. * @return the point at which the function value is zero. * @throws org.apache.commons.math3.exception.TooManyEvaluationsException * if the maximum number of evaluations is exceeded. * @throws NullArgumentException if the {@code coefficients} is * {@code null}. * @throws NoDataException if the {@code coefficients} array is empty. * @since 3.1 */ public Complex[] solveAllComplex(double[] coefficients, double initial) throws NullArgumentException, NoDataException, TooManyEvaluationsException { setup(Integer.MAX_VALUE, new PolynomialFunction(coefficients), Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY, initial); return complexSolver.solveAll(ComplexUtils.convertToComplex(coefficients), new Complex(initial, 0d)); } /** * Find a complex root for the polynomial with the given coefficients, * starting from the given initial value. *
                            * Note: This method is not part of the API of {@link BaseUnivariateSolver}. * * @param coefficients Polynomial coefficients. * @param initial Start value. * @return the point at which the function value is zero. * @throws org.apache.commons.math3.exception.TooManyEvaluationsException * if the maximum number of evaluations is exceeded. * @throws NullArgumentException if the {@code coefficients} is * {@code null}. * @throws NoDataException if the {@code coefficients} array is empty. * @since 3.1 */ public Complex solveComplex(double[] coefficients, double initial) throws NullArgumentException, NoDataException, TooManyEvaluationsException { setup(Integer.MAX_VALUE, new PolynomialFunction(coefficients), Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY, initial); return complexSolver.solve(ComplexUtils.convertToComplex(coefficients), new Complex(initial, 0d)); } /** * Class for searching all (complex) roots. */ private class ComplexSolver { /** * Check whether the given complex root is actually a real zero * in the given interval, within the solver tolerance level. * * @param min Lower bound for the interval. * @param max Upper bound for the interval. * @param z Complex root. * @return {@code true} if z is a real zero. */ public boolean isRoot(double min, double max, Complex z) { if (isSequence(min, z.getReal(), max)) { double tolerance = FastMath.max(getRelativeAccuracy() * z.abs(), getAbsoluteAccuracy()); return (FastMath.abs(z.getImaginary()) <= tolerance) || (z.abs() <= getFunctionValueAccuracy()); } return false; } /** * Find all complex roots for the polynomial with the given * coefficients, starting from the given initial value. * * @param coefficients Polynomial coefficients. * @param initial Start value. * @return the point at which the function value is zero. * @throws org.apache.commons.math3.exception.TooManyEvaluationsException * if the maximum number of evaluations is exceeded. * @throws NullArgumentException if the {@code coefficients} is * {@code null}. * @throws NoDataException if the {@code coefficients} array is empty. */ public Complex[] solveAll(Complex coefficients[], Complex initial) throws NullArgumentException, NoDataException, TooManyEvaluationsException { if (coefficients == null) { throw new NullArgumentException(); } final int n = coefficients.length - 1; if (n == 0) { throw new NoDataException(LocalizedFormats.POLYNOMIAL); } // Coefficients for deflated polynomial. final Complex c[] = new Complex[n + 1]; for (int i = 0; i <= n; i++) { c[i] = coefficients[i]; } // Solve individual roots successively. final Complex root[] = new Complex[n]; for (int i = 0; i < n; i++) { final Complex subarray[] = new Complex[n - i + 1]; System.arraycopy(c, 0, subarray, 0, subarray.length); root[i] = solve(subarray, initial); // Polynomial deflation using synthetic division. Complex newc = c[n - i]; Complex oldc = null; for (int j = n - i - 1; j >= 0; j--) { oldc = c[j]; c[j] = newc; newc = oldc.add(newc.multiply(root[i])); } } return root; } /** * Find a complex root for the polynomial with the given coefficients, * starting from the given initial value. * * @param coefficients Polynomial coefficients. * @param initial Start value. * @return the point at which the function value is zero. * @throws org.apache.commons.math3.exception.TooManyEvaluationsException * if the maximum number of evaluations is exceeded. * @throws NullArgumentException if the {@code coefficients} is * {@code null}. * @throws NoDataException if the {@code coefficients} array is empty. */ public Complex solve(Complex coefficients[], Complex initial) throws NullArgumentException, NoDataException, TooManyEvaluationsException { if (coefficients == null) { throw new NullArgumentException(); } final int n = coefficients.length - 1; if (n == 0) { throw new NoDataException(LocalizedFormats.POLYNOMIAL); } final double absoluteAccuracy = getAbsoluteAccuracy(); final double relativeAccuracy = getRelativeAccuracy(); final double functionValueAccuracy = getFunctionValueAccuracy(); final Complex nC = new Complex(n, 0); final Complex n1C = new Complex(n - 1, 0); Complex z = initial; Complex oldz = new Complex(Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY); while (true) { // Compute pv (polynomial value), dv (derivative value), and // d2v (second derivative value) simultaneously. Complex pv = coefficients[n]; Complex dv = Complex.ZERO; Complex d2v = Complex.ZERO; for (int j = n-1; j >= 0; j--) { d2v = dv.add(z.multiply(d2v)); dv = pv.add(z.multiply(dv)); pv = coefficients[j].add(z.multiply(pv)); } d2v = d2v.multiply(new Complex(2.0, 0.0)); // Check for convergence. final double tolerance = FastMath.max(relativeAccuracy * z.abs(), absoluteAccuracy); if ((z.subtract(oldz)).abs() <= tolerance) { return z; } if (pv.abs() <= functionValueAccuracy) { return z; } // Now pv != 0, calculate the new approximation. final Complex G = dv.divide(pv); final Complex G2 = G.multiply(G); final Complex H = G2.subtract(d2v.divide(pv)); final Complex delta = n1C.multiply((nC.multiply(H)).subtract(G2)); // Choose a denominator larger in magnitude. final Complex deltaSqrt = delta.sqrt(); final Complex dplus = G.add(deltaSqrt); final Complex dminus = G.subtract(deltaSqrt); final Complex denominator = dplus.abs() > dminus.abs() ? dplus : dminus; // Perturb z if denominator is zero, for instance, // p(x) = x^3 + 1, z = 0. if (denominator.equals(new Complex(0.0, 0.0))) { z = z.add(new Complex(absoluteAccuracy, absoluteAccuracy)); oldz = new Complex(Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY); } else { oldz = z; z = z.subtract(nC.divide(denominator)); } incrementEvaluationCount(); } } } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/analysis/solvers/UnivariateSolver.java100644 1750 1750 2310 12126627704 32204 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.analysis.solvers; import org.apache.commons.math3.analysis.UnivariateFunction; /** * Interface for (univariate real) root-finding algorithms. * Implementations will search for only one zero in the given interval. * * @version $Id: UnivariateSolver.java 1364387 2012-07-22 18:14:11Z tn $ */ public interface UnivariateSolver extends BaseUnivariateSolver {} commons-math3-3.2-src/src/main/java/org/apache/commons/math3/analysis/solvers/MullerSolver2.java100644 1750 1750 14245 12126627704 31451 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.analysis.solvers; import org.apache.commons.math3.exception.NoBracketingException; import org.apache.commons.math3.exception.NumberIsTooLargeException; import org.apache.commons.math3.exception.TooManyEvaluationsException; import org.apache.commons.math3.util.FastMath; /** * This class implements the * Muller's Method for root finding of real univariate functions. For * reference, see Elementary Numerical Analysis, ISBN 0070124477, * chapter 3. *

                            * Muller's method applies to both real and complex functions, but here we * restrict ourselves to real functions. * This class differs from {@link MullerSolver} in the way it avoids complex * operations.

                            * Except for the initial [min, max], it does not require bracketing * condition, e.g. f(x0), f(x1), f(x2) can have the same sign. If complex * number arises in the computation, we simply use its modulus as real * approximation.

                            *

                            * Because the interval may not be bracketing, bisection alternative is * not applicable here. However in practice our treatment usually works * well, especially near real zeroes where the imaginary part of complex * approximation is often negligible.

                            *

                            * The formulas here do not use divided differences directly.

                            * * @version $Id: MullerSolver2.java 1379560 2012-08-31 19:40:30Z erans $ * @since 1.2 * @see MullerSolver */ public class MullerSolver2 extends AbstractUnivariateSolver { /** Default absolute accuracy. */ private static final double DEFAULT_ABSOLUTE_ACCURACY = 1e-6; /** * Construct a solver with default accuracy (1e-6). */ public MullerSolver2() { this(DEFAULT_ABSOLUTE_ACCURACY); } /** * Construct a solver. * * @param absoluteAccuracy Absolute accuracy. */ public MullerSolver2(double absoluteAccuracy) { super(absoluteAccuracy); } /** * Construct a solver. * * @param relativeAccuracy Relative accuracy. * @param absoluteAccuracy Absolute accuracy. */ public MullerSolver2(double relativeAccuracy, double absoluteAccuracy) { super(relativeAccuracy, absoluteAccuracy); } /** * {@inheritDoc} */ @Override protected double doSolve() throws TooManyEvaluationsException, NumberIsTooLargeException, NoBracketingException { final double min = getMin(); final double max = getMax(); verifyInterval(min, max); final double relativeAccuracy = getRelativeAccuracy(); final double absoluteAccuracy = getAbsoluteAccuracy(); final double functionValueAccuracy = getFunctionValueAccuracy(); // x2 is the last root approximation // x is the new approximation and new x2 for next round // x0 < x1 < x2 does not hold here double x0 = min; double y0 = computeObjectiveValue(x0); if (FastMath.abs(y0) < functionValueAccuracy) { return x0; } double x1 = max; double y1 = computeObjectiveValue(x1); if (FastMath.abs(y1) < functionValueAccuracy) { return x1; } if(y0 * y1 > 0) { throw new NoBracketingException(x0, x1, y0, y1); } double x2 = 0.5 * (x0 + x1); double y2 = computeObjectiveValue(x2); double oldx = Double.POSITIVE_INFINITY; while (true) { // quadratic interpolation through x0, x1, x2 final double q = (x2 - x1) / (x1 - x0); final double a = q * (y2 - (1 + q) * y1 + q * y0); final double b = (2 * q + 1) * y2 - (1 + q) * (1 + q) * y1 + q * q * y0; final double c = (1 + q) * y2; final double delta = b * b - 4 * a * c; double x; final double denominator; if (delta >= 0.0) { // choose a denominator larger in magnitude double dplus = b + FastMath.sqrt(delta); double dminus = b - FastMath.sqrt(delta); denominator = FastMath.abs(dplus) > FastMath.abs(dminus) ? dplus : dminus; } else { // take the modulus of (B +/- FastMath.sqrt(delta)) denominator = FastMath.sqrt(b * b - delta); } if (denominator != 0) { x = x2 - 2.0 * c * (x2 - x1) / denominator; // perturb x if it exactly coincides with x1 or x2 // the equality tests here are intentional while (x == x1 || x == x2) { x += absoluteAccuracy; } } else { // extremely rare case, get a random number to skip it x = min + FastMath.random() * (max - min); oldx = Double.POSITIVE_INFINITY; } final double y = computeObjectiveValue(x); // check for convergence final double tolerance = FastMath.max(relativeAccuracy * FastMath.abs(x), absoluteAccuracy); if (FastMath.abs(x - oldx) <= tolerance || FastMath.abs(y) <= functionValueAccuracy) { return x; } // prepare the next iteration x0 = x1; y0 = y1; x1 = x2; y1 = y2; x2 = x; y2 = y; oldx = x; } } } ././@LongLink100644 0 0 155 12126630646 10262 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/analysis/solvers/BracketedUnivariateSolver.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/analysis/solvers/BracketedUnivariateSol100644 1750 1750 10466 12126627704 32407 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.analysis.solvers; import org.apache.commons.math3.analysis.UnivariateFunction; /** Interface for {@link UnivariateSolver (univariate real) root-finding * algorithms} that maintain a bracketed solution. There are several advantages * to having such root-finding algorithms: *
                              *
                            • The bracketed solution guarantees that the root is kept within the * interval. As such, these algorithms generally also guarantee * convergence.
                            • *
                            • The bracketed solution means that we have the opportunity to only * return roots that are greater than or equal to the actual root, or * are less than or equal to the actual root. That is, we can control * whether under-approximations and over-approximations are * {@link AllowedSolution allowed solutions}. Other root-finding * algorithms can usually only guarantee that the solution (the root that * was found) is around the actual root.
                            • *
                            * *

                            For backwards compatibility, all root-finding algorithms must have * {@link AllowedSolution#ANY_SIDE ANY_SIDE} as default for the allowed * solutions.

                            * @param Type of function to solve. * * @see AllowedSolution * @since 3.0 * @version $Id: BracketedUnivariateSolver.java 1364387 2012-07-22 18:14:11Z tn $ */ public interface BracketedUnivariateSolver extends BaseUnivariateSolver { /** * Solve for a zero in the given interval. * A solver may require that the interval brackets a single zero root. * Solvers that do require bracketing should be able to handle the case * where one of the endpoints is itself a root. * * @param maxEval Maximum number of evaluations. * @param f Function to solve. * @param min Lower bound for the interval. * @param max Upper bound for the interval. * @param allowedSolution The kind of solutions that the root-finding algorithm may * accept as solutions. * @return A value where the function is zero. * @throws org.apache.commons.math3.exception.MathIllegalArgumentException * if the arguments do not satisfy the requirements specified by the solver. * @throws org.apache.commons.math3.exception.TooManyEvaluationsException if * the allowed number of evaluations is exceeded. */ double solve(int maxEval, FUNC f, double min, double max, AllowedSolution allowedSolution); /** * Solve for a zero in the given interval, start at {@code startValue}. * A solver may require that the interval brackets a single zero root. * Solvers that do require bracketing should be able to handle the case * where one of the endpoints is itself a root. * * @param maxEval Maximum number of evaluations. * @param f Function to solve. * @param min Lower bound for the interval. * @param max Upper bound for the interval. * @param startValue Start value to use. * @param allowedSolution The kind of solutions that the root-finding algorithm may * accept as solutions. * @return A value where the function is zero. * @throws org.apache.commons.math3.exception.MathIllegalArgumentException * if the arguments do not satisfy the requirements specified by the solver. * @throws org.apache.commons.math3.exception.TooManyEvaluationsException if * the allowed number of evaluations is exceeded. */ double solve(int maxEval, FUNC f, double min, double max, double startValue, AllowedSolution allowedSolution); } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/analysis/solvers/BisectionSolver.java100644 1750 1750 5717 12126627704 32032 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.analysis.solvers; import org.apache.commons.math3.util.FastMath; import org.apache.commons.math3.exception.TooManyEvaluationsException; /** * Implements the * bisection algorithm for finding zeros of univariate real functions. *

                            * The function should be continuous but not necessarily smooth.

                            * * @version $Id: BisectionSolver.java 1391927 2012-09-30 00:03:30Z erans $ */ public class BisectionSolver extends AbstractUnivariateSolver { /** Default absolute accuracy. */ private static final double DEFAULT_ABSOLUTE_ACCURACY = 1e-6; /** * Construct a solver with default accuracy (1e-6). */ public BisectionSolver() { this(DEFAULT_ABSOLUTE_ACCURACY); } /** * Construct a solver. * * @param absoluteAccuracy Absolute accuracy. */ public BisectionSolver(double absoluteAccuracy) { super(absoluteAccuracy); } /** * Construct a solver. * * @param relativeAccuracy Relative accuracy. * @param absoluteAccuracy Absolute accuracy. */ public BisectionSolver(double relativeAccuracy, double absoluteAccuracy) { super(relativeAccuracy, absoluteAccuracy); } /** * {@inheritDoc} */ @Override protected double doSolve() throws TooManyEvaluationsException { double min = getMin(); double max = getMax(); verifyInterval(min, max); final double absoluteAccuracy = getAbsoluteAccuracy(); double m; double fm; double fmin; while (true) { m = UnivariateSolverUtils.midpoint(min, max); fmin = computeObjectiveValue(min); fm = computeObjectiveValue(m); if (fm * fmin > 0) { // max and m bracket the root. min = m; } else { // min and m bracket the root. max = m; } if (FastMath.abs(max - min) <= absoluteAccuracy) { m = UnivariateSolverUtils.midpoint(min, max); return m; } } } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/analysis/solvers/AllowedSolution.java100644 1750 1750 6604 12126627704 32040 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.analysis.solvers; /** The kinds of solutions that a {@link BracketedUnivariateSolver * (bracketed univariate real) root-finding algorithm} may accept as solutions. * This basically controls whether or not under-approximations and * over-approximations are allowed. * *

                            If all solutions are accepted ({@link #ANY_SIDE}), then the solution * that the root-finding algorithm returns for a given root may be equal to the * actual root, but it may also be an approximation that is slightly smaller * or slightly larger than the actual root. Root-finding algorithms generally * only guarantee that the returned solution is within the requested * tolerances. In certain cases however, in particular for * {@link org.apache.commons.math3.ode.events.EventHandler state events} of * {@link org.apache.commons.math3.ode.ODEIntegrator ODE solvers}, it * may be necessary to guarantee that a solution is returned that lies on a * specific side the solution.

                            * * @see BracketedUnivariateSolver * @since 3.0 * @version $Id: AllowedSolution.java 1364387 2012-07-22 18:14:11Z tn $ */ public enum AllowedSolution { /** There are no additional side restriction on the solutions for * root-finding. That is, both under-approximations and over-approximations * are allowed. So, if a function f(x) has a root at x = x0, then the * root-finding result s may be smaller than x0, equal to x0, or greater * than x0. */ ANY_SIDE, /** Only solutions that are less than or equal to the actual root are * acceptable as solutions for root-finding. In other words, * over-approximations are not allowed. So, if a function f(x) has a root * at x = x0, then the root-finding result s must satisfy s <= x0. */ LEFT_SIDE, /** Only solutions that are greater than or equal to the actual root are * acceptable as solutions for root-finding. In other words, * under-approximations are not allowed. So, if a function f(x) has a root * at x = x0, then the root-finding result s must satisfy s >= x0. */ RIGHT_SIDE, /** Only solutions for which values are less than or equal to zero are * acceptable as solutions for root-finding. So, if a function f(x) has * a root at x = x0, then the root-finding result s must satisfy f(s) <= 0. */ BELOW_SIDE, /** Only solutions for which values are greater than or equal to zero are * acceptable as solutions for root-finding. So, if a function f(x) has * a root at x = x0, then the root-finding result s must satisfy f(s) >= 0. */ ABOVE_SIDE; } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/analysis/solvers/NewtonSolver.java100644 1750 1750 6365 12126627704 31365 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.analysis.solvers; import org.apache.commons.math3.analysis.DifferentiableUnivariateFunction; import org.apache.commons.math3.util.FastMath; import org.apache.commons.math3.exception.TooManyEvaluationsException; /** * Implements * Newton's Method for finding zeros of real univariate functions. *

                            * The function should be continuous but not necessarily smooth.

                            * * @deprecated as of 3.1, replaced by {@link NewtonRaphsonSolver} * @version $Id: NewtonSolver.java 1395937 2012-10-09 10:04:36Z luc $ */ @Deprecated public class NewtonSolver extends AbstractDifferentiableUnivariateSolver { /** Default absolute accuracy. */ private static final double DEFAULT_ABSOLUTE_ACCURACY = 1e-6; /** * Construct a solver. */ public NewtonSolver() { this(DEFAULT_ABSOLUTE_ACCURACY); } /** * Construct a solver. * * @param absoluteAccuracy Absolute accuracy. */ public NewtonSolver(double absoluteAccuracy) { super(absoluteAccuracy); } /** * Find a zero near the midpoint of {@code min} and {@code max}. * * @param f Function to solve. * @param min Lower bound for the interval. * @param max Upper bound for the interval. * @param maxEval Maximum number of evaluations. * @return the value where the function is zero. * @throws org.apache.commons.math3.exception.TooManyEvaluationsException * if the maximum evaluation count is exceeded. * @throws org.apache.commons.math3.exception.NumberIsTooLargeException * if {@code min >= max}. */ @Override public double solve(int maxEval, final DifferentiableUnivariateFunction f, final double min, final double max) throws TooManyEvaluationsException { return super.solve(maxEval, f, UnivariateSolverUtils.midpoint(min, max)); } /** * {@inheritDoc} */ @Override protected double doSolve() throws TooManyEvaluationsException { final double startValue = getStartValue(); final double absoluteAccuracy = getAbsoluteAccuracy(); double x0 = startValue; double x1; while (true) { x1 = x0 - (computeObjectiveValue(x0) / computeDerivativeObjectiveValue(x0)); if (FastMath.abs(x1 - x0) <= absoluteAccuracy) { return x1; } x0 = x1; } } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/analysis/solvers/IllinoisSolver.java100644 1750 1750 6226 12126627704 31671 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.analysis.solvers; /** * Implements the Illinois method for root-finding (approximating * a zero of a univariate real function). It is a modified * {@link RegulaFalsiSolver Regula Falsi} method. * *

                            Like the Regula Falsi method, convergence is guaranteed by * maintaining a bracketed solution. The Illinois method however, * should converge much faster than the original Regula Falsi * method. Furthermore, this implementation of the Illinois method * should not suffer from the same implementation issues as the Regula * Falsi method, which may fail to convergence in certain cases.

                            * *

                            The Illinois method assumes that the function is continuous, * but not necessarily smooth.

                            * *

                            Implementation based on the following article: M. Dowell and P. Jarratt, * A modified regula falsi method for computing the root of an * equation, BIT Numerical Mathematics, volume 11, number 2, * pages 168-174, Springer, 1971.

                            * * @since 3.0 * @version $Id: IllinoisSolver.java 1364387 2012-07-22 18:14:11Z tn $ */ public class IllinoisSolver extends BaseSecantSolver { /** Construct a solver with default accuracy (1e-6). */ public IllinoisSolver() { super(DEFAULT_ABSOLUTE_ACCURACY, Method.ILLINOIS); } /** * Construct a solver. * * @param absoluteAccuracy Absolute accuracy. */ public IllinoisSolver(final double absoluteAccuracy) { super(absoluteAccuracy, Method.ILLINOIS); } /** * Construct a solver. * * @param relativeAccuracy Relative accuracy. * @param absoluteAccuracy Absolute accuracy. */ public IllinoisSolver(final double relativeAccuracy, final double absoluteAccuracy) { super(relativeAccuracy, absoluteAccuracy, Method.ILLINOIS); } /** * Construct a solver. * * @param relativeAccuracy Relative accuracy. * @param absoluteAccuracy Absolute accuracy. * @param functionValueAccuracy Maximum function value error. */ public IllinoisSolver(final double relativeAccuracy, final double absoluteAccuracy, final double functionValueAccuracy) { super(relativeAccuracy, absoluteAccuracy, functionValueAccuracy, Method.PEGASUS); } } ././@LongLink100644 0 0 145 12126630646 10261 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/analysis/solvers/RegulaFalsiSolver.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/analysis/solvers/RegulaFalsiSolver.java100644 1750 1750 7771 12126627704 32313 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.analysis.solvers; /** * Implements the Regula Falsi or False position method for * root-finding (approximating a zero of a univariate real function). It is a * modified {@link SecantSolver Secant} method. * *

                            The Regula Falsi method is included for completeness, for * testing purposes, for educational purposes, for comparison to other * algorithms, etc. It is however not intended to be used * for actual problems, as one of the bounds often remains fixed, resulting * in very slow convergence. Instead, one of the well-known modified * Regula Falsi algorithms can be used ({@link IllinoisSolver * Illinois} or {@link PegasusSolver Pegasus}). These two * algorithms solve the fundamental issues of the original Regula * Falsi algorithm, and greatly out-performs it for most, if not all, * (practical) functions. * *

                            Unlike the Secant method, the Regula Falsi guarantees * convergence, by maintaining a bracketed solution. Note however, that due to * the finite/limited precision of Java's {@link Double double} type, which is * used in this implementation, the algorithm may get stuck in a situation * where it no longer makes any progress. Such cases are detected and result * in a {@code ConvergenceException} exception being thrown. In other words, * the algorithm theoretically guarantees convergence, but the implementation * does not.

                            * *

                            The Regula Falsi method assumes that the function is continuous, * but not necessarily smooth.

                            * *

                            Implementation based on the following article: M. Dowell and P. Jarratt, * A modified regula falsi method for computing the root of an * equation, BIT Numerical Mathematics, volume 11, number 2, * pages 168-174, Springer, 1971.

                            * * @since 3.0 * @version $Id: RegulaFalsiSolver.java 1364387 2012-07-22 18:14:11Z tn $ */ public class RegulaFalsiSolver extends BaseSecantSolver { /** Construct a solver with default accuracy (1e-6). */ public RegulaFalsiSolver() { super(DEFAULT_ABSOLUTE_ACCURACY, Method.REGULA_FALSI); } /** * Construct a solver. * * @param absoluteAccuracy Absolute accuracy. */ public RegulaFalsiSolver(final double absoluteAccuracy) { super(absoluteAccuracy, Method.REGULA_FALSI); } /** * Construct a solver. * * @param relativeAccuracy Relative accuracy. * @param absoluteAccuracy Absolute accuracy. */ public RegulaFalsiSolver(final double relativeAccuracy, final double absoluteAccuracy) { super(relativeAccuracy, absoluteAccuracy, Method.REGULA_FALSI); } /** * Construct a solver. * * @param relativeAccuracy Relative accuracy. * @param absoluteAccuracy Absolute accuracy. * @param functionValueAccuracy Maximum function value error. */ public RegulaFalsiSolver(final double relativeAccuracy, final double absoluteAccuracy, final double functionValueAccuracy) { super(relativeAccuracy, absoluteAccuracy, functionValueAccuracy, Method.REGULA_FALSI); } } ././@LongLink100644 0 0 172 12126630646 10261 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/analysis/solvers/AbstractUnivariateDifferentiableSolver.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/analysis/solvers/AbstractUnivariateDiff100644 1750 1750 6310 12126627704 32352 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.analysis.solvers; import org.apache.commons.math3.analysis.differentiation.DerivativeStructure; import org.apache.commons.math3.analysis.differentiation.UnivariateDifferentiableFunction; import org.apache.commons.math3.exception.TooManyEvaluationsException; /** * Provide a default implementation for several functions useful to generic * solvers. * * @since 3.1 * @version $Id: AbstractUnivariateDifferentiableSolver.java 1455194 2013-03-11 15:45:54Z luc $ */ public abstract class AbstractUnivariateDifferentiableSolver extends BaseAbstractUnivariateSolver implements UnivariateDifferentiableSolver { /** Function to solve. */ private UnivariateDifferentiableFunction function; /** * Construct a solver with given absolute accuracy. * * @param absoluteAccuracy Maximum absolute error. */ protected AbstractUnivariateDifferentiableSolver(final double absoluteAccuracy) { super(absoluteAccuracy); } /** * Construct a solver with given accuracies. * * @param relativeAccuracy Maximum relative error. * @param absoluteAccuracy Maximum absolute error. * @param functionValueAccuracy Maximum function value error. */ protected AbstractUnivariateDifferentiableSolver(final double relativeAccuracy, final double absoluteAccuracy, final double functionValueAccuracy) { super(relativeAccuracy, absoluteAccuracy, functionValueAccuracy); } /** * Compute the objective function value. * * @param point Point at which the objective function must be evaluated. * @return the objective function value and derivative at specified point. * @throws TooManyEvaluationsException * if the maximal number of evaluations is exceeded. */ protected DerivativeStructure computeObjectiveValueAndDerivative(double point) throws TooManyEvaluationsException { incrementEvaluationCount(); return function.value(new DerivativeStructure(1, 1, 0, point)); } /** * {@inheritDoc} */ @Override protected void setup(int maxEval, UnivariateDifferentiableFunction f, double min, double max, double startValue) { super.setup(maxEval, f, min, max, startValue); function = f; } } ././@LongLink100644 0 0 162 12126630646 10260 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/analysis/solvers/UnivariateDifferentiableSolver.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/analysis/solvers/UnivariateDifferentiab100644 1750 1750 2436 12126627704 32405 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.analysis.solvers; import org.apache.commons.math3.analysis.differentiation.UnivariateDifferentiableFunction; /** * Interface for (univariate real) rootfinding algorithms. * Implementations will search for only one zero in the given interval. * * @since 3.1 * @version $Id: UnivariateDifferentiableSolver.java 1383441 2012-09-11 14:56:39Z luc $ */ public interface UnivariateDifferentiableSolver extends BaseUnivariateSolver {} ././@LongLink100644 0 0 154 12126630646 10261 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/analysis/solvers/AbstractUnivariateSolver.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/analysis/solvers/AbstractUnivariateSolv100644 1750 1750 4501 12126627704 32425 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.analysis.solvers; import org.apache.commons.math3.analysis.UnivariateFunction; /** * Base class for solvers. * * @since 3.0 * @version $Id: AbstractUnivariateSolver.java 1379560 2012-08-31 19:40:30Z erans $ */ public abstract class AbstractUnivariateSolver extends BaseAbstractUnivariateSolver implements UnivariateSolver { /** * Construct a solver with given absolute accuracy. * * @param absoluteAccuracy Maximum absolute error. */ protected AbstractUnivariateSolver(final double absoluteAccuracy) { super(absoluteAccuracy); } /** * Construct a solver with given accuracies. * * @param relativeAccuracy Maximum relative error. * @param absoluteAccuracy Maximum absolute error. */ protected AbstractUnivariateSolver(final double relativeAccuracy, final double absoluteAccuracy) { super(relativeAccuracy, absoluteAccuracy); } /** * Construct a solver with given accuracies. * * @param relativeAccuracy Maximum relative error. * @param absoluteAccuracy Maximum absolute error. * @param functionValueAccuracy Maximum function value error. */ protected AbstractUnivariateSolver(final double relativeAccuracy, final double absoluteAccuracy, final double functionValueAccuracy) { super(relativeAccuracy, absoluteAccuracy, functionValueAccuracy); } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/analysis/solvers/MullerSolver.java100644 1750 1750 20001 12126627704 31352 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.analysis.solvers; import org.apache.commons.math3.util.FastMath; import org.apache.commons.math3.exception.NumberIsTooLargeException; import org.apache.commons.math3.exception.NoBracketingException; import org.apache.commons.math3.exception.TooManyEvaluationsException; /** * This class implements the * Muller's Method for root finding of real univariate functions. For * reference, see Elementary Numerical Analysis, ISBN 0070124477, * chapter 3. *

                            * Muller's method applies to both real and complex functions, but here we * restrict ourselves to real functions. * This class differs from {@link MullerSolver} in the way it avoids complex * operations.

                            * Muller's original method would have function evaluation at complex point. * Since our f(x) is real, we have to find ways to avoid that. Bracketing * condition is one way to go: by requiring bracketing in every iteration, * the newly computed approximation is guaranteed to be real.

                            *

                            * Normally Muller's method converges quadratically in the vicinity of a * zero, however it may be very slow in regions far away from zeros. For * example, f(x) = exp(x) - 1, min = -50, max = 100. In such case we use * bisection as a safety backup if it performs very poorly.

                            *

                            * The formulas here use divided differences directly.

                            * * @version $Id: MullerSolver.java 1391927 2012-09-30 00:03:30Z erans $ * @since 1.2 * @see MullerSolver2 */ public class MullerSolver extends AbstractUnivariateSolver { /** Default absolute accuracy. */ private static final double DEFAULT_ABSOLUTE_ACCURACY = 1e-6; /** * Construct a solver with default accuracy (1e-6). */ public MullerSolver() { this(DEFAULT_ABSOLUTE_ACCURACY); } /** * Construct a solver. * * @param absoluteAccuracy Absolute accuracy. */ public MullerSolver(double absoluteAccuracy) { super(absoluteAccuracy); } /** * Construct a solver. * * @param relativeAccuracy Relative accuracy. * @param absoluteAccuracy Absolute accuracy. */ public MullerSolver(double relativeAccuracy, double absoluteAccuracy) { super(relativeAccuracy, absoluteAccuracy); } /** * {@inheritDoc} */ @Override protected double doSolve() throws TooManyEvaluationsException, NumberIsTooLargeException, NoBracketingException { final double min = getMin(); final double max = getMax(); final double initial = getStartValue(); final double functionValueAccuracy = getFunctionValueAccuracy(); verifySequence(min, initial, max); // check for zeros before verifying bracketing final double fMin = computeObjectiveValue(min); if (FastMath.abs(fMin) < functionValueAccuracy) { return min; } final double fMax = computeObjectiveValue(max); if (FastMath.abs(fMax) < functionValueAccuracy) { return max; } final double fInitial = computeObjectiveValue(initial); if (FastMath.abs(fInitial) < functionValueAccuracy) { return initial; } verifyBracketing(min, max); if (isBracketing(min, initial)) { return solve(min, initial, fMin, fInitial); } else { return solve(initial, max, fInitial, fMax); } } /** * Find a real root in the given interval. * * @param min Lower bound for the interval. * @param max Upper bound for the interval. * @param fMin function value at the lower bound. * @param fMax function value at the upper bound. * @return the point at which the function value is zero. * @throws TooManyEvaluationsException if the allowed number of calls to * the function to be solved has been exhausted. */ private double solve(double min, double max, double fMin, double fMax) throws TooManyEvaluationsException { final double relativeAccuracy = getRelativeAccuracy(); final double absoluteAccuracy = getAbsoluteAccuracy(); final double functionValueAccuracy = getFunctionValueAccuracy(); // [x0, x2] is the bracketing interval in each iteration // x1 is the last approximation and an interpolation point in (x0, x2) // x is the new root approximation and new x1 for next round // d01, d12, d012 are divided differences double x0 = min; double y0 = fMin; double x2 = max; double y2 = fMax; double x1 = 0.5 * (x0 + x2); double y1 = computeObjectiveValue(x1); double oldx = Double.POSITIVE_INFINITY; while (true) { // Muller's method employs quadratic interpolation through // x0, x1, x2 and x is the zero of the interpolating parabola. // Due to bracketing condition, this parabola must have two // real roots and we choose one in [x0, x2] to be x. final double d01 = (y1 - y0) / (x1 - x0); final double d12 = (y2 - y1) / (x2 - x1); final double d012 = (d12 - d01) / (x2 - x0); final double c1 = d01 + (x1 - x0) * d012; final double delta = c1 * c1 - 4 * y1 * d012; final double xplus = x1 + (-2.0 * y1) / (c1 + FastMath.sqrt(delta)); final double xminus = x1 + (-2.0 * y1) / (c1 - FastMath.sqrt(delta)); // xplus and xminus are two roots of parabola and at least // one of them should lie in (x0, x2) final double x = isSequence(x0, xplus, x2) ? xplus : xminus; final double y = computeObjectiveValue(x); // check for convergence final double tolerance = FastMath.max(relativeAccuracy * FastMath.abs(x), absoluteAccuracy); if (FastMath.abs(x - oldx) <= tolerance || FastMath.abs(y) <= functionValueAccuracy) { return x; } // Bisect if convergence is too slow. Bisection would waste // our calculation of x, hopefully it won't happen often. // the real number equality test x == x1 is intentional and // completes the proximity tests above it boolean bisect = (x < x1 && (x1 - x0) > 0.95 * (x2 - x0)) || (x > x1 && (x2 - x1) > 0.95 * (x2 - x0)) || (x == x1); // prepare the new bracketing interval for next iteration if (!bisect) { x0 = x < x1 ? x0 : x1; y0 = x < x1 ? y0 : y1; x2 = x > x1 ? x2 : x1; y2 = x > x1 ? y2 : y1; x1 = x; y1 = y; oldx = x; } else { double xm = 0.5 * (x0 + x2); double ym = computeObjectiveValue(xm); if (FastMath.signum(y0) + FastMath.signum(ym) == 0.0) { x2 = xm; y2 = ym; } else { x0 = xm; y0 = ym; } x1 = 0.5 * (x0 + x2); y1 = computeObjectiveValue(x1); oldx = Double.POSITIVE_INFINITY; } } } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/analysis/solvers/SecantSolver.java100644 1750 1750 11562 12126627704 31343 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.analysis.solvers; import org.apache.commons.math3.util.FastMath; import org.apache.commons.math3.exception.NoBracketingException; import org.apache.commons.math3.exception.TooManyEvaluationsException; /** * Implements the Secant method for root-finding (approximating a * zero of a univariate real function). The solution that is maintained is * not bracketed, and as such convergence is not guaranteed. * *

                            Implementation based on the following article: M. Dowell and P. Jarratt, * A modified regula falsi method for computing the root of an * equation, BIT Numerical Mathematics, volume 11, number 2, * pages 168-174, Springer, 1971.

                            * *

                            Note that since release 3.0 this class implements the actual * Secant algorithm, and not a modified one. As such, the 3.0 version * is not backwards compatible with previous versions. To use an algorithm * similar to the pre-3.0 releases, use the * {@link IllinoisSolver Illinois} algorithm or the * {@link PegasusSolver Pegasus} algorithm.

                            * * @version $Id: SecantSolver.java 1379560 2012-08-31 19:40:30Z erans $ */ public class SecantSolver extends AbstractUnivariateSolver { /** Default absolute accuracy. */ protected static final double DEFAULT_ABSOLUTE_ACCURACY = 1e-6; /** Construct a solver with default accuracy (1e-6). */ public SecantSolver() { super(DEFAULT_ABSOLUTE_ACCURACY); } /** * Construct a solver. * * @param absoluteAccuracy absolute accuracy */ public SecantSolver(final double absoluteAccuracy) { super(absoluteAccuracy); } /** * Construct a solver. * * @param relativeAccuracy relative accuracy * @param absoluteAccuracy absolute accuracy */ public SecantSolver(final double relativeAccuracy, final double absoluteAccuracy) { super(relativeAccuracy, absoluteAccuracy); } /** {@inheritDoc} */ @Override protected final double doSolve() throws TooManyEvaluationsException, NoBracketingException { // Get initial solution double x0 = getMin(); double x1 = getMax(); double f0 = computeObjectiveValue(x0); double f1 = computeObjectiveValue(x1); // If one of the bounds is the exact root, return it. Since these are // not under-approximations or over-approximations, we can return them // regardless of the allowed solutions. if (f0 == 0.0) { return x0; } if (f1 == 0.0) { return x1; } // Verify bracketing of initial solution. verifyBracketing(x0, x1); // Get accuracies. final double ftol = getFunctionValueAccuracy(); final double atol = getAbsoluteAccuracy(); final double rtol = getRelativeAccuracy(); // Keep finding better approximations. while (true) { // Calculate the next approximation. final double x = x1 - ((f1 * (x1 - x0)) / (f1 - f0)); final double fx = computeObjectiveValue(x); // If the new approximation is the exact root, return it. Since // this is not an under-approximation or an over-approximation, // we can return it regardless of the allowed solutions. if (fx == 0.0) { return x; } // Update the bounds with the new approximation. x0 = x1; f0 = f1; x1 = x; f1 = fx; // If the function value of the last approximation is too small, // given the function value accuracy, then we can't get closer to // the root than we already are. if (FastMath.abs(f1) <= ftol) { return x1; } // If the current interval is within the given accuracies, we // are satisfied with the current approximation. if (FastMath.abs(x1 - x0) < FastMath.max(rtol * FastMath.abs(x1), atol)) { return x1; } } } } ././@LongLink100644 0 0 161 12126630646 10257 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/analysis/solvers/BracketingNthOrderBrentSolver.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/analysis/solvers/BracketingNthOrderBren100644 1750 1750 37021 12126627704 32337 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.analysis.solvers; import org.apache.commons.math3.analysis.UnivariateFunction; import org.apache.commons.math3.exception.MathInternalError; import org.apache.commons.math3.exception.NoBracketingException; import org.apache.commons.math3.exception.NumberIsTooSmallException; import org.apache.commons.math3.exception.NumberIsTooLargeException; import org.apache.commons.math3.exception.TooManyEvaluationsException; import org.apache.commons.math3.util.FastMath; import org.apache.commons.math3.util.Precision; /** * This class implements a modification of the Brent algorithm. *

                            * The changes with respect to the original Brent algorithm are: *

                              *
                            • the returned value is chosen in the current interval according * to user specified {@link AllowedSolution},
                            • *
                            • the maximal order for the invert polynomial root search is * user-specified instead of being invert quadratic only
                            • *
                            *

                            * The given interval must bracket the root. * * @version $Id: BracketingNthOrderBrentSolver.java 1379560 2012-08-31 19:40:30Z erans $ */ public class BracketingNthOrderBrentSolver extends AbstractUnivariateSolver implements BracketedUnivariateSolver { /** Default absolute accuracy. */ private static final double DEFAULT_ABSOLUTE_ACCURACY = 1e-6; /** Default maximal order. */ private static final int DEFAULT_MAXIMAL_ORDER = 5; /** Maximal aging triggering an attempt to balance the bracketing interval. */ private static final int MAXIMAL_AGING = 2; /** Reduction factor for attempts to balance the bracketing interval. */ private static final double REDUCTION_FACTOR = 1.0 / 16.0; /** Maximal order. */ private final int maximalOrder; /** The kinds of solutions that the algorithm may accept. */ private AllowedSolution allowed; /** * Construct a solver with default accuracy and maximal order (1e-6 and 5 respectively) */ public BracketingNthOrderBrentSolver() { this(DEFAULT_ABSOLUTE_ACCURACY, DEFAULT_MAXIMAL_ORDER); } /** * Construct a solver. * * @param absoluteAccuracy Absolute accuracy. * @param maximalOrder maximal order. * @exception NumberIsTooSmallException if maximal order is lower than 2 */ public BracketingNthOrderBrentSolver(final double absoluteAccuracy, final int maximalOrder) throws NumberIsTooSmallException { super(absoluteAccuracy); if (maximalOrder < 2) { throw new NumberIsTooSmallException(maximalOrder, 2, true); } this.maximalOrder = maximalOrder; this.allowed = AllowedSolution.ANY_SIDE; } /** * Construct a solver. * * @param relativeAccuracy Relative accuracy. * @param absoluteAccuracy Absolute accuracy. * @param maximalOrder maximal order. * @exception NumberIsTooSmallException if maximal order is lower than 2 */ public BracketingNthOrderBrentSolver(final double relativeAccuracy, final double absoluteAccuracy, final int maximalOrder) throws NumberIsTooSmallException { super(relativeAccuracy, absoluteAccuracy); if (maximalOrder < 2) { throw new NumberIsTooSmallException(maximalOrder, 2, true); } this.maximalOrder = maximalOrder; this.allowed = AllowedSolution.ANY_SIDE; } /** * Construct a solver. * * @param relativeAccuracy Relative accuracy. * @param absoluteAccuracy Absolute accuracy. * @param functionValueAccuracy Function value accuracy. * @param maximalOrder maximal order. * @exception NumberIsTooSmallException if maximal order is lower than 2 */ public BracketingNthOrderBrentSolver(final double relativeAccuracy, final double absoluteAccuracy, final double functionValueAccuracy, final int maximalOrder) throws NumberIsTooSmallException { super(relativeAccuracy, absoluteAccuracy, functionValueAccuracy); if (maximalOrder < 2) { throw new NumberIsTooSmallException(maximalOrder, 2, true); } this.maximalOrder = maximalOrder; this.allowed = AllowedSolution.ANY_SIDE; } /** Get the maximal order. * @return maximal order */ public int getMaximalOrder() { return maximalOrder; } /** * {@inheritDoc} */ @Override protected double doSolve() throws TooManyEvaluationsException, NumberIsTooLargeException, NoBracketingException { // prepare arrays with the first points final double[] x = new double[maximalOrder + 1]; final double[] y = new double[maximalOrder + 1]; x[0] = getMin(); x[1] = getStartValue(); x[2] = getMax(); verifySequence(x[0], x[1], x[2]); // evaluate initial guess y[1] = computeObjectiveValue(x[1]); if (Precision.equals(y[1], 0.0, 1)) { // return the initial guess if it is a perfect root. return x[1]; } // evaluate first endpoint y[0] = computeObjectiveValue(x[0]); if (Precision.equals(y[0], 0.0, 1)) { // return the first endpoint if it is a perfect root. return x[0]; } int nbPoints; int signChangeIndex; if (y[0] * y[1] < 0) { // reduce interval if it brackets the root nbPoints = 2; signChangeIndex = 1; } else { // evaluate second endpoint y[2] = computeObjectiveValue(x[2]); if (Precision.equals(y[2], 0.0, 1)) { // return the second endpoint if it is a perfect root. return x[2]; } if (y[1] * y[2] < 0) { // use all computed point as a start sampling array for solving nbPoints = 3; signChangeIndex = 2; } else { throw new NoBracketingException(x[0], x[2], y[0], y[2]); } } // prepare a work array for inverse polynomial interpolation final double[] tmpX = new double[x.length]; // current tightest bracketing of the root double xA = x[signChangeIndex - 1]; double yA = y[signChangeIndex - 1]; double absYA = FastMath.abs(yA); int agingA = 0; double xB = x[signChangeIndex]; double yB = y[signChangeIndex]; double absYB = FastMath.abs(yB); int agingB = 0; // search loop while (true) { // check convergence of bracketing interval final double xTol = getAbsoluteAccuracy() + getRelativeAccuracy() * FastMath.max(FastMath.abs(xA), FastMath.abs(xB)); if (((xB - xA) <= xTol) || (FastMath.max(absYA, absYB) < getFunctionValueAccuracy())) { switch (allowed) { case ANY_SIDE : return absYA < absYB ? xA : xB; case LEFT_SIDE : return xA; case RIGHT_SIDE : return xB; case BELOW_SIDE : return (yA <= 0) ? xA : xB; case ABOVE_SIDE : return (yA < 0) ? xB : xA; default : // this should never happen throw new MathInternalError(); } } // target for the next evaluation point double targetY; if (agingA >= MAXIMAL_AGING) { // we keep updating the high bracket, try to compensate this final int p = agingA - MAXIMAL_AGING; final double weightA = (1 << p) - 1; final double weightB = p + 1; targetY = (weightA * yA - weightB * REDUCTION_FACTOR * yB) / (weightA + weightB); } else if (agingB >= MAXIMAL_AGING) { // we keep updating the low bracket, try to compensate this final int p = agingB - MAXIMAL_AGING; final double weightA = p + 1; final double weightB = (1 << p) - 1; targetY = (weightB * yB - weightA * REDUCTION_FACTOR * yA) / (weightA + weightB); } else { // bracketing is balanced, try to find the root itself targetY = 0; } // make a few attempts to guess a root, double nextX; int start = 0; int end = nbPoints; do { // guess a value for current target, using inverse polynomial interpolation System.arraycopy(x, start, tmpX, start, end - start); nextX = guessX(targetY, tmpX, y, start, end); if (!((nextX > xA) && (nextX < xB))) { // the guessed root is not strictly inside of the tightest bracketing interval // the guessed root is either not strictly inside the interval or it // is a NaN (which occurs when some sampling points share the same y) // we try again with a lower interpolation order if (signChangeIndex - start >= end - signChangeIndex) { // we have more points before the sign change, drop the lowest point ++start; } else { // we have more points after sign change, drop the highest point --end; } // we need to do one more attempt nextX = Double.NaN; } } while (Double.isNaN(nextX) && (end - start > 1)); if (Double.isNaN(nextX)) { // fall back to bisection nextX = xA + 0.5 * (xB - xA); start = signChangeIndex - 1; end = signChangeIndex; } // evaluate the function at the guessed root final double nextY = computeObjectiveValue(nextX); if (Precision.equals(nextY, 0.0, 1)) { // we have found an exact root, since it is not an approximation // we don't need to bother about the allowed solutions setting return nextX; } if ((nbPoints > 2) && (end - start != nbPoints)) { // we have been forced to ignore some points to keep bracketing, // they are probably too far from the root, drop them from now on nbPoints = end - start; System.arraycopy(x, start, x, 0, nbPoints); System.arraycopy(y, start, y, 0, nbPoints); signChangeIndex -= start; } else if (nbPoints == x.length) { // we have to drop one point in order to insert the new one nbPoints--; // keep the tightest bracketing interval as centered as possible if (signChangeIndex >= (x.length + 1) / 2) { // we drop the lowest point, we have to shift the arrays and the index System.arraycopy(x, 1, x, 0, nbPoints); System.arraycopy(y, 1, y, 0, nbPoints); --signChangeIndex; } } // insert the last computed point //(by construction, we know it lies inside the tightest bracketing interval) System.arraycopy(x, signChangeIndex, x, signChangeIndex + 1, nbPoints - signChangeIndex); x[signChangeIndex] = nextX; System.arraycopy(y, signChangeIndex, y, signChangeIndex + 1, nbPoints - signChangeIndex); y[signChangeIndex] = nextY; ++nbPoints; // update the bracketing interval if (nextY * yA <= 0) { // the sign change occurs before the inserted point xB = nextX; yB = nextY; absYB = FastMath.abs(yB); ++agingA; agingB = 0; } else { // the sign change occurs after the inserted point xA = nextX; yA = nextY; absYA = FastMath.abs(yA); agingA = 0; ++agingB; // update the sign change index signChangeIndex++; } } } /** Guess an x value by nth order inverse polynomial interpolation. *

                            * The x value is guessed by evaluating polynomial Q(y) at y = targetY, where Q * is built such that for all considered points (xi, yi), * Q(yi) = xi. *

                            * @param targetY target value for y * @param x reference points abscissas for interpolation, * note that this array is modified during computation * @param y reference points ordinates for interpolation * @param start start index of the points to consider (inclusive) * @param end end index of the points to consider (exclusive) * @return guessed root (will be a NaN if two points share the same y) */ private double guessX(final double targetY, final double[] x, final double[] y, final int start, final int end) { // compute Q Newton coefficients by divided differences for (int i = start; i < end - 1; ++i) { final int delta = i + 1 - start; for (int j = end - 1; j > i; --j) { x[j] = (x[j] - x[j-1]) / (y[j] - y[j - delta]); } } // evaluate Q(targetY) double x0 = 0; for (int j = end - 1; j >= start; --j) { x0 = x[j] + x0 * (targetY - y[j]); } return x0; } /** {@inheritDoc} */ public double solve(int maxEval, UnivariateFunction f, double min, double max, AllowedSolution allowedSolution) throws TooManyEvaluationsException, NumberIsTooLargeException, NoBracketingException { this.allowed = allowedSolution; return super.solve(maxEval, f, min, max); } /** {@inheritDoc} */ public double solve(int maxEval, UnivariateFunction f, double min, double max, double startValue, AllowedSolution allowedSolution) throws TooManyEvaluationsException, NumberIsTooLargeException, NoBracketingException { this.allowed = allowedSolution; return super.solve(maxEval, f, min, max, startValue); } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/analysis/solvers/BaseSecantSolver.java100644 1750 1750 24053 12126627704 32135 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.analysis.solvers; import org.apache.commons.math3.util.FastMath; import org.apache.commons.math3.analysis.UnivariateFunction; import org.apache.commons.math3.exception.ConvergenceException; import org.apache.commons.math3.exception.MathInternalError; /** * Base class for all bracketing Secant-based methods for root-finding * (approximating a zero of a univariate real function). * *

                            Implementation of the {@link RegulaFalsiSolver Regula Falsi} and * {@link IllinoisSolver Illinois} methods is based on the * following article: M. Dowell and P. Jarratt, * A modified regula falsi method for computing the root of an * equation, BIT Numerical Mathematics, volume 11, number 2, * pages 168-174, Springer, 1971.

                            * *

                            Implementation of the {@link PegasusSolver Pegasus} method is * based on the following article: M. Dowell and P. Jarratt, * The "Pegasus" method for computing the root of an equation, * BIT Numerical Mathematics, volume 12, number 4, pages 503-508, Springer, * 1972.

                            * *

                            The {@link SecantSolver Secant} method is not a * bracketing method, so it is not implemented here. It has a separate * implementation.

                            * * @since 3.0 * @version $Id: BaseSecantSolver.java 1455194 2013-03-11 15:45:54Z luc $ */ public abstract class BaseSecantSolver extends AbstractUnivariateSolver implements BracketedUnivariateSolver { /** Default absolute accuracy. */ protected static final double DEFAULT_ABSOLUTE_ACCURACY = 1e-6; /** The kinds of solutions that the algorithm may accept. */ private AllowedSolution allowed; /** The Secant-based root-finding method to use. */ private final Method method; /** * Construct a solver. * * @param absoluteAccuracy Absolute accuracy. * @param method Secant-based root-finding method to use. */ protected BaseSecantSolver(final double absoluteAccuracy, final Method method) { super(absoluteAccuracy); this.allowed = AllowedSolution.ANY_SIDE; this.method = method; } /** * Construct a solver. * * @param relativeAccuracy Relative accuracy. * @param absoluteAccuracy Absolute accuracy. * @param method Secant-based root-finding method to use. */ protected BaseSecantSolver(final double relativeAccuracy, final double absoluteAccuracy, final Method method) { super(relativeAccuracy, absoluteAccuracy); this.allowed = AllowedSolution.ANY_SIDE; this.method = method; } /** * Construct a solver. * * @param relativeAccuracy Maximum relative error. * @param absoluteAccuracy Maximum absolute error. * @param functionValueAccuracy Maximum function value error. * @param method Secant-based root-finding method to use */ protected BaseSecantSolver(final double relativeAccuracy, final double absoluteAccuracy, final double functionValueAccuracy, final Method method) { super(relativeAccuracy, absoluteAccuracy, functionValueAccuracy); this.allowed = AllowedSolution.ANY_SIDE; this.method = method; } /** {@inheritDoc} */ public double solve(final int maxEval, final UnivariateFunction f, final double min, final double max, final AllowedSolution allowedSolution) { return solve(maxEval, f, min, max, min + 0.5 * (max - min), allowedSolution); } /** {@inheritDoc} */ public double solve(final int maxEval, final UnivariateFunction f, final double min, final double max, final double startValue, final AllowedSolution allowedSolution) { this.allowed = allowedSolution; return super.solve(maxEval, f, min, max, startValue); } /** {@inheritDoc} */ @Override public double solve(final int maxEval, final UnivariateFunction f, final double min, final double max, final double startValue) { return solve(maxEval, f, min, max, startValue, AllowedSolution.ANY_SIDE); } /** * {@inheritDoc} * * @throws ConvergenceException if the algorithm failed due to finite * precision. */ @Override protected final double doSolve() throws ConvergenceException { // Get initial solution double x0 = getMin(); double x1 = getMax(); double f0 = computeObjectiveValue(x0); double f1 = computeObjectiveValue(x1); // If one of the bounds is the exact root, return it. Since these are // not under-approximations or over-approximations, we can return them // regardless of the allowed solutions. if (f0 == 0.0) { return x0; } if (f1 == 0.0) { return x1; } // Verify bracketing of initial solution. verifyBracketing(x0, x1); // Get accuracies. final double ftol = getFunctionValueAccuracy(); final double atol = getAbsoluteAccuracy(); final double rtol = getRelativeAccuracy(); // Keep track of inverted intervals, meaning that the left bound is // larger than the right bound. boolean inverted = false; // Keep finding better approximations. while (true) { // Calculate the next approximation. final double x = x1 - ((f1 * (x1 - x0)) / (f1 - f0)); final double fx = computeObjectiveValue(x); // If the new approximation is the exact root, return it. Since // this is not an under-approximation or an over-approximation, // we can return it regardless of the allowed solutions. if (fx == 0.0) { return x; } // Update the bounds with the new approximation. if (f1 * fx < 0) { // The value of x1 has switched to the other bound, thus inverting // the interval. x0 = x1; f0 = f1; inverted = !inverted; } else { switch (method) { case ILLINOIS: f0 *= 0.5; break; case PEGASUS: f0 *= f1 / (f1 + fx); break; case REGULA_FALSI: // Detect early that algorithm is stuck, instead of waiting // for the maximum number of iterations to be exceeded. if (x == x1) { throw new ConvergenceException(); } break; default: // Should never happen. throw new MathInternalError(); } } // Update from [x0, x1] to [x0, x]. x1 = x; f1 = fx; // If the function value of the last approximation is too small, // given the function value accuracy, then we can't get closer to // the root than we already are. if (FastMath.abs(f1) <= ftol) { switch (allowed) { case ANY_SIDE: return x1; case LEFT_SIDE: if (inverted) { return x1; } break; case RIGHT_SIDE: if (!inverted) { return x1; } break; case BELOW_SIDE: if (f1 <= 0) { return x1; } break; case ABOVE_SIDE: if (f1 >= 0) { return x1; } break; default: throw new MathInternalError(); } } // If the current interval is within the given accuracies, we // are satisfied with the current approximation. if (FastMath.abs(x1 - x0) < FastMath.max(rtol * FastMath.abs(x1), atol)) { switch (allowed) { case ANY_SIDE: return x1; case LEFT_SIDE: return inverted ? x1 : x0; case RIGHT_SIDE: return inverted ? x0 : x1; case BELOW_SIDE: return (f1 <= 0) ? x1 : x0; case ABOVE_SIDE: return (f1 >= 0) ? x1 : x0; default: throw new MathInternalError(); } } } } /** Secant-based root-finding methods. */ protected enum Method { /** * The {@link RegulaFalsiSolver Regula Falsi} or * False Position method. */ REGULA_FALSI, /** The {@link IllinoisSolver Illinois} method. */ ILLINOIS, /** The {@link PegasusSolver Pegasus} method. */ PEGASUS; } } ././@LongLink100644 0 0 160 12126630646 10256 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/analysis/solvers/BaseAbstractUnivariateSolver.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/analysis/solvers/BaseAbstractUnivariate100644 1750 1750 25237 12126627704 32405 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.analysis.solvers; import org.apache.commons.math3.analysis.UnivariateFunction; import org.apache.commons.math3.exception.MaxCountExceededException; import org.apache.commons.math3.exception.NoBracketingException; import org.apache.commons.math3.exception.TooManyEvaluationsException; import org.apache.commons.math3.exception.NumberIsTooLargeException; import org.apache.commons.math3.exception.NullArgumentException; import org.apache.commons.math3.util.Incrementor; import org.apache.commons.math3.util.MathUtils; /** * Provide a default implementation for several functions useful to generic * solvers. * * @param Type of function to solve. * * @since 2.0 * @version $Id: BaseAbstractUnivariateSolver.java 1455194 2013-03-11 15:45:54Z luc $ */ public abstract class BaseAbstractUnivariateSolver implements BaseUnivariateSolver { /** Default relative accuracy. */ private static final double DEFAULT_RELATIVE_ACCURACY = 1e-14; /** Default function value accuracy. */ private static final double DEFAULT_FUNCTION_VALUE_ACCURACY = 1e-15; /** Function value accuracy. */ private final double functionValueAccuracy; /** Absolute accuracy. */ private final double absoluteAccuracy; /** Relative accuracy. */ private final double relativeAccuracy; /** Evaluations counter. */ private final Incrementor evaluations = new Incrementor(); /** Lower end of search interval. */ private double searchMin; /** Higher end of search interval. */ private double searchMax; /** Initial guess. */ private double searchStart; /** Function to solve. */ private FUNC function; /** * Construct a solver with given absolute accuracy. * * @param absoluteAccuracy Maximum absolute error. */ protected BaseAbstractUnivariateSolver(final double absoluteAccuracy) { this(DEFAULT_RELATIVE_ACCURACY, absoluteAccuracy, DEFAULT_FUNCTION_VALUE_ACCURACY); } /** * Construct a solver with given accuracies. * * @param relativeAccuracy Maximum relative error. * @param absoluteAccuracy Maximum absolute error. */ protected BaseAbstractUnivariateSolver(final double relativeAccuracy, final double absoluteAccuracy) { this(relativeAccuracy, absoluteAccuracy, DEFAULT_FUNCTION_VALUE_ACCURACY); } /** * Construct a solver with given accuracies. * * @param relativeAccuracy Maximum relative error. * @param absoluteAccuracy Maximum absolute error. * @param functionValueAccuracy Maximum function value error. */ protected BaseAbstractUnivariateSolver(final double relativeAccuracy, final double absoluteAccuracy, final double functionValueAccuracy) { this.absoluteAccuracy = absoluteAccuracy; this.relativeAccuracy = relativeAccuracy; this.functionValueAccuracy = functionValueAccuracy; } /** {@inheritDoc} */ public int getMaxEvaluations() { return evaluations.getMaximalCount(); } /** {@inheritDoc} */ public int getEvaluations() { return evaluations.getCount(); } /** * @return the lower end of the search interval. */ public double getMin() { return searchMin; } /** * @return the higher end of the search interval. */ public double getMax() { return searchMax; } /** * @return the initial guess. */ public double getStartValue() { return searchStart; } /** * {@inheritDoc} */ public double getAbsoluteAccuracy() { return absoluteAccuracy; } /** * {@inheritDoc} */ public double getRelativeAccuracy() { return relativeAccuracy; } /** * {@inheritDoc} */ public double getFunctionValueAccuracy() { return functionValueAccuracy; } /** * Compute the objective function value. * * @param point Point at which the objective function must be evaluated. * @return the objective function value at specified point. * @throws TooManyEvaluationsException if the maximal number of evaluations * is exceeded. */ protected double computeObjectiveValue(double point) throws TooManyEvaluationsException { incrementEvaluationCount(); return function.value(point); } /** * Prepare for computation. * Subclasses must call this method if they override any of the * {@code solve} methods. * * @param f Function to solve. * @param min Lower bound for the interval. * @param max Upper bound for the interval. * @param startValue Start value to use. * @param maxEval Maximum number of evaluations. * @exception NullArgumentException if f is null */ protected void setup(int maxEval, FUNC f, double min, double max, double startValue) throws NullArgumentException { // Checks. MathUtils.checkNotNull(f); // Reset. searchMin = min; searchMax = max; searchStart = startValue; function = f; evaluations.setMaximalCount(maxEval); evaluations.resetCount(); } /** {@inheritDoc} */ public double solve(int maxEval, FUNC f, double min, double max, double startValue) throws TooManyEvaluationsException, NoBracketingException { // Initialization. setup(maxEval, f, min, max, startValue); // Perform computation. return doSolve(); } /** {@inheritDoc} */ public double solve(int maxEval, FUNC f, double min, double max) { return solve(maxEval, f, min, max, min + 0.5 * (max - min)); } /** {@inheritDoc} */ public double solve(int maxEval, FUNC f, double startValue) throws TooManyEvaluationsException, NoBracketingException { return solve(maxEval, f, Double.NaN, Double.NaN, startValue); } /** * Method for implementing actual optimization algorithms in derived * classes. * * @return the root. * @throws TooManyEvaluationsException if the maximal number of evaluations * is exceeded. * @throws NoBracketingException if the initial search interval does not bracket * a root and the solver requires it. */ protected abstract double doSolve() throws TooManyEvaluationsException, NoBracketingException; /** * Check whether the function takes opposite signs at the endpoints. * * @param lower Lower endpoint. * @param upper Upper endpoint. * @return {@code true} if the function values have opposite signs at the * given points. */ protected boolean isBracketing(final double lower, final double upper) { return UnivariateSolverUtils.isBracketing(function, lower, upper); } /** * Check whether the arguments form a (strictly) increasing sequence. * * @param start First number. * @param mid Second number. * @param end Third number. * @return {@code true} if the arguments form an increasing sequence. */ protected boolean isSequence(final double start, final double mid, final double end) { return UnivariateSolverUtils.isSequence(start, mid, end); } /** * Check that the endpoints specify an interval. * * @param lower Lower endpoint. * @param upper Upper endpoint. * @throws NumberIsTooLargeException if {@code lower >= upper}. */ protected void verifyInterval(final double lower, final double upper) throws NumberIsTooLargeException { UnivariateSolverUtils.verifyInterval(lower, upper); } /** * Check that {@code lower < initial < upper}. * * @param lower Lower endpoint. * @param initial Initial value. * @param upper Upper endpoint. * @throws NumberIsTooLargeException if {@code lower >= initial} or * {@code initial >= upper}. */ protected void verifySequence(final double lower, final double initial, final double upper) throws NumberIsTooLargeException { UnivariateSolverUtils.verifySequence(lower, initial, upper); } /** * Check that the endpoints specify an interval and the function takes * opposite signs at the endpoints. * * @param lower Lower endpoint. * @param upper Upper endpoint. * @throws NullArgumentException if the function has not been set. * @throws NoBracketingException if the function has the same sign at * the endpoints. */ protected void verifyBracketing(final double lower, final double upper) throws NullArgumentException, NoBracketingException { UnivariateSolverUtils.verifyBracketing(function, lower, upper); } /** * Increment the evaluation count by one. * Method {@link #computeObjectiveValue(double)} calls this method internally. * It is provided for subclasses that do not exclusively use * {@code computeObjectiveValue} to solve the function. * See e.g. {@link AbstractUnivariateDifferentiableSolver}. * * @throws TooManyEvaluationsException when the allowed number of function * evaluations has been exhausted. */ protected void incrementEvaluationCount() throws TooManyEvaluationsException { try { evaluations.incrementCount(); } catch (MaxCountExceededException e) { throw new TooManyEvaluationsException(e.getMax()); } } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/analysis/solvers/PegasusSolver.java100644 1750 1750 6443 12126627704 31517 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.analysis.solvers; /** * Implements the Pegasus method for root-finding (approximating * a zero of a univariate real function). It is a modified * {@link RegulaFalsiSolver Regula Falsi} method. * *

                            Like the Regula Falsi method, convergence is guaranteed by * maintaining a bracketed solution. The Pegasus method however, * should converge much faster than the original Regula Falsi * method. Furthermore, this implementation of the Pegasus method * should not suffer from the same implementation issues as the Regula * Falsi method, which may fail to convergence in certain cases. Also, * the Pegasus method should converge faster than the * {@link IllinoisSolver Illinois} method, another Regula * Falsi-based method.

                            * *

                            The Pegasus method assumes that the function is continuous, * but not necessarily smooth.

                            * *

                            Implementation based on the following article: M. Dowell and P. Jarratt, * The "Pegasus" method for computing the root of an equation, * BIT Numerical Mathematics, volume 12, number 4, pages 503-508, Springer, * 1972.

                            * * @since 3.0 * @version $Id: PegasusSolver.java 1364387 2012-07-22 18:14:11Z tn $ */ public class PegasusSolver extends BaseSecantSolver { /** Construct a solver with default accuracy (1e-6). */ public PegasusSolver() { super(DEFAULT_ABSOLUTE_ACCURACY, Method.PEGASUS); } /** * Construct a solver. * * @param absoluteAccuracy Absolute accuracy. */ public PegasusSolver(final double absoluteAccuracy) { super(absoluteAccuracy, Method.PEGASUS); } /** * Construct a solver. * * @param relativeAccuracy Relative accuracy. * @param absoluteAccuracy Absolute accuracy. */ public PegasusSolver(final double relativeAccuracy, final double absoluteAccuracy) { super(relativeAccuracy, absoluteAccuracy, Method.PEGASUS); } /** * Construct a solver. * * @param relativeAccuracy Relative accuracy. * @param absoluteAccuracy Absolute accuracy. * @param functionValueAccuracy Maximum function value error. */ public PegasusSolver(final double relativeAccuracy, final double absoluteAccuracy, final double functionValueAccuracy) { super(relativeAccuracy, absoluteAccuracy, functionValueAccuracy, Method.PEGASUS); } } ././@LongLink100644 0 0 154 12126630646 10261 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/analysis/solvers/AbstractPolynomialSolver.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/analysis/solvers/AbstractPolynomialSolv100644 1750 1750 5535 12126627704 32451 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.analysis.solvers; import org.apache.commons.math3.analysis.polynomials.PolynomialFunction; /** * Base class for solvers. * * @since 3.0 * @version $Id: AbstractPolynomialSolver.java 1364387 2012-07-22 18:14:11Z tn $ */ public abstract class AbstractPolynomialSolver extends BaseAbstractUnivariateSolver implements PolynomialSolver { /** Function. */ private PolynomialFunction polynomialFunction; /** * Construct a solver with given absolute accuracy. * * @param absoluteAccuracy Maximum absolute error. */ protected AbstractPolynomialSolver(final double absoluteAccuracy) { super(absoluteAccuracy); } /** * Construct a solver with given accuracies. * * @param relativeAccuracy Maximum relative error. * @param absoluteAccuracy Maximum absolute error. */ protected AbstractPolynomialSolver(final double relativeAccuracy, final double absoluteAccuracy) { super(relativeAccuracy, absoluteAccuracy); } /** * Construct a solver with given accuracies. * * @param relativeAccuracy Maximum relative error. * @param absoluteAccuracy Maximum absolute error. * @param functionValueAccuracy Maximum function value error. */ protected AbstractPolynomialSolver(final double relativeAccuracy, final double absoluteAccuracy, final double functionValueAccuracy) { super(relativeAccuracy, absoluteAccuracy, functionValueAccuracy); } /** * {@inheritDoc} */ @Override protected void setup(int maxEval, PolynomialFunction f, double min, double max, double startValue) { super.setup(maxEval, f, min, max, startValue); polynomialFunction = f; } /** * @return the coefficients of the polynomial function. */ protected double[] getCoefficients() { return polynomialFunction.getCoefficients(); } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/analysis/UnivariateMatrixFunction.java100644 1750 1750 2342 12126627707 32217 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.analysis; /** * An interface representing a univariate matrix function. * * @version $Id: UnivariateMatrixFunction.java 1364387 2012-07-22 18:14:11Z tn $ * @since 2.0 */ public interface UnivariateMatrixFunction { /** * Compute the value for the function. * @param x the point for which the function value should be computed * @return the value */ double[][] value(double x); } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/optim/SimpleVectorValueChecker.java100644 1750 1750 13252 12126627714 31440 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.optim; import org.apache.commons.math3.util.FastMath; import org.apache.commons.math3.exception.NotStrictlyPositiveException; /** * Simple implementation of the {@link ConvergenceChecker} interface using * only objective function values. * * Convergence is considered to have been reached if either the relative * difference between the objective function values is smaller than a * threshold or if either the absolute difference between the objective * function values is smaller than another threshold for all vectors elements. *
                            * The {@link #converged(int,PointVectorValuePair,PointVectorValuePair) converged} * method will also return {@code true} if the number of iterations has been set * (see {@link #SimpleVectorValueChecker(double,double,int) this constructor}). * * @version $Id: SimpleVectorValueChecker.java 1462503 2013-03-29 15:48:27Z luc $ * @since 3.0 */ public class SimpleVectorValueChecker extends AbstractConvergenceChecker { /** * If {@link #maxIterationCount} is set to this value, the number of * iterations will never cause * {@link #converged(int,PointVectorValuePair,PointVectorValuePair)} * to return {@code true}. */ private static final int ITERATION_CHECK_DISABLED = -1; /** * Number of iterations after which the * {@link #converged(int,PointVectorValuePair,PointVectorValuePair)} method * will return true (unless the check is disabled). */ private final int maxIterationCount; /** * Build an instance with specified thresholds. * * In order to perform only relative checks, the absolute tolerance * must be set to a negative value. In order to perform only absolute * checks, the relative tolerance must be set to a negative value. * * @param relativeThreshold relative tolerance threshold * @param absoluteThreshold absolute tolerance threshold */ public SimpleVectorValueChecker(final double relativeThreshold, final double absoluteThreshold) { super(relativeThreshold, absoluteThreshold); maxIterationCount = ITERATION_CHECK_DISABLED; } /** * Builds an instance with specified tolerance thresholds and * iteration count. * * In order to perform only relative checks, the absolute tolerance * must be set to a negative value. In order to perform only absolute * checks, the relative tolerance must be set to a negative value. * * @param relativeThreshold Relative tolerance threshold. * @param absoluteThreshold Absolute tolerance threshold. * @param maxIter Maximum iteration count. * @throws NotStrictlyPositiveException if {@code maxIter <= 0}. * * @since 3.1 */ public SimpleVectorValueChecker(final double relativeThreshold, final double absoluteThreshold, final int maxIter) { super(relativeThreshold, absoluteThreshold); if (maxIter <= 0) { throw new NotStrictlyPositiveException(maxIter); } maxIterationCount = maxIter; } /** * Check if the optimization algorithm has converged considering the * last two points. * This method may be called several times from the same algorithm * iteration with different points. This can be detected by checking the * iteration number at each call if needed. Each time this method is * called, the previous and current point correspond to points with the * same role at each iteration, so they can be compared. As an example, * simplex-based algorithms call this method for all points of the simplex, * not only for the best or worst ones. * * @param iteration Index of current iteration * @param previous Best point in the previous iteration. * @param current Best point in the current iteration. * @return {@code true} if the arguments satify the convergence criterion. */ @Override public boolean converged(final int iteration, final PointVectorValuePair previous, final PointVectorValuePair current) { if (maxIterationCount != ITERATION_CHECK_DISABLED && iteration >= maxIterationCount) { return true; } final double[] p = previous.getValueRef(); final double[] c = current.getValueRef(); for (int i = 0; i < p.length; ++i) { final double pi = p[i]; final double ci = c[i]; final double difference = FastMath.abs(pi - ci); final double size = FastMath.max(FastMath.abs(pi), FastMath.abs(ci)); if (difference > size * getRelativeThreshold() && difference > getAbsoluteThreshold()) { return false; } } return true; } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/optim/package-info.java100644 1750 1750 7474 12126627714 27057 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ /** *

                            * Generally, optimizers are algorithms that will either * {@link org.apache.commons.math3.optim.nonlinear.scalar.GoalType#MINIMIZE minimize} or * {@link org.apache.commons.math3.optim.nonlinear.scalar.GoalType#MAXIMIZE maximize} * a scalar function, called the * {@link org.apache.commons.math3.optim.nonlinear.scalar.ObjectiveFunction objective * function}. *
                            * For some scalar objective functions the gradient can be computed (analytically * or numerically). Algorithms that use this knowledge are defined in the * {@link org.apache.commons.math3.optim.nonlinear.scalar.gradient} package. * The algorithms that do not need this additional information are located in * the {@link org.apache.commons.math3.optim.nonlinear.scalar.noderiv} package. *

                            * *

                            * Some problems are solved more efficiently by algorithms that, instead of an * objective function, need access to a * {@link org.apache.commons.math3.optim.nonlinear.vector.ModelFunction * model function}: such a model predicts a set of values which the * algorithm tries to match with a set of given * {@link org.apache.commons.math3.optim.nonlinear.vector.Target target values}. * Those algorithms are located in the * {@link org.apache.commons.math3.optim.nonlinear.vector} package. *
                            * Algorithms that also require the * {@link org.apache.commons.math3.optim.nonlinear.vector.ModelFunctionJacobian * Jacobian matrix of the model} are located in the * {@link org.apache.commons.math3.optim.nonlinear.vector.jacobian} package. *
                            * The {@link org.apache.commons.math3.optim.nonlinear.vector.jacobian.AbstractLeastSquaresOptimizer * non-linear least-squares optimizers} are a specialization of the the latter, * that minimize the distance (called cost or χ2) * between model and observations. *
                            * For cases where the Jacobian cannot be provided, a utility class will * {@link org.apache.commons.math3.optim.nonlinear.scalar.LeastSquaresConverter * convert} a (vector) model into a (scalar) objective function. *

                            * *

                            * This package provides common functionality for the optimization algorithms. * Abstract classes ({@link org.apache.commons.math3.optim.BaseOptimizer} and * {@link org.apache.commons.math3.optim.BaseMultivariateOptimizer}) contain * boiler-plate code for storing {@link org.apache.commons.math3.optim.MaxEval * evaluations} and {@link org.apache.commons.math3.optim.MaxIter iterations} * counters and a user-defined * {@link org.apache.commons.math3.optim.ConvergenceChecker convergence checker}. *

                            * *

                            * For each of the optimizer types, there is a special implementation that * wraps an optimizer instance and provides a "multi-start" feature: it calls * the underlying optimizer several times with different starting points and * returns the best optimum found, or all optima if so desired. * This could be useful to avoid being trapped in a local extremum. *

                            */ package org.apache.commons.math3.optim; ././@LongLink100644 0 0 154 12126630646 10261 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/optim/BaseMultiStartMultivariateOptimizer.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/optim/BaseMultiStartMultivariateOptimiz100644 1750 1750 22177 12126627714 32476 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.optim; import org.apache.commons.math3.exception.MathIllegalStateException; import org.apache.commons.math3.exception.NotStrictlyPositiveException; import org.apache.commons.math3.exception.TooManyEvaluationsException; import org.apache.commons.math3.random.RandomVectorGenerator; /** * Base class multi-start optimizer for a multivariate function. *
                            * This class wraps an optimizer in order to use it several times in * turn with different starting points (trying to avoid being trapped * in a local extremum when looking for a global one). * It is not a "user" class. * * @param Type of the point/value pair returned by the optimization * algorithm. * * @version $Id: BaseMultiStartMultivariateOptimizer.java 1454746 2013-03-09 17:37:30Z luc $ * @since 3.0 */ public abstract class BaseMultiStartMultivariateOptimizer extends BaseMultivariateOptimizer { /** Underlying classical optimizer. */ private final BaseMultivariateOptimizer optimizer; /** Number of evaluations already performed for all starts. */ private int totalEvaluations; /** Number of starts to go. */ private int starts; /** Random generator for multi-start. */ private RandomVectorGenerator generator; /** Optimization data. */ private OptimizationData[] optimData; /** * Location in {@link #optimData} where the updated maximum * number of evaluations will be stored. */ private int maxEvalIndex = -1; /** * Location in {@link #optimData} where the updated start value * will be stored. */ private int initialGuessIndex = -1; /** * Create a multi-start optimizer from a single-start optimizer. *

                            * Note that if there are bounds constraints (see {@link #getLowerBound()} * and {@link #getUpperBound()}), then a simple rejection algorithm is used * at each restart. This implies that the random vector generator should have * a good probability to generate vectors in the bounded domain, otherwise the * rejection algorithm will hit the {@link #getMaxEvaluations()} count without * generating a proper restart point. Users must be take great care of the curse of dimensionality. *

                            * @param optimizer Single-start optimizer to wrap. * @param starts Number of starts to perform. If {@code starts == 1}, * the {@link #optimize(OptimizationData[]) optimize} will return the * same solution as the given {@code optimizer} would return. * @param generator Random vector generator to use for restarts. * @throws NotStrictlyPositiveException if {@code starts < 1}. */ public BaseMultiStartMultivariateOptimizer(final BaseMultivariateOptimizer optimizer, final int starts, final RandomVectorGenerator generator) { super(optimizer.getConvergenceChecker()); if (starts < 1) { throw new NotStrictlyPositiveException(starts); } this.optimizer = optimizer; this.starts = starts; this.generator = generator; } /** {@inheritDoc} */ @Override public int getEvaluations() { return totalEvaluations; } /** * Gets all the optima found during the last call to {@code optimize}. * The optimizer stores all the optima found during a set of * restarts. The {@code optimize} method returns the best point only. * This method returns all the points found at the end of each starts, * including the best one already returned by the {@code optimize} method. *
                            * The returned array as one element for each start as specified * in the constructor. It is ordered with the results from the * runs that did converge first, sorted from best to worst * objective value (i.e in ascending order if minimizing and in * descending order if maximizing), followed by {@code null} elements * corresponding to the runs that did not converge. This means all * elements will be {@code null} if the {@code optimize} method did throw * an exception. * This also means that if the first element is not {@code null}, it is * the best point found across all starts. *
                            * The behaviour is undefined if this method is called before * {@code optimize}; it will likely throw {@code NullPointerException}. * * @return an array containing the optima sorted from best to worst. */ public abstract PAIR[] getOptima(); /** * {@inheritDoc} * * @throws MathIllegalStateException if {@code optData} does not contain an * instance of {@link MaxEval} or {@link InitialGuess}. */ @Override public PAIR optimize(OptimizationData... optData) { // Store arguments in order to pass them to the internal optimizer. optimData = optData; // Set up base class and perform computations. return super.optimize(optData); } /** {@inheritDoc} */ @Override protected PAIR doOptimize() { // Remove all instances of "MaxEval" and "InitialGuess" from the // array that will be passed to the internal optimizer. // The former is to enforce smaller numbers of allowed evaluations // (according to how many have been used up already), and the latter // to impose a different start value for each start. for (int i = 0; i < optimData.length; i++) { if (optimData[i] instanceof MaxEval) { optimData[i] = null; maxEvalIndex = i; } if (optimData[i] instanceof InitialGuess) { optimData[i] = null; initialGuessIndex = i; continue; } } if (maxEvalIndex == -1) { throw new MathIllegalStateException(); } if (initialGuessIndex == -1) { throw new MathIllegalStateException(); } RuntimeException lastException = null; totalEvaluations = 0; clear(); final int maxEval = getMaxEvaluations(); final double[] min = getLowerBound(); final double[] max = getUpperBound(); final double[] startPoint = getStartPoint(); // Multi-start loop. for (int i = 0; i < starts; i++) { // CHECKSTYLE: stop IllegalCatch try { // Decrease number of allowed evaluations. optimData[maxEvalIndex] = new MaxEval(maxEval - totalEvaluations); // New start value. double[] s = null; if (i == 0) { s = startPoint; } else { int attempts = 0; while (s == null) { if (attempts++ >= getMaxEvaluations()) { throw new TooManyEvaluationsException(getMaxEvaluations()); } s = generator.nextVector(); for (int k = 0; s != null && k < s.length; ++k) { if ((min != null && s[k] < min[k]) || (max != null && s[k] > max[k])) { // reject the vector s = null; } } } } optimData[initialGuessIndex] = new InitialGuess(s); // Optimize. final PAIR result = optimizer.optimize(optimData); store(result); } catch (RuntimeException mue) { lastException = mue; } // CHECKSTYLE: resume IllegalCatch totalEvaluations += optimizer.getEvaluations(); } final PAIR[] optima = getOptima(); if (optima.length == 0) { // All runs failed. throw lastException; // Cannot be null if starts >= 1. } // Return the best optimum. return optima[0]; } /** * Method that will be called in order to store each found optimum. * * @param optimum Result of an optimization run. */ protected abstract void store(PAIR optimum); /** * Method that will called in order to clear all stored optima. */ protected abstract void clear(); } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/optim/PointValuePair.java100644 1750 1750 7760 12126627714 27433 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.optim; import java.io.Serializable; import org.apache.commons.math3.util.Pair; /** * This class holds a point and the value of an objective function at * that point. * * @see PointVectorValuePair * @see org.apache.commons.math3.analysis.MultivariateFunction * @version $Id: PointValuePair.java 1435539 2013-01-19 13:27:24Z tn $ * @since 3.0 */ public class PointValuePair extends Pair implements Serializable { /** Serializable UID. */ private static final long serialVersionUID = 20120513L; /** * Builds a point/objective function value pair. * * @param point Point coordinates. This instance will store * a copy of the array, not the array passed as argument. * @param value Value of the objective function at the point. */ public PointValuePair(final double[] point, final double value) { this(point, value, true); } /** * Builds a point/objective function value pair. * * @param point Point coordinates. * @param value Value of the objective function at the point. * @param copyArray if {@code true}, the input array will be copied, * otherwise it will be referenced. */ public PointValuePair(final double[] point, final double value, final boolean copyArray) { super(copyArray ? ((point == null) ? null : point.clone()) : point, value); } /** * Gets the point. * * @return a copy of the stored point. */ public double[] getPoint() { final double[] p = getKey(); return p == null ? null : p.clone(); } /** * Gets a reference to the point. * * @return a reference to the internal array storing the point. */ public double[] getPointRef() { return getKey(); } /** * Replace the instance with a data transfer object for serialization. * @return data transfer object that will be serialized */ private Object writeReplace() { return new DataTransferObject(getKey(), getValue()); } /** Internal class used only for serialization. */ private static class DataTransferObject implements Serializable { /** Serializable UID. */ private static final long serialVersionUID = 20120513L; /** * Point coordinates. * @Serial */ private final double[] point; /** * Value of the objective function at the point. * @Serial */ private final double value; /** Simple constructor. * @param point Point coordinates. * @param value Value of the objective function at the point. */ public DataTransferObject(final double[] point, final double value) { this.point = point.clone(); this.value = value; } /** Replace the deserialized data transfer object with a {@link PointValuePair}. * @return replacement {@link PointValuePair} */ private Object readResolve() { return new PointValuePair(point, value, false); } } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/optim/InitialGuess.java100644 1750 1750 2661 12126627714 27124 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.optim; /** * Starting point (first guess) of the optimization procedure. *
                            * Immutable class. * * @version $Id: InitialGuess.java 1435539 2013-01-19 13:27:24Z tn $ * @since 3.1 */ public class InitialGuess implements OptimizationData { /** Initial guess. */ private final double[] init; /** * @param startPoint Initial guess. */ public InitialGuess(double[] startPoint) { init = startPoint.clone(); } /** * Gets the initial guess. * * @return the initial guess. */ public double[] getInitialGuess() { return init.clone(); } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/optim/ConvergenceChecker.java100644 1750 1750 4646 12126627714 30254 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.optim; /** * This interface specifies how to check if an optimization algorithm has * converged. *
                            * Deciding if convergence has been reached is a problem-dependent issue. The * user should provide a class implementing this interface to allow the * optimization algorithm to stop its search according to the problem at hand. *
                            * For convenience, three implementations that fit simple needs are already * provided: {@link SimpleValueChecker}, {@link SimpleVectorValueChecker} and * {@link SimplePointChecker}. The first two consider that convergence is * reached when the objective function value does not change much anymore, it * does not use the point set at all. * The third one considers that convergence is reached when the input point * set does not change much anymore, it does not use objective function value * at all. * * @param Type of the (point, objective value) pair. * * @see org.apache.commons.math3.optim.SimplePointChecker * @see org.apache.commons.math3.optim.SimpleValueChecker * @see org.apache.commons.math3.optim.SimpleVectorValueChecker * * @version $Id: ConvergenceChecker.java 1435539 2013-01-19 13:27:24Z tn $ * @since 3.0 */ public interface ConvergenceChecker { /** * Check if the optimization algorithm has converged. * * @param iteration Current iteration. * @param previous Best point in the previous iteration. * @param current Best point in the current iteration. * @return {@code true} if the algorithm is considered to have converged. */ boolean converged(int iteration, PAIR previous, PAIR current); } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/optim/MaxIter.java100644 1750 1750 3726 12126627714 26100 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.optim; import org.apache.commons.math3.exception.NotStrictlyPositiveException; /** * Maximum number of iterations performed by an (iterative) algorithm. * * @version $Id: MaxIter.java 1435539 2013-01-19 13:27:24Z tn $ * @since 3.1 */ public class MaxIter implements OptimizationData { /** Allowed number of evalutations. */ private final int maxIter; /** * @param max Allowed number of iterations. * @throws NotStrictlyPositiveException if {@code max <= 0}. */ public MaxIter(int max) { if (max <= 0) { throw new NotStrictlyPositiveException(max); } maxIter = max; } /** * Gets the maximum number of evaluations. * * @return the allowed number of evaluations. */ public int getMaxIter() { return maxIter; } /** * Factory method that creates instance of this class that represents * a virtually unlimited number of iterations. * * @return a new instance suitable for allowing {@link Integer#MAX_VALUE} * evaluations. */ public static MaxIter unlimited() { return new MaxIter(Integer.MAX_VALUE); } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/optim/PointVectorValuePair.java100644 1750 1750 11307 12126627714 30626 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.optim; import java.io.Serializable; import org.apache.commons.math3.util.Pair; /** * This class holds a point and the vectorial value of an objective function at * that point. * * @see PointValuePair * @see org.apache.commons.math3.analysis.MultivariateVectorFunction * @version $Id: PointVectorValuePair.java 1435539 2013-01-19 13:27:24Z tn $ * @since 3.0 */ public class PointVectorValuePair extends Pair implements Serializable { /** Serializable UID. */ private static final long serialVersionUID = 20120513L; /** * Builds a point/objective function value pair. * * @param point Point coordinates. This instance will store * a copy of the array, not the array passed as argument. * @param value Value of the objective function at the point. */ public PointVectorValuePair(final double[] point, final double[] value) { this(point, value, true); } /** * Build a point/objective function value pair. * * @param point Point coordinates. * @param value Value of the objective function at the point. * @param copyArray if {@code true}, the input arrays will be copied, * otherwise they will be referenced. */ public PointVectorValuePair(final double[] point, final double[] value, final boolean copyArray) { super(copyArray ? ((point == null) ? null : point.clone()) : point, copyArray ? ((value == null) ? null : value.clone()) : value); } /** * Gets the point. * * @return a copy of the stored point. */ public double[] getPoint() { final double[] p = getKey(); return p == null ? null : p.clone(); } /** * Gets a reference to the point. * * @return a reference to the internal array storing the point. */ public double[] getPointRef() { return getKey(); } /** * Gets the value of the objective function. * * @return a copy of the stored value of the objective function. */ @Override public double[] getValue() { final double[] v = super.getValue(); return v == null ? null : v.clone(); } /** * Gets a reference to the value of the objective function. * * @return a reference to the internal array storing the value of * the objective function. */ public double[] getValueRef() { return super.getValue(); } /** * Replace the instance with a data transfer object for serialization. * @return data transfer object that will be serialized */ private Object writeReplace() { return new DataTransferObject(getKey(), getValue()); } /** Internal class used only for serialization. */ private static class DataTransferObject implements Serializable { /** Serializable UID. */ private static final long serialVersionUID = 20120513L; /** * Point coordinates. * @Serial */ private final double[] point; /** * Value of the objective function at the point. * @Serial */ private final double[] value; /** Simple constructor. * @param point Point coordinates. * @param value Value of the objective function at the point. */ public DataTransferObject(final double[] point, final double[] value) { this.point = point.clone(); this.value = value.clone(); } /** Replace the deserialized data transfer object with a {@link PointValuePair}. * @return replacement {@link PointValuePair} */ private Object readResolve() { return new PointVectorValuePair(point, value, false); } } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/optim/BaseMultivariateOptimizer.java100644 1750 1750 12301 12126627714 31700 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.optim; import org.apache.commons.math3.exception.DimensionMismatchException; import org.apache.commons.math3.exception.NumberIsTooSmallException; import org.apache.commons.math3.exception.NumberIsTooLargeException; /** * Base class for implementing optimizers for multivariate functions. * It contains the boiler-plate code for initial guess and bounds * specifications. * It is not a "user" class. * * @param Type of the point/value pair returned by the optimization * algorithm. * * @version $Id: BaseMultivariateOptimizer.java 1443444 2013-02-07 12:41:36Z erans $ * @since 3.1 */ public abstract class BaseMultivariateOptimizer extends BaseOptimizer { /** Initial guess. */ private double[] start; /** Lower bounds. */ private double[] lowerBound; /** Upper bounds. */ private double[] upperBound; /** * @param checker Convergence checker. */ protected BaseMultivariateOptimizer(ConvergenceChecker checker) { super(checker); } /** * {@inheritDoc} * * @param optData Optimization data. In addition to those documented in * {@link BaseOptimizer#parseOptimizationData(OptimizationData[]) BaseOptimizer}, * this method will register the following data: *
                              *
                            • {@link InitialGuess}
                            • *
                            • {@link SimpleBounds}
                            • *
                            * @return {@inheritDoc} */ @Override public PAIR optimize(OptimizationData... optData) { // Perform optimization. return super.optimize(optData); } /** * Scans the list of (required and optional) optimization data that * characterize the problem. * * @param optData Optimization data. The following data will be looked for: *
                              *
                            • {@link InitialGuess}
                            • *
                            • {@link SimpleBounds}
                            • *
                            */ @Override protected void parseOptimizationData(OptimizationData... optData) { // Allow base class to register its own data. super.parseOptimizationData(optData); // The existing values (as set by the previous call) are reused if // not provided in the argument list. for (OptimizationData data : optData) { if (data instanceof InitialGuess) { start = ((InitialGuess) data).getInitialGuess(); continue; } if (data instanceof SimpleBounds) { final SimpleBounds bounds = (SimpleBounds) data; lowerBound = bounds.getLower(); upperBound = bounds.getUpper(); continue; } } // Check input consistency. checkParameters(); } /** * Gets the initial guess. * * @return the initial guess, or {@code null} if not set. */ public double[] getStartPoint() { return start == null ? null : start.clone(); } /** * @return the lower bounds, or {@code null} if not set. */ public double[] getLowerBound() { return lowerBound == null ? null : lowerBound.clone(); } /** * @return the upper bounds, or {@code null} if not set. */ public double[] getUpperBound() { return upperBound == null ? null : upperBound.clone(); } /** * Check parameters consistency. */ private void checkParameters() { if (start != null) { final int dim = start.length; if (lowerBound != null) { if (lowerBound.length != dim) { throw new DimensionMismatchException(lowerBound.length, dim); } for (int i = 0; i < dim; i++) { final double v = start[i]; final double lo = lowerBound[i]; if (v < lo) { throw new NumberIsTooSmallException(v, lo, true); } } } if (upperBound != null) { if (upperBound.length != dim) { throw new DimensionMismatchException(upperBound.length, dim); } for (int i = 0; i < dim; i++) { final double v = start[i]; final double hi = upperBound[i]; if (v > hi) { throw new NumberIsTooLargeException(v, hi, true); } } } } } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/optim/linear/package-info.java100644 1750 1750 1627 12126627714 30323 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ /** * Optimization algorithms for linear constrained problems. */ package org.apache.commons.math3.optim.linear; commons-math3-3.2-src/src/main/java/org/apache/commons/math3/optim/linear/LinearOptimizer.java100644 1750 1750 10552 12126627714 31131 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.optim.linear; import java.util.Collection; import java.util.Collections; import org.apache.commons.math3.exception.TooManyIterationsException; import org.apache.commons.math3.optim.OptimizationData; import org.apache.commons.math3.optim.PointValuePair; import org.apache.commons.math3.optim.nonlinear.scalar.MultivariateOptimizer; /** * Base class for implementing linear optimizers. * * @version $Id: LinearOptimizer.java 1443444 2013-02-07 12:41:36Z erans $ * @since 3.1 */ public abstract class LinearOptimizer extends MultivariateOptimizer { /** * Linear objective function. */ private LinearObjectiveFunction function; /** * Linear constraints. */ private Collection linearConstraints; /** * Whether to restrict the variables to non-negative values. */ private boolean nonNegative; /** * Simple constructor with default settings. * */ protected LinearOptimizer() { super(null); // No convergence checker. } /** * @return {@code true} if the variables are restricted to non-negative values. */ protected boolean isRestrictedToNonNegative() { return nonNegative; } /** * @return the optimization type. */ protected LinearObjectiveFunction getFunction() { return function; } /** * @return the optimization type. */ protected Collection getConstraints() { return Collections.unmodifiableCollection(linearConstraints); } /** * {@inheritDoc} * * @param optData Optimization data. In addition to those documented in * {@link MultivariateOptimizer#parseOptimizationData(OptimizationData[]) * MultivariateOptimizer}, this method will register the following data: *
                              *
                            • {@link LinearObjectiveFunction}
                            • *
                            • {@link LinearConstraintSet}
                            • *
                            • {@link NonNegativeConstraint}
                            • *
                            * @return {@inheritDoc} * @throws TooManyIterationsException if the maximal number of * iterations is exceeded. */ @Override public PointValuePair optimize(OptimizationData... optData) throws TooManyIterationsException { // Set up base class and perform computation. return super.optimize(optData); } /** * Scans the list of (required and optional) optimization data that * characterize the problem. * * @param optData Optimization data. * The following data will be looked for: *
                              *
                            • {@link LinearObjectiveFunction}
                            • *
                            • {@link LinearConstraintSet}
                            • *
                            • {@link NonNegativeConstraint}
                            • *
                            */ @Override protected void parseOptimizationData(OptimizationData... optData) { // Allow base class to register its own data. super.parseOptimizationData(optData); // The existing values (as set by the previous call) are reused if // not provided in the argument list. for (OptimizationData data : optData) { if (data instanceof LinearObjectiveFunction) { function = (LinearObjectiveFunction) data; continue; } if (data instanceof LinearConstraintSet) { linearConstraints = ((LinearConstraintSet) data).getConstraints(); continue; } if (data instanceof NonNegativeConstraint) { nonNegative = ((NonNegativeConstraint) data).isRestrictedToNonNegative(); continue; } } } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/optim/linear/Relationship.java100644 1750 1750 3651 12126627714 30437 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.optim.linear; /** * Types of relationships between two cells in a Solver {@link LinearConstraint}. * * @version $Id: Relationship.java 1435539 2013-01-19 13:27:24Z tn $ * @since 2.0 */ public enum Relationship { /** Equality relationship. */ EQ("="), /** Lesser than or equal relationship. */ LEQ("<="), /** Greater than or equal relationship. */ GEQ(">="); /** Display string for the relationship. */ private final String stringValue; /** * Simple constructor. * * @param stringValue Display string for the relationship. */ private Relationship(String stringValue) { this.stringValue = stringValue; } @Override public String toString() { return stringValue; } /** * Gets the relationship obtained when multiplying all coefficients by -1. * * @return the opposite relationship. */ public Relationship oppositeRelationship() { switch (this) { case LEQ : return GEQ; case GEQ : return LEQ; default : return EQ; } } } ././@LongLink100644 0 0 145 12126630646 10261 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/optim/linear/NonNegativeConstraint.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/optim/linear/NonNegativeConstraint.java100644 1750 1750 3335 12126627714 32257 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.optim.linear; import org.apache.commons.math3.optim.OptimizationData; /** * A constraint for a linear optimization problem indicating whether all * variables must be restricted to non-negative values. * * @version $Id: NonNegativeConstraint.java 1435539 2013-01-19 13:27:24Z tn $ * @since 3.1 */ public class NonNegativeConstraint implements OptimizationData { /** Whether the variables are all positive. */ private final boolean isRestricted; /** * @param restricted If {@code true}, all the variables must be positive. */ public NonNegativeConstraint(boolean restricted) { isRestricted = restricted; } /** * Indicates whether all the variables must be restricted to non-negative * values. * * @return {@code true} if all the variables must be positive. */ public boolean isRestrictedToNonNegative() { return isRestricted; } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/optim/linear/LinearConstraint.java100644 1750 1750 24221 12126627713 31270 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.optim.linear; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.io.Serializable; import org.apache.commons.math3.linear.MatrixUtils; import org.apache.commons.math3.linear.RealVector; import org.apache.commons.math3.linear.ArrayRealVector; /** * A linear constraint for a linear optimization problem. *

                            * A linear constraint has one of the forms: *

                              *
                            • c1x1 + ... cnxn = v
                            • *
                            • c1x1 + ... cnxn <= v
                            • *
                            • c1x1 + ... cnxn >= v
                            • *
                            • l1x1 + ... lnxn + lcst = * r1x1 + ... rnxn + rcst
                            • *
                            • l1x1 + ... lnxn + lcst <= * r1x1 + ... rnxn + rcst
                            • *
                            • l1x1 + ... lnxn + lcst >= * r1x1 + ... rnxn + rcst
                            • *
                            * The ci, li or ri are the coefficients of the constraints, the xi * are the coordinates of the current point and v is the value of the constraint. *

                            * * @version $Id: LinearConstraint.java 1435539 2013-01-19 13:27:24Z tn $ * @since 2.0 */ public class LinearConstraint implements Serializable { /** Serializable version identifier. */ private static final long serialVersionUID = -764632794033034092L; /** Coefficients of the constraint (left hand side). */ private final transient RealVector coefficients; /** Relationship between left and right hand sides (=, <=, >=). */ private final Relationship relationship; /** Value of the constraint (right hand side). */ private final double value; /** * Build a constraint involving a single linear equation. *

                            * A linear constraint with a single linear equation has one of the forms: *

                              *
                            • c1x1 + ... cnxn = v
                            • *
                            • c1x1 + ... cnxn <= v
                            • *
                            • c1x1 + ... cnxn >= v
                            • *
                            *

                            * @param coefficients The coefficients of the constraint (left hand side) * @param relationship The type of (in)equality used in the constraint * @param value The value of the constraint (right hand side) */ public LinearConstraint(final double[] coefficients, final Relationship relationship, final double value) { this(new ArrayRealVector(coefficients), relationship, value); } /** * Build a constraint involving a single linear equation. *

                            * A linear constraint with a single linear equation has one of the forms: *

                              *
                            • c1x1 + ... cnxn = v
                            • *
                            • c1x1 + ... cnxn <= v
                            • *
                            • c1x1 + ... cnxn >= v
                            • *
                            *

                            * @param coefficients The coefficients of the constraint (left hand side) * @param relationship The type of (in)equality used in the constraint * @param value The value of the constraint (right hand side) */ public LinearConstraint(final RealVector coefficients, final Relationship relationship, final double value) { this.coefficients = coefficients; this.relationship = relationship; this.value = value; } /** * Build a constraint involving two linear equations. *

                            * A linear constraint with two linear equation has one of the forms: *

                              *
                            • l1x1 + ... lnxn + lcst = * r1x1 + ... rnxn + rcst
                            • *
                            • l1x1 + ... lnxn + lcst <= * r1x1 + ... rnxn + rcst
                            • *
                            • l1x1 + ... lnxn + lcst >= * r1x1 + ... rnxn + rcst
                            • *
                            *

                            * @param lhsCoefficients The coefficients of the linear expression on the left hand side of the constraint * @param lhsConstant The constant term of the linear expression on the left hand side of the constraint * @param relationship The type of (in)equality used in the constraint * @param rhsCoefficients The coefficients of the linear expression on the right hand side of the constraint * @param rhsConstant The constant term of the linear expression on the right hand side of the constraint */ public LinearConstraint(final double[] lhsCoefficients, final double lhsConstant, final Relationship relationship, final double[] rhsCoefficients, final double rhsConstant) { double[] sub = new double[lhsCoefficients.length]; for (int i = 0; i < sub.length; ++i) { sub[i] = lhsCoefficients[i] - rhsCoefficients[i]; } this.coefficients = new ArrayRealVector(sub, false); this.relationship = relationship; this.value = rhsConstant - lhsConstant; } /** * Build a constraint involving two linear equations. *

                            * A linear constraint with two linear equation has one of the forms: *

                              *
                            • l1x1 + ... lnxn + lcst = * r1x1 + ... rnxn + rcst
                            • *
                            • l1x1 + ... lnxn + lcst <= * r1x1 + ... rnxn + rcst
                            • *
                            • l1x1 + ... lnxn + lcst >= * r1x1 + ... rnxn + rcst
                            • *
                            *

                            * @param lhsCoefficients The coefficients of the linear expression on the left hand side of the constraint * @param lhsConstant The constant term of the linear expression on the left hand side of the constraint * @param relationship The type of (in)equality used in the constraint * @param rhsCoefficients The coefficients of the linear expression on the right hand side of the constraint * @param rhsConstant The constant term of the linear expression on the right hand side of the constraint */ public LinearConstraint(final RealVector lhsCoefficients, final double lhsConstant, final Relationship relationship, final RealVector rhsCoefficients, final double rhsConstant) { this.coefficients = lhsCoefficients.subtract(rhsCoefficients); this.relationship = relationship; this.value = rhsConstant - lhsConstant; } /** * Gets the coefficients of the constraint (left hand side). * * @return the coefficients of the constraint (left hand side). */ public RealVector getCoefficients() { return coefficients; } /** * Gets the relationship between left and right hand sides. * * @return the relationship between left and right hand sides. */ public Relationship getRelationship() { return relationship; } /** * Gets the value of the constraint (right hand side). * * @return the value of the constraint (right hand side). */ public double getValue() { return value; } @Override public boolean equals(Object other) { if (this == other) { return true; } if (other instanceof LinearConstraint) { LinearConstraint rhs = (LinearConstraint) other; return relationship == rhs.relationship && value == rhs.value && coefficients.equals(rhs.coefficients); } return false; } @Override public int hashCode() { return relationship.hashCode() ^ Double.valueOf(value).hashCode() ^ coefficients.hashCode(); } /** * Serialize the instance. * @param oos stream where object should be written * @throws IOException if object cannot be written to stream */ private void writeObject(ObjectOutputStream oos) throws IOException { oos.defaultWriteObject(); MatrixUtils.serializeRealVector(coefficients, oos); } /** * Deserialize the instance. * @param ois stream from which the object should be read * @throws ClassNotFoundException if a class in the stream cannot be found * @throws IOException if object cannot be read from the stream */ private void readObject(ObjectInputStream ois) throws ClassNotFoundException, IOException { ois.defaultReadObject(); MatrixUtils.deserializeRealVector(this, "coefficients", ois); } } ././@LongLink100644 0 0 153 12126630646 10260 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/optim/linear/NoFeasibleSolutionException.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/optim/linear/NoFeasibleSolutionExceptio100644 1750 1750 3031 12126627714 32313 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.optim.linear; import org.apache.commons.math3.exception.MathIllegalStateException; import org.apache.commons.math3.exception.util.LocalizedFormats; /** * This class represents exceptions thrown by optimizers when no solution fulfills the constraints. * * @version $Id: NoFeasibleSolutionException.java 1435539 2013-01-19 13:27:24Z tn $ * @since 2.0 */ public class NoFeasibleSolutionException extends MathIllegalStateException { /** Serializable version identifier. */ private static final long serialVersionUID = -3044253632189082760L; /** * Simple constructor using a default message. */ public NoFeasibleSolutionException() { super(LocalizedFormats.NO_FEASIBLE_SOLUTION); } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/optim/linear/SimplexSolver.java100644 1750 1750 30165 12126627714 30632 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.optim.linear; import java.util.ArrayList; import java.util.List; import org.apache.commons.math3.exception.TooManyIterationsException; import org.apache.commons.math3.optim.PointValuePair; import org.apache.commons.math3.util.Precision; /** * Solves a linear problem using the "Two-Phase Simplex" method. *

                            * Note: Depending on the problem definition, the default convergence criteria * may be too strict, resulting in {@link NoFeasibleSolutionException} or * {@link TooManyIterationsException}. In such a case it is advised to adjust these * criteria with more appropriate values, e.g. relaxing the epsilon value. *

                            * Default convergence criteria: *

                              *
                            • Algorithm convergence: 1e-6
                            • *
                            • Floating-point comparisons: 10 ulp
                            • *
                            • Cut-Off value: 1e-12
                            • *
                            *

                            * The cut-off value has been introduced to zero out very small numbers in the Simplex tableau, * as these may lead to numerical instabilities due to the nature of the Simplex algorithm * (the pivot element is used as a denominator). If the problem definition is very tight, the * default cut-off value may be too small, thus it is advised to increase it to a larger value, * in accordance with the chosen epsilon. *

                            * It may also be counter-productive to provide a too large value for {@link * org.apache.commons.math3.optim.MaxIter MaxIter} as parameter in the call of {@link * #optimize(org.apache.commons.math3.optim.OptimizationData...) optimize(OptimizationData...)}, * as the {@link SimplexSolver} will use different strategies depending on the current iteration * count. After half of the allowed max iterations has already been reached, the strategy to select * pivot rows will change in order to break possible cycles due to degenerate problems. * * @version $Id: SimplexSolver.java 1462503 2013-03-29 15:48:27Z luc $ * @since 2.0 */ public class SimplexSolver extends LinearOptimizer { /** Default amount of error to accept in floating point comparisons (as ulps). */ static final int DEFAULT_ULPS = 10; /** Default cut-off value. */ static final double DEFAULT_CUT_OFF = 1e-12; /** Default amount of error to accept for algorithm convergence. */ private static final double DEFAULT_EPSILON = 1.0e-6; /** Amount of error to accept for algorithm convergence. */ private final double epsilon; /** Amount of error to accept in floating point comparisons (as ulps). */ private final int maxUlps; /** * Cut-off value for entries in the tableau: values smaller than the cut-off * are treated as zero to improve numerical stability. */ private final double cutOff; /** * Builds a simplex solver with default settings. */ public SimplexSolver() { this(DEFAULT_EPSILON, DEFAULT_ULPS, DEFAULT_CUT_OFF); } /** * Builds a simplex solver with a specified accepted amount of error. * * @param epsilon Amount of error to accept for algorithm convergence. */ public SimplexSolver(final double epsilon) { this(epsilon, DEFAULT_ULPS, DEFAULT_CUT_OFF); } /** * Builds a simplex solver with a specified accepted amount of error. * * @param epsilon Amount of error to accept for algorithm convergence. * @param maxUlps Amount of error to accept in floating point comparisons. */ public SimplexSolver(final double epsilon, final int maxUlps) { this(epsilon, maxUlps, DEFAULT_CUT_OFF); } /** * Builds a simplex solver with a specified accepted amount of error. * * @param epsilon Amount of error to accept for algorithm convergence. * @param maxUlps Amount of error to accept in floating point comparisons. * @param cutOff Values smaller than the cutOff are treated as zero. */ public SimplexSolver(final double epsilon, final int maxUlps, final double cutOff) { this.epsilon = epsilon; this.maxUlps = maxUlps; this.cutOff = cutOff; } /** * Returns the column with the most negative coefficient in the objective function row. * * @param tableau Simple tableau for the problem. * @return the column with the most negative coefficient. */ private Integer getPivotColumn(SimplexTableau tableau) { double minValue = 0; Integer minPos = null; for (int i = tableau.getNumObjectiveFunctions(); i < tableau.getWidth() - 1; i++) { final double entry = tableau.getEntry(0, i); // check if the entry is strictly smaller than the current minimum // do not use a ulp/epsilon check if (entry < minValue) { minValue = entry; minPos = i; } } return minPos; } /** * Returns the row with the minimum ratio as given by the minimum ratio test (MRT). * * @param tableau Simple tableau for the problem. * @param col Column to test the ratio of (see {@link #getPivotColumn(SimplexTableau)}). * @return the row with the minimum ratio. */ private Integer getPivotRow(SimplexTableau tableau, final int col) { // create a list of all the rows that tie for the lowest score in the minimum ratio test List minRatioPositions = new ArrayList(); double minRatio = Double.MAX_VALUE; for (int i = tableau.getNumObjectiveFunctions(); i < tableau.getHeight(); i++) { final double rhs = tableau.getEntry(i, tableau.getWidth() - 1); final double entry = tableau.getEntry(i, col); if (Precision.compareTo(entry, 0d, maxUlps) > 0) { final double ratio = rhs / entry; // check if the entry is strictly equal to the current min ratio // do not use a ulp/epsilon check final int cmp = Double.compare(ratio, minRatio); if (cmp == 0) { minRatioPositions.add(i); } else if (cmp < 0) { minRatio = ratio; minRatioPositions = new ArrayList(); minRatioPositions.add(i); } } } if (minRatioPositions.size() == 0) { return null; } else if (minRatioPositions.size() > 1) { // there's a degeneracy as indicated by a tie in the minimum ratio test // 1. check if there's an artificial variable that can be forced out of the basis if (tableau.getNumArtificialVariables() > 0) { for (Integer row : minRatioPositions) { for (int i = 0; i < tableau.getNumArtificialVariables(); i++) { int column = i + tableau.getArtificialVariableOffset(); final double entry = tableau.getEntry(row, column); if (Precision.equals(entry, 1d, maxUlps) && row.equals(tableau.getBasicRow(column))) { return row; } } } } // 2. apply Bland's rule to prevent cycling: // take the row for which the corresponding basic variable has the smallest index // // see http://www.stanford.edu/class/msande310/blandrule.pdf // see http://en.wikipedia.org/wiki/Bland%27s_rule (not equivalent to the above paper) // // Additional heuristic: if we did not get a solution after half of maxIterations // revert to the simple case of just returning the top-most row // This heuristic is based on empirical data gathered while investigating MATH-828. if (getEvaluations() < getMaxEvaluations() / 2) { Integer minRow = null; int minIndex = tableau.getWidth(); final int varStart = tableau.getNumObjectiveFunctions(); final int varEnd = tableau.getWidth() - 1; for (Integer row : minRatioPositions) { for (int i = varStart; i < varEnd && !row.equals(minRow); i++) { final Integer basicRow = tableau.getBasicRow(i); if (basicRow != null && basicRow.equals(row) && i < minIndex) { minIndex = i; minRow = row; } } } return minRow; } } return minRatioPositions.get(0); } /** * Runs one iteration of the Simplex method on the given model. * * @param tableau Simple tableau for the problem. * @throws TooManyIterationsException if the allowed number of iterations has been exhausted. * @throws UnboundedSolutionException if the model is found not to have a bounded solution. */ protected void doIteration(final SimplexTableau tableau) throws TooManyIterationsException, UnboundedSolutionException { incrementIterationCount(); Integer pivotCol = getPivotColumn(tableau); Integer pivotRow = getPivotRow(tableau, pivotCol); if (pivotRow == null) { throw new UnboundedSolutionException(); } // set the pivot element to 1 double pivotVal = tableau.getEntry(pivotRow, pivotCol); tableau.divideRow(pivotRow, pivotVal); // set the rest of the pivot column to 0 for (int i = 0; i < tableau.getHeight(); i++) { if (i != pivotRow) { final double multiplier = tableau.getEntry(i, pivotCol); tableau.subtractRow(i, pivotRow, multiplier); } } } /** * Solves Phase 1 of the Simplex method. * * @param tableau Simple tableau for the problem. * @throws TooManyIterationsException if the allowed number of iterations has been exhausted. * @throws UnboundedSolutionException if the model is found not to have a bounded solution. * @throws NoFeasibleSolutionException if there is no feasible solution? */ protected void solvePhase1(final SimplexTableau tableau) throws TooManyIterationsException, UnboundedSolutionException, NoFeasibleSolutionException { // make sure we're in Phase 1 if (tableau.getNumArtificialVariables() == 0) { return; } while (!tableau.isOptimal()) { doIteration(tableau); } // if W is not zero then we have no feasible solution if (!Precision.equals(tableau.getEntry(0, tableau.getRhsOffset()), 0d, epsilon)) { throw new NoFeasibleSolutionException(); } } /** {@inheritDoc} */ @Override public PointValuePair doOptimize() throws TooManyIterationsException, UnboundedSolutionException, NoFeasibleSolutionException { final SimplexTableau tableau = new SimplexTableau(getFunction(), getConstraints(), getGoalType(), isRestrictedToNonNegative(), epsilon, maxUlps, cutOff); solvePhase1(tableau); tableau.dropPhase1Objective(); while (!tableau.isOptimal()) { doIteration(tableau); } return tableau.getSolution(); } } ././@LongLink100644 0 0 147 12126630646 10263 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/optim/linear/LinearObjectiveFunction.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/optim/linear/LinearObjectiveFunction.ja100644 1750 1750 12274 12126627714 32243 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.optim.linear; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.io.Serializable; import org.apache.commons.math3.analysis.MultivariateFunction; import org.apache.commons.math3.linear.MatrixUtils; import org.apache.commons.math3.linear.RealVector; import org.apache.commons.math3.linear.ArrayRealVector; import org.apache.commons.math3.optim.OptimizationData; /** * An objective function for a linear optimization problem. *

                            * A linear objective function has one the form: *

                             * c1x1 + ... cnxn + d
                             * 
                            * The ci and d are the coefficients of the equation, * the xi are the coordinates of the current point. *

                            * * @version $Id: LinearObjectiveFunction.java 1435539 2013-01-19 13:27:24Z tn $ * @since 2.0 */ public class LinearObjectiveFunction implements MultivariateFunction, OptimizationData, Serializable { /** Serializable version identifier. */ private static final long serialVersionUID = -4531815507568396090L; /** Coefficients of the linear equation (ci). */ private final transient RealVector coefficients; /** Constant term of the linear equation. */ private final double constantTerm; /** * @param coefficients Coefficients for the linear equation being optimized. * @param constantTerm Constant term of the linear equation. */ public LinearObjectiveFunction(double[] coefficients, double constantTerm) { this(new ArrayRealVector(coefficients), constantTerm); } /** * @param coefficients Coefficients for the linear equation being optimized. * @param constantTerm Constant term of the linear equation. */ public LinearObjectiveFunction(RealVector coefficients, double constantTerm) { this.coefficients = coefficients; this.constantTerm = constantTerm; } /** * Gets the coefficients of the linear equation being optimized. * * @return coefficients of the linear equation being optimized. */ public RealVector getCoefficients() { return coefficients; } /** * Gets the constant of the linear equation being optimized. * * @return constant of the linear equation being optimized. */ public double getConstantTerm() { return constantTerm; } /** * Computes the value of the linear equation at the current point. * * @param point Point at which linear equation must be evaluated. * @return the value of the linear equation at the current point. */ public double value(final double[] point) { return value(new ArrayRealVector(point, false)); } /** * Computes the value of the linear equation at the current point. * * @param point Point at which linear equation must be evaluated. * @return the value of the linear equation at the current point. */ public double value(final RealVector point) { return coefficients.dotProduct(point) + constantTerm; } @Override public boolean equals(Object other) { if (this == other) { return true; } if (other instanceof LinearObjectiveFunction) { LinearObjectiveFunction rhs = (LinearObjectiveFunction) other; return (constantTerm == rhs.constantTerm) && coefficients.equals(rhs.coefficients); } return false; } @Override public int hashCode() { return Double.valueOf(constantTerm).hashCode() ^ coefficients.hashCode(); } /** * Serialize the instance. * @param oos stream where object should be written * @throws IOException if object cannot be written to stream */ private void writeObject(ObjectOutputStream oos) throws IOException { oos.defaultWriteObject(); MatrixUtils.serializeRealVector(coefficients, oos); } /** * Deserialize the instance. * @param ois stream from which the object should be read * @throws ClassNotFoundException if a class in the stream cannot be found * @throws IOException if object cannot be read from the stream */ private void readObject(ObjectInputStream ois) throws ClassNotFoundException, IOException { ois.defaultReadObject(); MatrixUtils.deserializeRealVector(this, "coefficients", ois); } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/optim/linear/LinearConstraintSet.java100644 1750 1750 4150 12126627714 31724 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.optim.linear; import java.util.Set; import java.util.HashSet; import java.util.Collection; import java.util.Collections; import org.apache.commons.math3.optim.OptimizationData; /** * Class that represents a set of {@link LinearConstraint linear constraints}. * * @version $Id: LinearConstraintSet.java 1435539 2013-01-19 13:27:24Z tn $ * @since 3.1 */ public class LinearConstraintSet implements OptimizationData { /** Set of constraints. */ private final Set linearConstraints = new HashSet(); /** * Creates a set containing the given constraints. * * @param constraints Constraints. */ public LinearConstraintSet(LinearConstraint... constraints) { for (LinearConstraint c : constraints) { linearConstraints.add(c); } } /** * Creates a set containing the given constraints. * * @param constraints Constraints. */ public LinearConstraintSet(Collection constraints) { linearConstraints.addAll(constraints); } /** * Gets the set of linear constraints. * * @return the constraints. */ public Collection getConstraints() { return Collections.unmodifiableSet(linearConstraints); } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/optim/linear/SimplexTableau.java100644 1750 1750 60163 12126627714 30736 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.optim.linear; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.io.Serializable; import java.util.ArrayList; import java.util.Collection; import java.util.HashSet; import java.util.List; import java.util.Set; import java.util.TreeSet; import org.apache.commons.math3.linear.Array2DRowRealMatrix; import org.apache.commons.math3.linear.MatrixUtils; import org.apache.commons.math3.linear.RealMatrix; import org.apache.commons.math3.linear.RealVector; import org.apache.commons.math3.optim.nonlinear.scalar.GoalType; import org.apache.commons.math3.optim.PointValuePair; import org.apache.commons.math3.util.FastMath; import org.apache.commons.math3.util.Precision; /** * A tableau for use in the Simplex method. * *

                            * Example: *

                             *   W |  Z |  x1 |  x2 |  x- | s1 |  s2 |  a1 |  RHS
                             * ---------------------------------------------------
                             *  -1    0    0     0     0     0     0     1     0   <= phase 1 objective
                             *   0    1   -15   -10    0     0     0     0     0   <= phase 2 objective
                             *   0    0    1     0     0     1     0     0     2   <= constraint 1
                             *   0    0    0     1     0     0     1     0     3   <= constraint 2
                             *   0    0    1     1     0     0     0     1     4   <= constraint 3
                             * 
                            * W: Phase 1 objective function
                            * Z: Phase 2 objective function
                            * x1 & x2: Decision variables
                            * x-: Extra decision variable to allow for negative values
                            * s1 & s2: Slack/Surplus variables
                            * a1: Artificial variable
                            * RHS: Right hand side
                            *

                            * @version $Id: SimplexTableau.java 1435810 2013-01-20 10:04:45Z tn $ * @since 2.0 */ class SimplexTableau implements Serializable { /** Column label for negative vars. */ private static final String NEGATIVE_VAR_COLUMN_LABEL = "x-"; /** Serializable version identifier. */ private static final long serialVersionUID = -1369660067587938365L; /** Linear objective function. */ private final LinearObjectiveFunction f; /** Linear constraints. */ private final List constraints; /** Whether to restrict the variables to non-negative values. */ private final boolean restrictToNonNegative; /** The variables each column represents */ private final List columnLabels = new ArrayList(); /** Simple tableau. */ private transient RealMatrix tableau; /** Number of decision variables. */ private final int numDecisionVariables; /** Number of slack variables. */ private final int numSlackVariables; /** Number of artificial variables. */ private int numArtificialVariables; /** Amount of error to accept when checking for optimality. */ private final double epsilon; /** Amount of error to accept in floating point comparisons. */ private final int maxUlps; /** Cut-off value for entries in the tableau. */ private final double cutOff; /** * Builds a tableau for a linear problem. * * @param f Linear objective function. * @param constraints Linear constraints. * @param goalType Optimization goal: either {@link GoalType#MAXIMIZE} * or {@link GoalType#MINIMIZE}. * @param restrictToNonNegative Whether to restrict the variables to non-negative values. * @param epsilon Amount of error to accept when checking for optimality. */ SimplexTableau(final LinearObjectiveFunction f, final Collection constraints, final GoalType goalType, final boolean restrictToNonNegative, final double epsilon) { this(f, constraints, goalType, restrictToNonNegative, epsilon, SimplexSolver.DEFAULT_ULPS, SimplexSolver.DEFAULT_CUT_OFF); } /** * Build a tableau for a linear problem. * @param f linear objective function * @param constraints linear constraints * @param goalType type of optimization goal: either {@link GoalType#MAXIMIZE} or {@link GoalType#MINIMIZE} * @param restrictToNonNegative whether to restrict the variables to non-negative values * @param epsilon amount of error to accept when checking for optimality * @param maxUlps amount of error to accept in floating point comparisons */ SimplexTableau(final LinearObjectiveFunction f, final Collection constraints, final GoalType goalType, final boolean restrictToNonNegative, final double epsilon, final int maxUlps) { this(f, constraints, goalType, restrictToNonNegative, epsilon, maxUlps, SimplexSolver.DEFAULT_CUT_OFF); } /** * Build a tableau for a linear problem. * @param f linear objective function * @param constraints linear constraints * @param goalType type of optimization goal: either {@link GoalType#MAXIMIZE} or {@link GoalType#MINIMIZE} * @param restrictToNonNegative whether to restrict the variables to non-negative values * @param epsilon amount of error to accept when checking for optimality * @param maxUlps amount of error to accept in floating point comparisons * @param cutOff the cut-off value for tableau entries */ SimplexTableau(final LinearObjectiveFunction f, final Collection constraints, final GoalType goalType, final boolean restrictToNonNegative, final double epsilon, final int maxUlps, final double cutOff) { this.f = f; this.constraints = normalizeConstraints(constraints); this.restrictToNonNegative = restrictToNonNegative; this.epsilon = epsilon; this.maxUlps = maxUlps; this.cutOff = cutOff; this.numDecisionVariables = f.getCoefficients().getDimension() + (restrictToNonNegative ? 0 : 1); this.numSlackVariables = getConstraintTypeCounts(Relationship.LEQ) + getConstraintTypeCounts(Relationship.GEQ); this.numArtificialVariables = getConstraintTypeCounts(Relationship.EQ) + getConstraintTypeCounts(Relationship.GEQ); this.tableau = createTableau(goalType == GoalType.MAXIMIZE); initializeColumnLabels(); } /** * Initialize the labels for the columns. */ protected void initializeColumnLabels() { if (getNumObjectiveFunctions() == 2) { columnLabels.add("W"); } columnLabels.add("Z"); for (int i = 0; i < getOriginalNumDecisionVariables(); i++) { columnLabels.add("x" + i); } if (!restrictToNonNegative) { columnLabels.add(NEGATIVE_VAR_COLUMN_LABEL); } for (int i = 0; i < getNumSlackVariables(); i++) { columnLabels.add("s" + i); } for (int i = 0; i < getNumArtificialVariables(); i++) { columnLabels.add("a" + i); } columnLabels.add("RHS"); } /** * Create the tableau by itself. * @param maximize if true, goal is to maximize the objective function * @return created tableau */ protected RealMatrix createTableau(final boolean maximize) { // create a matrix of the correct size int width = numDecisionVariables + numSlackVariables + numArtificialVariables + getNumObjectiveFunctions() + 1; // + 1 is for RHS int height = constraints.size() + getNumObjectiveFunctions(); Array2DRowRealMatrix matrix = new Array2DRowRealMatrix(height, width); // initialize the objective function rows if (getNumObjectiveFunctions() == 2) { matrix.setEntry(0, 0, -1); } int zIndex = (getNumObjectiveFunctions() == 1) ? 0 : 1; matrix.setEntry(zIndex, zIndex, maximize ? 1 : -1); RealVector objectiveCoefficients = maximize ? f.getCoefficients().mapMultiply(-1) : f.getCoefficients(); copyArray(objectiveCoefficients.toArray(), matrix.getDataRef()[zIndex]); matrix.setEntry(zIndex, width - 1, maximize ? f.getConstantTerm() : -1 * f.getConstantTerm()); if (!restrictToNonNegative) { matrix.setEntry(zIndex, getSlackVariableOffset() - 1, getInvertedCoefficientSum(objectiveCoefficients)); } // initialize the constraint rows int slackVar = 0; int artificialVar = 0; for (int i = 0; i < constraints.size(); i++) { LinearConstraint constraint = constraints.get(i); int row = getNumObjectiveFunctions() + i; // decision variable coefficients copyArray(constraint.getCoefficients().toArray(), matrix.getDataRef()[row]); // x- if (!restrictToNonNegative) { matrix.setEntry(row, getSlackVariableOffset() - 1, getInvertedCoefficientSum(constraint.getCoefficients())); } // RHS matrix.setEntry(row, width - 1, constraint.getValue()); // slack variables if (constraint.getRelationship() == Relationship.LEQ) { matrix.setEntry(row, getSlackVariableOffset() + slackVar++, 1); // slack } else if (constraint.getRelationship() == Relationship.GEQ) { matrix.setEntry(row, getSlackVariableOffset() + slackVar++, -1); // excess } // artificial variables if ((constraint.getRelationship() == Relationship.EQ) || (constraint.getRelationship() == Relationship.GEQ)) { matrix.setEntry(0, getArtificialVariableOffset() + artificialVar, 1); matrix.setEntry(row, getArtificialVariableOffset() + artificialVar++, 1); matrix.setRowVector(0, matrix.getRowVector(0).subtract(matrix.getRowVector(row))); } } return matrix; } /** * Get new versions of the constraints which have positive right hand sides. * @param originalConstraints original (not normalized) constraints * @return new versions of the constraints */ public List normalizeConstraints(Collection originalConstraints) { List normalized = new ArrayList(); for (LinearConstraint constraint : originalConstraints) { normalized.add(normalize(constraint)); } return normalized; } /** * Get a new equation equivalent to this one with a positive right hand side. * @param constraint reference constraint * @return new equation */ private LinearConstraint normalize(final LinearConstraint constraint) { if (constraint.getValue() < 0) { return new LinearConstraint(constraint.getCoefficients().mapMultiply(-1), constraint.getRelationship().oppositeRelationship(), -1 * constraint.getValue()); } return new LinearConstraint(constraint.getCoefficients(), constraint.getRelationship(), constraint.getValue()); } /** * Get the number of objective functions in this tableau. * @return 2 for Phase 1. 1 for Phase 2. */ protected final int getNumObjectiveFunctions() { return this.numArtificialVariables > 0 ? 2 : 1; } /** * Get a count of constraints corresponding to a specified relationship. * @param relationship relationship to count * @return number of constraint with the specified relationship */ private int getConstraintTypeCounts(final Relationship relationship) { int count = 0; for (final LinearConstraint constraint : constraints) { if (constraint.getRelationship() == relationship) { ++count; } } return count; } /** * Get the -1 times the sum of all coefficients in the given array. * @param coefficients coefficients to sum * @return the -1 times the sum of all coefficients in the given array. */ protected static double getInvertedCoefficientSum(final RealVector coefficients) { double sum = 0; for (double coefficient : coefficients.toArray()) { sum -= coefficient; } return sum; } /** * Checks whether the given column is basic. * @param col index of the column to check * @return the row that the variable is basic in. null if the column is not basic */ protected Integer getBasicRow(final int col) { Integer row = null; for (int i = 0; i < getHeight(); i++) { final double entry = getEntry(i, col); if (Precision.equals(entry, 1d, maxUlps) && (row == null)) { row = i; } else if (!Precision.equals(entry, 0d, maxUlps)) { return null; } } return row; } /** * Removes the phase 1 objective function, positive cost non-artificial variables, * and the non-basic artificial variables from this tableau. */ protected void dropPhase1Objective() { if (getNumObjectiveFunctions() == 1) { return; } Set columnsToDrop = new TreeSet(); columnsToDrop.add(0); // positive cost non-artificial variables for (int i = getNumObjectiveFunctions(); i < getArtificialVariableOffset(); i++) { final double entry = tableau.getEntry(0, i); if (Precision.compareTo(entry, 0d, epsilon) > 0) { columnsToDrop.add(i); } } // non-basic artificial variables for (int i = 0; i < getNumArtificialVariables(); i++) { int col = i + getArtificialVariableOffset(); if (getBasicRow(col) == null) { columnsToDrop.add(col); } } double[][] matrix = new double[getHeight() - 1][getWidth() - columnsToDrop.size()]; for (int i = 1; i < getHeight(); i++) { int col = 0; for (int j = 0; j < getWidth(); j++) { if (!columnsToDrop.contains(j)) { matrix[i - 1][col++] = tableau.getEntry(i, j); } } } // remove the columns in reverse order so the indices are correct Integer[] drop = columnsToDrop.toArray(new Integer[columnsToDrop.size()]); for (int i = drop.length - 1; i >= 0; i--) { columnLabels.remove((int) drop[i]); } this.tableau = new Array2DRowRealMatrix(matrix); this.numArtificialVariables = 0; } /** * @param src the source array * @param dest the destination array */ private void copyArray(final double[] src, final double[] dest) { System.arraycopy(src, 0, dest, getNumObjectiveFunctions(), src.length); } /** * Returns whether the problem is at an optimal state. * @return whether the model has been solved */ boolean isOptimal() { for (int i = getNumObjectiveFunctions(); i < getWidth() - 1; i++) { final double entry = tableau.getEntry(0, i); if (Precision.compareTo(entry, 0d, epsilon) < 0) { return false; } } return true; } /** * Get the current solution. * @return current solution */ protected PointValuePair getSolution() { int negativeVarColumn = columnLabels.indexOf(NEGATIVE_VAR_COLUMN_LABEL); Integer negativeVarBasicRow = negativeVarColumn > 0 ? getBasicRow(negativeVarColumn) : null; double mostNegative = negativeVarBasicRow == null ? 0 : getEntry(negativeVarBasicRow, getRhsOffset()); Set basicRows = new HashSet(); double[] coefficients = new double[getOriginalNumDecisionVariables()]; for (int i = 0; i < coefficients.length; i++) { int colIndex = columnLabels.indexOf("x" + i); if (colIndex < 0) { coefficients[i] = 0; continue; } Integer basicRow = getBasicRow(colIndex); if (basicRow != null && basicRow == 0) { // if the basic row is found to be the objective function row // set the coefficient to 0 -> this case handles unconstrained // variables that are still part of the objective function coefficients[i] = 0; } else if (basicRows.contains(basicRow)) { // if multiple variables can take a given value // then we choose the first and set the rest equal to 0 coefficients[i] = 0 - (restrictToNonNegative ? 0 : mostNegative); } else { basicRows.add(basicRow); coefficients[i] = (basicRow == null ? 0 : getEntry(basicRow, getRhsOffset())) - (restrictToNonNegative ? 0 : mostNegative); } } return new PointValuePair(coefficients, f.value(coefficients)); } /** * Subtracts a multiple of one row from another. *

                            * After application of this operation, the following will hold: *

                            minuendRow = minuendRow - multiple * subtrahendRow
                            * * @param dividendRow index of the row * @param divisor value of the divisor */ protected void divideRow(final int dividendRow, final double divisor) { for (int j = 0; j < getWidth(); j++) { tableau.setEntry(dividendRow, j, tableau.getEntry(dividendRow, j) / divisor); } } /** * Subtracts a multiple of one row from another. *

                            * After application of this operation, the following will hold: *

                            minuendRow = minuendRow - multiple * subtrahendRow
                            * * @param minuendRow row index * @param subtrahendRow row index * @param multiple multiplication factor */ protected void subtractRow(final int minuendRow, final int subtrahendRow, final double multiple) { for (int i = 0; i < getWidth(); i++) { double result = tableau.getEntry(minuendRow, i) - tableau.getEntry(subtrahendRow, i) * multiple; // cut-off values smaller than the cut-off threshold, otherwise may lead to numerical instabilities if (FastMath.abs(result) < cutOff) { result = 0.0; } tableau.setEntry(minuendRow, i, result); } } /** * Get the width of the tableau. * @return width of the tableau */ protected final int getWidth() { return tableau.getColumnDimension(); } /** * Get the height of the tableau. * @return height of the tableau */ protected final int getHeight() { return tableau.getRowDimension(); } /** * Get an entry of the tableau. * @param row row index * @param column column index * @return entry at (row, column) */ protected final double getEntry(final int row, final int column) { return tableau.getEntry(row, column); } /** * Set an entry of the tableau. * @param row row index * @param column column index * @param value for the entry */ protected final void setEntry(final int row, final int column, final double value) { tableau.setEntry(row, column, value); } /** * Get the offset of the first slack variable. * @return offset of the first slack variable */ protected final int getSlackVariableOffset() { return getNumObjectiveFunctions() + numDecisionVariables; } /** * Get the offset of the first artificial variable. * @return offset of the first artificial variable */ protected final int getArtificialVariableOffset() { return getNumObjectiveFunctions() + numDecisionVariables + numSlackVariables; } /** * Get the offset of the right hand side. * @return offset of the right hand side */ protected final int getRhsOffset() { return getWidth() - 1; } /** * Get the number of decision variables. *

                            * If variables are not restricted to positive values, this will include 1 extra decision variable to represent * the absolute value of the most negative variable. * * @return number of decision variables * @see #getOriginalNumDecisionVariables() */ protected final int getNumDecisionVariables() { return numDecisionVariables; } /** * Get the original number of decision variables. * @return original number of decision variables * @see #getNumDecisionVariables() */ protected final int getOriginalNumDecisionVariables() { return f.getCoefficients().getDimension(); } /** * Get the number of slack variables. * @return number of slack variables */ protected final int getNumSlackVariables() { return numSlackVariables; } /** * Get the number of artificial variables. * @return number of artificial variables */ protected final int getNumArtificialVariables() { return numArtificialVariables; } /** * Get the tableau data. * @return tableau data */ protected final double[][] getData() { return tableau.getData(); } @Override public boolean equals(Object other) { if (this == other) { return true; } if (other instanceof SimplexTableau) { SimplexTableau rhs = (SimplexTableau) other; return (restrictToNonNegative == rhs.restrictToNonNegative) && (numDecisionVariables == rhs.numDecisionVariables) && (numSlackVariables == rhs.numSlackVariables) && (numArtificialVariables == rhs.numArtificialVariables) && (epsilon == rhs.epsilon) && (maxUlps == rhs.maxUlps) && f.equals(rhs.f) && constraints.equals(rhs.constraints) && tableau.equals(rhs.tableau); } return false; } @Override public int hashCode() { return Boolean.valueOf(restrictToNonNegative).hashCode() ^ numDecisionVariables ^ numSlackVariables ^ numArtificialVariables ^ Double.valueOf(epsilon).hashCode() ^ maxUlps ^ f.hashCode() ^ constraints.hashCode() ^ tableau.hashCode(); } /** * Serialize the instance. * @param oos stream where object should be written * @throws IOException if object cannot be written to stream */ private void writeObject(ObjectOutputStream oos) throws IOException { oos.defaultWriteObject(); MatrixUtils.serializeRealMatrix(tableau, oos); } /** * Deserialize the instance. * @param ois stream from which the object should be read * @throws ClassNotFoundException if a class in the stream cannot be found * @throws IOException if object cannot be read from the stream */ private void readObject(ObjectInputStream ois) throws ClassNotFoundException, IOException { ois.defaultReadObject(); MatrixUtils.deserializeRealMatrix(this, "tableau", ois); } } ././@LongLink100644 0 0 152 12126630646 10257 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/optim/linear/UnboundedSolutionException.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/optim/linear/UnboundedSolutionException100644 1750 1750 3014 12126627714 32406 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.optim.linear; import org.apache.commons.math3.exception.MathIllegalStateException; import org.apache.commons.math3.exception.util.LocalizedFormats; /** * This class represents exceptions thrown by optimizers when a solution escapes to infinity. * * @version $Id: UnboundedSolutionException.java 1435539 2013-01-19 13:27:24Z tn $ * @since 2.0 */ public class UnboundedSolutionException extends MathIllegalStateException { /** Serializable version identifier. */ private static final long serialVersionUID = 940539497277290619L; /** * Simple constructor using a default message. */ public UnboundedSolutionException() { super(LocalizedFormats.UNBOUNDED_SOLUTION); } } ././@LongLink100644 0 0 146 12126630646 10262 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/optim/nonlinear/vector/package-info.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/optim/nonlinear/vector/package-info.jav100644 1750 1750 1625 12126627713 32174 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ /** * Algorithms for optimizing a vector function. */ package org.apache.commons.math3.optim.nonlinear.vector; ././@LongLink100644 0 0 175 12126630646 10264 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/optim/nonlinear/vector/JacobianMultivariateVectorOptimizer.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/optim/nonlinear/vector/JacobianMultivar100644 1750 1750 10445 12126627713 32343 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.optim.nonlinear.vector; import org.apache.commons.math3.analysis.MultivariateMatrixFunction; import org.apache.commons.math3.optim.ConvergenceChecker; import org.apache.commons.math3.optim.OptimizationData; import org.apache.commons.math3.optim.PointVectorValuePair; import org.apache.commons.math3.exception.TooManyEvaluationsException; import org.apache.commons.math3.exception.DimensionMismatchException; /** * Base class for implementing optimizers for multivariate vector * differentiable functions. * It contains boiler-plate code for dealing with Jacobian evaluation. * It assumes that the rows of the Jacobian matrix iterate on the model * functions while the columns iterate on the parameters; thus, the numbers * of rows is equal to the dimension of the {@link Target} while the * number of columns is equal to the dimension of the * {@link org.apache.commons.math3.optim.InitialGuess InitialGuess}. * * @version $Id: JacobianMultivariateVectorOptimizer.java 1454464 2013-03-08 16:58:10Z luc $ * @since 3.1 */ public abstract class JacobianMultivariateVectorOptimizer extends MultivariateVectorOptimizer { /** * Jacobian of the model function. */ private MultivariateMatrixFunction jacobian; /** * @param checker Convergence checker. */ protected JacobianMultivariateVectorOptimizer(ConvergenceChecker checker) { super(checker); } /** * Computes the Jacobian matrix. * * @param params Point at which the Jacobian must be evaluated. * @return the Jacobian at the specified point. */ protected double[][] computeJacobian(final double[] params) { return jacobian.value(params); } /** * {@inheritDoc} * * @param optData Optimization data. In addition to those documented in * {@link MultivariateVectorOptimizer#optimize(OptimizationData...)} * MultivariateOptimizer}, this method will register the following data: *

                              *
                            • {@link ModelFunctionJacobian}
                            • *
                            * @return {@inheritDoc} * @throws TooManyEvaluationsException if the maximal number of * evaluations is exceeded. * @throws DimensionMismatchException if the initial guess, target, and weight * arguments have inconsistent dimensions. */ @Override public PointVectorValuePair optimize(OptimizationData... optData) throws TooManyEvaluationsException, DimensionMismatchException { // Set up base class and perform computation. return super.optimize(optData); } /** * Scans the list of (required and optional) optimization data that * characterize the problem. * * @param optData Optimization data. * The following data will be looked for: *
                              *
                            • {@link ModelFunctionJacobian}
                            • *
                            */ @Override protected void parseOptimizationData(OptimizationData... optData) { // Allow base class to register its own data. super.parseOptimizationData(optData); // The existing values (as set by the previous call) are reused if // not provided in the argument list. for (OptimizationData data : optData) { if (data instanceof ModelFunctionJacobian) { jacobian = ((ModelFunctionJacobian) data).getModelFunctionJacobian(); // If more data must be parsed, this statement _must_ be // changed to "continue". break; } } } } ././@LongLink100644 0 0 147 12126630646 10263 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/optim/nonlinear/vector/ModelFunction.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/optim/nonlinear/vector/ModelFunction.ja100644 1750 1750 3117 12126627713 32226 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.optim.nonlinear.vector; import org.apache.commons.math3.analysis.MultivariateVectorFunction; import org.apache.commons.math3.optim.OptimizationData; /** * Model (vector) function to be optimized. * * @version $Id: ModelFunction.java 1435539 2013-01-19 13:27:24Z tn $ * @since 3.1 */ public class ModelFunction implements OptimizationData { /** Function to be optimized. */ private final MultivariateVectorFunction model; /** * @param m Model function to be optimized. */ public ModelFunction(MultivariateVectorFunction m) { model = m; } /** * Gets the model function to be optimized. * * @return the model function. */ public MultivariateVectorFunction getModelFunction() { return model; } } ././@LongLink100644 0 0 177 12126630646 10266 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/optim/nonlinear/vector/MultiStartMultivariateVectorOptimizer.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/optim/nonlinear/vector/MultiStartMultiv100644 1750 1750 11134 12126627713 32416 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.optim.nonlinear.vector; import java.util.Collections; import java.util.List; import java.util.ArrayList; import java.util.Comparator; import org.apache.commons.math3.exception.NotStrictlyPositiveException; import org.apache.commons.math3.exception.NullArgumentException; import org.apache.commons.math3.linear.RealMatrix; import org.apache.commons.math3.linear.RealVector; import org.apache.commons.math3.linear.ArrayRealVector; import org.apache.commons.math3.random.RandomVectorGenerator; import org.apache.commons.math3.optim.BaseMultiStartMultivariateOptimizer; import org.apache.commons.math3.optim.PointVectorValuePair; /** * Multi-start optimizer for a (vector) model function. * * This class wraps an optimizer in order to use it several times in * turn with different starting points (trying to avoid being trapped * in a local extremum when looking for a global one). * * @version $Id: MultiStartMultivariateVectorOptimizer.java 1435539 2013-01-19 13:27:24Z tn $ * @since 3.0 */ public class MultiStartMultivariateVectorOptimizer extends BaseMultiStartMultivariateOptimizer { /** Underlying optimizer. */ private final MultivariateVectorOptimizer optimizer; /** Found optima. */ private final List optima = new ArrayList(); /** * Create a multi-start optimizer from a single-start optimizer. * * @param optimizer Single-start optimizer to wrap. * @param starts Number of starts to perform. * If {@code starts == 1}, the result will be same as if {@code optimizer} * is called directly. * @param generator Random vector generator to use for restarts. * @throws NullArgumentException if {@code optimizer} or {@code generator} * is {@code null}. * @throws NotStrictlyPositiveException if {@code starts < 1}. */ public MultiStartMultivariateVectorOptimizer(final MultivariateVectorOptimizer optimizer, final int starts, final RandomVectorGenerator generator) throws NullArgumentException, NotStrictlyPositiveException { super(optimizer, starts, generator); this.optimizer = optimizer; } /** * {@inheritDoc} */ @Override public PointVectorValuePair[] getOptima() { Collections.sort(optima, getPairComparator()); return optima.toArray(new PointVectorValuePair[0]); } /** * {@inheritDoc} */ @Override protected void store(PointVectorValuePair optimum) { optima.add(optimum); } /** * {@inheritDoc} */ @Override protected void clear() { optima.clear(); } /** * @return a comparator for sorting the optima. */ private Comparator getPairComparator() { return new Comparator() { private final RealVector target = new ArrayRealVector(optimizer.getTarget(), false); private final RealMatrix weight = optimizer.getWeight(); public int compare(final PointVectorValuePair o1, final PointVectorValuePair o2) { if (o1 == null) { return (o2 == null) ? 0 : 1; } else if (o2 == null) { return -1; } return Double.compare(weightedResidual(o1), weightedResidual(o2)); } private double weightedResidual(final PointVectorValuePair pv) { final RealVector v = new ArrayRealVector(pv.getValueRef(), false); final RealVector r = target.subtract(v); return r.dotProduct(weight.operate(r)); } }; } } ././@LongLink100644 0 0 165 12126630646 10263 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/optim/nonlinear/vector/MultivariateVectorOptimizer.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/optim/nonlinear/vector/MultivariateVect100644 1750 1750 13311 12126627713 32374 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.optim.nonlinear.vector; import org.apache.commons.math3.exception.TooManyEvaluationsException; import org.apache.commons.math3.exception.DimensionMismatchException; import org.apache.commons.math3.analysis.MultivariateVectorFunction; import org.apache.commons.math3.optim.OptimizationData; import org.apache.commons.math3.optim.BaseMultivariateOptimizer; import org.apache.commons.math3.optim.ConvergenceChecker; import org.apache.commons.math3.optim.PointVectorValuePair; import org.apache.commons.math3.linear.RealMatrix; /** * Base class for a multivariate vector function optimizer. * * @version $Id: MultivariateVectorOptimizer.java 1443444 2013-02-07 12:41:36Z erans $ * @since 3.1 */ public abstract class MultivariateVectorOptimizer extends BaseMultivariateOptimizer { /** Target values for the model function at optimum. */ private double[] target; /** Weight matrix. */ private RealMatrix weightMatrix; /** Model function. */ private MultivariateVectorFunction model; /** * @param checker Convergence checker. */ protected MultivariateVectorOptimizer(ConvergenceChecker checker) { super(checker); } /** * Computes the objective function value. * This method must be called by subclasses to enforce the * evaluation counter limit. * * @param params Point at which the objective function must be evaluated. * @return the objective function value at the specified point. * @throws TooManyEvaluationsException if the maximal number of evaluations * (of the model vector function) is exceeded. */ protected double[] computeObjectiveValue(double[] params) { super.incrementEvaluationCount(); return model.value(params); } /** * {@inheritDoc} * * @param optData Optimization data. In addition to those documented in * {@link BaseMultivariateOptimizer#parseOptimizationData(OptimizationData[]) * BaseMultivariateOptimizer}, this method will register the following data: *
                              *
                            • {@link Target}
                            • *
                            • {@link Weight}
                            • *
                            • {@link ModelFunction}
                            • *
                            * @return {@inheritDoc} * @throws TooManyEvaluationsException if the maximal number of * evaluations is exceeded. * @throws DimensionMismatchException if the initial guess, target, and weight * arguments have inconsistent dimensions. */ public PointVectorValuePair optimize(OptimizationData... optData) throws TooManyEvaluationsException, DimensionMismatchException { // Set up base class and perform computation. return super.optimize(optData); } /** * Gets the weight matrix of the observations. * * @return the weight matrix. */ public RealMatrix getWeight() { return weightMatrix.copy(); } /** * Gets the observed values to be matched by the objective vector * function. * * @return the target values. */ public double[] getTarget() { return target.clone(); } /** * Gets the number of observed values. * * @return the length of the target vector. */ public int getTargetSize() { return target.length; } /** * Scans the list of (required and optional) optimization data that * characterize the problem. * * @param optData Optimization data. The following data will be looked for: *
                              *
                            • {@link Target}
                            • *
                            • {@link Weight}
                            • *
                            • {@link ModelFunction}
                            • *
                            */ @Override protected void parseOptimizationData(OptimizationData... optData) { // Allow base class to register its own data. super.parseOptimizationData(optData); // The existing values (as set by the previous call) are reused if // not provided in the argument list. for (OptimizationData data : optData) { if (data instanceof ModelFunction) { model = ((ModelFunction) data).getModelFunction(); continue; } if (data instanceof Target) { target = ((Target) data).getTarget(); continue; } if (data instanceof Weight) { weightMatrix = ((Weight) data).getWeight(); continue; } } // Check input consistency. checkParameters(); } /** * Check parameters consistency. * * @throws DimensionMismatchException if {@link #target} and * {@link #weightMatrix} have inconsistent dimensions. */ private void checkParameters() { if (target.length != weightMatrix.getColumnDimension()) { throw new DimensionMismatchException(target.length, weightMatrix.getColumnDimension()); } } } ././@LongLink100644 0 0 157 12126630646 10264 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/optim/nonlinear/vector/ModelFunctionJacobian.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/optim/nonlinear/vector/ModelFunctionJac100644 1750 1750 3261 12126627713 32253 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.optim.nonlinear.vector; import org.apache.commons.math3.analysis.MultivariateMatrixFunction; import org.apache.commons.math3.optim.OptimizationData; /** * Jacobian of the model (vector) function to be optimized. * * @version $Id: ModelFunctionJacobian.java 1435539 2013-01-19 13:27:24Z tn $ * @since 3.1 */ public class ModelFunctionJacobian implements OptimizationData { /** Function to be optimized. */ private final MultivariateMatrixFunction jacobian; /** * @param j Jacobian of the model function to be optimized. */ public ModelFunctionJacobian(MultivariateMatrixFunction j) { jacobian = j; } /** * Gets the Jacobian of the model function to be optimized. * * @return the model function Jacobian. */ public MultivariateMatrixFunction getModelFunctionJacobian() { return jacobian; } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/optim/nonlinear/vector/Target.java100644 1750 1750 3176 12126627713 31242 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.optim.nonlinear.vector; import org.apache.commons.math3.optim.OptimizationData; /** * Target of the optimization procedure. * They are the values which the objective vector function must reproduce * When the parameters of the model have been optimized. *
                            * Immutable class. * * @version $Id: Target.java 1435539 2013-01-19 13:27:24Z tn $ * @since 3.1 */ public class Target implements OptimizationData { /** Target values (of the objective vector function). */ private final double[] target; /** * @param observations Target values. */ public Target(double[] observations) { target = observations.clone(); } /** * Gets the initial guess. * * @return the initial guess. */ public double[] getTarget() { return target.clone(); } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/optim/nonlinear/vector/Weight.java100644 1750 1750 4267 12126627713 31245 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.optim.nonlinear.vector; import org.apache.commons.math3.optim.OptimizationData; import org.apache.commons.math3.linear.RealMatrix; import org.apache.commons.math3.linear.DiagonalMatrix; import org.apache.commons.math3.linear.NonSquareMatrixException; /** * Weight matrix of the residuals between model and observations. *
                            * Immutable class. * * @version $Id: Weight.java 1435539 2013-01-19 13:27:24Z tn $ * @since 3.1 */ public class Weight implements OptimizationData { /** Weight matrix. */ private final RealMatrix weightMatrix; /** * Creates a diagonal weight matrix. * * @param weight List of the values of the diagonal. */ public Weight(double[] weight) { weightMatrix = new DiagonalMatrix(weight); } /** * @param weight Weight matrix. * @throws NonSquareMatrixException if the argument is not * a square matrix. */ public Weight(RealMatrix weight) { if (weight.getColumnDimension() != weight.getRowDimension()) { throw new NonSquareMatrixException(weight.getColumnDimension(), weight.getRowDimension()); } weightMatrix = weight.copy(); } /** * Gets the initial guess. * * @return the initial guess. */ public RealMatrix getWeight() { return weightMatrix.copy(); } } ././@LongLink100644 0 0 157 12126630646 10264 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/optim/nonlinear/vector/jacobian/package-info.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/optim/nonlinear/vector/jacobian/package100644 1750 1750 1671 12126627713 32233 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ /** * This package provides optimization algorithms that require derivatives. */ package org.apache.commons.math3.optim.nonlinear.vector.jacobian; ././@LongLink100644 0 0 200 12126630646 10251 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/optim/nonlinear/vector/jacobian/AbstractLeastSquaresOptimizer.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/optim/nonlinear/vector/jacobian/Abstrac100644 1750 1750 24563 12126627713 32244 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.optim.nonlinear.vector.jacobian; import org.apache.commons.math3.exception.DimensionMismatchException; import org.apache.commons.math3.exception.TooManyEvaluationsException; import org.apache.commons.math3.linear.ArrayRealVector; import org.apache.commons.math3.linear.RealMatrix; import org.apache.commons.math3.linear.DiagonalMatrix; import org.apache.commons.math3.linear.DecompositionSolver; import org.apache.commons.math3.linear.MatrixUtils; import org.apache.commons.math3.linear.QRDecomposition; import org.apache.commons.math3.linear.EigenDecomposition; import org.apache.commons.math3.optim.OptimizationData; import org.apache.commons.math3.optim.ConvergenceChecker; import org.apache.commons.math3.optim.PointVectorValuePair; import org.apache.commons.math3.optim.nonlinear.vector.Weight; import org.apache.commons.math3.optim.nonlinear.vector.JacobianMultivariateVectorOptimizer; import org.apache.commons.math3.util.FastMath; /** * Base class for implementing least-squares optimizers. * It provides methods for error estimation. * * @version $Id: AbstractLeastSquaresOptimizer.java 1443444 2013-02-07 12:41:36Z erans $ * @since 3.1 */ public abstract class AbstractLeastSquaresOptimizer extends JacobianMultivariateVectorOptimizer { /** Square-root of the weight matrix. */ private RealMatrix weightMatrixSqrt; /** Cost value (square root of the sum of the residuals). */ private double cost; /** * @param checker Convergence checker. */ protected AbstractLeastSquaresOptimizer(ConvergenceChecker checker) { super(checker); } /** * Computes the weighted Jacobian matrix. * * @param params Model parameters at which to compute the Jacobian. * @return the weighted Jacobian: W1/2 J. * @throws DimensionMismatchException if the Jacobian dimension does not * match problem dimension. */ protected RealMatrix computeWeightedJacobian(double[] params) { return weightMatrixSqrt.multiply(MatrixUtils.createRealMatrix(computeJacobian(params))); } /** * Computes the cost. * * @param residuals Residuals. * @return the cost. * @see #computeResiduals(double[]) */ protected double computeCost(double[] residuals) { final ArrayRealVector r = new ArrayRealVector(residuals); return FastMath.sqrt(r.dotProduct(getWeight().operate(r))); } /** * Gets the root-mean-square (RMS) value. * * The RMS the root of the arithmetic mean of the square of all weighted * residuals. * This is related to the criterion that is minimized by the optimizer * as follows: If c if the criterion, and n is the * number of measurements, then the RMS is sqrt (c/n). * * @return the RMS value. */ public double getRMS() { return FastMath.sqrt(getChiSquare() / getTargetSize()); } /** * Get a Chi-Square-like value assuming the N residuals follow N * distinct normal distributions centered on 0 and whose variances are * the reciprocal of the weights. * @return chi-square value */ public double getChiSquare() { return cost * cost; } /** * Gets the square-root of the weight matrix. * * @return the square-root of the weight matrix. */ public RealMatrix getWeightSquareRoot() { return weightMatrixSqrt.copy(); } /** * Sets the cost. * * @param cost Cost value. */ protected void setCost(double cost) { this.cost = cost; } /** * Get the covariance matrix of the optimized parameters. *
                            * Note that this operation involves the inversion of the * JTJ matrix, where {@code J} is the * Jacobian matrix. * The {@code threshold} parameter is a way for the caller to specify * that the result of this computation should be considered meaningless, * and thus trigger an exception. * * @param params Model parameters. * @param threshold Singularity threshold. * @return the covariance matrix. * @throws org.apache.commons.math3.linear.SingularMatrixException * if the covariance matrix cannot be computed (singular problem). */ public double[][] computeCovariances(double[] params, double threshold) { // Set up the Jacobian. final RealMatrix j = computeWeightedJacobian(params); // Compute transpose(J)J. final RealMatrix jTj = j.transpose().multiply(j); // Compute the covariances matrix. final DecompositionSolver solver = new QRDecomposition(jTj, threshold).getSolver(); return solver.getInverse().getData(); } /** * Computes an estimate of the standard deviation of the parameters. The * returned values are the square root of the diagonal coefficients of the * covariance matrix, {@code sd(a[i]) ~= sqrt(C[i][i])}, where {@code a[i]} * is the optimized value of the {@code i}-th parameter, and {@code C} is * the covariance matrix. * * @param params Model parameters. * @param covarianceSingularityThreshold Singularity threshold (see * {@link #computeCovariances(double[],double) computeCovariances}). * @return an estimate of the standard deviation of the optimized parameters * @throws org.apache.commons.math3.linear.SingularMatrixException * if the covariance matrix cannot be computed. */ public double[] computeSigma(double[] params, double covarianceSingularityThreshold) { final int nC = params.length; final double[] sig = new double[nC]; final double[][] cov = computeCovariances(params, covarianceSingularityThreshold); for (int i = 0; i < nC; ++i) { sig[i] = FastMath.sqrt(cov[i][i]); } return sig; } /** * {@inheritDoc} * * @param optData Optimization data. In addition to those documented in * {@link JacobianMultivariateVectorOptimizer#parseOptimizationData(OptimizationData[]) * JacobianMultivariateVectorOptimizer}, this method will register the following data: *
                              *
                            • {@link org.apache.commons.math3.optim.nonlinear.vector.Weight}
                            • *
                            * @return {@inheritDoc} * @throws TooManyEvaluationsException if the maximal number of * evaluations is exceeded. * @throws DimensionMismatchException if the initial guess, target, and weight * arguments have inconsistent dimensions. */ @Override public PointVectorValuePair optimize(OptimizationData... optData) throws TooManyEvaluationsException { // Set up base class and perform computation. return super.optimize(optData); } /** * Computes the residuals. * The residual is the difference between the observed (target) * values and the model (objective function) value. * There is one residual for each element of the vector-valued * function. * * @param objectiveValue Value of the the objective function. This is * the value returned from a call to * {@link #computeObjectiveValue(double[]) computeObjectiveValue} * (whose array argument contains the model parameters). * @return the residuals. * @throws DimensionMismatchException if {@code params} has a wrong * length. */ protected double[] computeResiduals(double[] objectiveValue) { final double[] target = getTarget(); if (objectiveValue.length != target.length) { throw new DimensionMismatchException(target.length, objectiveValue.length); } final double[] residuals = new double[target.length]; for (int i = 0; i < target.length; i++) { residuals[i] = target[i] - objectiveValue[i]; } return residuals; } /** * Scans the list of (required and optional) optimization data that * characterize the problem. * If the weight matrix is specified, the {@link #weightMatrixSqrt} * field is recomputed. * * @param optData Optimization data. The following data will be looked for: *
                              *
                            • {@link Weight}
                            • *
                            */ @Override protected void parseOptimizationData(OptimizationData... optData) { // Allow base class to register its own data. super.parseOptimizationData(optData); // The existing values (as set by the previous call) are reused if // not provided in the argument list. for (OptimizationData data : optData) { if (data instanceof Weight) { weightMatrixSqrt = squareRoot(((Weight) data).getWeight()); // If more data must be parsed, this statement _must_ be // changed to "continue". break; } } } /** * Computes the square-root of the weight matrix. * * @param m Symmetric, positive-definite (weight) matrix. * @return the square-root of the weight matrix. */ private RealMatrix squareRoot(RealMatrix m) { if (m instanceof DiagonalMatrix) { final int dim = m.getRowDimension(); final RealMatrix sqrtM = new DiagonalMatrix(dim); for (int i = 0; i < dim; i++) { sqrtM.setEntry(i, i, FastMath.sqrt(m.getEntry(i, i))); } return sqrtM; } else { final EigenDecomposition dec = new EigenDecomposition(m); return dec.getSquareRoot(); } } } ././@LongLink100644 0 0 176 12126630646 10265 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/optim/nonlinear/vector/jacobian/LevenbergMarquardtOptimizer.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/optim/nonlinear/vector/jacobian/Levenbe100644 1750 1750 117021 12126627713 32255 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.optim.nonlinear.vector.jacobian; import java.util.Arrays; import org.apache.commons.math3.exception.ConvergenceException; import org.apache.commons.math3.exception.MathUnsupportedOperationException; import org.apache.commons.math3.exception.util.LocalizedFormats; import org.apache.commons.math3.optim.PointVectorValuePair; import org.apache.commons.math3.optim.ConvergenceChecker; import org.apache.commons.math3.linear.RealMatrix; import org.apache.commons.math3.util.Precision; import org.apache.commons.math3.util.FastMath; /** * This class solves a least-squares problem using the Levenberg-Marquardt * algorithm. *
                            * Constraints are not supported: the call to * {@link #optimize(OptimizationData[]) optimize} will throw * {@link MathUnsupportedOperationException} if bounds are passed to it. * *

                            This implementation should work even for over-determined systems * (i.e. systems having more point than equations). Over-determined systems * are solved by ignoring the point which have the smallest impact according * to their jacobian column norm. Only the rank of the matrix and some loop bounds * are changed to implement this.

                            * *

                            The resolution engine is a simple translation of the MINPACK lmder routine with minor * changes. The changes include the over-determined resolution, the use of * inherited convergence checker and the Q.R. decomposition which has been * rewritten following the algorithm described in the * P. Lascaux and R. Theodor book Analyse numérique matricielle * appliquée à l'art de l'ingénieur, Masson 1986.

                            *

                            The authors of the original fortran version are: *

                              *
                            • Argonne National Laboratory. MINPACK project. March 1980
                            • *
                            • Burton S. Garbow
                            • *
                            • Kenneth E. Hillstrom
                            • *
                            • Jorge J. More
                            • *
                            * The redistribution policy for MINPACK is available here, for convenience, it * is reproduced below.

                            * * * * *
                            * Minpack Copyright Notice (1999) University of Chicago. * All rights reserved *
                            * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: *
                              *
                            1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer.
                            2. *
                            3. Redistributions in binary form must reproduce the above * copyright notice, this list of conditions and the following * disclaimer in the documentation and/or other materials provided * with the distribution.
                            4. *
                            5. The end-user documentation included with the redistribution, if any, * must include the following acknowledgment: * This product includes software developed by the University of * Chicago, as Operator of Argonne National Laboratory. * Alternately, this acknowledgment may appear in the software itself, * if and wherever such third-party acknowledgments normally appear.
                            6. *
                            7. WARRANTY DISCLAIMER. THE SOFTWARE IS SUPPLIED "AS IS" * WITHOUT WARRANTY OF ANY KIND. THE COPYRIGHT HOLDER, THE * UNITED STATES, THE UNITED STATES DEPARTMENT OF ENERGY, AND * THEIR EMPLOYEES: (1) DISCLAIM ANY WARRANTIES, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO ANY IMPLIED WARRANTIES * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE * OR NON-INFRINGEMENT, (2) DO NOT ASSUME ANY LEGAL LIABILITY * OR RESPONSIBILITY FOR THE ACCURACY, COMPLETENESS, OR * USEFULNESS OF THE SOFTWARE, (3) DO NOT REPRESENT THAT USE OF * THE SOFTWARE WOULD NOT INFRINGE PRIVATELY OWNED RIGHTS, (4) * DO NOT WARRANT THAT THE SOFTWARE WILL FUNCTION * UNINTERRUPTED, THAT IT IS ERROR-FREE OR THAT ANY ERRORS WILL * BE CORRECTED.
                            8. *
                            9. LIMITATION OF LIABILITY. IN NO EVENT WILL THE COPYRIGHT * HOLDER, THE UNITED STATES, THE UNITED STATES DEPARTMENT OF * ENERGY, OR THEIR EMPLOYEES: BE LIABLE FOR ANY INDIRECT, * INCIDENTAL, CONSEQUENTIAL, SPECIAL OR PUNITIVE DAMAGES OF * ANY KIND OR NATURE, INCLUDING BUT NOT LIMITED TO LOSS OF * PROFITS OR LOSS OF DATA, FOR ANY REASON WHATSOEVER, WHETHER * SUCH LIABILITY IS ASSERTED ON THE BASIS OF CONTRACT, TORT * (INCLUDING NEGLIGENCE OR STRICT LIABILITY), OR OTHERWISE, * EVEN IF ANY OF SAID PARTIES HAS BEEN WARNED OF THE * POSSIBILITY OF SUCH LOSS OR DAMAGES.
                            10. *
                              * * @version $Id: LevenbergMarquardtOptimizer.java 1462503 2013-03-29 15:48:27Z luc $ * @since 2.0 */ public class LevenbergMarquardtOptimizer extends AbstractLeastSquaresOptimizer { /** Twice the "epsilon machine". */ private static final double TWO_EPS = 2 * Precision.EPSILON; /** Number of solved point. */ private int solvedCols; /** Diagonal elements of the R matrix in the Q.R. decomposition. */ private double[] diagR; /** Norms of the columns of the jacobian matrix. */ private double[] jacNorm; /** Coefficients of the Householder transforms vectors. */ private double[] beta; /** Columns permutation array. */ private int[] permutation; /** Rank of the jacobian matrix. */ private int rank; /** Levenberg-Marquardt parameter. */ private double lmPar; /** Parameters evolution direction associated with lmPar. */ private double[] lmDir; /** Positive input variable used in determining the initial step bound. */ private final double initialStepBoundFactor; /** Desired relative error in the sum of squares. */ private final double costRelativeTolerance; /** Desired relative error in the approximate solution parameters. */ private final double parRelativeTolerance; /** Desired max cosine on the orthogonality between the function vector * and the columns of the jacobian. */ private final double orthoTolerance; /** Threshold for QR ranking. */ private final double qrRankingThreshold; /** Weighted residuals. */ private double[] weightedResidual; /** Weighted Jacobian. */ private double[][] weightedJacobian; /** * Build an optimizer for least squares problems with default values * for all the tuning parameters (see the {@link * #LevenbergMarquardtOptimizer(double,double,double,double,double) * other contructor}. * The default values for the algorithm settings are: *
                                *
                              • Initial step bound factor: 100
                              • *
                              • Cost relative tolerance: 1e-10
                              • *
                              • Parameters relative tolerance: 1e-10
                              • *
                              • Orthogonality tolerance: 1e-10
                              • *
                              • QR ranking threshold: {@link Precision#SAFE_MIN}
                              • *
                              */ public LevenbergMarquardtOptimizer() { this(100, 1e-10, 1e-10, 1e-10, Precision.SAFE_MIN); } /** * Constructor that allows the specification of a custom convergence * checker. * Note that all the usual convergence checks will be disabled. * The default values for the algorithm settings are: *
                                *
                              • Initial step bound factor: 100
                              • *
                              • Cost relative tolerance: 1e-10
                              • *
                              • Parameters relative tolerance: 1e-10
                              • *
                              • Orthogonality tolerance: 1e-10
                              • *
                              • QR ranking threshold: {@link Precision#SAFE_MIN}
                              • *
                              * * @param checker Convergence checker. */ public LevenbergMarquardtOptimizer(ConvergenceChecker checker) { this(100, checker, 1e-10, 1e-10, 1e-10, Precision.SAFE_MIN); } /** * Constructor that allows the specification of a custom convergence * checker, in addition to the standard ones. * * @param initialStepBoundFactor Positive input variable used in * determining the initial step bound. This bound is set to the * product of initialStepBoundFactor and the euclidean norm of * {@code diag * x} if non-zero, or else to {@code initialStepBoundFactor} * itself. In most cases factor should lie in the interval * {@code (0.1, 100.0)}. {@code 100} is a generally recommended value. * @param checker Convergence checker. * @param costRelativeTolerance Desired relative error in the sum of * squares. * @param parRelativeTolerance Desired relative error in the approximate * solution parameters. * @param orthoTolerance Desired max cosine on the orthogonality between * the function vector and the columns of the Jacobian. * @param threshold Desired threshold for QR ranking. If the squared norm * of a column vector is smaller or equal to this threshold during QR * decomposition, it is considered to be a zero vector and hence the rank * of the matrix is reduced. */ public LevenbergMarquardtOptimizer(double initialStepBoundFactor, ConvergenceChecker checker, double costRelativeTolerance, double parRelativeTolerance, double orthoTolerance, double threshold) { super(checker); this.initialStepBoundFactor = initialStepBoundFactor; this.costRelativeTolerance = costRelativeTolerance; this.parRelativeTolerance = parRelativeTolerance; this.orthoTolerance = orthoTolerance; this.qrRankingThreshold = threshold; } /** * Build an optimizer for least squares problems with default values * for some of the tuning parameters (see the {@link * #LevenbergMarquardtOptimizer(double,double,double,double,double) * other contructor}. * The default values for the algorithm settings are: *
                                *
                              • Initial step bound factor}: 100
                              • *
                              • QR ranking threshold}: {@link Precision#SAFE_MIN}
                              • *
                              * * @param costRelativeTolerance Desired relative error in the sum of * squares. * @param parRelativeTolerance Desired relative error in the approximate * solution parameters. * @param orthoTolerance Desired max cosine on the orthogonality between * the function vector and the columns of the Jacobian. */ public LevenbergMarquardtOptimizer(double costRelativeTolerance, double parRelativeTolerance, double orthoTolerance) { this(100, costRelativeTolerance, parRelativeTolerance, orthoTolerance, Precision.SAFE_MIN); } /** * The arguments control the behaviour of the default convergence checking * procedure. * Additional criteria can defined through the setting of a {@link * ConvergenceChecker}. * * @param initialStepBoundFactor Positive input variable used in * determining the initial step bound. This bound is set to the * product of initialStepBoundFactor and the euclidean norm of * {@code diag * x} if non-zero, or else to {@code initialStepBoundFactor} * itself. In most cases factor should lie in the interval * {@code (0.1, 100.0)}. {@code 100} is a generally recommended value. * @param costRelativeTolerance Desired relative error in the sum of * squares. * @param parRelativeTolerance Desired relative error in the approximate * solution parameters. * @param orthoTolerance Desired max cosine on the orthogonality between * the function vector and the columns of the Jacobian. * @param threshold Desired threshold for QR ranking. If the squared norm * of a column vector is smaller or equal to this threshold during QR * decomposition, it is considered to be a zero vector and hence the rank * of the matrix is reduced. */ public LevenbergMarquardtOptimizer(double initialStepBoundFactor, double costRelativeTolerance, double parRelativeTolerance, double orthoTolerance, double threshold) { super(null); // No custom convergence criterion. this.initialStepBoundFactor = initialStepBoundFactor; this.costRelativeTolerance = costRelativeTolerance; this.parRelativeTolerance = parRelativeTolerance; this.orthoTolerance = orthoTolerance; this.qrRankingThreshold = threshold; } /** {@inheritDoc} */ @Override protected PointVectorValuePair doOptimize() { checkParameters(); final int nR = getTarget().length; // Number of observed data. final double[] currentPoint = getStartPoint(); final int nC = currentPoint.length; // Number of parameters. // arrays shared with the other private methods solvedCols = FastMath.min(nR, nC); diagR = new double[nC]; jacNorm = new double[nC]; beta = new double[nC]; permutation = new int[nC]; lmDir = new double[nC]; // local point double delta = 0; double xNorm = 0; double[] diag = new double[nC]; double[] oldX = new double[nC]; double[] oldRes = new double[nR]; double[] oldObj = new double[nR]; double[] qtf = new double[nR]; double[] work1 = new double[nC]; double[] work2 = new double[nC]; double[] work3 = new double[nC]; final RealMatrix weightMatrixSqrt = getWeightSquareRoot(); // Evaluate the function at the starting point and calculate its norm. double[] currentObjective = computeObjectiveValue(currentPoint); double[] currentResiduals = computeResiduals(currentObjective); PointVectorValuePair current = new PointVectorValuePair(currentPoint, currentObjective); double currentCost = computeCost(currentResiduals); // Outer loop. lmPar = 0; boolean firstIteration = true; final ConvergenceChecker checker = getConvergenceChecker(); while (true) { incrementIterationCount(); final PointVectorValuePair previous = current; // QR decomposition of the jacobian matrix qrDecomposition(computeWeightedJacobian(currentPoint)); weightedResidual = weightMatrixSqrt.operate(currentResiduals); for (int i = 0; i < nR; i++) { qtf[i] = weightedResidual[i]; } // compute Qt.res qTy(qtf); // now we don't need Q anymore, // so let jacobian contain the R matrix with its diagonal elements for (int k = 0; k < solvedCols; ++k) { int pk = permutation[k]; weightedJacobian[k][pk] = diagR[pk]; } if (firstIteration) { // scale the point according to the norms of the columns // of the initial jacobian xNorm = 0; for (int k = 0; k < nC; ++k) { double dk = jacNorm[k]; if (dk == 0) { dk = 1.0; } double xk = dk * currentPoint[k]; xNorm += xk * xk; diag[k] = dk; } xNorm = FastMath.sqrt(xNorm); // initialize the step bound delta delta = (xNorm == 0) ? initialStepBoundFactor : (initialStepBoundFactor * xNorm); } // check orthogonality between function vector and jacobian columns double maxCosine = 0; if (currentCost != 0) { for (int j = 0; j < solvedCols; ++j) { int pj = permutation[j]; double s = jacNorm[pj]; if (s != 0) { double sum = 0; for (int i = 0; i <= j; ++i) { sum += weightedJacobian[i][pj] * qtf[i]; } maxCosine = FastMath.max(maxCosine, FastMath.abs(sum) / (s * currentCost)); } } } if (maxCosine <= orthoTolerance) { // Convergence has been reached. setCost(currentCost); return current; } // rescale if necessary for (int j = 0; j < nC; ++j) { diag[j] = FastMath.max(diag[j], jacNorm[j]); } // Inner loop. for (double ratio = 0; ratio < 1.0e-4;) { // save the state for (int j = 0; j < solvedCols; ++j) { int pj = permutation[j]; oldX[pj] = currentPoint[pj]; } final double previousCost = currentCost; double[] tmpVec = weightedResidual; weightedResidual = oldRes; oldRes = tmpVec; tmpVec = currentObjective; currentObjective = oldObj; oldObj = tmpVec; // determine the Levenberg-Marquardt parameter determineLMParameter(qtf, delta, diag, work1, work2, work3); // compute the new point and the norm of the evolution direction double lmNorm = 0; for (int j = 0; j < solvedCols; ++j) { int pj = permutation[j]; lmDir[pj] = -lmDir[pj]; currentPoint[pj] = oldX[pj] + lmDir[pj]; double s = diag[pj] * lmDir[pj]; lmNorm += s * s; } lmNorm = FastMath.sqrt(lmNorm); // on the first iteration, adjust the initial step bound. if (firstIteration) { delta = FastMath.min(delta, lmNorm); } // Evaluate the function at x + p and calculate its norm. currentObjective = computeObjectiveValue(currentPoint); currentResiduals = computeResiduals(currentObjective); current = new PointVectorValuePair(currentPoint, currentObjective); currentCost = computeCost(currentResiduals); // compute the scaled actual reduction double actRed = -1.0; if (0.1 * currentCost < previousCost) { double r = currentCost / previousCost; actRed = 1.0 - r * r; } // compute the scaled predicted reduction // and the scaled directional derivative for (int j = 0; j < solvedCols; ++j) { int pj = permutation[j]; double dirJ = lmDir[pj]; work1[j] = 0; for (int i = 0; i <= j; ++i) { work1[i] += weightedJacobian[i][pj] * dirJ; } } double coeff1 = 0; for (int j = 0; j < solvedCols; ++j) { coeff1 += work1[j] * work1[j]; } double pc2 = previousCost * previousCost; coeff1 = coeff1 / pc2; double coeff2 = lmPar * lmNorm * lmNorm / pc2; double preRed = coeff1 + 2 * coeff2; double dirDer = -(coeff1 + coeff2); // ratio of the actual to the predicted reduction ratio = (preRed == 0) ? 0 : (actRed / preRed); // update the step bound if (ratio <= 0.25) { double tmp = (actRed < 0) ? (0.5 * dirDer / (dirDer + 0.5 * actRed)) : 0.5; if ((0.1 * currentCost >= previousCost) || (tmp < 0.1)) { tmp = 0.1; } delta = tmp * FastMath.min(delta, 10.0 * lmNorm); lmPar /= tmp; } else if ((lmPar == 0) || (ratio >= 0.75)) { delta = 2 * lmNorm; lmPar *= 0.5; } // test for successful iteration. if (ratio >= 1.0e-4) { // successful iteration, update the norm firstIteration = false; xNorm = 0; for (int k = 0; k < nC; ++k) { double xK = diag[k] * currentPoint[k]; xNorm += xK * xK; } xNorm = FastMath.sqrt(xNorm); // tests for convergence. if (checker != null && checker.converged(getIterations(), previous, current)) { setCost(currentCost); return current; } } else { // failed iteration, reset the previous values currentCost = previousCost; for (int j = 0; j < solvedCols; ++j) { int pj = permutation[j]; currentPoint[pj] = oldX[pj]; } tmpVec = weightedResidual; weightedResidual = oldRes; oldRes = tmpVec; tmpVec = currentObjective; currentObjective = oldObj; oldObj = tmpVec; // Reset "current" to previous values. current = new PointVectorValuePair(currentPoint, currentObjective); } // Default convergence criteria. if ((FastMath.abs(actRed) <= costRelativeTolerance && preRed <= costRelativeTolerance && ratio <= 2.0) || delta <= parRelativeTolerance * xNorm) { setCost(currentCost); return current; } // tests for termination and stringent tolerances if (FastMath.abs(actRed) <= TWO_EPS && preRed <= TWO_EPS && ratio <= 2.0) { throw new ConvergenceException(LocalizedFormats.TOO_SMALL_COST_RELATIVE_TOLERANCE, costRelativeTolerance); } else if (delta <= TWO_EPS * xNorm) { throw new ConvergenceException(LocalizedFormats.TOO_SMALL_PARAMETERS_RELATIVE_TOLERANCE, parRelativeTolerance); } else if (maxCosine <= TWO_EPS) { throw new ConvergenceException(LocalizedFormats.TOO_SMALL_ORTHOGONALITY_TOLERANCE, orthoTolerance); } } } } /** * Determine the Levenberg-Marquardt parameter. *

                              This implementation is a translation in Java of the MINPACK * lmpar * routine.

                              *

                              This method sets the lmPar and lmDir attributes.

                              *

                              The authors of the original fortran function are:

                              *
                                *
                              • Argonne National Laboratory. MINPACK project. March 1980
                              • *
                              • Burton S. Garbow
                              • *
                              • Kenneth E. Hillstrom
                              • *
                              • Jorge J. More
                              • *
                              *

                              Luc Maisonobe did the Java translation.

                              * * @param qy array containing qTy * @param delta upper bound on the euclidean norm of diagR * lmDir * @param diag diagonal matrix * @param work1 work array * @param work2 work array * @param work3 work array */ private void determineLMParameter(double[] qy, double delta, double[] diag, double[] work1, double[] work2, double[] work3) { final int nC = weightedJacobian[0].length; // compute and store in x the gauss-newton direction, if the // jacobian is rank-deficient, obtain a least squares solution for (int j = 0; j < rank; ++j) { lmDir[permutation[j]] = qy[j]; } for (int j = rank; j < nC; ++j) { lmDir[permutation[j]] = 0; } for (int k = rank - 1; k >= 0; --k) { int pk = permutation[k]; double ypk = lmDir[pk] / diagR[pk]; for (int i = 0; i < k; ++i) { lmDir[permutation[i]] -= ypk * weightedJacobian[i][pk]; } lmDir[pk] = ypk; } // evaluate the function at the origin, and test // for acceptance of the Gauss-Newton direction double dxNorm = 0; for (int j = 0; j < solvedCols; ++j) { int pj = permutation[j]; double s = diag[pj] * lmDir[pj]; work1[pj] = s; dxNorm += s * s; } dxNorm = FastMath.sqrt(dxNorm); double fp = dxNorm - delta; if (fp <= 0.1 * delta) { lmPar = 0; return; } // if the jacobian is not rank deficient, the Newton step provides // a lower bound, parl, for the zero of the function, // otherwise set this bound to zero double sum2; double parl = 0; if (rank == solvedCols) { for (int j = 0; j < solvedCols; ++j) { int pj = permutation[j]; work1[pj] *= diag[pj] / dxNorm; } sum2 = 0; for (int j = 0; j < solvedCols; ++j) { int pj = permutation[j]; double sum = 0; for (int i = 0; i < j; ++i) { sum += weightedJacobian[i][pj] * work1[permutation[i]]; } double s = (work1[pj] - sum) / diagR[pj]; work1[pj] = s; sum2 += s * s; } parl = fp / (delta * sum2); } // calculate an upper bound, paru, for the zero of the function sum2 = 0; for (int j = 0; j < solvedCols; ++j) { int pj = permutation[j]; double sum = 0; for (int i = 0; i <= j; ++i) { sum += weightedJacobian[i][pj] * qy[i]; } sum /= diag[pj]; sum2 += sum * sum; } double gNorm = FastMath.sqrt(sum2); double paru = gNorm / delta; if (paru == 0) { paru = Precision.SAFE_MIN / FastMath.min(delta, 0.1); } // if the input par lies outside of the interval (parl,paru), // set par to the closer endpoint lmPar = FastMath.min(paru, FastMath.max(lmPar, parl)); if (lmPar == 0) { lmPar = gNorm / dxNorm; } for (int countdown = 10; countdown >= 0; --countdown) { // evaluate the function at the current value of lmPar if (lmPar == 0) { lmPar = FastMath.max(Precision.SAFE_MIN, 0.001 * paru); } double sPar = FastMath.sqrt(lmPar); for (int j = 0; j < solvedCols; ++j) { int pj = permutation[j]; work1[pj] = sPar * diag[pj]; } determineLMDirection(qy, work1, work2, work3); dxNorm = 0; for (int j = 0; j < solvedCols; ++j) { int pj = permutation[j]; double s = diag[pj] * lmDir[pj]; work3[pj] = s; dxNorm += s * s; } dxNorm = FastMath.sqrt(dxNorm); double previousFP = fp; fp = dxNorm - delta; // if the function is small enough, accept the current value // of lmPar, also test for the exceptional cases where parl is zero if ((FastMath.abs(fp) <= 0.1 * delta) || ((parl == 0) && (fp <= previousFP) && (previousFP < 0))) { return; } // compute the Newton correction for (int j = 0; j < solvedCols; ++j) { int pj = permutation[j]; work1[pj] = work3[pj] * diag[pj] / dxNorm; } for (int j = 0; j < solvedCols; ++j) { int pj = permutation[j]; work1[pj] /= work2[j]; double tmp = work1[pj]; for (int i = j + 1; i < solvedCols; ++i) { work1[permutation[i]] -= weightedJacobian[i][pj] * tmp; } } sum2 = 0; for (int j = 0; j < solvedCols; ++j) { double s = work1[permutation[j]]; sum2 += s * s; } double correction = fp / (delta * sum2); // depending on the sign of the function, update parl or paru. if (fp > 0) { parl = FastMath.max(parl, lmPar); } else if (fp < 0) { paru = FastMath.min(paru, lmPar); } // compute an improved estimate for lmPar lmPar = FastMath.max(parl, lmPar + correction); } } /** * Solve a*x = b and d*x = 0 in the least squares sense. *

                              This implementation is a translation in Java of the MINPACK * qrsolv * routine.

                              *

                              This method sets the lmDir and lmDiag attributes.

                              *

                              The authors of the original fortran function are:

                              *
                                *
                              • Argonne National Laboratory. MINPACK project. March 1980
                              • *
                              • Burton S. Garbow
                              • *
                              • Kenneth E. Hillstrom
                              • *
                              • Jorge J. More
                              • *
                              *

                              Luc Maisonobe did the Java translation.

                              * * @param qy array containing qTy * @param diag diagonal matrix * @param lmDiag diagonal elements associated with lmDir * @param work work array */ private void determineLMDirection(double[] qy, double[] diag, double[] lmDiag, double[] work) { // copy R and Qty to preserve input and initialize s // in particular, save the diagonal elements of R in lmDir for (int j = 0; j < solvedCols; ++j) { int pj = permutation[j]; for (int i = j + 1; i < solvedCols; ++i) { weightedJacobian[i][pj] = weightedJacobian[j][permutation[i]]; } lmDir[j] = diagR[pj]; work[j] = qy[j]; } // eliminate the diagonal matrix d using a Givens rotation for (int j = 0; j < solvedCols; ++j) { // prepare the row of d to be eliminated, locating the // diagonal element using p from the Q.R. factorization int pj = permutation[j]; double dpj = diag[pj]; if (dpj != 0) { Arrays.fill(lmDiag, j + 1, lmDiag.length, 0); } lmDiag[j] = dpj; // the transformations to eliminate the row of d // modify only a single element of Qty // beyond the first n, which is initially zero. double qtbpj = 0; for (int k = j; k < solvedCols; ++k) { int pk = permutation[k]; // determine a Givens rotation which eliminates the // appropriate element in the current row of d if (lmDiag[k] != 0) { final double sin; final double cos; double rkk = weightedJacobian[k][pk]; if (FastMath.abs(rkk) < FastMath.abs(lmDiag[k])) { final double cotan = rkk / lmDiag[k]; sin = 1.0 / FastMath.sqrt(1.0 + cotan * cotan); cos = sin * cotan; } else { final double tan = lmDiag[k] / rkk; cos = 1.0 / FastMath.sqrt(1.0 + tan * tan); sin = cos * tan; } // compute the modified diagonal element of R and // the modified element of (Qty,0) weightedJacobian[k][pk] = cos * rkk + sin * lmDiag[k]; final double temp = cos * work[k] + sin * qtbpj; qtbpj = -sin * work[k] + cos * qtbpj; work[k] = temp; // accumulate the tranformation in the row of s for (int i = k + 1; i < solvedCols; ++i) { double rik = weightedJacobian[i][pk]; final double temp2 = cos * rik + sin * lmDiag[i]; lmDiag[i] = -sin * rik + cos * lmDiag[i]; weightedJacobian[i][pk] = temp2; } } } // store the diagonal element of s and restore // the corresponding diagonal element of R lmDiag[j] = weightedJacobian[j][permutation[j]]; weightedJacobian[j][permutation[j]] = lmDir[j]; } // solve the triangular system for z, if the system is // singular, then obtain a least squares solution int nSing = solvedCols; for (int j = 0; j < solvedCols; ++j) { if ((lmDiag[j] == 0) && (nSing == solvedCols)) { nSing = j; } if (nSing < solvedCols) { work[j] = 0; } } if (nSing > 0) { for (int j = nSing - 1; j >= 0; --j) { int pj = permutation[j]; double sum = 0; for (int i = j + 1; i < nSing; ++i) { sum += weightedJacobian[i][pj] * work[i]; } work[j] = (work[j] - sum) / lmDiag[j]; } } // permute the components of z back to components of lmDir for (int j = 0; j < lmDir.length; ++j) { lmDir[permutation[j]] = work[j]; } } /** * Decompose a matrix A as A.P = Q.R using Householder transforms. *

                              As suggested in the P. Lascaux and R. Theodor book * Analyse numérique matricielle appliquée à * l'art de l'ingénieur (Masson, 1986), instead of representing * the Householder transforms with uk unit vectors such that: *

                                   * Hk = I - 2uk.ukt
                                   * 
                              * we use k non-unit vectors such that: *
                                   * Hk = I - betakvk.vkt
                                   * 
                              * where vk = ak - alphak ek. * The betak coefficients are provided upon exit as recomputing * them from the vk vectors would be costly.

                              *

                              This decomposition handles rank deficient cases since the tranformations * are performed in non-increasing columns norms order thanks to columns * pivoting. The diagonal elements of the R matrix are therefore also in * non-increasing absolute values order.

                              * * @param jacobian Weighted Jacobian matrix at the current point. * @exception ConvergenceException if the decomposition cannot be performed */ private void qrDecomposition(RealMatrix jacobian) throws ConvergenceException { // Code in this class assumes that the weighted Jacobian is -(W^(1/2) J), // hence the multiplication by -1. weightedJacobian = jacobian.scalarMultiply(-1).getData(); final int nR = weightedJacobian.length; final int nC = weightedJacobian[0].length; // initializations for (int k = 0; k < nC; ++k) { permutation[k] = k; double norm2 = 0; for (int i = 0; i < nR; ++i) { double akk = weightedJacobian[i][k]; norm2 += akk * akk; } jacNorm[k] = FastMath.sqrt(norm2); } // transform the matrix column after column for (int k = 0; k < nC; ++k) { // select the column with the greatest norm on active components int nextColumn = -1; double ak2 = Double.NEGATIVE_INFINITY; for (int i = k; i < nC; ++i) { double norm2 = 0; for (int j = k; j < nR; ++j) { double aki = weightedJacobian[j][permutation[i]]; norm2 += aki * aki; } if (Double.isInfinite(norm2) || Double.isNaN(norm2)) { throw new ConvergenceException(LocalizedFormats.UNABLE_TO_PERFORM_QR_DECOMPOSITION_ON_JACOBIAN, nR, nC); } if (norm2 > ak2) { nextColumn = i; ak2 = norm2; } } if (ak2 <= qrRankingThreshold) { rank = k; return; } int pk = permutation[nextColumn]; permutation[nextColumn] = permutation[k]; permutation[k] = pk; // choose alpha such that Hk.u = alpha ek double akk = weightedJacobian[k][pk]; double alpha = (akk > 0) ? -FastMath.sqrt(ak2) : FastMath.sqrt(ak2); double betak = 1.0 / (ak2 - akk * alpha); beta[pk] = betak; // transform the current column diagR[pk] = alpha; weightedJacobian[k][pk] -= alpha; // transform the remaining columns for (int dk = nC - 1 - k; dk > 0; --dk) { double gamma = 0; for (int j = k; j < nR; ++j) { gamma += weightedJacobian[j][pk] * weightedJacobian[j][permutation[k + dk]]; } gamma *= betak; for (int j = k; j < nR; ++j) { weightedJacobian[j][permutation[k + dk]] -= gamma * weightedJacobian[j][pk]; } } } rank = solvedCols; } /** * Compute the product Qt.y for some Q.R. decomposition. * * @param y vector to multiply (will be overwritten with the result) */ private void qTy(double[] y) { final int nR = weightedJacobian.length; final int nC = weightedJacobian[0].length; for (int k = 0; k < nC; ++k) { int pk = permutation[k]; double gamma = 0; for (int i = k; i < nR; ++i) { gamma += weightedJacobian[i][pk] * y[i]; } gamma *= beta[pk]; for (int i = k; i < nR; ++i) { y[i] -= gamma * weightedJacobian[i][pk]; } } } /** * @throws MathUnsupportedOperationException if bounds were passed to the * {@link #optimize(OptimizationData[]) optimize} method. */ private void checkParameters() { if (getLowerBound() != null || getUpperBound() != null) { throw new MathUnsupportedOperationException(LocalizedFormats.CONSTRAINT); } } } ././@LongLink100644 0 0 167 12126630646 10265 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/optim/nonlinear/vector/jacobian/GaussNewtonOptimizer.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/optim/nonlinear/vector/jacobian/GaussNe100644 1750 1750 16156 12126627713 32231 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.optim.nonlinear.vector.jacobian; import org.apache.commons.math3.exception.ConvergenceException; import org.apache.commons.math3.exception.NullArgumentException; import org.apache.commons.math3.exception.MathInternalError; import org.apache.commons.math3.exception.MathUnsupportedOperationException; import org.apache.commons.math3.exception.util.LocalizedFormats; import org.apache.commons.math3.linear.ArrayRealVector; import org.apache.commons.math3.linear.BlockRealMatrix; import org.apache.commons.math3.linear.DecompositionSolver; import org.apache.commons.math3.linear.LUDecomposition; import org.apache.commons.math3.linear.QRDecomposition; import org.apache.commons.math3.linear.RealMatrix; import org.apache.commons.math3.linear.SingularMatrixException; import org.apache.commons.math3.optim.ConvergenceChecker; import org.apache.commons.math3.optim.PointVectorValuePair; /** * Gauss-Newton least-squares solver. *
                              * Constraints are not supported: the call to * {@link #optimize(OptimizationData[]) optimize} will throw * {@link MathUnsupportedOperationException} if bounds are passed to it. * *

                              * This class solve a least-square problem by solving the normal equations * of the linearized problem at each iteration. Either LU decomposition or * QR decomposition can be used to solve the normal equations. LU decomposition * is faster but QR decomposition is more robust for difficult problems. *

                              * * @version $Id: GaussNewtonOptimizer.java 1458323 2013-03-19 14:51:30Z erans $ * @since 2.0 * */ public class GaussNewtonOptimizer extends AbstractLeastSquaresOptimizer { /** Indicator for using LU decomposition. */ private final boolean useLU; /** * Simple constructor with default settings. * The normal equations will be solved using LU decomposition. * * @param checker Convergence checker. */ public GaussNewtonOptimizer(ConvergenceChecker checker) { this(true, checker); } /** * @param useLU If {@code true}, the normal equations will be solved * using LU decomposition, otherwise they will be solved using QR * decomposition. * @param checker Convergence checker. */ public GaussNewtonOptimizer(final boolean useLU, ConvergenceChecker checker) { super(checker); this.useLU = useLU; } /** {@inheritDoc} */ @Override public PointVectorValuePair doOptimize() { checkParameters(); final ConvergenceChecker checker = getConvergenceChecker(); // Computation will be useless without a checker (see "for-loop"). if (checker == null) { throw new NullArgumentException(); } final double[] targetValues = getTarget(); final int nR = targetValues.length; // Number of observed data. final RealMatrix weightMatrix = getWeight(); // Diagonal of the weight matrix. final double[] residualsWeights = new double[nR]; for (int i = 0; i < nR; i++) { residualsWeights[i] = weightMatrix.getEntry(i, i); } final double[] currentPoint = getStartPoint(); final int nC = currentPoint.length; // iterate until convergence is reached PointVectorValuePair current = null; for (boolean converged = false; !converged;) { incrementIterationCount(); // evaluate the objective function and its jacobian PointVectorValuePair previous = current; // Value of the objective function at "currentPoint". final double[] currentObjective = computeObjectiveValue(currentPoint); final double[] currentResiduals = computeResiduals(currentObjective); final RealMatrix weightedJacobian = computeWeightedJacobian(currentPoint); current = new PointVectorValuePair(currentPoint, currentObjective); // build the linear problem final double[] b = new double[nC]; final double[][] a = new double[nC][nC]; for (int i = 0; i < nR; ++i) { final double[] grad = weightedJacobian.getRow(i); final double weight = residualsWeights[i]; final double residual = currentResiduals[i]; // compute the normal equation final double wr = weight * residual; for (int j = 0; j < nC; ++j) { b[j] += wr * grad[j]; } // build the contribution matrix for measurement i for (int k = 0; k < nC; ++k) { double[] ak = a[k]; double wgk = weight * grad[k]; for (int l = 0; l < nC; ++l) { ak[l] += wgk * grad[l]; } } } try { // solve the linearized least squares problem RealMatrix mA = new BlockRealMatrix(a); DecompositionSolver solver = useLU ? new LUDecomposition(mA).getSolver() : new QRDecomposition(mA).getSolver(); final double[] dX = solver.solve(new ArrayRealVector(b, false)).toArray(); // update the estimated parameters for (int i = 0; i < nC; ++i) { currentPoint[i] += dX[i]; } } catch (SingularMatrixException e) { throw new ConvergenceException(LocalizedFormats.UNABLE_TO_SOLVE_SINGULAR_PROBLEM); } // Check convergence. if (previous != null) { converged = checker.converged(getIterations(), previous, current); if (converged) { setCost(computeCost(currentResiduals)); return current; } } } // Must never happen. throw new MathInternalError(); } /** * @throws MathUnsupportedOperationException if bounds were passed to the * {@link #optimize(OptimizationData[]) optimize} method. */ private void checkParameters() { if (getLowerBound() != null || getUpperBound() != null) { throw new MathUnsupportedOperationException(LocalizedFormats.CONSTRAINT); } } } ././@LongLink100644 0 0 146 12126630646 10262 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/optim/nonlinear/scalar/package-info.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/optim/nonlinear/scalar/package-info.jav100644 1750 1750 1625 12126627713 32137 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ /** * Algorithms for optimizing a scalar function. */ package org.apache.commons.math3.optim.nonlinear.scalar; ././@LongLink100644 0 0 156 12126630646 10263 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/optim/nonlinear/scalar/noderiv/package-info.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/optim/nonlinear/scalar/noderiv/package-100644 1750 1750 1677 12126627713 32161 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ /** * This package provides optimization algorithms that do not require derivatives. */ package org.apache.commons.math3.optim.nonlinear.scalar.noderiv; ././@LongLink100644 0 0 171 12126630646 10260 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/optim/nonlinear/scalar/noderiv/MultiDirectionalSimplex.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/optim/nonlinear/scalar/noderiv/MultiDir100644 1750 1750 20475 12126627713 32257 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.optim.nonlinear.scalar.noderiv; import java.util.Comparator; import org.apache.commons.math3.analysis.MultivariateFunction; import org.apache.commons.math3.optim.PointValuePair; /** * This class implements the multi-directional direct search method. * * @version $Id: MultiDirectionalSimplex.java 1435539 2013-01-19 13:27:24Z tn $ * @since 3.0 */ public class MultiDirectionalSimplex extends AbstractSimplex { /** Default value for {@link #khi}: {@value}. */ private static final double DEFAULT_KHI = 2; /** Default value for {@link #gamma}: {@value}. */ private static final double DEFAULT_GAMMA = 0.5; /** Expansion coefficient. */ private final double khi; /** Contraction coefficient. */ private final double gamma; /** * Build a multi-directional simplex with default coefficients. * The default values are 2.0 for khi and 0.5 for gamma. * * @param n Dimension of the simplex. */ public MultiDirectionalSimplex(final int n) { this(n, 1d); } /** * Build a multi-directional simplex with default coefficients. * The default values are 2.0 for khi and 0.5 for gamma. * * @param n Dimension of the simplex. * @param sideLength Length of the sides of the default (hypercube) * simplex. See {@link AbstractSimplex#AbstractSimplex(int,double)}. */ public MultiDirectionalSimplex(final int n, double sideLength) { this(n, sideLength, DEFAULT_KHI, DEFAULT_GAMMA); } /** * Build a multi-directional simplex with specified coefficients. * * @param n Dimension of the simplex. See * {@link AbstractSimplex#AbstractSimplex(int,double)}. * @param khi Expansion coefficient. * @param gamma Contraction coefficient. */ public MultiDirectionalSimplex(final int n, final double khi, final double gamma) { this(n, 1d, khi, gamma); } /** * Build a multi-directional simplex with specified coefficients. * * @param n Dimension of the simplex. See * {@link AbstractSimplex#AbstractSimplex(int,double)}. * @param sideLength Length of the sides of the default (hypercube) * simplex. See {@link AbstractSimplex#AbstractSimplex(int,double)}. * @param khi Expansion coefficient. * @param gamma Contraction coefficient. */ public MultiDirectionalSimplex(final int n, double sideLength, final double khi, final double gamma) { super(n, sideLength); this.khi = khi; this.gamma = gamma; } /** * Build a multi-directional simplex with default coefficients. * The default values are 2.0 for khi and 0.5 for gamma. * * @param steps Steps along the canonical axes representing box edges. * They may be negative but not zero. See */ public MultiDirectionalSimplex(final double[] steps) { this(steps, DEFAULT_KHI, DEFAULT_GAMMA); } /** * Build a multi-directional simplex with specified coefficients. * * @param steps Steps along the canonical axes representing box edges. * They may be negative but not zero. See * {@link AbstractSimplex#AbstractSimplex(double[])}. * @param khi Expansion coefficient. * @param gamma Contraction coefficient. */ public MultiDirectionalSimplex(final double[] steps, final double khi, final double gamma) { super(steps); this.khi = khi; this.gamma = gamma; } /** * Build a multi-directional simplex with default coefficients. * The default values are 2.0 for khi and 0.5 for gamma. * * @param referenceSimplex Reference simplex. See * {@link AbstractSimplex#AbstractSimplex(double[][])}. */ public MultiDirectionalSimplex(final double[][] referenceSimplex) { this(referenceSimplex, DEFAULT_KHI, DEFAULT_GAMMA); } /** * Build a multi-directional simplex with specified coefficients. * * @param referenceSimplex Reference simplex. See * {@link AbstractSimplex#AbstractSimplex(double[][])}. * @param khi Expansion coefficient. * @param gamma Contraction coefficient. * @throws org.apache.commons.math3.exception.NotStrictlyPositiveException * if the reference simplex does not contain at least one point. * @throws org.apache.commons.math3.exception.DimensionMismatchException * if there is a dimension mismatch in the reference simplex. */ public MultiDirectionalSimplex(final double[][] referenceSimplex, final double khi, final double gamma) { super(referenceSimplex); this.khi = khi; this.gamma = gamma; } /** {@inheritDoc} */ @Override public void iterate(final MultivariateFunction evaluationFunction, final Comparator comparator) { // Save the original simplex. final PointValuePair[] original = getPoints(); final PointValuePair best = original[0]; // Perform a reflection step. final PointValuePair reflected = evaluateNewSimplex(evaluationFunction, original, 1, comparator); if (comparator.compare(reflected, best) < 0) { // Compute the expanded simplex. final PointValuePair[] reflectedSimplex = getPoints(); final PointValuePair expanded = evaluateNewSimplex(evaluationFunction, original, khi, comparator); if (comparator.compare(reflected, expanded) <= 0) { // Keep the reflected simplex. setPoints(reflectedSimplex); } // Keep the expanded simplex. return; } // Compute the contracted simplex. evaluateNewSimplex(evaluationFunction, original, gamma, comparator); } /** * Compute and evaluate a new simplex. * * @param evaluationFunction Evaluation function. * @param original Original simplex (to be preserved). * @param coeff Linear coefficient. * @param comparator Comparator to use to sort simplex vertices from best * to poorest. * @return the best point in the transformed simplex. * @throws org.apache.commons.math3.exception.TooManyEvaluationsException * if the maximal number of evaluations is exceeded. */ private PointValuePair evaluateNewSimplex(final MultivariateFunction evaluationFunction, final PointValuePair[] original, final double coeff, final Comparator comparator) { final double[] xSmallest = original[0].getPointRef(); // Perform a linear transformation on all the simplex points, // except the first one. setPoint(0, original[0]); final int dim = getDimension(); for (int i = 1; i < getSize(); i++) { final double[] xOriginal = original[i].getPointRef(); final double[] xTransformed = new double[dim]; for (int j = 0; j < dim; j++) { xTransformed[j] = xSmallest[j] + coeff * (xSmallest[j] - xOriginal[j]); } setPoint(i, new PointValuePair(xTransformed, Double.NaN, false)); } // Evaluate the simplex. evaluate(evaluationFunction, comparator); return getPoint(0); } } ././@LongLink100644 0 0 161 12126630646 10257 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/optim/nonlinear/scalar/noderiv/BOBYQAOptimizer.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/optim/nonlinear/scalar/noderiv/BOBYQAOp100644 1750 1750 335204 12126627713 32021 0ustarlucluc 0 0 // CHECKSTYLE: stop all /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.optim.nonlinear.scalar.noderiv; import org.apache.commons.math3.exception.MathIllegalStateException; import org.apache.commons.math3.exception.NumberIsTooSmallException; import org.apache.commons.math3.exception.OutOfRangeException; import org.apache.commons.math3.exception.util.LocalizedFormats; import org.apache.commons.math3.linear.Array2DRowRealMatrix; import org.apache.commons.math3.linear.ArrayRealVector; import org.apache.commons.math3.linear.RealVector; import org.apache.commons.math3.optim.nonlinear.scalar.GoalType; import org.apache.commons.math3.optim.PointValuePair; import org.apache.commons.math3.optim.nonlinear.scalar.MultivariateOptimizer; /** * Powell's BOBYQA algorithm. This implementation is translated and * adapted from the Fortran version available * here. * See * this paper for an introduction. *
                              * BOBYQA is particularly well suited for high dimensional problems * where derivatives are not available. In most cases it outperforms the * {@link PowellOptimizer} significantly. Stochastic algorithms like * {@link CMAESOptimizer} succeed more often than BOBYQA, but are more * expensive. BOBYQA could also be considered as a replacement of any * derivative-based optimizer when the derivatives are approximated by * finite differences. * * @version $Id: BOBYQAOptimizer.java 1462507 2013-03-29 15:50:22Z luc $ * @since 3.0 */ public class BOBYQAOptimizer extends MultivariateOptimizer { /** Minimum dimension of the problem: {@value} */ public static final int MINIMUM_PROBLEM_DIMENSION = 2; /** Default value for {@link #initialTrustRegionRadius}: {@value} . */ public static final double DEFAULT_INITIAL_RADIUS = 10.0; /** Default value for {@link #stoppingTrustRegionRadius}: {@value} . */ public static final double DEFAULT_STOPPING_RADIUS = 1E-8; private static final double ZERO = 0d; private static final double ONE = 1d; private static final double TWO = 2d; private static final double TEN = 10d; private static final double SIXTEEN = 16d; private static final double TWO_HUNDRED_FIFTY = 250d; private static final double MINUS_ONE = -ONE; private static final double HALF = ONE / 2; private static final double ONE_OVER_FOUR = ONE / 4; private static final double ONE_OVER_EIGHT = ONE / 8; private static final double ONE_OVER_TEN = ONE / 10; private static final double ONE_OVER_A_THOUSAND = ONE / 1000; /** * numberOfInterpolationPoints XXX */ private final int numberOfInterpolationPoints; /** * initialTrustRegionRadius XXX */ private double initialTrustRegionRadius; /** * stoppingTrustRegionRadius XXX */ private final double stoppingTrustRegionRadius; /** Goal type (minimize or maximize). */ private boolean isMinimize; /** * Current best values for the variables to be optimized. * The vector will be changed in-place to contain the values of the least * calculated objective function values. */ private ArrayRealVector currentBest; /** Differences between the upper and lower bounds. */ private double[] boundDifference; /** * Index of the interpolation point at the trust region center. */ private int trustRegionCenterInterpolationPointIndex; /** * Last n columns of matrix H (where n is the dimension * of the problem). * XXX "bmat" in the original code. */ private Array2DRowRealMatrix bMatrix; /** * Factorization of the leading npt square submatrix of H, this * factorization being Z ZT, which provides both the correct * rank and positive semi-definiteness. * XXX "zmat" in the original code. */ private Array2DRowRealMatrix zMatrix; /** * Coordinates of the interpolation points relative to {@link #originShift}. * XXX "xpt" in the original code. */ private Array2DRowRealMatrix interpolationPoints; /** * Shift of origin that should reduce the contributions from rounding * errors to values of the model and Lagrange functions. * XXX "xbase" in the original code. */ private ArrayRealVector originShift; /** * Values of the objective function at the interpolation points. * XXX "fval" in the original code. */ private ArrayRealVector fAtInterpolationPoints; /** * Displacement from {@link #originShift} of the trust region center. * XXX "xopt" in the original code. */ private ArrayRealVector trustRegionCenterOffset; /** * Gradient of the quadratic model at {@link #originShift} + * {@link #trustRegionCenterOffset}. * XXX "gopt" in the original code. */ private ArrayRealVector gradientAtTrustRegionCenter; /** * Differences {@link #getLowerBound()} - {@link #originShift}. * All the components of every {@link #trustRegionCenterOffset} are going * to satisfy the bounds
                              * {@link #getLowerBound() lowerBound}i ≤ * {@link #trustRegionCenterOffset}i,
                              * with appropriate equalities when {@link #trustRegionCenterOffset} is * on a constraint boundary. * XXX "sl" in the original code. */ private ArrayRealVector lowerDifference; /** * Differences {@link #getUpperBound()} - {@link #originShift} * All the components of every {@link #trustRegionCenterOffset} are going * to satisfy the bounds
                              * {@link #trustRegionCenterOffset}i ≤ * {@link #getUpperBound() upperBound}i,
                              * with appropriate equalities when {@link #trustRegionCenterOffset} is * on a constraint boundary. * XXX "su" in the original code. */ private ArrayRealVector upperDifference; /** * Parameters of the implicit second derivatives of the quadratic model. * XXX "pq" in the original code. */ private ArrayRealVector modelSecondDerivativesParameters; /** * Point chosen by function {@link #trsbox(double,ArrayRealVector, * ArrayRealVector, ArrayRealVector,ArrayRealVector,ArrayRealVector) trsbox} * or {@link #altmov(int,double) altmov}. * Usually {@link #originShift} + {@link #newPoint} is the vector of * variables for the next evaluation of the objective function. * It also satisfies the constraints indicated in {@link #lowerDifference} * and {@link #upperDifference}. * XXX "xnew" in the original code. */ private ArrayRealVector newPoint; /** * Alternative to {@link #newPoint}, chosen by * {@link #altmov(int,double) altmov}. * It may replace {@link #newPoint} in order to increase the denominator * in the {@link #update(double, double, int) updating procedure}. * XXX "xalt" in the original code. */ private ArrayRealVector alternativeNewPoint; /** * Trial step from {@link #trustRegionCenterOffset} which is usually * {@link #newPoint} - {@link #trustRegionCenterOffset}. * XXX "d__" in the original code. */ private ArrayRealVector trialStepPoint; /** * Values of the Lagrange functions at a new point. * XXX "vlag" in the original code. */ private ArrayRealVector lagrangeValuesAtNewPoint; /** * Explicit second derivatives of the quadratic model. * XXX "hq" in the original code. */ private ArrayRealVector modelSecondDerivativesValues; /** * @param numberOfInterpolationPoints Number of interpolation conditions. * For a problem of dimension {@code n}, its value must be in the interval * {@code [n+2, (n+1)(n+2)/2]}. * Choices that exceed {@code 2n+1} are not recommended. */ public BOBYQAOptimizer(int numberOfInterpolationPoints) { this(numberOfInterpolationPoints, DEFAULT_INITIAL_RADIUS, DEFAULT_STOPPING_RADIUS); } /** * @param numberOfInterpolationPoints Number of interpolation conditions. * For a problem of dimension {@code n}, its value must be in the interval * {@code [n+2, (n+1)(n+2)/2]}. * Choices that exceed {@code 2n+1} are not recommended. * @param initialTrustRegionRadius Initial trust region radius. * @param stoppingTrustRegionRadius Stopping trust region radius. */ public BOBYQAOptimizer(int numberOfInterpolationPoints, double initialTrustRegionRadius, double stoppingTrustRegionRadius) { super(null); // No custom convergence criterion. this.numberOfInterpolationPoints = numberOfInterpolationPoints; this.initialTrustRegionRadius = initialTrustRegionRadius; this.stoppingTrustRegionRadius = stoppingTrustRegionRadius; } /** {@inheritDoc} */ @Override protected PointValuePair doOptimize() { final double[] lowerBound = getLowerBound(); final double[] upperBound = getUpperBound(); // Validity checks. setup(lowerBound, upperBound); isMinimize = (getGoalType() == GoalType.MINIMIZE); currentBest = new ArrayRealVector(getStartPoint()); final double value = bobyqa(lowerBound, upperBound); return new PointValuePair(currentBest.getDataRef(), isMinimize ? value : -value); } /** * This subroutine seeks the least value of a function of many variables, * by applying a trust region method that forms quadratic models by * interpolation. There is usually some freedom in the interpolation * conditions, which is taken up by minimizing the Frobenius norm of * the change to the second derivative of the model, beginning with the * zero matrix. The values of the variables are constrained by upper and * lower bounds. The arguments of the subroutine are as follows. * * N must be set to the number of variables and must be at least two. * NPT is the number of interpolation conditions. Its value must be in * the interval [N+2,(N+1)(N+2)/2]. Choices that exceed 2*N+1 are not * recommended. * Initial values of the variables must be set in X(1),X(2),...,X(N). They * will be changed to the values that give the least calculated F. * For I=1,2,...,N, XL(I) and XU(I) must provide the lower and upper * bounds, respectively, on X(I). The construction of quadratic models * requires XL(I) to be strictly less than XU(I) for each I. Further, * the contribution to a model from changes to the I-th variable is * damaged severely by rounding errors if XU(I)-XL(I) is too small. * RHOBEG and RHOEND must be set to the initial and final values of a trust * region radius, so both must be positive with RHOEND no greater than * RHOBEG. Typically, RHOBEG should be about one tenth of the greatest * expected change to a variable, while RHOEND should indicate the * accuracy that is required in the final values of the variables. An * error return occurs if any of the differences XU(I)-XL(I), I=1,...,N, * is less than 2*RHOBEG. * MAXFUN must be set to an upper bound on the number of calls of CALFUN. * The array W will be used for working space. Its length must be at least * (NPT+5)*(NPT+N)+3*N*(N+5)/2. * * @param lowerBound Lower bounds. * @param upperBound Upper bounds. * @return the value of the objective at the optimum. */ private double bobyqa(double[] lowerBound, double[] upperBound) { printMethod(); // XXX final int n = currentBest.getDimension(); // Return if there is insufficient space between the bounds. Modify the // initial X if necessary in order to avoid conflicts between the bounds // and the construction of the first quadratic model. The lower and upper // bounds on moves from the updated X are set now, in the ISL and ISU // partitions of W, in order to provide useful and exact information about // components of X that become within distance RHOBEG from their bounds. for (int j = 0; j < n; j++) { final double boundDiff = boundDifference[j]; lowerDifference.setEntry(j, lowerBound[j] - currentBest.getEntry(j)); upperDifference.setEntry(j, upperBound[j] - currentBest.getEntry(j)); if (lowerDifference.getEntry(j) >= -initialTrustRegionRadius) { if (lowerDifference.getEntry(j) >= ZERO) { currentBest.setEntry(j, lowerBound[j]); lowerDifference.setEntry(j, ZERO); upperDifference.setEntry(j, boundDiff); } else { currentBest.setEntry(j, lowerBound[j] + initialTrustRegionRadius); lowerDifference.setEntry(j, -initialTrustRegionRadius); // Computing MAX final double deltaOne = upperBound[j] - currentBest.getEntry(j); upperDifference.setEntry(j, Math.max(deltaOne, initialTrustRegionRadius)); } } else if (upperDifference.getEntry(j) <= initialTrustRegionRadius) { if (upperDifference.getEntry(j) <= ZERO) { currentBest.setEntry(j, upperBound[j]); lowerDifference.setEntry(j, -boundDiff); upperDifference.setEntry(j, ZERO); } else { currentBest.setEntry(j, upperBound[j] - initialTrustRegionRadius); // Computing MIN final double deltaOne = lowerBound[j] - currentBest.getEntry(j); final double deltaTwo = -initialTrustRegionRadius; lowerDifference.setEntry(j, Math.min(deltaOne, deltaTwo)); upperDifference.setEntry(j, initialTrustRegionRadius); } } } // Make the call of BOBYQB. return bobyqb(lowerBound, upperBound); } // bobyqa // ---------------------------------------------------------------------------------------- /** * The arguments N, NPT, X, XL, XU, RHOBEG, RHOEND, IPRINT and MAXFUN * are identical to the corresponding arguments in SUBROUTINE BOBYQA. * XBASE holds a shift of origin that should reduce the contributions * from rounding errors to values of the model and Lagrange functions. * XPT is a two-dimensional array that holds the coordinates of the * interpolation points relative to XBASE. * FVAL holds the values of F at the interpolation points. * XOPT is set to the displacement from XBASE of the trust region centre. * GOPT holds the gradient of the quadratic model at XBASE+XOPT. * HQ holds the explicit second derivatives of the quadratic model. * PQ contains the parameters of the implicit second derivatives of the * quadratic model. * BMAT holds the last N columns of H. * ZMAT holds the factorization of the leading NPT by NPT submatrix of H, * this factorization being ZMAT times ZMAT^T, which provides both the * correct rank and positive semi-definiteness. * NDIM is the first dimension of BMAT and has the value NPT+N. * SL and SU hold the differences XL-XBASE and XU-XBASE, respectively. * All the components of every XOPT are going to satisfy the bounds * SL(I) .LEQ. XOPT(I) .LEQ. SU(I), with appropriate equalities when * XOPT is on a constraint boundary. * XNEW is chosen by SUBROUTINE TRSBOX or ALTMOV. Usually XBASE+XNEW is the * vector of variables for the next call of CALFUN. XNEW also satisfies * the SL and SU constraints in the way that has just been mentioned. * XALT is an alternative to XNEW, chosen by ALTMOV, that may replace XNEW * in order to increase the denominator in the updating of UPDATE. * D is reserved for a trial step from XOPT, which is usually XNEW-XOPT. * VLAG contains the values of the Lagrange functions at a new point X. * They are part of a product that requires VLAG to be of length NDIM. * W is a one-dimensional array that is used for working space. Its length * must be at least 3*NDIM = 3*(NPT+N). * * @param lowerBound Lower bounds. * @param upperBound Upper bounds. * @return the value of the objective at the optimum. */ private double bobyqb(double[] lowerBound, double[] upperBound) { printMethod(); // XXX final int n = currentBest.getDimension(); final int npt = numberOfInterpolationPoints; final int np = n + 1; final int nptm = npt - np; final int nh = n * np / 2; final ArrayRealVector work1 = new ArrayRealVector(n); final ArrayRealVector work2 = new ArrayRealVector(npt); final ArrayRealVector work3 = new ArrayRealVector(npt); double cauchy = Double.NaN; double alpha = Double.NaN; double dsq = Double.NaN; double crvmin = Double.NaN; // Set some constants. // Parameter adjustments // Function Body // The call of PRELIM sets the elements of XBASE, XPT, FVAL, GOPT, HQ, PQ, // BMAT and ZMAT for the first iteration, with the corresponding values of // of NF and KOPT, which are the number of calls of CALFUN so far and the // index of the interpolation point at the trust region centre. Then the // initial XOPT is set too. The branch to label 720 occurs if MAXFUN is // less than NPT. GOPT will be updated if KOPT is different from KBASE. trustRegionCenterInterpolationPointIndex = 0; prelim(lowerBound, upperBound); double xoptsq = ZERO; for (int i = 0; i < n; i++) { trustRegionCenterOffset.setEntry(i, interpolationPoints.getEntry(trustRegionCenterInterpolationPointIndex, i)); // Computing 2nd power final double deltaOne = trustRegionCenterOffset.getEntry(i); xoptsq += deltaOne * deltaOne; } double fsave = fAtInterpolationPoints.getEntry(0); final int kbase = 0; // Complete the settings that are required for the iterative procedure. int ntrits = 0; int itest = 0; int knew = 0; int nfsav = getEvaluations(); double rho = initialTrustRegionRadius; double delta = rho; double diffa = ZERO; double diffb = ZERO; double diffc = ZERO; double f = ZERO; double beta = ZERO; double adelt = ZERO; double denom = ZERO; double ratio = ZERO; double dnorm = ZERO; double scaden = ZERO; double biglsq = ZERO; double distsq = ZERO; // Update GOPT if necessary before the first iteration and after each // call of RESCUE that makes a call of CALFUN. int state = 20; for(;;) switch (state) { case 20: { printState(20); // XXX if (trustRegionCenterInterpolationPointIndex != kbase) { int ih = 0; for (int j = 0; j < n; j++) { for (int i = 0; i <= j; i++) { if (i < j) { gradientAtTrustRegionCenter.setEntry(j, gradientAtTrustRegionCenter.getEntry(j) + modelSecondDerivativesValues.getEntry(ih) * trustRegionCenterOffset.getEntry(i)); } gradientAtTrustRegionCenter.setEntry(i, gradientAtTrustRegionCenter.getEntry(i) + modelSecondDerivativesValues.getEntry(ih) * trustRegionCenterOffset.getEntry(j)); ih++; } } if (getEvaluations() > npt) { for (int k = 0; k < npt; k++) { double temp = ZERO; for (int j = 0; j < n; j++) { temp += interpolationPoints.getEntry(k, j) * trustRegionCenterOffset.getEntry(j); } temp *= modelSecondDerivativesParameters.getEntry(k); for (int i = 0; i < n; i++) { gradientAtTrustRegionCenter.setEntry(i, gradientAtTrustRegionCenter.getEntry(i) + temp * interpolationPoints.getEntry(k, i)); } } // throw new PathIsExploredException(); // XXX } } // Generate the next point in the trust region that provides a small value // of the quadratic model subject to the constraints on the variables. // The int NTRITS is set to the number "trust region" iterations that // have occurred since the last "alternative" iteration. If the length // of XNEW-XOPT is less than HALF*RHO, however, then there is a branch to // label 650 or 680 with NTRITS=-1, instead of calculating F at XNEW. } case 60: { printState(60); // XXX final ArrayRealVector gnew = new ArrayRealVector(n); final ArrayRealVector xbdi = new ArrayRealVector(n); final ArrayRealVector s = new ArrayRealVector(n); final ArrayRealVector hs = new ArrayRealVector(n); final ArrayRealVector hred = new ArrayRealVector(n); final double[] dsqCrvmin = trsbox(delta, gnew, xbdi, s, hs, hred); dsq = dsqCrvmin[0]; crvmin = dsqCrvmin[1]; // Computing MIN double deltaOne = delta; double deltaTwo = Math.sqrt(dsq); dnorm = Math.min(deltaOne, deltaTwo); if (dnorm < HALF * rho) { ntrits = -1; // Computing 2nd power deltaOne = TEN * rho; distsq = deltaOne * deltaOne; if (getEvaluations() <= nfsav + 2) { state = 650; break; } // The following choice between labels 650 and 680 depends on whether or // not our work with the current RHO seems to be complete. Either RHO is // decreased or termination occurs if the errors in the quadratic model at // the last three interpolation points compare favourably with predictions // of likely improvements to the model within distance HALF*RHO of XOPT. // Computing MAX deltaOne = Math.max(diffa, diffb); final double errbig = Math.max(deltaOne, diffc); final double frhosq = rho * ONE_OVER_EIGHT * rho; if (crvmin > ZERO && errbig > frhosq * crvmin) { state = 650; break; } final double bdtol = errbig / rho; for (int j = 0; j < n; j++) { double bdtest = bdtol; if (newPoint.getEntry(j) == lowerDifference.getEntry(j)) { bdtest = work1.getEntry(j); } if (newPoint.getEntry(j) == upperDifference.getEntry(j)) { bdtest = -work1.getEntry(j); } if (bdtest < bdtol) { double curv = modelSecondDerivativesValues.getEntry((j + j * j) / 2); for (int k = 0; k < npt; k++) { // Computing 2nd power final double d1 = interpolationPoints.getEntry(k, j); curv += modelSecondDerivativesParameters.getEntry(k) * (d1 * d1); } bdtest += HALF * curv * rho; if (bdtest < bdtol) { state = 650; break; } // throw new PathIsExploredException(); // XXX } } state = 680; break; } ++ntrits; // Severe cancellation is likely to occur if XOPT is too far from XBASE. // If the following test holds, then XBASE is shifted so that XOPT becomes // zero. The appropriate changes are made to BMAT and to the second // derivatives of the current model, beginning with the changes to BMAT // that do not depend on ZMAT. VLAG is used temporarily for working space. } case 90: { printState(90); // XXX if (dsq <= xoptsq * ONE_OVER_A_THOUSAND) { final double fracsq = xoptsq * ONE_OVER_FOUR; double sumpq = ZERO; // final RealVector sumVector // = new ArrayRealVector(npt, -HALF * xoptsq).add(interpolationPoints.operate(trustRegionCenter)); for (int k = 0; k < npt; k++) { sumpq += modelSecondDerivativesParameters.getEntry(k); double sum = -HALF * xoptsq; for (int i = 0; i < n; i++) { sum += interpolationPoints.getEntry(k, i) * trustRegionCenterOffset.getEntry(i); } // sum = sumVector.getEntry(k); // XXX "testAckley" and "testDiffPow" fail. work2.setEntry(k, sum); final double temp = fracsq - HALF * sum; for (int i = 0; i < n; i++) { work1.setEntry(i, bMatrix.getEntry(k, i)); lagrangeValuesAtNewPoint.setEntry(i, sum * interpolationPoints.getEntry(k, i) + temp * trustRegionCenterOffset.getEntry(i)); final int ip = npt + i; for (int j = 0; j <= i; j++) { bMatrix.setEntry(ip, j, bMatrix.getEntry(ip, j) + work1.getEntry(i) * lagrangeValuesAtNewPoint.getEntry(j) + lagrangeValuesAtNewPoint.getEntry(i) * work1.getEntry(j)); } } } // Then the revisions of BMAT that depend on ZMAT are calculated. for (int m = 0; m < nptm; m++) { double sumz = ZERO; double sumw = ZERO; for (int k = 0; k < npt; k++) { sumz += zMatrix.getEntry(k, m); lagrangeValuesAtNewPoint.setEntry(k, work2.getEntry(k) * zMatrix.getEntry(k, m)); sumw += lagrangeValuesAtNewPoint.getEntry(k); } for (int j = 0; j < n; j++) { double sum = (fracsq * sumz - HALF * sumw) * trustRegionCenterOffset.getEntry(j); for (int k = 0; k < npt; k++) { sum += lagrangeValuesAtNewPoint.getEntry(k) * interpolationPoints.getEntry(k, j); } work1.setEntry(j, sum); for (int k = 0; k < npt; k++) { bMatrix.setEntry(k, j, bMatrix.getEntry(k, j) + sum * zMatrix.getEntry(k, m)); } } for (int i = 0; i < n; i++) { final int ip = i + npt; final double temp = work1.getEntry(i); for (int j = 0; j <= i; j++) { bMatrix.setEntry(ip, j, bMatrix.getEntry(ip, j) + temp * work1.getEntry(j)); } } } // The following instructions complete the shift, including the changes // to the second derivative parameters of the quadratic model. int ih = 0; for (int j = 0; j < n; j++) { work1.setEntry(j, -HALF * sumpq * trustRegionCenterOffset.getEntry(j)); for (int k = 0; k < npt; k++) { work1.setEntry(j, work1.getEntry(j) + modelSecondDerivativesParameters.getEntry(k) * interpolationPoints.getEntry(k, j)); interpolationPoints.setEntry(k, j, interpolationPoints.getEntry(k, j) - trustRegionCenterOffset.getEntry(j)); } for (int i = 0; i <= j; i++) { modelSecondDerivativesValues.setEntry(ih, modelSecondDerivativesValues.getEntry(ih) + work1.getEntry(i) * trustRegionCenterOffset.getEntry(j) + trustRegionCenterOffset.getEntry(i) * work1.getEntry(j)); bMatrix.setEntry(npt + i, j, bMatrix.getEntry(npt + j, i)); ih++; } } for (int i = 0; i < n; i++) { originShift.setEntry(i, originShift.getEntry(i) + trustRegionCenterOffset.getEntry(i)); newPoint.setEntry(i, newPoint.getEntry(i) - trustRegionCenterOffset.getEntry(i)); lowerDifference.setEntry(i, lowerDifference.getEntry(i) - trustRegionCenterOffset.getEntry(i)); upperDifference.setEntry(i, upperDifference.getEntry(i) - trustRegionCenterOffset.getEntry(i)); trustRegionCenterOffset.setEntry(i, ZERO); } xoptsq = ZERO; } if (ntrits == 0) { state = 210; break; } state = 230; break; // XBASE is also moved to XOPT by a call of RESCUE. This calculation is // more expensive than the previous shift, because new matrices BMAT and // ZMAT are generated from scratch, which may include the replacement of // interpolation points whose positions seem to be causing near linear // dependence in the interpolation conditions. Therefore RESCUE is called // only if rounding errors have reduced by at least a factor of two the // denominator of the formula for updating the H matrix. It provides a // useful safeguard, but is not invoked in most applications of BOBYQA. } case 210: { printState(210); // XXX // Pick two alternative vectors of variables, relative to XBASE, that // are suitable as new positions of the KNEW-th interpolation point. // Firstly, XNEW is set to the point on a line through XOPT and another // interpolation point that minimizes the predicted value of the next // denominator, subject to ||XNEW - XOPT|| .LEQ. ADELT and to the SL // and SU bounds. Secondly, XALT is set to the best feasible point on // a constrained version of the Cauchy step of the KNEW-th Lagrange // function, the corresponding value of the square of this function // being returned in CAUCHY. The choice between these alternatives is // going to be made when the denominator is calculated. final double[] alphaCauchy = altmov(knew, adelt); alpha = alphaCauchy[0]; cauchy = alphaCauchy[1]; for (int i = 0; i < n; i++) { trialStepPoint.setEntry(i, newPoint.getEntry(i) - trustRegionCenterOffset.getEntry(i)); } // Calculate VLAG and BETA for the current choice of D. The scalar // product of D with XPT(K,.) is going to be held in W(NPT+K) for // use when VQUAD is calculated. } case 230: { printState(230); // XXX for (int k = 0; k < npt; k++) { double suma = ZERO; double sumb = ZERO; double sum = ZERO; for (int j = 0; j < n; j++) { suma += interpolationPoints.getEntry(k, j) * trialStepPoint.getEntry(j); sumb += interpolationPoints.getEntry(k, j) * trustRegionCenterOffset.getEntry(j); sum += bMatrix.getEntry(k, j) * trialStepPoint.getEntry(j); } work3.setEntry(k, suma * (HALF * suma + sumb)); lagrangeValuesAtNewPoint.setEntry(k, sum); work2.setEntry(k, suma); } beta = ZERO; for (int m = 0; m < nptm; m++) { double sum = ZERO; for (int k = 0; k < npt; k++) { sum += zMatrix.getEntry(k, m) * work3.getEntry(k); } beta -= sum * sum; for (int k = 0; k < npt; k++) { lagrangeValuesAtNewPoint.setEntry(k, lagrangeValuesAtNewPoint.getEntry(k) + sum * zMatrix.getEntry(k, m)); } } dsq = ZERO; double bsum = ZERO; double dx = ZERO; for (int j = 0; j < n; j++) { // Computing 2nd power final double d1 = trialStepPoint.getEntry(j); dsq += d1 * d1; double sum = ZERO; for (int k = 0; k < npt; k++) { sum += work3.getEntry(k) * bMatrix.getEntry(k, j); } bsum += sum * trialStepPoint.getEntry(j); final int jp = npt + j; for (int i = 0; i < n; i++) { sum += bMatrix.getEntry(jp, i) * trialStepPoint.getEntry(i); } lagrangeValuesAtNewPoint.setEntry(jp, sum); bsum += sum * trialStepPoint.getEntry(j); dx += trialStepPoint.getEntry(j) * trustRegionCenterOffset.getEntry(j); } beta = dx * dx + dsq * (xoptsq + dx + dx + HALF * dsq) + beta - bsum; // Original // beta += dx * dx + dsq * (xoptsq + dx + dx + HALF * dsq) - bsum; // XXX "testAckley" and "testDiffPow" fail. // beta = dx * dx + dsq * (xoptsq + 2 * dx + HALF * dsq) + beta - bsum; // XXX "testDiffPow" fails. lagrangeValuesAtNewPoint.setEntry(trustRegionCenterInterpolationPointIndex, lagrangeValuesAtNewPoint.getEntry(trustRegionCenterInterpolationPointIndex) + ONE); // If NTRITS is zero, the denominator may be increased by replacing // the step D of ALTMOV by a Cauchy step. Then RESCUE may be called if // rounding errors have damaged the chosen denominator. if (ntrits == 0) { // Computing 2nd power final double d1 = lagrangeValuesAtNewPoint.getEntry(knew); denom = d1 * d1 + alpha * beta; if (denom < cauchy && cauchy > ZERO) { for (int i = 0; i < n; i++) { newPoint.setEntry(i, alternativeNewPoint.getEntry(i)); trialStepPoint.setEntry(i, newPoint.getEntry(i) - trustRegionCenterOffset.getEntry(i)); } cauchy = ZERO; // XXX Useful statement? state = 230; break; } // Alternatively, if NTRITS is positive, then set KNEW to the index of // the next interpolation point to be deleted to make room for a trust // region step. Again RESCUE may be called if rounding errors have damaged_ // the chosen denominator, which is the reason for attempting to select // KNEW before calculating the next value of the objective function. } else { final double delsq = delta * delta; scaden = ZERO; biglsq = ZERO; knew = 0; for (int k = 0; k < npt; k++) { if (k == trustRegionCenterInterpolationPointIndex) { continue; } double hdiag = ZERO; for (int m = 0; m < nptm; m++) { // Computing 2nd power final double d1 = zMatrix.getEntry(k, m); hdiag += d1 * d1; } // Computing 2nd power final double d2 = lagrangeValuesAtNewPoint.getEntry(k); final double den = beta * hdiag + d2 * d2; distsq = ZERO; for (int j = 0; j < n; j++) { // Computing 2nd power final double d3 = interpolationPoints.getEntry(k, j) - trustRegionCenterOffset.getEntry(j); distsq += d3 * d3; } // Computing MAX // Computing 2nd power final double d4 = distsq / delsq; final double temp = Math.max(ONE, d4 * d4); if (temp * den > scaden) { scaden = temp * den; knew = k; denom = den; } // Computing MAX // Computing 2nd power final double d5 = lagrangeValuesAtNewPoint.getEntry(k); biglsq = Math.max(biglsq, temp * (d5 * d5)); } } // Put the variables for the next calculation of the objective function // in XNEW, with any adjustments for the bounds. // Calculate the value of the objective function at XBASE+XNEW, unless // the limit on the number of calculations of F has been reached. } case 360: { printState(360); // XXX for (int i = 0; i < n; i++) { // Computing MIN // Computing MAX final double d3 = lowerBound[i]; final double d4 = originShift.getEntry(i) + newPoint.getEntry(i); final double d1 = Math.max(d3, d4); final double d2 = upperBound[i]; currentBest.setEntry(i, Math.min(d1, d2)); if (newPoint.getEntry(i) == lowerDifference.getEntry(i)) { currentBest.setEntry(i, lowerBound[i]); } if (newPoint.getEntry(i) == upperDifference.getEntry(i)) { currentBest.setEntry(i, upperBound[i]); } } f = computeObjectiveValue(currentBest.toArray()); if (!isMinimize) f = -f; if (ntrits == -1) { fsave = f; state = 720; break; } // Use the quadratic model to predict the change in F due to the step D, // and set DIFF to the error of this prediction. final double fopt = fAtInterpolationPoints.getEntry(trustRegionCenterInterpolationPointIndex); double vquad = ZERO; int ih = 0; for (int j = 0; j < n; j++) { vquad += trialStepPoint.getEntry(j) * gradientAtTrustRegionCenter.getEntry(j); for (int i = 0; i <= j; i++) { double temp = trialStepPoint.getEntry(i) * trialStepPoint.getEntry(j); if (i == j) { temp *= HALF; } vquad += modelSecondDerivativesValues.getEntry(ih) * temp; ih++; } } for (int k = 0; k < npt; k++) { // Computing 2nd power final double d1 = work2.getEntry(k); final double d2 = d1 * d1; // "d1" must be squared first to prevent test failures. vquad += HALF * modelSecondDerivativesParameters.getEntry(k) * d2; } final double diff = f - fopt - vquad; diffc = diffb; diffb = diffa; diffa = Math.abs(diff); if (dnorm > rho) { nfsav = getEvaluations(); } // Pick the next value of DELTA after a trust region step. if (ntrits > 0) { if (vquad >= ZERO) { throw new MathIllegalStateException(LocalizedFormats.TRUST_REGION_STEP_FAILED, vquad); } ratio = (f - fopt) / vquad; final double hDelta = HALF * delta; if (ratio <= ONE_OVER_TEN) { // Computing MIN delta = Math.min(hDelta, dnorm); } else if (ratio <= .7) { // Computing MAX delta = Math.max(hDelta, dnorm); } else { // Computing MAX delta = Math.max(hDelta, 2 * dnorm); } if (delta <= rho * 1.5) { delta = rho; } // Recalculate KNEW and DENOM if the new F is less than FOPT. if (f < fopt) { final int ksav = knew; final double densav = denom; final double delsq = delta * delta; scaden = ZERO; biglsq = ZERO; knew = 0; for (int k = 0; k < npt; k++) { double hdiag = ZERO; for (int m = 0; m < nptm; m++) { // Computing 2nd power final double d1 = zMatrix.getEntry(k, m); hdiag += d1 * d1; } // Computing 2nd power final double d1 = lagrangeValuesAtNewPoint.getEntry(k); final double den = beta * hdiag + d1 * d1; distsq = ZERO; for (int j = 0; j < n; j++) { // Computing 2nd power final double d2 = interpolationPoints.getEntry(k, j) - newPoint.getEntry(j); distsq += d2 * d2; } // Computing MAX // Computing 2nd power final double d3 = distsq / delsq; final double temp = Math.max(ONE, d3 * d3); if (temp * den > scaden) { scaden = temp * den; knew = k; denom = den; } // Computing MAX // Computing 2nd power final double d4 = lagrangeValuesAtNewPoint.getEntry(k); final double d5 = temp * (d4 * d4); biglsq = Math.max(biglsq, d5); } if (scaden <= HALF * biglsq) { knew = ksav; denom = densav; } } } // Update BMAT and ZMAT, so that the KNEW-th interpolation point can be // moved. Also update the second derivative terms of the model. update(beta, denom, knew); ih = 0; final double pqold = modelSecondDerivativesParameters.getEntry(knew); modelSecondDerivativesParameters.setEntry(knew, ZERO); for (int i = 0; i < n; i++) { final double temp = pqold * interpolationPoints.getEntry(knew, i); for (int j = 0; j <= i; j++) { modelSecondDerivativesValues.setEntry(ih, modelSecondDerivativesValues.getEntry(ih) + temp * interpolationPoints.getEntry(knew, j)); ih++; } } for (int m = 0; m < nptm; m++) { final double temp = diff * zMatrix.getEntry(knew, m); for (int k = 0; k < npt; k++) { modelSecondDerivativesParameters.setEntry(k, modelSecondDerivativesParameters.getEntry(k) + temp * zMatrix.getEntry(k, m)); } } // Include the new interpolation point, and make the changes to GOPT at // the old XOPT that are caused by the updating of the quadratic model. fAtInterpolationPoints.setEntry(knew, f); for (int i = 0; i < n; i++) { interpolationPoints.setEntry(knew, i, newPoint.getEntry(i)); work1.setEntry(i, bMatrix.getEntry(knew, i)); } for (int k = 0; k < npt; k++) { double suma = ZERO; for (int m = 0; m < nptm; m++) { suma += zMatrix.getEntry(knew, m) * zMatrix.getEntry(k, m); } double sumb = ZERO; for (int j = 0; j < n; j++) { sumb += interpolationPoints.getEntry(k, j) * trustRegionCenterOffset.getEntry(j); } final double temp = suma * sumb; for (int i = 0; i < n; i++) { work1.setEntry(i, work1.getEntry(i) + temp * interpolationPoints.getEntry(k, i)); } } for (int i = 0; i < n; i++) { gradientAtTrustRegionCenter.setEntry(i, gradientAtTrustRegionCenter.getEntry(i) + diff * work1.getEntry(i)); } // Update XOPT, GOPT and KOPT if the new calculated F is less than FOPT. if (f < fopt) { trustRegionCenterInterpolationPointIndex = knew; xoptsq = ZERO; ih = 0; for (int j = 0; j < n; j++) { trustRegionCenterOffset.setEntry(j, newPoint.getEntry(j)); // Computing 2nd power final double d1 = trustRegionCenterOffset.getEntry(j); xoptsq += d1 * d1; for (int i = 0; i <= j; i++) { if (i < j) { gradientAtTrustRegionCenter.setEntry(j, gradientAtTrustRegionCenter.getEntry(j) + modelSecondDerivativesValues.getEntry(ih) * trialStepPoint.getEntry(i)); } gradientAtTrustRegionCenter.setEntry(i, gradientAtTrustRegionCenter.getEntry(i) + modelSecondDerivativesValues.getEntry(ih) * trialStepPoint.getEntry(j)); ih++; } } for (int k = 0; k < npt; k++) { double temp = ZERO; for (int j = 0; j < n; j++) { temp += interpolationPoints.getEntry(k, j) * trialStepPoint.getEntry(j); } temp *= modelSecondDerivativesParameters.getEntry(k); for (int i = 0; i < n; i++) { gradientAtTrustRegionCenter.setEntry(i, gradientAtTrustRegionCenter.getEntry(i) + temp * interpolationPoints.getEntry(k, i)); } } } // Calculate the parameters of the least Frobenius norm interpolant to // the current data, the gradient of this interpolant at XOPT being put // into VLAG(NPT+I), I=1,2,...,N. if (ntrits > 0) { for (int k = 0; k < npt; k++) { lagrangeValuesAtNewPoint.setEntry(k, fAtInterpolationPoints.getEntry(k) - fAtInterpolationPoints.getEntry(trustRegionCenterInterpolationPointIndex)); work3.setEntry(k, ZERO); } for (int j = 0; j < nptm; j++) { double sum = ZERO; for (int k = 0; k < npt; k++) { sum += zMatrix.getEntry(k, j) * lagrangeValuesAtNewPoint.getEntry(k); } for (int k = 0; k < npt; k++) { work3.setEntry(k, work3.getEntry(k) + sum * zMatrix.getEntry(k, j)); } } for (int k = 0; k < npt; k++) { double sum = ZERO; for (int j = 0; j < n; j++) { sum += interpolationPoints.getEntry(k, j) * trustRegionCenterOffset.getEntry(j); } work2.setEntry(k, work3.getEntry(k)); work3.setEntry(k, sum * work3.getEntry(k)); } double gqsq = ZERO; double gisq = ZERO; for (int i = 0; i < n; i++) { double sum = ZERO; for (int k = 0; k < npt; k++) { sum += bMatrix.getEntry(k, i) * lagrangeValuesAtNewPoint.getEntry(k) + interpolationPoints.getEntry(k, i) * work3.getEntry(k); } if (trustRegionCenterOffset.getEntry(i) == lowerDifference.getEntry(i)) { // Computing MIN // Computing 2nd power final double d1 = Math.min(ZERO, gradientAtTrustRegionCenter.getEntry(i)); gqsq += d1 * d1; // Computing 2nd power final double d2 = Math.min(ZERO, sum); gisq += d2 * d2; } else if (trustRegionCenterOffset.getEntry(i) == upperDifference.getEntry(i)) { // Computing MAX // Computing 2nd power final double d1 = Math.max(ZERO, gradientAtTrustRegionCenter.getEntry(i)); gqsq += d1 * d1; // Computing 2nd power final double d2 = Math.max(ZERO, sum); gisq += d2 * d2; } else { // Computing 2nd power final double d1 = gradientAtTrustRegionCenter.getEntry(i); gqsq += d1 * d1; gisq += sum * sum; } lagrangeValuesAtNewPoint.setEntry(npt + i, sum); } // Test whether to replace the new quadratic model by the least Frobenius // norm interpolant, making the replacement if the test is satisfied. ++itest; if (gqsq < TEN * gisq) { itest = 0; } if (itest >= 3) { for (int i = 0, max = Math.max(npt, nh); i < max; i++) { if (i < n) { gradientAtTrustRegionCenter.setEntry(i, lagrangeValuesAtNewPoint.getEntry(npt + i)); } if (i < npt) { modelSecondDerivativesParameters.setEntry(i, work2.getEntry(i)); } if (i < nh) { modelSecondDerivativesValues.setEntry(i, ZERO); } itest = 0; } } } // If a trust region step has provided a sufficient decrease in F, then // branch for another trust region calculation. The case NTRITS=0 occurs // when the new interpolation point was reached by an alternative step. if (ntrits == 0) { state = 60; break; } if (f <= fopt + ONE_OVER_TEN * vquad) { state = 60; break; } // Alternatively, find out if the interpolation points are close enough // to the best point so far. // Computing MAX // Computing 2nd power final double d1 = TWO * delta; // Computing 2nd power final double d2 = TEN * rho; distsq = Math.max(d1 * d1, d2 * d2); } case 650: { printState(650); // XXX knew = -1; for (int k = 0; k < npt; k++) { double sum = ZERO; for (int j = 0; j < n; j++) { // Computing 2nd power final double d1 = interpolationPoints.getEntry(k, j) - trustRegionCenterOffset.getEntry(j); sum += d1 * d1; } if (sum > distsq) { knew = k; distsq = sum; } } // If KNEW is positive, then ALTMOV finds alternative new positions for // the KNEW-th interpolation point within distance ADELT of XOPT. It is // reached via label 90. Otherwise, there is a branch to label 60 for // another trust region iteration, unless the calculations with the // current RHO are complete. if (knew >= 0) { final double dist = Math.sqrt(distsq); if (ntrits == -1) { // Computing MIN delta = Math.min(ONE_OVER_TEN * delta, HALF * dist); if (delta <= rho * 1.5) { delta = rho; } } ntrits = 0; // Computing MAX // Computing MIN final double d1 = Math.min(ONE_OVER_TEN * dist, delta); adelt = Math.max(d1, rho); dsq = adelt * adelt; state = 90; break; } if (ntrits == -1) { state = 680; break; } if (ratio > ZERO) { state = 60; break; } if (Math.max(delta, dnorm) > rho) { state = 60; break; } // The calculations with the current value of RHO are complete. Pick the // next values of RHO and DELTA. } case 680: { printState(680); // XXX if (rho > stoppingTrustRegionRadius) { delta = HALF * rho; ratio = rho / stoppingTrustRegionRadius; if (ratio <= SIXTEEN) { rho = stoppingTrustRegionRadius; } else if (ratio <= TWO_HUNDRED_FIFTY) { rho = Math.sqrt(ratio) * stoppingTrustRegionRadius; } else { rho *= ONE_OVER_TEN; } delta = Math.max(delta, rho); ntrits = 0; nfsav = getEvaluations(); state = 60; break; } // Return from the calculation, after another Newton-Raphson step, if // it is too short to have been tried before. if (ntrits == -1) { state = 360; break; } } case 720: { printState(720); // XXX if (fAtInterpolationPoints.getEntry(trustRegionCenterInterpolationPointIndex) <= fsave) { for (int i = 0; i < n; i++) { // Computing MIN // Computing MAX final double d3 = lowerBound[i]; final double d4 = originShift.getEntry(i) + trustRegionCenterOffset.getEntry(i); final double d1 = Math.max(d3, d4); final double d2 = upperBound[i]; currentBest.setEntry(i, Math.min(d1, d2)); if (trustRegionCenterOffset.getEntry(i) == lowerDifference.getEntry(i)) { currentBest.setEntry(i, lowerBound[i]); } if (trustRegionCenterOffset.getEntry(i) == upperDifference.getEntry(i)) { currentBest.setEntry(i, upperBound[i]); } } f = fAtInterpolationPoints.getEntry(trustRegionCenterInterpolationPointIndex); } return f; } default: { throw new MathIllegalStateException(LocalizedFormats.SIMPLE_MESSAGE, "bobyqb"); }} } // bobyqb // ---------------------------------------------------------------------------------------- /** * The arguments N, NPT, XPT, XOPT, BMAT, ZMAT, NDIM, SL and SU all have * the same meanings as the corresponding arguments of BOBYQB. * KOPT is the index of the optimal interpolation point. * KNEW is the index of the interpolation point that is going to be moved. * ADELT is the current trust region bound. * XNEW will be set to a suitable new position for the interpolation point * XPT(KNEW,.). Specifically, it satisfies the SL, SU and trust region * bounds and it should provide a large denominator in the next call of * UPDATE. The step XNEW-XOPT from XOPT is restricted to moves along the * straight lines through XOPT and another interpolation point. * XALT also provides a large value of the modulus of the KNEW-th Lagrange * function subject to the constraints that have been mentioned, its main * difference from XNEW being that XALT-XOPT is a constrained version of * the Cauchy step within the trust region. An exception is that XALT is * not calculated if all components of GLAG (see below) are zero. * ALPHA will be set to the KNEW-th diagonal element of the H matrix. * CAUCHY will be set to the square of the KNEW-th Lagrange function at * the step XALT-XOPT from XOPT for the vector XALT that is returned, * except that CAUCHY is set to zero if XALT is not calculated. * GLAG is a working space vector of length N for the gradient of the * KNEW-th Lagrange function at XOPT. * HCOL is a working space vector of length NPT for the second derivative * coefficients of the KNEW-th Lagrange function. * W is a working space vector of length 2N that is going to hold the * constrained Cauchy step from XOPT of the Lagrange function, followed * by the downhill version of XALT when the uphill step is calculated. * * Set the first NPT components of W to the leading elements of the * KNEW-th column of the H matrix. * @param knew * @param adelt */ private double[] altmov( int knew, double adelt ) { printMethod(); // XXX final int n = currentBest.getDimension(); final int npt = numberOfInterpolationPoints; final ArrayRealVector glag = new ArrayRealVector(n); final ArrayRealVector hcol = new ArrayRealVector(npt); final ArrayRealVector work1 = new ArrayRealVector(n); final ArrayRealVector work2 = new ArrayRealVector(n); for (int k = 0; k < npt; k++) { hcol.setEntry(k, ZERO); } for (int j = 0, max = npt - n - 1; j < max; j++) { final double tmp = zMatrix.getEntry(knew, j); for (int k = 0; k < npt; k++) { hcol.setEntry(k, hcol.getEntry(k) + tmp * zMatrix.getEntry(k, j)); } } final double alpha = hcol.getEntry(knew); final double ha = HALF * alpha; // Calculate the gradient of the KNEW-th Lagrange function at XOPT. for (int i = 0; i < n; i++) { glag.setEntry(i, bMatrix.getEntry(knew, i)); } for (int k = 0; k < npt; k++) { double tmp = ZERO; for (int j = 0; j < n; j++) { tmp += interpolationPoints.getEntry(k, j) * trustRegionCenterOffset.getEntry(j); } tmp *= hcol.getEntry(k); for (int i = 0; i < n; i++) { glag.setEntry(i, glag.getEntry(i) + tmp * interpolationPoints.getEntry(k, i)); } } // Search for a large denominator along the straight lines through XOPT // and another interpolation point. SLBD and SUBD will be lower and upper // bounds on the step along each of these lines in turn. PREDSQ will be // set to the square of the predicted denominator for each line. PRESAV // will be set to the largest admissible value of PREDSQ that occurs. double presav = ZERO; double step = Double.NaN; int ksav = 0; int ibdsav = 0; double stpsav = 0; for (int k = 0; k < npt; k++) { if (k == trustRegionCenterInterpolationPointIndex) { continue; } double dderiv = ZERO; double distsq = ZERO; for (int i = 0; i < n; i++) { final double tmp = interpolationPoints.getEntry(k, i) - trustRegionCenterOffset.getEntry(i); dderiv += glag.getEntry(i) * tmp; distsq += tmp * tmp; } double subd = adelt / Math.sqrt(distsq); double slbd = -subd; int ilbd = 0; int iubd = 0; final double sumin = Math.min(ONE, subd); // Revise SLBD and SUBD if necessary because of the bounds in SL and SU. for (int i = 0; i < n; i++) { final double tmp = interpolationPoints.getEntry(k, i) - trustRegionCenterOffset.getEntry(i); if (tmp > ZERO) { if (slbd * tmp < lowerDifference.getEntry(i) - trustRegionCenterOffset.getEntry(i)) { slbd = (lowerDifference.getEntry(i) - trustRegionCenterOffset.getEntry(i)) / tmp; ilbd = -i - 1; } if (subd * tmp > upperDifference.getEntry(i) - trustRegionCenterOffset.getEntry(i)) { // Computing MAX subd = Math.max(sumin, (upperDifference.getEntry(i) - trustRegionCenterOffset.getEntry(i)) / tmp); iubd = i + 1; } } else if (tmp < ZERO) { if (slbd * tmp > upperDifference.getEntry(i) - trustRegionCenterOffset.getEntry(i)) { slbd = (upperDifference.getEntry(i) - trustRegionCenterOffset.getEntry(i)) / tmp; ilbd = i + 1; } if (subd * tmp < lowerDifference.getEntry(i) - trustRegionCenterOffset.getEntry(i)) { // Computing MAX subd = Math.max(sumin, (lowerDifference.getEntry(i) - trustRegionCenterOffset.getEntry(i)) / tmp); iubd = -i - 1; } } } // Seek a large modulus of the KNEW-th Lagrange function when the index // of the other interpolation point on the line through XOPT is KNEW. step = slbd; int isbd = ilbd; double vlag = Double.NaN; if (k == knew) { final double diff = dderiv - ONE; vlag = slbd * (dderiv - slbd * diff); final double d1 = subd * (dderiv - subd * diff); if (Math.abs(d1) > Math.abs(vlag)) { step = subd; vlag = d1; isbd = iubd; } final double d2 = HALF * dderiv; final double d3 = d2 - diff * slbd; final double d4 = d2 - diff * subd; if (d3 * d4 < ZERO) { final double d5 = d2 * d2 / diff; if (Math.abs(d5) > Math.abs(vlag)) { step = d2 / diff; vlag = d5; isbd = 0; } } // Search along each of the other lines through XOPT and another point. } else { vlag = slbd * (ONE - slbd); final double tmp = subd * (ONE - subd); if (Math.abs(tmp) > Math.abs(vlag)) { step = subd; vlag = tmp; isbd = iubd; } if (subd > HALF && Math.abs(vlag) < ONE_OVER_FOUR) { step = HALF; vlag = ONE_OVER_FOUR; isbd = 0; } vlag *= dderiv; } // Calculate PREDSQ for the current line search and maintain PRESAV. final double tmp = step * (ONE - step) * distsq; final double predsq = vlag * vlag * (vlag * vlag + ha * tmp * tmp); if (predsq > presav) { presav = predsq; ksav = k; stpsav = step; ibdsav = isbd; } } // Construct XNEW in a way that satisfies the bound constraints exactly. for (int i = 0; i < n; i++) { final double tmp = trustRegionCenterOffset.getEntry(i) + stpsav * (interpolationPoints.getEntry(ksav, i) - trustRegionCenterOffset.getEntry(i)); newPoint.setEntry(i, Math.max(lowerDifference.getEntry(i), Math.min(upperDifference.getEntry(i), tmp))); } if (ibdsav < 0) { newPoint.setEntry(-ibdsav - 1, lowerDifference.getEntry(-ibdsav - 1)); } if (ibdsav > 0) { newPoint.setEntry(ibdsav - 1, upperDifference.getEntry(ibdsav - 1)); } // Prepare for the iterative method that assembles the constrained Cauchy // step in W. The sum of squares of the fixed components of W is formed in // WFIXSQ, and the free components of W are set to BIGSTP. final double bigstp = adelt + adelt; int iflag = 0; double cauchy = Double.NaN; double csave = ZERO; while (true) { double wfixsq = ZERO; double ggfree = ZERO; for (int i = 0; i < n; i++) { final double glagValue = glag.getEntry(i); work1.setEntry(i, ZERO); if (Math.min(trustRegionCenterOffset.getEntry(i) - lowerDifference.getEntry(i), glagValue) > ZERO || Math.max(trustRegionCenterOffset.getEntry(i) - upperDifference.getEntry(i), glagValue) < ZERO) { work1.setEntry(i, bigstp); // Computing 2nd power ggfree += glagValue * glagValue; } } if (ggfree == ZERO) { return new double[] { alpha, ZERO }; } // Investigate whether more components of W can be fixed. final double tmp1 = adelt * adelt - wfixsq; if (tmp1 > ZERO) { step = Math.sqrt(tmp1 / ggfree); ggfree = ZERO; for (int i = 0; i < n; i++) { if (work1.getEntry(i) == bigstp) { final double tmp2 = trustRegionCenterOffset.getEntry(i) - step * glag.getEntry(i); if (tmp2 <= lowerDifference.getEntry(i)) { work1.setEntry(i, lowerDifference.getEntry(i) - trustRegionCenterOffset.getEntry(i)); // Computing 2nd power final double d1 = work1.getEntry(i); wfixsq += d1 * d1; } else if (tmp2 >= upperDifference.getEntry(i)) { work1.setEntry(i, upperDifference.getEntry(i) - trustRegionCenterOffset.getEntry(i)); // Computing 2nd power final double d1 = work1.getEntry(i); wfixsq += d1 * d1; } else { // Computing 2nd power final double d1 = glag.getEntry(i); ggfree += d1 * d1; } } } } // Set the remaining free components of W and all components of XALT, // except that W may be scaled later. double gw = ZERO; for (int i = 0; i < n; i++) { final double glagValue = glag.getEntry(i); if (work1.getEntry(i) == bigstp) { work1.setEntry(i, -step * glagValue); final double min = Math.min(upperDifference.getEntry(i), trustRegionCenterOffset.getEntry(i) + work1.getEntry(i)); alternativeNewPoint.setEntry(i, Math.max(lowerDifference.getEntry(i), min)); } else if (work1.getEntry(i) == ZERO) { alternativeNewPoint.setEntry(i, trustRegionCenterOffset.getEntry(i)); } else if (glagValue > ZERO) { alternativeNewPoint.setEntry(i, lowerDifference.getEntry(i)); } else { alternativeNewPoint.setEntry(i, upperDifference.getEntry(i)); } gw += glagValue * work1.getEntry(i); } // Set CURV to the curvature of the KNEW-th Lagrange function along W. // Scale W by a factor less than one if that can reduce the modulus of // the Lagrange function at XOPT+W. Set CAUCHY to the final value of // the square of this function. double curv = ZERO; for (int k = 0; k < npt; k++) { double tmp = ZERO; for (int j = 0; j < n; j++) { tmp += interpolationPoints.getEntry(k, j) * work1.getEntry(j); } curv += hcol.getEntry(k) * tmp * tmp; } if (iflag == 1) { curv = -curv; } if (curv > -gw && curv < -gw * (ONE + Math.sqrt(TWO))) { final double scale = -gw / curv; for (int i = 0; i < n; i++) { final double tmp = trustRegionCenterOffset.getEntry(i) + scale * work1.getEntry(i); alternativeNewPoint.setEntry(i, Math.max(lowerDifference.getEntry(i), Math.min(upperDifference.getEntry(i), tmp))); } // Computing 2nd power final double d1 = HALF * gw * scale; cauchy = d1 * d1; } else { // Computing 2nd power final double d1 = gw + HALF * curv; cauchy = d1 * d1; } // If IFLAG is zero, then XALT is calculated as before after reversing // the sign of GLAG. Thus two XALT vectors become available. The one that // is chosen is the one that gives the larger value of CAUCHY. if (iflag == 0) { for (int i = 0; i < n; i++) { glag.setEntry(i, -glag.getEntry(i)); work2.setEntry(i, alternativeNewPoint.getEntry(i)); } csave = cauchy; iflag = 1; } else { break; } } if (csave > cauchy) { for (int i = 0; i < n; i++) { alternativeNewPoint.setEntry(i, work2.getEntry(i)); } cauchy = csave; } return new double[] { alpha, cauchy }; } // altmov // ---------------------------------------------------------------------------------------- /** * SUBROUTINE PRELIM sets the elements of XBASE, XPT, FVAL, GOPT, HQ, PQ, * BMAT and ZMAT for the first iteration, and it maintains the values of * NF and KOPT. The vector X is also changed by PRELIM. * * The arguments N, NPT, X, XL, XU, RHOBEG, IPRINT and MAXFUN are the * same as the corresponding arguments in SUBROUTINE BOBYQA. * The arguments XBASE, XPT, FVAL, HQ, PQ, BMAT, ZMAT, NDIM, SL and SU * are the same as the corresponding arguments in BOBYQB, the elements * of SL and SU being set in BOBYQA. * GOPT is usually the gradient of the quadratic model at XOPT+XBASE, but * it is set by PRELIM to the gradient of the quadratic model at XBASE. * If XOPT is nonzero, BOBYQB will change it to its usual value later. * NF is maintaned as the number of calls of CALFUN so far. * KOPT will be such that the least calculated value of F so far is at * the point XPT(KOPT,.)+XBASE in the space of the variables. * * @param lowerBound Lower bounds. * @param upperBound Upper bounds. */ private void prelim(double[] lowerBound, double[] upperBound) { printMethod(); // XXX final int n = currentBest.getDimension(); final int npt = numberOfInterpolationPoints; final int ndim = bMatrix.getRowDimension(); final double rhosq = initialTrustRegionRadius * initialTrustRegionRadius; final double recip = 1d / rhosq; final int np = n + 1; // Set XBASE to the initial vector of variables, and set the initial // elements of XPT, BMAT, HQ, PQ and ZMAT to zero. for (int j = 0; j < n; j++) { originShift.setEntry(j, currentBest.getEntry(j)); for (int k = 0; k < npt; k++) { interpolationPoints.setEntry(k, j, ZERO); } for (int i = 0; i < ndim; i++) { bMatrix.setEntry(i, j, ZERO); } } for (int i = 0, max = n * np / 2; i < max; i++) { modelSecondDerivativesValues.setEntry(i, ZERO); } for (int k = 0; k < npt; k++) { modelSecondDerivativesParameters.setEntry(k, ZERO); for (int j = 0, max = npt - np; j < max; j++) { zMatrix.setEntry(k, j, ZERO); } } // Begin the initialization procedure. NF becomes one more than the number // of function values so far. The coordinates of the displacement of the // next initial interpolation point from XBASE are set in XPT(NF+1,.). int ipt = 0; int jpt = 0; double fbeg = Double.NaN; do { final int nfm = getEvaluations(); final int nfx = nfm - n; final int nfmm = nfm - 1; final int nfxm = nfx - 1; double stepa = 0; double stepb = 0; if (nfm <= 2 * n) { if (nfm >= 1 && nfm <= n) { stepa = initialTrustRegionRadius; if (upperDifference.getEntry(nfmm) == ZERO) { stepa = -stepa; // throw new PathIsExploredException(); // XXX } interpolationPoints.setEntry(nfm, nfmm, stepa); } else if (nfm > n) { stepa = interpolationPoints.getEntry(nfx, nfxm); stepb = -initialTrustRegionRadius; if (lowerDifference.getEntry(nfxm) == ZERO) { stepb = Math.min(TWO * initialTrustRegionRadius, upperDifference.getEntry(nfxm)); // throw new PathIsExploredException(); // XXX } if (upperDifference.getEntry(nfxm) == ZERO) { stepb = Math.max(-TWO * initialTrustRegionRadius, lowerDifference.getEntry(nfxm)); // throw new PathIsExploredException(); // XXX } interpolationPoints.setEntry(nfm, nfxm, stepb); } } else { final int tmp1 = (nfm - np) / n; jpt = nfm - tmp1 * n - n; ipt = jpt + tmp1; if (ipt > n) { final int tmp2 = jpt; jpt = ipt - n; ipt = tmp2; // throw new PathIsExploredException(); // XXX } final int iptMinus1 = ipt - 1; final int jptMinus1 = jpt - 1; interpolationPoints.setEntry(nfm, iptMinus1, interpolationPoints.getEntry(ipt, iptMinus1)); interpolationPoints.setEntry(nfm, jptMinus1, interpolationPoints.getEntry(jpt, jptMinus1)); } // Calculate the next value of F. The least function value so far and // its index are required. for (int j = 0; j < n; j++) { currentBest.setEntry(j, Math.min(Math.max(lowerBound[j], originShift.getEntry(j) + interpolationPoints.getEntry(nfm, j)), upperBound[j])); if (interpolationPoints.getEntry(nfm, j) == lowerDifference.getEntry(j)) { currentBest.setEntry(j, lowerBound[j]); } if (interpolationPoints.getEntry(nfm, j) == upperDifference.getEntry(j)) { currentBest.setEntry(j, upperBound[j]); } } final double objectiveValue = computeObjectiveValue(currentBest.toArray()); final double f = isMinimize ? objectiveValue : -objectiveValue; final int numEval = getEvaluations(); // nfm + 1 fAtInterpolationPoints.setEntry(nfm, f); if (numEval == 1) { fbeg = f; trustRegionCenterInterpolationPointIndex = 0; } else if (f < fAtInterpolationPoints.getEntry(trustRegionCenterInterpolationPointIndex)) { trustRegionCenterInterpolationPointIndex = nfm; } // Set the nonzero initial elements of BMAT and the quadratic model in the // cases when NF is at most 2*N+1. If NF exceeds N+1, then the positions // of the NF-th and (NF-N)-th interpolation points may be switched, in // order that the function value at the first of them contributes to the // off-diagonal second derivative terms of the initial quadratic model. if (numEval <= 2 * n + 1) { if (numEval >= 2 && numEval <= n + 1) { gradientAtTrustRegionCenter.setEntry(nfmm, (f - fbeg) / stepa); if (npt < numEval + n) { final double oneOverStepA = ONE / stepa; bMatrix.setEntry(0, nfmm, -oneOverStepA); bMatrix.setEntry(nfm, nfmm, oneOverStepA); bMatrix.setEntry(npt + nfmm, nfmm, -HALF * rhosq); // throw new PathIsExploredException(); // XXX } } else if (numEval >= n + 2) { final int ih = nfx * (nfx + 1) / 2 - 1; final double tmp = (f - fbeg) / stepb; final double diff = stepb - stepa; modelSecondDerivativesValues.setEntry(ih, TWO * (tmp - gradientAtTrustRegionCenter.getEntry(nfxm)) / diff); gradientAtTrustRegionCenter.setEntry(nfxm, (gradientAtTrustRegionCenter.getEntry(nfxm) * stepb - tmp * stepa) / diff); if (stepa * stepb < ZERO && f < fAtInterpolationPoints.getEntry(nfm - n)) { fAtInterpolationPoints.setEntry(nfm, fAtInterpolationPoints.getEntry(nfm - n)); fAtInterpolationPoints.setEntry(nfm - n, f); if (trustRegionCenterInterpolationPointIndex == nfm) { trustRegionCenterInterpolationPointIndex = nfm - n; } interpolationPoints.setEntry(nfm - n, nfxm, stepb); interpolationPoints.setEntry(nfm, nfxm, stepa); } bMatrix.setEntry(0, nfxm, -(stepa + stepb) / (stepa * stepb)); bMatrix.setEntry(nfm, nfxm, -HALF / interpolationPoints.getEntry(nfm - n, nfxm)); bMatrix.setEntry(nfm - n, nfxm, -bMatrix.getEntry(0, nfxm) - bMatrix.getEntry(nfm, nfxm)); zMatrix.setEntry(0, nfxm, Math.sqrt(TWO) / (stepa * stepb)); zMatrix.setEntry(nfm, nfxm, Math.sqrt(HALF) / rhosq); // zMatrix.setEntry(nfm, nfxm, Math.sqrt(HALF) * recip); // XXX "testAckley" and "testDiffPow" fail. zMatrix.setEntry(nfm - n, nfxm, -zMatrix.getEntry(0, nfxm) - zMatrix.getEntry(nfm, nfxm)); } // Set the off-diagonal second derivatives of the Lagrange functions and // the initial quadratic model. } else { zMatrix.setEntry(0, nfxm, recip); zMatrix.setEntry(nfm, nfxm, recip); zMatrix.setEntry(ipt, nfxm, -recip); zMatrix.setEntry(jpt, nfxm, -recip); final int ih = ipt * (ipt - 1) / 2 + jpt - 1; final double tmp = interpolationPoints.getEntry(nfm, ipt - 1) * interpolationPoints.getEntry(nfm, jpt - 1); modelSecondDerivativesValues.setEntry(ih, (fbeg - fAtInterpolationPoints.getEntry(ipt) - fAtInterpolationPoints.getEntry(jpt) + f) / tmp); // throw new PathIsExploredException(); // XXX } } while (getEvaluations() < npt); } // prelim // ---------------------------------------------------------------------------------------- /** * A version of the truncated conjugate gradient is applied. If a line * search is restricted by a constraint, then the procedure is restarted, * the values of the variables that are at their bounds being fixed. If * the trust region boundary is reached, then further changes may be made * to D, each one being in the two dimensional space that is spanned * by the current D and the gradient of Q at XOPT+D, staying on the trust * region boundary. Termination occurs when the reduction in Q seems to * be close to the greatest reduction that can be achieved. * The arguments N, NPT, XPT, XOPT, GOPT, HQ, PQ, SL and SU have the same * meanings as the corresponding arguments of BOBYQB. * DELTA is the trust region radius for the present calculation, which * seeks a small value of the quadratic model within distance DELTA of * XOPT subject to the bounds on the variables. * XNEW will be set to a new vector of variables that is approximately * the one that minimizes the quadratic model within the trust region * subject to the SL and SU constraints on the variables. It satisfies * as equations the bounds that become active during the calculation. * D is the calculated trial step from XOPT, generated iteratively from an * initial value of zero. Thus XNEW is XOPT+D after the final iteration. * GNEW holds the gradient of the quadratic model at XOPT+D. It is updated * when D is updated. * xbdi.get( is a working space vector. For I=1,2,...,N, the element xbdi.get((I) is * set to -1.0, 0.0, or 1.0, the value being nonzero if and only if the * I-th variable has become fixed at a bound, the bound being SL(I) or * SU(I) in the case xbdi.get((I)=-1.0 or xbdi.get((I)=1.0, respectively. This * information is accumulated during the construction of XNEW. * The arrays S, HS and HRED are also used for working space. They hold the * current search direction, and the changes in the gradient of Q along S * and the reduced D, respectively, where the reduced D is the same as D, * except that the components of the fixed variables are zero. * DSQ will be set to the square of the length of XNEW-XOPT. * CRVMIN is set to zero if D reaches the trust region boundary. Otherwise * it is set to the least curvature of H that occurs in the conjugate * gradient searches that are not restricted by any constraints. The * value CRVMIN=-1.0D0 is set, however, if all of these searches are * constrained. * @param delta * @param gnew * @param xbdi * @param s * @param hs * @param hred */ private double[] trsbox( double delta, ArrayRealVector gnew, ArrayRealVector xbdi, ArrayRealVector s, ArrayRealVector hs, ArrayRealVector hred ) { printMethod(); // XXX final int n = currentBest.getDimension(); final int npt = numberOfInterpolationPoints; double dsq = Double.NaN; double crvmin = Double.NaN; // Local variables double ds; int iu; double dhd, dhs, cth, shs, sth, ssq, beta=0, sdec, blen; int iact = -1; int nact = 0; double angt = 0, qred; int isav; double temp = 0, xsav = 0, xsum = 0, angbd = 0, dredg = 0, sredg = 0; int iterc; double resid = 0, delsq = 0, ggsav = 0, tempa = 0, tempb = 0, redmax = 0, dredsq = 0, redsav = 0, gredsq = 0, rednew = 0; int itcsav = 0; double rdprev = 0, rdnext = 0, stplen = 0, stepsq = 0; int itermax = 0; // Set some constants. // Function Body // The sign of GOPT(I) gives the sign of the change to the I-th variable // that will reduce Q from its value at XOPT. Thus xbdi.get((I) shows whether // or not to fix the I-th variable at one of its bounds initially, with // NACT being set to the number of fixed variables. D and GNEW are also // set for the first iteration. DELSQ is the upper bound on the sum of // squares of the free variables. QRED is the reduction in Q so far. iterc = 0; nact = 0; for (int i = 0; i < n; i++) { xbdi.setEntry(i, ZERO); if (trustRegionCenterOffset.getEntry(i) <= lowerDifference.getEntry(i)) { if (gradientAtTrustRegionCenter.getEntry(i) >= ZERO) { xbdi.setEntry(i, MINUS_ONE); } } else if (trustRegionCenterOffset.getEntry(i) >= upperDifference.getEntry(i) && gradientAtTrustRegionCenter.getEntry(i) <= ZERO) { xbdi.setEntry(i, ONE); } if (xbdi.getEntry(i) != ZERO) { ++nact; } trialStepPoint.setEntry(i, ZERO); gnew.setEntry(i, gradientAtTrustRegionCenter.getEntry(i)); } delsq = delta * delta; qred = ZERO; crvmin = MINUS_ONE; // Set the next search direction of the conjugate gradient method. It is // the steepest descent direction initially and when the iterations are // restarted because a variable has just been fixed by a bound, and of // course the components of the fixed variables are zero. ITERMAX is an // upper bound on the indices of the conjugate gradient iterations. int state = 20; for(;;) { switch (state) { case 20: { printState(20); // XXX beta = ZERO; } case 30: { printState(30); // XXX stepsq = ZERO; for (int i = 0; i < n; i++) { if (xbdi.getEntry(i) != ZERO) { s.setEntry(i, ZERO); } else if (beta == ZERO) { s.setEntry(i, -gnew.getEntry(i)); } else { s.setEntry(i, beta * s.getEntry(i) - gnew.getEntry(i)); } // Computing 2nd power final double d1 = s.getEntry(i); stepsq += d1 * d1; } if (stepsq == ZERO) { state = 190; break; } if (beta == ZERO) { gredsq = stepsq; itermax = iterc + n - nact; } if (gredsq * delsq <= qred * 1e-4 * qred) { state = 190; break; } // Multiply the search direction by the second derivative matrix of Q and // calculate some scalars for the choice of steplength. Then set BLEN to // the length of the the step to the trust region boundary and STPLEN to // the steplength, ignoring the simple bounds. state = 210; break; } case 50: { printState(50); // XXX resid = delsq; ds = ZERO; shs = ZERO; for (int i = 0; i < n; i++) { if (xbdi.getEntry(i) == ZERO) { // Computing 2nd power final double d1 = trialStepPoint.getEntry(i); resid -= d1 * d1; ds += s.getEntry(i) * trialStepPoint.getEntry(i); shs += s.getEntry(i) * hs.getEntry(i); } } if (resid <= ZERO) { state = 90; break; } temp = Math.sqrt(stepsq * resid + ds * ds); if (ds < ZERO) { blen = (temp - ds) / stepsq; } else { blen = resid / (temp + ds); } stplen = blen; if (shs > ZERO) { // Computing MIN stplen = Math.min(blen, gredsq / shs); } // Reduce STPLEN if necessary in order to preserve the simple bounds, // letting IACT be the index of the new constrained variable. iact = -1; for (int i = 0; i < n; i++) { if (s.getEntry(i) != ZERO) { xsum = trustRegionCenterOffset.getEntry(i) + trialStepPoint.getEntry(i); if (s.getEntry(i) > ZERO) { temp = (upperDifference.getEntry(i) - xsum) / s.getEntry(i); } else { temp = (lowerDifference.getEntry(i) - xsum) / s.getEntry(i); } if (temp < stplen) { stplen = temp; iact = i; } } } // Update CRVMIN, GNEW and D. Set SDEC to the decrease that occurs in Q. sdec = ZERO; if (stplen > ZERO) { ++iterc; temp = shs / stepsq; if (iact == -1 && temp > ZERO) { crvmin = Math.min(crvmin,temp); if (crvmin == MINUS_ONE) { crvmin = temp; } } ggsav = gredsq; gredsq = ZERO; for (int i = 0; i < n; i++) { gnew.setEntry(i, gnew.getEntry(i) + stplen * hs.getEntry(i)); if (xbdi.getEntry(i) == ZERO) { // Computing 2nd power final double d1 = gnew.getEntry(i); gredsq += d1 * d1; } trialStepPoint.setEntry(i, trialStepPoint.getEntry(i) + stplen * s.getEntry(i)); } // Computing MAX final double d1 = stplen * (ggsav - HALF * stplen * shs); sdec = Math.max(d1, ZERO); qred += sdec; } // Restart the conjugate gradient method if it has hit a new bound. if (iact >= 0) { ++nact; xbdi.setEntry(iact, ONE); if (s.getEntry(iact) < ZERO) { xbdi.setEntry(iact, MINUS_ONE); } // Computing 2nd power final double d1 = trialStepPoint.getEntry(iact); delsq -= d1 * d1; if (delsq <= ZERO) { state = 190; break; } state = 20; break; } // If STPLEN is less than BLEN, then either apply another conjugate // gradient iteration or RETURN. if (stplen < blen) { if (iterc == itermax) { state = 190; break; } if (sdec <= qred * .01) { state = 190; break; } beta = gredsq / ggsav; state = 30; break; } } case 90: { printState(90); // XXX crvmin = ZERO; // Prepare for the alternative iteration by calculating some scalars // and by multiplying the reduced D by the second derivative matrix of // Q, where S holds the reduced D in the call of GGMULT. } case 100: { printState(100); // XXX if (nact >= n - 1) { state = 190; break; } dredsq = ZERO; dredg = ZERO; gredsq = ZERO; for (int i = 0; i < n; i++) { if (xbdi.getEntry(i) == ZERO) { // Computing 2nd power double d1 = trialStepPoint.getEntry(i); dredsq += d1 * d1; dredg += trialStepPoint.getEntry(i) * gnew.getEntry(i); // Computing 2nd power d1 = gnew.getEntry(i); gredsq += d1 * d1; s.setEntry(i, trialStepPoint.getEntry(i)); } else { s.setEntry(i, ZERO); } } itcsav = iterc; state = 210; break; // Let the search direction S be a linear combination of the reduced D // and the reduced G that is orthogonal to the reduced D. } case 120: { printState(120); // XXX ++iterc; temp = gredsq * dredsq - dredg * dredg; if (temp <= qred * 1e-4 * qred) { state = 190; break; } temp = Math.sqrt(temp); for (int i = 0; i < n; i++) { if (xbdi.getEntry(i) == ZERO) { s.setEntry(i, (dredg * trialStepPoint.getEntry(i) - dredsq * gnew.getEntry(i)) / temp); } else { s.setEntry(i, ZERO); } } sredg = -temp; // By considering the simple bounds on the variables, calculate an upper // bound on the tangent of half the angle of the alternative iteration, // namely ANGBD, except that, if already a free variable has reached a // bound, there is a branch back to label 100 after fixing that variable. angbd = ONE; iact = -1; for (int i = 0; i < n; i++) { if (xbdi.getEntry(i) == ZERO) { tempa = trustRegionCenterOffset.getEntry(i) + trialStepPoint.getEntry(i) - lowerDifference.getEntry(i); tempb = upperDifference.getEntry(i) - trustRegionCenterOffset.getEntry(i) - trialStepPoint.getEntry(i); if (tempa <= ZERO) { ++nact; xbdi.setEntry(i, MINUS_ONE); state = 100; break; } else if (tempb <= ZERO) { ++nact; xbdi.setEntry(i, ONE); state = 100; break; } // Computing 2nd power double d1 = trialStepPoint.getEntry(i); // Computing 2nd power double d2 = s.getEntry(i); ssq = d1 * d1 + d2 * d2; // Computing 2nd power d1 = trustRegionCenterOffset.getEntry(i) - lowerDifference.getEntry(i); temp = ssq - d1 * d1; if (temp > ZERO) { temp = Math.sqrt(temp) - s.getEntry(i); if (angbd * temp > tempa) { angbd = tempa / temp; iact = i; xsav = MINUS_ONE; } } // Computing 2nd power d1 = upperDifference.getEntry(i) - trustRegionCenterOffset.getEntry(i); temp = ssq - d1 * d1; if (temp > ZERO) { temp = Math.sqrt(temp) + s.getEntry(i); if (angbd * temp > tempb) { angbd = tempb / temp; iact = i; xsav = ONE; } } } } // Calculate HHD and some curvatures for the alternative iteration. state = 210; break; } case 150: { printState(150); // XXX shs = ZERO; dhs = ZERO; dhd = ZERO; for (int i = 0; i < n; i++) { if (xbdi.getEntry(i) == ZERO) { shs += s.getEntry(i) * hs.getEntry(i); dhs += trialStepPoint.getEntry(i) * hs.getEntry(i); dhd += trialStepPoint.getEntry(i) * hred.getEntry(i); } } // Seek the greatest reduction in Q for a range of equally spaced values // of ANGT in [0,ANGBD], where ANGT is the tangent of half the angle of // the alternative iteration. redmax = ZERO; isav = -1; redsav = ZERO; iu = (int) (angbd * 17. + 3.1); for (int i = 0; i < iu; i++) { angt = angbd * i / iu; sth = (angt + angt) / (ONE + angt * angt); temp = shs + angt * (angt * dhd - dhs - dhs); rednew = sth * (angt * dredg - sredg - HALF * sth * temp); if (rednew > redmax) { redmax = rednew; isav = i; rdprev = redsav; } else if (i == isav + 1) { rdnext = rednew; } redsav = rednew; } // Return if the reduction is zero. Otherwise, set the sine and cosine // of the angle of the alternative iteration, and calculate SDEC. if (isav < 0) { state = 190; break; } if (isav < iu) { temp = (rdnext - rdprev) / (redmax + redmax - rdprev - rdnext); angt = angbd * (isav + HALF * temp) / iu; } cth = (ONE - angt * angt) / (ONE + angt * angt); sth = (angt + angt) / (ONE + angt * angt); temp = shs + angt * (angt * dhd - dhs - dhs); sdec = sth * (angt * dredg - sredg - HALF * sth * temp); if (sdec <= ZERO) { state = 190; break; } // Update GNEW, D and HRED. If the angle of the alternative iteration // is restricted by a bound on a free variable, that variable is fixed // at the bound. dredg = ZERO; gredsq = ZERO; for (int i = 0; i < n; i++) { gnew.setEntry(i, gnew.getEntry(i) + (cth - ONE) * hred.getEntry(i) + sth * hs.getEntry(i)); if (xbdi.getEntry(i) == ZERO) { trialStepPoint.setEntry(i, cth * trialStepPoint.getEntry(i) + sth * s.getEntry(i)); dredg += trialStepPoint.getEntry(i) * gnew.getEntry(i); // Computing 2nd power final double d1 = gnew.getEntry(i); gredsq += d1 * d1; } hred.setEntry(i, cth * hred.getEntry(i) + sth * hs.getEntry(i)); } qred += sdec; if (iact >= 0 && isav == iu) { ++nact; xbdi.setEntry(iact, xsav); state = 100; break; } // If SDEC is sufficiently small, then RETURN after setting XNEW to // XOPT+D, giving careful attention to the bounds. if (sdec > qred * .01) { state = 120; break; } } case 190: { printState(190); // XXX dsq = ZERO; for (int i = 0; i < n; i++) { // Computing MAX // Computing MIN final double min = Math.min(trustRegionCenterOffset.getEntry(i) + trialStepPoint.getEntry(i), upperDifference.getEntry(i)); newPoint.setEntry(i, Math.max(min, lowerDifference.getEntry(i))); if (xbdi.getEntry(i) == MINUS_ONE) { newPoint.setEntry(i, lowerDifference.getEntry(i)); } if (xbdi.getEntry(i) == ONE) { newPoint.setEntry(i, upperDifference.getEntry(i)); } trialStepPoint.setEntry(i, newPoint.getEntry(i) - trustRegionCenterOffset.getEntry(i)); // Computing 2nd power final double d1 = trialStepPoint.getEntry(i); dsq += d1 * d1; } return new double[] { dsq, crvmin }; // The following instructions multiply the current S-vector by the second // derivative matrix of the quadratic model, putting the product in HS. // They are reached from three different parts of the software above and // they can be regarded as an external subroutine. } case 210: { printState(210); // XXX int ih = 0; for (int j = 0; j < n; j++) { hs.setEntry(j, ZERO); for (int i = 0; i <= j; i++) { if (i < j) { hs.setEntry(j, hs.getEntry(j) + modelSecondDerivativesValues.getEntry(ih) * s.getEntry(i)); } hs.setEntry(i, hs.getEntry(i) + modelSecondDerivativesValues.getEntry(ih) * s.getEntry(j)); ih++; } } final RealVector tmp = interpolationPoints.operate(s).ebeMultiply(modelSecondDerivativesParameters); for (int k = 0; k < npt; k++) { if (modelSecondDerivativesParameters.getEntry(k) != ZERO) { for (int i = 0; i < n; i++) { hs.setEntry(i, hs.getEntry(i) + tmp.getEntry(k) * interpolationPoints.getEntry(k, i)); } } } if (crvmin != ZERO) { state = 50; break; } if (iterc > itcsav) { state = 150; break; } for (int i = 0; i < n; i++) { hred.setEntry(i, hs.getEntry(i)); } state = 120; break; } default: { throw new MathIllegalStateException(LocalizedFormats.SIMPLE_MESSAGE, "trsbox"); }} } } // trsbox // ---------------------------------------------------------------------------------------- /** * The arrays BMAT and ZMAT are updated, as required by the new position * of the interpolation point that has the index KNEW. The vector VLAG has * N+NPT components, set on entry to the first NPT and last N components * of the product Hw in equation (4.11) of the Powell (2006) paper on * NEWUOA. Further, BETA is set on entry to the value of the parameter * with that name, and DENOM is set to the denominator of the updating * formula. Elements of ZMAT may be treated as zero if their moduli are * at most ZTEST. The first NDIM elements of W are used for working space. * @param beta * @param denom * @param knew */ private void update( double beta, double denom, int knew ) { printMethod(); // XXX final int n = currentBest.getDimension(); final int npt = numberOfInterpolationPoints; final int nptm = npt - n - 1; // XXX Should probably be split into two arrays. final ArrayRealVector work = new ArrayRealVector(npt + n); double ztest = ZERO; for (int k = 0; k < npt; k++) { for (int j = 0; j < nptm; j++) { // Computing MAX ztest = Math.max(ztest, Math.abs(zMatrix.getEntry(k, j))); } } ztest *= 1e-20; // Apply the rotations that put zeros in the KNEW-th row of ZMAT. for (int j = 1; j < nptm; j++) { final double d1 = zMatrix.getEntry(knew, j); if (Math.abs(d1) > ztest) { // Computing 2nd power final double d2 = zMatrix.getEntry(knew, 0); // Computing 2nd power final double d3 = zMatrix.getEntry(knew, j); final double d4 = Math.sqrt(d2 * d2 + d3 * d3); final double d5 = zMatrix.getEntry(knew, 0) / d4; final double d6 = zMatrix.getEntry(knew, j) / d4; for (int i = 0; i < npt; i++) { final double d7 = d5 * zMatrix.getEntry(i, 0) + d6 * zMatrix.getEntry(i, j); zMatrix.setEntry(i, j, d5 * zMatrix.getEntry(i, j) - d6 * zMatrix.getEntry(i, 0)); zMatrix.setEntry(i, 0, d7); } } zMatrix.setEntry(knew, j, ZERO); } // Put the first NPT components of the KNEW-th column of HLAG into W, // and calculate the parameters of the updating formula. for (int i = 0; i < npt; i++) { work.setEntry(i, zMatrix.getEntry(knew, 0) * zMatrix.getEntry(i, 0)); } final double alpha = work.getEntry(knew); final double tau = lagrangeValuesAtNewPoint.getEntry(knew); lagrangeValuesAtNewPoint.setEntry(knew, lagrangeValuesAtNewPoint.getEntry(knew) - ONE); // Complete the updating of ZMAT. final double sqrtDenom = Math.sqrt(denom); final double d1 = tau / sqrtDenom; final double d2 = zMatrix.getEntry(knew, 0) / sqrtDenom; for (int i = 0; i < npt; i++) { zMatrix.setEntry(i, 0, d1 * zMatrix.getEntry(i, 0) - d2 * lagrangeValuesAtNewPoint.getEntry(i)); } // Finally, update the matrix BMAT. for (int j = 0; j < n; j++) { final int jp = npt + j; work.setEntry(jp, bMatrix.getEntry(knew, j)); final double d3 = (alpha * lagrangeValuesAtNewPoint.getEntry(jp) - tau * work.getEntry(jp)) / denom; final double d4 = (-beta * work.getEntry(jp) - tau * lagrangeValuesAtNewPoint.getEntry(jp)) / denom; for (int i = 0; i <= jp; i++) { bMatrix.setEntry(i, j, bMatrix.getEntry(i, j) + d3 * lagrangeValuesAtNewPoint.getEntry(i) + d4 * work.getEntry(i)); if (i >= npt) { bMatrix.setEntry(jp, (i - npt), bMatrix.getEntry(i, j)); } } } } // update /** * Performs validity checks. * * @param lowerBound Lower bounds (constraints) of the objective variables. * @param upperBound Upperer bounds (constraints) of the objective variables. */ private void setup(double[] lowerBound, double[] upperBound) { printMethod(); // XXX double[] init = getStartPoint(); final int dimension = init.length; // Check problem dimension. if (dimension < MINIMUM_PROBLEM_DIMENSION) { throw new NumberIsTooSmallException(dimension, MINIMUM_PROBLEM_DIMENSION, true); } // Check number of interpolation points. final int[] nPointsInterval = { dimension + 2, (dimension + 2) * (dimension + 1) / 2 }; if (numberOfInterpolationPoints < nPointsInterval[0] || numberOfInterpolationPoints > nPointsInterval[1]) { throw new OutOfRangeException(LocalizedFormats.NUMBER_OF_INTERPOLATION_POINTS, numberOfInterpolationPoints, nPointsInterval[0], nPointsInterval[1]); } // Initialize bound differences. boundDifference = new double[dimension]; double requiredMinDiff = 2 * initialTrustRegionRadius; double minDiff = Double.POSITIVE_INFINITY; for (int i = 0; i < dimension; i++) { boundDifference[i] = upperBound[i] - lowerBound[i]; minDiff = Math.min(minDiff, boundDifference[i]); } if (minDiff < requiredMinDiff) { initialTrustRegionRadius = minDiff / 3.0; } // Initialize the data structures used by the "bobyqa" method. bMatrix = new Array2DRowRealMatrix(dimension + numberOfInterpolationPoints, dimension); zMatrix = new Array2DRowRealMatrix(numberOfInterpolationPoints, numberOfInterpolationPoints - dimension - 1); interpolationPoints = new Array2DRowRealMatrix(numberOfInterpolationPoints, dimension); originShift = new ArrayRealVector(dimension); fAtInterpolationPoints = new ArrayRealVector(numberOfInterpolationPoints); trustRegionCenterOffset = new ArrayRealVector(dimension); gradientAtTrustRegionCenter = new ArrayRealVector(dimension); lowerDifference = new ArrayRealVector(dimension); upperDifference = new ArrayRealVector(dimension); modelSecondDerivativesParameters = new ArrayRealVector(numberOfInterpolationPoints); newPoint = new ArrayRealVector(dimension); alternativeNewPoint = new ArrayRealVector(dimension); trialStepPoint = new ArrayRealVector(dimension); lagrangeValuesAtNewPoint = new ArrayRealVector(dimension + numberOfInterpolationPoints); modelSecondDerivativesValues = new ArrayRealVector(dimension * (dimension + 1) / 2); } // XXX utility for figuring out call sequence. private static String caller(int n) { final Throwable t = new Throwable(); final StackTraceElement[] elements = t.getStackTrace(); final StackTraceElement e = elements[n]; return e.getMethodName() + " (at line " + e.getLineNumber() + ")"; } // XXX utility for figuring out call sequence. private static void printState(int s) { // System.out.println(caller(2) + ": state " + s); } // XXX utility for figuring out call sequence. private static void printMethod() { // System.out.println(caller(2)); } /** * Marker for code paths that are not explored with the current unit tests. * If the path becomes explored, it should just be removed from the code. */ private static class PathIsExploredException extends RuntimeException { private static final long serialVersionUID = 745350979634801853L; private static final String PATH_IS_EXPLORED = "If this exception is thrown, just remove it from the code"; PathIsExploredException() { super(PATH_IS_EXPLORED + " " + BOBYQAOptimizer.caller(3)); } } } //CHECKSTYLE: resume all ././@LongLink100644 0 0 162 12126630646 10260 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/optim/nonlinear/scalar/noderiv/SimplexOptimizer.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/optim/nonlinear/scalar/noderiv/SimplexO100644 1750 1750 20764 12126627713 32267 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.optim.nonlinear.scalar.noderiv; import java.util.Comparator; import org.apache.commons.math3.analysis.MultivariateFunction; import org.apache.commons.math3.exception.NullArgumentException; import org.apache.commons.math3.exception.MathUnsupportedOperationException; import org.apache.commons.math3.exception.util.LocalizedFormats; import org.apache.commons.math3.optim.nonlinear.scalar.GoalType; import org.apache.commons.math3.optim.ConvergenceChecker; import org.apache.commons.math3.optim.PointValuePair; import org.apache.commons.math3.optim.SimpleValueChecker; import org.apache.commons.math3.optim.OptimizationData; import org.apache.commons.math3.optim.nonlinear.scalar.MultivariateOptimizer; /** * This class implements simplex-based direct search optimization. * *

                              * Direct search methods only use objective function values, they do * not need derivatives and don't either try to compute approximation * of the derivatives. According to a 1996 paper by Margaret H. Wright * (Direct * Search Methods: Once Scorned, Now Respectable), they are used * when either the computation of the derivative is impossible (noisy * functions, unpredictable discontinuities) or difficult (complexity, * computation cost). In the first cases, rather than an optimum, a * not too bad point is desired. In the latter cases, an * optimum is desired but cannot be reasonably found. In all cases * direct search methods can be useful. *

                              *

                              * Simplex-based direct search methods are based on comparison of * the objective function values at the vertices of a simplex (which is a * set of n+1 points in dimension n) that is updated by the algorithms * steps. *

                              *

                              * The simplex update procedure ({@link NelderMeadSimplex} or * {@link MultiDirectionalSimplex}) must be passed to the * {@code optimize} method. *

                              *

                              * Each call to {@code optimize} will re-use the start configuration of * the current simplex and move it such that its first vertex is at the * provided start point of the optimization. * If the {@code optimize} method is called to solve a different problem * and the number of parameters change, the simplex must be re-initialized * to one with the appropriate dimensions. *

                              *

                              * Convergence is checked by providing the worst points of * previous and current simplex to the convergence checker, not the best * ones. *

                              *

                              * This simplex optimizer implementation does not directly support constrained * optimization with simple bounds; so, for such optimizations, either a more * dedicated algorithm must be used like * {@link CMAESOptimizer} or {@link BOBYQAOptimizer}, or the objective * function must be wrapped in an adapter like * {@link org.apache.commons.math3.optim.nonlinear.scalar.MultivariateFunctionMappingAdapter * MultivariateFunctionMappingAdapter} or * {@link org.apache.commons.math3.optim.nonlinear.scalar.MultivariateFunctionPenaltyAdapter * MultivariateFunctionPenaltyAdapter}. *
                              * The call to {@link #optimize(OptimizationData[]) optimize} will throw * {@link MathUnsupportedOperationException} if bounds are passed to it. *

                              * * @version $Id: SimplexOptimizer.java 1458323 2013-03-19 14:51:30Z erans $ * @since 3.0 */ public class SimplexOptimizer extends MultivariateOptimizer { /** Simplex update rule. */ private AbstractSimplex simplex; /** * @param checker Convergence checker. */ public SimplexOptimizer(ConvergenceChecker checker) { super(checker); } /** * @param rel Relative threshold. * @param abs Absolute threshold. */ public SimplexOptimizer(double rel, double abs) { this(new SimpleValueChecker(rel, abs)); } /** * {@inheritDoc} * * @param optData Optimization data. In addition to those documented in * {@link MultivariateOptimizer#parseOptimizationData(OptimizationData[]) * MultivariateOptimizer}, this method will register the following data: *
                                *
                              • {@link AbstractSimplex}
                              • *
                              * @return {@inheritDoc} */ @Override public PointValuePair optimize(OptimizationData... optData) { // Set up base class and perform computation. return super.optimize(optData); } /** {@inheritDoc} */ @Override protected PointValuePair doOptimize() { checkParameters(); // Indirect call to "computeObjectiveValue" in order to update the // evaluations counter. final MultivariateFunction evalFunc = new MultivariateFunction() { public double value(double[] point) { return computeObjectiveValue(point); } }; final boolean isMinim = getGoalType() == GoalType.MINIMIZE; final Comparator comparator = new Comparator() { public int compare(final PointValuePair o1, final PointValuePair o2) { final double v1 = o1.getValue(); final double v2 = o2.getValue(); return isMinim ? Double.compare(v1, v2) : Double.compare(v2, v1); } }; // Initialize search. simplex.build(getStartPoint()); simplex.evaluate(evalFunc, comparator); PointValuePair[] previous = null; int iteration = 0; final ConvergenceChecker checker = getConvergenceChecker(); while (true) { if (getIterations() > 0) { boolean converged = true; for (int i = 0; i < simplex.getSize(); i++) { PointValuePair prev = previous[i]; converged = converged && checker.converged(iteration, prev, simplex.getPoint(i)); } if (converged) { // We have found an optimum. return simplex.getPoint(0); } } // We still need to search. previous = simplex.getPoints(); simplex.iterate(evalFunc, comparator); incrementIterationCount(); } } /** * Scans the list of (required and optional) optimization data that * characterize the problem. * * @param optData Optimization data. * The following data will be looked for: *
                                *
                              • {@link AbstractSimplex}
                              • *
                              */ @Override protected void parseOptimizationData(OptimizationData... optData) { // Allow base class to register its own data. super.parseOptimizationData(optData); // The existing values (as set by the previous call) are reused if // not provided in the argument list. for (OptimizationData data : optData) { if (data instanceof AbstractSimplex) { simplex = (AbstractSimplex) data; // If more data must be parsed, this statement _must_ be // changed to "continue". break; } } } /** * @throws MathUnsupportedOperationException if bounds were passed to the * {@link #optimize(OptimizationData[]) optimize} method. * @throws NullArgumentException if no initial simplex was passed to the * {@link #optimize(OptimizationData[]) optimize} method. */ private void checkParameters() { if (simplex == null) { throw new NullArgumentException(); } if (getLowerBound() != null || getUpperBound() != null) { throw new MathUnsupportedOperationException(LocalizedFormats.CONSTRAINT); } } } ././@LongLink100644 0 0 160 12126630646 10256 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/optim/nonlinear/scalar/noderiv/CMAESOptimizer.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/optim/nonlinear/scalar/noderiv/CMAESOpt100644 1750 1750 143053 12126627713 32057 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.optim.nonlinear.scalar.noderiv; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import org.apache.commons.math3.exception.DimensionMismatchException; import org.apache.commons.math3.exception.NotPositiveException; import org.apache.commons.math3.exception.NotStrictlyPositiveException; import org.apache.commons.math3.exception.OutOfRangeException; import org.apache.commons.math3.exception.TooManyEvaluationsException; import org.apache.commons.math3.linear.Array2DRowRealMatrix; import org.apache.commons.math3.linear.EigenDecomposition; import org.apache.commons.math3.linear.MatrixUtils; import org.apache.commons.math3.linear.RealMatrix; import org.apache.commons.math3.optim.ConvergenceChecker; import org.apache.commons.math3.optim.OptimizationData; import org.apache.commons.math3.optim.nonlinear.scalar.GoalType; import org.apache.commons.math3.optim.PointValuePair; import org.apache.commons.math3.optim.nonlinear.scalar.MultivariateOptimizer; import org.apache.commons.math3.random.RandomGenerator; import org.apache.commons.math3.util.MathArrays; /** *

                              An implementation of the active Covariance Matrix Adaptation Evolution Strategy (CMA-ES) * for non-linear, non-convex, non-smooth, global function minimization. * The CMA-Evolution Strategy (CMA-ES) is a reliable stochastic optimization method * which should be applied if derivative-based methods, e.g. quasi-Newton BFGS or * conjugate gradient, fail due to a rugged search landscape (e.g. noise, local * optima, outlier, etc.) of the objective function. Like a * quasi-Newton method, the CMA-ES learns and applies a variable metric * on the underlying search space. Unlike a quasi-Newton method, the * CMA-ES neither estimates nor uses gradients, making it considerably more * reliable in terms of finding a good, or even close to optimal, solution.

                              * *

                              In general, on smooth objective functions the CMA-ES is roughly ten times * slower than BFGS (counting objective function evaluations, no gradients provided). * For up to N=10 variables also the derivative-free simplex * direct search method (Nelder and Mead) can be faster, but it is * far less reliable than CMA-ES.

                              * *

                              The CMA-ES is particularly well suited for non-separable * and/or badly conditioned problems. To observe the advantage of CMA compared * to a conventional evolution strategy, it will usually take about * 30 N function evaluations. On difficult problems the complete * optimization (a single run) is expected to take roughly between * 30 N and 300 N2 * function evaluations.

                              * *

                              This implementation is translated and adapted from the Matlab version * of the CMA-ES algorithm as implemented in module {@code cmaes.m} version 3.51.

                              * * For more information, please refer to the following links: * * * @version $Id: CMAESOptimizer.java 1462503 2013-03-29 15:48:27Z luc $ * @since 3.0 */ public class CMAESOptimizer extends MultivariateOptimizer { // global search parameters /** * Population size, offspring number. The primary strategy parameter to play * with, which can be increased from its default value. Increasing the * population size improves global search properties in exchange to speed. * Speed decreases, as a rule, at most linearly with increasing population * size. It is advisable to begin with the default small population size. */ private int lambda; // population size /** * Covariance update mechanism, default is active CMA. isActiveCMA = true * turns on "active CMA" with a negative update of the covariance matrix and * checks for positive definiteness. OPTS.CMA.active = 2 does not check for * pos. def. and is numerically faster. Active CMA usually speeds up the * adaptation. */ private final boolean isActiveCMA; /** * Determines how often a new random offspring is generated in case it is * not feasible / beyond the defined limits, default is 0. */ private final int checkFeasableCount; /** * @see Sigma */ private double[] inputSigma; /** Number of objective variables/problem dimension */ private int dimension; /** * Defines the number of initial iterations, where the covariance matrix * remains diagonal and the algorithm has internally linear time complexity. * diagonalOnly = 1 means keeping the covariance matrix always diagonal and * this setting also exhibits linear space complexity. This can be * particularly useful for dimension > 100. * @see A Simple Modification in CMA-ES */ private int diagonalOnly; /** Number of objective variables/problem dimension */ private boolean isMinimize = true; /** Indicates whether statistic data is collected. */ private final boolean generateStatistics; // termination criteria /** Maximal number of iterations allowed. */ private final int maxIterations; /** Limit for fitness value. */ private final double stopFitness; /** Stop if x-changes larger stopTolUpX. */ private double stopTolUpX; /** Stop if x-change smaller stopTolX. */ private double stopTolX; /** Stop if fun-changes smaller stopTolFun. */ private double stopTolFun; /** Stop if back fun-changes smaller stopTolHistFun. */ private double stopTolHistFun; // selection strategy parameters /** Number of parents/points for recombination. */ private int mu; // /** log(mu + 0.5), stored for efficiency. */ private double logMu2; /** Array for weighted recombination. */ private RealMatrix weights; /** Variance-effectiveness of sum w_i x_i. */ private double mueff; // // dynamic strategy parameters and constants /** Overall standard deviation - search volume. */ private double sigma; /** Cumulation constant. */ private double cc; /** Cumulation constant for step-size. */ private double cs; /** Damping for step-size. */ private double damps; /** Learning rate for rank-one update. */ private double ccov1; /** Learning rate for rank-mu update' */ private double ccovmu; /** Expectation of ||N(0,I)|| == norm(randn(N,1)). */ private double chiN; /** Learning rate for rank-one update - diagonalOnly */ private double ccov1Sep; /** Learning rate for rank-mu update - diagonalOnly */ private double ccovmuSep; // CMA internal values - updated each generation /** Objective variables. */ private RealMatrix xmean; /** Evolution path. */ private RealMatrix pc; /** Evolution path for sigma. */ private RealMatrix ps; /** Norm of ps, stored for efficiency. */ private double normps; /** Coordinate system. */ private RealMatrix B; /** Scaling. */ private RealMatrix D; /** B*D, stored for efficiency. */ private RealMatrix BD; /** Diagonal of sqrt(D), stored for efficiency. */ private RealMatrix diagD; /** Covariance matrix. */ private RealMatrix C; /** Diagonal of C, used for diagonalOnly. */ private RealMatrix diagC; /** Number of iterations already performed. */ private int iterations; /** History queue of best values. */ private double[] fitnessHistory; /** Size of history queue of best values. */ private int historySize; /** Random generator. */ private final RandomGenerator random; /** History of sigma values. */ private final List statisticsSigmaHistory = new ArrayList(); /** History of mean matrix. */ private final List statisticsMeanHistory = new ArrayList(); /** History of fitness values. */ private final List statisticsFitnessHistory = new ArrayList(); /** History of D matrix. */ private final List statisticsDHistory = new ArrayList(); /** * @param maxIterations Maximal number of iterations. * @param stopFitness Whether to stop if objective function value is smaller than * {@code stopFitness}. * @param isActiveCMA Chooses the covariance matrix update method. * @param diagonalOnly Number of initial iterations, where the covariance matrix * remains diagonal. * @param checkFeasableCount Determines how often new random objective variables are * generated in case they are out of bounds. * @param random Random generator. * @param generateStatistics Whether statistic data is collected. * @param checker Convergence checker. * * @since 3.1 */ public CMAESOptimizer(int maxIterations, double stopFitness, boolean isActiveCMA, int diagonalOnly, int checkFeasableCount, RandomGenerator random, boolean generateStatistics, ConvergenceChecker checker) { super(checker); this.maxIterations = maxIterations; this.stopFitness = stopFitness; this.isActiveCMA = isActiveCMA; this.diagonalOnly = diagonalOnly; this.checkFeasableCount = checkFeasableCount; this.random = random; this.generateStatistics = generateStatistics; } /** * @return History of sigma values. */ public List getStatisticsSigmaHistory() { return statisticsSigmaHistory; } /** * @return History of mean matrix. */ public List getStatisticsMeanHistory() { return statisticsMeanHistory; } /** * @return History of fitness values. */ public List getStatisticsFitnessHistory() { return statisticsFitnessHistory; } /** * @return History of D matrix. */ public List getStatisticsDHistory() { return statisticsDHistory; } /** * Input sigma values. * They define the initial coordinate-wise standard deviations for * sampling new search points around the initial guess. * It is suggested to set them to the estimated distance from the * initial to the desired optimum. * Small values induce the search to be more local (and very small * values are more likely to find a local optimum close to the initial * guess). * Too small values might however lead to early termination. */ public static class Sigma implements OptimizationData { /** Sigma values. */ private final double[] sigma; /** * @param s Sigma values. * @throws NotPositiveException if any of the array entries is smaller * than zero. */ public Sigma(double[] s) throws NotPositiveException { for (int i = 0; i < s.length; i++) { if (s[i] < 0) { throw new NotPositiveException(s[i]); } } sigma = s.clone(); } /** * @return the sigma values. */ public double[] getSigma() { return sigma.clone(); } } /** * Population size. * The number of offspring is the primary strategy parameter. * In the absence of better clues, a good default could be an * integer close to {@code 4 + 3 ln(n)}, where {@code n} is the * number of optimized parameters. * Increasing the population size improves global search properties * at the expense of speed (which in general decreases at most * linearly with increasing population size). */ public static class PopulationSize implements OptimizationData { /** Population size. */ private final int lambda; /** * @param size Population size. * @throws NotStrictlyPositiveException if {@code size <= 0}. */ public PopulationSize(int size) throws NotStrictlyPositiveException { if (size <= 0) { throw new NotStrictlyPositiveException(size); } lambda = size; } /** * @return the population size. */ public int getPopulationSize() { return lambda; } } /** * {@inheritDoc} * * @param optData Optimization data. In addition to those documented in * {@link MultivariateOptimizer#parseOptimizationData(OptimizationData[]) * MultivariateOptimizer}, this method will register the following data: *
                                *
                              • {@link Sigma}
                              • *
                              • {@link PopulationSize}
                              • *
                              * @return {@inheritDoc} * @throws TooManyEvaluationsException if the maximal number of * evaluations is exceeded. * @throws DimensionMismatchException if the initial guess, target, and weight * arguments have inconsistent dimensions. */ @Override public PointValuePair optimize(OptimizationData... optData) throws TooManyEvaluationsException, DimensionMismatchException { // Set up base class and perform computation. return super.optimize(optData); } /** {@inheritDoc} */ @Override protected PointValuePair doOptimize() { // -------------------- Initialization -------------------------------- isMinimize = getGoalType().equals(GoalType.MINIMIZE); final FitnessFunction fitfun = new FitnessFunction(); final double[] guess = getStartPoint(); // number of objective variables/problem dimension dimension = guess.length; initializeCMA(guess); iterations = 0; double bestValue = fitfun.value(guess); push(fitnessHistory, bestValue); PointValuePair optimum = new PointValuePair(getStartPoint(), isMinimize ? bestValue : -bestValue); PointValuePair lastResult = null; // -------------------- Generation Loop -------------------------------- generationLoop: for (iterations = 1; iterations <= maxIterations; iterations++) { incrementIterationCount(); // Generate and evaluate lambda offspring final RealMatrix arz = randn1(dimension, lambda); final RealMatrix arx = zeros(dimension, lambda); final double[] fitness = new double[lambda]; // generate random offspring for (int k = 0; k < lambda; k++) { RealMatrix arxk = null; for (int i = 0; i < checkFeasableCount + 1; i++) { if (diagonalOnly <= 0) { arxk = xmean.add(BD.multiply(arz.getColumnMatrix(k)) .scalarMultiply(sigma)); // m + sig * Normal(0,C) } else { arxk = xmean.add(times(diagD,arz.getColumnMatrix(k)) .scalarMultiply(sigma)); } if (i >= checkFeasableCount || fitfun.isFeasible(arxk.getColumn(0))) { break; } // regenerate random arguments for row arz.setColumn(k, randn(dimension)); } copyColumn(arxk, 0, arx, k); try { fitness[k] = fitfun.value(arx.getColumn(k)); // compute fitness } catch (TooManyEvaluationsException e) { break generationLoop; } } // Sort by fitness and compute weighted mean into xmean final int[] arindex = sortedIndices(fitness); // Calculate new xmean, this is selection and recombination final RealMatrix xold = xmean; // for speed up of Eq. (2) and (3) final RealMatrix bestArx = selectColumns(arx, MathArrays.copyOf(arindex, mu)); xmean = bestArx.multiply(weights); final RealMatrix bestArz = selectColumns(arz, MathArrays.copyOf(arindex, mu)); final RealMatrix zmean = bestArz.multiply(weights); final boolean hsig = updateEvolutionPaths(zmean, xold); if (diagonalOnly <= 0) { updateCovariance(hsig, bestArx, arz, arindex, xold); } else { updateCovarianceDiagonalOnly(hsig, bestArz); } // Adapt step size sigma - Eq. (5) sigma *= Math.exp(Math.min(1, (normps/chiN - 1) * cs / damps)); final double bestFitness = fitness[arindex[0]]; final double worstFitness = fitness[arindex[arindex.length - 1]]; if (bestValue > bestFitness) { bestValue = bestFitness; lastResult = optimum; optimum = new PointValuePair(fitfun.repair(bestArx.getColumn(0)), isMinimize ? bestFitness : -bestFitness); if (getConvergenceChecker() != null && lastResult != null && getConvergenceChecker().converged(iterations, optimum, lastResult)) { break generationLoop; } } // handle termination criteria // Break, if fitness is good enough if (stopFitness != 0 && bestFitness < (isMinimize ? stopFitness : -stopFitness)) { break generationLoop; } final double[] sqrtDiagC = sqrt(diagC).getColumn(0); final double[] pcCol = pc.getColumn(0); for (int i = 0; i < dimension; i++) { if (sigma * Math.max(Math.abs(pcCol[i]), sqrtDiagC[i]) > stopTolX) { break; } if (i >= dimension - 1) { break generationLoop; } } for (int i = 0; i < dimension; i++) { if (sigma * sqrtDiagC[i] > stopTolUpX) { break generationLoop; } } final double historyBest = min(fitnessHistory); final double historyWorst = max(fitnessHistory); if (iterations > 2 && Math.max(historyWorst, worstFitness) - Math.min(historyBest, bestFitness) < stopTolFun) { break generationLoop; } if (iterations > fitnessHistory.length && historyWorst - historyBest < stopTolHistFun) { break generationLoop; } // condition number of the covariance matrix exceeds 1e14 if (max(diagD) / min(diagD) > 1e7) { break generationLoop; } // user defined termination if (getConvergenceChecker() != null) { final PointValuePair current = new PointValuePair(bestArx.getColumn(0), isMinimize ? bestFitness : -bestFitness); if (lastResult != null && getConvergenceChecker().converged(iterations, current, lastResult)) { break generationLoop; } lastResult = current; } // Adjust step size in case of equal function values (flat fitness) if (bestValue == fitness[arindex[(int)(0.1+lambda/4.)]]) { sigma = sigma * Math.exp(0.2 + cs / damps); } if (iterations > 2 && Math.max(historyWorst, bestFitness) - Math.min(historyBest, bestFitness) == 0) { sigma = sigma * Math.exp(0.2 + cs / damps); } // store best in history push(fitnessHistory,bestFitness); fitfun.setValueRange(worstFitness-bestFitness); if (generateStatistics) { statisticsSigmaHistory.add(sigma); statisticsFitnessHistory.add(bestFitness); statisticsMeanHistory.add(xmean.transpose()); statisticsDHistory.add(diagD.transpose().scalarMultiply(1E5)); } } return optimum; } /** * Scans the list of (required and optional) optimization data that * characterize the problem. * * @param optData Optimization data. The following data will be looked for: *
                                *
                              • {@link Sigma}
                              • *
                              • {@link PopulationSize}
                              • *
                              */ @Override protected void parseOptimizationData(OptimizationData... optData) { // Allow base class to register its own data. super.parseOptimizationData(optData); // The existing values (as set by the previous call) are reused if // not provided in the argument list. for (OptimizationData data : optData) { if (data instanceof Sigma) { inputSigma = ((Sigma) data).getSigma(); continue; } if (data instanceof PopulationSize) { lambda = ((PopulationSize) data).getPopulationSize(); continue; } } checkParameters(); } /** * Checks dimensions and values of boundaries and inputSigma if defined. */ private void checkParameters() { final double[] init = getStartPoint(); final double[] lB = getLowerBound(); final double[] uB = getUpperBound(); if (inputSigma != null) { if (inputSigma.length != init.length) { throw new DimensionMismatchException(inputSigma.length, init.length); } for (int i = 0; i < init.length; i++) { if (inputSigma[i] > uB[i] - lB[i]) { throw new OutOfRangeException(inputSigma[i], 0, uB[i] - lB[i]); } } } } /** * Initialization of the dynamic search parameters * * @param guess Initial guess for the arguments of the fitness function. */ private void initializeCMA(double[] guess) { if (lambda <= 0) { throw new NotStrictlyPositiveException(lambda); } // initialize sigma final double[][] sigmaArray = new double[guess.length][1]; for (int i = 0; i < guess.length; i++) { sigmaArray[i][0] = inputSigma[i]; } final RealMatrix insigma = new Array2DRowRealMatrix(sigmaArray, false); sigma = max(insigma); // overall standard deviation // initialize termination criteria stopTolUpX = 1e3 * max(insigma); stopTolX = 1e-11 * max(insigma); stopTolFun = 1e-12; stopTolHistFun = 1e-13; // initialize selection strategy parameters mu = lambda / 2; // number of parents/points for recombination logMu2 = Math.log(mu + 0.5); weights = log(sequence(1, mu, 1)).scalarMultiply(-1).scalarAdd(logMu2); double sumw = 0; double sumwq = 0; for (int i = 0; i < mu; i++) { double w = weights.getEntry(i, 0); sumw += w; sumwq += w * w; } weights = weights.scalarMultiply(1 / sumw); mueff = sumw * sumw / sumwq; // variance-effectiveness of sum w_i x_i // initialize dynamic strategy parameters and constants cc = (4 + mueff / dimension) / (dimension + 4 + 2 * mueff / dimension); cs = (mueff + 2) / (dimension + mueff + 3.); damps = (1 + 2 * Math.max(0, Math.sqrt((mueff - 1) / (dimension + 1)) - 1)) * Math.max(0.3, 1 - dimension / (1e-6 + maxIterations)) + cs; // minor increment ccov1 = 2 / ((dimension + 1.3) * (dimension + 1.3) + mueff); ccovmu = Math.min(1 - ccov1, 2 * (mueff - 2 + 1 / mueff) / ((dimension + 2) * (dimension + 2) + mueff)); ccov1Sep = Math.min(1, ccov1 * (dimension + 1.5) / 3); ccovmuSep = Math.min(1 - ccov1, ccovmu * (dimension + 1.5) / 3); chiN = Math.sqrt(dimension) * (1 - 1 / ((double) 4 * dimension) + 1 / ((double) 21 * dimension * dimension)); // intialize CMA internal values - updated each generation xmean = MatrixUtils.createColumnRealMatrix(guess); // objective variables diagD = insigma.scalarMultiply(1 / sigma); diagC = square(diagD); pc = zeros(dimension, 1); // evolution paths for C and sigma ps = zeros(dimension, 1); // B defines the coordinate system normps = ps.getFrobeniusNorm(); B = eye(dimension, dimension); D = ones(dimension, 1); // diagonal D defines the scaling BD = times(B, repmat(diagD.transpose(), dimension, 1)); C = B.multiply(diag(square(D)).multiply(B.transpose())); // covariance historySize = 10 + (int) (3 * 10 * dimension / (double) lambda); fitnessHistory = new double[historySize]; // history of fitness values for (int i = 0; i < historySize; i++) { fitnessHistory[i] = Double.MAX_VALUE; } } /** * Update of the evolution paths ps and pc. * * @param zmean Weighted row matrix of the gaussian random numbers generating * the current offspring. * @param xold xmean matrix of the previous generation. * @return hsig flag indicating a small correction. */ private boolean updateEvolutionPaths(RealMatrix zmean, RealMatrix xold) { ps = ps.scalarMultiply(1 - cs).add( B.multiply(zmean).scalarMultiply( Math.sqrt(cs * (2 - cs) * mueff))); normps = ps.getFrobeniusNorm(); final boolean hsig = normps / Math.sqrt(1 - Math.pow(1 - cs, 2 * iterations)) / chiN < 1.4 + 2 / ((double) dimension + 1); pc = pc.scalarMultiply(1 - cc); if (hsig) { pc = pc.add(xmean.subtract(xold).scalarMultiply(Math.sqrt(cc * (2 - cc) * mueff) / sigma)); } return hsig; } /** * Update of the covariance matrix C for diagonalOnly > 0 * * @param hsig Flag indicating a small correction. * @param bestArz Fitness-sorted matrix of the gaussian random values of the * current offspring. */ private void updateCovarianceDiagonalOnly(boolean hsig, final RealMatrix bestArz) { // minor correction if hsig==false double oldFac = hsig ? 0 : ccov1Sep * cc * (2 - cc); oldFac += 1 - ccov1Sep - ccovmuSep; diagC = diagC.scalarMultiply(oldFac) // regard old matrix .add(square(pc).scalarMultiply(ccov1Sep)) // plus rank one update .add((times(diagC, square(bestArz).multiply(weights))) // plus rank mu update .scalarMultiply(ccovmuSep)); diagD = sqrt(diagC); // replaces eig(C) if (diagonalOnly > 1 && iterations > diagonalOnly) { // full covariance matrix from now on diagonalOnly = 0; B = eye(dimension, dimension); BD = diag(diagD); C = diag(diagC); } } /** * Update of the covariance matrix C. * * @param hsig Flag indicating a small correction. * @param bestArx Fitness-sorted matrix of the argument vectors producing the * current offspring. * @param arz Unsorted matrix containing the gaussian random values of the * current offspring. * @param arindex Indices indicating the fitness-order of the current offspring. * @param xold xmean matrix of the previous generation. */ private void updateCovariance(boolean hsig, final RealMatrix bestArx, final RealMatrix arz, final int[] arindex, final RealMatrix xold) { double negccov = 0; if (ccov1 + ccovmu > 0) { final RealMatrix arpos = bestArx.subtract(repmat(xold, 1, mu)) .scalarMultiply(1 / sigma); // mu difference vectors final RealMatrix roneu = pc.multiply(pc.transpose()) .scalarMultiply(ccov1); // rank one update // minor correction if hsig==false double oldFac = hsig ? 0 : ccov1 * cc * (2 - cc); oldFac += 1 - ccov1 - ccovmu; if (isActiveCMA) { // Adapt covariance matrix C active CMA negccov = (1 - ccovmu) * 0.25 * mueff / (Math.pow(dimension + 2, 1.5) + 2 * mueff); // keep at least 0.66 in all directions, small popsize are most // critical final double negminresidualvariance = 0.66; // where to make up for the variance loss final double negalphaold = 0.5; // prepare vectors, compute negative updating matrix Cneg final int[] arReverseIndex = reverse(arindex); RealMatrix arzneg = selectColumns(arz, MathArrays.copyOf(arReverseIndex, mu)); RealMatrix arnorms = sqrt(sumRows(square(arzneg))); final int[] idxnorms = sortedIndices(arnorms.getRow(0)); final RealMatrix arnormsSorted = selectColumns(arnorms, idxnorms); final int[] idxReverse = reverse(idxnorms); final RealMatrix arnormsReverse = selectColumns(arnorms, idxReverse); arnorms = divide(arnormsReverse, arnormsSorted); final int[] idxInv = inverse(idxnorms); final RealMatrix arnormsInv = selectColumns(arnorms, idxInv); // check and set learning rate negccov final double negcovMax = (1 - negminresidualvariance) / square(arnormsInv).multiply(weights).getEntry(0, 0); if (negccov > negcovMax) { negccov = negcovMax; } arzneg = times(arzneg, repmat(arnormsInv, dimension, 1)); final RealMatrix artmp = BD.multiply(arzneg); final RealMatrix Cneg = artmp.multiply(diag(weights)).multiply(artmp.transpose()); oldFac += negalphaold * negccov; C = C.scalarMultiply(oldFac) .add(roneu) // regard old matrix .add(arpos.scalarMultiply( // plus rank one update ccovmu + (1 - negalphaold) * negccov) // plus rank mu update .multiply(times(repmat(weights, 1, dimension), arpos.transpose()))) .subtract(Cneg.scalarMultiply(negccov)); } else { // Adapt covariance matrix C - nonactive C = C.scalarMultiply(oldFac) // regard old matrix .add(roneu) // plus rank one update .add(arpos.scalarMultiply(ccovmu) // plus rank mu update .multiply(times(repmat(weights, 1, dimension), arpos.transpose()))); } } updateBD(negccov); } /** * Update B and D from C. * * @param negccov Negative covariance factor. */ private void updateBD(double negccov) { if (ccov1 + ccovmu + negccov > 0 && (iterations % 1. / (ccov1 + ccovmu + negccov) / dimension / 10.) < 1) { // to achieve O(N^2) C = triu(C, 0).add(triu(C, 1).transpose()); // enforce symmetry to prevent complex numbers final EigenDecomposition eig = new EigenDecomposition(C); B = eig.getV(); // eigen decomposition, B==normalized eigenvectors D = eig.getD(); diagD = diag(D); if (min(diagD) <= 0) { for (int i = 0; i < dimension; i++) { if (diagD.getEntry(i, 0) < 0) { diagD.setEntry(i, 0, 0); } } final double tfac = max(diagD) / 1e14; C = C.add(eye(dimension, dimension).scalarMultiply(tfac)); diagD = diagD.add(ones(dimension, 1).scalarMultiply(tfac)); } if (max(diagD) > 1e14 * min(diagD)) { final double tfac = max(diagD) / 1e14 - min(diagD); C = C.add(eye(dimension, dimension).scalarMultiply(tfac)); diagD = diagD.add(ones(dimension, 1).scalarMultiply(tfac)); } diagC = diag(C); diagD = sqrt(diagD); // D contains standard deviations now BD = times(B, repmat(diagD.transpose(), dimension, 1)); // O(n^2) } } /** * Pushes the current best fitness value in a history queue. * * @param vals History queue. * @param val Current best fitness value. */ private static void push(double[] vals, double val) { for (int i = vals.length-1; i > 0; i--) { vals[i] = vals[i-1]; } vals[0] = val; } /** * Sorts fitness values. * * @param doubles Array of values to be sorted. * @return a sorted array of indices pointing into doubles. */ private int[] sortedIndices(final double[] doubles) { final DoubleIndex[] dis = new DoubleIndex[doubles.length]; for (int i = 0; i < doubles.length; i++) { dis[i] = new DoubleIndex(doubles[i], i); } Arrays.sort(dis); final int[] indices = new int[doubles.length]; for (int i = 0; i < doubles.length; i++) { indices[i] = dis[i].index; } return indices; } /** * Used to sort fitness values. Sorting is always in lower value first * order. */ private static class DoubleIndex implements Comparable { /** Value to compare. */ private final double value; /** Index into sorted array. */ private final int index; /** * @param value Value to compare. * @param index Index into sorted array. */ DoubleIndex(double value, int index) { this.value = value; this.index = index; } /** {@inheritDoc} */ public int compareTo(DoubleIndex o) { return Double.compare(value, o.value); } /** {@inheritDoc} */ @Override public boolean equals(Object other) { if (this == other) { return true; } if (other instanceof DoubleIndex) { return Double.compare(value, ((DoubleIndex) other).value) == 0; } return false; } /** {@inheritDoc} */ @Override public int hashCode() { long bits = Double.doubleToLongBits(value); return (int) ((1438542 ^ (bits >>> 32) ^ bits) & 0xffffffff); } } /** * Normalizes fitness values to the range [0,1]. Adds a penalty to the * fitness value if out of range. The penalty is adjusted by calling * setValueRange(). */ private class FitnessFunction { /** Determines the penalty for boundary violations */ private double valueRange; /** * Flag indicating whether the objective variables are forced into their * bounds if defined */ private final boolean isRepairMode; /** Simple constructor. */ public FitnessFunction() { valueRange = 1; isRepairMode = true; } /** * @param point Normalized objective variables. * @return the objective value + penalty for violated bounds. */ public double value(final double[] point) { double value; if (isRepairMode) { double[] repaired = repair(point); value = CMAESOptimizer.this.computeObjectiveValue(repaired) + penalty(point, repaired); } else { value = CMAESOptimizer.this.computeObjectiveValue(point); } return isMinimize ? value : -value; } /** * @param x Normalized objective variables. * @return {@code true} if in bounds. */ public boolean isFeasible(final double[] x) { final double[] lB = CMAESOptimizer.this.getLowerBound(); final double[] uB = CMAESOptimizer.this.getUpperBound(); for (int i = 0; i < x.length; i++) { if (x[i] < lB[i]) { return false; } if (x[i] > uB[i]) { return false; } } return true; } /** * @param valueRange Adjusts the penalty computation. */ public void setValueRange(double valueRange) { this.valueRange = valueRange; } /** * @param x Normalized objective variables. * @return the repaired (i.e. all in bounds) objective variables. */ private double[] repair(final double[] x) { final double[] lB = CMAESOptimizer.this.getLowerBound(); final double[] uB = CMAESOptimizer.this.getUpperBound(); final double[] repaired = new double[x.length]; for (int i = 0; i < x.length; i++) { if (x[i] < lB[i]) { repaired[i] = lB[i]; } else if (x[i] > uB[i]) { repaired[i] = uB[i]; } else { repaired[i] = x[i]; } } return repaired; } /** * @param x Normalized objective variables. * @param repaired Repaired objective variables. * @return Penalty value according to the violation of the bounds. */ private double penalty(final double[] x, final double[] repaired) { double penalty = 0; for (int i = 0; i < x.length; i++) { double diff = Math.abs(x[i] - repaired[i]); penalty += diff * valueRange; } return isMinimize ? penalty : -penalty; } } // -----Matrix utility functions similar to the Matlab build in functions------ /** * @param m Input matrix * @return Matrix representing the element-wise logarithm of m. */ private static RealMatrix log(final RealMatrix m) { final double[][] d = new double[m.getRowDimension()][m.getColumnDimension()]; for (int r = 0; r < m.getRowDimension(); r++) { for (int c = 0; c < m.getColumnDimension(); c++) { d[r][c] = Math.log(m.getEntry(r, c)); } } return new Array2DRowRealMatrix(d, false); } /** * @param m Input matrix. * @return Matrix representing the element-wise square root of m. */ private static RealMatrix sqrt(final RealMatrix m) { final double[][] d = new double[m.getRowDimension()][m.getColumnDimension()]; for (int r = 0; r < m.getRowDimension(); r++) { for (int c = 0; c < m.getColumnDimension(); c++) { d[r][c] = Math.sqrt(m.getEntry(r, c)); } } return new Array2DRowRealMatrix(d, false); } /** * @param m Input matrix. * @return Matrix representing the element-wise square of m. */ private static RealMatrix square(final RealMatrix m) { final double[][] d = new double[m.getRowDimension()][m.getColumnDimension()]; for (int r = 0; r < m.getRowDimension(); r++) { for (int c = 0; c < m.getColumnDimension(); c++) { double e = m.getEntry(r, c); d[r][c] = e * e; } } return new Array2DRowRealMatrix(d, false); } /** * @param m Input matrix 1. * @param n Input matrix 2. * @return the matrix where the elements of m and n are element-wise multiplied. */ private static RealMatrix times(final RealMatrix m, final RealMatrix n) { final double[][] d = new double[m.getRowDimension()][m.getColumnDimension()]; for (int r = 0; r < m.getRowDimension(); r++) { for (int c = 0; c < m.getColumnDimension(); c++) { d[r][c] = m.getEntry(r, c) * n.getEntry(r, c); } } return new Array2DRowRealMatrix(d, false); } /** * @param m Input matrix 1. * @param n Input matrix 2. * @return Matrix where the elements of m and n are element-wise divided. */ private static RealMatrix divide(final RealMatrix m, final RealMatrix n) { final double[][] d = new double[m.getRowDimension()][m.getColumnDimension()]; for (int r = 0; r < m.getRowDimension(); r++) { for (int c = 0; c < m.getColumnDimension(); c++) { d[r][c] = m.getEntry(r, c) / n.getEntry(r, c); } } return new Array2DRowRealMatrix(d, false); } /** * @param m Input matrix. * @param cols Columns to select. * @return Matrix representing the selected columns. */ private static RealMatrix selectColumns(final RealMatrix m, final int[] cols) { final double[][] d = new double[m.getRowDimension()][cols.length]; for (int r = 0; r < m.getRowDimension(); r++) { for (int c = 0; c < cols.length; c++) { d[r][c] = m.getEntry(r, cols[c]); } } return new Array2DRowRealMatrix(d, false); } /** * @param m Input matrix. * @param k Diagonal position. * @return Upper triangular part of matrix. */ private static RealMatrix triu(final RealMatrix m, int k) { final double[][] d = new double[m.getRowDimension()][m.getColumnDimension()]; for (int r = 0; r < m.getRowDimension(); r++) { for (int c = 0; c < m.getColumnDimension(); c++) { d[r][c] = r <= c - k ? m.getEntry(r, c) : 0; } } return new Array2DRowRealMatrix(d, false); } /** * @param m Input matrix. * @return Row matrix representing the sums of the rows. */ private static RealMatrix sumRows(final RealMatrix m) { final double[][] d = new double[1][m.getColumnDimension()]; for (int c = 0; c < m.getColumnDimension(); c++) { double sum = 0; for (int r = 0; r < m.getRowDimension(); r++) { sum += m.getEntry(r, c); } d[0][c] = sum; } return new Array2DRowRealMatrix(d, false); } /** * @param m Input matrix. * @return the diagonal n-by-n matrix if m is a column matrix or the column * matrix representing the diagonal if m is a n-by-n matrix. */ private static RealMatrix diag(final RealMatrix m) { if (m.getColumnDimension() == 1) { final double[][] d = new double[m.getRowDimension()][m.getRowDimension()]; for (int i = 0; i < m.getRowDimension(); i++) { d[i][i] = m.getEntry(i, 0); } return new Array2DRowRealMatrix(d, false); } else { final double[][] d = new double[m.getRowDimension()][1]; for (int i = 0; i < m.getColumnDimension(); i++) { d[i][0] = m.getEntry(i, i); } return new Array2DRowRealMatrix(d, false); } } /** * Copies a column from m1 to m2. * * @param m1 Source matrix. * @param col1 Source column. * @param m2 Target matrix. * @param col2 Target column. */ private static void copyColumn(final RealMatrix m1, int col1, RealMatrix m2, int col2) { for (int i = 0; i < m1.getRowDimension(); i++) { m2.setEntry(i, col2, m1.getEntry(i, col1)); } } /** * @param n Number of rows. * @param m Number of columns. * @return n-by-m matrix filled with 1. */ private static RealMatrix ones(int n, int m) { final double[][] d = new double[n][m]; for (int r = 0; r < n; r++) { Arrays.fill(d[r], 1); } return new Array2DRowRealMatrix(d, false); } /** * @param n Number of rows. * @param m Number of columns. * @return n-by-m matrix of 0 values out of diagonal, and 1 values on * the diagonal. */ private static RealMatrix eye(int n, int m) { final double[][] d = new double[n][m]; for (int r = 0; r < n; r++) { if (r < m) { d[r][r] = 1; } } return new Array2DRowRealMatrix(d, false); } /** * @param n Number of rows. * @param m Number of columns. * @return n-by-m matrix of zero values. */ private static RealMatrix zeros(int n, int m) { return new Array2DRowRealMatrix(n, m); } /** * @param mat Input matrix. * @param n Number of row replicates. * @param m Number of column replicates. * @return a matrix which replicates the input matrix in both directions. */ private static RealMatrix repmat(final RealMatrix mat, int n, int m) { final int rd = mat.getRowDimension(); final int cd = mat.getColumnDimension(); final double[][] d = new double[n * rd][m * cd]; for (int r = 0; r < n * rd; r++) { for (int c = 0; c < m * cd; c++) { d[r][c] = mat.getEntry(r % rd, c % cd); } } return new Array2DRowRealMatrix(d, false); } /** * @param start Start value. * @param end End value. * @param step Step size. * @return a sequence as column matrix. */ private static RealMatrix sequence(double start, double end, double step) { final int size = (int) ((end - start) / step + 1); final double[][] d = new double[size][1]; double value = start; for (int r = 0; r < size; r++) { d[r][0] = value; value += step; } return new Array2DRowRealMatrix(d, false); } /** * @param m Input matrix. * @return the maximum of the matrix element values. */ private static double max(final RealMatrix m) { double max = -Double.MAX_VALUE; for (int r = 0; r < m.getRowDimension(); r++) { for (int c = 0; c < m.getColumnDimension(); c++) { double e = m.getEntry(r, c); if (max < e) { max = e; } } } return max; } /** * @param m Input matrix. * @return the minimum of the matrix element values. */ private static double min(final RealMatrix m) { double min = Double.MAX_VALUE; for (int r = 0; r < m.getRowDimension(); r++) { for (int c = 0; c < m.getColumnDimension(); c++) { double e = m.getEntry(r, c); if (min > e) { min = e; } } } return min; } /** * @param m Input array. * @return the maximum of the array values. */ private static double max(final double[] m) { double max = -Double.MAX_VALUE; for (int r = 0; r < m.length; r++) { if (max < m[r]) { max = m[r]; } } return max; } /** * @param m Input array. * @return the minimum of the array values. */ private static double min(final double[] m) { double min = Double.MAX_VALUE; for (int r = 0; r < m.length; r++) { if (min > m[r]) { min = m[r]; } } return min; } /** * @param indices Input index array. * @return the inverse of the mapping defined by indices. */ private static int[] inverse(final int[] indices) { final int[] inverse = new int[indices.length]; for (int i = 0; i < indices.length; i++) { inverse[indices[i]] = i; } return inverse; } /** * @param indices Input index array. * @return the indices in inverse order (last is first). */ private static int[] reverse(final int[] indices) { final int[] reverse = new int[indices.length]; for (int i = 0; i < indices.length; i++) { reverse[i] = indices[indices.length - i - 1]; } return reverse; } /** * @param size Length of random array. * @return an array of Gaussian random numbers. */ private double[] randn(int size) { final double[] randn = new double[size]; for (int i = 0; i < size; i++) { randn[i] = random.nextGaussian(); } return randn; } /** * @param size Number of rows. * @param popSize Population size. * @return a 2-dimensional matrix of Gaussian random numbers. */ private RealMatrix randn1(int size, int popSize) { final double[][] d = new double[size][popSize]; for (int r = 0; r < size; r++) { for (int c = 0; c < popSize; c++) { d[r][c] = random.nextGaussian(); } } return new Array2DRowRealMatrix(d, false); } } ././@LongLink100644 0 0 163 12126630646 10261 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/optim/nonlinear/scalar/noderiv/NelderMeadSimplex.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/optim/nonlinear/scalar/noderiv/NelderMe100644 1750 1750 25363 12126627713 32222 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.optim.nonlinear.scalar.noderiv; import java.util.Comparator; import org.apache.commons.math3.optim.PointValuePair; import org.apache.commons.math3.analysis.MultivariateFunction; /** * This class implements the Nelder-Mead simplex algorithm. * * @version $Id: NelderMeadSimplex.java 1435539 2013-01-19 13:27:24Z tn $ * @since 3.0 */ public class NelderMeadSimplex extends AbstractSimplex { /** Default value for {@link #rho}: {@value}. */ private static final double DEFAULT_RHO = 1; /** Default value for {@link #khi}: {@value}. */ private static final double DEFAULT_KHI = 2; /** Default value for {@link #gamma}: {@value}. */ private static final double DEFAULT_GAMMA = 0.5; /** Default value for {@link #sigma}: {@value}. */ private static final double DEFAULT_SIGMA = 0.5; /** Reflection coefficient. */ private final double rho; /** Expansion coefficient. */ private final double khi; /** Contraction coefficient. */ private final double gamma; /** Shrinkage coefficient. */ private final double sigma; /** * Build a Nelder-Mead simplex with default coefficients. * The default coefficients are 1.0 for rho, 2.0 for khi and 0.5 * for both gamma and sigma. * * @param n Dimension of the simplex. */ public NelderMeadSimplex(final int n) { this(n, 1d); } /** * Build a Nelder-Mead simplex with default coefficients. * The default coefficients are 1.0 for rho, 2.0 for khi and 0.5 * for both gamma and sigma. * * @param n Dimension of the simplex. * @param sideLength Length of the sides of the default (hypercube) * simplex. See {@link AbstractSimplex#AbstractSimplex(int,double)}. */ public NelderMeadSimplex(final int n, double sideLength) { this(n, sideLength, DEFAULT_RHO, DEFAULT_KHI, DEFAULT_GAMMA, DEFAULT_SIGMA); } /** * Build a Nelder-Mead simplex with specified coefficients. * * @param n Dimension of the simplex. See * {@link AbstractSimplex#AbstractSimplex(int,double)}. * @param sideLength Length of the sides of the default (hypercube) * simplex. See {@link AbstractSimplex#AbstractSimplex(int,double)}. * @param rho Reflection coefficient. * @param khi Expansion coefficient. * @param gamma Contraction coefficient. * @param sigma Shrinkage coefficient. */ public NelderMeadSimplex(final int n, double sideLength, final double rho, final double khi, final double gamma, final double sigma) { super(n, sideLength); this.rho = rho; this.khi = khi; this.gamma = gamma; this.sigma = sigma; } /** * Build a Nelder-Mead simplex with specified coefficients. * * @param n Dimension of the simplex. See * {@link AbstractSimplex#AbstractSimplex(int)}. * @param rho Reflection coefficient. * @param khi Expansion coefficient. * @param gamma Contraction coefficient. * @param sigma Shrinkage coefficient. */ public NelderMeadSimplex(final int n, final double rho, final double khi, final double gamma, final double sigma) { this(n, 1d, rho, khi, gamma, sigma); } /** * Build a Nelder-Mead simplex with default coefficients. * The default coefficients are 1.0 for rho, 2.0 for khi and 0.5 * for both gamma and sigma. * * @param steps Steps along the canonical axes representing box edges. * They may be negative but not zero. See */ public NelderMeadSimplex(final double[] steps) { this(steps, DEFAULT_RHO, DEFAULT_KHI, DEFAULT_GAMMA, DEFAULT_SIGMA); } /** * Build a Nelder-Mead simplex with specified coefficients. * * @param steps Steps along the canonical axes representing box edges. * They may be negative but not zero. See * {@link AbstractSimplex#AbstractSimplex(double[])}. * @param rho Reflection coefficient. * @param khi Expansion coefficient. * @param gamma Contraction coefficient. * @param sigma Shrinkage coefficient. * @throws IllegalArgumentException if one of the steps is zero. */ public NelderMeadSimplex(final double[] steps, final double rho, final double khi, final double gamma, final double sigma) { super(steps); this.rho = rho; this.khi = khi; this.gamma = gamma; this.sigma = sigma; } /** * Build a Nelder-Mead simplex with default coefficients. * The default coefficients are 1.0 for rho, 2.0 for khi and 0.5 * for both gamma and sigma. * * @param referenceSimplex Reference simplex. See * {@link AbstractSimplex#AbstractSimplex(double[][])}. */ public NelderMeadSimplex(final double[][] referenceSimplex) { this(referenceSimplex, DEFAULT_RHO, DEFAULT_KHI, DEFAULT_GAMMA, DEFAULT_SIGMA); } /** * Build a Nelder-Mead simplex with specified coefficients. * * @param referenceSimplex Reference simplex. See * {@link AbstractSimplex#AbstractSimplex(double[][])}. * @param rho Reflection coefficient. * @param khi Expansion coefficient. * @param gamma Contraction coefficient. * @param sigma Shrinkage coefficient. * @throws org.apache.commons.math3.exception.NotStrictlyPositiveException * if the reference simplex does not contain at least one point. * @throws org.apache.commons.math3.exception.DimensionMismatchException * if there is a dimension mismatch in the reference simplex. */ public NelderMeadSimplex(final double[][] referenceSimplex, final double rho, final double khi, final double gamma, final double sigma) { super(referenceSimplex); this.rho = rho; this.khi = khi; this.gamma = gamma; this.sigma = sigma; } /** {@inheritDoc} */ @Override public void iterate(final MultivariateFunction evaluationFunction, final Comparator comparator) { // The simplex has n + 1 points if dimension is n. final int n = getDimension(); // Interesting values. final PointValuePair best = getPoint(0); final PointValuePair secondBest = getPoint(n - 1); final PointValuePair worst = getPoint(n); final double[] xWorst = worst.getPointRef(); // Compute the centroid of the best vertices (dismissing the worst // point at index n). final double[] centroid = new double[n]; for (int i = 0; i < n; i++) { final double[] x = getPoint(i).getPointRef(); for (int j = 0; j < n; j++) { centroid[j] += x[j]; } } final double scaling = 1.0 / n; for (int j = 0; j < n; j++) { centroid[j] *= scaling; } // compute the reflection point final double[] xR = new double[n]; for (int j = 0; j < n; j++) { xR[j] = centroid[j] + rho * (centroid[j] - xWorst[j]); } final PointValuePair reflected = new PointValuePair(xR, evaluationFunction.value(xR), false); if (comparator.compare(best, reflected) <= 0 && comparator.compare(reflected, secondBest) < 0) { // Accept the reflected point. replaceWorstPoint(reflected, comparator); } else if (comparator.compare(reflected, best) < 0) { // Compute the expansion point. final double[] xE = new double[n]; for (int j = 0; j < n; j++) { xE[j] = centroid[j] + khi * (xR[j] - centroid[j]); } final PointValuePair expanded = new PointValuePair(xE, evaluationFunction.value(xE), false); if (comparator.compare(expanded, reflected) < 0) { // Accept the expansion point. replaceWorstPoint(expanded, comparator); } else { // Accept the reflected point. replaceWorstPoint(reflected, comparator); } } else { if (comparator.compare(reflected, worst) < 0) { // Perform an outside contraction. final double[] xC = new double[n]; for (int j = 0; j < n; j++) { xC[j] = centroid[j] + gamma * (xR[j] - centroid[j]); } final PointValuePair outContracted = new PointValuePair(xC, evaluationFunction.value(xC), false); if (comparator.compare(outContracted, reflected) <= 0) { // Accept the contraction point. replaceWorstPoint(outContracted, comparator); return; } } else { // Perform an inside contraction. final double[] xC = new double[n]; for (int j = 0; j < n; j++) { xC[j] = centroid[j] - gamma * (centroid[j] - xWorst[j]); } final PointValuePair inContracted = new PointValuePair(xC, evaluationFunction.value(xC), false); if (comparator.compare(inContracted, worst) < 0) { // Accept the contraction point. replaceWorstPoint(inContracted, comparator); return; } } // Perform a shrink. final double[] xSmallest = getPoint(0).getPointRef(); for (int i = 1; i <= n; i++) { final double[] x = getPoint(i).getPoint(); for (int j = 0; j < n; j++) { x[j] = xSmallest[j] + sigma * (x[j] - xSmallest[j]); } setPoint(i, new PointValuePair(x, Double.NaN, false)); } evaluate(evaluationFunction, comparator); } } } ././@LongLink100644 0 0 161 12126630646 10257 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/optim/nonlinear/scalar/noderiv/PowellOptimizer.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/optim/nonlinear/scalar/noderiv/PowellOp100644 1750 1750 34750 12126627713 32270 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.optim.nonlinear.scalar.noderiv; import org.apache.commons.math3.util.FastMath; import org.apache.commons.math3.util.MathArrays; import org.apache.commons.math3.analysis.UnivariateFunction; import org.apache.commons.math3.exception.NumberIsTooSmallException; import org.apache.commons.math3.exception.NotStrictlyPositiveException; import org.apache.commons.math3.exception.MathUnsupportedOperationException; import org.apache.commons.math3.exception.util.LocalizedFormats; import org.apache.commons.math3.optim.nonlinear.scalar.GoalType; import org.apache.commons.math3.optim.MaxEval; import org.apache.commons.math3.optim.PointValuePair; import org.apache.commons.math3.optim.ConvergenceChecker; import org.apache.commons.math3.optim.nonlinear.scalar.MultivariateOptimizer; import org.apache.commons.math3.optim.univariate.BracketFinder; import org.apache.commons.math3.optim.univariate.BrentOptimizer; import org.apache.commons.math3.optim.univariate.UnivariatePointValuePair; import org.apache.commons.math3.optim.univariate.SimpleUnivariateValueChecker; import org.apache.commons.math3.optim.univariate.SearchInterval; import org.apache.commons.math3.optim.univariate.UnivariateObjectiveFunction; /** * Powell's algorithm. * This code is translated and adapted from the Python version of this * algorithm (as implemented in module {@code optimize.py} v0.5 of * SciPy). *
                              * The default stopping criterion is based on the differences of the * function value between two successive iterations. It is however possible * to define a custom convergence checker that might terminate the algorithm * earlier. *
                              * The internal line search optimizer is a {@link BrentOptimizer} with a * convergence checker set to {@link SimpleUnivariateValueChecker}. *
                              * Constraints are not supported: the call to * {@link #optimize(OptimizationData[]) optimize} will throw * {@link MathUnsupportedOperationException} if bounds are passed to it. * In order to impose simple constraints, the objective function must be * wrapped in an adapter like * {@link org.apache.commons.math3.optim.nonlinear.scalar.MultivariateFunctionMappingAdapter * MultivariateFunctionMappingAdapter} or * {@link org.apache.commons.math3.optim.nonlinear.scalar.MultivariateFunctionPenaltyAdapter * MultivariateFunctionPenaltyAdapter}. * * @version $Id: PowellOptimizer.java 1462503 2013-03-29 15:48:27Z luc $ * @since 2.2 */ public class PowellOptimizer extends MultivariateOptimizer { /** * Minimum relative tolerance. */ private static final double MIN_RELATIVE_TOLERANCE = 2 * FastMath.ulp(1d); /** * Relative threshold. */ private final double relativeThreshold; /** * Absolute threshold. */ private final double absoluteThreshold; /** * Line search. */ private final LineSearch line; /** * This constructor allows to specify a user-defined convergence checker, * in addition to the parameters that control the default convergence * checking procedure. *
                              * The internal line search tolerances are set to the square-root of their * corresponding value in the multivariate optimizer. * * @param rel Relative threshold. * @param abs Absolute threshold. * @param checker Convergence checker. * @throws NotStrictlyPositiveException if {@code abs <= 0}. * @throws NumberIsTooSmallException if {@code rel < 2 * Math.ulp(1d)}. */ public PowellOptimizer(double rel, double abs, ConvergenceChecker checker) { this(rel, abs, FastMath.sqrt(rel), FastMath.sqrt(abs), checker); } /** * This constructor allows to specify a user-defined convergence checker, * in addition to the parameters that control the default convergence * checking procedure and the line search tolerances. * * @param rel Relative threshold for this optimizer. * @param abs Absolute threshold for this optimizer. * @param lineRel Relative threshold for the internal line search optimizer. * @param lineAbs Absolute threshold for the internal line search optimizer. * @param checker Convergence checker. * @throws NotStrictlyPositiveException if {@code abs <= 0}. * @throws NumberIsTooSmallException if {@code rel < 2 * Math.ulp(1d)}. */ public PowellOptimizer(double rel, double abs, double lineRel, double lineAbs, ConvergenceChecker checker) { super(checker); if (rel < MIN_RELATIVE_TOLERANCE) { throw new NumberIsTooSmallException(rel, MIN_RELATIVE_TOLERANCE, true); } if (abs <= 0) { throw new NotStrictlyPositiveException(abs); } relativeThreshold = rel; absoluteThreshold = abs; // Create the line search optimizer. line = new LineSearch(lineRel, lineAbs); } /** * The parameters control the default convergence checking procedure. *
                              * The internal line search tolerances are set to the square-root of their * corresponding value in the multivariate optimizer. * * @param rel Relative threshold. * @param abs Absolute threshold. * @throws NotStrictlyPositiveException if {@code abs <= 0}. * @throws NumberIsTooSmallException if {@code rel < 2 * Math.ulp(1d)}. */ public PowellOptimizer(double rel, double abs) { this(rel, abs, null); } /** * Builds an instance with the default convergence checking procedure. * * @param rel Relative threshold. * @param abs Absolute threshold. * @param lineRel Relative threshold for the internal line search optimizer. * @param lineAbs Absolute threshold for the internal line search optimizer. * @throws NotStrictlyPositiveException if {@code abs <= 0}. * @throws NumberIsTooSmallException if {@code rel < 2 * Math.ulp(1d)}. */ public PowellOptimizer(double rel, double abs, double lineRel, double lineAbs) { this(rel, abs, lineRel, lineAbs, null); } /** {@inheritDoc} */ @Override protected PointValuePair doOptimize() { checkParameters(); final GoalType goal = getGoalType(); final double[] guess = getStartPoint(); final int n = guess.length; final double[][] direc = new double[n][n]; for (int i = 0; i < n; i++) { direc[i][i] = 1; } final ConvergenceChecker checker = getConvergenceChecker(); double[] x = guess; double fVal = computeObjectiveValue(x); double[] x1 = x.clone(); while (true) { incrementIterationCount(); double fX = fVal; double fX2 = 0; double delta = 0; int bigInd = 0; double alphaMin = 0; for (int i = 0; i < n; i++) { final double[] d = MathArrays.copyOf(direc[i]); fX2 = fVal; final UnivariatePointValuePair optimum = line.search(x, d); fVal = optimum.getValue(); alphaMin = optimum.getPoint(); final double[][] result = newPointAndDirection(x, d, alphaMin); x = result[0]; if ((fX2 - fVal) > delta) { delta = fX2 - fVal; bigInd = i; } } // Default convergence check. boolean stop = 2 * (fX - fVal) <= (relativeThreshold * (FastMath.abs(fX) + FastMath.abs(fVal)) + absoluteThreshold); final PointValuePair previous = new PointValuePair(x1, fX); final PointValuePair current = new PointValuePair(x, fVal); if (!stop && checker != null) { // User-defined stopping criteria. stop = checker.converged(getIterations(), previous, current); } if (stop) { if (goal == GoalType.MINIMIZE) { return (fVal < fX) ? current : previous; } else { return (fVal > fX) ? current : previous; } } final double[] d = new double[n]; final double[] x2 = new double[n]; for (int i = 0; i < n; i++) { d[i] = x[i] - x1[i]; x2[i] = 2 * x[i] - x1[i]; } x1 = x.clone(); fX2 = computeObjectiveValue(x2); if (fX > fX2) { double t = 2 * (fX + fX2 - 2 * fVal); double temp = fX - fVal - delta; t *= temp * temp; temp = fX - fX2; t -= delta * temp * temp; if (t < 0.0) { final UnivariatePointValuePair optimum = line.search(x, d); fVal = optimum.getValue(); alphaMin = optimum.getPoint(); final double[][] result = newPointAndDirection(x, d, alphaMin); x = result[0]; final int lastInd = n - 1; direc[bigInd] = direc[lastInd]; direc[lastInd] = result[1]; } } } } /** * Compute a new point (in the original space) and a new direction * vector, resulting from the line search. * * @param p Point used in the line search. * @param d Direction used in the line search. * @param optimum Optimum found by the line search. * @return a 2-element array containing the new point (at index 0) and * the new direction (at index 1). */ private double[][] newPointAndDirection(double[] p, double[] d, double optimum) { final int n = p.length; final double[] nP = new double[n]; final double[] nD = new double[n]; for (int i = 0; i < n; i++) { nD[i] = d[i] * optimum; nP[i] = p[i] + nD[i]; } final double[][] result = new double[2][]; result[0] = nP; result[1] = nD; return result; } /** * Class for finding the minimum of the objective function along a given * direction. */ private class LineSearch extends BrentOptimizer { /** * Value that will pass the precondition check for {@link BrentOptimizer} * but will not pass the convergence check, so that the custom checker * will always decide when to stop the line search. */ private static final double REL_TOL_UNUSED = 1e-15; /** * Value that will pass the precondition check for {@link BrentOptimizer} * but will not pass the convergence check, so that the custom checker * will always decide when to stop the line search. */ private static final double ABS_TOL_UNUSED = Double.MIN_VALUE; /** * Automatic bracketing. */ private final BracketFinder bracket = new BracketFinder(); /** * The "BrentOptimizer" default stopping criterion uses the tolerances * to check the domain (point) values, not the function values. * We thus create a custom checker to use function values. * * @param rel Relative threshold. * @param abs Absolute threshold. */ LineSearch(double rel, double abs) { super(REL_TOL_UNUSED, ABS_TOL_UNUSED, new SimpleUnivariateValueChecker(rel, abs)); } /** * Find the minimum of the function {@code f(p + alpha * d)}. * * @param p Starting point. * @param d Search direction. * @return the optimum. * @throws org.apache.commons.math3.exception.TooManyEvaluationsException * if the number of evaluations is exceeded. */ public UnivariatePointValuePair search(final double[] p, final double[] d) { final int n = p.length; final UnivariateFunction f = new UnivariateFunction() { public double value(double alpha) { final double[] x = new double[n]; for (int i = 0; i < n; i++) { x[i] = p[i] + alpha * d[i]; } final double obj = PowellOptimizer.this.computeObjectiveValue(x); return obj; } }; final GoalType goal = PowellOptimizer.this.getGoalType(); bracket.search(f, goal, 0, 1); // Passing "MAX_VALUE" as a dummy value because it is the enclosing // class that counts the number of evaluations (and will eventually // generate the exception). return optimize(new MaxEval(Integer.MAX_VALUE), new UnivariateObjectiveFunction(f), goal, new SearchInterval(bracket.getLo(), bracket.getHi(), bracket.getMid())); } } /** * @throws MathUnsupportedOperationException if bounds were passed to the * {@link #optimize(OptimizationData[]) optimize} method. */ private void checkParameters() { if (getLowerBound() != null || getUpperBound() != null) { throw new MathUnsupportedOperationException(LocalizedFormats.CONSTRAINT); } } } ././@LongLink100644 0 0 161 12126630646 10257 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/optim/nonlinear/scalar/noderiv/AbstractSimplex.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/optim/nonlinear/scalar/noderiv/Abstract100644 1750 1750 31344 12126627713 32266 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.optim.nonlinear.scalar.noderiv; import java.util.Arrays; import java.util.Comparator; import org.apache.commons.math3.analysis.MultivariateFunction; import org.apache.commons.math3.exception.NotStrictlyPositiveException; import org.apache.commons.math3.exception.DimensionMismatchException; import org.apache.commons.math3.exception.ZeroException; import org.apache.commons.math3.exception.OutOfRangeException; import org.apache.commons.math3.exception.NullArgumentException; import org.apache.commons.math3.exception.MathIllegalArgumentException; import org.apache.commons.math3.exception.util.LocalizedFormats; import org.apache.commons.math3.optim.PointValuePair; import org.apache.commons.math3.optim.OptimizationData; /** * This class implements the simplex concept. * It is intended to be used in conjunction with {@link SimplexOptimizer}. *
                              * The initial configuration of the simplex is set by the constructors * {@link #AbstractSimplex(double[])} or {@link #AbstractSimplex(double[][])}. * The other {@link #AbstractSimplex(int) constructor} will set all steps * to 1, thus building a default configuration from a unit hypercube. *
                              * Users must call the {@link #build(double[]) build} method in order * to create the data structure that will be acted on by the other methods of * this class. * * @see SimplexOptimizer * @version $Id: AbstractSimplex.java 1435539 2013-01-19 13:27:24Z tn $ * @since 3.0 */ public abstract class AbstractSimplex implements OptimizationData { /** Simplex. */ private PointValuePair[] simplex; /** Start simplex configuration. */ private double[][] startConfiguration; /** Simplex dimension (must be equal to {@code simplex.length - 1}). */ private final int dimension; /** * Build a unit hypercube simplex. * * @param n Dimension of the simplex. */ protected AbstractSimplex(int n) { this(n, 1d); } /** * Build a hypercube simplex with the given side length. * * @param n Dimension of the simplex. * @param sideLength Length of the sides of the hypercube. */ protected AbstractSimplex(int n, double sideLength) { this(createHypercubeSteps(n, sideLength)); } /** * The start configuration for simplex is built from a box parallel to * the canonical axes of the space. The simplex is the subset of vertices * of a box parallel to the canonical axes. It is built as the path followed * while traveling from one vertex of the box to the diagonally opposite * vertex moving only along the box edges. The first vertex of the box will * be located at the start point of the optimization. * As an example, in dimension 3 a simplex has 4 vertices. Setting the * steps to (1, 10, 2) and the start point to (1, 1, 1) would imply the * start simplex would be: { (1, 1, 1), (2, 1, 1), (2, 11, 1), (2, 11, 3) }. * The first vertex would be set to the start point at (1, 1, 1) and the * last vertex would be set to the diagonally opposite vertex at (2, 11, 3). * * @param steps Steps along the canonical axes representing box edges. They * may be negative but not zero. * @throws NullArgumentException if {@code steps} is {@code null}. * @throws ZeroException if one of the steps is zero. */ protected AbstractSimplex(final double[] steps) { if (steps == null) { throw new NullArgumentException(); } if (steps.length == 0) { throw new ZeroException(); } dimension = steps.length; // Only the relative position of the n final vertices with respect // to the first one are stored. startConfiguration = new double[dimension][dimension]; for (int i = 0; i < dimension; i++) { final double[] vertexI = startConfiguration[i]; for (int j = 0; j < i + 1; j++) { if (steps[j] == 0) { throw new ZeroException(LocalizedFormats.EQUAL_VERTICES_IN_SIMPLEX); } System.arraycopy(steps, 0, vertexI, 0, j + 1); } } } /** * The real initial simplex will be set up by moving the reference * simplex such that its first point is located at the start point of the * optimization. * * @param referenceSimplex Reference simplex. * @throws NotStrictlyPositiveException if the reference simplex does not * contain at least one point. * @throws DimensionMismatchException if there is a dimension mismatch * in the reference simplex. * @throws IllegalArgumentException if one of its vertices is duplicated. */ protected AbstractSimplex(final double[][] referenceSimplex) { if (referenceSimplex.length <= 0) { throw new NotStrictlyPositiveException(LocalizedFormats.SIMPLEX_NEED_ONE_POINT, referenceSimplex.length); } dimension = referenceSimplex.length - 1; // Only the relative position of the n final vertices with respect // to the first one are stored. startConfiguration = new double[dimension][dimension]; final double[] ref0 = referenceSimplex[0]; // Loop over vertices. for (int i = 0; i < referenceSimplex.length; i++) { final double[] refI = referenceSimplex[i]; // Safety checks. if (refI.length != dimension) { throw new DimensionMismatchException(refI.length, dimension); } for (int j = 0; j < i; j++) { final double[] refJ = referenceSimplex[j]; boolean allEquals = true; for (int k = 0; k < dimension; k++) { if (refI[k] != refJ[k]) { allEquals = false; break; } } if (allEquals) { throw new MathIllegalArgumentException(LocalizedFormats.EQUAL_VERTICES_IN_SIMPLEX, i, j); } } // Store vertex i position relative to vertex 0 position. if (i > 0) { final double[] confI = startConfiguration[i - 1]; for (int k = 0; k < dimension; k++) { confI[k] = refI[k] - ref0[k]; } } } } /** * Get simplex dimension. * * @return the dimension of the simplex. */ public int getDimension() { return dimension; } /** * Get simplex size. * After calling the {@link #build(double[]) build} method, this method will * will be equivalent to {@code getDimension() + 1}. * * @return the size of the simplex. */ public int getSize() { return simplex.length; } /** * Compute the next simplex of the algorithm. * * @param evaluationFunction Evaluation function. * @param comparator Comparator to use to sort simplex vertices from best * to worst. * @throws org.apache.commons.math3.exception.TooManyEvaluationsException * if the algorithm fails to converge. */ public abstract void iterate(final MultivariateFunction evaluationFunction, final Comparator comparator); /** * Build an initial simplex. * * @param startPoint First point of the simplex. * @throws DimensionMismatchException if the start point does not match * simplex dimension. */ public void build(final double[] startPoint) { if (dimension != startPoint.length) { throw new DimensionMismatchException(dimension, startPoint.length); } // Set first vertex. simplex = new PointValuePair[dimension + 1]; simplex[0] = new PointValuePair(startPoint, Double.NaN); // Set remaining vertices. for (int i = 0; i < dimension; i++) { final double[] confI = startConfiguration[i]; final double[] vertexI = new double[dimension]; for (int k = 0; k < dimension; k++) { vertexI[k] = startPoint[k] + confI[k]; } simplex[i + 1] = new PointValuePair(vertexI, Double.NaN); } } /** * Evaluate all the non-evaluated points of the simplex. * * @param evaluationFunction Evaluation function. * @param comparator Comparator to use to sort simplex vertices from best to worst. * @throws org.apache.commons.math3.exception.TooManyEvaluationsException * if the maximal number of evaluations is exceeded. */ public void evaluate(final MultivariateFunction evaluationFunction, final Comparator comparator) { // Evaluate the objective function at all non-evaluated simplex points. for (int i = 0; i < simplex.length; i++) { final PointValuePair vertex = simplex[i]; final double[] point = vertex.getPointRef(); if (Double.isNaN(vertex.getValue())) { simplex[i] = new PointValuePair(point, evaluationFunction.value(point), false); } } // Sort the simplex from best to worst. Arrays.sort(simplex, comparator); } /** * Replace the worst point of the simplex by a new point. * * @param pointValuePair Point to insert. * @param comparator Comparator to use for sorting the simplex vertices * from best to worst. */ protected void replaceWorstPoint(PointValuePair pointValuePair, final Comparator comparator) { for (int i = 0; i < dimension; i++) { if (comparator.compare(simplex[i], pointValuePair) > 0) { PointValuePair tmp = simplex[i]; simplex[i] = pointValuePair; pointValuePair = tmp; } } simplex[dimension] = pointValuePair; } /** * Get the points of the simplex. * * @return all the simplex points. */ public PointValuePair[] getPoints() { final PointValuePair[] copy = new PointValuePair[simplex.length]; System.arraycopy(simplex, 0, copy, 0, simplex.length); return copy; } /** * Get the simplex point stored at the requested {@code index}. * * @param index Location. * @return the point at location {@code index}. */ public PointValuePair getPoint(int index) { if (index < 0 || index >= simplex.length) { throw new OutOfRangeException(index, 0, simplex.length - 1); } return simplex[index]; } /** * Store a new point at location {@code index}. * Note that no deep-copy of {@code point} is performed. * * @param index Location. * @param point New value. */ protected void setPoint(int index, PointValuePair point) { if (index < 0 || index >= simplex.length) { throw new OutOfRangeException(index, 0, simplex.length - 1); } simplex[index] = point; } /** * Replace all points. * Note that no deep-copy of {@code points} is performed. * * @param points New Points. */ protected void setPoints(PointValuePair[] points) { if (points.length != simplex.length) { throw new DimensionMismatchException(points.length, simplex.length); } simplex = points; } /** * Create steps for a unit hypercube. * * @param n Dimension of the hypercube. * @param sideLength Length of the sides of the hypercube. * @return the steps. */ private static double[] createHypercubeSteps(int n, double sideLength) { final double[] steps = new double[n]; for (int i = 0; i < n; i++) { steps[i] = sideLength; } return steps; } } ././@LongLink100644 0 0 174 12126630646 10263 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/optim/nonlinear/scalar/MultivariateFunctionMappingAdapter.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/optim/nonlinear/scalar/MultivariateFunc100644 1750 1750 25766 12126627713 32352 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.optim.nonlinear.scalar; import org.apache.commons.math3.analysis.MultivariateFunction; import org.apache.commons.math3.analysis.UnivariateFunction; import org.apache.commons.math3.analysis.function.Logit; import org.apache.commons.math3.analysis.function.Sigmoid; import org.apache.commons.math3.exception.DimensionMismatchException; import org.apache.commons.math3.exception.NumberIsTooSmallException; import org.apache.commons.math3.util.FastMath; import org.apache.commons.math3.util.MathUtils; /** *

                              Adapter for mapping bounded {@link MultivariateFunction} to unbounded ones.

                              * *

                              * This adapter can be used to wrap functions subject to simple bounds on * parameters so they can be used by optimizers that do not directly * support simple bounds. *

                              *

                              * The principle is that the user function that will be wrapped will see its * parameters bounded as required, i.e when its {@code value} method is called * with argument array {@code point}, the elements array will fulfill requirement * {@code lower[i] <= point[i] <= upper[i]} for all i. Some of the components * may be unbounded or bounded only on one side if the corresponding bound is * set to an infinite value. The optimizer will not manage the user function by * itself, but it will handle this adapter and it is this adapter that will take * care the bounds are fulfilled. The adapter {@link #value(double[])} method will * be called by the optimizer with unbound parameters, and the adapter will map * the unbounded value to the bounded range using appropriate functions like * {@link Sigmoid} for double bounded elements for example. *

                              *

                              * As the optimizer sees only unbounded parameters, it should be noted that the * start point or simplex expected by the optimizer should be unbounded, so the * user is responsible for converting his bounded point to unbounded by calling * {@link #boundedToUnbounded(double[])} before providing them to the optimizer. * For the same reason, the point returned by the {@link * org.apache.commons.math3.optimization.BaseMultivariateOptimizer#optimize(int, * MultivariateFunction, org.apache.commons.math3.optimization.GoalType, double[])} * method is unbounded. So to convert this point to bounded, users must call * {@link #unboundedToBounded(double[])} by themselves!

                              *

                              * This adapter is only a poor man solution to simple bounds optimization constraints * that can be used with simple optimizers like * {@link org.apache.commons.math3.optim.nonlinear.scalar.noderiv.SimplexOptimizer * SimplexOptimizer}. * A better solution is to use an optimizer that directly supports simple bounds like * {@link org.apache.commons.math3.optim.nonlinear.scalar.noderiv.CMAESOptimizer * CMAESOptimizer} or * {@link org.apache.commons.math3.optim.nonlinear.scalar.noderiv.BOBYQAOptimizer * BOBYQAOptimizer}. * One caveat of this poor-man's solution is that behavior near the bounds may be * numerically unstable as bounds are mapped from infinite values. * Another caveat is that convergence values are evaluated by the optimizer with * respect to unbounded variables, so there will be scales differences when * converted to bounded variables. *

                              * * @see MultivariateFunctionPenaltyAdapter * * @version $Id: MultivariateFunctionMappingAdapter.java 1435539 2013-01-19 13:27:24Z tn $ * @since 3.0 */ public class MultivariateFunctionMappingAdapter implements MultivariateFunction { /** Underlying bounded function. */ private final MultivariateFunction bounded; /** Mapping functions. */ private final Mapper[] mappers; /** Simple constructor. * @param bounded bounded function * @param lower lower bounds for each element of the input parameters array * (some elements may be set to {@code Double.NEGATIVE_INFINITY} for * unbounded values) * @param upper upper bounds for each element of the input parameters array * (some elements may be set to {@code Double.POSITIVE_INFINITY} for * unbounded values) * @exception DimensionMismatchException if lower and upper bounds are not * consistent, either according to dimension or to values */ public MultivariateFunctionMappingAdapter(final MultivariateFunction bounded, final double[] lower, final double[] upper) { // safety checks MathUtils.checkNotNull(lower); MathUtils.checkNotNull(upper); if (lower.length != upper.length) { throw new DimensionMismatchException(lower.length, upper.length); } for (int i = 0; i < lower.length; ++i) { // note the following test is written in such a way it also fails for NaN if (!(upper[i] >= lower[i])) { throw new NumberIsTooSmallException(upper[i], lower[i], true); } } this.bounded = bounded; this.mappers = new Mapper[lower.length]; for (int i = 0; i < mappers.length; ++i) { if (Double.isInfinite(lower[i])) { if (Double.isInfinite(upper[i])) { // element is unbounded, no transformation is needed mappers[i] = new NoBoundsMapper(); } else { // element is simple-bounded on the upper side mappers[i] = new UpperBoundMapper(upper[i]); } } else { if (Double.isInfinite(upper[i])) { // element is simple-bounded on the lower side mappers[i] = new LowerBoundMapper(lower[i]); } else { // element is double-bounded mappers[i] = new LowerUpperBoundMapper(lower[i], upper[i]); } } } } /** * Maps an array from unbounded to bounded. * * @param point Unbounded values. * @return the bounded values. */ public double[] unboundedToBounded(double[] point) { // Map unbounded input point to bounded point. final double[] mapped = new double[mappers.length]; for (int i = 0; i < mappers.length; ++i) { mapped[i] = mappers[i].unboundedToBounded(point[i]); } return mapped; } /** * Maps an array from bounded to unbounded. * * @param point Bounded values. * @return the unbounded values. */ public double[] boundedToUnbounded(double[] point) { // Map bounded input point to unbounded point. final double[] mapped = new double[mappers.length]; for (int i = 0; i < mappers.length; ++i) { mapped[i] = mappers[i].boundedToUnbounded(point[i]); } return mapped; } /** * Compute the underlying function value from an unbounded point. *

                              * This method simply bounds the unbounded point using the mappings * set up at construction and calls the underlying function using * the bounded point. *

                              * @param point unbounded value * @return underlying function value * @see #unboundedToBounded(double[]) */ public double value(double[] point) { return bounded.value(unboundedToBounded(point)); } /** Mapping interface. */ private interface Mapper { /** * Maps a value from unbounded to bounded. * * @param y Unbounded value. * @return the bounded value. */ double unboundedToBounded(double y); /** * Maps a value from bounded to unbounded. * * @param x Bounded value. * @return the unbounded value. */ double boundedToUnbounded(double x); } /** Local class for no bounds mapping. */ private static class NoBoundsMapper implements Mapper { /** {@inheritDoc} */ public double unboundedToBounded(final double y) { return y; } /** {@inheritDoc} */ public double boundedToUnbounded(final double x) { return x; } } /** Local class for lower bounds mapping. */ private static class LowerBoundMapper implements Mapper { /** Low bound. */ private final double lower; /** * Simple constructor. * * @param lower lower bound */ public LowerBoundMapper(final double lower) { this.lower = lower; } /** {@inheritDoc} */ public double unboundedToBounded(final double y) { return lower + FastMath.exp(y); } /** {@inheritDoc} */ public double boundedToUnbounded(final double x) { return FastMath.log(x - lower); } } /** Local class for upper bounds mapping. */ private static class UpperBoundMapper implements Mapper { /** Upper bound. */ private final double upper; /** Simple constructor. * @param upper upper bound */ public UpperBoundMapper(final double upper) { this.upper = upper; } /** {@inheritDoc} */ public double unboundedToBounded(final double y) { return upper - FastMath.exp(-y); } /** {@inheritDoc} */ public double boundedToUnbounded(final double x) { return -FastMath.log(upper - x); } } /** Local class for lower and bounds mapping. */ private static class LowerUpperBoundMapper implements Mapper { /** Function from unbounded to bounded. */ private final UnivariateFunction boundingFunction; /** Function from bounded to unbounded. */ private final UnivariateFunction unboundingFunction; /** * Simple constructor. * * @param lower lower bound * @param upper upper bound */ public LowerUpperBoundMapper(final double lower, final double upper) { boundingFunction = new Sigmoid(lower, upper); unboundingFunction = new Logit(lower, upper); } /** {@inheritDoc} */ public double unboundedToBounded(final double y) { return boundingFunction.value(y); } /** {@inheritDoc} */ public double boundedToUnbounded(final double x) { return unboundingFunction.value(x); } } } ././@LongLink100644 0 0 157 12126630646 10264 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/optim/nonlinear/scalar/gradient/package-info.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/optim/nonlinear/scalar/gradient/package100644 1750 1750 1671 12126627713 32225 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ /** * This package provides optimization algorithms that require derivatives. */ package org.apache.commons.math3.optim.nonlinear.scalar.gradient; ././@LongLink100644 0 0 206 12126630646 10257 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/optim/nonlinear/scalar/gradient/NonLinearConjugateGradientOptimizer.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/optim/nonlinear/scalar/gradient/NonLine100644 1750 1750 35735 12126627713 32224 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.optim.nonlinear.scalar.gradient; import org.apache.commons.math3.analysis.UnivariateFunction; import org.apache.commons.math3.analysis.solvers.BrentSolver; import org.apache.commons.math3.analysis.solvers.UnivariateSolver; import org.apache.commons.math3.exception.MathInternalError; import org.apache.commons.math3.exception.MathIllegalStateException; import org.apache.commons.math3.exception.TooManyEvaluationsException; import org.apache.commons.math3.exception.MathUnsupportedOperationException; import org.apache.commons.math3.exception.util.LocalizedFormats; import org.apache.commons.math3.optim.OptimizationData; import org.apache.commons.math3.optim.PointValuePair; import org.apache.commons.math3.optim.ConvergenceChecker; import org.apache.commons.math3.optim.nonlinear.scalar.GoalType; import org.apache.commons.math3.optim.nonlinear.scalar.GradientMultivariateOptimizer; import org.apache.commons.math3.util.FastMath; /** * Non-linear conjugate gradient optimizer. *
                              * This class supports both the Fletcher-Reeves and the Polak-Ribière * update formulas for the conjugate search directions. * It also supports optional preconditioning. *
                              * Constraints are not supported: the call to * {@link #optimize(OptimizationData[]) optimize} will throw * {@link MathUnsupportedOperationException} if bounds are passed to it. * * @version $Id: NonLinearConjugateGradientOptimizer.java 1462503 2013-03-29 15:48:27Z luc $ * @since 2.0 */ public class NonLinearConjugateGradientOptimizer extends GradientMultivariateOptimizer { /** Update formula for the beta parameter. */ private final Formula updateFormula; /** Preconditioner (may be null). */ private final Preconditioner preconditioner; /** solver to use in the line search (may be null). */ private final UnivariateSolver solver; /** Initial step used to bracket the optimum in line search. */ private double initialStep = 1; /** * Constructor with default {@link BrentSolver line search solver} and * {@link IdentityPreconditioner preconditioner}. * * @param updateFormula formula to use for updating the β parameter, * must be one of {@link Formula#FLETCHER_REEVES} or * {@link Formula#POLAK_RIBIERE}. * @param checker Convergence checker. */ public NonLinearConjugateGradientOptimizer(final Formula updateFormula, ConvergenceChecker checker) { this(updateFormula, checker, new BrentSolver(), new IdentityPreconditioner()); } /** * Available choices of update formulas for the updating the parameter * that is used to compute the successive conjugate search directions. * For non-linear conjugate gradients, there are * two formulas: *
                                *
                              • Fletcher-Reeves formula
                              • *
                              • Polak-Ribière formula
                              • *
                              * * On the one hand, the Fletcher-Reeves formula is guaranteed to converge * if the start point is close enough of the optimum whether the * Polak-Ribière formula may not converge in rare cases. On the * other hand, the Polak-Ribière formula is often faster when it * does converge. Polak-Ribière is often used. * * @since 2.0 */ public static enum Formula { /** Fletcher-Reeves formula. */ FLETCHER_REEVES, /** Polak-Ribière formula. */ POLAK_RIBIERE } /** * The initial step is a factor with respect to the search direction * (which itself is roughly related to the gradient of the function). *
                              * It is used to find an interval that brackets the optimum in line * search. * * @since 3.1 */ public static class BracketingStep implements OptimizationData { /** Initial step. */ private final double initialStep; /** * @param step Initial step for the bracket search. */ public BracketingStep(double step) { initialStep = step; } /** * Gets the initial step. * * @return the initial step. */ public double getBracketingStep() { return initialStep; } } /** * Constructor with default {@link IdentityPreconditioner preconditioner}. * * @param updateFormula formula to use for updating the β parameter, * must be one of {@link Formula#FLETCHER_REEVES} or * {@link Formula#POLAK_RIBIERE}. * @param checker Convergence checker. * @param lineSearchSolver Solver to use during line search. */ public NonLinearConjugateGradientOptimizer(final Formula updateFormula, ConvergenceChecker checker, final UnivariateSolver lineSearchSolver) { this(updateFormula, checker, lineSearchSolver, new IdentityPreconditioner()); } /** * @param updateFormula formula to use for updating the β parameter, * must be one of {@link Formula#FLETCHER_REEVES} or * {@link Formula#POLAK_RIBIERE}. * @param checker Convergence checker. * @param lineSearchSolver Solver to use during line search. * @param preconditioner Preconditioner. */ public NonLinearConjugateGradientOptimizer(final Formula updateFormula, ConvergenceChecker checker, final UnivariateSolver lineSearchSolver, final Preconditioner preconditioner) { super(checker); this.updateFormula = updateFormula; solver = lineSearchSolver; this.preconditioner = preconditioner; initialStep = 1; } /** * {@inheritDoc} * * @param optData Optimization data. In addition to those documented in * {@link GradientMultivariateOptimizer#parseOptimizationData(OptimizationData[]) * GradientMultivariateOptimizer}, this method will register the following data: *
                                *
                              • {@link BracketingStep}
                              • *
                              * @return {@inheritDoc} * @throws TooManyEvaluationsException if the maximal number of * evaluations (of the objective function) is exceeded. */ @Override public PointValuePair optimize(OptimizationData... optData) throws TooManyEvaluationsException { // Set up base class and perform computation. return super.optimize(optData); } /** {@inheritDoc} */ @Override protected PointValuePair doOptimize() { final ConvergenceChecker checker = getConvergenceChecker(); final double[] point = getStartPoint(); final GoalType goal = getGoalType(); final int n = point.length; double[] r = computeObjectiveGradient(point); if (goal == GoalType.MINIMIZE) { for (int i = 0; i < n; i++) { r[i] = -r[i]; } } // Initial search direction. double[] steepestDescent = preconditioner.precondition(point, r); double[] searchDirection = steepestDescent.clone(); double delta = 0; for (int i = 0; i < n; ++i) { delta += r[i] * searchDirection[i]; } PointValuePair current = null; int maxEval = getMaxEvaluations(); while (true) { incrementIterationCount(); final double objective = computeObjectiveValue(point); PointValuePair previous = current; current = new PointValuePair(point, objective); if (previous != null && checker.converged(getIterations(), previous, current)) { // We have found an optimum. return current; } // Find the optimal step in the search direction. final UnivariateFunction lsf = new LineSearchFunction(point, searchDirection); final double uB = findUpperBound(lsf, 0, initialStep); // XXX Last parameters is set to a value close to zero in order to // work around the divergence problem in the "testCircleFitting" // unit test (see MATH-439). final double step = solver.solve(maxEval, lsf, 0, uB, 1e-15); maxEval -= solver.getEvaluations(); // Subtract used up evaluations. // Validate new point. for (int i = 0; i < point.length; ++i) { point[i] += step * searchDirection[i]; } r = computeObjectiveGradient(point); if (goal == GoalType.MINIMIZE) { for (int i = 0; i < n; ++i) { r[i] = -r[i]; } } // Compute beta. final double deltaOld = delta; final double[] newSteepestDescent = preconditioner.precondition(point, r); delta = 0; for (int i = 0; i < n; ++i) { delta += r[i] * newSteepestDescent[i]; } final double beta; switch (updateFormula) { case FLETCHER_REEVES: beta = delta / deltaOld; break; case POLAK_RIBIERE: double deltaMid = 0; for (int i = 0; i < r.length; ++i) { deltaMid += r[i] * steepestDescent[i]; } beta = (delta - deltaMid) / deltaOld; break; default: // Should never happen. throw new MathInternalError(); } steepestDescent = newSteepestDescent; // Compute conjugate search direction. if (getIterations() % n == 0 || beta < 0) { // Break conjugation: reset search direction. searchDirection = steepestDescent.clone(); } else { // Compute new conjugate search direction. for (int i = 0; i < n; ++i) { searchDirection[i] = steepestDescent[i] + beta * searchDirection[i]; } } } } /** * Scans the list of (required and optional) optimization data that * characterize the problem. * * @param optData Optimization data. * The following data will be looked for: *
                                *
                              • {@link BracketingStep}
                              • *
                              */ @Override protected void parseOptimizationData(OptimizationData... optData) { // Allow base class to register its own data. super.parseOptimizationData(optData); // The existing values (as set by the previous call) are reused if // not provided in the argument list. for (OptimizationData data : optData) { if (data instanceof BracketingStep) { initialStep = ((BracketingStep) data).getBracketingStep(); // If more data must be parsed, this statement _must_ be // changed to "continue". break; } } checkParameters(); } /** * Finds the upper bound b ensuring bracketing of a root between a and b. * * @param f function whose root must be bracketed. * @param a lower bound of the interval. * @param h initial step to try. * @return b such that f(a) and f(b) have opposite signs. * @throws MathIllegalStateException if no bracket can be found. */ private double findUpperBound(final UnivariateFunction f, final double a, final double h) { final double yA = f.value(a); double yB = yA; for (double step = h; step < Double.MAX_VALUE; step *= FastMath.max(2, yA / yB)) { final double b = a + step; yB = f.value(b); if (yA * yB <= 0) { return b; } } throw new MathIllegalStateException(LocalizedFormats.UNABLE_TO_BRACKET_OPTIMUM_IN_LINE_SEARCH); } /** Default identity preconditioner. */ public static class IdentityPreconditioner implements Preconditioner { /** {@inheritDoc} */ public double[] precondition(double[] variables, double[] r) { return r.clone(); } } /** * Internal class for line search. *

                              * The function represented by this class is the dot product of * the objective function gradient and the search direction. Its * value is zero when the gradient is orthogonal to the search * direction, i.e. when the objective function value is a local * extremum along the search direction. *

                              */ private class LineSearchFunction implements UnivariateFunction { /** Current point. */ private final double[] currentPoint; /** Search direction. */ private final double[] searchDirection; /** * @param point Current point. * @param direction Search direction. */ public LineSearchFunction(double[] point, double[] direction) { currentPoint = point.clone(); searchDirection = direction.clone(); } /** {@inheritDoc} */ public double value(double x) { // current point in the search direction final double[] shiftedPoint = currentPoint.clone(); for (int i = 0; i < shiftedPoint.length; ++i) { shiftedPoint[i] += x * searchDirection[i]; } // gradient of the objective function final double[] gradient = computeObjectiveGradient(shiftedPoint); // dot product with the search direction double dotProduct = 0; for (int i = 0; i < gradient.length; ++i) { dotProduct += gradient[i] * searchDirection[i]; } return dotProduct; } } /** * @throws MathUnsupportedOperationException if bounds were passed to the * {@link #optimize(OptimizationData[]) optimize} method. */ private void checkParameters() { if (getLowerBound() != null || getUpperBound() != null) { throw new MathUnsupportedOperationException(LocalizedFormats.CONSTRAINT); } } } ././@LongLink100644 0 0 161 12126630646 10257 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/optim/nonlinear/scalar/gradient/Preconditioner.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/optim/nonlinear/scalar/gradient/Precond100644 1750 1750 4141 12126627713 32217 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.optim.nonlinear.scalar.gradient; /** * This interface represents a preconditioner for differentiable scalar * objective function optimizers. * @version $Id: Preconditioner.java 1435539 2013-01-19 13:27:24Z tn $ * @since 2.0 */ public interface Preconditioner { /** * Precondition a search direction. *

                              * The returned preconditioned search direction must be computed fast or * the algorithm performances will drop drastically. A classical approach * is to compute only the diagonal elements of the hessian and to divide * the raw search direction by these elements if they are all positive. * If at least one of them is negative, it is safer to return a clone of * the raw search direction as if the hessian was the identity matrix. The * rationale for this simplified choice is that a negative diagonal element * means the current point is far from the optimum and preconditioning will * not be efficient anyway in this case. *

                              * @param point current point at which the search direction was computed * @param r raw search direction (i.e. opposite of the gradient) * @return approximation of H-1r where H is the objective function hessian */ double[] precondition(double[] point, double[] r); } ././@LongLink100644 0 0 174 12126630646 10263 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/optim/nonlinear/scalar/MultivariateFunctionPenaltyAdapter.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/optim/nonlinear/scalar/MultivariateFunc100644 1750 1750 20647 12126627713 32343 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.optim.nonlinear.scalar; import org.apache.commons.math3.analysis.MultivariateFunction; import org.apache.commons.math3.exception.DimensionMismatchException; import org.apache.commons.math3.exception.NumberIsTooSmallException; import org.apache.commons.math3.util.FastMath; import org.apache.commons.math3.util.MathUtils; /** *

                              Adapter extending bounded {@link MultivariateFunction} to an unbouded * domain using a penalty function.

                              * *

                              * This adapter can be used to wrap functions subject to simple bounds on * parameters so they can be used by optimizers that do not directly * support simple bounds. *

                              *

                              * The principle is that the user function that will be wrapped will see its * parameters bounded as required, i.e when its {@code value} method is called * with argument array {@code point}, the elements array will fulfill requirement * {@code lower[i] <= point[i] <= upper[i]} for all i. Some of the components * may be unbounded or bounded only on one side if the corresponding bound is * set to an infinite value. The optimizer will not manage the user function by * itself, but it will handle this adapter and it is this adapter that will take * care the bounds are fulfilled. The adapter {@link #value(double[])} method will * be called by the optimizer with unbound parameters, and the adapter will check * if the parameters is within range or not. If it is in range, then the underlying * user function will be called, and if it is not the value of a penalty function * will be returned instead. *

                              *

                              * This adapter is only a poor-man's solution to simple bounds optimization * constraints that can be used with simple optimizers like * {@link org.apache.commons.math3.optim.nonlinear.scalar.noderiv.SimplexOptimizer * SimplexOptimizer}. * A better solution is to use an optimizer that directly supports simple bounds like * {@link org.apache.commons.math3.optim.nonlinear.scalar.noderiv.CMAESOptimizer * CMAESOptimizer} or * {@link org.apache.commons.math3.optim.nonlinear.scalar.noderiv.BOBYQAOptimizer * BOBYQAOptimizer}. * One caveat of this poor-man's solution is that if start point or start simplex * is completely outside of the allowed range, only the penalty function is used, * and the optimizer may converge without ever entering the range. *

                              * * @see MultivariateFunctionMappingAdapter * * @version $Id: MultivariateFunctionPenaltyAdapter.java 1435539 2013-01-19 13:27:24Z tn $ * @since 3.0 */ public class MultivariateFunctionPenaltyAdapter implements MultivariateFunction { /** Underlying bounded function. */ private final MultivariateFunction bounded; /** Lower bounds. */ private final double[] lower; /** Upper bounds. */ private final double[] upper; /** Penalty offset. */ private final double offset; /** Penalty scales. */ private final double[] scale; /** * Simple constructor. *

                              * When the optimizer provided points are out of range, the value of the * penalty function will be used instead of the value of the underlying * function. In order for this penalty to be effective in rejecting this * point during the optimization process, the penalty function value should * be defined with care. This value is computed as: *

                                   *   penalty(point) = offset + ∑i[scale[i] * √|point[i]-boundary[i]|]
                                   * 
                              * where indices i correspond to all the components that violates their boundaries. *

                              *

                              * So when attempting a function minimization, offset should be larger than * the maximum expected value of the underlying function and scale components * should all be positive. When attempting a function maximization, offset * should be lesser than the minimum expected value of the underlying function * and scale components should all be negative. * minimization, and lesser than the minimum expected value of the underlying * function when attempting maximization. *

                              *

                              * These choices for the penalty function have two properties. First, all out * of range points will return a function value that is worse than the value * returned by any in range point. Second, the penalty is worse for large * boundaries violation than for small violations, so the optimizer has an hint * about the direction in which it should search for acceptable points. *

                              * @param bounded bounded function * @param lower lower bounds for each element of the input parameters array * (some elements may be set to {@code Double.NEGATIVE_INFINITY} for * unbounded values) * @param upper upper bounds for each element of the input parameters array * (some elements may be set to {@code Double.POSITIVE_INFINITY} for * unbounded values) * @param offset base offset of the penalty function * @param scale scale of the penalty function * @exception DimensionMismatchException if lower bounds, upper bounds and * scales are not consistent, either according to dimension or to bounadary * values */ public MultivariateFunctionPenaltyAdapter(final MultivariateFunction bounded, final double[] lower, final double[] upper, final double offset, final double[] scale) { // safety checks MathUtils.checkNotNull(lower); MathUtils.checkNotNull(upper); MathUtils.checkNotNull(scale); if (lower.length != upper.length) { throw new DimensionMismatchException(lower.length, upper.length); } if (lower.length != scale.length) { throw new DimensionMismatchException(lower.length, scale.length); } for (int i = 0; i < lower.length; ++i) { // note the following test is written in such a way it also fails for NaN if (!(upper[i] >= lower[i])) { throw new NumberIsTooSmallException(upper[i], lower[i], true); } } this.bounded = bounded; this.lower = lower.clone(); this.upper = upper.clone(); this.offset = offset; this.scale = scale.clone(); } /** * Computes the underlying function value from an unbounded point. *

                              * This method simply returns the value of the underlying function * if the unbounded point already fulfills the bounds, and compute * a replacement value using the offset and scale if bounds are * violated, without calling the function at all. *

                              * @param point unbounded point * @return either underlying function value or penalty function value */ public double value(double[] point) { for (int i = 0; i < scale.length; ++i) { if ((point[i] < lower[i]) || (point[i] > upper[i])) { // bound violation starting at this component double sum = 0; for (int j = i; j < scale.length; ++j) { final double overshoot; if (point[j] < lower[j]) { overshoot = scale[j] * (lower[j] - point[j]); } else if (point[j] > upper[j]) { overshoot = scale[j] * (point[j] - upper[j]); } else { overshoot = 0; } sum += FastMath.sqrt(overshoot); } return offset + sum; } } // all boundaries are fulfilled, we are in the expected // domain of the underlying function return bounded.value(point); } } ././@LongLink100644 0 0 167 12126630646 10265 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/optim/nonlinear/scalar/GradientMultivariateOptimizer.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/optim/nonlinear/scalar/GradientMultivar100644 1750 1750 7366 12126627713 32325 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.optim.nonlinear.scalar; import org.apache.commons.math3.analysis.MultivariateVectorFunction; import org.apache.commons.math3.optim.ConvergenceChecker; import org.apache.commons.math3.optim.OptimizationData; import org.apache.commons.math3.optim.PointValuePair; import org.apache.commons.math3.exception.TooManyEvaluationsException; /** * Base class for implementing optimizers for multivariate scalar * differentiable functions. * It contains boiler-plate code for dealing with gradient evaluation. * * @version $Id: GradientMultivariateOptimizer.java 1443444 2013-02-07 12:41:36Z erans $ * @since 3.1 */ public abstract class GradientMultivariateOptimizer extends MultivariateOptimizer { /** * Gradient of the objective function. */ private MultivariateVectorFunction gradient; /** * @param checker Convergence checker. */ protected GradientMultivariateOptimizer(ConvergenceChecker checker) { super(checker); } /** * Compute the gradient vector. * * @param params Point at which the gradient must be evaluated. * @return the gradient at the specified point. */ protected double[] computeObjectiveGradient(final double[] params) { return gradient.value(params); } /** * {@inheritDoc} * * @param optData Optimization data. In addition to those documented in * {@link MultivariateOptimizer#parseOptimizationData(OptimizationData[]) * MultivariateOptimizer}, this method will register the following data: *
                                *
                              • {@link ObjectiveFunctionGradient}
                              • *
                              * @return {@inheritDoc} * @throws TooManyEvaluationsException if the maximal number of * evaluations (of the objective function) is exceeded. */ @Override public PointValuePair optimize(OptimizationData... optData) throws TooManyEvaluationsException { // Set up base class and perform computation. return super.optimize(optData); } /** * Scans the list of (required and optional) optimization data that * characterize the problem. * * @param optData Optimization data. * The following data will be looked for: *
                                *
                              • {@link ObjectiveFunctionGradient}
                              • *
                              */ @Override protected void parseOptimizationData(OptimizationData... optData) { // Allow base class to register its own data. super.parseOptimizationData(optData); // The existing values (as set by the previous call) are reused if // not provided in the argument list. for (OptimizationData data : optData) { if (data instanceof ObjectiveFunctionGradient) { gradient = ((ObjectiveFunctionGradient) data).getObjectiveFunctionGradient(); // If more data must be parsed, this statement _must_ be // changed to "continue". break; } } } } ././@LongLink100644 0 0 163 12126630646 10261 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/optim/nonlinear/scalar/ObjectiveFunctionGradient.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/optim/nonlinear/scalar/ObjectiveFunctio100644 1750 1750 3261 12126627713 32274 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.optim.nonlinear.scalar; import org.apache.commons.math3.analysis.MultivariateVectorFunction; import org.apache.commons.math3.optim.OptimizationData; /** * Gradient of the scalar function to be optimized. * * @version $Id: ObjectiveFunctionGradient.java 1435539 2013-01-19 13:27:24Z tn $ * @since 3.1 */ public class ObjectiveFunctionGradient implements OptimizationData { /** Function to be optimized. */ private final MultivariateVectorFunction gradient; /** * @param g Gradient of the function to be optimized. */ public ObjectiveFunctionGradient(MultivariateVectorFunction g) { gradient = g; } /** * Gets the gradient of the function to be optimized. * * @return the objective function gradient. */ public MultivariateVectorFunction getObjectiveFunctionGradient() { return gradient; } } ././@LongLink100644 0 0 153 12126630646 10260 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/optim/nonlinear/scalar/ObjectiveFunction.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/optim/nonlinear/scalar/ObjectiveFunctio100644 1750 1750 3100 12126627713 32264 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.optim.nonlinear.scalar; import org.apache.commons.math3.analysis.MultivariateFunction; import org.apache.commons.math3.optim.OptimizationData; /** * Scalar function to be optimized. * * @version $Id: ObjectiveFunction.java 1435539 2013-01-19 13:27:24Z tn $ * @since 3.1 */ public class ObjectiveFunction implements OptimizationData { /** Function to be optimized. */ private final MultivariateFunction function; /** * @param f Function to be optimized. */ public ObjectiveFunction(MultivariateFunction f) { function = f; } /** * Gets the function to be optimized. * * @return the objective function. */ public MultivariateFunction getObjectiveFunction() { return function; } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/optim/nonlinear/scalar/GoalType.java100644 1750 1750 2322 12126627713 31473 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.optim.nonlinear.scalar; import org.apache.commons.math3.optim.OptimizationData; /** * Goal type for an optimization problem (minimization or maximization of * a scalar function. * * @version $Id: GoalType.java 1435539 2013-01-19 13:27:24Z tn $ * @since 2.0 */ public enum GoalType implements OptimizationData { /** Maximization. */ MAXIMIZE, /** Minimization. */ MINIMIZE } ././@LongLink100644 0 0 157 12126630646 10264 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/optim/nonlinear/scalar/LeastSquaresConverter.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/optim/nonlinear/scalar/LeastSquaresConv100644 1750 1750 20165 12126627713 32316 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.optim.nonlinear.scalar; import org.apache.commons.math3.analysis.MultivariateFunction; import org.apache.commons.math3.analysis.MultivariateVectorFunction; import org.apache.commons.math3.exception.DimensionMismatchException; import org.apache.commons.math3.linear.RealMatrix; /** * This class converts * {@link MultivariateVectorFunction vectorial objective functions} to * {@link MultivariateFunction scalar objective functions} * when the goal is to minimize them. *
                              * This class is mostly used when the vectorial objective function represents * a theoretical result computed from a point set applied to a model and * the models point must be adjusted to fit the theoretical result to some * reference observations. The observations may be obtained for example from * physical measurements whether the model is built from theoretical * considerations. *
                              * This class computes a possibly weighted squared sum of the residuals, which is * a scalar value. The residuals are the difference between the theoretical model * (i.e. the output of the vectorial objective function) and the observations. The * class implements the {@link MultivariateFunction} interface and can therefore be * minimized by any optimizer supporting scalar objectives functions.This is one way * to perform a least square estimation. There are other ways to do this without using * this converter, as some optimization algorithms directly support vectorial objective * functions. *
                              * This class support combination of residuals with or without weights and correlations. * * @see MultivariateFunction * @see MultivariateVectorFunction * @version $Id: LeastSquaresConverter.java 1435539 2013-01-19 13:27:24Z tn $ * @since 2.0 */ public class LeastSquaresConverter implements MultivariateFunction { /** Underlying vectorial function. */ private final MultivariateVectorFunction function; /** Observations to be compared to objective function to compute residuals. */ private final double[] observations; /** Optional weights for the residuals. */ private final double[] weights; /** Optional scaling matrix (weight and correlations) for the residuals. */ private final RealMatrix scale; /** * Builds a simple converter for uncorrelated residuals with identical * weights. * * @param function vectorial residuals function to wrap * @param observations observations to be compared to objective function to compute residuals */ public LeastSquaresConverter(final MultivariateVectorFunction function, final double[] observations) { this.function = function; this.observations = observations.clone(); this.weights = null; this.scale = null; } /** * Builds a simple converter for uncorrelated residuals with the * specified weights. *

                              * The scalar objective function value is computed as: *

                                   * objective = ∑weighti(observationi-objectivei)2
                                   * 
                              *

                              *

                              * Weights can be used for example to combine residuals with different standard * deviations. As an example, consider a residuals array in which even elements * are angular measurements in degrees with a 0.01° standard deviation and * odd elements are distance measurements in meters with a 15m standard deviation. * In this case, the weights array should be initialized with value * 1.0/(0.012) in the even elements and 1.0/(15.02) in the * odd elements (i.e. reciprocals of variances). *

                              *

                              * The array computed by the objective function, the observations array and the * weights array must have consistent sizes or a {@link DimensionMismatchException} * will be triggered while computing the scalar objective. *

                              * * @param function vectorial residuals function to wrap * @param observations observations to be compared to objective function to compute residuals * @param weights weights to apply to the residuals * @throws DimensionMismatchException if the observations vector and the weights * vector dimensions do not match (objective function dimension is checked only when * the {@link #value(double[])} method is called) */ public LeastSquaresConverter(final MultivariateVectorFunction function, final double[] observations, final double[] weights) { if (observations.length != weights.length) { throw new DimensionMismatchException(observations.length, weights.length); } this.function = function; this.observations = observations.clone(); this.weights = weights.clone(); this.scale = null; } /** * Builds a simple converter for correlated residuals with the * specified weights. *

                              * The scalar objective function value is computed as: *

                                   * objective = yTy with y = scale×(observation-objective)
                                   * 
                              *

                              *

                              * The array computed by the objective function, the observations array and the * the scaling matrix must have consistent sizes or a {@link DimensionMismatchException} * will be triggered while computing the scalar objective. *

                              * * @param function vectorial residuals function to wrap * @param observations observations to be compared to objective function to compute residuals * @param scale scaling matrix * @throws DimensionMismatchException if the observations vector and the scale * matrix dimensions do not match (objective function dimension is checked only when * the {@link #value(double[])} method is called) */ public LeastSquaresConverter(final MultivariateVectorFunction function, final double[] observations, final RealMatrix scale) { if (observations.length != scale.getColumnDimension()) { throw new DimensionMismatchException(observations.length, scale.getColumnDimension()); } this.function = function; this.observations = observations.clone(); this.weights = null; this.scale = scale.copy(); } /** {@inheritDoc} */ public double value(final double[] point) { // compute residuals final double[] residuals = function.value(point); if (residuals.length != observations.length) { throw new DimensionMismatchException(residuals.length, observations.length); } for (int i = 0; i < residuals.length; ++i) { residuals[i] -= observations[i]; } // compute sum of squares double sumSquares = 0; if (weights != null) { for (int i = 0; i < residuals.length; ++i) { final double ri = residuals[i]; sumSquares += weights[i] * ri * ri; } } else if (scale != null) { for (final double yi : scale.operate(residuals)) { sumSquares += yi * yi; } } else { for (final double ri : residuals) { sumSquares += ri * ri; } } return sumSquares; } } ././@LongLink100644 0 0 157 12126630646 10264 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/optim/nonlinear/scalar/MultivariateOptimizer.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/optim/nonlinear/scalar/MultivariateOpti100644 1750 1750 10166 12126627713 32356 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.optim.nonlinear.scalar; import org.apache.commons.math3.analysis.MultivariateFunction; import org.apache.commons.math3.optim.BaseMultivariateOptimizer; import org.apache.commons.math3.optim.OptimizationData; import org.apache.commons.math3.optim.ConvergenceChecker; import org.apache.commons.math3.optim.PointValuePair; import org.apache.commons.math3.exception.TooManyEvaluationsException; /** * Base class for a multivariate scalar function optimizer. * * @version $Id: MultivariateOptimizer.java 1443444 2013-02-07 12:41:36Z erans $ * @since 3.1 */ public abstract class MultivariateOptimizer extends BaseMultivariateOptimizer { /** Objective function. */ private MultivariateFunction function; /** Type of optimization. */ private GoalType goal; /** * @param checker Convergence checker. */ protected MultivariateOptimizer(ConvergenceChecker checker) { super(checker); } /** * {@inheritDoc} * * @param optData Optimization data. In addition to those documented in * {@link BaseMultivariateOptimizer#parseOptimizationData(OptimizationData[]) * BaseMultivariateOptimizer}, this method will register the following data: *
                                *
                              • {@link ObjectiveFunction}
                              • *
                              • {@link GoalType}
                              • *
                              * @return {@inheritDoc} * @throws TooManyEvaluationsException if the maximal number of * evaluations is exceeded. */ @Override public PointValuePair optimize(OptimizationData... optData) throws TooManyEvaluationsException { // Set up base class and perform computation. return super.optimize(optData); } /** * Scans the list of (required and optional) optimization data that * characterize the problem. * * @param optData Optimization data. * The following data will be looked for: *
                                *
                              • {@link ObjectiveFunction}
                              • *
                              • {@link GoalType}
                              • *
                              */ @Override protected void parseOptimizationData(OptimizationData... optData) { // Allow base class to register its own data. super.parseOptimizationData(optData); // The existing values (as set by the previous call) are reused if // not provided in the argument list. for (OptimizationData data : optData) { if (data instanceof GoalType) { goal = (GoalType) data; continue; } if (data instanceof ObjectiveFunction) { function = ((ObjectiveFunction) data).getObjectiveFunction(); continue; } } } /** * @return the optimization type. */ public GoalType getGoalType() { return goal; } /** * Computes the objective function value. * This method must be called by subclasses to enforce the * evaluation counter limit. * * @param params Point at which the objective function must be evaluated. * @return the objective function value at the specified point. * @throws TooManyEvaluationsException if the maximal number of * evaluations is exceeded. */ protected double computeObjectiveValue(double[] params) { super.incrementEvaluationCount(); return function.value(params); } } ././@LongLink100644 0 0 171 12126630646 10260 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/optim/nonlinear/scalar/MultiStartMultivariateOptimizer.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/optim/nonlinear/scalar/MultiStartMultiv100644 1750 1750 7743 12126627713 32354 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.optim.nonlinear.scalar; import java.util.Collections; import java.util.List; import java.util.ArrayList; import java.util.Comparator; import org.apache.commons.math3.exception.NotStrictlyPositiveException; import org.apache.commons.math3.exception.NullArgumentException; import org.apache.commons.math3.random.RandomVectorGenerator; import org.apache.commons.math3.optim.BaseMultiStartMultivariateOptimizer; import org.apache.commons.math3.optim.PointValuePair; /** * Multi-start optimizer. * * This class wraps an optimizer in order to use it several times in * turn with different starting points (trying to avoid being trapped * in a local extremum when looking for a global one). * * @version $Id: MultiStartMultivariateOptimizer.java 1435539 2013-01-19 13:27:24Z tn $ * @since 3.0 */ public class MultiStartMultivariateOptimizer extends BaseMultiStartMultivariateOptimizer { /** Underlying optimizer. */ private final MultivariateOptimizer optimizer; /** Found optima. */ private final List optima = new ArrayList(); /** * Create a multi-start optimizer from a single-start optimizer. * * @param optimizer Single-start optimizer to wrap. * @param starts Number of starts to perform. * If {@code starts == 1}, the result will be same as if {@code optimizer} * is called directly. * @param generator Random vector generator to use for restarts. * @throws NullArgumentException if {@code optimizer} or {@code generator} * is {@code null}. * @throws NotStrictlyPositiveException if {@code starts < 1}. */ public MultiStartMultivariateOptimizer(final MultivariateOptimizer optimizer, final int starts, final RandomVectorGenerator generator) throws NullArgumentException, NotStrictlyPositiveException { super(optimizer, starts, generator); this.optimizer = optimizer; } /** * {@inheritDoc} */ @Override public PointValuePair[] getOptima() { Collections.sort(optima, getPairComparator()); return optima.toArray(new PointValuePair[0]); } /** * {@inheritDoc} */ @Override protected void store(PointValuePair optimum) { optima.add(optimum); } /** * {@inheritDoc} */ @Override protected void clear() { optima.clear(); } /** * @return a comparator for sorting the optima. */ private Comparator getPairComparator() { return new Comparator() { public int compare(final PointValuePair o1, final PointValuePair o2) { if (o1 == null) { return (o2 == null) ? 0 : 1; } else if (o2 == null) { return -1; } final double v1 = o1.getValue(); final double v2 = o2.getValue(); return (optimizer.getGoalType() == GoalType.MINIMIZE) ? Double.compare(v1, v2) : Double.compare(v2, v1); } }; } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/optim/SimplePointChecker.java100644 1750 1750 13071 12126627714 30271 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.optim; import org.apache.commons.math3.util.FastMath; import org.apache.commons.math3.util.Pair; import org.apache.commons.math3.exception.NotStrictlyPositiveException; /** * Simple implementation of the {@link ConvergenceChecker} interface using * only point coordinates. * * Convergence is considered to have been reached if either the relative * difference between each point coordinate are smaller than a threshold * or if either the absolute difference between the point coordinates are * smaller than another threshold. *
                              * The {@link #converged(int,Pair,Pair) converged} method will also return * {@code true} if the number of iterations has been set (see * {@link #SimplePointChecker(double,double,int) this constructor}). * * @param Type of the (point, value) pair. * The type of the "value" part of the pair (not used by this class). * * @version $Id: SimplePointChecker.java 1462503 2013-03-29 15:48:27Z luc $ * @since 3.0 */ public class SimplePointChecker> extends AbstractConvergenceChecker { /** * If {@link #maxIterationCount} is set to this value, the number of * iterations will never cause {@link #converged(int, Pair, Pair)} * to return {@code true}. */ private static final int ITERATION_CHECK_DISABLED = -1; /** * Number of iterations after which the * {@link #converged(int, Pair, Pair)} method * will return true (unless the check is disabled). */ private final int maxIterationCount; /** * Build an instance with specified thresholds. * In order to perform only relative checks, the absolute tolerance * must be set to a negative value. In order to perform only absolute * checks, the relative tolerance must be set to a negative value. * * @param relativeThreshold relative tolerance threshold * @param absoluteThreshold absolute tolerance threshold */ public SimplePointChecker(final double relativeThreshold, final double absoluteThreshold) { super(relativeThreshold, absoluteThreshold); maxIterationCount = ITERATION_CHECK_DISABLED; } /** * Builds an instance with specified thresholds. * In order to perform only relative checks, the absolute tolerance * must be set to a negative value. In order to perform only absolute * checks, the relative tolerance must be set to a negative value. * * @param relativeThreshold Relative tolerance threshold. * @param absoluteThreshold Absolute tolerance threshold. * @param maxIter Maximum iteration count. * @throws NotStrictlyPositiveException if {@code maxIter <= 0}. * * @since 3.1 */ public SimplePointChecker(final double relativeThreshold, final double absoluteThreshold, final int maxIter) { super(relativeThreshold, absoluteThreshold); if (maxIter <= 0) { throw new NotStrictlyPositiveException(maxIter); } maxIterationCount = maxIter; } /** * Check if the optimization algorithm has converged considering the * last two points. * This method may be called several times from the same algorithm * iteration with different points. This can be detected by checking the * iteration number at each call if needed. Each time this method is * called, the previous and current point correspond to points with the * same role at each iteration, so they can be compared. As an example, * simplex-based algorithms call this method for all points of the simplex, * not only for the best or worst ones. * * @param iteration Index of current iteration * @param previous Best point in the previous iteration. * @param current Best point in the current iteration. * @return {@code true} if the arguments satify the convergence criterion. */ @Override public boolean converged(final int iteration, final PAIR previous, final PAIR current) { if (maxIterationCount != ITERATION_CHECK_DISABLED && iteration >= maxIterationCount) { return true; } final double[] p = previous.getKey(); final double[] c = current.getKey(); for (int i = 0; i < p.length; ++i) { final double pi = p[i]; final double ci = c[i]; final double difference = FastMath.abs(pi - ci); final double size = FastMath.max(FastMath.abs(pi), FastMath.abs(ci)); if (difference > size * getRelativeThreshold() && difference > getAbsoluteThreshold()) { return false; } } return true; } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/optim/BaseOptimizer.java100644 1750 1750 16726 12126627714 27330 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.optim; import org.apache.commons.math3.util.Incrementor; import org.apache.commons.math3.exception.TooManyEvaluationsException; import org.apache.commons.math3.exception.TooManyIterationsException; /** * Base class for implementing optimizers. * It contains the boiler-plate code for counting the number of evaluations * of the objective function and the number of iterations of the algorithm, * and storing the convergence checker. * It is not a "user" class. * * @param Type of the point/value pair returned by the optimization * algorithm. * * @version $Id: BaseOptimizer.java 1458323 2013-03-19 14:51:30Z erans $ * @since 3.1 */ public abstract class BaseOptimizer { /** Evaluations counter. */ protected final Incrementor evaluations; /** Iterations counter. */ protected final Incrementor iterations; /** Convergence checker. */ private ConvergenceChecker checker; /** * @param checker Convergence checker. */ protected BaseOptimizer(ConvergenceChecker checker) { this.checker = checker; evaluations = new Incrementor(0, new MaxEvalCallback()); iterations = new Incrementor(Integer.MAX_VALUE, new MaxIterCallback()); } /** * Gets the maximal number of function evaluations. * * @return the maximal number of function evaluations. */ public int getMaxEvaluations() { return evaluations.getMaximalCount(); } /** * Gets the number of evaluations of the objective function. * The number of evaluations corresponds to the last call to the * {@code optimize} method. It is 0 if the method has not been * called yet. * * @return the number of evaluations of the objective function. */ public int getEvaluations() { return evaluations.getCount(); } /** * Gets the maximal number of iterations. * * @return the maximal number of iterations. */ public int getMaxIterations() { return iterations.getMaximalCount(); } /** * Gets the number of iterations performed by the algorithm. * The number iterations corresponds to the last call to the * {@code optimize} method. It is 0 if the method has not been * called yet. * * @return the number of evaluations of the objective function. */ public int getIterations() { return iterations.getCount(); } /** * Gets the convergence checker. * * @return the object used to check for convergence. */ public ConvergenceChecker getConvergenceChecker() { return checker; } /** * Stores data and performs the optimization. *
                              * The list of parameters is open-ended so that sub-classes can extend it * with arguments specific to their concrete implementations. *
                              * When the method is called multiple times, instance data is overwritten * only when actually present in the list of arguments: when not specified, * data set in a previous call is retained (and thus is optional in * subsequent calls). *
                              * Important note: Subclasses must override * {@link #parseOptimizationData(OptimizationData[])} if they need to register * their own options; but then, they must also call * {@code super.parseOptimizationData(optData)} within that method. * * @param optData Optimization data. * This method will register the following data: *
                                *
                              • {@link MaxEval}
                              • *
                              • {@link MaxIter}
                              • *
                              * @return a point/value pair that satifies the convergence criteria. * @throws TooManyEvaluationsException if the maximal number of * evaluations is exceeded. * @throws TooManyIterationsException if the maximal number of * iterations is exceeded. */ public PAIR optimize(OptimizationData... optData) throws TooManyEvaluationsException, TooManyIterationsException { // Parse options. parseOptimizationData(optData); // Reset counters. evaluations.resetCount(); iterations.resetCount(); // Perform optimization. return doOptimize(); } /** * Performs the bulk of the optimization algorithm. * * @return the point/value pair giving the optimal value of the * objective function. */ protected abstract PAIR doOptimize(); /** * Increment the evaluation count. * * @throws TooManyEvaluationsException if the allowed evaluations * have been exhausted. */ protected void incrementEvaluationCount() throws TooManyEvaluationsException { evaluations.incrementCount(); } /** * Increment the iteration count. * * @throws TooManyIterationsException if the allowed iterations * have been exhausted. */ protected void incrementIterationCount() throws TooManyIterationsException { iterations.incrementCount(); } /** * Scans the list of (required and optional) optimization data that * characterize the problem. * * @param optData Optimization data. * The following data will be looked for: *
                                *
                              • {@link MaxEval}
                              • *
                              • {@link MaxIter}
                              • *
                              */ protected void parseOptimizationData(OptimizationData... optData) { // The existing values (as set by the previous call) are reused if // not provided in the argument list. for (OptimizationData data : optData) { if (data instanceof MaxEval) { evaluations.setMaximalCount(((MaxEval) data).getMaxEval()); continue; } if (data instanceof MaxIter) { iterations.setMaximalCount(((MaxIter) data).getMaxIter()); continue; } } } /** * Defines the action to perform when reaching the maximum number * of evaluations. */ private static class MaxEvalCallback implements Incrementor.MaxCountExceededCallback { /** * {@inheritDoc} * @throws TooManyEvaluationsException. */ public void trigger(int max) { throw new TooManyEvaluationsException(max); } } /** * Defines the action to perform when reaching the maximum number * of evaluations. */ private static class MaxIterCallback implements Incrementor.MaxCountExceededCallback { /** * {@inheritDoc} * @throws TooManyIterationsException. */ public void trigger(int max) { throw new TooManyIterationsException(max); } } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/optim/MaxEval.java100644 1750 1750 3724 12126627714 26062 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.optim; import org.apache.commons.math3.exception.NotStrictlyPositiveException; /** * Maximum number of evaluations of the function to be optimized. * * @version $Id: MaxEval.java 1435539 2013-01-19 13:27:24Z tn $ * @since 3.1 */ public class MaxEval implements OptimizationData { /** Allowed number of evalutations. */ private final int maxEval; /** * @param max Allowed number of evalutations. * @throws NotStrictlyPositiveException if {@code max <= 0}. */ public MaxEval(int max) { if (max <= 0) { throw new NotStrictlyPositiveException(max); } maxEval = max; } /** * Gets the maximum number of evaluations. * * @return the allowed number of evaluations. */ public int getMaxEval() { return maxEval; } /** * Factory method that creates instance of this class that represents * a virtually unlimited number of evaluations. * * @return a new instance suitable for allowing {@link Integer#MAX_VALUE} * evaluations. */ public static MaxEval unlimited() { return new MaxEval(Integer.MAX_VALUE); } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/optim/univariate/package-info.java100644 1750 1750 1613 12126627713 31212 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ /** * One-dimensional optimization algorithms. */ package org.apache.commons.math3.optim.univariate; commons-math3-3.2-src/src/main/java/org/apache/commons/math3/optim/univariate/BracketFinder.java100644 1750 1750 17345 12126627713 31422 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.optim.univariate; import org.apache.commons.math3.util.Incrementor; import org.apache.commons.math3.exception.NotStrictlyPositiveException; import org.apache.commons.math3.exception.TooManyEvaluationsException; import org.apache.commons.math3.exception.MaxCountExceededException; import org.apache.commons.math3.analysis.UnivariateFunction; import org.apache.commons.math3.optim.nonlinear.scalar.GoalType; /** * Provide an interval that brackets a local optimum of a function. * This code is based on a Python implementation (from SciPy, * module {@code optimize.py} v0.5). * * @version $Id: BracketFinder.java 1435539 2013-01-19 13:27:24Z tn $ * @since 2.2 */ public class BracketFinder { /** Tolerance to avoid division by zero. */ private static final double EPS_MIN = 1e-21; /** * Golden section. */ private static final double GOLD = 1.618034; /** * Factor for expanding the interval. */ private final double growLimit; /** * Counter for function evaluations. */ private final Incrementor evaluations = new Incrementor(); /** * Lower bound of the bracket. */ private double lo; /** * Higher bound of the bracket. */ private double hi; /** * Point inside the bracket. */ private double mid; /** * Function value at {@link #lo}. */ private double fLo; /** * Function value at {@link #hi}. */ private double fHi; /** * Function value at {@link #mid}. */ private double fMid; /** * Constructor with default values {@code 100, 50} (see the * {@link #BracketFinder(double,int) other constructor}). */ public BracketFinder() { this(100, 50); } /** * Create a bracketing interval finder. * * @param growLimit Expanding factor. * @param maxEvaluations Maximum number of evaluations allowed for finding * a bracketing interval. */ public BracketFinder(double growLimit, int maxEvaluations) { if (growLimit <= 0) { throw new NotStrictlyPositiveException(growLimit); } if (maxEvaluations <= 0) { throw new NotStrictlyPositiveException(maxEvaluations); } this.growLimit = growLimit; evaluations.setMaximalCount(maxEvaluations); } /** * Search new points that bracket a local optimum of the function. * * @param func Function whose optimum should be bracketed. * @param goal {@link GoalType Goal type}. * @param xA Initial point. * @param xB Initial point. * @throws TooManyEvaluationsException if the maximum number of evaluations * is exceeded. */ public void search(UnivariateFunction func, GoalType goal, double xA, double xB) { evaluations.resetCount(); final boolean isMinim = goal == GoalType.MINIMIZE; double fA = eval(func, xA); double fB = eval(func, xB); if (isMinim ? fA < fB : fA > fB) { double tmp = xA; xA = xB; xB = tmp; tmp = fA; fA = fB; fB = tmp; } double xC = xB + GOLD * (xB - xA); double fC = eval(func, xC); while (isMinim ? fC < fB : fC > fB) { double tmp1 = (xB - xA) * (fB - fC); double tmp2 = (xB - xC) * (fB - fA); double val = tmp2 - tmp1; double denom = Math.abs(val) < EPS_MIN ? 2 * EPS_MIN : 2 * val; double w = xB - ((xB - xC) * tmp2 - (xB - xA) * tmp1) / denom; double wLim = xB + growLimit * (xC - xB); double fW; if ((w - xC) * (xB - w) > 0) { fW = eval(func, w); if (isMinim ? fW < fC : fW > fC) { xA = xB; xB = w; fA = fB; fB = fW; break; } else if (isMinim ? fW > fB : fW < fB) { xC = w; fC = fW; break; } w = xC + GOLD * (xC - xB); fW = eval(func, w); } else if ((w - wLim) * (wLim - xC) >= 0) { w = wLim; fW = eval(func, w); } else if ((w - wLim) * (xC - w) > 0) { fW = eval(func, w); if (isMinim ? fW < fC : fW > fC) { xB = xC; xC = w; w = xC + GOLD * (xC - xB); fB = fC; fC =fW; fW = eval(func, w); } } else { w = xC + GOLD * (xC - xB); fW = eval(func, w); } xA = xB; fA = fB; xB = xC; fB = fC; xC = w; fC = fW; } lo = xA; fLo = fA; mid = xB; fMid = fB; hi = xC; fHi = fC; if (lo > hi) { double tmp = lo; lo = hi; hi = tmp; tmp = fLo; fLo = fHi; fHi = tmp; } } /** * @return the number of evalutations. */ public int getMaxEvaluations() { return evaluations.getMaximalCount(); } /** * @return the number of evalutations. */ public int getEvaluations() { return evaluations.getCount(); } /** * @return the lower bound of the bracket. * @see #getFLo() */ public double getLo() { return lo; } /** * Get function value at {@link #getLo()}. * @return function value at {@link #getLo()} */ public double getFLo() { return fLo; } /** * @return the higher bound of the bracket. * @see #getFHi() */ public double getHi() { return hi; } /** * Get function value at {@link #getHi()}. * @return function value at {@link #getHi()} */ public double getFHi() { return fHi; } /** * @return a point in the middle of the bracket. * @see #getFMid() */ public double getMid() { return mid; } /** * Get function value at {@link #getMid()}. * @return function value at {@link #getMid()} */ public double getFMid() { return fMid; } /** * @param f Function. * @param x Argument. * @return {@code f(x)} * @throws TooManyEvaluationsException if the maximal number of evaluations is * exceeded. */ private double eval(UnivariateFunction f, double x) { try { evaluations.incrementCount(); } catch (MaxCountExceededException e) { throw new TooManyEvaluationsException(e.getMax()); } return f.value(x); } } ././@LongLink100644 0 0 160 12126630646 10256 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/optim/univariate/SimpleUnivariateValueChecker.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/optim/univariate/SimpleUnivariateValueC100644 1750 1750 13036 12126627713 32351 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.optim.univariate; import org.apache.commons.math3.util.FastMath; import org.apache.commons.math3.exception.NotStrictlyPositiveException; import org.apache.commons.math3.optim.AbstractConvergenceChecker; /** * Simple implementation of the * {@link org.apache.commons.math3.optimization.ConvergenceChecker} interface * that uses only objective function values. * * Convergence is considered to have been reached if either the relative * difference between the objective function values is smaller than a * threshold or if either the absolute difference between the objective * function values is smaller than another threshold. *
                              * The {@link #converged(int,UnivariatePointValuePair,UnivariatePointValuePair) * converged} method will also return {@code true} if the number of iterations * has been set (see {@link #SimpleUnivariateValueChecker(double,double,int) * this constructor}). * * @version $Id: SimpleUnivariateValueChecker.java 1462503 2013-03-29 15:48:27Z luc $ * @since 3.1 */ public class SimpleUnivariateValueChecker extends AbstractConvergenceChecker { /** * If {@link #maxIterationCount} is set to this value, the number of * iterations will never cause * {@link #converged(int,UnivariatePointValuePair,UnivariatePointValuePair)} * to return {@code true}. */ private static final int ITERATION_CHECK_DISABLED = -1; /** * Number of iterations after which the * {@link #converged(int,UnivariatePointValuePair,UnivariatePointValuePair)} * method will return true (unless the check is disabled). */ private final int maxIterationCount; /** Build an instance with specified thresholds. * * In order to perform only relative checks, the absolute tolerance * must be set to a negative value. In order to perform only absolute * checks, the relative tolerance must be set to a negative value. * * @param relativeThreshold relative tolerance threshold * @param absoluteThreshold absolute tolerance threshold */ public SimpleUnivariateValueChecker(final double relativeThreshold, final double absoluteThreshold) { super(relativeThreshold, absoluteThreshold); maxIterationCount = ITERATION_CHECK_DISABLED; } /** * Builds an instance with specified thresholds. * * In order to perform only relative checks, the absolute tolerance * must be set to a negative value. In order to perform only absolute * checks, the relative tolerance must be set to a negative value. * * @param relativeThreshold relative tolerance threshold * @param absoluteThreshold absolute tolerance threshold * @param maxIter Maximum iteration count. * @throws NotStrictlyPositiveException if {@code maxIter <= 0}. * * @since 3.1 */ public SimpleUnivariateValueChecker(final double relativeThreshold, final double absoluteThreshold, final int maxIter) { super(relativeThreshold, absoluteThreshold); if (maxIter <= 0) { throw new NotStrictlyPositiveException(maxIter); } maxIterationCount = maxIter; } /** * Check if the optimization algorithm has converged considering the * last two points. * This method may be called several time from the same algorithm * iteration with different points. This can be detected by checking the * iteration number at each call if needed. Each time this method is * called, the previous and current point correspond to points with the * same role at each iteration, so they can be compared. As an example, * simplex-based algorithms call this method for all points of the simplex, * not only for the best or worst ones. * * @param iteration Index of current iteration * @param previous Best point in the previous iteration. * @param current Best point in the current iteration. * @return {@code true} if the algorithm has converged. */ @Override public boolean converged(final int iteration, final UnivariatePointValuePair previous, final UnivariatePointValuePair current) { if (maxIterationCount != ITERATION_CHECK_DISABLED && iteration >= maxIterationCount) { return true; } final double p = previous.getValue(); final double c = current.getValue(); final double difference = FastMath.abs(p - c); final double size = FastMath.max(FastMath.abs(p), FastMath.abs(c)); return difference <= size * getRelativeThreshold() || difference <= getAbsoluteThreshold(); } } ././@LongLink100644 0 0 157 12126630646 10264 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/optim/univariate/UnivariateObjectiveFunction.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/optim/univariate/UnivariateObjectiveFun100644 1750 1750 3120 12126627713 32354 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.optim.univariate; import org.apache.commons.math3.analysis.UnivariateFunction; import org.apache.commons.math3.optim.OptimizationData; /** * Scalar function to be optimized. * * @version $Id: UnivariateObjectiveFunction.java 1435539 2013-01-19 13:27:24Z tn $ * @since 3.1 */ public class UnivariateObjectiveFunction implements OptimizationData { /** Function to be optimized. */ private final UnivariateFunction function; /** * @param f Function to be optimized. */ public UnivariateObjectiveFunction(UnivariateFunction f) { function = f; } /** * Gets the function to be optimized. * * @return the objective function. */ public UnivariateFunction getObjectiveFunction() { return function; } } ././@LongLink100644 0 0 161 12126630646 10257 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/optim/univariate/MultiStartUnivariateOptimizer.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/optim/univariate/MultiStartUnivariateOp100644 1750 1750 21343 12126627713 32427 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.optim.univariate; import java.util.Arrays; import java.util.Comparator; import org.apache.commons.math3.exception.MathIllegalStateException; import org.apache.commons.math3.exception.NotStrictlyPositiveException; import org.apache.commons.math3.exception.util.LocalizedFormats; import org.apache.commons.math3.random.RandomGenerator; import org.apache.commons.math3.optim.MaxEval; import org.apache.commons.math3.optim.nonlinear.scalar.GoalType; import org.apache.commons.math3.optim.OptimizationData; /** * Special implementation of the {@link UnivariateOptimizer} interface * adding multi-start features to an existing optimizer. *
                              * This class wraps an optimizer in order to use it several times in * turn with different starting points (trying to avoid being trapped * in a local extremum when looking for a global one). * * @version $Id: MultiStartUnivariateOptimizer.java 1435539 2013-01-19 13:27:24Z tn $ * @since 3.0 */ public class MultiStartUnivariateOptimizer extends UnivariateOptimizer { /** Underlying classical optimizer. */ private final UnivariateOptimizer optimizer; /** Number of evaluations already performed for all starts. */ private int totalEvaluations; /** Number of starts to go. */ private int starts; /** Random generator for multi-start. */ private RandomGenerator generator; /** Found optima. */ private UnivariatePointValuePair[] optima; /** Optimization data. */ private OptimizationData[] optimData; /** * Location in {@link #optimData} where the updated maximum * number of evaluations will be stored. */ private int maxEvalIndex = -1; /** * Location in {@link #optimData} where the updated start value * will be stored. */ private int searchIntervalIndex = -1; /** * Create a multi-start optimizer from a single-start optimizer. * * @param optimizer Single-start optimizer to wrap. * @param starts Number of starts to perform. If {@code starts == 1}, * the {@code optimize} methods will return the same solution as * {@code optimizer} would. * @param generator Random generator to use for restarts. * @throws NotStrictlyPositiveException if {@code starts < 1}. */ public MultiStartUnivariateOptimizer(final UnivariateOptimizer optimizer, final int starts, final RandomGenerator generator) { super(optimizer.getConvergenceChecker()); if (starts < 1) { throw new NotStrictlyPositiveException(starts); } this.optimizer = optimizer; this.starts = starts; this.generator = generator; } /** {@inheritDoc} */ @Override public int getEvaluations() { return totalEvaluations; } /** * Gets all the optima found during the last call to {@code optimize}. * The optimizer stores all the optima found during a set of * restarts. The {@code optimize} method returns the best point only. * This method returns all the points found at the end of each starts, * including the best one already returned by the {@code optimize} method. *
                              * The returned array as one element for each start as specified * in the constructor. It is ordered with the results from the * runs that did converge first, sorted from best to worst * objective value (i.e in ascending order if minimizing and in * descending order if maximizing), followed by {@code null} elements * corresponding to the runs that did not converge. This means all * elements will be {@code null} if the {@code optimize} method did throw * an exception. * This also means that if the first element is not {@code null}, it is * the best point found across all starts. * * @return an array containing the optima. * @throws MathIllegalStateException if {@link #optimize(OptimizationData[]) * optimize} has not been called. */ public UnivariatePointValuePair[] getOptima() { if (optima == null) { throw new MathIllegalStateException(LocalizedFormats.NO_OPTIMUM_COMPUTED_YET); } return optima.clone(); } /** * {@inheritDoc} * * @throws MathIllegalStateException if {@code optData} does not contain an * instance of {@link MaxEval} or {@link SearchInterval}. */ @Override public UnivariatePointValuePair optimize(OptimizationData... optData) { // Store arguments in order to pass them to the internal optimizer. optimData = optData; // Set up base class and perform computations. return super.optimize(optData); } /** {@inheritDoc} */ @Override protected UnivariatePointValuePair doOptimize() { // Remove all instances of "MaxEval" and "SearchInterval" from the // array that will be passed to the internal optimizer. // The former is to enforce smaller numbers of allowed evaluations // (according to how many have been used up already), and the latter // to impose a different start value for each start. for (int i = 0; i < optimData.length; i++) { if (optimData[i] instanceof MaxEval) { optimData[i] = null; maxEvalIndex = i; continue; } if (optimData[i] instanceof SearchInterval) { optimData[i] = null; searchIntervalIndex = i; continue; } } if (maxEvalIndex == -1) { throw new MathIllegalStateException(); } if (searchIntervalIndex == -1) { throw new MathIllegalStateException(); } RuntimeException lastException = null; optima = new UnivariatePointValuePair[starts]; totalEvaluations = 0; final int maxEval = getMaxEvaluations(); final double min = getMin(); final double max = getMax(); final double startValue = getStartValue(); // Multi-start loop. for (int i = 0; i < starts; i++) { // CHECKSTYLE: stop IllegalCatch try { // Decrease number of allowed evaluations. optimData[maxEvalIndex] = new MaxEval(maxEval - totalEvaluations); // New start value. final double s = (i == 0) ? startValue : min + generator.nextDouble() * (max - min); optimData[searchIntervalIndex] = new SearchInterval(min, max, s); // Optimize. optima[i] = optimizer.optimize(optimData); } catch (RuntimeException mue) { lastException = mue; optima[i] = null; } // CHECKSTYLE: resume IllegalCatch totalEvaluations += optimizer.getEvaluations(); } sortPairs(getGoalType()); if (optima[0] == null) { throw lastException; // Cannot be null if starts >= 1. } // Return the point with the best objective function value. return optima[0]; } /** * Sort the optima from best to worst, followed by {@code null} elements. * * @param goal Goal type. */ private void sortPairs(final GoalType goal) { Arrays.sort(optima, new Comparator() { public int compare(final UnivariatePointValuePair o1, final UnivariatePointValuePair o2) { if (o1 == null) { return (o2 == null) ? 0 : 1; } else if (o2 == null) { return -1; } final double v1 = o1.getValue(); final double v2 = o2.getValue(); return (goal == GoalType.MINIMIZE) ? Double.compare(v1, v2) : Double.compare(v2, v1); } }); } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/optim/univariate/SearchInterval.java100644 1750 1750 5337 12126627713 31607 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.optim.univariate; import org.apache.commons.math3.optim.OptimizationData; import org.apache.commons.math3.exception.NumberIsTooLargeException; import org.apache.commons.math3.exception.OutOfRangeException; /** * Search interval and (optional) start value. *
                              * Immutable class. * * @version $Id: SearchInterval.java 1435539 2013-01-19 13:27:24Z tn $ * @since 3.1 */ public class SearchInterval implements OptimizationData { /** Lower bound. */ private final double lower; /** Upper bound. */ private final double upper; /** Start value. */ private final double start; /** * @param lo Lower bound. * @param hi Upper bound. * @param init Start value. * @throws NumberIsTooLargeException if {@code lo >= hi}. * @throws OutOfRangeException if {@code init < lo} or {@code init > hi}. */ public SearchInterval(double lo, double hi, double init) { if (lo >= hi) { throw new NumberIsTooLargeException(lo, hi, false); } if (init < lo || init > hi) { throw new OutOfRangeException(init, lo, hi); } lower = lo; upper = hi; start = init; } /** * @param lo Lower bound. * @param hi Upper bound. * @throws NumberIsTooLargeException if {@code lo >= hi}. */ public SearchInterval(double lo, double hi) { this(lo, hi, 0.5 * (lo + hi)); } /** * Gets the lower bound. * * @return the lower bound. */ public double getMin() { return lower; } /** * Gets the upper bound. * * @return the upper bound. */ public double getMax() { return upper; } /** * Gets the start value. * * @return the start value. */ public double getStartValue() { return start; } } ././@LongLink100644 0 0 154 12126630646 10261 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/optim/univariate/UnivariatePointValuePair.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/optim/univariate/UnivariatePointValuePa100644 1750 1750 4066 12126627713 32352 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.optim.univariate; import java.io.Serializable; /** * This class holds a point and the value of an objective function at this * point. * This is a simple immutable container. * * @version $Id: UnivariatePointValuePair.java 1435539 2013-01-19 13:27:24Z tn $ * @since 3.0 */ public class UnivariatePointValuePair implements Serializable { /** Serializable version identifier. */ private static final long serialVersionUID = 1003888396256744753L; /** Point. */ private final double point; /** Value of the objective function at the point. */ private final double value; /** * Build a point/objective function value pair. * * @param point Point. * @param value Value of an objective function at the point */ public UnivariatePointValuePair(final double point, final double value) { this.point = point; this.value = value; } /** * Get the point. * * @return the point. */ public double getPoint() { return point; } /** * Get the value of the objective function. * * @return the stored value of the objective function. */ public double getValue() { return value; } } ././@LongLink100644 0 0 147 12126630646 10263 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/optim/univariate/UnivariateOptimizer.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/optim/univariate/UnivariateOptimizer.ja100644 1750 1750 11634 12126627713 32375 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.optim.univariate; import org.apache.commons.math3.analysis.UnivariateFunction; import org.apache.commons.math3.optim.BaseOptimizer; import org.apache.commons.math3.optim.OptimizationData; import org.apache.commons.math3.optim.nonlinear.scalar.GoalType; import org.apache.commons.math3.optim.ConvergenceChecker; import org.apache.commons.math3.exception.TooManyEvaluationsException; /** * Base class for a univariate scalar function optimizer. * * @version $Id: UnivariateOptimizer.java 1443444 2013-02-07 12:41:36Z erans $ * @since 3.1 */ public abstract class UnivariateOptimizer extends BaseOptimizer { /** Objective function. */ private UnivariateFunction function; /** Type of optimization. */ private GoalType goal; /** Initial guess. */ private double start; /** Lower bound. */ private double min; /** Upper bound. */ private double max; /** * @param checker Convergence checker. */ protected UnivariateOptimizer(ConvergenceChecker checker) { super(checker); } /** * {@inheritDoc} * * @param optData Optimization data. In addition to those documented in * {@link BaseOptimizer#parseOptimizationData(OptimizationData[]) * BaseOptimizer}, this method will register the following data: *
                                *
                              • {@link GoalType}
                              • *
                              • {@link SearchInterval}
                              • *
                              • {@link UnivariateObjectiveFunction}
                              • *
                              * @return {@inheritDoc} * @throws TooManyEvaluationsException if the maximal number of * evaluations is exceeded. */ public UnivariatePointValuePair optimize(OptimizationData... optData) throws TooManyEvaluationsException { // Perform computation. return super.optimize(optData); } /** * @return the optimization type. */ public GoalType getGoalType() { return goal; } /** * Scans the list of (required and optional) optimization data that * characterize the problem. * * @param optData Optimization data. * The following data will be looked for: *
                                *
                              • {@link GoalType}
                              • *
                              • {@link SearchInterval}
                              • *
                              • {@link UnivariateObjectiveFunction}
                              • *
                              */ @Override protected void parseOptimizationData(OptimizationData... optData) { // Allow base class to register its own data. super.parseOptimizationData(optData); // The existing values (as set by the previous call) are reused if // not provided in the argument list. for (OptimizationData data : optData) { if (data instanceof SearchInterval) { final SearchInterval interval = (SearchInterval) data; min = interval.getMin(); max = interval.getMax(); start = interval.getStartValue(); continue; } if (data instanceof UnivariateObjectiveFunction) { function = ((UnivariateObjectiveFunction) data).getObjectiveFunction(); continue; } if (data instanceof GoalType) { goal = (GoalType) data; continue; } } } /** * @return the initial guess. */ public double getStartValue() { return start; } /** * @return the lower bounds. */ public double getMin() { return min; } /** * @return the upper bounds. */ public double getMax() { return max; } /** * Computes the objective function value. * This method must be called by subclasses to enforce the * evaluation counter limit. * * @param x Point at which the objective function must be evaluated. * @return the objective function value at the specified point. * @throws TooManyEvaluationsException if the maximal number of * evaluations is exceeded. */ protected double computeObjectiveValue(double x) { super.incrementEvaluationCount(); return function.value(x); } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/optim/univariate/BrentOptimizer.java100644 1750 1750 25371 12126627713 31672 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.optim.univariate; import org.apache.commons.math3.util.Precision; import org.apache.commons.math3.util.FastMath; import org.apache.commons.math3.exception.NumberIsTooSmallException; import org.apache.commons.math3.exception.NotStrictlyPositiveException; import org.apache.commons.math3.optim.ConvergenceChecker; import org.apache.commons.math3.optim.nonlinear.scalar.GoalType; /** * For a function defined on some interval {@code (lo, hi)}, this class * finds an approximation {@code x} to the point at which the function * attains its minimum. * It implements Richard Brent's algorithm (from his book "Algorithms for * Minimization without Derivatives", p. 79) for finding minima of real * univariate functions. *
                              * This code is an adaptation, partly based on the Python code from SciPy * (module "optimize.py" v0.5); the original algorithm is also modified *
                                *
                              • to use an initial guess provided by the user,
                              • *
                              • to ensure that the best point encountered is the one returned.
                              • *
                              * * @version $Id: BrentOptimizer.java 1462503 2013-03-29 15:48:27Z luc $ * @since 2.0 */ public class BrentOptimizer extends UnivariateOptimizer { /** * Golden section. */ private static final double GOLDEN_SECTION = 0.5 * (3 - FastMath.sqrt(5)); /** * Minimum relative tolerance. */ private static final double MIN_RELATIVE_TOLERANCE = 2 * FastMath.ulp(1d); /** * Relative threshold. */ private final double relativeThreshold; /** * Absolute threshold. */ private final double absoluteThreshold; /** * The arguments are used implement the original stopping criterion * of Brent's algorithm. * {@code abs} and {@code rel} define a tolerance * {@code tol = rel |x| + abs}. {@code rel} should be no smaller than * 2 macheps and preferably not much less than sqrt(macheps), * where macheps is the relative machine precision. {@code abs} must * be positive. * * @param rel Relative threshold. * @param abs Absolute threshold. * @param checker Additional, user-defined, convergence checking * procedure. * @throws NotStrictlyPositiveException if {@code abs <= 0}. * @throws NumberIsTooSmallException if {@code rel < 2 * Math.ulp(1d)}. */ public BrentOptimizer(double rel, double abs, ConvergenceChecker checker) { super(checker); if (rel < MIN_RELATIVE_TOLERANCE) { throw new NumberIsTooSmallException(rel, MIN_RELATIVE_TOLERANCE, true); } if (abs <= 0) { throw new NotStrictlyPositiveException(abs); } relativeThreshold = rel; absoluteThreshold = abs; } /** * The arguments are used for implementing the original stopping criterion * of Brent's algorithm. * {@code abs} and {@code rel} define a tolerance * {@code tol = rel |x| + abs}. {@code rel} should be no smaller than * 2 macheps and preferably not much less than sqrt(macheps), * where macheps is the relative machine precision. {@code abs} must * be positive. * * @param rel Relative threshold. * @param abs Absolute threshold. * @throws NotStrictlyPositiveException if {@code abs <= 0}. * @throws NumberIsTooSmallException if {@code rel < 2 * Math.ulp(1d)}. */ public BrentOptimizer(double rel, double abs) { this(rel, abs, null); } /** {@inheritDoc} */ @Override protected UnivariatePointValuePair doOptimize() { final boolean isMinim = getGoalType() == GoalType.MINIMIZE; final double lo = getMin(); final double mid = getStartValue(); final double hi = getMax(); // Optional additional convergence criteria. final ConvergenceChecker checker = getConvergenceChecker(); double a; double b; if (lo < hi) { a = lo; b = hi; } else { a = hi; b = lo; } double x = mid; double v = x; double w = x; double d = 0; double e = 0; double fx = computeObjectiveValue(x); if (!isMinim) { fx = -fx; } double fv = fx; double fw = fx; UnivariatePointValuePair previous = null; UnivariatePointValuePair current = new UnivariatePointValuePair(x, isMinim ? fx : -fx); // Best point encountered so far (which is the initial guess). UnivariatePointValuePair best = current; int iter = 0; while (true) { final double m = 0.5 * (a + b); final double tol1 = relativeThreshold * FastMath.abs(x) + absoluteThreshold; final double tol2 = 2 * tol1; // Default stopping criterion. final boolean stop = FastMath.abs(x - m) <= tol2 - 0.5 * (b - a); if (!stop) { double p = 0; double q = 0; double r = 0; double u = 0; if (FastMath.abs(e) > tol1) { // Fit parabola. r = (x - w) * (fx - fv); q = (x - v) * (fx - fw); p = (x - v) * q - (x - w) * r; q = 2 * (q - r); if (q > 0) { p = -p; } else { q = -q; } r = e; e = d; if (p > q * (a - x) && p < q * (b - x) && FastMath.abs(p) < FastMath.abs(0.5 * q * r)) { // Parabolic interpolation step. d = p / q; u = x + d; // f must not be evaluated too close to a or b. if (u - a < tol2 || b - u < tol2) { if (x <= m) { d = tol1; } else { d = -tol1; } } } else { // Golden section step. if (x < m) { e = b - x; } else { e = a - x; } d = GOLDEN_SECTION * e; } } else { // Golden section step. if (x < m) { e = b - x; } else { e = a - x; } d = GOLDEN_SECTION * e; } // Update by at least "tol1". if (FastMath.abs(d) < tol1) { if (d >= 0) { u = x + tol1; } else { u = x - tol1; } } else { u = x + d; } double fu = computeObjectiveValue(u); if (!isMinim) { fu = -fu; } // User-defined convergence checker. previous = current; current = new UnivariatePointValuePair(u, isMinim ? fu : -fu); best = best(best, best(previous, current, isMinim), isMinim); if (checker != null && checker.converged(iter, previous, current)) { return best; } // Update a, b, v, w and x. if (fu <= fx) { if (u < x) { b = x; } else { a = x; } v = w; fv = fw; w = x; fw = fx; x = u; fx = fu; } else { if (u < x) { a = u; } else { b = u; } if (fu <= fw || Precision.equals(w, x)) { v = w; fv = fw; w = u; fw = fu; } else if (fu <= fv || Precision.equals(v, x) || Precision.equals(v, w)) { v = u; fv = fu; } } } else { // Default termination (Brent's criterion). return best(best, best(previous, current, isMinim), isMinim); } ++iter; } } /** * Selects the best of two points. * * @param a Point and value. * @param b Point and value. * @param isMinim {@code true} if the selected point must be the one with * the lowest value. * @return the best point, or {@code null} if {@code a} and {@code b} are * both {@code null}. When {@code a} and {@code b} have the same function * value, {@code a} is returned. */ private UnivariatePointValuePair best(UnivariatePointValuePair a, UnivariatePointValuePair b, boolean isMinim) { if (a == null) { return b; } if (b == null) { return a; } if (isMinim) { return a.getValue() <= b.getValue() ? a : b; } else { return a.getValue() >= b.getValue() ? a : b; } } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/optim/OptimizationData.java100644 1750 1750 2326 12126627714 30002 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.optim; /** * Marker interface. * Implementations will provide functionality (optional or required) needed * by the optimizers, and those will need to check the actual type of the * arguments and perform the appropriate cast in order to access the data * they need. * * @version $Id: OptimizationData.java 1435539 2013-01-19 13:27:24Z tn $ * @since 3.1 */ public interface OptimizationData {} commons-math3-3.2-src/src/main/java/org/apache/commons/math3/optim/AbstractConvergenceChecker.java100644 1750 1750 4350 12126627714 31730 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.optim; /** * Base class for all convergence checker implementations. * * @param Type of (point, value) pair. * * @version $Id: AbstractConvergenceChecker.java 1435539 2013-01-19 13:27:24Z tn $ * @since 3.0 */ public abstract class AbstractConvergenceChecker implements ConvergenceChecker { /** * Relative tolerance threshold. */ private final double relativeThreshold; /** * Absolute tolerance threshold. */ private final double absoluteThreshold; /** * Build an instance with a specified thresholds. * * @param relativeThreshold relative tolerance threshold * @param absoluteThreshold absolute tolerance threshold */ public AbstractConvergenceChecker(final double relativeThreshold, final double absoluteThreshold) { this.relativeThreshold = relativeThreshold; this.absoluteThreshold = absoluteThreshold; } /** * @return the relative threshold. */ public double getRelativeThreshold() { return relativeThreshold; } /** * @return the absolute threshold. */ public double getAbsoluteThreshold() { return absoluteThreshold; } /** * {@inheritDoc} */ public abstract boolean converged(int iteration, PAIR previous, PAIR current); } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/optim/SimpleValueChecker.java100644 1750 1750 12370 12126627714 30255 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.optim; import org.apache.commons.math3.util.FastMath; import org.apache.commons.math3.exception.NotStrictlyPositiveException; /** * Simple implementation of the {@link ConvergenceChecker} interface using * only objective function values. * * Convergence is considered to have been reached if either the relative * difference between the objective function values is smaller than a * threshold or if either the absolute difference between the objective * function values is smaller than another threshold. *
                              * The {@link #converged(int,PointValuePair,PointValuePair) converged} * method will also return {@code true} if the number of iterations has been set * (see {@link #SimpleValueChecker(double,double,int) this constructor}). * * @version $Id: SimpleValueChecker.java 1462503 2013-03-29 15:48:27Z luc $ * @since 3.0 */ public class SimpleValueChecker extends AbstractConvergenceChecker { /** * If {@link #maxIterationCount} is set to this value, the number of * iterations will never cause * {@link #converged(int,PointValuePair,PointValuePair)} * to return {@code true}. */ private static final int ITERATION_CHECK_DISABLED = -1; /** * Number of iterations after which the * {@link #converged(int,PointValuePair,PointValuePair)} method * will return true (unless the check is disabled). */ private final int maxIterationCount; /** Build an instance with specified thresholds. * * In order to perform only relative checks, the absolute tolerance * must be set to a negative value. In order to perform only absolute * checks, the relative tolerance must be set to a negative value. * * @param relativeThreshold relative tolerance threshold * @param absoluteThreshold absolute tolerance threshold */ public SimpleValueChecker(final double relativeThreshold, final double absoluteThreshold) { super(relativeThreshold, absoluteThreshold); maxIterationCount = ITERATION_CHECK_DISABLED; } /** * Builds an instance with specified thresholds. * * In order to perform only relative checks, the absolute tolerance * must be set to a negative value. In order to perform only absolute * checks, the relative tolerance must be set to a negative value. * * @param relativeThreshold relative tolerance threshold * @param absoluteThreshold absolute tolerance threshold * @param maxIter Maximum iteration count. * @throws NotStrictlyPositiveException if {@code maxIter <= 0}. * * @since 3.1 */ public SimpleValueChecker(final double relativeThreshold, final double absoluteThreshold, final int maxIter) { super(relativeThreshold, absoluteThreshold); if (maxIter <= 0) { throw new NotStrictlyPositiveException(maxIter); } maxIterationCount = maxIter; } /** * Check if the optimization algorithm has converged considering the * last two points. * This method may be called several time from the same algorithm * iteration with different points. This can be detected by checking the * iteration number at each call if needed. Each time this method is * called, the previous and current point correspond to points with the * same role at each iteration, so they can be compared. As an example, * simplex-based algorithms call this method for all points of the simplex, * not only for the best or worst ones. * * @param iteration Index of current iteration * @param previous Best point in the previous iteration. * @param current Best point in the current iteration. * @return {@code true} if the algorithm has converged. */ @Override public boolean converged(final int iteration, final PointValuePair previous, final PointValuePair current) { if (maxIterationCount != ITERATION_CHECK_DISABLED && iteration >= maxIterationCount) { return true; } final double p = previous.getValue(); final double c = current.getValue(); final double difference = FastMath.abs(p - c); final double size = FastMath.max(FastMath.abs(p), FastMath.abs(c)); return difference <= size * getRelativeThreshold() || difference <= getAbsoluteThreshold(); } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/optim/SimpleBounds.java100644 1750 1750 4573 12126627714 27134 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.optim; import java.util.Arrays; /** * Simple optimization constraints: lower and upper bounds. * The valid range of the parameters is an interval that can be infinite * (in one or both directions). *
                              * Immutable class. * * @version $Id: SimpleBounds.java 1435539 2013-01-19 13:27:24Z tn $ * @since 3.1 */ public class SimpleBounds implements OptimizationData { /** Lower bounds. */ private final double[] lower; /** Upper bounds. */ private final double[] upper; /** * @param lB Lower bounds. * @param uB Upper bounds. */ public SimpleBounds(double[] lB, double[] uB) { lower = lB.clone(); upper = uB.clone(); } /** * Gets the lower bounds. * * @return the lower bounds. */ public double[] getLower() { return lower.clone(); } /** * Gets the upper bounds. * * @return the upper bounds. */ public double[] getUpper() { return upper.clone(); } /** * Factory method that creates instance of this class that represents * unbounded ranges. * * @param dim Number of parameters. * @return a new instance suitable for passing to an optimizer that * requires bounds specification. */ public static SimpleBounds unbounded(int dim) { final double[] lB = new double[dim]; Arrays.fill(lB, Double.NEGATIVE_INFINITY); final double[] uB = new double[dim]; Arrays.fill(uB, Double.POSITIVE_INFINITY); return new SimpleBounds(lB, uB); } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/geometry/package-info.java100644 1750 1750 2020 12126627714 27540 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ /** * *

                              * This package is the top level package for geometry. It provides only a few interfaces * related to vectorial/affine spaces that are implemented in sub-packages. *

                              * */ package org.apache.commons.math3.geometry; commons-math3-3.2-src/src/main/java/org/apache/commons/math3/geometry/Vector.java100644 1750 1750 13476 12126627714 26517 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.geometry; import java.io.Serializable; import java.text.NumberFormat; import org.apache.commons.math3.exception.MathArithmeticException; /** This interface represents a generic vector in a vectorial space or a point in an affine space. * @param Type of the space. * @version $Id: Vector.java 1416643 2012-12-03 19:37:14Z tn $ * @see Space * @see Vector * @since 3.0 */ public interface Vector extends Serializable { /** Get the space to which the vector belongs. * @return containing space */ Space getSpace(); /** Get the null vector of the vectorial space or origin point of the affine space. * @return null vector of the vectorial space or origin point of the affine space */ Vector getZero(); /** Get the L1 norm for the vector. * @return L1 norm for the vector */ double getNorm1(); /** Get the L2 norm for the vector. * @return Euclidean norm for the vector */ double getNorm(); /** Get the square of the norm for the vector. * @return square of the Euclidean norm for the vector */ double getNormSq(); /** Get the L norm for the vector. * @return L norm for the vector */ double getNormInf(); /** Add a vector to the instance. * @param v vector to add * @return a new vector */ Vector add(Vector v); /** Add a scaled vector to the instance. * @param factor scale factor to apply to v before adding it * @param v vector to add * @return a new vector */ Vector add(double factor, Vector v); /** Subtract a vector from the instance. * @param v vector to subtract * @return a new vector */ Vector subtract(Vector v); /** Subtract a scaled vector from the instance. * @param factor scale factor to apply to v before subtracting it * @param v vector to subtract * @return a new vector */ Vector subtract(double factor, Vector v); /** Get the opposite of the instance. * @return a new vector which is opposite to the instance */ Vector negate(); /** Get a normalized vector aligned with the instance. * @return a new normalized vector * @exception MathArithmeticException if the norm is zero */ Vector normalize() throws MathArithmeticException; /** Multiply the instance by a scalar. * @param a scalar * @return a new vector */ Vector scalarMultiply(double a); /** * Returns true if any coordinate of this vector is NaN; false otherwise * @return true if any coordinate of this vector is NaN; false otherwise */ boolean isNaN(); /** * Returns true if any coordinate of this vector is infinite and none are NaN; * false otherwise * @return true if any coordinate of this vector is infinite and none are NaN; * false otherwise */ boolean isInfinite(); /** Compute the distance between the instance and another vector according to the L1 norm. *

                              Calling this method is equivalent to calling: * q.subtract(p).getNorm1() except that no intermediate * vector is built

                              * @param v second vector * @return the distance between the instance and p according to the L1 norm */ double distance1(Vector v); /** Compute the distance between the instance and another vector according to the L2 norm. *

                              Calling this method is equivalent to calling: * q.subtract(p).getNorm() except that no intermediate * vector is built

                              * @param v second vector * @return the distance between the instance and p according to the L2 norm */ double distance(Vector v); /** Compute the distance between the instance and another vector according to the L norm. *

                              Calling this method is equivalent to calling: * q.subtract(p).getNormInf() except that no intermediate * vector is built

                              * @param v second vector * @return the distance between the instance and p according to the L norm */ double distanceInf(Vector v); /** Compute the square of the distance between the instance and another vector. *

                              Calling this method is equivalent to calling: * q.subtract(p).getNormSq() except that no intermediate * vector is built

                              * @param v second vector * @return the square of the distance between the instance and p */ double distanceSq(Vector v); /** Compute the dot-product of the instance and another vector. * @param v second vector * @return the dot product this.v */ double dotProduct(Vector v); /** Get a string representation of this vector. * @param format the custom format for components * @return a string representation of this vector */ String toString(final NumberFormat format); } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/geometry/VectorFormat.java100644 1750 1750 22734 12126627714 27665 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.geometry; import java.text.FieldPosition; import java.text.NumberFormat; import java.text.ParsePosition; import java.util.Locale; import org.apache.commons.math3.util.CompositeFormat; import org.apache.commons.math3.exception.MathParseException; /** * Formats a vector in components list format "{x; y; ...}". *

                              The prefix and suffix "{" and "}" and the separator "; " can be replaced by * any user-defined strings. The number format for components can be configured.

                              *

                              White space is ignored at parse time, even if it is in the prefix, suffix * or separator specifications. So even if the default separator does include a space * character that is used at format time, both input string "{1;1;1}" and * " { 1 ; 1 ; 1 } " will be parsed without error and the same vector will be * returned. In the second case, however, the parse position after parsing will be * just after the closing curly brace, i.e. just before the trailing space.

                              * * @param Type of the space. * @version $Id: VectorFormat.java 1462503 2013-03-29 15:48:27Z luc $ * @since 3.0 */ public abstract class VectorFormat { /** The default prefix: "{". */ public static final String DEFAULT_PREFIX = "{"; /** The default suffix: "}". */ public static final String DEFAULT_SUFFIX = "}"; /** The default separator: ", ". */ public static final String DEFAULT_SEPARATOR = "; "; /** Prefix. */ private final String prefix; /** Suffix. */ private final String suffix; /** Separator. */ private final String separator; /** Trimmed prefix. */ private final String trimmedPrefix; /** Trimmed suffix. */ private final String trimmedSuffix; /** Trimmed separator. */ private final String trimmedSeparator; /** The format used for components. */ private final NumberFormat format; /** * Create an instance with default settings. *

                              The instance uses the default prefix, suffix and separator: * "{", "}", and "; " and the default number format for components.

                              */ protected VectorFormat() { this(DEFAULT_PREFIX, DEFAULT_SUFFIX, DEFAULT_SEPARATOR, CompositeFormat.getDefaultNumberFormat()); } /** * Create an instance with a custom number format for components. * @param format the custom format for components. */ protected VectorFormat(final NumberFormat format) { this(DEFAULT_PREFIX, DEFAULT_SUFFIX, DEFAULT_SEPARATOR, format); } /** * Create an instance with custom prefix, suffix and separator. * @param prefix prefix to use instead of the default "{" * @param suffix suffix to use instead of the default "}" * @param separator separator to use instead of the default "; " */ protected VectorFormat(final String prefix, final String suffix, final String separator) { this(prefix, suffix, separator, CompositeFormat.getDefaultNumberFormat()); } /** * Create an instance with custom prefix, suffix, separator and format * for components. * @param prefix prefix to use instead of the default "{" * @param suffix suffix to use instead of the default "}" * @param separator separator to use instead of the default "; " * @param format the custom format for components. */ protected VectorFormat(final String prefix, final String suffix, final String separator, final NumberFormat format) { this.prefix = prefix; this.suffix = suffix; this.separator = separator; trimmedPrefix = prefix.trim(); trimmedSuffix = suffix.trim(); trimmedSeparator = separator.trim(); this.format = format; } /** * Get the set of locales for which point/vector formats are available. *

                              This is the same set as the {@link NumberFormat} set.

                              * @return available point/vector format locales. */ public static Locale[] getAvailableLocales() { return NumberFormat.getAvailableLocales(); } /** * Get the format prefix. * @return format prefix. */ public String getPrefix() { return prefix; } /** * Get the format suffix. * @return format suffix. */ public String getSuffix() { return suffix; } /** * Get the format separator between components. * @return format separator. */ public String getSeparator() { return separator; } /** * Get the components format. * @return components format. */ public NumberFormat getFormat() { return format; } /** * Formats a {@link Vector} object to produce a string. * @param vector the object to format. * @return a formatted string. */ public String format(Vector vector) { return format(vector, new StringBuffer(), new FieldPosition(0)).toString(); } /** * Formats a {@link Vector} object to produce a string. * @param vector the object to format. * @param toAppendTo where the text is to be appended * @param pos On input: an alignment field, if desired. On output: the * offsets of the alignment field * @return the value passed in as toAppendTo. */ public abstract StringBuffer format(Vector vector, StringBuffer toAppendTo, FieldPosition pos); /** * Formats the coordinates of a {@link Vector} to produce a string. * @param toAppendTo where the text is to be appended * @param pos On input: an alignment field, if desired. On output: the * offsets of the alignment field * @param coordinates coordinates of the object to format. * @return the value passed in as toAppendTo. */ protected StringBuffer format(StringBuffer toAppendTo, FieldPosition pos, double ... coordinates) { pos.setBeginIndex(0); pos.setEndIndex(0); // format prefix toAppendTo.append(prefix); // format components for (int i = 0; i < coordinates.length; ++i) { if (i > 0) { toAppendTo.append(separator); } CompositeFormat.formatDouble(coordinates[i], format, toAppendTo, pos); } // format suffix toAppendTo.append(suffix); return toAppendTo; } /** * Parses a string to produce a {@link Vector} object. * @param source the string to parse * @return the parsed {@link Vector} object. * @throws MathParseException if the beginning of the specified string * cannot be parsed. */ public abstract Vector parse(String source) throws MathParseException; /** * Parses a string to produce a {@link Vector} object. * @param source the string to parse * @param pos input/output parsing parameter. * @return the parsed {@link Vector} object. */ public abstract Vector parse(String source, ParsePosition pos); /** * Parses a string to produce an array of coordinates. * @param dimension dimension of the space * @param source the string to parse * @param pos input/output parsing parameter. * @return coordinates array. */ protected double[] parseCoordinates(int dimension, String source, ParsePosition pos) { int initialIndex = pos.getIndex(); double[] coordinates = new double[dimension]; // parse prefix CompositeFormat.parseAndIgnoreWhitespace(source, pos); if (!CompositeFormat.parseFixedstring(source, trimmedPrefix, pos)) { return null; } for (int i = 0; i < dimension; ++i) { // skip whitespace CompositeFormat.parseAndIgnoreWhitespace(source, pos); // parse separator if (i > 0 && !CompositeFormat.parseFixedstring(source, trimmedSeparator, pos)) { return null; } // skip whitespace CompositeFormat.parseAndIgnoreWhitespace(source, pos); // parse coordinate Number c = CompositeFormat.parseNumber(source, format, pos); if (c == null) { // invalid coordinate // set index back to initial, error index should already be set pos.setIndex(initialIndex); return null; } // store coordinate coordinates[i] = c.doubleValue(); } // parse suffix CompositeFormat.parseAndIgnoreWhitespace(source, pos); if (!CompositeFormat.parseFixedstring(source, trimmedSuffix, pos)) { return null; } return coordinates; } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/geometry/Space.java100644 1750 1750 3122 12126627714 26253 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.geometry; import java.io.Serializable; import org.apache.commons.math3.exception.MathUnsupportedOperationException; /** This interface represents a generic space, with affine and vectorial counterparts. * @version $Id: Space.java 1416643 2012-12-03 19:37:14Z tn $ * @see Vector * @since 3.0 */ public interface Space extends Serializable { /** Get the dimension of the space. * @return dimension of the space */ int getDimension(); /** Get the n-1 dimension subspace of this space. * @return n-1 dimension sub-space of this space * @see #getDimension() * @exception MathUnsupportedOperationException for dimension-1 spaces * which do not have sub-spaces */ Space getSubSpace() throws MathUnsupportedOperationException; } ././@LongLink100644 0 0 145 12126630646 10261 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/geometry/partitioning/package-info.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/geometry/partitioning/package-info.java100644 1750 1750 12506 12126627714 32301 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ /** * * This package provides classes to implement Binary Space Partition trees. * *

                              * {@link org.apache.commons.math3.geometry.partitioning.BSPTree BSP trees} * are an efficient way to represent parts of space and in particular * polytopes (line segments in 1D, polygons in 2D and polyhedrons in 3D) * and to operate on them. The main principle is to recursively subdivide * the space using simple hyperplanes (points in 1D, lines in 2D, planes * in 3D). *

                              * *

                              * We start with a tree composed of a single node without any cut * hyperplane: it represents the complete space, which is a convex * part. If we add a cut hyperplane to this node, this represents a * partition with the hyperplane at the node level and two half spaces at * each side of the cut hyperplane. These half-spaces are represented by * two child nodes without any cut hyperplanes associated, the plus child * which represents the half space on the plus side of the cut hyperplane * and the minus child on the other side. Continuing the subdivisions, we * end up with a tree having internal nodes that are associated with a * cut hyperplane and leaf nodes without any hyperplane which correspond * to convex parts. *

                              * *

                              * When BSP trees are used to represent polytopes, the convex parts are * known to be completely inside or outside the polytope as long as there * is no facet in the part (which is obviously the case if the cut * hyperplanes have been chosen as the underlying hyperplanes of the * facets (this is called an autopartition) and if the subdivision * process has been continued until all facets have been processed. It is * important to note that the polytope is not defined by a * single part, but by several convex ones. This is the property that * allows BSP-trees to represent non-convex polytopes despites all parts * are convex. The {@link * org.apache.commons.math3.geometry.partitioning.Region Region} class is * devoted to this representation, it is build on top of the {@link * org.apache.commons.math3.geometry.partitioning.BSPTree BSPTree} class using * boolean objects as the leaf nodes attributes to represent the * inside/outside property of each leaf part, and also adds various * methods dealing with boundaries (i.e. the separation between the * inside and the outside parts). *

                              * *

                              * Rather than simply associating the internal nodes with an hyperplane, * we consider sub-hyperplanes which correspond to the part of * the hyperplane that is inside the convex part defined by all the * parent nodes (this implies that the sub-hyperplane at root node is in * fact a complete hyperplane, because there is no parent to bound * it). Since the parts are convex, the sub-hyperplanes are convex, in * 3D the convex parts are convex polyhedrons, and the sub-hyperplanes * are convex polygons that cut these polyhedrons in two * sub-polyhedrons. Using this definition, a BSP tree completely * partitions the space. Each point either belongs to one of the * sub-hyperplanes in an internal node or belongs to one of the leaf * convex parts. *

                              * *

                              * In order to determine where a point is, it is sufficient to check its * position with respect to the root cut hyperplane, to select the * corresponding child tree and to repeat the procedure recursively, * until either the point appears to be exactly on one of the hyperplanes * in the middle of the tree or to be in one of the leaf parts. For * this operation, it is sufficient to consider the complete hyperplanes, * there is no need to check the points with the boundary of the * sub-hyperplanes, because this check has in fact already been realized * by the recursive descent in the tree. This is very easy to do and very * efficient, especially if the tree is well balanced (the cost is * O(log(n)) where n is the number of facets) * or if the first tree levels close to the root discriminate large parts * of the total space. *

                              * *

                              * One of the main sources for the development of this package was Bruce * Naylor, John Amanatides and William Thibault paper Merging * BSP Trees Yields Polyhedral Set Operations Proc. Siggraph '90, * Computer Graphics 24(4), August 1990, pp 115-124, published by the * Association for Computing Machinery (ACM). The same paper can also be * found here. *

                              * * */ package org.apache.commons.math3.geometry.partitioning; commons-math3-3.2-src/src/main/java/org/apache/commons/math3/geometry/partitioning/Region.java100644 1750 1750 20117 12126627714 31175 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.geometry.partitioning; import org.apache.commons.math3.geometry.Space; import org.apache.commons.math3.geometry.Vector; /** This interface represents a region of a space as a partition. *

                              Region are subsets of a space, they can be infinite (whole * space, half space, infinite stripe ...) or finite (polygons in 2D, * polyhedrons in 3D ...). Their main characteristic is to separate * points that are considered to be inside the region from * points considered to be outside of it. In between, there * may be points on the boundary of the region.

                              *

                              This implementation is limited to regions for which the boundary * is composed of several {@link SubHyperplane sub-hyperplanes}, * including regions with no boundary at all: the whole space and the * empty region. They are not necessarily finite and not necessarily * path-connected. They can contain holes.

                              *

                              Regions can be combined using the traditional sets operations : * union, intersection, difference and symetric difference (exclusive * or) for the binary operations, complement for the unary * operation.

                              * @param Type of the space. * @version $Id: Region.java 1416643 2012-12-03 19:37:14Z tn $ * @since 3.0 */ public interface Region { /** Enumerate for the location of a point with respect to the region. */ public static enum Location { /** Code for points inside the partition. */ INSIDE, /** Code for points outside of the partition. */ OUTSIDE, /** Code for points on the partition boundary. */ BOUNDARY; } /** Build a region using the instance as a prototype. *

                              This method allow to create new instances without knowing * exactly the type of the region. It is an application of the * prototype design pattern.

                              *

                              The leaf nodes of the BSP tree must have a * {@code Boolean} attribute representing the inside status of * the corresponding cell (true for inside cells, false for outside * cells). In order to avoid building too many small objects, it is * recommended to use the predefined constants * {@code Boolean.TRUE} and {@code Boolean.FALSE}. The * tree also must have either null internal nodes or * internal nodes representing the boundary as specified in the * {@link #getTree getTree} method).

                              * @param newTree inside/outside BSP tree representing the new region * @return the built region */ Region buildNew(BSPTree newTree); /** Copy the instance. *

                              The instance created is completely independant of the original * one. A deep copy is used, none of the underlying objects are * shared (except for the underlying tree {@code Boolean} * attributes and immutable objects).

                              * @return a new region, copy of the instance */ Region copySelf(); /** Check if the instance is empty. * @return true if the instance is empty */ boolean isEmpty(); /** Check if the sub-tree starting at a given node is empty. * @param node root node of the sub-tree (must have {@link * Region Region} tree semantics, i.e. the leaf nodes must have * {@code Boolean} attributes representing an inside/outside * property) * @return true if the sub-tree starting at the given node is empty */ boolean isEmpty(final BSPTree node); /** Check if the instance entirely contains another region. * @param region region to check against the instance * @return true if the instance contains the specified tree */ boolean contains(final Region region); /** Check a point with respect to the region. * @param point point to check * @return a code representing the point status: either {@link * Location#INSIDE}, {@link Location#OUTSIDE} or {@link Location#BOUNDARY} */ Location checkPoint(final Vector point); /** Get the underlying BSP tree. *

                              Regions are represented by an underlying inside/outside BSP * tree whose leaf attributes are {@code Boolean} instances * representing inside leaf cells if the attribute value is * {@code true} and outside leaf cells if the attribute is * {@code false}. These leaf attributes are always present and * guaranteed to be non null.

                              *

                              In addition to the leaf attributes, the internal nodes which * correspond to cells split by cut sub-hyperplanes may contain * {@link BoundaryAttribute BoundaryAttribute} objects representing * the parts of the corresponding cut sub-hyperplane that belong to * the boundary. When the boundary attributes have been computed, * all internal nodes are guaranteed to have non-null * attributes, however some {@link BoundaryAttribute * BoundaryAttribute} instances may have their {@link * BoundaryAttribute#plusInside plusInside} and {@link * BoundaryAttribute#plusOutside plusOutside} fields both null if * the corresponding cut sub-hyperplane does not have any parts * belonging to the boundary.

                              *

                              Since computing the boundary is not always required and can be * time-consuming for large trees, these internal nodes attributes * are computed using lazy evaluation only when required by setting * the {@code includeBoundaryAttributes} argument to * {@code true}. Once computed, these attributes remain in the * tree, which implies that in this case, further calls to the * method for the same region will always include these attributes * regardless of the value of the * {@code includeBoundaryAttributes} argument.

                              * @param includeBoundaryAttributes if true, the boundary attributes * at internal nodes are guaranteed to be included (they may be * included even if the argument is false, if they have already been * computed due to a previous call) * @return underlying BSP tree * @see BoundaryAttribute */ BSPTree getTree(final boolean includeBoundaryAttributes); /** Get the size of the boundary. * @return the size of the boundary (this is 0 in 1D, a length in * 2D, an area in 3D ...) */ double getBoundarySize(); /** Get the size of the instance. * @return the size of the instance (this is a length in 1D, an area * in 2D, a volume in 3D ...) */ double getSize(); /** Get the barycenter of the instance. * @return an object representing the barycenter */ Vector getBarycenter(); /** Compute the relative position of the instance with respect to an * hyperplane. * @param hyperplane reference hyperplane * @return one of {@link Side#PLUS Side.PLUS}, {@link Side#MINUS * Side.MINUS}, {@link Side#BOTH Side.BOTH} or {@link Side#HYPER * Side.HYPER} (the latter result can occur only if the tree * contains only one cut hyperplane) */ Side side(final Hyperplane hyperplane); /** Get the parts of a sub-hyperplane that are contained in the region. *

                              The parts of the sub-hyperplane that belong to the boundary are * not included in the resulting parts.

                              * @param sub sub-hyperplane traversing the region * @return filtered sub-hyperplane */ SubHyperplane intersection(final SubHyperplane sub); } ././@LongLink100644 0 0 152 12126630646 10257 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/geometry/partitioning/BoundaryAttribute.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/geometry/partitioning/BoundaryAttribute100644 1750 1750 6651 12126627714 32470 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.geometry.partitioning; import org.apache.commons.math3.geometry.Space; /** Class holding boundary attributes. *

                              This class is used for the attributes associated with the * nodes of region boundary shell trees returned by the {@link * Region#getTree Region.getTree}. It contains the * parts of the node cut sub-hyperplane that belong to the * boundary.

                              *

                              This class is a simple placeholder, it does not provide any * processing methods.

                              * @param Type of the space. * @see Region#getTree * @version $Id: BoundaryAttribute.java 1416643 2012-12-03 19:37:14Z tn $ * @since 3.0 */ public class BoundaryAttribute { /** Part of the node cut sub-hyperplane that belongs to the * boundary and has the outside of the region on the plus side of * its underlying hyperplane (may be null). */ private final SubHyperplane plusOutside; /** Part of the node cut sub-hyperplane that belongs to the * boundary and has the inside of the region on the plus side of * its underlying hyperplane (may be null). */ private final SubHyperplane plusInside; /** Simple constructor. * @param plusOutside part of the node cut sub-hyperplane that * belongs to the boundary and has the outside of the region on * the plus side of its underlying hyperplane (may be null) * @param plusInside part of the node cut sub-hyperplane that * belongs to the boundary and has the inside of the region on the * plus side of its underlying hyperplane (may be null) */ public BoundaryAttribute(final SubHyperplane plusOutside, final SubHyperplane plusInside) { this.plusOutside = plusOutside; this.plusInside = plusInside; } /** Get the part of the node cut sub-hyperplane that belongs to the * boundary and has the outside of the region on the plus side of * its underlying hyperplane. * @return part of the node cut sub-hyperplane that belongs to the * boundary and has the outside of the region on the plus side of * its underlying hyperplane */ public SubHyperplane getPlusOutside() { return plusOutside; } /** Get the part of the node cut sub-hyperplane that belongs to the * boundary and has the inside of the region on the plus side of * its underlying hyperplane. * @return part of the node cut sub-hyperplane that belongs to the * boundary and has the inside of the region on the plus side of * its underlying hyperplane */ public SubHyperplane getPlusInside() { return plusInside; } } ././@LongLink100644 0 0 156 12126630646 10263 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/geometry/partitioning/AbstractSubHyperplane.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/geometry/partitioning/AbstractSubHyperp100644 1750 1750 14544 12126627714 32446 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.geometry.partitioning; import org.apache.commons.math3.geometry.Space; /** This class implements the dimension-independent parts of {@link SubHyperplane}. *

                              sub-hyperplanes are obtained when parts of an {@link * Hyperplane hyperplane} are chopped off by other hyperplanes that * intersect it. The remaining part is a convex region. Such objects * appear in {@link BSPTree BSP trees} as the intersection of a cut * hyperplane with the convex region which it splits, the chopping * hyperplanes are the cut hyperplanes closer to the tree root.

                              * @param Type of the embedding space. * @param Type of the embedded sub-space. * @version $Id: AbstractSubHyperplane.java 1421448 2012-12-13 19:45:57Z tn $ * @since 3.0 */ public abstract class AbstractSubHyperplane implements SubHyperplane { /** Underlying hyperplane. */ private final Hyperplane hyperplane; /** Remaining region of the hyperplane. */ private final Region remainingRegion; /** Build a sub-hyperplane from an hyperplane and a region. * @param hyperplane underlying hyperplane * @param remainingRegion remaining region of the hyperplane */ protected AbstractSubHyperplane(final Hyperplane hyperplane, final Region remainingRegion) { this.hyperplane = hyperplane; this.remainingRegion = remainingRegion; } /** Build a sub-hyperplane from an hyperplane and a region. * @param hyper underlying hyperplane * @param remaining remaining region of the hyperplane * @return a new sub-hyperplane */ protected abstract AbstractSubHyperplane buildNew(final Hyperplane hyper, final Region remaining); /** {@inheritDoc} */ public AbstractSubHyperplane copySelf() { return buildNew(hyperplane, remainingRegion); } /** Get the underlying hyperplane. * @return underlying hyperplane */ public Hyperplane getHyperplane() { return hyperplane; } /** Get the remaining region of the hyperplane. *

                              The returned region is expressed in the canonical hyperplane * frame and has the hyperplane dimension. For example a chopped * hyperplane in the 3D euclidean is a 2D plane and the * corresponding region is a convex 2D polygon.

                              * @return remaining region of the hyperplane */ public Region getRemainingRegion() { return remainingRegion; } /** {@inheritDoc} */ public double getSize() { return remainingRegion.getSize(); } /** {@inheritDoc} */ public AbstractSubHyperplane reunite(final SubHyperplane other) { @SuppressWarnings("unchecked") AbstractSubHyperplane o = (AbstractSubHyperplane) other; return buildNew(hyperplane, new RegionFactory().union(remainingRegion, o.remainingRegion)); } /** Apply a transform to the instance. *

                              The instance must be a (D-1)-dimension sub-hyperplane with * respect to the transform not a (D-2)-dimension * sub-hyperplane the transform knows how to transform by * itself. The transform will consist in transforming first the * hyperplane and then the all region using the various methods * provided by the transform.

                              * @param transform D-dimension transform to apply * @return the transformed instance */ public AbstractSubHyperplane applyTransform(final Transform transform) { final Hyperplane tHyperplane = transform.apply(hyperplane); final BSPTree tTree = recurseTransform(remainingRegion.getTree(false), tHyperplane, transform); return buildNew(tHyperplane, remainingRegion.buildNew(tTree)); } /** Recursively transform a BSP-tree from a sub-hyperplane. * @param node current BSP tree node * @param transformed image of the instance hyperplane by the transform * @param transform transform to apply * @return a new tree */ private BSPTree recurseTransform(final BSPTree node, final Hyperplane transformed, final Transform transform) { if (node.getCut() == null) { return new BSPTree(node.getAttribute()); } @SuppressWarnings("unchecked") BoundaryAttribute attribute = (BoundaryAttribute) node.getAttribute(); if (attribute != null) { final SubHyperplane tPO = (attribute.getPlusOutside() == null) ? null : transform.apply(attribute.getPlusOutside(), hyperplane, transformed); final SubHyperplane tPI = (attribute.getPlusInside() == null) ? null : transform.apply(attribute.getPlusInside(), hyperplane, transformed); attribute = new BoundaryAttribute(tPO, tPI); } return new BSPTree(transform.apply(node.getCut(), hyperplane, transformed), recurseTransform(node.getPlus(), transformed, transform), recurseTransform(node.getMinus(), transformed, transform), attribute); } /** {@inheritDoc} */ public abstract Side side(Hyperplane hyper); /** {@inheritDoc} */ public abstract SplitSubHyperplane split(Hyperplane hyper); /** {@inheritDoc} */ public boolean isEmpty() { return remainingRegion.isEmpty(); } } ././@LongLink100644 0 0 154 12126630646 10261 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/geometry/partitioning/BoundarySizeVisitor.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/geometry/partitioning/BoundarySizeVisit100644 1750 1750 4162 12126627714 32451 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.geometry.partitioning; import org.apache.commons.math3.geometry.Space; /** Visitor computing the boundary size. * @param Type of the space. * @version $Id: BoundarySizeVisitor.java 1416643 2012-12-03 19:37:14Z tn $ * @since 3.0 */ class BoundarySizeVisitor implements BSPTreeVisitor { /** Size of the boundary. */ private double boundarySize; /** Simple constructor. */ public BoundarySizeVisitor() { boundarySize = 0; } /** {@inheritDoc}*/ public Order visitOrder(final BSPTree node) { return Order.MINUS_SUB_PLUS; } /** {@inheritDoc}*/ public void visitInternalNode(final BSPTree node) { @SuppressWarnings("unchecked") final BoundaryAttribute attribute = (BoundaryAttribute) node.getAttribute(); if (attribute.getPlusOutside() != null) { boundarySize += attribute.getPlusOutside().getSize(); } if (attribute.getPlusInside() != null) { boundarySize += attribute.getPlusInside().getSize(); } } /** {@inheritDoc}*/ public void visitLeafNode(final BSPTree node) { } /** Get the size of the boundary. * @return size of the boundary */ public double getSize() { return boundarySize; } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/geometry/partitioning/BSPTree.java100644 1750 1750 62141 12126627714 31221 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.geometry.partitioning; import org.apache.commons.math3.exception.MathInternalError; import org.apache.commons.math3.geometry.Vector; import org.apache.commons.math3.geometry.Space; import org.apache.commons.math3.util.FastMath; /** This class represent a Binary Space Partition tree. *

                              BSP trees are an efficient way to represent space partitions and * to associate attributes with each cell. Each node in a BSP tree * represents a convex region which is partitioned in two convex * sub-regions at each side of a cut hyperplane. The root tree * contains the complete space.

                              *

                              The main use of such partitions is to use a boolean attribute to * define an inside/outside property, hence representing arbitrary * polytopes (line segments in 1D, polygons in 2D and polyhedrons in * 3D) and to operate on them.

                              *

                              Another example would be to represent Voronoi tesselations, the * attribute of each cell holding the defining point of the cell.

                              *

                              The application-defined attributes are shared among copied * instances and propagated to split parts. These attributes are not * used by the BSP-tree algorithms themselves, so the application can * use them for any purpose. Since the tree visiting method holds * internal and leaf nodes differently, it is possible to use * different classes for internal nodes attributes and leaf nodes * attributes. This should be used with care, though, because if the * tree is modified in any way after attributes have been set, some * internal nodes may become leaf nodes and some leaf nodes may become * internal nodes.

                              *

                              One of the main sources for the development of this package was * Bruce Naylor, John Amanatides and William Thibault paper Merging * BSP Trees Yields Polyhedral Set Operations Proc. Siggraph '90, * Computer Graphics 24(4), August 1990, pp 115-124, published by the * Association for Computing Machinery (ACM).

                              * @param Type of the space. * @version $Id: BSPTree.java 1416643 2012-12-03 19:37:14Z tn $ * @since 3.0 */ public class BSPTree { /** Cut sub-hyperplane. */ private SubHyperplane cut; /** Tree at the plus side of the cut hyperplane. */ private BSPTree plus; /** Tree at the minus side of the cut hyperplane. */ private BSPTree minus; /** Parent tree. */ private BSPTree parent; /** Application-defined attribute. */ private Object attribute; /** Build a tree having only one root cell representing the whole space. */ public BSPTree() { cut = null; plus = null; minus = null; parent = null; attribute = null; } /** Build a tree having only one root cell representing the whole space. * @param attribute attribute of the tree (may be null) */ public BSPTree(final Object attribute) { cut = null; plus = null; minus = null; parent = null; this.attribute = attribute; } /** Build a BSPTree from its underlying elements. *

                              This method does not perform any verification on * consistency of its arguments, it should therefore be used only * when then caller knows what it is doing.

                              *

                              This method is mainly useful kto build trees * bottom-up. Building trees top-down is realized with the help of * method {@link #insertCut insertCut}.

                              * @param cut cut sub-hyperplane for the tree * @param plus plus side sub-tree * @param minus minus side sub-tree * @param attribute attribute associated with the node (may be null) * @see #insertCut */ public BSPTree(final SubHyperplane cut, final BSPTree plus, final BSPTree minus, final Object attribute) { this.cut = cut; this.plus = plus; this.minus = minus; this.parent = null; this.attribute = attribute; plus.parent = this; minus.parent = this; } /** Insert a cut sub-hyperplane in a node. *

                              The sub-tree starting at this node will be completely * overwritten. The new cut sub-hyperplane will be built from the * intersection of the provided hyperplane with the cell. If the * hyperplane does intersect the cell, the cell will have two * children cells with {@code null} attributes on each side of * the inserted cut sub-hyperplane. If the hyperplane does not * intersect the cell then no cut hyperplane will be * inserted and the cell will be changed to a leaf cell. The * attribute of the node is never changed.

                              *

                              This method is mainly useful when called on leaf nodes * (i.e. nodes for which {@link #getCut getCut} returns * {@code null}), in this case it provides a way to build a * tree top-down (whereas the {@link #BSPTree(SubHyperplane, * BSPTree, BSPTree, Object) 4 arguments constructor} is devoted to * build trees bottom-up).

                              * @param hyperplane hyperplane to insert, it will be chopped in * order to fit in the cell defined by the parent nodes of the * instance * @return true if a cut sub-hyperplane has been inserted (i.e. if * the cell now has two leaf child nodes) * @see #BSPTree(SubHyperplane, BSPTree, BSPTree, Object) */ public boolean insertCut(final Hyperplane hyperplane) { if (cut != null) { plus.parent = null; minus.parent = null; } final SubHyperplane chopped = fitToCell(hyperplane.wholeHyperplane()); if (chopped == null || chopped.isEmpty()) { cut = null; plus = null; minus = null; return false; } cut = chopped; plus = new BSPTree(); plus.parent = this; minus = new BSPTree(); minus.parent = this; return true; } /** Copy the instance. *

                              The instance created is completely independant of the original * one. A deep copy is used, none of the underlying objects are * shared (except for the nodes attributes and immutable * objects).

                              * @return a new tree, copy of the instance */ public BSPTree copySelf() { if (cut == null) { return new BSPTree(attribute); } return new BSPTree(cut.copySelf(), plus.copySelf(), minus.copySelf(), attribute); } /** Get the cut sub-hyperplane. * @return cut sub-hyperplane, null if this is a leaf tree */ public SubHyperplane getCut() { return cut; } /** Get the tree on the plus side of the cut hyperplane. * @return tree on the plus side of the cut hyperplane, null if this * is a leaf tree */ public BSPTree getPlus() { return plus; } /** Get the tree on the minus side of the cut hyperplane. * @return tree on the minus side of the cut hyperplane, null if this * is a leaf tree */ public BSPTree getMinus() { return minus; } /** Get the parent node. * @return parent node, null if the node has no parents */ public BSPTree getParent() { return parent; } /** Associate an attribute with the instance. * @param attribute attribute to associate with the node * @see #getAttribute */ public void setAttribute(final Object attribute) { this.attribute = attribute; } /** Get the attribute associated with the instance. * @return attribute associated with the node or null if no * attribute has been explicitly set using the {@link #setAttribute * setAttribute} method * @see #setAttribute */ public Object getAttribute() { return attribute; } /** Visit the BSP tree nodes. * @param visitor object visiting the tree nodes */ public void visit(final BSPTreeVisitor visitor) { if (cut == null) { visitor.visitLeafNode(this); } else { switch (visitor.visitOrder(this)) { case PLUS_MINUS_SUB: plus.visit(visitor); minus.visit(visitor); visitor.visitInternalNode(this); break; case PLUS_SUB_MINUS: plus.visit(visitor); visitor.visitInternalNode(this); minus.visit(visitor); break; case MINUS_PLUS_SUB: minus.visit(visitor); plus.visit(visitor); visitor.visitInternalNode(this); break; case MINUS_SUB_PLUS: minus.visit(visitor); visitor.visitInternalNode(this); plus.visit(visitor); break; case SUB_PLUS_MINUS: visitor.visitInternalNode(this); plus.visit(visitor); minus.visit(visitor); break; case SUB_MINUS_PLUS: visitor.visitInternalNode(this); minus.visit(visitor); plus.visit(visitor); break; default: throw new MathInternalError(); } } } /** Fit a sub-hyperplane inside the cell defined by the instance. *

                              Fitting is done by chopping off the parts of the * sub-hyperplane that lie outside of the cell using the * cut-hyperplanes of the parent nodes of the instance.

                              * @param sub sub-hyperplane to fit * @return a new sub-hyperplane, guaranteed to have no part outside * of the instance cell */ private SubHyperplane fitToCell(final SubHyperplane sub) { SubHyperplane s = sub; for (BSPTree tree = this; tree.parent != null; tree = tree.parent) { if (tree == tree.parent.plus) { s = s.split(tree.parent.cut.getHyperplane()).getPlus(); } else { s = s.split(tree.parent.cut.getHyperplane()).getMinus(); } } return s; } /** Get the cell to which a point belongs. *

                              If the returned cell is a leaf node the points belongs to the * interior of the node, if the cell is an internal node the points * belongs to the node cut sub-hyperplane.

                              * @param point point to check * @return the tree cell to which the point belongs (can be */ public BSPTree getCell(final Vector point) { if (cut == null) { return this; } // position of the point with respect to the cut hyperplane final double offset = cut.getHyperplane().getOffset(point); if (FastMath.abs(offset) < 1.0e-10) { return this; } else if (offset <= 0) { // point is on the minus side of the cut hyperplane return minus.getCell(point); } else { // point is on the plus side of the cut hyperplane return plus.getCell(point); } } /** Perform condensation on a tree. *

                              The condensation operation is not recursive, it must be called * explicitely from leaves to root.

                              */ private void condense() { if ((cut != null) && (plus.cut == null) && (minus.cut == null) && (((plus.attribute == null) && (minus.attribute == null)) || ((plus.attribute != null) && plus.attribute.equals(minus.attribute)))) { attribute = (plus.attribute == null) ? minus.attribute : plus.attribute; cut = null; plus = null; minus = null; } } /** Merge a BSP tree with the instance. *

                              All trees are modified (parts of them are reused in the new * tree), it is the responsibility of the caller to ensure a copy * has been done before if any of the former tree should be * preserved, no such copy is done here!

                              *

                              The algorithm used here is directly derived from the one * described in the Naylor, Amanatides and Thibault paper (section * III, Binary Partitioning of a BSP Tree).

                              * @param tree other tree to merge with the instance (will be * unusable after the operation, as well as the * instance itself) * @param leafMerger object implementing the final merging phase * (this is where the semantic of the operation occurs, generally * depending on the attribute of the leaf node) * @return a new tree, result of instance <op> * tree, this value can be ignored if parentTree is not null * since all connections have already been established */ public BSPTree merge(final BSPTree tree, final LeafMerger leafMerger) { return merge(tree, leafMerger, null, false); } /** Merge a BSP tree with the instance. * @param tree other tree to merge with the instance (will be * unusable after the operation, as well as the * instance itself) * @param leafMerger object implementing the final merging phase * (this is where the semantic of the operation occurs, generally * depending on the attribute of the leaf node) * @param parentTree parent tree to connect to (may be null) * @param isPlusChild if true and if parentTree is not null, the * resulting tree should be the plus child of its parent, ignored if * parentTree is null * @return a new tree, result of instance <op> * tree, this value can be ignored if parentTree is not null * since all connections have already been established */ private BSPTree merge(final BSPTree tree, final LeafMerger leafMerger, final BSPTree parentTree, final boolean isPlusChild) { if (cut == null) { // cell/tree operation return leafMerger.merge(this, tree, parentTree, isPlusChild, true); } else if (tree.cut == null) { // tree/cell operation return leafMerger.merge(tree, this, parentTree, isPlusChild, false); } else { // tree/tree operation final BSPTree merged = tree.split(cut); if (parentTree != null) { merged.parent = parentTree; if (isPlusChild) { parentTree.plus = merged; } else { parentTree.minus = merged; } } // merging phase plus.merge(merged.plus, leafMerger, merged, true); minus.merge(merged.minus, leafMerger, merged, false); merged.condense(); if (merged.cut != null) { merged.cut = merged.fitToCell(merged.cut.getHyperplane().wholeHyperplane()); } return merged; } } /** This interface gather the merging operations between a BSP tree * leaf and another BSP tree. *

                              As explained in Bruce Naylor, John Amanatides and William * Thibault paper Merging * BSP Trees Yields Polyhedral Set Operations, * the operations on {@link BSPTree BSP trees} can be expressed as a * generic recursive merging operation where only the final part, * when one of the operand is a leaf, is specific to the real * operation semantics. For example, a tree representing a region * using a boolean attribute to identify inside cells and outside * cells would use four different objects to implement the final * merging phase of the four set operations union, intersection, * difference and symmetric difference (exclusive or).

                              * @param Type of the space. */ public interface LeafMerger { /** Merge a leaf node and a tree node. *

                              This method is called at the end of a recursive merging * resulting from a {@code tree1.merge(tree2, leafMerger)} * call, when one of the sub-trees involved is a leaf (i.e. when * its cut-hyperplane is null). This is the only place where the * precise semantics of the operation are required. For all upper * level nodes in the tree, the merging operation is only a * generic partitioning algorithm.

                              *

                              Since the final operation may be non-commutative, it is * important to know if the leaf node comes from the instance tree * ({@code tree1}) or the argument tree * ({@code tree2}). The third argument of the method is * devoted to this. It can be ignored for commutative * operations.

                              *

                              The {@link BSPTree#insertInTree BSPTree.insertInTree} method * may be useful to implement this method.

                              * @param leaf leaf node (its cut hyperplane is guaranteed to be * null) * @param tree tree node (its cut hyperplane may be null or not) * @param parentTree parent tree to connect to (may be null) * @param isPlusChild if true and if parentTree is not null, the * resulting tree should be the plus child of its parent, ignored if * parentTree is null * @param leafFromInstance if true, the leaf node comes from the * instance tree ({@code tree1}) and the tree node comes from * the argument tree ({@code tree2}) * @return the BSP tree resulting from the merging (may be one of * the arguments) */ BSPTree merge(BSPTree leaf, BSPTree tree, BSPTree parentTree, boolean isPlusChild, boolean leafFromInstance); } /** Split a BSP tree by an external sub-hyperplane. *

                              Split a tree in two halves, on each side of the * sub-hyperplane. The instance is not modified.

                              *

                              The tree returned is not upward-consistent: despite all of its * sub-trees cut sub-hyperplanes (including its own cut * sub-hyperplane) are bounded to the current cell, it is not * attached to any parent tree yet. This tree is intended to be * later inserted into an higher level tree.

                              *

                              The algorithm used here is the one given in Naylor, Amanatides * and Thibault paper (section III, Binary Partitioning of a BSP * Tree).

                              * @param sub partitioning sub-hyperplane, must be already clipped * to the convex region represented by the instance, will be used as * the cut sub-hyperplane of the returned tree * @return a tree having the specified sub-hyperplane as its cut * sub-hyperplane, the two parts of the split instance as its two * sub-trees and a null parent */ public BSPTree split(final SubHyperplane sub) { if (cut == null) { return new BSPTree(sub, copySelf(), new BSPTree(attribute), null); } final Hyperplane cHyperplane = cut.getHyperplane(); final Hyperplane sHyperplane = sub.getHyperplane(); switch (sub.side(cHyperplane)) { case PLUS : { // the partitioning sub-hyperplane is entirely in the plus sub-tree final BSPTree split = plus.split(sub); if (cut.side(sHyperplane) == Side.PLUS) { split.plus = new BSPTree(cut.copySelf(), split.plus, minus.copySelf(), attribute); split.plus.condense(); split.plus.parent = split; } else { split.minus = new BSPTree(cut.copySelf(), split.minus, minus.copySelf(), attribute); split.minus.condense(); split.minus.parent = split; } return split; } case MINUS : { // the partitioning sub-hyperplane is entirely in the minus sub-tree final BSPTree split = minus.split(sub); if (cut.side(sHyperplane) == Side.PLUS) { split.plus = new BSPTree(cut.copySelf(), plus.copySelf(), split.plus, attribute); split.plus.condense(); split.plus.parent = split; } else { split.minus = new BSPTree(cut.copySelf(), plus.copySelf(), split.minus, attribute); split.minus.condense(); split.minus.parent = split; } return split; } case BOTH : { final SubHyperplane.SplitSubHyperplane cutParts = cut.split(sHyperplane); final SubHyperplane.SplitSubHyperplane subParts = sub.split(cHyperplane); final BSPTree split = new BSPTree(sub, plus.split(subParts.getPlus()), minus.split(subParts.getMinus()), null); split.plus.cut = cutParts.getPlus(); split.minus.cut = cutParts.getMinus(); final BSPTree tmp = split.plus.minus; split.plus.minus = split.minus.plus; split.plus.minus.parent = split.plus; split.minus.plus = tmp; split.minus.plus.parent = split.minus; split.plus.condense(); split.minus.condense(); return split; } default : return cHyperplane.sameOrientationAs(sHyperplane) ? new BSPTree(sub, plus.copySelf(), minus.copySelf(), attribute) : new BSPTree(sub, minus.copySelf(), plus.copySelf(), attribute); } } /** Insert the instance into another tree. *

                              The instance itself is modified so its former parent should * not be used anymore.

                              * @param parentTree parent tree to connect to (may be null) * @param isPlusChild if true and if parentTree is not null, the * resulting tree should be the plus child of its parent, ignored if * parentTree is null * @see LeafMerger */ public void insertInTree(final BSPTree parentTree, final boolean isPlusChild) { // set up parent/child links parent = parentTree; if (parentTree != null) { if (isPlusChild) { parentTree.plus = this; } else { parentTree.minus = this; } } // make sure the inserted tree lies in the cell defined by its parent nodes if (cut != null) { // explore the parent nodes from here towards tree root for (BSPTree tree = this; tree.parent != null; tree = tree.parent) { // this is an hyperplane of some parent node final Hyperplane hyperplane = tree.parent.cut.getHyperplane(); // chop off the parts of the inserted tree that extend // on the wrong side of this parent hyperplane if (tree == tree.parent.plus) { cut = cut.split(hyperplane).getPlus(); plus.chopOffMinus(hyperplane); minus.chopOffMinus(hyperplane); } else { cut = cut.split(hyperplane).getMinus(); plus.chopOffPlus(hyperplane); minus.chopOffPlus(hyperplane); } } // since we may have drop some parts of the inserted tree, // perform a condensation pass to keep the tree structure simple condense(); } } /** Chop off parts of the tree. *

                              The instance is modified in place, all the parts that are on * the minus side of the chopping hyperplane are discarded, only the * parts on the plus side remain.

                              * @param hyperplane chopping hyperplane */ private void chopOffMinus(final Hyperplane hyperplane) { if (cut != null) { cut = cut.split(hyperplane).getPlus(); plus.chopOffMinus(hyperplane); minus.chopOffMinus(hyperplane); } } /** Chop off parts of the tree. *

                              The instance is modified in place, all the parts that are on * the plus side of the chopping hyperplane are discarded, only the * parts on the minus side remain.

                              * @param hyperplane chopping hyperplane */ private void chopOffPlus(final Hyperplane hyperplane) { if (cut != null) { cut = cut.split(hyperplane).getMinus(); plus.chopOffPlus(hyperplane); minus.chopOffPlus(hyperplane); } } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/geometry/partitioning/Transform.java100644 1750 1750 6112 12126627714 31704 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.geometry.partitioning; import org.apache.commons.math3.geometry.Vector; import org.apache.commons.math3.geometry.Space; /** This interface represents an inversible affine transform in a space. *

                              Inversible affine transform include for example scalings, * translations, rotations.

                              *

                              Transforms are dimension-specific. The consistency rules between * the three {@code apply} methods are the following ones for a * transformed defined for dimension D:

                              *
                                *
                              • * the transform can be applied to a point in the * D-dimension space using its {@link #apply(Vector)} * method *
                              • *
                              • * the transform can be applied to a (D-1)-dimension * hyperplane in the D-dimension space using its * {@link #apply(Hyperplane)} method *
                              • *
                              • * the transform can be applied to a (D-2)-dimension * sub-hyperplane in a (D-1)-dimension hyperplane using * its {@link #apply(SubHyperplane, Hyperplane, Hyperplane)} * method *
                              • *
                              * @param Type of the embedding space. * @param Type of the embedded sub-space. * @version $Id: Transform.java 1416643 2012-12-03 19:37:14Z tn $ * @since 3.0 */ public interface Transform { /** Transform a point of a space. * @param point point to transform * @return a new object representing the transformed point */ Vector apply(Vector point); /** Transform an hyperplane of a space. * @param hyperplane hyperplane to transform * @return a new object representing the transformed hyperplane */ Hyperplane apply(Hyperplane hyperplane); /** Transform a sub-hyperplane embedded in an hyperplane. * @param sub sub-hyperplane to transform * @param original hyperplane in which the sub-hyperplane is * defined (this is the original hyperplane, the transform has * not been applied to it) * @param transformed hyperplane in which the sub-hyperplane is * defined (this is the transformed hyperplane, the transform * has been applied to it) * @return a new object representing the transformed sub-hyperplane */ SubHyperplane apply(SubHyperplane sub, Hyperplane original, Hyperplane transformed); } ././@LongLink100644 0 0 147 12126630646 10263 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/geometry/partitioning/AbstractRegion.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/geometry/partitioning/AbstractRegion.ja100644 1750 1750 63701 12126627714 32340 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.geometry.partitioning; import java.lang.reflect.Array; import java.util.ArrayList; import java.util.Collection; import java.util.Comparator; import java.util.Iterator; import java.util.TreeSet; import org.apache.commons.math3.exception.MathInternalError; import org.apache.commons.math3.geometry.Space; import org.apache.commons.math3.geometry.Vector; /** Abstract class for all regions, independently of geometry type or dimension. * @param Type of the space. * @param Type of the sub-space. * @version $Id: AbstractRegion.java 1416643 2012-12-03 19:37:14Z tn $ * @since 3.0 */ public abstract class AbstractRegion implements Region { /** Inside/Outside BSP tree. */ private BSPTree tree; /** Size of the instance. */ private double size; /** Barycenter. */ private Vector barycenter; /** Build a region representing the whole space. */ protected AbstractRegion() { tree = new BSPTree(Boolean.TRUE); } /** Build a region from an inside/outside BSP tree. *

                              The leaf nodes of the BSP tree must have a * {@code Boolean} attribute representing the inside status of * the corresponding cell (true for inside cells, false for outside * cells). In order to avoid building too many small objects, it is * recommended to use the predefined constants * {@code Boolean.TRUE} and {@code Boolean.FALSE}. The * tree also must have either null internal nodes or * internal nodes representing the boundary as specified in the * {@link #getTree getTree} method).

                              * @param tree inside/outside BSP tree representing the region */ protected AbstractRegion(final BSPTree tree) { this.tree = tree; } /** Build a Region from a Boundary REPresentation (B-rep). *

                              The boundary is provided as a collection of {@link * SubHyperplane sub-hyperplanes}. Each sub-hyperplane has the * interior part of the region on its minus side and the exterior on * its plus side.

                              *

                              The boundary elements can be in any order, and can form * several non-connected sets (like for example polygons with holes * or a set of disjoints polyhedrons considered as a whole). In * fact, the elements do not even need to be connected together * (their topological connections are not used here). However, if the * boundary does not really separate an inside open from an outside * open (open having here its topological meaning), then subsequent * calls to the {@link #checkPoint(Vector) checkPoint} method will not be * meaningful anymore.

                              *

                              If the boundary is empty, the region will represent the whole * space.

                              * @param boundary collection of boundary elements, as a * collection of {@link SubHyperplane SubHyperplane} objects */ protected AbstractRegion(final Collection> boundary) { if (boundary.size() == 0) { // the tree represents the whole space tree = new BSPTree(Boolean.TRUE); } else { // sort the boundary elements in decreasing size order // (we don't want equal size elements to be removed, so // we use a trick to fool the TreeSet) final TreeSet> ordered = new TreeSet>(new Comparator>() { public int compare(final SubHyperplane o1, final SubHyperplane o2) { final double size1 = o1.getSize(); final double size2 = o2.getSize(); return (size2 < size1) ? -1 : ((o1 == o2) ? 0 : +1); } }); ordered.addAll(boundary); // build the tree top-down tree = new BSPTree(); insertCuts(tree, ordered); // set up the inside/outside flags tree.visit(new BSPTreeVisitor() { /** {@inheritDoc} */ public Order visitOrder(final BSPTree node) { return Order.PLUS_SUB_MINUS; } /** {@inheritDoc} */ public void visitInternalNode(final BSPTree node) { } /** {@inheritDoc} */ public void visitLeafNode(final BSPTree node) { node.setAttribute((node == node.getParent().getPlus()) ? Boolean.FALSE : Boolean.TRUE); } }); } } /** Build a convex region from an array of bounding hyperplanes. * @param hyperplanes array of bounding hyperplanes (if null, an * empty region will be built) */ public AbstractRegion(final Hyperplane[] hyperplanes) { if ((hyperplanes == null) || (hyperplanes.length == 0)) { tree = new BSPTree(Boolean.FALSE); } else { // use the first hyperplane to build the right class tree = hyperplanes[0].wholeSpace().getTree(false); // chop off parts of the space BSPTree node = tree; node.setAttribute(Boolean.TRUE); for (final Hyperplane hyperplane : hyperplanes) { if (node.insertCut(hyperplane)) { node.setAttribute(null); node.getPlus().setAttribute(Boolean.FALSE); node = node.getMinus(); node.setAttribute(Boolean.TRUE); } } } } /** {@inheritDoc} */ public abstract AbstractRegion buildNew(BSPTree newTree); /** Recursively build a tree by inserting cut sub-hyperplanes. * @param node current tree node (it is a leaf node at the beginning * of the call) * @param boundary collection of edges belonging to the cell defined * by the node */ private void insertCuts(final BSPTree node, final Collection> boundary) { final Iterator> iterator = boundary.iterator(); // build the current level Hyperplane inserted = null; while ((inserted == null) && iterator.hasNext()) { inserted = iterator.next().getHyperplane(); if (!node.insertCut(inserted.copySelf())) { inserted = null; } } if (!iterator.hasNext()) { return; } // distribute the remaining edges in the two sub-trees final ArrayList> plusList = new ArrayList>(); final ArrayList> minusList = new ArrayList>(); while (iterator.hasNext()) { final SubHyperplane other = iterator.next(); switch (other.side(inserted)) { case PLUS: plusList.add(other); break; case MINUS: minusList.add(other); break; case BOTH: final SubHyperplane.SplitSubHyperplane split = other.split(inserted); plusList.add(split.getPlus()); minusList.add(split.getMinus()); break; default: // ignore the sub-hyperplanes belonging to the cut hyperplane } } // recurse through lower levels insertCuts(node.getPlus(), plusList); insertCuts(node.getMinus(), minusList); } /** {@inheritDoc} */ public AbstractRegion copySelf() { return buildNew(tree.copySelf()); } /** {@inheritDoc} */ public boolean isEmpty() { return isEmpty(tree); } /** {@inheritDoc} */ public boolean isEmpty(final BSPTree node) { // we use a recursive function rather than the BSPTreeVisitor // interface because we can stop visiting the tree as soon as we // have found an inside cell if (node.getCut() == null) { // if we find an inside node, the region is not empty return !((Boolean) node.getAttribute()); } // check both sides of the sub-tree return isEmpty(node.getMinus()) && isEmpty(node.getPlus()); } /** {@inheritDoc} */ public boolean contains(final Region region) { return new RegionFactory().difference(region, this).isEmpty(); } /** {@inheritDoc} */ public Location checkPoint(final Vector point) { return checkPoint(tree, point); } /** Check a point with respect to the region starting at a given node. * @param node root node of the region * @param point point to check * @return a code representing the point status: either {@link * Region.Location#INSIDE INSIDE}, {@link Region.Location#OUTSIDE * OUTSIDE} or {@link Region.Location#BOUNDARY BOUNDARY} */ protected Location checkPoint(final BSPTree node, final Vector point) { final BSPTree cell = node.getCell(point); if (cell.getCut() == null) { // the point is in the interior of a cell, just check the attribute return ((Boolean) cell.getAttribute()) ? Location.INSIDE : Location.OUTSIDE; } // the point is on a cut-sub-hyperplane, is it on a boundary ? final Location minusCode = checkPoint(cell.getMinus(), point); final Location plusCode = checkPoint(cell.getPlus(), point); return (minusCode == plusCode) ? minusCode : Location.BOUNDARY; } /** {@inheritDoc} */ public BSPTree getTree(final boolean includeBoundaryAttributes) { if (includeBoundaryAttributes && (tree.getCut() != null) && (tree.getAttribute() == null)) { // we need to compute the boundary attributes tree.visit(new BoundaryBuilder()); } return tree; } /** Visitor building boundary shell tree. *

                              * The boundary shell is represented as {@link BoundaryAttribute boundary attributes} * at each internal node. *

                              */ private static class BoundaryBuilder implements BSPTreeVisitor { /** {@inheritDoc} */ public Order visitOrder(BSPTree node) { return Order.PLUS_MINUS_SUB; } /** {@inheritDoc} */ public void visitInternalNode(BSPTree node) { SubHyperplane plusOutside = null; SubHyperplane plusInside = null; // characterize the cut sub-hyperplane, // first with respect to the plus sub-tree @SuppressWarnings("unchecked") final SubHyperplane[] plusChar = (SubHyperplane[]) Array.newInstance(SubHyperplane.class, 2); characterize(node.getPlus(), node.getCut().copySelf(), plusChar); if (plusChar[0] != null && !plusChar[0].isEmpty()) { // plusChar[0] corresponds to a subset of the cut sub-hyperplane known to have // outside cells on its plus side, we want to check if parts of this subset // do have inside cells on their minus side @SuppressWarnings("unchecked") final SubHyperplane[] minusChar = (SubHyperplane[]) Array.newInstance(SubHyperplane.class, 2); characterize(node.getMinus(), plusChar[0], minusChar); if (minusChar[1] != null && !minusChar[1].isEmpty()) { // this part belongs to the boundary, // it has the outside on its plus side and the inside on its minus side plusOutside = minusChar[1]; } } if (plusChar[1] != null && !plusChar[1].isEmpty()) { // plusChar[1] corresponds to a subset of the cut sub-hyperplane known to have // inside cells on its plus side, we want to check if parts of this subset // do have outside cells on their minus side @SuppressWarnings("unchecked") final SubHyperplane[] minusChar = (SubHyperplane[]) Array.newInstance(SubHyperplane.class, 2); characterize(node.getMinus(), plusChar[1], minusChar); if (minusChar[0] != null && !minusChar[0].isEmpty()) { // this part belongs to the boundary, // it has the inside on its plus side and the outside on its minus side plusInside = minusChar[0]; } } // set the boundary attribute at non-leaf nodes node.setAttribute(new BoundaryAttribute(plusOutside, plusInside)); } /** {@inheritDoc} */ public void visitLeafNode(BSPTree node) { } /** Filter the parts of an hyperplane belonging to the boundary. *

                              The filtering consist in splitting the specified * sub-hyperplane into several parts lying in inside and outside * cells of the tree. The principle is to call this method twice for * each cut sub-hyperplane in the tree, once one the plus node and * once on the minus node. The parts that have the same flag * (inside/inside or outside/outside) do not belong to the boundary * while parts that have different flags (inside/outside or * outside/inside) do belong to the boundary.

                              * @param node current BSP tree node * @param sub sub-hyperplane to characterize * @param characterization placeholder where to put the characterized parts */ private void characterize(final BSPTree node, final SubHyperplane sub, final SubHyperplane[] characterization) { if (node.getCut() == null) { // we have reached a leaf node final boolean inside = (Boolean) node.getAttribute(); if (inside) { if (characterization[1] == null) { characterization[1] = sub; } else { characterization[1] = characterization[1].reunite(sub); } } else { if (characterization[0] == null) { characterization[0] = sub; } else { characterization[0] = characterization[0].reunite(sub); } } } else { final Hyperplane hyperplane = node.getCut().getHyperplane(); switch (sub.side(hyperplane)) { case PLUS: characterize(node.getPlus(), sub, characterization); break; case MINUS: characterize(node.getMinus(), sub, characterization); break; case BOTH: final SubHyperplane.SplitSubHyperplane split = sub.split(hyperplane); characterize(node.getPlus(), split.getPlus(), characterization); characterize(node.getMinus(), split.getMinus(), characterization); break; default: // this should not happen throw new MathInternalError(); } } } } /** {@inheritDoc} */ public double getBoundarySize() { final BoundarySizeVisitor visitor = new BoundarySizeVisitor(); getTree(true).visit(visitor); return visitor.getSize(); } /** {@inheritDoc} */ public double getSize() { if (barycenter == null) { computeGeometricalProperties(); } return size; } /** Set the size of the instance. * @param size size of the instance */ protected void setSize(final double size) { this.size = size; } /** {@inheritDoc} */ public Vector getBarycenter() { if (barycenter == null) { computeGeometricalProperties(); } return barycenter; } /** Set the barycenter of the instance. * @param barycenter barycenter of the instance */ protected void setBarycenter(final Vector barycenter) { this.barycenter = barycenter; } /** Compute some geometrical properties. *

                              The properties to compute are the barycenter and the size.

                              */ protected abstract void computeGeometricalProperties(); /** {@inheritDoc} */ public Side side(final Hyperplane hyperplane) { final Sides sides = new Sides(); recurseSides(tree, hyperplane.wholeHyperplane(), sides); return sides.plusFound() ? (sides.minusFound() ? Side.BOTH : Side.PLUS) : (sides.minusFound() ? Side.MINUS : Side.HYPER); } /** Search recursively for inside leaf nodes on each side of the given hyperplane. *

                              The algorithm used here is directly derived from the one * described in section III (Binary Partitioning of a BSP * Tree) of the Bruce Naylor, John Amanatides and William * Thibault paper Merging * BSP Trees Yields Polyhedral Set Operations Proc. Siggraph * '90, Computer Graphics 24(4), August 1990, pp 115-124, published * by the Association for Computing Machinery (ACM)..

                              * @param node current BSP tree node * @param sub sub-hyperplane * @param sides object holding the sides found */ private void recurseSides(final BSPTree node, final SubHyperplane sub, final Sides sides) { if (node.getCut() == null) { if ((Boolean) node.getAttribute()) { // this is an inside cell expanding across the hyperplane sides.rememberPlusFound(); sides.rememberMinusFound(); } return; } final Hyperplane hyperplane = node.getCut().getHyperplane(); switch (sub.side(hyperplane)) { case PLUS : // the sub-hyperplane is entirely in the plus sub-tree if (node.getCut().side(sub.getHyperplane()) == Side.PLUS) { if (!isEmpty(node.getMinus())) { sides.rememberPlusFound(); } } else { if (!isEmpty(node.getMinus())) { sides.rememberMinusFound(); } } if (!(sides.plusFound() && sides.minusFound())) { recurseSides(node.getPlus(), sub, sides); } break; case MINUS : // the sub-hyperplane is entirely in the minus sub-tree if (node.getCut().side(sub.getHyperplane()) == Side.PLUS) { if (!isEmpty(node.getPlus())) { sides.rememberPlusFound(); } } else { if (!isEmpty(node.getPlus())) { sides.rememberMinusFound(); } } if (!(sides.plusFound() && sides.minusFound())) { recurseSides(node.getMinus(), sub, sides); } break; case BOTH : // the sub-hyperplane extends in both sub-trees final SubHyperplane.SplitSubHyperplane split = sub.split(hyperplane); // explore first the plus sub-tree recurseSides(node.getPlus(), split.getPlus(), sides); // if needed, explore the minus sub-tree if (!(sides.plusFound() && sides.minusFound())) { recurseSides(node.getMinus(), split.getMinus(), sides); } break; default : // the sub-hyperplane and the cut sub-hyperplane share the same hyperplane if (node.getCut().getHyperplane().sameOrientationAs(sub.getHyperplane())) { if ((node.getPlus().getCut() != null) || ((Boolean) node.getPlus().getAttribute())) { sides.rememberPlusFound(); } if ((node.getMinus().getCut() != null) || ((Boolean) node.getMinus().getAttribute())) { sides.rememberMinusFound(); } } else { if ((node.getPlus().getCut() != null) || ((Boolean) node.getPlus().getAttribute())) { sides.rememberMinusFound(); } if ((node.getMinus().getCut() != null) || ((Boolean) node.getMinus().getAttribute())) { sides.rememberPlusFound(); } } } } /** Utility class holding the already found sides. */ private static final class Sides { /** Indicator of inside leaf nodes found on the plus side. */ private boolean plusFound; /** Indicator of inside leaf nodes found on the plus side. */ private boolean minusFound; /** Simple constructor. */ public Sides() { plusFound = false; minusFound = false; } /** Remember the fact that inside leaf nodes have been found on the plus side. */ public void rememberPlusFound() { plusFound = true; } /** Check if inside leaf nodes have been found on the plus side. * @return true if inside leaf nodes have been found on the plus side */ public boolean plusFound() { return plusFound; } /** Remember the fact that inside leaf nodes have been found on the minus side. */ public void rememberMinusFound() { minusFound = true; } /** Check if inside leaf nodes have been found on the minus side. * @return true if inside leaf nodes have been found on the minus side */ public boolean minusFound() { return minusFound; } } /** {@inheritDoc} */ public SubHyperplane intersection(final SubHyperplane sub) { return recurseIntersection(tree, sub); } /** Recursively compute the parts of a sub-hyperplane that are * contained in the region. * @param node current BSP tree node * @param sub sub-hyperplane traversing the region * @return filtered sub-hyperplane */ private SubHyperplane recurseIntersection(final BSPTree node, final SubHyperplane sub) { if (node.getCut() == null) { return (Boolean) node.getAttribute() ? sub.copySelf() : null; } final Hyperplane hyperplane = node.getCut().getHyperplane(); switch (sub.side(hyperplane)) { case PLUS : return recurseIntersection(node.getPlus(), sub); case MINUS : return recurseIntersection(node.getMinus(), sub); case BOTH : final SubHyperplane.SplitSubHyperplane split = sub.split(hyperplane); final SubHyperplane plus = recurseIntersection(node.getPlus(), split.getPlus()); final SubHyperplane minus = recurseIntersection(node.getMinus(), split.getMinus()); if (plus == null) { return minus; } else if (minus == null) { return plus; } else { return plus.reunite(minus); } default : return recurseIntersection(node.getPlus(), recurseIntersection(node.getMinus(), sub)); } } /** Transform a region. *

                              Applying a transform to a region consist in applying the * transform to all the hyperplanes of the underlying BSP tree and * of the boundary (and also to the sub-hyperplanes embedded in * these hyperplanes) and to the barycenter. The instance is not * modified, a new instance is built.

                              * @param transform transform to apply * @return a new region, resulting from the application of the * transform to the instance */ public AbstractRegion applyTransform(final Transform transform) { return buildNew(recurseTransform(getTree(false), transform)); } /** Recursively transform an inside/outside BSP-tree. * @param node current BSP tree node * @param transform transform to apply * @return a new tree */ @SuppressWarnings("unchecked") private BSPTree recurseTransform(final BSPTree node, final Transform transform) { if (node.getCut() == null) { return new BSPTree(node.getAttribute()); } final SubHyperplane sub = node.getCut(); final SubHyperplane tSub = ((AbstractSubHyperplane) sub).applyTransform(transform); BoundaryAttribute attribute = (BoundaryAttribute) node.getAttribute(); if (attribute != null) { final SubHyperplane tPO = (attribute.getPlusOutside() == null) ? null : ((AbstractSubHyperplane) attribute.getPlusOutside()).applyTransform(transform); final SubHyperplane tPI = (attribute.getPlusInside() == null) ? null : ((AbstractSubHyperplane) attribute.getPlusInside()).applyTransform(transform); attribute = new BoundaryAttribute(tPO, tPI); } return new BSPTree(tSub, recurseTransform(node.getPlus(), transform), recurseTransform(node.getMinus(), transform), attribute); } } ././@LongLink100644 0 0 157 12126630646 10264 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/geometry/partitioning/utilities/package-info.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/geometry/partitioning/utilities/package100644 1750 1750 1720 12126627714 32417 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ /** * *

                              * This package provides multidimensional ordering features for partitioning. *

                              * */ package org.apache.commons.math3.geometry.partitioning.utilities; ././@LongLink100644 0 0 152 12126630646 10257 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/geometry/partitioning/utilities/AVLTree.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/geometry/partitioning/utilities/AVLTree100644 1750 1750 52521 12126627714 32313 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.geometry.partitioning.utilities; /** This class implements AVL trees. * *

                              The purpose of this class is to sort elements while allowing * duplicate elements (i.e. such that {@code a.equals(b)} is * true). The {@code SortedSet} interface does not allow this, so * a specific class is needed. Null elements are not allowed.

                              * *

                              Since the {@code equals} method is not sufficient to * differentiate elements, the {@link #delete delete} method is * implemented using the equality operator.

                              * *

                              In order to clearly mark the methods provided here do not have * the same semantics as the ones specified in the * {@code SortedSet} interface, different names are used * ({@code add} has been replaced by {@link #insert insert} and * {@code remove} has been replaced by {@link #delete * delete}).

                              * *

                              This class is based on the C implementation Georg Kraml has put * in the public domain. Unfortunately, his page seems not * to exist any more.

                              * * @param the type of the elements * * @version $Id: AVLTree.java 1416643 2012-12-03 19:37:14Z tn $ * @since 3.0 */ public class AVLTree> { /** Top level node. */ private Node top; /** Build an empty tree. */ public AVLTree() { top = null; } /** Insert an element in the tree. * @param element element to insert (silently ignored if null) */ public void insert(final T element) { if (element != null) { if (top == null) { top = new Node(element, null); } else { top.insert(element); } } } /** Delete an element from the tree. *

                              The element is deleted only if there is a node {@code n} * containing exactly the element instance specified, i.e. for which * {@code n.getElement() == element}. This is purposely * different from the specification of the * {@code java.util.Set} {@code remove} method (in fact, * this is the reason why a specific class has been developed).

                              * @param element element to delete (silently ignored if null) * @return true if the element was deleted from the tree */ public boolean delete(final T element) { if (element != null) { for (Node node = getNotSmaller(element); node != null; node = node.getNext()) { // loop over all elements neither smaller nor larger // than the specified one if (node.element == element) { node.delete(); return true; } else if (node.element.compareTo(element) > 0) { // all the remaining elements are known to be larger, // the element is not in the tree return false; } } } return false; } /** Check if the tree is empty. * @return true if the tree is empty */ public boolean isEmpty() { return top == null; } /** Get the number of elements of the tree. * @return number of elements contained in the tree */ public int size() { return (top == null) ? 0 : top.size(); } /** Get the node whose element is the smallest one in the tree. * @return the tree node containing the smallest element in the tree * or null if the tree is empty * @see #getLargest * @see #getNotSmaller * @see #getNotLarger * @see Node#getPrevious * @see Node#getNext */ public Node getSmallest() { return (top == null) ? null : top.getSmallest(); } /** Get the node whose element is the largest one in the tree. * @return the tree node containing the largest element in the tree * or null if the tree is empty * @see #getSmallest * @see #getNotSmaller * @see #getNotLarger * @see Node#getPrevious * @see Node#getNext */ public Node getLargest() { return (top == null) ? null : top.getLargest(); } /** Get the node whose element is not smaller than the reference object. * @param reference reference object (may not be in the tree) * @return the tree node containing the smallest element not smaller * than the reference object or null if either the tree is empty or * all its elements are smaller than the reference object * @see #getSmallest * @see #getLargest * @see #getNotLarger * @see Node#getPrevious * @see Node#getNext */ public Node getNotSmaller(final T reference) { Node candidate = null; for (Node node = top; node != null;) { if (node.element.compareTo(reference) < 0) { if (node.right == null) { return candidate; } node = node.right; } else { candidate = node; if (node.left == null) { return candidate; } node = node.left; } } return null; } /** Get the node whose element is not larger than the reference object. * @param reference reference object (may not be in the tree) * @return the tree node containing the largest element not larger * than the reference object (in which case the node is guaranteed * not to be empty) or null if either the tree is empty or all its * elements are larger than the reference object * @see #getSmallest * @see #getLargest * @see #getNotSmaller * @see Node#getPrevious * @see Node#getNext */ public Node getNotLarger(final T reference) { Node candidate = null; for (Node node = top; node != null;) { if (node.element.compareTo(reference) > 0) { if (node.left == null) { return candidate; } node = node.left; } else { candidate = node; if (node.right == null) { return candidate; } node = node.right; } } return null; } /** Enum for tree skew factor. */ private static enum Skew { /** Code for left high trees. */ LEFT_HIGH, /** Code for right high trees. */ RIGHT_HIGH, /** Code for Skew.BALANCED trees. */ BALANCED; } /** This class implements AVL trees nodes. *

                              AVL tree nodes implement all the logical structure of the * tree. Nodes are created by the {@link AVLTree AVLTree} class.

                              *

                              The nodes are not independant from each other but must obey * specific balancing constraints and the tree structure is * rearranged as elements are inserted or deleted from the tree. The * creation, modification and tree-related navigation methods have * therefore restricted access. Only the order-related navigation, * reading and delete methods are public.

                              * @see AVLTree */ public class Node { /** Element contained in the current node. */ private T element; /** Left sub-tree. */ private Node left; /** Right sub-tree. */ private Node right; /** Parent tree. */ private Node parent; /** Skew factor. */ private Skew skew; /** Build a node for a specified element. * @param element element * @param parent parent node */ Node(final T element, final Node parent) { this.element = element; left = null; right = null; this.parent = parent; skew = Skew.BALANCED; } /** Get the contained element. * @return element contained in the node */ public T getElement() { return element; } /** Get the number of elements of the tree rooted at this node. * @return number of elements contained in the tree rooted at this node */ int size() { return 1 + ((left == null) ? 0 : left.size()) + ((right == null) ? 0 : right.size()); } /** Get the node whose element is the smallest one in the tree * rooted at this node. * @return the tree node containing the smallest element in the * tree rooted at this node or null if the tree is empty * @see #getLargest */ Node getSmallest() { Node node = this; while (node.left != null) { node = node.left; } return node; } /** Get the node whose element is the largest one in the tree * rooted at this node. * @return the tree node containing the largest element in the * tree rooted at this node or null if the tree is empty * @see #getSmallest */ Node getLargest() { Node node = this; while (node.right != null) { node = node.right; } return node; } /** Get the node containing the next smaller or equal element. * @return node containing the next smaller or equal element or * null if there is no smaller or equal element in the tree * @see #getNext */ public Node getPrevious() { if (left != null) { final Node node = left.getLargest(); if (node != null) { return node; } } for (Node node = this; node.parent != null; node = node.parent) { if (node != node.parent.left) { return node.parent; } } return null; } /** Get the node containing the next larger or equal element. * @return node containing the next larger or equal element (in * which case the node is guaranteed not to be empty) or null if * there is no larger or equal element in the tree * @see #getPrevious */ public Node getNext() { if (right != null) { final Node node = right.getSmallest(); if (node != null) { return node; } } for (Node node = this; node.parent != null; node = node.parent) { if (node != node.parent.right) { return node.parent; } } return null; } /** Insert an element in a sub-tree. * @param newElement element to insert * @return true if the parent tree should be re-Skew.BALANCED */ boolean insert(final T newElement) { if (newElement.compareTo(this.element) < 0) { // the inserted element is smaller than the node if (left == null) { left = new Node(newElement, this); return rebalanceLeftGrown(); } return left.insert(newElement) ? rebalanceLeftGrown() : false; } // the inserted element is equal to or greater than the node if (right == null) { right = new Node(newElement, this); return rebalanceRightGrown(); } return right.insert(newElement) ? rebalanceRightGrown() : false; } /** Delete the node from the tree. */ public void delete() { if ((parent == null) && (left == null) && (right == null)) { // this was the last node, the tree is now empty element = null; top = null; } else { Node node; Node child; boolean leftShrunk; if ((left == null) && (right == null)) { node = this; element = null; leftShrunk = node == node.parent.left; child = null; } else { node = (left != null) ? left.getLargest() : right.getSmallest(); element = node.element; leftShrunk = node == node.parent.left; child = (node.left != null) ? node.left : node.right; } node = node.parent; if (leftShrunk) { node.left = child; } else { node.right = child; } if (child != null) { child.parent = node; } while (leftShrunk ? node.rebalanceLeftShrunk() : node.rebalanceRightShrunk()) { if (node.parent == null) { return; } leftShrunk = node == node.parent.left; node = node.parent; } } } /** Re-balance the instance as left sub-tree has grown. * @return true if the parent tree should be reSkew.BALANCED too */ private boolean rebalanceLeftGrown() { switch (skew) { case LEFT_HIGH: if (left.skew == Skew.LEFT_HIGH) { rotateCW(); skew = Skew.BALANCED; right.skew = Skew.BALANCED; } else { final Skew s = left.right.skew; left.rotateCCW(); rotateCW(); switch(s) { case LEFT_HIGH: left.skew = Skew.BALANCED; right.skew = Skew.RIGHT_HIGH; break; case RIGHT_HIGH: left.skew = Skew.LEFT_HIGH; right.skew = Skew.BALANCED; break; default: left.skew = Skew.BALANCED; right.skew = Skew.BALANCED; } skew = Skew.BALANCED; } return false; case RIGHT_HIGH: skew = Skew.BALANCED; return false; default: skew = Skew.LEFT_HIGH; return true; } } /** Re-balance the instance as right sub-tree has grown. * @return true if the parent tree should be reSkew.BALANCED too */ private boolean rebalanceRightGrown() { switch (skew) { case LEFT_HIGH: skew = Skew.BALANCED; return false; case RIGHT_HIGH: if (right.skew == Skew.RIGHT_HIGH) { rotateCCW(); skew = Skew.BALANCED; left.skew = Skew.BALANCED; } else { final Skew s = right.left.skew; right.rotateCW(); rotateCCW(); switch (s) { case LEFT_HIGH: left.skew = Skew.BALANCED; right.skew = Skew.RIGHT_HIGH; break; case RIGHT_HIGH: left.skew = Skew.LEFT_HIGH; right.skew = Skew.BALANCED; break; default: left.skew = Skew.BALANCED; right.skew = Skew.BALANCED; } skew = Skew.BALANCED; } return false; default: skew = Skew.RIGHT_HIGH; return true; } } /** Re-balance the instance as left sub-tree has shrunk. * @return true if the parent tree should be reSkew.BALANCED too */ private boolean rebalanceLeftShrunk() { switch (skew) { case LEFT_HIGH: skew = Skew.BALANCED; return true; case RIGHT_HIGH: if (right.skew == Skew.RIGHT_HIGH) { rotateCCW(); skew = Skew.BALANCED; left.skew = Skew.BALANCED; return true; } else if (right.skew == Skew.BALANCED) { rotateCCW(); skew = Skew.LEFT_HIGH; left.skew = Skew.RIGHT_HIGH; return false; } else { final Skew s = right.left.skew; right.rotateCW(); rotateCCW(); switch (s) { case LEFT_HIGH: left.skew = Skew.BALANCED; right.skew = Skew.RIGHT_HIGH; break; case RIGHT_HIGH: left.skew = Skew.LEFT_HIGH; right.skew = Skew.BALANCED; break; default: left.skew = Skew.BALANCED; right.skew = Skew.BALANCED; } skew = Skew.BALANCED; return true; } default: skew = Skew.RIGHT_HIGH; return false; } } /** Re-balance the instance as right sub-tree has shrunk. * @return true if the parent tree should be reSkew.BALANCED too */ private boolean rebalanceRightShrunk() { switch (skew) { case RIGHT_HIGH: skew = Skew.BALANCED; return true; case LEFT_HIGH: if (left.skew == Skew.LEFT_HIGH) { rotateCW(); skew = Skew.BALANCED; right.skew = Skew.BALANCED; return true; } else if (left.skew == Skew.BALANCED) { rotateCW(); skew = Skew.RIGHT_HIGH; right.skew = Skew.LEFT_HIGH; return false; } else { final Skew s = left.right.skew; left.rotateCCW(); rotateCW(); switch (s) { case LEFT_HIGH: left.skew = Skew.BALANCED; right.skew = Skew.RIGHT_HIGH; break; case RIGHT_HIGH: left.skew = Skew.LEFT_HIGH; right.skew = Skew.BALANCED; break; default: left.skew = Skew.BALANCED; right.skew = Skew.BALANCED; } skew = Skew.BALANCED; return true; } default: skew = Skew.LEFT_HIGH; return false; } } /** Perform a clockwise rotation rooted at the instance. *

                              The skew factor are not updated by this method, they * must be updated by the caller

                              */ private void rotateCW() { final T tmpElt = element; element = left.element; left.element = tmpElt; final Node tmpNode = left; left = tmpNode.left; tmpNode.left = tmpNode.right; tmpNode.right = right; right = tmpNode; if (left != null) { left.parent = this; } if (right.right != null) { right.right.parent = right; } } /** Perform a counter-clockwise rotation rooted at the instance. *

                              The skew factor are not updated by this method, they * must be updated by the caller

                              */ private void rotateCCW() { final T tmpElt = element; element = right.element; right.element = tmpElt; final Node tmpNode = right; right = tmpNode.right; tmpNode.right = tmpNode.left; tmpNode.left = left; left = tmpNode; if (right != null) { right.parent = this; } if (left.left != null) { left.left.parent = left; } } } } ././@LongLink100644 0 0 157 12126630646 10264 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/geometry/partitioning/utilities/OrderedTuple.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/geometry/partitioning/utilities/Ordered100644 1750 1750 35756 12126627714 32450 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.geometry.partitioning.utilities; import java.util.Arrays; import org.apache.commons.math3.util.FastMath; /** This class implements an ordering operation for T-uples. * *

                              Ordering is done by encoding all components of the T-uple into a * single scalar value and using this value as the sorting * key. Encoding is performed using the method invented by Georg * Cantor in 1877 when he proved it was possible to establish a * bijection between a line and a plane. The binary representations of * the components of the T-uple are mixed together to form a single * scalar. This means that the 2k bit of component 0 is * followed by the 2k bit of component 1, then by the * 2k bit of component 2 up to the 2k bit of * component {@code t}, which is followed by the 2k-1 * bit of component 0, followed by the 2k-1 bit of * component 1 ... The binary representations are extended as needed * to handle numbers with different scales and a suitable * 2p offset is added to the components in order to avoid * negative numbers (this offset is adjusted as needed during the * comparison operations).

                              * *

                              The more interesting property of the encoding method for our * purpose is that it allows to select all the points that are in a * given range. This is depicted in dimension 2 by the following * picture:

                              * * * *

                              This picture shows a set of 100000 random 2-D pairs having their * first component between -50 and +150 and their second component * between -350 and +50. We wanted to extract all pairs having their * first component between +30 and +70 and their second component * between -120 and -30. We built the lower left point at coordinates * (30, -120) and the upper right point at coordinates (70, -30). All * points smaller than the lower left point are drawn in red and all * points larger than the upper right point are drawn in blue. The * green points are between the two limits. This picture shows that * all the desired points are selected, along with spurious points. In * this case, we get 15790 points, 4420 of which really belonging to * the desired rectangle. It is possible to extract very small * subsets. As an example extracting from the same 100000 points set * the points having their first component between +30 and +31 and * their second component between -91 and -90, we get a subset of 11 * points, 2 of which really belonging to the desired rectangle.

                              * *

                              the previous selection technique can be applied in all * dimensions, still using two points to define the interval. The * first point will have all its components set to their lower bounds * while the second point will have all its components set to their * upper bounds.

                              * *

                              T-uples with negative infinite or positive infinite components * are sorted logically.

                              * *

                              Since the specification of the {@code Comparator} interface * allows only {@code ClassCastException} errors, some arbitrary * choices have been made to handle specific cases. The rationale for * these choices is to keep regular and consistent T-uples * together.

                              *
                                *
                              • instances with different dimensions are sorted according to * their dimension regardless of their components values
                              • *
                              • instances with {@code Double.NaN} components are sorted * after all other ones (even after instances with positive infinite * components
                              • *
                              • instances with both positive and negative infinite components * are considered as if they had {@code Double.NaN} * components
                              • *
                              * * @version $Id: OrderedTuple.java 1416643 2012-12-03 19:37:14Z tn $ * @since 3.0 */ public class OrderedTuple implements Comparable { /** Sign bit mask. */ private static final long SIGN_MASK = 0x8000000000000000L; /** Exponent bits mask. */ private static final long EXPONENT_MASK = 0x7ff0000000000000L; /** Mantissa bits mask. */ private static final long MANTISSA_MASK = 0x000fffffffffffffL; /** Implicit MSB for normalized numbers. */ private static final long IMPLICIT_ONE = 0x0010000000000000L; /** Double components of the T-uple. */ private double[] components; /** Offset scale. */ private int offset; /** Least Significant Bit scale. */ private int lsb; /** Ordering encoding of the double components. */ private long[] encoding; /** Positive infinity marker. */ private boolean posInf; /** Negative infinity marker. */ private boolean negInf; /** Not A Number marker. */ private boolean nan; /** Build an ordered T-uple from its components. * @param components double components of the T-uple */ public OrderedTuple(final double ... components) { this.components = components.clone(); int msb = Integer.MIN_VALUE; lsb = Integer.MAX_VALUE; posInf = false; negInf = false; nan = false; for (int i = 0; i < components.length; ++i) { if (Double.isInfinite(components[i])) { if (components[i] < 0) { negInf = true; } else { posInf = true; } } else if (Double.isNaN(components[i])) { nan = true; } else { final long b = Double.doubleToLongBits(components[i]); final long m = mantissa(b); if (m != 0) { final int e = exponent(b); msb = FastMath.max(msb, e + computeMSB(m)); lsb = FastMath.min(lsb, e + computeLSB(m)); } } } if (posInf && negInf) { // instance cannot be sorted logically posInf = false; negInf = false; nan = true; } if (lsb <= msb) { // encode the T-upple with the specified offset encode(msb + 16); } else { encoding = new long[] { 0x0L }; } } /** Encode the T-uple with a given offset. * @param minOffset minimal scale of the offset to add to all * components (must be greater than the MSBs of all components) */ private void encode(final int minOffset) { // choose an offset with some margins offset = minOffset + 31; offset -= offset % 32; if ((encoding != null) && (encoding.length == 1) && (encoding[0] == 0x0L)) { // the components are all zeroes return; } // allocate an integer array to encode the components (we use only // 63 bits per element because there is no unsigned long in Java) final int neededBits = offset + 1 - lsb; final int neededLongs = (neededBits + 62) / 63; encoding = new long[components.length * neededLongs]; // mix the bits from all components int eIndex = 0; int shift = 62; long word = 0x0L; for (int k = offset; eIndex < encoding.length; --k) { for (int vIndex = 0; vIndex < components.length; ++vIndex) { if (getBit(vIndex, k) != 0) { word |= 0x1L << shift; } if (shift-- == 0) { encoding[eIndex++] = word; word = 0x0L; shift = 62; } } } } /** Compares this ordered T-uple with the specified object. *

                              The ordering method is detailed in the general description of * the class. Its main property is to be consistent with distance: * geometrically close T-uples stay close to each other when stored * in a sorted collection using this comparison method.

                              *

                              T-uples with negative infinite, positive infinite are sorted * logically.

                              *

                              Some arbitrary choices have been made to handle specific * cases. The rationale for these choices is to keep * normal and consistent T-uples together.

                              *
                                *
                              • instances with different dimensions are sorted according to * their dimension regardless of their components values
                              • *
                              • instances with {@code Double.NaN} components are sorted * after all other ones (evan after instances with positive infinite * components
                              • *
                              • instances with both positive and negative infinite components * are considered as if they had {@code Double.NaN} * components
                              • *
                              * @param ot T-uple to compare instance with * @return a negative integer if the instance is less than the * object, zero if they are equal, or a positive integer if the * instance is greater than the object */ public int compareTo(final OrderedTuple ot) { if (components.length == ot.components.length) { if (nan) { return +1; } else if (ot.nan) { return -1; } else if (negInf || ot.posInf) { return -1; } else if (posInf || ot.negInf) { return +1; } else { if (offset < ot.offset) { encode(ot.offset); } else if (offset > ot.offset) { ot.encode(offset); } final int limit = FastMath.min(encoding.length, ot.encoding.length); for (int i = 0; i < limit; ++i) { if (encoding[i] < ot.encoding[i]) { return -1; } else if (encoding[i] > ot.encoding[i]) { return +1; } } if (encoding.length < ot.encoding.length) { return -1; } else if (encoding.length > ot.encoding.length) { return +1; } else { return 0; } } } return components.length - ot.components.length; } /** {@inheritDoc} */ @Override public boolean equals(final Object other) { if (this == other) { return true; } else if (other instanceof OrderedTuple) { return compareTo((OrderedTuple) other) == 0; } else { return false; } } /** {@inheritDoc} */ @Override public int hashCode() { // the following constants are arbitrary small primes final int multiplier = 37; final int trueHash = 97; final int falseHash = 71; // hash fields and combine them // (we rely on the multiplier to have different combined weights // for all int fields and all boolean fields) int hash = Arrays.hashCode(components); hash = hash * multiplier + offset; hash = hash * multiplier + lsb; hash = hash * multiplier + (posInf ? trueHash : falseHash); hash = hash * multiplier + (negInf ? trueHash : falseHash); hash = hash * multiplier + (nan ? trueHash : falseHash); return hash; } /** Get the components array. * @return array containing the T-uple components */ public double[] getComponents() { return components.clone(); } /** Extract the sign from the bits of a double. * @param bits binary representation of the double * @return sign bit (zero if positive, non zero if negative) */ private static long sign(final long bits) { return bits & SIGN_MASK; } /** Extract the exponent from the bits of a double. * @param bits binary representation of the double * @return exponent */ private static int exponent(final long bits) { return ((int) ((bits & EXPONENT_MASK) >> 52)) - 1075; } /** Extract the mantissa from the bits of a double. * @param bits binary representation of the double * @return mantissa */ private static long mantissa(final long bits) { return ((bits & EXPONENT_MASK) == 0) ? ((bits & MANTISSA_MASK) << 1) : // subnormal number (IMPLICIT_ONE | (bits & MANTISSA_MASK)); // normal number } /** Compute the most significant bit of a long. * @param l long from which the most significant bit is requested * @return scale of the most significant bit of {@code l}, * or 0 if {@code l} is zero * @see #computeLSB */ private static int computeMSB(final long l) { long ll = l; long mask = 0xffffffffL; int scale = 32; int msb = 0; while (scale != 0) { if ((ll & mask) != ll) { msb |= scale; ll = ll >> scale; } scale = scale >> 1; mask = mask >> scale; } return msb; } /** Compute the least significant bit of a long. * @param l long from which the least significant bit is requested * @return scale of the least significant bit of {@code l}, * or 63 if {@code l} is zero * @see #computeMSB */ private static int computeLSB(final long l) { long ll = l; long mask = 0xffffffff00000000L; int scale = 32; int lsb = 0; while (scale != 0) { if ((ll & mask) == ll) { lsb |= scale; ll = ll >> scale; } scale = scale >> 1; mask = mask >> scale; } return lsb; } /** Get a bit from the mantissa of a double. * @param i index of the component * @param k scale of the requested bit * @return the specified bit (either 0 or 1), after the offset has * been added to the double */ private int getBit(final int i, final int k) { final long bits = Double.doubleToLongBits(components[i]); final int e = exponent(bits); if ((k < e) || (k > offset)) { return 0; } else if (k == offset) { return (sign(bits) == 0L) ? 1 : 0; } else if (k > (e + 52)) { return (sign(bits) == 0L) ? 0 : 1; } else { final long m = (sign(bits) == 0L) ? mantissa(bits) : -mantissa(bits); return (int) ((m >> (k - e)) & 0x1L); } } } ././@LongLink100644 0 0 170 12126630646 10257 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/geometry/partitioning/utilities/doc-files/OrderedTuple.pngcommons-math3-3.2-src/src/main/java/org/apache/commons/math3/geometry/partitioning/utilities/doc-fil100644 1750 1750 70322 12126627714 32365 0ustarlucluc 0 0 PNG  IHDRdd͎9PLTEܔr!gH@BBBY pHYs  tEXtTitleOrderedTuple.agr tEXtAuthorlucל{tEXtSoftwareGrace-5.1.12;3 IDATx?Qs-?쵆L"`Jr;um\D`kkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk?\[dm]ۿyot3Om%^nᄑ?w/yϯϒwo_l_m8<Sg׺Z?=>EFY^r[}:=DOc,={ d[|{q/;P~U_/&|ȯ'{X($8+FA,zEdo=A B,e{ݴfւ_7W>V tþv㽧ַ>Xg^ؿ +/CljtTnS,Ƶ?Srb>lz'TX.cOxMKNz[|Yrr+a+ _뻟J<.bi-pϏoJgD:r?z#UoǩiƟr{qzJ{' AQ3-W'JK'm8kFjRxl>>|"xwfĸK;3+G%m6>Ĥ' wSm>Iw-1|gU"gkܧc͸3~ &e\ Z /#%]^V0Dgϵ0zz/$JQ v0Bʫ9NXAWExm?n=fu9SQ.2~f|#)^J8Q-tܷ?𖌪E,z|Z۬[07Jy|B2N ( !AEJ5 K tKnt{ߟȑ~-؎BS9ʭA;t/Ϙ_+p۷6?[Ef/vB 礮*4A v\ŋKx.-O&wy :*0E {܈HW|>Ƣ(RxQD?8 0A]wO)Rsby١! BIP `@ގ $ޭSH9p`4YXW:)+`4T"zO}I.CFa\QYmT4M&g{ ^)crYj2"dl%ͣ ĸgd kMr_"@fz6vA^>at^.֋c{l^QrIݓP\;v/x fMp3tԷ2%*#ts@G "8'i=EG$8G.2]q<<&ja0UЪ*{ IDnqޗ5p͐!J Fm*Gadt\8blDwZ]^aW>rI@ީf,ǫOUM'hew}>⸤aW`!f#o WԾfcB.t52yp]|xΗd "geVZnu+Z4!#>k<,)D`D"lwPoOw~dD퍈Tppk0q)8HVBTS*ET ٙgYd>z}С$jDT65]UOfU(KIɨ^aQNBg`^=v mYP\-UmB}v =j`tO% 6#J`\1\C &Wuӷ86im.l3le}B>wE3m%lrïtcTnbix_2-t<83VGpK1iY56\z0u+[tP8 [HleH)WBL*O+LiUh-҇GO,,IQTdB4}RQVr@]Ϯv\QHb;56 Fd9ڏϝf/ZeWLT$鳇|Y2u鹗_V~_6}jK!g(cI۠`׍#~g-V` JMg?RjD@g${ 8|O=+a4?j*xK]!p5@K?X*Qʨyvl-ᅕ#_iʒ5t, ֣S,"|2k7O UkI0ߢ|eE&e: Ž(?EEUC?!L? GMG9iSUtjCK^zբXDNJBׯeGL9fUjIxɸƀŢ=j1[R27r]e.^ݩFL> z\LA\ʵ2UT@_-b4W"sk|v"-kmhrG0p!t޼ԉ4 2/њx(#LZR(;Tp1dM3u+ /.úA1tj|CYU3b>B*f:S88J\nJ@{咺&, T텰aYTzpXbl Vd~E [1F ro:z5%L =d0.*KۀqQWC Rʢ-q^yv0̏P+u̜E,b٩a*ƿ&GhXمEyn DF#CJ02),E)X㤪a])H*E`+2 pS |q% pdo8;!wurqxYPZ{d=o,< Z%hHⶶD9fwԒp2;exiTr=Av@6't|F~o0q41':#bdB\,Q-xX7F$È4ނEh~V}+UǿM #NMݭѿO -)ew/<)<g%-f9syj(,Ie&2`ɣCon!Bc%zcbS$uc>CR9e,Q>C*̱PjK) MtF!aXu^3`~^ *ꖈeDžp_kД`5)Dt l űt뫓*!݈Q(9c1mFrT]Q9;xp["ʒ=`Qh6VrZk1SET+SYwdv#N(JpaHM+G%& *!{,k'׽҆ѕO% H{`Sv~RCXDOA-2GM(oQ`Fc d&yI#|dz|g9˂hA$ ct3)s]BTdTڨ+nŨBf᭣Ȋ Ty/P>~3q=iz ӟOy)13Bu 1I>ANt]X\ iEV ij? _~ʹC,AXjSC:ˮ&lVbv\uXH}z@EyB[{9(K$kO€J<^0^bA:7p0LB ۠O-;.iWӧlರfj}v[졋h*q6TjbM׻|HT#H(ʙi 5+ $,l@iE ت- ;jWi\Q lvG/*~FCxWˮxPg` WO=|bg{fZUk7:e PYK)ā5m9qiXӧ ^,Ak0R*ayyÀ\ 5> ;,"&-F`ޓr)a{g$~QȆɞJ>& om+!8]뭓t70F٨lȔE#"R}.;dỤDWaѷ-2s|\#I* LZlss3Kb&\v<^evbBrtަK,)Fn׌}oap)t?0G 5d si>"((2E9.|Da@89m[ 7#5u)Ƒ}S3`俟[|R]H} b1C>\ԸCƪ&9ņPq,C"bdF1vdR6~r-%n6J@/%?*4P;OъA*dyp?ՠIvtS5L#U=D;{e"{ R@.FQ\dop~H$v^9~;p~kw=)Fɺ^Ũ K6XRd=8X0dy>RE-]FVgaO${l0-,no6?,ZbE7V]R(D;+rI%,rxDW>-I#UOZ,/d t)lG[}^TH`1fyޡ^wPs88KiaX Ch_t-wҞ_[$=ƂYJ5[p5X9S 4Yo!eAKY5Ļ+b']ۃDWqbќ`;8:;%I?V39ݗ; lzE>/:Vz da7TCn| Y.2QܜHCck0:5#R uC%ˡ^H$(̓II\> s_1N8O8c3&k6e6e:o;k5@sY*S0nY-9AŹB&yETǐ$b.TQdo;#&5l ">gq±3" y`q,'D0noҀ-@GĒK :/6ݳ.2}GųhLHVK2.J8K2ֶ9ԕe?D7|81:- sZ-Ƶ@eY%Xn[-yһS8\i:_i{SL>G{6.xYgn~]NUSm ;: i1VL<ìR TC.+V5$Qf'[B%>~]߫Ci3O$<7FRj3[%$gK{#KqY`?HLۨ+ 廷`HHQCQ84$$n!'#qtx-K\N@ 7}\E#z=VŚ@av=2i|'df& w ZZ{kWU!I IDAT]\rQֿc0DD;+8RY~uqR~l770(2'yd⾙%۬&Xz$VR C>#, /ݟ`7sic Q9l aZc["vEIP+ yETE"%2 K# զsq<0PNQKQf˷wE@u4P 6Y<_:Hb# q<.al/!W]UeX{2aj aF9K$6ay8= `2ʹ;>4-쥉TȤKwvlTl ԦX{GwmpS2eByco)ը1/ᳩd$X 463 &8S;ƛWTO8.OUe;yW *4>e6zRg?^QRa]w^j*QWraY#X) 1,"51ᗤL‼ +'4i"'$26@a)!9}?_JiQ, .HfuQlVG6$0Wc; 7ߛhbcdЎ:9>TF*CIZI-5/X=|x"B{XM'~@_g] U*l >=Xf2.J#m=O SwgӹJb0 55fI nV/e.J{l,$a|Кc@nݍsJvxfla4C\D&r@3@>Pڏqyʴ'o}#n;LWKQs_A;Ő(Շ3FlkPAήd~;I&ts/[5yт_F"ANE,}gIv0y[Cr+FHmmG$XT?)qNæ64G%{{edG P|21}QtYX M jmɆq<\6KmJq[t_ ts??!8;Q/} o8DZӯyľ]Yò)h!f|uT̹;;;z2Ai,CTy+.C6׎8}8 0nL:þZgrtf,IvI| tr3+.o{M_<`xh{KYЯ~@}9ޓm] UAKa`+!6/_ӡq5\nED孕"Y)T )Xw(riJb'q":l7,x3+]!Q1)dc =>X|(HhH*R+Q$:dtk[^N";WҮ?!Y&a0(Wp&}}-bJ1zi0NeG%K60P3/;J=Ah2LiR6MyREdEvyz}zt> *p@Qݻ-xg[YMϥv5S9g.u,zzV]FtKgQEA`JI<_j,xW̙RIMXt]x(4B |POTGǵȪ^/C#c00QA۾@b_MO<}j"׽u ;fHAs p`3u:1J(FêBVG,/'姲-ZL1sqEy==~\M"FcIyI/=s[qt\Fc/\wӓ.,.3!yFicR*txSOUE2e}vYD#oZtvZ~^fQ6LaxEYw+R$!11V\O_1_'{2rY8&OZN(=hSA4ACQJ2?@eYJ3` ⏶@Α.3 xdH!6i*7׍R8/`-Fdr(0V|pRũ^ݟ huxtaK(@tgjFTJ! !³scr,FO`촘M|Dou%g ,!62N7Qea4cc%L2H1GX2YlC2A@ "yx 7< ˔@q0&"B# >g!i+JrqdTcSԖëJONbj%$GP݈ &G9o1 k;ڹh!4JwRjcj$ G>kic0L2\!5 nTJ%yk4b|EgpZe̻*n?.~fj+e0+5;CwI,ȷP:MDh)L&{ @]Je"R`t0$'cAv/MCӤ2 8" HFNُ״R^Fc,e=X^'R}@i: csR690eL$Ah;~} [Yd=KFY=?'@+o< KSj}Ǣ+{ѹ•I_or&So'DjO-Q  3wŊAwF24ސBM۷ wnkT]զ`F}:4w5%di0Ƒ8W–ڭXȈ\GgXnm:>0 ?h/ Okk *vt/z/kPˎg~(Ŵ)CBAk}g{Qק?b=HѲWBUAn(VqѿA%g=}Ų vcp&`w0e ɥvj>HX4lФR0<#ܕ1O#5%`%7 q .rzۨJyeb͔po(XTWĎpbw4>i$H&އrm `mN/ .^ \O 6ՌKv4bbً'e|YoiR~ SM9> ~ֿyYg`8V]XUztTՓOa m%2?w&HۛZ%y{޼ k75 ^;} 88Gc"ĉȂ}i^H˅At:Y ASo2">ǽjJCQ*,><|޹}V螰}(@c R8dSH%>Q pAtC,va0rv ᲿH>{~v" hW6QX⪙c+/]kc(uk:%dl0as%R^it"A<\-o@v u9-X'Sh)J>,V~eL%#MPe. I_$.'3kk)jg E͵D&Z Y䍀 cI(L`닷o ]ǝ=,ISם|Õ3")ye5B@"N^$2kn$Z I ^38"LR"`Yp|E+0/}cd >?qzy$칋hX#j8(⥣ð C2i1PY0=2XRJ٥v%ش| WA9:d˭aQ0'hʷDRV9Ӻ*jcPjK-[:~HC+] gګ`P@K#~ YN4D?gB΂gӐ5&SQHN"/NnRHob²wZlJ7kp.kUH3Q47=FP RDDYW^ј/wjݍR.ķdݥwK1jk=)s{^ 롤P -Ƀyk<FF^ g,;g w2lCW[Ī'tTWxRIUOKA}X@ы OC߃ZitK豍Bՠ` e5 e)1 jC.0Ju3xaYl)`D ܳO': ]SXs]li~]p8*H>e~Y)K`dk$Ʒv8e1l # IFg͍83(r_Mוr,>>^ >(RzJ-]S|J qICEf.qBUjQ& 9#H AdXiyPNeNu4H)hǾo#E3L+}l cbOAZS< Aa=E8u9-G]J.-X,0}\Ry?Ut6;S3z0ԱtȤN|ѢHl1iS2V>\:."/}F TGZAΗ;4mZ`8P@aYZd73cw7yS ܧK9$b-NΩ^HxDW"Lq~CG* QCU+OȲ̏w(nT DI'h&ij`m 󕉳j-?vnq|FA е 4zOtb5@Pû1qTTH&fk%Y˯fZZH':KrPd'ڡ6v2s&0P=K"_$#/y%ƶ U62#n|#-et>*)^t4QW|Kƥ} Y)zўZvK<:M7LtsuC!;@ N&𩼣zfD2l~TJ@vp̆ OvsyƿN\ ׃x ],JPg!*_sP^1;dL:hh|! c@1op(1Ȩ6x#]d ܑy"EDSZafe\3?օx I Eɴt|,GčX~n@3fXP~쪭4Dv/Jl"8/kkU)Cܯͻ'U{aEu#d=8 jG Y/k'2L=M?H Ms\r RZ?(`V88d1SkleUx$;%)!0M4=+ Ըgi:?"!DTZze Z%4S2I~CVQڙWZPrΚUdXR0?[Af5[@$F,Ƴ Uq8398g_ݐ5dIMض36Aʷ g%)8Q_):BBVe],yTɻHb=5؀}l0qOmWڇz~F3Q~&q[Qlz.&f 7Ee уHDܶ}HOVxeX1 $y-Ab2%aUj |:^zA&NR4yop4Oc >XAJj{ sj#`tn[qkAy蔲L'[P|4)/۫;>â0af4/Q΋U'TXjQДlMydת uv.xtH7DRy"gf2mN@Lc3ųdtp )qHJiZSwF$6: 2z\KL}/hʀlݒa0p5!e.Q +$,3\} kj IDAT]Lқ0q^酓EV5kyM{h.P!z1LXeɪNѩgH^zđtHJKRRrJJir}|-D:oçıo4_? tLAXPZl'oR %, 4%3ĽI$Ne|dB)bP9+RLe22Ri =@qWTO7O봉\}U=RFl8vro%@4+Lf-I4+hfџDɁ5+ |8Up#WxFy+!q,S%KC)@:vMD6Dږq}1q +RP[\+x-4Kc:] =8.#5du_(Ưf#;f #Cf<"sM㮷+jiC jT$Mo#*;J^ZTJLcOc((O Kj#_3'm#)@45 &9LG]=ڕ}O"Ǖ1KdI7qӥ:8/@Dn>)i*sd-fK%pl4 ְUmL4 ʞ pÉVzC`UjP=h!!IZ!y' ҚiƲd%qBJ/ 56Z9$$| 嚹h&4wE9z1S]86mAggfJ?)cx40+n"c,Sr$oz㈷QtLA˂Ip n>#ۗ"*I('܊CDk!vϴ z ]1#XN1JwH3 >!=V%cm5[F^-=la|֚3TnWf۩T@`(bYNCgDo _+SFĵU2G`\2h q`*}el>BAKK^ESEcRx1t)IДum2"^DVW'vh"W-bl'(N>B/q l'sk&miPDpH*H?(TUY'[{<$eG H9ϭwފe<Dy;|/}cdx&Hg8,=!h ?* `$=2Vr^-;R ӘiE)$.Ԕ:V%ke`P"io:F|c{¶&9Y K^'eM"LkɎ埫 M]96df_(oބ;c{Z 7i_^bbiU8mS$pҨ Orf^OI\_dYx; lIIB,ú;7PL1'pwN0BOfB2jzd@yٚHbQƢ4x)fD0T™=DJ/II^mBej2?(53'BTN),jRJN7R)G5Y>@H"t*E ,WݾAͺ0apt^D>w4|7s ֨>@"ׅ$r^ROV5XDeqRd-oY3;ph^^áˢ|G"7>/ W|||W[뭨_\ח!!YM/w>.)omۜOqaMsjY$ɸPVcLhx!=yB $78ƀi]Ojrz_G ) ̋V9<)/B@ת>Q,{C1y+OXÇpc ,OQ_J׵0HfƟQoSV1ryTݶ #l;m*룼!&GE t,BdBKDt+ӽNi-^d[77gh9t&2@t %_5tM𣳼edwdM}xI~@,GxfcOc†v2D K|uuOl](F]d@/!Ygܐ>4~rDoQU b ,݅ىC!MHǗ*8iZ|2Q(<.Q[ĒJ],`縔36XVd6$rX7V]a`T.c+4tٚA`){Xl.CU< Fx=+QdiQ&me(sIF `sK,ߌjgk&)p;ED"598YD`Muy]5.a[}δJnʁų0RWbbO6p&bIT\, pP_]-]j뺂f<U:(QRS&g WtAfB{\ EIЋ&dwtUhIEL._>K6<=G|ဂ]79Z,2/+ jHl-$TT(m]#'cA CK|AR|()\]Вvww7\ݜc$V"|le-ӶVzhK,&G 8ߔ)*GHj#'a#c1{Wx@UW-fKyp,O>i)r,%YrƑȒÞWpW/$ܲ"]DPHΠGr(m72pJPpDڧHɤ>'9JY?-2C_h9\s0KeHlBw(.?tKtǛ>͉ŕ%;0.(dGu|b)SJ]$~\V`Rg8__I=ݏ;a"fGCf,r `gxm[JE m`ZL&uBDA1 3Sq|qkUV09Fg.P^0#TcM*֎; CU"a2fF fa_'@ZCfPj`k}dJXOd1`pRkb IYxnĔʸq%0T4UqiGX  \X3^ol(MZa]asl {G hMS**xh#5" |JyS3i#0~CkY:󬦚*P23N)f.Mtl$؃P`7cwLؚ =*7ϳ?", S#֐TNRp%HEas>3Rq_/55D1SQ;,STmXco _,8+v)q\IX7V%kF.,$s:Q&ЙSԷpw21B!`6`*J[ {ci#yUGk J߀0ZrOFGuoAޡ0hUDpqxBDOGYcrak1Yya$L{][0?f$O%P!>a@wEJhHjo=h]hSbZ0=LYf_@P 4w2ȡ0K*5bv)&hO=m#EF;qN3;Lϛ'1ӬHG|`,F>#2 &PԆF%*aH+IGG`׺@e^d'b1obgDKf1uzX8m2*D-9۵ L&?Ef0jI^PK|JJ{  yo6zwϴ+ҡq\J<:5`vN>~]iX8bd ]S,dW:ɇA寘FIrƽ_^ MOh0s Yɬ{`vURu{NLoɵBT(xQv$(4Mmt"3Hy>pVY@6.?|#=߅T2.uNH`2~78>NV%eB\yIɖp%*4{HO^[ѓǘ* 452O }(0>'+$e<-=NЗiA*u1Y%)+I~c8b'%k!E5vsa"#3Ri7\aw}o]Kalew_|1 mH=ydP:=JGE{2O1S5s'm fk9QYs|W7O>7}B.B:{;. DAH$g,SFCh(ڇ/x^v=\O/fIYt3L*k~ƾnٜ޻5v~m?blIg$z$.?k]:(<6}>~qKo`\49~XKrK׏Y̛W6"?a,ɓ^ℸ=oUϮu^om} {" uyW"{|ޭͺ??b"{|믯|㹝ه8-FO_WuG]nhyޞn|r{|\NPd(__~]|}Gzܾ 9}{z}~_B꧊\e9|<>GYW[\O7>ntaodB1xϋ?-ͧ_($ xƗCҫEN}`~2pv?k>p._v|U춹IќR$E>~G}OŮ}g;IA>i1 1=܈(noٯ?SW)˯I[dszׯ=v׿Ud[{W=s}_'-5El"pb,:XJQ1e|'їG6%kHr;\ .nG_lJRbϻ]="s;~YnHÕS}hnywi.xܚ#~v_E sn%q?Se0_}߿dzY|||8 cdH.)k}w'%!SGG J.鎁V,a|: V+_?i1z"E&f ]=fk݂֚W¦CO~qJz'ˡyf# ٕ'ױ! ,etsfG(`tr٥9-dsUd꿒^u諢kbh ЎNZ[BPR+ݹ?5DO @j뺵=ߔ$7{S}WhN7y~;ۣ U' qNb|BJU4DHDB$Iڋ%w{{ ㋅E"zEֲ;U9Fdz3|Z"{[>/!"QI}CIG4B]ESB},oYD{e"|oYP+mE&,.KIsjounꂯˠЈuI"p9e{U+=/eq|_o8kmEve/Vz驏KL2Xr5t {!Sjԭ J\DRdߐ$[WIҦvzg֓}obe]wiM\g~?$ǿ|w'ZCCkI\DO$.Iz/}{x60oߞڧrU7 r,$ES맰0 ZG{nk8Rx=4Y׏iȾUd;s<"[ݯ[F(Z9NiFN^bގ4'pjKA#{6DV`鷜IRb%IZ/gE]Næ?7O<-1UKу.5Ңg[|8{2Ro [Tئ& w{x s I8T$aZfBdCN[Zk~(sIӯ~ٟ\~OJՃhD~4;OPJf+؟u?t"CȨ2⁹2 F?V~O{_gNK%Q{"1 %> wЄ4௿Td?wjxkQFqO>11/4uCEC!X ѦbzʞEv?돴%ʼhCrPe+g&!FJ]tzRҞĨ }^{E>;->Lj/a7 ')?._l(12^?Sdk^eW*\הZpY%UJk*Q]dQd%qg~.[ihS?$3*닝UEv+HldZ-nͭ̏򽃡d=J.WdYbTekoqO0&]JTtV*t[ݕEAqO${;C~:{j(a&=C VKtSJKQb=$F_KqQmhBcWB<z(1OvU,?alK_B0ؙ ~L*EǑfN 8._Boop1YdCY/ۑ2SPLȮj=\C77d|ObJ,S@]̈́U?qyB [i4y*&ƤBD϶U#GHt6 ')v2ژI6-HNo?ӸJ& zPB?ƢYr1{,?x\b,VW7y|j~{E}\.+zMQs>L_8 T*ks/mw9m. Oֆ>eŸP5TNAƋn,(c$,tٵ:H@|M2.RQo>IyIDAT)N_0}L:3~LK- * Yl+7XU2NzBX (?c9RoNvM?Y_ne ,."fHjyguVL 4OQ+/2+ va6"}KI?G7h(]C~S"J9K][O{!ޱ%\h3& Q'bi\|nuࣇQ vx Irpm,5١O^5BXi|C!lBCbq{ְs1"st+ؙ+O0z{/nqK|1 H4GS܉;["]VRMyi Qd1 :(z YZ9=Yn$E?V|Q[`J="h,LXq2+K!c;snεy}n!Fs iF|q9.^9T AP"2ί ͦ,K _7֘m5H)evWoy|eM8isg'"FQ ?/I$@Z4#T^q@ +'P-i搇R͊g$>:9vEt!N BvJEk# c8l?v4od(.%vq`Vi*JFQ=t{6̨3#۰iaeD Uy^oF1:h6E3N^[Dy9lh 8{c. )W|y᧯w*]6R;UzVaTnYtG>NR{ka[a]bDv~yi J?w)CX٢ PyۥhQuLē 򒯭1h3(?*'I‹ PqSO`=`c;a/ߎ(%Ffpv(i72{ a[ALIwjBzRr̬\R`t>0%tm#.cPA"cs2)ӈ> "orH TӢt`*VeN'36'( $+{42KR1KAQArc׻PS;ӫ;e|2=K\m"F2qڣxw0@B*(Ž`;5$#XFӹY]^{BF] QSJ󡺵HwJt.iaGɬU<"ziCP|F=QgSQ#HX '^w۾NiP옊)6C{2l6 d)Y/!77k;^,hR"89S,*\;d?م;Y禓#yܒ:N|djդ'hmB*@G$Py5n[=d˷ȧ m UtkI} rCN[ YUPi;v|)0un@?:)3CzY3 TzX,>s!anQ.SBPL?lf~IxOD!N' |2vu ܐQ.[H8u*ڷMmtMm͍lxgb6Lqvl hAڍrjj8YIm:xM+>CI1[B(BFċJgrTW;n¦vfw:*>425UV "u2|KT&y^!@SQ\j04;6:^t:EZf&zvcGBeǃUXnGisAS>8bX W2;?t3e\!<*~rV?Ԗg Oޚ݉@:J d-u 9tfwiE\ [r~^ &b`LqRߋէ@S ^!NͱzL^fk}L\1SW&~C(cU*|H)m*#N7|!`sJ1)"S1:Z2EE6סkkku^_Ͽw^/㗺_.cŞHu^nѯr7 5~w|=Pܯ~Kz>WI|x~z}r=ɷ_o=yC-~wz<۷_Yd?|ܶNO?c?|Uo޺i>HcR6[}^Oъ"f,u"{OׯEv{XdD}\wz<_/ԯGOu0_o׿w'Z;p\OzȾ#~s=r]_?;=;돿_/?^? _ÿӯ;cx~ߪ?]呜kkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk}28qIENDB`commons-math3-3.2-src/src/main/java/org/apache/commons/math3/geometry/partitioning/Embedding.java100644 1750 1750 4760 12126627714 31616 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.geometry.partitioning; import org.apache.commons.math3.geometry.Vector; import org.apache.commons.math3.geometry.Space; /** This interface defines mappers between a space and one of its sub-spaces. *

                              Sub-spaces are the lower dimensions subsets of a n-dimensions * space. The (n-1)-dimension sub-spaces are specific sub-spaces known * as {@link Hyperplane hyperplanes}. This interface can be used regardless * of the dimensions differences. As an example, {@link * org.apache.commons.math3.geometry.euclidean.threed.Line Line} in 3D * implements Embedding<{@link * org.apache.commons.math3.geometry.euclidean.threed.Vector3D Vector3D}, {link * org.apache.commons.math3.geometry.euclidean.oned.Vector1D Vector1D>, i.e. it * maps directly dimensions 3 and 1.

                              *

                              In the 3D euclidean space, hyperplanes are 2D planes, and the 1D * sub-spaces are lines.

                              * @param Type of the embedding space. * @param Type of the embedded sub-space. * @see Hyperplane * @version $Id: Embedding.java 1416643 2012-12-03 19:37:14Z tn $ * @since 3.0 */ public interface Embedding { /** Transform a space point into a sub-space point. * @param point n-dimension point of the space * @return (n-1)-dimension point of the sub-space corresponding to * the specified space point * @see #toSpace */ Vector toSubSpace(Vector point); /** Transform a sub-space point into a space point. * @param point (n-1)-dimension point of the sub-space * @return n-dimension point of the space corresponding to the * specified sub-space point * @see #toSubSpace */ Vector toSpace(Vector point); } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/geometry/partitioning/Hyperplane.java100644 1750 1750 6403 12126627714 32043 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.geometry.partitioning; import org.apache.commons.math3.geometry.Vector; import org.apache.commons.math3.geometry.Space; /** This interface represents an hyperplane of a space. *

                              The most prominent place where hyperplane appears in space * partitioning is as cutters. Each partitioning node in a {@link * BSPTree BSP tree} has a cut {@link SubHyperplane sub-hyperplane} * which is either an hyperplane or a part of an hyperplane. In an * n-dimensions euclidean space, an hyperplane is an (n-1)-dimensions * hyperplane (for example a traditional plane in the 3D euclidean * space). They can be more exotic objects in specific fields, for * example a circle on the surface of the unit sphere.

                              * @param Type of the space. * @version $Id: Hyperplane.java 1416643 2012-12-03 19:37:14Z tn $ * @since 3.0 */ public interface Hyperplane { /** Copy the instance. *

                              The instance created is completely independant of the original * one. A deep copy is used, none of the underlying objects are * shared (except for immutable objects).

                              * @return a new hyperplane, copy of the instance */ Hyperplane copySelf(); /** Get the offset (oriented distance) of a point. *

                              The offset is 0 if the point is on the underlying hyperplane, * it is positive if the point is on one particular side of the * hyperplane, and it is negative if the point is on the other side, * according to the hyperplane natural orientation.

                              * @param point point to check * @return offset of the point */ double getOffset(Vector point); /** Check if the instance has the same orientation as another hyperplane. *

                              This method is expected to be called on parallel hyperplanes. The * method should not re-check for parallelism, only for * orientation, typically by testing something like the sign of the * dot-products of normals.

                              * @param other other hyperplane to check against the instance * @return true if the instance and the other hyperplane have * the same orientation */ boolean sameOrientationAs(Hyperplane other); /** Build a sub-hyperplane covering the whole hyperplane. * @return a sub-hyperplane covering the whole hyperplane */ SubHyperplane wholeHyperplane(); /** Build a region covering the whole space. * @return a region containing the instance */ Region wholeSpace(); } ././@LongLink100644 0 0 147 12126630646 10263 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/geometry/partitioning/BSPTreeVisitor.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/geometry/partitioning/BSPTreeVisitor.ja100644 1750 1750 10567 12126627714 32257 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.geometry.partitioning; import org.apache.commons.math3.geometry.Space; /** This interface is used to visit {@link BSPTree BSP tree} nodes. *

                              Navigation through {@link BSPTree BSP trees} can be done using * two different point of views:

                              *
                                *
                              • * the first one is in a node-oriented way using the {@link * BSPTree#getPlus}, {@link BSPTree#getMinus} and {@link * BSPTree#getParent} methods. Terminal nodes without associated * {@link SubHyperplane sub-hyperplanes} can be visited this way, * there is no constraint in the visit order, and it is possible * to visit either all nodes or only a subset of the nodes *
                              • *
                              • * the second one is in a sub-hyperplane-oriented way using * classes implementing this interface which obeys the visitor * design pattern. The visit order is provided by the visitor as * each node is first encountered. Each node is visited exactly * once. *
                              • *
                              * @param Type of the space. * @see BSPTree * @see SubHyperplane * @version $Id: BSPTreeVisitor.java 1416643 2012-12-03 19:37:14Z tn $ * @since 3.0 */ public interface BSPTreeVisitor { /** Enumerate for visit order with respect to plus sub-tree, minus sub-tree and cut sub-hyperplane. */ enum Order { /** Indicator for visit order plus sub-tree, then minus sub-tree, * and last cut sub-hyperplane. */ PLUS_MINUS_SUB, /** Indicator for visit order plus sub-tree, then cut sub-hyperplane, * and last minus sub-tree. */ PLUS_SUB_MINUS, /** Indicator for visit order minus sub-tree, then plus sub-tree, * and last cut sub-hyperplane. */ MINUS_PLUS_SUB, /** Indicator for visit order minus sub-tree, then cut sub-hyperplane, * and last plus sub-tree. */ MINUS_SUB_PLUS, /** Indicator for visit order cut sub-hyperplane, then plus sub-tree, * and last minus sub-tree. */ SUB_PLUS_MINUS, /** Indicator for visit order cut sub-hyperplane, then minus sub-tree, * and last plus sub-tree. */ SUB_MINUS_PLUS; } /** Determine the visit order for this node. *

                              Before attempting to visit an internal node, this method is * called to determine the desired ordering of the visit. It is * guaranteed that this method will be called before {@link * #visitInternalNode visitInternalNode} for a given node, it will be * called exactly once for each internal node.

                              * @param node BSP node guaranteed to have a non null cut sub-hyperplane * @return desired visit order, must be one of * {@link Order#PLUS_MINUS_SUB}, {@link Order#PLUS_SUB_MINUS}, * {@link Order#MINUS_PLUS_SUB}, {@link Order#MINUS_SUB_PLUS}, * {@link Order#SUB_PLUS_MINUS}, {@link Order#SUB_MINUS_PLUS} */ Order visitOrder(BSPTree node); /** Visit a BSP tree node node having a non-null sub-hyperplane. *

                              It is guaranteed that this method will be called after {@link * #visitOrder visitOrder} has been called for a given node, * it wil be called exactly once for each internal node.

                              * @param node BSP node guaranteed to have a non null cut sub-hyperplane * @see #visitLeafNode */ void visitInternalNode(BSPTree node); /** Visit a leaf BSP tree node node having a null sub-hyperplane. * @param node leaf BSP node having a null sub-hyperplane * @see #visitInternalNode */ void visitLeafNode(BSPTree node); } ././@LongLink100644 0 0 146 12126630646 10262 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/geometry/partitioning/RegionFactory.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/geometry/partitioning/RegionFactory.jav100644 1750 1750 24021 12126627714 32362 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.geometry.partitioning; import org.apache.commons.math3.geometry.Space; /** This class is a factory for {@link Region}. * @param Type of the space. * @version $Id: RegionFactory.java 1416643 2012-12-03 19:37:14Z tn $ * @since 3.0 */ public class RegionFactory { /** Visitor removing internal nodes attributes. */ private final NodesCleaner nodeCleaner; /** Simple constructor. */ public RegionFactory() { nodeCleaner = new NodesCleaner(); } /** Build a convex region from a collection of bounding hyperplanes. * @param hyperplanes collection of bounding hyperplanes * @return a new convex region, or null if the collection is empty */ public Region buildConvex(final Hyperplane ... hyperplanes) { if ((hyperplanes == null) || (hyperplanes.length == 0)) { return null; } // use the first hyperplane to build the right class final Region region = hyperplanes[0].wholeSpace(); // chop off parts of the space BSPTree node = region.getTree(false); node.setAttribute(Boolean.TRUE); for (final Hyperplane hyperplane : hyperplanes) { if (node.insertCut(hyperplane)) { node.setAttribute(null); node.getPlus().setAttribute(Boolean.FALSE); node = node.getMinus(); node.setAttribute(Boolean.TRUE); } } return region; } /** Compute the union of two regions. * @param region1 first region (will be unusable after the operation as * parts of it will be reused in the new region) * @param region2 second region (will be unusable after the operation as * parts of it will be reused in the new region) * @return a new region, result of {@code region1 union region2} */ public Region union(final Region region1, final Region region2) { final BSPTree tree = region1.getTree(false).merge(region2.getTree(false), new UnionMerger()); tree.visit(nodeCleaner); return region1.buildNew(tree); } /** Compute the intersection of two regions. * @param region1 first region (will be unusable after the operation as * parts of it will be reused in the new region) * @param region2 second region (will be unusable after the operation as * parts of it will be reused in the new region) * @return a new region, result of {@code region1 intersection region2} */ public Region intersection(final Region region1, final Region region2) { final BSPTree tree = region1.getTree(false).merge(region2.getTree(false), new IntersectionMerger()); tree.visit(nodeCleaner); return region1.buildNew(tree); } /** Compute the symmetric difference (exclusive or) of two regions. * @param region1 first region (will be unusable after the operation as * parts of it will be reused in the new region) * @param region2 second region (will be unusable after the operation as * parts of it will be reused in the new region) * @return a new region, result of {@code region1 xor region2} */ public Region xor(final Region region1, final Region region2) { final BSPTree tree = region1.getTree(false).merge(region2.getTree(false), new XorMerger()); tree.visit(nodeCleaner); return region1.buildNew(tree); } /** Compute the difference of two regions. * @param region1 first region (will be unusable after the operation as * parts of it will be reused in the new region) * @param region2 second region (will be unusable after the operation as * parts of it will be reused in the new region) * @return a new region, result of {@code region1 minus region2} */ public Region difference(final Region region1, final Region region2) { final BSPTree tree = region1.getTree(false).merge(region2.getTree(false), new DifferenceMerger()); tree.visit(nodeCleaner); return region1.buildNew(tree); } /** Get the complement of the region (exchanged interior/exterior). * @param region region to complement, it will not modified, a new * region independent region will be built * @return a new region, complement of the specified one */ public Region getComplement(final Region region) { return region.buildNew(recurseComplement(region.getTree(false))); } /** Recursively build the complement of a BSP tree. * @param node current node of the original tree * @return new tree, complement of the node */ private BSPTree recurseComplement(final BSPTree node) { if (node.getCut() == null) { return new BSPTree(((Boolean) node.getAttribute()) ? Boolean.FALSE : Boolean.TRUE); } @SuppressWarnings("unchecked") BoundaryAttribute attribute = (BoundaryAttribute) node.getAttribute(); if (attribute != null) { final SubHyperplane plusOutside = (attribute.getPlusInside() == null) ? null : attribute.getPlusInside().copySelf(); final SubHyperplane plusInside = (attribute.getPlusOutside() == null) ? null : attribute.getPlusOutside().copySelf(); attribute = new BoundaryAttribute(plusOutside, plusInside); } return new BSPTree(node.getCut().copySelf(), recurseComplement(node.getPlus()), recurseComplement(node.getMinus()), attribute); } /** BSP tree leaf merger computing union of two regions. */ private class UnionMerger implements BSPTree.LeafMerger { /** {@inheritDoc} */ public BSPTree merge(final BSPTree leaf, final BSPTree tree, final BSPTree parentTree, final boolean isPlusChild, final boolean leafFromInstance) { if ((Boolean) leaf.getAttribute()) { // the leaf node represents an inside cell leaf.insertInTree(parentTree, isPlusChild); return leaf; } // the leaf node represents an outside cell tree.insertInTree(parentTree, isPlusChild); return tree; } } /** BSP tree leaf merger computing union of two regions. */ private class IntersectionMerger implements BSPTree.LeafMerger { /** {@inheritDoc} */ public BSPTree merge(final BSPTree leaf, final BSPTree tree, final BSPTree parentTree, final boolean isPlusChild, final boolean leafFromInstance) { if ((Boolean) leaf.getAttribute()) { // the leaf node represents an inside cell tree.insertInTree(parentTree, isPlusChild); return tree; } // the leaf node represents an outside cell leaf.insertInTree(parentTree, isPlusChild); return leaf; } } /** BSP tree leaf merger computing union of two regions. */ private class XorMerger implements BSPTree.LeafMerger { /** {@inheritDoc} */ public BSPTree merge(final BSPTree leaf, final BSPTree tree, final BSPTree parentTree, final boolean isPlusChild, final boolean leafFromInstance) { BSPTree t = tree; if ((Boolean) leaf.getAttribute()) { // the leaf node represents an inside cell t = recurseComplement(t); } t.insertInTree(parentTree, isPlusChild); return t; } } /** BSP tree leaf merger computing union of two regions. */ private class DifferenceMerger implements BSPTree.LeafMerger { /** {@inheritDoc} */ public BSPTree merge(final BSPTree leaf, final BSPTree tree, final BSPTree parentTree, final boolean isPlusChild, final boolean leafFromInstance) { if ((Boolean) leaf.getAttribute()) { // the leaf node represents an inside cell final BSPTree argTree = recurseComplement(leafFromInstance ? tree : leaf); argTree.insertInTree(parentTree, isPlusChild); return argTree; } // the leaf node represents an outside cell final BSPTree instanceTree = leafFromInstance ? leaf : tree; instanceTree.insertInTree(parentTree, isPlusChild); return instanceTree; } } /** Visitor removing internal nodes attributes. */ private class NodesCleaner implements BSPTreeVisitor { /** {@inheritDoc} */ public Order visitOrder(final BSPTree node) { return Order.PLUS_SUB_MINUS; } /** {@inheritDoc} */ public void visitInternalNode(final BSPTree node) { node.setAttribute(null); } /** {@inheritDoc} */ public void visitLeafNode(final BSPTree node) { } } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/geometry/partitioning/Side.java100644 1750 1750 2511 12126627714 30614 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.geometry.partitioning; /** Enumerate representing the location of an element with respect to an * {@link Hyperplane hyperplane} of a space. * @version $Id: Side.java 1416643 2012-12-03 19:37:14Z tn $ * @since 3.0 */ public enum Side { /** Code for the plus side of the hyperplane. */ PLUS, /** Code for the minus side of the hyperplane. */ MINUS, /** Code for elements crossing the hyperplane from plus to minus side. */ BOTH, /** Code for the hyperplane itself. */ HYPER; } ././@LongLink100644 0 0 146 12126630646 10262 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/geometry/partitioning/SubHyperplane.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/geometry/partitioning/SubHyperplane.jav100644 1750 1750 11361 12126627714 32373 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.geometry.partitioning; import org.apache.commons.math3.geometry.Space; /** This interface represents the remaining parts of an hyperplane after * other parts have been chopped off. *

                              sub-hyperplanes are obtained when parts of an {@link * Hyperplane hyperplane} are chopped off by other hyperplanes that * intersect it. The remaining part is a convex region. Such objects * appear in {@link BSPTree BSP trees} as the intersection of a cut * hyperplane with the convex region which it splits, the chopping * hyperplanes are the cut hyperplanes closer to the tree root.

                              * @param Type of the embedding space. * @version $Id: SubHyperplane.java 1416643 2012-12-03 19:37:14Z tn $ * @since 3.0 */ public interface SubHyperplane { /** Copy the instance. *

                              The instance created is completely independent of the original * one. A deep copy is used, none of the underlying objects are * shared (except for the nodes attributes and immutable * objects).

                              * @return a new sub-hyperplane, copy of the instance */ SubHyperplane copySelf(); /** Get the underlying hyperplane. * @return underlying hyperplane */ Hyperplane getHyperplane(); /** Check if the instance is empty. * @return true if the instance is empty */ boolean isEmpty(); /** Get the size of the instance. * @return the size of the instance (this is a length in 1D, an area * in 2D, a volume in 3D ...) */ double getSize(); /** Compute the relative position of the instance with respect * to an hyperplane. * @param hyperplane hyperplane to check instance against * @return one of {@link Side#PLUS}, {@link Side#MINUS}, {@link Side#BOTH}, * {@link Side#HYPER} */ Side side(Hyperplane hyperplane); /** Split the instance in two parts by an hyperplane. * @param hyperplane splitting hyperplane * @return an object containing both the part of the instance * on the plus side of the instance and the part of the * instance on the minus side of the instance */ SplitSubHyperplane split(Hyperplane hyperplane); /** Compute the union of the instance and another sub-hyperplane. * @param other other sub-hyperplane to union (must be in the * same hyperplane as the instance) * @return a new sub-hyperplane, union of the instance and other */ SubHyperplane reunite(SubHyperplane other); /** Class holding the results of the {@link #split split} method. * @param Type of the embedding space. */ public static class SplitSubHyperplane { /** Part of the sub-hyperplane on the plus side of the splitting hyperplane. */ private final SubHyperplane plus; /** Part of the sub-hyperplane on the minus side of the splitting hyperplane. */ private final SubHyperplane minus; /** Build a SplitSubHyperplane from its parts. * @param plus part of the sub-hyperplane on the plus side of the * splitting hyperplane * @param minus part of the sub-hyperplane on the minus side of the * splitting hyperplane */ public SplitSubHyperplane(final SubHyperplane plus, final SubHyperplane minus) { this.plus = plus; this.minus = minus; } /** Get the part of the sub-hyperplane on the plus side of the splitting hyperplane. * @return part of the sub-hyperplane on the plus side of the splitting hyperplane */ public SubHyperplane getPlus() { return plus; } /** Get the part of the sub-hyperplane on the minus side of the splitting hyperplane. * @return part of the sub-hyperplane on the minus side of the splitting hyperplane */ public SubHyperplane getMinus() { return minus; } } } ././@LongLink100644 0 0 147 12126630646 10263 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/geometry/euclidean/twod/package-info.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/geometry/euclidean/twod/package-info.ja100644 1750 1750 1661 12126627714 32131 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ /** * *

                              * This package provides basic 2D geometry components. *

                              * */ package org.apache.commons.math3.geometry.euclidean.twod; ././@LongLink100644 0 0 146 12126630646 10262 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/geometry/euclidean/twod/NestedLoops.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/geometry/euclidean/twod/NestedLoops.jav100644 1750 1750 15744 12126627714 32261 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.geometry.euclidean.twod; import java.util.ArrayList; import java.util.Iterator; import org.apache.commons.math3.exception.MathIllegalArgumentException; import org.apache.commons.math3.exception.util.LocalizedFormats; import org.apache.commons.math3.geometry.euclidean.oned.IntervalsSet; import org.apache.commons.math3.geometry.partitioning.Region; import org.apache.commons.math3.geometry.partitioning.RegionFactory; import org.apache.commons.math3.geometry.partitioning.SubHyperplane; /** This class represent a tree of nested 2D boundary loops. *

                              This class is used for piecewise polygons construction. * Polygons are built using the outline edges as * representative of boundaries, the orientation of these lines are * meaningful. However, we want to allow the user to specify its * outline loops without having to take care of this orientation. This * class is devoted to correct mis-oriented loops.

                              *

                              Orientation is computed assuming the piecewise polygon is finite, * i.e. the outermost loops have their exterior side facing points at * infinity, and hence are oriented counter-clockwise. The orientation of * internal loops is computed as the reverse of the orientation of * their immediate surrounding loop.

                              * @version $Id: NestedLoops.java 1416643 2012-12-03 19:37:14Z tn $ * @since 3.0 */ class NestedLoops { /** Boundary loop. */ private Vector2D[] loop; /** Surrounded loops. */ private ArrayList surrounded; /** Polygon enclosing a finite region. */ private Region polygon; /** Indicator for original loop orientation. */ private boolean originalIsClockwise; /** Simple Constructor. *

                              Build an empty tree of nested loops. This instance will become * the root node of a complete tree, it is not associated with any * loop by itself, the outermost loops are in the root tree child * nodes.

                              */ public NestedLoops() { surrounded = new ArrayList(); } /** Constructor. *

                              Build a tree node with neither parent nor children

                              * @param loop boundary loop (will be reversed in place if needed) * @exception MathIllegalArgumentException if an outline has an open boundary loop */ private NestedLoops(final Vector2D[] loop) throws MathIllegalArgumentException { if (loop[0] == null) { throw new MathIllegalArgumentException(LocalizedFormats.OUTLINE_BOUNDARY_LOOP_OPEN); } this.loop = loop; surrounded = new ArrayList(); // build the polygon defined by the loop final ArrayList> edges = new ArrayList>(); Vector2D current = loop[loop.length - 1]; for (int i = 0; i < loop.length; ++i) { final Vector2D previous = current; current = loop[i]; final Line line = new Line(previous, current); final IntervalsSet region = new IntervalsSet(line.toSubSpace(previous).getX(), line.toSubSpace(current).getX()); edges.add(new SubLine(line, region)); } polygon = new PolygonsSet(edges); // ensure the polygon encloses a finite region of the plane if (Double.isInfinite(polygon.getSize())) { polygon = new RegionFactory().getComplement(polygon); originalIsClockwise = false; } else { originalIsClockwise = true; } } /** Add a loop in a tree. * @param bLoop boundary loop (will be reversed in place if needed) * @exception MathIllegalArgumentException if an outline has crossing * boundary loops or open boundary loops */ public void add(final Vector2D[] bLoop) throws MathIllegalArgumentException { add(new NestedLoops(bLoop)); } /** Add a loop in a tree. * @param node boundary loop (will be reversed in place if needed) * @exception MathIllegalArgumentException if an outline has boundary * loops that cross each other */ private void add(final NestedLoops node) throws MathIllegalArgumentException { // check if we can go deeper in the tree for (final NestedLoops child : surrounded) { if (child.polygon.contains(node.polygon)) { child.add(node); return; } } // check if we can absorb some of the instance children for (final Iterator iterator = surrounded.iterator(); iterator.hasNext();) { final NestedLoops child = iterator.next(); if (node.polygon.contains(child.polygon)) { node.surrounded.add(child); iterator.remove(); } } // we should be separate from the remaining children RegionFactory factory = new RegionFactory(); for (final NestedLoops child : surrounded) { if (!factory.intersection(node.polygon, child.polygon).isEmpty()) { throw new MathIllegalArgumentException(LocalizedFormats.CROSSING_BOUNDARY_LOOPS); } } surrounded.add(node); } /** Correct the orientation of the loops contained in the tree. *

                              This is this method that really inverts the loops that where * provided through the {@link #add(Vector2D[]) add} method if * they are mis-oriented

                              */ public void correctOrientation() { for (NestedLoops child : surrounded) { child.setClockWise(true); } } /** Set the loop orientation. * @param clockwise if true, the loop should be set to clockwise * orientation */ private void setClockWise(final boolean clockwise) { if (originalIsClockwise ^ clockwise) { // we need to inverse the original loop int min = -1; int max = loop.length; while (++min < --max) { final Vector2D tmp = loop[min]; loop[min] = loop[max]; loop[max] = tmp; } } // go deeper in the tree for (final NestedLoops child : surrounded) { child.setClockWise(!clockwise); } } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/geometry/euclidean/twod/SubLine.java100644 1750 1750 22764 12126627714 31524 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.geometry.euclidean.twod; import java.util.ArrayList; import java.util.List; import org.apache.commons.math3.geometry.euclidean.oned.Euclidean1D; import org.apache.commons.math3.geometry.euclidean.oned.Interval; import org.apache.commons.math3.geometry.euclidean.oned.IntervalsSet; import org.apache.commons.math3.geometry.euclidean.oned.OrientedPoint; import org.apache.commons.math3.geometry.euclidean.oned.Vector1D; import org.apache.commons.math3.geometry.partitioning.AbstractSubHyperplane; import org.apache.commons.math3.geometry.partitioning.BSPTree; import org.apache.commons.math3.geometry.partitioning.Hyperplane; import org.apache.commons.math3.geometry.partitioning.Region; import org.apache.commons.math3.geometry.partitioning.Region.Location; import org.apache.commons.math3.geometry.partitioning.Side; import org.apache.commons.math3.geometry.partitioning.SubHyperplane; import org.apache.commons.math3.util.FastMath; /** This class represents a sub-hyperplane for {@link Line}. * @version $Id: SubLine.java 1416643 2012-12-03 19:37:14Z tn $ * @since 3.0 */ public class SubLine extends AbstractSubHyperplane { /** Simple constructor. * @param hyperplane underlying hyperplane * @param remainingRegion remaining region of the hyperplane */ public SubLine(final Hyperplane hyperplane, final Region remainingRegion) { super(hyperplane, remainingRegion); } /** Create a sub-line from two endpoints. * @param start start point * @param end end point */ public SubLine(final Vector2D start, final Vector2D end) { super(new Line(start, end), buildIntervalSet(start, end)); } /** Create a sub-line from a segment. * @param segment single segment forming the sub-line */ public SubLine(final Segment segment) { super(segment.getLine(), buildIntervalSet(segment.getStart(), segment.getEnd())); } /** Get the endpoints of the sub-line. *

                              * A subline may be any arbitrary number of disjoints segments, so the endpoints * are provided as a list of endpoint pairs. Each element of the list represents * one segment, and each segment contains a start point at index 0 and an end point * at index 1. If the sub-line is unbounded in the negative infinity direction, * the start point of the first segment will have infinite coordinates. If the * sub-line is unbounded in the positive infinity direction, the end point of the * last segment will have infinite coordinates. So a sub-line covering the whole * line will contain just one row and both elements of this row will have infinite * coordinates. If the sub-line is empty, the returned list will contain 0 segments. *

                              * @return list of segments endpoints */ public List getSegments() { final Line line = (Line) getHyperplane(); final List list = ((IntervalsSet) getRemainingRegion()).asList(); final List segments = new ArrayList(); for (final Interval interval : list) { final Vector2D start = line.toSpace(new Vector1D(interval.getInf())); final Vector2D end = line.toSpace(new Vector1D(interval.getSup())); segments.add(new Segment(start, end, line)); } return segments; } /** Get the intersection of the instance and another sub-line. *

                              * This method is related to the {@link Line#intersection(Line) * intersection} method in the {@link Line Line} class, but in addition * to compute the point along infinite lines, it also checks the point * lies on both sub-line ranges. *

                              * @param subLine other sub-line which may intersect instance * @param includeEndPoints if true, endpoints are considered to belong to * instance (i.e. they are closed sets) and may be returned, otherwise endpoints * are considered to not belong to instance (i.e. they are open sets) and intersection * occurring on endpoints lead to null being returned * @return the intersection point if there is one, null if the sub-lines don't intersect */ public Vector2D intersection(final SubLine subLine, final boolean includeEndPoints) { // retrieve the underlying lines Line line1 = (Line) getHyperplane(); Line line2 = (Line) subLine.getHyperplane(); // compute the intersection on infinite line Vector2D v2D = line1.intersection(line2); // check location of point with respect to first sub-line Location loc1 = getRemainingRegion().checkPoint(line1.toSubSpace(v2D)); // check location of point with respect to second sub-line Location loc2 = subLine.getRemainingRegion().checkPoint(line2.toSubSpace(v2D)); if (includeEndPoints) { return ((loc1 != Location.OUTSIDE) && (loc2 != Location.OUTSIDE)) ? v2D : null; } else { return ((loc1 == Location.INSIDE) && (loc2 == Location.INSIDE)) ? v2D : null; } } /** Build an interval set from two points. * @param start start point * @param end end point * @return an interval set */ private static IntervalsSet buildIntervalSet(final Vector2D start, final Vector2D end) { final Line line = new Line(start, end); return new IntervalsSet(line.toSubSpace(start).getX(), line.toSubSpace(end).getX()); } /** {@inheritDoc} */ @Override protected AbstractSubHyperplane buildNew(final Hyperplane hyperplane, final Region remainingRegion) { return new SubLine(hyperplane, remainingRegion); } /** {@inheritDoc} */ @Override public Side side(final Hyperplane hyperplane) { final Line thisLine = (Line) getHyperplane(); final Line otherLine = (Line) hyperplane; final Vector2D crossing = thisLine.intersection(otherLine); if (crossing == null) { // the lines are parallel, final double global = otherLine.getOffset(thisLine); return (global < -1.0e-10) ? Side.MINUS : ((global > 1.0e-10) ? Side.PLUS : Side.HYPER); } // the lines do intersect final boolean direct = FastMath.sin(thisLine.getAngle() - otherLine.getAngle()) < 0; final Vector1D x = thisLine.toSubSpace(crossing); return getRemainingRegion().side(new OrientedPoint(x, direct)); } /** {@inheritDoc} */ @Override public SplitSubHyperplane split(final Hyperplane hyperplane) { final Line thisLine = (Line) getHyperplane(); final Line otherLine = (Line) hyperplane; final Vector2D crossing = thisLine.intersection(otherLine); if (crossing == null) { // the lines are parallel final double global = otherLine.getOffset(thisLine); return (global < -1.0e-10) ? new SplitSubHyperplane(null, this) : new SplitSubHyperplane(this, null); } // the lines do intersect final boolean direct = FastMath.sin(thisLine.getAngle() - otherLine.getAngle()) < 0; final Vector1D x = thisLine.toSubSpace(crossing); final SubHyperplane subPlus = new OrientedPoint(x, !direct).wholeHyperplane(); final SubHyperplane subMinus = new OrientedPoint(x, direct).wholeHyperplane(); final BSPTree splitTree = getRemainingRegion().getTree(false).split(subMinus); final BSPTree plusTree = getRemainingRegion().isEmpty(splitTree.getPlus()) ? new BSPTree(Boolean.FALSE) : new BSPTree(subPlus, new BSPTree(Boolean.FALSE), splitTree.getPlus(), null); final BSPTree minusTree = getRemainingRegion().isEmpty(splitTree.getMinus()) ? new BSPTree(Boolean.FALSE) : new BSPTree(subMinus, new BSPTree(Boolean.FALSE), splitTree.getMinus(), null); return new SplitSubHyperplane(new SubLine(thisLine.copySelf(), new IntervalsSet(plusTree)), new SubLine(thisLine.copySelf(), new IntervalsSet(minusTree))); } } ././@LongLink100644 0 0 146 12126630646 10262 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/geometry/euclidean/twod/Euclidean2D.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/geometry/euclidean/twod/Euclidean2D.jav100644 1750 1750 4474 12126627714 32057 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.geometry.euclidean.twod; import java.io.Serializable; import org.apache.commons.math3.geometry.Space; import org.apache.commons.math3.geometry.euclidean.oned.Euclidean1D; /** * This class implements a three-dimensional space. * @version $Id: Euclidean2D.java 1416643 2012-12-03 19:37:14Z tn $ * @since 3.0 */ public class Euclidean2D implements Serializable, Space { /** Serializable version identifier. */ private static final long serialVersionUID = 4793432849757649566L; /** Private constructor for the singleton. */ private Euclidean2D() { } /** Get the unique instance. * @return the unique instance */ public static Euclidean2D getInstance() { return LazyHolder.INSTANCE; } /** {@inheritDoc} */ public int getDimension() { return 2; } /** {@inheritDoc} */ public Euclidean1D getSubSpace() { return Euclidean1D.getInstance(); } // CHECKSTYLE: stop HideUtilityClassConstructor /** Holder for the instance. *

                              We use here the Initialization On Demand Holder Idiom.

                              */ private static class LazyHolder { /** Cached field instance. */ private static final Euclidean2D INSTANCE = new Euclidean2D(); } // CHECKSTYLE: resume HideUtilityClassConstructor /** Handle deserialization of the singleton. * @return the singleton instance */ private Object readResolve() { // return the singleton instance return LazyHolder.INSTANCE; } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/geometry/euclidean/twod/Segment.java100644 1750 1750 7426 12126627714 31543 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.geometry.euclidean.twod; import org.apache.commons.math3.util.FastMath; /** Simple container for a two-points segment. * @version $Id: Segment.java 1422195 2012-12-15 06:45:18Z psteitz $ * @since 3.0 */ public class Segment { /** Start point of the segment. */ private final Vector2D start; /** End point of the segments. */ private final Vector2D end; /** Line containing the segment. */ private final Line line; /** Build a segment. * @param start start point of the segment * @param end end point of the segment * @param line line containing the segment */ public Segment(final Vector2D start, final Vector2D end, final Line line) { this.start = start; this.end = end; this.line = line; } /** Get the start point of the segment. * @return start point of the segment */ public Vector2D getStart() { return start; } /** Get the end point of the segment. * @return end point of the segment */ public Vector2D getEnd() { return end; } /** Get the line containing the segment. * @return line containing the segment */ public Line getLine() { return line; } /** Calculates the shortest distance from a point to this line segment. *

                              * If the perpendicular extension from the point to the line does not * cross in the bounds of the line segment, the shortest distance to * the two end points will be returned. *

                              * * Algorithm adapted from: * * Thread @ Codeguru * * @param p to check * @return distance between the instance and the point * @since 3.1 */ public double distance(final Vector2D p) { final double deltaX = end.getX() - start.getX(); final double deltaY = end.getY() - start.getY(); final double r = ((p.getX() - start.getX()) * deltaX + (p.getY() - start.getY()) * deltaY) / (deltaX * deltaX + deltaY * deltaY); // r == 0 => P = startPt // r == 1 => P = endPt // r < 0 => P is on the backward extension of the segment // r > 1 => P is on the forward extension of the segment // 0 < r < 1 => P is on the segment // if point isn't on the line segment, just return the shortest distance to the end points if (r < 0 || r > 1) { final double dist1 = getStart().distance(p); final double dist2 = getEnd().distance(p); return FastMath.min(dist1, dist2); } else { // find point on line and see if it is in the line segment final double px = start.getX() + r * deltaX; final double py = start.getY() + r * deltaY; final Vector2D interPt = new Vector2D(px, py); return interPt.distance(p); } } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/geometry/euclidean/twod/Line.java100644 1750 1750 40543 12126627714 31045 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.geometry.euclidean.twod; import java.awt.geom.AffineTransform; import org.apache.commons.math3.exception.MathIllegalArgumentException; import org.apache.commons.math3.exception.util.LocalizedFormats; import org.apache.commons.math3.geometry.Vector; import org.apache.commons.math3.geometry.euclidean.oned.Euclidean1D; import org.apache.commons.math3.geometry.euclidean.oned.IntervalsSet; import org.apache.commons.math3.geometry.euclidean.oned.OrientedPoint; import org.apache.commons.math3.geometry.euclidean.oned.Vector1D; import org.apache.commons.math3.geometry.partitioning.Embedding; import org.apache.commons.math3.geometry.partitioning.Hyperplane; import org.apache.commons.math3.geometry.partitioning.SubHyperplane; import org.apache.commons.math3.geometry.partitioning.Transform; import org.apache.commons.math3.util.FastMath; import org.apache.commons.math3.util.MathUtils; /** This class represents an oriented line in the 2D plane. *

                              An oriented line can be defined either by prolongating a line * segment between two points past these points, or by one point and * an angular direction (in trigonometric orientation).

                              *

                              Since it is oriented the two half planes at its two sides are * unambiguously identified as a left half plane and a right half * plane. This can be used to identify the interior and the exterior * in a simple way by local properties only when part of a line is * used to define part of a polygon boundary.

                              *

                              A line can also be used to completely define a reference frame * in the plane. It is sufficient to select one specific point in the * line (the orthogonal projection of the original reference frame on * the line) and to use the unit vector in the line direction and the * orthogonal vector oriented from left half plane to right half * plane. We define two coordinates by the process, the * abscissa along the line, and the offset across * the line. All points of the plane are uniquely identified by these * two coordinates. The line is the set of points at zero offset, the * left half plane is the set of points with negative offsets and the * right half plane is the set of points with positive offsets.

                              * @version $Id: Line.java 1422195 2012-12-15 06:45:18Z psteitz $ * @since 3.0 */ public class Line implements Hyperplane, Embedding { /** Angle with respect to the abscissa axis. */ private double angle; /** Cosine of the line angle. */ private double cos; /** Sine of the line angle. */ private double sin; /** Offset of the frame origin. */ private double originOffset; /** Build a line from two points. *

                              The line is oriented from p1 to p2

                              * @param p1 first point * @param p2 second point */ public Line(final Vector2D p1, final Vector2D p2) { reset(p1, p2); } /** Build a line from a point and an angle. * @param p point belonging to the line * @param angle angle of the line with respect to abscissa axis */ public Line(final Vector2D p, final double angle) { reset(p, angle); } /** Build a line from its internal characteristics. * @param angle angle of the line with respect to abscissa axis * @param cos cosine of the angle * @param sin sine of the angle * @param originOffset offset of the origin */ private Line(final double angle, final double cos, final double sin, final double originOffset) { this.angle = angle; this.cos = cos; this.sin = sin; this.originOffset = originOffset; } /** Copy constructor. *

                              The created instance is completely independent from the * original instance, it is a deep copy.

                              * @param line line to copy */ public Line(final Line line) { angle = MathUtils.normalizeAngle(line.angle, FastMath.PI); cos = FastMath.cos(angle); sin = FastMath.sin(angle); originOffset = line.originOffset; } /** {@inheritDoc} */ public Line copySelf() { return new Line(this); } /** Reset the instance as if built from two points. *

                              The line is oriented from p1 to p2

                              * @param p1 first point * @param p2 second point */ public void reset(final Vector2D p1, final Vector2D p2) { final double dx = p2.getX() - p1.getX(); final double dy = p2.getY() - p1.getY(); final double d = FastMath.hypot(dx, dy); if (d == 0.0) { angle = 0.0; cos = 1.0; sin = 0.0; originOffset = p1.getY(); } else { angle = FastMath.PI + FastMath.atan2(-dy, -dx); cos = FastMath.cos(angle); sin = FastMath.sin(angle); originOffset = (p2.getX() * p1.getY() - p1.getX() * p2.getY()) / d; } } /** Reset the instance as if built from a line and an angle. * @param p point belonging to the line * @param alpha angle of the line with respect to abscissa axis */ public void reset(final Vector2D p, final double alpha) { this.angle = MathUtils.normalizeAngle(alpha, FastMath.PI); cos = FastMath.cos(this.angle); sin = FastMath.sin(this.angle); originOffset = cos * p.getY() - sin * p.getX(); } /** Revert the instance. */ public void revertSelf() { if (angle < FastMath.PI) { angle += FastMath.PI; } else { angle -= FastMath.PI; } cos = -cos; sin = -sin; originOffset = -originOffset; } /** Get the reverse of the instance. *

                              Get a line with reversed orientation with respect to the * instance. A new object is built, the instance is untouched.

                              * @return a new line, with orientation opposite to the instance orientation */ public Line getReverse() { return new Line((angle < FastMath.PI) ? (angle + FastMath.PI) : (angle - FastMath.PI), -cos, -sin, -originOffset); } /** {@inheritDoc} */ public Vector1D toSubSpace(final Vector point) { Vector2D p2 = (Vector2D) point; return new Vector1D(cos * p2.getX() + sin * p2.getY()); } /** {@inheritDoc} */ public Vector2D toSpace(final Vector point) { final double abscissa = ((Vector1D) point).getX(); return new Vector2D(abscissa * cos - originOffset * sin, abscissa * sin + originOffset * cos); } /** Get the intersection point of the instance and another line. * @param other other line * @return intersection point of the instance and the other line * or null if there are no intersection points */ public Vector2D intersection(final Line other) { final double d = sin * other.cos - other.sin * cos; if (FastMath.abs(d) < 1.0e-10) { return null; } return new Vector2D((cos * other.originOffset - other.cos * originOffset) / d, (sin * other.originOffset - other.sin * originOffset) / d); } /** {@inheritDoc} */ public SubLine wholeHyperplane() { return new SubLine(this, new IntervalsSet()); } /** Build a region covering the whole space. * @return a region containing the instance (really a {@link * PolygonsSet PolygonsSet} instance) */ public PolygonsSet wholeSpace() { return new PolygonsSet(); } /** Get the offset (oriented distance) of a parallel line. *

                              This method should be called only for parallel lines otherwise * the result is not meaningful.

                              *

                              The offset is 0 if both lines are the same, it is * positive if the line is on the right side of the instance and * negative if it is on the left side, according to its natural * orientation.

                              * @param line line to check * @return offset of the line */ public double getOffset(final Line line) { return originOffset + ((cos * line.cos + sin * line.sin > 0) ? -line.originOffset : line.originOffset); } /** {@inheritDoc} */ public double getOffset(final Vector point) { Vector2D p2 = (Vector2D) point; return sin * p2.getX() - cos * p2.getY() + originOffset; } /** {@inheritDoc} */ public boolean sameOrientationAs(final Hyperplane other) { final Line otherL = (Line) other; return (sin * otherL.sin + cos * otherL.cos) >= 0.0; } /** Get one point from the plane. * @param abscissa desired abscissa for the point * @param offset desired offset for the point * @return one point in the plane, with given abscissa and offset * relative to the line */ public Vector2D getPointAt(final Vector1D abscissa, final double offset) { final double x = abscissa.getX(); final double dOffset = offset - originOffset; return new Vector2D(x * cos + dOffset * sin, x * sin - dOffset * cos); } /** Check if the line contains a point. * @param p point to check * @return true if p belongs to the line */ public boolean contains(final Vector2D p) { return FastMath.abs(getOffset(p)) < 1.0e-10; } /** Compute the distance between the instance and a point. *

                              This is a shortcut for invoking FastMath.abs(getOffset(p)), * and provides consistency with what is in the * org.apache.commons.math3.geometry.euclidean.threed.Line class.

                              * * @param p to check * @return distance between the instance and the point * @since 3.1 */ public double distance(final Vector2D p) { return FastMath.abs(getOffset(p)); } /** Check the instance is parallel to another line. * @param line other line to check * @return true if the instance is parallel to the other line * (they can have either the same or opposite orientations) */ public boolean isParallelTo(final Line line) { return FastMath.abs(sin * line.cos - cos * line.sin) < 1.0e-10; } /** Translate the line to force it passing by a point. * @param p point by which the line should pass */ public void translateToPoint(final Vector2D p) { originOffset = cos * p.getY() - sin * p.getX(); } /** Get the angle of the line. * @return the angle of the line with respect to the abscissa axis */ public double getAngle() { return MathUtils.normalizeAngle(angle, FastMath.PI); } /** Set the angle of the line. * @param angle new angle of the line with respect to the abscissa axis */ public void setAngle(final double angle) { this.angle = MathUtils.normalizeAngle(angle, FastMath.PI); cos = FastMath.cos(this.angle); sin = FastMath.sin(this.angle); } /** Get the offset of the origin. * @return the offset of the origin */ public double getOriginOffset() { return originOffset; } /** Set the offset of the origin. * @param offset offset of the origin */ public void setOriginOffset(final double offset) { originOffset = offset; } /** Get a {@link org.apache.commons.math3.geometry.partitioning.Transform * Transform} embedding an affine transform. * @param transform affine transform to embed (must be inversible * otherwise the {@link * org.apache.commons.math3.geometry.partitioning.Transform#apply(Hyperplane) * apply(Hyperplane)} method would work only for some lines, and * fail for other ones) * @return a new transform that can be applied to either {@link * Vector2D Vector2D}, {@link Line Line} or {@link * org.apache.commons.math3.geometry.partitioning.SubHyperplane * SubHyperplane} instances * @exception MathIllegalArgumentException if the transform is non invertible */ public static Transform getTransform(final AffineTransform transform) throws MathIllegalArgumentException { return new LineTransform(transform); } /** Class embedding an affine transform. *

                              This class is used in order to apply an affine transform to a * line. Using a specific object allow to perform some computations * on the transform only once even if the same transform is to be * applied to a large number of lines (for example to a large * polygon)./

                              */ private static class LineTransform implements Transform { // CHECKSTYLE: stop JavadocVariable check private double cXX; private double cXY; private double cX1; private double cYX; private double cYY; private double cY1; private double c1Y; private double c1X; private double c11; // CHECKSTYLE: resume JavadocVariable check /** Build an affine line transform from a n {@code AffineTransform}. * @param transform transform to use (must be invertible otherwise * the {@link LineTransform#apply(Hyperplane)} method would work * only for some lines, and fail for other ones) * @exception MathIllegalArgumentException if the transform is non invertible */ public LineTransform(final AffineTransform transform) throws MathIllegalArgumentException { final double[] m = new double[6]; transform.getMatrix(m); cXX = m[0]; cXY = m[2]; cX1 = m[4]; cYX = m[1]; cYY = m[3]; cY1 = m[5]; c1Y = cXY * cY1 - cYY * cX1; c1X = cXX * cY1 - cYX * cX1; c11 = cXX * cYY - cYX * cXY; if (FastMath.abs(c11) < 1.0e-20) { throw new MathIllegalArgumentException(LocalizedFormats.NON_INVERTIBLE_TRANSFORM); } } /** {@inheritDoc} */ public Vector2D apply(final Vector point) { final Vector2D p2D = (Vector2D) point; final double x = p2D.getX(); final double y = p2D.getY(); return new Vector2D(cXX * x + cXY * y + cX1, cYX * x + cYY * y + cY1); } /** {@inheritDoc} */ public Line apply(final Hyperplane hyperplane) { final Line line = (Line) hyperplane; final double rOffset = c1X * line.cos + c1Y * line.sin + c11 * line.originOffset; final double rCos = cXX * line.cos + cXY * line.sin; final double rSin = cYX * line.cos + cYY * line.sin; final double inv = 1.0 / FastMath.sqrt(rSin * rSin + rCos * rCos); return new Line(FastMath.PI + FastMath.atan2(-rSin, -rCos), inv * rCos, inv * rSin, inv * rOffset); } /** {@inheritDoc} */ public SubHyperplane apply(final SubHyperplane sub, final Hyperplane original, final Hyperplane transformed) { final OrientedPoint op = (OrientedPoint) sub.getHyperplane(); final Line originalLine = (Line) original; final Line transformedLine = (Line) transformed; final Vector1D newLoc = transformedLine.toSubSpace(apply(originalLine.toSpace(op.getLocation()))); return new OrientedPoint(newLoc, op.isDirect()).wholeHyperplane(); } } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/geometry/euclidean/twod/Vector2D.java100644 1750 1750 30446 12126627714 31607 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.geometry.euclidean.twod; import java.text.NumberFormat; import org.apache.commons.math3.exception.DimensionMismatchException; import org.apache.commons.math3.exception.MathArithmeticException; import org.apache.commons.math3.exception.util.LocalizedFormats; import org.apache.commons.math3.geometry.Space; import org.apache.commons.math3.geometry.Vector; import org.apache.commons.math3.util.FastMath; import org.apache.commons.math3.util.MathUtils; /** This class represents a 2D vector. *

                              Instances of this class are guaranteed to be immutable.

                              * @version $Id: Vector2D.java 1416643 2012-12-03 19:37:14Z tn $ * @since 3.0 */ public class Vector2D implements Vector { /** Origin (coordinates: 0, 0). */ public static final Vector2D ZERO = new Vector2D(0, 0); // CHECKSTYLE: stop ConstantName /** A vector with all coordinates set to NaN. */ public static final Vector2D NaN = new Vector2D(Double.NaN, Double.NaN); // CHECKSTYLE: resume ConstantName /** A vector with all coordinates set to positive infinity. */ public static final Vector2D POSITIVE_INFINITY = new Vector2D(Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY); /** A vector with all coordinates set to negative infinity. */ public static final Vector2D NEGATIVE_INFINITY = new Vector2D(Double.NEGATIVE_INFINITY, Double.NEGATIVE_INFINITY); /** Serializable UID. */ private static final long serialVersionUID = 266938651998679754L; /** Abscissa. */ private final double x; /** Ordinate. */ private final double y; /** Simple constructor. * Build a vector from its coordinates * @param x abscissa * @param y ordinate * @see #getX() * @see #getY() */ public Vector2D(double x, double y) { this.x = x; this.y = y; } /** Simple constructor. * Build a vector from its coordinates * @param v coordinates array * @exception DimensionMismatchException if array does not have 2 elements * @see #toArray() */ public Vector2D(double[] v) throws DimensionMismatchException { if (v.length != 2) { throw new DimensionMismatchException(v.length, 2); } this.x = v[0]; this.y = v[1]; } /** Multiplicative constructor * Build a vector from another one and a scale factor. * The vector built will be a * u * @param a scale factor * @param u base (unscaled) vector */ public Vector2D(double a, Vector2D u) { this.x = a * u.x; this.y = a * u.y; } /** Linear constructor * Build a vector from two other ones and corresponding scale factors. * The vector built will be a1 * u1 + a2 * u2 * @param a1 first scale factor * @param u1 first base (unscaled) vector * @param a2 second scale factor * @param u2 second base (unscaled) vector */ public Vector2D(double a1, Vector2D u1, double a2, Vector2D u2) { this.x = a1 * u1.x + a2 * u2.x; this.y = a1 * u1.y + a2 * u2.y; } /** Linear constructor * Build a vector from three other ones and corresponding scale factors. * The vector built will be a1 * u1 + a2 * u2 + a3 * u3 * @param a1 first scale factor * @param u1 first base (unscaled) vector * @param a2 second scale factor * @param u2 second base (unscaled) vector * @param a3 third scale factor * @param u3 third base (unscaled) vector */ public Vector2D(double a1, Vector2D u1, double a2, Vector2D u2, double a3, Vector2D u3) { this.x = a1 * u1.x + a2 * u2.x + a3 * u3.x; this.y = a1 * u1.y + a2 * u2.y + a3 * u3.y; } /** Linear constructor * Build a vector from four other ones and corresponding scale factors. * The vector built will be a1 * u1 + a2 * u2 + a3 * u3 + a4 * u4 * @param a1 first scale factor * @param u1 first base (unscaled) vector * @param a2 second scale factor * @param u2 second base (unscaled) vector * @param a3 third scale factor * @param u3 third base (unscaled) vector * @param a4 fourth scale factor * @param u4 fourth base (unscaled) vector */ public Vector2D(double a1, Vector2D u1, double a2, Vector2D u2, double a3, Vector2D u3, double a4, Vector2D u4) { this.x = a1 * u1.x + a2 * u2.x + a3 * u3.x + a4 * u4.x; this.y = a1 * u1.y + a2 * u2.y + a3 * u3.y + a4 * u4.y; } /** Get the abscissa of the vector. * @return abscissa of the vector * @see #Vector2D(double, double) */ public double getX() { return x; } /** Get the ordinate of the vector. * @return ordinate of the vector * @see #Vector2D(double, double) */ public double getY() { return y; } /** Get the vector coordinates as a dimension 2 array. * @return vector coordinates * @see #Vector2D(double[]) */ public double[] toArray() { return new double[] { x, y }; } /** {@inheritDoc} */ public Space getSpace() { return Euclidean2D.getInstance(); } /** {@inheritDoc} */ public Vector2D getZero() { return ZERO; } /** {@inheritDoc} */ public double getNorm1() { return FastMath.abs(x) + FastMath.abs(y); } /** {@inheritDoc} */ public double getNorm() { return FastMath.sqrt (x * x + y * y); } /** {@inheritDoc} */ public double getNormSq() { return x * x + y * y; } /** {@inheritDoc} */ public double getNormInf() { return FastMath.max(FastMath.abs(x), FastMath.abs(y)); } /** {@inheritDoc} */ public Vector2D add(Vector v) { Vector2D v2 = (Vector2D) v; return new Vector2D(x + v2.getX(), y + v2.getY()); } /** {@inheritDoc} */ public Vector2D add(double factor, Vector v) { Vector2D v2 = (Vector2D) v; return new Vector2D(x + factor * v2.getX(), y + factor * v2.getY()); } /** {@inheritDoc} */ public Vector2D subtract(Vector p) { Vector2D p3 = (Vector2D) p; return new Vector2D(x - p3.x, y - p3.y); } /** {@inheritDoc} */ public Vector2D subtract(double factor, Vector v) { Vector2D v2 = (Vector2D) v; return new Vector2D(x - factor * v2.getX(), y - factor * v2.getY()); } /** {@inheritDoc} */ public Vector2D normalize() throws MathArithmeticException { double s = getNorm(); if (s == 0) { throw new MathArithmeticException(LocalizedFormats.CANNOT_NORMALIZE_A_ZERO_NORM_VECTOR); } return scalarMultiply(1 / s); } /** {@inheritDoc} */ public Vector2D negate() { return new Vector2D(-x, -y); } /** {@inheritDoc} */ public Vector2D scalarMultiply(double a) { return new Vector2D(a * x, a * y); } /** {@inheritDoc} */ public boolean isNaN() { return Double.isNaN(x) || Double.isNaN(y); } /** {@inheritDoc} */ public boolean isInfinite() { return !isNaN() && (Double.isInfinite(x) || Double.isInfinite(y)); } /** {@inheritDoc} */ public double distance1(Vector p) { Vector2D p3 = (Vector2D) p; final double dx = FastMath.abs(p3.x - x); final double dy = FastMath.abs(p3.y - y); return dx + dy; } /** {@inheritDoc} */ public double distance(Vector p) { Vector2D p3 = (Vector2D) p; final double dx = p3.x - x; final double dy = p3.y - y; return FastMath.sqrt(dx * dx + dy * dy); } /** {@inheritDoc} */ public double distanceInf(Vector p) { Vector2D p3 = (Vector2D) p; final double dx = FastMath.abs(p3.x - x); final double dy = FastMath.abs(p3.y - y); return FastMath.max(dx, dy); } /** {@inheritDoc} */ public double distanceSq(Vector p) { Vector2D p3 = (Vector2D) p; final double dx = p3.x - x; final double dy = p3.y - y; return dx * dx + dy * dy; } /** {@inheritDoc} */ public double dotProduct(final Vector v) { final Vector2D v2 = (Vector2D) v; return x * v2.x + y * v2.y; } /** Compute the distance between two vectors according to the L2 norm. *

                              Calling this method is equivalent to calling: * p1.subtract(p2).getNorm() except that no intermediate * vector is built

                              * @param p1 first vector * @param p2 second vector * @return the distance between p1 and p2 according to the L2 norm */ public static double distance(Vector2D p1, Vector2D p2) { return p1.distance(p2); } /** Compute the distance between two vectors according to the L norm. *

                              Calling this method is equivalent to calling: * p1.subtract(p2).getNormInf() except that no intermediate * vector is built

                              * @param p1 first vector * @param p2 second vector * @return the distance between p1 and p2 according to the L norm */ public static double distanceInf(Vector2D p1, Vector2D p2) { return p1.distanceInf(p2); } /** Compute the square of the distance between two vectors. *

                              Calling this method is equivalent to calling: * p1.subtract(p2).getNormSq() except that no intermediate * vector is built

                              * @param p1 first vector * @param p2 second vector * @return the square of the distance between p1 and p2 */ public static double distanceSq(Vector2D p1, Vector2D p2) { return p1.distanceSq(p2); } /** * Test for the equality of two 2D vectors. *

                              * If all coordinates of two 2D vectors are exactly the same, and none are * Double.NaN, the two 2D vectors are considered to be equal. *

                              *

                              * NaN coordinates are considered to affect globally the vector * and be equals to each other - i.e, if either (or all) coordinates of the * 2D vector are equal to Double.NaN, the 2D vector is equal to * {@link #NaN}. *

                              * * @param other Object to test for equality to this * @return true if two 2D vector objects are equal, false if * object is null, not an instance of Vector2D, or * not equal to this Vector2D instance * */ @Override public boolean equals(Object other) { if (this == other) { return true; } if (other instanceof Vector2D) { final Vector2D rhs = (Vector2D)other; if (rhs.isNaN()) { return this.isNaN(); } return (x == rhs.x) && (y == rhs.y); } return false; } /** * Get a hashCode for the 2D vector. *

                              * All NaN values have the same hash code.

                              * * @return a hash code value for this object */ @Override public int hashCode() { if (isNaN()) { return 542; } return 122 * (76 * MathUtils.hash(x) + MathUtils.hash(y)); } /** Get a string representation of this vector. * @return a string representation of this vector */ @Override public String toString() { return Vector2DFormat.getInstance().format(this); } /** {@inheritDoc} */ public String toString(final NumberFormat format) { return new Vector2DFormat(format).format(this); } } ././@LongLink100644 0 0 146 12126630646 10262 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/geometry/euclidean/twod/PolygonsSet.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/geometry/euclidean/twod/PolygonsSet.jav100644 1750 1750 104651 12126627714 32324 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.geometry.euclidean.twod; import java.util.ArrayList; import java.util.Collection; import java.util.List; import org.apache.commons.math3.exception.MathInternalError; import org.apache.commons.math3.geometry.euclidean.oned.Euclidean1D; import org.apache.commons.math3.geometry.euclidean.oned.Interval; import org.apache.commons.math3.geometry.euclidean.oned.IntervalsSet; import org.apache.commons.math3.geometry.euclidean.oned.Vector1D; import org.apache.commons.math3.geometry.partitioning.AbstractRegion; import org.apache.commons.math3.geometry.partitioning.AbstractSubHyperplane; import org.apache.commons.math3.geometry.partitioning.BSPTree; import org.apache.commons.math3.geometry.partitioning.BSPTreeVisitor; import org.apache.commons.math3.geometry.partitioning.BoundaryAttribute; import org.apache.commons.math3.geometry.partitioning.Side; import org.apache.commons.math3.geometry.partitioning.SubHyperplane; import org.apache.commons.math3.geometry.partitioning.utilities.AVLTree; import org.apache.commons.math3.geometry.partitioning.utilities.OrderedTuple; import org.apache.commons.math3.util.FastMath; /** This class represents a 2D region: a set of polygons. * @version $Id: PolygonsSet.java 1422195 2012-12-15 06:45:18Z psteitz $ * @since 3.0 */ public class PolygonsSet extends AbstractRegion { /** Vertices organized as boundary loops. */ private Vector2D[][] vertices; /** Build a polygons set representing the whole real line. */ public PolygonsSet() { super(); } /** Build a polygons set from a BSP tree. *

                              The leaf nodes of the BSP tree must have a * {@code Boolean} attribute representing the inside status of * the corresponding cell (true for inside cells, false for outside * cells). In order to avoid building too many small objects, it is * recommended to use the predefined constants * {@code Boolean.TRUE} and {@code Boolean.FALSE}

                              * @param tree inside/outside BSP tree representing the region */ public PolygonsSet(final BSPTree tree) { super(tree); } /** Build a polygons set from a Boundary REPresentation (B-rep). *

                              The boundary is provided as a collection of {@link * SubHyperplane sub-hyperplanes}. Each sub-hyperplane has the * interior part of the region on its minus side and the exterior on * its plus side.

                              *

                              The boundary elements can be in any order, and can form * several non-connected sets (like for example polygons with holes * or a set of disjoint polyhedrons considered as a whole). In * fact, the elements do not even need to be connected together * (their topological connections are not used here). However, if the * boundary does not really separate an inside open from an outside * open (open having here its topological meaning), then subsequent * calls to the {@link * org.apache.commons.math3.geometry.partitioning.Region#checkPoint(org.apache.commons.math3.geometry.Vector) * checkPoint} method will not be meaningful anymore.

                              *

                              If the boundary is empty, the region will represent the whole * space.

                              * @param boundary collection of boundary elements, as a * collection of {@link SubHyperplane SubHyperplane} objects */ public PolygonsSet(final Collection> boundary) { super(boundary); } /** Build a parallellepipedic box. * @param xMin low bound along the x direction * @param xMax high bound along the x direction * @param yMin low bound along the y direction * @param yMax high bound along the y direction */ public PolygonsSet(final double xMin, final double xMax, final double yMin, final double yMax) { super(boxBoundary(xMin, xMax, yMin, yMax)); } /** Build a polygon from a simple list of vertices. *

                              The boundary is provided as a list of points considering to * represent the vertices of a simple loop. The interior part of the * region is on the left side of this path and the exterior is on its * right side.

                              *

                              This constructor does not handle polygons with a boundary * forming several disconnected paths (such as polygons with holes).

                              *

                              For cases where this simple constructor applies, it is expected to * be numerically more robust than the {@link #PolygonsSet(Collection) general * constructor} using {@link SubHyperplane subhyperplanes}.

                              *

                              If the list is empty, the region will represent the whole * space.

                              *

                              * Polygons with thin pikes or dents are inherently difficult to handle because * they involve lines with almost opposite directions at some vertices. Polygons * whose vertices come from some physical measurement with noise are also * difficult because an edge that should be straight may be broken in lots of * different pieces with almost equal directions. In both cases, computing the * lines intersections is not numerically robust due to the almost 0 or almost * π angle. Such cases need to carefully adjust the {@code hyperplaneThickness} * parameter. A too small value would often lead to completely wrong polygons * with large area wrongly identified as inside or outside. Large values are * often much safer. As a rule of thumb, a value slightly below the size of the * most accurate detail needed is a good value for the {@code hyperplaneThickness} * parameter. *

                              * @param hyperplaneThickness tolerance below which points are considered to * belong to the hyperplane (which is therefore more a slab) * @param vertices vertices of the simple loop boundary * @since 3.1 */ public PolygonsSet(final double hyperplaneThickness, final Vector2D ... vertices) { super(verticesToTree(hyperplaneThickness, vertices)); } /** Create a list of hyperplanes representing the boundary of a box. * @param xMin low bound along the x direction * @param xMax high bound along the x direction * @param yMin low bound along the y direction * @param yMax high bound along the y direction * @return boundary of the box */ private static Line[] boxBoundary(final double xMin, final double xMax, final double yMin, final double yMax) { final Vector2D minMin = new Vector2D(xMin, yMin); final Vector2D minMax = new Vector2D(xMin, yMax); final Vector2D maxMin = new Vector2D(xMax, yMin); final Vector2D maxMax = new Vector2D(xMax, yMax); return new Line[] { new Line(minMin, maxMin), new Line(maxMin, maxMax), new Line(maxMax, minMax), new Line(minMax, minMin) }; } /** Build the BSP tree of a polygons set from a simple list of vertices. *

                              The boundary is provided as a list of points considering to * represent the vertices of a simple loop. The interior part of the * region is on the left side of this path and the exterior is on its * right side.

                              *

                              This constructor does not handle polygons with a boundary * forming several disconnected paths (such as polygons with holes).

                              *

                              For cases where this simple constructor applies, it is expected to * be numerically more robust than the {@link #PolygonsSet(Collection) general * constructor} using {@link SubHyperplane subhyperplanes}.

                              * @param hyperplaneThickness tolerance below which points are consider to * belong to the hyperplane (which is therefore more a slab) * @param vertices vertices of the simple loop boundary * @return the BSP tree of the input vertices */ private static BSPTree verticesToTree(final double hyperplaneThickness, final Vector2D ... vertices) { final int n = vertices.length; if (n == 0) { // the tree represents the whole space return new BSPTree(Boolean.TRUE); } // build the vertices final Vertex[] vArray = new Vertex[n]; for (int i = 0; i < n; ++i) { vArray[i] = new Vertex(vertices[i]); } // build the edges List edges = new ArrayList(); for (int i = 0; i < n; ++i) { // get the endpoints of the edge final Vertex start = vArray[i]; final Vertex end = vArray[(i + 1) % n]; // get the line supporting the edge, taking care not to recreate it // if it was already created earlier due to another edge being aligned // with the current one Line line = start.sharedLineWith(end); if (line == null) { line = new Line(start.getLocation(), end.getLocation()); } // create the edge and store it edges.add(new Edge(start, end, line)); // check if another vertex also happens to be on this line for (final Vertex vertex : vArray) { if (vertex != start && vertex != end && FastMath.abs(line.getOffset(vertex.getLocation())) <= hyperplaneThickness) { vertex.bindWith(line); } } } // build the tree top-down final BSPTree tree = new BSPTree(); insertEdges(hyperplaneThickness, tree, edges); return tree; } /** Recursively build a tree by inserting cut sub-hyperplanes. * @param hyperplaneThickness tolerance below which points are consider to * belong to the hyperplane (which is therefore more a slab) * @param node current tree node (it is a leaf node at the beginning * of the call) * @param edges list of edges to insert in the cell defined by this node * (excluding edges not belonging to the cell defined by this node) */ private static void insertEdges(final double hyperplaneThickness, final BSPTree node, final List edges) { // find an edge with an hyperplane that can be inserted in the node int index = 0; Edge inserted =null; while (inserted == null && index < edges.size()) { inserted = edges.get(index++); if (inserted.getNode() == null) { if (node.insertCut(inserted.getLine())) { inserted.setNode(node); } else { inserted = null; } } else { inserted = null; } } if (inserted == null) { // no suitable edge was found, the node remains a leaf node // we need to set its inside/outside boolean indicator final BSPTree parent = node.getParent(); if (parent == null || node == parent.getMinus()) { node.setAttribute(Boolean.TRUE); } else { node.setAttribute(Boolean.FALSE); } return; } // we have split the node by inserted an edge as a cut sub-hyperplane // distribute the remaining edges in the two sub-trees final List plusList = new ArrayList(); final List minusList = new ArrayList(); for (final Edge edge : edges) { if (edge != inserted) { final double startOffset = inserted.getLine().getOffset(edge.getStart().getLocation()); final double endOffset = inserted.getLine().getOffset(edge.getEnd().getLocation()); Side startSide = (FastMath.abs(startOffset) <= hyperplaneThickness) ? Side.HYPER : ((startOffset < 0) ? Side.MINUS : Side.PLUS); Side endSide = (FastMath.abs(endOffset) <= hyperplaneThickness) ? Side.HYPER : ((endOffset < 0) ? Side.MINUS : Side.PLUS); switch (startSide) { case PLUS: if (endSide == Side.MINUS) { // we need to insert a split point on the hyperplane final Vertex splitPoint = edge.split(inserted.getLine()); minusList.add(splitPoint.getOutgoing()); plusList.add(splitPoint.getIncoming()); } else { plusList.add(edge); } break; case MINUS: if (endSide == Side.PLUS) { // we need to insert a split point on the hyperplane final Vertex splitPoint = edge.split(inserted.getLine()); minusList.add(splitPoint.getIncoming()); plusList.add(splitPoint.getOutgoing()); } else { minusList.add(edge); } break; default: if (endSide == Side.PLUS) { plusList.add(edge); } else if (endSide == Side.MINUS) { minusList.add(edge); } break; } } } // recurse through lower levels if (!plusList.isEmpty()) { insertEdges(hyperplaneThickness, node.getPlus(), plusList); } else { node.getPlus().setAttribute(Boolean.FALSE); } if (!minusList.isEmpty()) { insertEdges(hyperplaneThickness, node.getMinus(), minusList); } else { node.getMinus().setAttribute(Boolean.TRUE); } } /** Internal class for holding vertices while they are processed to build a BSP tree. */ private static class Vertex { /** Vertex location. */ private final Vector2D location; /** Incoming edge. */ private Edge incoming; /** Outgoing edge. */ private Edge outgoing; /** Lines bound with this vertex. */ private final List lines; /** Build a non-processed vertex not owned by any node yet. * @param location vertex location */ public Vertex(final Vector2D location) { this.location = location; this.incoming = null; this.outgoing = null; this.lines = new ArrayList(); } /** Get Vertex location. * @return vertex location */ public Vector2D getLocation() { return location; } /** Bind a line considered to contain this vertex. * @param line line to bind with this vertex */ public void bindWith(final Line line) { lines.add(line); } /** Get the common line bound with both the instance and another vertex, if any. *

                              * When two vertices are both bound to the same line, this means they are * already handled by node associated with this line, so there is no need * to create a cut hyperplane for them. *

                              * @param vertex other vertex to check instance against * @return line bound with both the instance and another vertex, or null if the * two vertices do not share a line yet */ public Line sharedLineWith(final Vertex vertex) { for (final Line line1 : lines) { for (final Line line2 : vertex.lines) { if (line1 == line2) { return line1; } } } return null; } /** Set incoming edge. *

                              * The line supporting the incoming edge is automatically bound * with the instance. *

                              * @param incoming incoming edge */ public void setIncoming(final Edge incoming) { this.incoming = incoming; bindWith(incoming.getLine()); } /** Get incoming edge. * @return incoming edge */ public Edge getIncoming() { return incoming; } /** Set outgoing edge. *

                              * The line supporting the outgoing edge is automatically bound * with the instance. *

                              * @param outgoing outgoing edge */ public void setOutgoing(final Edge outgoing) { this.outgoing = outgoing; bindWith(outgoing.getLine()); } /** Get outgoing edge. * @return outgoing edge */ public Edge getOutgoing() { return outgoing; } } /** Internal class for holding edges while they are processed to build a BSP tree. */ private static class Edge { /** Start vertex. */ private final Vertex start; /** End vertex. */ private final Vertex end; /** Line supporting the edge. */ private final Line line; /** Node whose cut hyperplane contains this edge. */ private BSPTree node; /** Build an edge not contained in any node yet. * @param start start vertex * @param end end vertex * @param line line supporting the edge */ public Edge(final Vertex start, final Vertex end, final Line line) { this.start = start; this.end = end; this.line = line; this.node = null; // connect the vertices back to the edge start.setOutgoing(this); end.setIncoming(this); } /** Get start vertex. * @return start vertex */ public Vertex getStart() { return start; } /** Get end vertex. * @return end vertex */ public Vertex getEnd() { return end; } /** Get the line supporting this edge. * @return line supporting this edge */ public Line getLine() { return line; } /** Set the node whose cut hyperplane contains this edge. * @param node node whose cut hyperplane contains this edge */ public void setNode(final BSPTree node) { this.node = node; } /** Get the node whose cut hyperplane contains this edge. * @return node whose cut hyperplane contains this edge * (null if edge has not yet been inserted into the BSP tree) */ public BSPTree getNode() { return node; } /** Split the edge. *

                              * Once split, this edge is not referenced anymore by the vertices, * it is replaced by the two half-edges and an intermediate splitting * vertex is introduced to connect these two halves. *

                              * @param splitLine line splitting the edge in two halves * @return split vertex (its incoming and outgoing edges are the two halves) */ public Vertex split(final Line splitLine) { final Vertex splitVertex = new Vertex(line.intersection(splitLine)); splitVertex.bindWith(splitLine); final Edge startHalf = new Edge(start, splitVertex, line); final Edge endHalf = new Edge(splitVertex, end, line); startHalf.node = node; endHalf.node = node; return splitVertex; } } /** {@inheritDoc} */ @Override public PolygonsSet buildNew(final BSPTree tree) { return new PolygonsSet(tree); } /** {@inheritDoc} */ @Override protected void computeGeometricalProperties() { final Vector2D[][] v = getVertices(); if (v.length == 0) { final BSPTree tree = getTree(false); if (tree.getCut() == null && (Boolean) tree.getAttribute()) { // the instance covers the whole space setSize(Double.POSITIVE_INFINITY); setBarycenter(Vector2D.NaN); } else { setSize(0); setBarycenter(new Vector2D(0, 0)); } } else if (v[0][0] == null) { // there is at least one open-loop: the polygon is infinite setSize(Double.POSITIVE_INFINITY); setBarycenter(Vector2D.NaN); } else { // all loops are closed, we compute some integrals around the shape double sum = 0; double sumX = 0; double sumY = 0; for (Vector2D[] loop : v) { double x1 = loop[loop.length - 1].getX(); double y1 = loop[loop.length - 1].getY(); for (final Vector2D point : loop) { final double x0 = x1; final double y0 = y1; x1 = point.getX(); y1 = point.getY(); final double factor = x0 * y1 - y0 * x1; sum += factor; sumX += factor * (x0 + x1); sumY += factor * (y0 + y1); } } if (sum < 0) { // the polygon as a finite outside surrounded by an infinite inside setSize(Double.POSITIVE_INFINITY); setBarycenter(Vector2D.NaN); } else { setSize(sum / 2); setBarycenter(new Vector2D(sumX / (3 * sum), sumY / (3 * sum))); } } } /** Get the vertices of the polygon. *

                              The polygon boundary can be represented as an array of loops, * each loop being itself an array of vertices.

                              *

                              In order to identify open loops which start and end by * infinite edges, the open loops arrays start with a null point. In * this case, the first non null point and the last point of the * array do not represent real vertices, they are dummy points * intended only to get the direction of the first and last edge. An * open loop consisting of a single infinite line will therefore be * represented by a three elements array with one null point * followed by two dummy points. The open loops are always the first * ones in the loops array.

                              *

                              If the polygon has no boundary at all, a zero length loop * array will be returned.

                              *

                              All line segments in the various loops have the inside of the * region on their left side and the outside on their right side * when moving in the underlying line direction. This means that * closed loops surrounding finite areas obey the direct * trigonometric orientation.

                              * @return vertices of the polygon, organized as oriented boundary * loops with the open loops first (the returned value is guaranteed * to be non-null) */ public Vector2D[][] getVertices() { if (vertices == null) { if (getTree(false).getCut() == null) { vertices = new Vector2D[0][]; } else { // sort the segments according to their start point final SegmentsBuilder visitor = new SegmentsBuilder(); getTree(true).visit(visitor); final AVLTree sorted = visitor.getSorted(); // identify the loops, starting from the open ones // (their start segments are naturally at the sorted set beginning) final ArrayList> loops = new ArrayList>(); while (!sorted.isEmpty()) { final AVLTree.Node node = sorted.getSmallest(); final List loop = followLoop(node, sorted); if (loop != null) { loops.add(loop); } } // tranform the loops in an array of arrays of points vertices = new Vector2D[loops.size()][]; int i = 0; for (final List loop : loops) { if (loop.size() < 2) { // single infinite line final Line line = loop.get(0).getLine(); vertices[i++] = new Vector2D[] { null, line.toSpace(new Vector1D(-Float.MAX_VALUE)), line.toSpace(new Vector1D(+Float.MAX_VALUE)) }; } else if (loop.get(0).getStart() == null) { // open loop with at least one real point final Vector2D[] array = new Vector2D[loop.size() + 2]; int j = 0; for (Segment segment : loop) { if (j == 0) { // null point and first dummy point double x = segment.getLine().toSubSpace(segment.getEnd()).getX(); x -= FastMath.max(1.0, FastMath.abs(x / 2)); array[j++] = null; array[j++] = segment.getLine().toSpace(new Vector1D(x)); } if (j < (array.length - 1)) { // current point array[j++] = segment.getEnd(); } if (j == (array.length - 1)) { // last dummy point double x = segment.getLine().toSubSpace(segment.getStart()).getX(); x += FastMath.max(1.0, FastMath.abs(x / 2)); array[j++] = segment.getLine().toSpace(new Vector1D(x)); } } vertices[i++] = array; } else { final Vector2D[] array = new Vector2D[loop.size()]; int j = 0; for (Segment segment : loop) { array[j++] = segment.getStart(); } vertices[i++] = array; } } } } return vertices.clone(); } /** Follow a boundary loop. * @param node node containing the segment starting the loop * @param sorted set of segments belonging to the boundary, sorted by * start points (contains {@code node}) * @return a list of connected sub-hyperplanes starting at * {@code node} */ private List followLoop(final AVLTree.Node node, final AVLTree sorted) { final ArrayList loop = new ArrayList(); ComparableSegment segment = node.getElement(); loop.add(segment); final Vector2D globalStart = segment.getStart(); Vector2D end = segment.getEnd(); node.delete(); // is this an open or a closed loop ? final boolean open = segment.getStart() == null; while ((end != null) && (open || (globalStart.distance(end) > 1.0e-10))) { // search the sub-hyperplane starting where the previous one ended AVLTree.Node selectedNode = null; ComparableSegment selectedSegment = null; double selectedDistance = Double.POSITIVE_INFINITY; final ComparableSegment lowerLeft = new ComparableSegment(end, -1.0e-10, -1.0e-10); final ComparableSegment upperRight = new ComparableSegment(end, +1.0e-10, +1.0e-10); for (AVLTree.Node n = sorted.getNotSmaller(lowerLeft); (n != null) && (n.getElement().compareTo(upperRight) <= 0); n = n.getNext()) { segment = n.getElement(); final double distance = end.distance(segment.getStart()); if (distance < selectedDistance) { selectedNode = n; selectedSegment = segment; selectedDistance = distance; } } if (selectedDistance > 1.0e-10) { // this is a degenerated loop, it probably comes from a very // tiny region with some segments smaller than the threshold, we // simply ignore it return null; } end = selectedSegment.getEnd(); loop.add(selectedSegment); selectedNode.delete(); } if ((loop.size() == 2) && !open) { // this is a degenerated infinitely thin loop, we simply ignore it return null; } if ((end == null) && !open) { throw new MathInternalError(); } return loop; } /** Private extension of Segment allowing comparison. */ private static class ComparableSegment extends Segment implements Comparable { /** Sorting key. */ private OrderedTuple sortingKey; /** Build a segment. * @param start start point of the segment * @param end end point of the segment * @param line line containing the segment */ public ComparableSegment(final Vector2D start, final Vector2D end, final Line line) { super(start, end, line); sortingKey = (start == null) ? new OrderedTuple(Double.NEGATIVE_INFINITY, Double.NEGATIVE_INFINITY) : new OrderedTuple(start.getX(), start.getY()); } /** Build a dummy segment. *

                              * The object built is not a real segment, only the sorting key is used to * allow searching in the neighborhood of a point. This is an horrible hack ... *

                              * @param start start point of the segment * @param dx abscissa offset from the start point * @param dy ordinate offset from the start point */ public ComparableSegment(final Vector2D start, final double dx, final double dy) { super(null, null, null); sortingKey = new OrderedTuple(start.getX() + dx, start.getY() + dy); } /** {@inheritDoc} */ public int compareTo(final ComparableSegment o) { return sortingKey.compareTo(o.sortingKey); } /** {@inheritDoc} */ @Override public boolean equals(final Object other) { if (this == other) { return true; } else if (other instanceof ComparableSegment) { return compareTo((ComparableSegment) other) == 0; } else { return false; } } /** {@inheritDoc} */ @Override public int hashCode() { return getStart().hashCode() ^ getEnd().hashCode() ^ getLine().hashCode() ^ sortingKey.hashCode(); } } /** Visitor building segments. */ private static class SegmentsBuilder implements BSPTreeVisitor { /** Sorted segments. */ private AVLTree sorted; /** Simple constructor. */ public SegmentsBuilder() { sorted = new AVLTree(); } /** {@inheritDoc} */ public Order visitOrder(final BSPTree node) { return Order.MINUS_SUB_PLUS; } /** {@inheritDoc} */ public void visitInternalNode(final BSPTree node) { @SuppressWarnings("unchecked") final BoundaryAttribute attribute = (BoundaryAttribute) node.getAttribute(); if (attribute.getPlusOutside() != null) { addContribution(attribute.getPlusOutside(), false); } if (attribute.getPlusInside() != null) { addContribution(attribute.getPlusInside(), true); } } /** {@inheritDoc} */ public void visitLeafNode(final BSPTree node) { } /** Add he contribution of a boundary facet. * @param sub boundary facet * @param reversed if true, the facet has the inside on its plus side */ private void addContribution(final SubHyperplane sub, final boolean reversed) { @SuppressWarnings("unchecked") final AbstractSubHyperplane absSub = (AbstractSubHyperplane) sub; final Line line = (Line) sub.getHyperplane(); final List intervals = ((IntervalsSet) absSub.getRemainingRegion()).asList(); for (final Interval i : intervals) { final Vector2D start = Double.isInfinite(i.getInf()) ? null : (Vector2D) line.toSpace(new Vector1D(i.getInf())); final Vector2D end = Double.isInfinite(i.getSup()) ? null : (Vector2D) line.toSpace(new Vector1D(i.getSup())); if (reversed) { sorted.insert(new ComparableSegment(end, start, line.getReverse())); } else { sorted.insert(new ComparableSegment(start, end, line)); } } } /** Get the sorted segments. * @return sorted segments */ public AVLTree getSorted() { return sorted; } } } ././@LongLink100644 0 0 151 12126630646 10256 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/geometry/euclidean/twod/Vector2DFormat.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/geometry/euclidean/twod/Vector2DFormat.100644 1750 1750 12566 12126627714 32121 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.geometry.euclidean.twod; import java.text.FieldPosition; import java.text.NumberFormat; import java.text.ParsePosition; import java.util.Locale; import org.apache.commons.math3.exception.MathParseException; import org.apache.commons.math3.geometry.Vector; import org.apache.commons.math3.geometry.VectorFormat; import org.apache.commons.math3.util.CompositeFormat; /** * Formats a 2D vector in components list format "{x; y}". *

                              The prefix and suffix "{" and "}" and the separator "; " can be replaced by * any user-defined strings. The number format for components can be configured.

                              *

                              White space is ignored at parse time, even if it is in the prefix, suffix * or separator specifications. So even if the default separator does include a space * character that is used at format time, both input string "{1;1}" and * " { 1 ; 1 } " will be parsed without error and the same vector will be * returned. In the second case, however, the parse position after parsing will be * just after the closing curly brace, i.e. just before the trailing space.

                              * * @version $Id: Vector2DFormat.java 1416643 2012-12-03 19:37:14Z tn $ * @since 3.0 */ public class Vector2DFormat extends VectorFormat { /** * Create an instance with default settings. *

                              The instance uses the default prefix, suffix and separator: * "{", "}", and "; " and the default number format for components.

                              */ public Vector2DFormat() { super(DEFAULT_PREFIX, DEFAULT_SUFFIX, DEFAULT_SEPARATOR, CompositeFormat.getDefaultNumberFormat()); } /** * Create an instance with a custom number format for components. * @param format the custom format for components. */ public Vector2DFormat(final NumberFormat format) { super(DEFAULT_PREFIX, DEFAULT_SUFFIX, DEFAULT_SEPARATOR, format); } /** * Create an instance with custom prefix, suffix and separator. * @param prefix prefix to use instead of the default "{" * @param suffix suffix to use instead of the default "}" * @param separator separator to use instead of the default "; " */ public Vector2DFormat(final String prefix, final String suffix, final String separator) { super(prefix, suffix, separator, CompositeFormat.getDefaultNumberFormat()); } /** * Create an instance with custom prefix, suffix, separator and format * for components. * @param prefix prefix to use instead of the default "{" * @param suffix suffix to use instead of the default "}" * @param separator separator to use instead of the default "; " * @param format the custom format for components. */ public Vector2DFormat(final String prefix, final String suffix, final String separator, final NumberFormat format) { super(prefix, suffix, separator, format); } /** * Returns the default 2D vector format for the current locale. * @return the default 2D vector format. */ public static Vector2DFormat getInstance() { return getInstance(Locale.getDefault()); } /** * Returns the default 2D vector format for the given locale. * @param locale the specific locale used by the format. * @return the 2D vector format specific to the given locale. */ public static Vector2DFormat getInstance(final Locale locale) { return new Vector2DFormat(CompositeFormat.getDefaultNumberFormat(locale)); } /** {@inheritDoc} */ @Override public StringBuffer format(final Vector vector, final StringBuffer toAppendTo, final FieldPosition pos) { final Vector2D p2 = (Vector2D) vector; return format(toAppendTo, pos, p2.getX(), p2.getY()); } /** {@inheritDoc} */ @Override public Vector2D parse(final String source) throws MathParseException { ParsePosition parsePosition = new ParsePosition(0); Vector2D result = parse(source, parsePosition); if (parsePosition.getIndex() == 0) { throw new MathParseException(source, parsePosition.getErrorIndex(), Vector2D.class); } return result; } /** {@inheritDoc} */ @Override public Vector2D parse(final String source, final ParsePosition pos) { final double[] coordinates = parseCoordinates(2, source, pos); if (coordinates == null) { return null; } return new Vector2D(coordinates[0], coordinates[1]); } } ././@LongLink100644 0 0 151 12126630646 10256 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/geometry/euclidean/threed/package-info.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/geometry/euclidean/threed/package-info.100644 1750 1750 1663 12126627714 32116 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ /** * *

                              * This package provides basic 3D geometry components. *

                              * */ package org.apache.commons.math3.geometry.euclidean.threed; ././@LongLink100644 0 0 152 12126630646 10257 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/geometry/euclidean/threed/RotationOrder.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/geometry/euclidean/threed/RotationOrder100644 1750 1750 13434 12126627714 32326 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.geometry.euclidean.threed; /** * This class is a utility representing a rotation order specification * for Cardan or Euler angles specification. * * This class cannot be instanciated by the user. He can only use one * of the twelve predefined supported orders as an argument to either * the {@link Rotation#Rotation(RotationOrder,double,double,double)} * constructor or the {@link Rotation#getAngles} method. * * @version $Id: RotationOrder.java 1416643 2012-12-03 19:37:14Z tn $ * @since 1.2 */ public final class RotationOrder { /** Set of Cardan angles. * this ordered set of rotations is around X, then around Y, then * around Z */ public static final RotationOrder XYZ = new RotationOrder("XYZ", Vector3D.PLUS_I, Vector3D.PLUS_J, Vector3D.PLUS_K); /** Set of Cardan angles. * this ordered set of rotations is around X, then around Z, then * around Y */ public static final RotationOrder XZY = new RotationOrder("XZY", Vector3D.PLUS_I, Vector3D.PLUS_K, Vector3D.PLUS_J); /** Set of Cardan angles. * this ordered set of rotations is around Y, then around X, then * around Z */ public static final RotationOrder YXZ = new RotationOrder("YXZ", Vector3D.PLUS_J, Vector3D.PLUS_I, Vector3D.PLUS_K); /** Set of Cardan angles. * this ordered set of rotations is around Y, then around Z, then * around X */ public static final RotationOrder YZX = new RotationOrder("YZX", Vector3D.PLUS_J, Vector3D.PLUS_K, Vector3D.PLUS_I); /** Set of Cardan angles. * this ordered set of rotations is around Z, then around X, then * around Y */ public static final RotationOrder ZXY = new RotationOrder("ZXY", Vector3D.PLUS_K, Vector3D.PLUS_I, Vector3D.PLUS_J); /** Set of Cardan angles. * this ordered set of rotations is around Z, then around Y, then * around X */ public static final RotationOrder ZYX = new RotationOrder("ZYX", Vector3D.PLUS_K, Vector3D.PLUS_J, Vector3D.PLUS_I); /** Set of Euler angles. * this ordered set of rotations is around X, then around Y, then * around X */ public static final RotationOrder XYX = new RotationOrder("XYX", Vector3D.PLUS_I, Vector3D.PLUS_J, Vector3D.PLUS_I); /** Set of Euler angles. * this ordered set of rotations is around X, then around Z, then * around X */ public static final RotationOrder XZX = new RotationOrder("XZX", Vector3D.PLUS_I, Vector3D.PLUS_K, Vector3D.PLUS_I); /** Set of Euler angles. * this ordered set of rotations is around Y, then around X, then * around Y */ public static final RotationOrder YXY = new RotationOrder("YXY", Vector3D.PLUS_J, Vector3D.PLUS_I, Vector3D.PLUS_J); /** Set of Euler angles. * this ordered set of rotations is around Y, then around Z, then * around Y */ public static final RotationOrder YZY = new RotationOrder("YZY", Vector3D.PLUS_J, Vector3D.PLUS_K, Vector3D.PLUS_J); /** Set of Euler angles. * this ordered set of rotations is around Z, then around X, then * around Z */ public static final RotationOrder ZXZ = new RotationOrder("ZXZ", Vector3D.PLUS_K, Vector3D.PLUS_I, Vector3D.PLUS_K); /** Set of Euler angles. * this ordered set of rotations is around Z, then around Y, then * around Z */ public static final RotationOrder ZYZ = new RotationOrder("ZYZ", Vector3D.PLUS_K, Vector3D.PLUS_J, Vector3D.PLUS_K); /** Name of the rotations order. */ private final String name; /** Axis of the first rotation. */ private final Vector3D a1; /** Axis of the second rotation. */ private final Vector3D a2; /** Axis of the third rotation. */ private final Vector3D a3; /** Private constructor. * This is a utility class that cannot be instantiated by the user, * so its only constructor is private. * @param name name of the rotation order * @param a1 axis of the first rotation * @param a2 axis of the second rotation * @param a3 axis of the third rotation */ private RotationOrder(final String name, final Vector3D a1, final Vector3D a2, final Vector3D a3) { this.name = name; this.a1 = a1; this.a2 = a2; this.a3 = a3; } /** Get a string representation of the instance. * @return a string representation of the instance (in fact, its name) */ @Override public String toString() { return name; } /** Get the axis of the first rotation. * @return axis of the first rotation */ public Vector3D getA1() { return a1; } /** Get the axis of the second rotation. * @return axis of the second rotation */ public Vector3D getA2() { return a2; } /** Get the axis of the second rotation. * @return axis of the second rotation */ public Vector3D getA3() { return a3; } } ././@LongLink100644 0 0 145 12126630646 10261 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/geometry/euclidean/threed/Vector3D.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/geometry/euclidean/threed/Vector3D.java100644 1750 1750 50406 12126627714 32104 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.geometry.euclidean.threed; import java.io.Serializable; import java.text.NumberFormat; import org.apache.commons.math3.exception.DimensionMismatchException; import org.apache.commons.math3.exception.MathArithmeticException; import org.apache.commons.math3.exception.util.LocalizedFormats; import org.apache.commons.math3.geometry.Vector; import org.apache.commons.math3.geometry.Space; import org.apache.commons.math3.util.FastMath; import org.apache.commons.math3.util.MathUtils; import org.apache.commons.math3.util.MathArrays; /** * This class implements vectors in a three-dimensional space. *

                              Instance of this class are guaranteed to be immutable.

                              * @version $Id: Vector3D.java 1447259 2013-02-18 13:56:39Z luc $ * @since 1.2 */ public class Vector3D implements Serializable, Vector { /** Null vector (coordinates: 0, 0, 0). */ public static final Vector3D ZERO = new Vector3D(0, 0, 0); /** First canonical vector (coordinates: 1, 0, 0). */ public static final Vector3D PLUS_I = new Vector3D(1, 0, 0); /** Opposite of the first canonical vector (coordinates: -1, 0, 0). */ public static final Vector3D MINUS_I = new Vector3D(-1, 0, 0); /** Second canonical vector (coordinates: 0, 1, 0). */ public static final Vector3D PLUS_J = new Vector3D(0, 1, 0); /** Opposite of the second canonical vector (coordinates: 0, -1, 0). */ public static final Vector3D MINUS_J = new Vector3D(0, -1, 0); /** Third canonical vector (coordinates: 0, 0, 1). */ public static final Vector3D PLUS_K = new Vector3D(0, 0, 1); /** Opposite of the third canonical vector (coordinates: 0, 0, -1). */ public static final Vector3D MINUS_K = new Vector3D(0, 0, -1); // CHECKSTYLE: stop ConstantName /** A vector with all coordinates set to NaN. */ public static final Vector3D NaN = new Vector3D(Double.NaN, Double.NaN, Double.NaN); // CHECKSTYLE: resume ConstantName /** A vector with all coordinates set to positive infinity. */ public static final Vector3D POSITIVE_INFINITY = new Vector3D(Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY); /** A vector with all coordinates set to negative infinity. */ public static final Vector3D NEGATIVE_INFINITY = new Vector3D(Double.NEGATIVE_INFINITY, Double.NEGATIVE_INFINITY, Double.NEGATIVE_INFINITY); /** Serializable version identifier. */ private static final long serialVersionUID = 1313493323784566947L; /** Abscissa. */ private final double x; /** Ordinate. */ private final double y; /** Height. */ private final double z; /** Simple constructor. * Build a vector from its coordinates * @param x abscissa * @param y ordinate * @param z height * @see #getX() * @see #getY() * @see #getZ() */ public Vector3D(double x, double y, double z) { this.x = x; this.y = y; this.z = z; } /** Simple constructor. * Build a vector from its coordinates * @param v coordinates array * @exception DimensionMismatchException if array does not have 3 elements * @see #toArray() */ public Vector3D(double[] v) throws DimensionMismatchException { if (v.length != 3) { throw new DimensionMismatchException(v.length, 3); } this.x = v[0]; this.y = v[1]; this.z = v[2]; } /** Simple constructor. * Build a vector from its azimuthal coordinates * @param alpha azimuth (α) around Z * (0 is +X, π/2 is +Y, π is -X and 3π/2 is -Y) * @param delta elevation (δ) above (XY) plane, from -π/2 to +π/2 * @see #getAlpha() * @see #getDelta() */ public Vector3D(double alpha, double delta) { double cosDelta = FastMath.cos(delta); this.x = FastMath.cos(alpha) * cosDelta; this.y = FastMath.sin(alpha) * cosDelta; this.z = FastMath.sin(delta); } /** Multiplicative constructor * Build a vector from another one and a scale factor. * The vector built will be a * u * @param a scale factor * @param u base (unscaled) vector */ public Vector3D(double a, Vector3D u) { this.x = a * u.x; this.y = a * u.y; this.z = a * u.z; } /** Linear constructor * Build a vector from two other ones and corresponding scale factors. * The vector built will be a1 * u1 + a2 * u2 * @param a1 first scale factor * @param u1 first base (unscaled) vector * @param a2 second scale factor * @param u2 second base (unscaled) vector */ public Vector3D(double a1, Vector3D u1, double a2, Vector3D u2) { this.x = MathArrays.linearCombination(a1, u1.x, a2, u2.x); this.y = MathArrays.linearCombination(a1, u1.y, a2, u2.y); this.z = MathArrays.linearCombination(a1, u1.z, a2, u2.z); } /** Linear constructor * Build a vector from three other ones and corresponding scale factors. * The vector built will be a1 * u1 + a2 * u2 + a3 * u3 * @param a1 first scale factor * @param u1 first base (unscaled) vector * @param a2 second scale factor * @param u2 second base (unscaled) vector * @param a3 third scale factor * @param u3 third base (unscaled) vector */ public Vector3D(double a1, Vector3D u1, double a2, Vector3D u2, double a3, Vector3D u3) { this.x = MathArrays.linearCombination(a1, u1.x, a2, u2.x, a3, u3.x); this.y = MathArrays.linearCombination(a1, u1.y, a2, u2.y, a3, u3.y); this.z = MathArrays.linearCombination(a1, u1.z, a2, u2.z, a3, u3.z); } /** Linear constructor * Build a vector from four other ones and corresponding scale factors. * The vector built will be a1 * u1 + a2 * u2 + a3 * u3 + a4 * u4 * @param a1 first scale factor * @param u1 first base (unscaled) vector * @param a2 second scale factor * @param u2 second base (unscaled) vector * @param a3 third scale factor * @param u3 third base (unscaled) vector * @param a4 fourth scale factor * @param u4 fourth base (unscaled) vector */ public Vector3D(double a1, Vector3D u1, double a2, Vector3D u2, double a3, Vector3D u3, double a4, Vector3D u4) { this.x = MathArrays.linearCombination(a1, u1.x, a2, u2.x, a3, u3.x, a4, u4.x); this.y = MathArrays.linearCombination(a1, u1.y, a2, u2.y, a3, u3.y, a4, u4.y); this.z = MathArrays.linearCombination(a1, u1.z, a2, u2.z, a3, u3.z, a4, u4.z); } /** Get the abscissa of the vector. * @return abscissa of the vector * @see #Vector3D(double, double, double) */ public double getX() { return x; } /** Get the ordinate of the vector. * @return ordinate of the vector * @see #Vector3D(double, double, double) */ public double getY() { return y; } /** Get the height of the vector. * @return height of the vector * @see #Vector3D(double, double, double) */ public double getZ() { return z; } /** Get the vector coordinates as a dimension 3 array. * @return vector coordinates * @see #Vector3D(double[]) */ public double[] toArray() { return new double[] { x, y, z }; } /** {@inheritDoc} */ public Space getSpace() { return Euclidean3D.getInstance(); } /** {@inheritDoc} */ public Vector3D getZero() { return ZERO; } /** {@inheritDoc} */ public double getNorm1() { return FastMath.abs(x) + FastMath.abs(y) + FastMath.abs(z); } /** {@inheritDoc} */ public double getNorm() { // there are no cancellation problems here, so we use the straightforward formula return FastMath.sqrt (x * x + y * y + z * z); } /** {@inheritDoc} */ public double getNormSq() { // there are no cancellation problems here, so we use the straightforward formula return x * x + y * y + z * z; } /** {@inheritDoc} */ public double getNormInf() { return FastMath.max(FastMath.max(FastMath.abs(x), FastMath.abs(y)), FastMath.abs(z)); } /** Get the azimuth of the vector. * @return azimuth (α) of the vector, between -π and +π * @see #Vector3D(double, double) */ public double getAlpha() { return FastMath.atan2(y, x); } /** Get the elevation of the vector. * @return elevation (δ) of the vector, between -π/2 and +π/2 * @see #Vector3D(double, double) */ public double getDelta() { return FastMath.asin(z / getNorm()); } /** {@inheritDoc} */ public Vector3D add(final Vector v) { final Vector3D v3 = (Vector3D) v; return new Vector3D(x + v3.x, y + v3.y, z + v3.z); } /** {@inheritDoc} */ public Vector3D add(double factor, final Vector v) { return new Vector3D(1, this, factor, (Vector3D) v); } /** {@inheritDoc} */ public Vector3D subtract(final Vector v) { final Vector3D v3 = (Vector3D) v; return new Vector3D(x - v3.x, y - v3.y, z - v3.z); } /** {@inheritDoc} */ public Vector3D subtract(final double factor, final Vector v) { return new Vector3D(1, this, -factor, (Vector3D) v); } /** {@inheritDoc} */ public Vector3D normalize() throws MathArithmeticException { double s = getNorm(); if (s == 0) { throw new MathArithmeticException(LocalizedFormats.CANNOT_NORMALIZE_A_ZERO_NORM_VECTOR); } return scalarMultiply(1 / s); } /** Get a vector orthogonal to the instance. *

                              There are an infinite number of normalized vectors orthogonal * to the instance. This method picks up one of them almost * arbitrarily. It is useful when one needs to compute a reference * frame with one of the axes in a predefined direction. The * following example shows how to build a frame having the k axis * aligned with the known vector u : *

                              
                                   *   Vector3D k = u.normalize();
                                   *   Vector3D i = k.orthogonal();
                                   *   Vector3D j = Vector3D.crossProduct(k, i);
                                   * 

                              * @return a new normalized vector orthogonal to the instance * @exception MathArithmeticException if the norm of the instance is null */ public Vector3D orthogonal() throws MathArithmeticException { double threshold = 0.6 * getNorm(); if (threshold == 0) { throw new MathArithmeticException(LocalizedFormats.ZERO_NORM); } if (FastMath.abs(x) <= threshold) { double inverse = 1 / FastMath.sqrt(y * y + z * z); return new Vector3D(0, inverse * z, -inverse * y); } else if (FastMath.abs(y) <= threshold) { double inverse = 1 / FastMath.sqrt(x * x + z * z); return new Vector3D(-inverse * z, 0, inverse * x); } double inverse = 1 / FastMath.sqrt(x * x + y * y); return new Vector3D(inverse * y, -inverse * x, 0); } /** Compute the angular separation between two vectors. *

                              This method computes the angular separation between two * vectors using the dot product for well separated vectors and the * cross product for almost aligned vectors. This allows to have a * good accuracy in all cases, even for vectors very close to each * other.

                              * @param v1 first vector * @param v2 second vector * @return angular separation between v1 and v2 * @exception MathArithmeticException if either vector has a null norm */ public static double angle(Vector3D v1, Vector3D v2) throws MathArithmeticException { double normProduct = v1.getNorm() * v2.getNorm(); if (normProduct == 0) { throw new MathArithmeticException(LocalizedFormats.ZERO_NORM); } double dot = v1.dotProduct(v2); double threshold = normProduct * 0.9999; if ((dot < -threshold) || (dot > threshold)) { // the vectors are almost aligned, compute using the sine Vector3D v3 = crossProduct(v1, v2); if (dot >= 0) { return FastMath.asin(v3.getNorm() / normProduct); } return FastMath.PI - FastMath.asin(v3.getNorm() / normProduct); } // the vectors are sufficiently separated to use the cosine return FastMath.acos(dot / normProduct); } /** {@inheritDoc} */ public Vector3D negate() { return new Vector3D(-x, -y, -z); } /** {@inheritDoc} */ public Vector3D scalarMultiply(double a) { return new Vector3D(a * x, a * y, a * z); } /** {@inheritDoc} */ public boolean isNaN() { return Double.isNaN(x) || Double.isNaN(y) || Double.isNaN(z); } /** {@inheritDoc} */ public boolean isInfinite() { return !isNaN() && (Double.isInfinite(x) || Double.isInfinite(y) || Double.isInfinite(z)); } /** * Test for the equality of two 3D vectors. *

                              * If all coordinates of two 3D vectors are exactly the same, and none are * Double.NaN, the two 3D vectors are considered to be equal. *

                              *

                              * NaN coordinates are considered to affect globally the vector * and be equals to each other - i.e, if either (or all) coordinates of the * 3D vector are equal to Double.NaN, the 3D vector is equal to * {@link #NaN}. *

                              * * @param other Object to test for equality to this * @return true if two 3D vector objects are equal, false if * object is null, not an instance of Vector3D, or * not equal to this Vector3D instance * */ @Override public boolean equals(Object other) { if (this == other) { return true; } if (other instanceof Vector3D) { final Vector3D rhs = (Vector3D)other; if (rhs.isNaN()) { return this.isNaN(); } return (x == rhs.x) && (y == rhs.y) && (z == rhs.z); } return false; } /** * Get a hashCode for the 3D vector. *

                              * All NaN values have the same hash code.

                              * * @return a hash code value for this object */ @Override public int hashCode() { if (isNaN()) { return 642; } return 643 * (164 * MathUtils.hash(x) + 3 * MathUtils.hash(y) + MathUtils.hash(z)); } /** {@inheritDoc} *

                              * The implementation uses specific multiplication and addition * algorithms to preserve accuracy and reduce cancellation effects. * It should be very accurate even for nearly orthogonal vectors. *

                              * @see MathArrays#linearCombination(double, double, double, double, double, double) */ public double dotProduct(final Vector v) { final Vector3D v3 = (Vector3D) v; return MathArrays.linearCombination(x, v3.x, y, v3.y, z, v3.z); } /** Compute the cross-product of the instance with another vector. * @param v other vector * @return the cross product this ^ v as a new Vector3D */ public Vector3D crossProduct(final Vector v) { final Vector3D v3 = (Vector3D) v; return new Vector3D(MathArrays.linearCombination(y, v3.z, -z, v3.y), MathArrays.linearCombination(z, v3.x, -x, v3.z), MathArrays.linearCombination(x, v3.y, -y, v3.x)); } /** {@inheritDoc} */ public double distance1(Vector v) { final Vector3D v3 = (Vector3D) v; final double dx = FastMath.abs(v3.x - x); final double dy = FastMath.abs(v3.y - y); final double dz = FastMath.abs(v3.z - z); return dx + dy + dz; } /** {@inheritDoc} */ public double distance(Vector v) { final Vector3D v3 = (Vector3D) v; final double dx = v3.x - x; final double dy = v3.y - y; final double dz = v3.z - z; return FastMath.sqrt(dx * dx + dy * dy + dz * dz); } /** {@inheritDoc} */ public double distanceInf(Vector v) { final Vector3D v3 = (Vector3D) v; final double dx = FastMath.abs(v3.x - x); final double dy = FastMath.abs(v3.y - y); final double dz = FastMath.abs(v3.z - z); return FastMath.max(FastMath.max(dx, dy), dz); } /** {@inheritDoc} */ public double distanceSq(Vector v) { final Vector3D v3 = (Vector3D) v; final double dx = v3.x - x; final double dy = v3.y - y; final double dz = v3.z - z; return dx * dx + dy * dy + dz * dz; } /** Compute the dot-product of two vectors. * @param v1 first vector * @param v2 second vector * @return the dot product v1.v2 */ public static double dotProduct(Vector3D v1, Vector3D v2) { return v1.dotProduct(v2); } /** Compute the cross-product of two vectors. * @param v1 first vector * @param v2 second vector * @return the cross product v1 ^ v2 as a new Vector */ public static Vector3D crossProduct(final Vector3D v1, final Vector3D v2) { return v1.crossProduct(v2); } /** Compute the distance between two vectors according to the L1 norm. *

                              Calling this method is equivalent to calling: * v1.subtract(v2).getNorm1() except that no intermediate * vector is built

                              * @param v1 first vector * @param v2 second vector * @return the distance between v1 and v2 according to the L1 norm */ public static double distance1(Vector3D v1, Vector3D v2) { return v1.distance1(v2); } /** Compute the distance between two vectors according to the L2 norm. *

                              Calling this method is equivalent to calling: * v1.subtract(v2).getNorm() except that no intermediate * vector is built

                              * @param v1 first vector * @param v2 second vector * @return the distance between v1 and v2 according to the L2 norm */ public static double distance(Vector3D v1, Vector3D v2) { return v1.distance(v2); } /** Compute the distance between two vectors according to the L norm. *

                              Calling this method is equivalent to calling: * v1.subtract(v2).getNormInf() except that no intermediate * vector is built

                              * @param v1 first vector * @param v2 second vector * @return the distance between v1 and v2 according to the L norm */ public static double distanceInf(Vector3D v1, Vector3D v2) { return v1.distanceInf(v2); } /** Compute the square of the distance between two vectors. *

                              Calling this method is equivalent to calling: * v1.subtract(v2).getNormSq() except that no intermediate * vector is built

                              * @param v1 first vector * @param v2 second vector * @return the square of the distance between v1 and v2 */ public static double distanceSq(Vector3D v1, Vector3D v2) { return v1.distanceSq(v2); } /** Get a string representation of this vector. * @return a string representation of this vector */ @Override public String toString() { return Vector3DFormat.getInstance().format(this); } /** {@inheritDoc} */ public String toString(final NumberFormat format) { return new Vector3DFormat(format).format(this); } } ././@LongLink100644 0 0 152 12126630646 10257 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/geometry/euclidean/threed/FieldVector3D.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/geometry/euclidean/threed/FieldVector3D100644 1750 1750 136772 12126627714 32163 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.geometry.euclidean.threed; import java.io.Serializable; import java.text.NumberFormat; import org.apache.commons.math3.RealFieldElement; import org.apache.commons.math3.exception.DimensionMismatchException; import org.apache.commons.math3.exception.MathArithmeticException; import org.apache.commons.math3.exception.util.LocalizedFormats; import org.apache.commons.math3.util.FastMath; import org.apache.commons.math3.util.MathArrays; /** * This class is a re-implementation of {@link Vector3D} using {@link RealFieldElement}. *

                              Instance of this class are guaranteed to be immutable.

                              * @param the type of the field elements * @version $Id: FieldVector3D.java 1454903 2013-03-10 19:44:31Z luc $ * @since 3.2 */ public class FieldVector3D> implements Serializable { /** Serializable version identifier. */ private static final long serialVersionUID = 20130224L; /** Abscissa. */ private final T x; /** Ordinate. */ private final T y; /** Height. */ private final T z; /** Simple constructor. * Build a vector from its coordinates * @param x abscissa * @param y ordinate * @param z height * @see #getX() * @see #getY() * @see #getZ() */ public FieldVector3D(final T x, final T y, final T z) { this.x = x; this.y = y; this.z = z; } /** Simple constructor. * Build a vector from its coordinates * @param v coordinates array * @exception DimensionMismatchException if array does not have 3 elements * @see #toArray() */ public FieldVector3D(final T[] v) throws DimensionMismatchException { if (v.length != 3) { throw new DimensionMismatchException(v.length, 3); } this.x = v[0]; this.y = v[1]; this.z = v[2]; } /** Simple constructor. * Build a vector from its azimuthal coordinates * @param alpha azimuth (α) around Z * (0 is +X, π/2 is +Y, π is -X and 3π/2 is -Y) * @param delta elevation (δ) above (XY) plane, from -π/2 to +π/2 * @see #getAlpha() * @see #getDelta() */ public FieldVector3D(final T alpha, final T delta) { T cosDelta = delta.cos(); this.x = alpha.cos().multiply(cosDelta); this.y = alpha.sin().multiply(cosDelta); this.z = delta.sin(); } /** Multiplicative constructor * Build a vector from another one and a scale factor. * The vector built will be a * u * @param a scale factor * @param u base (unscaled) vector */ public FieldVector3D(final T a, final FieldVector3Du) { this.x = a.multiply(u.x); this.y = a.multiply(u.y); this.z = a.multiply(u.z); } /** Multiplicative constructor * Build a vector from another one and a scale factor. * The vector built will be a * u * @param a scale factor * @param u base (unscaled) vector */ public FieldVector3D(final T a, final Vector3D u) { this.x = a.multiply(u.getX()); this.y = a.multiply(u.getY()); this.z = a.multiply(u.getZ()); } /** Multiplicative constructor * Build a vector from another one and a scale factor. * The vector built will be a * u * @param a scale factor * @param u base (unscaled) vector */ public FieldVector3D(final double a, final FieldVector3D u) { this.x = u.x.multiply(a); this.y = u.y.multiply(a); this.z = u.z.multiply(a); } /** Linear constructor * Build a vector from two other ones and corresponding scale factors. * The vector built will be a1 * u1 + a2 * u2 * @param a1 first scale factor * @param u1 first base (unscaled) vector * @param a2 second scale factor * @param u2 second base (unscaled) vector */ public FieldVector3D(final T a1, final FieldVector3D u1, final T a2, final FieldVector3D u2) { final T prototype = a1; this.x = prototype.linearCombination(a1, u1.getX(), a2, u2.getX()); this.y = prototype.linearCombination(a1, u1.getY(), a2, u2.getY()); this.z = prototype.linearCombination(a1, u1.getZ(), a2, u2.getZ()); } /** Linear constructor * Build a vector from two other ones and corresponding scale factors. * The vector built will be a1 * u1 + a2 * u2 * @param a1 first scale factor * @param u1 first base (unscaled) vector * @param a2 second scale factor * @param u2 second base (unscaled) vector */ public FieldVector3D(final T a1, final Vector3D u1, final T a2, final Vector3D u2) { final T prototype = a1; this.x = prototype.linearCombination(u1.getX(), a1, u2.getX(), a2); this.y = prototype.linearCombination(u1.getY(), a1, u2.getY(), a2); this.z = prototype.linearCombination(u1.getZ(), a1, u2.getZ(), a2); } /** Linear constructor * Build a vector from two other ones and corresponding scale factors. * The vector built will be a1 * u1 + a2 * u2 * @param a1 first scale factor * @param u1 first base (unscaled) vector * @param a2 second scale factor * @param u2 second base (unscaled) vector */ public FieldVector3D(final double a1, final FieldVector3D u1, final double a2, final FieldVector3D u2) { final T prototype = u1.getX(); this.x = prototype.linearCombination(a1, u1.getX(), a2, u2.getX()); this.y = prototype.linearCombination(a1, u1.getY(), a2, u2.getY()); this.z = prototype.linearCombination(a1, u1.getZ(), a2, u2.getZ()); } /** Linear constructor * Build a vector from three other ones and corresponding scale factors. * The vector built will be a1 * u1 + a2 * u2 + a3 * u3 * @param a1 first scale factor * @param u1 first base (unscaled) vector * @param a2 second scale factor * @param u2 second base (unscaled) vector * @param a3 third scale factor * @param u3 third base (unscaled) vector */ public FieldVector3D(final T a1, final FieldVector3D u1, final T a2, final FieldVector3D u2, final T a3, final FieldVector3D u3) { final T prototype = a1; this.x = prototype.linearCombination(a1, u1.getX(), a2, u2.getX(), a3, u3.getX()); this.y = prototype.linearCombination(a1, u1.getY(), a2, u2.getY(), a3, u3.getY()); this.z = prototype.linearCombination(a1, u1.getZ(), a2, u2.getZ(), a3, u3.getZ()); } /** Linear constructor * Build a vector from three other ones and corresponding scale factors. * The vector built will be a1 * u1 + a2 * u2 + a3 * u3 * @param a1 first scale factor * @param u1 first base (unscaled) vector * @param a2 second scale factor * @param u2 second base (unscaled) vector * @param a3 third scale factor * @param u3 third base (unscaled) vector */ public FieldVector3D(final T a1, final Vector3D u1, final T a2, final Vector3D u2, final T a3, final Vector3D u3) { final T prototype = a1; this.x = prototype.linearCombination(u1.getX(), a1, u2.getX(), a2, u3.getX(), a3); this.y = prototype.linearCombination(u1.getY(), a1, u2.getY(), a2, u3.getY(), a3); this.z = prototype.linearCombination(u1.getZ(), a1, u2.getZ(), a2, u3.getZ(), a3); } /** Linear constructor * Build a vector from three other ones and corresponding scale factors. * The vector built will be a1 * u1 + a2 * u2 + a3 * u3 * @param a1 first scale factor * @param u1 first base (unscaled) vector * @param a2 second scale factor * @param u2 second base (unscaled) vector * @param a3 third scale factor * @param u3 third base (unscaled) vector */ public FieldVector3D(final double a1, final FieldVector3D u1, final double a2, final FieldVector3D u2, final double a3, final FieldVector3D u3) { final T prototype = u1.getX(); this.x = prototype.linearCombination(a1, u1.getX(), a2, u2.getX(), a3, u3.getX()); this.y = prototype.linearCombination(a1, u1.getY(), a2, u2.getY(), a3, u3.getY()); this.z = prototype.linearCombination(a1, u1.getZ(), a2, u2.getZ(), a3, u3.getZ()); } /** Linear constructor * Build a vector from four other ones and corresponding scale factors. * The vector built will be a1 * u1 + a2 * u2 + a3 * u3 + a4 * u4 * @param a1 first scale factor * @param u1 first base (unscaled) vector * @param a2 second scale factor * @param u2 second base (unscaled) vector * @param a3 third scale factor * @param u3 third base (unscaled) vector * @param a4 fourth scale factor * @param u4 fourth base (unscaled) vector */ public FieldVector3D(final T a1, final FieldVector3D u1, final T a2, final FieldVector3D u2, final T a3, final FieldVector3D u3, final T a4, final FieldVector3D u4) { final T prototype = a1; this.x = prototype.linearCombination(a1, u1.getX(), a2, u2.getX(), a3, u3.getX(), a4, u4.getX()); this.y = prototype.linearCombination(a1, u1.getY(), a2, u2.getY(), a3, u3.getY(), a4, u4.getY()); this.z = prototype.linearCombination(a1, u1.getZ(), a2, u2.getZ(), a3, u3.getZ(), a4, u4.getZ()); } /** Linear constructor * Build a vector from four other ones and corresponding scale factors. * The vector built will be a1 * u1 + a2 * u2 + a3 * u3 + a4 * u4 * @param a1 first scale factor * @param u1 first base (unscaled) vector * @param a2 second scale factor * @param u2 second base (unscaled) vector * @param a3 third scale factor * @param u3 third base (unscaled) vector * @param a4 fourth scale factor * @param u4 fourth base (unscaled) vector */ public FieldVector3D(final T a1, final Vector3D u1, final T a2, final Vector3D u2, final T a3, final Vector3D u3, final T a4, final Vector3D u4) { final T prototype = a1; this.x = prototype.linearCombination(u1.getX(), a1, u2.getX(), a2, u3.getX(), a3, u4.getX(), a4); this.y = prototype.linearCombination(u1.getY(), a1, u2.getY(), a2, u3.getY(), a3, u4.getY(), a4); this.z = prototype.linearCombination(u1.getZ(), a1, u2.getZ(), a2, u3.getZ(), a3, u4.getZ(), a4); } /** Linear constructor * Build a vector from four other ones and corresponding scale factors. * The vector built will be a1 * u1 + a2 * u2 + a3 * u3 + a4 * u4 * @param a1 first scale factor * @param u1 first base (unscaled) vector * @param a2 second scale factor * @param u2 second base (unscaled) vector * @param a3 third scale factor * @param u3 third base (unscaled) vector * @param a4 fourth scale factor * @param u4 fourth base (unscaled) vector */ public FieldVector3D(final double a1, final FieldVector3D u1, final double a2, final FieldVector3D u2, final double a3, final FieldVector3D u3, final double a4, final FieldVector3D u4) { final T prototype = u1.getX(); this.x = prototype.linearCombination(a1, u1.getX(), a2, u2.getX(), a3, u3.getX(), a4, u4.getX()); this.y = prototype.linearCombination(a1, u1.getY(), a2, u2.getY(), a3, u3.getY(), a4, u4.getY()); this.z = prototype.linearCombination(a1, u1.getZ(), a2, u2.getZ(), a3, u3.getZ(), a4, u4.getZ()); } /** Get the abscissa of the vector. * @return abscissa of the vector * @see #FieldVector3D(RealFieldElement, RealFieldElement, RealFieldElement) */ public T getX() { return x; } /** Get the ordinate of the vector. * @return ordinate of the vector * @see #FieldVector3D(RealFieldElement, RealFieldElement, RealFieldElement) */ public T getY() { return y; } /** Get the height of the vector. * @return height of the vector * @see #FieldVector3D(RealFieldElement, RealFieldElement, RealFieldElement) */ public T getZ() { return z; } /** Get the vector coordinates as a dimension 3 array. * @return vector coordinates * @see #FieldVector3D(RealFieldElement[]) */ public T[] toArray() { final T[] array = MathArrays.buildArray(x.getField(), 3); array[0] = x; array[1] = y; array[2] = z; return array; } /** Convert to a constant vector without derivatives. * @return a constant vector */ public Vector3D toVector3D() { return new Vector3D(x.getReal(), y.getReal(), z.getReal()); } /** Get the L1 norm for the vector. * @return L1 norm for the vector */ public T getNorm1() { return x.abs().add(y.abs()).add(z.abs()); } /** Get the L2 norm for the vector. * @return Euclidean norm for the vector */ public T getNorm() { // there are no cancellation problems here, so we use the straightforward formula return x.multiply(x).add(y.multiply(y)).add(z.multiply(z)).sqrt(); } /** Get the square of the norm for the vector. * @return square of the Euclidean norm for the vector */ public T getNormSq() { // there are no cancellation problems here, so we use the straightforward formula return x.multiply(x).add(y.multiply(y)).add(z.multiply(z)); } /** Get the L norm for the vector. * @return L norm for the vector */ public T getNormInf() { final T xAbs = x.abs(); final T yAbs = y.abs(); final T zAbs = z.abs(); if (xAbs.getReal() <= yAbs.getReal()) { if (yAbs.getReal() <= zAbs.getReal()) { return zAbs; } else { return yAbs; } } else { if (xAbs.getReal() <= zAbs.getReal()) { return zAbs; } else { return xAbs; } } } /** Get the azimuth of the vector. * @return azimuth (α) of the vector, between -π and +π * @see #FieldVector3D(RealFieldElement, RealFieldElement) */ public T getAlpha() { return y.atan2(x); } /** Get the elevation of the vector. * @return elevation (δ) of the vector, between -π/2 and +π/2 * @see #FieldVector3D(RealFieldElement, RealFieldElement) */ public T getDelta() { return z.divide(getNorm()).asin(); } /** Add a vector to the instance. * @param v vector to add * @return a new vector */ public FieldVector3D add(final FieldVector3D v) { return new FieldVector3D(x.add(v.x), y.add(v.y), z.add(v.z)); } /** Add a vector to the instance. * @param v vector to add * @return a new vector */ public FieldVector3D add(final Vector3D v) { return new FieldVector3D(x.add(v.getX()), y.add(v.getY()), z.add(v.getZ())); } /** Add a scaled vector to the instance. * @param factor scale factor to apply to v before adding it * @param v vector to add * @return a new vector */ public FieldVector3D add(final T factor, final FieldVector3D v) { return new FieldVector3D(x.getField().getOne(), this, factor, v); } /** Add a scaled vector to the instance. * @param factor scale factor to apply to v before adding it * @param v vector to add * @return a new vector */ public FieldVector3D add(final T factor, final Vector3D v) { return new FieldVector3D(x.add(factor.multiply(v.getX())), y.add(factor.multiply(v.getY())), z.add(factor.multiply(v.getZ()))); } /** Add a scaled vector to the instance. * @param factor scale factor to apply to v before adding it * @param v vector to add * @return a new vector */ public FieldVector3D add(final double factor, final FieldVector3D v) { return new FieldVector3D(1.0, this, factor, v); } /** Add a scaled vector to the instance. * @param factor scale factor to apply to v before adding it * @param v vector to add * @return a new vector */ public FieldVector3D add(final double factor, final Vector3D v) { return new FieldVector3D(x.add(factor * v.getX()), y.add(factor * v.getY()), z.add(factor * v.getZ())); } /** Subtract a vector from the instance. * @param v vector to subtract * @return a new vector */ public FieldVector3D subtract(final FieldVector3D v) { return new FieldVector3D(x.subtract(v.x), y.subtract(v.y), z.subtract(v.z)); } /** Subtract a vector from the instance. * @param v vector to subtract * @return a new vector */ public FieldVector3D subtract(final Vector3D v) { return new FieldVector3D(x.subtract(v.getX()), y.subtract(v.getY()), z.subtract(v.getZ())); } /** Subtract a scaled vector from the instance. * @param factor scale factor to apply to v before subtracting it * @param v vector to subtract * @return a new vector */ public FieldVector3D subtract(final T factor, final FieldVector3D v) { return new FieldVector3D(x.getField().getOne(), this, factor.negate(), v); } /** Subtract a scaled vector from the instance. * @param factor scale factor to apply to v before subtracting it * @param v vector to subtract * @return a new vector */ public FieldVector3D subtract(final T factor, final Vector3D v) { return new FieldVector3D(x.subtract(factor.multiply(v.getX())), y.subtract(factor.multiply(v.getY())), z.subtract(factor.multiply(v.getZ()))); } /** Subtract a scaled vector from the instance. * @param factor scale factor to apply to v before subtracting it * @param v vector to subtract * @return a new vector */ public FieldVector3D subtract(final double factor, final FieldVector3D v) { return new FieldVector3D(1.0, this, -factor, v); } /** Subtract a scaled vector from the instance. * @param factor scale factor to apply to v before subtracting it * @param v vector to subtract * @return a new vector */ public FieldVector3D subtract(final double factor, final Vector3D v) { return new FieldVector3D(x.subtract(factor * v.getX()), y.subtract(factor * v.getY()), z.subtract(factor * v.getZ())); } /** Get a normalized vector aligned with the instance. * @return a new normalized vector * @exception MathArithmeticException if the norm is zero */ public FieldVector3D normalize() throws MathArithmeticException { final T s = getNorm(); if (s.getReal() == 0) { throw new MathArithmeticException(LocalizedFormats.CANNOT_NORMALIZE_A_ZERO_NORM_VECTOR); } return scalarMultiply(s.reciprocal()); } /** Get a vector orthogonal to the instance. *

                              There are an infinite number of normalized vectors orthogonal * to the instance. This method picks up one of them almost * arbitrarily. It is useful when one needs to compute a reference * frame with one of the axes in a predefined direction. The * following example shows how to build a frame having the k axis * aligned with the known vector u : *

                              
                                   *   Vector3D k = u.normalize();
                                   *   Vector3D i = k.orthogonal();
                                   *   Vector3D j = Vector3D.crossProduct(k, i);
                                   * 

                              * @return a new normalized vector orthogonal to the instance * @exception MathArithmeticException if the norm of the instance is null */ public FieldVector3D orthogonal() throws MathArithmeticException { final double threshold = 0.6 * getNorm().getReal(); if (threshold == 0) { throw new MathArithmeticException(LocalizedFormats.ZERO_NORM); } if (FastMath.abs(x.getReal()) <= threshold) { final T inverse = y.multiply(y).add(z.multiply(z)).sqrt().reciprocal(); return new FieldVector3D(inverse.getField().getZero(), inverse.multiply(z), inverse.multiply(y).negate()); } else if (FastMath.abs(y.getReal()) <= threshold) { final T inverse = x.multiply(x).add(z.multiply(z)).sqrt().reciprocal(); return new FieldVector3D(inverse.multiply(z).negate(), inverse.getField().getZero(), inverse.multiply(x)); } else { final T inverse = x.multiply(x).add(y.multiply(y)).sqrt().reciprocal(); return new FieldVector3D(inverse.multiply(y), inverse.multiply(x).negate(), inverse.getField().getZero()); } } /** Compute the angular separation between two vectors. *

                              This method computes the angular separation between two * vectors using the dot product for well separated vectors and the * cross product for almost aligned vectors. This allows to have a * good accuracy in all cases, even for vectors very close to each * other.

                              * @param v1 first vector * @param v2 second vector * @param the type of the field elements * @return angular separation between v1 and v2 * @exception MathArithmeticException if either vector has a null norm */ public static > T angle(final FieldVector3D v1, final FieldVector3D v2) throws MathArithmeticException { final T normProduct = v1.getNorm().multiply(v2.getNorm()); if (normProduct.getReal() == 0) { throw new MathArithmeticException(LocalizedFormats.ZERO_NORM); } final T dot = dotProduct(v1, v2); final double threshold = normProduct.getReal() * 0.9999; if ((dot.getReal() < -threshold) || (dot.getReal() > threshold)) { // the vectors are almost aligned, compute using the sine FieldVector3D v3 = crossProduct(v1, v2); if (dot.getReal() >= 0) { return v3.getNorm().divide(normProduct).asin(); } return v3.getNorm().divide(normProduct).asin().subtract(FastMath.PI).negate(); } // the vectors are sufficiently separated to use the cosine return dot.divide(normProduct).acos(); } /** Compute the angular separation between two vectors. *

                              This method computes the angular separation between two * vectors using the dot product for well separated vectors and the * cross product for almost aligned vectors. This allows to have a * good accuracy in all cases, even for vectors very close to each * other.

                              * @param v1 first vector * @param v2 second vector * @param the type of the field elements * @return angular separation between v1 and v2 * @exception MathArithmeticException if either vector has a null norm */ public static > T angle(final FieldVector3D v1, final Vector3D v2) throws MathArithmeticException { final T normProduct = v1.getNorm().multiply(v2.getNorm()); if (normProduct.getReal() == 0) { throw new MathArithmeticException(LocalizedFormats.ZERO_NORM); } final T dot = dotProduct(v1, v2); final double threshold = normProduct.getReal() * 0.9999; if ((dot.getReal() < -threshold) || (dot.getReal() > threshold)) { // the vectors are almost aligned, compute using the sine FieldVector3D v3 = crossProduct(v1, v2); if (dot.getReal() >= 0) { return v3.getNorm().divide(normProduct).asin(); } return v3.getNorm().divide(normProduct).asin().subtract(FastMath.PI).negate(); } // the vectors are sufficiently separated to use the cosine return dot.divide(normProduct).acos(); } /** Compute the angular separation between two vectors. *

                              This method computes the angular separation between two * vectors using the dot product for well separated vectors and the * cross product for almost aligned vectors. This allows to have a * good accuracy in all cases, even for vectors very close to each * other.

                              * @param v1 first vector * @param v2 second vector * @param the type of the field elements * @return angular separation between v1 and v2 * @exception MathArithmeticException if either vector has a null norm */ public static > T angle(final Vector3D v1, final FieldVector3D v2) throws MathArithmeticException { return angle(v2, v1); } /** Get the opposite of the instance. * @return a new vector which is opposite to the instance */ public FieldVector3D negate() { return new FieldVector3D(x.negate(), y.negate(), z.negate()); } /** Multiply the instance by a scalar. * @param a scalar * @return a new vector */ public FieldVector3D scalarMultiply(final T a) { return new FieldVector3D(x.multiply(a), y.multiply(a), z.multiply(a)); } /** Multiply the instance by a scalar. * @param a scalar * @return a new vector */ public FieldVector3D scalarMultiply(final double a) { return new FieldVector3D(x.multiply(a), y.multiply(a), z.multiply(a)); } /** * Returns true if any coordinate of this vector is NaN; false otherwise * @return true if any coordinate of this vector is NaN; false otherwise */ public boolean isNaN() { return Double.isNaN(x.getReal()) || Double.isNaN(y.getReal()) || Double.isNaN(z.getReal()); } /** * Returns true if any coordinate of this vector is infinite and none are NaN; * false otherwise * @return true if any coordinate of this vector is infinite and none are NaN; * false otherwise */ public boolean isInfinite() { return !isNaN() && (Double.isInfinite(x.getReal()) || Double.isInfinite(y.getReal()) || Double.isInfinite(z.getReal())); } /** * Test for the equality of two 3D vectors. *

                              * If all coordinates of two 3D vectors are exactly the same, and none of their * {@link RealFieldElement#getReal() real part} are NaN, the * two 3D vectors are considered to be equal. *

                              *

                              * NaN coordinates are considered to affect globally the vector * and be equals to each other - i.e, if either (or all) real part of the * coordinates of the 3D vector are NaN, the 3D vector is NaN. *

                              * * @param other Object to test for equality to this * @return true if two 3D vector objects are equal, false if * object is null, not an instance of Vector3D, or * not equal to this Vector3D instance * */ @Override public boolean equals(Object other) { if (this == other) { return true; } if (other instanceof FieldVector3D) { @SuppressWarnings("unchecked") final FieldVector3D rhs = (FieldVector3D) other; if (rhs.isNaN()) { return this.isNaN(); } return x.equals(rhs.x) && y.equals(rhs.y) && z.equals(rhs.z); } return false; } /** * Get a hashCode for the 3D vector. *

                              * All NaN values have the same hash code.

                              * * @return a hash code value for this object */ @Override public int hashCode() { if (isNaN()) { return 409; } return 311 * (107 * x.hashCode() + 83 * y.hashCode() + z.hashCode()); } /** Compute the dot-product of the instance and another vector. *

                              * The implementation uses specific multiplication and addition * algorithms to preserve accuracy and reduce cancellation effects. * It should be very accurate even for nearly orthogonal vectors. *

                              * @see MathArrays#linearCombination(double, double, double, double, double, double) * @param v second vector * @return the dot product this.v */ public T dotProduct(final FieldVector3D v) { return x.linearCombination(x, v.x, y, v.y, z, v.z); } /** Compute the dot-product of the instance and another vector. *

                              * The implementation uses specific multiplication and addition * algorithms to preserve accuracy and reduce cancellation effects. * It should be very accurate even for nearly orthogonal vectors. *

                              * @see MathArrays#linearCombination(double, double, double, double, double, double) * @param v second vector * @return the dot product this.v */ public T dotProduct(final Vector3D v) { return x.linearCombination(v.getX(), x, v.getY(), y, v.getZ(), z); } /** Compute the cross-product of the instance with another vector. * @param v other vector * @return the cross product this ^ v as a new Vector3D */ public FieldVector3D crossProduct(final FieldVector3D v) { return new FieldVector3D(x.linearCombination(y, v.z, z.negate(), v.y), y.linearCombination(z, v.x, x.negate(), v.z), z.linearCombination(x, v.y, y.negate(), v.x)); } /** Compute the cross-product of the instance with another vector. * @param v other vector * @return the cross product this ^ v as a new Vector3D */ public FieldVector3D crossProduct(final Vector3D v) { return new FieldVector3D(x.linearCombination(v.getZ(), y, -v.getY(), z), y.linearCombination(v.getX(), z, -v.getZ(), x), z.linearCombination(v.getY(), x, -v.getX(), y)); } /** Compute the distance between the instance and another vector according to the L1 norm. *

                              Calling this method is equivalent to calling: * q.subtract(p).getNorm1() except that no intermediate * vector is built

                              * @param v second vector * @return the distance between the instance and p according to the L1 norm */ public T distance1(final FieldVector3D v) { final T dx = v.x.subtract(x).abs(); final T dy = v.y.subtract(y).abs(); final T dz = v.z.subtract(z).abs(); return dx.add(dy).add(dz); } /** Compute the distance between the instance and another vector according to the L1 norm. *

                              Calling this method is equivalent to calling: * q.subtract(p).getNorm1() except that no intermediate * vector is built

                              * @param v second vector * @return the distance between the instance and p according to the L1 norm */ public T distance1(final Vector3D v) { final T dx = x.subtract(v.getX()).abs(); final T dy = y.subtract(v.getY()).abs(); final T dz = z.subtract(v.getZ()).abs(); return dx.add(dy).add(dz); } /** Compute the distance between the instance and another vector according to the L2 norm. *

                              Calling this method is equivalent to calling: * q.subtract(p).getNorm() except that no intermediate * vector is built

                              * @param v second vector * @return the distance between the instance and p according to the L2 norm */ public T distance(final FieldVector3D v) { final T dx = v.x.subtract(x); final T dy = v.y.subtract(y); final T dz = v.z.subtract(z); return dx.multiply(dx).add(dy.multiply(dy)).add(dz.multiply(dz)).sqrt(); } /** Compute the distance between the instance and another vector according to the L2 norm. *

                              Calling this method is equivalent to calling: * q.subtract(p).getNorm() except that no intermediate * vector is built

                              * @param v second vector * @return the distance between the instance and p according to the L2 norm */ public T distance(final Vector3D v) { final T dx = x.subtract(v.getX()); final T dy = y.subtract(v.getY()); final T dz = z.subtract(v.getZ()); return dx.multiply(dx).add(dy.multiply(dy)).add(dz.multiply(dz)).sqrt(); } /** Compute the distance between the instance and another vector according to the L norm. *

                              Calling this method is equivalent to calling: * q.subtract(p).getNormInf() except that no intermediate * vector is built

                              * @param v second vector * @return the distance between the instance and p according to the L norm */ public T distanceInf(final FieldVector3D v) { final T dx = v.x.subtract(x).abs(); final T dy = v.y.subtract(y).abs(); final T dz = v.z.subtract(z).abs(); if (dx.getReal() <= dy.getReal()) { if (dy.getReal() <= dz.getReal()) { return dz; } else { return dy; } } else { if (dx.getReal() <= dz.getReal()) { return dz; } else { return dx; } } } /** Compute the distance between the instance and another vector according to the L norm. *

                              Calling this method is equivalent to calling: * q.subtract(p).getNormInf() except that no intermediate * vector is built

                              * @param v second vector * @return the distance between the instance and p according to the L norm */ public T distanceInf(final Vector3D v) { final T dx = x.subtract(v.getX()).abs(); final T dy = y.subtract(v.getY()).abs(); final T dz = z.subtract(v.getZ()).abs(); if (dx.getReal() <= dy.getReal()) { if (dy.getReal() <= dz.getReal()) { return dz; } else { return dy; } } else { if (dx.getReal() <= dz.getReal()) { return dz; } else { return dx; } } } /** Compute the square of the distance between the instance and another vector. *

                              Calling this method is equivalent to calling: * q.subtract(p).getNormSq() except that no intermediate * vector is built

                              * @param v second vector * @return the square of the distance between the instance and p */ public T distanceSq(final FieldVector3D v) { final T dx = v.x.subtract(x); final T dy = v.y.subtract(y); final T dz = v.z.subtract(z); return dx.multiply(dx).add(dy.multiply(dy)).add(dz.multiply(dz)); } /** Compute the square of the distance between the instance and another vector. *

                              Calling this method is equivalent to calling: * q.subtract(p).getNormSq() except that no intermediate * vector is built

                              * @param v second vector * @return the square of the distance between the instance and p */ public T distanceSq(final Vector3D v) { final T dx = x.subtract(v.getX()); final T dy = y.subtract(v.getY()); final T dz = z.subtract(v.getZ()); return dx.multiply(dx).add(dy.multiply(dy)).add(dz.multiply(dz)); } /** Compute the dot-product of two vectors. * @param v1 first vector * @param v2 second vector * @param the type of the field elements * @return the dot product v1.v2 */ public static > T dotProduct(final FieldVector3D v1, final FieldVector3D v2) { return v1.dotProduct(v2); } /** Compute the dot-product of two vectors. * @param v1 first vector * @param v2 second vector * @param the type of the field elements * @return the dot product v1.v2 */ public static > T dotProduct(final FieldVector3D v1, final Vector3D v2) { return v1.dotProduct(v2); } /** Compute the dot-product of two vectors. * @param v1 first vector * @param v2 second vector * @param the type of the field elements * @return the dot product v1.v2 */ public static > T dotProduct(final Vector3D v1, final FieldVector3D v2) { return v2.dotProduct(v1); } /** Compute the cross-product of two vectors. * @param v1 first vector * @param v2 second vector * @param the type of the field elements * @return the cross product v1 ^ v2 as a new Vector */ public static > FieldVector3D crossProduct(final FieldVector3D v1, final FieldVector3D v2) { return v1.crossProduct(v2); } /** Compute the cross-product of two vectors. * @param v1 first vector * @param v2 second vector * @param the type of the field elements * @return the cross product v1 ^ v2 as a new Vector */ public static > FieldVector3D crossProduct(final FieldVector3D v1, final Vector3D v2) { return v1.crossProduct(v2); } /** Compute the cross-product of two vectors. * @param v1 first vector * @param v2 second vector * @param the type of the field elements * @return the cross product v1 ^ v2 as a new Vector */ public static > FieldVector3D crossProduct(final Vector3D v1, final FieldVector3D v2) { return new FieldVector3D(v2.x.linearCombination(v1.getY(), v2.z, -v1.getZ(), v2.y), v2.y.linearCombination(v1.getZ(), v2.x, -v1.getX(), v2.z), v2.z.linearCombination(v1.getX(), v2.y, -v1.getY(), v2.x)); } /** Compute the distance between two vectors according to the L1 norm. *

                              Calling this method is equivalent to calling: * v1.subtract(v2).getNorm1() except that no intermediate * vector is built

                              * @param v1 first vector * @param v2 second vector * @param the type of the field elements * @return the distance between v1 and v2 according to the L1 norm */ public static > T distance1(final FieldVector3D v1, final FieldVector3D v2) { return v1.distance1(v2); } /** Compute the distance between two vectors according to the L1 norm. *

                              Calling this method is equivalent to calling: * v1.subtract(v2).getNorm1() except that no intermediate * vector is built

                              * @param v1 first vector * @param v2 second vector * @param the type of the field elements * @return the distance between v1 and v2 according to the L1 norm */ public static > T distance1(final FieldVector3D v1, final Vector3D v2) { return v1.distance1(v2); } /** Compute the distance between two vectors according to the L1 norm. *

                              Calling this method is equivalent to calling: * v1.subtract(v2).getNorm1() except that no intermediate * vector is built

                              * @param v1 first vector * @param v2 second vector * @param the type of the field elements * @return the distance between v1 and v2 according to the L1 norm */ public static > T distance1(final Vector3D v1, final FieldVector3D v2) { return v2.distance1(v1); } /** Compute the distance between two vectors according to the L2 norm. *

                              Calling this method is equivalent to calling: * v1.subtract(v2).getNorm() except that no intermediate * vector is built

                              * @param v1 first vector * @param v2 second vector * @param the type of the field elements * @return the distance between v1 and v2 according to the L2 norm */ public static > T distance(final FieldVector3D v1, final FieldVector3D v2) { return v1.distance(v2); } /** Compute the distance between two vectors according to the L2 norm. *

                              Calling this method is equivalent to calling: * v1.subtract(v2).getNorm() except that no intermediate * vector is built

                              * @param v1 first vector * @param v2 second vector * @param the type of the field elements * @return the distance between v1 and v2 according to the L2 norm */ public static > T distance(final FieldVector3D v1, final Vector3D v2) { return v1.distance(v2); } /** Compute the distance between two vectors according to the L2 norm. *

                              Calling this method is equivalent to calling: * v1.subtract(v2).getNorm() except that no intermediate * vector is built

                              * @param v1 first vector * @param v2 second vector * @param the type of the field elements * @return the distance between v1 and v2 according to the L2 norm */ public static > T distance(final Vector3D v1, final FieldVector3D v2) { return v2.distance(v1); } /** Compute the distance between two vectors according to the L norm. *

                              Calling this method is equivalent to calling: * v1.subtract(v2).getNormInf() except that no intermediate * vector is built

                              * @param v1 first vector * @param v2 second vector * @param the type of the field elements * @return the distance between v1 and v2 according to the L norm */ public static > T distanceInf(final FieldVector3D v1, final FieldVector3D v2) { return v1.distanceInf(v2); } /** Compute the distance between two vectors according to the L norm. *

                              Calling this method is equivalent to calling: * v1.subtract(v2).getNormInf() except that no intermediate * vector is built

                              * @param v1 first vector * @param v2 second vector * @param the type of the field elements * @return the distance between v1 and v2 according to the L norm */ public static > T distanceInf(final FieldVector3D v1, final Vector3D v2) { return v1.distanceInf(v2); } /** Compute the distance between two vectors according to the L norm. *

                              Calling this method is equivalent to calling: * v1.subtract(v2).getNormInf() except that no intermediate * vector is built

                              * @param v1 first vector * @param v2 second vector * @param the type of the field elements * @return the distance between v1 and v2 according to the L norm */ public static > T distanceInf(final Vector3D v1, final FieldVector3D v2) { return v2.distanceInf(v1); } /** Compute the square of the distance between two vectors. *

                              Calling this method is equivalent to calling: * v1.subtract(v2).getNormSq() except that no intermediate * vector is built

                              * @param v1 first vector * @param v2 second vector * @param the type of the field elements * @return the square of the distance between v1 and v2 */ public static > T distanceSq(final FieldVector3D v1, final FieldVector3D v2) { return v1.distanceSq(v2); } /** Compute the square of the distance between two vectors. *

                              Calling this method is equivalent to calling: * v1.subtract(v2).getNormSq() except that no intermediate * vector is built

                              * @param v1 first vector * @param v2 second vector * @param the type of the field elements * @return the square of the distance between v1 and v2 */ public static > T distanceSq(final FieldVector3D v1, final Vector3D v2) { return v1.distanceSq(v2); } /** Compute the square of the distance between two vectors. *

                              Calling this method is equivalent to calling: * v1.subtract(v2).getNormSq() except that no intermediate * vector is built

                              * @param v1 first vector * @param v2 second vector * @param the type of the field elements * @return the square of the distance between v1 and v2 */ public static > T distanceSq(final Vector3D v1, final FieldVector3D v2) { return v2.distanceSq(v1); } /** Get a string representation of this vector. * @return a string representation of this vector */ @Override public String toString() { return Vector3DFormat.getInstance().format(toVector3D()); } /** Get a string representation of this vector. * @param format the custom format for components * @return a string representation of this vector */ public String toString(final NumberFormat format) { return new Vector3DFormat(format).format(toVector3D()); } } ././@LongLink100644 0 0 150 12126630647 10256 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/geometry/euclidean/threed/Euclidean3D.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/geometry/euclidean/threed/Euclidean3D.j100644 1750 1750 4476 12126627714 32031 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.geometry.euclidean.threed; import java.io.Serializable; import org.apache.commons.math3.geometry.Space; import org.apache.commons.math3.geometry.euclidean.twod.Euclidean2D; /** * This class implements a three-dimensional space. * @version $Id: Euclidean3D.java 1416643 2012-12-03 19:37:14Z tn $ * @since 3.0 */ public class Euclidean3D implements Serializable, Space { /** Serializable version identifier. */ private static final long serialVersionUID = 6249091865814886817L; /** Private constructor for the singleton. */ private Euclidean3D() { } /** Get the unique instance. * @return the unique instance */ public static Euclidean3D getInstance() { return LazyHolder.INSTANCE; } /** {@inheritDoc} */ public int getDimension() { return 3; } /** {@inheritDoc} */ public Euclidean2D getSubSpace() { return Euclidean2D.getInstance(); } // CHECKSTYLE: stop HideUtilityClassConstructor /** Holder for the instance. *

                              We use here the Initialization On Demand Holder Idiom.

                              */ private static class LazyHolder { /** Cached field instance. */ private static final Euclidean3D INSTANCE = new Euclidean3D(); } // CHECKSTYLE: resume HideUtilityClassConstructor /** Handle deserialization of the singleton. * @return the singleton instance */ private Object readResolve() { // return the singleton instance return LazyHolder.INSTANCE; } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/geometry/euclidean/threed/SubLine.java100644 1750 1750 13703 12126627714 32013 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.geometry.euclidean.threed; import java.util.ArrayList; import java.util.List; import org.apache.commons.math3.exception.MathIllegalArgumentException; import org.apache.commons.math3.geometry.euclidean.oned.Interval; import org.apache.commons.math3.geometry.euclidean.oned.IntervalsSet; import org.apache.commons.math3.geometry.euclidean.oned.Vector1D; import org.apache.commons.math3.geometry.partitioning.Region.Location; /** This class represents a subset of a {@link Line}. * @version $Id: SubLine.java 1416643 2012-12-03 19:37:14Z tn $ * @since 3.0 */ public class SubLine { /** Underlying line. */ private final Line line; /** Remaining region of the hyperplane. */ private final IntervalsSet remainingRegion; /** Simple constructor. * @param line underlying line * @param remainingRegion remaining region of the line */ public SubLine(final Line line, final IntervalsSet remainingRegion) { this.line = line; this.remainingRegion = remainingRegion; } /** Create a sub-line from two endpoints. * @param start start point * @param end end point * @exception MathIllegalArgumentException if the points are equal */ public SubLine(final Vector3D start, final Vector3D end) throws MathIllegalArgumentException { this(new Line(start, end), buildIntervalSet(start, end)); } /** Create a sub-line from a segment. * @param segment single segment forming the sub-line * @exception MathIllegalArgumentException if the segment endpoints are equal */ public SubLine(final Segment segment) throws MathIllegalArgumentException { this(segment.getLine(), buildIntervalSet(segment.getStart(), segment.getEnd())); } /** Get the endpoints of the sub-line. *

                              * A subline may be any arbitrary number of disjoints segments, so the endpoints * are provided as a list of endpoint pairs. Each element of the list represents * one segment, and each segment contains a start point at index 0 and an end point * at index 1. If the sub-line is unbounded in the negative infinity direction, * the start point of the first segment will have infinite coordinates. If the * sub-line is unbounded in the positive infinity direction, the end point of the * last segment will have infinite coordinates. So a sub-line covering the whole * line will contain just one row and both elements of this row will have infinite * coordinates. If the sub-line is empty, the returned list will contain 0 segments. *

                              * @return list of segments endpoints */ public List getSegments() { final List list = remainingRegion.asList(); final List segments = new ArrayList(); for (final Interval interval : list) { final Vector3D start = line.toSpace(new Vector1D(interval.getInf())); final Vector3D end = line.toSpace(new Vector1D(interval.getSup())); segments.add(new Segment(start, end, line)); } return segments; } /** Get the intersection of the instance and another sub-line. *

                              * This method is related to the {@link Line#intersection(Line) * intersection} method in the {@link Line Line} class, but in addition * to compute the point along infinite lines, it also checks the point * lies on both sub-line ranges. *

                              * @param subLine other sub-line which may intersect instance * @param includeEndPoints if true, endpoints are considered to belong to * instance (i.e. they are closed sets) and may be returned, otherwise endpoints * are considered to not belong to instance (i.e. they are open sets) and intersection * occurring on endpoints lead to null being returned * @return the intersection point if there is one, null if the sub-lines don't intersect */ public Vector3D intersection(final SubLine subLine, final boolean includeEndPoints) { // compute the intersection on infinite line Vector3D v1D = line.intersection(subLine.line); // check location of point with respect to first sub-line Location loc1 = remainingRegion.checkPoint(line.toSubSpace(v1D)); // check location of point with respect to second sub-line Location loc2 = subLine.remainingRegion.checkPoint(subLine.line.toSubSpace(v1D)); if (includeEndPoints) { return ((loc1 != Location.OUTSIDE) && (loc2 != Location.OUTSIDE)) ? v1D : null; } else { return ((loc1 == Location.INSIDE) && (loc2 == Location.INSIDE)) ? v1D : null; } } /** Build an interval set from two points. * @param start start point * @param end end point * @return an interval set * @exception MathIllegalArgumentException if the points are equal */ private static IntervalsSet buildIntervalSet(final Vector3D start, final Vector3D end) throws MathIllegalArgumentException { final Line line = new Line(start, end); return new IntervalsSet(line.toSubSpace(start).getX(), line.toSubSpace(end).getX()); } } ././@LongLink100644 0 0 153 12126630647 10261 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/geometry/euclidean/threed/PolyhedronsSet.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/geometry/euclidean/threed/PolyhedronsSe100644 1750 1750 44712 12126627714 32334 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.geometry.euclidean.threed; import java.awt.geom.AffineTransform; import java.util.Collection; import org.apache.commons.math3.geometry.Vector; import org.apache.commons.math3.geometry.euclidean.oned.Euclidean1D; import org.apache.commons.math3.geometry.euclidean.twod.Euclidean2D; import org.apache.commons.math3.geometry.euclidean.twod.SubLine; import org.apache.commons.math3.geometry.euclidean.twod.Vector2D; import org.apache.commons.math3.geometry.partitioning.AbstractRegion; import org.apache.commons.math3.geometry.partitioning.BSPTree; import org.apache.commons.math3.geometry.partitioning.BSPTreeVisitor; import org.apache.commons.math3.geometry.partitioning.BoundaryAttribute; import org.apache.commons.math3.geometry.partitioning.Hyperplane; import org.apache.commons.math3.geometry.partitioning.Region; import org.apache.commons.math3.geometry.partitioning.RegionFactory; import org.apache.commons.math3.geometry.partitioning.SubHyperplane; import org.apache.commons.math3.geometry.partitioning.Transform; import org.apache.commons.math3.util.FastMath; /** This class represents a 3D region: a set of polyhedrons. * @version $Id: PolyhedronsSet.java 1416643 2012-12-03 19:37:14Z tn $ * @since 3.0 */ public class PolyhedronsSet extends AbstractRegion { /** Build a polyhedrons set representing the whole real line. */ public PolyhedronsSet() { super(); } /** Build a polyhedrons set from a BSP tree. *

                              The leaf nodes of the BSP tree must have a * {@code Boolean} attribute representing the inside status of * the corresponding cell (true for inside cells, false for outside * cells). In order to avoid building too many small objects, it is * recommended to use the predefined constants * {@code Boolean.TRUE} and {@code Boolean.FALSE}

                              * @param tree inside/outside BSP tree representing the region */ public PolyhedronsSet(final BSPTree tree) { super(tree); } /** Build a polyhedrons set from a Boundary REPresentation (B-rep). *

                              The boundary is provided as a collection of {@link * SubHyperplane sub-hyperplanes}. Each sub-hyperplane has the * interior part of the region on its minus side and the exterior on * its plus side.

                              *

                              The boundary elements can be in any order, and can form * several non-connected sets (like for example polyhedrons with holes * or a set of disjoint polyhedrons considered as a whole). In * fact, the elements do not even need to be connected together * (their topological connections are not used here). However, if the * boundary does not really separate an inside open from an outside * open (open having here its topological meaning), then subsequent * calls to the {@link Region#checkPoint(Vector) checkPoint} method will * not be meaningful anymore.

                              *

                              If the boundary is empty, the region will represent the whole * space.

                              * @param boundary collection of boundary elements, as a * collection of {@link SubHyperplane SubHyperplane} objects */ public PolyhedronsSet(final Collection> boundary) { super(boundary); } /** Build a parallellepipedic box. * @param xMin low bound along the x direction * @param xMax high bound along the x direction * @param yMin low bound along the y direction * @param yMax high bound along the y direction * @param zMin low bound along the z direction * @param zMax high bound along the z direction */ public PolyhedronsSet(final double xMin, final double xMax, final double yMin, final double yMax, final double zMin, final double zMax) { super(buildBoundary(xMin, xMax, yMin, yMax, zMin, zMax)); } /** Build a parallellepipedic box boundary. * @param xMin low bound along the x direction * @param xMax high bound along the x direction * @param yMin low bound along the y direction * @param yMax high bound along the y direction * @param zMin low bound along the z direction * @param zMax high bound along the z direction * @return boundary tree */ private static BSPTree buildBoundary(final double xMin, final double xMax, final double yMin, final double yMax, final double zMin, final double zMax) { final Plane pxMin = new Plane(new Vector3D(xMin, 0, 0), Vector3D.MINUS_I); final Plane pxMax = new Plane(new Vector3D(xMax, 0, 0), Vector3D.PLUS_I); final Plane pyMin = new Plane(new Vector3D(0, yMin, 0), Vector3D.MINUS_J); final Plane pyMax = new Plane(new Vector3D(0, yMax, 0), Vector3D.PLUS_J); final Plane pzMin = new Plane(new Vector3D(0, 0, zMin), Vector3D.MINUS_K); final Plane pzMax = new Plane(new Vector3D(0, 0, zMax), Vector3D.PLUS_K); @SuppressWarnings("unchecked") final Region boundary = new RegionFactory().buildConvex(pxMin, pxMax, pyMin, pyMax, pzMin, pzMax); return boundary.getTree(false); } /** {@inheritDoc} */ @Override public PolyhedronsSet buildNew(final BSPTree tree) { return new PolyhedronsSet(tree); } /** {@inheritDoc} */ @Override protected void computeGeometricalProperties() { // compute the contribution of all boundary facets getTree(true).visit(new FacetsContributionVisitor()); if (getSize() < 0) { // the polyhedrons set as a finite outside // surrounded by an infinite inside setSize(Double.POSITIVE_INFINITY); setBarycenter(Vector3D.NaN); } else { // the polyhedrons set is finite, apply the remaining scaling factors setSize(getSize() / 3.0); setBarycenter(new Vector3D(1.0 / (4 * getSize()), (Vector3D) getBarycenter())); } } /** Visitor computing geometrical properties. */ private class FacetsContributionVisitor implements BSPTreeVisitor { /** Simple constructor. */ public FacetsContributionVisitor() { setSize(0); setBarycenter(new Vector3D(0, 0, 0)); } /** {@inheritDoc} */ public Order visitOrder(final BSPTree node) { return Order.MINUS_SUB_PLUS; } /** {@inheritDoc} */ public void visitInternalNode(final BSPTree node) { @SuppressWarnings("unchecked") final BoundaryAttribute attribute = (BoundaryAttribute) node.getAttribute(); if (attribute.getPlusOutside() != null) { addContribution(attribute.getPlusOutside(), false); } if (attribute.getPlusInside() != null) { addContribution(attribute.getPlusInside(), true); } } /** {@inheritDoc} */ public void visitLeafNode(final BSPTree node) { } /** Add he contribution of a boundary facet. * @param facet boundary facet * @param reversed if true, the facet has the inside on its plus side */ private void addContribution(final SubHyperplane facet, final boolean reversed) { final Region polygon = ((SubPlane) facet).getRemainingRegion(); final double area = polygon.getSize(); if (Double.isInfinite(area)) { setSize(Double.POSITIVE_INFINITY); setBarycenter(Vector3D.NaN); } else { final Plane plane = (Plane) facet.getHyperplane(); final Vector3D facetB = plane.toSpace(polygon.getBarycenter()); double scaled = area * facetB.dotProduct(plane.getNormal()); if (reversed) { scaled = -scaled; } setSize(getSize() + scaled); setBarycenter(new Vector3D(1.0, (Vector3D) getBarycenter(), scaled, facetB)); } } } /** Get the first sub-hyperplane crossed by a semi-infinite line. * @param point start point of the part of the line considered * @param line line to consider (contains point) * @return the first sub-hyperplaned crossed by the line after the * given point, or null if the line does not intersect any * sub-hyperplaned */ public SubHyperplane firstIntersection(final Vector3D point, final Line line) { return recurseFirstIntersection(getTree(true), point, line); } /** Get the first sub-hyperplane crossed by a semi-infinite line. * @param node current node * @param point start point of the part of the line considered * @param line line to consider (contains point) * @return the first sub-hyperplaned crossed by the line after the * given point, or null if the line does not intersect any * sub-hyperplaned */ private SubHyperplane recurseFirstIntersection(final BSPTree node, final Vector3D point, final Line line) { final SubHyperplane cut = node.getCut(); if (cut == null) { return null; } final BSPTree minus = node.getMinus(); final BSPTree plus = node.getPlus(); final Plane plane = (Plane) cut.getHyperplane(); // establish search order final double offset = plane.getOffset(point); final boolean in = FastMath.abs(offset) < 1.0e-10; final BSPTree near; final BSPTree far; if (offset < 0) { near = minus; far = plus; } else { near = plus; far = minus; } if (in) { // search in the cut hyperplane final SubHyperplane facet = boundaryFacet(point, node); if (facet != null) { return facet; } } // search in the near branch final SubHyperplane crossed = recurseFirstIntersection(near, point, line); if (crossed != null) { return crossed; } if (!in) { // search in the cut hyperplane final Vector3D hit3D = plane.intersection(line); if (hit3D != null) { final SubHyperplane facet = boundaryFacet(hit3D, node); if (facet != null) { return facet; } } } // search in the far branch return recurseFirstIntersection(far, point, line); } /** Check if a point belongs to the boundary part of a node. * @param point point to check * @param node node containing the boundary facet to check * @return the boundary facet this points belongs to (or null if it * does not belong to any boundary facet) */ private SubHyperplane boundaryFacet(final Vector3D point, final BSPTree node) { final Vector2D point2D = ((Plane) node.getCut().getHyperplane()).toSubSpace(point); @SuppressWarnings("unchecked") final BoundaryAttribute attribute = (BoundaryAttribute) node.getAttribute(); if ((attribute.getPlusOutside() != null) && (((SubPlane) attribute.getPlusOutside()).getRemainingRegion().checkPoint(point2D) == Location.INSIDE)) { return attribute.getPlusOutside(); } if ((attribute.getPlusInside() != null) && (((SubPlane) attribute.getPlusInside()).getRemainingRegion().checkPoint(point2D) == Location.INSIDE)) { return attribute.getPlusInside(); } return null; } /** Rotate the region around the specified point. *

                              The instance is not modified, a new instance is created.

                              * @param center rotation center * @param rotation vectorial rotation operator * @return a new instance representing the rotated region */ public PolyhedronsSet rotate(final Vector3D center, final Rotation rotation) { return (PolyhedronsSet) applyTransform(new RotationTransform(center, rotation)); } /** 3D rotation as a Transform. */ private static class RotationTransform implements Transform { /** Center point of the rotation. */ private Vector3D center; /** Vectorial rotation. */ private Rotation rotation; /** Cached original hyperplane. */ private Plane cachedOriginal; /** Cached 2D transform valid inside the cached original hyperplane. */ private Transform cachedTransform; /** Build a rotation transform. * @param center center point of the rotation * @param rotation vectorial rotation */ public RotationTransform(final Vector3D center, final Rotation rotation) { this.center = center; this.rotation = rotation; } /** {@inheritDoc} */ public Vector3D apply(final Vector point) { final Vector3D delta = ((Vector3D) point).subtract(center); return new Vector3D(1.0, center, 1.0, rotation.applyTo(delta)); } /** {@inheritDoc} */ public Plane apply(final Hyperplane hyperplane) { return ((Plane) hyperplane).rotate(center, rotation); } /** {@inheritDoc} */ public SubHyperplane apply(final SubHyperplane sub, final Hyperplane original, final Hyperplane transformed) { if (original != cachedOriginal) { // we have changed hyperplane, reset the in-hyperplane transform final Plane oPlane = (Plane) original; final Plane tPlane = (Plane) transformed; final Vector3D p00 = oPlane.getOrigin(); final Vector3D p10 = oPlane.toSpace(new Vector2D(1.0, 0.0)); final Vector3D p01 = oPlane.toSpace(new Vector2D(0.0, 1.0)); final Vector2D tP00 = tPlane.toSubSpace(apply(p00)); final Vector2D tP10 = tPlane.toSubSpace(apply(p10)); final Vector2D tP01 = tPlane.toSubSpace(apply(p01)); final AffineTransform at = new AffineTransform(tP10.getX() - tP00.getX(), tP10.getY() - tP00.getY(), tP01.getX() - tP00.getX(), tP01.getY() - tP00.getY(), tP00.getX(), tP00.getY()); cachedOriginal = (Plane) original; cachedTransform = org.apache.commons.math3.geometry.euclidean.twod.Line.getTransform(at); } return ((SubLine) sub).applyTransform(cachedTransform); } } /** Translate the region by the specified amount. *

                              The instance is not modified, a new instance is created.

                              * @param translation translation to apply * @return a new instance representing the translated region */ public PolyhedronsSet translate(final Vector3D translation) { return (PolyhedronsSet) applyTransform(new TranslationTransform(translation)); } /** 3D translation as a transform. */ private static class TranslationTransform implements Transform { /** Translation vector. */ private Vector3D translation; /** Cached original hyperplane. */ private Plane cachedOriginal; /** Cached 2D transform valid inside the cached original hyperplane. */ private Transform cachedTransform; /** Build a translation transform. * @param translation translation vector */ public TranslationTransform(final Vector3D translation) { this.translation = translation; } /** {@inheritDoc} */ public Vector3D apply(final Vector point) { return new Vector3D(1.0, (Vector3D) point, 1.0, translation); } /** {@inheritDoc} */ public Plane apply(final Hyperplane hyperplane) { return ((Plane) hyperplane).translate(translation); } /** {@inheritDoc} */ public SubHyperplane apply(final SubHyperplane sub, final Hyperplane original, final Hyperplane transformed) { if (original != cachedOriginal) { // we have changed hyperplane, reset the in-hyperplane transform final Plane oPlane = (Plane) original; final Plane tPlane = (Plane) transformed; final Vector2D shift = tPlane.toSubSpace(apply(oPlane.getOrigin())); final AffineTransform at = AffineTransform.getTranslateInstance(shift.getX(), shift.getY()); cachedOriginal = (Plane) original; cachedTransform = org.apache.commons.math3.geometry.euclidean.twod.Line.getTransform(at); } return ((SubLine) sub).applyTransform(cachedTransform); } } } ././@LongLink100644 0 0 170 12126630647 10260 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/geometry/euclidean/threed/NotARotationMatrixException.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/geometry/euclidean/threed/NotARotationM100644 1750 1750 3343 12126627714 32207 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.geometry.euclidean.threed; import org.apache.commons.math3.exception.MathIllegalArgumentException; import org.apache.commons.math3.exception.util.Localizable; /** * This class represents exceptions thrown while building rotations * from matrices. * * @version $Id: NotARotationMatrixException.java 1416643 2012-12-03 19:37:14Z tn $ * @since 1.2 */ public class NotARotationMatrixException extends MathIllegalArgumentException { /** Serializable version identifier */ private static final long serialVersionUID = 5647178478658937642L; /** * Simple constructor. * Build an exception by translating and formating a message * @param specifier format specifier (to be translated) * @param parts to insert in the format (no translation) * @since 2.2 */ public NotARotationMatrixException(Localizable specifier, Object ... parts) { super(specifier, parts); } } ././@LongLink100644 0 0 155 12126630647 10263 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/geometry/euclidean/threed/OutlineExtractor.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/geometry/euclidean/threed/OutlineExtrac100644 1750 1750 26441 12126627714 32323 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.geometry.euclidean.threed; import java.util.ArrayList; import org.apache.commons.math3.geometry.euclidean.twod.Euclidean2D; import org.apache.commons.math3.geometry.euclidean.twod.PolygonsSet; import org.apache.commons.math3.geometry.euclidean.twod.Vector2D; import org.apache.commons.math3.geometry.partitioning.AbstractSubHyperplane; import org.apache.commons.math3.geometry.partitioning.BSPTree; import org.apache.commons.math3.geometry.partitioning.BSPTreeVisitor; import org.apache.commons.math3.geometry.partitioning.BoundaryAttribute; import org.apache.commons.math3.geometry.partitioning.RegionFactory; import org.apache.commons.math3.geometry.partitioning.SubHyperplane; import org.apache.commons.math3.util.FastMath; /** Extractor for {@link PolygonsSet polyhedrons sets} outlines. *

                              This class extracts the 2D outlines from {{@link PolygonsSet * polyhedrons sets} in a specified projection plane.

                              * @version $Id: OutlineExtractor.java 1416643 2012-12-03 19:37:14Z tn $ * @since 3.0 */ public class OutlineExtractor { /** Abscissa axis of the projection plane. */ private Vector3D u; /** Ordinate axis of the projection plane. */ private Vector3D v; /** Normal of the projection plane (viewing direction). */ private Vector3D w; /** Build an extractor for a specific projection plane. * @param u abscissa axis of the projection point * @param v ordinate axis of the projection point */ public OutlineExtractor(final Vector3D u, final Vector3D v) { this.u = u; this.v = v; w = Vector3D.crossProduct(u, v); } /** Extract the outline of a polyhedrons set. * @param polyhedronsSet polyhedrons set whose outline must be extracted * @return an outline, as an array of loops. */ public Vector2D[][] getOutline(final PolyhedronsSet polyhedronsSet) { // project all boundary facets into one polygons set final BoundaryProjector projector = new BoundaryProjector(); polyhedronsSet.getTree(true).visit(projector); final PolygonsSet projected = projector.getProjected(); // Remove the spurious intermediate vertices from the outline final Vector2D[][] outline = projected.getVertices(); for (int i = 0; i < outline.length; ++i) { final Vector2D[] rawLoop = outline[i]; int end = rawLoop.length; int j = 0; while (j < end) { if (pointIsBetween(rawLoop, end, j)) { // the point should be removed for (int k = j; k < (end - 1); ++k) { rawLoop[k] = rawLoop[k + 1]; } --end; } else { // the point remains in the loop ++j; } } if (end != rawLoop.length) { // resize the array outline[i] = new Vector2D[end]; System.arraycopy(rawLoop, 0, outline[i], 0, end); } } return outline; } /** Check if a point is geometrically between its neighbour in an array. *

                              The neighbours are computed considering the array is a loop * (i.e. point at index (n-1) is before point at index 0)

                              * @param loop points array * @param n number of points to consider in the array * @param i index of the point to check (must be between 0 and n-1) * @return true if the point is exactly between its neighbours */ private boolean pointIsBetween(final Vector2D[] loop, final int n, final int i) { final Vector2D previous = loop[(i + n - 1) % n]; final Vector2D current = loop[i]; final Vector2D next = loop[(i + 1) % n]; final double dx1 = current.getX() - previous.getX(); final double dy1 = current.getY() - previous.getY(); final double dx2 = next.getX() - current.getX(); final double dy2 = next.getY() - current.getY(); final double cross = dx1 * dy2 - dx2 * dy1; final double dot = dx1 * dx2 + dy1 * dy2; final double d1d2 = FastMath.sqrt((dx1 * dx1 + dy1 * dy1) * (dx2 * dx2 + dy2 * dy2)); return (FastMath.abs(cross) <= (1.0e-6 * d1d2)) && (dot >= 0.0); } /** Visitor projecting the boundary facets on a plane. */ private class BoundaryProjector implements BSPTreeVisitor { /** Projection of the polyhedrons set on the plane. */ private PolygonsSet projected; /** Simple constructor. */ public BoundaryProjector() { projected = new PolygonsSet(new BSPTree(Boolean.FALSE)); } /** {@inheritDoc} */ public Order visitOrder(final BSPTree node) { return Order.MINUS_SUB_PLUS; } /** {@inheritDoc} */ public void visitInternalNode(final BSPTree node) { @SuppressWarnings("unchecked") final BoundaryAttribute attribute = (BoundaryAttribute) node.getAttribute(); if (attribute.getPlusOutside() != null) { addContribution(attribute.getPlusOutside(), false); } if (attribute.getPlusInside() != null) { addContribution(attribute.getPlusInside(), true); } } /** {@inheritDoc} */ public void visitLeafNode(final BSPTree node) { } /** Add he contribution of a boundary facet. * @param facet boundary facet * @param reversed if true, the facet has the inside on its plus side */ private void addContribution(final SubHyperplane facet, final boolean reversed) { // extract the vertices of the facet @SuppressWarnings("unchecked") final AbstractSubHyperplane absFacet = (AbstractSubHyperplane) facet; final Plane plane = (Plane) facet.getHyperplane(); final double scal = plane.getNormal().dotProduct(w); if (FastMath.abs(scal) > 1.0e-3) { Vector2D[][] vertices = ((PolygonsSet) absFacet.getRemainingRegion()).getVertices(); if ((scal < 0) ^ reversed) { // the facet is seen from the inside, // we need to invert its boundary orientation final Vector2D[][] newVertices = new Vector2D[vertices.length][]; for (int i = 0; i < vertices.length; ++i) { final Vector2D[] loop = vertices[i]; final Vector2D[] newLoop = new Vector2D[loop.length]; if (loop[0] == null) { newLoop[0] = null; for (int j = 1; j < loop.length; ++j) { newLoop[j] = loop[loop.length - j]; } } else { for (int j = 0; j < loop.length; ++j) { newLoop[j] = loop[loop.length - (j + 1)]; } } newVertices[i] = newLoop; } // use the reverted vertices vertices = newVertices; } // compute the projection of the facet in the outline plane final ArrayList> edges = new ArrayList>(); for (Vector2D[] loop : vertices) { final boolean closed = loop[0] != null; int previous = closed ? (loop.length - 1) : 1; Vector3D previous3D = plane.toSpace(loop[previous]); int current = (previous + 1) % loop.length; Vector2D pPoint = new Vector2D(previous3D.dotProduct(u), previous3D.dotProduct(v)); while (current < loop.length) { final Vector3D current3D = plane.toSpace(loop[current]); final Vector2D cPoint = new Vector2D(current3D.dotProduct(u), current3D.dotProduct(v)); final org.apache.commons.math3.geometry.euclidean.twod.Line line = new org.apache.commons.math3.geometry.euclidean.twod.Line(pPoint, cPoint); SubHyperplane edge = line.wholeHyperplane(); if (closed || (previous != 1)) { // the previous point is a real vertex // it defines one bounding point of the edge final double angle = line.getAngle() + 0.5 * FastMath.PI; final org.apache.commons.math3.geometry.euclidean.twod.Line l = new org.apache.commons.math3.geometry.euclidean.twod.Line(pPoint, angle); edge = edge.split(l).getPlus(); } if (closed || (current != (loop.length - 1))) { // the current point is a real vertex // it defines one bounding point of the edge final double angle = line.getAngle() + 0.5 * FastMath.PI; final org.apache.commons.math3.geometry.euclidean.twod.Line l = new org.apache.commons.math3.geometry.euclidean.twod.Line(cPoint, angle); edge = edge.split(l).getMinus(); } edges.add(edge); previous = current++; previous3D = current3D; pPoint = cPoint; } } final PolygonsSet projectedFacet = new PolygonsSet(edges); // add the contribution of the facet to the global outline projected = (PolygonsSet) new RegionFactory().union(projected, projectedFacet); } } /** Get the projection of the polyhedrons set on the plane. * @return projection of the polyhedrons set on the plane */ public PolygonsSet getProjected() { return projected; } } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/geometry/euclidean/threed/Segment.java100644 1750 1750 3764 12126627714 32042 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.geometry.euclidean.threed; /** Simple container for a two-points segment. * @version $Id: Segment.java 1416643 2012-12-03 19:37:14Z tn $ * @since 3.0 */ public class Segment { /** Start point of the segment. */ private final Vector3D start; /** End point of the segments. */ private final Vector3D end; /** Line containing the segment. */ private final Line line; /** Build a segment. * @param start start point of the segment * @param end end point of the segment * @param line line containing the segment */ public Segment(final Vector3D start, final Vector3D end, final Line line) { this.start = start; this.end = end; this.line = line; } /** Get the start point of the segment. * @return start point of the segment */ public Vector3D getStart() { return start; } /** Get the end point of the segment. * @return end point of the segment */ public Vector3D getEnd() { return end; } /** Get the line containing the segment. * @return line containing the segment */ public Line getLine() { return line; } } ././@LongLink100644 0 0 152 12126630647 10260 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/geometry/euclidean/threed/FieldRotation.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/geometry/euclidean/threed/FieldRotation100644 1750 1750 165351 12126627714 32324 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.geometry.euclidean.threed; import java.io.Serializable; import org.apache.commons.math3.RealFieldElement; import org.apache.commons.math3.Field; import org.apache.commons.math3.exception.MathArithmeticException; import org.apache.commons.math3.exception.MathIllegalArgumentException; import org.apache.commons.math3.exception.util.LocalizedFormats; import org.apache.commons.math3.util.FastMath; import org.apache.commons.math3.util.MathArrays; /** * This class is a re-implementation of {@link Rotation} using {@link RealFieldElement}. *

                              Instance of this class are guaranteed to be immutable.

                              * * @param the type of the field elements * @version $Id: FieldRotation.java 1454903 2013-03-10 19:44:31Z luc $ * @see FieldVector3D * @see RotationOrder * @since 3.2 */ public class FieldRotation> implements Serializable { /** Serializable version identifier */ private static final long serialVersionUID = 20130224l; /** Scalar coordinate of the quaternion. */ private final T q0; /** First coordinate of the vectorial part of the quaternion. */ private final T q1; /** Second coordinate of the vectorial part of the quaternion. */ private final T q2; /** Third coordinate of the vectorial part of the quaternion. */ private final T q3; /** Build a rotation from the quaternion coordinates. *

                              A rotation can be built from a normalized quaternion, * i.e. a quaternion for which q02 + * q12 + q22 + * q32 = 1. If the quaternion is not normalized, * the constructor can normalize it in a preprocessing step.

                              *

                              Note that some conventions put the scalar part of the quaternion * as the 4th component and the vector part as the first three * components. This is not our convention. We put the scalar part * as the first component.

                              * @param q0 scalar part of the quaternion * @param q1 first coordinate of the vectorial part of the quaternion * @param q2 second coordinate of the vectorial part of the quaternion * @param q3 third coordinate of the vectorial part of the quaternion * @param needsNormalization if true, the coordinates are considered * not to be normalized, a normalization preprocessing step is performed * before using them */ public FieldRotation(final T q0, final T q1, final T q2, final T q3, final boolean needsNormalization) { if (needsNormalization) { // normalization preprocessing final T inv = q0.multiply(q0).add(q1.multiply(q1)).add(q2.multiply(q2)).add(q3.multiply(q3)).sqrt().reciprocal(); this.q0 = inv.multiply(q0); this.q1 = inv.multiply(q1); this.q2 = inv.multiply(q2); this.q3 = inv.multiply(q3); } else { this.q0 = q0; this.q1 = q1; this.q2 = q2; this.q3 = q3; } } /** Build a rotation from an axis and an angle. *

                              We use the convention that angles are oriented according to * the effect of the rotation on vectors around the axis. That means * that if (i, j, k) is a direct frame and if we first provide +k as * the axis and π/2 as the angle to this constructor, and then * {@link #applyTo(FieldVector3D) apply} the instance to +i, we will get * +j.

                              *

                              Another way to represent our convention is to say that a rotation * of angle θ about the unit vector (x, y, z) is the same as the * rotation build from quaternion components { cos(-θ/2), * x * sin(-θ/2), y * sin(-θ/2), z * sin(-θ/2) }. * Note the minus sign on the angle!

                              *

                              On the one hand this convention is consistent with a vectorial * perspective (moving vectors in fixed frames), on the other hand it * is different from conventions with a frame perspective (fixed vectors * viewed from different frames) like the ones used for example in spacecraft * attitude community or in the graphics community.

                              * @param axis axis around which to rotate * @param angle rotation angle. * @exception MathIllegalArgumentException if the axis norm is zero */ public FieldRotation(final FieldVector3D axis, final T angle) throws MathIllegalArgumentException { final T norm = axis.getNorm(); if (norm.getReal() == 0) { throw new MathIllegalArgumentException(LocalizedFormats.ZERO_NORM_FOR_ROTATION_AXIS); } final T halfAngle = angle.multiply(-0.5); final T coeff = halfAngle.sin().divide(norm); q0 = halfAngle.cos(); q1 = coeff.multiply(axis.getX()); q2 = coeff.multiply(axis.getY()); q3 = coeff.multiply(axis.getZ()); } /** Build a rotation from a 3X3 matrix. *

                              Rotation matrices are orthogonal matrices, i.e. unit matrices * (which are matrices for which m.mT = I) with real * coefficients. The module of the determinant of unit matrices is * 1, among the orthogonal 3X3 matrices, only the ones having a * positive determinant (+1) are rotation matrices.

                              *

                              When a rotation is defined by a matrix with truncated values * (typically when it is extracted from a technical sheet where only * four to five significant digits are available), the matrix is not * orthogonal anymore. This constructor handles this case * transparently by using a copy of the given matrix and applying a * correction to the copy in order to perfect its orthogonality. If * the Frobenius norm of the correction needed is above the given * threshold, then the matrix is considered to be too far from a * true rotation matrix and an exception is thrown.

                              * @param m rotation matrix * @param threshold convergence threshold for the iterative * orthogonality correction (convergence is reached when the * difference between two steps of the Frobenius norm of the * correction is below this threshold) * @exception NotARotationMatrixException if the matrix is not a 3X3 * matrix, or if it cannot be transformed into an orthogonal matrix * with the given threshold, or if the determinant of the resulting * orthogonal matrix is negative */ public FieldRotation(final T[][] m, final double threshold) throws NotARotationMatrixException { // dimension check if ((m.length != 3) || (m[0].length != 3) || (m[1].length != 3) || (m[2].length != 3)) { throw new NotARotationMatrixException( LocalizedFormats.ROTATION_MATRIX_DIMENSIONS, m.length, m[0].length); } // compute a "close" orthogonal matrix final T[][] ort = orthogonalizeMatrix(m, threshold); // check the sign of the determinant final T d0 = ort[1][1].multiply(ort[2][2]).subtract(ort[2][1].multiply(ort[1][2])); final T d1 = ort[0][1].multiply(ort[2][2]).subtract(ort[2][1].multiply(ort[0][2])); final T d2 = ort[0][1].multiply(ort[1][2]).subtract(ort[1][1].multiply(ort[0][2])); final T det = ort[0][0].multiply(d0).subtract(ort[1][0].multiply(d1)).add(ort[2][0].multiply(d2)); if (det.getReal() < 0.0) { throw new NotARotationMatrixException( LocalizedFormats.CLOSEST_ORTHOGONAL_MATRIX_HAS_NEGATIVE_DETERMINANT, det); } final T[] quat = mat2quat(ort); q0 = quat[0]; q1 = quat[1]; q2 = quat[2]; q3 = quat[3]; } /** Build the rotation that transforms a pair of vector into another pair. *

                              Except for possible scale factors, if the instance were applied to * the pair (u1, u2) it will produce the pair * (v1, v2).

                              *

                              If the angular separation between u1 and u2 is * not the same as the angular separation between v1 and * v2, then a corrected v'2 will be used rather than * v2, the corrected vector will be in the (v1, * v2) plane.

                              * @param u1 first vector of the origin pair * @param u2 second vector of the origin pair * @param v1 desired image of u1 by the rotation * @param v2 desired image of u2 by the rotation * @exception MathArithmeticException if the norm of one of the vectors is zero, * or if one of the pair is degenerated (i.e. the vectors of the pair are colinear) */ public FieldRotation(FieldVector3D u1, FieldVector3D u2, FieldVector3D v1, FieldVector3D v2) throws MathArithmeticException { // build orthonormalized base from u1, u2 // this fails when vectors are null or colinear, which is forbidden to define a rotation final FieldVector3D u3 = FieldVector3D.crossProduct(u1, u2).normalize(); u2 = FieldVector3D.crossProduct(u3, u1).normalize(); u1 = u1.normalize(); // build an orthonormalized base from v1, v2 // this fails when vectors are null or colinear, which is forbidden to define a rotation final FieldVector3D v3 = FieldVector3D.crossProduct(v1, v2).normalize(); v2 = FieldVector3D.crossProduct(v3, v1).normalize(); v1 = v1.normalize(); // buid a matrix transforming the first base into the second one final T[][] array = MathArrays.buildArray(u1.getX().getField(), 3, 3); array[0][0] = u1.getX().multiply(v1.getX()).add(u2.getX().multiply(v2.getX())).add(u3.getX().multiply(v3.getX())); array[0][1] = u1.getY().multiply(v1.getX()).add(u2.getY().multiply(v2.getX())).add(u3.getY().multiply(v3.getX())); array[0][2] = u1.getZ().multiply(v1.getX()).add(u2.getZ().multiply(v2.getX())).add(u3.getZ().multiply(v3.getX())); array[1][0] = u1.getX().multiply(v1.getY()).add(u2.getX().multiply(v2.getY())).add(u3.getX().multiply(v3.getY())); array[1][1] = u1.getY().multiply(v1.getY()).add(u2.getY().multiply(v2.getY())).add(u3.getY().multiply(v3.getY())); array[1][2] = u1.getZ().multiply(v1.getY()).add(u2.getZ().multiply(v2.getY())).add(u3.getZ().multiply(v3.getY())); array[2][0] = u1.getX().multiply(v1.getZ()).add(u2.getX().multiply(v2.getZ())).add(u3.getX().multiply(v3.getZ())); array[2][1] = u1.getY().multiply(v1.getZ()).add(u2.getY().multiply(v2.getZ())).add(u3.getY().multiply(v3.getZ())); array[2][2] = u1.getZ().multiply(v1.getZ()).add(u2.getZ().multiply(v2.getZ())).add(u3.getZ().multiply(v3.getZ())); T[] quat = mat2quat(array); q0 = quat[0]; q1 = quat[1]; q2 = quat[2]; q3 = quat[3]; } /** Build one of the rotations that transform one vector into another one. *

                              Except for a possible scale factor, if the instance were * applied to the vector u it will produce the vector v. There is an * infinite number of such rotations, this constructor choose the * one with the smallest associated angle (i.e. the one whose axis * is orthogonal to the (u, v) plane). If u and v are colinear, an * arbitrary rotation axis is chosen.

                              * @param u origin vector * @param v desired image of u by the rotation * @exception MathArithmeticException if the norm of one of the vectors is zero */ public FieldRotation(final FieldVector3D u, final FieldVector3D v) throws MathArithmeticException { final T normProduct = u.getNorm().multiply(v.getNorm()); if (normProduct.getReal() == 0) { throw new MathArithmeticException(LocalizedFormats.ZERO_NORM_FOR_ROTATION_DEFINING_VECTOR); } final T dot = FieldVector3D.dotProduct(u, v); if (dot.getReal() < ((2.0e-15 - 1.0) * normProduct.getReal())) { // special case u = -v: we select a PI angle rotation around // an arbitrary vector orthogonal to u final FieldVector3D w = u.orthogonal(); q0 = normProduct.getField().getZero(); q1 = w.getX().negate(); q2 = w.getY().negate(); q3 = w.getZ().negate(); } else { // general case: (u, v) defines a plane, we select // the shortest possible rotation: axis orthogonal to this plane q0 = dot.divide(normProduct).add(1.0).multiply(0.5).sqrt(); final T coeff = q0.multiply(normProduct).multiply(2.0).reciprocal(); final FieldVector3D q = FieldVector3D.crossProduct(v, u); q1 = coeff.multiply(q.getX()); q2 = coeff.multiply(q.getY()); q3 = coeff.multiply(q.getZ()); } } /** Build a rotation from three Cardan or Euler elementary rotations. *

                              Cardan rotations are three successive rotations around the * canonical axes X, Y and Z, each axis being used once. There are * 6 such sets of rotations (XYZ, XZY, YXZ, YZX, ZXY and ZYX). Euler * rotations are three successive rotations around the canonical * axes X, Y and Z, the first and last rotations being around the * same axis. There are 6 such sets of rotations (XYX, XZX, YXY, * YZY, ZXZ and ZYZ), the most popular one being ZXZ.

                              *

                              Beware that many people routinely use the term Euler angles even * for what really are Cardan angles (this confusion is especially * widespread in the aerospace business where Roll, Pitch and Yaw angles * are often wrongly tagged as Euler angles).

                              * @param order order of rotations to use * @param alpha1 angle of the first elementary rotation * @param alpha2 angle of the second elementary rotation * @param alpha3 angle of the third elementary rotation */ public FieldRotation(final RotationOrder order, final T alpha1, final T alpha2, final T alpha3) { final T one = alpha1.getField().getOne(); final FieldRotation r1 = new FieldRotation(new FieldVector3D(one, order.getA1()), alpha1); final FieldRotation r2 = new FieldRotation(new FieldVector3D(one, order.getA2()), alpha2); final FieldRotation r3 = new FieldRotation(new FieldVector3D(one, order.getA3()), alpha3); final FieldRotation composed = r1.applyTo(r2.applyTo(r3)); q0 = composed.q0; q1 = composed.q1; q2 = composed.q2; q3 = composed.q3; } /** Convert an orthogonal rotation matrix to a quaternion. * @param ort orthogonal rotation matrix * @return quaternion corresponding to the matrix */ private T[] mat2quat(final T[][] ort) { final T[] quat = MathArrays.buildArray(ort[0][0].getField(), 4); // There are different ways to compute the quaternions elements // from the matrix. They all involve computing one element from // the diagonal of the matrix, and computing the three other ones // using a formula involving a division by the first element, // which unfortunately can be zero. Since the norm of the // quaternion is 1, we know at least one element has an absolute // value greater or equal to 0.5, so it is always possible to // select the right formula and avoid division by zero and even // numerical inaccuracy. Checking the elements in turn and using // the first one greater than 0.45 is safe (this leads to a simple // test since qi = 0.45 implies 4 qi^2 - 1 = -0.19) T s = ort[0][0].add(ort[1][1]).add(ort[2][2]); if (s.getReal() > -0.19) { // compute q0 and deduce q1, q2 and q3 quat[0] = s.add(1.0).sqrt().multiply(0.5); T inv = quat[0].reciprocal().multiply(0.25); quat[1] = inv.multiply(ort[1][2].subtract(ort[2][1])); quat[2] = inv.multiply(ort[2][0].subtract(ort[0][2])); quat[3] = inv.multiply(ort[0][1].subtract(ort[1][0])); } else { s = ort[0][0].subtract(ort[1][1]).subtract(ort[2][2]); if (s.getReal() > -0.19) { // compute q1 and deduce q0, q2 and q3 quat[1] = s.add(1.0).sqrt().multiply(0.5); T inv = quat[1].reciprocal().multiply(0.25); quat[0] = inv.multiply(ort[1][2].subtract(ort[2][1])); quat[2] = inv.multiply(ort[0][1].add(ort[1][0])); quat[3] = inv.multiply(ort[0][2].add(ort[2][0])); } else { s = ort[1][1].subtract(ort[0][0]).subtract(ort[2][2]); if (s.getReal() > -0.19) { // compute q2 and deduce q0, q1 and q3 quat[2] = s.add(1.0).sqrt().multiply(0.5); T inv = quat[2].reciprocal().multiply(0.25); quat[0] = inv.multiply(ort[2][0].subtract(ort[0][2])); quat[1] = inv.multiply(ort[0][1].add(ort[1][0])); quat[3] = inv.multiply(ort[2][1].add(ort[1][2])); } else { // compute q3 and deduce q0, q1 and q2 s = ort[2][2].subtract(ort[0][0]).subtract(ort[1][1]); quat[3] = s.add(1.0).sqrt().multiply(0.5); T inv = quat[3].reciprocal().multiply(0.25); quat[0] = inv.multiply(ort[0][1].subtract(ort[1][0])); quat[1] = inv.multiply(ort[0][2].add(ort[2][0])); quat[2] = inv.multiply(ort[2][1].add(ort[1][2])); } } } return quat; } /** Revert a rotation. * Build a rotation which reverse the effect of another * rotation. This means that if r(u) = v, then r.revert(v) = u. The * instance is not changed. * @return a new rotation whose effect is the reverse of the effect * of the instance */ public FieldRotation revert() { return new FieldRotation(q0.negate(), q1, q2, q3, false); } /** Get the scalar coordinate of the quaternion. * @return scalar coordinate of the quaternion */ public T getQ0() { return q0; } /** Get the first coordinate of the vectorial part of the quaternion. * @return first coordinate of the vectorial part of the quaternion */ public T getQ1() { return q1; } /** Get the second coordinate of the vectorial part of the quaternion. * @return second coordinate of the vectorial part of the quaternion */ public T getQ2() { return q2; } /** Get the third coordinate of the vectorial part of the quaternion. * @return third coordinate of the vectorial part of the quaternion */ public T getQ3() { return q3; } /** Get the normalized axis of the rotation. * @return normalized axis of the rotation * @see #FieldRotation(FieldVector3D, RealFieldElement) */ public FieldVector3D getAxis() { final T squaredSine = q1.multiply(q1).add(q2.multiply(q2)).add(q3.multiply(q3)); if (squaredSine.getReal() == 0) { final Field field = squaredSine.getField(); return new FieldVector3D(field.getOne(), field.getZero(), field.getZero()); } else if (q0.getReal() < 0) { T inverse = squaredSine.sqrt().reciprocal(); return new FieldVector3D(q1.multiply(inverse), q2.multiply(inverse), q3.multiply(inverse)); } final T inverse = squaredSine.sqrt().reciprocal().negate(); return new FieldVector3D(q1.multiply(inverse), q2.multiply(inverse), q3.multiply(inverse)); } /** Get the angle of the rotation. * @return angle of the rotation (between 0 and π) * @see #FieldRotation(FieldVector3D, RealFieldElement) */ public T getAngle() { if ((q0.getReal() < -0.1) || (q0.getReal() > 0.1)) { return q1.multiply(q1).add(q2.multiply(q2)).add(q3.multiply(q3)).sqrt().asin().multiply(2); } else if (q0.getReal() < 0) { return q0.negate().acos().multiply(2); } return q0.acos().multiply(2); } /** Get the Cardan or Euler angles corresponding to the instance. *

                              The equations show that each rotation can be defined by two * different values of the Cardan or Euler angles set. For example * if Cardan angles are used, the rotation defined by the angles * a1, a2 and a3 is the same as * the rotation defined by the angles π + a1, π * - a2 and π + a3. This method implements * the following arbitrary choices:

                              *
                                *
                              • for Cardan angles, the chosen set is the one for which the * second angle is between -π/2 and π/2 (i.e its cosine is * positive),
                              • *
                              • for Euler angles, the chosen set is the one for which the * second angle is between 0 and π (i.e its sine is positive).
                              • *
                              *

                              Cardan and Euler angle have a very disappointing drawback: all * of them have singularities. This means that if the instance is * too close to the singularities corresponding to the given * rotation order, it will be impossible to retrieve the angles. For * Cardan angles, this is often called gimbal lock. There is * nothing to do to prevent this, it is an intrinsic problem * with Cardan and Euler representation (but not a problem with the * rotation itself, which is perfectly well defined). For Cardan * angles, singularities occur when the second angle is close to * -π/2 or +π/2, for Euler angle singularities occur when the * second angle is close to 0 or π, this implies that the identity * rotation is always singular for Euler angles!

                              * @param order rotation order to use * @return an array of three angles, in the order specified by the set * @exception CardanEulerSingularityException if the rotation is * singular with respect to the angles set specified */ public T[] getAngles(final RotationOrder order) throws CardanEulerSingularityException { if (order == RotationOrder.XYZ) { // r (+K) coordinates are : // sin (theta), -cos (theta) sin (phi), cos (theta) cos (phi) // (-r) (+I) coordinates are : // cos (psi) cos (theta), -sin (psi) cos (theta), sin (theta) final // and we can choose to have theta in the interval [-PI/2 ; +PI/2] FieldVector3D v1 = applyTo(vector(0, 0, 1)); final FieldVector3D v2 = applyInverseTo(vector(1, 0, 0)); if ((v2.getZ().getReal() < -0.9999999999) || (v2.getZ().getReal() > 0.9999999999)) { throw new CardanEulerSingularityException(true); } return buildArray(v1.getY().negate().atan2(v1.getZ()), v2.getZ().asin(), v2.getY().negate().atan2(v2.getX())); } else if (order == RotationOrder.XZY) { // r (+J) coordinates are : // -sin (psi), cos (psi) cos (phi), cos (psi) sin (phi) // (-r) (+I) coordinates are : // cos (theta) cos (psi), -sin (psi), sin (theta) cos (psi) // and we can choose to have psi in the interval [-PI/2 ; +PI/2] final FieldVector3D v1 = applyTo(vector(0, 1, 0)); final FieldVector3D v2 = applyInverseTo(vector(1, 0, 0)); if ((v2.getY().getReal() < -0.9999999999) || (v2.getY().getReal() > 0.9999999999)) { throw new CardanEulerSingularityException(true); } return buildArray(v1.getZ().atan2(v1.getY()), v2.getY().asin().negate(), v2.getZ().atan2(v2.getX())); } else if (order == RotationOrder.YXZ) { // r (+K) coordinates are : // cos (phi) sin (theta), -sin (phi), cos (phi) cos (theta) // (-r) (+J) coordinates are : // sin (psi) cos (phi), cos (psi) cos (phi), -sin (phi) // and we can choose to have phi in the interval [-PI/2 ; +PI/2] final FieldVector3D v1 = applyTo(vector(0, 0, 1)); final FieldVector3D v2 = applyInverseTo(vector(0, 1, 0)); if ((v2.getZ().getReal() < -0.9999999999) || (v2.getZ().getReal() > 0.9999999999)) { throw new CardanEulerSingularityException(true); } return buildArray(v1.getX().atan2(v1.getZ()), v2.getZ().asin().negate(), v2.getX().atan2(v2.getY())); } else if (order == RotationOrder.YZX) { // r (+I) coordinates are : // cos (psi) cos (theta), sin (psi), -cos (psi) sin (theta) // (-r) (+J) coordinates are : // sin (psi), cos (phi) cos (psi), -sin (phi) cos (psi) // and we can choose to have psi in the interval [-PI/2 ; +PI/2] final FieldVector3D v1 = applyTo(vector(1, 0, 0)); final FieldVector3D v2 = applyInverseTo(vector(0, 1, 0)); if ((v2.getX().getReal() < -0.9999999999) || (v2.getX().getReal() > 0.9999999999)) { throw new CardanEulerSingularityException(true); } return buildArray(v1.getZ().negate().atan2(v1.getX()), v2.getX().asin(), v2.getZ().negate().atan2(v2.getY())); } else if (order == RotationOrder.ZXY) { // r (+J) coordinates are : // -cos (phi) sin (psi), cos (phi) cos (psi), sin (phi) // (-r) (+K) coordinates are : // -sin (theta) cos (phi), sin (phi), cos (theta) cos (phi) // and we can choose to have phi in the interval [-PI/2 ; +PI/2] final FieldVector3D v1 = applyTo(vector(0, 1, 0)); final FieldVector3D v2 = applyInverseTo(vector(0, 0, 1)); if ((v2.getY().getReal() < -0.9999999999) || (v2.getY().getReal() > 0.9999999999)) { throw new CardanEulerSingularityException(true); } return buildArray(v1.getX().negate().atan2(v1.getY()), v2.getY().asin(), v2.getX().negate().atan2(v2.getZ())); } else if (order == RotationOrder.ZYX) { // r (+I) coordinates are : // cos (theta) cos (psi), cos (theta) sin (psi), -sin (theta) // (-r) (+K) coordinates are : // -sin (theta), sin (phi) cos (theta), cos (phi) cos (theta) // and we can choose to have theta in the interval [-PI/2 ; +PI/2] final FieldVector3D v1 = applyTo(vector(1, 0, 0)); final FieldVector3D v2 = applyInverseTo(vector(0, 0, 1)); if ((v2.getX().getReal() < -0.9999999999) || (v2.getX().getReal() > 0.9999999999)) { throw new CardanEulerSingularityException(true); } return buildArray(v1.getY().atan2(v1.getX()), v2.getX().asin().negate(), v2.getY().atan2(v2.getZ())); } else if (order == RotationOrder.XYX) { // r (+I) coordinates are : // cos (theta), sin (phi1) sin (theta), -cos (phi1) sin (theta) // (-r) (+I) coordinates are : // cos (theta), sin (theta) sin (phi2), sin (theta) cos (phi2) // and we can choose to have theta in the interval [0 ; PI] final FieldVector3D v1 = applyTo(vector(1, 0, 0)); final FieldVector3D v2 = applyInverseTo(vector(1, 0, 0)); if ((v2.getX().getReal() < -0.9999999999) || (v2.getX().getReal() > 0.9999999999)) { throw new CardanEulerSingularityException(false); } return buildArray(v1.getY().atan2(v1.getZ().negate()), v2.getX().acos(), v2.getY().atan2(v2.getZ())); } else if (order == RotationOrder.XZX) { // r (+I) coordinates are : // cos (psi), cos (phi1) sin (psi), sin (phi1) sin (psi) // (-r) (+I) coordinates are : // cos (psi), -sin (psi) cos (phi2), sin (psi) sin (phi2) // and we can choose to have psi in the interval [0 ; PI] final FieldVector3D v1 = applyTo(vector(1, 0, 0)); final FieldVector3D v2 = applyInverseTo(vector(1, 0, 0)); if ((v2.getX().getReal() < -0.9999999999) || (v2.getX().getReal() > 0.9999999999)) { throw new CardanEulerSingularityException(false); } return buildArray(v1.getZ().atan2(v1.getY()), v2.getX().acos(), v2.getZ().atan2(v2.getY().negate())); } else if (order == RotationOrder.YXY) { // r (+J) coordinates are : // sin (theta1) sin (phi), cos (phi), cos (theta1) sin (phi) // (-r) (+J) coordinates are : // sin (phi) sin (theta2), cos (phi), -sin (phi) cos (theta2) // and we can choose to have phi in the interval [0 ; PI] final FieldVector3D v1 = applyTo(vector(0, 1, 0)); final FieldVector3D v2 = applyInverseTo(vector(0, 1, 0)); if ((v2.getY().getReal() < -0.9999999999) || (v2.getY().getReal() > 0.9999999999)) { throw new CardanEulerSingularityException(false); } return buildArray(v1.getX().atan2(v1.getZ()), v2.getY().acos(), v2.getX().atan2(v2.getZ().negate())); } else if (order == RotationOrder.YZY) { // r (+J) coordinates are : // -cos (theta1) sin (psi), cos (psi), sin (theta1) sin (psi) // (-r) (+J) coordinates are : // sin (psi) cos (theta2), cos (psi), sin (psi) sin (theta2) // and we can choose to have psi in the interval [0 ; PI] final FieldVector3D v1 = applyTo(vector(0, 1, 0)); final FieldVector3D v2 = applyInverseTo(vector(0, 1, 0)); if ((v2.getY().getReal() < -0.9999999999) || (v2.getY().getReal() > 0.9999999999)) { throw new CardanEulerSingularityException(false); } return buildArray(v1.getZ().atan2(v1.getX().negate()), v2.getY().acos(), v2.getZ().atan2(v2.getX())); } else if (order == RotationOrder.ZXZ) { // r (+K) coordinates are : // sin (psi1) sin (phi), -cos (psi1) sin (phi), cos (phi) // (-r) (+K) coordinates are : // sin (phi) sin (psi2), sin (phi) cos (psi2), cos (phi) // and we can choose to have phi in the interval [0 ; PI] final FieldVector3D v1 = applyTo(vector(0, 0, 1)); final FieldVector3D v2 = applyInverseTo(vector(0, 0, 1)); if ((v2.getZ().getReal() < -0.9999999999) || (v2.getZ().getReal() > 0.9999999999)) { throw new CardanEulerSingularityException(false); } return buildArray(v1.getX().atan2(v1.getY().negate()), v2.getZ().acos(), v2.getX().atan2(v2.getY())); } else { // last possibility is ZYZ // r (+K) coordinates are : // cos (psi1) sin (theta), sin (psi1) sin (theta), cos (theta) // (-r) (+K) coordinates are : // -sin (theta) cos (psi2), sin (theta) sin (psi2), cos (theta) // and we can choose to have theta in the interval [0 ; PI] final FieldVector3D v1 = applyTo(vector(0, 0, 1)); final FieldVector3D v2 = applyInverseTo(vector(0, 0, 1)); if ((v2.getZ().getReal() < -0.9999999999) || (v2.getZ().getReal() > 0.9999999999)) { throw new CardanEulerSingularityException(false); } return buildArray(v1.getY().atan2(v1.getX()), v2.getZ().acos(), v2.getY().atan2(v2.getX().negate())); } } /** Create a dimension 3 array. * @param a0 first array element * @param a1 second array element * @param a2 third array element * @return new array */ private T[] buildArray(final T a0, final T a1, final T a2) { final T[] array = MathArrays.buildArray(a0.getField(), 3); array[0] = a0; array[1] = a1; array[2] = a2; return array; } /** Create a constant vector. * @param x abscissa * @param y ordinate * @param z height * @return a constant vector */ private FieldVector3D vector(final double x, final double y, final double z) { final T zero = q0.getField().getZero(); return new FieldVector3D(zero.add(x), zero.add(y), zero.add(z)); } /** Get the 3X3 matrix corresponding to the instance * @return the matrix corresponding to the instance */ public T[][] getMatrix() { // products final T q0q0 = q0.multiply(q0); final T q0q1 = q0.multiply(q1); final T q0q2 = q0.multiply(q2); final T q0q3 = q0.multiply(q3); final T q1q1 = q1.multiply(q1); final T q1q2 = q1.multiply(q2); final T q1q3 = q1.multiply(q3); final T q2q2 = q2.multiply(q2); final T q2q3 = q2.multiply(q3); final T q3q3 = q3.multiply(q3); // create the matrix final T[][] m = MathArrays.buildArray(q0.getField(), 3, 3); m [0][0] = q0q0.add(q1q1).multiply(2).subtract(1); m [1][0] = q1q2.subtract(q0q3).multiply(2); m [2][0] = q1q3.add(q0q2).multiply(2); m [0][1] = q1q2.add(q0q3).multiply(2); m [1][1] = q0q0.add(q2q2).multiply(2).subtract(1); m [2][1] = q2q3.subtract(q0q1).multiply(2); m [0][2] = q1q3.subtract(q0q2).multiply(2); m [1][2] = q2q3.add(q0q1).multiply(2); m [2][2] = q0q0.add(q3q3).multiply(2).subtract(1); return m; } /** Convert to a constant vector without derivatives. * @return a constant vector */ public Rotation toRotation() { return new Rotation(q0.getReal(), q1.getReal(), q2.getReal(), q3.getReal(), false); } /** Apply the rotation to a vector. * @param u vector to apply the rotation to * @return a new vector which is the image of u by the rotation */ public FieldVector3D applyTo(final FieldVector3D u) { final T x = u.getX(); final T y = u.getY(); final T z = u.getZ(); final T s = q1.multiply(x).add(q2.multiply(y)).add(q3.multiply(z)); return new FieldVector3D(q0.multiply(x.multiply(q0).subtract(q2.multiply(z).subtract(q3.multiply(y)))).add(s.multiply(q1)).multiply(2).subtract(x), q0.multiply(y.multiply(q0).subtract(q3.multiply(x).subtract(q1.multiply(z)))).add(s.multiply(q2)).multiply(2).subtract(y), q0.multiply(z.multiply(q0).subtract(q1.multiply(y).subtract(q2.multiply(x)))).add(s.multiply(q3)).multiply(2).subtract(z)); } /** Apply the rotation to a vector. * @param u vector to apply the rotation to * @return a new vector which is the image of u by the rotation */ public FieldVector3D applyTo(final Vector3D u) { final double x = u.getX(); final double y = u.getY(); final double z = u.getZ(); final T s = q1.multiply(x).add(q2.multiply(y)).add(q3.multiply(z)); return new FieldVector3D(q0.multiply(q0.multiply(x).subtract(q2.multiply(z).subtract(q3.multiply(y)))).add(s.multiply(q1)).multiply(2).subtract(x), q0.multiply(q0.multiply(y).subtract(q3.multiply(x).subtract(q1.multiply(z)))).add(s.multiply(q2)).multiply(2).subtract(y), q0.multiply(q0.multiply(z).subtract(q1.multiply(y).subtract(q2.multiply(x)))).add(s.multiply(q3)).multiply(2).subtract(z)); } /** Apply the rotation to a vector stored in an array. * @param in an array with three items which stores vector to rotate * @param out an array with three items to put result to (it can be the same * array as in) */ public void applyTo(final T[] in, final T[] out) { final T x = in[0]; final T y = in[1]; final T z = in[2]; final T s = q1.multiply(x).add(q2.multiply(y)).add(q3.multiply(z)); out[0] = q0.multiply(x.multiply(q0).subtract(q2.multiply(z).subtract(q3.multiply(y)))).add(s.multiply(q1)).multiply(2).subtract(x); out[1] = q0.multiply(y.multiply(q0).subtract(q3.multiply(x).subtract(q1.multiply(z)))).add(s.multiply(q2)).multiply(2).subtract(y); out[2] = q0.multiply(z.multiply(q0).subtract(q1.multiply(y).subtract(q2.multiply(x)))).add(s.multiply(q3)).multiply(2).subtract(z); } /** Apply the rotation to a vector stored in an array. * @param in an array with three items which stores vector to rotate * @param out an array with three items to put result to */ public void applyTo(final double[] in, final T[] out) { final double x = in[0]; final double y = in[1]; final double z = in[2]; final T s = q1.multiply(x).add(q2.multiply(y)).add(q3.multiply(z)); out[0] = q0.multiply(q0.multiply(x).subtract(q2.multiply(z).subtract(q3.multiply(y)))).add(s.multiply(q1)).multiply(2).subtract(x); out[1] = q0.multiply(q0.multiply(y).subtract(q3.multiply(x).subtract(q1.multiply(z)))).add(s.multiply(q2)).multiply(2).subtract(y); out[2] = q0.multiply(q0.multiply(z).subtract(q1.multiply(y).subtract(q2.multiply(x)))).add(s.multiply(q3)).multiply(2).subtract(z); } /** Apply a rotation to a vector. * @param r rotation to apply * @param u vector to apply the rotation to * @param the type of the field elements * @return a new vector which is the image of u by the rotation */ public static > FieldVector3D applyTo(final Rotation r, final FieldVector3D u) { final T x = u.getX(); final T y = u.getY(); final T z = u.getZ(); final T s = x.multiply(r.getQ1()).add(y.multiply(r.getQ2())).add(z.multiply(r.getQ3())); return new FieldVector3D(x.multiply(r.getQ0()).subtract(z.multiply(r.getQ2()).subtract(y.multiply(r.getQ3()))).multiply(r.getQ0()).add(s.multiply(r.getQ1())).multiply(2).subtract(x), y.multiply(r.getQ0()).subtract(x.multiply(r.getQ3()).subtract(z.multiply(r.getQ1()))).multiply(r.getQ0()).add(s.multiply(r.getQ2())).multiply(2).subtract(y), z.multiply(r.getQ0()).subtract(y.multiply(r.getQ1()).subtract(x.multiply(r.getQ2()))).multiply(r.getQ0()).add(s.multiply(r.getQ3())).multiply(2).subtract(z)); } /** Apply the inverse of the rotation to a vector. * @param u vector to apply the inverse of the rotation to * @return a new vector which such that u is its image by the rotation */ public FieldVector3D applyInverseTo(final FieldVector3D u) { final T x = u.getX(); final T y = u.getY(); final T z = u.getZ(); final T s = q1.multiply(x).add(q2.multiply(y)).add(q3.multiply(z)); final T m0 = q0.negate(); return new FieldVector3D(m0.multiply(x.multiply(m0).subtract(q2.multiply(z).subtract(q3.multiply(y)))).add(s.multiply(q1)).multiply(2).subtract(x), m0.multiply(y.multiply(m0).subtract(q3.multiply(x).subtract(q1.multiply(z)))).add(s.multiply(q2)).multiply(2).subtract(y), m0.multiply(z.multiply(m0).subtract(q1.multiply(y).subtract(q2.multiply(x)))).add(s.multiply(q3)).multiply(2).subtract(z)); } /** Apply the inverse of the rotation to a vector. * @param u vector to apply the inverse of the rotation to * @return a new vector which such that u is its image by the rotation */ public FieldVector3D applyInverseTo(final Vector3D u) { final double x = u.getX(); final double y = u.getY(); final double z = u.getZ(); final T s = q1.multiply(x).add(q2.multiply(y)).add(q3.multiply(z)); final T m0 = q0.negate(); return new FieldVector3D(m0.multiply(m0.multiply(x).subtract(q2.multiply(z).subtract(q3.multiply(y)))).add(s.multiply(q1)).multiply(2).subtract(x), m0.multiply(m0.multiply(y).subtract(q3.multiply(x).subtract(q1.multiply(z)))).add(s.multiply(q2)).multiply(2).subtract(y), m0.multiply(m0.multiply(z).subtract(q1.multiply(y).subtract(q2.multiply(x)))).add(s.multiply(q3)).multiply(2).subtract(z)); } /** Apply the inverse of the rotation to a vector stored in an array. * @param in an array with three items which stores vector to rotate * @param out an array with three items to put result to (it can be the same * array as in) */ public void applyInverseTo(final T[] in, final T[] out) { final T x = in[0]; final T y = in[1]; final T z = in[2]; final T s = q1.multiply(x).add(q2.multiply(y)).add(q3.multiply(z)); final T m0 = q0.negate(); out[0] = m0.multiply(x.multiply(m0).subtract(q2.multiply(z).subtract(q3.multiply(y)))).add(s.multiply(q1)).multiply(2).subtract(x); out[1] = m0.multiply(y.multiply(m0).subtract(q3.multiply(x).subtract(q1.multiply(z)))).add(s.multiply(q2)).multiply(2).subtract(y); out[2] = m0.multiply(z.multiply(m0).subtract(q1.multiply(y).subtract(q2.multiply(x)))).add(s.multiply(q3)).multiply(2).subtract(z); } /** Apply the inverse of the rotation to a vector stored in an array. * @param in an array with three items which stores vector to rotate * @param out an array with three items to put result to */ public void applyInverseTo(final double[] in, final T[] out) { final double x = in[0]; final double y = in[1]; final double z = in[2]; final T s = q1.multiply(x).add(q2.multiply(y)).add(q3.multiply(z)); final T m0 = q0.negate(); out[0] = m0.multiply(m0.multiply(x).subtract(q2.multiply(z).subtract(q3.multiply(y)))).add(s.multiply(q1)).multiply(2).subtract(x); out[1] = m0.multiply(m0.multiply(y).subtract(q3.multiply(x).subtract(q1.multiply(z)))).add(s.multiply(q2)).multiply(2).subtract(y); out[2] = m0.multiply(m0.multiply(z).subtract(q1.multiply(y).subtract(q2.multiply(x)))).add(s.multiply(q3)).multiply(2).subtract(z); } /** Apply the inverse of a rotation to a vector. * @param r rotation to apply * @param u vector to apply the inverse of the rotation to * @param the type of the field elements * @return a new vector which such that u is its image by the rotation */ public static > FieldVector3D applyInverseTo(final Rotation r, final FieldVector3D u) { final T x = u.getX(); final T y = u.getY(); final T z = u.getZ(); final T s = x.multiply(r.getQ1()).add(y.multiply(r.getQ2())).add(z.multiply(r.getQ3())); final double m0 = -r.getQ0(); return new FieldVector3D(x.multiply(m0).subtract(z.multiply(r.getQ2()).subtract(y.multiply(r.getQ3()))).multiply(m0).add(s.multiply(r.getQ1())).multiply(2).subtract(x), y.multiply(m0).subtract(x.multiply(r.getQ3()).subtract(z.multiply(r.getQ1()))).multiply(m0).add(s.multiply(r.getQ2())).multiply(2).subtract(y), z.multiply(m0).subtract(y.multiply(r.getQ1()).subtract(x.multiply(r.getQ2()))).multiply(m0).add(s.multiply(r.getQ3())).multiply(2).subtract(z)); } /** Apply the instance to another rotation. * Applying the instance to a rotation is computing the composition * in an order compliant with the following rule : let u be any * vector and v its image by r (i.e. r.applyTo(u) = v), let w be the image * of v by the instance (i.e. applyTo(v) = w), then w = comp.applyTo(u), * where comp = applyTo(r). * @param r rotation to apply the rotation to * @return a new rotation which is the composition of r by the instance */ public FieldRotation applyTo(final FieldRotation r) { return new FieldRotation(r.q0.multiply(q0).subtract(r.q1.multiply(q1).add(r.q2.multiply(q2)).add(r.q3.multiply(q3))), r.q1.multiply(q0).add(r.q0.multiply(q1)).add(r.q2.multiply(q3).subtract(r.q3.multiply(q2))), r.q2.multiply(q0).add(r.q0.multiply(q2)).add(r.q3.multiply(q1).subtract(r.q1.multiply(q3))), r.q3.multiply(q0).add(r.q0.multiply(q3)).add(r.q1.multiply(q2).subtract(r.q2.multiply(q1))), false); } /** Apply the instance to another rotation. * Applying the instance to a rotation is computing the composition * in an order compliant with the following rule : let u be any * vector and v its image by r (i.e. r.applyTo(u) = v), let w be the image * of v by the instance (i.e. applyTo(v) = w), then w = comp.applyTo(u), * where comp = applyTo(r). * @param r rotation to apply the rotation to * @return a new rotation which is the composition of r by the instance */ public FieldRotation applyTo(final Rotation r) { return new FieldRotation(q0.multiply(r.getQ0()).subtract(q1.multiply(r.getQ1()).add(q2.multiply(r.getQ2())).add(q3.multiply(r.getQ3()))), q0.multiply(r.getQ1()).add(q1.multiply(r.getQ0())).add(q3.multiply(r.getQ2()).subtract(q2.multiply(r.getQ3()))), q0.multiply(r.getQ2()).add(q2.multiply(r.getQ0())).add(q1.multiply(r.getQ3()).subtract(q3.multiply(r.getQ1()))), q0.multiply(r.getQ3()).add(q3.multiply(r.getQ0())).add(q2.multiply(r.getQ1()).subtract(q1.multiply(r.getQ2()))), false); } /** Apply a rotation to another rotation. * Applying a rotation to another rotation is computing the composition * in an order compliant with the following rule : let u be any * vector and v its image by rInner (i.e. rInner.applyTo(u) = v), let w be the image * of v by rOuter (i.e. rOuter.applyTo(v) = w), then w = comp.applyTo(u), * where comp = applyTo(rOuter, rInner). * @param r1 rotation to apply * @param rInner rotation to apply the rotation to * @param the type of the field elements * @return a new rotation which is the composition of r by the instance */ public static > FieldRotation applyTo(final Rotation r1, final FieldRotation rInner) { return new FieldRotation(rInner.q0.multiply(r1.getQ0()).subtract(rInner.q1.multiply(r1.getQ1()).add(rInner.q2.multiply(r1.getQ2())).add(rInner.q3.multiply(r1.getQ3()))), rInner.q1.multiply(r1.getQ0()).add(rInner.q0.multiply(r1.getQ1())).add(rInner.q2.multiply(r1.getQ3()).subtract(rInner.q3.multiply(r1.getQ2()))), rInner.q2.multiply(r1.getQ0()).add(rInner.q0.multiply(r1.getQ2())).add(rInner.q3.multiply(r1.getQ1()).subtract(rInner.q1.multiply(r1.getQ3()))), rInner.q3.multiply(r1.getQ0()).add(rInner.q0.multiply(r1.getQ3())).add(rInner.q1.multiply(r1.getQ2()).subtract(rInner.q2.multiply(r1.getQ1()))), false); } /** Apply the inverse of the instance to another rotation. * Applying the inverse of the instance to a rotation is computing * the composition in an order compliant with the following rule : * let u be any vector and v its image by r (i.e. r.applyTo(u) = v), * let w be the inverse image of v by the instance * (i.e. applyInverseTo(v) = w), then w = comp.applyTo(u), where * comp = applyInverseTo(r). * @param r rotation to apply the rotation to * @return a new rotation which is the composition of r by the inverse * of the instance */ public FieldRotation applyInverseTo(final FieldRotation r) { return new FieldRotation(r.q0.multiply(q0).add(r.q1.multiply(q1).add(r.q2.multiply(q2)).add(r.q3.multiply(q3))).negate(), r.q0.multiply(q1).add(r.q2.multiply(q3).subtract(r.q3.multiply(q2))).subtract(r.q1.multiply(q0)), r.q0.multiply(q2).add(r.q3.multiply(q1).subtract(r.q1.multiply(q3))).subtract(r.q2.multiply(q0)), r.q0.multiply(q3).add(r.q1.multiply(q2).subtract(r.q2.multiply(q1))).subtract(r.q3.multiply(q0)), false); } /** Apply the inverse of the instance to another rotation. * Applying the inverse of the instance to a rotation is computing * the composition in an order compliant with the following rule : * let u be any vector and v its image by r (i.e. r.applyTo(u) = v), * let w be the inverse image of v by the instance * (i.e. applyInverseTo(v) = w), then w = comp.applyTo(u), where * comp = applyInverseTo(r). * @param r rotation to apply the rotation to * @return a new rotation which is the composition of r by the inverse * of the instance */ public FieldRotation applyInverseTo(final Rotation r) { return new FieldRotation(q0.multiply(r.getQ0()).add(q1.multiply(r.getQ1()).add(q2.multiply(r.getQ2())).add(q3.multiply(r.getQ3()))).negate(), q1.multiply(r.getQ0()).add(q3.multiply(r.getQ2()).subtract(q2.multiply(r.getQ3()))).subtract(q0.multiply(r.getQ1())), q2.multiply(r.getQ0()).add(q1.multiply(r.getQ3()).subtract(q3.multiply(r.getQ1()))).subtract(q0.multiply(r.getQ2())), q3.multiply(r.getQ0()).add(q2.multiply(r.getQ1()).subtract(q1.multiply(r.getQ2()))).subtract(q0.multiply(r.getQ3())), false); } /** Apply the inverse of a rotation to another rotation. * Applying the inverse of a rotation to another rotation is computing * the composition in an order compliant with the following rule : * let u be any vector and v its image by rInner (i.e. rInner.applyTo(u) = v), * let w be the inverse image of v by rOuter * (i.e. rOuter.applyInverseTo(v) = w), then w = comp.applyTo(u), where * comp = applyInverseTo(rOuter, rInner). * @param rOuter rotation to apply the rotation to * @param rInner rotation to apply the rotation to * @param the type of the field elements * @return a new rotation which is the composition of r by the inverse * of the instance */ public static > FieldRotation applyInverseTo(final Rotation rOuter, final FieldRotation rInner) { return new FieldRotation(rInner.q0.multiply(rOuter.getQ0()).add(rInner.q1.multiply(rOuter.getQ1()).add(rInner.q2.multiply(rOuter.getQ2())).add(rInner.q3.multiply(rOuter.getQ3()))).negate(), rInner.q0.multiply(rOuter.getQ1()).add(rInner.q2.multiply(rOuter.getQ3()).subtract(rInner.q3.multiply(rOuter.getQ2()))).subtract(rInner.q1.multiply(rOuter.getQ0())), rInner.q0.multiply(rOuter.getQ2()).add(rInner.q3.multiply(rOuter.getQ1()).subtract(rInner.q1.multiply(rOuter.getQ3()))).subtract(rInner.q2.multiply(rOuter.getQ0())), rInner.q0.multiply(rOuter.getQ3()).add(rInner.q1.multiply(rOuter.getQ2()).subtract(rInner.q2.multiply(rOuter.getQ1()))).subtract(rInner.q3.multiply(rOuter.getQ0())), false); } /** Perfect orthogonality on a 3X3 matrix. * @param m initial matrix (not exactly orthogonal) * @param threshold convergence threshold for the iterative * orthogonality correction (convergence is reached when the * difference between two steps of the Frobenius norm of the * correction is below this threshold) * @return an orthogonal matrix close to m * @exception NotARotationMatrixException if the matrix cannot be * orthogonalized with the given threshold after 10 iterations */ private T[][] orthogonalizeMatrix(final T[][] m, final double threshold) throws NotARotationMatrixException { T x00 = m[0][0]; T x01 = m[0][1]; T x02 = m[0][2]; T x10 = m[1][0]; T x11 = m[1][1]; T x12 = m[1][2]; T x20 = m[2][0]; T x21 = m[2][1]; T x22 = m[2][2]; double fn = 0; double fn1; final T[][] o = MathArrays.buildArray(m[0][0].getField(), 3, 3); // iterative correction: Xn+1 = Xn - 0.5 * (Xn.Mt.Xn - M) int i = 0; while (++i < 11) { // Mt.Xn final T mx00 = m[0][0].multiply(x00).add(m[1][0].multiply(x10)).add(m[2][0].multiply(x20)); final T mx10 = m[0][1].multiply(x00).add(m[1][1].multiply(x10)).add(m[2][1].multiply(x20)); final T mx20 = m[0][2].multiply(x00).add(m[1][2].multiply(x10)).add(m[2][2].multiply(x20)); final T mx01 = m[0][0].multiply(x01).add(m[1][0].multiply(x11)).add(m[2][0].multiply(x21)); final T mx11 = m[0][1].multiply(x01).add(m[1][1].multiply(x11)).add(m[2][1].multiply(x21)); final T mx21 = m[0][2].multiply(x01).add(m[1][2].multiply(x11)).add(m[2][2].multiply(x21)); final T mx02 = m[0][0].multiply(x02).add(m[1][0].multiply(x12)).add(m[2][0].multiply(x22)); final T mx12 = m[0][1].multiply(x02).add(m[1][1].multiply(x12)).add(m[2][1].multiply(x22)); final T mx22 = m[0][2].multiply(x02).add(m[1][2].multiply(x12)).add(m[2][2].multiply(x22)); // Xn+1 o[0][0] = x00.subtract(x00.multiply(mx00).add(x01.multiply(mx10)).add(x02.multiply(mx20)).subtract(m[0][0]).multiply(0.5)); o[0][1] = x01.subtract(x00.multiply(mx01).add(x01.multiply(mx11)).add(x02.multiply(mx21)).subtract(m[0][1]).multiply(0.5)); o[0][2] = x02.subtract(x00.multiply(mx02).add(x01.multiply(mx12)).add(x02.multiply(mx22)).subtract(m[0][2]).multiply(0.5)); o[1][0] = x10.subtract(x10.multiply(mx00).add(x11.multiply(mx10)).add(x12.multiply(mx20)).subtract(m[1][0]).multiply(0.5)); o[1][1] = x11.subtract(x10.multiply(mx01).add(x11.multiply(mx11)).add(x12.multiply(mx21)).subtract(m[1][1]).multiply(0.5)); o[1][2] = x12.subtract(x10.multiply(mx02).add(x11.multiply(mx12)).add(x12.multiply(mx22)).subtract(m[1][2]).multiply(0.5)); o[2][0] = x20.subtract(x20.multiply(mx00).add(x21.multiply(mx10)).add(x22.multiply(mx20)).subtract(m[2][0]).multiply(0.5)); o[2][1] = x21.subtract(x20.multiply(mx01).add(x21.multiply(mx11)).add(x22.multiply(mx21)).subtract(m[2][1]).multiply(0.5)); o[2][2] = x22.subtract(x20.multiply(mx02).add(x21.multiply(mx12)).add(x22.multiply(mx22)).subtract(m[2][2]).multiply(0.5)); // correction on each elements final double corr00 = o[0][0].getReal() - m[0][0].getReal(); final double corr01 = o[0][1].getReal() - m[0][1].getReal(); final double corr02 = o[0][2].getReal() - m[0][2].getReal(); final double corr10 = o[1][0].getReal() - m[1][0].getReal(); final double corr11 = o[1][1].getReal() - m[1][1].getReal(); final double corr12 = o[1][2].getReal() - m[1][2].getReal(); final double corr20 = o[2][0].getReal() - m[2][0].getReal(); final double corr21 = o[2][1].getReal() - m[2][1].getReal(); final double corr22 = o[2][2].getReal() - m[2][2].getReal(); // Frobenius norm of the correction fn1 = corr00 * corr00 + corr01 * corr01 + corr02 * corr02 + corr10 * corr10 + corr11 * corr11 + corr12 * corr12 + corr20 * corr20 + corr21 * corr21 + corr22 * corr22; // convergence test if (FastMath.abs(fn1 - fn) <= threshold) { return o; } // prepare next iteration x00 = o[0][0]; x01 = o[0][1]; x02 = o[0][2]; x10 = o[1][0]; x11 = o[1][1]; x12 = o[1][2]; x20 = o[2][0]; x21 = o[2][1]; x22 = o[2][2]; fn = fn1; } // the algorithm did not converge after 10 iterations throw new NotARotationMatrixException(LocalizedFormats.UNABLE_TO_ORTHOGONOLIZE_MATRIX, i - 1); } /** Compute the distance between two rotations. *

                              The distance is intended here as a way to check if two * rotations are almost similar (i.e. they transform vectors the same way) * or very different. It is mathematically defined as the angle of * the rotation r that prepended to one of the rotations gives the other * one:

                              *
                                   *        r1(r) = r2
                                   * 
                              *

                              This distance is an angle between 0 and π. Its value is the smallest * possible upper bound of the angle in radians between r1(v) * and r2(v) for all possible vectors v. This upper bound is * reached for some v. The distance is equal to 0 if and only if the two * rotations are identical.

                              *

                              Comparing two rotations should always be done using this value rather * than for example comparing the components of the quaternions. It is much * more stable, and has a geometric meaning. Also comparing quaternions * components is error prone since for example quaternions (0.36, 0.48, -0.48, -0.64) * and (-0.36, -0.48, 0.48, 0.64) represent exactly the same rotation despite * their components are different (they are exact opposites).

                              * @param r1 first rotation * @param r2 second rotation * @param the type of the field elements * @return distance between r1 and r2 */ public static > T distance(final FieldRotation r1, final FieldRotation r2) { return r1.applyInverseTo(r2).getAngle(); } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/geometry/euclidean/threed/Line.java100644 1750 1750 20423 12126627714 31336 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.geometry.euclidean.threed; import org.apache.commons.math3.exception.MathIllegalArgumentException; import org.apache.commons.math3.exception.util.LocalizedFormats; import org.apache.commons.math3.geometry.Vector; import org.apache.commons.math3.geometry.euclidean.oned.Euclidean1D; import org.apache.commons.math3.geometry.euclidean.oned.IntervalsSet; import org.apache.commons.math3.geometry.euclidean.oned.Vector1D; import org.apache.commons.math3.geometry.partitioning.Embedding; import org.apache.commons.math3.util.FastMath; import org.apache.commons.math3.util.Precision; /** The class represent lines in a three dimensional space. *

                              Each oriented line is intrinsically associated with an abscissa * which is a coordinate on the line. The point at abscissa 0 is the * orthogonal projection of the origin on the line, another equivalent * way to express this is to say that it is the point of the line * which is closest to the origin. Abscissa increases in the line * direction.

                              * @version $Id: Line.java 1453218 2013-03-06 08:53:28Z luc $ * @since 3.0 */ public class Line implements Embedding { /** Line direction. */ private Vector3D direction; /** Line point closest to the origin. */ private Vector3D zero; /** Build a line from two points. * @param p1 first point belonging to the line (this can be any point) * @param p2 second point belonging to the line (this can be any point, different from p1) * @exception MathIllegalArgumentException if the points are equal */ public Line(final Vector3D p1, final Vector3D p2) throws MathIllegalArgumentException { reset(p1, p2); } /** Copy constructor. *

                              The created instance is completely independent from the * original instance, it is a deep copy.

                              * @param line line to copy */ public Line(final Line line) { this.direction = line.direction; this.zero = line.zero; } /** Reset the instance as if built from two points. * @param p1 first point belonging to the line (this can be any point) * @param p2 second point belonging to the line (this can be any point, different from p1) * @exception MathIllegalArgumentException if the points are equal */ public void reset(final Vector3D p1, final Vector3D p2) throws MathIllegalArgumentException { final Vector3D delta = p2.subtract(p1); final double norm2 = delta.getNormSq(); if (norm2 == 0.0) { throw new MathIllegalArgumentException(LocalizedFormats.ZERO_NORM); } this.direction = new Vector3D(1.0 / FastMath.sqrt(norm2), delta); zero = new Vector3D(1.0, p1, -p1.dotProduct(delta) / norm2, delta); } /** Get a line with reversed direction. * @return a new instance, with reversed direction */ public Line revert() { final Line reverted = new Line(this); reverted.direction = reverted.direction.negate(); return reverted; } /** Get the normalized direction vector. * @return normalized direction vector */ public Vector3D getDirection() { return direction; } /** Get the line point closest to the origin. * @return line point closest to the origin */ public Vector3D getOrigin() { return zero; } /** Get the abscissa of a point with respect to the line. *

                              The abscissa is 0 if the projection of the point and the * projection of the frame origin on the line are the same * point.

                              * @param point point to check * @return abscissa of the point */ public double getAbscissa(final Vector3D point) { return point.subtract(zero).dotProduct(direction); } /** Get one point from the line. * @param abscissa desired abscissa for the point * @return one point belonging to the line, at specified abscissa */ public Vector3D pointAt(final double abscissa) { return new Vector3D(1.0, zero, abscissa, direction); } /** {@inheritDoc} * @see #getAbscissa(Vector3D) */ public Vector1D toSubSpace(final Vector point) { return new Vector1D(getAbscissa((Vector3D) point)); } /** {@inheritDoc} * @see #pointAt(double) */ public Vector3D toSpace(final Vector point) { return pointAt(((Vector1D) point).getX()); } /** Check if the instance is similar to another line. *

                              Lines are considered similar if they contain the same * points. This does not mean they are equal since they can have * opposite directions.

                              * @param line line to which instance should be compared * @return true if the lines are similar */ public boolean isSimilarTo(final Line line) { final double angle = Vector3D.angle(direction, line.direction); return ((angle < 1.0e-10) || (angle > (FastMath.PI - 1.0e-10))) && contains(line.zero); } /** Check if the instance contains a point. * @param p point to check * @return true if p belongs to the line */ public boolean contains(final Vector3D p) { return distance(p) < 1.0e-10; } /** Compute the distance between the instance and a point. * @param p to check * @return distance between the instance and the point */ public double distance(final Vector3D p) { final Vector3D d = p.subtract(zero); final Vector3D n = new Vector3D(1.0, d, -d.dotProduct(direction), direction); return n.getNorm(); } /** Compute the shortest distance between the instance and another line. * @param line line to check against the instance * @return shortest distance between the instance and the line */ public double distance(final Line line) { final Vector3D normal = Vector3D.crossProduct(direction, line.direction); final double n = normal.getNorm(); if (n < Precision.SAFE_MIN) { // lines are parallel return distance(line.zero); } // signed separation of the two parallel planes that contains the lines final double offset = line.zero.subtract(zero).dotProduct(normal) / n; return FastMath.abs(offset); } /** Compute the point of the instance closest to another line. * @param line line to check against the instance * @return point of the instance closest to another line */ public Vector3D closestPoint(final Line line) { final double cos = direction.dotProduct(line.direction); final double n = 1 - cos * cos; if (n < Precision.EPSILON) { // the lines are parallel return zero; } final Vector3D delta0 = line.zero.subtract(zero); final double a = delta0.dotProduct(direction); final double b = delta0.dotProduct(line.direction); return new Vector3D(1, zero, (a - b * cos) / n, direction); } /** Get the intersection point of the instance and another line. * @param line other line * @return intersection point of the instance and the other line * or null if there are no intersection points */ public Vector3D intersection(final Line line) { final Vector3D closest = closestPoint(line); return line.contains(closest) ? closest : null; } /** Build a sub-line covering the whole line. * @return a sub-line covering the whole line */ public SubLine wholeLine() { return new SubLine(this, new IntervalsSet()); } } ././@LongLink100644 0 0 153 12126630647 10261 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/geometry/euclidean/threed/Vector3DFormat.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/geometry/euclidean/threed/Vector3DForma100644 1750 1750 14213 12126627714 32145 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.geometry.euclidean.threed; import java.text.FieldPosition; import java.text.NumberFormat; import java.text.ParsePosition; import java.util.Locale; import org.apache.commons.math3.exception.MathParseException; import org.apache.commons.math3.geometry.Vector; import org.apache.commons.math3.geometry.VectorFormat; import org.apache.commons.math3.util.CompositeFormat; /** * Formats a 3D vector in components list format "{x; y; z}". *

                              The prefix and suffix "{" and "}" and the separator "; " can be replaced by * any user-defined strings. The number format for components can be configured.

                              *

                              White space is ignored at parse time, even if it is in the prefix, suffix * or separator specifications. So even if the default separator does include a space * character that is used at format time, both input string "{1;1;1}" and * " { 1 ; 1 ; 1 } " will be parsed without error and the same vector will be * returned. In the second case, however, the parse position after parsing will be * just after the closing curly brace, i.e. just before the trailing space.

                              * * @version $Id: Vector3DFormat.java 1416643 2012-12-03 19:37:14Z tn $ */ public class Vector3DFormat extends VectorFormat { /** * Create an instance with default settings. *

                              The instance uses the default prefix, suffix and separator: * "{", "}", and "; " and the default number format for components.

                              */ public Vector3DFormat() { super(DEFAULT_PREFIX, DEFAULT_SUFFIX, DEFAULT_SEPARATOR, CompositeFormat.getDefaultNumberFormat()); } /** * Create an instance with a custom number format for components. * @param format the custom format for components. */ public Vector3DFormat(final NumberFormat format) { super(DEFAULT_PREFIX, DEFAULT_SUFFIX, DEFAULT_SEPARATOR, format); } /** * Create an instance with custom prefix, suffix and separator. * @param prefix prefix to use instead of the default "{" * @param suffix suffix to use instead of the default "}" * @param separator separator to use instead of the default "; " */ public Vector3DFormat(final String prefix, final String suffix, final String separator) { super(prefix, suffix, separator, CompositeFormat.getDefaultNumberFormat()); } /** * Create an instance with custom prefix, suffix, separator and format * for components. * @param prefix prefix to use instead of the default "{" * @param suffix suffix to use instead of the default "}" * @param separator separator to use instead of the default "; " * @param format the custom format for components. */ public Vector3DFormat(final String prefix, final String suffix, final String separator, final NumberFormat format) { super(prefix, suffix, separator, format); } /** * Returns the default 3D vector format for the current locale. * @return the default 3D vector format. */ public static Vector3DFormat getInstance() { return getInstance(Locale.getDefault()); } /** * Returns the default 3D vector format for the given locale. * @param locale the specific locale used by the format. * @return the 3D vector format specific to the given locale. */ public static Vector3DFormat getInstance(final Locale locale) { return new Vector3DFormat(CompositeFormat.getDefaultNumberFormat(locale)); } /** * Formats a {@link Vector3D} object to produce a string. * @param vector the object to format. * @param toAppendTo where the text is to be appended * @param pos On input: an alignment field, if desired. On output: the * offsets of the alignment field * @return the value passed in as toAppendTo. */ @Override public StringBuffer format(final Vector vector, final StringBuffer toAppendTo, final FieldPosition pos) { final Vector3D v3 = (Vector3D) vector; return format(toAppendTo, pos, v3.getX(), v3.getY(), v3.getZ()); } /** * Parses a string to produce a {@link Vector3D} object. * @param source the string to parse * @return the parsed {@link Vector3D} object. * @throws MathParseException if the beginning of the specified string * cannot be parsed. */ @Override public Vector3D parse(final String source) throws MathParseException { ParsePosition parsePosition = new ParsePosition(0); Vector3D result = parse(source, parsePosition); if (parsePosition.getIndex() == 0) { throw new MathParseException(source, parsePosition.getErrorIndex(), Vector3D.class); } return result; } /** * Parses a string to produce a {@link Vector3D} object. * @param source the string to parse * @param pos input/ouput parsing parameter. * @return the parsed {@link Vector3D} object. */ @Override public Vector3D parse(final String source, final ParsePosition pos) { final double[] coordinates = parseCoordinates(3, source, pos); if (coordinates == null) { return null; } return new Vector3D(coordinates[0], coordinates[1], coordinates[2]); } } ././@LongLink100644 0 0 161 12126630647 10260 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/geometry/euclidean/threed/SphericalCoordinates.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/geometry/euclidean/threed/SphericalCoor100644 1750 1750 36060 12126627714 32270 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.geometry.euclidean.threed; import java.io.Serializable; import org.apache.commons.math3.util.FastMath; /** This class provides conversions related to spherical coordinates. *

                              * The conventions used here are the mathematical ones, i.e. spherical coordinates are * related to Cartesian coordinates as follows: *

                              *
                                *
                              • x = r cos(θ) sin(Φ)
                              • *
                              • y = r sin(θ) sin(Φ)
                              • *
                              • z = r cos(Φ)
                              • *
                              *
                                *
                              • r = √(x2+y2+z2)
                              • *
                              • θ = atan2(y, x)
                              • *
                              • Φ = acos(z/r)
                              • *
                              *

                              * r is the radius, θ is the azimuthal angle in the x-y plane and Φ is the polar * (co-latitude) angle. These conventions are different from the conventions used * in physics (and in particular in spherical harmonics) where the meanings of θ and * Φ are reversed. *

                              *

                              * This class provides conversion of coordinates and also of gradient and Hessian * between spherical and Cartesian coordinates. *

                              * @since 3.2 * @version $Id: SphericalCoordinates.java 1443364 2013-02-07 09:28:04Z luc $ */ public class SphericalCoordinates implements Serializable { /** Serializable UID. */ private static final long serialVersionUID = 20130206L; /** Cartesian coordinates. */ private final Vector3D v; /** Radius. */ private final double r; /** Azimuthal angle in the x-y plane θ. */ private final double theta; /** Polar angle (co-latitude) Φ. */ private final double phi; /** Jacobian of (r, θ &Phi). */ private double[][] jacobian; /** Hessian of radius. */ private double[][] rHessian; /** Hessian of azimuthal angle in the x-y plane θ. */ private double[][] thetaHessian; /** Hessian of polar (co-latitude) angle Φ. */ private double[][] phiHessian; /** Build a spherical coordinates transformer from Cartesian coordinates. * @param v Cartesian coordinates */ public SphericalCoordinates(final Vector3D v) { // Cartesian coordinates this.v = v; // remaining spherical coordinates this.r = v.getNorm(); this.theta = v.getAlpha(); this.phi = FastMath.acos(v.getZ() / r); } /** Build a spherical coordinates transformer from spherical coordinates. * @param r radius * @param theta azimuthal angle in x-y plane * @param phi polar (co-latitude) angle */ public SphericalCoordinates(final double r, final double theta, final double phi) { final double cosTheta = FastMath.cos(theta); final double sinTheta = FastMath.sin(theta); final double cosPhi = FastMath.cos(phi); final double sinPhi = FastMath.sin(phi); // spherical coordinates this.r = r; this.theta = theta; this.phi = phi; // Cartesian coordinates this.v = new Vector3D(r * cosTheta * sinPhi, r * sinTheta * sinPhi, r * cosPhi); } /** Get the Cartesian coordinates. * @return Cartesian coordinates */ public Vector3D getCartesian() { return v; } /** Get the radius. * @return radius r * @see #getTheta() * @see #getPhi() */ public double getR() { return r; } /** Get the azimuthal angle in x-y plane. * @return azimuthal angle in x-y plane θ * @see #getR() * @see #getPhi() */ public double getTheta() { return theta; } /** Get the polar (co-latitude) angle. * @return polar (co-latitude) angle Φ * @see #getR() * @see #getTheta() */ public double getPhi() { return phi; } /** Convert a gradient with respect to spherical coordinates into a gradient * with respect to Cartesian coordinates. * @param sGradient gradient with respect to spherical coordinates * {df/dr, df/dθ, df/dΦ} * @return gradient with respect to Cartesian coordinates * {df/dx, df/dy, df/dz} */ public double[] toCartesianGradient(final double[] sGradient) { // lazy evaluation of Jacobian computeJacobian(); // compose derivatives as gradient^T . J // the expressions have been simplified since we know jacobian[1][2] = dTheta/dZ = 0 return new double[] { sGradient[0] * jacobian[0][0] + sGradient[1] * jacobian[1][0] + sGradient[2] * jacobian[2][0], sGradient[0] * jacobian[0][1] + sGradient[1] * jacobian[1][1] + sGradient[2] * jacobian[2][1], sGradient[0] * jacobian[0][2] + sGradient[2] * jacobian[2][2] }; } /** Convert a Hessian with respect to spherical coordinates into a Hessian * with respect to Cartesian coordinates. *

                              * As Hessian are always symmetric, we use only the lower left part of the provided * spherical Hessian, so the upper part may not be initialized. However, we still * do fill up the complete array we create, with guaranteed symmetry. *

                              * @param sHessian Hessian with respect to spherical coordinates * {{d2f/dr2, d2f/drdθ, d2f/drdΦ}, * {d2f/drdθ, d2f/dθ2, d2f/dθdΦ}, * {d2f/drdΦ, d2f/dθdΦ, d2f/dΦ2} * @param sGradient gradient with respect to spherical coordinates * {df/dr, df/dθ, df/dΦ} * @return Hessian with respect to Cartesian coordinates * {{d2f/dx2, d2f/dxdy, d2f/dxdz}, * {d2f/dxdy, d2f/dy2, d2f/dydz}, * {d2f/dxdz, d2f/dydz, d2f/dz2}} */ public double[][] toCartesianHessian(final double[][] sHessian, final double[] sGradient) { computeJacobian(); computeHessians(); // compose derivative as J^T . H_f . J + df/dr H_r + df/dtheta H_theta + df/dphi H_phi // the expressions have been simplified since we know jacobian[1][2] = dTheta/dZ = 0 // and H_theta is only a 2x2 matrix as it does not depend on z final double[][] hj = new double[3][3]; final double[][] cHessian = new double[3][3]; // compute H_f . J // beware we use ONLY the lower-left part of sHessian hj[0][0] = sHessian[0][0] * jacobian[0][0] + sHessian[1][0] * jacobian[1][0] + sHessian[2][0] * jacobian[2][0]; hj[0][1] = sHessian[0][0] * jacobian[0][1] + sHessian[1][0] * jacobian[1][1] + sHessian[2][0] * jacobian[2][1]; hj[0][2] = sHessian[0][0] * jacobian[0][2] + sHessian[2][0] * jacobian[2][2]; hj[1][0] = sHessian[1][0] * jacobian[0][0] + sHessian[1][1] * jacobian[1][0] + sHessian[2][1] * jacobian[2][0]; hj[1][1] = sHessian[1][0] * jacobian[0][1] + sHessian[1][1] * jacobian[1][1] + sHessian[2][1] * jacobian[2][1]; // don't compute hj[1][2] as it is not used below hj[2][0] = sHessian[2][0] * jacobian[0][0] + sHessian[2][1] * jacobian[1][0] + sHessian[2][2] * jacobian[2][0]; hj[2][1] = sHessian[2][0] * jacobian[0][1] + sHessian[2][1] * jacobian[1][1] + sHessian[2][2] * jacobian[2][1]; hj[2][2] = sHessian[2][0] * jacobian[0][2] + sHessian[2][2] * jacobian[2][2]; // compute lower-left part of J^T . H_f . J cHessian[0][0] = jacobian[0][0] * hj[0][0] + jacobian[1][0] * hj[1][0] + jacobian[2][0] * hj[2][0]; cHessian[1][0] = jacobian[0][1] * hj[0][0] + jacobian[1][1] * hj[1][0] + jacobian[2][1] * hj[2][0]; cHessian[2][0] = jacobian[0][2] * hj[0][0] + jacobian[2][2] * hj[2][0]; cHessian[1][1] = jacobian[0][1] * hj[0][1] + jacobian[1][1] * hj[1][1] + jacobian[2][1] * hj[2][1]; cHessian[2][1] = jacobian[0][2] * hj[0][1] + jacobian[2][2] * hj[2][1]; cHessian[2][2] = jacobian[0][2] * hj[0][2] + jacobian[2][2] * hj[2][2]; // add gradient contribution cHessian[0][0] += sGradient[0] * rHessian[0][0] + sGradient[1] * thetaHessian[0][0] + sGradient[2] * phiHessian[0][0]; cHessian[1][0] += sGradient[0] * rHessian[1][0] + sGradient[1] * thetaHessian[1][0] + sGradient[2] * phiHessian[1][0]; cHessian[2][0] += sGradient[0] * rHessian[2][0] + sGradient[2] * phiHessian[2][0]; cHessian[1][1] += sGradient[0] * rHessian[1][1] + sGradient[1] * thetaHessian[1][1] + sGradient[2] * phiHessian[1][1]; cHessian[2][1] += sGradient[0] * rHessian[2][1] + sGradient[2] * phiHessian[2][1]; cHessian[2][2] += sGradient[0] * rHessian[2][2] + sGradient[2] * phiHessian[2][2]; // ensure symmetry cHessian[0][1] = cHessian[1][0]; cHessian[0][2] = cHessian[2][0]; cHessian[1][2] = cHessian[2][1]; return cHessian; } /** Lazy evaluation of (r, θ, φ) Jacobian. */ private void computeJacobian() { if (jacobian == null) { // intermediate variables final double x = v.getX(); final double y = v.getY(); final double z = v.getZ(); final double rho2 = x * x + y * y; final double rho = FastMath.sqrt(rho2); final double r2 = rho2 + z * z; jacobian = new double[3][3]; // row representing the gradient of r jacobian[0][0] = x / r; jacobian[0][1] = y / r; jacobian[0][2] = z / r; // row representing the gradient of theta jacobian[1][0] = -y / rho2; jacobian[1][1] = x / rho2; // jacobian[1][2] is already set to 0 at allocation time // row representing the gradient of phi jacobian[2][0] = x * z / (rho * r2); jacobian[2][1] = y * z / (rho * r2); jacobian[2][2] = -rho / r2; } } /** Lazy evaluation of Hessians. */ private void computeHessians() { if (rHessian == null) { // intermediate variables final double x = v.getX(); final double y = v.getY(); final double z = v.getZ(); final double x2 = x * x; final double y2 = y * y; final double z2 = z * z; final double rho2 = x2 + y2; final double rho = FastMath.sqrt(rho2); final double r2 = rho2 + z2; final double xOr = x / r; final double yOr = y / r; final double zOr = z / r; final double xOrho2 = x / rho2; final double yOrho2 = y / rho2; final double xOr3 = xOr / r2; final double yOr3 = yOr / r2; final double zOr3 = zOr / r2; // lower-left part of Hessian of r rHessian = new double[3][3]; rHessian[0][0] = y * yOr3 + z * zOr3; rHessian[1][0] = -x * yOr3; rHessian[2][0] = -z * xOr3; rHessian[1][1] = x * xOr3 + z * zOr3; rHessian[2][1] = -y * zOr3; rHessian[2][2] = x * xOr3 + y * yOr3; // upper-right part is symmetric rHessian[0][1] = rHessian[1][0]; rHessian[0][2] = rHessian[2][0]; rHessian[1][2] = rHessian[2][1]; // lower-left part of Hessian of azimuthal angle theta thetaHessian = new double[2][2]; thetaHessian[0][0] = 2 * xOrho2 * yOrho2; thetaHessian[1][0] = yOrho2 * yOrho2 - xOrho2 * xOrho2; thetaHessian[1][1] = -2 * xOrho2 * yOrho2; // upper-right part is symmetric thetaHessian[0][1] = thetaHessian[1][0]; // lower-left part of Hessian of polar (co-latitude) angle phi final double rhor2 = rho * r2; final double rho2r2 = rho * rhor2; final double rhor4 = rhor2 * r2; final double rho3r4 = rhor4 * rho2; final double r2P2rho2 = 3 * rho2 + z2; phiHessian = new double[3][3]; phiHessian[0][0] = z * (rho2r2 - x2 * r2P2rho2) / rho3r4; phiHessian[1][0] = -x * y * z * r2P2rho2 / rho3r4; phiHessian[2][0] = x * (rho2 - z2) / rhor4; phiHessian[1][1] = z * (rho2r2 - y2 * r2P2rho2) / rho3r4; phiHessian[2][1] = y * (rho2 - z2) / rhor4; phiHessian[2][2] = 2 * rho * zOr3 / r; // upper-right part is symmetric phiHessian[0][1] = phiHessian[1][0]; phiHessian[0][2] = phiHessian[2][0]; phiHessian[1][2] = phiHessian[2][1]; } } /** * Replace the instance with a data transfer object for serialization. * @return data transfer object that will be serialized */ private Object writeReplace() { return new DataTransferObject(v.getX(), v.getY(), v.getZ()); } /** Internal class used only for serialization. */ private static class DataTransferObject implements Serializable { /** Serializable UID. */ private static final long serialVersionUID = 20130206L; /** Abscissa. * @serial */ private final double x; /** Ordinate. * @serial */ private final double y; /** Height. * @serial */ private final double z; /** Simple constructor. * @param x abscissa * @param y ordinate * @param z height */ public DataTransferObject(final double x, final double y, final double z) { this.x = x; this.y = y; this.z = z; } /** Replace the deserialized data transfer object with a {@link SphericalCoordinates}. * @return replacement {@link SphericalCoordinates} */ private Object readResolve() { return new SphericalCoordinates(new Vector3D(x, y, z)); } } } ././@LongLink100644 0 0 174 12126630647 10264 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/geometry/euclidean/threed/CardanEulerSingularityException.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/geometry/euclidean/threed/CardanEulerSi100644 1750 1750 3422 12126627714 32170 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.geometry.euclidean.threed; import org.apache.commons.math3.exception.MathIllegalStateException; import org.apache.commons.math3.exception.util.LocalizedFormats; /** This class represents exceptions thrown while extractiong Cardan * or Euler angles from a rotation. * @version $Id: CardanEulerSingularityException.java 1416643 2012-12-03 19:37:14Z tn $ * @since 1.2 */ public class CardanEulerSingularityException extends MathIllegalStateException { /** Serializable version identifier */ private static final long serialVersionUID = -1360952845582206770L; /** * Simple constructor. * build an exception with a default message. * @param isCardan if true, the rotation is related to Cardan angles, * if false it is related to EulerAngles */ public CardanEulerSingularityException(boolean isCardan) { super(isCardan ? LocalizedFormats.CARDAN_ANGLES_SINGULARITY : LocalizedFormats.EULER_ANGLES_SINGULARITY); } } ././@LongLink100644 0 0 145 12126630647 10262 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/geometry/euclidean/threed/SubPlane.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/geometry/euclidean/threed/SubPlane.java100644 1750 1750 15514 12126627714 32165 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.geometry.euclidean.threed; import org.apache.commons.math3.geometry.euclidean.oned.Vector1D; import org.apache.commons.math3.geometry.euclidean.twod.Euclidean2D; import org.apache.commons.math3.geometry.euclidean.twod.Vector2D; import org.apache.commons.math3.geometry.euclidean.twod.PolygonsSet; import org.apache.commons.math3.geometry.partitioning.AbstractSubHyperplane; import org.apache.commons.math3.geometry.partitioning.BSPTree; import org.apache.commons.math3.geometry.partitioning.Hyperplane; import org.apache.commons.math3.geometry.partitioning.Region; import org.apache.commons.math3.geometry.partitioning.Side; import org.apache.commons.math3.geometry.partitioning.SubHyperplane; /** This class represents a sub-hyperplane for {@link Plane}. * @version $Id: SubPlane.java 1416643 2012-12-03 19:37:14Z tn $ * @since 3.0 */ public class SubPlane extends AbstractSubHyperplane { /** Simple constructor. * @param hyperplane underlying hyperplane * @param remainingRegion remaining region of the hyperplane */ public SubPlane(final Hyperplane hyperplane, final Region remainingRegion) { super(hyperplane, remainingRegion); } /** {@inheritDoc} */ @Override protected AbstractSubHyperplane buildNew(final Hyperplane hyperplane, final Region remainingRegion) { return new SubPlane(hyperplane, remainingRegion); } /** {@inheritDoc} */ @Override public Side side(Hyperplane hyperplane) { final Plane otherPlane = (Plane) hyperplane; final Plane thisPlane = (Plane) getHyperplane(); final Line inter = otherPlane.intersection(thisPlane); if (inter == null) { // the hyperplanes are parallel, // any point can be used to check their relative position final double global = otherPlane.getOffset(thisPlane); return (global < -1.0e-10) ? Side.MINUS : ((global > 1.0e-10) ? Side.PLUS : Side.HYPER); } // create a 2D line in the otherPlane canonical 2D frame such that: // - the line is the crossing line of the two planes in 3D // - the line splits the otherPlane in two half planes with an // orientation consistent with the orientation of the instance // (i.e. the 3D half space on the plus side (resp. minus side) // of the instance contains the 2D half plane on the plus side // (resp. minus side) of the 2D line Vector2D p = thisPlane.toSubSpace(inter.toSpace(Vector1D.ZERO)); Vector2D q = thisPlane.toSubSpace(inter.toSpace(Vector1D.ONE)); Vector3D crossP = Vector3D.crossProduct(inter.getDirection(), thisPlane.getNormal()); if (crossP.dotProduct(otherPlane.getNormal()) < 0) { final Vector2D tmp = p; p = q; q = tmp; } final org.apache.commons.math3.geometry.euclidean.twod.Line line2D = new org.apache.commons.math3.geometry.euclidean.twod.Line(p, q); // check the side on the 2D plane return getRemainingRegion().side(line2D); } /** Split the instance in two parts by an hyperplane. * @param hyperplane splitting hyperplane * @return an object containing both the part of the instance * on the plus side of the instance and the part of the * instance on the minus side of the instance */ @Override public SplitSubHyperplane split(Hyperplane hyperplane) { final Plane otherPlane = (Plane) hyperplane; final Plane thisPlane = (Plane) getHyperplane(); final Line inter = otherPlane.intersection(thisPlane); if (inter == null) { // the hyperplanes are parallel final double global = otherPlane.getOffset(thisPlane); return (global < -1.0e-10) ? new SplitSubHyperplane(null, this) : new SplitSubHyperplane(this, null); } // the hyperplanes do intersect Vector2D p = thisPlane.toSubSpace(inter.toSpace(Vector1D.ZERO)); Vector2D q = thisPlane.toSubSpace(inter.toSpace(Vector1D.ONE)); Vector3D crossP = Vector3D.crossProduct(inter.getDirection(), thisPlane.getNormal()); if (crossP.dotProduct(otherPlane.getNormal()) < 0) { final Vector2D tmp = p; p = q; q = tmp; } final SubHyperplane l2DMinus = new org.apache.commons.math3.geometry.euclidean.twod.Line(p, q).wholeHyperplane(); final SubHyperplane l2DPlus = new org.apache.commons.math3.geometry.euclidean.twod.Line(q, p).wholeHyperplane(); final BSPTree splitTree = getRemainingRegion().getTree(false).split(l2DMinus); final BSPTree plusTree = getRemainingRegion().isEmpty(splitTree.getPlus()) ? new BSPTree(Boolean.FALSE) : new BSPTree(l2DPlus, new BSPTree(Boolean.FALSE), splitTree.getPlus(), null); final BSPTree minusTree = getRemainingRegion().isEmpty(splitTree.getMinus()) ? new BSPTree(Boolean.FALSE) : new BSPTree(l2DMinus, new BSPTree(Boolean.FALSE), splitTree.getMinus(), null); return new SplitSubHyperplane(new SubPlane(thisPlane.copySelf(), new PolygonsSet(plusTree)), new SubPlane(thisPlane.copySelf(), new PolygonsSet(minusTree))); } } ././@LongLink100644 0 0 145 12126630647 10262 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/geometry/euclidean/threed/Rotation.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/geometry/euclidean/threed/Rotation.java100644 1750 1750 125240 12126627714 32271 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.geometry.euclidean.threed; import java.io.Serializable; import org.apache.commons.math3.exception.MathArithmeticException; import org.apache.commons.math3.exception.MathIllegalArgumentException; import org.apache.commons.math3.exception.util.LocalizedFormats; import org.apache.commons.math3.util.FastMath; import org.apache.commons.math3.util.MathArrays; /** * This class implements rotations in a three-dimensional space. * *

                              Rotations can be represented by several different mathematical * entities (matrices, axe and angle, Cardan or Euler angles, * quaternions). This class presents an higher level abstraction, more * user-oriented and hiding this implementation details. Well, for the * curious, we use quaternions for the internal representation. The * user can build a rotation from any of these representations, and * any of these representations can be retrieved from a * Rotation instance (see the various constructors and * getters). In addition, a rotation can also be built implicitly * from a set of vectors and their image.

                              *

                              This implies that this class can be used to convert from one * representation to another one. For example, converting a rotation * matrix into a set of Cardan angles from can be done using the * following single line of code:

                              *
                               * double[] angles = new Rotation(matrix, 1.0e-10).getAngles(RotationOrder.XYZ);
                               * 
                              *

                              Focus is oriented on what a rotation do rather than on its * underlying representation. Once it has been built, and regardless of its * internal representation, a rotation is an operator which basically * transforms three dimensional {@link Vector3D vectors} into other three * dimensional {@link Vector3D vectors}. Depending on the application, the * meaning of these vectors may vary and the semantics of the rotation also.

                              *

                              For example in an spacecraft attitude simulation tool, users will often * consider the vectors are fixed (say the Earth direction for example) and the * frames change. The rotation transforms the coordinates of the vector in inertial * frame into the coordinates of the same vector in satellite frame. In this * case, the rotation implicitly defines the relation between the two frames.

                              *

                              Another example could be a telescope control application, where the rotation * would transform the sighting direction at rest into the desired observing * direction when the telescope is pointed towards an object of interest. In this * case the rotation transforms the direction at rest in a topocentric frame * into the sighting direction in the same topocentric frame. This implies in this * case the frame is fixed and the vector moves.

                              *

                              In many case, both approaches will be combined. In our telescope example, * we will probably also need to transform the observing direction in the topocentric * frame into the observing direction in inertial frame taking into account the observatory * location and the Earth rotation, which would essentially be an application of the * first approach.

                              * *

                              These examples show that a rotation is what the user wants it to be. This * class does not push the user towards one specific definition and hence does not * provide methods like projectVectorIntoDestinationFrame or * computeTransformedDirection. It provides simpler and more generic * methods: {@link #applyTo(Vector3D) applyTo(Vector3D)} and {@link * #applyInverseTo(Vector3D) applyInverseTo(Vector3D)}.

                              * *

                              Since a rotation is basically a vectorial operator, several rotations can be * composed together and the composite operation r = r1 o * r2 (which means that for each vector u, * r(u) = r1(r2(u))) is also a rotation. Hence * we can consider that in addition to vectors, a rotation can be applied to other * rotations as well (or to itself). With our previous notations, we would say we * can apply r1 to r2 and the result * we get is r = r1 o r2. For this purpose, the * class provides the methods: {@link #applyTo(Rotation) applyTo(Rotation)} and * {@link #applyInverseTo(Rotation) applyInverseTo(Rotation)}.

                              * *

                              Rotations are guaranteed to be immutable objects.

                              * * @version $Id: Rotation.java 1416643 2012-12-03 19:37:14Z tn $ * @see Vector3D * @see RotationOrder * @since 1.2 */ public class Rotation implements Serializable { /** Identity rotation. */ public static final Rotation IDENTITY = new Rotation(1.0, 0.0, 0.0, 0.0, false); /** Serializable version identifier */ private static final long serialVersionUID = -2153622329907944313L; /** Scalar coordinate of the quaternion. */ private final double q0; /** First coordinate of the vectorial part of the quaternion. */ private final double q1; /** Second coordinate of the vectorial part of the quaternion. */ private final double q2; /** Third coordinate of the vectorial part of the quaternion. */ private final double q3; /** Build a rotation from the quaternion coordinates. *

                              A rotation can be built from a normalized quaternion, * i.e. a quaternion for which q02 + * q12 + q22 + * q32 = 1. If the quaternion is not normalized, * the constructor can normalize it in a preprocessing step.

                              *

                              Note that some conventions put the scalar part of the quaternion * as the 4th component and the vector part as the first three * components. This is not our convention. We put the scalar part * as the first component.

                              * @param q0 scalar part of the quaternion * @param q1 first coordinate of the vectorial part of the quaternion * @param q2 second coordinate of the vectorial part of the quaternion * @param q3 third coordinate of the vectorial part of the quaternion * @param needsNormalization if true, the coordinates are considered * not to be normalized, a normalization preprocessing step is performed * before using them */ public Rotation(double q0, double q1, double q2, double q3, boolean needsNormalization) { if (needsNormalization) { // normalization preprocessing double inv = 1.0 / FastMath.sqrt(q0 * q0 + q1 * q1 + q2 * q2 + q3 * q3); q0 *= inv; q1 *= inv; q2 *= inv; q3 *= inv; } this.q0 = q0; this.q1 = q1; this.q2 = q2; this.q3 = q3; } /** Build a rotation from an axis and an angle. *

                              We use the convention that angles are oriented according to * the effect of the rotation on vectors around the axis. That means * that if (i, j, k) is a direct frame and if we first provide +k as * the axis and π/2 as the angle to this constructor, and then * {@link #applyTo(Vector3D) apply} the instance to +i, we will get * +j.

                              *

                              Another way to represent our convention is to say that a rotation * of angle θ about the unit vector (x, y, z) is the same as the * rotation build from quaternion components { cos(-θ/2), * x * sin(-θ/2), y * sin(-θ/2), z * sin(-θ/2) }. * Note the minus sign on the angle!

                              *

                              On the one hand this convention is consistent with a vectorial * perspective (moving vectors in fixed frames), on the other hand it * is different from conventions with a frame perspective (fixed vectors * viewed from different frames) like the ones used for example in spacecraft * attitude community or in the graphics community.

                              * @param axis axis around which to rotate * @param angle rotation angle. * @exception MathIllegalArgumentException if the axis norm is zero */ public Rotation(Vector3D axis, double angle) throws MathIllegalArgumentException { double norm = axis.getNorm(); if (norm == 0) { throw new MathIllegalArgumentException(LocalizedFormats.ZERO_NORM_FOR_ROTATION_AXIS); } double halfAngle = -0.5 * angle; double coeff = FastMath.sin(halfAngle) / norm; q0 = FastMath.cos (halfAngle); q1 = coeff * axis.getX(); q2 = coeff * axis.getY(); q3 = coeff * axis.getZ(); } /** Build a rotation from a 3X3 matrix. *

                              Rotation matrices are orthogonal matrices, i.e. unit matrices * (which are matrices for which m.mT = I) with real * coefficients. The module of the determinant of unit matrices is * 1, among the orthogonal 3X3 matrices, only the ones having a * positive determinant (+1) are rotation matrices.

                              *

                              When a rotation is defined by a matrix with truncated values * (typically when it is extracted from a technical sheet where only * four to five significant digits are available), the matrix is not * orthogonal anymore. This constructor handles this case * transparently by using a copy of the given matrix and applying a * correction to the copy in order to perfect its orthogonality. If * the Frobenius norm of the correction needed is above the given * threshold, then the matrix is considered to be too far from a * true rotation matrix and an exception is thrown.

                              * @param m rotation matrix * @param threshold convergence threshold for the iterative * orthogonality correction (convergence is reached when the * difference between two steps of the Frobenius norm of the * correction is below this threshold) * @exception NotARotationMatrixException if the matrix is not a 3X3 * matrix, or if it cannot be transformed into an orthogonal matrix * with the given threshold, or if the determinant of the resulting * orthogonal matrix is negative */ public Rotation(double[][] m, double threshold) throws NotARotationMatrixException { // dimension check if ((m.length != 3) || (m[0].length != 3) || (m[1].length != 3) || (m[2].length != 3)) { throw new NotARotationMatrixException( LocalizedFormats.ROTATION_MATRIX_DIMENSIONS, m.length, m[0].length); } // compute a "close" orthogonal matrix double[][] ort = orthogonalizeMatrix(m, threshold); // check the sign of the determinant double det = ort[0][0] * (ort[1][1] * ort[2][2] - ort[2][1] * ort[1][2]) - ort[1][0] * (ort[0][1] * ort[2][2] - ort[2][1] * ort[0][2]) + ort[2][0] * (ort[0][1] * ort[1][2] - ort[1][1] * ort[0][2]); if (det < 0.0) { throw new NotARotationMatrixException( LocalizedFormats.CLOSEST_ORTHOGONAL_MATRIX_HAS_NEGATIVE_DETERMINANT, det); } double[] quat = mat2quat(ort); q0 = quat[0]; q1 = quat[1]; q2 = quat[2]; q3 = quat[3]; } /** Build the rotation that transforms a pair of vector into another pair. *

                              Except for possible scale factors, if the instance were applied to * the pair (u1, u2) it will produce the pair * (v1, v2).

                              *

                              If the angular separation between u1 and u2 is * not the same as the angular separation between v1 and * v2, then a corrected v'2 will be used rather than * v2, the corrected vector will be in the (v1, * v2) plane.

                              * @param u1 first vector of the origin pair * @param u2 second vector of the origin pair * @param v1 desired image of u1 by the rotation * @param v2 desired image of u2 by the rotation * @exception MathArithmeticException if the norm of one of the vectors is zero, * or if one of the pair is degenerated (i.e. the vectors of the pair are colinear) */ public Rotation(Vector3D u1, Vector3D u2, Vector3D v1, Vector3D v2) throws MathArithmeticException { // build orthonormalized base from u1, u2 // this fails when vectors are null or colinear, which is forbidden to define a rotation final Vector3D u3 = u1.crossProduct(u2).normalize(); u2 = u3.crossProduct(u1).normalize(); u1 = u1.normalize(); // build an orthonormalized base from v1, v2 // this fails when vectors are null or colinear, which is forbidden to define a rotation final Vector3D v3 = v1.crossProduct(v2).normalize(); v2 = v3.crossProduct(v1).normalize(); v1 = v1.normalize(); // buid a matrix transforming the first base into the second one final double[][] m = new double[][] { { MathArrays.linearCombination(u1.getX(), v1.getX(), u2.getX(), v2.getX(), u3.getX(), v3.getX()), MathArrays.linearCombination(u1.getY(), v1.getX(), u2.getY(), v2.getX(), u3.getY(), v3.getX()), MathArrays.linearCombination(u1.getZ(), v1.getX(), u2.getZ(), v2.getX(), u3.getZ(), v3.getX()) }, { MathArrays.linearCombination(u1.getX(), v1.getY(), u2.getX(), v2.getY(), u3.getX(), v3.getY()), MathArrays.linearCombination(u1.getY(), v1.getY(), u2.getY(), v2.getY(), u3.getY(), v3.getY()), MathArrays.linearCombination(u1.getZ(), v1.getY(), u2.getZ(), v2.getY(), u3.getZ(), v3.getY()) }, { MathArrays.linearCombination(u1.getX(), v1.getZ(), u2.getX(), v2.getZ(), u3.getX(), v3.getZ()), MathArrays.linearCombination(u1.getY(), v1.getZ(), u2.getY(), v2.getZ(), u3.getY(), v3.getZ()), MathArrays.linearCombination(u1.getZ(), v1.getZ(), u2.getZ(), v2.getZ(), u3.getZ(), v3.getZ()) } }; double[] quat = mat2quat(m); q0 = quat[0]; q1 = quat[1]; q2 = quat[2]; q3 = quat[3]; } /** Build one of the rotations that transform one vector into another one. *

                              Except for a possible scale factor, if the instance were * applied to the vector u it will produce the vector v. There is an * infinite number of such rotations, this constructor choose the * one with the smallest associated angle (i.e. the one whose axis * is orthogonal to the (u, v) plane). If u and v are colinear, an * arbitrary rotation axis is chosen.

                              * @param u origin vector * @param v desired image of u by the rotation * @exception MathArithmeticException if the norm of one of the vectors is zero */ public Rotation(Vector3D u, Vector3D v) throws MathArithmeticException { double normProduct = u.getNorm() * v.getNorm(); if (normProduct == 0) { throw new MathArithmeticException(LocalizedFormats.ZERO_NORM_FOR_ROTATION_DEFINING_VECTOR); } double dot = u.dotProduct(v); if (dot < ((2.0e-15 - 1.0) * normProduct)) { // special case u = -v: we select a PI angle rotation around // an arbitrary vector orthogonal to u Vector3D w = u.orthogonal(); q0 = 0.0; q1 = -w.getX(); q2 = -w.getY(); q3 = -w.getZ(); } else { // general case: (u, v) defines a plane, we select // the shortest possible rotation: axis orthogonal to this plane q0 = FastMath.sqrt(0.5 * (1.0 + dot / normProduct)); double coeff = 1.0 / (2.0 * q0 * normProduct); Vector3D q = v.crossProduct(u); q1 = coeff * q.getX(); q2 = coeff * q.getY(); q3 = coeff * q.getZ(); } } /** Build a rotation from three Cardan or Euler elementary rotations. *

                              Cardan rotations are three successive rotations around the * canonical axes X, Y and Z, each axis being used once. There are * 6 such sets of rotations (XYZ, XZY, YXZ, YZX, ZXY and ZYX). Euler * rotations are three successive rotations around the canonical * axes X, Y and Z, the first and last rotations being around the * same axis. There are 6 such sets of rotations (XYX, XZX, YXY, * YZY, ZXZ and ZYZ), the most popular one being ZXZ.

                              *

                              Beware that many people routinely use the term Euler angles even * for what really are Cardan angles (this confusion is especially * widespread in the aerospace business where Roll, Pitch and Yaw angles * are often wrongly tagged as Euler angles).

                              * @param order order of rotations to use * @param alpha1 angle of the first elementary rotation * @param alpha2 angle of the second elementary rotation * @param alpha3 angle of the third elementary rotation */ public Rotation(RotationOrder order, double alpha1, double alpha2, double alpha3) { Rotation r1 = new Rotation(order.getA1(), alpha1); Rotation r2 = new Rotation(order.getA2(), alpha2); Rotation r3 = new Rotation(order.getA3(), alpha3); Rotation composed = r1.applyTo(r2.applyTo(r3)); q0 = composed.q0; q1 = composed.q1; q2 = composed.q2; q3 = composed.q3; } /** Convert an orthogonal rotation matrix to a quaternion. * @param ort orthogonal rotation matrix * @return quaternion corresponding to the matrix */ private static double[] mat2quat(final double[][] ort) { final double[] quat = new double[4]; // There are different ways to compute the quaternions elements // from the matrix. They all involve computing one element from // the diagonal of the matrix, and computing the three other ones // using a formula involving a division by the first element, // which unfortunately can be zero. Since the norm of the // quaternion is 1, we know at least one element has an absolute // value greater or equal to 0.5, so it is always possible to // select the right formula and avoid division by zero and even // numerical inaccuracy. Checking the elements in turn and using // the first one greater than 0.45 is safe (this leads to a simple // test since qi = 0.45 implies 4 qi^2 - 1 = -0.19) double s = ort[0][0] + ort[1][1] + ort[2][2]; if (s > -0.19) { // compute q0 and deduce q1, q2 and q3 quat[0] = 0.5 * FastMath.sqrt(s + 1.0); double inv = 0.25 / quat[0]; quat[1] = inv * (ort[1][2] - ort[2][1]); quat[2] = inv * (ort[2][0] - ort[0][2]); quat[3] = inv * (ort[0][1] - ort[1][0]); } else { s = ort[0][0] - ort[1][1] - ort[2][2]; if (s > -0.19) { // compute q1 and deduce q0, q2 and q3 quat[1] = 0.5 * FastMath.sqrt(s + 1.0); double inv = 0.25 / quat[1]; quat[0] = inv * (ort[1][2] - ort[2][1]); quat[2] = inv * (ort[0][1] + ort[1][0]); quat[3] = inv * (ort[0][2] + ort[2][0]); } else { s = ort[1][1] - ort[0][0] - ort[2][2]; if (s > -0.19) { // compute q2 and deduce q0, q1 and q3 quat[2] = 0.5 * FastMath.sqrt(s + 1.0); double inv = 0.25 / quat[2]; quat[0] = inv * (ort[2][0] - ort[0][2]); quat[1] = inv * (ort[0][1] + ort[1][0]); quat[3] = inv * (ort[2][1] + ort[1][2]); } else { // compute q3 and deduce q0, q1 and q2 s = ort[2][2] - ort[0][0] - ort[1][1]; quat[3] = 0.5 * FastMath.sqrt(s + 1.0); double inv = 0.25 / quat[3]; quat[0] = inv * (ort[0][1] - ort[1][0]); quat[1] = inv * (ort[0][2] + ort[2][0]); quat[2] = inv * (ort[2][1] + ort[1][2]); } } } return quat; } /** Revert a rotation. * Build a rotation which reverse the effect of another * rotation. This means that if r(u) = v, then r.revert(v) = u. The * instance is not changed. * @return a new rotation whose effect is the reverse of the effect * of the instance */ public Rotation revert() { return new Rotation(-q0, q1, q2, q3, false); } /** Get the scalar coordinate of the quaternion. * @return scalar coordinate of the quaternion */ public double getQ0() { return q0; } /** Get the first coordinate of the vectorial part of the quaternion. * @return first coordinate of the vectorial part of the quaternion */ public double getQ1() { return q1; } /** Get the second coordinate of the vectorial part of the quaternion. * @return second coordinate of the vectorial part of the quaternion */ public double getQ2() { return q2; } /** Get the third coordinate of the vectorial part of the quaternion. * @return third coordinate of the vectorial part of the quaternion */ public double getQ3() { return q3; } /** Get the normalized axis of the rotation. * @return normalized axis of the rotation * @see #Rotation(Vector3D, double) */ public Vector3D getAxis() { double squaredSine = q1 * q1 + q2 * q2 + q3 * q3; if (squaredSine == 0) { return new Vector3D(1, 0, 0); } else if (q0 < 0) { double inverse = 1 / FastMath.sqrt(squaredSine); return new Vector3D(q1 * inverse, q2 * inverse, q3 * inverse); } double inverse = -1 / FastMath.sqrt(squaredSine); return new Vector3D(q1 * inverse, q2 * inverse, q3 * inverse); } /** Get the angle of the rotation. * @return angle of the rotation (between 0 and π) * @see #Rotation(Vector3D, double) */ public double getAngle() { if ((q0 < -0.1) || (q0 > 0.1)) { return 2 * FastMath.asin(FastMath.sqrt(q1 * q1 + q2 * q2 + q3 * q3)); } else if (q0 < 0) { return 2 * FastMath.acos(-q0); } return 2 * FastMath.acos(q0); } /** Get the Cardan or Euler angles corresponding to the instance. *

                              The equations show that each rotation can be defined by two * different values of the Cardan or Euler angles set. For example * if Cardan angles are used, the rotation defined by the angles * a1, a2 and a3 is the same as * the rotation defined by the angles π + a1, π * - a2 and π + a3. This method implements * the following arbitrary choices:

                              *
                                *
                              • for Cardan angles, the chosen set is the one for which the * second angle is between -π/2 and π/2 (i.e its cosine is * positive),
                              • *
                              • for Euler angles, the chosen set is the one for which the * second angle is between 0 and π (i.e its sine is positive).
                              • *
                              *

                              Cardan and Euler angle have a very disappointing drawback: all * of them have singularities. This means that if the instance is * too close to the singularities corresponding to the given * rotation order, it will be impossible to retrieve the angles. For * Cardan angles, this is often called gimbal lock. There is * nothing to do to prevent this, it is an intrinsic problem * with Cardan and Euler representation (but not a problem with the * rotation itself, which is perfectly well defined). For Cardan * angles, singularities occur when the second angle is close to * -π/2 or +π/2, for Euler angle singularities occur when the * second angle is close to 0 or π, this implies that the identity * rotation is always singular for Euler angles!

                              * @param order rotation order to use * @return an array of three angles, in the order specified by the set * @exception CardanEulerSingularityException if the rotation is * singular with respect to the angles set specified */ public double[] getAngles(RotationOrder order) throws CardanEulerSingularityException { if (order == RotationOrder.XYZ) { // r (Vector3D.plusK) coordinates are : // sin (theta), -cos (theta) sin (phi), cos (theta) cos (phi) // (-r) (Vector3D.plusI) coordinates are : // cos (psi) cos (theta), -sin (psi) cos (theta), sin (theta) // and we can choose to have theta in the interval [-PI/2 ; +PI/2] Vector3D v1 = applyTo(Vector3D.PLUS_K); Vector3D v2 = applyInverseTo(Vector3D.PLUS_I); if ((v2.getZ() < -0.9999999999) || (v2.getZ() > 0.9999999999)) { throw new CardanEulerSingularityException(true); } return new double[] { FastMath.atan2(-(v1.getY()), v1.getZ()), FastMath.asin(v2.getZ()), FastMath.atan2(-(v2.getY()), v2.getX()) }; } else if (order == RotationOrder.XZY) { // r (Vector3D.plusJ) coordinates are : // -sin (psi), cos (psi) cos (phi), cos (psi) sin (phi) // (-r) (Vector3D.plusI) coordinates are : // cos (theta) cos (psi), -sin (psi), sin (theta) cos (psi) // and we can choose to have psi in the interval [-PI/2 ; +PI/2] Vector3D v1 = applyTo(Vector3D.PLUS_J); Vector3D v2 = applyInverseTo(Vector3D.PLUS_I); if ((v2.getY() < -0.9999999999) || (v2.getY() > 0.9999999999)) { throw new CardanEulerSingularityException(true); } return new double[] { FastMath.atan2(v1.getZ(), v1.getY()), -FastMath.asin(v2.getY()), FastMath.atan2(v2.getZ(), v2.getX()) }; } else if (order == RotationOrder.YXZ) { // r (Vector3D.plusK) coordinates are : // cos (phi) sin (theta), -sin (phi), cos (phi) cos (theta) // (-r) (Vector3D.plusJ) coordinates are : // sin (psi) cos (phi), cos (psi) cos (phi), -sin (phi) // and we can choose to have phi in the interval [-PI/2 ; +PI/2] Vector3D v1 = applyTo(Vector3D.PLUS_K); Vector3D v2 = applyInverseTo(Vector3D.PLUS_J); if ((v2.getZ() < -0.9999999999) || (v2.getZ() > 0.9999999999)) { throw new CardanEulerSingularityException(true); } return new double[] { FastMath.atan2(v1.getX(), v1.getZ()), -FastMath.asin(v2.getZ()), FastMath.atan2(v2.getX(), v2.getY()) }; } else if (order == RotationOrder.YZX) { // r (Vector3D.plusI) coordinates are : // cos (psi) cos (theta), sin (psi), -cos (psi) sin (theta) // (-r) (Vector3D.plusJ) coordinates are : // sin (psi), cos (phi) cos (psi), -sin (phi) cos (psi) // and we can choose to have psi in the interval [-PI/2 ; +PI/2] Vector3D v1 = applyTo(Vector3D.PLUS_I); Vector3D v2 = applyInverseTo(Vector3D.PLUS_J); if ((v2.getX() < -0.9999999999) || (v2.getX() > 0.9999999999)) { throw new CardanEulerSingularityException(true); } return new double[] { FastMath.atan2(-(v1.getZ()), v1.getX()), FastMath.asin(v2.getX()), FastMath.atan2(-(v2.getZ()), v2.getY()) }; } else if (order == RotationOrder.ZXY) { // r (Vector3D.plusJ) coordinates are : // -cos (phi) sin (psi), cos (phi) cos (psi), sin (phi) // (-r) (Vector3D.plusK) coordinates are : // -sin (theta) cos (phi), sin (phi), cos (theta) cos (phi) // and we can choose to have phi in the interval [-PI/2 ; +PI/2] Vector3D v1 = applyTo(Vector3D.PLUS_J); Vector3D v2 = applyInverseTo(Vector3D.PLUS_K); if ((v2.getY() < -0.9999999999) || (v2.getY() > 0.9999999999)) { throw new CardanEulerSingularityException(true); } return new double[] { FastMath.atan2(-(v1.getX()), v1.getY()), FastMath.asin(v2.getY()), FastMath.atan2(-(v2.getX()), v2.getZ()) }; } else if (order == RotationOrder.ZYX) { // r (Vector3D.plusI) coordinates are : // cos (theta) cos (psi), cos (theta) sin (psi), -sin (theta) // (-r) (Vector3D.plusK) coordinates are : // -sin (theta), sin (phi) cos (theta), cos (phi) cos (theta) // and we can choose to have theta in the interval [-PI/2 ; +PI/2] Vector3D v1 = applyTo(Vector3D.PLUS_I); Vector3D v2 = applyInverseTo(Vector3D.PLUS_K); if ((v2.getX() < -0.9999999999) || (v2.getX() > 0.9999999999)) { throw new CardanEulerSingularityException(true); } return new double[] { FastMath.atan2(v1.getY(), v1.getX()), -FastMath.asin(v2.getX()), FastMath.atan2(v2.getY(), v2.getZ()) }; } else if (order == RotationOrder.XYX) { // r (Vector3D.plusI) coordinates are : // cos (theta), sin (phi1) sin (theta), -cos (phi1) sin (theta) // (-r) (Vector3D.plusI) coordinates are : // cos (theta), sin (theta) sin (phi2), sin (theta) cos (phi2) // and we can choose to have theta in the interval [0 ; PI] Vector3D v1 = applyTo(Vector3D.PLUS_I); Vector3D v2 = applyInverseTo(Vector3D.PLUS_I); if ((v2.getX() < -0.9999999999) || (v2.getX() > 0.9999999999)) { throw new CardanEulerSingularityException(false); } return new double[] { FastMath.atan2(v1.getY(), -v1.getZ()), FastMath.acos(v2.getX()), FastMath.atan2(v2.getY(), v2.getZ()) }; } else if (order == RotationOrder.XZX) { // r (Vector3D.plusI) coordinates are : // cos (psi), cos (phi1) sin (psi), sin (phi1) sin (psi) // (-r) (Vector3D.plusI) coordinates are : // cos (psi), -sin (psi) cos (phi2), sin (psi) sin (phi2) // and we can choose to have psi in the interval [0 ; PI] Vector3D v1 = applyTo(Vector3D.PLUS_I); Vector3D v2 = applyInverseTo(Vector3D.PLUS_I); if ((v2.getX() < -0.9999999999) || (v2.getX() > 0.9999999999)) { throw new CardanEulerSingularityException(false); } return new double[] { FastMath.atan2(v1.getZ(), v1.getY()), FastMath.acos(v2.getX()), FastMath.atan2(v2.getZ(), -v2.getY()) }; } else if (order == RotationOrder.YXY) { // r (Vector3D.plusJ) coordinates are : // sin (theta1) sin (phi), cos (phi), cos (theta1) sin (phi) // (-r) (Vector3D.plusJ) coordinates are : // sin (phi) sin (theta2), cos (phi), -sin (phi) cos (theta2) // and we can choose to have phi in the interval [0 ; PI] Vector3D v1 = applyTo(Vector3D.PLUS_J); Vector3D v2 = applyInverseTo(Vector3D.PLUS_J); if ((v2.getY() < -0.9999999999) || (v2.getY() > 0.9999999999)) { throw new CardanEulerSingularityException(false); } return new double[] { FastMath.atan2(v1.getX(), v1.getZ()), FastMath.acos(v2.getY()), FastMath.atan2(v2.getX(), -v2.getZ()) }; } else if (order == RotationOrder.YZY) { // r (Vector3D.plusJ) coordinates are : // -cos (theta1) sin (psi), cos (psi), sin (theta1) sin (psi) // (-r) (Vector3D.plusJ) coordinates are : // sin (psi) cos (theta2), cos (psi), sin (psi) sin (theta2) // and we can choose to have psi in the interval [0 ; PI] Vector3D v1 = applyTo(Vector3D.PLUS_J); Vector3D v2 = applyInverseTo(Vector3D.PLUS_J); if ((v2.getY() < -0.9999999999) || (v2.getY() > 0.9999999999)) { throw new CardanEulerSingularityException(false); } return new double[] { FastMath.atan2(v1.getZ(), -v1.getX()), FastMath.acos(v2.getY()), FastMath.atan2(v2.getZ(), v2.getX()) }; } else if (order == RotationOrder.ZXZ) { // r (Vector3D.plusK) coordinates are : // sin (psi1) sin (phi), -cos (psi1) sin (phi), cos (phi) // (-r) (Vector3D.plusK) coordinates are : // sin (phi) sin (psi2), sin (phi) cos (psi2), cos (phi) // and we can choose to have phi in the interval [0 ; PI] Vector3D v1 = applyTo(Vector3D.PLUS_K); Vector3D v2 = applyInverseTo(Vector3D.PLUS_K); if ((v2.getZ() < -0.9999999999) || (v2.getZ() > 0.9999999999)) { throw new CardanEulerSingularityException(false); } return new double[] { FastMath.atan2(v1.getX(), -v1.getY()), FastMath.acos(v2.getZ()), FastMath.atan2(v2.getX(), v2.getY()) }; } else { // last possibility is ZYZ // r (Vector3D.plusK) coordinates are : // cos (psi1) sin (theta), sin (psi1) sin (theta), cos (theta) // (-r) (Vector3D.plusK) coordinates are : // -sin (theta) cos (psi2), sin (theta) sin (psi2), cos (theta) // and we can choose to have theta in the interval [0 ; PI] Vector3D v1 = applyTo(Vector3D.PLUS_K); Vector3D v2 = applyInverseTo(Vector3D.PLUS_K); if ((v2.getZ() < -0.9999999999) || (v2.getZ() > 0.9999999999)) { throw new CardanEulerSingularityException(false); } return new double[] { FastMath.atan2(v1.getY(), v1.getX()), FastMath.acos(v2.getZ()), FastMath.atan2(v2.getY(), -v2.getX()) }; } } /** Get the 3X3 matrix corresponding to the instance * @return the matrix corresponding to the instance */ public double[][] getMatrix() { // products double q0q0 = q0 * q0; double q0q1 = q0 * q1; double q0q2 = q0 * q2; double q0q3 = q0 * q3; double q1q1 = q1 * q1; double q1q2 = q1 * q2; double q1q3 = q1 * q3; double q2q2 = q2 * q2; double q2q3 = q2 * q3; double q3q3 = q3 * q3; // create the matrix double[][] m = new double[3][]; m[0] = new double[3]; m[1] = new double[3]; m[2] = new double[3]; m [0][0] = 2.0 * (q0q0 + q1q1) - 1.0; m [1][0] = 2.0 * (q1q2 - q0q3); m [2][0] = 2.0 * (q1q3 + q0q2); m [0][1] = 2.0 * (q1q2 + q0q3); m [1][1] = 2.0 * (q0q0 + q2q2) - 1.0; m [2][1] = 2.0 * (q2q3 - q0q1); m [0][2] = 2.0 * (q1q3 - q0q2); m [1][2] = 2.0 * (q2q3 + q0q1); m [2][2] = 2.0 * (q0q0 + q3q3) - 1.0; return m; } /** Apply the rotation to a vector. * @param u vector to apply the rotation to * @return a new vector which is the image of u by the rotation */ public Vector3D applyTo(Vector3D u) { double x = u.getX(); double y = u.getY(); double z = u.getZ(); double s = q1 * x + q2 * y + q3 * z; return new Vector3D(2 * (q0 * (x * q0 - (q2 * z - q3 * y)) + s * q1) - x, 2 * (q0 * (y * q0 - (q3 * x - q1 * z)) + s * q2) - y, 2 * (q0 * (z * q0 - (q1 * y - q2 * x)) + s * q3) - z); } /** Apply the rotation to a vector stored in an array. * @param in an array with three items which stores vector to rotate * @param out an array with three items to put result to (it can be the same * array as in) */ public void applyTo(final double[] in, final double[] out) { final double x = in[0]; final double y = in[1]; final double z = in[2]; final double s = q1 * x + q2 * y + q3 * z; out[0] = 2 * (q0 * (x * q0 - (q2 * z - q3 * y)) + s * q1) - x; out[1] = 2 * (q0 * (y * q0 - (q3 * x - q1 * z)) + s * q2) - y; out[2] = 2 * (q0 * (z * q0 - (q1 * y - q2 * x)) + s * q3) - z; } /** Apply the inverse of the rotation to a vector. * @param u vector to apply the inverse of the rotation to * @return a new vector which such that u is its image by the rotation */ public Vector3D applyInverseTo(Vector3D u) { double x = u.getX(); double y = u.getY(); double z = u.getZ(); double s = q1 * x + q2 * y + q3 * z; double m0 = -q0; return new Vector3D(2 * (m0 * (x * m0 - (q2 * z - q3 * y)) + s * q1) - x, 2 * (m0 * (y * m0 - (q3 * x - q1 * z)) + s * q2) - y, 2 * (m0 * (z * m0 - (q1 * y - q2 * x)) + s * q3) - z); } /** Apply the inverse of the rotation to a vector stored in an array. * @param in an array with three items which stores vector to rotate * @param out an array with three items to put result to (it can be the same * array as in) */ public void applyInverseTo(final double[] in, final double[] out) { final double x = in[0]; final double y = in[1]; final double z = in[2]; final double s = q1 * x + q2 * y + q3 * z; final double m0 = -q0; out[0] = 2 * (m0 * (x * m0 - (q2 * z - q3 * y)) + s * q1) - x; out[1] = 2 * (m0 * (y * m0 - (q3 * x - q1 * z)) + s * q2) - y; out[2] = 2 * (m0 * (z * m0 - (q1 * y - q2 * x)) + s * q3) - z; } /** Apply the instance to another rotation. * Applying the instance to a rotation is computing the composition * in an order compliant with the following rule : let u be any * vector and v its image by r (i.e. r.applyTo(u) = v), let w be the image * of v by the instance (i.e. applyTo(v) = w), then w = comp.applyTo(u), * where comp = applyTo(r). * @param r rotation to apply the rotation to * @return a new rotation which is the composition of r by the instance */ public Rotation applyTo(Rotation r) { return new Rotation(r.q0 * q0 - (r.q1 * q1 + r.q2 * q2 + r.q3 * q3), r.q1 * q0 + r.q0 * q1 + (r.q2 * q3 - r.q3 * q2), r.q2 * q0 + r.q0 * q2 + (r.q3 * q1 - r.q1 * q3), r.q3 * q0 + r.q0 * q3 + (r.q1 * q2 - r.q2 * q1), false); } /** Apply the inverse of the instance to another rotation. * Applying the inverse of the instance to a rotation is computing * the composition in an order compliant with the following rule : * let u be any vector and v its image by r (i.e. r.applyTo(u) = v), * let w be the inverse image of v by the instance * (i.e. applyInverseTo(v) = w), then w = comp.applyTo(u), where * comp = applyInverseTo(r). * @param r rotation to apply the rotation to * @return a new rotation which is the composition of r by the inverse * of the instance */ public Rotation applyInverseTo(Rotation r) { return new Rotation(-r.q0 * q0 - (r.q1 * q1 + r.q2 * q2 + r.q3 * q3), -r.q1 * q0 + r.q0 * q1 + (r.q2 * q3 - r.q3 * q2), -r.q2 * q0 + r.q0 * q2 + (r.q3 * q1 - r.q1 * q3), -r.q3 * q0 + r.q0 * q3 + (r.q1 * q2 - r.q2 * q1), false); } /** Perfect orthogonality on a 3X3 matrix. * @param m initial matrix (not exactly orthogonal) * @param threshold convergence threshold for the iterative * orthogonality correction (convergence is reached when the * difference between two steps of the Frobenius norm of the * correction is below this threshold) * @return an orthogonal matrix close to m * @exception NotARotationMatrixException if the matrix cannot be * orthogonalized with the given threshold after 10 iterations */ private double[][] orthogonalizeMatrix(double[][] m, double threshold) throws NotARotationMatrixException { double[] m0 = m[0]; double[] m1 = m[1]; double[] m2 = m[2]; double x00 = m0[0]; double x01 = m0[1]; double x02 = m0[2]; double x10 = m1[0]; double x11 = m1[1]; double x12 = m1[2]; double x20 = m2[0]; double x21 = m2[1]; double x22 = m2[2]; double fn = 0; double fn1; double[][] o = new double[3][3]; double[] o0 = o[0]; double[] o1 = o[1]; double[] o2 = o[2]; // iterative correction: Xn+1 = Xn - 0.5 * (Xn.Mt.Xn - M) int i = 0; while (++i < 11) { // Mt.Xn double mx00 = m0[0] * x00 + m1[0] * x10 + m2[0] * x20; double mx10 = m0[1] * x00 + m1[1] * x10 + m2[1] * x20; double mx20 = m0[2] * x00 + m1[2] * x10 + m2[2] * x20; double mx01 = m0[0] * x01 + m1[0] * x11 + m2[0] * x21; double mx11 = m0[1] * x01 + m1[1] * x11 + m2[1] * x21; double mx21 = m0[2] * x01 + m1[2] * x11 + m2[2] * x21; double mx02 = m0[0] * x02 + m1[0] * x12 + m2[0] * x22; double mx12 = m0[1] * x02 + m1[1] * x12 + m2[1] * x22; double mx22 = m0[2] * x02 + m1[2] * x12 + m2[2] * x22; // Xn+1 o0[0] = x00 - 0.5 * (x00 * mx00 + x01 * mx10 + x02 * mx20 - m0[0]); o0[1] = x01 - 0.5 * (x00 * mx01 + x01 * mx11 + x02 * mx21 - m0[1]); o0[2] = x02 - 0.5 * (x00 * mx02 + x01 * mx12 + x02 * mx22 - m0[2]); o1[0] = x10 - 0.5 * (x10 * mx00 + x11 * mx10 + x12 * mx20 - m1[0]); o1[1] = x11 - 0.5 * (x10 * mx01 + x11 * mx11 + x12 * mx21 - m1[1]); o1[2] = x12 - 0.5 * (x10 * mx02 + x11 * mx12 + x12 * mx22 - m1[2]); o2[0] = x20 - 0.5 * (x20 * mx00 + x21 * mx10 + x22 * mx20 - m2[0]); o2[1] = x21 - 0.5 * (x20 * mx01 + x21 * mx11 + x22 * mx21 - m2[1]); o2[2] = x22 - 0.5 * (x20 * mx02 + x21 * mx12 + x22 * mx22 - m2[2]); // correction on each elements double corr00 = o0[0] - m0[0]; double corr01 = o0[1] - m0[1]; double corr02 = o0[2] - m0[2]; double corr10 = o1[0] - m1[0]; double corr11 = o1[1] - m1[1]; double corr12 = o1[2] - m1[2]; double corr20 = o2[0] - m2[0]; double corr21 = o2[1] - m2[1]; double corr22 = o2[2] - m2[2]; // Frobenius norm of the correction fn1 = corr00 * corr00 + corr01 * corr01 + corr02 * corr02 + corr10 * corr10 + corr11 * corr11 + corr12 * corr12 + corr20 * corr20 + corr21 * corr21 + corr22 * corr22; // convergence test if (FastMath.abs(fn1 - fn) <= threshold) { return o; } // prepare next iteration x00 = o0[0]; x01 = o0[1]; x02 = o0[2]; x10 = o1[0]; x11 = o1[1]; x12 = o1[2]; x20 = o2[0]; x21 = o2[1]; x22 = o2[2]; fn = fn1; } // the algorithm did not converge after 10 iterations throw new NotARotationMatrixException( LocalizedFormats.UNABLE_TO_ORTHOGONOLIZE_MATRIX, i - 1); } /** Compute the distance between two rotations. *

                              The distance is intended here as a way to check if two * rotations are almost similar (i.e. they transform vectors the same way) * or very different. It is mathematically defined as the angle of * the rotation r that prepended to one of the rotations gives the other * one:

                              *
                                 *        r1(r) = r2
                                 * 
                              *

                              This distance is an angle between 0 and π. Its value is the smallest * possible upper bound of the angle in radians between r1(v) * and r2(v) for all possible vectors v. This upper bound is * reached for some v. The distance is equal to 0 if and only if the two * rotations are identical.

                              *

                              Comparing two rotations should always be done using this value rather * than for example comparing the components of the quaternions. It is much * more stable, and has a geometric meaning. Also comparing quaternions * components is error prone since for example quaternions (0.36, 0.48, -0.48, -0.64) * and (-0.36, -0.48, 0.48, 0.64) represent exactly the same rotation despite * their components are different (they are exact opposites).

                              * @param r1 first rotation * @param r2 second rotation * @return distance between r1 and r2 */ public static double distance(Rotation r1, Rotation r2) { return r1.applyInverseTo(r2).getAngle(); } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/geometry/euclidean/threed/Plane.java100644 1750 1750 40300 12126627714 31502 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.geometry.euclidean.threed; import org.apache.commons.math3.exception.MathArithmeticException; import org.apache.commons.math3.exception.util.LocalizedFormats; import org.apache.commons.math3.geometry.Vector; import org.apache.commons.math3.geometry.euclidean.oned.Vector1D; import org.apache.commons.math3.geometry.euclidean.twod.Euclidean2D; import org.apache.commons.math3.geometry.euclidean.twod.PolygonsSet; import org.apache.commons.math3.geometry.euclidean.twod.Vector2D; import org.apache.commons.math3.geometry.partitioning.Embedding; import org.apache.commons.math3.geometry.partitioning.Hyperplane; import org.apache.commons.math3.util.FastMath; /** The class represent planes in a three dimensional space. * @version $Id: Plane.java 1416643 2012-12-03 19:37:14Z tn $ * @since 3.0 */ public class Plane implements Hyperplane, Embedding { /** Offset of the origin with respect to the plane. */ private double originOffset; /** Origin of the plane frame. */ private Vector3D origin; /** First vector of the plane frame (in plane). */ private Vector3D u; /** Second vector of the plane frame (in plane). */ private Vector3D v; /** Third vector of the plane frame (plane normal). */ private Vector3D w; /** Build a plane normal to a given direction and containing the origin. * @param normal normal direction to the plane * @exception MathArithmeticException if the normal norm is too small */ public Plane(final Vector3D normal) throws MathArithmeticException { setNormal(normal); originOffset = 0; setFrame(); } /** Build a plane from a point and a normal. * @param p point belonging to the plane * @param normal normal direction to the plane * @exception MathArithmeticException if the normal norm is too small */ public Plane(final Vector3D p, final Vector3D normal) throws MathArithmeticException { setNormal(normal); originOffset = -p.dotProduct(w); setFrame(); } /** Build a plane from three points. *

                              The plane is oriented in the direction of * {@code (p2-p1) ^ (p3-p1)}

                              * @param p1 first point belonging to the plane * @param p2 second point belonging to the plane * @param p3 third point belonging to the plane * @exception MathArithmeticException if the points do not constitute a plane */ public Plane(final Vector3D p1, final Vector3D p2, final Vector3D p3) throws MathArithmeticException { this(p1, p2.subtract(p1).crossProduct(p3.subtract(p1))); } /** Copy constructor. *

                              The instance created is completely independant of the original * one. A deep copy is used, none of the underlying object are * shared.

                              * @param plane plane to copy */ public Plane(final Plane plane) { originOffset = plane.originOffset; origin = plane.origin; u = plane.u; v = plane.v; w = plane.w; } /** Copy the instance. *

                              The instance created is completely independant of the original * one. A deep copy is used, none of the underlying objects are * shared (except for immutable objects).

                              * @return a new hyperplane, copy of the instance */ public Plane copySelf() { return new Plane(this); } /** Reset the instance as if built from a point and a normal. * @param p point belonging to the plane * @param normal normal direction to the plane * @exception MathArithmeticException if the normal norm is too small */ public void reset(final Vector3D p, final Vector3D normal) throws MathArithmeticException { setNormal(normal); originOffset = -p.dotProduct(w); setFrame(); } /** Reset the instance from another one. *

                              The updated instance is completely independant of the original * one. A deep reset is used none of the underlying object is * shared.

                              * @param original plane to reset from */ public void reset(final Plane original) { originOffset = original.originOffset; origin = original.origin; u = original.u; v = original.v; w = original.w; } /** Set the normal vactor. * @param normal normal direction to the plane (will be copied) * @exception MathArithmeticException if the normal norm is too small */ private void setNormal(final Vector3D normal) throws MathArithmeticException { final double norm = normal.getNorm(); if (norm < 1.0e-10) { throw new MathArithmeticException(LocalizedFormats.ZERO_NORM); } w = new Vector3D(1.0 / norm, normal); } /** Reset the plane frame. */ private void setFrame() { origin = new Vector3D(-originOffset, w); u = w.orthogonal(); v = Vector3D.crossProduct(w, u); } /** Get the origin point of the plane frame. *

                              The point returned is the orthogonal projection of the * 3D-space origin in the plane.

                              * @return the origin point of the plane frame (point closest to the * 3D-space origin) */ public Vector3D getOrigin() { return origin; } /** Get the normalized normal vector. *

                              The frame defined by ({@link #getU getU}, {@link #getV getV}, * {@link #getNormal getNormal}) is a rigth-handed orthonormalized * frame).

                              * @return normalized normal vector * @see #getU * @see #getV */ public Vector3D getNormal() { return w; } /** Get the plane first canonical vector. *

                              The frame defined by ({@link #getU getU}, {@link #getV getV}, * {@link #getNormal getNormal}) is a rigth-handed orthonormalized * frame).

                              * @return normalized first canonical vector * @see #getV * @see #getNormal */ public Vector3D getU() { return u; } /** Get the plane second canonical vector. *

                              The frame defined by ({@link #getU getU}, {@link #getV getV}, * {@link #getNormal getNormal}) is a rigth-handed orthonormalized * frame).

                              * @return normalized second canonical vector * @see #getU * @see #getNormal */ public Vector3D getV() { return v; } /** Revert the plane. *

                              Replace the instance by a similar plane with opposite orientation.

                              *

                              The new plane frame is chosen in such a way that a 3D point that had * {@code (x, y)} in-plane coordinates and {@code z} offset with * respect to the plane and is unaffected by the change will have * {@code (y, x)} in-plane coordinates and {@code -z} offset with * respect to the new plane. This means that the {@code u} and {@code v} * vectors returned by the {@link #getU} and {@link #getV} methods are exchanged, * and the {@code w} vector returned by the {@link #getNormal} method is * reversed.

                              */ public void revertSelf() { final Vector3D tmp = u; u = v; v = tmp; w = w.negate(); originOffset = -originOffset; } /** Transform a 3D space point into an in-plane point. * @param point point of the space (must be a {@link Vector3D * Vector3D} instance) * @return in-plane point (really a {@link * org.apache.commons.math3.geometry.euclidean.twod.Vector2D Vector2D} instance) * @see #toSpace */ public Vector2D toSubSpace(final Vector point) { return new Vector2D(point.dotProduct(u), point.dotProduct(v)); } /** Transform an in-plane point into a 3D space point. * @param point in-plane point (must be a {@link * org.apache.commons.math3.geometry.euclidean.twod.Vector2D Vector2D} instance) * @return 3D space point (really a {@link Vector3D Vector3D} instance) * @see #toSubSpace */ public Vector3D toSpace(final Vector point) { final Vector2D p2D = (Vector2D) point; return new Vector3D(p2D.getX(), u, p2D.getY(), v, -originOffset, w); } /** Get one point from the 3D-space. * @param inPlane desired in-plane coordinates for the point in the * plane * @param offset desired offset for the point * @return one point in the 3D-space, with given coordinates and offset * relative to the plane */ public Vector3D getPointAt(final Vector2D inPlane, final double offset) { return new Vector3D(inPlane.getX(), u, inPlane.getY(), v, offset - originOffset, w); } /** Check if the instance is similar to another plane. *

                              Planes are considered similar if they contain the same * points. This does not mean they are equal since they can have * opposite normals.

                              * @param plane plane to which the instance is compared * @return true if the planes are similar */ public boolean isSimilarTo(final Plane plane) { final double angle = Vector3D.angle(w, plane.w); return ((angle < 1.0e-10) && (FastMath.abs(originOffset - plane.originOffset) < 1.0e-10)) || ((angle > (FastMath.PI - 1.0e-10)) && (FastMath.abs(originOffset + plane.originOffset) < 1.0e-10)); } /** Rotate the plane around the specified point. *

                              The instance is not modified, a new instance is created.

                              * @param center rotation center * @param rotation vectorial rotation operator * @return a new plane */ public Plane rotate(final Vector3D center, final Rotation rotation) { final Vector3D delta = origin.subtract(center); final Plane plane = new Plane(center.add(rotation.applyTo(delta)), rotation.applyTo(w)); // make sure the frame is transformed as desired plane.u = rotation.applyTo(u); plane.v = rotation.applyTo(v); return plane; } /** Translate the plane by the specified amount. *

                              The instance is not modified, a new instance is created.

                              * @param translation translation to apply * @return a new plane */ public Plane translate(final Vector3D translation) { final Plane plane = new Plane(origin.add(translation), w); // make sure the frame is transformed as desired plane.u = u; plane.v = v; return plane; } /** Get the intersection of a line with the instance. * @param line line intersecting the instance * @return intersection point between between the line and the * instance (null if the line is parallel to the instance) */ public Vector3D intersection(final Line line) { final Vector3D direction = line.getDirection(); final double dot = w.dotProduct(direction); if (FastMath.abs(dot) < 1.0e-10) { return null; } final Vector3D point = line.toSpace(Vector1D.ZERO); final double k = -(originOffset + w.dotProduct(point)) / dot; return new Vector3D(1.0, point, k, direction); } /** Build the line shared by the instance and another plane. * @param other other plane * @return line at the intersection of the instance and the * other plane (really a {@link Line Line} instance) */ public Line intersection(final Plane other) { final Vector3D direction = Vector3D.crossProduct(w, other.w); if (direction.getNorm() < 1.0e-10) { return null; } final Vector3D point = intersection(this, other, new Plane(direction)); return new Line(point, point.add(direction)); } /** Get the intersection point of three planes. * @param plane1 first plane1 * @param plane2 second plane2 * @param plane3 third plane2 * @return intersection point of three planes, null if some planes are parallel */ public static Vector3D intersection(final Plane plane1, final Plane plane2, final Plane plane3) { // coefficients of the three planes linear equations final double a1 = plane1.w.getX(); final double b1 = plane1.w.getY(); final double c1 = plane1.w.getZ(); final double d1 = plane1.originOffset; final double a2 = plane2.w.getX(); final double b2 = plane2.w.getY(); final double c2 = plane2.w.getZ(); final double d2 = plane2.originOffset; final double a3 = plane3.w.getX(); final double b3 = plane3.w.getY(); final double c3 = plane3.w.getZ(); final double d3 = plane3.originOffset; // direct Cramer resolution of the linear system // (this is still feasible for a 3x3 system) final double a23 = b2 * c3 - b3 * c2; final double b23 = c2 * a3 - c3 * a2; final double c23 = a2 * b3 - a3 * b2; final double determinant = a1 * a23 + b1 * b23 + c1 * c23; if (FastMath.abs(determinant) < 1.0e-10) { return null; } final double r = 1.0 / determinant; return new Vector3D( (-a23 * d1 - (c1 * b3 - c3 * b1) * d2 - (c2 * b1 - c1 * b2) * d3) * r, (-b23 * d1 - (c3 * a1 - c1 * a3) * d2 - (c1 * a2 - c2 * a1) * d3) * r, (-c23 * d1 - (b1 * a3 - b3 * a1) * d2 - (b2 * a1 - b1 * a2) * d3) * r); } /** Build a region covering the whole hyperplane. * @return a region covering the whole hyperplane */ public SubPlane wholeHyperplane() { return new SubPlane(this, new PolygonsSet()); } /** Build a region covering the whole space. * @return a region containing the instance (really a {@link * PolyhedronsSet PolyhedronsSet} instance) */ public PolyhedronsSet wholeSpace() { return new PolyhedronsSet(); } /** Check if the instance contains a point. * @param p point to check * @return true if p belongs to the plane */ public boolean contains(final Vector3D p) { return FastMath.abs(getOffset(p)) < 1.0e-10; } /** Get the offset (oriented distance) of a parallel plane. *

                              This method should be called only for parallel planes otherwise * the result is not meaningful.

                              *

                              The offset is 0 if both planes are the same, it is * positive if the plane is on the plus side of the instance and * negative if it is on the minus side, according to its natural * orientation.

                              * @param plane plane to check * @return offset of the plane */ public double getOffset(final Plane plane) { return originOffset + (sameOrientationAs(plane) ? -plane.originOffset : plane.originOffset); } /** Get the offset (oriented distance) of a point. *

                              The offset is 0 if the point is on the underlying hyperplane, * it is positive if the point is on one particular side of the * hyperplane, and it is negative if the point is on the other side, * according to the hyperplane natural orientation.

                              * @param point point to check * @return offset of the point */ public double getOffset(final Vector point) { return point.dotProduct(w) + originOffset; } /** Check if the instance has the same orientation as another hyperplane. * @param other other hyperplane to check against the instance * @return true if the instance and the other hyperplane have * the same orientation */ public boolean sameOrientationAs(final Hyperplane other) { return (((Plane) other).w).dotProduct(w) > 0.0; } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/geometry/euclidean/oned/Vector1D.java100644 1750 1750 25251 12126627714 31554 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.geometry.euclidean.oned; import java.text.NumberFormat; import org.apache.commons.math3.exception.MathArithmeticException; import org.apache.commons.math3.exception.util.LocalizedFormats; import org.apache.commons.math3.geometry.Space; import org.apache.commons.math3.geometry.Vector; import org.apache.commons.math3.util.FastMath; import org.apache.commons.math3.util.MathUtils; /** This class represents a 1D vector. *

                              Instances of this class are guaranteed to be immutable.

                              * @version $Id: Vector1D.java 1416643 2012-12-03 19:37:14Z tn $ * @since 3.0 */ public class Vector1D implements Vector { /** Origin (coordinates: 0). */ public static final Vector1D ZERO = new Vector1D(0.0); /** Unit (coordinates: 1). */ public static final Vector1D ONE = new Vector1D(1.0); // CHECKSTYLE: stop ConstantName /** A vector with all coordinates set to NaN. */ public static final Vector1D NaN = new Vector1D(Double.NaN); // CHECKSTYLE: resume ConstantName /** A vector with all coordinates set to positive infinity. */ public static final Vector1D POSITIVE_INFINITY = new Vector1D(Double.POSITIVE_INFINITY); /** A vector with all coordinates set to negative infinity. */ public static final Vector1D NEGATIVE_INFINITY = new Vector1D(Double.NEGATIVE_INFINITY); /** Serializable UID. */ private static final long serialVersionUID = 7556674948671647925L; /** Abscissa. */ private final double x; /** Simple constructor. * Build a vector from its coordinates * @param x abscissa * @see #getX() */ public Vector1D(double x) { this.x = x; } /** Multiplicative constructor * Build a vector from another one and a scale factor. * The vector built will be a * u * @param a scale factor * @param u base (unscaled) vector */ public Vector1D(double a, Vector1D u) { this.x = a * u.x; } /** Linear constructor * Build a vector from two other ones and corresponding scale factors. * The vector built will be a1 * u1 + a2 * u2 * @param a1 first scale factor * @param u1 first base (unscaled) vector * @param a2 second scale factor * @param u2 second base (unscaled) vector */ public Vector1D(double a1, Vector1D u1, double a2, Vector1D u2) { this.x = a1 * u1.x + a2 * u2.x; } /** Linear constructor * Build a vector from three other ones and corresponding scale factors. * The vector built will be a1 * u1 + a2 * u2 + a3 * u3 * @param a1 first scale factor * @param u1 first base (unscaled) vector * @param a2 second scale factor * @param u2 second base (unscaled) vector * @param a3 third scale factor * @param u3 third base (unscaled) vector */ public Vector1D(double a1, Vector1D u1, double a2, Vector1D u2, double a3, Vector1D u3) { this.x = a1 * u1.x + a2 * u2.x + a3 * u3.x; } /** Linear constructor * Build a vector from four other ones and corresponding scale factors. * The vector built will be a1 * u1 + a2 * u2 + a3 * u3 + a4 * u4 * @param a1 first scale factor * @param u1 first base (unscaled) vector * @param a2 second scale factor * @param u2 second base (unscaled) vector * @param a3 third scale factor * @param u3 third base (unscaled) vector * @param a4 fourth scale factor * @param u4 fourth base (unscaled) vector */ public Vector1D(double a1, Vector1D u1, double a2, Vector1D u2, double a3, Vector1D u3, double a4, Vector1D u4) { this.x = a1 * u1.x + a2 * u2.x + a3 * u3.x + a4 * u4.x; } /** Get the abscissa of the vector. * @return abscissa of the vector * @see #Vector1D(double) */ public double getX() { return x; } /** {@inheritDoc} */ public Space getSpace() { return Euclidean1D.getInstance(); } /** {@inheritDoc} */ public Vector1D getZero() { return ZERO; } /** {@inheritDoc} */ public double getNorm1() { return FastMath.abs(x); } /** {@inheritDoc} */ public double getNorm() { return FastMath.abs(x); } /** {@inheritDoc} */ public double getNormSq() { return x * x; } /** {@inheritDoc} */ public double getNormInf() { return FastMath.abs(x); } /** {@inheritDoc} */ public Vector1D add(Vector v) { Vector1D v1 = (Vector1D) v; return new Vector1D(x + v1.getX()); } /** {@inheritDoc} */ public Vector1D add(double factor, Vector v) { Vector1D v1 = (Vector1D) v; return new Vector1D(x + factor * v1.getX()); } /** {@inheritDoc} */ public Vector1D subtract(Vector p) { Vector1D p3 = (Vector1D) p; return new Vector1D(x - p3.x); } /** {@inheritDoc} */ public Vector1D subtract(double factor, Vector v) { Vector1D v1 = (Vector1D) v; return new Vector1D(x - factor * v1.getX()); } /** {@inheritDoc} */ public Vector1D normalize() throws MathArithmeticException { double s = getNorm(); if (s == 0) { throw new MathArithmeticException(LocalizedFormats.CANNOT_NORMALIZE_A_ZERO_NORM_VECTOR); } return scalarMultiply(1 / s); } /** {@inheritDoc} */ public Vector1D negate() { return new Vector1D(-x); } /** {@inheritDoc} */ public Vector1D scalarMultiply(double a) { return new Vector1D(a * x); } /** {@inheritDoc} */ public boolean isNaN() { return Double.isNaN(x); } /** {@inheritDoc} */ public boolean isInfinite() { return !isNaN() && Double.isInfinite(x); } /** {@inheritDoc} */ public double distance1(Vector p) { Vector1D p3 = (Vector1D) p; final double dx = FastMath.abs(p3.x - x); return dx; } /** {@inheritDoc} */ public double distance(Vector p) { Vector1D p3 = (Vector1D) p; final double dx = p3.x - x; return FastMath.abs(dx); } /** {@inheritDoc} */ public double distanceInf(Vector p) { Vector1D p3 = (Vector1D) p; final double dx = FastMath.abs(p3.x - x); return dx; } /** {@inheritDoc} */ public double distanceSq(Vector p) { Vector1D p3 = (Vector1D) p; final double dx = p3.x - x; return dx * dx; } /** {@inheritDoc} */ public double dotProduct(final Vector v) { final Vector1D v1 = (Vector1D) v; return x * v1.x; } /** Compute the distance between two vectors according to the L2 norm. *

                              Calling this method is equivalent to calling: * p1.subtract(p2).getNorm() except that no intermediate * vector is built

                              * @param p1 first vector * @param p2 second vector * @return the distance between p1 and p2 according to the L2 norm */ public static double distance(Vector1D p1, Vector1D p2) { return p1.distance(p2); } /** Compute the distance between two vectors according to the L norm. *

                              Calling this method is equivalent to calling: * p1.subtract(p2).getNormInf() except that no intermediate * vector is built

                              * @param p1 first vector * @param p2 second vector * @return the distance between p1 and p2 according to the L norm */ public static double distanceInf(Vector1D p1, Vector1D p2) { return p1.distanceInf(p2); } /** Compute the square of the distance between two vectors. *

                              Calling this method is equivalent to calling: * p1.subtract(p2).getNormSq() except that no intermediate * vector is built

                              * @param p1 first vector * @param p2 second vector * @return the square of the distance between p1 and p2 */ public static double distanceSq(Vector1D p1, Vector1D p2) { return p1.distanceSq(p2); } /** * Test for the equality of two 1D vectors. *

                              * If all coordinates of two 1D vectors are exactly the same, and none are * Double.NaN, the two 1D vectors are considered to be equal. *

                              *

                              * NaN coordinates are considered to affect globally the vector * and be equals to each other - i.e, if either (or all) coordinates of the * 1D vector are equal to Double.NaN, the 1D vector is equal to * {@link #NaN}. *

                              * * @param other Object to test for equality to this * @return true if two 1D vector objects are equal, false if * object is null, not an instance of Vector1D, or * not equal to this Vector1D instance * */ @Override public boolean equals(Object other) { if (this == other) { return true; } if (other instanceof Vector1D) { final Vector1D rhs = (Vector1D)other; if (rhs.isNaN()) { return this.isNaN(); } return x == rhs.x; } return false; } /** * Get a hashCode for the 1D vector. *

                              * All NaN values have the same hash code.

                              * * @return a hash code value for this object */ @Override public int hashCode() { if (isNaN()) { return 7785; } return 997 * MathUtils.hash(x); } /** Get a string representation of this vector. * @return a string representation of this vector */ @Override public String toString() { return Vector1DFormat.getInstance().format(this); } /** {@inheritDoc} */ public String toString(final NumberFormat format) { return new Vector1DFormat(format).format(this); } } ././@LongLink100644 0 0 147 12126630647 10264 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/geometry/euclidean/oned/package-info.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/geometry/euclidean/oned/package-info.ja100644 1750 1750 1661 12126627714 32101 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ /** * *

                              * This package provides basic 1D geometry components. *

                              * */ package org.apache.commons.math3.geometry.euclidean.oned; commons-math3-3.2-src/src/main/java/org/apache/commons/math3/geometry/euclidean/oned/Interval.java100644 1750 1750 7474 12126627714 31700 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.geometry.euclidean.oned; import org.apache.commons.math3.geometry.partitioning.Region.Location; /** This class represents a 1D interval. * @see IntervalsSet * @version $Id: Interval.java 1422195 2012-12-15 06:45:18Z psteitz $ * @since 3.0 */ public class Interval { /** The lower bound of the interval. */ private final double lower; /** The upper bound of the interval. */ private final double upper; /** Simple constructor. * @param lower lower bound of the interval * @param upper upper bound of the interval */ public Interval(final double lower, final double upper) { this.lower = lower; this.upper = upper; } /** Get the lower bound of the interval. * @return lower bound of the interval * @since 3.1 */ public double getInf() { return lower; } /** Get the lower bound of the interval. * @return lower bound of the interval * @deprecated as of 3.1, replaced by {@link #getInf()} */ @Deprecated public double getLower() { return getInf(); } /** Get the upper bound of the interval. * @return upper bound of the interval * @since 3.1 */ public double getSup() { return upper; } /** Get the upper bound of the interval. * @return upper bound of the interval * @deprecated as of 3.1, replaced by {@link #getSup()} */ @Deprecated public double getUpper() { return getSup(); } /** Get the size of the interval. * @return size of the interval * @since 3.1 */ public double getSize() { return upper - lower; } /** Get the length of the interval. * @return length of the interval * @deprecated as of 3.1, replaced by {@link #getSize()} */ @Deprecated public double getLength() { return getSize(); } /** Get the barycenter of the interval. * @return barycenter of the interval * @since 3.1 */ public double getBarycenter() { return 0.5 * (lower + upper); } /** Get the midpoint of the interval. * @return midpoint of the interval * @deprecated as of 3.1, replaced by {@link #getBarycenter()} */ @Deprecated public double getMidPoint() { return getBarycenter(); } /** Check a point with respect to the interval. * @param point point to check * @param tolerance tolerance below which points are considered to * belong to the boundary * @return a code representing the point status: either {@link * Location#INSIDE}, {@link Location#OUTSIDE} or {@link Location#BOUNDARY} * @since 3.1 */ public Location checkPoint(final double point, final double tolerance) { if (point < lower - tolerance || point > upper + tolerance) { return Location.OUTSIDE; } else if (point > lower + tolerance && point < upper - tolerance) { return Location.INSIDE; } else { return Location.BOUNDARY; } } } ././@LongLink100644 0 0 150 12126630647 10256 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/geometry/euclidean/oned/OrientedPoint.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/geometry/euclidean/oned/OrientedPoint.j100644 1750 1750 7442 12126627714 32202 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.geometry.euclidean.oned; import org.apache.commons.math3.geometry.Vector; import org.apache.commons.math3.geometry.partitioning.Hyperplane; /** This class represents a 1D oriented hyperplane. *

                              An hyperplane in 1D is a simple point, its orientation being a * boolean.

                              *

                              Instances of this class are guaranteed to be immutable.

                              * @version $Id: OrientedPoint.java 1416643 2012-12-03 19:37:14Z tn $ * @since 3.0 */ public class OrientedPoint implements Hyperplane { /** Vector location. */ private Vector1D location; /** Orientation. */ private boolean direct; /** Simple constructor. * @param location location of the hyperplane * @param direct if true, the plus side of the hyperplane is towards * abscissas greater than {@code location} */ public OrientedPoint(final Vector1D location, final boolean direct) { this.location = location; this.direct = direct; } /** Copy the instance. *

                              Since instances are immutable, this method directly returns * the instance.

                              * @return the instance itself */ public OrientedPoint copySelf() { return this; } /** {@inheritDoc} */ public double getOffset(final Vector point) { final double delta = ((Vector1D) point).getX() - location.getX(); return direct ? delta : -delta; } /** Build a region covering the whole hyperplane. *

                              Since this class represent zero dimension spaces which does * not have lower dimension sub-spaces, this method returns a dummy * implementation of a {@link * org.apache.commons.math3.geometry.partitioning.SubHyperplane SubHyperplane}. * This implementation is only used to allow the {@link * org.apache.commons.math3.geometry.partitioning.SubHyperplane * SubHyperplane} class implementation to work properly, it should * not be used otherwise.

                              * @return a dummy sub hyperplane */ public SubOrientedPoint wholeHyperplane() { return new SubOrientedPoint(this, null); } /** Build a region covering the whole space. * @return a region containing the instance (really an {@link * IntervalsSet IntervalsSet} instance) */ public IntervalsSet wholeSpace() { return new IntervalsSet(); } /** {@inheritDoc} */ public boolean sameOrientationAs(final Hyperplane other) { return !(direct ^ ((OrientedPoint) other).direct); } /** Get the hyperplane location on the real line. * @return the hyperplane location */ public Vector1D getLocation() { return location; } /** Check if the hyperplane orientation is direct. * @return true if the plus side of the hyperplane is towards * abscissae greater than hyperplane location */ public boolean isDirect() { return direct; } /** Revert the instance. */ public void revertSelf() { direct = !direct; } } ././@LongLink100644 0 0 153 12126630647 10261 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/geometry/euclidean/oned/SubOrientedPoint.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/geometry/euclidean/oned/SubOrientedPoin100644 1750 1750 5773 12126627714 32245 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.geometry.euclidean.oned; import org.apache.commons.math3.geometry.partitioning.AbstractSubHyperplane; import org.apache.commons.math3.geometry.partitioning.Hyperplane; import org.apache.commons.math3.geometry.partitioning.Region; import org.apache.commons.math3.geometry.partitioning.Side; /** This class represents sub-hyperplane for {@link OrientedPoint}. *

                              An hyperplane in 1D is a simple point, its orientation being a * boolean.

                              *

                              Instances of this class are guaranteed to be immutable.

                              * @version $Id: SubOrientedPoint.java 1416643 2012-12-03 19:37:14Z tn $ * @since 3.0 */ public class SubOrientedPoint extends AbstractSubHyperplane { /** Simple constructor. * @param hyperplane underlying hyperplane * @param remainingRegion remaining region of the hyperplane */ public SubOrientedPoint(final Hyperplane hyperplane, final Region remainingRegion) { super(hyperplane, remainingRegion); } /** {@inheritDoc} */ @Override public double getSize() { return 0; } /** {@inheritDoc} */ @Override protected AbstractSubHyperplane buildNew(final Hyperplane hyperplane, final Region remainingRegion) { return new SubOrientedPoint(hyperplane, remainingRegion); } /** {@inheritDoc} */ @Override public Side side(final Hyperplane hyperplane) { final double global = hyperplane.getOffset(((OrientedPoint) getHyperplane()).getLocation()); return (global < -1.0e-10) ? Side.MINUS : ((global > 1.0e-10) ? Side.PLUS : Side.HYPER); } /** {@inheritDoc} */ @Override public SplitSubHyperplane split(final Hyperplane hyperplane) { final double global = hyperplane.getOffset(((OrientedPoint) getHyperplane()).getLocation()); return (global < -1.0e-10) ? new SplitSubHyperplane(null, this) : new SplitSubHyperplane(this, null); } } ././@LongLink100644 0 0 151 12126630647 10257 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/geometry/euclidean/oned/Vector1DFormat.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/geometry/euclidean/oned/Vector1DFormat.100644 1750 1750 12170 12126627714 32057 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.geometry.euclidean.oned; import java.text.FieldPosition; import java.text.NumberFormat; import java.text.ParsePosition; import java.util.Locale; import org.apache.commons.math3.exception.MathParseException; import org.apache.commons.math3.geometry.Vector; import org.apache.commons.math3.geometry.VectorFormat; import org.apache.commons.math3.util.CompositeFormat; /** * Formats a 1D vector in components list format "{x}". *

                              The prefix and suffix "{" and "}" can be replaced by * any user-defined strings. The number format for components can be configured.

                              *

                              White space is ignored at parse time, even if it is in the prefix, suffix * or separator specifications. So even if the default separator does include a space * character that is used at format time, both input string "{1}" and * " { 1 } " will be parsed without error and the same vector will be * returned. In the second case, however, the parse position after parsing will be * just after the closing curly brace, i.e. just before the trailing space.

                              * * @version $Id: Vector1DFormat.java 1416643 2012-12-03 19:37:14Z tn $ * @since 3.0 */ public class Vector1DFormat extends VectorFormat { /** * Create an instance with default settings. *

                              The instance uses the default prefix, suffix and separator: * "{", "}", and "; " and the default number format for components.

                              */ public Vector1DFormat() { super(DEFAULT_PREFIX, DEFAULT_SUFFIX, DEFAULT_SEPARATOR, CompositeFormat.getDefaultNumberFormat()); } /** * Create an instance with a custom number format for components. * @param format the custom format for components. */ public Vector1DFormat(final NumberFormat format) { super(DEFAULT_PREFIX, DEFAULT_SUFFIX, DEFAULT_SEPARATOR, format); } /** * Create an instance with custom prefix, suffix and separator. * @param prefix prefix to use instead of the default "{" * @param suffix suffix to use instead of the default "}" */ public Vector1DFormat(final String prefix, final String suffix) { super(prefix, suffix, DEFAULT_SEPARATOR, CompositeFormat.getDefaultNumberFormat()); } /** * Create an instance with custom prefix, suffix, separator and format * for components. * @param prefix prefix to use instead of the default "{" * @param suffix suffix to use instead of the default "}" * @param format the custom format for components. */ public Vector1DFormat(final String prefix, final String suffix, final NumberFormat format) { super(prefix, suffix, DEFAULT_SEPARATOR, format); } /** * Returns the default 1D vector format for the current locale. * @return the default 1D vector format. */ public static Vector1DFormat getInstance() { return getInstance(Locale.getDefault()); } /** * Returns the default 1D vector format for the given locale. * @param locale the specific locale used by the format. * @return the 1D vector format specific to the given locale. */ public static Vector1DFormat getInstance(final Locale locale) { return new Vector1DFormat(CompositeFormat.getDefaultNumberFormat(locale)); } /** {@inheritDoc} */ @Override public StringBuffer format(final Vector vector, final StringBuffer toAppendTo, final FieldPosition pos) { final Vector1D p1 = (Vector1D) vector; return format(toAppendTo, pos, p1.getX()); } /** {@inheritDoc} */ @Override public Vector1D parse(final String source) throws MathParseException { ParsePosition parsePosition = new ParsePosition(0); Vector1D result = parse(source, parsePosition); if (parsePosition.getIndex() == 0) { throw new MathParseException(source, parsePosition.getErrorIndex(), Vector1D.class); } return result; } /** {@inheritDoc} */ @Override public Vector1D parse(final String source, final ParsePosition pos) { final double[] coordinates = parseCoordinates(1, source, pos); if (coordinates == null) { return null; } return new Vector1D(coordinates[0]); } } ././@LongLink100644 0 0 146 12126630647 10263 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/geometry/euclidean/oned/Euclidean1D.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/geometry/euclidean/oned/Euclidean1D.jav100644 1750 1750 5355 12126627714 32025 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.geometry.euclidean.oned; import java.io.Serializable; import org.apache.commons.math3.exception.MathUnsupportedOperationException; import org.apache.commons.math3.exception.util.LocalizedFormats; import org.apache.commons.math3.geometry.Space; /** * This class implements a one-dimensional space. * @version $Id: Euclidean1D.java 1416643 2012-12-03 19:37:14Z tn $ * @since 3.0 */ public class Euclidean1D implements Serializable, Space { /** Serializable version identifier. */ private static final long serialVersionUID = -1178039568877797126L; /** Private constructor for the singleton. */ private Euclidean1D() { } /** Get the unique instance. * @return the unique instance */ public static Euclidean1D getInstance() { return LazyHolder.INSTANCE; } /** {@inheritDoc} */ public int getDimension() { return 1; } /** {@inheritDoc} *

                              * As the 1-dimension Euclidean space does not have proper sub-spaces, * this method always throws a {@link MathUnsupportedOperationException} *

                              * @return nothing * @throws MathUnsupportedOperationException in all cases */ public Space getSubSpace() throws MathUnsupportedOperationException { throw new MathUnsupportedOperationException(LocalizedFormats.NOT_SUPPORTED_IN_DIMENSION_N, 1); } // CHECKSTYLE: stop HideUtilityClassConstructor /** Holder for the instance. *

                              We use here the Initialization On Demand Holder Idiom.

                              */ private static class LazyHolder { /** Cached field instance. */ private static final Euclidean1D INSTANCE = new Euclidean1D(); } // CHECKSTYLE: resume HideUtilityClassConstructor /** Handle deserialization of the singleton. * @return the singleton instance */ private Object readResolve() { // return the singleton instance return LazyHolder.INSTANCE; } } ././@LongLink100644 0 0 147 12126630647 10264 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/geometry/euclidean/oned/IntervalsSet.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/geometry/euclidean/oned/IntervalsSet.ja100644 1750 1750 26175 12126627714 32227 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.geometry.euclidean.oned; import java.util.ArrayList; import java.util.Collection; import java.util.List; import org.apache.commons.math3.geometry.partitioning.AbstractRegion; import org.apache.commons.math3.geometry.partitioning.BSPTree; import org.apache.commons.math3.geometry.partitioning.SubHyperplane; import org.apache.commons.math3.util.Precision; /** This class represents a 1D region: a set of intervals. * @version $Id: IntervalsSet.java 1416643 2012-12-03 19:37:14Z tn $ * @since 3.0 */ public class IntervalsSet extends AbstractRegion { /** Build an intervals set representing the whole real line. */ public IntervalsSet() { super(); } /** Build an intervals set corresponding to a single interval. * @param lower lower bound of the interval, must be lesser or equal * to {@code upper} (may be {@code Double.NEGATIVE_INFINITY}) * @param upper upper bound of the interval, must be greater or equal * to {@code lower} (may be {@code Double.POSITIVE_INFINITY}) */ public IntervalsSet(final double lower, final double upper) { super(buildTree(lower, upper)); } /** Build an intervals set from an inside/outside BSP tree. *

                              The leaf nodes of the BSP tree must have a * {@code Boolean} attribute representing the inside status of * the corresponding cell (true for inside cells, false for outside * cells). In order to avoid building too many small objects, it is * recommended to use the predefined constants * {@code Boolean.TRUE} and {@code Boolean.FALSE}

                              * @param tree inside/outside BSP tree representing the intervals set */ public IntervalsSet(final BSPTree tree) { super(tree); } /** Build an intervals set from a Boundary REPresentation (B-rep). *

                              The boundary is provided as a collection of {@link * SubHyperplane sub-hyperplanes}. Each sub-hyperplane has the * interior part of the region on its minus side and the exterior on * its plus side.

                              *

                              The boundary elements can be in any order, and can form * several non-connected sets (like for example polygons with holes * or a set of disjoints polyhedrons considered as a whole). In * fact, the elements do not even need to be connected together * (their topological connections are not used here). However, if the * boundary does not really separate an inside open from an outside * open (open having here its topological meaning), then subsequent * calls to the {@link * org.apache.commons.math3.geometry.partitioning.Region#checkPoint(org.apache.commons.math3.geometry.Vector) * checkPoint} method will not be meaningful anymore.

                              *

                              If the boundary is empty, the region will represent the whole * space.

                              * @param boundary collection of boundary elements */ public IntervalsSet(final Collection> boundary) { super(boundary); } /** Build an inside/outside tree representing a single interval. * @param lower lower bound of the interval, must be lesser or equal * to {@code upper} (may be {@code Double.NEGATIVE_INFINITY}) * @param upper upper bound of the interval, must be greater or equal * to {@code lower} (may be {@code Double.POSITIVE_INFINITY}) * @return the built tree */ private static BSPTree buildTree(final double lower, final double upper) { if (Double.isInfinite(lower) && (lower < 0)) { if (Double.isInfinite(upper) && (upper > 0)) { // the tree must cover the whole real line return new BSPTree(Boolean.TRUE); } // the tree must be open on the negative infinity side final SubHyperplane upperCut = new OrientedPoint(new Vector1D(upper), true).wholeHyperplane(); return new BSPTree(upperCut, new BSPTree(Boolean.FALSE), new BSPTree(Boolean.TRUE), null); } final SubHyperplane lowerCut = new OrientedPoint(new Vector1D(lower), false).wholeHyperplane(); if (Double.isInfinite(upper) && (upper > 0)) { // the tree must be open on the positive infinity side return new BSPTree(lowerCut, new BSPTree(Boolean.FALSE), new BSPTree(Boolean.TRUE), null); } // the tree must be bounded on the two sides final SubHyperplane upperCut = new OrientedPoint(new Vector1D(upper), true).wholeHyperplane(); return new BSPTree(lowerCut, new BSPTree(Boolean.FALSE), new BSPTree(upperCut, new BSPTree(Boolean.FALSE), new BSPTree(Boolean.TRUE), null), null); } /** {@inheritDoc} */ @Override public IntervalsSet buildNew(final BSPTree tree) { return new IntervalsSet(tree); } /** {@inheritDoc} */ @Override protected void computeGeometricalProperties() { if (getTree(false).getCut() == null) { setBarycenter(Vector1D.NaN); setSize(((Boolean) getTree(false).getAttribute()) ? Double.POSITIVE_INFINITY : 0); } else { double size = 0.0; double sum = 0.0; for (final Interval interval : asList()) { size += interval.getSize(); sum += interval.getSize() * interval.getBarycenter(); } setSize(size); if (Double.isInfinite(size)) { setBarycenter(Vector1D.NaN); } else if (size >= Precision.SAFE_MIN) { setBarycenter(new Vector1D(sum / size)); } else { setBarycenter(((OrientedPoint) getTree(false).getCut().getHyperplane()).getLocation()); } } } /** Get the lowest value belonging to the instance. * @return lowest value belonging to the instance * ({@code Double.NEGATIVE_INFINITY} if the instance doesn't * have any low bound, {@code Double.POSITIVE_INFINITY} if the * instance is empty) */ public double getInf() { BSPTree node = getTree(false); double inf = Double.POSITIVE_INFINITY; while (node.getCut() != null) { final OrientedPoint op = (OrientedPoint) node.getCut().getHyperplane(); inf = op.getLocation().getX(); node = op.isDirect() ? node.getMinus() : node.getPlus(); } return ((Boolean) node.getAttribute()) ? Double.NEGATIVE_INFINITY : inf; } /** Get the highest value belonging to the instance. * @return highest value belonging to the instance * ({@code Double.POSITIVE_INFINITY} if the instance doesn't * have any high bound, {@code Double.NEGATIVE_INFINITY} if the * instance is empty) */ public double getSup() { BSPTree node = getTree(false); double sup = Double.NEGATIVE_INFINITY; while (node.getCut() != null) { final OrientedPoint op = (OrientedPoint) node.getCut().getHyperplane(); sup = op.getLocation().getX(); node = op.isDirect() ? node.getPlus() : node.getMinus(); } return ((Boolean) node.getAttribute()) ? Double.POSITIVE_INFINITY : sup; } /** Build an ordered list of intervals representing the instance. *

                              This method builds this intervals set as an ordered list of * {@link Interval Interval} elements. If the intervals set has no * lower limit, the first interval will have its low bound equal to * {@code Double.NEGATIVE_INFINITY}. If the intervals set has * no upper limit, the last interval will have its upper bound equal * to {@code Double.POSITIVE_INFINITY}. An empty tree will * build an empty list while a tree representing the whole real line * will build a one element list with both bounds beeing * infinite.

                              * @return a new ordered list containing {@link Interval Interval} * elements */ public List asList() { final List list = new ArrayList(); recurseList(getTree(false), list, Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY); return list; } /** Update an intervals list. * @param node current node * @param list list to update * @param lower lower bound of the current convex cell * @param upper upper bound of the current convex cell */ private void recurseList(final BSPTree node, final List list, final double lower, final double upper) { if (node.getCut() == null) { if ((Boolean) node.getAttribute()) { // this leaf cell is an inside cell: an interval list.add(new Interval(lower, upper)); } } else { final OrientedPoint op = (OrientedPoint) node.getCut().getHyperplane(); final Vector1D loc = op.getLocation(); double x = loc.getX(); // make sure we explore the tree in increasing order final BSPTree low = op.isDirect() ? node.getMinus() : node.getPlus(); final BSPTree high = op.isDirect() ? node.getPlus() : node.getMinus(); recurseList(low, list, lower, x); if ((checkPoint(low, loc) == Location.INSIDE) && (checkPoint(high, loc) == Location.INSIDE)) { // merge the last interval added and the first one of the high sub-tree x = list.remove(list.size() - 1).getInf(); } recurseList(high, list, x, upper); } } } ././@LongLink100644 0 0 151 12126630647 10257 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/random/CorrelatedRandomVectorGenerator.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/random/CorrelatedRandomVectorGenerator.100644 1750 1750 17212 12126627700 32302 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.random; import org.apache.commons.math3.exception.DimensionMismatchException; import org.apache.commons.math3.linear.RealMatrix; import org.apache.commons.math3.linear.RectangularCholeskyDecomposition; /** * A {@link RandomVectorGenerator} that generates vectors with with * correlated components. *

                              Random vectors with correlated components are built by combining * the uncorrelated components of another random vector in such a way that * the resulting correlations are the ones specified by a positive * definite covariance matrix.

                              *

                              The main use for correlated random vector generation is for Monte-Carlo * simulation of physical problems with several variables, for example to * generate error vectors to be added to a nominal vector. A particularly * interesting case is when the generated vector should be drawn from a * Multivariate Normal Distribution. The approach using a Cholesky * decomposition is quite usual in this case. However, it can be extended * to other cases as long as the underlying random generator provides * {@link NormalizedRandomGenerator normalized values} like {@link * GaussianRandomGenerator} or {@link UniformRandomGenerator}.

                              *

                              Sometimes, the covariance matrix for a given simulation is not * strictly positive definite. This means that the correlations are * not all independent from each other. In this case, however, the non * strictly positive elements found during the Cholesky decomposition * of the covariance matrix should not be negative either, they * should be null. Another non-conventional extension handling this case * is used here. Rather than computing C = UT.U * where C is the covariance matrix and U * is an upper-triangular matrix, we compute C = B.BT * where B is a rectangular matrix having * more rows than columns. The number of columns of B is * the rank of the covariance matrix, and it is the dimension of the * uncorrelated random vector that is needed to compute the component * of the correlated vector. This class handles this situation * automatically.

                              * * @version $Id: CorrelatedRandomVectorGenerator.java 1416643 2012-12-03 19:37:14Z tn $ * @since 1.2 */ public class CorrelatedRandomVectorGenerator implements RandomVectorGenerator { /** Mean vector. */ private final double[] mean; /** Underlying generator. */ private final NormalizedRandomGenerator generator; /** Storage for the normalized vector. */ private final double[] normalized; /** Root of the covariance matrix. */ private final RealMatrix root; /** * Builds a correlated random vector generator from its mean * vector and covariance matrix. * * @param mean Expected mean values for all components. * @param covariance Covariance matrix. * @param small Diagonal elements threshold under which column are * considered to be dependent on previous ones and are discarded * @param generator underlying generator for uncorrelated normalized * components. * @throws org.apache.commons.math3.linear.NonPositiveDefiniteMatrixException * if the covariance matrix is not strictly positive definite. * @throws DimensionMismatchException if the mean and covariance * arrays dimensions do not match. */ public CorrelatedRandomVectorGenerator(double[] mean, RealMatrix covariance, double small, NormalizedRandomGenerator generator) { int order = covariance.getRowDimension(); if (mean.length != order) { throw new DimensionMismatchException(mean.length, order); } this.mean = mean.clone(); final RectangularCholeskyDecomposition decomposition = new RectangularCholeskyDecomposition(covariance, small); root = decomposition.getRootMatrix(); this.generator = generator; normalized = new double[decomposition.getRank()]; } /** * Builds a null mean random correlated vector generator from its * covariance matrix. * * @param covariance Covariance matrix. * @param small Diagonal elements threshold under which column are * considered to be dependent on previous ones and are discarded. * @param generator Underlying generator for uncorrelated normalized * components. * @throws org.apache.commons.math3.linear.NonPositiveDefiniteMatrixException * if the covariance matrix is not strictly positive definite. */ public CorrelatedRandomVectorGenerator(RealMatrix covariance, double small, NormalizedRandomGenerator generator) { int order = covariance.getRowDimension(); mean = new double[order]; for (int i = 0; i < order; ++i) { mean[i] = 0; } final RectangularCholeskyDecomposition decomposition = new RectangularCholeskyDecomposition(covariance, small); root = decomposition.getRootMatrix(); this.generator = generator; normalized = new double[decomposition.getRank()]; } /** Get the underlying normalized components generator. * @return underlying uncorrelated components generator */ public NormalizedRandomGenerator getGenerator() { return generator; } /** Get the rank of the covariance matrix. * The rank is the number of independent rows in the covariance * matrix, it is also the number of columns of the root matrix. * @return rank of the square matrix. * @see #getRootMatrix() */ public int getRank() { return normalized.length; } /** Get the root of the covariance matrix. * The root is the rectangular matrix B such that * the covariance matrix is equal to B.BT * @return root of the square matrix * @see #getRank() */ public RealMatrix getRootMatrix() { return root; } /** Generate a correlated random vector. * @return a random vector as an array of double. The returned array * is created at each call, the caller can do what it wants with it. */ public double[] nextVector() { // generate uncorrelated vector for (int i = 0; i < normalized.length; ++i) { normalized[i] = generator.nextNormalizedDouble(); } // compute correlated vector double[] correlated = new double[mean.length]; for (int i = 0; i < correlated.length; ++i) { correlated[i] = mean[i]; for (int j = 0; j < root.getColumnDimension(); ++j) { correlated[i] += root.getEntry(i, j) * normalized[j]; } } return correlated; } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/random/Well19937c.java100644 1750 1750 7765 12126627700 26344 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.random; /** This class implements the WELL19937c pseudo-random number generator * from François Panneton, Pierre L'Ecuyer and Makoto Matsumoto. *

                              This generator is described in a paper by François Panneton, * Pierre L'Ecuyer and Makoto Matsumoto Improved * Long-Period Generators Based on Linear Recurrences Modulo 2 ACM * Transactions on Mathematical Software, 32, 1 (2006). The errata for the paper * are in wellrng-errata.txt.

                              * @see WELL Random number generator * @version $Id: Well19937c.java 1416643 2012-12-03 19:37:14Z tn $ * @since 2.2 */ public class Well19937c extends AbstractWell { /** Serializable version identifier. */ private static final long serialVersionUID = -7203498180754925124L; /** Number of bits in the pool. */ private static final int K = 19937; /** First parameter of the algorithm. */ private static final int M1 = 70; /** Second parameter of the algorithm. */ private static final int M2 = 179; /** Third parameter of the algorithm. */ private static final int M3 = 449; /** Creates a new random number generator. *

                              The instance is initialized using the current time as the * seed.

                              */ public Well19937c() { super(K, M1, M2, M3); } /** Creates a new random number generator using a single int seed. * @param seed the initial seed (32 bits integer) */ public Well19937c(int seed) { super(K, M1, M2, M3, seed); } /** Creates a new random number generator using an int array seed. * @param seed the initial seed (32 bits integers array), if null * the seed of the generator will be related to the current time */ public Well19937c(int[] seed) { super(K, M1, M2, M3, seed); } /** Creates a new random number generator using a single long seed. * @param seed the initial seed (64 bits integer) */ public Well19937c(long seed) { super(K, M1, M2, M3, seed); } /** {@inheritDoc} */ @Override protected int next(final int bits) { final int indexRm1 = iRm1[index]; final int indexRm2 = iRm2[index]; final int v0 = v[index]; final int vM1 = v[i1[index]]; final int vM2 = v[i2[index]]; final int vM3 = v[i3[index]]; final int z0 = (0x80000000 & v[indexRm1]) ^ (0x7FFFFFFF & v[indexRm2]); final int z1 = (v0 ^ (v0 << 25)) ^ (vM1 ^ (vM1 >>> 27)); final int z2 = (vM2 >>> 9) ^ (vM3 ^ (vM3 >>> 1)); final int z3 = z1 ^ z2; int z4 = z0 ^ (z1 ^ (z1 << 9)) ^ (z2 ^ (z2 << 21)) ^ (z3 ^ (z3 >>> 21)); v[index] = z3; v[indexRm1] = z4; v[indexRm2] &= 0x80000000; index = indexRm1; // add Matsumoto-Kurita tempering // to get a maximally-equidistributed generator z4 = z4 ^ ((z4 << 7) & 0xe46e1700); z4 = z4 ^ ((z4 << 15) & 0x9b868000); return z4 >>> (32 - bits); } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/random/RandomAdaptor.java100644 1750 1750 15253 12126627700 27423 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.random; import java.util.Random; /** * Extension of java.util.Random wrapping a * {@link RandomGenerator}. * * @since 1.1 * @version $Id: RandomAdaptor.java 1416643 2012-12-03 19:37:14Z tn $ */ public class RandomAdaptor extends Random implements RandomGenerator { /** Serializable version identifier. */ private static final long serialVersionUID = 2306581345647615033L; /** Wrapped randomGenerator instance */ private final RandomGenerator randomGenerator; /** * Prevent instantiation without a generator argument */ @SuppressWarnings("unused") private RandomAdaptor() { randomGenerator = null; } /** * Construct a RandomAdaptor wrapping the supplied RandomGenerator. * * @param randomGenerator the wrapped generator */ public RandomAdaptor(RandomGenerator randomGenerator) { this.randomGenerator = randomGenerator; } /** * Factory method to create a Random using the supplied * RandomGenerator. * * @param randomGenerator wrapped RandomGenerator instance * @return a Random instance wrapping the RandomGenerator */ public static Random createAdaptor(RandomGenerator randomGenerator) { return new RandomAdaptor(randomGenerator); } /** * Returns the next pseudorandom, uniformly distributed * boolean value from this random number generator's * sequence. * * @return the next pseudorandom, uniformly distributed * boolean value from this random number generator's * sequence */ @Override public boolean nextBoolean() { return randomGenerator.nextBoolean(); } /** * Generates random bytes and places them into a user-supplied * byte array. The number of random bytes produced is equal to * the length of the byte array. * * @param bytes the non-null byte array in which to put the * random bytes */ @Override public void nextBytes(byte[] bytes) { randomGenerator.nextBytes(bytes); } /** * Returns the next pseudorandom, uniformly distributed * double value between 0.0 and * 1.0 from this random number generator's sequence. * * @return the next pseudorandom, uniformly distributed * double value between 0.0 and * 1.0 from this random number generator's sequence */ @Override public double nextDouble() { return randomGenerator.nextDouble(); } /** * Returns the next pseudorandom, uniformly distributed float * value between 0.0 and 1.0 from this random * number generator's sequence. * * @return the next pseudorandom, uniformly distributed float * value between 0.0 and 1.0 from this * random number generator's sequence */ @Override public float nextFloat() { return randomGenerator.nextFloat(); } /** * Returns the next pseudorandom, Gaussian ("normally") distributed * double value with mean 0.0 and standard * deviation 1.0 from this random number generator's sequence. * * @return the next pseudorandom, Gaussian ("normally") distributed * double value with mean 0.0 and * standard deviation 1.0 from this random number * generator's sequence */ @Override public double nextGaussian() { return randomGenerator.nextGaussian(); } /** * Returns the next pseudorandom, uniformly distributed int * value from this random number generator's sequence. * All 232 possible int values * should be produced with (approximately) equal probability. * * @return the next pseudorandom, uniformly distributed int * value from this random number generator's sequence */ @Override public int nextInt() { return randomGenerator.nextInt(); } /** * Returns a pseudorandom, uniformly distributed int value * between 0 (inclusive) and the specified value (exclusive), drawn from * this random number generator's sequence. * * @param n the bound on the random number to be returned. Must be * positive. * @return a pseudorandom, uniformly distributed int * value between 0 (inclusive) and n (exclusive). * @throws IllegalArgumentException if n is not positive. */ @Override public int nextInt(int n) { return randomGenerator.nextInt(n); } /** * Returns the next pseudorandom, uniformly distributed long * value from this random number generator's sequence. All * 264 possible long values * should be produced with (approximately) equal probability. * * @return the next pseudorandom, uniformly distributed long *value from this random number generator's sequence */ @Override public long nextLong() { return randomGenerator.nextLong(); } /** {@inheritDoc} */ public void setSeed(int seed) { if (randomGenerator != null) { // required to avoid NPE in constructor randomGenerator.setSeed(seed); } } /** {@inheritDoc} */ public void setSeed(int[] seed) { if (randomGenerator != null) { // required to avoid NPE in constructor randomGenerator.setSeed(seed); } } /** {@inheritDoc} */ @Override public void setSeed(long seed) { if (randomGenerator != null) { // required to avoid NPE in constructor randomGenerator.setSeed(seed); } } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/random/package-info.java100644 1750 1750 21163 12126627700 27211 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ /** * *

                              Random number and random data generators.

                              *

                              Commons-math provides a few pseudo random number generators. The top level interface is RandomGenerator. * It is implemented by three classes: *

                                *
                              • {@link org.apache.commons.math3.random.JDKRandomGenerator JDKRandomGenerator} * that extends the JDK provided generator
                              • *
                              • AbstractRandomGenerator as a helper for users generators
                              • *
                              • BitStreamGenerator which is an abstract class for several generators and * which in turn is extended by: *
                                  *
                                • {@link org.apache.commons.math3.random.MersenneTwister MersenneTwister}
                                • *
                                • {@link org.apache.commons.math3.random.Well512a Well512a}
                                • *
                                • {@link org.apache.commons.math3.random.Well1024a Well1024a}
                                • *
                                • {@link org.apache.commons.math3.random.Well19937a Well19937a}
                                • *
                                • {@link org.apache.commons.math3.random.Well19937c Well19937c}
                                • *
                                • {@link org.apache.commons.math3.random.Well44497a Well44497a}
                                • *
                                • {@link org.apache.commons.math3.random.Well44497b Well44497b}
                                • *
                                *
                              • *
                              *

                              * *

                              * The JDK provided generator is a simple one that can be used only for very simple needs. * The Mersenne Twister is a fast generator with very good properties well suited for * Monte-Carlo simulation. It is equidistributed for generating vectors up to dimension 623 * and has a huge period: 219937 - 1 (which is a Mersenne prime). This generator * is described in a paper by Makoto Matsumoto and Takuji Nishimura in 1998: Mersenne Twister: * A 623-Dimensionally Equidistributed Uniform Pseudo-Random Number Generator, ACM * Transactions on Modeling and Computer Simulation, Vol. 8, No. 1, January 1998, pp 3--30. * The WELL generators are a family of generators with period ranging from 2512 - 1 * to 244497 - 1 (this last one is also a Mersenne prime) with even better properties * than Mersenne Twister. These generators are described in a paper by François Panneton, * Pierre L'Ecuyer and Makoto Matsumoto Improved Long-Period * Generators Based on Linear Recurrences Modulo 2 ACM Transactions on Mathematical Software, * 32, 1 (2006). The errata for the paper are in wellrng-errata.txt. *

                              * *

                              * For simple sampling, any of these generators is sufficient. For Monte-Carlo simulations the * JDK generator does not have any of the good mathematical properties of the other generators, * so it should be avoided. The Mersenne twister and WELL generators have equidistribution properties * proven according to their bits pool size which is directly linked to their period (all of them * have maximal period, i.e. a generator with size n pool has a period 2n-1). They also * have equidistribution properties for 32 bits blocks up to s/32 dimension where s is their pool size. * So WELL19937c for exemple is equidistributed up to dimension 623 (19937/32). This means a Monte-Carlo * simulation generating a vector of n variables at each iteration has some guarantees on the properties * of the vector as long as its dimension does not exceed the limit. However, since we use bits from two * successive 32 bits generated integers to create one double, this limit is smaller when the variables are * of type double. so for Monte-Carlo simulation where less the 16 doubles are generated at each round, * WELL1024 may be sufficient. If a larger number of doubles are needed a generator with a larger pool * would be useful. *

                              * *

                              * The WELL generators are more modern then MersenneTwister (the paper describing than has been published * in 2006 instead of 1998) and fix some of its (few) drawbacks. If initialization array contains many * zero bits, MersenneTwister may take a very long time (several hundreds of thousands of iterations to * reach a steady state with a balanced number of zero and one in its bits pool). So the WELL generators * are better to escape zeroland as explained by the WELL generators creators. The Well19937a and * Well44497a generator are not maximally equidistributed (i.e. there are some dimensions or bits blocks * size for which they are not equidistributed). The Well512a, Well1024a, Well19937c and Well44497b are * maximally equidistributed for blocks size up to 32 bits (they should behave correctly also for double * based on more than 32 bits blocks, but equidistribution is not proven at these blocks sizes). *

                              * *

                              * The MersenneTwister generator uses a 624 elements integer array, so it consumes less than 2.5 kilobytes. * The WELL generators use 6 integer arrays with a size equal to the pool size, so for example the * WELL44497b generator uses about 33 kilobytes. This may be important if a very large number of * generator instances were used at the same time. *

                              * *

                              * All generators are quite fast. As an example, here are some comparisons, obtained on a 64 bits JVM on a * linux computer with a 2008 processor (AMD phenom Quad 9550 at 2.2 GHz). The generation rate for * MersenneTwister was about 27 millions doubles per second (remember we generate two 32 bits integers for * each double). Generation rates for other PRNG, relative to MersenneTwister: *

                              * *

                              * * * * * * * * * * * *
                              Example of performances
                              Namegeneration rate (relative to MersenneTwister)
                              {@link org.apache.commons.math3.random.MersenneTwister MersenneTwister}1
                              {@link org.apache.commons.math3.random.JDKRandomGenerator JDKRandomGenerator}between 0.96 and 1.16
                              {@link org.apache.commons.math3.random.Well512a Well512a}between 0.85 and 0.88
                              {@link org.apache.commons.math3.random.Well1024a Well1024a}between 0.63 and 0.73
                              {@link org.apache.commons.math3.random.Well19937a Well19937a}between 0.70 and 0.71
                              {@link org.apache.commons.math3.random.Well19937c Well19937c}between 0.57 and 0.71
                              {@link org.apache.commons.math3.random.Well44497a Well44497a}between 0.69 and 0.71
                              {@link org.apache.commons.math3.random.Well44497b Well44497b}between 0.65 and 0.71
                              *

                              * *

                              * So for most simulation problems, the better generators like {@link * org.apache.commons.math3.random.Well19937c Well19937c} and {@link * org.apache.commons.math3.random.Well44497b Well44497b} are probably very good choices. *

                              * *

                              * Note that none of these generators are suitable for cryptography. They are devoted * to simulation, and to generate very long series with strong properties on the series as a whole * (equidistribution, no correlation ...). They do not attempt to create small series but with * very strong properties of unpredictability as needed in cryptography. *

                              * * */ package org.apache.commons.math3.random; commons-math3-3.2-src/src/main/java/org/apache/commons/math3/random/AbstractRandomGenerator.java100644 1750 1750 23071 12126627700 31440 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.random; import org.apache.commons.math3.exception.NotStrictlyPositiveException; import org.apache.commons.math3.util.FastMath; /** * Abstract class implementing the {@link RandomGenerator} interface. * Default implementations for all methods other than {@link #nextDouble()} and * {@link #setSeed(long)} are provided. *

                              * All data generation methods are based on {@code code nextDouble()}. * Concrete implementations must override * this method and should provide better / more * performant implementations of the other methods if the underlying PRNG * supplies them.

                              * * @since 1.1 * @version $Id: AbstractRandomGenerator.java 1416643 2012-12-03 19:37:14Z tn $ */ public abstract class AbstractRandomGenerator implements RandomGenerator { /** * Cached random normal value. The default implementation for * {@link #nextGaussian} generates pairs of values and this field caches the * second value so that the full algorithm is not executed for every * activation. The value {@code Double.NaN} signals that there is * no cached value. Use {@link #clear} to clear the cached value. */ private double cachedNormalDeviate = Double.NaN; /** * Construct a RandomGenerator. */ public AbstractRandomGenerator() { super(); } /** * Clears the cache used by the default implementation of * {@link #nextGaussian}. Implementations that do not override the * default implementation of {@code nextGaussian} should call this * method in the implementation of {@link #setSeed(long)} */ public void clear() { cachedNormalDeviate = Double.NaN; } /** {@inheritDoc} */ public void setSeed(int seed) { setSeed((long) seed); } /** {@inheritDoc} */ public void setSeed(int[] seed) { // the following number is the largest prime that fits in 32 bits (it is 2^32 - 5) final long prime = 4294967291l; long combined = 0l; for (int s : seed) { combined = combined * prime + s; } setSeed(combined); } /** * Sets the seed of the underlying random number generator using a * {@code long} seed. Sequences of values generated starting with the * same seeds should be identical. *

                              * Implementations that do not override the default implementation of * {@code nextGaussian} should include a call to {@link #clear} in the * implementation of this method.

                              * * @param seed the seed value */ public abstract void setSeed(long seed); /** * Generates random bytes and places them into a user-supplied * byte array. The number of random bytes produced is equal to * the length of the byte array. *

                              * The default implementation fills the array with bytes extracted from * random integers generated using {@link #nextInt}.

                              * * @param bytes the non-null byte array in which to put the * random bytes */ public void nextBytes(byte[] bytes) { int bytesOut = 0; while (bytesOut < bytes.length) { int randInt = nextInt(); for (int i = 0; i < 3; i++) { if ( i > 0) { randInt = randInt >> 8; } bytes[bytesOut++] = (byte) randInt; if (bytesOut == bytes.length) { return; } } } } /** * Returns the next pseudorandom, uniformly distributed {@code int} * value from this random number generator's sequence. * All 232 possible {@code int} values * should be produced with (approximately) equal probability. *

                              * The default implementation provided here returns *

                                   * (int) (nextDouble() * Integer.MAX_VALUE)
                                   * 

                              * * @return the next pseudorandom, uniformly distributed {@code int} * value from this random number generator's sequence */ public int nextInt() { return (int) ((2d * nextDouble() - 1d) * Integer.MAX_VALUE); } /** * Returns a pseudorandom, uniformly distributed {@code int} value * between 0 (inclusive) and the specified value (exclusive), drawn from * this random number generator's sequence. *

                              * The default implementation returns *

                                   * (int) (nextDouble() * n
                                   * 

                              * * @param n the bound on the random number to be returned. Must be * positive. * @return a pseudorandom, uniformly distributed {@code int} * value between 0 (inclusive) and n (exclusive). * @throws NotStrictlyPositiveException if {@code n <= 0}. */ public int nextInt(int n) { if (n <= 0 ) { throw new NotStrictlyPositiveException(n); } int result = (int) (nextDouble() * n); return result < n ? result : n - 1; } /** * Returns the next pseudorandom, uniformly distributed {@code long} * value from this random number generator's sequence. All * 264 possible {@code long} values * should be produced with (approximately) equal probability. *

                              * The default implementation returns *

                                   * (long) (nextDouble() * Long.MAX_VALUE)
                                   * 

                              * * @return the next pseudorandom, uniformly distributed {@code long} *value from this random number generator's sequence */ public long nextLong() { return (long) ((2d * nextDouble() - 1d) * Long.MAX_VALUE); } /** * Returns the next pseudorandom, uniformly distributed * {@code boolean} value from this random number generator's * sequence. *

                              * The default implementation returns *

                                   * nextDouble() <= 0.5
                                   * 

                              * * @return the next pseudorandom, uniformly distributed * {@code boolean} value from this random number generator's * sequence */ public boolean nextBoolean() { return nextDouble() <= 0.5; } /** * Returns the next pseudorandom, uniformly distributed {@code float} * value between {@code 0.0} and {@code 1.0} from this random * number generator's sequence. *

                              * The default implementation returns *

                                   * (float) nextDouble() 
                                   * 

                              * * @return the next pseudorandom, uniformly distributed {@code float} * value between {@code 0.0} and {@code 1.0} from this * random number generator's sequence */ public float nextFloat() { return (float) nextDouble(); } /** * Returns the next pseudorandom, uniformly distributed * {@code double} value between {@code 0.0} and * {@code 1.0} from this random number generator's sequence. *

                              * This method provides the underlying source of random data used by the * other methods.

                              * * @return the next pseudorandom, uniformly distributed * {@code double} value between {@code 0.0} and * {@code 1.0} from this random number generator's sequence */ public abstract double nextDouble(); /** * Returns the next pseudorandom, Gaussian ("normally") distributed * {@code double} value with mean {@code 0.0} and standard * deviation {@code 1.0} from this random number generator's sequence. *

                              * The default implementation uses the Polar Method * due to G.E.P. Box, M.E. Muller and G. Marsaglia, as described in * D. Knuth, The Art of Computer Programming, 3.4.1C.

                              *

                              * The algorithm generates a pair of independent random values. One of * these is cached for reuse, so the full algorithm is not executed on each * activation. Implementations that do not override this method should * make sure to call {@link #clear} to clear the cached value in the * implementation of {@link #setSeed(long)}.

                              * * @return the next pseudorandom, Gaussian ("normally") distributed * {@code double} value with mean {@code 0.0} and * standard deviation {@code 1.0} from this random number * generator's sequence */ public double nextGaussian() { if (!Double.isNaN(cachedNormalDeviate)) { double dev = cachedNormalDeviate; cachedNormalDeviate = Double.NaN; return dev; } double v1 = 0; double v2 = 0; double s = 1; while (s >=1 ) { v1 = 2 * nextDouble() - 1; v2 = 2 * nextDouble() - 1; s = v1 * v1 + v2 * v2; } if (s != 0) { s = FastMath.sqrt(-2 * FastMath.log(s) / s); } cachedNormalDeviate = v2 * s; return v1 * s; } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/random/NormalizedRandomGenerator.java100644 1750 1750 3053 12126627700 31757 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.random; /** * This interface represent a normalized random generator for * scalars. * Normalized generator provide null mean and unit standard deviation scalars. * @version $Id: NormalizedRandomGenerator.java 1416643 2012-12-03 19:37:14Z tn $ * @since 1.2 */ public interface NormalizedRandomGenerator { /** Generate a random scalar with null mean and unit standard deviation. *

                              This method does not specify the shape of the * distribution, it is the implementing class that provides it. The * only contract here is to generate numbers with null mean and unit * standard deviation.

                              * @return a random scalar with null mean and unit standard deviation */ double nextNormalizedDouble(); } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/random/Well44497b.java100644 1750 1750 10523 12126627700 26344 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.random; /** This class implements the WELL44497b pseudo-random number generator * from François Panneton, Pierre L'Ecuyer and Makoto Matsumoto. *

                              This generator is described in a paper by François Panneton, * Pierre L'Ecuyer and Makoto Matsumoto Improved * Long-Period Generators Based on Linear Recurrences Modulo 2 ACM * Transactions on Mathematical Software, 32, 1 (2006). The errata for the paper * are in wellrng-errata.txt.

                              * @see WELL Random number generator * @version $Id: Well44497b.java 1416643 2012-12-03 19:37:14Z tn $ * @since 2.2 */ public class Well44497b extends AbstractWell { /** Serializable version identifier. */ private static final long serialVersionUID = 4032007538246675492L; /** Number of bits in the pool. */ private static final int K = 44497; /** First parameter of the algorithm. */ private static final int M1 = 23; /** Second parameter of the algorithm. */ private static final int M2 = 481; /** Third parameter of the algorithm. */ private static final int M3 = 229; /** Creates a new random number generator. *

                              The instance is initialized using the current time as the * seed.

                              */ public Well44497b() { super(K, M1, M2, M3); } /** Creates a new random number generator using a single int seed. * @param seed the initial seed (32 bits integer) */ public Well44497b(int seed) { super(K, M1, M2, M3, seed); } /** Creates a new random number generator using an int array seed. * @param seed the initial seed (32 bits integers array), if null * the seed of the generator will be related to the current time */ public Well44497b(int[] seed) { super(K, M1, M2, M3, seed); } /** Creates a new random number generator using a single long seed. * @param seed the initial seed (64 bits integer) */ public Well44497b(long seed) { super(K, M1, M2, M3, seed); } /** {@inheritDoc} */ @Override protected int next(final int bits) { // compute raw value given by WELL44497a generator // which is NOT maximally-equidistributed final int indexRm1 = iRm1[index]; final int indexRm2 = iRm2[index]; final int v0 = v[index]; final int vM1 = v[i1[index]]; final int vM2 = v[i2[index]]; final int vM3 = v[i3[index]]; // the values below include the errata of the original article final int z0 = (0xFFFF8000 & v[indexRm1]) ^ (0x00007FFF & v[indexRm2]); final int z1 = (v0 ^ (v0 << 24)) ^ (vM1 ^ (vM1 >>> 30)); final int z2 = (vM2 ^ (vM2 << 10)) ^ (vM3 << 26); final int z3 = z1 ^ z2; final int z2Prime = ((z2 << 9) ^ (z2 >>> 23)) & 0xfbffffff; final int z2Second = ((z2 & 0x00020000) != 0) ? (z2Prime ^ 0xb729fcec) : z2Prime; int z4 = z0 ^ (z1 ^ (z1 >>> 20)) ^ z2Second ^ z3; v[index] = z3; v[indexRm1] = z4; v[indexRm2] &= 0xFFFF8000; index = indexRm1; // add Matsumoto-Kurita tempering // to get a maximally-equidistributed generator z4 = z4 ^ ((z4 << 7) & 0x93dd1400); z4 = z4 ^ ((z4 << 15) & 0xfa118000); return z4 >>> (32 - bits); } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/random/ISAACRandom.java100644 1750 1750 20103 12126627700 26637 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.random; import java.io.Serializable; /** * * ISAAC: a fast cryptographic pseudo-random number generator *
                              * ISAAC (Indirection, Shift, Accumulate, Add, and Count) generates 32-bit * random numbers. * ISAAC has been designed to be cryptographically secure and is inspired * by RC4. * Cycles are guaranteed to be at least 240 values long, and they * are 28295 values long on average. * The results are uniformly distributed, unbiased, and unpredictable unless * you know the seed. *
                              * This code is based (with minor changes and improvements) on the original * implementation of the algorithm by Bob Jenkins. *
                              * * @version $Id: ISAACRandom.java 1416643 2012-12-03 19:37:14Z tn $ * @since 3.0 */ public class ISAACRandom extends BitsStreamGenerator implements Serializable { /** Serializable version identifier */ private static final long serialVersionUID = 7288197941165002400L; /** Log of size of rsl[] and mem[] */ private static final int SIZE_L = 8; /** Size of rsl[] and mem[] */ private static final int SIZE = 1 << SIZE_L; /** Half-size of rsl[] and mem[] */ private static final int H_SIZE = SIZE >> 1; /** For pseudo-random lookup */ private static final int MASK = SIZE - 1 << 2; /** The golden ratio */ private static final int GLD_RATIO = 0x9e3779b9; /** The results given to the user */ private final int[] rsl = new int[SIZE]; /** The internal state */ private final int[] mem = new int[SIZE]; /** Count through the results in rsl[] */ private int count; /** Accumulator */ private int isaacA; /** The last result */ private int isaacB; /** Counter, guarantees cycle is at least 2^40 */ private int isaacC; /** Service variable. */ private final int[] arr = new int[8]; /** Service variable. */ private int isaacX; /** Service variable. */ private int isaacI; /** Service variable. */ private int isaacJ; /** * Creates a new ISAAC random number generator. *
                              * The instance is initialized using a combination of the * current time and system hash code of the instance as the seed. */ public ISAACRandom() { setSeed(System.currentTimeMillis() + System.identityHashCode(this)); } /** * Creates a new ISAAC random number generator using a single long seed. * * @param seed Initial seed. */ public ISAACRandom(long seed) { setSeed(seed); } /** * Creates a new ISAAC random number generator using an int array seed. * * @param seed Initial seed. If {@code null}, the seed will be related * to the current time. */ public ISAACRandom(int[] seed) { setSeed(seed); } /** {@inheritDoc} */ @Override public void setSeed(int seed) { setSeed(new int[]{seed}); } /** {@inheritDoc} */ @Override public void setSeed(long seed) { setSeed(new int[]{(int) (seed >>> 32), (int) (seed & 0xffffffffL)}); } /** {@inheritDoc} */ @Override public void setSeed(int[] seed) { if (seed == null) { setSeed(System.currentTimeMillis() + System.identityHashCode(this)); return; } final int seedLen = seed.length; final int rslLen = rsl.length; System.arraycopy(seed, 0, rsl, 0, Math.min(seedLen, rslLen)); if (seedLen < rslLen) { for (int j = seedLen; j < rslLen; j++) { long k = rsl[j - seedLen]; rsl[j] = (int) (0x6c078965L * (k ^ k >> 30) + j & 0xffffffffL); } } initState(); } /** {@inheritDoc} */ @Override protected int next(int bits) { if (count < 0) { isaac(); count = SIZE - 1; } return rsl[count--] >>> 32 - bits; } /** Generate 256 results */ private void isaac() { isaacI = 0; isaacJ = H_SIZE; isaacB += ++isaacC; while (isaacI < H_SIZE) { isaac2(); } isaacJ = 0; while (isaacJ < H_SIZE) { isaac2(); } } /** Intermediate internal loop. */ private void isaac2() { isaacX = mem[isaacI]; isaacA ^= isaacA << 13; isaacA += mem[isaacJ++]; isaac3(); isaacX = mem[isaacI]; isaacA ^= isaacA >>> 6; isaacA += mem[isaacJ++]; isaac3(); isaacX = mem[isaacI]; isaacA ^= isaacA << 2; isaacA += mem[isaacJ++]; isaac3(); isaacX = mem[isaacI]; isaacA ^= isaacA >>> 16; isaacA += mem[isaacJ++]; isaac3(); } /** Lowest level internal loop. */ private void isaac3() { mem[isaacI] = mem[(isaacX & MASK) >> 2] + isaacA + isaacB; isaacB = mem[(mem[isaacI] >> SIZE_L & MASK) >> 2] + isaacX; rsl[isaacI++] = isaacB; } /** Initialize, or reinitialize, this instance of rand. */ private void initState() { isaacA = 0; isaacB = 0; isaacC = 0; for (int j = 0; j < arr.length; j++) { arr[j] = GLD_RATIO; } for (int j = 0; j < 4; j++) { shuffle(); } // fill in mem[] with messy stuff for (int j = 0; j < SIZE; j += 8) { arr[0] += rsl[j]; arr[1] += rsl[j + 1]; arr[2] += rsl[j + 2]; arr[3] += rsl[j + 3]; arr[4] += rsl[j + 4]; arr[5] += rsl[j + 5]; arr[6] += rsl[j + 6]; arr[7] += rsl[j + 7]; shuffle(); setState(j); } // second pass makes all of seed affect all of mem for (int j = 0; j < SIZE; j += 8) { arr[0] += mem[j]; arr[1] += mem[j + 1]; arr[2] += mem[j + 2]; arr[3] += mem[j + 3]; arr[4] += mem[j + 4]; arr[5] += mem[j + 5]; arr[6] += mem[j + 6]; arr[7] += mem[j + 7]; shuffle(); setState(j); } isaac(); count = SIZE - 1; clear(); } /** Shuffle array. */ private void shuffle() { arr[0] ^= arr[1] << 11; arr[3] += arr[0]; arr[1] += arr[2]; arr[1] ^= arr[2] >>> 2; arr[4] += arr[1]; arr[2] += arr[3]; arr[2] ^= arr[3] << 8; arr[5] += arr[2]; arr[3] += arr[4]; arr[3] ^= arr[4] >>> 16; arr[6] += arr[3]; arr[4] += arr[5]; arr[4] ^= arr[5] << 10; arr[7] += arr[4]; arr[5] += arr[6]; arr[5] ^= arr[6] >>> 4; arr[0] += arr[5]; arr[6] += arr[7]; arr[6] ^= arr[7] << 8; arr[1] += arr[6]; arr[7] += arr[0]; arr[7] ^= arr[0] >>> 9; arr[2] += arr[7]; arr[0] += arr[1]; } /** Set the state by copying the internal arrays. * * @param start First index into {@link #mem} array. */ private void setState(int start) { mem[start] = arr[0]; mem[start + 1] = arr[1]; mem[start + 2] = arr[2]; mem[start + 3] = arr[3]; mem[start + 4] = arr[4]; mem[start + 5] = arr[5]; mem[start + 6] = arr[6]; mem[start + 7] = arr[7]; } } ././@LongLink100644 0 0 145 12126630647 10262 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/random/SynchronizedRandomGenerator.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/random/SynchronizedRandomGenerator.java100644 1750 1750 6205 12126627700 32334 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.random; /** * Any {@link RandomGenerator} implementation can be thread-safe if it * is used through an instance of this class. * This is achieved by enclosing calls to the methods of the actual * generator inside the overridden {@code synchronized} methods of this * class. * * @since 3.1 * @version $Id: SynchronizedRandomGenerator.java 1416643 2012-12-03 19:37:14Z tn $ */ public class SynchronizedRandomGenerator implements RandomGenerator { /** Object to which all calls will be delegated. */ private final RandomGenerator wrapped; /** * Creates a synchronized wrapper for the given {@code RandomGenerator} * instance. * * @param rng Generator whose methods will be called through * their corresponding overridden synchronized version. * To ensure thread-safety, the wrapped generator must * not be used directly. */ public SynchronizedRandomGenerator(RandomGenerator rng) { wrapped = rng; } /** * {@inheritDoc} */ public synchronized void setSeed(int seed) { wrapped.setSeed(seed); } /** * {@inheritDoc} */ public synchronized void setSeed(int[] seed) { wrapped.setSeed(seed); } /** * {@inheritDoc} */ public synchronized void setSeed(long seed) { wrapped.setSeed(seed); } /** * {@inheritDoc} */ public synchronized void nextBytes(byte[] bytes) { wrapped.nextBytes(bytes); } /** * {@inheritDoc} */ public synchronized int nextInt() { return wrapped.nextInt(); } /** * {@inheritDoc} */ public synchronized int nextInt(int n) { return wrapped.nextInt(n); } /** * {@inheritDoc} */ public synchronized long nextLong() { return wrapped.nextLong(); } /** * {@inheritDoc} */ public synchronized boolean nextBoolean() { return wrapped.nextBoolean(); } /** * {@inheritDoc} */ public synchronized float nextFloat() { return wrapped.nextFloat(); } /** * {@inheritDoc} */ public synchronized double nextDouble() { return wrapped.nextDouble(); } /** * {@inheritDoc} */ public synchronized double nextGaussian() { return wrapped.nextGaussian(); } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/random/RandomVectorGenerator.java100644 1750 1750 2242 12126627700 31114 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.random; /** This interface represents a random generator for whole vectors. * * @since 1.2 * @version $Id: RandomVectorGenerator.java 1416643 2012-12-03 19:37:14Z tn $ * */ public interface RandomVectorGenerator { /** Generate a random vector. * @return a random vector as an array of double. */ double[] nextVector(); } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/random/JDKRandomGenerator.java100644 1750 1750 3235 12126627700 30265 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.random; import java.util.Random; /** * Extension of java.util.Random to implement * {@link RandomGenerator}. * * @since 1.1 * @version $Id: JDKRandomGenerator.java 1416643 2012-12-03 19:37:14Z tn $ */ public class JDKRandomGenerator extends Random implements RandomGenerator { /** Serializable version identifier. */ private static final long serialVersionUID = -7745277476784028798L; /** {@inheritDoc} */ public void setSeed(int seed) { setSeed((long) seed); } /** {@inheritDoc} */ public void setSeed(int[] seed) { // the following number is the largest prime that fits in 32 bits (it is 2^32 - 5) final long prime = 4294967291l; long combined = 0l; for (int s : seed) { combined = combined * prime + s; } setSeed(combined); } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/random/RandomData.java100644 1750 1750 27053 12126627700 26703 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.random; import java.util.Collection; import org.apache.commons.math3.exception.NotANumberException; import org.apache.commons.math3.exception.NotFiniteNumberException; import org.apache.commons.math3.exception.NotStrictlyPositiveException; import org.apache.commons.math3.exception.NumberIsTooLargeException; /** * Random data generation utilities. * @deprecated to be removed in 4.0. Use {@link RandomDataGenerator} directly * @version $Id: RandomData.java 1416643 2012-12-03 19:37:14Z tn $ */ public interface RandomData { /** * Generates a random string of hex characters of length {@code len}. *

                              * The generated string will be random, but not cryptographically * secure. To generate cryptographically secure strings, use * {@link #nextSecureHexString(int)}. *

                              * * @param len the length of the string to be generated * @return a random string of hex characters of length {@code len} * @throws NotStrictlyPositiveException * if {@code len <= 0} */ String nextHexString(int len) throws NotStrictlyPositiveException; /** * Generates a uniformly distributed random integer between {@code lower} * and {@code upper} (endpoints included). *

                              * The generated integer will be random, but not cryptographically secure. * To generate cryptographically secure integer sequences, use * {@link #nextSecureInt(int, int)}. *

                              * * @param lower lower bound for generated integer * @param upper upper bound for generated integer * @return a random integer greater than or equal to {@code lower} * and less than or equal to {@code upper} * @throws NumberIsTooLargeException if {@code lower >= upper} */ int nextInt(int lower, int upper) throws NumberIsTooLargeException; /** * Generates a uniformly distributed random long integer between * {@code lower} and {@code upper} (endpoints included). *

                              * The generated long integer values will be random, but not * cryptographically secure. To generate cryptographically secure sequences * of longs, use {@link #nextSecureLong(long, long)}. *

                              * * @param lower lower bound for generated long integer * @param upper upper bound for generated long integer * @return a random long integer greater than or equal to {@code lower} and * less than or equal to {@code upper} * @throws NumberIsTooLargeException if {@code lower >= upper} */ long nextLong(long lower, long upper) throws NumberIsTooLargeException; /** * Generates a random string of hex characters from a secure random * sequence. *

                              * If cryptographic security is not required, use * {@link #nextHexString(int)}. *

                              * * @param len the length of the string to be generated * @return a random string of hex characters of length {@code len} * @throws NotStrictlyPositiveException if {@code len <= 0} */ String nextSecureHexString(int len) throws NotStrictlyPositiveException; /** * Generates a uniformly distributed random integer between {@code lower} * and {@code upper} (endpoints included) from a secure random sequence. *

                              * Sequences of integers generated using this method will be * cryptographically secure. If cryptographic security is not required, * {@link #nextInt(int, int)} should be used instead of this method.

                              *

                              * Definition: * * Secure Random Sequence

                              * * @param lower lower bound for generated integer * @param upper upper bound for generated integer * @return a random integer greater than or equal to {@code lower} and less * than or equal to {@code upper}. * @throws NumberIsTooLargeException if {@code lower >= upper}. */ int nextSecureInt(int lower, int upper) throws NumberIsTooLargeException; /** * Generates a uniformly distributed random long integer between * {@code lower} and {@code upper} (endpoints included) from a secure random * sequence. *

                              * Sequences of long values generated using this method will be * cryptographically secure. If cryptographic security is not required, * {@link #nextLong(long, long)} should be used instead of this method.

                              *

                              * Definition: * * Secure Random Sequence

                              * * @param lower lower bound for generated integer * @param upper upper bound for generated integer * @return a random long integer greater than or equal to {@code lower} and * less than or equal to {@code upper}. * @throws NumberIsTooLargeException if {@code lower >= upper}. */ long nextSecureLong(long lower, long upper) throws NumberIsTooLargeException; /** * Generates a random value from the Poisson distribution with the given * mean. *

                              * Definition: * * Poisson Distribution

                              * * @param mean the mean of the Poisson distribution * @return a random value following the specified Poisson distribution * @throws NotStrictlyPositiveException if {@code mean <= 0}. */ long nextPoisson(double mean) throws NotStrictlyPositiveException; /** * Generates a random value from the Normal (or Gaussian) distribution with * specified mean and standard deviation. *

                              * Definition: * * Normal Distribution

                              * * @param mu the mean of the distribution * @param sigma the standard deviation of the distribution * @return a random value following the specified Gaussian distribution * @throws NotStrictlyPositiveException if {@code sigma <= 0}. */ double nextGaussian(double mu, double sigma) throws NotStrictlyPositiveException; /** * Generates a random value from the exponential distribution * with specified mean. *

                              * Definition: * * Exponential Distribution

                              * * @param mean the mean of the distribution * @return a random value following the specified exponential distribution * @throws NotStrictlyPositiveException if {@code mean <= 0}. */ double nextExponential(double mean) throws NotStrictlyPositiveException; /** * Generates a uniformly distributed random value from the open interval * {@code (lower, upper)} (i.e., endpoints excluded). *

                              * Definition: * * Uniform Distribution {@code lower} and {@code upper - lower} are the * * location and scale parameters, respectively.

                              * * @param lower the exclusive lower bound of the support * @param upper the exclusive upper bound of the support * @return a uniformly distributed random value between lower and upper * (exclusive) * @throws NumberIsTooLargeException if {@code lower >= upper} * @throws NotFiniteNumberException if one of the bounds is infinite * @throws NotANumberException if one of the bounds is NaN */ double nextUniform(double lower, double upper) throws NumberIsTooLargeException, NotFiniteNumberException, NotANumberException; /** * Generates a uniformly distributed random value from the interval * {@code (lower, upper)} or the interval {@code [lower, upper)}. The lower * bound is thus optionally included, while the upper bound is always * excluded. *

                              * Definition: * * Uniform Distribution {@code lower} and {@code upper - lower} are the * * location and scale parameters, respectively.

                              * * @param lower the lower bound of the support * @param upper the exclusive upper bound of the support * @param lowerInclusive {@code true} if the lower bound is inclusive * @return uniformly distributed random value in the {@code (lower, upper)} * interval, if {@code lowerInclusive} is {@code false}, or in the * {@code [lower, upper)} interval, if {@code lowerInclusive} is * {@code true} * @throws NumberIsTooLargeException if {@code lower >= upper} * @throws NotFiniteNumberException if one of the bounds is infinite * @throws NotANumberException if one of the bounds is NaN */ double nextUniform(double lower, double upper, boolean lowerInclusive) throws NumberIsTooLargeException, NotFiniteNumberException, NotANumberException; /** * Generates an integer array of length {@code k} whose entries are selected * randomly, without repetition, from the integers {@code 0, ..., n - 1} * (inclusive). *

                              * Generated arrays represent permutations of {@code n} taken {@code k} at a * time.

                              * * @param n the domain of the permutation * @param k the size of the permutation * @return a random {@code k}-permutation of {@code n}, as an array of * integers * @throws NumberIsTooLargeException if {@code k > n}. * @throws NotStrictlyPositiveException if {@code k <= 0}. */ int[] nextPermutation(int n, int k) throws NumberIsTooLargeException, NotStrictlyPositiveException; /** * Returns an array of {@code k} objects selected randomly from the * Collection {@code c}. *

                              * Sampling from {@code c} is without replacement; but if {@code c} contains * identical objects, the sample may include repeats. If all elements of * {@code c} are distinct, the resulting object array represents a * * Simple Random Sample of size {@code k} from the elements of * {@code c}.

                              * * @param c the collection to be sampled * @param k the size of the sample * @return a random sample of {@code k} elements from {@code c} * @throws NumberIsTooLargeException if {@code k > c.size()}. * @throws NotStrictlyPositiveException if {@code k <= 0}. */ Object[] nextSample(Collection c, int k) throws NumberIsTooLargeException, NotStrictlyPositiveException; } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/random/AbstractWell.java100644 1750 1750 17050 12126627700 27254 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.random; import java.io.Serializable; /** This abstract class implements the WELL class of pseudo-random number generator * from François Panneton, Pierre L'Ecuyer and Makoto Matsumoto. *

                              This generator is described in a paper by François Panneton, * Pierre L'Ecuyer and Makoto Matsumoto Improved * Long-Period Generators Based on Linear Recurrences Modulo 2 ACM * Transactions on Mathematical Software, 32, 1 (2006). The errata for the paper * are in wellrng-errata.txt.

                              * @see WELL Random number generator * @version $Id: AbstractWell.java 1416643 2012-12-03 19:37:14Z tn $ * @since 2.2 */ public abstract class AbstractWell extends BitsStreamGenerator implements Serializable { /** Serializable version identifier. */ private static final long serialVersionUID = -817701723016583596L; /** Current index in the bytes pool. */ protected int index; /** Bytes pool. */ protected final int[] v; /** Index indirection table giving for each index its predecessor taking table size into account. */ protected final int[] iRm1; /** Index indirection table giving for each index its second predecessor taking table size into account. */ protected final int[] iRm2; /** Index indirection table giving for each index the value index + m1 taking table size into account. */ protected final int[] i1; /** Index indirection table giving for each index the value index + m2 taking table size into account. */ protected final int[] i2; /** Index indirection table giving for each index the value index + m3 taking table size into account. */ protected final int[] i3; /** Creates a new random number generator. *

                              The instance is initialized using the current time plus the * system identity hash code of this instance as the seed.

                              * @param k number of bits in the pool (not necessarily a multiple of 32) * @param m1 first parameter of the algorithm * @param m2 second parameter of the algorithm * @param m3 third parameter of the algorithm */ protected AbstractWell(final int k, final int m1, final int m2, final int m3) { this(k, m1, m2, m3, null); } /** Creates a new random number generator using a single int seed. * @param k number of bits in the pool (not necessarily a multiple of 32) * @param m1 first parameter of the algorithm * @param m2 second parameter of the algorithm * @param m3 third parameter of the algorithm * @param seed the initial seed (32 bits integer) */ protected AbstractWell(final int k, final int m1, final int m2, final int m3, final int seed) { this(k, m1, m2, m3, new int[] { seed }); } /** Creates a new random number generator using an int array seed. * @param k number of bits in the pool (not necessarily a multiple of 32) * @param m1 first parameter of the algorithm * @param m2 second parameter of the algorithm * @param m3 third parameter of the algorithm * @param seed the initial seed (32 bits integers array), if null * the seed of the generator will be related to the current time */ protected AbstractWell(final int k, final int m1, final int m2, final int m3, final int[] seed) { // the bits pool contains k bits, k = r w - p where r is the number // of w bits blocks, w is the block size (always 32 in the original paper) // and p is the number of unused bits in the last block final int w = 32; final int r = (k + w - 1) / w; this.v = new int[r]; this.index = 0; // precompute indirection index tables. These tables are used for optimizing access // they allow saving computations like "(j + r - 2) % r" with costly modulo operations iRm1 = new int[r]; iRm2 = new int[r]; i1 = new int[r]; i2 = new int[r]; i3 = new int[r]; for (int j = 0; j < r; ++j) { iRm1[j] = (j + r - 1) % r; iRm2[j] = (j + r - 2) % r; i1[j] = (j + m1) % r; i2[j] = (j + m2) % r; i3[j] = (j + m3) % r; } // initialize the pool content setSeed(seed); } /** Creates a new random number generator using a single long seed. * @param k number of bits in the pool (not necessarily a multiple of 32) * @param m1 first parameter of the algorithm * @param m2 second parameter of the algorithm * @param m3 third parameter of the algorithm * @param seed the initial seed (64 bits integer) */ protected AbstractWell(final int k, final int m1, final int m2, final int m3, final long seed) { this(k, m1, m2, m3, new int[] { (int) (seed >>> 32), (int) (seed & 0xffffffffl) }); } /** Reinitialize the generator as if just built with the given int seed. *

                              The state of the generator is exactly the same as a new * generator built with the same seed.

                              * @param seed the initial seed (32 bits integer) */ @Override public void setSeed(final int seed) { setSeed(new int[] { seed }); } /** Reinitialize the generator as if just built with the given int array seed. *

                              The state of the generator is exactly the same as a new * generator built with the same seed.

                              * @param seed the initial seed (32 bits integers array). If null * the seed of the generator will be the system time plus the system identity * hash code of the instance. */ @Override public void setSeed(final int[] seed) { if (seed == null) { setSeed(System.currentTimeMillis() + System.identityHashCode(this)); return; } System.arraycopy(seed, 0, v, 0, Math.min(seed.length, v.length)); if (seed.length < v.length) { for (int i = seed.length; i < v.length; ++i) { final long l = v[i - seed.length]; v[i] = (int) ((1812433253l * (l ^ (l >> 30)) + i) & 0xffffffffL); } } index = 0; clear(); // Clear normal deviate cache } /** Reinitialize the generator as if just built with the given long seed. *

                              The state of the generator is exactly the same as a new * generator built with the same seed.

                              * @param seed the initial seed (64 bits integer) */ @Override public void setSeed(final long seed) { setSeed(new int[] { (int) (seed >>> 32), (int) (seed & 0xffffffffl) }); } /** {@inheritDoc} */ @Override protected abstract int next(final int bits); } ././@LongLink100644 0 0 151 12126630647 10257 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/random/UnitSphereRandomVectorGenerator.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/random/UnitSphereRandomVectorGenerator.100644 1750 1750 5204 12126627700 32262 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.random; import org.apache.commons.math3.util.FastMath; /** * Generate random vectors isotropically located on the surface of a sphere. * * @since 2.1 * @version $Id: UnitSphereRandomVectorGenerator.java 1444500 2013-02-10 08:10:40Z tn $ */ public class UnitSphereRandomVectorGenerator implements RandomVectorGenerator { /** * RNG used for generating the individual components of the vectors. */ private final RandomGenerator rand; /** * Space dimension. */ private final int dimension; /** * @param dimension Space dimension. * @param rand RNG for the individual components of the vectors. */ public UnitSphereRandomVectorGenerator(final int dimension, final RandomGenerator rand) { this.dimension = dimension; this.rand = rand; } /** * Create an object that will use a default RNG ({@link MersenneTwister}), * in order to generate the individual components. * * @param dimension Space dimension. */ public UnitSphereRandomVectorGenerator(final int dimension) { this(dimension, new MersenneTwister()); } /** {@inheritDoc} */ public double[] nextVector() { final double[] v = new double[dimension]; // See http://mathworld.wolfram.com/SpherePointPicking.html for example. // Pick a point by choosing a standard Gaussian for each element, and then // normalizing to unit length. double normSq = 0; for (int i = 0; i < dimension; i++) { final double comp = rand.nextGaussian(); v[i] = comp; normSq += comp * comp; } final double f = 1 / FastMath.sqrt(normSq); for (int i = 0; i < dimension; i++) { v[i] *= f; } return v; } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/random/BitsStreamGenerator.java100644 1750 1750 14626 12126627700 30617 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.random; import java.io.Serializable; import org.apache.commons.math3.exception.NotStrictlyPositiveException; import org.apache.commons.math3.util.FastMath; /** Base class for random number generators that generates bits streams. * * @version $Id: BitsStreamGenerator.java 1454897 2013-03-10 19:02:54Z luc $ * @since 2.0 */ public abstract class BitsStreamGenerator implements RandomGenerator, Serializable { /** Serializable version identifier */ private static final long serialVersionUID = 20130104L; /** Next gaussian. */ private double nextGaussian; /** * Creates a new random number generator. */ public BitsStreamGenerator() { nextGaussian = Double.NaN; } /** {@inheritDoc} */ public abstract void setSeed(int seed); /** {@inheritDoc} */ public abstract void setSeed(int[] seed); /** {@inheritDoc} */ public abstract void setSeed(long seed); /** Generate next pseudorandom number. *

                              This method is the core generation algorithm. It is used by all the * public generation methods for the various primitive types {@link * #nextBoolean()}, {@link #nextBytes(byte[])}, {@link #nextDouble()}, * {@link #nextFloat()}, {@link #nextGaussian()}, {@link #nextInt()}, * {@link #next(int)} and {@link #nextLong()}.

                              * @param bits number of random bits to produce * @return random bits generated */ protected abstract int next(int bits); /** {@inheritDoc} */ public boolean nextBoolean() { return next(1) != 0; } /** {@inheritDoc} */ public void nextBytes(byte[] bytes) { int i = 0; final int iEnd = bytes.length - 3; while (i < iEnd) { final int random = next(32); bytes[i] = (byte) (random & 0xff); bytes[i + 1] = (byte) ((random >> 8) & 0xff); bytes[i + 2] = (byte) ((random >> 16) & 0xff); bytes[i + 3] = (byte) ((random >> 24) & 0xff); i += 4; } int random = next(32); while (i < bytes.length) { bytes[i++] = (byte) (random & 0xff); random = random >> 8; } } /** {@inheritDoc} */ public double nextDouble() { final long high = ((long) next(26)) << 26; final int low = next(26); return (high | low) * 0x1.0p-52d; } /** {@inheritDoc} */ public float nextFloat() { return next(23) * 0x1.0p-23f; } /** {@inheritDoc} */ public double nextGaussian() { final double random; if (Double.isNaN(nextGaussian)) { // generate a new pair of gaussian numbers final double x = nextDouble(); final double y = nextDouble(); final double alpha = 2 * FastMath.PI * x; final double r = FastMath.sqrt(-2 * FastMath.log(y)); random = r * FastMath.cos(alpha); nextGaussian = r * FastMath.sin(alpha); } else { // use the second element of the pair already generated random = nextGaussian; nextGaussian = Double.NaN; } return random; } /** {@inheritDoc} */ public int nextInt() { return next(32); } /** * {@inheritDoc} *

                              This default implementation is copied from Apache Harmony * java.util.Random (r929253).

                              * *

                              Implementation notes:

                                *
                              • If n is a power of 2, this method returns * {@code (int) ((n * (long) next(31)) >> 31)}.
                              • * *
                              • If n is not a power of 2, what is returned is {@code next(31) % n} * with {@code next(31)} values rejected (i.e. regenerated) until a * value that is larger than the remainder of {@code Integer.MAX_VALUE / n} * is generated. Rejection of this initial segment is necessary to ensure * a uniform distribution.

                              */ public int nextInt(int n) throws IllegalArgumentException { if (n > 0) { if ((n & -n) == n) { return (int) ((n * (long) next(31)) >> 31); } int bits; int val; do { bits = next(31); val = bits % n; } while (bits - val + (n - 1) < 0); return val; } throw new NotStrictlyPositiveException(n); } /** {@inheritDoc} */ public long nextLong() { final long high = ((long) next(32)) << 32; final long low = ((long) next(32)) & 0xffffffffL; return high | low; } /** * Returns a pseudorandom, uniformly distributed long value * between 0 (inclusive) and the specified value (exclusive), drawn from * this random number generator's sequence. * * @param n the bound on the random number to be returned. Must be * positive. * @return a pseudorandom, uniformly distributed long * value between 0 (inclusive) and n (exclusive). * @throws IllegalArgumentException if n is not positive. */ public long nextLong(long n) throws IllegalArgumentException { if (n > 0) { long bits; long val; do { bits = ((long) next(31)) << 32; bits = bits | (((long) next(32)) & 0xffffffffL); val = bits % n; } while (bits - val + (n - 1) < 0); return val; } throw new NotStrictlyPositiveException(n); } /** * Clears the cache used by the default implementation of * {@link #nextGaussian}. */ public void clear() { nextGaussian = Double.NaN; } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/random/MersenneTwister.java100644 1750 1750 23754 12126627700 30033 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.random; import java.io.Serializable; import org.apache.commons.math3.util.FastMath; /** This class implements a powerful pseudo-random number generator * developed by Makoto Matsumoto and Takuji Nishimura during * 1996-1997. *

                              This generator features an extremely long period * (219937-1) and 623-dimensional equidistribution up to 32 * bits accuracy. The home page for this generator is located at * http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/emt.html.

                              *

                              This generator is described in a paper by Makoto Matsumoto and * Takuji Nishimura in 1998: Mersenne * Twister: A 623-Dimensionally Equidistributed Uniform Pseudo-Random * Number Generator, ACM Transactions on Modeling and Computer * Simulation, Vol. 8, No. 1, January 1998, pp 3--30

                              *

                              This class is mainly a Java port of the 2002-01-26 version of * the generator written in C by Makoto Matsumoto and Takuji * Nishimura. Here is their original copyright:

                              * * * * *
                              Copyright (C) 1997 - 2002, Makoto Matsumoto and Takuji Nishimura, * All rights reserved.
                              Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: *
                                *
                              1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer.
                              2. *
                              3. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution.
                              4. *
                              5. The names of its contributors may not be used to endorse or promote * products derived from this software without specific prior written * permission.
                              6. *
                              THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH * DAMAGE.
                              * @version $Id: MersenneTwister.java 1416643 2012-12-03 19:37:14Z tn $ * @since 2.0 */ public class MersenneTwister extends BitsStreamGenerator implements Serializable { /** Serializable version identifier. */ private static final long serialVersionUID = 8661194735290153518L; /** Size of the bytes pool. */ private static final int N = 624; /** Period second parameter. */ private static final int M = 397; /** X * MATRIX_A for X = {0, 1}. */ private static final int[] MAG01 = { 0x0, 0x9908b0df }; /** Bytes pool. */ private int[] mt; /** Current index in the bytes pool. */ private int mti; /** Creates a new random number generator. *

                              The instance is initialized using the current time plus the * system identity hash code of this instance as the seed.

                              */ public MersenneTwister() { mt = new int[N]; setSeed(System.currentTimeMillis() + System.identityHashCode(this)); } /** Creates a new random number generator using a single int seed. * @param seed the initial seed (32 bits integer) */ public MersenneTwister(int seed) { mt = new int[N]; setSeed(seed); } /** Creates a new random number generator using an int array seed. * @param seed the initial seed (32 bits integers array), if null * the seed of the generator will be related to the current time */ public MersenneTwister(int[] seed) { mt = new int[N]; setSeed(seed); } /** Creates a new random number generator using a single long seed. * @param seed the initial seed (64 bits integer) */ public MersenneTwister(long seed) { mt = new int[N]; setSeed(seed); } /** Reinitialize the generator as if just built with the given int seed. *

                              The state of the generator is exactly the same as a new * generator built with the same seed.

                              * @param seed the initial seed (32 bits integer) */ @Override public void setSeed(int seed) { // we use a long masked by 0xffffffffL as a poor man unsigned int long longMT = seed; // NB: unlike original C code, we are working with java longs, the cast below makes masking unnecessary mt[0]= (int) longMT; for (mti = 1; mti < N; ++mti) { // See Knuth TAOCP Vol2. 3rd Ed. P.106 for multiplier. // initializer from the 2002-01-09 C version by Makoto Matsumoto longMT = (1812433253l * (longMT ^ (longMT >> 30)) + mti) & 0xffffffffL; mt[mti]= (int) longMT; } clear(); // Clear normal deviate cache } /** Reinitialize the generator as if just built with the given int array seed. *

                              The state of the generator is exactly the same as a new * generator built with the same seed.

                              * @param seed the initial seed (32 bits integers array), if null * the seed of the generator will be the current system time plus the * system identity hash code of this instance */ @Override public void setSeed(int[] seed) { if (seed == null) { setSeed(System.currentTimeMillis() + System.identityHashCode(this)); return; } setSeed(19650218); int i = 1; int j = 0; for (int k = FastMath.max(N, seed.length); k != 0; k--) { long l0 = (mt[i] & 0x7fffffffl) | ((mt[i] < 0) ? 0x80000000l : 0x0l); long l1 = (mt[i-1] & 0x7fffffffl) | ((mt[i-1] < 0) ? 0x80000000l : 0x0l); long l = (l0 ^ ((l1 ^ (l1 >> 30)) * 1664525l)) + seed[j] + j; // non linear mt[i] = (int) (l & 0xffffffffl); i++; j++; if (i >= N) { mt[0] = mt[N - 1]; i = 1; } if (j >= seed.length) { j = 0; } } for (int k = N - 1; k != 0; k--) { long l0 = (mt[i] & 0x7fffffffl) | ((mt[i] < 0) ? 0x80000000l : 0x0l); long l1 = (mt[i-1] & 0x7fffffffl) | ((mt[i-1] < 0) ? 0x80000000l : 0x0l); long l = (l0 ^ ((l1 ^ (l1 >> 30)) * 1566083941l)) - i; // non linear mt[i] = (int) (l & 0xffffffffL); i++; if (i >= N) { mt[0] = mt[N - 1]; i = 1; } } mt[0] = 0x80000000; // MSB is 1; assuring non-zero initial array clear(); // Clear normal deviate cache } /** Reinitialize the generator as if just built with the given long seed. *

                              The state of the generator is exactly the same as a new * generator built with the same seed.

                              * @param seed the initial seed (64 bits integer) */ @Override public void setSeed(long seed) { setSeed(new int[] { (int) (seed >>> 32), (int) (seed & 0xffffffffl) }); } /** Generate next pseudorandom number. *

                              This method is the core generation algorithm. It is used by all the * public generation methods for the various primitive types {@link * #nextBoolean()}, {@link #nextBytes(byte[])}, {@link #nextDouble()}, * {@link #nextFloat()}, {@link #nextGaussian()}, {@link #nextInt()}, * {@link #next(int)} and {@link #nextLong()}.

                              * @param bits number of random bits to produce * @return random bits generated */ @Override protected int next(int bits) { int y; if (mti >= N) { // generate N words at one time int mtNext = mt[0]; for (int k = 0; k < N - M; ++k) { int mtCurr = mtNext; mtNext = mt[k + 1]; y = (mtCurr & 0x80000000) | (mtNext & 0x7fffffff); mt[k] = mt[k + M] ^ (y >>> 1) ^ MAG01[y & 0x1]; } for (int k = N - M; k < N - 1; ++k) { int mtCurr = mtNext; mtNext = mt[k + 1]; y = (mtCurr & 0x80000000) | (mtNext & 0x7fffffff); mt[k] = mt[k + (M - N)] ^ (y >>> 1) ^ MAG01[y & 0x1]; } y = (mtNext & 0x80000000) | (mt[0] & 0x7fffffff); mt[N - 1] = mt[M - 1] ^ (y >>> 1) ^ MAG01[y & 0x1]; mti = 0; } y = mt[mti++]; // tempering y ^= y >>> 11; y ^= (y << 7) & 0x9d2c5680; y ^= (y << 15) & 0xefc60000; y ^= y >>> 18; return y >>> (32 - bits); } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/random/RandomGenerator.java100644 1750 1750 12526 12126627700 27757 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.random; /** * Interface extracted from java.util.Random. This interface is * implemented by {@link AbstractRandomGenerator}. * * @since 1.1 * @version $Id: RandomGenerator.java 1416643 2012-12-03 19:37:14Z tn $ */ public interface RandomGenerator { /** * Sets the seed of the underlying random number generator using an * int seed. *

                              Sequences of values generated starting with the same seeds * should be identical. *

                              * @param seed the seed value */ void setSeed(int seed); /** * Sets the seed of the underlying random number generator using an * int array seed. *

                              Sequences of values generated starting with the same seeds * should be identical. *

                              * @param seed the seed value */ void setSeed(int[] seed); /** * Sets the seed of the underlying random number generator using a * long seed. *

                              Sequences of values generated starting with the same seeds * should be identical. *

                              * @param seed the seed value */ void setSeed(long seed); /** * Generates random bytes and places them into a user-supplied * byte array. The number of random bytes produced is equal to * the length of the byte array. * * @param bytes the non-null byte array in which to put the * random bytes */ void nextBytes(byte[] bytes); /** * Returns the next pseudorandom, uniformly distributed int * value from this random number generator's sequence. * All 232 possible int values * should be produced with (approximately) equal probability. * * @return the next pseudorandom, uniformly distributed int * value from this random number generator's sequence */ int nextInt(); /** * Returns a pseudorandom, uniformly distributed int value * between 0 (inclusive) and the specified value (exclusive), drawn from * this random number generator's sequence. * * @param n the bound on the random number to be returned. Must be * positive. * @return a pseudorandom, uniformly distributed int * value between 0 (inclusive) and n (exclusive). * @throws IllegalArgumentException if n is not positive. */ int nextInt(int n); /** * Returns the next pseudorandom, uniformly distributed long * value from this random number generator's sequence. All * 264 possible long values * should be produced with (approximately) equal probability. * * @return the next pseudorandom, uniformly distributed long *value from this random number generator's sequence */ long nextLong(); /** * Returns the next pseudorandom, uniformly distributed * boolean value from this random number generator's * sequence. * * @return the next pseudorandom, uniformly distributed * boolean value from this random number generator's * sequence */ boolean nextBoolean(); /** * Returns the next pseudorandom, uniformly distributed float * value between 0.0 and 1.0 from this random * number generator's sequence. * * @return the next pseudorandom, uniformly distributed float * value between 0.0 and 1.0 from this * random number generator's sequence */ float nextFloat(); /** * Returns the next pseudorandom, uniformly distributed * double value between 0.0 and * 1.0 from this random number generator's sequence. * * @return the next pseudorandom, uniformly distributed * double value between 0.0 and * 1.0 from this random number generator's sequence */ double nextDouble(); /** * Returns the next pseudorandom, Gaussian ("normally") distributed * double value with mean 0.0 and standard * deviation 1.0 from this random number generator's sequence. * * @return the next pseudorandom, Gaussian ("normally") distributed * double value with mean 0.0 and * standard deviation 1.0 from this random number * generator's sequence */ double nextGaussian(); } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/random/RandomDataImpl.java100644 1750 1750 57156 12126627700 27534 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.random; import java.io.Serializable; import java.security.NoSuchAlgorithmException; import java.security.NoSuchProviderException; import java.util.Collection; import org.apache.commons.math3.distribution.IntegerDistribution; import org.apache.commons.math3.distribution.RealDistribution; import org.apache.commons.math3.exception.NotANumberException; import org.apache.commons.math3.exception.NotFiniteNumberException; import org.apache.commons.math3.exception.NotPositiveException; import org.apache.commons.math3.exception.NotStrictlyPositiveException; import org.apache.commons.math3.exception.MathIllegalArgumentException; import org.apache.commons.math3.exception.NumberIsTooLargeException; import org.apache.commons.math3.exception.OutOfRangeException; /** * Generates random deviates and other random data using a {@link RandomGenerator} * instance to generate non-secure data and a {@link java.security.SecureRandom} * instance to provide data for the nextSecureXxx methods. If no * RandomGenerator is provided in the constructor, the default is * to use a {@link Well19937c} generator. To plug in a different * implementation, either implement RandomGenerator directly or * extend {@link AbstractRandomGenerator}. *

                              * Supports reseeding the underlying pseudo-random number generator (PRNG). The * SecurityProvider and Algorithm used by the * SecureRandom instance can also be reset. *

                              *

                              * For details on the default PRNGs, see {@link java.util.Random} and * {@link java.security.SecureRandom}. *

                              *

                              * Usage Notes: *

                                *
                              • * Instance variables are used to maintain RandomGenerator and * SecureRandom instances used in data generation. Therefore, to * generate a random sequence of values or strings, you should use just * one RandomDataGenerator instance repeatedly.
                              • *
                              • * The "secure" methods are *much* slower. These should be used only when a * cryptographically secure random sequence is required. A secure random * sequence is a sequence of pseudo-random values which, in addition to being * well-dispersed (so no subsequence of values is an any more likely than other * subsequence of the the same length), also has the additional property that * knowledge of values generated up to any point in the sequence does not make * it any easier to predict subsequent values.
                              • *
                              • * When a new RandomDataGenerator is created, the underlying random * number generators are not initialized. If you do not * explicitly seed the default non-secure generator, it is seeded with the * current time in milliseconds plus the system identity hash code on first use. * The same holds for the secure generator. If you provide a RandomGenerator * to the constructor, however, this generator is not reseeded by the constructor * nor is it reseeded on first use.
                              • *
                              • * The reSeed and reSeedSecure methods delegate to the * corresponding methods on the underlying RandomGenerator and * SecureRandom instances. Therefore, reSeed(long) * fully resets the initial state of the non-secure random number generator (so * that reseeding with a specific value always results in the same subsequent * random sequence); whereas reSeedSecure(long) does not * reinitialize the secure random number generator (so secure sequences started * with calls to reseedSecure(long) won't be identical).
                              • *
                              • * This implementation is not synchronized. The underlying RandomGenerator * or SecureRandom instances are not protected by synchronization and * are not guaranteed to be thread-safe. Therefore, if an instance of this class * is concurrently utilized by multiple threads, it is the responsibility of * client code to synchronize access to seeding and data generation methods. *
                              • *
                              *

                              * @deprecated to be removed in 4.0. Use {@link RandomDataGenerator} instead * @version $Id: RandomDataImpl.java 1421917 2012-12-14 15:05:18Z erans $ */ @Deprecated public class RandomDataImpl implements RandomData, Serializable { /** Serializable version identifier */ private static final long serialVersionUID = -626730818244969716L; /** RandomDataGenerator delegate */ private final RandomDataGenerator delegate; /** * Construct a RandomDataImpl, using a default random generator as the source * of randomness. * *

                              The default generator is a {@link Well19937c} seeded * with {@code System.currentTimeMillis() + System.identityHashCode(this))}. * The generator is initialized and seeded on first use.

                              */ public RandomDataImpl() { delegate = new RandomDataGenerator(); } /** * Construct a RandomDataImpl using the supplied {@link RandomGenerator} as * the source of (non-secure) random data. * * @param rand the source of (non-secure) random data * (may be null, resulting in the default generator) * @since 1.1 */ public RandomDataImpl(RandomGenerator rand) { delegate = new RandomDataGenerator(rand); } /** * @return the delegate object. * @deprecated To be removed in 4.0. */ @Deprecated RandomDataGenerator getDelegate() { return delegate; } /** * {@inheritDoc} *

                              * Algorithm Description: hex strings are generated using a * 2-step process. *

                                *
                              1. {@code len / 2 + 1} binary bytes are generated using the underlying * Random
                              2. *
                              3. Each binary byte is translated into 2 hex digits
                              4. *
                              *

                              * * @param len the desired string length. * @return the random string. * @throws NotStrictlyPositiveException if {@code len <= 0}. */ public String nextHexString(int len) throws NotStrictlyPositiveException { return delegate.nextHexString(len); } /** {@inheritDoc} */ public int nextInt(int lower, int upper) throws NumberIsTooLargeException { return delegate.nextInt(lower, upper); } /** {@inheritDoc} */ public long nextLong(long lower, long upper) throws NumberIsTooLargeException { return delegate.nextLong(lower, upper); } /** * {@inheritDoc} *

                              * Algorithm Description: hex strings are generated in * 40-byte segments using a 3-step process. *

                                *
                              1. * 20 random bytes are generated using the underlying * SecureRandom.
                              2. *
                              3. * SHA-1 hash is applied to yield a 20-byte binary digest.
                              4. *
                              5. * Each byte of the binary digest is converted to 2 hex digits.
                              6. *
                              *

                              */ public String nextSecureHexString(int len) throws NotStrictlyPositiveException { return delegate.nextSecureHexString(len); } /** {@inheritDoc} */ public int nextSecureInt(int lower, int upper) throws NumberIsTooLargeException { return delegate.nextSecureInt(lower, upper); } /** {@inheritDoc} */ public long nextSecureLong(long lower, long upper) throws NumberIsTooLargeException { return delegate.nextSecureLong(lower,upper); } /** * {@inheritDoc} *

                              * Algorithm Description: *

                              • For small means, uses simulation of a Poisson process * using Uniform deviates, as described * here. * The Poisson process (and hence value returned) is bounded by 1000 * mean.
                              • * *
                              • For large means, uses the rejection algorithm described in
                                * Devroye, Luc. (1981).The Computer Generation of Poisson Random Variables * Computing vol. 26 pp. 197-207.

                              */ public long nextPoisson(double mean) throws NotStrictlyPositiveException { return delegate.nextPoisson(mean); } /** {@inheritDoc} */ public double nextGaussian(double mu, double sigma) throws NotStrictlyPositiveException { return delegate.nextGaussian(mu,sigma); } /** * {@inheritDoc} * *

                              * Algorithm Description: Uses the Algorithm SA (Ahrens) * from p. 876 in: * [1]: Ahrens, J. H. and Dieter, U. (1972). Computer methods for * sampling from the exponential and normal distributions. * Communications of the ACM, 15, 873-882. *

                              */ public double nextExponential(double mean) throws NotStrictlyPositiveException { return delegate.nextExponential(mean); } /** * {@inheritDoc} * *

                              * Algorithm Description: scales the output of * Random.nextDouble(), but rejects 0 values (i.e., will generate another * random double if Random.nextDouble() returns 0). This is necessary to * provide a symmetric output interval (both endpoints excluded). *

                              */ public double nextUniform(double lower, double upper) throws NumberIsTooLargeException, NotFiniteNumberException, NotANumberException { return delegate.nextUniform(lower, upper); } /** * {@inheritDoc} * *

                              * Algorithm Description: if the lower bound is excluded, * scales the output of Random.nextDouble(), but rejects 0 values (i.e., * will generate another random double if Random.nextDouble() returns 0). * This is necessary to provide a symmetric output interval (both * endpoints excluded). *

                              * @since 3.0 */ public double nextUniform(double lower, double upper, boolean lowerInclusive) throws NumberIsTooLargeException, NotFiniteNumberException, NotANumberException { return delegate.nextUniform(lower, upper, lowerInclusive); } /** * Generates a random value from the {@link org.apache.commons.math3.distribution.BetaDistribution Beta Distribution}. * This implementation uses {@link #nextInversionDeviate(RealDistribution) inversion} * to generate random values. * * @param alpha first distribution shape parameter * @param beta second distribution shape parameter * @return random value sampled from the beta(alpha, beta) distribution * @since 2.2 */ public double nextBeta(double alpha, double beta) { return delegate.nextBeta(alpha, beta); } /** * Generates a random value from the {@link org.apache.commons.math3.distribution.BinomialDistribution Binomial Distribution}. * This implementation uses {@link #nextInversionDeviate(RealDistribution) inversion} * to generate random values. * * @param numberOfTrials number of trials of the Binomial distribution * @param probabilityOfSuccess probability of success of the Binomial distribution * @return random value sampled from the Binomial(numberOfTrials, probabilityOfSuccess) distribution * @since 2.2 */ public int nextBinomial(int numberOfTrials, double probabilityOfSuccess) { return delegate.nextBinomial(numberOfTrials, probabilityOfSuccess); } /** * Generates a random value from the {@link org.apache.commons.math3.distribution.CauchyDistribution Cauchy Distribution}. * This implementation uses {@link #nextInversionDeviate(RealDistribution) inversion} * to generate random values. * * @param median the median of the Cauchy distribution * @param scale the scale parameter of the Cauchy distribution * @return random value sampled from the Cauchy(median, scale) distribution * @since 2.2 */ public double nextCauchy(double median, double scale) { return delegate.nextCauchy(median, scale); } /** * Generates a random value from the {@link org.apache.commons.math3.distribution.ChiSquaredDistribution ChiSquare Distribution}. * This implementation uses {@link #nextInversionDeviate(RealDistribution) inversion} * to generate random values. * * @param df the degrees of freedom of the ChiSquare distribution * @return random value sampled from the ChiSquare(df) distribution * @since 2.2 */ public double nextChiSquare(double df) { return delegate.nextChiSquare(df); } /** * Generates a random value from the {@link org.apache.commons.math3.distribution.FDistribution F Distribution}. * This implementation uses {@link #nextInversionDeviate(RealDistribution) inversion} * to generate random values. * * @param numeratorDf the numerator degrees of freedom of the F distribution * @param denominatorDf the denominator degrees of freedom of the F distribution * @return random value sampled from the F(numeratorDf, denominatorDf) distribution * @throws NotStrictlyPositiveException if * {@code numeratorDf <= 0} or {@code denominatorDf <= 0}. * @since 2.2 */ public double nextF(double numeratorDf, double denominatorDf) throws NotStrictlyPositiveException { return delegate.nextF(numeratorDf, denominatorDf); } /** *

                              Generates a random value from the * {@link org.apache.commons.math3.distribution.GammaDistribution Gamma Distribution}.

                              * *

                              This implementation uses the following algorithms:

                              * *

                              For 0 < shape < 1:
                              * Ahrens, J. H. and Dieter, U., Computer methods for * sampling from gamma, beta, Poisson and binomial distributions. * Computing, 12, 223-246, 1974.

                              * *

                              For shape >= 1:
                              * Marsaglia and Tsang, A Simple Method for Generating * Gamma Variables. ACM Transactions on Mathematical Software, * Volume 26 Issue 3, September, 2000.

                              * * @param shape the median of the Gamma distribution * @param scale the scale parameter of the Gamma distribution * @return random value sampled from the Gamma(shape, scale) distribution * @throws NotStrictlyPositiveException if {@code shape <= 0} or * {@code scale <= 0}. * @since 2.2 */ public double nextGamma(double shape, double scale) throws NotStrictlyPositiveException { return delegate.nextGamma(shape, scale); } /** * Generates a random value from the {@link org.apache.commons.math3.distribution.HypergeometricDistribution Hypergeometric Distribution}. * This implementation uses {@link #nextInversionDeviate(IntegerDistribution) inversion} * to generate random values. * * @param populationSize the population size of the Hypergeometric distribution * @param numberOfSuccesses number of successes in the population of the Hypergeometric distribution * @param sampleSize the sample size of the Hypergeometric distribution * @return random value sampled from the Hypergeometric(numberOfSuccesses, sampleSize) distribution * @throws NumberIsTooLargeException if {@code numberOfSuccesses > populationSize}, * or {@code sampleSize > populationSize}. * @throws NotStrictlyPositiveException if {@code populationSize <= 0}. * @throws NotPositiveException if {@code numberOfSuccesses < 0}. * @since 2.2 */ public int nextHypergeometric(int populationSize, int numberOfSuccesses, int sampleSize) throws NotPositiveException, NotStrictlyPositiveException, NumberIsTooLargeException { return delegate.nextHypergeometric(populationSize, numberOfSuccesses, sampleSize); } /** * Generates a random value from the {@link org.apache.commons.math3.distribution.PascalDistribution Pascal Distribution}. * This implementation uses {@link #nextInversionDeviate(IntegerDistribution) inversion} * to generate random values. * * @param r the number of successes of the Pascal distribution * @param p the probability of success of the Pascal distribution * @return random value sampled from the Pascal(r, p) distribution * @since 2.2 * @throws NotStrictlyPositiveException if the number of successes is not positive * @throws OutOfRangeException if the probability of success is not in the * range {@code [0, 1]}. */ public int nextPascal(int r, double p) throws NotStrictlyPositiveException, OutOfRangeException { return delegate.nextPascal(r, p); } /** * Generates a random value from the {@link org.apache.commons.math3.distribution.TDistribution T Distribution}. * This implementation uses {@link #nextInversionDeviate(RealDistribution) inversion} * to generate random values. * * @param df the degrees of freedom of the T distribution * @return random value from the T(df) distribution * @since 2.2 * @throws NotStrictlyPositiveException if {@code df <= 0} */ public double nextT(double df) throws NotStrictlyPositiveException { return delegate.nextT(df); } /** * Generates a random value from the {@link org.apache.commons.math3.distribution.WeibullDistribution Weibull Distribution}. * This implementation uses {@link #nextInversionDeviate(RealDistribution) inversion} * to generate random values. * * @param shape the shape parameter of the Weibull distribution * @param scale the scale parameter of the Weibull distribution * @return random value sampled from the Weibull(shape, size) distribution * @since 2.2 * @throws NotStrictlyPositiveException if {@code shape <= 0} or * {@code scale <= 0}. */ public double nextWeibull(double shape, double scale) throws NotStrictlyPositiveException { return delegate.nextWeibull(shape, scale); } /** * Generates a random value from the {@link org.apache.commons.math3.distribution.ZipfDistribution Zipf Distribution}. * This implementation uses {@link #nextInversionDeviate(IntegerDistribution) inversion} * to generate random values. * * @param numberOfElements the number of elements of the ZipfDistribution * @param exponent the exponent of the ZipfDistribution * @return random value sampled from the Zipf(numberOfElements, exponent) distribution * @since 2.2 * @exception NotStrictlyPositiveException if {@code numberOfElements <= 0} * or {@code exponent <= 0}. */ public int nextZipf(int numberOfElements, double exponent) throws NotStrictlyPositiveException { return delegate.nextZipf(numberOfElements, exponent); } /** * Reseeds the random number generator with the supplied seed. *

                              * Will create and initialize if null. *

                              * * @param seed * the seed value to use */ public void reSeed(long seed) { delegate.reSeed(seed); } /** * Reseeds the secure random number generator with the current time in * milliseconds. *

                              * Will create and initialize if null. *

                              */ public void reSeedSecure() { delegate.reSeedSecure(); } /** * Reseeds the secure random number generator with the supplied seed. *

                              * Will create and initialize if null. *

                              * * @param seed * the seed value to use */ public void reSeedSecure(long seed) { delegate.reSeedSecure(seed); } /** * Reseeds the random number generator with * {@code System.currentTimeMillis() + System.identityHashCode(this))}. */ public void reSeed() { delegate.reSeed(); } /** * Sets the PRNG algorithm for the underlying SecureRandom instance using * the Security Provider API. The Security Provider API is defined in * Java Cryptography Architecture API Specification & Reference. *

                              * USAGE NOTE: This method carries significant * overhead and may take several seconds to execute. *

                              * * @param algorithm * the name of the PRNG algorithm * @param provider * the name of the provider * @throws NoSuchAlgorithmException * if the specified algorithm is not available * @throws NoSuchProviderException * if the specified provider is not installed */ public void setSecureAlgorithm(String algorithm, String provider) throws NoSuchAlgorithmException, NoSuchProviderException { delegate.setSecureAlgorithm(algorithm, provider); } /** * {@inheritDoc} * *

                              * Uses a 2-cycle permutation shuffle. The shuffling process is described * here. *

                              */ public int[] nextPermutation(int n, int k) throws NotStrictlyPositiveException, NumberIsTooLargeException { return delegate.nextPermutation(n, k); } /** * {@inheritDoc} * *

                              * Algorithm Description: Uses a 2-cycle permutation * shuffle to generate a random permutation of c.size() and * then returns the elements whose indexes correspond to the elements of the * generated permutation. This technique is described, and proven to * generate random samples * here *

                              */ public Object[] nextSample(Collection c, int k) throws NotStrictlyPositiveException, NumberIsTooLargeException { return delegate.nextSample(c, k); } /** * Generate a random deviate from the given distribution using the * inversion method. * * @param distribution Continuous distribution to generate a random value from * @return a random value sampled from the given distribution * @throws MathIllegalArgumentException if the underlynig distribution throws one * @since 2.2 * @deprecated use the distribution's sample() method */ public double nextInversionDeviate(RealDistribution distribution) throws MathIllegalArgumentException { return distribution.inverseCumulativeProbability(nextUniform(0, 1)); } /** * Generate a random deviate from the given distribution using the * inversion method. * * @param distribution Integer distribution to generate a random value from * @return a random value sampled from the given distribution * @throws MathIllegalArgumentException if the underlynig distribution throws one * @since 2.2 * @deprecated use the distribution's sample() method */ public int nextInversionDeviate(IntegerDistribution distribution) throws MathIllegalArgumentException { return distribution.inverseCumulativeProbability(nextUniform(0, 1)); } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/random/StableRandomGenerator.java100644 1750 1750 12240 12126627700 31103 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.random; import org.apache.commons.math3.exception.NullArgumentException; import org.apache.commons.math3.exception.OutOfRangeException; import org.apache.commons.math3.exception.util.LocalizedFormats; import org.apache.commons.math3.util.FastMath; /** *

                              This class provides a stable normalized random generator. It samples from a stable * distribution with location parameter 0 and scale 1.

                              * *

                              The implementation uses the Chambers-Mallows-Stuck method as described in * Handbook of computational statistics: concepts and methods by * James E. Gentle, Wolfgang Härdle, Yuichi Mori.

                              * * @version $Id: StableRandomGenerator.java 1394763 2012-10-05 19:54:00Z psteitz $ * @since 3.0 */ public class StableRandomGenerator implements NormalizedRandomGenerator { /** Underlying generator. */ private final RandomGenerator generator; /** stability parameter */ private final double alpha; /** skewness parameter */ private final double beta; /** cache of expression value used in generation */ private final double zeta; /** * Create a new generator. * * @param generator underlying random generator to use * @param alpha Stability parameter. Must be in range (0, 2] * @param beta Skewness parameter. Must be in range [-1, 1] * @throws NullArgumentException if generator is null * @throws OutOfRangeException if {@code alpha <= 0} or {@code alpha > 2} * or {@code beta < -1} or {@code beta > 1} */ public StableRandomGenerator(final RandomGenerator generator, final double alpha, final double beta) throws NullArgumentException, OutOfRangeException { if (generator == null) { throw new NullArgumentException(); } if (!(alpha > 0d && alpha <= 2d)) { throw new OutOfRangeException(LocalizedFormats.OUT_OF_RANGE_LEFT, alpha, 0, 2); } if (!(beta >= -1d && beta <= 1d)) { throw new OutOfRangeException(LocalizedFormats.OUT_OF_RANGE_SIMPLE, beta, -1, 1); } this.generator = generator; this.alpha = alpha; this.beta = beta; if (alpha < 2d && beta != 0d) { zeta = beta * FastMath.tan(FastMath.PI * alpha / 2); } else { zeta = 0d; } } /** * Generate a random scalar with zero location and unit scale. * * @return a random scalar with zero location and unit scale */ public double nextNormalizedDouble() { // we need 2 uniform random numbers to calculate omega and phi double omega = -FastMath.log(generator.nextDouble()); double phi = FastMath.PI * (generator.nextDouble() - 0.5); // Normal distribution case (Box-Muller algorithm) if (alpha == 2d) { return FastMath.sqrt(2d * omega) * FastMath.sin(phi); } double x; // when beta = 0, zeta is zero as well // Thus we can exclude it from the formula if (beta == 0d) { // Cauchy distribution case if (alpha == 1d) { x = FastMath.tan(phi); } else { x = FastMath.pow(omega * FastMath.cos((1 - alpha) * phi), 1d / alpha - 1d) * FastMath.sin(alpha * phi) / FastMath.pow(FastMath.cos(phi), 1d / alpha); } } else { // Generic stable distribution double cosPhi = FastMath.cos(phi); // to avoid rounding errors around alpha = 1 if (FastMath.abs(alpha - 1d) > 1e-8) { double alphaPhi = alpha * phi; double invAlphaPhi = phi - alphaPhi; x = (FastMath.sin(alphaPhi) + zeta * FastMath.cos(alphaPhi)) / cosPhi * (FastMath.cos(invAlphaPhi) + zeta * FastMath.sin(invAlphaPhi)) / FastMath.pow(omega * cosPhi, (1 - alpha) / alpha); } else { double betaPhi = FastMath.PI / 2 + beta * phi; x = 2d / FastMath.PI * (betaPhi * FastMath.tan(phi) - beta * FastMath.log(FastMath.PI / 2d * omega * cosPhi / betaPhi)); if (alpha != 1d) { x = x + beta * FastMath.tan(FastMath.PI * alpha / 2); } } } return x; } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/random/UniformRandomGenerator.java100644 1750 1750 4111 12126627700 31266 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.random; import org.apache.commons.math3.util.FastMath; /** * This class implements a normalized uniform random generator. *

                              Since it is a normalized random generator, it generates values * from a uniform distribution with mean equal to 0 and standard * deviation equal to 1. Generated values fall in the range * [-√3, +√3].

                              * * @since 1.2 * * @version $Id: UniformRandomGenerator.java 1416643 2012-12-03 19:37:14Z tn $ */ public class UniformRandomGenerator implements NormalizedRandomGenerator { /** Square root of three. */ private static final double SQRT3 = FastMath.sqrt(3.0); /** Underlying generator. */ private final RandomGenerator generator; /** Create a new generator. * @param generator underlying random generator to use */ public UniformRandomGenerator(RandomGenerator generator) { this.generator = generator; } /** Generate a random scalar with null mean and unit standard deviation. *

                              The number generated is uniformly distributed between -&sqrt;(3) * and +&sqrt;(3).

                              * @return a random scalar with null mean and unit standard deviation */ public double nextNormalizedDouble() { return SQRT3 * (2 * generator.nextDouble() - 1.0); } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/random/EmpiricalDistribution.java100644 1750 1750 70327 12126627700 31200 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.random; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.net.URL; import java.nio.charset.Charset; import java.util.ArrayList; import java.util.List; import org.apache.commons.math3.distribution.AbstractRealDistribution; import org.apache.commons.math3.distribution.NormalDistribution; import org.apache.commons.math3.distribution.RealDistribution; import org.apache.commons.math3.exception.MathIllegalStateException; import org.apache.commons.math3.exception.MathInternalError; import org.apache.commons.math3.exception.NullArgumentException; import org.apache.commons.math3.exception.OutOfRangeException; import org.apache.commons.math3.exception.ZeroException; import org.apache.commons.math3.exception.util.LocalizedFormats; import org.apache.commons.math3.stat.descriptive.StatisticalSummary; import org.apache.commons.math3.stat.descriptive.SummaryStatistics; import org.apache.commons.math3.util.FastMath; import org.apache.commons.math3.util.MathUtils; /** *

                              Represents an * empirical probability distribution -- a probability distribution derived * from observed data without making any assumptions about the functional form * of the population distribution that the data come from.

                              * *

                              An EmpiricalDistribution maintains data structures, called * distribution digests, that describe empirical distributions and * support the following operations:

                                *
                              • loading the distribution from a file of observed data values
                              • *
                              • dividing the input data into "bin ranges" and reporting bin frequency * counts (data for histogram)
                              • *
                              • reporting univariate statistics describing the full set of data values * as well as the observations within each bin
                              • *
                              • generating random values from the distribution
                              • *
                              * Applications can use EmpiricalDistribution to build grouped * frequency histograms representing the input data or to generate random values * "like" those in the input file -- i.e., the values generated will follow the * distribution of the values in the file.

                              * *

                              The implementation uses what amounts to the * * Variable Kernel Method with Gaussian smoothing:

                              * Digesting the input file *

                              1. Pass the file once to compute min and max.
                              2. *
                              3. Divide the range from min-max into binCount "bins."
                              4. *
                              5. Pass the data file again, computing bin counts and univariate * statistics (mean, std dev.) for each of the bins
                              6. *
                              7. Divide the interval (0,1) into subintervals associated with the bins, * with the length of a bin's subinterval proportional to its count.
                              * Generating random values from the distribution
                                *
                              1. Generate a uniformly distributed value in (0,1)
                              2. *
                              3. Select the subinterval to which the value belongs. *
                              4. Generate a random Gaussian value with mean = mean of the associated * bin and std dev = std dev of associated bin.

                              * *

                              EmpiricalDistribution implements the {@link RealDistribution} interface * as follows. Given x within the range of values in the dataset, let B * be the bin containing x and let K be the within-bin kernel for B. Let P(B-) * be the sum of the probabilities of the bins below B and let K(B) be the * mass of B under K (i.e., the integral of the kernel density over B). Then * set P(X < x) = P(B-) + P(B) * K(x) / K(B) where K(x) is the kernel distribution * evaluated at x. This results in a cdf that matches the grouped frequency * distribution at the bin endpoints and interpolates within bins using * within-bin kernels.

                              * *USAGE NOTES:
                                *
                              • The binCount is set by default to 1000. A good rule of thumb * is to set the bin count to approximately the length of the input file divided * by 10.
                              • *
                              • The input file must be a plain text file containing one valid numeric * entry per line.
                              • *

                              * * @version $Id: EmpiricalDistribution.java 1457372 2013-03-17 04:28:04Z psteitz $ */ public class EmpiricalDistribution extends AbstractRealDistribution { /** Default bin count */ public static final int DEFAULT_BIN_COUNT = 1000; /** Character set for file input */ private static final String FILE_CHARSET = "US-ASCII"; /** Serializable version identifier */ private static final long serialVersionUID = 5729073523949762654L; /** RandomDataGenerator instance to use in repeated calls to getNext() */ protected final RandomDataGenerator randomData; /** List of SummaryStatistics objects characterizing the bins */ private final List binStats; /** Sample statistics */ private SummaryStatistics sampleStats = null; /** Max loaded value */ private double max = Double.NEGATIVE_INFINITY; /** Min loaded value */ private double min = Double.POSITIVE_INFINITY; /** Grid size */ private double delta = 0d; /** number of bins */ private final int binCount; /** is the distribution loaded? */ private boolean loaded = false; /** upper bounds of subintervals in (0,1) "belonging" to the bins */ private double[] upperBounds = null; /** * Creates a new EmpiricalDistribution with the default bin count. */ public EmpiricalDistribution() { this(DEFAULT_BIN_COUNT); } /** * Creates a new EmpiricalDistribution with the specified bin count. * * @param binCount number of bins */ public EmpiricalDistribution(int binCount) { this(binCount, new RandomDataGenerator()); } /** * Creates a new EmpiricalDistribution with the specified bin count using the * provided {@link RandomGenerator} as the source of random data. * * @param binCount number of bins * @param generator random data generator (may be null, resulting in default JDK generator) * @since 3.0 */ public EmpiricalDistribution(int binCount, RandomGenerator generator) { this(binCount, new RandomDataGenerator(generator)); } /** * Creates a new EmpiricalDistribution with default bin count using the * provided {@link RandomGenerator} as the source of random data. * * @param generator random data generator (may be null, resulting in default JDK generator) * @since 3.0 */ public EmpiricalDistribution(RandomGenerator generator) { this(DEFAULT_BIN_COUNT, generator); } /** * Creates a new EmpiricalDistribution with the specified bin count using the * provided {@link RandomDataImpl} instance as the source of random data. * * @param binCount number of bins * @param randomData random data generator (may be null, resulting in default JDK generator) * @since 3.0 * @deprecated As of 3.1. Please use {@link #EmpiricalDistribution(int,RandomGenerator)} instead. */ @Deprecated public EmpiricalDistribution(int binCount, RandomDataImpl randomData) { this(binCount, randomData.getDelegate()); } /** * Creates a new EmpiricalDistribution with default bin count using the * provided {@link RandomDataImpl} as the source of random data. * * @param randomData random data generator (may be null, resulting in default JDK generator) * @since 3.0 * @deprecated As of 3.1. Please use {@link #EmpiricalDistribution(RandomGenerator)} instead. */ @Deprecated public EmpiricalDistribution(RandomDataImpl randomData) { this(DEFAULT_BIN_COUNT, randomData); } /** * Private constructor to allow lazy initialisation of the RNG contained * in the {@link #randomData} instance variable. * * @param binCount number of bins * @param randomData Random data generator. */ private EmpiricalDistribution(int binCount, RandomDataGenerator randomData) { super(null); this.binCount = binCount; this.randomData = randomData; binStats = new ArrayList(); } /** * Computes the empirical distribution from the provided * array of numbers. * * @param in the input data array * @exception NullArgumentException if in is null */ public void load(double[] in) throws NullArgumentException { DataAdapter da = new ArrayDataAdapter(in); try { da.computeStats(); // new adapter for the second pass fillBinStats(new ArrayDataAdapter(in)); } catch (IOException ex) { // Can't happen throw new MathInternalError(); } loaded = true; } /** * Computes the empirical distribution using data read from a URL. * *

                              The input file must be an ASCII text file containing one * valid numeric entry per line.

                              * * @param url url of the input file * * @throws IOException if an IO error occurs * @throws NullArgumentException if url is null * @throws ZeroException if URL contains no data */ public void load(URL url) throws IOException, NullArgumentException, ZeroException { MathUtils.checkNotNull(url); Charset charset = Charset.forName(FILE_CHARSET); BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream(), charset)); try { DataAdapter da = new StreamDataAdapter(in); da.computeStats(); if (sampleStats.getN() == 0) { throw new ZeroException(LocalizedFormats.URL_CONTAINS_NO_DATA, url); } // new adapter for the second pass in = new BufferedReader(new InputStreamReader(url.openStream(), charset)); fillBinStats(new StreamDataAdapter(in)); loaded = true; } finally { try { in.close(); } catch (IOException ex) { //NOPMD // ignore } } } /** * Computes the empirical distribution from the input file. * *

                              The input file must be an ASCII text file containing one * valid numeric entry per line.

                              * * @param file the input file * @throws IOException if an IO error occurs * @throws NullArgumentException if file is null */ public void load(File file) throws IOException, NullArgumentException { MathUtils.checkNotNull(file); Charset charset = Charset.forName(FILE_CHARSET); InputStream is = new FileInputStream(file); BufferedReader in = new BufferedReader(new InputStreamReader(is, charset)); try { DataAdapter da = new StreamDataAdapter(in); da.computeStats(); // new adapter for second pass is = new FileInputStream(file); in = new BufferedReader(new InputStreamReader(is, charset)); fillBinStats(new StreamDataAdapter(in)); loaded = true; } finally { try { in.close(); } catch (IOException ex) { //NOPMD // ignore } } } /** * Provides methods for computing sampleStats and * beanStats abstracting the source of data. */ private abstract class DataAdapter{ /** * Compute bin stats. * * @throws IOException if an error occurs computing bin stats */ public abstract void computeBinStats() throws IOException; /** * Compute sample statistics. * * @throws IOException if an error occurs computing sample stats */ public abstract void computeStats() throws IOException; } /** * DataAdapter for data provided through some input stream */ private class StreamDataAdapter extends DataAdapter{ /** Input stream providing access to the data */ private BufferedReader inputStream; /** * Create a StreamDataAdapter from a BufferedReader * * @param in BufferedReader input stream */ public StreamDataAdapter(BufferedReader in){ super(); inputStream = in; } /** {@inheritDoc} */ @Override public void computeBinStats() throws IOException { String str = null; double val = 0.0d; while ((str = inputStream.readLine()) != null) { val = Double.parseDouble(str); SummaryStatistics stats = binStats.get(findBin(val)); stats.addValue(val); } inputStream.close(); inputStream = null; } /** {@inheritDoc} */ @Override public void computeStats() throws IOException { String str = null; double val = 0.0; sampleStats = new SummaryStatistics(); while ((str = inputStream.readLine()) != null) { val = Double.valueOf(str).doubleValue(); sampleStats.addValue(val); } inputStream.close(); inputStream = null; } } /** * DataAdapter for data provided as array of doubles. */ private class ArrayDataAdapter extends DataAdapter { /** Array of input data values */ private double[] inputArray; /** * Construct an ArrayDataAdapter from a double[] array * * @param in double[] array holding the data * @throws NullArgumentException if in is null */ public ArrayDataAdapter(double[] in) throws NullArgumentException { super(); MathUtils.checkNotNull(in); inputArray = in; } /** {@inheritDoc} */ @Override public void computeStats() throws IOException { sampleStats = new SummaryStatistics(); for (int i = 0; i < inputArray.length; i++) { sampleStats.addValue(inputArray[i]); } } /** {@inheritDoc} */ @Override public void computeBinStats() throws IOException { for (int i = 0; i < inputArray.length; i++) { SummaryStatistics stats = binStats.get(findBin(inputArray[i])); stats.addValue(inputArray[i]); } } } /** * Fills binStats array (second pass through data file). * * @param da object providing access to the data * @throws IOException if an IO error occurs */ private void fillBinStats(final DataAdapter da) throws IOException { // Set up grid min = sampleStats.getMin(); max = sampleStats.getMax(); delta = (max - min)/(Double.valueOf(binCount)).doubleValue(); // Initialize binStats ArrayList if (!binStats.isEmpty()) { binStats.clear(); } for (int i = 0; i < binCount; i++) { SummaryStatistics stats = new SummaryStatistics(); binStats.add(i,stats); } // Filling data in binStats Array da.computeBinStats(); // Assign upperBounds based on bin counts upperBounds = new double[binCount]; upperBounds[0] = ((double) binStats.get(0).getN()) / (double) sampleStats.getN(); for (int i = 1; i < binCount-1; i++) { upperBounds[i] = upperBounds[i-1] + ((double) binStats.get(i).getN()) / (double) sampleStats.getN(); } upperBounds[binCount-1] = 1.0d; } /** * Returns the index of the bin to which the given value belongs * * @param value the value whose bin we are trying to find * @return the index of the bin containing the value */ private int findBin(double value) { return FastMath.min( FastMath.max((int) FastMath.ceil((value - min) / delta) - 1, 0), binCount - 1); } /** * Generates a random value from this distribution. * Preconditions:
                                *
                              • the distribution must be loaded before invoking this method
                              * @return the random value. * @throws MathIllegalStateException if the distribution has not been loaded */ public double getNextValue() throws MathIllegalStateException { if (!loaded) { throw new MathIllegalStateException(LocalizedFormats.DISTRIBUTION_NOT_LOADED); } // Start with a uniformly distributed random number in (0,1) final double x = randomData.nextUniform(0,1); // Use this to select the bin and generate a Gaussian within the bin for (int i = 0; i < binCount; i++) { if (x <= upperBounds[i]) { SummaryStatistics stats = binStats.get(i); if (stats.getN() > 0) { if (stats.getStandardDeviation() > 0) { // more than one obs return getKernel(stats).sample(); } else { return stats.getMean(); // only one obs in bin } } } } throw new MathIllegalStateException(LocalizedFormats.NO_BIN_SELECTED); } /** * Returns a {@link StatisticalSummary} describing this distribution. * Preconditions:
                                *
                              • the distribution must be loaded before invoking this method
                              * * @return the sample statistics * @throws IllegalStateException if the distribution has not been loaded */ public StatisticalSummary getSampleStats() { return sampleStats; } /** * Returns the number of bins. * * @return the number of bins. */ public int getBinCount() { return binCount; } /** * Returns a List of {@link SummaryStatistics} instances containing * statistics describing the values in each of the bins. The list is * indexed on the bin number. * * @return List of bin statistics. */ public List getBinStats() { return binStats; } /** *

                              Returns a fresh copy of the array of upper bounds for the bins. * Bins are:
                              * [min,upperBounds[0]],(upperBounds[0],upperBounds[1]],..., * (upperBounds[binCount-2], upperBounds[binCount-1] = max].

                              * *

                              Note: In versions 1.0-2.0 of commons-math, this method * incorrectly returned the array of probability generator upper * bounds now returned by {@link #getGeneratorUpperBounds()}.

                              * * @return array of bin upper bounds * @since 2.1 */ public double[] getUpperBounds() { double[] binUpperBounds = new double[binCount]; for (int i = 0; i < binCount - 1; i++) { binUpperBounds[i] = min + delta * (i + 1); } binUpperBounds[binCount - 1] = max; return binUpperBounds; } /** *

                              Returns a fresh copy of the array of upper bounds of the subintervals * of [0,1] used in generating data from the empirical distribution. * Subintervals correspond to bins with lengths proportional to bin counts.

                              * *

                              In versions 1.0-2.0 of commons-math, this array was (incorrectly) returned * by {@link #getUpperBounds()}.

                              * * @since 2.1 * @return array of upper bounds of subintervals used in data generation */ public double[] getGeneratorUpperBounds() { int len = upperBounds.length; double[] out = new double[len]; System.arraycopy(upperBounds, 0, out, 0, len); return out; } /** * Property indicating whether or not the distribution has been loaded. * * @return true if the distribution has been loaded */ public boolean isLoaded() { return loaded; } /** * Reseeds the random number generator used by {@link #getNextValue()}. * * @param seed random generator seed * @since 3.0 */ public void reSeed(long seed) { randomData.reSeed(seed); } // Distribution methods --------------------------- /** * {@inheritDoc} * @since 3.1 */ public double probability(double x) { return 0; } /** * {@inheritDoc} * *

                              Returns the kernel density normalized so that its integral over each bin * equals the bin mass.

                              * *

                              Algorithm description:

                                *
                              1. Find the bin B that x belongs to.
                              2. *
                              3. Compute K(B) = the mass of B with respect to the within-bin kernel (i.e., the * integral of the kernel density over B).
                              4. *
                              5. Return k(x) * P(B) / K(B), where k is the within-bin kernel density * and P(B) is the mass of B.

                              * @since 3.1 */ public double density(double x) { if (x < min || x > max) { return 0d; } final int binIndex = findBin(x); final RealDistribution kernel = getKernel(binStats.get(binIndex)); return kernel.density(x) * pB(binIndex) / kB(binIndex); } /** * {@inheritDoc} * *

                              Algorithm description:

                                *
                              1. Find the bin B that x belongs to.
                              2. *
                              3. Compute P(B) = the mass of B and P(B-) = the combined mass of the bins below B.
                              4. *
                              5. Compute K(B) = the probability mass of B with respect to the within-bin kernel * and K(B-) = the kernel distribution evaluated at the lower endpoint of B
                              6. *
                              7. Return P(B-) + P(B) * [K(x) - K(B-)] / K(B) where * K(x) is the within-bin kernel distribution function evaluated at x.

                              * * @since 3.1 */ public double cumulativeProbability(double x) { if (x < min) { return 0d; } else if (x >= max) { return 1d; } final int binIndex = findBin(x); final double pBminus = pBminus(binIndex); final double pB = pB(binIndex); final double[] binBounds = getUpperBounds(); final double kB = kB(binIndex); final double lower = binIndex == 0 ? min : binBounds[binIndex - 1]; final RealDistribution kernel = k(x); final double withinBinCum = (kernel.cumulativeProbability(x) - kernel.cumulativeProbability(lower)) / kB; return pBminus + pB * withinBinCum; } /** * {@inheritDoc} * *

                              Algorithm description:

                                *
                              1. Find the smallest i such that the sum of the masses of the bins * through i is at least p.
                              2. *
                              3. * Let K be the within-bin kernel distribution for bin i.
                                * Let K(B) be the mass of B under K.
                                * Let K(B-) be K evaluated at the lower endpoint of B (the combined * mass of the bins below B under K).
                                * Let P(B) be the probability of bin i.
                                * Let P(B-) be the sum of the bin masses below bin i.
                                * Let pCrit = p - P(B-)
                                *
                              4. Return the inverse of K evaluated at
                                * K(B-) + pCrit * K(B) / P(B)
                              5. *

                              * * @since 3.1 */ public double inverseCumulativeProbability(final double p) throws OutOfRangeException { if (p < 0.0 || p > 1.0) { throw new OutOfRangeException(p, 0, 1); } if (p == 0.0) { return getSupportLowerBound(); } if (p == 1.0) { return getSupportUpperBound(); } int i = 0; while (cumBinP(i) < p) { i++; } final RealDistribution kernel = getKernel(binStats.get(i)); final double kB = kB(i); final double[] binBounds = getUpperBounds(); final double lower = i == 0 ? min : binBounds[i - 1]; final double kBminus = kernel.cumulativeProbability(lower); final double pB = pB(i); final double pBminus = pBminus(i); final double pCrit = p - pBminus; if (pCrit <= 0) { return lower; } return kernel.inverseCumulativeProbability(kBminus + pCrit * kB / pB); } /** * {@inheritDoc} * @since 3.1 */ public double getNumericalMean() { return sampleStats.getMean(); } /** * {@inheritDoc} * @since 3.1 */ public double getNumericalVariance() { return sampleStats.getVariance(); } /** * {@inheritDoc} * @since 3.1 */ public double getSupportLowerBound() { return min; } /** * {@inheritDoc} * @since 3.1 */ public double getSupportUpperBound() { return max; } /** * {@inheritDoc} * @since 3.1 */ public boolean isSupportLowerBoundInclusive() { return true; } /** * {@inheritDoc} * @since 3.1 */ public boolean isSupportUpperBoundInclusive() { return true; } /** * {@inheritDoc} * @since 3.1 */ public boolean isSupportConnected() { return true; } /** * {@inheritDoc} * @since 3.1 */ @Override public double sample() { return getNextValue(); } /** * {@inheritDoc} * @since 3.1 */ @Override public void reseedRandomGenerator(long seed) { randomData.reSeed(seed); } /** * The probability of bin i. * * @param i the index of the bin * @return the probability that selection begins in bin i */ private double pB(int i) { return i == 0 ? upperBounds[0] : upperBounds[i] - upperBounds[i - 1]; } /** * The combined probability of the bins up to but not including bin i. * * @param i the index of the bin * @return the probability that selection begins in a bin below bin i. */ private double pBminus(int i) { return i == 0 ? 0 : upperBounds[i - 1]; } /** * Mass of bin i under the within-bin kernel of the bin. * * @param i index of the bin * @return the difference in the within-bin kernel cdf between the * upper and lower endpoints of bin i */ @SuppressWarnings("deprecation") private double kB(int i) { final double[] binBounds = getUpperBounds(); final RealDistribution kernel = getKernel(binStats.get(i)); return i == 0 ? kernel.cumulativeProbability(min, binBounds[0]) : kernel.cumulativeProbability(binBounds[i - 1], binBounds[i]); } /** * The within-bin kernel of the bin that x belongs to. * * @param x the value to locate within a bin * @return the within-bin kernel of the bin containing x */ private RealDistribution k(double x) { final int binIndex = findBin(x); return getKernel(binStats.get(binIndex)); } /** * The combined probability of the bins up to and including binIndex. * * @param binIndex maximum bin index * @return sum of the probabilities of bins through binIndex */ private double cumBinP(int binIndex) { return upperBounds[binIndex]; } /** * The within-bin smoothing kernel. * * @param bStats summary statistics for the bin * @return within-bin kernel parameterized by bStats */ protected RealDistribution getKernel(SummaryStatistics bStats) { // Default to Gaussian return new NormalDistribution(randomData.getRandomGenerator(), bStats.getMean(), bStats.getStandardDeviation(), NormalDistribution.DEFAULT_INVERSE_ABSOLUTE_ACCURACY); } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/random/Well19937a.java100644 1750 1750 7474 12126627700 26337 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.random; /** This class implements the WELL19937a pseudo-random number generator * from François Panneton, Pierre L'Ecuyer and Makoto Matsumoto. *

                              This generator is described in a paper by François Panneton, * Pierre L'Ecuyer and Makoto Matsumoto Improved * Long-Period Generators Based on Linear Recurrences Modulo 2 ACM * Transactions on Mathematical Software, 32, 1 (2006). The errata for the paper * are in wellrng-errata.txt.

                              * @see WELL Random number generator * @version $Id: Well19937a.java 1416643 2012-12-03 19:37:14Z tn $ * @since 2.2 */ public class Well19937a extends AbstractWell { /** Serializable version identifier. */ private static final long serialVersionUID = -7462102162223815419L; /** Number of bits in the pool. */ private static final int K = 19937; /** First parameter of the algorithm. */ private static final int M1 = 70; /** Second parameter of the algorithm. */ private static final int M2 = 179; /** Third parameter of the algorithm. */ private static final int M3 = 449; /** Creates a new random number generator. *

                              The instance is initialized using the current time as the * seed.

                              */ public Well19937a() { super(K, M1, M2, M3); } /** Creates a new random number generator using a single int seed. * @param seed the initial seed (32 bits integer) */ public Well19937a(int seed) { super(K, M1, M2, M3, seed); } /** Creates a new random number generator using an int array seed. * @param seed the initial seed (32 bits integers array), if null * the seed of the generator will be related to the current time */ public Well19937a(int[] seed) { super(K, M1, M2, M3, seed); } /** Creates a new random number generator using a single long seed. * @param seed the initial seed (64 bits integer) */ public Well19937a(long seed) { super(K, M1, M2, M3, seed); } /** {@inheritDoc} */ @Override protected int next(final int bits) { final int indexRm1 = iRm1[index]; final int indexRm2 = iRm2[index]; final int v0 = v[index]; final int vM1 = v[i1[index]]; final int vM2 = v[i2[index]]; final int vM3 = v[i3[index]]; final int z0 = (0x80000000 & v[indexRm1]) ^ (0x7FFFFFFF & v[indexRm2]); final int z1 = (v0 ^ (v0 << 25)) ^ (vM1 ^ (vM1 >>> 27)); final int z2 = (vM2 >>> 9) ^ (vM3 ^ (vM3 >>> 1)); final int z3 = z1 ^ z2; final int z4 = z0 ^ (z1 ^ (z1 << 9)) ^ (z2 ^ (z2 << 21)) ^ (z3 ^ (z3 >>> 21)); v[index] = z3; v[indexRm1] = z4; v[indexRm2] &= 0x80000000; index = indexRm1; return z4 >>> (32 - bits); } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/random/Well44497a.java100644 1750 1750 10051 12126627700 26337 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.random; /** This class implements the WELL44497a pseudo-random number generator * from François Panneton, Pierre L'Ecuyer and Makoto Matsumoto. *

                              This generator is described in a paper by François Panneton, * Pierre L'Ecuyer and Makoto Matsumoto Improved * Long-Period Generators Based on Linear Recurrences Modulo 2 ACM * Transactions on Mathematical Software, 32, 1 (2006). The errata for the paper * are in wellrng-errata.txt.

                              * @see WELL Random number generator * @version $Id: Well44497a.java 1416643 2012-12-03 19:37:14Z tn $ * @since 2.2 */ public class Well44497a extends AbstractWell { /** Serializable version identifier. */ private static final long serialVersionUID = -3859207588353972099L; /** Number of bits in the pool. */ private static final int K = 44497; /** First parameter of the algorithm. */ private static final int M1 = 23; /** Second parameter of the algorithm. */ private static final int M2 = 481; /** Third parameter of the algorithm. */ private static final int M3 = 229; /** Creates a new random number generator. *

                              The instance is initialized using the current time as the * seed.

                              */ public Well44497a() { super(K, M1, M2, M3); } /** Creates a new random number generator using a single int seed. * @param seed the initial seed (32 bits integer) */ public Well44497a(int seed) { super(K, M1, M2, M3, seed); } /** Creates a new random number generator using an int array seed. * @param seed the initial seed (32 bits integers array), if null * the seed of the generator will be related to the current time */ public Well44497a(int[] seed) { super(K, M1, M2, M3, seed); } /** Creates a new random number generator using a single long seed. * @param seed the initial seed (64 bits integer) */ public Well44497a(long seed) { super(K, M1, M2, M3, seed); } /** {@inheritDoc} */ @Override protected int next(final int bits) { final int indexRm1 = iRm1[index]; final int indexRm2 = iRm2[index]; final int v0 = v[index]; final int vM1 = v[i1[index]]; final int vM2 = v[i2[index]]; final int vM3 = v[i3[index]]; // the values below include the errata of the original article final int z0 = (0xFFFF8000 & v[indexRm1]) ^ (0x00007FFF & v[indexRm2]); final int z1 = (v0 ^ (v0 << 24)) ^ (vM1 ^ (vM1 >>> 30)); final int z2 = (vM2 ^ (vM2 << 10)) ^ (vM3 << 26); final int z3 = z1 ^ z2; final int z2Prime = ((z2 << 9) ^ (z2 >>> 23)) & 0xfbffffff; final int z2Second = ((z2 & 0x00020000) != 0) ? (z2Prime ^ 0xb729fcec) : z2Prime; final int z4 = z0 ^ (z1 ^ (z1 >>> 20)) ^ z2Second ^ z3; v[index] = z3; v[indexRm1] = z4; v[indexRm2] &= 0xFFFF8000; index = indexRm1; return z4 >>> (32 - bits); } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/random/GaussianRandomGenerator.java100644 1750 1750 3312 12126627700 31423 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.random; /** * This class is a gaussian normalized random generator for scalars. *

                              This class is a simple wrapper around the {@link * RandomGenerator#nextGaussian} method.

                              * @version $Id: GaussianRandomGenerator.java 1416643 2012-12-03 19:37:14Z tn $ * @since 1.2 */ public class GaussianRandomGenerator implements NormalizedRandomGenerator { /** Underlying generator. */ private final RandomGenerator generator; /** Create a new generator. * @param generator underlying random generator to use */ public GaussianRandomGenerator(final RandomGenerator generator) { this.generator = generator; } /** Generate a random scalar with null mean and unit standard deviation. * @return a random scalar with null mean and unit standard deviation */ public double nextNormalizedDouble() { return generator.nextGaussian(); } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/random/RandomDataGenerator.java100644 1750 1750 107373 12126627700 30576 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.random; import java.io.Serializable; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.security.NoSuchProviderException; import java.security.SecureRandom; import java.util.Collection; import org.apache.commons.math3.distribution.BetaDistribution; import org.apache.commons.math3.distribution.BinomialDistribution; import org.apache.commons.math3.distribution.CauchyDistribution; import org.apache.commons.math3.distribution.ChiSquaredDistribution; import org.apache.commons.math3.distribution.ExponentialDistribution; import org.apache.commons.math3.distribution.FDistribution; import org.apache.commons.math3.distribution.GammaDistribution; import org.apache.commons.math3.distribution.HypergeometricDistribution; import org.apache.commons.math3.distribution.PascalDistribution; import org.apache.commons.math3.distribution.PoissonDistribution; import org.apache.commons.math3.distribution.TDistribution; import org.apache.commons.math3.distribution.WeibullDistribution; import org.apache.commons.math3.distribution.ZipfDistribution; import org.apache.commons.math3.exception.MathInternalError; import org.apache.commons.math3.exception.NotANumberException; import org.apache.commons.math3.exception.NotFiniteNumberException; import org.apache.commons.math3.exception.NotPositiveException; import org.apache.commons.math3.exception.NotStrictlyPositiveException; import org.apache.commons.math3.exception.NumberIsTooLargeException; import org.apache.commons.math3.exception.OutOfRangeException; import org.apache.commons.math3.exception.util.LocalizedFormats; /** * Implements the {@link RandomData} interface using a {@link RandomGenerator} * instance to generate non-secure data and a {@link java.security.SecureRandom} * instance to provide data for the nextSecureXxx methods. If no * RandomGenerator is provided in the constructor, the default is * to use a {@link Well19937c} generator. To plug in a different * implementation, either implement RandomGenerator directly or * extend {@link AbstractRandomGenerator}. *

                              * Supports reseeding the underlying pseudo-random number generator (PRNG). The * SecurityProvider and Algorithm used by the * SecureRandom instance can also be reset. *

                              *

                              * For details on the default PRNGs, see {@link java.util.Random} and * {@link java.security.SecureRandom}. *

                              *

                              * Usage Notes: *

                                *
                              • * Instance variables are used to maintain RandomGenerator and * SecureRandom instances used in data generation. Therefore, to * generate a random sequence of values or strings, you should use just * one RandomDataImpl instance repeatedly.
                              • *
                              • * The "secure" methods are *much* slower. These should be used only when a * cryptographically secure random sequence is required. A secure random * sequence is a sequence of pseudo-random values which, in addition to being * well-dispersed (so no subsequence of values is an any more likely than other * subsequence of the the same length), also has the additional property that * knowledge of values generated up to any point in the sequence does not make * it any easier to predict subsequent values.
                              • *
                              • * When a new RandomDataImpl is created, the underlying random * number generators are not initialized. If you do not * explicitly seed the default non-secure generator, it is seeded with the * current time in milliseconds plus the system identity hash code on first use. * The same holds for the secure generator. If you provide a RandomGenerator * to the constructor, however, this generator is not reseeded by the constructor * nor is it reseeded on first use.
                              • *
                              • * The reSeed and reSeedSecure methods delegate to the * corresponding methods on the underlying RandomGenerator and * SecureRandom instances. Therefore, reSeed(long) * fully resets the initial state of the non-secure random number generator (so * that reseeding with a specific value always results in the same subsequent * random sequence); whereas reSeedSecure(long) does not * reinitialize the secure random number generator (so secure sequences started * with calls to reseedSecure(long) won't be identical).
                              • *
                              • * This implementation is not synchronized. The underlying RandomGenerator * or SecureRandom instances are not protected by synchronization and * are not guaranteed to be thread-safe. Therefore, if an instance of this class * is concurrently utilized by multiple threads, it is the responsibility of * client code to synchronize access to seeding and data generation methods. *
                              • *
                              *

                              * @since 3.1 * @version $Id: RandomDataGenerator.java 1462423 2013-03-29 07:25:18Z luc $ */ public class RandomDataGenerator implements RandomData, Serializable { /** Serializable version identifier */ private static final long serialVersionUID = -626730818244969716L; /** underlying random number generator */ private RandomGenerator rand = null; /** underlying secure random number generator */ private SecureRandom secRand = null; /** * Construct a RandomDataGenerator, using a default random generator as the source * of randomness. * *

                              The default generator is a {@link Well19937c} seeded * with {@code System.currentTimeMillis() + System.identityHashCode(this))}. * The generator is initialized and seeded on first use.

                              */ public RandomDataGenerator() { } /** * Construct a RandomDataGenerator using the supplied {@link RandomGenerator} as * the source of (non-secure) random data. * * @param rand the source of (non-secure) random data * (may be null, resulting in the default generator) */ public RandomDataGenerator(RandomGenerator rand) { this.rand = rand; } /** * {@inheritDoc} *

                              * Algorithm Description: hex strings are generated using a * 2-step process. *

                                *
                              1. {@code len / 2 + 1} binary bytes are generated using the underlying * Random
                              2. *
                              3. Each binary byte is translated into 2 hex digits
                              4. *
                              *

                              * * @param len the desired string length. * @return the random string. * @throws NotStrictlyPositiveException if {@code len <= 0}. */ public String nextHexString(int len) throws NotStrictlyPositiveException { if (len <= 0) { throw new NotStrictlyPositiveException(LocalizedFormats.LENGTH, len); } // Get a random number generator RandomGenerator ran = getRandomGenerator(); // Initialize output buffer StringBuilder outBuffer = new StringBuilder(); // Get int(len/2)+1 random bytes byte[] randomBytes = new byte[(len / 2) + 1]; ran.nextBytes(randomBytes); // Convert each byte to 2 hex digits for (int i = 0; i < randomBytes.length; i++) { Integer c = Integer.valueOf(randomBytes[i]); /* * Add 128 to byte value to make interval 0-255 before doing hex * conversion. This guarantees <= 2 hex digits from toHexString() * toHexString would otherwise add 2^32 to negative arguments. */ String hex = Integer.toHexString(c.intValue() + 128); // Make sure we add 2 hex digits for each byte if (hex.length() == 1) { hex = "0" + hex; } outBuffer.append(hex); } return outBuffer.toString().substring(0, len); } /** {@inheritDoc} */ public int nextInt(final int lower, final int upper) throws NumberIsTooLargeException { if (lower >= upper) { throw new NumberIsTooLargeException(LocalizedFormats.LOWER_BOUND_NOT_BELOW_UPPER_BOUND, lower, upper, false); } final int max = (upper - lower) + 1; if (max <= 0) { // the range is too wide to fit in a positive int (larger than 2^31); as it covers // more than half the integer range, we use directly a simple rejection method final RandomGenerator rng = getRandomGenerator(); while (true) { final int r = rng.nextInt(); if (r >= lower && r <= upper) { return r; } } } else { // we can shift the range and generate directly a positive int return lower + getRandomGenerator().nextInt(max); } } /** {@inheritDoc} */ public long nextLong(final long lower, final long upper) throws NumberIsTooLargeException { if (lower >= upper) { throw new NumberIsTooLargeException(LocalizedFormats.LOWER_BOUND_NOT_BELOW_UPPER_BOUND, lower, upper, false); } final long max = (upper - lower) + 1; if (max <= 0) { // the range is too wide to fit in a positive long (larger than 2^63); as it covers // more than half the long range, we use directly a simple rejection method final RandomGenerator rng = getRandomGenerator(); while (true) { final long r = rng.nextLong(); if (r >= lower && r <= upper) { return r; } } } else if (max < Integer.MAX_VALUE){ // we can shift the range and generate directly a positive int return lower + getRandomGenerator().nextInt((int) max); } else { // we can shift the range and generate directly a positive long return lower + nextLong(getRandomGenerator(), max); } } /** * Returns a pseudorandom, uniformly distributed long value * between 0 (inclusive) and the specified value (exclusive), drawn from * this random number generator's sequence. * * @param rng random generator to use * @param n the bound on the random number to be returned. Must be * positive. * @return a pseudorandom, uniformly distributed long * value between 0 (inclusive) and n (exclusive). * @throws IllegalArgumentException if n is not positive. */ private static long nextLong(final RandomGenerator rng, final long n) throws IllegalArgumentException { if (n > 0) { final byte[] byteArray = new byte[8]; long bits; long val; do { rng.nextBytes(byteArray); bits = 0; for (final byte b : byteArray) { bits = (bits << 8) | (((long) b) & 0xffL); } bits = bits & 0x7fffffffffffffffL; val = bits % n; } while (bits - val + (n - 1) < 0); return val; } throw new NotStrictlyPositiveException(n); } /** * {@inheritDoc} *

                              * Algorithm Description: hex strings are generated in * 40-byte segments using a 3-step process. *

                                *
                              1. * 20 random bytes are generated using the underlying * SecureRandom.
                              2. *
                              3. * SHA-1 hash is applied to yield a 20-byte binary digest.
                              4. *
                              5. * Each byte of the binary digest is converted to 2 hex digits.
                              6. *
                              *

                              * @throws NotStrictlyPositiveException if {@code len <= 0} */ public String nextSecureHexString(int len) throws NotStrictlyPositiveException { if (len <= 0) { throw new NotStrictlyPositiveException(LocalizedFormats.LENGTH, len); } // Get SecureRandom and setup Digest provider SecureRandom secRan = getSecRan(); MessageDigest alg = null; try { alg = MessageDigest.getInstance("SHA-1"); } catch (NoSuchAlgorithmException ex) { // this should never happen throw new MathInternalError(ex); } alg.reset(); // Compute number of iterations required (40 bytes each) int numIter = (len / 40) + 1; StringBuilder outBuffer = new StringBuilder(); for (int iter = 1; iter < numIter + 1; iter++) { byte[] randomBytes = new byte[40]; secRan.nextBytes(randomBytes); alg.update(randomBytes); // Compute hash -- will create 20-byte binary hash byte[] hash = alg.digest(); // Loop over the hash, converting each byte to 2 hex digits for (int i = 0; i < hash.length; i++) { Integer c = Integer.valueOf(hash[i]); /* * Add 128 to byte value to make interval 0-255 This guarantees * <= 2 hex digits from toHexString() toHexString would * otherwise add 2^32 to negative arguments */ String hex = Integer.toHexString(c.intValue() + 128); // Keep strings uniform length -- guarantees 40 bytes if (hex.length() == 1) { hex = "0" + hex; } outBuffer.append(hex); } } return outBuffer.toString().substring(0, len); } /** {@inheritDoc} */ public int nextSecureInt(final int lower, final int upper) throws NumberIsTooLargeException { if (lower >= upper) { throw new NumberIsTooLargeException(LocalizedFormats.LOWER_BOUND_NOT_BELOW_UPPER_BOUND, lower, upper, false); } final int max = (upper - lower) + 1; if (max <= 0) { // the range is too wide to fit in a positive int (larger than 2^31); as it covers // more than half the integer range, we use directly a simple rejection method final SecureRandom rng = getSecRan(); while (true) { final int r = rng.nextInt(); if (r >= lower && r <= upper) { return r; } } } else { // we can shift the range and generate directly a positive int return lower + getSecRan().nextInt(max); } } /** {@inheritDoc} */ public long nextSecureLong(final long lower, final long upper) throws NumberIsTooLargeException { if (lower >= upper) { throw new NumberIsTooLargeException(LocalizedFormats.LOWER_BOUND_NOT_BELOW_UPPER_BOUND, lower, upper, false); } final long max = (upper - lower) + 1; if (max <= 0) { // the range is too wide to fit in a positive long (larger than 2^63); as it covers // more than half the long range, we use directly a simple rejection method final SecureRandom rng = getSecRan(); while (true) { final long r = rng.nextLong(); if (r >= lower && r <= upper) { return r; } } } else if (max < Integer.MAX_VALUE){ // we can shift the range and generate directly a positive int return lower + getSecRan().nextInt((int) max); } else { // we can shift the range and generate directly a positive long return lower + nextLong(getSecRan(), max); } } /** * Returns a pseudorandom, uniformly distributed long value * between 0 (inclusive) and the specified value (exclusive), drawn from * this random number generator's sequence. * * @param rng random generator to use * @param n the bound on the random number to be returned. Must be * positive. * @return a pseudorandom, uniformly distributed long * value between 0 (inclusive) and n (exclusive). * @throws IllegalArgumentException if n is not positive. */ private static long nextLong(final SecureRandom rng, final long n) throws IllegalArgumentException { if (n > 0) { final byte[] byteArray = new byte[8]; long bits; long val; do { rng.nextBytes(byteArray); bits = 0; for (final byte b : byteArray) { bits = (bits << 8) | (((long) b) & 0xffL); } bits = bits & 0x7fffffffffffffffL; val = bits % n; } while (bits - val + (n - 1) < 0); return val; } throw new NotStrictlyPositiveException(n); } /** * {@inheritDoc} *

                              * Algorithm Description: *

                              • For small means, uses simulation of a Poisson process * using Uniform deviates, as described * here. * The Poisson process (and hence value returned) is bounded by 1000 * mean.
                              • * *
                              • For large means, uses the rejection algorithm described in
                                * Devroye, Luc. (1981).The Computer Generation of Poisson Random Variables * Computing vol. 26 pp. 197-207.

                              * @throws NotStrictlyPositiveException if {@code len <= 0} */ public long nextPoisson(double mean) throws NotStrictlyPositiveException { return new PoissonDistribution(getRandomGenerator(), mean, PoissonDistribution.DEFAULT_EPSILON, PoissonDistribution.DEFAULT_MAX_ITERATIONS).sample(); } /** {@inheritDoc} */ public double nextGaussian(double mu, double sigma) throws NotStrictlyPositiveException { if (sigma <= 0) { throw new NotStrictlyPositiveException(LocalizedFormats.STANDARD_DEVIATION, sigma); } return sigma * getRandomGenerator().nextGaussian() + mu; } /** * {@inheritDoc} * *

                              * Algorithm Description: Uses the Algorithm SA (Ahrens) * from p. 876 in: * [1]: Ahrens, J. H. and Dieter, U. (1972). Computer methods for * sampling from the exponential and normal distributions. * Communications of the ACM, 15, 873-882. *

                              */ public double nextExponential(double mean) throws NotStrictlyPositiveException { return new ExponentialDistribution(getRandomGenerator(), mean, ExponentialDistribution.DEFAULT_INVERSE_ABSOLUTE_ACCURACY).sample(); } /** *

                              Generates a random value from the * {@link org.apache.commons.math3.distribution.GammaDistribution Gamma Distribution}.

                              * *

                              This implementation uses the following algorithms:

                              * *

                              For 0 < shape < 1:
                              * Ahrens, J. H. and Dieter, U., Computer methods for * sampling from gamma, beta, Poisson and binomial distributions. * Computing, 12, 223-246, 1974.

                              * *

                              For shape >= 1:
                              * Marsaglia and Tsang, A Simple Method for Generating * Gamma Variables. ACM Transactions on Mathematical Software, * Volume 26 Issue 3, September, 2000.

                              * * @param shape the median of the Gamma distribution * @param scale the scale parameter of the Gamma distribution * @return random value sampled from the Gamma(shape, scale) distribution * @throws NotStrictlyPositiveException if {@code shape <= 0} or * {@code scale <= 0}. */ public double nextGamma(double shape, double scale) throws NotStrictlyPositiveException { return new GammaDistribution(getRandomGenerator(),shape, scale, GammaDistribution.DEFAULT_INVERSE_ABSOLUTE_ACCURACY).sample(); } /** * Generates a random value from the {@link HypergeometricDistribution Hypergeometric Distribution}. * * @param populationSize the population size of the Hypergeometric distribution * @param numberOfSuccesses number of successes in the population of the Hypergeometric distribution * @param sampleSize the sample size of the Hypergeometric distribution * @return random value sampled from the Hypergeometric(numberOfSuccesses, sampleSize) distribution * @throws NumberIsTooLargeException if {@code numberOfSuccesses > populationSize}, * or {@code sampleSize > populationSize}. * @throws NotStrictlyPositiveException if {@code populationSize <= 0}. * @throws NotPositiveException if {@code numberOfSuccesses < 0}. */ public int nextHypergeometric(int populationSize, int numberOfSuccesses, int sampleSize) throws NotPositiveException, NotStrictlyPositiveException, NumberIsTooLargeException { return new HypergeometricDistribution(getRandomGenerator(),populationSize, numberOfSuccesses, sampleSize).sample(); } /** * Generates a random value from the {@link PascalDistribution Pascal Distribution}. * * @param r the number of successes of the Pascal distribution * @param p the probability of success of the Pascal distribution * @return random value sampled from the Pascal(r, p) distribution * @throws NotStrictlyPositiveException if the number of successes is not positive * @throws OutOfRangeException if the probability of success is not in the * range {@code [0, 1]}. */ public int nextPascal(int r, double p) throws NotStrictlyPositiveException, OutOfRangeException { return new PascalDistribution(getRandomGenerator(), r, p).sample(); } /** * Generates a random value from the {@link TDistribution T Distribution}. * * @param df the degrees of freedom of the T distribution * @return random value from the T(df) distribution * @throws NotStrictlyPositiveException if {@code df <= 0} */ public double nextT(double df) throws NotStrictlyPositiveException { return new TDistribution(getRandomGenerator(), df, TDistribution.DEFAULT_INVERSE_ABSOLUTE_ACCURACY).sample(); } /** * Generates a random value from the {@link WeibullDistribution Weibull Distribution}. * * @param shape the shape parameter of the Weibull distribution * @param scale the scale parameter of the Weibull distribution * @return random value sampled from the Weibull(shape, size) distribution * @throws NotStrictlyPositiveException if {@code shape <= 0} or * {@code scale <= 0}. */ public double nextWeibull(double shape, double scale) throws NotStrictlyPositiveException { return new WeibullDistribution(getRandomGenerator(), shape, scale, WeibullDistribution.DEFAULT_INVERSE_ABSOLUTE_ACCURACY).sample(); } /** * Generates a random value from the {@link ZipfDistribution Zipf Distribution}. * * @param numberOfElements the number of elements of the ZipfDistribution * @param exponent the exponent of the ZipfDistribution * @return random value sampled from the Zipf(numberOfElements, exponent) distribution * @exception NotStrictlyPositiveException if {@code numberOfElements <= 0} * or {@code exponent <= 0}. */ public int nextZipf(int numberOfElements, double exponent) throws NotStrictlyPositiveException { return new ZipfDistribution(getRandomGenerator(), numberOfElements, exponent).sample(); } /** * Generates a random value from the {@link BetaDistribution Beta Distribution}. * * @param alpha first distribution shape parameter * @param beta second distribution shape parameter * @return random value sampled from the beta(alpha, beta) distribution */ public double nextBeta(double alpha, double beta) { return new BetaDistribution(getRandomGenerator(), alpha, beta, BetaDistribution.DEFAULT_INVERSE_ABSOLUTE_ACCURACY).sample(); } /** * Generates a random value from the {@link BinomialDistribution Binomial Distribution}. * * @param numberOfTrials number of trials of the Binomial distribution * @param probabilityOfSuccess probability of success of the Binomial distribution * @return random value sampled from the Binomial(numberOfTrials, probabilityOfSuccess) distribution */ public int nextBinomial(int numberOfTrials, double probabilityOfSuccess) { return new BinomialDistribution(getRandomGenerator(), numberOfTrials, probabilityOfSuccess).sample(); } /** * Generates a random value from the {@link CauchyDistribution Cauchy Distribution}. * * @param median the median of the Cauchy distribution * @param scale the scale parameter of the Cauchy distribution * @return random value sampled from the Cauchy(median, scale) distribution */ public double nextCauchy(double median, double scale) { return new CauchyDistribution(getRandomGenerator(), median, scale, CauchyDistribution.DEFAULT_INVERSE_ABSOLUTE_ACCURACY).sample(); } /** * Generates a random value from the {@link ChiSquaredDistribution ChiSquare Distribution}. * * @param df the degrees of freedom of the ChiSquare distribution * @return random value sampled from the ChiSquare(df) distribution */ public double nextChiSquare(double df) { return new ChiSquaredDistribution(getRandomGenerator(), df, ChiSquaredDistribution.DEFAULT_INVERSE_ABSOLUTE_ACCURACY).sample(); } /** * Generates a random value from the {@link FDistribution F Distribution}. * * @param numeratorDf the numerator degrees of freedom of the F distribution * @param denominatorDf the denominator degrees of freedom of the F distribution * @return random value sampled from the F(numeratorDf, denominatorDf) distribution * @throws NotStrictlyPositiveException if * {@code numeratorDf <= 0} or {@code denominatorDf <= 0}. */ public double nextF(double numeratorDf, double denominatorDf) throws NotStrictlyPositiveException { return new FDistribution(getRandomGenerator(), numeratorDf, denominatorDf, FDistribution.DEFAULT_INVERSE_ABSOLUTE_ACCURACY).sample(); } /** * {@inheritDoc} * *

                              * Algorithm Description: scales the output of * Random.nextDouble(), but rejects 0 values (i.e., will generate another * random double if Random.nextDouble() returns 0). This is necessary to * provide a symmetric output interval (both endpoints excluded). *

                              * @throws NumberIsTooLargeException if {@code lower >= upper} * @throws NotFiniteNumberException if one of the bounds is infinite * @throws NotANumberException if one of the bounds is NaN */ public double nextUniform(double lower, double upper) throws NumberIsTooLargeException, NotFiniteNumberException, NotANumberException { return nextUniform(lower, upper, false); } /** * {@inheritDoc} * *

                              * Algorithm Description: if the lower bound is excluded, * scales the output of Random.nextDouble(), but rejects 0 values (i.e., * will generate another random double if Random.nextDouble() returns 0). * This is necessary to provide a symmetric output interval (both * endpoints excluded). *

                              * * @throws NumberIsTooLargeException if {@code lower >= upper} * @throws NotFiniteNumberException if one of the bounds is infinite * @throws NotANumberException if one of the bounds is NaN */ public double nextUniform(double lower, double upper, boolean lowerInclusive) throws NumberIsTooLargeException, NotFiniteNumberException, NotANumberException { if (lower >= upper) { throw new NumberIsTooLargeException(LocalizedFormats.LOWER_BOUND_NOT_BELOW_UPPER_BOUND, lower, upper, false); } if (Double.isInfinite(lower)) { throw new NotFiniteNumberException(LocalizedFormats.INFINITE_BOUND, lower); } if (Double.isInfinite(upper)) { throw new NotFiniteNumberException(LocalizedFormats.INFINITE_BOUND, upper); } if (Double.isNaN(lower) || Double.isNaN(upper)) { throw new NotANumberException(); } final RandomGenerator generator = getRandomGenerator(); // ensure nextDouble() isn't 0.0 double u = generator.nextDouble(); while (!lowerInclusive && u <= 0.0) { u = generator.nextDouble(); } return u * upper + (1.0 - u) * lower; } /** * {@inheritDoc} * *

                              * Uses a 2-cycle permutation shuffle. The shuffling process is described * here. *

                              * @throws NumberIsTooLargeException if {@code k > n}. * @throws NotStrictlyPositiveException if {@code k <= 0}. */ public int[] nextPermutation(int n, int k) throws NumberIsTooLargeException, NotStrictlyPositiveException { if (k > n) { throw new NumberIsTooLargeException(LocalizedFormats.PERMUTATION_EXCEEDS_N, k, n, true); } if (k <= 0) { throw new NotStrictlyPositiveException(LocalizedFormats.PERMUTATION_SIZE, k); } int[] index = getNatural(n); shuffle(index, n - k); int[] result = new int[k]; for (int i = 0; i < k; i++) { result[i] = index[n - i - 1]; } return result; } /** * {@inheritDoc} * *

                              * Algorithm Description: Uses a 2-cycle permutation * shuffle to generate a random permutation of c.size() and * then returns the elements whose indexes correspond to the elements of the * generated permutation. This technique is described, and proven to * generate random samples * here *

                              */ public Object[] nextSample(Collection c, int k) throws NumberIsTooLargeException, NotStrictlyPositiveException { int len = c.size(); if (k > len) { throw new NumberIsTooLargeException(LocalizedFormats.SAMPLE_SIZE_EXCEEDS_COLLECTION_SIZE, k, len, true); } if (k <= 0) { throw new NotStrictlyPositiveException(LocalizedFormats.NUMBER_OF_SAMPLES, k); } Object[] objects = c.toArray(); int[] index = nextPermutation(len, k); Object[] result = new Object[k]; for (int i = 0; i < k; i++) { result[i] = objects[index[i]]; } return result; } /** * Reseeds the random number generator with the supplied seed. *

                              * Will create and initialize if null. *

                              * * @param seed the seed value to use */ public void reSeed(long seed) { getRandomGenerator().setSeed(seed); } /** * Reseeds the secure random number generator with the current time in * milliseconds. *

                              * Will create and initialize if null. *

                              */ public void reSeedSecure() { getSecRan().setSeed(System.currentTimeMillis()); } /** * Reseeds the secure random number generator with the supplied seed. *

                              * Will create and initialize if null. *

                              * * @param seed the seed value to use */ public void reSeedSecure(long seed) { getSecRan().setSeed(seed); } /** * Reseeds the random number generator with * {@code System.currentTimeMillis() + System.identityHashCode(this))}. */ public void reSeed() { getRandomGenerator().setSeed(System.currentTimeMillis() + System.identityHashCode(this)); } /** * Sets the PRNG algorithm for the underlying SecureRandom instance using * the Security Provider API. The Security Provider API is defined in * Java Cryptography Architecture API Specification & Reference. *

                              * USAGE NOTE: This method carries significant * overhead and may take several seconds to execute. *

                              * * @param algorithm the name of the PRNG algorithm * @param provider the name of the provider * @throws NoSuchAlgorithmException if the specified algorithm is not available * @throws NoSuchProviderException if the specified provider is not installed */ public void setSecureAlgorithm(String algorithm, String provider) throws NoSuchAlgorithmException, NoSuchProviderException { secRand = SecureRandom.getInstance(algorithm, provider); } /** * Returns the RandomGenerator used to generate non-secure random data. *

                              * Creates and initializes a default generator if null. Uses a {@link Well19937c} * generator with {@code System.currentTimeMillis() + System.identityHashCode(this))} * as the default seed. *

                              * * @return the Random used to generate random data * @since 3.2 */ public RandomGenerator getRandomGenerator() { if (rand == null) { initRan(); } return rand; } /** * Sets the default generator to a {@link Well19937c} generator seeded with * {@code System.currentTimeMillis() + System.identityHashCode(this))}. */ private void initRan() { rand = new Well19937c(System.currentTimeMillis() + System.identityHashCode(this)); } /** * Returns the SecureRandom used to generate secure random data. *

                              * Creates and initializes if null. Uses * {@code System.currentTimeMillis() + System.identityHashCode(this)} as the default seed. *

                              * * @return the SecureRandom used to generate secure random data */ private SecureRandom getSecRan() { if (secRand == null) { secRand = new SecureRandom(); secRand.setSeed(System.currentTimeMillis() + System.identityHashCode(this)); } return secRand; } /** * Uses a 2-cycle permutation shuffle to randomly re-order the last elements * of list. * * @param list list to be shuffled * @param end element past which shuffling begins */ private void shuffle(int[] list, int end) { int target = 0; for (int i = list.length - 1; i >= end; i--) { if (i == 0) { target = 0; } else { // NumberIsTooLargeException cannot occur target = nextInt(0, i); } int temp = list[target]; list[target] = list[i]; list[i] = temp; } } /** * Returns an array representing n. * * @param n the natural number to represent * @return array with entries = elements of n */ private int[] getNatural(int n) { int[] natural = new int[n]; for (int i = 0; i < n; i++) { natural[i] = i; } return natural; } } ././@LongLink100644 0 0 153 12126630647 10261 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/random/UncorrelatedRandomVectorGenerator.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/random/UncorrelatedRandomVectorGenerato100644 1750 1750 6477 12126627700 32400 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.random; import java.util.Arrays; import org.apache.commons.math3.exception.DimensionMismatchException; /** * A {@link RandomVectorGenerator} that generates vectors with uncorrelated * components. Components of generated vectors follow (independent) Gaussian * distributions, with parameters supplied in the constructor. * * @version $Id: UncorrelatedRandomVectorGenerator.java 1416643 2012-12-03 19:37:14Z tn $ * @since 1.2 */ public class UncorrelatedRandomVectorGenerator implements RandomVectorGenerator { /** Underlying scalar generator. */ private final NormalizedRandomGenerator generator; /** Mean vector. */ private final double[] mean; /** Standard deviation vector. */ private final double[] standardDeviation; /** Simple constructor. *

                              Build an uncorrelated random vector generator from * its mean and standard deviation vectors.

                              * @param mean expected mean values for each component * @param standardDeviation standard deviation for each component * @param generator underlying generator for uncorrelated normalized * components */ public UncorrelatedRandomVectorGenerator(double[] mean, double[] standardDeviation, NormalizedRandomGenerator generator) { if (mean.length != standardDeviation.length) { throw new DimensionMismatchException(mean.length, standardDeviation.length); } this.mean = mean.clone(); this.standardDeviation = standardDeviation.clone(); this.generator = generator; } /** Simple constructor. *

                              Build a null mean random and unit standard deviation * uncorrelated vector generator

                              * @param dimension dimension of the vectors to generate * @param generator underlying generator for uncorrelated normalized * components */ public UncorrelatedRandomVectorGenerator(int dimension, NormalizedRandomGenerator generator) { mean = new double[dimension]; standardDeviation = new double[dimension]; Arrays.fill(standardDeviation, 1.0); this.generator = generator; } /** Generate an uncorrelated random vector. * @return a random vector as a newly built array of double */ public double[] nextVector() { double[] random = new double[mean.length]; for (int i = 0; i < random.length; ++i) { random[i] = mean[i] + standardDeviation[i] * generator.nextNormalizedDouble(); } return random; } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/random/ValueServer.java100644 1750 1750 40614 12126627700 27132 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.random; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.net.MalformedURLException; import java.net.URL; import org.apache.commons.math3.exception.MathIllegalArgumentException; import org.apache.commons.math3.exception.MathIllegalStateException; import org.apache.commons.math3.exception.NullArgumentException; import org.apache.commons.math3.exception.ZeroException; import org.apache.commons.math3.exception.util.LocalizedFormats; /** * Generates values for use in simulation applications. *

                              * How values are generated is determined by the mode * property.

                              *

                              * Supported mode values are:

                                *
                              • DIGEST_MODE -- uses an empirical distribution
                              • *
                              • REPLAY_MODE -- replays data from valuesFileURL
                              • *
                              • UNIFORM_MODE -- generates uniformly distributed random values with * mean = mu
                              • *
                              • EXPONENTIAL_MODE -- generates exponentially distributed random values * with mean = mu
                              • *
                              • GAUSSIAN_MODE -- generates Gaussian distributed random values with * mean = mu and * standard deviation = sigma
                              • *
                              • CONSTANT_MODE -- returns mu every time.

                              * * @version $Id: ValueServer.java 1422350 2012-12-15 20:47:47Z psteitz $ * */ public class ValueServer { /** Use empirical distribution. */ public static final int DIGEST_MODE = 0; /** Replay data from valuesFilePath. */ public static final int REPLAY_MODE = 1; /** Uniform random deviates with mean = μ. */ public static final int UNIFORM_MODE = 2; /** Exponential random deviates with mean = μ. */ public static final int EXPONENTIAL_MODE = 3; /** Gaussian random deviates with mean = μ, std dev = σ. */ public static final int GAUSSIAN_MODE = 4; /** Always return mu */ public static final int CONSTANT_MODE = 5; /** mode determines how values are generated. */ private int mode = 5; /** URI to raw data values. */ private URL valuesFileURL = null; /** Mean for use with non-data-driven modes. */ private double mu = 0.0; /** Standard deviation for use with GAUSSIAN_MODE. */ private double sigma = 0.0; /** Empirical probability distribution for use with DIGEST_MODE. */ private EmpiricalDistribution empiricalDistribution = null; /** File pointer for REPLAY_MODE. */ private BufferedReader filePointer = null; /** RandomDataImpl to use for random data generation. */ private final RandomDataImpl randomData; // Data generation modes ====================================== /** Creates new ValueServer */ public ValueServer() { randomData = new RandomDataImpl(); } /** * Construct a ValueServer instance using a RandomDataImpl as its source * of random data. * * @param randomData the RandomDataImpl instance used to source random data * @since 3.0 * @deprecated use {@link #ValueServer(RandomGenerator)} */ public ValueServer(RandomDataImpl randomData) { this.randomData = randomData; } /** * Construct a ValueServer instance using a RandomGenerator as its source * of random data. * * @since 3.1 * @param generator source of random data */ public ValueServer(RandomGenerator generator) { this.randomData = new RandomDataImpl(generator); } /** * Returns the next generated value, generated according * to the mode value (see MODE constants). * * @return generated value * @throws IOException in REPLAY_MODE if a file I/O error occurs * @throws MathIllegalStateException if mode is not recognized * @throws MathIllegalArgumentException if the underlying random generator thwrows one */ public double getNext() throws IOException, MathIllegalStateException, MathIllegalArgumentException { switch (mode) { case DIGEST_MODE: return getNextDigest(); case REPLAY_MODE: return getNextReplay(); case UNIFORM_MODE: return getNextUniform(); case EXPONENTIAL_MODE: return getNextExponential(); case GAUSSIAN_MODE: return getNextGaussian(); case CONSTANT_MODE: return mu; default: throw new MathIllegalStateException( LocalizedFormats.UNKNOWN_MODE, mode, "DIGEST_MODE", DIGEST_MODE, "REPLAY_MODE", REPLAY_MODE, "UNIFORM_MODE", UNIFORM_MODE, "EXPONENTIAL_MODE", EXPONENTIAL_MODE, "GAUSSIAN_MODE", GAUSSIAN_MODE, "CONSTANT_MODE", CONSTANT_MODE); } } /** * Fills the input array with values generated using getNext() repeatedly. * * @param values array to be filled * @throws IOException in REPLAY_MODE if a file I/O error occurs * @throws MathIllegalStateException if mode is not recognized * @throws MathIllegalArgumentException if the underlying random generator thwrows one */ public void fill(double[] values) throws IOException, MathIllegalStateException, MathIllegalArgumentException { for (int i = 0; i < values.length; i++) { values[i] = getNext(); } } /** * Returns an array of length length with values generated * using getNext() repeatedly. * * @param length length of output array * @return array of generated values * @throws IOException in REPLAY_MODE if a file I/O error occurs * @throws MathIllegalStateException if mode is not recognized * @throws MathIllegalArgumentException if the underlying random generator thwrows one */ public double[] fill(int length) throws IOException, MathIllegalStateException, MathIllegalArgumentException { double[] out = new double[length]; for (int i = 0; i < length; i++) { out[i] = getNext(); } return out; } /** * Computes the empirical distribution using values from the file * in valuesFileURL, using the default number of bins. *

                              * valuesFileURL must exist and be * readable by *this at runtime.

                              *

                              * This method must be called before using getNext() * with mode = DIGEST_MODE

                              * * @throws IOException if an I/O error occurs reading the input file * @throws NullArgumentException if the {@code valuesFileURL} has not been set * @throws ZeroException if URL contains no data */ public void computeDistribution() throws IOException, ZeroException, NullArgumentException { computeDistribution(EmpiricalDistribution.DEFAULT_BIN_COUNT); } /** * Computes the empirical distribution using values from the file * in valuesFileURL and binCount bins. *

                              * valuesFileURL must exist and be readable by this process * at runtime.

                              *

                              * This method must be called before using getNext() * with mode = DIGEST_MODE

                              * * @param binCount the number of bins used in computing the empirical * distribution * @throws NullArgumentException if the {@code valuesFileURL} has not been set * @throws IOException if an error occurs reading the input file * @throws ZeroException if URL contains no data */ public void computeDistribution(int binCount) throws NullArgumentException, IOException, ZeroException { empiricalDistribution = new EmpiricalDistribution(binCount, randomData); empiricalDistribution.load(valuesFileURL); mu = empiricalDistribution.getSampleStats().getMean(); sigma = empiricalDistribution.getSampleStats().getStandardDeviation(); } /** * Returns the data generation mode. See {@link ValueServer the class javadoc} * for description of the valid values of this property. * * @return Value of property mode. */ public int getMode() { return mode; } /** * Sets the data generation mode. * * @param mode New value of the data generation mode. */ public void setMode(int mode) { this.mode = mode; } /** * Returns the URL for the file used to build the empirical distribution * when using {@link #DIGEST_MODE}. * * @return Values file URL. */ public URL getValuesFileURL() { return valuesFileURL; } /** * Sets the {@link #getValuesFileURL() values file URL} using a string * URL representation. * * @param url String representation for new valuesFileURL. * @throws MalformedURLException if url is not well formed */ public void setValuesFileURL(String url) throws MalformedURLException { this.valuesFileURL = new URL(url); } /** * Sets the the {@link #getValuesFileURL() values file URL}. * *

                              The values file must be an ASCII text file containing one * valid numeric entry per line.

                              * * @param url URL of the values file. */ public void setValuesFileURL(URL url) { this.valuesFileURL = url; } /** * Returns the {@link EmpiricalDistribution} used when operating in {@value #DIGEST_MODE}. * * @return EmpircalDistribution built by {@link #computeDistribution()} */ public EmpiricalDistribution getEmpiricalDistribution() { return empiricalDistribution; } /** * Resets REPLAY_MODE file pointer to the beginning of the valuesFileURL. * * @throws IOException if an error occurs opening the file */ public void resetReplayFile() throws IOException { if (filePointer != null) { try { filePointer.close(); filePointer = null; } catch (IOException ex) { //NOPMD // ignore } } filePointer = new BufferedReader(new InputStreamReader(valuesFileURL.openStream(), "UTF-8")); } /** * Closes {@code valuesFileURL} after use in REPLAY_MODE. * * @throws IOException if an error occurs closing the file */ public void closeReplayFile() throws IOException { if (filePointer != null) { filePointer.close(); filePointer = null; } } /** * Returns the mean used when operating in {@link #GAUSSIAN_MODE}, {@link #EXPONENTIAL_MODE} * or {@link #UNIFORM_MODE}. When operating in {@link #CONSTANT_MODE}, this is the constant * value always returned. Calling {@link #computeDistribution()} sets this value to the * overall mean of the values in the {@link #getValuesFileURL() values file}. * * @return Mean used in data generation. */ public double getMu() { return mu; } /** * Sets the {@link #getMu() mean} used in data generation. Note that calling this method * after {@link #computeDistribution()} has been called will have no effect on data * generated in {@link #DIGEST_MODE}. * * @param mu new Mean value. */ public void setMu(double mu) { this.mu = mu; } /** * Returns the standard deviation used when operating in {@link #GAUSSIAN_MODE}. * Calling {@link #computeDistribution()} sets this value to the overall standard * deviation of the values in the {@link #getValuesFileURL() values file}. This * property has no effect when the data generation mode is not * {@link #GAUSSIAN_MODE}. * * @return Standard deviation used when operating in {@link #GAUSSIAN_MODE}. */ public double getSigma() { return sigma; } /** * Sets the {@link #getSigma() standard deviation} used in {@link #GAUSSIAN_MODE}. * * @param sigma New standard deviation. */ public void setSigma(double sigma) { this.sigma = sigma; } /** * Reseeds the random data generator. * * @param seed Value with which to reseed the {@link RandomDataImpl} * used to generate random data. */ public void reSeed(long seed) { randomData.reSeed(seed); } //------------- private methods --------------------------------- /** * Gets a random value in DIGEST_MODE. *

                              * Preconditions:

                                *
                              • Before this method is called, computeDistribution() * must have completed successfully; otherwise an * IllegalStateException will be thrown

                              * * @return next random value from the empirical distribution digest * @throws MathIllegalStateException if digest has not been initialized */ private double getNextDigest() throws MathIllegalStateException { if ((empiricalDistribution == null) || (empiricalDistribution.getBinStats().size() == 0)) { throw new MathIllegalStateException(LocalizedFormats.DIGEST_NOT_INITIALIZED); } return empiricalDistribution.getNextValue(); } /** * Gets next sequential value from the valuesFileURL. *

                              * Throws an IOException if the read fails.

                              *

                              * This method will open the valuesFileURL if there is no * replay file open.

                              *

                              * The valuesFileURL will be closed and reopened to wrap around * from EOF to BOF if EOF is encountered. EOFException (which is a kind of * IOException) may still be thrown if the valuesFileURL is * empty.

                              * * @return next value from the replay file * @throws IOException if there is a problem reading from the file * @throws MathIllegalStateException if URL contains no data * @throws NumberFormatException if an invalid numeric string is * encountered in the file */ private double getNextReplay() throws IOException, MathIllegalStateException { String str = null; if (filePointer == null) { resetReplayFile(); } if ((str = filePointer.readLine()) == null) { // we have probably reached end of file, wrap around from EOF to BOF closeReplayFile(); resetReplayFile(); if ((str = filePointer.readLine()) == null) { throw new MathIllegalStateException(LocalizedFormats.URL_CONTAINS_NO_DATA, valuesFileURL); } } return Double.valueOf(str).doubleValue(); } /** * Gets a uniformly distributed random value with mean = mu. * * @return random uniform value * @throws MathIllegalArgumentException if the underlying random generator thwrows one */ private double getNextUniform() throws MathIllegalArgumentException { return randomData.nextUniform(0, 2 * mu); } /** * Gets an exponentially distributed random value with mean = mu. * * @return random exponential value * @throws MathIllegalArgumentException if the underlying random generator thwrows one */ private double getNextExponential() throws MathIllegalArgumentException { return randomData.nextExponential(mu); } /** * Gets a Gaussian distributed random value with mean = mu * and standard deviation = sigma. * * @return random Gaussian value * @throws MathIllegalArgumentException if the underlying random generator thwrows one */ private double getNextGaussian() throws MathIllegalArgumentException { return randomData.nextGaussian(mu, sigma); } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/random/Well512a.java100644 1750 1750 7265 12126627700 26150 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.random; /** This class implements the WELL512a pseudo-random number generator * from François Panneton, Pierre L'Ecuyer and Makoto Matsumoto. *

                              This generator is described in a paper by François Panneton, * Pierre L'Ecuyer and Makoto Matsumoto Improved * Long-Period Generators Based on Linear Recurrences Modulo 2 ACM * Transactions on Mathematical Software, 32, 1 (2006). The errata for the paper * are in wellrng-errata.txt.

                              * @see WELL Random number generator * @version $Id: Well512a.java 1416643 2012-12-03 19:37:14Z tn $ * @since 2.2 */ public class Well512a extends AbstractWell { /** Serializable version identifier. */ private static final long serialVersionUID = -6104179812103820574L; /** Number of bits in the pool. */ private static final int K = 512; /** First parameter of the algorithm. */ private static final int M1 = 13; /** Second parameter of the algorithm. */ private static final int M2 = 9; /** Third parameter of the algorithm. */ private static final int M3 = 5; /** Creates a new random number generator. *

                              The instance is initialized using the current time as the * seed.

                              */ public Well512a() { super(K, M1, M2, M3); } /** Creates a new random number generator using a single int seed. * @param seed the initial seed (32 bits integer) */ public Well512a(int seed) { super(K, M1, M2, M3, seed); } /** Creates a new random number generator using an int array seed. * @param seed the initial seed (32 bits integers array), if null * the seed of the generator will be related to the current time */ public Well512a(int[] seed) { super(K, M1, M2, M3, seed); } /** Creates a new random number generator using a single long seed. * @param seed the initial seed (64 bits integer) */ public Well512a(long seed) { super(K, M1, M2, M3, seed); } /** {@inheritDoc} */ @Override protected int next(final int bits) { final int indexRm1 = iRm1[index]; final int vi = v[index]; final int vi1 = v[i1[index]]; final int vi2 = v[i2[index]]; final int z0 = v[indexRm1]; // the values below include the errata of the original article final int z1 = (vi ^ (vi << 16)) ^ (vi1 ^ (vi1 << 15)); final int z2 = vi2 ^ (vi2 >>> 11); final int z3 = z1 ^ z2; final int z4 = (z0 ^ (z0 << 2)) ^ (z1 ^ (z1 << 18)) ^ (z2 << 28) ^ (z3 ^ ((z3 << 5) & 0xda442d24)); v[index] = z3; v[indexRm1] = z4; index = indexRm1; return z4 >>> (32 - bits); } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/random/Well1024a.java100644 1750 1750 7251 12126627700 26222 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.random; /** This class implements the WELL1024a pseudo-random number generator * from François Panneton, Pierre L'Ecuyer and Makoto Matsumoto. *

                              This generator is described in a paper by François Panneton, * Pierre L'Ecuyer and Makoto Matsumoto Improved * Long-Period Generators Based on Linear Recurrences Modulo 2 ACM * Transactions on Mathematical Software, 32, 1 (2006). The errata for the paper * are in wellrng-errata.txt.

                              * @see WELL Random number generator * @version $Id: Well1024a.java 1416643 2012-12-03 19:37:14Z tn $ * @since 2.2 */ public class Well1024a extends AbstractWell { /** Serializable version identifier. */ private static final long serialVersionUID = 5680173464174485492L; /** Number of bits in the pool. */ private static final int K = 1024; /** First parameter of the algorithm. */ private static final int M1 = 3; /** Second parameter of the algorithm. */ private static final int M2 = 24; /** Third parameter of the algorithm. */ private static final int M3 = 10; /** Creates a new random number generator. *

                              The instance is initialized using the current time as the * seed.

                              */ public Well1024a() { super(K, M1, M2, M3); } /** Creates a new random number generator using a single int seed. * @param seed the initial seed (32 bits integer) */ public Well1024a(int seed) { super(K, M1, M2, M3, seed); } /** Creates a new random number generator using an int array seed. * @param seed the initial seed (32 bits integers array), if null * the seed of the generator will be related to the current time */ public Well1024a(int[] seed) { super(K, M1, M2, M3, seed); } /** Creates a new random number generator using a single long seed. * @param seed the initial seed (64 bits integer) */ public Well1024a(long seed) { super(K, M1, M2, M3, seed); } /** {@inheritDoc} */ @Override protected int next(final int bits) { final int indexRm1 = iRm1[index]; final int v0 = v[index]; final int vM1 = v[i1[index]]; final int vM2 = v[i2[index]]; final int vM3 = v[i3[index]]; final int z0 = v[indexRm1]; final int z1 = v0 ^ (vM1 ^ (vM1 >>> 8)); final int z2 = (vM2 ^ (vM2 << 19)) ^ (vM3 ^ (vM3 << 14)); final int z3 = z1 ^ z2; final int z4 = (z0 ^ (z0 << 11)) ^ (z1 ^ (z1 << 7)) ^ (z2 ^ (z2 << 13)); v[index] = z3; v[indexRm1] = z4; index = indexRm1; return z4 >>> (32 - bits); } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/special/package-info.java100644 1750 1750 1625 12126627700 27332 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ /** * Implementations of special functions such as Beta and Gamma. */ package org.apache.commons.math3.special; commons-math3-3.2-src/src/main/java/org/apache/commons/math3/special/Gamma.java100644 1750 1750 66174 12126627700 26062 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.special; import org.apache.commons.math3.exception.MaxCountExceededException; import org.apache.commons.math3.exception.NumberIsTooLargeException; import org.apache.commons.math3.exception.NumberIsTooSmallException; import org.apache.commons.math3.util.ContinuedFraction; import org.apache.commons.math3.util.FastMath; /** *

                              * This is a utility class that provides computation methods related to the * Γ (Gamma) family of functions. *

                              *

                              * Implementation of {@link #invGamma1pm1(double)} and * {@link #logGamma1p(double)} is based on the algorithms described in *

                              * and implemented in the * NSWC Library of Mathematical Functions, * available * here. * This library is "approved for public release", and the * Copyright guidance * indicates that unless otherwise stated in the code, all FORTRAN functions in * this library are license free. Since no such notice appears in the code these * functions can safely be ported to Commons-Math. *

                              * * @version $Id: Gamma.java 1422313 2012-12-15 18:53:41Z psteitz $ */ public class Gamma { /** * Euler-Mascheroni constant * @since 2.0 */ public static final double GAMMA = 0.577215664901532860606512090082; /** * The value of the {@code g} constant in the Lanczos approximation, see * {@link #lanczos(double)}. * @since 3.1 */ public static final double LANCZOS_G = 607.0 / 128.0; /** Maximum allowed numerical error. */ private static final double DEFAULT_EPSILON = 10e-15; /** Lanczos coefficients */ private static final double[] LANCZOS = { 0.99999999999999709182, 57.156235665862923517, -59.597960355475491248, 14.136097974741747174, -0.49191381609762019978, .33994649984811888699e-4, .46523628927048575665e-4, -.98374475304879564677e-4, .15808870322491248884e-3, -.21026444172410488319e-3, .21743961811521264320e-3, -.16431810653676389022e-3, .84418223983852743293e-4, -.26190838401581408670e-4, .36899182659531622704e-5, }; /** Avoid repeated computation of log of 2 PI in logGamma */ private static final double HALF_LOG_2_PI = 0.5 * FastMath.log(2.0 * FastMath.PI); /** The constant value of √(2π). */ private static final double SQRT_TWO_PI = 2.506628274631000502; // limits for switching algorithm in digamma /** C limit. */ private static final double C_LIMIT = 49; /** S limit. */ private static final double S_LIMIT = 1e-5; /* * Constants for the computation of double invGamma1pm1(double). * Copied from DGAM1 in the NSWC library. */ /** The constant {@code A0} defined in {@code DGAM1}. */ private static final double INV_GAMMA1P_M1_A0 = .611609510448141581788E-08; /** The constant {@code A1} defined in {@code DGAM1}. */ private static final double INV_GAMMA1P_M1_A1 = .624730830116465516210E-08; /** The constant {@code B1} defined in {@code DGAM1}. */ private static final double INV_GAMMA1P_M1_B1 = .203610414066806987300E+00; /** The constant {@code B2} defined in {@code DGAM1}. */ private static final double INV_GAMMA1P_M1_B2 = .266205348428949217746E-01; /** The constant {@code B3} defined in {@code DGAM1}. */ private static final double INV_GAMMA1P_M1_B3 = .493944979382446875238E-03; /** The constant {@code B4} defined in {@code DGAM1}. */ private static final double INV_GAMMA1P_M1_B4 = -.851419432440314906588E-05; /** The constant {@code B5} defined in {@code DGAM1}. */ private static final double INV_GAMMA1P_M1_B5 = -.643045481779353022248E-05; /** The constant {@code B6} defined in {@code DGAM1}. */ private static final double INV_GAMMA1P_M1_B6 = .992641840672773722196E-06; /** The constant {@code B7} defined in {@code DGAM1}. */ private static final double INV_GAMMA1P_M1_B7 = -.607761895722825260739E-07; /** The constant {@code B8} defined in {@code DGAM1}. */ private static final double INV_GAMMA1P_M1_B8 = .195755836614639731882E-09; /** The constant {@code P0} defined in {@code DGAM1}. */ private static final double INV_GAMMA1P_M1_P0 = .6116095104481415817861E-08; /** The constant {@code P1} defined in {@code DGAM1}. */ private static final double INV_GAMMA1P_M1_P1 = .6871674113067198736152E-08; /** The constant {@code P2} defined in {@code DGAM1}. */ private static final double INV_GAMMA1P_M1_P2 = .6820161668496170657918E-09; /** The constant {@code P3} defined in {@code DGAM1}. */ private static final double INV_GAMMA1P_M1_P3 = .4686843322948848031080E-10; /** The constant {@code P4} defined in {@code DGAM1}. */ private static final double INV_GAMMA1P_M1_P4 = .1572833027710446286995E-11; /** The constant {@code P5} defined in {@code DGAM1}. */ private static final double INV_GAMMA1P_M1_P5 = -.1249441572276366213222E-12; /** The constant {@code P6} defined in {@code DGAM1}. */ private static final double INV_GAMMA1P_M1_P6 = .4343529937408594255178E-14; /** The constant {@code Q1} defined in {@code DGAM1}. */ private static final double INV_GAMMA1P_M1_Q1 = .3056961078365221025009E+00; /** The constant {@code Q2} defined in {@code DGAM1}. */ private static final double INV_GAMMA1P_M1_Q2 = .5464213086042296536016E-01; /** The constant {@code Q3} defined in {@code DGAM1}. */ private static final double INV_GAMMA1P_M1_Q3 = .4956830093825887312020E-02; /** The constant {@code Q4} defined in {@code DGAM1}. */ private static final double INV_GAMMA1P_M1_Q4 = .2692369466186361192876E-03; /** The constant {@code C} defined in {@code DGAM1}. */ private static final double INV_GAMMA1P_M1_C = -.422784335098467139393487909917598E+00; /** The constant {@code C0} defined in {@code DGAM1}. */ private static final double INV_GAMMA1P_M1_C0 = .577215664901532860606512090082402E+00; /** The constant {@code C1} defined in {@code DGAM1}. */ private static final double INV_GAMMA1P_M1_C1 = -.655878071520253881077019515145390E+00; /** The constant {@code C2} defined in {@code DGAM1}. */ private static final double INV_GAMMA1P_M1_C2 = -.420026350340952355290039348754298E-01; /** The constant {@code C3} defined in {@code DGAM1}. */ private static final double INV_GAMMA1P_M1_C3 = .166538611382291489501700795102105E+00; /** The constant {@code C4} defined in {@code DGAM1}. */ private static final double INV_GAMMA1P_M1_C4 = -.421977345555443367482083012891874E-01; /** The constant {@code C5} defined in {@code DGAM1}. */ private static final double INV_GAMMA1P_M1_C5 = -.962197152787697356211492167234820E-02; /** The constant {@code C6} defined in {@code DGAM1}. */ private static final double INV_GAMMA1P_M1_C6 = .721894324666309954239501034044657E-02; /** The constant {@code C7} defined in {@code DGAM1}. */ private static final double INV_GAMMA1P_M1_C7 = -.116516759185906511211397108401839E-02; /** The constant {@code C8} defined in {@code DGAM1}. */ private static final double INV_GAMMA1P_M1_C8 = -.215241674114950972815729963053648E-03; /** The constant {@code C9} defined in {@code DGAM1}. */ private static final double INV_GAMMA1P_M1_C9 = .128050282388116186153198626328164E-03; /** The constant {@code C10} defined in {@code DGAM1}. */ private static final double INV_GAMMA1P_M1_C10 = -.201348547807882386556893914210218E-04; /** The constant {@code C11} defined in {@code DGAM1}. */ private static final double INV_GAMMA1P_M1_C11 = -.125049348214267065734535947383309E-05; /** The constant {@code C12} defined in {@code DGAM1}. */ private static final double INV_GAMMA1P_M1_C12 = .113302723198169588237412962033074E-05; /** The constant {@code C13} defined in {@code DGAM1}. */ private static final double INV_GAMMA1P_M1_C13 = -.205633841697760710345015413002057E-06; /** * Default constructor. Prohibit instantiation. */ private Gamma() {} /** *

                              * Returns the value of log Γ(x) for x > 0. *

                              *

                              * For x ≤ 8, the implementation is based on the double precision * implementation in the NSWC Library of Mathematics Subroutines, * {@code DGAMLN}. For x > 8, the implementation is based on *

                              * * * @param x Argument. * @return the value of {@code log(Gamma(x))}, {@code Double.NaN} if * {@code x <= 0.0}. */ public static double logGamma(double x) { double ret; if (Double.isNaN(x) || (x <= 0.0)) { ret = Double.NaN; } else if (x < 0.5) { return logGamma1p(x) - FastMath.log(x); } else if (x <= 2.5) { return logGamma1p((x - 0.5) - 0.5); } else if (x <= 8.0) { final int n = (int) FastMath.floor(x - 1.5); double prod = 1.0; for (int i = 1; i <= n; i++) { prod *= x - i; } return logGamma1p(x - (n + 1)) + FastMath.log(prod); } else { double sum = lanczos(x); double tmp = x + LANCZOS_G + .5; ret = ((x + .5) * FastMath.log(tmp)) - tmp + HALF_LOG_2_PI + FastMath.log(sum / x); } return ret; } /** * Returns the regularized gamma function P(a, x). * * @param a Parameter. * @param x Value. * @return the regularized gamma function P(a, x). * @throws MaxCountExceededException if the algorithm fails to converge. */ public static double regularizedGammaP(double a, double x) { return regularizedGammaP(a, x, DEFAULT_EPSILON, Integer.MAX_VALUE); } /** * Returns the regularized gamma function P(a, x). * * The implementation of this method is based on: * * * @param a the a parameter. * @param x the value. * @param epsilon When the absolute value of the nth item in the * series is less than epsilon the approximation ceases to calculate * further elements in the series. * @param maxIterations Maximum number of "iterations" to complete. * @return the regularized gamma function P(a, x) * @throws MaxCountExceededException if the algorithm fails to converge. */ public static double regularizedGammaP(double a, double x, double epsilon, int maxIterations) { double ret; if (Double.isNaN(a) || Double.isNaN(x) || (a <= 0.0) || (x < 0.0)) { ret = Double.NaN; } else if (x == 0.0) { ret = 0.0; } else if (x >= a + 1) { // use regularizedGammaQ because it should converge faster in this // case. ret = 1.0 - regularizedGammaQ(a, x, epsilon, maxIterations); } else { // calculate series double n = 0.0; // current element index double an = 1.0 / a; // n-th element in the series double sum = an; // partial sum while (FastMath.abs(an/sum) > epsilon && n < maxIterations && sum < Double.POSITIVE_INFINITY) { // compute next element in the series n = n + 1.0; an = an * (x / (a + n)); // update partial sum sum = sum + an; } if (n >= maxIterations) { throw new MaxCountExceededException(maxIterations); } else if (Double.isInfinite(sum)) { ret = 1.0; } else { ret = FastMath.exp(-x + (a * FastMath.log(x)) - logGamma(a)) * sum; } } return ret; } /** * Returns the regularized gamma function Q(a, x) = 1 - P(a, x). * * @param a the a parameter. * @param x the value. * @return the regularized gamma function Q(a, x) * @throws MaxCountExceededException if the algorithm fails to converge. */ public static double regularizedGammaQ(double a, double x) { return regularizedGammaQ(a, x, DEFAULT_EPSILON, Integer.MAX_VALUE); } /** * Returns the regularized gamma function Q(a, x) = 1 - P(a, x). * * The implementation of this method is based on: * * * @param a the a parameter. * @param x the value. * @param epsilon When the absolute value of the nth item in the * series is less than epsilon the approximation ceases to calculate * further elements in the series. * @param maxIterations Maximum number of "iterations" to complete. * @return the regularized gamma function P(a, x) * @throws MaxCountExceededException if the algorithm fails to converge. */ public static double regularizedGammaQ(final double a, double x, double epsilon, int maxIterations) { double ret; if (Double.isNaN(a) || Double.isNaN(x) || (a <= 0.0) || (x < 0.0)) { ret = Double.NaN; } else if (x == 0.0) { ret = 1.0; } else if (x < a + 1.0) { // use regularizedGammaP because it should converge faster in this // case. ret = 1.0 - regularizedGammaP(a, x, epsilon, maxIterations); } else { // create continued fraction ContinuedFraction cf = new ContinuedFraction() { @Override protected double getA(int n, double x) { return ((2.0 * n) + 1.0) - a + x; } @Override protected double getB(int n, double x) { return n * (a - n); } }; ret = 1.0 / cf.evaluate(x, epsilon, maxIterations); ret = FastMath.exp(-x + (a * FastMath.log(x)) - logGamma(a)) * ret; } return ret; } /** *

                              Computes the digamma function of x.

                              * *

                              This is an independently written implementation of the algorithm described in * Jose Bernardo, Algorithm AS 103: Psi (Digamma) Function, Applied Statistics, 1976.

                              * *

                              Some of the constants have been changed to increase accuracy at the moderate expense * of run-time. The result should be accurate to within 10^-8 absolute tolerance for * x >= 10^-5 and within 10^-8 relative tolerance for x > 0.

                              * *

                              Performance for large negative values of x will be quite expensive (proportional to * |x|). Accuracy for negative values of x should be about 10^-8 absolute for results * less than 10^5 and 10^-8 relative for results larger than that.

                              * * @param x Argument. * @return digamma(x) to within 10-8 relative or absolute error whichever is smaller. * @see Digamma * @see Bernardo's original article * @since 2.0 */ public static double digamma(double x) { if (x > 0 && x <= S_LIMIT) { // use method 5 from Bernardo AS103 // accurate to O(x) return -GAMMA - 1 / x; } if (x >= C_LIMIT) { // use method 4 (accurate to O(1/x^8) double inv = 1 / (x * x); // 1 1 1 1 // log(x) - --- - ------ + ------- - ------- // 2 x 12 x^2 120 x^4 252 x^6 return FastMath.log(x) - 0.5 / x - inv * ((1.0 / 12) + inv * (1.0 / 120 - inv / 252)); } return digamma(x + 1) - 1 / x; } /** * Computes the trigamma function of x. * This function is derived by taking the derivative of the implementation * of digamma. * * @param x Argument. * @return trigamma(x) to within 10-8 relative or absolute error whichever is smaller * @see Trigamma * @see Gamma#digamma(double) * @since 2.0 */ public static double trigamma(double x) { if (x > 0 && x <= S_LIMIT) { return 1 / (x * x); } if (x >= C_LIMIT) { double inv = 1 / (x * x); // 1 1 1 1 1 // - + ---- + ---- - ----- + ----- // x 2 3 5 7 // 2 x 6 x 30 x 42 x return 1 / x + inv / 2 + inv / x * (1.0 / 6 - inv * (1.0 / 30 + inv / 42)); } return trigamma(x + 1) + 1 / (x * x); } /** *

                              * Returns the Lanczos approximation used to compute the gamma function. * The Lanczos approximation is related to the Gamma function by the * following equation *

                              * {@code gamma(x) = sqrt(2 * pi) / x * (x + g + 0.5) ^ (x + 0.5) * * exp(-x - g - 0.5) * lanczos(x)}, *
                              * where {@code g} is the Lanczos constant. *

                              * * @param x Argument. * @return The Lanczos approximation. * @see Lanczos Approximation * equations (1) through (5), and Paul Godfrey's * Note on the computation * of the convergent Lanczos complex Gamma approximation * @since 3.1 */ public static double lanczos(final double x) { double sum = 0.0; for (int i = LANCZOS.length - 1; i > 0; --i) { sum = sum + (LANCZOS[i] / (x + i)); } return sum + LANCZOS[0]; } /** * Returns the value of 1 / Γ(1 + x) - 1 for -0.5 ≤ x ≤ * 1.5. This implementation is based on the double precision * implementation in the NSWC Library of Mathematics Subroutines, * {@code DGAM1}. * * @param x Argument. * @return The value of {@code 1.0 / Gamma(1.0 + x) - 1.0}. * @throws NumberIsTooSmallException if {@code x < -0.5} * @throws NumberIsTooLargeException if {@code x > 1.5} * @since 3.1 */ public static double invGamma1pm1(final double x) { if (x < -0.5) { throw new NumberIsTooSmallException(x, -0.5, true); } if (x > 1.5) { throw new NumberIsTooLargeException(x, 1.5, true); } final double ret; final double t = x <= 0.5 ? x : (x - 0.5) - 0.5; if (t < 0.0) { final double a = INV_GAMMA1P_M1_A0 + t * INV_GAMMA1P_M1_A1; double b = INV_GAMMA1P_M1_B8; b = INV_GAMMA1P_M1_B7 + t * b; b = INV_GAMMA1P_M1_B6 + t * b; b = INV_GAMMA1P_M1_B5 + t * b; b = INV_GAMMA1P_M1_B4 + t * b; b = INV_GAMMA1P_M1_B3 + t * b; b = INV_GAMMA1P_M1_B2 + t * b; b = INV_GAMMA1P_M1_B1 + t * b; b = 1.0 + t * b; double c = INV_GAMMA1P_M1_C13 + t * (a / b); c = INV_GAMMA1P_M1_C12 + t * c; c = INV_GAMMA1P_M1_C11 + t * c; c = INV_GAMMA1P_M1_C10 + t * c; c = INV_GAMMA1P_M1_C9 + t * c; c = INV_GAMMA1P_M1_C8 + t * c; c = INV_GAMMA1P_M1_C7 + t * c; c = INV_GAMMA1P_M1_C6 + t * c; c = INV_GAMMA1P_M1_C5 + t * c; c = INV_GAMMA1P_M1_C4 + t * c; c = INV_GAMMA1P_M1_C3 + t * c; c = INV_GAMMA1P_M1_C2 + t * c; c = INV_GAMMA1P_M1_C1 + t * c; c = INV_GAMMA1P_M1_C + t * c; if (x > 0.5) { ret = t * c / x; } else { ret = x * ((c + 0.5) + 0.5); } } else { double p = INV_GAMMA1P_M1_P6; p = INV_GAMMA1P_M1_P5 + t * p; p = INV_GAMMA1P_M1_P4 + t * p; p = INV_GAMMA1P_M1_P3 + t * p; p = INV_GAMMA1P_M1_P2 + t * p; p = INV_GAMMA1P_M1_P1 + t * p; p = INV_GAMMA1P_M1_P0 + t * p; double q = INV_GAMMA1P_M1_Q4; q = INV_GAMMA1P_M1_Q3 + t * q; q = INV_GAMMA1P_M1_Q2 + t * q; q = INV_GAMMA1P_M1_Q1 + t * q; q = 1.0 + t * q; double c = INV_GAMMA1P_M1_C13 + (p / q) * t; c = INV_GAMMA1P_M1_C12 + t * c; c = INV_GAMMA1P_M1_C11 + t * c; c = INV_GAMMA1P_M1_C10 + t * c; c = INV_GAMMA1P_M1_C9 + t * c; c = INV_GAMMA1P_M1_C8 + t * c; c = INV_GAMMA1P_M1_C7 + t * c; c = INV_GAMMA1P_M1_C6 + t * c; c = INV_GAMMA1P_M1_C5 + t * c; c = INV_GAMMA1P_M1_C4 + t * c; c = INV_GAMMA1P_M1_C3 + t * c; c = INV_GAMMA1P_M1_C2 + t * c; c = INV_GAMMA1P_M1_C1 + t * c; c = INV_GAMMA1P_M1_C0 + t * c; if (x > 0.5) { ret = (t / x) * ((c - 0.5) - 0.5); } else { ret = x * c; } } return ret; } /** * Returns the value of log Γ(1 + x) for -0.5 ≤ x ≤ 1.5. * This implementation is based on the double precision implementation in * the NSWC Library of Mathematics Subroutines, {@code DGMLN1}. * * @param x Argument. * @return The value of {@code log(Gamma(1 + x))}. * @throws NumberIsTooSmallException if {@code x < -0.5}. * @throws NumberIsTooLargeException if {@code x > 1.5}. * @since 3.1 */ public static double logGamma1p(final double x) throws NumberIsTooSmallException, NumberIsTooLargeException { if (x < -0.5) { throw new NumberIsTooSmallException(x, -0.5, true); } if (x > 1.5) { throw new NumberIsTooLargeException(x, 1.5, true); } return -FastMath.log1p(invGamma1pm1(x)); } /** * Returns the value of Γ(x). Based on the NSWC Library of * Mathematics Subroutines double precision implementation, * {@code DGAMMA}. * * @param x Argument. * @return the value of {@code Gamma(x)}. * @since 3.1 */ public static double gamma(final double x) { if ((x == FastMath.rint(x)) && (x <= 0.0)) { return Double.NaN; } final double ret; final double absX = FastMath.abs(x); if (absX <= 20.0) { if (x >= 1.0) { /* * From the recurrence relation * Gamma(x) = (x - 1) * ... * (x - n) * Gamma(x - n), * then * Gamma(t) = 1 / [1 + invGamma1pm1(t - 1)], * where t = x - n. This means that t must satisfy * -0.5 <= t - 1 <= 1.5. */ double prod = 1.0; double t = x; while (t > 2.5) { t = t - 1.0; prod *= t; } ret = prod / (1.0 + invGamma1pm1(t - 1.0)); } else { /* * From the recurrence relation * Gamma(x) = Gamma(x + n + 1) / [x * (x + 1) * ... * (x + n)] * then * Gamma(x + n + 1) = 1 / [1 + invGamma1pm1(x + n)], * which requires -0.5 <= x + n <= 1.5. */ double prod = x; double t = x; while (t < -0.5) { t = t + 1.0; prod *= t; } ret = 1.0 / (prod * (1.0 + invGamma1pm1(t))); } } else { final double y = absX + LANCZOS_G + 0.5; final double gammaAbs = SQRT_TWO_PI / x * FastMath.pow(y, absX + 0.5) * FastMath.exp(-y) * lanczos(absX); if (x > 0.0) { ret = gammaAbs; } else { /* * From the reflection formula * Gamma(x) * Gamma(1 - x) * sin(pi * x) = pi, * and the recurrence relation * Gamma(1 - x) = -x * Gamma(-x), * it is found * Gamma(x) = -pi / [x * sin(pi * x) * Gamma(-x)]. */ ret = -FastMath.PI / (x * FastMath.sin(FastMath.PI * x) * gammaAbs); } } return ret; } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/special/Erf.java100644 1750 1750 23255 12126627700 25545 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.special; import org.apache.commons.math3.util.FastMath; /** * This is a utility class that provides computation methods related to the * error functions. * * @version $Id: Erf.java 1456905 2013-03-15 11:37:35Z luc $ */ public class Erf { /** * The number {@code X_CRIT} is used by {@link #erf(double, double)} internally. * This number solves {@code erf(x)=0.5} within 1ulp. * More precisely, the current implementations of * {@link #erf(double)} and {@link #erfc(double)} satisfy:
                              * {@code erf(X_CRIT) < 0.5},
                              * {@code erf(Math.nextUp(X_CRIT) > 0.5},
                              * {@code erfc(X_CRIT) = 0.5}, and
                              * {@code erfc(Math.nextUp(X_CRIT) < 0.5} */ private static final double X_CRIT = 0.4769362762044697; /** * Default constructor. Prohibit instantiation. */ private Erf() {} /** * Returns the error function. * *

                              erf(x) = 2/√π 0x e-t2dt

                              * *

                              This implementation computes erf(x) using the * {@link Gamma#regularizedGammaP(double, double, double, int) regularized gamma function}, * following Erf, equation (3)

                              * *

                              The value returned is always between -1 and 1 (inclusive). * If {@code abs(x) > 40}, then {@code erf(x)} is indistinguishable from * either 1 or -1 as a double, so the appropriate extreme value is returned. *

                              * * @param x the value. * @return the error function erf(x) * @throws org.apache.commons.math3.exception.MaxCountExceededException * if the algorithm fails to converge. * @see Gamma#regularizedGammaP(double, double, double, int) */ public static double erf(double x) { if (FastMath.abs(x) > 40) { return x > 0 ? 1 : -1; } final double ret = Gamma.regularizedGammaP(0.5, x * x, 1.0e-15, 10000); return x < 0 ? -ret : ret; } /** * Returns the complementary error function. * *

                              erfc(x) = 2/√π x e-t2dt *
                              * = 1 - {@link #erf(double) erf(x)}

                              * *

                              This implementation computes erfc(x) using the * {@link Gamma#regularizedGammaQ(double, double, double, int) regularized gamma function}, * following Erf, equation (3).

                              * *

                              The value returned is always between 0 and 2 (inclusive). * If {@code abs(x) > 40}, then {@code erf(x)} is indistinguishable from * either 0 or 2 as a double, so the appropriate extreme value is returned. *

                              * * @param x the value * @return the complementary error function erfc(x) * @throws org.apache.commons.math3.exception.MaxCountExceededException * if the algorithm fails to converge. * @see Gamma#regularizedGammaQ(double, double, double, int) * @since 2.2 */ public static double erfc(double x) { if (FastMath.abs(x) > 40) { return x > 0 ? 0 : 2; } final double ret = Gamma.regularizedGammaQ(0.5, x * x, 1.0e-15, 10000); return x < 0 ? 2 - ret : ret; } /** * Returns the difference between erf(x1) and erf(x2). * * The implementation uses either erf(double) or erfc(double) * depending on which provides the most precise result. * * @param x1 the first value * @param x2 the second value * @return erf(x2) - erf(x1) */ public static double erf(double x1, double x2) { if(x1 > x2) { return -erf(x2, x1); } return x1 < -X_CRIT ? x2 < 0.0 ? erfc(-x2) - erfc(-x1) : erf(x2) - erf(x1) : x2 > X_CRIT && x1 > 0.0 ? erfc(x1) - erfc(x2) : erf(x2) - erf(x1); } /** * Returns the inverse erf. *

                              * This implementation is described in the paper: * Approximating * the erfinv function by Mike Giles, Oxford-Man Institute of Quantitative Finance, * which was published in GPU Computing Gems, volume 2, 2010. * The source code is available here. *

                              * @param x the value * @return t such that x = erf(t) * @since 3.2 */ public static double erfInv(final double x) { // beware that the logarithm argument must be // commputed as (1.0 - x) * (1.0 + x), // it must NOT be simplified as 1.0 - x * x as this // would induce rounding errors near the boundaries +/-1 double w = - FastMath.log((1.0 - x) * (1.0 + x)); double p; if (w < 6.25) { w = w - 3.125; p = -3.6444120640178196996e-21; p = -1.685059138182016589e-19 + p * w; p = 1.2858480715256400167e-18 + p * w; p = 1.115787767802518096e-17 + p * w; p = -1.333171662854620906e-16 + p * w; p = 2.0972767875968561637e-17 + p * w; p = 6.6376381343583238325e-15 + p * w; p = -4.0545662729752068639e-14 + p * w; p = -8.1519341976054721522e-14 + p * w; p = 2.6335093153082322977e-12 + p * w; p = -1.2975133253453532498e-11 + p * w; p = -5.4154120542946279317e-11 + p * w; p = 1.051212273321532285e-09 + p * w; p = -4.1126339803469836976e-09 + p * w; p = -2.9070369957882005086e-08 + p * w; p = 4.2347877827932403518e-07 + p * w; p = -1.3654692000834678645e-06 + p * w; p = -1.3882523362786468719e-05 + p * w; p = 0.0001867342080340571352 + p * w; p = -0.00074070253416626697512 + p * w; p = -0.0060336708714301490533 + p * w; p = 0.24015818242558961693 + p * w; p = 1.6536545626831027356 + p * w; } else if (w < 16.0) { w = FastMath.sqrt(w) - 3.25; p = 2.2137376921775787049e-09; p = 9.0756561938885390979e-08 + p * w; p = -2.7517406297064545428e-07 + p * w; p = 1.8239629214389227755e-08 + p * w; p = 1.5027403968909827627e-06 + p * w; p = -4.013867526981545969e-06 + p * w; p = 2.9234449089955446044e-06 + p * w; p = 1.2475304481671778723e-05 + p * w; p = -4.7318229009055733981e-05 + p * w; p = 6.8284851459573175448e-05 + p * w; p = 2.4031110387097893999e-05 + p * w; p = -0.0003550375203628474796 + p * w; p = 0.00095328937973738049703 + p * w; p = -0.0016882755560235047313 + p * w; p = 0.0024914420961078508066 + p * w; p = -0.0037512085075692412107 + p * w; p = 0.005370914553590063617 + p * w; p = 1.0052589676941592334 + p * w; p = 3.0838856104922207635 + p * w; } else if (!Double.isInfinite(w)) { w = FastMath.sqrt(w) - 5.0; p = -2.7109920616438573243e-11; p = -2.5556418169965252055e-10 + p * w; p = 1.5076572693500548083e-09 + p * w; p = -3.7894654401267369937e-09 + p * w; p = 7.6157012080783393804e-09 + p * w; p = -1.4960026627149240478e-08 + p * w; p = 2.9147953450901080826e-08 + p * w; p = -6.7711997758452339498e-08 + p * w; p = 2.2900482228026654717e-07 + p * w; p = -9.9298272942317002539e-07 + p * w; p = 4.5260625972231537039e-06 + p * w; p = -1.9681778105531670567e-05 + p * w; p = 7.5995277030017761139e-05 + p * w; p = -0.00021503011930044477347 + p * w; p = -0.00013871931833623122026 + p * w; p = 1.0103004648645343977 + p * w; p = 4.8499064014085844221 + p * w; } else { // this branch does not appears in the original code, it // was added because the previous branch does not handle // x = +/-1 correctly. In this case, w is positive infinity // and as the first coefficient (-2.71e-11) is negative. // Once the first multiplication is done, p becomes negative // infinity and remains so throughout the polynomial evaluation. // So the branch above incorrectly returns negative infinity // instead of the correct positive infinity. p = Double.POSITIVE_INFINITY; } return p * x; } /** * Returns the inverse erfc. * @param x the value * @return t such that x = erfc(t) * @since 3.2 */ public static double erfcInv(final double x) { return erfInv(1 - x); } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/special/Beta.java100644 1750 1750 45565 12126627700 25714 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.special; import org.apache.commons.math3.exception.NumberIsTooSmallException; import org.apache.commons.math3.exception.OutOfRangeException; import org.apache.commons.math3.util.ContinuedFraction; import org.apache.commons.math3.util.FastMath; /** *

                              * This is a utility class that provides computation methods related to the * Beta family of functions. *

                              *

                              * Implementation of {@link #logBeta(double, double)} is based on the * algorithms described in *

                              * and implemented in the * NSWC Library of Mathematical Functions, * available * here. * This library is "approved for public release", and the * Copyright guidance * indicates that unless otherwise stated in the code, all FORTRAN functions in * this library are license free. Since no such notice appears in the code these * functions can safely be ported to Commons-Math. *

                              * * * @version $Id: Beta.java 1420669 2012-12-12 13:40:35Z erans $ */ public class Beta { /** Maximum allowed numerical error. */ private static final double DEFAULT_EPSILON = 1E-14; /** The constant value of ½log 2π. */ private static final double HALF_LOG_TWO_PI = .9189385332046727; /** *

                              * The coefficients of the series expansion of the Δ function. This function * is defined as follows *

                              *
                              Δ(x) = log Γ(x) - (x - 0.5) log a + a - 0.5 log 2π,
                              *

                              * see equation (23) in Didonato and Morris (1992). The series expansion, * which applies for x ≥ 10, reads *

                              *
                                   *                 14
                                   *                ====
                                   *             1  \                2 n
                                   *     Δ(x) = ---  >    d  (10 / x)
                                   *             x  /      n
                                   *                ====
                                   *                n = 0
                                   * 
                                   */
                                  private static final double[] DELTA = {
                                      .833333333333333333333333333333E-01,
                                      -.277777777777777777777777752282E-04,
                                      .793650793650793650791732130419E-07,
                                      -.595238095238095232389839236182E-09,
                                      .841750841750832853294451671990E-11,
                                      -.191752691751854612334149171243E-12,
                                      .641025640510325475730918472625E-14,
                                      -.295506514125338232839867823991E-15,
                                      .179643716359402238723287696452E-16,
                                      -.139228964661627791231203060395E-17,
                                      .133802855014020915603275339093E-18,
                                      -.154246009867966094273710216533E-19,
                                      .197701992980957427278370133333E-20,
                                      -.234065664793997056856992426667E-21,
                                      .171348014966398575409015466667E-22
                                  };
                              
                                  /**
                                   * Default constructor.  Prohibit instantiation.
                                   */
                                  private Beta() {}
                              
                                  /**
                                   * Returns the
                                   * 
                                   * regularized beta function I(x, a, b).
                                   *
                                   * @param x Value.
                                   * @param a Parameter {@code a}.
                                   * @param b Parameter {@code b}.
                                   * @return the regularized beta function I(x, a, b).
                                   * @throws org.apache.commons.math3.exception.MaxCountExceededException
                                   * if the algorithm fails to converge.
                                   */
                                  public static double regularizedBeta(double x, double a, double b) {
                                      return regularizedBeta(x, a, b, DEFAULT_EPSILON, Integer.MAX_VALUE);
                                  }
                              
                                  /**
                                   * Returns the
                                   * 
                                   * regularized beta function I(x, a, b).
                                   *
                                   * @param x Value.
                                   * @param a Parameter {@code a}.
                                   * @param b Parameter {@code b}.
                                   * @param epsilon When the absolute value of the nth item in the
                                   * series is less than epsilon the approximation ceases to calculate
                                   * further elements in the series.
                                   * @return the regularized beta function I(x, a, b)
                                   * @throws org.apache.commons.math3.exception.MaxCountExceededException
                                   * if the algorithm fails to converge.
                                   */
                                  public static double regularizedBeta(double x,
                                                                       double a, double b,
                                                                       double epsilon) {
                                      return regularizedBeta(x, a, b, epsilon, Integer.MAX_VALUE);
                                  }
                              
                                  /**
                                   * Returns the regularized beta function I(x, a, b).
                                   *
                                   * @param x the value.
                                   * @param a Parameter {@code a}.
                                   * @param b Parameter {@code b}.
                                   * @param maxIterations Maximum number of "iterations" to complete.
                                   * @return the regularized beta function I(x, a, b)
                                   * @throws org.apache.commons.math3.exception.MaxCountExceededException
                                   * if the algorithm fails to converge.
                                   */
                                  public static double regularizedBeta(double x,
                                                                       double a, double b,
                                                                       int maxIterations) {
                                      return regularizedBeta(x, a, b, DEFAULT_EPSILON, maxIterations);
                                  }
                              
                                  /**
                                   * Returns the regularized beta function I(x, a, b).
                                   *
                                   * The implementation of this method is based on:
                                   * 
                                   *
                                   * @param x the value.
                                   * @param a Parameter {@code a}.
                                   * @param b Parameter {@code b}.
                                   * @param epsilon When the absolute value of the nth item in the
                                   * series is less than epsilon the approximation ceases to calculate
                                   * further elements in the series.
                                   * @param maxIterations Maximum number of "iterations" to complete.
                                   * @return the regularized beta function I(x, a, b)
                                   * @throws org.apache.commons.math3.exception.MaxCountExceededException
                                   * if the algorithm fails to converge.
                                   */
                                  public static double regularizedBeta(double x,
                                                                       final double a, final double b,
                                                                       double epsilon, int maxIterations) {
                                      double ret;
                              
                                      if (Double.isNaN(x) ||
                                          Double.isNaN(a) ||
                                          Double.isNaN(b) ||
                                          x < 0 ||
                                          x > 1 ||
                                          a <= 0.0 ||
                                          b <= 0.0) {
                                          ret = Double.NaN;
                                      } else if (x > (a + 1.0) / (a + b + 2.0)) {
                                          ret = 1.0 - regularizedBeta(1.0 - x, b, a, epsilon, maxIterations);
                                      } else {
                                          ContinuedFraction fraction = new ContinuedFraction() {
                              
                                              @Override
                                              protected double getB(int n, double x) {
                                                  double ret;
                                                  double m;
                                                  if (n % 2 == 0) { // even
                                                      m = n / 2.0;
                                                      ret = (m * (b - m) * x) /
                                                          ((a + (2 * m) - 1) * (a + (2 * m)));
                                                  } else {
                                                      m = (n - 1.0) / 2.0;
                                                      ret = -((a + m) * (a + b + m) * x) /
                                                              ((a + (2 * m)) * (a + (2 * m) + 1.0));
                                                  }
                                                  return ret;
                                              }
                              
                                              @Override
                                              protected double getA(int n, double x) {
                                                  return 1.0;
                                              }
                                          };
                                          ret = FastMath.exp((a * FastMath.log(x)) + (b * FastMath.log(1.0 - x)) -
                                              FastMath.log(a) - logBeta(a, b)) *
                                              1.0 / fraction.evaluate(x, epsilon, maxIterations);
                                      }
                              
                                      return ret;
                                  }
                              
                                  /**
                                   * Returns the natural logarithm of the beta function B(a, b).
                                   *
                                   * The implementation of this method is based on:
                                   * 
                                   *
                                   * @param a Parameter {@code a}.
                                   * @param b Parameter {@code b}.
                                   * @param epsilon This parameter is ignored.
                                   * @param maxIterations This parameter is ignored.
                                   * @return log(B(a, b)).
                                   * @deprecated as of version 3.1, this method is deprecated as the
                                   * computation of the beta function is no longer iterative; it will be
                                   * removed in version 4.0. Current implementation of this method
                                   * internally calls {@link #logBeta(double, double)}.
                                   */
                                  @Deprecated
                                  public static double logBeta(double a, double b,
                                                               double epsilon,
                                                               int maxIterations) {
                              
                                      return logBeta(a, b);
                                  }
                              
                              
                                  /**
                                   * Returns the value of log Γ(a + b) for 1 ≤ a, b ≤ 2. Based on the
                                   * NSWC Library of Mathematics Subroutines double precision
                                   * implementation, {@code DGSMLN}. In {@link BetaTest#testLogGammaSum()},
                                   * this private method is accessed through reflection.
                                   *
                                   * @param a First argument.
                                   * @param b Second argument.
                                   * @return the value of {@code log(Gamma(a + b))}.
                                   * @throws OutOfRangeException if {@code a} or {@code b} is lower than
                                   * {@code 1.0} or greater than {@code 2.0}.
                                   */
                                  private static double logGammaSum(final double a, final double b)
                                      throws OutOfRangeException {
                              
                                      if ((a < 1.0) || (a > 2.0)) {
                                          throw new OutOfRangeException(a, 1.0, 2.0);
                                      }
                                      if ((b < 1.0) || (b > 2.0)) {
                                          throw new OutOfRangeException(b, 1.0, 2.0);
                                      }
                              
                                      final double x = (a - 1.0) + (b - 1.0);
                                      if (x <= 0.5) {
                                          return Gamma.logGamma1p(1.0 + x);
                                      } else if (x <= 1.5) {
                                          return Gamma.logGamma1p(x) + FastMath.log1p(x);
                                      } else {
                                          return Gamma.logGamma1p(x - 1.0) + FastMath.log(x * (1.0 + x));
                                      }
                                  }
                              
                                  /**
                                   * Returns the value of log[Γ(b) / Γ(a + b)] for a ≥ 0 and b ≥ 10. Based on
                                   * the NSWC Library of Mathematics Subroutines double precision
                                   * implementation, {@code DLGDIV}. In
                                   * {@link BetaTest#testLogGammaMinusLogGammaSum()}, this private method is
                                   * accessed through reflection.
                                   *
                                   * @param a First argument.
                                   * @param b Second argument.
                                   * @return the value of {@code log(Gamma(b) / Gamma(a + b))}.
                                   * @throws NumberIsTooSmallException if {@code a < 0.0} or {@code b < 10.0}.
                                   */
                                  private static double logGammaMinusLogGammaSum(final double a,
                                                                                 final double b)
                                      throws NumberIsTooSmallException {
                              
                                      if (a < 0.0) {
                                          throw new NumberIsTooSmallException(a, 0.0, true);
                                      }
                                      if (b < 10.0) {
                                          throw new NumberIsTooSmallException(b, 10.0, true);
                                      }
                              
                                      /*
                                       * d = a + b - 0.5
                                       */
                                      final double d;
                                      final double w;
                                      if (a <= b) {
                                          d = b + (a - 0.5);
                                          w = deltaMinusDeltaSum(a, b);
                                      } else {
                                          d = a + (b - 0.5);
                                          w = deltaMinusDeltaSum(b, a);
                                      }
                              
                                      final double u = d * FastMath.log1p(a / b);
                                      final double v = a * (FastMath.log(b) - 1.0);
                              
                                      return u <= v ? (w - u) - v : (w - v) - u;
                                  }
                              
                                  /**
                                   * Returns the value of Δ(b) - Δ(a + b), with 0 ≤ a ≤ b and b ≥ 10. Based
                                   * on equations (26), (27) and (28) in Didonato and Morris (1992).
                                   *
                                   * @param a First argument.
                                   * @param b Second argument.
                                   * @return the value of {@code Delta(b) - Delta(a + b)}
                                   * @throws OutOfRangeException if {@code a < 0} or {@code a > b}
                                   * @throws NumberIsTooSmallException if {@code b < 10}
                                   */
                                  private static double deltaMinusDeltaSum(final double a,
                                                                           final double b)
                                      throws OutOfRangeException, NumberIsTooSmallException {
                              
                                      if ((a < 0) || (a > b)) {
                                          throw new OutOfRangeException(a, 0, b);
                                      }
                                      if (b < 10) {
                                          throw new NumberIsTooSmallException(b, 10, true);
                                      }
                              
                                      final double h = a / b;
                                      final double p = h / (1.0 + h);
                                      final double q = 1.0 / (1.0 + h);
                                      final double q2 = q * q;
                                      /*
                                       * s[i] = 1 + q + ... - q**(2 * i)
                                       */
                                      final double[] s = new double[DELTA.length];
                                      s[0] = 1.0;
                                      for (int i = 1; i < s.length; i++) {
                                          s[i] = 1.0 + (q + q2 * s[i - 1]);
                                      }
                                      /*
                                       * w = Delta(b) - Delta(a + b)
                                       */
                                      final double sqrtT = 10.0 / b;
                                      final double t = sqrtT * sqrtT;
                                      double w = DELTA[DELTA.length - 1] * s[s.length - 1];
                                      for (int i = DELTA.length - 2; i >= 0; i--) {
                                          w = t * w + DELTA[i] * s[i];
                                      }
                                      return w * p / b;
                                  }
                              
                                  /**
                                   * Returns the value of Δ(p) + Δ(q) - Δ(p + q), with p, q ≥ 10. Based on
                                   * the NSWC Library of Mathematics Subroutines double precision
                                   * implementation, {@code DBCORR}. In
                                   * {@link BetaTest#testSumDeltaMinusDeltaSum()}, this private method is
                                   * accessed through reflection.
                                   *
                                   * @param p First argument.
                                   * @param q Second argument.
                                   * @return the value of {@code Delta(p) + Delta(q) - Delta(p + q)}.
                                   * @throws NumberIsTooSmallException if {@code p < 10.0} or {@code q < 10.0}.
                                   */
                                  private static double sumDeltaMinusDeltaSum(final double p,
                                                                              final double q) {
                              
                                      if (p < 10.0) {
                                          throw new NumberIsTooSmallException(p, 10.0, true);
                                      }
                                      if (q < 10.0) {
                                          throw new NumberIsTooSmallException(q, 10.0, true);
                                      }
                              
                                      final double a = FastMath.min(p, q);
                                      final double b = FastMath.max(p, q);
                                      final double sqrtT = 10.0 / a;
                                      final double t = sqrtT * sqrtT;
                                      double z = DELTA[DELTA.length - 1];
                                      for (int i = DELTA.length - 2; i >= 0; i--) {
                                          z = t * z + DELTA[i];
                                      }
                                      return z / a + deltaMinusDeltaSum(a, b);
                                  }
                              
                                  /**
                                   * Returns the value of log B(p, q) for 0 ≤ x ≤ 1 and p, q > 0. Based on the
                                   * NSWC Library of Mathematics Subroutines implementation,
                                   * {@code DBETLN}.
                                   *
                                   * @param p First argument.
                                   * @param q Second argument.
                                   * @return the value of {@code log(Beta(p, q))}, {@code NaN} if
                                   * {@code p <= 0} or {@code q <= 0}.
                                   */
                                  public static double logBeta(final double p, final double q) {
                                      if (Double.isNaN(p) || Double.isNaN(q) || (p <= 0.0) || (q <= 0.0)) {
                                          return Double.NaN;
                                      }
                              
                                      final double a = FastMath.min(p, q);
                                      final double b = FastMath.max(p, q);
                                      if (a >= 10.0) {
                                          final double w = sumDeltaMinusDeltaSum(a, b);
                                          final double h = a / b;
                                          final double c = h / (1.0 + h);
                                          final double u = -(a - 0.5) * FastMath.log(c);
                                          final double v = b * FastMath.log1p(h);
                                          if (u <= v) {
                                              return (((-0.5 * FastMath.log(b) + HALF_LOG_TWO_PI) + w) - u) - v;
                                          } else {
                                              return (((-0.5 * FastMath.log(b) + HALF_LOG_TWO_PI) + w) - v) - u;
                                          }
                                      } else if (a > 2.0) {
                                          if (b > 1000.0) {
                                              final int n = (int) FastMath.floor(a - 1.0);
                                              double prod = 1.0;
                                              double ared = a;
                                              for (int i = 0; i < n; i++) {
                                                  ared -= 1.0;
                                                  prod *= ared / (1.0 + ared / b);
                                              }
                                              return (FastMath.log(prod) - n * FastMath.log(b)) +
                                                      (Gamma.logGamma(ared) +
                                                       logGammaMinusLogGammaSum(ared, b));
                                          } else {
                                              double prod1 = 1.0;
                                              double ared = a;
                                              while (ared > 2.0) {
                                                  ared -= 1.0;
                                                  final double h = ared / b;
                                                  prod1 *= h / (1.0 + h);
                                              }
                                              if (b < 10.0) {
                                                  double prod2 = 1.0;
                                                  double bred = b;
                                                  while (bred > 2.0) {
                                                      bred -= 1.0;
                                                      prod2 *= bred / (ared + bred);
                                                  }
                                                  return FastMath.log(prod1) +
                                                         FastMath.log(prod2) +
                                                         (Gamma.logGamma(ared) +
                                                         (Gamma.logGamma(bred) -
                                                          logGammaSum(ared, bred)));
                                              } else {
                                                  return FastMath.log(prod1) +
                                                         Gamma.logGamma(ared) +
                                                         logGammaMinusLogGammaSum(ared, b);
                                              }
                                          }
                                      } else if (a >= 1.0) {
                                          if (b > 2.0) {
                                              if (b < 10.0) {
                                                  double prod = 1.0;
                                                  double bred = b;
                                                  while (bred > 2.0) {
                                                      bred -= 1.0;
                                                      prod *= bred / (a + bred);
                                                  }
                                                  return FastMath.log(prod) +
                                                         (Gamma.logGamma(a) +
                                                          (Gamma.logGamma(bred) -
                                                           logGammaSum(a, bred)));
                                              } else {
                                                  return Gamma.logGamma(a) +
                                                         logGammaMinusLogGammaSum(a, b);
                                              }
                                          } else {
                                              return Gamma.logGamma(a) +
                                                     Gamma.logGamma(b) -
                                                     logGammaSum(a, b);
                                          }
                                      } else {
                                          if (b >= 10.0) {
                                              return Gamma.logGamma(a) +
                                                     logGammaMinusLogGammaSum(a, b);
                                          } else {
                                              // The following command is the original NSWC implementation.
                                              // return Gamma.logGamma(a) +
                                              // (Gamma.logGamma(b) - Gamma.logGamma(a + b));
                                              // The following command turns out to be more accurate.
                                              return FastMath.log(Gamma.gamma(a) * Gamma.gamma(b) /
                                                                  Gamma.gamma(a + b));
                                          }
                                      }
                                  }
                              }
                              commons-math3-3.2-src/src/main/java/org/apache/commons/math3/RealFieldElement.java100644   1750   1750       31006 12126627720  26545 0ustarlucluc     0      0 /*
                               * Licensed to the Apache Software Foundation (ASF) under one or more
                               * contributor license agreements.  See the NOTICE file distributed with
                               * this work for additional information regarding copyright ownership.
                               * The ASF licenses this file to You 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.
                               */
                              package org.apache.commons.math3;
                              
                              import org.apache.commons.math3.exception.DimensionMismatchException;
                              
                              /**
                               * Interface representing a real
                               * field.
                               * @param  the type of the field elements
                               * @see FieldElement
                               * @version $Id: RealFieldElement.java 1455053 2013-03-11 08:37:12Z luc $
                               * @since 3.2
                               */
                              public interface RealFieldElement extends FieldElement {
                              
                                  /** Get the real value of the number.
                                   * @return real value
                                   */
                                  double getReal();
                              
                                  /** '+' operator.
                                   * @param a right hand side parameter of the operator
                                   * @return this+a
                                   */
                                  T add(double a);
                              
                                  /** '-' operator.
                                   * @param a right hand side parameter of the operator
                                   * @return this-a
                                   */
                                  T subtract(double a);
                              
                                  /** '×' operator.
                                   * @param a right hand side parameter of the operator
                                   * @return this×a
                                   */
                                  T multiply(double a);
                              
                                  /** '÷s;' operator.
                                   * @param a right hand side parameter of the operator
                                   * @return this÷s;a
                                   */
                                  T divide(double a);
                              
                                  /** IEEE remainder operator.
                                   * @param a right hand side parameter of the operator
                                   * @return this - n × a where n is the closest integer to this/a
                                   * (the even integer is chosen for n if this/a is halfway between two integers)
                                   */
                                  T remainder(double a);
                              
                                  /** IEEE remainder operator.
                                   * @param a right hand side parameter of the operator
                                   * @return this - n × a where n is the closest integer to this/a
                                   * (the even integer is chosen for n if this/a is halfway between two integers)
                                   * @exception DimensionMismatchException if number of free parameters or orders are inconsistent
                                   */
                                  T remainder(T a)
                                      throws DimensionMismatchException;
                              
                                  /** absolute value.
                                   * @return abs(this)
                                   */
                                  T abs();
                              
                                  /** Get the smallest whole number larger than instance.
                                   * @return ceil(this)
                                   */
                                  T ceil();
                              
                                  /** Get the largest whole number smaller than instance.
                                   * @return floor(this)
                                   */
                                  T floor();
                              
                                  /** Get the whole number that is the nearest to the instance, or the even one if x is exactly half way between two integers.
                                   * @return a double number r such that r is an integer r - 0.5 <= this <= r + 0.5
                                   */
                                  T rint();
                              
                                  /** Get the closest long to instance value.
                                   * @return closest long to {@link #getReal()}
                                   */
                                  long round();
                              
                                  /** Compute the signum of the instance.
                                   * The signum is -1 for negative numbers, +1 for positive numbers and 0 otherwise
                                   * @return -1.0, -0.0, +0.0, +1.0 or NaN depending on sign of a
                                   */
                                  T signum();
                              
                                  /**
                                   * Returns the instance with the sign of the argument.
                                   * A NaN {@code sign} argument is treated as positive.
                                   *
                                   * @param sign the sign for the returned value
                                   * @return the instance with the same sign as the {@code sign} argument
                                   */
                                  T copySign(T sign);
                              
                                  /**
                                   * Returns the instance with the sign of the argument.
                                   * A NaN {@code sign} argument is treated as positive.
                                   *
                                   * @param sign the sign for the returned value
                                   * @return the instance with the same sign as the {@code sign} argument
                                   */
                                  T copySign(double sign);
                              
                                  /**
                                   * Multiply the instance by a power of 2.
                                   * @param n power of 2
                                   * @return this × 2n
                                   */
                                  T scalb(int n);
                              
                                  /**
                                   * Returns the hypotenuse of a triangle with sides {@code this} and {@code y}
                                   * - sqrt(this2 +y2)
                              * avoiding intermediate overflow or underflow. * *
                                *
                              • If either argument is infinite, then the result is positive infinity.
                              • *
                              • else, if either argument is NaN then the result is NaN.
                              • *
                              * * @param y a value * @return sqrt(this2 +y2) * @exception DimensionMismatchException if number of free parameters or orders are inconsistent */ T hypot(T y) throws DimensionMismatchException; /** {@inheritDoc} */ T reciprocal(); /** Square root. * @return square root of the instance */ T sqrt(); /** Cubic root. * @return cubic root of the instance */ T cbrt(); /** Nth root. * @param n order of the root * @return nth root of the instance */ T rootN(int n); /** Power operation. * @param p power to apply * @return thisp */ T pow(double p); /** Integer power operation. * @param n power to apply * @return thisn */ T pow(int n); /** Power operation. * @param e exponent * @return thise * @exception DimensionMismatchException if number of free parameters or orders are inconsistent */ T pow(T e) throws DimensionMismatchException; /** Exponential. * @return exponential of the instance */ T exp(); /** Exponential minus 1. * @return exponential minus one of the instance */ T expm1(); /** Natural logarithm. * @return logarithm of the instance */ T log(); /** Shifted natural logarithm. * @return logarithm of one plus the instance */ T log1p(); // TODO: add this method in 4.0, as it is not possible to do it in 3.2 // due to incompatibility of the return type in the Dfp class // /** Base 10 logarithm. // * @return base 10 logarithm of the instance // */ // T log10(); /** Cosine operation. * @return cos(this) */ T cos(); /** Sine operation. * @return sin(this) */ T sin(); /** Tangent operation. * @return tan(this) */ T tan(); /** Arc cosine operation. * @return acos(this) */ T acos(); /** Arc sine operation. * @return asin(this) */ T asin(); /** Arc tangent operation. * @return atan(this) */ T atan(); /** Two arguments arc tangent operation. * @param x second argument of the arc tangent * @return atan2(this, x) * @exception DimensionMismatchException if number of free parameters or orders are inconsistent */ T atan2(T x) throws DimensionMismatchException; /** Hyperbolic cosine operation. * @return cosh(this) */ T cosh(); /** Hyperbolic sine operation. * @return sinh(this) */ T sinh(); /** Hyperbolic tangent operation. * @return tanh(this) */ T tanh(); /** Inverse hyperbolic cosine operation. * @return acosh(this) */ T acosh(); /** Inverse hyperbolic sine operation. * @return asin(this) */ T asinh(); /** Inverse hyperbolic tangent operation. * @return atanh(this) */ T atanh(); /** * Compute a linear combination. * @param a Factors. * @param b Factors. * @return Σi ai bi. * @throws DimensionMismatchException if arrays dimensions don't match * @since 3.2 */ T linearCombination(T[] a, T[] b) throws DimensionMismatchException; /** * Compute a linear combination. * @param a Factors. * @param b Factors. * @return Σi ai bi. * @throws DimensionMismatchException if arrays dimensions don't match * @since 3.2 */ T linearCombination(double[] a, T[] b) throws DimensionMismatchException; /** * Compute a linear combination. * @param a1 first factor of the first term * @param b1 second factor of the first term * @param a2 first factor of the second term * @param b2 second factor of the second term * @return a1×b1 + * a2×b2 * @see #linearCombination(Object, Object, Object, Object, Object, Object) * @see #linearCombination(Object, Object, Object, Object, Object, Object, Object, Object) * @since 3.2 */ T linearCombination(T a1, T b1, T a2, T b2); /** * Compute a linear combination. * @param a1 first factor of the first term * @param b1 second factor of the first term * @param a2 first factor of the second term * @param b2 second factor of the second term * @return a1×b1 + * a2×b2 * @see #linearCombination(double, Object, double, Object, double, Object) * @see #linearCombination(double, Object, double, Object, double, Object, double, Object) * @since 3.2 */ T linearCombination(double a1, T b1, double a2, T b2); /** * Compute a linear combination. * @param a1 first factor of the first term * @param b1 second factor of the first term * @param a2 first factor of the second term * @param b2 second factor of the second term * @param a3 first factor of the third term * @param b3 second factor of the third term * @return a1×b1 + * a2×b2 + a3×b3 * @see #linearCombination(Object, Object, Object, Object) * @see #linearCombination(Object, Object, Object, Object, Object, Object, Object, Object) * @since 3.2 */ T linearCombination(T a1, T b1, T a2, T b2, T a3, T b3); /** * Compute a linear combination. * @param a1 first factor of the first term * @param b1 second factor of the first term * @param a2 first factor of the second term * @param b2 second factor of the second term * @param a3 first factor of the third term * @param b3 second factor of the third term * @return a1×b1 + * a2×b2 + a3×b3 * @see #linearCombination(double, Object, double, Object) * @see #linearCombination(double, Object, double, Object, double, Object, double, Object) * @since 3.2 */ T linearCombination(double a1, T b1, double a2, T b2, double a3, T b3); /** * Compute a linear combination. * @param a1 first factor of the first term * @param b1 second factor of the first term * @param a2 first factor of the second term * @param b2 second factor of the second term * @param a3 first factor of the third term * @param b3 second factor of the third term * @param a4 first factor of the third term * @param b4 second factor of the third term * @return a1×b1 + * a2×b2 + a3×b3 + * a4×b4 * @see #linearCombination(Object, Object, Object, Object) * @see #linearCombination(Object, Object, Object, Object, Object, Object) * @since 3.2 */ T linearCombination(T a1, T b1, T a2, T b2, T a3, T b3, T a4, T b4); /** * Compute a linear combination. * @param a1 first factor of the first term * @param b1 second factor of the first term * @param a2 first factor of the second term * @param b2 second factor of the second term * @param a3 first factor of the third term * @param b3 second factor of the third term * @param a4 first factor of the third term * @param b4 second factor of the third term * @return a1×b1 + * a2×b2 + a3×b3 + * a4×b4 * @see #linearCombination(double, Object, double, Object) * @see #linearCombination(double, Object, double, Object, double, Object) * @since 3.2 */ T linearCombination(double a1, T b1, double a2, T b2, double a3, T b3, double a4, T b4); } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/linear/OpenMapRealVector.java100644 1750 1750 64535 12126627717 30227 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.linear; import java.io.Serializable; import org.apache.commons.math3.exception.DimensionMismatchException; import org.apache.commons.math3.exception.MathArithmeticException; import org.apache.commons.math3.exception.NotPositiveException; import org.apache.commons.math3.exception.OutOfRangeException; import org.apache.commons.math3.exception.util.LocalizedFormats; import org.apache.commons.math3.util.FastMath; import org.apache.commons.math3.util.OpenIntToDoubleHashMap; import org.apache.commons.math3.util.OpenIntToDoubleHashMap.Iterator; /** * This class implements the {@link RealVector} interface with a * {@link OpenIntToDoubleHashMap} backing store. * @version $Id: OpenMapRealVector.java 1462503 2013-03-29 15:48:27Z luc $ * @since 2.0 * @deprecated As of version 3.1, this class is deprecated, for reasons exposed * in this JIRA * ticket. This * class will be removed in version 4.0. */ @Deprecated public class OpenMapRealVector extends SparseRealVector implements Serializable { /** Default Tolerance for having a value considered zero. */ public static final double DEFAULT_ZERO_TOLERANCE = 1.0e-12; /** Serializable version identifier. */ private static final long serialVersionUID = 8772222695580707260L; /** Entries of the vector. */ private final OpenIntToDoubleHashMap entries; /** Dimension of the vector. */ private final int virtualSize; /** Tolerance for having a value considered zero. */ private final double epsilon; /** * Build a 0-length vector. * Zero-length vectors may be used to initialized construction of vectors * by data gathering. We start with zero-length and use either the {@link * #OpenMapRealVector(OpenMapRealVector, int)} constructor * or one of the {@code append} method ({@link #append(double)}, * {@link #append(RealVector)}) to gather data into this vector. */ public OpenMapRealVector() { this(0, DEFAULT_ZERO_TOLERANCE); } /** * Construct a vector of zeroes. * * @param dimension Size of the vector. */ public OpenMapRealVector(int dimension) { this(dimension, DEFAULT_ZERO_TOLERANCE); } /** * Construct a vector of zeroes, specifying zero tolerance. * * @param dimension Size of the vector. * @param epsilon Tolerance below which a value considered zero. */ public OpenMapRealVector(int dimension, double epsilon) { virtualSize = dimension; entries = new OpenIntToDoubleHashMap(0.0); this.epsilon = epsilon; } /** * Build a resized vector, for use with append. * * @param v Original vector. * @param resize Amount to add. */ protected OpenMapRealVector(OpenMapRealVector v, int resize) { virtualSize = v.getDimension() + resize; entries = new OpenIntToDoubleHashMap(v.entries); epsilon = v.epsilon; } /** * Build a vector with known the sparseness (for advanced use only). * * @param dimension Size of the vector. * @param expectedSize The expected number of non-zero entries. */ public OpenMapRealVector(int dimension, int expectedSize) { this(dimension, expectedSize, DEFAULT_ZERO_TOLERANCE); } /** * Build a vector with known the sparseness and zero tolerance * setting (for advanced use only). * * @param dimension Size of the vector. * @param expectedSize Expected number of non-zero entries. * @param epsilon Tolerance below which a value is considered zero. */ public OpenMapRealVector(int dimension, int expectedSize, double epsilon) { virtualSize = dimension; entries = new OpenIntToDoubleHashMap(expectedSize, 0.0); this.epsilon = epsilon; } /** * Create from an array. * Only non-zero entries will be stored. * * @param values Set of values to create from. */ public OpenMapRealVector(double[] values) { this(values, DEFAULT_ZERO_TOLERANCE); } /** * Create from an array, specifying zero tolerance. * Only non-zero entries will be stored. * * @param values Set of values to create from. * @param epsilon Tolerance below which a value is considered zero. */ public OpenMapRealVector(double[] values, double epsilon) { virtualSize = values.length; entries = new OpenIntToDoubleHashMap(0.0); this.epsilon = epsilon; for (int key = 0; key < values.length; key++) { double value = values[key]; if (!isDefaultValue(value)) { entries.put(key, value); } } } /** * Create from an array. * Only non-zero entries will be stored. * * @param values The set of values to create from */ public OpenMapRealVector(Double[] values) { this(values, DEFAULT_ZERO_TOLERANCE); } /** * Create from an array. * Only non-zero entries will be stored. * * @param values Set of values to create from. * @param epsilon Tolerance below which a value is considered zero. */ public OpenMapRealVector(Double[] values, double epsilon) { virtualSize = values.length; entries = new OpenIntToDoubleHashMap(0.0); this.epsilon = epsilon; for (int key = 0; key < values.length; key++) { double value = values[key].doubleValue(); if (!isDefaultValue(value)) { entries.put(key, value); } } } /** * Copy constructor. * * @param v Instance to copy from. */ public OpenMapRealVector(OpenMapRealVector v) { virtualSize = v.getDimension(); entries = new OpenIntToDoubleHashMap(v.getEntries()); epsilon = v.epsilon; } /** * Generic copy constructor. * * @param v Instance to copy from. */ public OpenMapRealVector(RealVector v) { virtualSize = v.getDimension(); entries = new OpenIntToDoubleHashMap(0.0); epsilon = DEFAULT_ZERO_TOLERANCE; for (int key = 0; key < virtualSize; key++) { double value = v.getEntry(key); if (!isDefaultValue(value)) { entries.put(key, value); } } } /** * Get the entries of this instance. * * @return the entries of this instance. */ private OpenIntToDoubleHashMap getEntries() { return entries; } /** * Determine if this value is within epsilon of zero. * * @param value Value to test * @return {@code true} if this value is within epsilon to zero, * {@code false} otherwise. * @since 2.1 */ protected boolean isDefaultValue(double value) { return FastMath.abs(value) < epsilon; } /** {@inheritDoc} */ @Override public RealVector add(RealVector v) throws DimensionMismatchException { checkVectorDimensions(v.getDimension()); if (v instanceof OpenMapRealVector) { return add((OpenMapRealVector) v); } else { return super.add(v); } } /** * Optimized method to add two OpenMapRealVectors. * It copies the larger vector, then iterates over the smaller. * * @param v Vector to add. * @return the sum of {@code this} and {@code v}. * @throws DimensionMismatchException if the dimensions do not match. */ public OpenMapRealVector add(OpenMapRealVector v) throws DimensionMismatchException { checkVectorDimensions(v.getDimension()); boolean copyThis = entries.size() > v.entries.size(); OpenMapRealVector res = copyThis ? this.copy() : v.copy(); Iterator iter = copyThis ? v.entries.iterator() : entries.iterator(); OpenIntToDoubleHashMap randomAccess = copyThis ? entries : v.entries; while (iter.hasNext()) { iter.advance(); int key = iter.key(); if (randomAccess.containsKey(key)) { res.setEntry(key, randomAccess.get(key) + iter.value()); } else { res.setEntry(key, iter.value()); } } return res; } /** * Optimized method to append a OpenMapRealVector. * @param v vector to append * @return The result of appending {@code v} to self */ public OpenMapRealVector append(OpenMapRealVector v) { OpenMapRealVector res = new OpenMapRealVector(this, v.getDimension()); Iterator iter = v.entries.iterator(); while (iter.hasNext()) { iter.advance(); res.setEntry(iter.key() + virtualSize, iter.value()); } return res; } /** {@inheritDoc} */ @Override public OpenMapRealVector append(RealVector v) { if (v instanceof OpenMapRealVector) { return append((OpenMapRealVector) v); } else { final OpenMapRealVector res = new OpenMapRealVector(this, v.getDimension()); for (int i = 0; i < v.getDimension(); i++) { res.setEntry(i + virtualSize, v.getEntry(i)); } return res; } } /** {@inheritDoc} */ @Override public OpenMapRealVector append(double d) { OpenMapRealVector res = new OpenMapRealVector(this, 1); res.setEntry(virtualSize, d); return res; } /** * {@inheritDoc} * @since 2.1 */ @Override public OpenMapRealVector copy() { return new OpenMapRealVector(this); } /** * Computes the dot product. * Note that the computation is now performed in the parent class: no * performance improvement is to be expected from this overloaded * method. * The previous implementation was buggy and cannot be easily fixed * (see MATH-795). * * @param v Vector. * @return the dot product of this vector with {@code v}. * @throws DimensionMismatchException if {@code v} is not the same size as * {@code this} vector. * * @deprecated as of 3.1 (to be removed in 4.0). The computation is * performed by the parent class. The method must be kept to maintain * backwards compatibility. */ @Deprecated public double dotProduct(OpenMapRealVector v) throws DimensionMismatchException { return dotProduct((RealVector) v); } /** {@inheritDoc} */ @Override public OpenMapRealVector ebeDivide(RealVector v) throws DimensionMismatchException { checkVectorDimensions(v.getDimension()); OpenMapRealVector res = new OpenMapRealVector(this); /* * MATH-803: it is not sufficient to loop through non zero entries of * this only. Indeed, if this[i] = 0d and v[i] = 0d, then * this[i] / v[i] = NaN, and not 0d. */ final int n = getDimension(); for (int i = 0; i < n; i++) { res.setEntry(i, this.getEntry(i) / v.getEntry(i)); } return res; } /** {@inheritDoc} */ @Override public OpenMapRealVector ebeMultiply(RealVector v) throws DimensionMismatchException { checkVectorDimensions(v.getDimension()); OpenMapRealVector res = new OpenMapRealVector(this); Iterator iter = entries.iterator(); while (iter.hasNext()) { iter.advance(); res.setEntry(iter.key(), iter.value() * v.getEntry(iter.key())); } /* * MATH-803: the above loop assumes that 0d * x = 0d for any double x, * which allows to consider only the non-zero entries of this. However, * this fails if this[i] == 0d and (v[i] = NaN or v[i] = Infinity). * * These special cases are handled below. */ if (v.isNaN() || v.isInfinite()) { final int n = getDimension(); for (int i = 0; i < n; i++) { final double y = v.getEntry(i); if (Double.isNaN(y)) { res.setEntry(i, Double.NaN); } else if (Double.isInfinite(y)) { final double x = this.getEntry(i); res.setEntry(i, x * y); } } } return res; } /** {@inheritDoc} */ @Override public OpenMapRealVector getSubVector(int index, int n) throws NotPositiveException, OutOfRangeException { checkIndex(index); if (n < 0) { throw new NotPositiveException(LocalizedFormats.NUMBER_OF_ELEMENTS_SHOULD_BE_POSITIVE, n); } checkIndex(index + n - 1); OpenMapRealVector res = new OpenMapRealVector(n); int end = index + n; Iterator iter = entries.iterator(); while (iter.hasNext()) { iter.advance(); int key = iter.key(); if (key >= index && key < end) { res.setEntry(key - index, iter.value()); } } return res; } /** {@inheritDoc} */ @Override public int getDimension() { return virtualSize; } /** * Optimized method to compute distance. * * @param v Vector to compute distance to. * @return the distance from {@code this} and {@code v}. * @throws DimensionMismatchException if the dimensions do not match. */ public double getDistance(OpenMapRealVector v) throws DimensionMismatchException { checkVectorDimensions(v.getDimension()); Iterator iter = entries.iterator(); double res = 0; while (iter.hasNext()) { iter.advance(); int key = iter.key(); double delta; delta = iter.value() - v.getEntry(key); res += delta * delta; } iter = v.getEntries().iterator(); while (iter.hasNext()) { iter.advance(); int key = iter.key(); if (!entries.containsKey(key)) { final double value = iter.value(); res += value * value; } } return FastMath.sqrt(res); } /** {@inheritDoc} */ @Override public double getDistance(RealVector v) throws DimensionMismatchException { checkVectorDimensions(v.getDimension()); if (v instanceof OpenMapRealVector) { return getDistance((OpenMapRealVector) v); } else { return super.getDistance(v); } } /** {@inheritDoc} */ @Override public double getEntry(int index) throws OutOfRangeException { checkIndex(index); return entries.get(index); } /** * Distance between two vectors. * This method computes the distance consistent with * L1 norm, i.e. the sum of the absolute values of * elements differences. * * @param v Vector to which distance is requested. * @return distance between this vector and {@code v}. * @throws DimensionMismatchException if the dimensions do not match. */ public double getL1Distance(OpenMapRealVector v) throws DimensionMismatchException { checkVectorDimensions(v.getDimension()); double max = 0; Iterator iter = entries.iterator(); while (iter.hasNext()) { iter.advance(); double delta = FastMath.abs(iter.value() - v.getEntry(iter.key())); max += delta; } iter = v.getEntries().iterator(); while (iter.hasNext()) { iter.advance(); int key = iter.key(); if (!entries.containsKey(key)) { double delta = FastMath.abs(iter.value()); max += FastMath.abs(delta); } } return max; } /** {@inheritDoc} */ @Override public double getL1Distance(RealVector v) throws DimensionMismatchException { checkVectorDimensions(v.getDimension()); if (v instanceof OpenMapRealVector) { return getL1Distance((OpenMapRealVector) v); } else { return super.getL1Distance(v); } } /** * Optimized method to compute LInfDistance. * * @param v Vector to compute distance from. * @return the LInfDistance. * @throws DimensionMismatchException if the dimensions do not match. */ private double getLInfDistance(OpenMapRealVector v) throws DimensionMismatchException { checkVectorDimensions(v.getDimension()); double max = 0; Iterator iter = entries.iterator(); while (iter.hasNext()) { iter.advance(); double delta = FastMath.abs(iter.value() - v.getEntry(iter.key())); if (delta > max) { max = delta; } } iter = v.getEntries().iterator(); while (iter.hasNext()) { iter.advance(); int key = iter.key(); if (!entries.containsKey(key) && iter.value() > max) { max = iter.value(); } } return max; } /** {@inheritDoc} */ @Override public double getLInfDistance(RealVector v) throws DimensionMismatchException { checkVectorDimensions(v.getDimension()); if (v instanceof OpenMapRealVector) { return getLInfDistance((OpenMapRealVector) v); } else { return super.getLInfDistance(v); } } /** {@inheritDoc} */ @Override public boolean isInfinite() { boolean infiniteFound = false; Iterator iter = entries.iterator(); while (iter.hasNext()) { iter.advance(); final double value = iter.value(); if (Double.isNaN(value)) { return false; } if (Double.isInfinite(value)) { infiniteFound = true; } } return infiniteFound; } /** {@inheritDoc} */ @Override public boolean isNaN() { Iterator iter = entries.iterator(); while (iter.hasNext()) { iter.advance(); if (Double.isNaN(iter.value())) { return true; } } return false; } /** {@inheritDoc} */ @Override public OpenMapRealVector mapAdd(double d) { return copy().mapAddToSelf(d); } /** {@inheritDoc} */ @Override public OpenMapRealVector mapAddToSelf(double d) { for (int i = 0; i < virtualSize; i++) { setEntry(i, getEntry(i) + d); } return this; } /** {@inheritDoc} */ @Override public void setEntry(int index, double value) throws OutOfRangeException { checkIndex(index); if (!isDefaultValue(value)) { entries.put(index, value); } else if (entries.containsKey(index)) { entries.remove(index); } } /** {@inheritDoc} */ @Override public void setSubVector(int index, RealVector v) throws OutOfRangeException { checkIndex(index); checkIndex(index + v.getDimension() - 1); for (int i = 0; i < v.getDimension(); i++) { setEntry(i + index, v.getEntry(i)); } } /** {@inheritDoc} */ @Override public void set(double value) { for (int i = 0; i < virtualSize; i++) { setEntry(i, value); } } /** * Optimized method to subtract OpenMapRealVectors. * * @param v Vector to subtract from {@code this}. * @return the difference of {@code this} and {@code v}. * @throws DimensionMismatchException if the dimensions do not match. */ public OpenMapRealVector subtract(OpenMapRealVector v) throws DimensionMismatchException { checkVectorDimensions(v.getDimension()); OpenMapRealVector res = copy(); Iterator iter = v.getEntries().iterator(); while (iter.hasNext()) { iter.advance(); int key = iter.key(); if (entries.containsKey(key)) { res.setEntry(key, entries.get(key) - iter.value()); } else { res.setEntry(key, -iter.value()); } } return res; } /** {@inheritDoc} */ @Override public RealVector subtract(RealVector v) throws DimensionMismatchException { checkVectorDimensions(v.getDimension()); if (v instanceof OpenMapRealVector) { return subtract((OpenMapRealVector) v); } else { return super.subtract(v); } } /** {@inheritDoc} */ @Override public OpenMapRealVector unitVector() throws MathArithmeticException { OpenMapRealVector res = copy(); res.unitize(); return res; } /** {@inheritDoc} */ @Override public void unitize() throws MathArithmeticException { double norm = getNorm(); if (isDefaultValue(norm)) { throw new MathArithmeticException(LocalizedFormats.ZERO_NORM); } Iterator iter = entries.iterator(); while (iter.hasNext()) { iter.advance(); entries.put(iter.key(), iter.value() / norm); } } /** {@inheritDoc} */ @Override public double[] toArray() { double[] res = new double[virtualSize]; Iterator iter = entries.iterator(); while (iter.hasNext()) { iter.advance(); res[iter.key()] = iter.value(); } return res; } /** * {@inheritDoc} * Implementation Note: This works on exact values, and as a result * it is possible for {@code a.subtract(b)} to be the zero vector, while * {@code a.hashCode() != b.hashCode()}. */ @Override public int hashCode() { final int prime = 31; int result = 1; long temp; temp = Double.doubleToLongBits(epsilon); result = prime * result + (int) (temp ^ (temp >>> 32)); result = prime * result + virtualSize; Iterator iter = entries.iterator(); while (iter.hasNext()) { iter.advance(); temp = Double.doubleToLongBits(iter.value()); result = prime * result + (int) (temp ^ (temp >>32)); } return result; } /** * {@inheritDoc} * Implementation Note: This performs an exact comparison, and as a result * it is possible for {@code a.subtract(b}} to be the zero vector, while * {@code a.equals(b) == false}. */ @Override public boolean equals(Object obj) { if (this == obj) { return true; } if (!(obj instanceof OpenMapRealVector)) { return false; } OpenMapRealVector other = (OpenMapRealVector) obj; if (virtualSize != other.virtualSize) { return false; } if (Double.doubleToLongBits(epsilon) != Double.doubleToLongBits(other.epsilon)) { return false; } Iterator iter = entries.iterator(); while (iter.hasNext()) { iter.advance(); double test = other.getEntry(iter.key()); if (Double.doubleToLongBits(test) != Double.doubleToLongBits(iter.value())) { return false; } } iter = other.getEntries().iterator(); while (iter.hasNext()) { iter.advance(); double test = iter.value(); if (Double.doubleToLongBits(test) != Double.doubleToLongBits(getEntry(iter.key()))) { return false; } } return true; } /** * * @return the percentage of none zero elements as a decimal percent. * @since 2.2 */ public double getSparsity() { return (double)entries.size()/(double)getDimension(); } /** {@inheritDoc} */ @Override public java.util.Iterator sparseIterator() { return new OpenMapSparseIterator(); } /** * Implementation of {@code Entry} optimized for OpenMap. * This implementation does not allow arbitrary calls to {@code setIndex} * since the order in which entries are returned is undefined. */ protected class OpenMapEntry extends Entry { /** Iterator pointing to the entry. */ private final Iterator iter; /** * Build an entry from an iterator point to an element. * * @param iter Iterator pointing to the entry. */ protected OpenMapEntry(Iterator iter) { this.iter = iter; } /** {@inheritDoc} */ @Override public double getValue() { return iter.value(); } /** {@inheritDoc} */ @Override public void setValue(double value) { entries.put(iter.key(), value); } /** {@inheritDoc} */ @Override public int getIndex() { return iter.key(); } } /** * Iterator class to do iteration over just the non-zero elements. * This implementation is fail-fast, so cannot be used to modify * any zero element. */ protected class OpenMapSparseIterator implements java.util.Iterator { /** Underlying iterator. */ private final Iterator iter; /** Current entry. */ private final Entry current; /** Simple constructor. */ protected OpenMapSparseIterator() { iter = entries.iterator(); current = new OpenMapEntry(iter); } /** {@inheritDoc} */ public boolean hasNext() { return iter.hasNext(); } /** {@inheritDoc} */ public Entry next() { iter.advance(); return current; } /** {@inheritDoc} */ public void remove() { throw new UnsupportedOperationException("Not supported"); } } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/linear/ArrayFieldVector.java100644 1750 1750 104243 12126627717 30115 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.linear; import java.io.Serializable; import java.util.Arrays; import org.apache.commons.math3.Field; import org.apache.commons.math3.FieldElement; import org.apache.commons.math3.exception.DimensionMismatchException; import org.apache.commons.math3.exception.MathArithmeticException; import org.apache.commons.math3.exception.NotPositiveException; import org.apache.commons.math3.exception.NullArgumentException; import org.apache.commons.math3.exception.NumberIsTooLargeException; import org.apache.commons.math3.exception.OutOfRangeException; import org.apache.commons.math3.exception.ZeroException; import org.apache.commons.math3.exception.util.LocalizedFormats; import org.apache.commons.math3.util.MathArrays; import org.apache.commons.math3.util.MathUtils; /** * This class implements the {@link FieldVector} interface with a {@link FieldElement} array. * @param the type of the field elements * @version $Id: ArrayFieldVector.java 1462423 2013-03-29 07:25:18Z luc $ * @since 2.0 */ public class ArrayFieldVector> implements FieldVector, Serializable { /** Serializable version identifier. */ private static final long serialVersionUID = 7648186910365927050L; /** Entries of the vector. */ private T[] data; /** Field to which the elements belong. */ private final Field field; /** * Build a 0-length vector. * Zero-length vectors may be used to initialize construction of vectors * by data gathering. We start with zero-length and use either the {@link * #ArrayFieldVector(ArrayFieldVector, ArrayFieldVector)} constructor * or one of the {@code append} methods ({@link #add(FieldVector)} or * {@link #append(ArrayFieldVector)}) to gather data into this vector. * * @param field field to which the elements belong */ public ArrayFieldVector(final Field field) { this(field, 0); } /** * Construct a vector of zeroes. * * @param field Field to which the elements belong. * @param size Size of the vector. */ public ArrayFieldVector(Field field, int size) { this.field = field; this.data = MathArrays.buildArray(field, size); } /** * Construct a vector with preset values. * * @param size Size of the vector. * @param preset All entries will be set with this value. */ public ArrayFieldVector(int size, T preset) { this(preset.getField(), size); Arrays.fill(data, preset); } /** * Construct a vector from an array, copying the input array. * This constructor needs a non-empty {@code d} array to retrieve * the field from its first element. This implies it cannot build * 0 length vectors. To build vectors from any size, one should * use the {@link #ArrayFieldVector(Field, FieldElement[])} constructor. * * @param d Array. * @throws NullArgumentException if {@code d} is {@code null}. * @throws ZeroException if {@code d} is empty. * @see #ArrayFieldVector(Field, FieldElement[]) */ public ArrayFieldVector(T[] d) throws NullArgumentException, ZeroException { MathUtils.checkNotNull(d); try { field = d[0].getField(); data = d.clone(); } catch (ArrayIndexOutOfBoundsException e) { throw new ZeroException(LocalizedFormats.VECTOR_MUST_HAVE_AT_LEAST_ONE_ELEMENT); } } /** * Construct a vector from an array, copying the input array. * * @param field Field to which the elements belong. * @param d Array. * @throws NullArgumentException if {@code d} is {@code null}. * @see #ArrayFieldVector(FieldElement[]) */ public ArrayFieldVector(Field field, T[] d) throws NullArgumentException { MathUtils.checkNotNull(d); this.field = field; data = d.clone(); } /** * Create a new ArrayFieldVector using the input array as the underlying * data array. * If an array is built specially in order to be embedded in a * ArrayFieldVector and not used directly, the {@code copyArray} may be * set to {@code false}. This will prevent the copying and improve * performance as no new array will be built and no data will be copied. * This constructor needs a non-empty {@code d} array to retrieve * the field from its first element. This implies it cannot build * 0 length vectors. To build vectors from any size, one should * use the {@link #ArrayFieldVector(Field, FieldElement[], boolean)} * constructor. * * @param d Data for the new vector. * @param copyArray If {@code true}, the input array will be copied, * otherwise it will be referenced. * @throws NullArgumentException if {@code d} is {@code null}. * @throws ZeroException if {@code d} is empty. * @see #ArrayFieldVector(FieldElement[]) * @see #ArrayFieldVector(Field, FieldElement[], boolean) */ public ArrayFieldVector(T[] d, boolean copyArray) throws NullArgumentException, ZeroException { MathUtils.checkNotNull(d); if (d.length == 0) { throw new ZeroException(LocalizedFormats.VECTOR_MUST_HAVE_AT_LEAST_ONE_ELEMENT); } field = d[0].getField(); data = copyArray ? d.clone() : d; } /** * Create a new ArrayFieldVector using the input array as the underlying * data array. * If an array is built specially in order to be embedded in a * ArrayFieldVector and not used directly, the {@code copyArray} may be * set to {@code false}. This will prevent the copying and improve * performance as no new array will be built and no data will be copied. * * @param field Field to which the elements belong. * @param d Data for the new vector. * @param copyArray If {@code true}, the input array will be copied, * otherwise it will be referenced. * @throws NullArgumentException if {@code d} is {@code null}. * @see #ArrayFieldVector(FieldElement[], boolean) */ public ArrayFieldVector(Field field, T[] d, boolean copyArray) throws NullArgumentException { MathUtils.checkNotNull(d); this.field = field; data = copyArray ? d.clone() : d; } /** * Construct a vector from part of a array. * * @param d Array. * @param pos Position of the first entry. * @param size Number of entries to copy. * @throws NullArgumentException if {@code d} is {@code null}. * @throws NumberIsTooLargeException if the size of {@code d} is less * than {@code pos + size}. */ public ArrayFieldVector(T[] d, int pos, int size) throws NullArgumentException, NumberIsTooLargeException { MathUtils.checkNotNull(d); if (d.length < pos + size) { throw new NumberIsTooLargeException(pos + size, d.length, true); } field = d[0].getField(); data = MathArrays.buildArray(field, size); System.arraycopy(d, pos, data, 0, size); } /** * Construct a vector from part of a array. * * @param field Field to which the elements belong. * @param d Array. * @param pos Position of the first entry. * @param size Number of entries to copy. * @throws NullArgumentException if {@code d} is {@code null}. * @throws NumberIsTooLargeException if the size of {@code d} is less * than {@code pos + size}. */ public ArrayFieldVector(Field field, T[] d, int pos, int size) throws NullArgumentException, NumberIsTooLargeException { MathUtils.checkNotNull(d); if (d.length < pos + size) { throw new NumberIsTooLargeException(pos + size, d.length, true); } this.field = field; data = MathArrays.buildArray(field, size); System.arraycopy(d, pos, data, 0, size); } /** * Construct a vector from another vector, using a deep copy. * * @param v Vector to copy. * @throws NullArgumentException if {@code v} is {@code null}. */ public ArrayFieldVector(FieldVector v) throws NullArgumentException { MathUtils.checkNotNull(v); field = v.getField(); data = MathArrays.buildArray(field, v.getDimension()); for (int i = 0; i < data.length; ++i) { data[i] = v.getEntry(i); } } /** * Construct a vector from another vector, using a deep copy. * * @param v Vector to copy. * @throws NullArgumentException if {@code v} is {@code null}. */ public ArrayFieldVector(ArrayFieldVector v) throws NullArgumentException { MathUtils.checkNotNull(v); field = v.getField(); data = v.data.clone(); } /** * Construct a vector from another vector. * * @param v Vector to copy. * @param deep If {@code true} perform a deep copy, otherwise perform * a shallow copy * @throws NullArgumentException if {@code v} is {@code null}. */ public ArrayFieldVector(ArrayFieldVector v, boolean deep) throws NullArgumentException { MathUtils.checkNotNull(v); field = v.getField(); data = deep ? v.data.clone() : v.data; } /** * Construct a vector by appending one vector to another vector. * * @param v1 First vector (will be put in front of the new vector). * @param v2 Second vector (will be put at back of the new vector). * @throws NullArgumentException if {@code v1} or {@code v2} is * {@code null}. * @deprecated as of 3.2, replaced by {@link #ArrayFieldVector(FieldVector, FieldVector)} */ @Deprecated public ArrayFieldVector(ArrayFieldVector v1, ArrayFieldVector v2) throws NullArgumentException { this((FieldVector) v1, (FieldVector) v2); } /** * Construct a vector by appending one vector to another vector. * * @param v1 First vector (will be put in front of the new vector). * @param v2 Second vector (will be put at back of the new vector). * @throws NullArgumentException if {@code v1} or {@code v2} is * {@code null}. * @since 3.2 */ public ArrayFieldVector(FieldVector v1, FieldVector v2) throws NullArgumentException { MathUtils.checkNotNull(v1); MathUtils.checkNotNull(v2); field = v1.getField(); final T[] v1Data = (v1 instanceof ArrayFieldVector) ? ((ArrayFieldVector) v1).data : v1.toArray(); final T[] v2Data = (v2 instanceof ArrayFieldVector) ? ((ArrayFieldVector) v2).data : v2.toArray(); data = MathArrays.buildArray(field, v1Data.length + v2Data.length); System.arraycopy(v1Data, 0, data, 0, v1Data.length); System.arraycopy(v2Data, 0, data, v1Data.length, v2Data.length); } /** * Construct a vector by appending one vector to another vector. * * @param v1 First vector (will be put in front of the new vector). * @param v2 Second vector (will be put at back of the new vector). * @throws NullArgumentException if {@code v1} or {@code v2} is * {@code null}. * @deprecated as of 3.2, replaced by {@link #ArrayFieldVector(FieldVector, FieldElement[])} */ @Deprecated public ArrayFieldVector(ArrayFieldVector v1, T[] v2) throws NullArgumentException { this((FieldVector) v1, v2); } /** * Construct a vector by appending one vector to another vector. * * @param v1 First vector (will be put in front of the new vector). * @param v2 Second vector (will be put at back of the new vector). * @throws NullArgumentException if {@code v1} or {@code v2} is * {@code null}. * @since 3.2 */ public ArrayFieldVector(FieldVector v1, T[] v2) throws NullArgumentException { MathUtils.checkNotNull(v1); MathUtils.checkNotNull(v2); field = v1.getField(); final T[] v1Data = (v1 instanceof ArrayFieldVector) ? ((ArrayFieldVector) v1).data : v1.toArray(); data = MathArrays.buildArray(field, v1Data.length + v2.length); System.arraycopy(v1Data, 0, data, 0, v1Data.length); System.arraycopy(v2, 0, data, v1Data.length, v2.length); } /** * Construct a vector by appending one vector to another vector. * * @param v1 First vector (will be put in front of the new vector). * @param v2 Second vector (will be put at back of the new vector). * @throws NullArgumentException if {@code v1} or {@code v2} is * {@code null}. * @deprecated as of 3.2, replaced by {@link #ArrayFieldVector(FieldElement[], FieldVector)} */ @Deprecated public ArrayFieldVector(T[] v1, ArrayFieldVector v2) throws NullArgumentException { this(v1, (FieldVector) v2); } /** * Construct a vector by appending one vector to another vector. * * @param v1 First vector (will be put in front of the new vector). * @param v2 Second vector (will be put at back of the new vector). * @throws NullArgumentException if {@code v1} or {@code v2} is * {@code null}. * @since 3.2 */ public ArrayFieldVector(T[] v1, FieldVector v2) throws NullArgumentException { MathUtils.checkNotNull(v1); MathUtils.checkNotNull(v2); field = v2.getField(); final T[] v2Data = (v2 instanceof ArrayFieldVector) ? ((ArrayFieldVector) v2).data : v2.toArray(); data = MathArrays.buildArray(field, v1.length + v2Data.length); System.arraycopy(v1, 0, data, 0, v1.length); System.arraycopy(v2Data, 0, data, v1.length, v2Data.length); } /** * Construct a vector by appending one vector to another vector. * This constructor needs at least one non-empty array to retrieve * the field from its first element. This implies it cannot build * 0 length vectors. To build vectors from any size, one should * use the {@link #ArrayFieldVector(Field, FieldElement[], FieldElement[])} * constructor. * * @param v1 First vector (will be put in front of the new vector). * @param v2 Second vector (will be put at back of the new vector). * @throws NullArgumentException if {@code v1} or {@code v2} is * {@code null}. * @throws ZeroException if both arrays are empty. * @see #ArrayFieldVector(Field, FieldElement[], FieldElement[]) */ public ArrayFieldVector(T[] v1, T[] v2) throws NullArgumentException, ZeroException { MathUtils.checkNotNull(v1); MathUtils.checkNotNull(v2); if (v1.length + v2.length == 0) { throw new ZeroException(LocalizedFormats.VECTOR_MUST_HAVE_AT_LEAST_ONE_ELEMENT); } data = MathArrays.buildArray(v1[0].getField(), v1.length + v2.length); System.arraycopy(v1, 0, data, 0, v1.length); System.arraycopy(v2, 0, data, v1.length, v2.length); field = data[0].getField(); } /** * Construct a vector by appending one vector to another vector. * * @param field Field to which the elements belong. * @param v1 First vector (will be put in front of the new vector). * @param v2 Second vector (will be put at back of the new vector). * @throws NullArgumentException if {@code v1} or {@code v2} is * {@code null}. * @throws ZeroException if both arrays are empty. * @see #ArrayFieldVector(FieldElement[], FieldElement[]) */ public ArrayFieldVector(Field field, T[] v1, T[] v2) throws NullArgumentException, ZeroException { MathUtils.checkNotNull(v1); MathUtils.checkNotNull(v2); if (v1.length + v2.length == 0) { throw new ZeroException(LocalizedFormats.VECTOR_MUST_HAVE_AT_LEAST_ONE_ELEMENT); } data = MathArrays.buildArray(field, v1.length + v2.length); System.arraycopy(v1, 0, data, 0, v1.length); System.arraycopy(v2, 0, data, v1.length, v2.length); this.field = field; } /** {@inheritDoc} */ public Field getField() { return field; } /** {@inheritDoc} */ public FieldVector copy() { return new ArrayFieldVector(this, true); } /** {@inheritDoc} */ public FieldVector add(FieldVector v) throws DimensionMismatchException { try { return add((ArrayFieldVector) v); } catch (ClassCastException cce) { checkVectorDimensions(v); T[] out = MathArrays.buildArray(field, data.length); for (int i = 0; i < data.length; i++) { out[i] = data[i].add(v.getEntry(i)); } return new ArrayFieldVector(field, out, false); } } /** * Compute the sum of {@code this} and {@code v}. * @param v vector to be added * @return {@code this + v} * @throws DimensionMismatchException if {@code v} is not the same size as * {@code this} */ public ArrayFieldVector add(ArrayFieldVector v) throws DimensionMismatchException { checkVectorDimensions(v.data.length); T[] out = MathArrays.buildArray(field, data.length); for (int i = 0; i < data.length; i++) { out[i] = data[i].add(v.data[i]); } return new ArrayFieldVector(field, out, false); } /** {@inheritDoc} */ public FieldVector subtract(FieldVector v) throws DimensionMismatchException { try { return subtract((ArrayFieldVector) v); } catch (ClassCastException cce) { checkVectorDimensions(v); T[] out = MathArrays.buildArray(field, data.length); for (int i = 0; i < data.length; i++) { out[i] = data[i].subtract(v.getEntry(i)); } return new ArrayFieldVector(field, out, false); } } /** * Compute {@code this} minus {@code v}. * @param v vector to be subtracted * @return {@code this - v} * @throws DimensionMismatchException if {@code v} is not the same size as * {@code this} */ public ArrayFieldVector subtract(ArrayFieldVector v) throws DimensionMismatchException { checkVectorDimensions(v.data.length); T[] out = MathArrays.buildArray(field, data.length); for (int i = 0; i < data.length; i++) { out[i] = data[i].subtract(v.data[i]); } return new ArrayFieldVector(field, out, false); } /** {@inheritDoc} */ public FieldVector mapAdd(T d) throws NullArgumentException { T[] out = MathArrays.buildArray(field, data.length); for (int i = 0; i < data.length; i++) { out[i] = data[i].add(d); } return new ArrayFieldVector(field, out, false); } /** {@inheritDoc} */ public FieldVector mapAddToSelf(T d) throws NullArgumentException { for (int i = 0; i < data.length; i++) { data[i] = data[i].add(d); } return this; } /** {@inheritDoc} */ public FieldVector mapSubtract(T d) throws NullArgumentException { T[] out = MathArrays.buildArray(field, data.length); for (int i = 0; i < data.length; i++) { out[i] = data[i].subtract(d); } return new ArrayFieldVector(field, out, false); } /** {@inheritDoc} */ public FieldVector mapSubtractToSelf(T d) throws NullArgumentException { for (int i = 0; i < data.length; i++) { data[i] = data[i].subtract(d); } return this; } /** {@inheritDoc} */ public FieldVector mapMultiply(T d) throws NullArgumentException { T[] out = MathArrays.buildArray(field, data.length); for (int i = 0; i < data.length; i++) { out[i] = data[i].multiply(d); } return new ArrayFieldVector(field, out, false); } /** {@inheritDoc} */ public FieldVector mapMultiplyToSelf(T d) throws NullArgumentException { for (int i = 0; i < data.length; i++) { data[i] = data[i].multiply(d); } return this; } /** {@inheritDoc} */ public FieldVector mapDivide(T d) throws NullArgumentException, MathArithmeticException { MathUtils.checkNotNull(d); T[] out = MathArrays.buildArray(field, data.length); for (int i = 0; i < data.length; i++) { out[i] = data[i].divide(d); } return new ArrayFieldVector(field, out, false); } /** {@inheritDoc} */ public FieldVector mapDivideToSelf(T d) throws NullArgumentException, MathArithmeticException { MathUtils.checkNotNull(d); for (int i = 0; i < data.length; i++) { data[i] = data[i].divide(d); } return this; } /** {@inheritDoc} */ public FieldVector mapInv() throws MathArithmeticException { T[] out = MathArrays.buildArray(field, data.length); final T one = field.getOne(); for (int i = 0; i < data.length; i++) { try { out[i] = one.divide(data[i]); } catch (final MathArithmeticException e) { throw new MathArithmeticException(LocalizedFormats.INDEX, i); } } return new ArrayFieldVector(field, out, false); } /** {@inheritDoc} */ public FieldVector mapInvToSelf() throws MathArithmeticException { final T one = field.getOne(); for (int i = 0; i < data.length; i++) { try { data[i] = one.divide(data[i]); } catch (final MathArithmeticException e) { throw new MathArithmeticException(LocalizedFormats.INDEX, i); } } return this; } /** {@inheritDoc} */ public FieldVector ebeMultiply(FieldVector v) throws DimensionMismatchException { try { return ebeMultiply((ArrayFieldVector) v); } catch (ClassCastException cce) { checkVectorDimensions(v); T[] out = MathArrays.buildArray(field, data.length); for (int i = 0; i < data.length; i++) { out[i] = data[i].multiply(v.getEntry(i)); } return new ArrayFieldVector(field, out, false); } } /** * Element-by-element multiplication. * @param v vector by which instance elements must be multiplied * @return a vector containing {@code this[i] * v[i]} for all {@code i} * @throws DimensionMismatchException if {@code v} is not the same size as * {@code this} */ public ArrayFieldVector ebeMultiply(ArrayFieldVector v) throws DimensionMismatchException { checkVectorDimensions(v.data.length); T[] out = MathArrays.buildArray(field, data.length); for (int i = 0; i < data.length; i++) { out[i] = data[i].multiply(v.data[i]); } return new ArrayFieldVector(field, out, false); } /** {@inheritDoc} */ public FieldVector ebeDivide(FieldVector v) throws DimensionMismatchException, MathArithmeticException { try { return ebeDivide((ArrayFieldVector) v); } catch (ClassCastException cce) { checkVectorDimensions(v); T[] out = MathArrays.buildArray(field, data.length); for (int i = 0; i < data.length; i++) { try { out[i] = data[i].divide(v.getEntry(i)); } catch (final MathArithmeticException e) { throw new MathArithmeticException(LocalizedFormats.INDEX, i); } } return new ArrayFieldVector(field, out, false); } } /** * Element-by-element division. * @param v vector by which instance elements must be divided * @return a vector containing {@code this[i] / v[i]} for all {@code i} * @throws DimensionMismatchException if {@code v} is not the same size as * {@code this} * @throws MathArithmeticException if one entry of {@code v} is zero. */ public ArrayFieldVector ebeDivide(ArrayFieldVector v) throws DimensionMismatchException, MathArithmeticException { checkVectorDimensions(v.data.length); T[] out = MathArrays.buildArray(field, data.length); for (int i = 0; i < data.length; i++) { try { out[i] = data[i].divide(v.data[i]); } catch (final MathArithmeticException e) { throw new MathArithmeticException(LocalizedFormats.INDEX, i); } } return new ArrayFieldVector(field, out, false); } /** {@inheritDoc} */ public T[] getData() { return data.clone(); } /** * Returns a reference to the underlying data array. *

                              Does not make a fresh copy of the underlying data.

                              * @return array of entries */ public T[] getDataRef() { return data; } /** {@inheritDoc} */ public T dotProduct(FieldVector v) throws DimensionMismatchException { try { return dotProduct((ArrayFieldVector) v); } catch (ClassCastException cce) { checkVectorDimensions(v); T dot = field.getZero(); for (int i = 0; i < data.length; i++) { dot = dot.add(data[i].multiply(v.getEntry(i))); } return dot; } } /** * Compute the dot product. * @param v vector with which dot product should be computed * @return the scalar dot product of {@code this} and {@code v} * @throws DimensionMismatchException if {@code v} is not the same size as * {@code this} */ public T dotProduct(ArrayFieldVector v) throws DimensionMismatchException { checkVectorDimensions(v.data.length); T dot = field.getZero(); for (int i = 0; i < data.length; i++) { dot = dot.add(data[i].multiply(v.data[i])); } return dot; } /** {@inheritDoc} */ public FieldVector projection(FieldVector v) throws DimensionMismatchException, MathArithmeticException { return v.mapMultiply(dotProduct(v).divide(v.dotProduct(v))); } /** Find the orthogonal projection of this vector onto another vector. * @param v vector onto which {@code this} must be projected * @return projection of {@code this} onto {@code v} * @throws DimensionMismatchException if {@code v} is not the same size as * {@code this} * @throws MathArithmeticException if {@code v} is the null vector. */ public ArrayFieldVector projection(ArrayFieldVector v) throws DimensionMismatchException, MathArithmeticException { return (ArrayFieldVector) v.mapMultiply(dotProduct(v).divide(v.dotProduct(v))); } /** {@inheritDoc} */ public FieldMatrix outerProduct(FieldVector v) { try { return outerProduct((ArrayFieldVector) v); } catch (ClassCastException cce) { final int m = data.length; final int n = v.getDimension(); final FieldMatrix out = new Array2DRowFieldMatrix(field, m, n); for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { out.setEntry(i, j, data[i].multiply(v.getEntry(j))); } } return out; } } /** * Compute the outer product. * @param v vector with which outer product should be computed * @return the matrix outer product between instance and v */ public FieldMatrix outerProduct(ArrayFieldVector v) { final int m = data.length; final int n = v.data.length; final FieldMatrix out = new Array2DRowFieldMatrix(field, m, n); for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { out.setEntry(i, j, data[i].multiply(v.data[j])); } } return out; } /** {@inheritDoc} */ public T getEntry(int index) { return data[index]; } /** {@inheritDoc} */ public int getDimension() { return data.length; } /** {@inheritDoc} */ public FieldVector append(FieldVector v) { try { return append((ArrayFieldVector) v); } catch (ClassCastException cce) { return new ArrayFieldVector(this,new ArrayFieldVector(v)); } } /** * Construct a vector by appending a vector to this vector. * @param v vector to append to this one. * @return a new vector */ public ArrayFieldVector append(ArrayFieldVector v) { return new ArrayFieldVector(this, v); } /** {@inheritDoc} */ public FieldVector append(T in) { final T[] out = MathArrays.buildArray(field, data.length + 1); System.arraycopy(data, 0, out, 0, data.length); out[data.length] = in; return new ArrayFieldVector(field, out, false); } /** {@inheritDoc} */ public FieldVector getSubVector(int index, int n) throws OutOfRangeException, NotPositiveException { if (n < 0) { throw new NotPositiveException(LocalizedFormats.NUMBER_OF_ELEMENTS_SHOULD_BE_POSITIVE, n); } ArrayFieldVector out = new ArrayFieldVector(field, n); try { System.arraycopy(data, index, out.data, 0, n); } catch (IndexOutOfBoundsException e) { checkIndex(index); checkIndex(index + n - 1); } return out; } /** {@inheritDoc} */ public void setEntry(int index, T value) { try { data[index] = value; } catch (IndexOutOfBoundsException e) { checkIndex(index); } } /** {@inheritDoc} */ public void setSubVector(int index, FieldVector v) throws OutOfRangeException { try { try { set(index, (ArrayFieldVector) v); } catch (ClassCastException cce) { for (int i = index; i < index + v.getDimension(); ++i) { data[i] = v.getEntry(i-index); } } } catch (IndexOutOfBoundsException e) { checkIndex(index); checkIndex(index + v.getDimension() - 1); } } /** * Set a set of consecutive elements. * * @param index index of first element to be set. * @param v vector containing the values to set. * @throws OutOfRangeException if the index is invalid. */ public void set(int index, ArrayFieldVector v) throws OutOfRangeException { try { System.arraycopy(v.data, 0, data, index, v.data.length); } catch (IndexOutOfBoundsException e) { checkIndex(index); checkIndex(index + v.data.length - 1); } } /** {@inheritDoc} */ public void set(T value) { Arrays.fill(data, value); } /** {@inheritDoc} */ public T[] toArray(){ return data.clone(); } /** * Check if instance and specified vectors have the same dimension. * @param v vector to compare instance with * @exception DimensionMismatchException if the vectors do not * have the same dimensions */ protected void checkVectorDimensions(FieldVector v) throws DimensionMismatchException { checkVectorDimensions(v.getDimension()); } /** * Check if instance dimension is equal to some expected value. * * @param n Expected dimension. * @throws DimensionMismatchException if the dimension is not equal to the * size of {@code this} vector. */ protected void checkVectorDimensions(int n) throws DimensionMismatchException { if (data.length != n) { throw new DimensionMismatchException(data.length, n); } } /** * Test for the equality of two vectors. * * @param other Object to test for equality. * @return {@code true} if two vector objects are equal, {@code false} * otherwise. */ @Override public boolean equals(Object other) { if (this == other) { return true; } if (other == null) { return false; } try { @SuppressWarnings("unchecked") // May fail, but we ignore ClassCastException FieldVector rhs = (FieldVector) other; if (data.length != rhs.getDimension()) { return false; } for (int i = 0; i < data.length; ++i) { if (!data[i].equals(rhs.getEntry(i))) { return false; } } return true; } catch (ClassCastException ex) { // ignore exception return false; } } /** * Get a hashCode for the real vector. *

                              All NaN values have the same hash code.

                              * @return a hash code value for this object */ @Override public int hashCode() { int h = 3542; for (final T a : data) { h = h ^ a.hashCode(); } return h; } /** * Check if an index is valid. * * @param index Index to check. * @exception OutOfRangeException if the index is not valid. */ private void checkIndex(final int index) throws OutOfRangeException { if (index < 0 || index >= getDimension()) { throw new OutOfRangeException(LocalizedFormats.INDEX, index, 0, getDimension() - 1); } } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/linear/FieldLUDecomposition.java100644 1750 1750 36544 12126627717 30721 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.linear; import org.apache.commons.math3.Field; import org.apache.commons.math3.FieldElement; import org.apache.commons.math3.exception.DimensionMismatchException; import org.apache.commons.math3.util.MathArrays; /** * Calculates the LUP-decomposition of a square matrix. *

                              The LUP-decomposition of a matrix A consists of three matrices * L, U and P that satisfy: PA = LU, L is lower triangular, and U is * upper triangular and P is a permutation matrix. All matrices are * m×m.

                              *

                              Since {@link FieldElement field elements} do not provide an ordering * operator, the permutation matrix is computed here only in order to avoid * a zero pivot element, no attempt is done to get the largest pivot * element.

                              *

                              This class is based on the class with similar name from the * JAMA library.

                              *
                                *
                              • a {@link #getP() getP} method has been added,
                              • *
                              • the {@code det} method has been renamed as {@link #getDeterminant() * getDeterminant},
                              • *
                              • the {@code getDoublePivot} method has been removed (but the int based * {@link #getPivot() getPivot} method has been kept),
                              • *
                              • the {@code solve} and {@code isNonSingular} methods have been replaced * by a {@link #getSolver() getSolver} method and the equivalent methods * provided by the returned {@link DecompositionSolver}.
                              • *
                              * * @param the type of the field elements * @see MathWorld * @see Wikipedia * @version $Id: FieldLUDecomposition.java 1449528 2013-02-24 19:06:20Z luc $ * @since 2.0 (changed to concrete class in 3.0) */ public class FieldLUDecomposition> { /** Field to which the elements belong. */ private final Field field; /** Entries of LU decomposition. */ private T[][] lu; /** Pivot permutation associated with LU decomposition. */ private int[] pivot; /** Parity of the permutation associated with the LU decomposition. */ private boolean even; /** Singularity indicator. */ private boolean singular; /** Cached value of L. */ private FieldMatrix cachedL; /** Cached value of U. */ private FieldMatrix cachedU; /** Cached value of P. */ private FieldMatrix cachedP; /** * Calculates the LU-decomposition of the given matrix. * @param matrix The matrix to decompose. * @throws NonSquareMatrixException if matrix is not square */ public FieldLUDecomposition(FieldMatrix matrix) { if (!matrix.isSquare()) { throw new NonSquareMatrixException(matrix.getRowDimension(), matrix.getColumnDimension()); } final int m = matrix.getColumnDimension(); field = matrix.getField(); lu = matrix.getData(); pivot = new int[m]; cachedL = null; cachedU = null; cachedP = null; // Initialize permutation array and parity for (int row = 0; row < m; row++) { pivot[row] = row; } even = true; singular = false; // Loop over columns for (int col = 0; col < m; col++) { T sum = field.getZero(); // upper for (int row = 0; row < col; row++) { final T[] luRow = lu[row]; sum = luRow[col]; for (int i = 0; i < row; i++) { sum = sum.subtract(luRow[i].multiply(lu[i][col])); } luRow[col] = sum; } // lower int nonZero = col; // permutation row for (int row = col; row < m; row++) { final T[] luRow = lu[row]; sum = luRow[col]; for (int i = 0; i < col; i++) { sum = sum.subtract(luRow[i].multiply(lu[i][col])); } luRow[col] = sum; if (lu[nonZero][col].equals(field.getZero())) { // try to select a better permutation choice ++nonZero; } } // Singularity check if (nonZero >= m) { singular = true; return; } // Pivot if necessary if (nonZero != col) { T tmp = field.getZero(); for (int i = 0; i < m; i++) { tmp = lu[nonZero][i]; lu[nonZero][i] = lu[col][i]; lu[col][i] = tmp; } int temp = pivot[nonZero]; pivot[nonZero] = pivot[col]; pivot[col] = temp; even = !even; } // Divide the lower elements by the "winning" diagonal elt. final T luDiag = lu[col][col]; for (int row = col + 1; row < m; row++) { final T[] luRow = lu[row]; luRow[col] = luRow[col].divide(luDiag); } } } /** * Returns the matrix L of the decomposition. *

                              L is a lower-triangular matrix

                              * @return the L matrix (or null if decomposed matrix is singular) */ public FieldMatrix getL() { if ((cachedL == null) && !singular) { final int m = pivot.length; cachedL = new Array2DRowFieldMatrix(field, m, m); for (int i = 0; i < m; ++i) { final T[] luI = lu[i]; for (int j = 0; j < i; ++j) { cachedL.setEntry(i, j, luI[j]); } cachedL.setEntry(i, i, field.getOne()); } } return cachedL; } /** * Returns the matrix U of the decomposition. *

                              U is an upper-triangular matrix

                              * @return the U matrix (or null if decomposed matrix is singular) */ public FieldMatrix getU() { if ((cachedU == null) && !singular) { final int m = pivot.length; cachedU = new Array2DRowFieldMatrix(field, m, m); for (int i = 0; i < m; ++i) { final T[] luI = lu[i]; for (int j = i; j < m; ++j) { cachedU.setEntry(i, j, luI[j]); } } } return cachedU; } /** * Returns the P rows permutation matrix. *

                              P is a sparse matrix with exactly one element set to 1.0 in * each row and each column, all other elements being set to 0.0.

                              *

                              The positions of the 1 elements are given by the {@link #getPivot() * pivot permutation vector}.

                              * @return the P rows permutation matrix (or null if decomposed matrix is singular) * @see #getPivot() */ public FieldMatrix getP() { if ((cachedP == null) && !singular) { final int m = pivot.length; cachedP = new Array2DRowFieldMatrix(field, m, m); for (int i = 0; i < m; ++i) { cachedP.setEntry(i, pivot[i], field.getOne()); } } return cachedP; } /** * Returns the pivot permutation vector. * @return the pivot permutation vector * @see #getP() */ public int[] getPivot() { return pivot.clone(); } /** * Return the determinant of the matrix. * @return determinant of the matrix */ public T getDeterminant() { if (singular) { return field.getZero(); } else { final int m = pivot.length; T determinant = even ? field.getOne() : field.getZero().subtract(field.getOne()); for (int i = 0; i < m; i++) { determinant = determinant.multiply(lu[i][i]); } return determinant; } } /** * Get a solver for finding the A × X = B solution in exact linear sense. * @return a solver */ public FieldDecompositionSolver getSolver() { return new Solver(field, lu, pivot, singular); } /** Specialized solver. */ private static class Solver> implements FieldDecompositionSolver { /** Field to which the elements belong. */ private final Field field; /** Entries of LU decomposition. */ private final T[][] lu; /** Pivot permutation associated with LU decomposition. */ private final int[] pivot; /** Singularity indicator. */ private final boolean singular; /** * Build a solver from decomposed matrix. * @param field field to which the matrix elements belong * @param lu entries of LU decomposition * @param pivot pivot permutation associated with LU decomposition * @param singular singularity indicator */ private Solver(final Field field, final T[][] lu, final int[] pivot, final boolean singular) { this.field = field; this.lu = lu; this.pivot = pivot; this.singular = singular; } /** {@inheritDoc} */ public boolean isNonSingular() { return !singular; } /** {@inheritDoc} */ public FieldVector solve(FieldVector b) { try { return solve((ArrayFieldVector) b); } catch (ClassCastException cce) { final int m = pivot.length; if (b.getDimension() != m) { throw new DimensionMismatchException(b.getDimension(), m); } if (singular) { throw new SingularMatrixException(); } // Apply permutations to b final T[] bp = MathArrays.buildArray(field, m); for (int row = 0; row < m; row++) { bp[row] = b.getEntry(pivot[row]); } // Solve LY = b for (int col = 0; col < m; col++) { final T bpCol = bp[col]; for (int i = col + 1; i < m; i++) { bp[i] = bp[i].subtract(bpCol.multiply(lu[i][col])); } } // Solve UX = Y for (int col = m - 1; col >= 0; col--) { bp[col] = bp[col].divide(lu[col][col]); final T bpCol = bp[col]; for (int i = 0; i < col; i++) { bp[i] = bp[i].subtract(bpCol.multiply(lu[i][col])); } } return new ArrayFieldVector(field, bp, false); } } /** Solve the linear equation A × X = B. *

                              The A matrix is implicit here. It is

                              * @param b right-hand side of the equation A × X = B * @return a vector X such that A × X = B * @throws DimensionMismatchException if the matrices dimensions do not match. * @throws SingularMatrixException if the decomposed matrix is singular. */ public ArrayFieldVector solve(ArrayFieldVector b) { final int m = pivot.length; final int length = b.getDimension(); if (length != m) { throw new DimensionMismatchException(length, m); } if (singular) { throw new SingularMatrixException(); } // Apply permutations to b final T[] bp = MathArrays.buildArray(field, m); for (int row = 0; row < m; row++) { bp[row] = b.getEntry(pivot[row]); } // Solve LY = b for (int col = 0; col < m; col++) { final T bpCol = bp[col]; for (int i = col + 1; i < m; i++) { bp[i] = bp[i].subtract(bpCol.multiply(lu[i][col])); } } // Solve UX = Y for (int col = m - 1; col >= 0; col--) { bp[col] = bp[col].divide(lu[col][col]); final T bpCol = bp[col]; for (int i = 0; i < col; i++) { bp[i] = bp[i].subtract(bpCol.multiply(lu[i][col])); } } return new ArrayFieldVector(bp, false); } /** {@inheritDoc} */ public FieldMatrix solve(FieldMatrix b) { final int m = pivot.length; if (b.getRowDimension() != m) { throw new DimensionMismatchException(b.getRowDimension(), m); } if (singular) { throw new SingularMatrixException(); } final int nColB = b.getColumnDimension(); // Apply permutations to b final T[][] bp = MathArrays.buildArray(field, m, nColB); for (int row = 0; row < m; row++) { final T[] bpRow = bp[row]; final int pRow = pivot[row]; for (int col = 0; col < nColB; col++) { bpRow[col] = b.getEntry(pRow, col); } } // Solve LY = b for (int col = 0; col < m; col++) { final T[] bpCol = bp[col]; for (int i = col + 1; i < m; i++) { final T[] bpI = bp[i]; final T luICol = lu[i][col]; for (int j = 0; j < nColB; j++) { bpI[j] = bpI[j].subtract(bpCol[j].multiply(luICol)); } } } // Solve UX = Y for (int col = m - 1; col >= 0; col--) { final T[] bpCol = bp[col]; final T luDiag = lu[col][col]; for (int j = 0; j < nColB; j++) { bpCol[j] = bpCol[j].divide(luDiag); } for (int i = 0; i < col; i++) { final T[] bpI = bp[i]; final T luICol = lu[i][col]; for (int j = 0; j < nColB; j++) { bpI[j] = bpI[j].subtract(bpCol[j].multiply(luICol)); } } } return new Array2DRowFieldMatrix(field, bp, false); } /** {@inheritDoc} */ public FieldMatrix getInverse() { final int m = pivot.length; final T one = field.getOne(); FieldMatrix identity = new Array2DRowFieldMatrix(field, m, m); for (int i = 0; i < m; ++i) { identity.setEntry(i, i, one); } return solve(identity); } } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/linear/MatrixUtils.java100644 1750 1750 123277 12126627717 27205 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.linear; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.util.Arrays; import org.apache.commons.math3.Field; import org.apache.commons.math3.FieldElement; import org.apache.commons.math3.exception.DimensionMismatchException; import org.apache.commons.math3.exception.MathArithmeticException; import org.apache.commons.math3.exception.NoDataException; import org.apache.commons.math3.exception.NullArgumentException; import org.apache.commons.math3.exception.NumberIsTooSmallException; import org.apache.commons.math3.exception.OutOfRangeException; import org.apache.commons.math3.exception.ZeroException; import org.apache.commons.math3.exception.util.LocalizedFormats; import org.apache.commons.math3.fraction.BigFraction; import org.apache.commons.math3.fraction.Fraction; import org.apache.commons.math3.util.FastMath; import org.apache.commons.math3.util.MathArrays; import org.apache.commons.math3.util.Precision; /** * A collection of static methods that operate on or return matrices. * * @version $Id: MatrixUtils.java 1449528 2013-02-24 19:06:20Z luc $ */ public class MatrixUtils { /** * The default format for {@link RealMatrix} objects. * @since 3.1 */ public static final RealMatrixFormat DEFAULT_FORMAT = RealMatrixFormat.getInstance(); /** * A format for {@link RealMatrix} objects compatible with octave. * @since 3.1 */ public static final RealMatrixFormat OCTAVE_FORMAT = new RealMatrixFormat("[", "]", "", "", "; ", ", "); /** * Private constructor. */ private MatrixUtils() { super(); } /** * Returns a {@link RealMatrix} with specified dimensions. *

                              The type of matrix returned depends on the dimension. Below * 212 elements (i.e. 4096 elements or 64×64 for a * square matrix) which can be stored in a 32kB array, a {@link * Array2DRowRealMatrix} instance is built. Above this threshold a {@link * BlockRealMatrix} instance is built.

                              *

                              The matrix elements are all set to 0.0.

                              * @param rows number of rows of the matrix * @param columns number of columns of the matrix * @return RealMatrix with specified dimensions * @see #createRealMatrix(double[][]) */ public static RealMatrix createRealMatrix(final int rows, final int columns) { return (rows * columns <= 4096) ? new Array2DRowRealMatrix(rows, columns) : new BlockRealMatrix(rows, columns); } /** * Returns a {@link FieldMatrix} with specified dimensions. *

                              The type of matrix returned depends on the dimension. Below * 212 elements (i.e. 4096 elements or 64×64 for a * square matrix), a {@link FieldMatrix} instance is built. Above * this threshold a {@link BlockFieldMatrix} instance is built.

                              *

                              The matrix elements are all set to field.getZero().

                              * @param the type of the field elements * @param field field to which the matrix elements belong * @param rows number of rows of the matrix * @param columns number of columns of the matrix * @return FieldMatrix with specified dimensions * @see #createFieldMatrix(FieldElement[][]) * @since 2.0 */ public static > FieldMatrix createFieldMatrix(final Field field, final int rows, final int columns) { return (rows * columns <= 4096) ? new Array2DRowFieldMatrix(field, rows, columns) : new BlockFieldMatrix(field, rows, columns); } /** * Returns a {@link RealMatrix} whose entries are the the values in the * the input array. *

                              The type of matrix returned depends on the dimension. Below * 212 elements (i.e. 4096 elements or 64×64 for a * square matrix) which can be stored in a 32kB array, a {@link * Array2DRowRealMatrix} instance is built. Above this threshold a {@link * BlockRealMatrix} instance is built.

                              *

                              The input array is copied, not referenced.

                              * * @param data input array * @return RealMatrix containing the values of the array * @throws org.apache.commons.math3.exception.DimensionMismatchException * if {@code data} is not rectangular (not all rows have the same length). * @throws NoDataException if a row or column is empty. * @throws NullArgumentException if either {@code data} or {@code data[0]} * is {@code null}. * @throws DimensionMismatchException if {@code data} is not rectangular. * @see #createRealMatrix(int, int) */ public static RealMatrix createRealMatrix(double[][] data) throws NullArgumentException, DimensionMismatchException, NoDataException { if (data == null || data[0] == null) { throw new NullArgumentException(); } return (data.length * data[0].length <= 4096) ? new Array2DRowRealMatrix(data) : new BlockRealMatrix(data); } /** * Returns a {@link FieldMatrix} whose entries are the the values in the * the input array. *

                              The type of matrix returned depends on the dimension. Below * 212 elements (i.e. 4096 elements or 64×64 for a * square matrix), a {@link FieldMatrix} instance is built. Above * this threshold a {@link BlockFieldMatrix} instance is built.

                              *

                              The input array is copied, not referenced.

                              * @param the type of the field elements * @param data input array * @return a matrix containing the values of the array. * @throws org.apache.commons.math3.exception.DimensionMismatchException * if {@code data} is not rectangular (not all rows have the same length). * @throws NoDataException if a row or column is empty. * @throws NullArgumentException if either {@code data} or {@code data[0]} * is {@code null}. * @see #createFieldMatrix(Field, int, int) * @since 2.0 */ public static > FieldMatrix createFieldMatrix(T[][] data) throws DimensionMismatchException, NoDataException, NullArgumentException { if (data == null || data[0] == null) { throw new NullArgumentException(); } return (data.length * data[0].length <= 4096) ? new Array2DRowFieldMatrix(data) : new BlockFieldMatrix(data); } /** * Returns dimension x dimension identity matrix. * * @param dimension dimension of identity matrix to generate * @return identity matrix * @throws IllegalArgumentException if dimension is not positive * @since 1.1 */ public static RealMatrix createRealIdentityMatrix(int dimension) { final RealMatrix m = createRealMatrix(dimension, dimension); for (int i = 0; i < dimension; ++i) { m.setEntry(i, i, 1.0); } return m; } /** * Returns dimension x dimension identity matrix. * * @param the type of the field elements * @param field field to which the elements belong * @param dimension dimension of identity matrix to generate * @return identity matrix * @throws IllegalArgumentException if dimension is not positive * @since 2.0 */ public static > FieldMatrix createFieldIdentityMatrix(final Field field, final int dimension) { final T zero = field.getZero(); final T one = field.getOne(); final T[][] d = MathArrays.buildArray(field, dimension, dimension); for (int row = 0; row < dimension; row++) { final T[] dRow = d[row]; Arrays.fill(dRow, zero); dRow[row] = one; } return new Array2DRowFieldMatrix(field, d, false); } /** * Returns a diagonal matrix with specified elements. * * @param diagonal diagonal elements of the matrix (the array elements * will be copied) * @return diagonal matrix * @since 2.0 */ public static RealMatrix createRealDiagonalMatrix(final double[] diagonal) { final RealMatrix m = createRealMatrix(diagonal.length, diagonal.length); for (int i = 0; i < diagonal.length; ++i) { m.setEntry(i, i, diagonal[i]); } return m; } /** * Returns a diagonal matrix with specified elements. * * @param the type of the field elements * @param diagonal diagonal elements of the matrix (the array elements * will be copied) * @return diagonal matrix * @since 2.0 */ public static > FieldMatrix createFieldDiagonalMatrix(final T[] diagonal) { final FieldMatrix m = createFieldMatrix(diagonal[0].getField(), diagonal.length, diagonal.length); for (int i = 0; i < diagonal.length; ++i) { m.setEntry(i, i, diagonal[i]); } return m; } /** * Creates a {@link RealVector} using the data from the input array. * * @param data the input data * @return a data.length RealVector * @throws NoDataException if {@code data} is empty. * @throws NullArgumentException if {@code data} is {@code null}. */ public static RealVector createRealVector(double[] data) throws NoDataException, NullArgumentException { if (data == null) { throw new NullArgumentException(); } return new ArrayRealVector(data, true); } /** * Creates a {@link FieldVector} using the data from the input array. * * @param the type of the field elements * @param data the input data * @return a data.length FieldVector * @throws NoDataException if {@code data} is empty. * @throws NullArgumentException if {@code data} is {@code null}. * @throws ZeroException if {@code data} has 0 elements */ public static > FieldVector createFieldVector(final T[] data) throws NoDataException, NullArgumentException, ZeroException { if (data == null) { throw new NullArgumentException(); } if (data.length == 0) { throw new ZeroException(LocalizedFormats.VECTOR_MUST_HAVE_AT_LEAST_ONE_ELEMENT); } return new ArrayFieldVector(data[0].getField(), data, true); } /** * Create a row {@link RealMatrix} using the data from the input * array. * * @param rowData the input row data * @return a 1 x rowData.length RealMatrix * @throws NoDataException if {@code rowData} is empty. * @throws NullArgumentException if {@code rowData} is {@code null}. */ public static RealMatrix createRowRealMatrix(double[] rowData) throws NoDataException, NullArgumentException { if (rowData == null) { throw new NullArgumentException(); } final int nCols = rowData.length; final RealMatrix m = createRealMatrix(1, nCols); for (int i = 0; i < nCols; ++i) { m.setEntry(0, i, rowData[i]); } return m; } /** * Create a row {@link FieldMatrix} using the data from the input * array. * * @param the type of the field elements * @param rowData the input row data * @return a 1 x rowData.length FieldMatrix * @throws NoDataException if {@code rowData} is empty. * @throws NullArgumentException if {@code rowData} is {@code null}. */ public static > FieldMatrix createRowFieldMatrix(final T[] rowData) throws NoDataException, NullArgumentException { if (rowData == null) { throw new NullArgumentException(); } final int nCols = rowData.length; if (nCols == 0) { throw new NoDataException(LocalizedFormats.AT_LEAST_ONE_COLUMN); } final FieldMatrix m = createFieldMatrix(rowData[0].getField(), 1, nCols); for (int i = 0; i < nCols; ++i) { m.setEntry(0, i, rowData[i]); } return m; } /** * Creates a column {@link RealMatrix} using the data from the input * array. * * @param columnData the input column data * @return a columnData x 1 RealMatrix * @throws NoDataException if {@code columnData} is empty. * @throws NullArgumentException if {@code columnData} is {@code null}. */ public static RealMatrix createColumnRealMatrix(double[] columnData) throws NoDataException, NullArgumentException { if (columnData == null) { throw new NullArgumentException(); } final int nRows = columnData.length; final RealMatrix m = createRealMatrix(nRows, 1); for (int i = 0; i < nRows; ++i) { m.setEntry(i, 0, columnData[i]); } return m; } /** * Creates a column {@link FieldMatrix} using the data from the input * array. * * @param the type of the field elements * @param columnData the input column data * @return a columnData x 1 FieldMatrix * @throws NoDataException if {@code data} is empty. * @throws NullArgumentException if {@code columnData} is {@code null}. */ public static > FieldMatrix createColumnFieldMatrix(final T[] columnData) throws NoDataException, NullArgumentException { if (columnData == null) { throw new NullArgumentException(); } final int nRows = columnData.length; if (nRows == 0) { throw new NoDataException(LocalizedFormats.AT_LEAST_ONE_ROW); } final FieldMatrix m = createFieldMatrix(columnData[0].getField(), nRows, 1); for (int i = 0; i < nRows; ++i) { m.setEntry(i, 0, columnData[i]); } return m; } /** * Checks whether a matrix is symmetric, within a given relative tolerance. * * @param matrix Matrix to check. * @param relativeTolerance Tolerance of the symmetry check. * @param raiseException If {@code true}, an exception will be raised if * the matrix is not symmetric. * @return {@code true} if {@code matrix} is symmetric. * @throws NonSquareMatrixException if the matrix is not square. * @throws NonSymmetricMatrixException if the matrix is not symmetric. */ private static boolean isSymmetricInternal(RealMatrix matrix, double relativeTolerance, boolean raiseException) { final int rows = matrix.getRowDimension(); if (rows != matrix.getColumnDimension()) { if (raiseException) { throw new NonSquareMatrixException(rows, matrix.getColumnDimension()); } else { return false; } } for (int i = 0; i < rows; i++) { for (int j = i + 1; j < rows; j++) { final double mij = matrix.getEntry(i, j); final double mji = matrix.getEntry(j, i); if (FastMath.abs(mij - mji) > FastMath.max(FastMath.abs(mij), FastMath.abs(mji)) * relativeTolerance) { if (raiseException) { throw new NonSymmetricMatrixException(i, j, relativeTolerance); } else { return false; } } } } return true; } /** * Checks whether a matrix is symmetric. * * @param matrix Matrix to check. * @param eps Relative tolerance. * @throws NonSquareMatrixException if the matrix is not square. * @throws NonSymmetricMatrixException if the matrix is not symmetric. * @since 3.1 */ public static void checkSymmetric(RealMatrix matrix, double eps) { isSymmetricInternal(matrix, eps, true); } /** * Checks whether a matrix is symmetric. * * @param matrix Matrix to check. * @param eps Relative tolerance. * @return {@code true} if {@code matrix} is symmetric. * @since 3.1 */ public static boolean isSymmetric(RealMatrix matrix, double eps) { return isSymmetricInternal(matrix, eps, false); } /** * Check if matrix indices are valid. * * @param m Matrix. * @param row Row index to check. * @param column Column index to check. * @throws OutOfRangeException if {@code row} or {@code column} is not * a valid index. */ public static void checkMatrixIndex(final AnyMatrix m, final int row, final int column) throws OutOfRangeException { checkRowIndex(m, row); checkColumnIndex(m, column); } /** * Check if a row index is valid. * * @param m Matrix. * @param row Row index to check. * @throws OutOfRangeException if {@code row} is not a valid index. */ public static void checkRowIndex(final AnyMatrix m, final int row) throws OutOfRangeException { if (row < 0 || row >= m.getRowDimension()) { throw new OutOfRangeException(LocalizedFormats.ROW_INDEX, row, 0, m.getRowDimension() - 1); } } /** * Check if a column index is valid. * * @param m Matrix. * @param column Column index to check. * @throws OutOfRangeException if {@code column} is not a valid index. */ public static void checkColumnIndex(final AnyMatrix m, final int column) throws OutOfRangeException { if (column < 0 || column >= m.getColumnDimension()) { throw new OutOfRangeException(LocalizedFormats.COLUMN_INDEX, column, 0, m.getColumnDimension() - 1); } } /** * Check if submatrix ranges indices are valid. * Rows and columns are indicated counting from 0 to {@code n - 1}. * * @param m Matrix. * @param startRow Initial row index. * @param endRow Final row index. * @param startColumn Initial column index. * @param endColumn Final column index. * @throws OutOfRangeException if the indices are invalid. * @throws NumberIsTooSmallException if {@code endRow < startRow} or * {@code endColumn < startColumn}. */ public static void checkSubMatrixIndex(final AnyMatrix m, final int startRow, final int endRow, final int startColumn, final int endColumn) throws NumberIsTooSmallException, OutOfRangeException { checkRowIndex(m, startRow); checkRowIndex(m, endRow); if (endRow < startRow) { throw new NumberIsTooSmallException(LocalizedFormats.INITIAL_ROW_AFTER_FINAL_ROW, endRow, startRow, false); } checkColumnIndex(m, startColumn); checkColumnIndex(m, endColumn); if (endColumn < startColumn) { throw new NumberIsTooSmallException(LocalizedFormats.INITIAL_COLUMN_AFTER_FINAL_COLUMN, endColumn, startColumn, false); } } /** * Check if submatrix ranges indices are valid. * Rows and columns are indicated counting from 0 to n-1. * * @param m Matrix. * @param selectedRows Array of row indices. * @param selectedColumns Array of column indices. * @throws NullArgumentException if {@code selectedRows} or * {@code selectedColumns} are {@code null}. * @throws NoDataException if the row or column selections are empty (zero * length). * @throws OutOfRangeException if row or column selections are not valid. */ public static void checkSubMatrixIndex(final AnyMatrix m, final int[] selectedRows, final int[] selectedColumns) throws NoDataException, NullArgumentException, OutOfRangeException { if (selectedRows == null) { throw new NullArgumentException(); } if (selectedColumns == null) { throw new NullArgumentException(); } if (selectedRows.length == 0) { throw new NoDataException(LocalizedFormats.EMPTY_SELECTED_ROW_INDEX_ARRAY); } if (selectedColumns.length == 0) { throw new NoDataException(LocalizedFormats.EMPTY_SELECTED_COLUMN_INDEX_ARRAY); } for (final int row : selectedRows) { checkRowIndex(m, row); } for (final int column : selectedColumns) { checkColumnIndex(m, column); } } /** * Check if matrices are addition compatible. * * @param left Left hand side matrix. * @param right Right hand side matrix. * @throws MatrixDimensionMismatchException if the matrices are not addition * compatible. */ public static void checkAdditionCompatible(final AnyMatrix left, final AnyMatrix right) throws MatrixDimensionMismatchException { if ((left.getRowDimension() != right.getRowDimension()) || (left.getColumnDimension() != right.getColumnDimension())) { throw new MatrixDimensionMismatchException(left.getRowDimension(), left.getColumnDimension(), right.getRowDimension(), right.getColumnDimension()); } } /** * Check if matrices are subtraction compatible * * @param left Left hand side matrix. * @param right Right hand side matrix. * @throws MatrixDimensionMismatchException if the matrices are not addition * compatible. */ public static void checkSubtractionCompatible(final AnyMatrix left, final AnyMatrix right) throws MatrixDimensionMismatchException { if ((left.getRowDimension() != right.getRowDimension()) || (left.getColumnDimension() != right.getColumnDimension())) { throw new MatrixDimensionMismatchException(left.getRowDimension(), left.getColumnDimension(), right.getRowDimension(), right.getColumnDimension()); } } /** * Check if matrices are multiplication compatible * * @param left Left hand side matrix. * @param right Right hand side matrix. * @throws DimensionMismatchException if matrices are not multiplication * compatible. */ public static void checkMultiplicationCompatible(final AnyMatrix left, final AnyMatrix right) throws DimensionMismatchException { if (left.getColumnDimension() != right.getRowDimension()) { throw new DimensionMismatchException(left.getColumnDimension(), right.getRowDimension()); } } /** * Convert a {@link FieldMatrix}/{@link Fraction} matrix to a {@link RealMatrix}. * @param m Matrix to convert. * @return the converted matrix. */ public static Array2DRowRealMatrix fractionMatrixToRealMatrix(final FieldMatrix m) { final FractionMatrixConverter converter = new FractionMatrixConverter(); m.walkInOptimizedOrder(converter); return converter.getConvertedMatrix(); } /** Converter for {@link FieldMatrix}/{@link Fraction}. */ private static class FractionMatrixConverter extends DefaultFieldMatrixPreservingVisitor { /** Converted array. */ private double[][] data; /** Simple constructor. */ public FractionMatrixConverter() { super(Fraction.ZERO); } /** {@inheritDoc} */ @Override public void start(int rows, int columns, int startRow, int endRow, int startColumn, int endColumn) { data = new double[rows][columns]; } /** {@inheritDoc} */ @Override public void visit(int row, int column, Fraction value) { data[row][column] = value.doubleValue(); } /** * Get the converted matrix. * * @return the converted matrix. */ Array2DRowRealMatrix getConvertedMatrix() { return new Array2DRowRealMatrix(data, false); } } /** * Convert a {@link FieldMatrix}/{@link BigFraction} matrix to a {@link RealMatrix}. * * @param m Matrix to convert. * @return the converted matrix. */ public static Array2DRowRealMatrix bigFractionMatrixToRealMatrix(final FieldMatrix m) { final BigFractionMatrixConverter converter = new BigFractionMatrixConverter(); m.walkInOptimizedOrder(converter); return converter.getConvertedMatrix(); } /** Converter for {@link FieldMatrix}/{@link BigFraction}. */ private static class BigFractionMatrixConverter extends DefaultFieldMatrixPreservingVisitor { /** Converted array. */ private double[][] data; /** Simple constructor. */ public BigFractionMatrixConverter() { super(BigFraction.ZERO); } /** {@inheritDoc} */ @Override public void start(int rows, int columns, int startRow, int endRow, int startColumn, int endColumn) { data = new double[rows][columns]; } /** {@inheritDoc} */ @Override public void visit(int row, int column, BigFraction value) { data[row][column] = value.doubleValue(); } /** * Get the converted matrix. * * @return the converted matrix. */ Array2DRowRealMatrix getConvertedMatrix() { return new Array2DRowRealMatrix(data, false); } } /** Serialize a {@link RealVector}. *

                              * This method is intended to be called from within a private * writeObject method (after a call to * oos.defaultWriteObject()) in a class that has a * {@link RealVector} field, which should be declared transient. * This way, the default handling does not serialize the vector (the {@link * RealVector} interface is not serializable by default) but this method does * serialize it specifically. *

                              *

                              * The following example shows how a simple class with a name and a real vector * should be written: *

                              
                                   * public class NamedVector implements Serializable {
                                   *
                                   *     private final String name;
                                   *     private final transient RealVector coefficients;
                                   *
                                   *     // omitted constructors, getters ...
                                   *
                                   *     private void writeObject(ObjectOutputStream oos) throws IOException {
                                   *         oos.defaultWriteObject();  // takes care of name field
                                   *         MatrixUtils.serializeRealVector(coefficients, oos);
                                   *     }
                                   *
                                   *     private void readObject(ObjectInputStream ois) throws ClassNotFoundException, IOException {
                                   *         ois.defaultReadObject();  // takes care of name field
                                   *         MatrixUtils.deserializeRealVector(this, "coefficients", ois);
                                   *     }
                                   *
                                   * }
                                   * 
                              *

                              * * @param vector real vector to serialize * @param oos stream where the real vector should be written * @exception IOException if object cannot be written to stream * @see #deserializeRealVector(Object, String, ObjectInputStream) */ public static void serializeRealVector(final RealVector vector, final ObjectOutputStream oos) throws IOException { final int n = vector.getDimension(); oos.writeInt(n); for (int i = 0; i < n; ++i) { oos.writeDouble(vector.getEntry(i)); } } /** Deserialize a {@link RealVector} field in a class. *

                              * This method is intended to be called from within a private * readObject method (after a call to * ois.defaultReadObject()) in a class that has a * {@link RealVector} field, which should be declared transient. * This way, the default handling does not deserialize the vector (the {@link * RealVector} interface is not serializable by default) but this method does * deserialize it specifically. *

                              * @param instance instance in which the field must be set up * @param fieldName name of the field within the class (may be private and final) * @param ois stream from which the real vector should be read * @exception ClassNotFoundException if a class in the stream cannot be found * @exception IOException if object cannot be read from the stream * @see #serializeRealVector(RealVector, ObjectOutputStream) */ public static void deserializeRealVector(final Object instance, final String fieldName, final ObjectInputStream ois) throws ClassNotFoundException, IOException { try { // read the vector data final int n = ois.readInt(); final double[] data = new double[n]; for (int i = 0; i < n; ++i) { data[i] = ois.readDouble(); } // create the instance final RealVector vector = new ArrayRealVector(data, false); // set up the field final java.lang.reflect.Field f = instance.getClass().getDeclaredField(fieldName); f.setAccessible(true); f.set(instance, vector); } catch (NoSuchFieldException nsfe) { IOException ioe = new IOException(); ioe.initCause(nsfe); throw ioe; } catch (IllegalAccessException iae) { IOException ioe = new IOException(); ioe.initCause(iae); throw ioe; } } /** Serialize a {@link RealMatrix}. *

                              * This method is intended to be called from within a private * writeObject method (after a call to * oos.defaultWriteObject()) in a class that has a * {@link RealMatrix} field, which should be declared transient. * This way, the default handling does not serialize the matrix (the {@link * RealMatrix} interface is not serializable by default) but this method does * serialize it specifically. *

                              *

                              * The following example shows how a simple class with a name and a real matrix * should be written: *

                              
                                   * public class NamedMatrix implements Serializable {
                                   *
                                   *     private final String name;
                                   *     private final transient RealMatrix coefficients;
                                   *
                                   *     // omitted constructors, getters ...
                                   *
                                   *     private void writeObject(ObjectOutputStream oos) throws IOException {
                                   *         oos.defaultWriteObject();  // takes care of name field
                                   *         MatrixUtils.serializeRealMatrix(coefficients, oos);
                                   *     }
                                   *
                                   *     private void readObject(ObjectInputStream ois) throws ClassNotFoundException, IOException {
                                   *         ois.defaultReadObject();  // takes care of name field
                                   *         MatrixUtils.deserializeRealMatrix(this, "coefficients", ois);
                                   *     }
                                   *
                                   * }
                                   * 
                              *

                              * * @param matrix real matrix to serialize * @param oos stream where the real matrix should be written * @exception IOException if object cannot be written to stream * @see #deserializeRealMatrix(Object, String, ObjectInputStream) */ public static void serializeRealMatrix(final RealMatrix matrix, final ObjectOutputStream oos) throws IOException { final int n = matrix.getRowDimension(); final int m = matrix.getColumnDimension(); oos.writeInt(n); oos.writeInt(m); for (int i = 0; i < n; ++i) { for (int j = 0; j < m; ++j) { oos.writeDouble(matrix.getEntry(i, j)); } } } /** Deserialize a {@link RealMatrix} field in a class. *

                              * This method is intended to be called from within a private * readObject method (after a call to * ois.defaultReadObject()) in a class that has a * {@link RealMatrix} field, which should be declared transient. * This way, the default handling does not deserialize the matrix (the {@link * RealMatrix} interface is not serializable by default) but this method does * deserialize it specifically. *

                              * @param instance instance in which the field must be set up * @param fieldName name of the field within the class (may be private and final) * @param ois stream from which the real matrix should be read * @exception ClassNotFoundException if a class in the stream cannot be found * @exception IOException if object cannot be read from the stream * @see #serializeRealMatrix(RealMatrix, ObjectOutputStream) */ public static void deserializeRealMatrix(final Object instance, final String fieldName, final ObjectInputStream ois) throws ClassNotFoundException, IOException { try { // read the matrix data final int n = ois.readInt(); final int m = ois.readInt(); final double[][] data = new double[n][m]; for (int i = 0; i < n; ++i) { final double[] dataI = data[i]; for (int j = 0; j < m; ++j) { dataI[j] = ois.readDouble(); } } // create the instance final RealMatrix matrix = new Array2DRowRealMatrix(data, false); // set up the field final java.lang.reflect.Field f = instance.getClass().getDeclaredField(fieldName); f.setAccessible(true); f.set(instance, matrix); } catch (NoSuchFieldException nsfe) { IOException ioe = new IOException(); ioe.initCause(nsfe); throw ioe; } catch (IllegalAccessException iae) { IOException ioe = new IOException(); ioe.initCause(iae); throw ioe; } } /**Solve a system of composed of a Lower Triangular Matrix * {@link RealMatrix}. *

                              * This method is called to solve systems of equations which are * of the lower triangular form. The matrix {@link RealMatrix} * is assumed, though not checked, to be in lower triangular form. * The vector {@link RealVector} is overwritten with the solution. * The matrix is checked that it is square and its dimensions match * the length of the vector. *

                              * @param rm RealMatrix which is lower triangular * @param b RealVector this is overwritten * @throws DimensionMismatchException if the matrix and vector are not * conformable * @throws NonSquareMatrixException if the matrix {@code rm} is not square * @throws MathArithmeticException if the absolute value of one of the diagonal * coefficient of {@code rm} is lower than {@link Precision#SAFE_MIN} */ public static void solveLowerTriangularSystem(RealMatrix rm, RealVector b) throws DimensionMismatchException, MathArithmeticException, NonSquareMatrixException { if ((rm == null) || (b == null) || ( rm.getRowDimension() != b.getDimension())) { throw new DimensionMismatchException( (rm == null) ? 0 : rm.getRowDimension(), (b == null) ? 0 : b.getDimension()); } if( rm.getColumnDimension() != rm.getRowDimension() ){ throw new NonSquareMatrixException(rm.getRowDimension(), rm.getColumnDimension()); } int rows = rm.getRowDimension(); for( int i = 0 ; i < rows ; i++ ){ double diag = rm.getEntry(i, i); if( FastMath.abs(diag) < Precision.SAFE_MIN ){ throw new MathArithmeticException(LocalizedFormats.ZERO_DENOMINATOR); } double bi = b.getEntry(i)/diag; b.setEntry(i, bi ); for( int j = i+1; j< rows; j++ ){ b.setEntry(j, b.getEntry(j)-bi*rm.getEntry(j,i) ); } } } /** Solver a system composed of an Upper Triangular Matrix * {@link RealMatrix}. *

                              * This method is called to solve systems of equations which are * of the lower triangular form. The matrix {@link RealMatrix} * is assumed, though not checked, to be in upper triangular form. * The vector {@link RealVector} is overwritten with the solution. * The matrix is checked that it is square and its dimensions match * the length of the vector. *

                              * @param rm RealMatrix which is upper triangular * @param b RealVector this is overwritten * @throws DimensionMismatchException if the matrix and vector are not * conformable * @throws NonSquareMatrixException if the matrix {@code rm} is not * square * @throws MathArithmeticException if the absolute value of one of the diagonal * coefficient of {@code rm} is lower than {@link Precision#SAFE_MIN} */ public static void solveUpperTriangularSystem(RealMatrix rm, RealVector b) throws DimensionMismatchException, MathArithmeticException, NonSquareMatrixException { if ((rm == null) || (b == null) || ( rm.getRowDimension() != b.getDimension())) { throw new DimensionMismatchException( (rm == null) ? 0 : rm.getRowDimension(), (b == null) ? 0 : b.getDimension()); } if( rm.getColumnDimension() != rm.getRowDimension() ){ throw new NonSquareMatrixException(rm.getRowDimension(), rm.getColumnDimension()); } int rows = rm.getRowDimension(); for( int i = rows-1 ; i >-1 ; i-- ){ double diag = rm.getEntry(i, i); if( FastMath.abs(diag) < Precision.SAFE_MIN ){ throw new MathArithmeticException(LocalizedFormats.ZERO_DENOMINATOR); } double bi = b.getEntry(i)/diag; b.setEntry(i, bi ); for( int j = i-1; j>-1; j-- ){ b.setEntry(j, b.getEntry(j)-bi*rm.getEntry(j,i) ); } } } /** * Computes the inverse of the given matrix by splitting it into * 4 sub-matrices. * * @param m Matrix whose inverse must be computed. * @param splitIndex Index that determines the "split" line and * column. * The element corresponding to this index will part of the * upper-left sub-matrix. * @return the inverse of {@code m}. * @throws NonSquareMatrixException if {@code m} is not square. */ public static RealMatrix blockInverse(RealMatrix m, int splitIndex) { final int n = m.getRowDimension(); if (m.getColumnDimension() != n) { throw new NonSquareMatrixException(m.getRowDimension(), m.getColumnDimension()); } final int splitIndex1 = splitIndex + 1; final RealMatrix a = m.getSubMatrix(0, splitIndex, 0, splitIndex); final RealMatrix b = m.getSubMatrix(0, splitIndex, splitIndex1, n - 1); final RealMatrix c = m.getSubMatrix(splitIndex1, n - 1, 0, splitIndex); final RealMatrix d = m.getSubMatrix(splitIndex1, n - 1, splitIndex1, n - 1); final SingularValueDecomposition aDec = new SingularValueDecomposition(a); final RealMatrix aInv = aDec.getSolver().getInverse(); final SingularValueDecomposition dDec = new SingularValueDecomposition(d); final RealMatrix dInv = dDec.getSolver().getInverse(); final RealMatrix tmp1 = a.subtract(b.multiply(dInv).multiply(c)); final SingularValueDecomposition tmp1Dec = new SingularValueDecomposition(tmp1); final RealMatrix result00 = tmp1Dec.getSolver().getInverse(); final RealMatrix tmp2 = d.subtract(c.multiply(aInv).multiply(b)); final SingularValueDecomposition tmp2Dec = new SingularValueDecomposition(tmp2); final RealMatrix result11 = tmp2Dec.getSolver().getInverse(); final RealMatrix result01 = aInv.multiply(b).multiply(result11).scalarMultiply(-1); final RealMatrix result10 = dInv.multiply(c).multiply(result00).scalarMultiply(-1); final RealMatrix result = new Array2DRowRealMatrix(n, n); result.setSubMatrix(result00.getData(), 0, 0); result.setSubMatrix(result01.getData(), 0, splitIndex1); result.setSubMatrix(result10.getData(), splitIndex1, 0); result.setSubMatrix(result11.getData(), splitIndex1, splitIndex1); return result; } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/linear/package-info.java100644 1750 1750 1557 12126627717 27200 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ /** * Linear algebra support. */ package org.apache.commons.math3.linear; commons-math3-3.2-src/src/main/java/org/apache/commons/math3/linear/SparseRealVector.java100644 1750 1750 2422 12126627717 30070 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.linear; /** * Marker class for RealVectors that require sparse backing storage * @version $Id: SparseRealVector.java 1416643 2012-12-03 19:37:14Z tn $ * @since 2.0 * @deprecated As of version 3.1, this class is deprecated, for reasons exposed * in this JIRA * ticket. This * class will be removed in version 4.0. * */ @Deprecated public abstract class SparseRealVector extends RealVector {} commons-math3-3.2-src/src/main/java/org/apache/commons/math3/linear/FieldMatrixChangingVisitor.java100644 1750 1750 4250 12126627717 32074 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.linear; import org.apache.commons.math3.FieldElement; /** * Interface defining a visitor for matrix entries. * * @param the type of the field elements * @version $Id: FieldMatrixChangingVisitor.java 1416643 2012-12-03 19:37:14Z tn $ * @since 2.0 */ public interface FieldMatrixChangingVisitor> { /** * Start visiting a matrix. *

                              This method is called once before any entry of the matrix is visited.

                              * @param rows number of rows of the matrix * @param columns number of columns of the matrix * @param startRow Initial row index * @param endRow Final row index (inclusive) * @param startColumn Initial column index * @param endColumn Final column index (inclusive) */ void start(int rows, int columns, int startRow, int endRow, int startColumn, int endColumn); /** * Visit one matrix entry. * @param row row index of the entry * @param column column index of the entry * @param value current value of the entry * @return the new value to be set for the entry */ T visit(int row, int column, T value); /** * End visiting a matrix. *

                              This method is called once after all entries of the matrix have been visited.

                              * @return the value that the walkInXxxOrder must return */ T end(); } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/linear/EigenDecomposition.java100644 1750 1750 111116 12126627717 30471 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.linear; import org.apache.commons.math3.complex.Complex; import org.apache.commons.math3.exception.MathArithmeticException; import org.apache.commons.math3.exception.MathUnsupportedOperationException; import org.apache.commons.math3.exception.MaxCountExceededException; import org.apache.commons.math3.exception.DimensionMismatchException; import org.apache.commons.math3.exception.util.LocalizedFormats; import org.apache.commons.math3.util.Precision; import org.apache.commons.math3.util.FastMath; /** * Calculates the eigen decomposition of a real matrix. *

                              The eigen decomposition of matrix A is a set of two matrices: * V and D such that A = V × D × VT. * A, V and D are all m × m matrices.

                              *

                              This class is similar in spirit to the EigenvalueDecomposition * class from the JAMA * library, with the following changes:

                              *
                                *
                              • a {@link #getVT() getVt} method has been added,
                              • *
                              • two {@link #getRealEigenvalue(int) getRealEigenvalue} and {@link #getImagEigenvalue(int) * getImagEigenvalue} methods to pick up a single eigenvalue have been added,
                              • *
                              • a {@link #getEigenvector(int) getEigenvector} method to pick up a single * eigenvector has been added,
                              • *
                              • a {@link #getDeterminant() getDeterminant} method has been added.
                              • *
                              • a {@link #getSolver() getSolver} method has been added.
                              • *
                              *

                              * As of 3.1, this class supports general real matrices (both symmetric and non-symmetric): *

                              *

                              * If A is symmetric, then A = V*D*V' where the eigenvalue matrix D is diagonal and the eigenvector * matrix V is orthogonal, i.e. A = V.multiply(D.multiply(V.transpose())) and * V.multiply(V.transpose()) equals the identity matrix. *

                              *

                              * If A is not symmetric, then the eigenvalue matrix D is block diagonal with the real eigenvalues * in 1-by-1 blocks and any complex eigenvalues, lambda + i*mu, in 2-by-2 blocks: *

                               *    [lambda, mu    ]
                               *    [   -mu, lambda]
                               * 
                              * The columns of V represent the eigenvectors in the sense that A*V = V*D, * i.e. A.multiply(V) equals V.multiply(D). * The matrix V may be badly conditioned, or even singular, so the validity of the equation * A = V*D*inverse(V) depends upon the condition of V. *

                              *

                              * This implementation is based on the paper by A. Drubrulle, R.S. Martin and * J.H. Wilkinson "The Implicit QL Algorithm" in Wilksinson and Reinsch (1971) * Handbook for automatic computation, vol. 2, Linear algebra, Springer-Verlag, * New-York *

                              * @see MathWorld * @see Wikipedia * @version $Id: EigenDecomposition.java 1452595 2013-03-04 23:29:39Z tn $ * @since 2.0 (changed to concrete class in 3.0) */ public class EigenDecomposition { /** Internally used epsilon criteria. */ private static final double EPSILON = 1e-12; /** Maximum number of iterations accepted in the implicit QL transformation */ private byte maxIter = 30; /** Main diagonal of the tridiagonal matrix. */ private double[] main; /** Secondary diagonal of the tridiagonal matrix. */ private double[] secondary; /** * Transformer to tridiagonal (may be null if matrix is already * tridiagonal). */ private TriDiagonalTransformer transformer; /** Real part of the realEigenvalues. */ private double[] realEigenvalues; /** Imaginary part of the realEigenvalues. */ private double[] imagEigenvalues; /** Eigenvectors. */ private ArrayRealVector[] eigenvectors; /** Cached value of V. */ private RealMatrix cachedV; /** Cached value of D. */ private RealMatrix cachedD; /** Cached value of Vt. */ private RealMatrix cachedVt; /** Whether the matrix is symmetric. */ private final boolean isSymmetric; /** * Calculates the eigen decomposition of the given real matrix. *

                              * Supports decomposition of a general matrix since 3.1. * * @param matrix Matrix to decompose. * @throws MaxCountExceededException if the algorithm fails to converge. * @throws MathArithmeticException if the decomposition of a general matrix * results in a matrix with zero norm * @since 3.1 */ public EigenDecomposition(final RealMatrix matrix) throws MathArithmeticException { final double symTol = 10 * matrix.getRowDimension() * matrix.getColumnDimension() * Precision.EPSILON; isSymmetric = MatrixUtils.isSymmetric(matrix, symTol); if (isSymmetric) { transformToTridiagonal(matrix); findEigenVectors(transformer.getQ().getData()); } else { final SchurTransformer t = transformToSchur(matrix); findEigenVectorsFromSchur(t); } } /** * Calculates the eigen decomposition of the given real matrix. * * @param matrix Matrix to decompose. * @param splitTolerance Dummy parameter (present for backward * compatibility only). * @throws MathArithmeticException if the decomposition of a general matrix * results in a matrix with zero norm * @throws MaxCountExceededException if the algorithm fails to converge. * @deprecated in 3.1 (to be removed in 4.0) due to unused parameter */ @Deprecated public EigenDecomposition(final RealMatrix matrix, final double splitTolerance) throws MathArithmeticException { this(matrix); } /** * Calculates the eigen decomposition of the symmetric tridiagonal * matrix. The Householder matrix is assumed to be the identity matrix. * * @param main Main diagonal of the symmetric tridiagonal form. * @param secondary Secondary of the tridiagonal form. * @throws MaxCountExceededException if the algorithm fails to converge. * @since 3.1 */ public EigenDecomposition(final double[] main, final double[] secondary) { isSymmetric = true; this.main = main.clone(); this.secondary = secondary.clone(); transformer = null; final int size = main.length; final double[][] z = new double[size][size]; for (int i = 0; i < size; i++) { z[i][i] = 1.0; } findEigenVectors(z); } /** * Calculates the eigen decomposition of the symmetric tridiagonal * matrix. The Householder matrix is assumed to be the identity matrix. * * @param main Main diagonal of the symmetric tridiagonal form. * @param secondary Secondary of the tridiagonal form. * @param splitTolerance Dummy parameter (present for backward * compatibility only). * @throws MaxCountExceededException if the algorithm fails to converge. * @deprecated in 3.1 (to be removed in 4.0) due to unused parameter */ @Deprecated public EigenDecomposition(final double[] main, final double[] secondary, final double splitTolerance) { this(main, secondary); } /** * Gets the matrix V of the decomposition. * V is an orthogonal matrix, i.e. its transpose is also its inverse. * The columns of V are the eigenvectors of the original matrix. * No assumption is made about the orientation of the system axes formed * by the columns of V (e.g. in a 3-dimension space, V can form a left- * or right-handed system). * * @return the V matrix. */ public RealMatrix getV() { if (cachedV == null) { final int m = eigenvectors.length; cachedV = MatrixUtils.createRealMatrix(m, m); for (int k = 0; k < m; ++k) { cachedV.setColumnVector(k, eigenvectors[k]); } } // return the cached matrix return cachedV; } /** * Gets the block diagonal matrix D of the decomposition. * D is a block diagonal matrix. * Real eigenvalues are on the diagonal while complex values are on * 2x2 blocks { {real +imaginary}, {-imaginary, real} }. * * @return the D matrix. * * @see #getRealEigenvalues() * @see #getImagEigenvalues() */ public RealMatrix getD() { if (cachedD == null) { // cache the matrix for subsequent calls cachedD = MatrixUtils.createRealDiagonalMatrix(realEigenvalues); for (int i = 0; i < imagEigenvalues.length; i++) { if (Precision.compareTo(imagEigenvalues[i], 0.0, EPSILON) > 0) { cachedD.setEntry(i, i+1, imagEigenvalues[i]); } else if (Precision.compareTo(imagEigenvalues[i], 0.0, EPSILON) < 0) { cachedD.setEntry(i, i-1, imagEigenvalues[i]); } } } return cachedD; } /** * Gets the transpose of the matrix V of the decomposition. * V is an orthogonal matrix, i.e. its transpose is also its inverse. * The columns of V are the eigenvectors of the original matrix. * No assumption is made about the orientation of the system axes formed * by the columns of V (e.g. in a 3-dimension space, V can form a left- * or right-handed system). * * @return the transpose of the V matrix. */ public RealMatrix getVT() { if (cachedVt == null) { final int m = eigenvectors.length; cachedVt = MatrixUtils.createRealMatrix(m, m); for (int k = 0; k < m; ++k) { cachedVt.setRowVector(k, eigenvectors[k]); } } // return the cached matrix return cachedVt; } /** * Returns whether the calculated eigen values are complex or real. *

                              The method performs a zero check for each element of the * {@link #getImagEigenvalues()} array and returns {@code true} if any * element is not equal to zero. * * @return {@code true} if the eigen values are complex, {@code false} otherwise * @since 3.1 */ public boolean hasComplexEigenvalues() { for (int i = 0; i < imagEigenvalues.length; i++) { if (!Precision.equals(imagEigenvalues[i], 0.0, EPSILON)) { return true; } } return false; } /** * Gets a copy of the real parts of the eigenvalues of the original matrix. * * @return a copy of the real parts of the eigenvalues of the original matrix. * * @see #getD() * @see #getRealEigenvalue(int) * @see #getImagEigenvalues() */ public double[] getRealEigenvalues() { return realEigenvalues.clone(); } /** * Returns the real part of the ith eigenvalue of the original * matrix. * * @param i index of the eigenvalue (counting from 0) * @return real part of the ith eigenvalue of the original * matrix. * * @see #getD() * @see #getRealEigenvalues() * @see #getImagEigenvalue(int) */ public double getRealEigenvalue(final int i) { return realEigenvalues[i]; } /** * Gets a copy of the imaginary parts of the eigenvalues of the original * matrix. * * @return a copy of the imaginary parts of the eigenvalues of the original * matrix. * * @see #getD() * @see #getImagEigenvalue(int) * @see #getRealEigenvalues() */ public double[] getImagEigenvalues() { return imagEigenvalues.clone(); } /** * Gets the imaginary part of the ith eigenvalue of the original * matrix. * * @param i Index of the eigenvalue (counting from 0). * @return the imaginary part of the ith eigenvalue of the original * matrix. * * @see #getD() * @see #getImagEigenvalues() * @see #getRealEigenvalue(int) */ public double getImagEigenvalue(final int i) { return imagEigenvalues[i]; } /** * Gets a copy of the ith eigenvector of the original matrix. * * @param i Index of the eigenvector (counting from 0). * @return a copy of the ith eigenvector of the original matrix. * @see #getD() */ public RealVector getEigenvector(final int i) { return eigenvectors[i].copy(); } /** * Computes the determinant of the matrix. * * @return the determinant of the matrix. */ public double getDeterminant() { double determinant = 1; for (double lambda : realEigenvalues) { determinant *= lambda; } return determinant; } /** * Computes the square-root of the matrix. * This implementation assumes that the matrix is symmetric and positive * definite. * * @return the square-root of the matrix. * @throws MathUnsupportedOperationException if the matrix is not * symmetric or not positive definite. * @since 3.1 */ public RealMatrix getSquareRoot() { if (!isSymmetric) { throw new MathUnsupportedOperationException(); } final double[] sqrtEigenValues = new double[realEigenvalues.length]; for (int i = 0; i < realEigenvalues.length; i++) { final double eigen = realEigenvalues[i]; if (eigen <= 0) { throw new MathUnsupportedOperationException(); } sqrtEigenValues[i] = FastMath.sqrt(eigen); } final RealMatrix sqrtEigen = MatrixUtils.createRealDiagonalMatrix(sqrtEigenValues); final RealMatrix v = getV(); final RealMatrix vT = getVT(); return v.multiply(sqrtEigen).multiply(vT); } /** * Gets a solver for finding the A × X = B solution in exact * linear sense. *

                              * Since 3.1, eigen decomposition of a general matrix is supported, * but the {@link DecompositionSolver} only supports real eigenvalues. * * @return a solver * @throws MathUnsupportedOperationException if the decomposition resulted in * complex eigenvalues */ public DecompositionSolver getSolver() { if (hasComplexEigenvalues()) { throw new MathUnsupportedOperationException(); } return new Solver(realEigenvalues, imagEigenvalues, eigenvectors); } /** Specialized solver. */ private static class Solver implements DecompositionSolver { /** Real part of the realEigenvalues. */ private double[] realEigenvalues; /** Imaginary part of the realEigenvalues. */ private double[] imagEigenvalues; /** Eigenvectors. */ private final ArrayRealVector[] eigenvectors; /** * Builds a solver from decomposed matrix. * * @param realEigenvalues Real parts of the eigenvalues. * @param imagEigenvalues Imaginary parts of the eigenvalues. * @param eigenvectors Eigenvectors. */ private Solver(final double[] realEigenvalues, final double[] imagEigenvalues, final ArrayRealVector[] eigenvectors) { this.realEigenvalues = realEigenvalues; this.imagEigenvalues = imagEigenvalues; this.eigenvectors = eigenvectors; } /** * Solves the linear equation A × X = B for symmetric matrices A. *

                              * This method only finds exact linear solutions, i.e. solutions for * which ||A × X - B|| is exactly 0. *

                              * * @param b Right-hand side of the equation A × X = B. * @return a Vector X that minimizes the two norm of A × X - B. * * @throws DimensionMismatchException if the matrices dimensions do not match. * @throws SingularMatrixException if the decomposed matrix is singular. */ public RealVector solve(final RealVector b) { if (!isNonSingular()) { throw new SingularMatrixException(); } final int m = realEigenvalues.length; if (b.getDimension() != m) { throw new DimensionMismatchException(b.getDimension(), m); } final double[] bp = new double[m]; for (int i = 0; i < m; ++i) { final ArrayRealVector v = eigenvectors[i]; final double[] vData = v.getDataRef(); final double s = v.dotProduct(b) / realEigenvalues[i]; for (int j = 0; j < m; ++j) { bp[j] += s * vData[j]; } } return new ArrayRealVector(bp, false); } /** {@inheritDoc} */ public RealMatrix solve(RealMatrix b) { if (!isNonSingular()) { throw new SingularMatrixException(); } final int m = realEigenvalues.length; if (b.getRowDimension() != m) { throw new DimensionMismatchException(b.getRowDimension(), m); } final int nColB = b.getColumnDimension(); final double[][] bp = new double[m][nColB]; final double[] tmpCol = new double[m]; for (int k = 0; k < nColB; ++k) { for (int i = 0; i < m; ++i) { tmpCol[i] = b.getEntry(i, k); bp[i][k] = 0; } for (int i = 0; i < m; ++i) { final ArrayRealVector v = eigenvectors[i]; final double[] vData = v.getDataRef(); double s = 0; for (int j = 0; j < m; ++j) { s += v.getEntry(j) * tmpCol[j]; } s /= realEigenvalues[i]; for (int j = 0; j < m; ++j) { bp[j][k] += s * vData[j]; } } } return new Array2DRowRealMatrix(bp, false); } /** * Checks whether the decomposed matrix is non-singular. * * @return true if the decomposed matrix is non-singular. */ public boolean isNonSingular() { for (int i = 0; i < realEigenvalues.length; ++i) { if (realEigenvalues[i] == 0 && imagEigenvalues[i] == 0) { return false; } } return true; } /** * Get the inverse of the decomposed matrix. * * @return the inverse matrix. * @throws SingularMatrixException if the decomposed matrix is singular. */ public RealMatrix getInverse() { if (!isNonSingular()) { throw new SingularMatrixException(); } final int m = realEigenvalues.length; final double[][] invData = new double[m][m]; for (int i = 0; i < m; ++i) { final double[] invI = invData[i]; for (int j = 0; j < m; ++j) { double invIJ = 0; for (int k = 0; k < m; ++k) { final double[] vK = eigenvectors[k].getDataRef(); invIJ += vK[i] * vK[j] / realEigenvalues[k]; } invI[j] = invIJ; } } return MatrixUtils.createRealMatrix(invData); } } /** * Transforms the matrix to tridiagonal form. * * @param matrix Matrix to transform. */ private void transformToTridiagonal(final RealMatrix matrix) { // transform the matrix to tridiagonal transformer = new TriDiagonalTransformer(matrix); main = transformer.getMainDiagonalRef(); secondary = transformer.getSecondaryDiagonalRef(); } /** * Find eigenvalues and eigenvectors (Dubrulle et al., 1971) * * @param householderMatrix Householder matrix of the transformation * to tridiagonal form. */ private void findEigenVectors(final double[][] householderMatrix) { final double[][]z = householderMatrix.clone(); final int n = main.length; realEigenvalues = new double[n]; imagEigenvalues = new double[n]; final double[] e = new double[n]; for (int i = 0; i < n - 1; i++) { realEigenvalues[i] = main[i]; e[i] = secondary[i]; } realEigenvalues[n - 1] = main[n - 1]; e[n - 1] = 0; // Determine the largest main and secondary value in absolute term. double maxAbsoluteValue = 0; for (int i = 0; i < n; i++) { if (FastMath.abs(realEigenvalues[i]) > maxAbsoluteValue) { maxAbsoluteValue = FastMath.abs(realEigenvalues[i]); } if (FastMath.abs(e[i]) > maxAbsoluteValue) { maxAbsoluteValue = FastMath.abs(e[i]); } } // Make null any main and secondary value too small to be significant if (maxAbsoluteValue != 0) { for (int i=0; i < n; i++) { if (FastMath.abs(realEigenvalues[i]) <= Precision.EPSILON * maxAbsoluteValue) { realEigenvalues[i] = 0; } if (FastMath.abs(e[i]) <= Precision.EPSILON * maxAbsoluteValue) { e[i]=0; } } } for (int j = 0; j < n; j++) { int its = 0; int m; do { for (m = j; m < n - 1; m++) { double delta = FastMath.abs(realEigenvalues[m]) + FastMath.abs(realEigenvalues[m + 1]); if (FastMath.abs(e[m]) + delta == delta) { break; } } if (m != j) { if (its == maxIter) { throw new MaxCountExceededException(LocalizedFormats.CONVERGENCE_FAILED, maxIter); } its++; double q = (realEigenvalues[j + 1] - realEigenvalues[j]) / (2 * e[j]); double t = FastMath.sqrt(1 + q * q); if (q < 0.0) { q = realEigenvalues[m] - realEigenvalues[j] + e[j] / (q - t); } else { q = realEigenvalues[m] - realEigenvalues[j] + e[j] / (q + t); } double u = 0.0; double s = 1.0; double c = 1.0; int i; for (i = m - 1; i >= j; i--) { double p = s * e[i]; double h = c * e[i]; if (FastMath.abs(p) >= FastMath.abs(q)) { c = q / p; t = FastMath.sqrt(c * c + 1.0); e[i + 1] = p * t; s = 1.0 / t; c = c * s; } else { s = p / q; t = FastMath.sqrt(s * s + 1.0); e[i + 1] = q * t; c = 1.0 / t; s = s * c; } if (e[i + 1] == 0.0) { realEigenvalues[i + 1] -= u; e[m] = 0.0; break; } q = realEigenvalues[i + 1] - u; t = (realEigenvalues[i] - q) * s + 2.0 * c * h; u = s * t; realEigenvalues[i + 1] = q + u; q = c * t - h; for (int ia = 0; ia < n; ia++) { p = z[ia][i + 1]; z[ia][i + 1] = s * z[ia][i] + c * p; z[ia][i] = c * z[ia][i] - s * p; } } if (t == 0.0 && i >= j) { continue; } realEigenvalues[j] -= u; e[j] = q; e[m] = 0.0; } } while (m != j); } //Sort the eigen values (and vectors) in increase order for (int i = 0; i < n; i++) { int k = i; double p = realEigenvalues[i]; for (int j = i + 1; j < n; j++) { if (realEigenvalues[j] > p) { k = j; p = realEigenvalues[j]; } } if (k != i) { realEigenvalues[k] = realEigenvalues[i]; realEigenvalues[i] = p; for (int j = 0; j < n; j++) { p = z[j][i]; z[j][i] = z[j][k]; z[j][k] = p; } } } // Determine the largest eigen value in absolute term. maxAbsoluteValue = 0; for (int i = 0; i < n; i++) { if (FastMath.abs(realEigenvalues[i]) > maxAbsoluteValue) { maxAbsoluteValue=FastMath.abs(realEigenvalues[i]); } } // Make null any eigen value too small to be significant if (maxAbsoluteValue != 0.0) { for (int i=0; i < n; i++) { if (FastMath.abs(realEigenvalues[i]) < Precision.EPSILON * maxAbsoluteValue) { realEigenvalues[i] = 0; } } } eigenvectors = new ArrayRealVector[n]; final double[] tmp = new double[n]; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { tmp[j] = z[j][i]; } eigenvectors[i] = new ArrayRealVector(tmp); } } /** * Transforms the matrix to Schur form and calculates the eigenvalues. * * @param matrix Matrix to transform. * @return the {@link SchurTransformer Shur transform} for this matrix */ private SchurTransformer transformToSchur(final RealMatrix matrix) { final SchurTransformer schurTransform = new SchurTransformer(matrix); final double[][] matT = schurTransform.getT().getData(); realEigenvalues = new double[matT.length]; imagEigenvalues = new double[matT.length]; for (int i = 0; i < realEigenvalues.length; i++) { if (i == (realEigenvalues.length - 1) || Precision.equals(matT[i + 1][i], 0.0, EPSILON)) { realEigenvalues[i] = matT[i][i]; } else { final double x = matT[i + 1][i + 1]; final double p = 0.5 * (matT[i][i] - x); final double z = FastMath.sqrt(FastMath.abs(p * p + matT[i + 1][i] * matT[i][i + 1])); realEigenvalues[i] = x + p; imagEigenvalues[i] = z; realEigenvalues[i + 1] = x + p; imagEigenvalues[i + 1] = -z; i++; } } return schurTransform; } /** * Performs a division of two complex numbers. * * @param xr real part of the first number * @param xi imaginary part of the first number * @param yr real part of the second number * @param yi imaginary part of the second number * @return result of the complex division */ private Complex cdiv(final double xr, final double xi, final double yr, final double yi) { return new Complex(xr, xi).divide(new Complex(yr, yi)); } /** * Find eigenvectors from a matrix transformed to Schur form. * * @param schur the schur transformation of the matrix * @throws MathArithmeticException if the Schur form has a norm of zero */ private void findEigenVectorsFromSchur(final SchurTransformer schur) throws MathArithmeticException { final double[][] matrixT = schur.getT().getData(); final double[][] matrixP = schur.getP().getData(); final int n = matrixT.length; // compute matrix norm double norm = 0.0; for (int i = 0; i < n; i++) { for (int j = FastMath.max(i - 1, 0); j < n; j++) { norm = norm + FastMath.abs(matrixT[i][j]); } } // we can not handle a matrix with zero norm if (Precision.equals(norm, 0.0, EPSILON)) { throw new MathArithmeticException(LocalizedFormats.ZERO_NORM); } // Backsubstitute to find vectors of upper triangular form double r = 0.0; double s = 0.0; double z = 0.0; for (int idx = n - 1; idx >= 0; idx--) { double p = realEigenvalues[idx]; double q = imagEigenvalues[idx]; if (Precision.equals(q, 0.0)) { // Real vector int l = idx; matrixT[idx][idx] = 1.0; for (int i = idx - 1; i >= 0; i--) { double w = matrixT[i][i] - p; r = 0.0; for (int j = l; j <= idx; j++) { r = r + matrixT[i][j] * matrixT[j][idx]; } if (Precision.compareTo(imagEigenvalues[i], 0.0, EPSILON) < 0.0) { z = w; s = r; } else { l = i; if (Precision.equals(imagEigenvalues[i], 0.0)) { if (w != 0.0) { matrixT[i][idx] = -r / w; } else { matrixT[i][idx] = -r / (Precision.EPSILON * norm); } } else { // Solve real equations double x = matrixT[i][i + 1]; double y = matrixT[i + 1][i]; q = (realEigenvalues[i] - p) * (realEigenvalues[i] - p) + imagEigenvalues[i] * imagEigenvalues[i]; double t = (x * s - z * r) / q; matrixT[i][idx] = t; if (FastMath.abs(x) > FastMath.abs(z)) { matrixT[i + 1][idx] = (-r - w * t) / x; } else { matrixT[i + 1][idx] = (-s - y * t) / z; } } // Overflow control double t = FastMath.abs(matrixT[i][idx]); if ((Precision.EPSILON * t) * t > 1) { for (int j = i; j <= idx; j++) { matrixT[j][idx] = matrixT[j][idx] / t; } } } } } else if (q < 0.0) { // Complex vector int l = idx - 1; // Last vector component imaginary so matrix is triangular if (FastMath.abs(matrixT[idx][idx - 1]) > FastMath.abs(matrixT[idx - 1][idx])) { matrixT[idx - 1][idx - 1] = q / matrixT[idx][idx - 1]; matrixT[idx - 1][idx] = -(matrixT[idx][idx] - p) / matrixT[idx][idx - 1]; } else { final Complex result = cdiv(0.0, -matrixT[idx - 1][idx], matrixT[idx - 1][idx - 1] - p, q); matrixT[idx - 1][idx - 1] = result.getReal(); matrixT[idx - 1][idx] = result.getImaginary(); } matrixT[idx][idx - 1] = 0.0; matrixT[idx][idx] = 1.0; for (int i = idx - 2; i >= 0; i--) { double ra = 0.0; double sa = 0.0; for (int j = l; j <= idx; j++) { ra = ra + matrixT[i][j] * matrixT[j][idx - 1]; sa = sa + matrixT[i][j] * matrixT[j][idx]; } double w = matrixT[i][i] - p; if (Precision.compareTo(imagEigenvalues[i], 0.0, EPSILON) < 0.0) { z = w; r = ra; s = sa; } else { l = i; if (Precision.equals(imagEigenvalues[i], 0.0)) { final Complex c = cdiv(-ra, -sa, w, q); matrixT[i][idx - 1] = c.getReal(); matrixT[i][idx] = c.getImaginary(); } else { // Solve complex equations double x = matrixT[i][i + 1]; double y = matrixT[i + 1][i]; double vr = (realEigenvalues[i] - p) * (realEigenvalues[i] - p) + imagEigenvalues[i] * imagEigenvalues[i] - q * q; final double vi = (realEigenvalues[i] - p) * 2.0 * q; if (Precision.equals(vr, 0.0) && Precision.equals(vi, 0.0)) { vr = Precision.EPSILON * norm * (FastMath.abs(w) + FastMath.abs(q) + FastMath.abs(x) + FastMath.abs(y) + FastMath.abs(z)); } final Complex c = cdiv(x * r - z * ra + q * sa, x * s - z * sa - q * ra, vr, vi); matrixT[i][idx - 1] = c.getReal(); matrixT[i][idx] = c.getImaginary(); if (FastMath.abs(x) > (FastMath.abs(z) + FastMath.abs(q))) { matrixT[i + 1][idx - 1] = (-ra - w * matrixT[i][idx - 1] + q * matrixT[i][idx]) / x; matrixT[i + 1][idx] = (-sa - w * matrixT[i][idx] - q * matrixT[i][idx - 1]) / x; } else { final Complex c2 = cdiv(-r - y * matrixT[i][idx - 1], -s - y * matrixT[i][idx], z, q); matrixT[i + 1][idx - 1] = c2.getReal(); matrixT[i + 1][idx] = c2.getImaginary(); } } // Overflow control double t = FastMath.max(FastMath.abs(matrixT[i][idx - 1]), FastMath.abs(matrixT[i][idx])); if ((Precision.EPSILON * t) * t > 1) { for (int j = i; j <= idx; j++) { matrixT[j][idx - 1] = matrixT[j][idx - 1] / t; matrixT[j][idx] = matrixT[j][idx] / t; } } } } } } // Vectors of isolated roots for (int i = 0; i < n; i++) { if (i < 0 | i > n - 1) { for (int j = i; j < n; j++) { matrixP[i][j] = matrixT[i][j]; } } } // Back transformation to get eigenvectors of original matrix for (int j = n - 1; j >= 0; j--) { for (int i = 0; i <= n - 1; i++) { z = 0.0; for (int k = 0; k <= FastMath.min(j, n - 1); k++) { z = z + matrixP[i][k] * matrixT[k][j]; } matrixP[i][j] = z; } } eigenvectors = new ArrayRealVector[n]; final double[] tmp = new double[n]; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { tmp[j] = matrixP[j][i]; } eigenvectors[i] = new ArrayRealVector(tmp); } } } ././@LongLink100644 0 0 156 12126630647 10264 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/linear/NonPositiveDefiniteOperatorException.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/linear/NonPositiveDefiniteOperatorExcep100644 1750 1750 3577 12126627717 32366 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.linear; import org.apache.commons.math3.exception.MathIllegalArgumentException; import org.apache.commons.math3.exception.util.LocalizedFormats; /** * Exception to be thrown when a symmetric, definite positive * {@link RealLinearOperator} is expected. * Since the coefficients of the matrix are not accessible, the most * general definition is used to check that {@code A} is not positive * definite, i.e. there exists {@code x} such that {@code x' A x <= 0}. * In the terminology of this exception, {@code A} is the "offending" * linear operator and {@code x} the "offending" vector. * * @version $Id: NonPositiveDefiniteOperatorException.java 1416643 2012-12-03 19:37:14Z tn $ * @since 3.0 */ public class NonPositiveDefiniteOperatorException extends MathIllegalArgumentException { /** Serializable version Id. */ private static final long serialVersionUID = 917034489420549847L; /** Creates a new instance of this class. */ public NonPositiveDefiniteOperatorException() { super(LocalizedFormats.NON_POSITIVE_DEFINITE_OPERATOR); } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/linear/HessenbergTransformer.java100644 1750 1750 20077 12126627717 31202 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.linear; import org.apache.commons.math3.util.FastMath; import org.apache.commons.math3.util.Precision; /** * Class transforming a general real matrix to Hessenberg form. *

                              A m × m matrix A can be written as the product of three matrices: A = P * × H × PT with P an orthogonal matrix and H a Hessenberg * matrix. Both P and H are m × m matrices.

                              *

                              Transformation to Hessenberg form is often not a goal by itself, but it is an * intermediate step in more general decomposition algorithms like * {@link EigenDecomposition eigen decomposition}. This class is therefore * intended for internal use by the library and is not public. As a consequence * of this explicitly limited scope, many methods directly returns references to * internal arrays, not copies.

                              *

                              This class is based on the method orthes in class EigenvalueDecomposition * from the JAMA library.

                              * * @see MathWorld * @see Householder Transformations * @version $Id: HessenbergTransformer.java 1334644 2012-05-06 14:33:32Z tn $ * @since 3.1 */ class HessenbergTransformer { /** Householder vectors. */ private final double householderVectors[][]; /** Temporary storage vector. */ private final double ort[]; /** Cached value of P. */ private RealMatrix cachedP; /** Cached value of Pt. */ private RealMatrix cachedPt; /** Cached value of H. */ private RealMatrix cachedH; /** * Build the transformation to Hessenberg form of a general matrix. * * @param matrix matrix to transform * @throws NonSquareMatrixException if the matrix is not square */ public HessenbergTransformer(final RealMatrix matrix) { if (!matrix.isSquare()) { throw new NonSquareMatrixException(matrix.getRowDimension(), matrix.getColumnDimension()); } final int m = matrix.getRowDimension(); householderVectors = matrix.getData(); ort = new double[m]; cachedP = null; cachedPt = null; cachedH = null; // transform matrix transform(); } /** * Returns the matrix P of the transform. *

                              P is an orthogonal matrix, i.e. its inverse is also its transpose.

                              * * @return the P matrix */ public RealMatrix getP() { if (cachedP == null) { final int n = householderVectors.length; final int high = n - 1; final double[][] pa = new double[n][n]; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { pa[i][j] = (i == j) ? 1 : 0; } } for (int m = high - 1; m >= 1; m--) { if (householderVectors[m][m - 1] != 0.0) { for (int i = m + 1; i <= high; i++) { ort[i] = householderVectors[i][m - 1]; } for (int j = m; j <= high; j++) { double g = 0.0; for (int i = m; i <= high; i++) { g += ort[i] * pa[i][j]; } // Double division avoids possible underflow g = (g / ort[m]) / householderVectors[m][m - 1]; for (int i = m; i <= high; i++) { pa[i][j] += g * ort[i]; } } } } cachedP = MatrixUtils.createRealMatrix(pa); } return cachedP; } /** * Returns the transpose of the matrix P of the transform. *

                              P is an orthogonal matrix, i.e. its inverse is also its transpose.

                              * * @return the transpose of the P matrix */ public RealMatrix getPT() { if (cachedPt == null) { cachedPt = getP().transpose(); } // return the cached matrix return cachedPt; } /** * Returns the Hessenberg matrix H of the transform. * * @return the H matrix */ public RealMatrix getH() { if (cachedH == null) { final int m = householderVectors.length; final double[][] h = new double[m][m]; for (int i = 0; i < m; ++i) { if (i > 0) { // copy the entry of the lower sub-diagonal h[i][i - 1] = householderVectors[i][i - 1]; } // copy upper triangular part of the matrix for (int j = i; j < m; ++j) { h[i][j] = householderVectors[i][j]; } } cachedH = MatrixUtils.createRealMatrix(h); } // return the cached matrix return cachedH; } /** * Get the Householder vectors of the transform. *

                              Note that since this class is only intended for internal use, it returns * directly a reference to its internal arrays, not a copy.

                              * * @return the main diagonal elements of the B matrix */ double[][] getHouseholderVectorsRef() { return householderVectors; } /** * Transform original matrix to Hessenberg form. *

                              Transformation is done using Householder transforms.

                              */ private void transform() { final int n = householderVectors.length; final int high = n - 1; for (int m = 1; m <= high - 1; m++) { // Scale column. double scale = 0; for (int i = m; i <= high; i++) { scale += FastMath.abs(householderVectors[i][m - 1]); } if (!Precision.equals(scale, 0)) { // Compute Householder transformation. double h = 0; for (int i = high; i >= m; i--) { ort[i] = householderVectors[i][m - 1] / scale; h += ort[i] * ort[i]; } final double g = (ort[m] > 0) ? -FastMath.sqrt(h) : FastMath.sqrt(h); h = h - ort[m] * g; ort[m] = ort[m] - g; // Apply Householder similarity transformation // H = (I - u*u' / h) * H * (I - u*u' / h) for (int j = m; j < n; j++) { double f = 0; for (int i = high; i >= m; i--) { f += ort[i] * householderVectors[i][j]; } f = f / h; for (int i = m; i <= high; i++) { householderVectors[i][j] -= f * ort[i]; } } for (int i = 0; i <= high; i++) { double f = 0; for (int j = high; j >= m; j--) { f += ort[j] * householderVectors[i][j]; } f = f / h; for (int j = m; j <= high; j++) { householderVectors[i][j] -= f * ort[j]; } } ort[m] = scale * ort[m]; householderVectors[m][m - 1] = scale * g; } } } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/linear/AnyMatrix.java100644 1750 1750 2632 12126627717 26563 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.linear; /** * Interface defining very basic matrix operations. * @version $Id: AnyMatrix.java 1416643 2012-12-03 19:37:14Z tn $ * @since 2.0 */ public interface AnyMatrix { /** * Is this a square matrix? * @return true if the matrix is square (rowDimension = columnDimension) */ boolean isSquare(); /** * Returns the number of rows in the matrix. * * @return rowDimension */ int getRowDimension(); /** * Returns the number of columns in the matrix. * * @return columnDimension */ int getColumnDimension(); } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/linear/RealVectorChangingVisitor.java100644 1750 1750 4416 12126627717 31736 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.linear; /** * This interface defines a visitor for the entries of a vector. Visitors * implementing this interface may alter the entries of the vector being * visited. * * @version $Id: RealVectorChangingVisitor.java 1416643 2012-12-03 19:37:14Z tn $ * @since 3.1 */ public interface RealVectorChangingVisitor { /** * Start visiting a vector. This method is called once, before any entry * of the vector is visited. * * @param dimension the size of the vector * @param start the index of the first entry to be visited * @param end the index of the last entry to be visited (inclusive) */ void start(int dimension, int start, int end); /** * Visit one entry of the vector. * * @param index the index of the entry being visited * @param value the value of the entry being visited * @return the new value of the entry being visited */ double visit(int index, double value); /** * End visiting a vector. This method is called once, after all entries of * the vector have been visited. * * @return the value returned by * {@link RealVector#walkInDefaultOrder(RealVectorChangingVisitor)}, * {@link RealVector#walkInDefaultOrder(RealVectorChangingVisitor, int, int)}, * {@link RealVector#walkInOptimizedOrder(RealVectorChangingVisitor)} * or * {@link RealVector#walkInOptimizedOrder(RealVectorChangingVisitor, int, int)} */ double end(); } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/linear/FieldVector.java100644 1750 1750 26142 12126627717 27077 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.linear; import org.apache.commons.math3.Field; import org.apache.commons.math3.FieldElement; import org.apache.commons.math3.exception.DimensionMismatchException; import org.apache.commons.math3.exception.MathArithmeticException; import org.apache.commons.math3.exception.NotPositiveException; import org.apache.commons.math3.exception.NullArgumentException; import org.apache.commons.math3.exception.OutOfRangeException; /** * Interface defining a field-valued vector with basic algebraic operations. *

                              * vector element indexing is 0-based -- e.g., getEntry(0) * returns the first element of the vector. *

                              *

                              * The various mapXxx and mapXxxToSelf methods operate * on vectors element-wise, i.e. they perform the same operation (adding a scalar, * applying a function ...) on each element in turn. The mapXxx * versions create a new vector to hold the result and do not change the instance. * The mapXxxToSelf versions use the instance itself to store the * results, so the instance is changed by these methods. In both cases, the result * vector is returned by the methods, this allows to use the fluent API * style, like this: *

                              *
                               *   RealVector result = v.mapAddToSelf(3.0).mapTanToSelf().mapSquareToSelf();
                               * 
                              *

                              * Note that as almost all operations on {@link FieldElement} throw {@link * NullArgumentException} when operating on a null element, it is the responsibility * of FieldVector implementations to make sure no null elements * are inserted into the vector. This must be done in all constructors and * all setters. *

                              * * @param the type of the field elements * @version $Id: FieldVector.java 1455233 2013-03-11 17:00:41Z luc $ * @since 2.0 */ public interface FieldVector> { /** * Get the type of field elements of the vector. * @return type of field elements of the vector */ Field getField(); /** * Returns a (deep) copy of this. * @return vector copy */ FieldVector copy(); /** * Compute the sum of {@code this} and {@code v}. * @param v vector to be added * @return {@code this + v} * @throws DimensionMismatchException if {@code v} is not the same size as {@code this} */ FieldVector add(FieldVector v) throws DimensionMismatchException; /** * Compute {@code this} minus {@code v}. * @param v vector to be subtracted * @return {@code this - v} * @throws DimensionMismatchException if {@code v} is not the same size as {@code this} */ FieldVector subtract(FieldVector v) throws DimensionMismatchException; /** * Map an addition operation to each entry. * @param d value to be added to each entry * @return {@code this + d} * @throws NullArgumentException if {@code d} is {@code null}. */ FieldVector mapAdd(T d) throws NullArgumentException; /** * Map an addition operation to each entry. *

                              The instance is changed by this method.

                              * @param d value to be added to each entry * @return for convenience, return {@code this} * @throws NullArgumentException if {@code d} is {@code null}. */ FieldVector mapAddToSelf(T d) throws NullArgumentException; /** * Map a subtraction operation to each entry. * @param d value to be subtracted to each entry * @return {@code this - d} * @throws NullArgumentException if {@code d} is {@code null} */ FieldVector mapSubtract(T d) throws NullArgumentException; /** * Map a subtraction operation to each entry. *

                              The instance is changed by this method.

                              * @param d value to be subtracted to each entry * @return for convenience, return {@code this} * @throws NullArgumentException if {@code d} is {@code null} */ FieldVector mapSubtractToSelf(T d) throws NullArgumentException; /** * Map a multiplication operation to each entry. * @param d value to multiply all entries by * @return {@code this * d} * @throws NullArgumentException if {@code d} is {@code null}. */ FieldVector mapMultiply(T d) throws NullArgumentException; /** * Map a multiplication operation to each entry. *

                              The instance is changed by this method.

                              * @param d value to multiply all entries by * @return for convenience, return {@code this} * @throws NullArgumentException if {@code d} is {@code null}. */ FieldVector mapMultiplyToSelf(T d) throws NullArgumentException; /** * Map a division operation to each entry. * @param d value to divide all entries by * @return {@code this / d} * @throws NullArgumentException if {@code d} is {@code null}. * @throws MathArithmeticException if {@code d} is zero. */ FieldVector mapDivide(T d) throws NullArgumentException, MathArithmeticException; /** * Map a division operation to each entry. *

                              The instance is changed by this method.

                              * @param d value to divide all entries by * @return for convenience, return {@code this} * @throws NullArgumentException if {@code d} is {@code null}. * @throws MathArithmeticException if {@code d} is zero. */ FieldVector mapDivideToSelf(T d) throws NullArgumentException, MathArithmeticException; /** * Map the 1/x function to each entry. * @return a vector containing the result of applying the function to each entry. * @throws MathArithmeticException if one of the entries is zero. */ FieldVector mapInv() throws MathArithmeticException; /** * Map the 1/x function to each entry. *

                              The instance is changed by this method.

                              * @return for convenience, return {@code this} * @throws MathArithmeticException if one of the entries is zero. */ FieldVector mapInvToSelf() throws MathArithmeticException; /** * Element-by-element multiplication. * @param v vector by which instance elements must be multiplied * @return a vector containing {@code this[i] * v[i]} for all {@code i} * @throws DimensionMismatchException if {@code v} is not the same size as {@code this} */ FieldVector ebeMultiply(FieldVector v) throws DimensionMismatchException; /** * Element-by-element division. * @param v vector by which instance elements must be divided * @return a vector containing {@code this[i] / v[i]} for all {@code i} * @throws DimensionMismatchException if {@code v} is not the same size as {@code this} * @throws MathArithmeticException if one entry of {@code v} is zero. */ FieldVector ebeDivide(FieldVector v) throws DimensionMismatchException, MathArithmeticException; /** * Returns vector entries as a T array. * @return T array of entries * @deprecated as of 3.1, to be removed in 4.0. Please use the {@link #toArray()} method instead. */ @Deprecated T[] getData(); /** * Compute the dot product. * @param v vector with which dot product should be computed * @return the scalar dot product of {@code this} and {@code v} * @throws DimensionMismatchException if {@code v} is not the same size as {@code this} */ T dotProduct(FieldVector v) throws DimensionMismatchException; /** * Find the orthogonal projection of this vector onto another vector. * @param v vector onto which {@code this} must be projected * @return projection of {@code this} onto {@code v} * @throws DimensionMismatchException if {@code v} is not the same size as {@code this} * @throws MathArithmeticException if {@code v} is the null vector. */ FieldVector projection(FieldVector v) throws DimensionMismatchException, MathArithmeticException; /** * Compute the outer product. * @param v vector with which outer product should be computed * @return the matrix outer product between instance and v */ FieldMatrix outerProduct(FieldVector v); /** * Returns the entry in the specified index. * * @param index Index location of entry to be fetched. * @return the vector entry at {@code index}. * @throws OutOfRangeException if the index is not valid. * @see #setEntry(int, FieldElement) */ T getEntry(int index) throws OutOfRangeException; /** * Set a single element. * @param index element index. * @param value new value for the element. * @throws OutOfRangeException if the index is not valid. * @see #getEntry(int) */ void setEntry(int index, T value) throws OutOfRangeException; /** * Returns the size of the vector. * @return size */ int getDimension(); /** * Construct a vector by appending a vector to this vector. * @param v vector to append to this one. * @return a new vector */ FieldVector append(FieldVector v); /** * Construct a vector by appending a T to this vector. * @param d T to append. * @return a new vector */ FieldVector append(T d); /** * Get a subvector from consecutive elements. * @param index index of first element. * @param n number of elements to be retrieved. * @return a vector containing n elements. * @throws OutOfRangeException if the index is not valid. * @throws NotPositiveException if the number of elements if not positive. */ FieldVector getSubVector(int index, int n) throws OutOfRangeException, NotPositiveException; /** * Set a set of consecutive elements. * @param index index of first element to be set. * @param v vector containing the values to set. * @throws OutOfRangeException if the index is not valid. */ void setSubVector(int index, FieldVector v) throws OutOfRangeException; /** * Set all elements to a single value. * @param value single value to set for all elements */ void set(T value); /** * Convert the vector to a T array. *

                              The array is independent from vector data, it's elements * are copied.

                              * @return array containing a copy of vector elements */ T[] toArray(); } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/linear/SingularValueDecomposition.java100644 1750 1750 70613 12126627717 32211 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.linear; import org.apache.commons.math3.exception.NumberIsTooLargeException; import org.apache.commons.math3.exception.util.LocalizedFormats; import org.apache.commons.math3.util.FastMath; import org.apache.commons.math3.util.Precision; /** * Calculates the compact Singular Value Decomposition of a matrix. *

                              * The Singular Value Decomposition of matrix A is a set of three matrices: U, * Σ and V such that A = U × Σ × VT. Let A be * a m × n matrix, then U is a m × p orthogonal matrix, Σ is a * p × p diagonal matrix with positive or null elements, V is a p × * n orthogonal matrix (hence VT is also orthogonal) where * p=min(m,n). *

                              *

                              This class is similar to the class with similar name from the * JAMA library, with the * following changes:

                              *
                                *
                              • the {@code norm2} method which has been renamed as {@link #getNorm() * getNorm},
                              • *
                              • the {@code cond} method which has been renamed as {@link * #getConditionNumber() getConditionNumber},
                              • *
                              • the {@code rank} method which has been renamed as {@link #getRank() * getRank},
                              • *
                              • a {@link #getUT() getUT} method has been added,
                              • *
                              • a {@link #getVT() getVT} method has been added,
                              • *
                              • a {@link #getSolver() getSolver} method has been added,
                              • *
                              • a {@link #getCovariance(double) getCovariance} method has been added.
                              • *
                              * @see MathWorld * @see Wikipedia * @version $Id: SingularValueDecomposition.java 1456931 2013-03-15 12:34:35Z luc $ * @since 2.0 (changed to concrete class in 3.0) */ public class SingularValueDecomposition { /** Relative threshold for small singular values. */ private static final double EPS = 0x1.0p-52; /** Absolute threshold for small singular values. */ private static final double TINY = 0x1.0p-966; /** Computed singular values. */ private final double[] singularValues; /** max(row dimension, column dimension). */ private final int m; /** min(row dimension, column dimension). */ private final int n; /** Indicator for transposed matrix. */ private final boolean transposed; /** Cached value of U matrix. */ private final RealMatrix cachedU; /** Cached value of transposed U matrix. */ private RealMatrix cachedUt; /** Cached value of S (diagonal) matrix. */ private RealMatrix cachedS; /** Cached value of V matrix. */ private final RealMatrix cachedV; /** Cached value of transposed V matrix. */ private RealMatrix cachedVt; /** * Tolerance value for small singular values, calculated once we have * populated "singularValues". **/ private final double tol; /** * Calculates the compact Singular Value Decomposition of the given matrix. * * @param matrix Matrix to decompose. */ public SingularValueDecomposition(final RealMatrix matrix) { final double[][] A; // "m" is always the largest dimension. if (matrix.getRowDimension() < matrix.getColumnDimension()) { transposed = true; A = matrix.transpose().getData(); m = matrix.getColumnDimension(); n = matrix.getRowDimension(); } else { transposed = false; A = matrix.getData(); m = matrix.getRowDimension(); n = matrix.getColumnDimension(); } singularValues = new double[n]; final double[][] U = new double[m][n]; final double[][] V = new double[n][n]; final double[] e = new double[n]; final double[] work = new double[m]; // Reduce A to bidiagonal form, storing the diagonal elements // in s and the super-diagonal elements in e. final int nct = FastMath.min(m - 1, n); final int nrt = FastMath.max(0, n - 2); for (int k = 0; k < FastMath.max(nct, nrt); k++) { if (k < nct) { // Compute the transformation for the k-th column and // place the k-th diagonal in s[k]. // Compute 2-norm of k-th column without under/overflow. singularValues[k] = 0; for (int i = k; i < m; i++) { singularValues[k] = FastMath.hypot(singularValues[k], A[i][k]); } if (singularValues[k] != 0) { if (A[k][k] < 0) { singularValues[k] = -singularValues[k]; } for (int i = k; i < m; i++) { A[i][k] /= singularValues[k]; } A[k][k] += 1; } singularValues[k] = -singularValues[k]; } for (int j = k + 1; j < n; j++) { if (k < nct && singularValues[k] != 0) { // Apply the transformation. double t = 0; for (int i = k; i < m; i++) { t += A[i][k] * A[i][j]; } t = -t / A[k][k]; for (int i = k; i < m; i++) { A[i][j] += t * A[i][k]; } } // Place the k-th row of A into e for the // subsequent calculation of the row transformation. e[j] = A[k][j]; } if (k < nct) { // Place the transformation in U for subsequent back // multiplication. for (int i = k; i < m; i++) { U[i][k] = A[i][k]; } } if (k < nrt) { // Compute the k-th row transformation and place the // k-th super-diagonal in e[k]. // Compute 2-norm without under/overflow. e[k] = 0; for (int i = k + 1; i < n; i++) { e[k] = FastMath.hypot(e[k], e[i]); } if (e[k] != 0) { if (e[k + 1] < 0) { e[k] = -e[k]; } for (int i = k + 1; i < n; i++) { e[i] /= e[k]; } e[k + 1] += 1; } e[k] = -e[k]; if (k + 1 < m && e[k] != 0) { // Apply the transformation. for (int i = k + 1; i < m; i++) { work[i] = 0; } for (int j = k + 1; j < n; j++) { for (int i = k + 1; i < m; i++) { work[i] += e[j] * A[i][j]; } } for (int j = k + 1; j < n; j++) { final double t = -e[j] / e[k + 1]; for (int i = k + 1; i < m; i++) { A[i][j] += t * work[i]; } } } // Place the transformation in V for subsequent // back multiplication. for (int i = k + 1; i < n; i++) { V[i][k] = e[i]; } } } // Set up the final bidiagonal matrix or order p. int p = n; if (nct < n) { singularValues[nct] = A[nct][nct]; } if (m < p) { singularValues[p - 1] = 0; } if (nrt + 1 < p) { e[nrt] = A[nrt][p - 1]; } e[p - 1] = 0; // Generate U. for (int j = nct; j < n; j++) { for (int i = 0; i < m; i++) { U[i][j] = 0; } U[j][j] = 1; } for (int k = nct - 1; k >= 0; k--) { if (singularValues[k] != 0) { for (int j = k + 1; j < n; j++) { double t = 0; for (int i = k; i < m; i++) { t += U[i][k] * U[i][j]; } t = -t / U[k][k]; for (int i = k; i < m; i++) { U[i][j] += t * U[i][k]; } } for (int i = k; i < m; i++) { U[i][k] = -U[i][k]; } U[k][k] = 1 + U[k][k]; for (int i = 0; i < k - 1; i++) { U[i][k] = 0; } } else { for (int i = 0; i < m; i++) { U[i][k] = 0; } U[k][k] = 1; } } // Generate V. for (int k = n - 1; k >= 0; k--) { if (k < nrt && e[k] != 0) { for (int j = k + 1; j < n; j++) { double t = 0; for (int i = k + 1; i < n; i++) { t += V[i][k] * V[i][j]; } t = -t / V[k + 1][k]; for (int i = k + 1; i < n; i++) { V[i][j] += t * V[i][k]; } } } for (int i = 0; i < n; i++) { V[i][k] = 0; } V[k][k] = 1; } // Main iteration loop for the singular values. final int pp = p - 1; int iter = 0; while (p > 0) { int k; int kase; // Here is where a test for too many iterations would go. // This section of the program inspects for // negligible elements in the s and e arrays. On // completion the variables kase and k are set as follows. // kase = 1 if s(p) and e[k-1] are negligible and k

                              = 0; k--) { final double threshold = TINY + EPS * (FastMath.abs(singularValues[k]) + FastMath.abs(singularValues[k + 1])); // the following condition is written this way in order // to break out of the loop when NaN occurs, writing it // as "if (FastMath.abs(e[k]) <= threshold)" would loop // indefinitely in case of NaNs because comparison on NaNs // always return false, regardless of what is checked // see issue MATH-947 if (!(FastMath.abs(e[k]) > threshold)) { e[k] = 0; break; } } if (k == p - 2) { kase = 4; } else { int ks; for (ks = p - 1; ks >= k; ks--) { if (ks == k) { break; } final double t = (ks != p ? FastMath.abs(e[ks]) : 0) + (ks != k + 1 ? FastMath.abs(e[ks - 1]) : 0); if (FastMath.abs(singularValues[ks]) <= TINY + EPS * t) { singularValues[ks] = 0; break; } } if (ks == k) { kase = 3; } else if (ks == p - 1) { kase = 1; } else { kase = 2; k = ks; } } k++; // Perform the task indicated by kase. switch (kase) { // Deflate negligible s(p). case 1: { double f = e[p - 2]; e[p - 2] = 0; for (int j = p - 2; j >= k; j--) { double t = FastMath.hypot(singularValues[j], f); final double cs = singularValues[j] / t; final double sn = f / t; singularValues[j] = t; if (j != k) { f = -sn * e[j - 1]; e[j - 1] = cs * e[j - 1]; } for (int i = 0; i < n; i++) { t = cs * V[i][j] + sn * V[i][p - 1]; V[i][p - 1] = -sn * V[i][j] + cs * V[i][p - 1]; V[i][j] = t; } } } break; // Split at negligible s(k). case 2: { double f = e[k - 1]; e[k - 1] = 0; for (int j = k; j < p; j++) { double t = FastMath.hypot(singularValues[j], f); final double cs = singularValues[j] / t; final double sn = f / t; singularValues[j] = t; f = -sn * e[j]; e[j] = cs * e[j]; for (int i = 0; i < m; i++) { t = cs * U[i][j] + sn * U[i][k - 1]; U[i][k - 1] = -sn * U[i][j] + cs * U[i][k - 1]; U[i][j] = t; } } } break; // Perform one qr step. case 3: { // Calculate the shift. final double maxPm1Pm2 = FastMath.max(FastMath.abs(singularValues[p - 1]), FastMath.abs(singularValues[p - 2])); final double scale = FastMath.max(FastMath.max(FastMath.max(maxPm1Pm2, FastMath.abs(e[p - 2])), FastMath.abs(singularValues[k])), FastMath.abs(e[k])); final double sp = singularValues[p - 1] / scale; final double spm1 = singularValues[p - 2] / scale; final double epm1 = e[p - 2] / scale; final double sk = singularValues[k] / scale; final double ek = e[k] / scale; final double b = ((spm1 + sp) * (spm1 - sp) + epm1 * epm1) / 2.0; final double c = (sp * epm1) * (sp * epm1); double shift = 0; if (b != 0 || c != 0) { shift = FastMath.sqrt(b * b + c); if (b < 0) { shift = -shift; } shift = c / (b + shift); } double f = (sk + sp) * (sk - sp) + shift; double g = sk * ek; // Chase zeros. for (int j = k; j < p - 1; j++) { double t = FastMath.hypot(f, g); double cs = f / t; double sn = g / t; if (j != k) { e[j - 1] = t; } f = cs * singularValues[j] + sn * e[j]; e[j] = cs * e[j] - sn * singularValues[j]; g = sn * singularValues[j + 1]; singularValues[j + 1] = cs * singularValues[j + 1]; for (int i = 0; i < n; i++) { t = cs * V[i][j] + sn * V[i][j + 1]; V[i][j + 1] = -sn * V[i][j] + cs * V[i][j + 1]; V[i][j] = t; } t = FastMath.hypot(f, g); cs = f / t; sn = g / t; singularValues[j] = t; f = cs * e[j] + sn * singularValues[j + 1]; singularValues[j + 1] = -sn * e[j] + cs * singularValues[j + 1]; g = sn * e[j + 1]; e[j + 1] = cs * e[j + 1]; if (j < m - 1) { for (int i = 0; i < m; i++) { t = cs * U[i][j] + sn * U[i][j + 1]; U[i][j + 1] = -sn * U[i][j] + cs * U[i][j + 1]; U[i][j] = t; } } } e[p - 2] = f; iter = iter + 1; } break; // Convergence. default: { // Make the singular values positive. if (singularValues[k] <= 0) { singularValues[k] = singularValues[k] < 0 ? -singularValues[k] : 0; for (int i = 0; i <= pp; i++) { V[i][k] = -V[i][k]; } } // Order the singular values. while (k < pp) { if (singularValues[k] >= singularValues[k + 1]) { break; } double t = singularValues[k]; singularValues[k] = singularValues[k + 1]; singularValues[k + 1] = t; if (k < n - 1) { for (int i = 0; i < n; i++) { t = V[i][k + 1]; V[i][k + 1] = V[i][k]; V[i][k] = t; } } if (k < m - 1) { for (int i = 0; i < m; i++) { t = U[i][k + 1]; U[i][k + 1] = U[i][k]; U[i][k] = t; } } k++; } iter = 0; p--; } break; } } // Set the small value tolerance used to calculate rank and pseudo-inverse tol = FastMath.max(m * singularValues[0] * EPS, FastMath.sqrt(Precision.SAFE_MIN)); if (!transposed) { cachedU = MatrixUtils.createRealMatrix(U); cachedV = MatrixUtils.createRealMatrix(V); } else { cachedU = MatrixUtils.createRealMatrix(V); cachedV = MatrixUtils.createRealMatrix(U); } } /** * Returns the matrix U of the decomposition. *

                              U is an orthogonal matrix, i.e. its transpose is also its inverse.

                              * @return the U matrix * @see #getUT() */ public RealMatrix getU() { // return the cached matrix return cachedU; } /** * Returns the transpose of the matrix U of the decomposition. *

                              U is an orthogonal matrix, i.e. its transpose is also its inverse.

                              * @return the U matrix (or null if decomposed matrix is singular) * @see #getU() */ public RealMatrix getUT() { if (cachedUt == null) { cachedUt = getU().transpose(); } // return the cached matrix return cachedUt; } /** * Returns the diagonal matrix Σ of the decomposition. *

                              Σ is a diagonal matrix. The singular values are provided in * non-increasing order, for compatibility with Jama.

                              * @return the Σ matrix */ public RealMatrix getS() { if (cachedS == null) { // cache the matrix for subsequent calls cachedS = MatrixUtils.createRealDiagonalMatrix(singularValues); } return cachedS; } /** * Returns the diagonal elements of the matrix Σ of the decomposition. *

                              The singular values are provided in non-increasing order, for * compatibility with Jama.

                              * @return the diagonal elements of the Σ matrix */ public double[] getSingularValues() { return singularValues.clone(); } /** * Returns the matrix V of the decomposition. *

                              V is an orthogonal matrix, i.e. its transpose is also its inverse.

                              * @return the V matrix (or null if decomposed matrix is singular) * @see #getVT() */ public RealMatrix getV() { // return the cached matrix return cachedV; } /** * Returns the transpose of the matrix V of the decomposition. *

                              V is an orthogonal matrix, i.e. its transpose is also its inverse.

                              * @return the V matrix (or null if decomposed matrix is singular) * @see #getV() */ public RealMatrix getVT() { if (cachedVt == null) { cachedVt = getV().transpose(); } // return the cached matrix return cachedVt; } /** * Returns the n × n covariance matrix. *

                              The covariance matrix is V × J × VT * where J is the diagonal matrix of the inverse of the squares of * the singular values.

                              * @param minSingularValue value below which singular values are ignored * (a 0 or negative value implies all singular value will be used) * @return covariance matrix * @exception IllegalArgumentException if minSingularValue is larger than * the largest singular value, meaning all singular values are ignored */ public RealMatrix getCovariance(final double minSingularValue) { // get the number of singular values to consider final int p = singularValues.length; int dimension = 0; while (dimension < p && singularValues[dimension] >= minSingularValue) { ++dimension; } if (dimension == 0) { throw new NumberIsTooLargeException(LocalizedFormats.TOO_LARGE_CUTOFF_SINGULAR_VALUE, minSingularValue, singularValues[0], true); } final double[][] data = new double[dimension][p]; getVT().walkInOptimizedOrder(new DefaultRealMatrixPreservingVisitor() { /** {@inheritDoc} */ @Override public void visit(final int row, final int column, final double value) { data[row][column] = value / singularValues[row]; } }, 0, dimension - 1, 0, p - 1); RealMatrix jv = new Array2DRowRealMatrix(data, false); return jv.transpose().multiply(jv); } /** * Returns the L2 norm of the matrix. *

                              The L2 norm is max(|A × u|2 / * |u|2), where |.|2 denotes the vectorial 2-norm * (i.e. the traditional euclidian norm).

                              * @return norm */ public double getNorm() { return singularValues[0]; } /** * Return the condition number of the matrix. * @return condition number of the matrix */ public double getConditionNumber() { return singularValues[0] / singularValues[n - 1]; } /** * Computes the inverse of the condition number. * In cases of rank deficiency, the {@link #getConditionNumber() condition * number} will become undefined. * * @return the inverse of the condition number. */ public double getInverseConditionNumber() { return singularValues[n - 1] / singularValues[0]; } /** * Return the effective numerical matrix rank. *

                              The effective numerical rank is the number of non-negligible * singular values. The threshold used to identify non-negligible * terms is max(m,n) × ulp(s1) where ulp(s1) * is the least significant bit of the largest singular value.

                              * @return effective numerical matrix rank */ public int getRank() { int r = 0; for (int i = 0; i < singularValues.length; i++) { if (singularValues[i] > tol) { r++; } } return r; } /** * Get a solver for finding the A × X = B solution in least square sense. * @return a solver */ public DecompositionSolver getSolver() { return new Solver(singularValues, getUT(), getV(), getRank() == m, tol); } /** Specialized solver. */ private static class Solver implements DecompositionSolver { /** Pseudo-inverse of the initial matrix. */ private final RealMatrix pseudoInverse; /** Singularity indicator. */ private boolean nonSingular; /** * Build a solver from decomposed matrix. * * @param singularValues Singular values. * @param uT UT matrix of the decomposition. * @param v V matrix of the decomposition. * @param nonSingular Singularity indicator. * @param tol tolerance for singular values */ private Solver(final double[] singularValues, final RealMatrix uT, final RealMatrix v, final boolean nonSingular, final double tol) { final double[][] suT = uT.getData(); for (int i = 0; i < singularValues.length; ++i) { final double a; if (singularValues[i] > tol) { a = 1 / singularValues[i]; } else { a = 0; } final double[] suTi = suT[i]; for (int j = 0; j < suTi.length; ++j) { suTi[j] *= a; } } pseudoInverse = v.multiply(new Array2DRowRealMatrix(suT, false)); this.nonSingular = nonSingular; } /** * Solve the linear equation A × X = B in least square sense. *

                              * The m×n matrix A may not be square, the solution X is such that * ||A × X - B|| is minimal. *

                              * @param b Right-hand side of the equation A × X = B * @return a vector X that minimizes the two norm of A × X - B * @throws org.apache.commons.math3.exception.DimensionMismatchException * if the matrices dimensions do not match. */ public RealVector solve(final RealVector b) { return pseudoInverse.operate(b); } /** * Solve the linear equation A × X = B in least square sense. *

                              * The m×n matrix A may not be square, the solution X is such that * ||A × X - B|| is minimal. *

                              * * @param b Right-hand side of the equation A × X = B * @return a matrix X that minimizes the two norm of A × X - B * @throws org.apache.commons.math3.exception.DimensionMismatchException * if the matrices dimensions do not match. */ public RealMatrix solve(final RealMatrix b) { return pseudoInverse.multiply(b); } /** * Check if the decomposed matrix is non-singular. * * @return {@code true} if the decomposed matrix is non-singular. */ public boolean isNonSingular() { return nonSingular; } /** * Get the pseudo-inverse of the decomposed matrix. * * @return the inverse matrix. */ public RealMatrix getInverse() { return pseudoInverse; } } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/linear/RealLinearOperator.java100644 1750 1750 10354 12126627717 30421 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.linear; import org.apache.commons.math3.exception.DimensionMismatchException; /** * This class defines a linear operator operating on real ({@code double}) * vector spaces. No direct access to the coefficients of the underlying matrix * is provided. * * The motivation for such an interface is well stated by * Barrett et al. (1994): *
                              * We restrict ourselves to iterative methods, which work by repeatedly * improving an approximate solution until it is accurate enough. These * methods access the coefficient matrix A of the linear system only via the * matrix-vector product y = A · x * (and perhaps z = AT · x). Thus the user need only * supply a subroutine for computing y (and perhaps z) given x, which permits * full exploitation of the sparsity or other special structure of A. *
                              *
                              * *
                              *
                              Barret et al. (1994)
                              *
                              * R. Barrett, M. Berry, T. F. Chan, J. Demmel, J. M. Donato, J. Dongarra, * V. Eijkhout, R. Pozo, C. Romine and H. Van der Vorst, * Templates for the Solution of Linear Systems: Building Blocks for * Iterative Methods, SIAM *
                              *
                              * * @version $Id: RealLinearOperator.java 1416643 2012-12-03 19:37:14Z tn $ * @since 3.0 */ public abstract class RealLinearOperator { /** * Returns the dimension of the codomain of this operator. * * @return the number of rows of the underlying matrix */ public abstract int getRowDimension(); /** * Returns the dimension of the domain of this operator. * * @return the number of columns of the underlying matrix */ public abstract int getColumnDimension(); /** * Returns the result of multiplying {@code this} by the vector {@code x}. * * @param x the vector to operate on * @return the product of {@code this} instance with {@code x} * @throws DimensionMismatchException if the column dimension does not match * the size of {@code x} */ public abstract RealVector operate(final RealVector x) throws DimensionMismatchException; /** * Returns the result of multiplying the transpose of {@code this} operator * by the vector {@code x} (optional operation). The default implementation * throws an {@link UnsupportedOperationException}. Users overriding this * method must also override {@link #isTransposable()}. * * @param x the vector to operate on * @return the product of the transpose of {@code this} instance with * {@code x} * @throws org.apache.commons.math3.exception.DimensionMismatchException * if the row dimension does not match the size of {@code x} * @throws UnsupportedOperationException if this operation is not supported * by {@code this} operator */ public RealVector operateTranspose(final RealVector x) throws DimensionMismatchException, UnsupportedOperationException { throw new UnsupportedOperationException(); } /** * Returns {@code true} if this operator supports * {@link #operateTranspose(RealVector)}. If {@code true} is returned, * {@link #operateTranspose(RealVector)} should not throw * {@code UnsupportedOperationException}. The default implementation returns * {@code false}. * * @return {@code false} */ public boolean isTransposable() { return false; } } ././@LongLink100644 0 0 146 12126630647 10263 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/linear/FieldMatrixPreservingVisitor.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/linear/FieldMatrixPreservingVisitor.jav100644 1750 1750 4172 12126627717 32344 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.linear; import org.apache.commons.math3.FieldElement; /** * Interface defining a visitor for matrix entries. * * @param the type of the field elements * @version $Id: FieldMatrixPreservingVisitor.java 1416643 2012-12-03 19:37:14Z tn $ * @since 2.0 */ public interface FieldMatrixPreservingVisitor> { /** * Start visiting a matrix. *

                              This method is called once before any entry of the matrix is visited.

                              * @param rows number of rows of the matrix * @param columns number of columns of the matrix * @param startRow Initial row index * @param endRow Final row index (inclusive) * @param startColumn Initial column index * @param endColumn Final column index (inclusive) */ void start(int rows, int columns, int startRow, int endRow, int startColumn, int endColumn); /** * Visit one matrix entry. * @param row row index of the entry * @param column column index of the entry * @param value current value of the entry */ void visit(int row, int column, T value); /** * End visiting a matrix. *

                              This method is called once after all entries of the matrix have been visited.

                              * @return the value that the walkInXxxOrder must return */ T end(); } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/linear/IterativeLinearSolver.java100644 1750 1750 17071 12126627717 31154 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.linear; import org.apache.commons.math3.exception.DimensionMismatchException; import org.apache.commons.math3.exception.MaxCountExceededException; import org.apache.commons.math3.exception.NullArgumentException; import org.apache.commons.math3.util.IterationManager; import org.apache.commons.math3.util.MathUtils; /** * This abstract class defines an iterative solver for the linear system A * · x = b. In what follows, the residual r is defined as r = b * - A · x, where A is the linear operator of the linear system, b is the * right-hand side vector, and x the current estimate of the solution. * * @version $Id: IterativeLinearSolver.java 1416643 2012-12-03 19:37:14Z tn $ * @since 3.0 */ public abstract class IterativeLinearSolver { /** The object in charge of managing the iterations. */ private final IterationManager manager; /** * Creates a new instance of this class, with default iteration manager. * * @param maxIterations the maximum number of iterations */ public IterativeLinearSolver(final int maxIterations) { this.manager = new IterationManager(maxIterations); } /** * Creates a new instance of this class, with custom iteration manager. * * @param manager the custom iteration manager * @throws NullArgumentException if {@code manager} is {@code null} */ public IterativeLinearSolver(final IterationManager manager) throws NullArgumentException { MathUtils.checkNotNull(manager); this.manager = manager; } /** * Performs all dimension checks on the parameters of * {@link #solve(RealLinearOperator, RealVector, RealVector) solve} and * {@link #solveInPlace(RealLinearOperator, RealVector, RealVector) solveInPlace}, * and throws an exception if one of the checks fails. * * @param a the linear operator A of the system * @param b the right-hand side vector * @param x0 the initial guess of the solution * @throws NullArgumentException if one of the parameters is {@code null} * @throws NonSquareOperatorException if {@code a} is not square * @throws DimensionMismatchException if {@code b} or {@code x0} have * dimensions inconsistent with {@code a} */ protected static void checkParameters(final RealLinearOperator a, final RealVector b, final RealVector x0) throws NullArgumentException, NonSquareOperatorException, DimensionMismatchException { MathUtils.checkNotNull(a); MathUtils.checkNotNull(b); MathUtils.checkNotNull(x0); if (a.getRowDimension() != a.getColumnDimension()) { throw new NonSquareOperatorException(a.getRowDimension(), a.getColumnDimension()); } if (b.getDimension() != a.getRowDimension()) { throw new DimensionMismatchException(b.getDimension(), a.getRowDimension()); } if (x0.getDimension() != a.getColumnDimension()) { throw new DimensionMismatchException(x0.getDimension(), a.getColumnDimension()); } } /** * Returns the iteration manager attached to this solver. * * @return the manager */ public IterationManager getIterationManager() { return manager; } /** * Returns an estimate of the solution to the linear system A · x = * b. * * @param a the linear operator A of the system * @param b the right-hand side vector * @return a new vector containing the solution * @throws NullArgumentException if one of the parameters is {@code null} * @throws NonSquareOperatorException if {@code a} is not square * @throws DimensionMismatchException if {@code b} has dimensions * inconsistent with {@code a} * @throws MaxCountExceededException at exhaustion of the iteration count, * unless a custom * {@link org.apache.commons.math3.util.Incrementor.MaxCountExceededCallback callback} * has been set at construction of the {@link IterationManager} */ public RealVector solve(final RealLinearOperator a, final RealVector b) throws NullArgumentException, NonSquareOperatorException, DimensionMismatchException, MaxCountExceededException { MathUtils.checkNotNull(a); final RealVector x = new ArrayRealVector(a.getColumnDimension()); x.set(0.); return solveInPlace(a, b, x); } /** * Returns an estimate of the solution to the linear system A · x = * b. * * @param a the linear operator A of the system * @param b the right-hand side vector * @param x0 the initial guess of the solution * @return a new vector containing the solution * @throws NullArgumentException if one of the parameters is {@code null} * @throws NonSquareOperatorException if {@code a} is not square * @throws DimensionMismatchException if {@code b} or {@code x0} have * dimensions inconsistent with {@code a} * @throws MaxCountExceededException at exhaustion of the iteration count, * unless a custom * {@link org.apache.commons.math3.util.Incrementor.MaxCountExceededCallback callback} * has been set at construction of the {@link IterationManager} */ public RealVector solve(RealLinearOperator a, RealVector b, RealVector x0) throws NullArgumentException, NonSquareOperatorException, DimensionMismatchException, MaxCountExceededException { MathUtils.checkNotNull(x0); return solveInPlace(a, b, x0.copy()); } /** * Returns an estimate of the solution to the linear system A · x = * b. The solution is computed in-place (initial guess is modified). * * @param a the linear operator A of the system * @param b the right-hand side vector * @param x0 initial guess of the solution * @return a reference to {@code x0} (shallow copy) updated with the * solution * @throws NullArgumentException if one of the parameters is {@code null} * @throws NonSquareOperatorException if {@code a} is not square * @throws DimensionMismatchException if {@code b} or {@code x0} have * dimensions inconsistent with {@code a} * @throws MaxCountExceededException at exhaustion of the iteration count, * unless a custom * {@link org.apache.commons.math3.util.Incrementor.MaxCountExceededCallback callback} * has been set at construction of the {@link IterationManager} */ public abstract RealVector solveInPlace(RealLinearOperator a, RealVector b, RealVector x0) throws NullArgumentException, NonSquareOperatorException, DimensionMismatchException, MaxCountExceededException; } ././@LongLink100644 0 0 151 12126630647 10257 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/linear/IllConditionedOperatorException.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/linear/IllConditionedOperatorException.100644 1750 1750 3240 12126627717 32274 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.linear; import org.apache.commons.math3.exception.MathIllegalArgumentException; import org.apache.commons.math3.exception.util.LocalizedFormats; /** * An exception to be thrown when the condition number of a * {@link RealLinearOperator} is too high. * * @version $Id: IllConditionedOperatorException.java 1416643 2012-12-03 19:37:14Z tn $ * @since 3.0 */ public class IllConditionedOperatorException extends MathIllegalArgumentException { /** Serializable version Id. */ private static final long serialVersionUID = -7883263944530490135L; /** * Creates a new instance of this class. * * @param cond An estimate of the condition number of the offending linear * operator. */ public IllConditionedOperatorException(final double cond) { super(LocalizedFormats.ILL_CONDITIONED_OPERATOR, cond); } } ././@LongLink100644 0 0 145 12126630647 10262 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/linear/RealVectorPreservingVisitor.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/linear/RealVectorPreservingVisitor.java100644 1750 1750 4343 12126627717 32343 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.linear; /** * This interface defines a visitor for the entries of a vector. Visitors * implementing this interface do not alter the entries of the vector being * visited. * * @version $Id: RealVectorPreservingVisitor.java 1416643 2012-12-03 19:37:14Z tn $ * @since 3.1 */ public interface RealVectorPreservingVisitor { /** * Start visiting a vector. This method is called once, before any entry * of the vector is visited. * * @param dimension the size of the vector * @param start the index of the first entry to be visited * @param end the index of the last entry to be visited (inclusive) */ void start(int dimension, int start, int end); /** * Visit one entry of the vector. * * @param index the index of the entry being visited * @param value the value of the entry being visited */ void visit(int index, double value); /** * End visiting a vector. This method is called once, after all entries of * the vector have been visited. * * @return the value returned by * {@link RealVector#walkInDefaultOrder(RealVectorPreservingVisitor)}, * {@link RealVector#walkInDefaultOrder(RealVectorPreservingVisitor, int, int)}, * {@link RealVector#walkInOptimizedOrder(RealVectorPreservingVisitor)} * or * {@link RealVector#walkInOptimizedOrder(RealVectorPreservingVisitor, int, int)} */ double end(); } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/linear/IterativeLinearSolverEvent.java100644 1750 1750 11023 12126627717 32145 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.linear; import org.apache.commons.math3.util.IterationEvent; import org.apache.commons.math3.exception.MathUnsupportedOperationException; /** * This is the base class for all events occuring during the iterations of a * {@link IterativeLinearSolver}. * * @version $Id: IterativeLinearSolverEvent.java 1416643 2012-12-03 19:37:14Z tn $ * @since 3.0 */ public abstract class IterativeLinearSolverEvent extends IterationEvent { /** Serialization identifier. */ private static final long serialVersionUID = 20120129L; /** * Creates a new instance of this class. * * @param source the iterative algorithm on which the event initially * occurred * @param iterations the number of iterations performed at the time * {@code this} event is created */ public IterativeLinearSolverEvent(final Object source, final int iterations) { super(source, iterations); } /** * Returns the current right-hand side of the linear system to be solved. * This method should return an unmodifiable view, or a deep copy of the * actual right-hand side vector, in order not to compromise subsequent * iterations of the source {@link IterativeLinearSolver}. * * @return the right-hand side vector, b */ public abstract RealVector getRightHandSideVector(); /** * Returns the norm of the residual. The returned value is not required to * be exact. Instead, the norm of the so-called updated * residual (if available) should be returned. For example, the * {@link ConjugateGradient conjugate gradient} method computes a sequence * of residuals, the norm of which is cheap to compute. However, due to * accumulation of round-off errors, this residual might differ from the * true residual after some iterations. See e.g. A. Greenbaum and * Z. Strakos, Predicting the Behavior of Finite Precision Lanzos and * Conjugate Gradient Computations, Technical Report 538, Department of * Computer Science, New York University, 1991 (available * here). * * @return the norm of the residual, ||r|| */ public abstract double getNormOfResidual(); /** *

                              * Returns the residual. This is an optional operation, as all iterative * linear solvers do not provide cheap estimate of the updated residual * vector, in which case *

                              *
                                *
                              • this method should throw a * {@link MathUnsupportedOperationException},
                              • *
                              • {@link #providesResidual()} returns {@code false}.
                              • *
                              *

                              * The default implementation throws a * {@link MathUnsupportedOperationException}. If this method is overriden, * then {@link #providesResidual()} should be overriden as well. *

                              * * @return the updated residual, r */ public RealVector getResidual() { throw new MathUnsupportedOperationException(); } /** * Returns the current estimate of the solution to the linear system to be * solved. This method should return an unmodifiable view, or a deep copy of * the actual current solution, in order not to compromise subsequent * iterations of the source {@link IterativeLinearSolver}. * * @return the solution, x */ public abstract RealVector getSolution(); /** * Returns {@code true} if {@link #getResidual()} is supported. The default * implementation returns {@code false}. * * @return {@code false} if {@link #getResidual()} throws a * {@link MathUnsupportedOperationException} */ public boolean providesResidual() { return false; } } ././@LongLink100644 0 0 155 12126630647 10263 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/linear/DefaultFieldMatrixPreservingVisitor.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/linear/DefaultFieldMatrixPreservingVisi100644 1750 1750 3707 12126627717 32350 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.linear; import org.apache.commons.math3.FieldElement; /** * Default implementation of the {@link FieldMatrixPreservingVisitor} interface. *

                              * This class is a convenience to create custom visitors without defining all * methods. This class provides default implementations that do nothing. *

                              * * @param the type of the field elements * @version $Id: DefaultFieldMatrixPreservingVisitor.java 1416643 2012-12-03 19:37:14Z tn $ * @since 2.0 */ public class DefaultFieldMatrixPreservingVisitor> implements FieldMatrixPreservingVisitor { /** Zero element of the field. */ private final T zero; /** Build a new instance. * @param zero additive identity of the field */ public DefaultFieldMatrixPreservingVisitor(final T zero) { this.zero = zero; } /** {@inheritDoc} */ public void start(int rows, int columns, int startRow, int endRow, int startColumn, int endColumn) { } /** {@inheritDoc} */ public void visit(int row, int column, T value) {} /** {@inheritDoc} */ public T end() { return zero; } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/linear/ConjugateGradient.java100644 1750 1750 22752 12126627717 30271 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.linear; import org.apache.commons.math3.exception.DimensionMismatchException; import org.apache.commons.math3.exception.MaxCountExceededException; import org.apache.commons.math3.exception.NullArgumentException; import org.apache.commons.math3.exception.util.ExceptionContext; import org.apache.commons.math3.util.IterationManager; /** *

                              * This is an implementation of the conjugate gradient method for * {@link RealLinearOperator}. It follows closely the template by Barrett et al. (1994) (figure 2.5). The linear system at * hand is A · x = b, and the residual is r = b - A · x. *

                              *

                              Default stopping criterion

                              *

                              * A default stopping criterion is implemented. The iterations stop when || r || * ≤ δ || b ||, where b is the right-hand side vector, r the current * estimate of the residual, and δ a user-specified tolerance. It should * be noted that r is the so-called updated residual, which might * differ from the true residual due to rounding-off errors (see e.g. Strakos and Tichy, 2002). *

                              *

                              Iteration count

                              *

                              * In the present context, an iteration should be understood as one evaluation * of the matrix-vector product A · x. The initialization phase therefore * counts as one iteration. *

                              *

                              Exception context

                              *

                              * Besides standard {@link DimensionMismatchException}, this class might throw * {@link NonPositiveDefiniteOperatorException} if the linear operator or * the preconditioner are not positive definite. In this case, the * {@link ExceptionContext} provides some more information *

                                *
                              • key {@code "operator"} points to the offending linear operator, say L,
                              • *
                              • key {@code "vector"} points to the offending vector, say x, such that * xT · L · x < 0.
                              • *
                              *

                              *

                              References

                              *
                              *
                              Barret et al. (1994)
                              *
                              R. Barrett, M. Berry, T. F. Chan, J. Demmel, J. M. Donato, J. Dongarra, * V. Eijkhout, R. Pozo, C. Romine and H. Van der Vorst, * * Templates for the Solution of Linear Systems: Building Blocks for Iterative * Methods, SIAM
                              *
                              Strakos and Tichy (2002) *
                              *
                              Z. Strakos and P. Tichy, * On error estimation in the conjugate gradient method and why it works * in finite precision computations, Electronic Transactions on * Numerical Analysis 13: 56-80, 2002
                              *
                              * * @version $Id: ConjugateGradient.java 1416643 2012-12-03 19:37:14Z tn $ * @since 3.0 */ public class ConjugateGradient extends PreconditionedIterativeLinearSolver { /** Key for the exception context. */ public static final String OPERATOR = "operator"; /** Key for the exception context. */ public static final String VECTOR = "vector"; /** * {@code true} if positive-definiteness of matrix and preconditioner should * be checked. */ private boolean check; /** The value of δ, for the default stopping criterion. */ private final double delta; /** * Creates a new instance of this class, with default * stopping criterion. * * @param maxIterations the maximum number of iterations * @param delta the δ parameter for the default stopping criterion * @param check {@code true} if positive definiteness of both matrix and * preconditioner should be checked */ public ConjugateGradient(final int maxIterations, final double delta, final boolean check) { super(maxIterations); this.delta = delta; this.check = check; } /** * Creates a new instance of this class, with default * stopping criterion and custom iteration manager. * * @param manager the custom iteration manager * @param delta the δ parameter for the default stopping criterion * @param check {@code true} if positive definiteness of both matrix and * preconditioner should be checked * @throws NullArgumentException if {@code manager} is {@code null} */ public ConjugateGradient(final IterationManager manager, final double delta, final boolean check) throws NullArgumentException { super(manager); this.delta = delta; this.check = check; } /** * Returns {@code true} if positive-definiteness should be checked for both * matrix and preconditioner. * * @return {@code true} if the tests are to be performed */ public final boolean getCheck() { return check; } /** * {@inheritDoc} * * @throws NonPositiveDefiniteOperatorException if {@code a} or {@code m} is * not positive definite */ @Override public RealVector solveInPlace(final RealLinearOperator a, final RealLinearOperator m, final RealVector b, final RealVector x0) throws NullArgumentException, NonPositiveDefiniteOperatorException, NonSquareOperatorException, DimensionMismatchException, MaxCountExceededException, NonPositiveDefiniteOperatorException { checkParameters(a, m, b, x0); final IterationManager manager = getIterationManager(); // Initialization of default stopping criterion manager.resetIterationCount(); final double rmax = delta * b.getNorm(); final RealVector bro = RealVector.unmodifiableRealVector(b); // Initialization phase counts as one iteration. manager.incrementIterationCount(); // p and x are constructed as copies of x0, since presumably, the type // of x is optimized for the calculation of the matrix-vector product // A.x. final RealVector x = x0; final RealVector xro = RealVector.unmodifiableRealVector(x); final RealVector p = x.copy(); RealVector q = a.operate(p); final RealVector r = b.combine(1, -1, q); final RealVector rro = RealVector.unmodifiableRealVector(r); double rnorm = r.getNorm(); RealVector z; if (m == null) { z = r; } else { z = null; } IterativeLinearSolverEvent evt; evt = new DefaultIterativeLinearSolverEvent(this, manager.getIterations(), xro, bro, rro, rnorm); manager.fireInitializationEvent(evt); if (rnorm <= rmax) { manager.fireTerminationEvent(evt); return x; } double rhoPrev = 0.; while (true) { manager.incrementIterationCount(); evt = new DefaultIterativeLinearSolverEvent(this, manager.getIterations(), xro, bro, rro, rnorm); manager.fireIterationStartedEvent(evt); if (m != null) { z = m.operate(r); } final double rhoNext = r.dotProduct(z); if (check && (rhoNext <= 0.)) { final NonPositiveDefiniteOperatorException e; e = new NonPositiveDefiniteOperatorException(); final ExceptionContext context = e.getContext(); context.setValue(OPERATOR, m); context.setValue(VECTOR, r); throw e; } if (manager.getIterations() == 2) { p.setSubVector(0, z); } else { p.combineToSelf(rhoNext / rhoPrev, 1., z); } q = a.operate(p); final double pq = p.dotProduct(q); if (check && (pq <= 0.)) { final NonPositiveDefiniteOperatorException e; e = new NonPositiveDefiniteOperatorException(); final ExceptionContext context = e.getContext(); context.setValue(OPERATOR, a); context.setValue(VECTOR, p); throw e; } final double alpha = rhoNext / pq; x.combineToSelf(1., alpha, p); r.combineToSelf(1., -alpha, q); rhoPrev = rhoNext; rnorm = r.getNorm(); evt = new DefaultIterativeLinearSolverEvent(this, manager.getIterations(), xro, bro, rro, rnorm); manager.fireIterationPerformedEvent(evt); if (rnorm <= rmax) { manager.fireTerminationEvent(evt); return x; } } } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/linear/SchurTransformer.java100644 1750 1750 40350 12126627717 30175 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.linear; import org.apache.commons.math3.exception.MaxCountExceededException; import org.apache.commons.math3.exception.util.LocalizedFormats; import org.apache.commons.math3.util.FastMath; import org.apache.commons.math3.util.Precision; /** * Class transforming a general real matrix to Schur form. *

                              A m × m matrix A can be written as the product of three matrices: A = P * × T × PT with P an orthogonal matrix and T an quasi-triangular * matrix. Both P and T are m × m matrices.

                              *

                              Transformation to Schur form is often not a goal by itself, but it is an * intermediate step in more general decomposition algorithms like * {@link EigenDecomposition eigen decomposition}. This class is therefore * intended for internal use by the library and is not public. As a consequence * of this explicitly limited scope, many methods directly returns references to * internal arrays, not copies.

                              *

                              This class is based on the method hqr2 in class EigenvalueDecomposition * from the JAMA library.

                              * * @see Schur Decomposition - MathWorld * @see Schur Decomposition - Wikipedia * @see Householder Transformations * @version $Id: SchurTransformer.java 1389129 2012-09-23 19:34:02Z tn $ * @since 3.1 */ class SchurTransformer { /** Maximum allowed iterations for convergence of the transformation. */ private static final int MAX_ITERATIONS = 100; /** P matrix. */ private final double matrixP[][]; /** T matrix. */ private final double matrixT[][]; /** Cached value of P. */ private RealMatrix cachedP; /** Cached value of T. */ private RealMatrix cachedT; /** Cached value of PT. */ private RealMatrix cachedPt; /** Epsilon criteria taken from JAMA code (originally was 2^-52). */ private final double epsilon = Precision.EPSILON; /** * Build the transformation to Schur form of a general real matrix. * * @param matrix matrix to transform * @throws NonSquareMatrixException if the matrix is not square */ public SchurTransformer(final RealMatrix matrix) { if (!matrix.isSquare()) { throw new NonSquareMatrixException(matrix.getRowDimension(), matrix.getColumnDimension()); } HessenbergTransformer transformer = new HessenbergTransformer(matrix); matrixT = transformer.getH().getData(); matrixP = transformer.getP().getData(); cachedT = null; cachedP = null; cachedPt = null; // transform matrix transform(); } /** * Returns the matrix P of the transform. *

                              P is an orthogonal matrix, i.e. its inverse is also its transpose.

                              * * @return the P matrix */ public RealMatrix getP() { if (cachedP == null) { cachedP = MatrixUtils.createRealMatrix(matrixP); } return cachedP; } /** * Returns the transpose of the matrix P of the transform. *

                              P is an orthogonal matrix, i.e. its inverse is also its transpose.

                              * * @return the transpose of the P matrix */ public RealMatrix getPT() { if (cachedPt == null) { cachedPt = getP().transpose(); } // return the cached matrix return cachedPt; } /** * Returns the quasi-triangular Schur matrix T of the transform. * * @return the T matrix */ public RealMatrix getT() { if (cachedT == null) { cachedT = MatrixUtils.createRealMatrix(matrixT); } // return the cached matrix return cachedT; } /** * Transform original matrix to Schur form. * @throws MaxCountExceededException if the transformation does not converge */ private void transform() { final int n = matrixT.length; // compute matrix norm final double norm = getNorm(); // shift information final ShiftInfo shift = new ShiftInfo(); // Outer loop over eigenvalue index int iteration = 0; int iu = n - 1; while (iu >= 0) { // Look for single small sub-diagonal element final int il = findSmallSubDiagonalElement(iu, norm); // Check for convergence if (il == iu) { // One root found matrixT[iu][iu] = matrixT[iu][iu] + shift.exShift; iu--; iteration = 0; } else if (il == iu - 1) { // Two roots found double p = (matrixT[iu - 1][iu - 1] - matrixT[iu][iu]) / 2.0; double q = p * p + matrixT[iu][iu - 1] * matrixT[iu - 1][iu]; matrixT[iu][iu] += shift.exShift; matrixT[iu - 1][iu - 1] += shift.exShift; if (q >= 0) { double z = FastMath.sqrt(FastMath.abs(q)); if (p >= 0) { z = p + z; } else { z = p - z; } final double x = matrixT[iu][iu - 1]; final double s = FastMath.abs(x) + FastMath.abs(z); p = x / s; q = z / s; final double r = FastMath.sqrt(p * p + q * q); p = p / r; q = q / r; // Row modification for (int j = iu - 1; j < n; j++) { z = matrixT[iu - 1][j]; matrixT[iu - 1][j] = q * z + p * matrixT[iu][j]; matrixT[iu][j] = q * matrixT[iu][j] - p * z; } // Column modification for (int i = 0; i <= iu; i++) { z = matrixT[i][iu - 1]; matrixT[i][iu - 1] = q * z + p * matrixT[i][iu]; matrixT[i][iu] = q * matrixT[i][iu] - p * z; } // Accumulate transformations for (int i = 0; i <= n - 1; i++) { z = matrixP[i][iu - 1]; matrixP[i][iu - 1] = q * z + p * matrixP[i][iu]; matrixP[i][iu] = q * matrixP[i][iu] - p * z; } } iu -= 2; iteration = 0; } else { // No convergence yet computeShift(il, iu, iteration, shift); // stop transformation after too many iterations if (++iteration > MAX_ITERATIONS) { throw new MaxCountExceededException(LocalizedFormats.CONVERGENCE_FAILED, MAX_ITERATIONS); } // the initial houseHolder vector for the QR step final double[] hVec = new double[3]; final int im = initQRStep(il, iu, shift, hVec); performDoubleQRStep(il, im, iu, shift, hVec); } } } /** * Computes the L1 norm of the (quasi-)triangular matrix T. * * @return the L1 norm of matrix T */ private double getNorm() { double norm = 0.0; for (int i = 0; i < matrixT.length; i++) { // as matrix T is (quasi-)triangular, also take the sub-diagonal element into account for (int j = FastMath.max(i - 1, 0); j < matrixT.length; j++) { norm += FastMath.abs(matrixT[i][j]); } } return norm; } /** * Find the first small sub-diagonal element and returns its index. * * @param startIdx the starting index for the search * @param norm the L1 norm of the matrix * @return the index of the first small sub-diagonal element */ private int findSmallSubDiagonalElement(final int startIdx, final double norm) { int l = startIdx; while (l > 0) { double s = FastMath.abs(matrixT[l - 1][l - 1]) + FastMath.abs(matrixT[l][l]); if (s == 0.0) { s = norm; } if (FastMath.abs(matrixT[l][l - 1]) < epsilon * s) { break; } l--; } return l; } /** * Compute the shift for the current iteration. * * @param l the index of the small sub-diagonal element * @param idx the current eigenvalue index * @param iteration the current iteration * @param shift holder for shift information */ private void computeShift(final int l, final int idx, final int iteration, final ShiftInfo shift) { // Form shift shift.x = matrixT[idx][idx]; shift.y = shift.w = 0.0; if (l < idx) { shift.y = matrixT[idx - 1][idx - 1]; shift.w = matrixT[idx][idx - 1] * matrixT[idx - 1][idx]; } // Wilkinson's original ad hoc shift if (iteration == 10) { shift.exShift += shift.x; for (int i = 0; i <= idx; i++) { matrixT[i][i] -= shift.x; } final double s = FastMath.abs(matrixT[idx][idx - 1]) + FastMath.abs(matrixT[idx - 1][idx - 2]); shift.x = 0.75 * s; shift.y = 0.75 * s; shift.w = -0.4375 * s * s; } // MATLAB's new ad hoc shift if (iteration == 30) { double s = (shift.y - shift.x) / 2.0; s = s * s + shift.w; if (s > 0.0) { s = FastMath.sqrt(s); if (shift.y < shift.x) { s = -s; } s = shift.x - shift.w / ((shift.y - shift.x) / 2.0 + s); for (int i = 0; i <= idx; i++) { matrixT[i][i] -= s; } shift.exShift += s; shift.x = shift.y = shift.w = 0.964; } } } /** * Initialize the householder vectors for the QR step. * * @param il the index of the small sub-diagonal element * @param iu the current eigenvalue index * @param shift shift information holder * @param hVec the initial houseHolder vector * @return the start index for the QR step */ private int initQRStep(int il, final int iu, final ShiftInfo shift, double[] hVec) { // Look for two consecutive small sub-diagonal elements int im = iu - 2; while (im >= il) { final double z = matrixT[im][im]; final double r = shift.x - z; double s = shift.y - z; hVec[0] = (r * s - shift.w) / matrixT[im + 1][im] + matrixT[im][im + 1]; hVec[1] = matrixT[im + 1][im + 1] - z - r - s; hVec[2] = matrixT[im + 2][im + 1]; if (im == il) { break; } final double lhs = FastMath.abs(matrixT[im][im - 1]) * (FastMath.abs(hVec[1]) + FastMath.abs(hVec[2])); final double rhs = FastMath.abs(hVec[0]) * (FastMath.abs(matrixT[im - 1][im - 1]) + FastMath.abs(z) + FastMath.abs(matrixT[im + 1][im + 1])); if (lhs < epsilon * rhs) { break; } im--; } return im; } /** * Perform a double QR step involving rows l:idx and columns m:n * * @param il the index of the small sub-diagonal element * @param im the start index for the QR step * @param iu the current eigenvalue index * @param shift shift information holder * @param hVec the initial houseHolder vector */ private void performDoubleQRStep(final int il, final int im, final int iu, final ShiftInfo shift, final double[] hVec) { final int n = matrixT.length; double p = hVec[0]; double q = hVec[1]; double r = hVec[2]; for (int k = im; k <= iu - 1; k++) { boolean notlast = k != (iu - 1); if (k != im) { p = matrixT[k][k - 1]; q = matrixT[k + 1][k - 1]; r = notlast ? matrixT[k + 2][k - 1] : 0.0; shift.x = FastMath.abs(p) + FastMath.abs(q) + FastMath.abs(r); if (!Precision.equals(shift.x, 0.0, epsilon)) { p = p / shift.x; q = q / shift.x; r = r / shift.x; } } if (shift.x == 0.0) { break; } double s = FastMath.sqrt(p * p + q * q + r * r); if (p < 0.0) { s = -s; } if (s != 0.0) { if (k != im) { matrixT[k][k - 1] = -s * shift.x; } else if (il != im) { matrixT[k][k - 1] = -matrixT[k][k - 1]; } p = p + s; shift.x = p / s; shift.y = q / s; double z = r / s; q = q / p; r = r / p; // Row modification for (int j = k; j < n; j++) { p = matrixT[k][j] + q * matrixT[k + 1][j]; if (notlast) { p = p + r * matrixT[k + 2][j]; matrixT[k + 2][j] = matrixT[k + 2][j] - p * z; } matrixT[k][j] = matrixT[k][j] - p * shift.x; matrixT[k + 1][j] = matrixT[k + 1][j] - p * shift.y; } // Column modification for (int i = 0; i <= FastMath.min(iu, k + 3); i++) { p = shift.x * matrixT[i][k] + shift.y * matrixT[i][k + 1]; if (notlast) { p = p + z * matrixT[i][k + 2]; matrixT[i][k + 2] = matrixT[i][k + 2] - p * r; } matrixT[i][k] = matrixT[i][k] - p; matrixT[i][k + 1] = matrixT[i][k + 1] - p * q; } // Accumulate transformations final int high = matrixT.length - 1; for (int i = 0; i <= high; i++) { p = shift.x * matrixP[i][k] + shift.y * matrixP[i][k + 1]; if (notlast) { p = p + z * matrixP[i][k + 2]; matrixP[i][k + 2] = matrixP[i][k + 2] - p * r; } matrixP[i][k] = matrixP[i][k] - p; matrixP[i][k + 1] = matrixP[i][k + 1] - p * q; } } // (s != 0) } // k loop // clean up pollution due to round-off errors for (int i = im + 2; i <= iu; i++) { matrixT[i][i-2] = 0.0; if (i > im + 2) { matrixT[i][i-3] = 0.0; } } } /** * Internal data structure holding the current shift information. * Contains variable names as present in the original JAMA code. */ private static class ShiftInfo { // CHECKSTYLE: stop all /** x shift info */ double x; /** y shift info */ double y; /** w shift info */ double w; /** Indicates an exceptional shift. */ double exShift; // CHECKSTYLE: resume all } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/linear/TriDiagonalTransformer.java100644 1750 1750 22523 12126627717 31310 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.linear; import java.util.Arrays; import org.apache.commons.math3.util.FastMath; /** * Class transforming a symmetrical matrix to tridiagonal shape. *

                              A symmetrical m × m matrix A can be written as the product of three matrices: * A = Q × T × QT with Q an orthogonal matrix and T a symmetrical * tridiagonal matrix. Both Q and T are m × m matrices.

                              *

                              This implementation only uses the upper part of the matrix, the part below the * diagonal is not accessed at all.

                              *

                              Transformation to tridiagonal shape is often not a goal by itself, but it is * an intermediate step in more general decomposition algorithms like {@link * EigenDecomposition eigen decomposition}. This class is therefore intended for internal * use by the library and is not public. As a consequence of this explicitly limited scope, * many methods directly returns references to internal arrays, not copies.

                              * @version $Id: TriDiagonalTransformer.java 1416643 2012-12-03 19:37:14Z tn $ * @since 2.0 */ class TriDiagonalTransformer { /** Householder vectors. */ private final double householderVectors[][]; /** Main diagonal. */ private final double[] main; /** Secondary diagonal. */ private final double[] secondary; /** Cached value of Q. */ private RealMatrix cachedQ; /** Cached value of Qt. */ private RealMatrix cachedQt; /** Cached value of T. */ private RealMatrix cachedT; /** * Build the transformation to tridiagonal shape of a symmetrical matrix. *

                              The specified matrix is assumed to be symmetrical without any check. * Only the upper triangular part of the matrix is used.

                              * * @param matrix Symmetrical matrix to transform. * @throws NonSquareMatrixException if the matrix is not square. */ public TriDiagonalTransformer(RealMatrix matrix) { if (!matrix.isSquare()) { throw new NonSquareMatrixException(matrix.getRowDimension(), matrix.getColumnDimension()); } final int m = matrix.getRowDimension(); householderVectors = matrix.getData(); main = new double[m]; secondary = new double[m - 1]; cachedQ = null; cachedQt = null; cachedT = null; // transform matrix transform(); } /** * Returns the matrix Q of the transform. *

                              Q is an orthogonal matrix, i.e. its transpose is also its inverse.

                              * @return the Q matrix */ public RealMatrix getQ() { if (cachedQ == null) { cachedQ = getQT().transpose(); } return cachedQ; } /** * Returns the transpose of the matrix Q of the transform. *

                              Q is an orthogonal matrix, i.e. its transpose is also its inverse.

                              * @return the Q matrix */ public RealMatrix getQT() { if (cachedQt == null) { final int m = householderVectors.length; double[][] qta = new double[m][m]; // build up first part of the matrix by applying Householder transforms for (int k = m - 1; k >= 1; --k) { final double[] hK = householderVectors[k - 1]; qta[k][k] = 1; if (hK[k] != 0.0) { final double inv = 1.0 / (secondary[k - 1] * hK[k]); double beta = 1.0 / secondary[k - 1]; qta[k][k] = 1 + beta * hK[k]; for (int i = k + 1; i < m; ++i) { qta[k][i] = beta * hK[i]; } for (int j = k + 1; j < m; ++j) { beta = 0; for (int i = k + 1; i < m; ++i) { beta += qta[j][i] * hK[i]; } beta *= inv; qta[j][k] = beta * hK[k]; for (int i = k + 1; i < m; ++i) { qta[j][i] += beta * hK[i]; } } } } qta[0][0] = 1; cachedQt = MatrixUtils.createRealMatrix(qta); } // return the cached matrix return cachedQt; } /** * Returns the tridiagonal matrix T of the transform. * @return the T matrix */ public RealMatrix getT() { if (cachedT == null) { final int m = main.length; double[][] ta = new double[m][m]; for (int i = 0; i < m; ++i) { ta[i][i] = main[i]; if (i > 0) { ta[i][i - 1] = secondary[i - 1]; } if (i < main.length - 1) { ta[i][i + 1] = secondary[i]; } } cachedT = MatrixUtils.createRealMatrix(ta); } // return the cached matrix return cachedT; } /** * Get the Householder vectors of the transform. *

                              Note that since this class is only intended for internal use, * it returns directly a reference to its internal arrays, not a copy.

                              * @return the main diagonal elements of the B matrix */ double[][] getHouseholderVectorsRef() { return householderVectors; } /** * Get the main diagonal elements of the matrix T of the transform. *

                              Note that since this class is only intended for internal use, * it returns directly a reference to its internal arrays, not a copy.

                              * @return the main diagonal elements of the T matrix */ double[] getMainDiagonalRef() { return main; } /** * Get the secondary diagonal elements of the matrix T of the transform. *

                              Note that since this class is only intended for internal use, * it returns directly a reference to its internal arrays, not a copy.

                              * @return the secondary diagonal elements of the T matrix */ double[] getSecondaryDiagonalRef() { return secondary; } /** * Transform original matrix to tridiagonal form. *

                              Transformation is done using Householder transforms.

                              */ private void transform() { final int m = householderVectors.length; final double[] z = new double[m]; for (int k = 0; k < m - 1; k++) { //zero-out a row and a column simultaneously final double[] hK = householderVectors[k]; main[k] = hK[k]; double xNormSqr = 0; for (int j = k + 1; j < m; ++j) { final double c = hK[j]; xNormSqr += c * c; } final double a = (hK[k + 1] > 0) ? -FastMath.sqrt(xNormSqr) : FastMath.sqrt(xNormSqr); secondary[k] = a; if (a != 0.0) { // apply Householder transform from left and right simultaneously hK[k + 1] -= a; final double beta = -1 / (a * hK[k + 1]); // compute a = beta A v, where v is the Householder vector // this loop is written in such a way // 1) only the upper triangular part of the matrix is accessed // 2) access is cache-friendly for a matrix stored in rows Arrays.fill(z, k + 1, m, 0); for (int i = k + 1; i < m; ++i) { final double[] hI = householderVectors[i]; final double hKI = hK[i]; double zI = hI[i] * hKI; for (int j = i + 1; j < m; ++j) { final double hIJ = hI[j]; zI += hIJ * hK[j]; z[j] += hIJ * hKI; } z[i] = beta * (z[i] + zI); } // compute gamma = beta vT z / 2 double gamma = 0; for (int i = k + 1; i < m; ++i) { gamma += z[i] * hK[i]; } gamma *= beta / 2; // compute z = z - gamma v for (int i = k + 1; i < m; ++i) { z[i] -= gamma * hK[i]; } // update matrix: A = A - v zT - z vT // only the upper triangular part of the matrix is updated for (int i = k + 1; i < m; ++i) { final double[] hI = householderVectors[i]; for (int j = i; j < m; ++j) { hI[j] -= hK[i] * z[j] + z[i] * hK[j]; } } } } main[m - 1] = householderVectors[m - 1][m - 1]; } } ././@LongLink100644 0 0 153 12126630647 10261 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/linear/DefaultIterativeLinearSolverEvent.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/linear/DefaultIterativeLinearSolverEven100644 1750 1750 12234 12126627717 32353 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.linear; import org.apache.commons.math3.exception.MathUnsupportedOperationException; /** * A default concrete implementation of the abstract class * {@link IterativeLinearSolverEvent}. * * @version $Id: DefaultIterativeLinearSolverEvent.java 1416643 2012-12-03 19:37:14Z tn $ */ public class DefaultIterativeLinearSolverEvent extends IterativeLinearSolverEvent { /** */ private static final long serialVersionUID = 20120129L; /** The right-hand side vector. */ private final RealVector b; /** The current estimate of the residual. */ private final RealVector r; /** The current estimate of the norm of the residual. */ private final double rnorm; /** The current estimate of the solution. */ private final RealVector x; /** * Creates a new instance of this class. This implementation does * not deep copy the specified vectors {@code x}, {@code b}, * {@code r}. Therefore the user must make sure that these vectors are * either unmodifiable views or deep copies of the same vectors actually * used by the {@code source}. Failure to do so may compromise subsequent * iterations of the {@code source}. If the residual vector {@code r} is * {@code null}, then {@link #getResidual()} throws a * {@link MathUnsupportedOperationException}, and * {@link #providesResidual()} returns {@code false}. * * @param source the iterative solver which fired this event * @param iterations the number of iterations performed at the time * {@code this} event is created * @param x the current estimate of the solution * @param b the right-hand side vector * @param r the current estimate of the residual (can be {@code null}) * @param rnorm the norm of the current estimate of the residual */ public DefaultIterativeLinearSolverEvent(final Object source, final int iterations, final RealVector x, final RealVector b, final RealVector r, final double rnorm) { super(source, iterations); this.x = x; this.b = b; this.r = r; this.rnorm = rnorm; } /** * Creates a new instance of this class. This implementation does * not deep copy the specified vectors {@code x}, {@code b}. * Therefore the user must make sure that these vectors are either * unmodifiable views or deep copies of the same vectors actually used by * the {@code source}. Failure to do so may compromise subsequent iterations * of the {@code source}. Callling {@link #getResidual()} on instances * returned by this constructor throws a * {@link MathUnsupportedOperationException}, while * {@link #providesResidual()} returns {@code false}. * * @param source the iterative solver which fired this event * @param iterations the number of iterations performed at the time * {@code this} event is created * @param x the current estimate of the solution * @param b the right-hand side vector * @param rnorm the norm of the current estimate of the residual */ public DefaultIterativeLinearSolverEvent(final Object source, final int iterations, final RealVector x, final RealVector b, final double rnorm) { super(source, iterations); this.x = x; this.b = b; this.r = null; this.rnorm = rnorm; } /** {@inheritDoc} */ @Override public double getNormOfResidual() { return rnorm; } /** * {@inheritDoc} * * This implementation throws an {@link MathUnsupportedOperationException} * if no residual vector {@code r} was provided at construction time. */ @Override public RealVector getResidual() { if (r != null) { return r; } throw new MathUnsupportedOperationException(); } /** {@inheritDoc} */ @Override public RealVector getRightHandSideVector() { return b; } /** {@inheritDoc} */ @Override public RealVector getSolution() { return x; } /** * {@inheritDoc} * * This implementation returns {@code true} if a non-{@code null} value was * specified for the residual vector {@code r} at construction time. * * @return {@code true} if {@code r != null} */ @Override public boolean providesResidual() { return r != null; } } ././@LongLink100644 0 0 152 12126630647 10260 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/linear/DefaultRealMatrixChangingVisitor.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/linear/DefaultRealMatrixChangingVisitor100644 1750 1750 3150 12126627717 32317 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.linear; /** * Default implementation of the {@link RealMatrixChangingVisitor} interface. *

                              * This class is a convenience to create custom visitors without defining all * methods. This class provides default implementations that do nothing. *

                              * * @version $Id: DefaultRealMatrixChangingVisitor.java 1416643 2012-12-03 19:37:14Z tn $ * @since 2.0 */ public class DefaultRealMatrixChangingVisitor implements RealMatrixChangingVisitor { /** {@inheritDoc} */ public void start(int rows, int columns, int startRow, int endRow, int startColumn, int endColumn) { } /** {@inheritDoc} */ public double visit(int row, int column, double value) { return value; } /** {@inheritDoc} */ public double end() { return 0; } } ././@LongLink100644 0 0 154 12126630647 10262 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/linear/DefaultRealMatrixPreservingVisitor.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/linear/DefaultRealMatrixPreservingVisit100644 1750 1750 3123 12126627717 32364 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.linear; /** * Default implementation of the {@link RealMatrixPreservingVisitor} interface. *

                              * This class is a convenience to create custom visitors without defining all * methods. This class provides default implementations that do nothing. *

                              * * @version $Id: DefaultRealMatrixPreservingVisitor.java 1416643 2012-12-03 19:37:14Z tn $ * @since 2.0 */ public class DefaultRealMatrixPreservingVisitor implements RealMatrixPreservingVisitor { /** {@inheritDoc} */ public void start(int rows, int columns, int startRow, int endRow, int startColumn, int endColumn) { } /** {@inheritDoc} */ public void visit(int row, int column, double value) {} /** {@inheritDoc} */ public double end() { return 0; } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/linear/AbstractRealMatrix.java100644 1750 1750 106671 12126627717 30453 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.linear; import java.util.ArrayList; import java.util.Locale; import org.apache.commons.math3.exception.NoDataException; import org.apache.commons.math3.exception.NotPositiveException; import org.apache.commons.math3.exception.NotStrictlyPositiveException; import org.apache.commons.math3.exception.DimensionMismatchException; import org.apache.commons.math3.exception.NullArgumentException; import org.apache.commons.math3.exception.NumberIsTooSmallException; import org.apache.commons.math3.exception.OutOfRangeException; import org.apache.commons.math3.exception.util.LocalizedFormats; import org.apache.commons.math3.util.MathUtils; import org.apache.commons.math3.util.FastMath; /** * Basic implementation of RealMatrix methods regardless of the underlying storage. *

                              All the methods implemented here use {@link #getEntry(int, int)} to access * matrix elements. Derived class can provide faster implementations.

                              * * @version $Id: AbstractRealMatrix.java 1459534 2013-03-21 21:24:45Z tn $ * @since 2.0 */ public abstract class AbstractRealMatrix extends RealLinearOperator implements RealMatrix { /** Default format. */ private static final RealMatrixFormat DEFAULT_FORMAT = RealMatrixFormat.getInstance(Locale.US); static { // set the minimum fraction digits to 1 to keep compatibility DEFAULT_FORMAT.getFormat().setMinimumFractionDigits(1); } /** * Creates a matrix with no data */ protected AbstractRealMatrix() {} /** * Create a new RealMatrix with the supplied row and column dimensions. * * @param rowDimension the number of rows in the new matrix * @param columnDimension the number of columns in the new matrix * @throws NotStrictlyPositiveException if row or column dimension is not positive */ protected AbstractRealMatrix(final int rowDimension, final int columnDimension) throws NotStrictlyPositiveException { if (rowDimension < 1) { throw new NotStrictlyPositiveException(rowDimension); } if (columnDimension < 1) { throw new NotStrictlyPositiveException(columnDimension); } } /** {@inheritDoc} */ public RealMatrix add(RealMatrix m) throws MatrixDimensionMismatchException { MatrixUtils.checkAdditionCompatible(this, m); final int rowCount = getRowDimension(); final int columnCount = getColumnDimension(); final RealMatrix out = createMatrix(rowCount, columnCount); for (int row = 0; row < rowCount; ++row) { for (int col = 0; col < columnCount; ++col) { out.setEntry(row, col, getEntry(row, col) + m.getEntry(row, col)); } } return out; } /** {@inheritDoc} */ public RealMatrix subtract(final RealMatrix m) throws MatrixDimensionMismatchException { MatrixUtils.checkSubtractionCompatible(this, m); final int rowCount = getRowDimension(); final int columnCount = getColumnDimension(); final RealMatrix out = createMatrix(rowCount, columnCount); for (int row = 0; row < rowCount; ++row) { for (int col = 0; col < columnCount; ++col) { out.setEntry(row, col, getEntry(row, col) - m.getEntry(row, col)); } } return out; } /** {@inheritDoc} */ public RealMatrix scalarAdd(final double d) { final int rowCount = getRowDimension(); final int columnCount = getColumnDimension(); final RealMatrix out = createMatrix(rowCount, columnCount); for (int row = 0; row < rowCount; ++row) { for (int col = 0; col < columnCount; ++col) { out.setEntry(row, col, getEntry(row, col) + d); } } return out; } /** {@inheritDoc} */ public RealMatrix scalarMultiply(final double d) { final int rowCount = getRowDimension(); final int columnCount = getColumnDimension(); final RealMatrix out = createMatrix(rowCount, columnCount); for (int row = 0; row < rowCount; ++row) { for (int col = 0; col < columnCount; ++col) { out.setEntry(row, col, getEntry(row, col) * d); } } return out; } /** {@inheritDoc} */ public RealMatrix multiply(final RealMatrix m) throws DimensionMismatchException { MatrixUtils.checkMultiplicationCompatible(this, m); final int nRows = getRowDimension(); final int nCols = m.getColumnDimension(); final int nSum = getColumnDimension(); final RealMatrix out = createMatrix(nRows, nCols); for (int row = 0; row < nRows; ++row) { for (int col = 0; col < nCols; ++col) { double sum = 0; for (int i = 0; i < nSum; ++i) { sum += getEntry(row, i) * m.getEntry(i, col); } out.setEntry(row, col, sum); } } return out; } /** {@inheritDoc} */ public RealMatrix preMultiply(final RealMatrix m) throws DimensionMismatchException { return m.multiply(this); } /** {@inheritDoc} */ public RealMatrix power(final int p) throws NotPositiveException, NonSquareMatrixException { if (p < 0) { throw new NotPositiveException(LocalizedFormats.NOT_POSITIVE_EXPONENT, p); } if (!isSquare()) { throw new NonSquareMatrixException(getRowDimension(), getColumnDimension()); } if (p == 0) { return MatrixUtils.createRealIdentityMatrix(this.getRowDimension()); } if (p == 1) { return this.copy(); } final int power = p - 1; /* * Only log_2(p) operations is used by doing as follows: * 5^214 = 5^128 * 5^64 * 5^16 * 5^4 * 5^2 * * In general, the same approach is used for A^p. */ final char[] binaryRepresentation = Integer.toBinaryString(power).toCharArray(); final ArrayList nonZeroPositions = new ArrayList(); int maxI = -1; for (int i = 0; i < binaryRepresentation.length; ++i) { if (binaryRepresentation[i] == '1') { final int pos = binaryRepresentation.length - i - 1; nonZeroPositions.add(pos); // The positions are taken in turn, so maxI is only changed once if (maxI == -1) { maxI = pos; } } } RealMatrix[] results = new RealMatrix[maxI + 1]; results[0] = this.copy(); for (int i = 1; i <= maxI; ++i) { results[i] = results[i-1].multiply(results[i-1]); } RealMatrix result = this.copy(); for (Integer i : nonZeroPositions) { result = result.multiply(results[i]); } return result; } /** {@inheritDoc} */ public double[][] getData() { final double[][] data = new double[getRowDimension()][getColumnDimension()]; for (int i = 0; i < data.length; ++i) { final double[] dataI = data[i]; for (int j = 0; j < dataI.length; ++j) { dataI[j] = getEntry(i, j); } } return data; } /** {@inheritDoc} */ public double getNorm() { return walkInColumnOrder(new RealMatrixPreservingVisitor() { /** Last row index. */ private double endRow; /** Sum of absolute values on one column. */ private double columnSum; /** Maximal sum across all columns. */ private double maxColSum; /** {@inheritDoc} */ public void start(final int rows, final int columns, final int startRow, final int endRow, final int startColumn, final int endColumn) { this.endRow = endRow; columnSum = 0; maxColSum = 0; } /** {@inheritDoc} */ public void visit(final int row, final int column, final double value) { columnSum += FastMath.abs(value); if (row == endRow) { maxColSum = FastMath.max(maxColSum, columnSum); columnSum = 0; } } /** {@inheritDoc} */ public double end() { return maxColSum; } }); } /** {@inheritDoc} */ public double getFrobeniusNorm() { return walkInOptimizedOrder(new RealMatrixPreservingVisitor() { /** Sum of squared entries. */ private double sum; /** {@inheritDoc} */ public void start(final int rows, final int columns, final int startRow, final int endRow, final int startColumn, final int endColumn) { sum = 0; } /** {@inheritDoc} */ public void visit(final int row, final int column, final double value) { sum += value * value; } /** {@inheritDoc} */ public double end() { return FastMath.sqrt(sum); } }); } /** {@inheritDoc} */ public RealMatrix getSubMatrix(final int startRow, final int endRow, final int startColumn, final int endColumn) throws OutOfRangeException, NumberIsTooSmallException { MatrixUtils.checkSubMatrixIndex(this, startRow, endRow, startColumn, endColumn); final RealMatrix subMatrix = createMatrix(endRow - startRow + 1, endColumn - startColumn + 1); for (int i = startRow; i <= endRow; ++i) { for (int j = startColumn; j <= endColumn; ++j) { subMatrix.setEntry(i - startRow, j - startColumn, getEntry(i, j)); } } return subMatrix; } /** {@inheritDoc} */ public RealMatrix getSubMatrix(final int[] selectedRows, final int[] selectedColumns) throws NullArgumentException, NoDataException, OutOfRangeException { MatrixUtils.checkSubMatrixIndex(this, selectedRows, selectedColumns); final RealMatrix subMatrix = createMatrix(selectedRows.length, selectedColumns.length); subMatrix.walkInOptimizedOrder(new DefaultRealMatrixChangingVisitor() { /** {@inheritDoc} */ @Override public double visit(final int row, final int column, final double value) { return getEntry(selectedRows[row], selectedColumns[column]); } }); return subMatrix; } /** {@inheritDoc} */ public void copySubMatrix(final int startRow, final int endRow, final int startColumn, final int endColumn, final double[][] destination) throws OutOfRangeException, NumberIsTooSmallException, MatrixDimensionMismatchException { MatrixUtils.checkSubMatrixIndex(this, startRow, endRow, startColumn, endColumn); final int rowsCount = endRow + 1 - startRow; final int columnsCount = endColumn + 1 - startColumn; if ((destination.length < rowsCount) || (destination[0].length < columnsCount)) { throw new MatrixDimensionMismatchException(destination.length, destination[0].length, rowsCount, columnsCount); } for (int i = 1; i < rowsCount; i++) { if (destination[i].length < columnsCount) { throw new MatrixDimensionMismatchException(destination.length, destination[i].length, rowsCount, columnsCount); } } walkInOptimizedOrder(new DefaultRealMatrixPreservingVisitor() { /** Initial row index. */ private int startRow; /** Initial column index. */ private int startColumn; /** {@inheritDoc} */ @Override public void start(final int rows, final int columns, final int startRow, final int endRow, final int startColumn, final int endColumn) { this.startRow = startRow; this.startColumn = startColumn; } /** {@inheritDoc} */ @Override public void visit(final int row, final int column, final double value) { destination[row - startRow][column - startColumn] = value; } }, startRow, endRow, startColumn, endColumn); } /** {@inheritDoc} */ public void copySubMatrix(int[] selectedRows, int[] selectedColumns, double[][] destination) throws OutOfRangeException, NullArgumentException, NoDataException, MatrixDimensionMismatchException { MatrixUtils.checkSubMatrixIndex(this, selectedRows, selectedColumns); final int nCols = selectedColumns.length; if ((destination.length < selectedRows.length) || (destination[0].length < nCols)) { throw new MatrixDimensionMismatchException(destination.length, destination[0].length, selectedRows.length, selectedColumns.length); } for (int i = 0; i < selectedRows.length; i++) { final double[] destinationI = destination[i]; if (destinationI.length < nCols) { throw new MatrixDimensionMismatchException(destination.length, destinationI.length, selectedRows.length, selectedColumns.length); } for (int j = 0; j < selectedColumns.length; j++) { destinationI[j] = getEntry(selectedRows[i], selectedColumns[j]); } } } /** {@inheritDoc} */ public void setSubMatrix(final double[][] subMatrix, final int row, final int column) throws NoDataException, OutOfRangeException, DimensionMismatchException, NullArgumentException { MathUtils.checkNotNull(subMatrix); final int nRows = subMatrix.length; if (nRows == 0) { throw new NoDataException(LocalizedFormats.AT_LEAST_ONE_ROW); } final int nCols = subMatrix[0].length; if (nCols == 0) { throw new NoDataException(LocalizedFormats.AT_LEAST_ONE_COLUMN); } for (int r = 1; r < nRows; ++r) { if (subMatrix[r].length != nCols) { throw new DimensionMismatchException(nCols, subMatrix[r].length); } } MatrixUtils.checkRowIndex(this, row); MatrixUtils.checkColumnIndex(this, column); MatrixUtils.checkRowIndex(this, nRows + row - 1); MatrixUtils.checkColumnIndex(this, nCols + column - 1); for (int i = 0; i < nRows; ++i) { for (int j = 0; j < nCols; ++j) { setEntry(row + i, column + j, subMatrix[i][j]); } } } /** {@inheritDoc} */ public RealMatrix getRowMatrix(final int row) throws OutOfRangeException { MatrixUtils.checkRowIndex(this, row); final int nCols = getColumnDimension(); final RealMatrix out = createMatrix(1, nCols); for (int i = 0; i < nCols; ++i) { out.setEntry(0, i, getEntry(row, i)); } return out; } /** {@inheritDoc} */ public void setRowMatrix(final int row, final RealMatrix matrix) throws OutOfRangeException, MatrixDimensionMismatchException { MatrixUtils.checkRowIndex(this, row); final int nCols = getColumnDimension(); if ((matrix.getRowDimension() != 1) || (matrix.getColumnDimension() != nCols)) { throw new MatrixDimensionMismatchException(matrix.getRowDimension(), matrix.getColumnDimension(), 1, nCols); } for (int i = 0; i < nCols; ++i) { setEntry(row, i, matrix.getEntry(0, i)); } } /** {@inheritDoc} */ public RealMatrix getColumnMatrix(final int column) throws OutOfRangeException { MatrixUtils.checkColumnIndex(this, column); final int nRows = getRowDimension(); final RealMatrix out = createMatrix(nRows, 1); for (int i = 0; i < nRows; ++i) { out.setEntry(i, 0, getEntry(i, column)); } return out; } /** {@inheritDoc} */ public void setColumnMatrix(final int column, final RealMatrix matrix) throws OutOfRangeException, MatrixDimensionMismatchException { MatrixUtils.checkColumnIndex(this, column); final int nRows = getRowDimension(); if ((matrix.getRowDimension() != nRows) || (matrix.getColumnDimension() != 1)) { throw new MatrixDimensionMismatchException(matrix.getRowDimension(), matrix.getColumnDimension(), nRows, 1); } for (int i = 0; i < nRows; ++i) { setEntry(i, column, matrix.getEntry(i, 0)); } } /** {@inheritDoc} */ public RealVector getRowVector(final int row) throws OutOfRangeException { return new ArrayRealVector(getRow(row), false); } /** {@inheritDoc} */ public void setRowVector(final int row, final RealVector vector) throws OutOfRangeException, MatrixDimensionMismatchException { MatrixUtils.checkRowIndex(this, row); final int nCols = getColumnDimension(); if (vector.getDimension() != nCols) { throw new MatrixDimensionMismatchException(1, vector.getDimension(), 1, nCols); } for (int i = 0; i < nCols; ++i) { setEntry(row, i, vector.getEntry(i)); } } /** {@inheritDoc} */ public RealVector getColumnVector(final int column) throws OutOfRangeException { return new ArrayRealVector(getColumn(column), false); } /** {@inheritDoc} */ public void setColumnVector(final int column, final RealVector vector) throws OutOfRangeException, MatrixDimensionMismatchException { MatrixUtils.checkColumnIndex(this, column); final int nRows = getRowDimension(); if (vector.getDimension() != nRows) { throw new MatrixDimensionMismatchException(vector.getDimension(), 1, nRows, 1); } for (int i = 0; i < nRows; ++i) { setEntry(i, column, vector.getEntry(i)); } } /** {@inheritDoc} */ public double[] getRow(final int row) throws OutOfRangeException { MatrixUtils.checkRowIndex(this, row); final int nCols = getColumnDimension(); final double[] out = new double[nCols]; for (int i = 0; i < nCols; ++i) { out[i] = getEntry(row, i); } return out; } /** {@inheritDoc} */ public void setRow(final int row, final double[] array) throws OutOfRangeException, MatrixDimensionMismatchException { MatrixUtils.checkRowIndex(this, row); final int nCols = getColumnDimension(); if (array.length != nCols) { throw new MatrixDimensionMismatchException(1, array.length, 1, nCols); } for (int i = 0; i < nCols; ++i) { setEntry(row, i, array[i]); } } /** {@inheritDoc} */ public double[] getColumn(final int column) throws OutOfRangeException { MatrixUtils.checkColumnIndex(this, column); final int nRows = getRowDimension(); final double[] out = new double[nRows]; for (int i = 0; i < nRows; ++i) { out[i] = getEntry(i, column); } return out; } /** {@inheritDoc} */ public void setColumn(final int column, final double[] array) throws OutOfRangeException, MatrixDimensionMismatchException { MatrixUtils.checkColumnIndex(this, column); final int nRows = getRowDimension(); if (array.length != nRows) { throw new MatrixDimensionMismatchException(array.length, 1, nRows, 1); } for (int i = 0; i < nRows; ++i) { setEntry(i, column, array[i]); } } /** {@inheritDoc} */ public void addToEntry(int row, int column, double increment) throws OutOfRangeException { MatrixUtils.checkMatrixIndex(this, row, column); setEntry(row, column, getEntry(row, column) + increment); } /** {@inheritDoc} */ public void multiplyEntry(int row, int column, double factor) throws OutOfRangeException { MatrixUtils.checkMatrixIndex(this, row, column); setEntry(row, column, getEntry(row, column) * factor); } /** {@inheritDoc} */ public RealMatrix transpose() { final int nRows = getRowDimension(); final int nCols = getColumnDimension(); final RealMatrix out = createMatrix(nCols, nRows); walkInOptimizedOrder(new DefaultRealMatrixPreservingVisitor() { /** {@inheritDoc} */ @Override public void visit(final int row, final int column, final double value) { out.setEntry(column, row, value); } }); return out; } /** {@inheritDoc} */ public boolean isSquare() { return getColumnDimension() == getRowDimension(); } /** * Returns the number of rows of this matrix. * * @return the number of rows. */ @Override public abstract int getRowDimension(); /** * Returns the number of columns of this matrix. * * @return the number of columns. */ @Override public abstract int getColumnDimension(); /** {@inheritDoc} */ public double getTrace() throws NonSquareMatrixException { final int nRows = getRowDimension(); final int nCols = getColumnDimension(); if (nRows != nCols) { throw new NonSquareMatrixException(nRows, nCols); } double trace = 0; for (int i = 0; i < nRows; ++i) { trace += getEntry(i, i); } return trace; } /** {@inheritDoc} */ public double[] operate(final double[] v) throws DimensionMismatchException { final int nRows = getRowDimension(); final int nCols = getColumnDimension(); if (v.length != nCols) { throw new DimensionMismatchException(v.length, nCols); } final double[] out = new double[nRows]; for (int row = 0; row < nRows; ++row) { double sum = 0; for (int i = 0; i < nCols; ++i) { sum += getEntry(row, i) * v[i]; } out[row] = sum; } return out; } /** {@inheritDoc} */ @Override public RealVector operate(final RealVector v) throws DimensionMismatchException { try { return new ArrayRealVector(operate(((ArrayRealVector) v).getDataRef()), false); } catch (ClassCastException cce) { final int nRows = getRowDimension(); final int nCols = getColumnDimension(); if (v.getDimension() != nCols) { throw new DimensionMismatchException(v.getDimension(), nCols); } final double[] out = new double[nRows]; for (int row = 0; row < nRows; ++row) { double sum = 0; for (int i = 0; i < nCols; ++i) { sum += getEntry(row, i) * v.getEntry(i); } out[row] = sum; } return new ArrayRealVector(out, false); } } /** {@inheritDoc} */ public double[] preMultiply(final double[] v) throws DimensionMismatchException { final int nRows = getRowDimension(); final int nCols = getColumnDimension(); if (v.length != nRows) { throw new DimensionMismatchException(v.length, nRows); } final double[] out = new double[nCols]; for (int col = 0; col < nCols; ++col) { double sum = 0; for (int i = 0; i < nRows; ++i) { sum += getEntry(i, col) * v[i]; } out[col] = sum; } return out; } /** {@inheritDoc} */ public RealVector preMultiply(final RealVector v) throws DimensionMismatchException { try { return new ArrayRealVector(preMultiply(((ArrayRealVector) v).getDataRef()), false); } catch (ClassCastException cce) { final int nRows = getRowDimension(); final int nCols = getColumnDimension(); if (v.getDimension() != nRows) { throw new DimensionMismatchException(v.getDimension(), nRows); } final double[] out = new double[nCols]; for (int col = 0; col < nCols; ++col) { double sum = 0; for (int i = 0; i < nRows; ++i) { sum += getEntry(i, col) * v.getEntry(i); } out[col] = sum; } return new ArrayRealVector(out, false); } } /** {@inheritDoc} */ public double walkInRowOrder(final RealMatrixChangingVisitor visitor) { final int rows = getRowDimension(); final int columns = getColumnDimension(); visitor.start(rows, columns, 0, rows - 1, 0, columns - 1); for (int row = 0; row < rows; ++row) { for (int column = 0; column < columns; ++column) { final double oldValue = getEntry(row, column); final double newValue = visitor.visit(row, column, oldValue); setEntry(row, column, newValue); } } return visitor.end(); } /** {@inheritDoc} */ public double walkInRowOrder(final RealMatrixPreservingVisitor visitor) { final int rows = getRowDimension(); final int columns = getColumnDimension(); visitor.start(rows, columns, 0, rows - 1, 0, columns - 1); for (int row = 0; row < rows; ++row) { for (int column = 0; column < columns; ++column) { visitor.visit(row, column, getEntry(row, column)); } } return visitor.end(); } /** {@inheritDoc} */ public double walkInRowOrder(final RealMatrixChangingVisitor visitor, final int startRow, final int endRow, final int startColumn, final int endColumn) throws OutOfRangeException, NumberIsTooSmallException { MatrixUtils.checkSubMatrixIndex(this, startRow, endRow, startColumn, endColumn); visitor.start(getRowDimension(), getColumnDimension(), startRow, endRow, startColumn, endColumn); for (int row = startRow; row <= endRow; ++row) { for (int column = startColumn; column <= endColumn; ++column) { final double oldValue = getEntry(row, column); final double newValue = visitor.visit(row, column, oldValue); setEntry(row, column, newValue); } } return visitor.end(); } /** {@inheritDoc} */ public double walkInRowOrder(final RealMatrixPreservingVisitor visitor, final int startRow, final int endRow, final int startColumn, final int endColumn) throws OutOfRangeException, NumberIsTooSmallException { MatrixUtils.checkSubMatrixIndex(this, startRow, endRow, startColumn, endColumn); visitor.start(getRowDimension(), getColumnDimension(), startRow, endRow, startColumn, endColumn); for (int row = startRow; row <= endRow; ++row) { for (int column = startColumn; column <= endColumn; ++column) { visitor.visit(row, column, getEntry(row, column)); } } return visitor.end(); } /** {@inheritDoc} */ public double walkInColumnOrder(final RealMatrixChangingVisitor visitor) { final int rows = getRowDimension(); final int columns = getColumnDimension(); visitor.start(rows, columns, 0, rows - 1, 0, columns - 1); for (int column = 0; column < columns; ++column) { for (int row = 0; row < rows; ++row) { final double oldValue = getEntry(row, column); final double newValue = visitor.visit(row, column, oldValue); setEntry(row, column, newValue); } } return visitor.end(); } /** {@inheritDoc} */ public double walkInColumnOrder(final RealMatrixPreservingVisitor visitor) { final int rows = getRowDimension(); final int columns = getColumnDimension(); visitor.start(rows, columns, 0, rows - 1, 0, columns - 1); for (int column = 0; column < columns; ++column) { for (int row = 0; row < rows; ++row) { visitor.visit(row, column, getEntry(row, column)); } } return visitor.end(); } /** {@inheritDoc} */ public double walkInColumnOrder(final RealMatrixChangingVisitor visitor, final int startRow, final int endRow, final int startColumn, final int endColumn) throws OutOfRangeException, NumberIsTooSmallException { MatrixUtils.checkSubMatrixIndex(this, startRow, endRow, startColumn, endColumn); visitor.start(getRowDimension(), getColumnDimension(), startRow, endRow, startColumn, endColumn); for (int column = startColumn; column <= endColumn; ++column) { for (int row = startRow; row <= endRow; ++row) { final double oldValue = getEntry(row, column); final double newValue = visitor.visit(row, column, oldValue); setEntry(row, column, newValue); } } return visitor.end(); } /** {@inheritDoc} */ public double walkInColumnOrder(final RealMatrixPreservingVisitor visitor, final int startRow, final int endRow, final int startColumn, final int endColumn) throws OutOfRangeException, NumberIsTooSmallException { MatrixUtils.checkSubMatrixIndex(this, startRow, endRow, startColumn, endColumn); visitor.start(getRowDimension(), getColumnDimension(), startRow, endRow, startColumn, endColumn); for (int column = startColumn; column <= endColumn; ++column) { for (int row = startRow; row <= endRow; ++row) { visitor.visit(row, column, getEntry(row, column)); } } return visitor.end(); } /** {@inheritDoc} */ public double walkInOptimizedOrder(final RealMatrixChangingVisitor visitor) { return walkInRowOrder(visitor); } /** {@inheritDoc} */ public double walkInOptimizedOrder(final RealMatrixPreservingVisitor visitor) { return walkInRowOrder(visitor); } /** {@inheritDoc} */ public double walkInOptimizedOrder(final RealMatrixChangingVisitor visitor, final int startRow, final int endRow, final int startColumn, final int endColumn) throws OutOfRangeException, NumberIsTooSmallException { return walkInRowOrder(visitor, startRow, endRow, startColumn, endColumn); } /** {@inheritDoc} */ public double walkInOptimizedOrder(final RealMatrixPreservingVisitor visitor, final int startRow, final int endRow, final int startColumn, final int endColumn) throws OutOfRangeException, NumberIsTooSmallException { return walkInRowOrder(visitor, startRow, endRow, startColumn, endColumn); } /** * Get a string representation for this matrix. * @return a string representation for this matrix */ @Override public String toString() { final StringBuilder res = new StringBuilder(); String fullClassName = getClass().getName(); String shortClassName = fullClassName.substring(fullClassName.lastIndexOf('.') + 1); res.append(shortClassName); res.append(DEFAULT_FORMAT.format(this)); return res.toString(); } /** * Returns true iff object is a * RealMatrix instance with the same dimensions as this * and all corresponding matrix entries are equal. * * @param object the object to test equality against. * @return true if object equals this */ @Override public boolean equals(final Object object) { if (object == this ) { return true; } if (object instanceof RealMatrix == false) { return false; } RealMatrix m = (RealMatrix) object; final int nRows = getRowDimension(); final int nCols = getColumnDimension(); if (m.getColumnDimension() != nCols || m.getRowDimension() != nRows) { return false; } for (int row = 0; row < nRows; ++row) { for (int col = 0; col < nCols; ++col) { if (getEntry(row, col) != m.getEntry(row, col)) { return false; } } } return true; } /** * Computes a hashcode for the matrix. * * @return hashcode for matrix */ @Override public int hashCode() { int ret = 7; final int nRows = getRowDimension(); final int nCols = getColumnDimension(); ret = ret * 31 + nRows; ret = ret * 31 + nCols; for (int row = 0; row < nRows; ++row) { for (int col = 0; col < nCols; ++col) { ret = ret * 31 + (11 * (row+1) + 17 * (col+1)) * MathUtils.hash(getEntry(row, col)); } } return ret; } /* * Empty implementations of these methods are provided in order to allow for * the use of the @Override tag with Java 1.5. */ /** {@inheritDoc} */ public abstract RealMatrix createMatrix(int rowDimension, int columnDimension) throws NotStrictlyPositiveException; /** {@inheritDoc} */ public abstract RealMatrix copy(); /** {@inheritDoc} */ public abstract double getEntry(int row, int column) throws OutOfRangeException; /** {@inheritDoc} */ public abstract void setEntry(int row, int column, double value) throws OutOfRangeException; } ././@LongLink100644 0 0 145 12126630647 10262 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/linear/RealMatrixPreservingVisitor.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/linear/RealMatrixPreservingVisitor.java100644 1750 1750 4066 12126627717 32347 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.linear; /** * Interface defining a visitor for matrix entries. * * @see DefaultRealMatrixPreservingVisitor * @version $Id: RealMatrixPreservingVisitor.java 1416643 2012-12-03 19:37:14Z tn $ * @since 2.0 */ public interface RealMatrixPreservingVisitor { /** * Start visiting a matrix. *

                              This method is called once before any entry of the matrix is visited.

                              * @param rows number of rows of the matrix * @param columns number of columns of the matrix * @param startRow Initial row index * @param endRow Final row index (inclusive) * @param startColumn Initial column index * @param endColumn Final column index (inclusive) */ void start(int rows, int columns, int startRow, int endRow, int startColumn, int endColumn); /** * Visit one matrix entry. * @param row row index of the entry * @param column column index of the entry * @param value current value of the entry */ void visit(int row, int column, double value); /** * End visiting a matrix. *

                              This method is called once after all entries of the matrix have been visited.

                              * @return the value that the walkInXxxOrder must return */ double end(); } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/linear/LUDecomposition.java100644 1750 1750 32122 12126627717 27741 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.linear; import org.apache.commons.math3.exception.DimensionMismatchException; import org.apache.commons.math3.util.FastMath; /** * Calculates the LUP-decomposition of a square matrix. *

                              The LUP-decomposition of a matrix A consists of three matrices L, U and * P that satisfy: P×A = L×U. L is lower triangular (with unit * diagonal terms), U is upper triangular and P is a permutation matrix. All * matrices are m×m.

                              *

                              As shown by the presence of the P matrix, this decomposition is * implemented using partial pivoting.

                              *

                              This class is based on the class with similar name from the * JAMA library.

                              *
                                *
                              • a {@link #getP() getP} method has been added,
                              • *
                              • the {@code det} method has been renamed as {@link #getDeterminant() * getDeterminant},
                              • *
                              • the {@code getDoublePivot} method has been removed (but the int based * {@link #getPivot() getPivot} method has been kept),
                              • *
                              • the {@code solve} and {@code isNonSingular} methods have been replaced * by a {@link #getSolver() getSolver} method and the equivalent methods * provided by the returned {@link DecompositionSolver}.
                              • *
                              * * @see MathWorld * @see Wikipedia * @version $Id: LUDecomposition.java 1416643 2012-12-03 19:37:14Z tn $ * @since 2.0 (changed to concrete class in 3.0) */ public class LUDecomposition { /** Default bound to determine effective singularity in LU decomposition. */ private static final double DEFAULT_TOO_SMALL = 1e-11; /** Entries of LU decomposition. */ private final double[][] lu; /** Pivot permutation associated with LU decomposition. */ private final int[] pivot; /** Parity of the permutation associated with the LU decomposition. */ private boolean even; /** Singularity indicator. */ private boolean singular; /** Cached value of L. */ private RealMatrix cachedL; /** Cached value of U. */ private RealMatrix cachedU; /** Cached value of P. */ private RealMatrix cachedP; /** * Calculates the LU-decomposition of the given matrix. * This constructor uses 1e-11 as default value for the singularity * threshold. * * @param matrix Matrix to decompose. * @throws NonSquareMatrixException if matrix is not square. */ public LUDecomposition(RealMatrix matrix) { this(matrix, DEFAULT_TOO_SMALL); } /** * Calculates the LU-decomposition of the given matrix. * @param matrix The matrix to decompose. * @param singularityThreshold threshold (based on partial row norm) * under which a matrix is considered singular * @throws NonSquareMatrixException if matrix is not square */ public LUDecomposition(RealMatrix matrix, double singularityThreshold) { if (!matrix.isSquare()) { throw new NonSquareMatrixException(matrix.getRowDimension(), matrix.getColumnDimension()); } final int m = matrix.getColumnDimension(); lu = matrix.getData(); pivot = new int[m]; cachedL = null; cachedU = null; cachedP = null; // Initialize permutation array and parity for (int row = 0; row < m; row++) { pivot[row] = row; } even = true; singular = false; // Loop over columns for (int col = 0; col < m; col++) { // upper for (int row = 0; row < col; row++) { final double[] luRow = lu[row]; double sum = luRow[col]; for (int i = 0; i < row; i++) { sum -= luRow[i] * lu[i][col]; } luRow[col] = sum; } // lower int max = col; // permutation row double largest = Double.NEGATIVE_INFINITY; for (int row = col; row < m; row++) { final double[] luRow = lu[row]; double sum = luRow[col]; for (int i = 0; i < col; i++) { sum -= luRow[i] * lu[i][col]; } luRow[col] = sum; // maintain best permutation choice if (FastMath.abs(sum) > largest) { largest = FastMath.abs(sum); max = row; } } // Singularity check if (FastMath.abs(lu[max][col]) < singularityThreshold) { singular = true; return; } // Pivot if necessary if (max != col) { double tmp = 0; final double[] luMax = lu[max]; final double[] luCol = lu[col]; for (int i = 0; i < m; i++) { tmp = luMax[i]; luMax[i] = luCol[i]; luCol[i] = tmp; } int temp = pivot[max]; pivot[max] = pivot[col]; pivot[col] = temp; even = !even; } // Divide the lower elements by the "winning" diagonal elt. final double luDiag = lu[col][col]; for (int row = col + 1; row < m; row++) { lu[row][col] /= luDiag; } } } /** * Returns the matrix L of the decomposition. *

                              L is a lower-triangular matrix

                              * @return the L matrix (or null if decomposed matrix is singular) */ public RealMatrix getL() { if ((cachedL == null) && !singular) { final int m = pivot.length; cachedL = MatrixUtils.createRealMatrix(m, m); for (int i = 0; i < m; ++i) { final double[] luI = lu[i]; for (int j = 0; j < i; ++j) { cachedL.setEntry(i, j, luI[j]); } cachedL.setEntry(i, i, 1.0); } } return cachedL; } /** * Returns the matrix U of the decomposition. *

                              U is an upper-triangular matrix

                              * @return the U matrix (or null if decomposed matrix is singular) */ public RealMatrix getU() { if ((cachedU == null) && !singular) { final int m = pivot.length; cachedU = MatrixUtils.createRealMatrix(m, m); for (int i = 0; i < m; ++i) { final double[] luI = lu[i]; for (int j = i; j < m; ++j) { cachedU.setEntry(i, j, luI[j]); } } } return cachedU; } /** * Returns the P rows permutation matrix. *

                              P is a sparse matrix with exactly one element set to 1.0 in * each row and each column, all other elements being set to 0.0.

                              *

                              The positions of the 1 elements are given by the {@link #getPivot() * pivot permutation vector}.

                              * @return the P rows permutation matrix (or null if decomposed matrix is singular) * @see #getPivot() */ public RealMatrix getP() { if ((cachedP == null) && !singular) { final int m = pivot.length; cachedP = MatrixUtils.createRealMatrix(m, m); for (int i = 0; i < m; ++i) { cachedP.setEntry(i, pivot[i], 1.0); } } return cachedP; } /** * Returns the pivot permutation vector. * @return the pivot permutation vector * @see #getP() */ public int[] getPivot() { return pivot.clone(); } /** * Return the determinant of the matrix * @return determinant of the matrix */ public double getDeterminant() { if (singular) { return 0; } else { final int m = pivot.length; double determinant = even ? 1 : -1; for (int i = 0; i < m; i++) { determinant *= lu[i][i]; } return determinant; } } /** * Get a solver for finding the A × X = B solution in exact linear * sense. * @return a solver */ public DecompositionSolver getSolver() { return new Solver(lu, pivot, singular); } /** Specialized solver. */ private static class Solver implements DecompositionSolver { /** Entries of LU decomposition. */ private final double[][] lu; /** Pivot permutation associated with LU decomposition. */ private final int[] pivot; /** Singularity indicator. */ private final boolean singular; /** * Build a solver from decomposed matrix. * @param lu entries of LU decomposition * @param pivot pivot permutation associated with LU decomposition * @param singular singularity indicator */ private Solver(final double[][] lu, final int[] pivot, final boolean singular) { this.lu = lu; this.pivot = pivot; this.singular = singular; } /** {@inheritDoc} */ public boolean isNonSingular() { return !singular; } /** {@inheritDoc} */ public RealVector solve(RealVector b) { final int m = pivot.length; if (b.getDimension() != m) { throw new DimensionMismatchException(b.getDimension(), m); } if (singular) { throw new SingularMatrixException(); } final double[] bp = new double[m]; // Apply permutations to b for (int row = 0; row < m; row++) { bp[row] = b.getEntry(pivot[row]); } // Solve LY = b for (int col = 0; col < m; col++) { final double bpCol = bp[col]; for (int i = col + 1; i < m; i++) { bp[i] -= bpCol * lu[i][col]; } } // Solve UX = Y for (int col = m - 1; col >= 0; col--) { bp[col] /= lu[col][col]; final double bpCol = bp[col]; for (int i = 0; i < col; i++) { bp[i] -= bpCol * lu[i][col]; } } return new ArrayRealVector(bp, false); } /** {@inheritDoc} */ public RealMatrix solve(RealMatrix b) { final int m = pivot.length; if (b.getRowDimension() != m) { throw new DimensionMismatchException(b.getRowDimension(), m); } if (singular) { throw new SingularMatrixException(); } final int nColB = b.getColumnDimension(); // Apply permutations to b final double[][] bp = new double[m][nColB]; for (int row = 0; row < m; row++) { final double[] bpRow = bp[row]; final int pRow = pivot[row]; for (int col = 0; col < nColB; col++) { bpRow[col] = b.getEntry(pRow, col); } } // Solve LY = b for (int col = 0; col < m; col++) { final double[] bpCol = bp[col]; for (int i = col + 1; i < m; i++) { final double[] bpI = bp[i]; final double luICol = lu[i][col]; for (int j = 0; j < nColB; j++) { bpI[j] -= bpCol[j] * luICol; } } } // Solve UX = Y for (int col = m - 1; col >= 0; col--) { final double[] bpCol = bp[col]; final double luDiag = lu[col][col]; for (int j = 0; j < nColB; j++) { bpCol[j] /= luDiag; } for (int i = 0; i < col; i++) { final double[] bpI = bp[i]; final double luICol = lu[i][col]; for (int j = 0; j < nColB; j++) { bpI[j] -= bpCol[j] * luICol; } } } return new Array2DRowRealMatrix(bp, false); } /** {@inheritDoc} */ public RealMatrix getInverse() { return solve(MatrixUtils.createRealIdentityMatrix(pivot.length)); } } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/linear/FieldDecompositionSolver.java100644 1750 1750 6416 12126627717 31626 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.linear; import org.apache.commons.math3.FieldElement; /** * Interface handling decomposition algorithms that can solve A × X = B. *

                              Decomposition algorithms decompose an A matrix has a product of several specific * matrices from which they can solve A × X = B in least squares sense: they find X * such that ||A × X - B|| is minimal.

                              *

                              Some solvers like {@link FieldLUDecomposition} can only find the solution for * square matrices and when the solution is an exact linear solution, i.e. when * ||A × X - B|| is exactly 0. Other solvers can also find solutions * with non-square matrix A and with non-null minimal norm. If an exact linear * solution exists it is also the minimal norm solution.

                              * * @param the type of the field elements * @version $Id: FieldDecompositionSolver.java 1416643 2012-12-03 19:37:14Z tn $ * @since 2.0 */ public interface FieldDecompositionSolver> { /** Solve the linear equation A × X = B for matrices A. *

                              The A matrix is implicit, it is provided by the underlying * decomposition algorithm.

                              * @param b right-hand side of the equation A × X = B * @return a vector X that minimizes the two norm of A × X - B * @throws org.apache.commons.math3.exception.DimensionMismatchException * if the matrices dimensions do not match. * @throws SingularMatrixException * if the decomposed matrix is singular. */ FieldVector solve(final FieldVector b); /** Solve the linear equation A × X = B for matrices A. *

                              The A matrix is implicit, it is provided by the underlying * decomposition algorithm.

                              * @param b right-hand side of the equation A × X = B * @return a matrix X that minimizes the two norm of A × X - B * @throws org.apache.commons.math3.exception.DimensionMismatchException * if the matrices dimensions do not match. * @throws SingularMatrixException * if the decomposed matrix is singular. */ FieldMatrix solve(final FieldMatrix b); /** * Check if the decomposed matrix is non-singular. * @return true if the decomposed matrix is non-singular */ boolean isNonSingular(); /** Get the inverse (or pseudo-inverse) of the decomposed matrix. * @return inverse matrix * @throws SingularMatrixException * if the decomposed matrix is singular. */ FieldMatrix getInverse(); } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/linear/JacobiPreconditioner.java100644 1750 1750 12002 12126627717 30753 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.linear; import org.apache.commons.math3.analysis.function.Sqrt; import org.apache.commons.math3.util.MathArrays; /** * This class implements the standard Jacobi (diagonal) preconditioner. For a * matrix Aij, this preconditioner is * M = diag(1 / A11, 1 / A22, …). * * @version $Id: JacobiPreconditioner.java 1422195 2012-12-15 06:45:18Z psteitz $ * @since 3.0 */ public class JacobiPreconditioner extends RealLinearOperator { /** The diagonal coefficients of the preconditioner. */ private final ArrayRealVector diag; /** * Creates a new instance of this class. * * @param diag the diagonal coefficients of the linear operator to be * preconditioned * @param deep {@code true} if a deep copy of the above array should be * performed */ public JacobiPreconditioner(final double[] diag, final boolean deep) { this.diag = new ArrayRealVector(diag, deep); } /** * Creates a new instance of this class. This method extracts the diagonal * coefficients of the specified linear operator. If {@code a} does not * extend {@link AbstractRealMatrix}, then the coefficients of the * underlying matrix are not accessible, coefficient extraction is made by * matrix-vector products with the basis vectors (and might therefore take * some time). With matrices, direct entry access is carried out. * * @param a the linear operator for which the preconditioner should be built * @return the diagonal preconditioner made of the inverse of the diagonal * coefficients of the specified linear operator * @throws NonSquareOperatorException if {@code a} is not square */ public static JacobiPreconditioner create(final RealLinearOperator a) throws NonSquareOperatorException { final int n = a.getColumnDimension(); if (a.getRowDimension() != n) { throw new NonSquareOperatorException(a.getRowDimension(), n); } final double[] diag = new double[n]; if (a instanceof AbstractRealMatrix) { final AbstractRealMatrix m = (AbstractRealMatrix) a; for (int i = 0; i < n; i++) { diag[i] = m.getEntry(i, i); } } else { final ArrayRealVector x = new ArrayRealVector(n); for (int i = 0; i < n; i++) { x.set(0.); x.setEntry(i, 1.); diag[i] = a.operate(x).getEntry(i); } } return new JacobiPreconditioner(diag, false); } /** {@inheritDoc} */ @Override public int getColumnDimension() { return diag.getDimension(); } /** {@inheritDoc} */ @Override public int getRowDimension() { return diag.getDimension(); } /** {@inheritDoc} */ @Override public RealVector operate(final RealVector x) { // Dimension check is carried out by ebeDivide return new ArrayRealVector(MathArrays.ebeDivide(x.toArray(), diag.toArray()), false); } /** * Returns the square root of {@code this} diagonal operator. More * precisely, this method returns * P = diag(1 / √A11, 1 / √A22, …). * * @return the square root of {@code this} preconditioner * @since 3.1 */ public RealLinearOperator sqrt() { final RealVector sqrtDiag = diag.map(new Sqrt()); return new RealLinearOperator() { /** {@inheritDoc} */ @Override public RealVector operate(final RealVector x) { return new ArrayRealVector(MathArrays.ebeDivide(x.toArray(), sqrtDiag.toArray()), false); } /** {@inheritDoc} */ @Override public int getRowDimension() { return sqrtDiag.getDimension(); } /** {@inheritDoc} */ @Override public int getColumnDimension() { return sqrtDiag.getDimension(); } }; } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/linear/RealMatrix.java100644 1750 1750 111756 12126627717 26767 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.linear; import org.apache.commons.math3.exception.DimensionMismatchException; import org.apache.commons.math3.exception.NoDataException; import org.apache.commons.math3.exception.NotPositiveException; import org.apache.commons.math3.exception.NotStrictlyPositiveException; import org.apache.commons.math3.exception.NullArgumentException; import org.apache.commons.math3.exception.NumberIsTooSmallException; import org.apache.commons.math3.exception.OutOfRangeException; /** * Interface defining a real-valued matrix with basic algebraic operations. *

                              * Matrix element indexing is 0-based -- e.g., getEntry(0, 0) * returns the element in the first row, first column of the matrix.

                              * * @version $Id: RealMatrix.java 1416643 2012-12-03 19:37:14Z tn $ */ public interface RealMatrix extends AnyMatrix { /** * Create a new RealMatrix of the same type as the instance with the * supplied * row and column dimensions. * * @param rowDimension the number of rows in the new matrix * @param columnDimension the number of columns in the new matrix * @return a new matrix of the same type as the instance * @throws NotStrictlyPositiveException if row or column dimension is not * positive. * @since 2.0 */ RealMatrix createMatrix(int rowDimension, int columnDimension) throws NotStrictlyPositiveException; /** * Returns a (deep) copy of this. * * @return matrix copy */ RealMatrix copy(); /** * Returns the sum of {@code this} and {@code m}. * * @param m matrix to be added * @return {@code this + m} * @throws MatrixDimensionMismatchException if {@code m} is not the same * size as {@code this}. */ RealMatrix add(RealMatrix m) throws MatrixDimensionMismatchException; /** * Returns {@code this} minus {@code m}. * * @param m matrix to be subtracted * @return {@code this - m} * @throws MatrixDimensionMismatchException if {@code m} is not the same * size as {@code this}. */ RealMatrix subtract(RealMatrix m) throws MatrixDimensionMismatchException; /** * Returns the result of adding {@code d} to each entry of {@code this}. * * @param d value to be added to each entry * @return {@code d + this} */ RealMatrix scalarAdd(double d); /** * Returns the result of multiplying each entry of {@code this} by * {@code d}. * * @param d value to multiply all entries by * @return {@code d * this} */ RealMatrix scalarMultiply(double d); /** * Returns the result of postmultiplying {@code this} by {@code m}. * * @param m matrix to postmultiply by * @return {@code this * m} * @throws DimensionMismatchException if * {@code columnDimension(this) != rowDimension(m)} */ RealMatrix multiply(RealMatrix m) throws DimensionMismatchException; /** * Returns the result of premultiplying {@code this} by {@code m}. * * @param m matrix to premultiply by * @return {@code m * this} * @throws DimensionMismatchException if * {@code rowDimension(this) != columnDimension(m)} */ RealMatrix preMultiply(RealMatrix m) throws DimensionMismatchException; /** * Returns the result of multiplying {@code this} with itself {@code p} * times. Depending on the underlying storage, instability for high powers * might occur. * * @param p raise {@code this} to power {@code p} * @return {@code this^p} * @throws NotPositiveException if {@code p < 0} * @throws NonSquareMatrixException if the matrix is not square */ RealMatrix power(final int p) throws NotPositiveException, NonSquareMatrixException; /** * Returns matrix entries as a two-dimensional array. * * @return 2-dimensional array of entries */ double[][] getData(); /** * Returns the * maximum absolute row sum norm of the matrix. * * @return norm */ double getNorm(); /** * Returns the * Frobenius norm of the matrix. * * @return norm */ double getFrobeniusNorm(); /** * Gets a submatrix. Rows and columns are indicated * counting from 0 to n-1. * * @param startRow Initial row index * @param endRow Final row index (inclusive) * @param startColumn Initial column index * @param endColumn Final column index (inclusive) * @return The subMatrix containing the data of the * specified rows and columns. * @throws OutOfRangeException if the indices are not valid. * @throws NumberIsTooSmallException if {@code endRow < startRow} or * {@code endColumn < startColumn}. */ RealMatrix getSubMatrix(int startRow, int endRow, int startColumn, int endColumn) throws OutOfRangeException, NumberIsTooSmallException; /** * Gets a submatrix. Rows and columns are indicated counting from 0 to n-1. * * @param selectedRows Array of row indices. * @param selectedColumns Array of column indices. * @return The subMatrix containing the data in the specified rows and * columns * @throws NullArgumentException if the row or column selections are * {@code null} * @throws NoDataException if the row or column selections are empty (zero * length). * @throws OutOfRangeException if the indices are not valid. */ RealMatrix getSubMatrix(int[] selectedRows, int[] selectedColumns) throws NullArgumentException, NoDataException, OutOfRangeException; /** * Copy a submatrix. Rows and columns are indicated counting from 0 to n-1. * * @param startRow Initial row index * @param endRow Final row index (inclusive) * @param startColumn Initial column index * @param endColumn Final column index (inclusive) * @param destination The arrays where the submatrix data should be copied * (if larger than rows/columns counts, only the upper-left part will be * used) * @throws OutOfRangeException if the indices are not valid. * @throws NumberIsTooSmallException if {@code endRow < startRow} or * {@code endColumn < startColumn}. * @throws MatrixDimensionMismatchException if the destination array is too * small. */ void copySubMatrix(int startRow, int endRow, int startColumn, int endColumn, double[][] destination) throws OutOfRangeException, NumberIsTooSmallException, MatrixDimensionMismatchException; /** * Copy a submatrix. Rows and columns are indicated counting from 0 to n-1. * * @param selectedRows Array of row indices. * @param selectedColumns Array of column indices. * @param destination The arrays where the submatrix data should be copied * (if larger than rows/columns counts, only the upper-left part will be * used) * @throws NullArgumentException if the row or column selections are * {@code null} * @throws NoDataException if the row or column selections are empty (zero * length). * @throws OutOfRangeException if the indices are not valid. * @throws MatrixDimensionMismatchException if the destination array is too * small. */ void copySubMatrix(int[] selectedRows, int[] selectedColumns, double[][] destination) throws OutOfRangeException, NullArgumentException, NoDataException, MatrixDimensionMismatchException; /** * Replace the submatrix starting at {@code row, column} using data in the * input {@code subMatrix} array. Indexes are 0-based. *

                              * Example:
                              * Starting with

                                  * 1  2  3  4
                                  * 5  6  7  8
                                  * 9  0  1  2
                                  * 
                              * and subMatrix = {{3, 4} {5,6}}, invoking * {@code setSubMatrix(subMatrix,1,1))} will result in
                                  * 1  2  3  4
                                  * 5  3  4  8
                                  * 9  5  6  2
                                  * 

                              * * @param subMatrix array containing the submatrix replacement data * @param row row coordinate of the top, left element to be replaced * @param column column coordinate of the top, left element to be replaced * @throws NoDataException if {@code subMatrix} is empty. * @throws OutOfRangeException if {@code subMatrix} does not fit into * this matrix from element in {@code (row, column)}. * @throws DimensionMismatchException if {@code subMatrix} is not rectangular * (not all rows have the same length) or empty. * @throws NullArgumentException if {@code subMatrix} is {@code null}. * @since 2.0 */ void setSubMatrix(double[][] subMatrix, int row, int column) throws NoDataException, OutOfRangeException, DimensionMismatchException, NullArgumentException; /** * Get the entries at the given row index as a row matrix. Row indices start * at 0. * * @param row Row to be fetched. * @return row Matrix. * @throws OutOfRangeException if the specified row index is invalid. */ RealMatrix getRowMatrix(int row) throws OutOfRangeException; /** * Sets the specified {@code row} of {@code this} matrix to the entries of * the specified row {@code matrix}. Row indices start at 0. * * @param row Row to be set. * @param matrix Row matrix to be copied (must have one row and the same * number of columns as the instance). * @throws OutOfRangeException if the specified row index is invalid. * @throws MatrixDimensionMismatchException if the row dimension of the * {@code matrix} is not {@code 1}, or the column dimensions of {@code this} * and {@code matrix} do not match. */ void setRowMatrix(int row, RealMatrix matrix) throws OutOfRangeException, MatrixDimensionMismatchException; /** * Get the entries at the given column index as a column matrix. Column * indices start at 0. * * @param column Column to be fetched. * @return column Matrix. * @throws OutOfRangeException if the specified column index is invalid. */ RealMatrix getColumnMatrix(int column) throws OutOfRangeException; /** * Sets the specified {@code column} of {@code this} matrix to the entries * of the specified column {@code matrix}. Column indices start at 0. * * @param column Column to be set. * @param matrix Column matrix to be copied (must have one column and the * same number of rows as the instance). * @throws OutOfRangeException if the specified column index is invalid. * @throws MatrixDimensionMismatchException if the column dimension of the * {@code matrix} is not {@code 1}, or the row dimensions of {@code this} * and {@code matrix} do not match. */ void setColumnMatrix(int column, RealMatrix matrix) throws OutOfRangeException, MatrixDimensionMismatchException; /** * Returns the entries in row number {@code row} as a vector. Row indices * start at 0. * * @param row Row to be fetched. * @return a row vector. * @throws OutOfRangeException if the specified row index is invalid. */ RealVector getRowVector(int row) throws OutOfRangeException; /** * Sets the specified {@code row} of {@code this} matrix to the entries of * the specified {@code vector}. Row indices start at 0. * * @param row Row to be set. * @param vector row vector to be copied (must have the same number of * column as the instance). * @throws OutOfRangeException if the specified row index is invalid. * @throws MatrixDimensionMismatchException if the {@code vector} dimension * does not match the column dimension of {@code this} matrix. */ void setRowVector(int row, RealVector vector) throws OutOfRangeException, MatrixDimensionMismatchException; /** * Get the entries at the given column index as a vector. Column indices * start at 0. * * @param column Column to be fetched. * @return a column vector. * @throws OutOfRangeException if the specified column index is invalid */ RealVector getColumnVector(int column) throws OutOfRangeException; /** * Sets the specified {@code column} of {@code this} matrix to the entries * of the specified {@code vector}. Column indices start at 0. * * @param column Column to be set. * @param vector column vector to be copied (must have the same number of * rows as the instance). * @throws OutOfRangeException if the specified column index is invalid. * @throws MatrixDimensionMismatchException if the {@code vector} dimension * does not match the row dimension of {@code this} matrix. */ void setColumnVector(int column, RealVector vector) throws OutOfRangeException, MatrixDimensionMismatchException; /** * Get the entries at the given row index. Row indices start at 0. * * @param row Row to be fetched. * @return the array of entries in the row. * @throws OutOfRangeException if the specified row index is not valid. */ double[] getRow(int row) throws OutOfRangeException; /** * Sets the specified {@code row} of {@code this} matrix to the entries * of the specified {@code array}. Row indices start at 0. * * @param row Row to be set. * @param array Row matrix to be copied (must have the same number of * columns as the instance) * @throws OutOfRangeException if the specified row index is invalid. * @throws MatrixDimensionMismatchException if the {@code array} length does * not match the column dimension of {@code this} matrix. */ void setRow(int row, double[] array) throws OutOfRangeException, MatrixDimensionMismatchException; /** * Get the entries at the given column index as an array. Column indices * start at 0. * * @param column Column to be fetched. * @return the array of entries in the column. * @throws OutOfRangeException if the specified column index is not valid. */ double[] getColumn(int column) throws OutOfRangeException; /** * Sets the specified {@code column} of {@code this} matrix to the entries * of the specified {@code array}. Column indices start at 0. * * @param column Column to be set. * @param array Column array to be copied (must have the same number of * rows as the instance). * @throws OutOfRangeException if the specified column index is invalid. * @throws MatrixDimensionMismatchException if the {@code array} length does * not match the row dimension of {@code this} matrix. */ void setColumn(int column, double[] array) throws OutOfRangeException, MatrixDimensionMismatchException; /** * Get the entry in the specified row and column. Row and column indices * start at 0. * * @param row Row index of entry to be fetched. * @param column Column index of entry to be fetched. * @return the matrix entry at {@code (row, column)}. * @throws OutOfRangeException if the row or column index is not valid. */ double getEntry(int row, int column) throws OutOfRangeException; /** * Set the entry in the specified row and column. Row and column indices * start at 0. * * @param row Row index of entry to be set. * @param column Column index of entry to be set. * @param value the new value of the entry. * @throws OutOfRangeException if the row or column index is not valid * @since 2.0 */ void setEntry(int row, int column, double value) throws OutOfRangeException; /** * Adds (in place) the specified value to the specified entry of * {@code this} matrix. Row and column indices start at 0. * * @param row Row index of the entry to be modified. * @param column Column index of the entry to be modified. * @param increment value to add to the matrix entry. * @throws OutOfRangeException if the row or column index is not valid. * @since 2.0 */ void addToEntry(int row, int column, double increment) throws OutOfRangeException; /** * Multiplies (in place) the specified entry of {@code this} matrix by the * specified value. Row and column indices start at 0. * * @param row Row index of the entry to be modified. * @param column Column index of the entry to be modified. * @param factor Multiplication factor for the matrix entry. * @throws OutOfRangeException if the row or column index is not valid. * @since 2.0 */ void multiplyEntry(int row, int column, double factor) throws OutOfRangeException; /** * Returns the transpose of this matrix. * * @return transpose matrix */ RealMatrix transpose(); /** * Returns the * trace of the matrix (the sum of the elements on the main diagonal). * * @return the trace. * @throws NonSquareMatrixException if the matrix is not square. */ double getTrace() throws NonSquareMatrixException; /** * Returns the result of multiplying this by the vector {@code v}. * * @param v the vector to operate on * @return {@code this * v} * @throws DimensionMismatchException if the length of {@code v} does not * match the column dimension of {@code this}. */ double[] operate(double[] v) throws DimensionMismatchException; /** * Returns the result of multiplying this by the vector {@code v}. * * @param v the vector to operate on * @return {@code this * v} * @throws DimensionMismatchException if the dimension of {@code v} does not * match the column dimension of {@code this}. */ RealVector operate(RealVector v) throws DimensionMismatchException; /** * Returns the (row) vector result of premultiplying this by the vector {@code v}. * * @param v the row vector to premultiply by * @return {@code v * this} * @throws DimensionMismatchException if the length of {@code v} does not * match the row dimension of {@code this}. */ double[] preMultiply(double[] v) throws DimensionMismatchException; /** * Returns the (row) vector result of premultiplying this by the vector {@code v}. * * @param v the row vector to premultiply by * @return {@code v * this} * @throws DimensionMismatchException if the dimension of {@code v} does not * match the row dimension of {@code this}. */ RealVector preMultiply(RealVector v) throws DimensionMismatchException; /** * Visit (and possibly change) all matrix entries in row order. *

                              Row order starts at upper left and iterating through all elements * of a row from left to right before going to the leftmost element * of the next row.

                              * @param visitor visitor used to process all matrix entries * @see #walkInRowOrder(RealMatrixPreservingVisitor) * @see #walkInRowOrder(RealMatrixChangingVisitor, int, int, int, int) * @see #walkInRowOrder(RealMatrixPreservingVisitor, int, int, int, int) * @see #walkInColumnOrder(RealMatrixChangingVisitor) * @see #walkInColumnOrder(RealMatrixPreservingVisitor) * @see #walkInColumnOrder(RealMatrixChangingVisitor, int, int, int, int) * @see #walkInColumnOrder(RealMatrixPreservingVisitor, int, int, int, int) * @see #walkInOptimizedOrder(RealMatrixChangingVisitor) * @see #walkInOptimizedOrder(RealMatrixPreservingVisitor) * @see #walkInOptimizedOrder(RealMatrixChangingVisitor, int, int, int, int) * @see #walkInOptimizedOrder(RealMatrixPreservingVisitor, int, int, int, int) * @return the value returned by {@link RealMatrixChangingVisitor#end()} at the end * of the walk */ double walkInRowOrder(RealMatrixChangingVisitor visitor); /** * Visit (but don't change) all matrix entries in row order. *

                              Row order starts at upper left and iterating through all elements * of a row from left to right before going to the leftmost element * of the next row.

                              * @param visitor visitor used to process all matrix entries * @see #walkInRowOrder(RealMatrixChangingVisitor) * @see #walkInRowOrder(RealMatrixChangingVisitor, int, int, int, int) * @see #walkInRowOrder(RealMatrixPreservingVisitor, int, int, int, int) * @see #walkInColumnOrder(RealMatrixChangingVisitor) * @see #walkInColumnOrder(RealMatrixPreservingVisitor) * @see #walkInColumnOrder(RealMatrixChangingVisitor, int, int, int, int) * @see #walkInColumnOrder(RealMatrixPreservingVisitor, int, int, int, int) * @see #walkInOptimizedOrder(RealMatrixChangingVisitor) * @see #walkInOptimizedOrder(RealMatrixPreservingVisitor) * @see #walkInOptimizedOrder(RealMatrixChangingVisitor, int, int, int, int) * @see #walkInOptimizedOrder(RealMatrixPreservingVisitor, int, int, int, int) * @return the value returned by {@link RealMatrixPreservingVisitor#end()} at the end * of the walk */ double walkInRowOrder(RealMatrixPreservingVisitor visitor); /** * Visit (and possibly change) some matrix entries in row order. *

                              Row order starts at upper left and iterating through all elements * of a row from left to right before going to the leftmost element * of the next row.

                              * @param visitor visitor used to process all matrix entries * @param startRow Initial row index * @param endRow Final row index (inclusive) * @param startColumn Initial column index * @param endColumn Final column index * @throws OutOfRangeException if the indices are not valid. * @throws NumberIsTooSmallException if {@code endRow < startRow} or * {@code endColumn < startColumn}. * @see #walkInRowOrder(RealMatrixChangingVisitor) * @see #walkInRowOrder(RealMatrixPreservingVisitor) * @see #walkInRowOrder(RealMatrixPreservingVisitor, int, int, int, int) * @see #walkInColumnOrder(RealMatrixChangingVisitor) * @see #walkInColumnOrder(RealMatrixPreservingVisitor) * @see #walkInColumnOrder(RealMatrixChangingVisitor, int, int, int, int) * @see #walkInColumnOrder(RealMatrixPreservingVisitor, int, int, int, int) * @see #walkInOptimizedOrder(RealMatrixChangingVisitor) * @see #walkInOptimizedOrder(RealMatrixPreservingVisitor) * @see #walkInOptimizedOrder(RealMatrixChangingVisitor, int, int, int, int) * @see #walkInOptimizedOrder(RealMatrixPreservingVisitor, int, int, int, int) * @return the value returned by {@link RealMatrixChangingVisitor#end()} at the end * of the walk */ double walkInRowOrder(RealMatrixChangingVisitor visitor, int startRow, int endRow, int startColumn, int endColumn) throws OutOfRangeException, NumberIsTooSmallException; /** * Visit (but don't change) some matrix entries in row order. *

                              Row order starts at upper left and iterating through all elements * of a row from left to right before going to the leftmost element * of the next row.

                              * @param visitor visitor used to process all matrix entries * @param startRow Initial row index * @param endRow Final row index (inclusive) * @param startColumn Initial column index * @param endColumn Final column index * @throws OutOfRangeException if the indices are not valid. * @throws NumberIsTooSmallException if {@code endRow < startRow} or * {@code endColumn < startColumn}. * @see #walkInRowOrder(RealMatrixChangingVisitor) * @see #walkInRowOrder(RealMatrixPreservingVisitor) * @see #walkInRowOrder(RealMatrixChangingVisitor, int, int, int, int) * @see #walkInColumnOrder(RealMatrixChangingVisitor) * @see #walkInColumnOrder(RealMatrixPreservingVisitor) * @see #walkInColumnOrder(RealMatrixChangingVisitor, int, int, int, int) * @see #walkInColumnOrder(RealMatrixPreservingVisitor, int, int, int, int) * @see #walkInOptimizedOrder(RealMatrixChangingVisitor) * @see #walkInOptimizedOrder(RealMatrixPreservingVisitor) * @see #walkInOptimizedOrder(RealMatrixChangingVisitor, int, int, int, int) * @see #walkInOptimizedOrder(RealMatrixPreservingVisitor, int, int, int, int) * @return the value returned by {@link RealMatrixPreservingVisitor#end()} at the end * of the walk */ double walkInRowOrder(RealMatrixPreservingVisitor visitor, int startRow, int endRow, int startColumn, int endColumn) throws OutOfRangeException, NumberIsTooSmallException; /** * Visit (and possibly change) all matrix entries in column order. *

                              Column order starts at upper left and iterating through all elements * of a column from top to bottom before going to the topmost element * of the next column.

                              * @param visitor visitor used to process all matrix entries * @see #walkInRowOrder(RealMatrixChangingVisitor) * @see #walkInRowOrder(RealMatrixPreservingVisitor) * @see #walkInRowOrder(RealMatrixChangingVisitor, int, int, int, int) * @see #walkInRowOrder(RealMatrixPreservingVisitor, int, int, int, int) * @see #walkInColumnOrder(RealMatrixPreservingVisitor) * @see #walkInColumnOrder(RealMatrixChangingVisitor, int, int, int, int) * @see #walkInColumnOrder(RealMatrixPreservingVisitor, int, int, int, int) * @see #walkInOptimizedOrder(RealMatrixChangingVisitor) * @see #walkInOptimizedOrder(RealMatrixPreservingVisitor) * @see #walkInOptimizedOrder(RealMatrixChangingVisitor, int, int, int, int) * @see #walkInOptimizedOrder(RealMatrixPreservingVisitor, int, int, int, int) * @return the value returned by {@link RealMatrixChangingVisitor#end()} at the end * of the walk */ double walkInColumnOrder(RealMatrixChangingVisitor visitor); /** * Visit (but don't change) all matrix entries in column order. *

                              Column order starts at upper left and iterating through all elements * of a column from top to bottom before going to the topmost element * of the next column.

                              * @param visitor visitor used to process all matrix entries * @see #walkInRowOrder(RealMatrixChangingVisitor) * @see #walkInRowOrder(RealMatrixPreservingVisitor) * @see #walkInRowOrder(RealMatrixChangingVisitor, int, int, int, int) * @see #walkInRowOrder(RealMatrixPreservingVisitor, int, int, int, int) * @see #walkInColumnOrder(RealMatrixChangingVisitor) * @see #walkInColumnOrder(RealMatrixChangingVisitor, int, int, int, int) * @see #walkInColumnOrder(RealMatrixPreservingVisitor, int, int, int, int) * @see #walkInOptimizedOrder(RealMatrixChangingVisitor) * @see #walkInOptimizedOrder(RealMatrixPreservingVisitor) * @see #walkInOptimizedOrder(RealMatrixChangingVisitor, int, int, int, int) * @see #walkInOptimizedOrder(RealMatrixPreservingVisitor, int, int, int, int) * @return the value returned by {@link RealMatrixPreservingVisitor#end()} at the end * of the walk */ double walkInColumnOrder(RealMatrixPreservingVisitor visitor); /** * Visit (and possibly change) some matrix entries in column order. *

                              Column order starts at upper left and iterating through all elements * of a column from top to bottom before going to the topmost element * of the next column.

                              * @param visitor visitor used to process all matrix entries * @param startRow Initial row index * @param endRow Final row index (inclusive) * @param startColumn Initial column index * @param endColumn Final column index * @throws OutOfRangeException if the indices are not valid. * @throws NumberIsTooSmallException if {@code endRow < startRow} or * {@code endColumn < startColumn}. * @see #walkInRowOrder(RealMatrixChangingVisitor) * @see #walkInRowOrder(RealMatrixPreservingVisitor) * @see #walkInRowOrder(RealMatrixChangingVisitor, int, int, int, int) * @see #walkInRowOrder(RealMatrixPreservingVisitor, int, int, int, int) * @see #walkInColumnOrder(RealMatrixChangingVisitor) * @see #walkInColumnOrder(RealMatrixPreservingVisitor) * @see #walkInColumnOrder(RealMatrixPreservingVisitor, int, int, int, int) * @see #walkInOptimizedOrder(RealMatrixChangingVisitor) * @see #walkInOptimizedOrder(RealMatrixPreservingVisitor) * @see #walkInOptimizedOrder(RealMatrixChangingVisitor, int, int, int, int) * @see #walkInOptimizedOrder(RealMatrixPreservingVisitor, int, int, int, int) * @return the value returned by {@link RealMatrixChangingVisitor#end()} at the end * of the walk */ double walkInColumnOrder(RealMatrixChangingVisitor visitor, int startRow, int endRow, int startColumn, int endColumn) throws OutOfRangeException, NumberIsTooSmallException; /** * Visit (but don't change) some matrix entries in column order. *

                              Column order starts at upper left and iterating through all elements * of a column from top to bottom before going to the topmost element * of the next column.

                              * @param visitor visitor used to process all matrix entries * @param startRow Initial row index * @param endRow Final row index (inclusive) * @param startColumn Initial column index * @param endColumn Final column index * @throws OutOfRangeException if the indices are not valid. * @throws NumberIsTooSmallException if {@code endRow < startRow} or * {@code endColumn < startColumn}. * @see #walkInRowOrder(RealMatrixChangingVisitor) * @see #walkInRowOrder(RealMatrixPreservingVisitor) * @see #walkInRowOrder(RealMatrixChangingVisitor, int, int, int, int) * @see #walkInRowOrder(RealMatrixPreservingVisitor, int, int, int, int) * @see #walkInColumnOrder(RealMatrixChangingVisitor) * @see #walkInColumnOrder(RealMatrixPreservingVisitor) * @see #walkInColumnOrder(RealMatrixChangingVisitor, int, int, int, int) * @see #walkInOptimizedOrder(RealMatrixChangingVisitor) * @see #walkInOptimizedOrder(RealMatrixPreservingVisitor) * @see #walkInOptimizedOrder(RealMatrixChangingVisitor, int, int, int, int) * @see #walkInOptimizedOrder(RealMatrixPreservingVisitor, int, int, int, int) * @return the value returned by {@link RealMatrixPreservingVisitor#end()} at the end * of the walk */ double walkInColumnOrder(RealMatrixPreservingVisitor visitor, int startRow, int endRow, int startColumn, int endColumn) throws OutOfRangeException, NumberIsTooSmallException; /** * Visit (and possibly change) all matrix entries using the fastest possible order. *

                              The fastest walking order depends on the exact matrix class. It may be * different from traditional row or column orders.

                              * @param visitor visitor used to process all matrix entries * @see #walkInRowOrder(RealMatrixChangingVisitor) * @see #walkInRowOrder(RealMatrixPreservingVisitor) * @see #walkInRowOrder(RealMatrixChangingVisitor, int, int, int, int) * @see #walkInRowOrder(RealMatrixPreservingVisitor, int, int, int, int) * @see #walkInColumnOrder(RealMatrixChangingVisitor) * @see #walkInColumnOrder(RealMatrixPreservingVisitor) * @see #walkInColumnOrder(RealMatrixChangingVisitor, int, int, int, int) * @see #walkInColumnOrder(RealMatrixPreservingVisitor, int, int, int, int) * @see #walkInOptimizedOrder(RealMatrixPreservingVisitor) * @see #walkInOptimizedOrder(RealMatrixChangingVisitor, int, int, int, int) * @see #walkInOptimizedOrder(RealMatrixPreservingVisitor, int, int, int, int) * @return the value returned by {@link RealMatrixChangingVisitor#end()} at the end * of the walk */ double walkInOptimizedOrder(RealMatrixChangingVisitor visitor); /** * Visit (but don't change) all matrix entries using the fastest possible order. *

                              The fastest walking order depends on the exact matrix class. It may be * different from traditional row or column orders.

                              * @param visitor visitor used to process all matrix entries * @see #walkInRowOrder(RealMatrixChangingVisitor) * @see #walkInRowOrder(RealMatrixPreservingVisitor) * @see #walkInRowOrder(RealMatrixChangingVisitor, int, int, int, int) * @see #walkInRowOrder(RealMatrixPreservingVisitor, int, int, int, int) * @see #walkInColumnOrder(RealMatrixChangingVisitor) * @see #walkInColumnOrder(RealMatrixPreservingVisitor) * @see #walkInColumnOrder(RealMatrixChangingVisitor, int, int, int, int) * @see #walkInColumnOrder(RealMatrixPreservingVisitor, int, int, int, int) * @see #walkInOptimizedOrder(RealMatrixChangingVisitor) * @see #walkInOptimizedOrder(RealMatrixChangingVisitor, int, int, int, int) * @see #walkInOptimizedOrder(RealMatrixPreservingVisitor, int, int, int, int) * @return the value returned by {@link RealMatrixPreservingVisitor#end()} at the end * of the walk */ double walkInOptimizedOrder(RealMatrixPreservingVisitor visitor); /** * Visit (and possibly change) some matrix entries using the fastest possible order. *

                              The fastest walking order depends on the exact matrix class. It may be * different from traditional row or column orders.

                              * @param visitor visitor used to process all matrix entries * @param startRow Initial row index * @param endRow Final row index (inclusive) * @param startColumn Initial column index * @param endColumn Final column index (inclusive) * @throws OutOfRangeException if the indices are not valid. * @throws NumberIsTooSmallException if {@code endRow < startRow} or * {@code endColumn < startColumn}. * @see #walkInRowOrder(RealMatrixChangingVisitor) * @see #walkInRowOrder(RealMatrixPreservingVisitor) * @see #walkInRowOrder(RealMatrixChangingVisitor, int, int, int, int) * @see #walkInRowOrder(RealMatrixPreservingVisitor, int, int, int, int) * @see #walkInColumnOrder(RealMatrixChangingVisitor) * @see #walkInColumnOrder(RealMatrixPreservingVisitor) * @see #walkInColumnOrder(RealMatrixChangingVisitor, int, int, int, int) * @see #walkInColumnOrder(RealMatrixPreservingVisitor, int, int, int, int) * @see #walkInOptimizedOrder(RealMatrixChangingVisitor) * @see #walkInOptimizedOrder(RealMatrixPreservingVisitor) * @see #walkInOptimizedOrder(RealMatrixPreservingVisitor, int, int, int, int) * @return the value returned by {@link RealMatrixChangingVisitor#end()} at the end * of the walk */ double walkInOptimizedOrder(RealMatrixChangingVisitor visitor, int startRow, int endRow, int startColumn, int endColumn) throws OutOfRangeException, NumberIsTooSmallException; /** * Visit (but don't change) some matrix entries using the fastest possible order. *

                              The fastest walking order depends on the exact matrix class. It may be * different from traditional row or column orders.

                              * @param visitor visitor used to process all matrix entries * @param startRow Initial row index * @param endRow Final row index (inclusive) * @param startColumn Initial column index * @param endColumn Final column index (inclusive) * @throws OutOfRangeException if the indices are not valid. * @throws NumberIsTooSmallException if {@code endRow < startRow} or * {@code endColumn < startColumn}. * @see #walkInRowOrder(RealMatrixChangingVisitor) * @see #walkInRowOrder(RealMatrixPreservingVisitor) * @see #walkInRowOrder(RealMatrixChangingVisitor, int, int, int, int) * @see #walkInRowOrder(RealMatrixPreservingVisitor, int, int, int, int) * @see #walkInColumnOrder(RealMatrixChangingVisitor) * @see #walkInColumnOrder(RealMatrixPreservingVisitor) * @see #walkInColumnOrder(RealMatrixChangingVisitor, int, int, int, int) * @see #walkInColumnOrder(RealMatrixPreservingVisitor, int, int, int, int) * @see #walkInOptimizedOrder(RealMatrixChangingVisitor) * @see #walkInOptimizedOrder(RealMatrixPreservingVisitor) * @see #walkInOptimizedOrder(RealMatrixChangingVisitor, int, int, int, int) * @return the value returned by {@link RealMatrixPreservingVisitor#end()} at the end * of the walk */ double walkInOptimizedOrder(RealMatrixPreservingVisitor visitor, int startRow, int endRow, int startColumn, int endColumn) throws OutOfRangeException, NumberIsTooSmallException; } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/linear/SparseFieldMatrix.java100644 1750 1750 13606 12126627717 30260 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.linear; import org.apache.commons.math3.Field; import org.apache.commons.math3.FieldElement; import org.apache.commons.math3.util.OpenIntToFieldHashMap; /** * Sparse matrix implementation based on an open addressed map. * * @param the type of the field elements * @version $Id: SparseFieldMatrix.java 1416643 2012-12-03 19:37:14Z tn $ * @since 2.0 * @deprecated As of version 3.1, this class is deprecated, for reasons exposed * in this JIRA * ticket. This * class will be removed in version 4.0. * */ @Deprecated public class SparseFieldMatrix> extends AbstractFieldMatrix { /** Storage for (sparse) matrix elements. */ private final OpenIntToFieldHashMap entries; /** Row dimension. */ private final int rows; /** Column dimension. */ private final int columns; /** * Create a matrix with no data. * * @param field Field to which the elements belong. */ public SparseFieldMatrix(final Field field) { super(field); rows = 0; columns= 0; entries = new OpenIntToFieldHashMap(field); } /** * Create a new SparseFieldMatrix with the supplied row and column * dimensions. * * @param field Field to which the elements belong. * @param rowDimension Number of rows in the new matrix. * @param columnDimension Number of columns in the new matrix. * @throws org.apache.commons.math3.exception.NotStrictlyPositiveException * if row or column dimension is not positive. */ public SparseFieldMatrix(final Field field, final int rowDimension, final int columnDimension) { super(field, rowDimension, columnDimension); this.rows = rowDimension; this.columns = columnDimension; entries = new OpenIntToFieldHashMap(field); } /** * Copy constructor. * * @param other Instance to copy. */ public SparseFieldMatrix(SparseFieldMatrix other) { super(other.getField(), other.getRowDimension(), other.getColumnDimension()); rows = other.getRowDimension(); columns = other.getColumnDimension(); entries = new OpenIntToFieldHashMap(other.entries); } /** * Generic copy constructor. * * @param other Instance to copy. */ public SparseFieldMatrix(FieldMatrix other){ super(other.getField(), other.getRowDimension(), other.getColumnDimension()); rows = other.getRowDimension(); columns = other.getColumnDimension(); entries = new OpenIntToFieldHashMap(getField()); for (int i = 0; i < rows; i++) { for (int j = 0; j < columns; j++) { setEntry(i, j, other.getEntry(i, j)); } } } /** {@inheritDoc} */ @Override public void addToEntry(int row, int column, T increment) { checkRowIndex(row); checkColumnIndex(column); final int key = computeKey(row, column); final T value = entries.get(key).add(increment); if (getField().getZero().equals(value)) { entries.remove(key); } else { entries.put(key, value); } } /** {@inheritDoc} */ @Override public FieldMatrix copy() { return new SparseFieldMatrix(this); } /** {@inheritDoc} */ @Override public FieldMatrix createMatrix(int rowDimension, int columnDimension) { return new SparseFieldMatrix(getField(), rowDimension, columnDimension); } /** {@inheritDoc} */ @Override public int getColumnDimension() { return columns; } /** {@inheritDoc} */ @Override public T getEntry(int row, int column) { checkRowIndex(row); checkColumnIndex(column); return entries.get(computeKey(row, column)); } /** {@inheritDoc} */ @Override public int getRowDimension() { return rows; } /** {@inheritDoc} */ @Override public void multiplyEntry(int row, int column, T factor) { checkRowIndex(row); checkColumnIndex(column); final int key = computeKey(row, column); final T value = entries.get(key).multiply(factor); if (getField().getZero().equals(value)) { entries.remove(key); } else { entries.put(key, value); } } /** {@inheritDoc} */ @Override public void setEntry(int row, int column, T value) { checkRowIndex(row); checkColumnIndex(column); if (getField().getZero().equals(value)) { entries.remove(computeKey(row, column)); } else { entries.put(computeKey(row, column), value); } } /** * Compute the key to access a matrix element. * * @param row Row index of the matrix element. * @param column Column index of the matrix element. * @return the key within the map to access the matrix element. */ private int computeKey(int row, int column) { return row * columns + column; } } ././@LongLink100644 0 0 152 12126630647 10260 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/linear/MatrixDimensionMismatchException.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/linear/MatrixDimensionMismatchException100644 1750 1750 5274 12126627717 32413 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.linear; import org.apache.commons.math3.exception.MultiDimensionMismatchException; import org.apache.commons.math3.exception.util.LocalizedFormats; /** * Exception to be thrown when either the number of rows or the number of * columns of a matrix do not match the expected values. * * @since 3.0 * @version $Id: MatrixDimensionMismatchException.java 1416643 2012-12-03 19:37:14Z tn $ */ public class MatrixDimensionMismatchException extends MultiDimensionMismatchException { /** Serializable version Id. */ private static final long serialVersionUID = -8415396756375798143L; /** * Construct an exception from the mismatched dimensions. * * @param wrongRowDim Wrong row dimension. * @param wrongColDim Wrong column dimension. * @param expectedRowDim Expected row dimension. * @param expectedColDim Expected column dimension. */ public MatrixDimensionMismatchException(int wrongRowDim, int wrongColDim, int expectedRowDim, int expectedColDim) { super(LocalizedFormats.DIMENSIONS_MISMATCH_2x2, new Integer[] { wrongRowDim, wrongColDim }, new Integer[] { expectedRowDim, expectedColDim }); } /** * @return the expected row dimension. */ public int getWrongRowDimension() { return getWrongDimension(0); } /** * @return the expected row dimension. */ public int getExpectedRowDimension() { return getExpectedDimension(0); } /** * @return the wrong column dimension. */ public int getWrongColumnDimension() { return getWrongDimension(1); } /** * @return the expected column dimension. */ public int getExpectedColumnDimension() { return getExpectedDimension(1); } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/linear/NonSquareMatrixException.java100644 1750 1750 3202 12126627717 31620 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.linear; import org.apache.commons.math3.exception.DimensionMismatchException; import org.apache.commons.math3.exception.util.LocalizedFormats; /** * Exception to be thrown when a square matrix is expected. * * @since 3.0 * @version $Id: NonSquareMatrixException.java 1416643 2012-12-03 19:37:14Z tn $ */ public class NonSquareMatrixException extends DimensionMismatchException { /** Serializable version Id. */ private static final long serialVersionUID = -660069396594485772L; /** * Construct an exception from the mismatched dimensions. * * @param wrong Row dimension. * @param expected Column dimension. */ public NonSquareMatrixException(int wrong, int expected) { super(LocalizedFormats.NON_SQUARE_MATRIX, wrong, expected); } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/linear/RealMatrixFormat.java100644 1750 1750 34743 12126627717 30120 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.linear; import java.text.FieldPosition; import java.text.NumberFormat; import java.text.ParsePosition; import java.util.ArrayList; import java.util.List; import java.util.Locale; import org.apache.commons.math3.exception.MathParseException; import org.apache.commons.math3.util.CompositeFormat; /** * Formats a {@code nxm} matrix in components list format * "{{a00,a01, ..., * a0m-1},{a10, * a11, ..., a1m-1},{...},{ * an-10, an-11, ..., * an-1m-1}}". *

                              The prefix and suffix "{" and "}", the row prefix and suffix "{" and "}", * the row separator "," and the column separator "," can be replaced by any * user-defined strings. The number format for components can be configured.

                              * *

                              White space is ignored at parse time, even if it is in the prefix, suffix * or separator specifications. So even if the default separator does include a space * character that is used at format time, both input string "{{1,1,1}}" and * " { { 1 , 1 , 1 } } " will be parsed without error and the same matrix will be * returned. In the second case, however, the parse position after parsing will be * just after the closing curly brace, i.e. just before the trailing space.

                              * *

                              Note: the grouping functionality of the used {@link NumberFormat} is * disabled to prevent problems when parsing (e.g. 1,345.34 would be a valid number * but conflicts with the default column separator).

                              * * @since 3.1 * @version $Id: RealMatrixFormat.java 1364793 2012-07-23 20:46:28Z tn $ */ public class RealMatrixFormat { /** The default prefix: "{". */ private static final String DEFAULT_PREFIX = "{"; /** The default suffix: "}". */ private static final String DEFAULT_SUFFIX = "}"; /** The default row prefix: "{". */ private static final String DEFAULT_ROW_PREFIX = "{"; /** The default row suffix: "}". */ private static final String DEFAULT_ROW_SUFFIX = "}"; /** The default row separator: ",". */ private static final String DEFAULT_ROW_SEPARATOR = ","; /** The default column separator: ",". */ private static final String DEFAULT_COLUMN_SEPARATOR = ","; /** Prefix. */ private final String prefix; /** Suffix. */ private final String suffix; /** Row prefix. */ private final String rowPrefix; /** Row suffix. */ private final String rowSuffix; /** Row separator. */ private final String rowSeparator; /** Column separator. */ private final String columnSeparator; /** The format used for components. */ private final NumberFormat format; /** * Create an instance with default settings. *

                              The instance uses the default prefix, suffix and row/column separator: * "[", "]", ";" and ", " and the default number format for components.

                              */ public RealMatrixFormat() { this(DEFAULT_PREFIX, DEFAULT_SUFFIX, DEFAULT_ROW_PREFIX, DEFAULT_ROW_SUFFIX, DEFAULT_ROW_SEPARATOR, DEFAULT_COLUMN_SEPARATOR, CompositeFormat.getDefaultNumberFormat()); } /** * Create an instance with a custom number format for components. * @param format the custom format for components. */ public RealMatrixFormat(final NumberFormat format) { this(DEFAULT_PREFIX, DEFAULT_SUFFIX, DEFAULT_ROW_PREFIX, DEFAULT_ROW_SUFFIX, DEFAULT_ROW_SEPARATOR, DEFAULT_COLUMN_SEPARATOR, format); } /** * Create an instance with custom prefix, suffix and separator. * @param prefix prefix to use instead of the default "{" * @param suffix suffix to use instead of the default "}" * @param rowPrefix row prefix to use instead of the default "{" * @param rowSuffix row suffix to use instead of the default "}" * @param rowSeparator tow separator to use instead of the default ";" * @param columnSeparator column separator to use instead of the default ", " */ public RealMatrixFormat(final String prefix, final String suffix, final String rowPrefix, final String rowSuffix, final String rowSeparator, final String columnSeparator) { this(prefix, suffix, rowPrefix, rowSuffix, rowSeparator, columnSeparator, CompositeFormat.getDefaultNumberFormat()); } /** * Create an instance with custom prefix, suffix, separator and format * for components. * @param prefix prefix to use instead of the default "{" * @param suffix suffix to use instead of the default "}" * @param rowPrefix row prefix to use instead of the default "{" * @param rowSuffix row suffix to use instead of the default "}" * @param rowSeparator tow separator to use instead of the default ";" * @param columnSeparator column separator to use instead of the default ", " * @param format the custom format for components. */ public RealMatrixFormat(final String prefix, final String suffix, final String rowPrefix, final String rowSuffix, final String rowSeparator, final String columnSeparator, final NumberFormat format) { this.prefix = prefix; this.suffix = suffix; this.rowPrefix = rowPrefix; this.rowSuffix = rowSuffix; this.rowSeparator = rowSeparator; this.columnSeparator = columnSeparator; this.format = format; // disable grouping to prevent parsing problems this.format.setGroupingUsed(false); } /** * Get the set of locales for which real vectors formats are available. *

                              This is the same set as the {@link NumberFormat} set.

                              * @return available real vector format locales. */ public static Locale[] getAvailableLocales() { return NumberFormat.getAvailableLocales(); } /** * Get the format prefix. * @return format prefix. */ public String getPrefix() { return prefix; } /** * Get the format suffix. * @return format suffix. */ public String getSuffix() { return suffix; } /** * Get the format prefix. * @return format prefix. */ public String getRowPrefix() { return rowPrefix; } /** * Get the format suffix. * @return format suffix. */ public String getRowSuffix() { return rowSuffix; } /** * Get the format separator between rows of the matrix. * @return format separator for rows. */ public String getRowSeparator() { return rowSeparator; } /** * Get the format separator between components. * @return format separator between components. */ public String getColumnSeparator() { return columnSeparator; } /** * Get the components format. * @return components format. */ public NumberFormat getFormat() { return format; } /** * Returns the default real vector format for the current locale. * @return the default real vector format. */ public static RealMatrixFormat getInstance() { return getInstance(Locale.getDefault()); } /** * Returns the default real vector format for the given locale. * @param locale the specific locale used by the format. * @return the real vector format specific to the given locale. */ public static RealMatrixFormat getInstance(final Locale locale) { return new RealMatrixFormat(CompositeFormat.getDefaultNumberFormat(locale)); } /** * This method calls {@link #format(RealMatrix,StringBuffer,FieldPosition)}. * * @param m RealMatrix object to format. * @return a formatted matrix. */ public String format(RealMatrix m) { return format(m, new StringBuffer(), new FieldPosition(0)).toString(); } /** * Formats a {@link RealMatrix} object to produce a string. * @param matrix the object to format. * @param toAppendTo where the text is to be appended * @param pos On input: an alignment field, if desired. On output: the * offsets of the alignment field * @return the value passed in as toAppendTo. */ public StringBuffer format(RealMatrix matrix, StringBuffer toAppendTo, FieldPosition pos) { pos.setBeginIndex(0); pos.setEndIndex(0); // format prefix toAppendTo.append(prefix); // format rows final int rows = matrix.getRowDimension(); for (int i = 0; i < rows; ++i) { toAppendTo.append(rowPrefix); for (int j = 0; j < matrix.getColumnDimension(); ++j) { if (j > 0) { toAppendTo.append(columnSeparator); } CompositeFormat.formatDouble(matrix.getEntry(i, j), format, toAppendTo, pos); } toAppendTo.append(rowSuffix); if (i < rows - 1) { toAppendTo.append(rowSeparator); } } // format suffix toAppendTo.append(suffix); return toAppendTo; } /** * Parse a string to produce a {@link RealMatrix} object. * * @param source String to parse. * @return the parsed {@link RealMatrix} object. * @throws MathParseException if the beginning of the specified string * cannot be parsed. */ public RealMatrix parse(String source) { final ParsePosition parsePosition = new ParsePosition(0); final RealMatrix result = parse(source, parsePosition); if (parsePosition.getIndex() == 0) { throw new MathParseException(source, parsePosition.getErrorIndex(), Array2DRowRealMatrix.class); } return result; } /** * Parse a string to produce a {@link RealMatrix} object. * * @param source String to parse. * @param pos input/ouput parsing parameter. * @return the parsed {@link RealMatrix} object. */ public RealMatrix parse(String source, ParsePosition pos) { int initialIndex = pos.getIndex(); final String trimmedPrefix = prefix.trim(); final String trimmedSuffix = suffix.trim(); final String trimmedRowPrefix = rowPrefix.trim(); final String trimmedRowSuffix = rowSuffix.trim(); final String trimmedColumnSeparator = columnSeparator.trim(); final String trimmedRowSeparator = rowSeparator.trim(); // parse prefix CompositeFormat.parseAndIgnoreWhitespace(source, pos); if (!CompositeFormat.parseFixedstring(source, trimmedPrefix, pos)) { return null; } // parse components List> matrix = new ArrayList>(); List rowComponents = new ArrayList(); for (boolean loop = true; loop;){ if (!rowComponents.isEmpty()) { CompositeFormat.parseAndIgnoreWhitespace(source, pos); if (!CompositeFormat.parseFixedstring(source, trimmedColumnSeparator, pos)) { if (trimmedRowSuffix.length() != 0 && !CompositeFormat.parseFixedstring(source, trimmedRowSuffix, pos)) { return null; } else { CompositeFormat.parseAndIgnoreWhitespace(source, pos); if (CompositeFormat.parseFixedstring(source, trimmedRowSeparator, pos)) { matrix.add(rowComponents); rowComponents = new ArrayList(); continue; } else { loop = false; } } } } else { CompositeFormat.parseAndIgnoreWhitespace(source, pos); if (trimmedRowPrefix.length() != 0 && !CompositeFormat.parseFixedstring(source, trimmedRowPrefix, pos)) { return null; } } if (loop) { CompositeFormat.parseAndIgnoreWhitespace(source, pos); Number component = CompositeFormat.parseNumber(source, format, pos); if (component != null) { rowComponents.add(component); } else { if (rowComponents.isEmpty()) { loop = false; } else { // invalid component // set index back to initial, error index should already be set pos.setIndex(initialIndex); return null; } } } } if (!rowComponents.isEmpty()) { matrix.add(rowComponents); } // parse suffix CompositeFormat.parseAndIgnoreWhitespace(source, pos); if (!CompositeFormat.parseFixedstring(source, trimmedSuffix, pos)) { return null; } // do not allow an empty matrix if (matrix.isEmpty()) { pos.setIndex(initialIndex); return null; } // build vector double[][] data = new double[matrix.size()][]; int row = 0; for (List rowList : matrix) { data[row] = new double[rowList.size()]; for (int i = 0; i < rowList.size(); i++) { data[row][i] = rowList.get(i).doubleValue(); } row++; } return MatrixUtils.createRealMatrix(data); } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/linear/SymmLQ.java100644 1750 1750 147006 12126627717 26076 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.linear; import org.apache.commons.math3.exception.DimensionMismatchException; import org.apache.commons.math3.exception.MaxCountExceededException; import org.apache.commons.math3.exception.NullArgumentException; import org.apache.commons.math3.exception.util.ExceptionContext; import org.apache.commons.math3.util.FastMath; import org.apache.commons.math3.util.IterationManager; import org.apache.commons.math3.util.MathUtils; /** *

                              * Implementation of the SYMMLQ iterative linear solver proposed by Paige and Saunders (1975). This implementation is * largely based on the FORTRAN code by Pr. Michael A. Saunders, available here. *

                              *

                              * SYMMLQ is designed to solve the system of linear equations A · x = b * where A is an n × n self-adjoint linear operator (defined as a * {@link RealLinearOperator}), and b is a given vector. The operator A is not * required to be positive definite. If A is known to be definite, the method of * conjugate gradients might be preferred, since it will require about the same * number of iterations as SYMMLQ but slightly less work per iteration. *

                              *

                              * SYMMLQ is designed to solve the system (A - shift · I) · x = b, * where shift is a specified scalar value. If shift and b are suitably chosen, * the computed vector x may approximate an (unnormalized) eigenvector of A, as * in the methods of inverse iteration and/or Rayleigh-quotient iteration. * Again, the linear operator (A - shift · I) need not be positive * definite (but must be self-adjoint). The work per iteration is very * slightly less if shift = 0. *

                              *

                              Preconditioning

                              *

                              * Preconditioning may reduce the number of iterations required. The solver may * be provided with a positive definite preconditioner * M = PT · P * that is known to approximate * (A - shift · I)-1 in some sense, where matrix-vector * products of the form M · y = x can be computed efficiently. Then * SYMMLQ will implicitly solve the system of equations * P · (A - shift · I) · PT · * xhat = P · b, i.e. * Ahat · xhat = bhat, * where * Ahat = P · (A - shift · I) · PT, * bhat = P · b, * and return the solution * x = PT · xhat. * The associated residual is * rhat = bhat - Ahat · xhat * = P · [b - (A - shift · I) · x] * = P · r. *

                              *

                              * In the case of preconditioning, the {@link IterativeLinearSolverEvent}s that * this solver fires are such that * {@link IterativeLinearSolverEvent#getNormOfResidual()} returns the norm of * the preconditioned, updated residual, ||P · r||, not the norm * of the true residual ||r||. *

                              *

                              Default stopping criterion

                              *

                              * A default stopping criterion is implemented. The iterations stop when || rhat * || ≤ δ || Ahat || || xhat ||, where xhat is the current estimate of * the solution of the transformed system, rhat the current estimate of the * corresponding residual, and δ a user-specified tolerance. *

                              *

                              Iteration count

                              *

                              * In the present context, an iteration should be understood as one evaluation * of the matrix-vector product A · x. The initialization phase therefore * counts as one iteration. If the user requires checks on the symmetry of A, * this entails one further matrix-vector product in the initial phase. This * further product is not accounted for in the iteration count. In * other words, the number of iterations required to reach convergence will be * identical, whether checks have been required or not. *

                              *

                              * The present definition of the iteration count differs from that adopted in * the original FOTRAN code, where the initialization phase was not * taken into account. *

                              *

                              Initial guess of the solution

                              *

                              * The {@code x} parameter in *

                                *
                              • {@link #solve(RealLinearOperator, RealVector, RealVector)},
                              • *
                              • {@link #solve(RealLinearOperator, RealLinearOperator, RealVector, RealVector)}},
                              • *
                              • {@link #solveInPlace(RealLinearOperator, RealVector, RealVector)},
                              • *
                              • {@link #solveInPlace(RealLinearOperator, RealLinearOperator, RealVector, RealVector)},
                              • *
                              • {@link #solveInPlace(RealLinearOperator, RealLinearOperator, RealVector, RealVector, boolean, double)},
                              • *
                              * should not be considered as an initial guess, as it is set to zero in the * initial phase. If x0 is known to be a good approximation to x, one * should compute r0 = b - A · x, solve A · dx = r0, * and set x = x0 + dx. *

                              *

                              Exception context

                              *

                              * Besides standard {@link DimensionMismatchException}, this class might throw * {@link NonSelfAdjointOperatorException} if the linear operator or the * preconditioner are not symmetric. In this case, the {@link ExceptionContext} * provides more information *

                                *
                              • key {@code "operator"} points to the offending linear operator, say L,
                              • *
                              • key {@code "vector1"} points to the first offending vector, say x, *
                              • key {@code "vector2"} points to the second offending vector, say y, such * that xT · L · y ≠ yT · L * · x (within a certain accuracy).
                              • *
                              *

                              *

                              * {@link NonPositiveDefiniteOperatorException} might also be thrown in case the * preconditioner is not positive definite. The relevant keys to the * {@link ExceptionContext} are *

                                *
                              • key {@code "operator"}, which points to the offending linear operator, * say L,
                              • *
                              • key {@code "vector"}, which points to the offending vector, say x, such * that xT · L · x < 0.
                              • *
                              *

                              *

                              References

                              *
                              *
                              Paige and Saunders (1975)
                              *
                              C. C. Paige and M. A. Saunders, * Solution of Sparse Indefinite Systems of Linear Equations, SIAM * Journal on Numerical Analysis 12(4): 617-629, 1975
                              *
                              * * @version $Id: SymmLQ.java 1416643 2012-12-03 19:37:14Z tn $ * @since 3.0 */ public class SymmLQ extends PreconditionedIterativeLinearSolver { /* * IMPLEMENTATION NOTES * -------------------- * The implementation follows as closely as possible the notations of Paige * and Saunders (1975). Attention must be paid to the fact that some * quantities which are relevant to iteration k can only be computed in * iteration (k+1). Therefore, minute attention must be paid to the index of * each state variable of this algorithm. * * 1. Preconditioning * --------------- * The Lanczos iterations associated with Ahat and bhat read * beta[1] = ||P * b|| * v[1] = P * b / beta[1] * beta[k+1] * v[k+1] = Ahat * v[k] - alpha[k] * v[k] - beta[k] * v[k-1] * = P * (A - shift * I) * P' * v[k] - alpha[k] * v[k] * - beta[k] * v[k-1] * Multiplying both sides by P', we get * beta[k+1] * (P' * v)[k+1] = M * (A - shift * I) * (P' * v)[k] * - alpha[k] * (P' * v)[k] * - beta[k] * (P' * v[k-1]), * and * alpha[k+1] = v[k+1]' * Ahat * v[k+1] * = v[k+1]' * P * (A - shift * I) * P' * v[k+1] * = (P' * v)[k+1]' * (A - shift * I) * (P' * v)[k+1]. * * In other words, the Lanczos iterations are unchanged, except for the fact * that we really compute (P' * v) instead of v. It can easily be checked * that all other formulas are unchanged. It must be noted that P is never * explicitly used, only matrix-vector products involving are invoked. * * 2. Accounting for the shift parameter * ---------------------------------- * Is trivial: each time A.operate(x) is invoked, one must subtract shift * x * to the result. * * 3. Accounting for the goodb flag * ----------------------------- * When goodb is set to true, the component of xL along b is computed * separately. From Paige and Saunders (1975), equation (5.9), we have * wbar[k+1] = s[k] * wbar[k] - c[k] * v[k+1], * wbar[1] = v[1]. * Introducing wbar2[k] = wbar[k] - s[1] * ... * s[k-1] * v[1], it can * easily be verified by induction that wbar2 follows the same recursive * relation * wbar2[k+1] = s[k] * wbar2[k] - c[k] * v[k+1], * wbar2[1] = 0, * and we then have * w[k] = c[k] * wbar2[k] + s[k] * v[k+1] * + s[1] * ... * s[k-1] * c[k] * v[1]. * Introducing w2[k] = w[k] - s[1] * ... * s[k-1] * c[k] * v[1], we find, * from (5.10) * xL[k] = zeta[1] * w[1] + ... + zeta[k] * w[k] * = zeta[1] * w2[1] + ... + zeta[k] * w2[k] * + (s[1] * c[2] * zeta[2] + ... * + s[1] * ... * s[k-1] * c[k] * zeta[k]) * v[1] * = xL2[k] + bstep[k] * v[1], * where xL2[k] is defined by * xL2[0] = 0, * xL2[k+1] = xL2[k] + zeta[k+1] * w2[k+1], * and bstep is defined by * bstep[1] = 0, * bstep[k] = bstep[k-1] + s[1] * ... * s[k-1] * c[k] * zeta[k]. * We also have, from (5.11) * xC[k] = xL[k-1] + zbar[k] * wbar[k] * = xL2[k-1] + zbar[k] * wbar2[k] * + (bstep[k-1] + s[1] * ... * s[k-1] * zbar[k]) * v[1]. */ /** *

                              * A simple container holding the non-final variables used in the * iterations. Making the current state of the solver visible from the * outside is necessary, because during the iterations, {@code x} does not * exactly hold the current estimate of the solution. Indeed, * {@code x} needs in general to be moved from the LQ point to the CG point. * Besides, additional upudates must be carried out in case {@code goodb} is * set to {@code true}. *

                              *

                              * In all subsequent comments, the description of the state variables refer * to their value after a call to {@link #update()}. In these comments, k is * the current number of evaluations of matrix-vector products. *

                              */ private static class State { /** The cubic root of {@link #MACH_PREC}. */ static final double CBRT_MACH_PREC; /** The machine precision. */ static final double MACH_PREC; /** Reference to the linear operator. */ private final RealLinearOperator a; /** Reference to the right-hand side vector. */ private final RealVector b; /** {@code true} if symmetry of matrix and conditioner must be checked. */ private final boolean check; /** * The value of the custom tolerance δ for the default stopping * criterion. */ private final double delta; /** The value of beta[k+1]. */ private double beta; /** The value of beta[1]. */ private double beta1; /** The value of bstep[k-1]. */ private double bstep; /** The estimate of the norm of P * rC[k]. */ private double cgnorm; /** The value of dbar[k+1] = -beta[k+1] * c[k-1]. */ private double dbar; /** * The value of gamma[k] * zeta[k]. Was called {@code rhs1} in the * initial code. */ private double gammaZeta; /** The value of gbar[k]. */ private double gbar; /** The value of max(|alpha[1]|, gamma[1], ..., gamma[k-1]). */ private double gmax; /** The value of min(|alpha[1]|, gamma[1], ..., gamma[k-1]). */ private double gmin; /** Copy of the {@code goodb} parameter. */ private final boolean goodb; /** {@code true} if the default convergence criterion is verified. */ private boolean hasConverged; /** The estimate of the norm of P * rL[k-1]. */ private double lqnorm; /** Reference to the preconditioner, M. */ private final RealLinearOperator m; /** * The value of (-eps[k+1] * zeta[k-1]). Was called {@code rhs2} in the * initial code. */ private double minusEpsZeta; /** The value of M * b. */ private final RealVector mb; /** The value of beta[k]. */ private double oldb; /** The value of beta[k] * M^(-1) * P' * v[k]. */ private RealVector r1; /** The value of beta[k+1] * M^(-1) * P' * v[k+1]. */ private RealVector r2; /** * The value of the updated, preconditioned residual P * r. This value is * given by {@code min(}{@link #cgnorm}{@code , }{@link #lqnorm}{@code )}. */ private double rnorm; /** Copy of the {@code shift} parameter. */ private final double shift; /** The value of s[1] * ... * s[k-1]. */ private double snprod; /** * An estimate of the square of the norm of A * V[k], based on Paige and * Saunders (1975), equation (3.3). */ private double tnorm; /** * The value of P' * wbar[k] or P' * (wbar[k] - s[1] * ... * s[k-1] * * v[1]) if {@code goodb} is {@code true}. Was called {@code w} in the * initial code. */ private RealVector wbar; /** * A reference to the vector to be updated with the solution. Contains * the value of xL[k-1] if {@code goodb} is {@code false}, (xL[k-1] - * bstep[k-1] * v[1]) otherwise. */ private final RealVector xL; /** The value of beta[k+1] * P' * v[k+1]. */ private RealVector y; /** The value of zeta[1]^2 + ... + zeta[k-1]^2. */ private double ynorm2; /** The value of {@code b == 0} (exact floating-point equality). */ private boolean bIsNull; static { MACH_PREC = FastMath.ulp(1.); CBRT_MACH_PREC = FastMath.cbrt(MACH_PREC); } /** * Creates and inits to k = 1 a new instance of this class. * * @param a the linear operator A of the system * @param m the preconditioner, M (can be {@code null}) * @param b the right-hand side vector * @param goodb usually {@code false}, except if {@code x} is expected * to contain a large multiple of {@code b} * @param shift the amount to be subtracted to all diagonal elements of * A * @param delta the δ parameter for the default stopping criterion * @param check {@code true} if self-adjointedness of both matrix and * preconditioner should be checked */ public State(final RealLinearOperator a, final RealLinearOperator m, final RealVector b, final boolean goodb, final double shift, final double delta, final boolean check) { this.a = a; this.m = m; this.b = b; this.xL = new ArrayRealVector(b.getDimension()); this.goodb = goodb; this.shift = shift; this.mb = m == null ? b : m.operate(b); this.hasConverged = false; this.check = check; this.delta = delta; } /** * Performs a symmetry check on the specified linear operator, and throws an * exception in case this check fails. Given a linear operator L, and a * vector x, this method checks that * x' · L · y = y' · L · x * (within a given accuracy), where y = L · x. * * @param l the linear operator L * @param x the candidate vector x * @param y the candidate vector y = L · x * @param z the vector z = L · y * @throws NonSelfAdjointOperatorException when the test fails */ private static void checkSymmetry(final RealLinearOperator l, final RealVector x, final RealVector y, final RealVector z) throws NonSelfAdjointOperatorException { final double s = y.dotProduct(y); final double t = x.dotProduct(z); final double epsa = (s + MACH_PREC) * CBRT_MACH_PREC; if (FastMath.abs(s - t) > epsa) { final NonSelfAdjointOperatorException e; e = new NonSelfAdjointOperatorException(); final ExceptionContext context = e.getContext(); context.setValue(SymmLQ.OPERATOR, l); context.setValue(SymmLQ.VECTOR1, x); context.setValue(SymmLQ.VECTOR2, y); context.setValue(SymmLQ.THRESHOLD, Double.valueOf(epsa)); throw e; } } /** * Throws a new {@link NonPositiveDefiniteOperatorException} with * appropriate context. * * @param l the offending linear operator * @param v the offending vector * @throws NonPositiveDefiniteOperatorException in any circumstances */ private static void throwNPDLOException(final RealLinearOperator l, final RealVector v) throws NonPositiveDefiniteOperatorException { final NonPositiveDefiniteOperatorException e; e = new NonPositiveDefiniteOperatorException(); final ExceptionContext context = e.getContext(); context.setValue(OPERATOR, l); context.setValue(VECTOR, v); throw e; } /** * A clone of the BLAS {@code DAXPY} function, which carries out the * operation y ← a · x + y. This is for internal use only: no * dimension checks are provided. * * @param a the scalar by which {@code x} is to be multiplied * @param x the vector to be added to {@code y} * @param y the vector to be incremented */ private static void daxpy(final double a, final RealVector x, final RealVector y) { final int n = x.getDimension(); for (int i = 0; i < n; i++) { y.setEntry(i, a * x.getEntry(i) + y.getEntry(i)); } } /** * A BLAS-like function, for the operation z ← a · x + b * · y + z. This is for internal use only: no dimension checks are * provided. * * @param a the scalar by which {@code x} is to be multiplied * @param x the first vector to be added to {@code z} * @param b the scalar by which {@code y} is to be multiplied * @param y the second vector to be added to {@code z} * @param z the vector to be incremented */ private static void daxpbypz(final double a, final RealVector x, final double b, final RealVector y, final RealVector z) { final int n = z.getDimension(); for (int i = 0; i < n; i++) { final double zi; zi = a * x.getEntry(i) + b * y.getEntry(i) + z.getEntry(i); z.setEntry(i, zi); } } /** *

                              * Move to the CG point if it seems better. In this version of SYMMLQ, * the convergence tests involve only cgnorm, so we're unlikely to stop * at an LQ point, except if the iteration limit interferes. *

                              *

                              * Additional upudates are also carried out in case {@code goodb} is set * to {@code true}. *

                              * * @param x the vector to be updated with the refined value of xL */ void refineSolution(final RealVector x) { final int n = this.xL.getDimension(); if (lqnorm < cgnorm) { if (!goodb) { x.setSubVector(0, this.xL); } else { final double step = bstep / beta1; for (int i = 0; i < n; i++) { final double bi = mb.getEntry(i); final double xi = this.xL.getEntry(i); x.setEntry(i, xi + step * bi); } } } else { final double anorm = FastMath.sqrt(tnorm); final double diag = gbar == 0. ? anorm * MACH_PREC : gbar; final double zbar = gammaZeta / diag; final double step = (bstep + snprod * zbar) / beta1; // ynorm = FastMath.sqrt(ynorm2 + zbar * zbar); if (!goodb) { for (int i = 0; i < n; i++) { final double xi = this.xL.getEntry(i); final double wi = wbar.getEntry(i); x.setEntry(i, xi + zbar * wi); } } else { for (int i = 0; i < n; i++) { final double xi = this.xL.getEntry(i); final double wi = wbar.getEntry(i); final double bi = mb.getEntry(i); x.setEntry(i, xi + zbar * wi + step * bi); } } } } /** * Performs the initial phase of the SYMMLQ algorithm. On return, the * value of the state variables of {@code this} object correspond to k = * 1. */ void init() { this.xL.set(0.); /* * Set up y for the first Lanczos vector. y and beta1 will be zero * if b = 0. */ this.r1 = this.b.copy(); this.y = this.m == null ? this.b.copy() : this.m.operate(this.r1); if ((this.m != null) && this.check) { checkSymmetry(this.m, this.r1, this.y, this.m.operate(this.y)); } this.beta1 = this.r1.dotProduct(this.y); if (this.beta1 < 0.) { throwNPDLOException(this.m, this.y); } if (this.beta1 == 0.) { /* If b = 0 exactly, stop with x = 0. */ this.bIsNull = true; return; } this.bIsNull = false; this.beta1 = FastMath.sqrt(this.beta1); /* At this point * r1 = b, * y = M * b, * beta1 = beta[1]. */ final RealVector v = this.y.mapMultiply(1. / this.beta1); this.y = this.a.operate(v); if (this.check) { checkSymmetry(this.a, v, this.y, this.a.operate(this.y)); } /* * Set up y for the second Lanczos vector. y and beta will be zero * or very small if b is an eigenvector. */ daxpy(-this.shift, v, this.y); final double alpha = v.dotProduct(this.y); daxpy(-alpha / this.beta1, this.r1, this.y); /* * At this point * alpha = alpha[1] * y = beta[2] * M^(-1) * P' * v[2] */ /* Make sure r2 will be orthogonal to the first v. */ final double vty = v.dotProduct(this.y); final double vtv = v.dotProduct(v); daxpy(-vty / vtv, v, this.y); this.r2 = this.y.copy(); if (this.m != null) { this.y = this.m.operate(this.r2); } this.oldb = this.beta1; this.beta = this.r2.dotProduct(this.y); if (this.beta < 0.) { throwNPDLOException(this.m, this.y); } this.beta = FastMath.sqrt(this.beta); /* * At this point * oldb = beta[1] * beta = beta[2] * y = beta[2] * P' * v[2] * r2 = beta[2] * M^(-1) * P' * v[2] */ this.cgnorm = this.beta1; this.gbar = alpha; this.dbar = this.beta; this.gammaZeta = this.beta1; this.minusEpsZeta = 0.; this.bstep = 0.; this.snprod = 1.; this.tnorm = alpha * alpha + this.beta * this.beta; this.ynorm2 = 0.; this.gmax = FastMath.abs(alpha) + MACH_PREC; this.gmin = this.gmax; if (this.goodb) { this.wbar = new ArrayRealVector(this.a.getRowDimension()); this.wbar.set(0.); } else { this.wbar = v; } updateNorms(); } /** * Performs the next iteration of the algorithm. The iteration count * should be incremented prior to calling this method. On return, the * value of the state variables of {@code this} object correspond to the * current iteration count {@code k}. */ void update() { final RealVector v = y.mapMultiply(1. / beta); y = a.operate(v); daxpbypz(-shift, v, -beta / oldb, r1, y); final double alpha = v.dotProduct(y); /* * At this point * v = P' * v[k], * y = (A - shift * I) * P' * v[k] - beta[k] * M^(-1) * P' * v[k-1], * alpha = v'[k] * P * (A - shift * I) * P' * v[k] * - beta[k] * v[k]' * P * M^(-1) * P' * v[k-1] * = v'[k] * P * (A - shift * I) * P' * v[k] * - beta[k] * v[k]' * v[k-1] * = alpha[k]. */ daxpy(-alpha / beta, r2, y); /* * At this point * y = (A - shift * I) * P' * v[k] - alpha[k] * M^(-1) * P' * v[k] * - beta[k] * M^(-1) * P' * v[k-1] * = M^(-1) * P' * (P * (A - shift * I) * P' * v[k] -alpha[k] * v[k] * - beta[k] * v[k-1]) * = beta[k+1] * M^(-1) * P' * v[k+1], * from Paige and Saunders (1975), equation (3.2). * * WATCH-IT: the two following lines work only because y is no longer * updated up to the end of the present iteration, and is * reinitialized at the beginning of the next iteration. */ r1 = r2; r2 = y; if (m != null) { y = m.operate(r2); } oldb = beta; beta = r2.dotProduct(y); if (beta < 0.) { throwNPDLOException(m, y); } beta = FastMath.sqrt(beta); /* * At this point * r1 = beta[k] * M^(-1) * P' * v[k], * r2 = beta[k+1] * M^(-1) * P' * v[k+1], * y = beta[k+1] * P' * v[k+1], * oldb = beta[k], * beta = beta[k+1]. */ tnorm += alpha * alpha + oldb * oldb + beta * beta; /* * Compute the next plane rotation for Q. See Paige and Saunders * (1975), equation (5.6), with * gamma = gamma[k-1], * c = c[k-1], * s = s[k-1]. */ final double gamma = FastMath.sqrt(gbar * gbar + oldb * oldb); final double c = gbar / gamma; final double s = oldb / gamma; /* * The relations * gbar[k] = s[k-1] * (-c[k-2] * beta[k]) - c[k-1] * alpha[k] * = s[k-1] * dbar[k] - c[k-1] * alpha[k], * delta[k] = c[k-1] * dbar[k] + s[k-1] * alpha[k], * are not stated in Paige and Saunders (1975), but can be retrieved * by expanding the (k, k-1) and (k, k) coefficients of the matrix in * equation (5.5). */ final double deltak = c * dbar + s * alpha; gbar = s * dbar - c * alpha; final double eps = s * beta; dbar = -c * beta; final double zeta = gammaZeta / gamma; /* * At this point * gbar = gbar[k] * deltak = delta[k] * eps = eps[k+1] * dbar = dbar[k+1] * zeta = zeta[k-1] */ final double zetaC = zeta * c; final double zetaS = zeta * s; final int n = xL.getDimension(); for (int i = 0; i < n; i++) { final double xi = xL.getEntry(i); final double vi = v.getEntry(i); final double wi = wbar.getEntry(i); xL.setEntry(i, xi + wi * zetaC + vi * zetaS); wbar.setEntry(i, wi * s - vi * c); } /* * At this point * x = xL[k-1], * ptwbar = P' wbar[k], * see Paige and Saunders (1975), equations (5.9) and (5.10). */ bstep += snprod * c * zeta; snprod *= s; gmax = FastMath.max(gmax, gamma); gmin = FastMath.min(gmin, gamma); ynorm2 += zeta * zeta; gammaZeta = minusEpsZeta - deltak * zeta; minusEpsZeta = -eps * zeta; /* * At this point * snprod = s[1] * ... * s[k-1], * gmax = max(|alpha[1]|, gamma[1], ..., gamma[k-1]), * gmin = min(|alpha[1]|, gamma[1], ..., gamma[k-1]), * ynorm2 = zeta[1]^2 + ... + zeta[k-1]^2, * gammaZeta = gamma[k] * zeta[k], * minusEpsZeta = -eps[k+1] * zeta[k-1]. * The relation for gammaZeta can be retrieved from Paige and * Saunders (1975), equation (5.4a), last line of the vector * gbar[k] * zbar[k] = -eps[k] * zeta[k-2] - delta[k] * zeta[k-1]. */ updateNorms(); } /** * Computes the norms of the residuals, and checks for convergence. * Updates {@link #lqnorm} and {@link #cgnorm}. */ private void updateNorms() { final double anorm = FastMath.sqrt(tnorm); final double ynorm = FastMath.sqrt(ynorm2); final double epsa = anorm * MACH_PREC; final double epsx = anorm * ynorm * MACH_PREC; final double epsr = anorm * ynorm * delta; final double diag = gbar == 0. ? epsa : gbar; lqnorm = FastMath.sqrt(gammaZeta * gammaZeta + minusEpsZeta * minusEpsZeta); final double qrnorm = snprod * beta1; cgnorm = qrnorm * beta / FastMath.abs(diag); /* * Estimate cond(A). In this version we look at the diagonals of L * in the factorization of the tridiagonal matrix, T = L * Q. * Sometimes, T[k] can be misleadingly ill-conditioned when T[k+1] * is not, so we must be careful not to overestimate acond. */ final double acond; if (lqnorm <= cgnorm) { acond = gmax / gmin; } else { acond = gmax / FastMath.min(gmin, FastMath.abs(diag)); } if (acond * MACH_PREC >= 0.1) { throw new IllConditionedOperatorException(acond); } if (beta1 <= epsx) { /* * x has converged to an eigenvector of A corresponding to the * eigenvalue shift. */ throw new SingularOperatorException(); } rnorm = FastMath.min(cgnorm, lqnorm); hasConverged = (cgnorm <= epsx) || (cgnorm <= epsr); } /** * Returns {@code true} if the default stopping criterion is fulfilled. * * @return {@code true} if convergence of the iterations has occured */ boolean hasConverged() { return hasConverged; } /** * Returns {@code true} if the right-hand side vector is zero exactly. * * @return the boolean value of {@code b == 0} */ boolean bEqualsNullVector() { return bIsNull; } /** * Returns {@code true} if {@code beta} is essentially zero. This method * is used to check for early stop of the iterations. * * @return {@code true} if {@code beta < }{@link #MACH_PREC} */ boolean betaEqualsZero() { return beta < MACH_PREC; } /** * Returns the norm of the updated, preconditioned residual. * * @return the norm of the residual, ||P * r|| */ double getNormOfResidual() { return rnorm; } } /** Key for the exception context. */ private static final String OPERATOR = "operator"; /** Key for the exception context. */ private static final String THRESHOLD = "threshold"; /** Key for the exception context. */ private static final String VECTOR = "vector"; /** Key for the exception context. */ private static final String VECTOR1 = "vector1"; /** Key for the exception context. */ private static final String VECTOR2 = "vector2"; /** {@code true} if symmetry of matrix and conditioner must be checked. */ private final boolean check; /** * The value of the custom tolerance δ for the default stopping * criterion. */ private final double delta; /** * Creates a new instance of this class, with default * stopping criterion. Note that setting {@code check} to {@code true} * entails an extra matrix-vector product in the initial phase. * * @param maxIterations the maximum number of iterations * @param delta the δ parameter for the default stopping criterion * @param check {@code true} if self-adjointedness of both matrix and * preconditioner should be checked */ public SymmLQ(final int maxIterations, final double delta, final boolean check) { super(maxIterations); this.delta = delta; this.check = check; } /** * Creates a new instance of this class, with default * stopping criterion and custom iteration manager. Note that setting * {@code check} to {@code true} entails an extra matrix-vector product in * the initial phase. * * @param manager the custom iteration manager * @param delta the δ parameter for the default stopping criterion * @param check {@code true} if self-adjointedness of both matrix and * preconditioner should be checked */ public SymmLQ(final IterationManager manager, final double delta, final boolean check) { super(manager); this.delta = delta; this.check = check; } /** * Returns {@code true} if symmetry of the matrix, and symmetry as well as * positive definiteness of the preconditioner should be checked. * * @return {@code true} if the tests are to be performed */ public final boolean getCheck() { return check; } /** * {@inheritDoc} * * @throws NonSelfAdjointOperatorException if {@link #getCheck()} is * {@code true}, and {@code a} or {@code m} is not self-adjoint * @throws NonPositiveDefiniteOperatorException if {@code m} is not * positive definite * @throws IllConditionedOperatorException if {@code a} is ill-conditioned */ @Override public RealVector solve(final RealLinearOperator a, final RealLinearOperator m, final RealVector b) throws NullArgumentException, NonSquareOperatorException, DimensionMismatchException, MaxCountExceededException, NonSelfAdjointOperatorException, NonPositiveDefiniteOperatorException, IllConditionedOperatorException { MathUtils.checkNotNull(a); final RealVector x = new ArrayRealVector(a.getColumnDimension()); return solveInPlace(a, m, b, x, false, 0.); } /** * Returns an estimate of the solution to the linear system (A - shift * · I) · x = b. *

                              * If the solution x is expected to contain a large multiple of {@code b} * (as in Rayleigh-quotient iteration), then better precision may be * achieved with {@code goodb} set to {@code true}; this however requires an * extra call to the preconditioner. *

                              *

                              * {@code shift} should be zero if the system A · x = b is to be * solved. Otherwise, it could be an approximation to an eigenvalue of A, * such as the Rayleigh quotient bT · A · b / * (bT · b) corresponding to the vector b. If b is * sufficiently like an eigenvector corresponding to an eigenvalue near * shift, then the computed x may have very large components. When * normalized, x may be closer to an eigenvector than b. *

                              * * @param a the linear operator A of the system * @param m the preconditioner, M (can be {@code null}) * @param b the right-hand side vector * @param goodb usually {@code false}, except if {@code x} is expected to * contain a large multiple of {@code b} * @param shift the amount to be subtracted to all diagonal elements of A * @return a reference to {@code x} (shallow copy) * @throws NullArgumentException if one of the parameters is {@code null} * @throws NonSquareOperatorException if {@code a} or {@code m} is not square * @throws DimensionMismatchException if {@code m} or {@code b} have dimensions * inconsistent with {@code a} * @throws MaxCountExceededException at exhaustion of the iteration count, * unless a custom * {@link org.apache.commons.math3.util.Incrementor.MaxCountExceededCallback callback} * has been set at construction of the {@link IterationManager} * @throws NonSelfAdjointOperatorException if {@link #getCheck()} is * {@code true}, and {@code a} or {@code m} is not self-adjoint * @throws NonPositiveDefiniteOperatorException if {@code m} is not * positive definite * @throws IllConditionedOperatorException if {@code a} is ill-conditioned */ public RealVector solve(final RealLinearOperator a, final RealLinearOperator m, final RealVector b, final boolean goodb, final double shift) throws NullArgumentException, NonSquareOperatorException, DimensionMismatchException, MaxCountExceededException, NonSelfAdjointOperatorException, NonPositiveDefiniteOperatorException, IllConditionedOperatorException { MathUtils.checkNotNull(a); final RealVector x = new ArrayRealVector(a.getColumnDimension()); return solveInPlace(a, m, b, x, goodb, shift); } /** * {@inheritDoc} * * @param x not meaningful in this implementation; should not be considered * as an initial guess (more) * @throws NonSelfAdjointOperatorException if {@link #getCheck()} is * {@code true}, and {@code a} or {@code m} is not self-adjoint * @throws NonPositiveDefiniteOperatorException if {@code m} is not positive * definite * @throws IllConditionedOperatorException if {@code a} is ill-conditioned */ @Override public RealVector solve(final RealLinearOperator a, final RealLinearOperator m, final RealVector b, final RealVector x) throws NullArgumentException, NonSquareOperatorException, DimensionMismatchException, NonSelfAdjointOperatorException, NonPositiveDefiniteOperatorException, IllConditionedOperatorException, MaxCountExceededException { MathUtils.checkNotNull(x); return solveInPlace(a, m, b, x.copy(), false, 0.); } /** * {@inheritDoc} * * @throws NonSelfAdjointOperatorException if {@link #getCheck()} is * {@code true}, and {@code a} is not self-adjoint * @throws IllConditionedOperatorException if {@code a} is ill-conditioned */ @Override public RealVector solve(final RealLinearOperator a, final RealVector b) throws NullArgumentException, NonSquareOperatorException, DimensionMismatchException, NonSelfAdjointOperatorException, IllConditionedOperatorException, MaxCountExceededException { MathUtils.checkNotNull(a); final RealVector x = new ArrayRealVector(a.getColumnDimension()); x.set(0.); return solveInPlace(a, null, b, x, false, 0.); } /** * Returns the solution to the system (A - shift · I) · x = b. *

                              * If the solution x is expected to contain a large multiple of {@code b} * (as in Rayleigh-quotient iteration), then better precision may be * achieved with {@code goodb} set to {@code true}. *

                              *

                              * {@code shift} should be zero if the system A · x = b is to be * solved. Otherwise, it could be an approximation to an eigenvalue of A, * such as the Rayleigh quotient bT · A · b / * (bT · b) corresponding to the vector b. If b is * sufficiently like an eigenvector corresponding to an eigenvalue near * shift, then the computed x may have very large components. When * normalized, x may be closer to an eigenvector than b. *

                              * * @param a the linear operator A of the system * @param b the right-hand side vector * @param goodb usually {@code false}, except if {@code x} is expected to * contain a large multiple of {@code b} * @param shift the amount to be subtracted to all diagonal elements of A * @return a reference to {@code x} * @throws NullArgumentException if one of the parameters is {@code null} * @throws NonSquareOperatorException if {@code a} is not square * @throws DimensionMismatchException if {@code b} has dimensions * inconsistent with {@code a} * @throws MaxCountExceededException at exhaustion of the iteration count, * unless a custom * {@link org.apache.commons.math3.util.Incrementor.MaxCountExceededCallback callback} * has been set at construction of the {@link IterationManager} * @throws NonSelfAdjointOperatorException if {@link #getCheck()} is * {@code true}, and {@code a} is not self-adjoint * @throws IllConditionedOperatorException if {@code a} is ill-conditioned */ public RealVector solve(final RealLinearOperator a, final RealVector b, final boolean goodb, final double shift) throws NullArgumentException, NonSquareOperatorException, DimensionMismatchException, NonSelfAdjointOperatorException, IllConditionedOperatorException, MaxCountExceededException { MathUtils.checkNotNull(a); final RealVector x = new ArrayRealVector(a.getColumnDimension()); return solveInPlace(a, null, b, x, goodb, shift); } /** * {@inheritDoc} * * @param x not meaningful in this implementation; should not be considered * as an initial guess (more) * @throws NonSelfAdjointOperatorException if {@link #getCheck()} is * {@code true}, and {@code a} is not self-adjoint * @throws IllConditionedOperatorException if {@code a} is ill-conditioned */ @Override public RealVector solve(final RealLinearOperator a, final RealVector b, final RealVector x) throws NullArgumentException, NonSquareOperatorException, DimensionMismatchException, NonSelfAdjointOperatorException, IllConditionedOperatorException, MaxCountExceededException { MathUtils.checkNotNull(x); return solveInPlace(a, null, b, x.copy(), false, 0.); } /** * {@inheritDoc} * * @param x the vector to be updated with the solution; {@code x} should * not be considered as an initial guess (more) * @throws NonSelfAdjointOperatorException if {@link #getCheck()} is * {@code true}, and {@code a} or {@code m} is not self-adjoint * @throws NonPositiveDefiniteOperatorException if {@code m} is not * positive definite * @throws IllConditionedOperatorException if {@code a} is ill-conditioned */ @Override public RealVector solveInPlace(final RealLinearOperator a, final RealLinearOperator m, final RealVector b, final RealVector x) throws NullArgumentException, NonSquareOperatorException, DimensionMismatchException, NonSelfAdjointOperatorException, NonPositiveDefiniteOperatorException, IllConditionedOperatorException, MaxCountExceededException { return solveInPlace(a, m, b, x, false, 0.); } /** * Returns an estimate of the solution to the linear system (A - shift * · I) · x = b. The solution is computed in-place. *

                              * If the solution x is expected to contain a large multiple of {@code b} * (as in Rayleigh-quotient iteration), then better precision may be * achieved with {@code goodb} set to {@code true}; this however requires an * extra call to the preconditioner. *

                              *

                              * {@code shift} should be zero if the system A · x = b is to be * solved. Otherwise, it could be an approximation to an eigenvalue of A, * such as the Rayleigh quotient bT · A · b / * (bT · b) corresponding to the vector b. If b is * sufficiently like an eigenvector corresponding to an eigenvalue near * shift, then the computed x may have very large components. When * normalized, x may be closer to an eigenvector than b. *

                              * * @param a the linear operator A of the system * @param m the preconditioner, M (can be {@code null}) * @param b the right-hand side vector * @param x the vector to be updated with the solution; {@code x} should * not be considered as an initial guess (more) * @param goodb usually {@code false}, except if {@code x} is expected to * contain a large multiple of {@code b} * @param shift the amount to be subtracted to all diagonal elements of A * @return a reference to {@code x} (shallow copy). * @throws NullArgumentException if one of the parameters is {@code null} * @throws NonSquareOperatorException if {@code a} or {@code m} is not square * @throws DimensionMismatchException if {@code m}, {@code b} or {@code x} * have dimensions inconsistent with {@code a}. * @throws MaxCountExceededException at exhaustion of the iteration count, * unless a custom * {@link org.apache.commons.math3.util.Incrementor.MaxCountExceededCallback callback} * has been set at construction of the {@link IterationManager} * @throws NonSelfAdjointOperatorException if {@link #getCheck()} is * {@code true}, and {@code a} or {@code m} is not self-adjoint * @throws NonPositiveDefiniteOperatorException if {@code m} is not positive * definite * @throws IllConditionedOperatorException if {@code a} is ill-conditioned */ public RealVector solveInPlace(final RealLinearOperator a, final RealLinearOperator m, final RealVector b, final RealVector x, final boolean goodb, final double shift) throws NullArgumentException, NonSquareOperatorException, DimensionMismatchException, NonSelfAdjointOperatorException, NonPositiveDefiniteOperatorException, IllConditionedOperatorException, MaxCountExceededException { checkParameters(a, m, b, x); final IterationManager manager = getIterationManager(); /* Initialization counts as an iteration. */ manager.resetIterationCount(); manager.incrementIterationCount(); final State state; state = new State(a, m, b, goodb, shift, delta, check); state.init(); state.refineSolution(x); IterativeLinearSolverEvent event; event = new DefaultIterativeLinearSolverEvent(this, manager.getIterations(), x, b, state.getNormOfResidual()); if (state.bEqualsNullVector()) { /* If b = 0 exactly, stop with x = 0. */ manager.fireTerminationEvent(event); return x; } /* Cause termination if beta is essentially zero. */ final boolean earlyStop; earlyStop = state.betaEqualsZero() || state.hasConverged(); manager.fireInitializationEvent(event); if (!earlyStop) { do { manager.incrementIterationCount(); event = new DefaultIterativeLinearSolverEvent(this, manager.getIterations(), x, b, state.getNormOfResidual()); manager.fireIterationStartedEvent(event); state.update(); state.refineSolution(x); event = new DefaultIterativeLinearSolverEvent(this, manager.getIterations(), x, b, state.getNormOfResidual()); manager.fireIterationPerformedEvent(event); } while (!state.hasConverged()); } event = new DefaultIterativeLinearSolverEvent(this, manager.getIterations(), x, b, state.getNormOfResidual()); manager.fireTerminationEvent(event); return x; } /** * {@inheritDoc} * * @param x the vector to be updated with the solution; {@code x} should * not be considered as an initial guess (more) * @throws NonSelfAdjointOperatorException if {@link #getCheck()} is * {@code true}, and {@code a} is not self-adjoint * @throws IllConditionedOperatorException if {@code a} is ill-conditioned */ @Override public RealVector solveInPlace(final RealLinearOperator a, final RealVector b, final RealVector x) throws NullArgumentException, NonSquareOperatorException, DimensionMismatchException, NonSelfAdjointOperatorException, IllConditionedOperatorException, MaxCountExceededException { return solveInPlace(a, null, b, x, false, 0.); } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/linear/RealMatrixChangingVisitor.java100644 1750 1750 4147 12126627717 31741 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.linear; /** * Interface defining a visitor for matrix entries. * * @see DefaultRealMatrixChangingVisitor * @version $Id: RealMatrixChangingVisitor.java 1416643 2012-12-03 19:37:14Z tn $ * @since 2.0 */ public interface RealMatrixChangingVisitor { /** * Start visiting a matrix. *

                              This method is called once before any entry of the matrix is visited.

                              * @param rows number of rows of the matrix * @param columns number of columns of the matrix * @param startRow Initial row index * @param endRow Final row index (inclusive) * @param startColumn Initial column index * @param endColumn Final column index (inclusive) */ void start(int rows, int columns, int startRow, int endRow, int startColumn, int endColumn); /** * Visit one matrix entry. * @param row row index of the entry * @param column column index of the entry * @param value current value of the entry * @return the new value to be set for the entry */ double visit(int row, int column, double value); /** * End visiting a matrix. *

                              This method is called once after all entries of the matrix have been visited.

                              * @return the value that the walkInXxxOrder must return */ double end(); } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/linear/RealVector.java100644 1750 1750 156206 12126627717 26764 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.linear; import java.util.Iterator; import java.util.NoSuchElementException; import org.apache.commons.math3.exception.MathUnsupportedOperationException; import org.apache.commons.math3.exception.DimensionMismatchException; import org.apache.commons.math3.exception.NotPositiveException; import org.apache.commons.math3.exception.NumberIsTooSmallException; import org.apache.commons.math3.exception.OutOfRangeException; import org.apache.commons.math3.exception.MathArithmeticException; import org.apache.commons.math3.analysis.FunctionUtils; import org.apache.commons.math3.analysis.function.Add; import org.apache.commons.math3.analysis.function.Multiply; import org.apache.commons.math3.analysis.function.Divide; import org.apache.commons.math3.analysis.UnivariateFunction; import org.apache.commons.math3.exception.util.LocalizedFormats; import org.apache.commons.math3.util.FastMath; /** * Class defining a real-valued vector with basic algebraic operations. *

                              * vector element indexing is 0-based -- e.g., {@code getEntry(0)} * returns the first element of the vector. *

                              *

                              * The {@code code map} and {@code mapToSelf} methods operate * on vectors element-wise, i.e. they perform the same operation (adding a scalar, * applying a function ...) on each element in turn. The {@code map} * versions create a new vector to hold the result and do not change the instance. * The {@code mapToSelf} version uses the instance itself to store the * results, so the instance is changed by this method. In all cases, the result * vector is returned by the methods, allowing the fluent API * style, like this: *

                              *
                               *   RealVector result = v.mapAddToSelf(3.4).mapToSelf(new Tan()).mapToSelf(new Power(2.3));
                               * 
                              * * @version $Id: RealVector.java 1422313 2012-12-15 18:53:41Z psteitz $ * @since 2.1 */ public abstract class RealVector { /** * Returns the size of the vector. * * @return the size of this vector. */ public abstract int getDimension(); /** * Return the entry at the specified index. * * @param index Index location of entry to be fetched. * @return the vector entry at {@code index}. * @throws OutOfRangeException if the index is not valid. * @see #setEntry(int, double) */ public abstract double getEntry(int index) throws OutOfRangeException; /** * Set a single element. * * @param index element index. * @param value new value for the element. * @throws OutOfRangeException if the index is not valid. * @see #getEntry(int) */ public abstract void setEntry(int index, double value) throws OutOfRangeException; /** * Change an entry at the specified index. * * @param index Index location of entry to be set. * @param increment Value to add to the vector entry. * @throws OutOfRangeException if the index is not valid. * @since 3.0 */ public void addToEntry(int index, double increment) throws OutOfRangeException { setEntry(index, getEntry(index) + increment); } /** * Construct a new vector by appending a vector to this vector. * * @param v vector to append to this one. * @return a new vector. */ public abstract RealVector append(RealVector v); /** * Construct a new vector by appending a double to this vector. * * @param d double to append. * @return a new vector. */ public abstract RealVector append(double d); /** * Get a subvector from consecutive elements. * * @param index index of first element. * @param n number of elements to be retrieved. * @return a vector containing n elements. * @throws OutOfRangeException if the index is not valid. * @throws NotPositiveException if the number of elements is not positive. */ public abstract RealVector getSubVector(int index, int n) throws NotPositiveException, OutOfRangeException; /** * Set a sequence of consecutive elements. * * @param index index of first element to be set. * @param v vector containing the values to set. * @throws OutOfRangeException if the index is not valid. */ public abstract void setSubVector(int index, RealVector v) throws OutOfRangeException; /** * Check whether any coordinate of this vector is {@code NaN}. * * @return {@code true} if any coordinate of this vector is {@code NaN}, * {@code false} otherwise. */ public abstract boolean isNaN(); /** * Check whether any coordinate of this vector is infinite and none are {@code NaN}. * * @return {@code true} if any coordinate of this vector is infinite and * none are {@code NaN}, {@code false} otherwise. */ public abstract boolean isInfinite(); /** * Check if instance and specified vectors have the same dimension. * * @param v Vector to compare instance with. * @throws DimensionMismatchException if the vectors do not * have the same dimension. */ protected void checkVectorDimensions(RealVector v) throws DimensionMismatchException { checkVectorDimensions(v.getDimension()); } /** * Check if instance dimension is equal to some expected value. * * @param n Expected dimension. * @throws DimensionMismatchException if the dimension is * inconsistent with the vector size. */ protected void checkVectorDimensions(int n) throws DimensionMismatchException { int d = getDimension(); if (d != n) { throw new DimensionMismatchException(d, n); } } /** * Check if an index is valid. * * @param index Index to check. * @exception OutOfRangeException if {@code index} is not valid. */ protected void checkIndex(final int index) throws OutOfRangeException { if (index < 0 || index >= getDimension()) { throw new OutOfRangeException(LocalizedFormats.INDEX, index, 0, getDimension() - 1); } } /** * Checks that the indices of a subvector are valid. * * @param start the index of the first entry of the subvector * @param end the index of the last entry of the subvector (inclusive) * @throws OutOfRangeException if {@code start} of {@code end} are not valid * @throws NumberIsTooSmallException if {@code end < start} * @since 3.1 */ protected void checkIndices(final int start, final int end) throws NumberIsTooSmallException, OutOfRangeException { final int dim = getDimension(); if ((start < 0) || (start >= dim)) { throw new OutOfRangeException(LocalizedFormats.INDEX, start, 0, dim - 1); } if ((end < 0) || (end >= dim)) { throw new OutOfRangeException(LocalizedFormats.INDEX, end, 0, dim - 1); } if (end < start) { // TODO Use more specific error message throw new NumberIsTooSmallException(LocalizedFormats.INITIAL_ROW_AFTER_FINAL_ROW, end, start, false); } } /** * Compute the sum of this vector and {@code v}. * Returns a new vector. Does not change instance data. * * @param v Vector to be added. * @return {@code this} + {@code v}. * @throws DimensionMismatchException if {@code v} is not the same size as * {@code this} vector. */ public RealVector add(RealVector v) throws DimensionMismatchException { checkVectorDimensions(v); RealVector result = v.copy(); Iterator it = iterator(); while (it.hasNext()) { final Entry e = it.next(); final int index = e.getIndex(); result.setEntry(index, e.getValue() + result.getEntry(index)); } return result; } /** * Subtract {@code v} from this vector. * Returns a new vector. Does not change instance data. * * @param v Vector to be subtracted. * @return {@code this} - {@code v}. * @throws DimensionMismatchException if {@code v} is not the same size as * {@code this} vector. */ public RealVector subtract(RealVector v) throws DimensionMismatchException { checkVectorDimensions(v); RealVector result = v.mapMultiply(-1d); Iterator it = iterator(); while (it.hasNext()) { final Entry e = it.next(); final int index = e.getIndex(); result.setEntry(index, e.getValue() + result.getEntry(index)); } return result; } /** * Add a value to each entry. * Returns a new vector. Does not change instance data. * * @param d Value to be added to each entry. * @return {@code this} + {@code d}. */ public RealVector mapAdd(double d) { return copy().mapAddToSelf(d); } /** * Add a value to each entry. * The instance is changed in-place. * * @param d Value to be added to each entry. * @return {@code this}. */ public RealVector mapAddToSelf(double d) { if (d != 0) { return mapToSelf(FunctionUtils.fix2ndArgument(new Add(), d)); } return this; } /** * Returns a (deep) copy of this vector. * * @return a vector copy. */ public abstract RealVector copy(); /** * Compute the dot product of this vector with {@code v}. * * @param v Vector with which dot product should be computed * @return the scalar dot product between this instance and {@code v}. * @throws DimensionMismatchException if {@code v} is not the same size as * {@code this} vector. */ public double dotProduct(RealVector v) throws DimensionMismatchException { checkVectorDimensions(v); double d = 0; final int n = getDimension(); for (int i = 0; i < n; i++) { d += getEntry(i) * v.getEntry(i); } return d; } /** * Computes the cosine of the angle between this vector and the * argument. * * @param v Vector. * @return the cosine of the angle between this vector and {@code v}. * @throws MathArithmeticException if {@code this} or {@code v} is the null * vector * @throws DimensionMismatchException if the dimensions of {@code this} and * {@code v} do not match */ public double cosine(RealVector v) throws DimensionMismatchException, MathArithmeticException { final double norm = getNorm(); final double vNorm = v.getNorm(); if (norm == 0 || vNorm == 0) { throw new MathArithmeticException(LocalizedFormats.ZERO_NORM); } return dotProduct(v) / (norm * vNorm); } /** * Element-by-element division. * * @param v Vector by which instance elements must be divided. * @return a vector containing this[i] / v[i] for all i. * @throws DimensionMismatchException if {@code v} is not the same size as * {@code this} vector. * @deprecated As of version 3.1, this method is deprecated, and will be * removed in version 4.0. This decision follows the discussion reported in * MATH-803. * Uses of this method involving sparse implementations of * {@link RealVector} might lead to wrong results. Since there is no * satisfactory correction to this bug, this method is deprecated. Users who * want to preserve this feature are advised to implement * {@link RealVectorPreservingVisitor} (possibly ignoring corner cases for * the sake of efficiency). */ @Deprecated public abstract RealVector ebeDivide(RealVector v) throws DimensionMismatchException; /** * Element-by-element multiplication. * * @param v Vector by which instance elements must be multiplied * @return a vector containing this[i] * v[i] for all i. * @throws DimensionMismatchException if {@code v} is not the same size as * {@code this} vector. * @deprecated As of version 3.1, this method is deprecated, and will be * removed in version 4.0. This decision follows the discussion reported in * MATH-803. * Uses of this method involving sparse implementations of * {@link RealVector} might lead to wrong results. Since there is no * satisfactory correction to this bug, this method is deprecated. Users who * want to preserve this feature are advised to implement * {@link RealVectorPreservingVisitor} (possibly ignoring corner cases for * the sake of efficiency). */ @Deprecated public abstract RealVector ebeMultiply(RealVector v) throws DimensionMismatchException; /** * Distance between two vectors. *

                              This method computes the distance consistent with the * L2 norm, i.e. the square root of the sum of * element differences, or Euclidean distance.

                              * * @param v Vector to which distance is requested. * @return the distance between two vectors. * @throws DimensionMismatchException if {@code v} is not the same size as * {@code this} vector. * @see #getL1Distance(RealVector) * @see #getLInfDistance(RealVector) * @see #getNorm() */ public double getDistance(RealVector v) throws DimensionMismatchException { checkVectorDimensions(v); double d = 0; Iterator it = iterator(); while (it.hasNext()) { final Entry e = it.next(); final double diff = e.getValue() - v.getEntry(e.getIndex()); d += diff * diff; } return FastMath.sqrt(d); } /** * Returns the L2 norm of the vector. *

                              The L2 norm is the root of the sum of * the squared elements.

                              * * @return the norm. * @see #getL1Norm() * @see #getLInfNorm() * @see #getDistance(RealVector) */ public double getNorm() { double sum = 0; Iterator it = iterator(); while (it.hasNext()) { final Entry e = it.next(); final double value = e.getValue(); sum += value * value; } return FastMath.sqrt(sum); } /** * Returns the L1 norm of the vector. *

                              The L1 norm is the sum of the absolute * values of the elements.

                              * * @return the norm. * @see #getNorm() * @see #getLInfNorm() * @see #getL1Distance(RealVector) */ public double getL1Norm() { double norm = 0; Iterator it = iterator(); while (it.hasNext()) { final Entry e = it.next(); norm += FastMath.abs(e.getValue()); } return norm; } /** * Returns the L norm of the vector. *

                              The L norm is the max of the absolute * values of the elements.

                              * * @return the norm. * @see #getNorm() * @see #getL1Norm() * @see #getLInfDistance(RealVector) */ public double getLInfNorm() { double norm = 0; Iterator it = iterator(); while (it.hasNext()) { final Entry e = it.next(); norm = FastMath.max(norm, FastMath.abs(e.getValue())); } return norm; } /** * Distance between two vectors. *

                              This method computes the distance consistent with * L1 norm, i.e. the sum of the absolute values of * the elements differences.

                              * * @param v Vector to which distance is requested. * @return the distance between two vectors. * @throws DimensionMismatchException if {@code v} is not the same size as * {@code this} vector. */ public double getL1Distance(RealVector v) throws DimensionMismatchException { checkVectorDimensions(v); double d = 0; Iterator it = iterator(); while (it.hasNext()) { final Entry e = it.next(); d += FastMath.abs(e.getValue() - v.getEntry(e.getIndex())); } return d; } /** * Distance between two vectors. *

                              This method computes the distance consistent with * L norm, i.e. the max of the absolute values of * element differences.

                              * * @param v Vector to which distance is requested. * @return the distance between two vectors. * @throws DimensionMismatchException if {@code v} is not the same size as * {@code this} vector. * @see #getDistance(RealVector) * @see #getL1Distance(RealVector) * @see #getLInfNorm() */ public double getLInfDistance(RealVector v) throws DimensionMismatchException { checkVectorDimensions(v); double d = 0; Iterator it = iterator(); while (it.hasNext()) { final Entry e = it.next(); d = FastMath.max(FastMath.abs(e.getValue() - v.getEntry(e.getIndex())), d); } return d; } /** * Get the index of the minimum entry. * * @return the index of the minimum entry or -1 if vector length is 0 * or all entries are {@code NaN}. */ public int getMinIndex() { int minIndex = -1; double minValue = Double.POSITIVE_INFINITY; Iterator iterator = iterator(); while (iterator.hasNext()) { final Entry entry = iterator.next(); if (entry.getValue() <= minValue) { minIndex = entry.getIndex(); minValue = entry.getValue(); } } return minIndex; } /** * Get the value of the minimum entry. * * @return the value of the minimum entry or {@code NaN} if all * entries are {@code NaN}. */ public double getMinValue() { final int minIndex = getMinIndex(); return minIndex < 0 ? Double.NaN : getEntry(minIndex); } /** * Get the index of the maximum entry. * * @return the index of the maximum entry or -1 if vector length is 0 * or all entries are {@code NaN} */ public int getMaxIndex() { int maxIndex = -1; double maxValue = Double.NEGATIVE_INFINITY; Iterator iterator = iterator(); while (iterator.hasNext()) { final Entry entry = iterator.next(); if (entry.getValue() >= maxValue) { maxIndex = entry.getIndex(); maxValue = entry.getValue(); } } return maxIndex; } /** * Get the value of the maximum entry. * * @return the value of the maximum entry or {@code NaN} if all * entries are {@code NaN}. */ public double getMaxValue() { final int maxIndex = getMaxIndex(); return maxIndex < 0 ? Double.NaN : getEntry(maxIndex); } /** * Multiply each entry by the argument. Returns a new vector. * Does not change instance data. * * @param d Multiplication factor. * @return {@code this} * {@code d}. */ public RealVector mapMultiply(double d) { return copy().mapMultiplyToSelf(d); } /** * Multiply each entry. * The instance is changed in-place. * * @param d Multiplication factor. * @return {@code this}. */ public RealVector mapMultiplyToSelf(double d){ return mapToSelf(FunctionUtils.fix2ndArgument(new Multiply(), d)); } /** * Subtract a value from each entry. Returns a new vector. * Does not change instance data. * * @param d Value to be subtracted. * @return {@code this} - {@code d}. */ public RealVector mapSubtract(double d) { return copy().mapSubtractToSelf(d); } /** * Subtract a value from each entry. * The instance is changed in-place. * * @param d Value to be subtracted. * @return {@code this}. */ public RealVector mapSubtractToSelf(double d){ return mapAddToSelf(-d); } /** * Divide each entry by the argument. Returns a new vector. * Does not change instance data. * * @param d Value to divide by. * @return {@code this} / {@code d}. */ public RealVector mapDivide(double d) { return copy().mapDivideToSelf(d); } /** * Divide each entry by the argument. * The instance is changed in-place. * * @param d Value to divide by. * @return {@code this}. */ public RealVector mapDivideToSelf(double d){ return mapToSelf(FunctionUtils.fix2ndArgument(new Divide(), d)); } /** * Compute the outer product. * * @param v Vector with which outer product should be computed. * @return the matrix outer product between this instance and {@code v}. */ public RealMatrix outerProduct(RealVector v) { final int m = this.getDimension(); final int n = v.getDimension(); final RealMatrix product; if (v instanceof SparseRealVector || this instanceof SparseRealVector) { product = new OpenMapRealMatrix(m, n); } else { product = new Array2DRowRealMatrix(m, n); } for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { product.setEntry(i, j, this.getEntry(i) * v.getEntry(j)); } } return product; } /** * Find the orthogonal projection of this vector onto another vector. * * @param v vector onto which instance must be projected. * @return projection of the instance onto {@code v}. * @throws DimensionMismatchException if {@code v} is not the same size as * {@code this} vector. * @throws MathArithmeticException if {@code this} or {@code v} is the null * vector */ public RealVector projection(final RealVector v) throws DimensionMismatchException, MathArithmeticException { final double norm2 = v.dotProduct(v); if (norm2 == 0.0) { throw new MathArithmeticException(LocalizedFormats.ZERO_NORM); } return v.mapMultiply(dotProduct(v) / v.dotProduct(v)); } /** * Set all elements to a single value. * * @param value Single value to set for all elements. */ public void set(double value) { Iterator it = iterator(); while (it.hasNext()) { final Entry e = it.next(); e.setValue(value); } } /** * Convert the vector to an array of {@code double}s. * The array is independent from this vector data: the elements * are copied. * * @return an array containing a copy of the vector elements. */ public double[] toArray() { int dim = getDimension(); double[] values = new double[dim]; for (int i = 0; i < dim; i++) { values[i] = getEntry(i); } return values; } /** * Creates a unit vector pointing in the direction of this vector. * The instance is not changed by this method. * * @return a unit vector pointing in direction of this vector. * @throws MathArithmeticException if the norm is zero. */ public RealVector unitVector() throws MathArithmeticException { final double norm = getNorm(); if (norm == 0) { throw new MathArithmeticException(LocalizedFormats.ZERO_NORM); } return mapDivide(norm); } /** * Converts this vector into a unit vector. * The instance itself is changed by this method. * * @throws MathArithmeticException if the norm is zero. */ public void unitize() throws MathArithmeticException { final double norm = getNorm(); if (norm == 0) { throw new MathArithmeticException(LocalizedFormats.ZERO_NORM); } mapDivideToSelf(getNorm()); } /** * Create a sparse iterator over the vector, which may omit some entries. * Specialized implementations may choose to not iterate over all * dimensions, either because those values are unset, or are equal * to defaultValue(), or are small enough to be ignored for the * purposes of iteration. No guarantees are made about order of iteration. * In dense implementations, this method will often delegate to * {@link #iterator()}. * *

                              Note: derived classes are required to return an {@link Iterator} that * returns non-null {@link Entry} objects as long as {@link Iterator#hasNext()} * returns {@code true}.

                              * * @return a sparse iterator. * @deprecated As of 3.1, this method is deprecated, because its interface * is too confusing (see * JIRA MATH-875). * This method will be completely removed in 4.0. */ @Deprecated public Iterator sparseIterator() { return new SparseEntryIterator(); } /** * Generic dense iterator. Iteration is in increasing order * of the vector index. * *

                              Note: derived classes are required to return an {@link Iterator} that * returns non-null {@link Entry} objects as long as {@link Iterator#hasNext()} * returns {@code true}.

                              * * @return a dense iterator. */ public Iterator iterator() { final int dim = getDimension(); return new Iterator() { /** Current index. */ private int i = 0; /** Current entry. */ private Entry e = new Entry(); /** {@inheritDoc} */ public boolean hasNext() { return i < dim; } /** {@inheritDoc} */ public Entry next() { if (i < dim) { e.setIndex(i++); return e; } else { throw new NoSuchElementException(); } } /** * {@inheritDoc} * * @throws MathUnsupportedOperationException in all circumstances. */ public void remove() throws MathUnsupportedOperationException { throw new MathUnsupportedOperationException(); } }; } /** * Acts as if implemented as: *
                                   *  return copy().mapToSelf(function);
                                   * 
                              * Returns a new vector. Does not change instance data. * * @param function Function to apply to each entry. * @return a new vector. */ public RealVector map(UnivariateFunction function) { return copy().mapToSelf(function); } /** * Acts as if it is implemented as: *
                                   *  Entry e = null;
                                   *  for(Iterator it = iterator(); it.hasNext(); e = it.next()) {
                                   *      e.setValue(function.value(e.getValue()));
                                   *  }
                                   * 
                              * Entries of this vector are modified in-place by this method. * * @param function Function to apply to each entry. * @return a reference to this vector. */ public RealVector mapToSelf(UnivariateFunction function) { Iterator it = iterator(); while (it.hasNext()) { final Entry e = it.next(); e.setValue(function.value(e.getValue())); } return this; } /** * Returns a new vector representing {@code a * this + b * y}, the linear * combination of {@code this} and {@code y}. * Returns a new vector. Does not change instance data. * * @param a Coefficient of {@code this}. * @param b Coefficient of {@code y}. * @param y Vector with which {@code this} is linearly combined. * @return a vector containing {@code a * this[i] + b * y[i]} for all * {@code i}. * @throws DimensionMismatchException if {@code y} is not the same size as * {@code this} vector. */ public RealVector combine(double a, double b, RealVector y) throws DimensionMismatchException { return copy().combineToSelf(a, b, y); } /** * Updates {@code this} with the linear combination of {@code this} and * {@code y}. * * @param a Weight of {@code this}. * @param b Weight of {@code y}. * @param y Vector with which {@code this} is linearly combined. * @return {@code this}, with components equal to * {@code a * this[i] + b * y[i]} for all {@code i}. * @throws DimensionMismatchException if {@code y} is not the same size as * {@code this} vector. */ public RealVector combineToSelf(double a, double b, RealVector y) throws DimensionMismatchException { checkVectorDimensions(y); for (int i = 0; i < getDimension(); i++) { final double xi = getEntry(i); final double yi = y.getEntry(i); setEntry(i, a * xi + b * yi); } return this; } /** * Visits (but does not alter) all entries of this vector in default order * (increasing index). * * @param visitor the visitor to be used to process the entries of this * vector * @return the value returned by {@link RealVectorPreservingVisitor#end()} * at the end of the walk * @since 3.1 */ public double walkInDefaultOrder(final RealVectorPreservingVisitor visitor) { final int dim = getDimension(); visitor.start(dim, 0, dim - 1); for (int i = 0; i < dim; i++) { visitor.visit(i, getEntry(i)); } return visitor.end(); } /** * Visits (but does not alter) some entries of this vector in default order * (increasing index). * * @param visitor visitor to be used to process the entries of this vector * @param start the index of the first entry to be visited * @param end the index of the last entry to be visited (inclusive) * @return the value returned by {@link RealVectorPreservingVisitor#end()} * at the end of the walk * @throws NumberIsTooSmallException if {@code end < start}. * @throws OutOfRangeException if the indices are not valid. * @since 3.1 */ public double walkInDefaultOrder(final RealVectorPreservingVisitor visitor, final int start, final int end) throws NumberIsTooSmallException, OutOfRangeException { checkIndices(start, end); visitor.start(getDimension(), start, end); for (int i = start; i <= end; i++) { visitor.visit(i, getEntry(i)); } return visitor.end(); } /** * Visits (but does not alter) all entries of this vector in optimized * order. The order in which the entries are visited is selected so as to * lead to the most efficient implementation; it might depend on the * concrete implementation of this abstract class. * * @param visitor the visitor to be used to process the entries of this * vector * @return the value returned by {@link RealVectorPreservingVisitor#end()} * at the end of the walk * @since 3.1 */ public double walkInOptimizedOrder(final RealVectorPreservingVisitor visitor) { return walkInDefaultOrder(visitor); } /** * Visits (but does not alter) some entries of this vector in optimized * order. The order in which the entries are visited is selected so as to * lead to the most efficient implementation; it might depend on the * concrete implementation of this abstract class. * * @param visitor visitor to be used to process the entries of this vector * @param start the index of the first entry to be visited * @param end the index of the last entry to be visited (inclusive) * @return the value returned by {@link RealVectorPreservingVisitor#end()} * at the end of the walk * @throws NumberIsTooSmallException if {@code end < start}. * @throws OutOfRangeException if the indices are not valid. * @since 3.1 */ public double walkInOptimizedOrder(final RealVectorPreservingVisitor visitor, final int start, final int end) throws NumberIsTooSmallException, OutOfRangeException { return walkInDefaultOrder(visitor, start, end); } /** * Visits (and possibly alters) all entries of this vector in default order * (increasing index). * * @param visitor the visitor to be used to process and modify the entries * of this vector * @return the value returned by {@link RealVectorChangingVisitor#end()} * at the end of the walk * @since 3.1 */ public double walkInDefaultOrder(final RealVectorChangingVisitor visitor) { final int dim = getDimension(); visitor.start(dim, 0, dim - 1); for (int i = 0; i < dim; i++) { setEntry(i, visitor.visit(i, getEntry(i))); } return visitor.end(); } /** * Visits (and possibly alters) some entries of this vector in default order * (increasing index). * * @param visitor visitor to be used to process the entries of this vector * @param start the index of the first entry to be visited * @param end the index of the last entry to be visited (inclusive) * @return the value returned by {@link RealVectorChangingVisitor#end()} * at the end of the walk * @throws NumberIsTooSmallException if {@code end < start}. * @throws OutOfRangeException if the indices are not valid. * @since 3.1 */ public double walkInDefaultOrder(final RealVectorChangingVisitor visitor, final int start, final int end) throws NumberIsTooSmallException, OutOfRangeException { checkIndices(start, end); visitor.start(getDimension(), start, end); for (int i = start; i <= end; i++) { setEntry(i, visitor.visit(i, getEntry(i))); } return visitor.end(); } /** * Visits (and possibly alters) all entries of this vector in optimized * order. The order in which the entries are visited is selected so as to * lead to the most efficient implementation; it might depend on the * concrete implementation of this abstract class. * * @param visitor the visitor to be used to process the entries of this * vector * @return the value returned by {@link RealVectorChangingVisitor#end()} * at the end of the walk * @since 3.1 */ public double walkInOptimizedOrder(final RealVectorChangingVisitor visitor) { return walkInDefaultOrder(visitor); } /** * Visits (and possibly change) some entries of this vector in optimized * order. The order in which the entries are visited is selected so as to * lead to the most efficient implementation; it might depend on the * concrete implementation of this abstract class. * * @param visitor visitor to be used to process the entries of this vector * @param start the index of the first entry to be visited * @param end the index of the last entry to be visited (inclusive) * @return the value returned by {@link RealVectorChangingVisitor#end()} * at the end of the walk * @throws NumberIsTooSmallException if {@code end < start}. * @throws OutOfRangeException if the indices are not valid. * @since 3.1 */ public double walkInOptimizedOrder(final RealVectorChangingVisitor visitor, final int start, final int end) throws NumberIsTooSmallException, OutOfRangeException { return walkInDefaultOrder(visitor, start, end); } /** An entry in the vector. */ protected class Entry { /** Index of this entry. */ private int index; /** Simple constructor. */ public Entry() { setIndex(0); } /** * Get the value of the entry. * * @return the value of the entry. */ public double getValue() { return getEntry(getIndex()); } /** * Set the value of the entry. * * @param value New value for the entry. */ public void setValue(double value) { setEntry(getIndex(), value); } /** * Get the index of the entry. * * @return the index of the entry. */ public int getIndex() { return index; } /** * Set the index of the entry. * * @param index New index for the entry. */ public void setIndex(int index) { this.index = index; } } /** *

                              * Test for the equality of two real vectors. If all coordinates of two real * vectors are exactly the same, and none are {@code NaN}, the two real * vectors are considered to be equal. {@code NaN} coordinates are * considered to affect globally the vector and be equals to each other - * i.e, if either (or all) coordinates of the real vector are equal to * {@code NaN}, the real vector is equal to a vector with all {@code NaN} * coordinates. *

                              *

                              * This method must be overriden by concrete subclasses of * {@link RealVector} (the current implementation throws an exception). *

                              * * @param other Object to test for equality. * @return {@code true} if two vector objects are equal, {@code false} if * {@code other} is null, not an instance of {@code RealVector}, or * not equal to this {@code RealVector} instance. * @throws MathUnsupportedOperationException if this method is not * overridden. */ @Override public boolean equals(Object other) throws MathUnsupportedOperationException { throw new MathUnsupportedOperationException(); } /** * {@inheritDoc}. This method must be overriden by concrete * subclasses of {@link RealVector} (current implementation throws an * exception). * * @throws MathUnsupportedOperationException if this method is not * overridden. */ @Override public int hashCode() throws MathUnsupportedOperationException { throw new MathUnsupportedOperationException(); } /** * This class should rarely be used, but is here to provide * a default implementation of sparseIterator(), which is implemented * by walking over the entries, skipping those whose values are the default one. * * Concrete subclasses which are SparseVector implementations should * make their own sparse iterator, rather than using this one. * * This implementation might be useful for ArrayRealVector, when expensive * operations which preserve the default value are to be done on the entries, * and the fraction of non-default values is small (i.e. someone took a * SparseVector, and passed it into the copy-constructor of ArrayRealVector) * * @deprecated As of 3.1, this class is deprecated, see * JIRA MATH-875. * This class will be completely removed in 4.0. */ @Deprecated protected class SparseEntryIterator implements Iterator { /** Dimension of the vector. */ private final int dim; /** Last entry returned by {@link #next()}. */ private Entry current; /** Next entry for {@link #next()} to return. */ private Entry next; /** Simple constructor. */ protected SparseEntryIterator() { dim = getDimension(); current = new Entry(); next = new Entry(); if (next.getValue() == 0) { advance(next); } } /** * Advance an entry up to the next nonzero one. * * @param e entry to advance. */ protected void advance(Entry e) { if (e == null) { return; } do { e.setIndex(e.getIndex() + 1); } while (e.getIndex() < dim && e.getValue() == 0); if (e.getIndex() >= dim) { e.setIndex(-1); } } /** {@inheritDoc} */ public boolean hasNext() { return next.getIndex() >= 0; } /** {@inheritDoc} */ public Entry next() { int index = next.getIndex(); if (index < 0) { throw new NoSuchElementException(); } current.setIndex(index); advance(next); return current; } /** * {@inheritDoc} * * @throws MathUnsupportedOperationException in all circumstances. */ public void remove() throws MathUnsupportedOperationException { throw new MathUnsupportedOperationException(); } } /** * Returns an unmodifiable view of the specified vector. * The returned vector has read-only access. An attempt to modify it will * result in a {@link MathUnsupportedOperationException}. However, the * returned vector is not immutable, since any modification of * {@code v} will also change the returned view. * For example, in the following piece of code *
                                   *     RealVector v = new ArrayRealVector(2);
                                   *     RealVector w = RealVector.unmodifiableRealVector(v);
                                   *     v.setEntry(0, 1.2);
                                   *     v.setEntry(1, -3.4);
                                   * 
                              * the changes will be seen in the {@code w} view of {@code v}. * * @param v Vector for which an unmodifiable view is to be returned. * @return an unmodifiable view of {@code v}. */ public static RealVector unmodifiableRealVector(final RealVector v) { /** * This anonymous class is an implementation of {@link RealVector} * with read-only access. * It wraps any {@link RealVector}, and exposes all methods which * do not modify it. Invoking methods which should normally result * in the modification of the calling {@link RealVector} results in * a {@link MathUnsupportedOperationException}. It should be noted * that {@link UnmodifiableVector} is not immutable. */ return new RealVector() { /** * {@inheritDoc} * * @throws MathUnsupportedOperationException in all circumstances. */ @Override public RealVector mapToSelf(UnivariateFunction function) throws MathUnsupportedOperationException { throw new MathUnsupportedOperationException(); } /** {@inheritDoc} */ @Override public RealVector map(UnivariateFunction function) { return v.map(function); } /** {@inheritDoc} */ @Override public Iterator iterator() { final Iterator i = v.iterator(); return new Iterator() { /** The current entry. */ private final UnmodifiableEntry e = new UnmodifiableEntry(); /** {@inheritDoc} */ public boolean hasNext() { return i.hasNext(); } /** {@inheritDoc} */ public Entry next() { e.setIndex(i.next().getIndex()); return e; } /** * {@inheritDoc} * * @throws MathUnsupportedOperationException in all * circumstances. */ public void remove() throws MathUnsupportedOperationException { throw new MathUnsupportedOperationException(); } }; } /** {@inheritDoc} */ @Override public Iterator sparseIterator() { final Iterator i = v.sparseIterator(); return new Iterator() { /** The current entry. */ private final UnmodifiableEntry e = new UnmodifiableEntry(); /** {@inheritDoc} */ public boolean hasNext() { return i.hasNext(); } /** {@inheritDoc} */ public Entry next() { e.setIndex(i.next().getIndex()); return e; } /** * {@inheritDoc} * * @throws MathUnsupportedOperationException in all * circumstances. */ public void remove() throws MathUnsupportedOperationException { throw new MathUnsupportedOperationException(); } }; } /** {@inheritDoc} */ @Override public RealVector copy() { return v.copy(); } /** {@inheritDoc} */ @Override public RealVector add(RealVector w) throws DimensionMismatchException { return v.add(w); } /** {@inheritDoc} */ @Override public RealVector subtract(RealVector w) throws DimensionMismatchException { return v.subtract(w); } /** {@inheritDoc} */ @Override public RealVector mapAdd(double d) { return v.mapAdd(d); } /** * {@inheritDoc} * * @throws MathUnsupportedOperationException in all * circumstances. */ @Override public RealVector mapAddToSelf(double d) throws MathUnsupportedOperationException { throw new MathUnsupportedOperationException(); } /** {@inheritDoc} */ @Override public RealVector mapSubtract(double d) { return v.mapSubtract(d); } /** * {@inheritDoc} * * @throws MathUnsupportedOperationException in all * circumstances. */ @Override public RealVector mapSubtractToSelf(double d) throws MathUnsupportedOperationException { throw new MathUnsupportedOperationException(); } /** {@inheritDoc} */ @Override public RealVector mapMultiply(double d) { return v.mapMultiply(d); } /** * {@inheritDoc} * * @throws MathUnsupportedOperationException in all * circumstances. */ @Override public RealVector mapMultiplyToSelf(double d) throws MathUnsupportedOperationException { throw new MathUnsupportedOperationException(); } /** {@inheritDoc} */ @Override public RealVector mapDivide(double d) { return v.mapDivide(d); } /** * {@inheritDoc} * * @throws MathUnsupportedOperationException in all * circumstances. */ @Override public RealVector mapDivideToSelf(double d) throws MathUnsupportedOperationException { throw new MathUnsupportedOperationException(); } /** {@inheritDoc} */ @Override public RealVector ebeMultiply(RealVector w) throws DimensionMismatchException { return v.ebeMultiply(w); } /** {@inheritDoc} */ @Override public RealVector ebeDivide(RealVector w) throws DimensionMismatchException { return v.ebeDivide(w); } /** {@inheritDoc} */ @Override public double dotProduct(RealVector w) throws DimensionMismatchException { return v.dotProduct(w); } /** {@inheritDoc} */ @Override public double cosine(RealVector w) throws DimensionMismatchException, MathArithmeticException { return v.cosine(w); } /** {@inheritDoc} */ @Override public double getNorm() { return v.getNorm(); } /** {@inheritDoc} */ @Override public double getL1Norm() { return v.getL1Norm(); } /** {@inheritDoc} */ @Override public double getLInfNorm() { return v.getLInfNorm(); } /** {@inheritDoc} */ @Override public double getDistance(RealVector w) throws DimensionMismatchException { return v.getDistance(w); } /** {@inheritDoc} */ @Override public double getL1Distance(RealVector w) throws DimensionMismatchException { return v.getL1Distance(w); } /** {@inheritDoc} */ @Override public double getLInfDistance(RealVector w) throws DimensionMismatchException { return v.getLInfDistance(w); } /** {@inheritDoc} */ @Override public RealVector unitVector() throws MathArithmeticException { return v.unitVector(); } /** * {@inheritDoc} * * @throws MathUnsupportedOperationException in all * circumstances. */ @Override public void unitize() throws MathUnsupportedOperationException { throw new MathUnsupportedOperationException(); } /** {@inheritDoc} */ @Override public RealMatrix outerProduct(RealVector w) { return v.outerProduct(w); } /** {@inheritDoc} */ @Override public double getEntry(int index) throws OutOfRangeException { return v.getEntry(index); } /** * {@inheritDoc} * * @throws MathUnsupportedOperationException in all * circumstances. */ @Override public void setEntry(int index, double value) throws MathUnsupportedOperationException { throw new MathUnsupportedOperationException(); } /** * {@inheritDoc} * * @throws MathUnsupportedOperationException in all * circumstances. */ @Override public void addToEntry(int index, double value) throws MathUnsupportedOperationException { throw new MathUnsupportedOperationException(); } /** {@inheritDoc} */ @Override public int getDimension() { return v.getDimension(); } /** {@inheritDoc} */ @Override public RealVector append(RealVector w) { return v.append(w); } /** {@inheritDoc} */ @Override public RealVector append(double d) { return v.append(d); } /** {@inheritDoc} */ @Override public RealVector getSubVector(int index, int n) throws OutOfRangeException, NotPositiveException { return v.getSubVector(index, n); } /** * {@inheritDoc} * * @throws MathUnsupportedOperationException in all * circumstances. */ @Override public void setSubVector(int index, RealVector w) throws MathUnsupportedOperationException { throw new MathUnsupportedOperationException(); } /** * {@inheritDoc} * * @throws MathUnsupportedOperationException in all * circumstances. */ @Override public void set(double value) throws MathUnsupportedOperationException { throw new MathUnsupportedOperationException(); } /** {@inheritDoc} */ @Override public double[] toArray() { return v.toArray(); } /** {@inheritDoc} */ @Override public boolean isNaN() { return v.isNaN(); } /** {@inheritDoc} */ @Override public boolean isInfinite() { return v.isInfinite(); } /** {@inheritDoc} */ @Override public RealVector combine(double a, double b, RealVector y) throws DimensionMismatchException { return v.combine(a, b, y); } /** * {@inheritDoc} * * @throws MathUnsupportedOperationException in all * circumstances. */ @Override public RealVector combineToSelf(double a, double b, RealVector y) throws MathUnsupportedOperationException { throw new MathUnsupportedOperationException(); } /** An entry in the vector. */ class UnmodifiableEntry extends Entry { /** {@inheritDoc} */ @Override public double getValue() { return v.getEntry(getIndex()); } /** * {@inheritDoc} * * @throws MathUnsupportedOperationException in all * circumstances. */ @Override public void setValue(double value) throws MathUnsupportedOperationException { throw new MathUnsupportedOperationException(); } } }; } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/linear/RRQRDecomposition.java100644 1750 1750 17765 12126627717 30227 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.linear; import org.apache.commons.math3.util.FastMath; /** * Calculates the rank-revealing QR-decomposition of a matrix, with column pivoting. *

                              The rank-revealing QR-decomposition of a matrix A consists of three matrices Q, * R and P such that AP=QR. Q is orthogonal (QTQ = I), and R is upper triangular. * If A is m×n, Q is m×m and R is m×n and P is n×n.

                              *

                              QR decomposition with column pivoting produces a rank-revealing QR * decomposition and the {@link #getRank(double)} method may be used to return the rank of the * input matrix A.

                              *

                              This class compute the decomposition using Householder reflectors.

                              *

                              For efficiency purposes, the decomposition in packed form is transposed. * This allows inner loop to iterate inside rows, which is much more cache-efficient * in Java.

                              *

                              This class is based on the class with similar name from the * JAMA library, with the * following changes:

                              *
                                *
                              • a {@link #getQT() getQT} method has been added,
                              • *
                              • the {@code solve} and {@code isFullRank} methods have been replaced * by a {@link #getSolver() getSolver} method and the equivalent methods * provided by the returned {@link DecompositionSolver}.
                              • *
                              * * @see MathWorld * @see Wikipedia * * @version $Id: RRQRDecomposition.java 1456956 2013-03-15 13:54:20Z luc $ * @since 3.2 */ public class RRQRDecomposition extends QRDecomposition { /** An array to record the column pivoting for later creation of P. */ private int[] p; /** Cached value of P. */ private RealMatrix cachedP; /** * Calculates the QR-decomposition of the given matrix. * The singularity threshold defaults to zero. * * @param matrix The matrix to decompose. * * @see #RRQRDecomposition(RealMatrix, double) */ public RRQRDecomposition(RealMatrix matrix) { this(matrix, 0d); } /** * Calculates the QR-decomposition of the given matrix. * * @param matrix The matrix to decompose. * @param threshold Singularity threshold. * @see #RRQRDecomposition(RealMatrix) */ public RRQRDecomposition(RealMatrix matrix, double threshold) { super(matrix, threshold); } /** Decompose matrix. * @param qrt transposed matrix */ protected void decompose(double[][] qrt) { p = new int[qrt.length]; for (int i = 0; i < p.length; i++) { p[i] = i; } super.decompose(qrt); } /** Perform Householder reflection for a minor A(minor, minor) of A. * @param minor minor index * @param qrt transposed matrix */ protected void performHouseholderReflection(int minor, double[][] qrt) { double l2NormSquaredMax = 0; // Find the unreduced column with the greatest L2-Norm int l2NormSquaredMaxIndex = minor; for (int i = minor; i < qrt.length; i++) { double l2NormSquared = 0; for (int j = 0; j < qrt[i].length; j++) { l2NormSquared += qrt[i][j] * qrt[i][j]; } if (l2NormSquared > l2NormSquaredMax) { l2NormSquaredMax = l2NormSquared; l2NormSquaredMaxIndex = i; } } // swap the current column with that with the greated L2-Norm and record in p if (l2NormSquaredMaxIndex != minor) { double[] tmp1 = qrt[minor]; qrt[minor] = qrt[l2NormSquaredMaxIndex]; qrt[l2NormSquaredMaxIndex] = tmp1; int tmp2 = p[minor]; p[minor] = p[l2NormSquaredMaxIndex]; p[l2NormSquaredMaxIndex] = tmp2; } super.performHouseholderReflection(minor, qrt); } /** * Returns the pivot matrix, P, used in the QR Decomposition of matrix A such that AP = QR. * * If no pivoting is used in this decomposition then P is equal to the identity matrix. * * @return a permutation matrix. */ public RealMatrix getP() { if (cachedP == null) { int n = p.length; cachedP = MatrixUtils.createRealMatrix(n,n); for (int i = 0; i < n; i++) { cachedP.setEntry(p[i], i, 1); } } return cachedP ; } /** * Return the effective numerical matrix rank. *

                              The effective numerical rank is the number of non-negligible * singular values.

                              *

                              This implementation looks at Frobenius norms of the sequence of * bottom right submatrices. When a large fall in norm is seen, * the rank is returned. The drop is computed as:

                              *
                                   *   (thisNorm/lastNorm) * rNorm < dropThreshold
                                   * 
                              *

                              * where thisNorm is the Frobenius norm of the current submatrix, * lastNorm is the Frobenius norm of the previous submatrix, * rNorm is is the Frobenius norm of the complete matrix *

                              * * @param dropThreshold threshold triggering rank computation * @return effective numerical matrix rank */ public int getRank(final double dropThreshold) { RealMatrix r = getR(); int rows = r.getRowDimension(); int columns = r.getColumnDimension(); int rank = 1; double lastNorm = r.getFrobeniusNorm(); double rNorm = lastNorm; while (rank < FastMath.min(rows, columns)) { double thisNorm = r.getSubMatrix(rank, rows - 1, rank, columns - 1).getFrobeniusNorm(); if (thisNorm == 0 || (thisNorm / lastNorm) * rNorm < dropThreshold) { break; } lastNorm = thisNorm; rank++; } return rank; } /** * Get a solver for finding the A × X = B solution in least square sense. * @return a solver */ public DecompositionSolver getSolver() { return new Solver(super.getSolver(), this.getP()); } /** Specialized solver. */ private static class Solver implements DecompositionSolver { /** Upper level solver. */ private final DecompositionSolver upper; /** A permutation matrix for the pivots used in the QR decomposition */ private RealMatrix p; /** * Build a solver from decomposed matrix. * * @param upper upper level solver. * @param p permutation matrix */ private Solver(final DecompositionSolver upper, final RealMatrix p) { this.upper = upper; this.p = p; } /** {@inheritDoc} */ public boolean isNonSingular() { return upper.isNonSingular(); } /** {@inheritDoc} */ public RealVector solve(RealVector b) { return p.operate(upper.solve(b)); } /** {@inheritDoc} */ public RealMatrix solve(RealMatrix b) { return p.multiply(upper.solve(b)); } /** {@inheritDoc} */ public RealMatrix getInverse() { return solve(MatrixUtils.createRealIdentityMatrix(p.getRowDimension())); } } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/linear/NonSquareOperatorException.java100644 1750 1750 3160 12126627717 32152 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.linear; import org.apache.commons.math3.exception.DimensionMismatchException; import org.apache.commons.math3.exception.util.LocalizedFormats; /** * Exception to be thrown when a square linear operator is expected. * * @since 3.0 * @version $Id: NonSquareOperatorException.java 1416643 2012-12-03 19:37:14Z tn $ */ public class NonSquareOperatorException extends DimensionMismatchException { /** Serializable version Id. */ private static final long serialVersionUID = -4145007524150846242L; /** * Construct an exception from the mismatched dimensions. * * @param wrong Row dimension. * @param expected Column dimension. */ public NonSquareOperatorException(int wrong, int expected) { super(LocalizedFormats.NON_SQUARE_OPERATOR, wrong, expected); } } ././@LongLink100644 0 0 152 12126630647 10260 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/linear/RectangularCholeskyDecomposition.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/linear/RectangularCholeskyDecomposition100644 1750 1750 17125 12126627717 32460 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.linear; import org.apache.commons.math3.util.FastMath; /** * Calculates the rectangular Cholesky decomposition of a matrix. *

                              The rectangular Cholesky decomposition of a real symmetric positive * semidefinite matrix A consists of a rectangular matrix B with the same * number of rows such that: A is almost equal to BBT, depending * on a user-defined tolerance. In a sense, this is the square root of A.

                              *

                              The difference with respect to the regular {@link CholeskyDecomposition} * is that rows/columns may be permuted (hence the rectangular shape instead * of the traditional triangular shape) and there is a threshold to ignore * small diagonal elements. This is used for example to generate {@link * org.apache.commons.math3.random.CorrelatedRandomVectorGenerator correlated * random n-dimensions vectors} in a p-dimension subspace (p < n). * In other words, it allows generating random vectors from a covariance * matrix that is only positive semidefinite, and not positive definite.

                              *

                              Rectangular Cholesky decomposition is not suited for solving * linear systems, so it does not provide any {@link DecompositionSolver * decomposition solver}.

                              * * @see MathWorld * @see Wikipedia * @version $Id: RectangularCholeskyDecomposition.java 1422313 2012-12-15 18:53:41Z psteitz $ * @since 2.0 (changed to concrete class in 3.0) */ public class RectangularCholeskyDecomposition { /** Permutated Cholesky root of the symmetric positive semidefinite matrix. */ private final RealMatrix root; /** Rank of the symmetric positive semidefinite matrix. */ private int rank; /** * Decompose a symmetric positive semidefinite matrix. *

                              * Note: this constructor follows the linpack method to detect dependent * columns by proceeding with the Cholesky algorithm until a nonpositive diagonal * element is encountered. * * @see * Analysis of the Cholesky Decomposition of a Semi-definite Matrix * * @param matrix Symmetric positive semidefinite matrix. * @exception NonPositiveDefiniteMatrixException if the matrix is not * positive semidefinite. * @since 3.1 */ public RectangularCholeskyDecomposition(RealMatrix matrix) throws NonPositiveDefiniteMatrixException { this(matrix, 0); } /** * Decompose a symmetric positive semidefinite matrix. * * @param matrix Symmetric positive semidefinite matrix. * @param small Diagonal elements threshold under which columns are * considered to be dependent on previous ones and are discarded. * @exception NonPositiveDefiniteMatrixException if the matrix is not * positive semidefinite. */ public RectangularCholeskyDecomposition(RealMatrix matrix, double small) throws NonPositiveDefiniteMatrixException { final int order = matrix.getRowDimension(); final double[][] c = matrix.getData(); final double[][] b = new double[order][order]; int[] index = new int[order]; for (int i = 0; i < order; ++i) { index[i] = i; } int r = 0; for (boolean loop = true; loop;) { // find maximal diagonal element int swapR = r; for (int i = r + 1; i < order; ++i) { int ii = index[i]; int isr = index[swapR]; if (c[ii][ii] > c[isr][isr]) { swapR = i; } } // swap elements if (swapR != r) { final int tmpIndex = index[r]; index[r] = index[swapR]; index[swapR] = tmpIndex; final double[] tmpRow = b[r]; b[r] = b[swapR]; b[swapR] = tmpRow; } // check diagonal element int ir = index[r]; if (c[ir][ir] <= small) { if (r == 0) { throw new NonPositiveDefiniteMatrixException(c[ir][ir], ir, small); } // check remaining diagonal elements for (int i = r; i < order; ++i) { if (c[index[i]][index[i]] < -small) { // there is at least one sufficiently negative diagonal element, // the symmetric positive semidefinite matrix is wrong throw new NonPositiveDefiniteMatrixException(c[index[i]][index[i]], i, small); } } // all remaining diagonal elements are close to zero, we consider we have // found the rank of the symmetric positive semidefinite matrix loop = false; } else { // transform the matrix final double sqrt = FastMath.sqrt(c[ir][ir]); b[r][r] = sqrt; final double inverse = 1 / sqrt; final double inverse2 = 1 / c[ir][ir]; for (int i = r + 1; i < order; ++i) { final int ii = index[i]; final double e = inverse * c[ii][ir]; b[i][r] = e; c[ii][ii] -= c[ii][ir] * c[ii][ir] * inverse2; for (int j = r + 1; j < i; ++j) { final int ij = index[j]; final double f = c[ii][ij] - e * b[j][r]; c[ii][ij] = f; c[ij][ii] = f; } } // prepare next iteration loop = ++r < order; } } // build the root matrix rank = r; root = MatrixUtils.createRealMatrix(order, r); for (int i = 0; i < order; ++i) { for (int j = 0; j < r; ++j) { root.setEntry(index[i], j, b[i][j]); } } } /** Get the root of the covariance matrix. * The root is the rectangular matrix B such that * the covariance matrix is equal to B.BT * @return root of the square matrix * @see #getRank() */ public RealMatrix getRootMatrix() { return root; } /** Get the rank of the symmetric positive semidefinite matrix. * The r is the number of independent rows in the symmetric positive semidefinite * matrix, it is also the number of columns of the rectangular * matrix of the decomposition. * @return r of the square matrix. * @see #getRootMatrix() */ public int getRank() { return rank; } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/linear/FieldMatrix.java100644 1750 1750 110214 12126627717 27113 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.linear; import org.apache.commons.math3.Field; import org.apache.commons.math3.FieldElement; import org.apache.commons.math3.exception.DimensionMismatchException; import org.apache.commons.math3.exception.NoDataException; import org.apache.commons.math3.exception.NotPositiveException; import org.apache.commons.math3.exception.NotStrictlyPositiveException; import org.apache.commons.math3.exception.NullArgumentException; import org.apache.commons.math3.exception.NumberIsTooSmallException; import org.apache.commons.math3.exception.OutOfRangeException; /** * Interface defining field-valued matrix with basic algebraic operations. *

                              * Matrix element indexing is 0-based -- e.g., getEntry(0, 0) * returns the element in the first row, first column of the matrix.

                              * * @param the type of the field elements * @version $Id: FieldMatrix.java 1416643 2012-12-03 19:37:14Z tn $ */ public interface FieldMatrix> extends AnyMatrix { /** * Get the type of field elements of the matrix. * * @return the type of field elements of the matrix. */ Field getField(); /** * Create a new FieldMatrix of the same type as the instance with * the supplied row and column dimensions. * * @param rowDimension the number of rows in the new matrix * @param columnDimension the number of columns in the new matrix * @return a new matrix of the same type as the instance * @throws NotStrictlyPositiveException if row or column dimension is not * positive. * @since 2.0 */ FieldMatrix createMatrix(final int rowDimension, final int columnDimension) throws NotStrictlyPositiveException; /** * Make a (deep) copy of this. * * @return a copy of this matrix. */ FieldMatrix copy(); /** * Compute the sum of this and m. * * @param m Matrix to be added. * @return {@code this} + {@code m}. * @throws MatrixDimensionMismatchException if {@code m} is not the same * size as {@code this} matrix. */ FieldMatrix add(FieldMatrix m) throws MatrixDimensionMismatchException; /** * Subtract {@code m} from this matrix. * * @param m Matrix to be subtracted. * @return {@code this} - {@code m}. * @throws MatrixDimensionMismatchException if {@code m} is not the same * size as {@code this} matrix. */ FieldMatrix subtract(FieldMatrix m) throws MatrixDimensionMismatchException; /** * Increment each entry of this matrix. * * @param d Value to be added to each entry. * @return {@code d} + {@code this}. */ FieldMatrix scalarAdd(T d); /** * Multiply each entry by {@code d}. * * @param d Value to multiply all entries by. * @return {@code d} * {@code this}. */ FieldMatrix scalarMultiply(T d); /** * Postmultiply this matrix by {@code m}. * * @param m Matrix to postmultiply by. * @return {@code this} * {@code m}. * @throws DimensionMismatchException if the number of columns of * {@code this} matrix is not equal to the number of rows of matrix * {@code m}. */ FieldMatrix multiply(FieldMatrix m) throws DimensionMismatchException; /** * Premultiply this matrix by {@code m}. * * @param m Matrix to premultiply by. * @return {@code m} * {@code this}. * @throws DimensionMismatchException if the number of columns of {@code m} * differs from the number of rows of {@code this} matrix. */ FieldMatrix preMultiply(FieldMatrix m) throws DimensionMismatchException; /** * Returns the result multiplying this with itself p times. * Depending on the type of the field elements, T, instability for high * powers might occur. * * @param p raise this to power p * @return this^p * @throws NotPositiveException if {@code p < 0} * @throws NonSquareMatrixException if {@code this matrix} is not square */ FieldMatrix power(final int p) throws NonSquareMatrixException, NotPositiveException; /** * Returns matrix entries as a two-dimensional array. * * @return a 2-dimensional array of entries. */ T[][] getData(); /** * Get a submatrix. Rows and columns are indicated * counting from 0 to n - 1. * * @param startRow Initial row index * @param endRow Final row index (inclusive) * @param startColumn Initial column index * @param endColumn Final column index (inclusive) * @return the matrix containing the data of the specified rows and columns. * @throws NumberIsTooSmallException is {@code endRow < startRow} of * {@code endColumn < startColumn}. * @throws OutOfRangeException if the indices are not valid. */ FieldMatrix getSubMatrix(int startRow, int endRow, int startColumn, int endColumn) throws NumberIsTooSmallException, OutOfRangeException; /** * Get a submatrix. Rows and columns are indicated * counting from 0 to n - 1. * * @param selectedRows Array of row indices. * @param selectedColumns Array of column indices. * @return the matrix containing the data in the * specified rows and columns. * @throws NoDataException if {@code selectedRows} or * {@code selectedColumns} is empty * @throws NullArgumentException if {@code selectedRows} or * {@code selectedColumns} is {@code null}. * @throws OutOfRangeException if row or column selections are not valid. */ FieldMatrix getSubMatrix(int[] selectedRows, int[] selectedColumns) throws NoDataException, NullArgumentException, OutOfRangeException; /** * Copy a submatrix. Rows and columns are indicated * counting from 0 to n-1. * * @param startRow Initial row index. * @param endRow Final row index (inclusive). * @param startColumn Initial column index. * @param endColumn Final column index (inclusive). * @param destination The arrays where the submatrix data should be copied * (if larger than rows/columns counts, only the upper-left part will be used). * @throws MatrixDimensionMismatchException if the dimensions of * {@code destination} do not match those of {@code this}. * @throws NumberIsTooSmallException is {@code endRow < startRow} of * {@code endColumn < startColumn}. * @throws OutOfRangeException if the indices are not valid. * @exception IllegalArgumentException if the destination array is too small. */ void copySubMatrix(int startRow, int endRow, int startColumn, int endColumn, T[][] destination) throws MatrixDimensionMismatchException, NumberIsTooSmallException, OutOfRangeException; /** * Copy a submatrix. Rows and columns are indicated * counting from 0 to n - 1. * * @param selectedRows Array of row indices. * @param selectedColumns Array of column indices. * @param destination Arrays where the submatrix data should be copied * (if larger than rows/columns counts, only the upper-left part will be used) * @throws MatrixDimensionMismatchException if the dimensions of * {@code destination} do not match those of {@code this}. * @throws NoDataException if {@code selectedRows} or * {@code selectedColumns} is empty * @throws NullArgumentException if {@code selectedRows} or * {@code selectedColumns} is {@code null}. * @throws OutOfRangeException if the indices are not valid. */ void copySubMatrix(int[] selectedRows, int[] selectedColumns, T[][] destination) throws MatrixDimensionMismatchException, NoDataException, NullArgumentException, OutOfRangeException; /** * Replace the submatrix starting at {@code (row, column)} using data in the * input {@code subMatrix} array. Indexes are 0-based. *

                              * Example:
                              * Starting with * *

                                   * 1  2  3  4
                                   * 5  6  7  8
                                   * 9  0  1  2
                                   * 
                              * * and subMatrix = {{3, 4} {5,6}}, invoking * setSubMatrix(subMatrix,1,1)) will result in * *
                                   * 1  2  3  4
                                   * 5  3  4  8
                                   * 9  5  6  2
                                   * 
                              * *

                              * * @param subMatrix Array containing the submatrix replacement data. * @param row Row coordinate of the top-left element to be replaced. * @param column Column coordinate of the top-left element to be replaced. * @throws OutOfRangeException if {@code subMatrix} does not fit into this * matrix from element in {@code (row, column)}. * @throws NoDataException if a row or column of {@code subMatrix} is empty. * @throws DimensionMismatchException if {@code subMatrix} is not * rectangular (not all rows have the same length). * @throws NullArgumentException if {@code subMatrix} is {@code null}. * @since 2.0 */ void setSubMatrix(T[][] subMatrix, int row, int column) throws DimensionMismatchException, OutOfRangeException, NoDataException, NullArgumentException; /** * Get the entries in row number {@code row} * as a row matrix. * * @param row Row to be fetched. * @return a row matrix. * @throws OutOfRangeException if the specified row index is invalid. */ FieldMatrix getRowMatrix(int row) throws OutOfRangeException; /** * Set the entries in row number {@code row} * as a row matrix. * * @param row Row to be set. * @param matrix Row matrix (must have one row and the same number * of columns as the instance). * @throws OutOfRangeException if the specified row index is invalid. * @throws MatrixDimensionMismatchException * if the matrix dimensions do not match one instance row. */ void setRowMatrix(int row, FieldMatrix matrix) throws MatrixDimensionMismatchException, OutOfRangeException; /** * Get the entries in column number {@code column} * as a column matrix. * * @param column Column to be fetched. * @return a column matrix. * @throws OutOfRangeException if the specified column index is invalid. */ FieldMatrix getColumnMatrix(int column) throws OutOfRangeException; /** * Set the entries in column number {@code column} * as a column matrix. * * @param column Column to be set. * @param matrix column matrix (must have one column and the same * number of rows as the instance). * @throws OutOfRangeException if the specified column index is invalid. * @throws MatrixDimensionMismatchException if the matrix dimensions do * not match one instance column. */ void setColumnMatrix(int column, FieldMatrix matrix) throws MatrixDimensionMismatchException, OutOfRangeException; /** * Get the entries in row number {@code row} * as a vector. * * @param row Row to be fetched * @return a row vector. * @throws OutOfRangeException if the specified row index is invalid. */ FieldVector getRowVector(int row) throws OutOfRangeException; /** * Set the entries in row number {@code row} * as a vector. * * @param row Row to be set. * @param vector row vector (must have the same number of columns * as the instance). * @throws OutOfRangeException if the specified row index is invalid. * @throws MatrixDimensionMismatchException if the vector dimension does not * match one instance row. */ void setRowVector(int row, FieldVector vector) throws MatrixDimensionMismatchException, OutOfRangeException; /** * Returns the entries in column number {@code column} * as a vector. * * @param column Column to be fetched. * @return a column vector. * @throws OutOfRangeException if the specified column index is invalid. */ FieldVector getColumnVector(int column) throws OutOfRangeException; /** * Set the entries in column number {@code column} * as a vector. * * @param column Column to be set. * @param vector Column vector (must have the same number of rows * as the instance). * @throws OutOfRangeException if the specified column index is invalid. * @throws MatrixDimensionMismatchException if the vector dimension does not * match one instance column. */ void setColumnVector(int column, FieldVector vector) throws MatrixDimensionMismatchException, OutOfRangeException; /** * Get the entries in row number {@code row} as an array. * * @param row Row to be fetched. * @return array of entries in the row. * @throws OutOfRangeException if the specified row index is not valid. */ T[] getRow(int row) throws OutOfRangeException; /** * Set the entries in row number {@code row} * as a row matrix. * * @param row Row to be set. * @param array Row matrix (must have the same number of columns as * the instance). * @throws OutOfRangeException if the specified row index is invalid. * @throws MatrixDimensionMismatchException if the array size does not match * one instance row. */ void setRow(int row, T[] array) throws MatrixDimensionMismatchException, OutOfRangeException; /** * Get the entries in column number {@code col} as an array. * * @param column the column to be fetched * @return array of entries in the column * @throws OutOfRangeException if the specified column index is not valid. */ T[] getColumn(int column) throws OutOfRangeException; /** * Set the entries in column number {@code column} * as a column matrix. * * @param column the column to be set * @param array column array (must have the same number of rows as the instance) * @throws OutOfRangeException if the specified column index is invalid. * @throws MatrixDimensionMismatchException if the array size does not match * one instance column. */ void setColumn(int column, T[] array) throws MatrixDimensionMismatchException, OutOfRangeException; /** * Returns the entry in the specified row and column. * * @param row row location of entry to be fetched * @param column column location of entry to be fetched * @return matrix entry in row,column * @throws OutOfRangeException if the row or column index is not valid. */ T getEntry(int row, int column) throws OutOfRangeException; /** * Set the entry in the specified row and column. * * @param row row location of entry to be set * @param column column location of entry to be set * @param value matrix entry to be set in row,column * @throws OutOfRangeException if the row or column index is not valid. * @since 2.0 */ void setEntry(int row, int column, T value) throws OutOfRangeException; /** * Change an entry in the specified row and column. * * @param row Row location of entry to be set. * @param column Column location of entry to be set. * @param increment Value to add to the current matrix entry in * {@code (row, column)}. * @throws OutOfRangeException if the row or column index is not valid. * @since 2.0 */ void addToEntry(int row, int column, T increment) throws OutOfRangeException; /** * Change an entry in the specified row and column. * * @param row Row location of entry to be set. * @param column Column location of entry to be set. * @param factor Multiplication factor for the current matrix entry * in {@code (row,column)} * @throws OutOfRangeException if the row or column index is not valid. * @since 2.0 */ void multiplyEntry(int row, int column, T factor) throws OutOfRangeException; /** * Returns the transpose of this matrix. * * @return transpose matrix */ FieldMatrix transpose(); /** * Returns the * trace of the matrix (the sum of the elements on the main diagonal). * * @return trace * @throws NonSquareMatrixException if the matrix is not square. */ T getTrace() throws NonSquareMatrixException; /** * Returns the result of multiplying this by the vector {@code v}. * * @param v the vector to operate on * @return {@code this * v} * @throws DimensionMismatchException if the number of columns of * {@code this} matrix is not equal to the size of the vector {@code v}. */ T[] operate(T[] v) throws DimensionMismatchException; /** * Returns the result of multiplying this by the vector {@code v}. * * @param v the vector to operate on * @return {@code this * v} * @throws DimensionMismatchException if the number of columns of * {@code this} matrix is not equal to the size of the vector {@code v}. */ FieldVector operate(FieldVector v) throws DimensionMismatchException; /** * Returns the (row) vector result of premultiplying this by the vector * {@code v}. * * @param v the row vector to premultiply by * @return {@code v * this} * @throws DimensionMismatchException if the number of rows of {@code this} * matrix is not equal to the size of the vector {@code v} */ T[] preMultiply(T[] v) throws DimensionMismatchException; /** * Returns the (row) vector result of premultiplying this by the vector * {@code v}. * * @param v the row vector to premultiply by * @return {@code v * this} * @throws DimensionMismatchException if the number of rows of {@code this} * matrix is not equal to the size of the vector {@code v} */ FieldVector preMultiply(FieldVector v) throws DimensionMismatchException; /** * Visit (and possibly change) all matrix entries in row order. *

                              Row order starts at upper left and iterating through all elements * of a row from left to right before going to the leftmost element * of the next row.

                              * @param visitor visitor used to process all matrix entries * @see #walkInRowOrder(FieldMatrixPreservingVisitor) * @see #walkInRowOrder(FieldMatrixChangingVisitor, int, int, int, int) * @see #walkInRowOrder(FieldMatrixPreservingVisitor, int, int, int, int) * @see #walkInColumnOrder(FieldMatrixChangingVisitor) * @see #walkInColumnOrder(FieldMatrixPreservingVisitor) * @see #walkInColumnOrder(FieldMatrixChangingVisitor, int, int, int, int) * @see #walkInColumnOrder(FieldMatrixPreservingVisitor, int, int, int, int) * @see #walkInOptimizedOrder(FieldMatrixChangingVisitor) * @see #walkInOptimizedOrder(FieldMatrixPreservingVisitor) * @see #walkInOptimizedOrder(FieldMatrixChangingVisitor, int, int, int, int) * @see #walkInOptimizedOrder(FieldMatrixPreservingVisitor, int, int, int, int) * @return the value returned by {@link FieldMatrixChangingVisitor#end()} at the end * of the walk */ T walkInRowOrder(FieldMatrixChangingVisitor visitor); /** * Visit (but don't change) all matrix entries in row order. *

                              Row order starts at upper left and iterating through all elements * of a row from left to right before going to the leftmost element * of the next row.

                              * @param visitor visitor used to process all matrix entries * @see #walkInRowOrder(FieldMatrixChangingVisitor) * @see #walkInRowOrder(FieldMatrixChangingVisitor, int, int, int, int) * @see #walkInRowOrder(FieldMatrixPreservingVisitor, int, int, int, int) * @see #walkInColumnOrder(FieldMatrixChangingVisitor) * @see #walkInColumnOrder(FieldMatrixPreservingVisitor) * @see #walkInColumnOrder(FieldMatrixChangingVisitor, int, int, int, int) * @see #walkInColumnOrder(FieldMatrixPreservingVisitor, int, int, int, int) * @see #walkInOptimizedOrder(FieldMatrixChangingVisitor) * @see #walkInOptimizedOrder(FieldMatrixPreservingVisitor) * @see #walkInOptimizedOrder(FieldMatrixChangingVisitor, int, int, int, int) * @see #walkInOptimizedOrder(FieldMatrixPreservingVisitor, int, int, int, int) * @return the value returned by {@link FieldMatrixPreservingVisitor#end()} at the end * of the walk */ T walkInRowOrder(FieldMatrixPreservingVisitor visitor); /** * Visit (and possibly change) some matrix entries in row order. *

                              Row order starts at upper left and iterating through all elements * of a row from left to right before going to the leftmost element * of the next row.

                              * @param visitor visitor used to process all matrix entries * @param startRow Initial row index * @param endRow Final row index (inclusive) * @param startColumn Initial column index * @param endColumn Final column index * @throws OutOfRangeException if the indices are not valid. * @throws NumberIsTooSmallException if {@code endRow < startRow} or * {@code endColumn < startColumn}. * @see #walkInRowOrder(FieldMatrixChangingVisitor) * @see #walkInRowOrder(FieldMatrixPreservingVisitor) * @see #walkInRowOrder(FieldMatrixPreservingVisitor, int, int, int, int) * @see #walkInColumnOrder(FieldMatrixChangingVisitor) * @see #walkInColumnOrder(FieldMatrixPreservingVisitor) * @see #walkInColumnOrder(FieldMatrixChangingVisitor, int, int, int, int) * @see #walkInColumnOrder(FieldMatrixPreservingVisitor, int, int, int, int) * @see #walkInOptimizedOrder(FieldMatrixChangingVisitor) * @see #walkInOptimizedOrder(FieldMatrixPreservingVisitor) * @see #walkInOptimizedOrder(FieldMatrixChangingVisitor, int, int, int, int) * @see #walkInOptimizedOrder(FieldMatrixPreservingVisitor, int, int, int, int) * @return the value returned by {@link FieldMatrixChangingVisitor#end()} at the end * of the walk */ T walkInRowOrder(FieldMatrixChangingVisitor visitor, int startRow, int endRow, int startColumn, int endColumn) throws OutOfRangeException, NumberIsTooSmallException; /** * Visit (but don't change) some matrix entries in row order. *

                              Row order starts at upper left and iterating through all elements * of a row from left to right before going to the leftmost element * of the next row.

                              * @param visitor visitor used to process all matrix entries * @param startRow Initial row index * @param endRow Final row index (inclusive) * @param startColumn Initial column index * @param endColumn Final column index * @throws OutOfRangeException if the indices are not valid. * @throws NumberIsTooSmallException if {@code endRow < startRow} or * {@code endColumn < startColumn}. * @see #walkInRowOrder(FieldMatrixChangingVisitor) * @see #walkInRowOrder(FieldMatrixPreservingVisitor) * @see #walkInRowOrder(FieldMatrixChangingVisitor, int, int, int, int) * @see #walkInColumnOrder(FieldMatrixChangingVisitor) * @see #walkInColumnOrder(FieldMatrixPreservingVisitor) * @see #walkInColumnOrder(FieldMatrixChangingVisitor, int, int, int, int) * @see #walkInColumnOrder(FieldMatrixPreservingVisitor, int, int, int, int) * @see #walkInOptimizedOrder(FieldMatrixChangingVisitor) * @see #walkInOptimizedOrder(FieldMatrixPreservingVisitor) * @see #walkInOptimizedOrder(FieldMatrixChangingVisitor, int, int, int, int) * @see #walkInOptimizedOrder(FieldMatrixPreservingVisitor, int, int, int, int) * @return the value returned by {@link FieldMatrixPreservingVisitor#end()} at the end * of the walk */ T walkInRowOrder(FieldMatrixPreservingVisitor visitor, int startRow, int endRow, int startColumn, int endColumn) throws OutOfRangeException, NumberIsTooSmallException; /** * Visit (and possibly change) all matrix entries in column order. *

                              Column order starts at upper left and iterating through all elements * of a column from top to bottom before going to the topmost element * of the next column.

                              * @param visitor visitor used to process all matrix entries * @see #walkInRowOrder(FieldMatrixChangingVisitor) * @see #walkInRowOrder(FieldMatrixPreservingVisitor) * @see #walkInRowOrder(FieldMatrixChangingVisitor, int, int, int, int) * @see #walkInRowOrder(FieldMatrixPreservingVisitor, int, int, int, int) * @see #walkInColumnOrder(FieldMatrixPreservingVisitor) * @see #walkInColumnOrder(FieldMatrixChangingVisitor, int, int, int, int) * @see #walkInColumnOrder(FieldMatrixPreservingVisitor, int, int, int, int) * @see #walkInOptimizedOrder(FieldMatrixChangingVisitor) * @see #walkInOptimizedOrder(FieldMatrixPreservingVisitor) * @see #walkInOptimizedOrder(FieldMatrixChangingVisitor, int, int, int, int) * @see #walkInOptimizedOrder(FieldMatrixPreservingVisitor, int, int, int, int) * @return the value returned by {@link FieldMatrixChangingVisitor#end()} at the end * of the walk */ T walkInColumnOrder(FieldMatrixChangingVisitor visitor); /** * Visit (but don't change) all matrix entries in column order. *

                              Column order starts at upper left and iterating through all elements * of a column from top to bottom before going to the topmost element * of the next column.

                              * @param visitor visitor used to process all matrix entries * @see #walkInRowOrder(FieldMatrixChangingVisitor) * @see #walkInRowOrder(FieldMatrixPreservingVisitor) * @see #walkInRowOrder(FieldMatrixChangingVisitor, int, int, int, int) * @see #walkInRowOrder(FieldMatrixPreservingVisitor, int, int, int, int) * @see #walkInColumnOrder(FieldMatrixChangingVisitor) * @see #walkInColumnOrder(FieldMatrixChangingVisitor, int, int, int, int) * @see #walkInColumnOrder(FieldMatrixPreservingVisitor, int, int, int, int) * @see #walkInOptimizedOrder(FieldMatrixChangingVisitor) * @see #walkInOptimizedOrder(FieldMatrixPreservingVisitor) * @see #walkInOptimizedOrder(FieldMatrixChangingVisitor, int, int, int, int) * @see #walkInOptimizedOrder(FieldMatrixPreservingVisitor, int, int, int, int) * @return the value returned by {@link FieldMatrixPreservingVisitor#end()} at the end * of the walk */ T walkInColumnOrder(FieldMatrixPreservingVisitor visitor); /** * Visit (and possibly change) some matrix entries in column order. *

                              Column order starts at upper left and iterating through all elements * of a column from top to bottom before going to the topmost element * of the next column.

                              * @param visitor visitor used to process all matrix entries * @param startRow Initial row index * @param endRow Final row index (inclusive) * @param startColumn Initial column index * @param endColumn Final column index * @throws NumberIsTooSmallException if {@code endRow < startRow} or * {@code endColumn < startColumn}. * @throws OutOfRangeException if the indices are not valid. * @see #walkInRowOrder(FieldMatrixChangingVisitor) * @see #walkInRowOrder(FieldMatrixPreservingVisitor) * @see #walkInRowOrder(FieldMatrixChangingVisitor, int, int, int, int) * @see #walkInRowOrder(FieldMatrixPreservingVisitor, int, int, int, int) * @see #walkInColumnOrder(FieldMatrixChangingVisitor) * @see #walkInColumnOrder(FieldMatrixPreservingVisitor) * @see #walkInColumnOrder(FieldMatrixPreservingVisitor, int, int, int, int) * @see #walkInOptimizedOrder(FieldMatrixChangingVisitor) * @see #walkInOptimizedOrder(FieldMatrixPreservingVisitor) * @see #walkInOptimizedOrder(FieldMatrixChangingVisitor, int, int, int, int) * @see #walkInOptimizedOrder(FieldMatrixPreservingVisitor, int, int, int, int) * @return the value returned by {@link FieldMatrixChangingVisitor#end()} at the end * of the walk */ T walkInColumnOrder(FieldMatrixChangingVisitor visitor, int startRow, int endRow, int startColumn, int endColumn) throws NumberIsTooSmallException, OutOfRangeException; /** * Visit (but don't change) some matrix entries in column order. *

                              Column order starts at upper left and iterating through all elements * of a column from top to bottom before going to the topmost element * of the next column.

                              * @param visitor visitor used to process all matrix entries * @param startRow Initial row index * @param endRow Final row index (inclusive) * @param startColumn Initial column index * @param endColumn Final column index * @throws NumberIsTooSmallException if {@code endRow < startRow} or * {@code endColumn < startColumn}. * @throws OutOfRangeException if the indices are not valid. * @see #walkInRowOrder(FieldMatrixChangingVisitor) * @see #walkInRowOrder(FieldMatrixPreservingVisitor) * @see #walkInRowOrder(FieldMatrixChangingVisitor, int, int, int, int) * @see #walkInRowOrder(FieldMatrixPreservingVisitor, int, int, int, int) * @see #walkInColumnOrder(FieldMatrixChangingVisitor) * @see #walkInColumnOrder(FieldMatrixPreservingVisitor) * @see #walkInColumnOrder(FieldMatrixChangingVisitor, int, int, int, int) * @see #walkInOptimizedOrder(FieldMatrixChangingVisitor) * @see #walkInOptimizedOrder(FieldMatrixPreservingVisitor) * @see #walkInOptimizedOrder(FieldMatrixChangingVisitor, int, int, int, int) * @see #walkInOptimizedOrder(FieldMatrixPreservingVisitor, int, int, int, int) * @return the value returned by {@link FieldMatrixPreservingVisitor#end()} at the end * of the walk */ T walkInColumnOrder(FieldMatrixPreservingVisitor visitor, int startRow, int endRow, int startColumn, int endColumn) throws NumberIsTooSmallException, OutOfRangeException; /** * Visit (and possibly change) all matrix entries using the fastest possible order. *

                              The fastest walking order depends on the exact matrix class. It may be * different from traditional row or column orders.

                              * @param visitor visitor used to process all matrix entries * @see #walkInRowOrder(FieldMatrixChangingVisitor) * @see #walkInRowOrder(FieldMatrixPreservingVisitor) * @see #walkInRowOrder(FieldMatrixChangingVisitor, int, int, int, int) * @see #walkInRowOrder(FieldMatrixPreservingVisitor, int, int, int, int) * @see #walkInColumnOrder(FieldMatrixChangingVisitor) * @see #walkInColumnOrder(FieldMatrixPreservingVisitor) * @see #walkInColumnOrder(FieldMatrixChangingVisitor, int, int, int, int) * @see #walkInColumnOrder(FieldMatrixPreservingVisitor, int, int, int, int) * @see #walkInOptimizedOrder(FieldMatrixPreservingVisitor) * @see #walkInOptimizedOrder(FieldMatrixChangingVisitor, int, int, int, int) * @see #walkInOptimizedOrder(FieldMatrixPreservingVisitor, int, int, int, int) * @return the value returned by {@link FieldMatrixChangingVisitor#end()} at the end * of the walk */ T walkInOptimizedOrder(FieldMatrixChangingVisitor visitor); /** * Visit (but don't change) all matrix entries using the fastest possible order. *

                              The fastest walking order depends on the exact matrix class. It may be * different from traditional row or column orders.

                              * @param visitor visitor used to process all matrix entries * @see #walkInRowOrder(FieldMatrixChangingVisitor) * @see #walkInRowOrder(FieldMatrixPreservingVisitor) * @see #walkInRowOrder(FieldMatrixChangingVisitor, int, int, int, int) * @see #walkInRowOrder(FieldMatrixPreservingVisitor, int, int, int, int) * @see #walkInColumnOrder(FieldMatrixChangingVisitor) * @see #walkInColumnOrder(FieldMatrixPreservingVisitor) * @see #walkInColumnOrder(FieldMatrixChangingVisitor, int, int, int, int) * @see #walkInColumnOrder(FieldMatrixPreservingVisitor, int, int, int, int) * @see #walkInOptimizedOrder(FieldMatrixChangingVisitor) * @see #walkInOptimizedOrder(FieldMatrixChangingVisitor, int, int, int, int) * @see #walkInOptimizedOrder(FieldMatrixPreservingVisitor, int, int, int, int) * @return the value returned by {@link FieldMatrixPreservingVisitor#end()} at the end * of the walk */ T walkInOptimizedOrder(FieldMatrixPreservingVisitor visitor); /** * Visit (and possibly change) some matrix entries using the fastest possible order. *

                              The fastest walking order depends on the exact matrix class. It may be * different from traditional row or column orders.

                              * @param visitor visitor used to process all matrix entries * @param startRow Initial row index * @param endRow Final row index (inclusive) * @param startColumn Initial column index * @param endColumn Final column index (inclusive) * @throws NumberIsTooSmallException if {@code endRow < startRow} or * {@code endColumn < startColumn}. * @throws OutOfRangeException if the indices are not valid. * @see #walkInRowOrder(FieldMatrixChangingVisitor) * @see #walkInRowOrder(FieldMatrixPreservingVisitor) * @see #walkInRowOrder(FieldMatrixChangingVisitor, int, int, int, int) * @see #walkInRowOrder(FieldMatrixPreservingVisitor, int, int, int, int) * @see #walkInColumnOrder(FieldMatrixChangingVisitor) * @see #walkInColumnOrder(FieldMatrixPreservingVisitor) * @see #walkInColumnOrder(FieldMatrixChangingVisitor, int, int, int, int) * @see #walkInColumnOrder(FieldMatrixPreservingVisitor, int, int, int, int) * @see #walkInOptimizedOrder(FieldMatrixChangingVisitor) * @see #walkInOptimizedOrder(FieldMatrixPreservingVisitor) * @see #walkInOptimizedOrder(FieldMatrixPreservingVisitor, int, int, int, int) * @return the value returned by {@link FieldMatrixChangingVisitor#end()} at the end * of the walk */ T walkInOptimizedOrder(FieldMatrixChangingVisitor visitor, int startRow, int endRow, int startColumn, int endColumn) throws NumberIsTooSmallException, OutOfRangeException; /** * Visit (but don't change) some matrix entries using the fastest possible order. *

                              The fastest walking order depends on the exact matrix class. It may be * different from traditional row or column orders.

                              * @param visitor visitor used to process all matrix entries * @param startRow Initial row index * @param endRow Final row index (inclusive) * @param startColumn Initial column index * @param endColumn Final column index (inclusive) * @throws NumberIsTooSmallException if {@code endRow < startRow} or * {@code endColumn < startColumn}. * @throws OutOfRangeException if the indices are not valid. * @see #walkInRowOrder(FieldMatrixChangingVisitor) * @see #walkInRowOrder(FieldMatrixPreservingVisitor) * @see #walkInRowOrder(FieldMatrixChangingVisitor, int, int, int, int) * @see #walkInRowOrder(FieldMatrixPreservingVisitor, int, int, int, int) * @see #walkInColumnOrder(FieldMatrixChangingVisitor) * @see #walkInColumnOrder(FieldMatrixPreservingVisitor) * @see #walkInColumnOrder(FieldMatrixChangingVisitor, int, int, int, int) * @see #walkInColumnOrder(FieldMatrixPreservingVisitor, int, int, int, int) * @see #walkInOptimizedOrder(FieldMatrixChangingVisitor) * @see #walkInOptimizedOrder(FieldMatrixPreservingVisitor) * @see #walkInOptimizedOrder(FieldMatrixChangingVisitor, int, int, int, int) * @return the value returned by {@link FieldMatrixPreservingVisitor#end()} at the end * of the walk */ T walkInOptimizedOrder(FieldMatrixPreservingVisitor visitor, int startRow, int endRow, int startColumn, int endColumn) throws NumberIsTooSmallException, OutOfRangeException; } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/linear/DecompositionSolver.java100644 1750 1750 6166 12126627717 30664 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.linear; /** * Interface handling decomposition algorithms that can solve A × X = B. *

                              Decomposition algorithms decompose an A matrix has a product of several specific * matrices from which they can solve A × X = B in least squares sense: they find X * such that ||A × X - B|| is minimal.

                              *

                              Some solvers like {@link LUDecomposition} can only find the solution for * square matrices and when the solution is an exact linear solution, i.e. when * ||A × X - B|| is exactly 0. Other solvers can also find solutions * with non-square matrix A and with non-null minimal norm. If an exact linear * solution exists it is also the minimal norm solution.

                              * * @version $Id: DecompositionSolver.java 1416643 2012-12-03 19:37:14Z tn $ * @since 2.0 */ public interface DecompositionSolver { /** Solve the linear equation A × X = B for matrices A. *

                              The A matrix is implicit, it is provided by the underlying * decomposition algorithm.

                              * @param b right-hand side of the equation A × X = B * @return a vector X that minimizes the two norm of A × X - B * @throws org.apache.commons.math3.exception.DimensionMismatchException * if the matrices dimensions do not match. * @throws SingularMatrixException * if the decomposed matrix is singular. */ RealVector solve(final RealVector b); /** Solve the linear equation A × X = B for matrices A. *

                              The A matrix is implicit, it is provided by the underlying * decomposition algorithm.

                              * @param b right-hand side of the equation A × X = B * @return a matrix X that minimizes the two norm of A × X - B * @throws org.apache.commons.math3.exception.DimensionMismatchException * if the matrices dimensions do not match. * @throws SingularMatrixException * if the decomposed matrix is singular. */ RealMatrix solve(final RealMatrix b); /** * Check if the decomposed matrix is non-singular. * @return true if the decomposed matrix is non-singular. */ boolean isNonSingular(); /** Get the inverse (or pseudo-inverse) of the decomposed matrix. * @return inverse matrix * @throws SingularMatrixException * if the decomposed matrix is singular. */ RealMatrix getInverse(); } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/linear/BlockRealMatrix.java100644 1750 1750 204110 12126627717 27725 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.linear; import java.io.Serializable; import java.util.Arrays; import org.apache.commons.math3.exception.DimensionMismatchException; import org.apache.commons.math3.exception.NoDataException; import org.apache.commons.math3.exception.NotStrictlyPositiveException; import org.apache.commons.math3.exception.NullArgumentException; import org.apache.commons.math3.exception.NumberIsTooSmallException; import org.apache.commons.math3.exception.OutOfRangeException; import org.apache.commons.math3.exception.util.LocalizedFormats; import org.apache.commons.math3.util.FastMath; import org.apache.commons.math3.util.MathUtils; /** * Cache-friendly implementation of RealMatrix using a flat arrays to store * square blocks of the matrix. *

                              * This implementation is specially designed to be cache-friendly. Square blocks are * stored as small arrays and allow efficient traversal of data both in row major direction * and columns major direction, one block at a time. This greatly increases performances * for algorithms that use crossed directions loops like multiplication or transposition. *

                              *

                              * The size of square blocks is a static parameter. It may be tuned according to the cache * size of the target computer processor. As a rule of thumbs, it should be the largest * value that allows three blocks to be simultaneously cached (this is necessary for example * for matrix multiplication). The default value is to use 52x52 blocks which is well suited * for processors with 64k L1 cache (one block holds 2704 values or 21632 bytes). This value * could be lowered to 36x36 for processors with 32k L1 cache. *

                              *

                              * The regular blocks represent {@link #BLOCK_SIZE} x {@link #BLOCK_SIZE} squares. Blocks * at right hand side and bottom side which may be smaller to fit matrix dimensions. The square * blocks are flattened in row major order in single dimension arrays which are therefore * {@link #BLOCK_SIZE}2 elements long for regular blocks. The blocks are themselves * organized in row major order. *

                              *

                              * As an example, for a block size of 52x52, a 100x60 matrix would be stored in 4 blocks. * Block 0 would be a double[2704] array holding the upper left 52x52 square, block 1 would be * a double[416] array holding the upper right 52x8 rectangle, block 2 would be a double[2496] * array holding the lower left 48x52 rectangle and block 3 would be a double[384] array * holding the lower right 48x8 rectangle. *

                              *

                              * The layout complexity overhead versus simple mapping of matrices to java * arrays is negligible for small matrices (about 1%). The gain from cache efficiency leads * to up to 3-fold improvements for matrices of moderate to large size. *

                              * @version $Id: BlockRealMatrix.java 1416643 2012-12-03 19:37:14Z tn $ * @since 2.0 */ public class BlockRealMatrix extends AbstractRealMatrix implements Serializable { /** Block size. */ public static final int BLOCK_SIZE = 52; /** Serializable version identifier */ private static final long serialVersionUID = 4991895511313664478L; /** Blocks of matrix entries. */ private final double blocks[][]; /** Number of rows of the matrix. */ private final int rows; /** Number of columns of the matrix. */ private final int columns; /** Number of block rows of the matrix. */ private final int blockRows; /** Number of block columns of the matrix. */ private final int blockColumns; /** * Create a new matrix with the supplied row and column dimensions. * * @param rows the number of rows in the new matrix * @param columns the number of columns in the new matrix * @throws NotStrictlyPositiveException if row or column dimension is not * positive. */ public BlockRealMatrix(final int rows, final int columns) throws NotStrictlyPositiveException { super(rows, columns); this.rows = rows; this.columns = columns; // number of blocks blockRows = (rows + BLOCK_SIZE - 1) / BLOCK_SIZE; blockColumns = (columns + BLOCK_SIZE - 1) / BLOCK_SIZE; // allocate storage blocks, taking care of smaller ones at right and bottom blocks = createBlocksLayout(rows, columns); } /** * Create a new dense matrix copying entries from raw layout data. *

                              The input array must already be in raw layout.

                              *

                              Calling this constructor is equivalent to call: *

                              matrix = new BlockRealMatrix(rawData.length, rawData[0].length,
                                   *                                   toBlocksLayout(rawData), false);
                              *

                              * * @param rawData data for new matrix, in raw layout * @throws DimensionMismatchException if the shape of {@code blockData} is * inconsistent with block layout. * @throws NotStrictlyPositiveException if row or column dimension is not * positive. * @see #BlockRealMatrix(int, int, double[][], boolean) */ public BlockRealMatrix(final double[][] rawData) throws DimensionMismatchException, NotStrictlyPositiveException { this(rawData.length, rawData[0].length, toBlocksLayout(rawData), false); } /** * Create a new dense matrix copying entries from block layout data. *

                              The input array must already be in blocks layout.

                              * * @param rows Number of rows in the new matrix. * @param columns Number of columns in the new matrix. * @param blockData data for new matrix * @param copyArray Whether the input array will be copied or referenced. * @throws DimensionMismatchException if the shape of {@code blockData} is * inconsistent with block layout. * @throws NotStrictlyPositiveException if row or column dimension is not * positive. * @see #createBlocksLayout(int, int) * @see #toBlocksLayout(double[][]) * @see #BlockRealMatrix(double[][]) */ public BlockRealMatrix(final int rows, final int columns, final double[][] blockData, final boolean copyArray) throws DimensionMismatchException, NotStrictlyPositiveException { super(rows, columns); this.rows = rows; this.columns = columns; // number of blocks blockRows = (rows + BLOCK_SIZE - 1) / BLOCK_SIZE; blockColumns = (columns + BLOCK_SIZE - 1) / BLOCK_SIZE; if (copyArray) { // allocate storage blocks, taking care of smaller ones at right and bottom blocks = new double[blockRows * blockColumns][]; } else { // reference existing array blocks = blockData; } int index = 0; for (int iBlock = 0; iBlock < blockRows; ++iBlock) { final int iHeight = blockHeight(iBlock); for (int jBlock = 0; jBlock < blockColumns; ++jBlock, ++index) { if (blockData[index].length != iHeight * blockWidth(jBlock)) { throw new DimensionMismatchException(blockData[index].length, iHeight * blockWidth(jBlock)); } if (copyArray) { blocks[index] = blockData[index].clone(); } } } } /** * Convert a data array from raw layout to blocks layout. *

                              * Raw layout is the straightforward layout where element at row i and * column j is in array element rawData[i][j]. Blocks layout * is the layout used in {@link BlockRealMatrix} instances, where the matrix * is split in square blocks (except at right and bottom side where blocks may * be rectangular to fit matrix size) and each block is stored in a flattened * one-dimensional array. *

                              *

                              * This method creates an array in blocks layout from an input array in raw layout. * It can be used to provide the array argument of the {@link * #BlockRealMatrix(int, int, double[][], boolean)} constructor. *

                              * @param rawData Data array in raw layout. * @return a new data array containing the same entries but in blocks layout. * @throws DimensionMismatchException if {@code rawData} is not rectangular. * @see #createBlocksLayout(int, int) * @see #BlockRealMatrix(int, int, double[][], boolean) */ public static double[][] toBlocksLayout(final double[][] rawData) throws DimensionMismatchException { final int rows = rawData.length; final int columns = rawData[0].length; final int blockRows = (rows + BLOCK_SIZE - 1) / BLOCK_SIZE; final int blockColumns = (columns + BLOCK_SIZE - 1) / BLOCK_SIZE; // safety checks for (int i = 0; i < rawData.length; ++i) { final int length = rawData[i].length; if (length != columns) { throw new DimensionMismatchException(columns, length); } } // convert array final double[][] blocks = new double[blockRows * blockColumns][]; int blockIndex = 0; for (int iBlock = 0; iBlock < blockRows; ++iBlock) { final int pStart = iBlock * BLOCK_SIZE; final int pEnd = FastMath.min(pStart + BLOCK_SIZE, rows); final int iHeight = pEnd - pStart; for (int jBlock = 0; jBlock < blockColumns; ++jBlock) { final int qStart = jBlock * BLOCK_SIZE; final int qEnd = FastMath.min(qStart + BLOCK_SIZE, columns); final int jWidth = qEnd - qStart; // allocate new block final double[] block = new double[iHeight * jWidth]; blocks[blockIndex] = block; // copy data int index = 0; for (int p = pStart; p < pEnd; ++p) { System.arraycopy(rawData[p], qStart, block, index, jWidth); index += jWidth; } ++blockIndex; } } return blocks; } /** * Create a data array in blocks layout. *

                              * This method can be used to create the array argument of the {@link * #BlockRealMatrix(int, int, double[][], boolean)} constructor. *

                              * @param rows Number of rows in the new matrix. * @param columns Number of columns in the new matrix. * @return a new data array in blocks layout. * @see #toBlocksLayout(double[][]) * @see #BlockRealMatrix(int, int, double[][], boolean) */ public static double[][] createBlocksLayout(final int rows, final int columns) { final int blockRows = (rows + BLOCK_SIZE - 1) / BLOCK_SIZE; final int blockColumns = (columns + BLOCK_SIZE - 1) / BLOCK_SIZE; final double[][] blocks = new double[blockRows * blockColumns][]; int blockIndex = 0; for (int iBlock = 0; iBlock < blockRows; ++iBlock) { final int pStart = iBlock * BLOCK_SIZE; final int pEnd = FastMath.min(pStart + BLOCK_SIZE, rows); final int iHeight = pEnd - pStart; for (int jBlock = 0; jBlock < blockColumns; ++jBlock) { final int qStart = jBlock * BLOCK_SIZE; final int qEnd = FastMath.min(qStart + BLOCK_SIZE, columns); final int jWidth = qEnd - qStart; blocks[blockIndex] = new double[iHeight * jWidth]; ++blockIndex; } } return blocks; } /** {@inheritDoc} */ @Override public BlockRealMatrix createMatrix(final int rowDimension, final int columnDimension) throws NotStrictlyPositiveException { return new BlockRealMatrix(rowDimension, columnDimension); } /** {@inheritDoc} */ @Override public BlockRealMatrix copy() { // create an empty matrix BlockRealMatrix copied = new BlockRealMatrix(rows, columns); // copy the blocks for (int i = 0; i < blocks.length; ++i) { System.arraycopy(blocks[i], 0, copied.blocks[i], 0, blocks[i].length); } return copied; } /** {@inheritDoc} */ @Override public BlockRealMatrix add(final RealMatrix m) throws MatrixDimensionMismatchException { try { return add((BlockRealMatrix) m); } catch (ClassCastException cce) { // safety check MatrixUtils.checkAdditionCompatible(this, m); final BlockRealMatrix out = new BlockRealMatrix(rows, columns); // perform addition block-wise, to ensure good cache behavior int blockIndex = 0; for (int iBlock = 0; iBlock < out.blockRows; ++iBlock) { for (int jBlock = 0; jBlock < out.blockColumns; ++jBlock) { // perform addition on the current block final double[] outBlock = out.blocks[blockIndex]; final double[] tBlock = blocks[blockIndex]; final int pStart = iBlock * BLOCK_SIZE; final int pEnd = FastMath.min(pStart + BLOCK_SIZE, rows); final int qStart = jBlock * BLOCK_SIZE; final int qEnd = FastMath.min(qStart + BLOCK_SIZE, columns); int k = 0; for (int p = pStart; p < pEnd; ++p) { for (int q = qStart; q < qEnd; ++q) { outBlock[k] = tBlock[k] + m.getEntry(p, q); ++k; } } // go to next block ++blockIndex; } } return out; } } /** * Compute the sum of this matrix and {@code m}. * * @param m Matrix to be added. * @return {@code this} + m. * @throws MatrixDimensionMismatchException if {@code m} is not the same * size as this matrix. */ public BlockRealMatrix add(final BlockRealMatrix m) throws MatrixDimensionMismatchException { // safety check MatrixUtils.checkAdditionCompatible(this, m); final BlockRealMatrix out = new BlockRealMatrix(rows, columns); // perform addition block-wise, to ensure good cache behavior for (int blockIndex = 0; blockIndex < out.blocks.length; ++blockIndex) { final double[] outBlock = out.blocks[blockIndex]; final double[] tBlock = blocks[blockIndex]; final double[] mBlock = m.blocks[blockIndex]; for (int k = 0; k < outBlock.length; ++k) { outBlock[k] = tBlock[k] + mBlock[k]; } } return out; } /** {@inheritDoc} */ @Override public BlockRealMatrix subtract(final RealMatrix m) throws MatrixDimensionMismatchException { try { return subtract((BlockRealMatrix) m); } catch (ClassCastException cce) { // safety check MatrixUtils.checkSubtractionCompatible(this, m); final BlockRealMatrix out = new BlockRealMatrix(rows, columns); // perform subtraction block-wise, to ensure good cache behavior int blockIndex = 0; for (int iBlock = 0; iBlock < out.blockRows; ++iBlock) { for (int jBlock = 0; jBlock < out.blockColumns; ++jBlock) { // perform subtraction on the current block final double[] outBlock = out.blocks[blockIndex]; final double[] tBlock = blocks[blockIndex]; final int pStart = iBlock * BLOCK_SIZE; final int pEnd = FastMath.min(pStart + BLOCK_SIZE, rows); final int qStart = jBlock * BLOCK_SIZE; final int qEnd = FastMath.min(qStart + BLOCK_SIZE, columns); int k = 0; for (int p = pStart; p < pEnd; ++p) { for (int q = qStart; q < qEnd; ++q) { outBlock[k] = tBlock[k] - m.getEntry(p, q); ++k; } } // go to next block ++blockIndex; } } return out; } } /** * Subtract {@code m} from this matrix. * * @param m Matrix to be subtracted. * @return {@code this} - m. * @throws MatrixDimensionMismatchException if {@code m} is not the * same size as this matrix. */ public BlockRealMatrix subtract(final BlockRealMatrix m) throws MatrixDimensionMismatchException { // safety check MatrixUtils.checkSubtractionCompatible(this, m); final BlockRealMatrix out = new BlockRealMatrix(rows, columns); // perform subtraction block-wise, to ensure good cache behavior for (int blockIndex = 0; blockIndex < out.blocks.length; ++blockIndex) { final double[] outBlock = out.blocks[blockIndex]; final double[] tBlock = blocks[blockIndex]; final double[] mBlock = m.blocks[blockIndex]; for (int k = 0; k < outBlock.length; ++k) { outBlock[k] = tBlock[k] - mBlock[k]; } } return out; } /** {@inheritDoc} */ @Override public BlockRealMatrix scalarAdd(final double d) { final BlockRealMatrix out = new BlockRealMatrix(rows, columns); // perform subtraction block-wise, to ensure good cache behavior for (int blockIndex = 0; blockIndex < out.blocks.length; ++blockIndex) { final double[] outBlock = out.blocks[blockIndex]; final double[] tBlock = blocks[blockIndex]; for (int k = 0; k < outBlock.length; ++k) { outBlock[k] = tBlock[k] + d; } } return out; } /** {@inheritDoc} */ @Override public RealMatrix scalarMultiply(final double d) { final BlockRealMatrix out = new BlockRealMatrix(rows, columns); // perform subtraction block-wise, to ensure good cache behavior for (int blockIndex = 0; blockIndex < out.blocks.length; ++blockIndex) { final double[] outBlock = out.blocks[blockIndex]; final double[] tBlock = blocks[blockIndex]; for (int k = 0; k < outBlock.length; ++k) { outBlock[k] = tBlock[k] * d; } } return out; } /** {@inheritDoc} */ @Override public BlockRealMatrix multiply(final RealMatrix m) throws DimensionMismatchException { try { return multiply((BlockRealMatrix) m); } catch (ClassCastException cce) { // safety check MatrixUtils.checkMultiplicationCompatible(this, m); final BlockRealMatrix out = new BlockRealMatrix(rows, m.getColumnDimension()); // perform multiplication block-wise, to ensure good cache behavior int blockIndex = 0; for (int iBlock = 0; iBlock < out.blockRows; ++iBlock) { final int pStart = iBlock * BLOCK_SIZE; final int pEnd = FastMath.min(pStart + BLOCK_SIZE, rows); for (int jBlock = 0; jBlock < out.blockColumns; ++jBlock) { final int qStart = jBlock * BLOCK_SIZE; final int qEnd = FastMath.min(qStart + BLOCK_SIZE, m.getColumnDimension()); // select current block final double[] outBlock = out.blocks[blockIndex]; // perform multiplication on current block for (int kBlock = 0; kBlock < blockColumns; ++kBlock) { final int kWidth = blockWidth(kBlock); final double[] tBlock = blocks[iBlock * blockColumns + kBlock]; final int rStart = kBlock * BLOCK_SIZE; int k = 0; for (int p = pStart; p < pEnd; ++p) { final int lStart = (p - pStart) * kWidth; final int lEnd = lStart + kWidth; for (int q = qStart; q < qEnd; ++q) { double sum = 0; int r = rStart; for (int l = lStart; l < lEnd; ++l) { sum += tBlock[l] * m.getEntry(r, q); ++r; } outBlock[k] += sum; ++k; } } } // go to next block ++blockIndex; } } return out; } } /** * Returns the result of postmultiplying this by {@code m}. * * @param m Matrix to postmultiply by. * @return {@code this} * m. * @throws DimensionMismatchException if the matrices are not compatible. */ public BlockRealMatrix multiply(BlockRealMatrix m) throws DimensionMismatchException { // safety check MatrixUtils.checkMultiplicationCompatible(this, m); final BlockRealMatrix out = new BlockRealMatrix(rows, m.columns); // perform multiplication block-wise, to ensure good cache behavior int blockIndex = 0; for (int iBlock = 0; iBlock < out.blockRows; ++iBlock) { final int pStart = iBlock * BLOCK_SIZE; final int pEnd = FastMath.min(pStart + BLOCK_SIZE, rows); for (int jBlock = 0; jBlock < out.blockColumns; ++jBlock) { final int jWidth = out.blockWidth(jBlock); final int jWidth2 = jWidth + jWidth; final int jWidth3 = jWidth2 + jWidth; final int jWidth4 = jWidth3 + jWidth; // select current block final double[] outBlock = out.blocks[blockIndex]; // perform multiplication on current block for (int kBlock = 0; kBlock < blockColumns; ++kBlock) { final int kWidth = blockWidth(kBlock); final double[] tBlock = blocks[iBlock * blockColumns + kBlock]; final double[] mBlock = m.blocks[kBlock * m.blockColumns + jBlock]; int k = 0; for (int p = pStart; p < pEnd; ++p) { final int lStart = (p - pStart) * kWidth; final int lEnd = lStart + kWidth; for (int nStart = 0; nStart < jWidth; ++nStart) { double sum = 0; int l = lStart; int n = nStart; while (l < lEnd - 3) { sum += tBlock[l] * mBlock[n] + tBlock[l + 1] * mBlock[n + jWidth] + tBlock[l + 2] * mBlock[n + jWidth2] + tBlock[l + 3] * mBlock[n + jWidth3]; l += 4; n += jWidth4; } while (l < lEnd) { sum += tBlock[l++] * mBlock[n]; n += jWidth; } outBlock[k] += sum; ++k; } } } // go to next block ++blockIndex; } } return out; } /** {@inheritDoc} */ @Override public double[][] getData() { final double[][] data = new double[getRowDimension()][getColumnDimension()]; final int lastColumns = columns - (blockColumns - 1) * BLOCK_SIZE; for (int iBlock = 0; iBlock < blockRows; ++iBlock) { final int pStart = iBlock * BLOCK_SIZE; final int pEnd = FastMath.min(pStart + BLOCK_SIZE, rows); int regularPos = 0; int lastPos = 0; for (int p = pStart; p < pEnd; ++p) { final double[] dataP = data[p]; int blockIndex = iBlock * blockColumns; int dataPos = 0; for (int jBlock = 0; jBlock < blockColumns - 1; ++jBlock) { System.arraycopy(blocks[blockIndex++], regularPos, dataP, dataPos, BLOCK_SIZE); dataPos += BLOCK_SIZE; } System.arraycopy(blocks[blockIndex], lastPos, dataP, dataPos, lastColumns); regularPos += BLOCK_SIZE; lastPos += lastColumns; } } return data; } /** {@inheritDoc} */ @Override public double getNorm() { final double[] colSums = new double[BLOCK_SIZE]; double maxColSum = 0; for (int jBlock = 0; jBlock < blockColumns; jBlock++) { final int jWidth = blockWidth(jBlock); Arrays.fill(colSums, 0, jWidth, 0.0); for (int iBlock = 0; iBlock < blockRows; ++iBlock) { final int iHeight = blockHeight(iBlock); final double[] block = blocks[iBlock * blockColumns + jBlock]; for (int j = 0; j < jWidth; ++j) { double sum = 0; for (int i = 0; i < iHeight; ++i) { sum += FastMath.abs(block[i * jWidth + j]); } colSums[j] += sum; } } for (int j = 0; j < jWidth; ++j) { maxColSum = FastMath.max(maxColSum, colSums[j]); } } return maxColSum; } /** {@inheritDoc} */ @Override public double getFrobeniusNorm() { double sum2 = 0; for (int blockIndex = 0; blockIndex < blocks.length; ++blockIndex) { for (final double entry : blocks[blockIndex]) { sum2 += entry * entry; } } return FastMath.sqrt(sum2); } /** {@inheritDoc} */ @Override public BlockRealMatrix getSubMatrix(final int startRow, final int endRow, final int startColumn, final int endColumn) throws OutOfRangeException, NumberIsTooSmallException { // safety checks MatrixUtils.checkSubMatrixIndex(this, startRow, endRow, startColumn, endColumn); // create the output matrix final BlockRealMatrix out = new BlockRealMatrix(endRow - startRow + 1, endColumn - startColumn + 1); // compute blocks shifts final int blockStartRow = startRow / BLOCK_SIZE; final int rowsShift = startRow % BLOCK_SIZE; final int blockStartColumn = startColumn / BLOCK_SIZE; final int columnsShift = startColumn % BLOCK_SIZE; // perform extraction block-wise, to ensure good cache behavior int pBlock = blockStartRow; for (int iBlock = 0; iBlock < out.blockRows; ++iBlock) { final int iHeight = out.blockHeight(iBlock); int qBlock = blockStartColumn; for (int jBlock = 0; jBlock < out.blockColumns; ++jBlock) { final int jWidth = out.blockWidth(jBlock); // handle one block of the output matrix final int outIndex = iBlock * out.blockColumns + jBlock; final double[] outBlock = out.blocks[outIndex]; final int index = pBlock * blockColumns + qBlock; final int width = blockWidth(qBlock); final int heightExcess = iHeight + rowsShift - BLOCK_SIZE; final int widthExcess = jWidth + columnsShift - BLOCK_SIZE; if (heightExcess > 0) { // the submatrix block spans on two blocks rows from the original matrix if (widthExcess > 0) { // the submatrix block spans on two blocks columns from the original matrix final int width2 = blockWidth(qBlock + 1); copyBlockPart(blocks[index], width, rowsShift, BLOCK_SIZE, columnsShift, BLOCK_SIZE, outBlock, jWidth, 0, 0); copyBlockPart(blocks[index + 1], width2, rowsShift, BLOCK_SIZE, 0, widthExcess, outBlock, jWidth, 0, jWidth - widthExcess); copyBlockPart(blocks[index + blockColumns], width, 0, heightExcess, columnsShift, BLOCK_SIZE, outBlock, jWidth, iHeight - heightExcess, 0); copyBlockPart(blocks[index + blockColumns + 1], width2, 0, heightExcess, 0, widthExcess, outBlock, jWidth, iHeight - heightExcess, jWidth - widthExcess); } else { // the submatrix block spans on one block column from the original matrix copyBlockPart(blocks[index], width, rowsShift, BLOCK_SIZE, columnsShift, jWidth + columnsShift, outBlock, jWidth, 0, 0); copyBlockPart(blocks[index + blockColumns], width, 0, heightExcess, columnsShift, jWidth + columnsShift, outBlock, jWidth, iHeight - heightExcess, 0); } } else { // the submatrix block spans on one block row from the original matrix if (widthExcess > 0) { // the submatrix block spans on two blocks columns from the original matrix final int width2 = blockWidth(qBlock + 1); copyBlockPart(blocks[index], width, rowsShift, iHeight + rowsShift, columnsShift, BLOCK_SIZE, outBlock, jWidth, 0, 0); copyBlockPart(blocks[index + 1], width2, rowsShift, iHeight + rowsShift, 0, widthExcess, outBlock, jWidth, 0, jWidth - widthExcess); } else { // the submatrix block spans on one block column from the original matrix copyBlockPart(blocks[index], width, rowsShift, iHeight + rowsShift, columnsShift, jWidth + columnsShift, outBlock, jWidth, 0, 0); } } ++qBlock; } ++pBlock; } return out; } /** * Copy a part of a block into another one *

                              This method can be called only when the specified part fits in both * blocks, no verification is done here.

                              * @param srcBlock source block * @param srcWidth source block width ({@link #BLOCK_SIZE} or smaller) * @param srcStartRow start row in the source block * @param srcEndRow end row (exclusive) in the source block * @param srcStartColumn start column in the source block * @param srcEndColumn end column (exclusive) in the source block * @param dstBlock destination block * @param dstWidth destination block width ({@link #BLOCK_SIZE} or smaller) * @param dstStartRow start row in the destination block * @param dstStartColumn start column in the destination block */ private void copyBlockPart(final double[] srcBlock, final int srcWidth, final int srcStartRow, final int srcEndRow, final int srcStartColumn, final int srcEndColumn, final double[] dstBlock, final int dstWidth, final int dstStartRow, final int dstStartColumn) { final int length = srcEndColumn - srcStartColumn; int srcPos = srcStartRow * srcWidth + srcStartColumn; int dstPos = dstStartRow * dstWidth + dstStartColumn; for (int srcRow = srcStartRow; srcRow < srcEndRow; ++srcRow) { System.arraycopy(srcBlock, srcPos, dstBlock, dstPos, length); srcPos += srcWidth; dstPos += dstWidth; } } /** {@inheritDoc} */ @Override public void setSubMatrix(final double[][] subMatrix, final int row, final int column) throws OutOfRangeException, NoDataException, NullArgumentException, DimensionMismatchException { // safety checks MathUtils.checkNotNull(subMatrix); final int refLength = subMatrix[0].length; if (refLength == 0) { throw new NoDataException(LocalizedFormats.AT_LEAST_ONE_COLUMN); } final int endRow = row + subMatrix.length - 1; final int endColumn = column + refLength - 1; MatrixUtils.checkSubMatrixIndex(this, row, endRow, column, endColumn); for (final double[] subRow : subMatrix) { if (subRow.length != refLength) { throw new DimensionMismatchException(refLength, subRow.length); } } // compute blocks bounds final int blockStartRow = row / BLOCK_SIZE; final int blockEndRow = (endRow + BLOCK_SIZE) / BLOCK_SIZE; final int blockStartColumn = column / BLOCK_SIZE; final int blockEndColumn = (endColumn + BLOCK_SIZE) / BLOCK_SIZE; // perform copy block-wise, to ensure good cache behavior for (int iBlock = blockStartRow; iBlock < blockEndRow; ++iBlock) { final int iHeight = blockHeight(iBlock); final int firstRow = iBlock * BLOCK_SIZE; final int iStart = FastMath.max(row, firstRow); final int iEnd = FastMath.min(endRow + 1, firstRow + iHeight); for (int jBlock = blockStartColumn; jBlock < blockEndColumn; ++jBlock) { final int jWidth = blockWidth(jBlock); final int firstColumn = jBlock * BLOCK_SIZE; final int jStart = FastMath.max(column, firstColumn); final int jEnd = FastMath.min(endColumn + 1, firstColumn + jWidth); final int jLength = jEnd - jStart; // handle one block, row by row final double[] block = blocks[iBlock * blockColumns + jBlock]; for (int i = iStart; i < iEnd; ++i) { System.arraycopy(subMatrix[i - row], jStart - column, block, (i - firstRow) * jWidth + (jStart - firstColumn), jLength); } } } } /** {@inheritDoc} */ @Override public BlockRealMatrix getRowMatrix(final int row) throws OutOfRangeException { MatrixUtils.checkRowIndex(this, row); final BlockRealMatrix out = new BlockRealMatrix(1, columns); // perform copy block-wise, to ensure good cache behavior final int iBlock = row / BLOCK_SIZE; final int iRow = row - iBlock * BLOCK_SIZE; int outBlockIndex = 0; int outIndex = 0; double[] outBlock = out.blocks[outBlockIndex]; for (int jBlock = 0; jBlock < blockColumns; ++jBlock) { final int jWidth = blockWidth(jBlock); final double[] block = blocks[iBlock * blockColumns + jBlock]; final int available = outBlock.length - outIndex; if (jWidth > available) { System.arraycopy(block, iRow * jWidth, outBlock, outIndex, available); outBlock = out.blocks[++outBlockIndex]; System.arraycopy(block, iRow * jWidth, outBlock, 0, jWidth - available); outIndex = jWidth - available; } else { System.arraycopy(block, iRow * jWidth, outBlock, outIndex, jWidth); outIndex += jWidth; } } return out; } /** {@inheritDoc} */ @Override public void setRowMatrix(final int row, final RealMatrix matrix) throws OutOfRangeException, MatrixDimensionMismatchException { try { setRowMatrix(row, (BlockRealMatrix) matrix); } catch (ClassCastException cce) { super.setRowMatrix(row, matrix); } } /** * Sets the entries in row number row * as a row matrix. Row indices start at 0. * * @param row the row to be set * @param matrix row matrix (must have one row and the same number of columns * as the instance) * @throws OutOfRangeException if the specified row index is invalid. * @throws MatrixDimensionMismatchException if the matrix dimensions do * not match one instance row. */ public void setRowMatrix(final int row, final BlockRealMatrix matrix) throws OutOfRangeException, MatrixDimensionMismatchException { MatrixUtils.checkRowIndex(this, row); final int nCols = getColumnDimension(); if ((matrix.getRowDimension() != 1) || (matrix.getColumnDimension() != nCols)) { throw new MatrixDimensionMismatchException(matrix.getRowDimension(), matrix.getColumnDimension(), 1, nCols); } // perform copy block-wise, to ensure good cache behavior final int iBlock = row / BLOCK_SIZE; final int iRow = row - iBlock * BLOCK_SIZE; int mBlockIndex = 0; int mIndex = 0; double[] mBlock = matrix.blocks[mBlockIndex]; for (int jBlock = 0; jBlock < blockColumns; ++jBlock) { final int jWidth = blockWidth(jBlock); final double[] block = blocks[iBlock * blockColumns + jBlock]; final int available = mBlock.length - mIndex; if (jWidth > available) { System.arraycopy(mBlock, mIndex, block, iRow * jWidth, available); mBlock = matrix.blocks[++mBlockIndex]; System.arraycopy(mBlock, 0, block, iRow * jWidth, jWidth - available); mIndex = jWidth - available; } else { System.arraycopy(mBlock, mIndex, block, iRow * jWidth, jWidth); mIndex += jWidth; } } } /** {@inheritDoc} */ @Override public BlockRealMatrix getColumnMatrix(final int column) throws OutOfRangeException { MatrixUtils.checkColumnIndex(this, column); final BlockRealMatrix out = new BlockRealMatrix(rows, 1); // perform copy block-wise, to ensure good cache behavior final int jBlock = column / BLOCK_SIZE; final int jColumn = column - jBlock * BLOCK_SIZE; final int jWidth = blockWidth(jBlock); int outBlockIndex = 0; int outIndex = 0; double[] outBlock = out.blocks[outBlockIndex]; for (int iBlock = 0; iBlock < blockRows; ++iBlock) { final int iHeight = blockHeight(iBlock); final double[] block = blocks[iBlock * blockColumns + jBlock]; for (int i = 0; i < iHeight; ++i) { if (outIndex >= outBlock.length) { outBlock = out.blocks[++outBlockIndex]; outIndex = 0; } outBlock[outIndex++] = block[i * jWidth + jColumn]; } } return out; } /** {@inheritDoc} */ @Override public void setColumnMatrix(final int column, final RealMatrix matrix) throws OutOfRangeException, MatrixDimensionMismatchException { try { setColumnMatrix(column, (BlockRealMatrix) matrix); } catch (ClassCastException cce) { super.setColumnMatrix(column, matrix); } } /** * Sets the entries in column number column * as a column matrix. Column indices start at 0. * * @param column the column to be set * @param matrix column matrix (must have one column and the same number of rows * as the instance) * @throws OutOfRangeException if the specified column index is invalid. * @throws MatrixDimensionMismatchException if the matrix dimensions do * not match one instance column. */ void setColumnMatrix(final int column, final BlockRealMatrix matrix) throws OutOfRangeException, MatrixDimensionMismatchException { MatrixUtils.checkColumnIndex(this, column); final int nRows = getRowDimension(); if ((matrix.getRowDimension() != nRows) || (matrix.getColumnDimension() != 1)) { throw new MatrixDimensionMismatchException(matrix.getRowDimension(), matrix.getColumnDimension(), nRows, 1); } // perform copy block-wise, to ensure good cache behavior final int jBlock = column / BLOCK_SIZE; final int jColumn = column - jBlock * BLOCK_SIZE; final int jWidth = blockWidth(jBlock); int mBlockIndex = 0; int mIndex = 0; double[] mBlock = matrix.blocks[mBlockIndex]; for (int iBlock = 0; iBlock < blockRows; ++iBlock) { final int iHeight = blockHeight(iBlock); final double[] block = blocks[iBlock * blockColumns + jBlock]; for (int i = 0; i < iHeight; ++i) { if (mIndex >= mBlock.length) { mBlock = matrix.blocks[++mBlockIndex]; mIndex = 0; } block[i * jWidth + jColumn] = mBlock[mIndex++]; } } } /** {@inheritDoc} */ @Override public RealVector getRowVector(final int row) throws OutOfRangeException { MatrixUtils.checkRowIndex(this, row); final double[] outData = new double[columns]; // perform copy block-wise, to ensure good cache behavior final int iBlock = row / BLOCK_SIZE; final int iRow = row - iBlock * BLOCK_SIZE; int outIndex = 0; for (int jBlock = 0; jBlock < blockColumns; ++jBlock) { final int jWidth = blockWidth(jBlock); final double[] block = blocks[iBlock * blockColumns + jBlock]; System.arraycopy(block, iRow * jWidth, outData, outIndex, jWidth); outIndex += jWidth; } return new ArrayRealVector(outData, false); } /** {@inheritDoc} */ @Override public void setRowVector(final int row, final RealVector vector) throws OutOfRangeException, MatrixDimensionMismatchException { try { setRow(row, ((ArrayRealVector) vector).getDataRef()); } catch (ClassCastException cce) { super.setRowVector(row, vector); } } /** {@inheritDoc} */ @Override public RealVector getColumnVector(final int column) throws OutOfRangeException { MatrixUtils.checkColumnIndex(this, column); final double[] outData = new double[rows]; // perform copy block-wise, to ensure good cache behavior final int jBlock = column / BLOCK_SIZE; final int jColumn = column - jBlock * BLOCK_SIZE; final int jWidth = blockWidth(jBlock); int outIndex = 0; for (int iBlock = 0; iBlock < blockRows; ++iBlock) { final int iHeight = blockHeight(iBlock); final double[] block = blocks[iBlock * blockColumns + jBlock]; for (int i = 0; i < iHeight; ++i) { outData[outIndex++] = block[i * jWidth + jColumn]; } } return new ArrayRealVector(outData, false); } /** {@inheritDoc} */ @Override public void setColumnVector(final int column, final RealVector vector) throws OutOfRangeException, MatrixDimensionMismatchException { try { setColumn(column, ((ArrayRealVector) vector).getDataRef()); } catch (ClassCastException cce) { super.setColumnVector(column, vector); } } /** {@inheritDoc} */ @Override public double[] getRow(final int row) throws OutOfRangeException { MatrixUtils.checkRowIndex(this, row); final double[] out = new double[columns]; // perform copy block-wise, to ensure good cache behavior final int iBlock = row / BLOCK_SIZE; final int iRow = row - iBlock * BLOCK_SIZE; int outIndex = 0; for (int jBlock = 0; jBlock < blockColumns; ++jBlock) { final int jWidth = blockWidth(jBlock); final double[] block = blocks[iBlock * blockColumns + jBlock]; System.arraycopy(block, iRow * jWidth, out, outIndex, jWidth); outIndex += jWidth; } return out; } /** {@inheritDoc} */ @Override public void setRow(final int row, final double[] array) throws OutOfRangeException, MatrixDimensionMismatchException { MatrixUtils.checkRowIndex(this, row); final int nCols = getColumnDimension(); if (array.length != nCols) { throw new MatrixDimensionMismatchException(1, array.length, 1, nCols); } // perform copy block-wise, to ensure good cache behavior final int iBlock = row / BLOCK_SIZE; final int iRow = row - iBlock * BLOCK_SIZE; int outIndex = 0; for (int jBlock = 0; jBlock < blockColumns; ++jBlock) { final int jWidth = blockWidth(jBlock); final double[] block = blocks[iBlock * blockColumns + jBlock]; System.arraycopy(array, outIndex, block, iRow * jWidth, jWidth); outIndex += jWidth; } } /** {@inheritDoc} */ @Override public double[] getColumn(final int column) throws OutOfRangeException { MatrixUtils.checkColumnIndex(this, column); final double[] out = new double[rows]; // perform copy block-wise, to ensure good cache behavior final int jBlock = column / BLOCK_SIZE; final int jColumn = column - jBlock * BLOCK_SIZE; final int jWidth = blockWidth(jBlock); int outIndex = 0; for (int iBlock = 0; iBlock < blockRows; ++iBlock) { final int iHeight = blockHeight(iBlock); final double[] block = blocks[iBlock * blockColumns + jBlock]; for (int i = 0; i < iHeight; ++i) { out[outIndex++] = block[i * jWidth + jColumn]; } } return out; } /** {@inheritDoc} */ @Override public void setColumn(final int column, final double[] array) throws OutOfRangeException, MatrixDimensionMismatchException { MatrixUtils.checkColumnIndex(this, column); final int nRows = getRowDimension(); if (array.length != nRows) { throw new MatrixDimensionMismatchException(array.length, 1, nRows, 1); } // perform copy block-wise, to ensure good cache behavior final int jBlock = column / BLOCK_SIZE; final int jColumn = column - jBlock * BLOCK_SIZE; final int jWidth = blockWidth(jBlock); int outIndex = 0; for (int iBlock = 0; iBlock < blockRows; ++iBlock) { final int iHeight = blockHeight(iBlock); final double[] block = blocks[iBlock * blockColumns + jBlock]; for (int i = 0; i < iHeight; ++i) { block[i * jWidth + jColumn] = array[outIndex++]; } } } /** {@inheritDoc} */ @Override public double getEntry(final int row, final int column) throws OutOfRangeException { MatrixUtils.checkMatrixIndex(this, row, column); final int iBlock = row / BLOCK_SIZE; final int jBlock = column / BLOCK_SIZE; final int k = (row - iBlock * BLOCK_SIZE) * blockWidth(jBlock) + (column - jBlock * BLOCK_SIZE); return blocks[iBlock * blockColumns + jBlock][k]; } /** {@inheritDoc} */ @Override public void setEntry(final int row, final int column, final double value) throws OutOfRangeException { MatrixUtils.checkMatrixIndex(this, row, column); final int iBlock = row / BLOCK_SIZE; final int jBlock = column / BLOCK_SIZE; final int k = (row - iBlock * BLOCK_SIZE) * blockWidth(jBlock) + (column - jBlock * BLOCK_SIZE); blocks[iBlock * blockColumns + jBlock][k] = value; } /** {@inheritDoc} */ @Override public void addToEntry(final int row, final int column, final double increment) throws OutOfRangeException { MatrixUtils.checkMatrixIndex(this, row, column); final int iBlock = row / BLOCK_SIZE; final int jBlock = column / BLOCK_SIZE; final int k = (row - iBlock * BLOCK_SIZE) * blockWidth(jBlock) + (column - jBlock * BLOCK_SIZE); blocks[iBlock * blockColumns + jBlock][k] += increment; } /** {@inheritDoc} */ @Override public void multiplyEntry(final int row, final int column, final double factor) throws OutOfRangeException { MatrixUtils.checkMatrixIndex(this, row, column); final int iBlock = row / BLOCK_SIZE; final int jBlock = column / BLOCK_SIZE; final int k = (row - iBlock * BLOCK_SIZE) * blockWidth(jBlock) + (column - jBlock * BLOCK_SIZE); blocks[iBlock * blockColumns + jBlock][k] *= factor; } /** {@inheritDoc} */ @Override public BlockRealMatrix transpose() { final int nRows = getRowDimension(); final int nCols = getColumnDimension(); final BlockRealMatrix out = new BlockRealMatrix(nCols, nRows); // perform transpose block-wise, to ensure good cache behavior int blockIndex = 0; for (int iBlock = 0; iBlock < blockColumns; ++iBlock) { for (int jBlock = 0; jBlock < blockRows; ++jBlock) { // transpose current block final double[] outBlock = out.blocks[blockIndex]; final double[] tBlock = blocks[jBlock * blockColumns + iBlock]; final int pStart = iBlock * BLOCK_SIZE; final int pEnd = FastMath.min(pStart + BLOCK_SIZE, columns); final int qStart = jBlock * BLOCK_SIZE; final int qEnd = FastMath.min(qStart + BLOCK_SIZE, rows); int k = 0; for (int p = pStart; p < pEnd; ++p) { final int lInc = pEnd - pStart; int l = p - pStart; for (int q = qStart; q < qEnd; ++q) { outBlock[k] = tBlock[l]; ++k; l+= lInc; } } // go to next block ++blockIndex; } } return out; } /** {@inheritDoc} */ @Override public int getRowDimension() { return rows; } /** {@inheritDoc} */ @Override public int getColumnDimension() { return columns; } /** {@inheritDoc} */ @Override public double[] operate(final double[] v) throws DimensionMismatchException { if (v.length != columns) { throw new DimensionMismatchException(v.length, columns); } final double[] out = new double[rows]; // perform multiplication block-wise, to ensure good cache behavior for (int iBlock = 0; iBlock < blockRows; ++iBlock) { final int pStart = iBlock * BLOCK_SIZE; final int pEnd = FastMath.min(pStart + BLOCK_SIZE, rows); for (int jBlock = 0; jBlock < blockColumns; ++jBlock) { final double[] block = blocks[iBlock * blockColumns + jBlock]; final int qStart = jBlock * BLOCK_SIZE; final int qEnd = FastMath.min(qStart + BLOCK_SIZE, columns); int k = 0; for (int p = pStart; p < pEnd; ++p) { double sum = 0; int q = qStart; while (q < qEnd - 3) { sum += block[k] * v[q] + block[k + 1] * v[q + 1] + block[k + 2] * v[q + 2] + block[k + 3] * v[q + 3]; k += 4; q += 4; } while (q < qEnd) { sum += block[k++] * v[q++]; } out[p] += sum; } } } return out; } /** {@inheritDoc} */ @Override public double[] preMultiply(final double[] v) throws DimensionMismatchException { if (v.length != rows) { throw new DimensionMismatchException(v.length, rows); } final double[] out = new double[columns]; // perform multiplication block-wise, to ensure good cache behavior for (int jBlock = 0; jBlock < blockColumns; ++jBlock) { final int jWidth = blockWidth(jBlock); final int jWidth2 = jWidth + jWidth; final int jWidth3 = jWidth2 + jWidth; final int jWidth4 = jWidth3 + jWidth; final int qStart = jBlock * BLOCK_SIZE; final int qEnd = FastMath.min(qStart + BLOCK_SIZE, columns); for (int iBlock = 0; iBlock < blockRows; ++iBlock) { final double[] block = blocks[iBlock * blockColumns + jBlock]; final int pStart = iBlock * BLOCK_SIZE; final int pEnd = FastMath.min(pStart + BLOCK_SIZE, rows); for (int q = qStart; q < qEnd; ++q) { int k = q - qStart; double sum = 0; int p = pStart; while (p < pEnd - 3) { sum += block[k] * v[p] + block[k + jWidth] * v[p + 1] + block[k + jWidth2] * v[p + 2] + block[k + jWidth3] * v[p + 3]; k += jWidth4; p += 4; } while (p < pEnd) { sum += block[k] * v[p++]; k += jWidth; } out[q] += sum; } } } return out; } /** {@inheritDoc} */ @Override public double walkInRowOrder(final RealMatrixChangingVisitor visitor) { visitor.start(rows, columns, 0, rows - 1, 0, columns - 1); for (int iBlock = 0; iBlock < blockRows; ++iBlock) { final int pStart = iBlock * BLOCK_SIZE; final int pEnd = FastMath.min(pStart + BLOCK_SIZE, rows); for (int p = pStart; p < pEnd; ++p) { for (int jBlock = 0; jBlock < blockColumns; ++jBlock) { final int jWidth = blockWidth(jBlock); final int qStart = jBlock * BLOCK_SIZE; final int qEnd = FastMath.min(qStart + BLOCK_SIZE, columns); final double[] block = blocks[iBlock * blockColumns + jBlock]; int k = (p - pStart) * jWidth; for (int q = qStart; q < qEnd; ++q) { block[k] = visitor.visit(p, q, block[k]); ++k; } } } } return visitor.end(); } /** {@inheritDoc} */ @Override public double walkInRowOrder(final RealMatrixPreservingVisitor visitor) { visitor.start(rows, columns, 0, rows - 1, 0, columns - 1); for (int iBlock = 0; iBlock < blockRows; ++iBlock) { final int pStart = iBlock * BLOCK_SIZE; final int pEnd = FastMath.min(pStart + BLOCK_SIZE, rows); for (int p = pStart; p < pEnd; ++p) { for (int jBlock = 0; jBlock < blockColumns; ++jBlock) { final int jWidth = blockWidth(jBlock); final int qStart = jBlock * BLOCK_SIZE; final int qEnd = FastMath.min(qStart + BLOCK_SIZE, columns); final double[] block = blocks[iBlock * blockColumns + jBlock]; int k = (p - pStart) * jWidth; for (int q = qStart; q < qEnd; ++q) { visitor.visit(p, q, block[k]); ++k; } } } } return visitor.end(); } /** {@inheritDoc} */ @Override public double walkInRowOrder(final RealMatrixChangingVisitor visitor, final int startRow, final int endRow, final int startColumn, final int endColumn) throws OutOfRangeException, NumberIsTooSmallException { MatrixUtils.checkSubMatrixIndex(this, startRow, endRow, startColumn, endColumn); visitor.start(rows, columns, startRow, endRow, startColumn, endColumn); for (int iBlock = startRow / BLOCK_SIZE; iBlock < 1 + endRow / BLOCK_SIZE; ++iBlock) { final int p0 = iBlock * BLOCK_SIZE; final int pStart = FastMath.max(startRow, p0); final int pEnd = FastMath.min((iBlock + 1) * BLOCK_SIZE, 1 + endRow); for (int p = pStart; p < pEnd; ++p) { for (int jBlock = startColumn / BLOCK_SIZE; jBlock < 1 + endColumn / BLOCK_SIZE; ++jBlock) { final int jWidth = blockWidth(jBlock); final int q0 = jBlock * BLOCK_SIZE; final int qStart = FastMath.max(startColumn, q0); final int qEnd = FastMath.min((jBlock + 1) * BLOCK_SIZE, 1 + endColumn); final double[] block = blocks[iBlock * blockColumns + jBlock]; int k = (p - p0) * jWidth + qStart - q0; for (int q = qStart; q < qEnd; ++q) { block[k] = visitor.visit(p, q, block[k]); ++k; } } } } return visitor.end(); } /** {@inheritDoc} */ @Override public double walkInRowOrder(final RealMatrixPreservingVisitor visitor, final int startRow, final int endRow, final int startColumn, final int endColumn) throws OutOfRangeException, NumberIsTooSmallException { MatrixUtils.checkSubMatrixIndex(this, startRow, endRow, startColumn, endColumn); visitor.start(rows, columns, startRow, endRow, startColumn, endColumn); for (int iBlock = startRow / BLOCK_SIZE; iBlock < 1 + endRow / BLOCK_SIZE; ++iBlock) { final int p0 = iBlock * BLOCK_SIZE; final int pStart = FastMath.max(startRow, p0); final int pEnd = FastMath.min((iBlock + 1) * BLOCK_SIZE, 1 + endRow); for (int p = pStart; p < pEnd; ++p) { for (int jBlock = startColumn / BLOCK_SIZE; jBlock < 1 + endColumn / BLOCK_SIZE; ++jBlock) { final int jWidth = blockWidth(jBlock); final int q0 = jBlock * BLOCK_SIZE; final int qStart = FastMath.max(startColumn, q0); final int qEnd = FastMath.min((jBlock + 1) * BLOCK_SIZE, 1 + endColumn); final double[] block = blocks[iBlock * blockColumns + jBlock]; int k = (p - p0) * jWidth + qStart - q0; for (int q = qStart; q < qEnd; ++q) { visitor.visit(p, q, block[k]); ++k; } } } } return visitor.end(); } /** {@inheritDoc} */ @Override public double walkInOptimizedOrder(final RealMatrixChangingVisitor visitor) { visitor.start(rows, columns, 0, rows - 1, 0, columns - 1); int blockIndex = 0; for (int iBlock = 0; iBlock < blockRows; ++iBlock) { final int pStart = iBlock * BLOCK_SIZE; final int pEnd = FastMath.min(pStart + BLOCK_SIZE, rows); for (int jBlock = 0; jBlock < blockColumns; ++jBlock) { final int qStart = jBlock * BLOCK_SIZE; final int qEnd = FastMath.min(qStart + BLOCK_SIZE, columns); final double[] block = blocks[blockIndex]; int k = 0; for (int p = pStart; p < pEnd; ++p) { for (int q = qStart; q < qEnd; ++q) { block[k] = visitor.visit(p, q, block[k]); ++k; } } ++blockIndex; } } return visitor.end(); } /** {@inheritDoc} */ @Override public double walkInOptimizedOrder(final RealMatrixPreservingVisitor visitor) { visitor.start(rows, columns, 0, rows - 1, 0, columns - 1); int blockIndex = 0; for (int iBlock = 0; iBlock < blockRows; ++iBlock) { final int pStart = iBlock * BLOCK_SIZE; final int pEnd = FastMath.min(pStart + BLOCK_SIZE, rows); for (int jBlock = 0; jBlock < blockColumns; ++jBlock) { final int qStart = jBlock * BLOCK_SIZE; final int qEnd = FastMath.min(qStart + BLOCK_SIZE, columns); final double[] block = blocks[blockIndex]; int k = 0; for (int p = pStart; p < pEnd; ++p) { for (int q = qStart; q < qEnd; ++q) { visitor.visit(p, q, block[k]); ++k; } } ++blockIndex; } } return visitor.end(); } /** {@inheritDoc} */ @Override public double walkInOptimizedOrder(final RealMatrixChangingVisitor visitor, final int startRow, final int endRow, final int startColumn, final int endColumn) throws OutOfRangeException, NumberIsTooSmallException { MatrixUtils.checkSubMatrixIndex(this, startRow, endRow, startColumn, endColumn); visitor.start(rows, columns, startRow, endRow, startColumn, endColumn); for (int iBlock = startRow / BLOCK_SIZE; iBlock < 1 + endRow / BLOCK_SIZE; ++iBlock) { final int p0 = iBlock * BLOCK_SIZE; final int pStart = FastMath.max(startRow, p0); final int pEnd = FastMath.min((iBlock + 1) * BLOCK_SIZE, 1 + endRow); for (int jBlock = startColumn / BLOCK_SIZE; jBlock < 1 + endColumn / BLOCK_SIZE; ++jBlock) { final int jWidth = blockWidth(jBlock); final int q0 = jBlock * BLOCK_SIZE; final int qStart = FastMath.max(startColumn, q0); final int qEnd = FastMath.min((jBlock + 1) * BLOCK_SIZE, 1 + endColumn); final double[] block = blocks[iBlock * blockColumns + jBlock]; for (int p = pStart; p < pEnd; ++p) { int k = (p - p0) * jWidth + qStart - q0; for (int q = qStart; q < qEnd; ++q) { block[k] = visitor.visit(p, q, block[k]); ++k; } } } } return visitor.end(); } /** {@inheritDoc} */ @Override public double walkInOptimizedOrder(final RealMatrixPreservingVisitor visitor, final int startRow, final int endRow, final int startColumn, final int endColumn) throws OutOfRangeException, NumberIsTooSmallException { MatrixUtils.checkSubMatrixIndex(this, startRow, endRow, startColumn, endColumn); visitor.start(rows, columns, startRow, endRow, startColumn, endColumn); for (int iBlock = startRow / BLOCK_SIZE; iBlock < 1 + endRow / BLOCK_SIZE; ++iBlock) { final int p0 = iBlock * BLOCK_SIZE; final int pStart = FastMath.max(startRow, p0); final int pEnd = FastMath.min((iBlock + 1) * BLOCK_SIZE, 1 + endRow); for (int jBlock = startColumn / BLOCK_SIZE; jBlock < 1 + endColumn / BLOCK_SIZE; ++jBlock) { final int jWidth = blockWidth(jBlock); final int q0 = jBlock * BLOCK_SIZE; final int qStart = FastMath.max(startColumn, q0); final int qEnd = FastMath.min((jBlock + 1) * BLOCK_SIZE, 1 + endColumn); final double[] block = blocks[iBlock * blockColumns + jBlock]; for (int p = pStart; p < pEnd; ++p) { int k = (p - p0) * jWidth + qStart - q0; for (int q = qStart; q < qEnd; ++q) { visitor.visit(p, q, block[k]); ++k; } } } } return visitor.end(); } /** * Get the height of a block. * @param blockRow row index (in block sense) of the block * @return height (number of rows) of the block */ private int blockHeight(final int blockRow) { return (blockRow == blockRows - 1) ? rows - blockRow * BLOCK_SIZE : BLOCK_SIZE; } /** * Get the width of a block. * @param blockColumn column index (in block sense) of the block * @return width (number of columns) of the block */ private int blockWidth(final int blockColumn) { return (blockColumn == blockColumns - 1) ? columns - blockColumn * BLOCK_SIZE : BLOCK_SIZE; } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/linear/SparseRealMatrix.java100644 1750 1750 2456 12126627717 30101 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.linear; /** * Marker interface for {@link RealMatrix} implementations that require sparse backing storage * * @version $Id: SparseRealMatrix.java 1416643 2012-12-03 19:37:14Z tn $ * @since 2.0 * @deprecated As of version 3.1, this class is deprecated, for reasons exposed * in this JIRA * ticket. This * class will be removed in version 4.0. * */ @Deprecated public interface SparseRealMatrix extends RealMatrix { } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/linear/SparseFieldVector.java100644 1750 1750 47720 12126627717 30262 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.linear; import java.io.Serializable; import org.apache.commons.math3.Field; import org.apache.commons.math3.FieldElement; import org.apache.commons.math3.exception.DimensionMismatchException; import org.apache.commons.math3.exception.MathArithmeticException; import org.apache.commons.math3.exception.NotPositiveException; import org.apache.commons.math3.exception.NullArgumentException; import org.apache.commons.math3.exception.OutOfRangeException; import org.apache.commons.math3.exception.util.LocalizedFormats; import org.apache.commons.math3.util.MathArrays; import org.apache.commons.math3.util.MathUtils; import org.apache.commons.math3.util.OpenIntToFieldHashMap; /** * This class implements the {@link FieldVector} interface with a {@link OpenIntToFieldHashMap} backing store. * @param the type of the field elements * @version $Id: SparseFieldVector.java 1455233 2013-03-11 17:00:41Z luc $ * @since 2.0 * @deprecated As of version 3.1, this class is deprecated, for reasons exposed * in this JIRA * ticket. This * class will be removed in version 4.0. */ @Deprecated public class SparseFieldVector> implements FieldVector, Serializable { /** Serialization identifier. */ private static final long serialVersionUID = 7841233292190413362L; /** Field to which the elements belong. */ private final Field field; /** Entries of the vector. */ private final OpenIntToFieldHashMap entries; /** Dimension of the vector. */ private final int virtualSize; /** * Build a 0-length vector. * Zero-length vectors may be used to initialize construction of vectors * by data gathering. We start with zero-length and use either the {@link * #SparseFieldVector(SparseFieldVector, int)} constructor * or one of the {@code append} method ({@link #append(FieldVector)} or * {@link #append(SparseFieldVector)}) to gather data into this vector. * * @param field Field to which the elements belong. */ public SparseFieldVector(Field field) { this(field, 0); } /** * Construct a vector of zeroes. * * @param field Field to which the elements belong. * @param dimension Size of the vector. */ public SparseFieldVector(Field field, int dimension) { this.field = field; virtualSize = dimension; entries = new OpenIntToFieldHashMap(field); } /** * Build a resized vector, for use with append. * * @param v Original vector * @param resize Amount to add. */ protected SparseFieldVector(SparseFieldVector v, int resize) { field = v.field; virtualSize = v.getDimension() + resize; entries = new OpenIntToFieldHashMap(v.entries); } /** * Build a vector with known the sparseness (for advanced use only). * * @param field Field to which the elements belong. * @param dimension Size of the vector. * @param expectedSize Expected number of non-zero entries. */ public SparseFieldVector(Field field, int dimension, int expectedSize) { this.field = field; virtualSize = dimension; entries = new OpenIntToFieldHashMap(field,expectedSize); } /** * Create from a Field array. * Only non-zero entries will be stored. * * @param field Field to which the elements belong. * @param values Set of values to create from. * @exception NullArgumentException if values is null */ public SparseFieldVector(Field field, T[] values) throws NullArgumentException { MathUtils.checkNotNull(values); this.field = field; virtualSize = values.length; entries = new OpenIntToFieldHashMap(field); for (int key = 0; key < values.length; key++) { T value = values[key]; entries.put(key, value); } } /** * Copy constructor. * * @param v Instance to copy. */ public SparseFieldVector(SparseFieldVector v) { field = v.field; virtualSize = v.getDimension(); entries = new OpenIntToFieldHashMap(v.getEntries()); } /** * Get the entries of this instance. * * @return the entries of this instance */ private OpenIntToFieldHashMap getEntries() { return entries; } /** * Optimized method to add sparse vectors. * * @param v Vector to add. * @return {@code this + v}. * @throws DimensionMismatchException if {@code v} is not the same size as * {@code this}. */ public FieldVector add(SparseFieldVector v) throws DimensionMismatchException { checkVectorDimensions(v.getDimension()); SparseFieldVector res = (SparseFieldVector)copy(); OpenIntToFieldHashMap.Iterator iter = v.getEntries().iterator(); while (iter.hasNext()) { iter.advance(); int key = iter.key(); T value = iter.value(); if (entries.containsKey(key)) { res.setEntry(key, entries.get(key).add(value)); } else { res.setEntry(key, value); } } return res; } /** * Construct a vector by appending a vector to this vector. * * @param v Vector to append to this one. * @return a new vector. */ public FieldVector append(SparseFieldVector v) { SparseFieldVector res = new SparseFieldVector(this, v.getDimension()); OpenIntToFieldHashMap.Iterator iter = v.entries.iterator(); while (iter.hasNext()) { iter.advance(); res.setEntry(iter.key() + virtualSize, iter.value()); } return res; } /** {@inheritDoc} */ public FieldVector append(FieldVector v) { if (v instanceof SparseFieldVector) { return append((SparseFieldVector) v); } else { final int n = v.getDimension(); FieldVector res = new SparseFieldVector(this, n); for (int i = 0; i < n; i++) { res.setEntry(i + virtualSize, v.getEntry(i)); } return res; } } /** {@inheritDoc} * @exception NullArgumentException if d is null */ public FieldVector append(T d) throws NullArgumentException { MathUtils.checkNotNull(d); FieldVector res = new SparseFieldVector(this, 1); res.setEntry(virtualSize, d); return res; } /** {@inheritDoc} */ public FieldVector copy() { return new SparseFieldVector(this); } /** {@inheritDoc} */ public T dotProduct(FieldVector v) throws DimensionMismatchException { checkVectorDimensions(v.getDimension()); T res = field.getZero(); OpenIntToFieldHashMap.Iterator iter = entries.iterator(); while (iter.hasNext()) { iter.advance(); res = res.add(v.getEntry(iter.key()).multiply(iter.value())); } return res; } /** {@inheritDoc} */ public FieldVector ebeDivide(FieldVector v) throws DimensionMismatchException, MathArithmeticException { checkVectorDimensions(v.getDimension()); SparseFieldVector res = new SparseFieldVector(this); OpenIntToFieldHashMap.Iterator iter = res.entries.iterator(); while (iter.hasNext()) { iter.advance(); res.setEntry(iter.key(), iter.value().divide(v.getEntry(iter.key()))); } return res; } /** {@inheritDoc} */ public FieldVector ebeMultiply(FieldVector v) throws DimensionMismatchException { checkVectorDimensions(v.getDimension()); SparseFieldVector res = new SparseFieldVector(this); OpenIntToFieldHashMap.Iterator iter = res.entries.iterator(); while (iter.hasNext()) { iter.advance(); res.setEntry(iter.key(), iter.value().multiply(v.getEntry(iter.key()))); } return res; } /** * {@inheritDoc} * * @deprecated as of 3.1, to be removed in 4.0. Please use the {@link #toArray()} method instead. */ @Deprecated public T[] getData() { return toArray(); } /** {@inheritDoc} */ public int getDimension() { return virtualSize; } /** {@inheritDoc} */ public T getEntry(int index) throws OutOfRangeException { checkIndex(index); return entries.get(index); } /** {@inheritDoc} */ public Field getField() { return field; } /** {@inheritDoc} */ public FieldVector getSubVector(int index, int n) throws OutOfRangeException, NotPositiveException { if (n < 0) { throw new NotPositiveException(LocalizedFormats.NUMBER_OF_ELEMENTS_SHOULD_BE_POSITIVE, n); } checkIndex(index); checkIndex(index + n - 1); SparseFieldVector res = new SparseFieldVector(field,n); int end = index + n; OpenIntToFieldHashMap.Iterator iter = entries.iterator(); while (iter.hasNext()) { iter.advance(); int key = iter.key(); if (key >= index && key < end) { res.setEntry(key - index, iter.value()); } } return res; } /** {@inheritDoc} */ public FieldVector mapAdd(T d) throws NullArgumentException { return copy().mapAddToSelf(d); } /** {@inheritDoc} */ public FieldVector mapAddToSelf(T d) throws NullArgumentException { for (int i = 0; i < virtualSize; i++) { setEntry(i, getEntry(i).add(d)); } return this; } /** {@inheritDoc} */ public FieldVector mapDivide(T d) throws NullArgumentException, MathArithmeticException { return copy().mapDivideToSelf(d); } /** {@inheritDoc} */ public FieldVector mapDivideToSelf(T d) throws NullArgumentException, MathArithmeticException { OpenIntToFieldHashMap.Iterator iter = entries.iterator(); while (iter.hasNext()) { iter.advance(); entries.put(iter.key(), iter.value().divide(d)); } return this; } /** {@inheritDoc} */ public FieldVector mapInv() throws MathArithmeticException { return copy().mapInvToSelf(); } /** {@inheritDoc} */ public FieldVector mapInvToSelf() throws MathArithmeticException { for (int i = 0; i < virtualSize; i++) { setEntry(i, field.getOne().divide(getEntry(i))); } return this; } /** {@inheritDoc} */ public FieldVector mapMultiply(T d) throws NullArgumentException { return copy().mapMultiplyToSelf(d); } /** {@inheritDoc} */ public FieldVector mapMultiplyToSelf(T d) throws NullArgumentException { OpenIntToFieldHashMap.Iterator iter = entries.iterator(); while (iter.hasNext()) { iter.advance(); entries.put(iter.key(), iter.value().multiply(d)); } return this; } /** {@inheritDoc} */ public FieldVector mapSubtract(T d) throws NullArgumentException { return copy().mapSubtractToSelf(d); } /** {@inheritDoc} */ public FieldVector mapSubtractToSelf(T d) throws NullArgumentException { return mapAddToSelf(field.getZero().subtract(d)); } /** * Optimized method to compute outer product when both vectors are sparse. * @param v vector with which outer product should be computed * @return the matrix outer product between instance and v */ public FieldMatrix outerProduct(SparseFieldVector v) { final int n = v.getDimension(); SparseFieldMatrix res = new SparseFieldMatrix(field, virtualSize, n); OpenIntToFieldHashMap.Iterator iter = entries.iterator(); while (iter.hasNext()) { iter.advance(); OpenIntToFieldHashMap.Iterator iter2 = v.entries.iterator(); while (iter2.hasNext()) { iter2.advance(); res.setEntry(iter.key(), iter2.key(), iter.value().multiply(iter2.value())); } } return res; } /** {@inheritDoc} */ public FieldMatrix outerProduct(FieldVector v) { if (v instanceof SparseFieldVector) { return outerProduct((SparseFieldVector)v); } else { final int n = v.getDimension(); FieldMatrix res = new SparseFieldMatrix(field, virtualSize, n); OpenIntToFieldHashMap.Iterator iter = entries.iterator(); while (iter.hasNext()) { iter.advance(); int row = iter.key(); FieldElementvalue = iter.value(); for (int col = 0; col < n; col++) { res.setEntry(row, col, value.multiply(v.getEntry(col))); } } return res; } } /** {@inheritDoc} */ public FieldVector projection(FieldVector v) throws DimensionMismatchException, MathArithmeticException { checkVectorDimensions(v.getDimension()); return v.mapMultiply(dotProduct(v).divide(v.dotProduct(v))); } /** {@inheritDoc} * @exception NullArgumentException if value is null */ public void set(T value) { MathUtils.checkNotNull(value); for (int i = 0; i < virtualSize; i++) { setEntry(i, value); } } /** {@inheritDoc} * @exception NullArgumentException if value is null */ public void setEntry(int index, T value) throws NullArgumentException, OutOfRangeException { MathUtils.checkNotNull(value); checkIndex(index); entries.put(index, value); } /** {@inheritDoc} */ public void setSubVector(int index, FieldVector v) throws OutOfRangeException { checkIndex(index); checkIndex(index + v.getDimension() - 1); final int n = v.getDimension(); for (int i = 0; i < n; i++) { setEntry(i + index, v.getEntry(i)); } } /** * Optimized method to compute {@code this} minus {@code v}. * @param v vector to be subtracted * @return {@code this - v} * @throws DimensionMismatchException if {@code v} is not the same size as * {@code this}. */ public SparseFieldVector subtract(SparseFieldVector v) throws DimensionMismatchException { checkVectorDimensions(v.getDimension()); SparseFieldVector res = (SparseFieldVector)copy(); OpenIntToFieldHashMap.Iterator iter = v.getEntries().iterator(); while (iter.hasNext()) { iter.advance(); int key = iter.key(); if (entries.containsKey(key)) { res.setEntry(key, entries.get(key).subtract(iter.value())); } else { res.setEntry(key, field.getZero().subtract(iter.value())); } } return res; } /** {@inheritDoc} */ public FieldVector subtract(FieldVector v) throws DimensionMismatchException { if (v instanceof SparseFieldVector) { return subtract((SparseFieldVector)v); } else { final int n = v.getDimension(); checkVectorDimensions(n); SparseFieldVector res = new SparseFieldVector(this); for (int i = 0; i < n; i++) { if (entries.containsKey(i)) { res.setEntry(i, entries.get(i).subtract(v.getEntry(i))); } else { res.setEntry(i, field.getZero().subtract(v.getEntry(i))); } } return res; } } /** {@inheritDoc} */ public T[] toArray() { T[] res = MathArrays.buildArray(field, virtualSize); OpenIntToFieldHashMap.Iterator iter = entries.iterator(); while (iter.hasNext()) { iter.advance(); res[iter.key()] = iter.value(); } return res; } /** * Check whether an index is valid. * * @param index Index to check. * @throws OutOfRangeException if the index is not valid. */ private void checkIndex(final int index) throws OutOfRangeException { if (index < 0 || index >= getDimension()) { throw new OutOfRangeException(index, 0, getDimension() - 1); } } /** * Check if instance dimension is equal to some expected value. * * @param n Expected dimension. * @throws DimensionMismatchException if the dimensions do not match. */ protected void checkVectorDimensions(int n) throws DimensionMismatchException { if (getDimension() != n) { throw new DimensionMismatchException(getDimension(), n); } } /** {@inheritDoc} */ public FieldVector add(FieldVector v) throws DimensionMismatchException { if (v instanceof SparseFieldVector) { return add((SparseFieldVector) v); } else { final int n = v.getDimension(); checkVectorDimensions(n); SparseFieldVector res = new SparseFieldVector(field, getDimension()); for (int i = 0; i < n; i++) { res.setEntry(i, v.getEntry(i).add(getEntry(i))); } return res; } } /** {@inheritDoc} */ @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((field == null) ? 0 : field.hashCode()); result = prime * result + virtualSize; OpenIntToFieldHashMap.Iterator iter = entries.iterator(); while (iter.hasNext()) { iter.advance(); int temp = iter.value().hashCode(); result = prime * result + temp; } return result; } /** {@inheritDoc} */ @Override public boolean equals(Object obj) { if (this == obj) { return true; } if (!(obj instanceof SparseFieldVector)) { return false; } @SuppressWarnings("unchecked") // OK, because "else if" check below ensures that // other must be the same type as this SparseFieldVector other = (SparseFieldVector) obj; if (field == null) { if (other.field != null) { return false; } } else if (!field.equals(other.field)) { return false; } if (virtualSize != other.virtualSize) { return false; } OpenIntToFieldHashMap.Iterator iter = entries.iterator(); while (iter.hasNext()) { iter.advance(); T test = other.getEntry(iter.key()); if (!test.equals(iter.value())) { return false; } } iter = other.getEntries().iterator(); while (iter.hasNext()) { iter.advance(); T test = iter.value(); if (!test.equals(getEntry(iter.key()))) { return false; } } return true; } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/linear/Array2DRowFieldMatrix.java100644 1750 1750 55334 12126627717 30763 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.linear; import java.io.Serializable; import org.apache.commons.math3.Field; import org.apache.commons.math3.FieldElement; import org.apache.commons.math3.exception.NoDataException; import org.apache.commons.math3.exception.DimensionMismatchException; import org.apache.commons.math3.exception.MathIllegalStateException; import org.apache.commons.math3.exception.NotStrictlyPositiveException; import org.apache.commons.math3.exception.NullArgumentException; import org.apache.commons.math3.exception.NumberIsTooSmallException; import org.apache.commons.math3.exception.OutOfRangeException; import org.apache.commons.math3.exception.util.LocalizedFormats; import org.apache.commons.math3.util.MathArrays; import org.apache.commons.math3.util.MathUtils; /** * Implementation of FieldMatrix using a {@link FieldElement}[][] array to store entries. *

                              * As specified in the {@link FieldMatrix} interface, matrix element indexing * is 0-based -- e.g., getEntry(0, 0) * returns the element in the first row, first column of the matrix. *

                              * * @param the type of the field elements * @version $Id: Array2DRowFieldMatrix.java 1449528 2013-02-24 19:06:20Z luc $ */ public class Array2DRowFieldMatrix> extends AbstractFieldMatrix implements Serializable { /** Serializable version identifier */ private static final long serialVersionUID = 7260756672015356458L; /** Entries of the matrix */ private T[][] data; /** * Creates a matrix with no data * @param field field to which the elements belong */ public Array2DRowFieldMatrix(final Field field) { super(field); } /** * Create a new {@code FieldMatrix} with the supplied row and column dimensions. * * @param field Field to which the elements belong. * @param rowDimension Number of rows in the new matrix. * @param columnDimension Number of columns in the new matrix. * @throws NotStrictlyPositiveException if row or column dimension is not positive. */ public Array2DRowFieldMatrix(final Field field, final int rowDimension, final int columnDimension) throws NotStrictlyPositiveException { super(field, rowDimension, columnDimension); data = MathArrays.buildArray(field, rowDimension, columnDimension); } /** * Create a new {@code FieldMatrix} using the input array as the underlying * data array. *

                              The input array is copied, not referenced. This constructor has * the same effect as calling {@link #Array2DRowFieldMatrix(FieldElement[][], boolean)} * with the second argument set to {@code true}.

                              * * @param d Data for the new matrix. * @throws DimensionMismatchException if {@code d} is not rectangular. * @throws NullArgumentException if {@code d} is {@code null}. * @throws NoDataException if there are not at least one row and one column. * @see #Array2DRowFieldMatrix(FieldElement[][], boolean) */ public Array2DRowFieldMatrix(final T[][] d) throws DimensionMismatchException, NullArgumentException, NoDataException { this(extractField(d), d); } /** * Create a new {@code FieldMatrix} using the input array as the underlying * data array. *

                              The input array is copied, not referenced. This constructor has * the same effect as calling {@link #Array2DRowFieldMatrix(FieldElement[][], boolean)} * with the second argument set to {@code true}.

                              * * @param field Field to which the elements belong. * @param d Data for the new matrix. * @throws DimensionMismatchException if {@code d} is not rectangular. * @throws NullArgumentException if {@code d} is {@code null}. * @throws NoDataException if there are not at least one row and one column. * @see #Array2DRowFieldMatrix(FieldElement[][], boolean) */ public Array2DRowFieldMatrix(final Field field, final T[][] d) throws DimensionMismatchException, NullArgumentException, NoDataException { super(field); copyIn(d); } /** * Create a new {@code FieldMatrix} using the input array as the underlying * data array. *

                              If an array is built specially in order to be embedded in a * {@code FieldMatrix} and not used directly, the {@code copyArray} may be * set to {@code false}. This will prevent the copying and improve * performance as no new array will be built and no data will be copied.

                              * * @param d Data for the new matrix. * @param copyArray Whether to copy or reference the input array. * @throws DimensionMismatchException if {@code d} is not rectangular. * @throws NoDataException if there are not at least one row and one column. * @throws NullArgumentException if {@code d} is {@code null}. * @see #Array2DRowFieldMatrix(FieldElement[][]) */ public Array2DRowFieldMatrix(final T[][] d, final boolean copyArray) throws DimensionMismatchException, NoDataException, NullArgumentException { this(extractField(d), d, copyArray); } /** * Create a new {@code FieldMatrix} using the input array as the underlying * data array. *

                              If an array is built specially in order to be embedded in a * {@code FieldMatrix} and not used directly, the {@code copyArray} may be * set to {@code false}. This will prevent the copying and improve * performance as no new array will be built and no data will be copied.

                              * * @param field Field to which the elements belong. * @param d Data for the new matrix. * @param copyArray Whether to copy or reference the input array. * @throws DimensionMismatchException if {@code d} is not rectangular. * @throws NoDataException if there are not at least one row and one column. * @throws NullArgumentException if {@code d} is {@code null}. * @see #Array2DRowFieldMatrix(FieldElement[][]) */ public Array2DRowFieldMatrix(final Field field, final T[][] d, final boolean copyArray) throws DimensionMismatchException, NoDataException, NullArgumentException { super(field); if (copyArray) { copyIn(d); } else { MathUtils.checkNotNull(d); final int nRows = d.length; if (nRows == 0) { throw new NoDataException(LocalizedFormats.AT_LEAST_ONE_ROW); } final int nCols = d[0].length; if (nCols == 0) { throw new NoDataException(LocalizedFormats.AT_LEAST_ONE_COLUMN); } for (int r = 1; r < nRows; r++) { if (d[r].length != nCols) { throw new DimensionMismatchException(nCols, d[r].length); } } data = d; } } /** * Create a new (column) {@code FieldMatrix} using {@code v} as the * data for the unique column of the created matrix. * The input array is copied. * * @param v Column vector holding data for new matrix. * @throws NoDataException if v is empty */ public Array2DRowFieldMatrix(final T[] v) throws NoDataException { this(extractField(v), v); } /** * Create a new (column) {@code FieldMatrix} using {@code v} as the * data for the unique column of the created matrix. * The input array is copied. * * @param field Field to which the elements belong. * @param v Column vector holding data for new matrix. */ public Array2DRowFieldMatrix(final Field field, final T[] v) { super(field); final int nRows = v.length; data = MathArrays.buildArray(getField(), nRows, 1); for (int row = 0; row < nRows; row++) { data[row][0] = v[row]; } } /** {@inheritDoc} */ @Override public FieldMatrix createMatrix(final int rowDimension, final int columnDimension) throws NotStrictlyPositiveException { return new Array2DRowFieldMatrix(getField(), rowDimension, columnDimension); } /** {@inheritDoc} */ @Override public FieldMatrix copy() { return new Array2DRowFieldMatrix(getField(), copyOut(), false); } /** * Add {@code m} to this matrix. * * @param m Matrix to be added. * @return {@code this} + m. * @throws MatrixDimensionMismatchException if {@code m} is not the same * size as this matrix. */ public Array2DRowFieldMatrix add(final Array2DRowFieldMatrix m) throws MatrixDimensionMismatchException { // safety check checkAdditionCompatible(m); final int rowCount = getRowDimension(); final int columnCount = getColumnDimension(); final T[][] outData = MathArrays.buildArray(getField(), rowCount, columnCount); for (int row = 0; row < rowCount; row++) { final T[] dataRow = data[row]; final T[] mRow = m.data[row]; final T[] outDataRow = outData[row]; for (int col = 0; col < columnCount; col++) { outDataRow[col] = dataRow[col].add(mRow[col]); } } return new Array2DRowFieldMatrix(getField(), outData, false); } /** * Subtract {@code m} from this matrix. * * @param m Matrix to be subtracted. * @return {@code this} + m. * @throws MatrixDimensionMismatchException if {@code m} is not the same * size as this matrix. */ public Array2DRowFieldMatrix subtract(final Array2DRowFieldMatrix m) throws MatrixDimensionMismatchException { // safety check checkSubtractionCompatible(m); final int rowCount = getRowDimension(); final int columnCount = getColumnDimension(); final T[][] outData = MathArrays.buildArray(getField(), rowCount, columnCount); for (int row = 0; row < rowCount; row++) { final T[] dataRow = data[row]; final T[] mRow = m.data[row]; final T[] outDataRow = outData[row]; for (int col = 0; col < columnCount; col++) { outDataRow[col] = dataRow[col].subtract(mRow[col]); } } return new Array2DRowFieldMatrix(getField(), outData, false); } /** * Postmultiplying this matrix by {@code m}. * * @param m Matrix to postmultiply by. * @return {@code this} * m. * @throws DimensionMismatchException if the number of columns of this * matrix is not equal to the number of rows of {@code m}. */ public Array2DRowFieldMatrix multiply(final Array2DRowFieldMatrix m) throws DimensionMismatchException { // safety check checkMultiplicationCompatible(m); final int nRows = this.getRowDimension(); final int nCols = m.getColumnDimension(); final int nSum = this.getColumnDimension(); final T[][] outData = MathArrays.buildArray(getField(), nRows, nCols); for (int row = 0; row < nRows; row++) { final T[] dataRow = data[row]; final T[] outDataRow = outData[row]; for (int col = 0; col < nCols; col++) { T sum = getField().getZero(); for (int i = 0; i < nSum; i++) { sum = sum.add(dataRow[i].multiply(m.data[i][col])); } outDataRow[col] = sum; } } return new Array2DRowFieldMatrix(getField(), outData, false); } /** {@inheritDoc} */ @Override public T[][] getData() { return copyOut(); } /** * Get a reference to the underlying data array. * This methods returns internal data, not fresh copy of it. * * @return the 2-dimensional array of entries. */ public T[][] getDataRef() { return data; } /** {@inheritDoc} */ @Override public void setSubMatrix(final T[][] subMatrix, final int row, final int column) throws OutOfRangeException, NullArgumentException, NoDataException, DimensionMismatchException { if (data == null) { if (row > 0) { throw new MathIllegalStateException(LocalizedFormats.FIRST_ROWS_NOT_INITIALIZED_YET, row); } if (column > 0) { throw new MathIllegalStateException(LocalizedFormats.FIRST_COLUMNS_NOT_INITIALIZED_YET, column); } final int nRows = subMatrix.length; if (nRows == 0) { throw new NoDataException(LocalizedFormats.AT_LEAST_ONE_ROW); } final int nCols = subMatrix[0].length; if (nCols == 0) { throw new NoDataException(LocalizedFormats.AT_LEAST_ONE_COLUMN); } data = MathArrays.buildArray(getField(), subMatrix.length, nCols); for (int i = 0; i < data.length; ++i) { if (subMatrix[i].length != nCols) { throw new DimensionMismatchException(nCols, subMatrix[i].length); } System.arraycopy(subMatrix[i], 0, data[i + row], column, nCols); } } else { super.setSubMatrix(subMatrix, row, column); } } /** {@inheritDoc} */ @Override public T getEntry(final int row, final int column) throws OutOfRangeException { checkRowIndex(row); checkColumnIndex(column); return data[row][column]; } /** {@inheritDoc} */ @Override public void setEntry(final int row, final int column, final T value) throws OutOfRangeException { checkRowIndex(row); checkColumnIndex(column); data[row][column] = value; } /** {@inheritDoc} */ @Override public void addToEntry(final int row, final int column, final T increment) throws OutOfRangeException { checkRowIndex(row); checkColumnIndex(column); data[row][column] = data[row][column].add(increment); } /** {@inheritDoc} */ @Override public void multiplyEntry(final int row, final int column, final T factor) throws OutOfRangeException { checkRowIndex(row); checkColumnIndex(column); data[row][column] = data[row][column].multiply(factor); } /** {@inheritDoc} */ @Override public int getRowDimension() { return (data == null) ? 0 : data.length; } /** {@inheritDoc} */ @Override public int getColumnDimension() { return ((data == null) || (data[0] == null)) ? 0 : data[0].length; } /** {@inheritDoc} */ @Override public T[] operate(final T[] v) throws DimensionMismatchException { final int nRows = this.getRowDimension(); final int nCols = this.getColumnDimension(); if (v.length != nCols) { throw new DimensionMismatchException(v.length, nCols); } final T[] out = MathArrays.buildArray(getField(), nRows); for (int row = 0; row < nRows; row++) { final T[] dataRow = data[row]; T sum = getField().getZero(); for (int i = 0; i < nCols; i++) { sum = sum.add(dataRow[i].multiply(v[i])); } out[row] = sum; } return out; } /** {@inheritDoc} */ @Override public T[] preMultiply(final T[] v) throws DimensionMismatchException { final int nRows = getRowDimension(); final int nCols = getColumnDimension(); if (v.length != nRows) { throw new DimensionMismatchException(v.length, nRows); } final T[] out = MathArrays.buildArray(getField(), nCols); for (int col = 0; col < nCols; ++col) { T sum = getField().getZero(); for (int i = 0; i < nRows; ++i) { sum = sum.add(data[i][col].multiply(v[i])); } out[col] = sum; } return out; } /** {@inheritDoc} */ @Override public T walkInRowOrder(final FieldMatrixChangingVisitor visitor) { final int rows = getRowDimension(); final int columns = getColumnDimension(); visitor.start(rows, columns, 0, rows - 1, 0, columns - 1); for (int i = 0; i < rows; ++i) { final T[] rowI = data[i]; for (int j = 0; j < columns; ++j) { rowI[j] = visitor.visit(i, j, rowI[j]); } } return visitor.end(); } /** {@inheritDoc} */ @Override public T walkInRowOrder(final FieldMatrixPreservingVisitor visitor) { final int rows = getRowDimension(); final int columns = getColumnDimension(); visitor.start(rows, columns, 0, rows - 1, 0, columns - 1); for (int i = 0; i < rows; ++i) { final T[] rowI = data[i]; for (int j = 0; j < columns; ++j) { visitor.visit(i, j, rowI[j]); } } return visitor.end(); } /** {@inheritDoc} */ @Override public T walkInRowOrder(final FieldMatrixChangingVisitor visitor, final int startRow, final int endRow, final int startColumn, final int endColumn) throws OutOfRangeException, NumberIsTooSmallException { checkSubMatrixIndex(startRow, endRow, startColumn, endColumn); visitor.start(getRowDimension(), getColumnDimension(), startRow, endRow, startColumn, endColumn); for (int i = startRow; i <= endRow; ++i) { final T[] rowI = data[i]; for (int j = startColumn; j <= endColumn; ++j) { rowI[j] = visitor.visit(i, j, rowI[j]); } } return visitor.end(); } /** {@inheritDoc} */ @Override public T walkInRowOrder(final FieldMatrixPreservingVisitor visitor, final int startRow, final int endRow, final int startColumn, final int endColumn) throws OutOfRangeException, NumberIsTooSmallException { checkSubMatrixIndex(startRow, endRow, startColumn, endColumn); visitor.start(getRowDimension(), getColumnDimension(), startRow, endRow, startColumn, endColumn); for (int i = startRow; i <= endRow; ++i) { final T[] rowI = data[i]; for (int j = startColumn; j <= endColumn; ++j) { visitor.visit(i, j, rowI[j]); } } return visitor.end(); } /** {@inheritDoc} */ @Override public T walkInColumnOrder(final FieldMatrixChangingVisitor visitor) { final int rows = getRowDimension(); final int columns = getColumnDimension(); visitor.start(rows, columns, 0, rows - 1, 0, columns - 1); for (int j = 0; j < columns; ++j) { for (int i = 0; i < rows; ++i) { final T[] rowI = data[i]; rowI[j] = visitor.visit(i, j, rowI[j]); } } return visitor.end(); } /** {@inheritDoc} */ @Override public T walkInColumnOrder(final FieldMatrixPreservingVisitor visitor) { final int rows = getRowDimension(); final int columns = getColumnDimension(); visitor.start(rows, columns, 0, rows - 1, 0, columns - 1); for (int j = 0; j < columns; ++j) { for (int i = 0; i < rows; ++i) { visitor.visit(i, j, data[i][j]); } } return visitor.end(); } /** {@inheritDoc} */ @Override public T walkInColumnOrder(final FieldMatrixChangingVisitor visitor, final int startRow, final int endRow, final int startColumn, final int endColumn) throws OutOfRangeException, NumberIsTooSmallException { checkSubMatrixIndex(startRow, endRow, startColumn, endColumn); visitor.start(getRowDimension(), getColumnDimension(), startRow, endRow, startColumn, endColumn); for (int j = startColumn; j <= endColumn; ++j) { for (int i = startRow; i <= endRow; ++i) { final T[] rowI = data[i]; rowI[j] = visitor.visit(i, j, rowI[j]); } } return visitor.end(); } /** {@inheritDoc} */ @Override public T walkInColumnOrder(final FieldMatrixPreservingVisitor visitor, final int startRow, final int endRow, final int startColumn, final int endColumn) throws OutOfRangeException, NumberIsTooSmallException { checkSubMatrixIndex(startRow, endRow, startColumn, endColumn); visitor.start(getRowDimension(), getColumnDimension(), startRow, endRow, startColumn, endColumn); for (int j = startColumn; j <= endColumn; ++j) { for (int i = startRow; i <= endRow; ++i) { visitor.visit(i, j, data[i][j]); } } return visitor.end(); } /** * Get a fresh copy of the underlying data array. * * @return a copy of the underlying data array. */ private T[][] copyOut() { final int nRows = this.getRowDimension(); final T[][] out = MathArrays.buildArray(getField(), nRows, getColumnDimension()); // can't copy 2-d array in one shot, otherwise get row references for (int i = 0; i < nRows; i++) { System.arraycopy(data[i], 0, out[i], 0, data[i].length); } return out; } /** * Replace data with a fresh copy of the input array. * * @param in Data to copy. * @throws NoDataException if the input array is empty. * @throws DimensionMismatchException if the input array is not rectangular. * @throws NullArgumentException if the input array is {@code null}. */ private void copyIn(final T[][] in) throws NullArgumentException, NoDataException, DimensionMismatchException { setSubMatrix(in, 0, 0); } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/linear/QRDecomposition.java100644 1750 1750 41723 12126627717 27752 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.linear; import java.util.Arrays; import org.apache.commons.math3.exception.DimensionMismatchException; import org.apache.commons.math3.util.FastMath; /** * Calculates the QR-decomposition of a matrix. *

                              The QR-decomposition of a matrix A consists of two matrices Q and R * that satisfy: A = QR, Q is orthogonal (QTQ = I), and R is * upper triangular. If A is m×n, Q is m×m and R m×n.

                              *

                              This class compute the decomposition using Householder reflectors.

                              *

                              For efficiency purposes, the decomposition in packed form is transposed. * This allows inner loop to iterate inside rows, which is much more cache-efficient * in Java.

                              *

                              This class is based on the class with similar name from the * JAMA library, with the * following changes:

                              *
                                *
                              • a {@link #getQT() getQT} method has been added,
                              • *
                              • the {@code solve} and {@code isFullRank} methods have been replaced * by a {@link #getSolver() getSolver} method and the equivalent methods * provided by the returned {@link DecompositionSolver}.
                              • *
                              * * @see MathWorld * @see Wikipedia * * @version $Id: QRDecomposition.java 1462423 2013-03-29 07:25:18Z luc $ * @since 1.2 (changed to concrete class in 3.0) */ public class QRDecomposition { /** * A packed TRANSPOSED representation of the QR decomposition. *

                              The elements BELOW the diagonal are the elements of the UPPER triangular * matrix R, and the rows ABOVE the diagonal are the Householder reflector vectors * from which an explicit form of Q can be recomputed if desired.

                              */ private double[][] qrt; /** The diagonal elements of R. */ private double[] rDiag; /** Cached value of Q. */ private RealMatrix cachedQ; /** Cached value of QT. */ private RealMatrix cachedQT; /** Cached value of R. */ private RealMatrix cachedR; /** Cached value of H. */ private RealMatrix cachedH; /** Singularity threshold. */ private final double threshold; /** * Calculates the QR-decomposition of the given matrix. * The singularity threshold defaults to zero. * * @param matrix The matrix to decompose. * * @see #QRDecomposition(RealMatrix,double) */ public QRDecomposition(RealMatrix matrix) { this(matrix, 0d); } /** * Calculates the QR-decomposition of the given matrix. * * @param matrix The matrix to decompose. * @param threshold Singularity threshold. */ public QRDecomposition(RealMatrix matrix, double threshold) { this.threshold = threshold; final int m = matrix.getRowDimension(); final int n = matrix.getColumnDimension(); qrt = matrix.transpose().getData(); rDiag = new double[FastMath.min(m, n)]; cachedQ = null; cachedQT = null; cachedR = null; cachedH = null; decompose(qrt); } /** Decompose matrix. * @param matrix transposed matrix * @since 3.2 */ protected void decompose(double[][] matrix) { for (int minor = 0; minor < FastMath.min(qrt.length, qrt[0].length); minor++) { performHouseholderReflection(minor, qrt); } } /** Perform Householder reflection for a minor A(minor, minor) of A. * @param minor minor index * @param matrix transposed matrix * @since 3.2 */ protected void performHouseholderReflection(int minor, double[][] matrix) { final double[] qrtMinor = qrt[minor]; /* * Let x be the first column of the minor, and a^2 = |x|^2. * x will be in the positions qr[minor][minor] through qr[m][minor]. * The first column of the transformed minor will be (a,0,0,..)' * The sign of a is chosen to be opposite to the sign of the first * component of x. Let's find a: */ double xNormSqr = 0; for (int row = minor; row < qrtMinor.length; row++) { final double c = qrtMinor[row]; xNormSqr += c * c; } final double a = (qrtMinor[minor] > 0) ? -FastMath.sqrt(xNormSqr) : FastMath.sqrt(xNormSqr); rDiag[minor] = a; if (a != 0.0) { /* * Calculate the normalized reflection vector v and transform * the first column. We know the norm of v beforehand: v = x-ae * so |v|^2 = = -2a+a^2 = * a^2+a^2-2a = 2a*(a - ). * Here is now qr[minor][minor]. * v = x-ae is stored in the column at qr: */ qrtMinor[minor] -= a; // now |v|^2 = -2a*(qr[minor][minor]) /* * Transform the rest of the columns of the minor: * They will be transformed by the matrix H = I-2vv'/|v|^2. * If x is a column vector of the minor, then * Hx = (I-2vv'/|v|^2)x = x-2vv'x/|v|^2 = x - 2/|v|^2 v. * Therefore the transformation is easily calculated by * subtracting the column vector (2/|v|^2)v from x. * * Let 2/|v|^2 = alpha. From above we have * |v|^2 = -2a*(qr[minor][minor]), so * alpha = -/(a*qr[minor][minor]) */ for (int col = minor+1; col < qrt.length; col++) { final double[] qrtCol = qrt[col]; double alpha = 0; for (int row = minor; row < qrtCol.length; row++) { alpha -= qrtCol[row] * qrtMinor[row]; } alpha /= a * qrtMinor[minor]; // Subtract the column vector alpha*v from x. for (int row = minor; row < qrtCol.length; row++) { qrtCol[row] -= alpha * qrtMinor[row]; } } } } /** * Returns the matrix R of the decomposition. *

                              R is an upper-triangular matrix

                              * @return the R matrix */ public RealMatrix getR() { if (cachedR == null) { // R is supposed to be m x n final int n = qrt.length; final int m = qrt[0].length; double[][] ra = new double[m][n]; // copy the diagonal from rDiag and the upper triangle of qr for (int row = FastMath.min(m, n) - 1; row >= 0; row--) { ra[row][row] = rDiag[row]; for (int col = row + 1; col < n; col++) { ra[row][col] = qrt[col][row]; } } cachedR = MatrixUtils.createRealMatrix(ra); } // return the cached matrix return cachedR; } /** * Returns the matrix Q of the decomposition. *

                              Q is an orthogonal matrix

                              * @return the Q matrix */ public RealMatrix getQ() { if (cachedQ == null) { cachedQ = getQT().transpose(); } return cachedQ; } /** * Returns the transpose of the matrix Q of the decomposition. *

                              Q is an orthogonal matrix

                              * @return the transpose of the Q matrix, QT */ public RealMatrix getQT() { if (cachedQT == null) { // QT is supposed to be m x m final int n = qrt.length; final int m = qrt[0].length; double[][] qta = new double[m][m]; /* * Q = Q1 Q2 ... Q_m, so Q is formed by first constructing Q_m and then * applying the Householder transformations Q_(m-1),Q_(m-2),...,Q1 in * succession to the result */ for (int minor = m - 1; minor >= FastMath.min(m, n); minor--) { qta[minor][minor] = 1.0d; } for (int minor = FastMath.min(m, n)-1; minor >= 0; minor--){ final double[] qrtMinor = qrt[minor]; qta[minor][minor] = 1.0d; if (qrtMinor[minor] != 0.0) { for (int col = minor; col < m; col++) { double alpha = 0; for (int row = minor; row < m; row++) { alpha -= qta[col][row] * qrtMinor[row]; } alpha /= rDiag[minor] * qrtMinor[minor]; for (int row = minor; row < m; row++) { qta[col][row] += -alpha * qrtMinor[row]; } } } } cachedQT = MatrixUtils.createRealMatrix(qta); } // return the cached matrix return cachedQT; } /** * Returns the Householder reflector vectors. *

                              H is a lower trapezoidal matrix whose columns represent * each successive Householder reflector vector. This matrix is used * to compute Q.

                              * @return a matrix containing the Householder reflector vectors */ public RealMatrix getH() { if (cachedH == null) { final int n = qrt.length; final int m = qrt[0].length; double[][] ha = new double[m][n]; for (int i = 0; i < m; ++i) { for (int j = 0; j < FastMath.min(i + 1, n); ++j) { ha[i][j] = qrt[j][i] / -rDiag[j]; } } cachedH = MatrixUtils.createRealMatrix(ha); } // return the cached matrix return cachedH; } /** * Get a solver for finding the A × X = B solution in least square sense. * @return a solver */ public DecompositionSolver getSolver() { return new Solver(qrt, rDiag, threshold); } /** Specialized solver. */ private static class Solver implements DecompositionSolver { /** * A packed TRANSPOSED representation of the QR decomposition. *

                              The elements BELOW the diagonal are the elements of the UPPER triangular * matrix R, and the rows ABOVE the diagonal are the Householder reflector vectors * from which an explicit form of Q can be recomputed if desired.

                              */ private final double[][] qrt; /** The diagonal elements of R. */ private final double[] rDiag; /** Singularity threshold. */ private final double threshold; /** * Build a solver from decomposed matrix. * * @param qrt Packed TRANSPOSED representation of the QR decomposition. * @param rDiag Diagonal elements of R. * @param threshold Singularity threshold. */ private Solver(final double[][] qrt, final double[] rDiag, final double threshold) { this.qrt = qrt; this.rDiag = rDiag; this.threshold = threshold; } /** {@inheritDoc} */ public boolean isNonSingular() { for (double diag : rDiag) { if (FastMath.abs(diag) <= threshold) { return false; } } return true; } /** {@inheritDoc} */ public RealVector solve(RealVector b) { final int n = qrt.length; final int m = qrt[0].length; if (b.getDimension() != m) { throw new DimensionMismatchException(b.getDimension(), m); } if (!isNonSingular()) { throw new SingularMatrixException(); } final double[] x = new double[n]; final double[] y = b.toArray(); // apply Householder transforms to solve Q.y = b for (int minor = 0; minor < FastMath.min(m, n); minor++) { final double[] qrtMinor = qrt[minor]; double dotProduct = 0; for (int row = minor; row < m; row++) { dotProduct += y[row] * qrtMinor[row]; } dotProduct /= rDiag[minor] * qrtMinor[minor]; for (int row = minor; row < m; row++) { y[row] += dotProduct * qrtMinor[row]; } } // solve triangular system R.x = y for (int row = rDiag.length - 1; row >= 0; --row) { y[row] /= rDiag[row]; final double yRow = y[row]; final double[] qrtRow = qrt[row]; x[row] = yRow; for (int i = 0; i < row; i++) { y[i] -= yRow * qrtRow[i]; } } return new ArrayRealVector(x, false); } /** {@inheritDoc} */ public RealMatrix solve(RealMatrix b) { final int n = qrt.length; final int m = qrt[0].length; if (b.getRowDimension() != m) { throw new DimensionMismatchException(b.getRowDimension(), m); } if (!isNonSingular()) { throw new SingularMatrixException(); } final int columns = b.getColumnDimension(); final int blockSize = BlockRealMatrix.BLOCK_SIZE; final int cBlocks = (columns + blockSize - 1) / blockSize; final double[][] xBlocks = BlockRealMatrix.createBlocksLayout(n, columns); final double[][] y = new double[b.getRowDimension()][blockSize]; final double[] alpha = new double[blockSize]; for (int kBlock = 0; kBlock < cBlocks; ++kBlock) { final int kStart = kBlock * blockSize; final int kEnd = FastMath.min(kStart + blockSize, columns); final int kWidth = kEnd - kStart; // get the right hand side vector b.copySubMatrix(0, m - 1, kStart, kEnd - 1, y); // apply Householder transforms to solve Q.y = b for (int minor = 0; minor < FastMath.min(m, n); minor++) { final double[] qrtMinor = qrt[minor]; final double factor = 1.0 / (rDiag[minor] * qrtMinor[minor]); Arrays.fill(alpha, 0, kWidth, 0.0); for (int row = minor; row < m; ++row) { final double d = qrtMinor[row]; final double[] yRow = y[row]; for (int k = 0; k < kWidth; ++k) { alpha[k] += d * yRow[k]; } } for (int k = 0; k < kWidth; ++k) { alpha[k] *= factor; } for (int row = minor; row < m; ++row) { final double d = qrtMinor[row]; final double[] yRow = y[row]; for (int k = 0; k < kWidth; ++k) { yRow[k] += alpha[k] * d; } } } // solve triangular system R.x = y for (int j = rDiag.length - 1; j >= 0; --j) { final int jBlock = j / blockSize; final int jStart = jBlock * blockSize; final double factor = 1.0 / rDiag[j]; final double[] yJ = y[j]; final double[] xBlock = xBlocks[jBlock * cBlocks + kBlock]; int index = (j - jStart) * kWidth; for (int k = 0; k < kWidth; ++k) { yJ[k] *= factor; xBlock[index++] = yJ[k]; } final double[] qrtJ = qrt[j]; for (int i = 0; i < j; ++i) { final double rIJ = qrtJ[i]; final double[] yI = y[i]; for (int k = 0; k < kWidth; ++k) { yI[k] -= yJ[k] * rIJ; } } } } return new BlockRealMatrix(n, columns, xBlocks, false); } /** {@inheritDoc} */ public RealMatrix getInverse() { return solve(MatrixUtils.createRealIdentityMatrix(rDiag.length)); } } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/linear/SingularOperatorException.java100644 1750 1750 2746 12126627717 32034 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.linear; import org.apache.commons.math3.exception.MathIllegalArgumentException; import org.apache.commons.math3.exception.util.LocalizedFormats; /** * Exception to be thrown when trying to invert a singular operator. * * @version $Id: SingularOperatorException.java 1416643 2012-12-03 19:37:14Z tn $ * @since 3.0 */ public class SingularOperatorException extends MathIllegalArgumentException { /** Serializable version Id. */ private static final long serialVersionUID = -476049978595245033L; /** * Creates a new instance of this class. */ public SingularOperatorException() { super(LocalizedFormats.SINGULAR_OPERATOR); } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/linear/CholeskyDecomposition.java100644 1750 1750 26163 12126627717 31212 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.linear; import org.apache.commons.math3.exception.DimensionMismatchException; import org.apache.commons.math3.util.FastMath; /** * Calculates the Cholesky decomposition of a matrix. *

                              The Cholesky decomposition of a real symmetric positive-definite * matrix A consists of a lower triangular matrix L with same size such * that: A = LLT. In a sense, this is the square root of A.

                              *

                              This class is based on the class with similar name from the * JAMA library, with the * following changes:

                              *
                                *
                              • a {@link #getLT() getLT} method has been added,
                              • *
                              • the {@code isspd} method has been removed, since the constructor of * this class throws a {@link NonPositiveDefiniteMatrixException} when a * matrix cannot be decomposed,
                              • *
                              • a {@link #getDeterminant() getDeterminant} method has been added,
                              • *
                              • the {@code solve} method has been replaced by a {@link #getSolver() * getSolver} method and the equivalent method provided by the returned * {@link DecompositionSolver}.
                              • *
                              * * @see MathWorld * @see Wikipedia * @version $Id: CholeskyDecomposition.java 1416643 2012-12-03 19:37:14Z tn $ * @since 2.0 (changed to concrete class in 3.0) */ public class CholeskyDecomposition { /** * Default threshold above which off-diagonal elements are considered too different * and matrix not symmetric. */ public static final double DEFAULT_RELATIVE_SYMMETRY_THRESHOLD = 1.0e-15; /** * Default threshold below which diagonal elements are considered null * and matrix not positive definite. */ public static final double DEFAULT_ABSOLUTE_POSITIVITY_THRESHOLD = 1.0e-10; /** Row-oriented storage for LT matrix data. */ private double[][] lTData; /** Cached value of L. */ private RealMatrix cachedL; /** Cached value of LT. */ private RealMatrix cachedLT; /** * Calculates the Cholesky decomposition of the given matrix. *

                              * Calling this constructor is equivalent to call {@link * #CholeskyDecomposition(RealMatrix, double, double)} with the * thresholds set to the default values {@link * #DEFAULT_RELATIVE_SYMMETRY_THRESHOLD} and {@link * #DEFAULT_ABSOLUTE_POSITIVITY_THRESHOLD} *

                              * @param matrix the matrix to decompose * @throws NonSquareMatrixException if the matrix is not square. * @throws NonSymmetricMatrixException if the matrix is not symmetric. * @throws NonPositiveDefiniteMatrixException if the matrix is not * strictly positive definite. * @see #CholeskyDecomposition(RealMatrix, double, double) * @see #DEFAULT_RELATIVE_SYMMETRY_THRESHOLD * @see #DEFAULT_ABSOLUTE_POSITIVITY_THRESHOLD */ public CholeskyDecomposition(final RealMatrix matrix) { this(matrix, DEFAULT_RELATIVE_SYMMETRY_THRESHOLD, DEFAULT_ABSOLUTE_POSITIVITY_THRESHOLD); } /** * Calculates the Cholesky decomposition of the given matrix. * @param matrix the matrix to decompose * @param relativeSymmetryThreshold threshold above which off-diagonal * elements are considered too different and matrix not symmetric * @param absolutePositivityThreshold threshold below which diagonal * elements are considered null and matrix not positive definite * @throws NonSquareMatrixException if the matrix is not square. * @throws NonSymmetricMatrixException if the matrix is not symmetric. * @throws NonPositiveDefiniteMatrixException if the matrix is not * strictly positive definite. * @see #CholeskyDecomposition(RealMatrix) * @see #DEFAULT_RELATIVE_SYMMETRY_THRESHOLD * @see #DEFAULT_ABSOLUTE_POSITIVITY_THRESHOLD */ public CholeskyDecomposition(final RealMatrix matrix, final double relativeSymmetryThreshold, final double absolutePositivityThreshold) { if (!matrix.isSquare()) { throw new NonSquareMatrixException(matrix.getRowDimension(), matrix.getColumnDimension()); } final int order = matrix.getRowDimension(); lTData = matrix.getData(); cachedL = null; cachedLT = null; // check the matrix before transformation for (int i = 0; i < order; ++i) { final double[] lI = lTData[i]; // check off-diagonal elements (and reset them to 0) for (int j = i + 1; j < order; ++j) { final double[] lJ = lTData[j]; final double lIJ = lI[j]; final double lJI = lJ[i]; final double maxDelta = relativeSymmetryThreshold * FastMath.max(FastMath.abs(lIJ), FastMath.abs(lJI)); if (FastMath.abs(lIJ - lJI) > maxDelta) { throw new NonSymmetricMatrixException(i, j, relativeSymmetryThreshold); } lJ[i] = 0; } } // transform the matrix for (int i = 0; i < order; ++i) { final double[] ltI = lTData[i]; // check diagonal element if (ltI[i] <= absolutePositivityThreshold) { throw new NonPositiveDefiniteMatrixException(ltI[i], i, absolutePositivityThreshold); } ltI[i] = FastMath.sqrt(ltI[i]); final double inverse = 1.0 / ltI[i]; for (int q = order - 1; q > i; --q) { ltI[q] *= inverse; final double[] ltQ = lTData[q]; for (int p = q; p < order; ++p) { ltQ[p] -= ltI[q] * ltI[p]; } } } } /** * Returns the matrix L of the decomposition. *

                              L is an lower-triangular matrix

                              * @return the L matrix */ public RealMatrix getL() { if (cachedL == null) { cachedL = getLT().transpose(); } return cachedL; } /** * Returns the transpose of the matrix L of the decomposition. *

                              LT is an upper-triangular matrix

                              * @return the transpose of the matrix L of the decomposition */ public RealMatrix getLT() { if (cachedLT == null) { cachedLT = MatrixUtils.createRealMatrix(lTData); } // return the cached matrix return cachedLT; } /** * Return the determinant of the matrix * @return determinant of the matrix */ public double getDeterminant() { double determinant = 1.0; for (int i = 0; i < lTData.length; ++i) { double lTii = lTData[i][i]; determinant *= lTii * lTii; } return determinant; } /** * Get a solver for finding the A × X = B solution in least square sense. * @return a solver */ public DecompositionSolver getSolver() { return new Solver(lTData); } /** Specialized solver. */ private static class Solver implements DecompositionSolver { /** Row-oriented storage for LT matrix data. */ private final double[][] lTData; /** * Build a solver from decomposed matrix. * @param lTData row-oriented storage for LT matrix data */ private Solver(final double[][] lTData) { this.lTData = lTData; } /** {@inheritDoc} */ public boolean isNonSingular() { // if we get this far, the matrix was positive definite, hence non-singular return true; } /** {@inheritDoc} */ public RealVector solve(final RealVector b) { final int m = lTData.length; if (b.getDimension() != m) { throw new DimensionMismatchException(b.getDimension(), m); } final double[] x = b.toArray(); // Solve LY = b for (int j = 0; j < m; j++) { final double[] lJ = lTData[j]; x[j] /= lJ[j]; final double xJ = x[j]; for (int i = j + 1; i < m; i++) { x[i] -= xJ * lJ[i]; } } // Solve LTX = Y for (int j = m - 1; j >= 0; j--) { x[j] /= lTData[j][j]; final double xJ = x[j]; for (int i = 0; i < j; i++) { x[i] -= xJ * lTData[i][j]; } } return new ArrayRealVector(x, false); } /** {@inheritDoc} */ public RealMatrix solve(RealMatrix b) { final int m = lTData.length; if (b.getRowDimension() != m) { throw new DimensionMismatchException(b.getRowDimension(), m); } final int nColB = b.getColumnDimension(); final double[][] x = b.getData(); // Solve LY = b for (int j = 0; j < m; j++) { final double[] lJ = lTData[j]; final double lJJ = lJ[j]; final double[] xJ = x[j]; for (int k = 0; k < nColB; ++k) { xJ[k] /= lJJ; } for (int i = j + 1; i < m; i++) { final double[] xI = x[i]; final double lJI = lJ[i]; for (int k = 0; k < nColB; ++k) { xI[k] -= xJ[k] * lJI; } } } // Solve LTX = Y for (int j = m - 1; j >= 0; j--) { final double lJJ = lTData[j][j]; final double[] xJ = x[j]; for (int k = 0; k < nColB; ++k) { xJ[k] /= lJJ; } for (int i = 0; i < j; i++) { final double[] xI = x[i]; final double lIJ = lTData[i][j]; for (int k = 0; k < nColB; ++k) { xI[k] -= xJ[k] * lIJ; } } } return new Array2DRowRealMatrix(x); } /** {@inheritDoc} */ public RealMatrix getInverse() { return solve(MatrixUtils.createRealIdentityMatrix(lTData.length)); } } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/linear/AbstractFieldMatrix.java100644 1750 1750 122525 12126627717 30607 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.linear; import java.util.ArrayList; import org.apache.commons.math3.Field; import org.apache.commons.math3.FieldElement; import org.apache.commons.math3.exception.DimensionMismatchException; import org.apache.commons.math3.exception.NoDataException; import org.apache.commons.math3.exception.NotPositiveException; import org.apache.commons.math3.exception.NotStrictlyPositiveException; import org.apache.commons.math3.exception.NullArgumentException; import org.apache.commons.math3.exception.NumberIsTooSmallException; import org.apache.commons.math3.exception.OutOfRangeException; import org.apache.commons.math3.exception.util.LocalizedFormats; import org.apache.commons.math3.util.MathArrays; /** * Basic implementation of {@link FieldMatrix} methods regardless of the underlying storage. *

                              All the methods implemented here use {@link #getEntry(int, int)} to access * matrix elements. Derived class can provide faster implementations.

                              * * @param Type of the field elements. * * @version $Id: AbstractFieldMatrix.java 1454876 2013-03-10 16:41:08Z luc $ * @since 2.0 */ public abstract class AbstractFieldMatrix> implements FieldMatrix { /** Field to which the elements belong. */ private final Field field; /** * Constructor for use with Serializable */ protected AbstractFieldMatrix() { field = null; } /** * Creates a matrix with no data * @param field field to which the elements belong */ protected AbstractFieldMatrix(final Field field) { this.field = field; } /** * Create a new FieldMatrix with the supplied row and column dimensions. * * @param field Field to which the elements belong. * @param rowDimension Number of rows in the new matrix. * @param columnDimension Number of columns in the new matrix. * @throws NotStrictlyPositiveException if row or column dimension is not * positive. */ protected AbstractFieldMatrix(final Field field, final int rowDimension, final int columnDimension) throws NotStrictlyPositiveException { if (rowDimension <= 0) { throw new NotStrictlyPositiveException(LocalizedFormats.DIMENSION, rowDimension); } if (columnDimension <= 0) { throw new NotStrictlyPositiveException(LocalizedFormats.DIMENSION, columnDimension); } this.field = field; } /** * Get the elements type from an array. * * @param Type of the field elements. * @param d Data array. * @return the field to which the array elements belong. * @throws NullArgumentException if the array is {@code null}. * @throws NoDataException if the array is empty. */ protected static > Field extractField(final T[][] d) throws NoDataException, NullArgumentException { if (d == null) { throw new NullArgumentException(); } if (d.length == 0) { throw new NoDataException(LocalizedFormats.AT_LEAST_ONE_ROW); } if (d[0].length == 0) { throw new NoDataException(LocalizedFormats.AT_LEAST_ONE_COLUMN); } return d[0][0].getField(); } /** * Get the elements type from an array. * * @param Type of the field elements. * @param d Data array. * @return the field to which the array elements belong. * @throws NoDataException if array is empty. */ protected static > Field extractField(final T[] d) throws NoDataException { if (d.length == 0) { throw new NoDataException(LocalizedFormats.AT_LEAST_ONE_ROW); } return d[0].getField(); } /** Build an array of elements. *

                              * Complete arrays are filled with field.getZero() *

                              * @param Type of the field elements * @param field field to which array elements belong * @param rows number of rows * @param columns number of columns (may be negative to build partial * arrays in the same way new Field[rows][] works) * @return a new array * @deprecated as of 3.2, replaced by {@link MathArrays#buildArray(Field, int, int)} */ @Deprecated protected static > T[][] buildArray(final Field field, final int rows, final int columns) { return MathArrays.buildArray(field, rows, columns); } /** Build an array of elements. *

                              * Arrays are filled with field.getZero() *

                              * @param the type of the field elements * @param field field to which array elements belong * @param length of the array * @return a new array * @deprecated as of 3.2, replaced by {@link MathArrays#buildArray(Field, int)} */ @Deprecated protected static > T[] buildArray(final Field field, final int length) { return MathArrays.buildArray(field, length); } /** {@inheritDoc} */ public Field getField() { return field; } /** {@inheritDoc} */ public abstract FieldMatrix createMatrix(final int rowDimension, final int columnDimension) throws NotStrictlyPositiveException; /** {@inheritDoc} */ public abstract FieldMatrix copy(); /** {@inheritDoc} */ public FieldMatrix add(FieldMatrix m) throws MatrixDimensionMismatchException { // safety check checkAdditionCompatible(m); final int rowCount = getRowDimension(); final int columnCount = getColumnDimension(); final FieldMatrix out = createMatrix(rowCount, columnCount); for (int row = 0; row < rowCount; ++row) { for (int col = 0; col < columnCount; ++col) { out.setEntry(row, col, getEntry(row, col).add(m.getEntry(row, col))); } } return out; } /** {@inheritDoc} */ public FieldMatrix subtract(final FieldMatrix m) throws MatrixDimensionMismatchException { // safety check checkSubtractionCompatible(m); final int rowCount = getRowDimension(); final int columnCount = getColumnDimension(); final FieldMatrix out = createMatrix(rowCount, columnCount); for (int row = 0; row < rowCount; ++row) { for (int col = 0; col < columnCount; ++col) { out.setEntry(row, col, getEntry(row, col).subtract(m.getEntry(row, col))); } } return out; } /** {@inheritDoc} */ public FieldMatrix scalarAdd(final T d) { final int rowCount = getRowDimension(); final int columnCount = getColumnDimension(); final FieldMatrix out = createMatrix(rowCount, columnCount); for (int row = 0; row < rowCount; ++row) { for (int col = 0; col < columnCount; ++col) { out.setEntry(row, col, getEntry(row, col).add(d)); } } return out; } /** {@inheritDoc} */ public FieldMatrix scalarMultiply(final T d) { final int rowCount = getRowDimension(); final int columnCount = getColumnDimension(); final FieldMatrix out = createMatrix(rowCount, columnCount); for (int row = 0; row < rowCount; ++row) { for (int col = 0; col < columnCount; ++col) { out.setEntry(row, col, getEntry(row, col).multiply(d)); } } return out; } /** {@inheritDoc} */ public FieldMatrix multiply(final FieldMatrix m) throws DimensionMismatchException { // safety check checkMultiplicationCompatible(m); final int nRows = getRowDimension(); final int nCols = m.getColumnDimension(); final int nSum = getColumnDimension(); final FieldMatrix out = createMatrix(nRows, nCols); for (int row = 0; row < nRows; ++row) { for (int col = 0; col < nCols; ++col) { T sum = field.getZero(); for (int i = 0; i < nSum; ++i) { sum = sum.add(getEntry(row, i).multiply(m.getEntry(i, col))); } out.setEntry(row, col, sum); } } return out; } /** {@inheritDoc} */ public FieldMatrix preMultiply(final FieldMatrix m) throws DimensionMismatchException { return m.multiply(this); } /** {@inheritDoc} */ public FieldMatrix power(final int p) throws NonSquareMatrixException, NotPositiveException { if (p < 0) { throw new NotPositiveException(p); } if (!isSquare()) { throw new NonSquareMatrixException(getRowDimension(), getColumnDimension()); } if (p == 0) { return MatrixUtils.createFieldIdentityMatrix(this.getField(), this.getRowDimension()); } if (p == 1) { return this.copy(); } final int power = p - 1; /* * Only log_2(p) operations is used by doing as follows: * 5^214 = 5^128 * 5^64 * 5^16 * 5^4 * 5^2 * * In general, the same approach is used for A^p. */ final char[] binaryRepresentation = Integer.toBinaryString(power) .toCharArray(); final ArrayList nonZeroPositions = new ArrayList(); for (int i = 0; i < binaryRepresentation.length; ++i) { if (binaryRepresentation[i] == '1') { final int pos = binaryRepresentation.length - i - 1; nonZeroPositions.add(pos); } } ArrayList> results = new ArrayList>( binaryRepresentation.length); results.add(0, this.copy()); for (int i = 1; i < binaryRepresentation.length; ++i) { final FieldMatrix s = results.get(i - 1); final FieldMatrix r = s.multiply(s); results.add(i, r); } FieldMatrix result = this.copy(); for (Integer i : nonZeroPositions) { result = result.multiply(results.get(i)); } return result; } /** {@inheritDoc} */ public T[][] getData() { final T[][] data = MathArrays.buildArray(field, getRowDimension(), getColumnDimension()); for (int i = 0; i < data.length; ++i) { final T[] dataI = data[i]; for (int j = 0; j < dataI.length; ++j) { dataI[j] = getEntry(i, j); } } return data; } /** {@inheritDoc} */ public FieldMatrix getSubMatrix(final int startRow, final int endRow, final int startColumn, final int endColumn) throws NumberIsTooSmallException, OutOfRangeException { checkSubMatrixIndex(startRow, endRow, startColumn, endColumn); final FieldMatrix subMatrix = createMatrix(endRow - startRow + 1, endColumn - startColumn + 1); for (int i = startRow; i <= endRow; ++i) { for (int j = startColumn; j <= endColumn; ++j) { subMatrix.setEntry(i - startRow, j - startColumn, getEntry(i, j)); } } return subMatrix; } /** {@inheritDoc} */ public FieldMatrix getSubMatrix(final int[] selectedRows, final int[] selectedColumns) throws NoDataException, NullArgumentException, OutOfRangeException { // safety checks checkSubMatrixIndex(selectedRows, selectedColumns); // copy entries final FieldMatrix subMatrix = createMatrix(selectedRows.length, selectedColumns.length); subMatrix.walkInOptimizedOrder(new DefaultFieldMatrixChangingVisitor(field.getZero()) { /** {@inheritDoc} */ @Override public T visit(final int row, final int column, final T value) { return getEntry(selectedRows[row], selectedColumns[column]); } }); return subMatrix; } /** {@inheritDoc} */ public void copySubMatrix(final int startRow, final int endRow, final int startColumn, final int endColumn, final T[][] destination) throws MatrixDimensionMismatchException, NumberIsTooSmallException, OutOfRangeException{ // safety checks checkSubMatrixIndex(startRow, endRow, startColumn, endColumn); final int rowsCount = endRow + 1 - startRow; final int columnsCount = endColumn + 1 - startColumn; if ((destination.length < rowsCount) || (destination[0].length < columnsCount)) { throw new MatrixDimensionMismatchException(destination.length, destination[0].length, rowsCount, columnsCount); } // copy entries walkInOptimizedOrder(new DefaultFieldMatrixPreservingVisitor(field.getZero()) { /** Initial row index. */ private int startRow; /** Initial column index. */ private int startColumn; /** {@inheritDoc} */ @Override public void start(final int rows, final int columns, final int startRow, final int endRow, final int startColumn, final int endColumn) { this.startRow = startRow; this.startColumn = startColumn; } /** {@inheritDoc} */ @Override public void visit(final int row, final int column, final T value) { destination[row - startRow][column - startColumn] = value; } }, startRow, endRow, startColumn, endColumn); } /** {@inheritDoc} */ public void copySubMatrix(int[] selectedRows, int[] selectedColumns, T[][] destination) throws MatrixDimensionMismatchException, NoDataException, NullArgumentException, OutOfRangeException { // safety checks checkSubMatrixIndex(selectedRows, selectedColumns); if ((destination.length < selectedRows.length) || (destination[0].length < selectedColumns.length)) { throw new MatrixDimensionMismatchException(destination.length, destination[0].length, selectedRows.length, selectedColumns.length); } // copy entries for (int i = 0; i < selectedRows.length; i++) { final T[] destinationI = destination[i]; for (int j = 0; j < selectedColumns.length; j++) { destinationI[j] = getEntry(selectedRows[i], selectedColumns[j]); } } } /** {@inheritDoc} */ public void setSubMatrix(final T[][] subMatrix, final int row, final int column) throws DimensionMismatchException, OutOfRangeException, NoDataException, NullArgumentException { if (subMatrix == null) { throw new NullArgumentException(); } final int nRows = subMatrix.length; if (nRows == 0) { throw new NoDataException(LocalizedFormats.AT_LEAST_ONE_ROW); } final int nCols = subMatrix[0].length; if (nCols == 0) { throw new NoDataException(LocalizedFormats.AT_LEAST_ONE_COLUMN); } for (int r = 1; r < nRows; ++r) { if (subMatrix[r].length != nCols) { throw new DimensionMismatchException(nCols, subMatrix[r].length); } } checkRowIndex(row); checkColumnIndex(column); checkRowIndex(nRows + row - 1); checkColumnIndex(nCols + column - 1); for (int i = 0; i < nRows; ++i) { for (int j = 0; j < nCols; ++j) { setEntry(row + i, column + j, subMatrix[i][j]); } } } /** {@inheritDoc} */ public FieldMatrix getRowMatrix(final int row) throws OutOfRangeException { checkRowIndex(row); final int nCols = getColumnDimension(); final FieldMatrix out = createMatrix(1, nCols); for (int i = 0; i < nCols; ++i) { out.setEntry(0, i, getEntry(row, i)); } return out; } /** {@inheritDoc} */ public void setRowMatrix(final int row, final FieldMatrix matrix) throws OutOfRangeException, MatrixDimensionMismatchException { checkRowIndex(row); final int nCols = getColumnDimension(); if ((matrix.getRowDimension() != 1) || (matrix.getColumnDimension() != nCols)) { throw new MatrixDimensionMismatchException(matrix.getRowDimension(), matrix.getColumnDimension(), 1, nCols); } for (int i = 0; i < nCols; ++i) { setEntry(row, i, matrix.getEntry(0, i)); } } /** {@inheritDoc} */ public FieldMatrix getColumnMatrix(final int column) throws OutOfRangeException { checkColumnIndex(column); final int nRows = getRowDimension(); final FieldMatrix out = createMatrix(nRows, 1); for (int i = 0; i < nRows; ++i) { out.setEntry(i, 0, getEntry(i, column)); } return out; } /** {@inheritDoc} */ public void setColumnMatrix(final int column, final FieldMatrix matrix) throws OutOfRangeException, MatrixDimensionMismatchException { checkColumnIndex(column); final int nRows = getRowDimension(); if ((matrix.getRowDimension() != nRows) || (matrix.getColumnDimension() != 1)) { throw new MatrixDimensionMismatchException(matrix.getRowDimension(), matrix.getColumnDimension(), nRows, 1); } for (int i = 0; i < nRows; ++i) { setEntry(i, column, matrix.getEntry(i, 0)); } } /** {@inheritDoc} */ public FieldVector getRowVector(final int row) throws OutOfRangeException { return new ArrayFieldVector(field, getRow(row), false); } /** {@inheritDoc} */ public void setRowVector(final int row, final FieldVector vector) throws OutOfRangeException, MatrixDimensionMismatchException { checkRowIndex(row); final int nCols = getColumnDimension(); if (vector.getDimension() != nCols) { throw new MatrixDimensionMismatchException(1, vector.getDimension(), 1, nCols); } for (int i = 0; i < nCols; ++i) { setEntry(row, i, vector.getEntry(i)); } } /** {@inheritDoc} */ public FieldVector getColumnVector(final int column) throws OutOfRangeException { return new ArrayFieldVector(field, getColumn(column), false); } /** {@inheritDoc} */ public void setColumnVector(final int column, final FieldVector vector) throws OutOfRangeException, MatrixDimensionMismatchException { checkColumnIndex(column); final int nRows = getRowDimension(); if (vector.getDimension() != nRows) { throw new MatrixDimensionMismatchException(vector.getDimension(), 1, nRows, 1); } for (int i = 0; i < nRows; ++i) { setEntry(i, column, vector.getEntry(i)); } } /** {@inheritDoc} */ public T[] getRow(final int row) throws OutOfRangeException { checkRowIndex(row); final int nCols = getColumnDimension(); final T[] out = MathArrays.buildArray(field, nCols); for (int i = 0; i < nCols; ++i) { out[i] = getEntry(row, i); } return out; } /** {@inheritDoc} */ public void setRow(final int row, final T[] array) throws OutOfRangeException, MatrixDimensionMismatchException { checkRowIndex(row); final int nCols = getColumnDimension(); if (array.length != nCols) { throw new MatrixDimensionMismatchException(1, array.length, 1, nCols); } for (int i = 0; i < nCols; ++i) { setEntry(row, i, array[i]); } } /** {@inheritDoc} */ public T[] getColumn(final int column) throws OutOfRangeException { checkColumnIndex(column); final int nRows = getRowDimension(); final T[] out = MathArrays.buildArray(field, nRows); for (int i = 0; i < nRows; ++i) { out[i] = getEntry(i, column); } return out; } /** {@inheritDoc} */ public void setColumn(final int column, final T[] array) throws OutOfRangeException, MatrixDimensionMismatchException { checkColumnIndex(column); final int nRows = getRowDimension(); if (array.length != nRows) { throw new MatrixDimensionMismatchException(array.length, 1, nRows, 1); } for (int i = 0; i < nRows; ++i) { setEntry(i, column, array[i]); } } /** {@inheritDoc} */ public abstract T getEntry(int row, int column) throws OutOfRangeException; /** {@inheritDoc} */ public abstract void setEntry(int row, int column, T value) throws OutOfRangeException; /** {@inheritDoc} */ public abstract void addToEntry(int row, int column, T increment) throws OutOfRangeException; /** {@inheritDoc} */ public abstract void multiplyEntry(int row, int column, T factor) throws OutOfRangeException; /** {@inheritDoc} */ public FieldMatrix transpose() { final int nRows = getRowDimension(); final int nCols = getColumnDimension(); final FieldMatrix out = createMatrix(nCols, nRows); walkInOptimizedOrder(new DefaultFieldMatrixPreservingVisitor(field.getZero()) { /** {@inheritDoc} */ @Override public void visit(final int row, final int column, final T value) { out.setEntry(column, row, value); } }); return out; } /** {@inheritDoc} */ public boolean isSquare() { return getColumnDimension() == getRowDimension(); } /** {@inheritDoc} */ public abstract int getRowDimension(); /** {@inheritDoc} */ public abstract int getColumnDimension(); /** {@inheritDoc} */ public T getTrace() throws NonSquareMatrixException { final int nRows = getRowDimension(); final int nCols = getColumnDimension(); if (nRows != nCols) { throw new NonSquareMatrixException(nRows, nCols); } T trace = field.getZero(); for (int i = 0; i < nRows; ++i) { trace = trace.add(getEntry(i, i)); } return trace; } /** {@inheritDoc} */ public T[] operate(final T[] v) throws DimensionMismatchException { final int nRows = getRowDimension(); final int nCols = getColumnDimension(); if (v.length != nCols) { throw new DimensionMismatchException(v.length, nCols); } final T[] out = MathArrays.buildArray(field, nRows); for (int row = 0; row < nRows; ++row) { T sum = field.getZero(); for (int i = 0; i < nCols; ++i) { sum = sum.add(getEntry(row, i).multiply(v[i])); } out[row] = sum; } return out; } /** {@inheritDoc} */ public FieldVector operate(final FieldVector v) throws DimensionMismatchException { try { return new ArrayFieldVector(field, operate(((ArrayFieldVector) v).getDataRef()), false); } catch (ClassCastException cce) { final int nRows = getRowDimension(); final int nCols = getColumnDimension(); if (v.getDimension() != nCols) { throw new DimensionMismatchException(v.getDimension(), nCols); } final T[] out = MathArrays.buildArray(field, nRows); for (int row = 0; row < nRows; ++row) { T sum = field.getZero(); for (int i = 0; i < nCols; ++i) { sum = sum.add(getEntry(row, i).multiply(v.getEntry(i))); } out[row] = sum; } return new ArrayFieldVector(field, out, false); } } /** {@inheritDoc} */ public T[] preMultiply(final T[] v) throws DimensionMismatchException { final int nRows = getRowDimension(); final int nCols = getColumnDimension(); if (v.length != nRows) { throw new DimensionMismatchException(v.length, nRows); } final T[] out = MathArrays.buildArray(field, nCols); for (int col = 0; col < nCols; ++col) { T sum = field.getZero(); for (int i = 0; i < nRows; ++i) { sum = sum.add(getEntry(i, col).multiply(v[i])); } out[col] = sum; } return out; } /** {@inheritDoc} */ public FieldVector preMultiply(final FieldVector v) throws DimensionMismatchException { try { return new ArrayFieldVector(field, preMultiply(((ArrayFieldVector) v).getDataRef()), false); } catch (ClassCastException cce) { final int nRows = getRowDimension(); final int nCols = getColumnDimension(); if (v.getDimension() != nRows) { throw new DimensionMismatchException(v.getDimension(), nRows); } final T[] out = MathArrays.buildArray(field, nCols); for (int col = 0; col < nCols; ++col) { T sum = field.getZero(); for (int i = 0; i < nRows; ++i) { sum = sum.add(getEntry(i, col).multiply(v.getEntry(i))); } out[col] = sum; } return new ArrayFieldVector(field, out, false); } } /** {@inheritDoc} */ public T walkInRowOrder(final FieldMatrixChangingVisitor visitor) { final int rows = getRowDimension(); final int columns = getColumnDimension(); visitor.start(rows, columns, 0, rows - 1, 0, columns - 1); for (int row = 0; row < rows; ++row) { for (int column = 0; column < columns; ++column) { final T oldValue = getEntry(row, column); final T newValue = visitor.visit(row, column, oldValue); setEntry(row, column, newValue); } } return visitor.end(); } /** {@inheritDoc} */ public T walkInRowOrder(final FieldMatrixPreservingVisitor visitor) { final int rows = getRowDimension(); final int columns = getColumnDimension(); visitor.start(rows, columns, 0, rows - 1, 0, columns - 1); for (int row = 0; row < rows; ++row) { for (int column = 0; column < columns; ++column) { visitor.visit(row, column, getEntry(row, column)); } } return visitor.end(); } /** {@inheritDoc} */ public T walkInRowOrder(final FieldMatrixChangingVisitor visitor, final int startRow, final int endRow, final int startColumn, final int endColumn) throws NumberIsTooSmallException, OutOfRangeException { checkSubMatrixIndex(startRow, endRow, startColumn, endColumn); visitor.start(getRowDimension(), getColumnDimension(), startRow, endRow, startColumn, endColumn); for (int row = startRow; row <= endRow; ++row) { for (int column = startColumn; column <= endColumn; ++column) { final T oldValue = getEntry(row, column); final T newValue = visitor.visit(row, column, oldValue); setEntry(row, column, newValue); } } return visitor.end(); } /** {@inheritDoc} */ public T walkInRowOrder(final FieldMatrixPreservingVisitor visitor, final int startRow, final int endRow, final int startColumn, final int endColumn) throws NumberIsTooSmallException, OutOfRangeException { checkSubMatrixIndex(startRow, endRow, startColumn, endColumn); visitor.start(getRowDimension(), getColumnDimension(), startRow, endRow, startColumn, endColumn); for (int row = startRow; row <= endRow; ++row) { for (int column = startColumn; column <= endColumn; ++column) { visitor.visit(row, column, getEntry(row, column)); } } return visitor.end(); } /** {@inheritDoc} */ public T walkInColumnOrder(final FieldMatrixChangingVisitor visitor) { final int rows = getRowDimension(); final int columns = getColumnDimension(); visitor.start(rows, columns, 0, rows - 1, 0, columns - 1); for (int column = 0; column < columns; ++column) { for (int row = 0; row < rows; ++row) { final T oldValue = getEntry(row, column); final T newValue = visitor.visit(row, column, oldValue); setEntry(row, column, newValue); } } return visitor.end(); } /** {@inheritDoc} */ public T walkInColumnOrder(final FieldMatrixPreservingVisitor visitor) { final int rows = getRowDimension(); final int columns = getColumnDimension(); visitor.start(rows, columns, 0, rows - 1, 0, columns - 1); for (int column = 0; column < columns; ++column) { for (int row = 0; row < rows; ++row) { visitor.visit(row, column, getEntry(row, column)); } } return visitor.end(); } /** {@inheritDoc} */ public T walkInColumnOrder(final FieldMatrixChangingVisitor visitor, final int startRow, final int endRow, final int startColumn, final int endColumn) throws NumberIsTooSmallException, OutOfRangeException { checkSubMatrixIndex(startRow, endRow, startColumn, endColumn); visitor.start(getRowDimension(), getColumnDimension(), startRow, endRow, startColumn, endColumn); for (int column = startColumn; column <= endColumn; ++column) { for (int row = startRow; row <= endRow; ++row) { final T oldValue = getEntry(row, column); final T newValue = visitor.visit(row, column, oldValue); setEntry(row, column, newValue); } } return visitor.end(); } /** {@inheritDoc} */ public T walkInColumnOrder(final FieldMatrixPreservingVisitor visitor, final int startRow, final int endRow, final int startColumn, final int endColumn) throws NumberIsTooSmallException, OutOfRangeException{ checkSubMatrixIndex(startRow, endRow, startColumn, endColumn); visitor.start(getRowDimension(), getColumnDimension(), startRow, endRow, startColumn, endColumn); for (int column = startColumn; column <= endColumn; ++column) { for (int row = startRow; row <= endRow; ++row) { visitor.visit(row, column, getEntry(row, column)); } } return visitor.end(); } /** {@inheritDoc} */ public T walkInOptimizedOrder(final FieldMatrixChangingVisitor visitor) { return walkInRowOrder(visitor); } /** {@inheritDoc} */ public T walkInOptimizedOrder(final FieldMatrixPreservingVisitor visitor) { return walkInRowOrder(visitor); } /** {@inheritDoc} */ public T walkInOptimizedOrder(final FieldMatrixChangingVisitor visitor, final int startRow, final int endRow, final int startColumn, final int endColumn) throws NumberIsTooSmallException, OutOfRangeException { return walkInRowOrder(visitor, startRow, endRow, startColumn, endColumn); } /** {@inheritDoc} */ public T walkInOptimizedOrder(final FieldMatrixPreservingVisitor visitor, final int startRow, final int endRow, final int startColumn, final int endColumn) throws NumberIsTooSmallException, OutOfRangeException { return walkInRowOrder(visitor, startRow, endRow, startColumn, endColumn); } /** * Get a string representation for this matrix. * @return a string representation for this matrix */ @Override public String toString() { final int nRows = getRowDimension(); final int nCols = getColumnDimension(); final StringBuffer res = new StringBuffer(); String fullClassName = getClass().getName(); String shortClassName = fullClassName.substring(fullClassName.lastIndexOf('.') + 1); res.append(shortClassName).append("{"); for (int i = 0; i < nRows; ++i) { if (i > 0) { res.append(","); } res.append("{"); for (int j = 0; j < nCols; ++j) { if (j > 0) { res.append(","); } res.append(getEntry(i, j)); } res.append("}"); } res.append("}"); return res.toString(); } /** * Returns true iff object is a * FieldMatrix instance with the same dimensions as this * and all corresponding matrix entries are equal. * * @param object the object to test equality against. * @return true if object equals this */ @Override public boolean equals(final Object object) { if (object == this ) { return true; } if (object instanceof FieldMatrix == false) { return false; } FieldMatrix m = (FieldMatrix) object; final int nRows = getRowDimension(); final int nCols = getColumnDimension(); if (m.getColumnDimension() != nCols || m.getRowDimension() != nRows) { return false; } for (int row = 0; row < nRows; ++row) { for (int col = 0; col < nCols; ++col) { if (!getEntry(row, col).equals(m.getEntry(row, col))) { return false; } } } return true; } /** * Computes a hashcode for the matrix. * * @return hashcode for matrix */ @Override public int hashCode() { int ret = 322562; final int nRows = getRowDimension(); final int nCols = getColumnDimension(); ret = ret * 31 + nRows; ret = ret * 31 + nCols; for (int row = 0; row < nRows; ++row) { for (int col = 0; col < nCols; ++col) { ret = ret * 31 + (11 * (row+1) + 17 * (col+1)) * getEntry(row, col).hashCode(); } } return ret; } /** * Check if a row index is valid. * * @param row Row index to check. * @throws OutOfRangeException if {@code index} is not valid. */ protected void checkRowIndex(final int row) throws OutOfRangeException { if (row < 0 || row >= getRowDimension()) { throw new OutOfRangeException(LocalizedFormats.ROW_INDEX, row, 0, getRowDimension() - 1); } } /** * Check if a column index is valid. * * @param column Column index to check. * @throws OutOfRangeException if {@code index} is not valid. */ protected void checkColumnIndex(final int column) throws OutOfRangeException { if (column < 0 || column >= getColumnDimension()) { throw new OutOfRangeException(LocalizedFormats.COLUMN_INDEX, column, 0, getColumnDimension() - 1); } } /** * Check if submatrix ranges indices are valid. * Rows and columns are indicated counting from 0 to n-1. * * @param startRow Initial row index. * @param endRow Final row index. * @param startColumn Initial column index. * @param endColumn Final column index. * @throws OutOfRangeException if the indices are not valid. * @throws NumberIsTooSmallException if {@code endRow < startRow} or * {@code endColumn < startColumn}. */ protected void checkSubMatrixIndex(final int startRow, final int endRow, final int startColumn, final int endColumn) throws NumberIsTooSmallException, OutOfRangeException { checkRowIndex(startRow); checkRowIndex(endRow); if (endRow < startRow) { throw new NumberIsTooSmallException(LocalizedFormats.INITIAL_ROW_AFTER_FINAL_ROW, endRow, startRow, true); } checkColumnIndex(startColumn); checkColumnIndex(endColumn); if (endColumn < startColumn) { throw new NumberIsTooSmallException(LocalizedFormats.INITIAL_COLUMN_AFTER_FINAL_COLUMN, endColumn, startColumn, true); } } /** * Check if submatrix ranges indices are valid. * Rows and columns are indicated counting from 0 to n-1. * * @param selectedRows Array of row indices. * @param selectedColumns Array of column indices. * @throws NullArgumentException if the arrays are {@code null}. * @throws NoDataException if the arrays have zero length. * @throws OutOfRangeException if row or column selections are not valid. */ protected void checkSubMatrixIndex(final int[] selectedRows, final int[] selectedColumns) throws NoDataException, NullArgumentException, OutOfRangeException { if (selectedRows == null || selectedColumns == null) { throw new NullArgumentException(); } if (selectedRows.length == 0 || selectedColumns.length == 0) { throw new NoDataException(); } for (final int row : selectedRows) { checkRowIndex(row); } for (final int column : selectedColumns) { checkColumnIndex(column); } } /** * Check if a matrix is addition compatible with the instance. * * @param m Matrix to check. * @throws MatrixDimensionMismatchException if the matrix is not * addition-compatible with instance. */ protected void checkAdditionCompatible(final FieldMatrix m) throws MatrixDimensionMismatchException { if ((getRowDimension() != m.getRowDimension()) || (getColumnDimension() != m.getColumnDimension())) { throw new MatrixDimensionMismatchException(m.getRowDimension(), m.getColumnDimension(), getRowDimension(), getColumnDimension()); } } /** * Check if a matrix is subtraction compatible with the instance. * * @param m Matrix to check. * @throws MatrixDimensionMismatchException if the matrix is not * subtraction-compatible with instance. */ protected void checkSubtractionCompatible(final FieldMatrix m) throws MatrixDimensionMismatchException { if ((getRowDimension() != m.getRowDimension()) || (getColumnDimension() != m.getColumnDimension())) { throw new MatrixDimensionMismatchException(m.getRowDimension(), m.getColumnDimension(), getRowDimension(), getColumnDimension()); } } /** * Check if a matrix is multiplication compatible with the instance. * * @param m Matrix to check. * @throws DimensionMismatchException if the matrix is not * multiplication-compatible with instance. */ protected void checkMultiplicationCompatible(final FieldMatrix m) throws DimensionMismatchException { if (getColumnDimension() != m.getRowDimension()) { throw new DimensionMismatchException(m.getRowDimension(), getColumnDimension()); } } } ././@LongLink100644 0 0 151 12126630647 10257 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/linear/NonSelfAdjointOperatorException.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/linear/NonSelfAdjointOperatorException.100644 1750 1750 3764 12126627717 32264 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.linear; import org.apache.commons.math3.exception.MathIllegalArgumentException; import org.apache.commons.math3.exception.util.LocalizedFormats; /** * Exception to be thrown when a self-adjoint {@link RealLinearOperator} * is expected. * Since the coefficients of the matrix are not accessible, the most * general definition is used to check that A is not self-adjoint, i.e. * there exist x and y such as {@code | x' A y - y' A x | >= eps}, * where {@code eps} is a user-specified tolerance, and {@code x'} * denotes the transpose of {@code x}. * In the terminology of this exception, {@code A} is the "offending" * linear operator, {@code x} and {@code y} are the first and second * "offending" vectors, respectively. * * @version $Id: NonSelfAdjointOperatorException.java 1416643 2012-12-03 19:37:14Z tn $ * @since 3.0 */ public class NonSelfAdjointOperatorException extends MathIllegalArgumentException { /** Serializable version Id. */ private static final long serialVersionUID = 1784999305030258247L; /** Creates a new instance of this class. */ public NonSelfAdjointOperatorException() { super(LocalizedFormats.NON_SELF_ADJOINT_OPERATOR); } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/linear/OpenMapRealMatrix.java100644 1750 1750 25470 12126627717 30224 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.linear; import java.io.Serializable; import org.apache.commons.math3.exception.DimensionMismatchException; import org.apache.commons.math3.exception.NotStrictlyPositiveException; import org.apache.commons.math3.exception.NumberIsTooLargeException; import org.apache.commons.math3.exception.OutOfRangeException; import org.apache.commons.math3.util.OpenIntToDoubleHashMap; /** * Sparse matrix implementation based on an open addressed map. * * @version $Id: OpenMapRealMatrix.java 1416643 2012-12-03 19:37:14Z tn $ * @since 2.0 * @deprecated As of version 3.1, this class is deprecated, for reasons exposed * in this JIRA * ticket. This * class will be removed in version 4.0. * */ @Deprecated public class OpenMapRealMatrix extends AbstractRealMatrix implements SparseRealMatrix, Serializable { /** Serializable version identifier. */ private static final long serialVersionUID = -5962461716457143437L; /** Number of rows of the matrix. */ private final int rows; /** Number of columns of the matrix. */ private final int columns; /** Storage for (sparse) matrix elements. */ private final OpenIntToDoubleHashMap entries; /** * Build a sparse matrix with the supplied row and column dimensions. * * @param rowDimension Number of rows of the matrix. * @param columnDimension Number of columns of the matrix. * @throws NotStrictlyPositiveException if row or column dimension is not * positive. * @throws NumberIsTooLargeException if the total number of entries of the * matrix is larger than {@code Integer.MAX_VALUE}. */ public OpenMapRealMatrix(int rowDimension, int columnDimension) throws NotStrictlyPositiveException, NumberIsTooLargeException { super(rowDimension, columnDimension); long lRow = rowDimension; long lCol = columnDimension; if (lRow * lCol >= Integer.MAX_VALUE) { throw new NumberIsTooLargeException(lRow * lCol, Integer.MAX_VALUE, false); } this.rows = rowDimension; this.columns = columnDimension; this.entries = new OpenIntToDoubleHashMap(0.0); } /** * Build a matrix by copying another one. * * @param matrix matrix to copy. */ public OpenMapRealMatrix(OpenMapRealMatrix matrix) { this.rows = matrix.rows; this.columns = matrix.columns; this.entries = new OpenIntToDoubleHashMap(matrix.entries); } /** {@inheritDoc} */ @Override public OpenMapRealMatrix copy() { return new OpenMapRealMatrix(this); } /** * {@inheritDoc} * * @throws NumberIsTooLargeException if the total number of entries of the * matrix is larger than {@code Integer.MAX_VALUE}. */ @Override public OpenMapRealMatrix createMatrix(int rowDimension, int columnDimension) throws NotStrictlyPositiveException, NumberIsTooLargeException { return new OpenMapRealMatrix(rowDimension, columnDimension); } /** {@inheritDoc} */ @Override public int getColumnDimension() { return columns; } /** * Compute the sum of this matrix and {@code m}. * * @param m Matrix to be added. * @return {@code this} + {@code m}. * @throws MatrixDimensionMismatchException if {@code m} is not the same * size as {@code this}. */ public OpenMapRealMatrix add(OpenMapRealMatrix m) throws MatrixDimensionMismatchException { MatrixUtils.checkAdditionCompatible(this, m); final OpenMapRealMatrix out = new OpenMapRealMatrix(this); for (OpenIntToDoubleHashMap.Iterator iterator = m.entries.iterator(); iterator.hasNext();) { iterator.advance(); final int row = iterator.key() / columns; final int col = iterator.key() - row * columns; out.setEntry(row, col, getEntry(row, col) + iterator.value()); } return out; } /** {@inheritDoc} */ @Override public OpenMapRealMatrix subtract(final RealMatrix m) throws MatrixDimensionMismatchException { try { return subtract((OpenMapRealMatrix) m); } catch (ClassCastException cce) { return (OpenMapRealMatrix) super.subtract(m); } } /** * Subtract {@code m} from this matrix. * * @param m Matrix to be subtracted. * @return {@code this} - {@code m}. * @throws MatrixDimensionMismatchException if {@code m} is not the same * size as {@code this}. */ public OpenMapRealMatrix subtract(OpenMapRealMatrix m) throws MatrixDimensionMismatchException { MatrixUtils.checkAdditionCompatible(this, m); final OpenMapRealMatrix out = new OpenMapRealMatrix(this); for (OpenIntToDoubleHashMap.Iterator iterator = m.entries.iterator(); iterator.hasNext();) { iterator.advance(); final int row = iterator.key() / columns; final int col = iterator.key() - row * columns; out.setEntry(row, col, getEntry(row, col) - iterator.value()); } return out; } /** * {@inheritDoc} * * @throws NumberIsTooLargeException if {@code m} is an * {@code OpenMapRealMatrix}, and the total number of entries of the product * is larger than {@code Integer.MAX_VALUE}. */ @Override public RealMatrix multiply(final RealMatrix m) throws DimensionMismatchException, NumberIsTooLargeException { try { return multiply((OpenMapRealMatrix) m); } catch (ClassCastException cce) { MatrixUtils.checkMultiplicationCompatible(this, m); final int outCols = m.getColumnDimension(); final BlockRealMatrix out = new BlockRealMatrix(rows, outCols); for (OpenIntToDoubleHashMap.Iterator iterator = entries.iterator(); iterator.hasNext();) { iterator.advance(); final double value = iterator.value(); final int key = iterator.key(); final int i = key / columns; final int k = key % columns; for (int j = 0; j < outCols; ++j) { out.addToEntry(i, j, value * m.getEntry(k, j)); } } return out; } } /** * Postmultiply this matrix by {@code m}. * * @param m Matrix to postmultiply by. * @return {@code this} * {@code m}. * @throws DimensionMismatchException if the number of rows of {@code m} * differ from the number of columns of {@code this} matrix. * @throws NumberIsTooLargeException if the total number of entries of the * product is larger than {@code Integer.MAX_VALUE}. */ public OpenMapRealMatrix multiply(OpenMapRealMatrix m) throws DimensionMismatchException, NumberIsTooLargeException { // Safety check. MatrixUtils.checkMultiplicationCompatible(this, m); final int outCols = m.getColumnDimension(); OpenMapRealMatrix out = new OpenMapRealMatrix(rows, outCols); for (OpenIntToDoubleHashMap.Iterator iterator = entries.iterator(); iterator.hasNext();) { iterator.advance(); final double value = iterator.value(); final int key = iterator.key(); final int i = key / columns; final int k = key % columns; for (int j = 0; j < outCols; ++j) { final int rightKey = m.computeKey(k, j); if (m.entries.containsKey(rightKey)) { final int outKey = out.computeKey(i, j); final double outValue = out.entries.get(outKey) + value * m.entries.get(rightKey); if (outValue == 0.0) { out.entries.remove(outKey); } else { out.entries.put(outKey, outValue); } } } } return out; } /** {@inheritDoc} */ @Override public double getEntry(int row, int column) throws OutOfRangeException { MatrixUtils.checkRowIndex(this, row); MatrixUtils.checkColumnIndex(this, column); return entries.get(computeKey(row, column)); } /** {@inheritDoc} */ @Override public int getRowDimension() { return rows; } /** {@inheritDoc} */ @Override public void setEntry(int row, int column, double value) throws OutOfRangeException { MatrixUtils.checkRowIndex(this, row); MatrixUtils.checkColumnIndex(this, column); if (value == 0.0) { entries.remove(computeKey(row, column)); } else { entries.put(computeKey(row, column), value); } } /** {@inheritDoc} */ @Override public void addToEntry(int row, int column, double increment) throws OutOfRangeException { MatrixUtils.checkRowIndex(this, row); MatrixUtils.checkColumnIndex(this, column); final int key = computeKey(row, column); final double value = entries.get(key) + increment; if (value == 0.0) { entries.remove(key); } else { entries.put(key, value); } } /** {@inheritDoc} */ @Override public void multiplyEntry(int row, int column, double factor) throws OutOfRangeException { MatrixUtils.checkRowIndex(this, row); MatrixUtils.checkColumnIndex(this, column); final int key = computeKey(row, column); final double value = entries.get(key) * factor; if (value == 0.0) { entries.remove(key); } else { entries.put(key, value); } } /** * Compute the key to access a matrix element * @param row row index of the matrix element * @param column column index of the matrix element * @return key within the map to access the matrix element */ private int computeKey(int row, int column) { return row * columns + column; } } ././@LongLink100644 0 0 153 12126630647 10261 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/linear/DefaultFieldMatrixChangingVisitor.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/linear/DefaultFieldMatrixChangingVisito100644 1750 1750 3725 12126627717 32305 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.linear; import org.apache.commons.math3.FieldElement; /** * Default implementation of the {@link FieldMatrixChangingVisitor} interface. *

                              * This class is a convenience to create custom visitors without defining all * methods. This class provides default implementations that do nothing. *

                              * * @param the type of the field elements * @version $Id: DefaultFieldMatrixChangingVisitor.java 1416643 2012-12-03 19:37:14Z tn $ * @since 2.0 */ public class DefaultFieldMatrixChangingVisitor> implements FieldMatrixChangingVisitor { /** Zero element of the field. */ private final T zero; /** Build a new instance. * @param zero additive identity of the field */ public DefaultFieldMatrixChangingVisitor(final T zero) { this.zero = zero; } /** {@inheritDoc} */ public void start(int rows, int columns, int startRow, int endRow, int startColumn, int endColumn) { } /** {@inheritDoc} */ public T visit(int row, int column, T value) { return value; } /** {@inheritDoc} */ public T end() { return zero; } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/linear/BiDiagonalTransformer.java100644 1750 1750 32177 12126627717 31112 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.linear; import org.apache.commons.math3.util.FastMath; /** * Class transforming any matrix to bi-diagonal shape. *

                              Any m × n matrix A can be written as the product of three matrices: * A = U × B × VT with U an m × m orthogonal matrix, * B an m × n bi-diagonal matrix (lower diagonal if m < n, upper diagonal * otherwise), and V an n × n orthogonal matrix.

                              *

                              Transformation to bi-diagonal shape is often not a goal by itself, but it is * an intermediate step in more general decomposition algorithms like {@link * SingularValueDecomposition Singular Value Decomposition}. This class is therefore * intended for internal use by the library and is not public. As a consequence of * this explicitly limited scope, many methods directly returns references to * internal arrays, not copies.

                              * @version $Id: BiDiagonalTransformer.java 1416643 2012-12-03 19:37:14Z tn $ * @since 2.0 */ class BiDiagonalTransformer { /** Householder vectors. */ private final double householderVectors[][]; /** Main diagonal. */ private final double[] main; /** Secondary diagonal. */ private final double[] secondary; /** Cached value of U. */ private RealMatrix cachedU; /** Cached value of B. */ private RealMatrix cachedB; /** Cached value of V. */ private RealMatrix cachedV; /** * Build the transformation to bi-diagonal shape of a matrix. * @param matrix the matrix to transform. */ public BiDiagonalTransformer(RealMatrix matrix) { final int m = matrix.getRowDimension(); final int n = matrix.getColumnDimension(); final int p = FastMath.min(m, n); householderVectors = matrix.getData(); main = new double[p]; secondary = new double[p - 1]; cachedU = null; cachedB = null; cachedV = null; // transform matrix if (m >= n) { transformToUpperBiDiagonal(); } else { transformToLowerBiDiagonal(); } } /** * Returns the matrix U of the transform. *

                              U is an orthogonal matrix, i.e. its transpose is also its inverse.

                              * @return the U matrix */ public RealMatrix getU() { if (cachedU == null) { final int m = householderVectors.length; final int n = householderVectors[0].length; final int p = main.length; final int diagOffset = (m >= n) ? 0 : 1; final double[] diagonal = (m >= n) ? main : secondary; double[][] ua = new double[m][m]; // fill up the part of the matrix not affected by Householder transforms for (int k = m - 1; k >= p; --k) { ua[k][k] = 1; } // build up first part of the matrix by applying Householder transforms for (int k = p - 1; k >= diagOffset; --k) { final double[] hK = householderVectors[k]; ua[k][k] = 1; if (hK[k - diagOffset] != 0.0) { for (int j = k; j < m; ++j) { double alpha = 0; for (int i = k; i < m; ++i) { alpha -= ua[i][j] * householderVectors[i][k - diagOffset]; } alpha /= diagonal[k - diagOffset] * hK[k - diagOffset]; for (int i = k; i < m; ++i) { ua[i][j] += -alpha * householderVectors[i][k - diagOffset]; } } } } if (diagOffset > 0) { ua[0][0] = 1; } cachedU = MatrixUtils.createRealMatrix(ua); } // return the cached matrix return cachedU; } /** * Returns the bi-diagonal matrix B of the transform. * @return the B matrix */ public RealMatrix getB() { if (cachedB == null) { final int m = householderVectors.length; final int n = householderVectors[0].length; double[][] ba = new double[m][n]; for (int i = 0; i < main.length; ++i) { ba[i][i] = main[i]; if (m < n) { if (i > 0) { ba[i][i-1] = secondary[i - 1]; } } else { if (i < main.length - 1) { ba[i][i+1] = secondary[i]; } } } cachedB = MatrixUtils.createRealMatrix(ba); } // return the cached matrix return cachedB; } /** * Returns the matrix V of the transform. *

                              V is an orthogonal matrix, i.e. its transpose is also its inverse.

                              * @return the V matrix */ public RealMatrix getV() { if (cachedV == null) { final int m = householderVectors.length; final int n = householderVectors[0].length; final int p = main.length; final int diagOffset = (m >= n) ? 1 : 0; final double[] diagonal = (m >= n) ? secondary : main; double[][] va = new double[n][n]; // fill up the part of the matrix not affected by Householder transforms for (int k = n - 1; k >= p; --k) { va[k][k] = 1; } // build up first part of the matrix by applying Householder transforms for (int k = p - 1; k >= diagOffset; --k) { final double[] hK = householderVectors[k - diagOffset]; va[k][k] = 1; if (hK[k] != 0.0) { for (int j = k; j < n; ++j) { double beta = 0; for (int i = k; i < n; ++i) { beta -= va[i][j] * hK[i]; } beta /= diagonal[k - diagOffset] * hK[k]; for (int i = k; i < n; ++i) { va[i][j] += -beta * hK[i]; } } } } if (diagOffset > 0) { va[0][0] = 1; } cachedV = MatrixUtils.createRealMatrix(va); } // return the cached matrix return cachedV; } /** * Get the Householder vectors of the transform. *

                              Note that since this class is only intended for internal use, * it returns directly a reference to its internal arrays, not a copy.

                              * @return the main diagonal elements of the B matrix */ double[][] getHouseholderVectorsRef() { return householderVectors; } /** * Get the main diagonal elements of the matrix B of the transform. *

                              Note that since this class is only intended for internal use, * it returns directly a reference to its internal arrays, not a copy.

                              * @return the main diagonal elements of the B matrix */ double[] getMainDiagonalRef() { return main; } /** * Get the secondary diagonal elements of the matrix B of the transform. *

                              Note that since this class is only intended for internal use, * it returns directly a reference to its internal arrays, not a copy.

                              * @return the secondary diagonal elements of the B matrix */ double[] getSecondaryDiagonalRef() { return secondary; } /** * Check if the matrix is transformed to upper bi-diagonal. * @return true if the matrix is transformed to upper bi-diagonal */ boolean isUpperBiDiagonal() { return householderVectors.length >= householderVectors[0].length; } /** * Transform original matrix to upper bi-diagonal form. *

                              Transformation is done using alternate Householder transforms * on columns and rows.

                              */ private void transformToUpperBiDiagonal() { final int m = householderVectors.length; final int n = householderVectors[0].length; for (int k = 0; k < n; k++) { //zero-out a column double xNormSqr = 0; for (int i = k; i < m; ++i) { final double c = householderVectors[i][k]; xNormSqr += c * c; } final double[] hK = householderVectors[k]; final double a = (hK[k] > 0) ? -FastMath.sqrt(xNormSqr) : FastMath.sqrt(xNormSqr); main[k] = a; if (a != 0.0) { hK[k] -= a; for (int j = k + 1; j < n; ++j) { double alpha = 0; for (int i = k; i < m; ++i) { final double[] hI = householderVectors[i]; alpha -= hI[j] * hI[k]; } alpha /= a * householderVectors[k][k]; for (int i = k; i < m; ++i) { final double[] hI = householderVectors[i]; hI[j] -= alpha * hI[k]; } } } if (k < n - 1) { //zero-out a row xNormSqr = 0; for (int j = k + 1; j < n; ++j) { final double c = hK[j]; xNormSqr += c * c; } final double b = (hK[k + 1] > 0) ? -FastMath.sqrt(xNormSqr) : FastMath.sqrt(xNormSqr); secondary[k] = b; if (b != 0.0) { hK[k + 1] -= b; for (int i = k + 1; i < m; ++i) { final double[] hI = householderVectors[i]; double beta = 0; for (int j = k + 1; j < n; ++j) { beta -= hI[j] * hK[j]; } beta /= b * hK[k + 1]; for (int j = k + 1; j < n; ++j) { hI[j] -= beta * hK[j]; } } } } } } /** * Transform original matrix to lower bi-diagonal form. *

                              Transformation is done using alternate Householder transforms * on rows and columns.

                              */ private void transformToLowerBiDiagonal() { final int m = householderVectors.length; final int n = householderVectors[0].length; for (int k = 0; k < m; k++) { //zero-out a row final double[] hK = householderVectors[k]; double xNormSqr = 0; for (int j = k; j < n; ++j) { final double c = hK[j]; xNormSqr += c * c; } final double a = (hK[k] > 0) ? -FastMath.sqrt(xNormSqr) : FastMath.sqrt(xNormSqr); main[k] = a; if (a != 0.0) { hK[k] -= a; for (int i = k + 1; i < m; ++i) { final double[] hI = householderVectors[i]; double alpha = 0; for (int j = k; j < n; ++j) { alpha -= hI[j] * hK[j]; } alpha /= a * householderVectors[k][k]; for (int j = k; j < n; ++j) { hI[j] -= alpha * hK[j]; } } } if (k < m - 1) { //zero-out a column final double[] hKp1 = householderVectors[k + 1]; xNormSqr = 0; for (int i = k + 1; i < m; ++i) { final double c = householderVectors[i][k]; xNormSqr += c * c; } final double b = (hKp1[k] > 0) ? -FastMath.sqrt(xNormSqr) : FastMath.sqrt(xNormSqr); secondary[k] = b; if (b != 0.0) { hKp1[k] -= b; for (int j = k + 1; j < n; ++j) { double beta = 0; for (int i = k + 1; i < m; ++i) { final double[] hI = householderVectors[i]; beta -= hI[j] * hI[k]; } beta /= b * hKp1[k]; for (int i = k + 1; i < m; ++i) { final double[] hI = householderVectors[i]; hI[j] -= beta * hI[k]; } } } } } } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/linear/RealVectorFormat.java100644 1750 1750 23621 12126627717 30107 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.linear; import java.text.FieldPosition; import java.text.NumberFormat; import java.text.ParsePosition; import java.util.ArrayList; import java.util.List; import java.util.Locale; import org.apache.commons.math3.exception.MathParseException; import org.apache.commons.math3.util.CompositeFormat; /** * Formats a vector in components list format "{v0; v1; ...; vk-1}". *

                              The prefix and suffix "{" and "}" and the separator "; " can be replaced by * any user-defined strings. The number format for components can be configured.

                              *

                              White space is ignored at parse time, even if it is in the prefix, suffix * or separator specifications. So even if the default separator does include a space * character that is used at format time, both input string "{1;1;1}" and * " { 1 ; 1 ; 1 } " will be parsed without error and the same vector will be * returned. In the second case, however, the parse position after parsing will be * just after the closing curly brace, i.e. just before the trailing space.

                              * * @version $Id: RealVectorFormat.java 1416643 2012-12-03 19:37:14Z tn $ * @since 2.0 */ public class RealVectorFormat { /** The default prefix: "{". */ private static final String DEFAULT_PREFIX = "{"; /** The default suffix: "}". */ private static final String DEFAULT_SUFFIX = "}"; /** The default separator: ", ". */ private static final String DEFAULT_SEPARATOR = "; "; /** Prefix. */ private final String prefix; /** Suffix. */ private final String suffix; /** Separator. */ private final String separator; /** Trimmed prefix. */ private final String trimmedPrefix; /** Trimmed suffix. */ private final String trimmedSuffix; /** Trimmed separator. */ private final String trimmedSeparator; /** The format used for components. */ private final NumberFormat format; /** * Create an instance with default settings. *

                              The instance uses the default prefix, suffix and separator: * "{", "}", and "; " and the default number format for components.

                              */ public RealVectorFormat() { this(DEFAULT_PREFIX, DEFAULT_SUFFIX, DEFAULT_SEPARATOR, CompositeFormat.getDefaultNumberFormat()); } /** * Create an instance with a custom number format for components. * @param format the custom format for components. */ public RealVectorFormat(final NumberFormat format) { this(DEFAULT_PREFIX, DEFAULT_SUFFIX, DEFAULT_SEPARATOR, format); } /** * Create an instance with custom prefix, suffix and separator. * @param prefix prefix to use instead of the default "{" * @param suffix suffix to use instead of the default "}" * @param separator separator to use instead of the default "; " */ public RealVectorFormat(final String prefix, final String suffix, final String separator) { this(prefix, suffix, separator, CompositeFormat.getDefaultNumberFormat()); } /** * Create an instance with custom prefix, suffix, separator and format * for components. * @param prefix prefix to use instead of the default "{" * @param suffix suffix to use instead of the default "}" * @param separator separator to use instead of the default "; " * @param format the custom format for components. */ public RealVectorFormat(final String prefix, final String suffix, final String separator, final NumberFormat format) { this.prefix = prefix; this.suffix = suffix; this.separator = separator; trimmedPrefix = prefix.trim(); trimmedSuffix = suffix.trim(); trimmedSeparator = separator.trim(); this.format = format; } /** * Get the set of locales for which real vectors formats are available. *

                              This is the same set as the {@link NumberFormat} set.

                              * @return available real vector format locales. */ public static Locale[] getAvailableLocales() { return NumberFormat.getAvailableLocales(); } /** * Get the format prefix. * @return format prefix. */ public String getPrefix() { return prefix; } /** * Get the format suffix. * @return format suffix. */ public String getSuffix() { return suffix; } /** * Get the format separator between components. * @return format separator. */ public String getSeparator() { return separator; } /** * Get the components format. * @return components format. */ public NumberFormat getFormat() { return format; } /** * Returns the default real vector format for the current locale. * @return the default real vector format. */ public static RealVectorFormat getInstance() { return getInstance(Locale.getDefault()); } /** * Returns the default real vector format for the given locale. * @param locale the specific locale used by the format. * @return the real vector format specific to the given locale. */ public static RealVectorFormat getInstance(final Locale locale) { return new RealVectorFormat(CompositeFormat.getDefaultNumberFormat(locale)); } /** * This method calls {@link #format(RealVector,StringBuffer,FieldPosition)}. * * @param v RealVector object to format. * @return a formatted vector. */ public String format(RealVector v) { return format(v, new StringBuffer(), new FieldPosition(0)).toString(); } /** * Formats a {@link RealVector} object to produce a string. * @param vector the object to format. * @param toAppendTo where the text is to be appended * @param pos On input: an alignment field, if desired. On output: the * offsets of the alignment field * @return the value passed in as toAppendTo. */ public StringBuffer format(RealVector vector, StringBuffer toAppendTo, FieldPosition pos) { pos.setBeginIndex(0); pos.setEndIndex(0); // format prefix toAppendTo.append(prefix); // format components for (int i = 0; i < vector.getDimension(); ++i) { if (i > 0) { toAppendTo.append(separator); } CompositeFormat.formatDouble(vector.getEntry(i), format, toAppendTo, pos); } // format suffix toAppendTo.append(suffix); return toAppendTo; } /** * Parse a string to produce a {@link RealVector} object. * * @param source String to parse. * @return the parsed {@link RealVector} object. * @throws MathParseException if the beginning of the specified string * cannot be parsed. */ public ArrayRealVector parse(String source) { final ParsePosition parsePosition = new ParsePosition(0); final ArrayRealVector result = parse(source, parsePosition); if (parsePosition.getIndex() == 0) { throw new MathParseException(source, parsePosition.getErrorIndex(), ArrayRealVector.class); } return result; } /** * Parse a string to produce a {@link RealVector} object. * * @param source String to parse. * @param pos input/ouput parsing parameter. * @return the parsed {@link RealVector} object. */ public ArrayRealVector parse(String source, ParsePosition pos) { int initialIndex = pos.getIndex(); // parse prefix CompositeFormat.parseAndIgnoreWhitespace(source, pos); if (!CompositeFormat.parseFixedstring(source, trimmedPrefix, pos)) { return null; } // parse components List components = new ArrayList(); for (boolean loop = true; loop;){ if (!components.isEmpty()) { CompositeFormat.parseAndIgnoreWhitespace(source, pos); if (!CompositeFormat.parseFixedstring(source, trimmedSeparator, pos)) { loop = false; } } if (loop) { CompositeFormat.parseAndIgnoreWhitespace(source, pos); Number component = CompositeFormat.parseNumber(source, format, pos); if (component != null) { components.add(component); } else { // invalid component // set index back to initial, error index should already be set pos.setIndex(initialIndex); return null; } } } // parse suffix CompositeFormat.parseAndIgnoreWhitespace(source, pos); if (!CompositeFormat.parseFixedstring(source, trimmedSuffix, pos)) { return null; } // build vector double[] data = new double[components.size()]; for (int i = 0; i < data.length; ++i) { data[i] = components.get(i).doubleValue(); } return new ArrayRealVector(data, false); } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/linear/SingularMatrixException.java100644 1750 1750 2712 12126627717 31476 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.linear; import org.apache.commons.math3.exception.MathIllegalArgumentException; import org.apache.commons.math3.exception.util.LocalizedFormats; /** * Exception to be thrown when a non-singular matrix is expected. * * @since 3.0 * @version $Id: SingularMatrixException.java 1416643 2012-12-03 19:37:14Z tn $ */ public class SingularMatrixException extends MathIllegalArgumentException { /** Serializable version Id. */ private static final long serialVersionUID = -4206514844735401070L; /** * Construct an exception. */ public SingularMatrixException() { super(LocalizedFormats.SINGULAR_MATRIX); } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/linear/DiagonalMatrix.java100644 1750 1750 23277 12126627717 27602 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.linear; import java.io.Serializable; import org.apache.commons.math3.exception.DimensionMismatchException; import org.apache.commons.math3.exception.NotStrictlyPositiveException; import org.apache.commons.math3.exception.NullArgumentException; import org.apache.commons.math3.exception.NumberIsTooLargeException; import org.apache.commons.math3.exception.OutOfRangeException; import org.apache.commons.math3.util.FastMath; import org.apache.commons.math3.util.MathUtils; import org.apache.commons.math3.util.Precision; /** * Implementation of a diagonal matrix. * * @version $Id$ * @since 3.1.1 */ public class DiagonalMatrix extends AbstractRealMatrix implements Serializable { /** Serializable version identifier. */ private static final long serialVersionUID = 20121229L; /** Entries of the diagonal. */ private final double[] data; /** * Creates a matrix with the supplied dimension. * * @param dimension Number of rows and columns in the new matrix. * @throws NotStrictlyPositiveException if the dimension is * not positive. */ public DiagonalMatrix(final int dimension) throws NotStrictlyPositiveException { super(dimension, dimension); data = new double[dimension]; } /** * Creates a matrix using the input array as the underlying data. *
                              * The input array is copied, not referenced. * * @param d Data for the new matrix. */ public DiagonalMatrix(final double[] d) { this(d, true); } /** * Creates a matrix using the input array as the underlying data. *
                              * If an array is created specially in order to be embedded in a * this instance and not used directly, the {@code copyArray} may be * set to {@code false}. * This will prevent the copying and improve performance as no new * array will be built and no data will be copied. * * @param d Data for new matrix. * @param copyArray if {@code true}, the input array will be copied, * otherwise it will be referenced. * @exception NullArgumentException if d is null */ public DiagonalMatrix(final double[] d, final boolean copyArray) throws NullArgumentException { MathUtils.checkNotNull(d); data = copyArray ? d.clone() : d; } /** * {@inheritDoc} * * @throws DimensionMismatchException if the requested dimensions are not equal. */ @Override public RealMatrix createMatrix(final int rowDimension, final int columnDimension) throws NotStrictlyPositiveException, DimensionMismatchException { if (rowDimension != columnDimension) { throw new DimensionMismatchException(rowDimension, columnDimension); } return new DiagonalMatrix(rowDimension); } /** {@inheritDoc} */ @Override public RealMatrix copy() { return new DiagonalMatrix(data); } /** * Compute the sum of {@code this} and {@code m}. * * @param m Matrix to be added. * @return {@code this + m}. * @throws MatrixDimensionMismatchException if {@code m} is not the same * size as {@code this}. */ public DiagonalMatrix add(final DiagonalMatrix m) throws MatrixDimensionMismatchException { // Safety check. MatrixUtils.checkAdditionCompatible(this, m); final int dim = getRowDimension(); final double[] outData = new double[dim]; for (int i = 0; i < dim; i++) { outData[i] = data[i] + m.data[i]; } return new DiagonalMatrix(outData, false); } /** * Returns {@code this} minus {@code m}. * * @param m Matrix to be subtracted. * @return {@code this - m} * @throws MatrixDimensionMismatchException if {@code m} is not the same * size as {@code this}. */ public DiagonalMatrix subtract(final DiagonalMatrix m) throws MatrixDimensionMismatchException { MatrixUtils.checkSubtractionCompatible(this, m); final int dim = getRowDimension(); final double[] outData = new double[dim]; for (int i = 0; i < dim; i++) { outData[i] = data[i] - m.data[i]; } return new DiagonalMatrix(outData, false); } /** * Returns the result of postmultiplying {@code this} by {@code m}. * * @param m matrix to postmultiply by * @return {@code this * m} * @throws DimensionMismatchException if * {@code columnDimension(this) != rowDimension(m)} */ public DiagonalMatrix multiply(final DiagonalMatrix m) throws DimensionMismatchException { MatrixUtils.checkMultiplicationCompatible(this, m); final int dim = getRowDimension(); final double[] outData = new double[dim]; for (int i = 0; i < dim; i++) { outData[i] = data[i] * m.data[i]; } return new DiagonalMatrix(outData, false); } /** * Returns the result of postmultiplying {@code this} by {@code m}. * * @param m matrix to postmultiply by * @return {@code this * m} * @throws DimensionMismatchException if * {@code columnDimension(this) != rowDimension(m)} */ public RealMatrix multiply(final RealMatrix m) throws DimensionMismatchException { if (m instanceof DiagonalMatrix) { return multiply((DiagonalMatrix) m); } else { MatrixUtils.checkMultiplicationCompatible(this, m); final int nRows = m.getRowDimension(); final int nCols = m.getColumnDimension(); final double[][] product = new double[nRows][nCols]; for (int r = 0; r < nRows; r++) { for (int c = 0; c < nCols; c++) { product[r][c] = data[r] * m.getEntry(r, c); } } return new Array2DRowRealMatrix(product, false); } } /** {@inheritDoc} */ @Override public double[][] getData() { final int dim = getRowDimension(); final double[][] out = new double[dim][dim]; for (int i = 0; i < dim; i++) { out[i][i] = data[i]; } return out; } /** * Gets a reference to the underlying data array. * * @return 1-dimensional array of entries. */ public double[] getDataRef() { return data; } /** {@inheritDoc} */ @Override public double getEntry(final int row, final int column) throws OutOfRangeException { MatrixUtils.checkMatrixIndex(this, row, column); return row == column ? data[row] : 0; } /** {@inheritDoc} * @throws NumberIsTooLargeException if {@code row != column} and value is non-zero. */ @Override public void setEntry(final int row, final int column, final double value) throws OutOfRangeException, NumberIsTooLargeException { if (row == column) { MatrixUtils.checkRowIndex(this, row); data[row] = value; } else { ensureZero(value); } } /** {@inheritDoc} * @throws NumberIsTooLargeException if {@code row != column} and increment is non-zero. */ @Override public void addToEntry(final int row, final int column, final double increment) throws OutOfRangeException, NumberIsTooLargeException { if (row == column) { MatrixUtils.checkRowIndex(this, row); data[row] += increment; } else { ensureZero(increment); } } /** {@inheritDoc} */ @Override public void multiplyEntry(final int row, final int column, final double factor) throws OutOfRangeException { // we don't care about non-diagonal elements for multiplication if (row == column) { MatrixUtils.checkRowIndex(this, row); data[row] *= factor; } } /** {@inheritDoc} */ @Override public int getRowDimension() { return data.length; } /** {@inheritDoc} */ @Override public int getColumnDimension() { return data.length; } /** {@inheritDoc} */ @Override public double[] operate(final double[] v) throws DimensionMismatchException { return multiply(new DiagonalMatrix(v, false)).getDataRef(); } /** {@inheritDoc} */ @Override public double[] preMultiply(final double[] v) throws DimensionMismatchException { return operate(v); } /** Ensure a value is zero. * @param value value to check * @exception NumberIsTooLargeException if value is not zero */ private void ensureZero(final double value) throws NumberIsTooLargeException { if (!Precision.equals(0.0, value, 1)) { throw new NumberIsTooLargeException(FastMath.abs(value), 0, true); } } } ././@LongLink100644 0 0 154 12126630647 10262 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/linear/NonPositiveDefiniteMatrixException.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/linear/NonPositiveDefiniteMatrixExcepti100644 1750 1750 5050 12126627717 32360 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.linear; import org.apache.commons.math3.exception.NumberIsTooSmallException; import org.apache.commons.math3.exception.util.LocalizedFormats; import org.apache.commons.math3.exception.util.ExceptionContext; /** * Exception to be thrown when a positive definite matrix is expected. * * @since 3.0 * @version $Id: NonPositiveDefiniteMatrixException.java 1416643 2012-12-03 19:37:14Z tn $ */ public class NonPositiveDefiniteMatrixException extends NumberIsTooSmallException { /** Serializable version Id. */ private static final long serialVersionUID = 1641613838113738061L; /** Index (diagonal element). */ private final int index; /** Threshold. */ private final double threshold; /** * Construct an exception. * * @param wrong Value that fails the positivity check. * @param index Row (and column) index. * @param threshold Absolute positivity threshold. */ public NonPositiveDefiniteMatrixException(double wrong, int index, double threshold) { super(wrong, threshold, false); this.index = index; this.threshold = threshold; final ExceptionContext context = getContext(); context.addMessage(LocalizedFormats.NOT_POSITIVE_DEFINITE_MATRIX); context.addMessage(LocalizedFormats.ARRAY_ELEMENT, wrong, index); } /** * @return the row index. */ public int getRow() { return index; } /** * @return the column index. */ public int getColumn() { return index; } /** * @return the absolute positivity threshold. */ public double getThreshold() { return threshold; } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/linear/ArrayRealVector.java100644 1750 1750 72651 12126627717 27744 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.linear; import java.io.Serializable; import java.util.Arrays; import java.util.Iterator; import org.apache.commons.math3.analysis.UnivariateFunction; import org.apache.commons.math3.exception.NotPositiveException; import org.apache.commons.math3.exception.NullArgumentException; import org.apache.commons.math3.exception.DimensionMismatchException; import org.apache.commons.math3.exception.NumberIsTooLargeException; import org.apache.commons.math3.exception.NumberIsTooSmallException; import org.apache.commons.math3.exception.OutOfRangeException; import org.apache.commons.math3.exception.util.LocalizedFormats; import org.apache.commons.math3.util.MathUtils; import org.apache.commons.math3.util.FastMath; /** * This class implements the {@link RealVector} interface with a double array. * @version $Id: ArrayRealVector.java 1416643 2012-12-03 19:37:14Z tn $ * @since 2.0 */ public class ArrayRealVector extends RealVector implements Serializable { /** Serializable version identifier. */ private static final long serialVersionUID = -1097961340710804027L; /** Default format. */ private static final RealVectorFormat DEFAULT_FORMAT = RealVectorFormat.getInstance(); /** Entries of the vector. */ private double data[]; /** * Build a 0-length vector. * Zero-length vectors may be used to initialized construction of vectors * by data gathering. We start with zero-length and use either the {@link * #ArrayRealVector(ArrayRealVector, ArrayRealVector)} constructor * or one of the {@code append} method ({@link #append(double)}, * {@link #append(ArrayRealVector)}) to gather data into this vector. */ public ArrayRealVector() { data = new double[0]; } /** * Construct a vector of zeroes. * * @param size Size of the vector. */ public ArrayRealVector(int size) { data = new double[size]; } /** * Construct a vector with preset values. * * @param size Size of the vector * @param preset All entries will be set with this value. */ public ArrayRealVector(int size, double preset) { data = new double[size]; Arrays.fill(data, preset); } /** * Construct a vector from an array, copying the input array. * * @param d Array. */ public ArrayRealVector(double[] d) { data = d.clone(); } /** * Create a new ArrayRealVector using the input array as the underlying * data array. * If an array is built specially in order to be embedded in a * ArrayRealVector and not used directly, the {@code copyArray} may be * set to {@code false}. This will prevent the copying and improve * performance as no new array will be built and no data will be copied. * * @param d Data for the new vector. * @param copyArray if {@code true}, the input array will be copied, * otherwise it will be referenced. * @throws NullArgumentException if {@code d} is {@code null}. * @see #ArrayRealVector(double[]) */ public ArrayRealVector(double[] d, boolean copyArray) throws NullArgumentException { if (d == null) { throw new NullArgumentException(); } data = copyArray ? d.clone() : d; } /** * Construct a vector from part of a array. * * @param d Array. * @param pos Position of first entry. * @param size Number of entries to copy. * @throws NullArgumentException if {@code d} is {@code null}. * @throws NumberIsTooLargeException if the size of {@code d} is less * than {@code pos + size}. */ public ArrayRealVector(double[] d, int pos, int size) throws NullArgumentException, NumberIsTooLargeException { if (d == null) { throw new NullArgumentException(); } if (d.length < pos + size) { throw new NumberIsTooLargeException(pos + size, d.length, true); } data = new double[size]; System.arraycopy(d, pos, data, 0, size); } /** * Construct a vector from an array. * * @param d Array of {@code Double}s. */ public ArrayRealVector(Double[] d) { data = new double[d.length]; for (int i = 0; i < d.length; i++) { data[i] = d[i].doubleValue(); } } /** * Construct a vector from part of an array. * * @param d Array. * @param pos Position of first entry. * @param size Number of entries to copy. * @throws NullArgumentException if {@code d} is {@code null}. * @throws NumberIsTooLargeException if the size of {@code d} is less * than {@code pos + size}. */ public ArrayRealVector(Double[] d, int pos, int size) throws NullArgumentException, NumberIsTooLargeException { if (d == null) { throw new NullArgumentException(); } if (d.length < pos + size) { throw new NumberIsTooLargeException(pos + size, d.length, true); } data = new double[size]; for (int i = pos; i < pos + size; i++) { data[i - pos] = d[i].doubleValue(); } } /** * Construct a vector from another vector, using a deep copy. * * @param v vector to copy. * @throws NullArgumentException if {@code v} is {@code null}. */ public ArrayRealVector(RealVector v) throws NullArgumentException { if (v == null) { throw new NullArgumentException(); } data = new double[v.getDimension()]; for (int i = 0; i < data.length; ++i) { data[i] = v.getEntry(i); } } /** * Construct a vector from another vector, using a deep copy. * * @param v Vector to copy. * @throws NullArgumentException if {@code v} is {@code null}. */ public ArrayRealVector(ArrayRealVector v) throws NullArgumentException { this(v, true); } /** * Construct a vector from another vector. * * @param v Vector to copy. * @param deep If {@code true} perform a deep copy, otherwise perform a * shallow copy. */ public ArrayRealVector(ArrayRealVector v, boolean deep) { data = deep ? v.data.clone() : v.data; } /** * Construct a vector by appending one vector to another vector. * @param v1 First vector (will be put in front of the new vector). * @param v2 Second vector (will be put at back of the new vector). */ public ArrayRealVector(ArrayRealVector v1, ArrayRealVector v2) { data = new double[v1.data.length + v2.data.length]; System.arraycopy(v1.data, 0, data, 0, v1.data.length); System.arraycopy(v2.data, 0, data, v1.data.length, v2.data.length); } /** * Construct a vector by appending one vector to another vector. * @param v1 First vector (will be put in front of the new vector). * @param v2 Second vector (will be put at back of the new vector). */ public ArrayRealVector(ArrayRealVector v1, RealVector v2) { final int l1 = v1.data.length; final int l2 = v2.getDimension(); data = new double[l1 + l2]; System.arraycopy(v1.data, 0, data, 0, l1); for (int i = 0; i < l2; ++i) { data[l1 + i] = v2.getEntry(i); } } /** * Construct a vector by appending one vector to another vector. * @param v1 First vector (will be put in front of the new vector). * @param v2 Second vector (will be put at back of the new vector). */ public ArrayRealVector(RealVector v1, ArrayRealVector v2) { final int l1 = v1.getDimension(); final int l2 = v2.data.length; data = new double[l1 + l2]; for (int i = 0; i < l1; ++i) { data[i] = v1.getEntry(i); } System.arraycopy(v2.data, 0, data, l1, l2); } /** * Construct a vector by appending one vector to another vector. * @param v1 First vector (will be put in front of the new vector). * @param v2 Second vector (will be put at back of the new vector). */ public ArrayRealVector(ArrayRealVector v1, double[] v2) { final int l1 = v1.getDimension(); final int l2 = v2.length; data = new double[l1 + l2]; System.arraycopy(v1.data, 0, data, 0, l1); System.arraycopy(v2, 0, data, l1, l2); } /** * Construct a vector by appending one vector to another vector. * @param v1 First vector (will be put in front of the new vector). * @param v2 Second vector (will be put at back of the new vector). */ public ArrayRealVector(double[] v1, ArrayRealVector v2) { final int l1 = v1.length; final int l2 = v2.getDimension(); data = new double[l1 + l2]; System.arraycopy(v1, 0, data, 0, l1); System.arraycopy(v2.data, 0, data, l1, l2); } /** * Construct a vector by appending one vector to another vector. * @param v1 first vector (will be put in front of the new vector) * @param v2 second vector (will be put at back of the new vector) */ public ArrayRealVector(double[] v1, double[] v2) { final int l1 = v1.length; final int l2 = v2.length; data = new double[l1 + l2]; System.arraycopy(v1, 0, data, 0, l1); System.arraycopy(v2, 0, data, l1, l2); } /** {@inheritDoc} */ @Override public ArrayRealVector copy() { return new ArrayRealVector(this, true); } /** {@inheritDoc} */ @Override public ArrayRealVector add(RealVector v) throws DimensionMismatchException { if (v instanceof ArrayRealVector) { final double[] vData = ((ArrayRealVector) v).data; final int dim = vData.length; checkVectorDimensions(dim); ArrayRealVector result = new ArrayRealVector(dim); double[] resultData = result.data; for (int i = 0; i < dim; i++) { resultData[i] = data[i] + vData[i]; } return result; } else { checkVectorDimensions(v); double[] out = data.clone(); Iterator it = v.iterator(); while (it.hasNext()) { final Entry e = it.next(); out[e.getIndex()] += e.getValue(); } return new ArrayRealVector(out, false); } } /** {@inheritDoc} */ @Override public ArrayRealVector subtract(RealVector v) throws DimensionMismatchException { if (v instanceof ArrayRealVector) { final double[] vData = ((ArrayRealVector) v).data; final int dim = vData.length; checkVectorDimensions(dim); ArrayRealVector result = new ArrayRealVector(dim); double[] resultData = result.data; for (int i = 0; i < dim; i++) { resultData[i] = data[i] - vData[i]; } return result; } else { checkVectorDimensions(v); double[] out = data.clone(); Iterator it = v.iterator(); while (it.hasNext()) { final Entry e = it.next(); out[e.getIndex()] -= e.getValue(); } return new ArrayRealVector(out, false); } } /** {@inheritDoc} */ @Override public ArrayRealVector map(UnivariateFunction function) { return copy().mapToSelf(function); } /** {@inheritDoc} */ @Override public ArrayRealVector mapToSelf(UnivariateFunction function) { for (int i = 0; i < data.length; i++) { data[i] = function.value(data[i]); } return this; } /** {@inheritDoc} */ @Override public RealVector mapAddToSelf(double d) { for (int i = 0; i < data.length; i++) { data[i] = data[i] + d; } return this; } /** {@inheritDoc} */ @Override public RealVector mapSubtractToSelf(double d) { for (int i = 0; i < data.length; i++) { data[i] = data[i] - d; } return this; } /** {@inheritDoc} */ @Override public RealVector mapMultiplyToSelf(double d) { for (int i = 0; i < data.length; i++) { data[i] = data[i] * d; } return this; } /** {@inheritDoc} */ @Override public RealVector mapDivideToSelf(double d) { for (int i = 0; i < data.length; i++) { data[i] = data[i] / d; } return this; } /** {@inheritDoc} */ @Override public ArrayRealVector ebeMultiply(RealVector v) throws DimensionMismatchException { if (v instanceof ArrayRealVector) { final double[] vData = ((ArrayRealVector) v).data; final int dim = vData.length; checkVectorDimensions(dim); ArrayRealVector result = new ArrayRealVector(dim); double[] resultData = result.data; for (int i = 0; i < dim; i++) { resultData[i] = data[i] * vData[i]; } return result; } else { checkVectorDimensions(v); double[] out = data.clone(); for (int i = 0; i < data.length; i++) { out[i] *= v.getEntry(i); } return new ArrayRealVector(out, false); } } /** {@inheritDoc} */ @Override public ArrayRealVector ebeDivide(RealVector v) throws DimensionMismatchException { if (v instanceof ArrayRealVector) { final double[] vData = ((ArrayRealVector) v).data; final int dim = vData.length; checkVectorDimensions(dim); ArrayRealVector result = new ArrayRealVector(dim); double[] resultData = result.data; for (int i = 0; i < dim; i++) { resultData[i] = data[i] / vData[i]; } return result; } else { checkVectorDimensions(v); double[] out = data.clone(); for (int i = 0; i < data.length; i++) { out[i] /= v.getEntry(i); } return new ArrayRealVector(out, false); } } /** * Get a reference to the underlying data array. * This method does not make a fresh copy of the underlying data. * * @return the array of entries. */ public double[] getDataRef() { return data; } /** {@inheritDoc} */ @Override public double dotProduct(RealVector v) throws DimensionMismatchException { if (v instanceof ArrayRealVector) { final double[] vData = ((ArrayRealVector) v).data; checkVectorDimensions(vData.length); double dot = 0; for (int i = 0; i < data.length; i++) { dot += data[i] * vData[i]; } return dot; } return super.dotProduct(v); } /** {@inheritDoc} */ @Override public double getNorm() { double sum = 0; for (double a : data) { sum += a * a; } return FastMath.sqrt(sum); } /** {@inheritDoc} */ @Override public double getL1Norm() { double sum = 0; for (double a : data) { sum += FastMath.abs(a); } return sum; } /** {@inheritDoc} */ @Override public double getLInfNorm() { double max = 0; for (double a : data) { max = FastMath.max(max, FastMath.abs(a)); } return max; } /** {@inheritDoc} */ @Override public double getDistance(RealVector v) throws DimensionMismatchException { if (v instanceof ArrayRealVector) { final double[] vData = ((ArrayRealVector) v).data; checkVectorDimensions(vData.length); double sum = 0; for (int i = 0; i < data.length; ++i) { final double delta = data[i] - vData[i]; sum += delta * delta; } return FastMath.sqrt(sum); } else { checkVectorDimensions(v); double sum = 0; for (int i = 0; i < data.length; ++i) { final double delta = data[i] - v.getEntry(i); sum += delta * delta; } return FastMath.sqrt(sum); } } /** {@inheritDoc} */ @Override public double getL1Distance(RealVector v) throws DimensionMismatchException { if (v instanceof ArrayRealVector) { final double[] vData = ((ArrayRealVector) v).data; checkVectorDimensions(vData.length); double sum = 0; for (int i = 0; i < data.length; ++i) { final double delta = data[i] - vData[i]; sum += FastMath.abs(delta); } return sum; } else { checkVectorDimensions(v); double sum = 0; for (int i = 0; i < data.length; ++i) { final double delta = data[i] - v.getEntry(i); sum += FastMath.abs(delta); } return sum; } } /** {@inheritDoc} */ @Override public double getLInfDistance(RealVector v) throws DimensionMismatchException { if (v instanceof ArrayRealVector) { final double[] vData = ((ArrayRealVector) v).data; checkVectorDimensions(vData.length); double max = 0; for (int i = 0; i < data.length; ++i) { final double delta = data[i] - vData[i]; max = FastMath.max(max, FastMath.abs(delta)); } return max; } else { checkVectorDimensions(v); double max = 0; for (int i = 0; i < data.length; ++i) { final double delta = data[i] - v.getEntry(i); max = FastMath.max(max, FastMath.abs(delta)); } return max; } } /** {@inheritDoc} */ @Override public RealMatrix outerProduct(RealVector v) { if (v instanceof ArrayRealVector) { final double[] vData = ((ArrayRealVector) v).data; final int m = data.length; final int n = vData.length; final RealMatrix out = MatrixUtils.createRealMatrix(m, n); for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { out.setEntry(i, j, data[i] * vData[j]); } } return out; } else { final int m = data.length; final int n = v.getDimension(); final RealMatrix out = MatrixUtils.createRealMatrix(m, n); for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { out.setEntry(i, j, data[i] * v.getEntry(j)); } } return out; } } /** {@inheritDoc} */ @Override public double getEntry(int index) throws OutOfRangeException { try { return data[index]; } catch (IndexOutOfBoundsException e) { throw new OutOfRangeException(LocalizedFormats.INDEX, index, 0, getDimension() - 1); } } /** {@inheritDoc} */ @Override public int getDimension() { return data.length; } /** {@inheritDoc} */ @Override public RealVector append(RealVector v) { try { return new ArrayRealVector(this, (ArrayRealVector) v); } catch (ClassCastException cce) { return new ArrayRealVector(this, v); } } /** * Construct a vector by appending a vector to this vector. * * @param v Vector to append to this one. * @return a new vector. */ public ArrayRealVector append(ArrayRealVector v) { return new ArrayRealVector(this, v); } /** {@inheritDoc} */ @Override public RealVector append(double in) { final double[] out = new double[data.length + 1]; System.arraycopy(data, 0, out, 0, data.length); out[data.length] = in; return new ArrayRealVector(out, false); } /** {@inheritDoc} */ @Override public RealVector getSubVector(int index, int n) throws OutOfRangeException, NotPositiveException { if (n < 0) { throw new NotPositiveException(LocalizedFormats.NUMBER_OF_ELEMENTS_SHOULD_BE_POSITIVE, n); } ArrayRealVector out = new ArrayRealVector(n); try { System.arraycopy(data, index, out.data, 0, n); } catch (IndexOutOfBoundsException e) { checkIndex(index); checkIndex(index + n - 1); } return out; } /** {@inheritDoc} */ @Override public void setEntry(int index, double value) throws OutOfRangeException { try { data[index] = value; } catch (IndexOutOfBoundsException e) { checkIndex(index); } } /** {@inheritDoc} */ @Override public void addToEntry(int index, double increment) throws OutOfRangeException { try { data[index] += increment; } catch(IndexOutOfBoundsException e){ throw new OutOfRangeException(LocalizedFormats.INDEX, index, 0, data.length - 1); } } /** {@inheritDoc} */ @Override public void setSubVector(int index, RealVector v) throws OutOfRangeException { if (v instanceof ArrayRealVector) { setSubVector(index, ((ArrayRealVector) v).data); } else { try { for (int i = index; i < index + v.getDimension(); ++i) { data[i] = v.getEntry(i - index); } } catch (IndexOutOfBoundsException e) { checkIndex(index); checkIndex(index + v.getDimension() - 1); } } } /** * Set a set of consecutive elements. * * @param index Index of first element to be set. * @param v Vector containing the values to set. * @throws OutOfRangeException if the index is inconsistent with the vector * size. */ public void setSubVector(int index, double[] v) throws OutOfRangeException { try { System.arraycopy(v, 0, data, index, v.length); } catch (IndexOutOfBoundsException e) { checkIndex(index); checkIndex(index + v.length - 1); } } /** {@inheritDoc} */ @Override public void set(double value) { Arrays.fill(data, value); } /** {@inheritDoc} */ @Override public double[] toArray(){ return data.clone(); } /** {@inheritDoc} */ @Override public String toString(){ return DEFAULT_FORMAT.format(this); } /** * Check if instance and specified vectors have the same dimension. * * @param v Vector to compare instance with. * @throws DimensionMismatchException if the vectors do not * have the same dimension. */ @Override protected void checkVectorDimensions(RealVector v) throws DimensionMismatchException { checkVectorDimensions(v.getDimension()); } /** * Check if instance dimension is equal to some expected value. * * @param n Expected dimension. * @throws DimensionMismatchException if the dimension is * inconsistent with vector size. */ @Override protected void checkVectorDimensions(int n) throws DimensionMismatchException { if (data.length != n) { throw new DimensionMismatchException(data.length, n); } } /** * Check if any coordinate of this vector is {@code NaN}. * * @return {@code true} if any coordinate of this vector is {@code NaN}, * {@code false} otherwise. */ @Override public boolean isNaN() { for (double v : data) { if (Double.isNaN(v)) { return true; } } return false; } /** * Check whether any coordinate of this vector is infinite and none * are {@code NaN}. * * @return {@code true} if any coordinate of this vector is infinite and * none are {@code NaN}, {@code false} otherwise. */ @Override public boolean isInfinite() { if (isNaN()) { return false; } for (double v : data) { if (Double.isInfinite(v)) { return true; } } return false; } /** {@inheritDoc} */ @Override public boolean equals(Object other) { if (this == other) { return true; } if (!(other instanceof RealVector)) { return false; } RealVector rhs = (RealVector) other; if (data.length != rhs.getDimension()) { return false; } if (rhs.isNaN()) { return this.isNaN(); } for (int i = 0; i < data.length; ++i) { if (data[i] != rhs.getEntry(i)) { return false; } } return true; } /** * {@inheritDoc} All {@code NaN} values have the same hash code. */ @Override public int hashCode() { if (isNaN()) { return 9; } return MathUtils.hash(data); } /** {@inheritDoc} */ @Override public ArrayRealVector combine(double a, double b, RealVector y) throws DimensionMismatchException { return copy().combineToSelf(a, b, y); } /** {@inheritDoc} */ @Override public ArrayRealVector combineToSelf(double a, double b, RealVector y) throws DimensionMismatchException { if (y instanceof ArrayRealVector) { final double[] yData = ((ArrayRealVector) y).data; checkVectorDimensions(yData.length); for (int i = 0; i < this.data.length; i++) { data[i] = a * data[i] + b * yData[i]; } } else { checkVectorDimensions(y); for (int i = 0; i < this.data.length; i++) { data[i] = a * data[i] + b * y.getEntry(i); } } return this; } /** {@inheritDoc} */ @Override public double walkInDefaultOrder(final RealVectorPreservingVisitor visitor) { visitor.start(data.length, 0, data.length - 1); for (int i = 0; i < data.length; i++) { visitor.visit(i, data[i]); } return visitor.end(); } /** {@inheritDoc} */ @Override public double walkInDefaultOrder(final RealVectorPreservingVisitor visitor, final int start, final int end) throws NumberIsTooSmallException, OutOfRangeException { checkIndices(start, end); visitor.start(data.length, start, end); for (int i = start; i <= end; i++) { visitor.visit(i, data[i]); } return visitor.end(); } /** * {@inheritDoc} * * In this implementation, the optimized order is the default order. */ @Override public double walkInOptimizedOrder(final RealVectorPreservingVisitor visitor) { return walkInDefaultOrder(visitor); } /** * {@inheritDoc} * * In this implementation, the optimized order is the default order. */ @Override public double walkInOptimizedOrder(final RealVectorPreservingVisitor visitor, final int start, final int end) throws NumberIsTooSmallException, OutOfRangeException { return walkInDefaultOrder(visitor, start, end); } /** {@inheritDoc} */ @Override public double walkInDefaultOrder(final RealVectorChangingVisitor visitor) { visitor.start(data.length, 0, data.length - 1); for (int i = 0; i < data.length; i++) { data[i] = visitor.visit(i, data[i]); } return visitor.end(); } /** {@inheritDoc} */ @Override public double walkInDefaultOrder(final RealVectorChangingVisitor visitor, final int start, final int end) throws NumberIsTooSmallException, OutOfRangeException { checkIndices(start, end); visitor.start(data.length, start, end); for (int i = start; i <= end; i++) { data[i] = visitor.visit(i, data[i]); } return visitor.end(); } /** * {@inheritDoc} * * In this implementation, the optimized order is the default order. */ @Override public double walkInOptimizedOrder(final RealVectorChangingVisitor visitor) { return walkInDefaultOrder(visitor); } /** * {@inheritDoc} * * In this implementation, the optimized order is the default order. */ @Override public double walkInOptimizedOrder(final RealVectorChangingVisitor visitor, final int start, final int end) throws NumberIsTooSmallException, OutOfRangeException { return walkInDefaultOrder(visitor, start, end); } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/linear/Array2DRowRealMatrix.java100644 1750 1750 47407 12126627717 30625 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.linear; import java.io.Serializable; import org.apache.commons.math3.exception.DimensionMismatchException; import org.apache.commons.math3.exception.MathIllegalStateException; import org.apache.commons.math3.exception.NoDataException; import org.apache.commons.math3.exception.NotStrictlyPositiveException; import org.apache.commons.math3.exception.NullArgumentException; import org.apache.commons.math3.exception.NumberIsTooSmallException; import org.apache.commons.math3.exception.OutOfRangeException; import org.apache.commons.math3.exception.util.LocalizedFormats; import org.apache.commons.math3.util.MathUtils; /** * Implementation of {@link RealMatrix} using a {@code double[][]} array to * store entries. * * @version $Id: Array2DRowRealMatrix.java 1459082 2013-03-20 22:24:09Z tn $ */ public class Array2DRowRealMatrix extends AbstractRealMatrix implements Serializable { /** Serializable version identifier. */ private static final long serialVersionUID = -1067294169172445528L; /** Entries of the matrix. */ private double data[][]; /** * Creates a matrix with no data */ public Array2DRowRealMatrix() {} /** * Create a new RealMatrix with the supplied row and column dimensions. * * @param rowDimension Number of rows in the new matrix. * @param columnDimension Number of columns in the new matrix. * @throws NotStrictlyPositiveException if the row or column dimension is * not positive. */ public Array2DRowRealMatrix(final int rowDimension, final int columnDimension) throws NotStrictlyPositiveException { super(rowDimension, columnDimension); data = new double[rowDimension][columnDimension]; } /** * Create a new {@code RealMatrix} using the input array as the underlying * data array. *

                              The input array is copied, not referenced. This constructor has * the same effect as calling {@link #Array2DRowRealMatrix(double[][], boolean)} * with the second argument set to {@code true}.

                              * * @param d Data for the new matrix. * @throws DimensionMismatchException if {@code d} is not rectangular. * @throws NoDataException if {@code d} row or column dimension is zero. * @throws NullArgumentException if {@code d} is {@code null}. * @see #Array2DRowRealMatrix(double[][], boolean) */ public Array2DRowRealMatrix(final double[][] d) throws DimensionMismatchException, NoDataException, NullArgumentException { copyIn(d); } /** * Create a new RealMatrix using the input array as the underlying * data array. * If an array is built specially in order to be embedded in a * RealMatrix and not used directly, the {@code copyArray} may be * set to {@code false}. This will prevent the copying and improve * performance as no new array will be built and no data will be copied. * * @param d Data for new matrix. * @param copyArray if {@code true}, the input array will be copied, * otherwise it will be referenced. * @throws DimensionMismatchException if {@code d} is not rectangular. * @throws NoDataException if {@code d} row or column dimension is zero. * @throws NullArgumentException if {@code d} is {@code null}. * @see #Array2DRowRealMatrix(double[][]) */ public Array2DRowRealMatrix(final double[][] d, final boolean copyArray) throws DimensionMismatchException, NoDataException, NullArgumentException { if (copyArray) { copyIn(d); } else { if (d == null) { throw new NullArgumentException(); } final int nRows = d.length; if (nRows == 0) { throw new NoDataException(LocalizedFormats.AT_LEAST_ONE_ROW); } final int nCols = d[0].length; if (nCols == 0) { throw new NoDataException(LocalizedFormats.AT_LEAST_ONE_COLUMN); } for (int r = 1; r < nRows; r++) { if (d[r].length != nCols) { throw new DimensionMismatchException(d[r].length, nCols); } } data = d; } } /** * Create a new (column) RealMatrix using {@code v} as the * data for the unique column of the created matrix. * The input array is copied. * * @param v Column vector holding data for new matrix. */ public Array2DRowRealMatrix(final double[] v) { final int nRows = v.length; data = new double[nRows][1]; for (int row = 0; row < nRows; row++) { data[row][0] = v[row]; } } /** {@inheritDoc} */ @Override public RealMatrix createMatrix(final int rowDimension, final int columnDimension) throws NotStrictlyPositiveException { return new Array2DRowRealMatrix(rowDimension, columnDimension); } /** {@inheritDoc} */ @Override public RealMatrix copy() { return new Array2DRowRealMatrix(copyOut(), false); } /** * Compute the sum of {@code this} and {@code m}. * * @param m Matrix to be added. * @return {@code this + m}. * @throws MatrixDimensionMismatchException if {@code m} is not the same * size as {@code this}. */ public Array2DRowRealMatrix add(final Array2DRowRealMatrix m) throws MatrixDimensionMismatchException { // Safety check. MatrixUtils.checkAdditionCompatible(this, m); final int rowCount = getRowDimension(); final int columnCount = getColumnDimension(); final double[][] outData = new double[rowCount][columnCount]; for (int row = 0; row < rowCount; row++) { final double[] dataRow = data[row]; final double[] mRow = m.data[row]; final double[] outDataRow = outData[row]; for (int col = 0; col < columnCount; col++) { outDataRow[col] = dataRow[col] + mRow[col]; } } return new Array2DRowRealMatrix(outData, false); } /** * Returns {@code this} minus {@code m}. * * @param m Matrix to be subtracted. * @return {@code this - m} * @throws MatrixDimensionMismatchException if {@code m} is not the same * size as {@code this}. */ public Array2DRowRealMatrix subtract(final Array2DRowRealMatrix m) throws MatrixDimensionMismatchException { MatrixUtils.checkSubtractionCompatible(this, m); final int rowCount = getRowDimension(); final int columnCount = getColumnDimension(); final double[][] outData = new double[rowCount][columnCount]; for (int row = 0; row < rowCount; row++) { final double[] dataRow = data[row]; final double[] mRow = m.data[row]; final double[] outDataRow = outData[row]; for (int col = 0; col < columnCount; col++) { outDataRow[col] = dataRow[col] - mRow[col]; } } return new Array2DRowRealMatrix(outData, false); } /** * Returns the result of postmultiplying {@code this} by {@code m}. * * @param m matrix to postmultiply by * @return {@code this * m} * @throws DimensionMismatchException if * {@code columnDimension(this) != rowDimension(m)} */ public Array2DRowRealMatrix multiply(final Array2DRowRealMatrix m) throws DimensionMismatchException { MatrixUtils.checkMultiplicationCompatible(this, m); final int nRows = this.getRowDimension(); final int nCols = m.getColumnDimension(); final int nSum = this.getColumnDimension(); final double[][] outData = new double[nRows][nCols]; // Will hold a column of "m". final double[] mCol = new double[nSum]; final double[][] mData = m.data; // Multiply. for (int col = 0; col < nCols; col++) { // Copy all elements of column "col" of "m" so that // will be in contiguous memory. for (int mRow = 0; mRow < nSum; mRow++) { mCol[mRow] = mData[mRow][col]; } for (int row = 0; row < nRows; row++) { final double[] dataRow = data[row]; double sum = 0; for (int i = 0; i < nSum; i++) { sum += dataRow[i] * mCol[i]; } outData[row][col] = sum; } } return new Array2DRowRealMatrix(outData, false); } /** {@inheritDoc} */ @Override public double[][] getData() { return copyOut(); } /** * Get a reference to the underlying data array. * * @return 2-dimensional array of entries. */ public double[][] getDataRef() { return data; } /** {@inheritDoc} */ @Override public void setSubMatrix(final double[][] subMatrix, final int row, final int column) throws NoDataException, OutOfRangeException, DimensionMismatchException, NullArgumentException { if (data == null) { if (row > 0) { throw new MathIllegalStateException(LocalizedFormats.FIRST_ROWS_NOT_INITIALIZED_YET, row); } if (column > 0) { throw new MathIllegalStateException(LocalizedFormats.FIRST_COLUMNS_NOT_INITIALIZED_YET, column); } MathUtils.checkNotNull(subMatrix); final int nRows = subMatrix.length; if (nRows == 0) { throw new NoDataException(LocalizedFormats.AT_LEAST_ONE_ROW); } final int nCols = subMatrix[0].length; if (nCols == 0) { throw new NoDataException(LocalizedFormats.AT_LEAST_ONE_COLUMN); } data = new double[subMatrix.length][nCols]; for (int i = 0; i < data.length; ++i) { if (subMatrix[i].length != nCols) { throw new DimensionMismatchException(subMatrix[i].length, nCols); } System.arraycopy(subMatrix[i], 0, data[i + row], column, nCols); } } else { super.setSubMatrix(subMatrix, row, column); } } /** {@inheritDoc} */ @Override public double getEntry(final int row, final int column) throws OutOfRangeException { MatrixUtils.checkMatrixIndex(this, row, column); return data[row][column]; } /** {@inheritDoc} */ @Override public void setEntry(final int row, final int column, final double value) throws OutOfRangeException { MatrixUtils.checkMatrixIndex(this, row, column); data[row][column] = value; } /** {@inheritDoc} */ @Override public void addToEntry(final int row, final int column, final double increment) throws OutOfRangeException { MatrixUtils.checkMatrixIndex(this, row, column); data[row][column] += increment; } /** {@inheritDoc} */ @Override public void multiplyEntry(final int row, final int column, final double factor) throws OutOfRangeException { MatrixUtils.checkMatrixIndex(this, row, column); data[row][column] *= factor; } /** {@inheritDoc} */ @Override public int getRowDimension() { return (data == null) ? 0 : data.length; } /** {@inheritDoc} */ @Override public int getColumnDimension() { return ((data == null) || (data[0] == null)) ? 0 : data[0].length; } /** {@inheritDoc} */ @Override public double[] operate(final double[] v) throws DimensionMismatchException { final int nRows = this.getRowDimension(); final int nCols = this.getColumnDimension(); if (v.length != nCols) { throw new DimensionMismatchException(v.length, nCols); } final double[] out = new double[nRows]; for (int row = 0; row < nRows; row++) { final double[] dataRow = data[row]; double sum = 0; for (int i = 0; i < nCols; i++) { sum += dataRow[i] * v[i]; } out[row] = sum; } return out; } /** {@inheritDoc} */ @Override public double[] preMultiply(final double[] v) throws DimensionMismatchException { final int nRows = getRowDimension(); final int nCols = getColumnDimension(); if (v.length != nRows) { throw new DimensionMismatchException(v.length, nRows); } final double[] out = new double[nCols]; for (int col = 0; col < nCols; ++col) { double sum = 0; for (int i = 0; i < nRows; ++i) { sum += data[i][col] * v[i]; } out[col] = sum; } return out; } /** {@inheritDoc} */ @Override public double walkInRowOrder(final RealMatrixChangingVisitor visitor) { final int rows = getRowDimension(); final int columns = getColumnDimension(); visitor.start(rows, columns, 0, rows - 1, 0, columns - 1); for (int i = 0; i < rows; ++i) { final double[] rowI = data[i]; for (int j = 0; j < columns; ++j) { rowI[j] = visitor.visit(i, j, rowI[j]); } } return visitor.end(); } /** {@inheritDoc} */ @Override public double walkInRowOrder(final RealMatrixPreservingVisitor visitor) { final int rows = getRowDimension(); final int columns = getColumnDimension(); visitor.start(rows, columns, 0, rows - 1, 0, columns - 1); for (int i = 0; i < rows; ++i) { final double[] rowI = data[i]; for (int j = 0; j < columns; ++j) { visitor.visit(i, j, rowI[j]); } } return visitor.end(); } /** {@inheritDoc} */ @Override public double walkInRowOrder(final RealMatrixChangingVisitor visitor, final int startRow, final int endRow, final int startColumn, final int endColumn) throws OutOfRangeException, NumberIsTooSmallException { MatrixUtils.checkSubMatrixIndex(this, startRow, endRow, startColumn, endColumn); visitor.start(getRowDimension(), getColumnDimension(), startRow, endRow, startColumn, endColumn); for (int i = startRow; i <= endRow; ++i) { final double[] rowI = data[i]; for (int j = startColumn; j <= endColumn; ++j) { rowI[j] = visitor.visit(i, j, rowI[j]); } } return visitor.end(); } /** {@inheritDoc} */ @Override public double walkInRowOrder(final RealMatrixPreservingVisitor visitor, final int startRow, final int endRow, final int startColumn, final int endColumn) throws OutOfRangeException, NumberIsTooSmallException { MatrixUtils.checkSubMatrixIndex(this, startRow, endRow, startColumn, endColumn); visitor.start(getRowDimension(), getColumnDimension(), startRow, endRow, startColumn, endColumn); for (int i = startRow; i <= endRow; ++i) { final double[] rowI = data[i]; for (int j = startColumn; j <= endColumn; ++j) { visitor.visit(i, j, rowI[j]); } } return visitor.end(); } /** {@inheritDoc} */ @Override public double walkInColumnOrder(final RealMatrixChangingVisitor visitor) { final int rows = getRowDimension(); final int columns = getColumnDimension(); visitor.start(rows, columns, 0, rows - 1, 0, columns - 1); for (int j = 0; j < columns; ++j) { for (int i = 0; i < rows; ++i) { final double[] rowI = data[i]; rowI[j] = visitor.visit(i, j, rowI[j]); } } return visitor.end(); } /** {@inheritDoc} */ @Override public double walkInColumnOrder(final RealMatrixPreservingVisitor visitor) { final int rows = getRowDimension(); final int columns = getColumnDimension(); visitor.start(rows, columns, 0, rows - 1, 0, columns - 1); for (int j = 0; j < columns; ++j) { for (int i = 0; i < rows; ++i) { visitor.visit(i, j, data[i][j]); } } return visitor.end(); } /** {@inheritDoc} */ @Override public double walkInColumnOrder(final RealMatrixChangingVisitor visitor, final int startRow, final int endRow, final int startColumn, final int endColumn) throws OutOfRangeException, NumberIsTooSmallException { MatrixUtils.checkSubMatrixIndex(this, startRow, endRow, startColumn, endColumn); visitor.start(getRowDimension(), getColumnDimension(), startRow, endRow, startColumn, endColumn); for (int j = startColumn; j <= endColumn; ++j) { for (int i = startRow; i <= endRow; ++i) { final double[] rowI = data[i]; rowI[j] = visitor.visit(i, j, rowI[j]); } } return visitor.end(); } /** {@inheritDoc} */ @Override public double walkInColumnOrder(final RealMatrixPreservingVisitor visitor, final int startRow, final int endRow, final int startColumn, final int endColumn) throws OutOfRangeException, NumberIsTooSmallException { MatrixUtils.checkSubMatrixIndex(this, startRow, endRow, startColumn, endColumn); visitor.start(getRowDimension(), getColumnDimension(), startRow, endRow, startColumn, endColumn); for (int j = startColumn; j <= endColumn; ++j) { for (int i = startRow; i <= endRow; ++i) { visitor.visit(i, j, data[i][j]); } } return visitor.end(); } /** * Get a fresh copy of the underlying data array. * * @return a copy of the underlying data array. */ private double[][] copyOut() { final int nRows = this.getRowDimension(); final double[][] out = new double[nRows][this.getColumnDimension()]; // can't copy 2-d array in one shot, otherwise get row references for (int i = 0; i < nRows; i++) { System.arraycopy(data[i], 0, out[i], 0, data[i].length); } return out; } /** * Replace data with a fresh copy of the input array. * * @param in Data to copy. * @throws NoDataException if the input array is empty. * @throws DimensionMismatchException if the input array is not rectangular. * @throws NullArgumentException if the input array is {@code null}. */ private void copyIn(final double[][] in) throws DimensionMismatchException, NoDataException, NullArgumentException { setSubMatrix(in, 0, 0); } } ././@LongLink100644 0 0 145 12126630647 10262 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/linear/NonSymmetricMatrixException.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/linear/NonSymmetricMatrixException.java100644 1750 1750 4472 12126627717 32346 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.linear; import org.apache.commons.math3.exception.MathIllegalArgumentException; import org.apache.commons.math3.exception.util.LocalizedFormats; /** * Exception to be thrown when a symmetric matrix is expected. * * @since 3.0 * @version $Id: NonSymmetricMatrixException.java 1416643 2012-12-03 19:37:14Z tn $ */ public class NonSymmetricMatrixException extends MathIllegalArgumentException { /** Serializable version Id. */ private static final long serialVersionUID = -7518495577824189882L; /** Row. */ private final int row; /** Column. */ private final int column; /** Threshold. */ private final double threshold; /** * Construct an exception. * * @param row Row index. * @param column Column index. * @param threshold Relative symmetry threshold. */ public NonSymmetricMatrixException(int row, int column, double threshold) { super(LocalizedFormats.NON_SYMMETRIC_MATRIX, row, column, threshold); this.row = row; this.column = column; this.threshold = threshold; } /** * @return the row index of the entry. */ public int getRow() { return row; } /** * @return the column index of the entry. */ public int getColumn() { return column; } /** * @return the relative symmetry threshold. */ public double getThreshold() { return threshold; } } ././@LongLink100644 0 0 155 12126630647 10263 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/linear/PreconditionedIterativeLinearSolver.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/linear/PreconditionedIterativeLinearSol100644 1750 1750 22311 12126627717 32377 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.linear; import org.apache.commons.math3.exception.DimensionMismatchException; import org.apache.commons.math3.exception.MaxCountExceededException; import org.apache.commons.math3.exception.NullArgumentException; import org.apache.commons.math3.util.IterationManager; import org.apache.commons.math3.util.MathUtils; /** *

                              * This abstract class defines preconditioned iterative solvers. When A is * ill-conditioned, instead of solving system A · x = b directly, it is * preferable to solve either *

                              * (M · A) · x = M · b *
                              * (left preconditioning), or *
                              * (A · M) · y = b,     followed by * M · y = x *
                              * (right preconditioning), where M approximates in some way A-1, * while matrix-vector products of the type M · y remain comparatively * easy to compute. In this library, M (not M-1!) is called the * preconditionner. *

                              *

                              * Concrete implementations of this abstract class must be provided with the * preconditioner M, as a {@link RealLinearOperator}. *

                              * * @version $Id: PreconditionedIterativeLinearSolver.java 1416643 2012-12-03 19:37:14Z tn $ * @since 3.0 */ public abstract class PreconditionedIterativeLinearSolver extends IterativeLinearSolver { /** * Creates a new instance of this class, with default iteration manager. * * @param maxIterations the maximum number of iterations */ public PreconditionedIterativeLinearSolver(final int maxIterations) { super(maxIterations); } /** * Creates a new instance of this class, with custom iteration manager. * * @param manager the custom iteration manager * @throws NullArgumentException if {@code manager} is {@code null} */ public PreconditionedIterativeLinearSolver(final IterationManager manager) throws NullArgumentException { super(manager); } /** * Returns an estimate of the solution to the linear system A · x = * b. * * @param a the linear operator A of the system * @param m the preconditioner, M (can be {@code null}) * @param b the right-hand side vector * @param x0 the initial guess of the solution * @return a new vector containing the solution * @throws NullArgumentException if one of the parameters is {@code null} * @throws NonSquareOperatorException if {@code a} or {@code m} is not * square * @throws DimensionMismatchException if {@code m}, {@code b} or * {@code x0} have dimensions inconsistent with {@code a} * @throws MaxCountExceededException at exhaustion of the iteration count, * unless a custom * {@link org.apache.commons.math3.util.Incrementor.MaxCountExceededCallback callback} * has been set at construction of the {@link IterationManager} */ public RealVector solve(final RealLinearOperator a, final RealLinearOperator m, final RealVector b, final RealVector x0) throws NullArgumentException, NonSquareOperatorException, DimensionMismatchException, MaxCountExceededException { MathUtils.checkNotNull(x0); return solveInPlace(a, m, b, x0.copy()); } /** {@inheritDoc} */ @Override public RealVector solve(final RealLinearOperator a, final RealVector b) throws NullArgumentException, NonSquareOperatorException, DimensionMismatchException, MaxCountExceededException { MathUtils.checkNotNull(a); final RealVector x = new ArrayRealVector(a.getColumnDimension()); x.set(0.); return solveInPlace(a, null, b, x); } /** {@inheritDoc} */ @Override public RealVector solve(final RealLinearOperator a, final RealVector b, final RealVector x0) throws NullArgumentException, NonSquareOperatorException, DimensionMismatchException, MaxCountExceededException { MathUtils.checkNotNull(x0); return solveInPlace(a, null, b, x0.copy()); } /** * Performs all dimension checks on the parameters of * {@link #solve(RealLinearOperator, RealLinearOperator, RealVector, RealVector) solve} * and * {@link #solveInPlace(RealLinearOperator, RealLinearOperator, RealVector, RealVector) solveInPlace}, * and throws an exception if one of the checks fails. * * @param a the linear operator A of the system * @param m the preconditioner, M (can be {@code null}) * @param b the right-hand side vector * @param x0 the initial guess of the solution * @throws NullArgumentException if one of the parameters is {@code null} * @throws NonSquareOperatorException if {@code a} or {@code m} is not * square * @throws DimensionMismatchException if {@code m}, {@code b} or * {@code x0} have dimensions inconsistent with {@code a} */ protected static void checkParameters(final RealLinearOperator a, final RealLinearOperator m, final RealVector b, final RealVector x0) throws NullArgumentException, NonSquareOperatorException, DimensionMismatchException { checkParameters(a, b, x0); if (m != null) { if (m.getColumnDimension() != m.getRowDimension()) { throw new NonSquareOperatorException(m.getColumnDimension(), m.getRowDimension()); } if (m.getRowDimension() != a.getRowDimension()) { throw new DimensionMismatchException(m.getRowDimension(), a.getRowDimension()); } } } /** * Returns an estimate of the solution to the linear system A · x = * b. * * @param a the linear operator A of the system * @param m the preconditioner, M (can be {@code null}) * @param b the right-hand side vector * @return a new vector containing the solution * @throws NullArgumentException if one of the parameters is {@code null} * @throws NonSquareOperatorException if {@code a} or {@code m} is not * square * @throws DimensionMismatchException if {@code m} or {@code b} have * dimensions inconsistent with {@code a} * @throws MaxCountExceededException at exhaustion of the iteration count, * unless a custom * {@link org.apache.commons.math3.util.Incrementor.MaxCountExceededCallback callback} * has been set at construction of the {@link IterationManager} */ public RealVector solve(RealLinearOperator a, RealLinearOperator m, RealVector b) throws NullArgumentException, NonSquareOperatorException, DimensionMismatchException, MaxCountExceededException { MathUtils.checkNotNull(a); final RealVector x = new ArrayRealVector(a.getColumnDimension()); return solveInPlace(a, m, b, x); } /** * Returns an estimate of the solution to the linear system A · x = * b. The solution is computed in-place (initial guess is modified). * * @param a the linear operator A of the system * @param m the preconditioner, M (can be {@code null}) * @param b the right-hand side vector * @param x0 the initial guess of the solution * @return a reference to {@code x0} (shallow copy) updated with the * solution * @throws NullArgumentException if one of the parameters is {@code null} * @throws NonSquareOperatorException if {@code a} or {@code m} is not * square * @throws DimensionMismatchException if {@code m}, {@code b} or * {@code x0} have dimensions inconsistent with {@code a} * @throws MaxCountExceededException at exhaustion of the iteration count, * unless a custom * {@link org.apache.commons.math3.util.Incrementor.MaxCountExceededCallback callback} * has been set at construction of the {@link IterationManager} */ public abstract RealVector solveInPlace(RealLinearOperator a, RealLinearOperator m, RealVector b, RealVector x0) throws NullArgumentException, NonSquareOperatorException, DimensionMismatchException, MaxCountExceededException; /** {@inheritDoc} */ @Override public RealVector solveInPlace(final RealLinearOperator a, final RealVector b, final RealVector x0) throws NullArgumentException, NonSquareOperatorException, DimensionMismatchException, MaxCountExceededException { return solveInPlace(a, null, b, x0); } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/linear/BlockFieldMatrix.java100644 1750 1750 204475 12126627717 30103 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.linear; import java.io.Serializable; import org.apache.commons.math3.Field; import org.apache.commons.math3.FieldElement; import org.apache.commons.math3.exception.NoDataException; import org.apache.commons.math3.exception.DimensionMismatchException; import org.apache.commons.math3.exception.NotStrictlyPositiveException; import org.apache.commons.math3.exception.NullArgumentException; import org.apache.commons.math3.exception.NumberIsTooSmallException; import org.apache.commons.math3.exception.OutOfRangeException; import org.apache.commons.math3.exception.util.LocalizedFormats; import org.apache.commons.math3.util.FastMath; import org.apache.commons.math3.util.MathArrays; import org.apache.commons.math3.util.MathUtils; /** * Cache-friendly implementation of FieldMatrix using a flat arrays to store * square blocks of the matrix. *

                              * This implementation is specially designed to be cache-friendly. Square blocks are * stored as small arrays and allow efficient traversal of data both in row major direction * and columns major direction, one block at a time. This greatly increases performances * for algorithms that use crossed directions loops like multiplication or transposition. *

                              *

                              * The size of square blocks is a static parameter. It may be tuned according to the cache * size of the target computer processor. As a rule of thumbs, it should be the largest * value that allows three blocks to be simultaneously cached (this is necessary for example * for matrix multiplication). The default value is to use 36x36 blocks. *

                              *

                              * The regular blocks represent {@link #BLOCK_SIZE} x {@link #BLOCK_SIZE} squares. Blocks * at right hand side and bottom side which may be smaller to fit matrix dimensions. The square * blocks are flattened in row major order in single dimension arrays which are therefore * {@link #BLOCK_SIZE}2 elements long for regular blocks. The blocks are themselves * organized in row major order. *

                              *

                              * As an example, for a block size of 36x36, a 100x60 matrix would be stored in 6 blocks. * Block 0 would be a Field[1296] array holding the upper left 36x36 square, block 1 would be * a Field[1296] array holding the upper center 36x36 square, block 2 would be a Field[1008] * array holding the upper right 36x28 rectangle, block 3 would be a Field[864] array holding * the lower left 24x36 rectangle, block 4 would be a Field[864] array holding the lower center * 24x36 rectangle and block 5 would be a Field[672] array holding the lower right 24x28 * rectangle. *

                              *

                              * The layout complexity overhead versus simple mapping of matrices to java * arrays is negligible for small matrices (about 1%). The gain from cache efficiency leads * to up to 3-fold improvements for matrices of moderate to large size. *

                              * @param the type of the field elements * @version $Id: BlockFieldMatrix.java 1449528 2013-02-24 19:06:20Z luc $ * @since 2.0 */ public class BlockFieldMatrix> extends AbstractFieldMatrix implements Serializable { /** Block size. */ public static final int BLOCK_SIZE = 36; /** Serializable version identifier. */ private static final long serialVersionUID = -4602336630143123183L; /** Blocks of matrix entries. */ private final T blocks[][]; /** Number of rows of the matrix. */ private final int rows; /** Number of columns of the matrix. */ private final int columns; /** Number of block rows of the matrix. */ private final int blockRows; /** Number of block columns of the matrix. */ private final int blockColumns; /** * Create a new matrix with the supplied row and column dimensions. * * @param field Field to which the elements belong. * @param rows Number of rows in the new matrix. * @param columns Number of columns in the new matrix. * @throws NotStrictlyPositiveException if row or column dimension is not * positive. */ public BlockFieldMatrix(final Field field, final int rows, final int columns) throws NotStrictlyPositiveException { super(field, rows, columns); this.rows = rows; this.columns = columns; // number of blocks blockRows = (rows + BLOCK_SIZE - 1) / BLOCK_SIZE; blockColumns = (columns + BLOCK_SIZE - 1) / BLOCK_SIZE; // allocate storage blocks, taking care of smaller ones at right and bottom blocks = createBlocksLayout(field, rows, columns); } /** * Create a new dense matrix copying entries from raw layout data. *

                              The input array must already be in raw layout.

                              *

                              Calling this constructor is equivalent to call: *

                              matrix = new BlockFieldMatrix(getField(), rawData.length, rawData[0].length,
                                   *                                   toBlocksLayout(rawData), false);
                              *

                              * * @param rawData Data for the new matrix, in raw layout. * @throws DimensionMismatchException if the {@code blockData} shape is * inconsistent with block layout. * @see #BlockFieldMatrix(int, int, FieldElement[][], boolean) */ public BlockFieldMatrix(final T[][] rawData) throws DimensionMismatchException { this(rawData.length, rawData[0].length, toBlocksLayout(rawData), false); } /** * Create a new dense matrix copying entries from block layout data. *

                              The input array must already be in blocks layout.

                              * @param rows the number of rows in the new matrix * @param columns the number of columns in the new matrix * @param blockData data for new matrix * @param copyArray if true, the input array will be copied, otherwise * it will be referenced * * @throws DimensionMismatchException if the {@code blockData} shape is * inconsistent with block layout. * @throws NotStrictlyPositiveException if row or column dimension is not * positive. * @see #createBlocksLayout(Field, int, int) * @see #toBlocksLayout(FieldElement[][]) * @see #BlockFieldMatrix(FieldElement[][]) */ public BlockFieldMatrix(final int rows, final int columns, final T[][] blockData, final boolean copyArray) throws DimensionMismatchException, NotStrictlyPositiveException { super(extractField(blockData), rows, columns); this.rows = rows; this.columns = columns; // number of blocks blockRows = (rows + BLOCK_SIZE - 1) / BLOCK_SIZE; blockColumns = (columns + BLOCK_SIZE - 1) / BLOCK_SIZE; if (copyArray) { // allocate storage blocks, taking care of smaller ones at right and bottom blocks = MathArrays.buildArray(getField(), blockRows * blockColumns, -1); } else { // reference existing array blocks = blockData; } int index = 0; for (int iBlock = 0; iBlock < blockRows; ++iBlock) { final int iHeight = blockHeight(iBlock); for (int jBlock = 0; jBlock < blockColumns; ++jBlock, ++index) { if (blockData[index].length != iHeight * blockWidth(jBlock)) { throw new DimensionMismatchException(blockData[index].length, iHeight * blockWidth(jBlock)); } if (copyArray) { blocks[index] = blockData[index].clone(); } } } } /** * Convert a data array from raw layout to blocks layout. *

                              * Raw layout is the straightforward layout where element at row i and * column j is in array element rawData[i][j]. Blocks layout * is the layout used in {@link BlockFieldMatrix} instances, where the matrix * is split in square blocks (except at right and bottom side where blocks may * be rectangular to fit matrix size) and each block is stored in a flattened * one-dimensional array. *

                              *

                              * This method creates an array in blocks layout from an input array in raw layout. * It can be used to provide the array argument of the {@link * #BlockFieldMatrix(int, int, FieldElement[][], boolean)} * constructor. *

                              * @param Type of the field elements. * @param rawData Data array in raw layout. * @return a new data array containing the same entries but in blocks layout * @throws DimensionMismatchException if {@code rawData} is not rectangular * (not all rows have the same length). * @see #createBlocksLayout(Field, int, int) * @see #BlockFieldMatrix(int, int, FieldElement[][], boolean) */ public static > T[][] toBlocksLayout(final T[][] rawData) throws DimensionMismatchException { final int rows = rawData.length; final int columns = rawData[0].length; final int blockRows = (rows + BLOCK_SIZE - 1) / BLOCK_SIZE; final int blockColumns = (columns + BLOCK_SIZE - 1) / BLOCK_SIZE; // safety checks for (int i = 0; i < rawData.length; ++i) { final int length = rawData[i].length; if (length != columns) { throw new DimensionMismatchException(columns, length); } } // convert array final Field field = extractField(rawData); final T[][] blocks = MathArrays.buildArray(field, blockRows * blockColumns, -1); int blockIndex = 0; for (int iBlock = 0; iBlock < blockRows; ++iBlock) { final int pStart = iBlock * BLOCK_SIZE; final int pEnd = FastMath.min(pStart + BLOCK_SIZE, rows); final int iHeight = pEnd - pStart; for (int jBlock = 0; jBlock < blockColumns; ++jBlock) { final int qStart = jBlock * BLOCK_SIZE; final int qEnd = FastMath.min(qStart + BLOCK_SIZE, columns); final int jWidth = qEnd - qStart; // allocate new block final T[] block = MathArrays.buildArray(field, iHeight * jWidth); blocks[blockIndex] = block; // copy data int index = 0; for (int p = pStart; p < pEnd; ++p) { System.arraycopy(rawData[p], qStart, block, index, jWidth); index += jWidth; } ++blockIndex; } } return blocks; } /** * Create a data array in blocks layout. *

                              * This method can be used to create the array argument of the {@link * #BlockFieldMatrix(int, int, FieldElement[][], boolean)} * constructor. *

                              * @param Type of the field elements. * @param field Field to which the elements belong. * @param rows Number of rows in the new matrix. * @param columns Number of columns in the new matrix. * @return a new data array in blocks layout. * @see #toBlocksLayout(FieldElement[][]) * @see #BlockFieldMatrix(int, int, FieldElement[][], boolean) */ public static > T[][] createBlocksLayout(final Field field, final int rows, final int columns) { final int blockRows = (rows + BLOCK_SIZE - 1) / BLOCK_SIZE; final int blockColumns = (columns + BLOCK_SIZE - 1) / BLOCK_SIZE; final T[][] blocks = MathArrays.buildArray(field, blockRows * blockColumns, -1); int blockIndex = 0; for (int iBlock = 0; iBlock < blockRows; ++iBlock) { final int pStart = iBlock * BLOCK_SIZE; final int pEnd = FastMath.min(pStart + BLOCK_SIZE, rows); final int iHeight = pEnd - pStart; for (int jBlock = 0; jBlock < blockColumns; ++jBlock) { final int qStart = jBlock * BLOCK_SIZE; final int qEnd = FastMath.min(qStart + BLOCK_SIZE, columns); final int jWidth = qEnd - qStart; blocks[blockIndex] = MathArrays.buildArray(field, iHeight * jWidth); ++blockIndex; } } return blocks; } /** {@inheritDoc} */ @Override public FieldMatrix createMatrix(final int rowDimension, final int columnDimension) throws NotStrictlyPositiveException { return new BlockFieldMatrix(getField(), rowDimension, columnDimension); } /** {@inheritDoc} */ @Override public FieldMatrix copy() { // create an empty matrix BlockFieldMatrix copied = new BlockFieldMatrix(getField(), rows, columns); // copy the blocks for (int i = 0; i < blocks.length; ++i) { System.arraycopy(blocks[i], 0, copied.blocks[i], 0, blocks[i].length); } return copied; } /** {@inheritDoc} */ @Override public FieldMatrix add(final FieldMatrix m) throws MatrixDimensionMismatchException { try { return add((BlockFieldMatrix) m); } catch (ClassCastException cce) { // safety check checkAdditionCompatible(m); final BlockFieldMatrix out = new BlockFieldMatrix(getField(), rows, columns); // perform addition block-wise, to ensure good cache behavior int blockIndex = 0; for (int iBlock = 0; iBlock < out.blockRows; ++iBlock) { for (int jBlock = 0; jBlock < out.blockColumns; ++jBlock) { // perform addition on the current block final T[] outBlock = out.blocks[blockIndex]; final T[] tBlock = blocks[blockIndex]; final int pStart = iBlock * BLOCK_SIZE; final int pEnd = FastMath.min(pStart + BLOCK_SIZE, rows); final int qStart = jBlock * BLOCK_SIZE; final int qEnd = FastMath.min(qStart + BLOCK_SIZE, columns); int k = 0; for (int p = pStart; p < pEnd; ++p) { for (int q = qStart; q < qEnd; ++q) { outBlock[k] = tBlock[k].add(m.getEntry(p, q)); ++k; } } // go to next block ++blockIndex; } } return out; } } /** * Compute the sum of {@code this} and {@code m}. * * @param m matrix to be added * @return {@code this + m} * @throws MatrixDimensionMismatchException if {@code m} is not the same * size as {@code this} */ public BlockFieldMatrix add(final BlockFieldMatrix m) throws MatrixDimensionMismatchException { // safety check checkAdditionCompatible(m); final BlockFieldMatrix out = new BlockFieldMatrix(getField(), rows, columns); // perform addition block-wise, to ensure good cache behavior for (int blockIndex = 0; blockIndex < out.blocks.length; ++blockIndex) { final T[] outBlock = out.blocks[blockIndex]; final T[] tBlock = blocks[blockIndex]; final T[] mBlock = m.blocks[blockIndex]; for (int k = 0; k < outBlock.length; ++k) { outBlock[k] = tBlock[k].add(mBlock[k]); } } return out; } /** {@inheritDoc} */ @Override public FieldMatrix subtract(final FieldMatrix m) throws MatrixDimensionMismatchException { try { return subtract((BlockFieldMatrix) m); } catch (ClassCastException cce) { // safety check checkSubtractionCompatible(m); final BlockFieldMatrix out = new BlockFieldMatrix(getField(), rows, columns); // perform subtraction block-wise, to ensure good cache behavior int blockIndex = 0; for (int iBlock = 0; iBlock < out.blockRows; ++iBlock) { for (int jBlock = 0; jBlock < out.blockColumns; ++jBlock) { // perform subtraction on the current block final T[] outBlock = out.blocks[blockIndex]; final T[] tBlock = blocks[blockIndex]; final int pStart = iBlock * BLOCK_SIZE; final int pEnd = FastMath.min(pStart + BLOCK_SIZE, rows); final int qStart = jBlock * BLOCK_SIZE; final int qEnd = FastMath.min(qStart + BLOCK_SIZE, columns); int k = 0; for (int p = pStart; p < pEnd; ++p) { for (int q = qStart; q < qEnd; ++q) { outBlock[k] = tBlock[k].subtract(m.getEntry(p, q)); ++k; } } // go to next block ++blockIndex; } } return out; } } /** * Compute {@code this - m}. * * @param m matrix to be subtracted * @return {@code this - m} * @throws MatrixDimensionMismatchException if {@code m} is not the same * size as {@code this} */ public BlockFieldMatrix subtract(final BlockFieldMatrix m) throws MatrixDimensionMismatchException { // safety check checkSubtractionCompatible(m); final BlockFieldMatrix out = new BlockFieldMatrix(getField(), rows, columns); // perform subtraction block-wise, to ensure good cache behavior for (int blockIndex = 0; blockIndex < out.blocks.length; ++blockIndex) { final T[] outBlock = out.blocks[blockIndex]; final T[] tBlock = blocks[blockIndex]; final T[] mBlock = m.blocks[blockIndex]; for (int k = 0; k < outBlock.length; ++k) { outBlock[k] = tBlock[k].subtract(mBlock[k]); } } return out; } /** {@inheritDoc} */ @Override public FieldMatrix scalarAdd(final T d) { final BlockFieldMatrix out = new BlockFieldMatrix(getField(), rows, columns); // perform subtraction block-wise, to ensure good cache behavior for (int blockIndex = 0; blockIndex < out.blocks.length; ++blockIndex) { final T[] outBlock = out.blocks[blockIndex]; final T[] tBlock = blocks[blockIndex]; for (int k = 0; k < outBlock.length; ++k) { outBlock[k] = tBlock[k].add(d); } } return out; } /** {@inheritDoc} */ @Override public FieldMatrix scalarMultiply(final T d) { final BlockFieldMatrix out = new BlockFieldMatrix(getField(), rows, columns); // perform subtraction block-wise, to ensure good cache behavior for (int blockIndex = 0; blockIndex < out.blocks.length; ++blockIndex) { final T[] outBlock = out.blocks[blockIndex]; final T[] tBlock = blocks[blockIndex]; for (int k = 0; k < outBlock.length; ++k) { outBlock[k] = tBlock[k].multiply(d); } } return out; } /** {@inheritDoc} */ @Override public FieldMatrix multiply(final FieldMatrix m) throws DimensionMismatchException { try { return multiply((BlockFieldMatrix) m); } catch (ClassCastException cce) { // safety check checkMultiplicationCompatible(m); final BlockFieldMatrix out = new BlockFieldMatrix(getField(), rows, m.getColumnDimension()); final T zero = getField().getZero(); // perform multiplication block-wise, to ensure good cache behavior int blockIndex = 0; for (int iBlock = 0; iBlock < out.blockRows; ++iBlock) { final int pStart = iBlock * BLOCK_SIZE; final int pEnd = FastMath.min(pStart + BLOCK_SIZE, rows); for (int jBlock = 0; jBlock < out.blockColumns; ++jBlock) { final int qStart = jBlock * BLOCK_SIZE; final int qEnd = FastMath.min(qStart + BLOCK_SIZE, m.getColumnDimension()); // select current block final T[] outBlock = out.blocks[blockIndex]; // perform multiplication on current block for (int kBlock = 0; kBlock < blockColumns; ++kBlock) { final int kWidth = blockWidth(kBlock); final T[] tBlock = blocks[iBlock * blockColumns + kBlock]; final int rStart = kBlock * BLOCK_SIZE; int k = 0; for (int p = pStart; p < pEnd; ++p) { final int lStart = (p - pStart) * kWidth; final int lEnd = lStart + kWidth; for (int q = qStart; q < qEnd; ++q) { T sum = zero; int r = rStart; for (int l = lStart; l < lEnd; ++l) { sum = sum.add(tBlock[l].multiply(m.getEntry(r, q))); ++r; } outBlock[k] = outBlock[k].add(sum); ++k; } } } // go to next block ++blockIndex; } } return out; } } /** * Returns the result of postmultiplying {@code this} by {@code m}. * * @param m matrix to postmultiply by * @return {@code this * m} * @throws DimensionMismatchException if the matrices are not compatible. */ public BlockFieldMatrix multiply(BlockFieldMatrix m) throws DimensionMismatchException { // safety check checkMultiplicationCompatible(m); final BlockFieldMatrix out = new BlockFieldMatrix(getField(), rows, m.columns); final T zero = getField().getZero(); // perform multiplication block-wise, to ensure good cache behavior int blockIndex = 0; for (int iBlock = 0; iBlock < out.blockRows; ++iBlock) { final int pStart = iBlock * BLOCK_SIZE; final int pEnd = FastMath.min(pStart + BLOCK_SIZE, rows); for (int jBlock = 0; jBlock < out.blockColumns; ++jBlock) { final int jWidth = out.blockWidth(jBlock); final int jWidth2 = jWidth + jWidth; final int jWidth3 = jWidth2 + jWidth; final int jWidth4 = jWidth3 + jWidth; // select current block final T[] outBlock = out.blocks[blockIndex]; // perform multiplication on current block for (int kBlock = 0; kBlock < blockColumns; ++kBlock) { final int kWidth = blockWidth(kBlock); final T[] tBlock = blocks[iBlock * blockColumns + kBlock]; final T[] mBlock = m.blocks[kBlock * m.blockColumns + jBlock]; int k = 0; for (int p = pStart; p < pEnd; ++p) { final int lStart = (p - pStart) * kWidth; final int lEnd = lStart + kWidth; for (int nStart = 0; nStart < jWidth; ++nStart) { T sum = zero; int l = lStart; int n = nStart; while (l < lEnd - 3) { sum = sum. add(tBlock[l].multiply(mBlock[n])). add(tBlock[l + 1].multiply(mBlock[n + jWidth])). add(tBlock[l + 2].multiply(mBlock[n + jWidth2])). add(tBlock[l + 3].multiply(mBlock[n + jWidth3])); l += 4; n += jWidth4; } while (l < lEnd) { sum = sum.add(tBlock[l++].multiply(mBlock[n])); n += jWidth; } outBlock[k] = outBlock[k].add(sum); ++k; } } } // go to next block ++blockIndex; } } return out; } /** {@inheritDoc} */ @Override public T[][] getData() { final T[][] data = MathArrays.buildArray(getField(), getRowDimension(), getColumnDimension()); final int lastColumns = columns - (blockColumns - 1) * BLOCK_SIZE; for (int iBlock = 0; iBlock < blockRows; ++iBlock) { final int pStart = iBlock * BLOCK_SIZE; final int pEnd = FastMath.min(pStart + BLOCK_SIZE, rows); int regularPos = 0; int lastPos = 0; for (int p = pStart; p < pEnd; ++p) { final T[] dataP = data[p]; int blockIndex = iBlock * blockColumns; int dataPos = 0; for (int jBlock = 0; jBlock < blockColumns - 1; ++jBlock) { System.arraycopy(blocks[blockIndex++], regularPos, dataP, dataPos, BLOCK_SIZE); dataPos += BLOCK_SIZE; } System.arraycopy(blocks[blockIndex], lastPos, dataP, dataPos, lastColumns); regularPos += BLOCK_SIZE; lastPos += lastColumns; } } return data; } /** {@inheritDoc} */ @Override public FieldMatrix getSubMatrix(final int startRow, final int endRow, final int startColumn, final int endColumn) throws OutOfRangeException, NumberIsTooSmallException { // safety checks checkSubMatrixIndex(startRow, endRow, startColumn, endColumn); // create the output matrix final BlockFieldMatrix out = new BlockFieldMatrix(getField(), endRow - startRow + 1, endColumn - startColumn + 1); // compute blocks shifts final int blockStartRow = startRow / BLOCK_SIZE; final int rowsShift = startRow % BLOCK_SIZE; final int blockStartColumn = startColumn / BLOCK_SIZE; final int columnsShift = startColumn % BLOCK_SIZE; // perform extraction block-wise, to ensure good cache behavior int pBlock = blockStartRow; for (int iBlock = 0; iBlock < out.blockRows; ++iBlock) { final int iHeight = out.blockHeight(iBlock); int qBlock = blockStartColumn; for (int jBlock = 0; jBlock < out.blockColumns; ++jBlock) { final int jWidth = out.blockWidth(jBlock); // handle one block of the output matrix final int outIndex = iBlock * out.blockColumns + jBlock; final T[] outBlock = out.blocks[outIndex]; final int index = pBlock * blockColumns + qBlock; final int width = blockWidth(qBlock); final int heightExcess = iHeight + rowsShift - BLOCK_SIZE; final int widthExcess = jWidth + columnsShift - BLOCK_SIZE; if (heightExcess > 0) { // the submatrix block spans on two blocks rows from the original matrix if (widthExcess > 0) { // the submatrix block spans on two blocks columns from the original matrix final int width2 = blockWidth(qBlock + 1); copyBlockPart(blocks[index], width, rowsShift, BLOCK_SIZE, columnsShift, BLOCK_SIZE, outBlock, jWidth, 0, 0); copyBlockPart(blocks[index + 1], width2, rowsShift, BLOCK_SIZE, 0, widthExcess, outBlock, jWidth, 0, jWidth - widthExcess); copyBlockPart(blocks[index + blockColumns], width, 0, heightExcess, columnsShift, BLOCK_SIZE, outBlock, jWidth, iHeight - heightExcess, 0); copyBlockPart(blocks[index + blockColumns + 1], width2, 0, heightExcess, 0, widthExcess, outBlock, jWidth, iHeight - heightExcess, jWidth - widthExcess); } else { // the submatrix block spans on one block column from the original matrix copyBlockPart(blocks[index], width, rowsShift, BLOCK_SIZE, columnsShift, jWidth + columnsShift, outBlock, jWidth, 0, 0); copyBlockPart(blocks[index + blockColumns], width, 0, heightExcess, columnsShift, jWidth + columnsShift, outBlock, jWidth, iHeight - heightExcess, 0); } } else { // the submatrix block spans on one block row from the original matrix if (widthExcess > 0) { // the submatrix block spans on two blocks columns from the original matrix final int width2 = blockWidth(qBlock + 1); copyBlockPart(blocks[index], width, rowsShift, iHeight + rowsShift, columnsShift, BLOCK_SIZE, outBlock, jWidth, 0, 0); copyBlockPart(blocks[index + 1], width2, rowsShift, iHeight + rowsShift, 0, widthExcess, outBlock, jWidth, 0, jWidth - widthExcess); } else { // the submatrix block spans on one block column from the original matrix copyBlockPart(blocks[index], width, rowsShift, iHeight + rowsShift, columnsShift, jWidth + columnsShift, outBlock, jWidth, 0, 0); } } ++qBlock; } ++pBlock; } return out; } /** * Copy a part of a block into another one *

                              This method can be called only when the specified part fits in both * blocks, no verification is done here.

                              * @param srcBlock source block * @param srcWidth source block width ({@link #BLOCK_SIZE} or smaller) * @param srcStartRow start row in the source block * @param srcEndRow end row (exclusive) in the source block * @param srcStartColumn start column in the source block * @param srcEndColumn end column (exclusive) in the source block * @param dstBlock destination block * @param dstWidth destination block width ({@link #BLOCK_SIZE} or smaller) * @param dstStartRow start row in the destination block * @param dstStartColumn start column in the destination block */ private void copyBlockPart(final T[] srcBlock, final int srcWidth, final int srcStartRow, final int srcEndRow, final int srcStartColumn, final int srcEndColumn, final T[] dstBlock, final int dstWidth, final int dstStartRow, final int dstStartColumn) { final int length = srcEndColumn - srcStartColumn; int srcPos = srcStartRow * srcWidth + srcStartColumn; int dstPos = dstStartRow * dstWidth + dstStartColumn; for (int srcRow = srcStartRow; srcRow < srcEndRow; ++srcRow) { System.arraycopy(srcBlock, srcPos, dstBlock, dstPos, length); srcPos += srcWidth; dstPos += dstWidth; } } /** {@inheritDoc} */ @Override public void setSubMatrix(final T[][] subMatrix, final int row, final int column) throws DimensionMismatchException, OutOfRangeException, NoDataException, NullArgumentException { // safety checks MathUtils.checkNotNull(subMatrix); final int refLength = subMatrix[0].length; if (refLength == 0) { throw new NoDataException(LocalizedFormats.AT_LEAST_ONE_COLUMN); } final int endRow = row + subMatrix.length - 1; final int endColumn = column + refLength - 1; checkSubMatrixIndex(row, endRow, column, endColumn); for (final T[] subRow : subMatrix) { if (subRow.length != refLength) { throw new DimensionMismatchException(refLength, subRow.length); } } // compute blocks bounds final int blockStartRow = row / BLOCK_SIZE; final int blockEndRow = (endRow + BLOCK_SIZE) / BLOCK_SIZE; final int blockStartColumn = column / BLOCK_SIZE; final int blockEndColumn = (endColumn + BLOCK_SIZE) / BLOCK_SIZE; // perform copy block-wise, to ensure good cache behavior for (int iBlock = blockStartRow; iBlock < blockEndRow; ++iBlock) { final int iHeight = blockHeight(iBlock); final int firstRow = iBlock * BLOCK_SIZE; final int iStart = FastMath.max(row, firstRow); final int iEnd = FastMath.min(endRow + 1, firstRow + iHeight); for (int jBlock = blockStartColumn; jBlock < blockEndColumn; ++jBlock) { final int jWidth = blockWidth(jBlock); final int firstColumn = jBlock * BLOCK_SIZE; final int jStart = FastMath.max(column, firstColumn); final int jEnd = FastMath.min(endColumn + 1, firstColumn + jWidth); final int jLength = jEnd - jStart; // handle one block, row by row final T[] block = blocks[iBlock * blockColumns + jBlock]; for (int i = iStart; i < iEnd; ++i) { System.arraycopy(subMatrix[i - row], jStart - column, block, (i - firstRow) * jWidth + (jStart - firstColumn), jLength); } } } } /** {@inheritDoc} */ @Override public FieldMatrix getRowMatrix(final int row) throws OutOfRangeException { checkRowIndex(row); final BlockFieldMatrix out = new BlockFieldMatrix(getField(), 1, columns); // perform copy block-wise, to ensure good cache behavior final int iBlock = row / BLOCK_SIZE; final int iRow = row - iBlock * BLOCK_SIZE; int outBlockIndex = 0; int outIndex = 0; T[] outBlock = out.blocks[outBlockIndex]; for (int jBlock = 0; jBlock < blockColumns; ++jBlock) { final int jWidth = blockWidth(jBlock); final T[] block = blocks[iBlock * blockColumns + jBlock]; final int available = outBlock.length - outIndex; if (jWidth > available) { System.arraycopy(block, iRow * jWidth, outBlock, outIndex, available); outBlock = out.blocks[++outBlockIndex]; System.arraycopy(block, iRow * jWidth, outBlock, 0, jWidth - available); outIndex = jWidth - available; } else { System.arraycopy(block, iRow * jWidth, outBlock, outIndex, jWidth); outIndex += jWidth; } } return out; } /** {@inheritDoc} */ @Override public void setRowMatrix(final int row, final FieldMatrix matrix) throws MatrixDimensionMismatchException, OutOfRangeException { try { setRowMatrix(row, (BlockFieldMatrix) matrix); } catch (ClassCastException cce) { super.setRowMatrix(row, matrix); } } /** * Sets the entries in row number row * as a row matrix. Row indices start at 0. * * @param row the row to be set * @param matrix row matrix (must have one row and the same number of columns * as the instance) * @throws MatrixDimensionMismatchException if the matrix dimensions do * not match one instance row. * @throws OutOfRangeException if the specified row index is invalid. */ public void setRowMatrix(final int row, final BlockFieldMatrix matrix) throws MatrixDimensionMismatchException, OutOfRangeException { checkRowIndex(row); final int nCols = getColumnDimension(); if ((matrix.getRowDimension() != 1) || (matrix.getColumnDimension() != nCols)) { throw new MatrixDimensionMismatchException(matrix.getRowDimension(), matrix.getColumnDimension(), 1, nCols); } // perform copy block-wise, to ensure good cache behavior final int iBlock = row / BLOCK_SIZE; final int iRow = row - iBlock * BLOCK_SIZE; int mBlockIndex = 0; int mIndex = 0; T[] mBlock = matrix.blocks[mBlockIndex]; for (int jBlock = 0; jBlock < blockColumns; ++jBlock) { final int jWidth = blockWidth(jBlock); final T[] block = blocks[iBlock * blockColumns + jBlock]; final int available = mBlock.length - mIndex; if (jWidth > available) { System.arraycopy(mBlock, mIndex, block, iRow * jWidth, available); mBlock = matrix.blocks[++mBlockIndex]; System.arraycopy(mBlock, 0, block, iRow * jWidth, jWidth - available); mIndex = jWidth - available; } else { System.arraycopy(mBlock, mIndex, block, iRow * jWidth, jWidth); mIndex += jWidth; } } } /** {@inheritDoc} */ @Override public FieldMatrix getColumnMatrix(final int column) throws OutOfRangeException { checkColumnIndex(column); final BlockFieldMatrix out = new BlockFieldMatrix(getField(), rows, 1); // perform copy block-wise, to ensure good cache behavior final int jBlock = column / BLOCK_SIZE; final int jColumn = column - jBlock * BLOCK_SIZE; final int jWidth = blockWidth(jBlock); int outBlockIndex = 0; int outIndex = 0; T[] outBlock = out.blocks[outBlockIndex]; for (int iBlock = 0; iBlock < blockRows; ++iBlock) { final int iHeight = blockHeight(iBlock); final T[] block = blocks[iBlock * blockColumns + jBlock]; for (int i = 0; i < iHeight; ++i) { if (outIndex >= outBlock.length) { outBlock = out.blocks[++outBlockIndex]; outIndex = 0; } outBlock[outIndex++] = block[i * jWidth + jColumn]; } } return out; } /** {@inheritDoc} */ @Override public void setColumnMatrix(final int column, final FieldMatrix matrix) throws MatrixDimensionMismatchException, OutOfRangeException { try { setColumnMatrix(column, (BlockFieldMatrix) matrix); } catch (ClassCastException cce) { super.setColumnMatrix(column, matrix); } } /** * Sets the entries in column number {@code column} * as a column matrix. Column indices start at 0. * * @param column Column to be set. * @param matrix Column matrix (must have one column and the same number of rows * as the instance). * @throws MatrixDimensionMismatchException if the matrix dimensions do * not match one instance column. * @throws OutOfRangeException if the specified column index is invalid. */ void setColumnMatrix(final int column, final BlockFieldMatrix matrix) throws MatrixDimensionMismatchException, OutOfRangeException { checkColumnIndex(column); final int nRows = getRowDimension(); if ((matrix.getRowDimension() != nRows) || (matrix.getColumnDimension() != 1)) { throw new MatrixDimensionMismatchException(matrix.getRowDimension(), matrix.getColumnDimension(), nRows, 1); } // perform copy block-wise, to ensure good cache behavior final int jBlock = column / BLOCK_SIZE; final int jColumn = column - jBlock * BLOCK_SIZE; final int jWidth = blockWidth(jBlock); int mBlockIndex = 0; int mIndex = 0; T[] mBlock = matrix.blocks[mBlockIndex]; for (int iBlock = 0; iBlock < blockRows; ++iBlock) { final int iHeight = blockHeight(iBlock); final T[] block = blocks[iBlock * blockColumns + jBlock]; for (int i = 0; i < iHeight; ++i) { if (mIndex >= mBlock.length) { mBlock = matrix.blocks[++mBlockIndex]; mIndex = 0; } block[i * jWidth + jColumn] = mBlock[mIndex++]; } } } /** {@inheritDoc} */ @Override public FieldVector getRowVector(final int row) throws OutOfRangeException { checkRowIndex(row); final T[] outData = MathArrays.buildArray(getField(), columns); // perform copy block-wise, to ensure good cache behavior final int iBlock = row / BLOCK_SIZE; final int iRow = row - iBlock * BLOCK_SIZE; int outIndex = 0; for (int jBlock = 0; jBlock < blockColumns; ++jBlock) { final int jWidth = blockWidth(jBlock); final T[] block = blocks[iBlock * blockColumns + jBlock]; System.arraycopy(block, iRow * jWidth, outData, outIndex, jWidth); outIndex += jWidth; } return new ArrayFieldVector(getField(), outData, false); } /** {@inheritDoc} */ @Override public void setRowVector(final int row, final FieldVector vector) throws MatrixDimensionMismatchException, OutOfRangeException { try { setRow(row, ((ArrayFieldVector) vector).getDataRef()); } catch (ClassCastException cce) { super.setRowVector(row, vector); } } /** {@inheritDoc} */ @Override public FieldVector getColumnVector(final int column) throws OutOfRangeException { checkColumnIndex(column); final T[] outData = MathArrays.buildArray(getField(), rows); // perform copy block-wise, to ensure good cache behavior final int jBlock = column / BLOCK_SIZE; final int jColumn = column - jBlock * BLOCK_SIZE; final int jWidth = blockWidth(jBlock); int outIndex = 0; for (int iBlock = 0; iBlock < blockRows; ++iBlock) { final int iHeight = blockHeight(iBlock); final T[] block = blocks[iBlock * blockColumns + jBlock]; for (int i = 0; i < iHeight; ++i) { outData[outIndex++] = block[i * jWidth + jColumn]; } } return new ArrayFieldVector(getField(), outData, false); } /** {@inheritDoc} */ @Override public void setColumnVector(final int column, final FieldVector vector) throws OutOfRangeException, MatrixDimensionMismatchException { try { setColumn(column, ((ArrayFieldVector) vector).getDataRef()); } catch (ClassCastException cce) { super.setColumnVector(column, vector); } } /** {@inheritDoc} */ @Override public T[] getRow(final int row) throws OutOfRangeException { checkRowIndex(row); final T[] out = MathArrays.buildArray(getField(), columns); // perform copy block-wise, to ensure good cache behavior final int iBlock = row / BLOCK_SIZE; final int iRow = row - iBlock * BLOCK_SIZE; int outIndex = 0; for (int jBlock = 0; jBlock < blockColumns; ++jBlock) { final int jWidth = blockWidth(jBlock); final T[] block = blocks[iBlock * blockColumns + jBlock]; System.arraycopy(block, iRow * jWidth, out, outIndex, jWidth); outIndex += jWidth; } return out; } /** {@inheritDoc} */ @Override public void setRow(final int row, final T[] array) throws OutOfRangeException, MatrixDimensionMismatchException { checkRowIndex(row); final int nCols = getColumnDimension(); if (array.length != nCols) { throw new MatrixDimensionMismatchException(1, array.length, 1, nCols); } // perform copy block-wise, to ensure good cache behavior final int iBlock = row / BLOCK_SIZE; final int iRow = row - iBlock * BLOCK_SIZE; int outIndex = 0; for (int jBlock = 0; jBlock < blockColumns; ++jBlock) { final int jWidth = blockWidth(jBlock); final T[] block = blocks[iBlock * blockColumns + jBlock]; System.arraycopy(array, outIndex, block, iRow * jWidth, jWidth); outIndex += jWidth; } } /** {@inheritDoc} */ @Override public T[] getColumn(final int column) throws OutOfRangeException { checkColumnIndex(column); final T[] out = MathArrays.buildArray(getField(), rows); // perform copy block-wise, to ensure good cache behavior final int jBlock = column / BLOCK_SIZE; final int jColumn = column - jBlock * BLOCK_SIZE; final int jWidth = blockWidth(jBlock); int outIndex = 0; for (int iBlock = 0; iBlock < blockRows; ++iBlock) { final int iHeight = blockHeight(iBlock); final T[] block = blocks[iBlock * blockColumns + jBlock]; for (int i = 0; i < iHeight; ++i) { out[outIndex++] = block[i * jWidth + jColumn]; } } return out; } /** {@inheritDoc} */ @Override public void setColumn(final int column, final T[] array) throws MatrixDimensionMismatchException, OutOfRangeException { checkColumnIndex(column); final int nRows = getRowDimension(); if (array.length != nRows) { throw new MatrixDimensionMismatchException(array.length, 1, nRows, 1); } // perform copy block-wise, to ensure good cache behavior final int jBlock = column / BLOCK_SIZE; final int jColumn = column - jBlock * BLOCK_SIZE; final int jWidth = blockWidth(jBlock); int outIndex = 0; for (int iBlock = 0; iBlock < blockRows; ++iBlock) { final int iHeight = blockHeight(iBlock); final T[] block = blocks[iBlock * blockColumns + jBlock]; for (int i = 0; i < iHeight; ++i) { block[i * jWidth + jColumn] = array[outIndex++]; } } } /** {@inheritDoc} */ @Override public T getEntry(final int row, final int column) throws OutOfRangeException { checkRowIndex(row); checkColumnIndex(column); final int iBlock = row / BLOCK_SIZE; final int jBlock = column / BLOCK_SIZE; final int k = (row - iBlock * BLOCK_SIZE) * blockWidth(jBlock) + (column - jBlock * BLOCK_SIZE); return blocks[iBlock * blockColumns + jBlock][k]; } /** {@inheritDoc} */ @Override public void setEntry(final int row, final int column, final T value) throws OutOfRangeException { checkRowIndex(row); checkColumnIndex(column); final int iBlock = row / BLOCK_SIZE; final int jBlock = column / BLOCK_SIZE; final int k = (row - iBlock * BLOCK_SIZE) * blockWidth(jBlock) + (column - jBlock * BLOCK_SIZE); blocks[iBlock * blockColumns + jBlock][k] = value; } /** {@inheritDoc} */ @Override public void addToEntry(final int row, final int column, final T increment) throws OutOfRangeException { checkRowIndex(row); checkColumnIndex(column); final int iBlock = row / BLOCK_SIZE; final int jBlock = column / BLOCK_SIZE; final int k = (row - iBlock * BLOCK_SIZE) * blockWidth(jBlock) + (column - jBlock * BLOCK_SIZE); final T[] blockIJ = blocks[iBlock * blockColumns + jBlock]; blockIJ[k] = blockIJ[k].add(increment); } /** {@inheritDoc} */ @Override public void multiplyEntry(final int row, final int column, final T factor) throws OutOfRangeException { checkRowIndex(row); checkColumnIndex(column); final int iBlock = row / BLOCK_SIZE; final int jBlock = column / BLOCK_SIZE; final int k = (row - iBlock * BLOCK_SIZE) * blockWidth(jBlock) + (column - jBlock * BLOCK_SIZE); final T[] blockIJ = blocks[iBlock * blockColumns + jBlock]; blockIJ[k] = blockIJ[k].multiply(factor); } /** {@inheritDoc} */ @Override public FieldMatrix transpose() { final int nRows = getRowDimension(); final int nCols = getColumnDimension(); final BlockFieldMatrix out = new BlockFieldMatrix(getField(), nCols, nRows); // perform transpose block-wise, to ensure good cache behavior int blockIndex = 0; for (int iBlock = 0; iBlock < blockColumns; ++iBlock) { for (int jBlock = 0; jBlock < blockRows; ++jBlock) { // transpose current block final T[] outBlock = out.blocks[blockIndex]; final T[] tBlock = blocks[jBlock * blockColumns + iBlock]; final int pStart = iBlock * BLOCK_SIZE; final int pEnd = FastMath.min(pStart + BLOCK_SIZE, columns); final int qStart = jBlock * BLOCK_SIZE; final int qEnd = FastMath.min(qStart + BLOCK_SIZE, rows); int k = 0; for (int p = pStart; p < pEnd; ++p) { final int lInc = pEnd - pStart; int l = p - pStart; for (int q = qStart; q < qEnd; ++q) { outBlock[k] = tBlock[l]; ++k; l+= lInc; } } // go to next block ++blockIndex; } } return out; } /** {@inheritDoc} */ @Override public int getRowDimension() { return rows; } /** {@inheritDoc} */ @Override public int getColumnDimension() { return columns; } /** {@inheritDoc} */ @Override public T[] operate(final T[] v) throws DimensionMismatchException { if (v.length != columns) { throw new DimensionMismatchException(v.length, columns); } final T[] out = MathArrays.buildArray(getField(), rows); final T zero = getField().getZero(); // perform multiplication block-wise, to ensure good cache behavior for (int iBlock = 0; iBlock < blockRows; ++iBlock) { final int pStart = iBlock * BLOCK_SIZE; final int pEnd = FastMath.min(pStart + BLOCK_SIZE, rows); for (int jBlock = 0; jBlock < blockColumns; ++jBlock) { final T[] block = blocks[iBlock * blockColumns + jBlock]; final int qStart = jBlock * BLOCK_SIZE; final int qEnd = FastMath.min(qStart + BLOCK_SIZE, columns); int k = 0; for (int p = pStart; p < pEnd; ++p) { T sum = zero; int q = qStart; while (q < qEnd - 3) { sum = sum. add(block[k].multiply(v[q])). add(block[k + 1].multiply(v[q + 1])). add(block[k + 2].multiply(v[q + 2])). add(block[k + 3].multiply(v[q + 3])); k += 4; q += 4; } while (q < qEnd) { sum = sum.add(block[k++].multiply(v[q++])); } out[p] = out[p].add(sum); } } } return out; } /** {@inheritDoc} */ @Override public T[] preMultiply(final T[] v) throws DimensionMismatchException { if (v.length != rows) { throw new DimensionMismatchException(v.length, rows); } final T[] out = MathArrays.buildArray(getField(), columns); final T zero = getField().getZero(); // perform multiplication block-wise, to ensure good cache behavior for (int jBlock = 0; jBlock < blockColumns; ++jBlock) { final int jWidth = blockWidth(jBlock); final int jWidth2 = jWidth + jWidth; final int jWidth3 = jWidth2 + jWidth; final int jWidth4 = jWidth3 + jWidth; final int qStart = jBlock * BLOCK_SIZE; final int qEnd = FastMath.min(qStart + BLOCK_SIZE, columns); for (int iBlock = 0; iBlock < blockRows; ++iBlock) { final T[] block = blocks[iBlock * blockColumns + jBlock]; final int pStart = iBlock * BLOCK_SIZE; final int pEnd = FastMath.min(pStart + BLOCK_SIZE, rows); for (int q = qStart; q < qEnd; ++q) { int k = q - qStart; T sum = zero; int p = pStart; while (p < pEnd - 3) { sum = sum. add(block[k].multiply(v[p])). add(block[k + jWidth].multiply(v[p + 1])). add(block[k + jWidth2].multiply(v[p + 2])). add(block[k + jWidth3].multiply(v[p + 3])); k += jWidth4; p += 4; } while (p < pEnd) { sum = sum.add(block[k].multiply(v[p++])); k += jWidth; } out[q] = out[q].add(sum); } } } return out; } /** {@inheritDoc} */ @Override public T walkInRowOrder(final FieldMatrixChangingVisitor visitor) { visitor.start(rows, columns, 0, rows - 1, 0, columns - 1); for (int iBlock = 0; iBlock < blockRows; ++iBlock) { final int pStart = iBlock * BLOCK_SIZE; final int pEnd = FastMath.min(pStart + BLOCK_SIZE, rows); for (int p = pStart; p < pEnd; ++p) { for (int jBlock = 0; jBlock < blockColumns; ++jBlock) { final int jWidth = blockWidth(jBlock); final int qStart = jBlock * BLOCK_SIZE; final int qEnd = FastMath.min(qStart + BLOCK_SIZE, columns); final T[] block = blocks[iBlock * blockColumns + jBlock]; int k = (p - pStart) * jWidth; for (int q = qStart; q < qEnd; ++q) { block[k] = visitor.visit(p, q, block[k]); ++k; } } } } return visitor.end(); } /** {@inheritDoc} */ @Override public T walkInRowOrder(final FieldMatrixPreservingVisitor visitor) { visitor.start(rows, columns, 0, rows - 1, 0, columns - 1); for (int iBlock = 0; iBlock < blockRows; ++iBlock) { final int pStart = iBlock * BLOCK_SIZE; final int pEnd = FastMath.min(pStart + BLOCK_SIZE, rows); for (int p = pStart; p < pEnd; ++p) { for (int jBlock = 0; jBlock < blockColumns; ++jBlock) { final int jWidth = blockWidth(jBlock); final int qStart = jBlock * BLOCK_SIZE; final int qEnd = FastMath.min(qStart + BLOCK_SIZE, columns); final T[] block = blocks[iBlock * blockColumns + jBlock]; int k = (p - pStart) * jWidth; for (int q = qStart; q < qEnd; ++q) { visitor.visit(p, q, block[k]); ++k; } } } } return visitor.end(); } /** {@inheritDoc} */ @Override public T walkInRowOrder(final FieldMatrixChangingVisitor visitor, final int startRow, final int endRow, final int startColumn, final int endColumn) throws OutOfRangeException, NumberIsTooSmallException { checkSubMatrixIndex(startRow, endRow, startColumn, endColumn); visitor.start(rows, columns, startRow, endRow, startColumn, endColumn); for (int iBlock = startRow / BLOCK_SIZE; iBlock < 1 + endRow / BLOCK_SIZE; ++iBlock) { final int p0 = iBlock * BLOCK_SIZE; final int pStart = FastMath.max(startRow, p0); final int pEnd = FastMath.min((iBlock + 1) * BLOCK_SIZE, 1 + endRow); for (int p = pStart; p < pEnd; ++p) { for (int jBlock = startColumn / BLOCK_SIZE; jBlock < 1 + endColumn / BLOCK_SIZE; ++jBlock) { final int jWidth = blockWidth(jBlock); final int q0 = jBlock * BLOCK_SIZE; final int qStart = FastMath.max(startColumn, q0); final int qEnd = FastMath.min((jBlock + 1) * BLOCK_SIZE, 1 + endColumn); final T[] block = blocks[iBlock * blockColumns + jBlock]; int k = (p - p0) * jWidth + qStart - q0; for (int q = qStart; q < qEnd; ++q) { block[k] = visitor.visit(p, q, block[k]); ++k; } } } } return visitor.end(); } /** {@inheritDoc} */ @Override public T walkInRowOrder(final FieldMatrixPreservingVisitor visitor, final int startRow, final int endRow, final int startColumn, final int endColumn) throws OutOfRangeException, NumberIsTooSmallException { checkSubMatrixIndex(startRow, endRow, startColumn, endColumn); visitor.start(rows, columns, startRow, endRow, startColumn, endColumn); for (int iBlock = startRow / BLOCK_SIZE; iBlock < 1 + endRow / BLOCK_SIZE; ++iBlock) { final int p0 = iBlock * BLOCK_SIZE; final int pStart = FastMath.max(startRow, p0); final int pEnd = FastMath.min((iBlock + 1) * BLOCK_SIZE, 1 + endRow); for (int p = pStart; p < pEnd; ++p) { for (int jBlock = startColumn / BLOCK_SIZE; jBlock < 1 + endColumn / BLOCK_SIZE; ++jBlock) { final int jWidth = blockWidth(jBlock); final int q0 = jBlock * BLOCK_SIZE; final int qStart = FastMath.max(startColumn, q0); final int qEnd = FastMath.min((jBlock + 1) * BLOCK_SIZE, 1 + endColumn); final T[] block = blocks[iBlock * blockColumns + jBlock]; int k = (p - p0) * jWidth + qStart - q0; for (int q = qStart; q < qEnd; ++q) { visitor.visit(p, q, block[k]); ++k; } } } } return visitor.end(); } /** {@inheritDoc} */ @Override public T walkInOptimizedOrder(final FieldMatrixChangingVisitor visitor) { visitor.start(rows, columns, 0, rows - 1, 0, columns - 1); int blockIndex = 0; for (int iBlock = 0; iBlock < blockRows; ++iBlock) { final int pStart = iBlock * BLOCK_SIZE; final int pEnd = FastMath.min(pStart + BLOCK_SIZE, rows); for (int jBlock = 0; jBlock < blockColumns; ++jBlock) { final int qStart = jBlock * BLOCK_SIZE; final int qEnd = FastMath.min(qStart + BLOCK_SIZE, columns); final T[] block = blocks[blockIndex]; int k = 0; for (int p = pStart; p < pEnd; ++p) { for (int q = qStart; q < qEnd; ++q) { block[k] = visitor.visit(p, q, block[k]); ++k; } } ++blockIndex; } } return visitor.end(); } /** {@inheritDoc} */ @Override public T walkInOptimizedOrder(final FieldMatrixPreservingVisitor visitor) { visitor.start(rows, columns, 0, rows - 1, 0, columns - 1); int blockIndex = 0; for (int iBlock = 0; iBlock < blockRows; ++iBlock) { final int pStart = iBlock * BLOCK_SIZE; final int pEnd = FastMath.min(pStart + BLOCK_SIZE, rows); for (int jBlock = 0; jBlock < blockColumns; ++jBlock) { final int qStart = jBlock * BLOCK_SIZE; final int qEnd = FastMath.min(qStart + BLOCK_SIZE, columns); final T[] block = blocks[blockIndex]; int k = 0; for (int p = pStart; p < pEnd; ++p) { for (int q = qStart; q < qEnd; ++q) { visitor.visit(p, q, block[k]); ++k; } } ++blockIndex; } } return visitor.end(); } /** {@inheritDoc} */ @Override public T walkInOptimizedOrder(final FieldMatrixChangingVisitor visitor, final int startRow, final int endRow, final int startColumn, final int endColumn) throws OutOfRangeException, NumberIsTooSmallException { checkSubMatrixIndex(startRow, endRow, startColumn, endColumn); visitor.start(rows, columns, startRow, endRow, startColumn, endColumn); for (int iBlock = startRow / BLOCK_SIZE; iBlock < 1 + endRow / BLOCK_SIZE; ++iBlock) { final int p0 = iBlock * BLOCK_SIZE; final int pStart = FastMath.max(startRow, p0); final int pEnd = FastMath.min((iBlock + 1) * BLOCK_SIZE, 1 + endRow); for (int jBlock = startColumn / BLOCK_SIZE; jBlock < 1 + endColumn / BLOCK_SIZE; ++jBlock) { final int jWidth = blockWidth(jBlock); final int q0 = jBlock * BLOCK_SIZE; final int qStart = FastMath.max(startColumn, q0); final int qEnd = FastMath.min((jBlock + 1) * BLOCK_SIZE, 1 + endColumn); final T[] block = blocks[iBlock * blockColumns + jBlock]; for (int p = pStart; p < pEnd; ++p) { int k = (p - p0) * jWidth + qStart - q0; for (int q = qStart; q < qEnd; ++q) { block[k] = visitor.visit(p, q, block[k]); ++k; } } } } return visitor.end(); } /** {@inheritDoc} */ @Override public T walkInOptimizedOrder(final FieldMatrixPreservingVisitor visitor, final int startRow, final int endRow, final int startColumn, final int endColumn) throws OutOfRangeException, NumberIsTooSmallException { checkSubMatrixIndex(startRow, endRow, startColumn, endColumn); visitor.start(rows, columns, startRow, endRow, startColumn, endColumn); for (int iBlock = startRow / BLOCK_SIZE; iBlock < 1 + endRow / BLOCK_SIZE; ++iBlock) { final int p0 = iBlock * BLOCK_SIZE; final int pStart = FastMath.max(startRow, p0); final int pEnd = FastMath.min((iBlock + 1) * BLOCK_SIZE, 1 + endRow); for (int jBlock = startColumn / BLOCK_SIZE; jBlock < 1 + endColumn / BLOCK_SIZE; ++jBlock) { final int jWidth = blockWidth(jBlock); final int q0 = jBlock * BLOCK_SIZE; final int qStart = FastMath.max(startColumn, q0); final int qEnd = FastMath.min((jBlock + 1) * BLOCK_SIZE, 1 + endColumn); final T[] block = blocks[iBlock * blockColumns + jBlock]; for (int p = pStart; p < pEnd; ++p) { int k = (p - p0) * jWidth + qStart - q0; for (int q = qStart; q < qEnd; ++q) { visitor.visit(p, q, block[k]); ++k; } } } } return visitor.end(); } /** * Get the height of a block. * @param blockRow row index (in block sense) of the block * @return height (number of rows) of the block */ private int blockHeight(final int blockRow) { return (blockRow == blockRows - 1) ? rows - blockRow * BLOCK_SIZE : BLOCK_SIZE; } /** * Get the width of a block. * @param blockColumn column index (in block sense) of the block * @return width (number of columns) of the block */ private int blockWidth(final int blockColumn) { return (blockColumn == blockColumns - 1) ? columns - blockColumn * BLOCK_SIZE : BLOCK_SIZE; } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/ml/package-info.java100644 1750 1750 1601 12126627716 26323 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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 package for machine learning algorithms. */ package org.apache.commons.math3.ml; commons-math3-3.2-src/src/main/java/org/apache/commons/math3/ml/distance/package-info.java100644 1750 1750 1566 12126627716 30127 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ /** * Common distance measures. */ package org.apache.commons.math3.ml.distance; commons-math3-3.2-src/src/main/java/org/apache/commons/math3/ml/distance/EuclideanDistance.java100644 1750 1750 2451 12126627716 31141 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.ml.distance; import org.apache.commons.math3.util.MathArrays; /** * Calculates the L2 (Euclidean) distance between two points. * * @version $Id $ * @since 3.2 */ public class EuclideanDistance implements DistanceMeasure { /** Serializable version identifier. */ private static final long serialVersionUID = 1717556319784040040L; /** {@inheritDoc} */ public double compute(double[] a, double[] b) { return MathArrays.distance(a, b); } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/ml/distance/CanberraDistance.java100644 1750 1750 3026 12126627716 30764 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.ml.distance; import org.apache.commons.math3.util.FastMath; /** * Calculates the Canberra distance between two points. * * @version $Id $ * @since 3.2 */ public class CanberraDistance implements DistanceMeasure { /** Serializable version identifier. */ private static final long serialVersionUID = -6972277381587032228L; /** {@inheritDoc} */ public double compute(double[] a, double[] b) { double sum = 0; for (int i = 0; i < a.length; i++) { final double num = FastMath.abs(a[i] - b[i]); final double denom = FastMath.abs(a[i]) + FastMath.abs(b[i]); sum += num == 0.0 && denom == 0.0 ? 0.0 : num / denom; } return sum; } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/ml/distance/ManhattanDistance.java100644 1750 1750 2454 12126627716 31166 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.ml.distance; import org.apache.commons.math3.util.MathArrays; /** * Calculates the L1 (sum of abs) distance between two points. * * @version $Id $ * @since 3.2 */ public class ManhattanDistance implements DistanceMeasure { /** Serializable version identifier. */ private static final long serialVersionUID = -9108154600539125566L; /** {@inheritDoc} */ public double compute(double[] a, double[] b) { return MathArrays.distance1(a, b); } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/ml/distance/DistanceMeasure.java100644 1750 1750 2527 12126627716 30655 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.ml.distance; import java.io.Serializable; /** * Interface for distance measures of n-dimensional vectors. * * @version $Id $ * @since 3.2 */ public interface DistanceMeasure extends Serializable { /** * Compute the distance between two n-dimensional vectors. *

                              * The two vectors are required to have the same dimension. * * @param a the first vector * @param b the second vector * @return the distance between the two vectors */ double compute(double[] a, double[] b); } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/ml/distance/ChebyshevDistance.java100644 1750 1750 2464 12126627716 31174 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.ml.distance; import org.apache.commons.math3.util.MathArrays; /** * Calculates the L (max of abs) distance between two points. * * @version $Id $ * @since 3.2 */ public class ChebyshevDistance implements DistanceMeasure { /** Serializable version identifier. */ private static final long serialVersionUID = -4694868171115238296L; /** {@inheritDoc} */ public double compute(double[] a, double[] b) { return MathArrays.distanceInf(a, b); } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/ml/clustering/package-info.java100644 1750 1750 1565 12126627716 30513 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ /** * Clustering algorithms. */ package org.apache.commons.math3.ml.clustering; commons-math3-3.2-src/src/main/java/org/apache/commons/math3/ml/clustering/CentroidCluster.java100644 1750 1750 3440 12126627716 31272 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.ml.clustering; /** * A Cluster used by centroid-based clustering algorithms. *

                              * Defines additionally a cluster center which may not necessarily be a member * of the original data set. * * @param the type of points that can be clustered * @version $Id $ * @since 3.2 */ public class CentroidCluster extends Cluster { /** Serializable version identifier. */ private static final long serialVersionUID = -3075288519071812288L; /** Center of the cluster. */ private final Clusterable center; /** * Build a cluster centered at a specified point. * @param center the point which is to be the center of this cluster */ public CentroidCluster(final Clusterable center) { super(); this.center = center; } /** * Get the point chosen to be the center of this cluster. * @return chosen cluster center */ public Clusterable getCenter() { return center; } } ././@LongLink100644 0 0 150 12126630647 10256 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/ml/clustering/KMeansPlusPlusClusterer.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/ml/clustering/KMeansPlusPlusClusterer.j100644 1750 1750 52174 12126627716 32300 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.ml.clustering; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; import java.util.List; import org.apache.commons.math3.exception.ConvergenceException; import org.apache.commons.math3.exception.MathIllegalArgumentException; import org.apache.commons.math3.exception.NumberIsTooSmallException; import org.apache.commons.math3.exception.util.LocalizedFormats; import org.apache.commons.math3.ml.distance.DistanceMeasure; import org.apache.commons.math3.ml.distance.EuclideanDistance; import org.apache.commons.math3.random.JDKRandomGenerator; import org.apache.commons.math3.random.RandomGenerator; import org.apache.commons.math3.stat.descriptive.moment.Variance; import org.apache.commons.math3.util.MathUtils; /** * Clustering algorithm based on David Arthur and Sergei Vassilvitski k-means++ algorithm. * @param type of the points to cluster * @see K-means++ (wikipedia) * @version $Id: KMeansPlusPlusClusterer.java 1461866 2013-03-27 21:54:36Z tn $ * @since 3.2 */ public class KMeansPlusPlusClusterer extends Clusterer { /** Strategies to use for replacing an empty cluster. */ public static enum EmptyClusterStrategy { /** Split the cluster with largest distance variance. */ LARGEST_VARIANCE, /** Split the cluster with largest number of points. */ LARGEST_POINTS_NUMBER, /** Create a cluster around the point farthest from its centroid. */ FARTHEST_POINT, /** Generate an error. */ ERROR } /** The number of clusters. */ private final int k; /** The maximum number of iterations. */ private final int maxIterations; /** Random generator for choosing initial centers. */ private final RandomGenerator random; /** Selected strategy for empty clusters. */ private final EmptyClusterStrategy emptyStrategy; /** Build a clusterer. *

                              * The default strategy for handling empty clusters that may appear during * algorithm iterations is to split the cluster with largest distance variance. *

                              * The euclidean distance will be used as default distance measure. * * @param k the number of clusters to split the data into */ public KMeansPlusPlusClusterer(final int k) { this(k, -1); } /** Build a clusterer. *

                              * The default strategy for handling empty clusters that may appear during * algorithm iterations is to split the cluster with largest distance variance. *

                              * The euclidean distance will be used as default distance measure. * * @param k the number of clusters to split the data into * @param maxIterations the maximum number of iterations to run the algorithm for. * If negative, no maximum will be used. */ public KMeansPlusPlusClusterer(final int k, final int maxIterations) { this(k, maxIterations, new EuclideanDistance()); } /** Build a clusterer. *

                              * The default strategy for handling empty clusters that may appear during * algorithm iterations is to split the cluster with largest distance variance. * * @param k the number of clusters to split the data into * @param maxIterations the maximum number of iterations to run the algorithm for. * If negative, no maximum will be used. * @param measure the distance measure to use */ public KMeansPlusPlusClusterer(final int k, final int maxIterations, final DistanceMeasure measure) { this(k, maxIterations, measure, new JDKRandomGenerator()); } /** Build a clusterer. *

                              * The default strategy for handling empty clusters that may appear during * algorithm iterations is to split the cluster with largest distance variance. * * @param k the number of clusters to split the data into * @param maxIterations the maximum number of iterations to run the algorithm for. * If negative, no maximum will be used. * @param measure the distance measure to use * @param random random generator to use for choosing initial centers */ public KMeansPlusPlusClusterer(final int k, final int maxIterations, final DistanceMeasure measure, final RandomGenerator random) { this(k, maxIterations, measure, random, EmptyClusterStrategy.LARGEST_VARIANCE); } /** Build a clusterer. * * @param k the number of clusters to split the data into * @param maxIterations the maximum number of iterations to run the algorithm for. * If negative, no maximum will be used. * @param measure the distance measure to use * @param random random generator to use for choosing initial centers * @param emptyStrategy strategy to use for handling empty clusters that * may appear during algorithm iterations */ public KMeansPlusPlusClusterer(final int k, final int maxIterations, final DistanceMeasure measure, final RandomGenerator random, final EmptyClusterStrategy emptyStrategy) { super(measure); this.k = k; this.maxIterations = maxIterations; this.random = random; this.emptyStrategy = emptyStrategy; } /** * Return the number of clusters this instance will use. * @return the number of clusters */ public int getK() { return k; } /** * Returns the maximum number of iterations this instance will use. * @return the maximum number of iterations, or -1 if no maximum is set */ public int getMaxIterations() { return maxIterations; } /** * Returns the random generator this instance will use. * @return the random generator */ public RandomGenerator getRandomGenerator() { return random; } /** * Returns the {@link EmptyClusterStrategy} used by this instance. * @return the {@link EmptyClusterStrategy} */ public EmptyClusterStrategy getEmptyClusterStrategy() { return emptyStrategy; } /** * Runs the K-means++ clustering algorithm. * * @param points the points to cluster * @return a list of clusters containing the points * @throws MathIllegalArgumentException if the data points are null or the number * of clusters is larger than the number of data points * @throws ConvergenceException if an empty cluster is encountered and the * {@link #emptyStrategy} is set to {@code ERROR} */ public List> cluster(final Collection points) throws MathIllegalArgumentException, ConvergenceException { // sanity checks MathUtils.checkNotNull(points); // number of clusters has to be smaller or equal the number of data points if (points.size() < k) { throw new NumberIsTooSmallException(points.size(), k, false); } // create the initial clusters List> clusters = chooseInitialCenters(points); // create an array containing the latest assignment of a point to a cluster // no need to initialize the array, as it will be filled with the first assignment int[] assignments = new int[points.size()]; assignPointsToClusters(clusters, points, assignments); // iterate through updating the centers until we're done final int max = (maxIterations < 0) ? Integer.MAX_VALUE : maxIterations; for (int count = 0; count < max; count++) { boolean emptyCluster = false; List> newClusters = new ArrayList>(); for (final CentroidCluster cluster : clusters) { final Clusterable newCenter; if (cluster.getPoints().isEmpty()) { switch (emptyStrategy) { case LARGEST_VARIANCE : newCenter = getPointFromLargestVarianceCluster(clusters); break; case LARGEST_POINTS_NUMBER : newCenter = getPointFromLargestNumberCluster(clusters); break; case FARTHEST_POINT : newCenter = getFarthestPoint(clusters); break; default : throw new ConvergenceException(LocalizedFormats.EMPTY_CLUSTER_IN_K_MEANS); } emptyCluster = true; } else { newCenter = centroidOf(cluster.getPoints(), cluster.getCenter().getPoint().length); } newClusters.add(new CentroidCluster(newCenter)); } int changes = assignPointsToClusters(newClusters, points, assignments); clusters = newClusters; // if there were no more changes in the point-to-cluster assignment // and there are no empty clusters left, return the current clusters if (changes == 0 && !emptyCluster) { return clusters; } } return clusters; } /** * Adds the given points to the closest {@link Cluster}. * * @param clusters the {@link Cluster}s to add the points to * @param points the points to add to the given {@link Cluster}s * @param assignments points assignments to clusters * @return the number of points assigned to different clusters as the iteration before */ private int assignPointsToClusters(final List> clusters, final Collection points, final int[] assignments) { int assignedDifferently = 0; int pointIndex = 0; for (final T p : points) { int clusterIndex = getNearestCluster(clusters, p); if (clusterIndex != assignments[pointIndex]) { assignedDifferently++; } CentroidCluster cluster = clusters.get(clusterIndex); cluster.addPoint(p); assignments[pointIndex++] = clusterIndex; } return assignedDifferently; } /** * Use K-means++ to choose the initial centers. * * @param points the points to choose the initial centers from * @return the initial centers */ private List> chooseInitialCenters(final Collection points) { // Convert to list for indexed access. Make it unmodifiable, since removal of items // would screw up the logic of this method. final List pointList = Collections.unmodifiableList(new ArrayList (points)); // The number of points in the list. final int numPoints = pointList.size(); // Set the corresponding element in this array to indicate when // elements of pointList are no longer available. final boolean[] taken = new boolean[numPoints]; // The resulting list of initial centers. final List> resultSet = new ArrayList>(); // Choose one center uniformly at random from among the data points. final int firstPointIndex = random.nextInt(numPoints); final T firstPoint = pointList.get(firstPointIndex); resultSet.add(new CentroidCluster(firstPoint)); // Must mark it as taken taken[firstPointIndex] = true; // To keep track of the minimum distance squared of elements of // pointList to elements of resultSet. final double[] minDistSquared = new double[numPoints]; // Initialize the elements. Since the only point in resultSet is firstPoint, // this is very easy. for (int i = 0; i < numPoints; i++) { if (i != firstPointIndex) { // That point isn't considered double d = distance(firstPoint, pointList.get(i)); minDistSquared[i] = d*d; } } while (resultSet.size() < k) { // Sum up the squared distances for the points in pointList not // already taken. double distSqSum = 0.0; for (int i = 0; i < numPoints; i++) { if (!taken[i]) { distSqSum += minDistSquared[i]; } } // Add one new data point as a center. Each point x is chosen with // probability proportional to D(x)2 final double r = random.nextDouble() * distSqSum; // The index of the next point to be added to the resultSet. int nextPointIndex = -1; // Sum through the squared min distances again, stopping when // sum >= r. double sum = 0.0; for (int i = 0; i < numPoints; i++) { if (!taken[i]) { sum += minDistSquared[i]; if (sum >= r) { nextPointIndex = i; break; } } } // If it's not set to >= 0, the point wasn't found in the previous // for loop, probably because distances are extremely small. Just pick // the last available point. if (nextPointIndex == -1) { for (int i = numPoints - 1; i >= 0; i--) { if (!taken[i]) { nextPointIndex = i; break; } } } // We found one. if (nextPointIndex >= 0) { final T p = pointList.get(nextPointIndex); resultSet.add(new CentroidCluster (p)); // Mark it as taken. taken[nextPointIndex] = true; if (resultSet.size() < k) { // Now update elements of minDistSquared. We only have to compute // the distance to the new center to do this. for (int j = 0; j < numPoints; j++) { // Only have to worry about the points still not taken. if (!taken[j]) { double d = distance(p, pointList.get(j)); double d2 = d * d; if (d2 < minDistSquared[j]) { minDistSquared[j] = d2; } } } } } else { // None found -- // Break from the while loop to prevent // an infinite loop. break; } } return resultSet; } /** * Get a random point from the {@link Cluster} with the largest distance variance. * * @param clusters the {@link Cluster}s to search * @return a random point from the selected cluster * @throws ConvergenceException if clusters are all empty */ private T getPointFromLargestVarianceCluster(final Collection> clusters) throws ConvergenceException { double maxVariance = Double.NEGATIVE_INFINITY; Cluster selected = null; for (final CentroidCluster cluster : clusters) { if (!cluster.getPoints().isEmpty()) { // compute the distance variance of the current cluster final Clusterable center = cluster.getCenter(); final Variance stat = new Variance(); for (final T point : cluster.getPoints()) { stat.increment(distance(point, center)); } final double variance = stat.getResult(); // select the cluster with the largest variance if (variance > maxVariance) { maxVariance = variance; selected = cluster; } } } // did we find at least one non-empty cluster ? if (selected == null) { throw new ConvergenceException(LocalizedFormats.EMPTY_CLUSTER_IN_K_MEANS); } // extract a random point from the cluster final List selectedPoints = selected.getPoints(); return selectedPoints.remove(random.nextInt(selectedPoints.size())); } /** * Get a random point from the {@link Cluster} with the largest number of points * * @param clusters the {@link Cluster}s to search * @return a random point from the selected cluster * @throws ConvergenceException if clusters are all empty */ private T getPointFromLargestNumberCluster(final Collection> clusters) throws ConvergenceException { int maxNumber = 0; Cluster selected = null; for (final Cluster cluster : clusters) { // get the number of points of the current cluster final int number = cluster.getPoints().size(); // select the cluster with the largest number of points if (number > maxNumber) { maxNumber = number; selected = cluster; } } // did we find at least one non-empty cluster ? if (selected == null) { throw new ConvergenceException(LocalizedFormats.EMPTY_CLUSTER_IN_K_MEANS); } // extract a random point from the cluster final List selectedPoints = selected.getPoints(); return selectedPoints.remove(random.nextInt(selectedPoints.size())); } /** * Get the point farthest to its cluster center * * @param clusters the {@link Cluster}s to search * @return point farthest to its cluster center * @throws ConvergenceException if clusters are all empty */ private T getFarthestPoint(final Collection> clusters) throws ConvergenceException { double maxDistance = Double.NEGATIVE_INFINITY; Cluster selectedCluster = null; int selectedPoint = -1; for (final CentroidCluster cluster : clusters) { // get the farthest point final Clusterable center = cluster.getCenter(); final List points = cluster.getPoints(); for (int i = 0; i < points.size(); ++i) { final double distance = distance(points.get(i), center); if (distance > maxDistance) { maxDistance = distance; selectedCluster = cluster; selectedPoint = i; } } } // did we find at least one non-empty cluster ? if (selectedCluster == null) { throw new ConvergenceException(LocalizedFormats.EMPTY_CLUSTER_IN_K_MEANS); } return selectedCluster.getPoints().remove(selectedPoint); } /** * Returns the nearest {@link Cluster} to the given point * * @param clusters the {@link Cluster}s to search * @param point the point to find the nearest {@link Cluster} for * @return the index of the nearest {@link Cluster} to the given point */ private int getNearestCluster(final Collection> clusters, final T point) { double minDistance = Double.MAX_VALUE; int clusterIndex = 0; int minCluster = 0; for (final CentroidCluster c : clusters) { final double distance = distance(point, c.getCenter()); if (distance < minDistance) { minDistance = distance; minCluster = clusterIndex; } clusterIndex++; } return minCluster; } /** * Computes the centroid for a set of points. * * @param points the set of points * @param dimension the point dimension * @return the computed centroid for the set of points */ private Clusterable centroidOf(final Collection points, final int dimension) { final double[] centroid = new double[dimension]; for (final T p : points) { final double[] point = p.getPoint(); for (int i = 0; i < centroid.length; i++) { centroid[i] += point[i]; } } for (int i = 0; i < centroid.length; i++) { centroid[i] /= points.size(); } return new DoublePoint(centroid); } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/ml/clustering/Cluster.java100644 1750 1750 3525 12126627716 27606 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.ml.clustering; import java.io.Serializable; import java.util.ArrayList; import java.util.List; /** * Cluster holding a set of {@link Clusterable} points. * @param the type of points that can be clustered * @version $Id: Cluster.java 1461862 2013-03-27 21:48:10Z tn $ * @since 3.2 */ public class Cluster implements Serializable { /** Serializable version identifier. */ private static final long serialVersionUID = -3442297081515880464L; /** The points contained in this cluster. */ private final List points; /** * Build a cluster centered at a specified point. */ public Cluster() { points = new ArrayList(); } /** * Add a point to this cluster. * @param point point to add */ public void addPoint(final T point) { points.add(point); } /** * Get the points contained in the cluster. * @return points contained in the cluster */ public List getPoints() { return points; } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/ml/clustering/DoublePoint.java100644 1750 1750 5040 12126627716 30403 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.ml.clustering; import java.io.Serializable; import java.util.Arrays; /** * A simple implementation of {@link Clusterable} for points with double coordinates. * @version $Id: DoublePoint.java 1461862 2013-03-27 21:48:10Z tn $ * @since 3.2 */ public class DoublePoint implements Clusterable, Serializable { /** Serializable version identifier. */ private static final long serialVersionUID = 3946024775784901369L; /** Point coordinates. */ private final double[] point; /** * Build an instance wrapping an double array. *

                              * The wrapped array is referenced, it is not copied. * * @param point the n-dimensional point in double space */ public DoublePoint(final double[] point) { this.point = point; } /** * Build an instance wrapping an integer array. *

                              * The wrapped array is copied to an internal double array. * * @param point the n-dimensional point in integer space */ public DoublePoint(final int[] point) { this.point = new double[point.length]; for ( int i = 0; i < point.length; i++) { this.point[i] = point[i]; } } /** {@inheritDoc} */ public double[] getPoint() { return point; } /** {@inheritDoc} */ @Override public boolean equals(final Object other) { if (!(other instanceof DoublePoint)) { return false; } return Arrays.equals(point, ((DoublePoint) other).point); } /** {@inheritDoc} */ @Override public int hashCode() { return Arrays.hashCode(point); } /** {@inheritDoc} */ @Override public String toString() { return Arrays.toString(point); } } ././@LongLink100644 0 0 155 12126630647 10263 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/ml/clustering/MultiKMeansPlusPlusClusterer.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/ml/clustering/MultiKMeansPlusPlusCluste100644 1750 1750 10560 12126627716 32343 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.ml.clustering; import java.util.Collection; import java.util.List; import org.apache.commons.math3.exception.ConvergenceException; import org.apache.commons.math3.exception.MathIllegalArgumentException; import org.apache.commons.math3.stat.descriptive.moment.Variance; /** * A wrapper around a k-means++ clustering algorithm which performs multiple trials * and returns the best solution. * @param type of the points to cluster * @version $Id: MultiKMeansPlusPlusClusterer.java 1462375 2013-03-29 01:42:42Z psteitz $ * @since 3.2 */ public class MultiKMeansPlusPlusClusterer extends Clusterer { /** The underlying k-means clusterer. */ private final KMeansPlusPlusClusterer clusterer; /** The number of trial runs. */ private final int numTrials; /** Build a clusterer. * @param clusterer the k-means clusterer to use * @param numTrials number of trial runs */ public MultiKMeansPlusPlusClusterer(final KMeansPlusPlusClusterer clusterer, final int numTrials) { super(clusterer.getDistanceMeasure()); this.clusterer = clusterer; this.numTrials = numTrials; } /** * Returns the embedded k-means clusterer used by this instance. * @return the embedded clusterer */ public KMeansPlusPlusClusterer getClusterer() { return clusterer; } /** * Returns the number of trials this instance will do. * @return the number of trials */ public int getNumTrials() { return numTrials; } /** * Runs the K-means++ clustering algorithm. * * @param points the points to cluster * @return a list of clusters containing the points * @throws MathIllegalArgumentException if the data points are null or the number * of clusters is larger than the number of data points * @throws ConvergenceException if an empty cluster is encountered and the * underlying {@link KMeansPlusPlusClusterer} has its * {@link KMeansPlusPlusClusterer.EmptyClusterStrategy} is set to {@code ERROR}. */ public List> cluster(final Collection points) throws MathIllegalArgumentException, ConvergenceException { // at first, we have not found any clusters list yet List> best = null; double bestVarianceSum = Double.POSITIVE_INFINITY; // do several clustering trials for (int i = 0; i < numTrials; ++i) { // compute a clusters list List> clusters = clusterer.cluster(points); // compute the variance of the current list double varianceSum = 0.0; for (final CentroidCluster cluster : clusters) { if (!cluster.getPoints().isEmpty()) { // compute the distance variance of the current cluster final Clusterable center = cluster.getCenter(); final Variance stat = new Variance(); for (final T point : cluster.getPoints()) { stat.increment(distance(point, center)); } varianceSum += stat.getResult(); } } if (varianceSum <= bestVarianceSum) { // this one is the best we have found so far, remember it best = clusters; bestVarianceSum = varianceSum; } } // return the best clusters list found return best; } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/ml/clustering/DBSCANClusterer.java100644 1750 1750 20443 12126627716 31026 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.ml.clustering; import java.util.ArrayList; import java.util.Collection; import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Set; import org.apache.commons.math3.exception.NotPositiveException; import org.apache.commons.math3.exception.NullArgumentException; import org.apache.commons.math3.ml.distance.DistanceMeasure; import org.apache.commons.math3.ml.distance.EuclideanDistance; import org.apache.commons.math3.util.MathUtils; /** * DBSCAN (density-based spatial clustering of applications with noise) algorithm. *

                              * The DBSCAN algorithm forms clusters based on the idea of density connectivity, i.e. * a point p is density connected to another point q, if there exists a chain of * points pi, with i = 1 .. n and p1 = p and pn = q, * such that each pair <pi, pi+1> is directly density-reachable. * A point q is directly density-reachable from point p if it is in the ε-neighborhood * of this point. *

                              * Any point that is not density-reachable from a formed cluster is treated as noise, and * will thus not be present in the result. *

                              * The algorithm requires two parameters: *

                                *
                              • eps: the distance that defines the ε-neighborhood of a point *
                              • minPoints: the minimum number of density-connected points required to form a cluster *
                              * * @param type of the points to cluster * @see DBSCAN (wikipedia) * @see * A Density-Based Algorithm for Discovering Clusters in Large Spatial Databases with Noise * @version $Id: DBSCANClusterer.java 1461866 2013-03-27 21:54:36Z tn $ * @since 3.2 */ public class DBSCANClusterer extends Clusterer { /** Maximum radius of the neighborhood to be considered. */ private final double eps; /** Minimum number of points needed for a cluster. */ private final int minPts; /** Status of a point during the clustering process. */ private enum PointStatus { /** The point has is considered to be noise. */ NOISE, /** The point is already part of a cluster. */ PART_OF_CLUSTER } /** * Creates a new instance of a DBSCANClusterer. *

                              * The euclidean distance will be used as default distance measure. * * @param eps maximum radius of the neighborhood to be considered * @param minPts minimum number of points needed for a cluster * @throws NotPositiveException if {@code eps < 0.0} or {@code minPts < 0} */ public DBSCANClusterer(final double eps, final int minPts) throws NotPositiveException { this(eps, minPts, new EuclideanDistance()); } /** * Creates a new instance of a DBSCANClusterer. * * @param eps maximum radius of the neighborhood to be considered * @param minPts minimum number of points needed for a cluster * @param measure the distance measure to use * @throws NotPositiveException if {@code eps < 0.0} or {@code minPts < 0} */ public DBSCANClusterer(final double eps, final int minPts, final DistanceMeasure measure) throws NotPositiveException { super(measure); if (eps < 0.0d) { throw new NotPositiveException(eps); } if (minPts < 0) { throw new NotPositiveException(minPts); } this.eps = eps; this.minPts = minPts; } /** * Returns the maximum radius of the neighborhood to be considered. * @return maximum radius of the neighborhood */ public double getEps() { return eps; } /** * Returns the minimum number of points needed for a cluster. * @return minimum number of points needed for a cluster */ public int getMinPts() { return minPts; } /** * Performs DBSCAN cluster analysis. * * @param points the points to cluster * @return the list of clusters * @throws NullArgumentException if the data points are null */ public List> cluster(final Collection points) throws NullArgumentException { // sanity checks MathUtils.checkNotNull(points); final List> clusters = new ArrayList>(); final Map visited = new HashMap(); for (final T point : points) { if (visited.get(point) != null) { continue; } final List neighbors = getNeighbors(point, points); if (neighbors.size() >= minPts) { // DBSCAN does not care about center points final Cluster cluster = new Cluster(); clusters.add(expandCluster(cluster, point, neighbors, points, visited)); } else { visited.put(point, PointStatus.NOISE); } } return clusters; } /** * Expands the cluster to include density-reachable items. * * @param cluster Cluster to expand * @param point Point to add to cluster * @param neighbors List of neighbors * @param points the data set * @param visited the set of already visited points * @return the expanded cluster */ private Cluster expandCluster(final Cluster cluster, final T point, final List neighbors, final Collection points, final Map visited) { cluster.addPoint(point); visited.put(point, PointStatus.PART_OF_CLUSTER); List seeds = new ArrayList(neighbors); int index = 0; while (index < seeds.size()) { final T current = seeds.get(index); PointStatus pStatus = visited.get(current); // only check non-visited points if (pStatus == null) { final List currentNeighbors = getNeighbors(current, points); if (currentNeighbors.size() >= minPts) { seeds = merge(seeds, currentNeighbors); } } if (pStatus != PointStatus.PART_OF_CLUSTER) { visited.put(current, PointStatus.PART_OF_CLUSTER); cluster.addPoint(current); } index++; } return cluster; } /** * Returns a list of density-reachable neighbors of a {@code point}. * * @param point the point to look for * @param points possible neighbors * @return the List of neighbors */ private List getNeighbors(final T point, final Collection points) { final List neighbors = new ArrayList(); for (final T neighbor : points) { if (point != neighbor && distance(neighbor, point) <= eps) { neighbors.add(neighbor); } } return neighbors; } /** * Merges two lists together. * * @param one first list * @param two second list * @return merged lists */ private List merge(final List one, final List two) { final Set oneSet = new HashSet(one); for (T item : two) { if (!oneSet.contains(item)) { one.add(item); } } return one; } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/ml/clustering/Clusterable.java100644 1750 1750 2213 12126627716 30423 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.ml.clustering; /** * Interface for n-dimensional points that can be clustered together. * @version $Id: Clusterable.java 1461862 2013-03-27 21:48:10Z tn $ * @since 3.2 */ public interface Clusterable { /** * Gets the n-dimensional point. * * @return the point array */ double[] getPoint(); } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/ml/clustering/Clusterer.java100644 1750 1750 5530 12126627716 30133 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.ml.clustering; import java.util.Collection; import java.util.List; import org.apache.commons.math3.exception.ConvergenceException; import org.apache.commons.math3.exception.MathIllegalArgumentException; import org.apache.commons.math3.ml.distance.DistanceMeasure; /** * Base class for clustering algorithms. * * @param the type of points that can be clustered * @version $Id $ * @since 3.2 */ public abstract class Clusterer { /** The distance measure to use. */ private DistanceMeasure measure; /** * Build a new clusterer with the given {@link DistanceMeasure}. * * @param measure the distance measure to use */ protected Clusterer(final DistanceMeasure measure) { this.measure = measure; } /** * Perform a cluster analysis on the given set of {@link Clusterable} instances. * * @param points the set of {@link Clusterable} instances * @return a {@link List} of clusters * @throws MathIllegalArgumentException if points are null or the number of * data points is not compatible with this clusterer * @throws ConvergenceException if the algorithm has not yet converged after * the maximum number of iterations has been exceeded */ public abstract List> cluster(Collection points) throws MathIllegalArgumentException, ConvergenceException; /** * Returns the {@link DistanceMeasure} instance used by this clusterer. * * @return the distance measure */ public DistanceMeasure getDistanceMeasure() { return measure; } /** * Calculates the distance between two {@link Clusterable} instances * with the configured {@link DistanceMeasure}. * * @param p1 the first clusterable * @param p2 the second clusterable * @return the distance between the two clusterables */ protected double distance(final Clusterable p1, final Clusterable p2) { return measure.compute(p1.getPoint(), p2.getPoint()); } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/dfp/DfpMath.java100644 1750 1750 71507 12126627716 25517 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.dfp; /** Mathematical routines for use with {@link Dfp}. * The constants are defined in {@link DfpField} * @version $Id: DfpMath.java 1462503 2013-03-29 15:48:27Z luc $ * @since 2.2 */ public class DfpMath { /** Name for traps triggered by pow. */ private static final String POW_TRAP = "pow"; /** * Private Constructor. */ private DfpMath() { } /** Breaks a string representation up into two dfp's. *

                              The two dfp are such that the sum of them is equivalent * to the input string, but has higher precision than using a * single dfp. This is useful for improving accuracy of * exponentiation and critical multiplies. * @param field field to which the Dfp must belong * @param a string representation to split * @return an array of two {@link Dfp} which sum is a */ protected static Dfp[] split(final DfpField field, final String a) { Dfp result[] = new Dfp[2]; char[] buf; boolean leading = true; int sp = 0; int sig = 0; buf = new char[a.length()]; for (int i = 0; i < buf.length; i++) { buf[i] = a.charAt(i); if (buf[i] >= '1' && buf[i] <= '9') { leading = false; } if (buf[i] == '.') { sig += (400 - sig) % 4; leading = false; } if (sig == (field.getRadixDigits() / 2) * 4) { sp = i; break; } if (buf[i] >= '0' && buf[i] <= '9' && !leading) { sig ++; } } result[0] = field.newDfp(new String(buf, 0, sp)); for (int i = 0; i < buf.length; i++) { buf[i] = a.charAt(i); if (buf[i] >= '0' && buf[i] <= '9' && i < sp) { buf[i] = '0'; } } result[1] = field.newDfp(new String(buf)); return result; } /** Splits a {@link Dfp} into 2 {@link Dfp}'s such that their sum is equal to the input {@link Dfp}. * @param a number to split * @return two elements array containing the split number */ protected static Dfp[] split(final Dfp a) { final Dfp[] result = new Dfp[2]; final Dfp shift = a.multiply(a.power10K(a.getRadixDigits() / 2)); result[0] = a.add(shift).subtract(shift); result[1] = a.subtract(result[0]); return result; } /** Multiply two numbers that are split in to two pieces that are * meant to be added together. * Use binomial multiplication so ab = a0 b0 + a0 b1 + a1 b0 + a1 b1 * Store the first term in result0, the rest in result1 * @param a first factor of the multiplication, in split form * @param b second factor of the multiplication, in split form * @return a × b, in split form */ protected static Dfp[] splitMult(final Dfp[] a, final Dfp[] b) { final Dfp[] result = new Dfp[2]; result[1] = a[0].getZero(); result[0] = a[0].multiply(b[0]); /* If result[0] is infinite or zero, don't compute result[1]. * Attempting to do so may produce NaNs. */ if (result[0].classify() == Dfp.INFINITE || result[0].equals(result[1])) { return result; } result[1] = a[0].multiply(b[1]).add(a[1].multiply(b[0])).add(a[1].multiply(b[1])); return result; } /** Divide two numbers that are split in to two pieces that are meant to be added together. * Inverse of split multiply above: * (a+b) / (c+d) = (a/c) + ( (bc-ad)/(c**2+cd) ) * @param a dividend, in split form * @param b divisor, in split form * @return a / b, in split form */ protected static Dfp[] splitDiv(final Dfp[] a, final Dfp[] b) { final Dfp[] result; result = new Dfp[2]; result[0] = a[0].divide(b[0]); result[1] = a[1].multiply(b[0]).subtract(a[0].multiply(b[1])); result[1] = result[1].divide(b[0].multiply(b[0]).add(b[0].multiply(b[1]))); return result; } /** Raise a split base to the a power. * @param base number to raise * @param a power * @return basea */ protected static Dfp splitPow(final Dfp[] base, int a) { boolean invert = false; Dfp[] r = new Dfp[2]; Dfp[] result = new Dfp[2]; result[0] = base[0].getOne(); result[1] = base[0].getZero(); if (a == 0) { // Special case a = 0 return result[0].add(result[1]); } if (a < 0) { // If a is less than zero invert = true; a = -a; } // Exponentiate by successive squaring do { r[0] = new Dfp(base[0]); r[1] = new Dfp(base[1]); int trial = 1; int prevtrial; while (true) { prevtrial = trial; trial = trial * 2; if (trial > a) { break; } r = splitMult(r, r); } trial = prevtrial; a -= trial; result = splitMult(result, r); } while (a >= 1); result[0] = result[0].add(result[1]); if (invert) { result[0] = base[0].getOne().divide(result[0]); } return result[0]; } /** Raises base to the power a by successive squaring. * @param base number to raise * @param a power * @return basea */ public static Dfp pow(Dfp base, int a) { boolean invert = false; Dfp result = base.getOne(); if (a == 0) { // Special case return result; } if (a < 0) { invert = true; a = -a; } // Exponentiate by successive squaring do { Dfp r = new Dfp(base); Dfp prevr; int trial = 1; int prevtrial; do { prevr = new Dfp(r); prevtrial = trial; r = r.multiply(r); trial = trial * 2; } while (a>trial); r = prevr; trial = prevtrial; a = a - trial; result = result.multiply(r); } while (a >= 1); if (invert) { result = base.getOne().divide(result); } return base.newInstance(result); } /** Computes e to the given power. * a is broken into two parts, such that a = n+m where n is an integer. * We use pow() to compute en and a Taylor series to compute * em. We return e*n × em * @param a power at which e should be raised * @return ea */ public static Dfp exp(final Dfp a) { final Dfp inta = a.rint(); final Dfp fraca = a.subtract(inta); final int ia = inta.intValue(); if (ia > 2147483646) { // return +Infinity return a.newInstance((byte)1, Dfp.INFINITE); } if (ia < -2147483646) { // return 0; return a.newInstance(); } final Dfp einta = splitPow(a.getField().getESplit(), ia); final Dfp efraca = expInternal(fraca); return einta.multiply(efraca); } /** Computes e to the given power. * Where -1 < a < 1. Use the classic Taylor series. 1 + x**2/2! + x**3/3! + x**4/4! ... * @param a power at which e should be raised * @return ea */ protected static Dfp expInternal(final Dfp a) { Dfp y = a.getOne(); Dfp x = a.getOne(); Dfp fact = a.getOne(); Dfp py = new Dfp(y); for (int i = 1; i < 90; i++) { x = x.multiply(a); fact = fact.divide(i); y = y.add(x.multiply(fact)); if (y.equals(py)) { break; } py = new Dfp(y); } return y; } /** Returns the natural logarithm of a. * a is first split into three parts such that a = (10000^h)(2^j)k. * ln(a) is computed by ln(a) = ln(5)*h + ln(2)*(h+j) + ln(k) * k is in the range 2/3 < k <4/3 and is passed on to a series expansion. * @param a number from which logarithm is requested * @return log(a) */ public static Dfp log(Dfp a) { int lr; Dfp x; int ix; int p2 = 0; // Check the arguments somewhat here if (a.equals(a.getZero()) || a.lessThan(a.getZero()) || a.isNaN()) { // negative, zero or NaN a.getField().setIEEEFlagsBits(DfpField.FLAG_INVALID); return a.dotrap(DfpField.FLAG_INVALID, "ln", a, a.newInstance((byte)1, Dfp.QNAN)); } if (a.classify() == Dfp.INFINITE) { return a; } x = new Dfp(a); lr = x.log10K(); x = x.divide(pow(a.newInstance(10000), lr)); /* This puts x in the range 0-10000 */ ix = x.floor().intValue(); while (ix > 2) { ix >>= 1; p2++; } Dfp[] spx = split(x); Dfp[] spy = new Dfp[2]; spy[0] = pow(a.getTwo(), p2); // use spy[0] temporarily as a divisor spx[0] = spx[0].divide(spy[0]); spx[1] = spx[1].divide(spy[0]); spy[0] = a.newInstance("1.33333"); // Use spy[0] for comparison while (spx[0].add(spx[1]).greaterThan(spy[0])) { spx[0] = spx[0].divide(2); spx[1] = spx[1].divide(2); p2++; } // X is now in the range of 2/3 < x < 4/3 Dfp[] spz = logInternal(spx); spx[0] = a.newInstance(new StringBuilder().append(p2+4*lr).toString()); spx[1] = a.getZero(); spy = splitMult(a.getField().getLn2Split(), spx); spz[0] = spz[0].add(spy[0]); spz[1] = spz[1].add(spy[1]); spx[0] = a.newInstance(new StringBuilder().append(4*lr).toString()); spx[1] = a.getZero(); spy = splitMult(a.getField().getLn5Split(), spx); spz[0] = spz[0].add(spy[0]); spz[1] = spz[1].add(spy[1]); return a.newInstance(spz[0].add(spz[1])); } /** Computes the natural log of a number between 0 and 2. * Let f(x) = ln(x), * * We know that f'(x) = 1/x, thus from Taylor's theorum we have: * * ----- n+1 n * f(x) = \ (-1) (x - 1) * / ---------------- for 1 <= n <= infinity * ----- n * * or * 2 3 4 * (x-1) (x-1) (x-1) * ln(x) = (x-1) - ----- + ------ - ------ + ... * 2 3 4 * * alternatively, * * 2 3 4 * x x x * ln(x+1) = x - - + - - - + ... * 2 3 4 * * This series can be used to compute ln(x), but it converges too slowly. * * If we substitute -x for x above, we get * * 2 3 4 * x x x * ln(1-x) = -x - - - - - - + ... * 2 3 4 * * Note that all terms are now negative. Because the even powered ones * absorbed the sign. Now, subtract the series above from the previous * one to get ln(x+1) - ln(1-x). Note the even terms cancel out leaving * only the odd ones * * 3 5 7 * 2x 2x 2x * ln(x+1) - ln(x-1) = 2x + --- + --- + ---- + ... * 3 5 7 * * By the property of logarithms that ln(a) - ln(b) = ln (a/b) we have: * * 3 5 7 * x+1 / x x x \ * ln ----- = 2 * | x + ---- + ---- + ---- + ... | * x-1 \ 3 5 7 / * * But now we want to find ln(a), so we need to find the value of x * such that a = (x+1)/(x-1). This is easily solved to find that * x = (a-1)/(a+1). * @param a number from which logarithm is requested, in split form * @return log(a) */ protected static Dfp[] logInternal(final Dfp a[]) { /* Now we want to compute x = (a-1)/(a+1) but this is prone to * loss of precision. So instead, compute x = (a/4 - 1/4) / (a/4 + 1/4) */ Dfp t = a[0].divide(4).add(a[1].divide(4)); Dfp x = t.add(a[0].newInstance("-0.25")).divide(t.add(a[0].newInstance("0.25"))); Dfp y = new Dfp(x); Dfp num = new Dfp(x); Dfp py = new Dfp(y); int den = 1; for (int i = 0; i < 10000; i++) { num = num.multiply(x); num = num.multiply(x); den = den + 2; t = num.divide(den); y = y.add(t); if (y.equals(py)) { break; } py = new Dfp(y); } y = y.multiply(a[0].getTwo()); return split(y); } /** Computes x to the y power.

                              * * Uses the following method:

                              * *

                                *
                              1. Set u = rint(y), v = y-u *
                              2. Compute a = v * ln(x) *
                              3. Compute b = rint( a/ln(2) ) *
                              4. Compute c = a - b*ln(2) *
                              5. xy = xu * 2b * ec *
                              * if |y| > 1e8, then we compute by exp(y*ln(x))

                              * * Special Cases

                              *

                                *
                              • if y is 0.0 or -0.0 then result is 1.0 *
                              • if y is 1.0 then result is x *
                              • if y is NaN then result is NaN *
                              • if x is NaN and y is not zero then result is NaN *
                              • if |x| > 1.0 and y is +Infinity then result is +Infinity *
                              • if |x| < 1.0 and y is -Infinity then result is +Infinity *
                              • if |x| > 1.0 and y is -Infinity then result is +0 *
                              • if |x| < 1.0 and y is +Infinity then result is +0 *
                              • if |x| = 1.0 and y is +/-Infinity then result is NaN *
                              • if x = +0 and y > 0 then result is +0 *
                              • if x = +Inf and y < 0 then result is +0 *
                              • if x = +0 and y < 0 then result is +Inf *
                              • if x = +Inf and y > 0 then result is +Inf *
                              • if x = -0 and y > 0, finite, not odd integer then result is +0 *
                              • if x = -0 and y < 0, finite, and odd integer then result is -Inf *
                              • if x = -Inf and y > 0, finite, and odd integer then result is -Inf *
                              • if x = -0 and y < 0, not finite odd integer then result is +Inf *
                              • if x = -Inf and y > 0, not finite odd integer then result is +Inf *
                              • if x < 0 and y > 0, finite, and odd integer then result is -(|x|y) *
                              • if x < 0 and y > 0, finite, and not integer then result is NaN *
                              * @param x base to be raised * @param y power to which base should be raised * @return xy */ public static Dfp pow(Dfp x, final Dfp y) { // make sure we don't mix number with different precision if (x.getField().getRadixDigits() != y.getField().getRadixDigits()) { x.getField().setIEEEFlagsBits(DfpField.FLAG_INVALID); final Dfp result = x.newInstance(x.getZero()); result.nans = Dfp.QNAN; return x.dotrap(DfpField.FLAG_INVALID, POW_TRAP, x, result); } final Dfp zero = x.getZero(); final Dfp one = x.getOne(); final Dfp two = x.getTwo(); boolean invert = false; int ui; /* Check for special cases */ if (y.equals(zero)) { return x.newInstance(one); } if (y.equals(one)) { if (x.isNaN()) { // Test for NaNs x.getField().setIEEEFlagsBits(DfpField.FLAG_INVALID); return x.dotrap(DfpField.FLAG_INVALID, POW_TRAP, x, x); } return x; } if (x.isNaN() || y.isNaN()) { // Test for NaNs x.getField().setIEEEFlagsBits(DfpField.FLAG_INVALID); return x.dotrap(DfpField.FLAG_INVALID, POW_TRAP, x, x.newInstance((byte)1, Dfp.QNAN)); } // X == 0 if (x.equals(zero)) { if (Dfp.copysign(one, x).greaterThan(zero)) { // X == +0 if (y.greaterThan(zero)) { return x.newInstance(zero); } else { return x.newInstance(x.newInstance((byte)1, Dfp.INFINITE)); } } else { // X == -0 if (y.classify() == Dfp.FINITE && y.rint().equals(y) && !y.remainder(two).equals(zero)) { // If y is odd integer if (y.greaterThan(zero)) { return x.newInstance(zero.negate()); } else { return x.newInstance(x.newInstance((byte)-1, Dfp.INFINITE)); } } else { // Y is not odd integer if (y.greaterThan(zero)) { return x.newInstance(zero); } else { return x.newInstance(x.newInstance((byte)1, Dfp.INFINITE)); } } } } if (x.lessThan(zero)) { // Make x positive, but keep track of it x = x.negate(); invert = true; } if (x.greaterThan(one) && y.classify() == Dfp.INFINITE) { if (y.greaterThan(zero)) { return y; } else { return x.newInstance(zero); } } if (x.lessThan(one) && y.classify() == Dfp.INFINITE) { if (y.greaterThan(zero)) { return x.newInstance(zero); } else { return x.newInstance(Dfp.copysign(y, one)); } } if (x.equals(one) && y.classify() == Dfp.INFINITE) { x.getField().setIEEEFlagsBits(DfpField.FLAG_INVALID); return x.dotrap(DfpField.FLAG_INVALID, POW_TRAP, x, x.newInstance((byte)1, Dfp.QNAN)); } if (x.classify() == Dfp.INFINITE) { // x = +/- inf if (invert) { // negative infinity if (y.classify() == Dfp.FINITE && y.rint().equals(y) && !y.remainder(two).equals(zero)) { // If y is odd integer if (y.greaterThan(zero)) { return x.newInstance(x.newInstance((byte)-1, Dfp.INFINITE)); } else { return x.newInstance(zero.negate()); } } else { // Y is not odd integer if (y.greaterThan(zero)) { return x.newInstance(x.newInstance((byte)1, Dfp.INFINITE)); } else { return x.newInstance(zero); } } } else { // positive infinity if (y.greaterThan(zero)) { return x; } else { return x.newInstance(zero); } } } if (invert && !y.rint().equals(y)) { x.getField().setIEEEFlagsBits(DfpField.FLAG_INVALID); return x.dotrap(DfpField.FLAG_INVALID, POW_TRAP, x, x.newInstance((byte)1, Dfp.QNAN)); } // End special cases Dfp r; if (y.lessThan(x.newInstance(100000000)) && y.greaterThan(x.newInstance(-100000000))) { final Dfp u = y.rint(); ui = u.intValue(); final Dfp v = y.subtract(u); if (v.unequal(zero)) { final Dfp a = v.multiply(log(x)); final Dfp b = a.divide(x.getField().getLn2()).rint(); final Dfp c = a.subtract(b.multiply(x.getField().getLn2())); r = splitPow(split(x), ui); r = r.multiply(pow(two, b.intValue())); r = r.multiply(exp(c)); } else { r = splitPow(split(x), ui); } } else { // very large exponent. |y| > 1e8 r = exp(log(x).multiply(y)); } if (invert && y.rint().equals(y) && !y.remainder(two).equals(zero)) { // if y is odd integer r = r.negate(); } return x.newInstance(r); } /** Computes sin(a) Used when 0 < a < pi/4. * Uses the classic Taylor series. x - x**3/3! + x**5/5! ... * @param a number from which sine is desired, in split form * @return sin(a) */ protected static Dfp sinInternal(Dfp a[]) { Dfp c = a[0].add(a[1]); Dfp y = c; c = c.multiply(c); Dfp x = y; Dfp fact = a[0].getOne(); Dfp py = new Dfp(y); for (int i = 3; i < 90; i += 2) { x = x.multiply(c); x = x.negate(); fact = fact.divide((i-1)*i); // 1 over fact y = y.add(x.multiply(fact)); if (y.equals(py)) { break; } py = new Dfp(y); } return y; } /** Computes cos(a) Used when 0 < a < pi/4. * Uses the classic Taylor series for cosine. 1 - x**2/2! + x**4/4! ... * @param a number from which cosine is desired, in split form * @return cos(a) */ protected static Dfp cosInternal(Dfp a[]) { final Dfp one = a[0].getOne(); Dfp x = one; Dfp y = one; Dfp c = a[0].add(a[1]); c = c.multiply(c); Dfp fact = one; Dfp py = new Dfp(y); for (int i = 2; i < 90; i += 2) { x = x.multiply(c); x = x.negate(); fact = fact.divide((i - 1) * i); // 1 over fact y = y.add(x.multiply(fact)); if (y.equals(py)) { break; } py = new Dfp(y); } return y; } /** computes the sine of the argument. * @param a number from which sine is desired * @return sin(a) */ public static Dfp sin(final Dfp a) { final Dfp pi = a.getField().getPi(); final Dfp zero = a.getField().getZero(); boolean neg = false; /* First reduce the argument to the range of +/- PI */ Dfp x = a.remainder(pi.multiply(2)); /* if x < 0 then apply identity sin(-x) = -sin(x) */ /* This puts x in the range 0 < x < PI */ if (x.lessThan(zero)) { x = x.negate(); neg = true; } /* Since sine(x) = sine(pi - x) we can reduce the range to * 0 < x < pi/2 */ if (x.greaterThan(pi.divide(2))) { x = pi.subtract(x); } Dfp y; if (x.lessThan(pi.divide(4))) { Dfp c[] = new Dfp[2]; c[0] = x; c[1] = zero; //y = sinInternal(c); y = sinInternal(split(x)); } else { final Dfp c[] = new Dfp[2]; final Dfp[] piSplit = a.getField().getPiSplit(); c[0] = piSplit[0].divide(2).subtract(x); c[1] = piSplit[1].divide(2); y = cosInternal(c); } if (neg) { y = y.negate(); } return a.newInstance(y); } /** computes the cosine of the argument. * @param a number from which cosine is desired * @return cos(a) */ public static Dfp cos(Dfp a) { final Dfp pi = a.getField().getPi(); final Dfp zero = a.getField().getZero(); boolean neg = false; /* First reduce the argument to the range of +/- PI */ Dfp x = a.remainder(pi.multiply(2)); /* if x < 0 then apply identity cos(-x) = cos(x) */ /* This puts x in the range 0 < x < PI */ if (x.lessThan(zero)) { x = x.negate(); } /* Since cos(x) = -cos(pi - x) we can reduce the range to * 0 < x < pi/2 */ if (x.greaterThan(pi.divide(2))) { x = pi.subtract(x); neg = true; } Dfp y; if (x.lessThan(pi.divide(4))) { Dfp c[] = new Dfp[2]; c[0] = x; c[1] = zero; y = cosInternal(c); } else { final Dfp c[] = new Dfp[2]; final Dfp[] piSplit = a.getField().getPiSplit(); c[0] = piSplit[0].divide(2).subtract(x); c[1] = piSplit[1].divide(2); y = sinInternal(c); } if (neg) { y = y.negate(); } return a.newInstance(y); } /** computes the tangent of the argument. * @param a number from which tangent is desired * @return tan(a) */ public static Dfp tan(final Dfp a) { return sin(a).divide(cos(a)); } /** computes the arc-tangent of the argument. * @param a number from which arc-tangent is desired * @return atan(a) */ protected static Dfp atanInternal(final Dfp a) { Dfp y = new Dfp(a); Dfp x = new Dfp(y); Dfp py = new Dfp(y); for (int i = 3; i < 90; i += 2) { x = x.multiply(a); x = x.multiply(a); x = x.negate(); y = y.add(x.divide(i)); if (y.equals(py)) { break; } py = new Dfp(y); } return y; } /** computes the arc tangent of the argument * * Uses the typical taylor series * * but may reduce arguments using the following identity * tan(x+y) = (tan(x) + tan(y)) / (1 - tan(x)*tan(y)) * * since tan(PI/8) = sqrt(2)-1, * * atan(x) = atan( (x - sqrt(2) + 1) / (1+x*sqrt(2) - x) + PI/8.0 * @param a number from which arc-tangent is desired * @return atan(a) */ public static Dfp atan(final Dfp a) { final Dfp zero = a.getField().getZero(); final Dfp one = a.getField().getOne(); final Dfp[] sqr2Split = a.getField().getSqr2Split(); final Dfp[] piSplit = a.getField().getPiSplit(); boolean recp = false; boolean neg = false; boolean sub = false; final Dfp ty = sqr2Split[0].subtract(one).add(sqr2Split[1]); Dfp x = new Dfp(a); if (x.lessThan(zero)) { neg = true; x = x.negate(); } if (x.greaterThan(one)) { recp = true; x = one.divide(x); } if (x.greaterThan(ty)) { Dfp sty[] = new Dfp[2]; sub = true; sty[0] = sqr2Split[0].subtract(one); sty[1] = sqr2Split[1]; Dfp[] xs = split(x); Dfp[] ds = splitMult(xs, sty); ds[0] = ds[0].add(one); xs[0] = xs[0].subtract(sty[0]); xs[1] = xs[1].subtract(sty[1]); xs = splitDiv(xs, ds); x = xs[0].add(xs[1]); //x = x.subtract(ty).divide(dfp.one.add(x.multiply(ty))); } Dfp y = atanInternal(x); if (sub) { y = y.add(piSplit[0].divide(8)).add(piSplit[1].divide(8)); } if (recp) { y = piSplit[0].divide(2).subtract(y).add(piSplit[1].divide(2)); } if (neg) { y = y.negate(); } return a.newInstance(y); } /** computes the arc-sine of the argument. * @param a number from which arc-sine is desired * @return asin(a) */ public static Dfp asin(final Dfp a) { return atan(a.divide(a.getOne().subtract(a.multiply(a)).sqrt())); } /** computes the arc-cosine of the argument. * @param a number from which arc-cosine is desired * @return acos(a) */ public static Dfp acos(Dfp a) { Dfp result; boolean negative = false; if (a.lessThan(a.getZero())) { negative = true; } a = Dfp.copysign(a, a.getOne()); // absolute value result = atan(a.getOne().subtract(a.multiply(a)).sqrt().divide(a)); if (negative) { result = a.getField().getPi().subtract(result); } return a.newInstance(result); } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/dfp/package-info.java100644 1750 1750 10013 12126627716 26501 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ /** * * Decimal floating point library for Java * *

                              Another floating point class. This one is built using radix 10000 * which is 104, so its almost decimal.

                              * *

                              The design goals here are: *

                                *
                              1. Decimal math, or close to it
                              2. *
                              3. Settable precision (but no mix between numbers using different settings)
                              4. *
                              5. Portability. Code should be keep as portable as possible.
                              6. *
                              7. Performance
                              8. *
                              9. Accuracy - Results should always be +/- 1 ULP for basic * algebraic operation
                              10. *
                              11. Comply with IEEE 854-1987 as much as possible. * (See IEEE 854-1987 notes below)
                              12. *

                              * *

                              Trade offs: *

                                *
                              1. Memory foot print. I'm using more memory than necessary to * represent numbers to get better performance.
                              2. *
                              3. Digits are bigger, so rounding is a greater loss. So, if you * really need 12 decimal digits, better use 4 base 10000 digits * there can be one partially filled.
                              4. *

                              * *

                              Numbers are represented in the following form: *

                               * n  =  sign × mant × (radix)exp;

                              *
                              * where sign is ±1, mantissa represents a fractional number between * zero and one. mant[0] is the least significant digit. * exp is in the range of -32767 to 32768

                              * *

                              IEEE 854-1987 Notes and differences

                              * *

                              IEEE 854 requires the radix to be either 2 or 10. The radix here is * 10000, so that requirement is not met, but it is possible that a * subclassed can be made to make it behave as a radix 10 * number. It is my opinion that if it looks and behaves as a radix * 10 number then it is one and that requirement would be met.

                              * *

                              The radix of 10000 was chosen because it should be faster to operate * on 4 decimal digits at once instead of one at a time. Radix 10 behavior * can be realized by add an additional rounding step to ensure that * the number of decimal digits represented is constant.

                              * *

                              The IEEE standard specifically leaves out internal data encoding, * so it is reasonable to conclude that such a subclass of this radix * 10000 system is merely an encoding of a radix 10 system.

                              * *

                              IEEE 854 also specifies the existence of "sub-normal" numbers. This * class does not contain any such entities. The most significant radix * 10000 digit is always non-zero. Instead, we support "gradual underflow" * by raising the underflow flag for numbers less with exponent less than * expMin, but don't flush to zero until the exponent reaches MIN_EXP-digits. * Thus the smallest number we can represent would be: * 1E(-(MIN_EXP-digits-1)∗4), eg, for digits=5, MIN_EXP=-32767, that would * be 1e-131092.

                              * *

                              IEEE 854 defines that the implied radix point lies just to the right * of the most significant digit and to the left of the remaining digits. * This implementation puts the implied radix point to the left of all * digits including the most significant one. The most significant digit * here is the one just to the right of the radix point. This is a fine * detail and is really only a matter of definition. Any side effects of * this can be rendered invisible by a subclass.

                              * */ package org.apache.commons.math3.dfp; commons-math3-3.2-src/src/main/java/org/apache/commons/math3/dfp/DfpField.java100644 1750 1750 60074 12126627716 25646 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.dfp; import org.apache.commons.math3.Field; import org.apache.commons.math3.FieldElement; /** Field for Decimal floating point instances. * @version $Id: DfpField.java 1416643 2012-12-03 19:37:14Z tn $ * @since 2.2 */ public class DfpField implements Field { /** Enumerate for rounding modes. */ public enum RoundingMode { /** Rounds toward zero (truncation). */ ROUND_DOWN, /** Rounds away from zero if discarded digit is non-zero. */ ROUND_UP, /** Rounds towards nearest unless both are equidistant in which case it rounds away from zero. */ ROUND_HALF_UP, /** Rounds towards nearest unless both are equidistant in which case it rounds toward zero. */ ROUND_HALF_DOWN, /** Rounds towards nearest unless both are equidistant in which case it rounds toward the even neighbor. * This is the default as specified by IEEE 854-1987 */ ROUND_HALF_EVEN, /** Rounds towards nearest unless both are equidistant in which case it rounds toward the odd neighbor. */ ROUND_HALF_ODD, /** Rounds towards positive infinity. */ ROUND_CEIL, /** Rounds towards negative infinity. */ ROUND_FLOOR; } /** IEEE 854-1987 flag for invalid operation. */ public static final int FLAG_INVALID = 1; /** IEEE 854-1987 flag for division by zero. */ public static final int FLAG_DIV_ZERO = 2; /** IEEE 854-1987 flag for overflow. */ public static final int FLAG_OVERFLOW = 4; /** IEEE 854-1987 flag for underflow. */ public static final int FLAG_UNDERFLOW = 8; /** IEEE 854-1987 flag for inexact result. */ public static final int FLAG_INEXACT = 16; /** High precision string representation of √2. */ private static String sqr2String; // Note: the static strings are set up (once) by the ctor and @GuardedBy("DfpField.class") /** High precision string representation of √2 / 2. */ private static String sqr2ReciprocalString; /** High precision string representation of √3. */ private static String sqr3String; /** High precision string representation of √3 / 3. */ private static String sqr3ReciprocalString; /** High precision string representation of π. */ private static String piString; /** High precision string representation of e. */ private static String eString; /** High precision string representation of ln(2). */ private static String ln2String; /** High precision string representation of ln(5). */ private static String ln5String; /** High precision string representation of ln(10). */ private static String ln10String; /** The number of radix digits. * Note these depend on the radix which is 10000 digits, * so each one is equivalent to 4 decimal digits. */ private final int radixDigits; /** A {@link Dfp} with value 0. */ private final Dfp zero; /** A {@link Dfp} with value 1. */ private final Dfp one; /** A {@link Dfp} with value 2. */ private final Dfp two; /** A {@link Dfp} with value √2. */ private final Dfp sqr2; /** A two elements {@link Dfp} array with value √2 split in two pieces. */ private final Dfp[] sqr2Split; /** A {@link Dfp} with value √2 / 2. */ private final Dfp sqr2Reciprocal; /** A {@link Dfp} with value √3. */ private final Dfp sqr3; /** A {@link Dfp} with value √3 / 3. */ private final Dfp sqr3Reciprocal; /** A {@link Dfp} with value π. */ private final Dfp pi; /** A two elements {@link Dfp} array with value π split in two pieces. */ private final Dfp[] piSplit; /** A {@link Dfp} with value e. */ private final Dfp e; /** A two elements {@link Dfp} array with value e split in two pieces. */ private final Dfp[] eSplit; /** A {@link Dfp} with value ln(2). */ private final Dfp ln2; /** A two elements {@link Dfp} array with value ln(2) split in two pieces. */ private final Dfp[] ln2Split; /** A {@link Dfp} with value ln(5). */ private final Dfp ln5; /** A two elements {@link Dfp} array with value ln(5) split in two pieces. */ private final Dfp[] ln5Split; /** A {@link Dfp} with value ln(10). */ private final Dfp ln10; /** Current rounding mode. */ private RoundingMode rMode; /** IEEE 854-1987 signals. */ private int ieeeFlags; /** Create a factory for the specified number of radix digits. *

                              * Note that since the {@link Dfp} class uses 10000 as its radix, each radix * digit is equivalent to 4 decimal digits. This implies that asking for * 13, 14, 15 or 16 decimal digits will really lead to a 4 radix 10000 digits in * all cases. *

                              * @param decimalDigits minimal number of decimal digits. */ public DfpField(final int decimalDigits) { this(decimalDigits, true); } /** Create a factory for the specified number of radix digits. *

                              * Note that since the {@link Dfp} class uses 10000 as its radix, each radix * digit is equivalent to 4 decimal digits. This implies that asking for * 13, 14, 15 or 16 decimal digits will really lead to a 4 radix 10000 digits in * all cases. *

                              * @param decimalDigits minimal number of decimal digits * @param computeConstants if true, the transcendental constants for the given precision * must be computed (setting this flag to false is RESERVED for the internal recursive call) */ private DfpField(final int decimalDigits, final boolean computeConstants) { this.radixDigits = (decimalDigits < 13) ? 4 : (decimalDigits + 3) / 4; this.rMode = RoundingMode.ROUND_HALF_EVEN; this.ieeeFlags = 0; this.zero = new Dfp(this, 0); this.one = new Dfp(this, 1); this.two = new Dfp(this, 2); if (computeConstants) { // set up transcendental constants synchronized (DfpField.class) { // as a heuristic to circumvent Table-Maker's Dilemma, we set the string // representation of the constants to be at least 3 times larger than the // number of decimal digits, also as an attempt to really compute these // constants only once, we set a minimum number of digits computeStringConstants((decimalDigits < 67) ? 200 : (3 * decimalDigits)); // set up the constants at current field accuracy sqr2 = new Dfp(this, sqr2String); sqr2Split = split(sqr2String); sqr2Reciprocal = new Dfp(this, sqr2ReciprocalString); sqr3 = new Dfp(this, sqr3String); sqr3Reciprocal = new Dfp(this, sqr3ReciprocalString); pi = new Dfp(this, piString); piSplit = split(piString); e = new Dfp(this, eString); eSplit = split(eString); ln2 = new Dfp(this, ln2String); ln2Split = split(ln2String); ln5 = new Dfp(this, ln5String); ln5Split = split(ln5String); ln10 = new Dfp(this, ln10String); } } else { // dummy settings for unused constants sqr2 = null; sqr2Split = null; sqr2Reciprocal = null; sqr3 = null; sqr3Reciprocal = null; pi = null; piSplit = null; e = null; eSplit = null; ln2 = null; ln2Split = null; ln5 = null; ln5Split = null; ln10 = null; } } /** Get the number of radix digits of the {@link Dfp} instances built by this factory. * @return number of radix digits */ public int getRadixDigits() { return radixDigits; } /** Set the rounding mode. * If not set, the default value is {@link RoundingMode#ROUND_HALF_EVEN}. * @param mode desired rounding mode * Note that the rounding mode is common to all {@link Dfp} instances * belonging to the current {@link DfpField} in the system and will * affect all future calculations. */ public void setRoundingMode(final RoundingMode mode) { rMode = mode; } /** Get the current rounding mode. * @return current rounding mode */ public RoundingMode getRoundingMode() { return rMode; } /** Get the IEEE 854 status flags. * @return IEEE 854 status flags * @see #clearIEEEFlags() * @see #setIEEEFlags(int) * @see #setIEEEFlagsBits(int) * @see #FLAG_INVALID * @see #FLAG_DIV_ZERO * @see #FLAG_OVERFLOW * @see #FLAG_UNDERFLOW * @see #FLAG_INEXACT */ public int getIEEEFlags() { return ieeeFlags; } /** Clears the IEEE 854 status flags. * @see #getIEEEFlags() * @see #setIEEEFlags(int) * @see #setIEEEFlagsBits(int) * @see #FLAG_INVALID * @see #FLAG_DIV_ZERO * @see #FLAG_OVERFLOW * @see #FLAG_UNDERFLOW * @see #FLAG_INEXACT */ public void clearIEEEFlags() { ieeeFlags = 0; } /** Sets the IEEE 854 status flags. * @param flags desired value for the flags * @see #getIEEEFlags() * @see #clearIEEEFlags() * @see #setIEEEFlagsBits(int) * @see #FLAG_INVALID * @see #FLAG_DIV_ZERO * @see #FLAG_OVERFLOW * @see #FLAG_UNDERFLOW * @see #FLAG_INEXACT */ public void setIEEEFlags(final int flags) { ieeeFlags = flags & (FLAG_INVALID | FLAG_DIV_ZERO | FLAG_OVERFLOW | FLAG_UNDERFLOW | FLAG_INEXACT); } /** Sets some bits in the IEEE 854 status flags, without changing the already set bits. *

                              * Calling this method is equivalent to call {@code setIEEEFlags(getIEEEFlags() | bits)} *

                              * @param bits bits to set * @see #getIEEEFlags() * @see #clearIEEEFlags() * @see #setIEEEFlags(int) * @see #FLAG_INVALID * @see #FLAG_DIV_ZERO * @see #FLAG_OVERFLOW * @see #FLAG_UNDERFLOW * @see #FLAG_INEXACT */ public void setIEEEFlagsBits(final int bits) { ieeeFlags |= bits & (FLAG_INVALID | FLAG_DIV_ZERO | FLAG_OVERFLOW | FLAG_UNDERFLOW | FLAG_INEXACT); } /** Makes a {@link Dfp} with a value of 0. * @return a new {@link Dfp} with a value of 0 */ public Dfp newDfp() { return new Dfp(this); } /** Create an instance from a byte value. * @param x value to convert to an instance * @return a new {@link Dfp} with the same value as x */ public Dfp newDfp(final byte x) { return new Dfp(this, x); } /** Create an instance from an int value. * @param x value to convert to an instance * @return a new {@link Dfp} with the same value as x */ public Dfp newDfp(final int x) { return new Dfp(this, x); } /** Create an instance from a long value. * @param x value to convert to an instance * @return a new {@link Dfp} with the same value as x */ public Dfp newDfp(final long x) { return new Dfp(this, x); } /** Create an instance from a double value. * @param x value to convert to an instance * @return a new {@link Dfp} with the same value as x */ public Dfp newDfp(final double x) { return new Dfp(this, x); } /** Copy constructor. * @param d instance to copy * @return a new {@link Dfp} with the same value as d */ public Dfp newDfp(Dfp d) { return new Dfp(d); } /** Create a {@link Dfp} given a String representation. * @param s string representation of the instance * @return a new {@link Dfp} parsed from specified string */ public Dfp newDfp(final String s) { return new Dfp(this, s); } /** Creates a {@link Dfp} with a non-finite value. * @param sign sign of the Dfp to create * @param nans code of the value, must be one of {@link Dfp#INFINITE}, * {@link Dfp#SNAN}, {@link Dfp#QNAN} * @return a new {@link Dfp} with a non-finite value */ public Dfp newDfp(final byte sign, final byte nans) { return new Dfp(this, sign, nans); } /** Get the constant 0. * @return a {@link Dfp} with value 0 */ public Dfp getZero() { return zero; } /** Get the constant 1. * @return a {@link Dfp} with value 1 */ public Dfp getOne() { return one; } /** {@inheritDoc} */ public Class> getRuntimeClass() { return Dfp.class; } /** Get the constant 2. * @return a {@link Dfp} with value 2 */ public Dfp getTwo() { return two; } /** Get the constant √2. * @return a {@link Dfp} with value √2 */ public Dfp getSqr2() { return sqr2; } /** Get the constant √2 split in two pieces. * @return a {@link Dfp} with value √2 split in two pieces */ public Dfp[] getSqr2Split() { return sqr2Split.clone(); } /** Get the constant √2 / 2. * @return a {@link Dfp} with value √2 / 2 */ public Dfp getSqr2Reciprocal() { return sqr2Reciprocal; } /** Get the constant √3. * @return a {@link Dfp} with value √3 */ public Dfp getSqr3() { return sqr3; } /** Get the constant √3 / 3. * @return a {@link Dfp} with value √3 / 3 */ public Dfp getSqr3Reciprocal() { return sqr3Reciprocal; } /** Get the constant π. * @return a {@link Dfp} with value π */ public Dfp getPi() { return pi; } /** Get the constant π split in two pieces. * @return a {@link Dfp} with value π split in two pieces */ public Dfp[] getPiSplit() { return piSplit.clone(); } /** Get the constant e. * @return a {@link Dfp} with value e */ public Dfp getE() { return e; } /** Get the constant e split in two pieces. * @return a {@link Dfp} with value e split in two pieces */ public Dfp[] getESplit() { return eSplit.clone(); } /** Get the constant ln(2). * @return a {@link Dfp} with value ln(2) */ public Dfp getLn2() { return ln2; } /** Get the constant ln(2) split in two pieces. * @return a {@link Dfp} with value ln(2) split in two pieces */ public Dfp[] getLn2Split() { return ln2Split.clone(); } /** Get the constant ln(5). * @return a {@link Dfp} with value ln(5) */ public Dfp getLn5() { return ln5; } /** Get the constant ln(5) split in two pieces. * @return a {@link Dfp} with value ln(5) split in two pieces */ public Dfp[] getLn5Split() { return ln5Split.clone(); } /** Get the constant ln(10). * @return a {@link Dfp} with value ln(10) */ public Dfp getLn10() { return ln10; } /** Breaks a string representation up into two {@link Dfp}'s. * The split is such that the sum of them is equivalent to the input string, * but has higher precision than using a single Dfp. * @param a string representation of the number to split * @return an array of two {@link Dfp Dfp} instances which sum equals a */ private Dfp[] split(final String a) { Dfp result[] = new Dfp[2]; boolean leading = true; int sp = 0; int sig = 0; char[] buf = new char[a.length()]; for (int i = 0; i < buf.length; i++) { buf[i] = a.charAt(i); if (buf[i] >= '1' && buf[i] <= '9') { leading = false; } if (buf[i] == '.') { sig += (400 - sig) % 4; leading = false; } if (sig == (radixDigits / 2) * 4) { sp = i; break; } if (buf[i] >= '0' && buf[i] <= '9' && !leading) { sig ++; } } result[0] = new Dfp(this, new String(buf, 0, sp)); for (int i = 0; i < buf.length; i++) { buf[i] = a.charAt(i); if (buf[i] >= '0' && buf[i] <= '9' && i < sp) { buf[i] = '0'; } } result[1] = new Dfp(this, new String(buf)); return result; } /** Recompute the high precision string constants. * @param highPrecisionDecimalDigits precision at which the string constants mus be computed */ private static void computeStringConstants(final int highPrecisionDecimalDigits) { if (sqr2String == null || sqr2String.length() < highPrecisionDecimalDigits - 3) { // recompute the string representation of the transcendental constants final DfpField highPrecisionField = new DfpField(highPrecisionDecimalDigits, false); final Dfp highPrecisionOne = new Dfp(highPrecisionField, 1); final Dfp highPrecisionTwo = new Dfp(highPrecisionField, 2); final Dfp highPrecisionThree = new Dfp(highPrecisionField, 3); final Dfp highPrecisionSqr2 = highPrecisionTwo.sqrt(); sqr2String = highPrecisionSqr2.toString(); sqr2ReciprocalString = highPrecisionOne.divide(highPrecisionSqr2).toString(); final Dfp highPrecisionSqr3 = highPrecisionThree.sqrt(); sqr3String = highPrecisionSqr3.toString(); sqr3ReciprocalString = highPrecisionOne.divide(highPrecisionSqr3).toString(); piString = computePi(highPrecisionOne, highPrecisionTwo, highPrecisionThree).toString(); eString = computeExp(highPrecisionOne, highPrecisionOne).toString(); ln2String = computeLn(highPrecisionTwo, highPrecisionOne, highPrecisionTwo).toString(); ln5String = computeLn(new Dfp(highPrecisionField, 5), highPrecisionOne, highPrecisionTwo).toString(); ln10String = computeLn(new Dfp(highPrecisionField, 10), highPrecisionOne, highPrecisionTwo).toString(); } } /** Compute π using Jonathan and Peter Borwein quartic formula. * @param one constant with value 1 at desired precision * @param two constant with value 2 at desired precision * @param three constant with value 3 at desired precision * @return π */ private static Dfp computePi(final Dfp one, final Dfp two, final Dfp three) { Dfp sqrt2 = two.sqrt(); Dfp yk = sqrt2.subtract(one); Dfp four = two.add(two); Dfp two2kp3 = two; Dfp ak = two.multiply(three.subtract(two.multiply(sqrt2))); // The formula converges quartically. This means the number of correct // digits is multiplied by 4 at each iteration! Five iterations are // sufficient for about 160 digits, eight iterations give about // 10000 digits (this has been checked) and 20 iterations more than // 160 billions of digits (this has NOT been checked). // So the limit here is considered sufficient for most purposes ... for (int i = 1; i < 20; i++) { final Dfp ykM1 = yk; final Dfp y2 = yk.multiply(yk); final Dfp oneMinusY4 = one.subtract(y2.multiply(y2)); final Dfp s = oneMinusY4.sqrt().sqrt(); yk = one.subtract(s).divide(one.add(s)); two2kp3 = two2kp3.multiply(four); final Dfp p = one.add(yk); final Dfp p2 = p.multiply(p); ak = ak.multiply(p2.multiply(p2)).subtract(two2kp3.multiply(yk).multiply(one.add(yk).add(yk.multiply(yk)))); if (yk.equals(ykM1)) { break; } } return one.divide(ak); } /** Compute exp(a). * @param a number for which we want the exponential * @param one constant with value 1 at desired precision * @return exp(a) */ public static Dfp computeExp(final Dfp a, final Dfp one) { Dfp y = new Dfp(one); Dfp py = new Dfp(one); Dfp f = new Dfp(one); Dfp fi = new Dfp(one); Dfp x = new Dfp(one); for (int i = 0; i < 10000; i++) { x = x.multiply(a); y = y.add(x.divide(f)); fi = fi.add(one); f = f.multiply(fi); if (y.equals(py)) { break; } py = new Dfp(y); } return y; } /** Compute ln(a). * * Let f(x) = ln(x), * * We know that f'(x) = 1/x, thus from Taylor's theorem we have: * * ----- n+1 n * f(x) = \ (-1) (x - 1) * / ---------------- for 1 <= n <= infinity * ----- n * * or * 2 3 4 * (x-1) (x-1) (x-1) * ln(x) = (x-1) - ----- + ------ - ------ + ... * 2 3 4 * * alternatively, * * 2 3 4 * x x x * ln(x+1) = x - - + - - - + ... * 2 3 4 * * This series can be used to compute ln(x), but it converges too slowly. * * If we substitute -x for x above, we get * * 2 3 4 * x x x * ln(1-x) = -x - - - - - - + ... * 2 3 4 * * Note that all terms are now negative. Because the even powered ones * absorbed the sign. Now, subtract the series above from the previous * one to get ln(x+1) - ln(1-x). Note the even terms cancel out leaving * only the odd ones * * 3 5 7 * 2x 2x 2x * ln(x+1) - ln(x-1) = 2x + --- + --- + ---- + ... * 3 5 7 * * By the property of logarithms that ln(a) - ln(b) = ln (a/b) we have: * * 3 5 7 * x+1 / x x x \ * ln ----- = 2 * | x + ---- + ---- + ---- + ... | * x-1 \ 3 5 7 / * * But now we want to find ln(a), so we need to find the value of x * such that a = (x+1)/(x-1). This is easily solved to find that * x = (a-1)/(a+1). * @param a number for which we want the exponential * @param one constant with value 1 at desired precision * @param two constant with value 2 at desired precision * @return ln(a) */ public static Dfp computeLn(final Dfp a, final Dfp one, final Dfp two) { int den = 1; Dfp x = a.add(new Dfp(a.getField(), -1)).divide(a.add(one)); Dfp y = new Dfp(x); Dfp num = new Dfp(x); Dfp py = new Dfp(y); for (int i = 0; i < 10000; i++) { num = num.multiply(x); num = num.multiply(x); den = den + 2; Dfp t = num.divide(den); y = y.add(t); if (y.equals(py)) { break; } py = new Dfp(y); } return y.multiply(two); } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/dfp/Dfp.java100644 1750 1750 247465 12126627716 24735 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.dfp; import java.util.Arrays; import org.apache.commons.math3.RealFieldElement; import org.apache.commons.math3.exception.DimensionMismatchException; import org.apache.commons.math3.util.FastMath; /** * Decimal floating point library for Java * *

                              Another floating point class. This one is built using radix 10000 * which is 104, so its almost decimal.

                              * *

                              The design goals here are: *

                                *
                              1. Decimal math, or close to it
                              2. *
                              3. Settable precision (but no mix between numbers using different settings)
                              4. *
                              5. Portability. Code should be kept as portable as possible.
                              6. *
                              7. Performance
                              8. *
                              9. Accuracy - Results should always be +/- 1 ULP for basic * algebraic operation
                              10. *
                              11. Comply with IEEE 854-1987 as much as possible. * (See IEEE 854-1987 notes below)
                              12. *

                              * *

                              Trade offs: *

                                *
                              1. Memory foot print. I'm using more memory than necessary to * represent numbers to get better performance.
                              2. *
                              3. Digits are bigger, so rounding is a greater loss. So, if you * really need 12 decimal digits, better use 4 base 10000 digits * there can be one partially filled.
                              4. *

                              * *

                              Numbers are represented in the following form: *

                               *  n  =  sign × mant × (radix)exp;

                              *
                              * where sign is ±1, mantissa represents a fractional number between * zero and one. mant[0] is the least significant digit. * exp is in the range of -32767 to 32768

                              * *

                              IEEE 854-1987 Notes and differences

                              * *

                              IEEE 854 requires the radix to be either 2 or 10. The radix here is * 10000, so that requirement is not met, but it is possible that a * subclassed can be made to make it behave as a radix 10 * number. It is my opinion that if it looks and behaves as a radix * 10 number then it is one and that requirement would be met.

                              * *

                              The radix of 10000 was chosen because it should be faster to operate * on 4 decimal digits at once instead of one at a time. Radix 10 behavior * can be realized by adding an additional rounding step to ensure that * the number of decimal digits represented is constant.

                              * *

                              The IEEE standard specifically leaves out internal data encoding, * so it is reasonable to conclude that such a subclass of this radix * 10000 system is merely an encoding of a radix 10 system.

                              * *

                              IEEE 854 also specifies the existence of "sub-normal" numbers. This * class does not contain any such entities. The most significant radix * 10000 digit is always non-zero. Instead, we support "gradual underflow" * by raising the underflow flag for numbers less with exponent less than * expMin, but don't flush to zero until the exponent reaches MIN_EXP-digits. * Thus the smallest number we can represent would be: * 1E(-(MIN_EXP-digits-1)*4), eg, for digits=5, MIN_EXP=-32767, that would * be 1e-131092.

                              * *

                              IEEE 854 defines that the implied radix point lies just to the right * of the most significant digit and to the left of the remaining digits. * This implementation puts the implied radix point to the left of all * digits including the most significant one. The most significant digit * here is the one just to the right of the radix point. This is a fine * detail and is really only a matter of definition. Any side effects of * this can be rendered invisible by a subclass.

                              * @see DfpField * @version $Id: Dfp.java 1462423 2013-03-29 07:25:18Z luc $ * @since 2.2 */ public class Dfp implements RealFieldElement { /** The radix, or base of this system. Set to 10000 */ public static final int RADIX = 10000; /** The minimum exponent before underflow is signaled. Flush to zero * occurs at minExp-DIGITS */ public static final int MIN_EXP = -32767; /** The maximum exponent before overflow is signaled and results flushed * to infinity */ public static final int MAX_EXP = 32768; /** The amount under/overflows are scaled by before going to trap handler */ public static final int ERR_SCALE = 32760; /** Indicator value for normal finite numbers. */ public static final byte FINITE = 0; /** Indicator value for Infinity. */ public static final byte INFINITE = 1; /** Indicator value for signaling NaN. */ public static final byte SNAN = 2; /** Indicator value for quiet NaN. */ public static final byte QNAN = 3; /** String for NaN representation. */ private static final String NAN_STRING = "NaN"; /** String for positive infinity representation. */ private static final String POS_INFINITY_STRING = "Infinity"; /** String for negative infinity representation. */ private static final String NEG_INFINITY_STRING = "-Infinity"; /** Name for traps triggered by addition. */ private static final String ADD_TRAP = "add"; /** Name for traps triggered by multiplication. */ private static final String MULTIPLY_TRAP = "multiply"; /** Name for traps triggered by division. */ private static final String DIVIDE_TRAP = "divide"; /** Name for traps triggered by square root. */ private static final String SQRT_TRAP = "sqrt"; /** Name for traps triggered by alignment. */ private static final String ALIGN_TRAP = "align"; /** Name for traps triggered by truncation. */ private static final String TRUNC_TRAP = "trunc"; /** Name for traps triggered by nextAfter. */ private static final String NEXT_AFTER_TRAP = "nextAfter"; /** Name for traps triggered by lessThan. */ private static final String LESS_THAN_TRAP = "lessThan"; /** Name for traps triggered by greaterThan. */ private static final String GREATER_THAN_TRAP = "greaterThan"; /** Name for traps triggered by newInstance. */ private static final String NEW_INSTANCE_TRAP = "newInstance"; /** Mantissa. */ protected int[] mant; /** Sign bit: 1 for positive, -1 for negative. */ protected byte sign; /** Exponent. */ protected int exp; /** Indicator for non-finite / non-number values. */ protected byte nans; /** Factory building similar Dfp's. */ private final DfpField field; /** Makes an instance with a value of zero. * @param field field to which this instance belongs */ protected Dfp(final DfpField field) { mant = new int[field.getRadixDigits()]; sign = 1; exp = 0; nans = FINITE; this.field = field; } /** Create an instance from a byte value. * @param field field to which this instance belongs * @param x value to convert to an instance */ protected Dfp(final DfpField field, byte x) { this(field, (long) x); } /** Create an instance from an int value. * @param field field to which this instance belongs * @param x value to convert to an instance */ protected Dfp(final DfpField field, int x) { this(field, (long) x); } /** Create an instance from a long value. * @param field field to which this instance belongs * @param x value to convert to an instance */ protected Dfp(final DfpField field, long x) { // initialize as if 0 mant = new int[field.getRadixDigits()]; nans = FINITE; this.field = field; boolean isLongMin = false; if (x == Long.MIN_VALUE) { // special case for Long.MIN_VALUE (-9223372036854775808) // we must shift it before taking its absolute value isLongMin = true; ++x; } // set the sign if (x < 0) { sign = -1; x = -x; } else { sign = 1; } exp = 0; while (x != 0) { System.arraycopy(mant, mant.length - exp, mant, mant.length - 1 - exp, exp); mant[mant.length - 1] = (int) (x % RADIX); x /= RADIX; exp++; } if (isLongMin) { // remove the shift added for Long.MIN_VALUE // we know in this case that fixing the last digit is sufficient for (int i = 0; i < mant.length - 1; i++) { if (mant[i] != 0) { mant[i]++; break; } } } } /** Create an instance from a double value. * @param field field to which this instance belongs * @param x value to convert to an instance */ protected Dfp(final DfpField field, double x) { // initialize as if 0 mant = new int[field.getRadixDigits()]; sign = 1; exp = 0; nans = FINITE; this.field = field; long bits = Double.doubleToLongBits(x); long mantissa = bits & 0x000fffffffffffffL; int exponent = (int) ((bits & 0x7ff0000000000000L) >> 52) - 1023; if (exponent == -1023) { // Zero or sub-normal if (x == 0) { // make sure 0 has the right sign if ((bits & 0x8000000000000000L) != 0) { sign = -1; } return; } exponent++; // Normalize the subnormal number while ( (mantissa & 0x0010000000000000L) == 0) { exponent--; mantissa <<= 1; } mantissa &= 0x000fffffffffffffL; } if (exponent == 1024) { // infinity or NAN if (x != x) { sign = (byte) 1; nans = QNAN; } else if (x < 0) { sign = (byte) -1; nans = INFINITE; } else { sign = (byte) 1; nans = INFINITE; } return; } Dfp xdfp = new Dfp(field, mantissa); xdfp = xdfp.divide(new Dfp(field, 4503599627370496l)).add(field.getOne()); // Divide by 2^52, then add one xdfp = xdfp.multiply(DfpMath.pow(field.getTwo(), exponent)); if ((bits & 0x8000000000000000L) != 0) { xdfp = xdfp.negate(); } System.arraycopy(xdfp.mant, 0, mant, 0, mant.length); sign = xdfp.sign; exp = xdfp.exp; nans = xdfp.nans; } /** Copy constructor. * @param d instance to copy */ public Dfp(final Dfp d) { mant = d.mant.clone(); sign = d.sign; exp = d.exp; nans = d.nans; field = d.field; } /** Create an instance from a String representation. * @param field field to which this instance belongs * @param s string representation of the instance */ protected Dfp(final DfpField field, final String s) { // initialize as if 0 mant = new int[field.getRadixDigits()]; sign = 1; exp = 0; nans = FINITE; this.field = field; boolean decimalFound = false; final int rsize = 4; // size of radix in decimal digits final int offset = 4; // Starting offset into Striped final char[] striped = new char[getRadixDigits() * rsize + offset * 2]; // Check some special cases if (s.equals(POS_INFINITY_STRING)) { sign = (byte) 1; nans = INFINITE; return; } if (s.equals(NEG_INFINITY_STRING)) { sign = (byte) -1; nans = INFINITE; return; } if (s.equals(NAN_STRING)) { sign = (byte) 1; nans = QNAN; return; } // Check for scientific notation int p = s.indexOf("e"); if (p == -1) { // try upper case? p = s.indexOf("E"); } final String fpdecimal; int sciexp = 0; if (p != -1) { // scientific notation fpdecimal = s.substring(0, p); String fpexp = s.substring(p+1); boolean negative = false; for (int i=0; i= '0' && fpexp.charAt(i) <= '9') { sciexp = sciexp * 10 + fpexp.charAt(i) - '0'; } } if (negative) { sciexp = -sciexp; } } else { // normal case fpdecimal = s; } // If there is a minus sign in the number then it is negative if (fpdecimal.indexOf("-") != -1) { sign = -1; } // First off, find all of the leading zeros, trailing zeros, and significant digits p = 0; // Move p to first significant digit int decimalPos = 0; for (;;) { if (fpdecimal.charAt(p) >= '1' && fpdecimal.charAt(p) <= '9') { break; } if (decimalFound && fpdecimal.charAt(p) == '0') { decimalPos--; } if (fpdecimal.charAt(p) == '.') { decimalFound = true; } p++; if (p == fpdecimal.length()) { break; } } // Copy the string onto Stripped int q = offset; striped[0] = '0'; striped[1] = '0'; striped[2] = '0'; striped[3] = '0'; int significantDigits=0; for(;;) { if (p == (fpdecimal.length())) { break; } // Don't want to run pass the end of the array if (q == mant.length*rsize+offset+1) { break; } if (fpdecimal.charAt(p) == '.') { decimalFound = true; decimalPos = significantDigits; p++; continue; } if (fpdecimal.charAt(p) < '0' || fpdecimal.charAt(p) > '9') { p++; continue; } striped[q] = fpdecimal.charAt(p); q++; p++; significantDigits++; } // If the decimal point has been found then get rid of trailing zeros. if (decimalFound && q != offset) { for (;;) { q--; if (q == offset) { break; } if (striped[q] == '0') { significantDigits--; } else { break; } } } // special case of numbers like "0.00000" if (decimalFound && significantDigits == 0) { decimalPos = 0; } // Implicit decimal point at end of number if not present if (!decimalFound) { decimalPos = q-offset; } // Find the number of significant trailing zeros q = offset; // set q to point to first sig digit p = significantDigits-1+offset; while (p > q) { if (striped[p] != '0') { break; } p--; } // Make sure the decimal is on a mod 10000 boundary int i = ((rsize * 100) - decimalPos - sciexp % rsize) % rsize; q -= i; decimalPos += i; // Make the mantissa length right by adding zeros at the end if necessary while ((p - q) < (mant.length * rsize)) { for (i = 0; i < rsize; i++) { striped[++p] = '0'; } } // Ok, now we know how many trailing zeros there are, // and where the least significant digit is for (i = mant.length - 1; i >= 0; i--) { mant[i] = (striped[q] - '0') * 1000 + (striped[q+1] - '0') * 100 + (striped[q+2] - '0') * 10 + (striped[q+3] - '0'); q += 4; } exp = (decimalPos+sciexp) / rsize; if (q < striped.length) { // Is there possible another digit? round((striped[q] - '0')*1000); } } /** Creates an instance with a non-finite value. * @param field field to which this instance belongs * @param sign sign of the Dfp to create * @param nans code of the value, must be one of {@link #INFINITE}, * {@link #SNAN}, {@link #QNAN} */ protected Dfp(final DfpField field, final byte sign, final byte nans) { this.field = field; this.mant = new int[field.getRadixDigits()]; this.sign = sign; this.exp = 0; this.nans = nans; } /** Create an instance with a value of 0. * Use this internally in preference to constructors to facilitate subclasses * @return a new instance with a value of 0 */ public Dfp newInstance() { return new Dfp(getField()); } /** Create an instance from a byte value. * @param x value to convert to an instance * @return a new instance with value x */ public Dfp newInstance(final byte x) { return new Dfp(getField(), x); } /** Create an instance from an int value. * @param x value to convert to an instance * @return a new instance with value x */ public Dfp newInstance(final int x) { return new Dfp(getField(), x); } /** Create an instance from a long value. * @param x value to convert to an instance * @return a new instance with value x */ public Dfp newInstance(final long x) { return new Dfp(getField(), x); } /** Create an instance from a double value. * @param x value to convert to an instance * @return a new instance with value x */ public Dfp newInstance(final double x) { return new Dfp(getField(), x); } /** Create an instance by copying an existing one. * Use this internally in preference to constructors to facilitate subclasses. * @param d instance to copy * @return a new instance with the same value as d */ public Dfp newInstance(final Dfp d) { // make sure we don't mix number with different precision if (field.getRadixDigits() != d.field.getRadixDigits()) { field.setIEEEFlagsBits(DfpField.FLAG_INVALID); final Dfp result = newInstance(getZero()); result.nans = QNAN; return dotrap(DfpField.FLAG_INVALID, NEW_INSTANCE_TRAP, d, result); } return new Dfp(d); } /** Create an instance from a String representation. * Use this internally in preference to constructors to facilitate subclasses. * @param s string representation of the instance * @return a new instance parsed from specified string */ public Dfp newInstance(final String s) { return new Dfp(field, s); } /** Creates an instance with a non-finite value. * @param sig sign of the Dfp to create * @param code code of the value, must be one of {@link #INFINITE}, * {@link #SNAN}, {@link #QNAN} * @return a new instance with a non-finite value */ public Dfp newInstance(final byte sig, final byte code) { return field.newDfp(sig, code); } /** Get the {@link org.apache.commons.math3.Field Field} (really a {@link DfpField}) to which the instance belongs. *

                              * The field is linked to the number of digits and acts as a factory * for {@link Dfp} instances. *

                              * @return {@link org.apache.commons.math3.Field Field} (really a {@link DfpField}) to which the instance belongs */ public DfpField getField() { return field; } /** Get the number of radix digits of the instance. * @return number of radix digits */ public int getRadixDigits() { return field.getRadixDigits(); } /** Get the constant 0. * @return a Dfp with value zero */ public Dfp getZero() { return field.getZero(); } /** Get the constant 1. * @return a Dfp with value one */ public Dfp getOne() { return field.getOne(); } /** Get the constant 2. * @return a Dfp with value two */ public Dfp getTwo() { return field.getTwo(); } /** Shift the mantissa left, and adjust the exponent to compensate. */ protected void shiftLeft() { for (int i = mant.length - 1; i > 0; i--) { mant[i] = mant[i-1]; } mant[0] = 0; exp--; } /* Note that shiftRight() does not call round() as that round() itself uses shiftRight() */ /** Shift the mantissa right, and adjust the exponent to compensate. */ protected void shiftRight() { for (int i = 0; i < mant.length - 1; i++) { mant[i] = mant[i+1]; } mant[mant.length - 1] = 0; exp++; } /** Make our exp equal to the supplied one, this may cause rounding. * Also causes de-normalized numbers. These numbers are generally * dangerous because most routines assume normalized numbers. * Align doesn't round, so it will return the last digit destroyed * by shifting right. * @param e desired exponent * @return last digit destroyed by shifting right */ protected int align(int e) { int lostdigit = 0; boolean inexact = false; int diff = exp - e; int adiff = diff; if (adiff < 0) { adiff = -adiff; } if (diff == 0) { return 0; } if (adiff > (mant.length + 1)) { // Special case Arrays.fill(mant, 0); exp = e; field.setIEEEFlagsBits(DfpField.FLAG_INEXACT); dotrap(DfpField.FLAG_INEXACT, ALIGN_TRAP, this, this); return 0; } for (int i = 0; i < adiff; i++) { if (diff < 0) { /* Keep track of loss -- only signal inexact after losing 2 digits. * the first lost digit is returned to add() and may be incorporated * into the result. */ if (lostdigit != 0) { inexact = true; } lostdigit = mant[0]; shiftRight(); } else { shiftLeft(); } } if (inexact) { field.setIEEEFlagsBits(DfpField.FLAG_INEXACT); dotrap(DfpField.FLAG_INEXACT, ALIGN_TRAP, this, this); } return lostdigit; } /** Check if instance is less than x. * @param x number to check instance against * @return true if instance is less than x and neither are NaN, false otherwise */ public boolean lessThan(final Dfp x) { // make sure we don't mix number with different precision if (field.getRadixDigits() != x.field.getRadixDigits()) { field.setIEEEFlagsBits(DfpField.FLAG_INVALID); final Dfp result = newInstance(getZero()); result.nans = QNAN; dotrap(DfpField.FLAG_INVALID, LESS_THAN_TRAP, x, result); return false; } /* if a nan is involved, signal invalid and return false */ if (isNaN() || x.isNaN()) { field.setIEEEFlagsBits(DfpField.FLAG_INVALID); dotrap(DfpField.FLAG_INVALID, LESS_THAN_TRAP, x, newInstance(getZero())); return false; } return compare(this, x) < 0; } /** Check if instance is greater than x. * @param x number to check instance against * @return true if instance is greater than x and neither are NaN, false otherwise */ public boolean greaterThan(final Dfp x) { // make sure we don't mix number with different precision if (field.getRadixDigits() != x.field.getRadixDigits()) { field.setIEEEFlagsBits(DfpField.FLAG_INVALID); final Dfp result = newInstance(getZero()); result.nans = QNAN; dotrap(DfpField.FLAG_INVALID, GREATER_THAN_TRAP, x, result); return false; } /* if a nan is involved, signal invalid and return false */ if (isNaN() || x.isNaN()) { field.setIEEEFlagsBits(DfpField.FLAG_INVALID); dotrap(DfpField.FLAG_INVALID, GREATER_THAN_TRAP, x, newInstance(getZero())); return false; } return compare(this, x) > 0; } /** Check if instance is less than or equal to 0. * @return true if instance is not NaN and less than or equal to 0, false otherwise */ public boolean negativeOrNull() { if (isNaN()) { field.setIEEEFlagsBits(DfpField.FLAG_INVALID); dotrap(DfpField.FLAG_INVALID, LESS_THAN_TRAP, this, newInstance(getZero())); return false; } return (sign < 0) || ((mant[mant.length - 1] == 0) && !isInfinite()); } /** Check if instance is strictly less than 0. * @return true if instance is not NaN and less than or equal to 0, false otherwise */ public boolean strictlyNegative() { if (isNaN()) { field.setIEEEFlagsBits(DfpField.FLAG_INVALID); dotrap(DfpField.FLAG_INVALID, LESS_THAN_TRAP, this, newInstance(getZero())); return false; } return (sign < 0) && ((mant[mant.length - 1] != 0) || isInfinite()); } /** Check if instance is greater than or equal to 0. * @return true if instance is not NaN and greater than or equal to 0, false otherwise */ public boolean positiveOrNull() { if (isNaN()) { field.setIEEEFlagsBits(DfpField.FLAG_INVALID); dotrap(DfpField.FLAG_INVALID, LESS_THAN_TRAP, this, newInstance(getZero())); return false; } return (sign > 0) || ((mant[mant.length - 1] == 0) && !isInfinite()); } /** Check if instance is strictly greater than 0. * @return true if instance is not NaN and greater than or equal to 0, false otherwise */ public boolean strictlyPositive() { if (isNaN()) { field.setIEEEFlagsBits(DfpField.FLAG_INVALID); dotrap(DfpField.FLAG_INVALID, LESS_THAN_TRAP, this, newInstance(getZero())); return false; } return (sign > 0) && ((mant[mant.length - 1] != 0) || isInfinite()); } /** Get the absolute value of instance. * @return absolute value of instance * @since 3.2 */ public Dfp abs() { Dfp result = newInstance(this); result.sign = 1; return result; } /** Check if instance is infinite. * @return true if instance is infinite */ public boolean isInfinite() { return nans == INFINITE; } /** Check if instance is not a number. * @return true if instance is not a number */ public boolean isNaN() { return (nans == QNAN) || (nans == SNAN); } /** Check if instance is equal to zero. * @return true if instance is equal to zero */ public boolean isZero() { if (isNaN()) { field.setIEEEFlagsBits(DfpField.FLAG_INVALID); dotrap(DfpField.FLAG_INVALID, LESS_THAN_TRAP, this, newInstance(getZero())); return false; } return (mant[mant.length - 1] == 0) && !isInfinite(); } /** Check if instance is equal to x. * @param other object to check instance against * @return true if instance is equal to x and neither are NaN, false otherwise */ @Override public boolean equals(final Object other) { if (other instanceof Dfp) { final Dfp x = (Dfp) other; if (isNaN() || x.isNaN() || field.getRadixDigits() != x.field.getRadixDigits()) { return false; } return compare(this, x) == 0; } return false; } /** * Gets a hashCode for the instance. * @return a hash code value for this object */ @Override public int hashCode() { return 17 + (sign << 8) + (nans << 16) + exp + Arrays.hashCode(mant); } /** Check if instance is not equal to x. * @param x number to check instance against * @return true if instance is not equal to x and neither are NaN, false otherwise */ public boolean unequal(final Dfp x) { if (isNaN() || x.isNaN() || field.getRadixDigits() != x.field.getRadixDigits()) { return false; } return greaterThan(x) || lessThan(x); } /** Compare two instances. * @param a first instance in comparison * @param b second instance in comparison * @return -1 if ab and 0 if a==b * Note this method does not properly handle NaNs or numbers with different precision. */ private static int compare(final Dfp a, final Dfp b) { // Ignore the sign of zero if (a.mant[a.mant.length - 1] == 0 && b.mant[b.mant.length - 1] == 0 && a.nans == FINITE && b.nans == FINITE) { return 0; } if (a.sign != b.sign) { if (a.sign == -1) { return -1; } else { return 1; } } // deal with the infinities if (a.nans == INFINITE && b.nans == FINITE) { return a.sign; } if (a.nans == FINITE && b.nans == INFINITE) { return -b.sign; } if (a.nans == INFINITE && b.nans == INFINITE) { return 0; } // Handle special case when a or b is zero, by ignoring the exponents if (b.mant[b.mant.length-1] != 0 && a.mant[b.mant.length-1] != 0) { if (a.exp < b.exp) { return -a.sign; } if (a.exp > b.exp) { return a.sign; } } // compare the mantissas for (int i = a.mant.length - 1; i >= 0; i--) { if (a.mant[i] > b.mant[i]) { return a.sign; } if (a.mant[i] < b.mant[i]) { return -a.sign; } } return 0; } /** Round to nearest integer using the round-half-even method. * That is round to nearest integer unless both are equidistant. * In which case round to the even one. * @return rounded value * @since 3.2 */ public Dfp rint() { return trunc(DfpField.RoundingMode.ROUND_HALF_EVEN); } /** Round to an integer using the round floor mode. * That is, round toward -Infinity * @return rounded value * @since 3.2 */ public Dfp floor() { return trunc(DfpField.RoundingMode.ROUND_FLOOR); } /** Round to an integer using the round ceil mode. * That is, round toward +Infinity * @return rounded value * @since 3.2 */ public Dfp ceil() { return trunc(DfpField.RoundingMode.ROUND_CEIL); } /** Returns the IEEE remainder. * @param d divisor * @return this less n × d, where n is the integer closest to this/d * @since 3.2 */ public Dfp remainder(final Dfp d) { final Dfp result = this.subtract(this.divide(d).rint().multiply(d)); // IEEE 854-1987 says that if the result is zero, then it carries the sign of this if (result.mant[mant.length-1] == 0) { result.sign = sign; } return result; } /** Does the integer conversions with the specified rounding. * @param rmode rounding mode to use * @return truncated value */ protected Dfp trunc(final DfpField.RoundingMode rmode) { boolean changed = false; if (isNaN()) { return newInstance(this); } if (nans == INFINITE) { return newInstance(this); } if (mant[mant.length-1] == 0) { // a is zero return newInstance(this); } /* If the exponent is less than zero then we can certainly * return zero */ if (exp < 0) { field.setIEEEFlagsBits(DfpField.FLAG_INEXACT); Dfp result = newInstance(getZero()); result = dotrap(DfpField.FLAG_INEXACT, TRUNC_TRAP, this, result); return result; } /* If the exponent is greater than or equal to digits, then it * must already be an integer since there is no precision left * for any fractional part */ if (exp >= mant.length) { return newInstance(this); } /* General case: create another dfp, result, that contains the * a with the fractional part lopped off. */ Dfp result = newInstance(this); for (int i = 0; i < mant.length-result.exp; i++) { changed |= result.mant[i] != 0; result.mant[i] = 0; } if (changed) { switch (rmode) { case ROUND_FLOOR: if (result.sign == -1) { // then we must increment the mantissa by one result = result.add(newInstance(-1)); } break; case ROUND_CEIL: if (result.sign == 1) { // then we must increment the mantissa by one result = result.add(getOne()); } break; case ROUND_HALF_EVEN: default: final Dfp half = newInstance("0.5"); Dfp a = subtract(result); // difference between this and result a.sign = 1; // force positive (take abs) if (a.greaterThan(half)) { a = newInstance(getOne()); a.sign = sign; result = result.add(a); } /** If exactly equal to 1/2 and odd then increment */ if (a.equals(half) && result.exp > 0 && (result.mant[mant.length-result.exp]&1) != 0) { a = newInstance(getOne()); a.sign = sign; result = result.add(a); } break; } field.setIEEEFlagsBits(DfpField.FLAG_INEXACT); // signal inexact result = dotrap(DfpField.FLAG_INEXACT, TRUNC_TRAP, this, result); return result; } return result; } /** Convert this to an integer. * If greater than 2147483647, it returns 2147483647. If less than -2147483648 it returns -2147483648. * @return converted number */ public int intValue() { Dfp rounded; int result = 0; rounded = rint(); if (rounded.greaterThan(newInstance(2147483647))) { return 2147483647; } if (rounded.lessThan(newInstance(-2147483648))) { return -2147483648; } for (int i = mant.length - 1; i >= mant.length - rounded.exp; i--) { result = result * RADIX + rounded.mant[i]; } if (rounded.sign == -1) { result = -result; } return result; } /** Get the exponent of the greatest power of 10000 that is * less than or equal to the absolute value of this. I.E. if * this is 106 then log10K would return 1. * @return integer base 10000 logarithm */ public int log10K() { return exp - 1; } /** Get the specified power of 10000. * @param e desired power * @return 10000e */ public Dfp power10K(final int e) { Dfp d = newInstance(getOne()); d.exp = e + 1; return d; } /** Get the exponent of the greatest power of 10 that is less than or equal to abs(this). * @return integer base 10 logarithm * @since 3.2 */ public int intLog10() { if (mant[mant.length-1] > 1000) { return exp * 4 - 1; } if (mant[mant.length-1] > 100) { return exp * 4 - 2; } if (mant[mant.length-1] > 10) { return exp * 4 - 3; } return exp * 4 - 4; } /** Return the specified power of 10. * @param e desired power * @return 10e */ public Dfp power10(final int e) { Dfp d = newInstance(getOne()); if (e >= 0) { d.exp = e / 4 + 1; } else { d.exp = (e + 1) / 4; } switch ((e % 4 + 4) % 4) { case 0: break; case 1: d = d.multiply(10); break; case 2: d = d.multiply(100); break; default: d = d.multiply(1000); } return d; } /** Negate the mantissa of this by computing the complement. * Leaves the sign bit unchanged, used internally by add. * Denormalized numbers are handled properly here. * @param extra ??? * @return ??? */ protected int complement(int extra) { extra = RADIX-extra; for (int i = 0; i < mant.length; i++) { mant[i] = RADIX-mant[i]-1; } int rh = extra / RADIX; extra = extra - rh * RADIX; for (int i = 0; i < mant.length; i++) { final int r = mant[i] + rh; rh = r / RADIX; mant[i] = r - rh * RADIX; } return extra; } /** Add x to this. * @param x number to add * @return sum of this and x */ public Dfp add(final Dfp x) { // make sure we don't mix number with different precision if (field.getRadixDigits() != x.field.getRadixDigits()) { field.setIEEEFlagsBits(DfpField.FLAG_INVALID); final Dfp result = newInstance(getZero()); result.nans = QNAN; return dotrap(DfpField.FLAG_INVALID, ADD_TRAP, x, result); } /* handle special cases */ if (nans != FINITE || x.nans != FINITE) { if (isNaN()) { return this; } if (x.isNaN()) { return x; } if (nans == INFINITE && x.nans == FINITE) { return this; } if (x.nans == INFINITE && nans == FINITE) { return x; } if (x.nans == INFINITE && nans == INFINITE && sign == x.sign) { return x; } if (x.nans == INFINITE && nans == INFINITE && sign != x.sign) { field.setIEEEFlagsBits(DfpField.FLAG_INVALID); Dfp result = newInstance(getZero()); result.nans = QNAN; result = dotrap(DfpField.FLAG_INVALID, ADD_TRAP, x, result); return result; } } /* copy this and the arg */ Dfp a = newInstance(this); Dfp b = newInstance(x); /* initialize the result object */ Dfp result = newInstance(getZero()); /* Make all numbers positive, but remember their sign */ final byte asign = a.sign; final byte bsign = b.sign; a.sign = 1; b.sign = 1; /* The result will be signed like the arg with greatest magnitude */ byte rsign = bsign; if (compare(a, b) > 0) { rsign = asign; } /* Handle special case when a or b is zero, by setting the exponent of the zero number equal to the other one. This avoids an alignment which would cause catastropic loss of precision */ if (b.mant[mant.length-1] == 0) { b.exp = a.exp; } if (a.mant[mant.length-1] == 0) { a.exp = b.exp; } /* align number with the smaller exponent */ int aextradigit = 0; int bextradigit = 0; if (a.exp < b.exp) { aextradigit = a.align(b.exp); } else { bextradigit = b.align(a.exp); } /* complement the smaller of the two if the signs are different */ if (asign != bsign) { if (asign == rsign) { bextradigit = b.complement(bextradigit); } else { aextradigit = a.complement(aextradigit); } } /* add the mantissas */ int rh = 0; /* acts as a carry */ for (int i = 0; i < mant.length; i++) { final int r = a.mant[i]+b.mant[i]+rh; rh = r / RADIX; result.mant[i] = r - rh * RADIX; } result.exp = a.exp; result.sign = rsign; /* handle overflow -- note, when asign!=bsign an overflow is * normal and should be ignored. */ if (rh != 0 && (asign == bsign)) { final int lostdigit = result.mant[0]; result.shiftRight(); result.mant[mant.length-1] = rh; final int excp = result.round(lostdigit); if (excp != 0) { result = dotrap(excp, ADD_TRAP, x, result); } } /* normalize the result */ for (int i = 0; i < mant.length; i++) { if (result.mant[mant.length-1] != 0) { break; } result.shiftLeft(); if (i == 0) { result.mant[0] = aextradigit+bextradigit; aextradigit = 0; bextradigit = 0; } } /* result is zero if after normalization the most sig. digit is zero */ if (result.mant[mant.length-1] == 0) { result.exp = 0; if (asign != bsign) { // Unless adding 2 negative zeros, sign is positive result.sign = 1; // Per IEEE 854-1987 Section 6.3 } } /* Call round to test for over/under flows */ final int excp = result.round(aextradigit + bextradigit); if (excp != 0) { result = dotrap(excp, ADD_TRAP, x, result); } return result; } /** Returns a number that is this number with the sign bit reversed. * @return the opposite of this */ public Dfp negate() { Dfp result = newInstance(this); result.sign = (byte) - result.sign; return result; } /** Subtract x from this. * @param x number to subtract * @return difference of this and a */ public Dfp subtract(final Dfp x) { return add(x.negate()); } /** Round this given the next digit n using the current rounding mode. * @param n ??? * @return the IEEE flag if an exception occurred */ protected int round(int n) { boolean inc = false; switch (field.getRoundingMode()) { case ROUND_DOWN: inc = false; break; case ROUND_UP: inc = n != 0; // round up if n!=0 break; case ROUND_HALF_UP: inc = n >= 5000; // round half up break; case ROUND_HALF_DOWN: inc = n > 5000; // round half down break; case ROUND_HALF_EVEN: inc = n > 5000 || (n == 5000 && (mant[0] & 1) == 1); // round half-even break; case ROUND_HALF_ODD: inc = n > 5000 || (n == 5000 && (mant[0] & 1) == 0); // round half-odd break; case ROUND_CEIL: inc = sign == 1 && n != 0; // round ceil break; case ROUND_FLOOR: default: inc = sign == -1 && n != 0; // round floor break; } if (inc) { // increment if necessary int rh = 1; for (int i = 0; i < mant.length; i++) { final int r = mant[i] + rh; rh = r / RADIX; mant[i] = r - rh * RADIX; } if (rh != 0) { shiftRight(); mant[mant.length-1] = rh; } } // check for exceptional cases and raise signals if necessary if (exp < MIN_EXP) { // Gradual Underflow field.setIEEEFlagsBits(DfpField.FLAG_UNDERFLOW); return DfpField.FLAG_UNDERFLOW; } if (exp > MAX_EXP) { // Overflow field.setIEEEFlagsBits(DfpField.FLAG_OVERFLOW); return DfpField.FLAG_OVERFLOW; } if (n != 0) { // Inexact field.setIEEEFlagsBits(DfpField.FLAG_INEXACT); return DfpField.FLAG_INEXACT; } return 0; } /** Multiply this by x. * @param x multiplicand * @return product of this and x */ public Dfp multiply(final Dfp x) { // make sure we don't mix number with different precision if (field.getRadixDigits() != x.field.getRadixDigits()) { field.setIEEEFlagsBits(DfpField.FLAG_INVALID); final Dfp result = newInstance(getZero()); result.nans = QNAN; return dotrap(DfpField.FLAG_INVALID, MULTIPLY_TRAP, x, result); } Dfp result = newInstance(getZero()); /* handle special cases */ if (nans != FINITE || x.nans != FINITE) { if (isNaN()) { return this; } if (x.isNaN()) { return x; } if (nans == INFINITE && x.nans == FINITE && x.mant[mant.length-1] != 0) { result = newInstance(this); result.sign = (byte) (sign * x.sign); return result; } if (x.nans == INFINITE && nans == FINITE && mant[mant.length-1] != 0) { result = newInstance(x); result.sign = (byte) (sign * x.sign); return result; } if (x.nans == INFINITE && nans == INFINITE) { result = newInstance(this); result.sign = (byte) (sign * x.sign); return result; } if ( (x.nans == INFINITE && nans == FINITE && mant[mant.length-1] == 0) || (nans == INFINITE && x.nans == FINITE && x.mant[mant.length-1] == 0) ) { field.setIEEEFlagsBits(DfpField.FLAG_INVALID); result = newInstance(getZero()); result.nans = QNAN; result = dotrap(DfpField.FLAG_INVALID, MULTIPLY_TRAP, x, result); return result; } } int[] product = new int[mant.length*2]; // Big enough to hold even the largest result for (int i = 0; i < mant.length; i++) { int rh = 0; // acts as a carry for (int j=0; j= 0; i--) { if (product[i] != 0) { md = i; break; } } // Copy the digits into the result for (int i = 0; i < mant.length; i++) { result.mant[mant.length - i - 1] = product[md - i]; } // Fixup the exponent. result.exp = exp + x.exp + md - 2 * mant.length + 1; result.sign = (byte)((sign == x.sign)?1:-1); if (result.mant[mant.length-1] == 0) { // if result is zero, set exp to zero result.exp = 0; } final int excp; if (md > (mant.length-1)) { excp = result.round(product[md-mant.length]); } else { excp = result.round(0); // has no effect except to check status } if (excp != 0) { result = dotrap(excp, MULTIPLY_TRAP, x, result); } return result; } /** Multiply this by a single digit x. * @param x multiplicand * @return product of this and x */ public Dfp multiply(final int x) { if (x >= 0 && x < RADIX) { return multiplyFast(x); } else { return multiply(newInstance(x)); } } /** Multiply this by a single digit 0<=x<radix. * There are speed advantages in this special case. * @param x multiplicand * @return product of this and x */ private Dfp multiplyFast(final int x) { Dfp result = newInstance(this); /* handle special cases */ if (nans != FINITE) { if (isNaN()) { return this; } if (nans == INFINITE && x != 0) { result = newInstance(this); return result; } if (nans == INFINITE && x == 0) { field.setIEEEFlagsBits(DfpField.FLAG_INVALID); result = newInstance(getZero()); result.nans = QNAN; result = dotrap(DfpField.FLAG_INVALID, MULTIPLY_TRAP, newInstance(getZero()), result); return result; } } /* range check x */ if (x < 0 || x >= RADIX) { field.setIEEEFlagsBits(DfpField.FLAG_INVALID); result = newInstance(getZero()); result.nans = QNAN; result = dotrap(DfpField.FLAG_INVALID, MULTIPLY_TRAP, result, result); return result; } int rh = 0; for (int i = 0; i < mant.length; i++) { final int r = mant[i] * x + rh; rh = r / RADIX; result.mant[i] = r - rh * RADIX; } int lostdigit = 0; if (rh != 0) { lostdigit = result.mant[0]; result.shiftRight(); result.mant[mant.length-1] = rh; } if (result.mant[mant.length-1] == 0) { // if result is zero, set exp to zero result.exp = 0; } final int excp = result.round(lostdigit); if (excp != 0) { result = dotrap(excp, MULTIPLY_TRAP, result, result); } return result; } /** Divide this by divisor. * @param divisor divisor * @return quotient of this by divisor */ public Dfp divide(Dfp divisor) { int dividend[]; // current status of the dividend int quotient[]; // quotient int remainder[];// remainder int qd; // current quotient digit we're working with int nsqd; // number of significant quotient digits we have int trial=0; // trial quotient digit int minadj; // minimum adjustment boolean trialgood; // Flag to indicate a good trail digit int md=0; // most sig digit in result int excp; // exceptions // make sure we don't mix number with different precision if (field.getRadixDigits() != divisor.field.getRadixDigits()) { field.setIEEEFlagsBits(DfpField.FLAG_INVALID); final Dfp result = newInstance(getZero()); result.nans = QNAN; return dotrap(DfpField.FLAG_INVALID, DIVIDE_TRAP, divisor, result); } Dfp result = newInstance(getZero()); /* handle special cases */ if (nans != FINITE || divisor.nans != FINITE) { if (isNaN()) { return this; } if (divisor.isNaN()) { return divisor; } if (nans == INFINITE && divisor.nans == FINITE) { result = newInstance(this); result.sign = (byte) (sign * divisor.sign); return result; } if (divisor.nans == INFINITE && nans == FINITE) { result = newInstance(getZero()); result.sign = (byte) (sign * divisor.sign); return result; } if (divisor.nans == INFINITE && nans == INFINITE) { field.setIEEEFlagsBits(DfpField.FLAG_INVALID); result = newInstance(getZero()); result.nans = QNAN; result = dotrap(DfpField.FLAG_INVALID, DIVIDE_TRAP, divisor, result); return result; } } /* Test for divide by zero */ if (divisor.mant[mant.length-1] == 0) { field.setIEEEFlagsBits(DfpField.FLAG_DIV_ZERO); result = newInstance(getZero()); result.sign = (byte) (sign * divisor.sign); result.nans = INFINITE; result = dotrap(DfpField.FLAG_DIV_ZERO, DIVIDE_TRAP, divisor, result); return result; } dividend = new int[mant.length+1]; // one extra digit needed quotient = new int[mant.length+2]; // two extra digits needed 1 for overflow, 1 for rounding remainder = new int[mant.length+1]; // one extra digit needed /* Initialize our most significant digits to zero */ dividend[mant.length] = 0; quotient[mant.length] = 0; quotient[mant.length+1] = 0; remainder[mant.length] = 0; /* copy our mantissa into the dividend, initialize the quotient while we are at it */ for (int i = 0; i < mant.length; i++) { dividend[i] = mant[i]; quotient[i] = 0; remainder[i] = 0; } /* outer loop. Once per quotient digit */ nsqd = 0; for (qd = mant.length+1; qd >= 0; qd--) { /* Determine outer limits of our quotient digit */ // r = most sig 2 digits of dividend final int divMsb = dividend[mant.length]*RADIX+dividend[mant.length-1]; int min = divMsb / (divisor.mant[mant.length-1]+1); int max = (divMsb + 1) / divisor.mant[mant.length-1]; trialgood = false; while (!trialgood) { // try the mean trial = (min+max)/2; /* Multiply by divisor and store as remainder */ int rh = 0; for (int i = 0; i < mant.length + 1; i++) { int dm = (i= 2) { min = trial+minadj; // update the minimum continue; } /* May have a good one here, check more thoroughly. Basically its a good one if it is less than the divisor */ trialgood = false; // assume false for (int i = mant.length - 1; i >= 0; i--) { if (divisor.mant[i] > remainder[i]) { trialgood = true; } if (divisor.mant[i] < remainder[i]) { break; } } if (remainder[mant.length] != 0) { trialgood = false; } if (trialgood == false) { min = trial+1; } } /* Great we have a digit! */ quotient[qd] = trial; if (trial != 0 || nsqd != 0) { nsqd++; } if (field.getRoundingMode() == DfpField.RoundingMode.ROUND_DOWN && nsqd == mant.length) { // We have enough for this mode break; } if (nsqd > mant.length) { // We have enough digits break; } /* move the remainder into the dividend while left shifting */ dividend[0] = 0; for (int i = 0; i < mant.length; i++) { dividend[i + 1] = remainder[i]; } } /* Find the most sig digit */ md = mant.length; // default for (int i = mant.length + 1; i >= 0; i--) { if (quotient[i] != 0) { md = i; break; } } /* Copy the digits into the result */ for (int i=0; i (mant.length-1)) { excp = result.round(quotient[md-mant.length]); } else { excp = result.round(0); } if (excp != 0) { result = dotrap(excp, DIVIDE_TRAP, divisor, result); } return result; } /** Divide by a single digit less than radix. * Special case, so there are speed advantages. 0 <= divisor < radix * @param divisor divisor * @return quotient of this by divisor */ public Dfp divide(int divisor) { // Handle special cases if (nans != FINITE) { if (isNaN()) { return this; } if (nans == INFINITE) { return newInstance(this); } } // Test for divide by zero if (divisor == 0) { field.setIEEEFlagsBits(DfpField.FLAG_DIV_ZERO); Dfp result = newInstance(getZero()); result.sign = sign; result.nans = INFINITE; result = dotrap(DfpField.FLAG_DIV_ZERO, DIVIDE_TRAP, getZero(), result); return result; } // range check divisor if (divisor < 0 || divisor >= RADIX) { field.setIEEEFlagsBits(DfpField.FLAG_INVALID); Dfp result = newInstance(getZero()); result.nans = QNAN; result = dotrap(DfpField.FLAG_INVALID, DIVIDE_TRAP, result, result); return result; } Dfp result = newInstance(this); int rl = 0; for (int i = mant.length-1; i >= 0; i--) { final int r = rl*RADIX + result.mant[i]; final int rh = r / divisor; rl = r - rh * divisor; result.mant[i] = rh; } if (result.mant[mant.length-1] == 0) { // normalize result.shiftLeft(); final int r = rl * RADIX; // compute the next digit and put it in final int rh = r / divisor; rl = r - rh * divisor; result.mant[0] = rh; } final int excp = result.round(rl * RADIX / divisor); // do the rounding if (excp != 0) { result = dotrap(excp, DIVIDE_TRAP, result, result); } return result; } /** {@inheritDoc} */ public Dfp reciprocal() { return field.getOne().divide(this); } /** Compute the square root. * @return square root of the instance * @since 3.2 */ public Dfp sqrt() { // check for unusual cases if (nans == FINITE && mant[mant.length-1] == 0) { // if zero return newInstance(this); } if (nans != FINITE) { if (nans == INFINITE && sign == 1) { // if positive infinity return newInstance(this); } if (nans == QNAN) { return newInstance(this); } if (nans == SNAN) { Dfp result; field.setIEEEFlagsBits(DfpField.FLAG_INVALID); result = newInstance(this); result = dotrap(DfpField.FLAG_INVALID, SQRT_TRAP, null, result); return result; } } if (sign == -1) { // if negative Dfp result; field.setIEEEFlagsBits(DfpField.FLAG_INVALID); result = newInstance(this); result.nans = QNAN; result = dotrap(DfpField.FLAG_INVALID, SQRT_TRAP, null, result); return result; } Dfp x = newInstance(this); /* Lets make a reasonable guess as to the size of the square root */ if (x.exp < -1 || x.exp > 1) { x.exp = this.exp / 2; } /* Coarsely estimate the mantissa */ switch (x.mant[mant.length-1] / 2000) { case 0: x.mant[mant.length-1] = x.mant[mant.length-1]/2+1; break; case 2: x.mant[mant.length-1] = 1500; break; case 3: x.mant[mant.length-1] = 2200; break; default: x.mant[mant.length-1] = 3000; } Dfp dx = newInstance(x); /* Now that we have the first pass estimate, compute the rest by the formula dx = (y - x*x) / (2x); */ Dfp px = getZero(); Dfp ppx = getZero(); while (x.unequal(px)) { dx = newInstance(x); dx.sign = -1; dx = dx.add(this.divide(x)); dx = dx.divide(2); ppx = px; px = x; x = x.add(dx); if (x.equals(ppx)) { // alternating between two values break; } // if dx is zero, break. Note testing the most sig digit // is a sufficient test since dx is normalized if (dx.mant[mant.length-1] == 0) { break; } } return x; } /** Get a string representation of the instance. * @return string representation of the instance */ @Override public String toString() { if (nans != FINITE) { // if non-finite exceptional cases if (nans == INFINITE) { return (sign < 0) ? NEG_INFINITY_STRING : POS_INFINITY_STRING; } else { return NAN_STRING; } } if (exp > mant.length || exp < -1) { return dfp2sci(); } return dfp2string(); } /** Convert an instance to a string using scientific notation. * @return string representation of the instance in scientific notation */ protected String dfp2sci() { char rawdigits[] = new char[mant.length * 4]; char outputbuffer[] = new char[mant.length * 4 + 20]; int p; int q; int e; int ae; int shf; // Get all the digits p = 0; for (int i = mant.length - 1; i >= 0; i--) { rawdigits[p++] = (char) ((mant[i] / 1000) + '0'); rawdigits[p++] = (char) (((mant[i] / 100) %10) + '0'); rawdigits[p++] = (char) (((mant[i] / 10) % 10) + '0'); rawdigits[p++] = (char) (((mant[i]) % 10) + '0'); } // Find the first non-zero one for (p = 0; p < rawdigits.length; p++) { if (rawdigits[p] != '0') { break; } } shf = p; // Now do the conversion q = 0; if (sign == -1) { outputbuffer[q++] = '-'; } if (p != rawdigits.length) { // there are non zero digits... outputbuffer[q++] = rawdigits[p++]; outputbuffer[q++] = '.'; while (p ae; p /= 10) { // nothing to do } if (e < 0) { outputbuffer[q++] = '-'; } while (p > 0) { outputbuffer[q++] = (char)(ae / p + '0'); ae = ae % p; p = p / 10; } return new String(outputbuffer, 0, q); } /** Convert an instance to a string using normal notation. * @return string representation of the instance in normal notation */ protected String dfp2string() { char buffer[] = new char[mant.length*4 + 20]; int p = 1; int q; int e = exp; boolean pointInserted = false; buffer[0] = ' '; if (e <= 0) { buffer[p++] = '0'; buffer[p++] = '.'; pointInserted = true; } while (e < 0) { buffer[p++] = '0'; buffer[p++] = '0'; buffer[p++] = '0'; buffer[p++] = '0'; e++; } for (int i = mant.length - 1; i >= 0; i--) { buffer[p++] = (char) ((mant[i] / 1000) + '0'); buffer[p++] = (char) (((mant[i] / 100) % 10) + '0'); buffer[p++] = (char) (((mant[i] / 10) % 10) + '0'); buffer[p++] = (char) (((mant[i]) % 10) + '0'); if (--e == 0) { buffer[p++] = '.'; pointInserted = true; } } while (e > 0) { buffer[p++] = '0'; buffer[p++] = '0'; buffer[p++] = '0'; buffer[p++] = '0'; e--; } if (!pointInserted) { // Ensure we have a radix point! buffer[p++] = '.'; } // Suppress leading zeros q = 1; while (buffer[q] == '0') { q++; } if (buffer[q] == '.') { q--; } // Suppress trailing zeros while (buffer[p-1] == '0') { p--; } // Insert sign if (sign < 0) { buffer[--q] = '-'; } return new String(buffer, q, p - q); } /** Raises a trap. This does not set the corresponding flag however. * @param type the trap type * @param what - name of routine trap occurred in * @param oper - input operator to function * @param result - the result computed prior to the trap * @return The suggested return value from the trap handler */ public Dfp dotrap(int type, String what, Dfp oper, Dfp result) { Dfp def = result; switch (type) { case DfpField.FLAG_INVALID: def = newInstance(getZero()); def.sign = result.sign; def.nans = QNAN; break; case DfpField.FLAG_DIV_ZERO: if (nans == FINITE && mant[mant.length-1] != 0) { // normal case, we are finite, non-zero def = newInstance(getZero()); def.sign = (byte)(sign*oper.sign); def.nans = INFINITE; } if (nans == FINITE && mant[mant.length-1] == 0) { // 0/0 def = newInstance(getZero()); def.nans = QNAN; } if (nans == INFINITE || nans == QNAN) { def = newInstance(getZero()); def.nans = QNAN; } if (nans == INFINITE || nans == SNAN) { def = newInstance(getZero()); def.nans = QNAN; } break; case DfpField.FLAG_UNDERFLOW: if ( (result.exp+mant.length) < MIN_EXP) { def = newInstance(getZero()); def.sign = result.sign; } else { def = newInstance(result); // gradual underflow } result.exp = result.exp + ERR_SCALE; break; case DfpField.FLAG_OVERFLOW: result.exp = result.exp - ERR_SCALE; def = newInstance(getZero()); def.sign = result.sign; def.nans = INFINITE; break; default: def = result; break; } return trap(type, what, oper, def, result); } /** Trap handler. Subclasses may override this to provide trap * functionality per IEEE 854-1987. * * @param type The exception type - e.g. FLAG_OVERFLOW * @param what The name of the routine we were in e.g. divide() * @param oper An operand to this function if any * @param def The default return value if trap not enabled * @param result The result that is specified to be delivered per * IEEE 854, if any * @return the value that should be return by the operation triggering the trap */ protected Dfp trap(int type, String what, Dfp oper, Dfp def, Dfp result) { return def; } /** Returns the type - one of FINITE, INFINITE, SNAN, QNAN. * @return type of the number */ public int classify() { return nans; } /** Creates an instance that is the same as x except that it has the sign of y. * abs(x) = dfp.copysign(x, dfp.one) * @param x number to get the value from * @param y number to get the sign from * @return a number with the value of x and the sign of y */ public static Dfp copysign(final Dfp x, final Dfp y) { Dfp result = x.newInstance(x); result.sign = y.sign; return result; } /** Returns the next number greater than this one in the direction of x. * If this==x then simply returns this. * @param x direction where to look at * @return closest number next to instance in the direction of x */ public Dfp nextAfter(final Dfp x) { // make sure we don't mix number with different precision if (field.getRadixDigits() != x.field.getRadixDigits()) { field.setIEEEFlagsBits(DfpField.FLAG_INVALID); final Dfp result = newInstance(getZero()); result.nans = QNAN; return dotrap(DfpField.FLAG_INVALID, NEXT_AFTER_TRAP, x, result); } // if this is greater than x boolean up = false; if (this.lessThan(x)) { up = true; } if (compare(this, x) == 0) { return newInstance(x); } if (lessThan(getZero())) { up = !up; } final Dfp inc; Dfp result; if (up) { inc = newInstance(getOne()); inc.exp = this.exp-mant.length+1; inc.sign = this.sign; if (this.equals(getZero())) { inc.exp = MIN_EXP-mant.length; } result = add(inc); } else { inc = newInstance(getOne()); inc.exp = this.exp; inc.sign = this.sign; if (this.equals(inc)) { inc.exp = this.exp-mant.length; } else { inc.exp = this.exp-mant.length+1; } if (this.equals(getZero())) { inc.exp = MIN_EXP-mant.length; } result = this.subtract(inc); } if (result.classify() == INFINITE && this.classify() != INFINITE) { field.setIEEEFlagsBits(DfpField.FLAG_INEXACT); result = dotrap(DfpField.FLAG_INEXACT, NEXT_AFTER_TRAP, x, result); } if (result.equals(getZero()) && this.equals(getZero()) == false) { field.setIEEEFlagsBits(DfpField.FLAG_INEXACT); result = dotrap(DfpField.FLAG_INEXACT, NEXT_AFTER_TRAP, x, result); } return result; } /** Convert the instance into a double. * @return a double approximating the instance * @see #toSplitDouble() */ public double toDouble() { if (isInfinite()) { if (lessThan(getZero())) { return Double.NEGATIVE_INFINITY; } else { return Double.POSITIVE_INFINITY; } } if (isNaN()) { return Double.NaN; } Dfp y = this; boolean negate = false; int cmp0 = compare(this, getZero()); if (cmp0 == 0) { return sign < 0 ? -0.0 : +0.0; } else if (cmp0 < 0) { y = negate(); negate = true; } /* Find the exponent, first estimate by integer log10, then adjust. Should be faster than doing a natural logarithm. */ int exponent = (int)(y.intLog10() * 3.32); if (exponent < 0) { exponent--; } Dfp tempDfp = DfpMath.pow(getTwo(), exponent); while (tempDfp.lessThan(y) || tempDfp.equals(y)) { tempDfp = tempDfp.multiply(2); exponent++; } exponent--; /* We have the exponent, now work on the mantissa */ y = y.divide(DfpMath.pow(getTwo(), exponent)); if (exponent > -1023) { y = y.subtract(getOne()); } if (exponent < -1074) { return 0; } if (exponent > 1023) { return negate ? Double.NEGATIVE_INFINITY : Double.POSITIVE_INFINITY; } y = y.multiply(newInstance(4503599627370496l)).rint(); String str = y.toString(); str = str.substring(0, str.length()-1); long mantissa = Long.parseLong(str); if (mantissa == 4503599627370496L) { // Handle special case where we round up to next power of two mantissa = 0; exponent++; } /* Its going to be subnormal, so make adjustments */ if (exponent <= -1023) { exponent--; } while (exponent < -1023) { exponent++; mantissa >>>= 1; } long bits = mantissa | ((exponent + 1023L) << 52); double x = Double.longBitsToDouble(bits); if (negate) { x = -x; } return x; } /** Convert the instance into a split double. * @return an array of two doubles which sum represent the instance * @see #toDouble() */ public double[] toSplitDouble() { double split[] = new double[2]; long mask = 0xffffffffc0000000L; split[0] = Double.longBitsToDouble(Double.doubleToLongBits(toDouble()) & mask); split[1] = subtract(newInstance(split[0])).toDouble(); return split; } /** {@inheritDoc} * @since 3.2 */ public double getReal() { return toDouble(); } /** {@inheritDoc} * @since 3.2 */ public Dfp add(final double a) { return add(newInstance(a)); } /** {@inheritDoc} * @since 3.2 */ public Dfp subtract(final double a) { return subtract(newInstance(a)); } /** {@inheritDoc} * @since 3.2 */ public Dfp multiply(final double a) { return multiply(newInstance(a)); } /** {@inheritDoc} * @since 3.2 */ public Dfp divide(final double a) { return divide(newInstance(a)); } /** {@inheritDoc} * @since 3.2 */ public Dfp remainder(final double a) { return remainder(newInstance(a)); } /** {@inheritDoc} * @since 3.2 */ public long round() { return FastMath.round(toDouble()); } /** {@inheritDoc} * @since 3.2 */ public Dfp signum() { if (isNaN() || isZero()) { return this; } else { return newInstance(sign > 0 ? +1 : -1); } } /** {@inheritDoc} * @since 3.2 */ public Dfp copySign(final Dfp s) { if ((sign >= 0 && s.sign >= 0) || (sign < 0 && s.sign < 0)) { // Sign is currently OK return this; } return negate(); // flip sign } /** {@inheritDoc} * @since 3.2 */ public Dfp copySign(final double s) { long sb = Double.doubleToLongBits(s); if ((sign >= 0 && sb >= 0) || (sign < 0 && sb < 0)) { // Sign is currently OK return this; } return negate(); // flip sign } /** {@inheritDoc} * @since 3.2 */ public Dfp scalb(final int n) { return multiply(DfpMath.pow(getTwo(), n)); } /** {@inheritDoc} * @since 3.2 */ public Dfp hypot(final Dfp y) { return multiply(this).add(y.multiply(y)).sqrt(); } /** {@inheritDoc} * @since 3.2 */ public Dfp cbrt() { return rootN(3); } /** {@inheritDoc} * @since 3.2 */ public Dfp rootN(final int n) { return (sign >= 0) ? DfpMath.pow(this, getOne().divide(n)) : DfpMath.pow(negate(), getOne().divide(n)).negate(); } /** {@inheritDoc} * @since 3.2 */ public Dfp pow(final double p) { return DfpMath.pow(this, newInstance(p)); } /** {@inheritDoc} * @since 3.2 */ public Dfp pow(final int n) { return DfpMath.pow(this, n); } /** {@inheritDoc} * @since 3.2 */ public Dfp pow(final Dfp e) { return DfpMath.pow(this, e); } /** {@inheritDoc} * @since 3.2 */ public Dfp exp() { return DfpMath.exp(this); } /** {@inheritDoc} * @since 3.2 */ public Dfp expm1() { return DfpMath.exp(this).subtract(getOne()); } /** {@inheritDoc} * @since 3.2 */ public Dfp log() { return DfpMath.log(this); } /** {@inheritDoc} * @since 3.2 */ public Dfp log1p() { return DfpMath.log(this.add(getOne())); } // TODO: deactivate this implementation (and return type) in 4.0 /** Get the exponent of the greatest power of 10 that is less than or equal to abs(this). * @return integer base 10 logarithm * @deprecated as of 3.2, replaced by {@link #intLog10()}, in 4.0 the return type * will be changed to Dfp */ @Deprecated public int log10() { return intLog10(); } // TODO: activate this implementation (and return type) in 4.0 // /** {@inheritDoc} // * @since 3.2 // */ // public Dfp log10() { // return DfpMath.log(this).divide(DfpMath.log(newInstance(10))); // } /** {@inheritDoc} * @since 3.2 */ public Dfp cos() { return DfpMath.cos(this); } /** {@inheritDoc} * @since 3.2 */ public Dfp sin() { return DfpMath.sin(this); } /** {@inheritDoc} * @since 3.2 */ public Dfp tan() { return DfpMath.tan(this); } /** {@inheritDoc} * @since 3.2 */ public Dfp acos() { return DfpMath.acos(this); } /** {@inheritDoc} * @since 3.2 */ public Dfp asin() { return DfpMath.asin(this); } /** {@inheritDoc} * @since 3.2 */ public Dfp atan() { return DfpMath.atan(this); } /** {@inheritDoc} * @since 3.2 */ public Dfp atan2(final Dfp x) throws DimensionMismatchException { // compute r = sqrt(x^2+y^2) final Dfp r = x.multiply(x).add(multiply(this)).sqrt(); if (x.sign >= 0) { // compute atan2(y, x) = 2 atan(y / (r + x)) return getTwo().multiply(divide(r.add(x)).atan()); } else { // compute atan2(y, x) = +/- pi - 2 atan(y / (r - x)) final Dfp tmp = getTwo().multiply(divide(r.subtract(x)).atan()); final Dfp pmPi = newInstance((tmp.sign <= 0) ? -FastMath.PI : FastMath.PI); return pmPi.subtract(tmp); } } /** {@inheritDoc} * @since 3.2 */ public Dfp cosh() { return DfpMath.exp(this).add(DfpMath.exp(negate())).divide(2); } /** {@inheritDoc} * @since 3.2 */ public Dfp sinh() { return DfpMath.exp(this).subtract(DfpMath.exp(negate())).divide(2); } /** {@inheritDoc} * @since 3.2 */ public Dfp tanh() { final Dfp ePlus = DfpMath.exp(this); final Dfp eMinus = DfpMath.exp(negate()); return ePlus.subtract(eMinus).divide(ePlus.add(eMinus)); } /** {@inheritDoc} * @since 3.2 */ public Dfp acosh() { return multiply(this).subtract(getOne()).sqrt().add(this).log(); } /** {@inheritDoc} * @since 3.2 */ public Dfp asinh() { return multiply(this).add(getOne()).sqrt().add(this).log(); } /** {@inheritDoc} * @since 3.2 */ public Dfp atanh() { return getOne().add(this).divide(getOne().subtract(this)).log().divide(2); } /** {@inheritDoc} * @since 3.2 */ public Dfp linearCombination(final Dfp[] a, final Dfp[] b) throws DimensionMismatchException { if (a.length != b.length) { throw new DimensionMismatchException(a.length, b.length); } Dfp r = getZero(); for (int i = 0; i < a.length; ++i) { r = r.add(a[i].multiply(b[i])); } return r; } /** {@inheritDoc} * @since 3.2 */ public Dfp linearCombination(final double[] a, final Dfp[] b) throws DimensionMismatchException { if (a.length != b.length) { throw new DimensionMismatchException(a.length, b.length); } Dfp r = getZero(); for (int i = 0; i < a.length; ++i) { r = r.add(b[i].multiply(a[i])); } return r; } /** {@inheritDoc} * @since 3.2 */ public Dfp linearCombination(final Dfp a1, final Dfp b1, final Dfp a2, final Dfp b2) { return a1.multiply(b1).add(a2.multiply(b2)); } /** {@inheritDoc} * @since 3.2 */ public Dfp linearCombination(final double a1, final Dfp b1, final double a2, final Dfp b2) { return b1.multiply(a1).add(b2.multiply(a2)); } /** {@inheritDoc} * @since 3.2 */ public Dfp linearCombination(final Dfp a1, final Dfp b1, final Dfp a2, final Dfp b2, final Dfp a3, final Dfp b3) { return a1.multiply(b1).add(a2.multiply(b2)).add(a3.multiply(b3)); } /** {@inheritDoc} * @since 3.2 */ public Dfp linearCombination(final double a1, final Dfp b1, final double a2, final Dfp b2, final double a3, final Dfp b3) { return b1.multiply(a1).add(b2.multiply(a2)).add(b3.multiply(a3)); } /** {@inheritDoc} * @since 3.2 */ public Dfp linearCombination(final Dfp a1, final Dfp b1, final Dfp a2, final Dfp b2, final Dfp a3, final Dfp b3, final Dfp a4, final Dfp b4) { return a1.multiply(b1).add(a2.multiply(b2)).add(a3.multiply(b3)).add(a4.multiply(b4)); } /** {@inheritDoc} * @since 3.2 */ public Dfp linearCombination(final double a1, final Dfp b1, final double a2, final Dfp b2, final double a3, final Dfp b3, final double a4, final Dfp b4) { return b1.multiply(a1).add(b2.multiply(a2)).add(b3.multiply(a3)).add(b4.multiply(a4)); } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/dfp/UnivariateDfpFunction.java100644 1750 1750 3134 12126627716 30412 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.dfp; /** * An interface representing a univariate {@link Dfp} function. * * @version $Id: UnivariateDfpFunction.java 1416643 2012-12-03 19:37:14Z tn $ */ public interface UnivariateDfpFunction { /** * Compute the value of the function. * * @param x Point at which the function value should be computed. * @return the value. * @throws IllegalArgumentException when the activated method itself can * ascertain that preconditions, specified in the API expressed at the * level of the activated method, have been violated. In the vast * majority of cases where Commons-Math throws IllegalArgumentException, * it is the result of argument checking of actual parameters immediately * passed to a method. */ Dfp value(Dfp x); } ././@LongLink100644 0 0 147 12126630647 10264 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/dfp/BracketingNthOrderBrentSolverDFP.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/dfp/BracketingNthOrderBrentSolverDFP.ja100644 1750 1750 37702 12126627716 32103 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.dfp; import org.apache.commons.math3.analysis.solvers.AllowedSolution; import org.apache.commons.math3.exception.MathInternalError; import org.apache.commons.math3.exception.NoBracketingException; import org.apache.commons.math3.exception.NullArgumentException; import org.apache.commons.math3.exception.NumberIsTooSmallException; import org.apache.commons.math3.util.Incrementor; import org.apache.commons.math3.util.MathUtils; /** * This class implements a modification of the Brent algorithm. *

                              * The changes with respect to the original Brent algorithm are: *

                                *
                              • the returned value is chosen in the current interval according * to user specified {@link AllowedSolution},
                              • *
                              • the maximal order for the invert polynomial root search is * user-specified instead of being invert quadratic only
                              • *
                              *

                              * The given interval must bracket the root. * * @version $Id: BracketingNthOrderBrentSolverDFP.java 1416643 2012-12-03 19:37:14Z tn $ */ public class BracketingNthOrderBrentSolverDFP { /** Maximal aging triggering an attempt to balance the bracketing interval. */ private static final int MAXIMAL_AGING = 2; /** Maximal order. */ private final int maximalOrder; /** Function value accuracy. */ private final Dfp functionValueAccuracy; /** Absolute accuracy. */ private final Dfp absoluteAccuracy; /** Relative accuracy. */ private final Dfp relativeAccuracy; /** Evaluations counter. */ private final Incrementor evaluations = new Incrementor(); /** * Construct a solver. * * @param relativeAccuracy Relative accuracy. * @param absoluteAccuracy Absolute accuracy. * @param functionValueAccuracy Function value accuracy. * @param maximalOrder maximal order. * @exception NumberIsTooSmallException if maximal order is lower than 2 */ public BracketingNthOrderBrentSolverDFP(final Dfp relativeAccuracy, final Dfp absoluteAccuracy, final Dfp functionValueAccuracy, final int maximalOrder) throws NumberIsTooSmallException { if (maximalOrder < 2) { throw new NumberIsTooSmallException(maximalOrder, 2, true); } this.maximalOrder = maximalOrder; this.absoluteAccuracy = absoluteAccuracy; this.relativeAccuracy = relativeAccuracy; this.functionValueAccuracy = functionValueAccuracy; } /** Get the maximal order. * @return maximal order */ public int getMaximalOrder() { return maximalOrder; } /** * Get the maximal number of function evaluations. * * @return the maximal number of function evaluations. */ public int getMaxEvaluations() { return evaluations.getMaximalCount(); } /** * Get the number of evaluations of the objective function. * The number of evaluations corresponds to the last call to the * {@code optimize} method. It is 0 if the method has not been * called yet. * * @return the number of evaluations of the objective function. */ public int getEvaluations() { return evaluations.getCount(); } /** * Get the absolute accuracy. * @return absolute accuracy */ public Dfp getAbsoluteAccuracy() { return absoluteAccuracy; } /** * Get the relative accuracy. * @return relative accuracy */ public Dfp getRelativeAccuracy() { return relativeAccuracy; } /** * Get the function accuracy. * @return function accuracy */ public Dfp getFunctionValueAccuracy() { return functionValueAccuracy; } /** * Solve for a zero in the given interval. * A solver may require that the interval brackets a single zero root. * Solvers that do require bracketing should be able to handle the case * where one of the endpoints is itself a root. * * @param maxEval Maximum number of evaluations. * @param f Function to solve. * @param min Lower bound for the interval. * @param max Upper bound for the interval. * @param allowedSolution The kind of solutions that the root-finding algorithm may * accept as solutions. * @return a value where the function is zero. * @exception NullArgumentException if f is null. * @exception NoBracketingException if root cannot be bracketed */ public Dfp solve(final int maxEval, final UnivariateDfpFunction f, final Dfp min, final Dfp max, final AllowedSolution allowedSolution) throws NullArgumentException, NoBracketingException { return solve(maxEval, f, min, max, min.add(max).divide(2), allowedSolution); } /** * Solve for a zero in the given interval, start at {@code startValue}. * A solver may require that the interval brackets a single zero root. * Solvers that do require bracketing should be able to handle the case * where one of the endpoints is itself a root. * * @param maxEval Maximum number of evaluations. * @param f Function to solve. * @param min Lower bound for the interval. * @param max Upper bound for the interval. * @param startValue Start value to use. * @param allowedSolution The kind of solutions that the root-finding algorithm may * accept as solutions. * @return a value where the function is zero. * @exception NullArgumentException if f is null. * @exception NoBracketingException if root cannot be bracketed */ public Dfp solve(final int maxEval, final UnivariateDfpFunction f, final Dfp min, final Dfp max, final Dfp startValue, final AllowedSolution allowedSolution) throws NullArgumentException, NoBracketingException { // Checks. MathUtils.checkNotNull(f); // Reset. evaluations.setMaximalCount(maxEval); evaluations.resetCount(); Dfp zero = startValue.getZero(); Dfp nan = zero.newInstance((byte) 1, Dfp.QNAN); // prepare arrays with the first points final Dfp[] x = new Dfp[maximalOrder + 1]; final Dfp[] y = new Dfp[maximalOrder + 1]; x[0] = min; x[1] = startValue; x[2] = max; // evaluate initial guess evaluations.incrementCount(); y[1] = f.value(x[1]); if (y[1].isZero()) { // return the initial guess if it is a perfect root. return x[1]; } // evaluate first endpoint evaluations.incrementCount(); y[0] = f.value(x[0]); if (y[0].isZero()) { // return the first endpoint if it is a perfect root. return x[0]; } int nbPoints; int signChangeIndex; if (y[0].multiply(y[1]).negativeOrNull()) { // reduce interval if it brackets the root nbPoints = 2; signChangeIndex = 1; } else { // evaluate second endpoint evaluations.incrementCount(); y[2] = f.value(x[2]); if (y[2].isZero()) { // return the second endpoint if it is a perfect root. return x[2]; } if (y[1].multiply(y[2]).negativeOrNull()) { // use all computed point as a start sampling array for solving nbPoints = 3; signChangeIndex = 2; } else { throw new NoBracketingException(x[0].toDouble(), x[2].toDouble(), y[0].toDouble(), y[2].toDouble()); } } // prepare a work array for inverse polynomial interpolation final Dfp[] tmpX = new Dfp[x.length]; // current tightest bracketing of the root Dfp xA = x[signChangeIndex - 1]; Dfp yA = y[signChangeIndex - 1]; Dfp absXA = xA.abs(); Dfp absYA = yA.abs(); int agingA = 0; Dfp xB = x[signChangeIndex]; Dfp yB = y[signChangeIndex]; Dfp absXB = xB.abs(); Dfp absYB = yB.abs(); int agingB = 0; // search loop while (true) { // check convergence of bracketing interval Dfp maxX = absXA.lessThan(absXB) ? absXB : absXA; Dfp maxY = absYA.lessThan(absYB) ? absYB : absYA; final Dfp xTol = absoluteAccuracy.add(relativeAccuracy.multiply(maxX)); if (xB.subtract(xA).subtract(xTol).negativeOrNull() || maxY.lessThan(functionValueAccuracy)) { switch (allowedSolution) { case ANY_SIDE : return absYA.lessThan(absYB) ? xA : xB; case LEFT_SIDE : return xA; case RIGHT_SIDE : return xB; case BELOW_SIDE : return yA.lessThan(zero) ? xA : xB; case ABOVE_SIDE : return yA.lessThan(zero) ? xB : xA; default : // this should never happen throw new MathInternalError(null); } } // target for the next evaluation point Dfp targetY; if (agingA >= MAXIMAL_AGING) { // we keep updating the high bracket, try to compensate this targetY = yB.divide(16).negate(); } else if (agingB >= MAXIMAL_AGING) { // we keep updating the low bracket, try to compensate this targetY = yA.divide(16).negate(); } else { // bracketing is balanced, try to find the root itself targetY = zero; } // make a few attempts to guess a root, Dfp nextX; int start = 0; int end = nbPoints; do { // guess a value for current target, using inverse polynomial interpolation System.arraycopy(x, start, tmpX, start, end - start); nextX = guessX(targetY, tmpX, y, start, end); if (!(nextX.greaterThan(xA) && nextX.lessThan(xB))) { // the guessed root is not strictly inside of the tightest bracketing interval // the guessed root is either not strictly inside the interval or it // is a NaN (which occurs when some sampling points share the same y) // we try again with a lower interpolation order if (signChangeIndex - start >= end - signChangeIndex) { // we have more points before the sign change, drop the lowest point ++start; } else { // we have more points after sign change, drop the highest point --end; } // we need to do one more attempt nextX = nan; } } while (nextX.isNaN() && (end - start > 1)); if (nextX.isNaN()) { // fall back to bisection nextX = xA.add(xB.subtract(xA).divide(2)); start = signChangeIndex - 1; end = signChangeIndex; } // evaluate the function at the guessed root evaluations.incrementCount(); final Dfp nextY = f.value(nextX); if (nextY.isZero()) { // we have found an exact root, since it is not an approximation // we don't need to bother about the allowed solutions setting return nextX; } if ((nbPoints > 2) && (end - start != nbPoints)) { // we have been forced to ignore some points to keep bracketing, // they are probably too far from the root, drop them from now on nbPoints = end - start; System.arraycopy(x, start, x, 0, nbPoints); System.arraycopy(y, start, y, 0, nbPoints); signChangeIndex -= start; } else if (nbPoints == x.length) { // we have to drop one point in order to insert the new one nbPoints--; // keep the tightest bracketing interval as centered as possible if (signChangeIndex >= (x.length + 1) / 2) { // we drop the lowest point, we have to shift the arrays and the index System.arraycopy(x, 1, x, 0, nbPoints); System.arraycopy(y, 1, y, 0, nbPoints); --signChangeIndex; } } // insert the last computed point //(by construction, we know it lies inside the tightest bracketing interval) System.arraycopy(x, signChangeIndex, x, signChangeIndex + 1, nbPoints - signChangeIndex); x[signChangeIndex] = nextX; System.arraycopy(y, signChangeIndex, y, signChangeIndex + 1, nbPoints - signChangeIndex); y[signChangeIndex] = nextY; ++nbPoints; // update the bracketing interval if (nextY.multiply(yA).negativeOrNull()) { // the sign change occurs before the inserted point xB = nextX; yB = nextY; absYB = yB.abs(); ++agingA; agingB = 0; } else { // the sign change occurs after the inserted point xA = nextX; yA = nextY; absYA = yA.abs(); agingA = 0; ++agingB; // update the sign change index signChangeIndex++; } } } /** Guess an x value by nth order inverse polynomial interpolation. *

                              * The x value is guessed by evaluating polynomial Q(y) at y = targetY, where Q * is built such that for all considered points (xi, yi), * Q(yi) = xi. *

                              * @param targetY target value for y * @param x reference points abscissas for interpolation, * note that this array is modified during computation * @param y reference points ordinates for interpolation * @param start start index of the points to consider (inclusive) * @param end end index of the points to consider (exclusive) * @return guessed root (will be a NaN if two points share the same y) */ private Dfp guessX(final Dfp targetY, final Dfp[] x, final Dfp[] y, final int start, final int end) { // compute Q Newton coefficients by divided differences for (int i = start; i < end - 1; ++i) { final int delta = i + 1 - start; for (int j = end - 1; j > i; --j) { x[j] = x[j].subtract(x[j-1]).divide(y[j].subtract(y[j - delta])); } } // evaluate Q(targetY) Dfp x0 = targetY.getZero(); for (int j = end - 1; j >= start; --j) { x0 = x[j].add(x0.multiply(targetY.subtract(y[j]))); } return x0; } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/dfp/DfpDec.java100644 1750 1750 25546 12126627716 25323 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.dfp; /** Subclass of {@link Dfp} which hides the radix-10000 artifacts of the superclass. * This should give outward appearances of being a decimal number with DIGITS*4-3 * decimal digits. This class can be subclassed to appear to be an arbitrary number * of decimal digits less than DIGITS*4-3. * @version $Id: DfpDec.java 1449529 2013-02-24 19:13:17Z luc $ * @since 2.2 */ public class DfpDec extends Dfp { /** Makes an instance with a value of zero. * @param factory factory linked to this instance */ protected DfpDec(final DfpField factory) { super(factory); } /** Create an instance from a byte value. * @param factory factory linked to this instance * @param x value to convert to an instance */ protected DfpDec(final DfpField factory, byte x) { super(factory, x); } /** Create an instance from an int value. * @param factory factory linked to this instance * @param x value to convert to an instance */ protected DfpDec(final DfpField factory, int x) { super(factory, x); } /** Create an instance from a long value. * @param factory factory linked to this instance * @param x value to convert to an instance */ protected DfpDec(final DfpField factory, long x) { super(factory, x); } /** Create an instance from a double value. * @param factory factory linked to this instance * @param x value to convert to an instance */ protected DfpDec(final DfpField factory, double x) { super(factory, x); round(0); } /** Copy constructor. * @param d instance to copy */ public DfpDec(final Dfp d) { super(d); round(0); } /** Create an instance from a String representation. * @param factory factory linked to this instance * @param s string representation of the instance */ protected DfpDec(final DfpField factory, final String s) { super(factory, s); round(0); } /** Creates an instance with a non-finite value. * @param factory factory linked to this instance * @param sign sign of the Dfp to create * @param nans code of the value, must be one of {@link #INFINITE}, * {@link #SNAN}, {@link #QNAN} */ protected DfpDec(final DfpField factory, final byte sign, final byte nans) { super(factory, sign, nans); } /** {@inheritDoc} */ @Override public Dfp newInstance() { return new DfpDec(getField()); } /** {@inheritDoc} */ @Override public Dfp newInstance(final byte x) { return new DfpDec(getField(), x); } /** {@inheritDoc} */ @Override public Dfp newInstance(final int x) { return new DfpDec(getField(), x); } /** {@inheritDoc} */ @Override public Dfp newInstance(final long x) { return new DfpDec(getField(), x); } /** {@inheritDoc} */ @Override public Dfp newInstance(final double x) { return new DfpDec(getField(), x); } /** {@inheritDoc} */ @Override public Dfp newInstance(final Dfp d) { // make sure we don't mix number with different precision if (getField().getRadixDigits() != d.getField().getRadixDigits()) { getField().setIEEEFlagsBits(DfpField.FLAG_INVALID); final Dfp result = newInstance(getZero()); result.nans = QNAN; return dotrap(DfpField.FLAG_INVALID, "newInstance", d, result); } return new DfpDec(d); } /** {@inheritDoc} */ @Override public Dfp newInstance(final String s) { return new DfpDec(getField(), s); } /** {@inheritDoc} */ @Override public Dfp newInstance(final byte sign, final byte nans) { return new DfpDec(getField(), sign, nans); } /** Get the number of decimal digits this class is going to represent. * Default implementation returns {@link #getRadixDigits()}*4-3. Subclasses can * override this to return something less. * @return number of decimal digits this class is going to represent */ protected int getDecimalDigits() { return getRadixDigits() * 4 - 3; } /** {@inheritDoc} */ @Override protected int round(int in) { int msb = mant[mant.length-1]; if (msb == 0) { // special case -- this == zero return 0; } int cmaxdigits = mant.length * 4; int lsbthreshold = 1000; while (lsbthreshold > msb) { lsbthreshold /= 10; cmaxdigits --; } final int digits = getDecimalDigits(); final int lsbshift = cmaxdigits - digits; final int lsd = lsbshift / 4; lsbthreshold = 1; for (int i = 0; i < lsbshift % 4; i++) { lsbthreshold *= 10; } final int lsb = mant[lsd]; if (lsbthreshold <= 1 && digits == 4 * mant.length - 3) { return super.round(in); } int discarded = in; // not looking at this after this point final int n; if (lsbthreshold == 1) { // look to the next digit for rounding n = (mant[lsd-1] / 1000) % 10; mant[lsd-1] %= 1000; discarded |= mant[lsd-1]; } else { n = (lsb * 10 / lsbthreshold) % 10; discarded |= lsb % (lsbthreshold/10); } for (int i = 0; i < lsd; i++) { discarded |= mant[i]; // need to know if there are any discarded bits mant[i] = 0; } mant[lsd] = lsb / lsbthreshold * lsbthreshold; final boolean inc; switch (getField().getRoundingMode()) { case ROUND_DOWN: inc = false; break; case ROUND_UP: inc = (n != 0) || (discarded != 0); // round up if n!=0 break; case ROUND_HALF_UP: inc = n >= 5; // round half up break; case ROUND_HALF_DOWN: inc = n > 5; // round half down break; case ROUND_HALF_EVEN: inc = (n > 5) || (n == 5 && discarded != 0) || (n == 5 && discarded == 0 && ((lsb / lsbthreshold) & 1) == 1); // round half-even break; case ROUND_HALF_ODD: inc = (n > 5) || (n == 5 && discarded != 0) || (n == 5 && discarded == 0 && ((lsb / lsbthreshold) & 1) == 0); // round half-odd break; case ROUND_CEIL: inc = (sign == 1) && (n != 0 || discarded != 0); // round ceil break; case ROUND_FLOOR: default: inc = (sign == -1) && (n != 0 || discarded != 0); // round floor break; } if (inc) { // increment if necessary int rh = lsbthreshold; for (int i = lsd; i < mant.length; i++) { final int r = mant[i] + rh; rh = r / RADIX; mant[i] = r % RADIX; } if (rh != 0) { shiftRight(); mant[mant.length-1]=rh; } } // Check for exceptional cases and raise signals if necessary if (exp < MIN_EXP) { // Gradual Underflow getField().setIEEEFlagsBits(DfpField.FLAG_UNDERFLOW); return DfpField.FLAG_UNDERFLOW; } if (exp > MAX_EXP) { // Overflow getField().setIEEEFlagsBits(DfpField.FLAG_OVERFLOW); return DfpField.FLAG_OVERFLOW; } if (n != 0 || discarded != 0) { // Inexact getField().setIEEEFlagsBits(DfpField.FLAG_INEXACT); return DfpField.FLAG_INEXACT; } return 0; } /** {@inheritDoc} */ @Override public Dfp nextAfter(Dfp x) { final String trapName = "nextAfter"; // make sure we don't mix number with different precision if (getField().getRadixDigits() != x.getField().getRadixDigits()) { getField().setIEEEFlagsBits(DfpField.FLAG_INVALID); final Dfp result = newInstance(getZero()); result.nans = QNAN; return dotrap(DfpField.FLAG_INVALID, trapName, x, result); } boolean up = false; Dfp result; Dfp inc; // if this is greater than x if (this.lessThan(x)) { up = true; } if (equals(x)) { return newInstance(x); } if (lessThan(getZero())) { up = !up; } if (up) { inc = power10(intLog10() - getDecimalDigits() + 1); inc = copysign(inc, this); if (this.equals(getZero())) { inc = power10K(MIN_EXP-mant.length-1); } if (inc.equals(getZero())) { result = copysign(newInstance(getZero()), this); } else { result = add(inc); } } else { inc = power10(intLog10()); inc = copysign(inc, this); if (this.equals(inc)) { inc = inc.divide(power10(getDecimalDigits())); } else { inc = inc.divide(power10(getDecimalDigits() - 1)); } if (this.equals(getZero())) { inc = power10K(MIN_EXP-mant.length-1); } if (inc.equals(getZero())) { result = copysign(newInstance(getZero()), this); } else { result = subtract(inc); } } if (result.classify() == INFINITE && this.classify() != INFINITE) { getField().setIEEEFlagsBits(DfpField.FLAG_INEXACT); result = dotrap(DfpField.FLAG_INEXACT, trapName, x, result); } if (result.equals(getZero()) && this.equals(getZero()) == false) { getField().setIEEEFlagsBits(DfpField.FLAG_INEXACT); result = dotrap(DfpField.FLAG_INEXACT, trapName, x, result); } return result; } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/stat/package-info.java100644 1750 1750 1606 12126627717 26674 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ /** * Data storage, manipulation and summary routines. */ package org.apache.commons.math3.stat; ././@LongLink100644 0 0 156 12126630647 10264 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/stat/regression/GLSMultipleLinearRegression.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/stat/regression/GLSMultipleLinearRegres100644 1750 1750 10377 12126627717 32301 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.stat.regression; import org.apache.commons.math3.linear.LUDecomposition; import org.apache.commons.math3.linear.RealMatrix; import org.apache.commons.math3.linear.Array2DRowRealMatrix; import org.apache.commons.math3.linear.RealVector; /** * The GLS implementation of the multiple linear regression. * * GLS assumes a general covariance matrix Omega of the error *
                               * u ~ N(0, Omega)
                               * 
                              * * Estimated by GLS, *
                               * b=(X' Omega^-1 X)^-1X'Omega^-1 y
                               * 
                              * whose variance is *
                               * Var(b)=(X' Omega^-1 X)^-1
                               * 
                              * @version $Id: GLSMultipleLinearRegression.java 1416643 2012-12-03 19:37:14Z tn $ * @since 2.0 */ public class GLSMultipleLinearRegression extends AbstractMultipleLinearRegression { /** Covariance matrix. */ private RealMatrix Omega; /** Inverse of covariance matrix. */ private RealMatrix OmegaInverse; /** Replace sample data, overriding any previous sample. * @param y y values of the sample * @param x x values of the sample * @param covariance array representing the covariance matrix */ public void newSampleData(double[] y, double[][] x, double[][] covariance) { validateSampleData(x, y); newYSampleData(y); newXSampleData(x); validateCovarianceData(x, covariance); newCovarianceData(covariance); } /** * Add the covariance data. * * @param omega the [n,n] array representing the covariance */ protected void newCovarianceData(double[][] omega){ this.Omega = new Array2DRowRealMatrix(omega); this.OmegaInverse = null; } /** * Get the inverse of the covariance. *

                              The inverse of the covariance matrix is lazily evaluated and cached.

                              * @return inverse of the covariance */ protected RealMatrix getOmegaInverse() { if (OmegaInverse == null) { OmegaInverse = new LUDecomposition(Omega).getSolver().getInverse(); } return OmegaInverse; } /** * Calculates beta by GLS. *
                                   *  b=(X' Omega^-1 X)^-1X'Omega^-1 y
                                   * 
                              * @return beta */ @Override protected RealVector calculateBeta() { RealMatrix OI = getOmegaInverse(); RealMatrix XT = getX().transpose(); RealMatrix XTOIX = XT.multiply(OI).multiply(getX()); RealMatrix inverse = new LUDecomposition(XTOIX).getSolver().getInverse(); return inverse.multiply(XT).multiply(OI).operate(getY()); } /** * Calculates the variance on the beta. *
                                   *  Var(b)=(X' Omega^-1 X)^-1
                                   * 
                              * @return The beta variance matrix */ @Override protected RealMatrix calculateBetaVariance() { RealMatrix OI = getOmegaInverse(); RealMatrix XTOIX = getX().transpose().multiply(OI).multiply(getX()); return new LUDecomposition(XTOIX).getSolver().getInverse(); } /** * Calculates the estimated variance of the error term using the formula *
                                   *  Var(u) = Tr(u' Omega^-1 u)/(n-k)
                                   * 
                              * where n and k are the row and column dimensions of the design * matrix X. * * @return error variance * @since 2.2 */ @Override protected double calculateErrorVariance() { RealVector residuals = calculateResiduals(); double t = residuals.dotProduct(getOmegaInverse().operate(residuals)); return t / (getX().getRowDimension() - getX().getColumnDimension()); } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/stat/regression/package-info.java100644 1750 1750 1635 12126627717 31056 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ /** * * Statistical routines involving multivariate data. * */ package org.apache.commons.math3.stat.regression; ././@LongLink100644 0 0 163 12126630647 10262 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/stat/regression/UpdatingMultipleLinearRegression.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/stat/regression/UpdatingMultipleLinearR100644 1750 1750 7761 12126627717 32364 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.stat.regression; import org.apache.commons.math3.exception.MathIllegalArgumentException; import org.apache.commons.math3.exception.NoDataException; /** * An interface for regression models allowing for dynamic updating of the data. * That is, the entire data set need not be loaded into memory. As observations * become available, they can be added to the regression model and an updated * estimate regression statistics can be calculated. * * @version $Id: UpdatingMultipleLinearRegression.java 1392342 2012-10-01 14:08:52Z psteitz $ * @since 3.0 */ public interface UpdatingMultipleLinearRegression { /** * Returns true if a constant has been included false otherwise. * * @return true if constant exists, false otherwise */ boolean hasIntercept(); /** * Returns the number of observations added to the regression model. * * @return Number of observations */ long getN(); /** * Adds one observation to the regression model. * * @param x the independent variables which form the design matrix * @param y the dependent or response variable * @throws ModelSpecificationException if the length of {@code x} does not equal * the number of independent variables in the model */ void addObservation(double[] x, double y) throws ModelSpecificationException; /** * Adds a series of observations to the regression model. The lengths of * x and y must be the same and x must be rectangular. * * @param x a series of observations on the independent variables * @param y a series of observations on the dependent variable * The length of x and y must be the same * @throws ModelSpecificationException if {@code x} is not rectangular, does not match * the length of {@code y} or does not contain sufficient data to estimate the model */ void addObservations(double[][] x, double[] y) throws ModelSpecificationException; /** * Clears internal buffers and resets the regression model. This means all * data and derived values are initialized */ void clear(); /** * Performs a regression on data present in buffers and outputs a RegressionResults object * @return RegressionResults acts as a container of regression output * @throws ModelSpecificationException if the model is not correctly specified * @throws NoDataException if there is not sufficient data in the model to * estimate the regression parameters */ RegressionResults regress() throws ModelSpecificationException, NoDataException; /** * Performs a regression on data present in buffers including only regressors * indexed in variablesToInclude and outputs a RegressionResults object * @param variablesToInclude an array of indices of regressors to include * @return RegressionResults acts as a container of regression output * @throws ModelSpecificationException if the model is not correctly specified * @throws MathIllegalArgumentException if the variablesToInclude array is null or zero length */ RegressionResults regress(int[] variablesToInclude) throws ModelSpecificationException, MathIllegalArgumentException; } ././@LongLink100644 0 0 163 12126630647 10262 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/stat/regression/AbstractMultipleLinearRegression.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/stat/regression/AbstractMultipleLinearR100644 1750 1750 32022 12126627717 32360 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.stat.regression; import org.apache.commons.math3.exception.DimensionMismatchException; import org.apache.commons.math3.exception.MathIllegalArgumentException; import org.apache.commons.math3.exception.NoDataException; import org.apache.commons.math3.exception.NullArgumentException; import org.apache.commons.math3.exception.NumberIsTooSmallException; import org.apache.commons.math3.exception.util.LocalizedFormats; import org.apache.commons.math3.linear.NonSquareMatrixException; import org.apache.commons.math3.linear.RealMatrix; import org.apache.commons.math3.linear.Array2DRowRealMatrix; import org.apache.commons.math3.linear.RealVector; import org.apache.commons.math3.linear.ArrayRealVector; import org.apache.commons.math3.stat.descriptive.moment.Variance; import org.apache.commons.math3.util.FastMath; /** * Abstract base class for implementations of MultipleLinearRegression. * @version $Id: AbstractMultipleLinearRegression.java 1416643 2012-12-03 19:37:14Z tn $ * @since 2.0 */ public abstract class AbstractMultipleLinearRegression implements MultipleLinearRegression { /** X sample data. */ private RealMatrix xMatrix; /** Y sample data. */ private RealVector yVector; /** Whether or not the regression model includes an intercept. True means no intercept. */ private boolean noIntercept = false; /** * @return the X sample data. */ protected RealMatrix getX() { return xMatrix; } /** * @return the Y sample data. */ protected RealVector getY() { return yVector; } /** * @return true if the model has no intercept term; false otherwise * @since 2.2 */ public boolean isNoIntercept() { return noIntercept; } /** * @param noIntercept true means the model is to be estimated without an intercept term * @since 2.2 */ public void setNoIntercept(boolean noIntercept) { this.noIntercept = noIntercept; } /** *

                              Loads model x and y sample data from a flat input array, overriding any previous sample. *

                              *

                              Assumes that rows are concatenated with y values first in each row. For example, an input * data array containing the sequence of values (1, 2, 3, 4, 5, 6, 7, 8, 9) with * nobs = 3 and nvars = 2 creates a regression dataset with two * independent variables, as below: *

                                   *   y   x[0]  x[1]
                                   *   --------------
                                   *   1     2     3
                                   *   4     5     6
                                   *   7     8     9
                                   * 
                              *

                              *

                              Note that there is no need to add an initial unitary column (column of 1's) when * specifying a model including an intercept term. If {@link #isNoIntercept()} is true, * the X matrix will be created without an initial column of "1"s; otherwise this column will * be added. *

                              *

                              Throws IllegalArgumentException if any of the following preconditions fail: *

                              • data cannot be null
                              • *
                              • data.length = nobs * (nvars + 1)
                              • *
                              • nobs > nvars
                              *

                              * * @param data input data array * @param nobs number of observations (rows) * @param nvars number of independent variables (columns, not counting y) * @throws NullArgumentException if the data array is null * @throws DimensionMismatchException if the length of the data array is not equal * to nobs * (nvars + 1) * @throws NumberIsTooSmallException if nobs is smaller than * nvars */ public void newSampleData(double[] data, int nobs, int nvars) { if (data == null) { throw new NullArgumentException(); } if (data.length != nobs * (nvars + 1)) { throw new DimensionMismatchException(data.length, nobs * (nvars + 1)); } if (nobs <= nvars) { throw new NumberIsTooSmallException(nobs, nvars, false); } double[] y = new double[nobs]; final int cols = noIntercept ? nvars: nvars + 1; double[][] x = new double[nobs][cols]; int pointer = 0; for (int i = 0; i < nobs; i++) { y[i] = data[pointer++]; if (!noIntercept) { x[i][0] = 1.0d; } for (int j = noIntercept ? 0 : 1; j < cols; j++) { x[i][j] = data[pointer++]; } } this.xMatrix = new Array2DRowRealMatrix(x); this.yVector = new ArrayRealVector(y); } /** * Loads new y sample data, overriding any previous data. * * @param y the array representing the y sample * @throws NullArgumentException if y is null * @throws NoDataException if y is empty */ protected void newYSampleData(double[] y) { if (y == null) { throw new NullArgumentException(); } if (y.length == 0) { throw new NoDataException(); } this.yVector = new ArrayRealVector(y); } /** *

                              Loads new x sample data, overriding any previous data. *

                              * The input x array should have one row for each sample * observation, with columns corresponding to independent variables. * For example, if
                                   *  x = new double[][] {{1, 2}, {3, 4}, {5, 6}} 
                              * then setXSampleData(x) results in a model with two independent * variables and 3 observations: *
                                   *   x[0]  x[1]
                                   *   ----------
                                   *     1    2
                                   *     3    4
                                   *     5    6
                                   * 
                              *

                              *

                              Note that there is no need to add an initial unitary column (column of 1's) when * specifying a model including an intercept term. *

                              * @param x the rectangular array representing the x sample * @throws NullArgumentException if x is null * @throws NoDataException if x is empty * @throws DimensionMismatchException if x is not rectangular */ protected void newXSampleData(double[][] x) { if (x == null) { throw new NullArgumentException(); } if (x.length == 0) { throw new NoDataException(); } if (noIntercept) { this.xMatrix = new Array2DRowRealMatrix(x, true); } else { // Augment design matrix with initial unitary column final int nVars = x[0].length; final double[][] xAug = new double[x.length][nVars + 1]; for (int i = 0; i < x.length; i++) { if (x[i].length != nVars) { throw new DimensionMismatchException(x[i].length, nVars); } xAug[i][0] = 1.0d; System.arraycopy(x[i], 0, xAug[i], 1, nVars); } this.xMatrix = new Array2DRowRealMatrix(xAug, false); } } /** * Validates sample data. Checks that *
                              • Neither x nor y is null or empty;
                              • *
                              • The length (i.e. number of rows) of x equals the length of y
                              • *
                              • x has at least one more row than it has columns (i.e. there is * sufficient data to estimate regression coefficients for each of the * columns in x plus an intercept.
                              • *
                              * * @param x the [n,k] array representing the x data * @param y the [n,1] array representing the y data * @throws NullArgumentException if {@code x} or {@code y} is null * @throws DimensionMismatchException if {@code x} and {@code y} do not * have the same length * @throws NoDataException if {@code x} or {@code y} are zero-length * @throws MathIllegalArgumentException if the number of rows of {@code x} * is not larger than the number of columns + 1 */ protected void validateSampleData(double[][] x, double[] y) throws MathIllegalArgumentException { if ((x == null) || (y == null)) { throw new NullArgumentException(); } if (x.length != y.length) { throw new DimensionMismatchException(y.length, x.length); } if (x.length == 0) { // Must be no y data either throw new NoDataException(); } if (x[0].length + 1 > x.length) { throw new MathIllegalArgumentException( LocalizedFormats.NOT_ENOUGH_DATA_FOR_NUMBER_OF_PREDICTORS, x.length, x[0].length); } } /** * Validates that the x data and covariance matrix have the same * number of rows and that the covariance matrix is square. * * @param x the [n,k] array representing the x sample * @param covariance the [n,n] array representing the covariance matrix * @throws DimensionMismatchException if the number of rows in x is not equal * to the number of rows in covariance * @throws NonSquareMatrixException if the covariance matrix is not square */ protected void validateCovarianceData(double[][] x, double[][] covariance) { if (x.length != covariance.length) { throw new DimensionMismatchException(x.length, covariance.length); } if (covariance.length > 0 && covariance.length != covariance[0].length) { throw new NonSquareMatrixException(covariance.length, covariance[0].length); } } /** * {@inheritDoc} */ public double[] estimateRegressionParameters() { RealVector b = calculateBeta(); return b.toArray(); } /** * {@inheritDoc} */ public double[] estimateResiduals() { RealVector b = calculateBeta(); RealVector e = yVector.subtract(xMatrix.operate(b)); return e.toArray(); } /** * {@inheritDoc} */ public double[][] estimateRegressionParametersVariance() { return calculateBetaVariance().getData(); } /** * {@inheritDoc} */ public double[] estimateRegressionParametersStandardErrors() { double[][] betaVariance = estimateRegressionParametersVariance(); double sigma = calculateErrorVariance(); int length = betaVariance[0].length; double[] result = new double[length]; for (int i = 0; i < length; i++) { result[i] = FastMath.sqrt(sigma * betaVariance[i][i]); } return result; } /** * {@inheritDoc} */ public double estimateRegressandVariance() { return calculateYVariance(); } /** * Estimates the variance of the error. * * @return estimate of the error variance * @since 2.2 */ public double estimateErrorVariance() { return calculateErrorVariance(); } /** * Estimates the standard error of the regression. * * @return regression standard error * @since 2.2 */ public double estimateRegressionStandardError() { return Math.sqrt(estimateErrorVariance()); } /** * Calculates the beta of multiple linear regression in matrix notation. * * @return beta */ protected abstract RealVector calculateBeta(); /** * Calculates the beta variance of multiple linear regression in matrix * notation. * * @return beta variance */ protected abstract RealMatrix calculateBetaVariance(); /** * Calculates the variance of the y values. * * @return Y variance */ protected double calculateYVariance() { return new Variance().evaluate(yVector.toArray()); } /** *

                              Calculates the variance of the error term.

                              * Uses the formula
                                   * var(u) = u · u / (n - k)
                                   * 
                              * where n and k are the row and column dimensions of the design * matrix X. * * @return error variance estimate * @since 2.2 */ protected double calculateErrorVariance() { RealVector residuals = calculateResiduals(); return residuals.dotProduct(residuals) / (xMatrix.getRowDimension() - xMatrix.getColumnDimension()); } /** * Calculates the residuals of multiple linear regression in matrix * notation. * *
                                   * u = y - X * b
                                   * 
                              * * @return The residuals [n,1] matrix */ protected RealVector calculateResiduals() { RealVector b = calculateBeta(); return yVector.subtract(xMatrix.operate(b)); } } ././@LongLink100644 0 0 153 12126630647 10261 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/stat/regression/MultipleLinearRegression.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/stat/regression/MultipleLinearRegressio100644 1750 1750 4722 12126627717 32423 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.stat.regression; /** * The multiple linear regression can be represented in matrix-notation. *
                               *  y=X*b+u
                               * 
                              * where y is an n-vector regressand, X is a [n,k] matrix whose k columns are called * regressors, b is k-vector of regression parameters and u is an n-vector * of error terms or residuals. * * The notation is quite standard in literature, * cf eg Davidson and MacKinnon, Econometrics Theory and Methods, 2004. * @version $Id: MultipleLinearRegression.java 1416643 2012-12-03 19:37:14Z tn $ * @since 2.0 */ public interface MultipleLinearRegression { /** * Estimates the regression parameters b. * * @return The [k,1] array representing b */ double[] estimateRegressionParameters(); /** * Estimates the variance of the regression parameters, ie Var(b). * * @return The [k,k] array representing the variance of b */ double[][] estimateRegressionParametersVariance(); /** * Estimates the residuals, ie u = y - X*b. * * @return The [n,1] array representing the residuals */ double[] estimateResiduals(); /** * Returns the variance of the regressand, ie Var(y). * * @return The double representing the variance of y */ double estimateRegressandVariance(); /** * Returns the standard errors of the regression parameters. * * @return standard errors of estimated regression parameters */ double[] estimateRegressionParametersStandardErrors(); } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/stat/regression/RegressionResults.java100644 1750 1750 37163 12126627717 32261 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.stat.regression; import java.io.Serializable; import java.util.Arrays; import org.apache.commons.math3.util.FastMath; import org.apache.commons.math3.util.MathArrays; import org.apache.commons.math3.exception.OutOfRangeException; /** * Results of a Multiple Linear Regression model fit. * * @version $Id: RegressionResults.java 1392342 2012-10-01 14:08:52Z psteitz $ * @since 3.0 */ public class RegressionResults implements Serializable { /** INDEX of Sum of Squared Errors */ private static final int SSE_IDX = 0; /** INDEX of Sum of Squares of Model */ private static final int SST_IDX = 1; /** INDEX of R-Squared of regression */ private static final int RSQ_IDX = 2; /** INDEX of Mean Squared Error */ private static final int MSE_IDX = 3; /** INDEX of Adjusted R Squared */ private static final int ADJRSQ_IDX = 4; /** UID */ private static final long serialVersionUID = 1l; /** regression slope parameters */ private final double[] parameters; /** variance covariance matrix of parameters */ private final double[][] varCovData; /** boolean flag for variance covariance matrix in symm compressed storage */ private final boolean isSymmetricVCD; /** rank of the solution */ @SuppressWarnings("unused") private final int rank; /** number of observations on which results are based */ private final long nobs; /** boolean flag indicator of whether a constant was included*/ private final boolean containsConstant; /** array storing global results, SSE, MSE, RSQ, adjRSQ */ private final double[] globalFitInfo; /** * Set the default constructor to private access * to prevent inadvertent instantiation */ @SuppressWarnings("unused") private RegressionResults() { this.parameters = null; this.varCovData = null; this.rank = -1; this.nobs = -1; this.containsConstant = false; this.isSymmetricVCD = false; this.globalFitInfo = null; } /** * Constructor for Regression Results. * * @param parameters a double array with the regression slope estimates * @param varcov the variance covariance matrix, stored either in a square matrix * or as a compressed * @param isSymmetricCompressed a flag which denotes that the variance covariance * matrix is in symmetric compressed format * @param nobs the number of observations of the regression estimation * @param rank the number of independent variables in the regression * @param sumy the sum of the independent variable * @param sumysq the sum of the squared independent variable * @param sse sum of squared errors * @param containsConstant true model has constant, false model does not have constant * @param copyData if true a deep copy of all input data is made, if false only references * are copied and the RegressionResults become mutable */ public RegressionResults( final double[] parameters, final double[][] varcov, final boolean isSymmetricCompressed, final long nobs, final int rank, final double sumy, final double sumysq, final double sse, final boolean containsConstant, final boolean copyData) { if (copyData) { this.parameters = MathArrays.copyOf(parameters); this.varCovData = new double[varcov.length][]; for (int i = 0; i < varcov.length; i++) { this.varCovData[i] = MathArrays.copyOf(varcov[i]); } } else { this.parameters = parameters; this.varCovData = varcov; } this.isSymmetricVCD = isSymmetricCompressed; this.nobs = nobs; this.rank = rank; this.containsConstant = containsConstant; this.globalFitInfo = new double[5]; Arrays.fill(this.globalFitInfo, Double.NaN); if (rank > 0) { this.globalFitInfo[SST_IDX] = containsConstant ? (sumysq - sumy * sumy / nobs) : sumysq; } this.globalFitInfo[SSE_IDX] = sse; this.globalFitInfo[MSE_IDX] = this.globalFitInfo[SSE_IDX] / (nobs - rank); this.globalFitInfo[RSQ_IDX] = 1.0 - this.globalFitInfo[SSE_IDX] / this.globalFitInfo[SST_IDX]; if (!containsConstant) { this.globalFitInfo[ADJRSQ_IDX] = 1.0- (1.0 - this.globalFitInfo[RSQ_IDX]) * ( (double) nobs / ( (double) (nobs - rank))); } else { this.globalFitInfo[ADJRSQ_IDX] = 1.0 - (sse * (nobs - 1.0)) / (globalFitInfo[SST_IDX] * (nobs - rank)); } } /** *

                              Returns the parameter estimate for the regressor at the given index.

                              * *

                              A redundant regressor will have its redundancy flag set, as well as * a parameters estimated equal to {@code Double.NaN}

                              * * @param index Index. * @return the parameters estimated for regressor at index. * @throws OutOfRangeException if {@code index} is not in the interval * {@code [0, number of parameters)}. */ public double getParameterEstimate(int index) throws OutOfRangeException { if (parameters == null) { return Double.NaN; } if (index < 0 || index >= this.parameters.length) { throw new OutOfRangeException(index, 0, this.parameters.length - 1); } return this.parameters[index]; } /** *

                              Returns a copy of the regression parameters estimates.

                              * *

                              The parameter estimates are returned in the natural order of the data.

                              * *

                              A redundant regressor will have its redundancy flag set, as will * a parameter estimate equal to {@code Double.NaN}.

                              * * @return array of parameter estimates, null if no estimation occurred */ public double[] getParameterEstimates() { if (this.parameters == null) { return null; } return MathArrays.copyOf(parameters); } /** * Returns the standard * error of the parameter estimate at index, * usually denoted s(bindex). * * @param index Index. * @return the standard errors associated with parameters estimated at index. * @throws OutOfRangeException if {@code index} is not in the interval * {@code [0, number of parameters)}. */ public double getStdErrorOfEstimate(int index) throws OutOfRangeException { if (parameters == null) { return Double.NaN; } if (index < 0 || index >= this.parameters.length) { throw new OutOfRangeException(index, 0, this.parameters.length - 1); } double var = this.getVcvElement(index, index); if (!Double.isNaN(var) && var > Double.MIN_VALUE) { return FastMath.sqrt(var); } return Double.NaN; } /** *

                              Returns the standard * error of the parameter estimates, * usually denoted s(bi).

                              * *

                              If there are problems with an ill conditioned design matrix then the regressor * which is redundant will be assigned Double.NaN.

                              * * @return an array standard errors associated with parameters estimates, * null if no estimation occurred */ public double[] getStdErrorOfEstimates() { if (parameters == null) { return null; } double[] se = new double[this.parameters.length]; for (int i = 0; i < this.parameters.length; i++) { double var = this.getVcvElement(i, i); if (!Double.isNaN(var) && var > Double.MIN_VALUE) { se[i] = FastMath.sqrt(var); continue; } se[i] = Double.NaN; } return se; } /** *

                              Returns the covariance between regression parameters i and j.

                              * *

                              If there are problems with an ill conditioned design matrix then the covariance * which involves redundant columns will be assigned {@code Double.NaN}.

                              * * @param i {@code i}th regression parameter. * @param j {@code j}th regression parameter. * @return the covariance of the parameter estimates. * @throws OutOfRangeException if {@code i} or {@code j} is not in the * interval {@code [0, number of parameters)}. */ public double getCovarianceOfParameters(int i, int j) throws OutOfRangeException { if (parameters == null) { return Double.NaN; } if (i < 0 || i >= this.parameters.length) { throw new OutOfRangeException(i, 0, this.parameters.length - 1); } if (j < 0 || j >= this.parameters.length) { throw new OutOfRangeException(j, 0, this.parameters.length - 1); } return this.getVcvElement(i, j); } /** *

                              Returns the number of parameters estimated in the model.

                              * *

                              This is the maximum number of regressors, some techniques may drop * redundant parameters

                              * * @return number of regressors, -1 if not estimated */ public int getNumberOfParameters() { if (this.parameters == null) { return -1; } return this.parameters.length; } /** * Returns the number of observations added to the regression model. * * @return Number of observations, -1 if an error condition prevents estimation */ public long getN() { return this.nobs; } /** *

                              Returns the sum of squared deviations of the y values about their mean.

                              * *

                              This is defined as SSTO * here.

                              * *

                              If {@code n < 2}, this returns {@code Double.NaN}.

                              * * @return sum of squared deviations of y values */ public double getTotalSumSquares() { return this.globalFitInfo[SST_IDX]; } /** *

                              Returns the sum of squared deviations of the predicted y values about * their mean (which equals the mean of y).

                              * *

                              This is usually abbreviated SSR or SSM. It is defined as SSM * here

                              * *

                              Preconditions:

                                *
                              • At least two observations (with at least two different x values) * must have been added before invoking this method. If this method is * invoked before a model can be estimated, Double.NaN is * returned. *

                              * * @return sum of squared deviations of predicted y values */ public double getRegressionSumSquares() { return this.globalFitInfo[SST_IDX] - this.globalFitInfo[SSE_IDX]; } /** *

                              Returns the * sum of squared errors (SSE) associated with the regression * model.

                              * *

                              The return value is constrained to be non-negative - i.e., if due to * rounding errors the computational formula returns a negative result, * 0 is returned.

                              * *

                              Preconditions:

                                *
                              • numberOfParameters data pairs * must have been added before invoking this method. If this method is * invoked before a model can be estimated, Double,NaN is * returned. *

                              * * @return sum of squared errors associated with the regression model */ public double getErrorSumSquares() { return this.globalFitInfo[ SSE_IDX]; } /** *

                              Returns the sum of squared errors divided by the degrees of freedom, * usually abbreviated MSE.

                              * *

                              If there are fewer than numberOfParameters + 1 data pairs in the model, * or if there is no variation in x, this returns * Double.NaN.

                              * * @return sum of squared deviations of y values */ public double getMeanSquareError() { return this.globalFitInfo[ MSE_IDX]; } /** *

                              Returns the * coefficient of multiple determination, * usually denoted r-square.

                              * *

                              Preconditions:

                                *
                              • At least numberOfParameters observations (with at least numberOfParameters different x values) * must have been added before invoking this method. If this method is * invoked before a model can be estimated, {@code Double,NaN} is * returned. *

                              * * @return r-square, a double in the interval [0, 1] */ public double getRSquared() { return this.globalFitInfo[ RSQ_IDX]; } /** *

                              Returns the adjusted R-squared statistic, defined by the formula

                                   * R2adj = 1 - [SSR (n - 1)] / [SSTO (n - p)]
                                   * 
                              * where SSR is the sum of squared residuals}, * SSTO is the total sum of squares}, n is the number * of observations and p is the number of parameters estimated (including the intercept).

                              * *

                              If the regression is estimated without an intercept term, what is returned is

                                   *  1 - (1 - {@link #getRSquared()} ) * (n / (n - p)) 
                                   * 

                              * * @return adjusted R-Squared statistic */ public double getAdjustedRSquared() { return this.globalFitInfo[ ADJRSQ_IDX]; } /** * Returns true if the regression model has been computed including an intercept. * In this case, the coefficient of the intercept is the first element of the * {@link #getParameterEstimates() parameter estimates}. * @return true if the model has an intercept term */ public boolean hasIntercept() { return this.containsConstant; } /** * Gets the i-jth element of the variance-covariance matrix. * * @param i first variable index * @param j second variable index * @return the requested variance-covariance matrix entry */ private double getVcvElement(int i, int j) { if (this.isSymmetricVCD) { if (this.varCovData.length > 1) { //could be stored in upper or lower triangular if (i == j) { return varCovData[i][i]; } else if (i >= varCovData[j].length) { return varCovData[i][j]; } else { return varCovData[j][i]; } } else {//could be in single array if (i > j) { return varCovData[0][(i + 1) * i / 2 + j]; } else { return varCovData[0][(j + 1) * j / 2 + i]; } } } else { return this.varCovData[i][j]; } } } ././@LongLink100644 0 0 153 12126630647 10261 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/stat/regression/MillerUpdatingRegression.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/stat/regression/MillerUpdatingRegressio100644 1750 1750 115174 12126627717 32461 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.stat.regression; import java.util.Arrays; import org.apache.commons.math3.exception.util.LocalizedFormats; import org.apache.commons.math3.util.FastMath; import org.apache.commons.math3.util.Precision; import org.apache.commons.math3.util.MathArrays; /** * This class is a concrete implementation of the {@link UpdatingMultipleLinearRegression} interface. * *

                              The algorithm is described in:

                               * Algorithm AS 274: Least Squares Routines to Supplement Those of Gentleman
                               * Author(s): Alan J. Miller
                               * Source: Journal of the Royal Statistical Society.
                               * Series C (Applied Statistics), Vol. 41, No. 2
                               * (1992), pp. 458-478
                               * Published by: Blackwell Publishing for the Royal Statistical Society
                               * Stable URL: http://www.jstor.org/stable/2347583 

                              * *

                              This method for multiple regression forms the solution to the OLS problem * by updating the QR decomposition as described by Gentleman.

                              * * @version $Id: MillerUpdatingRegression.java 1392358 2012-10-01 14:41:55Z psteitz $ * @since 3.0 */ public class MillerUpdatingRegression implements UpdatingMultipleLinearRegression { /** number of variables in regression */ private final int nvars; /** diagonals of cross products matrix */ private final double[] d; /** the elements of the R`Y */ private final double[] rhs; /** the off diagonal portion of the R matrix */ private final double[] r; /** the tolerance for each of the variables */ private final double[] tol; /** residual sum of squares for all nested regressions */ private final double[] rss; /** order of the regressors */ private final int[] vorder; /** scratch space for tolerance calc */ private final double[] work_tolset; /** number of observations entered */ private long nobs = 0; /** sum of squared errors of largest regression */ private double sserr = 0.0; /** has rss been called? */ private boolean rss_set = false; /** has the tolerance setting method been called */ private boolean tol_set = false; /** flags for variables with linear dependency problems */ private final boolean[] lindep; /** singular x values */ private final double[] x_sing; /** workspace for singularity method */ private final double[] work_sing; /** summation of Y variable */ private double sumy = 0.0; /** summation of squared Y values */ private double sumsqy = 0.0; /** boolean flag whether a regression constant is added */ private boolean hasIntercept; /** zero tolerance */ private final double epsilon; /** * Set the default constructor to private access * to prevent inadvertent instantiation */ @SuppressWarnings("unused") private MillerUpdatingRegression() { this(-1, false, Double.NaN); } /** * This is the augmented constructor for the MillerUpdatingRegression class. * * @param numberOfVariables number of regressors to expect, not including constant * @param includeConstant include a constant automatically * @param errorTolerance zero tolerance, how machine zero is determined * @throws ModelSpecificationException if {@code numberOfVariables is less than 1} */ public MillerUpdatingRegression(int numberOfVariables, boolean includeConstant, double errorTolerance) throws ModelSpecificationException { if (numberOfVariables < 1) { throw new ModelSpecificationException(LocalizedFormats.NO_REGRESSORS); } if (includeConstant) { this.nvars = numberOfVariables + 1; } else { this.nvars = numberOfVariables; } this.hasIntercept = includeConstant; this.nobs = 0; this.d = new double[this.nvars]; this.rhs = new double[this.nvars]; this.r = new double[this.nvars * (this.nvars - 1) / 2]; this.tol = new double[this.nvars]; this.rss = new double[this.nvars]; this.vorder = new int[this.nvars]; this.x_sing = new double[this.nvars]; this.work_sing = new double[this.nvars]; this.work_tolset = new double[this.nvars]; this.lindep = new boolean[this.nvars]; for (int i = 0; i < this.nvars; i++) { vorder[i] = i; } if (errorTolerance > 0) { this.epsilon = errorTolerance; } else { this.epsilon = -errorTolerance; } } /** * Primary constructor for the MillerUpdatingRegression. * * @param numberOfVariables maximum number of potential regressors * @param includeConstant include a constant automatically * @throws ModelSpecificationException if {@code numberOfVariables is less than 1} */ public MillerUpdatingRegression(int numberOfVariables, boolean includeConstant) throws ModelSpecificationException { this(numberOfVariables, includeConstant, Precision.EPSILON); } /** * A getter method which determines whether a constant is included. * @return true regression has an intercept, false no intercept */ public boolean hasIntercept() { return this.hasIntercept; } /** * Gets the number of observations added to the regression model. * @return number of observations */ public long getN() { return this.nobs; } /** * Adds an observation to the regression model. * @param x the array with regressor values * @param y the value of dependent variable given these regressors * @exception ModelSpecificationException if the length of {@code x} does not equal * the number of independent variables in the model */ public void addObservation(final double[] x, final double y) throws ModelSpecificationException { if ((!this.hasIntercept && x.length != nvars) || (this.hasIntercept && x.length + 1 != nvars)) { throw new ModelSpecificationException(LocalizedFormats.INVALID_REGRESSION_OBSERVATION, x.length, nvars); } if (!this.hasIntercept) { include(MathArrays.copyOf(x, x.length), 1.0, y); } else { final double[] tmp = new double[x.length + 1]; System.arraycopy(x, 0, tmp, 1, x.length); tmp[0] = 1.0; include(tmp, 1.0, y); } ++nobs; } /** * Adds multiple observations to the model. * @param x observations on the regressors * @param y observations on the regressand * @throws ModelSpecificationException if {@code x} is not rectangular, does not match * the length of {@code y} or does not contain sufficient data to estimate the model */ public void addObservations(double[][] x, double[] y) throws ModelSpecificationException { if ((x == null) || (y == null) || (x.length != y.length)) { throw new ModelSpecificationException( LocalizedFormats.DIMENSIONS_MISMATCH_SIMPLE, (x == null) ? 0 : x.length, (y == null) ? 0 : y.length); } if (x.length == 0) { // Must be no y data either throw new ModelSpecificationException( LocalizedFormats.NO_DATA); } if (x[0].length + 1 > x.length) { throw new ModelSpecificationException( LocalizedFormats.NOT_ENOUGH_DATA_FOR_NUMBER_OF_PREDICTORS, x.length, x[0].length); } for (int i = 0; i < x.length; i++) { addObservation(x[i], y[i]); } } /** * The include method is where the QR decomposition occurs. This statement forms all * intermediate data which will be used for all derivative measures. * According to the miller paper, note that in the original implementation the x vector * is overwritten. In this implementation, the include method is passed a copy of the * original data vector so that there is no contamination of the data. Additionally, * this method differs slightly from Gentleman's method, in that the assumption is * of dense design matrices, there is some advantage in using the original gentleman algorithm * on sparse matrices. * * @param x observations on the regressors * @param wi weight of the this observation (-1,1) * @param yi observation on the regressand */ private void include(final double[] x, final double wi, final double yi) { int nextr = 0; double w = wi; double y = yi; double xi; double di; double wxi; double dpi; double xk; double _w; this.rss_set = false; sumy = smartAdd(yi, sumy); sumsqy = smartAdd(sumsqy, yi * yi); for (int i = 0; i < x.length; i++) { if (w == 0.0) { return; } xi = x[i]; if (xi == 0.0) { nextr += nvars - i - 1; continue; } di = d[i]; wxi = w * xi; _w = w; if (di != 0.0) { dpi = smartAdd(di, wxi * xi); final double tmp = wxi * xi / di; if (FastMath.abs(tmp) > Precision.EPSILON) { w = (di * w) / dpi; } } else { dpi = wxi * xi; w = 0.0; } d[i] = dpi; for (int k = i + 1; k < nvars; k++) { xk = x[k]; x[k] = smartAdd(xk, -xi * r[nextr]); if (di != 0.0) { r[nextr] = smartAdd(di * r[nextr], (_w * xi) * xk) / dpi; } else { r[nextr] = xk / xi; } ++nextr; } xk = y; y = smartAdd(xk, -xi * rhs[i]); if (di != 0.0) { rhs[i] = smartAdd(di * rhs[i], wxi * xk) / dpi; } else { rhs[i] = xk / xi; } } sserr = smartAdd(sserr, w * y * y); } /** * Adds to number a and b such that the contamination due to * numerical smallness of one addend does not corrupt the sum. * @param a - an addend * @param b - an addend * @return the sum of the a and b */ private double smartAdd(double a, double b) { final double _a = FastMath.abs(a); final double _b = FastMath.abs(b); if (_a > _b) { final double eps = _a * Precision.EPSILON; if (_b > eps) { return a + b; } return a; } else { final double eps = _b * Precision.EPSILON; if (_a > eps) { return a + b; } return b; } } /** * As the name suggests, clear wipes the internals and reorders everything in the * canonical order. */ public void clear() { Arrays.fill(this.d, 0.0); Arrays.fill(this.rhs, 0.0); Arrays.fill(this.r, 0.0); Arrays.fill(this.tol, 0.0); Arrays.fill(this.rss, 0.0); Arrays.fill(this.work_tolset, 0.0); Arrays.fill(this.work_sing, 0.0); Arrays.fill(this.x_sing, 0.0); Arrays.fill(this.lindep, false); for (int i = 0; i < nvars; i++) { this.vorder[i] = i; } this.nobs = 0; this.sserr = 0.0; this.sumy = 0.0; this.sumsqy = 0.0; this.rss_set = false; this.tol_set = false; } /** * This sets up tolerances for singularity testing. */ private void tolset() { int pos; double total; final double eps = this.epsilon; for (int i = 0; i < nvars; i++) { this.work_tolset[i] = Math.sqrt(d[i]); } tol[0] = eps * this.work_tolset[0]; for (int col = 1; col < nvars; col++) { pos = col - 1; total = work_tolset[col]; for (int row = 0; row < col; row++) { total += Math.abs(r[pos]) * work_tolset[row]; pos += nvars - row - 2; } tol[col] = eps * total; } tol_set = true; } /** * The regcf method conducts the linear regression and extracts the * parameter vector. Notice that the algorithm can do subset regression * with no alteration. * * @param nreq how many of the regressors to include (either in canonical * order, or in the current reordered state) * @return an array with the estimated slope coefficients * @throws ModelSpecificationException if {@code nreq} is less than 1 * or greater than the number of independent variables */ private double[] regcf(int nreq) throws ModelSpecificationException { int nextr; if (nreq < 1) { throw new ModelSpecificationException(LocalizedFormats.NO_REGRESSORS); } if (nreq > this.nvars) { throw new ModelSpecificationException( LocalizedFormats.TOO_MANY_REGRESSORS, nreq, this.nvars); } if (!this.tol_set) { tolset(); } final double[] ret = new double[nreq]; boolean rankProblem = false; for (int i = nreq - 1; i > -1; i--) { if (Math.sqrt(d[i]) < tol[i]) { ret[i] = 0.0; d[i] = 0.0; rankProblem = true; } else { ret[i] = rhs[i]; nextr = i * (nvars + nvars - i - 1) / 2; for (int j = i + 1; j < nreq; j++) { ret[i] = smartAdd(ret[i], -r[nextr] * ret[j]); ++nextr; } } } if (rankProblem) { for (int i = 0; i < nreq; i++) { if (this.lindep[i]) { ret[i] = Double.NaN; } } } return ret; } /** * The method which checks for singularities and then eliminates the offending * columns. */ private void singcheck() { int pos; for (int i = 0; i < nvars; i++) { work_sing[i] = Math.sqrt(d[i]); } for (int col = 0; col < nvars; col++) { // Set elements within R to zero if they are less than tol(col) in // absolute value after being scaled by the square root of their row // multiplier final double temp = tol[col]; pos = col - 1; for (int row = 0; row < col - 1; row++) { if (Math.abs(r[pos]) * work_sing[row] < temp) { r[pos] = 0.0; } pos += nvars - row - 2; } // If diagonal element is near zero, set it to zero, set appropriate // element of LINDEP, and use INCLUD to augment the projections in // the lower rows of the orthogonalization. lindep[col] = false; if (work_sing[col] < temp) { lindep[col] = true; if (col < nvars - 1) { Arrays.fill(x_sing, 0.0); int _pi = col * (nvars + nvars - col - 1) / 2; for (int _xi = col + 1; _xi < nvars; _xi++, _pi++) { x_sing[_xi] = r[_pi]; r[_pi] = 0.0; } final double y = rhs[col]; final double weight = d[col]; d[col] = 0.0; rhs[col] = 0.0; this.include(x_sing, weight, y); } else { sserr += d[col] * rhs[col] * rhs[col]; } } } } /** * Calculates the sum of squared errors for the full regression * and all subsets in the following manner:
                                   * rss[] ={
                                   * ResidualSumOfSquares_allNvars,
                                   * ResidualSumOfSquares_FirstNvars-1,
                                   * ResidualSumOfSquares_FirstNvars-2,
                                   * ..., ResidualSumOfSquares_FirstVariable} 
                              */ private void ss() { double total = sserr; rss[nvars - 1] = sserr; for (int i = nvars - 1; i > 0; i--) { total += d[i] * rhs[i] * rhs[i]; rss[i - 1] = total; } rss_set = true; } /** * Calculates the cov matrix assuming only the first nreq variables are * included in the calculation. The returned array contains a symmetric * matrix stored in lower triangular form. The matrix will have * ( nreq + 1 ) * nreq / 2 elements. For illustration
                                   * cov =
                                   * {
                                   *  cov_00,
                                   *  cov_10, cov_11,
                                   *  cov_20, cov_21, cov22,
                                   *  ...
                                   * } 
                              * * @param nreq how many of the regressors to include (either in canonical * order, or in the current reordered state) * @return an array with the variance covariance of the included * regressors in lower triangular form */ private double[] cov(int nreq) { if (this.nobs <= nreq) { return null; } double rnk = 0.0; for (int i = 0; i < nreq; i++) { if (!this.lindep[i]) { rnk += 1.0; } } final double var = rss[nreq - 1] / (nobs - rnk); final double[] rinv = new double[nreq * (nreq - 1) / 2]; inverse(rinv, nreq); final double[] covmat = new double[nreq * (nreq + 1) / 2]; Arrays.fill(covmat, Double.NaN); int pos2; int pos1; int start = 0; double total = 0; for (int row = 0; row < nreq; row++) { pos2 = start; if (!this.lindep[row]) { for (int col = row; col < nreq; col++) { if (!this.lindep[col]) { pos1 = start + col - row; if (row == col) { total = 1.0 / d[col]; } else { total = rinv[pos1 - 1] / d[col]; } for (int k = col + 1; k < nreq; k++) { if (!this.lindep[k]) { total += rinv[pos1] * rinv[pos2] / d[k]; } ++pos1; ++pos2; } covmat[ (col + 1) * col / 2 + row] = total * var; } else { pos2 += nreq - col - 1; } } } start += nreq - row - 1; } return covmat; } /** * This internal method calculates the inverse of the upper-triangular portion * of the R matrix. * @param rinv the storage for the inverse of r * @param nreq how many of the regressors to include (either in canonical * order, or in the current reordered state) */ private void inverse(double[] rinv, int nreq) { int pos = nreq * (nreq - 1) / 2 - 1; int pos1 = -1; int pos2 = -1; double total = 0.0; Arrays.fill(rinv, Double.NaN); for (int row = nreq - 1; row > 0; --row) { if (!this.lindep[row]) { final int start = (row - 1) * (nvars + nvars - row) / 2; for (int col = nreq; col > row; --col) { pos1 = start; pos2 = pos; total = 0.0; for (int k = row; k < col - 1; k++) { pos2 += nreq - k - 1; if (!this.lindep[k]) { total += -r[pos1] * rinv[pos2]; } ++pos1; } rinv[pos] = total - r[pos1]; --pos; } } else { pos -= nreq - row; } } } /** * In the original algorithm only the partial correlations of the regressors * is returned to the user. In this implementation, we have
                                   * corr =
                                   * {
                                   *   corrxx - lower triangular
                                   *   corrxy - bottom row of the matrix
                                   * }
                                   * Replaces subroutines PCORR and COR of:
                                   * ALGORITHM AS274  APPL. STATIST. (1992) VOL.41, NO. 2 
                              * *

                              Calculate partial correlations after the variables in rows * 1, 2, ..., IN have been forced into the regression. * If IN = 1, and the first row of R represents a constant in the * model, then the usual simple correlations are returned.

                              * *

                              If IN = 0, the value returned in array CORMAT for the correlation * of variables Xi & Xj is:

                                   * sum ( Xi.Xj ) / Sqrt ( sum (Xi^2) . sum (Xj^2) )

                              * *

                              On return, array CORMAT contains the upper triangle of the matrix of * partial correlations stored by rows, excluding the 1's on the diagonal. * e.g. if IN = 2, the consecutive elements returned are: * (3,4) (3,5) ... (3,ncol), (4,5) (4,6) ... (4,ncol), etc. * Array YCORR stores the partial correlations with the Y-variable * starting with YCORR(IN+1) = partial correlation with the variable in * position (IN+1).

                              * * @param in how many of the regressors to include (either in canonical * order, or in the current reordered state) * @return an array with the partial correlations of the remainder of * regressors with each other and the regressand, in lower triangular form */ public double[] getPartialCorrelations(int in) { final double[] output = new double[(nvars - in + 1) * (nvars - in) / 2]; int pos; int pos1; int pos2; final int rms_off = -in; final int wrk_off = -(in + 1); final double[] rms = new double[nvars - in]; final double[] work = new double[nvars - in - 1]; double sumxx; double sumxy; double sumyy; final int offXX = (nvars - in) * (nvars - in - 1) / 2; if (in < -1 || in >= nvars) { return null; } final int nvm = nvars - 1; final int base_pos = r.length - (nvm - in) * (nvm - in + 1) / 2; if (d[in] > 0.0) { rms[in + rms_off] = 1.0 / Math.sqrt(d[in]); } for (int col = in + 1; col < nvars; col++) { pos = base_pos + col - 1 - in; sumxx = d[col]; for (int row = in; row < col; row++) { sumxx += d[row] * r[pos] * r[pos]; pos += nvars - row - 2; } if (sumxx > 0.0) { rms[col + rms_off] = 1.0 / Math.sqrt(sumxx); } else { rms[col + rms_off] = 0.0; } } sumyy = sserr; for (int row = in; row < nvars; row++) { sumyy += d[row] * rhs[row] * rhs[row]; } if (sumyy > 0.0) { sumyy = 1.0 / Math.sqrt(sumyy); } pos = 0; for (int col1 = in; col1 < nvars; col1++) { sumxy = 0.0; Arrays.fill(work, 0.0); pos1 = base_pos + col1 - in - 1; for (int row = in; row < col1; row++) { pos2 = pos1 + 1; for (int col2 = col1 + 1; col2 < nvars; col2++) { work[col2 + wrk_off] += d[row] * r[pos1] * r[pos2]; pos2++; } sumxy += d[row] * r[pos1] * rhs[row]; pos1 += nvars - row - 2; } pos2 = pos1 + 1; for (int col2 = col1 + 1; col2 < nvars; col2++) { work[col2 + wrk_off] += d[col1] * r[pos2]; ++pos2; output[ (col2 - 1 - in) * (col2 - in) / 2 + col1 - in] = work[col2 + wrk_off] * rms[col1 + rms_off] * rms[col2 + rms_off]; ++pos; } sumxy += d[col1] * rhs[col1]; output[col1 + rms_off + offXX] = sumxy * rms[col1 + rms_off] * sumyy; } return output; } /** * ALGORITHM AS274 APPL. STATIST. (1992) VOL.41, NO. 2. * Move variable from position FROM to position TO in an * orthogonal reduction produced by AS75.1. * * @param from initial position * @param to destination */ private void vmove(int from, int to) { double d1; double d2; double X; double d1new; double d2new; double cbar; double sbar; double Y; int first; int inc; int m1; int m2; int mp1; int pos; boolean bSkipTo40 = false; if (from == to) { return; } if (!this.rss_set) { ss(); } int count = 0; if (from < to) { first = from; inc = 1; count = to - from; } else { first = from - 1; inc = -1; count = from - to; } int m = first; int idx = 0; while (idx < count) { m1 = m * (nvars + nvars - m - 1) / 2; m2 = m1 + nvars - m - 1; mp1 = m + 1; d1 = d[m]; d2 = d[mp1]; // Special cases. if (d1 > this.epsilon || d2 > this.epsilon) { X = r[m1]; if (Math.abs(X) * Math.sqrt(d1) < tol[mp1]) { X = 0.0; } if (d1 < this.epsilon || Math.abs(X) < this.epsilon) { d[m] = d2; d[mp1] = d1; r[m1] = 0.0; for (int col = m + 2; col < nvars; col++) { ++m1; X = r[m1]; r[m1] = r[m2]; r[m2] = X; ++m2; } X = rhs[m]; rhs[m] = rhs[mp1]; rhs[mp1] = X; bSkipTo40 = true; //break; } else if (d2 < this.epsilon) { d[m] = d1 * X * X; r[m1] = 1.0 / X; for (int _i = m1 + 1; _i < m1 + nvars - m - 1; _i++) { r[_i] /= X; } rhs[m] = rhs[m] / X; bSkipTo40 = true; //break; } if (!bSkipTo40) { d1new = d2 + d1 * X * X; cbar = d2 / d1new; sbar = X * d1 / d1new; d2new = d1 * cbar; d[m] = d1new; d[mp1] = d2new; r[m1] = sbar; for (int col = m + 2; col < nvars; col++) { ++m1; Y = r[m1]; r[m1] = cbar * r[m2] + sbar * Y; r[m2] = Y - X * r[m2]; ++m2; } Y = rhs[m]; rhs[m] = cbar * rhs[mp1] + sbar * Y; rhs[mp1] = Y - X * rhs[mp1]; } } if (m > 0) { pos = m; for (int row = 0; row < m; row++) { X = r[pos]; r[pos] = r[pos - 1]; r[pos - 1] = X; pos += nvars - row - 2; } } // Adjust variable order (VORDER), the tolerances (TOL) and // the vector of residual sums of squares (RSS). m1 = vorder[m]; vorder[m] = vorder[mp1]; vorder[mp1] = m1; X = tol[m]; tol[m] = tol[mp1]; tol[mp1] = X; rss[m] = rss[mp1] + d[mp1] * rhs[mp1] * rhs[mp1]; m += inc; ++idx; } } /** * ALGORITHM AS274 APPL. STATIST. (1992) VOL.41, NO. 2 * *

                              Re-order the variables in an orthogonal reduction produced by * AS75.1 so that the N variables in LIST start at position POS1, * though will not necessarily be in the same order as in LIST. * Any variables in VORDER before position POS1 are not moved. * Auxiliary routine called: VMOVE.

                              * *

                              This internal method reorders the regressors.

                              * * @param list the regressors to move * @param pos1 where the list will be placed * @return -1 error, 0 everything ok */ private int reorderRegressors(int[] list, int pos1) { int next; int i; int l; if (list.length < 1 || list.length > nvars + 1 - pos1) { return -1; } next = pos1; i = pos1; while (i < nvars) { l = vorder[i]; for (int j = 0; j < list.length; j++) { if (l == list[j] && i > next) { this.vmove(i, next); ++next; if (next >= list.length + pos1) { return 0; } else { break; } } } ++i; } return 0; } /** * Gets the diagonal of the Hat matrix also known as the leverage matrix. * * @param row_data returns the diagonal of the hat matrix for this observation * @return the diagonal element of the hatmatrix */ public double getDiagonalOfHatMatrix(double[] row_data) { double[] wk = new double[this.nvars]; int pos; double total; if (row_data.length > nvars) { return Double.NaN; } double[] xrow; if (this.hasIntercept) { xrow = new double[row_data.length + 1]; xrow[0] = 1.0; System.arraycopy(row_data, 0, xrow, 1, row_data.length); } else { xrow = row_data; } double hii = 0.0; for (int col = 0; col < xrow.length; col++) { if (Math.sqrt(d[col]) < tol[col]) { wk[col] = 0.0; } else { pos = col - 1; total = xrow[col]; for (int row = 0; row < col; row++) { total = smartAdd(total, -wk[row] * r[pos]); pos += nvars - row - 2; } wk[col] = total; hii = smartAdd(hii, (total * total) / d[col]); } } return hii; } /** * Gets the order of the regressors, useful if some type of reordering * has been called. Calling regress with int[]{} args will trigger * a reordering. * * @return int[] with the current order of the regressors */ public int[] getOrderOfRegressors(){ return MathArrays.copyOf(vorder); } /** * Conducts a regression on the data in the model, using all regressors. * * @return RegressionResults the structure holding all regression results * @exception ModelSpecificationException - thrown if number of observations is * less than the number of variables */ public RegressionResults regress() throws ModelSpecificationException { return regress(this.nvars); } /** * Conducts a regression on the data in the model, using a subset of regressors. * * @param numberOfRegressors many of the regressors to include (either in canonical * order, or in the current reordered state) * @return RegressionResults the structure holding all regression results * @exception ModelSpecificationException - thrown if number of observations is * less than the number of variables or number of regressors requested * is greater than the regressors in the model */ public RegressionResults regress(int numberOfRegressors) throws ModelSpecificationException { if (this.nobs <= numberOfRegressors) { throw new ModelSpecificationException( LocalizedFormats.NOT_ENOUGH_DATA_FOR_NUMBER_OF_PREDICTORS, this.nobs, numberOfRegressors); } if( numberOfRegressors > this.nvars ){ throw new ModelSpecificationException( LocalizedFormats.TOO_MANY_REGRESSORS, numberOfRegressors, this.nvars); } tolset(); singcheck(); double[] beta = this.regcf(numberOfRegressors); ss(); double[] cov = this.cov(numberOfRegressors); int rnk = 0; for (int i = 0; i < this.lindep.length; i++) { if (!this.lindep[i]) { ++rnk; } } boolean needsReorder = false; for (int i = 0; i < numberOfRegressors; i++) { if (this.vorder[i] != i) { needsReorder = true; break; } } if (!needsReorder) { return new RegressionResults( beta, new double[][]{cov}, true, this.nobs, rnk, this.sumy, this.sumsqy, this.sserr, this.hasIntercept, false); } else { double[] betaNew = new double[beta.length]; double[] covNew = new double[cov.length]; int[] newIndices = new int[beta.length]; for (int i = 0; i < nvars; i++) { for (int j = 0; j < numberOfRegressors; j++) { if (this.vorder[j] == i) { betaNew[i] = beta[ j]; newIndices[i] = j; } } } int idx1 = 0; int idx2; int _i; int _j; for (int i = 0; i < beta.length; i++) { _i = newIndices[i]; for (int j = 0; j <= i; j++, idx1++) { _j = newIndices[j]; if (_i > _j) { idx2 = _i * (_i + 1) / 2 + _j; } else { idx2 = _j * (_j + 1) / 2 + _i; } covNew[idx1] = cov[idx2]; } } return new RegressionResults( betaNew, new double[][]{covNew}, true, this.nobs, rnk, this.sumy, this.sumsqy, this.sserr, this.hasIntercept, false); } } /** * Conducts a regression on the data in the model, using regressors in array * Calling this method will change the internal order of the regressors * and care is required in interpreting the hatmatrix. * * @param variablesToInclude array of variables to include in regression * @return RegressionResults the structure holding all regression results * @exception ModelSpecificationException - thrown if number of observations is * less than the number of variables, the number of regressors requested * is greater than the regressors in the model or a regressor index in * regressor array does not exist */ public RegressionResults regress(int[] variablesToInclude) throws ModelSpecificationException { if (variablesToInclude.length > this.nvars) { throw new ModelSpecificationException( LocalizedFormats.TOO_MANY_REGRESSORS, variablesToInclude.length, this.nvars); } if (this.nobs <= this.nvars) { throw new ModelSpecificationException( LocalizedFormats.NOT_ENOUGH_DATA_FOR_NUMBER_OF_PREDICTORS, this.nobs, this.nvars); } Arrays.sort(variablesToInclude); int iExclude = 0; for (int i = 0; i < variablesToInclude.length; i++) { if (i >= this.nvars) { throw new ModelSpecificationException( LocalizedFormats.INDEX_LARGER_THAN_MAX, i, this.nvars); } if (i > 0 && variablesToInclude[i] == variablesToInclude[i - 1]) { variablesToInclude[i] = -1; ++iExclude; } } int[] series; if (iExclude > 0) { int j = 0; series = new int[variablesToInclude.length - iExclude]; for (int i = 0; i < variablesToInclude.length; i++) { if (variablesToInclude[i] > -1) { series[j] = variablesToInclude[i]; ++j; } } } else { series = variablesToInclude; } reorderRegressors(series, 0); tolset(); singcheck(); double[] beta = this.regcf(series.length); ss(); double[] cov = this.cov(series.length); int rnk = 0; for (int i = 0; i < this.lindep.length; i++) { if (!this.lindep[i]) { ++rnk; } } boolean needsReorder = false; for (int i = 0; i < this.nvars; i++) { if (this.vorder[i] != series[i]) { needsReorder = true; break; } } if (!needsReorder) { return new RegressionResults( beta, new double[][]{cov}, true, this.nobs, rnk, this.sumy, this.sumsqy, this.sserr, this.hasIntercept, false); } else { double[] betaNew = new double[beta.length]; int[] newIndices = new int[beta.length]; for (int i = 0; i < series.length; i++) { for (int j = 0; j < this.vorder.length; j++) { if (this.vorder[j] == series[i]) { betaNew[i] = beta[ j]; newIndices[i] = j; } } } double[] covNew = new double[cov.length]; int idx1 = 0; int idx2; int _i; int _j; for (int i = 0; i < beta.length; i++) { _i = newIndices[i]; for (int j = 0; j <= i; j++, idx1++) { _j = newIndices[j]; if (_i > _j) { idx2 = _i * (_i + 1) / 2 + _j; } else { idx2 = _j * (_j + 1) / 2 + _i; } covNew[idx1] = cov[idx2]; } } return new RegressionResults( betaNew, new double[][]{covNew}, true, this.nobs, rnk, this.sumy, this.sumsqy, this.sserr, this.hasIntercept, false); } } } ././@LongLink100644 0 0 156 12126630647 10264 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/stat/regression/OLSMultipleLinearRegression.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/stat/regression/OLSMultipleLinearRegres100644 1750 1750 24457 12126627717 32315 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.stat.regression; import org.apache.commons.math3.exception.MathIllegalArgumentException; import org.apache.commons.math3.linear.Array2DRowRealMatrix; import org.apache.commons.math3.linear.LUDecomposition; import org.apache.commons.math3.linear.QRDecomposition; import org.apache.commons.math3.linear.RealMatrix; import org.apache.commons.math3.linear.RealVector; import org.apache.commons.math3.stat.StatUtils; import org.apache.commons.math3.stat.descriptive.moment.SecondMoment; /** *

                              Implements ordinary least squares (OLS) to estimate the parameters of a * multiple linear regression model.

                              * *

                              The regression coefficients, b, satisfy the normal equations: *

                               XT X b = XT y 

                              * *

                              To solve the normal equations, this implementation uses QR decomposition * of the X matrix. (See {@link QRDecomposition} for details on the * decomposition algorithm.) The X matrix, also known as the design matrix, * has rows corresponding to sample observations and columns corresponding to independent * variables. When the model is estimated using an intercept term (i.e. when * {@link #isNoIntercept() isNoIntercept} is false as it is by default), the X * matrix includes an initial column identically equal to 1. We solve the normal equations * as follows: *

                               XTX b = XT y
                               * (QR)T (QR) b = (QR)Ty
                               * RT (QTQ) R b = RT QT y
                               * RT R b = RT QT y
                               * (RT)-1 RT R b = (RT)-1 RT QT y
                               * R b = QT y 

                              * *

                              Given Q and R, the last equation is solved by back-substitution.

                              * * @version $Id: OLSMultipleLinearRegression.java 1416643 2012-12-03 19:37:14Z tn $ * @since 2.0 */ public class OLSMultipleLinearRegression extends AbstractMultipleLinearRegression { /** Cached QR decomposition of X matrix */ private QRDecomposition qr = null; /** * Loads model x and y sample data, overriding any previous sample. * * Computes and caches QR decomposition of the X matrix. * @param y the [n,1] array representing the y sample * @param x the [n,k] array representing the x sample * @throws MathIllegalArgumentException if the x and y array data are not * compatible for the regression */ public void newSampleData(double[] y, double[][] x) throws MathIllegalArgumentException { validateSampleData(x, y); newYSampleData(y); newXSampleData(x); } /** * {@inheritDoc} *

                              This implementation computes and caches the QR decomposition of the X matrix.

                              */ @Override public void newSampleData(double[] data, int nobs, int nvars) { super.newSampleData(data, nobs, nvars); qr = new QRDecomposition(getX()); } /** *

                              Compute the "hat" matrix. *

                              *

                              The hat matrix is defined in terms of the design matrix X * by X(XTX)-1XT *

                              *

                              The implementation here uses the QR decomposition to compute the * hat matrix as Q IpQT where Ip is the * p-dimensional identity matrix augmented by 0's. This computational * formula is from "The Hat Matrix in Regression and ANOVA", * David C. Hoaglin and Roy E. Welsch, * The American Statistician, Vol. 32, No. 1 (Feb., 1978), pp. 17-22. *

                              *

                              Data for the model must have been successfully loaded using one of * the {@code newSampleData} methods before invoking this method; otherwise * a {@code NullPointerException} will be thrown.

                              * * @return the hat matrix */ public RealMatrix calculateHat() { // Create augmented identity matrix RealMatrix Q = qr.getQ(); final int p = qr.getR().getColumnDimension(); final int n = Q.getColumnDimension(); // No try-catch or advertised NotStrictlyPositiveException - NPE above if n < 3 Array2DRowRealMatrix augI = new Array2DRowRealMatrix(n, n); double[][] augIData = augI.getDataRef(); for (int i = 0; i < n; i++) { for (int j =0; j < n; j++) { if (i == j && i < p) { augIData[i][j] = 1d; } else { augIData[i][j] = 0d; } } } // Compute and return Hat matrix // No DME advertised - args valid if we get here return Q.multiply(augI).multiply(Q.transpose()); } /** *

                              Returns the sum of squared deviations of Y from its mean.

                              * *

                              If the model has no intercept term, 0 is used for the * mean of Y - i.e., what is returned is the sum of the squared Y values.

                              * *

                              The value returned by this method is the SSTO value used in * the {@link #calculateRSquared() R-squared} computation.

                              * * @return SSTO - the total sum of squares * @throws MathIllegalArgumentException if the sample has not been set or does * not contain at least 3 observations * @see #isNoIntercept() * @since 2.2 */ public double calculateTotalSumOfSquares() throws MathIllegalArgumentException { if (isNoIntercept()) { return StatUtils.sumSq(getY().toArray()); } else { return new SecondMoment().evaluate(getY().toArray()); } } /** * Returns the sum of squared residuals. * * @return residual sum of squares * @since 2.2 */ public double calculateResidualSumOfSquares() { final RealVector residuals = calculateResiduals(); // No advertised DME, args are valid return residuals.dotProduct(residuals); } /** * Returns the R-Squared statistic, defined by the formula
                                   * R2 = 1 - SSR / SSTO
                                   * 
                              * where SSR is the {@link #calculateResidualSumOfSquares() sum of squared residuals} * and SSTO is the {@link #calculateTotalSumOfSquares() total sum of squares} * * @return R-square statistic * @throws MathIllegalArgumentException if the sample has not been set or does * not contain at least 3 observations * @since 2.2 */ public double calculateRSquared() throws MathIllegalArgumentException { return 1 - calculateResidualSumOfSquares() / calculateTotalSumOfSquares(); } /** *

                              Returns the adjusted R-squared statistic, defined by the formula

                                   * R2adj = 1 - [SSR (n - 1)] / [SSTO (n - p)]
                                   * 
                              * where SSR is the {@link #calculateResidualSumOfSquares() sum of squared residuals}, * SSTO is the {@link #calculateTotalSumOfSquares() total sum of squares}, n is the number * of observations and p is the number of parameters estimated (including the intercept).

                              * *

                              If the regression is estimated without an intercept term, what is returned is

                                   *  1 - (1 - {@link #calculateRSquared()}) * (n / (n - p)) 
                                   * 

                              * * @return adjusted R-Squared statistic * @throws MathIllegalArgumentException if the sample has not been set or does * not contain at least 3 observations * @see #isNoIntercept() * @since 2.2 */ public double calculateAdjustedRSquared() throws MathIllegalArgumentException { final double n = getX().getRowDimension(); if (isNoIntercept()) { return 1 - (1 - calculateRSquared()) * (n / (n - getX().getColumnDimension())); } else { return 1 - (calculateResidualSumOfSquares() * (n - 1)) / (calculateTotalSumOfSquares() * (n - getX().getColumnDimension())); } } /** * {@inheritDoc} *

                              This implementation computes and caches the QR decomposition of the X matrix * once it is successfully loaded.

                              */ @Override protected void newXSampleData(double[][] x) { super.newXSampleData(x); qr = new QRDecomposition(getX()); } /** * Calculates the regression coefficients using OLS. * *

                              Data for the model must have been successfully loaded using one of * the {@code newSampleData} methods before invoking this method; otherwise * a {@code NullPointerException} will be thrown.

                              * * @return beta */ @Override protected RealVector calculateBeta() { return qr.getSolver().solve(getY()); } /** *

                              Calculates the variance-covariance matrix of the regression parameters. *

                              *

                              Var(b) = (XTX)-1 *

                              *

                              Uses QR decomposition to reduce (XTX)-1 * to (RTR)-1, with only the top p rows of * R included, where p = the length of the beta vector.

                              * *

                              Data for the model must have been successfully loaded using one of * the {@code newSampleData} methods before invoking this method; otherwise * a {@code NullPointerException} will be thrown.

                              * * @return The beta variance-covariance matrix */ @Override protected RealMatrix calculateBetaVariance() { int p = getX().getColumnDimension(); RealMatrix Raug = qr.getR().getSubMatrix(0, p - 1 , 0, p - 1); RealMatrix Rinv = new LUDecomposition(Raug).getSolver().getInverse(); return Rinv.multiply(Rinv.transpose()); } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/stat/regression/SimpleRegression.java100644 1750 1750 100331 12126627717 32055 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.stat.regression; import java.io.Serializable; import org.apache.commons.math3.exception.OutOfRangeException; import org.apache.commons.math3.distribution.TDistribution; import org.apache.commons.math3.exception.MathIllegalArgumentException; import org.apache.commons.math3.exception.NoDataException; import org.apache.commons.math3.exception.util.LocalizedFormats; import org.apache.commons.math3.util.FastMath; import org.apache.commons.math3.util.Precision; /** * Estimates an ordinary least squares regression model * with one independent variable. *

                              * y = intercept + slope * x

                              *

                              * Standard errors for intercept and slope are * available as well as ANOVA, r-square and Pearson's r statistics.

                              *

                              * Observations (x,y pairs) can be added to the model one at a time or they * can be provided in a 2-dimensional array. The observations are not stored * in memory, so there is no limit to the number of observations that can be * added to the model.

                              *

                              * Usage Notes:

                                *
                              • When there are fewer than two observations in the model, or when * there is no variation in the x values (i.e. all x values are the same) * all statistics return NaN. At least two observations with * different x coordinates are required to estimate a bivariate regression * model. *
                              • *
                              • Getters for the statistics always compute values based on the current * set of observations -- i.e., you can get statistics, then add more data * and get updated statistics without using a new instance. There is no * "compute" method that updates all statistics. Each of the getters performs * the necessary computations to return the requested statistic. *
                              • *
                              • The intercept term may be suppressed by passing {@code false} to * the {@link #SimpleRegression(boolean)} constructor. When the * {@code hasIntercept} property is false, the model is estimated without a * constant term and {@link #getIntercept()} returns {@code 0}.
                              • *

                              * * @version $Id: SimpleRegression.java 1416643 2012-12-03 19:37:14Z tn $ */ public class SimpleRegression implements Serializable, UpdatingMultipleLinearRegression { /** Serializable version identifier */ private static final long serialVersionUID = -3004689053607543335L; /** sum of x values */ private double sumX = 0d; /** total variation in x (sum of squared deviations from xbar) */ private double sumXX = 0d; /** sum of y values */ private double sumY = 0d; /** total variation in y (sum of squared deviations from ybar) */ private double sumYY = 0d; /** sum of products */ private double sumXY = 0d; /** number of observations */ private long n = 0; /** mean of accumulated x values, used in updating formulas */ private double xbar = 0; /** mean of accumulated y values, used in updating formulas */ private double ybar = 0; /** include an intercept or not */ private final boolean hasIntercept; // ---------------------Public methods-------------------------------------- /** * Create an empty SimpleRegression instance */ public SimpleRegression() { this(true); } /** * Create a SimpleRegression instance, specifying whether or not to estimate * an intercept. * *

                              Use {@code false} to estimate a model with no intercept. When the * {@code hasIntercept} property is false, the model is estimated without a * constant term and {@link #getIntercept()} returns {@code 0}.

                              * * @param includeIntercept whether or not to include an intercept term in * the regression model */ public SimpleRegression(boolean includeIntercept) { super(); hasIntercept = includeIntercept; } /** * Adds the observation (x,y) to the regression data set. *

                              * Uses updating formulas for means and sums of squares defined in * "Algorithms for Computing the Sample Variance: Analysis and * Recommendations", Chan, T.F., Golub, G.H., and LeVeque, R.J. * 1983, American Statistician, vol. 37, pp. 242-247, referenced in * Weisberg, S. "Applied Linear Regression". 2nd Ed. 1985.

                              * * * @param x independent variable value * @param y dependent variable value */ public void addData(final double x,final double y) { if (n == 0) { xbar = x; ybar = y; } else { if( hasIntercept ){ final double fact1 = 1.0 + n; final double fact2 = n / (1.0 + n); final double dx = x - xbar; final double dy = y - ybar; sumXX += dx * dx * fact2; sumYY += dy * dy * fact2; sumXY += dx * dy * fact2; xbar += dx / fact1; ybar += dy / fact1; } } if( !hasIntercept ){ sumXX += x * x ; sumYY += y * y ; sumXY += x * y ; } sumX += x; sumY += y; n++; } /** * Removes the observation (x,y) from the regression data set. *

                              * Mirrors the addData method. This method permits the use of * SimpleRegression instances in streaming mode where the regression * is applied to a sliding "window" of observations, however the caller is * responsible for maintaining the set of observations in the window.

                              * * The method has no effect if there are no points of data (i.e. n=0) * * @param x independent variable value * @param y dependent variable value */ public void removeData(final double x,final double y) { if (n > 0) { if (hasIntercept) { final double fact1 = n - 1.0; final double fact2 = n / (n - 1.0); final double dx = x - xbar; final double dy = y - ybar; sumXX -= dx * dx * fact2; sumYY -= dy * dy * fact2; sumXY -= dx * dy * fact2; xbar -= dx / fact1; ybar -= dy / fact1; } else { final double fact1 = n - 1.0; sumXX -= x * x; sumYY -= y * y; sumXY -= x * y; xbar -= x / fact1; ybar -= y / fact1; } sumX -= x; sumY -= y; n--; } } /** * Adds the observations represented by the elements in * data. *

                              * (data[0][0],data[0][1]) will be the first observation, then * (data[1][0],data[1][1]), etc.

                              *

                              * This method does not replace data that has already been added. The * observations represented by data are added to the existing * dataset.

                              *

                              * To replace all data, use clear() before adding the new * data.

                              * * @param data array of observations to be added * @throws ModelSpecificationException if the length of {@code data[i]} is not * greater than or equal to 2 */ public void addData(final double[][] data) throws ModelSpecificationException { for (int i = 0; i < data.length; i++) { if( data[i].length < 2 ){ throw new ModelSpecificationException(LocalizedFormats.INVALID_REGRESSION_OBSERVATION, data[i].length, 2); } addData(data[i][0], data[i][1]); } } /** * Adds one observation to the regression model. * * @param x the independent variables which form the design matrix * @param y the dependent or response variable * @throws ModelSpecificationException if the length of {@code x} does not equal * the number of independent variables in the model */ public void addObservation(final double[] x,final double y) throws ModelSpecificationException { if( x == null || x.length == 0 ){ throw new ModelSpecificationException(LocalizedFormats.INVALID_REGRESSION_OBSERVATION,x!=null?x.length:0, 1); } addData( x[0], y ); } /** * Adds a series of observations to the regression model. The lengths of * x and y must be the same and x must be rectangular. * * @param x a series of observations on the independent variables * @param y a series of observations on the dependent variable * The length of x and y must be the same * @throws ModelSpecificationException if {@code x} is not rectangular, does not match * the length of {@code y} or does not contain sufficient data to estimate the model */ public void addObservations(final double[][] x,final double[] y) throws ModelSpecificationException { if ((x == null) || (y == null) || (x.length != y.length)) { throw new ModelSpecificationException( LocalizedFormats.DIMENSIONS_MISMATCH_SIMPLE, (x == null) ? 0 : x.length, (y == null) ? 0 : y.length); } boolean obsOk=true; for( int i = 0 ; i < x.length; i++){ if( x[i] == null || x[i].length == 0 ){ obsOk = false; } } if( !obsOk ){ throw new ModelSpecificationException( LocalizedFormats.NOT_ENOUGH_DATA_FOR_NUMBER_OF_PREDICTORS, 0, 1); } for( int i = 0 ; i < x.length ; i++){ addData( x[i][0], y[i] ); } } /** * Removes observations represented by the elements in data. *

                              * If the array is larger than the current n, only the first n elements are * processed. This method permits the use of SimpleRegression instances in * streaming mode where the regression is applied to a sliding "window" of * observations, however the caller is responsible for maintaining the set * of observations in the window.

                              *

                              * To remove all data, use clear().

                              * * @param data array of observations to be removed */ public void removeData(double[][] data) { for (int i = 0; i < data.length && n > 0; i++) { removeData(data[i][0], data[i][1]); } } /** * Clears all data from the model. */ public void clear() { sumX = 0d; sumXX = 0d; sumY = 0d; sumYY = 0d; sumXY = 0d; n = 0; } /** * Returns the number of observations that have been added to the model. * * @return n number of observations that have been added. */ public long getN() { return n; } /** * Returns the "predicted" y value associated with the * supplied x value, based on the data that has been * added to the model when this method is activated. *

                              * predict(x) = intercept + slope * x

                              *

                              * Preconditions:

                                *
                              • At least two observations (with at least two different x values) * must have been added before invoking this method. If this method is * invoked before a model can be estimated, Double,NaN is * returned. *

                              * * @param x input x value * @return predicted y value */ public double predict(final double x) { final double b1 = getSlope(); if (hasIntercept) { return getIntercept(b1) + b1 * x; } return b1 * x; } /** * Returns the intercept of the estimated regression line, if * {@link #hasIntercept()} is true; otherwise 0. *

                              * The least squares estimate of the intercept is computed using the * normal equations. * The intercept is sometimes denoted b0.

                              *

                              * Preconditions:

                                *
                              • At least two observations (with at least two different x values) * must have been added before invoking this method. If this method is * invoked before a model can be estimated, Double,NaN is * returned. *

                              * * @return the intercept of the regression line if the model includes an * intercept; 0 otherwise * @see #SimpleRegression(boolean) */ public double getIntercept() { return hasIntercept ? getIntercept(getSlope()) : 0.0; } /** * Returns true if the model includes an intercept term. * * @return true if the regression includes an intercept; false otherwise * @see #SimpleRegression(boolean) */ public boolean hasIntercept() { return hasIntercept; } /** * Returns the slope of the estimated regression line. *

                              * The least squares estimate of the slope is computed using the * normal equations. * The slope is sometimes denoted b1.

                              *

                              * Preconditions:

                                *
                              • At least two observations (with at least two different x values) * must have been added before invoking this method. If this method is * invoked before a model can be estimated, Double.NaN is * returned. *

                              * * @return the slope of the regression line */ public double getSlope() { if (n < 2) { return Double.NaN; //not enough data } if (FastMath.abs(sumXX) < 10 * Double.MIN_VALUE) { return Double.NaN; //not enough variation in x } return sumXY / sumXX; } /** * Returns the * sum of squared errors (SSE) associated with the regression * model. *

                              * The sum is computed using the computational formula

                              *

                              * SSE = SYY - (SXY * SXY / SXX)

                              *

                              * where SYY is the sum of the squared deviations of the y * values about their mean, SXX is similarly defined and * SXY is the sum of the products of x and y mean deviations. *

                              * The sums are accumulated using the updating algorithm referenced in * {@link #addData}.

                              *

                              * The return value is constrained to be non-negative - i.e., if due to * rounding errors the computational formula returns a negative result, * 0 is returned.

                              *

                              * Preconditions:

                                *
                              • At least two observations (with at least two different x values) * must have been added before invoking this method. If this method is * invoked before a model can be estimated, Double,NaN is * returned. *

                              * * @return sum of squared errors associated with the regression model */ public double getSumSquaredErrors() { return FastMath.max(0d, sumYY - sumXY * sumXY / sumXX); } /** * Returns the sum of squared deviations of the y values about their mean. *

                              * This is defined as SSTO * here.

                              *

                              * If n < 2, this returns Double.NaN.

                              * * @return sum of squared deviations of y values */ public double getTotalSumSquares() { if (n < 2) { return Double.NaN; } return sumYY; } /** * Returns the sum of squared deviations of the x values about their mean. * * If n < 2, this returns Double.NaN.

                              * * @return sum of squared deviations of x values */ public double getXSumSquares() { if (n < 2) { return Double.NaN; } return sumXX; } /** * Returns the sum of crossproducts, xi*yi. * * @return sum of cross products */ public double getSumOfCrossProducts() { return sumXY; } /** * Returns the sum of squared deviations of the predicted y values about * their mean (which equals the mean of y). *

                              * This is usually abbreviated SSR or SSM. It is defined as SSM * here

                              *

                              * Preconditions:

                                *
                              • At least two observations (with at least two different x values) * must have been added before invoking this method. If this method is * invoked before a model can be estimated, Double.NaN is * returned. *

                              * * @return sum of squared deviations of predicted y values */ public double getRegressionSumSquares() { return getRegressionSumSquares(getSlope()); } /** * Returns the sum of squared errors divided by the degrees of freedom, * usually abbreviated MSE. *

                              * If there are fewer than three data pairs in the model, * or if there is no variation in x, this returns * Double.NaN.

                              * * @return sum of squared deviations of y values */ public double getMeanSquareError() { if (n < 3) { return Double.NaN; } return hasIntercept ? (getSumSquaredErrors() / (n - 2)) : (getSumSquaredErrors() / (n - 1)); } /** * Returns * Pearson's product moment correlation coefficient, * usually denoted r. *

                              * Preconditions:

                                *
                              • At least two observations (with at least two different x values) * must have been added before invoking this method. If this method is * invoked before a model can be estimated, Double,NaN is * returned. *

                              * * @return Pearson's r */ public double getR() { double b1 = getSlope(); double result = FastMath.sqrt(getRSquare()); if (b1 < 0) { result = -result; } return result; } /** * Returns the * coefficient of determination, * usually denoted r-square. *

                              * Preconditions:

                                *
                              • At least two observations (with at least two different x values) * must have been added before invoking this method. If this method is * invoked before a model can be estimated, Double,NaN is * returned. *

                              * * @return r-square */ public double getRSquare() { double ssto = getTotalSumSquares(); return (ssto - getSumSquaredErrors()) / ssto; } /** * Returns the * standard error of the intercept estimate, * usually denoted s(b0). *

                              * If there are fewer that three observations in the * model, or if there is no variation in x, this returns * Double.NaN.

                              Additionally, a Double.NaN is * returned when the intercept is constrained to be zero * * @return standard error associated with intercept estimate */ public double getInterceptStdErr() { if( !hasIntercept ){ return Double.NaN; } return FastMath.sqrt( getMeanSquareError() * ((1d / n) + (xbar * xbar) / sumXX)); } /** * Returns the standard * error of the slope estimate, * usually denoted s(b1). *

                              * If there are fewer that three data pairs in the model, * or if there is no variation in x, this returns Double.NaN. *

                              * * @return standard error associated with slope estimate */ public double getSlopeStdErr() { return FastMath.sqrt(getMeanSquareError() / sumXX); } /** * Returns the half-width of a 95% confidence interval for the slope * estimate. *

                              * The 95% confidence interval is

                              *

                              * (getSlope() - getSlopeConfidenceInterval(), * getSlope() + getSlopeConfidenceInterval())

                              *

                              * If there are fewer that three observations in the * model, or if there is no variation in x, this returns * Double.NaN.

                              *

                              * Usage Note:
                              * The validity of this statistic depends on the assumption that the * observations included in the model are drawn from a * * Bivariate Normal Distribution.

                              * * @return half-width of 95% confidence interval for the slope estimate * @throws OutOfRangeException if the confidence interval can not be computed. */ public double getSlopeConfidenceInterval() throws OutOfRangeException { return getSlopeConfidenceInterval(0.05d); } /** * Returns the half-width of a (100-100*alpha)% confidence interval for * the slope estimate. *

                              * The (100-100*alpha)% confidence interval is

                              *

                              * (getSlope() - getSlopeConfidenceInterval(), * getSlope() + getSlopeConfidenceInterval())

                              *

                              * To request, for example, a 99% confidence interval, use * alpha = .01

                              *

                              * Usage Note:
                              * The validity of this statistic depends on the assumption that the * observations included in the model are drawn from a * * Bivariate Normal Distribution.

                              *

                              * Preconditions:

                                *
                              • If there are fewer that three observations in the * model, or if there is no variation in x, this returns * Double.NaN. *
                              • *
                              • (0 < alpha < 1); otherwise an * OutOfRangeException is thrown. *

                              * * @param alpha the desired significance level * @return half-width of 95% confidence interval for the slope estimate * @throws OutOfRangeException if the confidence interval can not be computed. */ public double getSlopeConfidenceInterval(final double alpha) throws OutOfRangeException { if (n < 3) { return Double.NaN; } if (alpha >= 1 || alpha <= 0) { throw new OutOfRangeException(LocalizedFormats.SIGNIFICANCE_LEVEL, alpha, 0, 1); } // No advertised NotStrictlyPositiveException here - will return NaN above TDistribution distribution = new TDistribution(n - 2); return getSlopeStdErr() * distribution.inverseCumulativeProbability(1d - alpha / 2d); } /** * Returns the significance level of the slope (equiv) correlation. *

                              * Specifically, the returned value is the smallest alpha * such that the slope confidence interval with significance level * equal to alpha does not include 0. * On regression output, this is often denoted Prob(|t| > 0) *

                              * Usage Note:
                              * The validity of this statistic depends on the assumption that the * observations included in the model are drawn from a * * Bivariate Normal Distribution.

                              *

                              * If there are fewer that three observations in the * model, or if there is no variation in x, this returns * Double.NaN.

                              * * @return significance level for slope/correlation * @throws org.apache.commons.math3.exception.MaxCountExceededException * if the significance level can not be computed. */ public double getSignificance() { if (n < 3) { return Double.NaN; } // No advertised NotStrictlyPositiveException here - will return NaN above TDistribution distribution = new TDistribution(n - 2); return 2d * (1.0 - distribution.cumulativeProbability( FastMath.abs(getSlope()) / getSlopeStdErr())); } // ---------------------Private methods----------------------------------- /** * Returns the intercept of the estimated regression line, given the slope. *

                              * Will return NaN if slope is NaN.

                              * * @param slope current slope * @return the intercept of the regression line */ private double getIntercept(final double slope) { if( hasIntercept){ return (sumY - slope * sumX) / n; } return 0.0; } /** * Computes SSR from b1. * * @param slope regression slope estimate * @return sum of squared deviations of predicted y values */ private double getRegressionSumSquares(final double slope) { return slope * slope * sumXX; } /** * Performs a regression on data present in buffers and outputs a RegressionResults object. * *

                              If there are fewer than 3 observations in the model and {@code hasIntercept} is true * a {@code NoDataException} is thrown. If there is no intercept term, the model must * contain at least 2 observations.

                              * * @return RegressionResults acts as a container of regression output * @throws ModelSpecificationException if the model is not correctly specified * @throws NoDataException if there is not sufficient data in the model to * estimate the regression parameters */ public RegressionResults regress() throws ModelSpecificationException, NoDataException { if (hasIntercept) { if( n < 3 ){ throw new NoDataException(LocalizedFormats.NOT_ENOUGH_DATA_REGRESSION); } if( FastMath.abs( sumXX ) > Precision.SAFE_MIN ){ final double[] params = new double[]{ getIntercept(), getSlope() }; final double mse = getMeanSquareError(); final double _syy = sumYY + sumY * sumY / n; final double[] vcv = new double[]{ mse * (xbar *xbar /sumXX + 1.0 / n), -xbar*mse/sumXX, mse/sumXX }; return new RegressionResults( params, new double[][]{vcv}, true, n, 2, sumY, _syy, getSumSquaredErrors(),true,false); }else{ final double[] params = new double[]{ sumY / n, Double.NaN }; //final double mse = getMeanSquareError(); final double[] vcv = new double[]{ ybar / (n - 1.0), Double.NaN, Double.NaN }; return new RegressionResults( params, new double[][]{vcv}, true, n, 1, sumY, sumYY, getSumSquaredErrors(),true,false); } }else{ if (n < 2) { throw new NoDataException(LocalizedFormats.NOT_ENOUGH_DATA_REGRESSION); } if( !Double.isNaN(sumXX) ){ final double[] vcv = new double[]{ getMeanSquareError() / sumXX }; final double[] params = new double[]{ sumXY/sumXX }; return new RegressionResults( params, new double[][]{vcv}, true, n, 1, sumY, sumYY, getSumSquaredErrors(),false,false); }else{ final double[] vcv = new double[]{Double.NaN }; final double[] params = new double[]{ Double.NaN }; return new RegressionResults( params, new double[][]{vcv}, true, n, 1, Double.NaN, Double.NaN, Double.NaN,false,false); } } } /** * Performs a regression on data present in buffers including only regressors * indexed in variablesToInclude and outputs a RegressionResults object * @param variablesToInclude an array of indices of regressors to include * @return RegressionResults acts as a container of regression output * @throws MathIllegalArgumentException if the variablesToInclude array is null or zero length * @throws OutOfRangeException if a requested variable is not present in model */ public RegressionResults regress(int[] variablesToInclude) throws MathIllegalArgumentException{ if( variablesToInclude == null || variablesToInclude.length == 0){ throw new MathIllegalArgumentException(LocalizedFormats.ARRAY_ZERO_LENGTH_OR_NULL_NOT_ALLOWED); } if( variablesToInclude.length > 2 || (variablesToInclude.length > 1 && !hasIntercept) ){ throw new ModelSpecificationException( LocalizedFormats.ARRAY_SIZE_EXCEEDS_MAX_VARIABLES, (variablesToInclude.length > 1 && !hasIntercept) ? 1 : 2); } if( hasIntercept ){ if( variablesToInclude.length == 2 ){ if( variablesToInclude[0] == 1 ){ throw new ModelSpecificationException(LocalizedFormats.NOT_INCREASING_SEQUENCE); }else if( variablesToInclude[0] != 0 ){ throw new OutOfRangeException( variablesToInclude[0], 0,1 ); } if( variablesToInclude[1] != 1){ throw new OutOfRangeException( variablesToInclude[0], 0,1 ); } return regress(); }else{ if( variablesToInclude[0] != 1 && variablesToInclude[0] != 0 ){ throw new OutOfRangeException( variablesToInclude[0],0,1 ); } final double _mean = sumY * sumY / n; final double _syy = sumYY + _mean; if( variablesToInclude[0] == 0 ){ //just the mean final double[] vcv = new double[]{ sumYY/(((n-1)*n)) }; final double[] params = new double[]{ ybar }; return new RegressionResults( params, new double[][]{vcv}, true, n, 1, sumY, _syy+_mean, sumYY,true,false); }else if( variablesToInclude[0] == 1){ //final double _syy = sumYY + sumY * sumY / ((double) n); final double _sxx = sumXX + sumX * sumX / n; final double _sxy = sumXY + sumX * sumY / n; final double _sse = FastMath.max(0d, _syy - _sxy * _sxy / _sxx); final double _mse = _sse/((n-1)); if( !Double.isNaN(_sxx) ){ final double[] vcv = new double[]{ _mse / _sxx }; final double[] params = new double[]{ _sxy/_sxx }; return new RegressionResults( params, new double[][]{vcv}, true, n, 1, sumY, _syy, _sse,false,false); }else{ final double[] vcv = new double[]{Double.NaN }; final double[] params = new double[]{ Double.NaN }; return new RegressionResults( params, new double[][]{vcv}, true, n, 1, Double.NaN, Double.NaN, Double.NaN,false,false); } } } }else{ if( variablesToInclude[0] != 0 ){ throw new OutOfRangeException(variablesToInclude[0],0,0); } return regress(); } return null; } } ././@LongLink100644 0 0 156 12126630647 10264 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/stat/regression/ModelSpecificationException.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/stat/regression/ModelSpecificationExcep100644 1750 1750 3153 12126627717 32335 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.stat.regression; import org.apache.commons.math3.exception.MathIllegalArgumentException; import org.apache.commons.math3.exception.util.Localizable; /** * Exception thrown when a regression model is not correctly specified. * * @since 3.0 * @version $Id: ModelSpecificationException.java 1244107 2012-02-14 16:17:55Z erans $ */ public class ModelSpecificationException extends MathIllegalArgumentException { /** Serializable version Id. */ private static final long serialVersionUID = 4206514456095401070L; /** * @param pattern message pattern describing the specification error. * * @param args arguments. */ public ModelSpecificationException(Localizable pattern, Object ... args) { super(pattern, args); } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/stat/inference/package-info.java100644 1750 1750 1675 12126627717 30640 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ /** * * Classes providing hypothesis testing and confidence interval * construction. * */ package org.apache.commons.math3.stat.inference; commons-math3-3.2-src/src/main/java/org/apache/commons/math3/stat/inference/TTest.java100644 1750 1750 150361 12126627717 27414 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.stat.inference; import org.apache.commons.math3.distribution.TDistribution; import org.apache.commons.math3.exception.DimensionMismatchException; import org.apache.commons.math3.exception.MathIllegalArgumentException; import org.apache.commons.math3.exception.MaxCountExceededException; import org.apache.commons.math3.exception.NoDataException; import org.apache.commons.math3.exception.NotStrictlyPositiveException; import org.apache.commons.math3.exception.NullArgumentException; import org.apache.commons.math3.exception.NumberIsTooSmallException; import org.apache.commons.math3.exception.OutOfRangeException; import org.apache.commons.math3.exception.util.LocalizedFormats; import org.apache.commons.math3.stat.StatUtils; import org.apache.commons.math3.stat.descriptive.StatisticalSummary; import org.apache.commons.math3.util.FastMath; /** * An implementation for Student's t-tests. *

                              * Tests can be:

                                *
                              • One-sample or two-sample
                              • *
                              • One-sided or two-sided
                              • *
                              • Paired or unpaired (for two-sample tests)
                              • *
                              • Homoscedastic (equal variance assumption) or heteroscedastic * (for two sample tests)
                              • *
                              • Fixed significance level (boolean-valued) or returning p-values. *

                              *

                              * Test statistics are available for all tests. Methods including "Test" in * in their names perform tests, all other methods return t-statistics. Among * the "Test" methods, double-valued methods return p-values; * boolean-valued methods perform fixed significance level tests. * Significance levels are always specified as numbers between 0 and 0.5 * (e.g. tests at the 95% level use alpha=0.05).

                              *

                              * Input to tests can be either double[] arrays or * {@link StatisticalSummary} instances.

                              * Uses commons-math {@link org.apache.commons.math3.distribution.TDistribution} * implementation to estimate exact p-values.

                              * * @version $Id: TTest.java 1416643 2012-12-03 19:37:14Z tn $ */ public class TTest { /** * Computes a paired, 2-sample t-statistic based on the data in the input * arrays. The t-statistic returned is equivalent to what would be returned by * computing the one-sample t-statistic {@link #t(double, double[])}, with * mu = 0 and the sample array consisting of the (signed) * differences between corresponding entries in sample1 and * sample2. *

                              * Preconditions:

                                *
                              • The input arrays must have the same length and their common length * must be at least 2. *

                              * * @param sample1 array of sample data values * @param sample2 array of sample data values * @return t statistic * @throws NullArgumentException if the arrays are null * @throws NoDataException if the arrays are empty * @throws DimensionMismatchException if the length of the arrays is not equal * @throws NumberIsTooSmallException if the length of the arrays is < 2 */ public double pairedT(final double[] sample1, final double[] sample2) throws NullArgumentException, NoDataException, DimensionMismatchException, NumberIsTooSmallException { checkSampleData(sample1); checkSampleData(sample2); double meanDifference = StatUtils.meanDifference(sample1, sample2); return t(meanDifference, 0, StatUtils.varianceDifference(sample1, sample2, meanDifference), sample1.length); } /** * Returns the observed significance level, or * p-value, associated with a paired, two-sample, two-tailed t-test * based on the data in the input arrays. *

                              * The number returned is the smallest significance level * at which one can reject the null hypothesis that the mean of the paired * differences is 0 in favor of the two-sided alternative that the mean paired * difference is not equal to 0. For a one-sided test, divide the returned * value by 2.

                              *

                              * This test is equivalent to a one-sample t-test computed using * {@link #tTest(double, double[])} with mu = 0 and the sample * array consisting of the signed differences between corresponding elements of * sample1 and sample2.

                              *

                              * Usage Note:
                              * The validity of the p-value depends on the assumptions of the parametric * t-test procedure, as discussed * * here

                              *

                              * Preconditions:

                                *
                              • The input array lengths must be the same and their common length must * be at least 2. *

                              * * @param sample1 array of sample data values * @param sample2 array of sample data values * @return p-value for t-test * @throws NullArgumentException if the arrays are null * @throws NoDataException if the arrays are empty * @throws DimensionMismatchException if the length of the arrays is not equal * @throws NumberIsTooSmallException if the length of the arrays is < 2 * @throws MaxCountExceededException if an error occurs computing the p-value */ public double pairedTTest(final double[] sample1, final double[] sample2) throws NullArgumentException, NoDataException, DimensionMismatchException, NumberIsTooSmallException, MaxCountExceededException { double meanDifference = StatUtils.meanDifference(sample1, sample2); return tTest(meanDifference, 0, StatUtils.varianceDifference(sample1, sample2, meanDifference), sample1.length); } /** * Performs a paired t-test evaluating the null hypothesis that the * mean of the paired differences between sample1 and * sample2 is 0 in favor of the two-sided alternative that the * mean paired difference is not equal to 0, with significance level * alpha. *

                              * Returns true iff the null hypothesis can be rejected with * confidence 1 - alpha. To perform a 1-sided test, use * alpha * 2

                              *

                              * Usage Note:
                              * The validity of the test depends on the assumptions of the parametric * t-test procedure, as discussed * * here

                              *

                              * Preconditions:

                                *
                              • The input array lengths must be the same and their common length * must be at least 2. *
                              • *
                              • 0 < alpha < 0.5 *

                              * * @param sample1 array of sample data values * @param sample2 array of sample data values * @param alpha significance level of the test * @return true if the null hypothesis can be rejected with * confidence 1 - alpha * @throws NullArgumentException if the arrays are null * @throws NoDataException if the arrays are empty * @throws DimensionMismatchException if the length of the arrays is not equal * @throws NumberIsTooSmallException if the length of the arrays is < 2 * @throws OutOfRangeException if alpha is not in the range (0, 0.5] * @throws MaxCountExceededException if an error occurs computing the p-value */ public boolean pairedTTest(final double[] sample1, final double[] sample2, final double alpha) throws NullArgumentException, NoDataException, DimensionMismatchException, NumberIsTooSmallException, OutOfRangeException, MaxCountExceededException { checkSignificanceLevel(alpha); return pairedTTest(sample1, sample2) < alpha; } /** * Computes a * t statistic given observed values and a comparison constant. *

                              * This statistic can be used to perform a one sample t-test for the mean. *

                              * Preconditions:

                                *
                              • The observed array length must be at least 2. *

                              * * @param mu comparison constant * @param observed array of values * @return t statistic * @throws NullArgumentException if observed is null * @throws NumberIsTooSmallException if the length of observed is < 2 */ public double t(final double mu, final double[] observed) throws NullArgumentException, NumberIsTooSmallException { checkSampleData(observed); // No try-catch or advertised exception because args have just been checked return t(StatUtils.mean(observed), mu, StatUtils.variance(observed), observed.length); } /** * Computes a * t statistic to use in comparing the mean of the dataset described by * sampleStats to mu. *

                              * This statistic can be used to perform a one sample t-test for the mean. *

                              * Preconditions:

                                *
                              • observed.getN() ≥ 2. *

                              * * @param mu comparison constant * @param sampleStats DescriptiveStatistics holding sample summary statitstics * @return t statistic * @throws NullArgumentException if sampleStats is null * @throws NumberIsTooSmallException if the number of samples is < 2 */ public double t(final double mu, final StatisticalSummary sampleStats) throws NullArgumentException, NumberIsTooSmallException { checkSampleData(sampleStats); return t(sampleStats.getMean(), mu, sampleStats.getVariance(), sampleStats.getN()); } /** * Computes a 2-sample t statistic, under the hypothesis of equal * subpopulation variances. To compute a t-statistic without the * equal variances hypothesis, use {@link #t(double[], double[])}. *

                              * This statistic can be used to perform a (homoscedastic) two-sample * t-test to compare sample means.

                              *

                              * The t-statistic is

                              *

                              *    t = (m1 - m2) / (sqrt(1/n1 +1/n2) sqrt(var)) *

                              * where n1 is the size of first sample; * n2 is the size of second sample; * m1 is the mean of first sample; * m2 is the mean of second sample * * and var is the pooled variance estimate: *

                              * var = sqrt(((n1 - 1)var1 + (n2 - 1)var2) / ((n1-1) + (n2-1))) *

                              * with var1 the variance of the first sample and * var2 the variance of the second sample. *

                              * Preconditions:

                                *
                              • The observed array lengths must both be at least 2. *

                              * * @param sample1 array of sample data values * @param sample2 array of sample data values * @return t statistic * @throws NullArgumentException if the arrays are null * @throws NumberIsTooSmallException if the length of the arrays is < 2 */ public double homoscedasticT(final double[] sample1, final double[] sample2) throws NullArgumentException, NumberIsTooSmallException { checkSampleData(sample1); checkSampleData(sample2); // No try-catch or advertised exception because args have just been checked return homoscedasticT(StatUtils.mean(sample1), StatUtils.mean(sample2), StatUtils.variance(sample1), StatUtils.variance(sample2), sample1.length, sample2.length); } /** * Computes a 2-sample t statistic, without the hypothesis of equal * subpopulation variances. To compute a t-statistic assuming equal * variances, use {@link #homoscedasticT(double[], double[])}. *

                              * This statistic can be used to perform a two-sample t-test to compare * sample means.

                              *

                              * The t-statistic is

                              *

                              *    t = (m1 - m2) / sqrt(var1/n1 + var2/n2) *

                              * where n1 is the size of the first sample * n2 is the size of the second sample; * m1 is the mean of the first sample; * m2 is the mean of the second sample; * var1 is the variance of the first sample; * var2 is the variance of the second sample; *

                              * Preconditions:

                                *
                              • The observed array lengths must both be at least 2. *

                              * * @param sample1 array of sample data values * @param sample2 array of sample data values * @return t statistic * @throws NullArgumentException if the arrays are null * @throws NumberIsTooSmallException if the length of the arrays is < 2 */ public double t(final double[] sample1, final double[] sample2) throws NullArgumentException, NumberIsTooSmallException { checkSampleData(sample1); checkSampleData(sample2); // No try-catch or advertised exception because args have just been checked return t(StatUtils.mean(sample1), StatUtils.mean(sample2), StatUtils.variance(sample1), StatUtils.variance(sample2), sample1.length, sample2.length); } /** * Computes a 2-sample t statistic , comparing the means of the datasets * described by two {@link StatisticalSummary} instances, without the * assumption of equal subpopulation variances. Use * {@link #homoscedasticT(StatisticalSummary, StatisticalSummary)} to * compute a t-statistic under the equal variances assumption. *

                              * This statistic can be used to perform a two-sample t-test to compare * sample means.

                              *

                              * The returned t-statistic is

                              *

                              *    t = (m1 - m2) / sqrt(var1/n1 + var2/n2) *

                              * where n1 is the size of the first sample; * n2 is the size of the second sample; * m1 is the mean of the first sample; * m2 is the mean of the second sample * var1 is the variance of the first sample; * var2 is the variance of the second sample *

                              * Preconditions:

                                *
                              • The datasets described by the two Univariates must each contain * at least 2 observations. *

                              * * @param sampleStats1 StatisticalSummary describing data from the first sample * @param sampleStats2 StatisticalSummary describing data from the second sample * @return t statistic * @throws NullArgumentException if the sample statistics are null * @throws NumberIsTooSmallException if the number of samples is < 2 */ public double t(final StatisticalSummary sampleStats1, final StatisticalSummary sampleStats2) throws NullArgumentException, NumberIsTooSmallException { checkSampleData(sampleStats1); checkSampleData(sampleStats2); return t(sampleStats1.getMean(), sampleStats2.getMean(), sampleStats1.getVariance(), sampleStats2.getVariance(), sampleStats1.getN(), sampleStats2.getN()); } /** * Computes a 2-sample t statistic, comparing the means of the datasets * described by two {@link StatisticalSummary} instances, under the * assumption of equal subpopulation variances. To compute a t-statistic * without the equal variances assumption, use * {@link #t(StatisticalSummary, StatisticalSummary)}. *

                              * This statistic can be used to perform a (homoscedastic) two-sample * t-test to compare sample means.

                              *

                              * The t-statistic returned is

                              *

                              *    t = (m1 - m2) / (sqrt(1/n1 +1/n2) sqrt(var)) *

                              * where n1 is the size of first sample; * n2 is the size of second sample; * m1 is the mean of first sample; * m2 is the mean of second sample * and var is the pooled variance estimate: *

                              * var = sqrt(((n1 - 1)var1 + (n2 - 1)var2) / ((n1-1) + (n2-1))) *

                              * with var1 the variance of the first sample and * var2 the variance of the second sample. *

                              * Preconditions:

                                *
                              • The datasets described by the two Univariates must each contain * at least 2 observations. *

                              * * @param sampleStats1 StatisticalSummary describing data from the first sample * @param sampleStats2 StatisticalSummary describing data from the second sample * @return t statistic * @throws NullArgumentException if the sample statistics are null * @throws NumberIsTooSmallException if the number of samples is < 2 */ public double homoscedasticT(final StatisticalSummary sampleStats1, final StatisticalSummary sampleStats2) throws NullArgumentException, NumberIsTooSmallException { checkSampleData(sampleStats1); checkSampleData(sampleStats2); return homoscedasticT(sampleStats1.getMean(), sampleStats2.getMean(), sampleStats1.getVariance(), sampleStats2.getVariance(), sampleStats1.getN(), sampleStats2.getN()); } /** * Returns the observed significance level, or * p-value, associated with a one-sample, two-tailed t-test * comparing the mean of the input array with the constant mu. *

                              * The number returned is the smallest significance level * at which one can reject the null hypothesis that the mean equals * mu in favor of the two-sided alternative that the mean * is different from mu. For a one-sided test, divide the * returned value by 2.

                              *

                              * Usage Note:
                              * The validity of the test depends on the assumptions of the parametric * t-test procedure, as discussed * here *

                              * Preconditions:

                                *
                              • The observed array length must be at least 2. *

                              * * @param mu constant value to compare sample mean against * @param sample array of sample data values * @return p-value * @throws NullArgumentException if the sample array is null * @throws NumberIsTooSmallException if the length of the array is < 2 * @throws MaxCountExceededException if an error occurs computing the p-value */ public double tTest(final double mu, final double[] sample) throws NullArgumentException, NumberIsTooSmallException, MaxCountExceededException { checkSampleData(sample); // No try-catch or advertised exception because args have just been checked return tTest(StatUtils.mean(sample), mu, StatUtils.variance(sample), sample.length); } /** * Performs a * two-sided t-test evaluating the null hypothesis that the mean of the population from * which sample is drawn equals mu. *

                              * Returns true iff the null hypothesis can be * rejected with confidence 1 - alpha. To * perform a 1-sided test, use alpha * 2

                              *

                              * Examples:

                                *
                              1. To test the (2-sided) hypothesis sample mean = mu at * the 95% level, use
                                tTest(mu, sample, 0.05) *
                              2. *
                              3. To test the (one-sided) hypothesis sample mean < mu * at the 99% level, first verify that the measured sample mean is less * than mu and then use *
                                tTest(mu, sample, 0.02) *

                              *

                              * Usage Note:
                              * The validity of the test depends on the assumptions of the one-sample * parametric t-test procedure, as discussed * here *

                              * Preconditions:

                                *
                              • The observed array length must be at least 2. *

                              * * @param mu constant value to compare sample mean against * @param sample array of sample data values * @param alpha significance level of the test * @return p-value * @throws NullArgumentException if the sample array is null * @throws NumberIsTooSmallException if the length of the array is < 2 * @throws OutOfRangeException if alpha is not in the range (0, 0.5] * @throws MaxCountExceededException if an error computing the p-value */ public boolean tTest(final double mu, final double[] sample, final double alpha) throws NullArgumentException, NumberIsTooSmallException, OutOfRangeException, MaxCountExceededException { checkSignificanceLevel(alpha); return tTest(mu, sample) < alpha; } /** * Returns the observed significance level, or * p-value, associated with a one-sample, two-tailed t-test * comparing the mean of the dataset described by sampleStats * with the constant mu. *

                              * The number returned is the smallest significance level * at which one can reject the null hypothesis that the mean equals * mu in favor of the two-sided alternative that the mean * is different from mu. For a one-sided test, divide the * returned value by 2.

                              *

                              * Usage Note:
                              * The validity of the test depends on the assumptions of the parametric * t-test procedure, as discussed * * here

                              *

                              * Preconditions:

                                *
                              • The sample must contain at least 2 observations. *

                              * * @param mu constant value to compare sample mean against * @param sampleStats StatisticalSummary describing sample data * @return p-value * @throws NullArgumentException if sampleStats is null * @throws NumberIsTooSmallException if the number of samples is < 2 * @throws MaxCountExceededException if an error occurs computing the p-value */ public double tTest(final double mu, final StatisticalSummary sampleStats) throws NullArgumentException, NumberIsTooSmallException, MaxCountExceededException { checkSampleData(sampleStats); return tTest(sampleStats.getMean(), mu, sampleStats.getVariance(), sampleStats.getN()); } /** * Performs a * two-sided t-test evaluating the null hypothesis that the mean of the * population from which the dataset described by stats is * drawn equals mu. *

                              * Returns true iff the null hypothesis can be rejected with * confidence 1 - alpha. To perform a 1-sided test, use * alpha * 2.

                              *

                              * Examples:

                                *
                              1. To test the (2-sided) hypothesis sample mean = mu at * the 95% level, use
                                tTest(mu, sampleStats, 0.05) *
                              2. *
                              3. To test the (one-sided) hypothesis sample mean < mu * at the 99% level, first verify that the measured sample mean is less * than mu and then use *
                                tTest(mu, sampleStats, 0.02) *

                              *

                              * Usage Note:
                              * The validity of the test depends on the assumptions of the one-sample * parametric t-test procedure, as discussed * here *

                              * Preconditions:

                                *
                              • The sample must include at least 2 observations. *

                              * * @param mu constant value to compare sample mean against * @param sampleStats StatisticalSummary describing sample data values * @param alpha significance level of the test * @return p-value * @throws NullArgumentException if sampleStats is null * @throws NumberIsTooSmallException if the number of samples is < 2 * @throws OutOfRangeException if alpha is not in the range (0, 0.5] * @throws MaxCountExceededException if an error occurs computing the p-value */ public boolean tTest(final double mu, final StatisticalSummary sampleStats, final double alpha) throws NullArgumentException, NumberIsTooSmallException, OutOfRangeException, MaxCountExceededException { checkSignificanceLevel(alpha); return tTest(mu, sampleStats) < alpha; } /** * Returns the observed significance level, or * p-value, associated with a two-sample, two-tailed t-test * comparing the means of the input arrays. *

                              * The number returned is the smallest significance level * at which one can reject the null hypothesis that the two means are * equal in favor of the two-sided alternative that they are different. * For a one-sided test, divide the returned value by 2.

                              *

                              * The test does not assume that the underlying popuation variances are * equal and it uses approximated degrees of freedom computed from the * sample data to compute the p-value. The t-statistic used is as defined in * {@link #t(double[], double[])} and the Welch-Satterthwaite approximation * to the degrees of freedom is used, * as described * * here. To perform the test under the assumption of equal subpopulation * variances, use {@link #homoscedasticTTest(double[], double[])}.

                              *

                              * Usage Note:
                              * The validity of the p-value depends on the assumptions of the parametric * t-test procedure, as discussed * * here

                              *

                              * Preconditions:

                                *
                              • The observed array lengths must both be at least 2. *

                              * * @param sample1 array of sample data values * @param sample2 array of sample data values * @return p-value for t-test * @throws NullArgumentException if the arrays are null * @throws NumberIsTooSmallException if the length of the arrays is < 2 * @throws MaxCountExceededException if an error occurs computing the p-value */ public double tTest(final double[] sample1, final double[] sample2) throws NullArgumentException, NumberIsTooSmallException, MaxCountExceededException { checkSampleData(sample1); checkSampleData(sample2); // No try-catch or advertised exception because args have just been checked return tTest(StatUtils.mean(sample1), StatUtils.mean(sample2), StatUtils.variance(sample1), StatUtils.variance(sample2), sample1.length, sample2.length); } /** * Returns the observed significance level, or * p-value, associated with a two-sample, two-tailed t-test * comparing the means of the input arrays, under the assumption that * the two samples are drawn from subpopulations with equal variances. * To perform the test without the equal variances assumption, use * {@link #tTest(double[], double[])}.

                              *

                              * The number returned is the smallest significance level * at which one can reject the null hypothesis that the two means are * equal in favor of the two-sided alternative that they are different. * For a one-sided test, divide the returned value by 2.

                              *

                              * A pooled variance estimate is used to compute the t-statistic. See * {@link #homoscedasticT(double[], double[])}. The sum of the sample sizes * minus 2 is used as the degrees of freedom.

                              *

                              * Usage Note:
                              * The validity of the p-value depends on the assumptions of the parametric * t-test procedure, as discussed * * here

                              *

                              * Preconditions:

                                *
                              • The observed array lengths must both be at least 2. *

                              * * @param sample1 array of sample data values * @param sample2 array of sample data values * @return p-value for t-test * @throws NullArgumentException if the arrays are null * @throws NumberIsTooSmallException if the length of the arrays is < 2 * @throws MaxCountExceededException if an error occurs computing the p-value */ public double homoscedasticTTest(final double[] sample1, final double[] sample2) throws NullArgumentException, NumberIsTooSmallException, MaxCountExceededException { checkSampleData(sample1); checkSampleData(sample2); // No try-catch or advertised exception because args have just been checked return homoscedasticTTest(StatUtils.mean(sample1), StatUtils.mean(sample2), StatUtils.variance(sample1), StatUtils.variance(sample2), sample1.length, sample2.length); } /** * Performs a * * two-sided t-test evaluating the null hypothesis that sample1 * and sample2 are drawn from populations with the same mean, * with significance level alpha. This test does not assume * that the subpopulation variances are equal. To perform the test assuming * equal variances, use * {@link #homoscedasticTTest(double[], double[], double)}. *

                              * Returns true iff the null hypothesis that the means are * equal can be rejected with confidence 1 - alpha. To * perform a 1-sided test, use alpha * 2

                              *

                              * See {@link #t(double[], double[])} for the formula used to compute the * t-statistic. Degrees of freedom are approximated using the * * Welch-Satterthwaite approximation.

                              *

                              * Examples:

                                *
                              1. To test the (2-sided) hypothesis mean 1 = mean 2 at * the 95% level, use *
                                tTest(sample1, sample2, 0.05). *
                              2. *
                              3. To test the (one-sided) hypothesis mean 1 < mean 2 , * at the 99% level, first verify that the measured mean of sample 1 * is less than the mean of sample 2 and then use *
                                tTest(sample1, sample2, 0.02) *

                              *

                              * Usage Note:
                              * The validity of the test depends on the assumptions of the parametric * t-test procedure, as discussed * * here

                              *

                              * Preconditions:

                                *
                              • The observed array lengths must both be at least 2. *
                              • *
                              • 0 < alpha < 0.5 *

                              * * @param sample1 array of sample data values * @param sample2 array of sample data values * @param alpha significance level of the test * @return true if the null hypothesis can be rejected with * confidence 1 - alpha * @throws NullArgumentException if the arrays are null * @throws NumberIsTooSmallException if the length of the arrays is < 2 * @throws OutOfRangeException if alpha is not in the range (0, 0.5] * @throws MaxCountExceededException if an error occurs computing the p-value */ public boolean tTest(final double[] sample1, final double[] sample2, final double alpha) throws NullArgumentException, NumberIsTooSmallException, OutOfRangeException, MaxCountExceededException { checkSignificanceLevel(alpha); return tTest(sample1, sample2) < alpha; } /** * Performs a * * two-sided t-test evaluating the null hypothesis that sample1 * and sample2 are drawn from populations with the same mean, * with significance level alpha, assuming that the * subpopulation variances are equal. Use * {@link #tTest(double[], double[], double)} to perform the test without * the assumption of equal variances. *

                              * Returns true iff the null hypothesis that the means are * equal can be rejected with confidence 1 - alpha. To * perform a 1-sided test, use alpha * 2. To perform the test * without the assumption of equal subpopulation variances, use * {@link #tTest(double[], double[], double)}.

                              *

                              * A pooled variance estimate is used to compute the t-statistic. See * {@link #t(double[], double[])} for the formula. The sum of the sample * sizes minus 2 is used as the degrees of freedom.

                              *

                              * Examples:

                                *
                              1. To test the (2-sided) hypothesis mean 1 = mean 2 at * the 95% level, use
                                tTest(sample1, sample2, 0.05). *
                              2. *
                              3. To test the (one-sided) hypothesis mean 1 < mean 2, * at the 99% level, first verify that the measured mean of * sample 1 is less than the mean of sample 2 * and then use *
                                tTest(sample1, sample2, 0.02) *

                              *

                              * Usage Note:
                              * The validity of the test depends on the assumptions of the parametric * t-test procedure, as discussed * * here

                              *

                              * Preconditions:

                                *
                              • The observed array lengths must both be at least 2. *
                              • *
                              • 0 < alpha < 0.5 *

                              * * @param sample1 array of sample data values * @param sample2 array of sample data values * @param alpha significance level of the test * @return true if the null hypothesis can be rejected with * confidence 1 - alpha * @throws NullArgumentException if the arrays are null * @throws NumberIsTooSmallException if the length of the arrays is < 2 * @throws OutOfRangeException if alpha is not in the range (0, 0.5] * @throws MaxCountExceededException if an error occurs computing the p-value */ public boolean homoscedasticTTest(final double[] sample1, final double[] sample2, final double alpha) throws NullArgumentException, NumberIsTooSmallException, OutOfRangeException, MaxCountExceededException { checkSignificanceLevel(alpha); return homoscedasticTTest(sample1, sample2) < alpha; } /** * Returns the observed significance level, or * p-value, associated with a two-sample, two-tailed t-test * comparing the means of the datasets described by two StatisticalSummary * instances. *

                              * The number returned is the smallest significance level * at which one can reject the null hypothesis that the two means are * equal in favor of the two-sided alternative that they are different. * For a one-sided test, divide the returned value by 2.

                              *

                              * The test does not assume that the underlying population variances are * equal and it uses approximated degrees of freedom computed from the * sample data to compute the p-value. To perform the test assuming * equal variances, use * {@link #homoscedasticTTest(StatisticalSummary, StatisticalSummary)}.

                              *

                              * Usage Note:
                              * The validity of the p-value depends on the assumptions of the parametric * t-test procedure, as discussed * * here

                              *

                              * Preconditions:

                                *
                              • The datasets described by the two Univariates must each contain * at least 2 observations. *

                              * * @param sampleStats1 StatisticalSummary describing data from the first sample * @param sampleStats2 StatisticalSummary describing data from the second sample * @return p-value for t-test * @throws NullArgumentException if the sample statistics are null * @throws NumberIsTooSmallException if the number of samples is < 2 * @throws MaxCountExceededException if an error occurs computing the p-value */ public double tTest(final StatisticalSummary sampleStats1, final StatisticalSummary sampleStats2) throws NullArgumentException, NumberIsTooSmallException, MaxCountExceededException { checkSampleData(sampleStats1); checkSampleData(sampleStats2); return tTest(sampleStats1.getMean(), sampleStats2.getMean(), sampleStats1.getVariance(), sampleStats2.getVariance(), sampleStats1.getN(), sampleStats2.getN()); } /** * Returns the observed significance level, or * p-value, associated with a two-sample, two-tailed t-test * comparing the means of the datasets described by two StatisticalSummary * instances, under the hypothesis of equal subpopulation variances. To * perform a test without the equal variances assumption, use * {@link #tTest(StatisticalSummary, StatisticalSummary)}. *

                              * The number returned is the smallest significance level * at which one can reject the null hypothesis that the two means are * equal in favor of the two-sided alternative that they are different. * For a one-sided test, divide the returned value by 2.

                              *

                              * See {@link #homoscedasticT(double[], double[])} for the formula used to * compute the t-statistic. The sum of the sample sizes minus 2 is used as * the degrees of freedom.

                              *

                              * Usage Note:
                              * The validity of the p-value depends on the assumptions of the parametric * t-test procedure, as discussed * here *

                              * Preconditions:

                                *
                              • The datasets described by the two Univariates must each contain * at least 2 observations. *

                              * * @param sampleStats1 StatisticalSummary describing data from the first sample * @param sampleStats2 StatisticalSummary describing data from the second sample * @return p-value for t-test * @throws NullArgumentException if the sample statistics are null * @throws NumberIsTooSmallException if the number of samples is < 2 * @throws MaxCountExceededException if an error occurs computing the p-value */ public double homoscedasticTTest(final StatisticalSummary sampleStats1, final StatisticalSummary sampleStats2) throws NullArgumentException, NumberIsTooSmallException, MaxCountExceededException { checkSampleData(sampleStats1); checkSampleData(sampleStats2); return homoscedasticTTest(sampleStats1.getMean(), sampleStats2.getMean(), sampleStats1.getVariance(), sampleStats2.getVariance(), sampleStats1.getN(), sampleStats2.getN()); } /** * Performs a * * two-sided t-test evaluating the null hypothesis that * sampleStats1 and sampleStats2 describe * datasets drawn from populations with the same mean, with significance * level alpha. This test does not assume that the * subpopulation variances are equal. To perform the test under the equal * variances assumption, use * {@link #homoscedasticTTest(StatisticalSummary, StatisticalSummary)}. *

                              * Returns true iff the null hypothesis that the means are * equal can be rejected with confidence 1 - alpha. To * perform a 1-sided test, use alpha * 2

                              *

                              * See {@link #t(double[], double[])} for the formula used to compute the * t-statistic. Degrees of freedom are approximated using the * * Welch-Satterthwaite approximation.

                              *

                              * Examples:

                                *
                              1. To test the (2-sided) hypothesis mean 1 = mean 2 at * the 95%, use *
                                tTest(sampleStats1, sampleStats2, 0.05) *
                              2. *
                              3. To test the (one-sided) hypothesis mean 1 < mean 2 * at the 99% level, first verify that the measured mean of * sample 1 is less than the mean of sample 2 * and then use *
                                tTest(sampleStats1, sampleStats2, 0.02) *

                              *

                              * Usage Note:
                              * The validity of the test depends on the assumptions of the parametric * t-test procedure, as discussed * * here

                              *

                              * Preconditions:

                                *
                              • The datasets described by the two Univariates must each contain * at least 2 observations. *
                              • *
                              • 0 < alpha < 0.5 *

                              * * @param sampleStats1 StatisticalSummary describing sample data values * @param sampleStats2 StatisticalSummary describing sample data values * @param alpha significance level of the test * @return true if the null hypothesis can be rejected with * confidence 1 - alpha * @throws NullArgumentException if the sample statistics are null * @throws NumberIsTooSmallException if the number of samples is < 2 * @throws OutOfRangeException if alpha is not in the range (0, 0.5] * @throws MaxCountExceededException if an error occurs computing the p-value */ public boolean tTest(final StatisticalSummary sampleStats1, final StatisticalSummary sampleStats2, final double alpha) throws NullArgumentException, NumberIsTooSmallException, OutOfRangeException, MaxCountExceededException { checkSignificanceLevel(alpha); return tTest(sampleStats1, sampleStats2) < alpha; } //----------------------------------------------- Protected methods /** * Computes approximate degrees of freedom for 2-sample t-test. * * @param v1 first sample variance * @param v2 second sample variance * @param n1 first sample n * @param n2 second sample n * @return approximate degrees of freedom */ protected double df(double v1, double v2, double n1, double n2) { return (((v1 / n1) + (v2 / n2)) * ((v1 / n1) + (v2 / n2))) / ((v1 * v1) / (n1 * n1 * (n1 - 1d)) + (v2 * v2) / (n2 * n2 * (n2 - 1d))); } /** * Computes t test statistic for 1-sample t-test. * * @param m sample mean * @param mu constant to test against * @param v sample variance * @param n sample n * @return t test statistic */ protected double t(final double m, final double mu, final double v, final double n) { return (m - mu) / FastMath.sqrt(v / n); } /** * Computes t test statistic for 2-sample t-test. *

                              * Does not assume that subpopulation variances are equal.

                              * * @param m1 first sample mean * @param m2 second sample mean * @param v1 first sample variance * @param v2 second sample variance * @param n1 first sample n * @param n2 second sample n * @return t test statistic */ protected double t(final double m1, final double m2, final double v1, final double v2, final double n1, final double n2) { return (m1 - m2) / FastMath.sqrt((v1 / n1) + (v2 / n2)); } /** * Computes t test statistic for 2-sample t-test under the hypothesis * of equal subpopulation variances. * * @param m1 first sample mean * @param m2 second sample mean * @param v1 first sample variance * @param v2 second sample variance * @param n1 first sample n * @param n2 second sample n * @return t test statistic */ protected double homoscedasticT(final double m1, final double m2, final double v1, final double v2, final double n1, final double n2) { final double pooledVariance = ((n1 - 1) * v1 + (n2 -1) * v2 ) / (n1 + n2 - 2); return (m1 - m2) / FastMath.sqrt(pooledVariance * (1d / n1 + 1d / n2)); } /** * Computes p-value for 2-sided, 1-sample t-test. * * @param m sample mean * @param mu constant to test against * @param v sample variance * @param n sample n * @return p-value * @throws MaxCountExceededException if an error occurs computing the p-value * @throws MathIllegalArgumentException if n is not greater than 1 */ protected double tTest(final double m, final double mu, final double v, final double n) throws MaxCountExceededException, MathIllegalArgumentException { double t = FastMath.abs(t(m, mu, v, n)); TDistribution distribution = new TDistribution(n - 1); return 2.0 * distribution.cumulativeProbability(-t); } /** * Computes p-value for 2-sided, 2-sample t-test. *

                              * Does not assume subpopulation variances are equal. Degrees of freedom * are estimated from the data.

                              * * @param m1 first sample mean * @param m2 second sample mean * @param v1 first sample variance * @param v2 second sample variance * @param n1 first sample n * @param n2 second sample n * @return p-value * @throws MaxCountExceededException if an error occurs computing the p-value * @throws NotStrictlyPositiveException if the estimated degrees of freedom is not * strictly positive */ protected double tTest(final double m1, final double m2, final double v1, final double v2, final double n1, final double n2) throws MaxCountExceededException, NotStrictlyPositiveException { final double t = FastMath.abs(t(m1, m2, v1, v2, n1, n2)); final double degreesOfFreedom = df(v1, v2, n1, n2); TDistribution distribution = new TDistribution(degreesOfFreedom); return 2.0 * distribution.cumulativeProbability(-t); } /** * Computes p-value for 2-sided, 2-sample t-test, under the assumption * of equal subpopulation variances. *

                              * The sum of the sample sizes minus 2 is used as degrees of freedom.

                              * * @param m1 first sample mean * @param m2 second sample mean * @param v1 first sample variance * @param v2 second sample variance * @param n1 first sample n * @param n2 second sample n * @return p-value * @throws MaxCountExceededException if an error occurs computing the p-value * @throws NotStrictlyPositiveException if the estimated degrees of freedom is not * strictly positive */ protected double homoscedasticTTest(double m1, double m2, double v1, double v2, double n1, double n2) throws MaxCountExceededException, NotStrictlyPositiveException { final double t = FastMath.abs(homoscedasticT(m1, m2, v1, v2, n1, n2)); final double degreesOfFreedom = n1 + n2 - 2; TDistribution distribution = new TDistribution(degreesOfFreedom); return 2.0 * distribution.cumulativeProbability(-t); } /** * Check significance level. * * @param alpha significance level * @throws OutOfRangeException if the significance level is out of bounds. */ private void checkSignificanceLevel(final double alpha) throws OutOfRangeException { if (alpha <= 0 || alpha > 0.5) { throw new OutOfRangeException(LocalizedFormats.SIGNIFICANCE_LEVEL, alpha, 0.0, 0.5); } } /** * Check sample data. * * @param data Sample data. * @throws NullArgumentException if {@code data} is {@code null}. * @throws NumberIsTooSmallException if there is not enough sample data. */ private void checkSampleData(final double[] data) throws NullArgumentException, NumberIsTooSmallException { if (data == null) { throw new NullArgumentException(); } if (data.length < 2) { throw new NumberIsTooSmallException( LocalizedFormats.INSUFFICIENT_DATA_FOR_T_STATISTIC, data.length, 2, true); } } /** * Check sample data. * * @param stat Statistical summary. * @throws NullArgumentException if {@code data} is {@code null}. * @throws NumberIsTooSmallException if there is not enough sample data. */ private void checkSampleData(final StatisticalSummary stat) throws NullArgumentException, NumberIsTooSmallException { if (stat == null) { throw new NullArgumentException(); } if (stat.getN() < 2) { throw new NumberIsTooSmallException( LocalizedFormats.INSUFFICIENT_DATA_FOR_T_STATISTIC, stat.getN(), 2, true); } } } ././@LongLink100644 0 0 150 12126630647 10256 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/stat/inference/WilcoxonSignedRankTest.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/stat/inference/WilcoxonSignedRankTest.j100644 1750 1750 27226 12126627717 32254 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.stat.inference; import org.apache.commons.math3.distribution.NormalDistribution; import org.apache.commons.math3.exception.ConvergenceException; import org.apache.commons.math3.exception.DimensionMismatchException; import org.apache.commons.math3.exception.MaxCountExceededException; import org.apache.commons.math3.exception.NoDataException; import org.apache.commons.math3.exception.NullArgumentException; import org.apache.commons.math3.exception.NumberIsTooLargeException; import org.apache.commons.math3.stat.ranking.NaNStrategy; import org.apache.commons.math3.stat.ranking.NaturalRanking; import org.apache.commons.math3.stat.ranking.TiesStrategy; import org.apache.commons.math3.util.FastMath; /** * An implementation of the Wilcoxon signed-rank test. * * @version $Id: WilcoxonSignedRankTest.java 1416643 2012-12-03 19:37:14Z tn $ */ public class WilcoxonSignedRankTest { /** Ranking algorithm. */ private NaturalRanking naturalRanking; /** * Create a test instance where NaN's are left in place and ties get * the average of applicable ranks. Use this unless you are very sure * of what you are doing. */ public WilcoxonSignedRankTest() { naturalRanking = new NaturalRanking(NaNStrategy.FIXED, TiesStrategy.AVERAGE); } /** * Create a test instance using the given strategies for NaN's and ties. * Only use this if you are sure of what you are doing. * * @param nanStrategy * specifies the strategy that should be used for Double.NaN's * @param tiesStrategy * specifies the strategy that should be used for ties */ public WilcoxonSignedRankTest(final NaNStrategy nanStrategy, final TiesStrategy tiesStrategy) { naturalRanking = new NaturalRanking(nanStrategy, tiesStrategy); } /** * Ensures that the provided arrays fulfills the assumptions. * * @param x first sample * @param y second sample * @throws NullArgumentException if {@code x} or {@code y} are {@code null}. * @throws NoDataException if {@code x} or {@code y} are zero-length. * @throws DimensionMismatchException if {@code x} and {@code y} do not * have the same length. */ private void ensureDataConformance(final double[] x, final double[] y) throws NullArgumentException, NoDataException, DimensionMismatchException { if (x == null || y == null) { throw new NullArgumentException(); } if (x.length == 0 || y.length == 0) { throw new NoDataException(); } if (y.length != x.length) { throw new DimensionMismatchException(y.length, x.length); } } /** * Calculates y[i] - x[i] for all i * * @param x first sample * @param y second sample * @return z = y - x */ private double[] calculateDifferences(final double[] x, final double[] y) { final double[] z = new double[x.length]; for (int i = 0; i < x.length; ++i) { z[i] = y[i] - x[i]; } return z; } /** * Calculates |z[i]| for all i * * @param z sample * @return |z| * @throws NullArgumentException if {@code z} is {@code null} * @throws NoDataException if {@code z} is zero-length. */ private double[] calculateAbsoluteDifferences(final double[] z) throws NullArgumentException, NoDataException { if (z == null) { throw new NullArgumentException(); } if (z.length == 0) { throw new NoDataException(); } final double[] zAbs = new double[z.length]; for (int i = 0; i < z.length; ++i) { zAbs[i] = FastMath.abs(z[i]); } return zAbs; } /** * Computes the * Wilcoxon signed ranked statistic comparing mean for two related * samples or repeated measurements on a single sample. *

                              * This statistic can be used to perform a Wilcoxon signed ranked test * evaluating the null hypothesis that the two related samples or repeated * measurements on a single sample has equal mean. *

                              *

                              * Let Xi denote the i'th individual of the first sample and * Yi the related i'th individual in the second sample. Let * Zi = Yi - Xi. *

                              *

                              * Preconditions: *

                                *
                              • The differences Zi must be independent.
                              • *
                              • Each Zi comes from a continuous population (they must be * identical) and is symmetric about a common median.
                              • *
                              • The values that Xi and Yi represent are * ordered, so the comparisons greater than, less than, and equal to are * meaningful.
                              • *
                              *

                              * * @param x the first sample * @param y the second sample * @return wilcoxonSignedRank statistic (the larger of W+ and W-) * @throws NullArgumentException if {@code x} or {@code y} are {@code null}. * @throws NoDataException if {@code x} or {@code y} are zero-length. * @throws DimensionMismatchException if {@code x} and {@code y} do not * have the same length. */ public double wilcoxonSignedRank(final double[] x, final double[] y) throws NullArgumentException, NoDataException, DimensionMismatchException { ensureDataConformance(x, y); // throws IllegalArgumentException if x and y are not correctly // specified final double[] z = calculateDifferences(x, y); final double[] zAbs = calculateAbsoluteDifferences(z); final double[] ranks = naturalRanking.rank(zAbs); double Wplus = 0; for (int i = 0; i < z.length; ++i) { if (z[i] > 0) { Wplus += ranks[i]; } } final int N = x.length; final double Wminus = (((double) (N * (N + 1))) / 2.0) - Wplus; return FastMath.max(Wplus, Wminus); } /** * Algorithm inspired by * http://www.fon.hum.uva.nl/Service/Statistics/Signed_Rank_Algorihms.html#C * by Rob van Son, Institute of Phonetic Sciences & IFOTT, * University of Amsterdam * * @param Wmax largest Wilcoxon signed rank value * @param N number of subjects (corresponding to x.length) * @return two-sided exact p-value */ private double calculateExactPValue(final double Wmax, final int N) { // Total number of outcomes (equal to 2^N but a lot faster) final int m = 1 << N; int largerRankSums = 0; for (int i = 0; i < m; ++i) { int rankSum = 0; // Generate all possible rank sums for (int j = 0; j < N; ++j) { // (i >> j) & 1 extract i's j-th bit from the right if (((i >> j) & 1) == 1) { rankSum += j + 1; } } if (rankSum >= Wmax) { ++largerRankSums; } } /* * largerRankSums / m gives the one-sided p-value, so it's multiplied * with 2 to get the two-sided p-value */ return 2 * ((double) largerRankSums) / ((double) m); } /** * @param Wmin smallest Wilcoxon signed rank value * @param N number of subjects (corresponding to x.length) * @return two-sided asymptotic p-value */ private double calculateAsymptoticPValue(final double Wmin, final int N) { final double ES = (double) (N * (N + 1)) / 4.0; /* Same as (but saves computations): * final double VarW = ((double) (N * (N + 1) * (2*N + 1))) / 24; */ final double VarS = ES * ((double) (2 * N + 1) / 6.0); // - 0.5 is a continuity correction final double z = (Wmin - ES - 0.5) / FastMath.sqrt(VarS); // No try-catch or advertised exception because args are valid final NormalDistribution standardNormal = new NormalDistribution(0, 1); return 2*standardNormal.cumulativeProbability(z); } /** * Returns the observed significance level, or * p-value, associated with a * Wilcoxon signed ranked statistic comparing mean for two related * samples or repeated measurements on a single sample. *

                              * Let Xi denote the i'th individual of the first sample and * Yi the related i'th individual in the second sample. Let * Zi = Yi - Xi. *

                              *

                              * Preconditions: *

                                *
                              • The differences Zi must be independent.
                              • *
                              • Each Zi comes from a continuous population (they must be * identical) and is symmetric about a common median.
                              • *
                              • The values that Xi and Yi represent are * ordered, so the comparisons greater than, less than, and equal to are * meaningful.
                              • *
                              *

                              * * @param x the first sample * @param y the second sample * @param exactPValue * if the exact p-value is wanted (only works for x.length <= 30, * if true and x.length > 30, this is ignored because * calculations may take too long) * @return p-value * @throws NullArgumentException if {@code x} or {@code y} are {@code null}. * @throws NoDataException if {@code x} or {@code y} are zero-length. * @throws DimensionMismatchException if {@code x} and {@code y} do not * have the same length. * @throws NumberIsTooLargeException if {@code exactPValue} is {@code true} * and {@code x.length} > 30 * @throws ConvergenceException if the p-value can not be computed due to * a convergence error * @throws MaxCountExceededException if the maximum number of iterations * is exceeded */ public double wilcoxonSignedRankTest(final double[] x, final double[] y, final boolean exactPValue) throws NullArgumentException, NoDataException, DimensionMismatchException, NumberIsTooLargeException, ConvergenceException, MaxCountExceededException { ensureDataConformance(x, y); final int N = x.length; final double Wmax = wilcoxonSignedRank(x, y); if (exactPValue && N > 30) { throw new NumberIsTooLargeException(N, 30, true); } if (exactPValue) { return calculateExactPValue(Wmax, N); } else { final double Wmin = ( (double)(N*(N+1)) / 2.0 ) - Wmax; return calculateAsymptoticPValue(Wmin, N); } } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/stat/inference/TestUtils.java100644 1750 1750 45754 12126627717 30302 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.stat.inference; import java.util.Collection; import org.apache.commons.math3.exception.ConvergenceException; import org.apache.commons.math3.exception.DimensionMismatchException; import org.apache.commons.math3.exception.MaxCountExceededException; import org.apache.commons.math3.exception.NoDataException; import org.apache.commons.math3.exception.NotPositiveException; import org.apache.commons.math3.exception.NotStrictlyPositiveException; import org.apache.commons.math3.exception.NullArgumentException; import org.apache.commons.math3.exception.NumberIsTooSmallException; import org.apache.commons.math3.exception.OutOfRangeException; import org.apache.commons.math3.exception.ZeroException; import org.apache.commons.math3.stat.descriptive.StatisticalSummary; /** * A collection of static methods to create inference test instances or to * perform inference tests. * * @since 1.1 * @version $Id: TestUtils.java 1422313 2012-12-15 18:53:41Z psteitz $ */ public class TestUtils { /** Singleton TTest instance. */ private static final TTest T_TEST = new TTest(); /** Singleton ChiSquareTest instance. */ private static final ChiSquareTest CHI_SQUARE_TEST = new ChiSquareTest(); /** Singleton OneWayAnova instance. */ private static final OneWayAnova ONE_WAY_ANANOVA = new OneWayAnova(); /** Singleton G-Test instance. */ private static final GTest G_TEST = new GTest(); /** * Prevent instantiation. */ private TestUtils() { super(); } // CHECKSTYLE: stop JavadocMethodCheck /** * @see org.apache.commons.math3.stat.inference.TTest#homoscedasticT(double[], double[]) */ public static double homoscedasticT(final double[] sample1, final double[] sample2) throws NullArgumentException, NumberIsTooSmallException { return T_TEST.homoscedasticT(sample1, sample2); } /** * @see org.apache.commons.math3.stat.inference.TTest#homoscedasticT(org.apache.commons.math3.stat.descriptive.StatisticalSummary, org.apache.commons.math3.stat.descriptive.StatisticalSummary) */ public static double homoscedasticT(final StatisticalSummary sampleStats1, final StatisticalSummary sampleStats2) throws NullArgumentException, NumberIsTooSmallException { return T_TEST.homoscedasticT(sampleStats1, sampleStats2); } /** * @see org.apache.commons.math3.stat.inference.TTest#homoscedasticTTest(double[], double[], double) */ public static boolean homoscedasticTTest(final double[] sample1, final double[] sample2, final double alpha) throws NullArgumentException, NumberIsTooSmallException, OutOfRangeException, MaxCountExceededException { return T_TEST.homoscedasticTTest(sample1, sample2, alpha); } /** * @see org.apache.commons.math3.stat.inference.TTest#homoscedasticTTest(double[], double[]) */ public static double homoscedasticTTest(final double[] sample1, final double[] sample2) throws NullArgumentException, NumberIsTooSmallException, MaxCountExceededException { return T_TEST.homoscedasticTTest(sample1, sample2); } /** * @see org.apache.commons.math3.stat.inference.TTest#homoscedasticTTest(org.apache.commons.math3.stat.descriptive.StatisticalSummary, org.apache.commons.math3.stat.descriptive.StatisticalSummary) */ public static double homoscedasticTTest(final StatisticalSummary sampleStats1, final StatisticalSummary sampleStats2) throws NullArgumentException, NumberIsTooSmallException, MaxCountExceededException { return T_TEST.homoscedasticTTest(sampleStats1, sampleStats2); } /** * @see org.apache.commons.math3.stat.inference.TTest#pairedT(double[], double[]) */ public static double pairedT(final double[] sample1, final double[] sample2) throws NullArgumentException, NoDataException, DimensionMismatchException, NumberIsTooSmallException { return T_TEST.pairedT(sample1, sample2); } /** * @see org.apache.commons.math3.stat.inference.TTest#pairedTTest(double[], double[], double) */ public static boolean pairedTTest(final double[] sample1, final double[] sample2, final double alpha) throws NullArgumentException, NoDataException, DimensionMismatchException, NumberIsTooSmallException, OutOfRangeException, MaxCountExceededException { return T_TEST.pairedTTest(sample1, sample2, alpha); } /** * @see org.apache.commons.math3.stat.inference.TTest#pairedTTest(double[], double[]) */ public static double pairedTTest(final double[] sample1, final double[] sample2) throws NullArgumentException, NoDataException, DimensionMismatchException, NumberIsTooSmallException, MaxCountExceededException { return T_TEST.pairedTTest(sample1, sample2); } /** * @see org.apache.commons.math3.stat.inference.TTest#t(double, double[]) */ public static double t(final double mu, final double[] observed) throws NullArgumentException, NumberIsTooSmallException { return T_TEST.t(mu, observed); } /** * @see org.apache.commons.math3.stat.inference.TTest#t(double, org.apache.commons.math3.stat.descriptive.StatisticalSummary) */ public static double t(final double mu, final StatisticalSummary sampleStats) throws NullArgumentException, NumberIsTooSmallException { return T_TEST.t(mu, sampleStats); } /** * @see org.apache.commons.math3.stat.inference.TTest#t(double[], double[]) */ public static double t(final double[] sample1, final double[] sample2) throws NullArgumentException, NumberIsTooSmallException { return T_TEST.t(sample1, sample2); } /** * @see org.apache.commons.math3.stat.inference.TTest#t(org.apache.commons.math3.stat.descriptive.StatisticalSummary, org.apache.commons.math3.stat.descriptive.StatisticalSummary) */ public static double t(final StatisticalSummary sampleStats1, final StatisticalSummary sampleStats2) throws NullArgumentException, NumberIsTooSmallException { return T_TEST.t(sampleStats1, sampleStats2); } /** * @see org.apache.commons.math3.stat.inference.TTest#tTest(double, double[], double) */ public static boolean tTest(final double mu, final double[] sample, final double alpha) throws NullArgumentException, NumberIsTooSmallException, OutOfRangeException, MaxCountExceededException { return T_TEST.tTest(mu, sample, alpha); } /** * @see org.apache.commons.math3.stat.inference.TTest#tTest(double, double[]) */ public static double tTest(final double mu, final double[] sample) throws NullArgumentException, NumberIsTooSmallException, MaxCountExceededException { return T_TEST.tTest(mu, sample); } /** * @see org.apache.commons.math3.stat.inference.TTest#tTest(double, org.apache.commons.math3.stat.descriptive.StatisticalSummary, double) */ public static boolean tTest(final double mu, final StatisticalSummary sampleStats, final double alpha) throws NullArgumentException, NumberIsTooSmallException, OutOfRangeException, MaxCountExceededException { return T_TEST.tTest(mu, sampleStats, alpha); } /** * @see org.apache.commons.math3.stat.inference.TTest#tTest(double, org.apache.commons.math3.stat.descriptive.StatisticalSummary) */ public static double tTest(final double mu, final StatisticalSummary sampleStats) throws NullArgumentException, NumberIsTooSmallException, MaxCountExceededException { return T_TEST.tTest(mu, sampleStats); } /** * @see org.apache.commons.math3.stat.inference.TTest#tTest(double[], double[], double) */ public static boolean tTest(final double[] sample1, final double[] sample2, final double alpha) throws NullArgumentException, NumberIsTooSmallException, OutOfRangeException, MaxCountExceededException { return T_TEST.tTest(sample1, sample2, alpha); } /** * @see org.apache.commons.math3.stat.inference.TTest#tTest(double[], double[]) */ public static double tTest(final double[] sample1, final double[] sample2) throws NullArgumentException, NumberIsTooSmallException, MaxCountExceededException { return T_TEST.tTest(sample1, sample2); } /** * @see org.apache.commons.math3.stat.inference.TTest#tTest(org.apache.commons.math3.stat.descriptive.StatisticalSummary, org.apache.commons.math3.stat.descriptive.StatisticalSummary, double) */ public static boolean tTest(final StatisticalSummary sampleStats1, final StatisticalSummary sampleStats2, final double alpha) throws NullArgumentException, NumberIsTooSmallException, OutOfRangeException, MaxCountExceededException { return T_TEST.tTest(sampleStats1, sampleStats2, alpha); } /** * @see org.apache.commons.math3.stat.inference.TTest#tTest(org.apache.commons.math3.stat.descriptive.StatisticalSummary, org.apache.commons.math3.stat.descriptive.StatisticalSummary) */ public static double tTest(final StatisticalSummary sampleStats1, final StatisticalSummary sampleStats2) throws NullArgumentException, NumberIsTooSmallException, MaxCountExceededException { return T_TEST.tTest(sampleStats1, sampleStats2); } /** * @see org.apache.commons.math3.stat.inference.ChiSquareTest#chiSquare(double[], long[]) */ public static double chiSquare(final double[] expected, final long[] observed) throws NotPositiveException, NotStrictlyPositiveException, DimensionMismatchException { return CHI_SQUARE_TEST.chiSquare(expected, observed); } /** * @see org.apache.commons.math3.stat.inference.ChiSquareTest#chiSquare(long[][]) */ public static double chiSquare(final long[][] counts) throws NullArgumentException, NotPositiveException, DimensionMismatchException { return CHI_SQUARE_TEST.chiSquare(counts); } /** * @see org.apache.commons.math3.stat.inference.ChiSquareTest#chiSquareTest(double[], long[], double) */ public static boolean chiSquareTest(final double[] expected, final long[] observed, final double alpha) throws NotPositiveException, NotStrictlyPositiveException, DimensionMismatchException, OutOfRangeException, MaxCountExceededException { return CHI_SQUARE_TEST.chiSquareTest(expected, observed, alpha); } /** * @see org.apache.commons.math3.stat.inference.ChiSquareTest#chiSquareTest(double[], long[]) */ public static double chiSquareTest(final double[] expected, final long[] observed) throws NotPositiveException, NotStrictlyPositiveException, DimensionMismatchException, MaxCountExceededException { return CHI_SQUARE_TEST.chiSquareTest(expected, observed); } /** * @see org.apache.commons.math3.stat.inference.ChiSquareTest#chiSquareTest(long[][], double) */ public static boolean chiSquareTest(final long[][] counts, final double alpha) throws NullArgumentException, DimensionMismatchException, NotPositiveException, OutOfRangeException, MaxCountExceededException { return CHI_SQUARE_TEST.chiSquareTest(counts, alpha); } /** * @see org.apache.commons.math3.stat.inference.ChiSquareTest#chiSquareTest(long[][]) */ public static double chiSquareTest(final long[][] counts) throws NullArgumentException, DimensionMismatchException, NotPositiveException, MaxCountExceededException { return CHI_SQUARE_TEST.chiSquareTest(counts); } /** * @see org.apache.commons.math3.stat.inference.ChiSquareTest#chiSquareDataSetsComparison(long[], long[]) * * @since 1.2 */ public static double chiSquareDataSetsComparison(final long[] observed1, final long[] observed2) throws DimensionMismatchException, NotPositiveException, ZeroException { return CHI_SQUARE_TEST.chiSquareDataSetsComparison(observed1, observed2); } /** * @see org.apache.commons.math3.stat.inference.ChiSquareTest#chiSquareTestDataSetsComparison(long[], long[]) * * @since 1.2 */ public static double chiSquareTestDataSetsComparison(final long[] observed1, final long[] observed2) throws DimensionMismatchException, NotPositiveException, ZeroException, MaxCountExceededException { return CHI_SQUARE_TEST.chiSquareTestDataSetsComparison(observed1, observed2); } /** * @see org.apache.commons.math3.stat.inference.ChiSquareTest#chiSquareTestDataSetsComparison(long[], long[], double) * * @since 1.2 */ public static boolean chiSquareTestDataSetsComparison(final long[] observed1, final long[] observed2, final double alpha) throws DimensionMismatchException, NotPositiveException, ZeroException, OutOfRangeException, MaxCountExceededException { return CHI_SQUARE_TEST.chiSquareTestDataSetsComparison(observed1, observed2, alpha); } /** * @see org.apache.commons.math3.stat.inference.OneWayAnova#anovaFValue(Collection) * * @since 1.2 */ public static double oneWayAnovaFValue(final Collection categoryData) throws NullArgumentException, DimensionMismatchException { return ONE_WAY_ANANOVA.anovaFValue(categoryData); } /** * @see org.apache.commons.math3.stat.inference.OneWayAnova#anovaPValue(Collection) * * @since 1.2 */ public static double oneWayAnovaPValue(final Collection categoryData) throws NullArgumentException, DimensionMismatchException, ConvergenceException, MaxCountExceededException { return ONE_WAY_ANANOVA.anovaPValue(categoryData); } /** * @see org.apache.commons.math3.stat.inference.OneWayAnova#anovaTest(Collection,double) * * @since 1.2 */ public static boolean oneWayAnovaTest(final Collection categoryData, final double alpha) throws NullArgumentException, DimensionMismatchException, OutOfRangeException, ConvergenceException, MaxCountExceededException { return ONE_WAY_ANANOVA.anovaTest(categoryData, alpha); } /** * @see org.apache.commons.math3.stat.inference.GTest#g(double[], long[]) * @since 3.1 */ public static double g(final double[] expected, final long[] observed) throws NotPositiveException, NotStrictlyPositiveException, DimensionMismatchException { return G_TEST.g(expected, observed); } /** * @see org.apache.commons.math3.stat.inference.GTest#gTest( double[], long[] ) * @since 3.1 */ public static double gTest(final double[] expected, final long[] observed) throws NotPositiveException, NotStrictlyPositiveException, DimensionMismatchException, MaxCountExceededException { return G_TEST.gTest(expected, observed); } /** * @see org.apache.commons.math3.stat.inference.GTest#gTestIntrinsic(double[], long[] ) * @since 3.1 */ public static double gTestIntrinsic(final double[] expected, final long[] observed) throws NotPositiveException, NotStrictlyPositiveException, DimensionMismatchException, MaxCountExceededException { return G_TEST.gTestIntrinsic(expected, observed); } /** * @see org.apache.commons.math3.stat.inference.GTest#gTest( double[],long[],double) * @since 3.1 */ public static boolean gTest(final double[] expected, final long[] observed, final double alpha) throws NotPositiveException, NotStrictlyPositiveException, DimensionMismatchException, OutOfRangeException, MaxCountExceededException { return G_TEST.gTest(expected, observed, alpha); } /** * @see org.apache.commons.math3.stat.inference.GTest#gDataSetsComparison(long[], long[]) * @since 3.1 */ public static double gDataSetsComparison(final long[] observed1, final long[] observed2) throws DimensionMismatchException, NotPositiveException, ZeroException { return G_TEST.gDataSetsComparison(observed1, observed2); } /** * @see org.apache.commons.math3.stat.inference.GTest#rootLogLikelihoodRatio(long, long, long, long) * @since 3.1 */ public static double rootLogLikelihoodRatio(final long k11, final long k12, final long k21, final long k22) throws DimensionMismatchException, NotPositiveException, ZeroException { return G_TEST.rootLogLikelihoodRatio(k11, k12, k21, k22); } /** * @see org.apache.commons.math3.stat.inference.GTest#gTestDataSetsComparison(long[], long[]) * @since 3.1 */ public static double gTestDataSetsComparison(final long[] observed1, final long[] observed2) throws DimensionMismatchException, NotPositiveException, ZeroException, MaxCountExceededException { return G_TEST.gTestDataSetsComparison(observed1, observed2); } /** * @see org.apache.commons.math3.stat.inference.GTest#gTestDataSetsComparison(long[],long[],double) * @since 3.1 */ public static boolean gTestDataSetsComparison(final long[] observed1, final long[] observed2, final double alpha) throws DimensionMismatchException, NotPositiveException, ZeroException, OutOfRangeException, MaxCountExceededException { return G_TEST.gTestDataSetsComparison(observed1, observed2, alpha); } // CHECKSTYLE: resume JavadocMethodCheck } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/stat/inference/MannWhitneyUTest.java100644 1750 1750 21577 12126627717 31565 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.stat.inference; import org.apache.commons.math3.distribution.NormalDistribution; import org.apache.commons.math3.exception.ConvergenceException; import org.apache.commons.math3.exception.MaxCountExceededException; import org.apache.commons.math3.exception.NoDataException; import org.apache.commons.math3.exception.NullArgumentException; import org.apache.commons.math3.stat.ranking.NaNStrategy; import org.apache.commons.math3.stat.ranking.NaturalRanking; import org.apache.commons.math3.stat.ranking.TiesStrategy; import org.apache.commons.math3.util.FastMath; /** * An implementation of the Mann-Whitney U test (also called Wilcoxon rank-sum test). * * @version $Id: MannWhitneyUTest.java 1416643 2012-12-03 19:37:14Z tn $ */ public class MannWhitneyUTest { /** Ranking algorithm. */ private NaturalRanking naturalRanking; /** * Create a test instance using where NaN's are left in place and ties get * the average of applicable ranks. Use this unless you are very sure of * what you are doing. */ public MannWhitneyUTest() { naturalRanking = new NaturalRanking(NaNStrategy.FIXED, TiesStrategy.AVERAGE); } /** * Create a test instance using the given strategies for NaN's and ties. * Only use this if you are sure of what you are doing. * * @param nanStrategy * specifies the strategy that should be used for Double.NaN's * @param tiesStrategy * specifies the strategy that should be used for ties */ public MannWhitneyUTest(final NaNStrategy nanStrategy, final TiesStrategy tiesStrategy) { naturalRanking = new NaturalRanking(nanStrategy, tiesStrategy); } /** * Ensures that the provided arrays fulfills the assumptions. * * @param x first sample * @param y second sample * @throws NullArgumentException if {@code x} or {@code y} are {@code null}. * @throws NoDataException if {@code x} or {@code y} are zero-length. */ private void ensureDataConformance(final double[] x, final double[] y) throws NullArgumentException, NoDataException { if (x == null || y == null) { throw new NullArgumentException(); } if (x.length == 0 || y.length == 0) { throw new NoDataException(); } } /** Concatenate the samples into one array. * @param x first sample * @param y second sample * @return concatenated array */ private double[] concatenateSamples(final double[] x, final double[] y) { final double[] z = new double[x.length + y.length]; System.arraycopy(x, 0, z, 0, x.length); System.arraycopy(y, 0, z, x.length, y.length); return z; } /** * Computes the Mann-Whitney * U statistic comparing mean for two independent samples possibly of * different length. *

                              * This statistic can be used to perform a Mann-Whitney U test evaluating * the null hypothesis that the two independent samples has equal mean. *

                              *

                              * Let Xi denote the i'th individual of the first sample and * Yj the j'th individual in the second sample. Note that the * samples would often have different length. *

                              *

                              * Preconditions: *

                                *
                              • All observations in the two samples are independent.
                              • *
                              • The observations are at least ordinal (continuous are also ordinal).
                              • *
                              *

                              * * @param x the first sample * @param y the second sample * @return Mann-Whitney U statistic (maximum of Ux and Uy) * @throws NullArgumentException if {@code x} or {@code y} are {@code null}. * @throws NoDataException if {@code x} or {@code y} are zero-length. */ public double mannWhitneyU(final double[] x, final double[] y) throws NullArgumentException, NoDataException { ensureDataConformance(x, y); final double[] z = concatenateSamples(x, y); final double[] ranks = naturalRanking.rank(z); double sumRankX = 0; /* * The ranks for x is in the first x.length entries in ranks because x * is in the first x.length entries in z */ for (int i = 0; i < x.length; ++i) { sumRankX += ranks[i]; } /* * U1 = R1 - (n1 * (n1 + 1)) / 2 where R1 is sum of ranks for sample 1, * e.g. x, n1 is the number of observations in sample 1. */ final double U1 = sumRankX - (x.length * (x.length + 1)) / 2; /* * It can be shown that U1 + U2 = n1 * n2 */ final double U2 = x.length * y.length - U1; return FastMath.max(U1, U2); } /** * @param Umin smallest Mann-Whitney U value * @param n1 number of subjects in first sample * @param n2 number of subjects in second sample * @return two-sided asymptotic p-value * @throws ConvergenceException if the p-value can not be computed * due to a convergence error * @throws MaxCountExceededException if the maximum number of * iterations is exceeded */ private double calculateAsymptoticPValue(final double Umin, final int n1, final int n2) throws ConvergenceException, MaxCountExceededException { /* long multiplication to avoid overflow (double not used due to efficiency * and to avoid precision loss) */ final long n1n2prod = (long) n1 * n2; // http://en.wikipedia.org/wiki/Mann%E2%80%93Whitney_U#Normal_approximation final double EU = n1n2prod / 2.0; final double VarU = n1n2prod * (n1 + n2 + 1) / 12.0; final double z = (Umin - EU) / FastMath.sqrt(VarU); // No try-catch or advertised exception because args are valid final NormalDistribution standardNormal = new NormalDistribution(0, 1); return 2 * standardNormal.cumulativeProbability(z); } /** * Returns the asymptotic observed significance level, or * p-value, associated with a Mann-Whitney * U statistic comparing mean for two independent samples. *

                              * Let Xi denote the i'th individual of the first sample and * Yj the j'th individual in the second sample. Note that the * samples would often have different length. *

                              *

                              * Preconditions: *

                                *
                              • All observations in the two samples are independent.
                              • *
                              • The observations are at least ordinal (continuous are also ordinal).
                              • *
                              *

                              * Ties give rise to biased variance at the moment. See e.g. http://mlsc.lboro.ac.uk/resources/statistics/Mannwhitney.pdf.

                              * * @param x the first sample * @param y the second sample * @return asymptotic p-value * @throws NullArgumentException if {@code x} or {@code y} are {@code null}. * @throws NoDataException if {@code x} or {@code y} are zero-length. * @throws ConvergenceException if the p-value can not be computed due to a * convergence error * @throws MaxCountExceededException if the maximum number of iterations * is exceeded */ public double mannWhitneyUTest(final double[] x, final double[] y) throws NullArgumentException, NoDataException, ConvergenceException, MaxCountExceededException { ensureDataConformance(x, y); final double Umax = mannWhitneyU(x, y); /* * It can be shown that U1 + U2 = n1 * n2 */ final double Umin = x.length * y.length - Umax; return calculateAsymptoticPValue(Umin, x.length, y.length); } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/stat/inference/OneWayAnova.java100644 1750 1750 35656 12126627717 30531 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.stat.inference; import java.util.ArrayList; import java.util.Collection; import org.apache.commons.math3.distribution.FDistribution; import org.apache.commons.math3.exception.ConvergenceException; import org.apache.commons.math3.exception.DimensionMismatchException; import org.apache.commons.math3.exception.MaxCountExceededException; import org.apache.commons.math3.exception.NullArgumentException; import org.apache.commons.math3.exception.OutOfRangeException; import org.apache.commons.math3.exception.util.LocalizedFormats; import org.apache.commons.math3.stat.descriptive.SummaryStatistics; import org.apache.commons.math3.util.MathUtils; /** * Implements one-way ANOVA (analysis of variance) statistics. * *

                              Tests for differences between two or more categories of univariate data * (for example, the body mass index of accountants, lawyers, doctors and * computer programmers). When two categories are given, this is equivalent to * the {@link org.apache.commons.math3.stat.inference.TTest}. *

                              * Uses the {@link org.apache.commons.math3.distribution.FDistribution * commons-math F Distribution implementation} to estimate exact p-values.

                              *

                              This implementation is based on a description at * http://faculty.vassar.edu/lowry/ch13pt1.html

                              *
                               * Abbreviations: bg = between groups,
                               *                wg = within groups,
                               *                ss = sum squared deviations
                               * 
                              * * @since 1.2 * @version $Id: OneWayAnova.java 1462423 2013-03-29 07:25:18Z luc $ */ public class OneWayAnova { /** * Default constructor. */ public OneWayAnova() { } /** * Computes the ANOVA F-value for a collection of double[] * arrays. * *

                              Preconditions:

                                *
                              • The categoryData Collection must contain * double[] arrays.
                              • *
                              • There must be at least two double[] arrays in the * categoryData collection and each of these arrays must * contain at least two values.

                              * This implementation computes the F statistic using the definitional * formula

                                   *   F = msbg/mswg
                              * where
                                   *  msbg = between group mean square
                                   *  mswg = within group mean square
                              * are as defined * here

                              * * @param categoryData Collection of double[] * arrays each containing data for one category * @return Fvalue * @throws NullArgumentException if categoryData is null * @throws DimensionMismatchException if the length of the categoryData * array is less than 2 or a contained double[] array does not have * at least two values */ public double anovaFValue(final Collection categoryData) throws NullArgumentException, DimensionMismatchException { AnovaStats a = anovaStats(categoryData); return a.F; } /** * Computes the ANOVA P-value for a collection of double[] * arrays. * *

                              Preconditions:

                                *
                              • The categoryData Collection must contain * double[] arrays.
                              • *
                              • There must be at least two double[] arrays in the * categoryData collection and each of these arrays must * contain at least two values.

                              * This implementation uses the * {@link org.apache.commons.math3.distribution.FDistribution * commons-math F Distribution implementation} to estimate the exact * p-value, using the formula

                                   *   p = 1 - cumulativeProbability(F)
                              * where F is the F value and cumulativeProbability * is the commons-math implementation of the F distribution.

                              * * @param categoryData Collection of double[] * arrays each containing data for one category * @return Pvalue * @throws NullArgumentException if categoryData is null * @throws DimensionMismatchException if the length of the categoryData * array is less than 2 or a contained double[] array does not have * at least two values * @throws ConvergenceException if the p-value can not be computed due to a convergence error * @throws MaxCountExceededException if the maximum number of iterations is exceeded */ public double anovaPValue(final Collection categoryData) throws NullArgumentException, DimensionMismatchException, ConvergenceException, MaxCountExceededException { AnovaStats a = anovaStats(categoryData); // No try-catch or advertised exception because args are valid FDistribution fdist = new FDistribution(a.dfbg, a.dfwg); return 1.0 - fdist.cumulativeProbability(a.F); } /** * Computes the ANOVA P-value for a collection of {@link SummaryStatistics}. * *

                              Preconditions:

                                *
                              • The categoryData Collection must contain * {@link SummaryStatistics}.
                              • *
                              • There must be at least two {@link SummaryStatistics} in the * categoryData collection and each of these statistics must * contain at least two values.

                              * This implementation uses the * {@link org.apache.commons.math3.distribution.FDistribution * commons-math F Distribution implementation} to estimate the exact * p-value, using the formula

                                   *   p = 1 - cumulativeProbability(F)
                              * where F is the F value and cumulativeProbability * is the commons-math implementation of the F distribution.

                              * * @param categoryData Collection of {@link SummaryStatistics} * each containing data for one category * @param allowOneElementData if true, allow computation for one catagory * only or for one data element per category * @return Pvalue * @throws NullArgumentException if categoryData is null * @throws DimensionMismatchException if the length of the categoryData * array is less than 2 or a contained {@link SummaryStatistics} does not have * at least two values * @throws ConvergenceException if the p-value can not be computed due to a convergence error * @throws MaxCountExceededException if the maximum number of iterations is exceeded * @since 3.2 */ public double anovaPValue(final Collection categoryData, final boolean allowOneElementData) throws NullArgumentException, DimensionMismatchException, ConvergenceException, MaxCountExceededException { final AnovaStats a = anovaStats(categoryData, allowOneElementData); final FDistribution fdist = new FDistribution(a.dfbg, a.dfwg); return 1.0 - fdist.cumulativeProbability(a.F); } /** * This method calls the method that actually does the calculations (except * P-value). * * @param categoryData * Collection of double[] arrays each * containing data for one category * @return computed AnovaStats * @throws NullArgumentException * if categoryData is null * @throws DimensionMismatchException * if the length of the categoryData array is less * than 2 or a contained double[] array does not * contain at least two values */ private AnovaStats anovaStats(final Collection categoryData) throws NullArgumentException, DimensionMismatchException { MathUtils.checkNotNull(categoryData); final Collection categoryDataSummaryStatistics = new ArrayList(categoryData.size()); // convert arrays to SummaryStatistics for (final double[] data : categoryData) { final SummaryStatistics dataSummaryStatistics = new SummaryStatistics(); categoryDataSummaryStatistics.add(dataSummaryStatistics); for (final double val : data) { dataSummaryStatistics.addValue(val); } } return anovaStats(categoryDataSummaryStatistics, false); } /** * Performs an ANOVA test, evaluating the null hypothesis that there * is no difference among the means of the data categories. * *

                              Preconditions:

                                *
                              • The categoryData Collection must contain * double[] arrays.
                              • *
                              • There must be at least two double[] arrays in the * categoryData collection and each of these arrays must * contain at least two values.
                              • *
                              • alpha must be strictly greater than 0 and less than or equal to 0.5. *

                              * This implementation uses the * {@link org.apache.commons.math3.distribution.FDistribution * commons-math F Distribution implementation} to estimate the exact * p-value, using the formula

                                   *   p = 1 - cumulativeProbability(F)
                              * where F is the F value and cumulativeProbability * is the commons-math implementation of the F distribution.

                              *

                              True is returned iff the estimated p-value is less than alpha.

                              * * @param categoryData Collection of double[] * arrays each containing data for one category * @param alpha significance level of the test * @return true if the null hypothesis can be rejected with * confidence 1 - alpha * @throws NullArgumentException if categoryData is null * @throws DimensionMismatchException if the length of the categoryData * array is less than 2 or a contained double[] array does not have * at least two values * @throws OutOfRangeException if alpha is not in the range (0, 0.5] * @throws ConvergenceException if the p-value can not be computed due to a convergence error * @throws MaxCountExceededException if the maximum number of iterations is exceeded */ public boolean anovaTest(final Collection categoryData, final double alpha) throws NullArgumentException, DimensionMismatchException, OutOfRangeException, ConvergenceException, MaxCountExceededException { if ((alpha <= 0) || (alpha > 0.5)) { throw new OutOfRangeException( LocalizedFormats.OUT_OF_BOUND_SIGNIFICANCE_LEVEL, alpha, 0, 0.5); } return anovaPValue(categoryData) < alpha; } /** * This method actually does the calculations (except P-value). * * @param categoryData Collection of double[] * arrays each containing data for one category * @param allowOneElementData if true, allow computation for one catagory * only or for one data element per category * @return computed AnovaStats * @throws NullArgumentException if categoryData is null * @throws DimensionMismatchException if allowOneElementData is false and the number of * categories is less than 2 or a contained SummaryStatistics does not contain * at least two values */ private AnovaStats anovaStats(final Collection categoryData, final boolean allowOneElementData) throws NullArgumentException, DimensionMismatchException { MathUtils.checkNotNull(categoryData); if (!allowOneElementData) { // check if we have enough categories if (categoryData.size() < 2) { throw new DimensionMismatchException(LocalizedFormats.TWO_OR_MORE_CATEGORIES_REQUIRED, categoryData.size(), 2); } // check if each category has enough data for (final SummaryStatistics array : categoryData) { if (array.getN() <= 1) { throw new DimensionMismatchException(LocalizedFormats.TWO_OR_MORE_VALUES_IN_CATEGORY_REQUIRED, (int) array.getN(), 2); } } } int dfwg = 0; double sswg = 0; double totsum = 0; double totsumsq = 0; int totnum = 0; for (final SummaryStatistics data : categoryData) { final double sum = data.getSum(); final double sumsq = data.getSumsq(); final int num = (int) data.getN(); totnum += num; totsum += sum; totsumsq += sumsq; dfwg += num - 1; final double ss = sumsq - ((sum * sum) / num); sswg += ss; } final double sst = totsumsq - ((totsum * totsum) / totnum); final double ssbg = sst - sswg; final int dfbg = categoryData.size() - 1; final double msbg = ssbg / dfbg; final double mswg = sswg / dfwg; final double F = msbg / mswg; return new AnovaStats(dfbg, dfwg, F); } /** Convenience class to pass dfbg,dfwg,F values around within OneWayAnova. No get/set methods provided. */ private static class AnovaStats { /** Degrees of freedom in numerator (between groups). */ private final int dfbg; /** Degrees of freedom in denominator (within groups). */ private final int dfwg; /** Statistic. */ private final double F; /** * Constructor * @param dfbg degrees of freedom in numerator (between groups) * @param dfwg degrees of freedom in denominator (within groups) * @param F statistic */ private AnovaStats(int dfbg, int dfwg, double F) { this.dfbg = dfbg; this.dfwg = dfwg; this.F = F; } } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/stat/inference/GTest.java100644 1750 1750 57542 12126627717 27366 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.stat.inference; import org.apache.commons.math3.distribution.ChiSquaredDistribution; import org.apache.commons.math3.exception.DimensionMismatchException; import org.apache.commons.math3.exception.MaxCountExceededException; import org.apache.commons.math3.exception.NotPositiveException; import org.apache.commons.math3.exception.NotStrictlyPositiveException; import org.apache.commons.math3.exception.OutOfRangeException; import org.apache.commons.math3.exception.ZeroException; import org.apache.commons.math3.exception.util.LocalizedFormats; import org.apache.commons.math3.util.FastMath; import org.apache.commons.math3.util.MathArrays; /** * Implements G Test * statistics. * *

                              This is known in statistical genetics as the McDonald-Kreitman test. * The implementation handles both known and unknown distributions.

                              * *

                              Two samples tests can be used when the distribution is unknown a priori * but provided by one sample, or when the hypothesis under test is that the two * samples come from the same underlying distribution.

                              * * @version $Id: GTest.java 1416643 2012-12-03 19:37:14Z tn $ * @since 3.1 */ public class GTest { /** * Computes the G statistic * for Goodness of Fit comparing {@code observed} and {@code expected} * frequency counts. * *

                              This statistic can be used to perform a G test (Log-Likelihood Ratio * Test) evaluating the null hypothesis that the observed counts follow the * expected distribution.

                              * *

                              Preconditions:

                                *
                              • Expected counts must all be positive.
                              • *
                              • Observed counts must all be ≥ 0.
                              • *
                              • The observed and expected arrays must have the same length and their * common length must be at least 2.

                              * *

                              If any of the preconditions are not met, a * {@code MathIllegalArgumentException} is thrown.

                              * *

                              Note:This implementation rescales the * {@code expected} array if necessary to ensure that the sum of the * expected and observed counts are equal.

                              * * @param observed array of observed frequency counts * @param expected array of expected frequency counts * @return G-Test statistic * @throws NotPositiveException if {@code observed} has negative entries * @throws NotStrictlyPositiveException if {@code expected} has entries that * are not strictly positive * @throws DimensionMismatchException if the array lengths do not match or * are less than 2. */ public double g(final double[] expected, final long[] observed) throws NotPositiveException, NotStrictlyPositiveException, DimensionMismatchException { if (expected.length < 2) { throw new DimensionMismatchException(expected.length, 2); } if (expected.length != observed.length) { throw new DimensionMismatchException(expected.length, observed.length); } MathArrays.checkPositive(expected); MathArrays.checkNonNegative(observed); double sumExpected = 0d; double sumObserved = 0d; for (int i = 0; i < observed.length; i++) { sumExpected += expected[i]; sumObserved += observed[i]; } double ratio = 1d; boolean rescale = false; if (Math.abs(sumExpected - sumObserved) > 10E-6) { ratio = sumObserved / sumExpected; rescale = true; } double sum = 0d; for (int i = 0; i < observed.length; i++) { final double dev = rescale ? FastMath.log((double) observed[i] / (ratio * expected[i])) : FastMath.log((double) observed[i] / expected[i]); sum += ((double) observed[i]) * dev; } return 2d * sum; } /** * Returns the observed significance level, or p-value, * associated with a G-Test for goodness of fit comparing the * {@code observed} frequency counts to those in the {@code expected} array. * *

                              The number returned is the smallest significance level at which one * can reject the null hypothesis that the observed counts conform to the * frequency distribution described by the expected counts.

                              * *

                              The probability returned is the tail probability beyond * {@link #g(double[], long[]) g(expected, observed)} * in the ChiSquare distribution with degrees of freedom one less than the * common length of {@code expected} and {@code observed}.

                              * *

                              Preconditions:

                                *
                              • Expected counts must all be positive.
                              • *
                              • Observed counts must all be ≥ 0.
                              • *
                              • The observed and expected arrays must have the * same length and their common length must be at least 2.
                              • *

                              * *

                              If any of the preconditions are not met, a * {@code MathIllegalArgumentException} is thrown.

                              * *

                              Note:This implementation rescales the * {@code expected} array if necessary to ensure that the sum of the * expected and observed counts are equal.

                              * * @param observed array of observed frequency counts * @param expected array of expected frequency counts * @return p-value * @throws NotPositiveException if {@code observed} has negative entries * @throws NotStrictlyPositiveException if {@code expected} has entries that * are not strictly positive * @throws DimensionMismatchException if the array lengths do not match or * are less than 2. * @throws MaxCountExceededException if an error occurs computing the * p-value. */ public double gTest(final double[] expected, final long[] observed) throws NotPositiveException, NotStrictlyPositiveException, DimensionMismatchException, MaxCountExceededException { final ChiSquaredDistribution distribution = new ChiSquaredDistribution(expected.length - 1.0); return 1.0 - distribution.cumulativeProbability( g(expected, observed)); } /** * Returns the intrinsic (Hardy-Weinberg proportions) p-Value, as described * in p64-69 of McDonald, J.H. 2009. Handbook of Biological Statistics * (2nd ed.). Sparky House Publishing, Baltimore, Maryland. * *

                              The probability returned is the tail probability beyond * {@link #g(double[], long[]) g(expected, observed)} * in the ChiSquare distribution with degrees of freedom two less than the * common length of {@code expected} and {@code observed}.

                              * * @param observed array of observed frequency counts * @param expected array of expected frequency counts * @return p-value * @throws NotPositiveException if {@code observed} has negative entries * @throws NotStrictlyPositiveException {@code expected} has entries that are * not strictly positive * @throws DimensionMismatchException if the array lengths do not match or * are less than 2. * @throws MaxCountExceededException if an error occurs computing the * p-value. */ public double gTestIntrinsic(final double[] expected, final long[] observed) throws NotPositiveException, NotStrictlyPositiveException, DimensionMismatchException, MaxCountExceededException { final ChiSquaredDistribution distribution = new ChiSquaredDistribution(expected.length - 2.0); return 1.0 - distribution.cumulativeProbability( g(expected, observed)); } /** * Performs a G-Test (Log-Likelihood Ratio Test) for goodness of fit * evaluating the null hypothesis that the observed counts conform to the * frequency distribution described by the expected counts, with * significance level {@code alpha}. Returns true iff the null * hypothesis can be rejected with {@code 100 * (1 - alpha)} percent confidence. * *

                              Example:
                              To test the hypothesis that * {@code observed} follows {@code expected} at the 99% level, * use

                              * {@code gTest(expected, observed, 0.01)}

                              * *

                              Returns true iff {@link #gTest(double[], long[]) * gTestGoodnessOfFitPValue(expected, observed)} < alpha

                              * *

                              Preconditions:

                                *
                              • Expected counts must all be positive.
                              • *
                              • Observed counts must all be ≥ 0.
                              • *
                              • The observed and expected arrays must have the same length and their * common length must be at least 2. *
                              • {@code 0 < alpha < 0.5}

                              * *

                              If any of the preconditions are not met, a * {@code MathIllegalArgumentException} is thrown.

                              * *

                              Note:This implementation rescales the * {@code expected} array if necessary to ensure that the sum of the * expected and observed counts are equal.

                              * * @param observed array of observed frequency counts * @param expected array of expected frequency counts * @param alpha significance level of the test * @return true iff null hypothesis can be rejected with confidence 1 - * alpha * @throws NotPositiveException if {@code observed} has negative entries * @throws NotStrictlyPositiveException if {@code expected} has entries that * are not strictly positive * @throws DimensionMismatchException if the array lengths do not match or * are less than 2. * @throws MaxCountExceededException if an error occurs computing the * p-value. * @throws OutOfRangeException if alpha is not strictly greater than zero * and less than or equal to 0.5 */ public boolean gTest(final double[] expected, final long[] observed, final double alpha) throws NotPositiveException, NotStrictlyPositiveException, DimensionMismatchException, OutOfRangeException, MaxCountExceededException { if ((alpha <= 0) || (alpha > 0.5)) { throw new OutOfRangeException(LocalizedFormats.OUT_OF_BOUND_SIGNIFICANCE_LEVEL, alpha, 0, 0.5); } return gTest(expected, observed) < alpha; } /** * Calculates the Shannon * entropy for 2 Dimensional Matrix. The value returned is the entropy * of the vector formed by concatenating the rows (or columns) of {@code k} * to form a vector. See {@link #entropy(long[])}. * * @param k 2 Dimensional Matrix of long values (for ex. the counts of a * trials) * @return Shannon Entropy of the given Matrix * */ private double entropy(final long[][] k) { double h = 0d; double sum_k = 0d; for (int i = 0; i < k.length; i++) { for (int j = 0; j < k[i].length; j++) { sum_k += (double) k[i][j]; } } for (int i = 0; i < k.length; i++) { for (int j = 0; j < k[i].length; j++) { if (k[i][j] != 0) { final double p_ij = (double) k[i][j] / sum_k; h += p_ij * Math.log(p_ij); } } } return -h; } /** * Calculates the * Shannon entropy for a vector. The values of {@code k} are taken to be * incidence counts of the values of a random variable. What is returned is
                              * ∑pilog(pi
                              * where pi = k[i] / (sum of elements in k) * * @param k Vector (for ex. Row Sums of a trials) * @return Shannon Entropy of the given Vector * */ private double entropy(final long[] k) { double h = 0d; double sum_k = 0d; for (int i = 0; i < k.length; i++) { sum_k += (double) k[i]; } for (int i = 0; i < k.length; i++) { if (k[i] != 0) { final double p_i = (double) k[i] / sum_k; h += p_i * Math.log(p_i); } } return -h; } /** *

                              Computes a G (Log-Likelihood Ratio) two sample test statistic for * independence comparing frequency counts in * {@code observed1} and {@code observed2}. The sums of frequency * counts in the two samples are not required to be the same. The formula * used to compute the test statistic is

                              * *

                              {@code 2 * totalSum * [H(rowSums) + H(colSums) - H(k)]}

                              * *

                              where {@code H} is the * * Shannon Entropy of the random variable formed by viewing the elements * of the argument array as incidence counts;
                              * {@code k} is a matrix with rows {@code [observed1, observed2]};
                              * {@code rowSums, colSums} are the row/col sums of {@code k};
                              * and {@code totalSum} is the overall sum of all entries in {@code k}.

                              * *

                              This statistic can be used to perform a G test evaluating the null * hypothesis that both observed counts are independent

                              * *

                              Preconditions:

                                *
                              • Observed counts must be non-negative.
                              • *
                              • Observed counts for a specific bin must not both be zero.
                              • *
                              • Observed counts for a specific sample must not all be 0.
                              • *
                              • The arrays {@code observed1} and {@code observed2} must have * the same length and their common length must be at least 2.

                              * *

                              If any of the preconditions are not met, a * {@code MathIllegalArgumentException} is thrown.

                              * * @param observed1 array of observed frequency counts of the first data set * @param observed2 array of observed frequency counts of the second data * set * @return G-Test statistic * @throws DimensionMismatchException the the lengths of the arrays do not * match or their common length is less than 2 * @throws NotPositiveException if any entry in {@code observed1} or * {@code observed2} is negative * @throws ZeroException if either all counts of * {@code observed1} or {@code observed2} are zero, or if the count * at the same index is zero for both arrays. */ public double gDataSetsComparison(final long[] observed1, final long[] observed2) throws DimensionMismatchException, NotPositiveException, ZeroException { // Make sure lengths are same if (observed1.length < 2) { throw new DimensionMismatchException(observed1.length, 2); } if (observed1.length != observed2.length) { throw new DimensionMismatchException(observed1.length, observed2.length); } // Ensure non-negative counts MathArrays.checkNonNegative(observed1); MathArrays.checkNonNegative(observed2); // Compute and compare count sums long countSum1 = 0; long countSum2 = 0; // Compute and compare count sums final long[] collSums = new long[observed1.length]; final long[][] k = new long[2][observed1.length]; for (int i = 0; i < observed1.length; i++) { if (observed1[i] == 0 && observed2[i] == 0) { throw new ZeroException(LocalizedFormats.OBSERVED_COUNTS_BOTTH_ZERO_FOR_ENTRY, i); } else { countSum1 += observed1[i]; countSum2 += observed2[i]; collSums[i] = observed1[i] + observed2[i]; k[0][i] = observed1[i]; k[1][i] = observed2[i]; } } // Ensure neither sample is uniformly 0 if (countSum1 == 0 || countSum2 == 0) { throw new ZeroException(); } final long[] rowSums = {countSum1, countSum2}; final double sum = (double) countSum1 + (double) countSum2; return 2 * sum * (entropy(rowSums) + entropy(collSums) - entropy(k)); } /** * Calculates the root log-likelihood ratio for 2 state Datasets. See * {@link #gDataSetsComparison(long[], long[] )}. * *

                              Given two events A and B, let k11 be the number of times both events * occur, k12 the incidence of B without A, k21 the count of A without B, * and k22 the number of times neither A nor B occurs. What is returned * by this method is

                              * *

                              {@code (sgn) sqrt(gValueDataSetsComparison({k11, k12}, {k21, k22})}

                              * *

                              where {@code sgn} is -1 if {@code k11 / (k11 + k12) < k21 / (k21 + k22))};
                              * 1 otherwise.

                              * *

                              Signed root LLR has two advantages over the basic LLR: a) it is positive * where k11 is bigger than expected, negative where it is lower b) if there is * no difference it is asymptotically normally distributed. This allows one * to talk about "number of standard deviations" which is a more common frame * of reference than the chi^2 distribution.

                              * * @param k11 number of times the two events occurred together (AB) * @param k12 number of times the second event occurred WITHOUT the * first event (notA,B) * @param k21 number of times the first event occurred WITHOUT the * second event (A, notB) * @param k22 number of times something else occurred (i.e. was neither * of these events (notA, notB) * @return root log-likelihood ratio * */ public double rootLogLikelihoodRatio(final long k11, long k12, final long k21, final long k22) { final double llr = gDataSetsComparison( new long[]{k11, k12}, new long[]{k21, k22}); double sqrt = FastMath.sqrt(llr); if ((double) k11 / (k11 + k12) < (double) k21 / (k21 + k22)) { sqrt = -sqrt; } return sqrt; } /** *

                              Returns the observed significance level, or * p-value, associated with a G-Value (Log-Likelihood Ratio) for two * sample test comparing bin frequency counts in {@code observed1} and * {@code observed2}.

                              * *

                              The number returned is the smallest significance level at which one * can reject the null hypothesis that the observed counts conform to the * same distribution.

                              * *

                              See {@link #gTest(double[], long[])} for details * on how the p-value is computed. The degrees of of freedom used to * perform the test is one less than the common length of the input observed * count arrays.

                              * *

                              Preconditions: *

                              • Observed counts must be non-negative.
                              • *
                              • Observed counts for a specific bin must not both be zero.
                              • *
                              • Observed counts for a specific sample must not all be 0.
                              • *
                              • The arrays {@code observed1} and {@code observed2} must * have the same length and their common length must be at least 2.
                              • *

                              *

                              If any of the preconditions are not met, a * {@code MathIllegalArgumentException} is thrown.

                              * * @param observed1 array of observed frequency counts of the first data set * @param observed2 array of observed frequency counts of the second data * set * @return p-value * @throws DimensionMismatchException the the length of the arrays does not * match or their common length is less than 2 * @throws NotPositiveException if any of the entries in {@code observed1} or * {@code observed2} are negative * @throws ZeroException if either all counts of {@code observed1} or * {@code observed2} are zero, or if the count at some index is * zero for both arrays * @throws MaxCountExceededException if an error occurs computing the * p-value. */ public double gTestDataSetsComparison(final long[] observed1, final long[] observed2) throws DimensionMismatchException, NotPositiveException, ZeroException, MaxCountExceededException { final ChiSquaredDistribution distribution = new ChiSquaredDistribution( (double) observed1.length - 1); return 1 - distribution.cumulativeProbability( gDataSetsComparison(observed1, observed2)); } /** *

                              Performs a G-Test (Log-Likelihood Ratio Test) comparing two binned * data sets. The test evaluates the null hypothesis that the two lists * of observed counts conform to the same frequency distribution, with * significance level {@code alpha}. Returns true iff the null * hypothesis can be rejected with 100 * (1 - alpha) percent confidence. *

                              *

                              See {@link #gDataSetsComparison(long[], long[])} for details * on the formula used to compute the G (LLR) statistic used in the test and * {@link #gTest(double[], long[])} for information on how * the observed significance level is computed. The degrees of of freedom used * to perform the test is one less than the common length of the input observed * count arrays.

                              * * Preconditions:
                                *
                              • Observed counts must be non-negative.
                              • *
                              • Observed counts for a specific bin must not both be zero.
                              • *
                              • Observed counts for a specific sample must not all be 0.
                              • *
                              • The arrays {@code observed1} and {@code observed2} must * have the same length and their common length must be at least 2.
                              • *
                              • {@code 0 < alpha < 0.5}

                              * *

                              If any of the preconditions are not met, a * {@code MathIllegalArgumentException} is thrown.

                              * * @param observed1 array of observed frequency counts of the first data set * @param observed2 array of observed frequency counts of the second data * set * @param alpha significance level of the test * @return true iff null hypothesis can be rejected with confidence 1 - * alpha * @throws DimensionMismatchException the the length of the arrays does not * match * @throws NotPositiveException if any of the entries in {@code observed1} or * {@code observed2} are negative * @throws ZeroException if either all counts of {@code observed1} or * {@code observed2} are zero, or if the count at some index is * zero for both arrays * @throws OutOfRangeException if {@code alpha} is not in the range * (0, 0.5] * @throws MaxCountExceededException if an error occurs performing the test */ public boolean gTestDataSetsComparison( final long[] observed1, final long[] observed2, final double alpha) throws DimensionMismatchException, NotPositiveException, ZeroException, OutOfRangeException, MaxCountExceededException { if (alpha <= 0 || alpha > 0.5) { throw new OutOfRangeException( LocalizedFormats.OUT_OF_BOUND_SIGNIFICANCE_LEVEL, alpha, 0, 0.5); } return gTestDataSetsComparison(observed1, observed2) < alpha; } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/stat/inference/ChiSquareTest.java100644 1750 1750 64120 12126627717 31052 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.stat.inference; import org.apache.commons.math3.distribution.ChiSquaredDistribution; import org.apache.commons.math3.exception.DimensionMismatchException; import org.apache.commons.math3.exception.MaxCountExceededException; import org.apache.commons.math3.exception.NotPositiveException; import org.apache.commons.math3.exception.NotStrictlyPositiveException; import org.apache.commons.math3.exception.NullArgumentException; import org.apache.commons.math3.exception.OutOfRangeException; import org.apache.commons.math3.exception.ZeroException; import org.apache.commons.math3.exception.util.LocalizedFormats; import org.apache.commons.math3.util.FastMath; import org.apache.commons.math3.util.MathArrays; /** * Implements Chi-Square test statistics. * *

                              This implementation handles both known and unknown distributions.

                              * *

                              Two samples tests can be used when the distribution is unknown a priori * but provided by one sample, or when the hypothesis under test is that the two * samples come from the same underlying distribution.

                              * * @version $Id: ChiSquareTest.java 1416643 2012-12-03 19:37:14Z tn $ */ public class ChiSquareTest { /** * Construct a ChiSquareTest */ public ChiSquareTest() { super(); } /** * Computes the * Chi-Square statistic comparing observed and expected * frequency counts. *

                              * This statistic can be used to perform a Chi-Square test evaluating the null * hypothesis that the observed counts follow the expected distribution.

                              *

                              * Preconditions:

                                *
                              • Expected counts must all be positive. *
                              • *
                              • Observed counts must all be ≥ 0. *
                              • *
                              • The observed and expected arrays must have the same length and * their common length must be at least 2. *

                              * If any of the preconditions are not met, an * IllegalArgumentException is thrown.

                              *

                              Note: This implementation rescales the * expected array if necessary to ensure that the sum of the * expected and observed counts are equal.

                              * * @param observed array of observed frequency counts * @param expected array of expected frequency counts * @return chiSquare test statistic * @throws NotPositiveException if observed has negative entries * @throws NotStrictlyPositiveException if expected has entries that are * not strictly positive * @throws DimensionMismatchException if the arrays length is less than 2 */ public double chiSquare(final double[] expected, final long[] observed) throws NotPositiveException, NotStrictlyPositiveException, DimensionMismatchException { if (expected.length < 2) { throw new DimensionMismatchException(expected.length, 2); } if (expected.length != observed.length) { throw new DimensionMismatchException(expected.length, observed.length); } MathArrays.checkPositive(expected); MathArrays.checkNonNegative(observed); double sumExpected = 0d; double sumObserved = 0d; for (int i = 0; i < observed.length; i++) { sumExpected += expected[i]; sumObserved += observed[i]; } double ratio = 1.0d; boolean rescale = false; if (FastMath.abs(sumExpected - sumObserved) > 10E-6) { ratio = sumObserved / sumExpected; rescale = true; } double sumSq = 0.0d; for (int i = 0; i < observed.length; i++) { if (rescale) { final double dev = observed[i] - ratio * expected[i]; sumSq += dev * dev / (ratio * expected[i]); } else { final double dev = observed[i] - expected[i]; sumSq += dev * dev / expected[i]; } } return sumSq; } /** * Returns the observed significance level, or * p-value, associated with a * * Chi-square goodness of fit test comparing the observed * frequency counts to those in the expected array. *

                              * The number returned is the smallest significance level at which one can reject * the null hypothesis that the observed counts conform to the frequency distribution * described by the expected counts.

                              *

                              * Preconditions:

                                *
                              • Expected counts must all be positive. *
                              • *
                              • Observed counts must all be ≥ 0. *
                              • *
                              • The observed and expected arrays must have the same length and * their common length must be at least 2. *

                              * If any of the preconditions are not met, an * IllegalArgumentException is thrown.

                              *

                              Note: This implementation rescales the * expected array if necessary to ensure that the sum of the * expected and observed counts are equal.

                              * * @param observed array of observed frequency counts * @param expected array of expected frequency counts * @return p-value * @throws NotPositiveException if observed has negative entries * @throws NotStrictlyPositiveException if expected has entries that are * not strictly positive * @throws DimensionMismatchException if the arrays length is less than 2 * @throws MaxCountExceededException if an error occurs computing the p-value */ public double chiSquareTest(final double[] expected, final long[] observed) throws NotPositiveException, NotStrictlyPositiveException, DimensionMismatchException, MaxCountExceededException { ChiSquaredDistribution distribution = new ChiSquaredDistribution(expected.length - 1.0); return 1.0 - distribution.cumulativeProbability(chiSquare(expected, observed)); } /** * Performs a * Chi-square goodness of fit test evaluating the null hypothesis that the * observed counts conform to the frequency distribution described by the expected * counts, with significance level alpha. Returns true iff the null * hypothesis can be rejected with 100 * (1 - alpha) percent confidence. *

                              * Example:
                              * To test the hypothesis that observed follows * expected at the 99% level, use

                              * chiSquareTest(expected, observed, 0.01)

                              *

                              * Preconditions:

                                *
                              • Expected counts must all be positive. *
                              • *
                              • Observed counts must all be ≥ 0. *
                              • *
                              • The observed and expected arrays must have the same length and * their common length must be at least 2. *
                              • 0 < alpha < 0.5 *

                              * If any of the preconditions are not met, an * IllegalArgumentException is thrown.

                              *

                              Note: This implementation rescales the * expected array if necessary to ensure that the sum of the * expected and observed counts are equal.

                              * * @param observed array of observed frequency counts * @param expected array of expected frequency counts * @param alpha significance level of the test * @return true iff null hypothesis can be rejected with confidence * 1 - alpha * @throws NotPositiveException if observed has negative entries * @throws NotStrictlyPositiveException if expected has entries that are * not strictly positive * @throws DimensionMismatchException if the arrays length is less than 2 * @throws OutOfRangeException if alpha is not in the range (0, 0.5] * @throws MaxCountExceededException if an error occurs computing the p-value */ public boolean chiSquareTest(final double[] expected, final long[] observed, final double alpha) throws NotPositiveException, NotStrictlyPositiveException, DimensionMismatchException, OutOfRangeException, MaxCountExceededException { if ((alpha <= 0) || (alpha > 0.5)) { throw new OutOfRangeException(LocalizedFormats.OUT_OF_BOUND_SIGNIFICANCE_LEVEL, alpha, 0, 0.5); } return chiSquareTest(expected, observed) < alpha; } /** * Computes the Chi-Square statistic associated with a * * chi-square test of independence based on the input counts * array, viewed as a two-way table. *

                              * The rows of the 2-way table are * count[0], ... , count[count.length - 1]

                              *

                              * Preconditions:

                                *
                              • All counts must be ≥ 0. *
                              • *
                              • The count array must be rectangular (i.e. all count[i] subarrays * must have the same length). *
                              • *
                              • The 2-way table represented by counts must have at * least 2 columns and at least 2 rows. *
                              • *

                              * If any of the preconditions are not met, an * IllegalArgumentException is thrown.

                              * * @param counts array representation of 2-way table * @return chiSquare test statistic * @throws NullArgumentException if the array is null * @throws DimensionMismatchException if the array is not rectangular * @throws NotPositiveException if {@code counts} has negative entries */ public double chiSquare(final long[][] counts) throws NullArgumentException, NotPositiveException, DimensionMismatchException { checkArray(counts); int nRows = counts.length; int nCols = counts[0].length; // compute row, column and total sums double[] rowSum = new double[nRows]; double[] colSum = new double[nCols]; double total = 0.0d; for (int row = 0; row < nRows; row++) { for (int col = 0; col < nCols; col++) { rowSum[row] += counts[row][col]; colSum[col] += counts[row][col]; total += counts[row][col]; } } // compute expected counts and chi-square double sumSq = 0.0d; double expected = 0.0d; for (int row = 0; row < nRows; row++) { for (int col = 0; col < nCols; col++) { expected = (rowSum[row] * colSum[col]) / total; sumSq += ((counts[row][col] - expected) * (counts[row][col] - expected)) / expected; } } return sumSq; } /** * Returns the observed significance level, or * p-value, associated with a * * chi-square test of independence based on the input counts * array, viewed as a two-way table. *

                              * The rows of the 2-way table are * count[0], ... , count[count.length - 1]

                              *

                              * Preconditions:

                                *
                              • All counts must be ≥ 0. *
                              • *
                              • The count array must be rectangular (i.e. all count[i] subarrays must have * the same length). *
                              • *
                              • The 2-way table represented by counts must have at least 2 * columns and at least 2 rows. *
                              • *

                              * If any of the preconditions are not met, an * IllegalArgumentException is thrown.

                              * * @param counts array representation of 2-way table * @return p-value * @throws NullArgumentException if the array is null * @throws DimensionMismatchException if the array is not rectangular * @throws NotPositiveException if {@code counts} has negative entries * @throws MaxCountExceededException if an error occurs computing the p-value */ public double chiSquareTest(final long[][] counts) throws NullArgumentException, DimensionMismatchException, NotPositiveException, MaxCountExceededException { checkArray(counts); double df = ((double) counts.length -1) * ((double) counts[0].length - 1); ChiSquaredDistribution distribution; distribution = new ChiSquaredDistribution(df); return 1 - distribution.cumulativeProbability(chiSquare(counts)); } /** * Performs a * chi-square test of independence evaluating the null hypothesis that the * classifications represented by the counts in the columns of the input 2-way table * are independent of the rows, with significance level alpha. * Returns true iff the null hypothesis can be rejected with 100 * (1 - alpha) percent * confidence. *

                              * The rows of the 2-way table are * count[0], ... , count[count.length - 1]

                              *

                              * Example:
                              * To test the null hypothesis that the counts in * count[0], ... , count[count.length - 1] * all correspond to the same underlying probability distribution at the 99% level, use

                              *

                              chiSquareTest(counts, 0.01)

                              *

                              * Preconditions:

                                *
                              • All counts must be ≥ 0. *
                              • *
                              • The count array must be rectangular (i.e. all count[i] subarrays must have the * same length).
                              • *
                              • The 2-way table represented by counts must have at least 2 columns and * at least 2 rows.
                              • *

                              * If any of the preconditions are not met, an * IllegalArgumentException is thrown.

                              * * @param counts array representation of 2-way table * @param alpha significance level of the test * @return true iff null hypothesis can be rejected with confidence * 1 - alpha * @throws NullArgumentException if the array is null * @throws DimensionMismatchException if the array is not rectangular * @throws NotPositiveException if {@code counts} has any negative entries * @throws OutOfRangeException if alpha is not in the range (0, 0.5] * @throws MaxCountExceededException if an error occurs computing the p-value */ public boolean chiSquareTest(final long[][] counts, final double alpha) throws NullArgumentException, DimensionMismatchException, NotPositiveException, OutOfRangeException, MaxCountExceededException { if ((alpha <= 0) || (alpha > 0.5)) { throw new OutOfRangeException(LocalizedFormats.OUT_OF_BOUND_SIGNIFICANCE_LEVEL, alpha, 0, 0.5); } return chiSquareTest(counts) < alpha; } /** *

                              Computes a * * Chi-Square two sample test statistic comparing bin frequency counts * in observed1 and observed2. The * sums of frequency counts in the two samples are not required to be the * same. The formula used to compute the test statistic is

                              * * ∑[(K * observed1[i] - observed2[i]/K)2 / (observed1[i] + observed2[i])] * where *
                              K = &sqrt;[&sum(observed2 / ∑(observed1)] *

                              *

                              This statistic can be used to perform a Chi-Square test evaluating the * null hypothesis that both observed counts follow the same distribution.

                              *

                              * Preconditions:

                                *
                              • Observed counts must be non-negative. *
                              • *
                              • Observed counts for a specific bin must not both be zero. *
                              • *
                              • Observed counts for a specific sample must not all be 0. *
                              • *
                              • The arrays observed1 and observed2 must have * the same length and their common length must be at least 2. *

                              * If any of the preconditions are not met, an * IllegalArgumentException is thrown.

                              * * @param observed1 array of observed frequency counts of the first data set * @param observed2 array of observed frequency counts of the second data set * @return chiSquare test statistic * @throws DimensionMismatchException the the length of the arrays does not match * @throws NotPositiveException if any entries in observed1 or * observed2 are negative * @throws ZeroException if either all counts of observed1 or * observed2 are zero, or if the count at some index is zero * for both arrays * @since 1.2 */ public double chiSquareDataSetsComparison(long[] observed1, long[] observed2) throws DimensionMismatchException, NotPositiveException, ZeroException { // Make sure lengths are same if (observed1.length < 2) { throw new DimensionMismatchException(observed1.length, 2); } if (observed1.length != observed2.length) { throw new DimensionMismatchException(observed1.length, observed2.length); } // Ensure non-negative counts MathArrays.checkNonNegative(observed1); MathArrays.checkNonNegative(observed2); // Compute and compare count sums long countSum1 = 0; long countSum2 = 0; boolean unequalCounts = false; double weight = 0.0; for (int i = 0; i < observed1.length; i++) { countSum1 += observed1[i]; countSum2 += observed2[i]; } // Ensure neither sample is uniformly 0 if (countSum1 == 0 || countSum2 == 0) { throw new ZeroException(); } // Compare and compute weight only if different unequalCounts = countSum1 != countSum2; if (unequalCounts) { weight = FastMath.sqrt((double) countSum1 / (double) countSum2); } // Compute ChiSquare statistic double sumSq = 0.0d; double dev = 0.0d; double obs1 = 0.0d; double obs2 = 0.0d; for (int i = 0; i < observed1.length; i++) { if (observed1[i] == 0 && observed2[i] == 0) { throw new ZeroException(LocalizedFormats.OBSERVED_COUNTS_BOTTH_ZERO_FOR_ENTRY, i); } else { obs1 = observed1[i]; obs2 = observed2[i]; if (unequalCounts) { // apply weights dev = obs1/weight - obs2 * weight; } else { dev = obs1 - obs2; } sumSq += (dev * dev) / (obs1 + obs2); } } return sumSq; } /** *

                              Returns the observed significance level, or * p-value, associated with a Chi-Square two sample test comparing * bin frequency counts in observed1 and * observed2. *

                              *

                              The number returned is the smallest significance level at which one * can reject the null hypothesis that the observed counts conform to the * same distribution. *

                              *

                              See {@link #chiSquareDataSetsComparison(long[], long[])} for details * on the formula used to compute the test statistic. The degrees of * of freedom used to perform the test is one less than the common length * of the input observed count arrays. *

                              * Preconditions:
                                *
                              • Observed counts must be non-negative. *
                              • *
                              • Observed counts for a specific bin must not both be zero. *
                              • *
                              • Observed counts for a specific sample must not all be 0. *
                              • *
                              • The arrays observed1 and observed2 must * have the same length and * their common length must be at least 2. *

                              * If any of the preconditions are not met, an * IllegalArgumentException is thrown.

                              * * @param observed1 array of observed frequency counts of the first data set * @param observed2 array of observed frequency counts of the second data set * @return p-value * @throws DimensionMismatchException the the length of the arrays does not match * @throws NotPositiveException if any entries in observed1 or * observed2 are negative * @throws ZeroException if either all counts of observed1 or * observed2 are zero, or if the count at the same index is zero * for both arrays * @throws MaxCountExceededException if an error occurs computing the p-value * @since 1.2 */ public double chiSquareTestDataSetsComparison(long[] observed1, long[] observed2) throws DimensionMismatchException, NotPositiveException, ZeroException, MaxCountExceededException { ChiSquaredDistribution distribution; distribution = new ChiSquaredDistribution((double) observed1.length - 1); return 1 - distribution.cumulativeProbability( chiSquareDataSetsComparison(observed1, observed2)); } /** *

                              Performs a Chi-Square two sample test comparing two binned data * sets. The test evaluates the null hypothesis that the two lists of * observed counts conform to the same frequency distribution, with * significance level alpha. Returns true iff the null * hypothesis can be rejected with 100 * (1 - alpha) percent confidence. *

                              *

                              See {@link #chiSquareDataSetsComparison(long[], long[])} for * details on the formula used to compute the Chisquare statistic used * in the test. The degrees of of freedom used to perform the test is * one less than the common length of the input observed count arrays. *

                              * Preconditions:
                                *
                              • Observed counts must be non-negative. *
                              • *
                              • Observed counts for a specific bin must not both be zero. *
                              • *
                              • Observed counts for a specific sample must not all be 0. *
                              • *
                              • The arrays observed1 and observed2 must * have the same length and their common length must be at least 2. *
                              • *
                              • 0 < alpha < 0.5 *

                              * If any of the preconditions are not met, an * IllegalArgumentException is thrown.

                              * * @param observed1 array of observed frequency counts of the first data set * @param observed2 array of observed frequency counts of the second data set * @param alpha significance level of the test * @return true iff null hypothesis can be rejected with confidence * 1 - alpha * @throws DimensionMismatchException the the length of the arrays does not match * @throws NotPositiveException if any entries in observed1 or * observed2 are negative * @throws ZeroException if either all counts of observed1 or * observed2 are zero, or if the count at the same index is zero * for both arrays * @throws OutOfRangeException if alpha is not in the range (0, 0.5] * @throws MaxCountExceededException if an error occurs performing the test * @since 1.2 */ public boolean chiSquareTestDataSetsComparison(final long[] observed1, final long[] observed2, final double alpha) throws DimensionMismatchException, NotPositiveException, ZeroException, OutOfRangeException, MaxCountExceededException { if (alpha <= 0 || alpha > 0.5) { throw new OutOfRangeException(LocalizedFormats.OUT_OF_BOUND_SIGNIFICANCE_LEVEL, alpha, 0, 0.5); } return chiSquareTestDataSetsComparison(observed1, observed2) < alpha; } /** * Checks to make sure that the input long[][] array is rectangular, * has at least 2 rows and 2 columns, and has all non-negative entries. * * @param in input 2-way table to check * @throws NullArgumentException if the array is null * @throws DimensionMismatchException if the array is not valid * @throws NotPositiveException if the array contains any negative entries */ private void checkArray(final long[][] in) throws NullArgumentException, DimensionMismatchException, NotPositiveException { if (in.length < 2) { throw new DimensionMismatchException(in.length, 2); } if (in[0].length < 2) { throw new DimensionMismatchException(in[0].length, 2); } MathArrays.checkRectangular(in); MathArrays.checkNonNegative(in); } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/stat/ranking/NaturalRanking.java100644 1750 1750 40454 12126627717 30745 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.stat.ranking; import java.util.ArrayList; import java.util.Arrays; import java.util.Iterator; import java.util.List; import org.apache.commons.math3.exception.MathInternalError; import org.apache.commons.math3.exception.NotANumberException; import org.apache.commons.math3.random.RandomDataGenerator; import org.apache.commons.math3.random.RandomGenerator; import org.apache.commons.math3.util.FastMath; /** *

                              Ranking based on the natural ordering on doubles.

                              *

                              NaNs are treated according to the configured {@link NaNStrategy} and ties * are handled using the selected {@link TiesStrategy}. * Configuration settings are supplied in optional constructor arguments. * Defaults are {@link NaNStrategy#FAILED} and {@link TiesStrategy#AVERAGE}, * respectively. When using {@link TiesStrategy#RANDOM}, a * {@link RandomGenerator} may be supplied as a constructor argument.

                              *

                              Examples: * * * * * * * * * * * * * * * * * * * * * * * *
                              * Input data: (20, 17, 30, 42.3, 17, 50, Double.NaN, Double.NEGATIVE_INFINITY, 17) *
                              NaNStrategyTiesStrategyrank(data)
                              default (NaNs maximal)default (ties averaged)(5, 3, 6, 7, 3, 8, 9, 1, 3)
                              default (NaNs maximal)MINIMUM(5, 2, 6, 7, 2, 8, 9, 1, 2)
                              MINIMALdefault (ties averaged)(6, 4, 7, 8, 4, 9, 1.5, 1.5, 4)
                              REMOVEDSEQUENTIAL(5, 2, 6, 7, 3, 8, 1, 4)
                              MINIMALMAXIMUM(6, 5, 7, 8, 5, 9, 2, 2, 5)

                              * * @since 2.0 * @version $Id: NaturalRanking.java 1454897 2013-03-10 19:02:54Z luc $ */ public class NaturalRanking implements RankingAlgorithm { /** default NaN strategy */ public static final NaNStrategy DEFAULT_NAN_STRATEGY = NaNStrategy.FAILED; /** default ties strategy */ public static final TiesStrategy DEFAULT_TIES_STRATEGY = TiesStrategy.AVERAGE; /** NaN strategy - defaults to NaNs maximal */ private final NaNStrategy nanStrategy; /** Ties strategy - defaults to ties averaged */ private final TiesStrategy tiesStrategy; /** Source of random data - used only when ties strategy is RANDOM */ private final RandomDataGenerator randomData; /** * Create a NaturalRanking with default strategies for handling ties and NaNs. */ public NaturalRanking() { super(); tiesStrategy = DEFAULT_TIES_STRATEGY; nanStrategy = DEFAULT_NAN_STRATEGY; randomData = null; } /** * Create a NaturalRanking with the given TiesStrategy. * * @param tiesStrategy the TiesStrategy to use */ public NaturalRanking(TiesStrategy tiesStrategy) { super(); this.tiesStrategy = tiesStrategy; nanStrategy = DEFAULT_NAN_STRATEGY; randomData = new RandomDataGenerator(); } /** * Create a NaturalRanking with the given NaNStrategy. * * @param nanStrategy the NaNStrategy to use */ public NaturalRanking(NaNStrategy nanStrategy) { super(); this.nanStrategy = nanStrategy; tiesStrategy = DEFAULT_TIES_STRATEGY; randomData = null; } /** * Create a NaturalRanking with the given NaNStrategy and TiesStrategy. * * @param nanStrategy NaNStrategy to use * @param tiesStrategy TiesStrategy to use */ public NaturalRanking(NaNStrategy nanStrategy, TiesStrategy tiesStrategy) { super(); this.nanStrategy = nanStrategy; this.tiesStrategy = tiesStrategy; randomData = new RandomDataGenerator(); } /** * Create a NaturalRanking with TiesStrategy.RANDOM and the given * RandomGenerator as the source of random data. * * @param randomGenerator source of random data */ public NaturalRanking(RandomGenerator randomGenerator) { super(); this.tiesStrategy = TiesStrategy.RANDOM; nanStrategy = DEFAULT_NAN_STRATEGY; randomData = new RandomDataGenerator(randomGenerator); } /** * Create a NaturalRanking with the given NaNStrategy, TiesStrategy.RANDOM * and the given source of random data. * * @param nanStrategy NaNStrategy to use * @param randomGenerator source of random data */ public NaturalRanking(NaNStrategy nanStrategy, RandomGenerator randomGenerator) { super(); this.nanStrategy = nanStrategy; this.tiesStrategy = TiesStrategy.RANDOM; randomData = new RandomDataGenerator(randomGenerator); } /** * Return the NaNStrategy * * @return returns the NaNStrategy */ public NaNStrategy getNanStrategy() { return nanStrategy; } /** * Return the TiesStrategy * * @return the TiesStrategy */ public TiesStrategy getTiesStrategy() { return tiesStrategy; } /** * Rank data using the natural ordering on Doubles, with * NaN values handled according to nanStrategy and ties * resolved using tiesStrategy. * * @param data array to be ranked * @return array of ranks * @throws NotANumberException if the selected {@link NaNStrategy} is {@code FAILED} * and a {@link Double#NaN} is encountered in the input data */ public double[] rank(double[] data) { // Array recording initial positions of data to be ranked IntDoublePair[] ranks = new IntDoublePair[data.length]; for (int i = 0; i < data.length; i++) { ranks[i] = new IntDoublePair(data[i], i); } // Recode, remove or record positions of NaNs List nanPositions = null; switch (nanStrategy) { case MAXIMAL: // Replace NaNs with +INFs recodeNaNs(ranks, Double.POSITIVE_INFINITY); break; case MINIMAL: // Replace NaNs with -INFs recodeNaNs(ranks, Double.NEGATIVE_INFINITY); break; case REMOVED: // Drop NaNs from data ranks = removeNaNs(ranks); break; case FIXED: // Record positions of NaNs nanPositions = getNanPositions(ranks); break; case FAILED: nanPositions = getNanPositions(ranks); if (nanPositions.size() > 0) { throw new NotANumberException(); } break; default: // this should not happen unless NaNStrategy enum is changed throw new MathInternalError(); } // Sort the IntDoublePairs Arrays.sort(ranks); // Walk the sorted array, filling output array using sorted positions, // resolving ties as we go double[] out = new double[ranks.length]; int pos = 1; // position in sorted array out[ranks[0].getPosition()] = pos; List tiesTrace = new ArrayList(); tiesTrace.add(ranks[0].getPosition()); for (int i = 1; i < ranks.length; i++) { if (Double.compare(ranks[i].getValue(), ranks[i - 1].getValue()) > 0) { // tie sequence has ended (or had length 1) pos = i + 1; if (tiesTrace.size() > 1) { // if seq is nontrivial, resolve resolveTie(out, tiesTrace); } tiesTrace = new ArrayList(); tiesTrace.add(ranks[i].getPosition()); } else { // tie sequence continues tiesTrace.add(ranks[i].getPosition()); } out[ranks[i].getPosition()] = pos; } if (tiesTrace.size() > 1) { // handle tie sequence at end resolveTie(out, tiesTrace); } if (nanStrategy == NaNStrategy.FIXED) { restoreNaNs(out, nanPositions); } return out; } /** * Returns an array that is a copy of the input array with IntDoublePairs * having NaN values removed. * * @param ranks input array * @return array with NaN-valued entries removed */ private IntDoublePair[] removeNaNs(IntDoublePair[] ranks) { if (!containsNaNs(ranks)) { return ranks; } IntDoublePair[] outRanks = new IntDoublePair[ranks.length]; int j = 0; for (int i = 0; i < ranks.length; i++) { if (Double.isNaN(ranks[i].getValue())) { // drop, but adjust original ranks of later elements for (int k = i + 1; k < ranks.length; k++) { ranks[k] = new IntDoublePair( ranks[k].getValue(), ranks[k].getPosition() - 1); } } else { outRanks[j] = new IntDoublePair( ranks[i].getValue(), ranks[i].getPosition()); j++; } } IntDoublePair[] returnRanks = new IntDoublePair[j]; System.arraycopy(outRanks, 0, returnRanks, 0, j); return returnRanks; } /** * Recodes NaN values to the given value. * * @param ranks array to recode * @param value the value to replace NaNs with */ private void recodeNaNs(IntDoublePair[] ranks, double value) { for (int i = 0; i < ranks.length; i++) { if (Double.isNaN(ranks[i].getValue())) { ranks[i] = new IntDoublePair( value, ranks[i].getPosition()); } } } /** * Checks for presence of NaNs in ranks. * * @param ranks array to be searched for NaNs * @return true iff ranks contains one or more NaNs */ private boolean containsNaNs(IntDoublePair[] ranks) { for (int i = 0; i < ranks.length; i++) { if (Double.isNaN(ranks[i].getValue())) { return true; } } return false; } /** * Resolve a sequence of ties, using the configured {@link TiesStrategy}. * The input ranks array is expected to take the same value * for all indices in tiesTrace. The common value is recoded * according to the tiesStrategy. For example, if ranks = <5,8,2,6,2,7,1,2>, * tiesTrace = <2,4,7> and tiesStrategy is MINIMUM, ranks will be unchanged. * The same array and trace with tiesStrategy AVERAGE will come out * <5,8,3,6,3,7,1,3>. * * @param ranks array of ranks * @param tiesTrace list of indices where ranks is constant * -- that is, for any i and j in TiesTrace, ranks[i] == ranks[j] * */ private void resolveTie(double[] ranks, List tiesTrace) { // constant value of ranks over tiesTrace final double c = ranks[tiesTrace.get(0)]; // length of sequence of tied ranks final int length = tiesTrace.size(); switch (tiesStrategy) { case AVERAGE: // Replace ranks with average fill(ranks, tiesTrace, (2 * c + length - 1) / 2d); break; case MAXIMUM: // Replace ranks with maximum values fill(ranks, tiesTrace, c + length - 1); break; case MINIMUM: // Replace ties with minimum fill(ranks, tiesTrace, c); break; case RANDOM: // Fill with random integral values in [c, c + length - 1] Iterator iterator = tiesTrace.iterator(); long f = FastMath.round(c); while (iterator.hasNext()) { // No advertised exception because args are guaranteed valid ranks[iterator.next()] = randomData.nextLong(f, f + length - 1); } break; case SEQUENTIAL: // Fill sequentially from c to c + length - 1 // walk and fill iterator = tiesTrace.iterator(); f = FastMath.round(c); int i = 0; while (iterator.hasNext()) { ranks[iterator.next()] = f + i++; } break; default: // this should not happen unless TiesStrategy enum is changed throw new MathInternalError(); } } /** * Setsdata[i] = value for each i in tiesTrace. * * @param data array to modify * @param tiesTrace list of index values to set * @param value value to set */ private void fill(double[] data, List tiesTrace, double value) { Iterator iterator = tiesTrace.iterator(); while (iterator.hasNext()) { data[iterator.next()] = value; } } /** * Set ranks[i] = Double.NaN for each i in nanPositions. * * @param ranks array to modify * @param nanPositions list of index values to set to Double.NaN */ private void restoreNaNs(double[] ranks, List nanPositions) { if (nanPositions.size() == 0) { return; } Iterator iterator = nanPositions.iterator(); while (iterator.hasNext()) { ranks[iterator.next().intValue()] = Double.NaN; } } /** * Returns a list of indexes where ranks is NaN. * * @param ranks array to search for NaNs * @return list of indexes i such that ranks[i] = NaN */ private List getNanPositions(IntDoublePair[] ranks) { ArrayList out = new ArrayList(); for (int i = 0; i < ranks.length; i++) { if (Double.isNaN(ranks[i].getValue())) { out.add(Integer.valueOf(i)); } } return out; } /** * Represents the position of a double value in an ordering. * Comparable interface is implemented so Arrays.sort can be used * to sort an array of IntDoublePairs by value. Note that the * implicitly defined natural ordering is NOT consistent with equals. */ private static class IntDoublePair implements Comparable { /** Value of the pair */ private final double value; /** Original position of the pair */ private final int position; /** * Construct an IntDoublePair with the given value and position. * @param value the value of the pair * @param position the original position */ public IntDoublePair(double value, int position) { this.value = value; this.position = position; } /** * Compare this IntDoublePair to another pair. * Only the values are compared. * * @param other the other pair to compare this to * @return result of Double.compare(value, other.value) */ public int compareTo(IntDoublePair other) { return Double.compare(value, other.value); } // N.B. equals() and hashCode() are not implemented; see MATH-610 for discussion. /** * Returns the value of the pair. * @return value */ public double getValue() { return value; } /** * Returns the original position of the pair. * @return position */ public int getPosition() { return position; } } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/stat/ranking/package-info.java100644 1750 1750 1620 12126627717 30321 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ /** * * Classes providing rank transformations. * */ package org.apache.commons.math3.stat.ranking; commons-math3-3.2-src/src/main/java/org/apache/commons/math3/stat/ranking/NaNStrategy.java100644 1750 1750 3732 12126627717 30202 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.stat.ranking; /** * Strategies for handling NaN values in rank transformations. *
                                *
                              • MINIMAL - NaNs are treated as minimal in the ordering, equivalent to * (that is, tied with) Double.NEGATIVE_INFINITY.
                              • *
                              • MAXIMAL - NaNs are treated as maximal in the ordering, equivalent to * Double.POSITIVE_INFINITY
                              • *
                              • REMOVED - NaNs are removed before the rank transform is applied
                              • *
                              • FIXED - NaNs are left "in place," that is the rank transformation is * applied to the other elements in the input array, but the NaN elements * are returned unchanged.
                              • *
                              • FAILED - If any NaN is encountered in the input array, an appropriate * exception is thrown
                              • *
                              * * @since 2.0 * @version $Id: NaNStrategy.java 1422313 2012-12-15 18:53:41Z psteitz $ */ public enum NaNStrategy { /** NaNs are considered minimal in the ordering */ MINIMAL, /** NaNs are considered maximal in the ordering */ MAXIMAL, /** NaNs are removed before computing ranks */ REMOVED, /** NaNs are left in place */ FIXED, /** NaNs result in an exception * @since 3.1 */ FAILED } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/stat/ranking/TiesStrategy.java100644 1750 1750 4311 12126627717 30424 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.stat.ranking; /** * Strategies for handling tied values in rank transformations. *
                                *
                              • SEQUENTIAL - Ties are assigned ranks in order of occurrence in the original array, * for example (1,3,4,3) is ranked as (1,2,4,3)
                              • *
                              • MINIMUM - Tied values are assigned the minimum applicable rank, or the rank * of the first occurrence. For example, (1,3,4,3) is ranked as (1,2,4,2)
                              • *
                              • MAXIMUM - Tied values are assigned the maximum applicable rank, or the rank * of the last occurrence. For example, (1,3,4,3) is ranked as (1,3,4,3)
                              • *
                              • AVERAGE - Tied values are assigned the average of the applicable ranks. * For example, (1,3,4,3) is ranked as (1,2.5,4,2.5)
                              • *
                              • RANDOM - Tied values are assigned a random integer rank from among the * applicable values. The assigned rank will always be an integer, (inclusively) * between the values returned by the MINIMUM and MAXIMUM strategies.
                              • *
                              * * @since 2.0 * @version $Id: TiesStrategy.java 1416643 2012-12-03 19:37:14Z tn $ */ public enum TiesStrategy { /** Ties assigned sequential ranks in order of occurrence */ SEQUENTIAL, /** Ties get the minimum applicable rank */ MINIMUM, /** Ties get the maximum applicable rank */ MAXIMUM, /** Ties get the average of applicable ranks */ AVERAGE, /** Ties get a random integral value from among applicable ranks */ RANDOM } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/stat/ranking/RankingAlgorithm.java100644 1750 1750 3201 12126627717 31232 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.stat.ranking; /** * Interface representing a rank transformation. * * @since 2.0 * @version $Id: RankingAlgorithm.java 1416643 2012-12-03 19:37:14Z tn $ */ public interface RankingAlgorithm { /** *

                              Performs a rank transformation on the input data, returning an array * of ranks.

                              * *

                              Ranks should be 1-based - that is, the smallest value * returned in an array of ranks should be greater than or equal to one, * rather than 0. Ranks should in general take integer values, though * implementations may return averages or other floating point values * to resolve ties in the input data.

                              * * @param data array of data to be ranked * @return an array of ranks corresponding to the elements of the input array */ double[] rank (double[] data); } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/stat/clustering/package-info.java100644 1750 1750 2143 12126627717 31050 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ /** *

                              All classes and sub-packages of this package are deprecated.

                              *

                              Please use their replacements, to be found under *
                                *
                              • {@link org.apache.commons.math3.ml.clustering}
                              • *
                              *

                              * *

                              * Clustering algorithms. *

                              */ package org.apache.commons.math3.stat.clustering; ././@LongLink100644 0 0 152 12126630647 10260 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/stat/clustering/KMeansPlusPlusClusterer.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/stat/clustering/KMeansPlusPlusClusterer100644 1750 1750 46661 12126627717 32420 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.stat.clustering; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; import java.util.List; import java.util.Random; import org.apache.commons.math3.exception.ConvergenceException; import org.apache.commons.math3.exception.MathIllegalArgumentException; import org.apache.commons.math3.exception.NumberIsTooSmallException; import org.apache.commons.math3.exception.util.LocalizedFormats; import org.apache.commons.math3.stat.descriptive.moment.Variance; import org.apache.commons.math3.util.MathUtils; /** * Clustering algorithm based on David Arthur and Sergei Vassilvitski k-means++ algorithm. * @param type of the points to cluster * @see K-means++ (wikipedia) * @version $Id: KMeansPlusPlusClusterer.java 1461871 2013-03-27 22:01:25Z tn $ * @since 2.0 * @deprecated As of 3.2 (to be removed in 4.0), * use {@link org.apache.commons.math3.ml.clustering.KMeansPlusPlusClusterer} instead */ @Deprecated public class KMeansPlusPlusClusterer> { /** Strategies to use for replacing an empty cluster. */ public static enum EmptyClusterStrategy { /** Split the cluster with largest distance variance. */ LARGEST_VARIANCE, /** Split the cluster with largest number of points. */ LARGEST_POINTS_NUMBER, /** Create a cluster around the point farthest from its centroid. */ FARTHEST_POINT, /** Generate an error. */ ERROR } /** Random generator for choosing initial centers. */ private final Random random; /** Selected strategy for empty clusters. */ private final EmptyClusterStrategy emptyStrategy; /** Build a clusterer. *

                              * The default strategy for handling empty clusters that may appear during * algorithm iterations is to split the cluster with largest distance variance. *

                              * @param random random generator to use for choosing initial centers */ public KMeansPlusPlusClusterer(final Random random) { this(random, EmptyClusterStrategy.LARGEST_VARIANCE); } /** Build a clusterer. * @param random random generator to use for choosing initial centers * @param emptyStrategy strategy to use for handling empty clusters that * may appear during algorithm iterations * @since 2.2 */ public KMeansPlusPlusClusterer(final Random random, final EmptyClusterStrategy emptyStrategy) { this.random = random; this.emptyStrategy = emptyStrategy; } /** * Runs the K-means++ clustering algorithm. * * @param points the points to cluster * @param k the number of clusters to split the data into * @param numTrials number of trial runs * @param maxIterationsPerTrial the maximum number of iterations to run the algorithm * for at each trial run. If negative, no maximum will be used * @return a list of clusters containing the points * @throws MathIllegalArgumentException if the data points are null or the number * of clusters is larger than the number of data points * @throws ConvergenceException if an empty cluster is encountered and the * {@link #emptyStrategy} is set to {@code ERROR} */ public List> cluster(final Collection points, final int k, int numTrials, int maxIterationsPerTrial) throws MathIllegalArgumentException, ConvergenceException { // at first, we have not found any clusters list yet List> best = null; double bestVarianceSum = Double.POSITIVE_INFINITY; // do several clustering trials for (int i = 0; i < numTrials; ++i) { // compute a clusters list List> clusters = cluster(points, k, maxIterationsPerTrial); // compute the variance of the current list double varianceSum = 0.0; for (final Cluster cluster : clusters) { if (!cluster.getPoints().isEmpty()) { // compute the distance variance of the current cluster final T center = cluster.getCenter(); final Variance stat = new Variance(); for (final T point : cluster.getPoints()) { stat.increment(point.distanceFrom(center)); } varianceSum += stat.getResult(); } } if (varianceSum <= bestVarianceSum) { // this one is the best we have found so far, remember it best = clusters; bestVarianceSum = varianceSum; } } // return the best clusters list found return best; } /** * Runs the K-means++ clustering algorithm. * * @param points the points to cluster * @param k the number of clusters to split the data into * @param maxIterations the maximum number of iterations to run the algorithm * for. If negative, no maximum will be used * @return a list of clusters containing the points * @throws MathIllegalArgumentException if the data points are null or the number * of clusters is larger than the number of data points * @throws ConvergenceException if an empty cluster is encountered and the * {@link #emptyStrategy} is set to {@code ERROR} */ public List> cluster(final Collection points, final int k, final int maxIterations) throws MathIllegalArgumentException, ConvergenceException { // sanity checks MathUtils.checkNotNull(points); // number of clusters has to be smaller or equal the number of data points if (points.size() < k) { throw new NumberIsTooSmallException(points.size(), k, false); } // create the initial clusters List> clusters = chooseInitialCenters(points, k, random); // create an array containing the latest assignment of a point to a cluster // no need to initialize the array, as it will be filled with the first assignment int[] assignments = new int[points.size()]; assignPointsToClusters(clusters, points, assignments); // iterate through updating the centers until we're done final int max = (maxIterations < 0) ? Integer.MAX_VALUE : maxIterations; for (int count = 0; count < max; count++) { boolean emptyCluster = false; List> newClusters = new ArrayList>(); for (final Cluster cluster : clusters) { final T newCenter; if (cluster.getPoints().isEmpty()) { switch (emptyStrategy) { case LARGEST_VARIANCE : newCenter = getPointFromLargestVarianceCluster(clusters); break; case LARGEST_POINTS_NUMBER : newCenter = getPointFromLargestNumberCluster(clusters); break; case FARTHEST_POINT : newCenter = getFarthestPoint(clusters); break; default : throw new ConvergenceException(LocalizedFormats.EMPTY_CLUSTER_IN_K_MEANS); } emptyCluster = true; } else { newCenter = cluster.getCenter().centroidOf(cluster.getPoints()); } newClusters.add(new Cluster(newCenter)); } int changes = assignPointsToClusters(newClusters, points, assignments); clusters = newClusters; // if there were no more changes in the point-to-cluster assignment // and there are no empty clusters left, return the current clusters if (changes == 0 && !emptyCluster) { return clusters; } } return clusters; } /** * Adds the given points to the closest {@link Cluster}. * * @param type of the points to cluster * @param clusters the {@link Cluster}s to add the points to * @param points the points to add to the given {@link Cluster}s * @param assignments points assignments to clusters * @return the number of points assigned to different clusters as the iteration before */ private static > int assignPointsToClusters(final List> clusters, final Collection points, final int[] assignments) { int assignedDifferently = 0; int pointIndex = 0; for (final T p : points) { int clusterIndex = getNearestCluster(clusters, p); if (clusterIndex != assignments[pointIndex]) { assignedDifferently++; } Cluster cluster = clusters.get(clusterIndex); cluster.addPoint(p); assignments[pointIndex++] = clusterIndex; } return assignedDifferently; } /** * Use K-means++ to choose the initial centers. * * @param type of the points to cluster * @param points the points to choose the initial centers from * @param k the number of centers to choose * @param random random generator to use * @return the initial centers */ private static > List> chooseInitialCenters(final Collection points, final int k, final Random random) { // Convert to list for indexed access. Make it unmodifiable, since removal of items // would screw up the logic of this method. final List pointList = Collections.unmodifiableList(new ArrayList (points)); // The number of points in the list. final int numPoints = pointList.size(); // Set the corresponding element in this array to indicate when // elements of pointList are no longer available. final boolean[] taken = new boolean[numPoints]; // The resulting list of initial centers. final List> resultSet = new ArrayList>(); // Choose one center uniformly at random from among the data points. final int firstPointIndex = random.nextInt(numPoints); final T firstPoint = pointList.get(firstPointIndex); resultSet.add(new Cluster(firstPoint)); // Must mark it as taken taken[firstPointIndex] = true; // To keep track of the minimum distance squared of elements of // pointList to elements of resultSet. final double[] minDistSquared = new double[numPoints]; // Initialize the elements. Since the only point in resultSet is firstPoint, // this is very easy. for (int i = 0; i < numPoints; i++) { if (i != firstPointIndex) { // That point isn't considered double d = firstPoint.distanceFrom(pointList.get(i)); minDistSquared[i] = d*d; } } while (resultSet.size() < k) { // Sum up the squared distances for the points in pointList not // already taken. double distSqSum = 0.0; for (int i = 0; i < numPoints; i++) { if (!taken[i]) { distSqSum += minDistSquared[i]; } } // Add one new data point as a center. Each point x is chosen with // probability proportional to D(x)2 final double r = random.nextDouble() * distSqSum; // The index of the next point to be added to the resultSet. int nextPointIndex = -1; // Sum through the squared min distances again, stopping when // sum >= r. double sum = 0.0; for (int i = 0; i < numPoints; i++) { if (!taken[i]) { sum += minDistSquared[i]; if (sum >= r) { nextPointIndex = i; break; } } } // If it's not set to >= 0, the point wasn't found in the previous // for loop, probably because distances are extremely small. Just pick // the last available point. if (nextPointIndex == -1) { for (int i = numPoints - 1; i >= 0; i--) { if (!taken[i]) { nextPointIndex = i; break; } } } // We found one. if (nextPointIndex >= 0) { final T p = pointList.get(nextPointIndex); resultSet.add(new Cluster (p)); // Mark it as taken. taken[nextPointIndex] = true; if (resultSet.size() < k) { // Now update elements of minDistSquared. We only have to compute // the distance to the new center to do this. for (int j = 0; j < numPoints; j++) { // Only have to worry about the points still not taken. if (!taken[j]) { double d = p.distanceFrom(pointList.get(j)); double d2 = d * d; if (d2 < minDistSquared[j]) { minDistSquared[j] = d2; } } } } } else { // None found -- // Break from the while loop to prevent // an infinite loop. break; } } return resultSet; } /** * Get a random point from the {@link Cluster} with the largest distance variance. * * @param clusters the {@link Cluster}s to search * @return a random point from the selected cluster * @throws ConvergenceException if clusters are all empty */ private T getPointFromLargestVarianceCluster(final Collection> clusters) throws ConvergenceException { double maxVariance = Double.NEGATIVE_INFINITY; Cluster selected = null; for (final Cluster cluster : clusters) { if (!cluster.getPoints().isEmpty()) { // compute the distance variance of the current cluster final T center = cluster.getCenter(); final Variance stat = new Variance(); for (final T point : cluster.getPoints()) { stat.increment(point.distanceFrom(center)); } final double variance = stat.getResult(); // select the cluster with the largest variance if (variance > maxVariance) { maxVariance = variance; selected = cluster; } } } // did we find at least one non-empty cluster ? if (selected == null) { throw new ConvergenceException(LocalizedFormats.EMPTY_CLUSTER_IN_K_MEANS); } // extract a random point from the cluster final List selectedPoints = selected.getPoints(); return selectedPoints.remove(random.nextInt(selectedPoints.size())); } /** * Get a random point from the {@link Cluster} with the largest number of points * * @param clusters the {@link Cluster}s to search * @return a random point from the selected cluster * @throws ConvergenceException if clusters are all empty */ private T getPointFromLargestNumberCluster(final Collection> clusters) throws ConvergenceException { int maxNumber = 0; Cluster selected = null; for (final Cluster cluster : clusters) { // get the number of points of the current cluster final int number = cluster.getPoints().size(); // select the cluster with the largest number of points if (number > maxNumber) { maxNumber = number; selected = cluster; } } // did we find at least one non-empty cluster ? if (selected == null) { throw new ConvergenceException(LocalizedFormats.EMPTY_CLUSTER_IN_K_MEANS); } // extract a random point from the cluster final List selectedPoints = selected.getPoints(); return selectedPoints.remove(random.nextInt(selectedPoints.size())); } /** * Get the point farthest to its cluster center * * @param clusters the {@link Cluster}s to search * @return point farthest to its cluster center * @throws ConvergenceException if clusters are all empty */ private T getFarthestPoint(final Collection> clusters) throws ConvergenceException { double maxDistance = Double.NEGATIVE_INFINITY; Cluster selectedCluster = null; int selectedPoint = -1; for (final Cluster cluster : clusters) { // get the farthest point final T center = cluster.getCenter(); final List points = cluster.getPoints(); for (int i = 0; i < points.size(); ++i) { final double distance = points.get(i).distanceFrom(center); if (distance > maxDistance) { maxDistance = distance; selectedCluster = cluster; selectedPoint = i; } } } // did we find at least one non-empty cluster ? if (selectedCluster == null) { throw new ConvergenceException(LocalizedFormats.EMPTY_CLUSTER_IN_K_MEANS); } return selectedCluster.getPoints().remove(selectedPoint); } /** * Returns the nearest {@link Cluster} to the given point * * @param type of the points to cluster * @param clusters the {@link Cluster}s to search * @param point the point to find the nearest {@link Cluster} for * @return the index of the nearest {@link Cluster} to the given point */ private static > int getNearestCluster(final Collection> clusters, final T point) { double minDistance = Double.MAX_VALUE; int clusterIndex = 0; int minCluster = 0; for (final Cluster c : clusters) { final double distance = point.distanceFrom(c.getCenter()); if (distance < minDistance) { minDistance = distance; minCluster = clusterIndex; } clusterIndex++; } return minCluster; } } ././@LongLink100644 0 0 150 12126630647 10256 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/stat/clustering/EuclideanIntegerPoint.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/stat/clustering/EuclideanIntegerPoint.j100644 1750 1750 6272 12126627717 32264 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.stat.clustering; import java.io.Serializable; import java.util.Arrays; import java.util.Collection; import org.apache.commons.math3.util.MathArrays; /** * A simple implementation of {@link Clusterable} for points with integer coordinates. * @version $Id: EuclideanIntegerPoint.java 1461871 2013-03-27 22:01:25Z tn $ * @since 2.0 * @deprecated As of 3.2 (to be removed in 4.0), * use {@link org.apache.commons.math3.ml.clustering.DoublePoint} instead */ @Deprecated public class EuclideanIntegerPoint implements Clusterable, Serializable { /** Serializable version identifier. */ private static final long serialVersionUID = 3946024775784901369L; /** Point coordinates. */ private final int[] point; /** * Build an instance wrapping an integer array. *

                              The wrapped array is referenced, it is not copied.

                              * @param point the n-dimensional point in integer space */ public EuclideanIntegerPoint(final int[] point) { this.point = point; } /** * Get the n-dimensional point in integer space. * @return a reference (not a copy!) to the wrapped array */ public int[] getPoint() { return point; } /** {@inheritDoc} */ public double distanceFrom(final EuclideanIntegerPoint p) { return MathArrays.distance(point, p.getPoint()); } /** {@inheritDoc} */ public EuclideanIntegerPoint centroidOf(final Collection points) { int[] centroid = new int[getPoint().length]; for (EuclideanIntegerPoint p : points) { for (int i = 0; i < centroid.length; i++) { centroid[i] += p.getPoint()[i]; } } for (int i = 0; i < centroid.length; i++) { centroid[i] /= points.size(); } return new EuclideanIntegerPoint(centroid); } /** {@inheritDoc} */ @Override public boolean equals(final Object other) { if (!(other instanceof EuclideanIntegerPoint)) { return false; } return Arrays.equals(point, ((EuclideanIntegerPoint) other).point); } /** {@inheritDoc} */ @Override public int hashCode() { return Arrays.hashCode(point); } /** * {@inheritDoc} * @since 2.1 */ @Override public String toString() { return Arrays.toString(point); } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/stat/clustering/Cluster.java100644 1750 1750 4475 12126627717 30157 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.stat.clustering; import java.io.Serializable; import java.util.ArrayList; import java.util.List; /** * Cluster holding a set of {@link Clusterable} points. * @param the type of points that can be clustered * @version $Id: Cluster.java 1461871 2013-03-27 22:01:25Z tn $ * @since 2.0 * @deprecated As of 3.2 (to be removed in 4.0), * use {@link org.apache.commons.math3.ml.clustering.Cluster} instead */ @Deprecated public class Cluster> implements Serializable { /** Serializable version identifier. */ private static final long serialVersionUID = -3442297081515880464L; /** The points contained in this cluster. */ private final List points; /** Center of the cluster. */ private final T center; /** * Build a cluster centered at a specified point. * @param center the point which is to be the center of this cluster */ public Cluster(final T center) { this.center = center; points = new ArrayList(); } /** * Add a point to this cluster. * @param point point to add */ public void addPoint(final T point) { points.add(point); } /** * Get the points contained in the cluster. * @return points contained in the cluster */ public List getPoints() { return points; } /** * Get the point chosen to be the center of this cluster. * @return chosen cluster center */ public T getCenter() { return center; } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/stat/clustering/DBSCANClusterer.java100644 1750 1750 20166 12126627717 31374 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.stat.clustering; import java.util.ArrayList; import java.util.Collection; import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Set; import org.apache.commons.math3.exception.NotPositiveException; import org.apache.commons.math3.exception.NullArgumentException; import org.apache.commons.math3.util.MathUtils; /** * DBSCAN (density-based spatial clustering of applications with noise) algorithm. *

                              * The DBSCAN algorithm forms clusters based on the idea of density connectivity, i.e. * a point p is density connected to another point q, if there exists a chain of * points pi, with i = 1 .. n and p1 = p and pn = q, * such that each pair <pi, pi+1> is directly density-reachable. * A point q is directly density-reachable from point p if it is in the ε-neighborhood * of this point. *

                              * Any point that is not density-reachable from a formed cluster is treated as noise, and * will thus not be present in the result. *

                              * The algorithm requires two parameters: *

                                *
                              • eps: the distance that defines the ε-neighborhood of a point *
                              • minPoints: the minimum number of density-connected points required to form a cluster *
                              *

                              * Note: as DBSCAN is not a centroid-based clustering algorithm, the resulting * {@link Cluster} objects will have no defined center, i.e. {@link Cluster#getCenter()} will * return {@code null}. * * @param type of the points to cluster * @see DBSCAN (wikipedia) * @see * A Density-Based Algorithm for Discovering Clusters in Large Spatial Databases with Noise * @version $Id: DBSCANClusterer.java 1461871 2013-03-27 22:01:25Z tn $ * @since 3.1 * @deprecated As of 3.2 (to be removed in 4.0), * use {@link org.apache.commons.math3.ml.clustering.DBSCANClusterer} instead */ @Deprecated public class DBSCANClusterer> { /** Maximum radius of the neighborhood to be considered. */ private final double eps; /** Minimum number of points needed for a cluster. */ private final int minPts; /** Status of a point during the clustering process. */ private enum PointStatus { /** The point has is considered to be noise. */ NOISE, /** The point is already part of a cluster. */ PART_OF_CLUSTER } /** * Creates a new instance of a DBSCANClusterer. * * @param eps maximum radius of the neighborhood to be considered * @param minPts minimum number of points needed for a cluster * @throws NotPositiveException if {@code eps < 0.0} or {@code minPts < 0} */ public DBSCANClusterer(final double eps, final int minPts) throws NotPositiveException { if (eps < 0.0d) { throw new NotPositiveException(eps); } if (minPts < 0) { throw new NotPositiveException(minPts); } this.eps = eps; this.minPts = minPts; } /** * Returns the maximum radius of the neighborhood to be considered. * * @return maximum radius of the neighborhood */ public double getEps() { return eps; } /** * Returns the minimum number of points needed for a cluster. * * @return minimum number of points needed for a cluster */ public int getMinPts() { return minPts; } /** * Performs DBSCAN cluster analysis. *

                              * Note: as DBSCAN is not a centroid-based clustering algorithm, the resulting * {@link Cluster} objects will have no defined center, i.e. {@link Cluster#getCenter()} will * return {@code null}. * * @param points the points to cluster * @return the list of clusters * @throws NullArgumentException if the data points are null */ public List> cluster(final Collection points) throws NullArgumentException { // sanity checks MathUtils.checkNotNull(points); final List> clusters = new ArrayList>(); final Map, PointStatus> visited = new HashMap, PointStatus>(); for (final T point : points) { if (visited.get(point) != null) { continue; } final List neighbors = getNeighbors(point, points); if (neighbors.size() >= minPts) { // DBSCAN does not care about center points final Cluster cluster = new Cluster(null); clusters.add(expandCluster(cluster, point, neighbors, points, visited)); } else { visited.put(point, PointStatus.NOISE); } } return clusters; } /** * Expands the cluster to include density-reachable items. * * @param cluster Cluster to expand * @param point Point to add to cluster * @param neighbors List of neighbors * @param points the data set * @param visited the set of already visited points * @return the expanded cluster */ private Cluster expandCluster(final Cluster cluster, final T point, final List neighbors, final Collection points, final Map, PointStatus> visited) { cluster.addPoint(point); visited.put(point, PointStatus.PART_OF_CLUSTER); List seeds = new ArrayList(neighbors); int index = 0; while (index < seeds.size()) { final T current = seeds.get(index); PointStatus pStatus = visited.get(current); // only check non-visited points if (pStatus == null) { final List currentNeighbors = getNeighbors(current, points); if (currentNeighbors.size() >= minPts) { seeds = merge(seeds, currentNeighbors); } } if (pStatus != PointStatus.PART_OF_CLUSTER) { visited.put(current, PointStatus.PART_OF_CLUSTER); cluster.addPoint(current); } index++; } return cluster; } /** * Returns a list of density-reachable neighbors of a {@code point}. * * @param point the point to look for * @param points possible neighbors * @return the List of neighbors */ private List getNeighbors(final T point, final Collection points) { final List neighbors = new ArrayList(); for (final T neighbor : points) { if (point != neighbor && neighbor.distanceFrom(point) <= eps) { neighbors.add(neighbor); } } return neighbors; } /** * Merges two lists together. * * @param one first list * @param two second list * @return merged lists */ private List merge(final List one, final List two) { final Set oneSet = new HashSet(one); for (T item : two) { if (!oneSet.contains(item)) { one.add(item); } } return one; } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/stat/clustering/Clusterable.java100644 1750 1750 3265 12126627717 30777 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.stat.clustering; import java.util.Collection; /** * Interface for points that can be clustered together. * @param the type of point that can be clustered * @version $Id: Clusterable.java 1461871 2013-03-27 22:01:25Z tn $ * @since 2.0 * @deprecated As of 3.2 (to be removed in 4.0), * use {@link org.apache.commons.math3.ml.clustering.Clusterable} instead */ @Deprecated public interface Clusterable { /** * Returns the distance from the given point. * * @param p the point to compute the distance from * @return the distance from the given point */ double distanceFrom(T p); /** * Returns the centroid of the given Collection of points. * * @param p the Collection of points to compute the centroid of * @return the centroid of the given Collection of Points */ T centroidOf(Collection p); } ././@LongLink100644 0 0 147 12126630647 10264 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/stat/clustering/EuclideanDoublePoint.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/stat/clustering/EuclideanDoublePoint.ja100644 1750 1750 6274 12126627717 32244 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.stat.clustering; import java.io.Serializable; import java.util.Collection; import java.util.Arrays; import org.apache.commons.math3.util.MathArrays; /** * A simple implementation of {@link Clusterable} for points with double coordinates. * @version $Id: EuclideanDoublePoint.java 1461871 2013-03-27 22:01:25Z tn $ * @since 3.1 * @deprecated As of 3.2 (to be removed in 4.0), * use {@link org.apache.commons.math3.ml.clustering.DoublePoint} instead */ @Deprecated public class EuclideanDoublePoint implements Clusterable, Serializable { /** Serializable version identifier. */ private static final long serialVersionUID = 8026472786091227632L; /** Point coordinates. */ private final double[] point; /** * Build an instance wrapping an integer array. *

                              * The wrapped array is referenced, it is not copied. * * @param point the n-dimensional point in integer space */ public EuclideanDoublePoint(final double[] point) { this.point = point; } /** {@inheritDoc} */ public EuclideanDoublePoint centroidOf(final Collection points) { final double[] centroid = new double[getPoint().length]; for (final EuclideanDoublePoint p : points) { for (int i = 0; i < centroid.length; i++) { centroid[i] += p.getPoint()[i]; } } for (int i = 0; i < centroid.length; i++) { centroid[i] /= points.size(); } return new EuclideanDoublePoint(centroid); } /** {@inheritDoc} */ public double distanceFrom(final EuclideanDoublePoint p) { return MathArrays.distance(point, p.getPoint()); } /** {@inheritDoc} */ @Override public boolean equals(final Object other) { if (!(other instanceof EuclideanDoublePoint)) { return false; } return Arrays.equals(point, ((EuclideanDoublePoint) other).point); } /** * Get the n-dimensional point in integer space. * * @return a reference (not a copy!) to the wrapped array */ public double[] getPoint() { return point; } /** {@inheritDoc} */ @Override public int hashCode() { return Arrays.hashCode(point); } /** {@inheritDoc} */ @Override public String toString() { return Arrays.toString(point); } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/stat/StatUtils.java100644 1750 1750 102260 12126627717 26342 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.stat; import org.apache.commons.math3.exception.MathIllegalArgumentException; import org.apache.commons.math3.exception.NumberIsTooSmallException; import org.apache.commons.math3.exception.DimensionMismatchException; import org.apache.commons.math3.exception.NoDataException; import org.apache.commons.math3.exception.util.LocalizedFormats; import org.apache.commons.math3.stat.descriptive.DescriptiveStatistics; import org.apache.commons.math3.stat.descriptive.UnivariateStatistic; import org.apache.commons.math3.stat.descriptive.moment.GeometricMean; import org.apache.commons.math3.stat.descriptive.moment.Mean; import org.apache.commons.math3.stat.descriptive.moment.Variance; import org.apache.commons.math3.stat.descriptive.rank.Max; import org.apache.commons.math3.stat.descriptive.rank.Min; import org.apache.commons.math3.stat.descriptive.rank.Percentile; import org.apache.commons.math3.stat.descriptive.summary.Product; import org.apache.commons.math3.stat.descriptive.summary.Sum; import org.apache.commons.math3.stat.descriptive.summary.SumOfLogs; import org.apache.commons.math3.stat.descriptive.summary.SumOfSquares; /** * StatUtils provides static methods for computing statistics based on data * stored in double[] arrays. * * @version $Id: StatUtils.java 1416643 2012-12-03 19:37:14Z tn $ */ public final class StatUtils { /** sum */ private static final UnivariateStatistic SUM = new Sum(); /** sumSq */ private static final UnivariateStatistic SUM_OF_SQUARES = new SumOfSquares(); /** prod */ private static final UnivariateStatistic PRODUCT = new Product(); /** sumLog */ private static final UnivariateStatistic SUM_OF_LOGS = new SumOfLogs(); /** min */ private static final UnivariateStatistic MIN = new Min(); /** max */ private static final UnivariateStatistic MAX = new Max(); /** mean */ private static final UnivariateStatistic MEAN = new Mean(); /** variance */ private static final Variance VARIANCE = new Variance(); /** percentile */ private static final Percentile PERCENTILE = new Percentile(); /** geometric mean */ private static final GeometricMean GEOMETRIC_MEAN = new GeometricMean(); /** * Private Constructor */ private StatUtils() { } /** * Returns the sum of the values in the input array, or * Double.NaN if the array is empty. *

                              * Throws IllegalArgumentException if the input array * is null.

                              * * @param values array of values to sum * @return the sum of the values or Double.NaN if the array * is empty * @throws MathIllegalArgumentException if the array is null */ public static double sum(final double[] values) throws MathIllegalArgumentException { return SUM.evaluate(values); } /** * Returns the sum of the entries in the specified portion of * the input array, or Double.NaN if the designated subarray * is empty. *

                              * Throws IllegalArgumentException if the array is null.

                              * * @param values the input array * @param begin index of the first array element to include * @param length the number of elements to include * @return the sum of the values or Double.NaN if length = 0 * @throws MathIllegalArgumentException if the array is null or the array index * parameters are not valid */ public static double sum(final double[] values, final int begin, final int length) throws MathIllegalArgumentException { return SUM.evaluate(values, begin, length); } /** * Returns the sum of the squares of the entries in the input array, or * Double.NaN if the array is empty. *

                              * Throws IllegalArgumentException if the array is null.

                              * * @param values input array * @return the sum of the squared values or Double.NaN if the * array is empty * @throws MathIllegalArgumentException if the array is null */ public static double sumSq(final double[] values) throws MathIllegalArgumentException { return SUM_OF_SQUARES.evaluate(values); } /** * Returns the sum of the squares of the entries in the specified portion of * the input array, or Double.NaN if the designated subarray * is empty. *

                              * Throws IllegalArgumentException if the array is null.

                              * * @param values the input array * @param begin index of the first array element to include * @param length the number of elements to include * @return the sum of the squares of the values or Double.NaN if length = 0 * @throws MathIllegalArgumentException if the array is null or the array index * parameters are not valid */ public static double sumSq(final double[] values, final int begin, final int length) throws MathIllegalArgumentException { return SUM_OF_SQUARES.evaluate(values, begin, length); } /** * Returns the product of the entries in the input array, or * Double.NaN if the array is empty. *

                              * Throws IllegalArgumentException if the array is null.

                              * * @param values the input array * @return the product of the values or Double.NaN if the array is empty * @throws MathIllegalArgumentException if the array is null */ public static double product(final double[] values) throws MathIllegalArgumentException { return PRODUCT.evaluate(values); } /** * Returns the product of the entries in the specified portion of * the input array, or Double.NaN if the designated subarray * is empty. *

                              * Throws IllegalArgumentException if the array is null.

                              * * @param values the input array * @param begin index of the first array element to include * @param length the number of elements to include * @return the product of the values or Double.NaN if length = 0 * @throws MathIllegalArgumentException if the array is null or the array index * parameters are not valid */ public static double product(final double[] values, final int begin, final int length) throws MathIllegalArgumentException { return PRODUCT.evaluate(values, begin, length); } /** * Returns the sum of the natural logs of the entries in the input array, or * Double.NaN if the array is empty. *

                              * Throws IllegalArgumentException if the array is null.

                              *

                              * See {@link org.apache.commons.math3.stat.descriptive.summary.SumOfLogs}. *

                              * * @param values the input array * @return the sum of the natural logs of the values or Double.NaN if * the array is empty * @throws MathIllegalArgumentException if the array is null */ public static double sumLog(final double[] values) throws MathIllegalArgumentException { return SUM_OF_LOGS.evaluate(values); } /** * Returns the sum of the natural logs of the entries in the specified portion of * the input array, or Double.NaN if the designated subarray * is empty. *

                              * Throws IllegalArgumentException if the array is null.

                              *

                              * See {@link org.apache.commons.math3.stat.descriptive.summary.SumOfLogs}. *

                              * * @param values the input array * @param begin index of the first array element to include * @param length the number of elements to include * @return the sum of the natural logs of the values or Double.NaN if * length = 0 * @throws MathIllegalArgumentException if the array is null or the array index * parameters are not valid */ public static double sumLog(final double[] values, final int begin, final int length) throws MathIllegalArgumentException { return SUM_OF_LOGS.evaluate(values, begin, length); } /** * Returns the arithmetic mean of the entries in the input array, or * Double.NaN if the array is empty. *

                              * Throws IllegalArgumentException if the array is null.

                              *

                              * See {@link org.apache.commons.math3.stat.descriptive.moment.Mean} for * details on the computing algorithm.

                              * * @param values the input array * @return the mean of the values or Double.NaN if the array is empty * @throws MathIllegalArgumentException if the array is null */ public static double mean(final double[] values) throws MathIllegalArgumentException { return MEAN.evaluate(values); } /** * Returns the arithmetic mean of the entries in the specified portion of * the input array, or Double.NaN if the designated subarray * is empty. *

                              * Throws IllegalArgumentException if the array is null.

                              *

                              * See {@link org.apache.commons.math3.stat.descriptive.moment.Mean} for * details on the computing algorithm.

                              * * @param values the input array * @param begin index of the first array element to include * @param length the number of elements to include * @return the mean of the values or Double.NaN if length = 0 * @throws MathIllegalArgumentException if the array is null or the array index * parameters are not valid */ public static double mean(final double[] values, final int begin, final int length) throws MathIllegalArgumentException { return MEAN.evaluate(values, begin, length); } /** * Returns the geometric mean of the entries in the input array, or * Double.NaN if the array is empty. *

                              * Throws IllegalArgumentException if the array is null.

                              *

                              * See {@link org.apache.commons.math3.stat.descriptive.moment.GeometricMean} * for details on the computing algorithm.

                              * * @param values the input array * @return the geometric mean of the values or Double.NaN if the array is empty * @throws MathIllegalArgumentException if the array is null */ public static double geometricMean(final double[] values) throws MathIllegalArgumentException { return GEOMETRIC_MEAN.evaluate(values); } /** * Returns the geometric mean of the entries in the specified portion of * the input array, or Double.NaN if the designated subarray * is empty. *

                              * Throws IllegalArgumentException if the array is null.

                              *

                              * See {@link org.apache.commons.math3.stat.descriptive.moment.GeometricMean} * for details on the computing algorithm.

                              * * @param values the input array * @param begin index of the first array element to include * @param length the number of elements to include * @return the geometric mean of the values or Double.NaN if length = 0 * @throws MathIllegalArgumentException if the array is null or the array index * parameters are not valid */ public static double geometricMean(final double[] values, final int begin, final int length) throws MathIllegalArgumentException { return GEOMETRIC_MEAN.evaluate(values, begin, length); } /** * Returns the variance of the entries in the input array, or * Double.NaN if the array is empty. * *

                              This method returns the bias-corrected sample variance (using {@code n - 1} in * the denominator). Use {@link #populationVariance(double[])} for the non-bias-corrected * population variance.

                              *

                              * See {@link org.apache.commons.math3.stat.descriptive.moment.Variance} for * details on the computing algorithm.

                              *

                              * Returns 0 for a single-value (i.e. length = 1) sample.

                              *

                              * Throws MathIllegalArgumentException if the array is null.

                              * * @param values the input array * @return the variance of the values or Double.NaN if the array is empty * @throws MathIllegalArgumentException if the array is null */ public static double variance(final double[] values) throws MathIllegalArgumentException { return VARIANCE.evaluate(values); } /** * Returns the variance of the entries in the specified portion of * the input array, or Double.NaN if the designated subarray * is empty. * *

                              This method returns the bias-corrected sample variance (using {@code n - 1} in * the denominator). Use {@link #populationVariance(double[], int, int)} for the non-bias-corrected * population variance.

                              *

                              * See {@link org.apache.commons.math3.stat.descriptive.moment.Variance} for * details on the computing algorithm.

                              *

                              * Returns 0 for a single-value (i.e. length = 1) sample.

                              *

                              * Throws MathIllegalArgumentException if the array is null or the * array index parameters are not valid.

                              * * @param values the input array * @param begin index of the first array element to include * @param length the number of elements to include * @return the variance of the values or Double.NaN if length = 0 * @throws MathIllegalArgumentException if the array is null or the array index * parameters are not valid */ public static double variance(final double[] values, final int begin, final int length) throws MathIllegalArgumentException { return VARIANCE.evaluate(values, begin, length); } /** * Returns the variance of the entries in the specified portion of * the input array, using the precomputed mean value. Returns * Double.NaN if the designated subarray is empty. * *

                              This method returns the bias-corrected sample variance (using {@code n - 1} in * the denominator). Use {@link #populationVariance(double[], double, int, int)} for the non-bias-corrected * population variance.

                              *

                              * See {@link org.apache.commons.math3.stat.descriptive.moment.Variance} for * details on the computing algorithm.

                              *

                              * The formula used assumes that the supplied mean value is the arithmetic * mean of the sample data, not a known population parameter. This method * is supplied only to save computation when the mean has already been * computed.

                              *

                              * Returns 0 for a single-value (i.e. length = 1) sample.

                              *

                              * Throws MathIllegalArgumentException if the array is null or the * array index parameters are not valid.

                              * * @param values the input array * @param mean the precomputed mean value * @param begin index of the first array element to include * @param length the number of elements to include * @return the variance of the values or Double.NaN if length = 0 * @throws MathIllegalArgumentException if the array is null or the array index * parameters are not valid */ public static double variance(final double[] values, final double mean, final int begin, final int length) throws MathIllegalArgumentException { return VARIANCE.evaluate(values, mean, begin, length); } /** * Returns the variance of the entries in the input array, using the * precomputed mean value. Returns Double.NaN if the array * is empty. * *

                              This method returns the bias-corrected sample variance (using {@code n - 1} in * the denominator). Use {@link #populationVariance(double[], double)} for the non-bias-corrected * population variance.

                              *

                              * See {@link org.apache.commons.math3.stat.descriptive.moment.Variance} for * details on the computing algorithm.

                              *

                              * The formula used assumes that the supplied mean value is the arithmetic * mean of the sample data, not a known population parameter. This method * is supplied only to save computation when the mean has already been * computed.

                              *

                              * Returns 0 for a single-value (i.e. length = 1) sample.

                              *

                              * Throws MathIllegalArgumentException if the array is null.

                              * * @param values the input array * @param mean the precomputed mean value * @return the variance of the values or Double.NaN if the array is empty * @throws MathIllegalArgumentException if the array is null */ public static double variance(final double[] values, final double mean) throws MathIllegalArgumentException { return VARIANCE.evaluate(values, mean); } /** * Returns the * population variance of the entries in the input array, or * Double.NaN if the array is empty. *

                              * See {@link org.apache.commons.math3.stat.descriptive.moment.Variance} for * details on the formula and computing algorithm.

                              *

                              * Returns 0 for a single-value (i.e. length = 1) sample.

                              *

                              * Throws MathIllegalArgumentException if the array is null.

                              * * @param values the input array * @return the population variance of the values or Double.NaN if the array is empty * @throws MathIllegalArgumentException if the array is null */ public static double populationVariance(final double[] values) throws MathIllegalArgumentException { return new Variance(false).evaluate(values); } /** * Returns the * population variance of the entries in the specified portion of * the input array, or Double.NaN if the designated subarray * is empty. *

                              * See {@link org.apache.commons.math3.stat.descriptive.moment.Variance} for * details on the computing algorithm.

                              *

                              * Returns 0 for a single-value (i.e. length = 1) sample.

                              *

                              * Throws MathIllegalArgumentException if the array is null or the * array index parameters are not valid.

                              * * @param values the input array * @param begin index of the first array element to include * @param length the number of elements to include * @return the population variance of the values or Double.NaN if length = 0 * @throws MathIllegalArgumentException if the array is null or the array index * parameters are not valid */ public static double populationVariance(final double[] values, final int begin, final int length) throws MathIllegalArgumentException { return new Variance(false).evaluate(values, begin, length); } /** * Returns the * population variance of the entries in the specified portion of * the input array, using the precomputed mean value. Returns * Double.NaN if the designated subarray is empty. *

                              * See {@link org.apache.commons.math3.stat.descriptive.moment.Variance} for * details on the computing algorithm.

                              *

                              * The formula used assumes that the supplied mean value is the arithmetic * mean of the sample data, not a known population parameter. This method * is supplied only to save computation when the mean has already been * computed.

                              *

                              * Returns 0 for a single-value (i.e. length = 1) sample.

                              *

                              * Throws MathIllegalArgumentException if the array is null or the * array index parameters are not valid.

                              * * @param values the input array * @param mean the precomputed mean value * @param begin index of the first array element to include * @param length the number of elements to include * @return the population variance of the values or Double.NaN if length = 0 * @throws MathIllegalArgumentException if the array is null or the array index * parameters are not valid */ public static double populationVariance(final double[] values, final double mean, final int begin, final int length) throws MathIllegalArgumentException { return new Variance(false).evaluate(values, mean, begin, length); } /** * Returns the * population variance of the entries in the input array, using the * precomputed mean value. Returns Double.NaN if the array * is empty. *

                              * See {@link org.apache.commons.math3.stat.descriptive.moment.Variance} for * details on the computing algorithm.

                              *

                              * The formula used assumes that the supplied mean value is the arithmetic * mean of the sample data, not a known population parameter. This method * is supplied only to save computation when the mean has already been * computed.

                              *

                              * Returns 0 for a single-value (i.e. length = 1) sample.

                              *

                              * Throws MathIllegalArgumentException if the array is null.

                              * * @param values the input array * @param mean the precomputed mean value * @return the population variance of the values or Double.NaN if the array is empty * @throws MathIllegalArgumentException if the array is null */ public static double populationVariance(final double[] values, final double mean) throws MathIllegalArgumentException { return new Variance(false).evaluate(values, mean); } /** * Returns the maximum of the entries in the input array, or * Double.NaN if the array is empty. *

                              * Throws MathIllegalArgumentException if the array is null.

                              *

                              *

                                *
                              • The result is NaN iff all values are NaN * (i.e. NaN values have no impact on the value of the statistic).
                              • *
                              • If any of the values equals Double.POSITIVE_INFINITY, * the result is Double.POSITIVE_INFINITY.
                              • *

                              * * @param values the input array * @return the maximum of the values or Double.NaN if the array is empty * @throws MathIllegalArgumentException if the array is null */ public static double max(final double[] values) throws MathIllegalArgumentException { return MAX.evaluate(values); } /** * Returns the maximum of the entries in the specified portion of * the input array, or Double.NaN if the designated subarray * is empty. *

                              * Throws MathIllegalArgumentException if the array is null or * the array index parameters are not valid.

                              *

                              *

                                *
                              • The result is NaN iff all values are NaN * (i.e. NaN values have no impact on the value of the statistic).
                              • *
                              • If any of the values equals Double.POSITIVE_INFINITY, * the result is Double.POSITIVE_INFINITY.
                              • *

                              * * @param values the input array * @param begin index of the first array element to include * @param length the number of elements to include * @return the maximum of the values or Double.NaN if length = 0 * @throws MathIllegalArgumentException if the array is null or the array index * parameters are not valid */ public static double max(final double[] values, final int begin, final int length) throws MathIllegalArgumentException { return MAX.evaluate(values, begin, length); } /** * Returns the minimum of the entries in the input array, or * Double.NaN if the array is empty. *

                              * Throws MathIllegalArgumentException if the array is null.

                              *

                              *

                                *
                              • The result is NaN iff all values are NaN * (i.e. NaN values have no impact on the value of the statistic).
                              • *
                              • If any of the values equals Double.NEGATIVE_INFINITY, * the result is Double.NEGATIVE_INFINITY.
                              • *

                              * * @param values the input array * @return the minimum of the values or Double.NaN if the array is empty * @throws MathIllegalArgumentException if the array is null */ public static double min(final double[] values) throws MathIllegalArgumentException { return MIN.evaluate(values); } /** * Returns the minimum of the entries in the specified portion of * the input array, or Double.NaN if the designated subarray * is empty. *

                              * Throws MathIllegalArgumentException if the array is null or * the array index parameters are not valid.

                              *

                              *

                                *
                              • The result is NaN iff all values are NaN * (i.e. NaN values have no impact on the value of the statistic).
                              • *
                              • If any of the values equals Double.NEGATIVE_INFINITY, * the result is Double.NEGATIVE_INFINITY.
                              • *

                              * * @param values the input array * @param begin index of the first array element to include * @param length the number of elements to include * @return the minimum of the values or Double.NaN if length = 0 * @throws MathIllegalArgumentException if the array is null or the array index * parameters are not valid */ public static double min(final double[] values, final int begin, final int length) throws MathIllegalArgumentException { return MIN.evaluate(values, begin, length); } /** * Returns an estimate of the pth percentile of the values * in the values array. *

                              *

                                *
                              • Returns Double.NaN if values has length * 0
                              • *
                              • Returns (for any value of p) values[0] * if values has length 1
                              • *
                              • Throws IllegalArgumentException if values * is null or p is not a valid quantile value (p must be greater than 0 * and less than or equal to 100)
                              • *

                              *

                              * See {@link org.apache.commons.math3.stat.descriptive.rank.Percentile} for * a description of the percentile estimation algorithm used.

                              * * @param values input array of values * @param p the percentile value to compute * @return the percentile value or Double.NaN if the array is empty * @throws MathIllegalArgumentException if values is null * or p is invalid */ public static double percentile(final double[] values, final double p) throws MathIllegalArgumentException { return PERCENTILE.evaluate(values,p); } /** * Returns an estimate of the pth percentile of the values * in the values array, starting with the element in (0-based) * position begin in the array and including length * values. *

                              *

                                *
                              • Returns Double.NaN if length = 0
                              • *
                              • Returns (for any value of p) values[begin] * if length = 1
                              • *
                              • Throws MathIllegalArgumentException if values * is null , begin or length is invalid, or * p is not a valid quantile value (p must be greater than 0 * and less than or equal to 100)
                              • *

                              *

                              * See {@link org.apache.commons.math3.stat.descriptive.rank.Percentile} for * a description of the percentile estimation algorithm used.

                              * * @param values array of input values * @param p the percentile to compute * @param begin the first (0-based) element to include in the computation * @param length the number of array elements to include * @return the percentile value * @throws MathIllegalArgumentException if the parameters are not valid or the * input array is null */ public static double percentile(final double[] values, final int begin, final int length, final double p) throws MathIllegalArgumentException { return PERCENTILE.evaluate(values, begin, length, p); } /** * Returns the sum of the (signed) differences between corresponding elements of the * input arrays -- i.e., sum(sample1[i] - sample2[i]). * * @param sample1 the first array * @param sample2 the second array * @return sum of paired differences * @throws DimensionMismatchException if the arrays do not have the same * (positive) length. * @throws NoDataException if the sample arrays are empty. */ public static double sumDifference(final double[] sample1, final double[] sample2) throws DimensionMismatchException, NoDataException { int n = sample1.length; if (n != sample2.length) { throw new DimensionMismatchException(n, sample2.length); } if (n <= 0) { throw new NoDataException(LocalizedFormats.INSUFFICIENT_DIMENSION); } double result = 0; for (int i = 0; i < n; i++) { result += sample1[i] - sample2[i]; } return result; } /** * Returns the mean of the (signed) differences between corresponding elements of the * input arrays -- i.e., sum(sample1[i] - sample2[i]) / sample1.length. * * @param sample1 the first array * @param sample2 the second array * @return mean of paired differences * @throws DimensionMismatchException if the arrays do not have the same * (positive) length. * @throws NoDataException if the sample arrays are empty. */ public static double meanDifference(final double[] sample1, final double[] sample2) throws DimensionMismatchException, NoDataException{ return sumDifference(sample1, sample2) / sample1.length; } /** * Returns the variance of the (signed) differences between corresponding elements of the * input arrays -- i.e., var(sample1[i] - sample2[i]). * * @param sample1 the first array * @param sample2 the second array * @param meanDifference the mean difference between corresponding entries * @see #meanDifference(double[],double[]) * @return variance of paired differences * @throws DimensionMismatchException if the arrays do not have the same * length. * @throws NumberIsTooSmallException if the arrays length is less than 2. */ public static double varianceDifference(final double[] sample1, final double[] sample2, double meanDifference) throws DimensionMismatchException, NumberIsTooSmallException { double sum1 = 0d; double sum2 = 0d; double diff = 0d; int n = sample1.length; if (n != sample2.length) { throw new DimensionMismatchException(n, sample2.length); } if (n < 2) { throw new NumberIsTooSmallException(n, 2, true); } for (int i = 0; i < n; i++) { diff = sample1[i] - sample2[i]; sum1 += (diff - meanDifference) *(diff - meanDifference); sum2 += diff - meanDifference; } return (sum1 - (sum2 * sum2 / n)) / (n - 1); } /** * Normalize (standardize) the sample, so it is has a mean of 0 and a standard deviation of 1. * * @param sample Sample to normalize. * @return normalized (standardized) sample. * @since 2.2 */ public static double[] normalize(final double[] sample) { DescriptiveStatistics stats = new DescriptiveStatistics(); // Add the data from the series to stats for (int i = 0; i < sample.length; i++) { stats.addValue(sample[i]); } // Compute mean and standard deviation double mean = stats.getMean(); double standardDeviation = stats.getStandardDeviation(); // initialize the standardizedSample, which has the same length as the sample double[] standardizedSample = new double[sample.length]; for (int i = 0; i < sample.length; i++) { // z = (x- mean)/standardDeviation standardizedSample[i] = (sample[i] - mean) / standardDeviation; } return standardizedSample; } } ././@LongLink100644 0 0 161 12126630647 10260 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/stat/descriptive/MultivariateSummaryStatistics.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/stat/descriptive/MultivariateSummarySta100644 1750 1750 57501 12126627717 32472 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.stat.descriptive; import java.io.Serializable; import java.util.Arrays; import org.apache.commons.math3.exception.util.LocalizedFormats; import org.apache.commons.math3.exception.DimensionMismatchException; import org.apache.commons.math3.exception.MathIllegalStateException; import org.apache.commons.math3.linear.RealMatrix; import org.apache.commons.math3.stat.descriptive.moment.GeometricMean; import org.apache.commons.math3.stat.descriptive.moment.Mean; import org.apache.commons.math3.stat.descriptive.moment.VectorialCovariance; import org.apache.commons.math3.stat.descriptive.rank.Max; import org.apache.commons.math3.stat.descriptive.rank.Min; import org.apache.commons.math3.stat.descriptive.summary.Sum; import org.apache.commons.math3.stat.descriptive.summary.SumOfLogs; import org.apache.commons.math3.stat.descriptive.summary.SumOfSquares; import org.apache.commons.math3.util.MathUtils; import org.apache.commons.math3.util.MathArrays; import org.apache.commons.math3.util.Precision; import org.apache.commons.math3.util.FastMath; /** *

                              Computes summary statistics for a stream of n-tuples added using the * {@link #addValue(double[]) addValue} method. The data values are not stored * in memory, so this class can be used to compute statistics for very large * n-tuple streams.

                              * *

                              The {@link StorelessUnivariateStatistic} instances used to maintain * summary state and compute statistics are configurable via setters. * For example, the default implementation for the mean can be overridden by * calling {@link #setMeanImpl(StorelessUnivariateStatistic[])}. Actual * parameters to these methods must implement the * {@link StorelessUnivariateStatistic} interface and configuration must be * completed before addValue is called. No configuration is * necessary to use the default, commons-math provided implementations.

                              * *

                              To compute statistics for a stream of n-tuples, construct a * MultivariateStatistics instance with dimension n and then use * {@link #addValue(double[])} to add n-tuples. The getXxx * methods where Xxx is a statistic return an array of double * values, where for i = 0,...,n-1 the ith array element is the * value of the given statistic for data range consisting of the ith element of * each of the input n-tuples. For example, if addValue is called * with actual parameters {0, 1, 2}, then {3, 4, 5} and finally {6, 7, 8}, * getSum will return a three-element array with values * {0+3+6, 1+4+7, 2+5+8}

                              * *

                              Note: This class is not thread-safe. Use * {@link SynchronizedMultivariateSummaryStatistics} if concurrent access from multiple * threads is required.

                              * * @since 1.2 * @version $Id: MultivariateSummaryStatistics.java 1416643 2012-12-03 19:37:14Z tn $ */ public class MultivariateSummaryStatistics implements StatisticalMultivariateSummary, Serializable { /** Serialization UID */ private static final long serialVersionUID = 2271900808994826718L; /** Dimension of the data. */ private int k; /** Count of values that have been added */ private long n = 0; /** Sum statistic implementation - can be reset by setter. */ private StorelessUnivariateStatistic[] sumImpl; /** Sum of squares statistic implementation - can be reset by setter. */ private StorelessUnivariateStatistic[] sumSqImpl; /** Minimum statistic implementation - can be reset by setter. */ private StorelessUnivariateStatistic[] minImpl; /** Maximum statistic implementation - can be reset by setter. */ private StorelessUnivariateStatistic[] maxImpl; /** Sum of log statistic implementation - can be reset by setter. */ private StorelessUnivariateStatistic[] sumLogImpl; /** Geometric mean statistic implementation - can be reset by setter. */ private StorelessUnivariateStatistic[] geoMeanImpl; /** Mean statistic implementation - can be reset by setter. */ private StorelessUnivariateStatistic[] meanImpl; /** Covariance statistic implementation - cannot be reset. */ private VectorialCovariance covarianceImpl; /** * Construct a MultivariateSummaryStatistics instance * @param k dimension of the data * @param isCovarianceBiasCorrected if true, the unbiased sample * covariance is computed, otherwise the biased population covariance * is computed */ public MultivariateSummaryStatistics(int k, boolean isCovarianceBiasCorrected) { this.k = k; sumImpl = new StorelessUnivariateStatistic[k]; sumSqImpl = new StorelessUnivariateStatistic[k]; minImpl = new StorelessUnivariateStatistic[k]; maxImpl = new StorelessUnivariateStatistic[k]; sumLogImpl = new StorelessUnivariateStatistic[k]; geoMeanImpl = new StorelessUnivariateStatistic[k]; meanImpl = new StorelessUnivariateStatistic[k]; for (int i = 0; i < k; ++i) { sumImpl[i] = new Sum(); sumSqImpl[i] = new SumOfSquares(); minImpl[i] = new Min(); maxImpl[i] = new Max(); sumLogImpl[i] = new SumOfLogs(); geoMeanImpl[i] = new GeometricMean(); meanImpl[i] = new Mean(); } covarianceImpl = new VectorialCovariance(k, isCovarianceBiasCorrected); } /** * Add an n-tuple to the data * * @param value the n-tuple to add * @throws DimensionMismatchException if the length of the array * does not match the one used at construction */ public void addValue(double[] value) throws DimensionMismatchException { checkDimension(value.length); for (int i = 0; i < k; ++i) { double v = value[i]; sumImpl[i].increment(v); sumSqImpl[i].increment(v); minImpl[i].increment(v); maxImpl[i].increment(v); sumLogImpl[i].increment(v); geoMeanImpl[i].increment(v); meanImpl[i].increment(v); } covarianceImpl.increment(value); n++; } /** * Returns the dimension of the data * @return The dimension of the data */ public int getDimension() { return k; } /** * Returns the number of available values * @return The number of available values */ public long getN() { return n; } /** * Returns an array of the results of a statistic. * @param stats univariate statistic array * @return results array */ private double[] getResults(StorelessUnivariateStatistic[] stats) { double[] results = new double[stats.length]; for (int i = 0; i < results.length; ++i) { results[i] = stats[i].getResult(); } return results; } /** * Returns an array whose ith entry is the sum of the * ith entries of the arrays that have been added using * {@link #addValue(double[])} * * @return the array of component sums */ public double[] getSum() { return getResults(sumImpl); } /** * Returns an array whose ith entry is the sum of squares of the * ith entries of the arrays that have been added using * {@link #addValue(double[])} * * @return the array of component sums of squares */ public double[] getSumSq() { return getResults(sumSqImpl); } /** * Returns an array whose ith entry is the sum of logs of the * ith entries of the arrays that have been added using * {@link #addValue(double[])} * * @return the array of component log sums */ public double[] getSumLog() { return getResults(sumLogImpl); } /** * Returns an array whose ith entry is the mean of the * ith entries of the arrays that have been added using * {@link #addValue(double[])} * * @return the array of component means */ public double[] getMean() { return getResults(meanImpl); } /** * Returns an array whose ith entry is the standard deviation of the * ith entries of the arrays that have been added using * {@link #addValue(double[])} * * @return the array of component standard deviations */ public double[] getStandardDeviation() { double[] stdDev = new double[k]; if (getN() < 1) { Arrays.fill(stdDev, Double.NaN); } else if (getN() < 2) { Arrays.fill(stdDev, 0.0); } else { RealMatrix matrix = covarianceImpl.getResult(); for (int i = 0; i < k; ++i) { stdDev[i] = FastMath.sqrt(matrix.getEntry(i, i)); } } return stdDev; } /** * Returns the covariance matrix of the values that have been added. * * @return the covariance matrix */ public RealMatrix getCovariance() { return covarianceImpl.getResult(); } /** * Returns an array whose ith entry is the maximum of the * ith entries of the arrays that have been added using * {@link #addValue(double[])} * * @return the array of component maxima */ public double[] getMax() { return getResults(maxImpl); } /** * Returns an array whose ith entry is the minimum of the * ith entries of the arrays that have been added using * {@link #addValue(double[])} * * @return the array of component minima */ public double[] getMin() { return getResults(minImpl); } /** * Returns an array whose ith entry is the geometric mean of the * ith entries of the arrays that have been added using * {@link #addValue(double[])} * * @return the array of component geometric means */ public double[] getGeometricMean() { return getResults(geoMeanImpl); } /** * Generates a text report displaying * summary statistics from values that * have been added. * @return String with line feeds displaying statistics */ @Override public String toString() { final String separator = ", "; final String suffix = System.getProperty("line.separator"); StringBuilder outBuffer = new StringBuilder(); outBuffer.append("MultivariateSummaryStatistics:" + suffix); outBuffer.append("n: " + getN() + suffix); append(outBuffer, getMin(), "min: ", separator, suffix); append(outBuffer, getMax(), "max: ", separator, suffix); append(outBuffer, getMean(), "mean: ", separator, suffix); append(outBuffer, getGeometricMean(), "geometric mean: ", separator, suffix); append(outBuffer, getSumSq(), "sum of squares: ", separator, suffix); append(outBuffer, getSumLog(), "sum of logarithms: ", separator, suffix); append(outBuffer, getStandardDeviation(), "standard deviation: ", separator, suffix); outBuffer.append("covariance: " + getCovariance().toString() + suffix); return outBuffer.toString(); } /** * Append a text representation of an array to a buffer. * @param buffer buffer to fill * @param data data array * @param prefix text prefix * @param separator elements separator * @param suffix text suffix */ private void append(StringBuilder buffer, double[] data, String prefix, String separator, String suffix) { buffer.append(prefix); for (int i = 0; i < data.length; ++i) { if (i > 0) { buffer.append(separator); } buffer.append(data[i]); } buffer.append(suffix); } /** * Resets all statistics and storage */ public void clear() { this.n = 0; for (int i = 0; i < k; ++i) { minImpl[i].clear(); maxImpl[i].clear(); sumImpl[i].clear(); sumLogImpl[i].clear(); sumSqImpl[i].clear(); geoMeanImpl[i].clear(); meanImpl[i].clear(); } covarianceImpl.clear(); } /** * Returns true iff object is a MultivariateSummaryStatistics * instance and all statistics have the same values as this. * @param object the object to test equality against. * @return true if object equals this */ @Override public boolean equals(Object object) { if (object == this ) { return true; } if (object instanceof MultivariateSummaryStatistics == false) { return false; } MultivariateSummaryStatistics stat = (MultivariateSummaryStatistics) object; return MathArrays.equalsIncludingNaN(stat.getGeometricMean(), getGeometricMean()) && MathArrays.equalsIncludingNaN(stat.getMax(), getMax()) && MathArrays.equalsIncludingNaN(stat.getMean(), getMean()) && MathArrays.equalsIncludingNaN(stat.getMin(), getMin()) && Precision.equalsIncludingNaN(stat.getN(), getN()) && MathArrays.equalsIncludingNaN(stat.getSum(), getSum()) && MathArrays.equalsIncludingNaN(stat.getSumSq(), getSumSq()) && MathArrays.equalsIncludingNaN(stat.getSumLog(), getSumLog()) && stat.getCovariance().equals( getCovariance()); } /** * Returns hash code based on values of statistics * * @return hash code */ @Override public int hashCode() { int result = 31 + MathUtils.hash(getGeometricMean()); result = result * 31 + MathUtils.hash(getGeometricMean()); result = result * 31 + MathUtils.hash(getMax()); result = result * 31 + MathUtils.hash(getMean()); result = result * 31 + MathUtils.hash(getMin()); result = result * 31 + MathUtils.hash(getN()); result = result * 31 + MathUtils.hash(getSum()); result = result * 31 + MathUtils.hash(getSumSq()); result = result * 31 + MathUtils.hash(getSumLog()); result = result * 31 + getCovariance().hashCode(); return result; } // Getters and setters for statistics implementations /** * Sets statistics implementations. * @param newImpl new implementations for statistics * @param oldImpl old implementations for statistics * @throws DimensionMismatchException if the array dimension * does not match the one used at construction * @throws MathIllegalStateException if data has already been added * (i.e. if n > 0) */ private void setImpl(StorelessUnivariateStatistic[] newImpl, StorelessUnivariateStatistic[] oldImpl) throws MathIllegalStateException, DimensionMismatchException { checkEmpty(); checkDimension(newImpl.length); System.arraycopy(newImpl, 0, oldImpl, 0, newImpl.length); } /** * Returns the currently configured Sum implementation * * @return the StorelessUnivariateStatistic implementing the sum */ public StorelessUnivariateStatistic[] getSumImpl() { return sumImpl.clone(); } /** *

                              Sets the implementation for the Sum.

                              *

                              This method must be activated before any data has been added - i.e., * before {@link #addValue(double[]) addValue} has been used to add data; * otherwise an IllegalStateException will be thrown.

                              * * @param sumImpl the StorelessUnivariateStatistic instance to use * for computing the Sum * @throws DimensionMismatchException if the array dimension * does not match the one used at construction * @throws MathIllegalStateException if data has already been added * (i.e if n > 0) */ public void setSumImpl(StorelessUnivariateStatistic[] sumImpl) throws MathIllegalStateException, DimensionMismatchException { setImpl(sumImpl, this.sumImpl); } /** * Returns the currently configured sum of squares implementation * * @return the StorelessUnivariateStatistic implementing the sum of squares */ public StorelessUnivariateStatistic[] getSumsqImpl() { return sumSqImpl.clone(); } /** *

                              Sets the implementation for the sum of squares.

                              *

                              This method must be activated before any data has been added - i.e., * before {@link #addValue(double[]) addValue} has been used to add data; * otherwise an IllegalStateException will be thrown.

                              * * @param sumsqImpl the StorelessUnivariateStatistic instance to use * for computing the sum of squares * @throws DimensionMismatchException if the array dimension * does not match the one used at construction * @throws MathIllegalStateException if data has already been added * (i.e if n > 0) */ public void setSumsqImpl(StorelessUnivariateStatistic[] sumsqImpl) throws MathIllegalStateException, DimensionMismatchException { setImpl(sumsqImpl, this.sumSqImpl); } /** * Returns the currently configured minimum implementation * * @return the StorelessUnivariateStatistic implementing the minimum */ public StorelessUnivariateStatistic[] getMinImpl() { return minImpl.clone(); } /** *

                              Sets the implementation for the minimum.

                              *

                              This method must be activated before any data has been added - i.e., * before {@link #addValue(double[]) addValue} has been used to add data; * otherwise an IllegalStateException will be thrown.

                              * * @param minImpl the StorelessUnivariateStatistic instance to use * for computing the minimum * @throws DimensionMismatchException if the array dimension * does not match the one used at construction * @throws MathIllegalStateException if data has already been added * (i.e if n > 0) */ public void setMinImpl(StorelessUnivariateStatistic[] minImpl) throws MathIllegalStateException, DimensionMismatchException { setImpl(minImpl, this.minImpl); } /** * Returns the currently configured maximum implementation * * @return the StorelessUnivariateStatistic implementing the maximum */ public StorelessUnivariateStatistic[] getMaxImpl() { return maxImpl.clone(); } /** *

                              Sets the implementation for the maximum.

                              *

                              This method must be activated before any data has been added - i.e., * before {@link #addValue(double[]) addValue} has been used to add data; * otherwise an IllegalStateException will be thrown.

                              * * @param maxImpl the StorelessUnivariateStatistic instance to use * for computing the maximum * @throws DimensionMismatchException if the array dimension * does not match the one used at construction * @throws MathIllegalStateException if data has already been added * (i.e if n > 0) */ public void setMaxImpl(StorelessUnivariateStatistic[] maxImpl) throws MathIllegalStateException, DimensionMismatchException{ setImpl(maxImpl, this.maxImpl); } /** * Returns the currently configured sum of logs implementation * * @return the StorelessUnivariateStatistic implementing the log sum */ public StorelessUnivariateStatistic[] getSumLogImpl() { return sumLogImpl.clone(); } /** *

                              Sets the implementation for the sum of logs.

                              *

                              This method must be activated before any data has been added - i.e., * before {@link #addValue(double[]) addValue} has been used to add data; * otherwise an IllegalStateException will be thrown.

                              * * @param sumLogImpl the StorelessUnivariateStatistic instance to use * for computing the log sum * @throws DimensionMismatchException if the array dimension * does not match the one used at construction * @throws MathIllegalStateException if data has already been added * (i.e if n > 0) */ public void setSumLogImpl(StorelessUnivariateStatistic[] sumLogImpl) throws MathIllegalStateException, DimensionMismatchException{ setImpl(sumLogImpl, this.sumLogImpl); } /** * Returns the currently configured geometric mean implementation * * @return the StorelessUnivariateStatistic implementing the geometric mean */ public StorelessUnivariateStatistic[] getGeoMeanImpl() { return geoMeanImpl.clone(); } /** *

                              Sets the implementation for the geometric mean.

                              *

                              This method must be activated before any data has been added - i.e., * before {@link #addValue(double[]) addValue} has been used to add data; * otherwise an IllegalStateException will be thrown.

                              * * @param geoMeanImpl the StorelessUnivariateStatistic instance to use * for computing the geometric mean * @throws DimensionMismatchException if the array dimension * does not match the one used at construction * @throws MathIllegalStateException if data has already been added * (i.e if n > 0) */ public void setGeoMeanImpl(StorelessUnivariateStatistic[] geoMeanImpl) throws MathIllegalStateException, DimensionMismatchException { setImpl(geoMeanImpl, this.geoMeanImpl); } /** * Returns the currently configured mean implementation * * @return the StorelessUnivariateStatistic implementing the mean */ public StorelessUnivariateStatistic[] getMeanImpl() { return meanImpl.clone(); } /** *

                              Sets the implementation for the mean.

                              *

                              This method must be activated before any data has been added - i.e., * before {@link #addValue(double[]) addValue} has been used to add data; * otherwise an IllegalStateException will be thrown.

                              * * @param meanImpl the StorelessUnivariateStatistic instance to use * for computing the mean * @throws DimensionMismatchException if the array dimension * does not match the one used at construction * @throws MathIllegalStateException if data has already been added * (i.e if n > 0) */ public void setMeanImpl(StorelessUnivariateStatistic[] meanImpl) throws MathIllegalStateException, DimensionMismatchException{ setImpl(meanImpl, this.meanImpl); } /** * Throws MathIllegalStateException if the statistic is not empty. * @throws MathIllegalStateException if n > 0. */ private void checkEmpty() throws MathIllegalStateException { if (n > 0) { throw new MathIllegalStateException( LocalizedFormats.VALUES_ADDED_BEFORE_CONFIGURING_STATISTIC, n); } } /** * Throws DimensionMismatchException if dimension != k. * @param dimension dimension to check * @throws DimensionMismatchException if dimension != k */ private void checkDimension(int dimension) throws DimensionMismatchException { if (dimension != k) { throw new DimensionMismatchException(dimension, k); } } } ././@LongLink100644 0 0 170 12126630647 10260 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/stat/descriptive/AbstractStorelessUnivariateStatistic.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/stat/descriptive/AbstractStorelessUniva100644 1750 1750 16353 12126627717 32450 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.stat.descriptive; import org.apache.commons.math3.exception.util.LocalizedFormats; import org.apache.commons.math3.exception.MathIllegalArgumentException; import org.apache.commons.math3.exception.NullArgumentException; import org.apache.commons.math3.util.MathUtils; import org.apache.commons.math3.util.Precision; /** * * Abstract implementation of the {@link StorelessUnivariateStatistic} interface. *

                              * Provides default evaluate() and incrementAll(double[]) * implementations.

                              *

                              * Note that these implementations are not synchronized.

                              * * @version $Id: AbstractStorelessUnivariateStatistic.java 1416643 2012-12-03 19:37:14Z tn $ */ public abstract class AbstractStorelessUnivariateStatistic extends AbstractUnivariateStatistic implements StorelessUnivariateStatistic { /** * This default implementation calls {@link #clear}, then invokes * {@link #increment} in a loop over the the input array, and then uses * {@link #getResult} to compute the return value. *

                              * Note that this implementation changes the internal state of the * statistic. Its side effects are the same as invoking {@link #clear} and * then {@link #incrementAll(double[])}.

                              *

                              * Implementations may override this method with a more efficient and * possibly more accurate implementation that works directly with the * input array.

                              *

                              * If the array is null, a MathIllegalArgumentException is thrown.

                              * @param values input array * @return the value of the statistic applied to the input array * @throws MathIllegalArgumentException if values is null * @see org.apache.commons.math3.stat.descriptive.UnivariateStatistic#evaluate(double[]) */ @Override public double evaluate(final double[] values) throws MathIllegalArgumentException { if (values == null) { throw new NullArgumentException(LocalizedFormats.INPUT_ARRAY); } return evaluate(values, 0, values.length); } /** * This default implementation calls {@link #clear}, then invokes * {@link #increment} in a loop over the specified portion of the input * array, and then uses {@link #getResult} to compute the return value. *

                              * Note that this implementation changes the internal state of the * statistic. Its side effects are the same as invoking {@link #clear} and * then {@link #incrementAll(double[], int, int)}.

                              *

                              * Implementations may override this method with a more efficient and * possibly more accurate implementation that works directly with the * input array.

                              *

                              * If the array is null or the index parameters are not valid, an * MathIllegalArgumentException is thrown.

                              * @param values the input array * @param begin the index of the first element to include * @param length the number of elements to include * @return the value of the statistic applied to the included array entries * @throws MathIllegalArgumentException if the array is null or the indices are not valid * @see org.apache.commons.math3.stat.descriptive.UnivariateStatistic#evaluate(double[], int, int) */ @Override public double evaluate(final double[] values, final int begin, final int length) throws MathIllegalArgumentException { if (test(values, begin, length)) { clear(); incrementAll(values, begin, length); } return getResult(); } /** * {@inheritDoc} */ @Override public abstract StorelessUnivariateStatistic copy(); /** * {@inheritDoc} */ public abstract void clear(); /** * {@inheritDoc} */ public abstract double getResult(); /** * {@inheritDoc} */ public abstract void increment(final double d); /** * This default implementation just calls {@link #increment} in a loop over * the input array. *

                              * Throws IllegalArgumentException if the input values array is null.

                              * * @param values values to add * @throws MathIllegalArgumentException if values is null * @see org.apache.commons.math3.stat.descriptive.StorelessUnivariateStatistic#incrementAll(double[]) */ public void incrementAll(double[] values) throws MathIllegalArgumentException { if (values == null) { throw new NullArgumentException(LocalizedFormats.INPUT_ARRAY); } incrementAll(values, 0, values.length); } /** * This default implementation just calls {@link #increment} in a loop over * the specified portion of the input array. *

                              * Throws IllegalArgumentException if the input values array is null.

                              * * @param values array holding values to add * @param begin index of the first array element to add * @param length number of array elements to add * @throws MathIllegalArgumentException if values is null * @see org.apache.commons.math3.stat.descriptive.StorelessUnivariateStatistic#incrementAll(double[], int, int) */ public void incrementAll(double[] values, int begin, int length) throws MathIllegalArgumentException { if (test(values, begin, length)) { int k = begin + length; for (int i = begin; i < k; i++) { increment(values[i]); } } } /** * Returns true iff object is an * AbstractStorelessUnivariateStatistic returning the same * values as this for getResult() and getN() * @param object object to test equality against. * @return true if object returns the same value as this */ @Override public boolean equals(Object object) { if (object == this ) { return true; } if (object instanceof AbstractStorelessUnivariateStatistic == false) { return false; } AbstractStorelessUnivariateStatistic stat = (AbstractStorelessUnivariateStatistic) object; return Precision.equalsIncludingNaN(stat.getResult(), this.getResult()) && Precision.equalsIncludingNaN(stat.getN(), this.getN()); } /** * Returns hash code based on getResult() and getN() * * @return hash code */ @Override public int hashCode() { return 31* (31 + MathUtils.hash(getResult())) + MathUtils.hash(getN()); } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/stat/descriptive/package-info.java100644 1750 1750 4407 12126627717 31217 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ /** * * Generic univariate summary statistic objects. * *

                              UnivariateStatistic API Usage Examples:

                              * *

                              UnivariateStatistic:

                              * /∗ evaluation approach ∗/
                              * double[] values = new double[] { 1, 2, 3, 4, 5 };
                              * UnivariateStatistic stat = new Mean();
                              * out.println("mean = " + stat.evaluate(values));
                              *
                              * *

                              StorelessUnivariateStatistic:

                              * /∗ incremental approach ∗/
                              * double[] values = new double[] { 1, 2, 3, 4, 5 };
                              * StorelessUnivariateStatistic stat = new Mean();
                              * out.println("mean before adding a value is NaN = " + stat.getResult());
                              * for (int i = 0; i < values.length; i++) {
                              *     stat.increment(values[i]);
                              *     out.println("current mean = " + stat2.getResult());
                              * }
                              * stat.clear();
                              * out.println("mean after clear is NaN = " + stat.getResult()); *
                              * */ package org.apache.commons.math3.stat.descriptive; ././@LongLink100644 0 0 145 12126630647 10262 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/stat/descriptive/SummaryStatistics.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/stat/descriptive/SummaryStatistics.java100644 1750 1750 62510 12126627717 32422 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.stat.descriptive; import java.io.Serializable; import org.apache.commons.math3.exception.MathIllegalStateException; import org.apache.commons.math3.exception.NullArgumentException; import org.apache.commons.math3.exception.util.LocalizedFormats; import org.apache.commons.math3.stat.descriptive.moment.GeometricMean; import org.apache.commons.math3.stat.descriptive.moment.Mean; import org.apache.commons.math3.stat.descriptive.moment.SecondMoment; import org.apache.commons.math3.stat.descriptive.moment.Variance; import org.apache.commons.math3.stat.descriptive.rank.Max; import org.apache.commons.math3.stat.descriptive.rank.Min; import org.apache.commons.math3.stat.descriptive.summary.Sum; import org.apache.commons.math3.stat.descriptive.summary.SumOfLogs; import org.apache.commons.math3.stat.descriptive.summary.SumOfSquares; import org.apache.commons.math3.util.MathUtils; import org.apache.commons.math3.util.Precision; import org.apache.commons.math3.util.FastMath; /** *

                              * Computes summary statistics for a stream of data values added using the * {@link #addValue(double) addValue} method. The data values are not stored in * memory, so this class can be used to compute statistics for very large data * streams. *

                              *

                              * The {@link StorelessUnivariateStatistic} instances used to maintain summary * state and compute statistics are configurable via setters. For example, the * default implementation for the variance can be overridden by calling * {@link #setVarianceImpl(StorelessUnivariateStatistic)}. Actual parameters to * these methods must implement the {@link StorelessUnivariateStatistic} * interface and configuration must be completed before addValue * is called. No configuration is necessary to use the default, commons-math * provided implementations. *

                              *

                              * Note: This class is not thread-safe. Use * {@link SynchronizedSummaryStatistics} if concurrent access from multiple * threads is required. *

                              * @version $Id: SummaryStatistics.java 1416643 2012-12-03 19:37:14Z tn $ */ public class SummaryStatistics implements StatisticalSummary, Serializable { /** Serialization UID */ private static final long serialVersionUID = -2021321786743555871L; /** count of values that have been added */ private long n = 0; /** SecondMoment is used to compute the mean and variance */ private SecondMoment secondMoment = new SecondMoment(); /** sum of values that have been added */ private Sum sum = new Sum(); /** sum of the square of each value that has been added */ private SumOfSquares sumsq = new SumOfSquares(); /** min of values that have been added */ private Min min = new Min(); /** max of values that have been added */ private Max max = new Max(); /** sumLog of values that have been added */ private SumOfLogs sumLog = new SumOfLogs(); /** geoMean of values that have been added */ private GeometricMean geoMean = new GeometricMean(sumLog); /** mean of values that have been added */ private Mean mean = new Mean(secondMoment); /** variance of values that have been added */ private Variance variance = new Variance(secondMoment); /** Sum statistic implementation - can be reset by setter. */ private StorelessUnivariateStatistic sumImpl = sum; /** Sum of squares statistic implementation - can be reset by setter. */ private StorelessUnivariateStatistic sumsqImpl = sumsq; /** Minimum statistic implementation - can be reset by setter. */ private StorelessUnivariateStatistic minImpl = min; /** Maximum statistic implementation - can be reset by setter. */ private StorelessUnivariateStatistic maxImpl = max; /** Sum of log statistic implementation - can be reset by setter. */ private StorelessUnivariateStatistic sumLogImpl = sumLog; /** Geometric mean statistic implementation - can be reset by setter. */ private StorelessUnivariateStatistic geoMeanImpl = geoMean; /** Mean statistic implementation - can be reset by setter. */ private StorelessUnivariateStatistic meanImpl = mean; /** Variance statistic implementation - can be reset by setter. */ private StorelessUnivariateStatistic varianceImpl = variance; /** * Construct a SummaryStatistics instance */ public SummaryStatistics() { } /** * A copy constructor. Creates a deep-copy of the {@code original}. * * @param original the {@code SummaryStatistics} instance to copy * @throws NullArgumentException if original is null */ public SummaryStatistics(SummaryStatistics original) throws NullArgumentException { copy(original, this); } /** * Return a {@link StatisticalSummaryValues} instance reporting current * statistics. * @return Current values of statistics */ public StatisticalSummary getSummary() { return new StatisticalSummaryValues(getMean(), getVariance(), getN(), getMax(), getMin(), getSum()); } /** * Add a value to the data * @param value the value to add */ public void addValue(double value) { sumImpl.increment(value); sumsqImpl.increment(value); minImpl.increment(value); maxImpl.increment(value); sumLogImpl.increment(value); secondMoment.increment(value); // If mean, variance or geomean have been overridden, // need to increment these if (meanImpl != mean) { meanImpl.increment(value); } if (varianceImpl != variance) { varianceImpl.increment(value); } if (geoMeanImpl != geoMean) { geoMeanImpl.increment(value); } n++; } /** * Returns the number of available values * @return The number of available values */ public long getN() { return n; } /** * Returns the sum of the values that have been added * @return The sum or Double.NaN if no values have been added */ public double getSum() { return sumImpl.getResult(); } /** * Returns the sum of the squares of the values that have been added. *

                              * Double.NaN is returned if no values have been added. *

                              * @return The sum of squares */ public double getSumsq() { return sumsqImpl.getResult(); } /** * Returns the mean of the values that have been added. *

                              * Double.NaN is returned if no values have been added. *

                              * @return the mean */ public double getMean() { return meanImpl.getResult(); } /** * Returns the standard deviation of the values that have been added. *

                              * Double.NaN is returned if no values have been added. *

                              * @return the standard deviation */ public double getStandardDeviation() { double stdDev = Double.NaN; if (getN() > 0) { if (getN() > 1) { stdDev = FastMath.sqrt(getVariance()); } else { stdDev = 0.0; } } return stdDev; } /** * Returns the (sample) variance of the available values. * *

                              This method returns the bias-corrected sample variance (using {@code n - 1} in * the denominator). Use {@link #getPopulationVariance()} for the non-bias-corrected * population variance.

                              * *

                              Double.NaN is returned if no values have been added.

                              * * @return the variance */ public double getVariance() { return varianceImpl.getResult(); } /** * Returns the * population variance of the values that have been added. * *

                              Double.NaN is returned if no values have been added.

                              * * @return the population variance */ public double getPopulationVariance() { Variance populationVariance = new Variance(secondMoment); populationVariance.setBiasCorrected(false); return populationVariance.getResult(); } /** * Returns the maximum of the values that have been added. *

                              * Double.NaN is returned if no values have been added. *

                              * @return the maximum */ public double getMax() { return maxImpl.getResult(); } /** * Returns the minimum of the values that have been added. *

                              * Double.NaN is returned if no values have been added. *

                              * @return the minimum */ public double getMin() { return minImpl.getResult(); } /** * Returns the geometric mean of the values that have been added. *

                              * Double.NaN is returned if no values have been added. *

                              * @return the geometric mean */ public double getGeometricMean() { return geoMeanImpl.getResult(); } /** * Returns the sum of the logs of the values that have been added. *

                              * Double.NaN is returned if no values have been added. *

                              * @return the sum of logs * @since 1.2 */ public double getSumOfLogs() { return sumLogImpl.getResult(); } /** * Returns a statistic related to the Second Central Moment. Specifically, * what is returned is the sum of squared deviations from the sample mean * among the values that have been added. *

                              * Returns Double.NaN if no data values have been added and * returns 0 if there is just one value in the data set.

                              *

                              * @return second central moment statistic * @since 2.0 */ public double getSecondMoment() { return secondMoment.getResult(); } /** * Generates a text report displaying summary statistics from values that * have been added. * @return String with line feeds displaying statistics * @since 1.2 */ @Override public String toString() { StringBuilder outBuffer = new StringBuilder(); String endl = "\n"; outBuffer.append("SummaryStatistics:").append(endl); outBuffer.append("n: ").append(getN()).append(endl); outBuffer.append("min: ").append(getMin()).append(endl); outBuffer.append("max: ").append(getMax()).append(endl); outBuffer.append("mean: ").append(getMean()).append(endl); outBuffer.append("geometric mean: ").append(getGeometricMean()) .append(endl); outBuffer.append("variance: ").append(getVariance()).append(endl); outBuffer.append("sum of squares: ").append(getSumsq()).append(endl); outBuffer.append("standard deviation: ").append(getStandardDeviation()) .append(endl); outBuffer.append("sum of logs: ").append(getSumOfLogs()).append(endl); return outBuffer.toString(); } /** * Resets all statistics and storage */ public void clear() { this.n = 0; minImpl.clear(); maxImpl.clear(); sumImpl.clear(); sumLogImpl.clear(); sumsqImpl.clear(); geoMeanImpl.clear(); secondMoment.clear(); if (meanImpl != mean) { meanImpl.clear(); } if (varianceImpl != variance) { varianceImpl.clear(); } } /** * Returns true iff object is a * SummaryStatistics instance and all statistics have the * same values as this. * @param object the object to test equality against. * @return true if object equals this */ @Override public boolean equals(Object object) { if (object == this) { return true; } if (object instanceof SummaryStatistics == false) { return false; } SummaryStatistics stat = (SummaryStatistics)object; return Precision.equalsIncludingNaN(stat.getGeometricMean(), getGeometricMean()) && Precision.equalsIncludingNaN(stat.getMax(), getMax()) && Precision.equalsIncludingNaN(stat.getMean(), getMean()) && Precision.equalsIncludingNaN(stat.getMin(), getMin()) && Precision.equalsIncludingNaN(stat.getN(), getN()) && Precision.equalsIncludingNaN(stat.getSum(), getSum()) && Precision.equalsIncludingNaN(stat.getSumsq(), getSumsq()) && Precision.equalsIncludingNaN(stat.getVariance(), getVariance()); } /** * Returns hash code based on values of statistics * @return hash code */ @Override public int hashCode() { int result = 31 + MathUtils.hash(getGeometricMean()); result = result * 31 + MathUtils.hash(getGeometricMean()); result = result * 31 + MathUtils.hash(getMax()); result = result * 31 + MathUtils.hash(getMean()); result = result * 31 + MathUtils.hash(getMin()); result = result * 31 + MathUtils.hash(getN()); result = result * 31 + MathUtils.hash(getSum()); result = result * 31 + MathUtils.hash(getSumsq()); result = result * 31 + MathUtils.hash(getVariance()); return result; } // Getters and setters for statistics implementations /** * Returns the currently configured Sum implementation * @return the StorelessUnivariateStatistic implementing the sum * @since 1.2 */ public StorelessUnivariateStatistic getSumImpl() { return sumImpl; } /** *

                              * Sets the implementation for the Sum. *

                              *

                              * This method must be activated before any data has been added - i.e., * before {@link #addValue(double) addValue} has been used to add data; * otherwise an IllegalStateException will be thrown. *

                              * @param sumImpl the StorelessUnivariateStatistic instance to use for * computing the Sum * @throws MathIllegalStateException if data has already been added (i.e if n >0) * @since 1.2 */ public void setSumImpl(StorelessUnivariateStatistic sumImpl) throws MathIllegalStateException { checkEmpty(); this.sumImpl = sumImpl; } /** * Returns the currently configured sum of squares implementation * @return the StorelessUnivariateStatistic implementing the sum of squares * @since 1.2 */ public StorelessUnivariateStatistic getSumsqImpl() { return sumsqImpl; } /** *

                              * Sets the implementation for the sum of squares. *

                              *

                              * This method must be activated before any data has been added - i.e., * before {@link #addValue(double) addValue} has been used to add data; * otherwise an IllegalStateException will be thrown. *

                              * @param sumsqImpl the StorelessUnivariateStatistic instance to use for * computing the sum of squares * @throws MathIllegalStateException if data has already been added (i.e if n > 0) * @since 1.2 */ public void setSumsqImpl(StorelessUnivariateStatistic sumsqImpl) throws MathIllegalStateException { checkEmpty(); this.sumsqImpl = sumsqImpl; } /** * Returns the currently configured minimum implementation * @return the StorelessUnivariateStatistic implementing the minimum * @since 1.2 */ public StorelessUnivariateStatistic getMinImpl() { return minImpl; } /** *

                              * Sets the implementation for the minimum. *

                              *

                              * This method must be activated before any data has been added - i.e., * before {@link #addValue(double) addValue} has been used to add data; * otherwise an IllegalStateException will be thrown. *

                              * @param minImpl the StorelessUnivariateStatistic instance to use for * computing the minimum * @throws MathIllegalStateException if data has already been added (i.e if n > 0) * @since 1.2 */ public void setMinImpl(StorelessUnivariateStatistic minImpl) throws MathIllegalStateException { checkEmpty(); this.minImpl = minImpl; } /** * Returns the currently configured maximum implementation * @return the StorelessUnivariateStatistic implementing the maximum * @since 1.2 */ public StorelessUnivariateStatistic getMaxImpl() { return maxImpl; } /** *

                              * Sets the implementation for the maximum. *

                              *

                              * This method must be activated before any data has been added - i.e., * before {@link #addValue(double) addValue} has been used to add data; * otherwise an IllegalStateException will be thrown. *

                              * @param maxImpl the StorelessUnivariateStatistic instance to use for * computing the maximum * @throws MathIllegalStateException if data has already been added (i.e if n > 0) * @since 1.2 */ public void setMaxImpl(StorelessUnivariateStatistic maxImpl) throws MathIllegalStateException { checkEmpty(); this.maxImpl = maxImpl; } /** * Returns the currently configured sum of logs implementation * @return the StorelessUnivariateStatistic implementing the log sum * @since 1.2 */ public StorelessUnivariateStatistic getSumLogImpl() { return sumLogImpl; } /** *

                              * Sets the implementation for the sum of logs. *

                              *

                              * This method must be activated before any data has been added - i.e., * before {@link #addValue(double) addValue} has been used to add data; * otherwise an IllegalStateException will be thrown. *

                              * @param sumLogImpl the StorelessUnivariateStatistic instance to use for * computing the log sum * @throws MathIllegalStateException if data has already been added (i.e if n > 0) * @since 1.2 */ public void setSumLogImpl(StorelessUnivariateStatistic sumLogImpl) throws MathIllegalStateException { checkEmpty(); this.sumLogImpl = sumLogImpl; geoMean.setSumLogImpl(sumLogImpl); } /** * Returns the currently configured geometric mean implementation * @return the StorelessUnivariateStatistic implementing the geometric mean * @since 1.2 */ public StorelessUnivariateStatistic getGeoMeanImpl() { return geoMeanImpl; } /** *

                              * Sets the implementation for the geometric mean. *

                              *

                              * This method must be activated before any data has been added - i.e., * before {@link #addValue(double) addValue} has been used to add data; * otherwise an IllegalStateException will be thrown. *

                              * @param geoMeanImpl the StorelessUnivariateStatistic instance to use for * computing the geometric mean * @throws MathIllegalStateException if data has already been added (i.e if n > 0) * @since 1.2 */ public void setGeoMeanImpl(StorelessUnivariateStatistic geoMeanImpl) throws MathIllegalStateException { checkEmpty(); this.geoMeanImpl = geoMeanImpl; } /** * Returns the currently configured mean implementation * @return the StorelessUnivariateStatistic implementing the mean * @since 1.2 */ public StorelessUnivariateStatistic getMeanImpl() { return meanImpl; } /** *

                              * Sets the implementation for the mean. *

                              *

                              * This method must be activated before any data has been added - i.e., * before {@link #addValue(double) addValue} has been used to add data; * otherwise an IllegalStateException will be thrown. *

                              * @param meanImpl the StorelessUnivariateStatistic instance to use for * computing the mean * @throws MathIllegalStateException if data has already been added (i.e if n > 0) * @since 1.2 */ public void setMeanImpl(StorelessUnivariateStatistic meanImpl) throws MathIllegalStateException { checkEmpty(); this.meanImpl = meanImpl; } /** * Returns the currently configured variance implementation * @return the StorelessUnivariateStatistic implementing the variance * @since 1.2 */ public StorelessUnivariateStatistic getVarianceImpl() { return varianceImpl; } /** *

                              * Sets the implementation for the variance. *

                              *

                              * This method must be activated before any data has been added - i.e., * before {@link #addValue(double) addValue} has been used to add data; * otherwise an IllegalStateException will be thrown. *

                              * @param varianceImpl the StorelessUnivariateStatistic instance to use for * computing the variance * @throws MathIllegalStateException if data has already been added (i.e if n > 0) * @since 1.2 */ public void setVarianceImpl(StorelessUnivariateStatistic varianceImpl) throws MathIllegalStateException { checkEmpty(); this.varianceImpl = varianceImpl; } /** * Throws IllegalStateException if n > 0. * @throws MathIllegalStateException if data has been added */ private void checkEmpty() throws MathIllegalStateException { if (n > 0) { throw new MathIllegalStateException( LocalizedFormats.VALUES_ADDED_BEFORE_CONFIGURING_STATISTIC, n); } } /** * Returns a copy of this SummaryStatistics instance with the same internal state. * * @return a copy of this */ public SummaryStatistics copy() { SummaryStatistics result = new SummaryStatistics(); // No try-catch or advertised exception because arguments are guaranteed non-null copy(this, result); return result; } /** * Copies source to dest. *

                              Neither source nor dest can be null.

                              * * @param source SummaryStatistics to copy * @param dest SummaryStatistics to copy to * @throws NullArgumentException if either source or dest is null */ public static void copy(SummaryStatistics source, SummaryStatistics dest) throws NullArgumentException { MathUtils.checkNotNull(source); MathUtils.checkNotNull(dest); dest.maxImpl = source.maxImpl.copy(); dest.minImpl = source.minImpl.copy(); dest.sumImpl = source.sumImpl.copy(); dest.sumLogImpl = source.sumLogImpl.copy(); dest.sumsqImpl = source.sumsqImpl.copy(); dest.secondMoment = source.secondMoment.copy(); dest.n = source.n; // Keep commons-math supplied statistics with embedded moments in synch if (source.getVarianceImpl() instanceof Variance) { dest.varianceImpl = new Variance(dest.secondMoment); } else { dest.varianceImpl = source.varianceImpl.copy(); } if (source.meanImpl instanceof Mean) { dest.meanImpl = new Mean(dest.secondMoment); } else { dest.meanImpl = source.meanImpl.copy(); } if (source.getGeoMeanImpl() instanceof GeometricMean) { dest.geoMeanImpl = new GeometricMean((SumOfLogs) dest.sumLogImpl); } else { dest.geoMeanImpl = source.geoMeanImpl.copy(); } // Make sure that if stat == statImpl in source, same // holds in dest; otherwise copy stat if (source.geoMean == source.geoMeanImpl) { dest.geoMean = (GeometricMean) dest.geoMeanImpl; } else { GeometricMean.copy(source.geoMean, dest.geoMean); } if (source.max == source.maxImpl) { dest.max = (Max) dest.maxImpl; } else { Max.copy(source.max, dest.max); } if (source.mean == source.meanImpl) { dest.mean = (Mean) dest.meanImpl; } else { Mean.copy(source.mean, dest.mean); } if (source.min == source.minImpl) { dest.min = (Min) dest.minImpl; } else { Min.copy(source.min, dest.min); } if (source.sum == source.sumImpl) { dest.sum = (Sum) dest.sumImpl; } else { Sum.copy(source.sum, dest.sum); } if (source.variance == source.varianceImpl) { dest.variance = (Variance) dest.varianceImpl; } else { Variance.copy(source.variance, dest.variance); } if (source.sumLog == source.sumLogImpl) { dest.sumLog = (SumOfLogs) dest.sumLogImpl; } else { SumOfLogs.copy(source.sumLog, dest.sumLog); } if (source.sumsq == source.sumsqImpl) { dest.sumsq = (SumOfSquares) dest.sumsqImpl; } else { SumOfSquares.copy(source.sumsq, dest.sumsq); } } } ././@LongLink100644 0 0 160 12126630647 10257 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/stat/descriptive/StorelessUnivariateStatistic.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/stat/descriptive/StorelessUnivariateSta100644 1750 1750 6377 12126627717 32446 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.stat.descriptive; import org.apache.commons.math3.exception.MathIllegalArgumentException; /** * Extends the definition of {@link UnivariateStatistic} with * {@link #increment} and {@link #incrementAll(double[])} methods for adding * values and updating internal state. *

                              * This interface is designed to be used for calculating statistics that can be * computed in one pass through the data without storing the full array of * sample values.

                              * * @version $Id: StorelessUnivariateStatistic.java 1416643 2012-12-03 19:37:14Z tn $ */ public interface StorelessUnivariateStatistic extends UnivariateStatistic { /** * Updates the internal state of the statistic to reflect the addition of the new value. * @param d the new value. */ void increment(double d); /** * Updates the internal state of the statistic to reflect addition of * all values in the values array. Does not clear the statistic first -- * i.e., the values are added incrementally to the dataset. * * @param values array holding the new values to add * @throws MathIllegalArgumentException if the array is null */ void incrementAll(double[] values) throws MathIllegalArgumentException; /** * Updates the internal state of the statistic to reflect addition of * the values in the designated portion of the values array. Does not * clear the statistic first -- i.e., the values are added * incrementally to the dataset. * * @param values array holding the new values to add * @param start the array index of the first value to add * @param length the number of elements to add * @throws MathIllegalArgumentException if the array is null or the index */ void incrementAll(double[] values, int start, int length) throws MathIllegalArgumentException; /** * Returns the current value of the Statistic. * @return value of the statistic, Double.NaN if it * has been cleared or just instantiated. */ double getResult(); /** * Returns the number of values that have been added. * @return the number of values. */ long getN(); /** * Clears the internal state of the Statistic */ void clear(); /** * Returns a copy of the statistic with the same internal state. * * @return a copy of the statistic */ StorelessUnivariateStatistic copy(); } ././@LongLink100644 0 0 175 12126630647 10265 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/stat/descriptive/SynchronizedMultivariateSummaryStatistics.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/stat/descriptive/SynchronizedMultivaria100644 1750 1750 16474 12126627717 32517 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.stat.descriptive; import org.apache.commons.math3.exception.DimensionMismatchException; import org.apache.commons.math3.exception.MathIllegalStateException; import org.apache.commons.math3.linear.RealMatrix; /** * Implementation of * {@link org.apache.commons.math3.stat.descriptive.MultivariateSummaryStatistics} that * is safe to use in a multithreaded environment. Multiple threads can safely * operate on a single instance without causing runtime exceptions due to race * conditions. In effect, this implementation makes modification and access * methods atomic operations for a single instance. That is to say, as one * thread is computing a statistic from the instance, no other thread can modify * the instance nor compute another statistic. * @since 1.2 * @version $Id: SynchronizedMultivariateSummaryStatistics.java 1416643 2012-12-03 19:37:14Z tn $ */ public class SynchronizedMultivariateSummaryStatistics extends MultivariateSummaryStatistics { /** Serialization UID */ private static final long serialVersionUID = 7099834153347155363L; /** * Construct a SynchronizedMultivariateSummaryStatistics instance * @param k dimension of the data * @param isCovarianceBiasCorrected if true, the unbiased sample * covariance is computed, otherwise the biased population covariance * is computed */ public SynchronizedMultivariateSummaryStatistics(int k, boolean isCovarianceBiasCorrected) { super(k, isCovarianceBiasCorrected); } /** * {@inheritDoc} */ @Override public synchronized void addValue(double[] value) throws DimensionMismatchException { super.addValue(value); } /** * {@inheritDoc} */ @Override public synchronized int getDimension() { return super.getDimension(); } /** * {@inheritDoc} */ @Override public synchronized long getN() { return super.getN(); } /** * {@inheritDoc} */ @Override public synchronized double[] getSum() { return super.getSum(); } /** * {@inheritDoc} */ @Override public synchronized double[] getSumSq() { return super.getSumSq(); } /** * {@inheritDoc} */ @Override public synchronized double[] getSumLog() { return super.getSumLog(); } /** * {@inheritDoc} */ @Override public synchronized double[] getMean() { return super.getMean(); } /** * {@inheritDoc} */ @Override public synchronized double[] getStandardDeviation() { return super.getStandardDeviation(); } /** * {@inheritDoc} */ @Override public synchronized RealMatrix getCovariance() { return super.getCovariance(); } /** * {@inheritDoc} */ @Override public synchronized double[] getMax() { return super.getMax(); } /** * {@inheritDoc} */ @Override public synchronized double[] getMin() { return super.getMin(); } /** * {@inheritDoc} */ @Override public synchronized double[] getGeometricMean() { return super.getGeometricMean(); } /** * {@inheritDoc} */ @Override public synchronized String toString() { return super.toString(); } /** * {@inheritDoc} */ @Override public synchronized void clear() { super.clear(); } /** * {@inheritDoc} */ @Override public synchronized boolean equals(Object object) { return super.equals(object); } /** * {@inheritDoc} */ @Override public synchronized int hashCode() { return super.hashCode(); } /** * {@inheritDoc} */ @Override public synchronized StorelessUnivariateStatistic[] getSumImpl() { return super.getSumImpl(); } /** * {@inheritDoc} */ @Override public synchronized void setSumImpl(StorelessUnivariateStatistic[] sumImpl) throws DimensionMismatchException, MathIllegalStateException { super.setSumImpl(sumImpl); } /** * {@inheritDoc} */ @Override public synchronized StorelessUnivariateStatistic[] getSumsqImpl() { return super.getSumsqImpl(); } /** * {@inheritDoc} */ @Override public synchronized void setSumsqImpl(StorelessUnivariateStatistic[] sumsqImpl) throws DimensionMismatchException, MathIllegalStateException { super.setSumsqImpl(sumsqImpl); } /** * {@inheritDoc} */ @Override public synchronized StorelessUnivariateStatistic[] getMinImpl() { return super.getMinImpl(); } /** * {@inheritDoc} */ @Override public synchronized void setMinImpl(StorelessUnivariateStatistic[] minImpl) throws DimensionMismatchException, MathIllegalStateException { super.setMinImpl(minImpl); } /** * {@inheritDoc} */ @Override public synchronized StorelessUnivariateStatistic[] getMaxImpl() { return super.getMaxImpl(); } /** * {@inheritDoc} */ @Override public synchronized void setMaxImpl(StorelessUnivariateStatistic[] maxImpl) throws DimensionMismatchException, MathIllegalStateException{ super.setMaxImpl(maxImpl); } /** * {@inheritDoc} */ @Override public synchronized StorelessUnivariateStatistic[] getSumLogImpl() { return super.getSumLogImpl(); } /** * {@inheritDoc} */ @Override public synchronized void setSumLogImpl(StorelessUnivariateStatistic[] sumLogImpl) throws DimensionMismatchException, MathIllegalStateException { super.setSumLogImpl(sumLogImpl); } /** * {@inheritDoc} */ @Override public synchronized StorelessUnivariateStatistic[] getGeoMeanImpl() { return super.getGeoMeanImpl(); } /** * {@inheritDoc} */ @Override public synchronized void setGeoMeanImpl(StorelessUnivariateStatistic[] geoMeanImpl) throws DimensionMismatchException, MathIllegalStateException { super.setGeoMeanImpl(geoMeanImpl); } /** * {@inheritDoc} */ @Override public synchronized StorelessUnivariateStatistic[] getMeanImpl() { return super.getMeanImpl(); } /** * {@inheritDoc} */ @Override public synchronized void setMeanImpl(StorelessUnivariateStatistic[] meanImpl) throws DimensionMismatchException, MathIllegalStateException { super.setMeanImpl(meanImpl); } } ././@LongLink100644 0 0 157 12126630647 10265 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/stat/descriptive/AbstractUnivariateStatistic.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/stat/descriptive/AbstractUnivariateStat100644 1750 1750 31060 12126627717 32415 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.stat.descriptive; import org.apache.commons.math3.exception.DimensionMismatchException; import org.apache.commons.math3.exception.NotPositiveException; import org.apache.commons.math3.exception.NullArgumentException; import org.apache.commons.math3.exception.NumberIsTooLargeException; import org.apache.commons.math3.exception.MathIllegalArgumentException; import org.apache.commons.math3.exception.util.LocalizedFormats; /** * Abstract base class for all implementations of the * {@link UnivariateStatistic} interface. *

                              * Provides a default implementation of evaluate(double[]), * delegating to evaluate(double[], int, int) in the natural way. *

                              *

                              * Also includes a test method that performs generic parameter * validation for the evaluate methods.

                              * * @version $Id: AbstractUnivariateStatistic.java 1416643 2012-12-03 19:37:14Z tn $ */ public abstract class AbstractUnivariateStatistic implements UnivariateStatistic { /** Stored data. */ private double[] storedData; /** * Set the data array. *

                              * The stored value is a copy of the parameter array, not the array itself. *

                              * @param values data array to store (may be null to remove stored data) * @see #evaluate() */ public void setData(final double[] values) { storedData = (values == null) ? null : values.clone(); } /** * Get a copy of the stored data array. * @return copy of the stored data array (may be null) */ public double[] getData() { return (storedData == null) ? null : storedData.clone(); } /** * Get a reference to the stored data array. * @return reference to the stored data array (may be null) */ protected double[] getDataRef() { return storedData; } /** * Set the data array. The input array is copied, not referenced. * * @param values data array to store * @param begin the index of the first element to include * @param length the number of elements to include * @throws MathIllegalArgumentException if values is null or the indices * are not valid * @see #evaluate() */ public void setData(final double[] values, final int begin, final int length) throws MathIllegalArgumentException { if (values == null) { throw new NullArgumentException(LocalizedFormats.INPUT_ARRAY); } if (begin < 0) { throw new NotPositiveException(LocalizedFormats.START_POSITION, begin); } if (length < 0) { throw new NotPositiveException(LocalizedFormats.LENGTH, length); } if (begin + length > values.length) { throw new NumberIsTooLargeException(LocalizedFormats.SUBARRAY_ENDS_AFTER_ARRAY_END, begin + length, values.length, true); } storedData = new double[length]; System.arraycopy(values, begin, storedData, 0, length); } /** * Returns the result of evaluating the statistic over the stored data. *

                              * The stored array is the one which was set by previous calls to {@link #setData(double[])}. *

                              * @return the value of the statistic applied to the stored data * @throws MathIllegalArgumentException if the stored data array is null */ public double evaluate() throws MathIllegalArgumentException { return evaluate(storedData); } /** * {@inheritDoc} */ public double evaluate(final double[] values) throws MathIllegalArgumentException { test(values, 0, 0); return evaluate(values, 0, values.length); } /** * {@inheritDoc} */ public abstract double evaluate(final double[] values, final int begin, final int length) throws MathIllegalArgumentException; /** * {@inheritDoc} */ public abstract UnivariateStatistic copy(); /** * This method is used by evaluate(double[], int, int) methods * to verify that the input parameters designate a subarray of positive length. *

                              *

                                *
                              • returns true iff the parameters designate a subarray of * positive length
                              • *
                              • throws MathIllegalArgumentException if the array is null or * or the indices are invalid
                              • *
                              • returns false
                              • if the array is non-null, but * length is 0. *

                              * * @param values the input array * @param begin index of the first array element to include * @param length the number of elements to include * @return true if the parameters are valid and designate a subarray of positive length * @throws MathIllegalArgumentException if the indices are invalid or the array is null */ protected boolean test( final double[] values, final int begin, final int length) throws MathIllegalArgumentException { return test(values, begin, length, false); } /** * This method is used by evaluate(double[], int, int) methods * to verify that the input parameters designate a subarray of positive length. *

                              *

                                *
                              • returns true iff the parameters designate a subarray of * non-negative length
                              • *
                              • throws IllegalArgumentException if the array is null or * or the indices are invalid
                              • *
                              • returns false
                              • if the array is non-null, but * length is 0 unless allowEmpty is true *

                              * * @param values the input array * @param begin index of the first array element to include * @param length the number of elements to include * @param allowEmpty if true then zero length arrays are allowed * @return true if the parameters are valid * @throws MathIllegalArgumentException if the indices are invalid or the array is null * @since 3.0 */ protected boolean test(final double[] values, final int begin, final int length, final boolean allowEmpty) throws MathIllegalArgumentException { if (values == null) { throw new NullArgumentException(LocalizedFormats.INPUT_ARRAY); } if (begin < 0) { throw new NotPositiveException(LocalizedFormats.START_POSITION, begin); } if (length < 0) { throw new NotPositiveException(LocalizedFormats.LENGTH, length); } if (begin + length > values.length) { throw new NumberIsTooLargeException(LocalizedFormats.SUBARRAY_ENDS_AFTER_ARRAY_END, begin + length, values.length, true); } if (length == 0 && !allowEmpty) { return false; } return true; } /** * This method is used by evaluate(double[], double[], int, int) methods * to verify that the begin and length parameters designate a subarray of positive length * and the weights are all non-negative, non-NaN, finite, and not all zero. *

                              *

                                *
                              • returns true iff the parameters designate a subarray of * positive length and the weights array contains legitimate values.
                              • *
                              • throws IllegalArgumentException if any of the following are true: *
                                • the values array is null
                                • *
                                • the weights array is null
                                • *
                                • the weights array does not have the same length as the values array
                                • *
                                • the weights array contains one or more infinite values
                                • *
                                • the weights array contains one or more NaN values
                                • *
                                • the weights array contains negative values
                                • *
                                • the start and length arguments do not determine a valid array
                                *
                              • *
                              • returns false
                              • if the array is non-null, but * length is 0. *

                              * * @param values the input array * @param weights the weights array * @param begin index of the first array element to include * @param length the number of elements to include * @return true if the parameters are valid and designate a subarray of positive length * @throws MathIllegalArgumentException if the indices are invalid or the array is null * @since 2.1 */ protected boolean test( final double[] values, final double[] weights, final int begin, final int length) throws MathIllegalArgumentException { return test(values, weights, begin, length, false); } /** * This method is used by evaluate(double[], double[], int, int) methods * to verify that the begin and length parameters designate a subarray of positive length * and the weights are all non-negative, non-NaN, finite, and not all zero. *

                              *

                                *
                              • returns true iff the parameters designate a subarray of * non-negative length and the weights array contains legitimate values.
                              • *
                              • throws MathIllegalArgumentException if any of the following are true: *
                                • the values array is null
                                • *
                                • the weights array is null
                                • *
                                • the weights array does not have the same length as the values array
                                • *
                                • the weights array contains one or more infinite values
                                • *
                                • the weights array contains one or more NaN values
                                • *
                                • the weights array contains negative values
                                • *
                                • the start and length arguments do not determine a valid array
                                *
                              • *
                              • returns false
                              • if the array is non-null, but * length is 0 unless allowEmpty is true. *

                              * * @param values the input array. * @param weights the weights array. * @param begin index of the first array element to include. * @param length the number of elements to include. * @param allowEmpty if {@code true} than allow zero length arrays to pass. * @return {@code true} if the parameters are valid. * @throws NullArgumentException if either of the arrays are null * @throws MathIllegalArgumentException if the array indices are not valid, * the weights array contains NaN, infinite or negative elements, or there * are no positive weights. * @since 3.0 */ protected boolean test(final double[] values, final double[] weights, final int begin, final int length, final boolean allowEmpty) throws MathIllegalArgumentException { if (weights == null || values == null) { throw new NullArgumentException(LocalizedFormats.INPUT_ARRAY); } if (weights.length != values.length) { throw new DimensionMismatchException(weights.length, values.length); } boolean containsPositiveWeight = false; for (int i = begin; i < begin + length; i++) { if (Double.isNaN(weights[i])) { throw new MathIllegalArgumentException(LocalizedFormats.NAN_ELEMENT_AT_INDEX, i); } if (Double.isInfinite(weights[i])) { throw new MathIllegalArgumentException(LocalizedFormats.INFINITE_ARRAY_ELEMENT, weights[i], i); } if (weights[i] < 0) { throw new MathIllegalArgumentException(LocalizedFormats.NEGATIVE_ELEMENT_AT_INDEX, i, weights[i]); } if (!containsPositiveWeight && weights[i] > 0.0) { containsPositiveWeight = true; } } if (!containsPositiveWeight) { throw new MathIllegalArgumentException(LocalizedFormats.WEIGHT_AT_LEAST_ONE_NON_ZERO); } return test(values, begin, length, allowEmpty); } } ././@LongLink100644 0 0 165 12126630647 10264 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/stat/descriptive/SynchronizedDescriptiveStatistics.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/stat/descriptive/SynchronizedDescriptiv100644 1750 1750 12715 12126627717 32510 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.stat.descriptive; import org.apache.commons.math3.exception.MathIllegalArgumentException; import org.apache.commons.math3.exception.NullArgumentException; import org.apache.commons.math3.util.MathUtils; /** * Implementation of * {@link org.apache.commons.math3.stat.descriptive.DescriptiveStatistics} that * is safe to use in a multithreaded environment. Multiple threads can safely * operate on a single instance without causing runtime exceptions due to race * conditions. In effect, this implementation makes modification and access * methods atomic operations for a single instance. That is to say, as one * thread is computing a statistic from the instance, no other thread can modify * the instance nor compute another statistic. * * @since 1.2 * @version $Id: SynchronizedDescriptiveStatistics.java 1416643 2012-12-03 19:37:14Z tn $ */ public class SynchronizedDescriptiveStatistics extends DescriptiveStatistics { /** Serialization UID */ private static final long serialVersionUID = 1L; /** * Construct an instance with infinite window */ public SynchronizedDescriptiveStatistics() { // no try-catch or advertized IAE because arg is valid this(INFINITE_WINDOW); } /** * Construct an instance with finite window * @param window the finite window size. * @throws MathIllegalArgumentException if window size is less than 1 but * not equal to {@link #INFINITE_WINDOW} */ public SynchronizedDescriptiveStatistics(int window) throws MathIllegalArgumentException { super(window); } /** * A copy constructor. Creates a deep-copy of the {@code original}. * * @param original the {@code SynchronizedDescriptiveStatistics} instance to copy * @throws NullArgumentException if original is null */ public SynchronizedDescriptiveStatistics(SynchronizedDescriptiveStatistics original) throws NullArgumentException { copy(original, this); } /** * {@inheritDoc} */ @Override public synchronized void addValue(double v) { super.addValue(v); } /** * {@inheritDoc} */ @Override public synchronized double apply(UnivariateStatistic stat) { return super.apply(stat); } /** * {@inheritDoc} */ @Override public synchronized void clear() { super.clear(); } /** * {@inheritDoc} */ @Override public synchronized double getElement(int index) { return super.getElement(index); } /** * {@inheritDoc} */ @Override public synchronized long getN() { return super.getN(); } /** * {@inheritDoc} */ @Override public synchronized double getStandardDeviation() { return super.getStandardDeviation(); } /** * {@inheritDoc} */ @Override public synchronized double[] getValues() { return super.getValues(); } /** * {@inheritDoc} */ @Override public synchronized int getWindowSize() { return super.getWindowSize(); } /** * {@inheritDoc} */ @Override public synchronized void setWindowSize(int windowSize) throws MathIllegalArgumentException { super.setWindowSize(windowSize); } /** * {@inheritDoc} */ @Override public synchronized String toString() { return super.toString(); } /** * Returns a copy of this SynchronizedDescriptiveStatistics instance with the * same internal state. * * @return a copy of this */ @Override public synchronized SynchronizedDescriptiveStatistics copy() { SynchronizedDescriptiveStatistics result = new SynchronizedDescriptiveStatistics(); // No try-catch or advertised exception because arguments are guaranteed non-null copy(this, result); return result; } /** * Copies source to dest. *

                              Neither source nor dest can be null.

                              *

                              Acquires synchronization lock on source, then dest before copying.

                              * * @param source SynchronizedDescriptiveStatistics to copy * @param dest SynchronizedDescriptiveStatistics to copy to * @throws NullArgumentException if either source or dest is null */ public static void copy(SynchronizedDescriptiveStatistics source, SynchronizedDescriptiveStatistics dest) throws NullArgumentException { MathUtils.checkNotNull(source); MathUtils.checkNotNull(dest); synchronized (source) { synchronized (dest) { DescriptiveStatistics.copy(source, dest); } } } } ././@LongLink100644 0 0 151 12126630647 10257 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/stat/descriptive/DescriptiveStatistics.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/stat/descriptive/DescriptiveStatistics.100644 1750 1750 65443 12126627717 32414 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.stat.descriptive; import java.io.Serializable; import java.lang.reflect.InvocationTargetException; import java.util.Arrays; import org.apache.commons.math3.exception.MathIllegalArgumentException; import org.apache.commons.math3.exception.NullArgumentException; import org.apache.commons.math3.exception.MathIllegalStateException; import org.apache.commons.math3.exception.util.LocalizedFormats; import org.apache.commons.math3.stat.descriptive.moment.GeometricMean; import org.apache.commons.math3.stat.descriptive.moment.Kurtosis; import org.apache.commons.math3.stat.descriptive.moment.Mean; import org.apache.commons.math3.stat.descriptive.moment.Skewness; import org.apache.commons.math3.stat.descriptive.moment.Variance; import org.apache.commons.math3.stat.descriptive.rank.Max; import org.apache.commons.math3.stat.descriptive.rank.Min; import org.apache.commons.math3.stat.descriptive.rank.Percentile; import org.apache.commons.math3.stat.descriptive.summary.Sum; import org.apache.commons.math3.stat.descriptive.summary.SumOfSquares; import org.apache.commons.math3.util.MathUtils; import org.apache.commons.math3.util.ResizableDoubleArray; import org.apache.commons.math3.util.FastMath; /** * Maintains a dataset of values of a single variable and computes descriptive * statistics based on stored data. The {@link #getWindowSize() windowSize} * property sets a limit on the number of values that can be stored in the * dataset. The default value, INFINITE_WINDOW, puts no limit on the size of * the dataset. This value should be used with caution, as the backing store * will grow without bound in this case. For very large datasets, * {@link SummaryStatistics}, which does not store the dataset, should be used * instead of this class. If windowSize is not INFINITE_WINDOW and * more values are added than can be stored in the dataset, new values are * added in a "rolling" manner, with new values replacing the "oldest" values * in the dataset. * *

                              Note: this class is not threadsafe. Use * {@link SynchronizedDescriptiveStatistics} if concurrent access from multiple * threads is required.

                              * * @version $Id: DescriptiveStatistics.java 1422354 2012-12-15 20:59:01Z psteitz $ */ public class DescriptiveStatistics implements StatisticalSummary, Serializable { /** * Represents an infinite window size. When the {@link #getWindowSize()} * returns this value, there is no limit to the number of data values * that can be stored in the dataset. */ public static final int INFINITE_WINDOW = -1; /** Serialization UID */ private static final long serialVersionUID = 4133067267405273064L; /** Name of the setQuantile method. */ private static final String SET_QUANTILE_METHOD_NAME = "setQuantile"; /** hold the window size **/ protected int windowSize = INFINITE_WINDOW; /** * Stored data values */ private ResizableDoubleArray eDA = new ResizableDoubleArray(); /** Mean statistic implementation - can be reset by setter. */ private UnivariateStatistic meanImpl = new Mean(); /** Geometric mean statistic implementation - can be reset by setter. */ private UnivariateStatistic geometricMeanImpl = new GeometricMean(); /** Kurtosis statistic implementation - can be reset by setter. */ private UnivariateStatistic kurtosisImpl = new Kurtosis(); /** Maximum statistic implementation - can be reset by setter. */ private UnivariateStatistic maxImpl = new Max(); /** Minimum statistic implementation - can be reset by setter. */ private UnivariateStatistic minImpl = new Min(); /** Percentile statistic implementation - can be reset by setter. */ private UnivariateStatistic percentileImpl = new Percentile(); /** Skewness statistic implementation - can be reset by setter. */ private UnivariateStatistic skewnessImpl = new Skewness(); /** Variance statistic implementation - can be reset by setter. */ private UnivariateStatistic varianceImpl = new Variance(); /** Sum of squares statistic implementation - can be reset by setter. */ private UnivariateStatistic sumsqImpl = new SumOfSquares(); /** Sum statistic implementation - can be reset by setter. */ private UnivariateStatistic sumImpl = new Sum(); /** * Construct a DescriptiveStatistics instance with an infinite window */ public DescriptiveStatistics() { } /** * Construct a DescriptiveStatistics instance with the specified window * * @param window the window size. * @throws MathIllegalArgumentException if window size is less than 1 but * not equal to {@link #INFINITE_WINDOW} */ public DescriptiveStatistics(int window) throws MathIllegalArgumentException { setWindowSize(window); } /** * Construct a DescriptiveStatistics instance with an infinite window * and the initial data values in double[] initialDoubleArray. * If initialDoubleArray is null, then this constructor corresponds to * DescriptiveStatistics() * * @param initialDoubleArray the initial double[]. */ public DescriptiveStatistics(double[] initialDoubleArray) { if (initialDoubleArray != null) { eDA = new ResizableDoubleArray(initialDoubleArray); } } /** * Copy constructor. Construct a new DescriptiveStatistics instance that * is a copy of original. * * @param original DescriptiveStatistics instance to copy * @throws NullArgumentException if original is null */ public DescriptiveStatistics(DescriptiveStatistics original) throws NullArgumentException { copy(original, this); } /** * Adds the value to the dataset. If the dataset is at the maximum size * (i.e., the number of stored elements equals the currently configured * windowSize), the first (oldest) element in the dataset is discarded * to make room for the new value. * * @param v the value to be added */ public void addValue(double v) { if (windowSize != INFINITE_WINDOW) { if (getN() == windowSize) { eDA.addElementRolling(v); } else if (getN() < windowSize) { eDA.addElement(v); } } else { eDA.addElement(v); } } /** * Removes the most recent value from the dataset. * * @throws MathIllegalStateException if there are no elements stored */ public void removeMostRecentValue() throws MathIllegalStateException { try { eDA.discardMostRecentElements(1); } catch (MathIllegalArgumentException ex) { throw new MathIllegalStateException(LocalizedFormats.NO_DATA); } } /** * Replaces the most recently stored value with the given value. * There must be at least one element stored to call this method. * * @param v the value to replace the most recent stored value * @return replaced value * @throws MathIllegalStateException if there are no elements stored */ public double replaceMostRecentValue(double v) throws MathIllegalStateException { return eDA.substituteMostRecentElement(v); } /** * Returns the * arithmetic mean of the available values * @return The mean or Double.NaN if no values have been added. */ public double getMean() { return apply(meanImpl); } /** * Returns the * geometric mean of the available values * @return The geometricMean, Double.NaN if no values have been added, * or if the product of the available values is less than or equal to 0. */ public double getGeometricMean() { return apply(geometricMeanImpl); } /** * Returns the (sample) variance of the available values. * *

                              This method returns the bias-corrected sample variance (using {@code n - 1} in * the denominator). Use {@link #getPopulationVariance()} for the non-bias-corrected * population variance.

                              * * @return The variance, Double.NaN if no values have been added * or 0.0 for a single value set. */ public double getVariance() { return apply(varianceImpl); } /** * Returns the * population variance of the available values. * * @return The population variance, Double.NaN if no values have been added, * or 0.0 for a single value set. */ public double getPopulationVariance() { return apply(new Variance(false)); } /** * Returns the standard deviation of the available values. * @return The standard deviation, Double.NaN if no values have been added * or 0.0 for a single value set. */ public double getStandardDeviation() { double stdDev = Double.NaN; if (getN() > 0) { if (getN() > 1) { stdDev = FastMath.sqrt(getVariance()); } else { stdDev = 0.0; } } return stdDev; } /** * Returns the skewness of the available values. Skewness is a * measure of the asymmetry of a given distribution. * @return The skewness, Double.NaN if no values have been added * or 0.0 for a value set <=2. */ public double getSkewness() { return apply(skewnessImpl); } /** * Returns the Kurtosis of the available values. Kurtosis is a * measure of the "peakedness" of a distribution * @return The kurtosis, Double.NaN if no values have been added, or 0.0 * for a value set <=3. */ public double getKurtosis() { return apply(kurtosisImpl); } /** * Returns the maximum of the available values * @return The max or Double.NaN if no values have been added. */ public double getMax() { return apply(maxImpl); } /** * Returns the minimum of the available values * @return The min or Double.NaN if no values have been added. */ public double getMin() { return apply(minImpl); } /** * Returns the number of available values * @return The number of available values */ public long getN() { return eDA.getNumElements(); } /** * Returns the sum of the values that have been added to Univariate. * @return The sum or Double.NaN if no values have been added */ public double getSum() { return apply(sumImpl); } /** * Returns the sum of the squares of the available values. * @return The sum of the squares or Double.NaN if no * values have been added. */ public double getSumsq() { return apply(sumsqImpl); } /** * Resets all statistics and storage */ public void clear() { eDA.clear(); } /** * Returns the maximum number of values that can be stored in the * dataset, or INFINITE_WINDOW (-1) if there is no limit. * * @return The current window size or -1 if its Infinite. */ public int getWindowSize() { return windowSize; } /** * WindowSize controls the number of values that contribute to the * reported statistics. For example, if windowSize is set to 3 and the * values {1,2,3,4,5} have been added in that order then * the available values are {3,4,5} and all reported statistics will * be based on these values. If {@code windowSize} is decreased as a result * of this call and there are more than the new value of elements in the * current dataset, values from the front of the array are discarded to * reduce the dataset to {@code windowSize} elements. * * @param windowSize sets the size of the window. * @throws MathIllegalArgumentException if window size is less than 1 but * not equal to {@link #INFINITE_WINDOW} */ public void setWindowSize(int windowSize) throws MathIllegalArgumentException { if (windowSize < 1 && windowSize != INFINITE_WINDOW) { throw new MathIllegalArgumentException( LocalizedFormats.NOT_POSITIVE_WINDOW_SIZE, windowSize); } this.windowSize = windowSize; // We need to check to see if we need to discard elements // from the front of the array. If the windowSize is less than // the current number of elements. if (windowSize != INFINITE_WINDOW && windowSize < eDA.getNumElements()) { eDA.discardFrontElements(eDA.getNumElements() - windowSize); } } /** * Returns the current set of values in an array of double primitives. * The order of addition is preserved. The returned array is a fresh * copy of the underlying data -- i.e., it is not a reference to the * stored data. * * @return returns the current set of numbers in the order in which they * were added to this set */ public double[] getValues() { return eDA.getElements(); } /** * Returns the current set of values in an array of double primitives, * sorted in ascending order. The returned array is a fresh * copy of the underlying data -- i.e., it is not a reference to the * stored data. * @return returns the current set of * numbers sorted in ascending order */ public double[] getSortedValues() { double[] sort = getValues(); Arrays.sort(sort); return sort; } /** * Returns the element at the specified index * @param index The Index of the element * @return return the element at the specified index */ public double getElement(int index) { return eDA.getElement(index); } /** * Returns an estimate for the pth percentile of the stored values. *

                              * The implementation provided here follows the first estimation procedure presented * here. *

                              * Preconditions:

                                *
                              • 0 < p ≤ 100 (otherwise an * MathIllegalArgumentException is thrown)
                              • *
                              • at least one value must be stored (returns Double.NaN * otherwise)
                              • *

                              * * @param p the requested percentile (scaled from 0 - 100) * @return An estimate for the pth percentile of the stored data * @throws MathIllegalStateException if percentile implementation has been * overridden and the supplied implementation does not support setQuantile * @throws MathIllegalArgumentException if p is not a valid quantile */ public double getPercentile(double p) throws MathIllegalStateException, MathIllegalArgumentException { if (percentileImpl instanceof Percentile) { ((Percentile) percentileImpl).setQuantile(p); } else { try { percentileImpl.getClass().getMethod(SET_QUANTILE_METHOD_NAME, new Class[] {Double.TYPE}).invoke(percentileImpl, new Object[] {Double.valueOf(p)}); } catch (NoSuchMethodException e1) { // Setter guard should prevent throw new MathIllegalStateException( LocalizedFormats.PERCENTILE_IMPLEMENTATION_UNSUPPORTED_METHOD, percentileImpl.getClass().getName(), SET_QUANTILE_METHOD_NAME); } catch (IllegalAccessException e2) { throw new MathIllegalStateException( LocalizedFormats.PERCENTILE_IMPLEMENTATION_CANNOT_ACCESS_METHOD, SET_QUANTILE_METHOD_NAME, percentileImpl.getClass().getName()); } catch (InvocationTargetException e3) { throw new IllegalStateException(e3.getCause()); } } return apply(percentileImpl); } /** * Generates a text report displaying univariate statistics from values * that have been added. Each statistic is displayed on a separate * line. * * @return String with line feeds displaying statistics */ @Override public String toString() { StringBuilder outBuffer = new StringBuilder(); String endl = "\n"; outBuffer.append("DescriptiveStatistics:").append(endl); outBuffer.append("n: ").append(getN()).append(endl); outBuffer.append("min: ").append(getMin()).append(endl); outBuffer.append("max: ").append(getMax()).append(endl); outBuffer.append("mean: ").append(getMean()).append(endl); outBuffer.append("std dev: ").append(getStandardDeviation()) .append(endl); try { // No catch for MIAE because actual parameter is valid below outBuffer.append("median: ").append(getPercentile(50)).append(endl); } catch (MathIllegalStateException ex) { outBuffer.append("median: unavailable").append(endl); } outBuffer.append("skewness: ").append(getSkewness()).append(endl); outBuffer.append("kurtosis: ").append(getKurtosis()).append(endl); return outBuffer.toString(); } /** * Apply the given statistic to the data associated with this set of statistics. * @param stat the statistic to apply * @return the computed value of the statistic. */ public double apply(UnivariateStatistic stat) { // No try-catch or advertised exception here because arguments are guaranteed valid return eDA.compute(stat); } // Implementation getters and setter /** * Returns the currently configured mean implementation. * * @return the UnivariateStatistic implementing the mean * @since 1.2 */ public synchronized UnivariateStatistic getMeanImpl() { return meanImpl; } /** *

                              Sets the implementation for the mean.

                              * * @param meanImpl the UnivariateStatistic instance to use * for computing the mean * @since 1.2 */ public synchronized void setMeanImpl(UnivariateStatistic meanImpl) { this.meanImpl = meanImpl; } /** * Returns the currently configured geometric mean implementation. * * @return the UnivariateStatistic implementing the geometric mean * @since 1.2 */ public synchronized UnivariateStatistic getGeometricMeanImpl() { return geometricMeanImpl; } /** *

                              Sets the implementation for the gemoetric mean.

                              * * @param geometricMeanImpl the UnivariateStatistic instance to use * for computing the geometric mean * @since 1.2 */ public synchronized void setGeometricMeanImpl( UnivariateStatistic geometricMeanImpl) { this.geometricMeanImpl = geometricMeanImpl; } /** * Returns the currently configured kurtosis implementation. * * @return the UnivariateStatistic implementing the kurtosis * @since 1.2 */ public synchronized UnivariateStatistic getKurtosisImpl() { return kurtosisImpl; } /** *

                              Sets the implementation for the kurtosis.

                              * * @param kurtosisImpl the UnivariateStatistic instance to use * for computing the kurtosis * @since 1.2 */ public synchronized void setKurtosisImpl(UnivariateStatistic kurtosisImpl) { this.kurtosisImpl = kurtosisImpl; } /** * Returns the currently configured maximum implementation. * * @return the UnivariateStatistic implementing the maximum * @since 1.2 */ public synchronized UnivariateStatistic getMaxImpl() { return maxImpl; } /** *

                              Sets the implementation for the maximum.

                              * * @param maxImpl the UnivariateStatistic instance to use * for computing the maximum * @since 1.2 */ public synchronized void setMaxImpl(UnivariateStatistic maxImpl) { this.maxImpl = maxImpl; } /** * Returns the currently configured minimum implementation. * * @return the UnivariateStatistic implementing the minimum * @since 1.2 */ public synchronized UnivariateStatistic getMinImpl() { return minImpl; } /** *

                              Sets the implementation for the minimum.

                              * * @param minImpl the UnivariateStatistic instance to use * for computing the minimum * @since 1.2 */ public synchronized void setMinImpl(UnivariateStatistic minImpl) { this.minImpl = minImpl; } /** * Returns the currently configured percentile implementation. * * @return the UnivariateStatistic implementing the percentile * @since 1.2 */ public synchronized UnivariateStatistic getPercentileImpl() { return percentileImpl; } /** * Sets the implementation to be used by {@link #getPercentile(double)}. * The supplied UnivariateStatistic must provide a * setQuantile(double) method; otherwise * IllegalArgumentException is thrown. * * @param percentileImpl the percentileImpl to set * @throws MathIllegalArgumentException if the supplied implementation does not * provide a setQuantile method * @since 1.2 */ public synchronized void setPercentileImpl(UnivariateStatistic percentileImpl) throws MathIllegalArgumentException { try { percentileImpl.getClass().getMethod(SET_QUANTILE_METHOD_NAME, new Class[] {Double.TYPE}).invoke(percentileImpl, new Object[] {Double.valueOf(50.0d)}); } catch (NoSuchMethodException e1) { throw new MathIllegalArgumentException( LocalizedFormats.PERCENTILE_IMPLEMENTATION_UNSUPPORTED_METHOD, percentileImpl.getClass().getName(), SET_QUANTILE_METHOD_NAME); } catch (IllegalAccessException e2) { throw new MathIllegalArgumentException( LocalizedFormats.PERCENTILE_IMPLEMENTATION_CANNOT_ACCESS_METHOD, SET_QUANTILE_METHOD_NAME, percentileImpl.getClass().getName()); } catch (InvocationTargetException e3) { throw new IllegalArgumentException(e3.getCause()); } this.percentileImpl = percentileImpl; } /** * Returns the currently configured skewness implementation. * * @return the UnivariateStatistic implementing the skewness * @since 1.2 */ public synchronized UnivariateStatistic getSkewnessImpl() { return skewnessImpl; } /** *

                              Sets the implementation for the skewness.

                              * * @param skewnessImpl the UnivariateStatistic instance to use * for computing the skewness * @since 1.2 */ public synchronized void setSkewnessImpl( UnivariateStatistic skewnessImpl) { this.skewnessImpl = skewnessImpl; } /** * Returns the currently configured variance implementation. * * @return the UnivariateStatistic implementing the variance * @since 1.2 */ public synchronized UnivariateStatistic getVarianceImpl() { return varianceImpl; } /** *

                              Sets the implementation for the variance.

                              * * @param varianceImpl the UnivariateStatistic instance to use * for computing the variance * @since 1.2 */ public synchronized void setVarianceImpl( UnivariateStatistic varianceImpl) { this.varianceImpl = varianceImpl; } /** * Returns the currently configured sum of squares implementation. * * @return the UnivariateStatistic implementing the sum of squares * @since 1.2 */ public synchronized UnivariateStatistic getSumsqImpl() { return sumsqImpl; } /** *

                              Sets the implementation for the sum of squares.

                              * * @param sumsqImpl the UnivariateStatistic instance to use * for computing the sum of squares * @since 1.2 */ public synchronized void setSumsqImpl(UnivariateStatistic sumsqImpl) { this.sumsqImpl = sumsqImpl; } /** * Returns the currently configured sum implementation. * * @return the UnivariateStatistic implementing the sum * @since 1.2 */ public synchronized UnivariateStatistic getSumImpl() { return sumImpl; } /** *

                              Sets the implementation for the sum.

                              * * @param sumImpl the UnivariateStatistic instance to use * for computing the sum * @since 1.2 */ public synchronized void setSumImpl(UnivariateStatistic sumImpl) { this.sumImpl = sumImpl; } /** * Returns a copy of this DescriptiveStatistics instance with the same internal state. * * @return a copy of this */ public DescriptiveStatistics copy() { DescriptiveStatistics result = new DescriptiveStatistics(); // No try-catch or advertised exception because parms are guaranteed valid copy(this, result); return result; } /** * Copies source to dest. *

                              Neither source nor dest can be null.

                              * * @param source DescriptiveStatistics to copy * @param dest DescriptiveStatistics to copy to * @throws NullArgumentException if either source or dest is null */ public static void copy(DescriptiveStatistics source, DescriptiveStatistics dest) throws NullArgumentException { MathUtils.checkNotNull(source); MathUtils.checkNotNull(dest); // Copy data and window size dest.eDA = source.eDA.copy(); dest.windowSize = source.windowSize; // Copy implementations dest.maxImpl = source.maxImpl.copy(); dest.meanImpl = source.meanImpl.copy(); dest.minImpl = source.minImpl.copy(); dest.sumImpl = source.sumImpl.copy(); dest.varianceImpl = source.varianceImpl.copy(); dest.sumsqImpl = source.sumsqImpl.copy(); dest.geometricMeanImpl = source.geometricMeanImpl.copy(); dest.kurtosisImpl = source.kurtosisImpl; dest.skewnessImpl = source.skewnessImpl; dest.percentileImpl = source.percentileImpl; } } ././@LongLink100644 0 0 147 12126630647 10264 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/stat/descriptive/UnivariateStatistic.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/stat/descriptive/UnivariateStatistic.ja100644 1750 1750 4275 12126627717 32346 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.stat.descriptive; import org.apache.commons.math3.exception.MathIllegalArgumentException; import org.apache.commons.math3.util.MathArrays; /** * Base interface implemented by all statistics. * * @version $Id: UnivariateStatistic.java 1416643 2012-12-03 19:37:14Z tn $ */ public interface UnivariateStatistic extends MathArrays.Function { /** * Returns the result of evaluating the statistic over the input array. * * @param values input array * @return the value of the statistic applied to the input array * @throws MathIllegalArgumentException if values is null */ double evaluate(double[] values) throws MathIllegalArgumentException; /** * Returns the result of evaluating the statistic over the specified entries * in the input array. * * @param values the input array * @param begin the index of the first element to include * @param length the number of elements to include * @return the value of the statistic applied to the included array entries * @throws MathIllegalArgumentException if values is null or the indices are invalid */ double evaluate(double[] values, int begin, int length) throws MathIllegalArgumentException; /** * Returns a copy of the statistic with the same internal state. * * @return a copy of the statistic */ UnivariateStatistic copy(); } ././@LongLink100644 0 0 145 12126630647 10262 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/stat/descriptive/rank/package-info.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/stat/descriptive/rank/package-info.java100644 1750 1750 1611 12126627717 32144 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ /** * Summary statistics based on ranks. */ package org.apache.commons.math3.stat.descriptive.rank; commons-math3-3.2-src/src/main/java/org/apache/commons/math3/stat/descriptive/rank/Min.java100644 1750 1750 12535 12126627717 30372 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.stat.descriptive.rank; import java.io.Serializable; import org.apache.commons.math3.exception.MathIllegalArgumentException; import org.apache.commons.math3.exception.NullArgumentException; import org.apache.commons.math3.stat.descriptive.AbstractStorelessUnivariateStatistic; import org.apache.commons.math3.util.MathUtils; /** * Returns the minimum of the available values. *

                              *

                                *
                              • The result is NaN iff all values are NaN * (i.e. NaN values have no impact on the value of the statistic).
                              • *
                              • If any of the values equals Double.NEGATIVE_INFINITY, * the result is Double.NEGATIVE_INFINITY.
                              • *

                              *

                              * Note that this implementation is not synchronized. If * multiple threads access an instance of this class concurrently, and at least * one of the threads invokes the increment() or * clear() method, it must be synchronized externally.

                              * * @version $Id: Min.java 1416643 2012-12-03 19:37:14Z tn $ */ public class Min extends AbstractStorelessUnivariateStatistic implements Serializable { /** Serializable version identifier */ private static final long serialVersionUID = -2941995784909003131L; /**Number of values that have been added */ private long n; /**Current value of the statistic */ private double value; /** * Create a Min instance */ public Min() { n = 0; value = Double.NaN; } /** * Copy constructor, creates a new {@code Min} identical * to the {@code original} * * @param original the {@code Min} instance to copy * @throws NullArgumentException if original is null */ public Min(Min original) throws NullArgumentException { copy(original, this); } /** * {@inheritDoc} */ @Override public void increment(final double d) { if (d < value || Double.isNaN(value)) { value = d; } n++; } /** * {@inheritDoc} */ @Override public void clear() { value = Double.NaN; n = 0; } /** * {@inheritDoc} */ @Override public double getResult() { return value; } /** * {@inheritDoc} */ public long getN() { return n; } /** * Returns the minimum of the entries in the specified portion of * the input array, or Double.NaN if the designated subarray * is empty. *

                              * Throws MathIllegalArgumentException if the array is null or * the array index parameters are not valid.

                              *

                              *

                                *
                              • The result is NaN iff all values are NaN * (i.e. NaN values have no impact on the value of the statistic).
                              • *
                              • If any of the values equals Double.NEGATIVE_INFINITY, * the result is Double.NEGATIVE_INFINITY.
                              • *

                              * * @param values the input array * @param begin index of the first array element to include * @param length the number of elements to include * @return the minimum of the values or Double.NaN if length = 0 * @throws MathIllegalArgumentException if the array is null or the array index * parameters are not valid */ @Override public double evaluate(final double[] values,final int begin, final int length) throws MathIllegalArgumentException { double min = Double.NaN; if (test(values, begin, length)) { min = values[begin]; for (int i = begin; i < begin + length; i++) { if (!Double.isNaN(values[i])) { min = (min < values[i]) ? min : values[i]; } } } return min; } /** * {@inheritDoc} */ @Override public Min copy() { Min result = new Min(); // No try-catch or advertised exception - args are non-null copy(this, result); return result; } /** * Copies source to dest. *

                              Neither source nor dest can be null.

                              * * @param source Min to copy * @param dest Min to copy to * @throws NullArgumentException if either source or dest is null */ public static void copy(Min source, Min dest) throws NullArgumentException { MathUtils.checkNotNull(source); MathUtils.checkNotNull(dest); dest.setData(source.getDataRef()); dest.n = source.n; dest.value = source.value; } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/stat/descriptive/rank/Percentile.java100644 1750 1750 46436 12126627717 31750 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.stat.descriptive.rank; import java.io.Serializable; import java.util.Arrays; import org.apache.commons.math3.exception.MathIllegalArgumentException; import org.apache.commons.math3.exception.NullArgumentException; import org.apache.commons.math3.exception.OutOfRangeException; import org.apache.commons.math3.exception.util.LocalizedFormats; import org.apache.commons.math3.stat.descriptive.AbstractUnivariateStatistic; import org.apache.commons.math3.util.FastMath; import org.apache.commons.math3.util.MathUtils; /** * Provides percentile computation. *

                              * There are several commonly used methods for estimating percentiles (a.k.a. * quantiles) based on sample data. For large samples, the different methods * agree closely, but when sample sizes are small, different methods will give * significantly different results. The algorithm implemented here works as follows: *

                                *
                              1. Let n be the length of the (sorted) array and * 0 < p <= 100 be the desired percentile.
                              2. *
                              3. If n = 1 return the unique array element (regardless of * the value of p); otherwise
                              4. *
                              5. Compute the estimated percentile position * pos = p * (n + 1) / 100 and the difference, d * between pos and floor(pos) (i.e. the fractional * part of pos).
                              6. *
                              7. If pos < 1 return the smallest element in the array.
                              8. *
                              9. Else if pos >= n return the largest element in the array.
                              10. *
                              11. Else let lower be the element in position * floor(pos) in the array and let upper be the * next element in the array. Return lower + d * (upper - lower) *
                              12. *

                              *

                              * To compute percentiles, the data must be at least partially ordered. Input * arrays are copied and recursively partitioned using an ordering definition. * The ordering used by Arrays.sort(double[]) is the one determined * by {@link java.lang.Double#compareTo(Double)}. This ordering makes * Double.NaN larger than any other value (including * Double.POSITIVE_INFINITY). Therefore, for example, the median * (50th percentile) of * {0, 1, 2, 3, 4, Double.NaN} evaluates to 2.5.

                              *

                              * Since percentile estimation usually involves interpolation between array * elements, arrays containing NaN or infinite values will often * result in NaN or infinite values returned.

                              *

                              * Since 2.2, Percentile uses only selection instead of complete sorting * and caches selection algorithm state between calls to the various * {@code evaluate} methods. This greatly improves efficiency, both for a single * percentile and multiple percentile computations. To maximize performance when * multiple percentiles are computed based on the same data, users should set the * data array once using either one of the {@link #evaluate(double[], double)} or * {@link #setData(double[])} methods and thereafter {@link #evaluate(double)} * with just the percentile provided. *

                              *

                              * Note that this implementation is not synchronized. If * multiple threads access an instance of this class concurrently, and at least * one of the threads invokes the increment() or * clear() method, it must be synchronized externally.

                              * * @version $Id: Percentile.java 1416643 2012-12-03 19:37:14Z tn $ */ public class Percentile extends AbstractUnivariateStatistic implements Serializable { /** Serializable version identifier */ private static final long serialVersionUID = -8091216485095130416L; /** Minimum size under which we use a simple insertion sort rather than Hoare's select. */ private static final int MIN_SELECT_SIZE = 15; /** Maximum number of partitioning pivots cached (each level double the number of pivots). */ private static final int MAX_CACHED_LEVELS = 10; /** Determines what percentile is computed when evaluate() is activated * with no quantile argument */ private double quantile = 0.0; /** Cached pivots. */ private int[] cachedPivots; /** * Constructs a Percentile with a default quantile * value of 50.0. */ public Percentile() { // No try-catch or advertised exception here - arg is valid this(50.0); } /** * Constructs a Percentile with the specific quantile value. * @param p the quantile * @throws MathIllegalArgumentException if p is not greater than 0 and less * than or equal to 100 */ public Percentile(final double p) throws MathIllegalArgumentException { setQuantile(p); cachedPivots = null; } /** * Copy constructor, creates a new {@code Percentile} identical * to the {@code original} * * @param original the {@code Percentile} instance to copy * @throws NullArgumentException if original is null */ public Percentile(Percentile original) throws NullArgumentException { copy(original, this); } /** {@inheritDoc} */ @Override public void setData(final double[] values) { if (values == null) { cachedPivots = null; } else { cachedPivots = new int[(0x1 << MAX_CACHED_LEVELS) - 1]; Arrays.fill(cachedPivots, -1); } super.setData(values); } /** {@inheritDoc} */ @Override public void setData(final double[] values, final int begin, final int length) throws MathIllegalArgumentException { if (values == null) { cachedPivots = null; } else { cachedPivots = new int[(0x1 << MAX_CACHED_LEVELS) - 1]; Arrays.fill(cachedPivots, -1); } super.setData(values, begin, length); } /** * Returns the result of evaluating the statistic over the stored data. *

                              * The stored array is the one which was set by previous calls to * {@link #setData(double[])} *

                              * @param p the percentile value to compute * @return the value of the statistic applied to the stored data * @throws MathIllegalArgumentException if p is not a valid quantile value * (p must be greater than 0 and less than or equal to 100) */ public double evaluate(final double p) throws MathIllegalArgumentException { return evaluate(getDataRef(), p); } /** * Returns an estimate of the pth percentile of the values * in the values array. *

                              * Calls to this method do not modify the internal quantile * state of this statistic.

                              *

                              *

                                *
                              • Returns Double.NaN if values has length * 0
                              • *
                              • Returns (for any value of p) values[0] * if values has length 1
                              • *
                              • Throws MathIllegalArgumentException if values * is null or p is not a valid quantile value (p must be greater than 0 * and less than or equal to 100)
                              • *

                              *

                              * See {@link Percentile} for a description of the percentile estimation * algorithm used.

                              * * @param values input array of values * @param p the percentile value to compute * @return the percentile value or Double.NaN if the array is empty * @throws MathIllegalArgumentException if values is null * or p is invalid */ public double evaluate(final double[] values, final double p) throws MathIllegalArgumentException { test(values, 0, 0); return evaluate(values, 0, values.length, p); } /** * Returns an estimate of the quantileth percentile of the * designated values in the values array. The quantile * estimated is determined by the quantile property. *

                              *

                                *
                              • Returns Double.NaN if length = 0
                              • *
                              • Returns (for any value of quantile) * values[begin] if length = 1
                              • *
                              • Throws MathIllegalArgumentException if values * is null, or start or length is invalid
                              • *

                              *

                              * See {@link Percentile} for a description of the percentile estimation * algorithm used.

                              * * @param values the input array * @param start index of the first array element to include * @param length the number of elements to include * @return the percentile value * @throws MathIllegalArgumentException if the parameters are not valid * */ @Override public double evaluate(final double[] values, final int start, final int length) throws MathIllegalArgumentException { return evaluate(values, start, length, quantile); } /** * Returns an estimate of the pth percentile of the values * in the values array, starting with the element in (0-based) * position begin in the array and including length * values. *

                              * Calls to this method do not modify the internal quantile * state of this statistic.

                              *

                              *

                                *
                              • Returns Double.NaN if length = 0
                              • *
                              • Returns (for any value of p) values[begin] * if length = 1
                              • *
                              • Throws MathIllegalArgumentException if values * is null , begin or length is invalid, or * p is not a valid quantile value (p must be greater than 0 * and less than or equal to 100)
                              • *

                              *

                              * See {@link Percentile} for a description of the percentile estimation * algorithm used.

                              * * @param values array of input values * @param p the percentile to compute * @param begin the first (0-based) element to include in the computation * @param length the number of array elements to include * @return the percentile value * @throws MathIllegalArgumentException if the parameters are not valid or the * input array is null */ public double evaluate(final double[] values, final int begin, final int length, final double p) throws MathIllegalArgumentException { test(values, begin, length); if ((p > 100) || (p <= 0)) { throw new OutOfRangeException( LocalizedFormats.OUT_OF_BOUNDS_QUANTILE_VALUE, p, 0, 100); } if (length == 0) { return Double.NaN; } if (length == 1) { return values[begin]; // always return single value for n = 1 } double n = length; double pos = p * (n + 1) / 100; double fpos = FastMath.floor(pos); int intPos = (int) fpos; double dif = pos - fpos; double[] work; int[] pivotsHeap; if (values == getDataRef()) { work = getDataRef(); pivotsHeap = cachedPivots; } else { work = new double[length]; System.arraycopy(values, begin, work, 0, length); pivotsHeap = new int[(0x1 << MAX_CACHED_LEVELS) - 1]; Arrays.fill(pivotsHeap, -1); } if (pos < 1) { return select(work, pivotsHeap, 0); } if (pos >= n) { return select(work, pivotsHeap, length - 1); } double lower = select(work, pivotsHeap, intPos - 1); double upper = select(work, pivotsHeap, intPos); return lower + dif * (upper - lower); } /** * Select the kth smallest element from work array * @param work work array (will be reorganized during the call) * @param pivotsHeap set of pivot index corresponding to elements that * are already at their sorted location, stored as an implicit heap * (i.e. a sorted binary tree stored in a flat array, where the * children of a node at index n are at indices 2n+1 for the left * child and 2n+2 for the right child, with 0-based indices) * @param k index of the desired element * @return kth smallest element */ private double select(final double[] work, final int[] pivotsHeap, final int k) { int begin = 0; int end = work.length; int node = 0; while (end - begin > MIN_SELECT_SIZE) { final int pivot; if ((node < pivotsHeap.length) && (pivotsHeap[node] >= 0)) { // the pivot has already been found in a previous call // and the array has already been partitioned around it pivot = pivotsHeap[node]; } else { // select a pivot and partition work array around it pivot = partition(work, begin, end, medianOf3(work, begin, end)); if (node < pivotsHeap.length) { pivotsHeap[node] = pivot; } } if (k == pivot) { // the pivot was exactly the element we wanted return work[k]; } else if (k < pivot) { // the element is in the left partition end = pivot; node = FastMath.min(2 * node + 1, pivotsHeap.length); // the min is here to avoid integer overflow } else { // the element is in the right partition begin = pivot + 1; node = FastMath.min(2 * node + 2, pivotsHeap.length); // the min is here to avoid integer overflow } } // the element is somewhere in the small sub-array // sort the sub-array using insertion sort insertionSort(work, begin, end); return work[k]; } /** Select a pivot index as the median of three * @param work data array * @param begin index of the first element of the slice * @param end index after the last element of the slice * @return the index of the median element chosen between the * first, the middle and the last element of the array slice */ int medianOf3(final double[] work, final int begin, final int end) { final int inclusiveEnd = end - 1; final int middle = begin + (inclusiveEnd - begin) / 2; final double wBegin = work[begin]; final double wMiddle = work[middle]; final double wEnd = work[inclusiveEnd]; if (wBegin < wMiddle) { if (wMiddle < wEnd) { return middle; } else { return (wBegin < wEnd) ? inclusiveEnd : begin; } } else { if (wBegin < wEnd) { return begin; } else { return (wMiddle < wEnd) ? inclusiveEnd : middle; } } } /** * Partition an array slice around a pivot *

                              * Partitioning exchanges array elements such that all elements * smaller than pivot are before it and all elements larger than * pivot are after it *

                              * @param work data array * @param begin index of the first element of the slice * @param end index after the last element of the slice * @param pivot initial index of the pivot * @return index of the pivot after partition */ private int partition(final double[] work, final int begin, final int end, final int pivot) { final double value = work[pivot]; work[pivot] = work[begin]; int i = begin + 1; int j = end - 1; while (i < j) { while ((i < j) && (work[j] > value)) { --j; } while ((i < j) && (work[i] < value)) { ++i; } if (i < j) { final double tmp = work[i]; work[i++] = work[j]; work[j--] = tmp; } } if ((i >= end) || (work[i] > value)) { --i; } work[begin] = work[i]; work[i] = value; return i; } /** * Sort in place a (small) array slice using insertion sort * @param work array to sort * @param begin index of the first element of the slice to sort * @param end index after the last element of the slice to sort */ private void insertionSort(final double[] work, final int begin, final int end) { for (int j = begin + 1; j < end; j++) { final double saved = work[j]; int i = j - 1; while ((i >= begin) && (saved < work[i])) { work[i + 1] = work[i]; i--; } work[i + 1] = saved; } } /** * Returns the value of the quantile field (determines what percentile is * computed when evaluate() is called with no quantile argument). * * @return quantile */ public double getQuantile() { return quantile; } /** * Sets the value of the quantile field (determines what percentile is * computed when evaluate() is called with no quantile argument). * * @param p a value between 0 < p <= 100 * @throws MathIllegalArgumentException if p is not greater than 0 and less * than or equal to 100 */ public void setQuantile(final double p) throws MathIllegalArgumentException { if (p <= 0 || p > 100) { throw new OutOfRangeException( LocalizedFormats.OUT_OF_BOUNDS_QUANTILE_VALUE, p, 0, 100); } quantile = p; } /** * {@inheritDoc} */ @Override public Percentile copy() { Percentile result = new Percentile(); //No try-catch or advertised exception because args are guaranteed non-null copy(this, result); return result; } /** * Copies source to dest. *

                              Neither source nor dest can be null.

                              * * @param source Percentile to copy * @param dest Percentile to copy to * @throws NullArgumentException if either source or dest is null */ public static void copy(Percentile source, Percentile dest) throws NullArgumentException { MathUtils.checkNotNull(source); MathUtils.checkNotNull(dest); dest.setData(source.getDataRef()); if (source.cachedPivots != null) { System.arraycopy(source.cachedPivots, 0, dest.cachedPivots, 0, source.cachedPivots.length); } dest.quantile = source.quantile; } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/stat/descriptive/rank/Median.java100644 1750 1750 4153 12126627717 31021 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.stat.descriptive.rank; import java.io.Serializable; import org.apache.commons.math3.exception.NullArgumentException; /** * Returns the median of the available values. This is the same as the 50th percentile. * See {@link Percentile} for a description of the algorithm used. *

                              * Note that this implementation is not synchronized. If * multiple threads access an instance of this class concurrently, and at least * one of the threads invokes the increment() or * clear() method, it must be synchronized externally.

                              * * @version $Id: Median.java 1416643 2012-12-03 19:37:14Z tn $ */ public class Median extends Percentile implements Serializable { /** Serializable version identifier */ private static final long serialVersionUID = -3961477041290915687L; /** * Default constructor. */ public Median() { // No try-catch or advertised exception - arg is valid super(50.0); } /** * Copy constructor, creates a new {@code Median} identical * to the {@code original} * * @param original the {@code Median} instance to copy * @throws NullArgumentException if original is null */ public Median(Median original) throws NullArgumentException { super(original); } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/stat/descriptive/rank/Max.java100644 1750 1750 12544 12126627717 30374 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.stat.descriptive.rank; import java.io.Serializable; import org.apache.commons.math3.exception.MathIllegalArgumentException; import org.apache.commons.math3.exception.NullArgumentException; import org.apache.commons.math3.stat.descriptive.AbstractStorelessUnivariateStatistic; import org.apache.commons.math3.util.MathUtils; /** * Returns the maximum of the available values. *

                              *

                                *
                              • The result is NaN iff all values are NaN * (i.e. NaN values have no impact on the value of the statistic).
                              • *
                              • If any of the values equals Double.POSITIVE_INFINITY, * the result is Double.POSITIVE_INFINITY.
                              • *

                              *

                              * Note that this implementation is not synchronized. If * multiple threads access an instance of this class concurrently, and at least * one of the threads invokes the increment() or * clear() method, it must be synchronized externally.

                              * * @version $Id: Max.java 1416643 2012-12-03 19:37:14Z tn $ */ public class Max extends AbstractStorelessUnivariateStatistic implements Serializable { /** Serializable version identifier */ private static final long serialVersionUID = -5593383832225844641L; /** Number of values that have been added */ private long n; /** Current value of the statistic */ private double value; /** * Create a Max instance */ public Max() { n = 0; value = Double.NaN; } /** * Copy constructor, creates a new {@code Max} identical * to the {@code original} * * @param original the {@code Max} instance to copy * @throws NullArgumentException if original is null */ public Max(Max original) throws NullArgumentException { copy(original, this); } /** * {@inheritDoc} */ @Override public void increment(final double d) { if (d > value || Double.isNaN(value)) { value = d; } n++; } /** * {@inheritDoc} */ @Override public void clear() { value = Double.NaN; n = 0; } /** * {@inheritDoc} */ @Override public double getResult() { return value; } /** * {@inheritDoc} */ public long getN() { return n; } /** * Returns the maximum of the entries in the specified portion of * the input array, or Double.NaN if the designated subarray * is empty. *

                              * Throws MathIllegalArgumentException if the array is null or * the array index parameters are not valid.

                              *

                              *

                                *
                              • The result is NaN iff all values are NaN * (i.e. NaN values have no impact on the value of the statistic).
                              • *
                              • If any of the values equals Double.POSITIVE_INFINITY, * the result is Double.POSITIVE_INFINITY.
                              • *

                              * * @param values the input array * @param begin index of the first array element to include * @param length the number of elements to include * @return the maximum of the values or Double.NaN if length = 0 * @throws MathIllegalArgumentException if the array is null or the array index * parameters are not valid */ @Override public double evaluate(final double[] values, final int begin, final int length) throws MathIllegalArgumentException { double max = Double.NaN; if (test(values, begin, length)) { max = values[begin]; for (int i = begin; i < begin + length; i++) { if (!Double.isNaN(values[i])) { max = (max > values[i]) ? max : values[i]; } } } return max; } /** * {@inheritDoc} */ @Override public Max copy() { Max result = new Max(); // No try-catch or advertised exception because args are non-null copy(this, result); return result; } /** * Copies source to dest. *

                              Neither source nor dest can be null.

                              * * @param source Max to copy * @param dest Max to copy to * @throws NullArgumentException if either source or dest is null */ public static void copy(Max source, Max dest) throws NullArgumentException { MathUtils.checkNotNull(source); MathUtils.checkNotNull(dest); dest.setData(source.getDataRef()); dest.n = source.n; dest.value = source.value; } } ././@LongLink100644 0 0 146 12126630647 10263 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/stat/descriptive/StatisticalSummary.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/stat/descriptive/StatisticalSummary.jav100644 1750 1750 4410 12126627717 32366 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.stat.descriptive; /** * Reporting interface for basic univariate statistics. * * @version $Id: StatisticalSummary.java 1416643 2012-12-03 19:37:14Z tn $ */ public interface StatisticalSummary { /** * Returns the * arithmetic mean of the available values * @return The mean or Double.NaN if no values have been added. */ double getMean(); /** * Returns the variance of the available values. * @return The variance, Double.NaN if no values have been added * or 0.0 for a single value set. */ double getVariance(); /** * Returns the standard deviation of the available values. * @return The standard deviation, Double.NaN if no values have been added * or 0.0 for a single value set. */ double getStandardDeviation(); /** * Returns the maximum of the available values * @return The max or Double.NaN if no values have been added. */ double getMax(); /** * Returns the minimum of the available values * @return The min or Double.NaN if no values have been added. */ double getMin(); /** * Returns the number of available values * @return The number of available values */ long getN(); /** * Returns the sum of the values that have been added to Univariate. * @return The sum or Double.NaN if no values have been added */ double getSum(); } ././@LongLink100644 0 0 162 12126630647 10261 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/stat/descriptive/StatisticalMultivariateSummary.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/stat/descriptive/StatisticalMultivariat100644 1750 1750 7276 12126627717 32470 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.stat.descriptive; import org.apache.commons.math3.linear.RealMatrix; /** * Reporting interface for basic multivariate statistics. * * @since 1.2 * @version $Id: StatisticalMultivariateSummary.java 1416643 2012-12-03 19:37:14Z tn $ */ public interface StatisticalMultivariateSummary { /** * Returns the dimension of the data * @return The dimension of the data */ int getDimension(); /** * Returns an array whose ith entry is the * mean of the ith entries of the arrays * that correspond to each multivariate sample * * @return the array of component means */ double[] getMean(); /** * Returns the covariance of the available values. * @return The covariance, null if no multivariate sample * have been added or a zeroed matrix for a single value set. */ RealMatrix getCovariance(); /** * Returns an array whose ith entry is the * standard deviation of the ith entries of the arrays * that correspond to each multivariate sample * * @return the array of component standard deviations */ double[] getStandardDeviation(); /** * Returns an array whose ith entry is the * maximum of the ith entries of the arrays * that correspond to each multivariate sample * * @return the array of component maxima */ double[] getMax(); /** * Returns an array whose ith entry is the * minimum of the ith entries of the arrays * that correspond to each multivariate sample * * @return the array of component minima */ double[] getMin(); /** * Returns the number of available values * @return The number of available values */ long getN(); /** * Returns an array whose ith entry is the * geometric mean of the ith entries of the arrays * that correspond to each multivariate sample * * @return the array of component geometric means */ double[] getGeometricMean(); /** * Returns an array whose ith entry is the * sum of the ith entries of the arrays * that correspond to each multivariate sample * * @return the array of component sums */ double[] getSum(); /** * Returns an array whose ith entry is the * sum of squares of the ith entries of the arrays * that correspond to each multivariate sample * * @return the array of component sums of squares */ double[] getSumSq(); /** * Returns an array whose ith entry is the * sum of logs of the ith entries of the arrays * that correspond to each multivariate sample * * @return the array of component log sums */ double[] getSumLog(); } ././@LongLink100644 0 0 161 12126630647 10260 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/stat/descriptive/SynchronizedSummaryStatistics.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/stat/descriptive/SynchronizedSummarySta100644 1750 1750 21342 12126627717 32475 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.stat.descriptive; import org.apache.commons.math3.exception.MathIllegalStateException; import org.apache.commons.math3.exception.NullArgumentException; import org.apache.commons.math3.util.MathUtils; /** * Implementation of * {@link org.apache.commons.math3.stat.descriptive.SummaryStatistics} that * is safe to use in a multithreaded environment. Multiple threads can safely * operate on a single instance without causing runtime exceptions due to race * conditions. In effect, this implementation makes modification and access * methods atomic operations for a single instance. That is to say, as one * thread is computing a statistic from the instance, no other thread can modify * the instance nor compute another statistic. * * @since 1.2 * @version $Id: SynchronizedSummaryStatistics.java 1416643 2012-12-03 19:37:14Z tn $ */ public class SynchronizedSummaryStatistics extends SummaryStatistics { /** Serialization UID */ private static final long serialVersionUID = 1909861009042253704L; /** * Construct a SynchronizedSummaryStatistics instance */ public SynchronizedSummaryStatistics() { super(); } /** * A copy constructor. Creates a deep-copy of the {@code original}. * * @param original the {@code SynchronizedSummaryStatistics} instance to copy * @throws NullArgumentException if original is null */ public SynchronizedSummaryStatistics(SynchronizedSummaryStatistics original) throws NullArgumentException { copy(original, this); } /** * {@inheritDoc} */ @Override public synchronized StatisticalSummary getSummary() { return super.getSummary(); } /** * {@inheritDoc} */ @Override public synchronized void addValue(double value) { super.addValue(value); } /** * {@inheritDoc} */ @Override public synchronized long getN() { return super.getN(); } /** * {@inheritDoc} */ @Override public synchronized double getSum() { return super.getSum(); } /** * {@inheritDoc} */ @Override public synchronized double getSumsq() { return super.getSumsq(); } /** * {@inheritDoc} */ @Override public synchronized double getMean() { return super.getMean(); } /** * {@inheritDoc} */ @Override public synchronized double getStandardDeviation() { return super.getStandardDeviation(); } /** * {@inheritDoc} */ @Override public synchronized double getVariance() { return super.getVariance(); } /** * {@inheritDoc} */ @Override public synchronized double getPopulationVariance() { return super.getPopulationVariance(); } /** * {@inheritDoc} */ @Override public synchronized double getMax() { return super.getMax(); } /** * {@inheritDoc} */ @Override public synchronized double getMin() { return super.getMin(); } /** * {@inheritDoc} */ @Override public synchronized double getGeometricMean() { return super.getGeometricMean(); } /** * {@inheritDoc} */ @Override public synchronized String toString() { return super.toString(); } /** * {@inheritDoc} */ @Override public synchronized void clear() { super.clear(); } /** * {@inheritDoc} */ @Override public synchronized boolean equals(Object object) { return super.equals(object); } /** * {@inheritDoc} */ @Override public synchronized int hashCode() { return super.hashCode(); } /** * {@inheritDoc} */ @Override public synchronized StorelessUnivariateStatistic getSumImpl() { return super.getSumImpl(); } /** * {@inheritDoc} */ @Override public synchronized void setSumImpl(StorelessUnivariateStatistic sumImpl) throws MathIllegalStateException { super.setSumImpl(sumImpl); } /** * {@inheritDoc} */ @Override public synchronized StorelessUnivariateStatistic getSumsqImpl() { return super.getSumsqImpl(); } /** * {@inheritDoc} */ @Override public synchronized void setSumsqImpl(StorelessUnivariateStatistic sumsqImpl) throws MathIllegalStateException { super.setSumsqImpl(sumsqImpl); } /** * {@inheritDoc} */ @Override public synchronized StorelessUnivariateStatistic getMinImpl() { return super.getMinImpl(); } /** * {@inheritDoc} */ @Override public synchronized void setMinImpl(StorelessUnivariateStatistic minImpl) throws MathIllegalStateException { super.setMinImpl(minImpl); } /** * {@inheritDoc} */ @Override public synchronized StorelessUnivariateStatistic getMaxImpl() { return super.getMaxImpl(); } /** * {@inheritDoc} */ @Override public synchronized void setMaxImpl(StorelessUnivariateStatistic maxImpl) throws MathIllegalStateException { super.setMaxImpl(maxImpl); } /** * {@inheritDoc} */ @Override public synchronized StorelessUnivariateStatistic getSumLogImpl() { return super.getSumLogImpl(); } /** * {@inheritDoc} */ @Override public synchronized void setSumLogImpl(StorelessUnivariateStatistic sumLogImpl) throws MathIllegalStateException { super.setSumLogImpl(sumLogImpl); } /** * {@inheritDoc} */ @Override public synchronized StorelessUnivariateStatistic getGeoMeanImpl() { return super.getGeoMeanImpl(); } /** * {@inheritDoc} */ @Override public synchronized void setGeoMeanImpl(StorelessUnivariateStatistic geoMeanImpl) throws MathIllegalStateException { super.setGeoMeanImpl(geoMeanImpl); } /** * {@inheritDoc} */ @Override public synchronized StorelessUnivariateStatistic getMeanImpl() { return super.getMeanImpl(); } /** * {@inheritDoc} */ @Override public synchronized void setMeanImpl(StorelessUnivariateStatistic meanImpl) throws MathIllegalStateException { super.setMeanImpl(meanImpl); } /** * {@inheritDoc} */ @Override public synchronized StorelessUnivariateStatistic getVarianceImpl() { return super.getVarianceImpl(); } /** * {@inheritDoc} */ @Override public synchronized void setVarianceImpl(StorelessUnivariateStatistic varianceImpl) throws MathIllegalStateException { super.setVarianceImpl(varianceImpl); } /** * Returns a copy of this SynchronizedSummaryStatistics instance with the * same internal state. * * @return a copy of this */ @Override public synchronized SynchronizedSummaryStatistics copy() { SynchronizedSummaryStatistics result = new SynchronizedSummaryStatistics(); // No try-catch or advertised exception because arguments are guaranteed non-null copy(this, result); return result; } /** * Copies source to dest. *

                              Neither source nor dest can be null.

                              *

                              Acquires synchronization lock on source, then dest before copying.

                              * * @param source SynchronizedSummaryStatistics to copy * @param dest SynchronizedSummaryStatistics to copy to * @throws NullArgumentException if either source or dest is null */ public static void copy(SynchronizedSummaryStatistics source, SynchronizedSummaryStatistics dest) throws NullArgumentException { MathUtils.checkNotNull(source); MathUtils.checkNotNull(dest); synchronized (source) { synchronized (dest) { SummaryStatistics.copy(source, dest); } } } } ././@LongLink100644 0 0 146 12126630647 10263 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/stat/descriptive/WeightedEvaluation.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/stat/descriptive/WeightedEvaluation.jav100644 1750 1750 4733 12126627717 32324 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.stat.descriptive; import org.apache.commons.math3.exception.MathIllegalArgumentException; /** * Weighted evaluation for statistics. * * @since 2.1 * @version $Id: WeightedEvaluation.java 1382332 2012-09-08 17:27:47Z psteitz $ */ public interface WeightedEvaluation { /** * Returns the result of evaluating the statistic over the input array, * using the supplied weights. * * @param values input array * @param weights array of weights * @return the value of the weighted statistic applied to the input array * @throws MathIllegalArgumentException if either array is null, lengths * do not match, weights contain NaN, negative or infinite values, or * weights does not include at least on positive value */ double evaluate(double[] values, double[] weights) throws MathIllegalArgumentException; /** * Returns the result of evaluating the statistic over the specified entries * in the input array, using corresponding entries in the supplied weights array. * * @param values the input array * @param weights array of weights * @param begin the index of the first element to include * @param length the number of elements to include * @return the value of the weighted statistic applied to the included array entries * @throws MathIllegalArgumentException if either array is null, lengths * do not match, indices are invalid, weights contain NaN, negative or * infinite values, or weights does not include at least on positive value */ double evaluate(double[] values, double[] weights, int begin, int length) throws MathIllegalArgumentException; } ././@LongLink100644 0 0 150 12126630647 10256 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/stat/descriptive/summary/package-info.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/stat/descriptive/summary/package-info.j100644 1750 1750 1603 12126627717 32217 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ /** * Other summary statistics. */ package org.apache.commons.math3.stat.descriptive.summary; ././@LongLink100644 0 0 150 12126630647 10256 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/stat/descriptive/summary/SumOfSquares.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/stat/descriptive/summary/SumOfSquares.j100644 1750 1750 11433 12126627717 32312 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.stat.descriptive.summary; import java.io.Serializable; import org.apache.commons.math3.exception.MathIllegalArgumentException; import org.apache.commons.math3.exception.NullArgumentException; import org.apache.commons.math3.stat.descriptive.AbstractStorelessUnivariateStatistic; import org.apache.commons.math3.util.MathUtils; /** * Returns the sum of the squares of the available values. *

                              * If there are no values in the dataset, then 0 is returned. * If any of the values are * NaN, then NaN is returned.

                              *

                              * Note that this implementation is not synchronized. If * multiple threads access an instance of this class concurrently, and at least * one of the threads invokes the increment() or * clear() method, it must be synchronized externally.

                              * * @version $Id: SumOfSquares.java 1416643 2012-12-03 19:37:14Z tn $ */ public class SumOfSquares extends AbstractStorelessUnivariateStatistic implements Serializable { /** Serializable version identifier */ private static final long serialVersionUID = 1460986908574398008L; /** */ private long n; /** * The currently running sumSq */ private double value; /** * Create a SumOfSquares instance */ public SumOfSquares() { n = 0; value = 0; } /** * Copy constructor, creates a new {@code SumOfSquares} identical * to the {@code original} * * @param original the {@code SumOfSquares} instance to copy * @throws NullArgumentException if original is null */ public SumOfSquares(SumOfSquares original) throws NullArgumentException { copy(original, this); } /** * {@inheritDoc} */ @Override public void increment(final double d) { value += d * d; n++; } /** * {@inheritDoc} */ @Override public double getResult() { return value; } /** * {@inheritDoc} */ public long getN() { return n; } /** * {@inheritDoc} */ @Override public void clear() { value = 0; n = 0; } /** * Returns the sum of the squares of the entries in the specified portion of * the input array, or Double.NaN if the designated subarray * is empty. *

                              * Throws MathIllegalArgumentException if the array is null.

                              * * @param values the input array * @param begin index of the first array element to include * @param length the number of elements to include * @return the sum of the squares of the values or 0 if length = 0 * @throws MathIllegalArgumentException if the array is null or the array index * parameters are not valid */ @Override public double evaluate(final double[] values,final int begin, final int length) throws MathIllegalArgumentException { double sumSq = Double.NaN; if (test(values, begin, length, true)) { sumSq = 0.0; for (int i = begin; i < begin + length; i++) { sumSq += values[i] * values[i]; } } return sumSq; } /** * {@inheritDoc} */ @Override public SumOfSquares copy() { SumOfSquares result = new SumOfSquares(); // no try-catch or advertised exception here because args are valid copy(this, result); return result; } /** * Copies source to dest. *

                              Neither source nor dest can be null.

                              * * @param source SumOfSquares to copy * @param dest SumOfSquares to copy to * @throws NullArgumentException if either source or dest is null */ public static void copy(SumOfSquares source, SumOfSquares dest) throws NullArgumentException { MathUtils.checkNotNull(source); MathUtils.checkNotNull(dest); dest.setData(source.getDataRef()); dest.n = source.n; dest.value = source.value; } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/stat/descriptive/summary/Product.java100644 1750 1750 17505 12126627717 32033 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.stat.descriptive.summary; import java.io.Serializable; import org.apache.commons.math3.exception.MathIllegalArgumentException; import org.apache.commons.math3.exception.NullArgumentException; import org.apache.commons.math3.stat.descriptive.AbstractStorelessUnivariateStatistic; import org.apache.commons.math3.stat.descriptive.WeightedEvaluation; import org.apache.commons.math3.util.FastMath; import org.apache.commons.math3.util.MathUtils; /** * Returns the product of the available values. *

                              * If there are no values in the dataset, then 1 is returned. * If any of the values are * NaN, then NaN is returned.

                              *

                              * Note that this implementation is not synchronized. If * multiple threads access an instance of this class concurrently, and at least * one of the threads invokes the increment() or * clear() method, it must be synchronized externally.

                              * * @version $Id: Product.java 1416643 2012-12-03 19:37:14Z tn $ */ public class Product extends AbstractStorelessUnivariateStatistic implements Serializable, WeightedEvaluation { /** Serializable version identifier */ private static final long serialVersionUID = 2824226005990582538L; /**The number of values that have been added */ private long n; /** * The current Running Product. */ private double value; /** * Create a Product instance */ public Product() { n = 0; value = 1; } /** * Copy constructor, creates a new {@code Product} identical * to the {@code original} * * @param original the {@code Product} instance to copy * @throws NullArgumentException if original is null */ public Product(Product original) throws NullArgumentException { copy(original, this); } /** * {@inheritDoc} */ @Override public void increment(final double d) { value *= d; n++; } /** * {@inheritDoc} */ @Override public double getResult() { return value; } /** * {@inheritDoc} */ public long getN() { return n; } /** * {@inheritDoc} */ @Override public void clear() { value = 1; n = 0; } /** * Returns the product of the entries in the specified portion of * the input array, or Double.NaN if the designated subarray * is empty. *

                              * Throws MathIllegalArgumentException if the array is null.

                              * * @param values the input array * @param begin index of the first array element to include * @param length the number of elements to include * @return the product of the values or 1 if length = 0 * @throws MathIllegalArgumentException if the array is null or the array index * parameters are not valid */ @Override public double evaluate(final double[] values, final int begin, final int length) throws MathIllegalArgumentException { double product = Double.NaN; if (test(values, begin, length, true)) { product = 1.0; for (int i = begin; i < begin + length; i++) { product *= values[i]; } } return product; } /** *

                              Returns the weighted product of the entries in the specified portion of * the input array, or Double.NaN if the designated subarray * is empty.

                              * *

                              Throws MathIllegalArgumentException if any of the following are true: *

                              • the values array is null
                              • *
                              • the weights array is null
                              • *
                              • the weights array does not have the same length as the values array
                              • *
                              • the weights array contains one or more infinite values
                              • *
                              • the weights array contains one or more NaN values
                              • *
                              • the weights array contains negative values
                              • *
                              • the start and length arguments do not determine a valid array
                              • *

                              * *

                              Uses the formula,

                                   *    weighted product = ∏values[i]weights[i]
                                   * 
                              * that is, the weights are applied as exponents when computing the weighted product.

                              * * @param values the input array * @param weights the weights array * @param begin index of the first array element to include * @param length the number of elements to include * @return the product of the values or 1 if length = 0 * @throws MathIllegalArgumentException if the parameters are not valid * @since 2.1 */ public double evaluate(final double[] values, final double[] weights, final int begin, final int length) throws MathIllegalArgumentException { double product = Double.NaN; if (test(values, weights, begin, length, true)) { product = 1.0; for (int i = begin; i < begin + length; i++) { product *= FastMath.pow(values[i], weights[i]); } } return product; } /** *

                              Returns the weighted product of the entries in the input array.

                              * *

                              Throws MathIllegalArgumentException if any of the following are true: *

                              • the values array is null
                              • *
                              • the weights array is null
                              • *
                              • the weights array does not have the same length as the values array
                              • *
                              • the weights array contains one or more infinite values
                              • *
                              • the weights array contains one or more NaN values
                              • *
                              • the weights array contains negative values
                              • *

                              * *

                              Uses the formula,

                                   *    weighted product = ∏values[i]weights[i]
                                   * 
                              * that is, the weights are applied as exponents when computing the weighted product.

                              * * @param values the input array * @param weights the weights array * @return the product of the values or Double.NaN if length = 0 * @throws MathIllegalArgumentException if the parameters are not valid * @since 2.1 */ public double evaluate(final double[] values, final double[] weights) throws MathIllegalArgumentException { return evaluate(values, weights, 0, values.length); } /** * {@inheritDoc} */ @Override public Product copy() { Product result = new Product(); // No try-catch or advertised exception because args are valid copy(this, result); return result; } /** * Copies source to dest. *

                              Neither source nor dest can be null.

                              * * @param source Product to copy * @param dest Product to copy to * @throws NullArgumentException if either source or dest is null */ public static void copy(Product source, Product dest) throws NullArgumentException { MathUtils.checkNotNull(source); MathUtils.checkNotNull(dest); dest.setData(source.getDataRef()); dest.n = source.n; dest.value = source.value; } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/stat/descriptive/summary/Sum.java100644 1750 1750 16375 12126627717 31163 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.stat.descriptive.summary; import java.io.Serializable; import org.apache.commons.math3.exception.MathIllegalArgumentException; import org.apache.commons.math3.exception.NullArgumentException; import org.apache.commons.math3.stat.descriptive.AbstractStorelessUnivariateStatistic; import org.apache.commons.math3.util.MathUtils; /** * Returns the sum of the available values. *

                              * If there are no values in the dataset, then 0 is returned. * If any of the values are * NaN, then NaN is returned.

                              *

                              * Note that this implementation is not synchronized. If * multiple threads access an instance of this class concurrently, and at least * one of the threads invokes the increment() or * clear() method, it must be synchronized externally.

                              * * @version $Id: Sum.java 1416643 2012-12-03 19:37:14Z tn $ */ public class Sum extends AbstractStorelessUnivariateStatistic implements Serializable { /** Serializable version identifier */ private static final long serialVersionUID = -8231831954703408316L; /** */ private long n; /** * The currently running sum. */ private double value; /** * Create a Sum instance */ public Sum() { n = 0; value = 0; } /** * Copy constructor, creates a new {@code Sum} identical * to the {@code original} * * @param original the {@code Sum} instance to copy * @throws NullArgumentException if original is null */ public Sum(Sum original) throws NullArgumentException { copy(original, this); } /** * {@inheritDoc} */ @Override public void increment(final double d) { value += d; n++; } /** * {@inheritDoc} */ @Override public double getResult() { return value; } /** * {@inheritDoc} */ public long getN() { return n; } /** * {@inheritDoc} */ @Override public void clear() { value = 0; n = 0; } /** * The sum of the entries in the specified portion of * the input array, or 0 if the designated subarray * is empty. *

                              * Throws MathIllegalArgumentException if the array is null.

                              * * @param values the input array * @param begin index of the first array element to include * @param length the number of elements to include * @return the sum of the values or 0 if length = 0 * @throws MathIllegalArgumentException if the array is null or the array index * parameters are not valid */ @Override public double evaluate(final double[] values, final int begin, final int length) throws MathIllegalArgumentException { double sum = Double.NaN; if (test(values, begin, length, true)) { sum = 0.0; for (int i = begin; i < begin + length; i++) { sum += values[i]; } } return sum; } /** * The weighted sum of the entries in the specified portion of * the input array, or 0 if the designated subarray * is empty. *

                              * Throws MathIllegalArgumentException if any of the following are true: *

                              • the values array is null
                              • *
                              • the weights array is null
                              • *
                              • the weights array does not have the same length as the values array
                              • *
                              • the weights array contains one or more infinite values
                              • *
                              • the weights array contains one or more NaN values
                              • *
                              • the weights array contains negative values
                              • *
                              • the start and length arguments do not determine a valid array
                              • *

                              *

                              * Uses the formula,

                                   *    weighted sum = Σ(values[i] * weights[i])
                                   * 

                              * * @param values the input array * @param weights the weights array * @param begin index of the first array element to include * @param length the number of elements to include * @return the sum of the values or 0 if length = 0 * @throws MathIllegalArgumentException if the parameters are not valid * @since 2.1 */ public double evaluate(final double[] values, final double[] weights, final int begin, final int length) throws MathIllegalArgumentException { double sum = Double.NaN; if (test(values, weights, begin, length, true)) { sum = 0.0; for (int i = begin; i < begin + length; i++) { sum += values[i] * weights[i]; } } return sum; } /** * The weighted sum of the entries in the the input array. *

                              * Throws MathIllegalArgumentException if any of the following are true: *

                              • the values array is null
                              • *
                              • the weights array is null
                              • *
                              • the weights array does not have the same length as the values array
                              • *
                              • the weights array contains one or more infinite values
                              • *
                              • the weights array contains one or more NaN values
                              • *
                              • the weights array contains negative values
                              • *

                              *

                              * Uses the formula,

                                   *    weighted sum = Σ(values[i] * weights[i])
                                   * 

                              * * @param values the input array * @param weights the weights array * @return the sum of the values or Double.NaN if length = 0 * @throws MathIllegalArgumentException if the parameters are not valid * @since 2.1 */ public double evaluate(final double[] values, final double[] weights) throws MathIllegalArgumentException { return evaluate(values, weights, 0, values.length); } /** * {@inheritDoc} */ @Override public Sum copy() { Sum result = new Sum(); // No try-catch or advertised exception because args are valid copy(this, result); return result; } /** * Copies source to dest. *

                              Neither source nor dest can be null.

                              * * @param source Sum to copy * @param dest Sum to copy to * @throws NullArgumentException if either source or dest is null */ public static void copy(Sum source, Sum dest) throws NullArgumentException { MathUtils.checkNotNull(source); MathUtils.checkNotNull(dest); dest.setData(source.getDataRef()); dest.n = source.n; dest.value = source.value; } } ././@LongLink100644 0 0 145 12126630647 10262 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/stat/descriptive/summary/SumOfLogs.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/stat/descriptive/summary/SumOfLogs.java100644 1750 1750 12441 12126627717 32263 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.stat.descriptive.summary; import java.io.Serializable; import org.apache.commons.math3.exception.MathIllegalArgumentException; import org.apache.commons.math3.exception.NullArgumentException; import org.apache.commons.math3.stat.descriptive.AbstractStorelessUnivariateStatistic; import org.apache.commons.math3.util.FastMath; import org.apache.commons.math3.util.MathUtils; /** * Returns the sum of the natural logs for this collection of values. *

                              * Uses {@link org.apache.commons.math3.util.FastMath#log(double)} to compute the logs. * Therefore, *

                                *
                              • If any of values are < 0, the result is NaN.
                              • *
                              • If all values are non-negative and less than * Double.POSITIVE_INFINITY, but at least one value is 0, the * result is Double.NEGATIVE_INFINITY.
                              • *
                              • If both Double.POSITIVE_INFINITY and * Double.NEGATIVE_INFINITY are among the values, the result is * NaN.
                              • *

                              *

                              * Note that this implementation is not synchronized. If * multiple threads access an instance of this class concurrently, and at least * one of the threads invokes the increment() or * clear() method, it must be synchronized externally.

                              * * @version $Id: SumOfLogs.java 1416643 2012-12-03 19:37:14Z tn $ */ public class SumOfLogs extends AbstractStorelessUnivariateStatistic implements Serializable { /** Serializable version identifier */ private static final long serialVersionUID = -370076995648386763L; /**Number of values that have been added */ private int n; /** * The currently running value */ private double value; /** * Create a SumOfLogs instance */ public SumOfLogs() { value = 0d; n = 0; } /** * Copy constructor, creates a new {@code SumOfLogs} identical * to the {@code original} * * @param original the {@code SumOfLogs} instance to copy * @throws NullArgumentException if original is null */ public SumOfLogs(SumOfLogs original) throws NullArgumentException { copy(original, this); } /** * {@inheritDoc} */ @Override public void increment(final double d) { value += FastMath.log(d); n++; } /** * {@inheritDoc} */ @Override public double getResult() { return value; } /** * {@inheritDoc} */ public long getN() { return n; } /** * {@inheritDoc} */ @Override public void clear() { value = 0d; n = 0; } /** * Returns the sum of the natural logs of the entries in the specified portion of * the input array, or Double.NaN if the designated subarray * is empty. *

                              * Throws MathIllegalArgumentException if the array is null.

                              *

                              * See {@link SumOfLogs}.

                              * * @param values the input array * @param begin index of the first array element to include * @param length the number of elements to include * @return the sum of the natural logs of the values or 0 if * length = 0 * @throws MathIllegalArgumentException if the array is null or the array index * parameters are not valid */ @Override public double evaluate(final double[] values, final int begin, final int length) throws MathIllegalArgumentException { double sumLog = Double.NaN; if (test(values, begin, length, true)) { sumLog = 0.0; for (int i = begin; i < begin + length; i++) { sumLog += FastMath.log(values[i]); } } return sumLog; } /** * {@inheritDoc} */ @Override public SumOfLogs copy() { SumOfLogs result = new SumOfLogs(); // No try-catch or advertised exception here because args are valid copy(this, result); return result; } /** * Copies source to dest. *

                              Neither source nor dest can be null.

                              * * @param source SumOfLogs to copy * @param dest SumOfLogs to copy to * @throws NullArgumentException if either source or dest is null */ public static void copy(SumOfLogs source, SumOfLogs dest) throws NullArgumentException { MathUtils.checkNotNull(source); MathUtils.checkNotNull(dest); dest.setData(source.getDataRef()); dest.n = source.n; dest.value = source.value; } } ././@LongLink100644 0 0 146 12126630647 10263 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/stat/descriptive/moment/FirstMoment.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/stat/descriptive/moment/FirstMoment.jav100644 1750 1750 11476 12126627717 32324 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.stat.descriptive.moment; import java.io.Serializable; import org.apache.commons.math3.exception.NullArgumentException; import org.apache.commons.math3.stat.descriptive.AbstractStorelessUnivariateStatistic; import org.apache.commons.math3.util.MathUtils; /** * Computes the first moment (arithmetic mean). Uses the definitional formula: *

                              * mean = sum(x_i) / n

                              *

                              * where n is the number of observations.

                              *

                              * To limit numeric errors, the value of the statistic is computed using the * following recursive updating algorithm:

                              *

                              *

                                *
                              1. Initialize m = the first value
                              2. *
                              3. For each additional value, update using
                                * m = m + (new value - m) / (number of observations)
                              4. *

                              *

                              * Returns Double.NaN if the dataset is empty.

                              *

                              * Note that this implementation is not synchronized. If * multiple threads access an instance of this class concurrently, and at least * one of the threads invokes the increment() or * clear() method, it must be synchronized externally.

                              * * @version $Id: FirstMoment.java 1416643 2012-12-03 19:37:14Z tn $ */ class FirstMoment extends AbstractStorelessUnivariateStatistic implements Serializable { /** Serializable version identifier */ private static final long serialVersionUID = 6112755307178490473L; /** Count of values that have been added */ protected long n; /** First moment of values that have been added */ protected double m1; /** * Deviation of most recently added value from previous first moment. * Retained to prevent repeated computation in higher order moments. */ protected double dev; /** * Deviation of most recently added value from previous first moment, * normalized by previous sample size. Retained to prevent repeated * computation in higher order moments */ protected double nDev; /** * Create a FirstMoment instance */ public FirstMoment() { n = 0; m1 = Double.NaN; dev = Double.NaN; nDev = Double.NaN; } /** * Copy constructor, creates a new {@code FirstMoment} identical * to the {@code original} * * @param original the {@code FirstMoment} instance to copy * @throws NullArgumentException if original is null */ public FirstMoment(FirstMoment original) throws NullArgumentException { super(); copy(original, this); } /** * {@inheritDoc} */ @Override public void increment(final double d) { if (n == 0) { m1 = 0.0; } n++; double n0 = n; dev = d - m1; nDev = dev / n0; m1 += nDev; } /** * {@inheritDoc} */ @Override public void clear() { m1 = Double.NaN; n = 0; dev = Double.NaN; nDev = Double.NaN; } /** * {@inheritDoc} */ @Override public double getResult() { return m1; } /** * {@inheritDoc} */ public long getN() { return n; } /** * {@inheritDoc} */ @Override public FirstMoment copy() { FirstMoment result = new FirstMoment(); // No try-catch or advertised exception because args are guaranteed non-null copy(this, result); return result; } /** * Copies source to dest. *

                              Neither source nor dest can be null.

                              * * @param source FirstMoment to copy * @param dest FirstMoment to copy to * @throws NullArgumentException if either source or dest is null */ public static void copy(FirstMoment source, FirstMoment dest) throws NullArgumentException { MathUtils.checkNotNull(source); MathUtils.checkNotNull(dest); dest.setData(source.getDataRef()); dest.n = source.n; dest.m1 = source.m1; dest.dev = source.dev; dest.nDev = source.nDev; } } ././@LongLink100644 0 0 154 12126630647 10262 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/stat/descriptive/moment/StandardDeviation.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/stat/descriptive/moment/StandardDeviati100644 1750 1750 24405 12126627717 32340 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.stat.descriptive.moment; import java.io.Serializable; import org.apache.commons.math3.exception.MathIllegalArgumentException; import org.apache.commons.math3.exception.NullArgumentException; import org.apache.commons.math3.stat.descriptive.AbstractStorelessUnivariateStatistic; import org.apache.commons.math3.util.FastMath; import org.apache.commons.math3.util.MathUtils; /** * Computes the sample standard deviation. The standard deviation * is the positive square root of the variance. This implementation wraps a * {@link Variance} instance. The isBiasCorrected property of the * wrapped Variance instance is exposed, so that this class can be used to * compute both the "sample standard deviation" (the square root of the * bias-corrected "sample variance") or the "population standard deviation" * (the square root of the non-bias-corrected "population variance"). See * {@link Variance} for more information. *

                              * Note that this implementation is not synchronized. If * multiple threads access an instance of this class concurrently, and at least * one of the threads invokes the increment() or * clear() method, it must be synchronized externally.

                              * * @version $Id: StandardDeviation.java 1416643 2012-12-03 19:37:14Z tn $ */ public class StandardDeviation extends AbstractStorelessUnivariateStatistic implements Serializable { /** Serializable version identifier */ private static final long serialVersionUID = 5728716329662425188L; /** Wrapped Variance instance */ private Variance variance = null; /** * Constructs a StandardDeviation. Sets the underlying {@link Variance} * instance's isBiasCorrected property to true. */ public StandardDeviation() { variance = new Variance(); } /** * Constructs a StandardDeviation from an external second moment. * * @param m2 the external moment */ public StandardDeviation(final SecondMoment m2) { variance = new Variance(m2); } /** * Copy constructor, creates a new {@code StandardDeviation} identical * to the {@code original} * * @param original the {@code StandardDeviation} instance to copy * @throws NullArgumentException if original is null */ public StandardDeviation(StandardDeviation original) throws NullArgumentException { copy(original, this); } /** * Contructs a StandardDeviation with the specified value for the * isBiasCorrected property. If this property is set to * true, the {@link Variance} used in computing results will * use the bias-corrected, or "sample" formula. See {@link Variance} for * details. * * @param isBiasCorrected whether or not the variance computation will use * the bias-corrected formula */ public StandardDeviation(boolean isBiasCorrected) { variance = new Variance(isBiasCorrected); } /** * Contructs a StandardDeviation with the specified value for the * isBiasCorrected property and the supplied external moment. * If isBiasCorrected is set to true, the * {@link Variance} used in computing results will use the bias-corrected, * or "sample" formula. See {@link Variance} for details. * * @param isBiasCorrected whether or not the variance computation will use * the bias-corrected formula * @param m2 the external moment */ public StandardDeviation(boolean isBiasCorrected, SecondMoment m2) { variance = new Variance(isBiasCorrected, m2); } /** * {@inheritDoc} */ @Override public void increment(final double d) { variance.increment(d); } /** * {@inheritDoc} */ public long getN() { return variance.getN(); } /** * {@inheritDoc} */ @Override public double getResult() { return FastMath.sqrt(variance.getResult()); } /** * {@inheritDoc} */ @Override public void clear() { variance.clear(); } /** * Returns the Standard Deviation of the entries in the input array, or * Double.NaN if the array is empty. *

                              * Returns 0 for a single-value (i.e. length = 1) sample.

                              *

                              * Throws MathIllegalArgumentException if the array is null.

                              *

                              * Does not change the internal state of the statistic.

                              * * @param values the input array * @return the standard deviation of the values or Double.NaN if length = 0 * @throws MathIllegalArgumentException if the array is null */ @Override public double evaluate(final double[] values) throws MathIllegalArgumentException { return FastMath.sqrt(variance.evaluate(values)); } /** * Returns the Standard Deviation of the entries in the specified portion of * the input array, or Double.NaN if the designated subarray * is empty. *

                              * Returns 0 for a single-value (i.e. length = 1) sample.

                              *

                              * Throws MathIllegalArgumentException if the array is null.

                              *

                              * Does not change the internal state of the statistic.

                              * * @param values the input array * @param begin index of the first array element to include * @param length the number of elements to include * @return the standard deviation of the values or Double.NaN if length = 0 * @throws MathIllegalArgumentException if the array is null or the array index * parameters are not valid */ @Override public double evaluate(final double[] values, final int begin, final int length) throws MathIllegalArgumentException { return FastMath.sqrt(variance.evaluate(values, begin, length)); } /** * Returns the Standard Deviation of the entries in the specified portion of * the input array, using the precomputed mean value. Returns * Double.NaN if the designated subarray is empty. *

                              * Returns 0 for a single-value (i.e. length = 1) sample.

                              *

                              * The formula used assumes that the supplied mean value is the arithmetic * mean of the sample data, not a known population parameter. This method * is supplied only to save computation when the mean has already been * computed.

                              *

                              * Throws IllegalArgumentException if the array is null.

                              *

                              * Does not change the internal state of the statistic.

                              * * @param values the input array * @param mean the precomputed mean value * @param begin index of the first array element to include * @param length the number of elements to include * @return the standard deviation of the values or Double.NaN if length = 0 * @throws MathIllegalArgumentException if the array is null or the array index * parameters are not valid */ public double evaluate(final double[] values, final double mean, final int begin, final int length) throws MathIllegalArgumentException { return FastMath.sqrt(variance.evaluate(values, mean, begin, length)); } /** * Returns the Standard Deviation of the entries in the input array, using * the precomputed mean value. Returns * Double.NaN if the designated subarray is empty. *

                              * Returns 0 for a single-value (i.e. length = 1) sample.

                              *

                              * The formula used assumes that the supplied mean value is the arithmetic * mean of the sample data, not a known population parameter. This method * is supplied only to save computation when the mean has already been * computed.

                              *

                              * Throws MathIllegalArgumentException if the array is null.

                              *

                              * Does not change the internal state of the statistic.

                              * * @param values the input array * @param mean the precomputed mean value * @return the standard deviation of the values or Double.NaN if length = 0 * @throws MathIllegalArgumentException if the array is null */ public double evaluate(final double[] values, final double mean) throws MathIllegalArgumentException { return FastMath.sqrt(variance.evaluate(values, mean)); } /** * @return Returns the isBiasCorrected. */ public boolean isBiasCorrected() { return variance.isBiasCorrected(); } /** * @param isBiasCorrected The isBiasCorrected to set. */ public void setBiasCorrected(boolean isBiasCorrected) { variance.setBiasCorrected(isBiasCorrected); } /** * {@inheritDoc} */ @Override public StandardDeviation copy() { StandardDeviation result = new StandardDeviation(); // No try-catch or advertised exception because args are guaranteed non-null copy(this, result); return result; } /** * Copies source to dest. *

                              Neither source nor dest can be null.

                              * * @param source StandardDeviation to copy * @param dest StandardDeviation to copy to * @throws NullArgumentException if either source or dest is null */ public static void copy(StandardDeviation source, StandardDeviation dest) throws NullArgumentException { MathUtils.checkNotNull(source); MathUtils.checkNotNull(dest); dest.setData(source.getDataRef()); dest.variance = source.variance.copy(); } } ././@LongLink100644 0 0 147 12126630647 10264 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/stat/descriptive/moment/package-info.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/stat/descriptive/moment/package-info.ja100644 1750 1750 1615 12126627717 32165 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ /** * Summary statistics based on moments. */ package org.apache.commons.math3.stat.descriptive.moment; ././@LongLink100644 0 0 156 12126630647 10264 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/stat/descriptive/moment/VectorialCovariance.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/stat/descriptive/moment/VectorialCovari100644 1750 1750 11507 12126627717 32365 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.stat.descriptive.moment; import java.io.Serializable; import java.util.Arrays; import org.apache.commons.math3.exception.DimensionMismatchException; import org.apache.commons.math3.linear.MatrixUtils; import org.apache.commons.math3.linear.RealMatrix; /** * Returns the covariance matrix of the available vectors. * @since 1.2 * @version $Id: VectorialCovariance.java 1416643 2012-12-03 19:37:14Z tn $ */ public class VectorialCovariance implements Serializable { /** Serializable version identifier */ private static final long serialVersionUID = 4118372414238930270L; /** Sums for each component. */ private final double[] sums; /** Sums of products for each component. */ private final double[] productsSums; /** Indicator for bias correction. */ private final boolean isBiasCorrected; /** Number of vectors in the sample. */ private long n; /** Constructs a VectorialCovariance. * @param dimension vectors dimension * @param isBiasCorrected if true, computed the unbiased sample covariance, * otherwise computes the biased population covariance */ public VectorialCovariance(int dimension, boolean isBiasCorrected) { sums = new double[dimension]; productsSums = new double[dimension * (dimension + 1) / 2]; n = 0; this.isBiasCorrected = isBiasCorrected; } /** * Add a new vector to the sample. * @param v vector to add * @throws DimensionMismatchException if the vector does not have the right dimension */ public void increment(double[] v) throws DimensionMismatchException { if (v.length != sums.length) { throw new DimensionMismatchException(v.length, sums.length); } int k = 0; for (int i = 0; i < v.length; ++i) { sums[i] += v[i]; for (int j = 0; j <= i; ++j) { productsSums[k++] += v[i] * v[j]; } } n++; } /** * Get the covariance matrix. * @return covariance matrix */ public RealMatrix getResult() { int dimension = sums.length; RealMatrix result = MatrixUtils.createRealMatrix(dimension, dimension); if (n > 1) { double c = 1.0 / (n * (isBiasCorrected ? (n - 1) : n)); int k = 0; for (int i = 0; i < dimension; ++i) { for (int j = 0; j <= i; ++j) { double e = c * (n * productsSums[k++] - sums[i] * sums[j]); result.setEntry(i, j, e); result.setEntry(j, i, e); } } } return result; } /** * Get the number of vectors in the sample. * @return number of vectors in the sample */ public long getN() { return n; } /** * Clears the internal state of the Statistic */ public void clear() { n = 0; Arrays.fill(sums, 0.0); Arrays.fill(productsSums, 0.0); } /** {@inheritDoc} */ @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + (isBiasCorrected ? 1231 : 1237); result = prime * result + (int) (n ^ (n >>> 32)); result = prime * result + Arrays.hashCode(productsSums); result = prime * result + Arrays.hashCode(sums); return result; } /** {@inheritDoc} */ @Override public boolean equals(Object obj) { if (this == obj) { return true; } if (!(obj instanceof VectorialCovariance)) { return false; } VectorialCovariance other = (VectorialCovariance) obj; if (isBiasCorrected != other.isBiasCorrected) { return false; } if (n != other.n) { return false; } if (!Arrays.equals(productsSums, other.productsSums)) { return false; } if (!Arrays.equals(sums, other.sums)) { return false; } return true; } } ././@LongLink100644 0 0 147 12126630647 10264 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/stat/descriptive/moment/SecondMoment.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/stat/descriptive/moment/SecondMoment.ja100644 1750 1750 7667 12126627717 32251 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.stat.descriptive.moment; import java.io.Serializable; import org.apache.commons.math3.exception.NullArgumentException; import org.apache.commons.math3.util.MathUtils; /** * Computes a statistic related to the Second Central Moment. Specifically, * what is computed is the sum of squared deviations from the sample mean. *

                              * The following recursive updating formula is used:

                              *

                              * Let

                                *
                              • dev = (current obs - previous mean)
                              • *
                              • n = number of observations (including current obs)
                              • *
                              * Then

                              *

                              * new value = old value + dev^2 * (n -1) / n.

                              *

                              * Returns Double.NaN if no data values have been added and * returns 0 if there is just one value in the data set.

                              *

                              * Note that this implementation is not synchronized. If * multiple threads access an instance of this class concurrently, and at least * one of the threads invokes the increment() or * clear() method, it must be synchronized externally.

                              * * @version $Id: SecondMoment.java 1416643 2012-12-03 19:37:14Z tn $ */ public class SecondMoment extends FirstMoment implements Serializable { /** Serializable version identifier */ private static final long serialVersionUID = 3942403127395076445L; /** second moment of values that have been added */ protected double m2; /** * Create a SecondMoment instance */ public SecondMoment() { super(); m2 = Double.NaN; } /** * Copy constructor, creates a new {@code SecondMoment} identical * to the {@code original} * * @param original the {@code SecondMoment} instance to copy * @throws NullArgumentException if original is null */ public SecondMoment(SecondMoment original) throws NullArgumentException { super(original); this.m2 = original.m2; } /** * {@inheritDoc} */ @Override public void increment(final double d) { if (n < 1) { m1 = m2 = 0.0; } super.increment(d); m2 += ((double) n - 1) * dev * nDev; } /** * {@inheritDoc} */ @Override public void clear() { super.clear(); m2 = Double.NaN; } /** * {@inheritDoc} */ @Override public double getResult() { return m2; } /** * {@inheritDoc} */ @Override public SecondMoment copy() { SecondMoment result = new SecondMoment(); // no try-catch or advertised NAE because args are guaranteed non-null copy(this, result); return result; } /** * Copies source to dest. *

                              Neither source nor dest can be null.

                              * * @param source SecondMoment to copy * @param dest SecondMoment to copy to * @throws NullArgumentException if either source or dest is null */ public static void copy(SecondMoment source, SecondMoment dest) throws NullArgumentException { MathUtils.checkNotNull(source); MathUtils.checkNotNull(dest); FirstMoment.copy(source, dest); dest.m2 = source.m2; } } ././@LongLink100644 0 0 147 12126630647 10264 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/stat/descriptive/moment/SemiVariance.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/stat/descriptive/moment/SemiVariance.ja100644 1750 1750 32734 12126627717 32235 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.stat.descriptive.moment; import java.io.Serializable; import org.apache.commons.math3.exception.MathIllegalArgumentException; import org.apache.commons.math3.exception.NullArgumentException; import org.apache.commons.math3.stat.descriptive.AbstractUnivariateStatistic; import org.apache.commons.math3.util.MathUtils; /** *

                              Computes the semivariance of a set of values with respect to a given cutoff value. * We define the downside semivariance of a set of values x * against the cutoff value cutoff to be
                              * Σ (x[i] - target)2 / df
                              * where the sum is taken over all i such that x[i] < cutoff * and df is the length of x (non-bias-corrected) or * one less than this number (bias corrected). The upside semivariance * is defined similarly, with the sum taken over values of x that * exceed the cutoff value.

                              * *

                              The cutoff value defaults to the mean, bias correction defaults to true * and the "variance direction" (upside or downside) defaults to downside. The variance direction * and bias correction may be set using property setters or their values can provided as * parameters to {@link #evaluate(double[], double, Direction, boolean, int, int)}.

                              * *

                              If the input array is null, evaluate methods throw * IllegalArgumentException. If the array has length 1, 0 * is returned, regardless of the value of the cutoff. * *

                              Note that this class is not intended to be threadsafe. If * multiple threads access an instance of this class concurrently, and one or * more of these threads invoke property setters, external synchronization must * be provided to ensure correct results.

                              * * @since 2.1 * @version $Id: SemiVariance.java 1385386 2012-09-16 22:11:15Z psteitz $ */ public class SemiVariance extends AbstractUnivariateStatistic implements Serializable { /** * The UPSIDE Direction is used to specify that the observations above the * cutoff point will be used to calculate SemiVariance. */ public static final Direction UPSIDE_VARIANCE = Direction.UPSIDE; /** * The DOWNSIDE Direction is used to specify that the observations below * the cutoff point will be used to calculate SemiVariance */ public static final Direction DOWNSIDE_VARIANCE = Direction.DOWNSIDE; /** Serializable version identifier */ private static final long serialVersionUID = -2653430366886024994L; /** * Determines whether or not bias correction is applied when computing the * value of the statisic. True means that bias is corrected. */ private boolean biasCorrected = true; /** * Determines whether to calculate downside or upside SemiVariance. */ private Direction varianceDirection = Direction.DOWNSIDE; /** * Constructs a SemiVariance with default (true) biasCorrected * property and default (Downside) varianceDirection property. */ public SemiVariance() { } /** * Constructs a SemiVariance with the specified biasCorrected * property and default (Downside) varianceDirection property. * * @param biasCorrected setting for bias correction - true means * bias will be corrected and is equivalent to using the argumentless * constructor */ public SemiVariance(final boolean biasCorrected) { this.biasCorrected = biasCorrected; } /** * Constructs a SemiVariance with the specified Direction property * and default (true) biasCorrected property * * @param direction setting for the direction of the SemiVariance * to calculate */ public SemiVariance(final Direction direction) { this.varianceDirection = direction; } /** * Constructs a SemiVariance with the specified isBiasCorrected * property and the specified Direction property. * * @param corrected setting for bias correction - true means * bias will be corrected and is equivalent to using the argumentless * constructor * * @param direction setting for the direction of the SemiVariance * to calculate */ public SemiVariance(final boolean corrected, final Direction direction) { this.biasCorrected = corrected; this.varianceDirection = direction; } /** * Copy constructor, creates a new {@code SemiVariance} identical * to the {@code original} * * @param original the {@code SemiVariance} instance to copy * @throws NullArgumentException if original is null */ public SemiVariance(final SemiVariance original) throws NullArgumentException { copy(original, this); } /** * {@inheritDoc} */ @Override public SemiVariance copy() { SemiVariance result = new SemiVariance(); // No try-catch or advertised exception because args are guaranteed non-null copy(this, result); return result; } /** * Copies source to dest. *

                              Neither source nor dest can be null.

                              * * @param source SemiVariance to copy * @param dest SemiVariance to copy to * @throws NullArgumentException if either source or dest is null */ public static void copy(final SemiVariance source, SemiVariance dest) throws NullArgumentException { MathUtils.checkNotNull(source); MathUtils.checkNotNull(dest); dest.setData(source.getDataRef()); dest.biasCorrected = source.biasCorrected; dest.varianceDirection = source.varianceDirection; } /** *

                              Returns the {@link SemiVariance} of the designated values against the mean, using * instance properties varianceDirection and biasCorrection.

                              * *

                              Returns NaN if the array is empty and throws * IllegalArgumentException if the array is null.

                              * * @param values the input array * @param start index of the first array element to include * @param length the number of elements to include * @return the SemiVariance * @throws MathIllegalArgumentException if the parameters are not valid * */ @Override public double evaluate(final double[] values, final int start, final int length) throws MathIllegalArgumentException { double m = (new Mean()).evaluate(values, start, length); return evaluate(values, m, varianceDirection, biasCorrected, 0, values.length); } /** * This method calculates {@link SemiVariance} for the entire array against the mean, using * the current value of the biasCorrection instance property. * * @param values the input array * @param direction the {@link Direction} of the semivariance * @return the SemiVariance * @throws MathIllegalArgumentException if values is null * */ public double evaluate(final double[] values, Direction direction) throws MathIllegalArgumentException { double m = (new Mean()).evaluate(values); return evaluate (values, m, direction, biasCorrected, 0, values.length); } /** *

                              Returns the {@link SemiVariance} of the designated values against the cutoff, using * instance properties variancDirection and biasCorrection.

                              * *

                              Returns NaN if the array is empty and throws * MathIllegalArgumentException if the array is null.

                              * * @param values the input array * @param cutoff the reference point * @return the SemiVariance * @throws MathIllegalArgumentException if values is null */ public double evaluate(final double[] values, final double cutoff) throws MathIllegalArgumentException { return evaluate(values, cutoff, varianceDirection, biasCorrected, 0, values.length); } /** *

                              Returns the {@link SemiVariance} of the designated values against the cutoff in the * given direction, using the current value of the biasCorrection instance property.

                              * *

                              Returns NaN if the array is empty and throws * MathIllegalArgumentException if the array is null.

                              * * @param values the input array * @param cutoff the reference point * @param direction the {@link Direction} of the semivariance * @return the SemiVariance * @throws MathIllegalArgumentException if values is null */ public double evaluate(final double[] values, final double cutoff, final Direction direction) throws MathIllegalArgumentException { return evaluate(values, cutoff, direction, biasCorrected, 0, values.length); } /** *

                              Returns the {@link SemiVariance} of the designated values against the cutoff * in the given direction with the provided bias correction.

                              * *

                              Returns NaN if the array is empty and throws * IllegalArgumentException if the array is null.

                              * * @param values the input array * @param cutoff the reference point * @param direction the {@link Direction} of the semivariance * @param corrected the BiasCorrection flag * @param start index of the first array element to include * @param length the number of elements to include * @return the SemiVariance * @throws MathIllegalArgumentException if the parameters are not valid * */ public double evaluate (final double[] values, final double cutoff, final Direction direction, final boolean corrected, final int start, final int length) throws MathIllegalArgumentException { test(values, start, length); if (values.length == 0) { return Double.NaN; } else { if (values.length == 1) { return 0.0; } else { final boolean booleanDirection = direction.getDirection(); double dev = 0.0; double sumsq = 0.0; for (int i = start; i < length; i++) { if ((values[i] > cutoff) == booleanDirection) { dev = values[i] - cutoff; sumsq += dev * dev; } } if (corrected) { return sumsq / (length - 1.0); } else { return sumsq / length; } } } } /** * Returns true iff biasCorrected property is set to true. * * @return the value of biasCorrected. */ public boolean isBiasCorrected() { return biasCorrected; } /** * Sets the biasCorrected property. * * @param biasCorrected new biasCorrected property value */ public void setBiasCorrected(boolean biasCorrected) { this.biasCorrected = biasCorrected; } /** * Returns the varianceDirection property. * * @return the varianceDirection */ public Direction getVarianceDirection () { return varianceDirection; } /** * Sets the variance direction * * @param varianceDirection the direction of the semivariance */ public void setVarianceDirection(Direction varianceDirection) { this.varianceDirection = varianceDirection; } /** * The direction of the semivariance - either upside or downside. The direction * is represented by boolean, with true corresponding to UPSIDE semivariance. */ public enum Direction { /** * The UPSIDE Direction is used to specify that the observations above the * cutoff point will be used to calculate SemiVariance */ UPSIDE (true), /** * The DOWNSIDE Direction is used to specify that the observations below * the cutoff point will be used to calculate SemiVariance */ DOWNSIDE (false); /** * boolean value UPSIDE <-> true */ private boolean direction; /** * Create a Direction with the given value. * * @param b boolean value representing the Direction. True corresponds to UPSIDE. */ Direction (boolean b) { direction = b; } /** * Returns the value of this Direction. True corresponds to UPSIDE. * * @return true if direction is UPSIDE; false otherwise */ boolean getDirection () { return direction; } } } ././@LongLink100644 0 0 150 12126630647 10256 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/stat/descriptive/moment/GeometricMean.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/stat/descriptive/moment/GeometricMean.j100644 1750 1750 16234 12126627717 32242 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.stat.descriptive.moment; import java.io.Serializable; import org.apache.commons.math3.exception.MathIllegalArgumentException; import org.apache.commons.math3.exception.MathIllegalStateException; import org.apache.commons.math3.exception.NullArgumentException; import org.apache.commons.math3.exception.util.LocalizedFormats; import org.apache.commons.math3.stat.descriptive.AbstractStorelessUnivariateStatistic; import org.apache.commons.math3.stat.descriptive.StorelessUnivariateStatistic; import org.apache.commons.math3.stat.descriptive.summary.SumOfLogs; import org.apache.commons.math3.util.FastMath; import org.apache.commons.math3.util.MathUtils; /** * Returns the * geometric mean of the available values. *

                              * Uses a {@link SumOfLogs} instance to compute sum of logs and returns * exp( 1/n (sum of logs) ). Therefore,

                              *
                                *
                              • If any of values are < 0, the result is NaN.
                              • *
                              • If all values are non-negative and less than * Double.POSITIVE_INFINITY, but at least one value is 0, the * result is 0.
                              • *
                              • If both Double.POSITIVE_INFINITY and * Double.NEGATIVE_INFINITY are among the values, the result is * NaN.
                              • *

                              *

                              * Note that this implementation is not synchronized. If * multiple threads access an instance of this class concurrently, and at least * one of the threads invokes the increment() or * clear() method, it must be synchronized externally.

                              * * * @version $Id: GeometricMean.java 1416643 2012-12-03 19:37:14Z tn $ */ public class GeometricMean extends AbstractStorelessUnivariateStatistic implements Serializable { /** Serializable version identifier */ private static final long serialVersionUID = -8178734905303459453L; /** Wrapped SumOfLogs instance */ private StorelessUnivariateStatistic sumOfLogs; /** * Create a GeometricMean instance */ public GeometricMean() { sumOfLogs = new SumOfLogs(); } /** * Copy constructor, creates a new {@code GeometricMean} identical * to the {@code original} * * @param original the {@code GeometricMean} instance to copy * @throws NullArgumentException if original is null */ public GeometricMean(GeometricMean original) throws NullArgumentException { super(); copy(original, this); } /** * Create a GeometricMean instance using the given SumOfLogs instance * @param sumOfLogs sum of logs instance to use for computation */ public GeometricMean(SumOfLogs sumOfLogs) { this.sumOfLogs = sumOfLogs; } /** * {@inheritDoc} */ @Override public GeometricMean copy() { GeometricMean result = new GeometricMean(); // no try-catch or advertised exception because args guaranteed non-null copy(this, result); return result; } /** * {@inheritDoc} */ @Override public void increment(final double d) { sumOfLogs.increment(d); } /** * {@inheritDoc} */ @Override public double getResult() { if (sumOfLogs.getN() > 0) { return FastMath.exp(sumOfLogs.getResult() / sumOfLogs.getN()); } else { return Double.NaN; } } /** * {@inheritDoc} */ @Override public void clear() { sumOfLogs.clear(); } /** * Returns the geometric mean of the entries in the specified portion * of the input array. *

                              * See {@link GeometricMean} for details on the computing algorithm.

                              *

                              * Throws IllegalArgumentException if the array is null.

                              * * @param values input array containing the values * @param begin first array element to include * @param length the number of elements to include * @return the geometric mean or Double.NaN if length = 0 or * any of the values are <= 0. * @throws MathIllegalArgumentException if the input array is null or the array * index parameters are not valid */ @Override public double evaluate( final double[] values, final int begin, final int length) throws MathIllegalArgumentException { return FastMath.exp( sumOfLogs.evaluate(values, begin, length) / length); } /** * {@inheritDoc} */ public long getN() { return sumOfLogs.getN(); } /** *

                              Sets the implementation for the sum of logs.

                              *

                              This method must be activated before any data has been added - i.e., * before {@link #increment(double) increment} has been used to add data; * otherwise an IllegalStateException will be thrown.

                              * * @param sumLogImpl the StorelessUnivariateStatistic instance to use * for computing the log sum * @throws MathIllegalStateException if data has already been added * (i.e if n > 0) */ public void setSumLogImpl(StorelessUnivariateStatistic sumLogImpl) throws MathIllegalStateException { checkEmpty(); this.sumOfLogs = sumLogImpl; } /** * Returns the currently configured sum of logs implementation * * @return the StorelessUnivariateStatistic implementing the log sum */ public StorelessUnivariateStatistic getSumLogImpl() { return sumOfLogs; } /** * Copies source to dest. *

                              Neither source nor dest can be null.

                              * * @param source GeometricMean to copy * @param dest GeometricMean to copy to * @throws NullArgumentException if either source or dest is null */ public static void copy(GeometricMean source, GeometricMean dest) throws NullArgumentException { MathUtils.checkNotNull(source); MathUtils.checkNotNull(dest); dest.setData(source.getDataRef()); dest.sumOfLogs = source.sumOfLogs.copy(); } /** * Throws MathIllegalStateException if n > 0. * @throws MathIllegalStateException if data has been added to this statistic */ private void checkEmpty() throws MathIllegalStateException { if (getN() > 0) { throw new MathIllegalStateException( LocalizedFormats.VALUES_ADDED_BEFORE_CONFIGURING_STATISTIC, getN()); } } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/stat/descriptive/moment/Skewness.java100644 1750 1750 16173 12126627717 32017 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.stat.descriptive.moment; import java.io.Serializable; import org.apache.commons.math3.exception.MathIllegalArgumentException; import org.apache.commons.math3.exception.NullArgumentException; import org.apache.commons.math3.stat.descriptive.AbstractStorelessUnivariateStatistic; import org.apache.commons.math3.util.FastMath; import org.apache.commons.math3.util.MathUtils; /** * Computes the skewness of the available values. *

                              * We use the following (unbiased) formula to define skewness:

                              *

                              * skewness = [n / (n -1) (n - 2)] sum[(x_i - mean)^3] / std^3

                              *

                              * where n is the number of values, mean is the {@link Mean} and std is the * {@link StandardDeviation}

                              *

                              * Note that this implementation is not synchronized. If * multiple threads access an instance of this class concurrently, and at least * one of the threads invokes the increment() or * clear() method, it must be synchronized externally.

                              * * @version $Id: Skewness.java 1416643 2012-12-03 19:37:14Z tn $ */ public class Skewness extends AbstractStorelessUnivariateStatistic implements Serializable { /** Serializable version identifier */ private static final long serialVersionUID = 7101857578996691352L; /** Third moment on which this statistic is based */ protected ThirdMoment moment = null; /** * Determines whether or not this statistic can be incremented or cleared. *

                              * Statistics based on (constructed from) external moments cannot * be incremented or cleared.

                              */ protected boolean incMoment; /** * Constructs a Skewness */ public Skewness() { incMoment = true; moment = new ThirdMoment(); } /** * Constructs a Skewness with an external moment * @param m3 external moment */ public Skewness(final ThirdMoment m3) { incMoment = false; this.moment = m3; } /** * Copy constructor, creates a new {@code Skewness} identical * to the {@code original} * * @param original the {@code Skewness} instance to copy * @throws NullArgumentException if original is null */ public Skewness(Skewness original) throws NullArgumentException { copy(original, this); } /** * {@inheritDoc} *

                              Note that when {@link #Skewness(ThirdMoment)} is used to * create a Skewness, this method does nothing. In that case, the * ThirdMoment should be incremented directly.

                              */ @Override public void increment(final double d) { if (incMoment) { moment.increment(d); } } /** * Returns the value of the statistic based on the values that have been added. *

                              * See {@link Skewness} for the definition used in the computation.

                              * * @return the skewness of the available values. */ @Override public double getResult() { if (moment.n < 3) { return Double.NaN; } double variance = moment.m2 / (moment.n - 1); if (variance < 10E-20) { return 0.0d; } else { double n0 = moment.getN(); return (n0 * moment.m3) / ((n0 - 1) * (n0 -2) * FastMath.sqrt(variance) * variance); } } /** * {@inheritDoc} */ public long getN() { return moment.getN(); } /** * {@inheritDoc} */ @Override public void clear() { if (incMoment) { moment.clear(); } } /** * Returns the Skewness of the entries in the specifed portion of the * input array. *

                              * See {@link Skewness} for the definition used in the computation.

                              *

                              * Throws IllegalArgumentException if the array is null.

                              * * @param values the input array * @param begin the index of the first array element to include * @param length the number of elements to include * @return the skewness of the values or Double.NaN if length is less than * 3 * @throws MathIllegalArgumentException if the array is null or the array index * parameters are not valid */ @Override public double evaluate(final double[] values,final int begin, final int length) throws MathIllegalArgumentException { // Initialize the skewness double skew = Double.NaN; if (test(values, begin, length) && length > 2 ){ Mean mean = new Mean(); // Get the mean and the standard deviation double m = mean.evaluate(values, begin, length); // Calc the std, this is implemented here instead // of using the standardDeviation method eliminate // a duplicate pass to get the mean double accum = 0.0; double accum2 = 0.0; for (int i = begin; i < begin + length; i++) { final double d = values[i] - m; accum += d * d; accum2 += d; } final double variance = (accum - (accum2 * accum2 / length)) / (length - 1); double accum3 = 0.0; for (int i = begin; i < begin + length; i++) { final double d = values[i] - m; accum3 += d * d * d; } accum3 /= variance * FastMath.sqrt(variance); // Get N double n0 = length; // Calculate skewness skew = (n0 / ((n0 - 1) * (n0 - 2))) * accum3; } return skew; } /** * {@inheritDoc} */ @Override public Skewness copy() { Skewness result = new Skewness(); // No try-catch or advertised exception because args are guaranteed non-null copy(this, result); return result; } /** * Copies source to dest. *

                              Neither source nor dest can be null.

                              * * @param source Skewness to copy * @param dest Skewness to copy to * @throws NullArgumentException if either source or dest is null */ public static void copy(Skewness source, Skewness dest) throws NullArgumentException { MathUtils.checkNotNull(source); MathUtils.checkNotNull(dest); dest.setData(source.getDataRef()); dest.moment = new ThirdMoment(source.moment.copy()); dest.incMoment = source.incMoment; } } ././@LongLink100644 0 0 150 12126630647 10256 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/stat/descriptive/moment/VectorialMean.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/stat/descriptive/moment/VectorialMean.j100644 1750 1750 6206 12126627717 32232 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.stat.descriptive.moment; import java.io.Serializable; import java.util.Arrays; import org.apache.commons.math3.exception.DimensionMismatchException; /** * Returns the arithmetic mean of the available vectors. * @since 1.2 * @version $Id: VectorialMean.java 1416643 2012-12-03 19:37:14Z tn $ */ public class VectorialMean implements Serializable { /** Serializable version identifier */ private static final long serialVersionUID = 8223009086481006892L; /** Means for each component. */ private final Mean[] means; /** Constructs a VectorialMean. * @param dimension vectors dimension */ public VectorialMean(int dimension) { means = new Mean[dimension]; for (int i = 0; i < dimension; ++i) { means[i] = new Mean(); } } /** * Add a new vector to the sample. * @param v vector to add * @throws DimensionMismatchException if the vector does not have the right dimension */ public void increment(double[] v) throws DimensionMismatchException { if (v.length != means.length) { throw new DimensionMismatchException(v.length, means.length); } for (int i = 0; i < v.length; ++i) { means[i].increment(v[i]); } } /** * Get the mean vector. * @return mean vector */ public double[] getResult() { double[] result = new double[means.length]; for (int i = 0; i < result.length; ++i) { result[i] = means[i].getResult(); } return result; } /** * Get the number of vectors in the sample. * @return number of vectors in the sample */ public long getN() { return (means.length == 0) ? 0 : means[0].getN(); } /** {@inheritDoc} */ @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + Arrays.hashCode(means); return result; } /** {@inheritDoc} */ @Override public boolean equals(Object obj) { if (this == obj) { return true; } if (!(obj instanceof VectorialMean)) { return false; } VectorialMean other = (VectorialMean) obj; if (!Arrays.equals(means, other.means)) { return false; } return true; } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/stat/descriptive/moment/Kurtosis.java100644 1750 1750 16337 12126627717 32042 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.stat.descriptive.moment; import java.io.Serializable; import org.apache.commons.math3.exception.MathIllegalArgumentException; import org.apache.commons.math3.exception.NullArgumentException; import org.apache.commons.math3.stat.descriptive.AbstractStorelessUnivariateStatistic; import org.apache.commons.math3.util.FastMath; import org.apache.commons.math3.util.MathUtils; /** * Computes the Kurtosis of the available values. *

                              * We use the following (unbiased) formula to define kurtosis:

                              *

                              * kurtosis = { [n(n+1) / (n -1)(n - 2)(n-3)] sum[(x_i - mean)^4] / std^4 } - [3(n-1)^2 / (n-2)(n-3)] *

                              * where n is the number of values, mean is the {@link Mean} and std is the * {@link StandardDeviation}

                              *

                              * Note that this statistic is undefined for n < 4. Double.Nan * is returned when there is not sufficient data to compute the statistic.

                              *

                              * Note that this implementation is not synchronized. If * multiple threads access an instance of this class concurrently, and at least * one of the threads invokes the increment() or * clear() method, it must be synchronized externally.

                              * * @version $Id: Kurtosis.java 1416643 2012-12-03 19:37:14Z tn $ */ public class Kurtosis extends AbstractStorelessUnivariateStatistic implements Serializable { /** Serializable version identifier */ private static final long serialVersionUID = 2784465764798260919L; /**Fourth Moment on which this statistic is based */ protected FourthMoment moment; /** * Determines whether or not this statistic can be incremented or cleared. *

                              * Statistics based on (constructed from) external moments cannot * be incremented or cleared.

                              */ protected boolean incMoment; /** * Construct a Kurtosis */ public Kurtosis() { incMoment = true; moment = new FourthMoment(); } /** * Construct a Kurtosis from an external moment * * @param m4 external Moment */ public Kurtosis(final FourthMoment m4) { incMoment = false; this.moment = m4; } /** * Copy constructor, creates a new {@code Kurtosis} identical * to the {@code original} * * @param original the {@code Kurtosis} instance to copy * @throws NullArgumentException if original is null */ public Kurtosis(Kurtosis original) throws NullArgumentException { copy(original, this); } /** * {@inheritDoc} *

                              Note that when {@link #Kurtosis(FourthMoment)} is used to * create a Variance, this method does nothing. In that case, the * FourthMoment should be incremented directly.

                              */ @Override public void increment(final double d) { if (incMoment) { moment.increment(d); } } /** * {@inheritDoc} */ @Override public double getResult() { double kurtosis = Double.NaN; if (moment.getN() > 3) { double variance = moment.m2 / (moment.n - 1); if (moment.n <= 3 || variance < 10E-20) { kurtosis = 0.0; } else { double n = moment.n; kurtosis = (n * (n + 1) * moment.getResult() - 3 * moment.m2 * moment.m2 * (n - 1)) / ((n - 1) * (n -2) * (n -3) * variance * variance); } } return kurtosis; } /** * {@inheritDoc} */ @Override public void clear() { if (incMoment) { moment.clear(); } } /** * {@inheritDoc} */ public long getN() { return moment.getN(); } /* UnvariateStatistic Approach */ /** * Returns the kurtosis of the entries in the specified portion of the * input array. *

                              * See {@link Kurtosis} for details on the computing algorithm.

                              *

                              * Throws IllegalArgumentException if the array is null.

                              * * @param values the input array * @param begin index of the first array element to include * @param length the number of elements to include * @return the kurtosis of the values or Double.NaN if length is less than 4 * @throws MathIllegalArgumentException if the input array is null or the array * index parameters are not valid */ @Override public double evaluate(final double[] values,final int begin, final int length) throws MathIllegalArgumentException { // Initialize the kurtosis double kurt = Double.NaN; if (test(values, begin, length) && length > 3) { // Compute the mean and standard deviation Variance variance = new Variance(); variance.incrementAll(values, begin, length); double mean = variance.moment.m1; double stdDev = FastMath.sqrt(variance.getResult()); // Sum the ^4 of the distance from the mean divided by the // standard deviation double accum3 = 0.0; for (int i = begin; i < begin + length; i++) { accum3 += FastMath.pow(values[i] - mean, 4.0); } accum3 /= FastMath.pow(stdDev, 4.0d); // Get N double n0 = length; double coefficientOne = (n0 * (n0 + 1)) / ((n0 - 1) * (n0 - 2) * (n0 - 3)); double termTwo = (3 * FastMath.pow(n0 - 1, 2.0)) / ((n0 - 2) * (n0 - 3)); // Calculate kurtosis kurt = (coefficientOne * accum3) - termTwo; } return kurt; } /** * {@inheritDoc} */ @Override public Kurtosis copy() { Kurtosis result = new Kurtosis(); // No try-catch because args are guaranteed non-null copy(this, result); return result; } /** * Copies source to dest. *

                              Neither source nor dest can be null.

                              * * @param source Kurtosis to copy * @param dest Kurtosis to copy to * @throws NullArgumentException if either source or dest is null */ public static void copy(Kurtosis source, Kurtosis dest) throws NullArgumentException { MathUtils.checkNotNull(source); MathUtils.checkNotNull(dest); dest.setData(source.getDataRef()); dest.moment = source.moment.copy(); dest.incMoment = source.incMoment; } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/stat/descriptive/moment/Variance.java100644 1750 1750 62053 12126627717 31743 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.stat.descriptive.moment; import java.io.Serializable; import org.apache.commons.math3.exception.MathIllegalArgumentException; import org.apache.commons.math3.exception.NullArgumentException; import org.apache.commons.math3.exception.util.LocalizedFormats; import org.apache.commons.math3.stat.descriptive.WeightedEvaluation; import org.apache.commons.math3.stat.descriptive.AbstractStorelessUnivariateStatistic; import org.apache.commons.math3.util.MathUtils; /** * Computes the variance of the available values. By default, the unbiased * "sample variance" definitional formula is used: *

                              * variance = sum((x_i - mean)^2) / (n - 1)

                              *

                              * where mean is the {@link Mean} and n is the number * of sample observations.

                              *

                              * The definitional formula does not have good numerical properties, so * this implementation does not compute the statistic using the definitional * formula.

                                *
                              • The getResult method computes the variance using * updating formulas based on West's algorithm, as described in * Chan, T. F. and * J. G. Lewis 1979, Communications of the ACM, * vol. 22 no. 9, pp. 526-531.
                              • *
                              • The evaluate methods leverage the fact that they have the * full array of values in memory to execute a two-pass algorithm. * Specifically, these methods use the "corrected two-pass algorithm" from * Chan, Golub, Levesque, Algorithms for Computing the Sample Variance, * American Statistician, vol. 37, no. 3 (1983) pp. 242-247.
                              * Note that adding values using increment or * incrementAll and then executing getResult will * sometimes give a different, less accurate, result than executing * evaluate with the full array of values. The former approach * should only be used when the full array of values is not available.

                              *

                              * The "population variance" ( sum((x_i - mean)^2) / n ) can also * be computed using this statistic. The isBiasCorrected * property determines whether the "population" or "sample" value is * returned by the evaluate and getResult methods. * To compute population variances, set this property to false. *

                              *

                              * Note that this implementation is not synchronized. If * multiple threads access an instance of this class concurrently, and at least * one of the threads invokes the increment() or * clear() method, it must be synchronized externally.

                              * * @version $Id: Variance.java 1416643 2012-12-03 19:37:14Z tn $ */ public class Variance extends AbstractStorelessUnivariateStatistic implements Serializable, WeightedEvaluation { /** Serializable version identifier */ private static final long serialVersionUID = -9111962718267217978L; /** SecondMoment is used in incremental calculation of Variance*/ protected SecondMoment moment = null; /** * Whether or not {@link #increment(double)} should increment * the internal second moment. When a Variance is constructed with an * external SecondMoment as a constructor parameter, this property is * set to false and increments must be applied to the second moment * directly. */ protected boolean incMoment = true; /** * Whether or not bias correction is applied when computing the * value of the statistic. True means that bias is corrected. See * {@link Variance} for details on the formula. */ private boolean isBiasCorrected = true; /** * Constructs a Variance with default (true) isBiasCorrected * property. */ public Variance() { moment = new SecondMoment(); } /** * Constructs a Variance based on an external second moment. * When this constructor is used, the statistic may only be * incremented via the moment, i.e., {@link #increment(double)} * does nothing; whereas {@code m2.increment(value)} increments * both {@code m2} and the Variance instance constructed from it. * * @param m2 the SecondMoment (Third or Fourth moments work * here as well.) */ public Variance(final SecondMoment m2) { incMoment = false; this.moment = m2; } /** * Constructs a Variance with the specified isBiasCorrected * property * * @param isBiasCorrected setting for bias correction - true means * bias will be corrected and is equivalent to using the argumentless * constructor */ public Variance(boolean isBiasCorrected) { moment = new SecondMoment(); this.isBiasCorrected = isBiasCorrected; } /** * Constructs a Variance with the specified isBiasCorrected * property and the supplied external second moment. * * @param isBiasCorrected setting for bias correction - true means * bias will be corrected * @param m2 the SecondMoment (Third or Fourth moments work * here as well.) */ public Variance(boolean isBiasCorrected, SecondMoment m2) { incMoment = false; this.moment = m2; this.isBiasCorrected = isBiasCorrected; } /** * Copy constructor, creates a new {@code Variance} identical * to the {@code original} * * @param original the {@code Variance} instance to copy * @throws NullArgumentException if original is null */ public Variance(Variance original) throws NullArgumentException { copy(original, this); } /** * {@inheritDoc} *

                              If all values are available, it is more accurate to use * {@link #evaluate(double[])} rather than adding values one at a time * using this method and then executing {@link #getResult}, since * evaluate leverages the fact that is has the full * list of values together to execute a two-pass algorithm. * See {@link Variance}.

                              * *

                              Note also that when {@link #Variance(SecondMoment)} is used to * create a Variance, this method does nothing. In that case, the * SecondMoment should be incremented directly.

                              */ @Override public void increment(final double d) { if (incMoment) { moment.increment(d); } } /** * {@inheritDoc} */ @Override public double getResult() { if (moment.n == 0) { return Double.NaN; } else if (moment.n == 1) { return 0d; } else { if (isBiasCorrected) { return moment.m2 / (moment.n - 1d); } else { return moment.m2 / (moment.n); } } } /** * {@inheritDoc} */ public long getN() { return moment.getN(); } /** * {@inheritDoc} */ @Override public void clear() { if (incMoment) { moment.clear(); } } /** * Returns the variance of the entries in the input array, or * Double.NaN if the array is empty. *

                              * See {@link Variance} for details on the computing algorithm.

                              *

                              * Returns 0 for a single-value (i.e. length = 1) sample.

                              *

                              * Throws MathIllegalArgumentException if the array is null.

                              *

                              * Does not change the internal state of the statistic.

                              * * @param values the input array * @return the variance of the values or Double.NaN if length = 0 * @throws MathIllegalArgumentException if the array is null */ @Override public double evaluate(final double[] values) throws MathIllegalArgumentException { if (values == null) { throw new NullArgumentException(LocalizedFormats.INPUT_ARRAY); } return evaluate(values, 0, values.length); } /** * Returns the variance of the entries in the specified portion of * the input array, or Double.NaN if the designated subarray * is empty. *

                              * See {@link Variance} for details on the computing algorithm.

                              *

                              * Returns 0 for a single-value (i.e. length = 1) sample.

                              *

                              * Does not change the internal state of the statistic.

                              *

                              * Throws MathIllegalArgumentException if the array is null.

                              * * @param values the input array * @param begin index of the first array element to include * @param length the number of elements to include * @return the variance of the values or Double.NaN if length = 0 * @throws MathIllegalArgumentException if the array is null or the array index * parameters are not valid */ @Override public double evaluate(final double[] values, final int begin, final int length) throws MathIllegalArgumentException { double var = Double.NaN; if (test(values, begin, length)) { clear(); if (length == 1) { var = 0.0; } else if (length > 1) { Mean mean = new Mean(); double m = mean.evaluate(values, begin, length); var = evaluate(values, m, begin, length); } } return var; } /** *

                              Returns the weighted variance of the entries in the specified portion of * the input array, or Double.NaN if the designated subarray * is empty.

                              *

                              * Uses the formula

                                   *   Σ(weights[i]*(values[i] - weightedMean)2)/(Σ(weights[i]) - 1)
                                   * 
                              * where weightedMean is the weighted mean

                              *

                              * This formula will not return the same result as the unweighted variance when all * weights are equal, unless all weights are equal to 1. The formula assumes that * weights are to be treated as "expansion values," as will be the case if for example * the weights represent frequency counts. To normalize weights so that the denominator * in the variance computation equals the length of the input vector minus one, use

                                   *   evaluate(values, MathArrays.normalizeArray(weights, values.length)); 
                                   * 
                              *

                              * Returns 0 for a single-value (i.e. length = 1) sample.

                              *

                              * Throws IllegalArgumentException if any of the following are true: *

                              • the values array is null
                              • *
                              • the weights array is null
                              • *
                              • the weights array does not have the same length as the values array
                              • *
                              • the weights array contains one or more infinite values
                              • *
                              • the weights array contains one or more NaN values
                              • *
                              • the weights array contains negative values
                              • *
                              • the start and length arguments do not determine a valid array
                              • *

                              *

                              * Does not change the internal state of the statistic.

                              *

                              * Throws MathIllegalArgumentException if either array is null.

                              * * @param values the input array * @param weights the weights array * @param begin index of the first array element to include * @param length the number of elements to include * @return the weighted variance of the values or Double.NaN if length = 0 * @throws MathIllegalArgumentException if the parameters are not valid * @since 2.1 */ public double evaluate(final double[] values, final double[] weights, final int begin, final int length) throws MathIllegalArgumentException { double var = Double.NaN; if (test(values, weights,begin, length)) { clear(); if (length == 1) { var = 0.0; } else if (length > 1) { Mean mean = new Mean(); double m = mean.evaluate(values, weights, begin, length); var = evaluate(values, weights, m, begin, length); } } return var; } /** *

                              * Returns the weighted variance of the entries in the the input array.

                              *

                              * Uses the formula

                                   *   Σ(weights[i]*(values[i] - weightedMean)2)/(Σ(weights[i]) - 1)
                                   * 
                              * where weightedMean is the weighted mean

                              *

                              * This formula will not return the same result as the unweighted variance when all * weights are equal, unless all weights are equal to 1. The formula assumes that * weights are to be treated as "expansion values," as will be the case if for example * the weights represent frequency counts. To normalize weights so that the denominator * in the variance computation equals the length of the input vector minus one, use

                                   *   evaluate(values, MathArrays.normalizeArray(weights, values.length)); 
                                   * 
                              *

                              * Returns 0 for a single-value (i.e. length = 1) sample.

                              *

                              * Throws MathIllegalArgumentException if any of the following are true: *

                              • the values array is null
                              • *
                              • the weights array is null
                              • *
                              • the weights array does not have the same length as the values array
                              • *
                              • the weights array contains one or more infinite values
                              • *
                              • the weights array contains one or more NaN values
                              • *
                              • the weights array contains negative values
                              • *

                              *

                              * Does not change the internal state of the statistic.

                              *

                              * Throws MathIllegalArgumentException if either array is null.

                              * * @param values the input array * @param weights the weights array * @return the weighted variance of the values * @throws MathIllegalArgumentException if the parameters are not valid * @since 2.1 */ public double evaluate(final double[] values, final double[] weights) throws MathIllegalArgumentException { return evaluate(values, weights, 0, values.length); } /** * Returns the variance of the entries in the specified portion of * the input array, using the precomputed mean value. Returns * Double.NaN if the designated subarray is empty. *

                              * See {@link Variance} for details on the computing algorithm.

                              *

                              * The formula used assumes that the supplied mean value is the arithmetic * mean of the sample data, not a known population parameter. This method * is supplied only to save computation when the mean has already been * computed.

                              *

                              * Returns 0 for a single-value (i.e. length = 1) sample.

                              *

                              * Throws MathIllegalArgumentException if the array is null.

                              *

                              * Does not change the internal state of the statistic.

                              * * @param values the input array * @param mean the precomputed mean value * @param begin index of the first array element to include * @param length the number of elements to include * @return the variance of the values or Double.NaN if length = 0 * @throws MathIllegalArgumentException if the array is null or the array index * parameters are not valid */ public double evaluate(final double[] values, final double mean, final int begin, final int length) throws MathIllegalArgumentException { double var = Double.NaN; if (test(values, begin, length)) { if (length == 1) { var = 0.0; } else if (length > 1) { double accum = 0.0; double dev = 0.0; double accum2 = 0.0; for (int i = begin; i < begin + length; i++) { dev = values[i] - mean; accum += dev * dev; accum2 += dev; } double len = length; if (isBiasCorrected) { var = (accum - (accum2 * accum2 / len)) / (len - 1.0); } else { var = (accum - (accum2 * accum2 / len)) / len; } } } return var; } /** * Returns the variance of the entries in the input array, using the * precomputed mean value. Returns Double.NaN if the array * is empty. *

                              * See {@link Variance} for details on the computing algorithm.

                              *

                              * If isBiasCorrected is true the formula used * assumes that the supplied mean value is the arithmetic mean of the * sample data, not a known population parameter. If the mean is a known * population parameter, or if the "population" version of the variance is * desired, set isBiasCorrected to false before * invoking this method.

                              *

                              * Returns 0 for a single-value (i.e. length = 1) sample.

                              *

                              * Throws MathIllegalArgumentException if the array is null.

                              *

                              * Does not change the internal state of the statistic.

                              * * @param values the input array * @param mean the precomputed mean value * @return the variance of the values or Double.NaN if the array is empty * @throws MathIllegalArgumentException if the array is null */ public double evaluate(final double[] values, final double mean) throws MathIllegalArgumentException { return evaluate(values, mean, 0, values.length); } /** * Returns the weighted variance of the entries in the specified portion of * the input array, using the precomputed weighted mean value. Returns * Double.NaN if the designated subarray is empty. *

                              * Uses the formula

                                   *   Σ(weights[i]*(values[i] - mean)2)/(Σ(weights[i]) - 1)
                                   * 

                              *

                              * The formula used assumes that the supplied mean value is the weighted arithmetic * mean of the sample data, not a known population parameter. This method * is supplied only to save computation when the mean has already been * computed.

                              *

                              * This formula will not return the same result as the unweighted variance when all * weights are equal, unless all weights are equal to 1. The formula assumes that * weights are to be treated as "expansion values," as will be the case if for example * the weights represent frequency counts. To normalize weights so that the denominator * in the variance computation equals the length of the input vector minus one, use

                                   *   evaluate(values, MathArrays.normalizeArray(weights, values.length), mean); 
                                   * 
                              *

                              * Returns 0 for a single-value (i.e. length = 1) sample.

                              *

                              * Throws MathIllegalArgumentException if any of the following are true: *

                              • the values array is null
                              • *
                              • the weights array is null
                              • *
                              • the weights array does not have the same length as the values array
                              • *
                              • the weights array contains one or more infinite values
                              • *
                              • the weights array contains one or more NaN values
                              • *
                              • the weights array contains negative values
                              • *
                              • the start and length arguments do not determine a valid array
                              • *

                              *

                              * Does not change the internal state of the statistic.

                              * * @param values the input array * @param weights the weights array * @param mean the precomputed weighted mean value * @param begin index of the first array element to include * @param length the number of elements to include * @return the variance of the values or Double.NaN if length = 0 * @throws MathIllegalArgumentException if the parameters are not valid * @since 2.1 */ public double evaluate(final double[] values, final double[] weights, final double mean, final int begin, final int length) throws MathIllegalArgumentException { double var = Double.NaN; if (test(values, weights, begin, length)) { if (length == 1) { var = 0.0; } else if (length > 1) { double accum = 0.0; double dev = 0.0; double accum2 = 0.0; for (int i = begin; i < begin + length; i++) { dev = values[i] - mean; accum += weights[i] * (dev * dev); accum2 += weights[i] * dev; } double sumWts = 0; for (int i = begin; i < begin + length; i++) { sumWts += weights[i]; } if (isBiasCorrected) { var = (accum - (accum2 * accum2 / sumWts)) / (sumWts - 1.0); } else { var = (accum - (accum2 * accum2 / sumWts)) / sumWts; } } } return var; } /** *

                              Returns the weighted variance of the values in the input array, using * the precomputed weighted mean value.

                              *

                              * Uses the formula

                                   *   Σ(weights[i]*(values[i] - mean)2)/(Σ(weights[i]) - 1)
                                   * 

                              *

                              * The formula used assumes that the supplied mean value is the weighted arithmetic * mean of the sample data, not a known population parameter. This method * is supplied only to save computation when the mean has already been * computed.

                              *

                              * This formula will not return the same result as the unweighted variance when all * weights are equal, unless all weights are equal to 1. The formula assumes that * weights are to be treated as "expansion values," as will be the case if for example * the weights represent frequency counts. To normalize weights so that the denominator * in the variance computation equals the length of the input vector minus one, use

                                   *   evaluate(values, MathArrays.normalizeArray(weights, values.length), mean); 
                                   * 
                              *

                              * Returns 0 for a single-value (i.e. length = 1) sample.

                              *

                              * Throws MathIllegalArgumentException if any of the following are true: *

                              • the values array is null
                              • *
                              • the weights array is null
                              • *
                              • the weights array does not have the same length as the values array
                              • *
                              • the weights array contains one or more infinite values
                              • *
                              • the weights array contains one or more NaN values
                              • *
                              • the weights array contains negative values
                              • *

                              *

                              * Does not change the internal state of the statistic.

                              * * @param values the input array * @param weights the weights array * @param mean the precomputed weighted mean value * @return the variance of the values or Double.NaN if length = 0 * @throws MathIllegalArgumentException if the parameters are not valid * @since 2.1 */ public double evaluate(final double[] values, final double[] weights, final double mean) throws MathIllegalArgumentException { return evaluate(values, weights, mean, 0, values.length); } /** * @return Returns the isBiasCorrected. */ public boolean isBiasCorrected() { return isBiasCorrected; } /** * @param biasCorrected The isBiasCorrected to set. */ public void setBiasCorrected(boolean biasCorrected) { this.isBiasCorrected = biasCorrected; } /** * {@inheritDoc} */ @Override public Variance copy() { Variance result = new Variance(); // No try-catch or advertised exception because parameters are guaranteed non-null copy(this, result); return result; } /** * Copies source to dest. *

                              Neither source nor dest can be null.

                              * * @param source Variance to copy * @param dest Variance to copy to * @throws NullArgumentException if either source or dest is null */ public static void copy(Variance source, Variance dest) throws NullArgumentException { MathUtils.checkNotNull(source); MathUtils.checkNotNull(dest); dest.setData(source.getDataRef()); dest.moment = source.moment.copy(); dest.isBiasCorrected = source.isBiasCorrected; dest.incMoment = source.incMoment; } } ././@LongLink100644 0 0 147 12126630647 10264 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/stat/descriptive/moment/FourthMoment.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/stat/descriptive/moment/FourthMoment.ja100644 1750 1750 10645 12126627717 32313 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.stat.descriptive.moment; import java.io.Serializable; import org.apache.commons.math3.exception.NullArgumentException; import org.apache.commons.math3.util.MathUtils; /** * Computes a statistic related to the Fourth Central Moment. Specifically, * what is computed is the sum of *

                              * (x_i - xbar) ^ 4,

                              *

                              * where the x_i are the * sample observations and xbar is the sample mean.

                              *

                              * The following recursive updating formula is used:

                              *

                              * Let

                                *
                              • dev = (current obs - previous mean)
                              • *
                              • m2 = previous value of {@link SecondMoment}
                              • *
                              • m2 = previous value of {@link ThirdMoment}
                              • *
                              • n = number of observations (including current obs)
                              • *
                              * Then

                              *

                              * new value = old value - 4 * (dev/n) * m3 + 6 * (dev/n)^2 * m2 +
                              * [n^2 - 3 * (n-1)] * dev^4 * (n-1) / n^3

                              *

                              * Returns Double.NaN if no data values have been added and * returns 0 if there is just one value in the data set.

                              *

                              * Note that this implementation is not synchronized. If * multiple threads access an instance of this class concurrently, and at least * one of the threads invokes the increment() or * clear() method, it must be synchronized externally.

                              * * @version $Id: FourthMoment.java 1416643 2012-12-03 19:37:14Z tn $ */ class FourthMoment extends ThirdMoment implements Serializable{ /** Serializable version identifier */ private static final long serialVersionUID = 4763990447117157611L; /** fourth moment of values that have been added */ private double m4; /** * Create a FourthMoment instance */ public FourthMoment() { super(); m4 = Double.NaN; } /** * Copy constructor, creates a new {@code FourthMoment} identical * to the {@code original} * * @param original the {@code FourthMoment} instance to copy * @throws NullArgumentException if original is null */ public FourthMoment(FourthMoment original) throws NullArgumentException { super(); copy(original, this); } /** * {@inheritDoc} */ @Override public void increment(final double d) { if (n < 1) { m4 = 0.0; m3 = 0.0; m2 = 0.0; m1 = 0.0; } double prevM3 = m3; double prevM2 = m2; super.increment(d); double n0 = n; m4 = m4 - 4.0 * nDev * prevM3 + 6.0 * nDevSq * prevM2 + ((n0 * n0) - 3 * (n0 -1)) * (nDevSq * nDevSq * (n0 - 1) * n0); } /** * {@inheritDoc} */ @Override public double getResult() { return m4; } /** * {@inheritDoc} */ @Override public void clear() { super.clear(); m4 = Double.NaN; } /** * {@inheritDoc} */ @Override public FourthMoment copy() { FourthMoment result = new FourthMoment(); // No try-catch or advertised exception because args are guaranteed non-null copy(this, result); return result; } /** * Copies source to dest. *

                              Neither source nor dest can be null.

                              * * @param source FourthMoment to copy * @param dest FourthMoment to copy to * @throws NullArgumentException if either source or dest is null */ public static void copy(FourthMoment source, FourthMoment dest) throws NullArgumentException { MathUtils.checkNotNull(source); MathUtils.checkNotNull(dest); ThirdMoment.copy(source, dest); dest.m4 = source.m4; } } ././@LongLink100644 0 0 146 12126630647 10263 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/stat/descriptive/moment/ThirdMoment.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/stat/descriptive/moment/ThirdMoment.jav100644 1750 1750 10703 12126627717 32277 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.stat.descriptive.moment; import java.io.Serializable; import org.apache.commons.math3.exception.NullArgumentException; import org.apache.commons.math3.util.MathUtils; /** * Computes a statistic related to the Third Central Moment. Specifically, * what is computed is the sum of cubed deviations from the sample mean. *

                              * The following recursive updating formula is used:

                              *

                              * Let

                                *
                              • dev = (current obs - previous mean)
                              • *
                              • m2 = previous value of {@link SecondMoment}
                              • *
                              • n = number of observations (including current obs)
                              • *
                              * Then

                              *

                              * new value = old value - 3 * (dev/n) * m2 + (n-1) * (n -2) * (dev^3/n^2)

                              *

                              * Returns Double.NaN if no data values have been added and * returns 0 if there is just one value in the data set.

                              *

                              * Note that this implementation is not synchronized. If * multiple threads access an instance of this class concurrently, and at least * one of the threads invokes the increment() or * clear() method, it must be synchronized externally.

                              * * @version $Id: ThirdMoment.java 1416643 2012-12-03 19:37:14Z tn $ */ class ThirdMoment extends SecondMoment implements Serializable { /** Serializable version identifier */ private static final long serialVersionUID = -7818711964045118679L; /** third moment of values that have been added */ protected double m3; /** * Square of deviation of most recently added value from previous first * moment, normalized by previous sample size. Retained to prevent * repeated computation in higher order moments. nDevSq = nDev * nDev. */ protected double nDevSq; /** * Create a FourthMoment instance */ public ThirdMoment() { super(); m3 = Double.NaN; nDevSq = Double.NaN; } /** * Copy constructor, creates a new {@code ThirdMoment} identical * to the {@code original} * * @param original the {@code ThirdMoment} instance to copy * @throws NullArgumentException if orginal is null */ public ThirdMoment(ThirdMoment original) throws NullArgumentException { copy(original, this); } /** * {@inheritDoc} */ @Override public void increment(final double d) { if (n < 1) { m3 = m2 = m1 = 0.0; } double prevM2 = m2; super.increment(d); nDevSq = nDev * nDev; double n0 = n; m3 = m3 - 3.0 * nDev * prevM2 + (n0 - 1) * (n0 - 2) * nDevSq * dev; } /** * {@inheritDoc} */ @Override public double getResult() { return m3; } /** * {@inheritDoc} */ @Override public void clear() { super.clear(); m3 = Double.NaN; nDevSq = Double.NaN; } /** * {@inheritDoc} */ @Override public ThirdMoment copy() { ThirdMoment result = new ThirdMoment(); // No try-catch or advertised exception because args are guaranteed non-null copy(this, result); return result; } /** * Copies source to dest. *

                              Neither source nor dest can be null.

                              * * @param source ThirdMoment to copy * @param dest ThirdMoment to copy to * @throws NullArgumentException if either source or dest is null */ public static void copy(ThirdMoment source, ThirdMoment dest) throws NullArgumentException { MathUtils.checkNotNull(source); MathUtils.checkNotNull(dest); SecondMoment.copy(source, dest); dest.m3 = source.m3; dest.nDevSq = source.nDevSq; } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/stat/descriptive/moment/Mean.java100644 1750 1750 24660 12126627717 31075 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.stat.descriptive.moment; import java.io.Serializable; import org.apache.commons.math3.exception.MathIllegalArgumentException; import org.apache.commons.math3.exception.NullArgumentException; import org.apache.commons.math3.stat.descriptive.AbstractStorelessUnivariateStatistic; import org.apache.commons.math3.stat.descriptive.WeightedEvaluation; import org.apache.commons.math3.stat.descriptive.summary.Sum; import org.apache.commons.math3.util.MathUtils; /** *

                              Computes the arithmetic mean of a set of values. Uses the definitional * formula:

                              *

                              * mean = sum(x_i) / n *

                              *

                              where n is the number of observations. *

                              *

                              When {@link #increment(double)} is used to add data incrementally from a * stream of (unstored) values, the value of the statistic that * {@link #getResult()} returns is computed using the following recursive * updating algorithm:

                              *
                                *
                              1. Initialize m = the first value
                              2. *
                              3. For each additional value, update using
                                * m = m + (new value - m) / (number of observations)
                              4. *
                              *

                              If {@link #evaluate(double[])} is used to compute the mean of an array * of stored values, a two-pass, corrected algorithm is used, starting with * the definitional formula computed using the array of stored values and then * correcting this by adding the mean deviation of the data values from the * arithmetic mean. See, e.g. "Comparison of Several Algorithms for Computing * Sample Means and Variances," Robert F. Ling, Journal of the American * Statistical Association, Vol. 69, No. 348 (Dec., 1974), pp. 859-866.

                              *

                              * Returns Double.NaN if the dataset is empty. *

                              * Note that this implementation is not synchronized. If * multiple threads access an instance of this class concurrently, and at least * one of the threads invokes the increment() or * clear() method, it must be synchronized externally. * * @version $Id: Mean.java 1416643 2012-12-03 19:37:14Z tn $ */ public class Mean extends AbstractStorelessUnivariateStatistic implements Serializable, WeightedEvaluation { /** Serializable version identifier */ private static final long serialVersionUID = -1296043746617791564L; /** First moment on which this statistic is based. */ protected FirstMoment moment; /** * Determines whether or not this statistic can be incremented or cleared. *

                              * Statistics based on (constructed from) external moments cannot * be incremented or cleared.

                              */ protected boolean incMoment; /** Constructs a Mean. */ public Mean() { incMoment = true; moment = new FirstMoment(); } /** * Constructs a Mean with an External Moment. * * @param m1 the moment */ public Mean(final FirstMoment m1) { this.moment = m1; incMoment = false; } /** * Copy constructor, creates a new {@code Mean} identical * to the {@code original} * * @param original the {@code Mean} instance to copy * @throws NullArgumentException if original is null */ public Mean(Mean original) throws NullArgumentException { copy(original, this); } /** * {@inheritDoc} *

                              Note that when {@link #Mean(FirstMoment)} is used to * create a Mean, this method does nothing. In that case, the * FirstMoment should be incremented directly.

                              */ @Override public void increment(final double d) { if (incMoment) { moment.increment(d); } } /** * {@inheritDoc} */ @Override public void clear() { if (incMoment) { moment.clear(); } } /** * {@inheritDoc} */ @Override public double getResult() { return moment.m1; } /** * {@inheritDoc} */ public long getN() { return moment.getN(); } /** * Returns the arithmetic mean of the entries in the specified portion of * the input array, or Double.NaN if the designated subarray * is empty. *

                              * Throws IllegalArgumentException if the array is null.

                              *

                              * See {@link Mean} for details on the computing algorithm.

                              * * @param values the input array * @param begin index of the first array element to include * @param length the number of elements to include * @return the mean of the values or Double.NaN if length = 0 * @throws MathIllegalArgumentException if the array is null or the array index * parameters are not valid */ @Override public double evaluate(final double[] values,final int begin, final int length) throws MathIllegalArgumentException { if (test(values, begin, length)) { Sum sum = new Sum(); double sampleSize = length; // Compute initial estimate using definitional formula double xbar = sum.evaluate(values, begin, length) / sampleSize; // Compute correction factor in second pass double correction = 0; for (int i = begin; i < begin + length; i++) { correction += values[i] - xbar; } return xbar + (correction/sampleSize); } return Double.NaN; } /** * Returns the weighted arithmetic mean of the entries in the specified portion of * the input array, or Double.NaN if the designated subarray * is empty. *

                              * Throws IllegalArgumentException if either array is null.

                              *

                              * See {@link Mean} for details on the computing algorithm. The two-pass algorithm * described above is used here, with weights applied in computing both the original * estimate and the correction factor.

                              *

                              * Throws IllegalArgumentException if any of the following are true: *

                              • the values array is null
                              • *
                              • the weights array is null
                              • *
                              • the weights array does not have the same length as the values array
                              • *
                              • the weights array contains one or more infinite values
                              • *
                              • the weights array contains one or more NaN values
                              • *
                              • the weights array contains negative values
                              • *
                              • the start and length arguments do not determine a valid array
                              • *

                              * * @param values the input array * @param weights the weights array * @param begin index of the first array element to include * @param length the number of elements to include * @return the mean of the values or Double.NaN if length = 0 * @throws MathIllegalArgumentException if the parameters are not valid * @since 2.1 */ public double evaluate(final double[] values, final double[] weights, final int begin, final int length) throws MathIllegalArgumentException { if (test(values, weights, begin, length)) { Sum sum = new Sum(); // Compute initial estimate using definitional formula double sumw = sum.evaluate(weights,begin,length); double xbarw = sum.evaluate(values, weights, begin, length) / sumw; // Compute correction factor in second pass double correction = 0; for (int i = begin; i < begin + length; i++) { correction += weights[i] * (values[i] - xbarw); } return xbarw + (correction/sumw); } return Double.NaN; } /** * Returns the weighted arithmetic mean of the entries in the input array. *

                              * Throws MathIllegalArgumentException if either array is null.

                              *

                              * See {@link Mean} for details on the computing algorithm. The two-pass algorithm * described above is used here, with weights applied in computing both the original * estimate and the correction factor.

                              *

                              * Throws MathIllegalArgumentException if any of the following are true: *

                              • the values array is null
                              • *
                              • the weights array is null
                              • *
                              • the weights array does not have the same length as the values array
                              • *
                              • the weights array contains one or more infinite values
                              • *
                              • the weights array contains one or more NaN values
                              • *
                              • the weights array contains negative values
                              • *

                              * * @param values the input array * @param weights the weights array * @return the mean of the values or Double.NaN if length = 0 * @throws MathIllegalArgumentException if the parameters are not valid * @since 2.1 */ public double evaluate(final double[] values, final double[] weights) throws MathIllegalArgumentException { return evaluate(values, weights, 0, values.length); } /** * {@inheritDoc} */ @Override public Mean copy() { Mean result = new Mean(); // No try-catch or advertised exception because args are guaranteed non-null copy(this, result); return result; } /** * Copies source to dest. *

                              Neither source nor dest can be null.

                              * * @param source Mean to copy * @param dest Mean to copy to * @throws NullArgumentException if either source or dest is null */ public static void copy(Mean source, Mean dest) throws NullArgumentException { MathUtils.checkNotNull(source); MathUtils.checkNotNull(dest); dest.setData(source.getDataRef()); dest.incMoment = source.incMoment; dest.moment = source.moment.copy(); } } ././@LongLink100644 0 0 156 12126630647 10264 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/stat/descriptive/AggregateSummaryStatistics.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/stat/descriptive/AggregateSummaryStatis100644 1750 1750 36073 12126627717 32433 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.stat.descriptive; import java.io.Serializable; import java.util.Collection; import java.util.Iterator; import org.apache.commons.math3.exception.NullArgumentException; /** *

                              * An aggregator for {@code SummaryStatistics} from several data sets or * data set partitions. In its simplest usage mode, the client creates an * instance via the zero-argument constructor, then uses * {@link #createContributingStatistics()} to obtain a {@code SummaryStatistics} * for each individual data set / partition. The per-set statistics objects * are used as normal, and at any time the aggregate statistics for all the * contributors can be obtained from this object. *

                              * Clients with specialized requirements can use alternative constructors to * control the statistics implementations and initial values used by the * contributing and the internal aggregate {@code SummaryStatistics} objects. *

                              * A static {@link #aggregate(Collection)} method is also included that computes * aggregate statistics directly from a Collection of SummaryStatistics instances. *

                              * When {@link #createContributingStatistics()} is used to create SummaryStatistics * instances to be aggregated concurrently, the created instances' * {@link SummaryStatistics#addValue(double)} methods must synchronize on the aggregating * instance maintained by this class. In multithreaded environments, if the functionality * provided by {@link #aggregate(Collection)} is adequate, that method should be used * to avoid unnecessary computation and synchronization delays.

                              * * @since 2.0 * @version $Id: AggregateSummaryStatistics.java 1416643 2012-12-03 19:37:14Z tn $ * */ public class AggregateSummaryStatistics implements StatisticalSummary, Serializable { /** Serializable version identifier */ private static final long serialVersionUID = -8207112444016386906L; /** * A SummaryStatistics serving as a prototype for creating SummaryStatistics * contributing to this aggregate */ private final SummaryStatistics statisticsPrototype; /** * The SummaryStatistics in which aggregate statistics are accumulated. */ private final SummaryStatistics statistics; /** * Initializes a new AggregateSummaryStatistics with default statistics * implementations. * */ public AggregateSummaryStatistics() { // No try-catch or throws NAE because arg is guaranteed non-null this(new SummaryStatistics()); } /** * Initializes a new AggregateSummaryStatistics with the specified statistics * object as a prototype for contributing statistics and for the internal * aggregate statistics. This provides for customized statistics implementations * to be used by contributing and aggregate statistics. * * @param prototypeStatistics a {@code SummaryStatistics} serving as a * prototype both for the internal aggregate statistics and for * contributing statistics obtained via the * {@code createContributingStatistics()} method. Being a prototype * means that other objects are initialized by copying this object's state. * If {@code null}, a new, default statistics object is used. Any statistic * values in the prototype are propagated to contributing statistics * objects and (once) into these aggregate statistics. * @throws NullArgumentException if prototypeStatistics is null * @see #createContributingStatistics() */ public AggregateSummaryStatistics(SummaryStatistics prototypeStatistics) throws NullArgumentException { this(prototypeStatistics, prototypeStatistics == null ? null : new SummaryStatistics(prototypeStatistics)); } /** * Initializes a new AggregateSummaryStatistics with the specified statistics * object as a prototype for contributing statistics and for the internal * aggregate statistics. This provides for different statistics implementations * to be used by contributing and aggregate statistics and for an initial * state to be supplied for the aggregate statistics. * * @param prototypeStatistics a {@code SummaryStatistics} serving as a * prototype both for the internal aggregate statistics and for * contributing statistics obtained via the * {@code createContributingStatistics()} method. Being a prototype * means that other objects are initialized by copying this object's state. * If {@code null}, a new, default statistics object is used. Any statistic * values in the prototype are propagated to contributing statistics * objects, but not into these aggregate statistics. * @param initialStatistics a {@code SummaryStatistics} to serve as the * internal aggregate statistics object. If {@code null}, a new, default * statistics object is used. * @see #createContributingStatistics() */ public AggregateSummaryStatistics(SummaryStatistics prototypeStatistics, SummaryStatistics initialStatistics) { this.statisticsPrototype = (prototypeStatistics == null) ? new SummaryStatistics() : prototypeStatistics; this.statistics = (initialStatistics == null) ? new SummaryStatistics() : initialStatistics; } /** * {@inheritDoc}. This version returns the maximum over all the aggregated * data. * * @see StatisticalSummary#getMax() */ public double getMax() { synchronized (statistics) { return statistics.getMax(); } } /** * {@inheritDoc}. This version returns the mean of all the aggregated data. * * @see StatisticalSummary#getMean() */ public double getMean() { synchronized (statistics) { return statistics.getMean(); } } /** * {@inheritDoc}. This version returns the minimum over all the aggregated * data. * * @see StatisticalSummary#getMin() */ public double getMin() { synchronized (statistics) { return statistics.getMin(); } } /** * {@inheritDoc}. This version returns a count of all the aggregated data. * * @see StatisticalSummary#getN() */ public long getN() { synchronized (statistics) { return statistics.getN(); } } /** * {@inheritDoc}. This version returns the standard deviation of all the * aggregated data. * * @see StatisticalSummary#getStandardDeviation() */ public double getStandardDeviation() { synchronized (statistics) { return statistics.getStandardDeviation(); } } /** * {@inheritDoc}. This version returns a sum of all the aggregated data. * * @see StatisticalSummary#getSum() */ public double getSum() { synchronized (statistics) { return statistics.getSum(); } } /** * {@inheritDoc}. This version returns the variance of all the aggregated * data. * * @see StatisticalSummary#getVariance() */ public double getVariance() { synchronized (statistics) { return statistics.getVariance(); } } /** * Returns the sum of the logs of all the aggregated data. * * @return the sum of logs * @see SummaryStatistics#getSumOfLogs() */ public double getSumOfLogs() { synchronized (statistics) { return statistics.getSumOfLogs(); } } /** * Returns the geometric mean of all the aggregated data. * * @return the geometric mean * @see SummaryStatistics#getGeometricMean() */ public double getGeometricMean() { synchronized (statistics) { return statistics.getGeometricMean(); } } /** * Returns the sum of the squares of all the aggregated data. * * @return The sum of squares * @see SummaryStatistics#getSumsq() */ public double getSumsq() { synchronized (statistics) { return statistics.getSumsq(); } } /** * Returns a statistic related to the Second Central Moment. Specifically, * what is returned is the sum of squared deviations from the sample mean * among the all of the aggregated data. * * @return second central moment statistic * @see SummaryStatistics#getSecondMoment() */ public double getSecondMoment() { synchronized (statistics) { return statistics.getSecondMoment(); } } /** * Return a {@link StatisticalSummaryValues} instance reporting current * aggregate statistics. * * @return Current values of aggregate statistics */ public StatisticalSummary getSummary() { synchronized (statistics) { return new StatisticalSummaryValues(getMean(), getVariance(), getN(), getMax(), getMin(), getSum()); } } /** * Creates and returns a {@code SummaryStatistics} whose data will be * aggregated with those of this {@code AggregateSummaryStatistics}. * * @return a {@code SummaryStatistics} whose data will be aggregated with * those of this {@code AggregateSummaryStatistics}. The initial state * is a copy of the configured prototype statistics. */ public SummaryStatistics createContributingStatistics() { SummaryStatistics contributingStatistics = new AggregatingSummaryStatistics(statistics); // No try - catch or advertising NAE because neither argument will ever be null SummaryStatistics.copy(statisticsPrototype, contributingStatistics); return contributingStatistics; } /** * Computes aggregate summary statistics. This method can be used to combine statistics * computed over partitions or subsamples - i.e., the StatisticalSummaryValues returned * should contain the same values that would have been obtained by computing a single * StatisticalSummary over the combined dataset. *

                              * Returns null if the collection is empty or null. *

                              * * @param statistics collection of SummaryStatistics to aggregate * @return summary statistics for the combined dataset */ public static StatisticalSummaryValues aggregate(Collection statistics) { if (statistics == null) { return null; } Iterator iterator = statistics.iterator(); if (!iterator.hasNext()) { return null; } SummaryStatistics current = iterator.next(); long n = current.getN(); double min = current.getMin(); double sum = current.getSum(); double max = current.getMax(); double m2 = current.getSecondMoment(); double mean = current.getMean(); while (iterator.hasNext()) { current = iterator.next(); if (current.getMin() < min || Double.isNaN(min)) { min = current.getMin(); } if (current.getMax() > max || Double.isNaN(max)) { max = current.getMax(); } sum += current.getSum(); final double oldN = n; final double curN = current.getN(); n += curN; final double meanDiff = current.getMean() - mean; mean = sum / n; m2 = m2 + current.getSecondMoment() + meanDiff * meanDiff * oldN * curN / n; } final double variance; if (n == 0) { variance = Double.NaN; } else if (n == 1) { variance = 0d; } else { variance = m2 / (n - 1); } return new StatisticalSummaryValues(mean, variance, n, max, min, sum); } /** * A SummaryStatistics that also forwards all values added to it to a second * {@code SummaryStatistics} for aggregation. * * @since 2.0 */ private static class AggregatingSummaryStatistics extends SummaryStatistics { /** * The serialization version of this class */ private static final long serialVersionUID = 1L; /** * An additional SummaryStatistics into which values added to these * statistics (and possibly others) are aggregated */ private final SummaryStatistics aggregateStatistics; /** * Initializes a new AggregatingSummaryStatistics with the specified * aggregate statistics object * * @param aggregateStatistics a {@code SummaryStatistics} into which * values added to this statistics object should be aggregated */ public AggregatingSummaryStatistics(SummaryStatistics aggregateStatistics) { this.aggregateStatistics = aggregateStatistics; } /** * {@inheritDoc}. This version adds the provided value to the configured * aggregate after adding it to these statistics. * * @see SummaryStatistics#addValue(double) */ @Override public void addValue(double value) { super.addValue(value); synchronized (aggregateStatistics) { aggregateStatistics.addValue(value); } } /** * Returns true iff object is a * SummaryStatistics instance and all statistics have the * same values as this. * @param object the object to test equality against. * @return true if object equals this */ @Override public boolean equals(Object object) { if (object == this) { return true; } if (object instanceof AggregatingSummaryStatistics == false) { return false; } AggregatingSummaryStatistics stat = (AggregatingSummaryStatistics)object; return super.equals(stat) && aggregateStatistics.equals(stat.aggregateStatistics); } /** * Returns hash code based on values of statistics * @return hash code */ @Override public int hashCode() { return 123 + super.hashCode() + aggregateStatistics.hashCode(); } } } ././@LongLink100644 0 0 154 12126630647 10262 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/stat/descriptive/StatisticalSummaryValues.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/stat/descriptive/StatisticalSummaryValu100644 1750 1750 13103 12126627717 32456 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.stat.descriptive; import java.io.Serializable; import org.apache.commons.math3.util.FastMath; import org.apache.commons.math3.util.MathUtils; import org.apache.commons.math3.util.Precision; /** * Value object representing the results of a univariate statistical summary. * * @version $Id: StatisticalSummaryValues.java 1416643 2012-12-03 19:37:14Z tn $ */ public class StatisticalSummaryValues implements Serializable, StatisticalSummary { /** Serialization id */ private static final long serialVersionUID = -5108854841843722536L; /** The sample mean */ private final double mean; /** The sample variance */ private final double variance; /** The number of observations in the sample */ private final long n; /** The maximum value */ private final double max; /** The minimum value */ private final double min; /** The sum of the sample values */ private final double sum; /** * Constructor * * @param mean the sample mean * @param variance the sample variance * @param n the number of observations in the sample * @param max the maximum value * @param min the minimum value * @param sum the sum of the values */ public StatisticalSummaryValues(double mean, double variance, long n, double max, double min, double sum) { super(); this.mean = mean; this.variance = variance; this.n = n; this.max = max; this.min = min; this.sum = sum; } /** * @return Returns the max. */ public double getMax() { return max; } /** * @return Returns the mean. */ public double getMean() { return mean; } /** * @return Returns the min. */ public double getMin() { return min; } /** * @return Returns the number of values. */ public long getN() { return n; } /** * @return Returns the sum. */ public double getSum() { return sum; } /** * @return Returns the standard deviation */ public double getStandardDeviation() { return FastMath.sqrt(variance); } /** * @return Returns the variance. */ public double getVariance() { return variance; } /** * Returns true iff object is a * StatisticalSummaryValues instance and all statistics have * the same values as this. * * @param object the object to test equality against. * @return true if object equals this */ @Override public boolean equals(Object object) { if (object == this ) { return true; } if (object instanceof StatisticalSummaryValues == false) { return false; } StatisticalSummaryValues stat = (StatisticalSummaryValues) object; return Precision.equalsIncludingNaN(stat.getMax(), getMax()) && Precision.equalsIncludingNaN(stat.getMean(), getMean()) && Precision.equalsIncludingNaN(stat.getMin(), getMin()) && Precision.equalsIncludingNaN(stat.getN(), getN()) && Precision.equalsIncludingNaN(stat.getSum(), getSum()) && Precision.equalsIncludingNaN(stat.getVariance(), getVariance()); } /** * Returns hash code based on values of statistics * * @return hash code */ @Override public int hashCode() { int result = 31 + MathUtils.hash(getMax()); result = result * 31 + MathUtils.hash(getMean()); result = result * 31 + MathUtils.hash(getMin()); result = result * 31 + MathUtils.hash(getN()); result = result * 31 + MathUtils.hash(getSum()); result = result * 31 + MathUtils.hash(getVariance()); return result; } /** * Generates a text report displaying values of statistics. * Each statistic is displayed on a separate line. * * @return String with line feeds displaying statistics */ @Override public String toString() { StringBuffer outBuffer = new StringBuffer(); String endl = "\n"; outBuffer.append("StatisticalSummaryValues:").append(endl); outBuffer.append("n: ").append(getN()).append(endl); outBuffer.append("min: ").append(getMin()).append(endl); outBuffer.append("max: ").append(getMax()).append(endl); outBuffer.append("mean: ").append(getMean()).append(endl); outBuffer.append("std dev: ").append(getStandardDeviation()) .append(endl); outBuffer.append("variance: ").append(getVariance()).append(endl); outBuffer.append("sum: ").append(getSum()).append(endl); return outBuffer.toString(); } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/stat/correlation/package-info.java100644 1750 1750 1624 12126627717 31215 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ /** * * Correlations/Covariance computations. * */ package org.apache.commons.math3.stat.correlation; ././@LongLink100644 0 0 160 12126630647 10257 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/stat/correlation/StorelessBivariateCovariance.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/stat/correlation/StorelessBivariateCova100644 1750 1750 7721 12126627717 32400 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.stat.correlation; import org.apache.commons.math3.exception.NumberIsTooSmallException; import org.apache.commons.math3.exception.util.LocalizedFormats; /** * Bivariate Covariance implementation that does not require input data to be * stored in memory. * *

                              This class is based on a paper written by Philippe Pébay: * * Formulas for Robust, One-Pass Parallel Computation of Covariances and * Arbitrary-Order Statistical Moments, 2008, Technical Report SAND2008-6212, * Sandia National Laboratories. It computes the covariance for a pair of variables. * Use {@link StorelessCovariance} to estimate an entire covariance matrix.

                              * *

                              Note: This class is package private as it is only used internally in * the {@link StorelessCovariance} class.

                              * * @version $Id: StorelessBivariateCovariance.java 1410238 2012-11-16 07:58:49Z luc $ * @since 3.0 */ class StorelessBivariateCovariance { /** the mean of variable x */ private double meanX; /** the mean of variable y */ private double meanY; /** number of observations */ private double n; /** the running covariance estimate */ private double covarianceNumerator; /** flag for bias correction */ private boolean biasCorrected; /** * Create an empty {@link StorelessBivariateCovariance} instance with * bias correction. */ public StorelessBivariateCovariance() { this(true); } /** * Create an empty {@link StorelessBivariateCovariance} instance. * * @param biasCorrection if true the covariance estimate is corrected * for bias, i.e. n-1 in the denominator, otherwise there is no bias correction, * i.e. n in the denominator. */ public StorelessBivariateCovariance(final boolean biasCorrection) { meanX = meanY = 0.0; n = 0; covarianceNumerator = 0.0; biasCorrected = biasCorrection; } /** * Update the covariance estimation with a pair of variables (x, y). * * @param x the x value * @param y the y value */ public void increment(final double x, final double y) { n++; final double deltaX = x - meanX; final double deltaY = y - meanY; meanX += deltaX / n; meanY += deltaY / n; covarianceNumerator += ((n - 1.0) / n) * deltaX * deltaY; } /** * Returns the number of observations. * * @return number of observations */ public double getN() { return n; } /** * Return the current covariance estimate. * * @return the current covariance * @throws NumberIsTooSmallException if the number of observations * is < 2 */ public double getResult() throws NumberIsTooSmallException { if (n < 2) { throw new NumberIsTooSmallException(LocalizedFormats.INSUFFICIENT_DIMENSION, n, 2, true); } if (biasCorrected) { return covarianceNumerator / (n - 1d); } else { return covarianceNumerator / n; } } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/stat/correlation/Covariance.java100644 1750 1750 27373 12126627717 30774 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.stat.correlation; import org.apache.commons.math3.exception.MathIllegalArgumentException; import org.apache.commons.math3.exception.NotStrictlyPositiveException; import org.apache.commons.math3.exception.util.LocalizedFormats; import org.apache.commons.math3.linear.RealMatrix; import org.apache.commons.math3.linear.BlockRealMatrix; import org.apache.commons.math3.stat.descriptive.moment.Mean; import org.apache.commons.math3.stat.descriptive.moment.Variance; /** * Computes covariances for pairs of arrays or columns of a matrix. * *

                              The constructors that take RealMatrix or * double[][] arguments generate covariance matrices. The * columns of the input matrices are assumed to represent variable values.

                              * *

                              The constructor argument biasCorrected determines whether or * not computed covariances are bias-corrected.

                              * *

                              Unbiased covariances are given by the formula

                              * cov(X, Y) = Σ[(xi - E(X))(yi - E(Y))] / (n - 1) * where E(X) is the mean of X and E(Y) * is the mean of the Y values. * *

                              Non-bias-corrected estimates use n in place of n - 1 * * @version $Id: Covariance.java 1453271 2013-03-06 10:29:51Z luc $ * @since 2.0 */ public class Covariance { /** covariance matrix */ private final RealMatrix covarianceMatrix; /** * Create an empty covariance matrix. */ /** Number of observations (length of covariate vectors) */ private final int n; /** * Create a Covariance with no data */ public Covariance() { super(); covarianceMatrix = null; n = 0; } /** * Create a Covariance matrix from a rectangular array * whose columns represent covariates. * *

                              The biasCorrected parameter determines whether or not * covariance estimates are bias-corrected.

                              * *

                              The input array must be rectangular with at least one column * and two rows.

                              * * @param data rectangular array with columns representing covariates * @param biasCorrected true means covariances are bias-corrected * @throws MathIllegalArgumentException if the input data array is not * rectangular with at least two rows and one column. * @throws NotStrictlyPositiveException if the input data array is not * rectangular with at least one row and one column. */ public Covariance(double[][] data, boolean biasCorrected) throws MathIllegalArgumentException, NotStrictlyPositiveException { this(new BlockRealMatrix(data), biasCorrected); } /** * Create a Covariance matrix from a rectangular array * whose columns represent covariates. * *

                              The input array must be rectangular with at least one column * and two rows

                              * * @param data rectangular array with columns representing covariates * @throws MathIllegalArgumentException if the input data array is not * rectangular with at least two rows and one column. * @throws NotStrictlyPositiveException if the input data array is not * rectangular with at least one row and one column. */ public Covariance(double[][] data) throws MathIllegalArgumentException, NotStrictlyPositiveException { this(data, true); } /** * Create a covariance matrix from a matrix whose columns * represent covariates. * *

                              The biasCorrected parameter determines whether or not * covariance estimates are bias-corrected.

                              * *

                              The matrix must have at least one column and two rows

                              * * @param matrix matrix with columns representing covariates * @param biasCorrected true means covariances are bias-corrected * @throws MathIllegalArgumentException if the input matrix does not have * at least two rows and one column */ public Covariance(RealMatrix matrix, boolean biasCorrected) throws MathIllegalArgumentException { checkSufficientData(matrix); n = matrix.getRowDimension(); covarianceMatrix = computeCovarianceMatrix(matrix, biasCorrected); } /** * Create a covariance matrix from a matrix whose columns * represent covariates. * *

                              The matrix must have at least one column and two rows

                              * * @param matrix matrix with columns representing covariates * @throws MathIllegalArgumentException if the input matrix does not have * at least two rows and one column */ public Covariance(RealMatrix matrix) throws MathIllegalArgumentException { this(matrix, true); } /** * Returns the covariance matrix * * @return covariance matrix */ public RealMatrix getCovarianceMatrix() { return covarianceMatrix; } /** * Returns the number of observations (length of covariate vectors) * * @return number of observations */ public int getN() { return n; } /** * Compute a covariance matrix from a matrix whose columns represent * covariates. * @param matrix input matrix (must have at least one column and two rows) * @param biasCorrected determines whether or not covariance estimates are bias-corrected * @return covariance matrix * @throws MathIllegalArgumentException if the matrix does not contain sufficient data */ protected RealMatrix computeCovarianceMatrix(RealMatrix matrix, boolean biasCorrected) throws MathIllegalArgumentException { int dimension = matrix.getColumnDimension(); Variance variance = new Variance(biasCorrected); RealMatrix outMatrix = new BlockRealMatrix(dimension, dimension); for (int i = 0; i < dimension; i++) { for (int j = 0; j < i; j++) { double cov = covariance(matrix.getColumn(i), matrix.getColumn(j), biasCorrected); outMatrix.setEntry(i, j, cov); outMatrix.setEntry(j, i, cov); } outMatrix.setEntry(i, i, variance.evaluate(matrix.getColumn(i))); } return outMatrix; } /** * Create a covariance matrix from a matrix whose columns represent * covariates. Covariances are computed using the bias-corrected formula. * @param matrix input matrix (must have at least one column and two rows) * @return covariance matrix * @throws MathIllegalArgumentException if matrix does not contain sufficient data * @see #Covariance */ protected RealMatrix computeCovarianceMatrix(RealMatrix matrix) throws MathIllegalArgumentException { return computeCovarianceMatrix(matrix, true); } /** * Compute a covariance matrix from a rectangular array whose columns represent * covariates. * @param data input array (must have at least one column and two rows) * @param biasCorrected determines whether or not covariance estimates are bias-corrected * @return covariance matrix * @throws MathIllegalArgumentException if the data array does not contain sufficient * data * @throws NotStrictlyPositiveException if the input data array is not * rectangular with at least one row and one column. */ protected RealMatrix computeCovarianceMatrix(double[][] data, boolean biasCorrected) throws MathIllegalArgumentException, NotStrictlyPositiveException { return computeCovarianceMatrix(new BlockRealMatrix(data), biasCorrected); } /** * Create a covariance matrix from a rectangular array whose columns represent * covariates. Covariances are computed using the bias-corrected formula. * @param data input array (must have at least one column and two rows) * @return covariance matrix * @throws MathIllegalArgumentException if the data array does not contain sufficient data * @throws NotStrictlyPositiveException if the input data array is not * rectangular with at least one row and one column. * @see #Covariance */ protected RealMatrix computeCovarianceMatrix(double[][] data) throws MathIllegalArgumentException, NotStrictlyPositiveException { return computeCovarianceMatrix(data, true); } /** * Computes the covariance between the two arrays. * *

                              Array lengths must match and the common length must be at least 2.

                              * * @param xArray first data array * @param yArray second data array * @param biasCorrected if true, returned value will be bias-corrected * @return returns the covariance for the two arrays * @throws MathIllegalArgumentException if the arrays lengths do not match or * there is insufficient data */ public double covariance(final double[] xArray, final double[] yArray, boolean biasCorrected) throws MathIllegalArgumentException { Mean mean = new Mean(); double result = 0d; int length = xArray.length; if (length != yArray.length) { throw new MathIllegalArgumentException( LocalizedFormats.DIMENSIONS_MISMATCH_SIMPLE, length, yArray.length); } else if (length < 2) { throw new MathIllegalArgumentException( LocalizedFormats.INSUFFICIENT_OBSERVED_POINTS_IN_SAMPLE, length, 2); } else { double xMean = mean.evaluate(xArray); double yMean = mean.evaluate(yArray); for (int i = 0; i < length; i++) { double xDev = xArray[i] - xMean; double yDev = yArray[i] - yMean; result += (xDev * yDev - result) / (i + 1); } } return biasCorrected ? result * ((double) length / (double)(length - 1)) : result; } /** * Computes the covariance between the two arrays, using the bias-corrected * formula. * *

                              Array lengths must match and the common length must be at least 2.

                              * * @param xArray first data array * @param yArray second data array * @return returns the covariance for the two arrays * @throws MathIllegalArgumentException if the arrays lengths do not match or * there is insufficient data */ public double covariance(final double[] xArray, final double[] yArray) throws MathIllegalArgumentException { return covariance(xArray, yArray, true); } /** * Throws MathIllegalArgumentException if the matrix does not have at least * one column and two rows. * @param matrix matrix to check * @throws MathIllegalArgumentException if the matrix does not contain sufficient data * to compute covariance */ private void checkSufficientData(final RealMatrix matrix) throws MathIllegalArgumentException { int nRows = matrix.getRowDimension(); int nCols = matrix.getColumnDimension(); if (nRows < 2 || nCols < 1) { throw new MathIllegalArgumentException( LocalizedFormats.INSUFFICIENT_ROWS_AND_COLUMNS, nRows, nCols); } } } ././@LongLink100644 0 0 147 12126630647 10264 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/stat/correlation/StorelessCovariance.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/stat/correlation/StorelessCovariance.ja100644 1750 1750 17122 12126627717 32340 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.stat.correlation; import org.apache.commons.math3.exception.DimensionMismatchException; import org.apache.commons.math3.exception.MathUnsupportedOperationException; import org.apache.commons.math3.exception.NumberIsTooSmallException; import org.apache.commons.math3.linear.MatrixUtils; import org.apache.commons.math3.linear.RealMatrix; /** * Covariance implementation that does not require input data to be * stored in memory. The size of the covariance matrix is specified in the * constructor. Specific elements of the matrix are incrementally updated with * calls to incrementRow() or increment Covariance(). * *

                              This class is based on a paper written by Philippe Pébay: * * Formulas for Robust, One-Pass Parallel Computation of Covariances and * Arbitrary-Order Statistical Moments, 2008, Technical Report SAND2008-6212, * Sandia National Laboratories.

                              * *

                              Note: the underlying covariance matrix is symmetric, thus only the * upper triangular part of the matrix is stored and updated each increment.

                              * * @version $Id: StorelessCovariance.java 1410238 2012-11-16 07:58:49Z luc $ * @since 3.0 */ public class StorelessCovariance extends Covariance { /** the square covariance matrix (upper triangular part) */ private StorelessBivariateCovariance[] covMatrix; /** dimension of the square covariance matrix */ private int dimension; /** * Create a bias corrected covariance matrix with a given dimension. * * @param dim the dimension of the square covariance matrix */ public StorelessCovariance(final int dim) { this(dim, true); } /** * Create a covariance matrix with a given number of rows and columns and the * indicated bias correction. * * @param dim the dimension of the covariance matrix * @param biasCorrected if true the covariance estimate is corrected * for bias, i.e. n-1 in the denominator, otherwise there is no bias correction, * i.e. n in the denominator. */ public StorelessCovariance(final int dim, final boolean biasCorrected) { dimension = dim; covMatrix = new StorelessBivariateCovariance[dimension * (dimension + 1) / 2]; initializeMatrix(biasCorrected); } /** * Initialize the internal two-dimensional array of * {@link StorelessBivariateCovariance} instances. * * @param biasCorrected if the covariance estimate shall be corrected for bias */ private void initializeMatrix(final boolean biasCorrected) { for(int i = 0; i < dimension; i++){ for(int j = 0; j < dimension; j++){ setElement(i, j, new StorelessBivariateCovariance(biasCorrected)); } } } /** * Returns the index (i, j) translated into the one-dimensional * array used to store the upper triangular part of the symmetric * covariance matrix. * * @param i the row index * @param j the column index * @return the corresponding index in the matrix array */ private int indexOf(final int i, final int j) { return j < i ? i * (i + 1) / 2 + j : j * (j + 1) / 2 + i; } /** * Gets the element at index (i, j) from the covariance matrix * @param i the row index * @param j the column index * @return the {@link StorelessBivariateCovariance} element at the given index */ private StorelessBivariateCovariance getElement(final int i, final int j) { return covMatrix[indexOf(i, j)]; } /** * Sets the covariance element at index (i, j) in the covariance matrix * @param i the row index * @param j the column index * @param cov the {@link StorelessBivariateCovariance} element to be set */ private void setElement(final int i, final int j, final StorelessBivariateCovariance cov) { covMatrix[indexOf(i, j)] = cov; } /** * Get the covariance for an individual element of the covariance matrix. * * @param xIndex row index in the covariance matrix * @param yIndex column index in the covariance matrix * @return the covariance of the given element * @throws NumberIsTooSmallException if the number of observations * in the cell is < 2 */ public double getCovariance(final int xIndex, final int yIndex) throws NumberIsTooSmallException { return getElement(xIndex, yIndex).getResult(); } /** * Increment the covariance matrix with one row of data. * * @param data array representing one row of data. * @throws DimensionMismatchException if the length of rowData * does not match with the covariance matrix */ public void increment(final double[] data) throws DimensionMismatchException { int length = data.length; if (length != dimension) { throw new DimensionMismatchException(length, dimension); } // only update the upper triangular part of the covariance matrix // as only these parts are actually stored for (int i = 0; i < length; i++){ for (int j = i; j < length; j++){ getElement(i, j).increment(data[i], data[j]); } } } /** * {@inheritDoc} * @throws NumberIsTooSmallException if the number of observations * in a cell is < 2 */ @Override public RealMatrix getCovarianceMatrix() throws NumberIsTooSmallException { return MatrixUtils.createRealMatrix(getData()); } /** * Return the covariance matrix as two-dimensional array. * * @return a two-dimensional double array of covariance values * @throws NumberIsTooSmallException if the number of observations * for a cell is < 2 */ public double[][] getData() throws NumberIsTooSmallException { final double[][] data = new double[dimension][dimension]; for (int i = 0; i < dimension; i++) { for (int j = 0; j < dimension; j++) { data[i][j] = getElement(i, j).getResult(); } } return data; } /** * This {@link Covariance} method is not supported by a {@link StorelessCovariance}, * since the number of bivariate observations does not have to be the same for different * pairs of covariates - i.e., N as defined in {@link Covariance#getN()} is undefined. * * @return nothing as this implementation always throws a * {@link MathUnsupportedOperationException} * @throws MathUnsupportedOperationException in all cases */ @Override public int getN() throws MathUnsupportedOperationException { throw new MathUnsupportedOperationException(); } } ././@LongLink100644 0 0 147 12126630647 10264 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/stat/correlation/PearsonsCorrelation.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/stat/correlation/PearsonsCorrelation.ja100644 1750 1750 26465 12126627717 32370 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.stat.correlation; import org.apache.commons.math3.distribution.TDistribution; import org.apache.commons.math3.exception.util.LocalizedFormats; import org.apache.commons.math3.exception.MathIllegalArgumentException; import org.apache.commons.math3.exception.NullArgumentException; import org.apache.commons.math3.exception.DimensionMismatchException; import org.apache.commons.math3.linear.RealMatrix; import org.apache.commons.math3.linear.BlockRealMatrix; import org.apache.commons.math3.stat.regression.SimpleRegression; import org.apache.commons.math3.util.FastMath; /** * Computes Pearson's product-moment correlation coefficients for pairs of arrays * or columns of a matrix. * *

                              The constructors that take RealMatrix or * double[][] arguments generate correlation matrices. The * columns of the input matrices are assumed to represent variable values. * Correlations are given by the formula

                              * cor(X, Y) = Σ[(xi - E(X))(yi - E(Y))] / [(n - 1)s(X)s(Y)] * where E(X) is the mean of X, E(Y) * is the mean of the Y values and s(X), s(Y) are standard deviations. * * @version $Id: PearsonsCorrelation.java 1416643 2012-12-03 19:37:14Z tn $ * @since 2.0 */ public class PearsonsCorrelation { /** correlation matrix */ private final RealMatrix correlationMatrix; /** number of observations */ private final int nObs; /** * Create a PearsonsCorrelation instance without data */ public PearsonsCorrelation() { super(); correlationMatrix = null; nObs = 0; } /** * Create a PearsonsCorrelation from a rectangular array * whose columns represent values of variables to be correlated. * * @param data rectangular array with columns representing variables * @throws IllegalArgumentException if the input data array is not * rectangular with at least two rows and two columns. */ public PearsonsCorrelation(double[][] data) { this(new BlockRealMatrix(data)); } /** * Create a PearsonsCorrelation from a RealMatrix whose columns * represent variables to be correlated. * * @param matrix matrix with columns representing variables to correlate */ public PearsonsCorrelation(RealMatrix matrix) { checkSufficientData(matrix); nObs = matrix.getRowDimension(); correlationMatrix = computeCorrelationMatrix(matrix); } /** * Create a PearsonsCorrelation from a {@link Covariance}. The correlation * matrix is computed by scaling the Covariance's covariance matrix. * The Covariance instance must have been created from a data matrix with * columns representing variable values. * * @param covariance Covariance instance */ public PearsonsCorrelation(Covariance covariance) { RealMatrix covarianceMatrix = covariance.getCovarianceMatrix(); if (covarianceMatrix == null) { throw new NullArgumentException(LocalizedFormats.COVARIANCE_MATRIX); } nObs = covariance.getN(); correlationMatrix = covarianceToCorrelation(covarianceMatrix); } /** * Create a PearsonsCorrelation from a covariance matrix. The correlation * matrix is computed by scaling the covariance matrix. * * @param covarianceMatrix covariance matrix * @param numberOfObservations the number of observations in the dataset used to compute * the covariance matrix */ public PearsonsCorrelation(RealMatrix covarianceMatrix, int numberOfObservations) { nObs = numberOfObservations; correlationMatrix = covarianceToCorrelation(covarianceMatrix); } /** * Returns the correlation matrix * * @return correlation matrix */ public RealMatrix getCorrelationMatrix() { return correlationMatrix; } /** * Returns a matrix of standard errors associated with the estimates * in the correlation matrix.
                              * getCorrelationStandardErrors().getEntry(i,j) is the standard * error associated with getCorrelationMatrix.getEntry(i,j) *

                              The formula used to compute the standard error is
                              * SEr = ((1 - r2) / (n - 2))1/2 * where r is the estimated correlation coefficient and * n is the number of observations in the source dataset.

                              * * @return matrix of correlation standard errors */ public RealMatrix getCorrelationStandardErrors() { int nVars = correlationMatrix.getColumnDimension(); double[][] out = new double[nVars][nVars]; for (int i = 0; i < nVars; i++) { for (int j = 0; j < nVars; j++) { double r = correlationMatrix.getEntry(i, j); out[i][j] = FastMath.sqrt((1 - r * r) /(nObs - 2)); } } return new BlockRealMatrix(out); } /** * Returns a matrix of p-values associated with the (two-sided) null * hypothesis that the corresponding correlation coefficient is zero. *

                              getCorrelationPValues().getEntry(i,j) is the probability * that a random variable distributed as tn-2 takes * a value with absolute value greater than or equal to
                              * |r|((n - 2) / (1 - r2))1/2

                              *

                              The values in the matrix are sometimes referred to as the * significance of the corresponding correlation coefficients.

                              * * @return matrix of p-values * @throws org.apache.commons.math3.exception.MaxCountExceededException * if an error occurs estimating probabilities */ public RealMatrix getCorrelationPValues() { TDistribution tDistribution = new TDistribution(nObs - 2); int nVars = correlationMatrix.getColumnDimension(); double[][] out = new double[nVars][nVars]; for (int i = 0; i < nVars; i++) { for (int j = 0; j < nVars; j++) { if (i == j) { out[i][j] = 0d; } else { double r = correlationMatrix.getEntry(i, j); double t = FastMath.abs(r * FastMath.sqrt((nObs - 2)/(1 - r * r))); out[i][j] = 2 * tDistribution.cumulativeProbability(-t); } } } return new BlockRealMatrix(out); } /** * Computes the correlation matrix for the columns of the * input matrix. * * @param matrix matrix with columns representing variables to correlate * @return correlation matrix */ public RealMatrix computeCorrelationMatrix(RealMatrix matrix) { int nVars = matrix.getColumnDimension(); RealMatrix outMatrix = new BlockRealMatrix(nVars, nVars); for (int i = 0; i < nVars; i++) { for (int j = 0; j < i; j++) { double corr = correlation(matrix.getColumn(i), matrix.getColumn(j)); outMatrix.setEntry(i, j, corr); outMatrix.setEntry(j, i, corr); } outMatrix.setEntry(i, i, 1d); } return outMatrix; } /** * Computes the correlation matrix for the columns of the * input rectangular array. The colums of the array represent values * of variables to be correlated. * * @param data matrix with columns representing variables to correlate * @return correlation matrix */ public RealMatrix computeCorrelationMatrix(double[][] data) { return computeCorrelationMatrix(new BlockRealMatrix(data)); } /** * Computes the Pearson's product-moment correlation coefficient between the two arrays. * *

                              Throws IllegalArgumentException if the arrays do not have the same length * or their common length is less than 2

                              * * @param xArray first data array * @param yArray second data array * @return Returns Pearson's correlation coefficient for the two arrays * @throws DimensionMismatchException if the arrays lengths do not match * @throws MathIllegalArgumentException if there is insufficient data */ public double correlation(final double[] xArray, final double[] yArray) { SimpleRegression regression = new SimpleRegression(); if (xArray.length != yArray.length) { throw new DimensionMismatchException(xArray.length, yArray.length); } else if (xArray.length < 2) { throw new MathIllegalArgumentException(LocalizedFormats.INSUFFICIENT_DIMENSION, xArray.length, 2); } else { for(int i=0; iUses the formula
                              * r(X,Y) = cov(X,Y)/s(X)s(Y) where * r(·,·) is the correlation coefficient and * s(·) means standard deviation.

                              * * @param covarianceMatrix the covariance matrix * @return correlation matrix */ public RealMatrix covarianceToCorrelation(RealMatrix covarianceMatrix) { int nVars = covarianceMatrix.getColumnDimension(); RealMatrix outMatrix = new BlockRealMatrix(nVars, nVars); for (int i = 0; i < nVars; i++) { double sigma = FastMath.sqrt(covarianceMatrix.getEntry(i, i)); outMatrix.setEntry(i, i, 1d); for (int j = 0; j < i; j++) { double entry = covarianceMatrix.getEntry(i, j) / (sigma * FastMath.sqrt(covarianceMatrix.getEntry(j, j))); outMatrix.setEntry(i, j, entry); outMatrix.setEntry(j, i, entry); } } return outMatrix; } /** * Throws IllegalArgumentException of the matrix does not have at least * two columns and two rows * * @param matrix matrix to check for sufficiency * @throws MathIllegalArgumentException if there is insufficient data */ private void checkSufficientData(final RealMatrix matrix) { int nRows = matrix.getRowDimension(); int nCols = matrix.getColumnDimension(); if (nRows < 2 || nCols < 2) { throw new MathIllegalArgumentException(LocalizedFormats.INSUFFICIENT_ROWS_AND_COLUMNS, nRows, nCols); } } } ././@LongLink100644 0 0 150 12126630647 10256 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/stat/correlation/SpearmansCorrelation.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/stat/correlation/SpearmansCorrelation.j100644 1750 1750 23331 12126627717 32353 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.stat.correlation; import java.util.ArrayList; import java.util.HashSet; import java.util.List; import java.util.Set; import org.apache.commons.math3.exception.DimensionMismatchException; import org.apache.commons.math3.exception.MathIllegalArgumentException; import org.apache.commons.math3.exception.util.LocalizedFormats; import org.apache.commons.math3.linear.BlockRealMatrix; import org.apache.commons.math3.linear.RealMatrix; import org.apache.commons.math3.stat.ranking.NaNStrategy; import org.apache.commons.math3.stat.ranking.NaturalRanking; import org.apache.commons.math3.stat.ranking.RankingAlgorithm; /** * Spearman's rank correlation. This implementation performs a rank * transformation on the input data and then computes {@link PearsonsCorrelation} * on the ranked data. *

                              * By default, ranks are computed using {@link NaturalRanking} with default * strategies for handling NaNs and ties in the data (NaNs maximal, ties averaged). * The ranking algorithm can be set using a constructor argument. * * @since 2.0 * @version $Id: SpearmansCorrelation.java 1461822 2013-03-27 19:44:22Z tn $ */ public class SpearmansCorrelation { /** Input data */ private final RealMatrix data; /** Ranking algorithm */ private final RankingAlgorithm rankingAlgorithm; /** Rank correlation */ private final PearsonsCorrelation rankCorrelation; /** * Create a SpearmansCorrelation without data. */ public SpearmansCorrelation() { this(new NaturalRanking()); } /** * Create a SpearmansCorrelation with the given ranking algorithm. *

                              * From version 4.0 onwards this constructor will throw an exception * if the provided {@link NaturalRanking} uses a {@link NaNStrategy#REMOVED} strategy. * * @param rankingAlgorithm ranking algorithm * @since 3.1 */ public SpearmansCorrelation(final RankingAlgorithm rankingAlgorithm) { data = null; this.rankingAlgorithm = rankingAlgorithm; rankCorrelation = null; } /** * Create a SpearmansCorrelation from the given data matrix. * * @param dataMatrix matrix of data with columns representing * variables to correlate */ public SpearmansCorrelation(final RealMatrix dataMatrix) { this(dataMatrix, new NaturalRanking()); } /** * Create a SpearmansCorrelation with the given input data matrix * and ranking algorithm. *

                              * From version 4.0 onwards this constructor will throw an exception * if the provided {@link NaturalRanking} uses a {@link NaNStrategy#REMOVED} strategy. * * @param dataMatrix matrix of data with columns representing * variables to correlate * @param rankingAlgorithm ranking algorithm */ public SpearmansCorrelation(final RealMatrix dataMatrix, final RankingAlgorithm rankingAlgorithm) { this.rankingAlgorithm = rankingAlgorithm; this.data = rankTransform(dataMatrix); rankCorrelation = new PearsonsCorrelation(data); } /** * Calculate the Spearman Rank Correlation Matrix. * * @return Spearman Rank Correlation Matrix */ public RealMatrix getCorrelationMatrix() { return rankCorrelation.getCorrelationMatrix(); } /** * Returns a {@link PearsonsCorrelation} instance constructed from the * ranked input data. That is, * new SpearmansCorrelation(matrix).getRankCorrelation() * is equivalent to * new PearsonsCorrelation(rankTransform(matrix)) where * rankTransform(matrix) is the result of applying the * configured RankingAlgorithm to each of the columns of * matrix. * * @return PearsonsCorrelation among ranked column data */ public PearsonsCorrelation getRankCorrelation() { return rankCorrelation; } /** * Computes the Spearman's rank correlation matrix for the columns of the * input matrix. * * @param matrix matrix with columns representing variables to correlate * @return correlation matrix */ public RealMatrix computeCorrelationMatrix(final RealMatrix matrix) { final RealMatrix matrixCopy = rankTransform(matrix); return new PearsonsCorrelation().computeCorrelationMatrix(matrixCopy); } /** * Computes the Spearman's rank correlation matrix for the columns of the * input rectangular array. The columns of the array represent values * of variables to be correlated. * * @param matrix matrix with columns representing variables to correlate * @return correlation matrix */ public RealMatrix computeCorrelationMatrix(final double[][] matrix) { return computeCorrelationMatrix(new BlockRealMatrix(matrix)); } /** * Computes the Spearman's rank correlation coefficient between the two arrays. * * @param xArray first data array * @param yArray second data array * @return Returns Spearman's rank correlation coefficient for the two arrays * @throws DimensionMismatchException if the arrays lengths do not match * @throws MathIllegalArgumentException if the array length is less than 2 */ public double correlation(final double[] xArray, final double[] yArray) { if (xArray.length != yArray.length) { throw new DimensionMismatchException(xArray.length, yArray.length); } else if (xArray.length < 2) { throw new MathIllegalArgumentException(LocalizedFormats.INSUFFICIENT_DIMENSION, xArray.length, 2); } else { double[] x = xArray; double[] y = yArray; if (rankingAlgorithm instanceof NaturalRanking && NaNStrategy.REMOVED == ((NaturalRanking) rankingAlgorithm).getNanStrategy()) { final Set nanPositions = new HashSet(); nanPositions.addAll(getNaNPositions(xArray)); nanPositions.addAll(getNaNPositions(yArray)); x = removeValues(xArray, nanPositions); y = removeValues(yArray, nanPositions); } return new PearsonsCorrelation().correlation(rankingAlgorithm.rank(x), rankingAlgorithm.rank(y)); } } /** * Applies rank transform to each of the columns of matrix * using the current rankingAlgorithm. * * @param matrix matrix to transform * @return a rank-transformed matrix */ private RealMatrix rankTransform(final RealMatrix matrix) { RealMatrix transformed = null; if (rankingAlgorithm instanceof NaturalRanking && ((NaturalRanking) rankingAlgorithm).getNanStrategy() == NaNStrategy.REMOVED) { final Set nanPositions = new HashSet(); for (int i = 0; i < matrix.getColumnDimension(); i++) { nanPositions.addAll(getNaNPositions(matrix.getColumn(i))); } // if we have found NaN values, we have to update the matrix size if (!nanPositions.isEmpty()) { transformed = new BlockRealMatrix(matrix.getRowDimension() - nanPositions.size(), matrix.getColumnDimension()); for (int i = 0; i < transformed.getColumnDimension(); i++) { transformed.setColumn(i, removeValues(matrix.getColumn(i), nanPositions)); } } } if (transformed == null) { transformed = matrix.copy(); } for (int i = 0; i < transformed.getColumnDimension(); i++) { transformed.setColumn(i, rankingAlgorithm.rank(transformed.getColumn(i))); } return transformed; } /** * Returns a list containing the indices of NaN values in the input array. * * @param input the input array * @return a list of NaN positions in the input array */ private List getNaNPositions(final double[] input) { final List positions = new ArrayList(); for (int i = 0; i < input.length; i++) { if (Double.isNaN(input[i])) { positions.add(i); } } return positions; } /** * Removes all values from the input array at the specified indices. * * @param input the input array * @param indices a set containing the indices to be removed * @return the input array without the values at the specified indices */ private double[] removeValues(final double[] input, final Set indices) { if (indices.isEmpty()) { return input; } final double[] result = new double[input.length - indices.size()]; for (int i = 0, j = 0; i < input.length; i++) { if (!indices.contains(i)) { result[j++] = input[i]; } } return result; } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/stat/Frequency.java100644 1750 1750 46514 12126627717 26340 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.stat; import java.io.Serializable; import java.text.NumberFormat; import java.util.Collection; import java.util.Iterator; import java.util.Comparator; import java.util.Map; import java.util.TreeMap; import org.apache.commons.math3.exception.MathIllegalArgumentException; import org.apache.commons.math3.exception.NullArgumentException; import org.apache.commons.math3.exception.util.LocalizedFormats; import org.apache.commons.math3.util.MathUtils; /** * Maintains a frequency distribution. *

                              * Accepts int, long, char or Comparable values. New values added must be * comparable to those that have been added, otherwise the add method will * throw an IllegalArgumentException.

                              *

                              * Integer values (int, long, Integer, Long) are not distinguished by type -- * i.e. addValue(Long.valueOf(2)), addValue(2), addValue(2l) all have * the same effect (similarly for arguments to getCount, etc.).

                              *

                              * char values are converted by addValue to Character instances. * As such, these values are not comparable to integral values, so attempts * to combine integral types with chars in a frequency distribution will fail. *

                              *

                              * The values are ordered using the default (natural order), unless a * Comparator is supplied in the constructor.

                              * * @version $Id: Frequency.java 1455703 2013-03-12 20:46:23Z tn $ */ public class Frequency implements Serializable { /** Serializable version identifier */ private static final long serialVersionUID = -3845586908418844111L; /** underlying collection */ private final TreeMap, Long> freqTable; /** * Default constructor. */ public Frequency() { freqTable = new TreeMap, Long>(); } /** * Constructor allowing values Comparator to be specified. * * @param comparator Comparator used to order values */ @SuppressWarnings("unchecked") // TODO is the cast OK? public Frequency(Comparator comparator) { freqTable = new TreeMap, Long>((Comparator>) comparator); } /** * Return a string representation of this frequency * distribution. * * @return a string representation. */ @Override public String toString() { NumberFormat nf = NumberFormat.getPercentInstance(); StringBuilder outBuffer = new StringBuilder(); outBuffer.append("Value \t Freq. \t Pct. \t Cum Pct. \n"); Iterator> iter = freqTable.keySet().iterator(); while (iter.hasNext()) { Comparable value = iter.next(); outBuffer.append(value); outBuffer.append('\t'); outBuffer.append(getCount(value)); outBuffer.append('\t'); outBuffer.append(nf.format(getPct(value))); outBuffer.append('\t'); outBuffer.append(nf.format(getCumPct(value))); outBuffer.append('\n'); } return outBuffer.toString(); } /** * Adds 1 to the frequency count for v. *

                              * If other objects have already been added to this Frequency, v must * be comparable to those that have already been added. *

                              * * @param v the value to add. * @throws MathIllegalArgumentException if v is not comparable with previous entries */ public void addValue(Comparable v) throws MathIllegalArgumentException { incrementValue(v, 1); } /** * Increments the frequency count for v. *

                              * If other objects have already been added to this Frequency, v must * be comparable to those that have already been added. *

                              * * @param v the value to add. * @param increment the amount by which the value should be incremented * @throws IllegalArgumentException if v is not comparable with previous entries * @since 3.1 */ public void incrementValue(Comparable v, long increment){ Comparable obj = v; if (v instanceof Integer) { obj = Long.valueOf(((Integer) v).longValue()); } try { Long count = freqTable.get(obj); if (count == null) { freqTable.put(obj, Long.valueOf(increment)); } else { freqTable.put(obj, Long.valueOf(count.longValue() + increment)); } } catch (ClassCastException ex) { //TreeMap will throw ClassCastException if v is not comparable throw new MathIllegalArgumentException( LocalizedFormats.INSTANCES_NOT_COMPARABLE_TO_EXISTING_VALUES, v.getClass().getName()); } } /** * Adds 1 to the frequency count for v. * * @param v the value to add. * @throws MathIllegalArgumentException if the table contains entries not * comparable to Integer */ public void addValue(int v) throws MathIllegalArgumentException { addValue(Long.valueOf(v)); } /** * Adds 1 to the frequency count for v. * * @param v the value to add. * @throws MathIllegalArgumentException if the table contains entries not * comparable to Long */ public void addValue(long v) throws MathIllegalArgumentException { addValue(Long.valueOf(v)); } /** * Adds 1 to the frequency count for v. * * @param v the value to add. * @throws MathIllegalArgumentException if the table contains entries not * comparable to Char */ public void addValue(char v) throws MathIllegalArgumentException { addValue(Character.valueOf(v)); } /** Clears the frequency table */ public void clear() { freqTable.clear(); } /** * Returns an Iterator over the set of values that have been added. *

                              * If added values are integral (i.e., integers, longs, Integers, or Longs), * they are converted to Longs when they are added, so the objects returned * by the Iterator will in this case be Longs.

                              * * @return values Iterator */ public Iterator> valuesIterator() { return freqTable.keySet().iterator(); } /** * Return an Iterator over the set of keys and values that have been added. * Using the entry set to iterate is more efficient in the case where you * need to access respective counts as well as values, since it doesn't * require a "get" for every key...the value is provided in the Map.Entry. *

                              * If added values are integral (i.e., integers, longs, Integers, or Longs), * they are converted to Longs when they are added, so the values of the * map entries returned by the Iterator will in this case be Longs.

                              * * @return entry set Iterator * @since 3.1 */ public Iterator, Long>> entrySetIterator() { return freqTable.entrySet().iterator(); } //------------------------------------------------------------------------- /** * Returns the sum of all frequencies. * * @return the total frequency count. */ public long getSumFreq() { long result = 0; Iterator iterator = freqTable.values().iterator(); while (iterator.hasNext()) { result += iterator.next().longValue(); } return result; } /** * Returns the number of values = v. * Returns 0 if the value is not comparable. * * @param v the value to lookup. * @return the frequency of v. */ public long getCount(Comparable v) { if (v instanceof Integer) { return getCount(((Integer) v).longValue()); } long result = 0; try { Long count = freqTable.get(v); if (count != null) { result = count.longValue(); } } catch (ClassCastException ex) { // NOPMD // ignore and return 0 -- ClassCastException will be thrown if value is not comparable } return result; } /** * Returns the number of values = v. * * @param v the value to lookup. * @return the frequency of v. */ public long getCount(int v) { return getCount(Long.valueOf(v)); } /** * Returns the number of values = v. * * @param v the value to lookup. * @return the frequency of v. */ public long getCount(long v) { return getCount(Long.valueOf(v)); } /** * Returns the number of values = v. * * @param v the value to lookup. * @return the frequency of v. */ public long getCount(char v) { return getCount(Character.valueOf(v)); } /** * Returns the number of values in the frequency table. * * @return the number of unique values that have been added to the frequency table. * @see #valuesIterator() */ public int getUniqueCount(){ return freqTable.keySet().size(); } /** * Returns the percentage of values that are equal to v * (as a proportion between 0 and 1). *

                              * Returns Double.NaN if no values have been added.

                              * * @param v the value to lookup * @return the proportion of values equal to v */ public double getPct(Comparable v) { final long sumFreq = getSumFreq(); if (sumFreq == 0) { return Double.NaN; } return (double) getCount(v) / (double) sumFreq; } /** * Returns the percentage of values that are equal to v * (as a proportion between 0 and 1). * * @param v the value to lookup * @return the proportion of values equal to v */ public double getPct(int v) { return getPct(Long.valueOf(v)); } /** * Returns the percentage of values that are equal to v * (as a proportion between 0 and 1). * * @param v the value to lookup * @return the proportion of values equal to v */ public double getPct(long v) { return getPct(Long.valueOf(v)); } /** * Returns the percentage of values that are equal to v * (as a proportion between 0 and 1). * * @param v the value to lookup * @return the proportion of values equal to v */ public double getPct(char v) { return getPct(Character.valueOf(v)); } //----------------------------------------------------------------------------------------- /** * Returns the cumulative frequency of values less than or equal to v. *

                              * Returns 0 if v is not comparable to the values set.

                              * * @param v the value to lookup. * @return the proportion of values equal to v */ @SuppressWarnings({ "rawtypes", "unchecked" }) public long getCumFreq(Comparable v) { if (getSumFreq() == 0) { return 0; } if (v instanceof Integer) { return getCumFreq(((Integer) v).longValue()); } Comparator> c = (Comparator>) freqTable.comparator(); if (c == null) { c = new NaturalComparator(); } long result = 0; try { Long value = freqTable.get(v); if (value != null) { result = value.longValue(); } } catch (ClassCastException ex) { return result; // v is not comparable } if (c.compare(v, freqTable.firstKey()) < 0) { return 0; // v is comparable, but less than first value } if (c.compare(v, freqTable.lastKey()) >= 0) { return getSumFreq(); // v is comparable, but greater than the last value } Iterator> values = valuesIterator(); while (values.hasNext()) { Comparable nextValue = values.next(); if (c.compare(v, nextValue) > 0) { result += getCount(nextValue); } else { return result; } } return result; } /** * Returns the cumulative frequency of values less than or equal to v. *

                              * Returns 0 if v is not comparable to the values set.

                              * * @param v the value to lookup * @return the proportion of values equal to v */ public long getCumFreq(int v) { return getCumFreq(Long.valueOf(v)); } /** * Returns the cumulative frequency of values less than or equal to v. *

                              * Returns 0 if v is not comparable to the values set.

                              * * @param v the value to lookup * @return the proportion of values equal to v */ public long getCumFreq(long v) { return getCumFreq(Long.valueOf(v)); } /** * Returns the cumulative frequency of values less than or equal to v. *

                              * Returns 0 if v is not comparable to the values set.

                              * * @param v the value to lookup * @return the proportion of values equal to v */ public long getCumFreq(char v) { return getCumFreq(Character.valueOf(v)); } //---------------------------------------------------------------------------------------------- /** * Returns the cumulative percentage of values less than or equal to v * (as a proportion between 0 and 1). *

                              * Returns Double.NaN if no values have been added. * Returns 0 if at least one value has been added, but v is not comparable * to the values set.

                              * * @param v the value to lookup * @return the proportion of values less than or equal to v */ public double getCumPct(Comparable v) { final long sumFreq = getSumFreq(); if (sumFreq == 0) { return Double.NaN; } return (double) getCumFreq(v) / (double) sumFreq; } /** * Returns the cumulative percentage of values less than or equal to v * (as a proportion between 0 and 1). *

                              * Returns 0 if v is not comparable to the values set.

                              * * @param v the value to lookup * @return the proportion of values less than or equal to v */ public double getCumPct(int v) { return getCumPct(Long.valueOf(v)); } /** * Returns the cumulative percentage of values less than or equal to v * (as a proportion between 0 and 1). *

                              * Returns 0 if v is not comparable to the values set.

                              * * @param v the value to lookup * @return the proportion of values less than or equal to v */ public double getCumPct(long v) { return getCumPct(Long.valueOf(v)); } /** * Returns the cumulative percentage of values less than or equal to v * (as a proportion between 0 and 1). *

                              * Returns 0 if v is not comparable to the values set.

                              * * @param v the value to lookup * @return the proportion of values less than or equal to v */ public double getCumPct(char v) { return getCumPct(Character.valueOf(v)); } //---------------------------------------------------------------------------------------------- /** * Merge another Frequency object's counts into this instance. * This Frequency's counts will be incremented (or set when not already set) * by the counts represented by other. * * @param other the other {@link Frequency} object to be merged * @throws NullArgumentException if {@code other} is null * @since 3.1 */ public void merge(final Frequency other) throws NullArgumentException { MathUtils.checkNotNull(other, LocalizedFormats.NULL_NOT_ALLOWED); final Iterator, Long>> iter = other.entrySetIterator(); while (iter.hasNext()) { final Map.Entry, Long> entry = iter.next(); incrementValue(entry.getKey(), entry.getValue()); } } /** * Merge a {@link Collection} of {@link Frequency} objects into this instance. * This Frequency's counts will be incremented (or set when not already set) * by the counts represented by each of the others. * * @param others the other {@link Frequency} objects to be merged * @throws NullArgumentException if the collection is null * @since 3.1 */ public void merge(final Collection others) throws NullArgumentException { MathUtils.checkNotNull(others, LocalizedFormats.NULL_NOT_ALLOWED); for (final Frequency freq : others) { merge(freq); } } //---------------------------------------------------------------------------------------------- /** * A Comparator that compares comparable objects using the * natural order. Copied from Commons Collections ComparableComparator. */ private static class NaturalComparator> implements Comparator>, Serializable { /** Serializable version identifier */ private static final long serialVersionUID = -3852193713161395148L; /** * Compare the two {@link Comparable Comparable} arguments. * This method is equivalent to: *
                              (({@link Comparable Comparable})o1).{@link Comparable#compareTo compareTo}(o2)
                              * * @param o1 the first object * @param o2 the second object * @return result of comparison * @throws NullPointerException when o1 is null, * or when ((Comparable)o1).compareTo(o2) does * @throws ClassCastException when o1 is not a {@link Comparable Comparable}, * or when ((Comparable)o1).compareTo(o2) does */ @SuppressWarnings("unchecked") // cast to (T) may throw ClassCastException, see Javadoc public int compare(Comparable o1, Comparable o2) { return o1.compareTo((T) o2); } } /** {@inheritDoc} */ @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((freqTable == null) ? 0 : freqTable.hashCode()); return result; } /** {@inheritDoc} */ @Override public boolean equals(Object obj) { if (this == obj) { return true; } if (!(obj instanceof Frequency)) { return false; } Frequency other = (Frequency) obj; if (freqTable == null) { if (other.freqTable != null) { return false; } } else if (!freqTable.equals(other.freqTable)) { return false; } return true; } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/FieldElement.java100644 1750 1750 6207 12126627720 25726 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3; import org.apache.commons.math3.exception.MathArithmeticException; import org.apache.commons.math3.exception.NullArgumentException; /** * Interface representing field elements. * @param the type of the field elements * @see Field * @version $Id: FieldElement.java 1416643 2012-12-03 19:37:14Z tn $ * @since 2.0 */ public interface FieldElement { /** Compute this + a. * @param a element to add * @return a new element representing this + a * @throws NullArgumentException if {@code addend} is {@code null}. */ T add(T a) throws NullArgumentException; /** Compute this - a. * @param a element to subtract * @return a new element representing this - a * @throws NullArgumentException if {@code a} is {@code null}. */ T subtract(T a) throws NullArgumentException; /** * Returns the additive inverse of {@code this} element. * @return the opposite of {@code this}. */ T negate(); /** Compute n × this. Multiplication by an integer number is defined * as the following sum *
                              * n × this = ∑i=1n this. *
                              * @param n Number of times {@code this} must be added to itself. * @return A new element representing n × this. */ T multiply(int n); /** Compute this × a. * @param a element to multiply * @return a new element representing this × a * @throws NullArgumentException if {@code a} is {@code null}. */ T multiply(T a) throws NullArgumentException; /** Compute this ÷ a. * @param a element to add * @return a new element representing this ÷ a * @throws NullArgumentException if {@code a} is {@code null}. * @throws MathArithmeticException if {@code a} is zero */ T divide(T a) throws NullArgumentException, MathArithmeticException; /** * Returns the multiplicative inverse of {@code this} element. * @return the inverse of {@code this}. * @throws MathArithmeticException if {@code this} is zero */ T reciprocal() throws MathArithmeticException; /** Get the {@link Field} to which the instance belongs. * @return {@link Field} to which the instance belongs */ Field getField(); } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/distribution/RealDistribution.java100644 1750 1750 16535 12126627700 31416 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.distribution; import org.apache.commons.math3.exception.NumberIsTooLargeException; import org.apache.commons.math3.exception.OutOfRangeException; /** * Base interface for distributions on the reals. * * @version $Id: RealDistribution.java 1416643 2012-12-03 19:37:14Z tn $ * @since 3.0 */ public interface RealDistribution { /** * For a random variable {@code X} whose values are distributed according * to this distribution, this method returns {@code P(X = x)}. In other * words, this method represents the probability mass function (PMF) * for the distribution. * * @param x the point at which the PMF is evaluated * @return the value of the probability mass function at point {@code x} */ double probability(double x); /** * Returns the probability density function (PDF) of this distribution * evaluated at the specified point {@code x}. In general, the PDF is * the derivative of the {@link #cumulativeProbability(double) CDF}. * If the derivative does not exist at {@code x}, then an appropriate * replacement should be returned, e.g. {@code Double.POSITIVE_INFINITY}, * {@code Double.NaN}, or the limit inferior or limit superior of the * difference quotient. * * @param x the point at which the PDF is evaluated * @return the value of the probability density function at point {@code x} */ double density(double x); /** * For a random variable {@code X} whose values are distributed according * to this distribution, this method returns {@code P(X <= x)}. In other * words, this method represents the (cumulative) distribution function * (CDF) for this distribution. * * @param x the point at which the CDF is evaluated * @return the probability that a random variable with this * distribution takes a value less than or equal to {@code x} */ double cumulativeProbability(double x); /** * For a random variable {@code X} whose values are distributed according * to this distribution, this method returns {@code P(x0 < X <= x1)}. * * @param x0 the exclusive lower bound * @param x1 the inclusive upper bound * @return the probability that a random variable with this distribution * takes a value between {@code x0} and {@code x1}, * excluding the lower and including the upper endpoint * @throws NumberIsTooLargeException if {@code x0 > x1} * * @deprecated As of 3.1. In 4.0, this method will be renamed * {@code probability(double x0, double x1)}. */ @Deprecated double cumulativeProbability(double x0, double x1) throws NumberIsTooLargeException; /** * Computes the quantile function of this distribution. For a random * variable {@code X} distributed according to this distribution, the * returned value is *
                                *
                              • inf{x in R | P(X<=x) >= p} for {@code 0 < p <= 1},
                              • *
                              • inf{x in R | P(X<=x) > 0} for {@code p = 0}.
                              • *
                              * * @param p the cumulative probability * @return the smallest {@code p}-quantile of this distribution * (largest 0-quantile for {@code p = 0}) * @throws OutOfRangeException if {@code p < 0} or {@code p > 1} */ double inverseCumulativeProbability(double p) throws OutOfRangeException; /** * Use this method to get the numerical value of the mean of this * distribution. * * @return the mean or {@code Double.NaN} if it is not defined */ double getNumericalMean(); /** * Use this method to get the numerical value of the variance of this * distribution. * * @return the variance (possibly {@code Double.POSITIVE_INFINITY} as * for certain cases in {@link TDistribution}) or {@code Double.NaN} if it * is not defined */ double getNumericalVariance(); /** * Access the lower bound of the support. This method must return the same * value as {@code inverseCumulativeProbability(0)}. In other words, this * method must return *

                              inf {x in R | P(X <= x) > 0}.

                              * * @return lower bound of the support (might be * {@code Double.NEGATIVE_INFINITY}) */ double getSupportLowerBound(); /** * Access the upper bound of the support. This method must return the same * value as {@code inverseCumulativeProbability(1)}. In other words, this * method must return *

                              inf {x in R | P(X <= x) = 1}.

                              * * @return upper bound of the support (might be * {@code Double.POSITIVE_INFINITY}) */ double getSupportUpperBound(); /** * Whether or not the lower bound of support is in the domain of the density * function. Returns true iff {@code getSupporLowerBound()} is finite and * {@code density(getSupportLowerBound())} returns a non-NaN, non-infinite * value. * * @return true if the lower bound of support is finite and the density * function returns a non-NaN, non-infinite value there * @deprecated to be removed in 4.0 */ boolean isSupportLowerBoundInclusive(); /** * Whether or not the upper bound of support is in the domain of the density * function. Returns true iff {@code getSupportUpperBound()} is finite and * {@code density(getSupportUpperBound())} returns a non-NaN, non-infinite * value. * * @return true if the upper bound of support is finite and the density * function returns a non-NaN, non-infinite value there * @deprecated to be removed in 4.0 */ boolean isSupportUpperBoundInclusive(); /** * Use this method to get information about whether the support is connected, * i.e. whether all values between the lower and upper bound of the support * are included in the support. * * @return whether the support is connected or not */ boolean isSupportConnected(); /** * Reseed the random generator used to generate samples. * * @param seed the new seed */ void reseedRandomGenerator(long seed); /** * Generate a random value sampled from this distribution. * * @return a random value. */ double sample(); /** * Generate a random sample from the distribution. * * @param sampleSize the number of random values to generate * @return an array representing the random sample * @throws org.apache.commons.math3.exception.NotStrictlyPositiveException * if {@code sampleSize} is not positive */ double[] sample(int sampleSize); } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/distribution/package-info.java100644 1750 1750 1636 12126627700 30433 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ /** * Implementations of common discrete and continuous distributions. */ package org.apache.commons.math3.distribution; commons-math3-3.2-src/src/main/java/org/apache/commons/math3/distribution/PoissonDistribution.java100644 1750 1750 31716 12126627700 32163 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.distribution; import org.apache.commons.math3.exception.NotStrictlyPositiveException; import org.apache.commons.math3.exception.util.LocalizedFormats; import org.apache.commons.math3.special.Gamma; import org.apache.commons.math3.util.MathUtils; import org.apache.commons.math3.util.ArithmeticUtils; import org.apache.commons.math3.util.FastMath; import org.apache.commons.math3.random.RandomGenerator; import org.apache.commons.math3.random.Well19937c; /** * Implementation of the Poisson distribution. * * @see Poisson distribution (Wikipedia) * @see Poisson distribution (MathWorld) * @version $Id: PoissonDistribution.java 1416643 2012-12-03 19:37:14Z tn $ */ public class PoissonDistribution extends AbstractIntegerDistribution { /** * Default maximum number of iterations for cumulative probability calculations. * @since 2.1 */ public static final int DEFAULT_MAX_ITERATIONS = 10000000; /** * Default convergence criterion. * @since 2.1 */ public static final double DEFAULT_EPSILON = 1e-12; /** Serializable version identifier. */ private static final long serialVersionUID = -3349935121172596109L; /** Distribution used to compute normal approximation. */ private final NormalDistribution normal; /** Distribution needed for the {@link #sample()} method. */ private final ExponentialDistribution exponential; /** Mean of the distribution. */ private final double mean; /** * Maximum number of iterations for cumulative probability. Cumulative * probabilities are estimated using either Lanczos series approximation * of {@link Gamma#regularizedGammaP(double, double, double, int)} * or continued fraction approximation of * {@link Gamma#regularizedGammaQ(double, double, double, int)}. */ private final int maxIterations; /** Convergence criterion for cumulative probability. */ private final double epsilon; /** * Creates a new Poisson distribution with specified mean. * * @param p the Poisson mean * @throws NotStrictlyPositiveException if {@code p <= 0}. */ public PoissonDistribution(double p) throws NotStrictlyPositiveException { this(p, DEFAULT_EPSILON, DEFAULT_MAX_ITERATIONS); } /** * Creates a new Poisson distribution with specified mean, convergence * criterion and maximum number of iterations. * * @param p Poisson mean. * @param epsilon Convergence criterion for cumulative probabilities. * @param maxIterations the maximum number of iterations for cumulative * probabilities. * @throws NotStrictlyPositiveException if {@code p <= 0}. * @since 2.1 */ public PoissonDistribution(double p, double epsilon, int maxIterations) throws NotStrictlyPositiveException { this(new Well19937c(), p, epsilon, maxIterations); } /** * Creates a new Poisson distribution with specified mean, convergence * criterion and maximum number of iterations. * * @param rng Random number generator. * @param p Poisson mean. * @param epsilon Convergence criterion for cumulative probabilities. * @param maxIterations the maximum number of iterations for cumulative * probabilities. * @throws NotStrictlyPositiveException if {@code p <= 0}. * @since 3.1 */ public PoissonDistribution(RandomGenerator rng, double p, double epsilon, int maxIterations) throws NotStrictlyPositiveException { super(rng); if (p <= 0) { throw new NotStrictlyPositiveException(LocalizedFormats.MEAN, p); } mean = p; this.epsilon = epsilon; this.maxIterations = maxIterations; // Use the same RNG instance as the parent class. normal = new NormalDistribution(rng, p, FastMath.sqrt(p), NormalDistribution.DEFAULT_INVERSE_ABSOLUTE_ACCURACY); exponential = new ExponentialDistribution(rng, 1, ExponentialDistribution.DEFAULT_INVERSE_ABSOLUTE_ACCURACY); } /** * Creates a new Poisson distribution with the specified mean and * convergence criterion. * * @param p Poisson mean. * @param epsilon Convergence criterion for cumulative probabilities. * @throws NotStrictlyPositiveException if {@code p <= 0}. * @since 2.1 */ public PoissonDistribution(double p, double epsilon) throws NotStrictlyPositiveException { this(p, epsilon, DEFAULT_MAX_ITERATIONS); } /** * Creates a new Poisson distribution with the specified mean and maximum * number of iterations. * * @param p Poisson mean. * @param maxIterations Maximum number of iterations for cumulative * probabilities. * @since 2.1 */ public PoissonDistribution(double p, int maxIterations) { this(p, DEFAULT_EPSILON, maxIterations); } /** * Get the mean for the distribution. * * @return the mean for the distribution. */ public double getMean() { return mean; } /** {@inheritDoc} */ public double probability(int x) { double ret; if (x < 0 || x == Integer.MAX_VALUE) { ret = 0.0; } else if (x == 0) { ret = FastMath.exp(-mean); } else { ret = FastMath.exp(-SaddlePointExpansion.getStirlingError(x) - SaddlePointExpansion.getDeviancePart(x, mean)) / FastMath.sqrt(MathUtils.TWO_PI * x); } return ret; } /** {@inheritDoc} */ public double cumulativeProbability(int x) { if (x < 0) { return 0; } if (x == Integer.MAX_VALUE) { return 1; } return Gamma.regularizedGammaQ((double) x + 1, mean, epsilon, maxIterations); } /** * Calculates the Poisson distribution function using a normal * approximation. The {@code N(mean, sqrt(mean))} distribution is used * to approximate the Poisson distribution. The computation uses * "half-correction" (evaluating the normal distribution function at * {@code x + 0.5}). * * @param x Upper bound, inclusive. * @return the distribution function value calculated using a normal * approximation. */ public double normalApproximateProbability(int x) { // calculate the probability using half-correction return normal.cumulativeProbability(x + 0.5); } /** * {@inheritDoc} * * For mean parameter {@code p}, the mean is {@code p}. */ public double getNumericalMean() { return getMean(); } /** * {@inheritDoc} * * For mean parameter {@code p}, the variance is {@code p}. */ public double getNumericalVariance() { return getMean(); } /** * {@inheritDoc} * * The lower bound of the support is always 0 no matter the mean parameter. * * @return lower bound of the support (always 0) */ public int getSupportLowerBound() { return 0; } /** * {@inheritDoc} * * The upper bound of the support is positive infinity, * regardless of the parameter values. There is no integer infinity, * so this method returns {@code Integer.MAX_VALUE}. * * @return upper bound of the support (always {@code Integer.MAX_VALUE} for * positive infinity) */ public int getSupportUpperBound() { return Integer.MAX_VALUE; } /** * {@inheritDoc} * * The support of this distribution is connected. * * @return {@code true} */ public boolean isSupportConnected() { return true; } /** * {@inheritDoc} *

                              * Algorithm Description: *

                                *
                              • For small means, uses simulation of a Poisson process * using Uniform deviates, as described * here. * The Poisson process (and hence value returned) is bounded by 1000 * mean. *
                              • *
                              • For large means, uses the rejection algorithm described in * * Devroye, Luc. (1981).The Computer Generation of Poisson Random Variables * Computing vol. 26 pp. 197-207. * *
                              • *
                              *

                              * * @return a random value. * @since 2.2 */ @Override public int sample() { return (int) FastMath.min(nextPoisson(mean), Integer.MAX_VALUE); } /** * @param meanPoisson Mean of the Poisson distribution. * @return the next sample. */ private long nextPoisson(double meanPoisson) { final double pivot = 40.0d; if (meanPoisson < pivot) { double p = FastMath.exp(-meanPoisson); long n = 0; double r = 1.0d; double rnd = 1.0d; while (n < 1000 * meanPoisson) { rnd = random.nextDouble(); r = r * rnd; if (r >= p) { n++; } else { return n; } } return n; } else { final double lambda = FastMath.floor(meanPoisson); final double lambdaFractional = meanPoisson - lambda; final double logLambda = FastMath.log(lambda); final double logLambdaFactorial = ArithmeticUtils.factorialLog((int) lambda); final long y2 = lambdaFractional < Double.MIN_VALUE ? 0 : nextPoisson(lambdaFractional); final double delta = FastMath.sqrt(lambda * FastMath.log(32 * lambda / FastMath.PI + 1)); final double halfDelta = delta / 2; final double twolpd = 2 * lambda + delta; final double a1 = FastMath.sqrt(FastMath.PI * twolpd) * FastMath.exp(1 / 8 * lambda); final double a2 = (twolpd / delta) * FastMath.exp(-delta * (1 + delta) / twolpd); final double aSum = a1 + a2 + 1; final double p1 = a1 / aSum; final double p2 = a2 / aSum; final double c1 = 1 / (8 * lambda); double x = 0; double y = 0; double v = 0; int a = 0; double t = 0; double qr = 0; double qa = 0; for (;;) { final double u = random.nextDouble(); if (u <= p1) { final double n = random.nextGaussian(); x = n * FastMath.sqrt(lambda + halfDelta) - 0.5d; if (x > delta || x < -lambda) { continue; } y = x < 0 ? FastMath.floor(x) : FastMath.ceil(x); final double e = exponential.sample(); v = -e - (n * n / 2) + c1; } else { if (u > p1 + p2) { y = lambda; break; } else { x = delta + (twolpd / delta) * exponential.sample(); y = FastMath.ceil(x); v = -exponential.sample() - delta * (x + 1) / twolpd; } } a = x < 0 ? 1 : 0; t = y * (y + 1) / (2 * lambda); if (v < -t && a == 0) { y = lambda + y; break; } qr = t * ((2 * y + 1) / (6 * lambda) - 1); qa = qr - (t * t) / (3 * (lambda + a * (y + 1))); if (v < qa) { y = lambda + y; break; } if (v > qr) { continue; } if (v < y * logLambda - ArithmeticUtils.factorialLog((int) (y + lambda)) + logLambdaFactorial) { y = lambda + y; break; } } return y2 + (long) y; } } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/distribution/FDistribution.java100644 1750 1750 23650 12126627700 30714 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.distribution; import org.apache.commons.math3.exception.NotStrictlyPositiveException; import org.apache.commons.math3.exception.util.LocalizedFormats; import org.apache.commons.math3.special.Beta; import org.apache.commons.math3.util.FastMath; import org.apache.commons.math3.random.RandomGenerator; import org.apache.commons.math3.random.Well19937c; /** * Implementation of the F-distribution. * * @see F-distribution (Wikipedia) * @see F-distribution (MathWorld) * @version $Id: FDistribution.java 1416643 2012-12-03 19:37:14Z tn $ */ public class FDistribution extends AbstractRealDistribution { /** * Default inverse cumulative probability accuracy. * @since 2.1 */ public static final double DEFAULT_INVERSE_ABSOLUTE_ACCURACY = 1e-9; /** Serializable version identifier. */ private static final long serialVersionUID = -8516354193418641566L; /** The numerator degrees of freedom. */ private final double numeratorDegreesOfFreedom; /** The numerator degrees of freedom. */ private final double denominatorDegreesOfFreedom; /** Inverse cumulative probability accuracy. */ private final double solverAbsoluteAccuracy; /** Cached numerical variance */ private double numericalVariance = Double.NaN; /** Whether or not the numerical variance has been calculated */ private boolean numericalVarianceIsCalculated = false; /** * Creates an F distribution using the given degrees of freedom. * * @param numeratorDegreesOfFreedom Numerator degrees of freedom. * @param denominatorDegreesOfFreedom Denominator degrees of freedom. * @throws NotStrictlyPositiveException if * {@code numeratorDegreesOfFreedom <= 0} or * {@code denominatorDegreesOfFreedom <= 0}. */ public FDistribution(double numeratorDegreesOfFreedom, double denominatorDegreesOfFreedom) throws NotStrictlyPositiveException { this(numeratorDegreesOfFreedom, denominatorDegreesOfFreedom, DEFAULT_INVERSE_ABSOLUTE_ACCURACY); } /** * Creates an F distribution using the given degrees of freedom * and inverse cumulative probability accuracy. * * @param numeratorDegreesOfFreedom Numerator degrees of freedom. * @param denominatorDegreesOfFreedom Denominator degrees of freedom. * @param inverseCumAccuracy the maximum absolute error in inverse * cumulative probability estimates. * @throws NotStrictlyPositiveException if * {@code numeratorDegreesOfFreedom <= 0} or * {@code denominatorDegreesOfFreedom <= 0}. * @since 2.1 */ public FDistribution(double numeratorDegreesOfFreedom, double denominatorDegreesOfFreedom, double inverseCumAccuracy) throws NotStrictlyPositiveException { this(new Well19937c(), numeratorDegreesOfFreedom, denominatorDegreesOfFreedom, inverseCumAccuracy); } /** * Creates an F distribution. * * @param rng Random number generator. * @param numeratorDegreesOfFreedom Numerator degrees of freedom. * @param denominatorDegreesOfFreedom Denominator degrees of freedom. * @param inverseCumAccuracy the maximum absolute error in inverse * cumulative probability estimates. * @throws NotStrictlyPositiveException if * {@code numeratorDegreesOfFreedom <= 0} or * {@code denominatorDegreesOfFreedom <= 0}. * @since 3.1 */ public FDistribution(RandomGenerator rng, double numeratorDegreesOfFreedom, double denominatorDegreesOfFreedom, double inverseCumAccuracy) throws NotStrictlyPositiveException { super(rng); if (numeratorDegreesOfFreedom <= 0) { throw new NotStrictlyPositiveException(LocalizedFormats.DEGREES_OF_FREEDOM, numeratorDegreesOfFreedom); } if (denominatorDegreesOfFreedom <= 0) { throw new NotStrictlyPositiveException(LocalizedFormats.DEGREES_OF_FREEDOM, denominatorDegreesOfFreedom); } this.numeratorDegreesOfFreedom = numeratorDegreesOfFreedom; this.denominatorDegreesOfFreedom = denominatorDegreesOfFreedom; solverAbsoluteAccuracy = inverseCumAccuracy; } /** * {@inheritDoc} * * @since 2.1 */ public double density(double x) { final double nhalf = numeratorDegreesOfFreedom / 2; final double mhalf = denominatorDegreesOfFreedom / 2; final double logx = FastMath.log(x); final double logn = FastMath.log(numeratorDegreesOfFreedom); final double logm = FastMath.log(denominatorDegreesOfFreedom); final double lognxm = FastMath.log(numeratorDegreesOfFreedom * x + denominatorDegreesOfFreedom); return FastMath.exp(nhalf * logn + nhalf * logx - logx + mhalf * logm - nhalf * lognxm - mhalf * lognxm - Beta.logBeta(nhalf, mhalf)); } /** * {@inheritDoc} * * The implementation of this method is based on * */ public double cumulativeProbability(double x) { double ret; if (x <= 0) { ret = 0; } else { double n = numeratorDegreesOfFreedom; double m = denominatorDegreesOfFreedom; ret = Beta.regularizedBeta((n * x) / (m + n * x), 0.5 * n, 0.5 * m); } return ret; } /** * Access the numerator degrees of freedom. * * @return the numerator degrees of freedom. */ public double getNumeratorDegreesOfFreedom() { return numeratorDegreesOfFreedom; } /** * Access the denominator degrees of freedom. * * @return the denominator degrees of freedom. */ public double getDenominatorDegreesOfFreedom() { return denominatorDegreesOfFreedom; } /** {@inheritDoc} */ @Override protected double getSolverAbsoluteAccuracy() { return solverAbsoluteAccuracy; } /** * {@inheritDoc} * * For denominator degrees of freedom parameter {@code b}, the mean is *
                                *
                              • if {@code b > 2} then {@code b / (b - 2)},
                              • *
                              • else undefined ({@code Double.NaN}). *
                              */ public double getNumericalMean() { final double denominatorDF = getDenominatorDegreesOfFreedom(); if (denominatorDF > 2) { return denominatorDF / (denominatorDF - 2); } return Double.NaN; } /** * {@inheritDoc} * * For numerator degrees of freedom parameter {@code a} and denominator * degrees of freedom parameter {@code b}, the variance is *
                                *
                              • * if {@code b > 4} then * {@code [2 * b^2 * (a + b - 2)] / [a * (b - 2)^2 * (b - 4)]}, *
                              • *
                              • else undefined ({@code Double.NaN}). *
                              */ public double getNumericalVariance() { if (!numericalVarianceIsCalculated) { numericalVariance = calculateNumericalVariance(); numericalVarianceIsCalculated = true; } return numericalVariance; } /** * used by {@link #getNumericalVariance()} * * @return the variance of this distribution */ protected double calculateNumericalVariance() { final double denominatorDF = getDenominatorDegreesOfFreedom(); if (denominatorDF > 4) { final double numeratorDF = getNumeratorDegreesOfFreedom(); final double denomDFMinusTwo = denominatorDF - 2; return ( 2 * (denominatorDF * denominatorDF) * (numeratorDF + denominatorDF - 2) ) / ( (numeratorDF * (denomDFMinusTwo * denomDFMinusTwo) * (denominatorDF - 4)) ); } return Double.NaN; } /** * {@inheritDoc} * * The lower bound of the support is always 0 no matter the parameters. * * @return lower bound of the support (always 0) */ public double getSupportLowerBound() { return 0; } /** * {@inheritDoc} * * The upper bound of the support is always positive infinity * no matter the parameters. * * @return upper bound of the support (always Double.POSITIVE_INFINITY) */ public double getSupportUpperBound() { return Double.POSITIVE_INFINITY; } /** {@inheritDoc} */ public boolean isSupportLowerBoundInclusive() { return false; } /** {@inheritDoc} */ public boolean isSupportUpperBoundInclusive() { return false; } /** * {@inheritDoc} * * The support of this distribution is connected. * * @return {@code true} */ public boolean isSupportConnected() { return true; } } ././@LongLink100644 0 0 154 12126630647 10262 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/distribution/MultivariateRealDistribution.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/distribution/MultivariateRealDistributi100644 1750 1750 5761 12126627700 32507 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.distribution; import org.apache.commons.math3.exception.NotStrictlyPositiveException; /** * Base interface for multivariate distributions on the reals. * * This is based largely on the RealDistribution interface, but cumulative * distribution functions are not required because they are often quite * difficult to compute for multivariate distributions. * * @version $Id: MultivariateRealDistribution.java 1416643 2012-12-03 19:37:14Z tn $ * @since 3.1 */ public interface MultivariateRealDistribution { /** * Returns the probability density function (PDF) of this distribution * evaluated at the specified point {@code x}. In general, the PDF is the * derivative of the cumulative distribution function. If the derivative * does not exist at {@code x}, then an appropriate replacement should be * returned, e.g. {@code Double.POSITIVE_INFINITY}, {@code Double.NaN}, or * the limit inferior or limit superior of the difference quotient. * * @param x Point at which the PDF is evaluated. * @return the value of the probability density function at point {@code x}. */ double density(double[] x); /** * Reseeds the random generator used to generate samples. * * @param seed Seed with which to initialize the random number generator. */ void reseedRandomGenerator(long seed); /** * Gets the number of random variables of the distribution. * It is the size of the array returned by the {@link #sample() sample} * method. * * @return the number of variables. */ int getDimension(); /** * Generates a random value vector sampled from this distribution. * * @return a random value vector. */ double[] sample(); /** * Generates a list of a random value vectors from the distribution. * * @param sampleSize the number of random vectors to generate. * @return an array representing the random samples. * @throws org.apache.commons.math3.exception.NotStrictlyPositiveException * if {@code sampleSize} is not positive. * * @see #sample() */ double[][] sample(int sampleSize) throws NotStrictlyPositiveException; } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/distribution/SaddlePointExpansion.java100644 1750 1750 15707 12126627700 32226 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.distribution; import org.apache.commons.math3.special.Gamma; import org.apache.commons.math3.util.FastMath; import org.apache.commons.math3.util.MathUtils; /** *

                              * Utility class used by various distributions to accurately compute their * respective probability mass functions. The implementation for this class is * based on the Catherine Loader's dbinom routines. *

                              *

                              * This class is not intended to be called directly. *

                              *

                              * References: *

                                *
                              1. Catherine Loader (2000). "Fast and Accurate Computation of Binomial * Probabilities.". * http://www.herine.net/stat/papers/dbinom.pdf
                              2. *
                              *

                              * * @since 2.1 * @version $Id: SaddlePointExpansion.java 1416643 2012-12-03 19:37:14Z tn $ */ final class SaddlePointExpansion { /** 1/2 * log(2 π). */ private static final double HALF_LOG_2_PI = 0.5 * FastMath.log(MathUtils.TWO_PI); /** exact Stirling expansion error for certain values. */ private static final double[] EXACT_STIRLING_ERRORS = { 0.0, /* 0.0 */ 0.1534264097200273452913848, /* 0.5 */ 0.0810614667953272582196702, /* 1.0 */ 0.0548141210519176538961390, /* 1.5 */ 0.0413406959554092940938221, /* 2.0 */ 0.03316287351993628748511048, /* 2.5 */ 0.02767792568499833914878929, /* 3.0 */ 0.02374616365629749597132920, /* 3.5 */ 0.02079067210376509311152277, /* 4.0 */ 0.01848845053267318523077934, /* 4.5 */ 0.01664469118982119216319487, /* 5.0 */ 0.01513497322191737887351255, /* 5.5 */ 0.01387612882307074799874573, /* 6.0 */ 0.01281046524292022692424986, /* 6.5 */ 0.01189670994589177009505572, /* 7.0 */ 0.01110455975820691732662991, /* 7.5 */ 0.010411265261972096497478567, /* 8.0 */ 0.009799416126158803298389475, /* 8.5 */ 0.009255462182712732917728637, /* 9.0 */ 0.008768700134139385462952823, /* 9.5 */ 0.008330563433362871256469318, /* 10.0 */ 0.007934114564314020547248100, /* 10.5 */ 0.007573675487951840794972024, /* 11.0 */ 0.007244554301320383179543912, /* 11.5 */ 0.006942840107209529865664152, /* 12.0 */ 0.006665247032707682442354394, /* 12.5 */ 0.006408994188004207068439631, /* 13.0 */ 0.006171712263039457647532867, /* 13.5 */ 0.005951370112758847735624416, /* 14.0 */ 0.005746216513010115682023589, /* 14.5 */ 0.005554733551962801371038690 /* 15.0 */ }; /** * Default constructor. */ private SaddlePointExpansion() { super(); } /** * Compute the error of Stirling's series at the given value. *

                              * References: *

                                *
                              1. Eric W. Weisstein. "Stirling's Series." From MathWorld--A Wolfram Web * Resource. * http://mathworld.wolfram.com/StirlingsSeries.html
                              2. *
                              *

                              * * @param z the value. * @return the Striling's series error. */ static double getStirlingError(double z) { double ret; if (z < 15.0) { double z2 = 2.0 * z; if (FastMath.floor(z2) == z2) { ret = EXACT_STIRLING_ERRORS[(int) z2]; } else { ret = Gamma.logGamma(z + 1.0) - (z + 0.5) * FastMath.log(z) + z - HALF_LOG_2_PI; } } else { double z2 = z * z; ret = (0.083333333333333333333 - (0.00277777777777777777778 - (0.00079365079365079365079365 - (0.000595238095238095238095238 - 0.0008417508417508417508417508 / z2) / z2) / z2) / z2) / z; } return ret; } /** * A part of the deviance portion of the saddle point approximation. *

                              * References: *

                                *
                              1. Catherine Loader (2000). "Fast and Accurate Computation of Binomial * Probabilities.". * http://www.herine.net/stat/papers/dbinom.pdf
                              2. *
                              *

                              * * @param x the x value. * @param mu the average. * @return a part of the deviance. */ static double getDeviancePart(double x, double mu) { double ret; if (FastMath.abs(x - mu) < 0.1 * (x + mu)) { double d = x - mu; double v = d / (x + mu); double s1 = v * d; double s = Double.NaN; double ej = 2.0 * x * v; v = v * v; int j = 1; while (s1 != s) { s = s1; ej *= v; s1 = s + ej / ((j * 2) + 1); ++j; } ret = s1; } else { ret = x * FastMath.log(x / mu) + mu - x; } return ret; } /** * Compute the logarithm of the PMF for a binomial distribution * using the saddle point expansion. * * @param x the value at which the probability is evaluated. * @param n the number of trials. * @param p the probability of success. * @param q the probability of failure (1 - p). * @return log(p(x)). */ static double logBinomialProbability(int x, int n, double p, double q) { double ret; if (x == 0) { if (p < 0.1) { ret = -getDeviancePart(n, n * q) - n * p; } else { ret = n * FastMath.log(q); } } else if (x == n) { if (q < 0.1) { ret = -getDeviancePart(n, n * p) - n * q; } else { ret = n * FastMath.log(p); } } else { ret = getStirlingError(n) - getStirlingError(x) - getStirlingError(n - x) - getDeviancePart(x, n * p) - getDeviancePart(n - x, n * q); double f = (MathUtils.TWO_PI * x * (n - x)) / n; ret = -0.5 * FastMath.log(f) + ret; } return ret; } } ././@LongLink100644 0 0 163 12126630647 10262 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/distribution/MixtureMultivariateRealDistribution.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/distribution/MixtureMultivariateRealDis100644 1750 1750 13533 12126627700 32476 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.distribution; import java.util.List; import java.util.ArrayList; import org.apache.commons.math3.exception.DimensionMismatchException; import org.apache.commons.math3.exception.NotPositiveException; import org.apache.commons.math3.exception.MathArithmeticException; import org.apache.commons.math3.exception.util.LocalizedFormats; import org.apache.commons.math3.random.RandomGenerator; import org.apache.commons.math3.random.Well19937c; import org.apache.commons.math3.util.Pair; /** * Class for representing * mixture model distributions. * * @param Type of the mixture components. * * @version $Id: MixtureMultivariateRealDistribution.java 1416643 2012-12-03 19:37:14Z tn $ * @since 3.1 */ public class MixtureMultivariateRealDistribution extends AbstractMultivariateRealDistribution { /** Normalized weight of each mixture component. */ private final double[] weight; /** Mixture components. */ private final List distribution; /** * Creates a mixture model from a list of distributions and their * associated weights. * * @param components List of (weight, distribution) pairs from which to sample. */ public MixtureMultivariateRealDistribution(List> components) { this(new Well19937c(), components); } /** * Creates a mixture model from a list of distributions and their * associated weights. * * @param rng Random number generator. * @param components Distributions from which to sample. * @throws NotPositiveException if any of the weights is negative. * @throws DimensionMismatchException if not all components have the same * number of variables. */ public MixtureMultivariateRealDistribution(RandomGenerator rng, List> components) { super(rng, components.get(0).getSecond().getDimension()); final int numComp = components.size(); final int dim = getDimension(); double weightSum = 0; for (int i = 0; i < numComp; i++) { final Pair comp = components.get(i); if (comp.getSecond().getDimension() != dim) { throw new DimensionMismatchException(comp.getSecond().getDimension(), dim); } if (comp.getFirst() < 0) { throw new NotPositiveException(comp.getFirst()); } weightSum += comp.getFirst(); } // Check for overflow. if (Double.isInfinite(weightSum)) { throw new MathArithmeticException(LocalizedFormats.OVERFLOW); } // Store each distribution and its normalized weight. distribution = new ArrayList(); weight = new double[numComp]; for (int i = 0; i < numComp; i++) { final Pair comp = components.get(i); weight[i] = comp.getFirst() / weightSum; distribution.add(comp.getSecond()); } } /** {@inheritDoc} */ public double density(final double[] values) { double p = 0; for (int i = 0; i < weight.length; i++) { p += weight[i] * distribution.get(i).density(values); } return p; } /** {@inheritDoc} */ public double[] sample() { // Sampled values. double[] vals = null; // Determine which component to sample from. final double randomValue = random.nextDouble(); double sum = 0; for (int i = 0; i < weight.length; i++) { sum += weight[i]; if (randomValue <= sum) { // pick model i vals = distribution.get(i).sample(); break; } } if (vals == null) { // This should never happen, but it ensures we won't return a null in // case the loop above has some floating point inequality problem on // the final iteration. vals = distribution.get(weight.length - 1).sample(); } return vals; } /** {@inheritDoc} */ public void reseedRandomGenerator(long seed) { // Seed needs to be propagated to underlying components // in order to maintain consistency between runs. super.reseedRandomGenerator(seed); for (int i = 0; i < distribution.size(); i++) { // Make each component's seed different in order to avoid // using the same sequence of random numbers. distribution.get(i).reseedRandomGenerator(i + 1 + seed); } } /** * Gets the distributions that make up the mixture model. * * @return the component distributions and associated weights. */ public List> getComponents() { final List> list = new ArrayList>(); for (int i = 0; i < weight.length; i++) { list.add(new Pair(weight[i], distribution.get(i))); } return list; } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/distribution/fitting/package-info.java100644 1750 1750 1622 12126627677 32107 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ /** * Fitting of parameters against distributions. */ package org.apache.commons.math3.distribution.fitting; ././@LongLink100644 0 0 210 12126630647 10253 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/distribution/fitting/MultivariateNormalMixtureExpectationMaximization.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/distribution/fitting/MultivariateNormal100644 1750 1750 44020 12126627677 32501 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.distribution.fitting; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import org.apache.commons.math3.distribution.MultivariateNormalDistribution; import org.apache.commons.math3.distribution.MixtureMultivariateNormalDistribution; import org.apache.commons.math3.exception.ConvergenceException; import org.apache.commons.math3.exception.DimensionMismatchException; import org.apache.commons.math3.exception.NotStrictlyPositiveException; import org.apache.commons.math3.exception.NumberIsTooSmallException; import org.apache.commons.math3.exception.NumberIsTooLargeException; import org.apache.commons.math3.exception.util.LocalizedFormats; import org.apache.commons.math3.linear.Array2DRowRealMatrix; import org.apache.commons.math3.linear.RealMatrix; import org.apache.commons.math3.linear.SingularMatrixException; import org.apache.commons.math3.stat.correlation.Covariance; import org.apache.commons.math3.util.MathArrays; import org.apache.commons.math3.util.Pair; /** * Expectation-Maximization algorithm for fitting the parameters of * multivariate normal mixture model distributions. * * This implementation is pure original code based on * EM Demystified: An Expectation-Maximization Tutorial by Yihua Chen and Maya R. Gupta, * Department of Electrical Engineering, University of Washington, Seattle, WA 98195. * It was verified using external tools like CRAN Mixtools * (see the JUnit test cases) but it is not based on Mixtools code at all. * The discussion of the origin of this class can be seen in the comments of the MATH-817 JIRA issue. * @version $Id: MultivariateNormalMixtureExpectationMaximization.java 1462508 2013-03-29 15:50:47Z luc $ * @since 3.2 */ public class MultivariateNormalMixtureExpectationMaximization { /** * Default maximum number of iterations allowed per fitting process. */ private static final int DEFAULT_MAX_ITERATIONS = 1000; /** * Default convergence threshold for fitting. */ private static final double DEFAULT_THRESHOLD = 1E-5; /** * The data to fit. */ private final double[][] data; /** * The model fit against the data. */ private MixtureMultivariateNormalDistribution fittedModel; /** * The log likelihood of the data given the fitted model. */ private double logLikelihood = 0d; /** * Creates an object to fit a multivariate normal mixture model to data. * * @param data Data to use in fitting procedure * @throws NotStrictlyPositiveException if data has no rows * @throws DimensionMismatchException if rows of data have different numbers * of columns * @throws NumberIsTooSmallException if the number of columns in the data is * less than 2 */ public MultivariateNormalMixtureExpectationMaximization(double[][] data) throws NotStrictlyPositiveException, DimensionMismatchException, NumberIsTooSmallException { if (data.length < 1) { throw new NotStrictlyPositiveException(data.length); } this.data = new double[data.length][data[0].length]; for (int i = 0; i < data.length; i++) { if (data[i].length != data[0].length) { // Jagged arrays not allowed throw new DimensionMismatchException(data[i].length, data[0].length); } if (data[i].length < 2) { throw new NumberIsTooSmallException(LocalizedFormats.NUMBER_TOO_SMALL, data[i].length, 2, true); } this.data[i] = MathArrays.copyOf(data[i], data[i].length); } } /** * Fit a mixture model to the data supplied to the constructor. * * The quality of the fit depends on the concavity of the data provided to * the constructor and the initial mixture provided to this function. If the * data has many local optima, multiple runs of the fitting function with * different initial mixtures may be required to find the optimal solution. * If a SingularMatrixException is encountered, it is possible that another * initialization would work. * * @param initialMixture Model containing initial values of weights and * multivariate normals * @param maxIterations Maximum iterations allowed for fit * @param threshold Convergence threshold computed as difference in * logLikelihoods between successive iterations * @throws SingularMatrixException if any component's covariance matrix is * singular during fitting * @throws NotStrictlyPositiveException if numComponents is less than one * or threshold is less than Double.MIN_VALUE * @throws DimensionMismatchException if initialMixture mean vector and data * number of columns are not equal */ public void fit(final MixtureMultivariateNormalDistribution initialMixture, final int maxIterations, final double threshold) throws SingularMatrixException, NotStrictlyPositiveException, DimensionMismatchException { if (maxIterations < 1) { throw new NotStrictlyPositiveException(maxIterations); } if (threshold < Double.MIN_VALUE) { throw new NotStrictlyPositiveException(threshold); } final int n = data.length; // Number of data columns. Jagged data already rejected in constructor, // so we can assume the lengths of each row are equal. final int numCols = data[0].length; final int k = initialMixture.getComponents().size(); final int numMeanColumns = initialMixture.getComponents().get(0).getSecond().getMeans().length; if (numMeanColumns != numCols) { throw new DimensionMismatchException(numMeanColumns, numCols); } int numIterations = 0; double previousLogLikelihood = 0d; logLikelihood = Double.NEGATIVE_INFINITY; // Initialize model to fit to initial mixture. fittedModel = new MixtureMultivariateNormalDistribution(initialMixture.getComponents()); while (numIterations++ <= maxIterations && Math.abs(previousLogLikelihood - logLikelihood) > threshold) { previousLogLikelihood = logLikelihood; double sumLogLikelihood = 0d; // Mixture components final List> components = fittedModel.getComponents(); // Weight and distribution of each component final double[] weights = new double[k]; final MultivariateNormalDistribution[] mvns = new MultivariateNormalDistribution[k]; for (int j = 0; j < k; j++) { weights[j] = components.get(j).getFirst(); mvns[j] = components.get(j).getSecond(); } // E-step: compute the data dependent parameters of the expectation // function. // The percentage of row's total density between a row and a // component final double[][] gamma = new double[n][k]; // Sum of gamma for each component final double[] gammaSums = new double[k]; // Sum of gamma times its row for each each component final double[][] gammaDataProdSums = new double[k][numCols]; for (int i = 0; i < n; i++) { final double rowDensity = fittedModel.density(data[i]); sumLogLikelihood += Math.log(rowDensity); for (int j = 0; j < k; j++) { gamma[i][j] = weights[j] * mvns[j].density(data[i]) / rowDensity; gammaSums[j] += gamma[i][j]; for (int col = 0; col < numCols; col++) { gammaDataProdSums[j][col] += gamma[i][j] * data[i][col]; } } } logLikelihood = sumLogLikelihood / n; // M-step: compute the new parameters based on the expectation // function. final double[] newWeights = new double[k]; final double[][] newMeans = new double[k][numCols]; for (int j = 0; j < k; j++) { newWeights[j] = gammaSums[j] / n; for (int col = 0; col < numCols; col++) { newMeans[j][col] = gammaDataProdSums[j][col] / gammaSums[j]; } } // Compute new covariance matrices final RealMatrix[] newCovMats = new RealMatrix[k]; for (int j = 0; j < k; j++) { newCovMats[j] = new Array2DRowRealMatrix(numCols, numCols); } for (int i = 0; i < n; i++) { for (int j = 0; j < k; j++) { final RealMatrix vec = new Array2DRowRealMatrix(MathArrays.ebeSubtract(data[i], newMeans[j])); final RealMatrix dataCov = vec.multiply(vec.transpose()).scalarMultiply(gamma[i][j]); newCovMats[j] = newCovMats[j].add(dataCov); } } // Converting to arrays for use by fitted model final double[][][] newCovMatArrays = new double[k][numCols][numCols]; for (int j = 0; j < k; j++) { newCovMats[j] = newCovMats[j].scalarMultiply(1d / gammaSums[j]); newCovMatArrays[j] = newCovMats[j].getData(); } // Update current model fittedModel = new MixtureMultivariateNormalDistribution(newWeights, newMeans, newCovMatArrays); } if (Math.abs(previousLogLikelihood - logLikelihood) > threshold) { // Did not converge before the maximum number of iterations throw new ConvergenceException(); } } /** * Fit a mixture model to the data supplied to the constructor. * * The quality of the fit depends on the concavity of the data provided to * the constructor and the initial mixture provided to this function. If the * data has many local optima, multiple runs of the fitting function with * different initial mixtures may be required to find the optimal solution. * If a SingularMatrixException is encountered, it is possible that another * initialization would work. * * @param initialMixture Model containing initial values of weights and * multivariate normals * @throws SingularMatrixException if any component's covariance matrix is * singular during fitting * @throws NotStrictlyPositiveException if numComponents is less than one or * threshold is less than Double.MIN_VALUE */ public void fit(MixtureMultivariateNormalDistribution initialMixture) throws SingularMatrixException, NotStrictlyPositiveException { fit(initialMixture, DEFAULT_MAX_ITERATIONS, DEFAULT_THRESHOLD); } /** * Helper method to create a multivariate normal mixture model which can be * used to initialize {@link #fit(MixtureMultivariateNormalDistribution)}. * * This method uses the data supplied to the constructor to try to determine * a good mixture model at which to start the fit, but it is not guaranteed * to supply a model which will find the optimal solution or even converge. * * @param data Data to estimate distribution * @param numComponents Number of components for estimated mixture * @return Multivariate normal mixture model estimated from the data * @throws NumberIsTooLargeException if {@code numComponents} is greater * than the number of data rows. * @throws NumberIsTooSmallException if {@code numComponents < 2}. * @throws NotStrictlyPositiveException if data has less than 2 rows * @throws DimensionMismatchException if rows of data have different numbers * of columns */ public static MixtureMultivariateNormalDistribution estimate(final double[][] data, final int numComponents) throws NotStrictlyPositiveException, DimensionMismatchException { if (data.length < 2) { throw new NotStrictlyPositiveException(data.length); } if (numComponents < 2) { throw new NumberIsTooSmallException(numComponents, 2, true); } if (numComponents > data.length) { throw new NumberIsTooLargeException(numComponents, data.length, true); } final int numRows = data.length; final int numCols = data[0].length; // sort the data final DataRow[] sortedData = new DataRow[numRows]; for (int i = 0; i < numRows; i++) { sortedData[i] = new DataRow(data[i]); } Arrays.sort(sortedData); // uniform weight for each bin final double weight = 1d / numComponents; // components of mixture model to be created final List> components = new ArrayList>(); // create a component based on data in each bin for (int binIndex = 0; binIndex < numComponents; binIndex++) { // minimum index (inclusive) from sorted data for this bin final int minIndex = (binIndex * numRows) / numComponents; // maximum index (exclusive) from sorted data for this bin final int maxIndex = ((binIndex + 1) * numRows) / numComponents; // number of data records that will be in this bin final int numBinRows = maxIndex - minIndex; // data for this bin final double[][] binData = new double[numBinRows][numCols]; // mean of each column for the data in the this bin final double[] columnMeans = new double[numCols]; // populate bin and create component for (int i = minIndex, iBin = 0; i < maxIndex; i++, iBin++) { for (int j = 0; j < numCols; j++) { final double val = sortedData[i].getRow()[j]; columnMeans[j] += val; binData[iBin][j] = val; } } MathArrays.scaleInPlace(1d / numBinRows, columnMeans); // covariance matrix for this bin final double[][] covMat = new Covariance(binData).getCovarianceMatrix().getData(); final MultivariateNormalDistribution mvn = new MultivariateNormalDistribution(columnMeans, covMat); components.add(new Pair(weight, mvn)); } return new MixtureMultivariateNormalDistribution(components); } /** * Gets the log likelihood of the data under the fitted model. * * @return Log likelihood of data or zero of no data has been fit */ public double getLogLikelihood() { return logLikelihood; } /** * Gets the fitted model. * * @return fitted model or {@code null} if no fit has been performed yet. */ public MixtureMultivariateNormalDistribution getFittedModel() { return new MixtureMultivariateNormalDistribution(fittedModel.getComponents()); } /** * Class used for sorting user-supplied data. */ private static class DataRow implements Comparable { /** One data row. */ private final double[] row; /** Mean of the data row. */ private Double mean; /** * Create a data row. * @param data Data to use for the row */ DataRow(final double[] data) { // Store reference. row = data; // Compute mean. mean = 0d; for (int i = 0; i < data.length; i++) { mean += data[i]; } mean /= data.length; } /** * Compare two data rows. * @param other The other row * @return int for sorting */ public int compareTo(final DataRow other) { return mean.compareTo(other.mean); } /** {@inheritDoc} */ @Override public boolean equals(Object other) { if (this == other) { return true; } if (other instanceof DataRow) { return MathArrays.equals(row, ((DataRow) other).row); } return false; } /** {@inheritDoc} */ @Override public int hashCode() { return Arrays.hashCode(row); } /** * Get a data row. * @return data row array */ public double[] getRow() { return row; } } } ././@LongLink100644 0 0 152 12126630647 10260 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/distribution/HypergeometricDistribution.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/distribution/HypergeometricDistribution100644 1750 1750 27230 12126627700 32573 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.distribution; import org.apache.commons.math3.exception.NotPositiveException; import org.apache.commons.math3.exception.NotStrictlyPositiveException; import org.apache.commons.math3.exception.NumberIsTooLargeException; import org.apache.commons.math3.exception.util.LocalizedFormats; import org.apache.commons.math3.util.FastMath; import org.apache.commons.math3.random.RandomGenerator; import org.apache.commons.math3.random.Well19937c; /** * Implementation of the hypergeometric distribution. * * @see Hypergeometric distribution (Wikipedia) * @see Hypergeometric distribution (MathWorld) * @version $Id: HypergeometricDistribution.java 1416643 2012-12-03 19:37:14Z tn $ */ public class HypergeometricDistribution extends AbstractIntegerDistribution { /** Serializable version identifier. */ private static final long serialVersionUID = -436928820673516179L; /** The number of successes in the population. */ private final int numberOfSuccesses; /** The population size. */ private final int populationSize; /** The sample size. */ private final int sampleSize; /** Cached numerical variance */ private double numericalVariance = Double.NaN; /** Whether or not the numerical variance has been calculated */ private boolean numericalVarianceIsCalculated = false; /** * Construct a new hypergeometric distribution with the specified population * size, number of successes in the population, and sample size. * * @param populationSize Population size. * @param numberOfSuccesses Number of successes in the population. * @param sampleSize Sample size. * @throws NotPositiveException if {@code numberOfSuccesses < 0}. * @throws NotStrictlyPositiveException if {@code populationSize <= 0}. * @throws NumberIsTooLargeException if {@code numberOfSuccesses > populationSize}, * or {@code sampleSize > populationSize}. */ public HypergeometricDistribution(int populationSize, int numberOfSuccesses, int sampleSize) throws NotPositiveException, NotStrictlyPositiveException, NumberIsTooLargeException { this(new Well19937c(), populationSize, numberOfSuccesses, sampleSize); } /** * Creates a new hypergeometric distribution. * * @param rng Random number generator. * @param populationSize Population size. * @param numberOfSuccesses Number of successes in the population. * @param sampleSize Sample size. * @throws NotPositiveException if {@code numberOfSuccesses < 0}. * @throws NotStrictlyPositiveException if {@code populationSize <= 0}. * @throws NumberIsTooLargeException if {@code numberOfSuccesses > populationSize}, * or {@code sampleSize > populationSize}. * @since 3.1 */ public HypergeometricDistribution(RandomGenerator rng, int populationSize, int numberOfSuccesses, int sampleSize) throws NotPositiveException, NotStrictlyPositiveException, NumberIsTooLargeException { super(rng); if (populationSize <= 0) { throw new NotStrictlyPositiveException(LocalizedFormats.POPULATION_SIZE, populationSize); } if (numberOfSuccesses < 0) { throw new NotPositiveException(LocalizedFormats.NUMBER_OF_SUCCESSES, numberOfSuccesses); } if (sampleSize < 0) { throw new NotPositiveException(LocalizedFormats.NUMBER_OF_SAMPLES, sampleSize); } if (numberOfSuccesses > populationSize) { throw new NumberIsTooLargeException(LocalizedFormats.NUMBER_OF_SUCCESS_LARGER_THAN_POPULATION_SIZE, numberOfSuccesses, populationSize, true); } if (sampleSize > populationSize) { throw new NumberIsTooLargeException(LocalizedFormats.SAMPLE_SIZE_LARGER_THAN_POPULATION_SIZE, sampleSize, populationSize, true); } this.numberOfSuccesses = numberOfSuccesses; this.populationSize = populationSize; this.sampleSize = sampleSize; } /** {@inheritDoc} */ public double cumulativeProbability(int x) { double ret; int[] domain = getDomain(populationSize, numberOfSuccesses, sampleSize); if (x < domain[0]) { ret = 0.0; } else if (x >= domain[1]) { ret = 1.0; } else { ret = innerCumulativeProbability(domain[0], x, 1); } return ret; } /** * Return the domain for the given hypergeometric distribution parameters. * * @param n Population size. * @param m Number of successes in the population. * @param k Sample size. * @return a two element array containing the lower and upper bounds of the * hypergeometric distribution. */ private int[] getDomain(int n, int m, int k) { return new int[] { getLowerDomain(n, m, k), getUpperDomain(m, k) }; } /** * Return the lowest domain value for the given hypergeometric distribution * parameters. * * @param n Population size. * @param m Number of successes in the population. * @param k Sample size. * @return the lowest domain value of the hypergeometric distribution. */ private int getLowerDomain(int n, int m, int k) { return FastMath.max(0, m - (n - k)); } /** * Access the number of successes. * * @return the number of successes. */ public int getNumberOfSuccesses() { return numberOfSuccesses; } /** * Access the population size. * * @return the population size. */ public int getPopulationSize() { return populationSize; } /** * Access the sample size. * * @return the sample size. */ public int getSampleSize() { return sampleSize; } /** * Return the highest domain value for the given hypergeometric distribution * parameters. * * @param m Number of successes in the population. * @param k Sample size. * @return the highest domain value of the hypergeometric distribution. */ private int getUpperDomain(int m, int k) { return FastMath.min(k, m); } /** {@inheritDoc} */ public double probability(int x) { double ret; int[] domain = getDomain(populationSize, numberOfSuccesses, sampleSize); if (x < domain[0] || x > domain[1]) { ret = 0.0; } else { double p = (double) sampleSize / (double) populationSize; double q = (double) (populationSize - sampleSize) / (double) populationSize; double p1 = SaddlePointExpansion.logBinomialProbability(x, numberOfSuccesses, p, q); double p2 = SaddlePointExpansion.logBinomialProbability(sampleSize - x, populationSize - numberOfSuccesses, p, q); double p3 = SaddlePointExpansion.logBinomialProbability(sampleSize, populationSize, p, q); ret = FastMath.exp(p1 + p2 - p3); } return ret; } /** * For this distribution, {@code X}, this method returns {@code P(X >= x)}. * * @param x Value at which the CDF is evaluated. * @return the upper tail CDF for this distribution. * @since 1.1 */ public double upperCumulativeProbability(int x) { double ret; final int[] domain = getDomain(populationSize, numberOfSuccesses, sampleSize); if (x <= domain[0]) { ret = 1.0; } else if (x > domain[1]) { ret = 0.0; } else { ret = innerCumulativeProbability(domain[1], x, -1); } return ret; } /** * For this distribution, {@code X}, this method returns * {@code P(x0 <= X <= x1)}. * This probability is computed by summing the point probabilities for the * values {@code x0, x0 + 1, x0 + 2, ..., x1}, in the order directed by * {@code dx}. * * @param x0 Inclusive lower bound. * @param x1 Inclusive upper bound. * @param dx Direction of summation (1 indicates summing from x0 to x1, and * 0 indicates summing from x1 to x0). * @return {@code P(x0 <= X <= x1)}. */ private double innerCumulativeProbability(int x0, int x1, int dx) { double ret = probability(x0); while (x0 != x1) { x0 += dx; ret += probability(x0); } return ret; } /** * {@inheritDoc} * * For population size {@code N}, number of successes {@code m}, and sample * size {@code n}, the mean is {@code n * m / N}. */ public double getNumericalMean() { return (double) (getSampleSize() * getNumberOfSuccesses()) / (double) getPopulationSize(); } /** * {@inheritDoc} * * For population size {@code N}, number of successes {@code m}, and sample * size {@code n}, the variance is * {@code [n * m * (N - n) * (N - m)] / [N^2 * (N - 1)]}. */ public double getNumericalVariance() { if (!numericalVarianceIsCalculated) { numericalVariance = calculateNumericalVariance(); numericalVarianceIsCalculated = true; } return numericalVariance; } /** * Used by {@link #getNumericalVariance()}. * * @return the variance of this distribution */ protected double calculateNumericalVariance() { final double N = getPopulationSize(); final double m = getNumberOfSuccesses(); final double n = getSampleSize(); return (n * m * (N - n) * (N - m)) / (N * N * (N - 1)); } /** * {@inheritDoc} * * For population size {@code N}, number of successes {@code m}, and sample * size {@code n}, the lower bound of the support is * {@code max(0, n + m - N)}. * * @return lower bound of the support */ public int getSupportLowerBound() { return FastMath.max(0, getSampleSize() + getNumberOfSuccesses() - getPopulationSize()); } /** * {@inheritDoc} * * For number of successes {@code m} and sample size {@code n}, the upper * bound of the support is {@code min(m, n)}. * * @return upper bound of the support */ public int getSupportUpperBound() { return FastMath.min(getNumberOfSuccesses(), getSampleSize()); } /** * {@inheritDoc} * * The support of this distribution is connected. * * @return {@code true} */ public boolean isSupportConnected() { return true; } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/distribution/GammaDistribution.java100644 1750 1750 34656 12126627700 31561 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.distribution; import org.apache.commons.math3.exception.NotStrictlyPositiveException; import org.apache.commons.math3.exception.util.LocalizedFormats; import org.apache.commons.math3.special.Gamma; import org.apache.commons.math3.util.FastMath; import org.apache.commons.math3.random.RandomGenerator; import org.apache.commons.math3.random.Well19937c; /** * Implementation of the Gamma distribution. * * @see Gamma distribution (Wikipedia) * @see Gamma distribution (MathWorld) * @version $Id: GammaDistribution.java 1422195 2012-12-15 06:45:18Z psteitz $ */ public class GammaDistribution extends AbstractRealDistribution { /** * Default inverse cumulative probability accuracy. * @since 2.1 */ public static final double DEFAULT_INVERSE_ABSOLUTE_ACCURACY = 1e-9; /** Serializable version identifier. */ private static final long serialVersionUID = 20120524L; /** The shape parameter. */ private final double shape; /** The scale parameter. */ private final double scale; /** * The constant value of {@code shape + g + 0.5}, where {@code g} is the * Lanczos constant {@link Gamma#LANCZOS_G}. */ private final double shiftedShape; /** * The constant value of * {@code shape / scale * sqrt(e / (2 * pi * (shape + g + 0.5))) / L(shape)}, * where {@code L(shape)} is the Lanczos approximation returned by * {@link Gamma#lanczos(double)}. This prefactor is used in * {@link #density(double)}, when no overflow occurs with the natural * calculation. */ private final double densityPrefactor1; /** * The constant value of * {@code shape * sqrt(e / (2 * pi * (shape + g + 0.5))) / L(shape)}, * where {@code L(shape)} is the Lanczos approximation returned by * {@link Gamma#lanczos(double)}. This prefactor is used in * {@link #density(double)}, when overflow occurs with the natural * calculation. */ private final double densityPrefactor2; /** * Lower bound on {@code y = x / scale} for the selection of the computation * method in {@link #density(double)}. For {@code y <= minY}, the natural * calculation overflows. */ private final double minY; /** * Upper bound on {@code log(y)} ({@code y = x / scale}) for the selection * of the computation method in {@link #density(double)}. For * {@code log(y) >= maxLogY}, the natural calculation overflows. */ private final double maxLogY; /** Inverse cumulative probability accuracy. */ private final double solverAbsoluteAccuracy; /** * Creates a new gamma distribution with specified values of the shape and * scale parameters. * * @param shape the shape parameter * @param scale the scale parameter * @throws NotStrictlyPositiveException if {@code shape <= 0} or * {@code scale <= 0}. */ public GammaDistribution(double shape, double scale) throws NotStrictlyPositiveException { this(shape, scale, DEFAULT_INVERSE_ABSOLUTE_ACCURACY); } /** * Creates a new gamma distribution with specified values of the shape and * scale parameters. * * @param shape the shape parameter * @param scale the scale parameter * @param inverseCumAccuracy the maximum absolute error in inverse * cumulative probability estimates (defaults to * {@link #DEFAULT_INVERSE_ABSOLUTE_ACCURACY}). * @throws NotStrictlyPositiveException if {@code shape <= 0} or * {@code scale <= 0}. * @since 2.1 */ public GammaDistribution(double shape, double scale, double inverseCumAccuracy) throws NotStrictlyPositiveException { this(new Well19937c(), shape, scale, inverseCumAccuracy); } /** * Creates a Gamma distribution. * * @param rng Random number generator. * @param shape the shape parameter * @param scale the scale parameter * @param inverseCumAccuracy the maximum absolute error in inverse * cumulative probability estimates (defaults to * {@link #DEFAULT_INVERSE_ABSOLUTE_ACCURACY}). * @throws NotStrictlyPositiveException if {@code shape <= 0} or * {@code scale <= 0}. * @since 3.1 */ public GammaDistribution(RandomGenerator rng, double shape, double scale, double inverseCumAccuracy) throws NotStrictlyPositiveException { super(rng); if (shape <= 0) { throw new NotStrictlyPositiveException(LocalizedFormats.SHAPE, shape); } if (scale <= 0) { throw new NotStrictlyPositiveException(LocalizedFormats.SCALE, scale); } this.shape = shape; this.scale = scale; this.solverAbsoluteAccuracy = inverseCumAccuracy; this.shiftedShape = shape + Gamma.LANCZOS_G + 0.5; final double aux = FastMath.E / (2.0 * FastMath.PI * shiftedShape); this.densityPrefactor2 = shape * FastMath.sqrt(aux) / Gamma.lanczos(shape); this.densityPrefactor1 = this.densityPrefactor2 / scale * FastMath.pow(shiftedShape, -shape) * FastMath.exp(shape + Gamma.LANCZOS_G); this.minY = shape + Gamma.LANCZOS_G - FastMath.log(Double.MAX_VALUE); this.maxLogY = FastMath.log(Double.MAX_VALUE) / (shape - 1.0); } /** * Returns the shape parameter of {@code this} distribution. * * @return the shape parameter * @deprecated as of version 3.1, {@link #getShape()} should be preferred. * This method will be removed in version 4.0. */ @Deprecated public double getAlpha() { return shape; } /** * Returns the shape parameter of {@code this} distribution. * * @return the shape parameter * @since 3.1 */ public double getShape() { return shape; } /** * Returns the scale parameter of {@code this} distribution. * * @return the scale parameter * @deprecated as of version 3.1, {@link #getScale()} should be preferred. * This method will be removed in version 4.0. */ @Deprecated public double getBeta() { return scale; } /** * Returns the scale parameter of {@code this} distribution. * * @return the scale parameter * @since 3.1 */ public double getScale() { return scale; } /** {@inheritDoc} */ public double density(double x) { /* The present method must return the value of * * 1 x a - x * ---------- (-) exp(---) * x Gamma(a) b b * * where a is the shape parameter, and b the scale parameter. * Substituting the Lanczos approximation of Gamma(a) leads to the * following expression of the density * * a e 1 y a * - sqrt(------------------) ---- (-----------) exp(a - y + g), * x 2 pi (a + g + 0.5) L(a) a + g + 0.5 * * where y = x / b. The above formula is the "natural" computation, which * is implemented when no overflow is likely to occur. If overflow occurs * with the natural computation, the following identity is used. It is * based on the BOOST library * http://www.boost.org/doc/libs/1_35_0/libs/math/doc/sf_and_dist/html/math_toolkit/special/sf_gamma/igamma.html * Formula (15) needs adaptations, which are detailed below. * * y a * (-----------) exp(a - y + g) * a + g + 0.5 * y - a - g - 0.5 y (g + 0.5) * = exp(a log1pm(---------------) - ----------- + g), * a + g + 0.5 a + g + 0.5 * * where log1pm(z) = log(1 + z) - z. Therefore, the value to be * returned is * * a e 1 * - sqrt(------------------) ---- * x 2 pi (a + g + 0.5) L(a) * y - a - g - 0.5 y (g + 0.5) * * exp(a log1pm(---------------) - ----------- + g). * a + g + 0.5 a + g + 0.5 */ if (x < 0) { return 0; } final double y = x / scale; if ((y <= minY) || (FastMath.log(y) >= maxLogY)) { /* * Overflow. */ final double aux1 = (y - shiftedShape) / shiftedShape; final double aux2 = shape * (FastMath.log1p(aux1) - aux1); final double aux3 = -y * (Gamma.LANCZOS_G + 0.5) / shiftedShape + Gamma.LANCZOS_G + aux2; return densityPrefactor2 / x * FastMath.exp(aux3); } /* * Natural calculation. */ return densityPrefactor1 * FastMath.exp(-y) * FastMath.pow(y, shape - 1); } /** * {@inheritDoc} * * The implementation of this method is based on: *
                                *
                              • * * Chi-Squared Distribution, equation (9). *
                              • *
                              • Casella, G., & Berger, R. (1990). Statistical Inference. * Belmont, CA: Duxbury Press. *
                              • *
                              */ public double cumulativeProbability(double x) { double ret; if (x <= 0) { ret = 0; } else { ret = Gamma.regularizedGammaP(shape, x / scale); } return ret; } /** {@inheritDoc} */ @Override protected double getSolverAbsoluteAccuracy() { return solverAbsoluteAccuracy; } /** * {@inheritDoc} * * For shape parameter {@code alpha} and scale parameter {@code beta}, the * mean is {@code alpha * beta}. */ public double getNumericalMean() { return shape * scale; } /** * {@inheritDoc} * * For shape parameter {@code alpha} and scale parameter {@code beta}, the * variance is {@code alpha * beta^2}. * * @return {@inheritDoc} */ public double getNumericalVariance() { return shape * scale * scale; } /** * {@inheritDoc} * * The lower bound of the support is always 0 no matter the parameters. * * @return lower bound of the support (always 0) */ public double getSupportLowerBound() { return 0; } /** * {@inheritDoc} * * The upper bound of the support is always positive infinity * no matter the parameters. * * @return upper bound of the support (always Double.POSITIVE_INFINITY) */ public double getSupportUpperBound() { return Double.POSITIVE_INFINITY; } /** {@inheritDoc} */ public boolean isSupportLowerBoundInclusive() { return true; } /** {@inheritDoc} */ public boolean isSupportUpperBoundInclusive() { return false; } /** * {@inheritDoc} * * The support of this distribution is connected. * * @return {@code true} */ public boolean isSupportConnected() { return true; } /** *

                              This implementation uses the following algorithms:

                              * *

                              For 0 < shape < 1:
                              * Ahrens, J. H. and Dieter, U., Computer methods for * sampling from gamma, beta, Poisson and binomial distributions. * Computing, 12, 223-246, 1974.

                              * *

                              For shape >= 1:
                              * Marsaglia and Tsang, A Simple Method for Generating * Gamma Variables. ACM Transactions on Mathematical Software, * Volume 26 Issue 3, September, 2000.

                              * * @return random value sampled from the Gamma(shape, scale) distribution */ @Override public double sample() { if (shape < 1) { // [1]: p. 228, Algorithm GS while (true) { // Step 1: final double u = random.nextDouble(); final double bGS = 1 + shape / FastMath.E; final double p = bGS * u; if (p <= 1) { // Step 2: final double x = FastMath.pow(p, 1 / shape); final double u2 = random.nextDouble(); if (u2 > FastMath.exp(-x)) { // Reject continue; } else { return scale * x; } } else { // Step 3: final double x = -1 * FastMath.log((bGS - p) / shape); final double u2 = random.nextDouble(); if (u2 > FastMath.pow(x, shape - 1)) { // Reject continue; } else { return scale * x; } } } } // Now shape >= 1 final double d = shape - 0.333333333333333333; final double c = 1 / (3 * FastMath.sqrt(d)); while (true) { final double x = random.nextGaussian(); final double v = (1 + c * x) * (1 + c * x) * (1 + c * x); if (v <= 0) { continue; } final double x2 = x * x; final double u = random.nextDouble(); // Squeeze if (u < 1 - 0.0331 * x2 * x2) { return scale * d * v; } if (FastMath.log(u) < 0.5 * x2 + d * (1 - v + FastMath.log(v))) { return scale * d * v; } } } } ././@LongLink100644 0 0 146 12126630647 10263 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/distribution/TriangularDistribution.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/distribution/TriangularDistribution.jav100644 1750 1750 21111 12126627700 32464 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.distribution; import org.apache.commons.math3.exception.NumberIsTooLargeException; import org.apache.commons.math3.exception.NumberIsTooSmallException; import org.apache.commons.math3.exception.OutOfRangeException; import org.apache.commons.math3.exception.util.LocalizedFormats; import org.apache.commons.math3.util.FastMath; import org.apache.commons.math3.random.RandomGenerator; import org.apache.commons.math3.random.Well19937c; /** * Implementation of the triangular real distribution. * * @see * Triangular distribution (Wikipedia) * * @version $Id: TriangularDistribution.java 1416643 2012-12-03 19:37:14Z tn $ * @since 3.0 */ public class TriangularDistribution extends AbstractRealDistribution { /** Serializable version identifier. */ private static final long serialVersionUID = 20120112L; /** Lower limit of this distribution (inclusive). */ private final double a; /** Upper limit of this distribution (inclusive). */ private final double b; /** Mode of this distribution. */ private final double c; /** Inverse cumulative probability accuracy. */ private final double solverAbsoluteAccuracy; /** * Creates a triangular real distribution using the given lower limit, * upper limit, and mode. * * @param a Lower limit of this distribution (inclusive). * @param b Upper limit of this distribution (inclusive). * @param c Mode of this distribution. * @throws NumberIsTooLargeException if {@code a >= b} or if {@code c > b}. * @throws NumberIsTooSmallException if {@code c < a}. */ public TriangularDistribution(double a, double c, double b) throws NumberIsTooLargeException, NumberIsTooSmallException { this(new Well19937c(), a, c, b); } /** * Creates a triangular distribution. * * @param rng Random number generator. * @param a Lower limit of this distribution (inclusive). * @param b Upper limit of this distribution (inclusive). * @param c Mode of this distribution. * @throws NumberIsTooLargeException if {@code a >= b} or if {@code c > b}. * @throws NumberIsTooSmallException if {@code c < a}. * @since 3.1 */ public TriangularDistribution(RandomGenerator rng, double a, double c, double b) throws NumberIsTooLargeException, NumberIsTooSmallException { super(rng); if (a >= b) { throw new NumberIsTooLargeException( LocalizedFormats.LOWER_BOUND_NOT_BELOW_UPPER_BOUND, a, b, false); } if (c < a) { throw new NumberIsTooSmallException( LocalizedFormats.NUMBER_TOO_SMALL, c, a, true); } if (c > b) { throw new NumberIsTooLargeException( LocalizedFormats.NUMBER_TOO_LARGE, c, b, true); } this.a = a; this.c = c; this.b = b; solverAbsoluteAccuracy = FastMath.max(FastMath.ulp(a), FastMath.ulp(b)); } /** * Returns the mode {@code c} of this distribution. * * @return the mode {@code c} of this distribution */ public double getMode() { return c; } /** * {@inheritDoc} * *

                              * For this distribution, the returned value is not really meaningful, * since exact formulas are implemented for the computation of the * {@link #inverseCumulativeProbability(double)} (no solver is invoked). *

                              *

                              * For lower limit {@code a} and upper limit {@code b}, the current * implementation returns {@code max(ulp(a), ulp(b)}. *

                              */ @Override protected double getSolverAbsoluteAccuracy() { return solverAbsoluteAccuracy; } /** * {@inheritDoc} * * For lower limit {@code a}, upper limit {@code b} and mode {@code c}, the * PDF is given by *
                                *
                              • {@code 2 * (x - a) / [(b - a) * (c - a)]} if {@code a <= x < c},
                              • *
                              • {@code 2 / (b - a)} if {@code x = c},
                              • *
                              • {@code 2 * (b - x) / [(b - a) * (b - c)]} if {@code c < x <= b},
                              • *
                              • {@code 0} otherwise. *
                              */ public double density(double x) { if (x < a) { return 0; } if (a <= x && x < c) { double divident = 2 * (x - a); double divisor = (b - a) * (c - a); return divident / divisor; } if (x == c) { return 2 / (b - a); } if (c < x && x <= b) { double divident = 2 * (b - x); double divisor = (b - a) * (b - c); return divident / divisor; } return 0; } /** * {@inheritDoc} * * For lower limit {@code a}, upper limit {@code b} and mode {@code c}, the * CDF is given by *
                                *
                              • {@code 0} if {@code x < a},
                              • *
                              • {@code (x - a)^2 / [(b - a) * (c - a)]} if {@code a <= x < c},
                              • *
                              • {@code (c - a) / (b - a)} if {@code x = c},
                              • *
                              • {@code 1 - (b - x)^2 / [(b - a) * (b - c)]} if {@code c < x <= b},
                              • *
                              • {@code 1} if {@code x > b}.
                              • *
                              */ public double cumulativeProbability(double x) { if (x < a) { return 0; } if (a <= x && x < c) { double divident = (x - a) * (x - a); double divisor = (b - a) * (c - a); return divident / divisor; } if (x == c) { return (c - a) / (b - a); } if (c < x && x <= b) { double divident = (b - x) * (b - x); double divisor = (b - a) * (b - c); return 1 - (divident / divisor); } return 1; } /** * {@inheritDoc} * * For lower limit {@code a}, upper limit {@code b}, and mode {@code c}, * the mean is {@code (a + b + c) / 3}. */ public double getNumericalMean() { return (a + b + c) / 3; } /** * {@inheritDoc} * * For lower limit {@code a}, upper limit {@code b}, and mode {@code c}, * the variance is {@code (a^2 + b^2 + c^2 - a * b - a * c - b * c) / 18}. */ public double getNumericalVariance() { return (a * a + b * b + c * c - a * b - a * c - b * c) / 18; } /** * {@inheritDoc} * * The lower bound of the support is equal to the lower limit parameter * {@code a} of the distribution. * * @return lower bound of the support */ public double getSupportLowerBound() { return a; } /** * {@inheritDoc} * * The upper bound of the support is equal to the upper limit parameter * {@code b} of the distribution. * * @return upper bound of the support */ public double getSupportUpperBound() { return b; } /** {@inheritDoc} */ public boolean isSupportLowerBoundInclusive() { return true; } /** {@inheritDoc} */ public boolean isSupportUpperBoundInclusive() { return true; } /** * {@inheritDoc} * * The support of this distribution is connected. * * @return {@code true} */ public boolean isSupportConnected() { return true; } @Override public double inverseCumulativeProbability(double p) throws OutOfRangeException { if (p < 0 || p > 1) { throw new OutOfRangeException(p, 0, 1); } if (p == 0) { return a; } if (p == 1) { return b; } if (p < (c - a) / (b - a)) { return a + FastMath.sqrt(p * (b - a) * (c - a)); } return b - FastMath.sqrt((1 - p) * (b - a) * (b - c)); } } ././@LongLink100644 0 0 150 12126630647 10256 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/distribution/AbstractRealDistribution.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/distribution/AbstractRealDistribution.j100644 1750 1750 24140 12126627700 32401 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.distribution; import java.io.Serializable; import org.apache.commons.math3.analysis.UnivariateFunction; import org.apache.commons.math3.analysis.solvers.UnivariateSolverUtils; import org.apache.commons.math3.exception.NotStrictlyPositiveException; import org.apache.commons.math3.exception.NumberIsTooLargeException; import org.apache.commons.math3.exception.OutOfRangeException; import org.apache.commons.math3.exception.util.LocalizedFormats; import org.apache.commons.math3.random.RandomGenerator; import org.apache.commons.math3.random.RandomDataImpl; import org.apache.commons.math3.util.FastMath; /** * Base class for probability distributions on the reals. * Default implementations are provided for some of the methods * that do not vary from distribution to distribution. * * @version $Id: AbstractRealDistribution.java 1422195 2012-12-15 06:45:18Z psteitz $ * @since 3.0 */ public abstract class AbstractRealDistribution implements RealDistribution, Serializable { /** Default accuracy. */ public static final double SOLVER_DEFAULT_ABSOLUTE_ACCURACY = 1e-6; /** Serializable version identifier */ private static final long serialVersionUID = -38038050983108802L; /** * RandomData instance used to generate samples from the distribution. * @deprecated As of 3.1, to be removed in 4.0. Please use the * {@link #random} instance variable instead. */ @Deprecated protected RandomDataImpl randomData = new RandomDataImpl(); /** * RNG instance used to generate samples from the distribution. * @since 3.1 */ protected final RandomGenerator random; /** Solver absolute accuracy for inverse cumulative computation */ private double solverAbsoluteAccuracy = SOLVER_DEFAULT_ABSOLUTE_ACCURACY; /** * @deprecated As of 3.1, to be removed in 4.0. Please use * {@link #AbstractRealDistribution(RandomGenerator)} instead. */ @Deprecated protected AbstractRealDistribution() { // Legacy users are only allowed to access the deprecated "randomData". // New users are forbidden to use this constructor. random = null; } /** * @param rng Random number generator. * @since 3.1 */ protected AbstractRealDistribution(RandomGenerator rng) { random = rng; } /** * {@inheritDoc} * * The default implementation uses the identity *

                              {@code P(x0 < X <= x1) = P(X <= x1) - P(X <= x0)}

                              * * @deprecated As of 3.1 (to be removed in 4.0). Please use * {@link #probability(double,double)} instead. */ @Deprecated public double cumulativeProbability(double x0, double x1) throws NumberIsTooLargeException { return probability(x0, x1); } /** * For a random variable {@code X} whose values are distributed according * to this distribution, this method returns {@code P(x0 < X <= x1)}. * * @param x0 Lower bound (excluded). * @param x1 Upper bound (included). * @return the probability that a random variable with this distribution * takes a value between {@code x0} and {@code x1}, excluding the lower * and including the upper endpoint. * @throws NumberIsTooLargeException if {@code x0 > x1}. * * The default implementation uses the identity * {@code P(x0 < X <= x1) = P(X <= x1) - P(X <= x0)} * * @since 3.1 */ public double probability(double x0, double x1) { if (x0 > x1) { throw new NumberIsTooLargeException(LocalizedFormats.LOWER_ENDPOINT_ABOVE_UPPER_ENDPOINT, x0, x1, true); } return cumulativeProbability(x1) - cumulativeProbability(x0); } /** * {@inheritDoc} * * The default implementation returns *
                                *
                              • {@link #getSupportLowerBound()} for {@code p = 0},
                              • *
                              • {@link #getSupportUpperBound()} for {@code p = 1}.
                              • *
                              */ public double inverseCumulativeProbability(final double p) throws OutOfRangeException { /* * IMPLEMENTATION NOTES * -------------------- * Where applicable, use is made of the one-sided Chebyshev inequality * to bracket the root. This inequality states that * P(X - mu >= k * sig) <= 1 / (1 + k^2), * mu: mean, sig: standard deviation. Equivalently * 1 - P(X < mu + k * sig) <= 1 / (1 + k^2), * F(mu + k * sig) >= k^2 / (1 + k^2). * * For k = sqrt(p / (1 - p)), we find * F(mu + k * sig) >= p, * and (mu + k * sig) is an upper-bound for the root. * * Then, introducing Y = -X, mean(Y) = -mu, sd(Y) = sig, and * P(Y >= -mu + k * sig) <= 1 / (1 + k^2), * P(-X >= -mu + k * sig) <= 1 / (1 + k^2), * P(X <= mu - k * sig) <= 1 / (1 + k^2), * F(mu - k * sig) <= 1 / (1 + k^2). * * For k = sqrt((1 - p) / p), we find * F(mu - k * sig) <= p, * and (mu - k * sig) is a lower-bound for the root. * * In cases where the Chebyshev inequality does not apply, geometric * progressions 1, 2, 4, ... and -1, -2, -4, ... are used to bracket * the root. */ if (p < 0.0 || p > 1.0) { throw new OutOfRangeException(p, 0, 1); } double lowerBound = getSupportLowerBound(); if (p == 0.0) { return lowerBound; } double upperBound = getSupportUpperBound(); if (p == 1.0) { return upperBound; } final double mu = getNumericalMean(); final double sig = FastMath.sqrt(getNumericalVariance()); final boolean chebyshevApplies; chebyshevApplies = !(Double.isInfinite(mu) || Double.isNaN(mu) || Double.isInfinite(sig) || Double.isNaN(sig)); if (lowerBound == Double.NEGATIVE_INFINITY) { if (chebyshevApplies) { lowerBound = mu - sig * FastMath.sqrt((1. - p) / p); } else { lowerBound = -1.0; while (cumulativeProbability(lowerBound) >= p) { lowerBound *= 2.0; } } } if (upperBound == Double.POSITIVE_INFINITY) { if (chebyshevApplies) { upperBound = mu + sig * FastMath.sqrt(p / (1. - p)); } else { upperBound = 1.0; while (cumulativeProbability(upperBound) < p) { upperBound *= 2.0; } } } final UnivariateFunction toSolve = new UnivariateFunction() { public double value(final double x) { return cumulativeProbability(x) - p; } }; double x = UnivariateSolverUtils.solve(toSolve, lowerBound, upperBound, getSolverAbsoluteAccuracy()); if (!isSupportConnected()) { /* Test for plateau. */ final double dx = getSolverAbsoluteAccuracy(); if (x - dx >= getSupportLowerBound()) { double px = cumulativeProbability(x); if (cumulativeProbability(x - dx) == px) { upperBound = x; while (upperBound - lowerBound > dx) { final double midPoint = 0.5 * (lowerBound + upperBound); if (cumulativeProbability(midPoint) < px) { lowerBound = midPoint; } else { upperBound = midPoint; } } return upperBound; } } } return x; } /** * Returns the solver absolute accuracy for inverse cumulative computation. * You can override this method in order to use a Brent solver with an * absolute accuracy different from the default. * * @return the maximum absolute error in inverse cumulative probability estimates */ protected double getSolverAbsoluteAccuracy() { return solverAbsoluteAccuracy; } /** {@inheritDoc} */ public void reseedRandomGenerator(long seed) { random.setSeed(seed); randomData.reSeed(seed); } /** * {@inheritDoc} * * The default implementation uses the * * inversion method. * */ public double sample() { return inverseCumulativeProbability(random.nextDouble()); } /** * {@inheritDoc} * * The default implementation generates the sample by calling * {@link #sample()} in a loop. */ public double[] sample(int sampleSize) { if (sampleSize <= 0) { throw new NotStrictlyPositiveException(LocalizedFormats.NUMBER_OF_SAMPLES, sampleSize); } double[] out = new double[sampleSize]; for (int i = 0; i < sampleSize; i++) { out[i] = sample(); } return out; } /** * {@inheritDoc} * * @return zero. * @since 3.1 */ public double probability(double x) { return 0d; } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/distribution/BinomialDistribution.java100644 1750 1750 13301 12126627700 32251 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.distribution; import org.apache.commons.math3.exception.OutOfRangeException; import org.apache.commons.math3.exception.NotPositiveException; import org.apache.commons.math3.exception.util.LocalizedFormats; import org.apache.commons.math3.special.Beta; import org.apache.commons.math3.util.FastMath; import org.apache.commons.math3.random.RandomGenerator; import org.apache.commons.math3.random.Well19937c; /** * Implementation of the binomial distribution. * * @see Binomial distribution (Wikipedia) * @see Binomial Distribution (MathWorld) * @version $Id: BinomialDistribution.java 1416643 2012-12-03 19:37:14Z tn $ */ public class BinomialDistribution extends AbstractIntegerDistribution { /** Serializable version identifier. */ private static final long serialVersionUID = 6751309484392813623L; /** The number of trials. */ private final int numberOfTrials; /** The probability of success. */ private final double probabilityOfSuccess; /** * Create a binomial distribution with the given number of trials and * probability of success. * * @param trials Number of trials. * @param p Probability of success. * @throws NotPositiveException if {@code trials < 0}. * @throws OutOfRangeException if {@code p < 0} or {@code p > 1}. */ public BinomialDistribution(int trials, double p) { this(new Well19937c(), trials, p); } /** * Creates a binomial distribution. * * @param rng Random number generator. * @param trials Number of trials. * @param p Probability of success. * @throws NotPositiveException if {@code trials < 0}. * @throws OutOfRangeException if {@code p < 0} or {@code p > 1}. * @since 3.1 */ public BinomialDistribution(RandomGenerator rng, int trials, double p) { super(rng); if (trials < 0) { throw new NotPositiveException(LocalizedFormats.NUMBER_OF_TRIALS, trials); } if (p < 0 || p > 1) { throw new OutOfRangeException(p, 0, 1); } probabilityOfSuccess = p; numberOfTrials = trials; } /** * Access the number of trials for this distribution. * * @return the number of trials. */ public int getNumberOfTrials() { return numberOfTrials; } /** * Access the probability of success for this distribution. * * @return the probability of success. */ public double getProbabilityOfSuccess() { return probabilityOfSuccess; } /** {@inheritDoc} */ public double probability(int x) { double ret; if (x < 0 || x > numberOfTrials) { ret = 0.0; } else { ret = FastMath.exp(SaddlePointExpansion.logBinomialProbability(x, numberOfTrials, probabilityOfSuccess, 1.0 - probabilityOfSuccess)); } return ret; } /** {@inheritDoc} */ public double cumulativeProbability(int x) { double ret; if (x < 0) { ret = 0.0; } else if (x >= numberOfTrials) { ret = 1.0; } else { ret = 1.0 - Beta.regularizedBeta(probabilityOfSuccess, x + 1.0, numberOfTrials - x); } return ret; } /** * {@inheritDoc} * * For {@code n} trials and probability parameter {@code p}, the mean is * {@code n * p}. */ public double getNumericalMean() { return numberOfTrials * probabilityOfSuccess; } /** * {@inheritDoc} * * For {@code n} trials and probability parameter {@code p}, the variance is * {@code n * p * (1 - p)}. */ public double getNumericalVariance() { final double p = probabilityOfSuccess; return numberOfTrials * p * (1 - p); } /** * {@inheritDoc} * * The lower bound of the support is always 0 except for the probability * parameter {@code p = 1}. * * @return lower bound of the support (0 or the number of trials) */ public int getSupportLowerBound() { return probabilityOfSuccess < 1.0 ? 0 : numberOfTrials; } /** * {@inheritDoc} * * The upper bound of the support is the number of trials except for the * probability parameter {@code p = 0}. * * @return upper bound of the support (number of trials or 0) */ public int getSupportUpperBound() { return probabilityOfSuccess > 0.0 ? numberOfTrials : 0; } /** * {@inheritDoc} * * The support of this distribution is connected. * * @return {@code true} */ public boolean isSupportConnected() { return true; } } ././@LongLink100644 0 0 152 12126630647 10260 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/distribution/EnumeratedRealDistribution.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/distribution/EnumeratedRealDistribution100644 1750 1750 20121 12126627700 32472 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.distribution; import java.util.ArrayList; import java.util.List; import org.apache.commons.math3.exception.DimensionMismatchException; import org.apache.commons.math3.exception.MathArithmeticException; import org.apache.commons.math3.exception.NotANumberException; import org.apache.commons.math3.exception.NotFiniteNumberException; import org.apache.commons.math3.exception.NotPositiveException; import org.apache.commons.math3.random.RandomGenerator; import org.apache.commons.math3.random.Well19937c; import org.apache.commons.math3.util.Pair; /** *

                              Implementation of a real-valued {@link EnumeratedDistribution}. * *

                              Values with zero-probability are allowed but they do not extend the * support.
                              * Duplicate values are allowed. Probabilities of duplicate values are combined * when computing cumulative probabilities and statistics.

                              * * @version $Id: EnumeratedRealDistribution.java 1456769 2013-03-15 04:51:34Z psteitz $ * @since 3.2 */ public class EnumeratedRealDistribution extends AbstractRealDistribution { /** Serializable UID. */ private static final long serialVersionUID = 20130308L; /** * {@link EnumeratedDistribution} (using the {@link Double} wrapper) * used to generate the pmf. */ protected final EnumeratedDistribution innerDistribution; /** * Create a discrete distribution using the given probability mass function * enumeration. * * @param singletons array of random variable values. * @param probabilities array of probabilities. * @throws DimensionMismatchException if * {@code singletons.length != probabilities.length} * @throws NotPositiveException if any of the probabilities are negative. * @throws NotFiniteNumberException if any of the probabilities are infinite. * @throws NotANumberException if any of the probabilities are NaN. * @throws MathArithmeticException all of the probabilities are 0. */ public EnumeratedRealDistribution(final double[] singletons, final double[] probabilities) throws DimensionMismatchException, NotPositiveException, MathArithmeticException, NotFiniteNumberException, NotANumberException { this(new Well19937c(), singletons, probabilities); } /** * Create a discrete distribution using the given random number generator * and probability mass function enumeration. * * @param rng random number generator. * @param singletons array of random variable values. * @param probabilities array of probabilities. * @throws DimensionMismatchException if * {@code singletons.length != probabilities.length} * @throws NotPositiveException if any of the probabilities are negative. * @throws NotFiniteNumberException if any of the probabilities are infinite. * @throws NotANumberException if any of the probabilities are NaN. * @throws MathArithmeticException all of the probabilities are 0. */ public EnumeratedRealDistribution(final RandomGenerator rng, final double[] singletons, final double[] probabilities) throws DimensionMismatchException, NotPositiveException, MathArithmeticException, NotFiniteNumberException, NotANumberException { super(rng); if (singletons.length != probabilities.length) { throw new DimensionMismatchException(probabilities.length, singletons.length); } List> samples = new ArrayList>(singletons.length); for (int i = 0; i < singletons.length; i++) { samples.add(new Pair(singletons[i], probabilities[i])); } innerDistribution = new EnumeratedDistribution(rng, samples); } /** * {@inheritDoc} */ @Override public double probability(final double x) { return innerDistribution.probability(x); } /** * For a random variable {@code X} whose values are distributed according to * this distribution, this method returns {@code P(X = x)}. In other words, * this method represents the probability mass function (PMF) for the * distribution. * * @param x the point at which the PMF is evaluated * @return the value of the probability mass function at point {@code x} */ public double density(final double x) { return probability(x); } /** * {@inheritDoc} */ public double cumulativeProbability(final double x) { double probability = 0; for (final Pair sample : innerDistribution.getPmf()) { if (sample.getKey() <= x) { probability += sample.getValue(); } } return probability; } /** * {@inheritDoc} * * @return {@code sum(singletons[i] * probabilities[i])} */ public double getNumericalMean() { double mean = 0; for (final Pair sample : innerDistribution.getPmf()) { mean += sample.getValue() * sample.getKey(); } return mean; } /** * {@inheritDoc} * * @return {@code sum((singletons[i] - mean) ^ 2 * probabilities[i])} */ public double getNumericalVariance() { double mean = 0; double meanOfSquares = 0; for (final Pair sample : innerDistribution.getPmf()) { mean += sample.getValue() * sample.getKey(); meanOfSquares += sample.getValue() * sample.getKey() * sample.getKey(); } return meanOfSquares - mean * mean; } /** * {@inheritDoc} * * Returns the lowest value with non-zero probability. * * @return the lowest value with non-zero probability. */ public double getSupportLowerBound() { double min = Double.POSITIVE_INFINITY; for (final Pair sample : innerDistribution.getPmf()) { if (sample.getKey() < min && sample.getValue() > 0) { min = sample.getKey(); } } return min; } /** * {@inheritDoc} * * Returns the highest value with non-zero probability. * * @return the highest value with non-zero probability. */ public double getSupportUpperBound() { double max = Double.NEGATIVE_INFINITY; for (final Pair sample : innerDistribution.getPmf()) { if (sample.getKey() > max && sample.getValue() > 0) { max = sample.getKey(); } } return max; } /** * {@inheritDoc} * * The support of this distribution includes the lower bound. * * @return {@code true} */ public boolean isSupportLowerBoundInclusive() { return true; } /** * {@inheritDoc} * * The support of this distribution includes the upper bound. * * @return {@code true} */ public boolean isSupportUpperBoundInclusive() { return true; } /** * {@inheritDoc} * * The support of this distribution is connected. * * @return {@code true} */ public boolean isSupportConnected() { return true; } /** * {@inheritDoc} */ @Override public double sample() { return innerDistribution.sample(); } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/distribution/LevyDistribution.java100644 1750 1750 11332 12126627700 31440 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.distribution; import org.apache.commons.math3.exception.OutOfRangeException; import org.apache.commons.math3.random.RandomGenerator; import org.apache.commons.math3.special.Erf; import org.apache.commons.math3.util.FastMath; /** * This class implements the * Lévy distribution. * * @version $Id: LevyDistribution.java 1456957 2013-03-15 13:54:52Z luc $ * @since 3.2 */ public class LevyDistribution extends AbstractRealDistribution { /** Serializable UID. */ private static final long serialVersionUID = 20130314L; /** Location parameter. */ private final double mu; /** Scale parameter. */ private final double c; // Setting this to 1 returns a cumProb of 1.0 /** Half of c (for calculations). */ private final double halfC; /** * Creates a LevyDistribution. * @param rng random generator to be used for sampling * @param mu location * @param c scale parameter */ public LevyDistribution(final RandomGenerator rng, final double mu, final double c) { super(rng); this.mu = mu; this.c = c; this.halfC = 0.5 * c; } /** {@inheritDoc} *

                              * From Wikipedia: The probability density function of the Lévy distribution * over the domain is *

                              *
                                  * f(x; μ, c) = √(c / 2π) * e-c / 2 (x - μ) / (x - μ)3/2
                                  * 
                              *

                              * For this distribution, {@code X}, this method returns {@code P(X < x)}. * If {@code x} is less than location parameter μ, {@code Double.NaN} is * returned, as in these cases the distribution is not defined. *

                              */ public double density(final double x) { if (x < mu) { return Double.NaN; } final double delta = x - mu; final double f = halfC / delta; return FastMath.sqrt(f / FastMath.PI) * FastMath.exp(-f) /delta; } /** {@inheritDoc} *

                              * From Wikipedia: the cumulative distribution function is *

                              *
                                   * f(x; u, c) = erfc (√ (c / 2 (x - u )))
                                   * 
                              */ public double cumulativeProbability(final double x) { if (x < mu) { return Double.NaN; } return Erf.erfc(FastMath.sqrt(halfC / (x - mu))); } /** {@inheritDoc} */ @Override public double inverseCumulativeProbability(final double p) throws OutOfRangeException { if (p < 0.0 || p > 1.0) { throw new OutOfRangeException(p, 0, 1); } final double t = Erf.erfcInv(p); return mu + halfC / (t * t); } /** Get the scale parameter of the distribution. * @return scale parameter of the distribution */ public double getScale() { return c; } /** Get the location parameter of the distribution. * @return location parameter of the distribution */ public double getLocation() { return mu; } /** {@inheritDoc} */ public double getNumericalMean() { return Double.POSITIVE_INFINITY; } /** {@inheritDoc} */ public double getNumericalVariance() { return Double.POSITIVE_INFINITY; } /** {@inheritDoc} */ public double getSupportLowerBound() { return mu; } /** {@inheritDoc} */ public double getSupportUpperBound() { return Double.POSITIVE_INFINITY; } /** {@inheritDoc} */ public boolean isSupportLowerBoundInclusive() { // there is a division by x-mu in the computation, so density // is not finite at lower bound, bound must be excluded return false; } /** {@inheritDoc} */ public boolean isSupportUpperBoundInclusive() { // upper bound is infinite, so it must be excluded return false; } /** {@inheritDoc} */ public boolean isSupportConnected() { return true; } } ././@LongLink100644 0 0 153 12126630647 10261 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/distribution/AbstractIntegerDistribution.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/distribution/AbstractIntegerDistributio100644 1750 1750 20516 12126627700 32510 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.distribution; import java.io.Serializable; import org.apache.commons.math3.exception.MathInternalError; import org.apache.commons.math3.exception.NotStrictlyPositiveException; import org.apache.commons.math3.exception.NumberIsTooLargeException; import org.apache.commons.math3.exception.OutOfRangeException; import org.apache.commons.math3.exception.util.LocalizedFormats; import org.apache.commons.math3.random.RandomGenerator; import org.apache.commons.math3.random.RandomDataImpl; import org.apache.commons.math3.util.FastMath; /** * Base class for integer-valued discrete distributions. Default * implementations are provided for some of the methods that do not vary * from distribution to distribution. * * @version $Id: AbstractIntegerDistribution.java 1455716 2013-03-12 21:12:21Z tn $ */ public abstract class AbstractIntegerDistribution implements IntegerDistribution, Serializable { /** Serializable version identifier */ private static final long serialVersionUID = -1146319659338487221L; /** * RandomData instance used to generate samples from the distribution. * @deprecated As of 3.1, to be removed in 4.0. Please use the * {@link #random} instance variable instead. */ @Deprecated protected final RandomDataImpl randomData = new RandomDataImpl(); /** * RNG instance used to generate samples from the distribution. * @since 3.1 */ protected final RandomGenerator random; /** * @deprecated As of 3.1, to be removed in 4.0. Please use * {@link #AbstractIntegerDistribution(RandomGenerator)} instead. */ @Deprecated protected AbstractIntegerDistribution() { // Legacy users are only allowed to access the deprecated "randomData". // New users are forbidden to use this constructor. random = null; } /** * @param rng Random number generator. * @since 3.1 */ protected AbstractIntegerDistribution(RandomGenerator rng) { random = rng; } /** * {@inheritDoc} * * The default implementation uses the identity *

                              {@code P(x0 < X <= x1) = P(X <= x1) - P(X <= x0)}

                              */ public double cumulativeProbability(int x0, int x1) throws NumberIsTooLargeException { if (x1 < x0) { throw new NumberIsTooLargeException(LocalizedFormats.LOWER_ENDPOINT_ABOVE_UPPER_ENDPOINT, x0, x1, true); } return cumulativeProbability(x1) - cumulativeProbability(x0); } /** * {@inheritDoc} * * The default implementation returns *
                                *
                              • {@link #getSupportLowerBound()} for {@code p = 0},
                              • *
                              • {@link #getSupportUpperBound()} for {@code p = 1}, and
                              • *
                              • {@link #solveInverseCumulativeProbability(double, int, int)} for * {@code 0 < p < 1}.
                              • *
                              */ public int inverseCumulativeProbability(final double p) throws OutOfRangeException { if (p < 0.0 || p > 1.0) { throw new OutOfRangeException(p, 0, 1); } int lower = getSupportLowerBound(); if (p == 0.0) { return lower; } if (lower == Integer.MIN_VALUE) { if (checkedCumulativeProbability(lower) >= p) { return lower; } } else { lower -= 1; // this ensures cumulativeProbability(lower) < p, which // is important for the solving step } int upper = getSupportUpperBound(); if (p == 1.0) { return upper; } // use the one-sided Chebyshev inequality to narrow the bracket // cf. AbstractRealDistribution.inverseCumulativeProbability(double) final double mu = getNumericalMean(); final double sigma = FastMath.sqrt(getNumericalVariance()); final boolean chebyshevApplies = !(Double.isInfinite(mu) || Double.isNaN(mu) || Double.isInfinite(sigma) || Double.isNaN(sigma) || sigma == 0.0); if (chebyshevApplies) { double k = FastMath.sqrt((1.0 - p) / p); double tmp = mu - k * sigma; if (tmp > lower) { lower = ((int) Math.ceil(tmp)) - 1; } k = 1.0 / k; tmp = mu + k * sigma; if (tmp < upper) { upper = ((int) Math.ceil(tmp)) - 1; } } return solveInverseCumulativeProbability(p, lower, upper); } /** * This is a utility function used by {@link * #inverseCumulativeProbability(double)}. It assumes {@code 0 < p < 1} and * that the inverse cumulative probability lies in the bracket {@code * (lower, upper]}. The implementation does simple bisection to find the * smallest {@code p}-quantile inf{x in Z | P(X<=x) >= p}. * * @param p the cumulative probability * @param lower a value satisfying {@code cumulativeProbability(lower) < p} * @param upper a value satisfying {@code p <= cumulativeProbability(upper)} * @return the smallest {@code p}-quantile of this distribution */ protected int solveInverseCumulativeProbability(final double p, int lower, int upper) { while (lower + 1 < upper) { int xm = (lower + upper) / 2; if (xm < lower || xm > upper) { /* * Overflow. * There will never be an overflow in both calculation methods * for xm at the same time */ xm = lower + (upper - lower) / 2; } double pm = checkedCumulativeProbability(xm); if (pm >= p) { upper = xm; } else { lower = xm; } } return upper; } /** {@inheritDoc} */ public void reseedRandomGenerator(long seed) { random.setSeed(seed); randomData.reSeed(seed); } /** * {@inheritDoc} * * The default implementation uses the * * inversion method. */ public int sample() { return inverseCumulativeProbability(random.nextDouble()); } /** * {@inheritDoc} * * The default implementation generates the sample by calling * {@link #sample()} in a loop. */ public int[] sample(int sampleSize) { if (sampleSize <= 0) { throw new NotStrictlyPositiveException( LocalizedFormats.NUMBER_OF_SAMPLES, sampleSize); } int[] out = new int[sampleSize]; for (int i = 0; i < sampleSize; i++) { out[i] = sample(); } return out; } /** * Computes the cumulative probability function and checks for {@code NaN} * values returned. Throws {@code MathInternalError} if the value is * {@code NaN}. Rethrows any exception encountered evaluating the cumulative * probability function. Throws {@code MathInternalError} if the cumulative * probability function returns {@code NaN}. * * @param argument input value * @return the cumulative probability * @throws MathInternalError if the cumulative probability is {@code NaN} */ private double checkedCumulativeProbability(int argument) throws MathInternalError { double result = Double.NaN; result = cumulativeProbability(argument); if (Double.isNaN(result)) { throw new MathInternalError(LocalizedFormats .DISCRETE_CUMULATIVE_PROBABILITY_RETURNED_NAN, argument); } return result; } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/distribution/PascalDistribution.java100644 1750 1750 17142 12126627700 31731 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.distribution; import org.apache.commons.math3.exception.NotStrictlyPositiveException; import org.apache.commons.math3.exception.OutOfRangeException; import org.apache.commons.math3.exception.util.LocalizedFormats; import org.apache.commons.math3.special.Beta; import org.apache.commons.math3.util.ArithmeticUtils; import org.apache.commons.math3.util.FastMath; import org.apache.commons.math3.random.RandomGenerator; import org.apache.commons.math3.random.Well19937c; /** *

                              * Implementation of the Pascal distribution. The Pascal distribution is a * special case of the Negative Binomial distribution where the number of * successes parameter is an integer. *

                              *

                              * There are various ways to express the probability mass and distribution * functions for the Pascal distribution. The present implementation represents * the distribution of the number of failures before {@code r} successes occur. * This is the convention adopted in e.g. * MathWorld, * but not in * Wikipedia. *

                              *

                              * For a random variable {@code X} whose values are distributed according to this * distribution, the probability mass function is given by
                              * {@code P(X = k) = C(k + r - 1, r - 1) * p^r * (1 - p)^k,}
                              * where {@code r} is the number of successes, {@code p} is the probability of * success, and {@code X} is the total number of failures. {@code C(n, k)} is * the binomial coefficient ({@code n} choose {@code k}). The mean and variance * of {@code X} are
                              * {@code E(X) = (1 - p) * r / p, var(X) = (1 - p) * r / p^2.}
                              * Finally, the cumulative distribution function is given by
                              * {@code P(X <= k) = I(p, r, k + 1)}, * where I is the regularized incomplete Beta function. *

                              * * @see * Negative binomial distribution (Wikipedia) * @see * Negative binomial distribution (MathWorld) * @version $Id: PascalDistribution.java 1416643 2012-12-03 19:37:14Z tn $ * @since 1.2 (changed to concrete class in 3.0) */ public class PascalDistribution extends AbstractIntegerDistribution { /** Serializable version identifier. */ private static final long serialVersionUID = 6751309484392813623L; /** The number of successes. */ private final int numberOfSuccesses; /** The probability of success. */ private final double probabilityOfSuccess; /** * Create a Pascal distribution with the given number of successes and * probability of success. * * @param r Number of successes. * @param p Probability of success. * @throws NotStrictlyPositiveException if the number of successes is not positive * @throws OutOfRangeException if the probability of success is not in the * range {@code [0, 1]}. */ public PascalDistribution(int r, double p) throws NotStrictlyPositiveException, OutOfRangeException { this(new Well19937c(), r, p); } /** * Create a Pascal distribution with the given number of successes and * probability of success. * * @param rng Random number generator. * @param r Number of successes. * @param p Probability of success. * @throws NotStrictlyPositiveException if the number of successes is not positive * @throws OutOfRangeException if the probability of success is not in the * range {@code [0, 1]}. * @since 3.1 */ public PascalDistribution(RandomGenerator rng, int r, double p) throws NotStrictlyPositiveException, OutOfRangeException { super(rng); if (r <= 0) { throw new NotStrictlyPositiveException(LocalizedFormats.NUMBER_OF_SUCCESSES, r); } if (p < 0 || p > 1) { throw new OutOfRangeException(p, 0, 1); } numberOfSuccesses = r; probabilityOfSuccess = p; } /** * Access the number of successes for this distribution. * * @return the number of successes. */ public int getNumberOfSuccesses() { return numberOfSuccesses; } /** * Access the probability of success for this distribution. * * @return the probability of success. */ public double getProbabilityOfSuccess() { return probabilityOfSuccess; } /** {@inheritDoc} */ public double probability(int x) { double ret; if (x < 0) { ret = 0.0; } else { ret = ArithmeticUtils.binomialCoefficientDouble(x + numberOfSuccesses - 1, numberOfSuccesses - 1) * FastMath.pow(probabilityOfSuccess, numberOfSuccesses) * FastMath.pow(1.0 - probabilityOfSuccess, x); } return ret; } /** {@inheritDoc} */ public double cumulativeProbability(int x) { double ret; if (x < 0) { ret = 0.0; } else { ret = Beta.regularizedBeta(probabilityOfSuccess, numberOfSuccesses, x + 1.0); } return ret; } /** * {@inheritDoc} * * For number of successes {@code r} and probability of success {@code p}, * the mean is {@code r * (1 - p) / p}. */ public double getNumericalMean() { final double p = getProbabilityOfSuccess(); final double r = getNumberOfSuccesses(); return (r * (1 - p)) / p; } /** * {@inheritDoc} * * For number of successes {@code r} and probability of success {@code p}, * the variance is {@code r * (1 - p) / p^2}. */ public double getNumericalVariance() { final double p = getProbabilityOfSuccess(); final double r = getNumberOfSuccesses(); return r * (1 - p) / (p * p); } /** * {@inheritDoc} * * The lower bound of the support is always 0 no matter the parameters. * * @return lower bound of the support (always 0) */ public int getSupportLowerBound() { return 0; } /** * {@inheritDoc} * * The upper bound of the support is always positive infinity no matter the * parameters. Positive infinity is symbolized by {@code Integer.MAX_VALUE}. * * @return upper bound of the support (always {@code Integer.MAX_VALUE} * for positive infinity) */ public int getSupportUpperBound() { return Integer.MAX_VALUE; } /** * {@inheritDoc} * * The support of this distribution is connected. * * @return {@code true} */ public boolean isSupportConnected() { return true; } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/distribution/BetaDistribution.java100644 1750 1750 16417 12126627700 31405 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.distribution; import org.apache.commons.math3.exception.NumberIsTooSmallException; import org.apache.commons.math3.exception.util.LocalizedFormats; import org.apache.commons.math3.special.Gamma; import org.apache.commons.math3.special.Beta; import org.apache.commons.math3.util.FastMath; import org.apache.commons.math3.random.RandomGenerator; import org.apache.commons.math3.random.Well19937c; /** * Implements the Beta distribution. * * @see Beta distribution * @version $Id: BetaDistribution.java 1416643 2012-12-03 19:37:14Z tn $ * @since 2.0 (changed to concrete class in 3.0) */ public class BetaDistribution extends AbstractRealDistribution { /** * Default inverse cumulative probability accuracy. * @since 2.1 */ public static final double DEFAULT_INVERSE_ABSOLUTE_ACCURACY = 1e-9; /** Serializable version identifier. */ private static final long serialVersionUID = -1221965979403477668L; /** First shape parameter. */ private final double alpha; /** Second shape parameter. */ private final double beta; /** Normalizing factor used in density computations. * updated whenever alpha or beta are changed. */ private double z; /** Inverse cumulative probability accuracy. */ private final double solverAbsoluteAccuracy; /** * Build a new instance. * * @param alpha First shape parameter (must be positive). * @param beta Second shape parameter (must be positive). */ public BetaDistribution(double alpha, double beta) { this(alpha, beta, DEFAULT_INVERSE_ABSOLUTE_ACCURACY); } /** * Build a new instance. * * @param alpha First shape parameter (must be positive). * @param beta Second shape parameter (must be positive). * @param inverseCumAccuracy Maximum absolute error in inverse * cumulative probability estimates (defaults to * {@link #DEFAULT_INVERSE_ABSOLUTE_ACCURACY}). * @since 2.1 */ public BetaDistribution(double alpha, double beta, double inverseCumAccuracy) { this(new Well19937c(), alpha, beta, inverseCumAccuracy); } /** * Creates a β distribution. * * @param rng Random number generator. * @param alpha First shape parameter (must be positive). * @param beta Second shape parameter (must be positive). * @param inverseCumAccuracy Maximum absolute error in inverse * cumulative probability estimates (defaults to * {@link #DEFAULT_INVERSE_ABSOLUTE_ACCURACY}). * @since 3.1 */ public BetaDistribution(RandomGenerator rng, double alpha, double beta, double inverseCumAccuracy) { super(rng); this.alpha = alpha; this.beta = beta; z = Double.NaN; solverAbsoluteAccuracy = inverseCumAccuracy; } /** * Access the first shape parameter, {@code alpha}. * * @return the first shape parameter. */ public double getAlpha() { return alpha; } /** * Access the second shape parameter, {@code beta}. * * @return the second shape parameter. */ public double getBeta() { return beta; } /** Recompute the normalization factor. */ private void recomputeZ() { if (Double.isNaN(z)) { z = Gamma.logGamma(alpha) + Gamma.logGamma(beta) - Gamma.logGamma(alpha + beta); } } /** {@inheritDoc} */ public double density(double x) { recomputeZ(); if (x < 0 || x > 1) { return 0; } else if (x == 0) { if (alpha < 1) { throw new NumberIsTooSmallException(LocalizedFormats.CANNOT_COMPUTE_BETA_DENSITY_AT_0_FOR_SOME_ALPHA, alpha, 1, false); } return 0; } else if (x == 1) { if (beta < 1) { throw new NumberIsTooSmallException(LocalizedFormats.CANNOT_COMPUTE_BETA_DENSITY_AT_1_FOR_SOME_BETA, beta, 1, false); } return 0; } else { double logX = FastMath.log(x); double log1mX = FastMath.log1p(-x); return FastMath.exp((alpha - 1) * logX + (beta - 1) * log1mX - z); } } /** {@inheritDoc} */ public double cumulativeProbability(double x) { if (x <= 0) { return 0; } else if (x >= 1) { return 1; } else { return Beta.regularizedBeta(x, alpha, beta); } } /** * Return the absolute accuracy setting of the solver used to estimate * inverse cumulative probabilities. * * @return the solver absolute accuracy. * @since 2.1 */ @Override protected double getSolverAbsoluteAccuracy() { return solverAbsoluteAccuracy; } /** * {@inheritDoc} * * For first shape parameter {@code alpha} and second shape parameter * {@code beta}, the mean is {@code alpha / (alpha + beta)}. */ public double getNumericalMean() { final double a = getAlpha(); return a / (a + getBeta()); } /** * {@inheritDoc} * * For first shape parameter {@code alpha} and second shape parameter * {@code beta}, the variance is * {@code (alpha * beta) / [(alpha + beta)^2 * (alpha + beta + 1)]}. */ public double getNumericalVariance() { final double a = getAlpha(); final double b = getBeta(); final double alphabetasum = a + b; return (a * b) / ((alphabetasum * alphabetasum) * (alphabetasum + 1)); } /** * {@inheritDoc} * * The lower bound of the support is always 0 no matter the parameters. * * @return lower bound of the support (always 0) */ public double getSupportLowerBound() { return 0; } /** * {@inheritDoc} * * The upper bound of the support is always 1 no matter the parameters. * * @return upper bound of the support (always 1) */ public double getSupportUpperBound() { return 1; } /** {@inheritDoc} */ public boolean isSupportLowerBoundInclusive() { return false; } /** {@inheritDoc} */ public boolean isSupportUpperBoundInclusive() { return false; } /** * {@inheritDoc} * * The support of this distribution is connected. * * @return {@code true} */ public boolean isSupportConnected() { return true; } } ././@LongLink100644 0 0 164 12126630647 10263 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/distribution/AbstractMultivariateRealDistribution.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/distribution/AbstractMultivariateRealDi100644 1750 1750 4776 12126627700 32412 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.distribution; import org.apache.commons.math3.exception.NotStrictlyPositiveException; import org.apache.commons.math3.exception.util.LocalizedFormats; import org.apache.commons.math3.random.RandomGenerator; /** * Base class for multivariate probability distributions. * * @version $Id: AbstractMultivariateRealDistribution.java 1416643 2012-12-03 19:37:14Z tn $ * @since 3.1 */ public abstract class AbstractMultivariateRealDistribution implements MultivariateRealDistribution { /** RNG instance used to generate samples from the distribution. */ protected final RandomGenerator random; /** The number of dimensions or columns in the multivariate distribution. */ private final int dimension; /** * @param rng Random number generator. * @param n Number of dimensions. */ protected AbstractMultivariateRealDistribution(RandomGenerator rng, int n) { random = rng; dimension = n; } /** {@inheritDoc} */ public void reseedRandomGenerator(long seed) { random.setSeed(seed); } /** {@inheritDoc} */ public int getDimension() { return dimension; } /** {@inheritDoc} */ public abstract double[] sample(); /** {@inheritDoc} */ public double[][] sample(final int sampleSize) { if (sampleSize <= 0) { throw new NotStrictlyPositiveException(LocalizedFormats.NUMBER_OF_SAMPLES, sampleSize); } final double[][] out = new double[sampleSize][dimension]; for (int i = 0; i < sampleSize; i++) { out[i] = sample(); } return out; } } ././@LongLink100644 0 0 155 12126630647 10263 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/distribution/KolmogorovSmirnovDistribution.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/distribution/KolmogorovSmirnovDistribut100644 1750 1750 32053 12126627700 32612 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.distribution; import java.io.Serializable; import java.math.BigDecimal; import org.apache.commons.math3.exception.MathArithmeticException; import org.apache.commons.math3.exception.NotStrictlyPositiveException; import org.apache.commons.math3.exception.NumberIsTooLargeException; import org.apache.commons.math3.exception.util.LocalizedFormats; import org.apache.commons.math3.fraction.BigFraction; import org.apache.commons.math3.fraction.BigFractionField; import org.apache.commons.math3.fraction.FractionConversionException; import org.apache.commons.math3.linear.Array2DRowFieldMatrix; import org.apache.commons.math3.linear.Array2DRowRealMatrix; import org.apache.commons.math3.linear.FieldMatrix; import org.apache.commons.math3.linear.RealMatrix; /** * Implementation of the Kolmogorov-Smirnov distribution. * *

                              * Treats the distribution of the two-sided {@code P(D_n < d)} where * {@code D_n = sup_x |G(x) - G_n (x)|} for the theoretical cdf {@code G} and * the empirical cdf {@code G_n}. *

                              *

                              * This implementation is based on [1] with certain quick decisions for extreme * values given in [2]. *

                              *

                              * In short, when wanting to evaluate {@code P(D_n < d)}, the method in [1] is * to write {@code d = (k - h) / n} for positive integer {@code k} and * {@code 0 <= h < 1}. Then {@code P(D_n < d) = (n! / n^n) * t_kk}, where * {@code t_kk} is the {@code (k, k)}'th entry in the special matrix * {@code H^n}, i.e. {@code H} to the {@code n}'th power. *

                              *

                              * References: *

                              * Note that [1] contains an error in computing h, refer to * MATH-437 for details. *

                              * * @see * Kolmogorov-Smirnov test (Wikipedia) * @version $Id: KolmogorovSmirnovDistribution.java 1416643 2012-12-03 19:37:14Z tn $ */ public class KolmogorovSmirnovDistribution implements Serializable { /** Serializable version identifier. */ private static final long serialVersionUID = -4670676796862967187L; /** Number of observations. */ private int n; /** * @param n Number of observations * @throws NotStrictlyPositiveException if {@code n <= 0} */ public KolmogorovSmirnovDistribution(int n) throws NotStrictlyPositiveException { if (n <= 0) { throw new NotStrictlyPositiveException(LocalizedFormats.NOT_POSITIVE_NUMBER_OF_SAMPLES, n); } this.n = n; } /** * Calculates {@code P(D_n < d)} using method described in [1] with quick * decisions for extreme values given in [2] (see above). The result is not * exact as with * {@link KolmogorovSmirnovDistribution#cdfExact(double)} because * calculations are based on {@code double} rather than * {@link org.apache.commons.math3.fraction.BigFraction}. * * @param d statistic * @return the two-sided probability of {@code P(D_n < d)} * @throws MathArithmeticException if algorithm fails to convert {@code h} * to a {@link org.apache.commons.math3.fraction.BigFraction} in expressing * {@code d} as {@code (k - h) / m} for integer {@code k, m} and * {@code 0 <= h < 1}. */ public double cdf(double d) throws MathArithmeticException { return this.cdf(d, false); } /** * Calculates {@code P(D_n < d)} using method described in [1] with quick * decisions for extreme values given in [2] (see above). The result is * exact in the sense that BigFraction/BigReal is used everywhere at the * expense of very slow execution time. Almost never choose this in real * applications unless you are very sure; this is almost solely for * verification purposes. Normally, you would choose * {@link KolmogorovSmirnovDistribution#cdf(double)} * * @param d statistic * @return the two-sided probability of {@code P(D_n < d)} * @throws MathArithmeticException if algorithm fails to convert {@code h} * to a {@link org.apache.commons.math3.fraction.BigFraction} in expressing * {@code d} as {@code (k - h) / m} for integer {@code k, m} and * {@code 0 <= h < 1}. */ public double cdfExact(double d) throws MathArithmeticException { return this.cdf(d, true); } /** * Calculates {@code P(D_n < d)} using method described in [1] with quick * decisions for extreme values given in [2] (see above). * * @param d statistic * @param exact whether the probability should be calculated exact using * {@link org.apache.commons.math3.fraction.BigFraction} everywhere at the * expense of very slow execution time, or if {@code double} should be used * convenient places to gain speed. Almost never choose {@code true} in real * applications unless you are very sure; {@code true} is almost solely for * verification purposes. * @return the two-sided probability of {@code P(D_n < d)} * @throws MathArithmeticException if algorithm fails to convert {@code h} * to a {@link org.apache.commons.math3.fraction.BigFraction} in expressing * {@code d} as {@code (k - h) / m} for integer {@code k, m} and * {@code 0 <= h < 1}. */ public double cdf(double d, boolean exact) throws MathArithmeticException { final double ninv = 1 / ((double) n); final double ninvhalf = 0.5 * ninv; if (d <= ninvhalf) { return 0; } else if (ninvhalf < d && d <= ninv) { double res = 1; double f = 2 * d - ninv; // n! f^n = n*f * (n-1)*f * ... * 1*x for (int i = 1; i <= n; ++i) { res *= i * f; } return res; } else if (1 - ninv <= d && d < 1) { return 1 - 2 * Math.pow(1 - d, n); } else if (1 <= d) { return 1; } return exact ? exactK(d) : roundedK(d); } /** * Calculates the exact value of {@code P(D_n < d)} using method described * in [1] and {@link org.apache.commons.math3.fraction.BigFraction} (see * above). * * @param d statistic * @return the two-sided probability of {@code P(D_n < d)} * @throws MathArithmeticException if algorithm fails to convert {@code h} * to a {@link org.apache.commons.math3.fraction.BigFraction} in expressing * {@code d} as {@code (k - h) / m} for integer {@code k, m} and * {@code 0 <= h < 1}. */ private double exactK(double d) throws MathArithmeticException { final int k = (int) Math.ceil(n * d); final FieldMatrix H = this.createH(d); final FieldMatrix Hpower = H.power(n); BigFraction pFrac = Hpower.getEntry(k - 1, k - 1); for (int i = 1; i <= n; ++i) { pFrac = pFrac.multiply(i).divide(n); } /* * BigFraction.doubleValue converts numerator to double and the * denominator to double and divides afterwards. That gives NaN quite * easy. This does not (scale is the number of digits): */ return pFrac.bigDecimalValue(20, BigDecimal.ROUND_HALF_UP).doubleValue(); } /** * Calculates {@code P(D_n < d)} using method described in [1] and doubles * (see above). * * @param d statistic * @return the two-sided probability of {@code P(D_n < d)} * @throws MathArithmeticException if algorithm fails to convert {@code h} * to a {@link org.apache.commons.math3.fraction.BigFraction} in expressing * {@code d} as {@code (k - h) / m} for integer {@code k, m} and * {@code 0 <= h < 1}. */ private double roundedK(double d) throws MathArithmeticException { final int k = (int) Math.ceil(n * d); final FieldMatrix HBigFraction = this.createH(d); final int m = HBigFraction.getRowDimension(); /* * Here the rounding part comes into play: use * RealMatrix instead of FieldMatrix */ final RealMatrix H = new Array2DRowRealMatrix(m, m); for (int i = 0; i < m; ++i) { for (int j = 0; j < m; ++j) { H.setEntry(i, j, HBigFraction.getEntry(i, j).doubleValue()); } } final RealMatrix Hpower = H.power(n); double pFrac = Hpower.getEntry(k - 1, k - 1); for (int i = 1; i <= n; ++i) { pFrac *= (double) i / (double) n; } return pFrac; } /*** * Creates {@code H} of size {@code m x m} as described in [1] (see above). * * @param d statistic * @return H matrix * @throws NumberIsTooLargeException if fractional part is greater than 1 * @throws FractionConversionException if algorithm fails to convert * {@code h} to a {@link org.apache.commons.math3.fraction.BigFraction} in * expressing {@code d} as {@code (k - h) / m} for integer {@code k, m} and * {@code 0 <= h < 1}. */ private FieldMatrix createH(double d) throws NumberIsTooLargeException, FractionConversionException { int k = (int) Math.ceil(n * d); int m = 2 * k - 1; double hDouble = k - n * d; if (hDouble >= 1) { throw new NumberIsTooLargeException(hDouble, 1.0, false); } BigFraction h = null; try { h = new BigFraction(hDouble, 1.0e-20, 10000); } catch (FractionConversionException e1) { try { h = new BigFraction(hDouble, 1.0e-10, 10000); } catch (FractionConversionException e2) { h = new BigFraction(hDouble, 1.0e-5, 10000); } } final BigFraction[][] Hdata = new BigFraction[m][m]; /* * Start by filling everything with either 0 or 1. */ for (int i = 0; i < m; ++i) { for (int j = 0; j < m; ++j) { if (i - j + 1 < 0) { Hdata[i][j] = BigFraction.ZERO; } else { Hdata[i][j] = BigFraction.ONE; } } } /* * Setting up power-array to avoid calculating the same value twice: * hPowers[0] = h^1 ... hPowers[m-1] = h^m */ final BigFraction[] hPowers = new BigFraction[m]; hPowers[0] = h; for (int i = 1; i < m; ++i) { hPowers[i] = h.multiply(hPowers[i - 1]); } /* * First column and last row has special values (each other reversed). */ for (int i = 0; i < m; ++i) { Hdata[i][0] = Hdata[i][0].subtract(hPowers[i]); Hdata[m - 1][i] = Hdata[m - 1][i].subtract(hPowers[m - i - 1]); } /* * [1] states: "For 1/2 < h < 1 the bottom left element of the matrix * should be (1 - 2*h^m + (2h - 1)^m )/m!" Since 0 <= h < 1, then if h > * 1/2 is sufficient to check: */ if (h.compareTo(BigFraction.ONE_HALF) == 1) { Hdata[m - 1][0] = Hdata[m - 1][0].add(h.multiply(2).subtract(1).pow(m)); } /* * Aside from the first column and last row, the (i, j)-th element is * 1/(i - j + 1)! if i - j + 1 >= 0, else 0. 1's and 0's are already * put, so only division with (i - j + 1)! is needed in the elements * that have 1's. There is no need to calculate (i - j + 1)! and then * divide - small steps avoid overflows. * * Note that i - j + 1 > 0 <=> i + 1 > j instead of j'ing all the way to * m. Also note that it is started at g = 2 because dividing by 1 isn't * really necessary. */ for (int i = 0; i < m; ++i) { for (int j = 0; j < i + 1; ++j) { if (i - j + 1 > 0) { for (int g = 2; g <= i - j + 1; ++g) { Hdata[i][j] = Hdata[i][j].divide(g); } } } } return new Array2DRowFieldMatrix(BigFractionField.getInstance(), Hdata); } } ././@LongLink100644 0 0 152 12126630647 10260 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/distribution/UniformIntegerDistribution.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/distribution/UniformIntegerDistribution100644 1750 1750 12036 12126627700 32540 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.distribution; import org.apache.commons.math3.exception.NumberIsTooLargeException; import org.apache.commons.math3.exception.util.LocalizedFormats; import org.apache.commons.math3.random.RandomGenerator; import org.apache.commons.math3.random.Well19937c; import org.apache.commons.math3.util.FastMath; /** * Implementation of the uniform integer distribution. * * @see Uniform distribution (discrete), at Wikipedia * * @version $Id: UniformIntegerDistribution.java 1416643 2012-12-03 19:37:14Z tn $ * @since 3.0 */ public class UniformIntegerDistribution extends AbstractIntegerDistribution { /** Serializable version identifier. */ private static final long serialVersionUID = 20120109L; /** Lower bound (inclusive) of this distribution. */ private final int lower; /** Upper bound (inclusive) of this distribution. */ private final int upper; /** * Creates a new uniform integer distribution using the given lower and * upper bounds (both inclusive). * * @param lower Lower bound (inclusive) of this distribution. * @param upper Upper bound (inclusive) of this distribution. * @throws NumberIsTooLargeException if {@code lower >= upper}. */ public UniformIntegerDistribution(int lower, int upper) throws NumberIsTooLargeException { this(new Well19937c(), lower, upper); } /** * Creates a new uniform integer distribution using the given lower and * upper bounds (both inclusive). * * @param rng Random number generator. * @param lower Lower bound (inclusive) of this distribution. * @param upper Upper bound (inclusive) of this distribution. * @throws NumberIsTooLargeException if {@code lower >= upper}. * @since 3.1 */ public UniformIntegerDistribution(RandomGenerator rng, int lower, int upper) throws NumberIsTooLargeException { super(rng); if (lower >= upper) { throw new NumberIsTooLargeException( LocalizedFormats.LOWER_BOUND_NOT_BELOW_UPPER_BOUND, lower, upper, false); } this.lower = lower; this.upper = upper; } /** {@inheritDoc} */ public double probability(int x) { if (x < lower || x > upper) { return 0; } return 1.0 / (upper - lower + 1); } /** {@inheritDoc} */ public double cumulativeProbability(int x) { if (x < lower) { return 0; } if (x > upper) { return 1; } return (x - lower + 1.0) / (upper - lower + 1.0); } /** * {@inheritDoc} * * For lower bound {@code lower} and upper bound {@code upper}, the mean is * {@code 0.5 * (lower + upper)}. */ public double getNumericalMean() { return 0.5 * (lower + upper); } /** * {@inheritDoc} * * For lower bound {@code lower} and upper bound {@code upper}, and * {@code n = upper - lower + 1}, the variance is {@code (n^2 - 1) / 12}. */ public double getNumericalVariance() { double n = upper - lower + 1; return (n * n - 1) / 12.0; } /** * {@inheritDoc} * * The lower bound of the support is equal to the lower bound parameter * of the distribution. * * @return lower bound of the support */ public int getSupportLowerBound() { return lower; } /** * {@inheritDoc} * * The upper bound of the support is equal to the upper bound parameter * of the distribution. * * @return upper bound of the support */ public int getSupportUpperBound() { return upper; } /** * {@inheritDoc} * * The support of this distribution is connected. * * @return {@code true} */ public boolean isSupportConnected() { return true; } /** {@inheritDoc} */ @Override public int sample() { final double r = random.nextDouble(); final double scaled = r * upper + (1 - r) * lower + r; return (int) FastMath.floor(scaled); } } ././@LongLink100644 0 0 146 12126630647 10263 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/distribution/EnumeratedDistribution.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/distribution/EnumeratedDistribution.jav100644 1750 1750 24414 12126627700 32456 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.distribution; import java.io.Serializable; import java.lang.reflect.Array; import java.util.ArrayList; import java.util.List; import org.apache.commons.math3.exception.MathArithmeticException; import org.apache.commons.math3.exception.NotANumberException; import org.apache.commons.math3.exception.NotFiniteNumberException; import org.apache.commons.math3.exception.NotPositiveException; import org.apache.commons.math3.exception.NotStrictlyPositiveException; import org.apache.commons.math3.exception.NullArgumentException; import org.apache.commons.math3.exception.util.LocalizedFormats; import org.apache.commons.math3.random.RandomGenerator; import org.apache.commons.math3.random.Well19937c; import org.apache.commons.math3.util.MathArrays; import org.apache.commons.math3.util.Pair; /** *

                              A generic implementation of a * * discrete probability distribution (Wikipedia) over a finite sample space, * based on an enumerated list of <value, probability> pairs. Input probabilities must all be non-negative, * but zero values are allowed and their sum does not have to equal one. Constructors will normalize input * probabilities to make them sum to one.

                              * *

                              The list of pairs does not, strictly speaking, have to be a function and it can * contain null values. The pmf created by the constructor will combine probabilities of equal values and * will treat null values as equal. For example, if the list of pairs <"dog", 0.2>, <null, 0.1>, * <"pig", 0.2>, <"dog", 0.1>, <null, 0.4> is provided to the constructor, the resulting * pmf will assign mass of 0.5 to null, 0.3 to "dog" and 0.2 to null.

                              * * @param type of the elements in the sample space. * @version $Id: EnumeratedDistribution.java 1456769 2013-03-15 04:51:34Z psteitz $ * @since 3.2 */ public class EnumeratedDistribution implements Serializable { /** Serializable UID. */ private static final long serialVersionUID = 20123308L; /** * RNG instance used to generate samples from the distribution. */ protected final RandomGenerator random; /** * List of random variable values. */ private final List singletons; /** * Probabilities of respective random variable values. For i = 0, ..., singletons.size() - 1, * probability[i] is the probability that a random variable following this distribution takes * the value singletons[i]. */ private final double[] probabilities; /** * Create an enumerated distribution using the given probability mass function * enumeration. * * @param pmf probability mass function enumerated as a list of * pairs. * @throws NotPositiveException if any of the probabilities are negative. * @throws NotFiniteNumberException if any of the probabilities are infinite. * @throws NotANumberException if any of the probabilities are NaN. * @throws MathArithmeticException all of the probabilities are 0. */ public EnumeratedDistribution(final List> pmf) throws NotPositiveException, MathArithmeticException, NotFiniteNumberException, NotANumberException { this(new Well19937c(), pmf); } /** * Create an enumerated distribution using the given random number generator * and probability mass function enumeration. * * @param rng random number generator. * @param pmf probability mass function enumerated as a list of * pairs. * @throws NotPositiveException if any of the probabilities are negative. * @throws NotFiniteNumberException if any of the probabilities are infinite. * @throws NotANumberException if any of the probabilities are NaN. * @throws MathArithmeticException all of the probabilities are 0. */ public EnumeratedDistribution(final RandomGenerator rng, final List> pmf) throws NotPositiveException, MathArithmeticException, NotFiniteNumberException, NotANumberException { random = rng; singletons = new ArrayList(pmf.size()); final double[] probs = new double[pmf.size()]; for (int i = 0; i < pmf.size(); i++) { final Pair sample = pmf.get(i); singletons.add(sample.getKey()); final double p = sample.getValue(); if (p < 0) { throw new NotPositiveException(sample.getValue()); } if (Double.isInfinite(p)) { throw new NotFiniteNumberException(p); } if (Double.isNaN(p)) { throw new NotANumberException(); } probs[i] = p; } probabilities = MathArrays.normalizeArray(probs, 1.0); } /** * Reseed the random generator used to generate samples. * * @param seed the new seed */ public void reseedRandomGenerator(long seed) { random.setSeed(seed); } /** *

                              For a random variable {@code X} whose values are distributed according to * this distribution, this method returns {@code P(X = x)}. In other words, * this method represents the probability mass function (PMF) for the * distribution.

                              * *

                              Note that if {@code x1} and {@code x2} satisfy {@code x1.equals(x2)}, * or both are null, then {@code probability(x1) = probability(x2)}.

                              * * @param x the point at which the PMF is evaluated * @return the value of the probability mass function at {@code x} */ double probability(final T x) { double probability = 0; for (int i = 0; i < probabilities.length; i++) { if ((x == null && singletons.get(i) == null) || (x != null && x.equals(singletons.get(i)))) { probability += probabilities[i]; } } return probability; } /** *

                              Return the probability mass function as a list of pairs.

                              * *

                              Note that if duplicate and / or null values were provided to the constructor * when creating this EnumeratedDistribution, the returned list will contain these * values. If duplicates values exist, what is returned will not represent * a pmf (i.e., it is up to the caller to consolidate duplicate mass points).

                              * * @return the probability mass function. */ public List> getPmf() { final List> samples = new ArrayList>(probabilities.length); for (int i = 0; i < probabilities.length; i++) { samples.add(new Pair(singletons.get(i), probabilities[i])); } return samples; } /** * Generate a random value sampled from this distribution. * * @return a random value. */ public T sample() { final double randomValue = random.nextDouble(); double sum = 0; for (int i = 0; i < probabilities.length; i++) { sum += probabilities[i]; if (randomValue < sum) { return singletons.get(i); } } /* This should never happen, but it ensures we will return a correct * object in case the loop above has some floating point inequality * problem on the final iteration. */ return singletons.get(singletons.size() - 1); } /** * Generate a random sample from the distribution. * * @param sampleSize the number of random values to generate. * @return an array representing the random sample. * @throws NotStrictlyPositiveException if {@code sampleSize} is not * positive. */ public Object[] sample(int sampleSize) throws NotStrictlyPositiveException { if (sampleSize <= 0) { throw new NotStrictlyPositiveException(LocalizedFormats.NUMBER_OF_SAMPLES, sampleSize); } final Object[] out = new Object[sampleSize]; for (int i = 0; i < sampleSize; i++) { out[i] = sample(); } return out; } /** * Generate a random sample from the distribution. *

                              * If the requested samples fit in the specified array, it is returned * therein. Otherwise, a new array is allocated with the runtime type of * the specified array and the size of this collection. * * @param sampleSize the number of random values to generate. * @param array the array to populate. * @return an array representing the random sample. * @throws NotStrictlyPositiveException if {@code sampleSize} is not positive. * @throws NullArgumentException if {@code array} is null */ public T[] sample(int sampleSize, final T[] array) throws NotStrictlyPositiveException { if (sampleSize <= 0) { throw new NotStrictlyPositiveException(LocalizedFormats.NUMBER_OF_SAMPLES, sampleSize); } if (array == null) { throw new NullArgumentException(LocalizedFormats.INPUT_ARRAY); } T[] out; if (array.length < sampleSize) { @SuppressWarnings("unchecked") // safe as both are of type T final T[] unchecked = (T[]) Array.newInstance(array.getClass().getComponentType(), sampleSize); out = unchecked; } else { out = array; } for (int i = 0; i < sampleSize; i++) { out[i] = sample(); } return out; } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/distribution/CauchyDistribution.java100644 1750 1750 16141 12126627700 31740 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.distribution; import org.apache.commons.math3.exception.NotStrictlyPositiveException; import org.apache.commons.math3.exception.OutOfRangeException; import org.apache.commons.math3.exception.util.LocalizedFormats; import org.apache.commons.math3.util.FastMath; import org.apache.commons.math3.random.RandomGenerator; import org.apache.commons.math3.random.Well19937c; /** * Implementation of the Cauchy distribution. * * @see Cauchy distribution (Wikipedia) * @see Cauchy Distribution (MathWorld) * @since 1.1 (changed to concrete class in 3.0) * @version $Id: CauchyDistribution.java 1416643 2012-12-03 19:37:14Z tn $ */ public class CauchyDistribution extends AbstractRealDistribution { /** * Default inverse cumulative probability accuracy. * @since 2.1 */ public static final double DEFAULT_INVERSE_ABSOLUTE_ACCURACY = 1e-9; /** Serializable version identifier */ private static final long serialVersionUID = 8589540077390120676L; /** The median of this distribution. */ private final double median; /** The scale of this distribution. */ private final double scale; /** Inverse cumulative probability accuracy */ private final double solverAbsoluteAccuracy; /** * Creates a Cauchy distribution with the median equal to zero and scale * equal to one. */ public CauchyDistribution() { this(0, 1); } /** * Creates a Cauchy distribution using the given median and scale. * * @param median Median for this distribution. * @param scale Scale parameter for this distribution. */ public CauchyDistribution(double median, double scale) { this(median, scale, DEFAULT_INVERSE_ABSOLUTE_ACCURACY); } /** * Creates a Cauchy distribution using the given median and scale. * * @param median Median for this distribution. * @param scale Scale parameter for this distribution. * @param inverseCumAccuracy Maximum absolute error in inverse * cumulative probability estimates * (defaults to {@link #DEFAULT_INVERSE_ABSOLUTE_ACCURACY}). * @throws NotStrictlyPositiveException if {@code scale <= 0}. * @since 2.1 */ public CauchyDistribution(double median, double scale, double inverseCumAccuracy) { this(new Well19937c(), median, scale, inverseCumAccuracy); } /** * Creates a Cauchy distribution. * * @param rng Random number generator. * @param median Median for this distribution. * @param scale Scale parameter for this distribution. * @param inverseCumAccuracy Maximum absolute error in inverse * cumulative probability estimates * (defaults to {@link #DEFAULT_INVERSE_ABSOLUTE_ACCURACY}). * @throws NotStrictlyPositiveException if {@code scale <= 0}. * @since 3.1 */ public CauchyDistribution(RandomGenerator rng, double median, double scale, double inverseCumAccuracy) { super(rng); if (scale <= 0) { throw new NotStrictlyPositiveException(LocalizedFormats.SCALE, scale); } this.scale = scale; this.median = median; solverAbsoluteAccuracy = inverseCumAccuracy; } /** {@inheritDoc} */ public double cumulativeProbability(double x) { return 0.5 + (FastMath.atan((x - median) / scale) / FastMath.PI); } /** * Access the median. * * @return the median for this distribution. */ public double getMedian() { return median; } /** * Access the scale parameter. * * @return the scale parameter for this distribution. */ public double getScale() { return scale; } /** {@inheritDoc} */ public double density(double x) { final double dev = x - median; return (1 / FastMath.PI) * (scale / (dev * dev + scale * scale)); } /** * {@inheritDoc} * * Returns {@code Double.NEGATIVE_INFINITY} when {@code p == 0} * and {@code Double.POSITIVE_INFINITY} when {@code p == 1}. */ @Override public double inverseCumulativeProbability(double p) throws OutOfRangeException { double ret; if (p < 0 || p > 1) { throw new OutOfRangeException(p, 0, 1); } else if (p == 0) { ret = Double.NEGATIVE_INFINITY; } else if (p == 1) { ret = Double.POSITIVE_INFINITY; } else { ret = median + scale * FastMath.tan(FastMath.PI * (p - .5)); } return ret; } /** {@inheritDoc} */ @Override protected double getSolverAbsoluteAccuracy() { return solverAbsoluteAccuracy; } /** * {@inheritDoc} * * The mean is always undefined no matter the parameters. * * @return mean (always Double.NaN) */ public double getNumericalMean() { return Double.NaN; } /** * {@inheritDoc} * * The variance is always undefined no matter the parameters. * * @return variance (always Double.NaN) */ public double getNumericalVariance() { return Double.NaN; } /** * {@inheritDoc} * * The lower bound of the support is always negative infinity no matter * the parameters. * * @return lower bound of the support (always Double.NEGATIVE_INFINITY) */ public double getSupportLowerBound() { return Double.NEGATIVE_INFINITY; } /** * {@inheritDoc} * * The upper bound of the support is always positive infinity no matter * the parameters. * * @return upper bound of the support (always Double.POSITIVE_INFINITY) */ public double getSupportUpperBound() { return Double.POSITIVE_INFINITY; } /** {@inheritDoc} */ public boolean isSupportLowerBoundInclusive() { return false; } /** {@inheritDoc} */ public boolean isSupportUpperBoundInclusive() { return false; } /** * {@inheritDoc} * * The support of this distribution is connected. * * @return {@code true} */ public boolean isSupportConnected() { return true; } } ././@LongLink100644 0 0 156 12126630647 10264 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/distribution/MultivariateNormalDistribution.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/distribution/MultivariateNormalDistribu100644 1750 1750 21573 12126627700 32536 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.distribution; import org.apache.commons.math3.exception.DimensionMismatchException; import org.apache.commons.math3.linear.Array2DRowRealMatrix; import org.apache.commons.math3.linear.EigenDecomposition; import org.apache.commons.math3.linear.NonPositiveDefiniteMatrixException; import org.apache.commons.math3.linear.RealMatrix; import org.apache.commons.math3.linear.SingularMatrixException; import org.apache.commons.math3.random.RandomGenerator; import org.apache.commons.math3.random.Well19937c; import org.apache.commons.math3.util.FastMath; import org.apache.commons.math3.util.MathArrays; /** * Implementation of the multivariate normal (Gaussian) distribution. * * @see * Multivariate normal distribution (Wikipedia) * @see * Multivariate normal distribution (MathWorld) * * @version $Id: MultivariateNormalDistribution.java 1433367 2013-01-15 12:15:50Z erans $ * @since 3.1 */ public class MultivariateNormalDistribution extends AbstractMultivariateRealDistribution { /** Vector of means. */ private final double[] means; /** Covariance matrix. */ private final RealMatrix covarianceMatrix; /** The matrix inverse of the covariance matrix. */ private final RealMatrix covarianceMatrixInverse; /** The determinant of the covariance matrix. */ private final double covarianceMatrixDeterminant; /** Matrix used in computation of samples. */ private final RealMatrix samplingMatrix; /** * Creates a multivariate normal distribution with the given mean vector and * covariance matrix. *
                              * The number of dimensions is equal to the length of the mean vector * and to the number of rows and columns of the covariance matrix. * It is frequently written as "p" in formulae. * * @param means Vector of means. * @param covariances Covariance matrix. * @throws DimensionMismatchException if the arrays length are * inconsistent. * @throws SingularMatrixException if the eigenvalue decomposition cannot * be performed on the provided covariance matrix. * @throws NonPositiveDefiniteMatrixException if any of the eigenvalues is * negative. */ public MultivariateNormalDistribution(final double[] means, final double[][] covariances) throws SingularMatrixException, DimensionMismatchException, NonPositiveDefiniteMatrixException { this(new Well19937c(), means, covariances); } /** * Creates a multivariate normal distribution with the given mean vector and * covariance matrix. *
                              * The number of dimensions is equal to the length of the mean vector * and to the number of rows and columns of the covariance matrix. * It is frequently written as "p" in formulae. * * @param rng Random Number Generator. * @param means Vector of means. * @param covariances Covariance matrix. * @throws DimensionMismatchException if the arrays length are * inconsistent. * @throws SingularMatrixException if the eigenvalue decomposition cannot * be performed on the provided covariance matrix. * @throws NonPositiveDefiniteMatrixException if any of the eigenvalues is * negative. */ public MultivariateNormalDistribution(RandomGenerator rng, final double[] means, final double[][] covariances) throws SingularMatrixException, DimensionMismatchException, NonPositiveDefiniteMatrixException { super(rng, means.length); final int dim = means.length; if (covariances.length != dim) { throw new DimensionMismatchException(covariances.length, dim); } for (int i = 0; i < dim; i++) { if (dim != covariances[i].length) { throw new DimensionMismatchException(covariances[i].length, dim); } } this.means = MathArrays.copyOf(means); covarianceMatrix = new Array2DRowRealMatrix(covariances); // Covariance matrix eigen decomposition. final EigenDecomposition covMatDec = new EigenDecomposition(covarianceMatrix); // Compute and store the inverse. covarianceMatrixInverse = covMatDec.getSolver().getInverse(); // Compute and store the determinant. covarianceMatrixDeterminant = covMatDec.getDeterminant(); // Eigenvalues of the covariance matrix. final double[] covMatEigenvalues = covMatDec.getRealEigenvalues(); for (int i = 0; i < covMatEigenvalues.length; i++) { if (covMatEigenvalues[i] < 0) { throw new NonPositiveDefiniteMatrixException(covMatEigenvalues[i], i, 0); } } // Matrix where each column is an eigenvector of the covariance matrix. final Array2DRowRealMatrix covMatEigenvectors = new Array2DRowRealMatrix(dim, dim); for (int v = 0; v < dim; v++) { final double[] evec = covMatDec.getEigenvector(v).toArray(); covMatEigenvectors.setColumn(v, evec); } final RealMatrix tmpMatrix = covMatEigenvectors.transpose(); // Scale each eigenvector by the square root of its eigenvalue. for (int row = 0; row < dim; row++) { final double factor = FastMath.sqrt(covMatEigenvalues[row]); for (int col = 0; col < dim; col++) { tmpMatrix.multiplyEntry(row, col, factor); } } samplingMatrix = covMatEigenvectors.multiply(tmpMatrix); } /** * Gets the mean vector. * * @return the mean vector. */ public double[] getMeans() { return MathArrays.copyOf(means); } /** * Gets the covariance matrix. * * @return the covariance matrix. */ public RealMatrix getCovariances() { return covarianceMatrix.copy(); } /** {@inheritDoc} */ public double density(final double[] vals) throws DimensionMismatchException { final int dim = getDimension(); if (vals.length != dim) { throw new DimensionMismatchException(vals.length, dim); } return FastMath.pow(2 * FastMath.PI, -0.5 * dim) * FastMath.pow(covarianceMatrixDeterminant, -0.5) * getExponentTerm(vals); } /** * Gets the square root of each element on the diagonal of the covariance * matrix. * * @return the standard deviations. */ public double[] getStandardDeviations() { final int dim = getDimension(); final double[] std = new double[dim]; final double[][] s = covarianceMatrix.getData(); for (int i = 0; i < dim; i++) { std[i] = FastMath.sqrt(s[i][i]); } return std; } /** {@inheritDoc} */ public double[] sample() { final int dim = getDimension(); final double[] normalVals = new double[dim]; for (int i = 0; i < dim; i++) { normalVals[i] = random.nextGaussian(); } final double[] vals = samplingMatrix.operate(normalVals); for (int i = 0; i < dim; i++) { vals[i] += means[i]; } return vals; } /** * Computes the term used in the exponent (see definition of the distribution). * * @param values Values at which to compute density. * @return the multiplication factor of density calculations. */ private double getExponentTerm(final double[] values) { final double[] centered = new double[values.length]; for (int i = 0; i < centered.length; i++) { centered[i] = values[i] - getMeans()[i]; } final double[] preMultiplied = covarianceMatrixInverse.preMultiply(centered); double sum = 0; for (int i = 0; i < preMultiplied.length; i++) { sum += preMultiplied[i] * centered[i]; } return FastMath.exp(-0.5 * sum); } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/distribution/IntegerDistribution.java100644 1750 1750 13321 12126627700 32116 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.distribution; import org.apache.commons.math3.exception.NumberIsTooLargeException; import org.apache.commons.math3.exception.OutOfRangeException; /** * Interface for distributions on the integers. * * @version $Id: IntegerDistribution.java 1416643 2012-12-03 19:37:14Z tn $ */ public interface IntegerDistribution { /** * For a random variable {@code X} whose values are distributed according * to this distribution, this method returns {@code P(X = x)}. In other * words, this method represents the probability mass function (PMF) * for the distribution. * * @param x the point at which the PMF is evaluated * @return the value of the probability mass function at {@code x} */ double probability(int x); /** * For a random variable {@code X} whose values are distributed according * to this distribution, this method returns {@code P(X <= x)}. In other * words, this method represents the (cumulative) distribution function * (CDF) for this distribution. * * @param x the point at which the CDF is evaluated * @return the probability that a random variable with this * distribution takes a value less than or equal to {@code x} */ double cumulativeProbability(int x); /** * For a random variable {@code X} whose values are distributed according * to this distribution, this method returns {@code P(x0 < X <= x1)}. * * @param x0 the exclusive lower bound * @param x1 the inclusive upper bound * @return the probability that a random variable with this distribution * will take a value between {@code x0} and {@code x1}, * excluding the lower and including the upper endpoint * @throws NumberIsTooLargeException if {@code x0 > x1} */ double cumulativeProbability(int x0, int x1) throws NumberIsTooLargeException; /** * Computes the quantile function of this distribution. * For a random variable {@code X} distributed according to this distribution, * the returned value is *

                                *
                              • inf{x in Z | P(X<=x) >= p} for {@code 0 < p <= 1},
                              • *
                              • inf{x in Z | P(X<=x) > 0} for {@code p = 0}.
                              • *
                              * If the result exceeds the range of the data type {@code int}, * then {@code Integer.MIN_VALUE} or {@code Integer.MAX_VALUE} is returned. * * @param p the cumulative probability * @return the smallest {@code p}-quantile of this distribution * (largest 0-quantile for {@code p = 0}) * @throws OutOfRangeException if {@code p < 0} or {@code p > 1} */ int inverseCumulativeProbability(double p) throws OutOfRangeException; /** * Use this method to get the numerical value of the mean of this * distribution. * * @return the mean or {@code Double.NaN} if it is not defined */ double getNumericalMean(); /** * Use this method to get the numerical value of the variance of this * distribution. * * @return the variance (possibly {@code Double.POSITIVE_INFINITY} or * {@code Double.NaN} if it is not defined) */ double getNumericalVariance(); /** * Access the lower bound of the support. This method must return the same * value as {@code inverseCumulativeProbability(0)}. In other words, this * method must return *

                              inf {x in Z | P(X <= x) > 0}.

                              * * @return lower bound of the support ({@code Integer.MIN_VALUE} * for negative infinity) */ int getSupportLowerBound(); /** * Access the upper bound of the support. This method must return the same * value as {@code inverseCumulativeProbability(1)}. In other words, this * method must return *

                              inf {x in R | P(X <= x) = 1}.

                              * * @return upper bound of the support ({@code Integer.MAX_VALUE} * for positive infinity) */ int getSupportUpperBound(); /** * Use this method to get information about whether the support is * connected, i.e. whether all integers between the lower and upper bound of * the support are included in the support. * * @return whether the support is connected or not */ boolean isSupportConnected(); /** * Reseed the random generator used to generate samples. * * @param seed the new seed * @since 3.0 */ void reseedRandomGenerator(long seed); /** * Generate a random value sampled from this distribution. * * @return a random value * @since 3.0 */ int sample(); /** * Generate a random sample from the distribution. * * @param sampleSize the number of random values to generate * @return an array representing the random sample * @throws org.apache.commons.math3.exception.NotStrictlyPositiveException * if {@code sampleSize} is not positive * @since 3.0 */ int[] sample(int sampleSize); } ././@LongLink100644 0 0 147 12126630647 10264 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/distribution/UniformRealDistribution.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/distribution/UniformRealDistribution.ja100644 1750 1750 16421 12126627700 32421 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.distribution; import org.apache.commons.math3.exception.NumberIsTooLargeException; import org.apache.commons.math3.exception.OutOfRangeException; import org.apache.commons.math3.exception.util.LocalizedFormats; import org.apache.commons.math3.random.RandomGenerator; import org.apache.commons.math3.random.Well19937c; /** * Implementation of the uniform real distribution. * * @see Uniform distribution (continuous), at Wikipedia * * @version $Id: UniformRealDistribution.java 1462020 2013-03-28 10:24:45Z luc $ * @since 3.0 */ public class UniformRealDistribution extends AbstractRealDistribution { /** Default inverse cumulative probability accuracy. * @deprecated as of 3.2 not used anymore, will be removed in 4.0 */ @Deprecated public static final double DEFAULT_INVERSE_ABSOLUTE_ACCURACY = 1e-9; /** Serializable version identifier. */ private static final long serialVersionUID = 20120109L; /** Lower bound of this distribution (inclusive). */ private final double lower; /** Upper bound of this distribution (exclusive). */ private final double upper; /** * Create a standard uniform real distribution with lower bound (inclusive) * equal to zero and upper bound (exclusive) equal to one. */ public UniformRealDistribution() { this(0, 1); } /** * Create a uniform real distribution using the given lower and upper * bounds. * * @param lower Lower bound of this distribution (inclusive). * @param upper Upper bound of this distribution (exclusive). * @throws NumberIsTooLargeException if {@code lower >= upper}. */ public UniformRealDistribution(double lower, double upper) throws NumberIsTooLargeException { this(new Well19937c(), lower, upper); } /** * Create a uniform distribution. * * @param lower Lower bound of this distribution (inclusive). * @param upper Upper bound of this distribution (exclusive). * @param inverseCumAccuracy Inverse cumulative probability accuracy. * @throws NumberIsTooLargeException if {@code lower >= upper}. * @deprecated as of 3.2, inverse CDF is now calculated analytically, use * {@link #UniformRealDistribution(double, double)} instead. */ @Deprecated public UniformRealDistribution(double lower, double upper, double inverseCumAccuracy) throws NumberIsTooLargeException { this(new Well19937c(), lower, upper); } /** * Creates a uniform distribution. * * @param rng Random number generator. * @param lower Lower bound of this distribution (inclusive). * @param upper Upper bound of this distribution (exclusive). * @param inverseCumAccuracy Inverse cumulative probability accuracy. * @throws NumberIsTooLargeException if {@code lower >= upper}. * @since 3.1 * @deprecated as of 3.2, inverse CDF is now calculated analytically, use * {@link #UniformRealDistribution(RandomGenerator, double, double)} * instead. */ @Deprecated public UniformRealDistribution(RandomGenerator rng, double lower, double upper, double inverseCumAccuracy){ this(rng, lower, upper); } /** * Creates a uniform distribution. * * @param rng Random number generator. * @param lower Lower bound of this distribution (inclusive). * @param upper Upper bound of this distribution (exclusive). * @throws NumberIsTooLargeException if {@code lower >= upper}. * @since 3.1 */ public UniformRealDistribution(RandomGenerator rng, double lower, double upper) throws NumberIsTooLargeException { super(rng); if (lower >= upper) { throw new NumberIsTooLargeException( LocalizedFormats.LOWER_BOUND_NOT_BELOW_UPPER_BOUND, lower, upper, false); } this.lower = lower; this.upper = upper; } /** {@inheritDoc} */ public double density(double x) { if (x < lower || x > upper) { return 0.0; } return 1 / (upper - lower); } /** {@inheritDoc} */ public double cumulativeProbability(double x) { if (x <= lower) { return 0; } if (x >= upper) { return 1; } return (x - lower) / (upper - lower); } @Override public double inverseCumulativeProbability(final double p) throws OutOfRangeException { if (p < 0.0 || p > 1.0) { throw new OutOfRangeException(p, 0, 1); } return p * (upper - lower) + lower; } /** * {@inheritDoc} * * For lower bound {@code lower} and upper bound {@code upper}, the mean is * {@code 0.5 * (lower + upper)}. */ public double getNumericalMean() { return 0.5 * (lower + upper); } /** * {@inheritDoc} * * For lower bound {@code lower} and upper bound {@code upper}, the * variance is {@code (upper - lower)^2 / 12}. */ public double getNumericalVariance() { double ul = upper - lower; return ul * ul / 12; } /** * {@inheritDoc} * * The lower bound of the support is equal to the lower bound parameter * of the distribution. * * @return lower bound of the support */ public double getSupportLowerBound() { return lower; } /** * {@inheritDoc} * * The upper bound of the support is equal to the upper bound parameter * of the distribution. * * @return upper bound of the support */ public double getSupportUpperBound() { return upper; } /** {@inheritDoc} */ public boolean isSupportLowerBoundInclusive() { return true; } /** {@inheritDoc} */ public boolean isSupportUpperBoundInclusive() { return true; } /** * {@inheritDoc} * * The support of this distribution is connected. * * @return {@code true} */ public boolean isSupportConnected() { return true; } /** {@inheritDoc} */ @Override public double sample() { final double u = random.nextDouble(); return u * upper + (1 - u) * lower; } } ././@LongLink100644 0 0 145 12126630647 10262 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/distribution/LogNormalDistribution.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/distribution/LogNormalDistribution.java100644 1750 1750 23760 12126627700 32423 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.distribution; import org.apache.commons.math3.exception.NotStrictlyPositiveException; import org.apache.commons.math3.exception.NumberIsTooLargeException; import org.apache.commons.math3.exception.util.LocalizedFormats; import org.apache.commons.math3.special.Erf; import org.apache.commons.math3.util.FastMath; import org.apache.commons.math3.random.RandomGenerator; import org.apache.commons.math3.random.Well19937c; /** * Implementation of the log-normal (gaussian) distribution. * *

                              * Parameters: * {@code X} is log-normally distributed if its natural logarithm {@code log(X)} * is normally distributed. The probability distribution function of {@code X} * is given by (for {@code x > 0}) *

                              *

                              * {@code exp(-0.5 * ((ln(x) - m) / s)^2) / (s * sqrt(2 * pi) * x)} *

                              *
                                *
                              • {@code m} is the scale parameter: this is the mean of the * normally distributed natural logarithm of this distribution,
                              • *
                              • {@code s} is the shape parameter: this is the standard * deviation of the normally distributed natural logarithm of this * distribution. *
                              * * @see * Log-normal distribution (Wikipedia) * @see * Log Normal distribution (MathWorld) * * @version $Id: LogNormalDistribution.java 1422195 2012-12-15 06:45:18Z psteitz $ * @since 3.0 */ public class LogNormalDistribution extends AbstractRealDistribution { /** Default inverse cumulative probability accuracy. */ public static final double DEFAULT_INVERSE_ABSOLUTE_ACCURACY = 1e-9; /** Serializable version identifier. */ private static final long serialVersionUID = 20120112; /** √(2 π) */ private static final double SQRT2PI = FastMath.sqrt(2 * FastMath.PI); /** √(2) */ private static final double SQRT2 = FastMath.sqrt(2.0); /** The scale parameter of this distribution. */ private final double scale; /** The shape parameter of this distribution. */ private final double shape; /** Inverse cumulative probability accuracy. */ private final double solverAbsoluteAccuracy; /** * Create a log-normal distribution, where the mean and standard deviation * of the {@link NormalDistribution normally distributed} natural * logarithm of the log-normal distribution are equal to zero and one * respectively. In other words, the scale of the returned distribution is * {@code 0}, while its shape is {@code 1}. */ public LogNormalDistribution() { this(0, 1); } /** * Create a log-normal distribution using the specified scale and shape. * * @param scale the scale parameter of this distribution * @param shape the shape parameter of this distribution * @throws NotStrictlyPositiveException if {@code shape <= 0}. */ public LogNormalDistribution(double scale, double shape) throws NotStrictlyPositiveException { this(scale, shape, DEFAULT_INVERSE_ABSOLUTE_ACCURACY); } /** * Create a log-normal distribution using the specified scale, shape and * inverse cumulative distribution accuracy. * * @param scale the scale parameter of this distribution * @param shape the shape parameter of this distribution * @param inverseCumAccuracy Inverse cumulative probability accuracy. * @throws NotStrictlyPositiveException if {@code shape <= 0}. */ public LogNormalDistribution(double scale, double shape, double inverseCumAccuracy) throws NotStrictlyPositiveException { this(new Well19937c(), scale, shape, inverseCumAccuracy); } /** * Creates a log-normal distribution. * * @param rng Random number generator. * @param scale Scale parameter of this distribution. * @param shape Shape parameter of this distribution. * @param inverseCumAccuracy Inverse cumulative probability accuracy. * @throws NotStrictlyPositiveException if {@code shape <= 0}. * @since 3.1 */ public LogNormalDistribution(RandomGenerator rng, double scale, double shape, double inverseCumAccuracy) throws NotStrictlyPositiveException { super(rng); if (shape <= 0) { throw new NotStrictlyPositiveException(LocalizedFormats.SHAPE, shape); } this.scale = scale; this.shape = shape; this.solverAbsoluteAccuracy = inverseCumAccuracy; } /** * Returns the scale parameter of this distribution. * * @return the scale parameter */ public double getScale() { return scale; } /** * Returns the shape parameter of this distribution. * * @return the shape parameter */ public double getShape() { return shape; } /** * {@inheritDoc} * * For scale {@code m}, and shape {@code s} of this distribution, the PDF * is given by *
                                *
                              • {@code 0} if {@code x <= 0},
                              • *
                              • {@code exp(-0.5 * ((ln(x) - m) / s)^2) / (s * sqrt(2 * pi) * x)} * otherwise.
                              • *
                              */ public double density(double x) { if (x <= 0) { return 0; } final double x0 = FastMath.log(x) - scale; final double x1 = x0 / shape; return FastMath.exp(-0.5 * x1 * x1) / (shape * SQRT2PI * x); } /** * {@inheritDoc} * * For scale {@code m}, and shape {@code s} of this distribution, the CDF * is given by *
                                *
                              • {@code 0} if {@code x <= 0},
                              • *
                              • {@code 0} if {@code ln(x) - m < 0} and {@code m - ln(x) > 40 * s}, as * in these cases the actual value is within {@code Double.MIN_VALUE} of 0, *
                              • {@code 1} if {@code ln(x) - m >= 0} and {@code ln(x) - m > 40 * s}, * as in these cases the actual value is within {@code Double.MIN_VALUE} of * 1,
                              • *
                              • {@code 0.5 + 0.5 * erf((ln(x) - m) / (s * sqrt(2))} otherwise.
                              • *
                              */ public double cumulativeProbability(double x) { if (x <= 0) { return 0; } final double dev = FastMath.log(x) - scale; if (FastMath.abs(dev) > 40 * shape) { return dev < 0 ? 0.0d : 1.0d; } return 0.5 + 0.5 * Erf.erf(dev / (shape * SQRT2)); } /** * {@inheritDoc} * * @deprecated See {@link RealDistribution#cumulativeProbability(double,double)} */ @Override@Deprecated public double cumulativeProbability(double x0, double x1) throws NumberIsTooLargeException { return probability(x0, x1); } /** {@inheritDoc} */ @Override public double probability(double x0, double x1) throws NumberIsTooLargeException { if (x0 > x1) { throw new NumberIsTooLargeException(LocalizedFormats.LOWER_ENDPOINT_ABOVE_UPPER_ENDPOINT, x0, x1, true); } if (x0 <= 0 || x1 <= 0) { return super.probability(x0, x1); } final double denom = shape * SQRT2; final double v0 = (FastMath.log(x0) - scale) / denom; final double v1 = (FastMath.log(x1) - scale) / denom; return 0.5 * Erf.erf(v0, v1); } /** {@inheritDoc} */ @Override protected double getSolverAbsoluteAccuracy() { return solverAbsoluteAccuracy; } /** * {@inheritDoc} * * For scale {@code m} and shape {@code s}, the mean is * {@code exp(m + s^2 / 2)}. */ public double getNumericalMean() { double s = shape; return FastMath.exp(scale + (s * s / 2)); } /** * {@inheritDoc} * * For scale {@code m} and shape {@code s}, the variance is * {@code (exp(s^2) - 1) * exp(2 * m + s^2)}. */ public double getNumericalVariance() { final double s = shape; final double ss = s * s; return (FastMath.exp(ss) - 1) * FastMath.exp(2 * scale + ss); } /** * {@inheritDoc} * * The lower bound of the support is always 0 no matter the parameters. * * @return lower bound of the support (always 0) */ public double getSupportLowerBound() { return 0; } /** * {@inheritDoc} * * The upper bound of the support is always positive infinity * no matter the parameters. * * @return upper bound of the support (always * {@code Double.POSITIVE_INFINITY}) */ public double getSupportUpperBound() { return Double.POSITIVE_INFINITY; } /** {@inheritDoc} */ public boolean isSupportLowerBoundInclusive() { return true; } /** {@inheritDoc} */ public boolean isSupportUpperBoundInclusive() { return false; } /** * {@inheritDoc} * * The support of this distribution is connected. * * @return {@code true} */ public boolean isSupportConnected() { return true; } /** {@inheritDoc} */ @Override public double sample() { final double n = random.nextGaussian(); return FastMath.exp(scale + shape * n); } } ././@LongLink100644 0 0 165 12126630647 10264 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/distribution/MixtureMultivariateNormalDistribution.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/distribution/MixtureMultivariateNormalD100644 1750 1750 7777 12126627700 32504 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.distribution; import java.util.List; import java.util.ArrayList; import org.apache.commons.math3.exception.DimensionMismatchException; import org.apache.commons.math3.exception.NotPositiveException; import org.apache.commons.math3.random.RandomGenerator; import org.apache.commons.math3.util.Pair; /** * Multivariate normal mixture distribution. * This class is mainly syntactic sugar. * * @see MixtureMultivariateRealDistribution * @version $Id: MixtureMultivariateNormalDistribution.java 1459551 2013-03-21 22:17:44Z tn $ * @since 3.2 */ public class MixtureMultivariateNormalDistribution extends MixtureMultivariateRealDistribution { /** * Creates a multivariate normal mixture distribution. * * @param weights Weights of each component. * @param means Mean vector for each component. * @param covariances Covariance matrix for each component. */ public MixtureMultivariateNormalDistribution(double[] weights, double[][] means, double[][][] covariances) { super(createComponents(weights, means, covariances)); } /** * Creates a mixture model from a list of distributions and their * associated weights. * * @param components List of (weight, distribution) pairs from which to sample. */ public MixtureMultivariateNormalDistribution(List> components) { super(components); } /** * Creates a mixture model from a list of distributions and their * associated weights. * * @param rng Random number generator. * @param components Distributions from which to sample. * @throws NotPositiveException if any of the weights is negative. * @throws DimensionMismatchException if not all components have the same * number of variables. */ public MixtureMultivariateNormalDistribution(RandomGenerator rng, List> components) throws NotPositiveException, DimensionMismatchException { super(rng, components); } /** * @param weights Weights of each component. * @param means Mean vector for each component. * @param covariances Covariance matrix for each component. * @return the list of components. */ private static List> createComponents(double[] weights, double[][] means, double[][][] covariances) { final List> mvns = new ArrayList>(); for (int i = 0; i < weights.length; i++) { final MultivariateNormalDistribution dist = new MultivariateNormalDistribution(means[i], covariances[i]); mvns.add(new Pair(weights[i], dist)); } return mvns; } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/distribution/WeibullDistribution.java100644 1750 1750 23062 12126627700 32127 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.distribution; import org.apache.commons.math3.exception.OutOfRangeException; import org.apache.commons.math3.exception.NotStrictlyPositiveException; import org.apache.commons.math3.exception.util.LocalizedFormats; import org.apache.commons.math3.special.Gamma; import org.apache.commons.math3.util.FastMath; import org.apache.commons.math3.random.RandomGenerator; import org.apache.commons.math3.random.Well19937c; /** * Implementation of the Weibull distribution. This implementation uses the * two parameter form of the distribution defined by * * Weibull Distribution, equations (1) and (2). * * @see Weibull distribution (Wikipedia) * @see Weibull distribution (MathWorld) * @since 1.1 (changed to concrete class in 3.0) * @version $Id: WeibullDistribution.java 1416643 2012-12-03 19:37:14Z tn $ */ public class WeibullDistribution extends AbstractRealDistribution { /** * Default inverse cumulative probability accuracy. * @since 2.1 */ public static final double DEFAULT_INVERSE_ABSOLUTE_ACCURACY = 1e-9; /** Serializable version identifier. */ private static final long serialVersionUID = 8589540077390120676L; /** The shape parameter. */ private final double shape; /** The scale parameter. */ private final double scale; /** Inverse cumulative probability accuracy. */ private final double solverAbsoluteAccuracy; /** Cached numerical mean */ private double numericalMean = Double.NaN; /** Whether or not the numerical mean has been calculated */ private boolean numericalMeanIsCalculated = false; /** Cached numerical variance */ private double numericalVariance = Double.NaN; /** Whether or not the numerical variance has been calculated */ private boolean numericalVarianceIsCalculated = false; /** * Create a Weibull distribution with the given shape and scale and a * location equal to zero. * * @param alpha Shape parameter. * @param beta Scale parameter. * @throws NotStrictlyPositiveException if {@code alpha <= 0} or * {@code beta <= 0}. */ public WeibullDistribution(double alpha, double beta) throws NotStrictlyPositiveException { this(alpha, beta, DEFAULT_INVERSE_ABSOLUTE_ACCURACY); } /** * Create a Weibull distribution with the given shape, scale and inverse * cumulative probability accuracy and a location equal to zero. * * @param alpha Shape parameter. * @param beta Scale parameter. * @param inverseCumAccuracy Maximum absolute error in inverse * cumulative probability estimates * (defaults to {@link #DEFAULT_INVERSE_ABSOLUTE_ACCURACY}). * @throws NotStrictlyPositiveException if {@code alpha <= 0} or * {@code beta <= 0}. * @since 2.1 */ public WeibullDistribution(double alpha, double beta, double inverseCumAccuracy) { this(new Well19937c(), alpha, beta, inverseCumAccuracy); } /** * Creates a Weibull distribution. * * @param rng Random number generator. * @param alpha Shape parameter. * @param beta Scale parameter. * @param inverseCumAccuracy Maximum absolute error in inverse * cumulative probability estimates * (defaults to {@link #DEFAULT_INVERSE_ABSOLUTE_ACCURACY}). * @throws NotStrictlyPositiveException if {@code alpha <= 0} or * {@code beta <= 0}. * @since 3.1 */ public WeibullDistribution(RandomGenerator rng, double alpha, double beta, double inverseCumAccuracy) throws NotStrictlyPositiveException { super(rng); if (alpha <= 0) { throw new NotStrictlyPositiveException(LocalizedFormats.SHAPE, alpha); } if (beta <= 0) { throw new NotStrictlyPositiveException(LocalizedFormats.SCALE, beta); } scale = beta; shape = alpha; solverAbsoluteAccuracy = inverseCumAccuracy; } /** * Access the shape parameter, {@code alpha}. * * @return the shape parameter, {@code alpha}. */ public double getShape() { return shape; } /** * Access the scale parameter, {@code beta}. * * @return the scale parameter, {@code beta}. */ public double getScale() { return scale; } /** {@inheritDoc} */ public double density(double x) { if (x < 0) { return 0; } final double xscale = x / scale; final double xscalepow = FastMath.pow(xscale, shape - 1); /* * FastMath.pow(x / scale, shape) = * FastMath.pow(xscale, shape) = * FastMath.pow(xscale, shape - 1) * xscale */ final double xscalepowshape = xscalepow * xscale; return (shape / scale) * xscalepow * FastMath.exp(-xscalepowshape); } /** {@inheritDoc} */ public double cumulativeProbability(double x) { double ret; if (x <= 0.0) { ret = 0.0; } else { ret = 1.0 - FastMath.exp(-FastMath.pow(x / scale, shape)); } return ret; } /** * {@inheritDoc} * * Returns {@code 0} when {@code p == 0} and * {@code Double.POSITIVE_INFINITY} when {@code p == 1}. */ @Override public double inverseCumulativeProbability(double p) { double ret; if (p < 0.0 || p > 1.0) { throw new OutOfRangeException(p, 0.0, 1.0); } else if (p == 0) { ret = 0.0; } else if (p == 1) { ret = Double.POSITIVE_INFINITY; } else { ret = scale * FastMath.pow(-FastMath.log(1.0 - p), 1.0 / shape); } return ret; } /** * Return the absolute accuracy setting of the solver used to estimate * inverse cumulative probabilities. * * @return the solver absolute accuracy. * @since 2.1 */ @Override protected double getSolverAbsoluteAccuracy() { return solverAbsoluteAccuracy; } /** * {@inheritDoc} * * The mean is {@code scale * Gamma(1 + (1 / shape))}, where {@code Gamma()} * is the Gamma-function. */ public double getNumericalMean() { if (!numericalMeanIsCalculated) { numericalMean = calculateNumericalMean(); numericalMeanIsCalculated = true; } return numericalMean; } /** * used by {@link #getNumericalMean()} * * @return the mean of this distribution */ protected double calculateNumericalMean() { final double sh = getShape(); final double sc = getScale(); return sc * FastMath.exp(Gamma.logGamma(1 + (1 / sh))); } /** * {@inheritDoc} * * The variance is {@code scale^2 * Gamma(1 + (2 / shape)) - mean^2} * where {@code Gamma()} is the Gamma-function. */ public double getNumericalVariance() { if (!numericalVarianceIsCalculated) { numericalVariance = calculateNumericalVariance(); numericalVarianceIsCalculated = true; } return numericalVariance; } /** * used by {@link #getNumericalVariance()} * * @return the variance of this distribution */ protected double calculateNumericalVariance() { final double sh = getShape(); final double sc = getScale(); final double mn = getNumericalMean(); return (sc * sc) * FastMath.exp(Gamma.logGamma(1 + (2 / sh))) - (mn * mn); } /** * {@inheritDoc} * * The lower bound of the support is always 0 no matter the parameters. * * @return lower bound of the support (always 0) */ public double getSupportLowerBound() { return 0; } /** * {@inheritDoc} * * The upper bound of the support is always positive infinity * no matter the parameters. * * @return upper bound of the support (always * {@code Double.POSITIVE_INFINITY}) */ public double getSupportUpperBound() { return Double.POSITIVE_INFINITY; } /** {@inheritDoc} */ public boolean isSupportLowerBoundInclusive() { return true; } /** {@inheritDoc} */ public boolean isSupportUpperBoundInclusive() { return false; } /** * {@inheritDoc} * * The support of this distribution is connected. * * @return {@code true} */ public boolean isSupportConnected() { return true; } } ././@LongLink100644 0 0 147 12126630647 10264 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/distribution/ExponentialDistribution.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/distribution/ExponentialDistribution.ja100644 1750 1750 22236 12126627700 32465 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.distribution; import org.apache.commons.math3.exception.NotStrictlyPositiveException; import org.apache.commons.math3.exception.OutOfRangeException; import org.apache.commons.math3.exception.util.LocalizedFormats; import org.apache.commons.math3.util.FastMath; import org.apache.commons.math3.util.ArithmeticUtils; import org.apache.commons.math3.util.ResizableDoubleArray; import org.apache.commons.math3.random.RandomGenerator; import org.apache.commons.math3.random.Well19937c; /** * Implementation of the exponential distribution. * * @see Exponential distribution (Wikipedia) * @see Exponential distribution (MathWorld) * @version $Id: ExponentialDistribution.java 1416643 2012-12-03 19:37:14Z tn $ */ public class ExponentialDistribution extends AbstractRealDistribution { /** * Default inverse cumulative probability accuracy. * @since 2.1 */ public static final double DEFAULT_INVERSE_ABSOLUTE_ACCURACY = 1e-9; /** Serializable version identifier */ private static final long serialVersionUID = 2401296428283614780L; /** * Used when generating Exponential samples. * Table containing the constants * q_i = sum_{j=1}^i (ln 2)^j/j! = ln 2 + (ln 2)^2/2 + ... + (ln 2)^i/i! * until the largest representable fraction below 1 is exceeded. * * Note that * 1 = 2 - 1 = exp(ln 2) - 1 = sum_{n=1}^infty (ln 2)^n / n! * thus q_i -> 1 as i -> +inf, * so the higher i, the closer to one we get (the series is not alternating). * * By trying, n = 16 in Java is enough to reach 1.0. */ private static final double[] EXPONENTIAL_SA_QI; /** The mean of this distribution. */ private final double mean; /** Inverse cumulative probability accuracy. */ private final double solverAbsoluteAccuracy; /** * Initialize tables. */ static { /** * Filling EXPONENTIAL_SA_QI table. * Note that we don't want qi = 0 in the table. */ final double LN2 = FastMath.log(2); double qi = 0; int i = 1; /** * ArithmeticUtils provides factorials up to 20, so let's use that * limit together with Precision.EPSILON to generate the following * code (a priori, we know that there will be 16 elements, but it is * better to not hardcode it). */ final ResizableDoubleArray ra = new ResizableDoubleArray(20); while (qi < 1) { qi += FastMath.pow(LN2, i) / ArithmeticUtils.factorial(i); ra.addElement(qi); ++i; } EXPONENTIAL_SA_QI = ra.getElements(); } /** * Create an exponential distribution with the given mean. * @param mean mean of this distribution. */ public ExponentialDistribution(double mean) { this(mean, DEFAULT_INVERSE_ABSOLUTE_ACCURACY); } /** * Create an exponential distribution with the given mean. * * @param mean Mean of this distribution. * @param inverseCumAccuracy Maximum absolute error in inverse * cumulative probability estimates (defaults to * {@link #DEFAULT_INVERSE_ABSOLUTE_ACCURACY}). * @throws NotStrictlyPositiveException if {@code mean <= 0}. * @since 2.1 */ public ExponentialDistribution(double mean, double inverseCumAccuracy) { this(new Well19937c(), mean, inverseCumAccuracy); } /** * Creates an exponential distribution. * * @param rng Random number generator. * @param mean Mean of this distribution. * @param inverseCumAccuracy Maximum absolute error in inverse * cumulative probability estimates (defaults to * {@link #DEFAULT_INVERSE_ABSOLUTE_ACCURACY}). * @throws NotStrictlyPositiveException if {@code mean <= 0}. * @since 3.1 */ public ExponentialDistribution(RandomGenerator rng, double mean, double inverseCumAccuracy) throws NotStrictlyPositiveException { super(rng); if (mean <= 0) { throw new NotStrictlyPositiveException(LocalizedFormats.MEAN, mean); } this.mean = mean; solverAbsoluteAccuracy = inverseCumAccuracy; } /** * Access the mean. * * @return the mean. */ public double getMean() { return mean; } /** {@inheritDoc} */ public double density(double x) { if (x < 0) { return 0; } return FastMath.exp(-x / mean) / mean; } /** * {@inheritDoc} * * The implementation of this method is based on: * */ public double cumulativeProbability(double x) { double ret; if (x <= 0.0) { ret = 0.0; } else { ret = 1.0 - FastMath.exp(-x / mean); } return ret; } /** * {@inheritDoc} * * Returns {@code 0} when {@code p= = 0} and * {@code Double.POSITIVE_INFINITY} when {@code p == 1}. */ @Override public double inverseCumulativeProbability(double p) throws OutOfRangeException { double ret; if (p < 0.0 || p > 1.0) { throw new OutOfRangeException(p, 0.0, 1.0); } else if (p == 1.0) { ret = Double.POSITIVE_INFINITY; } else { ret = -mean * FastMath.log(1.0 - p); } return ret; } /** * {@inheritDoc} * *

                              Algorithm Description: this implementation uses the * * Inversion Method to generate exponentially distributed random values * from uniform deviates.

                              * * @return a random value. * @since 2.2 */ @Override public double sample() { // Step 1: double a = 0; double u = random.nextDouble(); // Step 2 and 3: while (u < 0.5) { a += EXPONENTIAL_SA_QI[0]; u *= 2; } // Step 4 (now u >= 0.5): u += u - 1; // Step 5: if (u <= EXPONENTIAL_SA_QI[0]) { return mean * (a + u); } // Step 6: int i = 0; // Should be 1, be we iterate before it in while using 0 double u2 = random.nextDouble(); double umin = u2; // Step 7 and 8: do { ++i; u2 = random.nextDouble(); if (u2 < umin) { umin = u2; } // Step 8: } while (u > EXPONENTIAL_SA_QI[i]); // Ensured to exit since EXPONENTIAL_SA_QI[MAX] = 1 return mean * (a + umin * EXPONENTIAL_SA_QI[0]); } /** {@inheritDoc} */ @Override protected double getSolverAbsoluteAccuracy() { return solverAbsoluteAccuracy; } /** * {@inheritDoc} * * For mean parameter {@code k}, the mean is {@code k}. */ public double getNumericalMean() { return getMean(); } /** * {@inheritDoc} * * For mean parameter {@code k}, the variance is {@code k^2}. */ public double getNumericalVariance() { final double m = getMean(); return m * m; } /** * {@inheritDoc} * * The lower bound of the support is always 0 no matter the mean parameter. * * @return lower bound of the support (always 0) */ public double getSupportLowerBound() { return 0; } /** * {@inheritDoc} * * The upper bound of the support is always positive infinity * no matter the mean parameter. * * @return upper bound of the support (always Double.POSITIVE_INFINITY) */ public double getSupportUpperBound() { return Double.POSITIVE_INFINITY; } /** {@inheritDoc} */ public boolean isSupportLowerBoundInclusive() { return true; } /** {@inheritDoc} */ public boolean isSupportUpperBoundInclusive() { return false; } /** * {@inheritDoc} * * The support of this distribution is connected. * * @return {@code true} */ public boolean isSupportConnected() { return true; } } ././@LongLink100644 0 0 155 12126630647 10263 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/distribution/EnumeratedIntegerDistribution.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/distribution/EnumeratedIntegerDistribut100644 1750 1750 16234 12126627700 32510 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.distribution; import java.util.ArrayList; import java.util.List; import org.apache.commons.math3.exception.DimensionMismatchException; import org.apache.commons.math3.exception.MathArithmeticException; import org.apache.commons.math3.exception.NotANumberException; import org.apache.commons.math3.exception.NotFiniteNumberException; import org.apache.commons.math3.exception.NotPositiveException; import org.apache.commons.math3.random.RandomGenerator; import org.apache.commons.math3.random.Well19937c; import org.apache.commons.math3.util.Pair; /** *

                              Implementation of an integer-valued {@link EnumeratedDistribution}.

                              * *

                              Values with zero-probability are allowed but they do not extend the * support.
                              * Duplicate values are allowed. Probabilities of duplicate values are combined * when computing cumulative probabilities and statistics.

                              * * @version $Id: EnumeratedIntegerDistribution.java 1456769 2013-03-15 04:51:34Z psteitz $ * @since 3.2 */ public class EnumeratedIntegerDistribution extends AbstractIntegerDistribution { /** Serializable UID. */ private static final long serialVersionUID = 20130308L; /** * {@link EnumeratedDistribution} instance (using the {@link Integer} wrapper) * used to generate the pmf. */ protected final EnumeratedDistribution innerDistribution; /** * Create a discrete distribution using the given probability mass function * definition. * * @param singletons array of random variable values. * @param probabilities array of probabilities. * @throws DimensionMismatchException if * {@code singletons.length != probabilities.length} * @throws NotPositiveException if any of the probabilities are negative. * @throws NotFiniteNumberException if any of the probabilities are infinite. * @throws NotANumberException if any of the probabilities are NaN. * @throws MathArithmeticException all of the probabilities are 0. */ public EnumeratedIntegerDistribution(final int[] singletons, final double[] probabilities) throws DimensionMismatchException, NotPositiveException, MathArithmeticException, NotFiniteNumberException, NotANumberException{ this(new Well19937c(), singletons, probabilities); } /** * Create a discrete distribution using the given random number generator * and probability mass function definition. * * @param rng random number generator. * @param singletons array of random variable values. * @param probabilities array of probabilities. * @throws DimensionMismatchException if * {@code singletons.length != probabilities.length} * @throws NotPositiveException if any of the probabilities are negative. * @throws NotFiniteNumberException if any of the probabilities are infinite. * @throws NotANumberException if any of the probabilities are NaN. * @throws MathArithmeticException all of the probabilities are 0. */ public EnumeratedIntegerDistribution(final RandomGenerator rng, final int[] singletons, final double[] probabilities) throws DimensionMismatchException, NotPositiveException, MathArithmeticException, NotFiniteNumberException, NotANumberException { super(rng); if (singletons.length != probabilities.length) { throw new DimensionMismatchException(probabilities.length, singletons.length); } final List> samples = new ArrayList>(singletons.length); for (int i = 0; i < singletons.length; i++) { samples.add(new Pair(singletons[i], probabilities[i])); } innerDistribution = new EnumeratedDistribution(rng, samples); } /** * {@inheritDoc} */ public double probability(final int x) { return innerDistribution.probability(x); } /** * {@inheritDoc} */ public double cumulativeProbability(final int x) { double probability = 0; for (final Pair sample : innerDistribution.getPmf()) { if (sample.getKey() <= x) { probability += sample.getValue(); } } return probability; } /** * {@inheritDoc} * * @return {@code sum(singletons[i] * probabilities[i])} */ public double getNumericalMean() { double mean = 0; for (final Pair sample : innerDistribution.getPmf()) { mean += sample.getValue() * sample.getKey(); } return mean; } /** * {@inheritDoc} * * @return {@code sum((singletons[i] - mean) ^ 2 * probabilities[i])} */ public double getNumericalVariance() { double mean = 0; double meanOfSquares = 0; for (final Pair sample : innerDistribution.getPmf()) { mean += sample.getValue() * sample.getKey(); meanOfSquares += sample.getValue() * sample.getKey() * sample.getKey(); } return meanOfSquares - mean * mean; } /** * {@inheritDoc} * * Returns the lowest value with non-zero probability. * * @return the lowest value with non-zero probability. */ public int getSupportLowerBound() { int min = Integer.MAX_VALUE; for (final Pair sample : innerDistribution.getPmf()) { if (sample.getKey() < min && sample.getValue() > 0) { min = sample.getKey(); } } return min; } /** * {@inheritDoc} * * Returns the highest value with non-zero probability. * * @return the highest value with non-zero probability. */ public int getSupportUpperBound() { int max = Integer.MIN_VALUE; for (final Pair sample : innerDistribution.getPmf()) { if (sample.getKey() > max && sample.getValue() > 0) { max = sample.getKey(); } } return max; } /** * {@inheritDoc} * * The support of this distribution is connected. * * @return {@code true} */ public boolean isSupportConnected() { return true; } /** * {@inheritDoc} */ @Override public int sample() { return innerDistribution.sample(); } } ././@LongLink100644 0 0 146 12126630647 10263 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/distribution/ChiSquaredDistribution.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/distribution/ChiSquaredDistribution.jav100644 1750 1750 12461 12126627700 32414 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.distribution; import org.apache.commons.math3.random.RandomGenerator; import org.apache.commons.math3.random.Well19937c; /** * Implementation of the chi-squared distribution. * * @see Chi-squared distribution (Wikipedia) * @see Chi-squared Distribution (MathWorld) * @version $Id: ChiSquaredDistribution.java 1416643 2012-12-03 19:37:14Z tn $ */ public class ChiSquaredDistribution extends AbstractRealDistribution { /** * Default inverse cumulative probability accuracy * @since 2.1 */ public static final double DEFAULT_INVERSE_ABSOLUTE_ACCURACY = 1e-9; /** Serializable version identifier */ private static final long serialVersionUID = -8352658048349159782L; /** Internal Gamma distribution. */ private final GammaDistribution gamma; /** Inverse cumulative probability accuracy */ private final double solverAbsoluteAccuracy; /** * Create a Chi-Squared distribution with the given degrees of freedom. * * @param degreesOfFreedom Degrees of freedom. */ public ChiSquaredDistribution(double degreesOfFreedom) { this(degreesOfFreedom, DEFAULT_INVERSE_ABSOLUTE_ACCURACY); } /** * Create a Chi-Squared distribution with the given degrees of freedom and * inverse cumulative probability accuracy. * * @param degreesOfFreedom Degrees of freedom. * @param inverseCumAccuracy the maximum absolute error in inverse * cumulative probability estimates (defaults to * {@link #DEFAULT_INVERSE_ABSOLUTE_ACCURACY}). * @since 2.1 */ public ChiSquaredDistribution(double degreesOfFreedom, double inverseCumAccuracy) { this(new Well19937c(), degreesOfFreedom, inverseCumAccuracy); } /** * Create a Chi-Squared distribution with the given degrees of freedom and * inverse cumulative probability accuracy. * * @param rng Random number generator. * @param degreesOfFreedom Degrees of freedom. * @param inverseCumAccuracy the maximum absolute error in inverse * cumulative probability estimates (defaults to * {@link #DEFAULT_INVERSE_ABSOLUTE_ACCURACY}). * @since 3.1 */ public ChiSquaredDistribution(RandomGenerator rng, double degreesOfFreedom, double inverseCumAccuracy) { super(rng); gamma = new GammaDistribution(degreesOfFreedom / 2, 2); solverAbsoluteAccuracy = inverseCumAccuracy; } /** * Access the number of degrees of freedom. * * @return the degrees of freedom. */ public double getDegreesOfFreedom() { return gamma.getShape() * 2.0; } /** {@inheritDoc} */ public double density(double x) { return gamma.density(x); } /** {@inheritDoc} */ public double cumulativeProbability(double x) { return gamma.cumulativeProbability(x); } /** {@inheritDoc} */ @Override protected double getSolverAbsoluteAccuracy() { return solverAbsoluteAccuracy; } /** * {@inheritDoc} * * For {@code k} degrees of freedom, the mean is {@code k}. */ public double getNumericalMean() { return getDegreesOfFreedom(); } /** * {@inheritDoc} * * @return {@code 2 * k}, where {@code k} is the number of degrees of freedom. */ public double getNumericalVariance() { return 2 * getDegreesOfFreedom(); } /** * {@inheritDoc} * * The lower bound of the support is always 0 no matter the * degrees of freedom. * * @return zero. */ public double getSupportLowerBound() { return 0; } /** * {@inheritDoc} * * The upper bound of the support is always positive infinity no matter the * degrees of freedom. * * @return {@code Double.POSITIVE_INFINITY}. */ public double getSupportUpperBound() { return Double.POSITIVE_INFINITY; } /** {@inheritDoc} */ public boolean isSupportLowerBoundInclusive() { return true; } /** {@inheritDoc} */ public boolean isSupportUpperBoundInclusive() { return false; } /** * {@inheritDoc} * * The support of this distribution is connected. * * @return {@code true} */ public boolean isSupportConnected() { return true; } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/distribution/TDistribution.java100644 1750 1750 16647 12126627700 30742 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.distribution; import org.apache.commons.math3.exception.NotStrictlyPositiveException; import org.apache.commons.math3.exception.util.LocalizedFormats; import org.apache.commons.math3.special.Beta; import org.apache.commons.math3.special.Gamma; import org.apache.commons.math3.util.FastMath; import org.apache.commons.math3.random.RandomGenerator; import org.apache.commons.math3.random.Well19937c; /** * Implementation of Student's t-distribution. * * @see "Student's t-distribution (Wikipedia)" * @see "Student's t-distribution (MathWorld)" * @version $Id: TDistribution.java 1416643 2012-12-03 19:37:14Z tn $ */ public class TDistribution extends AbstractRealDistribution { /** * Default inverse cumulative probability accuracy. * @since 2.1 */ public static final double DEFAULT_INVERSE_ABSOLUTE_ACCURACY = 1e-9; /** Serializable version identifier */ private static final long serialVersionUID = -5852615386664158222L; /** The degrees of freedom. */ private final double degreesOfFreedom; /** Inverse cumulative probability accuracy. */ private final double solverAbsoluteAccuracy; /** * Create a t distribution using the given degrees of freedom. * * @param degreesOfFreedom Degrees of freedom. * @throws NotStrictlyPositiveException if {@code degreesOfFreedom <= 0} */ public TDistribution(double degreesOfFreedom) throws NotStrictlyPositiveException { this(degreesOfFreedom, DEFAULT_INVERSE_ABSOLUTE_ACCURACY); } /** * Create a t distribution using the given degrees of freedom and the * specified inverse cumulative probability absolute accuracy. * * @param degreesOfFreedom Degrees of freedom. * @param inverseCumAccuracy the maximum absolute error in inverse * cumulative probability estimates * (defaults to {@link #DEFAULT_INVERSE_ABSOLUTE_ACCURACY}). * @throws NotStrictlyPositiveException if {@code degreesOfFreedom <= 0} * @since 2.1 */ public TDistribution(double degreesOfFreedom, double inverseCumAccuracy) throws NotStrictlyPositiveException { this(new Well19937c(), degreesOfFreedom, inverseCumAccuracy); } /** * Creates a t distribution. * * @param rng Random number generator. * @param degreesOfFreedom Degrees of freedom. * @param inverseCumAccuracy the maximum absolute error in inverse * cumulative probability estimates * (defaults to {@link #DEFAULT_INVERSE_ABSOLUTE_ACCURACY}). * @throws NotStrictlyPositiveException if {@code degreesOfFreedom <= 0} * @since 3.1 */ public TDistribution(RandomGenerator rng, double degreesOfFreedom, double inverseCumAccuracy) throws NotStrictlyPositiveException { super(rng); if (degreesOfFreedom <= 0) { throw new NotStrictlyPositiveException(LocalizedFormats.DEGREES_OF_FREEDOM, degreesOfFreedom); } this.degreesOfFreedom = degreesOfFreedom; solverAbsoluteAccuracy = inverseCumAccuracy; } /** * Access the degrees of freedom. * * @return the degrees of freedom. */ public double getDegreesOfFreedom() { return degreesOfFreedom; } /** {@inheritDoc} */ public double density(double x) { final double n = degreesOfFreedom; final double nPlus1Over2 = (n + 1) / 2; return FastMath.exp(Gamma.logGamma(nPlus1Over2) - 0.5 * (FastMath.log(FastMath.PI) + FastMath.log(n)) - Gamma.logGamma(n / 2) - nPlus1Over2 * FastMath.log(1 + x * x / n)); } /** {@inheritDoc} */ public double cumulativeProbability(double x) { double ret; if (x == 0) { ret = 0.5; } else { double t = Beta.regularizedBeta( degreesOfFreedom / (degreesOfFreedom + (x * x)), 0.5 * degreesOfFreedom, 0.5); if (x < 0.0) { ret = 0.5 * t; } else { ret = 1.0 - 0.5 * t; } } return ret; } /** {@inheritDoc} */ @Override protected double getSolverAbsoluteAccuracy() { return solverAbsoluteAccuracy; } /** * {@inheritDoc} * * For degrees of freedom parameter {@code df}, the mean is *
                                *
                              • if {@code df > 1} then {@code 0},
                              • *
                              • else undefined ({@code Double.NaN}).
                              • *
                              */ public double getNumericalMean() { final double df = getDegreesOfFreedom(); if (df > 1) { return 0; } return Double.NaN; } /** * {@inheritDoc} * * For degrees of freedom parameter {@code df}, the variance is *
                                *
                              • if {@code df > 2} then {@code df / (df - 2)},
                              • *
                              • if {@code 1 < df <= 2} then positive infinity * ({@code Double.POSITIVE_INFINITY}),
                              • *
                              • else undefined ({@code Double.NaN}).
                              • *
                              */ public double getNumericalVariance() { final double df = getDegreesOfFreedom(); if (df > 2) { return df / (df - 2); } if (df > 1 && df <= 2) { return Double.POSITIVE_INFINITY; } return Double.NaN; } /** * {@inheritDoc} * * The lower bound of the support is always negative infinity no matter the * parameters. * * @return lower bound of the support (always * {@code Double.NEGATIVE_INFINITY}) */ public double getSupportLowerBound() { return Double.NEGATIVE_INFINITY; } /** * {@inheritDoc} * * The upper bound of the support is always positive infinity no matter the * parameters. * * @return upper bound of the support (always * {@code Double.POSITIVE_INFINITY}) */ public double getSupportUpperBound() { return Double.POSITIVE_INFINITY; } /** {@inheritDoc} */ public boolean isSupportLowerBoundInclusive() { return false; } /** {@inheritDoc} */ public boolean isSupportUpperBoundInclusive() { return false; } /** * {@inheritDoc} * * The support of this distribution is connected. * * @return {@code true} */ public boolean isSupportConnected() { return true; } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/distribution/ZipfDistribution.java100644 1750 1750 17307 12126627700 31441 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.distribution; import org.apache.commons.math3.exception.NotStrictlyPositiveException; import org.apache.commons.math3.exception.util.LocalizedFormats; import org.apache.commons.math3.util.FastMath; import org.apache.commons.math3.random.RandomGenerator; import org.apache.commons.math3.random.Well19937c; /** * Implementation of the Zipf distribution. * * @see Zipf distribution (MathWorld) * @version $Id: ZipfDistribution.java 1416643 2012-12-03 19:37:14Z tn $ */ public class ZipfDistribution extends AbstractIntegerDistribution { /** Serializable version identifier. */ private static final long serialVersionUID = -140627372283420404L; /** Number of elements. */ private final int numberOfElements; /** Exponent parameter of the distribution. */ private final double exponent; /** Cached numerical mean */ private double numericalMean = Double.NaN; /** Whether or not the numerical mean has been calculated */ private boolean numericalMeanIsCalculated = false; /** Cached numerical variance */ private double numericalVariance = Double.NaN; /** Whether or not the numerical variance has been calculated */ private boolean numericalVarianceIsCalculated = false; /** * Create a new Zipf distribution with the given number of elements and * exponent. * * @param numberOfElements Number of elements. * @param exponent Exponent. * @exception NotStrictlyPositiveException if {@code numberOfElements <= 0} * or {@code exponent <= 0}. */ public ZipfDistribution(final int numberOfElements, final double exponent) { this(new Well19937c(), numberOfElements, exponent); } /** * Creates a Zipf distribution. * * @param rng Random number generator. * @param numberOfElements Number of elements. * @param exponent Exponent. * @exception NotStrictlyPositiveException if {@code numberOfElements <= 0} * or {@code exponent <= 0}. * @since 3.1 */ public ZipfDistribution(RandomGenerator rng, int numberOfElements, double exponent) throws NotStrictlyPositiveException { super(rng); if (numberOfElements <= 0) { throw new NotStrictlyPositiveException(LocalizedFormats.DIMENSION, numberOfElements); } if (exponent <= 0) { throw new NotStrictlyPositiveException(LocalizedFormats.EXPONENT, exponent); } this.numberOfElements = numberOfElements; this.exponent = exponent; } /** * Get the number of elements (e.g. corpus size) for the distribution. * * @return the number of elements */ public int getNumberOfElements() { return numberOfElements; } /** * Get the exponent characterizing the distribution. * * @return the exponent */ public double getExponent() { return exponent; } /** {@inheritDoc} */ public double probability(final int x) { if (x <= 0 || x > numberOfElements) { return 0.0; } return (1.0 / FastMath.pow(x, exponent)) / generalizedHarmonic(numberOfElements, exponent); } /** {@inheritDoc} */ public double cumulativeProbability(final int x) { if (x <= 0) { return 0.0; } else if (x >= numberOfElements) { return 1.0; } return generalizedHarmonic(x, exponent) / generalizedHarmonic(numberOfElements, exponent); } /** * {@inheritDoc} * * For number of elements {@code N} and exponent {@code s}, the mean is * {@code Hs1 / Hs}, where *
                                *
                              • {@code Hs1 = generalizedHarmonic(N, s - 1)},
                              • *
                              • {@code Hs = generalizedHarmonic(N, s)}.
                              • *
                              */ public double getNumericalMean() { if (!numericalMeanIsCalculated) { numericalMean = calculateNumericalMean(); numericalMeanIsCalculated = true; } return numericalMean; } /** * Used by {@link #getNumericalMean()}. * * @return the mean of this distribution */ protected double calculateNumericalMean() { final int N = getNumberOfElements(); final double s = getExponent(); final double Hs1 = generalizedHarmonic(N, s - 1); final double Hs = generalizedHarmonic(N, s); return Hs1 / Hs; } /** * {@inheritDoc} * * For number of elements {@code N} and exponent {@code s}, the mean is * {@code (Hs2 / Hs) - (Hs1^2 / Hs^2)}, where *
                                *
                              • {@code Hs2 = generalizedHarmonic(N, s - 2)},
                              • *
                              • {@code Hs1 = generalizedHarmonic(N, s - 1)},
                              • *
                              • {@code Hs = generalizedHarmonic(N, s)}.
                              • *
                              */ public double getNumericalVariance() { if (!numericalVarianceIsCalculated) { numericalVariance = calculateNumericalVariance(); numericalVarianceIsCalculated = true; } return numericalVariance; } /** * Used by {@link #getNumericalVariance()}. * * @return the variance of this distribution */ protected double calculateNumericalVariance() { final int N = getNumberOfElements(); final double s = getExponent(); final double Hs2 = generalizedHarmonic(N, s - 2); final double Hs1 = generalizedHarmonic(N, s - 1); final double Hs = generalizedHarmonic(N, s); return (Hs2 / Hs) - ((Hs1 * Hs1) / (Hs * Hs)); } /** * Calculates the Nth generalized harmonic number. See * Harmonic * Series. * * @param n Term in the series to calculate (must be larger than 1) * @param m Exponent (special case {@code m = 1} is the harmonic series). * @return the nth generalized harmonic number. */ private double generalizedHarmonic(final int n, final double m) { double value = 0; for (int k = n; k > 0; --k) { value += 1.0 / FastMath.pow(k, m); } return value; } /** * {@inheritDoc} * * The lower bound of the support is always 1 no matter the parameters. * * @return lower bound of the support (always 1) */ public int getSupportLowerBound() { return 1; } /** * {@inheritDoc} * * The upper bound of the support is the number of elements. * * @return upper bound of the support */ public int getSupportUpperBound() { return getNumberOfElements(); } /** * {@inheritDoc} * * The support of this distribution is connected. * * @return {@code true} */ public boolean isSupportConnected() { return true; } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/distribution/NormalDistribution.java100644 1750 1750 20700 12126627700 31750 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.distribution; import org.apache.commons.math3.exception.NotStrictlyPositiveException; import org.apache.commons.math3.exception.NumberIsTooLargeException; import org.apache.commons.math3.exception.OutOfRangeException; import org.apache.commons.math3.exception.util.LocalizedFormats; import org.apache.commons.math3.special.Erf; import org.apache.commons.math3.util.FastMath; import org.apache.commons.math3.random.RandomGenerator; import org.apache.commons.math3.random.Well19937c; /** * Implementation of the normal (gaussian) distribution. * * @see Normal distribution (Wikipedia) * @see Normal distribution (MathWorld) * @version $Id: NormalDistribution.java 1462423 2013-03-29 07:25:18Z luc $ */ public class NormalDistribution extends AbstractRealDistribution { /** * Default inverse cumulative probability accuracy. * @since 2.1 */ public static final double DEFAULT_INVERSE_ABSOLUTE_ACCURACY = 1e-9; /** Serializable version identifier. */ private static final long serialVersionUID = 8589540077390120676L; /** √(2 π) */ private static final double SQRT2PI = FastMath.sqrt(2 * FastMath.PI); /** √(2) */ private static final double SQRT2 = FastMath.sqrt(2.0); /** Mean of this distribution. */ private final double mean; /** Standard deviation of this distribution. */ private final double standardDeviation; /** Inverse cumulative probability accuracy. */ private final double solverAbsoluteAccuracy; /** * Create a normal distribution with mean equal to zero and standard * deviation equal to one. */ public NormalDistribution() { this(0, 1); } /** * Create a normal distribution using the given mean and standard deviation. * * @param mean Mean for this distribution. * @param sd Standard deviation for this distribution. * @throws NotStrictlyPositiveException if {@code sd <= 0}. */ public NormalDistribution(double mean, double sd) throws NotStrictlyPositiveException { this(mean, sd, DEFAULT_INVERSE_ABSOLUTE_ACCURACY); } /** * Create a normal distribution using the given mean, standard deviation and * inverse cumulative distribution accuracy. * * @param mean Mean for this distribution. * @param sd Standard deviation for this distribution. * @param inverseCumAccuracy Inverse cumulative probability accuracy. * @throws NotStrictlyPositiveException if {@code sd <= 0}. * @since 2.1 */ public NormalDistribution(double mean, double sd, double inverseCumAccuracy) throws NotStrictlyPositiveException { this(new Well19937c(), mean, sd, inverseCumAccuracy); } /** * Creates a normal distribution. * * @param rng Random number generator. * @param mean Mean for this distribution. * @param sd Standard deviation for this distribution. * @param inverseCumAccuracy Inverse cumulative probability accuracy. * @throws NotStrictlyPositiveException if {@code sd <= 0}. * @since 3.1 */ public NormalDistribution(RandomGenerator rng, double mean, double sd, double inverseCumAccuracy) throws NotStrictlyPositiveException { super(rng); if (sd <= 0) { throw new NotStrictlyPositiveException(LocalizedFormats.STANDARD_DEVIATION, sd); } this.mean = mean; standardDeviation = sd; solverAbsoluteAccuracy = inverseCumAccuracy; } /** * Access the mean. * * @return the mean for this distribution. */ public double getMean() { return mean; } /** * Access the standard deviation. * * @return the standard deviation for this distribution. */ public double getStandardDeviation() { return standardDeviation; } /** {@inheritDoc} */ public double density(double x) { final double x0 = x - mean; final double x1 = x0 / standardDeviation; return FastMath.exp(-0.5 * x1 * x1) / (standardDeviation * SQRT2PI); } /** * {@inheritDoc} * * If {@code x} is more than 40 standard deviations from the mean, 0 or 1 * is returned, as in these cases the actual value is within * {@code Double.MIN_VALUE} of 0 or 1. */ public double cumulativeProbability(double x) { final double dev = x - mean; if (FastMath.abs(dev) > 40 * standardDeviation) { return dev < 0 ? 0.0d : 1.0d; } return 0.5 * (1 + Erf.erf(dev / (standardDeviation * SQRT2))); } /** {@inheritDoc} * @since 3.2 */ @Override public double inverseCumulativeProbability(final double p) throws OutOfRangeException { if (p < 0.0 || p > 1.0) { throw new OutOfRangeException(p, 0, 1); } return mean + standardDeviation * SQRT2 * Erf.erfInv(2 * p - 1); } /** * {@inheritDoc} * * @deprecated See {@link RealDistribution#cumulativeProbability(double,double)} */ @Override@Deprecated public double cumulativeProbability(double x0, double x1) throws NumberIsTooLargeException { return probability(x0, x1); } /** {@inheritDoc} */ @Override public double probability(double x0, double x1) throws NumberIsTooLargeException { if (x0 > x1) { throw new NumberIsTooLargeException(LocalizedFormats.LOWER_ENDPOINT_ABOVE_UPPER_ENDPOINT, x0, x1, true); } final double denom = standardDeviation * SQRT2; final double v0 = (x0 - mean) / denom; final double v1 = (x1 - mean) / denom; return 0.5 * Erf.erf(v0, v1); } /** {@inheritDoc} */ @Override protected double getSolverAbsoluteAccuracy() { return solverAbsoluteAccuracy; } /** * {@inheritDoc} * * For mean parameter {@code mu}, the mean is {@code mu}. */ public double getNumericalMean() { return getMean(); } /** * {@inheritDoc} * * For standard deviation parameter {@code s}, the variance is {@code s^2}. */ public double getNumericalVariance() { final double s = getStandardDeviation(); return s * s; } /** * {@inheritDoc} * * The lower bound of the support is always negative infinity * no matter the parameters. * * @return lower bound of the support (always * {@code Double.NEGATIVE_INFINITY}) */ public double getSupportLowerBound() { return Double.NEGATIVE_INFINITY; } /** * {@inheritDoc} * * The upper bound of the support is always positive infinity * no matter the parameters. * * @return upper bound of the support (always * {@code Double.POSITIVE_INFINITY}) */ public double getSupportUpperBound() { return Double.POSITIVE_INFINITY; } /** {@inheritDoc} */ public boolean isSupportLowerBoundInclusive() { return false; } /** {@inheritDoc} */ public boolean isSupportUpperBoundInclusive() { return false; } /** * {@inheritDoc} * * The support of this distribution is connected. * * @return {@code true} */ public boolean isSupportConnected() { return true; } /** {@inheritDoc} */ @Override public double sample() { return standardDeviation * random.nextGaussian() + mean; } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/exception/package-info.java100644 1750 1750 1727 12126627714 27720 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ /** * * Specialized exceptions for algorithms errors. The exceptions can be localized * using simple java properties. * */ package org.apache.commons.math3.exception; ././@LongLink100644 0 0 146 12126630647 10263 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/exception/MaxCountExceededException.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/exception/MaxCountExceededException.jav100644 1750 1750 4206 12126627714 32272 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.exception; import org.apache.commons.math3.exception.util.Localizable; import org.apache.commons.math3.exception.util.LocalizedFormats; /** * Exception to be thrown when some counter maximum value is exceeded. * * @since 3.0 * @version $Id: MaxCountExceededException.java 1364378 2012-07-22 17:42:38Z tn $ */ public class MaxCountExceededException extends MathIllegalStateException { /** Serializable version Id. */ private static final long serialVersionUID = 4330003017885151975L; /** * Maximum number of evaluations. */ private final Number max; /** * Construct the exception. * * @param max Maximum. */ public MaxCountExceededException(Number max) { this(LocalizedFormats.MAX_COUNT_EXCEEDED, max); } /** * Construct the exception with a specific context. * * @param specific Specific context pattern. * @param max Maximum. * @param args Additional arguments. */ public MaxCountExceededException(Localizable specific, Number max, Object ... args) { getContext().addMessage(specific, max, args); this.max = max; } /** * @return the maximum number of evaluations. */ public Number getMax() { return max; } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/exception/MathRuntimeException.java100644 1750 1750 4527 12126627714 31511 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.exception; import org.apache.commons.math3.exception.util.Localizable; import org.apache.commons.math3.exception.util.ExceptionContext; import org.apache.commons.math3.exception.util.ExceptionContextProvider; /** * As of release 4.0, all exceptions thrown by the Commons Math code (except * {@link NullArgumentException}) inherit from this class. * In most cases, this class should not be instantiated directly: it should * serve as a base class for implementing exception classes that describe a * specific "problem". * * @since 3.1 * @version $Id: MathRuntimeException.java 1416643 2012-12-03 19:37:14Z tn $ */ public class MathRuntimeException extends RuntimeException implements ExceptionContextProvider { /** Serializable version Id. */ private static final long serialVersionUID = 20120926L; /** Context. */ private final ExceptionContext context; /** * @param pattern Message pattern explaining the cause of the error. * @param args Arguments. */ public MathRuntimeException(Localizable pattern, Object ... args) { context = new ExceptionContext(this); context.addMessage(pattern, args); } /** {@inheritDoc} */ public ExceptionContext getContext() { return context; } /** {@inheritDoc} */ @Override public String getMessage() { return context.getMessage(); } /** {@inheritDoc} */ @Override public String getLocalizedMessage() { return context.getLocalizedMessage(); } } ././@LongLink100644 0 0 145 12126630647 10262 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/exception/NotFiniteNumberException.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/exception/NotFiniteNumberException.java100644 1750 1750 4022 12126627715 32313 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.exception; import org.apache.commons.math3.exception.util.Localizable; import org.apache.commons.math3.exception.util.LocalizedFormats; /** * Exception to be thrown when a number is not finite. * * @since 3.0 * @version $Id: NotFiniteNumberException.java 1364378 2012-07-22 17:42:38Z tn $ */ public class NotFiniteNumberException extends MathIllegalNumberException { /** Serializable version Id. */ private static final long serialVersionUID = -6100997100383932834L; /** * Construct the exception. * * @param wrong Value that is infinite or NaN. * @param args Optional arguments. */ public NotFiniteNumberException(Number wrong, Object ... args) { this(LocalizedFormats.NOT_FINITE_NUMBER, wrong, args); } /** * Construct the exception with a specific context. * * @param specific Specific context pattern. * @param wrong Value that is infinite or NaN. * @param args Optional arguments. */ public NotFiniteNumberException(Localizable specific, Number wrong, Object ... args) { super(specific, wrong, args); } } ././@LongLink100644 0 0 150 12126630647 10256 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/exception/TooManyEvaluationsException.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/exception/TooManyEvaluationsException.j100644 1750 1750 2776 12126627714 32371 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.exception; import org.apache.commons.math3.exception.util.LocalizedFormats; /** * Exception to be thrown when the maximal number of evaluations is exceeded. * * @since 3.0 * @version $Id: TooManyEvaluationsException.java 1364378 2012-07-22 17:42:38Z tn $ */ public class TooManyEvaluationsException extends MaxCountExceededException { /** Serializable version Id. */ private static final long serialVersionUID = 4330003017885151975L; /** * Construct the exception. * * @param max Maximum number of evaluations. */ public TooManyEvaluationsException(Number max) { super(max); getContext().addMessage(LocalizedFormats.EVALUATIONS); } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/exception/MathParseException.java100644 1750 1750 4411 12126627714 31130 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.exception; import org.apache.commons.math3.exception.util.ExceptionContextProvider; import org.apache.commons.math3.exception.util.LocalizedFormats; /** * Class to signal parse failures. * * @since 2.2 * @version $Id: MathParseException.java 1364378 2012-07-22 17:42:38Z tn $ */ public class MathParseException extends MathIllegalStateException implements ExceptionContextProvider { /** Serializable version Id. */ private static final long serialVersionUID = -6024911025449780478L; /** * @param wrong Bad string representation of the object. * @param position Index, in the {@code wrong} string, that caused the * parsing to fail. * @param type Class of the object supposedly represented by the * {@code wrong} string. */ public MathParseException(String wrong, int position, Class type) { getContext().addMessage(LocalizedFormats.CANNOT_PARSE_AS_TYPE, wrong, Integer.valueOf(position), type.getName()); } /** * @param wrong Bad string representation of the object. * @param position Index, in the {@code wrong} string, that caused the * parsing to fail. */ public MathParseException(String wrong, int position) { getContext().addMessage(LocalizedFormats.CANNOT_PARSE, wrong, Integer.valueOf(position)); } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/exception/NullArgumentException.java100644 1750 1750 4056 12126627715 31667 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.exception; import org.apache.commons.math3.exception.util.Localizable; import org.apache.commons.math3.exception.util.LocalizedFormats; /** * All conditions checks that fail due to a {@code null} argument must throw * this exception. * This class is meant to signal a precondition violation ("null is an illegal * argument") and so does not extend the standard {@code NullPointerException}. * Propagation of {@code NullPointerException} from within Commons-Math is * construed to be a bug. * * @since 2.2 * @version $Id: NullArgumentException.java 1364378 2012-07-22 17:42:38Z tn $ */ public class NullArgumentException extends MathIllegalArgumentException { /** Serializable version Id. */ private static final long serialVersionUID = -6024911025449780478L; /** * Default constructor. */ public NullArgumentException() { this(LocalizedFormats.NULL_NOT_ALLOWED); } /** * @param pattern Message pattern providing the specific context of * the error. * @param arguments Values for replacing the placeholders in {@code pattern}. */ public NullArgumentException(Localizable pattern, Object ... arguments) { super(pattern, arguments); } } ././@LongLink100644 0 0 146 12126630647 10263 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/exception/NumberIsTooLargeException.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/exception/NumberIsTooLargeException.jav100644 1750 1750 5655 12126627714 32277 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.exception; import org.apache.commons.math3.exception.util.Localizable; import org.apache.commons.math3.exception.util.LocalizedFormats; /** * Exception to be thrown when a number is too large. * * @since 2.2 * @version $Id: NumberIsTooLargeException.java 1364378 2012-07-22 17:42:38Z tn $ */ public class NumberIsTooLargeException extends MathIllegalNumberException { /** Serializable version Id. */ private static final long serialVersionUID = 4330003017885151975L; /** * Higher bound. */ private final Number max; /** * Whether the maximum is included in the allowed range. */ private final boolean boundIsAllowed; /** * Construct the exception. * * @param wrong Value that is larger than the maximum. * @param max Maximum. * @param boundIsAllowed if true the maximum is included in the allowed range. */ public NumberIsTooLargeException(Number wrong, Number max, boolean boundIsAllowed) { this(boundIsAllowed ? LocalizedFormats.NUMBER_TOO_LARGE : LocalizedFormats.NUMBER_TOO_LARGE_BOUND_EXCLUDED, wrong, max, boundIsAllowed); } /** * Construct the exception with a specific context. * * @param specific Specific context pattern. * @param wrong Value that is larger than the maximum. * @param max Maximum. * @param boundIsAllowed if true the maximum is included in the allowed range. */ public NumberIsTooLargeException(Localizable specific, Number wrong, Number max, boolean boundIsAllowed) { super(specific, wrong, max); this.max = max; this.boundIsAllowed = boundIsAllowed; } /** * @return {@code true} if the maximum is included in the allowed range. */ public boolean getBoundIsAllowed() { return boundIsAllowed; } /** * @return the maximum. */ public Number getMax() { return max; } } ././@LongLink100644 0 0 151 12126630647 10257 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/exception/NotStrictlyPositiveException.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/exception/NotStrictlyPositiveException.100644 1750 1750 3372 12126627714 32430 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.exception; import org.apache.commons.math3.exception.util.Localizable; /** * Exception to be thrown when the argument is negative. * * @since 2.2 * @version $Id: NotStrictlyPositiveException.java 1364378 2012-07-22 17:42:38Z tn $ */ public class NotStrictlyPositiveException extends NumberIsTooSmallException { /** Serializable version Id. */ private static final long serialVersionUID = -7824848630829852237L; /** * Construct the exception. * * @param value Argument. */ public NotStrictlyPositiveException(Number value) { super(value, 0, false); } /** * Construct the exception with a specific context. * * @param specific Specific context where the error occurred. * @param value Argument. */ public NotStrictlyPositiveException(Localizable specific, Number value) { super(specific, value, 0, false); } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/exception/OutOfRangeException.java100644 1750 1750 4777 12126627715 31275 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.exception; import org.apache.commons.math3.exception.util.LocalizedFormats; import org.apache.commons.math3.exception.util.Localizable; /** * Exception to be thrown when some argument is out of range. * * @since 2.2 * @version $Id: OutOfRangeException.java 1364378 2012-07-22 17:42:38Z tn $ */ public class OutOfRangeException extends MathIllegalNumberException { /** Serializable version Id. */ private static final long serialVersionUID = 111601815794403609L; /** Lower bound. */ private final Number lo; /** Higher bound. */ private final Number hi; /** * Construct an exception from the mismatched dimensions. * * @param wrong Requested value. * @param lo Lower bound. * @param hi Higher bound. */ public OutOfRangeException(Number wrong, Number lo, Number hi) { this(LocalizedFormats.OUT_OF_RANGE_SIMPLE, wrong, lo, hi); } /** * Construct an exception from the mismatched dimensions with a * specific context information. * * @param specific Context information. * @param wrong Requested value. * @param lo Lower bound. * @param hi Higher bound. */ public OutOfRangeException(Localizable specific, Number wrong, Number lo, Number hi) { super(specific, wrong, lo, hi); this.lo = lo; this.hi = hi; } /** * @return the lower bound. */ public Number getLo() { return lo; } /** * @return the higher bound. */ public Number getHi() { return hi; } } ././@LongLink100644 0 0 146 12126630647 10263 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/exception/NumberIsTooSmallException.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/exception/NumberIsTooSmallException.jav100644 1750 1750 5661 12126627714 32312 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.exception; import org.apache.commons.math3.exception.util.Localizable; import org.apache.commons.math3.exception.util.LocalizedFormats; /** * Exception to be thrown when a number is too small. * * @since 2.2 * @version $Id: NumberIsTooSmallException.java 1364378 2012-07-22 17:42:38Z tn $ */ public class NumberIsTooSmallException extends MathIllegalNumberException { /** Serializable version Id. */ private static final long serialVersionUID = -6100997100383932834L; /** * Higher bound. */ private final Number min; /** * Whether the maximum is included in the allowed range. */ private final boolean boundIsAllowed; /** * Construct the exception. * * @param wrong Value that is smaller than the minimum. * @param min Minimum. * @param boundIsAllowed Whether {@code min} is included in the allowed range. */ public NumberIsTooSmallException(Number wrong, Number min, boolean boundIsAllowed) { this(boundIsAllowed ? LocalizedFormats.NUMBER_TOO_SMALL : LocalizedFormats.NUMBER_TOO_SMALL_BOUND_EXCLUDED, wrong, min, boundIsAllowed); } /** * Construct the exception with a specific context. * * @param specific Specific context pattern. * @param wrong Value that is smaller than the minimum. * @param min Minimum. * @param boundIsAllowed Whether {@code min} is included in the allowed range. */ public NumberIsTooSmallException(Localizable specific, Number wrong, Number min, boolean boundIsAllowed) { super(specific, wrong, min); this.min = min; this.boundIsAllowed = boundIsAllowed; } /** * @return {@code true} if the minimum is included in the allowed range. */ public boolean getBoundIsAllowed() { return boundIsAllowed; } /** * @return the minimum. */ public Number getMin() { return min; } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/exception/NotANumberException.java100644 1750 1750 2565 12126627715 31267 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.exception; import org.apache.commons.math3.exception.util.LocalizedFormats; /** * Exception to be thrown when a number is not a number. * * @since 3.1 * @version $Id: NotANumberException.java 1422195 2012-12-15 06:45:18Z psteitz $ */ public class NotANumberException extends MathIllegalNumberException { /** Serializable version Id. */ private static final long serialVersionUID = 20120906L; /** * Construct the exception. */ public NotANumberException() { super(LocalizedFormats.NAN_NOT_ALLOWED, Double.NaN); } } ././@LongLink100644 0 0 146 12126630647 10263 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/exception/MathIllegalStateException.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/exception/MathIllegalStateException.jav100644 1750 1750 5573 12126627714 32301 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.exception; import org.apache.commons.math3.exception.util.Localizable; import org.apache.commons.math3.exception.util.LocalizedFormats; import org.apache.commons.math3.exception.util.ExceptionContext; import org.apache.commons.math3.exception.util.ExceptionContextProvider; /** * Base class for all exceptions that signal a mismatch between the * current state and the user's expectations. * * @since 2.2 * @version $Id: MathIllegalStateException.java 1364378 2012-07-22 17:42:38Z tn $ */ public class MathIllegalStateException extends IllegalStateException implements ExceptionContextProvider { /** Serializable version Id. */ private static final long serialVersionUID = -6024911025449780478L; /** Context. */ private final ExceptionContext context; /** * Simple constructor. * * @param pattern Message pattern explaining the cause of the error. * @param args Arguments. */ public MathIllegalStateException(Localizable pattern, Object ... args) { context = new ExceptionContext(this); context.addMessage(pattern, args); } /** * Simple constructor. * * @param cause Root cause. * @param pattern Message pattern explaining the cause of the error. * @param args Arguments. */ public MathIllegalStateException(Throwable cause, Localizable pattern, Object ... args) { super(cause); context = new ExceptionContext(this); context.addMessage(pattern, args); } /** * Default constructor. */ public MathIllegalStateException() { this(LocalizedFormats.ILLEGAL_STATE); } /** {@inheritDoc} */ public ExceptionContext getContext() { return context; } /** {@inheritDoc} */ @Override public String getMessage() { return context.getMessage(); } /** {@inheritDoc} */ @Override public String getLocalizedMessage() { return context.getLocalizedMessage(); } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/exception/NoBracketingException.java100644 1750 1750 6666 12126627715 31631 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.exception; import org.apache.commons.math3.exception.util.Localizable; import org.apache.commons.math3.exception.util.LocalizedFormats; /** * Exception to be thrown when function values have the same sign at both * ends of an interval. * * @since 3.0 * @version $Id: NoBracketingException.java 1364378 2012-07-22 17:42:38Z tn $ */ public class NoBracketingException extends MathIllegalArgumentException { /** Serializable version Id. */ private static final long serialVersionUID = -3629324471511904459L; /** Lower end of the interval. */ private final double lo; /** Higher end of the interval. */ private final double hi; /** Value at lower end of the interval. */ private final double fLo; /** Value at higher end of the interval. */ private final double fHi; /** * Construct the exception. * * @param lo Lower end of the interval. * @param hi Higher end of the interval. * @param fLo Value at lower end of the interval. * @param fHi Value at higher end of the interval. */ public NoBracketingException(double lo, double hi, double fLo, double fHi) { this(LocalizedFormats.SAME_SIGN_AT_ENDPOINTS, lo, hi, fLo, fHi); } /** * Construct the exception with a specific context. * * @param specific Contextual information on what caused the exception. * @param lo Lower end of the interval. * @param hi Higher end of the interval. * @param fLo Value at lower end of the interval. * @param fHi Value at higher end of the interval. * @param args Additional arguments. */ public NoBracketingException(Localizable specific, double lo, double hi, double fLo, double fHi, Object ... args) { super(specific, lo, hi, fLo, fHi, args); this.lo = lo; this.hi = hi; this.fLo = fLo; this.fHi = fHi; } /** * Get the lower end of the interval. * * @return the lower end. */ public double getLo() { return lo; } /** * Get the higher end of the interval. * * @return the higher end. */ public double getHi() { return hi; } /** * Get the value at the lower end of the interval. * * @return the value at the lower end. */ public double getFLo() { return fLo; } /** * Get the value at the higher end of the interval. * * @return the value at the higher end. */ public double getFHi() { return fHi; } } ././@LongLink100644 0 0 154 12126630647 10262 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/exception/MultiDimensionMismatchException.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/exception/MultiDimensionMismatchExcepti100644 1750 1750 6076 12126627714 32426 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.exception; import org.apache.commons.math3.exception.util.Localizable; import org.apache.commons.math3.exception.util.LocalizedFormats; /** * Exception to be thrown when two sets of dimensions differ. * * @since 3.0 * @version $Id: MultiDimensionMismatchException.java 1364378 2012-07-22 17:42:38Z tn $ */ public class MultiDimensionMismatchException extends MathIllegalArgumentException { /** Serializable version Id. */ private static final long serialVersionUID = -8415396756375798143L; /** Wrong dimensions. */ private final Integer[] wrong; /** Correct dimensions. */ private final Integer[] expected; /** * Construct an exception from the mismatched dimensions. * * @param wrong Wrong dimensions. * @param expected Expected dimensions. */ public MultiDimensionMismatchException(Integer[] wrong, Integer[] expected) { this(LocalizedFormats.DIMENSIONS_MISMATCH, wrong, expected); } /** * Construct an exception from the mismatched dimensions. * * @param specific Message pattern providing the specific context of * the error. * @param wrong Wrong dimensions. * @param expected Expected dimensions. */ public MultiDimensionMismatchException(Localizable specific, Integer[] wrong, Integer[] expected) { super(specific, wrong, expected); this.wrong = wrong.clone(); this.expected = expected.clone(); } /** * @return an array containing the wrong dimensions. */ public Integer[] getWrongDimensions() { return wrong.clone(); } /** * @return an array containing the expected dimensions. */ public Integer[] getExpectedDimensions() { return expected.clone(); } /** * @param index Dimension index. * @return the wrong dimension stored at {@code index}. */ public int getWrongDimension(int index) { return wrong[index]; } /** * @param index Dimension index. * @return the expected dimension stored at {@code index}. */ public int getExpectedDimension(int index) { return expected[index]; } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/exception/MathInternalError.java100644 1750 1750 4062 12126627714 30767 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.exception; import org.apache.commons.math3.exception.util.Localizable; import org.apache.commons.math3.exception.util.LocalizedFormats; /** * Exception triggered when something that shouldn't happen does happen. * * @since 2.2 * @version $Id: MathInternalError.java 1364378 2012-07-22 17:42:38Z tn $ */ public class MathInternalError extends MathIllegalStateException { /** Serializable version Id. */ private static final long serialVersionUID = -6276776513966934846L; /** URL for reporting problems. */ private static final String REPORT_URL = "https://issues.apache.org/jira/browse/MATH"; /** * Simple constructor. */ public MathInternalError() { getContext().addMessage(LocalizedFormats.INTERNAL_ERROR, REPORT_URL); } /** * Simple constructor. * @param cause root cause */ public MathInternalError(final Throwable cause) { super(cause, LocalizedFormats.INTERNAL_ERROR, REPORT_URL); } /** * Constructor accepting a localized message. * * @param pattern Message pattern explaining the cause of the error. * @param args Arguments. */ public MathInternalError(Localizable pattern, Object ... args) { super(pattern, args); } } ././@LongLink100644 0 0 147 12126630647 10264 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/exception/MathIllegalNumberException.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/exception/MathIllegalNumberException.ja100644 1750 1750 4015 12126627714 32251 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.exception; import org.apache.commons.math3.exception.util.Localizable; /** * Base class for exceptions raised by a wrong number. * This class is not intended to be instantiated directly: it should serve * as a base class to create all the exceptions that are raised because some * precondition is violated by a number argument. * * @since 2.2 * @version $Id: MathIllegalNumberException.java 1364378 2012-07-22 17:42:38Z tn $ */ public class MathIllegalNumberException extends MathIllegalArgumentException { /** Serializable version Id. */ private static final long serialVersionUID = -7447085893598031110L; /** Requested. */ private final Number argument; /** * Construct an exception. * * @param pattern Localizable pattern. * @param wrong Wrong number. * @param arguments Arguments. */ protected MathIllegalNumberException(Localizable pattern, Number wrong, Object ... arguments) { super(pattern, wrong, arguments); argument = wrong; } /** * @return the requested value. */ public Number getArgument() { return argument; } } ././@LongLink100644 0 0 147 12126630647 10264 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/exception/DimensionMismatchException.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/exception/DimensionMismatchException.ja100644 1750 1750 4425 12126627715 32336 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.exception; import org.apache.commons.math3.exception.util.LocalizedFormats; import org.apache.commons.math3.exception.util.Localizable; /** * Exception to be thrown when two dimensions differ. * * @since 2.2 * @version $Id: DimensionMismatchException.java 1364378 2012-07-22 17:42:38Z tn $ */ public class DimensionMismatchException extends MathIllegalNumberException { /** Serializable version Id. */ private static final long serialVersionUID = -8415396756375798143L; /** Correct dimension. */ private final int dimension; /** * Construct an exception from the mismatched dimensions. * * @param specific Specific context information pattern. * @param wrong Wrong dimension. * @param expected Expected dimension. */ public DimensionMismatchException(Localizable specific, int wrong, int expected) { super(specific, wrong, expected); dimension = expected; } /** * Construct an exception from the mismatched dimensions. * * @param wrong Wrong dimension. * @param expected Expected dimension. */ public DimensionMismatchException(int wrong, int expected) { this(LocalizedFormats.DIMENSIONS_MISMATCH_SIMPLE, wrong, expected); } /** * @return the expected dimension. */ public int getDimension() { return dimension; } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/exception/NoDataException.java100644 1750 1750 3212 12126627714 30410 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.exception; import org.apache.commons.math3.exception.util.Localizable; import org.apache.commons.math3.exception.util.LocalizedFormats; /** * Exception to be thrown when the required data is missing. * * @since 2.2 * @version $Id: NoDataException.java 1364378 2012-07-22 17:42:38Z tn $ */ public class NoDataException extends MathIllegalArgumentException { /** Serializable version Id. */ private static final long serialVersionUID = -3629324471511904459L; /** * Construct the exception. */ public NoDataException() { this(LocalizedFormats.NO_DATA); } /** * Construct the exception with a specific context. * * @param specific Contextual information on what caused the exception. */ public NoDataException(Localizable specific) { super(specific); } } ././@LongLink100644 0 0 147 12126630647 10264 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/exception/TooManyIterationsException.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/exception/TooManyIterationsException.ja100644 1750 1750 2654 12126627714 32354 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.exception; import org.apache.commons.math3.exception.util.LocalizedFormats; /** * Exception to be thrown when the maximal number of iterations is exceeded. * * @since 3.1 * @version $Id$ */ public class TooManyIterationsException extends MaxCountExceededException { /** Serializable version Id. */ private static final long serialVersionUID = 20121211L; /** * Construct the exception. * * @param max Maximum number of evaluations. */ public TooManyIterationsException(Number max) { super(max); getContext().addMessage(LocalizedFormats.ITERATIONS); } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/exception/NotPositiveException.java100644 1750 1750 3317 12126627714 31533 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.exception; import org.apache.commons.math3.exception.util.Localizable; /** * Exception to be thrown when the argument is negative. * * @since 2.2 * @version $Id: NotPositiveException.java 1364378 2012-07-22 17:42:38Z tn $ */ public class NotPositiveException extends NumberIsTooSmallException { /** Serializable version Id. */ private static final long serialVersionUID = -2250556892093726375L; /** * Construct the exception. * * @param value Argument. */ public NotPositiveException(Number value) { super(value, 0, true); } /** * Construct the exception with a specific context. * * @param specific Specific context where the error occurred. * @param value Argument. */ public NotPositiveException(Localizable specific, Number value) { super(specific, value, 0, true); } } ././@LongLink100644 0 0 152 12126630647 10260 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/exception/NonMonotonicSequenceException.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/exception/NonMonotonicSequenceException100644 1750 1750 10402 12126627715 32453 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.exception; import org.apache.commons.math3.util.MathArrays; import org.apache.commons.math3.exception.util.LocalizedFormats; /** * Exception to be thrown when the a sequence of values is not monotonically * increasing or decreasing. * * @since 2.2 (name changed to "NonMonotonicSequenceException" in 3.0) * @version $Id: NonMonotonicSequenceException.java 1364378 2012-07-22 17:42:38Z tn $ */ public class NonMonotonicSequenceException extends MathIllegalNumberException { /** Serializable version Id. */ private static final long serialVersionUID = 3596849179428944575L; /** * Direction (positive for increasing, negative for decreasing). */ private final MathArrays.OrderDirection direction; /** * Whether the sequence must be strictly increasing or decreasing. */ private final boolean strict; /** * Index of the wrong value. */ private final int index; /** * Previous value. */ private final Number previous; /** * Construct the exception. * This constructor uses default values assuming that the sequence should * have been strictly increasing. * * @param wrong Value that did not match the requirements. * @param previous Previous value in the sequence. * @param index Index of the value that did not match the requirements. */ public NonMonotonicSequenceException(Number wrong, Number previous, int index) { this(wrong, previous, index, MathArrays.OrderDirection.INCREASING, true); } /** * Construct the exception. * * @param wrong Value that did not match the requirements. * @param previous Previous value in the sequence. * @param index Index of the value that did not match the requirements. * @param direction Strictly positive for a sequence required to be * increasing, negative (or zero) for a decreasing sequence. * @param strict Whether the sequence must be strictly increasing or * decreasing. */ public NonMonotonicSequenceException(Number wrong, Number previous, int index, MathArrays.OrderDirection direction, boolean strict) { super(direction == MathArrays.OrderDirection.INCREASING ? (strict ? LocalizedFormats.NOT_STRICTLY_INCREASING_SEQUENCE : LocalizedFormats.NOT_INCREASING_SEQUENCE) : (strict ? LocalizedFormats.NOT_STRICTLY_DECREASING_SEQUENCE : LocalizedFormats.NOT_DECREASING_SEQUENCE), wrong, previous, index, index - 1); this.direction = direction; this.strict = strict; this.index = index; this.previous = previous; } /** * @return the order direction. **/ public MathArrays.OrderDirection getDirection() { return direction; } /** * @return {@code true} is the sequence should be strictly monotonic. **/ public boolean getStrict() { return strict; } /** * Get the index of the wrong value. * * @return the current index. */ public int getIndex() { return index; } /** * @return the previous value. */ public Number getPrevious() { return previous; } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/exception/ZeroException.java100644 1750 1750 3311 12126627715 30162 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.exception; import org.apache.commons.math3.exception.util.Localizable; import org.apache.commons.math3.exception.util.LocalizedFormats; /** * Exception to be thrown when zero is provided where it is not allowed. * * @since 2.2 * @version $Id: ZeroException.java 1364378 2012-07-22 17:42:38Z tn $ */ public class ZeroException extends MathIllegalNumberException { /** Serializable version identifier */ private static final long serialVersionUID = -1960874856936000015L; /** * Construct the exception. */ public ZeroException() { this(LocalizedFormats.ZERO_NOT_ALLOWED); } /** * Construct the exception with a specific context. * * @param specific Specific context pattern. * @param arguments Arguments. */ public ZeroException(Localizable specific, Object ... arguments) { super(specific, 0, arguments); } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/exception/MathArithmeticException.java100644 1750 1750 5117 12126627714 32153 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.exception; import org.apache.commons.math3.exception.util.Localizable; import org.apache.commons.math3.exception.util.LocalizedFormats; import org.apache.commons.math3.exception.util.ExceptionContext; import org.apache.commons.math3.exception.util.ExceptionContextProvider; /** * Base class for arithmetic exceptions. * It is used for all the exceptions that have the semantics of the standard * {@link ArithmeticException}, but must also provide a localized * message. * * @since 3.0 * @version $Id: MathArithmeticException.java 1364378 2012-07-22 17:42:38Z tn $ */ public class MathArithmeticException extends ArithmeticException implements ExceptionContextProvider { /** Serializable version Id. */ private static final long serialVersionUID = -6024911025449780478L; /** Context. */ private final ExceptionContext context; /** * Default constructor. */ public MathArithmeticException() { context = new ExceptionContext(this); context.addMessage(LocalizedFormats.ARITHMETIC_EXCEPTION); } /** * Constructor with a specific message. * * @param pattern Message pattern providing the specific context of * the error. * @param args Arguments. */ public MathArithmeticException(Localizable pattern, Object ... args) { context = new ExceptionContext(this); context.addMessage(pattern, args); } /** {@inheritDoc} */ public ExceptionContext getContext() { return context; } /** {@inheritDoc} */ @Override public String getMessage() { return context.getMessage(); } /** {@inheritDoc} */ @Override public String getLocalizedMessage() { return context.getLocalizedMessage(); } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/exception/util/package-info.java100644 1750 1750 1623 12126627714 30670 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ /** * * Classes supporting exception localization. * */ package org.apache.commons.math3.exception.util; commons-math3-3.2-src/src/main/java/org/apache/commons/math3/exception/util/DummyLocalizable.java100644 1750 1750 3332 12126627714 31600 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.exception.util; import java.util.Locale; /** * Dummy implementation of the {@link Localizable} interface, without localization. * * @version $Id: DummyLocalizable.java 1416643 2012-12-03 19:37:14Z tn $ * @since 2.2 */ public class DummyLocalizable implements Localizable { /** Serializable version identifier. */ private static final long serialVersionUID = 8843275624471387299L; /** Source string. */ private final String source; /** Simple constructor. * @param source source text */ public DummyLocalizable(final String source) { this.source = source; } /** {@inheritDoc} */ public String getSourceString() { return source; } /** {@inheritDoc} */ public String getLocalizedString(Locale locale) { return source; } /** {@inheritDoc} */ @Override public String toString() { return source; } } ././@LongLink100644 0 0 152 12126630647 10260 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/exception/util/ExceptionContextProvider.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/exception/util/ExceptionContextProvider100644 1750 1750 2501 12126627714 32436 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.exception.util; /** * Interface for accessing the context data structure stored in Commons Math * exceptions. * * @version $Id: ExceptionContextProvider.java 1364388 2012-07-22 18:16:43Z tn $ */ public interface ExceptionContextProvider { /** * Gets a reference to the "rich context" data structure that allows to * customize error messages and store key, value pairs in exceptions. * * @return a reference to the exception context. */ ExceptionContext getContext(); } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/exception/util/LocalizedFormats.java100644 1750 1750 67757 12126627714 31652 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.exception.util; import java.util.Locale; import java.util.MissingResourceException; import java.util.ResourceBundle; /** * Enumeration for localized messages formats used in exceptions messages. *

                              * The constants in this enumeration represent the available * formats as localized strings. These formats are intended to be * localized using simple properties files, using the constant * name as the key and the property value as the message format. * The source English format is provided in the constants themselves * to serve both as a reminder for developers to understand the parameters * needed by each format, as a basis for translators to create * localized properties files, and as a default format if some * translation is missing. *

                              * @since 2.2 * @version $Id: LocalizedFormats.java 1442377 2013-02-04 21:31:42Z erans $ */ public enum LocalizedFormats implements Localizable { // CHECKSTYLE: stop MultipleVariableDeclarations // CHECKSTYLE: stop JavadocVariable ARGUMENT_OUTSIDE_DOMAIN("Argument {0} outside domain [{1} ; {2}]"), ARRAY_SIZE_EXCEEDS_MAX_VARIABLES("array size cannot be greater than {0}"), ARRAY_SIZES_SHOULD_HAVE_DIFFERENCE_1("array sizes should have difference 1 ({0} != {1} + 1)"), ARRAY_SUMS_TO_ZERO("array sums to zero"), ASSYMETRIC_EIGEN_NOT_SUPPORTED("eigen decomposition of assymetric matrices not supported yet"), AT_LEAST_ONE_COLUMN("matrix must have at least one column"), AT_LEAST_ONE_ROW("matrix must have at least one row"), BANDWIDTH("bandwidth ({0})"), BINOMIAL_INVALID_PARAMETERS_ORDER("must have n >= k for binomial coefficient (n, k), got k = {0}, n = {1}"), BINOMIAL_NEGATIVE_PARAMETER("must have n >= 0 for binomial coefficient (n, k), got n = {0}"), CANNOT_CLEAR_STATISTIC_CONSTRUCTED_FROM_EXTERNAL_MOMENTS("statistics constructed from external moments cannot be cleared"), CANNOT_COMPUTE_0TH_ROOT_OF_UNITY("cannot compute 0-th root of unity, indefinite result"), CANNOT_COMPUTE_BETA_DENSITY_AT_0_FOR_SOME_ALPHA("cannot compute beta density at 0 when alpha = {0,number}"), CANNOT_COMPUTE_BETA_DENSITY_AT_1_FOR_SOME_BETA("cannot compute beta density at 1 when beta = %.3g"), CANNOT_COMPUTE_NTH_ROOT_FOR_NEGATIVE_N("cannot compute nth root for null or negative n: {0}"), CANNOT_DISCARD_NEGATIVE_NUMBER_OF_ELEMENTS("cannot discard a negative number of elements ({0})"), CANNOT_FORMAT_INSTANCE_AS_3D_VECTOR("cannot format a {0} instance as a 3D vector"), CANNOT_FORMAT_INSTANCE_AS_COMPLEX("cannot format a {0} instance as a complex number"), CANNOT_FORMAT_INSTANCE_AS_REAL_VECTOR("cannot format a {0} instance as a real vector"), CANNOT_FORMAT_OBJECT_TO_FRACTION("cannot format given object as a fraction number"), CANNOT_INCREMENT_STATISTIC_CONSTRUCTED_FROM_EXTERNAL_MOMENTS("statistics constructed from external moments cannot be incremented"), CANNOT_NORMALIZE_A_ZERO_NORM_VECTOR("cannot normalize a zero norm vector"), CANNOT_RETRIEVE_AT_NEGATIVE_INDEX("elements cannot be retrieved from a negative array index {0}"), CANNOT_SET_AT_NEGATIVE_INDEX("cannot set an element at a negative index {0}"), CANNOT_SUBSTITUTE_ELEMENT_FROM_EMPTY_ARRAY("cannot substitute an element from an empty array"), CANNOT_TRANSFORM_TO_DOUBLE("Conversion Exception in Transformation: {0}"), CARDAN_ANGLES_SINGULARITY("Cardan angles singularity"), CLASS_DOESNT_IMPLEMENT_COMPARABLE("class ({0}) does not implement Comparable"), CLOSEST_ORTHOGONAL_MATRIX_HAS_NEGATIVE_DETERMINANT("the closest orthogonal matrix has a negative determinant {0}"), COLUMN_INDEX_OUT_OF_RANGE("column index {0} out of allowed range [{1}, {2}]"), COLUMN_INDEX("column index ({0})"), /* keep */ CONSTRAINT("constraint"), /* keep */ CONTINUED_FRACTION_INFINITY_DIVERGENCE("Continued fraction convergents diverged to +/- infinity for value {0}"), CONTINUED_FRACTION_NAN_DIVERGENCE("Continued fraction diverged to NaN for value {0}"), CONTRACTION_CRITERIA_SMALLER_THAN_EXPANSION_FACTOR("contraction criteria ({0}) smaller than the expansion factor ({1}). This would lead to a never ending loop of expansion and contraction as a newly expanded internal storage array would immediately satisfy the criteria for contraction."), CONTRACTION_CRITERIA_SMALLER_THAN_ONE("contraction criteria smaller than one ({0}). This would lead to a never ending loop of expansion and contraction as an internal storage array length equal to the number of elements would satisfy the contraction criteria."), CONVERGENCE_FAILED("convergence failed"), /* keep */ CROSSING_BOUNDARY_LOOPS("some outline boundary loops cross each other"), CROSSOVER_RATE("crossover rate ({0})"), CUMULATIVE_PROBABILITY_RETURNED_NAN("Cumulative probability function returned NaN for argument {0} p = {1}"), DIFFERENT_ROWS_LENGTHS("some rows have length {0} while others have length {1}"), DIFFERENT_ORIG_AND_PERMUTED_DATA("original and permuted data must contain the same elements"), DIGEST_NOT_INITIALIZED("digest not initialized"), DIMENSIONS_MISMATCH_2x2("got {0}x{1} but expected {2}x{3}"), /* keep */ DIMENSIONS_MISMATCH_SIMPLE("{0} != {1}"), /* keep */ DIMENSIONS_MISMATCH("dimensions mismatch"), /* keep */ DISCRETE_CUMULATIVE_PROBABILITY_RETURNED_NAN("Discrete cumulative probability function returned NaN for argument {0}"), DISTRIBUTION_NOT_LOADED("distribution not loaded"), DUPLICATED_ABSCISSA_DIVISION_BY_ZERO("duplicated abscissa {0} causes division by zero"), ELITISM_RATE("elitism rate ({0})"), EMPTY_CLUSTER_IN_K_MEANS("empty cluster in k-means"), EMPTY_INTERPOLATION_SAMPLE("sample for interpolation is empty"), EMPTY_POLYNOMIALS_COEFFICIENTS_ARRAY("empty polynomials coefficients array"), /* keep */ EMPTY_SELECTED_COLUMN_INDEX_ARRAY("empty selected column index array"), EMPTY_SELECTED_ROW_INDEX_ARRAY("empty selected row index array"), EMPTY_STRING_FOR_IMAGINARY_CHARACTER("empty string for imaginary character"), ENDPOINTS_NOT_AN_INTERVAL("endpoints do not specify an interval: [{0}, {1}]"), EQUAL_VERTICES_IN_SIMPLEX("equal vertices {0} and {1} in simplex configuration"), EULER_ANGLES_SINGULARITY("Euler angles singularity"), EVALUATION("evaluation"), /* keep */ EXPANSION_FACTOR_SMALLER_THAN_ONE("expansion factor smaller than one ({0})"), FACTORIAL_NEGATIVE_PARAMETER("must have n >= 0 for n!, got n = {0}"), FAILED_BRACKETING("number of iterations={4}, maximum iterations={5}, initial={6}, lower bound={7}, upper bound={8}, final a value={0}, final b value={1}, f(a)={2}, f(b)={3}"), FAILED_FRACTION_CONVERSION("Unable to convert {0} to fraction after {1} iterations"), FIRST_COLUMNS_NOT_INITIALIZED_YET("first {0} columns are not initialized yet"), FIRST_ELEMENT_NOT_ZERO("first element is not 0: {0}"), FIRST_ROWS_NOT_INITIALIZED_YET("first {0} rows are not initialized yet"), FRACTION_CONVERSION_OVERFLOW("Overflow trying to convert {0} to fraction ({1}/{2})"), FUNCTION_NOT_DIFFERENTIABLE("function is not differentiable"), FUNCTION_NOT_POLYNOMIAL("function is not polynomial"), GCD_OVERFLOW_32_BITS("overflow: gcd({0}, {1}) is 2^31"), GCD_OVERFLOW_64_BITS("overflow: gcd({0}, {1}) is 2^63"), HOLE_BETWEEN_MODELS_TIME_RANGES("{0} wide hole between models time ranges"), ILL_CONDITIONED_OPERATOR("condition number {1} is too high "), INDEX_LARGER_THAN_MAX("the index specified: {0} is larger than the current maximal index {1}"), INDEX_NOT_POSITIVE("index ({0}) is not positive"), INDEX_OUT_OF_RANGE("index {0} out of allowed range [{1}, {2}]"), INDEX("index ({0})"), /* keep */ NOT_FINITE_NUMBER("{0} is not a finite number"), /* keep */ INFINITE_BOUND("interval bounds must be finite"), ARRAY_ELEMENT("value {0} at index {1}"), /* keep */ INFINITE_ARRAY_ELEMENT("Array contains an infinite element, {0} at index {1}"), INFINITE_VALUE_CONVERSION("cannot convert infinite value"), INITIAL_CAPACITY_NOT_POSITIVE("initial capacity ({0}) is not positive"), INITIAL_COLUMN_AFTER_FINAL_COLUMN("initial column {1} after final column {0}"), INITIAL_ROW_AFTER_FINAL_ROW("initial row {1} after final row {0}"), @Deprecated INPUT_DATA_FROM_UNSUPPORTED_DATASOURCE("input data comes from unsupported datasource: {0}, supported sources: {1}, {2}"), INSTANCES_NOT_COMPARABLE_TO_EXISTING_VALUES("instance of class {0} not comparable to existing values"), INSUFFICIENT_DATA_FOR_T_STATISTIC("insufficient data for t statistic, needs at least 2, got {0}"), INSUFFICIENT_DIMENSION("insufficient dimension {0}, must be at least {1}"), DIMENSION("dimension ({0})"), /* keep */ INSUFFICIENT_OBSERVED_POINTS_IN_SAMPLE("sample contains {0} observed points, at least {1} are required"), INSUFFICIENT_ROWS_AND_COLUMNS("insufficient data: only {0} rows and {1} columns."), INTEGRATION_METHOD_NEEDS_AT_LEAST_TWO_PREVIOUS_POINTS("multistep method needs at least {0} previous steps, got {1}"), INTERNAL_ERROR("internal error, please fill a bug report at {0}"), INVALID_BINARY_DIGIT("invalid binary digit: {0}"), INVALID_BINARY_CHROMOSOME("binary mutation works on BinaryChromosome only"), INVALID_BRACKETING_PARAMETERS("invalid bracketing parameters: lower bound={0}, initial={1}, upper bound={2}"), INVALID_FIXED_LENGTH_CHROMOSOME("one-point crossover only works with fixed-length chromosomes"), INVALID_INTERVAL_INITIAL_VALUE_PARAMETERS("invalid interval, initial value parameters: lower={0}, initial={1}, upper={2}"), INVALID_ITERATIONS_LIMITS("invalid iteration limits: min={0}, max={1}"), INVALID_MAX_ITERATIONS("bad value for maximum iterations number: {0}"), NOT_ENOUGH_DATA_REGRESSION("the number of observations is not sufficient to conduct regression"), INVALID_REGRESSION_ARRAY("input data array length = {0} does not match the number of observations = {1} and the number of regressors = {2}"), INVALID_REGRESSION_OBSERVATION("length of regressor array = {0} does not match the number of variables = {1} in the model"), INVALID_ROUNDING_METHOD("invalid rounding method {0}, valid methods: {1} ({2}), {3} ({4}), {5} ({6}), {7} ({8}), {9} ({10}), {11} ({12}), {13} ({14}), {15} ({16})"), ITERATOR_EXHAUSTED("iterator exhausted"), ITERATIONS("iterations"), /* keep */ LCM_OVERFLOW_32_BITS("overflow: lcm({0}, {1}) is 2^31"), LCM_OVERFLOW_64_BITS("overflow: lcm({0}, {1}) is 2^63"), LIST_OF_CHROMOSOMES_BIGGER_THAN_POPULATION_SIZE("list of chromosomes bigger than maxPopulationSize"), LOESS_EXPECTS_AT_LEAST_ONE_POINT("Loess expects at least 1 point"), LOWER_BOUND_NOT_BELOW_UPPER_BOUND("lower bound ({0}) must be strictly less than upper bound ({1})"), /* keep */ LOWER_ENDPOINT_ABOVE_UPPER_ENDPOINT("lower endpoint ({0}) must be less than or equal to upper endpoint ({1})"), MAP_MODIFIED_WHILE_ITERATING("map has been modified while iterating"), EVALUATIONS("evaluations"), /* keep */ MAX_COUNT_EXCEEDED("maximal count ({0}) exceeded"), /* keep */ MAX_ITERATIONS_EXCEEDED("maximal number of iterations ({0}) exceeded"), MINIMAL_STEPSIZE_REACHED_DURING_INTEGRATION("minimal step size ({1,number,0.00E00}) reached, integration needs {0,number,0.00E00}"), MISMATCHED_LOESS_ABSCISSA_ORDINATE_ARRAYS("Loess expects the abscissa and ordinate arrays to be of the same size, but got {0} abscissae and {1} ordinatae"), MUTATION_RATE("mutation rate ({0})"), NAN_ELEMENT_AT_INDEX("element {0} is NaN"), NAN_VALUE_CONVERSION("cannot convert NaN value"), NEGATIVE_BRIGHTNESS_EXPONENT("brightness exponent should be positive or null, but got {0}"), NEGATIVE_COMPLEX_MODULE("negative complex module {0}"), NEGATIVE_ELEMENT_AT_2D_INDEX("element ({0}, {1}) is negative: {2}"), NEGATIVE_ELEMENT_AT_INDEX("element {0} is negative: {1}"), NEGATIVE_NUMBER_OF_SUCCESSES("number of successes must be non-negative ({0})"), NUMBER_OF_SUCCESSES("number of successes ({0})"), /* keep */ NEGATIVE_NUMBER_OF_TRIALS("number of trials must be non-negative ({0})"), NUMBER_OF_INTERPOLATION_POINTS("number of interpolation points ({0})"), /* keep */ NUMBER_OF_TRIALS("number of trials ({0})"), ROBUSTNESS_ITERATIONS("number of robustness iterations ({0})"), START_POSITION("start position ({0})"), /* keep */ NON_CONVERGENT_CONTINUED_FRACTION("Continued fraction convergents failed to converge (in less than {0} iterations) for value {1}"), NON_INVERTIBLE_TRANSFORM("non-invertible affine transform collapses some lines into single points"), NON_POSITIVE_MICROSPHERE_ELEMENTS("number of microsphere elements must be positive, but got {0}"), NON_POSITIVE_POLYNOMIAL_DEGREE("polynomial degree must be positive: degree={0}"), NON_REAL_FINITE_ABSCISSA("all abscissae must be finite real numbers, but {0}-th is {1}"), NON_REAL_FINITE_ORDINATE("all ordinatae must be finite real numbers, but {0}-th is {1}"), NON_REAL_FINITE_WEIGHT("all weights must be finite real numbers, but {0}-th is {1}"), NON_SQUARE_MATRIX("non square ({0}x{1}) matrix"), NORM("Norm ({0})"), /* keep */ NORMALIZE_INFINITE("Cannot normalize to an infinite value"), NORMALIZE_NAN("Cannot normalize to NaN"), NOT_ADDITION_COMPATIBLE_MATRICES("{0}x{1} and {2}x{3} matrices are not addition compatible"), NOT_DECREASING_NUMBER_OF_POINTS("points {0} and {1} are not decreasing ({2} < {3})"), NOT_DECREASING_SEQUENCE("points {3} and {2} are not decreasing ({1} < {0})"), /* keep */ NOT_ENOUGH_DATA_FOR_NUMBER_OF_PREDICTORS("not enough data ({0} rows) for this many predictors ({1} predictors)"), NOT_ENOUGH_POINTS_IN_SPLINE_PARTITION("spline partition must have at least {0} points, got {1}"), NOT_INCREASING_NUMBER_OF_POINTS("points {0} and {1} are not increasing ({2} > {3})"), NOT_INCREASING_SEQUENCE("points {3} and {2} are not increasing ({1} > {0})"), /* keep */ NOT_MULTIPLICATION_COMPATIBLE_MATRICES("{0}x{1} and {2}x{3} matrices are not multiplication compatible"), NOT_POSITIVE_DEFINITE_MATRIX("not positive definite matrix"), /* keep */ NON_POSITIVE_DEFINITE_MATRIX("not positive definite matrix: diagonal element at ({1},{1}) is smaller than {2} ({0})"), NON_POSITIVE_DEFINITE_OPERATOR("non positive definite linear operator"), /* keep */ NON_SELF_ADJOINT_OPERATOR("non self-adjoint linear operator"), /* keep */ NON_SQUARE_OPERATOR("non square ({0}x{1}) linear operator"), /* keep */ DEGREES_OF_FREEDOM("degrees of freedom ({0})"), /* keep */ NOT_POSITIVE_DEGREES_OF_FREEDOM("degrees of freedom must be positive ({0})"), NOT_POSITIVE_ELEMENT_AT_INDEX("element {0} is not positive: {1}"), NOT_POSITIVE_EXPONENT("invalid exponent {0} (must be positive)"), NUMBER_OF_ELEMENTS_SHOULD_BE_POSITIVE("number of elements should be positive ({0})"), EXPONENT("exponent ({0})"), /* keep */ NOT_POSITIVE_LENGTH("length must be positive ({0})"), LENGTH("length ({0})"), /* keep */ NOT_POSITIVE_MEAN("mean must be positive ({0})"), MEAN("mean ({0})"), /* keep */ NOT_POSITIVE_NUMBER_OF_SAMPLES("number of sample is not positive: {0}"), NUMBER_OF_SAMPLES("number of samples ({0})"), /* keep */ NOT_POSITIVE_PERMUTATION("permutation k ({0}) must be positive"), PERMUTATION_SIZE("permutation size ({0}"), /* keep */ NOT_POSITIVE_POISSON_MEAN("the Poisson mean must be positive ({0})"), NOT_POSITIVE_POPULATION_SIZE("population size must be positive ({0})"), POPULATION_SIZE("population size ({0})"), /* keep */ NOT_POSITIVE_ROW_DIMENSION("invalid row dimension: {0} (must be positive)"), NOT_POSITIVE_SAMPLE_SIZE("sample size must be positive ({0})"), NOT_POSITIVE_SCALE("scale must be positive ({0})"), SCALE("scale ({0})"), /* keep */ NOT_POSITIVE_SHAPE("shape must be positive ({0})"), SHAPE("shape ({0})"), /* keep */ NOT_POSITIVE_STANDARD_DEVIATION("standard deviation must be positive ({0})"), STANDARD_DEVIATION("standard deviation ({0})"), /* keep */ NOT_POSITIVE_UPPER_BOUND("upper bound must be positive ({0})"), NOT_POSITIVE_WINDOW_SIZE("window size must be positive ({0})"), NOT_POWER_OF_TWO("{0} is not a power of 2"), NOT_POWER_OF_TWO_CONSIDER_PADDING("{0} is not a power of 2, consider padding for fix"), NOT_POWER_OF_TWO_PLUS_ONE("{0} is not a power of 2 plus one"), NOT_STRICTLY_DECREASING_NUMBER_OF_POINTS("points {0} and {1} are not strictly decreasing ({2} <= {3})"), NOT_STRICTLY_DECREASING_SEQUENCE("points {3} and {2} are not strictly decreasing ({1} <= {0})"), /* keep */ NOT_STRICTLY_INCREASING_KNOT_VALUES("knot values must be strictly increasing"), NOT_STRICTLY_INCREASING_NUMBER_OF_POINTS("points {0} and {1} are not strictly increasing ({2} >= {3})"), NOT_STRICTLY_INCREASING_SEQUENCE("points {3} and {2} are not strictly increasing ({1} >= {0})"), /* keep */ NOT_SUBTRACTION_COMPATIBLE_MATRICES("{0}x{1} and {2}x{3} matrices are not subtraction compatible"), NOT_SUPPORTED_IN_DIMENSION_N("method not supported in dimension {0}"), NOT_SYMMETRIC_MATRIX("not symmetric matrix"), NON_SYMMETRIC_MATRIX("non symmetric matrix: the difference between entries at ({0},{1}) and ({1},{0}) is larger than {2}"), /* keep */ NO_BIN_SELECTED("no bin selected"), NO_CONVERGENCE_WITH_ANY_START_POINT("none of the {0} start points lead to convergence"), /* keep */ NO_DATA("no data"), /* keep */ NO_DEGREES_OF_FREEDOM("no degrees of freedom ({0} measurements, {1} parameters)"), NO_DENSITY_FOR_THIS_DISTRIBUTION("This distribution does not have a density function implemented"), NO_FEASIBLE_SOLUTION("no feasible solution"), NO_OPTIMUM_COMPUTED_YET("no optimum computed yet"), /* keep */ NO_REGRESSORS("Regression model must include at least one regressor"), NO_RESULT_AVAILABLE("no result available"), NO_SUCH_MATRIX_ENTRY("no entry at indices ({0}, {1}) in a {2}x{3} matrix"), NAN_NOT_ALLOWED("NaN is not allowed"), NULL_NOT_ALLOWED("null is not allowed"), /* keep */ ARRAY_ZERO_LENGTH_OR_NULL_NOT_ALLOWED("a null or zero length array not allowed"), COVARIANCE_MATRIX("covariance matrix"), /* keep */ DENOMINATOR("denominator"), /* keep */ DENOMINATOR_FORMAT("denominator format"), /* keep */ FRACTION("fraction"), /* keep */ FUNCTION("function"), /* keep */ IMAGINARY_FORMAT("imaginary format"), /* keep */ INPUT_ARRAY("input array"), /* keep */ NUMERATOR("numerator"), /* keep */ NUMERATOR_FORMAT("numerator format"), /* keep */ OBJECT_TRANSFORMATION("conversion exception in transformation"), /* keep */ REAL_FORMAT("real format"), /* keep */ WHOLE_FORMAT("whole format"), /* keep */ NUMBER_TOO_LARGE("{0} is larger than the maximum ({1})"), /* keep */ NUMBER_TOO_SMALL("{0} is smaller than the minimum ({1})"), /* keep */ NUMBER_TOO_LARGE_BOUND_EXCLUDED("{0} is larger than, or equal to, the maximum ({1})"), /* keep */ NUMBER_TOO_SMALL_BOUND_EXCLUDED("{0} is smaller than, or equal to, the minimum ({1})"), /* keep */ NUMBER_OF_SUCCESS_LARGER_THAN_POPULATION_SIZE("number of successes ({0}) must be less than or equal to population size ({1})"), NUMERATOR_OVERFLOW_AFTER_MULTIPLY("overflow, numerator too large after multiply: {0}"), N_POINTS_GAUSS_LEGENDRE_INTEGRATOR_NOT_SUPPORTED("{0} points Legendre-Gauss integrator not supported, number of points must be in the {1}-{2} range"), OBSERVED_COUNTS_ALL_ZERO("observed counts are all 0 in observed array {0}"), OBSERVED_COUNTS_BOTTH_ZERO_FOR_ENTRY("observed counts are both zero for entry {0}"), BOBYQA_BOUND_DIFFERENCE_CONDITION("the difference between the upper and lower bound must be larger than twice the initial trust region radius ({0})"), OUT_OF_BOUNDS_QUANTILE_VALUE("out of bounds quantile value: {0}, must be in (0, 100]"), OUT_OF_BOUND_SIGNIFICANCE_LEVEL("out of bounds significance level {0}, must be between {1} and {2}"), SIGNIFICANCE_LEVEL("significance level ({0})"), /* keep */ OUT_OF_ORDER_ABSCISSA_ARRAY("the abscissae array must be sorted in a strictly increasing order, but the {0}-th element is {1} whereas {2}-th is {3}"), OUT_OF_RANGE_ROOT_OF_UNITY_INDEX("out of range root of unity index {0} (must be in [{1};{2}])"), OUT_OF_RANGE("out of range"), /* keep */ OUT_OF_RANGE_SIMPLE("{0} out of [{1}, {2}] range"), /* keep */ OUT_OF_RANGE_LEFT("{0} out of ({1}, {2}] range"), OUT_OF_RANGE_RIGHT("{0} out of [{1}, {2}) range"), OUTLINE_BOUNDARY_LOOP_OPEN("an outline boundary loop is open"), OVERFLOW("overflow"), /* keep */ OVERFLOW_IN_FRACTION("overflow in fraction {0}/{1}, cannot negate"), OVERFLOW_IN_ADDITION("overflow in addition: {0} + {1}"), OVERFLOW_IN_SUBTRACTION("overflow in subtraction: {0} - {1}"), PERCENTILE_IMPLEMENTATION_CANNOT_ACCESS_METHOD("cannot access {0} method in percentile implementation {1}"), PERCENTILE_IMPLEMENTATION_UNSUPPORTED_METHOD("percentile implementation {0} does not support {1}"), PERMUTATION_EXCEEDS_N("permutation size ({0}) exceeds permuation domain ({1})"), /* keep */ POLYNOMIAL("polynomial"), /* keep */ POLYNOMIAL_INTERPOLANTS_MISMATCH_SEGMENTS("number of polynomial interpolants must match the number of segments ({0} != {1} - 1)"), POPULATION_LIMIT_NOT_POSITIVE("population limit has to be positive"), POWER_NEGATIVE_PARAMETERS("cannot raise an integral value to a negative power ({0}^{1})"), PROPAGATION_DIRECTION_MISMATCH("propagation direction mismatch"), RANDOMKEY_MUTATION_WRONG_CLASS("RandomKeyMutation works only with RandomKeys, not {0}"), ROOTS_OF_UNITY_NOT_COMPUTED_YET("roots of unity have not been computed yet"), ROTATION_MATRIX_DIMENSIONS("a {0}x{1} matrix cannot be a rotation matrix"), ROW_INDEX_OUT_OF_RANGE("row index {0} out of allowed range [{1}, {2}]"), ROW_INDEX("row index ({0})"), /* keep */ SAME_SIGN_AT_ENDPOINTS("function values at endpoints do not have different signs, endpoints: [{0}, {1}], values: [{2}, {3}]"), SAMPLE_SIZE_EXCEEDS_COLLECTION_SIZE("sample size ({0}) exceeds collection size ({1})"), /* keep */ SAMPLE_SIZE_LARGER_THAN_POPULATION_SIZE("sample size ({0}) must be less than or equal to population size ({1})"), SIMPLEX_NEED_ONE_POINT("simplex must contain at least one point"), SIMPLE_MESSAGE("{0}"), SINGULAR_MATRIX("matrix is singular"), /* keep */ SINGULAR_OPERATOR("operator is singular"), SUBARRAY_ENDS_AFTER_ARRAY_END("subarray ends after array end"), TOO_LARGE_CUTOFF_SINGULAR_VALUE("cutoff singular value is {0}, should be at most {1}"), TOO_LARGE_TOURNAMENT_ARITY("tournament arity ({0}) cannot be bigger than population size ({1})"), TOO_MANY_ELEMENTS_TO_DISCARD_FROM_ARRAY("cannot discard {0} elements from a {1} elements array"), TOO_MANY_REGRESSORS("too many regressors ({0}) specified, only {1} in the model"), TOO_SMALL_COST_RELATIVE_TOLERANCE("cost relative tolerance is too small ({0}), no further reduction in the sum of squares is possible"), TOO_SMALL_INTEGRATION_INTERVAL("too small integration interval: length = {0}"), TOO_SMALL_ORTHOGONALITY_TOLERANCE("orthogonality tolerance is too small ({0}), solution is orthogonal to the jacobian"), TOO_SMALL_PARAMETERS_RELATIVE_TOLERANCE("parameters relative tolerance is too small ({0}), no further improvement in the approximate solution is possible"), TRUST_REGION_STEP_FAILED("trust region step has failed to reduce Q"), TWO_OR_MORE_CATEGORIES_REQUIRED("two or more categories required, got {0}"), TWO_OR_MORE_VALUES_IN_CATEGORY_REQUIRED("two or more values required in each category, one has {0}"), UNABLE_TO_BRACKET_OPTIMUM_IN_LINE_SEARCH("unable to bracket optimum in line search"), UNABLE_TO_COMPUTE_COVARIANCE_SINGULAR_PROBLEM("unable to compute covariances: singular problem"), UNABLE_TO_FIRST_GUESS_HARMONIC_COEFFICIENTS("unable to first guess the harmonic coefficients"), UNABLE_TO_ORTHOGONOLIZE_MATRIX("unable to orthogonalize matrix in {0} iterations"), UNABLE_TO_PERFORM_QR_DECOMPOSITION_ON_JACOBIAN("unable to perform Q.R decomposition on the {0}x{1} jacobian matrix"), UNABLE_TO_SOLVE_SINGULAR_PROBLEM("unable to solve: singular problem"), UNBOUNDED_SOLUTION("unbounded solution"), UNKNOWN_MODE("unknown mode {0}, known modes: {1} ({2}), {3} ({4}), {5} ({6}), {7} ({8}), {9} ({10}) and {11} ({12})"), UNKNOWN_PARAMETER("unknown parameter {0}"), UNMATCHED_ODE_IN_EXPANDED_SET("ode does not match the main ode set in the extended set"), CANNOT_PARSE_AS_TYPE("string \"{0}\" unparseable (from position {1}) as an object of type {2}"), /* keep */ CANNOT_PARSE("string \"{0}\" unparseable (from position {1})"), /* keep */ UNPARSEABLE_3D_VECTOR("unparseable 3D vector: \"{0}\""), UNPARSEABLE_COMPLEX_NUMBER("unparseable complex number: \"{0}\""), UNPARSEABLE_REAL_VECTOR("unparseable real vector: \"{0}\""), UNSUPPORTED_EXPANSION_MODE("unsupported expansion mode {0}, supported modes are {1} ({2}) and {3} ({4})"), UNSUPPORTED_OPERATION("unsupported operation"), /* keep */ ARITHMETIC_EXCEPTION("arithmetic exception"), /* keep */ ILLEGAL_STATE("illegal state"), /* keep */ USER_EXCEPTION("exception generated in user code"), /* keep */ URL_CONTAINS_NO_DATA("URL {0} contains no data"), VALUES_ADDED_BEFORE_CONFIGURING_STATISTIC("{0} values have been added before statistic is configured"), VECTOR_LENGTH_MISMATCH("vector length mismatch: got {0} but expected {1}"), VECTOR_MUST_HAVE_AT_LEAST_ONE_ELEMENT("vector must have at least one element"), WEIGHT_AT_LEAST_ONE_NON_ZERO("weigth array must contain at least one non-zero value"), WRONG_BLOCK_LENGTH("wrong array shape (block length = {0}, expected {1})"), WRONG_NUMBER_OF_POINTS("{0} points are required, got only {1}"), NUMBER_OF_POINTS("number of points ({0})"), /* keep */ ZERO_DENOMINATOR("denominator must be different from 0"), /* keep */ ZERO_DENOMINATOR_IN_FRACTION("zero denominator in fraction {0}/{1}"), ZERO_FRACTION_TO_DIVIDE_BY("the fraction to divide by must not be zero: {0}/{1}"), ZERO_NORM("zero norm"), ZERO_NORM_FOR_ROTATION_AXIS("zero norm for rotation axis"), ZERO_NORM_FOR_ROTATION_DEFINING_VECTOR("zero norm for rotation defining vector"), ZERO_NOT_ALLOWED("zero not allowed here"); // CHECKSTYLE: resume JavadocVariable // CHECKSTYLE: resume MultipleVariableDeclarations /** Source English format. */ private final String sourceFormat; /** Simple constructor. * @param sourceFormat source English format to use when no * localized version is available */ private LocalizedFormats(final String sourceFormat) { this.sourceFormat = sourceFormat; } /** {@inheritDoc} */ public String getSourceString() { return sourceFormat; } /** {@inheritDoc} */ public String getLocalizedString(final Locale locale) { try { final String path = LocalizedFormats.class.getName().replaceAll("\\.", "/"); ResourceBundle bundle = ResourceBundle.getBundle("assets/" + path, locale); if (bundle.getLocale().getLanguage().equals(locale.getLanguage())) { // the value of the resource is the translated format return bundle.getString(toString()); } } catch (MissingResourceException mre) { // NOPMD // do nothing here } // either the locale is not supported or the resource is unknown // don't translate and fall back to using the source format return sourceFormat; } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/exception/util/ArgUtils.java100644 1750 1750 3572 12126627714 30103 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.exception.util; import java.util.List; import java.util.ArrayList; /** * Utility class for transforming the list of arguments passed to * constructors of exceptions. * * @version $Id: ArgUtils.java 1364388 2012-07-22 18:16:43Z tn $ */ public class ArgUtils { /** * Class contains only static methods. */ private ArgUtils() {} /** * Transform a multidimensional array into a one-dimensional list. * * @param array Array (possibly multidimensional). * @return a list of all the {@code Object} instances contained in * {@code array}. */ public static Object[] flatten(Object[] array) { final List list = new ArrayList(); if (array != null) { for (Object o : array) { if (o instanceof Object[]) { for (Object oR : flatten((Object[]) o)) { list.add(oR); } } else { list.add(o); } } } return list.toArray(); } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/exception/util/ExceptionContext.java100644 1750 1750 24622 12126627714 31673 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.exception.util; import java.util.List; import java.util.ArrayList; import java.util.Set; import java.util.Map; import java.io.IOException; import java.io.Serializable; import java.io.ObjectOutputStream; import java.io.ObjectInputStream; import java.util.HashMap; import java.text.MessageFormat; import java.util.Locale; /** * Class that contains the actual implementation of the functionality mandated * by the {@link ExceptionContext} interface. * All Commons Math exceptions delegate the interface's methods to this class. * * @since 3.0 * @version $Id: ExceptionContext.java 1364388 2012-07-22 18:16:43Z tn $ */ public class ExceptionContext implements Serializable { /** Serializable version Id. */ private static final long serialVersionUID = -6024911025449780478L; /** * The throwable to which this context refers to. */ private Throwable throwable; /** * Various informations that enrich the informative message. */ private List msgPatterns; /** * Various informations that enrich the informative message. * The arguments will replace the corresponding place-holders in * {@link #msgPatterns}. */ private List msgArguments; /** * Arbitrary context information. */ private Map context; /** Simple constructor. * @param throwable the exception this context refers too */ public ExceptionContext(final Throwable throwable) { this.throwable = throwable; msgPatterns = new ArrayList(); msgArguments = new ArrayList(); context = new HashMap(); } /** Get a reference to the exception to which the context relates. * @return a reference to the exception to which the context relates */ public Throwable getThrowable() { return throwable; } /** * Adds a message. * * @param pattern Message pattern. * @param arguments Values for replacing the placeholders in the message * pattern. */ public void addMessage(Localizable pattern, Object ... arguments) { msgPatterns.add(pattern); msgArguments.add(ArgUtils.flatten(arguments)); } /** * Sets the context (key, value) pair. * Keys are assumed to be unique within an instance. If the same key is * assigned a new value, the previous one will be lost. * * @param key Context key (not null). * @param value Context value. */ public void setValue(String key, Object value) { context.put(key, value); } /** * Gets the value associated to the given context key. * * @param key Context key. * @return the context value or {@code null} if the key does not exist. */ public Object getValue(String key) { return context.get(key); } /** * Gets all the keys stored in the exception * * @return the set of keys. */ public Set getKeys() { return context.keySet(); } /** * Gets the default message. * * @return the message. */ public String getMessage() { return getMessage(Locale.US); } /** * Gets the message in the default locale. * * @return the localized message. */ public String getLocalizedMessage() { return getMessage(Locale.getDefault()); } /** * Gets the message in a specified locale. * * @param locale Locale in which the message should be translated. * @return the localized message. */ public String getMessage(final Locale locale) { return buildMessage(locale, ": "); } /** * Gets the message in a specified locale. * * @param locale Locale in which the message should be translated. * @param separator Separator inserted between the message parts. * @return the localized message. */ public String getMessage(final Locale locale, final String separator) { return buildMessage(locale, separator); } /** * Builds a message string. * * @param locale Locale in which the message should be translated. * @param separator Message separator. * @return a localized message string. */ private String buildMessage(Locale locale, String separator) { final StringBuilder sb = new StringBuilder(); int count = 0; final int len = msgPatterns.size(); for (int i = 0; i < len; i++) { final Localizable pat = msgPatterns.get(i); final Object[] args = msgArguments.get(i); final MessageFormat fmt = new MessageFormat(pat.getLocalizedString(locale), locale); sb.append(fmt.format(args)); if (++count < len) { // Add a separator if there are other messages. sb.append(separator); } } return sb.toString(); } /** * Serialize this object to the given stream. * * @param out Stream. * @throws IOException This should never happen. */ private void writeObject(ObjectOutputStream out) throws IOException { out.writeObject(throwable); serializeMessages(out); serializeContext(out); } /** * Deserialize this object from the given stream. * * @param in Stream. * @throws IOException This should never happen. * @throws ClassNotFoundException This should never happen. */ private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException { throwable = (Throwable) in.readObject(); deSerializeMessages(in); deSerializeContext(in); } /** * Serialize {@link #msgPatterns} and {@link #msgArguments}. * * @param out Stream. * @throws IOException This should never happen. */ private void serializeMessages(ObjectOutputStream out) throws IOException { // Step 1. final int len = msgPatterns.size(); out.writeInt(len); // Step 2. for (int i = 0; i < len; i++) { final Localizable pat = msgPatterns.get(i); // Step 3. out.writeObject(pat); final Object[] args = msgArguments.get(i); final int aLen = args.length; // Step 4. out.writeInt(aLen); for (int j = 0; j < aLen; j++) { if (args[j] instanceof Serializable) { // Step 5a. out.writeObject(args[j]); } else { // Step 5b. out.writeObject(nonSerializableReplacement(args[j])); } } } } /** * Deserialize {@link #msgPatterns} and {@link #msgArguments}. * * @param in Stream. * @throws IOException This should never happen. * @throws ClassNotFoundException This should never happen. */ private void deSerializeMessages(ObjectInputStream in) throws IOException, ClassNotFoundException { // Step 1. final int len = in.readInt(); msgPatterns = new ArrayList(len); msgArguments = new ArrayList(len); // Step 2. for (int i = 0; i < len; i++) { // Step 3. final Localizable pat = (Localizable) in.readObject(); msgPatterns.add(pat); // Step 4. final int aLen = in.readInt(); final Object[] args = new Object[aLen]; for (int j = 0; j < aLen; j++) { // Step 5. args[j] = in.readObject(); } msgArguments.add(args); } } /** * Serialize {@link #context}. * * @param out Stream. * @throws IOException This should never happen. */ private void serializeContext(ObjectOutputStream out) throws IOException { // Step 1. final int len = context.keySet().size(); out.writeInt(len); for (String key : context.keySet()) { // Step 2. out.writeObject(key); final Object value = context.get(key); if (value instanceof Serializable) { // Step 3a. out.writeObject(value); } else { // Step 3b. out.writeObject(nonSerializableReplacement(value)); } } } /** * Deserialize {@link #context}. * * @param in Stream. * @throws IOException This should never happen. * @throws ClassNotFoundException This should never happen. */ private void deSerializeContext(ObjectInputStream in) throws IOException, ClassNotFoundException { // Step 1. final int len = in.readInt(); context = new HashMap(); for (int i = 0; i < len; i++) { // Step 2. final String key = (String) in.readObject(); // Step 3. final Object value = in.readObject(); context.put(key, value); } } /** * Replaces a non-serializable object with an error message string. * * @param obj Object that does not implement the {@code Serializable} * interface. * @return a string that mentions which class could not be serialized. */ private String nonSerializableReplacement(Object obj) { return "[Object could not be serialized: " + obj.getClass().getName() + "]"; } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/exception/util/Localizable.java100644 1750 1750 2723 12126627714 30567 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.exception.util; import java.io.Serializable; import java.util.Locale; /** * Interface for localizable strings. * * @version $Id: Localizable.java 1416643 2012-12-03 19:37:14Z tn $ * @since 2.2 */ public interface Localizable extends Serializable { /** * Gets the source (non-localized) string. * * @return the source string. */ String getSourceString(); /** * Gets the localized string. * * @param locale locale into which to get the string. * @return the localized string or the source string if no * localized version is available. */ String getLocalizedString(Locale locale); } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/exception/ConvergenceException.java100644 1750 1750 3561 12126627714 31507 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.exception; import org.apache.commons.math3.exception.util.Localizable; import org.apache.commons.math3.exception.util.LocalizedFormats; /** * Error thrown when a numerical computation can not be performed because the * numerical result failed to converge to a finite value. * * @since 2.2 * @version $Id: ConvergenceException.java 1364378 2012-07-22 17:42:38Z tn $ */ public class ConvergenceException extends MathIllegalStateException { /** Serializable version Id. */ private static final long serialVersionUID = 4330003017885151975L; /** * Construct the exception. */ public ConvergenceException() { this(LocalizedFormats.CONVERGENCE_FAILED); } /** * Construct the exception with a specific context and arguments. * * @param pattern Message pattern providing the specific context of * the error. * @param args Arguments. */ public ConvergenceException(Localizable pattern, Object ... args) { getContext().addMessage(pattern, args); } } ././@LongLink100644 0 0 156 12126630647 10264 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/exception/MathUnsupportedOperationException.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/exception/MathUnsupportedOperationExcep100644 1750 1750 5051 12126627714 32456 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.exception; import org.apache.commons.math3.exception.util.Localizable; import org.apache.commons.math3.exception.util.LocalizedFormats; import org.apache.commons.math3.exception.util.ExceptionContext; import org.apache.commons.math3.exception.util.ExceptionContextProvider; /** * Base class for all unsupported features. * It is used for all the exceptions that have the semantics of the standard * {@link UnsupportedOperationException}, but must also provide a localized * message. * * @since 2.2 * @version $Id: MathUnsupportedOperationException.java 1364378 2012-07-22 17:42:38Z tn $ */ public class MathUnsupportedOperationException extends UnsupportedOperationException implements ExceptionContextProvider { /** Serializable version Id. */ private static final long serialVersionUID = -6024911025449780478L; /** Context. */ private final ExceptionContext context; /** * Default constructor. */ public MathUnsupportedOperationException() { this(LocalizedFormats.UNSUPPORTED_OPERATION); } /** * @param pattern Message pattern providing the specific context of * the error. * @param args Arguments. */ public MathUnsupportedOperationException(Localizable pattern, Object ... args) { context = new ExceptionContext(this); context.addMessage(pattern, args); } /** {@inheritDoc} */ public ExceptionContext getContext() { return context; } /** {@inheritDoc} */ @Override public String getMessage() { return context.getMessage(); } /** {@inheritDoc} */ @Override public String getLocalizedMessage() { return context.getLocalizedMessage(); } } ././@LongLink100644 0 0 151 12126630647 10257 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/exception/MathIllegalArgumentException.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/exception/MathIllegalArgumentException.100644 1750 1750 4535 12126627714 32277 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.exception; import org.apache.commons.math3.exception.util.Localizable; import org.apache.commons.math3.exception.util.ExceptionContext; import org.apache.commons.math3.exception.util.ExceptionContextProvider; /** * Base class for all preconditions violation exceptions. * In most cases, this class should not be instantiated directly: it should * serve as a base class to create all the exceptions that have the semantics * of the standard {@link IllegalArgumentException}. * * @since 2.2 * @version $Id: MathIllegalArgumentException.java 1364378 2012-07-22 17:42:38Z tn $ */ public class MathIllegalArgumentException extends IllegalArgumentException implements ExceptionContextProvider { /** Serializable version Id. */ private static final long serialVersionUID = -6024911025449780478L; /** Context. */ private final ExceptionContext context; /** * @param pattern Message pattern explaining the cause of the error. * @param args Arguments. */ public MathIllegalArgumentException(Localizable pattern, Object ... args) { context = new ExceptionContext(this); context.addMessage(pattern, args); } /** {@inheritDoc} */ public ExceptionContext getContext() { return context; } /** {@inheritDoc} */ @Override public String getMessage() { return context.getMessage(); } /** {@inheritDoc} */ @Override public String getLocalizedMessage() { return context.getLocalizedMessage(); } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/util/TransformerMap.java100644 1750 1750 13512 12126627720 27323 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.util; import java.io.Serializable; import java.util.Collection; import java.util.HashMap; import java.util.Map; import java.util.Set; import org.apache.commons.math3.exception.MathIllegalArgumentException; /** * This TansformerMap automates the transformation of mixed object types. * It provides a means to set NumberTransformers that will be selected * based on the Class of the object handed to the Maps * double transform(Object o) method. * @version $Id: TransformerMap.java 1416643 2012-12-03 19:37:14Z tn $ */ public class TransformerMap implements NumberTransformer, Serializable { /** Serializable version identifier */ private static final long serialVersionUID = 4605318041528645258L; /** * A default Number Transformer for Numbers and numeric Strings. */ private NumberTransformer defaultTransformer = null; /** * The internal Map. */ private Map, NumberTransformer> map = null; /** * Build a map containing only the default transformer. */ public TransformerMap() { map = new HashMap, NumberTransformer>(); defaultTransformer = new DefaultTransformer(); } /** * Tests if a Class is present in the TransformerMap. * @param key Class to check * @return true|false */ public boolean containsClass(Class key) { return map.containsKey(key); } /** * Tests if a NumberTransformer is present in the TransformerMap. * @param value NumberTransformer to check * @return true|false */ public boolean containsTransformer(NumberTransformer value) { return map.containsValue(value); } /** * Returns the Transformer that is mapped to a class * if mapping is not present, this returns null. * @param key The Class of the object * @return the mapped NumberTransformer or null. */ public NumberTransformer getTransformer(Class key) { return map.get(key); } /** * Sets a Class to Transformer Mapping in the Map. If * the Class is already present, this overwrites that * mapping. * @param key The Class * @param transformer The NumberTransformer * @return the replaced transformer if one is present */ public NumberTransformer putTransformer(Class key, NumberTransformer transformer) { return map.put(key, transformer); } /** * Removes a Class to Transformer Mapping in the Map. * @param key The Class * @return the removed transformer if one is present or * null if none was present. */ public NumberTransformer removeTransformer(Class key) { return map.remove(key); } /** * Clears all the Class to Transformer mappings. */ public void clear() { map.clear(); } /** * Returns the Set of Classes used as keys in the map. * @return Set of Classes */ public Set> classes() { return map.keySet(); } /** * Returns the Set of NumberTransformers used as values * in the map. * @return Set of NumberTransformers */ public Collection transformers() { return map.values(); } /** * Attempts to transform the Object against the map of * NumberTransformers. Otherwise it returns Double.NaN. * * @param o the Object to be transformed. * @return the double value of the Object. * @throws MathIllegalArgumentException if the Object can not be * transformed into a Double. * @see org.apache.commons.math3.util.NumberTransformer#transform(java.lang.Object) */ public double transform(Object o) throws MathIllegalArgumentException { double value = Double.NaN; if (o instanceof Number || o instanceof String) { value = defaultTransformer.transform(o); } else { NumberTransformer trans = getTransformer(o.getClass()); if (trans != null) { value = trans.transform(o); } } return value; } /** {@inheritDoc} */ @Override public boolean equals(Object other) { if (this == other) { return true; } if (other instanceof TransformerMap) { TransformerMap rhs = (TransformerMap) other; if (! defaultTransformer.equals(rhs.defaultTransformer)) { return false; } if (map.size() != rhs.map.size()) { return false; } for (Map.Entry, NumberTransformer> entry : map.entrySet()) { if (! entry.getValue().equals(rhs.map.get(entry.getKey()))) { return false; } } return true; } return false; } /** {@inheritDoc} */ @Override public int hashCode() { int hash = defaultTransformer.hashCode(); for (NumberTransformer t : map.values()) { hash = hash * 31 + t.hashCode(); } return hash; } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/util/Pair.java100644 1750 1750 6730 12126627720 25242 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.util; /** * Generic pair. *
                              * Although the instances of this class are immutable, it is impossible * to ensure that the references passed to the constructor will not be * modified by the caller. * * @param Key type. * @param Value type. * * @since 3.0 * @version $Id: Pair.java 1422313 2012-12-15 18:53:41Z psteitz $ */ public class Pair { /** Key. */ private final K key; /** Value. */ private final V value; /** * Create an entry representing a mapping from the specified key to the * specified value. * * @param k Key (first element of the pair). * @param v Value (second element of the pair). */ public Pair(K k, V v) { key = k; value = v; } /** * Create an entry representing the same mapping as the specified entry. * * @param entry Entry to copy. */ public Pair(Pair entry) { this(entry.getKey(), entry.getValue()); } /** * Get the key. * * @return the key (first element of the pair). */ public K getKey() { return key; } /** * Get the value. * * @return the value (second element of the pair). */ public V getValue() { return value; } /** * Get the first element of the pair. * * @return the first element of the pair. * @since 3.1 */ public K getFirst() { return key; } /** * Get the second element of the pair. * * @return the second element of the pair. * @since 3.1 */ public V getSecond() { return value; } /** * Compare the specified object with this entry for equality. * * @param o Object. * @return {@code true} if the given object is also a map entry and * the two entries represent the same mapping. */ @Override public boolean equals(Object o) { if (this == o) { return true; } if (!(o instanceof Pair)) { return false; } else { Pair oP = (Pair) o; return (key == null ? oP.key == null : key.equals(oP.key)) && (value == null ? oP.value == null : value.equals(oP.value)); } } /** * Compute a hash code. * * @return the hash code value. */ @Override public int hashCode() { int result = key == null ? 0 : key.hashCode(); final int h = value == null ? 0 : value.hashCode(); result = 37 * result + h ^ (h >>> 16); return result; } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/util/ResizableDoubleArray.java100644 1750 1750 133522 12126627720 30461 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.util; import java.io.Serializable; import java.util.Arrays; import org.apache.commons.math3.exception.MathIllegalArgumentException; import org.apache.commons.math3.exception.MathIllegalStateException; import org.apache.commons.math3.exception.MathInternalError; import org.apache.commons.math3.exception.NullArgumentException; import org.apache.commons.math3.exception.NotStrictlyPositiveException; import org.apache.commons.math3.exception.NumberIsTooSmallException; import org.apache.commons.math3.exception.util.LocalizedFormats; /** *

                              * A variable length {@link DoubleArray} implementation that automatically * handles expanding and contracting its internal storage array as elements * are added and removed. *

                              *

                              Important note: Usage should not assume that this class is thread-safe * even though some of the methods are {@code synchronized}. * This qualifier will be dropped in the next major release (4.0).

                              *

                              * The internal storage array starts with capacity determined by the * {@code initialCapacity} property, which can be set by the constructor. * The default initial capacity is 16. Adding elements using * {@link #addElement(double)} appends elements to the end of the array. * When there are no open entries at the end of the internal storage array, * the array is expanded. The size of the expanded array depends on the * {@code expansionMode} and {@code expansionFactor} properties. * The {@code expansionMode} determines whether the size of the array is * multiplied by the {@code expansionFactor} * ({@link ExpansionMode#MULTIPLICATIVE}) or if the expansion is additive * ({@link ExpansionMode#ADDITIVE} -- {@code expansionFactor} storage * locations added). * The default {@code expansionMode} is {@code MULTIPLICATIVE} and the default * {@code expansionFactor} is 2. *

                              *

                              * The {@link #addElementRolling(double)} method adds a new element to the end * of the internal storage array and adjusts the "usable window" of the * internal array forward by one position (effectively making what was the * second element the first, and so on). Repeated activations of this method * (or activation of {@link #discardFrontElements(int)}) will effectively orphan * the storage locations at the beginning of the internal storage array. To * reclaim this storage, each time one of these methods is activated, the size * of the internal storage array is compared to the number of addressable * elements (the {@code numElements} property) and if the difference * is too large, the internal array is contracted to size * {@code numElements + 1}. The determination of when the internal * storage array is "too large" depends on the {@code expansionMode} and * {@code contractionFactor} properties. If the {@code expansionMode} * is {@code MULTIPLICATIVE}, contraction is triggered when the * ratio between storage array length and {@code numElements} exceeds * {@code contractionFactor.} If the {@code expansionMode} * is {@code ADDITIVE}, the number of excess storage locations * is compared to {@code contractionFactor}. *

                              *

                              * To avoid cycles of expansions and contractions, the * {@code expansionFactor} must not exceed the {@code contractionFactor}. * Constructors and mutators for both of these properties enforce this * requirement, throwing a {@code MathIllegalArgumentException} if it is * violated. *

                              * @version $Id: ResizableDoubleArray.java 1462504 2013-03-29 15:48:57Z luc $ */ public class ResizableDoubleArray implements DoubleArray, Serializable { /** Additive expansion mode. * @deprecated As of 3.1. Please use {@link ExpansionMode#ADDITIVE} instead. */ @Deprecated public static final int ADDITIVE_MODE = 1; /** Multiplicative expansion mode. * @deprecated As of 3.1. Please use {@link ExpansionMode#MULTIPLICATIVE} instead. */ @Deprecated public static final int MULTIPLICATIVE_MODE = 0; /** Serializable version identifier. */ private static final long serialVersionUID = -3485529955529426875L; /** Default value for initial capacity. */ private static final int DEFAULT_INITIAL_CAPACITY = 16; /** Default value for array size modifier. */ private static final double DEFAULT_EXPANSION_FACTOR = 2.0; /** * Default value for the difference between {@link #contractionCriterion} * and {@link #expansionFactor}. */ private static final double DEFAULT_CONTRACTION_DELTA = 0.5; /** * The contraction criteria determines when the internal array will be * contracted to fit the number of elements contained in the element * array + 1. */ private double contractionCriterion = 2.5; /** * The expansion factor of the array. When the array needs to be expanded, * the new array size will be * {@code internalArray.length * expansionFactor} * if {@code expansionMode} is set to MULTIPLICATIVE_MODE, or * {@code internalArray.length + expansionFactor} if * {@code expansionMode} is set to ADDITIVE_MODE. */ private double expansionFactor = 2.0; /** * Determines whether array expansion by {@code expansionFactor} * is additive or multiplicative. */ private ExpansionMode expansionMode = ExpansionMode.MULTIPLICATIVE; /** * The internal storage array. */ private double[] internalArray; /** * The number of addressable elements in the array. Note that this * has nothing to do with the length of the internal storage array. */ private int numElements = 0; /** * The position of the first addressable element in the internal storage * array. The addressable elements in the array are * {@code internalArray[startIndex],...,internalArray[startIndex + numElements - 1]}. */ private int startIndex = 0; /** * Specification of expansion algorithm. * @since 3.1 */ public static enum ExpansionMode { /** Multiplicative expansion mode. */ MULTIPLICATIVE, /** Additive expansion mode. */ ADDITIVE } /** * Creates an instance with default properties. *
                                *
                              • {@code initialCapacity = 16}
                              • *
                              • {@code expansionMode = MULTIPLICATIVE}
                              • *
                              • {@code expansionFactor = 2.0}
                              • *
                              • {@code contractionCriterion = 2.5}
                              • *
                              */ public ResizableDoubleArray() { this(DEFAULT_INITIAL_CAPACITY); } /** * Creates an instance with the specified initial capacity. * Other properties take default values: *
                                *
                              • {@code expansionMode = MULTIPLICATIVE}
                              • *
                              • {@code expansionFactor = 2.0}
                              • *
                              • {@code contractionCriterion = 2.5}
                              • *
                              * @param initialCapacity Initial size of the internal storage array. * @throws MathIllegalArgumentException if {@code initialCapacity <= 0}. */ public ResizableDoubleArray(int initialCapacity) throws MathIllegalArgumentException { this(initialCapacity, DEFAULT_EXPANSION_FACTOR); } /** * Creates an instance from an existing {@code double[]} with the * initial capacity and numElements corresponding to the size of * the supplied {@code double[]} array. * If the supplied array is null, a new empty array with the default * initial capacity will be created. * The input array is copied, not referenced. * Other properties take default values: *
                                *
                              • {@code initialCapacity = 16}
                              • *
                              • {@code expansionMode = MULTIPLICATIVE}
                              • *
                              • {@code expansionFactor = 2.0}
                              • *
                              • {@code contractionCriterion = 2.5}
                              • *
                              * * @param initialArray initial array * @since 2.2 */ public ResizableDoubleArray(double[] initialArray) { this(DEFAULT_INITIAL_CAPACITY, DEFAULT_EXPANSION_FACTOR, DEFAULT_CONTRACTION_DELTA + DEFAULT_EXPANSION_FACTOR, ExpansionMode.MULTIPLICATIVE, initialArray); } /** * Creates an instance with the specified initial capacity * and expansion factor. * The remaining properties take default values: *
                                *
                              • {@code expansionMode = MULTIPLICATIVE}
                              • *
                              • {@code contractionCriterion = 0.5 + expansionFactor}
                              • *
                              *
                              * Throws IllegalArgumentException if the following conditions are * not met: *
                                *
                              • {@code initialCapacity > 0}
                              • *
                              • {@code expansionFactor > 1}
                              • *
                              * * @param initialCapacity Initial size of the internal storage array. * @param expansionFactor The array will be expanded based on this * parameter. * @throws MathIllegalArgumentException if parameters are not valid. * @deprecated As of 3.1. Please use * {@link #ResizableDoubleArray(int,double)} instead. */ @Deprecated public ResizableDoubleArray(int initialCapacity, float expansionFactor) throws MathIllegalArgumentException { this(initialCapacity, (double) expansionFactor); } /** * Creates an instance with the specified initial capacity * and expansion factor. * The remaining properties take default values: *
                                *
                              • {@code expansionMode = MULTIPLICATIVE}
                              • *
                              • {@code contractionCriterion = 0.5 + expansionFactor}
                              • *
                              *
                              * Throws IllegalArgumentException if the following conditions are * not met: *
                                *
                              • {@code initialCapacity > 0}
                              • *
                              • {@code expansionFactor > 1}
                              • *
                              * * @param initialCapacity Initial size of the internal storage array. * @param expansionFactor The array will be expanded based on this * parameter. * @throws MathIllegalArgumentException if parameters are not valid. * @since 3.1 */ public ResizableDoubleArray(int initialCapacity, double expansionFactor) throws MathIllegalArgumentException { this(initialCapacity, expansionFactor, DEFAULT_CONTRACTION_DELTA + expansionFactor); } /** * Creates an instance with the specified initialCapacity, * expansionFactor, and contractionCriterion. * The expansion mode will default to {@code MULTIPLICATIVE}. *
                              * Throws IllegalArgumentException if the following conditions are * not met: *
                                *
                              • {@code initialCapacity > 0}
                              • *
                              • {@code expansionFactor > 1}
                              • *
                              • {@code contractionCriterion >= expansionFactor}
                              • *
                              * * @param initialCapacity Initial size of the internal storage array.. * @param expansionFactor The array will be expanded based on this * parameter. * @param contractionCriteria Contraction criteria. * @throws MathIllegalArgumentException if parameters are not valid. * @deprecated As of 3.1. Please use * {@link #ResizableDoubleArray(int,double,double)} instead. */ @Deprecated public ResizableDoubleArray(int initialCapacity, float expansionFactor, float contractionCriteria) throws MathIllegalArgumentException { this(initialCapacity, (double) expansionFactor, (double) contractionCriteria); } /** * Creates an instance with the specified initial capacity, * expansion factor, and contraction criteria. * The expansion mode will default to {@code MULTIPLICATIVE}. *
                              * Throws IllegalArgumentException if the following conditions are * not met: *
                                *
                              • {@code initialCapacity > 0}
                              • *
                              • {@code expansionFactor > 1}
                              • *
                              • {@code contractionCriterion >= expansionFactor}
                              • *
                              * * @param initialCapacity Initial size of the internal storage array.. * @param expansionFactor The array will be expanded based on this * parameter. * @param contractionCriterion Contraction criterion. * @throws MathIllegalArgumentException if the parameters are not valid. * @since 3.1 */ public ResizableDoubleArray(int initialCapacity, double expansionFactor, double contractionCriterion) throws MathIllegalArgumentException { this(initialCapacity, expansionFactor, contractionCriterion, ExpansionMode.MULTIPLICATIVE, null); } /** *

                              * Create a ResizableArray with the specified properties.

                              *

                              * Throws IllegalArgumentException if the following conditions are * not met: *

                                *
                              • initialCapacity > 0
                              • *
                              • expansionFactor > 1
                              • *
                              • contractionFactor >= expansionFactor
                              • *
                              • expansionMode in {MULTIPLICATIVE_MODE, ADDITIVE_MODE} *
                              • *

                              * * @param initialCapacity the initial size of the internal storage array * @param expansionFactor the array will be expanded based on this * parameter * @param contractionCriteria the contraction Criteria * @param expansionMode the expansion mode * @throws MathIllegalArgumentException if parameters are not valid * @deprecated As of 3.1. Please use * {@link #ResizableDoubleArray(int,double,double,ExpansionMode,double[])} * instead. */ public ResizableDoubleArray(int initialCapacity, float expansionFactor, float contractionCriteria, int expansionMode) throws MathIllegalArgumentException { this(initialCapacity, expansionFactor, contractionCriteria, expansionMode == ADDITIVE_MODE ? ExpansionMode.ADDITIVE : ExpansionMode.MULTIPLICATIVE, null); // XXX Just ot retain the expected failure in a unit test. // With the new "enum", that test will become obsolete. setExpansionMode(expansionMode); } /** * Creates an instance with the specified properties. *
                              * Throws MathIllegalArgumentException if the following conditions are * not met: *
                                *
                              • {@code initialCapacity > 0}
                              • *
                              • {@code expansionFactor > 1}
                              • *
                              • {@code contractionCriterion >= expansionFactor}
                              • *
                              * * @param initialCapacity Initial size of the internal storage array. * @param expansionFactor The array will be expanded based on this * parameter. * @param contractionCriterion Contraction criteria. * @param expansionMode Expansion mode. * @param data Initial contents of the array. * @throws MathIllegalArgumentException if the parameters are not valid. */ public ResizableDoubleArray(int initialCapacity, double expansionFactor, double contractionCriterion, ExpansionMode expansionMode, double ... data) throws MathIllegalArgumentException { if (initialCapacity <= 0) { throw new NotStrictlyPositiveException(LocalizedFormats.INITIAL_CAPACITY_NOT_POSITIVE, initialCapacity); } checkContractExpand(contractionCriterion, expansionFactor); this.expansionFactor = expansionFactor; this.contractionCriterion = contractionCriterion; this.expansionMode = expansionMode; internalArray = new double[initialCapacity]; numElements = 0; startIndex = 0; if (data != null) { addElements(data); } } /** * Copy constructor. Creates a new ResizableDoubleArray that is a deep, * fresh copy of the original. Needs to acquire synchronization lock * on original. Original may not be null; otherwise a {@link NullArgumentException} * is thrown. * * @param original array to copy * @exception NullArgumentException if original is null * @since 2.0 */ public ResizableDoubleArray(ResizableDoubleArray original) throws NullArgumentException { MathUtils.checkNotNull(original); copy(original, this); } /** * Adds an element to the end of this expandable array. * * @param value Value to be added to end of array. */ public synchronized void addElement(double value) { if (internalArray.length <= startIndex + numElements) { expand(); } internalArray[startIndex + numElements++] = value; } /** * Adds several element to the end of this expandable array. * * @param values Values to be added to end of array. * @since 2.2 */ public synchronized void addElements(double[] values) { final double[] tempArray = new double[numElements + values.length + 1]; System.arraycopy(internalArray, startIndex, tempArray, 0, numElements); System.arraycopy(values, 0, tempArray, numElements, values.length); internalArray = tempArray; startIndex = 0; numElements += values.length; } /** *

                              * Adds an element to the end of the array and removes the first * element in the array. Returns the discarded first element. * The effect is similar to a push operation in a FIFO queue. *

                              *

                              * Example: If the array contains the elements 1, 2, 3, 4 (in that order) * and addElementRolling(5) is invoked, the result is an array containing * the entries 2, 3, 4, 5 and the value returned is 1. *

                              * * @param value Value to be added to the array. * @return the value which has been discarded or "pushed" out of the array * by this rolling insert. */ public synchronized double addElementRolling(double value) { double discarded = internalArray[startIndex]; if ((startIndex + (numElements + 1)) > internalArray.length) { expand(); } // Increment the start index startIndex += 1; // Add the new value internalArray[startIndex + (numElements - 1)] = value; // Check the contraction criterion. if (shouldContract()) { contract(); } return discarded; } /** * Substitutes value for the most recently added value. * Returns the value that has been replaced. If the array is empty (i.e. * if {@link #numElements} is zero), an IllegalStateException is thrown. * * @param value New value to substitute for the most recently added value * @return the value that has been replaced in the array. * @throws MathIllegalStateException if the array is empty * @since 2.0 */ public synchronized double substituteMostRecentElement(double value) throws MathIllegalStateException { if (numElements < 1) { throw new MathIllegalStateException( LocalizedFormats.CANNOT_SUBSTITUTE_ELEMENT_FROM_EMPTY_ARRAY); } final int substIndex = startIndex + (numElements - 1); final double discarded = internalArray[substIndex]; internalArray[substIndex] = value; return discarded; } /** * Checks the expansion factor and the contraction criterion and throws an * IllegalArgumentException if the contractionCriteria is less than the * expansionCriteria * * @param expansion factor to be checked * @param contraction criteria to be checked * @throws MathIllegalArgumentException if the contractionCriteria is less than * the expansionCriteria. * @deprecated As of 3.1. Please use * {@link #checkContractExpand(double,double)} instead. */ protected void checkContractExpand(float contraction, float expansion) throws MathIllegalArgumentException { checkContractExpand((double) contraction, (double) expansion); } /** * Checks the expansion factor and the contraction criterion and raises * an exception if the contraction criterion is smaller than the * expansion criterion. * * @param contraction Criterion to be checked. * @param expansion Factor to be checked. * @throws NumberIsTooSmallException if {@code contraction < expansion}. * @throws NumberIsTooSmallException if {@code contraction <= 1}. * @throws NumberIsTooSmallException if {@code expansion <= 1 }. * @since 3.1 */ protected void checkContractExpand(double contraction, double expansion) throws NumberIsTooSmallException { if (contraction < expansion) { final NumberIsTooSmallException e = new NumberIsTooSmallException(contraction, 1, true); e.getContext().addMessage(LocalizedFormats.CONTRACTION_CRITERIA_SMALLER_THAN_EXPANSION_FACTOR, contraction, expansion); throw e; } if (contraction <= 1) { final NumberIsTooSmallException e = new NumberIsTooSmallException(contraction, 1, false); e.getContext().addMessage(LocalizedFormats.CONTRACTION_CRITERIA_SMALLER_THAN_ONE, contraction); throw e; } if (expansion <= 1) { final NumberIsTooSmallException e = new NumberIsTooSmallException(contraction, 1, false); e.getContext().addMessage(LocalizedFormats.EXPANSION_FACTOR_SMALLER_THAN_ONE, expansion); throw e; } } /** * Clear the array contents, resetting the number of elements to zero. */ public synchronized void clear() { numElements = 0; startIndex = 0; } /** * Contracts the storage array to the (size of the element set) + 1 - to * avoid a zero length array. This function also resets the startIndex to * zero. */ public synchronized void contract() { final double[] tempArray = new double[numElements + 1]; // Copy and swap - copy only the element array from the src array. System.arraycopy(internalArray, startIndex, tempArray, 0, numElements); internalArray = tempArray; // Reset the start index to zero startIndex = 0; } /** * Discards the i initial elements of the array. For example, * if the array contains the elements 1,2,3,4, invoking * discardFrontElements(2) will cause the first two elements * to be discarded, leaving 3,4 in the array. Throws illegalArgumentException * if i exceeds numElements. * * @param i the number of elements to discard from the front of the array * @throws MathIllegalArgumentException if i is greater than numElements. * @since 2.0 */ public synchronized void discardFrontElements(int i) throws MathIllegalArgumentException { discardExtremeElements(i,true); } /** * Discards the i last elements of the array. For example, * if the array contains the elements 1,2,3,4, invoking * discardMostRecentElements(2) will cause the last two elements * to be discarded, leaving 1,2 in the array. Throws illegalArgumentException * if i exceeds numElements. * * @param i the number of elements to discard from the end of the array * @throws MathIllegalArgumentException if i is greater than numElements. * @since 2.0 */ public synchronized void discardMostRecentElements(int i) throws MathIllegalArgumentException { discardExtremeElements(i,false); } /** * Discards the i first or last elements of the array, * depending on the value of front. * For example, if the array contains the elements 1,2,3,4, invoking * discardExtremeElements(2,false) will cause the last two elements * to be discarded, leaving 1,2 in the array. * For example, if the array contains the elements 1,2,3,4, invoking * discardExtremeElements(2,true) will cause the first two elements * to be discarded, leaving 3,4 in the array. * Throws illegalArgumentException * if i exceeds numElements. * * @param i the number of elements to discard from the front/end of the array * @param front true if elements are to be discarded from the front * of the array, false if elements are to be discarded from the end * of the array * @throws MathIllegalArgumentException if i is greater than numElements. * @since 2.0 */ private synchronized void discardExtremeElements(int i, boolean front) throws MathIllegalArgumentException { if (i > numElements) { throw new MathIllegalArgumentException( LocalizedFormats.TOO_MANY_ELEMENTS_TO_DISCARD_FROM_ARRAY, i, numElements); } else if (i < 0) { throw new MathIllegalArgumentException( LocalizedFormats.CANNOT_DISCARD_NEGATIVE_NUMBER_OF_ELEMENTS, i); } else { // "Subtract" this number of discarded from numElements numElements -= i; if (front) { startIndex += i; } } if (shouldContract()) { contract(); } } /** * Expands the internal storage array using the expansion factor. *

                              * if expansionMode is set to MULTIPLICATIVE_MODE, * the new array size will be internalArray.length * expansionFactor. * If expansionMode is set to ADDITIVE_MODE, the length * after expansion will be internalArray.length + expansionFactor *

                              */ protected synchronized void expand() { // notice the use of FastMath.ceil(), this guarantees that we will always // have an array of at least currentSize + 1. Assume that the // current initial capacity is 1 and the expansion factor // is 1.000000000000000001. The newly calculated size will be // rounded up to 2 after the multiplication is performed. int newSize = 0; if (expansionMode == ExpansionMode.MULTIPLICATIVE) { newSize = (int) FastMath.ceil(internalArray.length * expansionFactor); } else { newSize = (int) (internalArray.length + FastMath.round(expansionFactor)); } final double[] tempArray = new double[newSize]; // Copy and swap System.arraycopy(internalArray, 0, tempArray, 0, internalArray.length); internalArray = tempArray; } /** * Expands the internal storage array to the specified size. * * @param size Size of the new internal storage array. */ private synchronized void expandTo(int size) { final double[] tempArray = new double[size]; // Copy and swap System.arraycopy(internalArray, 0, tempArray, 0, internalArray.length); internalArray = tempArray; } /** * The contraction criteria defines when the internal array will contract * to store only the number of elements in the element array. * If the expansionMode is MULTIPLICATIVE_MODE, * contraction is triggered when the ratio between storage array length * and numElements exceeds contractionFactor. * If the expansionMode is ADDITIVE_MODE, the * number of excess storage locations is compared to * contractionFactor. * * @return the contraction criteria used to reclaim memory. * @deprecated As of 3.1. Please use {@link #getContractionCriterion()} * instead. */ @Deprecated public float getContractionCriteria() { return (float) getContractionCriterion(); } /** * The contraction criterion defines when the internal array will contract * to store only the number of elements in the element array. * If the expansionMode is MULTIPLICATIVE_MODE, * contraction is triggered when the ratio between storage array length * and numElements exceeds contractionFactor. * If the expansionMode is ADDITIVE_MODE, the * number of excess storage locations is compared to * contractionFactor. * * @return the contraction criterion used to reclaim memory. * @since 3.1 */ public double getContractionCriterion() { return contractionCriterion; } /** * Returns the element at the specified index * * @param index index to fetch a value from * @return value stored at the specified index * @throws ArrayIndexOutOfBoundsException if index is less than * zero or is greater than getNumElements() - 1. */ public synchronized double getElement(int index) { if (index >= numElements) { throw new ArrayIndexOutOfBoundsException(index); } else if (index >= 0) { return internalArray[startIndex + index]; } else { throw new ArrayIndexOutOfBoundsException(index); } } /** * Returns a double array containing the elements of this * ResizableArray. This method returns a copy, not a * reference to the underlying array, so that changes made to the returned * array have no effect on this ResizableArray. * @return the double array. */ public synchronized double[] getElements() { final double[] elementArray = new double[numElements]; System.arraycopy(internalArray, startIndex, elementArray, 0, numElements); return elementArray; } /** * The expansion factor controls the size of a new array when an array * needs to be expanded. The expansionMode * determines whether the size of the array is multiplied by the * expansionFactor (MULTIPLICATIVE_MODE) or if * the expansion is additive (ADDITIVE_MODE -- expansionFactor * storage locations added). The default expansionMode is * MULTIPLICATIVE_MODE and the default expansionFactor * is 2.0. * * @return the expansion factor of this expandable double array * @deprecated As of 3.1. Return type will be changed to "double" in 4.0. */ @Deprecated public float getExpansionFactor() { return (float) expansionFactor; } /** * The expansion mode determines whether the internal storage * array grows additively or multiplicatively when it is expanded. * * @return the expansion mode. * @deprecated As of 3.1. Return value to be changed to * {@link ExpansionMode} in 4.0. */ public int getExpansionMode() { switch (expansionMode) { case MULTIPLICATIVE: return MULTIPLICATIVE_MODE; case ADDITIVE: return ADDITIVE_MODE; default: throw new MathInternalError(); // Should never happen. } } /** * Notice the package scope on this method. This method is simply here * for the JUnit test, it allows us check if the expansion is working * properly after a number of expansions. This is not meant to be a part * of the public interface of this class. * * @return the length of the internal storage array. * @deprecated As of 3.1. Please use {@link #getCapacity()} instead. */ @Deprecated synchronized int getInternalLength() { return internalArray.length; } /** * Gets the currently allocated size of the internal data structure used * for storing elements. * This is not to be confused with {@link #getNumElements() the number of * elements actually stored}. * * @return the length of the internal array. * @since 3.1 */ public int getCapacity() { return internalArray.length; } /** * Returns the number of elements currently in the array. Please note * that this is different from the length of the internal storage array. * * @return the number of elements. */ public synchronized int getNumElements() { return numElements; } /** * Returns the internal storage array. Note that this method returns * a reference to the internal storage array, not a copy, and to correctly * address elements of the array, the startIndex is * required (available via the {@link #start} method). This method should * only be used in cases where copying the internal array is not practical. * The {@link #getElements} method should be used in all other cases. * * * @return the internal storage array used by this object * @since 2.0 * @deprecated As of 3.1. */ @Deprecated public synchronized double[] getInternalValues() { return internalArray; } /** * Provides direct access to the internal storage array. * Please note that this method returns a reference to this object's * storage array, not a copy. *
                              * To correctly address elements of the array, the "start index" is * required (available via the {@link #getStartIndex() getStartIndex} * method. *
                              * This method should only be used to avoid copying the internal array. * The returned value must be used for reading only; other * uses could lead to this object becoming inconsistent. *
                              * The {@link #getElements} method has no such limitation since it * returns a copy of this array's addressable elements. * * @return the internal storage array used by this object. * @since 3.1 */ protected double[] getArrayRef() { return internalArray; } /** * Returns the "start index" of the internal array. * This index is the position of the first addressable element in the * internal storage array. * The addressable elements in the array are at indices contained in * the interval [{@link #getStartIndex()}, * {@link #getStartIndex()} + {@link #getNumElements()} - 1]. * * @return the start index. * @since 3.1 */ protected int getStartIndex() { return startIndex; } /** * Sets the contraction criteria. * * @param contractionCriteria contraction criteria * @throws MathIllegalArgumentException if the contractionCriteria is less than * the expansionCriteria. * @deprecated As of 3.1 (to be removed in 4.0 as field will become "final"). */ @Deprecated public void setContractionCriteria(float contractionCriteria) throws MathIllegalArgumentException { checkContractExpand(contractionCriteria, getExpansionFactor()); synchronized(this) { this.contractionCriterion = contractionCriteria; } } /** * Performs an operation on the addressable elements of the array. * * @param f Function to be applied on this array. * @return the result. * @since 3.1 */ public double compute(MathArrays.Function f) { final double[] array; final int start; final int num; synchronized(this) { array = internalArray; start = startIndex; num = numElements; } return f.evaluate(array, start, num); } /** * Sets the element at the specified index. If the specified index is greater than * getNumElements() - 1, the numElements property * is increased to index +1 and additional storage is allocated * (if necessary) for the new element and all (uninitialized) elements * between the new element and the previous end of the array). * * @param index index to store a value in * @param value value to store at the specified index * @throws ArrayIndexOutOfBoundsException if {@code index < 0}. */ public synchronized void setElement(int index, double value) { if (index < 0) { throw new ArrayIndexOutOfBoundsException(index); } if (index + 1 > numElements) { numElements = index + 1; } if ((startIndex + index) >= internalArray.length) { expandTo(startIndex + (index + 1)); } internalArray[startIndex + index] = value; } /** * Sets the expansionFactor. Throws IllegalArgumentException if the * the following conditions are not met: *
                                *
                              • expansionFactor > 1
                              • *
                              • contractionFactor >= expansionFactor
                              • *
                              * @param expansionFactor the new expansion factor value. * @throws MathIllegalArgumentException if expansionFactor is <= 1 or greater * than contractionFactor * @deprecated As of 3.1 (to be removed in 4.0 as field will become "final"). */ @Deprecated public void setExpansionFactor(float expansionFactor) throws MathIllegalArgumentException { checkContractExpand(getContractionCriterion(), expansionFactor); // The check above verifies that the expansion factor is > 1.0; synchronized(this) { this.expansionFactor = expansionFactor; } } /** * Sets the expansionMode. The specified value must be one of * ADDITIVE_MODE, MULTIPLICATIVE_MODE. * * @param expansionMode The expansionMode to set. * @throws MathIllegalArgumentException if the specified mode value is not valid. * @deprecated As of 3.1. Please use {@link #setExpansionMode(ExpansionMode)} instead. */ @Deprecated public void setExpansionMode(int expansionMode) throws MathIllegalArgumentException { if (expansionMode != MULTIPLICATIVE_MODE && expansionMode != ADDITIVE_MODE) { throw new MathIllegalArgumentException(LocalizedFormats.UNSUPPORTED_EXPANSION_MODE, expansionMode, MULTIPLICATIVE_MODE, "MULTIPLICATIVE_MODE", ADDITIVE_MODE, "ADDITIVE_MODE"); } synchronized(this) { if (expansionMode == MULTIPLICATIVE_MODE) { setExpansionMode(ExpansionMode.MULTIPLICATIVE); } else if (expansionMode == ADDITIVE_MODE) { setExpansionMode(ExpansionMode.ADDITIVE); } } } /** * Sets the {@link ExpansionMode expansion mode}. * * @param expansionMode Expansion mode to use for resizing the array. * @deprecated As of 3.1 (to be removed in 4.0 as field will become "final"). */ @Deprecated public void setExpansionMode(ExpansionMode expansionMode) { this.expansionMode = expansionMode; } /** * Sets the initial capacity. Should only be invoked by constructors. * * @param initialCapacity of the array * @throws MathIllegalArgumentException if initialCapacity is not * positive. * @deprecated As of 3.1, this is a no-op. */ @Deprecated protected void setInitialCapacity(int initialCapacity) throws MathIllegalArgumentException { // Body removed in 3.1. } /** * This function allows you to control the number of elements contained * in this array, and can be used to "throw out" the last n values in an * array. This function will also expand the internal array as needed. * * @param i a new number of elements * @throws MathIllegalArgumentException if i is negative. */ public synchronized void setNumElements(int i) throws MathIllegalArgumentException { // If index is negative thrown an error. if (i < 0) { throw new MathIllegalArgumentException( LocalizedFormats.INDEX_NOT_POSITIVE, i); } // Test the new num elements, check to see if the array needs to be // expanded to accommodate this new number of elements. final int newSize = startIndex + i; if (newSize > internalArray.length) { expandTo(newSize); } // Set the new number of elements to new value. numElements = i; } /** * Returns true if the internal storage array has too many unused * storage positions. * * @return true if array satisfies the contraction criteria */ private synchronized boolean shouldContract() { if (expansionMode == ExpansionMode.MULTIPLICATIVE) { return (internalArray.length / ((float) numElements)) > contractionCriterion; } else { return (internalArray.length - numElements) > contractionCriterion; } } /** * Returns the starting index of the internal array. The starting index is * the position of the first addressable element in the internal storage * array. The addressable elements in the array are * internalArray[startIndex],...,internalArray[startIndex + numElements -1] * * * @return the starting index. * @deprecated As of 3.1. */ @Deprecated public synchronized int start() { return startIndex; } /** *

                              Copies source to dest, copying the underlying data, so dest is * a new, independent copy of source. Does not contract before * the copy.

                              * *

                              Obtains synchronization locks on both source and dest * (in that order) before performing the copy.

                              * *

                              Neither source nor dest may be null; otherwise a {@link NullArgumentException} * is thrown

                              * * @param source ResizableDoubleArray to copy * @param dest ResizableArray to replace with a copy of the source array * @exception NullArgumentException if either source or dest is null * @since 2.0 * */ public static void copy(ResizableDoubleArray source, ResizableDoubleArray dest) throws NullArgumentException { MathUtils.checkNotNull(source); MathUtils.checkNotNull(dest); synchronized(source) { synchronized(dest) { dest.contractionCriterion = source.contractionCriterion; dest.expansionFactor = source.expansionFactor; dest.expansionMode = source.expansionMode; dest.internalArray = new double[source.internalArray.length]; System.arraycopy(source.internalArray, 0, dest.internalArray, 0, dest.internalArray.length); dest.numElements = source.numElements; dest.startIndex = source.startIndex; } } } /** * Returns a copy of the ResizableDoubleArray. Does not contract before * the copy, so the returned object is an exact copy of this. * * @return a new ResizableDoubleArray with the same data and configuration * properties as this * @since 2.0 */ public synchronized ResizableDoubleArray copy() { final ResizableDoubleArray result = new ResizableDoubleArray(); copy(this, result); return result; } /** * Returns true iff object is a ResizableDoubleArray with the same properties * as this and an identical internal storage array. * * @param object object to be compared for equality with this * @return true iff object is a ResizableDoubleArray with the same data and * properties as this * @since 2.0 */ @Override public boolean equals(Object object) { if (object == this ) { return true; } if (object instanceof ResizableDoubleArray == false) { return false; } synchronized(this) { synchronized(object) { boolean result = true; final ResizableDoubleArray other = (ResizableDoubleArray) object; result = result && (other.contractionCriterion == contractionCriterion); result = result && (other.expansionFactor == expansionFactor); result = result && (other.expansionMode == expansionMode); result = result && (other.numElements == numElements); result = result && (other.startIndex == startIndex); if (!result) { return false; } else { return Arrays.equals(internalArray, other.internalArray); } } } } /** * Returns a hash code consistent with equals. * * @return the hash code representing this {@code ResizableDoubleArray}. * @since 2.0 */ @Override public synchronized int hashCode() { final int[] hashData = new int[6]; hashData[0] = Double.valueOf(expansionFactor).hashCode(); hashData[1] = Double.valueOf(contractionCriterion).hashCode(); hashData[2] = expansionMode.hashCode(); hashData[3] = Arrays.hashCode(internalArray); hashData[4] = numElements; hashData[5] = startIndex; return Arrays.hashCode(hashData); } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/util/ContinuedFraction.java100644 1750 1750 15056 12126627720 30006 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.util; import org.apache.commons.math3.exception.ConvergenceException; import org.apache.commons.math3.exception.MaxCountExceededException; import org.apache.commons.math3.exception.util.LocalizedFormats; /** * Provides a generic means to evaluate continued fractions. Subclasses simply * provided the a and b coefficients to evaluate the continued fraction. * *

                              * References: *

                              *

                              * * @version $Id: ContinuedFraction.java 1416643 2012-12-03 19:37:14Z tn $ */ public abstract class ContinuedFraction { /** Maximum allowed numerical error. */ private static final double DEFAULT_EPSILON = 10e-9; /** * Default constructor. */ protected ContinuedFraction() { super(); } /** * Access the n-th a coefficient of the continued fraction. Since a can be * a function of the evaluation point, x, that is passed in as well. * @param n the coefficient index to retrieve. * @param x the evaluation point. * @return the n-th a coefficient. */ protected abstract double getA(int n, double x); /** * Access the n-th b coefficient of the continued fraction. Since b can be * a function of the evaluation point, x, that is passed in as well. * @param n the coefficient index to retrieve. * @param x the evaluation point. * @return the n-th b coefficient. */ protected abstract double getB(int n, double x); /** * Evaluates the continued fraction at the value x. * @param x the evaluation point. * @return the value of the continued fraction evaluated at x. * @throws ConvergenceException if the algorithm fails to converge. */ public double evaluate(double x) throws ConvergenceException { return evaluate(x, DEFAULT_EPSILON, Integer.MAX_VALUE); } /** * Evaluates the continued fraction at the value x. * @param x the evaluation point. * @param epsilon maximum error allowed. * @return the value of the continued fraction evaluated at x. * @throws ConvergenceException if the algorithm fails to converge. */ public double evaluate(double x, double epsilon) throws ConvergenceException { return evaluate(x, epsilon, Integer.MAX_VALUE); } /** * Evaluates the continued fraction at the value x. * @param x the evaluation point. * @param maxIterations maximum number of convergents * @return the value of the continued fraction evaluated at x. * @throws ConvergenceException if the algorithm fails to converge. * @throws MaxCountExceededException if maximal number of iterations is reached */ public double evaluate(double x, int maxIterations) throws ConvergenceException, MaxCountExceededException { return evaluate(x, DEFAULT_EPSILON, maxIterations); } /** * Evaluates the continued fraction at the value x. *

                              * The implementation of this method is based on the modified Lentz algorithm as described * on page 18 ff. in: *

                              * Note: the implementation uses the terms ai and bi as defined in * Continued Fraction @ MathWorld. *

                              * * @param x the evaluation point. * @param epsilon maximum error allowed. * @param maxIterations maximum number of convergents * @return the value of the continued fraction evaluated at x. * @throws ConvergenceException if the algorithm fails to converge. * @throws MaxCountExceededException if maximal number of iterations is reached */ public double evaluate(double x, double epsilon, int maxIterations) throws ConvergenceException, MaxCountExceededException { final double small = 1e-50; double hPrev = getA(0, x); // use the value of small as epsilon criteria for zero checks if (Precision.equals(hPrev, 0.0, small)) { hPrev = small; } int n = 1; double dPrev = 0.0; double cPrev = hPrev; double hN = hPrev; while (n < maxIterations) { final double a = getA(n, x); final double b = getB(n, x); double dN = a + b * dPrev; if (Precision.equals(dN, 0.0, small)) { dN = small; } double cN = a + b / cPrev; if (Precision.equals(cN, 0.0, small)) { cN = small; } dN = 1 / dN; final double deltaN = cN * dN; hN = hPrev * deltaN; if (Double.isInfinite(hN)) { throw new ConvergenceException(LocalizedFormats.CONTINUED_FRACTION_INFINITY_DIVERGENCE, x); } if (Double.isNaN(hN)) { throw new ConvergenceException(LocalizedFormats.CONTINUED_FRACTION_NAN_DIVERGENCE, x); } if (FastMath.abs(deltaN - 1.0) < epsilon) { break; } dPrev = dN; cPrev = cN; hPrev = hN; n++; } if (n >= maxIterations) { throw new MaxCountExceededException(LocalizedFormats.NON_CONVERGENT_CONTINUED_FRACTION, maxIterations, x); } return hN; } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/util/package-info.java100644 1750 1750 1657 12126627720 26676 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ /** * Convenience routines and common data structures used throughout the commons-math library. */ package org.apache.commons.math3.util; commons-math3-3.2-src/src/main/java/org/apache/commons/math3/util/FastMathCalc.java100644 1750 1750 52527 12126627720 26666 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.util; import java.io.PrintStream; import org.apache.commons.math3.exception.DimensionMismatchException; /** Class used to compute the classical functions tables. * @version $Id: FastMathCalc.java 1416643 2012-12-03 19:37:14Z tn $ * @since 3.0 */ class FastMathCalc { /** * 0x40000000 - used to split a double into two parts, both with the low order bits cleared. * Equivalent to 2^30. */ private static final long HEX_40000000 = 0x40000000L; // 1073741824L /** Factorial table, for Taylor series expansions. 0!, 1!, 2!, ... 19! */ private static final double FACT[] = new double[] { +1.0d, // 0 +1.0d, // 1 +2.0d, // 2 +6.0d, // 3 +24.0d, // 4 +120.0d, // 5 +720.0d, // 6 +5040.0d, // 7 +40320.0d, // 8 +362880.0d, // 9 +3628800.0d, // 10 +39916800.0d, // 11 +479001600.0d, // 12 +6227020800.0d, // 13 +87178291200.0d, // 14 +1307674368000.0d, // 15 +20922789888000.0d, // 16 +355687428096000.0d, // 17 +6402373705728000.0d, // 18 +121645100408832000.0d, // 19 }; /** Coefficients for slowLog. */ private static final double LN_SPLIT_COEF[][] = { {2.0, 0.0}, {0.6666666269302368, 3.9736429850260626E-8}, {0.3999999761581421, 2.3841857910019882E-8}, {0.2857142686843872, 1.7029898543501842E-8}, {0.2222222089767456, 1.3245471311735498E-8}, {0.1818181574344635, 2.4384203044354907E-8}, {0.1538461446762085, 9.140260083262505E-9}, {0.13333332538604736, 9.220590270857665E-9}, {0.11764700710773468, 1.2393345855018391E-8}, {0.10526403784751892, 8.251545029714408E-9}, {0.0952233225107193, 1.2675934823758863E-8}, {0.08713622391223907, 1.1430250008909141E-8}, {0.07842259109020233, 2.404307984052299E-9}, {0.08371849358081818, 1.176342548272881E-8}, {0.030589580535888672, 1.2958646899018938E-9}, {0.14982303977012634, 1.225743062930824E-8}, }; /** Table start declaration. */ private static final String TABLE_START_DECL = " {"; /** Table end declaration. */ private static final String TABLE_END_DECL = " };"; /** * Private Constructor. */ private FastMathCalc() { } /** Build the sine and cosine tables. * @param SINE_TABLE_A table of the most significant part of the sines * @param SINE_TABLE_B table of the least significant part of the sines * @param COSINE_TABLE_A table of the most significant part of the cosines * @param COSINE_TABLE_B table of the most significant part of the cosines * @param SINE_TABLE_LEN length of the tables * @param TANGENT_TABLE_A table of the most significant part of the tangents * @param TANGENT_TABLE_B table of the most significant part of the tangents */ @SuppressWarnings("unused") private static void buildSinCosTables(double[] SINE_TABLE_A, double[] SINE_TABLE_B, double[] COSINE_TABLE_A, double[] COSINE_TABLE_B, int SINE_TABLE_LEN, double[] TANGENT_TABLE_A, double[] TANGENT_TABLE_B) { final double result[] = new double[2]; /* Use taylor series for 0 <= x <= 6/8 */ for (int i = 0; i < 7; i++) { double x = i / 8.0; slowSin(x, result); SINE_TABLE_A[i] = result[0]; SINE_TABLE_B[i] = result[1]; slowCos(x, result); COSINE_TABLE_A[i] = result[0]; COSINE_TABLE_B[i] = result[1]; } /* Use angle addition formula to complete table to 13/8, just beyond pi/2 */ for (int i = 7; i < SINE_TABLE_LEN; i++) { double xs[] = new double[2]; double ys[] = new double[2]; double as[] = new double[2]; double bs[] = new double[2]; double temps[] = new double[2]; if ( (i & 1) == 0) { // Even, use double angle xs[0] = SINE_TABLE_A[i/2]; xs[1] = SINE_TABLE_B[i/2]; ys[0] = COSINE_TABLE_A[i/2]; ys[1] = COSINE_TABLE_B[i/2]; /* compute sine */ splitMult(xs, ys, result); SINE_TABLE_A[i] = result[0] * 2.0; SINE_TABLE_B[i] = result[1] * 2.0; /* Compute cosine */ splitMult(ys, ys, as); splitMult(xs, xs, temps); temps[0] = -temps[0]; temps[1] = -temps[1]; splitAdd(as, temps, result); COSINE_TABLE_A[i] = result[0]; COSINE_TABLE_B[i] = result[1]; } else { xs[0] = SINE_TABLE_A[i/2]; xs[1] = SINE_TABLE_B[i/2]; ys[0] = COSINE_TABLE_A[i/2]; ys[1] = COSINE_TABLE_B[i/2]; as[0] = SINE_TABLE_A[i/2+1]; as[1] = SINE_TABLE_B[i/2+1]; bs[0] = COSINE_TABLE_A[i/2+1]; bs[1] = COSINE_TABLE_B[i/2+1]; /* compute sine */ splitMult(xs, bs, temps); splitMult(ys, as, result); splitAdd(result, temps, result); SINE_TABLE_A[i] = result[0]; SINE_TABLE_B[i] = result[1]; /* Compute cosine */ splitMult(ys, bs, result); splitMult(xs, as, temps); temps[0] = -temps[0]; temps[1] = -temps[1]; splitAdd(result, temps, result); COSINE_TABLE_A[i] = result[0]; COSINE_TABLE_B[i] = result[1]; } } /* Compute tangent = sine/cosine */ for (int i = 0; i < SINE_TABLE_LEN; i++) { double xs[] = new double[2]; double ys[] = new double[2]; double as[] = new double[2]; as[0] = COSINE_TABLE_A[i]; as[1] = COSINE_TABLE_B[i]; splitReciprocal(as, ys); xs[0] = SINE_TABLE_A[i]; xs[1] = SINE_TABLE_B[i]; splitMult(xs, ys, as); TANGENT_TABLE_A[i] = as[0]; TANGENT_TABLE_B[i] = as[1]; } } /** * For x between 0 and pi/4 compute cosine using Talor series * cos(x) = 1 - x^2/2! + x^4/4! ... * @param x number from which cosine is requested * @param result placeholder where to put the result in extended precision * (may be null) * @return cos(x) */ static double slowCos(final double x, final double result[]) { final double xs[] = new double[2]; final double ys[] = new double[2]; final double facts[] = new double[2]; final double as[] = new double[2]; split(x, xs); ys[0] = ys[1] = 0.0; for (int i = FACT.length-1; i >= 0; i--) { splitMult(xs, ys, as); ys[0] = as[0]; ys[1] = as[1]; if ( (i & 1) != 0) { // skip odd entries continue; } split(FACT[i], as); splitReciprocal(as, facts); if ( (i & 2) != 0 ) { // alternate terms are negative facts[0] = -facts[0]; facts[1] = -facts[1]; } splitAdd(ys, facts, as); ys[0] = as[0]; ys[1] = as[1]; } if (result != null) { result[0] = ys[0]; result[1] = ys[1]; } return ys[0] + ys[1]; } /** * For x between 0 and pi/4 compute sine using Taylor expansion: * sin(x) = x - x^3/3! + x^5/5! - x^7/7! ... * @param x number from which sine is requested * @param result placeholder where to put the result in extended precision * (may be null) * @return sin(x) */ static double slowSin(final double x, final double result[]) { final double xs[] = new double[2]; final double ys[] = new double[2]; final double facts[] = new double[2]; final double as[] = new double[2]; split(x, xs); ys[0] = ys[1] = 0.0; for (int i = FACT.length-1; i >= 0; i--) { splitMult(xs, ys, as); ys[0] = as[0]; ys[1] = as[1]; if ( (i & 1) == 0) { // Ignore even numbers continue; } split(FACT[i], as); splitReciprocal(as, facts); if ( (i & 2) != 0 ) { // alternate terms are negative facts[0] = -facts[0]; facts[1] = -facts[1]; } splitAdd(ys, facts, as); ys[0] = as[0]; ys[1] = as[1]; } if (result != null) { result[0] = ys[0]; result[1] = ys[1]; } return ys[0] + ys[1]; } /** * For x between 0 and 1, returns exp(x), uses extended precision * @param x argument of exponential * @param result placeholder where to place exp(x) split in two terms * for extra precision (i.e. exp(x) = result[0] + result[1] * @return exp(x) */ static double slowexp(final double x, final double result[]) { final double xs[] = new double[2]; final double ys[] = new double[2]; final double facts[] = new double[2]; final double as[] = new double[2]; split(x, xs); ys[0] = ys[1] = 0.0; for (int i = FACT.length-1; i >= 0; i--) { splitMult(xs, ys, as); ys[0] = as[0]; ys[1] = as[1]; split(FACT[i], as); splitReciprocal(as, facts); splitAdd(ys, facts, as); ys[0] = as[0]; ys[1] = as[1]; } if (result != null) { result[0] = ys[0]; result[1] = ys[1]; } return ys[0] + ys[1]; } /** Compute split[0], split[1] such that their sum is equal to d, * and split[0] has its 30 least significant bits as zero. * @param d number to split * @param split placeholder where to place the result */ private static void split(final double d, final double split[]) { if (d < 8e298 && d > -8e298) { final double a = d * HEX_40000000; split[0] = (d + a) - a; split[1] = d - split[0]; } else { final double a = d * 9.31322574615478515625E-10; split[0] = (d + a - d) * HEX_40000000; split[1] = d - split[0]; } } /** Recompute a split. * @param a input/out array containing the split, changed * on output */ private static void resplit(final double a[]) { final double c = a[0] + a[1]; final double d = -(c - a[0] - a[1]); if (c < 8e298 && c > -8e298) { // MAGIC NUMBER double z = c * HEX_40000000; a[0] = (c + z) - z; a[1] = c - a[0] + d; } else { double z = c * 9.31322574615478515625E-10; a[0] = (c + z - c) * HEX_40000000; a[1] = c - a[0] + d; } } /** Multiply two numbers in split form. * @param a first term of multiplication * @param b second term of multiplication * @param ans placeholder where to put the result */ private static void splitMult(double a[], double b[], double ans[]) { ans[0] = a[0] * b[0]; ans[1] = a[0] * b[1] + a[1] * b[0] + a[1] * b[1]; /* Resplit */ resplit(ans); } /** Add two numbers in split form. * @param a first term of addition * @param b second term of addition * @param ans placeholder where to put the result */ private static void splitAdd(final double a[], final double b[], final double ans[]) { ans[0] = a[0] + b[0]; ans[1] = a[1] + b[1]; resplit(ans); } /** Compute the reciprocal of in. Use the following algorithm. * in = c + d. * want to find x + y such that x+y = 1/(c+d) and x is much * larger than y and x has several zero bits on the right. * * Set b = 1/(2^22), a = 1 - b. Thus (a+b) = 1. * Use following identity to compute (a+b)/(c+d) * * (a+b)/(c+d) = a/c + (bc - ad) / (c^2 + cd) * set x = a/c and y = (bc - ad) / (c^2 + cd) * This will be close to the right answer, but there will be * some rounding in the calculation of X. So by carefully * computing 1 - (c+d)(x+y) we can compute an error and * add that back in. This is done carefully so that terms * of similar size are subtracted first. * @param in initial number, in split form * @param result placeholder where to put the result */ static void splitReciprocal(final double in[], final double result[]) { final double b = 1.0/4194304.0; final double a = 1.0 - b; if (in[0] == 0.0) { in[0] = in[1]; in[1] = 0.0; } result[0] = a / in[0]; result[1] = (b*in[0]-a*in[1]) / (in[0]*in[0] + in[0]*in[1]); if (result[1] != result[1]) { // can happen if result[1] is NAN result[1] = 0.0; } /* Resplit */ resplit(result); for (int i = 0; i < 2; i++) { /* this may be overkill, probably once is enough */ double err = 1.0 - result[0] * in[0] - result[0] * in[1] - result[1] * in[0] - result[1] * in[1]; /*err = 1.0 - err; */ err = err * (result[0] + result[1]); /*printf("err = %16e\n", err); */ result[1] += err; } } /** Compute (a[0] + a[1]) * (b[0] + b[1]) in extended precision. * @param a first term of the multiplication * @param b second term of the multiplication * @param result placeholder where to put the result */ private static void quadMult(final double a[], final double b[], final double result[]) { final double xs[] = new double[2]; final double ys[] = new double[2]; final double zs[] = new double[2]; /* a[0] * b[0] */ split(a[0], xs); split(b[0], ys); splitMult(xs, ys, zs); result[0] = zs[0]; result[1] = zs[1]; /* a[0] * b[1] */ split(b[1], ys); splitMult(xs, ys, zs); double tmp = result[0] + zs[0]; result[1] = result[1] - (tmp - result[0] - zs[0]); result[0] = tmp; tmp = result[0] + zs[1]; result[1] = result[1] - (tmp - result[0] - zs[1]); result[0] = tmp; /* a[1] * b[0] */ split(a[1], xs); split(b[0], ys); splitMult(xs, ys, zs); tmp = result[0] + zs[0]; result[1] = result[1] - (tmp - result[0] - zs[0]); result[0] = tmp; tmp = result[0] + zs[1]; result[1] = result[1] - (tmp - result[0] - zs[1]); result[0] = tmp; /* a[1] * b[0] */ split(a[1], xs); split(b[1], ys); splitMult(xs, ys, zs); tmp = result[0] + zs[0]; result[1] = result[1] - (tmp - result[0] - zs[0]); result[0] = tmp; tmp = result[0] + zs[1]; result[1] = result[1] - (tmp - result[0] - zs[1]); result[0] = tmp; } /** Compute exp(p) for a integer p in extended precision. * @param p integer whose exponential is requested * @param result placeholder where to put the result in extended precision * @return exp(p) in standard precision (equal to result[0] + result[1]) */ static double expint(int p, final double result[]) { //double x = M_E; final double xs[] = new double[2]; final double as[] = new double[2]; final double ys[] = new double[2]; //split(x, xs); //xs[1] = (double)(2.7182818284590452353602874713526625L - xs[0]); //xs[0] = 2.71827697753906250000; //xs[1] = 4.85091998273542816811e-06; //xs[0] = Double.longBitsToDouble(0x4005bf0800000000L); //xs[1] = Double.longBitsToDouble(0x3ed458a2bb4a9b00L); /* E */ xs[0] = 2.718281828459045; xs[1] = 1.4456468917292502E-16; split(1.0, ys); while (p > 0) { if ((p & 1) != 0) { quadMult(ys, xs, as); ys[0] = as[0]; ys[1] = as[1]; } quadMult(xs, xs, as); xs[0] = as[0]; xs[1] = as[1]; p >>= 1; } if (result != null) { result[0] = ys[0]; result[1] = ys[1]; resplit(result); } return ys[0] + ys[1]; } /** xi in the range of [1, 2]. * 3 5 7 * x+1 / x x x \ * ln ----- = 2 * | x + ---- + ---- + ---- + ... | * 1-x \ 3 5 7 / * * So, compute a Remez approximation of the following function * * ln ((sqrt(x)+1)/(1-sqrt(x))) / x * * This will be an even function with only positive coefficents. * x is in the range [0 - 1/3]. * * Transform xi for input to the above function by setting * x = (xi-1)/(xi+1). Input to the polynomial is x^2, then * the result is multiplied by x. * @param xi number from which log is requested * @return log(xi) */ static double[] slowLog(double xi) { double x[] = new double[2]; double x2[] = new double[2]; double y[] = new double[2]; double a[] = new double[2]; split(xi, x); /* Set X = (x-1)/(x+1) */ x[0] += 1.0; resplit(x); splitReciprocal(x, a); x[0] -= 2.0; resplit(x); splitMult(x, a, y); x[0] = y[0]; x[1] = y[1]; /* Square X -> X2*/ splitMult(x, x, x2); //x[0] -= 1.0; //resplit(x); y[0] = LN_SPLIT_COEF[LN_SPLIT_COEF.length-1][0]; y[1] = LN_SPLIT_COEF[LN_SPLIT_COEF.length-1][1]; for (int i = LN_SPLIT_COEF.length-2; i >= 0; i--) { splitMult(y, x2, a); y[0] = a[0]; y[1] = a[1]; splitAdd(y, LN_SPLIT_COEF[i], a); y[0] = a[0]; y[1] = a[1]; } splitMult(y, x, a); y[0] = a[0]; y[1] = a[1]; return y; } /** * Print an array. * @param out text output stream where output should be printed * @param name array name * @param expectedLen expected length of the array * @param array2d array data */ static void printarray(PrintStream out, String name, int expectedLen, double[][] array2d) { out.println(name); checkLen(expectedLen, array2d.length); out.println(TABLE_START_DECL + " "); int i = 0; for(double[] array : array2d) { // "double array[]" causes PMD parsing error out.print(" {"); for(double d : array) { // assume inner array has very few entries out.printf("%-25.25s", format(d)); // multiple entries per line } out.println("}, // " + i++); } out.println(TABLE_END_DECL); } /** * Print an array. * @param out text output stream where output should be printed * @param name array name * @param expectedLen expected length of the array * @param array array data */ static void printarray(PrintStream out, String name, int expectedLen, double[] array) { out.println(name + "="); checkLen(expectedLen, array.length); out.println(TABLE_START_DECL); for(double d : array){ out.printf(" %s%n", format(d)); // one entry per line } out.println(TABLE_END_DECL); } /** Format a double. * @param d double number to format * @return formatted number */ static String format(double d) { if (d != d) { return "Double.NaN,"; } else { return ((d >= 0) ? "+" : "") + Double.toString(d) + "d,"; } } /** * Check two lengths are equal. * @param expectedLen expected length * @param actual actual length * @exception DimensionMismatchException if the two lengths are not equal */ private static void checkLen(int expectedLen, int actual) throws DimensionMismatchException { if (expectedLen != actual) { throw new DimensionMismatchException(actual, expectedLen); } } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/util/OpenIntToDoubleHashMap.java100644 1750 1750 43175 12126627720 30647 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.util; import java.io.IOException; import java.io.ObjectInputStream; import java.io.Serializable; import java.util.ConcurrentModificationException; import java.util.NoSuchElementException; /** * Open addressed map from int to double. *

                              This class provides a dedicated map from integers to doubles with a * much smaller memory overhead than standard java.util.Map.

                              *

                              This class is not synchronized. The specialized iterators returned by * {@link #iterator()} are fail-fast: they throw a * ConcurrentModificationException when they detect the map has been * modified during iteration.

                              * @version $Id: OpenIntToDoubleHashMap.java 1421448 2012-12-13 19:45:57Z tn $ * @since 2.0 */ public class OpenIntToDoubleHashMap implements Serializable { /** Status indicator for free table entries. */ protected static final byte FREE = 0; /** Status indicator for full table entries. */ protected static final byte FULL = 1; /** Status indicator for removed table entries. */ protected static final byte REMOVED = 2; /** Serializable version identifier */ private static final long serialVersionUID = -3646337053166149105L; /** Load factor for the map. */ private static final float LOAD_FACTOR = 0.5f; /** Default starting size. *

                              This must be a power of two for bit mask to work properly.

                              */ private static final int DEFAULT_EXPECTED_SIZE = 16; /** Multiplier for size growth when map fills up. *

                              This must be a power of two for bit mask to work properly.

                              */ private static final int RESIZE_MULTIPLIER = 2; /** Number of bits to perturb the index when probing for collision resolution. */ private static final int PERTURB_SHIFT = 5; /** Keys table. */ private int[] keys; /** Values table. */ private double[] values; /** States table. */ private byte[] states; /** Return value for missing entries. */ private final double missingEntries; /** Current size of the map. */ private int size; /** Bit mask for hash values. */ private int mask; /** Modifications count. */ private transient int count; /** * Build an empty map with default size and using NaN for missing entries. */ public OpenIntToDoubleHashMap() { this(DEFAULT_EXPECTED_SIZE, Double.NaN); } /** * Build an empty map with default size * @param missingEntries value to return when a missing entry is fetched */ public OpenIntToDoubleHashMap(final double missingEntries) { this(DEFAULT_EXPECTED_SIZE, missingEntries); } /** * Build an empty map with specified size and using NaN for missing entries. * @param expectedSize expected number of elements in the map */ public OpenIntToDoubleHashMap(final int expectedSize) { this(expectedSize, Double.NaN); } /** * Build an empty map with specified size. * @param expectedSize expected number of elements in the map * @param missingEntries value to return when a missing entry is fetched */ public OpenIntToDoubleHashMap(final int expectedSize, final double missingEntries) { final int capacity = computeCapacity(expectedSize); keys = new int[capacity]; values = new double[capacity]; states = new byte[capacity]; this.missingEntries = missingEntries; mask = capacity - 1; } /** * Copy constructor. * @param source map to copy */ public OpenIntToDoubleHashMap(final OpenIntToDoubleHashMap source) { final int length = source.keys.length; keys = new int[length]; System.arraycopy(source.keys, 0, keys, 0, length); values = new double[length]; System.arraycopy(source.values, 0, values, 0, length); states = new byte[length]; System.arraycopy(source.states, 0, states, 0, length); missingEntries = source.missingEntries; size = source.size; mask = source.mask; count = source.count; } /** * Compute the capacity needed for a given size. * @param expectedSize expected size of the map * @return capacity to use for the specified size */ private static int computeCapacity(final int expectedSize) { if (expectedSize == 0) { return 1; } final int capacity = (int) FastMath.ceil(expectedSize / LOAD_FACTOR); final int powerOfTwo = Integer.highestOneBit(capacity); if (powerOfTwo == capacity) { return capacity; } return nextPowerOfTwo(capacity); } /** * Find the smallest power of two greater than the input value * @param i input value * @return smallest power of two greater than the input value */ private static int nextPowerOfTwo(final int i) { return Integer.highestOneBit(i) << 1; } /** * Get the stored value associated with the given key * @param key key associated with the data * @return data associated with the key */ public double get(final int key) { final int hash = hashOf(key); int index = hash & mask; if (containsKey(key, index)) { return values[index]; } if (states[index] == FREE) { return missingEntries; } int j = index; for (int perturb = perturb(hash); states[index] != FREE; perturb >>= PERTURB_SHIFT) { j = probe(perturb, j); index = j & mask; if (containsKey(key, index)) { return values[index]; } } return missingEntries; } /** * Check if a value is associated with a key. * @param key key to check * @return true if a value is associated with key */ public boolean containsKey(final int key) { final int hash = hashOf(key); int index = hash & mask; if (containsKey(key, index)) { return true; } if (states[index] == FREE) { return false; } int j = index; for (int perturb = perturb(hash); states[index] != FREE; perturb >>= PERTURB_SHIFT) { j = probe(perturb, j); index = j & mask; if (containsKey(key, index)) { return true; } } return false; } /** * Get an iterator over map elements. *

                              The specialized iterators returned are fail-fast: they throw a * ConcurrentModificationException when they detect the map * has been modified during iteration.

                              * @return iterator over the map elements */ public Iterator iterator() { return new Iterator(); } /** * Perturb the hash for starting probing. * @param hash initial hash * @return perturbed hash */ private static int perturb(final int hash) { return hash & 0x7fffffff; } /** * Find the index at which a key should be inserted * @param key key to lookup * @return index at which key should be inserted */ private int findInsertionIndex(final int key) { return findInsertionIndex(keys, states, key, mask); } /** * Find the index at which a key should be inserted * @param keys keys table * @param states states table * @param key key to lookup * @param mask bit mask for hash values * @return index at which key should be inserted */ private static int findInsertionIndex(final int[] keys, final byte[] states, final int key, final int mask) { final int hash = hashOf(key); int index = hash & mask; if (states[index] == FREE) { return index; } else if (states[index] == FULL && keys[index] == key) { return changeIndexSign(index); } int perturb = perturb(hash); int j = index; if (states[index] == FULL) { while (true) { j = probe(perturb, j); index = j & mask; perturb >>= PERTURB_SHIFT; if (states[index] != FULL || keys[index] == key) { break; } } } if (states[index] == FREE) { return index; } else if (states[index] == FULL) { // due to the loop exit condition, // if (states[index] == FULL) then keys[index] == key return changeIndexSign(index); } final int firstRemoved = index; while (true) { j = probe(perturb, j); index = j & mask; if (states[index] == FREE) { return firstRemoved; } else if (states[index] == FULL && keys[index] == key) { return changeIndexSign(index); } perturb >>= PERTURB_SHIFT; } } /** * Compute next probe for collision resolution * @param perturb perturbed hash * @param j previous probe * @return next probe */ private static int probe(final int perturb, final int j) { return (j << 2) + j + perturb + 1; } /** * Change the index sign * @param index initial index * @return changed index */ private static int changeIndexSign(final int index) { return -index - 1; } /** * Get the number of elements stored in the map. * @return number of elements stored in the map */ public int size() { return size; } /** * Remove the value associated with a key. * @param key key to which the value is associated * @return removed value */ public double remove(final int key) { final int hash = hashOf(key); int index = hash & mask; if (containsKey(key, index)) { return doRemove(index); } if (states[index] == FREE) { return missingEntries; } int j = index; for (int perturb = perturb(hash); states[index] != FREE; perturb >>= PERTURB_SHIFT) { j = probe(perturb, j); index = j & mask; if (containsKey(key, index)) { return doRemove(index); } } return missingEntries; } /** * Check if the tables contain an element associated with specified key * at specified index. * @param key key to check * @param index index to check * @return true if an element is associated with key at index */ private boolean containsKey(final int key, final int index) { return (key != 0 || states[index] == FULL) && keys[index] == key; } /** * Remove an element at specified index. * @param index index of the element to remove * @return removed value */ private double doRemove(int index) { keys[index] = 0; states[index] = REMOVED; final double previous = values[index]; values[index] = missingEntries; --size; ++count; return previous; } /** * Put a value associated with a key in the map. * @param key key to which value is associated * @param value value to put in the map * @return previous value associated with the key */ public double put(final int key, final double value) { int index = findInsertionIndex(key); double previous = missingEntries; boolean newMapping = true; if (index < 0) { index = changeIndexSign(index); previous = values[index]; newMapping = false; } keys[index] = key; states[index] = FULL; values[index] = value; if (newMapping) { ++size; if (shouldGrowTable()) { growTable(); } ++count; } return previous; } /** * Grow the tables. */ private void growTable() { final int oldLength = states.length; final int[] oldKeys = keys; final double[] oldValues = values; final byte[] oldStates = states; final int newLength = RESIZE_MULTIPLIER * oldLength; final int[] newKeys = new int[newLength]; final double[] newValues = new double[newLength]; final byte[] newStates = new byte[newLength]; final int newMask = newLength - 1; for (int i = 0; i < oldLength; ++i) { if (oldStates[i] == FULL) { final int key = oldKeys[i]; final int index = findInsertionIndex(newKeys, newStates, key, newMask); newKeys[index] = key; newValues[index] = oldValues[i]; newStates[index] = FULL; } } mask = newMask; keys = newKeys; values = newValues; states = newStates; } /** * Check if tables should grow due to increased size. * @return true if tables should grow */ private boolean shouldGrowTable() { return size > (mask + 1) * LOAD_FACTOR; } /** * Compute the hash value of a key * @param key key to hash * @return hash value of the key */ private static int hashOf(final int key) { final int h = key ^ ((key >>> 20) ^ (key >>> 12)); return h ^ (h >>> 7) ^ (h >>> 4); } /** Iterator class for the map. */ public class Iterator { /** Reference modification count. */ private final int referenceCount; /** Index of current element. */ private int current; /** Index of next element. */ private int next; /** * Simple constructor. */ private Iterator() { // preserve the modification count of the map to detect concurrent modifications later referenceCount = count; // initialize current index next = -1; try { advance(); } catch (NoSuchElementException nsee) { // NOPMD // ignored } } /** * Check if there is a next element in the map. * @return true if there is a next element */ public boolean hasNext() { return next >= 0; } /** * Get the key of current entry. * @return key of current entry * @exception ConcurrentModificationException if the map is modified during iteration * @exception NoSuchElementException if there is no element left in the map */ public int key() throws ConcurrentModificationException, NoSuchElementException { if (referenceCount != count) { throw new ConcurrentModificationException(); } if (current < 0) { throw new NoSuchElementException(); } return keys[current]; } /** * Get the value of current entry. * @return value of current entry * @exception ConcurrentModificationException if the map is modified during iteration * @exception NoSuchElementException if there is no element left in the map */ public double value() throws ConcurrentModificationException, NoSuchElementException { if (referenceCount != count) { throw new ConcurrentModificationException(); } if (current < 0) { throw new NoSuchElementException(); } return values[current]; } /** * Advance iterator one step further. * @exception ConcurrentModificationException if the map is modified during iteration * @exception NoSuchElementException if there is no element left in the map */ public void advance() throws ConcurrentModificationException, NoSuchElementException { if (referenceCount != count) { throw new ConcurrentModificationException(); } // advance on step current = next; // prepare next step try { while (states[++next] != FULL) { // NOPMD // nothing to do } } catch (ArrayIndexOutOfBoundsException e) { next = -2; if (current < 0) { throw new NoSuchElementException(); } } } } /** * Read a serialized object. * @param stream input stream * @throws IOException if object cannot be read * @throws ClassNotFoundException if the class corresponding * to the serialized object cannot be found */ private void readObject(final ObjectInputStream stream) throws IOException, ClassNotFoundException { stream.defaultReadObject(); count = 0; } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/util/BigReal.java100644 1750 1750 22170 12126627720 25670 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.util; import java.io.Serializable; import java.math.BigDecimal; import java.math.BigInteger; import java.math.MathContext; import java.math.RoundingMode; import org.apache.commons.math3.Field; import org.apache.commons.math3.FieldElement; import org.apache.commons.math3.exception.MathArithmeticException; import org.apache.commons.math3.exception.util.LocalizedFormats; /** * Arbitrary precision decimal number. *

                              * This class is a simple wrapper around the standard BigDecimal * in order to implement the {@link FieldElement} interface. *

                              * @since 2.0 * @version $Id: BigReal.java 1416643 2012-12-03 19:37:14Z tn $ */ public class BigReal implements FieldElement, Comparable, Serializable { /** A big real representing 0. */ public static final BigReal ZERO = new BigReal(BigDecimal.ZERO); /** A big real representing 1. */ public static final BigReal ONE = new BigReal(BigDecimal.ONE); /** Serializable version identifier. */ private static final long serialVersionUID = 4984534880991310382L; /** Underlying BigDecimal. */ private final BigDecimal d; /** Rounding mode for divisions. **/ private RoundingMode roundingMode = RoundingMode.HALF_UP; /*** BigDecimal scale ***/ private int scale = 64; /** Build an instance from a BigDecimal. * @param val value of the instance */ public BigReal(BigDecimal val) { d = val; } /** Build an instance from a BigInteger. * @param val value of the instance */ public BigReal(BigInteger val) { d = new BigDecimal(val); } /** Build an instance from an unscaled BigInteger. * @param unscaledVal unscaled value * @param scale scale to use */ public BigReal(BigInteger unscaledVal, int scale) { d = new BigDecimal(unscaledVal, scale); } /** Build an instance from an unscaled BigInteger. * @param unscaledVal unscaled value * @param scale scale to use * @param mc to used */ public BigReal(BigInteger unscaledVal, int scale, MathContext mc) { d = new BigDecimal(unscaledVal, scale, mc); } /** Build an instance from a BigInteger. * @param val value of the instance * @param mc context to use */ public BigReal(BigInteger val, MathContext mc) { d = new BigDecimal(val, mc); } /** Build an instance from a characters representation. * @param in character representation of the value */ public BigReal(char[] in) { d = new BigDecimal(in); } /** Build an instance from a characters representation. * @param in character representation of the value * @param offset offset of the first character to analyze * @param len length of the array slice to analyze */ public BigReal(char[] in, int offset, int len) { d = new BigDecimal(in, offset, len); } /** Build an instance from a characters representation. * @param in character representation of the value * @param offset offset of the first character to analyze * @param len length of the array slice to analyze * @param mc context to use */ public BigReal(char[] in, int offset, int len, MathContext mc) { d = new BigDecimal(in, offset, len, mc); } /** Build an instance from a characters representation. * @param in character representation of the value * @param mc context to use */ public BigReal(char[] in, MathContext mc) { d = new BigDecimal(in, mc); } /** Build an instance from a double. * @param val value of the instance */ public BigReal(double val) { d = new BigDecimal(val); } /** Build an instance from a double. * @param val value of the instance * @param mc context to use */ public BigReal(double val, MathContext mc) { d = new BigDecimal(val, mc); } /** Build an instance from an int. * @param val value of the instance */ public BigReal(int val) { d = new BigDecimal(val); } /** Build an instance from an int. * @param val value of the instance * @param mc context to use */ public BigReal(int val, MathContext mc) { d = new BigDecimal(val, mc); } /** Build an instance from a long. * @param val value of the instance */ public BigReal(long val) { d = new BigDecimal(val); } /** Build an instance from a long. * @param val value of the instance * @param mc context to use */ public BigReal(long val, MathContext mc) { d = new BigDecimal(val, mc); } /** Build an instance from a String representation. * @param val character representation of the value */ public BigReal(String val) { d = new BigDecimal(val); } /** Build an instance from a String representation. * @param val character representation of the value * @param mc context to use */ public BigReal(String val, MathContext mc) { d = new BigDecimal(val, mc); } /*** * Gets the rounding mode for division operations * The default is {@code RoundingMode.HALF_UP} * @return the rounding mode. * @since 2.1 */ public RoundingMode getRoundingMode() { return roundingMode; } /*** * Sets the rounding mode for decimal divisions. * @param roundingMode rounding mode for decimal divisions * @since 2.1 */ public void setRoundingMode(RoundingMode roundingMode) { this.roundingMode = roundingMode; } /*** * Sets the scale for division operations. * The default is 64 * @return the scale * @since 2.1 */ public int getScale() { return scale; } /*** * Sets the scale for division operations. * @param scale scale for division operations * @since 2.1 */ public void setScale(int scale) { this.scale = scale; } /** {@inheritDoc} */ public BigReal add(BigReal a) { return new BigReal(d.add(a.d)); } /** {@inheritDoc} */ public BigReal subtract(BigReal a) { return new BigReal(d.subtract(a.d)); } /** {@inheritDoc} */ public BigReal negate() { return new BigReal(d.negate()); } /** * {@inheritDoc} * * @throws MathArithmeticException if {@code a} is zero */ public BigReal divide(BigReal a) throws MathArithmeticException { try { return new BigReal(d.divide(a.d, scale, roundingMode)); } catch (ArithmeticException e) { // Division by zero has occured throw new MathArithmeticException(LocalizedFormats.ZERO_NOT_ALLOWED); } } /** * {@inheritDoc} * * @throws MathArithmeticException if {@code this} is zero */ public BigReal reciprocal() throws MathArithmeticException { try { return new BigReal(BigDecimal.ONE.divide(d, scale, roundingMode)); } catch (ArithmeticException e) { // Division by zero has occured throw new MathArithmeticException(LocalizedFormats.ZERO_NOT_ALLOWED); } } /** {@inheritDoc} */ public BigReal multiply(BigReal a) { return new BigReal(d.multiply(a.d)); } /** {@inheritDoc} */ public BigReal multiply(final int n) { return new BigReal(d.multiply(new BigDecimal(n))); } /** {@inheritDoc} */ public int compareTo(BigReal a) { return d.compareTo(a.d); } /** Get the double value corresponding to the instance. * @return double value corresponding to the instance */ public double doubleValue() { return d.doubleValue(); } /** Get the BigDecimal value corresponding to the instance. * @return BigDecimal value corresponding to the instance */ public BigDecimal bigDecimalValue() { return d; } /** {@inheritDoc} */ @Override public boolean equals(Object other) { if (this == other){ return true; } if (other instanceof BigReal){ return d.equals(((BigReal) other).d); } return false; } /** {@inheritDoc} */ @Override public int hashCode() { return d.hashCode(); } /** {@inheritDoc} */ public Field getField() { return BigRealField.getInstance(); } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/util/CompositeFormat.java100644 1750 1750 17637 12126627720 27512 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.util; import java.text.FieldPosition; import java.text.NumberFormat; import java.text.ParsePosition; import java.util.Locale; /** * Base class for formatters of composite objects (complex numbers, vectors ...). * * @version $Id: CompositeFormat.java 1462503 2013-03-29 15:48:27Z luc $ */ public class CompositeFormat { /** * Class contains only static methods. */ private CompositeFormat() {} /** * Create a default number format. The default number format is based on * {@link NumberFormat#getInstance()} with the only customizing that the * maximum number of fraction digits is set to 10. * @return the default number format. */ public static NumberFormat getDefaultNumberFormat() { return getDefaultNumberFormat(Locale.getDefault()); } /** * Create a default number format. The default number format is based on * {@link NumberFormat#getInstance(java.util.Locale)} with the only * customizing that the maximum number of fraction digits is set to 10. * @param locale the specific locale used by the format. * @return the default number format specific to the given locale. */ public static NumberFormat getDefaultNumberFormat(final Locale locale) { final NumberFormat nf = NumberFormat.getInstance(locale); nf.setMaximumFractionDigits(10); return nf; } /** * Parses source until a non-whitespace character is found. * * @param source the string to parse * @param pos input/output parsing parameter. On output, pos * holds the index of the next non-whitespace character. */ public static void parseAndIgnoreWhitespace(final String source, final ParsePosition pos) { parseNextCharacter(source, pos); pos.setIndex(pos.getIndex() - 1); } /** * Parses source until a non-whitespace character is found. * * @param source the string to parse * @param pos input/output parsing parameter. * @return the first non-whitespace character. */ public static char parseNextCharacter(final String source, final ParsePosition pos) { int index = pos.getIndex(); final int n = source.length(); char ret = 0; if (index < n) { char c; do { c = source.charAt(index++); } while (Character.isWhitespace(c) && index < n); pos.setIndex(index); if (index < n) { ret = c; } } return ret; } /** * Parses source for special double values. These values * include Double.NaN, Double.POSITIVE_INFINITY, Double.NEGATIVE_INFINITY. * * @param source the string to parse * @param value the special value to parse. * @param pos input/output parsing parameter. * @return the special number. */ private static Number parseNumber(final String source, final double value, final ParsePosition pos) { Number ret = null; StringBuilder sb = new StringBuilder(); sb.append('('); sb.append(value); sb.append(')'); final int n = sb.length(); final int startIndex = pos.getIndex(); final int endIndex = startIndex + n; if (endIndex < source.length() && source.substring(startIndex, endIndex).compareTo(sb.toString()) == 0) { ret = Double.valueOf(value); pos.setIndex(endIndex); } return ret; } /** * Parses source for a number. This method can parse normal, * numeric values as well as special values. These special values include * Double.NaN, Double.POSITIVE_INFINITY, Double.NEGATIVE_INFINITY. * * @param source the string to parse * @param format the number format used to parse normal, numeric values. * @param pos input/output parsing parameter. * @return the parsed number. */ public static Number parseNumber(final String source, final NumberFormat format, final ParsePosition pos) { final int startIndex = pos.getIndex(); Number number = format.parse(source, pos); final int endIndex = pos.getIndex(); // check for error parsing number if (startIndex == endIndex) { // try parsing special numbers final double[] special = { Double.NaN, Double.POSITIVE_INFINITY, Double.NEGATIVE_INFINITY }; for (int i = 0; i < special.length; ++i) { number = parseNumber(source, special[i], pos); if (number != null) { break; } } } return number; } /** * Parse source for an expected fixed string. * @param source the string to parse * @param expected expected string * @param pos input/output parsing parameter. * @return true if the expected string was there */ public static boolean parseFixedstring(final String source, final String expected, final ParsePosition pos) { final int startIndex = pos.getIndex(); final int endIndex = startIndex + expected.length(); if ((startIndex >= source.length()) || (endIndex > source.length()) || (source.substring(startIndex, endIndex).compareTo(expected) != 0)) { // set index back to start, error index should be the start index pos.setIndex(startIndex); pos.setErrorIndex(startIndex); return false; } // the string was here pos.setIndex(endIndex); return true; } /** * Formats a double value to produce a string. In general, the value is * formatted using the formatting rules of format. There are * three exceptions to this: *
                                *
                              1. NaN is formatted as '(NaN)'
                              2. *
                              3. Positive infinity is formatted as '(Infinity)'
                              4. *
                              5. Negative infinity is formatted as '(-Infinity)'
                              6. *
                              * * @param value the double to format. * @param format the format used. * @param toAppendTo where the text is to be appended * @param pos On input: an alignment field, if desired. On output: the * offsets of the alignment field * @return the value passed in as toAppendTo. */ public static StringBuffer formatDouble(final double value, final NumberFormat format, final StringBuffer toAppendTo, final FieldPosition pos) { if( Double.isNaN(value) || Double.isInfinite(value) ) { toAppendTo.append('('); toAppendTo.append(value); toAppendTo.append(')'); } else { format.format(value, toAppendTo, pos); } return toAppendTo; } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/util/IterationEvent.java100644 1750 1750 3611 12126627720 27302 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.util; import java.util.EventObject; /** * The root class from which all events occurring while running an * {@link IterationManager} should be derived. * * @version $Id: IterationEvent.java 1416643 2012-12-03 19:37:14Z tn $ */ public class IterationEvent extends EventObject { /** */ private static final long serialVersionUID = 20120128L; /** The number of iterations performed so far. */ private final int iterations; /** * Creates a new instance of this class. * * @param source the iterative algorithm on which the event initially * occurred * @param iterations the number of iterations performed at the time * {@code this} event is created */ public IterationEvent(final Object source, final int iterations) { super(source); this.iterations = iterations; } /** * Returns the number of iterations performed at the time {@code this} event * is created. * * @return the number of iterations performed */ public int getIterations() { return iterations; } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/util/Decimal64Field.java100644 1750 1750 3571 12126627720 27023 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.util; import org.apache.commons.math3.Field; import org.apache.commons.math3.FieldElement; /** * The field of double precision floating-point numbers. * * @since 3.1 * @version $Id: Decimal64Field.java 1306177 2012-03-28 05:40:46Z celestin $ * @see Decimal64 */ public class Decimal64Field implements Field { /** The unique instance of this class. */ private static final Decimal64Field INSTANCE = new Decimal64Field(); /** Default constructor. */ private Decimal64Field() { // Do nothing } /** * Returns the unique instance of this class. * * @return the unique instance of this class */ public static final Decimal64Field getInstance() { return INSTANCE; } /** {@inheritDoc} */ public Decimal64 getZero() { return Decimal64.ZERO; } /** {@inheritDoc} */ public Decimal64 getOne() { return Decimal64.ONE; } /** {@inheritDoc} */ public Class> getRuntimeClass() { return Decimal64.class; } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/util/MultidimensionalCounter.java100644 1750 1750 21266 12126627720 31245 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.util; import org.apache.commons.math3.exception.DimensionMismatchException; import org.apache.commons.math3.exception.NotStrictlyPositiveException; import org.apache.commons.math3.exception.OutOfRangeException; /** * Converter between unidimensional storage structure and multidimensional * conceptual structure. * This utility will convert from indices in a multidimensional structure * to the corresponding index in a one-dimensional array. For example, * assuming that the ranges (in 3 dimensions) of indices are 2, 4 and 3, * the following correspondences, between 3-tuples indices and unidimensional * indices, will hold: *
                                *
                              • (0, 0, 0) corresponds to 0
                              • *
                              • (0, 0, 1) corresponds to 1
                              • *
                              • (0, 0, 2) corresponds to 2
                              • *
                              • (0, 1, 0) corresponds to 3
                              • *
                              • ...
                              • *
                              • (1, 0, 0) corresponds to 12
                              • *
                              • ...
                              • *
                              • (1, 3, 2) corresponds to 23
                              • *
                              * * @since 2.2 * @version $Id: MultidimensionalCounter.java 1382887 2012-09-10 14:37:27Z luc $ */ public class MultidimensionalCounter implements Iterable { /** * Number of dimensions. */ private final int dimension; /** * Offset for each dimension. */ private final int[] uniCounterOffset; /** * Counter sizes. */ private final int[] size; /** * Total number of (one-dimensional) slots. */ private final int totalSize; /** * Index of last dimension. */ private final int last; /** * Perform iteration over the multidimensional counter. */ public class Iterator implements java.util.Iterator { /** * Multidimensional counter. */ private final int[] counter = new int[dimension]; /** * Unidimensional counter. */ private int count = -1; /** * Create an iterator * @see #iterator() */ Iterator() { counter[last] = -1; } /** * {@inheritDoc} */ public boolean hasNext() { for (int i = 0; i < dimension; i++) { if (counter[i] != size[i] - 1) { return true; } } return false; } /** * @return the unidimensional count after the counter has been * incremented by {@code 1}. */ public Integer next() { for (int i = last; i >= 0; i--) { if (counter[i] == size[i] - 1) { counter[i] = 0; } else { ++counter[i]; break; } } return ++count; } /** * Get the current unidimensional counter slot. * * @return the index within the unidimensionl counter. */ public int getCount() { return count; } /** * Get the current multidimensional counter slots. * * @return the indices within the multidimensional counter. */ public int[] getCounts() { return MathArrays.copyOf(counter); } /** * Get the current count in the selected dimension. * * @param dim Dimension index. * @return the count at the corresponding index for the current state * of the iterator. * @throws IndexOutOfBoundsException if {@code index} is not in the * correct interval (as defined by the length of the argument in the * {@link MultidimensionalCounter#MultidimensionalCounter(int[]) * constructor of the enclosing class}). */ public int getCount(int dim) { return counter[dim]; } /** * @throws UnsupportedOperationException */ public void remove() { throw new UnsupportedOperationException(); } } /** * Create a counter. * * @param size Counter sizes (number of slots in each dimension). * @throws NotStrictlyPositiveException if one of the sizes is * negative or zero. */ public MultidimensionalCounter(int ... size) throws NotStrictlyPositiveException { dimension = size.length; this.size = MathArrays.copyOf(size); uniCounterOffset = new int[dimension]; last = dimension - 1; int tS = size[last]; for (int i = 0; i < last; i++) { int count = 1; for (int j = i + 1; j < dimension; j++) { count *= size[j]; } uniCounterOffset[i] = count; tS *= size[i]; } uniCounterOffset[last] = 0; if (tS <= 0) { throw new NotStrictlyPositiveException(tS); } totalSize = tS; } /** * Create an iterator over this counter. * * @return the iterator. */ public Iterator iterator() { return new Iterator(); } /** * Get the number of dimensions of the multidimensional counter. * * @return the number of dimensions. */ public int getDimension() { return dimension; } /** * Convert to multidimensional counter. * * @param index Index in unidimensional counter. * @return the multidimensional counts. * @throws OutOfRangeException if {@code index} is not between * {@code 0} and the value returned by {@link #getSize()} (excluded). */ public int[] getCounts(int index) throws OutOfRangeException { if (index < 0 || index >= totalSize) { throw new OutOfRangeException(index, 0, totalSize); } final int[] indices = new int[dimension]; int count = 0; for (int i = 0; i < last; i++) { int idx = 0; final int offset = uniCounterOffset[i]; while (count <= index) { count += offset; ++idx; } --idx; count -= offset; indices[i] = idx; } indices[last] = index - count; return indices; } /** * Convert to unidimensional counter. * * @param c Indices in multidimensional counter. * @return the index within the unidimensionl counter. * @throws DimensionMismatchException if the size of {@code c} * does not match the size of the array given in the constructor. * @throws OutOfRangeException if a value of {@code c} is not in * the range of the corresponding dimension, as defined in the * {@link MultidimensionalCounter#MultidimensionalCounter(int...) constructor}. */ public int getCount(int ... c) throws OutOfRangeException, DimensionMismatchException { if (c.length != dimension) { throw new DimensionMismatchException(c.length, dimension); } int count = 0; for (int i = 0; i < dimension; i++) { final int index = c[i]; if (index < 0 || index >= size[i]) { throw new OutOfRangeException(index, 0, size[i] - 1); } count += uniCounterOffset[i] * c[i]; } return count + c[last]; } /** * Get the total number of elements. * * @return the total size of the unidimensional counter. */ public int getSize() { return totalSize; } /** * Get the number of multidimensional counter slots in each dimension. * * @return the sizes of the multidimensional counter in each dimension. */ public int[] getSizes() { return MathArrays.copyOf(size); } /** * {@inheritDoc} */ @Override public String toString() { final StringBuilder sb = new StringBuilder(); for (int i = 0; i < dimension; i++) { sb.append("[").append(getCount(i)).append("]"); } return sb.toString(); } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/util/NumberTransformer.java100644 1750 1750 3006 12126627720 30013 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.util; import org.apache.commons.math3.exception.MathIllegalArgumentException; /** * Subclasses implementing this interface can transform Objects to doubles. * @version $Id: NumberTransformer.java 1416643 2012-12-03 19:37:14Z tn $ * * No longer extends Serializable since 2.0 * */ public interface NumberTransformer { /** * Implementing this interface provides a facility to transform * from Object to Double. * * @param o the Object to be transformed. * @return the double value of the Object. * @throws MathIllegalArgumentException if the Object can not be transformed into a Double. */ double transform(Object o) throws MathIllegalArgumentException; } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/util/FastMath.java100644 1750 1750 357052 12126627720 26124 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.util; import java.io.PrintStream; /** * Faster, more accurate, portable alternative to {@link Math} and * {@link StrictMath} for large scale computation. *

                              * FastMath is a drop-in replacement for both Math and StrictMath. This * means that for any method in Math (say {@code Math.sin(x)} or * {@code Math.cbrt(y)}), user can directly change the class and use the * methods as is (using {@code FastMath.sin(x)} or {@code FastMath.cbrt(y)} * in the previous example). *

                              *

                              * FastMath speed is achieved by relying heavily on optimizing compilers * to native code present in many JVMs today and use of large tables. * The larger tables are lazily initialised on first use, so that the setup * time does not penalise methods that don't need them. *

                              *

                              * Note that FastMath is * extensively used inside Apache Commons Math, so by calling some algorithms, * the overhead when the the tables need to be intialised will occur * regardless of the end-user calling FastMath methods directly or not. * Performance figures for a specific JVM and hardware can be evaluated by * running the FastMathTestPerformance tests in the test directory of the source * distribution. *

                              *

                              * FastMath accuracy should be mostly independent of the JVM as it relies only * on IEEE-754 basic operations and on embedded tables. Almost all operations * are accurate to about 0.5 ulp throughout the domain range. This statement, * of course is only a rough global observed behavior, it is not a * guarantee for every double numbers input (see William Kahan's Table * Maker's Dilemma). *

                              *

                              * FastMath additionally implements the following methods not found in Math/StrictMath: *

                                *
                              • {@link #asinh(double)}
                              • *
                              • {@link #acosh(double)}
                              • *
                              • {@link #atanh(double)}
                              • *
                              * The following methods are found in Math/StrictMath since 1.6 only, they are provided * by FastMath even in 1.5 Java virtual machines *
                                *
                              • {@link #copySign(double, double)}
                              • *
                              • {@link #getExponent(double)}
                              • *
                              • {@link #nextAfter(double,double)}
                              • *
                              • {@link #nextUp(double)}
                              • *
                              • {@link #scalb(double, int)}
                              • *
                              • {@link #copySign(float, float)}
                              • *
                              • {@link #getExponent(float)}
                              • *
                              • {@link #nextAfter(float,double)}
                              • *
                              • {@link #nextUp(float)}
                              • *
                              • {@link #scalb(float, int)}
                              • *
                              *

                              * @version $Id: FastMath.java 1462503 2013-03-29 15:48:27Z luc $ * @since 2.2 */ public class FastMath { /** Archimede's constant PI, ratio of circle circumference to diameter. */ public static final double PI = 105414357.0 / 33554432.0 + 1.984187159361080883e-9; /** Napier's constant e, base of the natural logarithm. */ public static final double E = 2850325.0 / 1048576.0 + 8.254840070411028747e-8; /** Index of exp(0) in the array of integer exponentials. */ static final int EXP_INT_TABLE_MAX_INDEX = 750; /** Length of the array of integer exponentials. */ static final int EXP_INT_TABLE_LEN = EXP_INT_TABLE_MAX_INDEX * 2; /** Logarithm table length. */ static final int LN_MANT_LEN = 1024; /** Exponential fractions table length. */ static final int EXP_FRAC_TABLE_LEN = 1025; // 0, 1/1024, ... 1024/1024 /** StrictMath.log(Double.MAX_VALUE): {@value} */ private static final double LOG_MAX_VALUE = StrictMath.log(Double.MAX_VALUE); /** Indicator for tables initialization. *

                              * This compile-time constant should be set to true only if one explicitly * wants to compute the tables at class loading time instead of using the * already computed ones provided as literal arrays below. *

                              */ private static final boolean RECOMPUTE_TABLES_AT_RUNTIME = false; /** log(2) (high bits). */ private static final double LN_2_A = 0.693147063255310059; /** log(2) (low bits). */ private static final double LN_2_B = 1.17304635250823482e-7; /** Coefficients for log, when input 0.99 < x < 1.01. */ private static final double LN_QUICK_COEF[][] = { {1.0, 5.669184079525E-24}, {-0.25, -0.25}, {0.3333333134651184, 1.986821492305628E-8}, {-0.25, -6.663542893624021E-14}, {0.19999998807907104, 1.1921056801463227E-8}, {-0.1666666567325592, -7.800414592973399E-9}, {0.1428571343421936, 5.650007086920087E-9}, {-0.12502530217170715, -7.44321345601866E-11}, {0.11113807559013367, 9.219544613762692E-9}, }; /** Coefficients for log in the range of 1.0 < x < 1.0 + 2^-10. */ private static final double LN_HI_PREC_COEF[][] = { {1.0, -6.032174644509064E-23}, {-0.25, -0.25}, {0.3333333134651184, 1.9868161777724352E-8}, {-0.2499999701976776, -2.957007209750105E-8}, {0.19999954104423523, 1.5830993332061267E-10}, {-0.16624879837036133, -2.6033824355191673E-8} }; /** Sine, Cosine, Tangent tables are for 0, 1/8, 2/8, ... 13/8 = PI/2 approx. */ private static final int SINE_TABLE_LEN = 14; /** Sine table (high bits). */ private static final double SINE_TABLE_A[] = { +0.0d, +0.1246747374534607d, +0.24740394949913025d, +0.366272509098053d, +0.4794255495071411d, +0.5850973129272461d, +0.6816387176513672d, +0.7675435543060303d, +0.8414709568023682d, +0.902267575263977d, +0.9489846229553223d, +0.9808930158615112d, +0.9974949359893799d, +0.9985313415527344d, }; /** Sine table (low bits). */ private static final double SINE_TABLE_B[] = { +0.0d, -4.068233003401932E-9d, +9.755392680573412E-9d, +1.9987994582857286E-8d, -1.0902938113007961E-8d, -3.9986783938944604E-8d, +4.23719669792332E-8d, -5.207000323380292E-8d, +2.800552834259E-8d, +1.883511811213715E-8d, -3.5997360512765566E-9d, +4.116164446561962E-8d, +5.0614674548127384E-8d, -1.0129027912496858E-9d, }; /** Cosine table (high bits). */ private static final double COSINE_TABLE_A[] = { +1.0d, +0.9921976327896118d, +0.9689123630523682d, +0.9305076599121094d, +0.8775825500488281d, +0.8109631538391113d, +0.7316888570785522d, +0.6409968137741089d, +0.5403022766113281d, +0.4311765432357788d, +0.3153223395347595d, +0.19454771280288696d, +0.07073719799518585d, -0.05417713522911072d, }; /** Cosine table (low bits). */ private static final double COSINE_TABLE_B[] = { +0.0d, +3.4439717236742845E-8d, +5.865827662008209E-8d, -3.7999795083850525E-8d, +1.184154459111628E-8d, -3.43338934259355E-8d, +1.1795268640216787E-8d, +4.438921624363781E-8d, +2.925681159240093E-8d, -2.6437112632041807E-8d, +2.2860509143963117E-8d, -4.813899778443457E-9d, +3.6725170580355583E-9d, +2.0217439756338078E-10d, }; /** Tangent table, used by atan() (high bits). */ private static final double TANGENT_TABLE_A[] = { +0.0d, +0.1256551444530487d, +0.25534194707870483d, +0.3936265707015991d, +0.5463024377822876d, +0.7214844226837158d, +0.9315965175628662d, +1.1974215507507324d, +1.5574076175689697d, +2.092571258544922d, +3.0095696449279785d, +5.041914939880371d, +14.101419448852539d, -18.430862426757812d, }; /** Tangent table, used by atan() (low bits). */ private static final double TANGENT_TABLE_B[] = { +0.0d, -7.877917738262007E-9d, -2.5857668567479893E-8d, +5.2240336371356666E-9d, +5.206150291559893E-8d, +1.8307188599677033E-8d, -5.7618793749770706E-8d, +7.848361555046424E-8d, +1.0708593250394448E-7d, +1.7827257129423813E-8d, +2.893485277253286E-8d, +3.1660099222737955E-7d, +4.983191803254889E-7d, -3.356118100840571E-7d, }; /** Bits of 1/(2*pi), need for reducePayneHanek(). */ private static final long RECIP_2PI[] = new long[] { (0x28be60dbL << 32) | 0x9391054aL, (0x7f09d5f4L << 32) | 0x7d4d3770L, (0x36d8a566L << 32) | 0x4f10e410L, (0x7f9458eaL << 32) | 0xf7aef158L, (0x6dc91b8eL << 32) | 0x909374b8L, (0x01924bbaL << 32) | 0x82746487L, (0x3f877ac7L << 32) | 0x2c4a69cfL, (0xba208d7dL << 32) | 0x4baed121L, (0x3a671c09L << 32) | 0xad17df90L, (0x4e64758eL << 32) | 0x60d4ce7dL, (0x272117e2L << 32) | 0xef7e4a0eL, (0xc7fe25ffL << 32) | 0xf7816603L, (0xfbcbc462L << 32) | 0xd6829b47L, (0xdb4d9fb3L << 32) | 0xc9f2c26dL, (0xd3d18fd9L << 32) | 0xa797fa8bL, (0x5d49eeb1L << 32) | 0xfaf97c5eL, (0xcf41ce7dL << 32) | 0xe294a4baL, 0x9afed7ecL << 32 }; /** Bits of pi/4, need for reducePayneHanek(). */ private static final long PI_O_4_BITS[] = new long[] { (0xc90fdaa2L << 32) | 0x2168c234L, (0xc4c6628bL << 32) | 0x80dc1cd1L }; /** Eighths. * This is used by sinQ, because its faster to do a table lookup than * a multiply in this time-critical routine */ private static final double EIGHTHS[] = {0, 0.125, 0.25, 0.375, 0.5, 0.625, 0.75, 0.875, 1.0, 1.125, 1.25, 1.375, 1.5, 1.625}; /** Table of 2^((n+2)/3) */ private static final double CBRTTWO[] = { 0.6299605249474366, 0.7937005259840998, 1.0, 1.2599210498948732, 1.5874010519681994 }; /* * There are 52 bits in the mantissa of a double. * For additional precision, the code splits double numbers into two parts, * by clearing the low order 30 bits if possible, and then performs the arithmetic * on each half separately. */ /** * 0x40000000 - used to split a double into two parts, both with the low order bits cleared. * Equivalent to 2^30. */ private static final long HEX_40000000 = 0x40000000L; // 1073741824L /** Mask used to clear low order 30 bits */ private static final long MASK_30BITS = -1L - (HEX_40000000 -1); // 0xFFFFFFFFC0000000L; /** Mask used to clear the non-sign part of an int. */ private static final int MASK_NON_SIGN_INT = 0x7fffffff; /** Mask used to clear the non-sign part of a long. */ private static final long MASK_NON_SIGN_LONG = 0x7fffffffffffffffl; /** 2^52 - double numbers this large must be integral (no fraction) or NaN or Infinite */ private static final double TWO_POWER_52 = 4503599627370496.0; /** 2^53 - double numbers this large must be even. */ private static final double TWO_POWER_53 = 2 * TWO_POWER_52; /** Constant: {@value}. */ private static final double F_1_3 = 1d / 3d; /** Constant: {@value}. */ private static final double F_1_5 = 1d / 5d; /** Constant: {@value}. */ private static final double F_1_7 = 1d / 7d; /** Constant: {@value}. */ private static final double F_1_9 = 1d / 9d; /** Constant: {@value}. */ private static final double F_1_11 = 1d / 11d; /** Constant: {@value}. */ private static final double F_1_13 = 1d / 13d; /** Constant: {@value}. */ private static final double F_1_15 = 1d / 15d; /** Constant: {@value}. */ private static final double F_1_17 = 1d / 17d; /** Constant: {@value}. */ private static final double F_3_4 = 3d / 4d; /** Constant: {@value}. */ private static final double F_15_16 = 15d / 16d; /** Constant: {@value}. */ private static final double F_13_14 = 13d / 14d; /** Constant: {@value}. */ private static final double F_11_12 = 11d / 12d; /** Constant: {@value}. */ private static final double F_9_10 = 9d / 10d; /** Constant: {@value}. */ private static final double F_7_8 = 7d / 8d; /** Constant: {@value}. */ private static final double F_5_6 = 5d / 6d; /** Constant: {@value}. */ private static final double F_1_2 = 1d / 2d; /** Constant: {@value}. */ private static final double F_1_4 = 1d / 4d; /** * Private Constructor */ private FastMath() {} // Generic helper methods /** * Get the high order bits from the mantissa. * Equivalent to adding and subtracting HEX_40000 but also works for very large numbers * * @param d the value to split * @return the high order part of the mantissa */ private static double doubleHighPart(double d) { if (d > -Precision.SAFE_MIN && d < Precision.SAFE_MIN){ return d; // These are un-normalised - don't try to convert } long xl = Double.doubleToRawLongBits(d); // can take raw bits because just gonna convert it back xl = xl & MASK_30BITS; // Drop low order bits return Double.longBitsToDouble(xl); } /** Compute the square root of a number. *

                              Note: this implementation currently delegates to {@link Math#sqrt} * @param a number on which evaluation is done * @return square root of a */ public static double sqrt(final double a) { return Math.sqrt(a); } /** Compute the hyperbolic cosine of a number. * @param x number on which evaluation is done * @return hyperbolic cosine of x */ public static double cosh(double x) { if (x != x) { return x; } // cosh[z] = (exp(z) + exp(-z))/2 // for numbers with magnitude 20 or so, // exp(-z) can be ignored in comparison with exp(z) if (x > 20) { if (x >= LOG_MAX_VALUE) { // Avoid overflow (MATH-905). final double t = exp(0.5 * x); return (0.5 * t) * t; } else { return 0.5 * exp(x); } } else if (x < -20) { if (x <= -LOG_MAX_VALUE) { // Avoid overflow (MATH-905). final double t = exp(-0.5 * x); return (0.5 * t) * t; } else { return 0.5 * exp(-x); } } final double hiPrec[] = new double[2]; if (x < 0.0) { x = -x; } exp(x, 0.0, hiPrec); double ya = hiPrec[0] + hiPrec[1]; double yb = -(ya - hiPrec[0] - hiPrec[1]); double temp = ya * HEX_40000000; double yaa = ya + temp - temp; double yab = ya - yaa; // recip = 1/y double recip = 1.0/ya; temp = recip * HEX_40000000; double recipa = recip + temp - temp; double recipb = recip - recipa; // Correct for rounding in division recipb += (1.0 - yaa*recipa - yaa*recipb - yab*recipa - yab*recipb) * recip; // Account for yb recipb += -yb * recip * recip; // y = y + 1/y temp = ya + recipa; yb += -(temp - ya - recipa); ya = temp; temp = ya + recipb; yb += -(temp - ya - recipb); ya = temp; double result = ya + yb; result *= 0.5; return result; } /** Compute the hyperbolic sine of a number. * @param x number on which evaluation is done * @return hyperbolic sine of x */ public static double sinh(double x) { boolean negate = false; if (x != x) { return x; } // sinh[z] = (exp(z) - exp(-z) / 2 // for values of z larger than about 20, // exp(-z) can be ignored in comparison with exp(z) if (x > 20) { if (x >= LOG_MAX_VALUE) { // Avoid overflow (MATH-905). final double t = exp(0.5 * x); return (0.5 * t) * t; } else { return 0.5 * exp(x); } } else if (x < -20) { if (x <= -LOG_MAX_VALUE) { // Avoid overflow (MATH-905). final double t = exp(-0.5 * x); return (-0.5 * t) * t; } else { return -0.5 * exp(-x); } } if (x == 0) { return x; } if (x < 0.0) { x = -x; negate = true; } double result; if (x > 0.25) { double hiPrec[] = new double[2]; exp(x, 0.0, hiPrec); double ya = hiPrec[0] + hiPrec[1]; double yb = -(ya - hiPrec[0] - hiPrec[1]); double temp = ya * HEX_40000000; double yaa = ya + temp - temp; double yab = ya - yaa; // recip = 1/y double recip = 1.0/ya; temp = recip * HEX_40000000; double recipa = recip + temp - temp; double recipb = recip - recipa; // Correct for rounding in division recipb += (1.0 - yaa*recipa - yaa*recipb - yab*recipa - yab*recipb) * recip; // Account for yb recipb += -yb * recip * recip; recipa = -recipa; recipb = -recipb; // y = y + 1/y temp = ya + recipa; yb += -(temp - ya - recipa); ya = temp; temp = ya + recipb; yb += -(temp - ya - recipb); ya = temp; result = ya + yb; result *= 0.5; } else { double hiPrec[] = new double[2]; expm1(x, hiPrec); double ya = hiPrec[0] + hiPrec[1]; double yb = -(ya - hiPrec[0] - hiPrec[1]); /* Compute expm1(-x) = -expm1(x) / (expm1(x) + 1) */ double denom = 1.0 + ya; double denomr = 1.0 / denom; double denomb = -(denom - 1.0 - ya) + yb; double ratio = ya * denomr; double temp = ratio * HEX_40000000; double ra = ratio + temp - temp; double rb = ratio - ra; temp = denom * HEX_40000000; double za = denom + temp - temp; double zb = denom - za; rb += (ya - za*ra - za*rb - zb*ra - zb*rb) * denomr; // Adjust for yb rb += yb*denomr; // numerator rb += -ya * denomb * denomr * denomr; // denominator // y = y - 1/y temp = ya + ra; yb += -(temp - ya - ra); ya = temp; temp = ya + rb; yb += -(temp - ya - rb); ya = temp; result = ya + yb; result *= 0.5; } if (negate) { result = -result; } return result; } /** Compute the hyperbolic tangent of a number. * @param x number on which evaluation is done * @return hyperbolic tangent of x */ public static double tanh(double x) { boolean negate = false; if (x != x) { return x; } // tanh[z] = sinh[z] / cosh[z] // = (exp(z) - exp(-z)) / (exp(z) + exp(-z)) // = (exp(2x) - 1) / (exp(2x) + 1) // for magnitude > 20, sinh[z] == cosh[z] in double precision if (x > 20.0) { return 1.0; } if (x < -20) { return -1.0; } if (x == 0) { return x; } if (x < 0.0) { x = -x; negate = true; } double result; if (x >= 0.5) { double hiPrec[] = new double[2]; // tanh(x) = (exp(2x) - 1) / (exp(2x) + 1) exp(x*2.0, 0.0, hiPrec); double ya = hiPrec[0] + hiPrec[1]; double yb = -(ya - hiPrec[0] - hiPrec[1]); /* Numerator */ double na = -1.0 + ya; double nb = -(na + 1.0 - ya); double temp = na + yb; nb += -(temp - na - yb); na = temp; /* Denominator */ double da = 1.0 + ya; double db = -(da - 1.0 - ya); temp = da + yb; db += -(temp - da - yb); da = temp; temp = da * HEX_40000000; double daa = da + temp - temp; double dab = da - daa; // ratio = na/da double ratio = na/da; temp = ratio * HEX_40000000; double ratioa = ratio + temp - temp; double ratiob = ratio - ratioa; // Correct for rounding in division ratiob += (na - daa*ratioa - daa*ratiob - dab*ratioa - dab*ratiob) / da; // Account for nb ratiob += nb / da; // Account for db ratiob += -db * na / da / da; result = ratioa + ratiob; } else { double hiPrec[] = new double[2]; // tanh(x) = expm1(2x) / (expm1(2x) + 2) expm1(x*2.0, hiPrec); double ya = hiPrec[0] + hiPrec[1]; double yb = -(ya - hiPrec[0] - hiPrec[1]); /* Numerator */ double na = ya; double nb = yb; /* Denominator */ double da = 2.0 + ya; double db = -(da - 2.0 - ya); double temp = da + yb; db += -(temp - da - yb); da = temp; temp = da * HEX_40000000; double daa = da + temp - temp; double dab = da - daa; // ratio = na/da double ratio = na/da; temp = ratio * HEX_40000000; double ratioa = ratio + temp - temp; double ratiob = ratio - ratioa; // Correct for rounding in division ratiob += (na - daa*ratioa - daa*ratiob - dab*ratioa - dab*ratiob) / da; // Account for nb ratiob += nb / da; // Account for db ratiob += -db * na / da / da; result = ratioa + ratiob; } if (negate) { result = -result; } return result; } /** Compute the inverse hyperbolic cosine of a number. * @param a number on which evaluation is done * @return inverse hyperbolic cosine of a */ public static double acosh(final double a) { return FastMath.log(a + FastMath.sqrt(a * a - 1)); } /** Compute the inverse hyperbolic sine of a number. * @param a number on which evaluation is done * @return inverse hyperbolic sine of a */ public static double asinh(double a) { boolean negative = false; if (a < 0) { negative = true; a = -a; } double absAsinh; if (a > 0.167) { absAsinh = FastMath.log(FastMath.sqrt(a * a + 1) + a); } else { final double a2 = a * a; if (a > 0.097) { absAsinh = a * (1 - a2 * (F_1_3 - a2 * (F_1_5 - a2 * (F_1_7 - a2 * (F_1_9 - a2 * (F_1_11 - a2 * (F_1_13 - a2 * (F_1_15 - a2 * F_1_17 * F_15_16) * F_13_14) * F_11_12) * F_9_10) * F_7_8) * F_5_6) * F_3_4) * F_1_2); } else if (a > 0.036) { absAsinh = a * (1 - a2 * (F_1_3 - a2 * (F_1_5 - a2 * (F_1_7 - a2 * (F_1_9 - a2 * (F_1_11 - a2 * F_1_13 * F_11_12) * F_9_10) * F_7_8) * F_5_6) * F_3_4) * F_1_2); } else if (a > 0.0036) { absAsinh = a * (1 - a2 * (F_1_3 - a2 * (F_1_5 - a2 * (F_1_7 - a2 * F_1_9 * F_7_8) * F_5_6) * F_3_4) * F_1_2); } else { absAsinh = a * (1 - a2 * (F_1_3 - a2 * F_1_5 * F_3_4) * F_1_2); } } return negative ? -absAsinh : absAsinh; } /** Compute the inverse hyperbolic tangent of a number. * @param a number on which evaluation is done * @return inverse hyperbolic tangent of a */ public static double atanh(double a) { boolean negative = false; if (a < 0) { negative = true; a = -a; } double absAtanh; if (a > 0.15) { absAtanh = 0.5 * FastMath.log((1 + a) / (1 - a)); } else { final double a2 = a * a; if (a > 0.087) { absAtanh = a * (1 + a2 * (F_1_3 + a2 * (F_1_5 + a2 * (F_1_7 + a2 * (F_1_9 + a2 * (F_1_11 + a2 * (F_1_13 + a2 * (F_1_15 + a2 * F_1_17)))))))); } else if (a > 0.031) { absAtanh = a * (1 + a2 * (F_1_3 + a2 * (F_1_5 + a2 * (F_1_7 + a2 * (F_1_9 + a2 * (F_1_11 + a2 * F_1_13)))))); } else if (a > 0.003) { absAtanh = a * (1 + a2 * (F_1_3 + a2 * (F_1_5 + a2 * (F_1_7 + a2 * F_1_9)))); } else { absAtanh = a * (1 + a2 * (F_1_3 + a2 * F_1_5)); } } return negative ? -absAtanh : absAtanh; } /** Compute the signum of a number. * The signum is -1 for negative numbers, +1 for positive numbers and 0 otherwise * @param a number on which evaluation is done * @return -1.0, -0.0, +0.0, +1.0 or NaN depending on sign of a */ public static double signum(final double a) { return (a < 0.0) ? -1.0 : ((a > 0.0) ? 1.0 : a); // return +0.0/-0.0/NaN depending on a } /** Compute the signum of a number. * The signum is -1 for negative numbers, +1 for positive numbers and 0 otherwise * @param a number on which evaluation is done * @return -1.0, -0.0, +0.0, +1.0 or NaN depending on sign of a */ public static float signum(final float a) { return (a < 0.0f) ? -1.0f : ((a > 0.0f) ? 1.0f : a); // return +0.0/-0.0/NaN depending on a } /** Compute next number towards positive infinity. * @param a number to which neighbor should be computed * @return neighbor of a towards positive infinity */ public static double nextUp(final double a) { return nextAfter(a, Double.POSITIVE_INFINITY); } /** Compute next number towards positive infinity. * @param a number to which neighbor should be computed * @return neighbor of a towards positive infinity */ public static float nextUp(final float a) { return nextAfter(a, Float.POSITIVE_INFINITY); } /** Returns a pseudo-random number between 0.0 and 1.0. *

                              Note: this implementation currently delegates to {@link Math#random} * @return a random number between 0.0 and 1.0 */ public static double random() { return Math.random(); } /** * Exponential function. * * Computes exp(x), function result is nearly rounded. It will be correctly * rounded to the theoretical value for 99.9% of input values, otherwise it will * have a 1 UPL error. * * Method: * Lookup intVal = exp(int(x)) * Lookup fracVal = exp(int(x-int(x) / 1024.0) * 1024.0 ); * Compute z as the exponential of the remaining bits by a polynomial minus one * exp(x) = intVal * fracVal * (1 + z) * * Accuracy: * Calculation is done with 63 bits of precision, so result should be correctly * rounded for 99.9% of input values, with less than 1 ULP error otherwise. * * @param x a double * @return double ex */ public static double exp(double x) { return exp(x, 0.0, null); } /** * Internal helper method for exponential function. * @param x original argument of the exponential function * @param extra extra bits of precision on input (To Be Confirmed) * @param hiPrec extra bits of precision on output (To Be Confirmed) * @return exp(x) */ private static double exp(double x, double extra, double[] hiPrec) { double intPartA; double intPartB; int intVal; /* Lookup exp(floor(x)). * intPartA will have the upper 22 bits, intPartB will have the lower * 52 bits. */ if (x < 0.0) { intVal = (int) -x; if (intVal > 746) { if (hiPrec != null) { hiPrec[0] = 0.0; hiPrec[1] = 0.0; } return 0.0; } if (intVal > 709) { /* This will produce a subnormal output */ final double result = exp(x+40.19140625, extra, hiPrec) / 285040095144011776.0; if (hiPrec != null) { hiPrec[0] /= 285040095144011776.0; hiPrec[1] /= 285040095144011776.0; } return result; } if (intVal == 709) { /* exp(1.494140625) is nearly a machine number... */ final double result = exp(x+1.494140625, extra, hiPrec) / 4.455505956692756620; if (hiPrec != null) { hiPrec[0] /= 4.455505956692756620; hiPrec[1] /= 4.455505956692756620; } return result; } intVal++; intPartA = ExpIntTable.EXP_INT_TABLE_A[EXP_INT_TABLE_MAX_INDEX-intVal]; intPartB = ExpIntTable.EXP_INT_TABLE_B[EXP_INT_TABLE_MAX_INDEX-intVal]; intVal = -intVal; } else { intVal = (int) x; if (intVal > 709) { if (hiPrec != null) { hiPrec[0] = Double.POSITIVE_INFINITY; hiPrec[1] = 0.0; } return Double.POSITIVE_INFINITY; } intPartA = ExpIntTable.EXP_INT_TABLE_A[EXP_INT_TABLE_MAX_INDEX+intVal]; intPartB = ExpIntTable.EXP_INT_TABLE_B[EXP_INT_TABLE_MAX_INDEX+intVal]; } /* Get the fractional part of x, find the greatest multiple of 2^-10 less than * x and look up the exp function of it. * fracPartA will have the upper 22 bits, fracPartB the lower 52 bits. */ final int intFrac = (int) ((x - intVal) * 1024.0); final double fracPartA = ExpFracTable.EXP_FRAC_TABLE_A[intFrac]; final double fracPartB = ExpFracTable.EXP_FRAC_TABLE_B[intFrac]; /* epsilon is the difference in x from the nearest multiple of 2^-10. It * has a value in the range 0 <= epsilon < 2^-10. * Do the subtraction from x as the last step to avoid possible loss of percison. */ final double epsilon = x - (intVal + intFrac / 1024.0); /* Compute z = exp(epsilon) - 1.0 via a minimax polynomial. z has full double precision (52 bits). Since z < 2^-10, we will have 62 bits of precision when combined with the contant 1. This will be used in the last addition below to get proper rounding. */ /* Remez generated polynomial. Converges on the interval [0, 2^-10], error is less than 0.5 ULP */ double z = 0.04168701738764507; z = z * epsilon + 0.1666666505023083; z = z * epsilon + 0.5000000000042687; z = z * epsilon + 1.0; z = z * epsilon + -3.940510424527919E-20; /* Compute (intPartA+intPartB) * (fracPartA+fracPartB) by binomial expansion. tempA is exact since intPartA and intPartB only have 22 bits each. tempB will have 52 bits of precision. */ double tempA = intPartA * fracPartA; double tempB = intPartA * fracPartB + intPartB * fracPartA + intPartB * fracPartB; /* Compute the result. (1+z)(tempA+tempB). Order of operations is important. For accuracy add by increasing size. tempA is exact and much larger than the others. If there are extra bits specified from the pow() function, use them. */ final double tempC = tempB + tempA; final double result; if (extra != 0.0) { result = tempC*extra*z + tempC*extra + tempC*z + tempB + tempA; } else { result = tempC*z + tempB + tempA; } if (hiPrec != null) { // If requesting high precision hiPrec[0] = tempA; hiPrec[1] = tempC*extra*z + tempC*extra + tempC*z + tempB; } return result; } /** Compute exp(x) - 1 * @param x number to compute shifted exponential * @return exp(x) - 1 */ public static double expm1(double x) { return expm1(x, null); } /** Internal helper method for expm1 * @param x number to compute shifted exponential * @param hiPrecOut receive high precision result for -1.0 < x < 1.0 * @return exp(x) - 1 */ private static double expm1(double x, double hiPrecOut[]) { if (x != x || x == 0.0) { // NaN or zero return x; } if (x <= -1.0 || x >= 1.0) { // If not between +/- 1.0 //return exp(x) - 1.0; double hiPrec[] = new double[2]; exp(x, 0.0, hiPrec); if (x > 0.0) { return -1.0 + hiPrec[0] + hiPrec[1]; } else { final double ra = -1.0 + hiPrec[0]; double rb = -(ra + 1.0 - hiPrec[0]); rb += hiPrec[1]; return ra + rb; } } double baseA; double baseB; double epsilon; boolean negative = false; if (x < 0.0) { x = -x; negative = true; } { int intFrac = (int) (x * 1024.0); double tempA = ExpFracTable.EXP_FRAC_TABLE_A[intFrac] - 1.0; double tempB = ExpFracTable.EXP_FRAC_TABLE_B[intFrac]; double temp = tempA + tempB; tempB = -(temp - tempA - tempB); tempA = temp; temp = tempA * HEX_40000000; baseA = tempA + temp - temp; baseB = tempB + (tempA - baseA); epsilon = x - intFrac/1024.0; } /* Compute expm1(epsilon) */ double zb = 0.008336750013465571; zb = zb * epsilon + 0.041666663879186654; zb = zb * epsilon + 0.16666666666745392; zb = zb * epsilon + 0.49999999999999994; zb = zb * epsilon; zb = zb * epsilon; double za = epsilon; double temp = za + zb; zb = -(temp - za - zb); za = temp; temp = za * HEX_40000000; temp = za + temp - temp; zb += za - temp; za = temp; /* Combine the parts. expm1(a+b) = expm1(a) + expm1(b) + expm1(a)*expm1(b) */ double ya = za * baseA; //double yb = za*baseB + zb*baseA + zb*baseB; temp = ya + za * baseB; double yb = -(temp - ya - za * baseB); ya = temp; temp = ya + zb * baseA; yb += -(temp - ya - zb * baseA); ya = temp; temp = ya + zb * baseB; yb += -(temp - ya - zb*baseB); ya = temp; //ya = ya + za + baseA; //yb = yb + zb + baseB; temp = ya + baseA; yb += -(temp - baseA - ya); ya = temp; temp = ya + za; //yb += (ya > za) ? -(temp - ya - za) : -(temp - za - ya); yb += -(temp - ya - za); ya = temp; temp = ya + baseB; //yb += (ya > baseB) ? -(temp - ya - baseB) : -(temp - baseB - ya); yb += -(temp - ya - baseB); ya = temp; temp = ya + zb; //yb += (ya > zb) ? -(temp - ya - zb) : -(temp - zb - ya); yb += -(temp - ya - zb); ya = temp; if (negative) { /* Compute expm1(-x) = -expm1(x) / (expm1(x) + 1) */ double denom = 1.0 + ya; double denomr = 1.0 / denom; double denomb = -(denom - 1.0 - ya) + yb; double ratio = ya * denomr; temp = ratio * HEX_40000000; final double ra = ratio + temp - temp; double rb = ratio - ra; temp = denom * HEX_40000000; za = denom + temp - temp; zb = denom - za; rb += (ya - za * ra - za * rb - zb * ra - zb * rb) * denomr; // f(x) = x/1+x // Compute f'(x) // Product rule: d(uv) = du*v + u*dv // Chain rule: d(f(g(x)) = f'(g(x))*f(g'(x)) // d(1/x) = -1/(x*x) // d(1/1+x) = -1/( (1+x)^2) * 1 = -1/((1+x)*(1+x)) // d(x/1+x) = -x/((1+x)(1+x)) + 1/1+x = 1 / ((1+x)(1+x)) // Adjust for yb rb += yb * denomr; // numerator rb += -ya * denomb * denomr * denomr; // denominator // negate ya = -ra; yb = -rb; } if (hiPrecOut != null) { hiPrecOut[0] = ya; hiPrecOut[1] = yb; } return ya + yb; } /** * Natural logarithm. * * @param x a double * @return log(x) */ public static double log(final double x) { return log(x, null); } /** * Internal helper method for natural logarithm function. * @param x original argument of the natural logarithm function * @param hiPrec extra bits of precision on output (To Be Confirmed) * @return log(x) */ private static double log(final double x, final double[] hiPrec) { if (x==0) { // Handle special case of +0/-0 return Double.NEGATIVE_INFINITY; } long bits = Double.doubleToRawLongBits(x); /* Handle special cases of negative input, and NaN */ if (((bits & 0x8000000000000000L) != 0 || x != x) && x != 0.0) { if (hiPrec != null) { hiPrec[0] = Double.NaN; } return Double.NaN; } /* Handle special cases of Positive infinity. */ if (x == Double.POSITIVE_INFINITY) { if (hiPrec != null) { hiPrec[0] = Double.POSITIVE_INFINITY; } return Double.POSITIVE_INFINITY; } /* Extract the exponent */ int exp = (int)(bits >> 52)-1023; if ((bits & 0x7ff0000000000000L) == 0) { // Subnormal! if (x == 0) { // Zero if (hiPrec != null) { hiPrec[0] = Double.NEGATIVE_INFINITY; } return Double.NEGATIVE_INFINITY; } /* Normalize the subnormal number. */ bits <<= 1; while ( (bits & 0x0010000000000000L) == 0) { --exp; bits <<= 1; } } if ((exp == -1 || exp == 0) && x < 1.01 && x > 0.99 && hiPrec == null) { /* The normal method doesn't work well in the range [0.99, 1.01], so call do a straight polynomial expansion in higer precision. */ /* Compute x - 1.0 and split it */ double xa = x - 1.0; double xb = xa - x + 1.0; double tmp = xa * HEX_40000000; double aa = xa + tmp - tmp; double ab = xa - aa; xa = aa; xb = ab; final double[] lnCoef_last = LN_QUICK_COEF[LN_QUICK_COEF.length - 1]; double ya = lnCoef_last[0]; double yb = lnCoef_last[1]; for (int i = LN_QUICK_COEF.length - 2; i >= 0; i--) { /* Multiply a = y * x */ aa = ya * xa; ab = ya * xb + yb * xa + yb * xb; /* split, so now y = a */ tmp = aa * HEX_40000000; ya = aa + tmp - tmp; yb = aa - ya + ab; /* Add a = y + lnQuickCoef */ final double[] lnCoef_i = LN_QUICK_COEF[i]; aa = ya + lnCoef_i[0]; ab = yb + lnCoef_i[1]; /* Split y = a */ tmp = aa * HEX_40000000; ya = aa + tmp - tmp; yb = aa - ya + ab; } /* Multiply a = y * x */ aa = ya * xa; ab = ya * xb + yb * xa + yb * xb; /* split, so now y = a */ tmp = aa * HEX_40000000; ya = aa + tmp - tmp; yb = aa - ya + ab; return ya + yb; } // lnm is a log of a number in the range of 1.0 - 2.0, so 0 <= lnm < ln(2) final double[] lnm = lnMant.LN_MANT[(int)((bits & 0x000ffc0000000000L) >> 42)]; /* double epsilon = x / Double.longBitsToDouble(bits & 0xfffffc0000000000L); epsilon -= 1.0; */ // y is the most significant 10 bits of the mantissa //double y = Double.longBitsToDouble(bits & 0xfffffc0000000000L); //double epsilon = (x - y) / y; final double epsilon = (bits & 0x3ffffffffffL) / (TWO_POWER_52 + (bits & 0x000ffc0000000000L)); double lnza = 0.0; double lnzb = 0.0; if (hiPrec != null) { /* split epsilon -> x */ double tmp = epsilon * HEX_40000000; double aa = epsilon + tmp - tmp; double ab = epsilon - aa; double xa = aa; double xb = ab; /* Need a more accurate epsilon, so adjust the division. */ final double numer = bits & 0x3ffffffffffL; final double denom = TWO_POWER_52 + (bits & 0x000ffc0000000000L); aa = numer - xa*denom - xb * denom; xb += aa / denom; /* Remez polynomial evaluation */ final double[] lnCoef_last = LN_HI_PREC_COEF[LN_HI_PREC_COEF.length-1]; double ya = lnCoef_last[0]; double yb = lnCoef_last[1]; for (int i = LN_HI_PREC_COEF.length - 2; i >= 0; i--) { /* Multiply a = y * x */ aa = ya * xa; ab = ya * xb + yb * xa + yb * xb; /* split, so now y = a */ tmp = aa * HEX_40000000; ya = aa + tmp - tmp; yb = aa - ya + ab; /* Add a = y + lnHiPrecCoef */ final double[] lnCoef_i = LN_HI_PREC_COEF[i]; aa = ya + lnCoef_i[0]; ab = yb + lnCoef_i[1]; /* Split y = a */ tmp = aa * HEX_40000000; ya = aa + tmp - tmp; yb = aa - ya + ab; } /* Multiply a = y * x */ aa = ya * xa; ab = ya * xb + yb * xa + yb * xb; /* split, so now lnz = a */ /* tmp = aa * 1073741824.0; lnza = aa + tmp - tmp; lnzb = aa - lnza + ab; */ lnza = aa + ab; lnzb = -(lnza - aa - ab); } else { /* High precision not required. Eval Remez polynomial using standard double precision */ lnza = -0.16624882440418567; lnza = lnza * epsilon + 0.19999954120254515; lnza = lnza * epsilon + -0.2499999997677497; lnza = lnza * epsilon + 0.3333333333332802; lnza = lnza * epsilon + -0.5; lnza = lnza * epsilon + 1.0; lnza = lnza * epsilon; } /* Relative sizes: * lnzb [0, 2.33E-10] * lnm[1] [0, 1.17E-7] * ln2B*exp [0, 1.12E-4] * lnza [0, 9.7E-4] * lnm[0] [0, 0.692] * ln2A*exp [0, 709] */ /* Compute the following sum: * lnzb + lnm[1] + ln2B*exp + lnza + lnm[0] + ln2A*exp; */ //return lnzb + lnm[1] + ln2B*exp + lnza + lnm[0] + ln2A*exp; double a = LN_2_A*exp; double b = 0.0; double c = a+lnm[0]; double d = -(c-a-lnm[0]); a = c; b = b + d; c = a + lnza; d = -(c - a - lnza); a = c; b = b + d; c = a + LN_2_B*exp; d = -(c - a - LN_2_B*exp); a = c; b = b + d; c = a + lnm[1]; d = -(c - a - lnm[1]); a = c; b = b + d; c = a + lnzb; d = -(c - a - lnzb); a = c; b = b + d; if (hiPrec != null) { hiPrec[0] = a; hiPrec[1] = b; } return a + b; } /** * Computes log(1 + x). * * @param x Number. * @return {@code log(1 + x)}. */ public static double log1p(final double x) { if (x == -1) { return Double.NEGATIVE_INFINITY; } if (x == Double.POSITIVE_INFINITY) { return Double.POSITIVE_INFINITY; } if (x > 1e-6 || x < -1e-6) { final double xpa = 1 + x; final double xpb = -(xpa - 1 - x); final double[] hiPrec = new double[2]; final double lores = log(xpa, hiPrec); if (Double.isInfinite(lores)) { // Don't allow this to be converted to NaN return lores; } // Do a taylor series expansion around xpa: // f(x+y) = f(x) + f'(x) y + f''(x)/2 y^2 final double fx1 = xpb / xpa; final double epsilon = 0.5 * fx1 + 1; return epsilon * fx1 + hiPrec[1] + hiPrec[0]; } else { // Value is small |x| < 1e6, do a Taylor series centered on 1. final double y = (x * F_1_3 - F_1_2) * x + 1; return y * x; } } /** Compute the base 10 logarithm. * @param x a number * @return log10(x) */ public static double log10(final double x) { final double hiPrec[] = new double[2]; final double lores = log(x, hiPrec); if (Double.isInfinite(lores)){ // don't allow this to be converted to NaN return lores; } final double tmp = hiPrec[0] * HEX_40000000; final double lna = hiPrec[0] + tmp - tmp; final double lnb = hiPrec[0] - lna + hiPrec[1]; final double rln10a = 0.4342944622039795; final double rln10b = 1.9699272335463627E-8; return rln10b * lnb + rln10b * lna + rln10a * lnb + rln10a * lna; } /** * Computes the * logarithm in a given base. * * Returns {@code NaN} if either argument is negative. * If {@code base} is 0 and {@code x} is positive, 0 is returned. * If {@code base} is positive and {@code x} is 0, * {@code Double.NEGATIVE_INFINITY} is returned. * If both arguments are 0, the result is {@code NaN}. * * @param base Base of the logarithm, must be greater than 0. * @param x Argument, must be greater than 0. * @return the value of the logarithm, i.e. the number {@code y} such that * basey = x. * @since 1.2 (previously in {@code MathUtils}, moved as of version 3.0) */ public static double log(double base, double x) { return log(x) / log(base); } /** * Power function. Compute x^y. * * @param x a double * @param y a double * @return double */ public static double pow(double x, double y) { final double lns[] = new double[2]; if (y == 0.0) { return 1.0; } if (x != x) { // X is NaN return x; } if (x == 0) { long bits = Double.doubleToRawLongBits(x); if ((bits & 0x8000000000000000L) != 0) { // -zero long yi = (long) y; if (y < 0 && y == yi && (yi & 1) == 1) { return Double.NEGATIVE_INFINITY; } if (y > 0 && y == yi && (yi & 1) == 1) { return -0.0; } } if (y < 0) { return Double.POSITIVE_INFINITY; } if (y > 0) { return 0.0; } return Double.NaN; } if (x == Double.POSITIVE_INFINITY) { if (y != y) { // y is NaN return y; } if (y < 0.0) { return 0.0; } else { return Double.POSITIVE_INFINITY; } } if (y == Double.POSITIVE_INFINITY) { if (x * x == 1.0) { return Double.NaN; } if (x * x > 1.0) { return Double.POSITIVE_INFINITY; } else { return 0.0; } } if (x == Double.NEGATIVE_INFINITY) { if (y != y) { // y is NaN return y; } if (y < 0) { long yi = (long) y; if (y == yi && (yi & 1) == 1) { return -0.0; } return 0.0; } if (y > 0) { long yi = (long) y; if (y == yi && (yi & 1) == 1) { return Double.NEGATIVE_INFINITY; } return Double.POSITIVE_INFINITY; } } if (y == Double.NEGATIVE_INFINITY) { if (x * x == 1.0) { return Double.NaN; } if (x * x < 1.0) { return Double.POSITIVE_INFINITY; } else { return 0.0; } } /* Handle special case x<0 */ if (x < 0) { // y is an even integer in this case if (y >= TWO_POWER_53 || y <= -TWO_POWER_53) { return pow(-x, y); } if (y == (long) y) { // If y is an integer return ((long)y & 1) == 0 ? pow(-x, y) : -pow(-x, y); } else { return Double.NaN; } } /* Split y into ya and yb such that y = ya+yb */ double ya; double yb; if (y < 8e298 && y > -8e298) { double tmp1 = y * HEX_40000000; ya = y + tmp1 - tmp1; yb = y - ya; } else { double tmp1 = y * 9.31322574615478515625E-10; double tmp2 = tmp1 * 9.31322574615478515625E-10; ya = (tmp1 + tmp2 - tmp1) * HEX_40000000 * HEX_40000000; yb = y - ya; } /* Compute ln(x) */ final double lores = log(x, lns); if (Double.isInfinite(lores)){ // don't allow this to be converted to NaN return lores; } double lna = lns[0]; double lnb = lns[1]; /* resplit lns */ double tmp1 = lna * HEX_40000000; double tmp2 = lna + tmp1 - tmp1; lnb += lna - tmp2; lna = tmp2; // y*ln(x) = (aa+ab) final double aa = lna * ya; final double ab = lna * yb + lnb * ya + lnb * yb; lna = aa+ab; lnb = -(lna - aa - ab); double z = 1.0 / 120.0; z = z * lnb + (1.0 / 24.0); z = z * lnb + (1.0 / 6.0); z = z * lnb + 0.5; z = z * lnb + 1.0; z = z * lnb; final double result = exp(lna, z, null); //result = result + result * z; return result; } /** * Raise a double to an int power. * * @param d Number to raise. * @param e Exponent. * @return de * @since 3.1 */ public static double pow(double d, int e) { if (e == 0) { return 1.0; } else if (e < 0) { e = -e; d = 1.0 / d; } // split d as two 26 bits numbers // beware the following expressions must NOT be simplified, they rely on floating point arithmetic properties final int splitFactor = 0x8000001; final double cd = splitFactor * d; final double d1High = cd - (cd - d); final double d1Low = d - d1High; // prepare result double resultHigh = 1; double resultLow = 0; // d^(2p) double d2p = d; double d2pHigh = d1High; double d2pLow = d1Low; while (e != 0) { if ((e & 0x1) != 0) { // accurate multiplication result = result * d^(2p) using Veltkamp TwoProduct algorithm // beware the following expressions must NOT be simplified, they rely on floating point arithmetic properties final double tmpHigh = resultHigh * d2p; final double cRH = splitFactor * resultHigh; final double rHH = cRH - (cRH - resultHigh); final double rHL = resultHigh - rHH; final double tmpLow = rHL * d2pLow - (((tmpHigh - rHH * d2pHigh) - rHL * d2pHigh) - rHH * d2pLow); resultHigh = tmpHigh; resultLow = resultLow * d2p + tmpLow; } // accurate squaring d^(2(p+1)) = d^(2p) * d^(2p) using Veltkamp TwoProduct algorithm // beware the following expressions must NOT be simplified, they rely on floating point arithmetic properties final double tmpHigh = d2pHigh * d2p; final double cD2pH = splitFactor * d2pHigh; final double d2pHH = cD2pH - (cD2pH - d2pHigh); final double d2pHL = d2pHigh - d2pHH; final double tmpLow = d2pHL * d2pLow - (((tmpHigh - d2pHH * d2pHigh) - d2pHL * d2pHigh) - d2pHH * d2pLow); final double cTmpH = splitFactor * tmpHigh; d2pHigh = cTmpH - (cTmpH - tmpHigh); d2pLow = d2pLow * d2p + tmpLow + (tmpHigh - d2pHigh); d2p = d2pHigh + d2pLow; e = e >> 1; } return resultHigh + resultLow; } /** * Computes sin(x) - x, where |x| < 1/16. * Use a Remez polynomial approximation. * @param x a number smaller than 1/16 * @return sin(x) - x */ private static double polySine(final double x) { double x2 = x*x; double p = 2.7553817452272217E-6; p = p * x2 + -1.9841269659586505E-4; p = p * x2 + 0.008333333333329196; p = p * x2 + -0.16666666666666666; //p *= x2; //p *= x; p = p * x2 * x; return p; } /** * Computes cos(x) - 1, where |x| < 1/16. * Use a Remez polynomial approximation. * @param x a number smaller than 1/16 * @return cos(x) - 1 */ private static double polyCosine(double x) { double x2 = x*x; double p = 2.479773539153719E-5; p = p * x2 + -0.0013888888689039883; p = p * x2 + 0.041666666666621166; p = p * x2 + -0.49999999999999994; p *= x2; return p; } /** * Compute sine over the first quadrant (0 < x < pi/2). * Use combination of table lookup and rational polynomial expansion. * @param xa number from which sine is requested * @param xb extra bits for x (may be 0.0) * @return sin(xa + xb) */ private static double sinQ(double xa, double xb) { int idx = (int) ((xa * 8.0) + 0.5); final double epsilon = xa - EIGHTHS[idx]; //idx*0.125; // Table lookups final double sintA = SINE_TABLE_A[idx]; final double sintB = SINE_TABLE_B[idx]; final double costA = COSINE_TABLE_A[idx]; final double costB = COSINE_TABLE_B[idx]; // Polynomial eval of sin(epsilon), cos(epsilon) double sinEpsA = epsilon; double sinEpsB = polySine(epsilon); final double cosEpsA = 1.0; final double cosEpsB = polyCosine(epsilon); // Split epsilon xa + xb = x final double temp = sinEpsA * HEX_40000000; double temp2 = (sinEpsA + temp) - temp; sinEpsB += sinEpsA - temp2; sinEpsA = temp2; /* Compute sin(x) by angle addition formula */ double result; /* Compute the following sum: * * result = sintA + costA*sinEpsA + sintA*cosEpsB + costA*sinEpsB + * sintB + costB*sinEpsA + sintB*cosEpsB + costB*sinEpsB; * * Ranges of elements * * xxxtA 0 PI/2 * xxxtB -1.5e-9 1.5e-9 * sinEpsA -0.0625 0.0625 * sinEpsB -6e-11 6e-11 * cosEpsA 1.0 * cosEpsB 0 -0.0625 * */ //result = sintA + costA*sinEpsA + sintA*cosEpsB + costA*sinEpsB + // sintB + costB*sinEpsA + sintB*cosEpsB + costB*sinEpsB; //result = sintA + sintA*cosEpsB + sintB + sintB * cosEpsB; //result += costA*sinEpsA + costA*sinEpsB + costB*sinEpsA + costB * sinEpsB; double a = 0; double b = 0; double t = sintA; double c = a + t; double d = -(c - a - t); a = c; b = b + d; t = costA * sinEpsA; c = a + t; d = -(c - a - t); a = c; b = b + d; b = b + sintA * cosEpsB + costA * sinEpsB; /* t = sintA*cosEpsB; c = a + t; d = -(c - a - t); a = c; b = b + d; t = costA*sinEpsB; c = a + t; d = -(c - a - t); a = c; b = b + d; */ b = b + sintB + costB * sinEpsA + sintB * cosEpsB + costB * sinEpsB; /* t = sintB; c = a + t; d = -(c - a - t); a = c; b = b + d; t = costB*sinEpsA; c = a + t; d = -(c - a - t); a = c; b = b + d; t = sintB*cosEpsB; c = a + t; d = -(c - a - t); a = c; b = b + d; t = costB*sinEpsB; c = a + t; d = -(c - a - t); a = c; b = b + d; */ if (xb != 0.0) { t = ((costA + costB) * (cosEpsA + cosEpsB) - (sintA + sintB) * (sinEpsA + sinEpsB)) * xb; // approximate cosine*xb c = a + t; d = -(c - a - t); a = c; b = b + d; } result = a + b; return result; } /** * Compute cosine in the first quadrant by subtracting input from PI/2 and * then calling sinQ. This is more accurate as the input approaches PI/2. * @param xa number from which cosine is requested * @param xb extra bits for x (may be 0.0) * @return cos(xa + xb) */ private static double cosQ(double xa, double xb) { final double pi2a = 1.5707963267948966; final double pi2b = 6.123233995736766E-17; final double a = pi2a - xa; double b = -(a - pi2a + xa); b += pi2b - xb; return sinQ(a, b); } /** * Compute tangent (or cotangent) over the first quadrant. 0 < x < pi/2 * Use combination of table lookup and rational polynomial expansion. * @param xa number from which sine is requested * @param xb extra bits for x (may be 0.0) * @param cotanFlag if true, compute the cotangent instead of the tangent * @return tan(xa+xb) (or cotangent, depending on cotanFlag) */ private static double tanQ(double xa, double xb, boolean cotanFlag) { int idx = (int) ((xa * 8.0) + 0.5); final double epsilon = xa - EIGHTHS[idx]; //idx*0.125; // Table lookups final double sintA = SINE_TABLE_A[idx]; final double sintB = SINE_TABLE_B[idx]; final double costA = COSINE_TABLE_A[idx]; final double costB = COSINE_TABLE_B[idx]; // Polynomial eval of sin(epsilon), cos(epsilon) double sinEpsA = epsilon; double sinEpsB = polySine(epsilon); final double cosEpsA = 1.0; final double cosEpsB = polyCosine(epsilon); // Split epsilon xa + xb = x double temp = sinEpsA * HEX_40000000; double temp2 = (sinEpsA + temp) - temp; sinEpsB += sinEpsA - temp2; sinEpsA = temp2; /* Compute sin(x) by angle addition formula */ /* Compute the following sum: * * result = sintA + costA*sinEpsA + sintA*cosEpsB + costA*sinEpsB + * sintB + costB*sinEpsA + sintB*cosEpsB + costB*sinEpsB; * * Ranges of elements * * xxxtA 0 PI/2 * xxxtB -1.5e-9 1.5e-9 * sinEpsA -0.0625 0.0625 * sinEpsB -6e-11 6e-11 * cosEpsA 1.0 * cosEpsB 0 -0.0625 * */ //result = sintA + costA*sinEpsA + sintA*cosEpsB + costA*sinEpsB + // sintB + costB*sinEpsA + sintB*cosEpsB + costB*sinEpsB; //result = sintA + sintA*cosEpsB + sintB + sintB * cosEpsB; //result += costA*sinEpsA + costA*sinEpsB + costB*sinEpsA + costB * sinEpsB; double a = 0; double b = 0; // Compute sine double t = sintA; double c = a + t; double d = -(c - a - t); a = c; b = b + d; t = costA*sinEpsA; c = a + t; d = -(c - a - t); a = c; b = b + d; b = b + sintA*cosEpsB + costA*sinEpsB; b = b + sintB + costB*sinEpsA + sintB*cosEpsB + costB*sinEpsB; double sina = a + b; double sinb = -(sina - a - b); // Compute cosine a = b = c = d = 0.0; t = costA*cosEpsA; c = a + t; d = -(c - a - t); a = c; b = b + d; t = -sintA*sinEpsA; c = a + t; d = -(c - a - t); a = c; b = b + d; b = b + costB*cosEpsA + costA*cosEpsB + costB*cosEpsB; b = b - (sintB*sinEpsA + sintA*sinEpsB + sintB*sinEpsB); double cosa = a + b; double cosb = -(cosa - a - b); if (cotanFlag) { double tmp; tmp = cosa; cosa = sina; sina = tmp; tmp = cosb; cosb = sinb; sinb = tmp; } /* estimate and correct, compute 1.0/(cosa+cosb) */ /* double est = (sina+sinb)/(cosa+cosb); double err = (sina - cosa*est) + (sinb - cosb*est); est += err/(cosa+cosb); err = (sina - cosa*est) + (sinb - cosb*est); */ // f(x) = 1/x, f'(x) = -1/x^2 double est = sina/cosa; /* Split the estimate to get more accurate read on division rounding */ temp = est * HEX_40000000; double esta = (est + temp) - temp; double estb = est - esta; temp = cosa * HEX_40000000; double cosaa = (cosa + temp) - temp; double cosab = cosa - cosaa; //double err = (sina - est*cosa)/cosa; // Correction for division rounding double err = (sina - esta*cosaa - esta*cosab - estb*cosaa - estb*cosab)/cosa; // Correction for division rounding err += sinb/cosa; // Change in est due to sinb err += -sina * cosb / cosa / cosa; // Change in est due to cosb if (xb != 0.0) { // tan' = 1 + tan^2 cot' = -(1 + cot^2) // Approximate impact of xb double xbadj = xb + est*est*xb; if (cotanFlag) { xbadj = -xbadj; } err += xbadj; } return est+err; } /** Reduce the input argument using the Payne and Hanek method. * This is good for all inputs 0.0 < x < inf * Output is remainder after dividing by PI/2 * The result array should contain 3 numbers. * result[0] is the integer portion, so mod 4 this gives the quadrant. * result[1] is the upper bits of the remainder * result[2] is the lower bits of the remainder * * @param x number to reduce * @param result placeholder where to put the result */ private static void reducePayneHanek(double x, double result[]) { /* Convert input double to bits */ long inbits = Double.doubleToRawLongBits(x); int exponent = (int) ((inbits >> 52) & 0x7ff) - 1023; /* Convert to fixed point representation */ inbits &= 0x000fffffffffffffL; inbits |= 0x0010000000000000L; /* Normalize input to be between 0.5 and 1.0 */ exponent++; inbits <<= 11; /* Based on the exponent, get a shifted copy of recip2pi */ long shpi0; long shpiA; long shpiB; int idx = exponent >> 6; int shift = exponent - (idx << 6); if (shift != 0) { shpi0 = (idx == 0) ? 0 : (RECIP_2PI[idx-1] << shift); shpi0 |= RECIP_2PI[idx] >>> (64-shift); shpiA = (RECIP_2PI[idx] << shift) | (RECIP_2PI[idx+1] >>> (64-shift)); shpiB = (RECIP_2PI[idx+1] << shift) | (RECIP_2PI[idx+2] >>> (64-shift)); } else { shpi0 = (idx == 0) ? 0 : RECIP_2PI[idx-1]; shpiA = RECIP_2PI[idx]; shpiB = RECIP_2PI[idx+1]; } /* Multiply input by shpiA */ long a = inbits >>> 32; long b = inbits & 0xffffffffL; long c = shpiA >>> 32; long d = shpiA & 0xffffffffL; long ac = a * c; long bd = b * d; long bc = b * c; long ad = a * d; long prodB = bd + (ad << 32); long prodA = ac + (ad >>> 32); boolean bita = (bd & 0x8000000000000000L) != 0; boolean bitb = (ad & 0x80000000L ) != 0; boolean bitsum = (prodB & 0x8000000000000000L) != 0; /* Carry */ if ( (bita && bitb) || ((bita || bitb) && !bitsum) ) { prodA++; } bita = (prodB & 0x8000000000000000L) != 0; bitb = (bc & 0x80000000L ) != 0; prodB = prodB + (bc << 32); prodA = prodA + (bc >>> 32); bitsum = (prodB & 0x8000000000000000L) != 0; /* Carry */ if ( (bita && bitb) || ((bita || bitb) && !bitsum) ) { prodA++; } /* Multiply input by shpiB */ c = shpiB >>> 32; d = shpiB & 0xffffffffL; ac = a * c; bc = b * c; ad = a * d; /* Collect terms */ ac = ac + ((bc + ad) >>> 32); bita = (prodB & 0x8000000000000000L) != 0; bitb = (ac & 0x8000000000000000L ) != 0; prodB += ac; bitsum = (prodB & 0x8000000000000000L) != 0; /* Carry */ if ( (bita && bitb) || ((bita || bitb) && !bitsum) ) { prodA++; } /* Multiply by shpi0 */ c = shpi0 >>> 32; d = shpi0 & 0xffffffffL; bd = b * d; bc = b * c; ad = a * d; prodA += bd + ((bc + ad) << 32); /* * prodA, prodB now contain the remainder as a fraction of PI. We want this as a fraction of * PI/2, so use the following steps: * 1.) multiply by 4. * 2.) do a fixed point muliply by PI/4. * 3.) Convert to floating point. * 4.) Multiply by 2 */ /* This identifies the quadrant */ int intPart = (int)(prodA >>> 62); /* Multiply by 4 */ prodA <<= 2; prodA |= prodB >>> 62; prodB <<= 2; /* Multiply by PI/4 */ a = prodA >>> 32; b = prodA & 0xffffffffL; c = PI_O_4_BITS[0] >>> 32; d = PI_O_4_BITS[0] & 0xffffffffL; ac = a * c; bd = b * d; bc = b * c; ad = a * d; long prod2B = bd + (ad << 32); long prod2A = ac + (ad >>> 32); bita = (bd & 0x8000000000000000L) != 0; bitb = (ad & 0x80000000L ) != 0; bitsum = (prod2B & 0x8000000000000000L) != 0; /* Carry */ if ( (bita && bitb) || ((bita || bitb) && !bitsum) ) { prod2A++; } bita = (prod2B & 0x8000000000000000L) != 0; bitb = (bc & 0x80000000L ) != 0; prod2B = prod2B + (bc << 32); prod2A = prod2A + (bc >>> 32); bitsum = (prod2B & 0x8000000000000000L) != 0; /* Carry */ if ( (bita && bitb) || ((bita || bitb) && !bitsum) ) { prod2A++; } /* Multiply input by pio4bits[1] */ c = PI_O_4_BITS[1] >>> 32; d = PI_O_4_BITS[1] & 0xffffffffL; ac = a * c; bc = b * c; ad = a * d; /* Collect terms */ ac = ac + ((bc + ad) >>> 32); bita = (prod2B & 0x8000000000000000L) != 0; bitb = (ac & 0x8000000000000000L ) != 0; prod2B += ac; bitsum = (prod2B & 0x8000000000000000L) != 0; /* Carry */ if ( (bita && bitb) || ((bita || bitb) && !bitsum) ) { prod2A++; } /* Multiply inputB by pio4bits[0] */ a = prodB >>> 32; b = prodB & 0xffffffffL; c = PI_O_4_BITS[0] >>> 32; d = PI_O_4_BITS[0] & 0xffffffffL; ac = a * c; bc = b * c; ad = a * d; /* Collect terms */ ac = ac + ((bc + ad) >>> 32); bita = (prod2B & 0x8000000000000000L) != 0; bitb = (ac & 0x8000000000000000L ) != 0; prod2B += ac; bitsum = (prod2B & 0x8000000000000000L) != 0; /* Carry */ if ( (bita && bitb) || ((bita || bitb) && !bitsum) ) { prod2A++; } /* Convert to double */ double tmpA = (prod2A >>> 12) / TWO_POWER_52; // High order 52 bits double tmpB = (((prod2A & 0xfffL) << 40) + (prod2B >>> 24)) / TWO_POWER_52 / TWO_POWER_52; // Low bits double sumA = tmpA + tmpB; double sumB = -(sumA - tmpA - tmpB); /* Multiply by PI/2 and return */ result[0] = intPart; result[1] = sumA * 2.0; result[2] = sumB * 2.0; } /** * Sine function. * * @param x Argument. * @return sin(x) */ public static double sin(double x) { boolean negative = false; int quadrant = 0; double xa; double xb = 0.0; /* Take absolute value of the input */ xa = x; if (x < 0) { negative = true; xa = -xa; } /* Check for zero and negative zero */ if (xa == 0.0) { long bits = Double.doubleToRawLongBits(x); if (bits < 0) { return -0.0; } return 0.0; } if (xa != xa || xa == Double.POSITIVE_INFINITY) { return Double.NaN; } /* Perform any argument reduction */ if (xa > 3294198.0) { // PI * (2**20) // Argument too big for CodyWaite reduction. Must use // PayneHanek. double reduceResults[] = new double[3]; reducePayneHanek(xa, reduceResults); quadrant = ((int) reduceResults[0]) & 3; xa = reduceResults[1]; xb = reduceResults[2]; } else if (xa > 1.5707963267948966) { final CodyWaite cw = new CodyWaite(xa); quadrant = cw.getK() & 3; xa = cw.getRemA(); xb = cw.getRemB(); } if (negative) { quadrant ^= 2; // Flip bit 1 } switch (quadrant) { case 0: return sinQ(xa, xb); case 1: return cosQ(xa, xb); case 2: return -sinQ(xa, xb); case 3: return -cosQ(xa, xb); default: return Double.NaN; } } /** * Cosine function. * * @param x Argument. * @return cos(x) */ public static double cos(double x) { int quadrant = 0; /* Take absolute value of the input */ double xa = x; if (x < 0) { xa = -xa; } if (xa != xa || xa == Double.POSITIVE_INFINITY) { return Double.NaN; } /* Perform any argument reduction */ double xb = 0; if (xa > 3294198.0) { // PI * (2**20) // Argument too big for CodyWaite reduction. Must use // PayneHanek. double reduceResults[] = new double[3]; reducePayneHanek(xa, reduceResults); quadrant = ((int) reduceResults[0]) & 3; xa = reduceResults[1]; xb = reduceResults[2]; } else if (xa > 1.5707963267948966) { final CodyWaite cw = new CodyWaite(xa); quadrant = cw.getK() & 3; xa = cw.getRemA(); xb = cw.getRemB(); } //if (negative) // quadrant = (quadrant + 2) % 4; switch (quadrant) { case 0: return cosQ(xa, xb); case 1: return -sinQ(xa, xb); case 2: return -cosQ(xa, xb); case 3: return sinQ(xa, xb); default: return Double.NaN; } } /** * Tangent function. * * @param x Argument. * @return tan(x) */ public static double tan(double x) { boolean negative = false; int quadrant = 0; /* Take absolute value of the input */ double xa = x; if (x < 0) { negative = true; xa = -xa; } /* Check for zero and negative zero */ if (xa == 0.0) { long bits = Double.doubleToRawLongBits(x); if (bits < 0) { return -0.0; } return 0.0; } if (xa != xa || xa == Double.POSITIVE_INFINITY) { return Double.NaN; } /* Perform any argument reduction */ double xb = 0; if (xa > 3294198.0) { // PI * (2**20) // Argument too big for CodyWaite reduction. Must use // PayneHanek. double reduceResults[] = new double[3]; reducePayneHanek(xa, reduceResults); quadrant = ((int) reduceResults[0]) & 3; xa = reduceResults[1]; xb = reduceResults[2]; } else if (xa > 1.5707963267948966) { final CodyWaite cw = new CodyWaite(xa); quadrant = cw.getK() & 3; xa = cw.getRemA(); xb = cw.getRemB(); } if (xa > 1.5) { // Accuracy suffers between 1.5 and PI/2 final double pi2a = 1.5707963267948966; final double pi2b = 6.123233995736766E-17; final double a = pi2a - xa; double b = -(a - pi2a + xa); b += pi2b - xb; xa = a + b; xb = -(xa - a - b); quadrant ^= 1; negative ^= true; } double result; if ((quadrant & 1) == 0) { result = tanQ(xa, xb, false); } else { result = -tanQ(xa, xb, true); } if (negative) { result = -result; } return result; } /** * Arctangent function * @param x a number * @return atan(x) */ public static double atan(double x) { return atan(x, 0.0, false); } /** Internal helper function to compute arctangent. * @param xa number from which arctangent is requested * @param xb extra bits for x (may be 0.0) * @param leftPlane if true, result angle must be put in the left half plane * @return atan(xa + xb) (or angle shifted by {@code PI} if leftPlane is true) */ private static double atan(double xa, double xb, boolean leftPlane) { boolean negate = false; int idx; if (xa == 0.0) { // Matches +/- 0.0; return correct sign return leftPlane ? copySign(Math.PI, xa) : xa; } if (xa < 0) { // negative xa = -xa; xb = -xb; negate = true; } if (xa > 1.633123935319537E16) { // Very large input return (negate ^ leftPlane) ? (-Math.PI * F_1_2) : (Math.PI * F_1_2); } /* Estimate the closest tabulated arctan value, compute eps = xa-tangentTable */ if (xa < 1) { idx = (int) (((-1.7168146928204136 * xa * xa + 8.0) * xa) + 0.5); } else { final double oneOverXa = 1 / xa; idx = (int) (-((-1.7168146928204136 * oneOverXa * oneOverXa + 8.0) * oneOverXa) + 13.07); } double epsA = xa - TANGENT_TABLE_A[idx]; double epsB = -(epsA - xa + TANGENT_TABLE_A[idx]); epsB += xb - TANGENT_TABLE_B[idx]; double temp = epsA + epsB; epsB = -(temp - epsA - epsB); epsA = temp; /* Compute eps = eps / (1.0 + xa*tangent) */ temp = xa * HEX_40000000; double ya = xa + temp - temp; double yb = xb + xa - ya; xa = ya; xb += yb; //if (idx > 8 || idx == 0) if (idx == 0) { /* If the slope of the arctan is gentle enough (< 0.45), this approximation will suffice */ //double denom = 1.0 / (1.0 + xa*tangentTableA[idx] + xb*tangentTableA[idx] + xa*tangentTableB[idx] + xb*tangentTableB[idx]); final double denom = 1d / (1d + (xa + xb) * (TANGENT_TABLE_A[idx] + TANGENT_TABLE_B[idx])); //double denom = 1.0 / (1.0 + xa*tangentTableA[idx]); ya = epsA * denom; yb = epsB * denom; } else { double temp2 = xa * TANGENT_TABLE_A[idx]; double za = 1d + temp2; double zb = -(za - 1d - temp2); temp2 = xb * TANGENT_TABLE_A[idx] + xa * TANGENT_TABLE_B[idx]; temp = za + temp2; zb += -(temp - za - temp2); za = temp; zb += xb * TANGENT_TABLE_B[idx]; ya = epsA / za; temp = ya * HEX_40000000; final double yaa = (ya + temp) - temp; final double yab = ya - yaa; temp = za * HEX_40000000; final double zaa = (za + temp) - temp; final double zab = za - zaa; /* Correct for rounding in division */ yb = (epsA - yaa * zaa - yaa * zab - yab * zaa - yab * zab) / za; yb += -epsA * zb / za / za; yb += epsB / za; } epsA = ya; epsB = yb; /* Evaluate polynomial */ final double epsA2 = epsA * epsA; /* yb = -0.09001346640161823; yb = yb * epsA2 + 0.11110718400605211; yb = yb * epsA2 + -0.1428571349122913; yb = yb * epsA2 + 0.19999999999273194; yb = yb * epsA2 + -0.33333333333333093; yb = yb * epsA2 * epsA; */ yb = 0.07490822288864472; yb = yb * epsA2 + -0.09088450866185192; yb = yb * epsA2 + 0.11111095942313305; yb = yb * epsA2 + -0.1428571423679182; yb = yb * epsA2 + 0.19999999999923582; yb = yb * epsA2 + -0.33333333333333287; yb = yb * epsA2 * epsA; ya = epsA; temp = ya + yb; yb = -(temp - ya - yb); ya = temp; /* Add in effect of epsB. atan'(x) = 1/(1+x^2) */ yb += epsB / (1d + epsA * epsA); //result = yb + eighths[idx] + ya; double za = EIGHTHS[idx] + ya; double zb = -(za - EIGHTHS[idx] - ya); temp = za + yb; zb += -(temp - za - yb); za = temp; double result = za + zb; double resultb = -(result - za - zb); if (leftPlane) { // Result is in the left plane final double pia = 1.5707963267948966 * 2; final double pib = 6.123233995736766E-17 * 2; za = pia - result; zb = -(za - pia + result); zb += pib - resultb; result = za + zb; resultb = -(result - za - zb); } if (negate ^ leftPlane) { result = -result; } return result; } /** * Two arguments arctangent function * @param y ordinate * @param x abscissa * @return phase angle of point (x,y) between {@code -PI} and {@code PI} */ public static double atan2(double y, double x) { if (x != x || y != y) { return Double.NaN; } if (y == 0) { final double result = x * y; final double invx = 1d / x; final double invy = 1d / y; if (invx == 0) { // X is infinite if (x > 0) { return y; // return +/- 0.0 } else { return copySign(Math.PI, y); } } if (x < 0 || invx < 0) { if (y < 0 || invy < 0) { return -Math.PI; } else { return Math.PI; } } else { return result; } } // y cannot now be zero if (y == Double.POSITIVE_INFINITY) { if (x == Double.POSITIVE_INFINITY) { return Math.PI * F_1_4; } if (x == Double.NEGATIVE_INFINITY) { return Math.PI * F_3_4; } return Math.PI * F_1_2; } if (y == Double.NEGATIVE_INFINITY) { if (x == Double.POSITIVE_INFINITY) { return -Math.PI * F_1_4; } if (x == Double.NEGATIVE_INFINITY) { return -Math.PI * F_3_4; } return -Math.PI * F_1_2; } if (x == Double.POSITIVE_INFINITY) { if (y > 0 || 1 / y > 0) { return 0d; } if (y < 0 || 1 / y < 0) { return -0d; } } if (x == Double.NEGATIVE_INFINITY) { if (y > 0.0 || 1 / y > 0.0) { return Math.PI; } if (y < 0 || 1 / y < 0) { return -Math.PI; } } // Neither y nor x can be infinite or NAN here if (x == 0) { if (y > 0 || 1 / y > 0) { return Math.PI * F_1_2; } if (y < 0 || 1 / y < 0) { return -Math.PI * F_1_2; } } // Compute ratio r = y/x final double r = y / x; if (Double.isInfinite(r)) { // bypass calculations that can create NaN return atan(r, 0, x < 0); } double ra = doubleHighPart(r); double rb = r - ra; // Split x final double xa = doubleHighPart(x); final double xb = x - xa; rb += (y - ra * xa - ra * xb - rb * xa - rb * xb) / x; final double temp = ra + rb; rb = -(temp - ra - rb); ra = temp; if (ra == 0) { // Fix up the sign so atan works correctly ra = copySign(0d, y); } // Call atan final double result = atan(ra, rb, x < 0); return result; } /** Compute the arc sine of a number. * @param x number on which evaluation is done * @return arc sine of x */ public static double asin(double x) { if (x != x) { return Double.NaN; } if (x > 1.0 || x < -1.0) { return Double.NaN; } if (x == 1.0) { return Math.PI/2.0; } if (x == -1.0) { return -Math.PI/2.0; } if (x == 0.0) { // Matches +/- 0.0; return correct sign return x; } /* Compute asin(x) = atan(x/sqrt(1-x*x)) */ /* Split x */ double temp = x * HEX_40000000; final double xa = x + temp - temp; final double xb = x - xa; /* Square it */ double ya = xa*xa; double yb = xa*xb*2.0 + xb*xb; /* Subtract from 1 */ ya = -ya; yb = -yb; double za = 1.0 + ya; double zb = -(za - 1.0 - ya); temp = za + yb; zb += -(temp - za - yb); za = temp; /* Square root */ double y; y = sqrt(za); temp = y * HEX_40000000; ya = y + temp - temp; yb = y - ya; /* Extend precision of sqrt */ yb += (za - ya*ya - 2*ya*yb - yb*yb) / (2.0*y); /* Contribution of zb to sqrt */ double dx = zb / (2.0*y); // Compute ratio r = x/y double r = x/y; temp = r * HEX_40000000; double ra = r + temp - temp; double rb = r - ra; rb += (x - ra*ya - ra*yb - rb*ya - rb*yb) / y; // Correct for rounding in division rb += -x * dx / y / y; // Add in effect additional bits of sqrt. temp = ra + rb; rb = -(temp - ra - rb); ra = temp; return atan(ra, rb, false); } /** Compute the arc cosine of a number. * @param x number on which evaluation is done * @return arc cosine of x */ public static double acos(double x) { if (x != x) { return Double.NaN; } if (x > 1.0 || x < -1.0) { return Double.NaN; } if (x == -1.0) { return Math.PI; } if (x == 1.0) { return 0.0; } if (x == 0) { return Math.PI/2.0; } /* Compute acos(x) = atan(sqrt(1-x*x)/x) */ /* Split x */ double temp = x * HEX_40000000; final double xa = x + temp - temp; final double xb = x - xa; /* Square it */ double ya = xa*xa; double yb = xa*xb*2.0 + xb*xb; /* Subtract from 1 */ ya = -ya; yb = -yb; double za = 1.0 + ya; double zb = -(za - 1.0 - ya); temp = za + yb; zb += -(temp - za - yb); za = temp; /* Square root */ double y = sqrt(za); temp = y * HEX_40000000; ya = y + temp - temp; yb = y - ya; /* Extend precision of sqrt */ yb += (za - ya*ya - 2*ya*yb - yb*yb) / (2.0*y); /* Contribution of zb to sqrt */ yb += zb / (2.0*y); y = ya+yb; yb = -(y - ya - yb); // Compute ratio r = y/x double r = y/x; // Did r overflow? if (Double.isInfinite(r)) { // x is effectively zero return Math.PI/2; // so return the appropriate value } double ra = doubleHighPart(r); double rb = r - ra; rb += (y - ra*xa - ra*xb - rb*xa - rb*xb) / x; // Correct for rounding in division rb += yb / x; // Add in effect additional bits of sqrt. temp = ra + rb; rb = -(temp - ra - rb); ra = temp; return atan(ra, rb, x<0); } /** Compute the cubic root of a number. * @param x number on which evaluation is done * @return cubic root of x */ public static double cbrt(double x) { /* Convert input double to bits */ long inbits = Double.doubleToRawLongBits(x); int exponent = (int) ((inbits >> 52) & 0x7ff) - 1023; boolean subnormal = false; if (exponent == -1023) { if (x == 0) { return x; } /* Subnormal, so normalize */ subnormal = true; x *= 1.8014398509481984E16; // 2^54 inbits = Double.doubleToRawLongBits(x); exponent = (int) ((inbits >> 52) & 0x7ff) - 1023; } if (exponent == 1024) { // Nan or infinity. Don't care which. return x; } /* Divide the exponent by 3 */ int exp3 = exponent / 3; /* p2 will be the nearest power of 2 to x with its exponent divided by 3 */ double p2 = Double.longBitsToDouble((inbits & 0x8000000000000000L) | (long)(((exp3 + 1023) & 0x7ff)) << 52); /* This will be a number between 1 and 2 */ final double mant = Double.longBitsToDouble((inbits & 0x000fffffffffffffL) | 0x3ff0000000000000L); /* Estimate the cube root of mant by polynomial */ double est = -0.010714690733195933; est = est * mant + 0.0875862700108075; est = est * mant + -0.3058015757857271; est = est * mant + 0.7249995199969751; est = est * mant + 0.5039018405998233; est *= CBRTTWO[exponent % 3 + 2]; // est should now be good to about 15 bits of precision. Do 2 rounds of // Newton's method to get closer, this should get us full double precision // Scale down x for the purpose of doing newtons method. This avoids over/under flows. final double xs = x / (p2*p2*p2); est += (xs - est*est*est) / (3*est*est); est += (xs - est*est*est) / (3*est*est); // Do one round of Newton's method in extended precision to get the last bit right. double temp = est * HEX_40000000; double ya = est + temp - temp; double yb = est - ya; double za = ya * ya; double zb = ya * yb * 2.0 + yb * yb; temp = za * HEX_40000000; double temp2 = za + temp - temp; zb += za - temp2; za = temp2; zb = za * yb + ya * zb + zb * yb; za = za * ya; double na = xs - za; double nb = -(na - xs + za); nb -= zb; est += (na+nb)/(3*est*est); /* Scale by a power of two, so this is exact. */ est *= p2; if (subnormal) { est *= 3.814697265625E-6; // 2^-18 } return est; } /** * Convert degrees to radians, with error of less than 0.5 ULP * @param x angle in degrees * @return x converted into radians */ public static double toRadians(double x) { if (Double.isInfinite(x) || x == 0.0) { // Matches +/- 0.0; return correct sign return x; } // These are PI/180 split into high and low order bits final double facta = 0.01745329052209854; final double factb = 1.997844754509471E-9; double xa = doubleHighPart(x); double xb = x - xa; double result = xb * factb + xb * facta + xa * factb + xa * facta; if (result == 0) { result = result * x; // ensure correct sign if calculation underflows } return result; } /** * Convert radians to degrees, with error of less than 0.5 ULP * @param x angle in radians * @return x converted into degrees */ public static double toDegrees(double x) { if (Double.isInfinite(x) || x == 0.0) { // Matches +/- 0.0; return correct sign return x; } // These are 180/PI split into high and low order bits final double facta = 57.2957763671875; final double factb = 3.145894820876798E-6; double xa = doubleHighPart(x); double xb = x - xa; return xb * factb + xb * facta + xa * factb + xa * facta; } /** * Absolute value. * @param x number from which absolute value is requested * @return abs(x) */ public static int abs(final int x) { final int i = x >>> 31; return (x ^ (~i + 1)) + i; } /** * Absolute value. * @param x number from which absolute value is requested * @return abs(x) */ public static long abs(final long x) { final long l = x >>> 63; // l is one if x negative zero else // ~l+1 is zero if x is positive, -1 if x is negative // x^(~l+1) is x is x is positive, ~x if x is negative // add around return (x ^ (~l + 1)) + l; } /** * Absolute value. * @param x number from which absolute value is requested * @return abs(x) */ public static float abs(final float x) { return Float.intBitsToFloat(MASK_NON_SIGN_INT & Float.floatToRawIntBits(x)); } /** * Absolute value. * @param x number from which absolute value is requested * @return abs(x) */ public static double abs(double x) { return Double.longBitsToDouble(MASK_NON_SIGN_LONG & Double.doubleToRawLongBits(x)); } /** * Compute least significant bit (Unit in Last Position) for a number. * @param x number from which ulp is requested * @return ulp(x) */ public static double ulp(double x) { if (Double.isInfinite(x)) { return Double.POSITIVE_INFINITY; } return abs(x - Double.longBitsToDouble(Double.doubleToRawLongBits(x) ^ 1)); } /** * Compute least significant bit (Unit in Last Position) for a number. * @param x number from which ulp is requested * @return ulp(x) */ public static float ulp(float x) { if (Float.isInfinite(x)) { return Float.POSITIVE_INFINITY; } return abs(x - Float.intBitsToFloat(Float.floatToIntBits(x) ^ 1)); } /** * Multiply a double number by a power of 2. * @param d number to multiply * @param n power of 2 * @return d × 2n */ public static double scalb(final double d, final int n) { // first simple and fast handling when 2^n can be represented using normal numbers if ((n > -1023) && (n < 1024)) { return d * Double.longBitsToDouble(((long) (n + 1023)) << 52); } // handle special cases if (Double.isNaN(d) || Double.isInfinite(d) || (d == 0)) { return d; } if (n < -2098) { return (d > 0) ? 0.0 : -0.0; } if (n > 2097) { return (d > 0) ? Double.POSITIVE_INFINITY : Double.NEGATIVE_INFINITY; } // decompose d final long bits = Double.doubleToRawLongBits(d); final long sign = bits & 0x8000000000000000L; int exponent = ((int) (bits >>> 52)) & 0x7ff; long mantissa = bits & 0x000fffffffffffffL; // compute scaled exponent int scaledExponent = exponent + n; if (n < 0) { // we are really in the case n <= -1023 if (scaledExponent > 0) { // both the input and the result are normal numbers, we only adjust the exponent return Double.longBitsToDouble(sign | (((long) scaledExponent) << 52) | mantissa); } else if (scaledExponent > -53) { // the input is a normal number and the result is a subnormal number // recover the hidden mantissa bit mantissa = mantissa | (1L << 52); // scales down complete mantissa, hence losing least significant bits final long mostSignificantLostBit = mantissa & (1L << (-scaledExponent)); mantissa = mantissa >>> (1 - scaledExponent); if (mostSignificantLostBit != 0) { // we need to add 1 bit to round up the result mantissa++; } return Double.longBitsToDouble(sign | mantissa); } else { // no need to compute the mantissa, the number scales down to 0 return (sign == 0L) ? 0.0 : -0.0; } } else { // we are really in the case n >= 1024 if (exponent == 0) { // the input number is subnormal, normalize it while ((mantissa >>> 52) != 1) { mantissa = mantissa << 1; --scaledExponent; } ++scaledExponent; mantissa = mantissa & 0x000fffffffffffffL; if (scaledExponent < 2047) { return Double.longBitsToDouble(sign | (((long) scaledExponent) << 52) | mantissa); } else { return (sign == 0L) ? Double.POSITIVE_INFINITY : Double.NEGATIVE_INFINITY; } } else if (scaledExponent < 2047) { return Double.longBitsToDouble(sign | (((long) scaledExponent) << 52) | mantissa); } else { return (sign == 0L) ? Double.POSITIVE_INFINITY : Double.NEGATIVE_INFINITY; } } } /** * Multiply a float number by a power of 2. * @param f number to multiply * @param n power of 2 * @return f × 2n */ public static float scalb(final float f, final int n) { // first simple and fast handling when 2^n can be represented using normal numbers if ((n > -127) && (n < 128)) { return f * Float.intBitsToFloat((n + 127) << 23); } // handle special cases if (Float.isNaN(f) || Float.isInfinite(f) || (f == 0f)) { return f; } if (n < -277) { return (f > 0) ? 0.0f : -0.0f; } if (n > 276) { return (f > 0) ? Float.POSITIVE_INFINITY : Float.NEGATIVE_INFINITY; } // decompose f final int bits = Float.floatToIntBits(f); final int sign = bits & 0x80000000; int exponent = (bits >>> 23) & 0xff; int mantissa = bits & 0x007fffff; // compute scaled exponent int scaledExponent = exponent + n; if (n < 0) { // we are really in the case n <= -127 if (scaledExponent > 0) { // both the input and the result are normal numbers, we only adjust the exponent return Float.intBitsToFloat(sign | (scaledExponent << 23) | mantissa); } else if (scaledExponent > -24) { // the input is a normal number and the result is a subnormal number // recover the hidden mantissa bit mantissa = mantissa | (1 << 23); // scales down complete mantissa, hence losing least significant bits final int mostSignificantLostBit = mantissa & (1 << (-scaledExponent)); mantissa = mantissa >>> (1 - scaledExponent); if (mostSignificantLostBit != 0) { // we need to add 1 bit to round up the result mantissa++; } return Float.intBitsToFloat(sign | mantissa); } else { // no need to compute the mantissa, the number scales down to 0 return (sign == 0) ? 0.0f : -0.0f; } } else { // we are really in the case n >= 128 if (exponent == 0) { // the input number is subnormal, normalize it while ((mantissa >>> 23) != 1) { mantissa = mantissa << 1; --scaledExponent; } ++scaledExponent; mantissa = mantissa & 0x007fffff; if (scaledExponent < 255) { return Float.intBitsToFloat(sign | (scaledExponent << 23) | mantissa); } else { return (sign == 0) ? Float.POSITIVE_INFINITY : Float.NEGATIVE_INFINITY; } } else if (scaledExponent < 255) { return Float.intBitsToFloat(sign | (scaledExponent << 23) | mantissa); } else { return (sign == 0) ? Float.POSITIVE_INFINITY : Float.NEGATIVE_INFINITY; } } } /** * Get the next machine representable number after a number, moving * in the direction of another number. *

                              * The ordering is as follows (increasing): *

                                *
                              • -INFINITY
                              • *
                              • -MAX_VALUE
                              • *
                              • -MIN_VALUE
                              • *
                              • -0.0
                              • *
                              • +0.0
                              • *
                              • +MIN_VALUE
                              • *
                              • +MAX_VALUE
                              • *
                              • +INFINITY
                              • *
                              • *

                                * If arguments compare equal, then the second argument is returned. *

                                * If {@code direction} is greater than {@code d}, * the smallest machine representable number strictly greater than * {@code d} is returned; if less, then the largest representable number * strictly less than {@code d} is returned.

                                *

                                * If {@code d} is infinite and direction does not * bring it back to finite numbers, it is returned unchanged.

                                * * @param d base number * @param direction (the only important thing is whether * {@code direction} is greater or smaller than {@code d}) * @return the next machine representable number in the specified direction */ public static double nextAfter(double d, double direction) { // handling of some important special cases if (Double.isNaN(d) || Double.isNaN(direction)) { return Double.NaN; } else if (d == direction) { return direction; } else if (Double.isInfinite(d)) { return (d < 0) ? -Double.MAX_VALUE : Double.MAX_VALUE; } else if (d == 0) { return (direction < 0) ? -Double.MIN_VALUE : Double.MIN_VALUE; } // special cases MAX_VALUE to infinity and MIN_VALUE to 0 // are handled just as normal numbers // can use raw bits since already dealt with infinity and NaN final long bits = Double.doubleToRawLongBits(d); final long sign = bits & 0x8000000000000000L; if ((direction < d) ^ (sign == 0L)) { return Double.longBitsToDouble(sign | ((bits & 0x7fffffffffffffffL) + 1)); } else { return Double.longBitsToDouble(sign | ((bits & 0x7fffffffffffffffL) - 1)); } } /** * Get the next machine representable number after a number, moving * in the direction of another number. *

                                * The ordering is as follows (increasing): *

                                  *
                                • -INFINITY
                                • *
                                • -MAX_VALUE
                                • *
                                • -MIN_VALUE
                                • *
                                • -0.0
                                • *
                                • +0.0
                                • *
                                • +MIN_VALUE
                                • *
                                • +MAX_VALUE
                                • *
                                • +INFINITY
                                • *
                                • *

                                  * If arguments compare equal, then the second argument is returned. *

                                  * If {@code direction} is greater than {@code f}, * the smallest machine representable number strictly greater than * {@code f} is returned; if less, then the largest representable number * strictly less than {@code f} is returned.

                                  *

                                  * If {@code f} is infinite and direction does not * bring it back to finite numbers, it is returned unchanged.

                                  * * @param f base number * @param direction (the only important thing is whether * {@code direction} is greater or smaller than {@code f}) * @return the next machine representable number in the specified direction */ public static float nextAfter(final float f, final double direction) { // handling of some important special cases if (Double.isNaN(f) || Double.isNaN(direction)) { return Float.NaN; } else if (f == direction) { return (float) direction; } else if (Float.isInfinite(f)) { return (f < 0f) ? -Float.MAX_VALUE : Float.MAX_VALUE; } else if (f == 0f) { return (direction < 0) ? -Float.MIN_VALUE : Float.MIN_VALUE; } // special cases MAX_VALUE to infinity and MIN_VALUE to 0 // are handled just as normal numbers final int bits = Float.floatToIntBits(f); final int sign = bits & 0x80000000; if ((direction < f) ^ (sign == 0)) { return Float.intBitsToFloat(sign | ((bits & 0x7fffffff) + 1)); } else { return Float.intBitsToFloat(sign | ((bits & 0x7fffffff) - 1)); } } /** Get the largest whole number smaller than x. * @param x number from which floor is requested * @return a double number f such that f is an integer f <= x < f + 1.0 */ public static double floor(double x) { long y; if (x != x) { // NaN return x; } if (x >= TWO_POWER_52 || x <= -TWO_POWER_52) { return x; } y = (long) x; if (x < 0 && y != x) { y--; } if (y == 0) { return x*y; } return y; } /** Get the smallest whole number larger than x. * @param x number from which ceil is requested * @return a double number c such that c is an integer c - 1.0 < x <= c */ public static double ceil(double x) { double y; if (x != x) { // NaN return x; } y = floor(x); if (y == x) { return y; } y += 1.0; if (y == 0) { return x*y; } return y; } /** Get the whole number that is the nearest to x, or the even one if x is exactly half way between two integers. * @param x number from which nearest whole number is requested * @return a double number r such that r is an integer r - 0.5 <= x <= r + 0.5 */ public static double rint(double x) { double y = floor(x); double d = x - y; if (d > 0.5) { if (y == -1.0) { return -0.0; // Preserve sign of operand } return y+1.0; } if (d < 0.5) { return y; } /* half way, round to even */ long z = (long) y; return (z & 1) == 0 ? y : y + 1.0; } /** Get the closest long to x. * @param x number from which closest long is requested * @return closest long to x */ public static long round(double x) { return (long) floor(x + 0.5); } /** Get the closest int to x. * @param x number from which closest int is requested * @return closest int to x */ public static int round(final float x) { return (int) floor(x + 0.5f); } /** Compute the minimum of two values * @param a first value * @param b second value * @return a if a is lesser or equal to b, b otherwise */ public static int min(final int a, final int b) { return (a <= b) ? a : b; } /** Compute the minimum of two values * @param a first value * @param b second value * @return a if a is lesser or equal to b, b otherwise */ public static long min(final long a, final long b) { return (a <= b) ? a : b; } /** Compute the minimum of two values * @param a first value * @param b second value * @return a if a is lesser or equal to b, b otherwise */ public static float min(final float a, final float b) { if (a > b) { return b; } if (a < b) { return a; } /* if either arg is NaN, return NaN */ if (a != b) { return Float.NaN; } /* min(+0.0,-0.0) == -0.0 */ /* 0x80000000 == Float.floatToRawIntBits(-0.0d) */ int bits = Float.floatToRawIntBits(a); if (bits == 0x80000000) { return a; } return b; } /** Compute the minimum of two values * @param a first value * @param b second value * @return a if a is lesser or equal to b, b otherwise */ public static double min(final double a, final double b) { if (a > b) { return b; } if (a < b) { return a; } /* if either arg is NaN, return NaN */ if (a != b) { return Double.NaN; } /* min(+0.0,-0.0) == -0.0 */ /* 0x8000000000000000L == Double.doubleToRawLongBits(-0.0d) */ long bits = Double.doubleToRawLongBits(a); if (bits == 0x8000000000000000L) { return a; } return b; } /** Compute the maximum of two values * @param a first value * @param b second value * @return b if a is lesser or equal to b, a otherwise */ public static int max(final int a, final int b) { return (a <= b) ? b : a; } /** Compute the maximum of two values * @param a first value * @param b second value * @return b if a is lesser or equal to b, a otherwise */ public static long max(final long a, final long b) { return (a <= b) ? b : a; } /** Compute the maximum of two values * @param a first value * @param b second value * @return b if a is lesser or equal to b, a otherwise */ public static float max(final float a, final float b) { if (a > b) { return a; } if (a < b) { return b; } /* if either arg is NaN, return NaN */ if (a != b) { return Float.NaN; } /* min(+0.0,-0.0) == -0.0 */ /* 0x80000000 == Float.floatToRawIntBits(-0.0d) */ int bits = Float.floatToRawIntBits(a); if (bits == 0x80000000) { return b; } return a; } /** Compute the maximum of two values * @param a first value * @param b second value * @return b if a is lesser or equal to b, a otherwise */ public static double max(final double a, final double b) { if (a > b) { return a; } if (a < b) { return b; } /* if either arg is NaN, return NaN */ if (a != b) { return Double.NaN; } /* min(+0.0,-0.0) == -0.0 */ /* 0x8000000000000000L == Double.doubleToRawLongBits(-0.0d) */ long bits = Double.doubleToRawLongBits(a); if (bits == 0x8000000000000000L) { return b; } return a; } /** * Returns the hypotenuse of a triangle with sides {@code x} and {@code y} * - sqrt(x2 +y2)
                                  * avoiding intermediate overflow or underflow. * *
                                    *
                                  • If either argument is infinite, then the result is positive infinity.
                                  • *
                                  • else, if either argument is NaN then the result is NaN.
                                  • *
                                  * * @param x a value * @param y a value * @return sqrt(x2 +y2) */ public static double hypot(final double x, final double y) { if (Double.isInfinite(x) || Double.isInfinite(y)) { return Double.POSITIVE_INFINITY; } else if (Double.isNaN(x) || Double.isNaN(y)) { return Double.NaN; } else { final int expX = getExponent(x); final int expY = getExponent(y); if (expX > expY + 27) { // y is neglectible with respect to x return abs(x); } else if (expY > expX + 27) { // x is neglectible with respect to y return abs(y); } else { // find an intermediate scale to avoid both overflow and underflow final int middleExp = (expX + expY) / 2; // scale parameters without losing precision final double scaledX = scalb(x, -middleExp); final double scaledY = scalb(y, -middleExp); // compute scaled hypotenuse final double scaledH = sqrt(scaledX * scaledX + scaledY * scaledY); // remove scaling return scalb(scaledH, middleExp); } } } /** * Computes the remainder as prescribed by the IEEE 754 standard. * The remainder value is mathematically equal to {@code x - y*n} * where {@code n} is the mathematical integer closest to the exact mathematical value * of the quotient {@code x/y}. * If two mathematical integers are equally close to {@code x/y} then * {@code n} is the integer that is even. *

                                  *

                                    *
                                  • If either operand is NaN, the result is NaN.
                                  • *
                                  • If the result is not NaN, the sign of the result equals the sign of the dividend.
                                  • *
                                  • If the dividend is an infinity, or the divisor is a zero, or both, the result is NaN.
                                  • *
                                  • If the dividend is finite and the divisor is an infinity, the result equals the dividend.
                                  • *
                                  • If the dividend is a zero and the divisor is finite, the result equals the dividend.
                                  • *
                                  *

                                  Note: this implementation currently delegates to {@link StrictMath#IEEEremainder} * @param dividend the number to be divided * @param divisor the number by which to divide * @return the remainder, rounded */ public static double IEEEremainder(double dividend, double divisor) { return StrictMath.IEEEremainder(dividend, divisor); // TODO provide our own implementation } /** * Returns the first argument with the sign of the second argument. * A NaN {@code sign} argument is treated as positive. * * @param magnitude the value to return * @param sign the sign for the returned value * @return the magnitude with the same sign as the {@code sign} argument */ public static double copySign(double magnitude, double sign){ // The highest order bit is going to be zero if the // highest order bit of m and s is the same and one otherwise. // So (m^s) will be positive if both m and s have the same sign // and negative otherwise. final long m = Double.doubleToRawLongBits(magnitude); // don't care about NaN final long s = Double.doubleToRawLongBits(sign); if ((m^s) >= 0) { return magnitude; } return -magnitude; // flip sign } /** * Returns the first argument with the sign of the second argument. * A NaN {@code sign} argument is treated as positive. * * @param magnitude the value to return * @param sign the sign for the returned value * @return the magnitude with the same sign as the {@code sign} argument */ public static float copySign(float magnitude, float sign){ // The highest order bit is going to be zero if the // highest order bit of m and s is the same and one otherwise. // So (m^s) will be positive if both m and s have the same sign // and negative otherwise. final int m = Float.floatToRawIntBits(magnitude); final int s = Float.floatToRawIntBits(sign); if ((m^s) >= 0) { return magnitude; } return -magnitude; // flip sign } /** * Return the exponent of a double number, removing the bias. *

                                  * For double numbers of the form 2x, the unbiased * exponent is exactly x. *

                                  * @param d number from which exponent is requested * @return exponent for d in IEEE754 representation, without bias */ public static int getExponent(final double d) { // NaN and Infinite will return 1024 anywho so can use raw bits return (int) ((Double.doubleToRawLongBits(d) >>> 52) & 0x7ff) - 1023; } /** * Return the exponent of a float number, removing the bias. *

                                  * For float numbers of the form 2x, the unbiased * exponent is exactly x. *

                                  * @param f number from which exponent is requested * @return exponent for d in IEEE754 representation, without bias */ public static int getExponent(final float f) { // NaN and Infinite will return the same exponent anywho so can use raw bits return ((Float.floatToRawIntBits(f) >>> 23) & 0xff) - 127; } /** * Print out contents of arrays, and check the length. *

                                  used to generate the preset arrays originally.

                                  * @param a unused */ public static void main(String[] a) { PrintStream out = System.out; FastMathCalc.printarray(out, "EXP_INT_TABLE_A", EXP_INT_TABLE_LEN, ExpIntTable.EXP_INT_TABLE_A); FastMathCalc.printarray(out, "EXP_INT_TABLE_B", EXP_INT_TABLE_LEN, ExpIntTable.EXP_INT_TABLE_B); FastMathCalc.printarray(out, "EXP_FRAC_TABLE_A", EXP_FRAC_TABLE_LEN, ExpFracTable.EXP_FRAC_TABLE_A); FastMathCalc.printarray(out, "EXP_FRAC_TABLE_B", EXP_FRAC_TABLE_LEN, ExpFracTable.EXP_FRAC_TABLE_B); FastMathCalc.printarray(out, "LN_MANT",LN_MANT_LEN, lnMant.LN_MANT); FastMathCalc.printarray(out, "SINE_TABLE_A", SINE_TABLE_LEN, SINE_TABLE_A); FastMathCalc.printarray(out, "SINE_TABLE_B", SINE_TABLE_LEN, SINE_TABLE_B); FastMathCalc.printarray(out, "COSINE_TABLE_A", SINE_TABLE_LEN, COSINE_TABLE_A); FastMathCalc.printarray(out, "COSINE_TABLE_B", SINE_TABLE_LEN, COSINE_TABLE_B); FastMathCalc.printarray(out, "TANGENT_TABLE_A", SINE_TABLE_LEN, TANGENT_TABLE_A); FastMathCalc.printarray(out, "TANGENT_TABLE_B", SINE_TABLE_LEN, TANGENT_TABLE_B); } /** Enclose large data table in nested static class so it's only loaded on first access. */ private static class ExpIntTable { /** Exponential evaluated at integer values, * exp(x) = expIntTableA[x + EXP_INT_TABLE_MAX_INDEX] + expIntTableB[x+EXP_INT_TABLE_MAX_INDEX]. */ private static final double[] EXP_INT_TABLE_A; /** Exponential evaluated at integer values, * exp(x) = expIntTableA[x + EXP_INT_TABLE_MAX_INDEX] + expIntTableB[x+EXP_INT_TABLE_MAX_INDEX] */ private static final double[] EXP_INT_TABLE_B; static { if (RECOMPUTE_TABLES_AT_RUNTIME) { EXP_INT_TABLE_A = new double[FastMath.EXP_INT_TABLE_LEN]; EXP_INT_TABLE_B = new double[FastMath.EXP_INT_TABLE_LEN]; final double tmp[] = new double[2]; final double recip[] = new double[2]; // Populate expIntTable for (int i = 0; i < FastMath.EXP_INT_TABLE_MAX_INDEX; i++) { FastMathCalc.expint(i, tmp); EXP_INT_TABLE_A[i + FastMath.EXP_INT_TABLE_MAX_INDEX] = tmp[0]; EXP_INT_TABLE_B[i + FastMath.EXP_INT_TABLE_MAX_INDEX] = tmp[1]; if (i != 0) { // Negative integer powers FastMathCalc.splitReciprocal(tmp, recip); EXP_INT_TABLE_A[FastMath.EXP_INT_TABLE_MAX_INDEX - i] = recip[0]; EXP_INT_TABLE_B[FastMath.EXP_INT_TABLE_MAX_INDEX - i] = recip[1]; } } } else { EXP_INT_TABLE_A = FastMathLiteralArrays.loadExpIntA(); EXP_INT_TABLE_B = FastMathLiteralArrays.loadExpIntB(); } } } /** Enclose large data table in nested static class so it's only loaded on first access. */ private static class ExpFracTable { /** Exponential over the range of 0 - 1 in increments of 2^-10 * exp(x/1024) = expFracTableA[x] + expFracTableB[x]. * 1024 = 2^10 */ private static final double[] EXP_FRAC_TABLE_A; /** Exponential over the range of 0 - 1 in increments of 2^-10 * exp(x/1024) = expFracTableA[x] + expFracTableB[x]. */ private static final double[] EXP_FRAC_TABLE_B; static { if (RECOMPUTE_TABLES_AT_RUNTIME) { EXP_FRAC_TABLE_A = new double[FastMath.EXP_FRAC_TABLE_LEN]; EXP_FRAC_TABLE_B = new double[FastMath.EXP_FRAC_TABLE_LEN]; final double tmp[] = new double[2]; // Populate expFracTable final double factor = 1d / (EXP_FRAC_TABLE_LEN - 1); for (int i = 0; i < EXP_FRAC_TABLE_A.length; i++) { FastMathCalc.slowexp(i * factor, tmp); EXP_FRAC_TABLE_A[i] = tmp[0]; EXP_FRAC_TABLE_B[i] = tmp[1]; } } else { EXP_FRAC_TABLE_A = FastMathLiteralArrays.loadExpFracA(); EXP_FRAC_TABLE_B = FastMathLiteralArrays.loadExpFracB(); } } } /** Enclose large data table in nested static class so it's only loaded on first access. */ private static class lnMant { /** Extended precision logarithm table over the range 1 - 2 in increments of 2^-10. */ private static final double[][] LN_MANT; static { if (RECOMPUTE_TABLES_AT_RUNTIME) { LN_MANT = new double[FastMath.LN_MANT_LEN][]; // Populate lnMant table for (int i = 0; i < LN_MANT.length; i++) { final double d = Double.longBitsToDouble( (((long) i) << 42) | 0x3ff0000000000000L ); LN_MANT[i] = FastMathCalc.slowLog(d); } } else { LN_MANT = FastMathLiteralArrays.loadLnMant(); } } } /** Enclose the Cody/Waite reduction (used in "sin", "cos" and "tan"). */ private static class CodyWaite { /** k */ private final int finalK; /** remA */ private final double finalRemA; /** remB */ private final double finalRemB; /** * @param xa Argument. */ CodyWaite(double xa) { // Estimate k. //k = (int)(xa / 1.5707963267948966); int k = (int)(xa * 0.6366197723675814); // Compute remainder. double remA; double remB; while (true) { double a = -k * 1.570796251296997; remA = xa + a; remB = -(remA - xa - a); a = -k * 7.549789948768648E-8; double b = remA; remA = a + b; remB += -(remA - b - a); a = -k * 6.123233995736766E-17; b = remA; remA = a + b; remB += -(remA - b - a); if (remA > 0) { break; } // Remainder is negative, so decrement k and try again. // This should only happen if the input is very close // to an even multiple of pi/2. --k; } this.finalK = k; this.finalRemA = remA; this.finalRemB = remB; } /** * @return k */ int getK() { return finalK; } /** * @return remA */ double getRemA() { return finalRemA; } /** * @return remB */ double getRemB() { return finalRemB; } } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/util/DefaultTransformer.java100644 1750 1750 5747 12126627720 30165 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.util; import java.io.Serializable; import org.apache.commons.math3.exception.util.LocalizedFormats; import org.apache.commons.math3.exception.MathIllegalArgumentException; import org.apache.commons.math3.exception.NullArgumentException; /** * A Default NumberTransformer for java.lang.Numbers and Numeric Strings. This * provides some simple conversion capabilities to turn any java.lang.Number * into a primitive double or to turn a String representation of a Number into * a double. * * @version $Id: DefaultTransformer.java 1416643 2012-12-03 19:37:14Z tn $ */ public class DefaultTransformer implements NumberTransformer, Serializable { /** Serializable version identifier */ private static final long serialVersionUID = 4019938025047800455L; /** * @param o the object that gets transformed. * @return a double primitive representation of the Object o. * @throws NullArgumentException if Object o is {@code null}. * @throws MathIllegalArgumentException if Object o * cannot successfully be transformed * @see Commons Collections Transformer */ public double transform(Object o) throws NullArgumentException, MathIllegalArgumentException { if (o == null) { throw new NullArgumentException(LocalizedFormats.OBJECT_TRANSFORMATION); } if (o instanceof Number) { return ((Number)o).doubleValue(); } try { return Double.valueOf(o.toString()).doubleValue(); } catch (NumberFormatException e) { throw new MathIllegalArgumentException(LocalizedFormats.CANNOT_TRANSFORM_TO_DOUBLE, o.toString()); } } /** {@inheritDoc} */ @Override public boolean equals(Object other) { if (this == other) { return true; } return other instanceof DefaultTransformer; } /** {@inheritDoc} */ @Override public int hashCode() { // some arbitrary number ... return 401993047; } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/util/IterationListener.java100644 1750 1750 3734 12126627720 30014 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.util; import java.util.EventListener; /** * The listener interface for receiving events occurring in an iterative * algorithm. * * @version $Id: IterationListener.java 1416643 2012-12-03 19:37:14Z tn $ */ public interface IterationListener extends EventListener { /** * Invoked after completion of the initial phase of the iterative algorithm * (prior to the main iteration loop). * * @param e The {@link IterationEvent} object. */ void initializationPerformed(IterationEvent e); /** * Invoked each time an iteration is completed (in the main iteration loop). * * @param e The {@link IterationEvent} object. */ void iterationPerformed(IterationEvent e); /** * Invoked each time a new iteration is completed (in the main iteration * loop). * * @param e The {@link IterationEvent} object. */ void iterationStarted(IterationEvent e); /** * Invoked after completion of the operations which occur after breaking out * of the main iteration loop. * * @param e The {@link IterationEvent} object. */ void terminationPerformed(IterationEvent e); } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/util/ArithmeticUtils.java100644 1750 1750 112777 12126627720 27532 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.util; import java.math.BigInteger; import java.util.concurrent.atomic.AtomicReference; import org.apache.commons.math3.exception.MathArithmeticException; import org.apache.commons.math3.exception.NotPositiveException; import org.apache.commons.math3.exception.NumberIsTooLargeException; import org.apache.commons.math3.exception.util.Localizable; import org.apache.commons.math3.exception.util.LocalizedFormats; /** * Some useful, arithmetics related, additions to the built-in functions in * {@link Math}. * * @version $Id: ArithmeticUtils.java 1422313 2012-12-15 18:53:41Z psteitz $ */ public final class ArithmeticUtils { /** All long-representable factorials */ static final long[] FACTORIALS = new long[] { 1l, 1l, 2l, 6l, 24l, 120l, 720l, 5040l, 40320l, 362880l, 3628800l, 39916800l, 479001600l, 6227020800l, 87178291200l, 1307674368000l, 20922789888000l, 355687428096000l, 6402373705728000l, 121645100408832000l, 2432902008176640000l }; /** Stirling numbers of the second kind. */ static final AtomicReference STIRLING_S2 = new AtomicReference (null); /** Private constructor. */ private ArithmeticUtils() { super(); } /** * Add two integers, checking for overflow. * * @param x an addend * @param y an addend * @return the sum {@code x+y} * @throws MathArithmeticException if the result can not be represented * as an {@code int}. * @since 1.1 */ public static int addAndCheck(int x, int y) throws MathArithmeticException { long s = (long)x + (long)y; if (s < Integer.MIN_VALUE || s > Integer.MAX_VALUE) { throw new MathArithmeticException(LocalizedFormats.OVERFLOW_IN_ADDITION, x, y); } return (int)s; } /** * Add two long integers, checking for overflow. * * @param a an addend * @param b an addend * @return the sum {@code a+b} * @throws MathArithmeticException if the result can not be represented as an * long * @since 1.2 */ public static long addAndCheck(long a, long b) throws MathArithmeticException { return ArithmeticUtils.addAndCheck(a, b, LocalizedFormats.OVERFLOW_IN_ADDITION); } /** * Returns an exact representation of the Binomial * Coefficient, "{@code n choose k}", the number of * {@code k}-element subsets that can be selected from an * {@code n}-element set. *

                                  * Preconditions: *

                                    *
                                  • {@code 0 <= k <= n } (otherwise * {@code IllegalArgumentException} is thrown)
                                  • *
                                  • The result is small enough to fit into a {@code long}. The * largest value of {@code n} for which all coefficients are * {@code < Long.MAX_VALUE} is 66. If the computed value exceeds * {@code Long.MAX_VALUE} an {@code ArithMeticException} is * thrown.
                                  • *

                                  * * @param n the size of the set * @param k the size of the subsets to be counted * @return {@code n choose k} * @throws NotPositiveException if {@code n < 0}. * @throws NumberIsTooLargeException if {@code k > n}. * @throws MathArithmeticException if the result is too large to be * represented by a long integer. */ public static long binomialCoefficient(final int n, final int k) throws NotPositiveException, NumberIsTooLargeException, MathArithmeticException { ArithmeticUtils.checkBinomial(n, k); if ((n == k) || (k == 0)) { return 1; } if ((k == 1) || (k == n - 1)) { return n; } // Use symmetry for large k if (k > n / 2) { return binomialCoefficient(n, n - k); } // We use the formula // (n choose k) = n! / (n-k)! / k! // (n choose k) == ((n-k+1)*...*n) / (1*...*k) // which could be written // (n choose k) == (n-1 choose k-1) * n / k long result = 1; if (n <= 61) { // For n <= 61, the naive implementation cannot overflow. int i = n - k + 1; for (int j = 1; j <= k; j++) { result = result * i / j; i++; } } else if (n <= 66) { // For n > 61 but n <= 66, the result cannot overflow, // but we must take care not to overflow intermediate values. int i = n - k + 1; for (int j = 1; j <= k; j++) { // We know that (result * i) is divisible by j, // but (result * i) may overflow, so we split j: // Filter out the gcd, d, so j/d and i/d are integer. // result is divisible by (j/d) because (j/d) // is relative prime to (i/d) and is a divisor of // result * (i/d). final long d = gcd(i, j); result = (result / (j / d)) * (i / d); i++; } } else { // For n > 66, a result overflow might occur, so we check // the multiplication, taking care to not overflow // unnecessary. int i = n - k + 1; for (int j = 1; j <= k; j++) { final long d = gcd(i, j); result = mulAndCheck(result / (j / d), i / d); i++; } } return result; } /** * Returns a {@code double} representation of the Binomial * Coefficient, "{@code n choose k}", the number of * {@code k}-element subsets that can be selected from an * {@code n}-element set. *

                                  * Preconditions: *

                                    *
                                  • {@code 0 <= k <= n } (otherwise * {@code IllegalArgumentException} is thrown)
                                  • *
                                  • The result is small enough to fit into a {@code double}. The * largest value of {@code n} for which all coefficients are < * Double.MAX_VALUE is 1029. If the computed value exceeds Double.MAX_VALUE, * Double.POSITIVE_INFINITY is returned
                                  • *

                                  * * @param n the size of the set * @param k the size of the subsets to be counted * @return {@code n choose k} * @throws NotPositiveException if {@code n < 0}. * @throws NumberIsTooLargeException if {@code k > n}. * @throws MathArithmeticException if the result is too large to be * represented by a long integer. */ public static double binomialCoefficientDouble(final int n, final int k) throws NotPositiveException, NumberIsTooLargeException, MathArithmeticException { ArithmeticUtils.checkBinomial(n, k); if ((n == k) || (k == 0)) { return 1d; } if ((k == 1) || (k == n - 1)) { return n; } if (k > n/2) { return binomialCoefficientDouble(n, n - k); } if (n < 67) { return binomialCoefficient(n,k); } double result = 1d; for (int i = 1; i <= k; i++) { result *= (double)(n - k + i) / (double)i; } return FastMath.floor(result + 0.5); } /** * Returns the natural {@code log} of the Binomial * Coefficient, "{@code n choose k}", the number of * {@code k}-element subsets that can be selected from an * {@code n}-element set. *

                                  * Preconditions: *

                                    *
                                  • {@code 0 <= k <= n } (otherwise * {@code IllegalArgumentException} is thrown)
                                  • *

                                  * * @param n the size of the set * @param k the size of the subsets to be counted * @return {@code n choose k} * @throws NotPositiveException if {@code n < 0}. * @throws NumberIsTooLargeException if {@code k > n}. * @throws MathArithmeticException if the result is too large to be * represented by a long integer. */ public static double binomialCoefficientLog(final int n, final int k) throws NotPositiveException, NumberIsTooLargeException, MathArithmeticException { ArithmeticUtils.checkBinomial(n, k); if ((n == k) || (k == 0)) { return 0; } if ((k == 1) || (k == n - 1)) { return FastMath.log(n); } /* * For values small enough to do exact integer computation, * return the log of the exact value */ if (n < 67) { return FastMath.log(binomialCoefficient(n,k)); } /* * Return the log of binomialCoefficientDouble for values that will not * overflow binomialCoefficientDouble */ if (n < 1030) { return FastMath.log(binomialCoefficientDouble(n, k)); } if (k > n / 2) { return binomialCoefficientLog(n, n - k); } /* * Sum logs for values that could overflow */ double logSum = 0; // n!/(n-k)! for (int i = n - k + 1; i <= n; i++) { logSum += FastMath.log(i); } // divide by k! for (int i = 2; i <= k; i++) { logSum -= FastMath.log(i); } return logSum; } /** * Returns n!. Shorthand for {@code n} Factorial, the * product of the numbers {@code 1,...,n}. *

                                  * Preconditions: *

                                    *
                                  • {@code n >= 0} (otherwise * {@code IllegalArgumentException} is thrown)
                                  • *
                                  • The result is small enough to fit into a {@code long}. The * largest value of {@code n} for which {@code n!} < * Long.MAX_VALUE} is 20. If the computed value exceeds {@code Long.MAX_VALUE} * an {@code ArithMeticException } is thrown.
                                  • *
                                  *

                                  * * @param n argument * @return {@code n!} * @throws MathArithmeticException if the result is too large to be represented * by a {@code long}. * @throws NotPositiveException if {@code n < 0}. * @throws MathArithmeticException if {@code n > 20}: The factorial value is too * large to fit in a {@code long}. */ public static long factorial(final int n) throws NotPositiveException, MathArithmeticException { if (n < 0) { throw new NotPositiveException(LocalizedFormats.FACTORIAL_NEGATIVE_PARAMETER, n); } if (n > 20) { throw new MathArithmeticException(); } return FACTORIALS[n]; } /** * Compute n!, the * factorial of {@code n} (the product of the numbers 1 to n), as a * {@code double}. * The result should be small enough to fit into a {@code double}: The * largest {@code n} for which {@code n! < Double.MAX_VALUE} is 170. * If the computed value exceeds {@code Double.MAX_VALUE}, * {@code Double.POSITIVE_INFINITY} is returned. * * @param n Argument. * @return {@code n!} * @throws NotPositiveException if {@code n < 0}. */ public static double factorialDouble(final int n) throws NotPositiveException { if (n < 0) { throw new NotPositiveException(LocalizedFormats.FACTORIAL_NEGATIVE_PARAMETER, n); } if (n < 21) { return FACTORIALS[n]; } return FastMath.floor(FastMath.exp(ArithmeticUtils.factorialLog(n)) + 0.5); } /** * Compute the natural logarithm of the factorial of {@code n}. * * @param n Argument. * @return {@code n!} * @throws NotPositiveException if {@code n < 0}. */ public static double factorialLog(final int n) throws NotPositiveException { if (n < 0) { throw new NotPositiveException(LocalizedFormats.FACTORIAL_NEGATIVE_PARAMETER, n); } if (n < 21) { return FastMath.log(FACTORIALS[n]); } double logSum = 0; for (int i = 2; i <= n; i++) { logSum += FastMath.log(i); } return logSum; } /** * Computes the greatest common divisor of the absolute value of two * numbers, using a modified version of the "binary gcd" method. * See Knuth 4.5.2 algorithm B. * The algorithm is due to Josef Stein (1961). *
                                  * Special cases: *
                                    *
                                  • The invocations * {@code gcd(Integer.MIN_VALUE, Integer.MIN_VALUE)}, * {@code gcd(Integer.MIN_VALUE, 0)} and * {@code gcd(0, Integer.MIN_VALUE)} throw an * {@code ArithmeticException}, because the result would be 2^31, which * is too large for an int value.
                                  • *
                                  • The result of {@code gcd(x, x)}, {@code gcd(0, x)} and * {@code gcd(x, 0)} is the absolute value of {@code x}, except * for the special cases above.
                                  • *
                                  • The invocation {@code gcd(0, 0)} is the only one which returns * {@code 0}.
                                  • *
                                  * * @param p Number. * @param q Number. * @return the greatest common divisor (never negative). * @throws MathArithmeticException if the result cannot be represented as * a non-negative {@code int} value. * @since 1.1 */ public static int gcd(int p, int q) throws MathArithmeticException { int a = p; int b = q; if (a == 0 || b == 0) { if (a == Integer.MIN_VALUE || b == Integer.MIN_VALUE) { throw new MathArithmeticException(LocalizedFormats.GCD_OVERFLOW_32_BITS, p, q); } return FastMath.abs(a + b); } long al = a; long bl = b; boolean useLong = false; if (a < 0) { if(Integer.MIN_VALUE == a) { useLong = true; } else { a = -a; } al = -al; } if (b < 0) { if (Integer.MIN_VALUE == b) { useLong = true; } else { b = -b; } bl = -bl; } if (useLong) { if(al == bl) { throw new MathArithmeticException(LocalizedFormats.GCD_OVERFLOW_32_BITS, p, q); } long blbu = bl; bl = al; al = blbu % al; if (al == 0) { if (bl > Integer.MAX_VALUE) { throw new MathArithmeticException(LocalizedFormats.GCD_OVERFLOW_32_BITS, p, q); } return (int) bl; } blbu = bl; // Now "al" and "bl" fit in an "int". b = (int) al; a = (int) (blbu % al); } return gcdPositive(a, b); } /** * Computes the greatest common divisor of two positive numbers * (this precondition is not checked and the result is undefined * if not fulfilled) using the "binary gcd" method which avoids division * and modulo operations. * See Knuth 4.5.2 algorithm B. * The algorithm is due to Josef Stein (1961). *
                                  * Special cases: *
                                    *
                                  • The result of {@code gcd(x, x)}, {@code gcd(0, x)} and * {@code gcd(x, 0)} is the value of {@code x}.
                                  • *
                                  • The invocation {@code gcd(0, 0)} is the only one which returns * {@code 0}.
                                  • *
                                  * * @param a Positive number. * @param b Positive number. * @return the greatest common divisor. */ private static int gcdPositive(int a, int b) { if (a == 0) { return b; } else if (b == 0) { return a; } // Make "a" and "b" odd, keeping track of common power of 2. final int aTwos = Integer.numberOfTrailingZeros(a); a >>= aTwos; final int bTwos = Integer.numberOfTrailingZeros(b); b >>= bTwos; final int shift = Math.min(aTwos, bTwos); // "a" and "b" are positive. // If a > b then "gdc(a, b)" is equal to "gcd(a - b, b)". // If a < b then "gcd(a, b)" is equal to "gcd(b - a, a)". // Hence, in the successive iterations: // "a" becomes the absolute difference of the current values, // "b" becomes the minimum of the current values. while (a != b) { final int delta = a - b; b = Math.min(a, b); a = Math.abs(delta); // Remove any power of 2 in "a" ("b" is guaranteed to be odd). a >>= Integer.numberOfTrailingZeros(a); } // Recover the common power of 2. return a << shift; } /** *

                                  * Gets the greatest common divisor of the absolute value of two numbers, * using the "binary gcd" method which avoids division and modulo * operations. See Knuth 4.5.2 algorithm B. This algorithm is due to Josef * Stein (1961). *

                                  * Special cases: *
                                    *
                                  • The invocations * {@code gcd(Long.MIN_VALUE, Long.MIN_VALUE)}, * {@code gcd(Long.MIN_VALUE, 0L)} and * {@code gcd(0L, Long.MIN_VALUE)} throw an * {@code ArithmeticException}, because the result would be 2^63, which * is too large for a long value.
                                  • *
                                  • The result of {@code gcd(x, x)}, {@code gcd(0L, x)} and * {@code gcd(x, 0L)} is the absolute value of {@code x}, except * for the special cases above. *
                                  • The invocation {@code gcd(0L, 0L)} is the only one which returns * {@code 0L}.
                                  • *
                                  * * @param p Number. * @param q Number. * @return the greatest common divisor, never negative. * @throws MathArithmeticException if the result cannot be represented as * a non-negative {@code long} value. * @since 2.1 */ public static long gcd(final long p, final long q) throws MathArithmeticException { long u = p; long v = q; if ((u == 0) || (v == 0)) { if ((u == Long.MIN_VALUE) || (v == Long.MIN_VALUE)){ throw new MathArithmeticException(LocalizedFormats.GCD_OVERFLOW_64_BITS, p, q); } return FastMath.abs(u) + FastMath.abs(v); } // keep u and v negative, as negative integers range down to // -2^63, while positive numbers can only be as large as 2^63-1 // (i.e. we can't necessarily negate a negative number without // overflow) /* assert u!=0 && v!=0; */ if (u > 0) { u = -u; } // make u negative if (v > 0) { v = -v; } // make v negative // B1. [Find power of 2] int k = 0; while ((u & 1) == 0 && (v & 1) == 0 && k < 63) { // while u and v are // both even... u /= 2; v /= 2; k++; // cast out twos. } if (k == 63) { throw new MathArithmeticException(LocalizedFormats.GCD_OVERFLOW_64_BITS, p, q); } // B2. Initialize: u and v have been divided by 2^k and at least // one is odd. long t = ((u & 1) == 1) ? v : -(u / 2)/* B3 */; // t negative: u was odd, v may be even (t replaces v) // t positive: u was even, v is odd (t replaces u) do { /* assert u<0 && v<0; */ // B4/B3: cast out twos from t. while ((t & 1) == 0) { // while t is even.. t /= 2; // cast out twos } // B5 [reset max(u,v)] if (t > 0) { u = -t; } else { v = t; } // B6/B3. at this point both u and v should be odd. t = (v - u) / 2; // |u| larger: t positive (replace u) // |v| larger: t negative (replace v) } while (t != 0); return -u * (1L << k); // gcd is u*2^k } /** *

                                  * Returns the least common multiple of the absolute value of two numbers, * using the formula {@code lcm(a,b) = (a / gcd(a,b)) * b}. *

                                  * Special cases: *
                                    *
                                  • The invocations {@code lcm(Integer.MIN_VALUE, n)} and * {@code lcm(n, Integer.MIN_VALUE)}, where {@code abs(n)} is a * power of 2, throw an {@code ArithmeticException}, because the result * would be 2^31, which is too large for an int value.
                                  • *
                                  • The result of {@code lcm(0, x)} and {@code lcm(x, 0)} is * {@code 0} for any {@code x}. *
                                  * * @param a Number. * @param b Number. * @return the least common multiple, never negative. * @throws MathArithmeticException if the result cannot be represented as * a non-negative {@code int} value. * @since 1.1 */ public static int lcm(int a, int b) throws MathArithmeticException { if (a == 0 || b == 0){ return 0; } int lcm = FastMath.abs(ArithmeticUtils.mulAndCheck(a / gcd(a, b), b)); if (lcm == Integer.MIN_VALUE) { throw new MathArithmeticException(LocalizedFormats.LCM_OVERFLOW_32_BITS, a, b); } return lcm; } /** *

                                  * Returns the least common multiple of the absolute value of two numbers, * using the formula {@code lcm(a,b) = (a / gcd(a,b)) * b}. *

                                  * Special cases: *
                                    *
                                  • The invocations {@code lcm(Long.MIN_VALUE, n)} and * {@code lcm(n, Long.MIN_VALUE)}, where {@code abs(n)} is a * power of 2, throw an {@code ArithmeticException}, because the result * would be 2^63, which is too large for an int value.
                                  • *
                                  • The result of {@code lcm(0L, x)} and {@code lcm(x, 0L)} is * {@code 0L} for any {@code x}. *
                                  * * @param a Number. * @param b Number. * @return the least common multiple, never negative. * @throws MathArithmeticException if the result cannot be represented * as a non-negative {@code long} value. * @since 2.1 */ public static long lcm(long a, long b) throws MathArithmeticException { if (a == 0 || b == 0){ return 0; } long lcm = FastMath.abs(ArithmeticUtils.mulAndCheck(a / gcd(a, b), b)); if (lcm == Long.MIN_VALUE){ throw new MathArithmeticException(LocalizedFormats.LCM_OVERFLOW_64_BITS, a, b); } return lcm; } /** * Multiply two integers, checking for overflow. * * @param x Factor. * @param y Factor. * @return the product {@code x * y}. * @throws MathArithmeticException if the result can not be * represented as an {@code int}. * @since 1.1 */ public static int mulAndCheck(int x, int y) throws MathArithmeticException { long m = ((long)x) * ((long)y); if (m < Integer.MIN_VALUE || m > Integer.MAX_VALUE) { throw new MathArithmeticException(); } return (int)m; } /** * Multiply two long integers, checking for overflow. * * @param a Factor. * @param b Factor. * @return the product {@code a * b}. * @throws MathArithmeticException if the result can not be represented * as a {@code long}. * @since 1.2 */ public static long mulAndCheck(long a, long b) throws MathArithmeticException { long ret; if (a > b) { // use symmetry to reduce boundary cases ret = mulAndCheck(b, a); } else { if (a < 0) { if (b < 0) { // check for positive overflow with negative a, negative b if (a >= Long.MAX_VALUE / b) { ret = a * b; } else { throw new MathArithmeticException(); } } else if (b > 0) { // check for negative overflow with negative a, positive b if (Long.MIN_VALUE / b <= a) { ret = a * b; } else { throw new MathArithmeticException(); } } else { // assert b == 0 ret = 0; } } else if (a > 0) { // assert a > 0 // assert b > 0 // check for positive overflow with positive a, positive b if (a <= Long.MAX_VALUE / b) { ret = a * b; } else { throw new MathArithmeticException(); } } else { // assert a == 0 ret = 0; } } return ret; } /** * Subtract two integers, checking for overflow. * * @param x Minuend. * @param y Subtrahend. * @return the difference {@code x - y}. * @throws MathArithmeticException if the result can not be represented * as an {@code int}. * @since 1.1 */ public static int subAndCheck(int x, int y) throws MathArithmeticException { long s = (long)x - (long)y; if (s < Integer.MIN_VALUE || s > Integer.MAX_VALUE) { throw new MathArithmeticException(LocalizedFormats.OVERFLOW_IN_SUBTRACTION, x, y); } return (int)s; } /** * Subtract two long integers, checking for overflow. * * @param a Value. * @param b Value. * @return the difference {@code a - b}. * @throws MathArithmeticException if the result can not be represented as a * {@code long}. * @since 1.2 */ public static long subAndCheck(long a, long b) throws MathArithmeticException { long ret; if (b == Long.MIN_VALUE) { if (a < 0) { ret = a - b; } else { throw new MathArithmeticException(LocalizedFormats.OVERFLOW_IN_ADDITION, a, -b); } } else { // use additive inverse ret = addAndCheck(a, -b, LocalizedFormats.OVERFLOW_IN_ADDITION); } return ret; } /** * Raise an int to an int power. * * @param k Number to raise. * @param e Exponent (must be positive or zero). * @return ke * @throws NotPositiveException if {@code e < 0}. */ public static int pow(final int k, int e) throws NotPositiveException { if (e < 0) { throw new NotPositiveException(LocalizedFormats.EXPONENT, e); } int result = 1; int k2p = k; while (e != 0) { if ((e & 0x1) != 0) { result *= k2p; } k2p *= k2p; e = e >> 1; } return result; } /** * Raise an int to a long power. * * @param k Number to raise. * @param e Exponent (must be positive or zero). * @return ke * @throws NotPositiveException if {@code e < 0}. */ public static int pow(final int k, long e) throws NotPositiveException { if (e < 0) { throw new NotPositiveException(LocalizedFormats.EXPONENT, e); } int result = 1; int k2p = k; while (e != 0) { if ((e & 0x1) != 0) { result *= k2p; } k2p *= k2p; e = e >> 1; } return result; } /** * Raise a long to an int power. * * @param k Number to raise. * @param e Exponent (must be positive or zero). * @return ke * @throws NotPositiveException if {@code e < 0}. */ public static long pow(final long k, int e) throws NotPositiveException { if (e < 0) { throw new NotPositiveException(LocalizedFormats.EXPONENT, e); } long result = 1l; long k2p = k; while (e != 0) { if ((e & 0x1) != 0) { result *= k2p; } k2p *= k2p; e = e >> 1; } return result; } /** * Raise a long to a long power. * * @param k Number to raise. * @param e Exponent (must be positive or zero). * @return ke * @throws NotPositiveException if {@code e < 0}. */ public static long pow(final long k, long e) throws NotPositiveException { if (e < 0) { throw new NotPositiveException(LocalizedFormats.EXPONENT, e); } long result = 1l; long k2p = k; while (e != 0) { if ((e & 0x1) != 0) { result *= k2p; } k2p *= k2p; e = e >> 1; } return result; } /** * Raise a BigInteger to an int power. * * @param k Number to raise. * @param e Exponent (must be positive or zero). * @return ke * @throws NotPositiveException if {@code e < 0}. */ public static BigInteger pow(final BigInteger k, int e) throws NotPositiveException { if (e < 0) { throw new NotPositiveException(LocalizedFormats.EXPONENT, e); } return k.pow(e); } /** * Raise a BigInteger to a long power. * * @param k Number to raise. * @param e Exponent (must be positive or zero). * @return ke * @throws NotPositiveException if {@code e < 0}. */ public static BigInteger pow(final BigInteger k, long e) throws NotPositiveException { if (e < 0) { throw new NotPositiveException(LocalizedFormats.EXPONENT, e); } BigInteger result = BigInteger.ONE; BigInteger k2p = k; while (e != 0) { if ((e & 0x1) != 0) { result = result.multiply(k2p); } k2p = k2p.multiply(k2p); e = e >> 1; } return result; } /** * Raise a BigInteger to a BigInteger power. * * @param k Number to raise. * @param e Exponent (must be positive or zero). * @return ke * @throws NotPositiveException if {@code e < 0}. */ public static BigInteger pow(final BigInteger k, BigInteger e) throws NotPositiveException { if (e.compareTo(BigInteger.ZERO) < 0) { throw new NotPositiveException(LocalizedFormats.EXPONENT, e); } BigInteger result = BigInteger.ONE; BigInteger k2p = k; while (!BigInteger.ZERO.equals(e)) { if (e.testBit(0)) { result = result.multiply(k2p); } k2p = k2p.multiply(k2p); e = e.shiftRight(1); } return result; } /** * Returns the * Stirling number of the second kind, "{@code S(n,k)}", the number of * ways of partitioning an {@code n}-element set into {@code k} non-empty * subsets. *

                                  * The preconditions are {@code 0 <= k <= n } (otherwise * {@code NotPositiveException} is thrown) *

                                  * @param n the size of the set * @param k the number of non-empty subsets * @return {@code S(n,k)} * @throws NotPositiveException if {@code k < 0}. * @throws NumberIsTooLargeException if {@code k > n}. * @throws MathArithmeticException if some overflow happens, typically for n exceeding 25 and * k between 20 and n-2 (S(n,n-1) is handled specifically and does not overflow) * @since 3.1 */ public static long stirlingS2(final int n, final int k) throws NotPositiveException, NumberIsTooLargeException, MathArithmeticException { if (k < 0) { throw new NotPositiveException(k); } if (k > n) { throw new NumberIsTooLargeException(k, n, true); } long[][] stirlingS2 = STIRLING_S2.get(); if (stirlingS2 == null) { // the cache has never been initialized, compute the first numbers // by direct recurrence relation // as S(26,9) = 11201516780955125625 is larger than Long.MAX_VALUE // we must stop computation at row 26 final int maxIndex = 26; stirlingS2 = new long[maxIndex][]; stirlingS2[0] = new long[] { 1l }; for (int i = 1; i < stirlingS2.length; ++i) { stirlingS2[i] = new long[i + 1]; stirlingS2[i][0] = 0; stirlingS2[i][1] = 1; stirlingS2[i][i] = 1; for (int j = 2; j < i; ++j) { stirlingS2[i][j] = j * stirlingS2[i - 1][j] + stirlingS2[i - 1][j - 1]; } } // atomically save the cache STIRLING_S2.compareAndSet(null, stirlingS2); } if (n < stirlingS2.length) { // the number is in the small cache return stirlingS2[n][k]; } else { // use explicit formula to compute the number without caching it if (k == 0) { return 0; } else if (k == 1 || k == n) { return 1; } else if (k == 2) { return (1l << (n - 1)) - 1l; } else if (k == n - 1) { return binomialCoefficient(n, 2); } else { // definition formula: note that this may trigger some overflow long sum = 0; long sign = ((k & 0x1) == 0) ? 1 : -1; for (int j = 1; j <= k; ++j) { sign = -sign; sum += sign * binomialCoefficient(k, j) * pow(j, n); if (sum < 0) { // there was an overflow somewhere throw new MathArithmeticException(LocalizedFormats.ARGUMENT_OUTSIDE_DOMAIN, n, 0, stirlingS2.length - 1); } } return sum / factorial(k); } } } /** * Add two long integers, checking for overflow. * * @param a Addend. * @param b Addend. * @param pattern Pattern to use for any thrown exception. * @return the sum {@code a + b}. * @throws MathArithmeticException if the result cannot be represented * as a {@code long}. * @since 1.2 */ private static long addAndCheck(long a, long b, Localizable pattern) throws MathArithmeticException { long ret; if (a > b) { // use symmetry to reduce boundary cases ret = addAndCheck(b, a, pattern); } else { // assert a <= b if (a < 0) { if (b < 0) { // check for negative overflow if (Long.MIN_VALUE - b <= a) { ret = a + b; } else { throw new MathArithmeticException(pattern, a, b); } } else { // opposite sign addition is always safe ret = a + b; } } else { // assert a >= 0 // assert b >= 0 // check for positive overflow if (a <= Long.MAX_VALUE - b) { ret = a + b; } else { throw new MathArithmeticException(pattern, a, b); } } } return ret; } /** * Check binomial preconditions. * * @param n Size of the set. * @param k Size of the subsets to be counted. * @throws NotPositiveException if {@code n < 0}. * @throws NumberIsTooLargeException if {@code k > n}. */ private static void checkBinomial(final int n, final int k) throws NumberIsTooLargeException, NotPositiveException { if (n < k) { throw new NumberIsTooLargeException(LocalizedFormats.BINOMIAL_INVALID_PARAMETERS_ORDER, k, n, true); } if (n < 0) { throw new NotPositiveException(LocalizedFormats.BINOMIAL_NEGATIVE_PARAMETER, n); } } /** * Returns true if the argument is a power of two. * * @param n the number to test * @return true if the argument is a power of two */ public static boolean isPowerOfTwo(long n) { return (n > 0) && ((n & (n - 1)) == 0); } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/util/MathArrays.java100644 1750 1750 147145 12126627720 26470 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.util; import java.lang.reflect.Array; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.Comparator; import java.util.List; import org.apache.commons.math3.Field; import org.apache.commons.math3.exception.DimensionMismatchException; import org.apache.commons.math3.exception.MathArithmeticException; import org.apache.commons.math3.exception.MathIllegalArgumentException; import org.apache.commons.math3.exception.MathInternalError; import org.apache.commons.math3.exception.NonMonotonicSequenceException; import org.apache.commons.math3.exception.NotPositiveException; import org.apache.commons.math3.exception.NotStrictlyPositiveException; import org.apache.commons.math3.exception.NullArgumentException; import org.apache.commons.math3.exception.util.LocalizedFormats; /** * Arrays utilities. * * @since 3.0 * @version $Id: MathArrays.java 1462423 2013-03-29 07:25:18Z luc $ */ public class MathArrays { /** Factor used for splitting double numbers: n = 2^27 + 1 (i.e. {@value}). */ private static final int SPLIT_FACTOR = 0x8000001; /** * Private constructor. */ private MathArrays() {} /** * Real-valued function that operate on an array or a part of it. * @since 3.1 */ public interface Function { /** * Operates on an entire array. * * @param array Array to operate on. * @return the result of the operation. */ double evaluate(double[] array); /** * @param array Array to operate on. * @param startIndex Index of the first element to take into account. * @param numElements Number of elements to take into account. * @return the result of the operation. */ double evaluate(double[] array, int startIndex, int numElements); } /** * Create a copy of an array scaled by a value. * * @param arr Array to scale. * @param val Scalar. * @return scaled copy of array with each entry multiplied by val. * @since 3.2 */ public static double[] scale(double val, final double[] arr) { double[] newArr = new double[arr.length]; for (int i = 0; i < arr.length; i++) { newArr[i] = arr[i] * val; } return newArr; } /** *

                                  Multiply each element of an array by a value.

                                  * *

                                  The array is modified in place (no copy is created).

                                  * * @param arr Array to scale * @param val Scalar * @since 3.2 */ public static void scaleInPlace(double val, final double[] arr) { for (int i = 0; i < arr.length; i++) { arr[i] *= val; } } /** * Creates an array whose contents will be the element-by-element * addition of the arguments. * * @param a First term of the addition. * @param b Second term of the addition. * @return a new array {@code r} where {@code r[i] = a[i] + b[i]}. * @throws DimensionMismatchException if the array lengths differ. * @since 3.1 */ public static double[] ebeAdd(double[] a, double[] b) throws DimensionMismatchException { if (a.length != b.length) { throw new DimensionMismatchException(a.length, b.length); } final double[] result = a.clone(); for (int i = 0; i < a.length; i++) { result[i] += b[i]; } return result; } /** * Creates an array whose contents will be the element-by-element * subtraction of the second argument from the first. * * @param a First term. * @param b Element to be subtracted. * @return a new array {@code r} where {@code r[i] = a[i] - b[i]}. * @throws DimensionMismatchException if the array lengths differ. * @since 3.1 */ public static double[] ebeSubtract(double[] a, double[] b) throws DimensionMismatchException { if (a.length != b.length) { throw new DimensionMismatchException(a.length, b.length); } final double[] result = a.clone(); for (int i = 0; i < a.length; i++) { result[i] -= b[i]; } return result; } /** * Creates an array whose contents will be the element-by-element * multiplication of the arguments. * * @param a First factor of the multiplication. * @param b Second factor of the multiplication. * @return a new array {@code r} where {@code r[i] = a[i] * b[i]}. * @throws DimensionMismatchException if the array lengths differ. * @since 3.1 */ public static double[] ebeMultiply(double[] a, double[] b) throws DimensionMismatchException { if (a.length != b.length) { throw new DimensionMismatchException(a.length, b.length); } final double[] result = a.clone(); for (int i = 0; i < a.length; i++) { result[i] *= b[i]; } return result; } /** * Creates an array whose contents will be the element-by-element * division of the first argument by the second. * * @param a Numerator of the division. * @param b Denominator of the division. * @return a new array {@code r} where {@code r[i] = a[i] / b[i]}. * @throws DimensionMismatchException if the array lengths differ. * @since 3.1 */ public static double[] ebeDivide(double[] a, double[] b) throws DimensionMismatchException { if (a.length != b.length) { throw new DimensionMismatchException(a.length, b.length); } final double[] result = a.clone(); for (int i = 0; i < a.length; i++) { result[i] /= b[i]; } return result; } /** * Calculates the L1 (sum of abs) distance between two points. * * @param p1 the first point * @param p2 the second point * @return the L1 distance between the two points */ public static double distance1(double[] p1, double[] p2) { double sum = 0; for (int i = 0; i < p1.length; i++) { sum += FastMath.abs(p1[i] - p2[i]); } return sum; } /** * Calculates the L1 (sum of abs) distance between two points. * * @param p1 the first point * @param p2 the second point * @return the L1 distance between the two points */ public static int distance1(int[] p1, int[] p2) { int sum = 0; for (int i = 0; i < p1.length; i++) { sum += FastMath.abs(p1[i] - p2[i]); } return sum; } /** * Calculates the L2 (Euclidean) distance between two points. * * @param p1 the first point * @param p2 the second point * @return the L2 distance between the two points */ public static double distance(double[] p1, double[] p2) { double sum = 0; for (int i = 0; i < p1.length; i++) { final double dp = p1[i] - p2[i]; sum += dp * dp; } return FastMath.sqrt(sum); } /** * Calculates the L2 (Euclidean) distance between two points. * * @param p1 the first point * @param p2 the second point * @return the L2 distance between the two points */ public static double distance(int[] p1, int[] p2) { double sum = 0; for (int i = 0; i < p1.length; i++) { final double dp = p1[i] - p2[i]; sum += dp * dp; } return FastMath.sqrt(sum); } /** * Calculates the L (max of abs) distance between two points. * * @param p1 the first point * @param p2 the second point * @return the L distance between the two points */ public static double distanceInf(double[] p1, double[] p2) { double max = 0; for (int i = 0; i < p1.length; i++) { max = FastMath.max(max, FastMath.abs(p1[i] - p2[i])); } return max; } /** * Calculates the L (max of abs) distance between two points. * * @param p1 the first point * @param p2 the second point * @return the L distance between the two points */ public static int distanceInf(int[] p1, int[] p2) { int max = 0; for (int i = 0; i < p1.length; i++) { max = FastMath.max(max, FastMath.abs(p1[i] - p2[i])); } return max; } /** * Specification of ordering direction. */ public static enum OrderDirection { /** Constant for increasing direction. */ INCREASING, /** Constant for decreasing direction. */ DECREASING } /** * Check that an array is monotonically increasing or decreasing. * * @param the type of the elements in the specified array * @param val Values. * @param dir Ordering direction. * @param strict Whether the order should be strict. * @return {@code true} if sorted, {@code false} otherwise. */ public static > boolean isMonotonic(T[] val, OrderDirection dir, boolean strict) { T previous = val[0]; final int max = val.length; for (int i = 1; i < max; i++) { final int comp; switch (dir) { case INCREASING: comp = previous.compareTo(val[i]); if (strict) { if (comp >= 0) { return false; } } else { if (comp > 0) { return false; } } break; case DECREASING: comp = val[i].compareTo(previous); if (strict) { if (comp >= 0) { return false; } } else { if (comp > 0) { return false; } } break; default: // Should never happen. throw new MathInternalError(); } previous = val[i]; } return true; } /** * Check that an array is monotonically increasing or decreasing. * * @param val Values. * @param dir Ordering direction. * @param strict Whether the order should be strict. * @return {@code true} if sorted, {@code false} otherwise. */ public static boolean isMonotonic(double[] val, OrderDirection dir, boolean strict) { return checkOrder(val, dir, strict, false); } /** * Check that the given array is sorted. * * @param val Values. * @param dir Ordering direction. * @param strict Whether the order should be strict. * @param abort Whether to throw an exception if the check fails. * @return {@code true} if the array is sorted. * @throws NonMonotonicSequenceException if the array is not sorted * and {@code abort} is {@code true}. */ public static boolean checkOrder(double[] val, OrderDirection dir, boolean strict, boolean abort) throws NonMonotonicSequenceException { double previous = val[0]; final int max = val.length; int index; ITEM: for (index = 1; index < max; index++) { switch (dir) { case INCREASING: if (strict) { if (val[index] <= previous) { break ITEM; } } else { if (val[index] < previous) { break ITEM; } } break; case DECREASING: if (strict) { if (val[index] >= previous) { break ITEM; } } else { if (val[index] > previous) { break ITEM; } } break; default: // Should never happen. throw new MathInternalError(); } previous = val[index]; } if (index == max) { // Loop completed. return true; } // Loop early exit means wrong ordering. if (abort) { throw new NonMonotonicSequenceException(val[index], previous, index, dir, strict); } else { return false; } } /** * Check that the given array is sorted. * * @param val Values. * @param dir Ordering direction. * @param strict Whether the order should be strict. * @throws NonMonotonicSequenceException if the array is not sorted. * @since 2.2 */ public static void checkOrder(double[] val, OrderDirection dir, boolean strict) throws NonMonotonicSequenceException { checkOrder(val, dir, strict, true); } /** * Check that the given array is sorted in strictly increasing order. * * @param val Values. * @throws NonMonotonicSequenceException if the array is not sorted. * @since 2.2 */ public static void checkOrder(double[] val) throws NonMonotonicSequenceException { checkOrder(val, OrderDirection.INCREASING, true); } /** * Throws DimensionMismatchException if the input array is not rectangular. * * @param in array to be tested * @throws NullArgumentException if input array is null * @throws DimensionMismatchException if input array is not rectangular * @since 3.1 */ public static void checkRectangular(final long[][] in) throws NullArgumentException, DimensionMismatchException { MathUtils.checkNotNull(in); for (int i = 1; i < in.length; i++) { if (in[i].length != in[0].length) { throw new DimensionMismatchException( LocalizedFormats.DIFFERENT_ROWS_LENGTHS, in[i].length, in[0].length); } } } /** * Check that all entries of the input array are strictly positive. * * @param in Array to be tested * @throws NotStrictlyPositiveException if any entries of the array are not * strictly positive. * @since 3.1 */ public static void checkPositive(final double[] in) throws NotStrictlyPositiveException { for (int i = 0; i < in.length; i++) { if (in[i] <= 0) { throw new NotStrictlyPositiveException(in[i]); } } } /** * Check that all entries of the input array are >= 0. * * @param in Array to be tested * @throws NotPositiveException if any array entries are less than 0. * @since 3.1 */ public static void checkNonNegative(final long[] in) throws NotPositiveException { for (int i = 0; i < in.length; i++) { if (in[i] < 0) { throw new NotPositiveException(in[i]); } } } /** * Check all entries of the input array are >= 0. * * @param in Array to be tested * @throws NotPositiveException if any array entries are less than 0. * @since 3.1 */ public static void checkNonNegative(final long[][] in) throws NotPositiveException { for (int i = 0; i < in.length; i ++) { for (int j = 0; j < in[i].length; j++) { if (in[i][j] < 0) { throw new NotPositiveException(in[i][j]); } } } } /** * Returns the Cartesian norm (2-norm), handling both overflow and underflow. * Translation of the minpack enorm subroutine. * * The redistribution policy for MINPACK is available * here, for * convenience, it is reproduced below.

                                  * * * * *
                                  * Minpack Copyright Notice (1999) University of Chicago. * All rights reserved *
                                  * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: *
                                    *
                                  1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer.
                                  2. *
                                  3. Redistributions in binary form must reproduce the above * copyright notice, this list of conditions and the following * disclaimer in the documentation and/or other materials provided * with the distribution.
                                  4. *
                                  5. The end-user documentation included with the redistribution, if any, * must include the following acknowledgment: * {@code This product includes software developed by the University of * Chicago, as Operator of Argonne National Laboratory.} * Alternately, this acknowledgment may appear in the software itself, * if and wherever such third-party acknowledgments normally appear.
                                  6. *
                                  7. WARRANTY DISCLAIMER. THE SOFTWARE IS SUPPLIED "AS IS" * WITHOUT WARRANTY OF ANY KIND. THE COPYRIGHT HOLDER, THE * UNITED STATES, THE UNITED STATES DEPARTMENT OF ENERGY, AND * THEIR EMPLOYEES: (1) DISCLAIM ANY WARRANTIES, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO ANY IMPLIED WARRANTIES * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE * OR NON-INFRINGEMENT, (2) DO NOT ASSUME ANY LEGAL LIABILITY * OR RESPONSIBILITY FOR THE ACCURACY, COMPLETENESS, OR * USEFULNESS OF THE SOFTWARE, (3) DO NOT REPRESENT THAT USE OF * THE SOFTWARE WOULD NOT INFRINGE PRIVATELY OWNED RIGHTS, (4) * DO NOT WARRANT THAT THE SOFTWARE WILL FUNCTION * UNINTERRUPTED, THAT IT IS ERROR-FREE OR THAT ANY ERRORS WILL * BE CORRECTED.
                                  8. *
                                  9. LIMITATION OF LIABILITY. IN NO EVENT WILL THE COPYRIGHT * HOLDER, THE UNITED STATES, THE UNITED STATES DEPARTMENT OF * ENERGY, OR THEIR EMPLOYEES: BE LIABLE FOR ANY INDIRECT, * INCIDENTAL, CONSEQUENTIAL, SPECIAL OR PUNITIVE DAMAGES OF * ANY KIND OR NATURE, INCLUDING BUT NOT LIMITED TO LOSS OF * PROFITS OR LOSS OF DATA, FOR ANY REASON WHATSOEVER, WHETHER * SUCH LIABILITY IS ASSERTED ON THE BASIS OF CONTRACT, TORT * (INCLUDING NEGLIGENCE OR STRICT LIABILITY), OR OTHERWISE, * EVEN IF ANY OF SAID PARTIES HAS BEEN WARNED OF THE * POSSIBILITY OF SUCH LOSS OR DAMAGES.
                                  10. *
                                    * * @param v Vector of doubles. * @return the 2-norm of the vector. * @since 2.2 */ public static double safeNorm(double[] v) { double rdwarf = 3.834e-20; double rgiant = 1.304e+19; double s1 = 0; double s2 = 0; double s3 = 0; double x1max = 0; double x3max = 0; double floatn = v.length; double agiant = rgiant / floatn; for (int i = 0; i < v.length; i++) { double xabs = Math.abs(v[i]); if (xabs < rdwarf || xabs > agiant) { if (xabs > rdwarf) { if (xabs > x1max) { double r = x1max / xabs; s1= 1 + s1 * r * r; x1max = xabs; } else { double r = xabs / x1max; s1 += r * r; } } else { if (xabs > x3max) { double r = x3max / xabs; s3= 1 + s3 * r * r; x3max = xabs; } else { if (xabs != 0) { double r = xabs / x3max; s3 += r * r; } } } } else { s2 += xabs * xabs; } } double norm; if (s1 != 0) { norm = x1max * Math.sqrt(s1 + (s2 / x1max) / x1max); } else { if (s2 == 0) { norm = x3max * Math.sqrt(s3); } else { if (s2 >= x3max) { norm = Math.sqrt(s2 * (1 + (x3max / s2) * (x3max * s3))); } else { norm = Math.sqrt(x3max * ((s2 / x3max) + (x3max * s3))); } } } return norm; } /** * Sort an array in ascending order in place and perform the same reordering * of entries on other arrays. For example, if * {@code x = [3, 1, 2], y = [1, 2, 3]} and {@code z = [0, 5, 7]}, then * {@code sortInPlace(x, y, z)} will update {@code x} to {@code [1, 2, 3]}, * {@code y} to {@code [2, 3, 1]} and {@code z} to {@code [5, 7, 0]}. * * @param x Array to be sorted and used as a pattern for permutation * of the other arrays. * @param yList Set of arrays whose permutations of entries will follow * those performed on {@code x}. * @throws DimensionMismatchException if any {@code y} is not the same * size as {@code x}. * @throws NullArgumentException if {@code x} or any {@code y} is null. * @since 3.0 */ public static void sortInPlace(double[] x, double[] ... yList) throws DimensionMismatchException, NullArgumentException { sortInPlace(x, OrderDirection.INCREASING, yList); } /** * Sort an array in place and perform the same reordering of entries on * other arrays. This method works the same as the other * {@link #sortInPlace(double[], double[][]) sortInPlace} method, but * allows the order of the sort to be provided in the {@code dir} * parameter. * * @param x Array to be sorted and used as a pattern for permutation * of the other arrays. * @param dir Order direction. * @param yList Set of arrays whose permutations of entries will follow * those performed on {@code x}. * @throws DimensionMismatchException if any {@code y} is not the same * size as {@code x}. * @throws NullArgumentException if {@code x} or any {@code y} is null * @since 3.0 */ public static void sortInPlace(double[] x, final OrderDirection dir, double[] ... yList) throws NullArgumentException, DimensionMismatchException { if (x == null) { throw new NullArgumentException(); } final int len = x.length; final List> list = new ArrayList>(len); final int yListLen = yList.length; for (int i = 0; i < len; i++) { final double[] yValues = new double[yListLen]; for (int j = 0; j < yListLen; j++) { double[] y = yList[j]; if (y == null) { throw new NullArgumentException(); } if (y.length != len) { throw new DimensionMismatchException(y.length, len); } yValues[j] = y[i]; } list.add(new Pair(x[i], yValues)); } final Comparator> comp = new Comparator>() { public int compare(Pair o1, Pair o2) { int val; switch (dir) { case INCREASING: val = o1.getKey().compareTo(o2.getKey()); break; case DECREASING: val = o2.getKey().compareTo(o1.getKey()); break; default: // Should never happen. throw new MathInternalError(); } return val; } }; Collections.sort(list, comp); for (int i = 0; i < len; i++) { final Pair e = list.get(i); x[i] = e.getKey(); final double[] yValues = e.getValue(); for (int j = 0; j < yListLen; j++) { yList[j][i] = yValues[j]; } } } /** * Creates a copy of the {@code source} array. * * @param source Array to be copied. * @return the copied array. */ public static int[] copyOf(int[] source) { return copyOf(source, source.length); } /** * Creates a copy of the {@code source} array. * * @param source Array to be copied. * @return the copied array. */ public static double[] copyOf(double[] source) { return copyOf(source, source.length); } /** * Creates a copy of the {@code source} array. * * @param source Array to be copied. * @param len Number of entries to copy. If smaller then the source * length, the copy will be truncated, if larger it will padded with * zeroes. * @return the copied array. */ public static int[] copyOf(int[] source, int len) { final int[] output = new int[len]; System.arraycopy(source, 0, output, 0, FastMath.min(len, source.length)); return output; } /** * Creates a copy of the {@code source} array. * * @param source Array to be copied. * @param len Number of entries to copy. If smaller then the source * length, the copy will be truncated, if larger it will padded with * zeroes. * @return the copied array. */ public static double[] copyOf(double[] source, int len) { final double[] output = new double[len]; System.arraycopy(source, 0, output, 0, FastMath.min(len, source.length)); return output; } /** * Compute a linear combination accurately. * This method computes the sum of the products * ai bi to high accuracy. * It does so by using specific multiplication and addition algorithms to * preserve accuracy and reduce cancellation effects. *
                                    * It is based on the 2005 paper * * Accurate Sum and Dot Product by Takeshi Ogita, Siegfried M. Rump, * and Shin'ichi Oishi published in SIAM J. Sci. Comput. * * @param a Factors. * @param b Factors. * @return Σi ai bi. * @throws DimensionMismatchException if arrays dimensions don't match */ public static double linearCombination(final double[] a, final double[] b) throws DimensionMismatchException { final int len = a.length; if (len != b.length) { throw new DimensionMismatchException(len, b.length); } final double[] prodHigh = new double[len]; double prodLowSum = 0; for (int i = 0; i < len; i++) { final double ai = a[i]; final double ca = SPLIT_FACTOR * ai; final double aHigh = ca - (ca - ai); final double aLow = ai - aHigh; final double bi = b[i]; final double cb = SPLIT_FACTOR * bi; final double bHigh = cb - (cb - bi); final double bLow = bi - bHigh; prodHigh[i] = ai * bi; final double prodLow = aLow * bLow - (((prodHigh[i] - aHigh * bHigh) - aLow * bHigh) - aHigh * bLow); prodLowSum += prodLow; } final double prodHighCur = prodHigh[0]; double prodHighNext = prodHigh[1]; double sHighPrev = prodHighCur + prodHighNext; double sPrime = sHighPrev - prodHighNext; double sLowSum = (prodHighNext - (sHighPrev - sPrime)) + (prodHighCur - sPrime); final int lenMinusOne = len - 1; for (int i = 1; i < lenMinusOne; i++) { prodHighNext = prodHigh[i + 1]; final double sHighCur = sHighPrev + prodHighNext; sPrime = sHighCur - prodHighNext; sLowSum += (prodHighNext - (sHighCur - sPrime)) + (sHighPrev - sPrime); sHighPrev = sHighCur; } double result = sHighPrev + (prodLowSum + sLowSum); if (Double.isNaN(result)) { // either we have split infinite numbers or some coefficients were NaNs, // just rely on the naive implementation and let IEEE754 handle this result = 0; for (int i = 0; i < len; ++i) { result += a[i] * b[i]; } } return result; } /** * Compute a linear combination accurately. *

                                    * This method computes a1×b1 + * a2×b2 to high accuracy. It does * so by using specific multiplication and addition algorithms to * preserve accuracy and reduce cancellation effects. It is based * on the 2005 paper * Accurate Sum and Dot Product by Takeshi Ogita, * Siegfried M. Rump, and Shin'ichi Oishi published in SIAM J. Sci. Comput. *

                                    * @param a1 first factor of the first term * @param b1 second factor of the first term * @param a2 first factor of the second term * @param b2 second factor of the second term * @return a1×b1 + * a2×b2 * @see #linearCombination(double, double, double, double, double, double) * @see #linearCombination(double, double, double, double, double, double, double, double) */ public static double linearCombination(final double a1, final double b1, final double a2, final double b2) { // the code below is split in many additions/subtractions that may // appear redundant. However, they should NOT be simplified, as they // use IEEE754 floating point arithmetic rounding properties. // as an example, the expression "ca1 - (ca1 - a1)" is NOT the same as "a1" // The variable naming conventions are that xyzHigh contains the most significant // bits of xyz and xyzLow contains its least significant bits. So theoretically // xyz is the sum xyzHigh + xyzLow, but in many cases below, this sum cannot // be represented in only one double precision number so we preserve two numbers // to hold it as long as we can, combining the high and low order bits together // only at the end, after cancellation may have occurred on high order bits // split a1 and b1 as two 26 bits numbers final double ca1 = SPLIT_FACTOR * a1; final double a1High = ca1 - (ca1 - a1); final double a1Low = a1 - a1High; final double cb1 = SPLIT_FACTOR * b1; final double b1High = cb1 - (cb1 - b1); final double b1Low = b1 - b1High; // accurate multiplication a1 * b1 final double prod1High = a1 * b1; final double prod1Low = a1Low * b1Low - (((prod1High - a1High * b1High) - a1Low * b1High) - a1High * b1Low); // split a2 and b2 as two 26 bits numbers final double ca2 = SPLIT_FACTOR * a2; final double a2High = ca2 - (ca2 - a2); final double a2Low = a2 - a2High; final double cb2 = SPLIT_FACTOR * b2; final double b2High = cb2 - (cb2 - b2); final double b2Low = b2 - b2High; // accurate multiplication a2 * b2 final double prod2High = a2 * b2; final double prod2Low = a2Low * b2Low - (((prod2High - a2High * b2High) - a2Low * b2High) - a2High * b2Low); // accurate addition a1 * b1 + a2 * b2 final double s12High = prod1High + prod2High; final double s12Prime = s12High - prod2High; final double s12Low = (prod2High - (s12High - s12Prime)) + (prod1High - s12Prime); // final rounding, s12 may have suffered many cancellations, we try // to recover some bits from the extra words we have saved up to now double result = s12High + (prod1Low + prod2Low + s12Low); if (Double.isNaN(result)) { // either we have split infinite numbers or some coefficients were NaNs, // just rely on the naive implementation and let IEEE754 handle this result = a1 * b1 + a2 * b2; } return result; } /** * Compute a linear combination accurately. *

                                    * This method computes a1×b1 + * a2×b2 + a3×b3 * to high accuracy. It does so by using specific multiplication and * addition algorithms to preserve accuracy and reduce cancellation effects. * It is based on the 2005 paper * Accurate Sum and Dot Product by Takeshi Ogita, * Siegfried M. Rump, and Shin'ichi Oishi published in SIAM J. Sci. Comput. *

                                    * @param a1 first factor of the first term * @param b1 second factor of the first term * @param a2 first factor of the second term * @param b2 second factor of the second term * @param a3 first factor of the third term * @param b3 second factor of the third term * @return a1×b1 + * a2×b2 + a3×b3 * @see #linearCombination(double, double, double, double) * @see #linearCombination(double, double, double, double, double, double, double, double) */ public static double linearCombination(final double a1, final double b1, final double a2, final double b2, final double a3, final double b3) { // the code below is split in many additions/subtractions that may // appear redundant. However, they should NOT be simplified, as they // do use IEEE754 floating point arithmetic rounding properties. // as an example, the expression "ca1 - (ca1 - a1)" is NOT the same as "a1" // The variables naming conventions are that xyzHigh contains the most significant // bits of xyz and xyzLow contains its least significant bits. So theoretically // xyz is the sum xyzHigh + xyzLow, but in many cases below, this sum cannot // be represented in only one double precision number so we preserve two numbers // to hold it as long as we can, combining the high and low order bits together // only at the end, after cancellation may have occurred on high order bits // split a1 and b1 as two 26 bits numbers final double ca1 = SPLIT_FACTOR * a1; final double a1High = ca1 - (ca1 - a1); final double a1Low = a1 - a1High; final double cb1 = SPLIT_FACTOR * b1; final double b1High = cb1 - (cb1 - b1); final double b1Low = b1 - b1High; // accurate multiplication a1 * b1 final double prod1High = a1 * b1; final double prod1Low = a1Low * b1Low - (((prod1High - a1High * b1High) - a1Low * b1High) - a1High * b1Low); // split a2 and b2 as two 26 bits numbers final double ca2 = SPLIT_FACTOR * a2; final double a2High = ca2 - (ca2 - a2); final double a2Low = a2 - a2High; final double cb2 = SPLIT_FACTOR * b2; final double b2High = cb2 - (cb2 - b2); final double b2Low = b2 - b2High; // accurate multiplication a2 * b2 final double prod2High = a2 * b2; final double prod2Low = a2Low * b2Low - (((prod2High - a2High * b2High) - a2Low * b2High) - a2High * b2Low); // split a3 and b3 as two 26 bits numbers final double ca3 = SPLIT_FACTOR * a3; final double a3High = ca3 - (ca3 - a3); final double a3Low = a3 - a3High; final double cb3 = SPLIT_FACTOR * b3; final double b3High = cb3 - (cb3 - b3); final double b3Low = b3 - b3High; // accurate multiplication a3 * b3 final double prod3High = a3 * b3; final double prod3Low = a3Low * b3Low - (((prod3High - a3High * b3High) - a3Low * b3High) - a3High * b3Low); // accurate addition a1 * b1 + a2 * b2 final double s12High = prod1High + prod2High; final double s12Prime = s12High - prod2High; final double s12Low = (prod2High - (s12High - s12Prime)) + (prod1High - s12Prime); // accurate addition a1 * b1 + a2 * b2 + a3 * b3 final double s123High = s12High + prod3High; final double s123Prime = s123High - prod3High; final double s123Low = (prod3High - (s123High - s123Prime)) + (s12High - s123Prime); // final rounding, s123 may have suffered many cancellations, we try // to recover some bits from the extra words we have saved up to now double result = s123High + (prod1Low + prod2Low + prod3Low + s12Low + s123Low); if (Double.isNaN(result)) { // either we have split infinite numbers or some coefficients were NaNs, // just rely on the naive implementation and let IEEE754 handle this result = a1 * b1 + a2 * b2 + a3 * b3; } return result; } /** * Compute a linear combination accurately. *

                                    * This method computes a1×b1 + * a2×b2 + a3×b3 + * a4×b4 * to high accuracy. It does so by using specific multiplication and * addition algorithms to preserve accuracy and reduce cancellation effects. * It is based on the 2005 paper * Accurate Sum and Dot Product by Takeshi Ogita, * Siegfried M. Rump, and Shin'ichi Oishi published in SIAM J. Sci. Comput. *

                                    * @param a1 first factor of the first term * @param b1 second factor of the first term * @param a2 first factor of the second term * @param b2 second factor of the second term * @param a3 first factor of the third term * @param b3 second factor of the third term * @param a4 first factor of the third term * @param b4 second factor of the third term * @return a1×b1 + * a2×b2 + a3×b3 + * a4×b4 * @see #linearCombination(double, double, double, double) * @see #linearCombination(double, double, double, double, double, double) */ public static double linearCombination(final double a1, final double b1, final double a2, final double b2, final double a3, final double b3, final double a4, final double b4) { // the code below is split in many additions/subtractions that may // appear redundant. However, they should NOT be simplified, as they // do use IEEE754 floating point arithmetic rounding properties. // as an example, the expression "ca1 - (ca1 - a1)" is NOT the same as "a1" // The variables naming conventions are that xyzHigh contains the most significant // bits of xyz and xyzLow contains its least significant bits. So theoretically // xyz is the sum xyzHigh + xyzLow, but in many cases below, this sum cannot // be represented in only one double precision number so we preserve two numbers // to hold it as long as we can, combining the high and low order bits together // only at the end, after cancellation may have occurred on high order bits // split a1 and b1 as two 26 bits numbers final double ca1 = SPLIT_FACTOR * a1; final double a1High = ca1 - (ca1 - a1); final double a1Low = a1 - a1High; final double cb1 = SPLIT_FACTOR * b1; final double b1High = cb1 - (cb1 - b1); final double b1Low = b1 - b1High; // accurate multiplication a1 * b1 final double prod1High = a1 * b1; final double prod1Low = a1Low * b1Low - (((prod1High - a1High * b1High) - a1Low * b1High) - a1High * b1Low); // split a2 and b2 as two 26 bits numbers final double ca2 = SPLIT_FACTOR * a2; final double a2High = ca2 - (ca2 - a2); final double a2Low = a2 - a2High; final double cb2 = SPLIT_FACTOR * b2; final double b2High = cb2 - (cb2 - b2); final double b2Low = b2 - b2High; // accurate multiplication a2 * b2 final double prod2High = a2 * b2; final double prod2Low = a2Low * b2Low - (((prod2High - a2High * b2High) - a2Low * b2High) - a2High * b2Low); // split a3 and b3 as two 26 bits numbers final double ca3 = SPLIT_FACTOR * a3; final double a3High = ca3 - (ca3 - a3); final double a3Low = a3 - a3High; final double cb3 = SPLIT_FACTOR * b3; final double b3High = cb3 - (cb3 - b3); final double b3Low = b3 - b3High; // accurate multiplication a3 * b3 final double prod3High = a3 * b3; final double prod3Low = a3Low * b3Low - (((prod3High - a3High * b3High) - a3Low * b3High) - a3High * b3Low); // split a4 and b4 as two 26 bits numbers final double ca4 = SPLIT_FACTOR * a4; final double a4High = ca4 - (ca4 - a4); final double a4Low = a4 - a4High; final double cb4 = SPLIT_FACTOR * b4; final double b4High = cb4 - (cb4 - b4); final double b4Low = b4 - b4High; // accurate multiplication a4 * b4 final double prod4High = a4 * b4; final double prod4Low = a4Low * b4Low - (((prod4High - a4High * b4High) - a4Low * b4High) - a4High * b4Low); // accurate addition a1 * b1 + a2 * b2 final double s12High = prod1High + prod2High; final double s12Prime = s12High - prod2High; final double s12Low = (prod2High - (s12High - s12Prime)) + (prod1High - s12Prime); // accurate addition a1 * b1 + a2 * b2 + a3 * b3 final double s123High = s12High + prod3High; final double s123Prime = s123High - prod3High; final double s123Low = (prod3High - (s123High - s123Prime)) + (s12High - s123Prime); // accurate addition a1 * b1 + a2 * b2 + a3 * b3 + a4 * b4 final double s1234High = s123High + prod4High; final double s1234Prime = s1234High - prod4High; final double s1234Low = (prod4High - (s1234High - s1234Prime)) + (s123High - s1234Prime); // final rounding, s1234 may have suffered many cancellations, we try // to recover some bits from the extra words we have saved up to now double result = s1234High + (prod1Low + prod2Low + prod3Low + prod4Low + s12Low + s123Low + s1234Low); if (Double.isNaN(result)) { // either we have split infinite numbers or some coefficients were NaNs, // just rely on the naive implementation and let IEEE754 handle this result = a1 * b1 + a2 * b2 + a3 * b3 + a4 * b4; } return result; } /** * Returns true iff both arguments are null or have same dimensions and all * their elements are equal as defined by * {@link Precision#equals(float,float)}. * * @param x first array * @param y second array * @return true if the values are both null or have same dimension * and equal elements. */ public static boolean equals(float[] x, float[] y) { if ((x == null) || (y == null)) { return !((x == null) ^ (y == null)); } if (x.length != y.length) { return false; } for (int i = 0; i < x.length; ++i) { if (!Precision.equals(x[i], y[i])) { return false; } } return true; } /** * Returns true iff both arguments are null or have same dimensions and all * their elements are equal as defined by * {@link Precision#equalsIncludingNaN(double,double) this method}. * * @param x first array * @param y second array * @return true if the values are both null or have same dimension and * equal elements * @since 2.2 */ public static boolean equalsIncludingNaN(float[] x, float[] y) { if ((x == null) || (y == null)) { return !((x == null) ^ (y == null)); } if (x.length != y.length) { return false; } for (int i = 0; i < x.length; ++i) { if (!Precision.equalsIncludingNaN(x[i], y[i])) { return false; } } return true; } /** * Returns {@code true} iff both arguments are {@code null} or have same * dimensions and all their elements are equal as defined by * {@link Precision#equals(double,double)}. * * @param x First array. * @param y Second array. * @return {@code true} if the values are both {@code null} or have same * dimension and equal elements. */ public static boolean equals(double[] x, double[] y) { if ((x == null) || (y == null)) { return !((x == null) ^ (y == null)); } if (x.length != y.length) { return false; } for (int i = 0; i < x.length; ++i) { if (!Precision.equals(x[i], y[i])) { return false; } } return true; } /** * Returns {@code true} iff both arguments are {@code null} or have same * dimensions and all their elements are equal as defined by * {@link Precision#equalsIncludingNaN(double,double) this method}. * * @param x First array. * @param y Second array. * @return {@code true} if the values are both {@code null} or have same * dimension and equal elements. * @since 2.2 */ public static boolean equalsIncludingNaN(double[] x, double[] y) { if ((x == null) || (y == null)) { return !((x == null) ^ (y == null)); } if (x.length != y.length) { return false; } for (int i = 0; i < x.length; ++i) { if (!Precision.equalsIncludingNaN(x[i], y[i])) { return false; } } return true; } /** * Normalizes an array to make it sum to a specified value. * Returns the result of the transformation
                                          *    x |-> x * normalizedSum / sum
                                          * 
                                    * applied to each non-NaN element x of the input array, where sum is the * sum of the non-NaN entries in the input array.

                                    * *

                                    Throws IllegalArgumentException if {@code normalizedSum} is infinite * or NaN and ArithmeticException if the input array contains any infinite elements * or sums to 0.

                                    * *

                                    Ignores (i.e., copies unchanged to the output array) NaNs in the input array.

                                    * * @param values Input array to be normalized * @param normalizedSum Target sum for the normalized array * @return the normalized array. * @throws MathArithmeticException if the input array contains infinite * elements or sums to zero. * @throws MathIllegalArgumentException if the target sum is infinite or {@code NaN}. * @since 2.1 */ public static double[] normalizeArray(double[] values, double normalizedSum) throws MathIllegalArgumentException, MathArithmeticException { if (Double.isInfinite(normalizedSum)) { throw new MathIllegalArgumentException(LocalizedFormats.NORMALIZE_INFINITE); } if (Double.isNaN(normalizedSum)) { throw new MathIllegalArgumentException(LocalizedFormats.NORMALIZE_NAN); } double sum = 0d; final int len = values.length; double[] out = new double[len]; for (int i = 0; i < len; i++) { if (Double.isInfinite(values[i])) { throw new MathIllegalArgumentException(LocalizedFormats.INFINITE_ARRAY_ELEMENT, values[i], i); } if (!Double.isNaN(values[i])) { sum += values[i]; } } if (sum == 0) { throw new MathArithmeticException(LocalizedFormats.ARRAY_SUMS_TO_ZERO); } for (int i = 0; i < len; i++) { if (Double.isNaN(values[i])) { out[i] = Double.NaN; } else { out[i] = values[i] * normalizedSum / sum; } } return out; } /** Build an array of elements. *

                                    * Arrays are filled with field.getZero() *

                                    * @param the type of the field elements * @param field field to which array elements belong * @param length of the array * @return a new array * @since 3.2 */ public static T[] buildArray(final Field field, final int length) { @SuppressWarnings("unchecked") // OK because field must be correct class T[] array = (T[]) Array.newInstance(field.getRuntimeClass(), length); Arrays.fill(array, field.getZero()); return array; } /** Build a double dimension array of elements. *

                                    * Arrays are filled with field.getZero() *

                                    * @param the type of the field elements * @param field field to which array elements belong * @param rows number of rows in the array * @param columns number of columns (may be negative to build partial * arrays in the same way new Field[rows][] works) * @return a new array * @since 3.2 */ @SuppressWarnings("unchecked") public static T[][] buildArray(final Field field, final int rows, final int columns) { final T[][] array; if (columns < 0) { T[] dummyRow = buildArray(field, 0); array = (T[][]) Array.newInstance(dummyRow.getClass(), rows); } else { array = (T[][]) Array.newInstance(field.getRuntimeClass(), new int[] { rows, columns }); for (int i = 0; i < rows; ++i) { Arrays.fill(array[i], field.getZero()); } } return array; } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/util/IterationManager.java100644 1750 1750 13255 12126627720 27620 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.util; import java.util.Collection; import java.util.concurrent.CopyOnWriteArrayList; import org.apache.commons.math3.exception.MaxCountExceededException; /** * This abstract class provides a general framework for managing iterative * algorithms. The maximum number of iterations can be set, and methods are * provided to monitor the current iteration count. A lightweight event * framework is also provided. * * @version $Id: IterationManager.java 1422313 2012-12-15 18:53:41Z psteitz $ */ public class IterationManager { /** Keeps a count of the number of iterations. */ private final Incrementor iterations; /** The collection of all listeners attached to this iterative algorithm. */ private final Collection listeners; /** * Creates a new instance of this class. * * @param maxIterations the maximum number of iterations */ public IterationManager(final int maxIterations) { this.iterations = new Incrementor(maxIterations); this.listeners = new CopyOnWriteArrayList(); } /** * Creates a new instance of this class. * * @param maxIterations the maximum number of iterations * @param callBack the function to be called when the maximum number of * iterations has been reached * @throws org.apache.commons.math3.exception.NullArgumentException if {@code callBack} is {@code null} * @since 3.1 */ public IterationManager(final int maxIterations, final Incrementor.MaxCountExceededCallback callBack) { this.iterations = new Incrementor(maxIterations, callBack); this.listeners = new CopyOnWriteArrayList(); } /** * Attaches a listener to this manager. * * @param listener A {@code IterationListener} object. */ public void addIterationListener(final IterationListener listener) { listeners.add(listener); } /** * Informs all registered listeners that the initial phase (prior to the * main iteration loop) has been completed. * * @param e The {@link IterationEvent} object. */ public void fireInitializationEvent(final IterationEvent e) { for (IterationListener l : listeners) { l.initializationPerformed(e); } } /** * Informs all registered listeners that a new iteration (in the main * iteration loop) has been performed. * * @param e The {@link IterationEvent} object. */ public void fireIterationPerformedEvent(final IterationEvent e) { for (IterationListener l : listeners) { l.iterationPerformed(e); } } /** * Informs all registered listeners that a new iteration (in the main * iteration loop) has been started. * * @param e The {@link IterationEvent} object. */ public void fireIterationStartedEvent(final IterationEvent e) { for (IterationListener l : listeners) { l.iterationStarted(e); } } /** * Informs all registered listeners that the final phase (post-iterations) * has been completed. * * @param e The {@link IterationEvent} object. */ public void fireTerminationEvent(final IterationEvent e) { for (IterationListener l : listeners) { l.terminationPerformed(e); } } /** * Returns the number of iterations of this solver, 0 if no iterations has * been performed yet. * * @return the number of iterations. */ public int getIterations() { return iterations.getCount(); } /** * Returns the maximum number of iterations. * * @return the maximum number of iterations. */ public int getMaxIterations() { return iterations.getMaximalCount(); } /** * Increments the iteration count by one, and throws an exception if the * maximum number of iterations is reached. This method should be called at * the beginning of a new iteration. * * @throws MaxCountExceededException if the maximum number of iterations is * reached. */ public void incrementIterationCount() throws MaxCountExceededException { iterations.incrementCount(); } /** * Removes the specified iteration listener from the list of listeners * currently attached to {@code this} object. Attempting to remove a * listener which was not previously registered does not cause any * error. * * @param listener The {@link IterationListener} to be removed. */ public void removeIterationListener(final IterationListener listener) { listeners.remove(listener); } /** * Sets the iteration count to 0. This method must be called during the * initial phase. */ public void resetIterationCount() { iterations.resetCount(); } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/util/OpenIntToFieldHashMap.java100644 1750 1750 45003 12126627720 30450 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.util; import java.io.IOException; import java.io.ObjectInputStream; import java.io.Serializable; import java.lang.reflect.Array; import java.util.ConcurrentModificationException; import java.util.NoSuchElementException; import org.apache.commons.math3.Field; import org.apache.commons.math3.FieldElement; /** * Open addressed map from int to FieldElement. *

                                    This class provides a dedicated map from integers to FieldElements with a * much smaller memory overhead than standard java.util.Map.

                                    *

                                    This class is not synchronized. The specialized iterators returned by * {@link #iterator()} are fail-fast: they throw a * ConcurrentModificationException when they detect the map has been * modified during iteration.

                                    * @param the type of the field elements * @version $Id: OpenIntToFieldHashMap.java 1421448 2012-12-13 19:45:57Z tn $ * @since 2.0 */ public class OpenIntToFieldHashMap> implements Serializable { /** Status indicator for free table entries. */ protected static final byte FREE = 0; /** Status indicator for full table entries. */ protected static final byte FULL = 1; /** Status indicator for removed table entries. */ protected static final byte REMOVED = 2; /** Serializable version identifier. */ private static final long serialVersionUID = -9179080286849120720L; /** Load factor for the map. */ private static final float LOAD_FACTOR = 0.5f; /** Default starting size. *

                                    This must be a power of two for bit mask to work properly.

                                    */ private static final int DEFAULT_EXPECTED_SIZE = 16; /** Multiplier for size growth when map fills up. *

                                    This must be a power of two for bit mask to work properly.

                                    */ private static final int RESIZE_MULTIPLIER = 2; /** Number of bits to perturb the index when probing for collision resolution. */ private static final int PERTURB_SHIFT = 5; /** Field to which the elements belong. */ private final Field field; /** Keys table. */ private int[] keys; /** Values table. */ private T[] values; /** States table. */ private byte[] states; /** Return value for missing entries. */ private final T missingEntries; /** Current size of the map. */ private int size; /** Bit mask for hash values. */ private int mask; /** Modifications count. */ private transient int count; /** * Build an empty map with default size and using zero for missing entries. * @param field field to which the elements belong */ public OpenIntToFieldHashMap(final Fieldfield) { this(field, DEFAULT_EXPECTED_SIZE, field.getZero()); } /** * Build an empty map with default size * @param field field to which the elements belong * @param missingEntries value to return when a missing entry is fetched */ public OpenIntToFieldHashMap(final Fieldfield, final T missingEntries) { this(field,DEFAULT_EXPECTED_SIZE, missingEntries); } /** * Build an empty map with specified size and using zero for missing entries. * @param field field to which the elements belong * @param expectedSize expected number of elements in the map */ public OpenIntToFieldHashMap(final Field field,final int expectedSize) { this(field,expectedSize, field.getZero()); } /** * Build an empty map with specified size. * @param field field to which the elements belong * @param expectedSize expected number of elements in the map * @param missingEntries value to return when a missing entry is fetched */ public OpenIntToFieldHashMap(final Field field,final int expectedSize, final T missingEntries) { this.field = field; final int capacity = computeCapacity(expectedSize); keys = new int[capacity]; values = buildArray(capacity); states = new byte[capacity]; this.missingEntries = missingEntries; mask = capacity - 1; } /** * Copy constructor. * @param source map to copy */ public OpenIntToFieldHashMap(final OpenIntToFieldHashMap source) { field = source.field; final int length = source.keys.length; keys = new int[length]; System.arraycopy(source.keys, 0, keys, 0, length); values = buildArray(length); System.arraycopy(source.values, 0, values, 0, length); states = new byte[length]; System.arraycopy(source.states, 0, states, 0, length); missingEntries = source.missingEntries; size = source.size; mask = source.mask; count = source.count; } /** * Compute the capacity needed for a given size. * @param expectedSize expected size of the map * @return capacity to use for the specified size */ private static int computeCapacity(final int expectedSize) { if (expectedSize == 0) { return 1; } final int capacity = (int) FastMath.ceil(expectedSize / LOAD_FACTOR); final int powerOfTwo = Integer.highestOneBit(capacity); if (powerOfTwo == capacity) { return capacity; } return nextPowerOfTwo(capacity); } /** * Find the smallest power of two greater than the input value * @param i input value * @return smallest power of two greater than the input value */ private static int nextPowerOfTwo(final int i) { return Integer.highestOneBit(i) << 1; } /** * Get the stored value associated with the given key * @param key key associated with the data * @return data associated with the key */ public T get(final int key) { final int hash = hashOf(key); int index = hash & mask; if (containsKey(key, index)) { return values[index]; } if (states[index] == FREE) { return missingEntries; } int j = index; for (int perturb = perturb(hash); states[index] != FREE; perturb >>= PERTURB_SHIFT) { j = probe(perturb, j); index = j & mask; if (containsKey(key, index)) { return values[index]; } } return missingEntries; } /** * Check if a value is associated with a key. * @param key key to check * @return true if a value is associated with key */ public boolean containsKey(final int key) { final int hash = hashOf(key); int index = hash & mask; if (containsKey(key, index)) { return true; } if (states[index] == FREE) { return false; } int j = index; for (int perturb = perturb(hash); states[index] != FREE; perturb >>= PERTURB_SHIFT) { j = probe(perturb, j); index = j & mask; if (containsKey(key, index)) { return true; } } return false; } /** * Get an iterator over map elements. *

                                    The specialized iterators returned are fail-fast: they throw a * ConcurrentModificationException when they detect the map * has been modified during iteration.

                                    * @return iterator over the map elements */ public Iterator iterator() { return new Iterator(); } /** * Perturb the hash for starting probing. * @param hash initial hash * @return perturbed hash */ private static int perturb(final int hash) { return hash & 0x7fffffff; } /** * Find the index at which a key should be inserted * @param key key to lookup * @return index at which key should be inserted */ private int findInsertionIndex(final int key) { return findInsertionIndex(keys, states, key, mask); } /** * Find the index at which a key should be inserted * @param keys keys table * @param states states table * @param key key to lookup * @param mask bit mask for hash values * @return index at which key should be inserted */ private static int findInsertionIndex(final int[] keys, final byte[] states, final int key, final int mask) { final int hash = hashOf(key); int index = hash & mask; if (states[index] == FREE) { return index; } else if (states[index] == FULL && keys[index] == key) { return changeIndexSign(index); } int perturb = perturb(hash); int j = index; if (states[index] == FULL) { while (true) { j = probe(perturb, j); index = j & mask; perturb >>= PERTURB_SHIFT; if (states[index] != FULL || keys[index] == key) { break; } } } if (states[index] == FREE) { return index; } else if (states[index] == FULL) { // due to the loop exit condition, // if (states[index] == FULL) then keys[index] == key return changeIndexSign(index); } final int firstRemoved = index; while (true) { j = probe(perturb, j); index = j & mask; if (states[index] == FREE) { return firstRemoved; } else if (states[index] == FULL && keys[index] == key) { return changeIndexSign(index); } perturb >>= PERTURB_SHIFT; } } /** * Compute next probe for collision resolution * @param perturb perturbed hash * @param j previous probe * @return next probe */ private static int probe(final int perturb, final int j) { return (j << 2) + j + perturb + 1; } /** * Change the index sign * @param index initial index * @return changed index */ private static int changeIndexSign(final int index) { return -index - 1; } /** * Get the number of elements stored in the map. * @return number of elements stored in the map */ public int size() { return size; } /** * Remove the value associated with a key. * @param key key to which the value is associated * @return removed value */ public T remove(final int key) { final int hash = hashOf(key); int index = hash & mask; if (containsKey(key, index)) { return doRemove(index); } if (states[index] == FREE) { return missingEntries; } int j = index; for (int perturb = perturb(hash); states[index] != FREE; perturb >>= PERTURB_SHIFT) { j = probe(perturb, j); index = j & mask; if (containsKey(key, index)) { return doRemove(index); } } return missingEntries; } /** * Check if the tables contain an element associated with specified key * at specified index. * @param key key to check * @param index index to check * @return true if an element is associated with key at index */ private boolean containsKey(final int key, final int index) { return (key != 0 || states[index] == FULL) && keys[index] == key; } /** * Remove an element at specified index. * @param index index of the element to remove * @return removed value */ private T doRemove(int index) { keys[index] = 0; states[index] = REMOVED; final T previous = values[index]; values[index] = missingEntries; --size; ++count; return previous; } /** * Put a value associated with a key in the map. * @param key key to which value is associated * @param value value to put in the map * @return previous value associated with the key */ public T put(final int key, final T value) { int index = findInsertionIndex(key); T previous = missingEntries; boolean newMapping = true; if (index < 0) { index = changeIndexSign(index); previous = values[index]; newMapping = false; } keys[index] = key; states[index] = FULL; values[index] = value; if (newMapping) { ++size; if (shouldGrowTable()) { growTable(); } ++count; } return previous; } /** * Grow the tables. */ private void growTable() { final int oldLength = states.length; final int[] oldKeys = keys; final T[] oldValues = values; final byte[] oldStates = states; final int newLength = RESIZE_MULTIPLIER * oldLength; final int[] newKeys = new int[newLength]; final T[] newValues = buildArray(newLength); final byte[] newStates = new byte[newLength]; final int newMask = newLength - 1; for (int i = 0; i < oldLength; ++i) { if (oldStates[i] == FULL) { final int key = oldKeys[i]; final int index = findInsertionIndex(newKeys, newStates, key, newMask); newKeys[index] = key; newValues[index] = oldValues[i]; newStates[index] = FULL; } } mask = newMask; keys = newKeys; values = newValues; states = newStates; } /** * Check if tables should grow due to increased size. * @return true if tables should grow */ private boolean shouldGrowTable() { return size > (mask + 1) * LOAD_FACTOR; } /** * Compute the hash value of a key * @param key key to hash * @return hash value of the key */ private static int hashOf(final int key) { final int h = key ^ ((key >>> 20) ^ (key >>> 12)); return h ^ (h >>> 7) ^ (h >>> 4); } /** Iterator class for the map. */ public class Iterator { /** Reference modification count. */ private final int referenceCount; /** Index of current element. */ private int current; /** Index of next element. */ private int next; /** * Simple constructor. */ private Iterator() { // preserve the modification count of the map to detect concurrent modifications later referenceCount = count; // initialize current index next = -1; try { advance(); } catch (NoSuchElementException nsee) { // NOPMD // ignored } } /** * Check if there is a next element in the map. * @return true if there is a next element */ public boolean hasNext() { return next >= 0; } /** * Get the key of current entry. * @return key of current entry * @exception ConcurrentModificationException if the map is modified during iteration * @exception NoSuchElementException if there is no element left in the map */ public int key() throws ConcurrentModificationException, NoSuchElementException { if (referenceCount != count) { throw new ConcurrentModificationException(); } if (current < 0) { throw new NoSuchElementException(); } return keys[current]; } /** * Get the value of current entry. * @return value of current entry * @exception ConcurrentModificationException if the map is modified during iteration * @exception NoSuchElementException if there is no element left in the map */ public T value() throws ConcurrentModificationException, NoSuchElementException { if (referenceCount != count) { throw new ConcurrentModificationException(); } if (current < 0) { throw new NoSuchElementException(); } return values[current]; } /** * Advance iterator one step further. * @exception ConcurrentModificationException if the map is modified during iteration * @exception NoSuchElementException if there is no element left in the map */ public void advance() throws ConcurrentModificationException, NoSuchElementException { if (referenceCount != count) { throw new ConcurrentModificationException(); } // advance on step current = next; // prepare next step try { while (states[++next] != FULL) { // NOPMD // nothing to do } } catch (ArrayIndexOutOfBoundsException e) { next = -2; if (current < 0) { throw new NoSuchElementException(); } } } } /** * Read a serialized object. * @param stream input stream * @throws IOException if object cannot be read * @throws ClassNotFoundException if the class corresponding * to the serialized object cannot be found */ private void readObject(final ObjectInputStream stream) throws IOException, ClassNotFoundException { stream.defaultReadObject(); count = 0; } /** Build an array of elements. * @param length size of the array to build * @return a new array */ @SuppressWarnings("unchecked") // field is of type T private T[] buildArray(final int length) { return (T[]) Array.newInstance(field.getRuntimeClass(), length); } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/util/Decimal64.java100644 1750 1750 43772 12126627720 26106 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.util; import org.apache.commons.math3.RealFieldElement; import org.apache.commons.math3.Field; import org.apache.commons.math3.exception.DimensionMismatchException; /** * This class wraps a {@code double} value in an object. It is similar to the * standard class {@link Double}, while also implementing the * {@link RealFieldElement} interface. * * @since 3.1 * @version $Id: Decimal64.java 1462423 2013-03-29 07:25:18Z luc $ */ public class Decimal64 extends Number implements RealFieldElement, Comparable { /** The constant value of {@code 0d} as a {@code Decimal64}. */ public static final Decimal64 ZERO; /** The constant value of {@code 1d} as a {@code Decimal64}. */ public static final Decimal64 ONE; /** * The constant value of {@link Double#NEGATIVE_INFINITY} as a * {@code Decimal64}. */ public static final Decimal64 NEGATIVE_INFINITY; /** * The constant value of {@link Double#POSITIVE_INFINITY} as a * {@code Decimal64}. */ public static final Decimal64 POSITIVE_INFINITY; /** The constant value of {@link Double#NaN} as a {@code Decimal64}. */ public static final Decimal64 NAN; /** */ private static final long serialVersionUID = 20120227L; static { ZERO = new Decimal64(0d); ONE = new Decimal64(1d); NEGATIVE_INFINITY = new Decimal64(Double.NEGATIVE_INFINITY); POSITIVE_INFINITY = new Decimal64(Double.POSITIVE_INFINITY); NAN = new Decimal64(Double.NaN); } /** The primitive {@code double} value of this object. */ private final double value; /** * Creates a new instance of this class. * * @param x the primitive {@code double} value of the object to be created */ public Decimal64(final double x) { this.value = x; } /* * Methods from the FieldElement interface. */ /** {@inheritDoc} */ public Field getField() { return Decimal64Field.getInstance(); } /** * {@inheritDoc} * * The current implementation strictly enforces * {@code this.add(a).equals(new Decimal64(this.doubleValue() * + a.doubleValue()))}. */ public Decimal64 add(final Decimal64 a) { return new Decimal64(this.value + a.value); } /** * {@inheritDoc} * * The current implementation strictly enforces * {@code this.subtract(a).equals(new Decimal64(this.doubleValue() * - a.doubleValue()))}. */ public Decimal64 subtract(final Decimal64 a) { return new Decimal64(this.value - a.value); } /** * {@inheritDoc} * * The current implementation strictly enforces * {@code this.negate().equals(new Decimal64(-this.doubleValue()))}. */ public Decimal64 negate() { return new Decimal64(-this.value); } /** * {@inheritDoc} * * The current implementation strictly enforces * {@code this.multiply(a).equals(new Decimal64(this.doubleValue() * * a.doubleValue()))}. */ public Decimal64 multiply(final Decimal64 a) { return new Decimal64(this.value * a.value); } /** * {@inheritDoc} * * The current implementation strictly enforces * {@code this.multiply(n).equals(new Decimal64(n * this.doubleValue()))}. */ public Decimal64 multiply(final int n) { return new Decimal64(n * this.value); } /** * {@inheritDoc} * * The current implementation strictly enforces * {@code this.divide(a).equals(new Decimal64(this.doubleValue() * / a.doubleValue()))}. * */ public Decimal64 divide(final Decimal64 a) { return new Decimal64(this.value / a.value); } /** * {@inheritDoc} * * The current implementation strictly enforces * {@code this.reciprocal().equals(new Decimal64(1.0 * / this.doubleValue()))}. */ public Decimal64 reciprocal() { return new Decimal64(1.0 / this.value); } /* * Methods from the Number abstract class */ /** * {@inheritDoc} * * The current implementation performs casting to a {@code byte}. */ @Override public byte byteValue() { return (byte) value; } /** * {@inheritDoc} * * The current implementation performs casting to a {@code short}. */ @Override public short shortValue() { return (short) value; } /** * {@inheritDoc} * * The current implementation performs casting to a {@code int}. */ @Override public int intValue() { return (int) value; } /** * {@inheritDoc} * * The current implementation performs casting to a {@code long}. */ @Override public long longValue() { return (long) value; } /** * {@inheritDoc} * * The current implementation performs casting to a {@code float}. */ @Override public float floatValue() { return (float) value; } /** {@inheritDoc} */ @Override public double doubleValue() { return value; } /* * Methods from the Comparable interface. */ /** * {@inheritDoc} * * The current implementation returns the same value as *
                                    {@code new Double(this.doubleValue()).compareTo(new * Double(o.doubleValue()))}
                                    * * @see Double#compareTo(Double) */ public int compareTo(final Decimal64 o) { return Double.compare(this.value, o.value); } /* * Methods from the Object abstract class. */ /** {@inheritDoc} */ @Override public boolean equals(final Object obj) { if (obj instanceof Decimal64) { final Decimal64 that = (Decimal64) obj; return Double.doubleToLongBits(this.value) == Double .doubleToLongBits(that.value); } return false; } /** * {@inheritDoc} * * The current implementation returns the same value as * {@code new Double(this.doubleValue()).hashCode()} * * @see Double#hashCode() */ @Override public int hashCode() { long v = Double.doubleToLongBits(value); return (int) (v ^ (v >>> 32)); } /** * {@inheritDoc} * * The returned {@code String} is equal to * {@code Double.toString(this.doubleValue())} * * @see Double#toString(double) */ @Override public String toString() { return Double.toString(value); } /* * Methods inspired by the Double class. */ /** * Returns {@code true} if {@code this} double precision number is infinite * ({@link Double#POSITIVE_INFINITY} or {@link Double#NEGATIVE_INFINITY}). * * @return {@code true} if {@code this} number is infinite */ public boolean isInfinite() { return Double.isInfinite(value); } /** * Returns {@code true} if {@code this} double precision number is * Not-a-Number ({@code NaN}), false otherwise. * * @return {@code true} if {@code this} is {@code NaN} */ public boolean isNaN() { return Double.isNaN(value); } /** {@inheritDoc} * @since 3.2 */ public double getReal() { return value; } /** {@inheritDoc} * @since 3.2 */ public Decimal64 add(final double a) { return new Decimal64(value + a); } /** {@inheritDoc} * @since 3.2 */ public Decimal64 subtract(final double a) { return new Decimal64(value - a); } /** {@inheritDoc} * @since 3.2 */ public Decimal64 multiply(final double a) { return new Decimal64(value * a); } /** {@inheritDoc} * @since 3.2 */ public Decimal64 divide(final double a) { return new Decimal64(value / a); } /** {@inheritDoc} * @since 3.2 */ public Decimal64 remainder(final double a) { return new Decimal64(FastMath.IEEEremainder(value, a)); } /** {@inheritDoc} * @since 3.2 */ public Decimal64 remainder(final Decimal64 a) { return new Decimal64(FastMath.IEEEremainder(value, a.value)); } /** {@inheritDoc} * @since 3.2 */ public Decimal64 abs() { return new Decimal64(FastMath.abs(value)); } /** {@inheritDoc} * @since 3.2 */ public Decimal64 ceil() { return new Decimal64(FastMath.ceil(value)); } /** {@inheritDoc} * @since 3.2 */ public Decimal64 floor() { return new Decimal64(FastMath.floor(value)); } /** {@inheritDoc} * @since 3.2 */ public Decimal64 rint() { return new Decimal64(FastMath.rint(value)); } /** {@inheritDoc} * @since 3.2 */ public long round() { return FastMath.round(value); } /** {@inheritDoc} * @since 3.2 */ public Decimal64 signum() { return new Decimal64(FastMath.signum(value)); } /** {@inheritDoc} * @since 3.2 */ public Decimal64 copySign(final Decimal64 sign) { return new Decimal64(FastMath.copySign(value, sign.value)); } /** {@inheritDoc} * @since 3.2 */ public Decimal64 copySign(final double sign) { return new Decimal64(FastMath.copySign(value, sign)); } /** {@inheritDoc} * @since 3.2 */ public Decimal64 scalb(final int n) { return new Decimal64(FastMath.scalb(value, n)); } /** {@inheritDoc} * @since 3.2 */ public Decimal64 hypot(final Decimal64 y) { return new Decimal64(FastMath.hypot(value, y.value)); } /** {@inheritDoc} * @since 3.2 */ public Decimal64 sqrt() { return new Decimal64(FastMath.sqrt(value)); } /** {@inheritDoc} * @since 3.2 */ public Decimal64 cbrt() { return new Decimal64(FastMath.cbrt(value)); } /** {@inheritDoc} * @since 3.2 */ public Decimal64 rootN(final int n) { if (value < 0) { return new Decimal64(-FastMath.pow(-value, 1.0 / n)); } else { return new Decimal64(FastMath.pow(value, 1.0 / n)); } } /** {@inheritDoc} * @since 3.2 */ public Decimal64 pow(final double p) { return new Decimal64(FastMath.pow(value, p)); } /** {@inheritDoc} * @since 3.2 */ public Decimal64 pow(final int n) { return new Decimal64(FastMath.pow(value, n)); } /** {@inheritDoc} * @since 3.2 */ public Decimal64 pow(final Decimal64 e) { return new Decimal64(FastMath.pow(value, e.value)); } /** {@inheritDoc} * @since 3.2 */ public Decimal64 exp() { return new Decimal64(FastMath.exp(value)); } /** {@inheritDoc} * @since 3.2 */ public Decimal64 expm1() { return new Decimal64(FastMath.expm1(value)); } /** {@inheritDoc} * @since 3.2 */ public Decimal64 log() { return new Decimal64(FastMath.log(value)); } /** {@inheritDoc} * @since 3.2 */ public Decimal64 log1p() { return new Decimal64(FastMath.log1p(value)); } /** Base 10 logarithm. * @return base 10 logarithm of the instance * @since 3.2 */ public Decimal64 log10() { return new Decimal64(FastMath.log10(value)); } /** {@inheritDoc} * @since 3.2 */ public Decimal64 cos() { return new Decimal64(FastMath.cos(value)); } /** {@inheritDoc} * @since 3.2 */ public Decimal64 sin() { return new Decimal64(FastMath.sin(value)); } /** {@inheritDoc} * @since 3.2 */ public Decimal64 tan() { return new Decimal64(FastMath.tan(value)); } /** {@inheritDoc} * @since 3.2 */ public Decimal64 acos() { return new Decimal64(FastMath.acos(value)); } /** {@inheritDoc} * @since 3.2 */ public Decimal64 asin() { return new Decimal64(FastMath.asin(value)); } /** {@inheritDoc} * @since 3.2 */ public Decimal64 atan() { return new Decimal64(FastMath.atan(value)); } /** {@inheritDoc} * @since 3.2 */ public Decimal64 atan2(final Decimal64 x) { return new Decimal64(FastMath.atan2(value, x.value)); } /** {@inheritDoc} * @since 3.2 */ public Decimal64 cosh() { return new Decimal64(FastMath.cosh(value)); } /** {@inheritDoc} * @since 3.2 */ public Decimal64 sinh() { return new Decimal64(FastMath.sinh(value)); } /** {@inheritDoc} * @since 3.2 */ public Decimal64 tanh() { return new Decimal64(FastMath.tanh(value)); } /** {@inheritDoc} * @since 3.2 */ public Decimal64 acosh() { return new Decimal64(FastMath.acosh(value)); } /** {@inheritDoc} * @since 3.2 */ public Decimal64 asinh() { return new Decimal64(FastMath.asinh(value)); } /** {@inheritDoc} * @since 3.2 */ public Decimal64 atanh() { return new Decimal64(FastMath.atanh(value)); } /** {@inheritDoc} * @since 3.2 */ public Decimal64 linearCombination(final Decimal64[] a, final Decimal64[] b) throws DimensionMismatchException { if (a.length != b.length) { throw new DimensionMismatchException(a.length, b.length); } final double[] aDouble = new double[a.length]; final double[] bDouble = new double[b.length]; for (int i = 0; i < a.length; ++i) { aDouble[i] = a[i].value; bDouble[i] = b[i].value; } return new Decimal64(MathArrays.linearCombination(aDouble, bDouble)); } /** {@inheritDoc} * @since 3.2 */ public Decimal64 linearCombination(final double[] a, final Decimal64[] b) throws DimensionMismatchException { if (a.length != b.length) { throw new DimensionMismatchException(a.length, b.length); } final double[] bDouble = new double[b.length]; for (int i = 0; i < a.length; ++i) { bDouble[i] = b[i].value; } return new Decimal64(MathArrays.linearCombination(a, bDouble)); } /** {@inheritDoc} * @since 3.2 */ public Decimal64 linearCombination(final Decimal64 a1, final Decimal64 b1, final Decimal64 a2, final Decimal64 b2) { return new Decimal64(MathArrays.linearCombination(a1.value, b1.value, a2.value, b2.value)); } /** {@inheritDoc} * @since 3.2 */ public Decimal64 linearCombination(final double a1, final Decimal64 b1, final double a2, final Decimal64 b2) { return new Decimal64(MathArrays.linearCombination(a1, b1.value, a2, b2.value)); } /** {@inheritDoc} * @since 3.2 */ public Decimal64 linearCombination(final Decimal64 a1, final Decimal64 b1, final Decimal64 a2, final Decimal64 b2, final Decimal64 a3, final Decimal64 b3) { return new Decimal64(MathArrays.linearCombination(a1.value, b1.value, a2.value, b2.value, a3.value, b3.value)); } /** {@inheritDoc} * @since 3.2 */ public Decimal64 linearCombination(final double a1, final Decimal64 b1, final double a2, final Decimal64 b2, final double a3, final Decimal64 b3) { return new Decimal64(MathArrays.linearCombination(a1, b1.value, a2, b2.value, a3, b3.value)); } /** {@inheritDoc} * @since 3.2 */ public Decimal64 linearCombination(final Decimal64 a1, final Decimal64 b1, final Decimal64 a2, final Decimal64 b2, final Decimal64 a3, final Decimal64 b3, final Decimal64 a4, final Decimal64 b4) { return new Decimal64(MathArrays.linearCombination(a1.value, b1.value, a2.value, b2.value, a3.value, b3.value, a4.value, b4.value)); } /** {@inheritDoc} * @since 3.2 */ public Decimal64 linearCombination(final double a1, final Decimal64 b1, final double a2, final Decimal64 b2, final double a3, final Decimal64 b3, final double a4, final Decimal64 b4) { return new Decimal64(MathArrays.linearCombination(a1, b1.value, a2, b2.value, a3, b3.value, a4, b4.value)); } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/util/BigRealField.java100644 1750 1750 4732 12126627720 26620 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.util; import java.io.Serializable; import org.apache.commons.math3.Field; import org.apache.commons.math3.FieldElement; /** * Representation of real numbers with arbitrary precision field. *

                                    * This class is a singleton. *

                                    * @see BigReal * @version $Id: BigRealField.java 1416643 2012-12-03 19:37:14Z tn $ * @since 2.0 */ public class BigRealField implements Field, Serializable { /** Serializable version identifier */ private static final long serialVersionUID = 4756431066541037559L; /** Private constructor for the singleton. */ private BigRealField() { } /** Get the unique instance. * @return the unique instance */ public static BigRealField getInstance() { return LazyHolder.INSTANCE; } /** {@inheritDoc} */ public BigReal getOne() { return BigReal.ONE; } /** {@inheritDoc} */ public BigReal getZero() { return BigReal.ZERO; } /** {@inheritDoc} */ public Class> getRuntimeClass() { return BigReal.class; } // CHECKSTYLE: stop HideUtilityClassConstructor /** Holder for the instance. *

                                    We use here the Initialization On Demand Holder Idiom.

                                    */ private static class LazyHolder { /** Cached field instance. */ private static final BigRealField INSTANCE = new BigRealField(); } // CHECKSTYLE: resume HideUtilityClassConstructor /** Handle deserialization of the singleton. * @return the singleton instance */ private Object readResolve() { // return the singleton instance return LazyHolder.INSTANCE; } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/util/DoubleArray.java100644 1750 1750 7772 12126627720 26567 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.util; /** * Provides a standard interface for double arrays. Allows different * array implementations to support various storage mechanisms * such as automatic expansion, contraction, and array "rolling". * * @version $Id: DoubleArray.java 1416643 2012-12-03 19:37:14Z tn $ */ public interface DoubleArray { /** * Returns the number of elements currently in the array. Please note * that this may be different from the length of the internal storage array. * * @return number of elements */ int getNumElements(); /** * Returns the element at the specified index. Note that if an * out of bounds index is supplied a ArrayIndexOutOfBoundsException * will be thrown. * * @param index index to fetch a value from * @return value stored at the specified index * @throws ArrayIndexOutOfBoundsException if index is less than * zero or is greater than getNumElements() - 1. */ double getElement(int index); /** * Sets the element at the specified index. If the specified index is greater than * getNumElements() - 1, the numElements property * is increased to index +1 and additional storage is allocated * (if necessary) for the new element and all (uninitialized) elements * between the new element and the previous end of the array). * * @param index index to store a value in * @param value value to store at the specified index * @throws ArrayIndexOutOfBoundsException if index is less than * zero. */ void setElement(int index, double value); /** * Adds an element to the end of this expandable array * * @param value to be added to end of array */ void addElement(double value); /** * Adds elements to the end of this expandable array * * @param values to be added to end of array */ void addElements(double[] values); /** *

                                    * Adds an element to the end of the array and removes the first * element in the array. Returns the discarded first element. * The effect is similar to a push operation in a FIFO queue. *

                                    *

                                    * Example: If the array contains the elements 1, 2, 3, 4 (in that order) * and addElementRolling(5) is invoked, the result is an array containing * the entries 2, 3, 4, 5 and the value returned is 1. *

                                    * * @param value the value to be added to the array * @return the value which has been discarded or "pushed" out of the array * by this rolling insert */ double addElementRolling(double value); /** * Returns a double[] array containing the elements of this * DoubleArray. If the underlying implementation is * array-based, this method should always return a copy, rather than a * reference to the underlying array so that changes made to the returned * array have no effect on the DoubleArray. * * @return all elements added to the array */ double[] getElements(); /** * Clear the double array */ void clear(); } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/util/FastMathLiteralArrays.java100644 1750 1750 677277 12126627720 30641 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.util; /** * Utility class for loading tabulated data used by {@link FastMath}. * * @version $Id: FastMathLiteralArrays.java 1364389 2012-07-22 18:19:26Z tn $ */ class FastMathLiteralArrays { /** Exponential evaluated at integer values, * exp(x) = expIntTableA[x + EXP_INT_TABLE_MAX_INDEX] + expIntTableB[x+EXP_INT_TABLE_MAX_INDEX]. */ private static final double[] EXP_INT_A = new double[] { +0.0d, Double.NaN, Double.NaN, Double.NaN, Double.NaN, Double.NaN, Double.NaN, Double.NaN, Double.NaN, Double.NaN, Double.NaN, Double.NaN, Double.NaN, Double.NaN, Double.NaN, Double.NaN, Double.NaN, Double.NaN, Double.NaN, Double.NaN, Double.NaN, Double.NaN, Double.NaN, Double.NaN, Double.NaN, Double.NaN, Double.NaN, Double.NaN, Double.NaN, Double.NaN, Double.NaN, Double.NaN, Double.NaN, Double.NaN, Double.NaN, Double.NaN, Double.NaN, Double.NaN, Double.NaN, Double.NaN, Double.NaN, +1.2167807682331913E-308d, +3.3075532478807267E-308d, +8.990862214387203E-308d, +2.4439696075216986E-307d, +6.64339758024534E-307d, +1.8058628951432254E-306d, +4.908843759498681E-306d, +1.334362017065677E-305d, +3.627172425759641E-305d, +9.85967600992008E-305d, +2.680137967689915E-304d, +7.285370725133842E-304d, +1.9803689272433392E-303d, +5.3832011494782624E-303d, +1.463305638201413E-302d, +3.9776772027043775E-302d, +1.0812448255518705E-301d, +2.9391280956327795E-301d, +7.989378677301346E-301d, +2.1717383041010577E-300d, +5.903396499766243E-300d, +1.604709595901607E-299d, +4.3620527352131126E-299d, +1.1857289715706991E-298d, +3.2231452986239366E-298d, +8.761416875971053E-298d, +2.381600167287677E-297d, +6.473860152384321E-297d, +1.7597776278732318E-296d, +4.7835721669653157E-296d, +1.3003096668152053E-295d, +3.5346080979652066E-295d, +9.608060944124859E-295d, +2.6117415961302846E-294d, +7.099449830809996E-294d, +1.9298305829106006E-293d, +5.245823134132673E-293d, +1.4259627797225802E-292d, +3.8761686729764145E-292d, +1.0536518897078156E-291d, +2.864122672853628E-291d, +7.785491934690374E-291d, +2.116316283183901E-290d, +5.7527436249968E-290d, +1.5637579898345352E-289d, +4.250734424415339E-289d, +1.1554696041977512E-288d, +3.1408919441362495E-288d, +8.537829238438662E-288d, +2.320822576772103E-287d, +6.308649765138419E-287d, +1.7148689119310826E-286d, +4.66149719271323E-286d, +1.267126226441217E-285d, +3.444406231880653E-285d, +9.362866914115166E-285d, +2.5450911557068313E-284d, +6.918275021321188E-284d, +1.880582039589629E-283d, +5.111952261540649E-283d, +1.3895726688907995E-282d, +3.7772500667438066E-282d, +1.026763015362553E-281d, +2.791031173360063E-281d, +7.586808748646825E-281d, +2.0623086887184633E-280d, +5.605936171588964E-280d, +1.5238514098804918E-279d, +4.1422578754033235E-279d, +1.1259823210174452E-278d, +3.060737220976933E-278d, +8.319947089683576E-278d, +2.2615958035357106E-277d, +6.147655179898435E-277d, +1.6711060014400145E-276d, +4.542536646012133E-276d, +1.2347896500246374E-275d, +3.3565057475434694E-275d, +9.123929070778758E-275d, +2.4801413921885483E-274d, +6.741722283079056E-274d, +1.8325902719086093E-273d, +4.981496462621207E-273d, +1.3541112064618357E-272d, +3.68085620656127E-272d, +1.0005602916630382E-271d, +2.719805132368625E-271d, +7.393196131284108E-271d, +2.0096791226867E-270d, +5.462874707256208E-270d, +1.4849631831943512E-269d, +4.036548930895323E-269d, +1.0972476870931676E-268d, +2.9826282194717127E-268d, +8.107624153838987E-268d, +2.2038806519542315E-267d, +5.990769236615968E-267d, +1.628459873440512E-266d, +4.4266130556431266E-266d, +1.203278237867575E-265d, +3.270849446965521E-265d, +8.891090288030614E-265d, +2.4168487931443637E-264d, +6.569676185250389E-264d, +1.7858231429575898E-263d, +4.85437090269903E-263d, +1.3195548295785448E-262d, +3.5869215528816054E-262d, +9.750264097807267E-262d, +2.650396454019762E-261d, +7.204525142098426E-261d, +1.958392846081373E-260d, +5.32346341339996E-260d, +1.4470673509275515E-259d, +3.9335373658569176E-259d, +1.0692462289051038E-258d, +2.9065128598079075E-258d, +7.900720862969045E-258d, +2.147638465376883E-257d, +5.8378869339035456E-257d, +1.5869022483809747E-256d, +4.3136475849391444E-256d, +1.1725710340687719E-255d, +3.1873780814410126E-255d, +8.66419234315257E-255d, +2.35517168886351E-254d, +6.402020300783889E-254d, +1.740249660600677E-253d, +4.7304887145310405E-253d, +1.2858802448614707E-252d, +3.495384792953975E-252d, +9.501439740542955E-252d, +2.582759362004277E-251d, +7.020668578160457E-251d, +1.908415302517694E-250d, +5.1876107490791666E-250d, +1.4101386971763257E-249d, +3.8331545111676784E-249d, +1.0419594359065132E-248d, +2.8323395451363237E-248d, +7.699097067385825E-248d, +2.0928317096428755E-247d, +5.688906371296133E-247d, +1.5464049837965422E-246d, +4.2035646586788297E-246d, +1.1426473877336358E-245d, +3.106037603716254E-245d, +8.443084996839363E-245d, +2.2950686306677644E-244d, +6.238642390386363E-244d, +1.695838923802857E-243d, +4.6097680405580995E-243d, +1.2530649392922358E-242d, +3.4061835424180075E-242d, +9.25896798127602E-242d, +2.5168480541429286E-241d, +6.841502859109196E-241d, +1.8597132378953187E-240d, +5.055224959032211E-240d, +1.374152583940637E-239d, +3.735333866258403E-239d, +1.0153690688015855E-238d, +2.7600590782738726E-238d, +7.502618487550056E-238d, +2.0394233446495043E-237d, +5.543727690168612E-237d, +1.5069412868172555E-236d, +4.0962906236847E-236d, +1.1134873918971586E-235d, +3.026772467749944E-235d, +8.227620163729258E-235d, +2.2364990583200056E-234d, +6.079434951446575E-234d, +1.6525617499662284E-233d, +4.4921289690525345E-233d, +1.2210872189854344E-232d, +3.3192593301633E-232d, +9.02268127425393E-232d, +2.4526190464373087E-231d, +6.666909874218774E-231d, +1.8122539547625083E-230d, +4.926216840507529E-230d, +1.3390847149416908E-229d, +3.6400093808551196E-229d, +9.894571625944288E-229d, +2.689623698321582E-228d, +7.31115423069187E-228d, +1.9873779569310022E-227d, +5.402252865260326E-227d, +1.4684846983789053E-226d, +3.991755413823315E-226d, +1.0850715739509136E-225d, +2.9495302004590423E-225d, +8.017654713159388E-225d, +2.179424521221378E-224d, +5.924290380648597E-224d, +1.6103890140790331E-223d, +4.377491272857675E-223d, +1.1899254154663847E-222d, +3.2345523990372546E-222d, +8.792425221770645E-222d, +2.3900289095512176E-221d, +6.496772856703278E-221d, +1.7660059778220905E-220d, +4.800501435803201E-220d, +1.3049116216750674E-219d, +3.5471180281159325E-219d, +9.642065709892252E-219d, +2.6209850274990846E-218d, +7.124574366530717E-218d, +1.9366601417010147E-217d, +5.264388476949737E-217d, +1.431009021985696E-216d, +3.889885799962507E-216d, +1.057380684430436E-215d, +2.8742587656021775E-215d, +7.813044552050569E-215d, +2.1238058974550874E-214d, +5.773102661099307E-214d, +1.5692921723471877E-213d, +4.2657777816050375E-213d, +1.1595585743839232E-212d, +3.1520070828798975E-212d, +8.568043768122183E-212d, +2.329035966595791E-211d, +6.33097561889469E-211d, +1.720937714565362E-210d, +4.677993239821998E-210d, +1.2716105485691878E-209d, +3.456595573934475E-209d, +9.396000024637834E-209d, +2.55409795397022E-208d, +6.942757623821567E-208d, +1.887237361505784E-207d, +5.13004286606108E-207d, +1.3944901709366118E-206d, +3.7906173667738715E-206d, +1.0303966192973381E-205d, +2.8009086220877197E-205d, +7.613657850210907E-205d, +2.0696069842597556E-204d, +5.6257755605305175E-204d, +1.5292444435954893E-203d, +4.156916476922876E-203d, +1.12996721591364E-202d, +3.071569248856111E-202d, +8.349390727162016E-202d, +2.2695999828608633E-201d, +6.1694117899971836E-201d, +1.677020107827128E-200d, +4.558612479525779E-200d, +1.2391595516612638E-199d, +3.3683846288580648E-199d, +9.156218120779494E-199d, +2.4889182184335247E-198d, +6.765580431441772E-198d, +1.839075686473352E-197d, +4.999126524757713E-197d, +1.3589033107846643E-196d, +3.6938826366068014E-196d, +1.0041012794280992E-195d, +2.7294301888986675E-195d, +7.419361045185406E-195d, +2.016791373353671E-194d, +5.482208065983983E-194d, +1.490218341008089E-193d, +4.050833763855709E-193d, +1.101130773265179E-192d, +2.993183789477209E-192d, +8.136316299122392E-192d, +2.2116799789922265E-191d, +6.011969568315371E-191d, +1.6342228966392253E-190d, +4.4422779589171113E-190d, +1.2075364784547675E-189d, +3.282424571107068E-189d, +8.92255448602772E-189d, +2.425402115319395E-188d, +6.592926904915355E-188d, +1.79214305133496E-187d, +4.871550528055661E-187d, +1.3242245776666673E-186d, +3.599615946028287E-186d, +9.78476998200719E-186d, +2.659776075359514E-185d, +7.230020851688713E-185d, +1.9653234116333892E-184d, +5.34230278107224E-184d, +1.4521887058451231E-183d, +3.947457923821984E-183d, +1.0730302255093144E-182d, +2.9167986204137332E-182d, +7.928680793406766E-182d, +2.1552386987482013E-181d, +5.858546779607288E-181d, +1.5925182066949723E-180d, +4.328913614497258E-180d, +1.1767205227552116E-179d, +3.198658219194836E-179d, +8.694853785564504E-179d, +2.363506255864984E-178d, +6.42467573615509E-178d, +1.746408207555959E-177d, +4.747229597770176E-177d, +1.2904307529671472E-176d, +3.507754341050756E-176d, +9.535066345267336E-176d, +2.591899541396432E-175d, +7.045512786902009E-175d, +1.9151693415969248E-174d, +5.205969622575851E-174d, +1.4151292367806538E-173d, +3.846720258072078E-173d, +1.045647032279984E-172d, +2.8423629805010285E-172d, +7.726344058192276E-172d, +2.1002377128928765E-171d, +5.709039546124285E-171d, +1.5518778128928824E-170d, +4.218440703602533E-170d, +1.1466910691560932E-169d, +3.1170298734336303E-169d, +8.472965161251656E-169d, +2.303190374523956E-168d, +6.260720440258473E-168d, +1.701840523821621E-167d, +4.62608152166211E-167d, +1.2574995962791943E-166d, +3.418237608335161E-166d, +9.29173407843235E-166d, +2.5257552661512635E-165d, +6.865714679174435E-165d, +1.866294830116931E-164d, +5.073114566291778E-164d, +1.3790154522394582E-163d, +3.7485528226129495E-163d, +1.0189624503698769E-162d, +2.7698267293941856E-162d, +7.529170882336924E-162d, +2.0466404088178596E-161d, +5.56334611651382E-161d, +1.512274346576166E-160d, +4.110787043867721E-160d, +1.1174279267498045E-159d, +3.0374839443564585E-159d, +8.25673801176584E-159d, +2.244414150254963E-158d, +6.1009492034592176E-158d, +1.6584100275603453E-157d, +4.50802633729044E-157d, +1.2254085656601853E-156d, +3.3310057014599044E-156d, +9.054612259832416E-156d, +2.4612985502035675E-155d, +6.690503835950083E-155d, +1.8186679660152888E-154d, +4.9436516047443576E-154d, +1.3438240331106108E-153d, +3.652892398145774E-153d, +9.92958982547828E-153d, +2.6991427376823027E-152d, +7.3370297995122135E-152d, +1.994411660450821E-151d, +5.421372463189529E-151d, +1.4736818914204564E-150d, +4.005882964287806E-150d, +1.088911919926534E-149d, +2.9599693109692324E-149d, +8.046030012041041E-149d, +2.18713790898745E-148d, +5.945256705384597E-148d, +1.6160884846515524E-147d, +4.392983574030969E-147d, +1.1941366764543551E-146d, +3.2460001983475855E-146d, +8.8235440586675E-146d, +2.3984878190403553E-145d, +6.519765758635405E-145d, +1.772256261139753E-144d, +4.817491674217065E-144d, +1.3095299991573769E-143d, +3.559671483107555E-143d, +9.676190774054103E-143d, +2.630261301303634E-142d, +7.149792225695347E-142d, +1.943514969662872E-141d, +5.283020542151163E-141d, +1.4360739330834996E-140d, +3.9036541111764032E-140d, +1.0611230602364477E-139d, +2.8844319473099593E-139d, +7.84069876400596E-139d, +2.1313228444765414E-138d, +5.793536445518422E-138d, +1.5748463788034308E-137d, +4.2808762411845363E-137d, +1.1636629220608724E-136d, +3.163163464591171E-136d, +8.598369704466743E-136d, +2.337279322276433E-135d, +6.353384093665193E-135d, +1.7270287031459572E-134d, +4.694550492773212E-134d, +1.2761111606368036E-133d, +3.4688299108856403E-133d, +9.429257929713919E-133d, +2.5631381141873417E-132d, +6.967331001069377E-132d, +1.8939170679975288E-131d, +5.148199748336684E-131d, +1.3994258162094293E-130d, +3.804034213613942E-130d, +1.0340436948077763E-129d, +2.8108219632627907E-129d, +7.640606938467665E-129d, +2.0769322678328357E-128d, +5.645687086879944E-128d, +1.5346568127351796E-127d, +4.171630237420918E-127d, +1.1339665711932977E-126d, +3.0824406750909563E-126d, +8.37894218404787E-126d, +2.2776327994966818E-125d, +6.191247522703296E-125d, +1.6829556040859853E-124d, +4.5747479502862494E-124d, +1.2435453481209945E-123d, +3.3803067202247166E-123d, +9.188625696750548E-123d, +2.4977273040076145E-122d, +6.789527378582775E-122d, +1.845584943222965E-121d, +5.016820182185716E-121d, +1.3637129731022491E-120d, +3.706956710275979E-120d, +1.0076552294433743E-119d, +2.739090595934893E-119d, +7.445620503219039E-119d, +2.023929422267303E-118d, +5.501611507503037E-118d, +1.4954928881576769E-117d, +4.0651709187617596E-117d, +1.1050280679513555E-116d, +3.003777734030334E-116d, +8.165114384910189E-116d, +2.219508285637377E-115d, +6.033249389304709E-115d, +1.6400070480930697E-114d, +4.458001565878111E-114d, +1.2118105325725891E-113d, +3.2940421731384895E-113d, +8.954135150208654E-113d, +2.433986351722258E-112d, +6.616260705434716E-112d, +1.7984863104885375E-111d, +4.888792154132158E-111d, +1.3289115531074511E-110d, +3.612356038181234E-110d, +9.819402293160495E-110d, +2.6691899766673256E-109d, +7.255611264437603E-109d, +1.9722796756250217E-108d, +5.361211684173837E-108d, +1.4573285967670963E-107d, +3.961429477016909E-107d, +1.0768281419102595E-106d, +2.9271223293841774E-106d, +7.956744351476403E-106d, +2.1628672925745152E-105d, +5.879282834821692E-105d, +1.5981547034872092E-104d, +4.344234755347641E-104d, +1.1808855501885005E-103d, +3.2099795870407646E-103d, +8.725629524586503E-103d, +2.3718718327094683E-102d, +6.44741641521183E-102d, +1.7525895549820557E-101d, +4.7640323331013947E-101d, +1.2949980563724296E-100d, +3.5201699899499525E-100d, +9.56881327374431E-100d, +2.6010732940533088E-99d, +7.070450309820548E-99d, +1.9219478787856753E-98d, +5.2243955659975294E-98d, +1.4201378353978042E-97d, +3.8603349913851996E-97d, +1.0493479260117497E-96d, +2.8524232604238555E-96d, +7.753690709912764E-96d, +2.1076716069929933E-95d, +5.72924572981599E-95d, +1.5573703263204683E-94d, +4.233371554108682E-94d, +1.1507496472539512E-93d, +3.1280620563875923E-93d, +8.5029538631631E-93d, +2.3113425190436427E-92d, +6.28287989314225E-92d, +1.7078641226055994E-91d, +4.6424556110307644E-91d, +1.261950308999819E-90d, +3.430336362898836E-90d, +9.324622137237299E-90d, +2.5346947846365435E-89d, +6.890014851450124E-89d, +1.8729003560057785E-88d, +5.091070300111434E-88d, +1.3838964592430477E-87d, +3.761820584522275E-87d, +1.0225689628581036E-86d, +2.7796303536272215E-86d, +7.555818934379333E-86d, +2.053884626293416E-85d, +5.583037134407759E-85d, +1.5176268538776042E-84d, +4.125337057189083E-84d, +1.121383042095528E-83d, +3.0482348236054953E-83d, +8.285962249116636E-83d, +2.2523580600947705E-82d, +6.122543452787843E-82d, +1.664279766968299E-81d, +4.523982262003404E-81d, +1.2297456769063303E-80d, +3.342795345742034E-80d, +9.086660081726823E-80d, +2.4700104681773258E-79d, +6.714184569587689E-79d, +1.8251046352720517E-78d, +4.961148056969105E-78d, +1.3485799924445315E-77d, +3.665820371396835E-77d, +9.964732578705785E-77d, +2.708695208461993E-76d, +7.362996533913695E-76d, +2.0014700145557332E-75d, +5.440559532453721E-75d, +1.4788974793889734E-74d, +4.020060558571273E-74d, +1.092765612182012E-73d, +2.970445258959489E-73d, +8.074507236705857E-73d, +2.1948784599535102E-72d, +5.966298125808066E-72d, +1.6218081151910012E-71d, +4.408531734441582E-71d, +1.198363039426718E-70d, +3.257488853378793E-70d, +8.854771398921902E-70d, +2.406976727302894E-69d, +6.542840888268955E-69d, +1.778528517418201E-68d, +4.834541417183388E-68d, +1.3141647465063647E-67d, +3.572270133517001E-67d, +9.710435805122717E-67d, +2.63957027915428E-66d, +7.175096392165733E-66d, +1.9503931430716318E-65d, +5.3017188565638215E-65d, +1.4411566290936352E-64d, +3.9174693825966044E-64d, +1.0648786018364265E-63d, +2.8946401383311E-63d, +7.868447965383903E-63d, +2.1388659707647114E-62d, +5.814040618670345E-62d, +1.5804200403673568E-61d, +4.296027044486766E-61d, +1.1677812418806031E-60d, +3.174358801839755E-60d, +8.62880163941313E-60d, +2.345551464945955E-59d, +6.3758692300917355E-59d, +1.733140900346534E-58d, +4.711165925070571E-58d, +1.2806275683797178E-57d, +3.481106736845E-57d, +9.462629520363307E-57d, +2.5722094667974783E-56d, +6.9919903587080315E-56d, +1.9006201022568844E-55d, +5.166420404109835E-55d, +1.4043786616805493E-54d, +3.8174968984748894E-54d, +1.03770335512154E-53d, +2.820769858672565E-53d, +7.667647949477605E-53d, +2.0842827711783212E-52d, +5.6656680900216754E-52d, +1.5400881501571645E-51d, +4.1863938339341257E-51d, +1.1379799629071911E-50d, +3.093350150840571E-50d, +8.408597060399334E-50d, +2.2856938448387544E-49d, +6.2131591878042886E-49d, +1.688911928929718E-48d, +4.5909386437919143E-48d, +1.2479464696643861E-47d, +3.3922703599272275E-47d, +9.221146830884422E-47d, +2.5065676066043174E-46d, +6.8135571305481364E-46d, +1.8521166948363666E-45d, +5.0345752964740226E-45d, +1.368539456379101E-44d, +3.720075801577098E-44d, +1.0112214979786464E-43d, +2.7487849807248755E-43d, +7.47197247068667E-43d, +2.0310928323153876E-42d, +5.521082422279256E-42d, +1.5007857288519654E-41d, +4.0795586181406803E-41d, +1.108938997126179E-40d, +3.0144088843073416E-40d, +8.194012195477669E-40d, +2.2273635587196807E-39d, +6.054601485195952E-39d, +1.6458113136245473E-38d, +4.473779311490168E-38d, +1.2160992719555806E-37d, +3.3057007442449645E-37d, +8.985825281444118E-37d, +2.442600707513088E-36d, +6.639677673630215E-36d, +1.8048513285848406E-35d, +4.906094420881007E-35d, +1.3336148713971936E-34d, +3.625141007634431E-34d, +9.854154449263851E-34d, +2.6786368134431636E-33d, +7.28128971953363E-33d, +1.9792597720953414E-32d, +5.380185921962174E-32d, +1.4624861244004054E-31d, +3.975449484028966E-31d, +1.080639291795678E-30d, +2.9374821418009058E-30d, +7.984904044796711E-30d, +2.1705221445447534E-29d, +5.900089995748943E-29d, +1.6038109389511792E-28d, +4.359610133382778E-28d, +1.185064946717304E-27d, +3.221340469489223E-27d, +8.756510122348782E-27d, +2.380266370880709E-26d, +6.47023467943241E-26d, +1.75879225876483E-25d, +4.780892502168074E-25d, +1.2995814853898995E-24d, +3.5326287852455166E-24d, +9.602680736954162E-24d, +2.6102792042257208E-23d, +7.095474414148981E-23d, +1.9287497671359936E-22d, +5.242885191553114E-22d, +1.4251641388208515E-21d, +3.873997809109103E-21d, +1.0530616658562386E-20d, +2.862518609581133E-20d, +7.78113163345177E-20d, +2.1151310700892382E-19d, +5.74952254077566E-19d, +1.5628822871880503E-18d, +4.24835413113866E-18d, +1.1548223864099742E-17d, +3.139132557537509E-17d, +8.533046968331264E-17d, +2.3195229636950566E-16d, +6.305116324200775E-16d, +1.71390848833098E-15d, +4.6588861918718874E-15d, +1.2664165777252073E-14d, +3.442477422913037E-14d, +9.357622912219837E-14d, +2.5436656904062604E-13d, +6.914399608426436E-13d, +1.879528650772233E-12d, +5.1090893668503945E-12d, +1.3887944613766301E-11d, +3.775134371775124E-11d, +1.0261880234452292E-10d, +2.789468100949932E-10d, +7.582560135332983E-10d, +2.061153470123145E-9d, +5.602796449011294E-9d, +1.5229979055675358E-8d, +4.139937459513021E-8d, +1.1253517584464134E-7d, +3.059023470086686E-7d, +8.315287232107949E-7d, +2.260329438286135E-6d, +6.1442124206223525E-6d, +1.670170240686275E-5d, +4.539993096841499E-5d, +1.2340981629677117E-4d, +3.35462624207139E-4d, +9.118819143623114E-4d, +0.0024787522852420807d, +0.006737947463989258d, +0.018315639346837997d, +0.049787066876888275d, +0.1353352963924408d, +0.3678794503211975d, +1.0d, +2.7182817459106445d, +7.389056205749512d, +20.08553695678711d, +54.59815216064453d, +148.41314697265625d, +403.42877197265625d, +1096.633056640625d, +2980.9580078125d, +8103.083984375d, +22026.46484375d, +59874.140625d, +162754.78125d, +442413.375d, +1202604.25d, +3269017.5d, +8886110.0d, +2.4154952E7d, +6.5659968E7d, +1.78482304E8d, +4.85165184E8d, +1.318815744E9d, +3.584912896E9d, +9.74480384E9d, +2.6489122816E10d, +7.200489472E10d, +1.95729620992E11d, +5.32048248832E11d, +1.446257098752E12d, +3.9313342464E12d, +1.0686474223616E13d, +2.904884772864E13d, +7.8962956959744E13d, +2.14643574308864E14d, +5.83461777702912E14d, +1.586013579247616E15d, +4.31123180027904E15d, +1.1719142537166848E16d, +3.1855931348221952E16d, +8.6593395455164416E16d, +2.35385270340419584E17d, +6.3984347447610573E17d, +1.73927483790327808E18d, +4.7278395262972723E18d, +1.285159987981792E19d, +3.493427277593156E19d, +9.496119530068797E19d, +2.581312717296228E20d, +7.016736290557636E20d, +1.907346499785443E21d, +5.1847060206155E21d, +1.4093490364499379E22d, +3.831007739580998E22d, +1.0413759887481643E23d, +2.8307533984544136E23d, +7.694785471490595E23d, +2.0916595931561093E24d, +5.685720022003016E24d, +1.545539007875769E25d, +4.201209991636407E25d, +1.142007304008196E26d, +3.104297782658242E26d, +8.43835682327257E26d, +2.2937832658080656E27d, +6.23514943204966E27d, +1.694889206675675E28d, +4.607187019879158E28d, +1.2523630909973607E29d, +3.4042761729010895E29d, +9.253781621373885E29d, +2.5154385492401904E30d, +6.837671137556327E30d, +1.8586717056324128E31d, +5.05239404378821E31d, +1.3733830589835937E32d, +3.733241849647479E32d, +1.014800418749161E33d, +2.758513549969986E33d, +7.498416981578345E33d, +2.0382811492597872E34d, +5.540622484676759E34d, +1.5060972626944096E35d, +4.0939972479624634E35d, +1.1128638067747114E36d, +3.0250770246136387E36d, +8.223012393018281E36d, +2.2352467822017166E37d, +6.076029840339376E37d, +1.6516361647240826E38d, +4.4896127778163155E38d, +1.2204032949639917E39d, +3.3174000012927697E39d, +9.017628107716908E39d, +2.451245443147225E40d, +6.663175904917432E40d, +1.8112388823726723E41d, +4.923458004084836E41d, +1.3383347029375378E42d, +3.637970747803715E42d, +9.889030935681123E42d, +2.6881169167589747E43d, +7.307059786371152E43d, +1.986264756071962E44d, +5.399227989109673E44d, +1.467662348860426E45d, +3.989519470441919E45d, +1.0844638420493122E46d, +2.9478781225754055E46d, +8.013164089994031E46d, +2.1782039447564253E47d, +5.920972420778763E47d, +1.609486943324346E48d, +4.3750396394525074E48d, +1.1892591576149107E49d, +3.2327411123173475E49d, +8.787501601904039E49d, +2.3886908001521312E50d, +6.493134033643613E50d, +1.7650169203544438E51d, +4.7978130078372714E51d, +1.3041809768060802E52d, +3.5451314095271004E52d, +9.636666808527841E52d, +2.6195174357581655E53d, +7.120586694432509E53d, +1.9355758655647052E54d, +5.2614409704305464E54d, +1.4302079642723736E55d, +3.8877083524279136E55d, +1.0567886837680406E56d, +2.872649515690124E56d, +7.808670894670738E56d, +2.1226166967029073E57d, +5.769871153180574E57d, +1.568413405104933E58d, +4.263390023436419E58d, +1.1589095247718807E59d, +3.150242850860434E59d, +8.563247933339596E59d, +2.3277319969498524E60d, +6.327431953939798E60d, +1.719974302355042E61d, +4.675374788964851E61d, +1.2708985520400816E62d, +3.454660807101683E62d, +9.390740355567705E62d, +2.5526681615684215E63d, +6.938871462941557E63d, +1.8861808782043154E64d, +5.1271712215233855E64d, +1.3937096689052236E65d, +3.7884955399150257E65d, +1.0298199046367501E66d, +2.799340708992666E66d, +7.609396391563323E66d, +2.0684484008569103E67d, +5.622626080395226E67d, +1.528388084444653E68d, +4.1545899609113734E68d, +1.1293346659459732E69d, +3.069849599753188E69d, +8.344717266683004E69d, +2.268329019570017E70d, +6.165958325782564E70d, +1.676081191364984E71d, +4.556060380835955E71d, +1.2384658100355657E72d, +3.3664990715562672E72d, +9.15109220707761E72d, +2.4875248571153216E73d, +6.761793219649385E73d, +1.8380461271305958E74d, +4.996327312938759E74d, +1.3581426848077408E75d, +3.691814001080034E75d, +1.0035391101975138E76d, +2.7279024753382288E76d, +7.415207287657125E76d, +2.0156621983963848E77d, +5.479138512760614E77d, +1.4893842728520671E78d, +4.048565732162643E78d, +1.1005142643914475E79d, +2.991508131437659E79d, +8.131762373533769E79d, +2.210442148596269E80d, +6.008604166110734E80d, +1.633308028614055E81d, +4.439791652732591E81d, +1.206860599814453E82d, +3.280586734644871E82d, +8.917559854082513E82d, +2.4240442814945802E83d, +6.589235682116406E83d, +1.7911398904871E84d, +4.86882298924053E84d, +1.3234832005748183E85d, +3.597600556519039E85d, +9.77929222446451E85d, +2.658286976862848E86d, +7.225974166887662E86d, +1.9642232209552433E87d, +5.3393125705958075E87d, +1.4513757076459615E88d, +3.945247871835613E88d, +1.0724295693252266E89d, +2.915165904253785E89d, +7.924242330665303E89d, +2.1540322390343345E90d, +5.855267177907345E90d, +1.5916266807316476E91d, +4.326489915443873E91d, +1.1760619079592718E92d, +3.1968677404735245E92d, +8.689987517871135E92d, +2.3621834216830225E93d, +6.421080550439423E93d, +1.7454306955949023E94d, +4.744571892885607E94d, +1.2897084285532175E95d, +3.505791114318544E95d, +9.529727908157224E95d, +2.5904487437231458E96d, +7.041568925985714E96d, +1.9140971884979424E97d, +5.203055142575272E97d, +1.4143368931719686E98d, +3.8445667684706366E98d, +1.0450615121235744E99d, +2.8407720200442806E99d, +7.722018663521402E99d, +2.0990624115923312E100d, +5.705842978547001E100d, +1.5510089388648915E101d, +4.216079296087462E101d, +1.1460491592124923E102d, +3.1152847602082673E102d, +8.468222063292654E102d, +2.3019011105282883E103d, +6.257216813084462E103d, +1.7008878437355237E104d, +4.62349260394851E104d, +1.2567956334920216E105d, +3.416324322370112E105d, +9.286532888251822E105d, +2.5243410574836706E106d, +6.861870970598542E106d, +1.8652499723625443E107d, +5.070274654122399E107d, +1.3782437251846782E108d, +3.746454626411946E108d, +1.0183920005400422E109d, +2.768276122845335E109d, +7.524954624697075E109d, +2.0454950851007314E110d, +5.56023190218245E110d, +1.511427628805191E111d, +4.1084862677372065E111d, +1.1168024085164686E112d, +3.0357834799588566E112d, +8.252116273466952E112d, +2.2431576057283144E113d, +6.097534318207731E113d, +1.65748157925005E114d, +4.5055022172222453E114d, +1.2247224482958058E115d, +3.329140840363789E115d, +9.049543313665034E115d, +2.4599209935197392E116d, +6.686758417135634E116d, +1.817649308779104E117d, +4.940883275207154E117d, +1.3430713954289087E118d, +3.6508464654683645E118d, +9.924030156169606E118d, +2.697631034485758E119d, +7.332921137166064E119d, +1.9932945470297703E120d, +5.418336099279846E120d, +1.472856595860236E121d, +4.0036393271908754E121d, +1.0883019300873278E122d, +2.9583112936666607E122d, +8.041523923017192E122d, +2.1859129781586158E123d, +5.941927186144745E123d, +1.6151834292371802E124d, +4.390523815859274E124d, +1.1934680816813702E125d, +3.2441826014060764E125d, +8.81860282490643E125d, +2.3971445233885962E126d, +6.516115189736396E126d, +1.7712635751001657E127d, +4.814793918384117E127d, +1.3087966177291396E128d, +3.557678449715009E128d, +9.670771210463886E128d, +2.628788218289742E129d, +7.145787619369324E129d, +1.9424264981694277E130d, +5.280062387569078E130d, +1.4352697002457768E131d, +3.901467289560222E131d, +1.0605288965077546E132d, +2.882816299252225E132d, +7.836307815186044E132d, +2.1301292155181736E133d, +5.790291758828013E133d, +1.573964437869041E134d, +4.278478878300888E134d, +1.1630112062985817E135d, +3.1613917467297413E135d, +8.593554223894477E135d, +2.335970335559215E136d, +6.349826172787151E136d, +1.7260616357651607E137d, +4.691921416188566E137d, +1.2753966504932798E138d, +3.466887271843006E138d, +9.423976538577447E138d, +2.561702766944378E139d, +6.963429563637273E139d, +1.892856346657855E140d, +5.1453167686439515E140d, +1.3986421289359558E141d, +3.8019036618832785E141d, +1.033464507572145E142d, +2.809247950589945E142d, +7.636326960498012E142d, +2.075769060297565E143d, +5.64252553828769E143d, +1.5337974510118784E144d, +4.169293918423203E144d, +1.1333315586787883E145d, +3.080714152600695E145d, +8.374250298636991E145d, +2.276357074042286E146d, +6.187780443461367E146d, +1.6820131331794073E147d, +4.572185635487065E147d, +1.2428488853188662E148d, +3.378413594504258E148d, +9.183480622172801E148d, +2.4963286658278886E149d, +6.785725312893433E149d, +1.8445514681108982E150d, +5.014010481958507E150d, +1.3629491735708616E151d, +3.7048805655699485E151d, +1.0070909418550386E152d, +2.7375567044077912E152d, +7.441451374243517E152d, +2.022795961737854E153d, +5.4985298195094216E153d, +1.494655405262451E154d, +4.062894701808608E154d, +1.1044092571980793E155d, +3.002095574584687E155d, +8.160542326793782E155d, +2.218265110516721E156d, +6.02987028472758E156d, +1.6390888071605646E157d, +4.455504920700703E157d, +1.2111317421229415E158d, +3.2921976772303727E158d, +8.94912101169977E158d, +2.432623425087251E159d, +6.612555731556604E159d, +1.7974788874847574E160d, +4.8860545948985793E160d, +1.328167263606087E161d, +3.610333312791256E161d, +9.813901863427107E161d, +2.667695552814763E162d, +7.251548346906463E162d, +1.9711751621240536E163d, +5.3582093498119173E163d, +1.4565123573071036E164d, +3.959211091077107E164d, +1.0762251933089556E165d, +2.9254832789181E165d, +7.952287052787358E165d, +2.161656025361765E166d, +5.8759898326913254E166d, +1.597259768214821E167d, +4.3418021646459346E167d, +1.1802241249113175E168d, +3.2081817253680657E168d, +8.720743087611513E168d, +2.3705435424427623E169d, +6.443805025317327E169d, +1.7516078165936552E170d, +4.7613641572445654E170d, +1.2942728582966776E171d, +3.518198614137319E171d, +9.563454814394247E171d, +2.5996166206245285E172d, +7.066491077377918E172d, +1.920871394985668E173d, +5.221469250951617E173d, +1.4193426880442385E174d, +3.8581732071331E174d, +1.0487601931965087E175d, +2.850825930161946E175d, +7.749348772180658E175d, +2.1064911705560668E176d, +5.726036941135634E176d, +1.5564982816556894E177d, +4.231000988846797E177d, +1.1501053030837989E178d, +3.1263099916916113E178d, +8.498192212235393E178d, +2.3100480183046895E179d, +6.279361500971995E179d, +1.7069074829463731E180d, +4.63985600437427E180d, +1.2612435745231905E181d, +3.4284156709489884E181d, +9.319400030019162E181d, +2.5332752658571312E182d, +6.88615578404537E182d, +1.8718514371423056E183d, +5.088219872370737E183d, +1.3831214731781958E184d, +3.759713966511158E184d, +1.021996184153141E185d, +2.778073442169904E185d, +7.55158797540476E185d, +2.0527342305586606E186d, +5.579910641313343E186d, +1.5167767828844167E187d, +4.123026721295484E187d, +1.1207549425651513E188d, +3.0465278560980536E188d, +8.281321669236493E188d, +2.251096660331649E189d, +6.119114404399683E189d, +1.6633478556884994E190d, +4.521448560089285E190d, +1.2290570545894685E191d, +3.340923580982338E191d, +9.081571104550255E191d, +2.468626868232408E192d, +6.710424255583952E192d, +1.8240823171621646E193d, +4.958369974640573E193d, +1.3478247120462365E194d, +3.6637673548790206E194d, +9.959152908532152E194d, +2.707178052117959E195d, +7.358873642076596E195d, +2.0003490682463053E196d, +5.4375131636754E196d, +1.4780692924846082E197d, +4.01780853635105E197d, +1.0921536132159379E198d, +2.968781250496917E198d, +8.069984512111955E198d, +2.193649279840519E199d, +5.962956589227457E199d, +1.620899738203635E200d, +4.406062052965071E200d, +1.1976919074588434E201d, +3.2556641859513496E201d, +8.849812639395597E201d, +2.40562867677584E202d, +6.539175932653188E202d, +1.7775323307944624E203d, +4.831833881898182E203d, +1.3134287685114547E204d, +3.5702693195009266E204d, +9.704997606668411E204d, +2.63809219778715E205d, +7.171077244202293E205d, +1.949300880034352E206d, +5.298749302736127E206d, +1.4403494631058154E207d, +3.91527572177694E207d, +1.0642823992403076E208d, +2.8930193727937684E208d, +7.8640411896421955E208d, +2.1376680994038112E209d, +5.8107841809216616E209d, +1.5795351101531684E210d, +4.293620869258453E210d, +1.1671272667059652E211d, +3.172580666390786E211d, +8.623968972387222E211d, +2.3442378838418366E212d, +6.372298757235201E212d, +1.7321703934464356E213d, +4.708527306855985E213d, +1.279910496643312E214d, +3.479157135998568E214d, +9.45732984079136E214d, +2.5707689593428096E215d, +6.988074107282322E215d, +1.8995553996578656E216d, +5.1635269305465607E216d, +1.4035923083915864E217d, +3.815359096108819E217d, +1.0371220592190472E218d, +2.819190456167585E218d, +7.663353127378024E218d, +2.083115484919861E219d, +5.662495731848751E219d, +1.5392257142577226E220d, +4.184049381430498E220d, +1.1373425785132867E221d, +3.091617462831603E221d, +8.403887374207366E221d, +2.2844135610697528E222d, +6.209679892802781E222d, +1.6879660933816274E223d, +4.588367423411997E223d, +1.2472476068464461E224d, +3.3903703993793316E224d, +9.215982463319503E224d, +2.5051637206758385E225d, +6.809741127603255E225d, +1.8510795864289367E226d, +5.031755776868959E226d, +1.3677729802316034E227d, +3.7179924024793253E227d, +1.0106552237522032E228d, +2.7472456017809066E228d, +7.467788172398272E228d, +2.029955237703202E229d, +5.517990469846618E229d, +1.4999452522236406E230d, +4.0772734783595525E230d, +1.1083180046837618E231d, +3.012720614547867E231d, +8.18942426109357E231d, +2.2261161215322043E232d, +6.051211457626543E232d, +1.6448897917725177E233d, +4.471273900208441E233d, +1.2154183152078517E234d, +3.3038494682728794E234d, +8.98079409878202E234d, +2.4412328161430576E235d, +6.63595840453991E235d, +1.8038406914061554E236d, +4.90334700062756E236d, +1.3328680266667662E237d, +3.623110695743118E237d, +9.848636053777669E237d, +2.677136737066629E238d, +7.277212447141125E238d, +1.978151484427976E239d, +5.377173488599035E239d, +1.4616672175682191E240d, +3.973222981713661E240d, +1.0800340064859439E241d, +2.935837009891444E241d, +7.980432566722885E241d, +2.169306470354036E242d, +5.896786161387733E242d, +1.6029126916635028E243d, +4.357168123448786E243d, +1.1844011798406507E244d, +3.2195361624179725E244d, +8.751606149833694E244d, +2.3789334438756013E245d, +6.466611224443739E245d, +1.7578073785142153E246d, +4.7782149589194885E246d, +1.2988535295611824E247d, +3.5306502960727705E247d, +9.597302512507479E247d, +2.608817438130718E248d, +7.091500562953208E248d, +1.9276698418065647E249d, +5.239949786641934E249d, +1.42436589329759E250d, +3.8718282216768776E250d, +1.0524719896550007E251d, +2.860915548426704E251d, +7.77677492833005E251d, +2.113946677051906E252d, +5.7463023795153145E252d, +1.56200679236425E253d, +4.2459748085663055E253d, +1.1541756557557508E254d, +3.137374584307575E254d, +8.528268445871411E254d, +2.3182239583484444E255d, +6.301585387776819E255d, +1.7129486892266285E256d, +4.6562769567905925E256d, +1.26570724146049E257d, +3.4405490416979487E257d, +9.352382323649647E257d, +2.54224113415832E258d, +6.910528108396216E258d, +1.8784760208391767E259d, +5.106228040084293E259d, +1.3880166914480165E260d, +3.7730204737910044E260d, +1.0256131352582533E261d, +2.787906051540986E261d, +7.578313650939932E261d, +2.0599991793068063E262d, +5.5996586041611455E262d, +1.522145133131402E263d, +4.137618951061827E263d, +1.1247213964487372E264d, +3.0573102223682595E264d, +8.310629417537063E264d, +2.2590636576955473E265d, +6.1407711078356886E265d, +1.6692346202615142E266d, +4.5374504961394207E266d, +1.2334070098307164E267d, +3.3527476928456816E267d, +9.113713162029408E267d, +2.4773638527240193E268d, +6.734172833429278E268d, +1.8305382378470305E269d, +4.9759187284770303E269d, +1.352594940263854E270d, +3.6767339705169146E270d, +9.994400500679653E270d, +2.716759624268743E271d, +7.384918458508588E271d, +2.007428933605617E272d, +5.456757565532369E272d, +1.4833003969415539E273d, +4.0320284712983994E273d, +1.096019026243815E274d, +2.979288529962515E274d, +8.098545495417704E274d, +2.201412886580694E275d, +5.984060832462728E275d, +1.6266362950862408E276d, +4.4216561713555547E276d, +1.2019307065458128E277d, +3.2671863888979078E277d, +8.881133159512924E277d, +2.4141423627760256E278d, +6.562319473965767E278d, +1.7838233889223407E279d, +4.848934634563382E279d, +1.3180771991576186E280d, +3.5829049382293792E280d, +9.739345931419228E280d, +2.6474285478041252E281d, +7.196457718729758E281d, +1.956199868121249E282d, +5.31750271790054E282d, +1.4454470027638629E283d, +3.929132560365955E283d, +1.0680488848057261E284d, +2.9032581477488686E284d, +7.89187408872514E284d, +2.1452336456259667E285d, +5.831349876080173E285d, +1.5851251724785243E286d, +4.308816643345461E286d, +1.1712579802975851E287d, +3.1838092090922606E287d, +8.654490685278886E287d, +2.3525345191912968E288d, +6.39485115791896E288d, +1.7383009254496851E289d, +4.725191397657393E289d, +1.2844402232816276E290d, +3.491470347090126E290d, +9.490800658395667E290d, +2.579867270991543E291d, +7.012806239173502E291d, +1.906278351789277E292d, +5.181801397059486E292d, +1.408559707497606E293d, +3.8288623079292884E293d, +1.0407926842436056E294d, +2.829168201470791E294d, +7.690475570840264E294d, +2.0904882610105383E295d, +5.68253547942899E295d, +1.544673396032028E296d, +4.1988574190754736E296d, +1.1413677466646359E297d, +3.102559332875688E297d, +8.433630296371073E297d, +2.292498520423419E298d, +6.23165710486722E298d, +1.6939399242810123E299d, +4.604606371472047E299d, +1.2516618713553432E300d, +3.402369329874797E300d, +9.248598815279678E300d, +2.51402968559859E301d, +6.833842035076675E301d, +1.8576309291617257E302d, +5.049564425991982E302d, +1.3726137091534984E303d, +3.7311513682845094E303d, +1.0142320772726397E304d, +2.7569686255975333E304d, +7.494218049456063E304d, +2.037139607241041E305d, +5.5375196488302575E305d, +1.5052539519895093E306d, +4.091704288360009E306d, +1.1122405335641184E307d, +3.023383151402969E307d, +8.218407798110846E307d, Double.NaN, Double.NaN, Double.NaN, Double.NaN, Double.NaN, Double.NaN, Double.NaN, Double.NaN, Double.NaN, Double.NaN, Double.NaN, Double.NaN, Double.NaN, Double.NaN, Double.NaN, Double.NaN, Double.NaN, Double.NaN, Double.NaN, Double.NaN, Double.NaN, Double.NaN, Double.NaN, Double.NaN, Double.NaN, Double.NaN, Double.NaN, Double.NaN, Double.NaN, Double.NaN, Double.NaN, Double.NaN, Double.NaN, Double.NaN, Double.NaN, Double.NaN, Double.NaN, Double.NaN, Double.NaN, Double.NaN, }; /** Exponential evaluated at integer values, * exp(x) = expIntTableA[x + EXP_INT_TABLE_MAX_INDEX] + expIntTableB[x+EXP_INT_TABLE_MAX_INDEX] */ private static final double[] EXP_INT_B = new double[] { +0.0d, Double.NaN, Double.NaN, Double.NaN, Double.NaN, Double.NaN, Double.NaN, Double.NaN, Double.NaN, Double.NaN, Double.NaN, Double.NaN, Double.NaN, Double.NaN, Double.NaN, Double.NaN, Double.NaN, Double.NaN, Double.NaN, Double.NaN, Double.NaN, Double.NaN, Double.NaN, Double.NaN, Double.NaN, Double.NaN, Double.NaN, Double.NaN, Double.NaN, Double.NaN, Double.NaN, Double.NaN, Double.NaN, Double.NaN, Double.NaN, Double.NaN, Double.NaN, Double.NaN, Double.NaN, Double.NaN, Double.NaN, -1.76097684E-316d, -2.44242319E-315d, -9.879317845E-315d, -1.3811462167E-314d, +2.1775261204E-314d, -1.4379095864E-313d, +1.4219324087E-313d, +1.00605438061E-312d, -1.287101187097E-312d, +5.33839690397E-312d, -9.35130825405E-313d, -4.15218681073E-311d, +4.546040329134E-311d, -1.57333572310673E-310d, +1.05387548454467E-309d, +2.095732474644446E-309d, -2.62524392470767E-310d, +5.86440876259637E-309d, -2.401816502004675E-309d, -2.2711230715729753E-308d, +2.0670460065057715E-307d, +3.436860020483706E-308d, +2.0862243734177337E-306d, -4.637025318037353E-306d, +9.222671009756424E-306d, +6.704597874020559E-305d, +4.351284159444109E-305d, +4.232889602759328E-304d, +1.2840977763293412E-303d, -2.6993478083348727E-303d, -1.053265874779237E-303d, +1.207746682843556E-303d, +5.21281096513035E-303d, +1.6515377082609677E-301d, +3.3951607353932444E-301d, +5.609418227003629E-301d, +4.238775357914848E-300d, -9.441842771290538E-300d, -2.1745347282493023E-299d, -6.203839803215248E-299d, -5.617718879466363E-299d, +5.2869976233132615E-298d, -1.4300075619643524E-298d, +4.3198234936686506E-297d, -2.6448316331572387E-297d, +4.315655444002347E-296d, -7.253671992213344E-296d, -1.1288398461391523E-295d, -4.83901764243093E-296d, +1.7407497662694827E-295d, +1.1969717029666017E-294d, -7.752519943329177E-294d, -4.019569741253664E-293d, -2.4467928392518484E-293d, -1.0269233640424235E-292d, -3.2330960700986594E-292d, -1.440995270758115E-291d, -3.726946038150935E-291d, -1.3424576100819801E-291d, -3.128894928199484E-290d, -5.989337506920005E-290d, -9.438168176533759E-290d, -1.9220613500411237E-289d, +2.1186736024949195E-289d, +6.3015208029537436E-288d, -8.168129112703755E-288d, -1.6040513288090055E-287d, -1.0809972724404233E-287d, -3.080380385962424E-286d, +2.6399157174374624E-286d, +1.3317127674213423E-285d, -3.5821668044872306E-285d, +1.978536584535392E-284d, +1.3399392455370071E-284d, -2.870168560029448E-284d, +3.5311184272663063E-283d, -7.204247881190918E-283d, +3.2425604548983798E-282d, +3.913063150326019E-282d, -2.260957518848075E-281d, +3.807242187736102E-281d, -5.095591405025083E-281d, +2.3400625068490396E-280d, -1.1564717694090882E-280d, -3.517594695450786E-279d, +6.666544384808297E-279d, -9.204784113858607E-279d, +4.8677119923665573E-278d, +7.942176091555472E-278d, -2.5113270522478854E-277d, +5.332900939354667E-277d, -3.491241408725929E-276d, -2.1141094074221325E-276d, +1.722049095222509E-275d, +4.0430160253378594E-275d, +1.9888195459082551E-274d, +3.230089643550739E-275d, +5.077824728028163E-274d, -3.526547961682877E-274d, -6.4376298274983765E-273d, -2.5338279333399964E-272d, -3.614847626733713E-272d, +2.510812179067931E-272d, +3.953806005373127E-272d, +7.112596406315374E-272d, -2.850217520533226E-270d, -8.571477929711754E-270d, +1.2902019831221148E-269d, -6.978783784755863E-270d, +9.89845486618531E-269d, -3.538563171970534E-268d, +3.537475449241181E-268d, +3.6924578046381256E-267d, +1.3555502536444713E-266d, -1.1279742372661484E-266d, +5.475072932318336E-266d, -1.1679889049814275E-265d, -8.946297908979776E-266d, +1.0565816011650582E-264d, -3.2161237736296753E-265d, -6.022045553485609E-264d, -2.0332050860436034E-263d, -1.0488538406930105E-262d, +1.6793752843984384E-262d, +3.2558720916543104E-263d, -1.9546569053899882E-262d, +5.082190670014963E-262d, -1.0188117475357564E-260d, +3.7920054509691455E-261d, -8.330969967504819E-260d, -1.1623181434592597E-259d, +9.09665088462258E-259d, -1.56400149127482E-259d, -7.796557225750673E-258d, +6.751460509863465E-258d, +7.243157658226935E-258d, +1.2574668958946027E-256d, +2.2678858131411216E-256d, +5.1079306249351287E-256d, -5.672261759108003E-257d, +3.476539491009769E-256d, -1.3481093992496937E-254d, -3.314051560952014E-254d, +7.408112967339146E-255d, -7.164884605413269E-254d, -6.456588023278983E-253d, -1.4881197370811587E-252d, +1.7534012237555307E-252d, -1.3070101381473173E-251d, +6.081420141954215E-251d, +6.591143677421159E-251d, +2.6917461073773043E-250d, +3.683043641790553E-251d, +1.2195076420741757E-249d, -8.220283439582378E-249d, +1.637852737426943E-248d, -8.332543237340988E-249d, +2.9581193516975647E-248d, -1.7790661150204172E-247d, -1.7809679916043692E-247d, +8.378574405736031E-247d, -2.883847036065813E-246d, +1.3223776943337897E-245d, +3.098547586845664E-245d, -1.1036542789147287E-244d, -5.7187703271582225E-244d, -1.8058492822440396E-244d, +4.4373726292703545E-243d, -3.4631935816990754E-243d, -1.82770041073856E-243d, +3.845535085273936E-242d, +8.446532344375812E-242d, +2.7751016140238277E-242d, +1.3158882241538003E-241d, -3.579433051074272E-240d, -6.151751570213211E-240d, -2.990535475079021E-239d, +2.3396028616528764E-239d, +7.233790684263346E-239d, +1.0847913100494912E-238d, +7.103148400942551E-238d, +3.463600299750966E-237d, -4.873121855093712E-237d, +1.3407295326570417E-236d, +9.390271617387205E-237d, -2.4767709454727603E-235d, +3.205923535388443E-235d, -1.0074984709952582E-234d, +2.4747880175747574E-234d, -5.146939682310558E-234d, -2.827581009333298E-233d, -3.0307641004671077E-233d, +5.92044714050651E-233d, -2.0582596893119236E-232d, -6.58066591313112E-232d, -4.869955151949929E-231d, -5.763495903609913E-231d, -2.3580462372762525E-230d, +1.8559980428862584E-230d, +2.854978560542175E-229d, +5.637945686485334E-229d, +2.1454644909004582E-228d, -1.1918070206953359E-228d, -5.021851606912854E-228d, +3.861525553653117E-227d, +6.533561982617909E-227d, -3.015709444206057E-226d, -5.042005018212734E-227d, +1.5959614205422845E-225d, +2.0402105689098835E-224d, +5.164902728917601E-224d, +9.981031744879876E-224d, +4.0281104210095145E-223d, +1.1158160971176672E-222d, +2.0736172194624895E-222d, +4.983162653734032E-222d, +2.1753390051977871E-221d, +3.969413618002761E-221d, +1.3961255018698695E-220d, +2.1290855095314206E-220d, +1.1927747883417406E-219d, +3.7264401117998796E-219d, +9.318532410862293E-219d, +2.3414841777613345E-218d, +4.3791842770430786E-218d, +1.7173159016511951E-217d, +3.5037536832675478E-217d, +1.4300098613455884E-216d, +2.4189403362149483E-216d, +9.306541421999056E-216d, +3.442100456607687E-215d, +5.94407068841904E-215d, +2.0483260435783403E-214d, +3.8410992889527954E-214d, +1.2038281262953917E-213d, +3.865007795216205E-213d, +9.754659138599756E-213d, +2.7653605770745684E-212d, +5.359568079675375E-212d, +2.61726605666378E-211d, +5.054202073556894E-211d, +8.707092668016246E-211d, +1.4080573899148006E-210d, +1.288124387778789E-209d, +1.8639901642011898E-209d, +6.076014540574561E-209d, +1.798489141298457E-208d, +2.1525406805994896E-208d, +1.1864056832305874E-207d, +2.1077440662171152E-207d, +1.3784853708457332E-206d, +1.6965806532093783E-206d, +7.241626420445137E-206d, +2.575584299085016E-205d, +6.151951078101721E-205d, +2.40652042118887E-204d, +4.022633486003565E-204d, +5.8840879519086286E-204d, +3.2820308007277566E-203d, +4.31880454864738E-203d, +2.427240455243201E-202d, +7.326955749884755E-202d, +1.4310184489676175E-201d, +4.464279133463661E-201d, +4.895131474682867E-201d, +4.48614966943544E-200d, +8.924048768324976E-200d, +2.5035535029701945E-199d, +6.627829836338812E-199d, +2.6066826304502746E-198d, +8.042275310036546E-198d, +2.115062964308555E-197d, +4.413745413236018E-197d, +1.644449394585716E-196d, +3.138217752973845E-196d, +7.48533983136081E-196d, +2.613626422028823E-195d, +3.6741841454219095E-195d, +5.906102862953403E-195d, +4.4940857547850743E-194d, +5.840064709376958E-194d, +3.087661273836024E-193d, +4.995552216100365E-193d, +1.991444798915497E-192d, +7.097454751809522E-192d, +2.0510193986749737E-191d, +5.759440286608551E-191d, +1.7013941257113314E-190d, +2.1383323934483528E-190d, +8.280292810015406E-190d, +3.138655772049104E-189d, +7.961506427685701E-189d, +2.0579001228504997E-188d, +7.530840351477639E-188d, +1.4582863136475673E-187d, +3.149267215638608E-187d, +5.443114553057336E-187d, +3.4672966834277804E-186d, +7.374944406615125E-186d, +2.7318417252599104E-185d, +7.913674211949961E-185d, +2.5217716516462005E-184d, +4.0866585874353075E-184d, +1.2087698972768686E-183d, +3.7072473866919033E-183d, +1.1333588840402273E-182d, +1.61949812578045E-182d, +6.567779607147072E-182d, +2.422974840736314E-181d, +2.551170809294396E-181d, +1.0905890688083124E-180d, +3.221279639653057E-180d, +7.068244813489027E-180d, +1.3752309224575428E-179d, +7.20154303462761E-179d, +1.5391707185581056E-178d, +7.708777608683431E-178d, +5.597398155472547E-178d, +1.8487854656676722E-177d, +1.0577249492414076E-176d, +2.8926683313922764E-176d, +4.090184282164232E-176d, +1.6142943398013813E-175d, +7.873864351702525E-175d, +2.242630017261011E-174d, +3.4637009373878283E-174d, +1.5907089565090164E-173d, +1.6985075903314236E-173d, +1.1552273904608563E-172d, +2.237894048535414E-172d, +5.321990399912051E-172d, +1.4106062639738257E-171d, +2.9850404523368767E-171d, +1.5683802588004895E-170d, +4.880146806045633E-170d, +1.1489352403441815E-169d, +1.6401586605693734E-169d, +8.29169700697816E-169d, +1.0380723705441457E-168d, +7.126414081261746E-168d, +1.253325949455206E-167d, +2.595079206183114E-167d, +1.537490712803659E-166d, +2.6338455225993276E-166d, +7.994936425058567E-166d, +1.5716634677516025E-165d, +3.669404761339415E-165d, +1.9941628263579332E-164d, +4.5012079983352374E-164d, +7.283163019991001E-164d, +2.398038505188316E-163d, +7.868666894503849E-163d, +2.1478649410390003E-162d, +8.306111510463843E-162d, +1.5453160659068463E-161d, -4.590496588813841E-162d, +3.5449293983801232E-161d, -1.0440854056870505E-160d, -2.321064927632431E-160d, +5.707867001443433E-160d, -2.238614484037969E-159d, +2.482282821883242E-159d, -1.1508772192025259E-158d, +1.9903990578876104E-158d, -1.2116165315442256E-158d, -2.9084557554502667E-157d, -1.1211083853006645E-156d, -1.309893394818129E-156d, +4.2269712317468864E-156d, -7.678973146281339E-156d, +3.2021376921211934E-155d, -7.08313012515209E-155d, +1.944398214330544E-154d, +1.1860061363751161E-153d, +1.5234465914578058E-153d, -2.9020908354550263E-153d, +4.980100072851796E-153d, +2.3101551448625578E-152d, -1.1959241322537072E-151d, -9.27398924154364E-153d, +5.999390491704392E-152d, +1.3373196561281372E-150d, -1.0271780540759147E-150d, +2.575620466387945E-150d, -6.56250013356227E-149d, -1.1961357917482867E-148d, +5.5807813570926636E-148d, +9.252840189663807E-148d, -1.830335419852293E-147d, +9.350990339947455E-147d, -1.6072409939877762E-146d, -2.5309995887229526E-146d, -1.6014373376410622E-146d, -3.303297758377758E-145d, +1.5640419864850202E-145d, +9.544642884951585E-145d, -8.64864445321803E-144d, +7.580392204597681E-144d, +2.678334184447568E-143d, -3.7269289985326055E-143d, -2.851965258161176E-142d, +7.243267286265823E-142d, +4.4510805312036926E-141d, +9.008499734799015E-141d, +1.130435759928337E-140d, -3.096539751496479E-140d, -1.497405487919762E-139d, +3.51519845948652E-139d, -4.713790209541894E-139d, +4.740753295616865E-138d, +9.517570994930463E-138d, -1.8842098029339485E-137d, -3.825558165008403E-137d, +1.1817638600528107E-136d, -3.514601201473235E-136d, -6.344612631552417E-136d, -1.6754164785291923E-136d, +4.445372986583078E-135d, -3.89604237755475E-134d, -1.0155552195374609E-134d, +2.1858142063550155E-134d, +3.497714990137842E-133d, -7.635830383612894E-133d, +1.2050744860079718E-132d, -7.683019590615251E-133d, -3.344806129021162E-131d, -1.6737914131474577E-131d, -4.30610076666344E-131d, +5.184023388254773E-130d, +2.6290763595212492E-129d, +7.90041744728452E-130d, -3.204473056113754E-129d, -2.552517201762272E-128d, +7.130134251490065E-128d, -3.2244113258340395E-127d, -1.064920993515727E-127d, +2.7466520735457463E-126d, +4.368312797746065E-126d, +1.8802599072446818E-125d, -4.257625799463564E-125d, +5.491672256552995E-125d, +3.7298611779671127E-124d, +5.724180836308973E-124d, +1.3861841053630075E-123d, +4.2303826056297614E-123d, +3.5335436928899096E-123d, -2.522906629540626E-122d, +1.0147808005267102E-121d, +6.734406065735473E-122d, -4.948973160958133E-121d, +2.4256181927024344E-120d, +4.9056283164780554E-120d, +6.846440394397547E-120d, +3.512747689569002E-119d, -9.020907406701404E-119d, +2.5718749916003624E-118d, +4.3724191002977524E-119d, +1.001455050575191E-117d, -2.4442443105031435E-117d, +2.38873950760028E-116d, -4.831068747037129E-118d, -5.148989321866988E-116d, +1.7875271881514469E-115d, -1.1821586412088555E-114d, +4.43247726423679E-115d, +4.634817120492781E-114d, +1.671311907037975E-113d, -4.595250028278979E-113d, -5.905511605694905E-113d, -1.3657642265608213E-112d, +2.881416869529271E-112d, +2.1253302469985373E-111d, -5.301386276260592E-111d, +1.4198782892306878E-112d, -3.395494928605007E-110d, +9.284633292147283E-110d, -6.054133004896379E-110d, -8.324100783330331E-109d, -2.193190669794277E-108d, +1.3613655394659198E-107d, +6.463452607647978E-108d, +1.0187183636134904E-106d, +1.0705673935006142E-106d, +2.509050608571019E-106d, -1.5096182622106617E-105d, +1.7794190449526737E-106d, +1.2261246749706581E-104d, +2.1377905661197194E-104d, -2.2015877944429946E-104d, +7.873970951802825E-104d, -1.7999197335480384E-103d, +1.0487383011058756E-105d, -2.9988278531841556E-102d, +4.7976477743232285E-102d, +3.452316818502442E-102d, +5.89953246760617E-101d, -4.0785601577267006E-101d, +2.7214076662438963E-100d, +5.237807655758373E-100d, +6.180972117932364E-99d, -1.3019742873005683E-98d, +4.501188264957416E-99d, -2.4075054705261798E-98d, +1.6503086546628772E-97d, -6.878666975101243E-97d, +1.196718116616528E-96d, +2.476190162339265E-96d, -7.1844969234484515E-96d, +5.088386759261555E-95d, +6.749368983223726E-95d, +1.965737856765605E-94d, -5.574080023496771E-94d, +1.2493696959436675E-93d, +8.533262777516794E-94d, -7.225259028588793E-93d, -7.340587186324432E-93d, -3.482412195764625E-92d, +3.4742610108480497E-91d, -7.177274244758699E-91d, +1.2736636153072213E-90d, -5.730160886217076E-90d, -1.545495535488274E-89d, +1.1304179460367007E-89d, +1.249260560756154E-88d, -4.7439719382414206E-88d, +7.164663249266942E-88d, +1.7617425105337704E-87d, +2.4175248449172035E-87d, -1.043079666926483E-86d, -2.8137609614326677E-86d, -1.2091497144395591E-85d, +3.7944631664558904E-85d, -2.8144926807308225E-85d, +3.9782728352520784E-85d, +4.313978872469646E-84d, +5.82190887044604E-84d, +5.883385169571802E-83d, +1.134857098306787E-82d, +3.468049324128309E-82d, +2.625423995658143E-82d, -3.42827917465521E-81d, +5.119461911618321E-81d, -2.134387988350615E-80d, -4.4703076268400615E-80d, +4.806078883451016E-80d, +2.3820250362443495E-79d, -7.258551497833573E-79d, -4.0297835558876335E-78d, +2.1424166787650852E-78d, -3.2117127164185917E-77d, +4.8459153070935316E-77d, -1.766924303914492E-76d, -2.6921749814579492E-76d, -4.1291070428848755E-76d, +2.2086994756104319E-75d, -7.814146377574201E-75d, -1.9589778310104216E-74d, +6.52658129486538E-74d, +1.7804909509998055E-74d, -4.1900132227036916E-73d, +1.5705861683841123E-72d, -1.904714978998808E-72d, -7.81295459930537E-72d, +2.818537910881676E-71d, +5.840507984320445E-71d, +1.7331720051707087E-70d, +1.936946987935961E-70d, -5.86517231340979E-71d, -1.3277440528416646E-69d, +1.9906256185827793E-69d, +8.668714514280051E-69d, +6.643105496829061E-69d, -2.5436254170647032E-67d, -4.8279217213630774E-67d, -1.2640304072937576E-66d, +3.51187258511716E-66d, +1.4199501303738373E-65d, -1.2351697477129173E-65d, +7.0542365522414836E-65d, +1.030593104122615E-64d, -5.452692909894593E-65d, -9.415506349675128E-64d, -3.6206211446779087E-63d, -1.6699188275658641E-62d, +2.287280262665656E-62d, +7.076135457313529E-62d, +2.9019628518165404E-61d, -3.1305705497720186E-61d, +2.2978757040142953E-60d, +1.2424439441817321E-60d, +7.140343013236265E-60d, +8.633726388939636E-60d, +1.3483035574114863E-58d, +1.653701058949654E-58d, -8.939932297357388E-58d, -1.395320103272191E-57d, +6.440430933947252E-58d, -1.681200826841738E-56d, +3.9904382022898837E-56d, -4.870518577546228E-56d, -1.6990896855901115E-55d, -6.751434891261518E-56d, -1.669012123121194E-54d, -4.079585475491198E-54d, -1.3070436427679952E-53d, -3.090028378908628E-53d, +7.468160889798606E-53d, +6.229095980733463E-53d, +1.4794751934479566E-52d, +1.7444373785853918E-51d, -5.3681978363391484E-52d, +2.71853394036182E-51d, -1.3334367969274016E-50d, -1.6958057665854177E-49d, -1.452507231312146E-49d, +3.3855429446520427E-49d, +4.903687986212687E-49d, +2.2185957416622524E-48d, -9.924196700842429E-48d, +4.285128462851149E-47d, +3.076063086193525E-48d, +4.102052341676543E-46d, +1.1745772638457318E-45d, -5.309047216809048E-47d, +2.72972449891179E-45d, -1.1748423022293739E-44d, +6.626052626622228E-44d, +3.0227439688367925E-44d, -4.740494808228372E-43d, +5.926057457356852E-43d, +3.09768273342776E-42d, -5.589493227475577E-42d, -8.84908716783327E-42d, +2.3684740712822874E-41d, +1.4836491430755657E-40d, +4.5878801324451396E-40d, +1.0585156316103144E-39d, +2.3805896467049493E-39d, +1.0285082556185196E-38d, +2.5187968110874885E-38d, -1.4088399542613178E-38d, -3.00901028043488E-38d, +2.0089026801414973E-37d, -1.3324111396289096E-36d, +5.458481186294964E-36d, -4.8402541351522003E-36d, -1.3331969720555312E-35d, -8.248332290732976E-35d, -1.8349670703969982E-34d, +6.403477383195494E-34d, +3.7813691654412385E-34d, +2.4621305031382827E-33d, -5.634051826192439E-33d, +3.817173955083142E-32d, -6.038239639506472E-32d, -2.130447095555397E-31d, -6.824454861992054E-31d, -1.3455801602048414E-30d, -2.518642767561659E-30d, +8.082792416221215E-30d, +4.718103502869148E-29d, -5.607991635038776E-29d, -1.8042191582018579E-28d, +6.989914264479507E-28d, -2.9031739430339586E-28d, +6.076820259849921E-27d, -3.24981577480893E-27d, -2.7648210023059463E-26d, -9.785306155980342E-26d, +1.241529292737115E-25d, +3.0891604448087654E-25d, +2.3451052074796954E-24d, +6.574128018028633E-24d, -1.3345148716925826E-23d, +4.3594621428644293E-23d, -5.678896695157704E-23d, -4.676849004137386E-23d, -2.281578975407609E-22d, -3.144430608076357E-21d, +5.662033727488754E-22d, -4.30293375386492E-21d, +4.985137671479376E-20d, +1.657668502165438E-19d, -3.3878706977811337E-19d, -7.488022803661722E-19d, +1.725039737424264E-18d, -6.0275040161173166E-18d, -8.081007442213538E-19d, +2.9257892371894816E-17d, +1.5231541295722552E-16d, -1.1474026049124666E-17d, +6.890372706231206E-16d, +2.592721454922832E-15d, -1.1253822296423454E-15d, -2.650684279637763E-14d, -4.107226967119929E-15d, -3.130508064738312E-14d, -6.729414275200856E-14d, -1.6166170913368169E-12d, -1.2059301405584488E-12d, -1.2210091619211167E-11d, +3.695372823623631E-12d, +5.119220484478292E-11d, -1.0857572226543142E-10d, -4.6490379071586397E-10d, -4.5810381714280557E-10d, +1.4909756678328582E-9d, -1.3155828104004438E-8d, -9.149755188170102E-9d, +0.0d, +8.254840070411029E-8d, -1.0681886149151956E-7d, -3.359944163407147E-8d, -2.1275002921718894E-6d, +1.2129920353421116E-5d, +2.1520078872608393E-5d, +1.0178783359926372E-4d, -2.077077172525637E-5d, -5.67996159922899E-5d, +9.510567165169581E-4d, +0.0010901978184553272d, +0.010169003920808009d, +0.017008920503326107d, +0.03416477677774927d, -0.1275278893606981d, +0.5205078726367633d, +0.7535752982147762d, +1.1373305111387886d, -3.036812739155085d, +11.409790277969124d, -9.516785302789955d, -49.86840843831867d, -393.7510973999651d, -686.1565277058598d, +4617.385872524165d, -11563.161235730215d, -8230.201383316231d, -34460.52482632287d, +50744.04207438878d, +357908.46214699093d, +1936607.425231087d, +3222936.695160983d, +5477052.0646243105d, -3.517545711859706E7d, -1.2693418527187027E8d, -2.5316384477288628E8d, -1.6436423669122624E8d, +4.0889180422033095E8d, +4.968829330953611E9d, -3.503399598592085E9d, +1.905394922122271E10d, +1.0361722296739479E11d, -5.806792575852521E10d, +2.3454138776381036E11d, -1.718446464587963E12d, -1.0946634815588584E12d, +1.6889383928999305E13d, -3.784600043778247E13d, +7.270965670658928E13d, -4.9202842786896806E14d, +4.597700093952774E14d, +2.6113557852262235E15d, -4.544525556171388E15d, -9.517971970450354E15d, -2.0634857819227416E16d, -9.7143113104549808E16d, -2.2667083759873216E16d, -7.2285665164439578E17d, +4.1215410760803866E18d, +8.5807488300972206E18d, +1.530436781375042E19d, -1.5453111533064765E19d, -1.0633845571643594E20d, -3.512380426745336E20d, +3.7734658676841284E20d, -3.855478664503271E21d, +7.984485303520287E21d, -1.2296934902142301E22d, +1.042139023692827E22d, +1.2167897656061312E23d, +9.22064170155394E22d, +3.965171513035854E23d, -4.135121057126514E24d, -7.944341754299148E24d, +1.4715152230577016E25d, -3.0635272288480756E25d, -9.54468158713835E25d, +1.5411775738825048E25d, -8.274711842374368E26d, -1.0028324930788433E27d, +5.189062091114782E27d, -2.8583500869462184E28d, -5.198295198128238E28d, +2.9758750368256437E29d, +3.216046320616945E29d, -1.7846700158234043E30d, +3.847174961282827E30d, +9.026991921214922E30d, +4.1358029739592175E30d, -6.461509354879894E29d, +9.704297297526684E31d, +2.9731739067444943E32d, +9.97728609663656E32d, +3.1149346370027763E33d, +2.0051635097366476E34d, +2.819272221032373E34d, +1.6266731695798413E34d, +1.998050894021586E35d, -6.1633417615076335E35d, +2.2505716077585116E36d, +1.9299691540987203E36d, +8.006569251375383E36d, -3.785295042408568E37d, -1.1870498357197593E38d, +1.0010529668998112E38d, +1.3240710866573994E38d, +2.6888010385137123E39d, +1.7400655988987023E39d, -6.402740469853475E39d, -3.93114092562274E40d, +1.2363717201084252E41d, -1.9219116633978794E41d, -1.347867098583136E42d, +7.87675118338788E41d, +3.3932984011177642E41d, -1.9872713979884691E43d, +2.220208491349658E43d, -3.466267817480825E43d, +3.19462030745197E44d, -9.841244788104406E44d, -2.2676593395522725E45d, -1.1349246400274207E46d, -1.1700910284427406E46d, -3.6754317105801715E46d, +1.7647101734915075E47d, +2.122358392979746E47d, +3.156243682143956E47d, +5.356668151937413E47d, +2.7668218233914262E48d, +3.5127708120698784E48d, +1.7884841356632925E49d, +1.716531820904728E50d, -2.9114757102866277E50d, +1.0657703081219677E51d, -7.512169809356372E50d, +1.764200470879736E51d, -1.0088898215431471E52d, -3.1085734725176E52d, +4.3529009584292495E52d, -2.467842129213774E53d, -3.9317379627195146E53d, -4.332335454045836E52d, +7.979013724931926E54d, -1.5038413653121357E55d, +9.310799925566843E55d, -2.2042966348036592E55d, -4.518315366841937E55d, -6.971366338144781E56d, -2.0461505570781806E57d, -8.823884392655312E57d, -1.1264032993918548E58d, -7.692065092509875E58d, -1.8472516879728875E59d, +8.72220314694275E58d, +1.6525336989036362E59d, -3.343201925128334E60d, +5.493352163155986E60d, -2.548073509300398E61d, -9.566541624209933E61d, +4.0891054447206644E61d, -7.724182294653349E62d, +1.0143022354947225E63d, -4.952031310451961E63d, -7.877410133454722E63d, +4.505432606253564E64d, -7.330635250808021E64d, -1.642361029990822E65d, +5.982180242124184E65d, +7.120242132370469E65d, +5.908356249789671E66d, -2.8477710945673134E65d, +6.65688196961235E66d, -9.233295580238604E67d, +3.2850043261803593E68d, +7.041681569694413E68d, -1.5652761725518397E69d, +1.5377053215489084E68d, +1.282130763903269E70d, -2.380286345847567E70d, -7.207022875977515E70d, +2.7641662602473095E71d, +7.685235201534525E71d, +4.3239378585884645E70d, -1.6840562544109314E72d, -5.04128025464686E71d, +5.4557485189210095E73d, +7.160277784358221E73d, +7.636179075087608E73d, -8.18804507680012E74d, +2.807397988979441E75d, +2.165163304600171E75d, -1.3208450062862734E76d, -5.1939252391404724E76d, -6.985952908805853E76d, -1.6259920998287064E77d, +6.098975200926637E77d, -5.63383579957466E77d, -1.5876819186852907E78d, +2.1487475413123092E79d, -3.987619123706934E79d, +9.772655251656639E79d, -1.638756156057952E79d, -7.83892088580041E80d, +1.274413296252691E81d, +2.51946651720982E81d, -2.516866097506943E81d, +1.053956282234684E82d, +1.8279051206232177E83d, +1.2250764591564252E82d, -4.0353723442917463E83d, -1.4121324224340735E84d, -5.45287716696021E84d, -1.7514953095665195E85d, -5.0706081370522526E85d, -4.35799392139009E85d, -3.982538093450217E86d, -1.4591838284752642E87d, +2.5313735821872488E87d, -3.718501227185903E86d, -1.3907979640327008E88d, -5.79002114093961E86d, -1.2500675565781447E89d, +4.8182788286170926E89d, -1.7198866036687559E90d, -4.690417668647599E88d, +1.3020631859056421E91d, -1.3850458263351744E91d, +4.87301010703588E91d, -1.695546877943826E92d, -1.6353756659909833E92d, -1.5483926773679628E93d, -1.8921091400297595E93d, -6.183525570536406E93d, -4.987913342551977E93d, +1.0186485886120274E93d, -1.5343120819745468E95d, -5.262123923229857E95d, +1.618327917706804E96d, -4.135185828158998E96d, -8.016793741945299E96d, -3.0399439534134115E97d, -1.2319346292749103E98d, +7.536337311795176E97d, -3.577715974851322E98d, +2.0521614818695524E99d, +1.2627736197958951E98d, -5.206910481915062E99d, +3.0974593993948837E100d, -9.522726334561169E100d, -1.1909272509710985E100d, -5.056512677995137E101d, +2.0902045062932175E102d, +6.243669516810509E102d, -1.7375090618655787E103d, -2.5445477450140954E103d, +3.619891246849381E103d, +8.90737333900943E103d, -2.7897360297480367E104d, +1.3725786770437066E105d, -8.316530604593264E105d, -6.054541568735673E105d, +7.523374196797555E105d, +1.1475955030427985E107d, +1.5260756679495707E107d, +7.370294848920685E107d, +1.3608995799112174E108d, +1.0700758858011432E108d, -4.989318918773146E108d, -1.6629755787634093E108d, +7.635999584053557E109d, +1.892621828736983E109d, -6.793094743406533E110d, -8.160628910742724E110d, -7.724219106106896E111d, -1.6059226011778748E112d, -1.5277127454062126E112d, +3.911086668967361E112d, +3.529920406834134E113d, -4.3991443996021166E113d, -1.2631909085915044E114d, +3.8656278695544835E114d, +1.71845288713123E115d, +3.7660598745907915E115d, -4.048086182363988E115d, +2.3093822298965837E116d, -9.684925795536813E116d, -3.137992585221854E117d, -5.637415935329794E117d, -1.5536658521931418E118d, -6.336314643222911E118d, +8.550658957115427E118d, -5.591880480212007E119d, +2.4137404318673354E119d, -2.631656656397244E120d, -7.653117429165879E119d, -4.073965591445897E121d, +3.634781057940233E121d, +4.537273754534966E121d, -2.5138919966097735E122d, -1.0292817180691822E123d, -1.4265564976097062E122d, +6.000235114895513E123d, +4.186590347846346E124d, -1.8950538406321535E124d, +7.716762345695022E124d, -4.443798187035849E125d, -2.268994961992292E125d, -2.8169291774231604E126d, -2.749127978087685E126d, -2.2929764629585683E126d, -7.369842361872221E127d, +2.81312841469177E128d, +2.7856896414497757E128d, -3.096733638475319E128d, -5.4199510725063615E129d, -7.315860999413894E129d, +3.6424644535156437E130d, -7.886250961456327E130d, +5.289988151341401E130d, +2.7758613753516344E131d, -2.738246981762776E132d, -2.2667181460478093E132d, -3.614672661225457E131d, +2.325337720526947E133d, +4.16603235883392E133d, -6.50348962894948E133d, +3.851445905038431E134d, -5.46060534001412E134d, +5.4679180659102885E135d, -3.037477806841494E135d, -3.0417051809209134E136d, -6.995964550587914E136d, -3.6897084415718804E137d, -6.938000231893302E137d, +2.403806217004454E138d, -3.4552363953199905E138d, +7.3409917428393E138d, -1.7445917446236717E139d, -6.680679913078676E139d, -8.193572619487537E139d, +5.337290292186291E139d, -3.951314467739045E140d, -4.4662073456574476E141d, +6.249381778908997E141d, -2.928362616578011E142d, -1.6661676835672304E143d, -1.974465323891493E143d, +1.3083870531380308E144d, -2.382825271750576E144d, -5.4826958838142734E144d, +1.5340733916570804E145d, -3.1327120557842516E145d, +1.5790297768522832E146d, +1.1518771984292262E146d, -4.789917000227385E145d, -8.689594184775204E146d, +3.0680417869552433E146d, +4.877860620031438E147d, -3.4650891244084597E148d, +1.8702183451052442E149d, -3.5727227900139915E148d, -1.3457821696677932E150d, +3.3212950284273017E149d, +7.316033240396569E150d, -7.187723217018267E150d, -8.537194547485455E150d, -1.4561530066010593E152d, -7.548155147049997E151d, +1.0047353208353007E153d, -1.2489460589853119E153d, +4.426120229279107E153d, -2.5466223330961086E154d, +8.831699889789037E154d, -2.0258084311749475E155d, -5.525009099476396E155d, -1.0235056525096769E156d, -4.117971654572494E154d, -4.7559175309753334E156d, -1.4656240137098836E157d, -7.675790582869644E157d, -1.0126616322947826E158d, +7.084865265284368E158d, -9.374695893307895E158d, +2.05597910889115E159d, -7.368602086210704E159d, -1.6167825196198978E160d, +2.3832096207000712E160d, +1.3166970112139726E161d, -6.432337568761393E161d, +2.9279594746502846E161d, +4.8926595743317624E162d, +1.2704793774453618E163d, -1.1345910784680524E163d, +7.75933511025868E163d, -1.1441115218462356E163d, +5.162248481759758E164d, +6.362563919556132E164d, -2.8362173224732088E165d, -4.342161053332263E165d, +4.388125271425036E166d, -7.049068240916723E166d, +3.8520227881415595E166d, +2.9274120974020826E167d, -7.500936767542933E167d, -6.540181860667302E168d, +4.664436780622191E168d, -1.436111169285268E169d, -1.0407581736224179E170d, -2.7670181051374297E170d, -6.788169932297778E170d, +1.6997387217850427E171d, -1.0965324942770584E171d, +9.841563119484623E171d, +3.175748919314254E172d, +2.9621181706527444E172d, -3.30101656090905E173d, -3.791840683760427E173d, -2.841347842666459E174d, -7.836327226971707E174d, +9.650358667643114E174d, +5.9994277301267294E175d, -6.0490084078440215E175d, -2.8964095485948707E176d, +9.916187343252014E175d, +2.7535627955313556E176d, +3.886891475472745E177d, +3.1962472803616787E178d, -5.50599549115449E178d, +5.672812341879918E178d, -3.295268490032475E179d, +9.761163062156018E179d, +3.107837179570674E180d, +3.3894811576571423E179d, -5.235397688850367E180d, -5.004237248003625E181d, -1.7544995191195304E182d, +2.645622651144787E182d, -3.459885432869825E182d, -4.0361435606199565E183d, -1.8382923511801317E183d, -1.7332235571505177E184d, +2.847653455671381E184d, +1.7991060813894693E185d, -2.0937429891059164E185d, +5.744446753652847E185d, -2.1349396267483754E184d, -1.2542332720182776E186d, +3.3730714236579374E186d, -5.923734606208998E187d, +2.24669039465627E188d, -1.2588742703536392E188d, +1.474522484905093E189d, -2.4006971787803736E189d, -3.52597540499141E189d, +2.6676722922838097E190d, +5.27579825970359E190d, +2.1360492104281465E191d, +1.9442210982008953E191d, -1.4691239161932232E190d, +3.8218180377739526E192d, +1.9722862688653467E192d, +3.047601928063002E193d, +1.6747356805175311E193d, +7.710512446969693E192d, +1.7780021277684035E194d, -1.4015110811648513E195d, +4.0447634595724164E195d, +9.023639664212642E195d, +1.976868146639626E196d, -9.084495133765657E196d, -1.2023077889892748E196d, +5.7455368446308694E197d, -1.7766273910482863E198d, +3.5590470673352285E198d, +1.1304970373249033E199d, +1.6496143246803731E199d, -2.394588390685223E199d, -1.4677321100833294E199d, -1.1843870433971731E201d, -1.8853982316037226E201d, +2.8829871423339434E201d, +5.369687677705385E200d, +1.8356062677502141E202d, -1.5544655377217875E203d, +2.955364187248884E203d, -2.7651059253677425E203d, +9.903174064539538E203d, -3.284204788892967E204d, -1.5843229740595697E205d, +5.333371443528904E204d, +1.2781631468016048E205d, +3.2188292385399854E205d, -6.619064395428225E206d, +1.291561142865928E207d, +1.3142988156905172E207d, -1.3841980097978606E208d, +6.380177790989479E207d, +1.0386032577072378E209d, +2.7681631086098026E209d, -9.053874899534375E209d, +1.2424707839848734E210d, +1.045546633850141E211d, -1.2448938139338362E211d, +7.221902646057552E211d, +6.651345415954053E211d, -5.8180712702152444E212d, +5.275183961165903E212d, +5.092753117288608E212d, -2.437796532151255E213d, +1.3480763914637323E214d, +5.619995933180841E214d, +2.547000388735681E214d, +4.817319356453926E214d, -7.897146442236022E215d, -7.93844120619577E215d, -4.9489938500591624E216d, -2.862720607805682E216d, -2.9275804461593914E217d, -3.411186219855533E217d, -2.0110092718356274E218d, -8.472642266772353E218d, -4.357990742470246E217d, +4.793444363780116E219d, +1.6544084224626834E220d, -6.017988576347111E220d, -3.580397221598409E220d, -4.7208848667217906E221d, -7.724899660259369E221d, -2.4459728627968634E222d, +3.667348665023154E221d, +4.544122762558404E223d, -4.0573420909530794E223d, -3.2552002992257195E223d, -6.488296536838142E224d, +1.7544839352461719E224d, -4.0873400635183405E225d, -8.833499967268279E225d, -1.0953484767704112E226d, -8.56825295972308E226d, -1.8097633115378247E227d, -6.171564449018882E227d, -4.351843341274115E227d, +2.8032429752543687E228d, -1.0065901934522996E229d, +9.863720960170636E228d, -9.481088691357648E229d, -1.6843492713373762E229d, -1.3282890219894906E230d, +6.883577595238845E230d, -1.153577281189635E231d, -8.009548754642203E231d, -4.722612904888278E232d, -4.768909872963015E232d, +3.2542391242036633E233d, +6.513425781583774E233d, -1.8889614379831606E233d, -2.227647301474917E234d, -4.7971208532986115E234d, +6.693500938105557E234d, -6.587776621471115E234d, +3.0099905634916516E236d, -4.6694407626686244E235d, +2.965546585110978E236d, +5.771457643937893E237d, -9.029878114318277E237d, +8.169926810324408E237d, -1.779945804977441E239d, +4.1218749988429474E239d, +7.201319954099161E239d, -1.530845432304069E240d, -3.861762510530086E240d, -2.4090696463777446E241d, -1.8196842273916379E241d, -1.7959243076374794E242d, -3.7257346819782323E242d, +3.413310324247329E242d, -2.0406580894051073E243d, -1.5335923091350053E243d, -1.056727406551016E244d, -4.6753408714233723E244d, -2.0697130057384643E245d, -1.0356006160554071E245d, +1.1339195187304043E246d, +1.792783182582235E246d, +9.599214853681978E245d, +1.5367645598839362E247d, +2.934570385464815E247d, -1.6411525886171892E248d, +2.2638862982382794E248d, -1.2268014119628852E249d, +4.737693450915584E247d, +6.3818993714899675E249d, +1.2639113706171572E250d, -4.011320021817099E249d, -5.2744376732859406E250d, -3.732266217624991E251d, +1.7591819833844019E252d, -3.292458622014749E252d, -9.161340309319204E252d, -1.728610646009749E253d, +1.1698424008604891E254d, -1.8494343291160577E254d, +2.0568656302182574E254d, +1.0537591246531136E255d, +1.803052068234866E254d, -1.053036399720808E256d, +2.1836166619192508E256d, +1.0368403169781264E257d, -2.0648015610276362E257d, +8.426174035728768E257d, -1.3577357192972777E258d, +2.1313950901331177E258d, +8.919141843592823E258d, -1.1800039972549816E259d, -1.1878772398311421E260d, -1.538273497873993E260d, -4.51305093266001E260d, +1.1241179396053055E261d, +6.154786508667658E261d, -1.0626125049032536E262d, -1.8908603201210102E262d, -4.571195152299358E262d, +1.526100002923062E263d, -9.457084582570225E263d, -1.5460500618825853E264d, -5.598276199126451E264d, -1.2074097381167957E265d, -3.015972957475025E265d, +1.4345106852061226E265d, +8.28479585346867E265d, -3.118741081244705E266d, -1.2054747399765794E266d, +3.4454766202661184E267d, +1.1279135096919439E268d, +1.2066382528772518E268d, +1.1984128162292276E269d, +3.685169705587367E268d, +6.570047690198998E269d, +1.8836492887460383E270d, +7.4364594917181125E270d, +1.2773080633674971E271d, +1.8928981707279692E271d, +4.039437286589528E271d, +1.785277385538302E272d, -6.017681359527226E272d, +1.9716943051755635E273d, -8.772048092842086E271d, +1.5645672698520312E274d, -3.7979660725865874E274d, +5.324902289537048E274d, -1.8806716685063293E274d, +9.320900373401115E275d, +1.4615985810260016E275d, +8.321226457219046E276d, -4.608112855795952E276d, -3.476352191116455E277d, +5.266381689434054E277d, -9.622106063561645E277d, +4.1719443712336026E278d, +4.222411269063919E279d, -6.714376022102489E279d, -1.0732735585199074E280d, -2.5866883048437488E280d, -1.1306860837934988E281d, +3.690690354793168E281d, -5.5299180508885456E281d, +2.7006726968568243E282d, +4.135457669031131E282d, +2.8401159516008676E283d, +5.127265762024798E283d, -3.4893601256685762E283d, -1.145160459652136E283d, +2.1742808735341656E284d, +4.656972469326391E285d, +7.672307991205681E285d, +1.5781599575584034E286d, +4.218682431618625E286d, -2.4602260687026867E287d, +2.7211316452521414E287d, -1.8740018211089393E288d, +2.6367639658206183E288d, -3.102678910525039E288d, +1.1992295328636466E289d, +6.8190133180135345E289d, +5.783203879030497E289d, +5.171047077293295E290d, +1.8396930096213817E290d, +1.4977047507315718E290d, +1.0672499803427623E292d, +3.3310942289102464E291d, -7.962256961838823E292d, +1.7396889119023863E293d, +3.8072183820435085E293d, +2.2772059538865722E294d, -2.0549866377878678E294d, -1.2277120342804144E295d, -3.609949022969024E295d, +1.1479863663699871E296d, -1.5314373779304356E296d, -2.2537635160762597E296d, -6.1370690793508674E296d, -4.996854125490041E297d, -6.883499809714189E297d, -2.595456638706416E298d, -1.1892631528580186E299d, -1.4672600326020399E299d, -3.200068509818696E299d, -7.126913872617518E298d, -3.3655587417265094E300d, Double.NaN, Double.NaN, Double.NaN, Double.NaN, Double.NaN, Double.NaN, Double.NaN, Double.NaN, Double.NaN, Double.NaN, Double.NaN, Double.NaN, Double.NaN, Double.NaN, Double.NaN, Double.NaN, Double.NaN, Double.NaN, Double.NaN, Double.NaN, Double.NaN, Double.NaN, Double.NaN, Double.NaN, Double.NaN, Double.NaN, Double.NaN, Double.NaN, Double.NaN, Double.NaN, Double.NaN, Double.NaN, Double.NaN, Double.NaN, Double.NaN, Double.NaN, Double.NaN, Double.NaN, Double.NaN, Double.NaN, }; /** Exponential over the range of 0 - 1 in increments of 2^-10 * exp(x/1024) = expFracTableA[x] + expFracTableB[x]. * 1024 = 2^10 */ private static final double[] EXP_FRAC_A = new double[] { +1.0d, +1.0009770393371582d, +1.0019550323486328d, +1.0029339790344238d, +1.0039138793945312d, +1.004894733428955d, +1.0058765411376953d, +1.006859302520752d, +1.007843017578125d, +1.0088276863098145d, +1.0098135471343994d, +1.0108001232147217d, +1.0117876529693604d, +1.0127761363983154d, +1.013765811920166d, +1.014756202697754d, +1.0157477855682373d, +1.016740083694458d, +1.0177335739135742d, +1.0187277793884277d, +1.0197231769561768d, +1.0207195281982422d, +1.021716833114624d, +1.0227150917053223d, +1.023714303970337d, +1.024714469909668d, +1.0257158279418945d, +1.0267179012298584d, +1.0277209281921387d, +1.0287251472473145d, +1.0297303199768066d, +1.0307364463806152d, +1.0317435264587402d, +1.0327515602111816d, +1.0337605476379395d, +1.0347704887390137d, +1.0357816219329834d, +1.0367934703826904d, +1.037806510925293d, +1.038820505142212d, +1.0398354530334473d, +1.040851354598999d, +1.0418684482574463d, +1.0428862571716309d, +1.043905258178711d, +1.0449252128601074d, +1.0459461212158203d, +1.0469679832458496d, +1.0479910373687744d, +1.0490150451660156d, +1.0500397682189941d, +1.0510656833648682d, +1.0520927906036377d, +1.0531206130981445d, +1.0541496276855469d, +1.0551795959472656d, +1.0562105178833008d, +1.0572423934936523d, +1.0582754611968994d, +1.059309482574463d, +1.0603444576263428d, +1.061380386352539d, +1.0624175071716309d, +1.06345534324646d, +1.0644943714141846d, +1.0655345916748047d, +1.066575527191162d, +1.067617654800415d, +1.0686607360839844d, +1.0697050094604492d, +1.0707499980926514d, +1.071796178817749d, +1.072843313217163d, +1.0738916397094727d, +1.0749409198760986d, +1.075991153717041d, +1.0770423412322998d, +1.078094720840454d, +1.0791480541229248d, +1.080202341079712d, +1.0812578201293945d, +1.0823142528533936d, +1.083371639251709d, +1.08443021774292d, +1.0854897499084473d, +1.086550235748291d, +1.0876119136810303d, +1.088674545288086d, +1.089738130569458d, +1.0908029079437256d, +1.0918686389923096d, +1.092935562133789d, +1.094003438949585d, +1.0950722694396973d, +1.096142053604126d, +1.0972130298614502d, +1.09828519821167d, +1.099358320236206d, +1.1004323959350586d, +1.1015074253082275d, +1.102583646774292d, +1.103661060333252d, +1.1047391891479492d, +1.105818748474121d, +1.1068990230560303d, +1.107980489730835d, +1.1090631484985352d, +1.1101467609405518d, +1.1112313270568848d, +1.1123170852661133d, +1.1134037971496582d, +1.1144917011260986d, +1.1155805587768555d, +1.1166706085205078d, +1.1177616119384766d, +1.1188538074493408d, +1.1199469566345215d, +1.1210410594940186d, +1.1221363544464111d, +1.1232328414916992d, +1.1243302822113037d, +1.1254286766052246d, +1.126528263092041d, +1.127629041671753d, +1.1287307739257812d, +1.129833459854126d, +1.1309373378753662d, +1.132042407989502d, +1.133148431777954d, +1.1342556476593018d, +1.1353638172149658d, +1.1364731788635254d, +1.1375834941864014d, +1.1386950016021729d, +1.1398074626922607d, +1.1409211158752441d, +1.142035961151123d, +1.1431517601013184d, +1.14426851272583d, +1.1453864574432373d, +1.14650559425354d, +1.1476259231567383d, +1.148747205734253d, +1.149869441986084d, +1.1509928703308105d, +1.1521174907684326d, +1.153243064880371d, +1.154369831085205d, +1.1554977893829346d, +1.1566267013549805d, +1.1577568054199219d, +1.1588881015777588d, +1.160020351409912d, +1.161153793334961d, +1.1622881889343262d, +1.163423776626587d, +1.1645605564117432d, +1.1656982898712158d, +1.166837215423584d, +1.1679773330688477d, +1.1691184043884277d, +1.1702606678009033d, +1.1714041233062744d, +1.172548532485962d, +1.173694133758545d, +1.1748409271240234d, +1.1759889125823975d, +1.177137851715088d, +1.1782879829406738d, +1.1794393062591553d, +1.1805915832519531d, +1.1817450523376465d, +1.1828997135162354d, +1.1840553283691406d, +1.1852121353149414d, +1.1863701343536377d, +1.1875293254852295d, +1.1886897087097168d, +1.1898510456085205d, +1.1910135746002197d, +1.1921772956848145d, +1.1933419704437256d, +1.1945080757141113d, +1.1956751346588135d, +1.1968433856964111d, +1.1980125904083252d, +1.1991832256317139d, +1.200354814529419d, +1.2015275955200195d, +1.2027015686035156d, +1.2038767337799072d, +1.2050528526306152d, +1.2062301635742188d, +1.2074086666107178d, +1.2085883617401123d, +1.2097692489624023d, +1.210951328277588d, +1.2121343612670898d, +1.2133188247680664d, +1.2145042419433594d, +1.2156908512115479d, +1.2168786525726318d, +1.2180676460266113d, +1.2192575931549072d, +1.2204489707946777d, +1.2216413021087646d, +1.222834825515747d, +1.224029779434204d, +1.2252256870269775d, +1.2264227867126465d, +1.227621078491211d, +1.2288203239440918d, +1.2300209999084473d, +1.2312228679656982d, +1.2324256896972656d, +1.2336299419403076d, +1.234835147857666d, +1.23604154586792d, +1.2372493743896484d, +1.2384581565856934d, +1.2396681308746338d, +1.2408792972564697d, +1.2420918941497803d, +1.2433054447174072d, +1.2445201873779297d, +1.2457361221313477d, +1.2469532489776611d, +1.2481715679168701d, +1.2493910789489746d, +1.2506117820739746d, +1.2518336772918701d, +1.2530567646026611d, +1.2542810440063477d, +1.2555065155029297d, +1.2567331790924072d, +1.2579610347747803d, +1.2591900825500488d, +1.260420322418213d, +1.2616519927978516d, +1.2628846168518066d, +1.2641184329986572d, +1.2653534412384033d, +1.266589879989624d, +1.2678272724151611d, +1.2690660953521729d, +1.27030611038208d, +1.2715470790863037d, +1.272789478302002d, +1.2740330696105957d, +1.275277853012085d, +1.2765238285064697d, +1.27777099609375d, +1.2790195941925049d, +1.2802691459655762d, +1.281519889831543d, +1.2827720642089844d, +1.2840254306793213d, +1.2852799892425537d, +1.2865357398986816d, +1.287792682647705d, +1.2890510559082031d, +1.2903103828430176d, +1.2915711402893066d, +1.2928330898284912d, +1.2940962314605713d, +1.2953605651855469d, +1.296626091003418d, +1.2978930473327637d, +1.2991611957550049d, +1.3004305362701416d, +1.3017010688781738d, +1.3029727935791016d, +1.304245948791504d, +1.3055200576782227d, +1.3067958354949951d, +1.308072566986084d, +1.3093504905700684d, +1.3106298446655273d, +1.3119103908538818d, +1.3131921291351318d, +1.3144752979278564d, +1.3157594203948975d, +1.317044973373413d, +1.3183319568634033d, +1.31961989402771d, +1.3209092617034912d, +1.322199821472168d, +1.3234915733337402d, +1.324784755706787d, +1.3260791301727295d, +1.3273746967315674d, +1.3286716938018799d, +1.329969882965088d, +1.3312692642211914d, +1.3325698375701904d, +1.333871841430664d, +1.3351752758026123d, +1.336479663848877d, +1.3377854824066162d, +1.339092493057251d, +1.3404009342193604d, +1.3417105674743652d, +1.3430213928222656d, +1.3443336486816406d, +1.3456470966339111d, +1.3469617366790771d, +1.3482778072357178d, +1.349595069885254d, +1.3509137630462646d, +1.352233648300171d, +1.3535549640655518d, +1.3548774719238281d, +1.356201171875d, +1.3575263023376465d, +1.3588526248931885d, +1.360180139541626d, +1.361509084701538d, +1.3628394603729248d, +1.364171028137207d, +1.3655037879943848d, +1.366837978363037d, +1.368173360824585d, +1.3695101737976074d, +1.3708481788635254d, +1.372187614440918d, +1.373528242111206d, +1.3748703002929688d, +1.376213550567627d, +1.3775582313537598d, +1.378904104232788d, +1.380251407623291d, +1.3815999031066895d, +1.3829498291015625d, +1.384300947189331d, +1.3856534957885742d, +1.387007236480713d, +1.3883624076843262d, +1.389719009399414d, +1.3910768032073975d, +1.3924360275268555d, +1.393796443939209d, +1.395158290863037d, +1.3965213298797607d, +1.397885799407959d, +1.3992514610290527d, +1.4006187915802002d, +1.401987075805664d, +1.4033570289611816d, +1.4047281742095947d, +1.4061005115509033d, +1.4074742794036865d, +1.4088494777679443d, +1.4102261066436768d, +1.4116039276123047d, +1.4129831790924072d, +1.4143636226654053d, +1.415745496749878d, +1.4171288013458252d, +1.418513298034668d, +1.4198992252349854d, +1.4212865829467773d, +1.4226751327514648d, +1.424065351486206d, +1.4254565238952637d, +1.426849365234375d, +1.4282433986663818d, +1.4296388626098633d, +1.4310357570648193d, +1.432433843612671d, +1.433833360671997d, +1.4352343082427979d, +1.4366366863250732d, +1.4380402565002441d, +1.4394452571868896d, +1.4408516883850098d, +1.4422595500946045d, +1.4436686038970947d, +1.4450790882110596d, +1.446491003036499d, +1.447904348373413d, +1.4493188858032227d, +1.450735092163086d, +1.4521524906158447d, +1.4535713195800781d, +1.454991340637207d, +1.4564130306243896d, +1.4578359127044678d, +1.4592602252960205d, +1.460686206817627d, +1.4621131420135498d, +1.4635417461395264d, +1.4649717807769775d, +1.4664030075073242d, +1.4678359031677246d, +1.4692699909210205d, +1.470705509185791d, +1.4721424579620361d, +1.4735808372497559d, +1.475020408630371d, +1.47646164894104d, +1.4779040813446045d, +1.4793481826782227d, +1.4807934761047363d, +1.4822404384613037d, +1.4836885929107666d, +1.485138177871704d, +1.4865891933441162d, +1.488041639328003d, +1.4894955158233643d, +1.4909508228302002d, +1.4924075603485107d, +1.493865728378296d, +1.4953253269195557d, +1.49678635597229d, +1.49824857711792d, +1.4997124671936035d, +1.5011777877807617d, +1.5026445388793945d, +1.504112720489502d, +1.505582332611084d, +1.5070531368255615d, +1.5085256099700928d, +1.5099995136260986d, +1.511474847793579d, +1.5129516124725342d, +1.5144298076629639d, +1.5159096717834473d, +1.5173907279968262d, +1.5188732147216797d, +1.5203571319580078d, +1.5218427181243896d, +1.523329496383667d, +1.524817943572998d, +1.5263078212738037d, +1.5277988910675049d, +1.5292916297912598d, +1.5307857990264893d, +1.5322813987731934d, +1.5337786674499512d, +1.5352771282196045d, +1.5367772579193115d, +1.538278579711914d, +1.5397815704345703d, +1.5412859916687012d, +1.5427920818328857d, +1.5442993640899658d, +1.5458080768585205d, +1.547318458557129d, +1.548830270767212d, +1.5503435134887695d, +1.5518584251403809d, +1.5533745288848877d, +1.5548923015594482d, +1.5564115047454834d, +1.5579321384429932d, +1.5594542026519775d, +1.5609779357910156d, +1.5625030994415283d, +1.5640296936035156d, +1.5655577182769775d, +1.5670874118804932d, +1.5686185359954834d, +1.5701510906219482d, +1.5716853141784668d, +1.5732207298278809d, +1.5747578144073486d, +1.5762965679168701d, +1.577836513519287d, +1.5793781280517578d, +1.5809214115142822d, +1.5824658870697021d, +1.5840120315551758d, +1.5855598449707031d, +1.587108850479126d, +1.5886595249176025d, +1.5902118682861328d, +1.5917654037475586d, +1.593320608139038d, +1.5948774814605713d, +1.596435785293579d, +1.5979955196380615d, +1.5995566844940186d, +1.6011195182800293d, +1.6026840209960938d, +1.6042497158050537d, +1.6058173179626465d, +1.6073861122131348d, +1.6089565753936768d, +1.6105287075042725d, +1.6121022701263428d, +1.6136772632598877d, +1.6152539253234863d, +1.6168320178985596d, +1.6184117794036865d, +1.619992971420288d, +1.6215758323669434d, +1.6231601238250732d, +1.6247460842132568d, +1.626333475112915d, +1.627922534942627d, +1.6295130252838135d, +1.6311051845550537d, +1.6326987743377686d, +1.634294033050537d, +1.6358907222747803d, +1.6374890804290771d, +1.6390891075134277d, +1.640690565109253d, +1.6422934532165527d, +1.6438980102539062d, +1.6455042362213135d, +1.6471118927001953d, +1.6487212181091309d, +1.6503322124481201d, +1.651944637298584d, +1.6535584926605225d, +1.6551742553710938d, +1.6567914485931396d, +1.6584100723266602d, +1.6600303649902344d, +1.6616523265838623d, +1.663275957107544d, +1.6649010181427002d, +1.666527509689331d, +1.6681559085845947d, +1.669785737991333d, +1.671417236328125d, +1.6730501651763916d, +1.674684762954712d, +1.676321029663086d, +1.6779589653015137d, +1.679598331451416d, +1.681239366531372d, +1.6828820705413818d, +1.6845262050628662d, +1.6861720085144043d, +1.687819480895996d, +1.6894686222076416d, +1.6911191940307617d, +1.6927716732025146d, +1.6944255828857422d, +1.6960809230804443d, +1.6977381706237793d, +1.6993968486785889d, +1.7010571956634521d, +1.7027192115783691d, +1.7043828964233398d, +1.7060482501983643d, +1.7077150344848633d, +1.709383487701416d, +1.7110536098480225d, +1.7127254009246826d, +1.7143988609313965d, +1.716073989868164d, +1.7177505493164062d, +1.7194287776947021d, +1.7211089134216309d, +1.7227904796600342d, +1.7244737148284912d, +1.726158618927002d, +1.7278449535369873d, +1.7295331954956055d, +1.7312231063842773d, +1.7329144477844238d, +1.7346076965332031d, +1.736302375793457d, +1.7379989624023438d, +1.739696979522705d, +1.7413966655731201d, +1.7430980205535889d, +1.7448012828826904d, +1.7465059757232666d, +1.7482123374938965d, +1.74992036819458d, +1.7516300678253174d, +1.7533416748046875d, +1.7550547122955322d, +1.7567694187164307d, +1.7584857940673828d, +1.7602040767669678d, +1.7619237899780273d, +1.7636451721191406d, +1.7653684616088867d, +1.7670931816101074d, +1.768819808959961d, +1.770547866821289d, +1.77227783203125d, +1.7740094661712646d, +1.775742769241333d, +1.777477741241455d, +1.7792143821716309d, +1.7809526920318604d, +1.7826926708221436d, +1.7844345569610596d, +1.7861778736114502d, +1.7879230976104736d, +1.7896699905395508d, +1.7914185523986816d, +1.7931687831878662d, +1.7949209213256836d, +1.7966744899749756d, +1.7984299659729004d, +1.800187110900879d, +1.8019459247589111d, +1.8037066459655762d, +1.8054687976837158d, +1.8072328567504883d, +1.8089985847473145d, +1.8107659816741943d, +1.812535285949707d, +1.8143062591552734d, +1.8160789012908936d, +1.8178532123565674d, +1.819629430770874d, +1.8214070796966553d, +1.8231868743896484d, +1.8249680995941162d, +1.8267512321472168d, +1.828536033630371d, +1.830322504043579d, +1.83211088180542d, +1.8339009284973145d, +1.8356926441192627d, +1.8374862670898438d, +1.8392815589904785d, +1.841078519821167d, +1.8428773880004883d, +1.8446779251098633d, +1.846480131149292d, +1.8482842445373535d, +1.8500902652740479d, +1.8518977165222168d, +1.8537070751190186d, +1.8555183410644531d, +1.8573312759399414d, +1.8591458797454834d, +1.8609623908996582d, +1.8627805709838867d, +1.864600658416748d, +1.866422414779663d, +1.8682458400726318d, +1.8700714111328125d, +1.8718984127044678d, +1.8737273216247559d, +1.8755581378936768d, +1.8773906230926514d, +1.8792247772216797d, +1.8810608386993408d, +1.8828988075256348d, +1.8847384452819824d, +1.886579990386963d, +1.888423204421997d, +1.890268325805664d, +1.8921151161193848d, +1.8939638137817383d, +1.8958141803741455d, +1.8976664543151855d, +1.8995206356048584d, +1.901376485824585d, +1.9032342433929443d, +1.9050939083099365d, +1.9069552421569824d, +1.908818244934082d, +1.9106833934783936d, +1.9125502109527588d, +1.9144186973571777d, +1.9162893295288086d, +1.9181616306304932d, +1.9200356006622314d, +1.9219114780426025d, +1.9237892627716064d, +1.9256689548492432d, +1.9275505542755127d, +1.929433822631836d, +1.931318759918213d, +1.9332058429718018d, +1.9350945949554443d, +1.9369852542877197d, +1.938877820968628d, +1.940772294998169d, +1.9426684379577637d, +1.9445664882659912d, +1.9464664459228516d, +1.9483680725097656d, +1.9502718448638916d, +1.9521772861480713d, +1.9540846347808838d, +1.955993890762329d, +1.9579050540924072d, +1.959817886352539d, +1.9617326259613037d, +1.9636495113372803d, +1.9655680656433105d, +1.9674885272979736d, +1.9694106578826904d, +1.9713349342346191d, +1.9732608795166016d, +1.975188970565796d, +1.977118730545044d, +1.9790503978729248d, +1.9809842109680176d, +1.982919692993164d, +1.9848570823669434d, +1.9867963790893555d, +1.9887375831604004d, +1.990680456161499d, +1.9926254749298096d, +1.994572401046753d, +1.996521234512329d, +1.998471736907959d, +2.000424385070801d, +2.0023789405822754d, +2.004335403442383d, +2.006293773651123d, +2.008254051208496d, +2.010216236114502d, +2.0121798515319824d, +2.014145851135254d, +2.016113758087158d, +2.0180835723876953d, +2.0200552940368652d, +2.022029399871826d, +2.0240049362182617d, +2.02598237991333d, +2.0279617309570312d, +2.0299429893493652d, +2.0319266319274902d, +2.03391170501709d, +2.0358991622924805d, +2.0378880500793457d, +2.039879322052002d, +2.041872501373291d, +2.0438671112060547d, +2.0458641052246094d, +2.047863006591797d, +2.049863815307617d, +2.0518670082092285d, +2.0538716316223145d, +2.055878162384033d, +2.057887077331543d, +2.0598974227905273d, +2.0619101524353027d, +2.063924789428711d, +2.065941333770752d, +2.067959785461426d, +2.0699801445007324d, +2.07200288772583d, +2.0740270614624023d, +2.0760536193847656d, +2.0780820846557617d, +2.0801124572753906d, +2.0821447372436523d, +2.084178924560547d, +2.0862154960632324d, +2.0882534980773926d, +2.0902938842773438d, +2.0923361778259277d, +2.0943803787231445d, +2.0964269638061523d, +2.0984749794006348d, +2.100525379180908d, +2.1025776863098145d, +2.1046319007873535d, +2.1066884994506836d, +2.1087465286254883d, +2.110806941986084d, +2.1128692626953125d, +2.114933490753174d, +2.117000102996826d, +2.1190686225891113d, +2.1211390495300293d, +2.12321138381958d, +2.1252856254577637d, +2.1273622512817383d, +2.1294407844543457d, +2.131521224975586d, +2.133604049682617d, +2.135688304901123d, +2.13777494430542d, +2.139863967895508d, +2.1419544219970703d, +2.144047260284424d, +2.14614200592041d, +2.1482391357421875d, +2.1503376960754395d, +2.1524391174316406d, +2.1545419692993164d, +2.156647205352783d, +2.1587538719177246d, +2.1608633995056152d, +2.1629743576049805d, +2.1650876998901367d, +2.167203426361084d, +2.169320583343506d, +2.1714401245117188d, +2.1735615730285645d, +2.175685405731201d, +2.1778111457824707d, +2.179938793182373d, +2.1820688247680664d, +2.1842007637023926d, +2.1863350868225098d, +2.1884708404541016d, +2.1906094551086426d, +2.192749500274658d, +2.194891929626465d, +2.1970362663269043d, +2.1991829872131348d, +2.201331615447998d, +2.2034826278686523d, +2.2056355476379395d, +2.2077903747558594d, +2.2099475860595703d, +2.212106704711914d, +2.214268207550049d, +2.2164316177368164d, +2.218596935272217d, +2.220764636993408d, +2.2229342460632324d, +2.2251062393188477d, +2.2272801399230957d, +2.2294564247131348d, +2.2316346168518066d, +2.2338151931762695d, +2.2359976768493652d, +2.2381820678710938d, +2.2403693199157715d, +2.242558002471924d, +2.244749069213867d, +2.2469425201416016d, +2.2491378784179688d, +2.2513351440429688d, +2.2535347938537598d, +2.2557363510131836d, +2.2579402923583984d, +2.2601466178894043d, +2.262354850769043d, +2.2645654678344727d, +2.266777992248535d, +2.2689924240112305d, +2.271209716796875d, +2.273428440093994d, +2.2756495475769043d, +2.2778730392456055d, +2.2800989151000977d, +2.2823266983032227d, +2.2845563888549805d, +2.2867884635925293d, +2.289022922515869d, +2.291259288787842d, +2.2934980392456055d, +2.295738697052002d, +2.2979817390441895d, +2.300227165222168d, +2.3024744987487793d, +2.3047242164611816d, +2.306975841522217d, +2.309229850769043d, +2.31148624420166d, +2.31374454498291d, +2.316005229949951d, +2.318267822265625d, +2.32053279876709d, +2.3228001594543457d, +2.3250694274902344d, +2.3273415565490723d, +2.3296151161193848d, +2.3318915367126465d, +2.334169864654541d, +2.3364500999450684d, +2.338733196258545d, +2.3410181999206543d, +2.3433055877685547d, +2.345594882965088d, +2.347886562347412d, +2.3501806259155273d, +2.3524770736694336d, +2.3547754287719727d, +2.3570761680603027d, +2.3593788146972656d, +2.3616843223571777d, +2.3639917373657227d, +2.3663015365600586d, +2.3686132431030273d, +2.370927333831787d, +2.373243808746338d, +2.3755626678466797d, +2.3778839111328125d, +2.380207061767578d, +2.3825325965881348d, +2.3848605155944824d, +2.387190818786621d, +2.3895230293273926d, +2.391857624053955d, +2.3941946029663086d, +2.396533966064453d, +2.3988752365112305d, +2.401218891143799d, +2.4035654067993164d, +2.4059133529663086d, +2.40826416015625d, +2.4106173515319824d, +2.4129724502563477d, +2.415329933166504d, +2.417689800262451d, +2.4200520515441895d, +2.4224166870117188d, +2.424783229827881d, +2.427152633666992d, +2.4295239448547363d, +2.4318976402282715d, +2.4342737197875977d, +2.436652183532715d, +2.439032554626465d, +2.441415786743164d, +2.4438014030456543d, +2.4461889266967773d, +2.4485788345336914d, +2.4509711265563965d, +2.4533658027648926d, +2.4557628631591797d, +2.458162307739258d, +2.460564136505127d, +2.462968349456787d, +2.46537446975708d, +2.4677834510803223d, +2.4701943397521973d, +2.4726080894470215d, +2.4750237464904785d, +2.4774417877197266d, +2.479862689971924d, +2.482285499572754d, +2.484710693359375d, +2.487138271331787d, +2.4895682334899902d, +2.4920010566711426d, +2.4944357872009277d, +2.496872901916504d, +2.499312400817871d, +2.5017542839050293d, +2.5041985511779785d, +2.5066452026367188d, +2.50909423828125d, +2.5115456581115723d, +2.5139999389648438d, +2.516456127166748d, +2.5189146995544434d, +2.5213756561279297d, +2.5238394737243652d, +2.5263051986694336d, +2.528773307800293d, +2.5312442779541016d, +2.533717155456543d, +2.5361928939819336d, +2.538670539855957d, +2.5411510467529297d, +2.5436339378356934d, +2.546119213104248d, +2.5486068725585938d, +2.5510969161987305d, +2.553589344024658d, +2.556084632873535d, +2.558581829071045d, +2.5610814094543457d, +2.5635838508605957d, +2.5660886764526367d, +2.5685958862304688d, +2.571105480194092d, +2.573617458343506d, +2.576131820678711d, +2.5786490440368652d, +2.5811686515808105d, +2.5836901664733887d, +2.586214542388916d, +2.5887417793273926d, +2.591270923614502d, +2.5938024520874023d, +2.596336841583252d, +2.5988736152648926d, +2.601412773132324d, +2.603954315185547d, +2.6064987182617188d, +2.6090455055236816d, +2.6115946769714355d, +2.6141462326049805d, +2.6167001724243164d, +2.6192569732666016d, +2.6218161582946777d, +2.624377727508545d, +2.626941680908203d, +2.6295084953308105d, +2.632077217102051d, +2.6346492767333984d, +2.637223243713379d, +2.6398000717163086d, +2.6423792839050293d, +2.644960880279541d, +2.6475448608398438d, +2.6501317024230957d, +2.6527209281921387d, +2.655313014984131d, +2.657907009124756d, +2.6605043411254883d, +2.6631035804748535d, +2.665705680847168d, +2.6683101654052734d, +2.67091703414917d, +2.6735267639160156d, +2.6761388778686523d, +2.67875337600708d, +2.681370735168457d, +2.683990478515625d, +2.686613082885742d, +2.689237594604492d, +2.6918654441833496d, +2.69449520111084d, +2.6971278190612793d, +2.699763298034668d, +2.7024011611938477d, +2.7050414085388184d, +2.70768404006958d, +2.710329532623291d, +2.712977886199951d, +2.7156286239624023d, +2.7182817459106445d, }; /** Exponential over the range of 0 - 1 in increments of 2^-10 * exp(x/1024) = expFracTableA[x] + expFracTableB[x]. */ private static final double[] EXP_FRAC_B = new double[] { +0.0d, +1.552583321178453E-10d, +1.2423699995465188E-9d, +4.194022929828008E-9d, +9.94381632344361E-9d, +1.9426261544163577E-8d, +3.3576783010266685E-8d, +5.3331719086630523E-8d, +7.962832297769345E-8d, +1.1340476362128895E-7d, -8.281845251820919E-8d, -3.126416414805498E-8d, +3.058997113995161E-8d, +1.0368579417304741E-7d, -4.9452513107409435E-8d, +4.8955889659397494E-8d, -7.698155155722897E-8d, +5.051784853384516E-8d, -4.443661736519001E-8d, +1.1593958457401774E-7d, +5.575759739697068E-8d, +1.4385227981629147E-8d, -7.227368462584163E-9d, -8.129108387083023E-9d, +1.263202100290635E-8d, +5.600896265625552E-8d, -1.154629885168314E-7d, -2.399186832888246E-8d, +9.295948298604103E-8d, -2.070841011504222E-9d, -6.97066538508643E-8d, -1.0898941254272996E-7d, -1.1895963756343625E-7d, -9.865691193993138E-8d, -4.711988033385175E-8d, +3.6613751875298095E-8d, -8.491135959370133E-8d, +6.610611940107793E-8d, +1.3794148633283659E-8d, -2.462631860370667E-9d, +1.830278273495162E-8d, +7.705834203598065E-8d, -6.364563771711373E-8d, +7.39978436695387E-8d, +1.4122417557484554E-8d, -3.881598887298574E-9d, +2.0958481826069642E-8d, +8.96162975425619E-8d, -3.535214171178576E-8d, -1.1455271549574576E-7d, +9.140964977432485E-8d, +1.0667524445105459E-7d, -6.777752790396222E-8d, +4.586785041291296E-8d, -2.8245462428022094E-8d, -5.071761314397018E-8d, -2.0566368810068663E-8d, +6.319146317890346E-8d, -3.687854305539139E-8d, -8.137269363160008E-8d, -6.930491127388755E-8d, +3.1184473002226595E-10d, -1.0995299963140049E-7d, +7.772668425499348E-8d, +8.750367485925089E-8d, -7.963112393823186E-8d, +5.415131809829094E-8d, +1.3006683896462346E-8d, +3.634736373360733E-8d, -1.132504393233074E-7d, +4.2046187038837375E-8d, +2.6396811618001066E-8d, +7.92177143584738E-8d, -3.691100820545433E-8d, -8.257112559083188E-8d, -5.676200971739166E-8d, +4.151794514828518E-8d, -2.5147255753587636E-8d, -1.7335469415174996E-8d, +6.595784859136531E-8d, -1.2680354928109105E-8d, -1.3824992526093461E-8d, +6.353142754175797E-8d, -1.8021197722549054E-8d, -1.9054827792903468E-8d, +6.144098503892116E-8d, -1.3940903373095247E-8d, -5.7694907599522404E-9d, +8.696863522320578E-8d, +2.6869297963554945E-8d, +5.3366470162689076E-8d, -7.094204160127543E-8d, -1.0662027949814858E-7d, -5.26498707801063E-8d, +9.198855229106814E-8d, +8.989677431456647E-8d, -5.790384407322479E-8d, -1.1197236522467887E-7d, -7.12854317090566E-8d, +6.51813137650059E-8d, +6.003465022483798E-8d, -8.569906238528267E-8d, +1.0584469687624562E-7d, -7.956144278281947E-8d, +7.43676272093501E-8d, +9.182512565315022E-8d, -2.6157563728873715E-8d, -4.012947040998503E-8d, +5.094280572218447E-8d, +9.675095351161728E-9d, +7.552139802281006E-8d, +1.1099566726533146E-8d, +5.58656252899437E-8d, -2.756054703800197E-8d, +2.791018095971047E-10d, -9.799351869734466E-8d, -8.291832428736212E-8d, +4.654720780112994E-8d, +5.302803981406403E-8d, -6.243126731995636E-8d, -6.036655299348577E-8d, +6.026878587378257E-8d, +6.210379583313526E-8d, -5.381287389094251E-8d, -4.8012970400697E-8d, +8.055420567281602E-8d, +9.452180117175641E-8d, -5.057430382371206E-9d, +2.1288872215266507E-8d, -6.380305844689076E-8d, -2.0858800984600168E-8d, -8.724006061713588E-8d, -2.3470351753125604E-8d, -6.690931338790221E-8d, +2.192160831263035E-8d, +5.6648446166177225E-9d, -1.1461755745719884E-7d, -9.944393412663547E-8d, +5.2249837964645906E-8d, +1.0311034276196487E-7d, +5.4203784018566126E-8d, -9.340259278913173E-8d, -1.0022192034216903E-7d, +3.481513333662908E-8d, +7.436036590244714E-8d, +1.9485199912395296E-8d, +1.0968068384729757E-7d, +1.0760175582979094E-7d, +1.4322981952798675E-8d, +6.933855730431659E-8d, +3.530656968851287E-8d, -8.669526204279467E-8d, -5.7169586962345785E-8d, -1.1345515834332824E-7d, -1.605251622332555E-8d, -2.298302779758532E-9d, -7.110952399338234E-8d, +1.70164513845372E-8d, +2.4746155561368937E-8d, -4.6834239957353325E-8d, +4.1781076667923185E-8d, +5.326182134294869E-8d, -1.1302647617762544E-8d, +8.759667154796094E-8d, +1.126326877851684E-7d, +6.48979555673987E-8d, -5.451390316294111E-8d, -6.0896188500539086E-9d, -2.7152010585461855E-8d, -1.1660424775832058E-7d, -3.492984900939992E-8d, -1.944841848873016E-8d, -6.905990750285027E-8d, +5.575538653428039E-8d, +1.1768108384670781E-7d, +1.178204606523101E-7d, +5.727787111340131E-8d, -6.284125161007433E-8d, -3.0118152047565877E-9d, -5.448044533034374E-10d, -5.433154287341921E-8d, +7.515630833946181E-8d, -8.780756503572527E-8d, -6.527407547535494E-8d, -9.45487863616303E-8d, +6.390098458668406E-8d, -6.564672913105876E-8d, -5.238488022920792E-9d, +7.824500749252316E-9d, -2.5339299158309795E-8d, -1.036103313062145E-7d, +1.2550633697348567E-8d, +8.584676196065558E-8d, +1.1740089468291563E-7d, +1.0833697012353316E-7d, +5.978002467397905E-8d, -2.7143806069290897E-8d, +8.711129287069315E-8d, -7.316349947981893E-8d, -3.00015852582934E-8d, -2.0691000399732483E-8d, -4.4100097152254264E-8d, -9.909612209943178E-8d, +5.38733640215475E-8d, -6.0893829005035E-8d, +3.457553391989844E-8d, +1.0300006058273187E-7d, -9.290053015365092E-8d, -7.514966995961323E-8d, -8.10254145615142E-8d, -1.0938612624777085E-7d, +7.932952721989251E-8d, +9.428257290008738E-9d, -7.952636967837795E-8d, +5.203033137154554E-8d, -7.159157201731446E-8d, +2.7593424989059015E-8d, +1.1231621190000476E-7d, -5.469119869891027E-8d, +4.560067256086347E-9d, +5.280427179595944E-8d, +9.119538242455128E-8d, -1.1753008498403413E-7d, -9.537874867759656E-8d, -7.96118345325538E-8d, -6.907085854395348E-8d, -6.259620482221904E-8d, -5.902712448725381E-8d, -5.720173456146447E-8d, -5.5957016861703E-8d, -5.412881689012608E-8d, -5.0551842723970724E-8d, -4.405966390424518E-8d, -3.348471032333413E-8d, -1.7658271111516935E-8d, +4.589506477601956E-9d, +3.4429618182751655E-8d, +7.303420385174346E-8d, -1.168420305422519E-7d, -5.718749537552229E-8d, +1.4754809136835937E-8d, +1.001616104682875E-7d, -3.8207793300052055E-8d, +7.766278405014509E-8d, -2.7883635712109803E-8d, -1.1524714043067699E-7d, +5.517333625963128E-8d, +7.724278756071081E-9d, -1.7990934773848504E-8d, -2.0786347668702902E-8d, +5.251554594269693E-10d, +4.7131849857076246E-8d, -1.1819540733893871E-7d, -1.742885956093543E-8d, +1.1220467571570283E-7d, +3.347954541376715E-8d, -1.399157980498908E-8d, -2.9013441705763093E-8d, -1.0389614239253089E-8d, +4.307749759934266E-8d, -1.0583192018912101E-7d, +2.0919226941745448E-8d, -5.2305110482722706E-8d, -8.588407110184028E-8d, -7.861419797923639E-8d, -2.929085835358592E-8d, +6.329175751021792E-8d, -3.807794163054899E-8d, -9.377320954068088E-8d, -1.0258469865953145E-7d, -6.330187984612758E-8d, +2.5286958775281306E-8d, -7.40238661307607E-8d, +1.1681688445204168E-7d, -1.1623125976292733E-7d, -5.6696107089038004E-8d, +5.822140627806124E-8d, -8.678466172071259E-9d, -1.7757121899175995E-8d, +3.220665454652531E-8d, -9.598330731102836E-8d, +7.573375369829243E-8d, +7.174547784678893E-8d, -1.0672213971363184E-7d, +1.8395252217743006E-8d, -2.8511112548600118E-8d, -7.79306270997787E-9d, +8.178019529487065E-8d, +3.0220784595602374E-9d, -4.4156343103298585E-9d, +6.07014616741277E-8d, -3.8809601937571554E-8d, -6.329342805230603E-8d, -1.1511990258493999E-8d, +1.177739474561431E-7d, +8.738625278484571E-8d, -1.0143341551207646E-7d, +2.9394972678456236E-8d, +4.278345398213486E-9d, +6.28805835150457E-8d, -3.197037359731606E-8d, -4.060821046423735E-8d, +3.82160283750664E-8d, -3.2666060441373307E-8d, -1.3584500601329896E-8d, +9.671332777035621E-8d, +6.10626893063691E-8d, +1.1913723189736356E-7d, +3.3774671482641995E-8d, +4.4651109654500895E-8d, -8.539328154875224E-8d, -1.166799420361101E-7d, -4.794765976694151E-8d, -1.1635256954820579E-7d, -8.221241452580445E-8d, +5.5737717715868425E-8d, +6.034539636024073E-8d, -6.712199323081945E-8d, -8.697724830833087E-8d, +2.0494942705297694E-9d, -3.718924074653624E-8d, +3.499747150995707E-8d, -1.8535359161566028E-8d, +4.1905679587096103E-8d, -2.0821912536551675E-8d, +3.297776915751238E-8d, -3.3835280846270374E-8d, +1.8437339356553904E-8d, -4.734187609526424E-8d, +8.527976799299225E-9d, -5.1088103279787804E-8d, +1.3513294656751725E-8d, -3.480032127343472E-8d, +4.367697180842916E-8d, +1.1815196363705356E-8d, +1.0932279207149782E-7d, +9.907230065250944E-8d, -1.764389559496152E-8d, -1.1135725625095859E-9d, -8.846040040259342E-8d, -3.996962588736431E-8d, -9.276238757878814E-8d, -7.12139818505956E-9d, -2.016525972830718E-8d, +1.0782585410141121E-7d, -9.868269632073771E-8d, +7.686861750031585E-8d, -7.947087669425045E-8d, -8.955768055535647E-8d, +4.791582240886607E-8d, +9.583994718167641E-8d, +5.5524866689108584E-8d, -7.171796605211277E-8d, -4.6157237582310713E-8d, -1.0489751005162237E-7d, -8.204903560604627E-9d, +6.818588687884566E-9d, -5.850916105103205E-8d, +3.5549586192569994E-8d, +5.1896700056778354E-8d, -8.146080588190463E-9d, +9.516285362051742E-8d, -1.1368933260611668E-7d, +8.187871486648885E-8d, -3.206182925646474E-8d, +2.265440168347286E-8d, +8.938334752179552E-9d, -7.187922490287331E-8d, +1.9952407216533937E-8d, +4.734805892507655E-8d, +1.1642439930208906E-8d, -8.582843599651953E-8d, -5.3086706437795354E-9d, +1.6121782610217253E-8d, -2.0197142620980974E-8d, -1.129242035557684E-7d, -2.2298267863810133E-8d, +1.4605950309628873E-8d, -8.663710700190489E-10d, -6.736873974532501E-8d, +5.486523121881414E-8d, -1.0965249168570443E-7d, -8.27343074126263E-8d, -1.0144703278439455E-7d, +7.39809943048038E-8d, -3.193297932837415E-8d, +5.900393284617182E-8d, +1.0973020465397083E-7d, -1.1681436418514489E-7d, +9.5985669644661E-8d, +3.423560333632085E-8d, -6.22836197265283E-8d, +4.621027492345726E-8d, -1.1575484316683829E-7d, -6.997545435826076E-8d, -5.3502441327259514E-8d, -6.49667713553005E-8d, -1.029980741248172E-7d, +7.219393868923887E-8d, -1.4854841678687828E-8d, +1.1406713393562271E-7d, -1.650155887561251E-8d, +7.165331603232264E-8d, -9.692697614257269E-8d, -4.402550702194912E-8d, -6.679737442193143E-9d, +1.6492800268960003E-8d, +2.68759245092879E-8d, +2.5854805721793077E-8d, +1.4815967715704613E-8d, -4.852711011229633E-9d, -3.176199594915881E-8d, -6.452129525125173E-8d, -1.01738658407525E-7d, +9.639780418418697E-8d, +5.4445606140746644E-8d, +1.2219361033150988E-8d, -2.8883532688356087E-8d, -6.746431126005811E-8d, -1.0212284427080097E-7d, +1.0696094577483825E-7d, +8.43527683868743E-8d, +6.987544103716777E-8d, +6.493457409236137E-8d, +7.093715125593688E-8d, +8.929153091001965E-8d, -1.1701113164306871E-7d, -6.972256643013266E-8d, -5.848862070736576E-9d, +7.602385197610123E-8d, -6.110775144284437E-8d, +6.101012058093429E-8d, -3.304167134225169E-8d, -1.0342514383702196E-7d, +8.969907328603505E-8d, +7.091600108064668E-8d, +8.006778743052707E-8d, +1.1857939200074815E-7d, -5.0541412403312774E-8d, +5.0970277930552287E-8d, -5.229355472795119E-8d, +1.1793478462381443E-7d, +8.625007227318527E-8d, +9.250422086873268E-8d, -1.0028661472061573E-7d, -1.384914052949463E-8d, +1.1483560326413004E-7d, +4.878798101459259E-8d, +2.7866921183936055E-8d, +5.3514180410849046E-8d, -1.1124565511436785E-7d, +1.186914813275767E-8d, -5.253258132241335E-8d, -6.458486486369316E-8d, -2.2838888809969377E-8d, +7.415557606805398E-8d, -1.0568403170659571E-8d, -3.7139182948393606E-8d, -4.1022790876160215E-9d, +8.999821367768787E-8d, +8.201043988912348E-9d, -9.616457442665051E-9d, +3.8005886250603055E-8d, -8.588890051473289E-8d, +9.699937202692456E-8d, +1.11298006674538E-7d, -4.1527104733570825E-8d, +1.1682852007826251E-7d, +1.1099648061301941E-7d, -5.755303038890997E-8d, +8.948877445235827E-8d, +7.675780395028194E-8d, -9.427143563390596E-8d, +5.471416081500162E-8d, +4.8354824064383506E-8d, -1.118706134478866E-7d, +5.235528379688445E-8d, +6.567708120053687E-8d, -7.042204992948526E-8d, -1.1603891006723397E-7d, -6.968742825553785E-8d, +7.01199184127881E-8d, +6.645352711199266E-8d, -7.919617109348822E-8d, +1.1149986927391714E-7d, -7.522074418324674E-8d, +7.739252980388984E-8d, +9.39987974788905E-8d, -2.390421480210064E-8d, -3.639873824357815E-8d, +5.8015881615938497E-8d, +2.2423186335040668E-8d, +9.674534330665206E-8d, +4.4068830785712375E-8d, +1.0431875573076199E-7d, +4.0584538834428926E-8d, +9.279423236781974E-8d, +2.404020521381534E-8d, +7.425346071427343E-8d, +6.529321706138789E-9d, +6.080174837146273E-8d, +1.6902327633329284E-10d, +6.456806922371733E-8d, +1.7100134295216033E-8d, +9.770510970673519E-8d, +6.94872148530716E-8d, -6.602926393514549E-8d, -6.889997193778161E-8d, +6.240235720677117E-8d, +9.098790295810902E-8d, +1.8386917534879182E-8d, +8.454972737414241E-8d, +5.259099728747365E-8d, -7.595453077213505E-8d, -6.113203624663034E-8d, +9.859622328905143E-8d, -7.206766550807255E-8d, -9.474579567171831E-8d, +3.210408693366267E-8d, +7.160716418525417E-8d, +2.530870537724554E-8d, -1.0524451040704701E-7d, -8.008561371849434E-8d, +1.0233519853128553E-7d, -3.326791455362767E-8d, -8.504961764629757E-9d, -6.024017201863256E-8d, +5.1500902632092514E-8d, +8.98570720774568E-8d, +5.638724693948384E-8d, -4.734813904255994E-8d, +1.8631451577542948E-8d, +1.7470924137873214E-8d, -4.926470933588261E-8d, +5.84096713620797E-8d, +1.0364355880696472E-7d, +8.800655674349468E-8d, +1.3069802481237792E-8d, +1.1882454749452428E-7d, -6.999215748398631E-8d, -7.49674072510849E-8d, +1.054760847603618E-7d, -3.920012014371067E-9d, +7.526183084319617E-8d, +1.0618494853096868E-7d, +9.043280094115832E-8d, +2.9590395068826316E-8d, -7.475571347653619E-8d, +1.7401160143611842E-8d, +6.923209420670962E-8d, +8.232829924979753E-8d, +5.82825404854514E-8d, -1.3108606792380822E-9d, -9.485602512220194E-8d, +1.7663064617118723E-8d, +9.942682855652123E-8d, -8.638275100090915E-8d, -6.132639063569726E-8d, -6.221897889344726E-8d, -8.745525834919404E-8d, +1.029901759234897E-7d, +3.3888561478632076E-8d, -5.47315553588771E-8d, +7.715994473741065E-8d, -4.566098167230033E-8d, +5.5257514455273825E-8d, -9.530545662611411E-8d, -1.889488909834863E-8d, +4.769006625301079E-8d, +1.0607041998938709E-7d, -8.054981263802322E-8d, -3.370929373457322E-8d, +9.799164177397836E-9d, +5.160291611526656E-8d, +9.333090708652975E-8d, -1.0180490545927503E-7d, -5.533523366931846E-8d, -4.044932340334176E-9d, +5.370131904567218E-8d, -1.1887814032213867E-7d, -4.3307634616102625E-8d, +4.363437558318513E-8d, -9.482896784430338E-8d, +1.9782818312325887E-8d, -8.77224935488516E-8d, +6.113879253864931E-8d, -8.822335132515693E-9d, -5.753754066078771E-8d, -8.335545536862392E-8d, -8.462309712606694E-8d, -5.968586877433824E-8d, -6.887556547891059E-9d, +7.542967150507818E-8d, -4.949331199790077E-8d, +9.684172421525468E-8d, +3.9260317944365246E-8d, +1.784536881359796E-8d, +3.426282345243592E-8d, +9.018025618601154E-8d, -5.1151708476133135E-8d, +8.877492215808044E-8d, +3.479545684576179E-8d, +2.7002575714977818E-8d, +6.707201545505014E-8d, -8.173742908533777E-8d, +5.909041310777802E-8d, +1.439903710393587E-8d, +2.4289317341982113E-8d, +9.044519282818302E-8d, -2.3866331257845713E-8d, -7.853944465095286E-8d, -7.188526769607005E-8d, -2.2132706360079843E-9d, -1.0624985110080394E-7d, +9.453598391231829E-8d, -1.134160131581847E-7d, -1.315295870404327E-8d, -7.981320644583728E-8d, -7.327771300038971E-8d, +8.155647334672472E-9d, -7.222791579580787E-8d, -7.430436987497092E-8d, +3.633404807819848E-9d, -7.512438321498593E-8d, -7.044869765481105E-8d, +1.9372589859580955E-8d, -4.2365298585101096E-8d, -1.552830824758035E-8d, +1.0160071259930585E-7d, +7.232201430620959E-8d, -1.0164389431039905E-7d, +5.826233477413577E-8d, +7.6927415825689E-8d, -4.392309439525734E-8d, -6.414337408955734E-8d, +1.799550702470095E-8d, -3.4194410638967946E-8d, +1.9437762419688045E-8d, -5.7792549966531335E-8d, -2.5731071572354522E-8d, +1.173595905705643E-7d, -1.0361863127101014E-7d, +2.8330789837569332E-8d, +3.81131861433539E-8d, -7.252724942149532E-8d, -6.342604067787756E-8d, +6.716441526213986E-8d, +8.257484966196574E-8d, -1.5443717968117592E-8d, +1.3280021798948244E-8d, -6.79180673261558E-8d, -1.8863249269709046E-8d, -7.62162303263991E-8d, +2.011589233663723E-10d, -2.62683511147141E-8d, +8.455684903712996E-8d, +9.602293320384794E-8d, +9.896378545255258E-9d, +6.636396724067746E-8d, +2.8777050870552646E-8d, -1.0109271059094341E-7d, -8.305334708631055E-8d, +8.467026501338835E-8d, -7.29821745001452E-8d, -7.739491336852633E-8d, +7.321238022013781E-8d, -9.621538067089515E-8d, -1.0705722541811197E-7d, +4.247240125405735E-8d, +1.1574222007764044E-7d, +1.145412771487496E-7d, +4.066036653218687E-8d, -1.0410796803072171E-7d, -7.955085231106037E-8d, +1.1612776191572459E-7d, +7.888519481107568E-9d, +7.436813814737735E-8d, +7.894935661289349E-8d, +2.343525263620692E-8d, -9.036933434595339E-8d, -2.2239222395888823E-8d, -8.784622656707742E-9d, -4.819540032304379E-8d, +9.975892708522332E-8d, -3.9945124955316294E-8d, +1.1345047468988893E-8d, +1.702808472925844E-8d, -2.10770182066344E-8d, -1.0114948914089626E-7d, +1.70518021921727E-8d, +9.693260855961159E-8d, -9.809953482725758E-8d, -8.937957126662392E-8d, -1.134963954323427E-7d, +6.980004387880031E-8d, -1.4494150014095534E-8d, +1.122932337832262E-7d, -2.483811732227808E-8d, +5.278759515330048E-8d, +1.0859222881334994E-7d, -9.400056055939758E-8d, -7.630957994128623E-8d, -7.490757191850264E-8d, -8.794689652049879E-8d, -1.1357810855950775E-7d, +8.846862323478745E-8d, +4.32092015744956E-8d, -9.082923009890997E-9d, -6.655106680680314E-8d, +1.1108184705020206E-7d, +4.8838973948592766E-8d, -1.2998975819628988E-8d, -7.25680516883106E-8d, -1.280024819379844E-7d, -1.7743467191652895E-7d, -2.1899520225809197E-7d, +2.2602433110285232E-7d, +2.0582268590356215E-7d, +1.9911192455808124E-7d, +2.0776878313278689E-7d, +2.3367183133931002E-7d, -1.9813568387704588E-7d, -1.320972037315105E-7d, -4.316580502355056E-8d, +7.054443447243064E-8d, +2.109212796025238E-7d, -9.698281856949837E-8d, +1.0239791185239086E-7d, -1.4271754202157014E-7d, +1.232402895636637E-7d, -5.150590480969644E-8d, -1.882201085012735E-7d, +1.918355503889933E-7d, +1.368893262241355E-7d, +1.256828068633383E-7d, +1.601222826656464E-7d, -2.3472125169205568E-7d, -1.032634625827871E-7d, +7.957037517331382E-8d, -1.6114314525832115E-7d, +1.3018591370778052E-7d, +1.8007284821359149E-9d, -6.75421764491544E-8d, -7.592155950645605E-8d, -2.1414301981236817E-8d, +9.79045937979623E-8d, -1.9287515190177685E-7d, +6.184953843236509E-8d, -8.966500602352001E-8d, -1.686490951669855E-7d, -1.7316830893872364E-7d, -1.0128633727463388E-7d, +4.8935021740786486E-8d, -1.9740129448026905E-7d, +1.1532102163380318E-7d, +3.5371542244169364E-8d, +4.153321337726989E-8d, +1.3575372396796738E-7d, -1.5685449228299222E-7d, +1.1933437776279623E-7d, +1.2599421120614435E-8d, +1.7331079674066365E-9d, +8.869266069401045E-8d, -2.013999442282902E-7d, +8.709065843311144E-8d, +2.453117120472083E-9d, +2.3489472779602617E-8d, +1.5216652792122652E-7d, -8.638415150333099E-8d, -2.1335475961524608E-7d, -2.2677272333821516E-7d, -1.246635423141374E-7d, +9.494921297991565E-8d, -4.27932550865546E-8d, -5.907349480138712E-8d, +4.809072216941908E-8d, -1.9615359732789476E-7d, +1.6385396676990034E-7d, +1.7642714221524228E-7d, -1.564440844355254E-7d, +1.2090653407564583E-7d, +5.679855838941285E-8d, +1.3006497185242537E-7d, -1.341336085949317E-7d, +2.1987686050231372E-7d, -2.3641341460419062E-7d, -7.048932272279454E-8d, -2.3401958604540354E-7d, +2.2867766559333004E-7d, -1.1089952719756529E-7d, +1.7977178878541792E-7d, +1.4903074102418675E-7d, -2.011072593789072E-7d, +8.504948422097802E-8d, +5.5846006716348844E-8d, +1.9014079059505456E-7d, +1.3119976852347583E-8d, +3.645999732952202E-9d, +1.6374611405314333E-7d, +1.8612397134087598E-8d, +4.7113225346448296E-8d, -2.2555535676499395E-7d, +1.5631615647329739E-7d, -2.3574653182047758E-7d, +3.08072210937242E-8d, +4.344259288116142E-9d, +1.6374489573868447E-7d, +3.42171232580676E-8d, +9.46452492584643E-8d, -1.297587351085525E-7d, -1.601065201853145E-7d, +5.6550495386976275E-9d, -1.0725602261510391E-7d, -1.9945408945084193E-8d, -2.071910882200156E-7d, -1.900947109027913E-7d, +3.34069282059055E-8d, -1.145810806477298E-8d, +1.5421457732308477E-7d, +5.5657084775121975E-8d, +1.7177785285061278E-7d, +2.7813027425289027E-8d, +1.0267509648109748E-7d, -7.839574072711142E-8d, -3.648293887796095E-8d, +2.3049492079013518E-7d, -2.290530257391564E-7d, +1.747018414872141E-8d, +1.8477759656842807E-8d, -2.2394073401050633E-7d, -2.3085653185818848E-7d, -1.7598351175286083E-10d, -6.640551220774385E-9d, +2.2868466674913266E-7d, +2.3106230530437902E-7d, +2.594209135294356E-9d, +2.2221434720602702E-8d, -1.847872222755186E-7d, -1.3948659218254467E-7d, +1.6023339607737848E-7d, -2.3718944120137026E-7d, +1.0087056692827474E-7d, +2.228553660510707E-7d, +1.3088328582956644E-7d, -1.7292527438195104E-7d, -2.0961068531216087E-7d, +2.2951597845188004E-8d, +5.005103745740068E-8d, -1.2618366811281002E-7d, -2.6784582477238417E-8d, -1.2645600379949252E-7d, +5.3774170051560117E-8d, +3.9205810725333715E-8d, -1.6802196396307013E-7d, -8.893078799284047E-8d, -1.9821451970481713E-7d, -1.689060694498032E-8d, -1.9648717830943396E-8d, -2.0433926409457167E-7d, -9.1973399031975E-8d, -1.5723449006087263E-7d, +7.887051614592191E-8d, +1.4166246290402286E-7d, +3.330146018487787E-8d, +2.3278688667580978E-7d, -2.1139124097042925E-7d, +1.334449995534113E-7d, -1.6104730195920897E-7d, -1.3902314592614197E-7d, +2.0169027167169864E-7d, -9.040643863751471E-8d, -5.946190852360168E-8d, -1.8013411720005014E-7d, +2.6595401669835947E-8d, +8.607292924069425E-8d, +4.84038176769263E-10d, -2.2798356346688802E-7d, -1.203028719549339E-7d, -1.5111906039270745E-7d, +1.5859915617670956E-7d, -1.426262681506497E-7d, -9.892260062323546E-8d, -1.8492643515928268E-7d, +7.840210076743552E-8d, +2.1643071541578027E-7d, +2.313664294893465E-7d, +1.2541842003811723E-7d, -9.920197743470107E-8d, +3.655589133934081E-8d, +5.807052689551411E-8d, -3.244024724169575E-8d, -2.327564406466327E-7d, -6.38187356721971E-8d, -2.3995994000400915E-10d, -3.9793609609721186E-8d, -1.802510054588344E-7d, +5.745586744591196E-8d, +1.987228872666507E-7d, -2.3105188606976847E-7d, +2.0088042407239129E-7d, +6.624793114025702E-8d, -1.5587043044056635E-7d, +1.3606464059428694E-8d, +1.0008761540741556E-7d, +1.058213771597129E-7d, +3.3058299602856804E-8d, -1.1594886810010702E-7d, +1.378919824418909E-7d, -1.5683631181406778E-7d, -4.4200075770425176E-8d, +1.2250985436706623E-9d, -1.8297013058336644E-8d, -1.005004229646318E-7d, +2.337202285991116E-7d, +3.296104292035678E-8d, -2.23668185816307E-7d, -5.7055442971184756E-8d, +5.82391923137467E-8d, +1.244950238958056E-7d, +1.4399358260219398E-7d, +1.1901862840583523E-7d, +5.1856152603337505E-8d, -5.520562000491495E-8d, -1.9987622893254038E-7d, +9.697418238031897E-8d, -1.1603376405901542E-7d, +1.170714288147407E-7d, -1.550851303094034E-7d, +2.3472546699189522E-8d, +1.78211222185955E-7d, -1.6540009048230807E-7d, -5.137865010872577E-8d, +4.57490653163866E-8d, +1.2829599363166098E-7d, +1.985773325073412E-7d, -2.1792661654989742E-7d, -1.652218131743459E-7d, -1.178234251477505E-7d, -7.34071933723896E-8d, -2.9646587857612632E-8d, +1.5787194498912167E-8d, +6.52252321321176E-8d, +1.2100088103262734E-7d, +1.8544977697201776E-7d, -2.159273204728711E-7d, -1.2711589287782304E-7d, -2.2610609958205195E-8d, +9.993330547750349E-8d, -2.33974236642384E-7d, -6.830955860192377E-8d, +1.2244183812423448E-7d, -1.3620325027706252E-7d, +1.1178574689680927E-7d, -8.490693031052439E-8d, +2.2975389535985893E-7d, +1.0445707500867073E-7d, +1.8405243253979117E-8d, -2.6033812325397097E-8d, -2.6489990728664908E-8d, +1.9409124727247465E-8d, +1.1403826867020365E-7d, -2.1706266226554237E-7d, -1.7839974359909697E-8d, +2.3725087624341041E-7d, +7.37567604176979E-8d, -2.9098805266958403E-8d, -6.892713087722722E-8d, -4.333719263537725E-8d, +5.006436936098099E-8d, +2.1367325342138113E-7d, -2.6949659655907758E-8d, -1.9256682968755803E-7d, +1.960616287777496E-7d, +1.876664741413704E-7d, -2.1534486893602122E-7d, -5.688830723853217E-8d, +1.8861113228746644E-7d, +4.6730779443102234E-8d, -3.275360514112964E-9d, +4.1011920825226876E-8d, +1.820141955326842E-7d, -5.468175655175594E-8d, -1.8981247089866317E-7d, -2.209492705846306E-7d, -1.4566110577298295E-7d, +3.848544860465368E-8d, -1.429109630340783E-7d, -2.105749999899302E-7d, -1.6206609756618993E-7d, +5.058693461947143E-9d, -1.8359244902596882E-7d, +2.2810251664891242E-7d, -1.8791776732592608E-7d, +1.3106843166204263E-9d, -1.5543153797220025E-7d, -1.7884997059081524E-7d, -6.648490725635754E-8d, +1.8412576154421806E-7d, +9.860939269906055E-8d, +1.5627006743114285E-7d, -1.17260039161597E-7d, +2.3416513526430908E-7d, -2.1749172296989992E-7d, -3.9242560971295217E-8d, -1.822826971477839E-7d, -1.6729355321895212E-7d, +8.208715337901827E-9d, -1.301267783434537E-7d, -1.029741755377153E-7d, +9.215765583599035E-8d, -1.907487641016455E-8d, +4.2661388254716074E-8d, -1.9697226735187428E-7d, +2.1819935527247946E-7d, -1.398318929248588E-7d, +1.6195123407015624E-7d, +1.723826394935661E-7d, -1.0602700638269148E-7d, -1.9392742205954563E-7d, -8.880302882034106E-8d, +2.1186420987133E-7d, +2.3375763256988976E-7d, -2.0599801342241997E-8d, -7.184550924856607E-8d, +8.254840070367875E-8d, }; /** Extended precision logarithm table over the range 1 - 2 in increments of 2^-10. */ private static final double[][] LN_MANT = new double[][] { {+0.0d, +0.0d, }, // 0 {+9.760860120877624E-4d, -3.903230345984362E-11d, }, // 1 {+0.0019512202125042677d, -8.124251825289188E-11d, }, // 2 {+0.0029254043474793434d, -1.8374207360194882E-11d,}, // 3 {+0.0038986406289041042d, -2.1324678121885073E-10d,}, // 4 {+0.004870930686593056d, -4.5199654318611534E-10d,}, // 5 {+0.00584227591753006d, -2.933016992001806E-10d, }, // 6 {+0.006812678650021553d, -2.325147219074669E-10d, }, // 7 {+0.007782140746712685d, -3.046577356838847E-10d, }, // 8 {+0.008750664070248604d, -5.500631513861575E-10d, }, // 9 {+0.00971824862062931d, +8.48292035519895E-10d, }, // 10 {+0.010684899985790253d, +1.1422610134013436E-10d,}, // 11 {+0.01165061630308628d, +9.168889933128375E-10d, }, // 12 {+0.012615403160452843d, -5.303786078838E-10d, }, // 13 {+0.013579258695244789d, -5.688639355498786E-10d, }, // 14 {+0.01454218477010727d, +7.296670293275653E-10d, }, // 15 {+0.015504186972975731d, -4.370104767451421E-10d, }, // 16 {+0.016465261578559875d, +1.43695591408832E-9d, }, // 17 {+0.01742541790008545d, -1.1862263158849434E-9d, }, // 18 {+0.018384650349617004d, -9.482976524690715E-10d, }, // 19 {+0.01934296265244484d, +1.9068609515836638E-10d,}, // 20 {+0.020300358533859253d, +2.655990315697216E-10d, }, // 21 {+0.021256837993860245d, +1.0315548713040775E-9d, }, // 22 {+0.022212404757738113d, +5.13345647019085E-10d, }, // 23 {+0.02316705882549286d, +4.5604151934208014E-10d,}, // 24 {+0.02412080392241478d, -1.1255706987475148E-9d, }, // 25 {+0.025073636323213577d, +1.2289023836765196E-9d, }, // 26 {+0.02602556347846985d, +1.7990281828096504E-9d, }, // 27 {+0.026976589113473892d, -1.4152718164638451E-9d, }, // 28 {+0.02792670577764511d, +7.568772963781632E-10d, }, // 29 {+0.0288759246468544d, -1.1449998592111558E-9d, }, // 30 {+0.029824241995811462d, -1.6850976862319495E-9d, }, // 31 {+0.030771657824516296d, +8.422373919843096E-10d, }, // 32 {+0.0317181795835495d, +6.872350402175489E-10d, }, // 33 {+0.03266380727291107d, -4.541194749189272E-10d, }, // 34 {+0.03360854089260101d, -8.9064764856495E-10d, }, // 35 {+0.034552380442619324d, +1.0640404096769032E-9d, }, // 36 {+0.0354953333735466d, -3.5901655945224663E-10d,}, // 37 {+0.03643739968538284d, -3.4829517943661266E-9d, }, // 38 {+0.037378571927547455d, +8.149473794244232E-10d, }, // 39 {+0.03831886500120163d, -6.990650304449166E-10d, }, // 40 {+0.03925827145576477d, +1.0883076226453258E-9d, }, // 41 {+0.040196798741817474d, +3.845192807999274E-10d, }, // 42 {+0.04113444685935974d, -1.1570594692045927E-9d, }, // 43 {+0.04207121580839157d, -1.8877045166697178E-9d, }, // 44 {+0.043007105588912964d, -1.6332083257987747E-10d,}, // 45 {+0.04394212365150452d, -1.7950057534514933E-9d, }, // 46 {+0.04487626254558563d, +2.302710041648838E-9d, }, // 47 {+0.045809537172317505d, -1.1410233017161343E-9d, }, // 48 {+0.04674194008111954d, -3.0498741599744685E-9d, }, // 49 {+0.04767347127199173d, -1.8026348269183678E-9d, }, // 50 {+0.04860413819551468d, -3.233204600453039E-9d, }, // 51 {+0.04953393340110779d, +1.7211688427961583E-9d, }, // 52 {+0.05046287178993225d, -2.329967807055457E-10d, }, // 53 {+0.05139094591140747d, -4.191810118556531E-11d, }, // 54 {+0.052318163216114044d, -3.5574324788328143E-9d, }, // 55 {+0.053244516253471375d, -1.7346590916458485E-9d, }, // 56 {+0.05417001247406006d, -4.343048751383674E-10d, }, // 57 {+0.055094651877880096d, +1.92909364037955E-9d, }, // 58 {+0.056018441915512085d, -5.139745677199588E-10d, }, // 59 {+0.05694137513637543d, +1.2637629975129189E-9d, }, // 60 {+0.05786345899105072d, +1.3840561112481119E-9d, }, // 61 {+0.058784693479537964d, +1.414889689612056E-9d, }, // 62 {+0.05970507860183716d, +2.9199191907666474E-9d, }, // 63 {+0.0606246218085289d, +7.90594243412116E-12d, }, // 64 {+0.06154331564903259d, +1.6844747839686189E-9d, }, // 65 {+0.06246116757392883d, +2.0498074572151747E-9d, }, // 66 {+0.06337818503379822d, -4.800180493433863E-9d, }, // 67 {+0.06429435312747955d, -2.4220822960064277E-9d, }, // 68 {+0.06520968675613403d, -4.179048566709334E-9d, }, // 69 {+0.06612417101860046d, +6.363872957010456E-9d, }, // 70 {+0.06703783571720123d, +9.339468680056365E-10d, }, // 71 {+0.06795066595077515d, -4.04226739708981E-9d, }, // 72 {+0.0688626617193222d, -7.043545052852817E-9d, }, // 73 {+0.06977382302284241d, -6.552819560439773E-9d, }, // 74 {+0.07068414986133575d, -1.0571674860370546E-9d, }, // 75 {+0.07159365713596344d, -3.948954622015801E-9d, }, // 76 {+0.07250232994556427d, +1.1776625988228244E-9d, }, // 77 {+0.07341018319129944d, +9.221072639606492E-10d, }, // 78 {+0.07431721687316895d, -3.219119568928366E-9d, }, // 79 {+0.0752234160900116d, +5.147575929018918E-9d, }, // 80 {+0.07612881064414978d, -2.291749683541979E-9d, }, // 81 {+0.07703337073326111d, +5.749565906124772E-9d, }, // 82 {+0.07793712615966797d, +9.495158151301779E-10d, }, // 83 {+0.07884006202220917d, -3.144331429489291E-10d, }, // 84 {+0.0797421783208847d, +3.430029236134205E-9d, }, // 85 {+0.08064348995685577d, -1.2499290483167703E-9d, }, // 86 {+0.08154398202896118d, +2.011215719133196E-9d, }, // 87 {+0.08244366943836212d, -2.2728753031387152E-10d,}, // 88 {+0.0833425521850586d, -6.508966857277253E-9d, }, // 89 {+0.0842406153678894d, -4.801131671405377E-10d, }, // 90 {+0.08513787388801575d, +4.406750291994231E-9d, }, // 91 {+0.08603434264659882d, -5.304795662536171E-9d, }, // 92 {+0.08692999184131622d, +1.6284313912612293E-9d, }, // 93 {+0.08782485127449036d, -3.158898981674071E-9d, }, // 94 {+0.08871890604496002d, -3.3324878834139977E-9d, }, // 95 {+0.08961215615272522d, +2.536961912893389E-9d, }, // 96 {+0.09050461649894714d, +9.737596728980696E-10d, }, // 97 {+0.0913962870836258d, -6.600437262505396E-9d, }, // 98 {+0.09228715300559998d, -3.866609889222889E-9d, }, // 99 {+0.09317722916603088d, -4.311847594020281E-9d, }, // 100 {+0.09406651556491852d, -6.525851105645959E-9d, }, // 101 {+0.09495499730110168d, +5.799080912675435E-9d, }, // 102 {+0.09584270417690277d, +4.2634204358490415E-9d, }, // 103 {+0.09672962129116058d, +5.167390528799477E-9d, }, // 104 {+0.09761576354503632d, -4.994827392841906E-9d, }, // 105 {+0.09850110113620758d, +4.970725577861395E-9d, }, // 106 {+0.09938566386699677d, +6.6496705953229645E-9d, }, // 107 {+0.10026945173740387d, +1.4262712796792241E-9d, }, // 108 {+0.1011524498462677d, +5.5822855204629114E-9d, }, // 109 {+0.10203467309474945d, +5.593494835247651E-9d, }, // 110 {+0.10291612148284912d, +2.8332008343480686E-9d, }, // 111 {+0.10379679501056671d, -1.3289231465997192E-9d, }, // 112 {+0.10467669367790222d, -5.526819276639527E-9d, }, // 113 {+0.10555580258369446d, +6.503128678219282E-9d, }, // 114 {+0.10643415153026581d, +6.317463237641817E-9d, }, // 115 {+0.10731174051761627d, -4.728528221305482E-9d, }, // 116 {+0.10818853974342346d, +4.519199083083901E-9d, }, // 117 {+0.10906457901000977d, +5.606492666349878E-9d, }, // 118 {+0.10993985831737518d, -1.220176214398581E-10d, }, // 119 {+0.11081436276435852d, +3.5759315936869937E-9d, }, // 120 {+0.11168810725212097d, +3.1367659571899855E-9d, }, // 121 {+0.11256109178066254d, -1.0543075713098835E-10d,}, // 122 {+0.11343331634998322d, -4.820065619207094E-9d, }, // 123 {+0.11430476605892181d, +5.221136819669415E-9d, }, // 124 {+0.11517547070980072d, +1.5395018670011342E-9d, }, // 125 {+0.11604541540145874d, +3.5638391501880846E-10d,}, // 126 {+0.11691460013389587d, +2.9885336757136527E-9d, }, // 127 {+0.11778303980827332d, -4.151889860890893E-9d, }, // 128 {+0.11865071952342987d, -4.853823938804204E-9d, }, // 129 {+0.11951763927936554d, +2.189226237170704E-9d, }, // 130 {+0.12038381397724152d, +3.3791993048776982E-9d, }, // 131 {+0.1212492436170578d, +1.5811884868243975E-11d,}, // 132 {+0.12211392819881439d, -6.6045909118908625E-9d, }, // 133 {+0.1229778528213501d, -2.8786263916116364E-10d,}, // 134 {+0.12384103238582611d, +5.354472503748251E-9d, }, // 135 {+0.12470348179340363d, -3.2924463896248744E-9d, }, // 136 {+0.12556517124176025d, +4.856678149580005E-9d, }, // 137 {+0.12642613053321838d, +1.2791850600366742E-9d, }, // 138 {+0.12728634476661682d, +2.1525945093362843E-9d, }, // 139 {+0.12814581394195557d, +8.749974471767862E-9d, }, // 140 {+0.129004567861557d, -7.461209161105275E-9d, }, // 141 {+0.12986254692077637d, +1.4390208226263824E-8d, }, // 142 {+0.1307198405265808d, -1.3839477920475328E-8d, }, // 143 {+0.13157635927200317d, -1.483283901239408E-9d, }, // 144 {+0.13243216276168823d, -6.889072914229094E-9d, }, // 145 {+0.1332872211933136d, +9.990351100568362E-10d, }, // 146 {+0.13414156436920166d, -6.370937412495338E-9d, }, // 147 {+0.13499516248703003d, +2.05047480130511E-9d, }, // 148 {+0.1358480453491211d, -2.29509872547079E-9d, }, // 149 {+0.13670018315315247d, +1.16354361977249E-8d, }, // 150 {+0.13755163550376892d, -1.452496267904829E-8d, }, // 151 {+0.1384023129940033d, +9.865115839786888E-9d, }, // 152 {+0.13925230503082275d, -3.369999130712228E-9d, }, // 153 {+0.14010155200958252d, +6.602496401651853E-9d, }, // 154 {+0.14095008373260498d, +1.1205312852298845E-8d, }, // 155 {+0.14179790019989014d, +1.1660367213160203E-8d, }, // 156 {+0.142645001411438d, +9.186471222585239E-9d, }, // 157 {+0.14349138736724854d, +4.999341878263704E-9d, }, // 158 {+0.14433705806732178d, +3.11611905696257E-10d, }, // 159 {+0.14518201351165771d, -3.6671598175618173E-9d, }, // 160 {+0.14602625370025635d, -5.730477881659618E-9d, }, // 161 {+0.14686977863311768d, -4.674900007989718E-9d, }, // 162 {+0.1477125883102417d, +6.999732437141968E-10d, }, // 163 {+0.14855468273162842d, +1.159150872494107E-8d, }, // 164 {+0.14939609169960022d, -6.082714828488485E-10d, }, // 165 {+0.15023678541183472d, -4.905712741596318E-9d, }, // 166 {+0.1510767638683319d, -1.124848988733307E-10d, }, // 167 {+0.15191605687141418d, -1.484557220949851E-8d, }, // 168 {+0.15275460481643677d, +1.1682026251371384E-8d, }, // 169 {+0.15359249711036682d, -8.757272519238786E-9d, }, // 170 {+0.15442964434623718d, +1.4419920764774415E-8d, }, // 171 {+0.15526613593101501d, -7.019891063126053E-9d, }, // 172 {+0.15610191226005554d, -1.230153548825964E-8d, }, // 173 {+0.15693697333335876d, -2.574172005933276E-10d, }, // 174 {+0.15777134895324707d, +4.748140799544371E-10d, }, // 175 {+0.15860503911972046d, -8.943081874891003E-9d, }, // 176 {+0.15943801403045654d, +2.4500739038517657E-9d, }, // 177 {+0.1602703034877777d, +6.007922084557054E-9d, }, // 178 {+0.16110190749168396d, +2.8835418231126645E-9d, }, // 179 {+0.1619328260421753d, -5.772862039728412E-9d, }, // 180 {+0.16276302933692932d, +1.0988372954605789E-8d, }, // 181 {+0.16359257698059082d, -5.292913162607026E-9d, }, // 182 {+0.16442140936851501d, +6.12956339275823E-9d, }, // 183 {+0.16524958610534668d, -1.3210039516811888E-8d, }, // 184 {+0.16607704758644104d, -2.5711014608334873E-9d, }, // 185 {+0.16690382361412048d, +9.37721319457112E-9d, }, // 186 {+0.1677299439907074d, -6.0370682395944045E-9d, }, // 187 {+0.168555349111557d, +1.1918249660105651E-8d, }, // 188 {+0.1693800985813141d, +4.763282949656017E-9d, }, // 189 {+0.17020416259765625d, +3.4223342273948817E-9d, }, // 190 {+0.1710275411605835d, +9.014612241310916E-9d, }, // 191 {+0.1718502640724182d, -7.145758990550526E-9d, }, // 192 {+0.172672301530838d, -1.4142763934081504E-8d, }, // 193 {+0.1734936535358429d, -1.0865453656579032E-8d, }, // 194 {+0.17431432008743286d, +3.794385569450774E-9d, }, // 195 {+0.1751343309879303d, +1.1399188501627291E-9d, }, // 196 {+0.17595365643501282d, +1.2076238768270153E-8d, }, // 197 {+0.1767723262310028d, +7.901084730502162E-9d, }, // 198 {+0.17759034037590027d, -1.0288181007465474E-8d, }, // 199 {+0.1784076690673828d, -1.15945645153806E-8d, }, // 200 {+0.17922431230545044d, +5.073923825786778E-9d, }, // 201 {+0.18004029989242554d, +1.1004278077575267E-8d, }, // 202 {+0.1808556318283081d, +7.2831502374676964E-9d, }, // 203 {+0.18167030811309814d, -5.0054634662706464E-9d, }, // 204 {+0.18248429894447327d, +5.022108460298934E-9d, }, // 205 {+0.18329763412475586d, +8.642254225732676E-9d, }, // 206 {+0.18411031365394592d, +6.931054493326395E-9d, }, // 207 {+0.18492233753204346d, +9.619685356326533E-10d, }, // 208 {+0.18573370575904846d, -8.194157257980706E-9d, }, // 209 {+0.18654438853263855d, +1.0333241479437797E-8d, }, // 210 {+0.1873544454574585d, -1.9948340196027965E-9d, }, // 211 {+0.1881638467311859d, -1.4313002926259948E-8d, }, // 212 {+0.1889725625514984d, +4.241536392174967E-9d, }, // 213 {+0.18978065252304077d, -4.877952454011428E-9d, }, // 214 {+0.1905880868434906d, -1.0813801247641613E-8d, }, // 215 {+0.1913948655128479d, -1.2513218445781325E-8d, }, // 216 {+0.19220098853111267d, -8.925958555729115E-9d, }, // 217 {+0.1930064558982849d, +9.956860681280245E-10d, }, // 218 {+0.193811297416687d, -1.1505428993246996E-8d, }, // 219 {+0.1946154534816742d, +1.4217997464522202E-8d, }, // 220 {+0.19541901350021362d, -1.0200858727747717E-8d, }, // 221 {+0.19622188806533813d, +5.682607223902455E-9d, }, // 222 {+0.1970241367816925d, +3.2988908516009827E-9d, }, // 223 {+0.19782572984695435d, +1.3482965534659446E-8d, }, // 224 {+0.19862669706344604d, +7.462678536479685E-9d, }, // 225 {+0.1994270384311676d, -1.3734273888891115E-8d, }, // 226 {+0.20022669434547424d, +1.0521983802642893E-8d, }, // 227 {+0.20102575421333313d, -8.152742388541905E-9d, }, // 228 {+0.2018241584300995d, -9.133484280193855E-9d, }, // 229 {+0.20262190699577332d, +8.59763959528144E-9d, }, // 230 {+0.2034190595149994d, -1.3548568223001414E-8d, }, // 231 {+0.20421552658081055d, +1.4847880344628818E-8d, }, // 232 {+0.20501139760017395d, +5.390620378060543E-9d, }, // 233 {+0.2058066427707672d, -1.1109834472051523E-8d, }, // 234 {+0.20660123229026794d, -3.845373872038116E-9d, }, // 235 {+0.20739519596099854d, -1.6149279479975042E-9d, }, // 236 {+0.20818853378295898d, -3.4174925203771133E-9d, }, // 237 {+0.2089812457561493d, -8.254443919468538E-9d, }, // 238 {+0.20977330207824707d, +1.4672790944499144E-8d, }, // 239 {+0.2105647623538971d, +6.753452542942992E-9d, }, // 240 {+0.21135559678077698d, -1.218609462241927E-9d, }, // 241 {+0.21214580535888672d, -8.254218316367887E-9d, }, // 242 {+0.21293538808822632d, -1.3366540360587255E-8d, }, // 243 {+0.2137243151664734d, +1.4231244750190031E-8d, }, // 244 {+0.2145126760005951d, -1.3885660525939072E-8d, }, // 245 {+0.21530038118362427d, -7.3304404046850136E-9d, }, // 246 {+0.2160874605178833d, +5.072117654842356E-9d, }, // 247 {+0.21687394380569458d, -5.505080220459036E-9d, }, // 248 {+0.21765980124473572d, -8.286782292266659E-9d, }, // 249 {+0.2184450328350067d, -2.302351152358085E-9d, }, // 250 {+0.21922963857650757d, +1.3416565858314603E-8d, }, // 251 {+0.22001364827156067d, +1.0033721426962048E-8d, }, // 252 {+0.22079706192016602d, -1.1487079818684332E-8d, }, // 253 {+0.22157981991767883d, +9.420348186357043E-9d, }, // 254 {+0.2223619818687439d, +1.4110645699377834E-8d, }, // 255 {+0.2231435477733612d, +3.5408485497116107E-9d, }, // 256 {+0.22392448782920837d, +8.468072777056227E-9d, }, // 257 {+0.2247048318386078d, +4.255446699237779E-11d, }, // 258 {+0.22548454999923706d, +9.016946273084244E-9d, }, // 259 {+0.22626367211341858d, +6.537034810260226E-9d, }, // 260 {+0.22704219818115234d, -6.451285264969768E-9d, }, // 261 {+0.22782009840011597d, +7.979956357126066E-10d, }, // 262 {+0.22859740257263184d, -5.759582672039005E-10d, }, // 263 {+0.22937411069869995d, -9.633854121180397E-9d, }, // 264 {+0.23015019297599792d, +4.363736368635843E-9d, }, // 265 {+0.23092567920684814d, +1.2549416560182509E-8d, }, // 266 {+0.231700599193573d, -1.3946383592553814E-8d, }, // 267 {+0.2324748933315277d, -1.458843364504023E-8d, }, // 268 {+0.23324856162071228d, +1.1551692104697154E-8d, }, // 269 {+0.23402166366577148d, +5.795621295524984E-9d, }, // 270 {+0.23479416966438293d, -1.1301979046684263E-9d, }, // 271 {+0.23556607961654663d, -8.303779721781787E-9d, }, // 272 {+0.23633739352226257d, -1.4805271785394075E-8d, }, // 273 {+0.23710808157920837d, +1.0085373835899469E-8d, }, // 274 {+0.2378782033920288d, +7.679117635349454E-9d, }, // 275 {+0.2386477291584015d, +8.69177352065934E-9d, }, // 276 {+0.23941665887832642d, +1.4034725764547136E-8d, }, // 277 {+0.24018502235412598d, -5.185064518887831E-9d, }, // 278 {+0.2409527599811554d, +1.1544236628121676E-8d, }, // 279 {+0.24171993136405945d, +5.523085719902123E-9d, }, // 280 {+0.24248650670051575d, +7.456824943331887E-9d, }, // 281 {+0.24325251579284668d, -1.1555923403029638E-8d, }, // 282 {+0.24401789903640747d, +8.988361382732908E-9d, }, // 283 {+0.2447827160358429d, +1.0381848020926893E-8d, }, // 284 {+0.24554696679115295d, -6.480706118857055E-9d, }, // 285 {+0.24631062150001526d, -1.0904271124793968E-8d, }, // 286 {+0.2470736801624298d, -1.998183061531611E-9d, }, // 287 {+0.247836172580719d, -8.676137737360023E-9d, }, // 288 {+0.24859806895256042d, -2.4921733203932487E-10d,}, // 289 {+0.2493593990802765d, -5.635173762130303E-9d, }, // 290 {+0.2501201629638672d, -2.3951455355985637E-8d, }, // 291 {+0.25088030099868774d, +5.287121672447825E-9d, }, // 292 {+0.2516399025917053d, -6.447877375049486E-9d, }, // 293 {+0.25239890813827515d, +1.32472428796441E-9d, }, // 294 {+0.2531573176383972d, +2.9479464287605006E-8d, }, // 295 {+0.2539151906967163d, +1.9284247135543574E-8d, }, // 296 {+0.2546725273132324d, -2.8390360197221716E-8d, }, // 297 {+0.255429208278656d, +6.533522495226226E-9d, }, // 298 {+0.2561853528022766d, +5.713225978895991E-9d, }, // 299 {+0.25694090127944946d, +2.9618050962556135E-8d, }, // 300 {+0.25769591331481934d, +1.950605015323617E-8d, }, // 301 {+0.25845038890838623d, -2.3762031507525576E-8d, }, // 302 {+0.2592042088508606d, +1.98818938195077E-8d, }, // 303 {+0.25995755195617676d, -2.751925069084042E-8d, }, // 304 {+0.2607102394104004d, +1.3703391844683932E-8d, }, // 305 {+0.26146239042282104d, +2.5193525310038174E-8d, }, // 306 {+0.2622140049934387d, +7.802219817310385E-9d, }, // 307 {+0.26296502351760864d, +2.1983272709242607E-8d, }, // 308 {+0.2637155055999756d, +8.979279989292184E-9d, }, // 309 {+0.2644653916358948d, +2.9240221157844312E-8d, }, // 310 {+0.265214741230011d, +2.4004885823813374E-8d, }, // 311 {+0.2659635543823242d, -5.885186277410878E-9d, }, // 312 {+0.2667117714881897d, +1.4300386517357162E-11d,}, // 313 {+0.2674594521522522d, -1.7063531531989365E-8d, }, // 314 {+0.26820653676986694d, +3.3218524692903896E-9d, }, // 315 {+0.2689530849456787d, +2.3998252479954764E-9d, }, // 316 {+0.2696990966796875d, -1.8997462070389404E-8d, }, // 317 {+0.27044451236724854d, -4.350745270980051E-10d, }, // 318 {+0.2711893916130066d, -6.892221115467135E-10d, }, // 319 {+0.27193373441696167d, -1.89333199110902E-8d, }, // 320 {+0.272677481174469d, +5.262017392507765E-9d, }, // 321 {+0.27342069149017334d, +1.3115046679980076E-8d, }, // 322 {+0.2741633653640747d, +5.4468361834451975E-9d, }, // 323 {+0.2749055027961731d, -1.692337384653611E-8d, }, // 324 {+0.27564704418182373d, +6.426479056697412E-9d, }, // 325 {+0.2763880491256714d, +1.670735065191342E-8d, }, // 326 {+0.27712851762771606d, +1.4733029698334834E-8d, }, // 327 {+0.27786844968795776d, +1.315498542514467E-9d, }, // 328 {+0.2786078453063965d, -2.2735061539223372E-8d, }, // 329 {+0.27934664487838745d, +2.994379757313727E-9d, }, // 330 {+0.28008490800857544d, +1.970577274107218E-8d, }, // 331 {+0.28082263469696045d, +2.820392733542077E-8d, }, // 332 {+0.2815598249435425d, +2.929187356678173E-8d, }, // 333 {+0.28229647874832153d, +2.377086680926386E-8d, }, // 334 {+0.2830325961112976d, +1.2440393009992529E-8d, }, // 335 {+0.2837681770324707d, -3.901826104778096E-9d, }, // 336 {+0.2845032215118408d, -2.4459827842685974E-8d, }, // 337 {+0.2852376699447632d, +1.1165241398059789E-8d, }, // 338 {+0.28597164154052734d, -1.54434478239181E-8d, }, // 339 {+0.28670501708984375d, +1.5714110564653245E-8d, }, // 340 {+0.28743791580200195d, -1.3782394940142479E-8d, }, // 341 {+0.2881702184677124d, +1.6063569876284005E-8d, }, // 342 {+0.28890204429626465d, -1.317176818216125E-8d, }, // 343 {+0.28963327407836914d, +1.8504673536253893E-8d, }, // 344 {+0.29036402702331543d, -7.334319635123628E-9d, }, // 345 {+0.29109418392181396d, +2.9300903540317107E-8d, }, // 346 {+0.2918238639831543d, +9.979706999541057E-9d, }, // 347 {+0.29255300760269165d, -4.916314210412424E-9d, }, // 348 {+0.293281614780426d, -1.4611908070155308E-8d, }, // 349 {+0.2940096855163574d, -1.833351586679361E-8d, }, // 350 {+0.29473721981048584d, -1.530926726615185E-8d, }, // 351 {+0.2954642176628113d, -4.7689754029101934E-9d, }, // 352 {+0.29619067907333374d, +1.4055868011423819E-8d, }, // 353 {+0.296916663646698d, -1.7672547212604003E-8d, }, // 354 {+0.2976420521736145d, +2.0020234215759705E-8d, }, // 355 {+0.2983669638633728d, +8.688424478730524E-9d, }, // 356 {+0.2990913391113281d, +8.69851089918337E-9d, }, // 357 {+0.29981517791748047d, +2.0810681643102672E-8d, }, // 358 {+0.3005385398864746d, -1.3821169493779352E-8d, }, // 359 {+0.301261305809021d, +2.4769140784919128E-8d, }, // 360 {+0.3019835948944092d, +1.8127576600610336E-8d, }, // 361 {+0.3027053475379944d, +2.6612401062437074E-8d, }, // 362 {+0.3034266233444214d, -8.629042891789934E-9d, }, // 363 {+0.3041473627090454d, -2.724174869314043E-8d, }, // 364 {+0.30486756563186646d, -2.8476975783775358E-8d, }, // 365 {+0.3055872321128845d, -1.1587600174449919E-8d, }, // 366 {+0.3063063621520996d, +2.417189020581056E-8d, }, // 367 {+0.3070250153541565d, +1.99407553679345E-8d, }, // 368 {+0.3077431917190552d, -2.35387025694381E-8d, }, // 369 {+0.3084607720375061d, +1.3683509995845583E-8d, }, // 370 {+0.30917787551879883d, +1.3137214081023085E-8d, }, // 371 {+0.30989450216293335d, -2.444006866174775E-8d, }, // 372 {+0.3106105327606201d, +2.0896888605749563E-8d, }, // 373 {+0.31132614612579346d, -2.893149098508887E-8d, }, // 374 {+0.31204116344451904d, +5.621509038251498E-9d, }, // 375 {+0.3127557039260864d, +6.0778104626050015E-9d, }, // 376 {+0.3134697675704956d, -2.6832941696716294E-8d, }, // 377 {+0.31418323516845703d, +2.6826625274495256E-8d, }, // 378 {+0.31489628553390503d, -1.1030897183911054E-8d, }, // 379 {+0.31560879945755005d, -2.047124671392676E-8d, }, // 380 {+0.3163207769393921d, -7.709990443086711E-10d, }, // 381 {+0.3170322775840759d, -1.0812918808112342E-8d, }, // 382 {+0.3177432417869568d, +9.727979174888975E-9d, }, // 383 {+0.31845372915267944d, +1.9658551724508715E-9d, }, // 384 {+0.3191636800765991d, +2.6222628001695826E-8d, }, // 385 {+0.3198731541633606d, +2.3609400272358744E-8d, }, // 386 {+0.32058215141296387d, -5.159602957634814E-9d, }, // 387 {+0.32129061222076416d, +2.329701319016099E-10d, }, // 388 {+0.32199859619140625d, -1.910633190395738E-8d, }, // 389 {+0.32270604372024536d, -2.863180390093667E-9d, }, // 390 {+0.32341301441192627d, -9.934041364456825E-9d, }, // 391 {+0.3241194486618042d, +1.999240777687192E-8d, }, // 392 {+0.3248254060745239d, +2.801670341647724E-8d, }, // 393 {+0.32553088665008545d, +1.4842534265191358E-8d, }, // 394 {+0.32623589038848877d, -1.882789920477354E-8d, }, // 395 {+0.3269403576850891d, -1.268923579073577E-8d, }, // 396 {+0.32764434814453125d, -2.564688370677835E-8d, }, // 397 {+0.3283478021621704d, +2.6015626820520968E-9d, }, // 398 {+0.32905077934265137d, +1.3147747907784344E-8d, }, // 399 {+0.3297532796859741d, +6.686493860720675E-9d, }, // 400 {+0.33045530319213867d, -1.608884086544153E-8d, }, // 401 {+0.33115679025650024d, +5.118287907840204E-9d, }, // 402 {+0.3318578004837036d, +1.139367970944884E-8d, }, // 403 {+0.3325583338737488d, +3.426327822115399E-9d, }, // 404 {+0.33325839042663574d, -1.809622142990733E-8d, }, // 405 {+0.3339579105377197d, +7.116780143398601E-9d, }, // 406 {+0.3346569538116455d, +2.0145352306345386E-8d, }, // 407 {+0.3353555202484131d, +2.167272474431968E-8d, }, // 408 {+0.33605360984802246d, +1.2380696294966822E-8d, }, // 409 {+0.33675122261047363d, -7.050361059209181E-9d, }, // 410 {+0.3374482989311218d, +2.366314656322868E-8d, }, // 411 {+0.3381449580192566d, -1.4010540194086646E-8d, }, // 412 {+0.3388410806655884d, -1.860165465666482E-10d, }, // 413 {+0.33953672647476196d, +6.206776940880773E-9d, }, // 414 {+0.34023189544677734d, +5.841137379010982E-9d, }, // 415 {+0.3409265875816345d, -6.11041311179286E-10d, }, // 416 {+0.3416208028793335d, -1.2479264502054702E-8d, }, // 417 {+0.34231454133987427d, -2.909443297645926E-8d, }, // 418 {+0.34300774335861206d, +9.815805717097634E-9d, }, // 419 {+0.3437005281448364d, -1.4291517981101049E-8d, }, // 420 {+0.3443927764892578d, +1.8457821628427503E-8d, }, // 421 {+0.34508460760116577d, -1.0481908869377813E-8d, }, // 422 {+0.34577590227127075d, +1.876076001514746E-8d, }, // 423 {+0.3464667797088623d, -1.2362653723769037E-8d, }, // 424 {+0.3471571207046509d, +1.6016578405624026E-8d, }, // 425 {+0.347847044467926d, -1.4652759033760925E-8d, }, // 426 {+0.3485364317893982d, +1.549533655901835E-8d, }, // 427 {+0.34922540187835693d, -1.2093068629412478E-8d, }, // 428 {+0.3499138355255127d, +2.244531711424792E-8d, }, // 429 {+0.35060185194015503d, +5.538565518604807E-10d, }, // 430 {+0.35128939151763916d, -1.7511499366215853E-8d, }, // 431 {+0.3519763946533203d, +2.850385787215544E-8d, }, // 432 {+0.35266298055648804d, +2.003926370146842E-8d, }, // 433 {+0.35334908962249756d, +1.734665280502264E-8d, }, // 434 {+0.3540347218513489d, +2.1071983674869414E-8d, }, // 435 {+0.35471993684768677d, -2.774475773922311E-8d, }, // 436 {+0.3554046154022217d, -9.250975291734664E-9d, }, // 437 {+0.3560888171195984d, +1.7590672330295415E-8d, }, // 438 {+0.35677260160446167d, -6.1837904549178745E-9d, }, // 439 {+0.35745590925216675d, -2.0330362973820856E-8d, }, // 440 {+0.3581387400627136d, -2.42109990366786E-8d, }, // 441 {+0.3588210940361023d, -1.7188958587407816E-8d, }, // 442 {+0.35950297117233276d, +1.3711958590112228E-9d, }, // 443 {+0.3601844310760498d, -2.7501042008405925E-8d, }, // 444 {+0.36086535453796387d, +1.6036460343275798E-8d, }, // 445 {+0.3615458607673645d, +1.3405964389498495E-8d, }, // 446 {+0.36222589015960693d, +2.484237749027735E-8d, }, // 447 {+0.36290550231933594d, -8.629967484362177E-9d, }, // 448 {+0.36358463764190674d, -2.6778729562324134E-8d, }, // 449 {+0.36426329612731934d, -2.8977490516960565E-8d, }, // 450 {+0.36494147777557373d, -1.4601106624823502E-8d, }, // 451 {+0.3656191825866699d, +1.69742947894444E-8d, }, // 452 {+0.3662964701652527d, +6.7666740211281175E-9d, }, // 453 {+0.36697328090667725d, +1.500201674336832E-8d, }, // 454 {+0.3676496744155884d, -1.730424167425052E-8d, }, // 455 {+0.36832553148269653d, +2.9676011119845104E-8d, }, // 456 {+0.36900103092193604d, -2.2253590346826743E-8d, }, // 457 {+0.36967599391937256d, +6.3372065441089185E-9d, }, // 458 {+0.37035053968429565d, -3.145816653215968E-9d, }, // 459 {+0.37102460861206055d, +9.515812117036965E-9d, }, // 460 {+0.371698260307312d, -1.4669965113042639E-8d, }, // 461 {+0.3723714351654053d, -1.548715389333397E-8d, }, // 462 {+0.37304413318634033d, +7.674361647125109E-9d, }, // 463 {+0.37371641397476196d, -4.181177882069608E-9d, }, // 464 {+0.3743882179260254d, +9.158530500130718E-9d, }, // 465 {+0.3750596046447754d, -1.13047236597869E-8d, }, // 466 {+0.3757305145263672d, -5.36108186384227E-9d, }, // 467 {+0.3764009475708008d, +2.7593452284747873E-8d, }, // 468 {+0.37707096338272095d, +2.8557016344085205E-8d, }, // 469 {+0.3777405619621277d, -1.868818164036E-9d, }, // 470 {+0.3784096837043762d, -3.479042513414447E-9d, }, // 471 {+0.37907832860946655d, +2.432550290565648E-8d, }, // 472 {+0.37974655628204346d, +2.2538131805476768E-8d, }, // 473 {+0.38041436672210693d, -8.244395239939089E-9d, }, // 474 {+0.3810817003250122d, -7.821867597227376E-9d, }, // 475 {+0.3817485570907593d, +2.4400089062515914E-8d, }, // 476 {+0.3824149966239929d, +2.9410015940087773E-8d, }, // 477 {+0.38308101892471313d, +7.799913824734797E-9d, }, // 478 {+0.38374656438827515d, +1.976524624939355E-8d, }, // 479 {+0.38441169261932373d, +6.291008309266035E-9d, }, // 480 {+0.3850763440132141d, +2.757030889767851E-8d, }, // 481 {+0.38574057817459106d, +2.4585794728405612E-8d, }, // 482 {+0.3864043951034546d, -2.0764122246389383E-9d, }, // 483 {+0.3870677351951599d, +7.77328837578952E-9d, }, // 484 {+0.3877306580543518d, -4.8859560029989374E-9d, }, // 485 {+0.3883931040763855d, +2.0133131420595028E-8d, }, // 486 {+0.38905513286590576d, +2.380738071335498E-8d, }, // 487 {+0.3897167444229126d, +6.7171126157142075E-9d, }, // 488 {+0.39037787914276123d, +2.9046141593926277E-8d, }, // 489 {+0.3910386562347412d, -2.7836800219410262E-8d, }, // 490 {+0.3916988968849182d, +1.545909820981726E-8d, }, // 491 {+0.39235877990722656d, -1.930436269002062E-8d, }, // 492 {+0.3930181860923767d, -1.2343297554921835E-8d, }, // 493 {+0.3936771750450134d, -2.268889128622553E-8d, }, // 494 {+0.39433568716049194d, +9.835827818608177E-9d, }, // 495 {+0.39499378204345703d, +2.6197411946856397E-8d, }, // 496 {+0.3956514596939087d, +2.6965931069318893E-8d, }, // 497 {+0.3963087201118469d, +1.2710331127772166E-8d, }, // 498 {+0.39696556329727173d, -1.6001563011916016E-8d, }, // 499 {+0.39762192964553833d, +1.0016001590267064E-9d, }, // 500 {+0.3982778787612915d, +4.680767399874334E-9d, }, // 501 {+0.39893341064453125d, -4.399582029272418E-9d, }, // 502 {+0.39958852529525757d, -2.5676078228301587E-8d, }, // 503 {+0.4002431631088257d, +1.0181870233355787E-9d, }, // 504 {+0.40089738368988037d, +1.6639728835984655E-8d, }, // 505 {+0.40155118703842163d, +2.174860642202632E-8d, }, // 506 {+0.40220457315444946d, +1.6903781197123503E-8d, }, // 507 {+0.40285754203796387d, +2.663119647467697E-9d, }, // 508 {+0.40351009368896484d, -2.0416603812329616E-8d, }, // 509 {+0.4041621685028076d, +7.82494078472695E-9d, }, // 510 {+0.40481382608413696d, +2.833770747113627E-8d, }, // 511 {+0.40546512603759766d, -1.7929433274271985E-8d, }, // 512 {+0.40611594915390015d, -1.1214757379328965E-8d, }, // 513 {+0.4067663550376892d, -1.0571553019207106E-8d, }, // 514 {+0.40741634368896484d, -1.5449538712332313E-8d, }, // 515 {+0.40806591510772705d, -2.529950530235105E-8d, }, // 516 {+0.40871500968933105d, +2.0031331601617008E-8d, }, // 517 {+0.4093637466430664d, +1.880755298741952E-9d, }, // 518 {+0.41001206636428833d, -1.9600580584843318E-8d, }, // 519 {+0.41065990924835205d, +1.573691633515306E-8d, }, // 520 {+0.4113073945045471d, -1.0772154376548336E-8d, }, // 521 {+0.411954402923584d, +2.0624330192486066E-8d, }, // 522 {+0.4126010537147522d, -8.741139170029572E-9d, }, // 523 {+0.4132472276687622d, +2.0881457123894216E-8d, }, // 524 {+0.41389304399490356d, -9.177488027521808E-9d, }, // 525 {+0.4145383834838867d, +2.0829952491625585E-8d, }, // 526 {+0.4151833653450012d, -7.767915492597301E-9d, }, // 527 {+0.4158278703689575d, +2.4774753446082082E-8d, }, // 528 {+0.41647201776504517d, -2.1581119071750435E-10d,}, // 529 {+0.4171157479286194d, -2.260047972865202E-8d, }, // 530 {+0.4177590012550354d, +1.775884601423381E-8d, }, // 531 {+0.41840189695358276d, +2.185301053838889E-9d, }, // 532 {+0.4190443754196167d, -9.185071463667081E-9d, }, // 533 {+0.4196864366531372d, -1.5821896727910552E-8d, }, // 534 {+0.4203280806541443d, -1.719582086188318E-8d, }, // 535 {+0.42096930742263794d, -1.2778508303324259E-8d, }, // 536 {+0.42161011695861816d, -2.042639194493364E-9d, }, // 537 {+0.42225050926208496d, +1.5538093219698803E-8d, }, // 538 {+0.4228905439376831d, -1.9115659590156936E-8d, }, // 539 {+0.42353010177612305d, +1.3729680248843432E-8d, }, // 540 {+0.42416930198669434d, -4.611893838830296E-9d, }, // 541 {+0.4248080849647522d, -1.4013456880651706E-8d, }, // 542 {+0.42544645071029663d, -1.3953728897042917E-8d, }, // 543 {+0.42608439922332764d, -3.912427573594197E-9d, }, // 544 {+0.4267219305038452d, +1.6629734283189315E-8d, }, // 545 {+0.42735910415649414d, -1.1413593493354881E-8d, }, // 546 {+0.42799586057662964d, -2.792046157580119E-8d, }, // 547 {+0.42863214015960693d, +2.723009182661306E-8d, }, // 548 {+0.42926812171936035d, -2.4260535621557444E-8d, }, // 549 {+0.42990362644195557d, -3.064060124024764E-9d, }, // 550 {+0.43053877353668213d, -2.787640178598121E-8d, }, // 551 {+0.4311734437942505d, +2.102412085257792E-8d, }, // 552 {+0.4318077564239502d, +2.4939635093999683E-8d, }, // 553 {+0.43244171142578125d, -1.5619414792273914E-8d, }, // 554 {+0.4330751895904541d, +1.9065734894871523E-8d, }, // 555 {+0.4337083101272583d, +1.0294301092654604E-8d, }, // 556 {+0.4343410134315491d, +1.8178469851136E-8d, }, // 557 {+0.4349733591079712d, -1.6379825102473853E-8d, }, // 558 {+0.4356052279472351d, +2.6334323946685834E-8d, }, // 559 {+0.43623673915863037d, +2.761628769925529E-8d, }, // 560 {+0.436867892742157d, -1.2030229087793677E-8d, }, // 561 {+0.4374985694885254d, +2.7106814809424793E-8d, }, // 562 {+0.43812888860702515d, +2.631993083235205E-8d, }, // 563 {+0.43875885009765625d, -1.3890028312254422E-8d, }, // 564 {+0.43938833475112915d, +2.6186133735555794E-8d, }, // 565 {+0.4400174617767334d, +2.783809071694788E-8d, }, // 566 {+0.440646231174469d, -8.436135220472006E-9d, }, // 567 {+0.44127458333969116d, -2.2534815932619883E-8d, }, // 568 {+0.4419025182723999d, -1.3961804471714283E-8d, }, // 569 {+0.4425300359725952d, +1.7778112039716255E-8d, }, // 570 {+0.4431571960449219d, +1.3574569976673652E-8d, }, // 571 {+0.4437839984893799d, -2.607907890164073E-8d, }, // 572 {+0.4444103240966797d, +1.8518879652136628E-8d, }, // 573 {+0.44503629207611084d, +2.865065604247164E-8d, }, // 574 {+0.44566190242767334d, +4.806827797299427E-9d, }, // 575 {+0.4462870955467224d, +7.0816970994232115E-9d, }, // 576 {+0.44691193103790283d, -2.3640641240074437E-8d, }, // 577 {+0.4475363492965698d, -2.7267718387865538E-8d, }, // 578 {+0.4481603503227234d, -3.3126235292976077E-9d, }, // 579 {+0.4487839937210083d, -1.0894001590268427E-8d, }, // 580 {+0.4494072198867798d, +1.0077883359971829E-8d, }, // 581 {+0.4500300884246826d, +4.825712712114668E-10d, }, // 582 {+0.450652539730072d, +2.0407987470746858E-8d, }, // 583 {+0.4512746334075928d, +1.073186581170719E-8d, }, // 584 {+0.4518963694572449d, -2.8064314757880205E-8d, }, // 585 {+0.45251762866973877d, +2.3709316816226527E-8d, }, // 586 {+0.4531385898590088d, -1.2281487504266522E-8d, }, // 587 {+0.4537591338157654d, -1.634864487421458E-8d, }, // 588 {+0.45437926054000854d, +1.1985747222409522E-8d, }, // 589 {+0.45499902963638306d, +1.3594057956219485E-8d, }, // 590 {+0.4556184411048889d, -1.1047585095328619E-8d, }, // 591 {+0.45623743534088135d, -1.8592937532754405E-9d, }, // 592 {+0.4568560719490051d, -1.797135137545755E-8d, }, // 593 {+0.4574742913246155d, +6.943684261645378E-10d, }, // 594 {+0.4580921530723572d, -4.994175141684681E-9d, }, // 595 {+0.45870959758758545d, +2.5039391215625133E-8d, }, // 596 {+0.45932674407958984d, -2.7943366835352838E-8d, }, // 597 {+0.45994341373443604d, +1.534146910128904E-8d, }, // 598 {+0.46055978536605835d, -2.3450920230816267E-8d, }, // 599 {+0.46117573976516724d, -2.4642997069960124E-8d, }, // 600 {+0.4617912769317627d, +1.2232622070370946E-8d, }, // 601 {+0.4624064564704895d, +2.80378133047839E-8d, }, // 602 {+0.46302127838134766d, +2.3238237048117092E-8d, }, // 603 {+0.46363574266433716d, -1.7013046451109475E-9d, }, // 604 {+0.46424978971481323d, +1.3287778803035383E-8d, }, // 605 {+0.46486347913742065d, +9.06393426961373E-9d, }, // 606 {+0.4654768109321594d, -1.3910598647592876E-8d, }, // 607 {+0.46608972549438477d, +4.430214458933614E-9d, }, // 608 {+0.46670228242874146d, +4.942270562885745E-9d, }, // 609 {+0.4673144817352295d, -1.1914734393460718E-8d, }, // 610 {+0.4679262638092041d, +1.3922696570638494E-8d, }, // 611 {+0.46853768825531006d, +2.3307929211781914E-8d, }, // 612 {+0.46914875507354736d, +1.669813444584674E-8d, }, // 613 {+0.469759464263916d, -5.450354376430758E-9d, }, // 614 {+0.47036975622177124d, +1.6922605350647674E-8d, }, // 615 {+0.4709796905517578d, +2.4667033200046904E-8d, }, // 616 {+0.47158926725387573d, +1.8236762070433784E-8d, }, // 617 {+0.472198486328125d, -1.915204563140137E-9d, }, // 618 {+0.47280728816986084d, +2.426795414605756E-8d, }, // 619 {+0.4734157919883728d, -2.19717006713618E-8d, }, // 620 {+0.47402387857437134d, -2.0974352165535873E-8d, }, // 621 {+0.47463154792785645d, +2.770970558184228E-8d, }, // 622 {+0.4752389192581177d, +5.32006955298355E-9d, }, // 623 {+0.47584593296051025d, -2.809054633964104E-8d, }, // 624 {+0.4764525294303894d, -1.2470243596102937E-8d, }, // 625 {+0.4770587682723999d, -6.977226702440138E-9d, }, // 626 {+0.47766464948654175d, -1.1165866833118273E-8d, }, // 627 {+0.47827017307281494d, -2.4591344661022708E-8d, }, // 628 {+0.4788752794265747d, +1.2794996377383974E-8d, }, // 629 {+0.4794800877571106d, -1.7772927065973874E-8d, }, // 630 {+0.48008447885513306d, +3.35657712457243E-9d, }, // 631 {+0.48068851232528687d, +1.7020465042442242E-8d, }, // 632 {+0.481292188167572d, +2.365953779624783E-8d, }, // 633 {+0.4818955063819885d, +2.3713798664443718E-8d, }, // 634 {+0.4824984669685364d, +1.7622455019548098E-8d, }, // 635 {+0.4831010699272156d, +5.823920246566496E-9d, }, // 636 {+0.4837033152580261d, -1.1244184344361017E-8d, }, // 637 {+0.48430514335632324d, +2.645961716432205E-8d, }, // 638 {+0.4849066734313965d, +1.6207809718247905E-10d,}, // 639 {+0.4855077862739563d, +2.9507744508973654E-8d, }, // 640 {+0.48610860109329224d, -4.278201128741098E-9d, }, // 641 {+0.48670899868011475d, +1.844722015961139E-8d, }, // 642 {+0.4873090982437134d, -2.1092372471088425E-8d, }, // 643 {+0.4879087805747986d, -3.2555596107382053E-9d, }, // 644 {+0.48850810527801514d, +1.2784366845429667E-8d, }, // 645 {+0.48910707235336304d, +2.7457984659996047E-8d, }, // 646 {+0.48970574140548706d, -1.8409546441412518E-8d, }, // 647 {+0.49030399322509766d, -5.179903818099661E-9d, }, // 648 {+0.4909018874168396d, +7.97053127828682E-9d, }, // 649 {+0.4914994239807129d, +2.146925464473481E-8d, }, // 650 {+0.4920966625213623d, -2.3861648589988232E-8d, }, // 651 {+0.4926934838294983d, -8.386923035320549E-9d, }, // 652 {+0.4932899475097656d, +8.713990131749256E-9d, }, // 653 {+0.4938860535621643d, +2.7865534085810115E-8d, }, // 654 {+0.4944818615913391d, -1.011325138560159E-8d, }, // 655 {+0.4950772523880005d, +1.4409851026316708E-8d, }, // 656 {+0.495672345161438d, -1.735227547472004E-8d, }, // 657 {+0.49626702070236206d, +1.4231078209064581E-8d, }, // 658 {+0.49686139822006226d, -9.628709342929729E-9d, }, // 659 {+0.4974554181098938d, -2.8907074856577267E-8d, }, // 660 {+0.4980490207672119d, +1.6419797090870802E-8d, }, // 661 {+0.49864232540130615d, +7.561041519403049E-9d, }, // 662 {+0.49923527240753174d, +4.538983468118194E-9d, }, // 663 {+0.49982786178588867d, +7.770560657946324E-9d, }, // 664 {+0.500420093536377d, +1.767197002609876E-8d, }, // 665 {+0.5010119676589966d, +3.46586694799214E-8d, }, // 666 {+0.5016034841537476d, +5.914537964556077E-8d, }, // 667 {+0.5021947622299194d, -2.7663203939320167E-8d, }, // 668 {+0.5027855634689331d, +1.3064749115929298E-8d, }, // 669 {+0.5033761262893677d, -5.667682106730711E-8d, }, // 670 {+0.503966212272644d, +1.9424534974370594E-9d, }, // 671 {+0.5045560598373413d, -4.908494602153544E-8d, }, // 672 {+0.5051454305648804d, +2.906989285008994E-8d, }, // 673 {+0.5057345628738403d, -1.602000800745108E-9d, }, // 674 {+0.5063233375549316d, -2.148245271118002E-8d, }, // 675 {+0.5069117546081543d, -3.016329994276181E-8d, }, // 676 {+0.5074998140335083d, -2.7237099632871992E-8d, }, // 677 {+0.5080875158309937d, -1.2297127301923986E-8d, }, // 678 {+0.5086748600006104d, +1.5062624834468093E-8d, }, // 679 {+0.5092618465423584d, +5.524744954836658E-8d, }, // 680 {+0.5098485946655273d, -1.054736327333046E-8d, }, // 681 {+0.5104348659515381d, +5.650063324725722E-8d, }, // 682 {+0.5110208988189697d, +1.8376017791642605E-8d, }, // 683 {+0.5116065740585327d, -5.309470636324855E-9d, }, // 684 {+0.512191891670227d, -1.4154089255217218E-8d, }, // 685 {+0.5127768516540527d, -7.756800301729815E-9d, }, // 686 {+0.5133614540100098d, +1.4282730618002001E-8d, }, // 687 {+0.5139456987380981d, +5.2364136172269755E-8d, }, // 688 {+0.5145297050476074d, -1.2322940607922115E-8d, }, // 689 {+0.5151132345199585d, +5.903831350855322E-8d, }, // 690 {+0.5156965255737305d, +2.8426856726994483E-8d, }, // 691 {+0.5162794589996338d, +1.544882070711032E-8d, }, // 692 {+0.5168620347976685d, +2.0500353979930155E-8d, }, // 693 {+0.5174442529678345d, +4.397691311390564E-8d, }, // 694 {+0.5180262327194214d, -3.2936025225250634E-8d, }, // 695 {+0.5186077356338501d, +2.857419553449673E-8d, }, // 696 {+0.5191890001296997d, -9.51761338269325E-9d, }, // 697 {+0.5197699069976807d, -2.7609457648450225E-8d, }, // 698 {+0.520350456237793d, -2.5309316441333305E-8d, }, // 699 {+0.5209306478500366d, -2.2258513086839407E-9d, }, // 700 {+0.5215104818344116d, +4.203159541613745E-8d, }, // 701 {+0.5220900774002075d, -1.1356287358852729E-8d, }, // 702 {+0.5226693153381348d, -4.279090925831093E-8d, }, // 703 {+0.5232481956481934d, -5.188364552285819E-8d, }, // 704 {+0.5238267183303833d, -3.82465458937857E-8d, }, // 705 {+0.5244048833847046d, -1.4923330530645769E-9d, }, // 706 {+0.5249826908111572d, +5.8765598932137004E-8d, }, // 707 {+0.5255602598190308d, +2.3703896609663678E-8d, }, // 708 {+0.5261374711990356d, +1.2917117341231647E-8d, }, // 709 {+0.5267143249511719d, +2.6789862192139226E-8d, }, // 710 {+0.527290940284729d, -5.350322253112414E-8d, }, // 711 {+0.5278670787811279d, +1.0839714455426386E-8d, }, // 712 {+0.5284429788589478d, -1.821729591343314E-8d, }, // 713 {+0.5290185213088989d, -2.1083014672301448E-8d, }, // 714 {+0.5295937061309814d, +2.623848491704216E-9d, }, // 715 {+0.5301685333251953d, +5.328392630534142E-8d, }, // 716 {+0.5307431221008301d, +1.206790586971942E-8d, }, // 717 {+0.5313173532485962d, -1.4356011804377797E-9d, }, // 718 {+0.5318912267684937d, +1.3152074173459994E-8d, }, // 719 {+0.5324647426605225d, +5.6208949382936426E-8d, }, // 720 {+0.5330380201339722d, +8.90310227565917E-9d, }, // 721 {+0.5336109399795532d, -9.179458802504127E-9d, }, // 722 {+0.5341835021972656d, +2.337337845617735E-9d, }, // 723 {+0.5347557067871094d, +4.3828918300477925E-8d, }, // 724 {+0.535327672958374d, -3.5392250480081715E-9d, }, // 725 {+0.53589928150177d, -2.0183663375378704E-8d, }, // 726 {+0.5364705324172974d, -5.730898606435436E-9d, }, // 727 {+0.537041425704956d, +4.0191927599879235E-8d, }, // 728 {+0.5376120805740356d, -1.2522542401353875E-9d, }, // 729 {+0.5381823778152466d, -1.0482571326594316E-8d, }, // 730 {+0.5387523174285889d, +1.2871924223480165E-8d, }, // 731 {+0.539322018623352d, -5.002774317612589E-8d, }, // 732 {+0.539891242980957d, +3.960668706590162E-8d, }, // 733 {+0.5404602289199829d, +4.372568630242375E-8d, }, // 734 {+0.5410289764404297d, -3.730232461206926E-8d, }, // 735 {+0.5415972471237183d, +3.5309026109857795E-8d, }, // 736 {+0.5421652793884277d, +2.3508325311148225E-8d, }, // 737 {+0.5427329540252686d, +4.6871403168921666E-8d, }, // 738 {+0.5433003902435303d, -1.3445113140270216E-8d, }, // 739 {+0.5438674688339233d, -3.786663982218041E-8d, }, // 740 {+0.5444341897964478d, -2.602850370608209E-8d, }, // 741 {+0.5450005531311035d, +2.2433348713144506E-8d, }, // 742 {+0.5455666780471802d, -1.1326936872620137E-8d, }, // 743 {+0.5461324453353882d, -7.737252533211342E-9d, }, // 744 {+0.5466978549957275d, +3.3564604642699844E-8d, }, // 745 {+0.5472630262374878d, -6.269066061111782E-9d, }, // 746 {+0.5478278398513794d, -7.667998948729528E-9d, }, // 747 {+0.5483922958374023d, +2.9728170818998143E-8d, }, // 748 {+0.5489565134048462d, -1.2930091396008281E-8d, }, // 749 {+0.5495203733444214d, -1.607434968107079E-8d, }, // 750 {+0.5500838756561279d, +2.0653935146671156E-8d, }, // 751 {+0.5506471395492554d, -2.1596593091833788E-8d, }, // 752 {+0.5512100458145142d, -2.3259315921149476E-8d, }, // 753 {+0.5517725944519043d, +1.6022492496522704E-8d, }, // 754 {+0.5523349046707153d, -2.260433328226171E-8d, }, // 755 {+0.5528968572616577d, -1.957497997726303E-8d, }, // 756 {+0.5534584522247314d, +2.5465477111883854E-8d, }, // 757 {+0.5540198087692261d, -6.33792454933092E-9d, }, // 758 {+0.554580807685852d, +4.577835263278281E-9d, }, // 759 {+0.5551414489746094d, +5.856589221771548E-8d, }, // 760 {+0.5557018518447876d, +3.6769498759522324E-8d, }, // 761 {+0.5562618970870972d, +5.874989409410614E-8d, }, // 762 {+0.5568217039108276d, +5.649147309876989E-9d, }, // 763 {+0.5573811531066895d, -2.9726830960751796E-9d, }, // 764 {+0.5579402446746826d, +3.323458344853057E-8d, }, // 765 {+0.5584990978240967d, -4.588749093664028E-9d, }, // 766 {+0.5590575933456421d, +3.115616594184543E-9d, }, // 767 {+0.5596157312393188d, +5.6696103838614634E-8d, }, // 768 {+0.5601736307144165d, +3.7291263280048303E-8d, }, // 769 {+0.5607312917709351d, -5.4751646725093355E-8d, }, // 770 {+0.5612884759902954d, +1.9332630743320287E-8d, }, // 771 {+0.5618454217910767d, +2.147161515775941E-8d, }, // 772 {+0.5624021291732788d, -4.7989172862560625E-8d, }, // 773 {+0.5629583597183228d, +4.971378973445109E-8d, }, // 774 {+0.5635144710540771d, -4.2702997139152675E-8d, }, // 775 {+0.5640701055526733d, +3.273212962622764E-8d, }, // 776 {+0.5646255016326904d, +3.79438125545842E-8d, }, // 777 {+0.5651806592941284d, -2.6725298288329835E-8d, }, // 778 {+0.5657354593276978d, -4.1723833577410244E-8d, }, // 779 {+0.5662899017333984d, -6.71028256490915E-9d, }, // 780 {+0.56684410572052d, -4.055299181908475E-8d, }, // 781 {+0.567397952079773d, -2.3702295314000405E-8d, }, // 782 {+0.5679514408111572d, +4.4181618172507453E-8d, }, // 783 {+0.5685046911239624d, +4.4228706309734985E-8d, }, // 784 {+0.5690577030181885d, -2.3222346436879016E-8d, }, // 785 {+0.5696103572845459d, -3.862412756175274E-8d, }, // 786 {+0.5701626539230347d, -1.6390743801589046E-9d, }, // 787 {+0.5707147121429443d, -3.1139472791083883E-8d, }, // 788 {+0.5712664127349854d, -7.579587391156013E-9d, }, // 789 {+0.5718178749084473d, -4.983281844744412E-8d, }, // 790 {+0.5723689794540405d, -3.835454246739619E-8d, }, // 791 {+0.5729197263717651d, +2.7190020372374008E-8d, }, // 792 {+0.5734702348709106d, +2.7925807446276126E-8d, }, // 793 {+0.574020504951477d, -3.5813506001861646E-8d, }, // 794 {+0.5745704174041748d, -4.448550564530588E-8d, }, // 795 {+0.5751199722290039d, +2.2423840341717488E-9d, }, // 796 {+0.5756692886352539d, -1.450709904687712E-8d, }, // 797 {+0.5762182474136353d, +2.4806815282282017E-8d, }, // 798 {+0.5767669677734375d, +1.3057724436551892E-9d, }, // 799 {+0.5773153305053711d, +3.4529452510568104E-8d, }, // 800 {+0.5778634548187256d, +5.598413198183808E-9d, }, // 801 {+0.5784112215042114d, +3.405124925700107E-8d, }, // 802 {+0.5789587497711182d, +1.0074354568442952E-9d, }, // 803 {+0.5795059204101562d, +2.600448597385527E-8d, }, // 804 {+0.5800528526306152d, -9.83920263200211E-9d, }, // 805 {+0.5805994272232056d, +1.3012807963586057E-8d, }, // 806 {+0.5811457633972168d, -2.432215917965441E-8d, }, // 807 {+0.5816917419433594d, -2.308736892479391E-9d, }, // 808 {+0.5822374820709229d, -3.983067093146514E-8d, }, // 809 {+0.5827828645706177d, -1.735366061128156E-8d, }, // 810 {+0.5833280086517334d, -5.376251584638963E-8d, }, // 811 {+0.5838727951049805d, -2.952399778965259E-8d, }, // 812 {+0.5844172239303589d, +5.5685313670430624E-8d, }, // 813 {+0.5849615335464478d, -3.6230268489088716E-8d, }, // 814 {+0.5855053663253784d, +5.267948957869391E-8d, }, // 815 {+0.5860490798950195d, -3.489144132234588E-8d, }, // 816 {+0.5865923166275024d, +5.9006122320612716E-8d, }, // 817 {+0.5871354341506958d, -2.2934896740542648E-8d, }, // 818 {+0.5876781940460205d, -4.1975650319859075E-8d, }, // 819 {+0.5882205963134766d, +2.2036094805348692E-9d, }, // 820 {+0.5887627601623535d, -9.287179048539306E-9d, }, // 821 {+0.5893045663833618d, +4.3079982556221595E-8d, }, // 822 {+0.589846134185791d, +4.041399585161321E-8d, }, // 823 {+0.5903874635696411d, -1.696746473863933E-8d, }, // 824 {+0.5909284353256226d, -9.53795080582038E-9d, }, // 825 {+0.5914691686630249d, -5.619010749352923E-8d, }, // 826 {+0.5920095443725586d, -3.7398514182529506E-8d, }, // 827 {+0.5925495624542236d, +4.71524479659295E-8d, }, // 828 {+0.5930894613265991d, -4.0640692434639215E-8d, }, // 829 {+0.5936288833618164d, +5.716453096255401E-8d, }, // 830 {+0.5941681861877441d, -1.6745661720946737E-8d, }, // 831 {+0.5947071313858032d, -2.3639110433141897E-8d, }, // 832 {+0.5952457189559937d, +3.67972590471072E-8d, }, // 833 {+0.595784068107605d, +4.566672575206695E-8d, }, // 834 {+0.5963221788406372d, +3.2813537149653483E-9d, }, // 835 {+0.5968599319458008d, +2.916199305533732E-8d, }, // 836 {+0.5973974466323853d, +4.410412409109416E-9d, }, // 837 {+0.5979346036911011d, +4.85464582112459E-8d, }, // 838 {+0.5984715223312378d, +4.267089756924666E-8d, }, // 839 {+0.5990082025527954d, -1.2906712010774655E-8d, }, // 840 {+0.5995445251464844d, +1.3319784467641742E-9d, }, // 841 {+0.6000806093215942d, -3.35137581974451E-8d, }, // 842 {+0.6006163358688354d, +2.0734340706476473E-9d, }, // 843 {+0.6011518239974976d, -1.0808162722402073E-8d, }, // 844 {+0.601686954498291d, +4.735781872502109E-8d, }, // 845 {+0.6022218465805054d, +5.76686738430634E-8d, }, // 846 {+0.6027565002441406d, +2.043049589651736E-8d, }, // 847 {+0.6032907962799072d, +5.515817703577808E-8d, }, // 848 {+0.6038248538970947d, +4.2947540692649586E-8d, }, // 849 {+0.6043586730957031d, -1.589678872195875E-8d, }, // 850 {+0.6048921346664429d, -1.8613847754677912E-9d, }, // 851 {+0.6054253578186035d, -3.3851886626187444E-8d, }, // 852 {+0.6059582233428955d, +7.64416021682279E-9d, }, // 853 {+0.6064908504486084d, +3.7201467248814224E-9d, }, // 854 {+0.6070232391357422d, -4.532172996647129E-8d, }, // 855 {+0.6075552701950073d, -1.997046552871766E-8d, }, // 856 {+0.6080870628356934d, -3.913411606668587E-8d, }, // 857 {+0.6086184978485107d, +1.6697361107868944E-8d, }, // 858 {+0.609149694442749d, +2.8614950293715483E-8d, }, // 859 {+0.6096806526184082d, -3.081552929643174E-9d, }, // 860 {+0.6102112531661987d, +4.111645931319645E-8d, }, // 861 {+0.6107416152954102d, +4.2298539553668435E-8d, }, // 862 {+0.6112717390060425d, +7.630546413718035E-10d, }, // 863 {+0.6118015050888062d, +3.601718675118614E-8d, }, // 864 {+0.6123310327529907d, +2.914906573537692E-8d, }, // 865 {+0.6128603219985962d, -1.9544361222269494E-8d, }, // 866 {+0.613389253616333d, +9.442671392695732E-9d, }, // 867 {+0.6139179468154907d, -2.8031202304593286E-9d, }, // 868 {+0.6144464015960693d, -5.598619958143586E-8d, }, // 869 {+0.6149744987487793d, -3.060220883766096E-8d, }, // 870 {+0.6155023574829102d, -4.556583652800433E-8d, }, // 871 {+0.6160298585891724d, +1.8626341656366314E-8d, }, // 872 {+0.6165571212768555d, +4.305870564227991E-8d, }, // 873 {+0.6170841455459595d, +2.8024460607734262E-8d, }, // 874 {+0.6176109313964844d, -2.6183651590639875E-8d, }, // 875 {+0.6181373596191406d, -6.406189112730307E-11d, }, // 876 {+0.6186635494232178d, -1.2534241706168776E-8d, }, // 877 {+0.6191893815994263d, +5.5906456251308664E-8d, }, // 878 {+0.6197150945663452d, -3.286964881802063E-8d, }, // 879 {+0.6202404499053955d, -4.0153537978961E-8d, }, // 880 {+0.6207654476165771d, +3.434477109643361E-8d, }, // 881 {+0.6212903261184692d, -4.750377491075032E-8d, }, // 882 {+0.6218148469924927d, -4.699152670372743E-8d, }, // 883 {+0.6223390102386475d, +3.617013128065961E-8d, }, // 884 {+0.6228630542755127d, -3.6149218175202596E-8d, }, // 885 {+0.6233867406845093d, -2.5243286814648133E-8d, }, // 886 {+0.6239101886749268d, -5.003410681432538E-8d, }, // 887 {+0.6244332790374756d, +8.974417915105033E-9d, }, // 888 {+0.6249561309814453d, +3.285935446876949E-8d, }, // 889 {+0.6254787445068359d, +2.190661054038537E-8d, }, // 890 {+0.6260011196136475d, -2.3598354190515998E-8d, }, // 891 {+0.6265231370925903d, +1.5838762427747586E-8d, }, // 892 {+0.6270449161529541d, +2.129323729978037E-8d, }, // 893 {+0.6275664567947388d, -6.950808333865794E-9d, }, // 894 {+0.6280876398086548d, +5.059959203156465E-8d, }, // 895 {+0.6286087036132812d, -4.41909071122557E-8d, }, // 896 {+0.6291294097900391d, -5.262093550784066E-8d, }, // 897 {+0.6296497583389282d, +2.559185648444699E-8d, }, // 898 {+0.6301699876785278d, -4.768920119497491E-8d, }, // 899 {+0.6306898593902588d, -3.376406008397877E-8d, }, // 900 {+0.6312094926834106d, -5.156097914033476E-8d, }, // 901 {+0.6317287683486938d, +1.840992392368355E-8d, }, // 902 {+0.632247805595398d, +5.721951534729663E-8d, }, // 903 {+0.6327667236328125d, -5.406177467045421E-8d, }, // 904 {+0.6332851648330688d, +4.247320713683124E-8d, }, // 905 {+0.6338034868240356d, -1.0524557502830645E-8d, }, // 906 {+0.6343214511871338d, +2.5641927558519502E-8d, }, // 907 {+0.6348391771316528d, +3.204135737993823E-8d, }, // 908 {+0.6353566646575928d, +8.951285029786536E-9d, }, // 909 {+0.6358739137649536d, -4.335116707228395E-8d, }, // 910 {+0.6363908052444458d, -5.380016714089483E-9d, }, // 911 {+0.6369074583053589d, +3.931710344901743E-9d, }, // 912 {+0.6374238729476929d, -1.5140150088220166E-8d, }, // 913 {+0.6379399299621582d, +5.688910024377372E-8d, }, // 914 {+0.638455867767334d, -1.8124135273572568E-8d, }, // 915 {+0.6389714479446411d, -1.486720391901626E-9d, }, // 916 {+0.6394867897033691d, -1.2133811978747018E-8d, }, // 917 {+0.6400018930435181d, -4.9791700939901716E-8d, }, // 918 {+0.6405166387557983d, +5.022188652837274E-9d, }, // 919 {+0.6410311460494995d, +3.337143177933685E-8d, }, // 920 {+0.6415454149246216d, +3.55284719912458E-8d, }, // 921 {+0.6420594453811646d, +1.1765332726757802E-8d, }, // 922 {+0.6425732374191284d, -3.7646381826067834E-8d, }, // 923 {+0.6430866718292236d, +6.773803682579552E-9d, }, // 924 {+0.6435998678207397d, +2.608736797081283E-8d, }, // 925 {+0.6441128253936768d, +2.056466263408266E-8d, }, // 926 {+0.6446255445480347d, -9.524376551107945E-9d, }, // 927 {+0.6451379060745239d, +5.5299060775883977E-8d, }, // 928 {+0.6456501483917236d, -2.3114497793159813E-8d, }, // 929 {+0.6461620330810547d, -6.077779731902102E-9d, }, // 930 {+0.6466736793518066d, -1.2531793589140273E-8d, }, // 931 {+0.6471850872039795d, -4.220866994206517E-8d, }, // 932 {+0.6476961374282837d, +2.4368339445199057E-8d, }, // 933 {+0.6482070684432983d, -5.095229574221907E-8d, }, // 934 {+0.6487176418304443d, -2.9485356677301627E-8d, }, // 935 {+0.6492279767990112d, -3.0173901411577916E-8d, }, // 936 {+0.649738073348999d, -5.275210583909726E-8d, }, // 937 {+0.6502478122711182d, +2.2254737134350224E-8d, }, // 938 {+0.6507574319839478d, -4.330693978322885E-8d, }, // 939 {+0.6512666940689087d, -1.0753950588009912E-8d, }, // 940 {+0.6517757177352905d, +9.686179886293545E-10d, }, // 941 {+0.6522845029830933d, -7.875434494414498E-9d, }, // 942 {+0.6527930498123169d, -3.702271091849158E-8d, }, // 943 {+0.6533012390136719d, +3.2999073763758614E-8d, }, // 944 {+0.6538093090057373d, -3.5966064858620067E-8d, }, // 945 {+0.6543170213699341d, -5.23735298540578E-9d, }, // 946 {+0.6548244953155518d, +6.237715351293023E-9d, }, // 947 {+0.6553317308425903d, -1.279462699936282E-9d, }, // 948 {+0.6558387279510498d, -2.7527887552743672E-8d, }, // 949 {+0.6563453674316406d, +4.696233317356646E-8d, }, // 950 {+0.6568518877029419d, -1.5967172745329108E-8d, }, // 951 {+0.6573580503463745d, +2.2361985518423144E-8d, }, // 952 {+0.657863974571228d, +4.2999935789083046E-8d, }, // 953 {+0.6583696603775024d, +4.620570188811826E-8d, }, // 954 {+0.6588751077651978d, +3.223791487908353E-8d, }, // 955 {+0.659380316734314d, +1.3548138612715822E-9d, }, // 956 {+0.6598852872848511d, -4.618575323863973E-8d, }, // 957 {+0.6603899002075195d, +9.082960673843353E-9d, }, // 958 {+0.6608942747116089d, +4.820873399634487E-8d, }, // 959 {+0.6613985300064087d, -4.776104368314602E-8d, }, // 960 {+0.6619024276733398d, -4.0151502150238136E-8d, }, // 961 {+0.6624060869216919d, -4.791602708710648E-8d, }, // 962 {+0.6629093885421753d, +4.8410188461165925E-8d, }, // 963 {+0.6634125709533691d, +1.0663697110471944E-8d, }, // 964 {+0.6639155149459839d, -4.1691464781797555E-8d, }, // 965 {+0.66441810131073d, +1.080835500478704E-8d, }, // 966 {+0.664920449256897d, +4.920784622407246E-8d, }, // 967 {+0.6654226779937744d, -4.544868396511241E-8d, }, // 968 {+0.6659245491027832d, -3.448944157854234E-8d, }, // 969 {+0.6664261817932129d, -3.6870882345139385E-8d, }, // 970 {+0.6669275760650635d, -5.234055273962444E-8d, }, // 971 {+0.6674286127090454d, +3.856291077979099E-8d, }, // 972 {+0.6679295301437378d, -2.327375671320742E-9d, }, // 973 {+0.6684302091598511d, -5.555080534042001E-8d, }, // 974 {+0.6689305305480957d, -1.6471487337453832E-9d, }, // 975 {+0.6694306135177612d, +4.042486803683015E-8d, }, // 976 {+0.6699305772781372d, -4.8293856891818295E-8d, }, // 977 {+0.6704301834106445d, -2.9134931730784303E-8d, }, // 978 {+0.6709295511245728d, -2.1058207594753368E-8d, }, // 979 {+0.6714286804199219d, -2.3814619551682855E-8d, }, // 980 {+0.6719275712966919d, -3.7155475428252136E-8d, }, // 981 {+0.6724261045455933d, +5.8376834484391746E-8d, }, // 982 {+0.6729245185852051d, +2.4611679969129262E-8d, }, // 983 {+0.6734226942062378d, -1.899407107267079E-8d, }, // 984 {+0.6739205121994019d, +4.7016079464436395E-8d, }, // 985 {+0.6744182109832764d, -1.5529608026276525E-8d, }, // 986 {+0.6749155521392822d, +3.203391672602453E-8d, }, // 987 {+0.6754127740859985d, -4.8465821804075345E-8d, }, // 988 {+0.6759096384048462d, -1.8364507801369988E-8d, }, // 989 {+0.6764062643051147d, +3.3739397633046517E-9d, }, // 990 {+0.6769026517868042d, +1.6994526063192333E-8d, }, // 991 {+0.6773988008499146d, +2.2741891590028428E-8d, }, // 992 {+0.6778947114944458d, +2.0860312877435047E-8d, }, // 993 {+0.678390383720398d, +1.1593703222523284E-8d, }, // 994 {+0.678885817527771d, -4.814386594291911E-9d, }, // 995 {+0.6793810129165649d, -2.812076759125914E-8d, }, // 996 {+0.6798759698867798d, -5.808261186903479E-8d, }, // 997 {+0.680370569229126d, +2.4751837654582522E-8d, }, // 998 {+0.6808650493621826d, -1.7793890245755405E-8d, }, // 999 {+0.6813591718673706d, +5.294053246347931E-8d, }, // 1000 {+0.681853175163269d, -1.2220826223585654E-9d, }, // 1001 {+0.6823468208312988d, +5.8377876767612725E-8d, }, // 1002 {+0.6828403472900391d, -6.437492120743254E-9d, }, // 1003 {+0.6833335161209106d, +4.2990710043633113E-8d, }, // 1004 {+0.6838265657424927d, -3.1516131027023284E-8d, }, // 1005 {+0.684319257736206d, +8.70017386744679E-9d, }, // 1006 {+0.6848117113113403d, +4.466959125843237E-8d, }, // 1007 {+0.6853040456771851d, -4.25782656420497E-8d, }, // 1008 {+0.6857960224151611d, -1.4386267593671393E-8d, }, // 1009 {+0.6862877607345581d, +1.0274494061148778E-8d, }, // 1010 {+0.686779260635376d, +3.164186629229597E-8d, }, // 1011 {+0.6872705221176147d, +4.995334552140326E-8d, }, // 1012 {+0.687761664390564d, -5.3763211240398744E-8d, }, // 1013 {+0.6882524490356445d, -4.0852427502515625E-8d, }, // 1014 {+0.688742995262146d, -3.0287143914420064E-8d, }, // 1015 {+0.6892333030700684d, -2.183125937905008E-8d, }, // 1016 {+0.6897233724594116d, -1.524901992178814E-8d, }, // 1017 {+0.6902132034301758d, -1.0305018010328949E-8d, }, // 1018 {+0.6907027959823608d, -6.764191876212205E-9d, }, // 1019 {+0.6911921501159668d, -4.391824838015402E-9d, }, // 1020 {+0.6916812658309937d, -2.9535446262017846E-9d, }, // 1021 {+0.6921701431274414d, -2.2153227096187463E-9d, }, // 1022 {+0.6926587820053101d, -1.943473623641502E-9d, }, // 1023 }; /** * Class contains only static methods. */ private FastMathLiteralArrays() {} /** * Load "EXP_INT_A". * * @return a clone of the data array. */ static double[] loadExpIntA() { return EXP_INT_A.clone(); } /** * Load "EXP_INT_B". * * @return a clone of the data array. */ static double[] loadExpIntB() { return EXP_INT_B.clone(); } /** * Load "EXP_FRAC_A". * * @return a clone of the data array. */ static double[] loadExpFracA() { return EXP_FRAC_A.clone(); } /** * Load "EXP_FRAC_B". * * @return a clone of the data array. */ static double[] loadExpFracB() { return EXP_FRAC_B.clone(); } /** * Load "LN_MANT". * * @return a clone of the data array. */ static double[][] loadLnMant() { return LN_MANT.clone(); } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/util/MathUtils.java100644 1750 1750 22757 12126627720 26310 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.util; import java.util.Arrays; import org.apache.commons.math3.exception.MathArithmeticException; import org.apache.commons.math3.exception.NotFiniteNumberException; import org.apache.commons.math3.exception.NullArgumentException; import org.apache.commons.math3.exception.util.Localizable; import org.apache.commons.math3.exception.util.LocalizedFormats; /** * Miscellaneous utility functions. * * @see ArithmeticUtils * @see Precision * @see MathArrays * * @version $Id: MathUtils.java 1416643 2012-12-03 19:37:14Z tn $ */ public final class MathUtils { /** * 2 π. * @since 2.1 */ public static final double TWO_PI = 2 * FastMath.PI; /** * Class contains only static methods. */ private MathUtils() {} /** * Returns an integer hash code representing the given double value. * * @param value the value to be hashed * @return the hash code */ public static int hash(double value) { return new Double(value).hashCode(); } /** * Returns an integer hash code representing the given double array. * * @param value the value to be hashed (may be null) * @return the hash code * @since 1.2 */ public static int hash(double[] value) { return Arrays.hashCode(value); } /** * Normalize an angle in a 2&pi wide interval around a center value. *

                                    This method has three main uses:

                                    *
                                      *
                                    • normalize an angle between 0 and 2π:
                                      * {@code a = MathUtils.normalizeAngle(a, FastMath.PI);}
                                    • *
                                    • normalize an angle between -π and +π
                                      * {@code a = MathUtils.normalizeAngle(a, 0.0);}
                                    • *
                                    • compute the angle between two defining angular positions:
                                      * {@code angle = MathUtils.normalizeAngle(end, start) - start;}
                                    • *
                                    *

                                    Note that due to numerical accuracy and since π cannot be represented * exactly, the result interval is closed, it cannot be half-closed * as would be more satisfactory in a purely mathematical view.

                                    * @param a angle to normalize * @param center center of the desired 2π interval for the result * @return a-2kπ with integer k and center-π <= a-2kπ <= center+π * @since 1.2 */ public static double normalizeAngle(double a, double center) { return a - TWO_PI * FastMath.floor((a + FastMath.PI - center) / TWO_PI); } /** *

                                    Reduce {@code |a - offset|} to the primary interval * {@code [0, |period|)}.

                                    * *

                                    Specifically, the value returned is
                                    * {@code a - |period| * floor((a - offset) / |period|) - offset}.

                                    * *

                                    If any of the parameters are {@code NaN} or infinite, the result is * {@code NaN}.

                                    * * @param a Value to reduce. * @param period Period. * @param offset Value that will be mapped to {@code 0}. * @return the value, within the interval {@code [0 |period|)}, * that corresponds to {@code a}. */ public static double reduce(double a, double period, double offset) { final double p = FastMath.abs(period); return a - p * FastMath.floor((a - offset) / p) - offset; } /** * Returns the first argument with the sign of the second argument. * * @param magnitude Magnitude of the returned value. * @param sign Sign of the returned value. * @return a value with magnitude equal to {@code magnitude} and with the * same sign as the {@code sign} argument. * @throws MathArithmeticException if {@code magnitude == Byte.MIN_VALUE} * and {@code sign >= 0}. */ public static byte copySign(byte magnitude, byte sign) throws MathArithmeticException { if ((magnitude >= 0 && sign >= 0) || (magnitude < 0 && sign < 0)) { // Sign is OK. return magnitude; } else if (sign >= 0 && magnitude == Byte.MIN_VALUE) { throw new MathArithmeticException(LocalizedFormats.OVERFLOW); } else { return (byte) -magnitude; // Flip sign. } } /** * Returns the first argument with the sign of the second argument. * * @param magnitude Magnitude of the returned value. * @param sign Sign of the returned value. * @return a value with magnitude equal to {@code magnitude} and with the * same sign as the {@code sign} argument. * @throws MathArithmeticException if {@code magnitude == Short.MIN_VALUE} * and {@code sign >= 0}. */ public static short copySign(short magnitude, short sign) throws MathArithmeticException { if ((magnitude >= 0 && sign >= 0) || (magnitude < 0 && sign < 0)) { // Sign is OK. return magnitude; } else if (sign >= 0 && magnitude == Short.MIN_VALUE) { throw new MathArithmeticException(LocalizedFormats.OVERFLOW); } else { return (short) -magnitude; // Flip sign. } } /** * Returns the first argument with the sign of the second argument. * * @param magnitude Magnitude of the returned value. * @param sign Sign of the returned value. * @return a value with magnitude equal to {@code magnitude} and with the * same sign as the {@code sign} argument. * @throws MathArithmeticException if {@code magnitude == Integer.MIN_VALUE} * and {@code sign >= 0}. */ public static int copySign(int magnitude, int sign) throws MathArithmeticException { if ((magnitude >= 0 && sign >= 0) || (magnitude < 0 && sign < 0)) { // Sign is OK. return magnitude; } else if (sign >= 0 && magnitude == Integer.MIN_VALUE) { throw new MathArithmeticException(LocalizedFormats.OVERFLOW); } else { return -magnitude; // Flip sign. } } /** * Returns the first argument with the sign of the second argument. * * @param magnitude Magnitude of the returned value. * @param sign Sign of the returned value. * @return a value with magnitude equal to {@code magnitude} and with the * same sign as the {@code sign} argument. * @throws MathArithmeticException if {@code magnitude == Long.MIN_VALUE} * and {@code sign >= 0}. */ public static long copySign(long magnitude, long sign) throws MathArithmeticException { if ((magnitude >= 0 && sign >= 0) || (magnitude < 0 && sign < 0)) { // Sign is OK. return magnitude; } else if (sign >= 0 && magnitude == Long.MIN_VALUE) { throw new MathArithmeticException(LocalizedFormats.OVERFLOW); } else { return -magnitude; // Flip sign. } } /** * Check that the argument is a real number. * * @param x Argument. * @throws NotFiniteNumberException if {@code x} is not a * finite real number. */ public static void checkFinite(final double x) throws NotFiniteNumberException { if (Double.isInfinite(x) || Double.isNaN(x)) { throw new NotFiniteNumberException(x); } } /** * Check that all the elements are real numbers. * * @param val Arguments. * @throws NotFiniteNumberException if any values of the array is not a * finite real number. */ public static void checkFinite(final double[] val) throws NotFiniteNumberException { for (int i = 0; i < val.length; i++) { final double x = val[i]; if (Double.isInfinite(x) || Double.isNaN(x)) { throw new NotFiniteNumberException(LocalizedFormats.ARRAY_ELEMENT, x, i); } } } /** * Checks that an object is not null. * * @param o Object to be checked. * @param pattern Message pattern. * @param args Arguments to replace the placeholders in {@code pattern}. * @throws NullArgumentException if {@code o} is {@code null}. */ public static void checkNotNull(Object o, Localizable pattern, Object ... args) throws NullArgumentException { if (o == null) { throw new NullArgumentException(pattern, args); } } /** * Checks that an object is not null. * * @param o Object to be checked. * @throws NullArgumentException if {@code o} is {@code null}. */ public static void checkNotNull(Object o) throws NullArgumentException { if (o == null) { throw new NullArgumentException(); } } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/util/Precision.java100644 1750 1750 53152 12126627720 26322 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.util; import java.math.BigDecimal; import org.apache.commons.math3.exception.MathArithmeticException; import org.apache.commons.math3.exception.MathIllegalArgumentException; import org.apache.commons.math3.exception.util.LocalizedFormats; /** * Utilities for comparing numbers. * * @since 3.0 * @version $Id: Precision.java 1422313 2012-12-15 18:53:41Z psteitz $ */ public class Precision { /** *

                                    * Largest double-precision floating-point number such that * {@code 1 + EPSILON} is numerically equal to 1. This value is an upper * bound on the relative error due to rounding real numbers to double * precision floating-point numbers. *

                                    *

                                    * In IEEE 754 arithmetic, this is 2-53. *

                                    * * @see Machine epsilon */ public static final double EPSILON; /** * Safe minimum, such that {@code 1 / SAFE_MIN} does not overflow. *
                                    * In IEEE 754 arithmetic, this is also the smallest normalized * number 2-1022. */ public static final double SAFE_MIN; /** Exponent offset in IEEE754 representation. */ private static final long EXPONENT_OFFSET = 1023l; /** Offset to order signed double numbers lexicographically. */ private static final long SGN_MASK = 0x8000000000000000L; /** Offset to order signed double numbers lexicographically. */ private static final int SGN_MASK_FLOAT = 0x80000000; static { /* * This was previously expressed as = 0x1.0p-53; * However, OpenJDK (Sparc Solaris) cannot handle such small * constants: MATH-721 */ EPSILON = Double.longBitsToDouble((EXPONENT_OFFSET - 53l) << 52); /* * This was previously expressed as = 0x1.0p-1022; * However, OpenJDK (Sparc Solaris) cannot handle such small * constants: MATH-721 */ SAFE_MIN = Double.longBitsToDouble((EXPONENT_OFFSET - 1022l) << 52); } /** * Private constructor. */ private Precision() {} /** * Compares two numbers given some amount of allowed error. * * @param x the first number * @param y the second number * @param eps the amount of error to allow when checking for equality * @return
                                    • 0 if {@link #equals(double, double, double) equals(x, y, eps)}
                                    • *
                                    • < 0 if !{@link #equals(double, double, double) equals(x, y, eps)} && x < y
                                    • *
                                    • > 0 if !{@link #equals(double, double, double) equals(x, y, eps)} && x > y
                                    */ public static int compareTo(double x, double y, double eps) { if (equals(x, y, eps)) { return 0; } else if (x < y) { return -1; } return 1; } /** * Compares two numbers given some amount of allowed error. * Two float numbers are considered equal if there are {@code (maxUlps - 1)} * (or fewer) floating point numbers between them, i.e. two adjacent floating * point numbers are considered equal. * Adapted from * Bruce Dawson * * @param x first value * @param y second value * @param maxUlps {@code (maxUlps - 1)} is the number of floating point * values between {@code x} and {@code y}. * @return
                                    • 0 if {@link #equals(double, double, int) equals(x, y, maxUlps)}
                                    • *
                                    • < 0 if !{@link #equals(double, double, int) equals(x, y, maxUlps)} && x < y
                                    • *
                                    • > 0 if !{@link #equals(double, double, int) equals(x, y, maxUlps)} && x > y
                                    */ public static int compareTo(final double x, final double y, final int maxUlps) { if (equals(x, y, maxUlps)) { return 0; } else if (x < y) { return -1; } return 1; } /** * Returns true iff they are equal as defined by * {@link #equals(float,float,int) equals(x, y, 1)}. * * @param x first value * @param y second value * @return {@code true} if the values are equal. */ public static boolean equals(float x, float y) { return equals(x, y, 1); } /** * Returns true if both arguments are NaN or neither is NaN and they are * equal as defined by {@link #equals(float,float) equals(x, y, 1)}. * * @param x first value * @param y second value * @return {@code true} if the values are equal or both are NaN. * @since 2.2 */ public static boolean equalsIncludingNaN(float x, float y) { return (Float.isNaN(x) && Float.isNaN(y)) || equals(x, y, 1); } /** * Returns true if both arguments are equal or within the range of allowed * error (inclusive). * * @param x first value * @param y second value * @param eps the amount of absolute error to allow. * @return {@code true} if the values are equal or within range of each other. * @since 2.2 */ public static boolean equals(float x, float y, float eps) { return equals(x, y, 1) || FastMath.abs(y - x) <= eps; } /** * Returns true if both arguments are NaN or are equal or within the range * of allowed error (inclusive). * * @param x first value * @param y second value * @param eps the amount of absolute error to allow. * @return {@code true} if the values are equal or within range of each other, * or both are NaN. * @since 2.2 */ public static boolean equalsIncludingNaN(float x, float y, float eps) { return equalsIncludingNaN(x, y) || (FastMath.abs(y - x) <= eps); } /** * Returns true if both arguments are equal or within the range of allowed * error (inclusive). * Two float numbers are considered equal if there are {@code (maxUlps - 1)} * (or fewer) floating point numbers between them, i.e. two adjacent floating * point numbers are considered equal. * Adapted from * Bruce Dawson * * @param x first value * @param y second value * @param maxUlps {@code (maxUlps - 1)} is the number of floating point * values between {@code x} and {@code y}. * @return {@code true} if there are fewer than {@code maxUlps} floating * point values between {@code x} and {@code y}. * @since 2.2 */ public static boolean equals(float x, float y, int maxUlps) { int xInt = Float.floatToIntBits(x); int yInt = Float.floatToIntBits(y); // Make lexicographically ordered as a two's-complement integer. if (xInt < 0) { xInt = SGN_MASK_FLOAT - xInt; } if (yInt < 0) { yInt = SGN_MASK_FLOAT - yInt; } final boolean isEqual = FastMath.abs(xInt - yInt) <= maxUlps; return isEqual && !Float.isNaN(x) && !Float.isNaN(y); } /** * Returns true if both arguments are NaN or if they are equal as defined * by {@link #equals(float,float,int) equals(x, y, maxUlps)}. * * @param x first value * @param y second value * @param maxUlps {@code (maxUlps - 1)} is the number of floating point * values between {@code x} and {@code y}. * @return {@code true} if both arguments are NaN or if there are less than * {@code maxUlps} floating point values between {@code x} and {@code y}. * @since 2.2 */ public static boolean equalsIncludingNaN(float x, float y, int maxUlps) { return (Float.isNaN(x) && Float.isNaN(y)) || equals(x, y, maxUlps); } /** * Returns true iff they are equal as defined by * {@link #equals(double,double,int) equals(x, y, 1)}. * * @param x first value * @param y second value * @return {@code true} if the values are equal. */ public static boolean equals(double x, double y) { return equals(x, y, 1); } /** * Returns true if both arguments are NaN or neither is NaN and they are * equal as defined by {@link #equals(double,double) equals(x, y, 1)}. * * @param x first value * @param y second value * @return {@code true} if the values are equal or both are NaN. * @since 2.2 */ public static boolean equalsIncludingNaN(double x, double y) { return (Double.isNaN(x) && Double.isNaN(y)) || equals(x, y, 1); } /** * Returns {@code true} if there is no double value strictly between the * arguments or the difference between them is within the range of allowed * error (inclusive). * * @param x First value. * @param y Second value. * @param eps Amount of allowed absolute error. * @return {@code true} if the values are two adjacent floating point * numbers or they are within range of each other. */ public static boolean equals(double x, double y, double eps) { return equals(x, y, 1) || FastMath.abs(y - x) <= eps; } /** * Returns {@code true} if there is no double value strictly between the * arguments or the reltaive difference between them is smaller or equal * to the given tolerance. * * @param x First value. * @param y Second value. * @param eps Amount of allowed relative error. * @return {@code true} if the values are two adjacent floating point * numbers or they are within range of each other. * @since 3.1 */ public static boolean equalsWithRelativeTolerance(double x, double y, double eps) { if (equals(x, y, 1)) { return true; } final double absoluteMax = FastMath.max(FastMath.abs(x), FastMath.abs(y)); final double relativeDifference = FastMath.abs((x - y) / absoluteMax); return relativeDifference <= eps; } /** * Returns true if both arguments are NaN or are equal or within the range * of allowed error (inclusive). * * @param x first value * @param y second value * @param eps the amount of absolute error to allow. * @return {@code true} if the values are equal or within range of each other, * or both are NaN. * @since 2.2 */ public static boolean equalsIncludingNaN(double x, double y, double eps) { return equalsIncludingNaN(x, y) || (FastMath.abs(y - x) <= eps); } /** * Returns true if both arguments are equal or within the range of allowed * error (inclusive). * Two float numbers are considered equal if there are {@code (maxUlps - 1)} * (or fewer) floating point numbers between them, i.e. two adjacent floating * point numbers are considered equal. * Adapted from * Bruce Dawson * * @param x first value * @param y second value * @param maxUlps {@code (maxUlps - 1)} is the number of floating point * values between {@code x} and {@code y}. * @return {@code true} if there are fewer than {@code maxUlps} floating * point values between {@code x} and {@code y}. */ public static boolean equals(double x, double y, int maxUlps) { long xInt = Double.doubleToLongBits(x); long yInt = Double.doubleToLongBits(y); // Make lexicographically ordered as a two's-complement integer. if (xInt < 0) { xInt = SGN_MASK - xInt; } if (yInt < 0) { yInt = SGN_MASK - yInt; } final boolean isEqual = FastMath.abs(xInt - yInt) <= maxUlps; return isEqual && !Double.isNaN(x) && !Double.isNaN(y); } /** * Returns true if both arguments are NaN or if they are equal as defined * by {@link #equals(double,double,int) equals(x, y, maxUlps)}. * * @param x first value * @param y second value * @param maxUlps {@code (maxUlps - 1)} is the number of floating point * values between {@code x} and {@code y}. * @return {@code true} if both arguments are NaN or if there are less than * {@code maxUlps} floating point values between {@code x} and {@code y}. * @since 2.2 */ public static boolean equalsIncludingNaN(double x, double y, int maxUlps) { return (Double.isNaN(x) && Double.isNaN(y)) || equals(x, y, maxUlps); } /** * Rounds the given value to the specified number of decimal places. * The value is rounded using the {@link BigDecimal#ROUND_HALF_UP} method. * * @param x Value to round. * @param scale Number of digits to the right of the decimal point. * @return the rounded value. * @since 1.1 (previously in {@code MathUtils}, moved as of version 3.0) */ public static double round(double x, int scale) { return round(x, scale, BigDecimal.ROUND_HALF_UP); } /** * Rounds the given value to the specified number of decimal places. * The value is rounded using the given method which is any method defined * in {@link BigDecimal}. * If {@code x} is infinite or {@code NaN}, then the value of {@code x} is * returned unchanged, regardless of the other parameters. * * @param x Value to round. * @param scale Number of digits to the right of the decimal point. * @param roundingMethod Rounding method as defined in {@link BigDecimal}. * @return the rounded value. * @throws ArithmeticException if {@code roundingMethod == ROUND_UNNECESSARY} * and the specified scaling operation would require rounding. * @throws IllegalArgumentException if {@code roundingMethod} does not * represent a valid rounding mode. * @since 1.1 (previously in {@code MathUtils}, moved as of version 3.0) */ public static double round(double x, int scale, int roundingMethod) { try { return (new BigDecimal (Double.toString(x)) .setScale(scale, roundingMethod)) .doubleValue(); } catch (NumberFormatException ex) { if (Double.isInfinite(x)) { return x; } else { return Double.NaN; } } } /** * Rounds the given value to the specified number of decimal places. * The value is rounded using the {@link BigDecimal#ROUND_HALF_UP} method. * * @param x Value to round. * @param scale Number of digits to the right of the decimal point. * @return the rounded value. * @since 1.1 (previously in {@code MathUtils}, moved as of version 3.0) */ public static float round(float x, int scale) { return round(x, scale, BigDecimal.ROUND_HALF_UP); } /** * Rounds the given value to the specified number of decimal places. * The value is rounded using the given method which is any method defined * in {@link BigDecimal}. * * @param x Value to round. * @param scale Number of digits to the right of the decimal point. * @param roundingMethod Rounding method as defined in {@link BigDecimal}. * @return the rounded value. * @since 1.1 (previously in {@code MathUtils}, moved as of version 3.0) * @throws MathArithmeticException if an exact operation is required but result is not exact * @throws MathIllegalArgumentException if {@code roundingMethod} is not a valid rounding method. */ public static float round(float x, int scale, int roundingMethod) throws MathArithmeticException, MathIllegalArgumentException { final float sign = FastMath.copySign(1f, x); final float factor = (float) FastMath.pow(10.0f, scale) * sign; return (float) roundUnscaled(x * factor, sign, roundingMethod) / factor; } /** * Rounds the given non-negative value to the "nearest" integer. Nearest is * determined by the rounding method specified. Rounding methods are defined * in {@link BigDecimal}. * * @param unscaled Value to round. * @param sign Sign of the original, scaled value. * @param roundingMethod Rounding method, as defined in {@link BigDecimal}. * @return the rounded value. * @throws MathArithmeticException if an exact operation is required but result is not exact * @throws MathIllegalArgumentException if {@code roundingMethod} is not a valid rounding method. * @since 1.1 (previously in {@code MathUtils}, moved as of version 3.0) */ private static double roundUnscaled(double unscaled, double sign, int roundingMethod) throws MathArithmeticException, MathIllegalArgumentException { switch (roundingMethod) { case BigDecimal.ROUND_CEILING : if (sign == -1) { unscaled = FastMath.floor(FastMath.nextAfter(unscaled, Double.NEGATIVE_INFINITY)); } else { unscaled = FastMath.ceil(FastMath.nextAfter(unscaled, Double.POSITIVE_INFINITY)); } break; case BigDecimal.ROUND_DOWN : unscaled = FastMath.floor(FastMath.nextAfter(unscaled, Double.NEGATIVE_INFINITY)); break; case BigDecimal.ROUND_FLOOR : if (sign == -1) { unscaled = FastMath.ceil(FastMath.nextAfter(unscaled, Double.POSITIVE_INFINITY)); } else { unscaled = FastMath.floor(FastMath.nextAfter(unscaled, Double.NEGATIVE_INFINITY)); } break; case BigDecimal.ROUND_HALF_DOWN : { unscaled = FastMath.nextAfter(unscaled, Double.NEGATIVE_INFINITY); double fraction = unscaled - FastMath.floor(unscaled); if (fraction > 0.5) { unscaled = FastMath.ceil(unscaled); } else { unscaled = FastMath.floor(unscaled); } break; } case BigDecimal.ROUND_HALF_EVEN : { double fraction = unscaled - FastMath.floor(unscaled); if (fraction > 0.5) { unscaled = FastMath.ceil(unscaled); } else if (fraction < 0.5) { unscaled = FastMath.floor(unscaled); } else { // The following equality test is intentional and needed for rounding purposes if (FastMath.floor(unscaled) / 2.0 == FastMath.floor(Math .floor(unscaled) / 2.0)) { // even unscaled = FastMath.floor(unscaled); } else { // odd unscaled = FastMath.ceil(unscaled); } } break; } case BigDecimal.ROUND_HALF_UP : { unscaled = FastMath.nextAfter(unscaled, Double.POSITIVE_INFINITY); double fraction = unscaled - FastMath.floor(unscaled); if (fraction >= 0.5) { unscaled = FastMath.ceil(unscaled); } else { unscaled = FastMath.floor(unscaled); } break; } case BigDecimal.ROUND_UNNECESSARY : if (unscaled != FastMath.floor(unscaled)) { throw new MathArithmeticException(); } break; case BigDecimal.ROUND_UP : unscaled = FastMath.ceil(FastMath.nextAfter(unscaled, Double.POSITIVE_INFINITY)); break; default : throw new MathIllegalArgumentException(LocalizedFormats.INVALID_ROUNDING_METHOD, roundingMethod, "ROUND_CEILING", BigDecimal.ROUND_CEILING, "ROUND_DOWN", BigDecimal.ROUND_DOWN, "ROUND_FLOOR", BigDecimal.ROUND_FLOOR, "ROUND_HALF_DOWN", BigDecimal.ROUND_HALF_DOWN, "ROUND_HALF_EVEN", BigDecimal.ROUND_HALF_EVEN, "ROUND_HALF_UP", BigDecimal.ROUND_HALF_UP, "ROUND_UNNECESSARY", BigDecimal.ROUND_UNNECESSARY, "ROUND_UP", BigDecimal.ROUND_UP); } return unscaled; } /** * Computes a number {@code delta} close to {@code originalDelta} with * the property that
                                    
                                         *   x + delta - x
                                         * 
                                    * is exactly machine-representable. * This is useful when computing numerical derivatives, in order to reduce * roundoff errors. * * @param x Value. * @param originalDelta Offset value. * @return a number {@code delta} so that {@code x + delta} and {@code x} * differ by a representable floating number. */ public static double representableDelta(double x, double originalDelta) { return x + originalDelta - x; } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/util/Incrementor.java100644 1750 1750 13006 12126627720 26646 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.util; import org.apache.commons.math3.exception.MaxCountExceededException; import org.apache.commons.math3.exception.NullArgumentException; /** * Utility that increments a counter until a maximum is reached, at * which point, the instance will by default throw a * {@link MaxCountExceededException}. * However, the user is able to override this behaviour by defining a * custom {@link MaxCountExceededCallback callback}, in order to e.g. * select which exception must be thrown. * * @since 3.0 * @version $Id: Incrementor.java 1455194 2013-03-11 15:45:54Z luc $ */ public class Incrementor { /** * Upper limit for the counter. */ private int maximalCount; /** * Current count. */ private int count = 0; /** * Function called at counter exhaustion. */ private final MaxCountExceededCallback maxCountCallback; /** * Default constructor. * For the new instance to be useful, the maximal count must be set * by calling {@link #setMaximalCount(int) setMaximalCount}. */ public Incrementor() { this(0); } /** * Defines a maximal count. * * @param max Maximal count. */ public Incrementor(int max) { this(max, new MaxCountExceededCallback() { /** {@inheritDoc} */ public void trigger(int max) throws MaxCountExceededException { throw new MaxCountExceededException(max); } }); } /** * Defines a maximal count and a callback method to be triggered at * counter exhaustion. * * @param max Maximal count. * @param cb Function to be called when the maximal count has been reached. * @throws NullArgumentException if {@code cb} is {@code null} */ public Incrementor(int max, MaxCountExceededCallback cb) throws NullArgumentException { if (cb == null){ throw new NullArgumentException(); } maximalCount = max; maxCountCallback = cb; } /** * Sets the upper limit for the counter. * This does not automatically reset the current count to zero (see * {@link #resetCount()}). * * @param max Upper limit of the counter. */ public void setMaximalCount(int max) { maximalCount = max; } /** * Gets the upper limit of the counter. * * @return the counter upper limit. */ public int getMaximalCount() { return maximalCount; } /** * Gets the current count. * * @return the current count. */ public int getCount() { return count; } /** * Checks whether a single increment is allowed. * * @return {@code false} if the next call to {@link #incrementCount(int) * incrementCount} will trigger a {@code MaxCountExceededException}, * {@code true} otherwise. */ public boolean canIncrement() { return count < maximalCount; } /** * Performs multiple increments. * See the other {@link #incrementCount() incrementCount} method). * * @param value Number of increments. * @throws MaxCountExceededException at counter exhaustion. */ public void incrementCount(int value) throws MaxCountExceededException { for (int i = 0; i < value; i++) { incrementCount(); } } /** * Adds one to the current iteration count. * At counter exhaustion, this method will call the * {@link MaxCountExceededCallback#trigger(int) trigger} method of the * callback object passed to the * {@link #Incrementor(int,MaxCountExceededCallback) constructor}. * If not explictly set, a default callback is used that will throw * a {@code MaxCountExceededException}. * * @throws MaxCountExceededException at counter exhaustion, unless a * custom {@link MaxCountExceededCallback callback} has been set at * construction. */ public void incrementCount() throws MaxCountExceededException { if (++count > maximalCount) { maxCountCallback.trigger(maximalCount); } } /** * Resets the counter to 0. */ public void resetCount() { count = 0; } /** * Defines a method to be called at counter exhaustion. * The {@link #trigger(int) trigger} method should usually throw an exception. */ public interface MaxCountExceededCallback { /** * Function called when the maximal count has been reached. * * @param maximalCount Maximal count. * @throws MaxCountExceededException at counter exhaustion */ void trigger(int maximalCount) throws MaxCountExceededException; } } ././@LongLink100644 0 0 152 12126630647 10260 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/genetics/InvalidRepresentationException.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/genetics/InvalidRepresentationException100644 1750 1750 3143 12126627715 32442 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.genetics; import org.apache.commons.math3.exception.MathIllegalArgumentException; import org.apache.commons.math3.exception.util.Localizable; /** * Exception indicating that the representation of a chromosome is not valid. * * @version $Id: InvalidRepresentationException.java 1416643 2012-12-03 19:37:14Z tn $ * @since 2.0 */ public class InvalidRepresentationException extends MathIllegalArgumentException { /** Serialization version id */ private static final long serialVersionUID = 1L; /** * Construct an InvalidRepresentationException with a specialized message. * * @param pattern Message pattern. * @param args Arguments. */ public InvalidRepresentationException(Localizable pattern, Object ... args) { super(pattern, args); } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/genetics/package-info.java100644 1750 1750 1642 12126627715 27520 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ /** * This package provides Genetic Algorithms components and implementations. */ package org.apache.commons.math3.genetics; commons-math3-3.2-src/src/main/java/org/apache/commons/math3/genetics/RandomKeyMutation.java100644 1750 1750 4324 12126627715 30606 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.genetics; import java.util.ArrayList; import java.util.List; import org.apache.commons.math3.exception.MathIllegalArgumentException; import org.apache.commons.math3.exception.util.LocalizedFormats; /** * Mutation operator for {@link RandomKey}s. Changes a randomly chosen element * of the array representation to a random value uniformly distributed in [0,1]. * * @since 2.0 * @version $Id: RandomKeyMutation.java 1416643 2012-12-03 19:37:14Z tn $ */ public class RandomKeyMutation implements MutationPolicy { /** * {@inheritDoc} * * @throws MathIllegalArgumentException if original is not a {@link RandomKey} instance */ public Chromosome mutate(final Chromosome original) throws MathIllegalArgumentException { if (!(original instanceof RandomKey)) { throw new MathIllegalArgumentException(LocalizedFormats.RANDOMKEY_MUTATION_WRONG_CLASS, original.getClass().getSimpleName()); } RandomKey originalRk = (RandomKey) original; List repr = originalRk.getRepresentation(); int rInd = GeneticAlgorithm.getRandomGenerator().nextInt(repr.size()); List newRepr = new ArrayList (repr); newRepr.set(rInd, GeneticAlgorithm.getRandomGenerator().nextDouble()); return originalRk.newFixedLengthChromosome(newRepr); } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/genetics/UniformCrossover.java100644 1750 1750 13617 12126627715 30546 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.genetics; import java.util.ArrayList; import java.util.List; import org.apache.commons.math3.exception.DimensionMismatchException; import org.apache.commons.math3.exception.MathIllegalArgumentException; import org.apache.commons.math3.exception.OutOfRangeException; import org.apache.commons.math3.exception.util.LocalizedFormats; import org.apache.commons.math3.random.RandomGenerator; /** * Perform Uniform Crossover [UX] on the specified chromosomes. A fixed mixing * ratio is used to combine genes from the first and second parents, e.g. using a * ratio of 0.5 would result in approximately 50% of genes coming from each * parent. This is typically a poor method of crossover, but empirical evidence * suggests that it is more exploratory and results in a larger part of the * problem space being searched. *

                                    * This crossover policy evaluates each gene of the parent chromosomes by chosing a * uniform random number {@code p} in the range [0, 1]. If {@code p} < {@code ratio}, * the parent genes are swapped. This means with a ratio of 0.7, 30% of the genes from the * first parent and 70% from the second parent will be selected for the first offspring (and * vice versa for the second offspring). *

                                    * This policy works only on {@link AbstractListChromosome}, and therefore it * is parameterized by T. Moreover, the chromosomes must have same lengths. * * @see Crossover techniques (Wikipedia) * @see Crossover (Obitko.com) * @see Uniform crossover * @param generic type of the {@link AbstractListChromosome}s for crossover * @since 3.1 * @version $Id: UniformCrossover.java 1385297 2012-09-16 16:05:57Z tn $ */ public class UniformCrossover implements CrossoverPolicy { /** The mixing ratio. */ private final double ratio; /** * Creates a new {@link UniformCrossover} policy using the given mixing ratio. * * @param ratio the mixing ratio * @throws OutOfRangeException if the mixing ratio is outside the [0, 1] range */ public UniformCrossover(final double ratio) throws OutOfRangeException { if (ratio < 0.0d || ratio > 1.0d) { throw new OutOfRangeException(LocalizedFormats.CROSSOVER_RATE, ratio, 0.0d, 1.0d); } this.ratio = ratio; } /** * Returns the mixing ratio used by this {@link CrossoverPolicy}. * * @return the mixing ratio */ public double getRatio() { return ratio; } /** * {@inheritDoc} * * @throws MathIllegalArgumentException iff one of the chromosomes is * not an instance of {@link AbstractListChromosome} * @throws DimensionMismatchException if the length of the two chromosomes is different */ @SuppressWarnings("unchecked") public ChromosomePair crossover(final Chromosome first, final Chromosome second) throws DimensionMismatchException, MathIllegalArgumentException { if (!(first instanceof AbstractListChromosome && second instanceof AbstractListChromosome)) { throw new MathIllegalArgumentException(LocalizedFormats.INVALID_FIXED_LENGTH_CHROMOSOME); } return mate((AbstractListChromosome) first, (AbstractListChromosome) second); } /** * Helper for {@link #crossover(Chromosome, Chromosome)}. Performs the actual crossover. * * @param first the first chromosome * @param second the second chromosome * @return the pair of new chromosomes that resulted from the crossover * @throws DimensionMismatchException if the length of the two chromosomes is different */ private ChromosomePair mate(final AbstractListChromosome first, final AbstractListChromosome second) throws DimensionMismatchException { final int length = first.getLength(); if (length != second.getLength()) { throw new DimensionMismatchException(second.getLength(), length); } // array representations of the parents final List parent1Rep = first.getRepresentation(); final List parent2Rep = second.getRepresentation(); // and of the children final List child1Rep = new ArrayList(first.getLength()); final List child2Rep = new ArrayList(second.getLength()); final RandomGenerator random = GeneticAlgorithm.getRandomGenerator(); for (int index = 0; index < length; index++) { if (random.nextDouble() < ratio) { // swap the bits -> take other parent child1Rep.add(parent2Rep.get(index)); child2Rep.add(parent1Rep.get(index)); } else { child1Rep.add(parent1Rep.get(index)); child2Rep.add(parent2Rep.get(index)); } } return new ChromosomePair(first.newFixedLengthChromosome(child1Rep), second.newFixedLengthChromosome(child2Rep)); } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/genetics/FixedGenerationCount.java100644 1750 1750 5335 12126627715 31263 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.genetics; import org.apache.commons.math3.exception.NumberIsTooSmallException; /** * Stops after a fixed number of generations. Each time {@link #isSatisfied(Population)} is invoked, a generation * counter is incremented. Once the counter reaches the configured maxGenerations value, * {@link #isSatisfied(Population)} returns true. * * @version $Id: FixedGenerationCount.java 1416643 2012-12-03 19:37:14Z tn $ * @since 2.0 */ public class FixedGenerationCount implements StoppingCondition { /** Number of generations that have passed */ private int numGenerations = 0; /** Maximum number of generations (stopping criteria) */ private final int maxGenerations; /** * Create a new FixedGenerationCount instance. * * @param maxGenerations number of generations to evolve * @throws NumberIsTooSmallException if the number of generations is < 1 */ public FixedGenerationCount(final int maxGenerations) throws NumberIsTooSmallException { if (maxGenerations <= 0) { throw new NumberIsTooSmallException(maxGenerations, 1, true); } this.maxGenerations = maxGenerations; } /** * Determine whether or not the given number of generations have passed. Increments the number of generations * counter if the maximum has not been reached. * * @param population ignored (no impact on result) * @return true IFF the maximum number of generations has been exceeded */ public boolean isSatisfied(final Population population) { if (this.numGenerations < this.maxGenerations) { numGenerations++; return false; } return true; } /** * Returns the number of generations that have already passed. * @return the number of generations that have passed */ public int getNumGenerations() { return numGenerations; } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/genetics/OnePointCrossover.java100644 1750 1750 13173 12126627715 30657 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.genetics; import java.util.ArrayList; import java.util.List; import org.apache.commons.math3.exception.DimensionMismatchException; import org.apache.commons.math3.exception.MathIllegalArgumentException; import org.apache.commons.math3.exception.util.LocalizedFormats; /** * One point crossover policy. A random crossover point is selected and the * first part from each parent is copied to the corresponding child, and the * second parts are copied crosswise. * * Example: *

                                     * -C- denotes a crossover point
                                     *                   -C-                                 -C-
                                     * p1 = (1 0 1 0 0 1  | 0 1 1)    X    p2 = (0 1 1 0 1 0  | 1 1 1)
                                     *      \------------/ \-----/              \------------/ \-----/
                                     *            ||         (*)                       ||        (**)
                                     *            VV         (**)                      VV        (*)
                                     *      /------------\ /-----\              /------------\ /-----\
                                     * c1 = (1 0 1 0 0 1  | 1 1 1)    X    c2 = (0 1 1 0 1 0  | 0 1 1)
                                     * 
                                    * * This policy works only on {@link AbstractListChromosome}, and therefore it * is parameterized by T. Moreover, the chromosomes must have same lengths. * * @param generic type of the {@link AbstractListChromosome}s for crossover * @since 2.0 * @version $Id: OnePointCrossover.java 1416643 2012-12-03 19:37:14Z tn $ * */ public class OnePointCrossover implements CrossoverPolicy { /** * Performs one point crossover. A random crossover point is selected and the * first part from each parent is copied to the corresponding child, and the * second parts are copied crosswise. * * Example: *
                                         * -C- denotes a crossover point
                                         *                   -C-                                 -C-
                                         * p1 = (1 0 1 0 0 1  | 0 1 1)    X    p2 = (0 1 1 0 1 0  | 1 1 1)
                                         *      \------------/ \-----/              \------------/ \-----/
                                         *            ||         (*)                       ||        (**)
                                         *            VV         (**)                      VV        (*)
                                         *      /------------\ /-----\              /------------\ /-----\
                                         * c1 = (1 0 1 0 0 1  | 1 1 1)    X    c2 = (0 1 1 0 1 0  | 0 1 1)
                                         * 
                                    * * @param first first parent (p1) * @param second second parent (p2) * @return pair of two children (c1,c2) * @throws MathIllegalArgumentException iff one of the chromosomes is * not an instance of {@link AbstractListChromosome} * @throws DimensionMismatchException if the length of the two chromosomes is different */ @SuppressWarnings("unchecked") // OK because of instanceof checks public ChromosomePair crossover(final Chromosome first, final Chromosome second) throws DimensionMismatchException, MathIllegalArgumentException { if (! (first instanceof AbstractListChromosome && second instanceof AbstractListChromosome)) { throw new MathIllegalArgumentException(LocalizedFormats.INVALID_FIXED_LENGTH_CHROMOSOME); } return crossover((AbstractListChromosome) first, (AbstractListChromosome) second); } /** * Helper for {@link #crossover(Chromosome, Chromosome)}. Performs the actual crossover. * * @param first the first chromosome. * @param second the second chromosome. * @return the pair of new chromosomes that resulted from the crossover. * @throws DimensionMismatchException if the length of the two chromosomes is different */ private ChromosomePair crossover(final AbstractListChromosome first, final AbstractListChromosome second) throws DimensionMismatchException { final int length = first.getLength(); if (length != second.getLength()) { throw new DimensionMismatchException(second.getLength(), length); } // array representations of the parents final List parent1Rep = first.getRepresentation(); final List parent2Rep = second.getRepresentation(); // and of the children final ArrayList child1Rep = new ArrayList (first.getLength()); final ArrayList child2Rep = new ArrayList (second.getLength()); // select a crossover point at random (0 and length makes no sense) final int crossoverIndex = 1 + (GeneticAlgorithm.getRandomGenerator().nextInt(length-2)); // copy the first part for (int i = 0; i < crossoverIndex; i++) { child1Rep.add(parent1Rep.get(i)); child2Rep.add(parent2Rep.get(i)); } // and switch the second part for (int i = crossoverIndex; i < length; i++) { child1Rep.add(parent2Rep.get(i)); child2Rep.add(parent1Rep.get(i)); } return new ChromosomePair(first.newFixedLengthChromosome(child1Rep), second.newFixedLengthChromosome(child2Rep)); } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/genetics/MutationPolicy.java100644 1750 1750 2631 12126627715 30153 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.genetics; import org.apache.commons.math3.exception.MathIllegalArgumentException; /** * Algorithm used to mutate a chromosome. * * @since 2.0 * @version $Id: MutationPolicy.java 1416643 2012-12-03 19:37:14Z tn $ */ public interface MutationPolicy { /** * Mutate the given chromosome. * @param original the original chromosome. * @return the mutated chromosome. * @throws MathIllegalArgumentException if the given chromosome is not compatible with this {@link MutationPolicy} */ Chromosome mutate(Chromosome original) throws MathIllegalArgumentException; } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/genetics/BinaryChromosome.java100644 1750 1750 6662 12126627715 30463 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.genetics; import java.util.ArrayList; import java.util.List; import org.apache.commons.math3.exception.util.LocalizedFormats; /** * Chromosome represented by a vector of 0s and 1s. * * @version $Id: BinaryChromosome.java 1416643 2012-12-03 19:37:14Z tn $ * @since 2.0 */ public abstract class BinaryChromosome extends AbstractListChromosome { /** * Constructor. * @param representation list of {0,1} values representing the chromosome * @throws InvalidRepresentationException iff the representation can not represent a valid chromosome */ public BinaryChromosome(List representation) throws InvalidRepresentationException { super(representation); } /** * Constructor. * @param representation array of {0,1} values representing the chromosome * @throws InvalidRepresentationException iff the representation can not represent a valid chromosome */ public BinaryChromosome(Integer[] representation) throws InvalidRepresentationException { super(representation); } /** * {@inheritDoc} */ @Override protected void checkValidity(List chromosomeRepresentation) throws InvalidRepresentationException { for (int i : chromosomeRepresentation) { if (i < 0 || i >1) { throw new InvalidRepresentationException(LocalizedFormats.INVALID_BINARY_DIGIT, i); } } } /** * Returns a representation of a random binary array of length length. * @param length length of the array * @return a random binary array of length length */ public static List randomBinaryRepresentation(int length) { // random binary list List rList= new ArrayList (length); for (int j=0; j chromosomes; /** maximal size of the population */ private int populationLimit; /** * Creates a new ListPopulation instance and initializes its inner chromosome list. * * @param populationLimit maximal size of the population * @throws NotPositiveException if the population limit is not a positive number (< 1) */ public ListPopulation(final int populationLimit) throws NotPositiveException { this(Collections. emptyList(), populationLimit); } /** * Creates a new ListPopulation instance. *

                                    * Note: the chromosomes of the specified list are added to the population. * * @param chromosomes list of chromosomes to be added to the population * @param populationLimit maximal size of the population * @throws NullArgumentException if the list of chromosomes is {@code null} * @throws NotPositiveException if the population limit is not a positive number (< 1) * @throws NumberIsTooLargeException if the list of chromosomes exceeds the population limit */ public ListPopulation(final List chromosomes, final int populationLimit) throws NullArgumentException, NotPositiveException, NumberIsTooLargeException { if (chromosomes == null) { throw new NullArgumentException(); } if (populationLimit <= 0) { throw new NotPositiveException(LocalizedFormats.POPULATION_LIMIT_NOT_POSITIVE, populationLimit); } if (chromosomes.size() > populationLimit) { throw new NumberIsTooLargeException(LocalizedFormats.LIST_OF_CHROMOSOMES_BIGGER_THAN_POPULATION_SIZE, chromosomes.size(), populationLimit, false); } this.populationLimit = populationLimit; this.chromosomes = new ArrayList(populationLimit); this.chromosomes.addAll(chromosomes); } /** * Sets the list of chromosomes. *

                                    * Note: this method removed all existing chromosomes in the population and adds all chromosomes * of the specified list to the population. * * @param chromosomes the list of chromosomes * @throws NullArgumentException if the list of chromosomes is {@code null} * @throws NumberIsTooLargeException if the list of chromosomes exceeds the population limit * @deprecated use {@link #addChromosomes(Collection)} instead */ @Deprecated public void setChromosomes(final List chromosomes) throws NullArgumentException, NumberIsTooLargeException { if (chromosomes == null) { throw new NullArgumentException(); } if (chromosomes.size() > populationLimit) { throw new NumberIsTooLargeException(LocalizedFormats.LIST_OF_CHROMOSOMES_BIGGER_THAN_POPULATION_SIZE, chromosomes.size(), populationLimit, false); } this.chromosomes.clear(); this.chromosomes.addAll(chromosomes); } /** * Add a {@link Collection} of chromosomes to this {@link Population}. * @param chromosomeColl a {@link Collection} of chromosomes * @throws NumberIsTooLargeException if the population would exceed the population limit when * adding this chromosome * @since 3.1 */ public void addChromosomes(final Collection chromosomeColl) throws NumberIsTooLargeException { if (chromosomes.size() + chromosomeColl.size() > populationLimit) { throw new NumberIsTooLargeException(LocalizedFormats.LIST_OF_CHROMOSOMES_BIGGER_THAN_POPULATION_SIZE, chromosomes.size(), populationLimit, false); } this.chromosomes.addAll(chromosomeColl); } /** * Returns an unmodifiable list of the chromosomes in this population. * @return the unmodifiable list of chromosomes */ public List getChromosomes() { return Collections.unmodifiableList(chromosomes); } /** * Access the list of chromosomes. * @return the list of chromosomes * @since 3.1 */ protected List getChromosomeList() { return chromosomes; } /** * Add the given chromosome to the population. * * @param chromosome the chromosome to add. * @throws NumberIsTooLargeException if the population would exceed the {@code populationLimit} after * adding this chromosome */ public void addChromosome(final Chromosome chromosome) throws NumberIsTooLargeException { if (chromosomes.size() >= populationLimit) { throw new NumberIsTooLargeException(LocalizedFormats.LIST_OF_CHROMOSOMES_BIGGER_THAN_POPULATION_SIZE, chromosomes.size(), populationLimit, false); } this.chromosomes.add(chromosome); } /** * Access the fittest chromosome in this population. * @return the fittest chromosome. */ public Chromosome getFittestChromosome() { // best so far Chromosome bestChromosome = this.chromosomes.get(0); for (Chromosome chromosome : this.chromosomes) { if (chromosome.compareTo(bestChromosome) > 0) { // better chromosome found bestChromosome = chromosome; } } return bestChromosome; } /** * Access the maximum population size. * @return the maximum population size. */ public int getPopulationLimit() { return this.populationLimit; } /** * Sets the maximal population size. * @param populationLimit maximal population size. * @throws NotPositiveException if the population limit is not a positive number (< 1) * @throws NumberIsTooSmallException if the new population size is smaller than the current number * of chromosomes in the population */ public void setPopulationLimit(final int populationLimit) throws NotPositiveException, NumberIsTooSmallException { if (populationLimit <= 0) { throw new NotPositiveException(LocalizedFormats.POPULATION_LIMIT_NOT_POSITIVE, populationLimit); } if (populationLimit < chromosomes.size()) { throw new NumberIsTooSmallException(populationLimit, chromosomes.size(), true); } this.populationLimit = populationLimit; } /** * Access the current population size. * @return the current population size. */ public int getPopulationSize() { return this.chromosomes.size(); } /** * {@inheritDoc} */ @Override public String toString() { return this.chromosomes.toString(); } /** * Returns an iterator over the unmodifiable list of chromosomes. *

                                    Any call to {@link Iterator#remove()} will result in a {@link UnsupportedOperationException}.

                                    * * @return chromosome iterator */ public Iterator iterator() { return getChromosomes().iterator(); } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/genetics/TournamentSelection.java100644 1750 1750 11071 12126627715 31213 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.genetics; import java.util.ArrayList; import java.util.List; import org.apache.commons.math3.exception.MathIllegalArgumentException; import org.apache.commons.math3.exception.util.LocalizedFormats; /** * Tournament selection scheme. Each of the two selected chromosomes is selected * based on n-ary tournament -- this is done by drawing {@link #arity} random * chromosomes without replacement from the population, and then selecting the * fittest chromosome among them. * * @since 2.0 * @version $Id: TournamentSelection.java 1416643 2012-12-03 19:37:14Z tn $ */ public class TournamentSelection implements SelectionPolicy { /** number of chromosomes included in the tournament selections */ private int arity; /** * Creates a new TournamentSelection instance. * * @param arity how many chromosomes will be drawn to the tournament */ public TournamentSelection(final int arity) { this.arity = arity; } /** * Select two chromosomes from the population. Each of the two selected * chromosomes is selected based on n-ary tournament -- this is done by * drawing {@link #arity} random chromosomes without replacement from the * population, and then selecting the fittest chromosome among them. * * @param population the population from which the chromosomes are chosen. * @return the selected chromosomes. * @throws MathIllegalArgumentException if the tournament arity is bigger than the population size */ public ChromosomePair select(final Population population) throws MathIllegalArgumentException { return new ChromosomePair(tournament((ListPopulation) population), tournament((ListPopulation) population)); } /** * Helper for {@link #select(Population)}. Draw {@link #arity} random chromosomes without replacement from the * population, and then select the fittest chromosome among them. * * @param population the population from which the chromosomes are choosen. * @return the selected chromosome. * @throws MathIllegalArgumentException if the tournament arity is bigger than the population size */ private Chromosome tournament(final ListPopulation population) throws MathIllegalArgumentException { if (population.getPopulationSize() < this.arity) { throw new MathIllegalArgumentException(LocalizedFormats.TOO_LARGE_TOURNAMENT_ARITY, arity, population.getPopulationSize()); } // auxiliary population ListPopulation tournamentPopulation = new ListPopulation(this.arity) { public Population nextGeneration() { // not useful here return null; } }; // create a copy of the chromosome list List chromosomes = new ArrayList (population.getChromosomes()); for (int i=0; i type of the permuted objects * @since 2.0 * @version $Id: PermutationChromosome.java 1416643 2012-12-03 19:37:14Z tn $ */ public interface PermutationChromosome { /** * Permutes the sequence of objects of type T according to the * permutation this chromosome represents. For example, if this chromosome * represents a permutation (3,0,1,2), and the unpermuted sequence is * (a,b,c,d), this yields (d,a,b,c). * * @param sequence the unpermuted (original) sequence of objects * @return permutation of sequence represented by this permutation */ List decode(List sequence); } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/genetics/AbstractListChromosome.java100644 1750 1750 7327 12126627715 31635 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.genetics; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.List; /** * Chromosome represented by an immutable list of a fixed length. * * @param type of the representation list * @version $Id: AbstractListChromosome.java 1416643 2012-12-03 19:37:14Z tn $ * @since 2.0 */ public abstract class AbstractListChromosome extends Chromosome { /** List representing the chromosome */ private final List representation; /** * Constructor. * @param representation inner representation of the chromosome * @throws InvalidRepresentationException iff the representation can not represent a valid chromosome */ public AbstractListChromosome(final List representation) throws InvalidRepresentationException { checkValidity(representation); this.representation = Collections.unmodifiableList(new ArrayList (representation)); } /** * Constructor. * @param representation inner representation of the chromosome * @throws InvalidRepresentationException iff the representation can not represent a valid chromosome */ public AbstractListChromosome(final T[] representation) throws InvalidRepresentationException { this(Arrays.asList(representation)); } /** * Asserts that representation can represent a valid chromosome. * * @param chromosomeRepresentation representation of the chromosome * @throws InvalidRepresentationException iff the representation can not represent a valid chromosome */ protected abstract void checkValidity(List chromosomeRepresentation) throws InvalidRepresentationException; /** * Returns the (immutable) inner representation of the chromosome. * @return the representation of the chromosome */ protected List getRepresentation() { return representation; } /** * Returns the length of the chromosome. * @return the length of the chromosome */ public int getLength() { return getRepresentation().size(); } /** * Creates a new instance of the same class as this is, with a given arrayRepresentation. * This is needed in crossover and mutation operators, where we need a new instance of the same class, but with * different array representation. *

                                    * Usually, this method just calls a constructor of the class. * * @param chromosomeRepresentation the inner array representation of the new chromosome. * @return new instance extended from FixedLengthChromosome with the given arrayRepresentation */ public abstract AbstractListChromosome newFixedLengthChromosome(final List chromosomeRepresentation); @Override public String toString() { return String.format("(f=%s %s)", getFitness(), getRepresentation()); } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/genetics/ChromosomePair.java100644 1750 1750 3545 12126627715 30127 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.genetics; /** * A pair of {@link Chromosome} objects. * @since 2.0 * * @version $Id: ChromosomePair.java 1416643 2012-12-03 19:37:14Z tn $ */ public class ChromosomePair { /** the first chromosome in the pair. */ private final Chromosome first; /** the second chromosome in the pair. */ private final Chromosome second; /** * Create a chromosome pair. * * @param c1 the first chromosome. * @param c2 the second chromosome. */ public ChromosomePair(final Chromosome c1, final Chromosome c2) { super(); first = c1; second = c2; } /** * Access the first chromosome. * * @return the first chromosome. */ public Chromosome getFirst() { return first; } /** * Access the second chromosome. * * @return the second chromosome. */ public Chromosome getSecond() { return second; } @Override public String toString() { return String.format("(%s,%s)", getFirst(), getSecond()); } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/genetics/StoppingCondition.java100644 1750 1750 2542 12126627715 30646 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.genetics; /** * Algorithm used to determine when to stop evolution. * * @since 2.0 * @version $Id: StoppingCondition.java 1416643 2012-12-03 19:37:14Z tn $ */ public interface StoppingCondition { /** * Determine whether or not the given population satisfies the stopping condition. * * @param population the population to test. * @return true if this stopping condition is met by the given population, * false otherwise. */ boolean isSatisfied(Population population); } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/genetics/SelectionPolicy.java100644 1750 1750 2743 12126627715 30304 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.genetics; import org.apache.commons.math3.exception.MathIllegalArgumentException; /** * Algorithm used to select a chromosome pair from a population. * * @since 2.0 * @version $Id: SelectionPolicy.java 1416643 2012-12-03 19:37:14Z tn $ */ public interface SelectionPolicy { /** * Select two chromosomes from the population. * @param population the population from which the chromosomes are choosen. * @return the selected chromosomes. * @throws MathIllegalArgumentException if the population is not compatible with this {@link SelectionPolicy} */ ChromosomePair select(Population population) throws MathIllegalArgumentException; } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/genetics/OrderedCrossover.java100644 1750 1750 14362 12126627715 30511 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.genetics; import java.util.ArrayList; import java.util.Collections; import java.util.HashSet; import java.util.List; import java.util.Set; import org.apache.commons.math3.exception.DimensionMismatchException; import org.apache.commons.math3.exception.MathIllegalArgumentException; import org.apache.commons.math3.exception.util.LocalizedFormats; import org.apache.commons.math3.random.RandomGenerator; import org.apache.commons.math3.util.FastMath; /** * Order 1 Crossover [OX1] builds offspring from ordered chromosomes by copying a * consecutive slice from one parent, and filling up the remaining genes from the other * parent as they appear. *

                                    * This policy works by applying the following rules: *

                                      *
                                    1. select a random slice of consecutive genes from parent 1
                                    2. *
                                    3. copy the slice to child 1 and mark out the genes in parent 2
                                    4. *
                                    5. starting from the right side of the slice, copy genes from parent 2 as they * appear to child 1 if they are not yet marked out.
                                    6. *
                                    *

                                    * Example (random sublist from index 3 to 7, underlined): *

                                     * p1 = (8 4 7 3 6 2 5 1 9 0)   X   c1 = (0 4 7 3 6 2 5 1 8 9)
                                     *             ---------                        ---------
                                     * p2 = (0 1 2 3 4 5 6 7 8 9)   X   c2 = (8 1 2 3 4 5 6 7 9 0)
                                     * 
                                    *

                                    * This policy works only on {@link AbstractListChromosome}, and therefore it * is parameterized by T. Moreover, the chromosomes must have same lengths. * * @see * Order 1 Crossover Operator * * @param generic type of the {@link AbstractListChromosome}s for crossover * @since 3.1 * @version $Id: OrderedCrossover.java 1385297 2012-09-16 16:05:57Z tn $ */ public class OrderedCrossover implements CrossoverPolicy { /** * {@inheritDoc} * * @throws MathIllegalArgumentException iff one of the chromosomes is * not an instance of {@link AbstractListChromosome} * @throws DimensionMismatchException if the length of the two chromosomes is different */ @SuppressWarnings("unchecked") public ChromosomePair crossover(final Chromosome first, final Chromosome second) throws DimensionMismatchException, MathIllegalArgumentException { if (!(first instanceof AbstractListChromosome && second instanceof AbstractListChromosome)) { throw new MathIllegalArgumentException(LocalizedFormats.INVALID_FIXED_LENGTH_CHROMOSOME); } return mate((AbstractListChromosome) first, (AbstractListChromosome) second); } /** * Helper for {@link #crossover(Chromosome, Chromosome)}. Performs the actual crossover. * * @param first the first chromosome * @param second the second chromosome * @return the pair of new chromosomes that resulted from the crossover * @throws DimensionMismatchException if the length of the two chromosomes is different */ protected ChromosomePair mate(final AbstractListChromosome first, final AbstractListChromosome second) throws DimensionMismatchException { final int length = first.getLength(); if (length != second.getLength()) { throw new DimensionMismatchException(second.getLength(), length); } // array representations of the parents final List parent1Rep = first.getRepresentation(); final List parent2Rep = second.getRepresentation(); // and of the children final List child1 = new ArrayList(length); final List child2 = new ArrayList(length); // sets of already inserted items for quick access final Set child1Set = new HashSet(length); final Set child2Set = new HashSet(length); final RandomGenerator random = GeneticAlgorithm.getRandomGenerator(); // choose random points, making sure that lb < ub. int a = random.nextInt(length); int b; do { b = random.nextInt(length); } while (a == b); // determine the lower and upper bounds final int lb = FastMath.min(a, b); final int ub = FastMath.max(a, b); // add the subLists that are between lb and ub child1.addAll(parent1Rep.subList(lb, ub + 1)); child1Set.addAll(child1); child2.addAll(parent2Rep.subList(lb, ub + 1)); child2Set.addAll(child2); // iterate over every item in the parents for (int i = 1; i <= length; i++) { final int idx = (ub + i) % length; // retrieve the current item in each parent final T item1 = parent1Rep.get(idx); final T item2 = parent2Rep.get(idx); // if the first child already contains the item in the second parent add it if (!child1Set.contains(item2)) { child1.add(item2); child1Set.add(item2); } // if the second child already contains the item in the first parent add it if (!child2Set.contains(item1)) { child2.add(item1); child2Set.add(item1); } } // rotate so that the original slice is in the same place as in the parents. Collections.rotate(child1, lb); Collections.rotate(child2, lb); return new ChromosomePair(first.newFixedLengthChromosome(child1), second.newFixedLengthChromosome(child2)); } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/genetics/RandomKey.java100644 1750 1750 26726 12126627715 27117 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.genetics; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.Comparator; import java.util.List; import org.apache.commons.math3.exception.DimensionMismatchException; import org.apache.commons.math3.exception.MathIllegalArgumentException; import org.apache.commons.math3.exception.util.LocalizedFormats; /** * Random Key chromosome is used for permutation representation. It is a vector * of a fixed length of real numbers in [0,1] interval. The index of the i-th * smallest value in the vector represents an i-th member of the permutation. *

                                    * For example, the random key [0.2, 0.3, 0.8, 0.1] corresponds to the * permutation of indices (3,0,1,2). If the original (unpermuted) sequence would * be (a,b,c,d), this would mean the sequence (d,a,b,c). *

                                    * With this representation, common operators like n-point crossover can be * used, because any such chromosome represents a valid permutation. *

                                    * Since the chromosome (and thus its arrayRepresentation) is immutable, the * array representation is sorted only once in the constructor. *

                                    * For details, see: *

                                      *
                                    • Bean, J.C.: Genetic algorithms and random keys for sequencing and * optimization. ORSA Journal on Computing 6 (1994) 154-160
                                    • *
                                    • Rothlauf, F.: Representations for Genetic and Evolutionary Algorithms. * Volume 104 of Studies in Fuzziness and Soft Computing. Physica-Verlag, * Heidelberg (2002)
                                    • *
                                    * * @param type of the permuted objects * @since 2.0 * @version $Id: RandomKey.java 1416643 2012-12-03 19:37:14Z tn $ */ public abstract class RandomKey extends AbstractListChromosome implements PermutationChromosome { /** Cache of sorted representation (unmodifiable). */ private final List sortedRepresentation; /** * Base sequence [0,1,...,n-1], permuted accorting to the representation (unmodifiable). */ private final List baseSeqPermutation; /** * Constructor. * * @param representation list of [0,1] values representing the permutation * @throws InvalidRepresentationException iff the representation can not represent a valid chromosome */ public RandomKey(final List representation) throws InvalidRepresentationException { super(representation); // store the sorted representation List sortedRepr = new ArrayList (getRepresentation()); Collections.sort(sortedRepr); sortedRepresentation = Collections.unmodifiableList(sortedRepr); // store the permutation of [0,1,...,n-1] list for toString() and isSame() methods baseSeqPermutation = Collections.unmodifiableList( decodeGeneric(baseSequence(getLength()), getRepresentation(), sortedRepresentation) ); } /** * Constructor. * * @param representation array of [0,1] values representing the permutation * @throws InvalidRepresentationException iff the representation can not represent a valid chromosome */ public RandomKey(final Double[] representation) throws InvalidRepresentationException { this(Arrays.asList(representation)); } /** * {@inheritDoc} */ public List decode(final List sequence) { return decodeGeneric(sequence, getRepresentation(), sortedRepresentation); } /** * Decodes a permutation represented by representation and * returns a (generic) list with the permuted values. * * @param generic type of the sequence values * @param sequence the unpermuted sequence * @param representation representation of the permutation ([0,1] vector) * @param sortedRepr sorted representation * @return list with the sequence values permuted according to the representation * @throws DimensionMismatchException iff the length of the sequence, * representation or sortedRepr lists are not equal */ private static List decodeGeneric(final List sequence, List representation, final List sortedRepr) throws DimensionMismatchException { int l = sequence.size(); // the size of the three lists must be equal if (representation.size() != l) { throw new DimensionMismatchException(representation.size(), l); } if (sortedRepr.size() != l) { throw new DimensionMismatchException(sortedRepr.size(), l); } // do not modify the original representation List reprCopy = new ArrayList (representation); // now find the indices in the original repr and use them for permuting List res = new ArrayList (l); for (int i=0; itrue iff another is a RandomKey and * encodes the same permutation. * * @param another chromosome to compare * @return true iff chromosomes encode the same permutation */ @Override protected boolean isSame(final Chromosome another) { // type check if (! (another instanceof RandomKey)) { return false; } RandomKey anotherRk = (RandomKey) another; // size check if (getLength() != anotherRk.getLength()) { return false; } // two different representations can still encode the same permutation // the ordering is what counts List thisPerm = this.baseSeqPermutation; List anotherPerm = anotherRk.baseSeqPermutation; for (int i=0; i chromosomeRepresentation) throws InvalidRepresentationException { for (double val : chromosomeRepresentation) { if (val < 0 || val > 1) { throw new InvalidRepresentationException(LocalizedFormats.OUT_OF_RANGE_SIMPLE, val, 0, 1); } } } /** * Generates a representation corresponding to a random permutation of * length l which can be passed to the RandomKey constructor. * * @param l length of the permutation * @return representation of a random permutation */ public static final List randomPermutation(final int l) { List repr = new ArrayList(l); for (int i=0; i identityPermutation(final int l) { List repr = new ArrayList(l); for (int i=0; idata sorted by comparator. The * data is not modified during the process. * * This is useful if you want to inject some permutations to the initial * population. * * @param type of the data * @param data list of data determining the order * @param comparator how the data will be compared * @return list representation of the permutation corresponding to the parameters */ public static List comparatorPermutation(final List data, final Comparator comparator) { List sortedData = new ArrayList(data); Collections.sort(sortedData, comparator); return inducedPermutation(data, sortedData); } /** * Generates a representation of a permutation corresponding to a * permutation which yields permutedData when applied to * originalData. * * This method can be viewed as an inverse to {@link #decode(List)}. * * @param type of the data * @param originalData the original, unpermuted data * @param permutedData the data, somehow permuted * @return representation of a permutation corresponding to the permutation * originalData -> permutedData * @throws DimensionMismatchException iff the length of originalData * and permutedData lists are not equal * @throws MathIllegalArgumentException iff the permutedData and * originalData lists contain different data */ public static List inducedPermutation(final List originalData, final List permutedData) throws DimensionMismatchException, MathIllegalArgumentException { if (originalData.size() != permutedData.size()) { throw new DimensionMismatchException(permutedData.size(), originalData.size()); } int l = originalData.size(); List origDataCopy = new ArrayList (originalData); Double[] res = new Double[l]; for (int i=0; i baseSequence(final int l) { List baseSequence = new ArrayList (l); for (int i=0; ioriginal is not an instance of {@link BinaryChromosome}. */ public Chromosome mutate(Chromosome original) throws MathIllegalArgumentException { if (!(original instanceof BinaryChromosome)) { throw new MathIllegalArgumentException(LocalizedFormats.INVALID_BINARY_CHROMOSOME); } BinaryChromosome origChrom = (BinaryChromosome) original; List newRepr = new ArrayList(origChrom.getRepresentation()); // randomly select a gene int geneIndex = GeneticAlgorithm.getRandomGenerator().nextInt(origChrom.getLength()); // and change it newRepr.set(geneIndex, origChrom.getRepresentation().get(geneIndex) == 0 ? 1 : 0); Chromosome newChrom = origChrom.newFixedLengthChromosome(newRepr); return newChrom; } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/genetics/Population.java100644 1750 1750 3774 12126627715 27336 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.genetics; import org.apache.commons.math3.exception.NumberIsTooLargeException; /** * A collection of chromosomes that facilitates generational evolution. * * @since 2.0 * @version $Id: Population.java 1416643 2012-12-03 19:37:14Z tn $ */ public interface Population extends Iterable { /** * Access the current population size. * @return the current population size. */ int getPopulationSize(); /** * Access the maximum population size. * @return the maximum population size. */ int getPopulationLimit(); /** * Start the population for the next generation. * @return the beginnings of the next generation. */ Population nextGeneration(); /** * Add the given chromosome to the population. * @param chromosome the chromosome to add. * @throws NumberIsTooLargeException if the population would exceed the population limit when adding * this chromosome */ void addChromosome(Chromosome chromosome) throws NumberIsTooLargeException; /** * Access the fittest chromosome in this population. * @return the fittest chromosome. */ Chromosome getFittestChromosome(); } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/genetics/Chromosome.java100644 1750 1750 7666 12126627715 27323 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.genetics; /** * Individual in a population. Chromosomes are compared based on their fitness. *

                                    * The chromosomes are IMMUTABLE, and so their fitness is also immutable and * therefore it can be cached. * * @since 2.0 * @version $Id: Chromosome.java 1416643 2012-12-03 19:37:14Z tn $ */ public abstract class Chromosome implements Comparable,Fitness { /** Value assigned when no fitness has been computed yet. */ private static final double NO_FITNESS = Double.NEGATIVE_INFINITY; /** Cached value of the fitness of this chromosome. */ private double fitness = NO_FITNESS; /** * Access the fitness of this chromosome. The bigger the fitness, the better the chromosome. *

                                    * Computation of fitness is usually very time-consuming task, therefore the fitness is cached. * * @return the fitness */ public double getFitness() { if (this.fitness == NO_FITNESS) { // no cache - compute the fitness this.fitness = fitness(); } return this.fitness; } /** * Compares two chromosomes based on their fitness. The bigger the fitness, the better the chromosome. * * @param another another chromosome to compare * @return *

                                      *
                                    • -1 if another is better than this
                                    • *
                                    • 1 if another is worse than this
                                    • *
                                    • 0 if the two chromosomes have the same fitness
                                    • *
                                    */ public int compareTo(final Chromosome another) { return ((Double)this.getFitness()).compareTo(another.getFitness()); } /** * Returns true iff another has the same representation and therefore the same fitness. By * default, it returns false -- override it in your implementation if you need it. * * @param another chromosome to compare * @return true if another is equivalent to this chromosome */ protected boolean isSame(final Chromosome another) { return false; } /** * Searches the population for another chromosome with the same representation. If such chromosome is * found, it is returned, if no such chromosome exists, returns null. * * @param population Population to search * @return Chromosome with the same representation, or null if no such chromosome exists. */ protected Chromosome findSameChromosome(final Population population) { for (Chromosome anotherChr : population) { if (this.isSame(anotherChr)) { return anotherChr; } } return null; } /** * Searches the population for a chromosome representing the same solution, and if it finds one, * updates the fitness to its value. * * @param population Population to search */ public void searchForFitnessUpdate(final Population population) { Chromosome sameChromosome = findSameChromosome(population); if (sameChromosome != null) { fitness = sameChromosome.getFitness(); } } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/genetics/CrossoverPolicy.java100644 1750 1750 3164 12126627715 30342 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.genetics; import org.apache.commons.math3.exception.MathIllegalArgumentException; /** * Policy used to create a pair of new chromosomes by performing a crossover * operation on a source pair of chromosomes. * * @since 2.0 * @version $Id: CrossoverPolicy.java 1416643 2012-12-03 19:37:14Z tn $ */ public interface CrossoverPolicy { /** * Perform a crossover operation on the given chromosomes. * * @param first the first chromosome. * @param second the second chromosome. * @return the pair of new chromosomes that resulted from the crossover. * @throws MathIllegalArgumentException if the given chromosomes are not compatible with this {@link CrossoverPolicy} */ ChromosomePair crossover(Chromosome first, Chromosome second) throws MathIllegalArgumentException; } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/genetics/CycleCrossover.java100644 1750 1750 16257 12126627715 30171 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.genetics; import java.util.ArrayList; import java.util.HashSet; import java.util.List; import java.util.Set; import org.apache.commons.math3.exception.DimensionMismatchException; import org.apache.commons.math3.exception.MathIllegalArgumentException; import org.apache.commons.math3.exception.util.LocalizedFormats; /** * Cycle Crossover [CX] builds offspring from ordered chromosomes by identifying cycles * between two parent chromosomes. To form the children, the cycles are copied from the * respective parents. *

                                    * To form a cycle the following procedure is applied: *

                                      *
                                    1. start with the first gene of parent 1
                                    2. *
                                    3. look at the gene at the same position of parent 2
                                    4. *
                                    5. go to the position with the same gene in parent 1
                                    6. *
                                    7. add this gene index to the cycle
                                    8. *
                                    9. repeat the steps 2-5 until we arrive at the starting gene of this cycle
                                    10. *
                                    * The indices that form a cycle are then used to form the children in alternating order, i.e. * in cycle 1, the genes of parent 1 are copied to child 1, while in cycle 2 the genes of parent 1 * are copied to child 2, and so forth ... *

                                    * * Example (zero-start cycle): *
                                     * p1 = (8 4 7 3 6 2 5 1 9 0)    X   c1 = (8 1 2 3 4 5 6 7 9 0)
                                     * p2 = (0 1 2 3 4 5 6 7 8 9)    X   c2 = (0 4 7 3 6 2 5 1 8 9)
                                     *
                                     * cycle 1: 8 0 9
                                     * cycle 2: 4 1 7 2 5 6
                                     * cycle 3: 3
                                     * 
                                    * * This policy works only on {@link AbstractListChromosome}, and therefore it * is parameterized by T. Moreover, the chromosomes must have same lengths. * * @see * Cycle Crossover Operator * * @param generic type of the {@link AbstractListChromosome}s for crossover * @since 3.1 * @version $Id: CycleCrossover.java 1385297 2012-09-16 16:05:57Z tn $ */ public class CycleCrossover implements CrossoverPolicy { /** If the start index shall be chosen randomly. */ private final boolean randomStart; /** * Creates a new {@link CycleCrossover} policy. */ public CycleCrossover() { this(false); } /** * Creates a new {@link CycleCrossover} policy using the given {@code randomStart} behavior. * * @param randomStart whether the start index shall be chosen randomly or be set to 0 */ public CycleCrossover(final boolean randomStart) { this.randomStart = randomStart; } /** * Returns whether the starting index is chosen randomly or set to zero. * * @return {@code true} if the starting index is chosen randomly, {@code false} otherwise */ public boolean isRandomStart() { return randomStart; } /** * {@inheritDoc} * * @throws MathIllegalArgumentException if the chromosomes are not an instance of {@link AbstractListChromosome} * @throws DimensionMismatchException if the length of the two chromosomes is different */ @SuppressWarnings("unchecked") public ChromosomePair crossover(final Chromosome first, final Chromosome second) throws DimensionMismatchException, MathIllegalArgumentException { if (!(first instanceof AbstractListChromosome && second instanceof AbstractListChromosome)) { throw new MathIllegalArgumentException(LocalizedFormats.INVALID_FIXED_LENGTH_CHROMOSOME); } return mate((AbstractListChromosome) first, (AbstractListChromosome) second); } /** * Helper for {@link #crossover(Chromosome, Chromosome)}. Performs the actual crossover. * * @param first the first chromosome * @param second the second chromosome * @return the pair of new chromosomes that resulted from the crossover * @throws DimensionMismatchException if the length of the two chromosomes is different */ protected ChromosomePair mate(final AbstractListChromosome first, final AbstractListChromosome second) throws DimensionMismatchException { final int length = first.getLength(); if (length != second.getLength()) { throw new DimensionMismatchException(second.getLength(), length); } // array representations of the parents final List parent1Rep = first.getRepresentation(); final List parent2Rep = second.getRepresentation(); // and of the children: do a crossover copy to simplify the later processing final List child1Rep = new ArrayList(second.getRepresentation()); final List child2Rep = new ArrayList(first.getRepresentation()); // the set of all visited indices so far final Set visitedIndices = new HashSet(length); // the indices of the current cycle final List indices = new ArrayList(length); // determine the starting index int idx = randomStart ? GeneticAlgorithm.getRandomGenerator().nextInt(length) : 0; int cycle = 1; while (visitedIndices.size() < length) { indices.add(idx); T item = parent2Rep.get(idx); idx = parent1Rep.indexOf(item); while (idx != indices.get(0)) { // add that index to the cycle indices indices.add(idx); // get the item in the second parent at that index item = parent2Rep.get(idx); // get the index of that item in the first parent idx = parent1Rep.indexOf(item); } // for even cycles: swap the child elements on the indices found in this cycle if (cycle++ % 2 != 0) { for (int i : indices) { T tmp = child1Rep.get(i); child1Rep.set(i, child2Rep.get(i)); child2Rep.set(i, tmp); } } visitedIndices.addAll(indices); // find next starting index: last one + 1 until we find an unvisited index idx = (indices.get(0) + 1) % length; while (visitedIndices.contains(idx) && visitedIndices.size() < length) { idx++; if (idx >= length) { idx = 0; } } indices.clear(); } return new ChromosomePair(first.newFixedLengthChromosome(child1Rep), second.newFixedLengthChromosome(child2Rep)); } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/genetics/GeneticAlgorithm.java100644 1750 1750 20731 12126627715 30441 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.genetics; import org.apache.commons.math3.exception.OutOfRangeException; import org.apache.commons.math3.exception.util.LocalizedFormats; import org.apache.commons.math3.random.RandomGenerator; import org.apache.commons.math3.random.JDKRandomGenerator; /** * Implementation of a genetic algorithm. All factors that govern the operation * of the algorithm can be configured for a specific problem. * * @since 2.0 * @version $Id: GeneticAlgorithm.java 1416643 2012-12-03 19:37:14Z tn $ */ public class GeneticAlgorithm { /** * Static random number generator shared by GA implementation classes. Set the randomGenerator seed to get * reproducible results. Use {@link #setRandomGenerator(RandomGenerator)} to supply an alternative to the default * JDK-provided PRNG. */ //@GuardedBy("this") private static RandomGenerator randomGenerator = new JDKRandomGenerator(); /** the crossover policy used by the algorithm. */ private final CrossoverPolicy crossoverPolicy; /** the rate of crossover for the algorithm. */ private final double crossoverRate; /** the mutation policy used by the algorithm. */ private final MutationPolicy mutationPolicy; /** the rate of mutation for the algorithm. */ private final double mutationRate; /** the selection policy used by the algorithm. */ private final SelectionPolicy selectionPolicy; /** the number of generations evolved to reach {@link StoppingCondition} in the last run. */ private int generationsEvolved = 0; /** * Create a new genetic algorithm. * @param crossoverPolicy The {@link CrossoverPolicy} * @param crossoverRate The crossover rate as a percentage (0-1 inclusive) * @param mutationPolicy The {@link MutationPolicy} * @param mutationRate The mutation rate as a percentage (0-1 inclusive) * @param selectionPolicy The {@link SelectionPolicy} * @throws OutOfRangeException if the crossover or mutation rate is outside the [0, 1] range */ public GeneticAlgorithm(final CrossoverPolicy crossoverPolicy, final double crossoverRate, final MutationPolicy mutationPolicy, final double mutationRate, final SelectionPolicy selectionPolicy) throws OutOfRangeException { if (crossoverRate < 0 || crossoverRate > 1) { throw new OutOfRangeException(LocalizedFormats.CROSSOVER_RATE, crossoverRate, 0, 1); } if (mutationRate < 0 || mutationRate > 1) { throw new OutOfRangeException(LocalizedFormats.MUTATION_RATE, mutationRate, 0, 1); } this.crossoverPolicy = crossoverPolicy; this.crossoverRate = crossoverRate; this.mutationPolicy = mutationPolicy; this.mutationRate = mutationRate; this.selectionPolicy = selectionPolicy; } /** * Set the (static) random generator. * * @param random random generator */ public static synchronized void setRandomGenerator(final RandomGenerator random) { randomGenerator = random; } /** * Returns the (static) random generator. * * @return the static random generator shared by GA implementation classes */ public static synchronized RandomGenerator getRandomGenerator() { return randomGenerator; } /** * Evolve the given population. Evolution stops when the stopping condition * is satisfied. Updates the {@link #getGenerationsEvolved() generationsEvolved} * property with the number of generations evolved before the StoppingCondition * is satisfied. * * @param initial the initial, seed population. * @param condition the stopping condition used to stop evolution. * @return the population that satisfies the stopping condition. */ public Population evolve(final Population initial, final StoppingCondition condition) { Population current = initial; generationsEvolved = 0; while (!condition.isSatisfied(current)) { current = nextGeneration(current); generationsEvolved++; } return current; } /** * Evolve the given population into the next generation. *

                                    *

                                      *
                                    1. Get nextGeneration population to fill from current * generation, using its nextGeneration method
                                    2. *
                                    3. Loop until new generation is filled:
                                    4. *
                                      • Apply configured SelectionPolicy to select a pair of parents * from current
                                      • *
                                      • With probability = {@link #getCrossoverRate()}, apply * configured {@link CrossoverPolicy} to parents
                                      • *
                                      • With probability = {@link #getMutationRate()}, apply * configured {@link MutationPolicy} to each of the offspring
                                      • *
                                      • Add offspring individually to nextGeneration, * space permitting
                                      • *
                                      *
                                    5. Return nextGeneration
                                    6. *
                                    * * @param current the current population. * @return the population for the next generation. */ public Population nextGeneration(final Population current) { Population nextGeneration = current.nextGeneration(); RandomGenerator randGen = getRandomGenerator(); while (nextGeneration.getPopulationSize() < nextGeneration.getPopulationLimit()) { // select parent chromosomes ChromosomePair pair = getSelectionPolicy().select(current); // crossover? if (randGen.nextDouble() < getCrossoverRate()) { // apply crossover policy to create two offspring pair = getCrossoverPolicy().crossover(pair.getFirst(), pair.getSecond()); } // mutation? if (randGen.nextDouble() < getMutationRate()) { // apply mutation policy to the chromosomes pair = new ChromosomePair( getMutationPolicy().mutate(pair.getFirst()), getMutationPolicy().mutate(pair.getSecond())); } // add the first chromosome to the population nextGeneration.addChromosome(pair.getFirst()); // is there still a place for the second chromosome? if (nextGeneration.getPopulationSize() < nextGeneration.getPopulationLimit()) { // add the second chromosome to the population nextGeneration.addChromosome(pair.getSecond()); } } return nextGeneration; } /** * Returns the crossover policy. * @return crossover policy */ public CrossoverPolicy getCrossoverPolicy() { return crossoverPolicy; } /** * Returns the crossover rate. * @return crossover rate */ public double getCrossoverRate() { return crossoverRate; } /** * Returns the mutation policy. * @return mutation policy */ public MutationPolicy getMutationPolicy() { return mutationPolicy; } /** * Returns the mutation rate. * @return mutation rate */ public double getMutationRate() { return mutationRate; } /** * Returns the selection policy. * @return selection policy */ public SelectionPolicy getSelectionPolicy() { return selectionPolicy; } /** * Returns the number of generations evolved to reach {@link StoppingCondition} in the last run. * * @return number of generations evolved * @since 2.1 */ public int getGenerationsEvolved() { return generationsEvolved; } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/genetics/NPointCrossover.java100644 1750 1750 17357 12126627715 30343 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.genetics; import java.util.ArrayList; import java.util.List; import org.apache.commons.math3.exception.DimensionMismatchException; import org.apache.commons.math3.exception.MathIllegalArgumentException; import org.apache.commons.math3.exception.NotStrictlyPositiveException; import org.apache.commons.math3.exception.NumberIsTooLargeException; import org.apache.commons.math3.exception.util.LocalizedFormats; import org.apache.commons.math3.random.RandomGenerator; /** * N-point crossover policy. For each iteration a random crossover point is * selected and the first part from each parent is copied to the corresponding * child, and the second parts are copied crosswise. * * Example (2-point crossover): *
                                     * -C- denotes a crossover point
                                     *           -C-       -C-                         -C-        -C-
                                     * p1 = (1 0  | 1 0 0 1 | 0 1 1)    X    p2 = (0 1  | 1 0 1 0  | 1 1 1)
                                     *      \----/ \-------/ \-----/              \----/ \--------/ \-----/
                                     *        ||      (*)       ||                  ||      (**)       ||
                                     *        VV      (**)      VV                  VV      (*)        VV
                                     *      /----\ /--------\ /-----\             /----\ /--------\ /-----\
                                     * c1 = (1 0  | 1 0 1 0  | 0 1 1)    X   c2 = (0 1  | 1 0 0 1  | 0 1 1)
                                     * 
                                    * * This policy works only on {@link AbstractListChromosome}, and therefore it * is parameterized by T. Moreover, the chromosomes must have same lengths. * * @param generic type of the {@link AbstractListChromosome}s for crossover * @since 3.1 * @version $Id: NPointCrossover.java 1385297 2012-09-16 16:05:57Z tn $ */ public class NPointCrossover implements CrossoverPolicy { /** The number of crossover points. */ private final int crossoverPoints; /** * Creates a new {@link NPointCrossover} policy using the given number of points. *

                                    * Note: the number of crossover points must be < chromosome length - 1. * This condition can only be checked at runtime, as the chromosome length is not known in advance. * * @param crossoverPoints the number of crossover points * @throws NotStrictlyPositiveException if the number of {@code crossoverPoints} is not strictly positive */ public NPointCrossover(final int crossoverPoints) throws NotStrictlyPositiveException { if (crossoverPoints <= 0) { throw new NotStrictlyPositiveException(crossoverPoints); } this.crossoverPoints = crossoverPoints; } /** * Returns the number of crossover points used by this {@link CrossoverPolicy}. * * @return the number of crossover points */ public int getCrossoverPoints() { return crossoverPoints; } /** * Performs a N-point crossover. N random crossover points are selected and are used * to divide the parent chromosomes into segments. The segments are copied in alternate * order from the two parents to the corresponding child chromosomes. * * Example (2-point crossover): *

                                         * -C- denotes a crossover point
                                         *           -C-       -C-                         -C-        -C-
                                         * p1 = (1 0  | 1 0 0 1 | 0 1 1)    X    p2 = (0 1  | 1 0 1 0  | 1 1 1)
                                         *      \----/ \-------/ \-----/              \----/ \--------/ \-----/
                                         *        ||      (*)       ||                  ||      (**)       ||
                                         *        VV      (**)      VV                  VV      (*)        VV
                                         *      /----\ /--------\ /-----\             /----\ /--------\ /-----\
                                         * c1 = (1 0  | 1 0 1 0  | 0 1 1)    X   c2 = (0 1  | 1 0 0 1  | 0 1 1)
                                         * 
                                    * * @param first first parent (p1) * @param second second parent (p2) * @return pair of two children (c1,c2) * @throws MathIllegalArgumentException iff one of the chromosomes is * not an instance of {@link AbstractListChromosome} * @throws DimensionMismatchException if the length of the two chromosomes is different */ @SuppressWarnings("unchecked") // OK because of instanceof checks public ChromosomePair crossover(final Chromosome first, final Chromosome second) throws DimensionMismatchException, MathIllegalArgumentException { if (!(first instanceof AbstractListChromosome && second instanceof AbstractListChromosome)) { throw new MathIllegalArgumentException(LocalizedFormats.INVALID_FIXED_LENGTH_CHROMOSOME); } return mate((AbstractListChromosome) first, (AbstractListChromosome) second); } /** * Helper for {@link #crossover(Chromosome, Chromosome)}. Performs the actual crossover. * * @param first the first chromosome * @param second the second chromosome * @return the pair of new chromosomes that resulted from the crossover * @throws DimensionMismatchException if the length of the two chromosomes is different * @throws NumberIsTooLargeException if the number of crossoverPoints is too large for the actual chromosomes */ private ChromosomePair mate(final AbstractListChromosome first, final AbstractListChromosome second) throws DimensionMismatchException, NumberIsTooLargeException { final int length = first.getLength(); if (length != second.getLength()) { throw new DimensionMismatchException(second.getLength(), length); } if (crossoverPoints >= length) { throw new NumberIsTooLargeException(crossoverPoints, length, false); } // array representations of the parents final List parent1Rep = first.getRepresentation(); final List parent2Rep = second.getRepresentation(); // and of the children final ArrayList child1Rep = new ArrayList(first.getLength()); final ArrayList child2Rep = new ArrayList(second.getLength()); final RandomGenerator random = GeneticAlgorithm.getRandomGenerator(); ArrayList c1 = child1Rep; ArrayList c2 = child2Rep; int remainingPoints = crossoverPoints; int lastIndex = 0; for (int i = 0; i < crossoverPoints; i++, remainingPoints--) { // select the next crossover point at random final int crossoverIndex = 1 + lastIndex + random.nextInt(length - lastIndex - remainingPoints); // copy the current segment for (int j = lastIndex; j < crossoverIndex; j++) { c1.add(parent1Rep.get(j)); c2.add(parent2Rep.get(j)); } // swap the children for the next segment ArrayList tmp = c1; c1 = c2; c2 = tmp; lastIndex = crossoverIndex; } // copy the last segment for (int j = lastIndex; j < length; j++) { c1.add(parent1Rep.get(j)); c2.add(parent2Rep.get(j)); } return new ChromosomePair(first.newFixedLengthChromosome(child1Rep), second.newFixedLengthChromosome(child2Rep)); } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/genetics/FixedElapsedTime.java100644 1750 1750 5770 12126627715 30356 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.genetics; import java.util.concurrent.TimeUnit; import org.apache.commons.math3.exception.NumberIsTooSmallException; /** * Stops after a fixed amount of time has elapsed. *

                                    * The first time {@link #isSatisfied(Population)} is invoked, the end time of the evolution is determined based on the * provided maxTime value. Once the elapsed time reaches the configured maxTime value, * {@link #isSatisfied(Population)} returns true. * * @version $Id: FixedElapsedTime.java 1385297 2012-09-16 16:05:57Z tn $ * @since 3.1 */ public class FixedElapsedTime implements StoppingCondition { /** Maximum allowed time period (in nanoseconds). */ private final long maxTimePeriod; /** The predetermined termination time (stopping condition). */ private long endTime = -1; /** * Create a new {@link FixedElapsedTime} instance. * * @param maxTime maximum number of seconds generations are allowed to evolve * @throws NumberIsTooSmallException if the provided time is < 0 */ public FixedElapsedTime(final long maxTime) throws NumberIsTooSmallException { this(maxTime, TimeUnit.SECONDS); } /** * Create a new {@link FixedElapsedTime} instance. * * @param maxTime maximum time generations are allowed to evolve * @param unit {@link TimeUnit} of the maxTime argument * @throws NumberIsTooSmallException if the provided time is < 0 */ public FixedElapsedTime(final long maxTime, final TimeUnit unit) throws NumberIsTooSmallException { if (maxTime < 0) { throw new NumberIsTooSmallException(maxTime, 0, true); } maxTimePeriod = unit.toNanos(maxTime); } /** * Determine whether or not the maximum allowed time has passed. * The termination time is determined after the first generation. * * @param population ignored (no impact on result) * @return true IFF the maximum allowed time period has elapsed */ public boolean isSatisfied(final Population population) { if (endTime < 0) { endTime = System.nanoTime() + maxTimePeriod; } return System.nanoTime() >= endTime; } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/genetics/ElitisticListPopulation.java100644 1750 1750 12032 12126627715 32047 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.genetics; import java.util.Collections; import java.util.List; import org.apache.commons.math3.exception.NotPositiveException; import org.apache.commons.math3.exception.NullArgumentException; import org.apache.commons.math3.exception.NumberIsTooLargeException; import org.apache.commons.math3.exception.OutOfRangeException; import org.apache.commons.math3.exception.util.LocalizedFormats; import org.apache.commons.math3.util.FastMath; /** * Population of chromosomes which uses elitism (certain percentage of the best * chromosomes is directly copied to the next generation). * * @version $Id: ElitisticListPopulation.java 1416643 2012-12-03 19:37:14Z tn $ * @since 2.0 */ public class ElitisticListPopulation extends ListPopulation { /** percentage of chromosomes copied to the next generation */ private double elitismRate = 0.9; /** * Creates a new {@link ElitisticListPopulation} instance. * * @param chromosomes list of chromosomes in the population * @param populationLimit maximal size of the population * @param elitismRate how many best chromosomes will be directly transferred to the next generation [in %] * @throws NullArgumentException if the list of chromosomes is {@code null} * @throws NotPositiveException if the population limit is not a positive number (< 1) * @throws NumberIsTooLargeException if the list of chromosomes exceeds the population limit * @throws OutOfRangeException if the elitism rate is outside the [0, 1] range */ public ElitisticListPopulation(final List chromosomes, final int populationLimit, final double elitismRate) throws NullArgumentException, NotPositiveException, NumberIsTooLargeException, OutOfRangeException { super(chromosomes, populationLimit); setElitismRate(elitismRate); } /** * Creates a new {@link ElitisticListPopulation} instance and initializes its inner chromosome list. * * @param populationLimit maximal size of the population * @param elitismRate how many best chromosomes will be directly transferred to the next generation [in %] * @throws NotPositiveException if the population limit is not a positive number (< 1) * @throws OutOfRangeException if the elitism rate is outside the [0, 1] range */ public ElitisticListPopulation(final int populationLimit, final double elitismRate) throws NotPositiveException, OutOfRangeException { super(populationLimit); setElitismRate(elitismRate); } /** * Start the population for the next generation. The {@link #elitismRate} * percents of the best chromosomes are directly copied to the next generation. * * @return the beginnings of the next generation. */ public Population nextGeneration() { // initialize a new generation with the same parameters ElitisticListPopulation nextGeneration = new ElitisticListPopulation(getPopulationLimit(), getElitismRate()); final List oldChromosomes = getChromosomeList(); Collections.sort(oldChromosomes); // index of the last "not good enough" chromosome int boundIndex = (int) FastMath.ceil((1.0 - getElitismRate()) * oldChromosomes.size()); for (int i = boundIndex; i < oldChromosomes.size(); i++) { nextGeneration.addChromosome(oldChromosomes.get(i)); } return nextGeneration; } /** * Sets the elitism rate, i.e. how many best chromosomes will be directly transferred to the next generation [in %]. * * @param elitismRate how many best chromosomes will be directly transferred to the next generation [in %] * @throws OutOfRangeException if the elitism rate is outside the [0, 1] range */ public void setElitismRate(final double elitismRate) throws OutOfRangeException { if (elitismRate < 0 || elitismRate > 1) { throw new OutOfRangeException(LocalizedFormats.ELITISM_RATE, elitismRate, 0, 1); } this.elitismRate = elitismRate; } /** * Access the elitism rate. * @return the elitism rate */ public double getElitismRate() { return this.elitismRate; } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/Field.java100644 1750 1750 4114 12126627720 24407 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3; /** * Interface representing a field. *

                                    * Classes implementing this interface will often be singletons. *

                                    * @param the type of the field elements * @see FieldElement * @version $Id: Field.java 1416643 2012-12-03 19:37:14Z tn $ * @since 2.0 */ public interface Field { /** Get the additive identity of the field. *

                                    * The additive identity is the element e0 of the field such that * for all elements a of the field, the equalities a + e0 = * e0 + a = a hold. *

                                    * @return additive identity of the field */ T getZero(); /** Get the multiplicative identity of the field. *

                                    * The multiplicative identity is the element e1 of the field such that * for all elements a of the field, the equalities a × e1 = * e1 × a = a hold. *

                                    * @return multiplicative identity of the field */ T getOne(); /** * Returns the runtime class of the FieldElement. * * @return The {@code Class} object that represents the runtime * class of this object. */ Class> getRuntimeClass(); } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/fraction/package-info.java100644 1750 1750 1627 12126627715 27527 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ /** * * Fraction number type and fraction number formatting. * */ package org.apache.commons.math3.fraction; commons-math3-3.2-src/src/main/java/org/apache/commons/math3/fraction/FractionField.java100644 1750 1750 4735 12126627715 27717 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.fraction; import java.io.Serializable; import org.apache.commons.math3.Field; import org.apache.commons.math3.FieldElement; /** * Representation of the fractional numbers field. *

                                    * This class is a singleton. *

                                    * @see Fraction * @version $Id: FractionField.java 1416643 2012-12-03 19:37:14Z tn $ * @since 2.0 */ public class FractionField implements Field, Serializable { /** Serializable version identifier */ private static final long serialVersionUID = -1257768487499119313L; /** Private constructor for the singleton. */ private FractionField() { } /** Get the unique instance. * @return the unique instance */ public static FractionField getInstance() { return LazyHolder.INSTANCE; } /** {@inheritDoc} */ public Fraction getOne() { return Fraction.ONE; } /** {@inheritDoc} */ public Fraction getZero() { return Fraction.ZERO; } /** {@inheritDoc} */ public Class> getRuntimeClass() { return Fraction.class; } // CHECKSTYLE: stop HideUtilityClassConstructor /** Holder for the instance. *

                                    We use here the Initialization On Demand Holder Idiom.

                                    */ private static class LazyHolder { /** Cached field instance. */ private static final FractionField INSTANCE = new FractionField(); } // CHECKSTYLE: resume HideUtilityClassConstructor /** Handle deserialization of the singleton. * @return the singleton instance */ private Object readResolve() { // return the singleton instance return LazyHolder.INSTANCE; } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/fraction/ProperBigFractionFormat.java100644 1750 1750 20126 12126627715 31746 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.fraction; import java.math.BigInteger; import java.text.FieldPosition; import java.text.NumberFormat; import java.text.ParsePosition; import org.apache.commons.math3.exception.util.LocalizedFormats; import org.apache.commons.math3.exception.NullArgumentException; /** * Formats a BigFraction number in proper format. The number format for each of * the whole number, numerator and, denominator can be configured. *

                                    * Minus signs are only allowed in the whole number part - i.e., * "-3 1/2" is legitimate and denotes -7/2, but "-3 -1/2" is invalid and * will result in a ParseException.

                                    * * @since 1.1 * @version $Id: ProperBigFractionFormat.java 1416643 2012-12-03 19:37:14Z tn $ */ public class ProperBigFractionFormat extends BigFractionFormat { /** Serializable version identifier */ private static final long serialVersionUID = -6337346779577272307L; /** The format used for the whole number. */ private NumberFormat wholeFormat; /** * Create a proper formatting instance with the default number format for * the whole, numerator, and denominator. */ public ProperBigFractionFormat() { this(getDefaultNumberFormat()); } /** * Create a proper formatting instance with a custom number format for the * whole, numerator, and denominator. * @param format the custom format for the whole, numerator, and * denominator. */ public ProperBigFractionFormat(final NumberFormat format) { this(format, (NumberFormat)format.clone(), (NumberFormat)format.clone()); } /** * Create a proper formatting instance with a custom number format for each * of the whole, numerator, and denominator. * @param wholeFormat the custom format for the whole. * @param numeratorFormat the custom format for the numerator. * @param denominatorFormat the custom format for the denominator. */ public ProperBigFractionFormat(final NumberFormat wholeFormat, final NumberFormat numeratorFormat, final NumberFormat denominatorFormat) { super(numeratorFormat, denominatorFormat); setWholeFormat(wholeFormat); } /** * Formats a {@link BigFraction} object to produce a string. The BigFraction * is output in proper format. * * @param fraction the object to format. * @param toAppendTo where the text is to be appended * @param pos On input: an alignment field, if desired. On output: the * offsets of the alignment field * @return the value passed in as toAppendTo. */ @Override public StringBuffer format(final BigFraction fraction, final StringBuffer toAppendTo, final FieldPosition pos) { pos.setBeginIndex(0); pos.setEndIndex(0); BigInteger num = fraction.getNumerator(); BigInteger den = fraction.getDenominator(); BigInteger whole = num.divide(den); num = num.remainder(den); if (!BigInteger.ZERO.equals(whole)) { getWholeFormat().format(whole, toAppendTo, pos); toAppendTo.append(' '); if (num.compareTo(BigInteger.ZERO) < 0) { num = num.negate(); } } getNumeratorFormat().format(num, toAppendTo, pos); toAppendTo.append(" / "); getDenominatorFormat().format(den, toAppendTo, pos); return toAppendTo; } /** * Access the whole format. * @return the whole format. */ public NumberFormat getWholeFormat() { return wholeFormat; } /** * Parses a string to produce a {@link BigFraction} object. This method * expects the string to be formatted as a proper BigFraction. *

                                    * Minus signs are only allowed in the whole number part - i.e., * "-3 1/2" is legitimate and denotes -7/2, but "-3 -1/2" is invalid and * will result in a ParseException.

                                    * * @param source the string to parse * @param pos input/ouput parsing parameter. * @return the parsed {@link BigFraction} object. */ @Override public BigFraction parse(final String source, final ParsePosition pos) { // try to parse improper BigFraction BigFraction ret = super.parse(source, pos); if (ret != null) { return ret; } final int initialIndex = pos.getIndex(); // parse whitespace parseAndIgnoreWhitespace(source, pos); // parse whole BigInteger whole = parseNextBigInteger(source, pos); if (whole == null) { // invalid integer number // set index back to initial, error index should already be set // character examined. pos.setIndex(initialIndex); return null; } // parse whitespace parseAndIgnoreWhitespace(source, pos); // parse numerator BigInteger num = parseNextBigInteger(source, pos); if (num == null) { // invalid integer number // set index back to initial, error index should already be set // character examined. pos.setIndex(initialIndex); return null; } if (num.compareTo(BigInteger.ZERO) < 0) { // minus signs should be leading, invalid expression pos.setIndex(initialIndex); return null; } // parse '/' final int startIndex = pos.getIndex(); final char c = parseNextCharacter(source, pos); switch (c) { case 0 : // no '/' // return num as a BigFraction return new BigFraction(num); case '/' : // found '/', continue parsing denominator break; default : // invalid '/' // set index back to initial, error index should be the last // character examined. pos.setIndex(initialIndex); pos.setErrorIndex(startIndex); return null; } // parse whitespace parseAndIgnoreWhitespace(source, pos); // parse denominator final BigInteger den = parseNextBigInteger(source, pos); if (den == null) { // invalid integer number // set index back to initial, error index should already be set // character examined. pos.setIndex(initialIndex); return null; } if (den.compareTo(BigInteger.ZERO) < 0) { // minus signs must be leading, invalid pos.setIndex(initialIndex); return null; } boolean wholeIsNeg = whole.compareTo(BigInteger.ZERO) < 0; if (wholeIsNeg) { whole = whole.negate(); } num = whole.multiply(den).add(num); if (wholeIsNeg) { num = num.negate(); } return new BigFraction(num, den); } /** * Modify the whole format. * @param format The new whole format value. * @throws NullArgumentException if {@code format} is {@code null}. */ public void setWholeFormat(final NumberFormat format) { if (format == null) { throw new NullArgumentException(LocalizedFormats.WHOLE_FORMAT); } this.wholeFormat = format; } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/fraction/ProperFractionFormat.java100644 1750 1750 17261 12126627715 31332 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.fraction; import java.text.FieldPosition; import java.text.NumberFormat; import java.text.ParsePosition; import org.apache.commons.math3.exception.util.LocalizedFormats; import org.apache.commons.math3.exception.NullArgumentException; import org.apache.commons.math3.util.MathUtils; /** * Formats a Fraction number in proper format. The number format for each of * the whole number, numerator and, denominator can be configured. *

                                    * Minus signs are only allowed in the whole number part - i.e., * "-3 1/2" is legitimate and denotes -7/2, but "-3 -1/2" is invalid and * will result in a ParseException.

                                    * * @since 1.1 * @version $Id: ProperFractionFormat.java 1416643 2012-12-03 19:37:14Z tn $ */ public class ProperFractionFormat extends FractionFormat { /** Serializable version identifier */ private static final long serialVersionUID = 760934726031766749L; /** The format used for the whole number. */ private NumberFormat wholeFormat; /** * Create a proper formatting instance with the default number format for * the whole, numerator, and denominator. */ public ProperFractionFormat() { this(getDefaultNumberFormat()); } /** * Create a proper formatting instance with a custom number format for the * whole, numerator, and denominator. * @param format the custom format for the whole, numerator, and * denominator. */ public ProperFractionFormat(NumberFormat format) { this(format, (NumberFormat)format.clone(), (NumberFormat)format.clone()); } /** * Create a proper formatting instance with a custom number format for each * of the whole, numerator, and denominator. * @param wholeFormat the custom format for the whole. * @param numeratorFormat the custom format for the numerator. * @param denominatorFormat the custom format for the denominator. */ public ProperFractionFormat(NumberFormat wholeFormat, NumberFormat numeratorFormat, NumberFormat denominatorFormat) { super(numeratorFormat, denominatorFormat); setWholeFormat(wholeFormat); } /** * Formats a {@link Fraction} object to produce a string. The fraction * is output in proper format. * * @param fraction the object to format. * @param toAppendTo where the text is to be appended * @param pos On input: an alignment field, if desired. On output: the * offsets of the alignment field * @return the value passed in as toAppendTo. */ @Override public StringBuffer format(Fraction fraction, StringBuffer toAppendTo, FieldPosition pos) { pos.setBeginIndex(0); pos.setEndIndex(0); int num = fraction.getNumerator(); int den = fraction.getDenominator(); int whole = num / den; num = num % den; if (whole != 0) { getWholeFormat().format(whole, toAppendTo, pos); toAppendTo.append(' '); num = Math.abs(num); } getNumeratorFormat().format(num, toAppendTo, pos); toAppendTo.append(" / "); getDenominatorFormat().format(den, toAppendTo, pos); return toAppendTo; } /** * Access the whole format. * @return the whole format. */ public NumberFormat getWholeFormat() { return wholeFormat; } /** * Parses a string to produce a {@link Fraction} object. This method * expects the string to be formatted as a proper fraction. *

                                    * Minus signs are only allowed in the whole number part - i.e., * "-3 1/2" is legitimate and denotes -7/2, but "-3 -1/2" is invalid and * will result in a ParseException.

                                    * * @param source the string to parse * @param pos input/ouput parsing parameter. * @return the parsed {@link Fraction} object. */ @Override public Fraction parse(String source, ParsePosition pos) { // try to parse improper fraction Fraction ret = super.parse(source, pos); if (ret != null) { return ret; } int initialIndex = pos.getIndex(); // parse whitespace parseAndIgnoreWhitespace(source, pos); // parse whole Number whole = getWholeFormat().parse(source, pos); if (whole == null) { // invalid integer number // set index back to initial, error index should already be set // character examined. pos.setIndex(initialIndex); return null; } // parse whitespace parseAndIgnoreWhitespace(source, pos); // parse numerator Number num = getNumeratorFormat().parse(source, pos); if (num == null) { // invalid integer number // set index back to initial, error index should already be set // character examined. pos.setIndex(initialIndex); return null; } if (num.intValue() < 0) { // minus signs should be leading, invalid expression pos.setIndex(initialIndex); return null; } // parse '/' int startIndex = pos.getIndex(); char c = parseNextCharacter(source, pos); switch (c) { case 0 : // no '/' // return num as a fraction return new Fraction(num.intValue(), 1); case '/' : // found '/', continue parsing denominator break; default : // invalid '/' // set index back to initial, error index should be the last // character examined. pos.setIndex(initialIndex); pos.setErrorIndex(startIndex); return null; } // parse whitespace parseAndIgnoreWhitespace(source, pos); // parse denominator Number den = getDenominatorFormat().parse(source, pos); if (den == null) { // invalid integer number // set index back to initial, error index should already be set // character examined. pos.setIndex(initialIndex); return null; } if (den.intValue() < 0) { // minus signs must be leading, invalid pos.setIndex(initialIndex); return null; } int w = whole.intValue(); int n = num.intValue(); int d = den.intValue(); return new Fraction(((Math.abs(w) * d) + n) * MathUtils.copySign(1, w), d); } /** * Modify the whole format. * @param format The new whole format value. * @throws NullArgumentException if {@code format} is {@code null}. */ public void setWholeFormat(NumberFormat format) { if (format == null) { throw new NullArgumentException(LocalizedFormats.WHOLE_FORMAT); } this.wholeFormat = format; } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/fraction/BigFractionFormat.java100644 1750 1750 24503 12126627715 30561 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.fraction; import java.io.Serializable; import java.math.BigInteger; import java.text.FieldPosition; import java.text.NumberFormat; import java.text.ParsePosition; import java.util.Locale; import org.apache.commons.math3.exception.MathIllegalArgumentException; import org.apache.commons.math3.exception.MathParseException; import org.apache.commons.math3.exception.util.LocalizedFormats; /** * Formats a BigFraction number in proper format or improper format. *

                                    * The number format for each of the whole number, numerator and, * denominator can be configured. *

                                    * * @since 2.0 * @version $Id: BigFractionFormat.java 1416643 2012-12-03 19:37:14Z tn $ */ public class BigFractionFormat extends AbstractFormat implements Serializable { /** Serializable version identifier */ private static final long serialVersionUID = -2932167925527338976L; /** * Create an improper formatting instance with the default number format * for the numerator and denominator. */ public BigFractionFormat() { } /** * Create an improper formatting instance with a custom number format for * both the numerator and denominator. * @param format the custom format for both the numerator and denominator. */ public BigFractionFormat(final NumberFormat format) { super(format); } /** * Create an improper formatting instance with a custom number format for * the numerator and a custom number format for the denominator. * @param numeratorFormat the custom format for the numerator. * @param denominatorFormat the custom format for the denominator. */ public BigFractionFormat(final NumberFormat numeratorFormat, final NumberFormat denominatorFormat) { super(numeratorFormat, denominatorFormat); } /** * Get the set of locales for which complex formats are available. This * is the same set as the {@link NumberFormat} set. * @return available complex format locales. */ public static Locale[] getAvailableLocales() { return NumberFormat.getAvailableLocales(); } /** * This static method calls formatBigFraction() on a default instance of * BigFractionFormat. * * @param f BigFraction object to format * @return A formatted BigFraction in proper form. */ public static String formatBigFraction(final BigFraction f) { return getImproperInstance().format(f); } /** * Returns the default complex format for the current locale. * @return the default complex format. */ public static BigFractionFormat getImproperInstance() { return getImproperInstance(Locale.getDefault()); } /** * Returns the default complex format for the given locale. * @param locale the specific locale used by the format. * @return the complex format specific to the given locale. */ public static BigFractionFormat getImproperInstance(final Locale locale) { return new BigFractionFormat(getDefaultNumberFormat(locale)); } /** * Returns the default complex format for the current locale. * @return the default complex format. */ public static BigFractionFormat getProperInstance() { return getProperInstance(Locale.getDefault()); } /** * Returns the default complex format for the given locale. * @param locale the specific locale used by the format. * @return the complex format specific to the given locale. */ public static BigFractionFormat getProperInstance(final Locale locale) { return new ProperBigFractionFormat(getDefaultNumberFormat(locale)); } /** * Formats a {@link BigFraction} object to produce a string. The BigFraction is * output in improper format. * * @param BigFraction the object to format. * @param toAppendTo where the text is to be appended * @param pos On input: an alignment field, if desired. On output: the * offsets of the alignment field * @return the value passed in as toAppendTo. */ public StringBuffer format(final BigFraction BigFraction, final StringBuffer toAppendTo, final FieldPosition pos) { pos.setBeginIndex(0); pos.setEndIndex(0); getNumeratorFormat().format(BigFraction.getNumerator(), toAppendTo, pos); toAppendTo.append(" / "); getDenominatorFormat().format(BigFraction.getDenominator(), toAppendTo, pos); return toAppendTo; } /** * Formats an object and appends the result to a StringBuffer. * obj must be either a {@link BigFraction} object or a * {@link BigInteger} object or a {@link Number} object. Any other type of * object will result in an {@link IllegalArgumentException} being thrown. * * @param obj the object to format. * @param toAppendTo where the text is to be appended * @param pos On input: an alignment field, if desired. On output: the * offsets of the alignment field * @return the value passed in as toAppendTo. * @see java.text.Format#format(java.lang.Object, java.lang.StringBuffer, java.text.FieldPosition) * @throws MathIllegalArgumentException if obj is not a valid type. */ @Override public StringBuffer format(final Object obj, final StringBuffer toAppendTo, final FieldPosition pos) { final StringBuffer ret; if (obj instanceof BigFraction) { ret = format((BigFraction) obj, toAppendTo, pos); } else if (obj instanceof BigInteger) { ret = format(new BigFraction((BigInteger) obj), toAppendTo, pos); } else if (obj instanceof Number) { ret = format(new BigFraction(((Number) obj).doubleValue()), toAppendTo, pos); } else { throw new MathIllegalArgumentException(LocalizedFormats.CANNOT_FORMAT_OBJECT_TO_FRACTION); } return ret; } /** * Parses a string to produce a {@link BigFraction} object. * @param source the string to parse * @return the parsed {@link BigFraction} object. * @exception MathParseException if the beginning of the specified string * cannot be parsed. */ @Override public BigFraction parse(final String source) throws MathParseException { final ParsePosition parsePosition = new ParsePosition(0); final BigFraction result = parse(source, parsePosition); if (parsePosition.getIndex() == 0) { throw new MathParseException(source, parsePosition.getErrorIndex(), BigFraction.class); } return result; } /** * Parses a string to produce a {@link BigFraction} object. * This method expects the string to be formatted as an improper BigFraction. * @param source the string to parse * @param pos input/output parsing parameter. * @return the parsed {@link BigFraction} object. */ @Override public BigFraction parse(final String source, final ParsePosition pos) { final int initialIndex = pos.getIndex(); // parse whitespace parseAndIgnoreWhitespace(source, pos); // parse numerator final BigInteger num = parseNextBigInteger(source, pos); if (num == null) { // invalid integer number // set index back to initial, error index should already be set // character examined. pos.setIndex(initialIndex); return null; } // parse '/' final int startIndex = pos.getIndex(); final char c = parseNextCharacter(source, pos); switch (c) { case 0 : // no '/' // return num as a BigFraction return new BigFraction(num); case '/' : // found '/', continue parsing denominator break; default : // invalid '/' // set index back to initial, error index should be the last // character examined. pos.setIndex(initialIndex); pos.setErrorIndex(startIndex); return null; } // parse whitespace parseAndIgnoreWhitespace(source, pos); // parse denominator final BigInteger den = parseNextBigInteger(source, pos); if (den == null) { // invalid integer number // set index back to initial, error index should already be set // character examined. pos.setIndex(initialIndex); return null; } return new BigFraction(num, den); } /** * Parses a string to produce a BigInteger. * @param source the string to parse * @param pos input/output parsing parameter. * @return a parsed BigInteger or null if string does not * contain a BigInteger at the specified position */ protected BigInteger parseNextBigInteger(final String source, final ParsePosition pos) { final int start = pos.getIndex(); int end = (source.charAt(start) == '-') ? (start + 1) : start; while((end < source.length()) && Character.isDigit(source.charAt(end))) { ++end; } try { BigInteger n = new BigInteger(source.substring(start, end)); pos.setIndex(end); return n; } catch (NumberFormatException nfe) { pos.setErrorIndex(start); return null; } } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/fraction/BigFractionField.java100644 1750 1750 5032 12126627715 30330 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.fraction; import java.io.Serializable; import org.apache.commons.math3.Field; import org.apache.commons.math3.FieldElement; /** * Representation of the fractional numbers without any overflow field. *

                                    * This class is a singleton. *

                                    * @see Fraction * @version $Id: BigFractionField.java 1416643 2012-12-03 19:37:14Z tn $ * @since 2.0 */ public class BigFractionField implements Field, Serializable { /** Serializable version identifier */ private static final long serialVersionUID = -1699294557189741703L; /** Private constructor for the singleton. */ private BigFractionField() { } /** Get the unique instance. * @return the unique instance */ public static BigFractionField getInstance() { return LazyHolder.INSTANCE; } /** {@inheritDoc} */ public BigFraction getOne() { return BigFraction.ONE; } /** {@inheritDoc} */ public BigFraction getZero() { return BigFraction.ZERO; } /** {@inheritDoc} */ public Class> getRuntimeClass() { return BigFraction.class; } // CHECKSTYLE: stop HideUtilityClassConstructor /** Holder for the instance. *

                                    We use here the Initialization On Demand Holder Idiom.

                                    */ private static class LazyHolder { /** Cached field instance. */ private static final BigFractionField INSTANCE = new BigFractionField(); } // CHECKSTYLE: resume HideUtilityClassConstructor /** Handle deserialization of the singleton. * @return the singleton instance */ private Object readResolve() { // return the singleton instance return LazyHolder.INSTANCE; } } ././@LongLink100644 0 0 147 12126630647 10264 Lustar 0 0 commons-math3-3.2-src/src/main/java/org/apache/commons/math3/fraction/FractionConversionException.javacommons-math3-3.2-src/src/main/java/org/apache/commons/math3/fraction/FractionConversionException.ja100644 1750 1750 4316 12126627715 32344 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.fraction; import org.apache.commons.math3.exception.ConvergenceException; import org.apache.commons.math3.exception.util.LocalizedFormats; /** * Error thrown when a double value cannot be converted to a fraction * in the allowed number of iterations. * * @version $Id: FractionConversionException.java 1416643 2012-12-03 19:37:14Z tn $ * @since 1.2 */ public class FractionConversionException extends ConvergenceException { /** Serializable version identifier. */ private static final long serialVersionUID = -4661812640132576263L; /** * Constructs an exception with specified formatted detail message. * Message formatting is delegated to {@link java.text.MessageFormat}. * @param value double value to convert * @param maxIterations maximal number of iterations allowed */ public FractionConversionException(double value, int maxIterations) { super(LocalizedFormats.FAILED_FRACTION_CONVERSION, value, maxIterations); } /** * Constructs an exception with specified formatted detail message. * Message formatting is delegated to {@link java.text.MessageFormat}. * @param value double value to convert * @param p current numerator * @param q current denominator */ public FractionConversionException(double value, long p, long q) { super(LocalizedFormats.FRACTION_CONVERSION_OVERFLOW, value, p, q); } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/fraction/FractionFormat.java100644 1750 1750 23152 12126627715 30136 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.fraction; import java.text.FieldPosition; import java.text.NumberFormat; import java.text.ParsePosition; import java.util.Locale; import org.apache.commons.math3.exception.MathIllegalArgumentException; import org.apache.commons.math3.exception.MathParseException; import org.apache.commons.math3.exception.util.LocalizedFormats; /** * Formats a Fraction number in proper format or improper format. The number * format for each of the whole number, numerator and, denominator can be * configured. * * @since 1.1 * @version $Id: FractionFormat.java 1416643 2012-12-03 19:37:14Z tn $ */ public class FractionFormat extends AbstractFormat { /** Serializable version identifier */ private static final long serialVersionUID = 3008655719530972611L; /** * Create an improper formatting instance with the default number format * for the numerator and denominator. */ public FractionFormat() { } /** * Create an improper formatting instance with a custom number format for * both the numerator and denominator. * @param format the custom format for both the numerator and denominator. */ public FractionFormat(final NumberFormat format) { super(format); } /** * Create an improper formatting instance with a custom number format for * the numerator and a custom number format for the denominator. * @param numeratorFormat the custom format for the numerator. * @param denominatorFormat the custom format for the denominator. */ public FractionFormat(final NumberFormat numeratorFormat, final NumberFormat denominatorFormat) { super(numeratorFormat, denominatorFormat); } /** * Get the set of locales for which complex formats are available. This * is the same set as the {@link NumberFormat} set. * @return available complex format locales. */ public static Locale[] getAvailableLocales() { return NumberFormat.getAvailableLocales(); } /** * This static method calls formatFraction() on a default instance of * FractionFormat. * * @param f Fraction object to format * @return a formatted fraction in proper form. */ public static String formatFraction(Fraction f) { return getImproperInstance().format(f); } /** * Returns the default complex format for the current locale. * @return the default complex format. */ public static FractionFormat getImproperInstance() { return getImproperInstance(Locale.getDefault()); } /** * Returns the default complex format for the given locale. * @param locale the specific locale used by the format. * @return the complex format specific to the given locale. */ public static FractionFormat getImproperInstance(final Locale locale) { return new FractionFormat(getDefaultNumberFormat(locale)); } /** * Returns the default complex format for the current locale. * @return the default complex format. */ public static FractionFormat getProperInstance() { return getProperInstance(Locale.getDefault()); } /** * Returns the default complex format for the given locale. * @param locale the specific locale used by the format. * @return the complex format specific to the given locale. */ public static FractionFormat getProperInstance(final Locale locale) { return new ProperFractionFormat(getDefaultNumberFormat(locale)); } /** * Create a default number format. The default number format is based on * {@link NumberFormat#getNumberInstance(java.util.Locale)} with the only * customizing is the maximum number of fraction digits, which is set to 0. * @return the default number format. */ protected static NumberFormat getDefaultNumberFormat() { return getDefaultNumberFormat(Locale.getDefault()); } /** * Formats a {@link Fraction} object to produce a string. The fraction is * output in improper format. * * @param fraction the object to format. * @param toAppendTo where the text is to be appended * @param pos On input: an alignment field, if desired. On output: the * offsets of the alignment field * @return the value passed in as toAppendTo. */ public StringBuffer format(final Fraction fraction, final StringBuffer toAppendTo, final FieldPosition pos) { pos.setBeginIndex(0); pos.setEndIndex(0); getNumeratorFormat().format(fraction.getNumerator(), toAppendTo, pos); toAppendTo.append(" / "); getDenominatorFormat().format(fraction.getDenominator(), toAppendTo, pos); return toAppendTo; } /** * Formats an object and appends the result to a StringBuffer. obj must be either a * {@link Fraction} object or a {@link Number} object. Any other type of * object will result in an {@link IllegalArgumentException} being thrown. * * @param obj the object to format. * @param toAppendTo where the text is to be appended * @param pos On input: an alignment field, if desired. On output: the * offsets of the alignment field * @return the value passed in as toAppendTo. * @see java.text.Format#format(java.lang.Object, java.lang.StringBuffer, java.text.FieldPosition) * @throws FractionConversionException if the number cannot be converted to a fraction * @throws MathIllegalArgumentException if obj is not a valid type. */ @Override public StringBuffer format(final Object obj, final StringBuffer toAppendTo, final FieldPosition pos) throws FractionConversionException, MathIllegalArgumentException { StringBuffer ret = null; if (obj instanceof Fraction) { ret = format((Fraction) obj, toAppendTo, pos); } else if (obj instanceof Number) { ret = format(new Fraction(((Number) obj).doubleValue()), toAppendTo, pos); } else { throw new MathIllegalArgumentException(LocalizedFormats.CANNOT_FORMAT_OBJECT_TO_FRACTION); } return ret; } /** * Parses a string to produce a {@link Fraction} object. * @param source the string to parse * @return the parsed {@link Fraction} object. * @exception MathParseException if the beginning of the specified string * cannot be parsed. */ @Override public Fraction parse(final String source) throws MathParseException { final ParsePosition parsePosition = new ParsePosition(0); final Fraction result = parse(source, parsePosition); if (parsePosition.getIndex() == 0) { throw new MathParseException(source, parsePosition.getErrorIndex(), Fraction.class); } return result; } /** * Parses a string to produce a {@link Fraction} object. This method * expects the string to be formatted as an improper fraction. * @param source the string to parse * @param pos input/output parsing parameter. * @return the parsed {@link Fraction} object. */ @Override public Fraction parse(final String source, final ParsePosition pos) { final int initialIndex = pos.getIndex(); // parse whitespace parseAndIgnoreWhitespace(source, pos); // parse numerator final Number num = getNumeratorFormat().parse(source, pos); if (num == null) { // invalid integer number // set index back to initial, error index should already be set // character examined. pos.setIndex(initialIndex); return null; } // parse '/' final int startIndex = pos.getIndex(); final char c = parseNextCharacter(source, pos); switch (c) { case 0 : // no '/' // return num as a fraction return new Fraction(num.intValue(), 1); case '/' : // found '/', continue parsing denominator break; default : // invalid '/' // set index back to initial, error index should be the last // character examined. pos.setIndex(initialIndex); pos.setErrorIndex(startIndex); return null; } // parse whitespace parseAndIgnoreWhitespace(source, pos); // parse denominator final Number den = getDenominatorFormat().parse(source, pos); if (den == null) { // invalid integer number // set index back to initial, error index should already be set // character examined. pos.setIndex(initialIndex); return null; } return new Fraction(num.intValue(), den.intValue()); } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/fraction/Fraction.java100644 1750 1750 55647 12126627715 27003 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.fraction; import java.io.Serializable; import java.math.BigInteger; import org.apache.commons.math3.FieldElement; import org.apache.commons.math3.exception.util.LocalizedFormats; import org.apache.commons.math3.exception.MathArithmeticException; import org.apache.commons.math3.exception.NullArgumentException; import org.apache.commons.math3.util.ArithmeticUtils; import org.apache.commons.math3.util.FastMath; /** * Representation of a rational number. * * implements Serializable since 2.0 * * @since 1.1 * @version $Id: Fraction.java 1416643 2012-12-03 19:37:14Z tn $ */ public class Fraction extends Number implements FieldElement, Comparable, Serializable { /** A fraction representing "2 / 1". */ public static final Fraction TWO = new Fraction(2, 1); /** A fraction representing "1". */ public static final Fraction ONE = new Fraction(1, 1); /** A fraction representing "0". */ public static final Fraction ZERO = new Fraction(0, 1); /** A fraction representing "4/5". */ public static final Fraction FOUR_FIFTHS = new Fraction(4, 5); /** A fraction representing "1/5". */ public static final Fraction ONE_FIFTH = new Fraction(1, 5); /** A fraction representing "1/2". */ public static final Fraction ONE_HALF = new Fraction(1, 2); /** A fraction representing "1/4". */ public static final Fraction ONE_QUARTER = new Fraction(1, 4); /** A fraction representing "1/3". */ public static final Fraction ONE_THIRD = new Fraction(1, 3); /** A fraction representing "3/5". */ public static final Fraction THREE_FIFTHS = new Fraction(3, 5); /** A fraction representing "3/4". */ public static final Fraction THREE_QUARTERS = new Fraction(3, 4); /** A fraction representing "2/5". */ public static final Fraction TWO_FIFTHS = new Fraction(2, 5); /** A fraction representing "2/4". */ public static final Fraction TWO_QUARTERS = new Fraction(2, 4); /** A fraction representing "2/3". */ public static final Fraction TWO_THIRDS = new Fraction(2, 3); /** A fraction representing "-1 / 1". */ public static final Fraction MINUS_ONE = new Fraction(-1, 1); /** Serializable version identifier */ private static final long serialVersionUID = 3698073679419233275L; /** The denominator. */ private final int denominator; /** The numerator. */ private final int numerator; /** * Create a fraction given the double value. * @param value the double value to convert to a fraction. * @throws FractionConversionException if the continued fraction failed to * converge. */ public Fraction(double value) throws FractionConversionException { this(value, 1.0e-5, 100); } /** * Create a fraction given the double value and maximum error allowed. *

                                    * References: *

                                    *

                                    * @param value the double value to convert to a fraction. * @param epsilon maximum error allowed. The resulting fraction is within * {@code epsilon} of {@code value}, in absolute terms. * @param maxIterations maximum number of convergents * @throws FractionConversionException if the continued fraction failed to * converge. */ public Fraction(double value, double epsilon, int maxIterations) throws FractionConversionException { this(value, epsilon, Integer.MAX_VALUE, maxIterations); } /** * Create a fraction given the double value and maximum denominator. *

                                    * References: *

                                    *

                                    * @param value the double value to convert to a fraction. * @param maxDenominator The maximum allowed value for denominator * @throws FractionConversionException if the continued fraction failed to * converge */ public Fraction(double value, int maxDenominator) throws FractionConversionException { this(value, 0, maxDenominator, 100); } /** * Create a fraction given the double value and either the maximum error * allowed or the maximum number of denominator digits. *

                                    * * NOTE: This constructor is called with EITHER * - a valid epsilon value and the maxDenominator set to Integer.MAX_VALUE * (that way the maxDenominator has no effect). * OR * - a valid maxDenominator value and the epsilon value set to zero * (that way epsilon only has effect if there is an exact match before * the maxDenominator value is reached). *

                                    * * It has been done this way so that the same code can be (re)used for both * scenarios. However this could be confusing to users if it were part of * the public API and this constructor should therefore remain PRIVATE. *

                                    * * See JIRA issue ticket MATH-181 for more details: * * https://issues.apache.org/jira/browse/MATH-181 * * @param value the double value to convert to a fraction. * @param epsilon maximum error allowed. The resulting fraction is within * {@code epsilon} of {@code value}, in absolute terms. * @param maxDenominator maximum denominator value allowed. * @param maxIterations maximum number of convergents * @throws FractionConversionException if the continued fraction failed to * converge. */ private Fraction(double value, double epsilon, int maxDenominator, int maxIterations) throws FractionConversionException { long overflow = Integer.MAX_VALUE; double r0 = value; long a0 = (long)FastMath.floor(r0); if (FastMath.abs(a0) > overflow) { throw new FractionConversionException(value, a0, 1l); } // check for (almost) integer arguments, which should not go // to iterations. if (FastMath.abs(a0 - value) < epsilon) { this.numerator = (int) a0; this.denominator = 1; return; } long p0 = 1; long q0 = 0; long p1 = a0; long q1 = 1; long p2 = 0; long q2 = 1; int n = 0; boolean stop = false; do { ++n; double r1 = 1.0 / (r0 - a0); long a1 = (long)FastMath.floor(r1); p2 = (a1 * p1) + p0; q2 = (a1 * q1) + q0; if ((FastMath.abs(p2) > overflow) || (FastMath.abs(q2) > overflow)) { throw new FractionConversionException(value, p2, q2); } double convergent = (double)p2 / (double)q2; if (n < maxIterations && FastMath.abs(convergent - value) > epsilon && q2 < maxDenominator) { p0 = p1; p1 = p2; q0 = q1; q1 = q2; a0 = a1; r0 = r1; } else { stop = true; } } while (!stop); if (n >= maxIterations) { throw new FractionConversionException(value, maxIterations); } if (q2 < maxDenominator) { this.numerator = (int) p2; this.denominator = (int) q2; } else { this.numerator = (int) p1; this.denominator = (int) q1; } } /** * Create a fraction from an int. * The fraction is num / 1. * @param num the numerator. */ public Fraction(int num) { this(num, 1); } /** * Create a fraction given the numerator and denominator. The fraction is * reduced to lowest terms. * @param num the numerator. * @param den the denominator. * @throws MathArithmeticException if the denominator is {@code zero} */ public Fraction(int num, int den) { if (den == 0) { throw new MathArithmeticException(LocalizedFormats.ZERO_DENOMINATOR_IN_FRACTION, num, den); } if (den < 0) { if (num == Integer.MIN_VALUE || den == Integer.MIN_VALUE) { throw new MathArithmeticException(LocalizedFormats.OVERFLOW_IN_FRACTION, num, den); } num = -num; den = -den; } // reduce numerator and denominator by greatest common denominator. final int d = ArithmeticUtils.gcd(num, den); if (d > 1) { num /= d; den /= d; } // move sign to numerator. if (den < 0) { num = -num; den = -den; } this.numerator = num; this.denominator = den; } /** * Returns the absolute value of this fraction. * @return the absolute value. */ public Fraction abs() { Fraction ret; if (numerator >= 0) { ret = this; } else { ret = negate(); } return ret; } /** * Compares this object to another based on size. * @param object the object to compare to * @return -1 if this is less than object, +1 if this is greater * than object, 0 if they are equal. */ public int compareTo(Fraction object) { long nOd = ((long) numerator) * object.denominator; long dOn = ((long) denominator) * object.numerator; return (nOd < dOn) ? -1 : ((nOd > dOn) ? +1 : 0); } /** * Gets the fraction as a double. This calculates the fraction as * the numerator divided by denominator. * @return the fraction as a double */ @Override public double doubleValue() { return (double)numerator / (double)denominator; } /** * Test for the equality of two fractions. If the lowest term * numerator and denominators are the same for both fractions, the two * fractions are considered to be equal. * @param other fraction to test for equality to this fraction * @return true if two fractions are equal, false if object is * null, not an instance of {@link Fraction}, or not equal * to this fraction instance. */ @Override public boolean equals(Object other) { if (this == other) { return true; } if (other instanceof Fraction) { // since fractions are always in lowest terms, numerators and // denominators can be compared directly for equality. Fraction rhs = (Fraction)other; return (numerator == rhs.numerator) && (denominator == rhs.denominator); } return false; } /** * Gets the fraction as a float. This calculates the fraction as * the numerator divided by denominator. * @return the fraction as a float */ @Override public float floatValue() { return (float)doubleValue(); } /** * Access the denominator. * @return the denominator. */ public int getDenominator() { return denominator; } /** * Access the numerator. * @return the numerator. */ public int getNumerator() { return numerator; } /** * Gets a hashCode for the fraction. * @return a hash code value for this object */ @Override public int hashCode() { return 37 * (37 * 17 + numerator) + denominator; } /** * Gets the fraction as an int. This returns the whole number part * of the fraction. * @return the whole number fraction part */ @Override public int intValue() { return (int)doubleValue(); } /** * Gets the fraction as a long. This returns the whole number part * of the fraction. * @return the whole number fraction part */ @Override public long longValue() { return (long)doubleValue(); } /** * Return the additive inverse of this fraction. * @return the negation of this fraction. */ public Fraction negate() { if (numerator==Integer.MIN_VALUE) { throw new MathArithmeticException(LocalizedFormats.OVERFLOW_IN_FRACTION, numerator, denominator); } return new Fraction(-numerator, denominator); } /** * Return the multiplicative inverse of this fraction. * @return the reciprocal fraction */ public Fraction reciprocal() { return new Fraction(denominator, numerator); } /** *

                                    Adds the value of this fraction to another, returning the result in reduced form. * The algorithm follows Knuth, 4.5.1.

                                    * * @param fraction the fraction to add, must not be {@code null} * @return a {@code Fraction} instance with the resulting values * @throws NullArgumentException if the fraction is {@code null} * @throws MathArithmeticException if the resulting numerator or denominator exceeds * {@code Integer.MAX_VALUE} */ public Fraction add(Fraction fraction) { return addSub(fraction, true /* add */); } /** * Add an integer to the fraction. * @param i the integer to add. * @return this + i */ public Fraction add(final int i) { return new Fraction(numerator + i * denominator, denominator); } /** *

                                    Subtracts the value of another fraction from the value of this one, * returning the result in reduced form.

                                    * * @param fraction the fraction to subtract, must not be {@code null} * @return a {@code Fraction} instance with the resulting values * @throws NullArgumentException if the fraction is {@code null} * @throws MathArithmeticException if the resulting numerator or denominator * cannot be represented in an {@code int}. */ public Fraction subtract(Fraction fraction) { return addSub(fraction, false /* subtract */); } /** * Subtract an integer from the fraction. * @param i the integer to subtract. * @return this - i */ public Fraction subtract(final int i) { return new Fraction(numerator - i * denominator, denominator); } /** * Implement add and subtract using algorithm described in Knuth 4.5.1. * * @param fraction the fraction to subtract, must not be {@code null} * @param isAdd true to add, false to subtract * @return a {@code Fraction} instance with the resulting values * @throws NullArgumentException if the fraction is {@code null} * @throws MathArithmeticException if the resulting numerator or denominator * cannot be represented in an {@code int}. */ private Fraction addSub(Fraction fraction, boolean isAdd) { if (fraction == null) { throw new NullArgumentException(LocalizedFormats.FRACTION); } // zero is identity for addition. if (numerator == 0) { return isAdd ? fraction : fraction.negate(); } if (fraction.numerator == 0) { return this; } // if denominators are randomly distributed, d1 will be 1 about 61% // of the time. int d1 = ArithmeticUtils.gcd(denominator, fraction.denominator); if (d1==1) { // result is ( (u*v' +/- u'v) / u'v') int uvp = ArithmeticUtils.mulAndCheck(numerator, fraction.denominator); int upv = ArithmeticUtils.mulAndCheck(fraction.numerator, denominator); return new Fraction (isAdd ? ArithmeticUtils.addAndCheck(uvp, upv) : ArithmeticUtils.subAndCheck(uvp, upv), ArithmeticUtils.mulAndCheck(denominator, fraction.denominator)); } // the quantity 't' requires 65 bits of precision; see knuth 4.5.1 // exercise 7. we're going to use a BigInteger. // t = u(v'/d1) +/- v(u'/d1) BigInteger uvp = BigInteger.valueOf(numerator) .multiply(BigInteger.valueOf(fraction.denominator/d1)); BigInteger upv = BigInteger.valueOf(fraction.numerator) .multiply(BigInteger.valueOf(denominator/d1)); BigInteger t = isAdd ? uvp.add(upv) : uvp.subtract(upv); // but d2 doesn't need extra precision because // d2 = gcd(t,d1) = gcd(t mod d1, d1) int tmodd1 = t.mod(BigInteger.valueOf(d1)).intValue(); int d2 = (tmodd1==0)?d1:ArithmeticUtils.gcd(tmodd1, d1); // result is (t/d2) / (u'/d1)(v'/d2) BigInteger w = t.divide(BigInteger.valueOf(d2)); if (w.bitLength() > 31) { throw new MathArithmeticException(LocalizedFormats.NUMERATOR_OVERFLOW_AFTER_MULTIPLY, w); } return new Fraction (w.intValue(), ArithmeticUtils.mulAndCheck(denominator/d1, fraction.denominator/d2)); } /** *

                                    Multiplies the value of this fraction by another, returning the * result in reduced form.

                                    * * @param fraction the fraction to multiply by, must not be {@code null} * @return a {@code Fraction} instance with the resulting values * @throws NullArgumentException if the fraction is {@code null} * @throws MathArithmeticException if the resulting numerator or denominator exceeds * {@code Integer.MAX_VALUE} */ public Fraction multiply(Fraction fraction) { if (fraction == null) { throw new NullArgumentException(LocalizedFormats.FRACTION); } if (numerator == 0 || fraction.numerator == 0) { return ZERO; } // knuth 4.5.1 // make sure we don't overflow unless the result *must* overflow. int d1 = ArithmeticUtils.gcd(numerator, fraction.denominator); int d2 = ArithmeticUtils.gcd(fraction.numerator, denominator); return getReducedFraction (ArithmeticUtils.mulAndCheck(numerator/d1, fraction.numerator/d2), ArithmeticUtils.mulAndCheck(denominator/d2, fraction.denominator/d1)); } /** * Multiply the fraction by an integer. * @param i the integer to multiply by. * @return this * i */ public Fraction multiply(final int i) { return new Fraction(numerator * i, denominator); } /** *

                                    Divide the value of this fraction by another.

                                    * * @param fraction the fraction to divide by, must not be {@code null} * @return a {@code Fraction} instance with the resulting values * @throws IllegalArgumentException if the fraction is {@code null} * @throws MathArithmeticException if the fraction to divide by is zero * @throws MathArithmeticException if the resulting numerator or denominator exceeds * {@code Integer.MAX_VALUE} */ public Fraction divide(Fraction fraction) { if (fraction == null) { throw new NullArgumentException(LocalizedFormats.FRACTION); } if (fraction.numerator == 0) { throw new MathArithmeticException(LocalizedFormats.ZERO_FRACTION_TO_DIVIDE_BY, fraction.numerator, fraction.denominator); } return multiply(fraction.reciprocal()); } /** * Divide the fraction by an integer. * @param i the integer to divide by. * @return this * i */ public Fraction divide(final int i) { return new Fraction(numerator, denominator * i); } /** *

                                    * Gets the fraction percentage as a double. This calculates the * fraction as the numerator divided by denominator multiplied by 100. *

                                    * * @return the fraction percentage as a double. */ public double percentageValue() { return 100 * doubleValue(); } /** *

                                    Creates a {@code Fraction} instance with the 2 parts * of a fraction Y/Z.

                                    * *

                                    Any negative signs are resolved to be on the numerator.

                                    * * @param numerator the numerator, for example the three in 'three sevenths' * @param denominator the denominator, for example the seven in 'three sevenths' * @return a new fraction instance, with the numerator and denominator reduced * @throws MathArithmeticException if the denominator is {@code zero} */ public static Fraction getReducedFraction(int numerator, int denominator) { if (denominator == 0) { throw new MathArithmeticException(LocalizedFormats.ZERO_DENOMINATOR_IN_FRACTION, numerator, denominator); } if (numerator==0) { return ZERO; // normalize zero. } // allow 2^k/-2^31 as a valid fraction (where k>0) if (denominator==Integer.MIN_VALUE && (numerator&1)==0) { numerator/=2; denominator/=2; } if (denominator < 0) { if (numerator==Integer.MIN_VALUE || denominator==Integer.MIN_VALUE) { throw new MathArithmeticException(LocalizedFormats.OVERFLOW_IN_FRACTION, numerator, denominator); } numerator = -numerator; denominator = -denominator; } // simplify fraction. int gcd = ArithmeticUtils.gcd(numerator, denominator); numerator /= gcd; denominator /= gcd; return new Fraction(numerator, denominator); } /** *

                                    * Returns the {@code String} representing this fraction, ie * "num / dem" or just "num" if the denominator is one. *

                                    * * @return a string representation of the fraction. * @see java.lang.Object#toString() */ @Override public String toString() { String str = null; if (denominator == 1) { str = Integer.toString(numerator); } else if (numerator == 0) { str = "0"; } else { str = numerator + " / " + denominator; } return str; } /** {@inheritDoc} */ public FractionField getField() { return FractionField.getInstance(); } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/fraction/BigFraction.java100644 1750 1750 113034 12126627715 27426 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.fraction; import java.io.Serializable; import java.math.BigDecimal; import java.math.BigInteger; import org.apache.commons.math3.FieldElement; import org.apache.commons.math3.exception.MathArithmeticException; import org.apache.commons.math3.exception.MathIllegalArgumentException; import org.apache.commons.math3.exception.NullArgumentException; import org.apache.commons.math3.exception.ZeroException; import org.apache.commons.math3.exception.util.LocalizedFormats; import org.apache.commons.math3.util.ArithmeticUtils; import org.apache.commons.math3.util.FastMath; import org.apache.commons.math3.util.MathUtils; /** * Representation of a rational number without any overflow. This class is * immutable. * * @version $Id: BigFraction.java 1416643 2012-12-03 19:37:14Z tn $ * @since 2.0 */ public class BigFraction extends Number implements FieldElement, Comparable, Serializable { /** A fraction representing "2 / 1". */ public static final BigFraction TWO = new BigFraction(2); /** A fraction representing "1". */ public static final BigFraction ONE = new BigFraction(1); /** A fraction representing "0". */ public static final BigFraction ZERO = new BigFraction(0); /** A fraction representing "-1 / 1". */ public static final BigFraction MINUS_ONE = new BigFraction(-1); /** A fraction representing "4/5". */ public static final BigFraction FOUR_FIFTHS = new BigFraction(4, 5); /** A fraction representing "1/5". */ public static final BigFraction ONE_FIFTH = new BigFraction(1, 5); /** A fraction representing "1/2". */ public static final BigFraction ONE_HALF = new BigFraction(1, 2); /** A fraction representing "1/4". */ public static final BigFraction ONE_QUARTER = new BigFraction(1, 4); /** A fraction representing "1/3". */ public static final BigFraction ONE_THIRD = new BigFraction(1, 3); /** A fraction representing "3/5". */ public static final BigFraction THREE_FIFTHS = new BigFraction(3, 5); /** A fraction representing "3/4". */ public static final BigFraction THREE_QUARTERS = new BigFraction(3, 4); /** A fraction representing "2/5". */ public static final BigFraction TWO_FIFTHS = new BigFraction(2, 5); /** A fraction representing "2/4". */ public static final BigFraction TWO_QUARTERS = new BigFraction(2, 4); /** A fraction representing "2/3". */ public static final BigFraction TWO_THIRDS = new BigFraction(2, 3); /** Serializable version identifier. */ private static final long serialVersionUID = -5630213147331578515L; /** BigInteger representation of 100. */ private static final BigInteger ONE_HUNDRED = BigInteger.valueOf(100); /** The numerator. */ private final BigInteger numerator; /** The denominator. */ private final BigInteger denominator; /** *

                                    * Create a {@link BigFraction} equivalent to the passed BigInteger, ie * "num / 1". *

                                    * * @param num * the numerator. */ public BigFraction(final BigInteger num) { this(num, BigInteger.ONE); } /** * Create a {@link BigFraction} given the numerator and denominator as * {@code BigInteger}. The {@link BigFraction} is reduced to lowest terms. * * @param num the numerator, must not be {@code null}. * @param den the denominator, must not be {@code null}. * @throws ZeroException if the denominator is zero. * @throws NullArgumentException if either of the arguments is null */ public BigFraction(BigInteger num, BigInteger den) { MathUtils.checkNotNull(num, LocalizedFormats.NUMERATOR); MathUtils.checkNotNull(den, LocalizedFormats.DENOMINATOR); if (BigInteger.ZERO.equals(den)) { throw new ZeroException(LocalizedFormats.ZERO_DENOMINATOR); } if (BigInteger.ZERO.equals(num)) { numerator = BigInteger.ZERO; denominator = BigInteger.ONE; } else { // reduce numerator and denominator by greatest common denominator final BigInteger gcd = num.gcd(den); if (BigInteger.ONE.compareTo(gcd) < 0) { num = num.divide(gcd); den = den.divide(gcd); } // move sign to numerator if (BigInteger.ZERO.compareTo(den) > 0) { num = num.negate(); den = den.negate(); } // store the values in the final fields numerator = num; denominator = den; } } /** * Create a fraction given the double value. *

                                    * This constructor behaves differently from * {@link #BigFraction(double, double, int)}. It converts the double value * exactly, considering its internal bits representation. This works for all * values except NaN and infinities and does not requires any loop or * convergence threshold. *

                                    *

                                    * Since this conversion is exact and since double numbers are sometimes * approximated, the fraction created may seem strange in some cases. For example, * calling new BigFraction(1.0 / 3.0) does not create * the fraction 1/3, but the fraction 6004799503160661 / 18014398509481984 * because the double number passed to the constructor is not exactly 1/3 * (this number cannot be stored exactly in IEEE754). *

                                    * @see #BigFraction(double, double, int) * @param value the double value to convert to a fraction. * @exception MathIllegalArgumentException if value is NaN or infinite */ public BigFraction(final double value) throws MathIllegalArgumentException { if (Double.isNaN(value)) { throw new MathIllegalArgumentException(LocalizedFormats.NAN_VALUE_CONVERSION); } if (Double.isInfinite(value)) { throw new MathIllegalArgumentException(LocalizedFormats.INFINITE_VALUE_CONVERSION); } // compute m and k such that value = m * 2^k final long bits = Double.doubleToLongBits(value); final long sign = bits & 0x8000000000000000L; final long exponent = bits & 0x7ff0000000000000L; long m = bits & 0x000fffffffffffffL; if (exponent != 0) { // this was a normalized number, add the implicit most significant bit m |= 0x0010000000000000L; } if (sign != 0) { m = -m; } int k = ((int) (exponent >> 52)) - 1075; while (((m & 0x001ffffffffffffeL) != 0) && ((m & 0x1) == 0)) { m = m >> 1; ++k; } if (k < 0) { numerator = BigInteger.valueOf(m); denominator = BigInteger.ZERO.flipBit(-k); } else { numerator = BigInteger.valueOf(m).multiply(BigInteger.ZERO.flipBit(k)); denominator = BigInteger.ONE; } } /** * Create a fraction given the double value and maximum error allowed. *

                                    * References: *

                                    *

                                    * * @param value * the double value to convert to a fraction. * @param epsilon * maximum error allowed. The resulting fraction is within * epsilon of value, in absolute terms. * @param maxIterations * maximum number of convergents. * @throws FractionConversionException * if the continued fraction failed to converge. * @see #BigFraction(double) */ public BigFraction(final double value, final double epsilon, final int maxIterations) throws FractionConversionException { this(value, epsilon, Integer.MAX_VALUE, maxIterations); } /** * Create a fraction given the double value and either the maximum error * allowed or the maximum number of denominator digits. *

                                    * * NOTE: This constructor is called with EITHER - a valid epsilon value and * the maxDenominator set to Integer.MAX_VALUE (that way the maxDenominator * has no effect). OR - a valid maxDenominator value and the epsilon value * set to zero (that way epsilon only has effect if there is an exact match * before the maxDenominator value is reached). *

                                    *

                                    * * It has been done this way so that the same code can be (re)used for both * scenarios. However this could be confusing to users if it were part of * the public API and this constructor should therefore remain PRIVATE. *

                                    * * See JIRA issue ticket MATH-181 for more details: * * https://issues.apache.org/jira/browse/MATH-181 * * @param value * the double value to convert to a fraction. * @param epsilon * maximum error allowed. The resulting fraction is within * epsilon of value, in absolute terms. * @param maxDenominator * maximum denominator value allowed. * @param maxIterations * maximum number of convergents. * @throws FractionConversionException * if the continued fraction failed to converge. */ private BigFraction(final double value, final double epsilon, final int maxDenominator, int maxIterations) throws FractionConversionException { long overflow = Integer.MAX_VALUE; double r0 = value; long a0 = (long) FastMath.floor(r0); if (a0 > overflow) { throw new FractionConversionException(value, a0, 1l); } // check for (almost) integer arguments, which should not go // to iterations. if (FastMath.abs(a0 - value) < epsilon) { numerator = BigInteger.valueOf(a0); denominator = BigInteger.ONE; return; } long p0 = 1; long q0 = 0; long p1 = a0; long q1 = 1; long p2 = 0; long q2 = 1; int n = 0; boolean stop = false; do { ++n; final double r1 = 1.0 / (r0 - a0); final long a1 = (long) FastMath.floor(r1); p2 = (a1 * p1) + p0; q2 = (a1 * q1) + q0; if ((p2 > overflow) || (q2 > overflow)) { throw new FractionConversionException(value, p2, q2); } final double convergent = (double) p2 / (double) q2; if ((n < maxIterations) && (FastMath.abs(convergent - value) > epsilon) && (q2 < maxDenominator)) { p0 = p1; p1 = p2; q0 = q1; q1 = q2; a0 = a1; r0 = r1; } else { stop = true; } } while (!stop); if (n >= maxIterations) { throw new FractionConversionException(value, maxIterations); } if (q2 < maxDenominator) { numerator = BigInteger.valueOf(p2); denominator = BigInteger.valueOf(q2); } else { numerator = BigInteger.valueOf(p1); denominator = BigInteger.valueOf(q1); } } /** * Create a fraction given the double value and maximum denominator. *

                                    * References: *

                                    *

                                    * * @param value * the double value to convert to a fraction. * @param maxDenominator * The maximum allowed value for denominator. * @throws FractionConversionException * if the continued fraction failed to converge. */ public BigFraction(final double value, final int maxDenominator) throws FractionConversionException { this(value, 0, maxDenominator, 100); } /** *

                                    * Create a {@link BigFraction} equivalent to the passed int, ie * "num / 1". *

                                    * * @param num * the numerator. */ public BigFraction(final int num) { this(BigInteger.valueOf(num), BigInteger.ONE); } /** *

                                    * Create a {@link BigFraction} given the numerator and denominator as simple * int. The {@link BigFraction} is reduced to lowest terms. *

                                    * * @param num * the numerator. * @param den * the denominator. */ public BigFraction(final int num, final int den) { this(BigInteger.valueOf(num), BigInteger.valueOf(den)); } /** *

                                    * Create a {@link BigFraction} equivalent to the passed long, ie "num / 1". *

                                    * * @param num * the numerator. */ public BigFraction(final long num) { this(BigInteger.valueOf(num), BigInteger.ONE); } /** *

                                    * Create a {@link BigFraction} given the numerator and denominator as simple * long. The {@link BigFraction} is reduced to lowest terms. *

                                    * * @param num * the numerator. * @param den * the denominator. */ public BigFraction(final long num, final long den) { this(BigInteger.valueOf(num), BigInteger.valueOf(den)); } /** *

                                    * Creates a BigFraction instance with the 2 parts of a fraction * Y/Z. *

                                    * *

                                    * Any negative signs are resolved to be on the numerator. *

                                    * * @param numerator * the numerator, for example the three in 'three sevenths'. * @param denominator * the denominator, for example the seven in 'three sevenths'. * @return a new fraction instance, with the numerator and denominator * reduced. * @throws ArithmeticException * if the denominator is zero. */ public static BigFraction getReducedFraction(final int numerator, final int denominator) { if (numerator == 0) { return ZERO; // normalize zero. } return new BigFraction(numerator, denominator); } /** *

                                    * Returns the absolute value of this {@link BigFraction}. *

                                    * * @return the absolute value as a {@link BigFraction}. */ public BigFraction abs() { return (BigInteger.ZERO.compareTo(numerator) <= 0) ? this : negate(); } /** *

                                    * Adds the value of this fraction to the passed {@link BigInteger}, * returning the result in reduced form. *

                                    * * @param bg * the {@link BigInteger} to add, must'nt be null. * @return a BigFraction instance with the resulting values. * @throws NullArgumentException * if the {@link BigInteger} is null. */ public BigFraction add(final BigInteger bg) throws NullArgumentException { MathUtils.checkNotNull(bg); return new BigFraction(numerator.add(denominator.multiply(bg)), denominator); } /** *

                                    * Adds the value of this fraction to the passed integer, returning * the result in reduced form. *

                                    * * @param i * the integer to add. * @return a BigFraction instance with the resulting values. */ public BigFraction add(final int i) { return add(BigInteger.valueOf(i)); } /** *

                                    * Adds the value of this fraction to the passed long, returning * the result in reduced form. *

                                    * * @param l * the long to add. * @return a BigFraction instance with the resulting values. */ public BigFraction add(final long l) { return add(BigInteger.valueOf(l)); } /** *

                                    * Adds the value of this fraction to another, returning the result in * reduced form. *

                                    * * @param fraction * the {@link BigFraction} to add, must not be null. * @return a {@link BigFraction} instance with the resulting values. * @throws NullArgumentException if the {@link BigFraction} is {@code null}. */ public BigFraction add(final BigFraction fraction) { if (fraction == null) { throw new NullArgumentException(LocalizedFormats.FRACTION); } if (ZERO.equals(fraction)) { return this; } BigInteger num = null; BigInteger den = null; if (denominator.equals(fraction.denominator)) { num = numerator.add(fraction.numerator); den = denominator; } else { num = (numerator.multiply(fraction.denominator)).add((fraction.numerator).multiply(denominator)); den = denominator.multiply(fraction.denominator); } return new BigFraction(num, den); } /** *

                                    * Gets the fraction as a BigDecimal. This calculates the * fraction as the numerator divided by denominator. *

                                    * * @return the fraction as a BigDecimal. * @throws ArithmeticException * if the exact quotient does not have a terminating decimal * expansion. * @see BigDecimal */ public BigDecimal bigDecimalValue() { return new BigDecimal(numerator).divide(new BigDecimal(denominator)); } /** *

                                    * Gets the fraction as a BigDecimal following the passed * rounding mode. This calculates the fraction as the numerator divided by * denominator. *

                                    * * @param roundingMode * rounding mode to apply. see {@link BigDecimal} constants. * @return the fraction as a BigDecimal. * @throws IllegalArgumentException * if roundingMode does not represent a valid rounding * mode. * @see BigDecimal */ public BigDecimal bigDecimalValue(final int roundingMode) { return new BigDecimal(numerator).divide(new BigDecimal(denominator), roundingMode); } /** *

                                    * Gets the fraction as a BigDecimal following the passed scale * and rounding mode. This calculates the fraction as the numerator divided * by denominator. *

                                    * * @param scale * scale of the BigDecimal quotient to be returned. * see {@link BigDecimal} for more information. * @param roundingMode * rounding mode to apply. see {@link BigDecimal} constants. * @return the fraction as a BigDecimal. * @see BigDecimal */ public BigDecimal bigDecimalValue(final int scale, final int roundingMode) { return new BigDecimal(numerator).divide(new BigDecimal(denominator), scale, roundingMode); } /** *

                                    * Compares this object to another based on size. *

                                    * * @param object * the object to compare to, must not be null. * @return -1 if this is less than object, +1 if this is greater * than object, 0 if they are equal. * @see java.lang.Comparable#compareTo(java.lang.Object) */ public int compareTo(final BigFraction object) { BigInteger nOd = numerator.multiply(object.denominator); BigInteger dOn = denominator.multiply(object.numerator); return nOd.compareTo(dOn); } /** *

                                    * Divide the value of this fraction by the passed {@code BigInteger}, * ie {@code this * 1 / bg}, returning the result in reduced form. *

                                    * * @param bg the {@code BigInteger} to divide by, must not be {@code null} * @return a {@link BigFraction} instance with the resulting values * @throws NullArgumentException if the {@code BigInteger} is {@code null} * @throws MathArithmeticException if the fraction to divide by is zero */ public BigFraction divide(final BigInteger bg) { if (bg == null) { throw new NullArgumentException(LocalizedFormats.FRACTION); } if (BigInteger.ZERO.equals(bg)) { throw new MathArithmeticException(LocalizedFormats.ZERO_DENOMINATOR); } return new BigFraction(numerator, denominator.multiply(bg)); } /** *

                                    * Divide the value of this fraction by the passed {@code int}, ie * {@code this * 1 / i}, returning the result in reduced form. *

                                    * * @param i the {@code int} to divide by * @return a {@link BigFraction} instance with the resulting values * @throws MathArithmeticException if the fraction to divide by is zero */ public BigFraction divide(final int i) { return divide(BigInteger.valueOf(i)); } /** *

                                    * Divide the value of this fraction by the passed {@code long}, ie * {@code this * 1 / l}, returning the result in reduced form. *

                                    * * @param l the {@code long} to divide by * @return a {@link BigFraction} instance with the resulting values * @throws MathArithmeticException if the fraction to divide by is zero */ public BigFraction divide(final long l) { return divide(BigInteger.valueOf(l)); } /** *

                                    * Divide the value of this fraction by another, returning the result in * reduced form. *

                                    * * @param fraction Fraction to divide by, must not be {@code null}. * @return a {@link BigFraction} instance with the resulting values. * @throws NullArgumentException if the {@code fraction} is {@code null}. * @throws MathArithmeticException if the fraction to divide by is zero */ public BigFraction divide(final BigFraction fraction) { if (fraction == null) { throw new NullArgumentException(LocalizedFormats.FRACTION); } if (BigInteger.ZERO.equals(fraction.numerator)) { throw new MathArithmeticException(LocalizedFormats.ZERO_DENOMINATOR); } return multiply(fraction.reciprocal()); } /** *

                                    * Gets the fraction as a double. This calculates the fraction as * the numerator divided by denominator. *

                                    * * @return the fraction as a double * @see java.lang.Number#doubleValue() */ @Override public double doubleValue() { double result = numerator.doubleValue() / denominator.doubleValue(); if (Double.isNaN(result)) { // Numerator and/or denominator must be out of range: // Calculate how far to shift them to put them in range. int shift = Math.max(numerator.bitLength(), denominator.bitLength()) - FastMath.getExponent(Double.MAX_VALUE); result = numerator.shiftRight(shift).doubleValue() / denominator.shiftRight(shift).doubleValue(); } return result; } /** *

                                    * Test for the equality of two fractions. If the lowest term numerator and * denominators are the same for both fractions, the two fractions are * considered to be equal. *

                                    * * @param other * fraction to test for equality to this fraction, can be * null. * @return true if two fractions are equal, false if object is * null, not an instance of {@link BigFraction}, or not * equal to this fraction instance. * @see java.lang.Object#equals(java.lang.Object) */ @Override public boolean equals(final Object other) { boolean ret = false; if (this == other) { ret = true; } else if (other instanceof BigFraction) { BigFraction rhs = ((BigFraction) other).reduce(); BigFraction thisOne = this.reduce(); ret = thisOne.numerator.equals(rhs.numerator) && thisOne.denominator.equals(rhs.denominator); } return ret; } /** *

                                    * Gets the fraction as a float. This calculates the fraction as * the numerator divided by denominator. *

                                    * * @return the fraction as a float. * @see java.lang.Number#floatValue() */ @Override public float floatValue() { float result = numerator.floatValue() / denominator.floatValue(); if (Double.isNaN(result)) { // Numerator and/or denominator must be out of range: // Calculate how far to shift them to put them in range. int shift = Math.max(numerator.bitLength(), denominator.bitLength()) - FastMath.getExponent(Float.MAX_VALUE); result = numerator.shiftRight(shift).floatValue() / denominator.shiftRight(shift).floatValue(); } return result; } /** *

                                    * Access the denominator as a BigInteger. *

                                    * * @return the denominator as a BigInteger. */ public BigInteger getDenominator() { return denominator; } /** *

                                    * Access the denominator as a int. *

                                    * * @return the denominator as a int. */ public int getDenominatorAsInt() { return denominator.intValue(); } /** *

                                    * Access the denominator as a long. *

                                    * * @return the denominator as a long. */ public long getDenominatorAsLong() { return denominator.longValue(); } /** *

                                    * Access the numerator as a BigInteger. *

                                    * * @return the numerator as a BigInteger. */ public BigInteger getNumerator() { return numerator; } /** *

                                    * Access the numerator as a int. *

                                    * * @return the numerator as a int. */ public int getNumeratorAsInt() { return numerator.intValue(); } /** *

                                    * Access the numerator as a long. *

                                    * * @return the numerator as a long. */ public long getNumeratorAsLong() { return numerator.longValue(); } /** *

                                    * Gets a hashCode for the fraction. *

                                    * * @return a hash code value for this object. * @see java.lang.Object#hashCode() */ @Override public int hashCode() { return 37 * (37 * 17 + numerator.hashCode()) + denominator.hashCode(); } /** *

                                    * Gets the fraction as an int. This returns the whole number part * of the fraction. *

                                    * * @return the whole number fraction part. * @see java.lang.Number#intValue() */ @Override public int intValue() { return numerator.divide(denominator).intValue(); } /** *

                                    * Gets the fraction as a long. This returns the whole number part * of the fraction. *

                                    * * @return the whole number fraction part. * @see java.lang.Number#longValue() */ @Override public long longValue() { return numerator.divide(denominator).longValue(); } /** *

                                    * Multiplies the value of this fraction by the passed * BigInteger, returning the result in reduced form. *

                                    * * @param bg the {@code BigInteger} to multiply by. * @return a {@code BigFraction} instance with the resulting values. * @throws NullArgumentException if {@code bg} is {@code null}. */ public BigFraction multiply(final BigInteger bg) { if (bg == null) { throw new NullArgumentException(); } return new BigFraction(bg.multiply(numerator), denominator); } /** *

                                    * Multiply the value of this fraction by the passed int, returning * the result in reduced form. *

                                    * * @param i * the int to multiply by. * @return a {@link BigFraction} instance with the resulting values. */ public BigFraction multiply(final int i) { return multiply(BigInteger.valueOf(i)); } /** *

                                    * Multiply the value of this fraction by the passed long, * returning the result in reduced form. *

                                    * * @param l * the long to multiply by. * @return a {@link BigFraction} instance with the resulting values. */ public BigFraction multiply(final long l) { return multiply(BigInteger.valueOf(l)); } /** *

                                    * Multiplies the value of this fraction by another, returning the result in * reduced form. *

                                    * * @param fraction Fraction to multiply by, must not be {@code null}. * @return a {@link BigFraction} instance with the resulting values. * @throws NullArgumentException if {@code fraction} is {@code null}. */ public BigFraction multiply(final BigFraction fraction) { if (fraction == null) { throw new NullArgumentException(LocalizedFormats.FRACTION); } if (numerator.equals(BigInteger.ZERO) || fraction.numerator.equals(BigInteger.ZERO)) { return ZERO; } return new BigFraction(numerator.multiply(fraction.numerator), denominator.multiply(fraction.denominator)); } /** *

                                    * Return the additive inverse of this fraction, returning the result in * reduced form. *

                                    * * @return the negation of this fraction. */ public BigFraction negate() { return new BigFraction(numerator.negate(), denominator); } /** *

                                    * Gets the fraction percentage as a double. This calculates the * fraction as the numerator divided by denominator multiplied by 100. *

                                    * * @return the fraction percentage as a double. */ public double percentageValue() { return multiply(ONE_HUNDRED).doubleValue(); } /** *

                                    * Returns a {@code BigFraction} whose value is * {@code (thisexponent)}, returning the result in reduced form. *

                                    * * @param exponent * exponent to which this {@code BigFraction} is to be * raised. * @return thisexponent. */ public BigFraction pow(final int exponent) { if (exponent < 0) { return new BigFraction(denominator.pow(-exponent), numerator.pow(-exponent)); } return new BigFraction(numerator.pow(exponent), denominator.pow(exponent)); } /** *

                                    * Returns a BigFraction whose value is * (thisexponent), returning the result in reduced form. *

                                    * * @param exponent * exponent to which this BigFraction is to be raised. * @return thisexponent as a BigFraction. */ public BigFraction pow(final long exponent) { if (exponent < 0) { return new BigFraction(ArithmeticUtils.pow(denominator, -exponent), ArithmeticUtils.pow(numerator, -exponent)); } return new BigFraction(ArithmeticUtils.pow(numerator, exponent), ArithmeticUtils.pow(denominator, exponent)); } /** *

                                    * Returns a BigFraction whose value is * (thisexponent), returning the result in reduced form. *

                                    * * @param exponent * exponent to which this BigFraction is to be raised. * @return thisexponent as a BigFraction. */ public BigFraction pow(final BigInteger exponent) { if (exponent.compareTo(BigInteger.ZERO) < 0) { final BigInteger eNeg = exponent.negate(); return new BigFraction(ArithmeticUtils.pow(denominator, eNeg), ArithmeticUtils.pow(numerator, eNeg)); } return new BigFraction(ArithmeticUtils.pow(numerator, exponent), ArithmeticUtils.pow(denominator, exponent)); } /** *

                                    * Returns a double whose value is * (thisexponent), returning the result in reduced form. *

                                    * * @param exponent * exponent to which this BigFraction is to be raised. * @return thisexponent. */ public double pow(final double exponent) { return FastMath.pow(numerator.doubleValue(), exponent) / FastMath.pow(denominator.doubleValue(), exponent); } /** *

                                    * Return the multiplicative inverse of this fraction. *

                                    * * @return the reciprocal fraction. */ public BigFraction reciprocal() { return new BigFraction(denominator, numerator); } /** *

                                    * Reduce this BigFraction to its lowest terms. *

                                    * * @return the reduced BigFraction. It doesn't change anything if * the fraction can be reduced. */ public BigFraction reduce() { final BigInteger gcd = numerator.gcd(denominator); return new BigFraction(numerator.divide(gcd), denominator.divide(gcd)); } /** *

                                    * Subtracts the value of an {@link BigInteger} from the value of this * {@code BigFraction}, returning the result in reduced form. *

                                    * * @param bg the {@link BigInteger} to subtract, cannot be {@code null}. * @return a {@code BigFraction} instance with the resulting values. * @throws NullArgumentException if the {@link BigInteger} is {@code null}. */ public BigFraction subtract(final BigInteger bg) { if (bg == null) { throw new NullArgumentException(); } return new BigFraction(numerator.subtract(denominator.multiply(bg)), denominator); } /** *

                                    * Subtracts the value of an {@code integer} from the value of this * {@code BigFraction}, returning the result in reduced form. *

                                    * * @param i the {@code integer} to subtract. * @return a {@code BigFraction} instance with the resulting values. */ public BigFraction subtract(final int i) { return subtract(BigInteger.valueOf(i)); } /** *

                                    * Subtracts the value of a {@code long} from the value of this * {@code BigFraction}, returning the result in reduced form. *

                                    * * @param l the {@code long} to subtract. * @return a {@code BigFraction} instance with the resulting values. */ public BigFraction subtract(final long l) { return subtract(BigInteger.valueOf(l)); } /** *

                                    * Subtracts the value of another fraction from the value of this one, * returning the result in reduced form. *

                                    * * @param fraction {@link BigFraction} to subtract, must not be {@code null}. * @return a {@link BigFraction} instance with the resulting values * @throws NullArgumentException if the {@code fraction} is {@code null}. */ public BigFraction subtract(final BigFraction fraction) { if (fraction == null) { throw new NullArgumentException(LocalizedFormats.FRACTION); } if (ZERO.equals(fraction)) { return this; } BigInteger num = null; BigInteger den = null; if (denominator.equals(fraction.denominator)) { num = numerator.subtract(fraction.numerator); den = denominator; } else { num = (numerator.multiply(fraction.denominator)).subtract((fraction.numerator).multiply(denominator)); den = denominator.multiply(fraction.denominator); } return new BigFraction(num, den); } /** *

                                    * Returns the String representing this fraction, ie * "num / dem" or just "num" if the denominator is one. *

                                    * * @return a string representation of the fraction. * @see java.lang.Object#toString() */ @Override public String toString() { String str = null; if (BigInteger.ONE.equals(denominator)) { str = numerator.toString(); } else if (BigInteger.ZERO.equals(numerator)) { str = "0"; } else { str = numerator + " / " + denominator; } return str; } /** {@inheritDoc} */ public BigFractionField getField() { return BigFractionField.getInstance(); } } commons-math3-3.2-src/src/main/java/org/apache/commons/math3/fraction/AbstractFormat.java100644 1750 1750 16764 12126627715 30147 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ package org.apache.commons.math3.fraction; import java.io.Serializable; import java.text.FieldPosition; import java.text.NumberFormat; import java.text.ParsePosition; import java.util.Locale; import org.apache.commons.math3.exception.NullArgumentException; import org.apache.commons.math3.exception.util.LocalizedFormats; /** * Common part shared by both {@link FractionFormat} and {@link BigFractionFormat}. * @version $Id: AbstractFormat.java 1416643 2012-12-03 19:37:14Z tn $ * @since 2.0 */ public abstract class AbstractFormat extends NumberFormat implements Serializable { /** Serializable version identifier. */ private static final long serialVersionUID = -6981118387974191891L; /** The format used for the denominator. */ private NumberFormat denominatorFormat; /** The format used for the numerator. */ private NumberFormat numeratorFormat; /** * Create an improper formatting instance with the default number format * for the numerator and denominator. */ protected AbstractFormat() { this(getDefaultNumberFormat()); } /** * Create an improper formatting instance with a custom number format for * both the numerator and denominator. * @param format the custom format for both the numerator and denominator. */ protected AbstractFormat(final NumberFormat format) { this(format, (NumberFormat) format.clone()); } /** * Create an improper formatting instance with a custom number format for * the numerator and a custom number format for the denominator. * @param numeratorFormat the custom format for the numerator. * @param denominatorFormat the custom format for the denominator. */ protected AbstractFormat(final NumberFormat numeratorFormat, final NumberFormat denominatorFormat) { this.numeratorFormat = numeratorFormat; this.denominatorFormat = denominatorFormat; } /** * Create a default number format. The default number format is based on * {@link NumberFormat#getNumberInstance(java.util.Locale)}. The only * customization is the maximum number of BigFraction digits, which is set to 0. * @return the default number format. */ protected static NumberFormat getDefaultNumberFormat() { return getDefaultNumberFormat(Locale.getDefault()); } /** * Create a default number format. The default number format is based on * {@link NumberFormat#getNumberInstance(java.util.Locale)}. The only * customization is the maximum number of BigFraction digits, which is set to 0. * @param locale the specific locale used by the format. * @return the default number format specific to the given locale. */ protected static NumberFormat getDefaultNumberFormat(final Locale locale) { final NumberFormat nf = NumberFormat.getNumberInstance(locale); nf.setMaximumFractionDigits(0); nf.setParseIntegerOnly(true); return nf; } /** * Access the denominator format. * @return the denominator format. */ public NumberFormat getDenominatorFormat() { return denominatorFormat; } /** * Access the numerator format. * @return the numerator format. */ public NumberFormat getNumeratorFormat() { return numeratorFormat; } /** * Modify the denominator format. * @param format the new denominator format value. * @throws NullArgumentException if {@code format} is {@code null}. */ public void setDenominatorFormat(final NumberFormat format) { if (format == null) { throw new NullArgumentException(LocalizedFormats.DENOMINATOR_FORMAT); } this.denominatorFormat = format; } /** * Modify the numerator format. * @param format the new numerator format value. * @throws NullArgumentException if {@code format} is {@code null}. */ public void setNumeratorFormat(final NumberFormat format) { if (format == null) { throw new NullArgumentException(LocalizedFormats.NUMERATOR_FORMAT); } this.numeratorFormat = format; } /** * Parses source until a non-whitespace character is found. * @param source the string to parse * @param pos input/output parsing parameter. On output, pos * holds the index of the next non-whitespace character. */ protected static void parseAndIgnoreWhitespace(final String source, final ParsePosition pos) { parseNextCharacter(source, pos); pos.setIndex(pos.getIndex() - 1); } /** * Parses source until a non-whitespace character is found. * @param source the string to parse * @param pos input/output parsing parameter. * @return the first non-whitespace character. */ protected static char parseNextCharacter(final String source, final ParsePosition pos) { int index = pos.getIndex(); final int n = source.length(); char ret = 0; if (index < n) { char c; do { c = source.charAt(index++); } while (Character.isWhitespace(c) && index < n); pos.setIndex(index); if (index < n) { ret = c; } } return ret; } /** * Formats a double value as a fraction and appends the result to a StringBuffer. * * @param value the double value to format * @param buffer StringBuffer to append to * @param position On input: an alignment field, if desired. On output: the * offsets of the alignment field * @return a reference to the appended buffer * @see #format(Object, StringBuffer, FieldPosition) */ @Override public StringBuffer format(final double value, final StringBuffer buffer, final FieldPosition position) { return format(Double.valueOf(value), buffer, position); } /** * Formats a long value as a fraction and appends the result to a StringBuffer. * * @param value the long value to format * @param buffer StringBuffer to append to * @param position On input: an alignment field, if desired. On output: the * offsets of the alignment field * @return a reference to the appended buffer * @see #format(Object, StringBuffer, FieldPosition) */ @Override public StringBuffer format(final long value, final StringBuffer buffer, final FieldPosition position) { return format(Long.valueOf(value), buffer, position); } } commons-math3-3.2-src/src/main/assembly/src.xml100644 1750 1750 2624 12126627677 20250 0ustarlucluc 0 0 src tar.gz zip ${project.artifactId}-${commons.release.version}-src *.txt *.xml src **/mantissa/** **/experimental/** **/download*.cgi commons-math3-3.2-src/src/main/assembly/bin.xml100644 1750 1750 3204 12126627677 20224 0ustarlucluc 0 0 bin tar.gz zip false LICENSE* NOTICE* RELEASE-NOTES.txt target *.jar target/site docs **/customsorttypes.js **/sortabletable.js **/stringbuilder.js ././@LongLink100644 0 0 167 12126630647 10266 Lustar 0 0 commons-math3-3.2-src/src/main/resources/assets/org/apache/commons/math3/exception/util/LocalizedFormats_fr.propertiescommons-math3-3.2-src/src/main/resources/assets/org/apache/commons/math3/exception/util/LocalizedFor100644 1750 1750 70421 12126627721 32414 0ustarlucluc 0 0 # Licensed to the Apache Software Foundation (ASF) under one or more # contributor license agreements. See the NOTICE file distributed with # this work for additional information regarding copyright ownership. # The ASF licenses this file to You 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. ARGUMENT_OUTSIDE_DOMAIN = argument {0} hors domaine [{1} ; {2}] ARRAY_SIZE_EXCEEDS_MAX_VARIABLES = la taille de tableau ne devrait pas d\u00e9passer {0} ARRAY_SIZES_SHOULD_HAVE_DIFFERENCE_1 = les tableaux devraient avoir une diff\u00e9rence de taille de 1 ({0} != {1} + 1) ARRAY_SUMS_TO_ZERO = somme du tableau nulle ASSYMETRIC_EIGEN_NOT_SUPPORTED = la d\u00e9composition en valeurs/vecteurs propres de matrices AT_LEAST_ONE_COLUMN = une matrice doit comporter au moins une colonne AT_LEAST_ONE_ROW = une matrice doit comporter au moins une ligne BANDWIDTH = bande passante ({0}) BINOMIAL_INVALID_PARAMETERS_ORDER = n doit \u00eatre sup\u00e9rieur ou \u00e9gal \u00e0 k pour le coefficient du bin\u00f4me (n, k), or k = {0}, n = {1} BINOMIAL_NEGATIVE_PARAMETER = n doit \u00eatre positif pour le coefficient du bin\u00f4me (n, k), or n = {0} CANNOT_CLEAR_STATISTIC_CONSTRUCTED_FROM_EXTERNAL_MOMENTS = les statistiques bas\u00e9es sur des moments externes ne peuvent pas \u00eatre remises \u00e0 z\u00e9ro CANNOT_COMPUTE_0TH_ROOT_OF_UNITY = impossible de calculer la racine z\u00e9roi\u00e8me de l''unit\u00e9, CANNOT_COMPUTE_BETA_DENSITY_AT_0_FOR_SOME_ALPHA = impossible de calculer la densit\u00e9 beta en 0 lorsque alpha = {0,number} CANNOT_COMPUTE_BETA_DENSITY_AT_1_FOR_SOME_BETA = impossible de calculer la densit\u00e9 beta en 1 lorsque beta = %.3g CANNOT_COMPUTE_NTH_ROOT_FOR_NEGATIVE_N = impossible de calculer la racine ni\u00e8me pour n n\u00e9gatif ou nul : {0} CANNOT_DISCARD_NEGATIVE_NUMBER_OF_ELEMENTS = impossible d''enlever un nombre d''\u00e9l\u00e9ments{0} n\u00e9gatif CANNOT_FORMAT_INSTANCE_AS_3D_VECTOR = impossible de formater une instance de {0} comme un vecteur de dimension 3 CANNOT_FORMAT_INSTANCE_AS_COMPLEX = impossible de formater une instance de {0} comme un nombre complexe CANNOT_FORMAT_INSTANCE_AS_REAL_VECTOR = impossible de formater une instance de {0} comme un vecteur r\u00e9el CANNOT_FORMAT_OBJECT_TO_FRACTION = impossible de formater l''objet sous forme d''un nombre rationnel CANNOT_INCREMENT_STATISTIC_CONSTRUCTED_FROM_EXTERNAL_MOMENTS = les statistiques bas\u00e9es sur des moments externes ne peuvent pas \u00eatre incr\u00e9ment\u00e9es CANNOT_NORMALIZE_A_ZERO_NORM_VECTOR = impossible de normer un vecteur de norme nulle CANNOT_RETRIEVE_AT_NEGATIVE_INDEX = impossible d''extraire un \u00e9l\u00e9ment \u00e0 un index n\u00e9gatif ({0}) CANNOT_SET_AT_NEGATIVE_INDEX = impossible de mettre un \u00e9l\u00e9ment \u00e0 un index n\u00e9gatif ({0}) CANNOT_SUBSTITUTE_ELEMENT_FROM_EMPTY_ARRAY = impossible de substituer un \u00e9l\u00e9ment dans un tableau vide CANNOT_TRANSFORM_TO_DOUBLE = Exception de conversion dans une transformation : {0} CARDAN_ANGLES_SINGULARITY = singularit\u00e9 d''angles de Cardan CLASS_DOESNT_IMPLEMENT_COMPARABLE = la classe ({0}) n''implante pas l''interface Comparable CLOSEST_ORTHOGONAL_MATRIX_HAS_NEGATIVE_DETERMINANT = la matrice orthogonale la plus proche a un d\u00e9terminant n\u00e9gatif {0} COLUMN_INDEX_OUT_OF_RANGE = l''index de colonne {0} est hors du domaine autoris\u00e9 [{1}, {2}] COLUMN_INDEX = index de colonne ({0}) CONSTRAINT = contrainte CONTINUED_FRACTION_INFINITY_DIVERGENCE = Divergence de fraction continue \u00e0 l''infini pour la valeur {0} CONTINUED_FRACTION_NAN_DIVERGENCE = Divergence de fraction continue \u00e0 NaN pour la valeur {0} CONTRACTION_CRITERIA_SMALLER_THAN_EXPANSION_FACTOR = crit\u00e8re de contraction ({0}) inf\u00e9rieur au facteur d''extension ({1}). Ceci induit une boucle infinie d''extensions/contractions car tout tableau de stockage fra\u00eechement \u00e9tendu respecte imm\u00e9diatement le crit\u00e8re de contraction. CONTRACTION_CRITERIA_SMALLER_THAN_ONE = crit\u00e8re de contraction inf\u00e9rieur \u00e0 un ({0}). Ceci induit une boucle infinie d''extensions/contractions car tout tableau de stockage de longueur \u00e9gale au nombre d''\u00e9l\u00e9ments respecte le crit\u00e8re de contraction. CONVERGENCE_FAILED = \u00c9chec de convergence CROSSING_BOUNDARY_LOOPS = certains p\u00e9rim\u00e8tres de fronti\u00e8res se croisent CROSSOVER_RATE = proportion de m\u00e9lange ({0}) CUMULATIVE_PROBABILITY_RETURNED_NAN = Fonction de probabilit\u00e9 cumulative retourn\u00e9 NaN \u00e0 l''argument de {0} p = {1} DIFFERENT_ROWS_LENGTHS = certaines lignes ont une longueur de {0} alors que d''autres ont une longueur de {1} DIFFERENT_ORIG_AND_PERMUTED_DATA = les donn\u00e9es originales et permut\u00e9es doivent contenir les m\u00eames \u00e9l\u00e9ments DIGEST_NOT_INITIALIZED = mod\u00e8le empirique non initialis\u00e9 DIMENSIONS_MISMATCH_2x2 = {0}x{1} \u00e0 la place de {2}x{3} DIMENSIONS_MISMATCH_SIMPLE = {0} != {1} DIMENSIONS_MISMATCH = dimensions incoh\u00e9rentes DISCRETE_CUMULATIVE_PROBABILITY_RETURNED_NAN = Discr\u00e8tes fonction de probabilit\u00e9 cumulative retourn\u00e9 NaN \u00e0 l''argument de {0} DISTRIBUTION_NOT_LOADED = aucune distribution n''a \u00e9t\u00e9 charg\u00e9e DUPLICATED_ABSCISSA_DIVISION_BY_ZERO = la duplication de l''abscisse {0} engendre une division par z\u00e9ro ELITISM_RATE = proportion d''\u00e9litisme ({0}) EMPTY_CLUSTER_IN_K_MEANS = groupe vide dans l''algorithme des k-moyennes EMPTY_INTERPOLATION_SAMPLE = \u00e9chantillon d''interpolation vide EMPTY_POLYNOMIALS_COEFFICIENTS_ARRAY = tableau de coefficients polyn\u00f4miaux vide EMPTY_SELECTED_COLUMN_INDEX_ARRAY = tableau des indices de colonnes s\u00e9lectionn\u00e9es vide EMPTY_SELECTED_ROW_INDEX_ARRAY = tableau des indices de lignes s\u00e9lectionn\u00e9es vide EMPTY_STRING_FOR_IMAGINARY_CHARACTER = cha\u00eene vide pour le caract\u00e8 imaginaire ENDPOINTS_NOT_AN_INTERVAL = les bornes ne d\u00e9finissent pas un intervalle : [{0}, {1}] EQUAL_VERTICES_IN_SIMPLEX = sommets {0} et {1} \u00e9gaux dans la configuration du simplex EULER_ANGLES_SINGULARITY = singularit\u00e9 d''angles d''Euler EVALUATION = \u00e9valuation EXPANSION_FACTOR_SMALLER_THAN_ONE = facteur d''extension inf\u00e9rieur \u00e0 un ({0}) FACTORIAL_NEGATIVE_PARAMETER = n doit \u00eatre positif pour le calcul de n!, or n = {0} FAILED_BRACKETING = nombre d''it\u00e9rations = {4}, it\u00e9rations maximum = {5}, valeur initiale = {6}, borne inf\u00e9rieure = {7}, borne sup\u00e9rieure = {8}, valeur a finale = {0}, valeur b finale = {1}, f(a) = {2}, f(b) = {3} FAILED_FRACTION_CONVERSION = Impossible de convertir {0} en fraction apr\u00e8s {1} it\u00e9rations FIRST_COLUMNS_NOT_INITIALIZED_YET = les {0} premi\u00e8res colonnes ne sont pas encore initialis\u00e9es FIRST_ELEMENT_NOT_ZERO = le premier \u00e9l\u00e9ment n''est pas nul : {0} FIRST_ROWS_NOT_INITIALIZED_YET = les {0} premi\u00e8res lignes ne sont pas encore initialis\u00e9es FRACTION_CONVERSION_OVERFLOW = D\u00e9passement de capacit\u00e9 lors de la conversion de {0} en fraction ({1}/{2}) FUNCTION_NOT_DIFFERENTIABLE = la fonction n''est pas diff\u00e9rentiable FUNCTION_NOT_POLYNOMIAL = la fonction n''est pas p\u00f4lynomiale GCD_OVERFLOW_32_BITS = d\u00e9passement de capacit\u00e9 : le PGCD de {0} et {1} vaut 2^31 GCD_OVERFLOW_64_BITS = d\u00e9passement de capacit\u00e9 : le PGCD de {0} et {1} vaut 2^63 HOLE_BETWEEN_MODELS_TIME_RANGES = trou de longueur {0} entre les domaines temporels des mod\u00e8les ILL_CONDITIONED_OPERATOR = le conditionnement {1} est trop \u00e9lev\u00e9 INDEX_LARGER_THAN_MAX = l''index sp\u00e9cifi\u00e9 ({0}) d\u00e9passe l''index maximal courant ({1}) INDEX_NOT_POSITIVE = l''indice ({0}) n''est pas positif INDEX_OUT_OF_RANGE = l''indice ({0}) est hors du domaine autoris\u00e9 [{1}, {2}] INDEX = indice ({0}) NOT_FINITE_NUMBER = {0} n''est pas un nombre fini INFINITE_BOUND = intervalle limites doit \u00eatre finie ARRAY_ELEMENT = valeur {0} à l''indice {1} INFINITE_ARRAY_ELEMENT = le tableau contient l''\u00e9l\u00e9ment infini {0} \u00e0 l''index {1} INFINITE_VALUE_CONVERSION = les valeurs infinies ne peuvent \u00eatre converties INITIAL_CAPACITY_NOT_POSITIVE = la capacit\u00e9 initiale ({0}) n''est pas positive INITIAL_COLUMN_AFTER_FINAL_COLUMN = colonne initiale {1} apr\u00e8s la colonne finale {0} INITIAL_ROW_AFTER_FINAL_ROW = ligne initiale {1} apr\u00e8s la ligne finale {0} INPUT_DATA_FROM_UNSUPPORTED_DATASOURCE = les donn\u00e9es d''entr\u00e9e proviennent d''une source non prise en compte : {0}, sources prises en comptes : {1}, {2} INSTANCES_NOT_COMPARABLE_TO_EXISTING_VALUES = l''instance de la classe {0} n''est pas comparable aux valeurs existantes INSUFFICIENT_DATA_FOR_T_STATISTIC = deux valeurs ou plus sont n\u00e9cessaires pour la statistique t, il y en a {0} INSUFFICIENT_DIMENSION = dimension {0} insuffisante, elle devrait \u00eatre au moins {1} DIMENSION = dimension ({0}) INSUFFICIENT_OBSERVED_POINTS_IN_SAMPLE = l''\u00e9chantillon ne contient que {0} points alors qu''au moins {1} sont n\u00e9cessaires INSUFFICIENT_ROWS_AND_COLUMNS = donn\u00e9es insuffisantes : seulement {0} lignes et {1} colonnes. INTEGRATION_METHOD_NEEDS_AT_LEAST_TWO_PREVIOUS_POINTS = les m\u00e9thodes multi-pas n\u00e9cessitent au moins {0} pas pr\u00e9c\u00e9dents, il y en a {1} INTERNAL_ERROR = erreur interne, veuillez signaler l''erreur \u00e0 {0} INVALID_BINARY_DIGIT = chiffre binaire invalide : {0} INVALID_BINARY_CHROMOSOME = la mutation binaire ne fonctionne qu''avec BinaryChromosome INVALID_BRACKETING_PARAMETERS = param\u00e8tres d''encadrement invalides : borne inf\u00e9rieure = {0}, valeur initiale = {1}, borne sup\u00e9rieure = {2} INVALID_FIXED_LENGTH_CHROMOSOME = le m\u00e9lange \u00e0 un point ne fonctionne qu''avec les chromosomes \u00e0 taille fixe INVALID_INTERVAL_INITIAL_VALUE_PARAMETERS = param\u00e8tres de l''intervalle initial invalides : borne inf = {0}, valeur initiale = {1}, borne sup = {2} INVALID_ITERATIONS_LIMITS = limites d''it\u00e9rations invalides : min = {0}, max = {1} INVALID_MAX_ITERATIONS = valeur invalide pour le nombre maximal d''it\u00e9rations : {0} NOT_ENOUGH_DATA_REGRESSION = le nombre d''observations est insuffisant pour r\u00e9aliser une r\u00e9gression INVALID_REGRESSION_ARRAY= la longueur du tableau de donn\u00e9es = {0} ne correspond pas au nombre d''observations = {1} et le nombre de variables explicatives = {2} INVALID_REGRESSION_OBSERVATION = la longueur du tableau de variables explicatives ({0}) ne correspond pas au nombre de variables dans le mod\u00e8le ({1}) INVALID_ROUNDING_METHOD = m\u00e9thode d''arondi {0} invalide, m\u00e9thodes valides : {1} ({2}), {3} ({4}), {5} ({6}), {7} ({8}), {9} ({10}), {11} ({12}), {13} ({14}), {15} ({16}) ITERATOR_EXHAUSTED = it\u00e9ration achev\u00e9e ITERATIONS = it\u00e9rations LCM_OVERFLOW_32_BITS = d\u00e9passement de capacit\u00e9 : le MCM de {0} et {1} vaut 2^31 LCM_OVERFLOW_64_BITS = d\u00e9passement de capacit\u00e9 : le MCM de {0} et {1} vaut 2^63 LIST_OF_CHROMOSOMES_BIGGER_THAN_POPULATION_SIZE = la liste des chromosomes d\u00e9passe maxPopulationSize LOESS_EXPECTS_AT_LEAST_ONE_POINT = la r\u00e9gression Loess n\u00e9cessite au moins un point LOWER_BOUND_NOT_BELOW_UPPER_BOUND = la borne inf\u00e9rieure ({0}) doit \u00eatre strictement plus petite que la borne sup\u00e9rieure ({1}) LOWER_ENDPOINT_ABOVE_UPPER_ENDPOINT = la borne inf\u00e9rieure ({0}) devrait \u00eatre inf\u00e9rieure ou \u00e9gale \u00e0 la borne sup\u00e9rieure ({1}) MAP_MODIFIED_WHILE_ITERATING = la table d''adressage a \u00e9t\u00e9 modifi\u00e9e pendant l''it\u00e9ration EVALUATIONS = \u00e9valuations MAX_COUNT_EXCEEDED = limite ({0}) d\u00e9pass\u00e9 MAX_ITERATIONS_EXCEEDED = nombre maximal d''it\u00e9rations ({0}) d\u00e9pass\u00e9 MINIMAL_STEPSIZE_REACHED_DURING_INTEGRATION = pas minimal ({1,number,0.00E00}) atteint, l''int\u00e9gration n\u00e9cessite {0,number,0.00E00} MISMATCHED_LOESS_ABSCISSA_ORDINATE_ARRAYS = Loess a besoin de tableaux d'abscisses et d'oordonn\u00e9es de m\u00eame taille, mais il y a {0} points en abscisse et {1} en ordonn\u00e9e MUTATION_RATE = proportion de mutation ({0}) NAN_ELEMENT_AT_INDEX = l''\u00e9l\u00e9ment {0} est un NaN NAN_VALUE_CONVERSION = les valeurs NaN ne peuvent \u00eatre converties NEGATIVE_BRIGHTNESS_EXPONENT = l''exposant de brillance devrait \u00eatre positif ou null, or e = {0} NEGATIVE_COMPLEX_MODULE = module n\u00e9gatif ({0}) pour un nombre complexe NEGATIVE_ELEMENT_AT_2D_INDEX = l''\u00e9l\u00e9ment ({0}, {1}) est n\u00e9gatif : {2} NEGATIVE_ELEMENT_AT_INDEX = l''\u00e9l\u00e9ment {0} est n\u00e9gatif : {1} NEGATIVE_NUMBER_OF_SUCCESSES = le nombre de succ\u00e8s ne doit pas \u00eatre n\u00e9gatif ({0}) NUMBER_OF_SUCCESSES = nombre de succ\u00e8s ({0}) NEGATIVE_NUMBER_OF_TRIALS = le nombre d''essais ne doit pas \u00eatre n\u00e9gatif ({0}) NUMBER_OF_INTERPOLATION_POINTS = nombre de points d''interpolation ({0}) NUMBER_OF_TRIALS = nombre d''essais ({0}) ROBUSTNESS_ITERATIONS = nombre d''it\u00e9rations robuste ({0}) START_POSITION = position de d\u00e9part ({0}) NON_CONVERGENT_CONTINUED_FRACTION = \u00c9chec de convergence (en moins de {0} it\u00e9rations) de fraction continue pour la valeur {1} NON_INVERTIBLE_TRANSFORM = la transformation affine non-inversible r\u00e9duit des lignes \u00e0 de simples points NON_POSITIVE_MICROSPHERE_ELEMENTS = le nombre d''\u00e9l\u00e9ments de la microsph\u00e8re devrait \u00eatre positif, or n = {0} NON_POSITIVE_POLYNOMIAL_DEGREE = le polyn\u00f4me doit \u00eatre de degr\u00e9 positif : degr\u00e9 = {0} NON_REAL_FINITE_ABSCISSA = toutes les abscisses doivent \u00eatre des nombres r\u00e9els finis, mais l''abscisse {0} vaut {1} NON_REAL_FINITE_ORDINATE = toutes les ordonn\u00e9es doivent \u00eatre des nombres r\u00e9els finis, mais l''ordonn\u00e9e {0} vaut {1} NON_REAL_FINITE_WEIGHT = tous les poids doivent \u00eatre des nombres r\u00e9els finis, mais le poids {0} vaut {1} NON_SQUARE_MATRIX = matrice non carr\u00e9e ({0}x{1}) NORM = norme ({0}) NORMALIZE_INFINITE = impossible de normaliser vers une valeur infinie NORMALIZE_NAN = impossible de normaliser vers NaN NOT_ADDITION_COMPATIBLE_MATRICES = les dimensions {0}x{1} et {2}x{3} sont incompatibles pour l''addition matricielle NOT_DECREASING_NUMBER_OF_POINTS = les points {0} et {1} ne sont pas d\u00e9croissants ({2} < {3}) NOT_DECREASING_SEQUENCE = les points {3} et {2} ne sont pas d\u00e9croissants ({1} < {0}) NOT_ENOUGH_DATA_FOR_NUMBER_OF_PREDICTORS = pas assez de donn\u00e9es ({0} lignes) pour {1} pr\u00e9dicteurs NOT_ENOUGH_POINTS_IN_SPLINE_PARTITION = une partiction spline n\u00e9cessite au moins {0} points, seuls {1} ont \u00e9t\u00e9 fournis NOT_INCREASING_NUMBER_OF_POINTS = les points {0} et {1} ne sont pas croissants ({2} > {3}) NOT_INCREASING_SEQUENCE = les points {3} et {2} ne sont pas croissants ({1} > {0}) NOT_MULTIPLICATION_COMPATIBLE_MATRICES = les dimensions {0}x{1} et {2}x{3} sont incompatibles pour la multiplication matricielle NOT_POSITIVE_DEFINITE_MATRIX = matrice non d\u00e9finie positive NON_POSITIVE_DEFINITE_MATRIX = matrice non d\u00e9finie positive: l''\u00e9l\u00e9ment diagonal ({1},{1}) est inf\u00e9rieur \u00e0 {2} ({0}) NON_POSITIVE_DEFINITE_OPERATOR = op\u00e9rateur lin\u00e9aire non d\u00e9fini positif NON_SELF_ADJOINT_OPERATOR = op\u00e9rateur lin\u00e9aire non auto-adjoint NON_SQUARE_OPERATOR = op\u00e9rateur lin\u00e9aire non carr\u00e9 ({0}x{1}) NOT_POSITIVE_DEGREES_OF_FREEDOM = les degr\u00e9s de libert\u00e9 doivent \u00eatre positifs ({0}) DEGREES_OF_FREEDOM = degr\u00e9s de libert\u00e9 ({0}) NOT_POSITIVE_ELEMENT_AT_INDEX = l''\u00e9l\u00e9ment {0} n''est pas positif : {1} NOT_POSITIVE_EXPONENT = exposant {0} invalide (doit \u00eatre positif) NUMBER_OF_ELEMENTS_SHOULD_BE_POSITIVE = le nombre d''\u00e9l\u00e9ments devrait \u00eatre positif ({0}) EXPONENT = exposant ({0}) NOT_POSITIVE_LENGTH = la longueur doit \u00eatre positive ({0}) LENGTH = longueur ({0}) NOT_POSITIVE_MEAN = la moyenne doit \u00eatre positive ({0}) MEAN = moyenne ({0}) NOT_POSITIVE_NUMBER_OF_SAMPLES = le nombre d''\u00e9chantillons n''est pas positif : {0} NUMBER_OF_SAMPLES = nombre d''\u00e9chantillons ({0}) NOT_POSITIVE_PERMUTATION = la permutation k ({0}) doit \u00eatre positive PERMUTATION_SIZE = taille de la permutation ({0}) NOT_POSITIVE_POISSON_MEAN = la moyenne de Poisson doit \u00eatre positive ({0}) NOT_POSITIVE_POPULATION_SIZE = la taille de la population doit \u00eatre positive ({0}) POPULATION_SIZE = taille de la population ({0}) NOT_POSITIVE_ROW_DIMENSION = nombre de lignes invalide : {0} (doit \u00eatre positif) NOT_POSITIVE_SAMPLE_SIZE = la taille de l''\u00e9chantillon doit \u00eatre positive ({0}) NOT_POSITIVE_SCALE = l''\u00e9chelle doit \u00eatre positive ({0}) SCALE = facteur d''\u00e9chelle ({0}) NOT_POSITIVE_SHAPE = le facteur de forme doit \u00eatre positif ({0}) SHAPE = facteur de forme ({0}) NOT_POSITIVE_STANDARD_DEVIATION = l''\u00e9cart type doit \u00eatre positif ({0}) STANDARD_DEVIATION = \u00e9cart type ({0}) NOT_POSITIVE_UPPER_BOUND = la borne sup\u00e9rieure doit \u00eatre positive ({0}) NOT_POSITIVE_WINDOW_SIZE = la taille de la fen\u00eatre doit \u00eatre positive ({0}) NOT_POWER_OF_TWO = {0} n''est pas une puissance de 2 NOT_POWER_OF_TWO_CONSIDER_PADDING = {0} n''est pas une puissance de 2, ajoutez des \u00e9l\u00e9ments pour corriger NOT_POWER_OF_TWO_PLUS_ONE = {0} n''est pas une puissance de 2 plus un NOT_STRICTLY_DECREASING_NUMBER_OF_POINTS = les points {0} et {1} ne sont pas strictement d\u00e9croissants ({2} <= {3}) NOT_STRICTLY_DECREASING_SEQUENCE = les points {3} et {2} ne sont pas strictement d\u00e9croissants ({1} <= {0}) NOT_STRICTLY_INCREASING_KNOT_VALUES = les n\u0153uds d''interpolation doivent \u00eatre strictement croissants NOT_STRICTLY_INCREASING_NUMBER_OF_POINTS = les points {0} et {1} ne sont pas strictement croissants ({2} >= {3}) NOT_STRICTLY_INCREASING_SEQUENCE = les points {3} et {2} ne sont pas strictement croissants ({1} >= {0}) NOT_SUBTRACTION_COMPATIBLE_MATRICES = les dimensions {0}x{1} et {2}x{3} sont incompatibles pour la soustraction matricielle NOT_SUPPORTED_IN_DIMENSION_N = m\u00e9thode non disponible en dimension {0} NOT_SYMMETRIC_MATRIX = matrice non symm\u00e9trique NON_SYMMETRIC_MATRIX = matrice non symm\u00e9trique: la diff\u00e9rence entre les \u00e9l\u00e9ments ({0},{1}) et ({1},{0}) est sup\u00e9rieure \u00e0 {2} NO_BIN_SELECTED = aucun compartiment s\u00e9lectionn\u00e9 NO_CONVERGENCE_WITH_ANY_START_POINT = aucun des {0} points de d\u00e9part n''aboutit \u00e0 une convergence NO_DATA = aucune donn\u00e9e NO_DEGREES_OF_FREEDOM = aucun degr\u00e9 de libert\u00e9 ({0} mesures, {1} param\u00e8tres) NO_DENSITY_FOR_THIS_DISTRIBUTION = La fonction de densit\u00e9 pour cette distribution n''a pas \u00e9t\u00e9 mis en \u0153uvre NO_FEASIBLE_SOLUTION = aucune solution r\u00e9alisable NO_OPTIMUM_COMPUTED_YET = aucun optimum n''a encore \u00e9t\u00e9 calcul\u00e9 NO_REGRESSORS = le mod\u00e8le de r\u00e9gression doit inclure au moins une variable explicative NO_RESULT_AVAILABLE = aucun r\u00e9sultat n''est disponible NO_SUCH_MATRIX_ENTRY = pas d''\u00e9l\u00e9ment ({0}, {1}) dans une matrice {2}x{3} NAN_NOT_ALLOWED = "NaN" n''est pas permis NULL_NOT_ALLOWED = "null" n''est pas permis ARRAY_ZERO_LENGTH_OR_NULL_NOT_ALLOWED = un tableau nul ou de taille z\u00e9ro n''est pas autoris\u00e9 COVARIANCE_MATRIX = matrice de covariance DENOMINATOR = d\u00e9nominateur DENOMINATOR_FORMAT = format du d\u00e9nominateur FRACTION = fraction FUNCTION = fonction IMAGINARY_FORMAT = format de la partie imaginaire INPUT_ARRAY = tableau d''entr\u00e9e NUMERATOR = num\u00e9rateur NUMERATOR_FORMAT = format du num\u00e9rateur OBJECT_TRANSFORMATION = exception de conversion dans une transformation REAL_FORMAT = format de la partie r\u00e9elle WHOLE_FORMAT = format complet NUMBER_TOO_LARGE = {0} est plus grand que le maximum ({1}) NUMBER_TOO_SMALL = {0} est plus petit que le minimum ({1}) NUMBER_TOO_LARGE_BOUND_EXCLUDED = {0} n''est pas strictement plus petit que le maximum ({1}) NUMBER_TOO_SMALL_BOUND_EXCLUDED = {0} n''est pas strictement plus grand que le minimum ({1}) NUMBER_OF_SUCCESS_LARGER_THAN_POPULATION_SIZE = le nombre de succ\u00e8s ({0}) doit \u00eatre inf\u00e9rieur ou \u00e9gal \u00e0 la taille de la population ({1}) NUMERATOR_OVERFLOW_AFTER_MULTIPLY = d\u00e9passement de capacit\u00e9 pour le num\u00e9rateur apr\u00e8s multiplication : {0} N_POINTS_GAUSS_LEGENDRE_INTEGRATOR_NOT_SUPPORTED = l''int\u00e9grateur de Legendre-Gauss en {0} points n''est pas disponible, le nombre de points doit \u00eatre entre {1} et {2} OBSERVED_COUNTS_ALL_ZERO = aucune occurrence dans le tableau des observations {0} OBSERVED_COUNTS_BOTTH_ZERO_FOR_ENTRY = les occurrences observ\u00e9es sont toutes deux nulles pour l''entr\u00e9e {0} BOBYQA_BOUND_DIFFERENCE_CONDITION = la diff\u00e9rence entre la contrainte sup\u00e9rieure et inf\u00e9rieure doit \u00eatre plus grande que deux fois le rayon de la r\u00e9gion de confiance initiale ({0}) OUT_OF_BOUNDS_QUANTILE_VALUE = valeur de quantile {0} hors bornes, doit \u00eatre dans l''intervalle ]0, 100] OUT_OF_BOUND_SIGNIFICANCE_LEVEL = niveau de signification {0} hors domaine, doit \u00eatre entre {1} et {2} SIGNIFICANCE_LEVEL = niveau de signification ({0}) OUT_OF_ORDER_ABSCISSA_ARRAY = les abscisses doivent \u00eatre en ordre strictement croissant, mais l''\u00e9l\u00e9ment {0} vaut {1} alors que l''\u00e9l\u00e9ment {2} vaut {3} OUT_OF_RANGE_ROOT_OF_UNITY_INDEX = l''indice de racine de l''unit\u00e9 {0} est hors du domaine autoris\u00e9 [{1};{2}] OUT_OF_RANGE_SIMPLE = {0} hors du domaine [{1}, {2}] OUT_OF_RANGE_LEFT = {0} hors du domaine ({1}, {2}] OUT_OF_RANGE_RIGHT = {0} hors du domaine [{1}, {2}) OUT_OF_RANGE = hors domaine OUTLINE_BOUNDARY_LOOP_OPEN = un p\u00e9rim\u00e8tre fronti\u00e8re est ouvert OVERFLOW = d\u00e9passement de capacit\u00e9 OVERFLOW_IN_FRACTION = d\u00e9passement de capacit\u00e9 pour la fraction {0}/{1}, son signe ne peut \u00eatre chang\u00e9 OVERFLOW_IN_ADDITION = d\u00e9passement de capacit\u00e9 pour l''addition : {0} + {1} OVERFLOW_IN_SUBTRACTION = d\u00e9passement de capacit\u00e9 pour la soustraction : {0} - {1} PERCENTILE_IMPLEMENTATION_CANNOT_ACCESS_METHOD = acc\u00e8s impossible \u00e0 la m\u00e9thode {0} dans la mise en \u0153uvre du pourcentage {1} PERCENTILE_IMPLEMENTATION_UNSUPPORTED_METHOD = l''implantation de pourcentage {0} ne dispose pas de la m\u00e9thode {1} PERMUTATION_EXCEEDS_N = la taille de la permutation ({0}) d\u00e9passe le domaine de la permutation ({1}) POLYNOMIAL = polyn\u00f4me POLYNOMIAL_INTERPOLANTS_MISMATCH_SEGMENTS = le nombre d''interpolants polyn\u00f4miaux doit correspondre au nombre de segments ({0} != {1} - 1) POPULATION_LIMIT_NOT_POSITIVE = la limite de population doit \u00eatre positive POWER_NEGATIVE_PARAMETERS = impossible d''\u00e9lever une valeur enti\u00e8re \u00e0 une puissance n\u00e9gative ({0}^{1}) PROPAGATION_DIRECTION_MISMATCH = directions de propagation incoh\u00e9rentes RANDOMKEY_MUTATION_WRONG_CLASS = RandomKeyMutation ne fonctionne qu''avec la classe RandomKeys, pas avec {0} ROOTS_OF_UNITY_NOT_COMPUTED_YET = les racines de l''unit\u00e9 n''ont pas encore \u00e9t\u00e9 calcul\u00e9es ROTATION_MATRIX_DIMENSIONS = une matrice {0}x{1} ne peut pas \u00eatre une matrice de rotation ROW_INDEX_OUT_OF_RANGE = l''index de ligne {0} est hors du domaine autoris\u00e9 [{1}, {2}] ROW_INDEX = index de ligne ({0}) SAME_SIGN_AT_ENDPOINTS = les valeurs aux bornes de la fonction devraient avoir des signes difff\u00e9rents ; bornes : [{0}, {1}], valeurs : [{2}, {3}] SAMPLE_SIZE_EXCEEDS_COLLECTION_SIZE = la taille de l''\u00e9chantillon ({0}) d\u00e9passe la taille de la collection ({1}) SAMPLE_SIZE_LARGER_THAN_POPULATION_SIZE = la taille de l''\u00e9chantillon ({0}) doit \u00eatre inf\u00e9rieure ou \u00e9gale \u00e0 la taille de la population ({1}) SIMPLEX_NEED_ONE_POINT = le simplex doit contenir au moins un point SIMPLE_MESSAGE = {0} SINGULAR_MATRIX = matrice singuli\u00e8re SINGULAR_OPERATOR = l''op\u00e9rateur est singulier SUBARRAY_ENDS_AFTER_ARRAY_END = le sous-tableau se termine apr\u00e8s la fin du tableau TOO_LARGE_CUTOFF_SINGULAR_VALUE = la valeur singuli\u00e8re de coupure vaut {0}, elle ne devrait pas d\u00e9passer {1} TOO_LARGE_TOURNAMENT_ARITY = l''arit\u00e9 du tournois ({0}) ne doit pas d\u00e9passer la taille de la population ({1}) TOO_MANY_ELEMENTS_TO_DISCARD_FROM_ARRAY = impossible d''enlever {0} \u00e9l\u00e9ments d''un tableau en contenant {1} TOO_MANY_REGRESSORS = trop de variables explicatives sp\u00e9cifi\u00e9es {0}, il n''y en a que {1} dans le mod\u00e8le TOO_SMALL_COST_RELATIVE_TOLERANCE = trop petite tol\u00e9rance relative sur le co\u00fbt ({0}), aucune r\u00e9duction de la somme des carr\u00e9s n''est possible TOO_SMALL_INTEGRATION_INTERVAL = intervalle d''int\u00e9gration trop petit : {0} TOO_SMALL_ORTHOGONALITY_TOLERANCE = trop petite tol\u00e9rance sur l''orthogonalit\u00e9 ({0}), la solution est orthogonale \u00e0 la jacobienne TOO_SMALL_PARAMETERS_RELATIVE_TOLERANCE = trop petite tol\u00e9rance relative sur les param\u00e8tres ({0}), aucune am\u00e9lioration de la solution approximative n''est possible TRUST_REGION_STEP_FAILED = l''\u00e9tape de la r\u00e9gion de confiance n''a pas r\u00e9duit Q"), TWO_OR_MORE_CATEGORIES_REQUIRED = deux cat\u00e9gories ou plus sont n\u00e9cessaires, il y en a {0} TWO_OR_MORE_VALUES_IN_CATEGORY_REQUIRED = deux valeurs ou plus sont n\u00e9cessaires pour chaque cat\u00e9gorie, une cat\u00e9gorie en a {0} UNABLE_TO_BRACKET_OPTIMUM_IN_LINE_SEARCH = impossible d''encadrer l''optimum lors de la recherche lin\u00e9aire UNABLE_TO_COMPUTE_COVARIANCE_SINGULAR_PROBLEM = impossible de calculer les covariances : probl\u00e8me singulier UNABLE_TO_FIRST_GUESS_HARMONIC_COEFFICIENTS = impossible de faire une premi\u00e8re estimation des coefficients harmoniques UNABLE_TO_ORTHOGONOLIZE_MATRIX = impossible de rendre la matrice orthogonale en {0} it\u00e9rations UNABLE_TO_PERFORM_QR_DECOMPOSITION_ON_JACOBIAN = impossible de calculer la factorisation Q.R de la matrice jacobienne {0}x{1} UNABLE_TO_SOLVE_SINGULAR_PROBLEM = r\u00e9solution impossible : probl\u00e8me singulier UNBOUNDED_SOLUTION = solution non born\u00e9e UNKNOWN_MODE = mode {0} inconnu, modes connus : {1} ({2}), {3} ({4}), {5} ({6}), {7} ({8}), {9} ({10}) et {11} ({12}) UNKNOWN_PARAMETER = param\u00e8tre {0} inconnu UNMATCHED_ODE_IN_EXPANDED_SET = l''\u00e9quation diff\u00e9rentielle ne correspond pas \u00e0 l''\u00e9quation principale du jeu \u00e9tendu CANNOT_PARSE_AS_TYPE = cha\u00eene "{0}" non analysable (\u00e0 partir de la position {1}) en un objet de type {2} CANNOT_PARSE = cha\u00eene "{0}" non analysable (\u00e0 partir de la position {1}) UNPARSEABLE_3D_VECTOR = vecteur 3D non analysable : "{0}" UNPARSEABLE_COMPLEX_NUMBER = nombre complexe non analysable : "{0}" UNPARSEABLE_REAL_VECTOR = vecteur r\u00e9el non analysable : "{0}" UNSUPPORTED_EXPANSION_MODE = mode d''extension {0} non support\u00e9, les modes support\u00e9s sont {1} ({2}) et {3} ({4}) UNSUPPORTED_OPERATION = op\u00e9ration non disponible ARITHMETIC_EXCEPTION = erreur arithm\u00e9tique ILLEGAL_STATE = \u00e9tat incoh\u00e9rent USER_EXCEPTION = erreur g\u00e9n\u00e9r\u00e9e par le code utilisateur URL_CONTAINS_NO_DATA = l''adresse {0} ne contient aucune donn\u00e9e VALUES_ADDED_BEFORE_CONFIGURING_STATISTIC = {0} valeurs ont \u00e9t\u00e9 ajout\u00e9es VECTOR_LENGTH_MISMATCH = taille de vecteur invalide : {0} au lieu de {1} attendue VECTOR_MUST_HAVE_AT_LEAST_ONE_ELEMENT = un vecteur doit comporter au moins un \u00e9l\u00e9ment WEIGHT_AT_LEAST_ONE_NON_ZERO = le tableau des poids doit contenir au moins une valeur non nulle WRONG_BLOCK_LENGTH = forme de tableau erron\u00e9e (bloc de longueur {0} au lieu des {1} attendus) WRONG_NUMBER_OF_POINTS = {0} sont n\u00e9cessaires, seuls {1} ont \u00e9t\u00e9 fournis NUMBER_OF_POINTS = nombre de points ({0}) ZERO_DENOMINATOR = le d\u00e9nominateur doit \u00eatre diff\u00e9rent de 0 ZERO_DENOMINATOR_IN_FRACTION = d\u00e9nominateur nul dans le nombre rationnel {0}/{1} ZERO_FRACTION_TO_DIVIDE_BY = division par un nombre rationnel nul : {0}/{1} ZERO_NORM = norme nulle ZERO_NORM_FOR_ROTATION_AXIS = norme nulle pour un axe de rotation ZERO_NORM_FOR_ROTATION_DEFINING_VECTOR = norme nulle pour un axe de d\u00e9finition de rotation ZERO_NOT_ALLOWED = la valeur z\u00e9ro n''est pas autoris\u00e9e ici commons-math3-3.2-src/src/changes/release-notes.vm100644 1750 1750 7642 12126627721 20711 0ustarlucluc 0 0 ## Licensed to the Apache Software Foundation (ASF) under one ## or more contributor license agreements. See the NOTICE file ## distributed with this work for additional information ## regarding copyright ownership. The ASF licenses this file ## to you 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. Apache ${project.name} ${version} RELEASE NOTES The ${developmentTeam} is pleased to announce the release of ${finalName} $introduction.replaceAll("(? Commons Math Release Notes Fixed wrong array dimensions in secondary equations handling in some cases. Fixed missing side effects of secondary equations on main state in Ordinary Differential Equations integration. Fixed inverse cumulative probability for uniform distribution. All contents of package "o.a.c.m.stat.clustering" refactored into new package "o.a.c.m.ml.clustering" and added support for additional distance measures in package "o.a.c.m.ml.distance": "CanberraDistance", "ChebyshevDistance", "EuclideanDistance" and "ManhattanDistance". "SpearmansCorrelation" now works correctly in case of a provided "NaturalRanking" with a "NaNStrategy.REMOVED" strategy and the input data contains NaN values. From version 4.0 onwards this strategy will not be supported anymore. Replaced hard-coded numbers in "LevenbergMarquardtOptimizer". Fixed loading of test file when path contains a space. Improved speed of FastMath.abs methods for all signatures, by removing branching. Improved speed of several FastMath methods. Fixed Complex.reciprocal() for zero argument. Improved speed of FastMath copysign methods. Added Multivariate Normal Mixture Model Fitting by Expectation Maximization. AbstractRealMatrix will now check for rectangular input arrays in its copySubMatrix methods. Increment iteration counter in optimization algorithms. Added a way to trigger only increasing or decreasing events in ODE integration. Fixed missing update in ODE event handlers, when a RESET_STATE is triggered. Made EmpiricalDisribution smoothing kernel pluggable. Added array-scaling methods to MathArrays. Allow direct use of SummaryStatistics in one-way ANOVA. Fixed infinite loop when NaN occurs in singular value decomposition. Added the Lévy distribution. Normal distribution now uses a direct implementation of the inverse error function to compute inverse cumulative probability instead of relying on a numerical solver. This is much faster, more accurate and does not need convergence threshold. Implementations for inverse error function and inverse complementary error functions have been added. Extended ranges for FastMath performance tests. Finalized implementation of diagonal matrix. Added rank revealing QR decomposition. ArrayFieldVector can now be constructed from any FieldVector. Improved checking of null vector elements. Added utilities for prime numbers. Fixed generation of long random numbers between two bounds. Fixed creation of generic array. Check bounds in multi-start vector optimizers. Added discrete distributions. Fixed abstract test class naming that broke ant builds. Allow covariance to be computed for one-dimensional variables. Fixed accuracy of 3D Line.revert(). Improved javadoc to explain how switching functions should behave across events in ODE events detection. Added Hermite interpolator for RealFieldElement instances. Added RealFieldElement interface to represent anything that is real number like, implemented by both Decimal64, Dfp and DerivativeStructure. Added partial derivatives computation for 3D vectors and rotations. Fixed DerivativeStructure.atan2 for special cases when both arguments are +/-0. Added accurate linear combination of DerivativeStructure instances, avoiding cancellation. Throw "MathUnsupportedOperationException" from optimizers that do not support constraints (previous behaviour was to silently ignore the "SimpleBounds" argument). Added conversion of gradients and Hessians from spherical to Cartesian coordinates in 3D. Greater efficiency in "UnitSphereRandomVectorGenerator". Improved class javadoc wrt convergence criteria and added additional constructors to override the default epsilon and cut-off values in class "SimplexSolver". Fixed truncated value in "MultivariateNormalDistribution". Made "BitStreamGenerator" implement the "Serializable" interface. Added a (minimal) "DiagonalMatrix" implementation, such that the case of a large number of uncorrelated observations is handled efficiently. All contents of package "o.a.c.m.optimization" refactored into new packages "o.a.c.m.optimization" and "o.a.c.m.fitting". Added methods to EmpiricalDistribution to implement the RealDistribution interface. DBSCAN clustering algorithm (in package "o.a.c.m.stat.clustering"). Added element-by-element addition, subtraction, multiplication and division (in class "o.a.c.m.util.MathArrays"). Fixed "pow" method in class "FastMath". Created a "maximum number of iterations" stopping criterion in the convergence checkers (package "o.a.c.m.optimization") that allows the optimizers to return the "current best point" even if the convergence criteria are not met. Fixed overflow in "sinh" and "cosh" methods in class "FastMath". Accuracy improvements of Gamma.logGamma, and implementation of Gamma.gamma. Both new implementations are based on the NSWC Library of Mathematical Functions. New constructor in the custom checker classes (package "o.a.c.m.optimization") for passing the number of iterations after which the "convergence test" will return true. This allows an algorithm to return the best solution found (after the user-defined number of iterations) even if it does not meet the other convergence criteria. Use "NaNStrategy#FAILED" as default strategy in "NaturalRanking". Added a new "SynchronizedRandomGenerator" that wraps another "RandomGenerator" with all methods being synchronized, thus rendering the code thread-safe (at some efficiency cost). Add new "NaNStrategy": FAILED, used in "RankingAlgorithm" implementations. Any encountered input value that succeeds a "Double#isNaN" check, results in a "NotANumberException". Add new constructor to "SpearmansCorrelation" class which allows to specify the "RankingAlgorithm" to be used. Fixed naming inconsistencies between Interval and IntervalsSet classes. Added a method to check points in the Interval class, with a tolerance for boundary. Added G-test statistics. New "getSquareRoot" method in class "EigenDecomposition" (package "o.a.c.m.linear"). Added "isSymmetric" and "checkSymmetric" in "MatrixUtils" (package "o.a.c.m.linear"). Moved private array argument validation methods from ChiSquareTest to MathArrays. New class for generic multivariate mixture model distributions. Use getter/setter methods of super class for access to field "windowSize" in "ListUnivariateImpl". Improved construction of polygons with an additional constructor, more robust numerically. Added new methods "merge(Frequency)", "merge(Collection<Frequency>)", "incrementValue(Comparable<?>, long)" and "entrySetIterator()" to the "Frequency" class. Allow unlimited input values for "Dfp#multiply(int)". Added distance to point to 2D Line and Segment. "PowellOptimizer" (package "o.a.c.m.optimization.direct") uses "BrentOptimizer" as its internal line search optimizer. The fix forces the convergence criterion of "BrentOptimizer" to use function values (instead of domain values). Numerical accuracy problems arose in "CMAESOptimizer" (in package "o.a.c.m.optimization.direct") when large finite boundaries were specified, because the interval of allowed values was mapped to [0, 1]. This mapping was not necessary and its removal allows finite and infinite boundaries to be used together. Fixed some issues in nth root derivatives at 0. Fixed transformation to a Schur matrix for certain input matrices. Added matrix "block inversion" (in "o.a.c.m.linear.MatrixUtils"). "CMAESOptimizer": Solution was not constrained to lie within the provided boundaries. New "Quaternion" class (package "o.a.c.m.complex"). Added method to test for floating-point numbers equality with a relative tolerance (class "o.a.c.m.util.Precision"). Deprecated "FieldVector#getData()" in favor of "toArray()". Fixed an error in rectangular Cholesky decomposition. Clarified definition of isSupportXxxBoundInclusive in RealDistribution interface, made code consistent with the definition, and deprecated these methods, marking for removal in 4.0. Performance improvement in computation of the greatest common divisor (in class "o.a.c.m.util.ArithmeticUtils"). Added a check so that the returned point will always be the best one. Added a NewtonRaphsonSolver that uses the new differentiation package to define the function to solve. This class is intended to replace the former NewtonSolver which is deprecated. Added RandomDataGenerator to replace RandomDataImpl and deprecated RandomData interface and RandomDataImpl class. Deprecated nextInversionDeviate methods from RandomDataImpl class. Ensured that all nextXxx methods in RandomDataImpl/RandomDataGenerator use the configured RandomGenerator. Generate an exception for rare ill-conditioned cases in "HarmonicFitter" guessing procedure (package "o.a.c.m.optimization.fitting"). New interface for multivariate distributions. Added multivariate normal distribution. Added a utility method to compute Stirling numbers of the second kind. Added a new package dealing with differentials, for one or more free parameters and derivation order 1 or higher. Added additional crossover policies: "CycleCrossover", "NPointCrossover", "OrderedCrossover" and "UniformCrossover". Improved numerical stability of "SimplexSolver" by introducing Bland's rule to prevent cycling and a cutoff threshold to zero out very small values. Fixed overflow detection for negative values in constructor of class "Fraction". Fixed overflow in method "percentageValue" in class "Fraction". Raised (to 10) the default number of fractional digits to print out. Removed duplicate code. Added a new "RealMatrixFormat" class for input/output of RealMatrix instances. Default formatter instances for commons-math and octave have been added to "MatrixUtils". New "IterativeLegendreGaussIntegrator" that performs the same automatic subdivision of the integration interval as "LegendreGaussIntegrator", but uses the classes from package "o.a.c.m.analysis.integration.gauss" to perform the Gauss integration on the sub-interval. Deprecated "LegendreGaussIntegrator". Improve performance of quantile evaluation in "Percentile" class for cases with lots of equal values. New framework for Gauss integration schemes (in package "o.a.c.m.analysis.integration.gauss"). Gauss-Legendre quadrature rules (of unlimited order) implemented in double precision, and high precision (using "BigDecimal"). For all distribution classes (in package "o.a.c.m.distribution"), a new constructor takes a "RandomGenerator" parameter. The "RandomDataImpl" instance has been superseded by this RNG. All "sample()" methods have been modified to use this RNG instead of delegating to the methods in "RandomData". Added support for real asymmetric matrices to "EigenDecomposition". Re-instated methods to find all complex roots of a polynomial (class "LaguerreSolver" in package "o.a.c.m.analysis.solvers"). Added accessors to the "Pair" class (package "o.a.c.m.util"). Added new constructors in "EigenDecomposition" and deprecated two constructors with unused parameters. Added overridden method in "PolynomialFitter" (package "o.a.c.m.optimization.fitting") to limit the number of evaluations. Added a new constructor to o.a.c.m.utils.IterationManager, allowing for the specification of a callback function in case the maximum number of iteration is reached. A new HermiteInterpolator class allows interpolation of vector-valued functions using both values and derivatives of the function at sample points. Parameterized "CurveFitter" class (package "o.a.c.m.optimization.fitting") with the type of the fitting function. Updated subclasses "PolynomialFitter", "HarmonicFitter", "GaussianFitter". Fixed a problem when building rotations from two pairs of vectors. In very rare cases, due to numerical inaccuracies the computed quaternion was not normalized (some examples went as high as 1.0e8) and even after normalization, the quaternion was plain wrong. Removed unused fields LocalizedFormats.ALPHA and LocalizedFormats.BETA. This is an acceptable compatibility break, as these fields are only meant for internal use. Fix computation of upperCumulativeProbability in "HypergeometricDistribution" and cleanup of duplicate probability mass function. In GammaDistribution, deprecated getAlpha() and getBeta(). Replaced with getShape() and getScale(), respectively. Use inline computation for OrderedTuple hash code. Use modified Lentz-Thompson algorithm for continued fraction evaluation to avoid underflows. Fixed a wrong assumption on BSP tree attributes when boundary collapses to a too small polygon at a non-leaf node. Put serialization back for PointValuePair and PointVectorValuePair. Avoid superfluous null check when using iterators in RealVector and ArrayRealVector. Use epsilon instead of ulp in floating-point comparison when dropping columns after phase 1 in SimplexSolver. Added a workaround for an OpenJDK issue on sparc solaris with too small constants. Fixed ListPopulation#iterator to return an unmodifiable iterator. Cleanup of ListPopulation to consistently enforce the population limit. Use same range check in constructor for ElitisticListPopulation as in corresponding setter. Fixed unbalanced use of code tags in javadoc of several classes. Added class FixedElapsedTime (new StoppingCondition for evolution of generations) to genetics package. Added classes Decimal64 and Decimal64Field, which are wrapper classes around primitive doubles. These classes implement FieldElement and Field, respectively. Added statistical hypothesis tests "MannWhitneyUTest" and "WilcoxonSignedRankTest". Classes "CMAESOptimizer" and "BOBYQAOptimizer" inherit from "BaseAbstractMultivariateSimpleBoundsOptimizer" (in package "o.a.c.m.optimization.direct"). Added pre-computed arrays to speed up initial loading time for FastMath. Resources for error messages translations have been moved out of META-INF folder in the jar, to avoid interferences with some build systems. Fixed "doubleValue" and "floatValue" method in "BigFraction" when numerator and denominator are larger than the range of the corresponding primitive type. Removed "MathException" (from package "o.a.c.math"). Removed "MathRuntimeException" (from package "o.a.c.math"). Merged interface and implementation of statistical tests in o.a.c.m.stat.inference package. Merged interface and implementation of EmpiricalDistribution. Relaxed test for equality in UnivariateStatisticAbstractTest. Modified the genetics package to use localized exception messages. Fixed a faulty test for zero in TridiagonalTransformer. Changed algorithm in computation of the covariance matrix in "AbstractLeastSquares" (package "optimization.general"), from "LUDecomposition" to "QRDecomposition". Fixed rounding error in RandomDataImpl nextInt, nextLong methods causing lower endpoints to be excluded when negative. Also improved robustness of nextUniform for extreme values and changed its contract to throw IAE when provided bounds are infinite or NaN. Fixed "offset by one" bug in "BOBYQAOptimizer". Check first step size in embedded Runge-Kutta integrators, and truncate it if needed. Modified setSeed methods in Bitstream generators (MersenneTwister and Well generators) to clear the cache of normal deviates used by nextGaussian. Fixed bracketing interval balancing in BracketingNthOrderBrentSolver. Removed unused or duplicate utility methods from "MathUtils". Math functions with "double" arguments were moved to class "FastMath". Broke up bloated "MathUtils" class into "MathArrays", "Precision", "ArithmeticUtils" classes. Fixed array indexing error in Variance evaluate method for computing the weighted variance of an array segment. Fixed case of unconstrained variables that still occur in the objective function in simplex solver. The fast cryptographically secure pseudorandom number generator ISAAC has been added. The reset method in StepHandler interface has been renamed init and is provided more information on the integration by the calling integrator. A similar init method has been added to the EventHandler interface. Improved accuracy of Runge-Kutta based step interpolators near step start. Fixed errors in SummaryStatistics addValue causing variance, mean, or geometric mean statistics not to be updated if they have been overridden using instances of commons-math supplied implementations. Removed First, Third, Fourth moments from the public API. These internally used statistics have non-standard definitions. The classes remain, but now have package scope. Added support for population variance in StatUtils, SummaryStatistics and DescriptiveStatistics and clarified javadoc to make it clear that 'variance' means sample variance. Fixed BigFraction percentage method which did not work at all. Added interface and abstract class for supporting optimizers classes that can take simple constraints into account. Fixed a bad interaction between step handlers and event handlers in ODE integrators. Added array constructor and getter for Vector2D and Vector3D. Added applyTo and applyInverseTo methods in the Rotation class that handle directly arrays instead of Vector3D instances. Added adapters for simple bound constraints optimization that can be used for all direct optimization methods, including the ones that do not support constraints by themselves. CMA-ES optimizer input sigma is now consistent with boundaries range units. Added stable random generator based on Chambers-Mallows-Stuck method. Changed the default seed used for RandomDataImpl, AbstractWell and MersenneTwister PRNGs to add the system identity hash code of the instance to the current system time, so generators initialized with default seeds within system clock resolution will generate different sequences. Changed the default non-secure generator used by RandomDataImpl to Well19937c. Fixed an event resetting issue in ODE. Default implementation for "addToEntry" and "multiplyEntry" in "AbstractRealMatrix". Method "addToEntry" in "RealVector". Replaced temporary matrices and entry mutators with double[][] arrays to speed computation in loops within QRDecomposition, Bi- and TriDiagonalTransformer implementations. Fixed an integer overflow in OpenMapRealMatrix. "FastMath": Use constant fields instead of recomputing them at method call. Added Jacobi polynomials. Added "shift" method to compute the coefficients of a new polynomial whose values are the same as those of another polynomial but computed at a shifted point. Faster "multiply" method in "Array2DRowRealMatrix". Code inspired from the Jama project. Replaced package.html with package-info.java for package documentation. Added a getRuntimeClass method to the Field interface allowing to use a complete hierarchy of fields sharing a common interface. The last remaining uses of OptimizationException have been replaced by unchecked exceptions (UnboundSolutionException, NoFeasibleSolutionException, MaxCountExceededException ...) The checked ConvergenceException has been replaced by an unchecked ConvergenceException. ODE step interpolation with Jacobians is now fully merged with classical step interpolation. Completely revamped the computation of Jacobians in ODE. This computation is now included in the mainstream class hierarchy, not in a separate package anymore, and it allows adding other types of equations to a main ODE, not only variational equations for Jacobians computation. SimpleRegression implements UpdatingMultipleLinearRegression interface. Added isMonotone methods in MathUtils. Optimized checkOrder method. In class "AbstractLeastSquaresOptimizer": Allow to specify a singularity threshold in call to "getCovariances" method. Added the ability to suppress the estimation of the intercept in SimpleRegression. Dead code in FastMath.pow(double, double) and some improvement in test coverage. Removed "getData()" method from "RealVector" as it was redundant with "toArray()". Removed completely MathUserException. Use the refactored exceptions framework for ODE. Added a getter to allow retrieving the exception related to an exception context provider. Added erf(double,double) to Erf and used this to improve tail probability accuracy in NormalDistributionImpl. Enabled reseeding of the random generators used by EmpiricalDistributionImpl and ValueServer. Modified ValueServer to pass its RandomData instance to the EmpiricalDistributionImpl that it creates when used in DIGEST_MODE, so reseeding ValueServer works as expected. Renamed "AbstractRealVector" to "RealVector". The interface was removed in favour of its unique (abstract) implementation. Also removed several methods with double[] arguments from AbstractRealVector. Unmodifiable view of a "RealVector". Refactored integration API for consistency with solvers API. Now the main convergence parameters are set in the constructor and remain fixed. Added a maximal number of function evaluations to the integration method, similar to what is done in the solvers API. Added storeless covariance implementation. Eliminated extraneous constructor from SimpleRegression. Eliminated pluggability of ChiSquaredDistribution used by ChiSquaredDistributionTest. Made RandomGenerator configurable and reseedable in EmpiricalDistributionImpl. Added a 3D SubLine class. Fixed exceptions generated by "ebeMultiply" and "ebeDivide" in "OpenMapRealVector". In "SingularValueDecompositionImpl", added accessor for the inverse of the condition number. Made pseudo-inverse consistent with rank computation in SingularValueDecompositionImpl. Added methods to solve upper and lower triangular systems to MatrixUtils. Improved performance of nextInt(int) in BitsStreamGenerator. Fixed a wrong detection of rotation axis versus vectors plane in Rotation constructor using two vectors pairs. Added a few linearCombination utility methods in MathUtils to compute accurately linear combinations a1.b1 + a2.b2 + ... + an.bn taking great care to compensate for cancellation effects. This both improves and simplify several methods in euclidean geometry classes, including linear constructors, dot product and cross product. Fixed bugs in AbstractRandomGenerator nextInt() and nextLong() default implementations. Prior to the fix for this issue, these methods generated only positive values. Simple benchmark utility (new class "PerfTestUtils" added to test sources in src/test). Added a solver for Dfp-based (i.e. high accuracy) functions. Added a Brent-like solver that has higher (user specified) order and does bracket selection on the result: BracketingNthOrderBrentSolver. Added a few shortcut methods and predicates to Dfp (abs, isZero, negativeOrNull, strictlyNegative, positiveOrNull, strictlyPositive). "AbstractRealMatrix" inherits from "RealLinearOperator". Linear combination of vectors: "RealVector" interface updated, implemented in "AbstractRealVector" and "ArrayRealVector". Slightly more efficient implementation of basic operations in "ArrayRealVector". Overloaded methods (add, subtract, multiply, divide) to take a "double" parameter (as a complex number with zero imaginary part). Code and Javadoc cleanup. Added "valueOf" methods. Implemented faster generation of random gamma distributed values with algorithm from Ahrens and Dieter (1972) for shape < 1 and Marsaglia and Tsang (2001) otherwise. Allow discrete events to be detected by ODE solvers also at step start. Rewritten SVD decomposition based on JAMA code. Complex add javadoc says that if either addend has NaN parts, the result should be Complex.NaN. Prior to the fix for this issue, NaNs were propagated only in real and imaginary parts individually. Framework for iterative linear solvers. Improved efficiency in RandomDataImpl, LaguerreSolver, FastMath and OutlineExtractor by moving conditional code into blocks where it is needed. Prevent step normalizer to output twice the last point in MULTIPLES mode. Removed the requiresDenseOutput method from the StepHandler interface. Now integrators always consider dense output is required and set up the appropriate state interpolators, so step handlers can rely on them. Modified "SecantSolver" to comply with the original algorithm. Added several secant-based solvers. Added a way to select the side of the root with bracketing solvers. Fixed javadoc for ODEIntegrator interface Extended StepNormalizer with normalization mode and bounds settings. Implemented faster generation of random exponential distributed values with algorithm from Ahrens and Dieter (1972): Computer methods for sampling from the exponential and normal distributions. K-means++ clustering can now run multiple trials Added a way to compute sub-lines intersections, considering sub-lines either as open sets or closed sets Added a way to build a sub-line from its endpoints, and to retrieve the endpoints from a sub-line Javadoc fixes in ODE. Added a way to specify a custom root solver to find events in ODE integration. Fixed error in javadoc describing Integer distribution inverse cumulative probability API. Fixed error in javadoc describing the behavior of the Percentile algorithm for small percentiles in small datasets. New "filter" package. Initial implementation of Kalman filter. Improved k-means++ clustering performances and initial cluster center choice. Fixed tricube function implementation in Loess interpolator. Fixed documentation of statistics examples. Improved documentation of general optimization with a thorough example. Replaced NullPointerException by NullArgumentException. Added a consistent classes hierarchy for Euclidean spaces in dimension 1, 2 and 3. Improved javadoc for FastMath explaining the overhead at class loading and the targeted use cases. Allow outer product of vectors of different sizes. A complete generic implementation of Binary Space Partitioning Trees (BSP trees) has been added. A few specializations of this implementation are also provided for 1D, 2D and 3D Euclidean geometry. This allows support for arbitrary intervals sets (1D), polygons sets (2D) and polyhedrons sets (3D) with all sets operations (union, intersection, symmetric difference, difference, complement), with predicates (point inside/outside/on boundary, emptiness, other region contained), with geometrical computation (barycenter, size, boundary size) and with conversions from and to boundary representation. Avoid some array copying in add and subtract ArrayFieldVector. Fixed an unused constructor parameter in ArrayFieldVector. Created an "ExceptionContext" class: It provides the customization feature of "MathRuntimeException" without imposing a singly rooted hierarchy of the Comons Math exceptions. Thus, those exceptions now inherit from their Java standard counterparts (e.g. "MathIllegalArgumentException" inherits from "IllegalArgumentException"). Fixed conversion problems to/from 0 in Decimal Floating Point (Dfp) class. Fixed initialization of multistep ODE integrators. Relying on the interpolation model of the starter integrator inside only one step was wrong. The model may have a too low order to compute high degrees derivatives in the Nordsieck vector. Now we use several steps and use only grid points instead of interpolated points. Added solve methods using double[][] to linear algebra decomposition solvers. Added an interpolator adapter for data with known period. Added a "rectangular" Cholesky decomposition for positive semidefinite matrices. Added setters allowing to change the step size control parameters of adaptive step size ODE integrators. Added a compareTo method to MathUtils that uses a number of ulps as a tolerance error, and works well on all numbers, including normals, subnormals, signed zeroes, infinities and NaNs. Fixed two errors in simplex solver when entries are close together or when variables are not restricted to non-negative. Improved robustness of k-means++ algorithm, by tracking changes in points assignments to clusters. Changed MathUtils.round(double,int,int) to propagate rather than wrap runtime exceptions. Instead of MathRuntimeException, this method now throws IllegalArgumentException or ArithmeticException under the conditions specified in the javadoc. Reduced cancellation errors in Vector3D.crossProduct Fixed bug in "MultidimensionalCounter". All unit tests have been converted to Junit 4. They need at least Junit 4.5 to run (the ant and maven build systems are currently set to use Junit 4.8.2) Removed the ConvergingAlgorithm interface and ConvergingAlgorithmImpl class Added a consistency check for number of points with respect to the number of clusters in Kmeans++ clustering Added two sided Kolmogorov-Smirnov distribution using modified Marsaglia et al. (2003) implementation and quick decisions for certain parameter areas according to Simard et al. (2011). Added "power" method in "RealMatrix" and "FieldMatrix<T>" interfaces and their default implementations in "AbstractRealMatrix" and "AbstractFieldMatrix". Added "cosine" method in "RealVector" interface and default implementation in "AbstractRealVector". Fixed bug in "KMeansPlusPlusClusterer". All exceptions defined in Commons Math provide a context and a compound message list. Refactored "PolynomialFitter" (in package "optimization.fitting"). Added parametric function in "PolynomialFunction" (in package "analysis.polynomials"). Removed "HarmonicFunction" (in package "optimization.fitting"); superseded by class "HarmonicOscillator" (in package "analysis.function"). Refactored "HarmonicFitter" (in package "optimization.fitting"). "HarmonicCoefficientsGuesser" removed. Removed "GaussianFunction" and "GaussianDerivativeFunction" (in package "optimization.fitting"); functionality moved to class "Gaussian" (in package "analysis.function"). Removed "ParametricGaussianFunction" (in package "optimization.fitting"); functionality moved to class "Gaussian" (in package "analysis.function"). Refactored "GaussianFitter" (in package "optimization.fitting"). The class now really fits a Gaussian function (whereas previously it was fitting the sum of a constant and a Gaussian). Implementation of the CMA-ES optimization algorithm. The interface "ParametricRealFunction" (in package "optimization.fitting") has been renamed to "ParametricUnivariateRealFunction" and moved to package "analysis". TestUtils is thread-hostile. Remove getters and setters, and make static variables final. Create FastMath copySign methods FastMath atan2 does not agree with StrictMath for special cases Removed checked "MaxIterationsExceededException" (superseded by "MaxCountExceededException" from package "exception"). Removed checked "DimensionMismatchException". Replaced all occurrences by its equivalent from package "exception". FastMath acos fails when input abs value is less than about 5.7851920321187236E-300 - returns NaN FastMath toRadian and toDegree don't handle large double numbers well FastMath does not handle all special cases correctly Fix ulp(Infinity) to return Infinity rather than NaN FastMath.signum(-0.0) does not agree with Math.signum(-0.0) FastMath is not an exact replacement for StrictMath (partially fixed) Add signum(float), ulp(float) FastMath.max(50.0f, -50.0f) => -50.0f; should be +50.0f Fixed FastMath.max(float, float) so it returns correct value. MATH-491 MathUtils.equals(double, double) does not work properly for floats - add equivalent (float, float) methods and basic tests Removed "MathException" from the "throws" clause of the "interpolate" method of the interpolators interfaces (package "analysis.interpolation"). In order to comply with the new runtime exceptions policy, the classes "RealVectorFormat", "ComplexFormat", "Vector3DFormat" and "CompositeFormat" do not inherit anymore from the Java standard "Format" class. Failed parsing will result in the throwing of a "MathParseException". "CompositeFormat" is now a utility class ("private" constructor). By policy, all Commons Math exceptions must inherit from class "MathRuntimeException". For optimizers (package "optimization"), the number of allowed function evaluations is passed as a parameter to the "optimize" method. For solvers (package "analysis.solvers"), the number of allowed function evaluations is passed as a parameter to the "solve" method. Created a "MathRuntimeException" to serve as a base class for exception types that need to wrap another (lower-level) exception. Replaced "ComposableFunction" and "BinaryFunction" (in package "analysis") by a set of utilities in the new class "FunctionUtils" together with independent function objects in the new "analysis.function" package. Removed redundant "mapXxx" and "mapXxxToSelf" methods in "RealVector" and "AbstractRealVector" (in package "linear"). Refactoring of the "analysis.solvers" package. Removed the ode.jacobians package. Removed classes "FunctionEvaluationException", "MatrixVisitorException" and "DerivativeException" (superseded by the new "MathUserException"). Created a generic "Pair" class to replace the "AbstractMap.SimpleEntry" that is only available in Java 6 and later. Class "DirectSearchOptimizer" (and subclasses "NelderMead" and "MultiDirectional") was refactored into new classes: "SimplexOptimizer" and "AbstractSimplex" (and subclasses "NelderMeadSimplex" and "MultiDirectionalSimplex"). The old classes were deprecated and removed. Created "MatrixDimensionMismatchException" to replace "InvalidMatrixException" (when the latter is used to signal that matrices are not compatible for some operation). Replaced "MatrixIndexException" with "OutOfRangeException" (when the former is used to signal a row or column dimension mismatch). Made "sample" methods part of the "IntegerDistribution" and "ContinuousDistribution" interfaces. All distribution classes (in package "distribution") are immutable. Created an unchecked "FunctionEvaluationException". Modified the "UnivariateRealFunction" interface: Removed the checked "FunctionEvaluationException" from the signature of the "value" method. Modified semantics: "equals" methods now consider that NaNs are not equal (compliant with IEEE754). Also, two adjacent floating point numbers are considered equal (this is consistent with the fact that all real numbers between them can be represented by either of the two). One consequence of that is that "equals" is not transitive. Removed methods referring to the concept of "iteration". Removed interface methods to access the number of evaluations of the gradient and Jacobian. Added new "Incrementor" utility to be used as a bounded counter for objective function evaluations. Removed all references to "OptimizationException" (replaced by "ConvergenceException"). Modified "AbstractUnivariateRealOptimizer" to make it more similar to "BaseAbstractScalarOptimizer". Added utility methods in "ConvergingAlgorithmImpl". Refactoring of some classes in package "optimization.general" to remove code duplication and to allow the implementation of optimization algorithms that do not use derivatives. Created interface "BaseMultivariateRealOptimizer". Created classes "BaseAbstractScalarOptimizer" and "AbstractScalarOptimizer". "MultivariateRealOptimizer" and "DifferentiableMultivariateRealOptimizer" both extend "BaseMultivariateRealOptimizer". "AbstractScalarOptimizer" extends "BaseAbstractScalarOptimizer" and "AbstractScalarDifferentiableOptimizer" inherits from "AbstractScalarOptimizer". FastMath is not an exact replacement for StrictMath (partially fixed) added scalb(double, int), scalb(float, int) FastMath is not an exact replacement for StrictMath (partially fixed) added hypot(double, double), nextAfter(double, double) and nextAfter(float,double) (beware of the strange double second argument) so that they handle special values in the way as StrictMath FastMath is not an exact replacement for StrictMath (partially fixed) added getExponent(double) and getExponent(float) FastMath is not an exact replacement for StrictMath (partially fixed) Add copySign(double), copySign(float) separate discrete event detection from adaptive step size handling in ODE solvers, thus improving robustness, maintainability and speed Fixed an awkward statement that triggered a false positive warning. Added complementary error function, erfc. Modified erf (and erfc) to return extreme values for x with abs(x) > 40. For these arguments, the true value is indistinguishable from an extrema as a double. Added characteristic support to distributions, including methods to return numerical estimates of the mean and variance and upper and lower bounds of support. In version 2.2, methods returning distribution characteristics have been added only to the implementation classes. In version 3, supporting methods have been added to the abstract base classes and distribution interfaces. Added a constructor and addValues(double[]) methods allowing DescriptiveStatistics to be initialized with values from a double[] array. Similarly enhanced ResizeableDoubleArray. Added a getUniqueCount() method to Frequency to return the number of unique values included in the frequency table. Modified NormalDistributionImpl.cumulativeProbability to return 0 or 1, respectively for values more than 40 standard deviations from the mean. For these values, the actual probability is indistinguishable from 0 or 1 as a double. Top coding improves performance for extreme values and prevents convergence exceptions. Added toString() override to StatisticalSummaryValues. Deprecated the whole ode.jacobians package. It is clumsy and difficult to use. It will be replaced by a completely rewritten implementation in 3.0, which will be more tightly bound to the top level ode package Added a normalization feature to transform samples so they have zero mean and unit standard deviation Created "MathUserException" class to convey cause of failure between layers of user code separated by a layer of Commons Math code. Deprecated classes "FunctionEvaluationException", "MatrixVisitorException" and "DerivativeException". Fixed k-means++ to add several strategies to deal with empty clusters that may appear during iterations. Improved Percentile performance by using a selection algorithm instead of a complete sort, and by allowing caching data array and pivots when several different percentiles are desired. Fixed an error preventing zero length vectors to be built by some constructors. Fixed an error preventing ODE solvers to be restarted after they have been stopped by a discrete event. Added new random number generators from the Well Equidistributed Long-period Linear (WELL). Made intercept / no intercept configurable in multiple regression classes. By default, regression models are estimated with an intercept term. When the "noIntercept" property is set to true, regression models are estimated without intercepts. Fixed lost cause in MathRuntimeException.createInternalError. Note that the message is still the default message for internal errors asking to report a bug to Commons Math JIRA tracker. In order to retrieve the message from the root cause, one has to get the cause itself by getCause(). Modified multiple regression newSample methods to ensure that by default in all cases, regression models are estimated with intercept terms. Prior to the fix for this issue, newXSampleData(double[][]), newSampleData(double[], double[][]) and newSampleData(double[], double[][], double[][]) all required columns of "1's" to be inserted into the x[][] arrays to create a model with an intercept term; while newSampleData(double[], int, int) created a model including an intercept term without requiring the unitary column. All methods have been changed to eliminate the need for users to add unitary columns to specify regression models. Users of OLSMultipleLinearRegression or GLSMultipleLinearRegression versions 2.0 or 2.1 should either verify that their code either does not use the first set of data loading methods above or set the noIntercept property on estimated models to get the previous behavior. Added the dfp library providing arbitrary precision floating point computation in the spirit of IEEE 854-1987 (not exactly as it uses base 1000 instead of base 10). In addition to finite numbers, infinities and NaNs are available (but there are no subnormals). All IEEE 854-1987 rounding modes and signaling flags are supported. The available operations are +, -, *, / and the available functions are sqrt, sin, cos, tan, asin, acos, atan, exp, log. Added faster and more accurate version of traditional mathematical functions in a FastMath class intended to be a drop-in replacement for java.util.Math at source-level. Some functions still directly delegates to Math but this will improve with time. Some functions like exp may be twice as fast (even 3 times faster on some processors). Sine, cosine or power functions show typical speedups of 1.5 times faster or more. Added R-squared and adjusted R-squared statistics to OLSMultipleLinearRegression. Corrected the formula used for Y variance returned by calculateYVariance and associated methods in multiple regression classes (AbstractMultipleLinearRegression, OLSMultipleLinearRegression, GLSMultipleLinearRegression). These methods previously returned estimates of the variance in the model error term. New "calulateErrorVariance" methods have been added to compute what was previously returned by calculateYVariance. Bug fixed in Levenberg-Marquardt (handling of weights). Bug fixed in Levenberg-Marquardt (consistency of current). Bug fixed in chi-square computation in AbstractLeastSquaresOptimizer. Added support for Gaussian curve fitting. Fixed several bugs in "BrentOptimizer". Fixed inconsistency in return values in "MultiStartUnivariateRealOptimizer". Added a feature allowing error estimation to be computed only on a subset of Ordinary Differential Equations, considered as the main set, the remaining equations being considered only as an extension set that should not influence the ODE integration algorithm Fixed bug in precondition check (method "setMicrosphereElements"). Created "MultidimensionalCounter" class. Created package "exception" to contain the new exceptions hierarchy. Created package "exception.util": utilities for the exception classes (e.g. managing the localization of error messages). Default policy for dealing with invalid null references: raise a "NullArgumentException" (subclass of "IllegalArgumentException"). Implementation of linear interpolation. Improved localization of error messages. Allow multiple optimizations with a default simplex. Added new "equalsIncludingNaN" methods that have the same semantics as the old "equals" methods. These are deprecated, and their semantics will be modified (in the next major release) such that NaNs are not considered equal (to be more compliant with IEEE754). Added a setQRRankingThreshold method to Levenberg-Marquardt optimizer to improve robustness of rank determination. Added random data generation methods to RandomDataImpl for the remaining distributions in the distributions package. Added a generic nextInversionDeviate method that takes a discrete or continuous distribution as argument and generates a random deviate from the distribution. Also added sampling methods based on the implementations in RandomDataImpl to distributions. Fixed Levenberg-Marquardt optimizer that did not use the vectorial convergence checker. Now this optimizer can use either the general vectorial convergence checker or its own specialized convergence settings. Fixed loss of significance error in PersonsCorrelation p-value computation causing p-values smaller than the machine epsilon (~1E-16) to be reported as 0. Fix NullPointerException in BisectionSolver.solve(f, min, max, initial) Fix spelling of getSparcity [sic] method of OpenMapRealVector Fix problem with the default sparseIterator when a RealVector has exactly one non-zero entry Implementation of tricubic interpolation. Deprecated SmoothingBicubicSplineInterpolator and SmoothingBicubicSplineInterpolatorTest. Added BicubicSplineInterpolator and BicubicSplineInterpolatorTest. Added SmoothingPolynomialBicubicSplineInterpolator and SmoothingPolynomialBicubicSplineInterpolatorTest. Added method to clear the list of observations in PolynomialFitter. Fix use of wrong variable in SmoothingBicubicSplineInterpolatorTest.testPreconditions() Added method to clear the list of observations in CurveFitter. Implementation of bicubic interpolation. Added density functions to remaining continuous distributions (F, T, Weibull, Cauchy). As of Math 2.1, all continuous distributions implement density functions. The HasDensity interface has been deprecated and in version 3.0, density(double) will be added to the ContinuousDistribution interface. Changed equals() methods to use instanceof check rather than catching ClassCastException; this also allows some null checks to be omitted. Removed unnecessary null checks in equals methods. Fraction.hashCode() implementation was not fully consistent with Fraction.equals(). Changed hashCode() to use fields directly to agree with equals(). Resolved multiple problems leading to inaccuracy and/or failure to compute Normal, ChiSquare and Poisson probabilities, Erf and Gamma functions. Made Brent solver absolute accuracy configurable for all continuous distributions. Fixed too stringent interval check in Brent solver: initial guess is now allowed to be at either interval end Added a way to compute both the final state in an Initial Value Problem (IVP) for Ordinary Differential Equations (ODE) and its derivatives with respect to initial state and with respect to some problem parameters. This allows wrapping ODE solvers into optimization or root finding algorithms, which in turn can be used to solve Boundary Value Problems (BVP). There are no implementations (yet) of BVP solvers in the library. Fixed an error in events handling in ODE solvers. In some rare cases, events occurring close to a step start were handled without truncating the step, making them appear as is they occurred close to the step end Fixed a problem with getInterpolatedDerivatives returning zero derivatives when an ODE step handler is configured to not use interpolation. It now returns a constant but non-zero value consistent with at least one point inside the step Fixed wrong return values when enpoints are roots in Brent solver with a user provided initial guess Fixed a missing bracketing check of initial interval in Brent solver. In SVD, the matrices passed to EigenDecomposition are now symmetric by construction (rather than simply by definition). In EigenDecomposition, once the tridiagonal form is obtained, the non-significant elements are set to 0. A EigenDecompositionImpl simplified makes it possible to compute the SVD of a singular matrix (with the right number of elements in the diagonal matrix) or a matrix with singular value(s) of multiplicity greater than 1. Added SemiVariance statistic. Added a warning in the getCoefficients method documentation for PolynomialFunctionLagrangeForm. Computation may be ill-conditioned so this method should be used with care. Fixed an error in BigFraction multiplication for large numerators that don't fit in a primitive int. Fixed a spurious exception in EigenDecompositionImpl when a 3x3 block had two identical eigenvalues. Added min/max getters for real vectors (not yet in the RealVector interface for compatibility purposes, but in the AbstractRealVector abstract class). Fixed automatic step initialization in embedded Runge-Kutta integrators. The relative tolerance setting was never used, only the absolute tolerance was used. Fixed regression in Frequency.getPct(Object) introduced in 2.0. Cumulative percent was being returned for Object arguments in place of percent. Singular Value Decomposition now computes either the compact SVD (using only positive singular values) or truncated SVD (using a user-specified maximal number of singular values). Fixed Singular Value Decomposition solving of singular systems. Added MathUtils methods to compute gcd and lcm for long arguments. Added support for weighted univariate statistics. Fixed a wrong implementation of the Linf norm in vectors. Fixed a convergence discrepancy with respect to theory in Gragg-Bulirsch-Stoer integrator. Fixed a wrong dimension check in SVD solver. Added composition features for real functions. Added mapping and iteration methods to vectors. Provided a default implementation for the numerous simple methods in the RealVectorInterface. Fixed an error in handling very close events in ODE integration. Fixed an overflow error in MathUtils.distance that was causing KMeansPlusPlusClusterer to fail with a NullPointerException when component distances between points exceeded Integer.MAXVALUE. Added generationsEvolved property to GeneticAlgorithm to track the number of generations evolved by the evolve() method before reaching the StoppingCondition. Fixed an index computation error in eigen decomposition. Once again, kudos to Dimitri for debugging this. Fixed an ArrayIndexOutOfBoundsException in eigen decomposition. Kudos to Dimitri for debugging this, it was really difficult. Fixed parameter test in RandomDataImpl#nextExponential. The method now throws IllegalArgumentException for mean = 0. Changed probability calculations for Binomial, Poisson, and Hypergeometric distributions to use Catherine Loader's saddle point approximations. Removed dead code from Complex#divide. Fixed implementation of RandomDataImpl#nextPoisson by implementing an alternative algorithm for large means. Added support for multidimensional interpolation using the robust microsphere algorithm. Fixed implementation of EmpiricalDistributionImpl#getUpperBounds to match interface contract. Added getGeneratorUpperBounds method to EmpiricalDistributionImpl providing previous behavior. Fixed wrong results on Loess interpolation, also added a way to set weights for smoothing and to ignore zero weights for coefficients computation Fixed a OutOfBoundException in simplex solver when some constraints are tight. Fixed misleading number formats in error messages for adaptive stepsize integrators. Added support for weighted descriptive statistics. Added normalizeArray method to MathUtils. Fixed a NullPointerException in simplex solver when no solution is possible and some constraints are negative. Removed an unused argument in a private method in simplex solver. Fixed an error induced by entries set to 0 in simplex solver. Fixed an error leading the simplex solver to compute the right solution but return another one. Prevent infinite loops in multi-directional direct optimization method when the start point is exactly at the optimal point. Prevent possible zero divides on eigenvectors of indefinite matrices Fixed an error in RealMatrix javadoc Added an implementation of the Mersenne twister pseudo random number generator from Makoto Matsumoto and Takuji Nishimura Changed the return type of the various interpolation algorithms to the specific implementation of UnivariateRealFunction each one uses The behavior of the bracket method in UnivariateRealSolverUtils has been changed to return successfully when a tentative bracketing interval has a root exactly at one of its end points. Previously, such intervals triggered an exception. Added a check for too few rows with respect to the number of predictors in linear regression Added a getCovariance method to singular value decomposition Added robust locally weighted regression (Loess). Added a scalar multiply to the Complex class Added curve fitting with a general case and two specific cases (polynomial and harmonic). Optimized Complex isNaN(), isInfinite() by moving computation to constructor. Added Genetic Algorithm implementation. Fixed detection of not positive definite matrices in Cholesky decomposition Fixed a wrong check for basic variables Fixed a problem when setting some variables (several variables were set instead of only one) Added a way to limit the number of functions evaluations in optimizers (the number of iterations could already be limited) Added digamma function. Added Spearman's rank correlation (SpearmansCorrelation). Added support for rank transformations. Completed internationalization of all error messages Added a clustering package with an implementation of the k-means++ algorithm Added distance1, distance and distanceInf utility methods for double and int arrays in MathUtils Added an utility equality method between double numbers using tolerance in ulps (Units in Last Position) Added getNorm1, getNormInf, distance1 and distanceInf to the Vector3D class Added support for any type of field in linear algebra (FielxMatrix, FieldVector, FieldLUDecomposition) The RealMatrixImpl implementation classes has been renamed Array2DRowRealMatrix to reflect its specificities and for consistency with the new implementations. The previous name is still available but is deprecated Added a block-based storage type for dense matrices improving speed for large dimensions Added AggregateSummaryStatistics class to support aggregation of SummaryStatistics. Added general Field and FieldElement interfaces to allow generic algorithms to operate on fields. The library already provides several implementations: Complex, Fraction, BigFraction and BigReal Fixed inconsistent access to multidimensional array in FastFourierTransformer Greatly improved multiplication speed for sparse matrices Fixed threading issues with MathException and MathRuntimeException Fixed threading issues with UnivariateRealSolverUtils factory Reduced visibility of MessagesResources_fr.contents field to private Added Fraction.toString() Added add/subtract/multiply/divide functions with integer parameters to Fraction Added some utility functions to compute powers with integral types (int, long, BigInteger). Fixed a comparison error when two different fractions evaluate to the same double due to limited precision. Added a BigFraction class that does not overflow when big numerators or denominators are used. Added an optimizer for constrained linear problems based on 2-phases standard simplex. Redesigned the optimization framework for a simpler yet more powerful API. Added non-linear conjugate gradient optimizer. Fixed an error in computing gcd and lcm for some extreme values at integer range boundaries. Added a MathUtils method to check equality given some error bounds. Added PearsonsCorrelation class to compute correlation matrices, standard errors and p-values for correlation coefficients. Added Covariance class to compute variance-covariance matrices in new correlation package. Improved fast Fourier transform efficiency. Added a SparseRealVector class that implements a sparse vector for the RealVector interface. Added factory methods to create Chebyshev, Hermite, Laguerre and Legendre polynomials. Added add, subtract, negate, multiply and toString methods to PolynomialFunction. Changed FractionFormat to extend NumberFormat. Forced symmetry in binomialCoefficientLog and added test cases for MathUtils. Fixed error in binomial coefficient computation. Added a Legendre-Gauss integrator. Fixed error in factorial computation for 17 <= n <= 20. Integration algorithms now can have both relative and absolute accuracy settings. Added a new univariate sub-package below the optimization package. The analysis package has been reorganized with several sub-packages. Fixed an error in gcd computation for large values. Added method to walk matrix entries with or without changing them in the visitor design pattern sense. Three different orders can be used, row by row, column by column or optimized order according to internal storage. Optimized order should be preferred when no specific order is needed, because it will be more cache efficient. Added Fast Hadamard Transform. Added nth root computation for complex numbers. Added support for sparse matrix. Added an int/double hash map (OpenIntToDoubleHashMap) with much smaller memory overhead than standard java.util.Map (open addressing and no boxing). Added support for multi-dimensional Fourier transform. The root solvers and the integrators now take the function to solve as a parameter to the solve/integrate methods, thus allowing to reuse the same solver/integrator for different functions. Added setter methods for rows and columns in matrices. Added Frobenius matrix norm. Added an abstract matrix implementation simplifying extension by users. Added support for the Zipf distribution. Added support for copying statistics. Changes to stats classes include copy constructor, static copy(-,-) and instance copy() methods. Added copy() to UnivariateStatistic and StorelessUnivariateStatistic interfaces. Added a removal feature for observations in descriptive statistics. Added a scalb method in MathUtils. This method is similar to the method with same name added in java.lang.Math as of Java 6. Fixed F distribution inverse CDF computation for small denominator degrees of freedom. Fixed an error in CorrelatedRandomVectorGenerator leading to a component of the generated vector being constant. Greatly improved QR-decomposition speed using transposed matrices internally. Fixed an infinite loop encountered in some backward integration cases for ODE solvers. Added beta distribution. Added probability density functions computation for distributions for which it is feasible. Changed the Complex.equals() method so that it considers +0 and -0 are equal, as required by IEEE-754 standard. Added JAMA-like interfaces for eigen/singular problems. The implementation are based on the very quick dqd/dqds algorithms and some parts of the MRRR algorithm. This leads to very fast and accurate solutions. Added JAMA-like interfaces for decomposition algorithms. These interfaces decompose a matrix as a product of several other matrices with predefined properties and shapes depending on the algorithm. These algorithms allow to solve the equation A * X = B, either for an exact linear solution (LU-decomposition, Cholesky decomposition) or an exact or least-squares solution (QR-decomposition). Added removeData methods for the SimpleRegression class. This allows to support regression calculations across a sliding window of (time-based) observations without having to recalculate for the entire window every time. Support for one dimensional vectors has been added to the linear algebra package with a RealVector interface, a RealVectorImpl default implementation using a single double array to store elements and a RealVectorFormat for input/output. Changed OLS regression implementation added in MATH-203 to use QR decomposition to solve the normal equations. New ODE integrators have been added: the explicit Adams-Bashforth and implicit Adams-Moulton multistep methods. As the implementations of these methods are based on Nordsieck vector rather than a traditional array of previous steps, they both have been improved to handle adaptive stepsize. These methods provide the same rich features has the existing ones: continuous output, step handlers, discrete events, G-stop ... Replaced size adjustment of all steps of fixed steps Runge-Kutta integrators by a truncation of the last step only. The ODE integrators now support several step handlers at once, instead of just one. This is more consistent with event handlers management. The setStepHandler method has therefore been replaced by addStephandler, the getStepHandler method has been replaced by getStepHandlers which returns a Collection and a clearStepHandlers method has been added. All ODE integrators now support setting a maximal number of evaluations of differential equations function. If this number is exceeded, an exception will be thrown during integration. This can be used to prevent infinite loops if for example error control or discrete events create a really large number of extremely small steps. All step interpolators for ODE integrators now provide interpolation for both the state and its time derivatives. The interpolated derivatives are the exact derivatives of the interpolated state, thus preserving consistency. The general step handlers hence do not need to call the derivation function anymore. The fixed step handlers also get the time derivative of the state as an additional argument along with the state when they are called. Changed return type for FirstOrderIntegrator.integrate() to double in order to retrieve exact stop time. This allows to handle properly integration interruption due to an EventHandler instance asking to stop the integration when its associated event is triggered. The state was already set to the current state at interruption time, but it was difficult to get the corresponding time (it involved setting a step handler monitoring the last step specially). Events handlers in the ODE package now also provide the switching function variation (increasing/decreasing) when an event occurs Clarified the ODE package by breaking in into several sub-packages and renaming classes (SwitchingFunctions/EventHandler, SwitchingFunctionsHandler/CombinedEventsManager) Changed return type for FirstOrderIntegrator.getSwitchingFunctions() to a collection of SwitchingFunction instances. This better suits the intended use of the method and fixes a visibility error since the previous return type referenced the package private SwitchState class. Fixed dimension error on output vector for the operate method in RealMatrixImpl and BigMatrixImpl classes. The FirstOrderDifferentialEquations, FirstOrderIntegrator and FixedStepHandler interfaces now extends Serializable, allowing integrators, problems and handlers to be embedded into users Serializable classes. Added several convenience methods and constants for Vector3D and Rotation. Replaced public no argument constructors with IDENTITY or ZERO static instances for immutable classes Vector3D and Rotation. Fixed inconsistencies in the naming scheme for static fields in Vector3D and Rotation with respect to the overall library. Greatly improved RealMatrixImpl and BigMatrixImpl performances, both in terms of speed and in terms of temporary memory footprint. Added Mauro's patch to support multiple regression. Starting with version 2.0 of the library, the minimal version of the Java platform required to compile and use Commons Math is Java 5. This version is widely deployed now on many systems. It brings new important features for specific mathematical developments, for example new functions (log10, cbrt, ulp, signum, cosh, sinh, tanh, hypot, expm1, log1p), autoboxing, MathContext or RoundingMode. It also brings important features for general development, for example enums, generics or annotations. Switching functions can now throw dedicated SwitchException from all their method. At upper call level, the various ODE integrators handle these new exceptions and wrap them into IntegratorException instances, hence the integrators methods signature did not change. Added the getSwitchingFunctions and clearSwitchingFunctions to the FirstOrderIntegrator interface and all its implementations Removed deprecated features. This includes the following changes. Factory-based instantiation replaced by setter injection in 1.2 in several classes have been removed. Protected fields in matrices implementations have been declared final and private. Identity factory methods moved to MatrixUtils class have been removed. Complex utilities methods that have been superseded by Complex instance methods have been removed. Fixed formula in fast cosine transformer javadoc comments. Javadoc and style fixes. Added an error detection for missing imaginary character while parsing complex string Detect numerical problems in Q.R decomposition for Levenberg-Marquardt estimator and report them appropriately Fixed several crashes in getCovariances() and guessParametersErrors() in AbstractEstimator when some parameters are bound. The methods now explicitly give result only about unbound parameters. Fixed truncation error in t-test classes for large t values. Added root checks for the endpoints. Fixed numerous warnings in test code. Use the initial guess provided by the user in BrentSolver.solve(), thus improving speed. Added the estimation, optimization, geometry and ode packages from the Mantissa library. Made ComplexFormat format double values with a provided NumberFormat instance instead of using the real part format for all values. Added Pascal distribution implementation. Added QR Decomposition. Modified ProperFractionFormat to reject embedded minus signs. Added a nextAfter method in MathUtils to return the next machine-representable number in a specified direction from a given floating point number. Used this to ensure that MathUtils.round does not return incorrect results for numbers with bad IEEE754 representations. Added Fast Fourier Transform implementation. Modified getSumSquaredErrors method in SimpleRegression to always return a non-negative result. Corrected nextInt and nextLong to handle wide value ranges. Increased default precision of Gamma and Beta functions. Added log function to MathUtils. Added two sample (binned comparison) ChiSquare test. Modified NormalDistributionImpl.cumulativeProbablity to catch MaxIterationsExceededException and return 0 or 1, resp. if the argument is more than 20 standard deviations from the mean. Added SynchronizedDescriptiveStatistics class. Added addAndCheck, mulAndCheck, and subAndCheck MathUtils methods for long integer arguments. Merged most functions from ComplexUtils into Complex class, added static factory method to Complex. Deprecated abstract factory methods and made DescriptiveStatistics and and SummaryStatistics concrete classes. Pushed implementations up from DescriptiveStatisticsImpl, SummaryStatisticsImpl. Made implementations of statistics configurable via setters. Changed Mean.evaluate() to use a two-pass algorithm, improving accuracy by exploiting the the fact that this method has access to the full array of data values. Added check and rescaling of expected counts to sum to sum of expected counts if necessary in ChiSquare test. Handle multiplication of Complex numbers with infinite parts specially. Add errors guessing to least-squares estimators. Add tests for Fraction constructor using double parameter. Added one-way ANOVA implementation. Add Fraction constructor using max denominator value. Add integer overflow checks in Fraction constructor using double parameter. Throw EOFException when using empty files with ValueServer in replay and digest modes. Added a equals and hash methods in MathUtils to check for double arrays Added an angle normalization method in MathUtils to force angles in some user-defined interval Added vectorial covariance computation (either sample or population covariance) Added multivariate summary statistics. Added getSumOfLogs method to SummaryStatistics and made SumOfLogs instance used by GeometricMean configurable. Fixed AbstractIntegerDistribution cumulativeProbablility(-,-) to correctly handle double arguments. Made numerator and denominator final in Fraction and deprecated protected real and imaginary parts fields in Complex, making Fraction immutable and preparing Complex to become fully immutable in 2.0. Made NewtonSolver derivative field transient and implemented readObject to initialize. Made sampleStats field private and changed getUpperBounds to return a fresh copy in EmpiricalDistributionImpl. Added polar2Complex method to ComplexUtils to create Complex numbers from polar representations. Made all serialVersionUIDs private. Improved documentation and test cases related to handling of infinite and NaN values in Complex, ComplexUtils classes. Fixed incorrect NaN handling in o.a.m.s.d.rank.Min, Max Changed RealMatrixImpl.equals to use Double.doubleToLongBits to compare corresponding matrix entries. Eliminated floating point equals comparison in Percentile.evaluate. Eliminated unnecessary assignment statements in Skewness.getResult method. Synchronized getters in ResizeableDoubleArray. Eliminated unnecessary assignment statement in BisectionSolver.solve method. Implemented hashCode in the Complex class and changed the semantics of equals to make all instances with real or imaginary part NaN equal. Fixed bin index overflow problem in EmpiricalDistributionImpl. Added protection against numerical overflow and underflow in the isBracketing method. Fixed division by zero error in rounding methods. Added upper tail cumulative probability method to HypergeometricDistributionImpl. Added better handling of numerical overflow and division by zero in Complex calculations. Changed ContinuedFraction to better handle infinite convergents that resulted in divergent continued fraction evaluations. Changed rounding methods to not rely on BigDecimal conversions which was causing numerical error. Changed Fraction(double) to correctly handle near-integral arguments. Changed lcm to throw ArithmeticException (instead of returning bogus value) if the result is too large to store as an integer. Added factories for TTest, ChiSquareTest and TestUtils class with static methods to create instances and execute tests. Eliminated repeated endpoint function evalutations in BrentSolver, SecantSolver. Added setSubMatrix methods to RealMatrixImpl, BigMatrixImpl. To Preserve backward compatibility with version 1.0, these methods were not added to the RealMatrix, BigMatrix interfaces. Added createXIdentityMatrix methods to MatrixUtils and deprecated getIdentity methods in RealMatrixImpl, BigMatrixImpl. Modified RealMatrixImpl, BigMatrixImpl constructors to throw IllegalArgumentExceptions instead of ArrayIndexOutOfBounds when dimension arguments are not positive. Made PRNG pluggable for classes in the random package. Added RandomGenerator interface extracted from java.util.random and abstract implementation, AbstractRandomGenerator providing default implementations of methods based on nextDouble(). Added a constructor taking a RandomGenerator as an argument to RandomDataImpl. Changed ValueServer to use a RandomData in its constructor. Changes to 1.0 classes should be backward compatible (including serialization). Added utility methods for overflow-checked integer arithmetic and improved gcd method in MathUtils. Fixed error in TTestImpl.homoscedasticTtest. Implementation was incorrectly using heteroscedastic t statistic. Also improved sensitivity of test cases. Fixed javadoc errors. One-sided t-test significance adjustment was reversed in javadoc for boolean-valued test methods. Fixed bug in PolynomialSplineFunction to allow evaluation of the function at the last knot point. Added Weibull distribution implementation. Added Cauchy distribution implementation. Added convenience methods for rounding. Added Fraction class based on commons-lang implementation. With the fraction class, FractionFormat and ProperFractionFormat classes were added to provide fraction formatting and parsing. commons-math3-3.2-src/src/media/math.xcf100644 1750 1750 47626 12126627666 16737 0ustarlucluc 0 0 gimp xcf v001,d.../13 7 :.-*(4 9>>A;D3HDG;DL$QD]y3"M,Z?&I.Vkv'uI4b8x88w8P8?8,dBackground (0ms)     ,dOrO~O,dB+7>">*@F K       ƿ ƽ þÿžѺ½Ӿɿýƿ̿¿Ƚÿ¾ºÿ½ž¾ ¾Ⱦþӽ¾ʾþԾŽо þ¿˿ȿɿþоľ¾þ¾ƽ¾ ξ ǿͺᄑľƽཾý彿Ǿ¿󽾽 齾þ¿򽾿¿  ľ¿¿鿽ƾſؽǾ¾ý Ŀοҽκ ˾ֿƽ ÿ ƿȾǿg}}|{n|ǿ}}|{nn¿¿ǿHPFF988N33E2Z0  .¾¾R'¿¿K$%!!6!¾¾>o" rT--=fe]Y6¿¿»W6ݻ¾c6 ɿ¾ÿ W6 žW6 ƾ¾H6 þW6z6  Commons Math /images/math.gif /index.html commons-math3-3.2-src/src/site/apt/userguide/special.apt100644 1750 1750 23616 12126627721 22060 0ustarlucluc 0 0 ~~ ~~ Licensed to the Apache Software Foundation (ASF) under one or more ~~ contributor license agreements. See the NOTICE file distributed with ~~ this work for additional information regarding copyright ownership. ~~ The ASF licenses this file to You 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. ~~ ----------------------------------------------- The Commons Math User Guide - Special Functions ----------------------------------------------- 5 Special Functions * 5.1 Overview The <<>> package of Commons-Math gathers several useful special functions not provided by <<>>. * 5.2 Erf functions {{{../apidocs/org/apache/commons/math3/special/Erf.html}Erf}} contains several useful functions involving the Error Function, Erf. *----------------+---------+-------------------------------------------------------------------+ || Function || Method || Reference | *----------------+---------+-------------------------------------------------------------------+ | Error Function |erf | See {{{http://mathworld.wolfram.com/Erf.html}Erf}} from MathWorld | *----------------+---------+-------------------------------------------------------------------+ * 5.3 Gamma functions Class {{{../apidocs/org/apache/commons/math3/special/Gamma.html}<<>>}} contains several useful functions involving the Gamma Function. ** Gamma <<>> computes the Gamma function, Γ(x) (see {{{http://mathworld.wolfram.com/GammaFunction.html}MathWorld}}, {{{http://dlmf.nist.gov/5}DLMF}}). The accuracy of the Commons-Math implementation is assessed by comparaison with high precision values computed with the {{{http://maxima.sourceforge.net/}Maxima}} Computer Algebra System. *----------------+-----------------------------------------------+-----------------+----------------------+----------------+ || Interval || Values tested || Average error || Standard deviation || Maximum error | *----------------+-----------------------------------------------+-----------------+----------------------+----------------+ | -5 \< x \< -4 | <<>> | 0.49 ulps | 0.57 ulps | 3.0 ulps | *----------------+-----------------------------------------------+-----------------+----------------------+----------------+ | -4 \< x \< -3 | <<>> | 0.36 ulps | 0.51 ulps | 2.0 ulps | *----------------+-----------------------------------------------+-----------------+----------------------+----------------+ | -3 \< x \< -2 | <<>> | 0.41 ulps | 0.53 ulps | 2.0 ulps | *----------------+-----------------------------------------------+-----------------+----------------------+----------------+ | -2 \< x \< -1 | <<>> | 0.37 ulps | 0.50 ulps | 2.0 ulps | *----------------+-----------------------------------------------+-----------------+----------------------+----------------+ | -1 \< x \< 0 | <<>> | 0.46 ulps | 0.54 ulps | 2.0 ulps | *----------------+-----------------------------------------------+-----------------+----------------------+----------------+ | 0 \< x ≤ 8 | <<>> | 0.33 ulps | 0.48 ulps | 2.0 ulps | *----------------+-----------------------------------------------+-----------------+----------------------+----------------+ | 8 \< x ≤ 141 | <<>> | 1.32 ulps | 1.19 ulps | 7.0 ulps | *----------------+-----------------------------------------------+-----------------+----------------------+----------------+ ** Log Gamma <<>> computes the natural logarithm of the Gamma function, log \u0393(x), for x > 0 (see {{{http://mathworld.wolfram.com/LogGammaFunction.html}MathWorld}}, {{{http://dlmf.nist.gov/5}DLMF}}). The accuracy of the Commons-Math implementation is assessed by comparaison with high precision values computed with the {{{http://maxima.sourceforge.net/}Maxima}} Computer Algebra System. *------------------------------------------------+-----------------------------------------------+-----------------+----------------------+----------------+ || Interval || Values tested || Average error || Standard deviation || Maximum error | *------------------------------------------------+-----------------------------------------------+-----------------+----------------------+----------------+ | 0 \< x ≤ 8 | <<>> | 0.32 ulps | 0.50 ulps | 4.0 ulps | *------------------------------------------------+-----------------------------------------------+-----------------+----------------------+----------------+ | 8 \< x ≤ 1024 | <<>> | 0.43 ulps | 0.53 ulps | 3.0 ulps | *------------------------------------------------+-----------------------------------------------+-----------------+----------------------+----------------+ | 1024 \< x ≤ 8192 | <<>> | 0.53 ulps | 0.56 ulps | 3.0 ulps | *------------------------------------------------+-----------------------------------------------+-----------------+----------------------+----------------+ | 8933.439345993791 ≤ x ≤ 1.75555970201398e+305 | <<>> | 0.35 ulps | 0.49 ulps | 2.0 ulps | *------------------------------------------------+-----------------------------------------------+-----------------+----------------------+----------------+ ** Regularized Gamma <<>> computes the value of the regularized Gamma function, P(a, x) (see {{{http://mathworld.wolfram.com/RegularizedGammaFunction.html}MathWorld}}). * 5.4 Beta functions {{{../apidocs/org/apache/commons/math3/special/Beta.html}Beta}} contains several useful functions involving the Beta Function. ** Log Beta <<>> computes the value of the natural logarithm of the Beta function, log B(a, b). (see {{{http://mathworld.wolfram.com/BetaFunction.html}MathWorld}}, {{{http://dlmf.nist.gov/5.12}DLMF}}). The accuracy of the Commons-Math implementation is assessed by comparaison with high precision values computed with the {{{http://maxima.sourceforge.net/}Maxima}} Computer Algebra System. *----------------+-----------------------------------------+-----------------+----------------------+----------------+ || Interval || Values tested || Average error || Standard deviation || Maximum error | *----------------+-----------------------------------------+-----------------+----------------------+-----------------+ | 0 \< x ≤ 8\ | <<>>\ | 1.80 ulps | 81.08 ulps | 14031.0 ulps | | 0 \< y ≤ 8 | <<>> | | | | *----------------+-----------------------------------------+-----------------+----------------------+-----------------+ | 0 \< x ≤ 8\ | <<>>\ | 0.50 ulps | 3.64 ulps | 694.0 ulps | | 8 \< y ≤ 16 | <<>> | | | | *----------------+-----------------------------------------+-----------------+----------------------+-----------------+ | 0 \< x ≤ 8\ | <<>>\ | 1.04 ulps | 139.32 ulps | 34509.0 ulps | | 16 \< y ≤ 256 | <<>> | | | | *----------------+-----------------------------------------+-----------------+----------------------+-----------------+ | 8 \< x ≤ 16\ | <<>>\ | 0.35 ulps | 0.48 ulps | 2.0 ulps | | 8 \< y ≤ 16 | <<>> | | | | *----------------+-----------------------------------------+-----------------+----------------------+-----------------+ | 8 \< x ≤ 16\ | <<>>\ | 0.31 ulps | 0.47 ulps | 2.0 ulps | | 16 \< y ≤ 256 | <<>> | | | | *----------------+-----------------------------------------+-----------------+----------------------+-----------------+ | 16 \< x ≤ 256\ | <<>>\ | 0.35 ulps | 0.49 ulps | 2.0 ulps | | 16 \< y ≤ 256 | <<>> | | | | *----------------+-----------------------------------------+-----------------+----------------------+-----------------+ ** Regularized Beta (see {{{http://mathworld.wolfram.com/RegularizedBetaFunction.html}MathWorld}}) commons-math3-3.2-src/src/site/xdoc/issue-tracking.xml100644 1750 1750 13221 12126627721 21550 0ustarlucluc 0 0 Commons Math Issue tracking Commons Documentation Team

                                    Commons Math uses ASF JIRA for tracking issues. See the Commons Math JIRA project page.

                                    To use JIRA you may need to create an account (if you have previously created/updated Commons issues using Bugzilla an account will have been automatically created and you can use the Forgot Password page to get a new password).

                                    If you would like to report a bug, or raise an enhancement request with Commons Math please do the following:

                                    1. Search existing open bugs. If you find your issue listed then please add a comment with your details.
                                    2. Search the mailing list archive(s). You may find your issue or idea has already been discussed.
                                    3. Decide if your issue is a bug or an enhancement.
                                    4. Submit either a bug report or enhancement request.

                                    Please also remember these points:

                                    • the more information you provide, the better we can help you
                                    • test cases are vital, particularly for any proposed enhancements
                                    • the developers of Commons Math are all unpaid volunteers

                                    For more information on subversion and creating patches see the Apache Contributors Guide.

                                    You may also find these links useful:

                                    commons-math3-3.2-src/src/site/xdoc/download_math.xml100644 1750 1750 20721 12126627721 21443 0ustarlucluc 0 0 Download Commons Math Commons Documentation Team

                                    We recommend you use a mirror to download our release builds, but you must verify the integrity of the downloaded files using signatures downloaded from our main distribution directories. Recent releases (48 hours) may not yet be available from the mirrors.

                                    You are currently using [preferred]. If you encounter a problem with this mirror, please select another mirror. If all mirrors are failing, there are backup mirrors (at the end of the mirrors list) that should be available.

                                    [if-any logo][end]

                                    Other mirrors:

                                    The KEYS link links to the code signing keys used to sign the product. The PGP link downloads the OpenPGP compatible signature from our main site. The MD5 link downloads the checksum from the main site.

                                    commons-math3-3.2-bin.tar.gz md5 pgp
                                    commons-math3-3.2-bin.zip md5 pgp
                                    commons-math3-3.2-src.tar.gz md5 pgp
                                    commons-math3-3.2-src.zip md5 pgp
                                    commons-math-2.2.tar.gz md5 pgp
                                    commons-math-2.2.zip md5 pgp
                                    commons-math-2.2-src.tar.gz md5 pgp
                                    commons-math-2.2-src.zip md5 pgp

                                    Older releases can be obtained from the archives.

                                    commons-math3-3.2-src/src/site/xdoc/index.xml100644 1750 1750 5355 12126627721 17720 0ustarlucluc 0 0 Commons Math: The Apache Commons Mathematics Library

                                    Commons Math is a library of lightweight, self-contained mathematics and statistics components addressing the most common problems not available in the Java programming language or Commons Lang.

                                    Guiding principles:

                                    1. Real-world application use cases determine development priority.
                                    2. This package emphasizes small, easily integrated components rather than large libraries with complex dependencies and configurations.
                                    3. All algorithms are fully documented and follow generally accepted best practices.
                                    4. In situations where multiple standard algorithms exist, a Strategy pattern is used to support multiple implementations.
                                    5. Limited dependencies. No external dependencies beyond Commons components and the core Java platform (at least Java 1.3 up to version 1.2 of the library, at least Java 5 starting with version 2.0 of the library).

                                    Download the Latest Release of Commons Math.

                                    commons-math3-3.2-src/src/site/xdoc/proposal.xml100644 1750 1750 12446 12126627721 20467 0ustarlucluc 0 0 Proposal for math Package Robert Burrell Donkin

                                    The Java programming language and the math extensions in commons-lang provide implementations for only the most basic mathematical algorithms. Routine development tasks such as computing basic statistics or solving a system of linear equations require components not available in java or commons-lang.

                                    Most basic mathematical or statistical algorithms are available in open source implementations, but to assemble a simple set of capabilities one has to use multiple libraries, many of which have more restrictive licensing terms than the ASF. In addition, many of the best open source implementations (e.g. the R statistical package) are either not available in Java or require large support libraries and/or external dependencies to work.

                                    A commons-math community will provide a productive environment for aggregation, testing and support of efficient Java implementations of commonly used mathematical and statistical algorithms.

                                    The Math project shall create and maintain a library of lightweight, self-contained mathematics and statistics components addressing the most common practical problems not immediately available in the Java programming language or commons-lang. The guiding principles for commons-math will be:

                                    1. Real-world application use cases determine priority
                                    2. Emphasis on small, easily integrated components rather than large libraries with complex dependencies
                                    3. All algorithms are fully documented and follow generally accepted best practices
                                    4. In situations where multiple standard algorithms exist, use the Strategy pattern to support multiple implementations
                                    5. Limited dependencies. No external dependencies beyond Commons components and the JDK

                                    math relies only on standard JDK 1.2 (or later) APIs for production deployment. It utilizes the JUnit unit testing framework for developing and executing unit tests, but this is of interest only to developers of the component.

                                    No external configuration files are utilized.

                                    The initial codebase will consist of implementations of basic statistical algorithms such as the following:

                                    • Simple univariate statistics (mean, standard deviation, n, confidence intervals)
                                    • Frequency distributions
                                    • t-test, chi-square test
                                    • Random numbers from Gaussian, Exponential, Poisson distributions
                                    • Random sampling/resampling
                                    • Bivariate regression, corellation
                                    and mathematical algorithms such as the following:
                                    • Basic Complex Number representation with algebraic operations
                                    • Newton's method for finding roots
                                    • Binomial coefficients
                                    • Exponential growth and decay (set up for financial applications)
                                    • Polynomial Interpolation (curve fitting)
                                    • Basic Matrix representation with algebraic operations

                                    The proposed package name for the new component is org.apache.commons.math3.

                                    • CVS Repository - New directory math in the jakarta-commons CVS repository.
                                    • Mailing List - Discussions will take place on the general dev@commons.apache.org mailing list. To help list subscribers identify messages of interest, it is suggested that the message subject of messages about this component be prefixed with [math].
                                    • Bugzilla - New component "math" under the "Commons" product category, with appropriate version identifiers as needed.
                                    • Jyve FAQ - New category "commons-math" (when available).

                                    The initial committers on the math component shall be:

                                    commons-math3-3.2-src/src/site/xdoc/mail-lists.xml100644 1750 1750 22224 12126627721 20701 0ustarlucluc 0 0 Commons Math Mailing Lists Commons Documentation Team

                                    Commons Math shares mailing lists with all the other Commons Components. To make it easier for people to only read messages related to components they are interested in, the convention in Commons is to prefix the subject line of messages with the component's name, for example:

                                    • [math] Problem with the ...

                                    Questions related to the usage of Commons Math should be posted to the User List.
                                    The Developer List is for questions and discussion related to the development of Commons Math.
                                    Please do not cross-post; developers are also subscribed to the user list.

                                    Note: please don't send patches or attachments to any of the mailing lists. Patches are best handled via the Issue Tracking system. Otherwise, please upload the file to a public server and include the URL in the mail.

                                    Please prefix the subject line of any messages for Commons Math with [math] - thanks!

                                    Name Subscribe Unsubscribe Post Archive Other Archives
                                    Commons User List

                                    Questions on using Commons Math.

                                    Subscribe Unsubscribe Post mail-archives.apache.org markmail.org
                                    www.mail-archive.com
                                    news.gmane.org
                                    Commons Developer List

                                    Discussion of development of Commons Math.

                                    Subscribe Unsubscribe Post mail-archives.apache.org markmail.org
                                    www.mail-archive.com
                                    news.gmane.org
                                    Commons Issues List

                                    Only for e-mails automatically generated by the issue tracking system.

                                    Subscribe Unsubscribe read only mail-archives.apache.org markmail.org
                                    www.mail-archive.com
                                    Commons Commits List

                                    Only for e-mails automatically generated by the source control sytem.

                                    Subscribe Unsubscribe read only mail-archives.apache.org markmail.org
                                    www.mail-archive.com

                                    Other mailing lists which you may find useful include:

                                    Name Subscribe Unsubscribe Post Archive Other Archives
                                    Apache Announce List

                                    General announcements of Apache project releases.

                                    Subscribe Unsubscribe read only mail-archives.apache.org markmail.org
                                    old.nabble.com
                                    www.mail-archive.com
                                    news.gmane.org
                                    commons-math3-3.2-src/src/site/xdoc/developers.xml100644 1750 1750 40172 12126627721 20775 0ustarlucluc 0 0 Developers Guide

                                    Creating and maintaining a mathematical and statistical library that is accurate requires a greater degree of communication than might be the case for other components. It is important that developers follow guidelines laid down by the community to ensure that the code they create can be successfully maintained by others.

                                    Developers are asked to comply with the following development guidelines. Code that does not comply with the guidelines including the word must will not be committed. Our aim will be to fix all of the exceptions to the "should" guidelines prior to a release.

                                    Getting Started

                                    1. Start by reviewing the overall objectives stated in the proposal upon which the project is founded.
                                    2. Download the Commons Math source code. Follow the instructions under the heading "Anonymous Subversion" on the Apache version control page (Also have a look at the Commons wiki svn page ) to check out the Commons Math code base from Subversion. The svn url for the current development sources of Commons Math is https://svn.apache.org/repos/asf/commons/proper/math/trunk
                                    3. Like most commons components, Commons Math uses Apache Maven as our build tool. We now use Maven 2 as our primary build platform (what we use to cut releases, for continuous integration builds, and for development). The sources can also be built using Ant (a working Ant build.xml is included in the top level project directory). To build Commons Math using Maven 2, you can follow the instructions for Building a project with Maven 2. Launch Maven from the top-level directory in the checkout of Commons Math trunk. No special setup is required, except that currently to build the site (i.e. to execute Maven's "site" goal), you may need to increase the default memory allocation (e.g. export MAVEN_OPTS=-Xmx512m) before launching Maven.
                                    4. Have a look at the new features that users and developers have requested on the Math Wish List Wiki Page.
                                    5. Be sure to join the commons-dev and commons-user email lists and use them appropriately (make sure the string "[math]" starts the Subject line of all your postings). Make any proposals here where the group can comment on them.
                                    6. Setup an account on JIRA and use it to submit patches and identify bugs. Read the directions for submitting bugs and search the database to determine if an issue exists or has already been dealt with.

                                      See the Commons Math Issue Tracking Page for more information on how to search for or submit bugs or enhancement requests.

                                    7. Generating patches: The requested format for generating patches is the Unified Diff format, which can be easily generated using the svn client or various IDEs. svn diff > patch

                                    Contributing ideas and code

                                    Follow the steps below when making suggestions for additions or enhancements to Commons Math. This will make it easier for the community to comment on your ideas and for the committers to keep track of them. Thanks in advance!

                                    1. Start with a post to the commons-dev mailing list, with [math] at the beginning of the subject line, followed by a good, short title describing the new feature or enhancement. For example, "[math] Principal Components Analysis." The body of the post should include each of the following items (but be as brief as possible):
                                      • A concise description of the new feature / enhancement
                                      • References to definitions and algorithms. Using standard definitions and algorithms makes communication much easier and will greatly increase the chances that we will accept the code / idea
                                      • Some indication of why the addition / enhancement is practically useful
                                    2. Assuming a generally favorable response to the idea on commons-dev, the next step is to add an entry to the Math Wish List corresponding to the idea. Include a reference to the discussion thread and, for substantial enhancements, a new Wiki page named using the enhancement / addition name, e.g. "PrincipalComponentsAnalysis." We can then us this page to lay out the development plan and track progress and decisions related to the feature.
                                    3. Create a JIRA ticket using the the feature title as the short description. Incorporate feedback from the initial posting in the description. Add a reference to the JIRA ticket to the WishList entry.
                                    4. Submit code as attachments to the JIRA ticket. Please use one ticket for each feature, adding multiple patches to the ticket as necessary. Use the svn diff command to generate your patches as diffs. Please do not submit modified copies of existing java files. Be patient (but not too patient) with committers reviewing patches. Post a *nudge* message to commons-dev with a reference to the ticket if a patch goes more than a few days with no comment or commit.

                                    Commons Math follows Code Conventions for the Java Programming Language. As part of the maven build process, style checking is performed using the Checkstyle plugin, using the properties specified in checkstyle.xml. Committed code should generate no Checkstyle errors. One thing that Checkstyle will complain about is tabs included in the source code. Please make sure to set your IDE or editor to use spaces instead of tabs.

                                    Committers should make sure that svn properties are correctly set on files added to the repository. See the section on Committer Subversion Access on the Apache Source Code Repositories page.

                                    • Committed code must include full javadoc.
                                    • All component contracts must be fully specified in the javadoc class, interface or method comments, including specification of acceptable ranges of values, exceptions or special return values.
                                    • External references or full statements of definitions for all mathematical terms used in component documentation must be provided.
                                    • Implementations should use standard algorithms and references or full descriptions of all algorithms should be provided.
                                    • Additions and enhancements should include updates to the User Guide.
                                    • Exceptions generated by Commons Math are all unchecked.
                                    • All public methods advertise all exceptions that they can generate. Exceptions must be documented in both javadoc and method signatures and the documentation in the javadoc must include full description of the conditions under which exceptions are thrown.
                                    • All exceptions inherit from the base class, MathRuntimeException.
                                    • Methods should fully specify parameter preconditions required for successful activation. When preconditions are violated, a MathIllegalArgumentException should be thrown. Subclasses of MathIllegalArgumentException may be used to represent common parameter contract violations (for example, NoBracketingException). Exception messages must contain sufficient information on parameter values to determine the exact precondition failure.
                                    • Exceptions generated by Commons Math make sense without knowing implementation details other than those stated in the public API. For example, a NoBracketingException makes sense thrown by a solver that has a precondition requiring that initial points bracket a root. This exception does not make sense, however, thrown by an inverse cumulative probability estimator.
                                    • MathIllegalArgumentException should only be thrown in situations where preconditions can be exhaustively provided so that what arguments are "illegal" can be specified fully to the caller. Domain-specific exceptions need to be defined for cases where failures cannot be attributed to parameter precondition violation. For example, the exact domain of successful activation of a solver or quadrature method may be impossible to specify because of numerical properties of the method. If a solver fails to find a root or a quadrature method fails to converge for a given set of parameters, unless those parameters violate the advertised preconditions it is not appropriate to throw MathIllegalArgumentException.
                                    • State information included in exception messages must be available in exception properties - i.e., successful handling or reporting of Commons Math exceptions must not require parsing exception messages.
                                    • Committed code must include unit tests.
                                    • Unit tests should provide full path coverage.
                                    • Unit tests should verify all boundary conditions specified in interface contracts, including verification that exceptions are thrown or special values (e.g. Double.NaN, Double.Infinity) are returned as expected.
                                    • All new source file submissions must include the Apache Software License in a comment that begins the file
                                    • All contributions must comply with the terms of the Apache Contributor License Agreement (CLA)
                                    • Patches must be accompanied by a clear reference to a "source" - if code has been "ported" from another language, clearly state the source of the original implementation. If the "expression" of a given algorithm is derivative, please note the original source (textbook, paper, etc.).
                                    • References to source materials covered by restrictive proprietary licenses should be avoided. In particular, contributions should not implement or include references to algorithms in Numerical Recipes (NR). Any questions about copyright or patent issues should be raised on the commons-dev mailing list before contributing or committing code.

                                    Here is a list of relevant materials. Much of the discussion surrounding the development of this component will refer to the various sources listed below, and frequently the Javadoc for a particular class or interface will link to a definition contained in these documents.

                                    Concerning floating point arithmetic.
                                    http://www.validlab.com/goldberg/paper.pdf
                                    http://www.cs.berkeley.edu/~wkahan/ieee754status/IEEE754.PDF
                                    http://www.cs.berkeley.edu/~wkahan/JAVAhurt.pdf
                                    Numerical analysis
                                    Scientific Computing FAQ @ Mathcom
                                    Bibliography of accuracy and stability of numerical algorithms
                                    SUNY Stony Brook numerical methods page
                                    SIAM Journal of Numerical Analysis Online
                                    Probability and statistics
                                    Statlib at CMU
                                    NIST Engineering Statistics Handbook
                                    Online Introductory Statistics (David W. Stockburger)
                                    Online Journal of Statistical Software
                                    References for mathematical definitions.
                                    http://rd11.web.cern.ch/RD11/rkb/titleA.html
                                    http://mathworld.wolfram.com/
                                    http://www.itl.nist.gov/div898/handbook
                                    Chan, T. F. and J. G. Lewis 1979, Communications of the ACM, vol. 22 no. 9, pp. 526-531.
                                    XML related resources.
                                    http://www.openmath.org
                                    commons-math3-3.2-src/src/site/xdoc/userguide/stat.xml100644 1750 1750 156617 12126627721 21630 0ustarlucluc 0 0 The Commons Math User Guide - Statistics

                                    The statistics package provides frameworks and implementations for basic Descriptive statistics, frequency distributions, bivariate regression, and t-, chi-square and ANOVA test statistics.

                                    Descriptive statistics

                                    Frequency distributions

                                    Simple Regression

                                    Multiple Regression

                                    Rank transformations

                                    Covariance and correlation

                                    Statistical Tests

                                    The stat package includes a framework and default implementations for the following Descriptive statistics:

                                    • arithmetic and geometric means
                                    • variance and standard deviation
                                    • sum, product, log sum, sum of squared values
                                    • minimum, maximum, median, and percentiles
                                    • skewness and kurtosis
                                    • first, second, third and fourth moments

                                    With the exception of percentiles and the median, all of these statistics can be computed without maintaining the full list of input data values in memory. The stat package provides interfaces and implementations that do not require value storage as well as implementations that operate on arrays of stored values.

                                    The top level interface is UnivariateStatistic. This interface, implemented by all statistics, consists of evaluate() methods that take double[] arrays as arguments and return the value of the statistic. This interface is extended by StorelessUnivariateStatistic, which adds increment(), getResult() and associated methods to support "storageless" implementations that maintain counters, sums or other state information as values are added using the increment() method.

                                    Abstract implementations of the top level interfaces are provided in AbstractUnivariateStatistic and AbstractStorelessUnivariateStatistic respectively.

                                    Each statistic is implemented as a separate class, in one of the subpackages (moment, rank, summary) and each extends one of the abstract classes above (depending on whether or not value storage is required to compute the statistic). There are several ways to instantiate and use statistics. Statistics can be instantiated and used directly, but it is generally more convenient (and efficient) to access them using the provided aggregates, DescriptiveStatistics and SummaryStatistics.

                                    DescriptiveStatistics maintains the input data in memory and has the capability of producing "rolling" statistics computed from a "window" consisting of the most recently added values.

                                    SummaryStatistics does not store the input data values in memory, so the statistics included in this aggregate are limited to those that can be computed in one pass through the data without access to the full array of values.

                                    AggregateStatistics IncludedValues stored? "Rolling" capability?
                                    DescriptiveStatisticsmin, max, mean, geometric mean, n, sum, sum of squares, standard deviation, variance, percentiles, skewness, kurtosis, medianYesYes
                                    SummaryStatisticsmin, max, mean, geometric mean, n, sum, sum of squares, standard deviation, varianceNoNo

                                    SummaryStatistics can be aggregated using AggregateSummaryStatistics. This class can be used to concurrently gather statistics for multiple datasets as well as for a combined sample including all of the data.

                                    MultivariateSummaryStatistics is similar to SummaryStatistics but handles n-tuple values instead of scalar values. It can also compute the full covariance matrix for the input data.

                                    Neither DescriptiveStatistics nor SummaryStatistics is thread-safe. SynchronizedDescriptiveStatistics and SynchronizedSummaryStatistics, respectively, provide thread-safe versions for applications that require concurrent access to statistical aggregates by multiple threads. SynchronizedMultivariateSummaryStatistics provides thread-safe MultivariateSummaryStatistics.

                                    There is also a utility class, StatUtils, that provides static methods for computing statistics directly from double[] arrays.

                                    Here are some examples showing how to compute Descriptive statistics.

                                    Compute summary statistics for a list of double values


                                    Using the DescriptiveStatistics aggregate (values are stored in memory): // Get a DescriptiveStatistics instance DescriptiveStatistics stats = new DescriptiveStatistics(); // Add the data from the array for( int i = 0; i < inputArray.length; i++) { stats.addValue(inputArray[i]); } // Compute some statistics double mean = stats.getMean(); double std = stats.getStandardDeviation(); double median = stats.getPercentile(50);
                                    Using the SummaryStatistics aggregate (values are not stored in memory): // Get a SummaryStatistics instance SummaryStatistics stats = new SummaryStatistics(); // Read data from an input stream, // adding values and updating sums, counters, etc. while (line != null) { line = in.readLine(); stats.addValue(Double.parseDouble(line.trim())); } in.close(); // Compute the statistics double mean = stats.getMean(); double std = stats.getStandardDeviation(); //double median = stats.getMedian(); <-- NOT AVAILABLE
                                    Using the StatUtils utility class: // Compute statistics directly from the array // assume values is a double[] array double mean = StatUtils.mean(values); double std = StatUtils.variance(values); double median = StatUtils.percentile(values, 50); // Compute the mean of the first three values in the array mean = StatUtils.mean(values, 0, 3);
                                    Maintain a "rolling mean" of the most recent 100 values from an input stream


                                    Use a DescriptiveStatistics instance with window size set to 100 // Create a DescriptiveStats instance and set the window size to 100 DescriptiveStatistics stats = new DescriptiveStatistics(); stats.setWindowSize(100); // Read data from an input stream, // displaying the mean of the most recent 100 observations // after every 100 observations long nLines = 0; while (line != null) { line = in.readLine(); stats.addValue(Double.parseDouble(line.trim())); if (nLines == 100) { nLines = 0; System.out.println(stats.getMean()); } } in.close();
                                    Compute statistics in a thread-safe manner

                                    Use a SynchronizedDescriptiveStatistics instance // Create a SynchronizedDescriptiveStatistics instance and // use as any other DescriptiveStatistics instance DescriptiveStatistics stats = new SynchronizedDescriptiveStatistics();
                                    Compute statistics for multiple samples and overall statistics concurrently

                                    There are two ways to do this using AggregateSummaryStatistics. The first is to use an AggregateSummaryStatistics instance to accumulate overall statistics contributed by SummaryStatistics instances created using AggregateSummaryStatistics.createContributingStatistics(): // Create a AggregateSummaryStatistics instance to accumulate the overall statistics // and AggregatingSummaryStatistics for the subsamples AggregateSummaryStatistics aggregate = new AggregateSummaryStatistics(); SummaryStatistics setOneStats = aggregate.createContributingStatistics(); SummaryStatistics setTwoStats = aggregate.createContributingStatistics(); // Add values to the subsample aggregates setOneStats.addValue(2); setOneStats.addValue(3); setTwoStats.addValue(2); setTwoStats.addValue(4); ... // Full sample data is reported by the aggregate double totalSampleSum = aggregate.getSum(); The above approach has the disadvantages that the addValue calls must be synchronized on the SummaryStatistics instance maintained by the aggregate and each value addition updates the aggregate as well as the subsample. For applications that can wait to do the aggregation until all values have been added, a static aggregate method is available, as shown in the following example. This method should be used when aggregation needs to be done across threads. // Create SummaryStatistics instances for the subsample data SummaryStatistics setOneStats = new SummaryStatistics(); SummaryStatistics setTwoStats = new SummaryStatistics(); // Add values to the subsample SummaryStatistics instances setOneStats.addValue(2); setOneStats.addValue(3); setTwoStats.addValue(2); setTwoStats.addValue(4); ... // Aggregate the subsample statistics Collection<SummaryStatistics> aggregate = new ArrayList<SummaryStatistics>(); aggregate.add(setOneStats); aggregate.add(setTwoStats); StatisticalSummary aggregatedStats = AggregateSummaryStatistics.aggregate(aggregate); // Full sample data is reported by aggregatedStats double totalSampleSum = aggregatedStats.getSum();

                                    Frequency provides a simple interface for maintaining counts and percentages of discrete values.

                                    Strings, integers, longs and chars are all supported as value types, as well as instances of any class that implements Comparable. The ordering of values used in computing cumulative frequencies is by default the natural ordering, but this can be overriden by supplying a Comparator to the constructor. Adding values that are not comparable to those that have already been added results in an IllegalArgumentException.

                                    Here are some examples.

                                    Compute a frequency distribution based on integer values


                                    Mixing integers, longs, Integers and Longs: Frequency f = new Frequency(); f.addValue(1); f.addValue(new Integer(1)); f.addValue(new Long(1)); f.addValue(2); f.addValue(new Integer(-1)); System.out.prinltn(f.getCount(1)); // displays 3 System.out.println(f.getCumPct(0)); // displays 0.2 System.out.println(f.getPct(new Integer(1))); // displays 0.6 System.out.println(f.getCumPct(-2)); // displays 0 System.out.println(f.getCumPct(10)); // displays 1
                                    Count string frequencies


                                    Using case-sensitive comparison, alpha sort order (natural comparator): Frequency f = new Frequency(); f.addValue("one"); f.addValue("One"); f.addValue("oNe"); f.addValue("Z"); System.out.println(f.getCount("one")); // displays 1 System.out.println(f.getCumPct("Z")); // displays 0.5 System.out.println(f.getCumPct("Ot")); // displays 0.25
                                    Using case-insensitive comparator: Frequency f = new Frequency(String.CASE_INSENSITIVE_ORDER); f.addValue("one"); f.addValue("One"); f.addValue("oNe"); f.addValue("Z"); System.out.println(f.getCount("one")); // displays 3 System.out.println(f.getCumPct("z")); // displays 1

                                    SimpleRegression provides ordinary least squares regression with one independent variable estimating the linear model:

                                    y = intercept + slope * x

                                    or

                                    y = slope * x

                                    Standard errors for intercept and slope are available as well as ANOVA, r-square and Pearson's r statistics.

                                    Observations (x,y pairs) can be added to the model one at a time or they can be provided in a 2-dimensional array. The observations are not stored in memory, so there is no limit to the number of observations that can be added to the model.

                                    Usage Notes:

                                    • When there are fewer than two observations in the model, or when there is no variation in the x values (i.e. all x values are the same) all statistics return NaN. At least two observations with different x coordinates are requred to estimate a bivariate regression model.
                                    • getters for the statistics always compute values based on the current set of observations -- i.e., you can get statistics, then add more data and get updated statistics without using a new instance. There is no "compute" method that updates all statistics. Each of the getters performs the necessary computations to return the requested statistic.
                                    • The intercept term may be suppressed by passing false to the SimpleRegression(boolean) constructor. When the hasIntercept property is false, the model is estimated without a constant term and getIntercept() returns 0.

                                    Implementation Notes:

                                    • As observations are added to the model, the sum of x values, y values, cross products (x times y), and squared deviations of x and y from their respective means are updated using updating formulas defined in "Algorithms for Computing the Sample Variance: Analysis and Recommendations", Chan, T.F., Golub, G.H., and LeVeque, R.J. 1983, American Statistician, vol. 37, pp. 242-247, referenced in Weisberg, S. "Applied Linear Regression". 2nd Ed. 1985. All regression statistics are computed from these sums.
                                    • Inference statistics (confidence intervals, parameter significance levels) are based on on the assumption that the observations included in the model are drawn from a Bivariate Normal Distribution

                                    Here are some examples.

                                    Estimate a model based on observations added one at a time
                                    Instantiate a regression instance and add data points regression = new SimpleRegression(); regression.addData(1d, 2d); // At this point, with only one observation, // all regression statistics will return NaN regression.addData(3d, 3d); // With only two observations, // slope and intercept can be computed // but inference statistics will return NaN regression.addData(3d, 3d); // Now all statistics are defined.
                                    Compute some statistics based on observations added so far System.out.println(regression.getIntercept()); // displays intercept of regression line System.out.println(regression.getSlope()); // displays slope of regression line System.out.println(regression.getSlopeStdErr()); // displays slope standard error
                                    Use the regression model to predict the y value for a new x value System.out.println(regression.predict(1.5d) // displays predicted y value for x = 1.5 More data points can be added and subsequent getXxx calls will incorporate additional data in statistics.


                                    Estimate a model from a double[][] array of data points
                                    Instantiate a regression object and load dataset double[][] data = { { 1, 3 }, {2, 5 }, {3, 7 }, {4, 14 }, {5, 11 }}; SimpleRegression regression = new SimpleRegression(); regression.addData(data);
                                    Estimate regression model based on data System.out.println(regression.getIntercept()); // displays intercept of regression line System.out.println(regression.getSlope()); // displays slope of regression line System.out.println(regression.getSlopeStdErr()); // displays slope standard error More data points -- even another double[][] array -- can be added and subsequent getXxx calls will incorporate additional data in statistics.


                                    Estimate a model from a double[][] array of data points, excluding the intercept
                                    Instantiate a regression object and load dataset double[][] data = { { 1, 3 }, {2, 5 }, {3, 7 }, {4, 14 }, {5, 11 }}; SimpleRegression regression = new SimpleRegression(false); //the argument, false, tells the class not to include a constant regression.addData(data);
                                    Estimate regression model based on data System.out.println(regression.getIntercept()); // displays intercept of regression line, since we have constrained the constant, 0.0 is returned System.out.println(regression.getSlope()); // displays slope of regression line System.out.println(regression.getSlopeStdErr()); // displays slope standard error System.out.println(regression.getInterceptStdErr() ); // will return Double.NaN, since we constrained the parameter to zero Caution must be exercised when interpreting the slope when no constant is being estimated. The slope may be biased.

                                    OLSMultipleLinearRegression and GLSMultipleLinearRegression provide least squares regression to fit the linear model:

                                    Y=X*b+u

                                    where Y is an n-vector regressand, X is a [n,k] matrix whose k columns are called regressors, b is k-vector of regression parameters and u is an n-vector of error terms or residuals.

                                    OLSMultipleLinearRegression provides Ordinary Least Squares Regression, and GLSMultipleLinearRegression implements Generalized Least Squares. See the javadoc for these classes for details on the algorithms and forumlas used.

                                    Data for OLS models can be loaded in a single double[] array, consisting of concatenated rows of data, each containing the regressand (Y) value, followed by regressor values; or using a double[][] array with rows corresponding to observations. GLS models also require a double[][] array representing the covariance matrix of the error terms. See AbstractMultipleLinearRegression#newSampleData(double[],int,int), OLSMultipleLinearRegression#newSampleData(double[], double[][]) and GLSMultipleLinearRegression#newSampleData(double[],double[][],double[][]) for details.

                                    Usage Notes:

                                    • Data are validated when invoking any of the newSample, newX, newY or newCovariance methods and IllegalArgumentException is thrown when input data arrays do not have matching dimensions or do not contain sufficient data to estimate the model.
                                    • By default, regression models are estimated with intercept terms. In the notation above, this implies that the X matrix contains an initial row identically equal to 1. X data supplied to the newX or newSample methods should not include this column - the data loading methods will create it automatically. To estimate a model without an intercept term, set the noIntercept property to true.

                                    Here are some examples.

                                    OLS regression


                                    Instantiate an OLS regression object and load a dataset: OLSMultipleLinearRegression regression = new OLSMultipleLinearRegression(); double[] y = new double[]{11.0, 12.0, 13.0, 14.0, 15.0, 16.0}; double[] x = new double[6][]; x[0] = new double[]{0, 0, 0, 0, 0}; x[1] = new double[]{2.0, 0, 0, 0, 0}; x[2] = new double[]{0, 3.0, 0, 0, 0}; x[3] = new double[]{0, 0, 4.0, 0, 0}; x[4] = new double[]{0, 0, 0, 5.0, 0}; x[5] = new double[]{0, 0, 0, 0, 6.0}; regression.newSample(y, x);
                                    Get regression parameters and diagnostics: double[] beta = regression.estimateRegressionParameters(); double[] residuals = regression.estimateResiduals(); double[][] parametersVariance = regression.estimateRegressionParametersVariance(); double regressandVariance = regression.estimateRegressandVariance(); double rSquared = regression.calculateRSquared(); double sigma = regression.estimateRegressionStandardError();
                                    GLS regression


                                    Instantiate a GLS regression object and load a dataset: GLSMultipleLinearRegression regression = new GLSMultipleLinearRegression(); double[] y = new double[]{11.0, 12.0, 13.0, 14.0, 15.0, 16.0}; double[] x = new double[6][]; x[0] = new double[]{0, 0, 0, 0, 0}; x[1] = new double[]{2.0, 0, 0, 0, 0}; x[2] = new double[]{0, 3.0, 0, 0, 0}; x[3] = new double[]{0, 0, 4.0, 0, 0}; x[4] = new double[]{0, 0, 0, 5.0, 0}; x[5] = new double[]{0, 0, 0, 0, 6.0}; double[][] omega = new double[6][]; omega[0] = new double[]{1.1, 0, 0, 0, 0, 0}; omega[1] = new double[]{0, 2.2, 0, 0, 0, 0}; omega[2] = new double[]{0, 0, 3.3, 0, 0, 0}; omega[3] = new double[]{0, 0, 0, 4.4, 0, 0}; omega[4] = new double[]{0, 0, 0, 0, 5.5, 0}; omega[5] = new double[]{0, 0, 0, 0, 0, 6.6}; regression.newSampleData(y, x, omega);

                                    Some statistical algorithms require that input data be replaced by ranks. The org.apache.commons.math3.stat.ranking package provides rank transformation. RankingAlgorithm defines the interface for ranking. NaturalRanking provides an implementation that has two configuration options.

                                    • Ties strategy deterimines how ties in the source data are handled by the ranking
                                    • NaN strategy determines how NaN values in the source data are handled.

                                    Examples: NaturalRanking ranking = new NaturalRanking(NaNStrategy.MINIMAL, TiesStrategy.MAXIMUM); double[] data = { 20, 17, 30, 42.3, 17, 50, Double.NaN, Double.NEGATIVE_INFINITY, 17 }; double[] ranks = ranking.rank(exampleData); results in ranks containing {6, 5, 7, 8, 5, 9, 2, 2, 5}. new NaturalRanking(NaNStrategy.REMOVED,TiesStrategy.SEQUENTIAL).rank(exampleData); returns {5, 2, 6, 7, 3, 8, 1, 4}.

                                    The default NaNStrategy is NaNStrategy.MAXIMAL. This makes NaN values larger than any other value (including Double.POSITIVE_INFINITY). The default TiesStrategy is TiesStrategy.AVERAGE, which assigns tied values the average of the ranks applicable to the sequence of ties. See the NaturalRanking for more examples and TiesStrategy and NaNStrategy for details on these configuration options.

                                    The org.apache.commons.math3.stat.correlation package computes covariances and correlations for pairs of arrays or columns of a matrix. Covariance computes covariances, PearsonsCorrelation provides Pearson's Product-Moment correlation coefficients and SpearmansCorrelation computes Spearman's rank correlation.

                                    Implementation Notes

                                    • Unbiased covariances are given by the formula

                                      cov(X, Y) = sum [(xi - E(X))(yi - E(Y))] / (n - 1) where E(X) is the mean of X and E(Y) is the mean of the Y values. Non-bias-corrected estimates use n in place of n - 1. Whether or not covariances are bias-corrected is determined by the optional parameter, "biasCorrected," which defaults to true.
                                    • PearsonsCorrelation computes correlations defined by the formula

                                      cor(X, Y) = sum[(xi - E(X))(yi - E(Y))] / [(n - 1)s(X)s(Y)]
                                      where E(X) and E(Y) are means of X and Y and s(X), s(Y) are standard deviations.
                                    • SpearmansCorrelation applies a rank transformation to the input data and computes Pearson's correlation on the ranked data. The ranking algorithm is configurable. By default, NaturalRanking with default strategies for handling ties and NaN values is used.

                                    Examples:

                                    Covariance of 2 arrays


                                    To compute the unbiased covariance between 2 double arrays, x and y, use: new Covariance().covariance(x, y) For non-bias-corrected covariances, use covariance(x, y, false)


                                    Covariance matrix


                                    A covariance matrix over the columns of a source matrix data can be computed using new Covariance().computeCovarianceMatrix(data) The i-jth entry of the returned matrix is the unbiased covariance of the ith and jth columns of data. As above, to get non-bias-corrected covariances, use computeCovarianceMatrix(data, false)


                                    Pearson's correlation of 2 arrays


                                    To compute the Pearson's product-moment correlation between two double arrays x and y, use: new PearsonsCorrelation().correlation(x, y)


                                    Pearson's correlation matrix


                                    A (Pearson's) correlation matrix over the columns of a source matrix data can be computed using new PearsonsCorrelation().computeCorrelationMatrix(data) The i-jth entry of the returned matrix is the Pearson's product-moment correlation between the ith and jth columns of data.


                                    Pearson's correlation significance and standard errors


                                    To compute standard errors and/or significances of correlation coefficients associated with Pearson's correlation coefficients, start by creating a PearsonsCorrelation instance PearsonsCorrelation correlation = new PearsonsCorrelation(data); where data is either a rectangular array or a RealMatrix. Then the matrix of standard errors is correlation.getCorrelationStandardErrors(); The formula used to compute the standard error is
                                    SEr = ((1 - r2) / (n - 2))1/2
                                    where r is the estimated correlation coefficient and n is the number of observations in the source dataset.

                                    p-values for the (2-sided) null hypotheses that elements of a correlation matrix are zero populate the RealMatrix returned by correlation.getCorrelationPValues() getCorrelationPValues().getEntry(i,j) is the probability that a random variable distributed as tn-2 takes a value with absolute value greater than or equal to

                                    |rij|((n - 2) / (1 - rij2))1/2, where rij is the estimated correlation between the ith and jth columns of the source array or RealMatrix. This is sometimes referred to as the significance of the coefficient.

                                    For example, if data is a RealMatrix with 2 columns and 10 rows, then new PearsonsCorrelation(data).getCorrelationPValues().getEntry(0,1) is the significance of the Pearson's correlation coefficient between the two columns of data. If this value is less than .01, we can say that the correlation between the two columns of data is significant at the 99% level.


                                    Spearman's rank correlation coefficient


                                    To compute the Spearman's rank-moment correlation between two double arrays x and y: new SpearmansCorrelation().correlation(x, y) This is equivalent to RankingAlgorithm ranking = new NaturalRanking(); new PearsonsCorrelation().correlation(ranking.rank(x), ranking.rank(y))


                                    The org.apache.commons.math3.stat.inference package provides implementations for Student's t, Chi-Square, G Test, One-Way ANOVA, Mann-Whitney U and Wilcoxon signed rank test statistics as well as p-values associated with t-, Chi-Square, G, One-Way ANOVA, Mann-Whitney U and Wilcoxon signed rank tests. The respective test classes are TTest, ChiSquareTest, GTest, OneWayAnova, MannWhitneyUTest, and WilcoxonSignedRankTest. The TestUtils class provides static methods to get test instances or to compute test statistics directly. The examples below all use the static methods in TestUtils to execute tests. To get test object instances, either use e.g., TestUtils.getTTest() or use the implementation constructors directly, e.g. new TTest().

                                    Implementation Notes

                                    • Both one- and two-sample t-tests are supported. Two sample tests can be either paired or unpaired and the unpaired two-sample tests can be conducted under the assumption of equal subpopulation variances or without this assumption. When equal variances is assumed, a pooled variance estimate is used to compute the t-statistic and the degrees of freedom used in the t-test equals the sum of the sample sizes minus 2. When equal variances is not assumed, the t-statistic uses both sample variances and the Welch-Satterwaite approximation is used to compute the degrees of freedom. Methods to return t-statistics and p-values are provided in each case, as well as boolean-valued methods to perform fixed significance level tests. The names of methods or methods that assume equal subpopulation variances always start with "homoscedastic." Test or test-statistic methods that just start with "t" do not assume equal variances. See the examples below and the API documentation for more details.
                                    • The validity of the p-values returned by the t-test depends on the assumptions of the parametric t-test procedure, as discussed here
                                    • p-values returned by t-, chi-square and Anova tests are exact, based on numerical approximations to the t-, chi-square and F distributions in the distributions package.
                                    • The G test implementation provides two p-values: gTest(expected, observed), which is the tail probability beyond g(expected, observed) in the ChiSquare distribution with degrees of freedom one less than the common length of input arrays and gTestIntrinsic(expected, observed) which is the same tail probability computed using a ChiSquare distribution with one less degeree of freedom.
                                    • p-values returned by t-tests are for two-sided tests and the boolean-valued methods supporting fixed significance level tests assume that the hypotheses are two-sided. One sided tests can be performed by dividing returned p-values (resp. critical values) by 2.
                                    • Degrees of freedom for G- and chi-square tests are integral values, based on the number of observed or expected counts (number of observed counts - 1).

                                    Examples:

                                    One-sample t tests


                                    To compare the mean of a double[] array to a fixed value: double[] observed = {1d, 2d, 3d}; double mu = 2.5d; System.out.println(TestUtils.t(mu, observed)); The code above will display the t-statisitic associated with a one-sample t-test comparing the mean of the observed values against mu.
                                    To compare the mean of a dataset described by a StatisticalSummary to a fixed value: double[] observed ={1d, 2d, 3d}; double mu = 2.5d; SummaryStatistics sampleStats = new SummaryStatistics(); for (int i = 0; i < observed.length; i++) { sampleStats.addValue(observed[i]); } System.out.println(TestUtils.t(mu, observed));
                                    To compute the p-value associated with the null hypothesis that the mean of a set of values equals a point estimate, against the two-sided alternative that the mean is different from the target value: double[] observed = {1d, 2d, 3d}; double mu = 2.5d; System.out.println(TestUtils.tTest(mu, observed)); The snippet above will display the p-value associated with the null hypothesis that the mean of the population from which the observed values are drawn equals mu.
                                    To perform the test using a fixed significance level, use: TestUtils.tTest(mu, observed, alpha); where 0 < alpha < 0.5 is the significance level of the test. The boolean value returned will be true iff the null hypothesis can be rejected with confidence 1 - alpha. To test, for example at the 95% level of confidence, use alpha = 0.05


                                    Two-Sample t-tests


                                    Example 1: Paired test evaluating the null hypothesis that the mean difference between corresponding (paired) elements of the double[] arrays sample1 and sample2 is zero.

                                    To compute the t-statistic: TestUtils.pairedT(sample1, sample2);

                                    To compute the p-value: TestUtils.pairedTTest(sample1, sample2);

                                    To perform a fixed significance level test with alpha = .05: TestUtils.pairedTTest(sample1, sample2, .05);

                                    The last example will return true iff the p-value returned by TestUtils.pairedTTest(sample1, sample2) is less than .05
                                    Example 2: unpaired, two-sided, two-sample t-test using StatisticalSummary instances, without assuming that subpopulation variances are equal.

                                    First create the StatisticalSummary instances. Both DescriptiveStatistics and SummaryStatistics implement this interface. Assume that summary1 and summary2 are SummaryStatistics instances, each of which has had at least 2 values added to the (virtual) dataset that it describes. The sample sizes do not have to be the same -- all that is required is that both samples have at least 2 elements.

                                    Note: The SummaryStatistics class does not store the dataset that it describes in memory, but it does compute all statistics necessary to perform t-tests, so this method can be used to conduct t-tests with very large samples. One-sample tests can also be performed this way. (See Descriptive statistics for details on the SummaryStatistics class.)

                                    To compute the t-statistic: TestUtils.t(summary1, summary2);

                                    To compute the p-value: TestUtils.tTest(sample1, sample2);

                                    To perform a fixed significance level test with alpha = .05: TestUtils.tTest(sample1, sample2, .05);

                                    In each case above, the test does not assume that the subpopulation variances are equal. To perform the tests under this assumption, replace "t" at the beginning of the method name with "homoscedasticT"



                                    Chi-square tests


                                    To compute a chi-square statistic measuring the agreement between a long[] array of observed counts and a double[] array of expected counts, use: long[] observed = {10, 9, 11}; double[] expected = {10.1, 9.8, 10.3}; System.out.println(TestUtils.chiSquare(expected, observed)); the value displayed will be sum((expected[i] - observed[i])^2 / expected[i])
                                    To get the p-value associated with the null hypothesis that observed conforms to expected use: TestUtils.chiSquareTest(expected, observed);
                                    To test the null hypothesis that observed conforms to expected with alpha siginficance level (equiv. 100 * (1-alpha)% confidence) where 0 < alpha < 1 use: TestUtils.chiSquareTest(expected, observed, alpha); The boolean value returned will be true iff the null hypothesis can be rejected with confidence 1 - alpha.
                                    To compute a chi-square statistic statistic associated with a chi-square test of independence based on a two-dimensional (long[][]) counts array viewed as a two-way table, use: TestUtils.chiSquareTest(counts); The rows of the 2-way table are count[0], ... , count[count.length - 1].

                                    The chi-square statistic returned is sum((counts[i][j] - expected[i][j])^2/expected[i][j]) where the sum is taken over all table entries and expected[i][j] is the product of the row and column sums at row i, column j divided by the total count.
                                    To compute the p-value associated with the null hypothesis that the classifications represented by the counts in the columns of the input 2-way table are independent of the rows, use: TestUtils.chiSquareTest(counts);
                                    To perform a chi-square test of independence with alpha siginficance level (equiv. 100 * (1-alpha)% confidence) where 0 < alpha < 1 use: TestUtils.chiSquareTest(counts, alpha); The boolean value returned will be true iff the null hypothesis can be rejected with confidence 1 - alpha.


                                    G tests


                                    G tests are an alternative to chi-square tests that are recommended when observed counts are small and / or incidence probabillities for some cells are small. See Ted Dunning's paper, Accurate Methods for the Statistics of Surprise and Coincidence for background and an empirical analysis showing now chi-square statistics can be misldeading in the presence of low incidence probabilities. This paper also derives the formulas used in computing G statistics and the root log likelihood ratio provided by the GTest class.
                                    To compute a G-test statistic measuring the agreement between a long[] array of observed counts and a double[] array of expected counts, use: double[] expected = new double[]{0.54d, 0.40d, 0.05d, 0.01d}; long[] observed = new long[]{70, 79, 3, 4}; System.out.println(TestUtils.g(expected, observed)); the value displayed will be 2 * sum(observed[i]) * log(observed[i]/expected[i])
                                    To get the p-value associated with the null hypothesis that observed conforms to expected use: TestUtils.gTest(expected, observed);
                                    To test the null hypothesis that observed conforms to expected with alpha siginficance level (equiv. 100 * (1-alpha)% confidence) where 0 < alpha < 1 use: TestUtils.gTest(expected, observed, alpha); The boolean value returned will be true iff the null hypothesis can be rejected with confidence 1 - alpha.
                                    To evaluate the hypothesis that two sets of counts come from the same underlying distribution, use long[] arrays for the counts and gDataSetsComparison for the test statistic long[] obs1 = new long[]{268, 199, 42}; long[] obs2 = new long[]{807, 759, 184}; System.out.println(TestUtils.gDataSetsComparison(obs1, obs2)); // G statistic System.out.println(TestUtils.gTestDataSetsComparison(obs1, obs2)); // p-value
                                    For 2 x 2 designs, the rootLogLikelihoodRaio method computes the signed root log likelihood ratio. For example, suppose that for two events A and B, the observed count of AB (both occurring) is 5, not A and B (B without A) is 1995, A not B is 0; and neither A nor B is 10000. Then new GTest().rootLogLikelihoodRatio(5, 1995, 0, 100000); returns the root log likelihood associated with the null hypothesis that A and B are independent.


                                    One-Way Anova tests


                                    double[] classA = {93.0, 103.0, 95.0, 101.0, 91.0, 105.0, 96.0, 94.0, 101.0 }; double[] classB = {99.0, 92.0, 102.0, 100.0, 102.0, 89.0 }; double[] classC = {110.0, 115.0, 111.0, 117.0, 128.0, 117.0 }; List classes = new ArrayList(); classes.add(classA); classes.add(classB); classes.add(classC); Then you can compute ANOVA F- or p-values associated with the null hypothesis that the class means are all the same using a OneWayAnova instance or TestUtils methods: double fStatistic = TestUtils.oneWayAnovaFValue(classes); // F-value double pValue = TestUtils.oneWayAnovaPValue(classes); // P-value To test perform a One-Way Anova test with signficance level set at 0.01 (so the test will, assuming assumptions are met, reject the null hypothesis incorrectly only about one in 100 times), use TestUtils.oneWayAnovaTest(classes, 0.01); // returns a boolean // true means reject null hypothesis

                                    commons-math3-3.2-src/src/site/xdoc/userguide/analysis.xml100644 1750 1750 131373 12126627721 22470 0ustarlucluc 0 0 The Commons Math User Guide - Numerical Analysis

                                    The analysis package is the parent package for algorithms dealing with real-valued functions of one real variable. It contains dedicated sub-packages providing numerical root-finding, integration, interpolation and differentiation. It also contains a polynomials sub-package that considers polynomials with real coefficients as differentiable real functions.

                                    Functions interfaces are intended to be implemented by user code to represent their domain problems. The algorithms provided by the library will then operate on these function to find their roots, or integrate them, or ... Functions can be multivariate or univariate, real vectorial or matrix valued, and they can be differentiable or not.

                                    For user-defined functions, when the method encounters an error during evaluation, users must use their own unchecked exceptions. The following example shows the recommended way to do that, using root solving as the example (the same construct should be used for ODE integrators or for optimizations).

                                    private static class LocalException extends RuntimeException { // the x value that caused the problem private final double x; public LocalException(double x) { this.x = x; } public double getX() { return x; } } private static class MyFunction implements UnivariateFunction { public double value(double x) { double y = hugeFormula(x); if (somethingBadHappens) { throw new LocalException(x); } return y; } } public void compute() { try { solver.solve(maxEval, new MyFunction(a, b, c), min, max); } catch (LocalException le) { // retrieve the x value } }

                                    As shown in this example the exception is really something local to user code and there is a guarantee Apache Commons Math will not mess with it. The user is safe.

                                    UnivariateSolver, UnivariateDifferentiableSolver and PolynomialSolver provide means to find roots of univariate real-valued functions, differentiable univariate real-valued functions, and polynomial functions respectively. A root is the value where the function takes the value 0. Commons-Math includes implementations of the several root-finding algorithms:

                                    Root solvers
                                    NameFunction typeConvergenceNeeds initial bracketingBracket side selection
                                    Bisection univariate real-valued functions linear, guaranteed yes yes
                                    Brent-Dekker univariate real-valued functions super-linear, guaranteed yes no
                                    bracketing nth order Brent univariate real-valued functions variable order, guaranteed yes yes
                                    Illinois Method univariate real-valued functions super-linear, guaranteed yes yes
                                    Laguerre's Method polynomial functions cubic for simple root, linear for multiple root yes no
                                    Muller's Method using bracketing to deal with real-valued functions univariate real-valued functions quadratic close to roots yes no
                                    Muller's Method using modulus to deal with real-valued functions univariate real-valued functions quadratic close to root yes no
                                    Newton-Raphson's Method differentiable univariate real-valued functions quadratic, non-guaranteed no no
                                    Pegasus Method univariate real-valued functions super-linear, guaranteed yes yes
                                    Regula Falsi (false position) Method univariate real-valued functions linear, guaranteed yes yes
                                    Ridder's Method univariate real-valued functions super-linear yes no
                                    Secant Method univariate real-valued functions super-linear, non-guaranteed yes no

                                    Some algorithms require that the initial search interval brackets the root (i.e. the function values at interval end points have opposite signs). Some algorithms preserve bracketing throughout computation and allow user to specify which side of the convergence interval to select as the root. It is also possible to force a side selection after a root has been found even for algorithms that do not provide this feature by themselves. This is useful for example in sequential search, for which a new search interval is started after a root has been found in order to find the next root. In this case, user must select a side to ensure his loop is not stuck on one root and always return the same solution without making any progress.

                                    There are numerous non-obvious traps and pitfalls in root finding. First, the usual disclaimers due to the way real world computers calculate values apply. If the computation of the function provides numerical instabilities, for example due to bit cancellation, the root finding algorithms may behave badly and fail to converge or even return bogus values. There will not necessarily be an indication that the computed root is way off the true value. Secondly, the root finding problem itself may be inherently ill-conditioned. There is a "domain of indeterminacy", the interval for which the function has near zero absolute values around the true root, which may be large. Even worse, small problems like roundoff error may cause the function value to "numerically oscillate" between negative and positive values. This may again result in roots way off the true value, without indication. There is not much a generic algorithm can do if ill-conditioned problems are met. A way around this is to transform the problem in order to get a better conditioned function. Proper selection of a root-finding algorithm and its configuration parameters requires knowledge of the analytical properties of the function under analysis and numerical analysis techniques. Users are encouraged to consult a numerical analysis text (or a numerical analyst) when selecting and configuring a solver.

                                    In order to use the root-finding features, first a solver object must be created by calling its constructor, often providing relative and absolute accuracy. Using a solver object, roots of functions are easily found using the solve methods. These methods takes a maximum iteration count maxEval, a function f, and either two domain values, min and max, or a startValue as parameters. If the maximal number of iterations count is exceeded, non-convergence is assumed and a ConvergenceException exception is thrown. A suggested value is 100, which should be plenty, given that a bisection algorithm can't get any more accurate after 52 iterations because of the number of mantissa bits in a double precision floating point number. If a number of ill-conditioned problems is to be solved, this number can be decreased in order to avoid wasting time. Bracketed solvers also take an allowed solution enum parameter to specify which side of the final convergence interval should be selected as the root. It can be ANY_SIDE, LEFT_SIDE, RIGHT_SIDE, BELOW_SIDE or ABOVE_SIDE. Left and right are used to specify the root along the function parameter axis while below and above refer to the function value axis. The solve methods compute a value c such that:

                                    • f(c) = 0.0 (see "function value accuracy")
                                    • min <= c <= max (except for the secant method, which may find a solution outside the interval)

                                    Typical usage:

                                    UnivariateFunction function = // some user defined function object final double relativeAccuracy = 1.0e-12; final double absoluteAccuracy = 1.0e-8; final int maxOrder = 5; UnivariateSolver solver = new BracketingNthOrderBrentSolver(relativeAccuracy, absoluteAccuracy, maxOrder); double c = solver.solve(100, function, 1.0, 5.0, AllowedSolution.LEFT_SIDE);

                                    Force bracketing, by refining a base solution found by a non-bracketing solver:

                                    UnivariateFunction function = // some user defined function object final double relativeAccuracy = 1.0e-12; final double absoluteAccuracy = 1.0e-8; UnivariateSolver nonBracketing = new BrentSolver(relativeAccuracy, absoluteAccuracy); double baseRoot = nonBracketing.solve(100, function, 1.0, 5.0); double c = UnivariateSolverUtils.forceSide(100, function, new PegasusSolver(relativeAccuracy, absoluteAccuracy), baseRoot, 1.0, 5.0, AllowedSolution.LEFT_SIDE);

                                    The BrentSolver uses the Brent-Dekker algorithm which is fast and robust. If there are multiple roots in the interval, or there is a large domain of indeterminacy, the algorithm will converge to a random root in the interval without indication that there are problems. Interestingly, the examined text book implementations all disagree in details of the convergence criteria. Also each implementation had problems for one of the test cases, so the expressions had to be fudged further. Don't expect to get exactly the same root values as for other implementations of this algorithm.

                                    The BracketingNthOrderBrentSolver uses an extension of the Brent-Dekker algorithm which uses inverse nth order polynomial interpolation instead of inverse quadratic interpolation, and which allows selection of the side of the convergence interval for result bracketing. This is now the recommended algorithm for most users since it has the largest order, doesn't require derivatives, has guaranteed convergence and allows result bracket selection.

                                    The SecantSolver uses a straightforward secant algorithm which does not bracket the search and therefore does not guarantee convergence. It may be faster than Brent on some well-behaved functions.

                                    The RegulaFalsiSolver is variation of secant preserving bracketing, but then it may be slow, as one end point of the search interval will become fixed after and only the other end point will converge to the root, hence resulting in a search interval size that does not decrease to zero.

                                    The IllinoisSolver and PegasusSolver are well-known variations of regula falsi that fix the problem of stuck end points by slightly weighting one endpoint to balance the interval at next iteration. Pegasus is often faster than Illinois. Pegasus may be the algorithm of choice for selecting a specific side of the convergence interval.

                                    The BisectionSolver is included for completeness and for establishing a fall back in cases of emergency. The algorithm is simple, most likely bug free and guaranteed to converge even in very adverse circumstances which might cause other algorithms to malfunction. The drawback is of course that it is also guaranteed to be slow.

                                    The UnivariateSolver interface exposes many properties to control the convergence of a solver. The accuracy properties are set at solver instance creation and cannot be changed afterwards, there are only getters to retriveve their values, no setters are available.
                                    PropertyPurpose
                                    Absolute accuracy The Absolute Accuracy is (estimated) maximal difference between the computed root and the true root of the function. This is what most people think of as "accuracy" intuitively. The default value is chosen as a sane value for most real world problems, for roots in the range from -100 to +100. For accurate computation of roots near zero, in the range form -0.0001 to +0.0001, the value may be decreased. For computing roots much larger in absolute value than 100, the default absolute accuracy may never be reached because the given relative accuracy is reached first.
                                    Relative accuracy The Relative Accuracy is the maximal difference between the computed root and the true root, divided by the maximum of the absolute values of the numbers. This accuracy measurement is better suited for numerical calculations with computers, due to the way floating point numbers are represented. The default value is chosen so that algorithms will get a result even for roots with large absolute values, even while it may be impossible to reach the given absolute accuracy.
                                    Function value accuracy This value is used by some algorithms in order to prevent numerical instabilities. If the function is evaluated to an absolute value smaller than the Function Value Accuracy, the algorithms assume they hit a root and return the value immediately. The default value is a "very small value". If the goal is to get a near zero function value rather than an accurate root, computation may be sped up by setting this value appropriately.

                                    A UnivariateInterpolator is used to find a univariate real-valued function f which for a given set of ordered pairs (xi,yi) yields f(xi)=yi to the best accuracy possible. The result is provided as an object implementing the UnivariateFunction interface. It can therefore be evaluated at any point, including point not belonging to the original set. Currently, only an interpolator for generating natural cubic splines and a polynomial interpolator are available. There is no interpolator factory, mainly because the interpolation algorithm is more determined by the kind of the interpolated function rather than the set of points to interpolate. There aren't currently any accuracy controls either, as interpolation accuracy is in general determined by the algorithm.

                                    Typical usage:

                                    double x[] = { 0.0, 1.0, 2.0 }; double y[] = { 1.0, -1.0, 2.0); UnivariateInterpolator interpolator = new SplineInterpolator(); UnivariateFunction function = interpolator.interpolate(x, y); double interpolationX = 0.5; double interpolatedY = function.evaluate(x); System.out println("f(" + interpolationX + ") = " + interpolatedY);

                                    A natural cubic spline is a function consisting of a polynomial of third degree for each subinterval determined by the x-coordinates of the interpolated points. A function interpolating N value pairs consists of N-1 polynomials. The function is continuous, smooth and can be differentiated twice. The second derivative is continuous but not smooth. The x values passed to the interpolator must be ordered in ascending order. It is not valid to evaluate the function for values outside the range x0..xN.

                                    The polynomial function returned by the Neville's algorithm is a single polynomial guaranteed to pass exactly through the interpolation points. The degree of the polynomial is the number of points minus 1 (i.e. the interpolation polynomial for a three points set will be a quadratic polynomial). Despite the fact the interpolating polynomials is a perfect approximation of a function at interpolation points, it may be a loose approximation between the points. Due to Runge's phenomenom the error can get worse as the degree of the polynomial increases, so adding more points does not always lead to a better interpolation.

                                    Loess (or Lowess) interpolation is a robust interpolation useful for smoothing univariate scaterplots. It has been described by William Cleveland in his 1979 seminal paper Robust Locally Weighted Regression and Smoothing Scatterplots. This kind of interpolation is computationally intensive but robust.

                                    Microsphere interpolation is a robust multidimensional interpolation algorithm. It has been described in William Dudziak's MS thesis.

                                    Hermite interpolation is an interpolation method that can use derivatives in addition to function values at sample points. The HermiteInterpolator class implements this method for vector-valued functions. The sampling points can have any spacing (there are no requirements for a regular grid) and some points may provide derivatives while others don't provide them (or provide derivatives to a smaller order). Points are added one at a time, as shown in the following example:

                                    HermiteInterpolator interpolator = new HermiteInterpolator; // at x = 0, we provide both value and first derivative interpolator.addSamplePoint(0.0, new double[] { 1.0 }, new double[] { 2.0 }); // at x = 1, we provide only function value interpolator.addSamplePoint(1.0, new double[] { 4.0 }); // at x = 2, we provide both value and first derivative interpolator.addSamplePoint(2.0, new double[] { 5.0 }, new double[] { 2.0 }); // should print "value at x = 0.5: 2.5625" System.out.println("value at x = 0.5: " + interpolator.value(0.5)[0]); // should print "derivative at x = 0.5: 3.5" System.out.println("derivative at x = 0.5: " + interpolator.derivative(0.5)[0]); // should print "interpolation polynomial: 1 + 2 x + 4 x^2 - 4 x^3 + x^4" System.out.println("interpolation polynomial: " + interpolator.getPolynomials()[0]);

                                    A BivariateGridInterpolator is used to find a bivariate real-valued function f which for a given set of tuples (xi,yj,fij) yields f(xi,yj)=fij to the best accuracy possible. The result is provided as an object implementing the BivariateFunction interface. It can therefore be evaluated at any point, including a point not belonging to the original set. The arrays xi and yj must be sorted in increasing order in order to define a two-dimensional grid.

                                    In bicubic interpolation, the interpolation function is a 3rd-degree polynomial of two variables. The coefficients are computed from the function values sampled on a grid, as well as the values of the partial derivatives of the function at those grid points. From two-dimensional data sampled on a grid, the BicubicSplineInterpolator computes a bicubic interpolating function. Prior to computing an interpolating function, the SmoothingPolynomialBicubicSplineInterpolator class performs smoothing of the data by computing the polynomial that best fits each of the one-dimensional curves along each of the coordinate axes.

                                    A TrivariateGridInterpolator is used to find a trivariate real-valued function f which for a given set of tuples (xi,yj,zk, fijk) yields f(xi,yj,zk)=fijk to the best accuracy possible. The result is provided as an object implementing the TrivariateFunction interface. It can therefore be evaluated at any point, including a point not belonging to the original set. The arrays xi, yj and zk must be sorted in increasing order in order to define a three-dimensional grid.

                                    In tricubic interpolation, the interpolation function is a 3rd-degree polynomial of three variables. The coefficients are computed from the function values sampled on a grid, as well as the values of the partial derivatives of the function at those grid points. From three-dimensional data sampled on a grid, the TricubicSplineInterpolator computes a tricubic interpolating function.

                                    A UnivariateIntegrator provides the means to numerically integrate univariate real-valued functions. Commons-Math includes implementations of the following integration algorithms:

                                    The org.apache.commons.math3.analysis.polynomials package provides real coefficients polynomials.

                                    The PolynomialFunction class is the most general one, using traditional coefficients arrays. The PolynomialsUtils utility class provides static factory methods to build Chebyshev, Hermite, Jacobi, Laguerre and Legendre polynomials. Coefficients are computed using exact fractions so these factory methods can build polynomials up to any degree.

                                    The org.apache.commons.math3.analysis.differentiation package provides a general-purpose differentiation framework.

                                    The core class is DerivativeStructure which holds the value and the differentials of a function. This class handles some arbitrary number of free parameters and arbitrary derivation order. It is used both as the input and the output type for the UnivariateDifferentiableFunction interface. Any differentiable function should implement this interface.

                                    The main idea behind the DerivativeStructure class is that it can be used almost as a number (i.e. it can be added, multiplied, its square root can be extracted or its cosine computed... However, in addition to computed the value itself when doing these computations, the partial derivatives are also computed alongside. This is an extension of what is sometimes called Rall's numbers. This extension is described in Dan Kalman's paper Doubly Recursive Multivariate Automatic Differentiation, Mathematics Magazine, vol. 75, no. 3, June 2002. Rall's numbers only hold the first derivative with respect to one free parameter whereas Dan Kalman's derivative structures hold all partial derivatives up to any specified order, with respect to any number of free parameters. Rall's numbers therefore can be seen as derivative structures for order one derivative and one free parameter, and primitive real numbers can be seen as derivative structures with zero order derivative and no free parameters.

                                    The workflow of computation of a derivatives of an expression y=f(x) is the following one. First we configure an input parameter x of type DerivativeStructure so it will drive the function to compute all derivatives up to order 3 for example. Then we compute y=f(x) normally by passing this parameter to the f function.At the end, we extract from y the value and the derivatives we want. As we have specified 3rd order when we built x, we can retrieve the derivatives up to 3rd order from y. The following example shows that (the 0 parameter in the DerivativeStructure constructor will be explained in the next paragraph):

                                    int params = 1; int order = 3; double xRealValue = 2.5; DerivativeStructure x = new DerivativeStructure(params, order, 0, xRealValue); DerivativeStructure y = f(x); System.out.println("y = " + y.getValue(); System.out.println("y' = " + y.getPartialDerivative(1); System.out.println("y'' = " + y.getPartialDerivative(2); System.out.println("y''' = " + y.getPartialDerivative(3);

                                    In fact, there are no notions of variables in the framework, so neither x nor y are considered to be variables per se. They are both considered to be functions and to depend on implicit free parameters which are represented only by indices in the framework. The x instance above is there considered by the framework to be a function of free parameter p0 at index 0, and as y is computed from x it is the result of a functions composition and is therefore also a function of this p0 free parameter. The p0 is not represented by itself, it is simply defined implicitely by the 0 index above. This index is the third argument in the constructor of the x instance. What this constructor means is that we built x as a function that depends on one free parameter only (first constructor argument set to 1), that can be differentiated up to order 3 (second constructor argument set to 3), and which correspond to an identity function with respect to implicit free parameter number 0 (third constructor argument set to 0), with current value equal to 2.5 (fourth constructor argument set to 2.5). This specific constructor defines identity functions, and identity functions are the trick we use to represent variables (there are of course other constructors, for example to build constants or functions from all their derivatives if they are known beforehand). From the user point of view, the x instance can be seen as the x variable, but it is really the identity function applied to free parameter number 0. As the identity function, it has the same value as its parameter, its first derivative is 1.0 with respect to this free parameter, and all its higher order derivatives are 0.0. This can be checked by calling the getValue() or getPartialDerivative() methods on x.

                                    When we compute y from this setting, what we really do is chain f after the identity function, so the net result is that the derivatives are computed with respect to the indexed free parameters (i.e. only free parameter number 0 here since there is only one free parameter) of the identity function x. Going one step further, if we compute z = g(y), we will also compute z as a function of the initial free parameter. The very important consequence is that if we call z.getPartialDerivative(1), we will not get the first derivative of g with respect to y, but with respect to the free parameter p0: the derivatives of g and f will be chained together automatically, without user intervention.

                                    This design choice is a very classical one in many algorithmic differentiation frameworks, either based on operator overloading (like the one we implemented here) or based on code generation. It implies the user has to bootstrap the system by providing initial derivatives, and this is essentially done by setting up identity function, i.e. functions that represent the variables themselves and have only unit first derivative.

                                    This design also allow a very interesting feature which can be explained with the following example. Suppose we have a two arguments function f and a one argument function g. If we compute g(f(x, y)) with x and y be two variables, we want to be able to compute the partial derivatives dg/dx, dg/dy, d2g/dx2 d2g/dxdy d2g/dy2. This does make sense since we combined the two functions, and it does make sense despite g is a one argument function only. In order to do this, we simply set up x as an identity function of an implicit free parameter p0 and y as an identity function of a different implicit free parameter p1 and compute everything directly. In order to be able to combine everything, however, both x and y must be built with the appropriate dimensions, so they will both be declared to handle two free parameters, but x will depend only on parameter 0 while y will depend on parameter 1. Here is how we do this (note that getPartialDerivative is a variable arguments method which take as arguments the derivation order with respect to all free parameters, i.e. the first argument is derivation order with respect to free parameter 0 and the second argument is derivation order with respect to free parameter 1):

                                    int params = 2; int order = 2; double xRealValue = 2.5; double yRealValue = -1.3; DerivativeStructure x = new DerivativeStructure(params, order, 0, xRealValue); DerivativeStructure y = new DerivativeStructure(params, order, 1, yRealValue); DerivativeStructure f = DerivativeStructure.hypot(x, y); DerivativeStructure g = f.log(); System.out.println("g = " + g.getValue(); System.out.println("dg/dx = " + g.getPartialDerivative(1, 0); System.out.println("dg/dy = " + g.getPartialDerivative(0, 1); System.out.println("d2g/dx2 = " + g.getPartialDerivative(2, 0); System.out.println("d2g/dxdy = " + g.getPartialDerivative(1, 1); System.out.println("d2g/dy2 = " + g.getPartialDerivative(0, 2);

                                    There are several ways a user can create an implementation of the UnivariateDifferentiableFunction interface. The first method is to simply write it directly using the appropriate methods from DerivativeStructure to compute addition, subtraction, sine, cosine... This is often quite straigthforward and there is no need to remember the rules for differentiation: the user code only represent the function itself, the differentials will be computed automatically under the hood. The second method is to write a classical UnivariateFunction and to pass it to an existing implementation of the UnivariateFunctionDifferentiator interface to retrieve a differentiated version of the same function. The first method is more suited to small functions for which user already control all the underlying code. The second method is more suited to either large functions that would be cumbersome to write using the DerivativeStructure API, or functions for which user does not have control to the full underlying code (for example functions that call external libraries).

                                    Apache Commons Math provides one implementation of the UnivariateFunctionDifferentiator interface: FiniteDifferencesDifferentiator. This class creates a wrapper that will call the user-provided function on a grid sample and will use finite differences to compute the derivatives. It takes care of boundaries if the variable is not defined on the whole real line. It is possible to use more points than strictly required by the derivation order (for example one can specify an 8-points scheme to compute first derivative only). However, one must be aware that tuning the parameters for finite differences is highly problem-dependent. Choosing the wrong step size or the wrong number of sampling points can lead to huge errors. Finite differences are also not well suited to compute high order derivatives.

                                    Another implementation of the UnivariateFunctionDifferentiator interface is under development in the related project Apache Commons Nabla. This implementation uses automatic code analysis and generation at binary level. However, at time of writing (end 2012), this project is not yet suitable for production use.

                                    commons-math3-3.2-src/src/site/xdoc/userguide/index.xml100644 1750 1750 25333 12126627721 21732 0ustarlucluc 0 0 The Commons Math User Guide - Table of Contents
                                    commons-math3-3.2-src/src/site/xdoc/userguide/utilities.xml100644 1750 1750 25410 12126627721 22632 0ustarlucluc 0 0 The Commons Math User Guide - Utilites

                                    The org.apache.commons.math3.util package collects a group of array utilities, value transformers, and numerical routines used by implementation classes in commons-math.

                                    To maintain statistics based on a "rolling" window of values, a resizable array implementation was developed and is provided for reuse in the util package. The core functionality provided is described in the documentation for the interface, DoubleArray. This interface adds one method, addElementRolling(double) to basic list accessors. The addElementRolling method adds an element (the actual parameter) to the end of the list and removes the first element in the list.

                                    The ResizableDoubleArray class provides a configurable, array-backed implementation of the DoubleArray interface. When addElementRolling is invoked, the underlying array is expanded if necessary, the new element is added to the end of the array and the "usable window" of the array is moved forward, so that the first element is effectively discarded, what was the second becomes the first, and so on. To efficiently manage storage, two maintenance operations need to be periodically performed -- orphaned elements at the beginning of the array need to be reclaimed and space for new elements at the end needs to be created. Both of these operations are handled automatically, with frequency / effect driven by the configuration properties expansionMode, expansionFactor and contractionCriteria. See ResizableDoubleArray for details.

                                    The OpenIntToDoubleHashMap class provides a specialized hash map implementation for int/double. This implementation has a much smaller memory overhead than standard java.util.HashMap class. It uses open addressing and primitive arrays, which greatly reduces the number of intermediate objects and improve data locality.

                                    The ContinuedFraction class provides a generic way to create and evaluate continued fractions. The easiest way to create a continued fraction is to subclass ContinuedFraction and override the getA and getB methods which return the continued fraction terms. The precise definition of these terms is explained in Continued Fraction, equation (1) from MathWorld.

                                    As an example, the constant Pi can be computed using a continued fraction. The following anonymous class provides the implementation: ContinuedFraction c = new ContinuedFraction() { public double getA(int n, double x) { switch(n) { case 0: return 3.0; default: return 6.0; } } public double getB(int n, double x) { double y = (2.0 * n) - 1.0; return y * y; } }

                                    Then, to evalute Pi, simply call any of the evalute methods (Note, the point of evalution in this example is meaningless since Pi is a constant).

                                    For a more practical use of continued fractions, consider the exponential function. The following anonymous class provides its implementation: ContinuedFraction c = new ContinuedFraction() { public double getA(int n, double x) { if (n % 2 == 0) { switch(n) { case 0: return 1.0; default: return 2.0; } } else { return n; } } public double getB(int n, double x) { if (n % 2 == 0) { return -x; } else { return x; } } }

                                    Then, to evalute ex for any value x, simply call any of the evalute methods.

                                    A collection of reusable math functions is provided in the ArithmeticUtils utility class. ArithmeticUtils currently includes methods to compute the following:

                                    • Binomial coefficients -- "n choose k" available as an (exact) long value, binomialCoefficient(int, int) for small n, k; as a double, binomialCoefficientDouble(int, int) for larger values; and in a "super-sized" version, binomialCoefficientLog(int, int) that returns the natural logarithm of the value.
                                    • Stirling numbers of the second kind -- S(n,k) as an exact long value stirlingS2(int, int) for small n, k.
                                    • Factorials -- like binomial coefficients, these are available as exact long values, factorial(int); doubles, factorialDouble(int); or logs, factorialLog(int).
                                    • Least common multiple and greatest common denominator functions.

                                    Apache Commons Math provides a faster, more accurate, portable alternative to the regular Math and StrictMathclasses for large scale computation.

                                    FastMath is a drop-in replacement for both Math and StrictMath. This means that for any method in Math (say Math.sin(x) or Math.cbrt(y)), user can directly change the class and use the methods as is (using FastMath.sin(x) or FastMath.cbrt(y) in the previous example).

                                    FastMath speed is achieved by relying heavily on optimizing compilers to native code present in many JVM todays and use of large tables. Precomputed literal arrays are provided in this class to speed up load time. These precomputed tables are used in the default configuration, to improve speed even at first use of the class. If users prefer to compute the tables automatically at load time, they can change a compile-time constant. This will increase class load time at first use, but this overhead will occur only once per run, regardless of the number of subsequent calls to computation methods. Note that FastMath is extensively used inside Apache Commons Math, so by calling some algorithms, the one-shot overhead when the constant is set to false will occur regardless of the end-user calling FastMath methods directly or not. Performance figures for a specific JVM and hardware can be evaluated by running the FastMathTestPerformance tests in the test directory of the source distribution.

                                    FastMath accuracy should be mostly independent of the JVM as it relies only on IEEE-754 basic operations and on embedded tables. Almost all operations are accurate to about 0.5 ulp throughout the domain range. This statement, of course is only a rough global observed behavior, it is not a guarantee for every double numbers input (see William Kahan's Table Maker's Dilemma).

                                    FastMath additionally implements the following methods not found in Math/StrictMath:

                                    • asinh(double)
                                    • acosh(double)
                                    • atanh(double)
                                    • pow(double,int)
                                    The following methods are found in Math/StrictMath since 1.6 only, they are provided by FastMath even in 1.5 Java virtual machines
                                    • copySign(double, double)
                                    • getExponent(double)
                                    • nextAfter(double,double)
                                    • nextUp(double)
                                    • scalb(double, int)
                                    • copySign(float, float)
                                    • getExponent(float)
                                    • nextAfter(float,double)
                                    • nextUp(float)
                                    • scalb(float, int)

                                    The MultidimensionalCounter is a utility class that converts a set of indices (identifying points in a multidimensional space) to a single index (e.g. identifying a location in a one-dimensional array.
                                    commons-math3-3.2-src/src/site/xdoc/userguide/overview.xml100644 1750 1750 21122 12126627721 22461 0ustarlucluc 0 0 User Guide - Overview

                                    This guide is intended to help programmers quickly find what they need to develop solutions using Commons Math. It also provides a supplement to the javadoc API documentation, providing a little more explanation of the mathematical objects and functions included in the package.

                                    Commons Math is made up of a small set of math/stat utilities addressing programming problems like the ones in the list below. This list is not exhaustive, it's just meant to give a feel for the kinds of things that Commons Math provides.

                                    • Computing means, variances and other summary statistics for a list of numbers
                                    • Fitting a line to a set of data points using linear regression
                                    • Finding a smooth curve that passes through a collection of points (interpolation)
                                    • Fitting a parametric model to a set of measurements using least-squares methods
                                    • Solving equations involving real-valued functions (i.e. root-finding)
                                    • Solving systems of linear equations
                                    • Solving Ordinary Differential Equations
                                    • Minimizing multi-dimensional functions
                                    • Generating random numbers with more restrictions (e.g distribution, range) than what is possible using the JDK
                                    • Generating random samples and/or datasets that are "like" the data in an input file
                                    • Performing statistical significance tests
                                    • Miscellaneous mathematical functions such as factorials, binomial coefficients and "special functions" (e.g. gamma, beta functions)

                                    We are actively seeking ideas for additional components that fit into the Commons Math vision of a set of lightweight, self-contained math/stat components useful for solving common programming problems. Suggestions for new components or enhancements to existing functionality are always welcome! All feedback/suggestions for improvement should be sent to the commons-dev mailing list with [math] at the beginning of the subject line.

                                    Commons Math is divided into fourteen subpackages, based on functionality provided.

                                    1. org.apache.commons.math3.stat - statistics, statistical tests
                                    2. org.apache.commons.math3.analysis - rootfinding, integration, interpolation, polynomials
                                    3. org.apache.commons.math3.random - random numbers, strings and data generation
                                    4. org.apache.commons.math3.special - special functions (Gamma, Beta)
                                    5. org.apache.commons.math3.linear - matrices, solving linear systems
                                    6. org.apache.commons.math3.util - common math/stat functions extending java.lang.Math
                                    7. org.apache.commons.math3.complex - complex numbers
                                    8. org.apache.commons.math3.distribution - probability distributions
                                    9. org.apache.commons.math3.fraction - rational numbers
                                    10. org.apache.commons.math3.transform - transform methods (Fast Fourier)
                                    11. org.apache.commons.math3.geometry - geometry (Euclidean spaces and Binary Space Partitioning)
                                    12. org.apache.commons.math3.optimization - function maximization or minimization
                                    13. org.apache.commons.math3.ode - Ordinary Differential Equations integration
                                    14. org.apache.commons.math3.genetics - Genetic Algorithms
                                    Package javadocs are here

                                    You should always read the javadoc class and method comments carefully when using Commons Math components in your programs. The javadoc provides references to the algorithms that are used, usage notes about limitations, performance, etc. as well as interface contracts. Interface contracts are specified in terms of preconditions (what has to be true in order for the method to return valid results), special values returned (e.g. Double.NaN) or exceptions that may be thrown if the preconditions are not met, and definitions for returned values/objects or state changes.

                                    When the actual parameters provided to a method or the internal state of an object make a computation meaningless, a IllegalArgumentException or MathIllegalStateException may be thrown. Exact conditions under which runtime exceptions (and any other exceptions) are thrown are specified in the javadoc method comments. In some cases, to be consistent with the IEEE 754 standard for floating point arithmetic and with java.lang.Math, Commons Math methods return Double.NaN values. Conditions under which Double.NaN or other special values are returned are fully specified in the javadoc method comments.

                                    As of version 2.2, the policy for dealing with null references is as follows: When an argument is unexpectedly null, a NullArgumentException is raised for signalling the illegal argument. Note that this class does not inherit from the standard NullPointerException but is a subclass of MathIllegalArgumentException. No NullPointerException should be propagated from within Commons Math.

                                    Commons Math requires JDK 1.5+ and has no runtime dependencies.

                                    Commons Math is distributed under the terms of the Apache License, Version 2.0: .

                                    This product includes software developed by the University of Chicago, as Operator of Argonne National Laboratory. This corresponds to software translated from the lmder, lmpar and qrsolv Fortran routines from the Minpack package and distributed under the following disclaimer: .

                                    This product includes software translated from the odex Fortran routine developed by E. Hairer and G. Wanner and distributed under the following license: .

                                    This product includes software translated from some LAPACK Fortran routines and distributed under the following license: .

                                    commons-math3-3.2-src/src/site/xdoc/userguide/filter.xml100644 1750 1750 20167 12126627721 22110 0ustarlucluc 0 0 The Commons Math User Guide - Filters

                                    The filter package currently provides only an implementation of a Kalman filter.

                                    KalmanFilter provides a discrete-time filter to estimate a stochastic linear process.

                                    A Kalman filter is initialized with a ProcessModel and a MeasurementModel, which contain the corresponding transformation and noise covariance matrices. The parameter names used in the respective models correspond to the following names commonly used in the mathematical literature:

                                    • A - state transition matrix
                                    • B - control input matrix
                                    • H - measurement matrix
                                    • Q - process noise covariance matrix
                                    • R - measurement noise covariance matrix
                                    • P - error covariance matrix

                                    Initialization
                                    The following code will create a Kalman filter using the provided DefaultMeasurementModel and DefaultProcessModel classes. To support dynamically changing process and measurement noises, simply implement your own models. // A = [ 1 ] RealMatrix A = new Array2DRowRealMatrix(new double[] { 1d }); // no control input RealMatrix B = null; // H = [ 1 ] RealMatrix H = new Array2DRowRealMatrix(new double[] { 1d }); // Q = [ 0 ] RealMatrix Q = new Array2DRowRealMatrix(new double[] { 0 }); // R = [ 0 ] RealMatrix R = new Array2DRowRealMatrix(new double[] { 0 }); ProcessModel pm = new DefaultProcessModel(A, B, Q, new ArrayRealVector(new double[] { 0 }), null); MeasurementModel mm = new DefaultMeasurementModel(H, R); KalmanFilter filter = new KalmanFilter(pm, mm);
                                    Iteration
                                    The following code illustrates how to perform the predict/correct cycle: for (;;) { // predict the state estimate one time-step ahead // optionally provide some control input filter.predict(); // obtain measurement vector z RealVector z = getMeasurement(); // correct the state estimate with the latest measurement filter.correct(z); double[] stateEstimate = filter.getStateEstimation(); // do something with it }
                                    Constant Voltage Example
                                    The following example creates a Kalman filter for a static process: a system with a constant voltage as internal state. We observe this process with an artificially imposed measurement noise of 0.1V and assume an internal process noise of 1e-5V. double constantVoltage = 10d; double measurementNoise = 0.1d; double processNoise = 1e-5d; // A = [ 1 ] RealMatrix A = new Array2DRowRealMatrix(new double[] { 1d }); // B = null RealMatrix B = null; // H = [ 1 ] RealMatrix H = new Array2DRowRealMatrix(new double[] { 1d }); // x = [ 10 ] RealVector x = new ArrayRealVector(new double[] { constantVoltage }); // Q = [ 1e-5 ] RealMatrix Q = new Array2DRowRealMatrix(new double[] { processNoise }); // P = [ 1 ] RealMatrix P0 = new Array2DRowRealMatrix(new double[] { 1d }); // R = [ 0.1 ] RealMatrix R = new Array2DRowRealMatrix(new double[] { measurementNoise }); ProcessModel pm = new DefaultProcessModel(A, B, Q, x, P0); MeasurementModel mm = new DefaultMeasurementModel(H, R); KalmanFilter filter = new KalmanFilter(pm, mm); // process and measurement noise vectors RealVector pNoise = new ArrayRealVector(1); RealVector mNoise = new ArrayRealVector(1); RandomGenerator rand = new JDKRandomGenerator(); // iterate 60 steps for (int i = 0; i < 60; i++) { filter.predict(); // simulate the process pNoise.setEntry(0, processNoise * rand.nextGaussian()); // x = A * x + p_noise x = A.operate(x).add(pNoise); // simulate the measurement mNoise.setEntry(0, measurementNoise * rand.nextGaussian()); // z = H * x + m_noise RealVector z = H.operate(x).add(mNoise); filter.correct(z); double voltage = filter.getStateEstimation()[0]; }
                                    Increasing Speed Vehicle Example
                                    The following example creates a Kalman filter for a simple linear process: a vehicle driving along a street with a velocity increasing at a constant rate. The process state is modeled as (position, velocity) and we only observe the position. A measurement noise of 10m is imposed on the simulated measurement. // discrete time interval double dt = 0.1d; // position measurement noise (meter) double measurementNoise = 10d; // acceleration noise (meter/sec^2) double accelNoise = 0.2d; // A = [ 1 dt ] // [ 0 1 ] RealMatrix A = new Array2DRowRealMatrix(new double[][] { { 1, dt }, { 0, 1 } }); // B = [ dt^2/2 ] // [ dt ] RealMatrix B = new Array2DRowRealMatrix(new double[][] { { Math.pow(dt, 2d) / 2d }, { dt } }); // H = [ 1 0 ] RealMatrix H = new Array2DRowRealMatrix(new double[][] { { 1d, 0d } }); // x = [ 0 0 ] RealVector x = new ArrayRealVector(new double[] { 0, 0 }); RealMatrix tmp = new Array2DRowRealMatrix(new double[][] { { Math.pow(dt, 4d) / 4d, Math.pow(dt, 3d) / 2d }, { Math.pow(dt, 3d) / 2d, Math.pow(dt, 2d) } }); // Q = [ dt^4/4 dt^3/2 ] // [ dt^3/2 dt^2 ] RealMatrix Q = tmp.scalarMultiply(Math.pow(accelNoise, 2)); // P0 = [ 1 1 ] // [ 1 1 ] RealMatrix P0 = new Array2DRowRealMatrix(new double[][] { { 1, 1 }, { 1, 1 } }); // R = [ measurementNoise^2 ] RealMatrix R = new Array2DRowRealMatrix(new double[] { Math.pow(measurementNoise, 2) }); // constant control input, increase velocity by 0.1 m/s per cycle RealVector u = new ArrayRealVector(new double[] { 0.1d }); ProcessModel pm = new DefaultProcessModel(A, B, Q, x, P0); MeasurementModel mm = new DefaultMeasurementModel(H, R); KalmanFilter filter = new KalmanFilter(pm, mm); RandomGenerator rand = new JDKRandomGenerator(); RealVector tmpPNoise = new ArrayRealVector(new double[] { Math.pow(dt, 2d) / 2d, dt }); RealVector mNoise = new ArrayRealVector(1); // iterate 60 steps for (int i = 0; i < 60; i++) { filter.predict(u); // simulate the process RealVector pNoise = tmpPNoise.mapMultiply(accelNoise * rand.nextGaussian()); // x = A * x + B * u + pNoise x = A.operate(x).add(B.operate(u)).add(pNoise); // simulate the measurement mNoise.setEntry(0, measurementNoise * rand.nextGaussian()); // z = H * x + m_noise RealVector z = H.operate(x).add(mNoise); filter.correct(z); double position = filter.getStateEstimation()[0]; double velocity = filter.getStateEstimation()[1]; }

                                    commons-math3-3.2-src/src/site/xdoc/userguide/genetics.xml100644 1750 1750 15046 12126627721 22424 0ustarlucluc 0 0 The Commons Math User Guide - Genetic Algorithms

                                    The genetics package provides a framework and implementations for genetic algorithms.

                                    GeneticAlgorithm provides an execution framework for Genetic Algorithms (GA). Populations, consisting of Chromosomes are evolved by the GeneticAlgorithm until a StoppingCondition is reached. Evolution is determined by SelectionPolicy, MutationPolicy and Fitness.

                                    The GA itself is implemented by the evolve method of the GeneticAlgorithm class, which looks like this: public Population evolve(Population initial, StoppingCondition condition) { Population current = initial; while (!condition.isSatisfied(current)) { current = nextGeneration(current); } return current; } The nextGeneration method implements the following algorithm:

                                    1. Get nextGeneration population to fill from current generation, using its nextGeneration method
                                    2. Loop until new generation is filled:
                                      • Apply configured SelectionPolicy to select a pair of parents from current
                                      • With probability = getCrossoverRate(), apply configured CrossoverPolicy to parents
                                      • With probability = getMutationRate(), apply configured MutationPolicy to each of the offspring
                                      • Add offspring individually to nextGeneration, space permitting
                                    3. Return nextGeneration

                                    Here is an example GA execution: // initialize a new genetic algorithm GeneticAlgorithm ga = new GeneticAlgorithm( new OnePointCrossover<Integer>(), 1, new RandomKeyMutation(), 0.10, new TournamentSelection(TOURNAMENT_ARITY) ); // initial population Population initial = getInitialPopulation(); // stopping condition StoppingCondition stopCond = new FixedGenerationCount(NUM_GENERATIONS); // run the algorithm Population finalPopulation = ga.evolve(initial, stopCond); // best chromosome from the final population Chromosome bestFinal = finalPopulation.getFittestChromosome(); The arguments to the GeneticAlgorithm constructor above are:
                                    Parametervalue in examplemeaning
                                    crossoverPolicy OnePointCrossover A random crossover point is selected and the first part from each parent is copied to the corresponding child, and the second parts are copied crosswise.
                                    crossoverRate 1 Always apply crossover
                                    mutationPolicy RandomKeyMutation Changes a randomly chosen element of the array representation to a random value uniformly distributed in [0,1].
                                    mutationRate .1 Apply mutation with probability 0.1 - that is, 10% of the time.
                                    selectionPolicy TournamentSelection Each of the two selected chromosomes is selected based on an n-ary tournament -- this is done by drawing n random chromosomes without replacement from the population, and then selecting the fittest chromosome among them.

                                    The algorithm starts with an initial population of Chromosomes. and executes until the specified StoppingCondition is reached. In the example above, a FixedGenerationCount stopping condition is used, which means the algorithm proceeds through a fixed number of generations.

                                    commons-math3-3.2-src/src/site/xdoc/userguide/transform.xml100644 1750 1750 3760 12126627721 22616 0ustarlucluc 0 0 The Commons Math User Guide - Transform methods

                                    This package provides a few transformers for signal analysis. All transformers provide both direct and inverse transforms.

                                    commons-math3-3.2-src/src/site/xdoc/userguide/random.xml100644 1750 1750 70514 12126627721 22104 0ustarlucluc 0 0 The Commons Math User Guide - Data Generation

                                    The Commons Math random package includes utilities for

                                    • generating random numbers
                                    • generating random vectors
                                    • generating random strings
                                    • generating cryptographically secure sequences of random numbers or strings
                                    • generating random samples and permutations
                                    • analyzing distributions of values in an input file and generating values "like" the values in the file
                                    • generating data for grouped frequency distributions or histograms

                                    The source of random data used by the data generation utilities is pluggable. By default, the JDK-supplied PseudoRandom Number Generator (PRNG) is used, but alternative generators can be "plugged in" using an adaptor framework, which provides a generic facility for replacing java.util.Random with an alternative PRNG. Other very good PRNG suitable for Monte-Carlo analysis (but not for cryptography) provided by the library are the Mersenne twister from Makoto Matsumoto and Takuji Nishimura and the more recent WELL generators (Well Equidistributed Long-period Linear) from François Panneton, Pierre L'Ecuyer and Makoto Matsumoto.

                                    Sections 2.2-2.6 below show how to use the commons math API to generate different kinds of random data. The examples all use the default JDK-supplied PRNG. PRNG pluggability is covered in 2.7. The only modification required to the examples to use alternative PRNGs is to replace the argumentless constructor calls with invocations including a RandomGenerator instance as a parameter.

                                    The RandomData interface defines methods for generating random sequences of numbers. The API contracts of these methods use the following concepts:

                                    Random sequence of numbers from a probability distribution
                                    There is no such thing as a single "random number." What can be generated are sequences of numbers that appear to be random. When using the built-in JDK function Math.random(), sequences of values generated follow the Uniform Distribution, which means that the values are evenly spread over the interval between 0 and 1, with no sub-interval having a greater probability of containing generated values than any other interval of the same length. The mathematical concept of a probability distribution basically amounts to asserting that different ranges in the set of possible values of a random variable have different probabilities of containing the value. Commons Math supports generating random sequences from each of the distributions in the distributions package. The javadoc for the nextXxx methods in RandomDataImpl describes the algorithms used to generate random deviates.
                                    Cryptographically secure random sequences
                                    It is possible for a sequence of numbers to appear random, but nonetheless to be predictable based on the algorithm used to generate the sequence. If in addition to randomness, strong unpredictability is required, it is best to use a secure random number generator to generate values (or strings). The nextSecureXxx methods in the RandomDataImpl implementation of the RandomData interface use the JDK SecureRandom PRNG to generate cryptographically secure sequences. The setSecureAlgorithm method allows you to change the underlying PRNG. These methods are much slower than the corresponding "non-secure" versions, so they should only be used when cryptographic security is required. The ISAACRandom class implements a fast cryptographically secure pseudorandom numbers generator.
                                    Seeding pseudo-random number generators
                                    By default, the implementation provided in RandomDataImpl uses the JDK-provided PRNG. Like most other PRNGs, the JDK generator generates sequences of random numbers based on an initial "seed value". For the non-secure methods, starting with the same seed always produces the same sequence of values. Secure sequences started with the same seeds will diverge. When a new RandomDataImpl is created, the underlying random number generators are not initialized. The first call to a data generation method, or to a reSeed() method initializes the appropriate generator. If you do not explicitly seed the generator, it is by default seeded with the current time in milliseconds. Therefore, to generate sequences of random data values, you should always instantiate one RandomDataImpl and use it repeatedly instead of creating new instances for subsequent values in the sequence. For example, the following will generate a random sequence of 50 long integers between 1 and 1,000,000, using the current time in milliseconds as the seed for the JDK PRNG: RandomData randomData = new RandomDataImpl(); for (int i = 0; i < 1000; i++) { value = randomData.nextLong(1, 1000000); } The following will not in general produce a good random sequence, since the PRNG is reseeded each time through the loop with the current time in milliseconds: for (int i = 0; i < 1000; i++) { RandomDataImpl randomData = new RandomDataImpl(); value = randomData.nextLong(1, 1000000); } The following will produce the same random sequence each time it is executed: RandomData randomData = new RandomDataImpl(); randomData.reSeed(1000); for (int i = 0; i = 1000; i++) { value = randomData.nextLong(1, 1000000); } The following will produce a different random sequence each time it is executed. RandomData randomData = new RandomDataImpl(); randomData.reSeedSecure(1000); for (int i = 0; i < 1000; i++) { value = randomData.nextSecureLong(1, 1000000); }

                                    Some algorithms require random vectors instead of random scalars. When the components of these vectors are uncorrelated, they may be generated simply one at a time and packed together in the vector. The UncorrelatedRandomVectorGenerator class simplifies this process by setting the mean and deviation of each component once and generating complete vectors. When the components are correlated however, generating them is much more difficult. The CorrelatedRandomVectorGenerator class provides this service. In this case, the user must set up a complete covariance matrix instead of a simple standard deviations vector. This matrix gathers both the variance and the correlation information of the probability law.

                                    The main use for correlated random vector generation is for Monte-Carlo simulation of physical problems with several variables, for example to generate error vectors to be added to a nominal vector. A particularly common case is when the generated vector should be drawn from a Multivariate Normal Distribution.

                                    Generating random vectors from a bivariate normal distribution
                                    // Create and seed a RandomGenerator (could use any of the generators in the random package here) RandomGenerator rg = new JDKRandomGenerator(); rg.setSeed(17399225432l); // Fixed seed means same results every time // Create a GassianRandomGenerator using rg as its source of randomness GaussianRandomGenerator rawGenerator = new GaussianRandomGenerator(rg); // Create a CorrelatedRandomVectorGenerator using rawGenerator for the components CorrelatedRandomVectorGenerator generator = new CorrelatedRandomVectorGenerator(mean, covariance, 1.0e-12 * covariance.getNorm(), rawGenerator); // Use the generator to generate correlated vectors double[] randomVector = generator.nextVector(); ... The mean argument is a double[] array holding the means of the random vector components. In the bivariate case, it must have length 2. The covariance argument is a RealMatrix, which needs to be 2 x 2. The main diagonal elements are the variances of the vector components and the off-diagonal elements are the covariances. For example, if the means are 1 and 2 respectively, and the desired standard deviations are 3 and 4, respectively, then we need to use double[] mean = {1, 2}; double[][] cov = {{9, c}, {c, 16}}; RealMatrix covariance = MatrixUtils.createRealMatrix(cov); where c is the desired covariance. If you are starting with a desired correlation, you need to translate this to a covariance by multiplying it by the product of the standard deviations. For example, if you want to generate data that will give Pearson's R of 0.5, you would use c = 3 * 4 * .5 = 6.

                                    In addition to multivariate normal distributions, correlated vectors from multivariate uniform distributions can be generated by creating a UniformRandomGenerator in place of the GaussianRandomGenerator above. More generally, any NormalizedRandomGenerator may be used.

                                    The methods nextHexString and nextSecureHexString can be used to generate random strings of hexadecimal characters. Both of these methods produce sequences of strings with good dispersion properties. The difference between the two methods is that the second is cryptographically secure. Specifically, the implementation of nextHexString(n) in RandomDataImpl uses the following simple algorithm to generate a string of n hex digits:

                                    1. n/2+1 binary bytes are generated using the underlying Random
                                    2. Each binary byte is translated into 2 hex digits
                                    The RandomDataImpl implementation of the "secure" version, nextSecureHexString generates hex characters in 40-byte "chunks" using a 3-step process:
                                    1. 20 random bytes are generated using the underlying SecureRandom.
                                    2. SHA-1 hash is applied to yield a 20-byte binary digest.
                                    3. Each byte of the binary digest is converted to 2 hex digits
                                    Similarly to the secure random number generation methods, nextSecureHexString is much slower than the non-secure version. It should be used only for applications such as generating unique session or transaction ids where predictability of subsequent ids based on observation of previous values is a security concern. If all that is needed is an even distribution of hex characters in the generated strings, the non-secure method should be used.

                                    To select a random sample of objects in a collection, you can use the nextSample method in the RandomData interface. Specifically, if c is a collection containing at least k objects, and randomData is a RandomData instance randomData.nextSample(c, k) will return an object[] array of length k consisting of elements randomly selected from the collection. If c contains duplicate references, there may be duplicate references in the returned array; otherwise returned elements will be unique -- i.e., the sampling is without replacement among the object references in the collection.

                                    If randomData is a RandomData instance, and n and k are integers with k <= n, then randomData.nextPermutation(n, k) returns an int[] array of length k whose whose entries are selected randomly, without repetition, from the integers 0 through n-1 (inclusive), i.e., randomData.nextPermutation(n, k) returns a random permutation of n taken k at a time.

                                    Using the ValueServer class, you can generate data based on the values in an input file in one of two ways:

                                    Replay Mode
                                    The following code will read data from url (a java.net.URL instance), cycling through the values in the file in sequence, reopening and starting at the beginning again when all values have been read. ValueServer vs = new ValueServer(); vs.setValuesFileURL(url); vs.setMode(ValueServer.REPLAY_MODE); vs.resetReplayFile(); double value = vs.getNext(); // ...Generate and use more values... vs.closeReplayFile(); The values in the file are not stored in memory, so it does not matter how large the file is, but you do need to explicitly close the file as above. The expected file format is \n -delimited (i.e. one per line) strings representing valid floating point numbers.
                                    Digest Mode
                                    When used in Digest Mode, the ValueServer reads the entire input file and estimates a probability density function based on data from the file. The estimation method is essentially the Variable Kernel Method with Gaussian smoothing. Once the density has been estimated, getNext() returns random values whose probability distribution matches the empirical distribution -- i.e., if you generate a large number of such values, their distribution should "look like" the distribution of the values in the input file. The values are not stored in memory in this case either, so there is no limit to the size of the input file. Here is an example: ValueServer vs = new ValueServer(); vs.setValuesFileURL(url); vs.setMode(ValueServer.DIGEST_MODE); vs.computeDistribution(500); //Read file and estimate distribution using 500 bins double value = vs.getNext(); // ...Generate and use more values... See the javadoc for ValueServer and EmpiricalDistribution for more details. Note that computeDistribution() opens and closes the input file by itself.

                                    To enable alternative PRNGs to be "plugged in" to the commons-math data generation utilities and to provide a generic means to replace java.util.Random in applications, a random generator adaptor framework has been added to commons-math. The RandomGenerator interface abstracts the public interface of java.util.Random and any implementation of this interface can be used as the source of random data for the commons-math data generation classes. An abstract base class, AbstractRandomGenerator is provided to make implementation easier. This class provides default implementations of "derived" data generation methods based on the primitive, nextDouble(). To support generic replacement of java.util.Random, the RandomAdaptor class is provided, which extends java.util.Random and wraps and delegates calls to a RandomGenerator instance.

                                    Commons-math provides by itself several implementations of the RandomGenerator interface:

                                    The JDK provided generator is a simple one that can be used only for very simple needs. The Mersenne Twister is a fast generator with very good properties well suited for Monte-Carlo simulation. It is equidistributed for generating vectors up to dimension 623 and has a huge period: 219937 - 1 (which is a Mersenne prime). This generator is described in a paper by Makoto Matsumoto and Takuji Nishimura in 1998: Mersenne Twister: A 623-Dimensionally Equidistributed Uniform Pseudo-Random Number Generator, ACM Transactions on Modeling and Computer Simulation, Vol. 8, No. 1, January 1998, pp 3--30. The WELL generators are a family of generators with period ranging from 2512 - 1 to 244497 - 1 (this last one is also a Mersenne prime) with even better properties than Mersenne Twister. These generators are described in a paper by François Panneton, Pierre L'Ecuyer and Makoto Matsumoto Improved Long-Period Generators Based on Linear Recurrences Modulo 2 ACM Transactions on Mathematical Software, 32, 1 (2006). The errata for the paper are in wellrng-errata.txt.

                                    For simple sampling, any of these generators is sufficient. For Monte-Carlo simulations the JDK generator does not have any of the good mathematical properties of the other generators, so it should be avoided. The Mersenne twister and WELL generators have equidistribution properties proven according to their bits pool size which is directly linked to their period (all of them have maximal period, i.e. a generator with size n pool has a period 2n-1). They also have equidistribution properties for 32 bits blocks up to s/32 dimension where s is their pool size. So WELL19937c for exemple is equidistributed up to dimension 623 (19937/32). This means a Monte-Carlo simulation generating a vector of n variables at each iteration has some guarantees on the properties of the vector as long as its dimension does not exceed the limit. However, since we use bits from two successive 32 bits generated integers to create one double, this limit is smaller when the variables are of type double. so for Monte-Carlo simulation where less the 16 doubles are generated at each round, WELL1024 may be sufficient. If a larger number of doubles are needed a generator with a larger pool would be useful.

                                    The WELL generators are more modern then MersenneTwister (the paper describing than has been published in 2006 instead of 1998) and fix some of its (few) drawbacks. If initialization array contains many zero bits, MersenneTwister may take a very long time (several hundreds of thousands of iterations to reach a steady state with a balanced number of zero and one in its bits pool). So the WELL generators are better to escape zeroland as explained by the WELL generators creators. The Well19937a and Well44497a generator are not maximally equidistributed (i.e. there are some dimensions or bits blocks size for which they are not equidistributed). The Well512a, Well1024a, Well19937c and Well44497b are maximally equidistributed for blocks size up to 32 bits (they should behave correctly also for double based on more than 32 bits blocks, but equidistribution is not proven at these blocks sizes).

                                    The MersenneTwister generator uses a 624 elements integer array, so it consumes less than 2.5 kilobytes. The WELL generators use 6 integer arrays with a size equal to the pool size, so for example the WELL44497b generator uses about 33 kilobytes. This may be important if a very large number of generator instances were used at the same time.

                                    All generators are quite fast. As an example, here are some comparisons, obtained on a 64 bits JVM on a linux computer with a 2008 processor (AMD phenom Quad 9550 at 2.2 GHz). The generation rate for MersenneTwister was between 25 and 27 millions doubles per second (remember we generate two 32 bits integers for each double). Generation rates for other PRNG, relative to MersenneTwister:

                                    Example of performances
                                    Namegeneration rate (relative to MersenneTwister)
                                    MersenneTwister1
                                    JDKRandomGeneratorbetween 0.96 and 1.16
                                    Well512abetween 0.85 and 0.88
                                    Well1024abetween 0.63 and 0.73
                                    Well19937abetween 0.70 and 0.71
                                    Well19937cbetween 0.57 and 0.71
                                    Well44497abetween 0.69 and 0.71
                                    Well44497bbetween 0.65 and 0.71

                                    So for most simulation problems, the better generators like Well19937c and Well44497b are probably very good choices.

                                    Note that none of these generators are suitable for cryptography. They are devoted to simulation, and to generate very long series with strong properties on the series as a whole (equidistribution, no correlation ...). They do not attempt to create small series but with very strong properties of unpredictability as needed in cryptography.

                                    Examples:

                                    Create a RandomGenerator based on RngPack's Mersenne Twister
                                    To create a RandomGenerator using the RngPack Mersenne Twister PRNG as the source of randomness, extend AbstractRandomGenerator overriding the derived methods that the RngPack implementation provides: import edu.cornell.lassp.houle.RngPack.RanMT; /** * AbstractRandomGenerator based on RngPack RanMT generator. */ public class RngPackGenerator extends AbstractRandomGenerator { private RanMT random = new RanMT(); public void setSeed(long seed) { random = new RanMT(seed); } public double nextDouble() { return random.raw(); } public double nextGaussian() { return random.gaussian(); } public int nextInt(int n) { return random.choose(n); } public boolean nextBoolean() { return random.coin(); } }
                                    Use the Mersenne Twister RandomGenerator in place of java.util.Random in RandomData
                                    RandomData randomData = new RandomDataImpl(new RngPackGenerator());
                                    Create an adaptor instance based on the Mersenne Twister generator that can be used in place of a Random
                                    RandomGenerator generator = new RngPackGenerator(); Random random = RandomAdaptor.createAdaptor(generator); // random can now be used in place of a Random instance, data generation // calls will be delegated to the wrapped Mersenne Twister

                                    commons-math3-3.2-src/src/site/xdoc/userguide/fraction.xml100644 1750 1750 12274 12126627721 22430 0ustarlucluc 0 0 The Commons Math User Guide - Fractions

                                    The fraction packages provides a fraction number type as well as fraction number formatting.

                                    Fraction and BigFraction provide fraction number type that forms the basis for the fraction functionality found in Commons-Math. The former one can be used for fractions whose numerators and denominators are small enough to fit in an int (taking care of intermediate values) while the second class should be used when there is a risk the numerator and denominator grow very large.

                                    A fraction number, can be built from two integer arguments representing numerator and denominator or from a double which will be approximated: Fraction f = new Fraction(1, 3); // 1 / 3 Fraction g = new Fraction(0.25); // 1 / 4

                                    Of special note with fraction construction, when a fraction is created it is always reduced to lowest terms.

                                    The Fraction class provides many unary and binary fraction operations. These operations provide the means to add, subtract, multiple and, divide fractions along with other functions similar to the real number functions found in java.math.BigDecimal: Fraction lhs = new Fraction(1, 3); Fraction rhs = new Fraction(2, 5); Fraction answer = lhs.add(rhs); // add two fractions answer = lhs.subtract(rhs); // subtract two fractions answer = lhs.abs(); // absolute value answer = lhs.reciprocal(); // reciprocal of lhs

                                    Like fraction construction, for each of the fraction functions, the resulting fraction is reduced to lowest terms.

                                    Fraction instances can be converted to and from strings using the FractionFormat class. FractionFormat is a java.text.Format extension and, as such, is used like other formatting objects (e.g. java.text.SimpleDateFormat): FractionFormat format = new FractionFormat(); // default format Fraction f = new Fraction(2, 4); String s = format.format(f); // s contains "1 / 2", note the reduced fraction

                                    To customize the formatting output, one or two java.text.NumberFormat instances can be used to construct a FractionFormat. These number formats control the formatting of the numerator and denominator of the fraction: NumberFormat nf = NumberFormat.getInstance(Locale.FRANCE); // create fraction format with custom number format // when one number format is used, both numerator and // denominator are formatted the same FractionFormat format = new FractionFormat(nf); Fraction f = new Fraction(2000, 3333); String s = format.format(c); // s contains "2.000 / 3.333" NumberFormat nf2 = NumberFormat.getInstance(Locale.US); // create fraction format with custom number formats format = new FractionFormat(nf, nf2); s = format.format(f); // s contains "2.000 / 3,333"

                                    Formatting's inverse operation, parsing, can also be performed by FractionFormat. To parse a fraction from a string, simply call the parse method: FractionFormat ff = new FractionFormat(); Fraction f = ff.parse("-10 / 21");

                                    commons-math3-3.2-src/src/site/xdoc/userguide/ode.xml100644 1750 1750 55622 12126627721 21376 0ustarlucluc 0 0 The Commons Math User Guide - Ordinary Differential Equations Integration

                                    The ode package provides classes to solve Ordinary Differential Equations problems.

                                    This package solves Initial Value Problems of the form y'=f(t,y) with t0 and y(t0)=y0 known. The provided integrators compute an estimate of y(t) from t=t0 to t=t1.

                                    All integrators provide dense output. This means that besides computing the state vector at discrete times, they also provide a cheap mean to get both the state and its derivative between the time steps. They do so through classes extending the StepInterpolator abstract class, which are made available to the user at the end of each step.

                                    All integrators handle multiple discrete events detection based on switching functions. This means that the integrator can be driven by user specified discrete events (occurring when the sign of user-supplied switching function changes). The steps are shortened as needed to ensure the events occur at step boundaries (even if the integrator is a fixed-step integrator). When the events are triggered, integration can be stopped (this is called a G-stop facility), the state vector can be changed, or integration can simply go on. The latter case is useful to handle discontinuities in the differential equations gracefully and get accurate dense output even close to the discontinuity.

                                    All integrators support setting a maximal number of evaluations of differential equations function. If this number is exceeded, an exception will be thrown during integration. This can be used to prevent infinite loops if for example error control or discrete events create a really large number of extremely small steps. By default, the maximal number of evaluation is set to Integer.MAX_VALUE (i.e. 231-1 or 2147483647). It is recommended to set this maximal number to a value suited to the ODE problem, integration range, and step size or error control settings.

                                    The user should describe his problem in his own classes which should implement the FirstOrderDifferentialEquations interface. Then he should pass it to the integrator he prefers among all the classes that implement the FirstOrderIntegrator interface. The following example shows how to implement the simple two-dimensional problem:

                                    • y'0(t) = ω × (c1 - y1(t))
                                    • y'1(t) = ω × (y0(t) - c0)
                                    with some initial state y(t0) = (y0(t0), y1(t0)). In fact, the exact solution of this problem is that y(t) moves along a circle centered at c = (c0, c1) with constant angular rate ω.

                                    private static class CircleODE implements FirstOrderDifferentialEquations { private double[] c; private double omega; public CircleODE(double[] c, double omega) { this.c = c; this.omega = omega; } public int getDimension() { return 2; } public void computeDerivatives(double t, double[] y, double[] yDot) { yDot[0] = omega * (c[1] - y[1]); yDot[1] = omega * (y[0] - c[0]); } }

                                    Computing the state y(16.0) starting from y(0.0) = (0.0, 1.0) and integrating the ODE is done as follows (using Dormand-Prince 8(5,3) integrator as an example):

                                    FirstOrderIntegrator dp853 = new DormandPrince853Integrator(1.0e-8, 100.0, 1.0e-10, 1.0e-10); FirstOrderDifferentialEquations ode = new CircleODE(new double[] { 1.0, 1.0 }, 0.1); double[] y = new double[] { 0.0, 1.0 }; // initial state dp853.integrate(ode, 0.0, y, 16.0, y); // now y contains final state at time t=16.0

                                    The solution of the integration problem is provided by two means. The first one is aimed towards simple use: the state vector at the end of the integration process is copied in the y array of the FirstOrderIntegrator.integrate method, as shown by previous example. The second one should be used when more in-depth information is needed throughout the integration process. The user can register an object implementing the StepHandler interface or a StepNormalizer object wrapping a user-specified object implementing the FixedStepHandler interface into the integrator before calling the FirstOrderIntegrator.integrate method. The user object will be called appropriately during the integration process, allowing the user to process intermediate results. The default step handler does nothing. Considering again the previous example, we want to print the trajectory of the point to check it really is a circle arc. We simply add the following before the call to integrator.integrate:

                                    StepHandler stepHandler = new StepHandler() { public void init(double t0, double[] y0, double t) { } public void handleStep(StepInterpolator interpolator, boolean isLast) { double t = interpolator.getCurrentTime(); double[] y = interpolator.getInterpolatedState(); System.out.println(t + " " + y[0] + " " + y[1]); } }; integrator.addStepHandler(stepHandler);

                                    ContinuousOutputModel is a special-purpose step handler that is able to store all steps and to provide transparent access to any intermediate result once the integration is over. An important feature of this class is that it implements the Serializable interface. This means that a complete continuous model of the integrated function throughout the integration range can be serialized and reused later (if stored into a persistent medium like a file system or a database) or elsewhere (if sent to another application). Only the result of the integration is stored, there is no reference to the integrated problem by itself.

                                    Other default implementations of the StepHandler interface are available for general needs (DummyStepHandler, StepNormalizer) and custom implementations can be developed for specific needs. As an example, if an application is to be completely driven by the integration process, then most of the application code will be run inside a step handler specific to this application.

                                    Some integrators (the simple ones) use fixed steps that are set at creation time. The more efficient integrators use variable steps that are handled internally in order to control the integration error with respect to a specified accuracy (these integrators extend the AdaptiveStepsizeIntegrator abstract class). In this case, the step handler which is called after each successful step shows up the variable stepsize. The StepNormalizer class can be used to convert the variable stepsize into a fixed stepsize that can be handled by classes implementing the FixedStepHandler interface. Adaptive stepsize integrators can automatically compute the initial stepsize by themselves, however the user can specify it if he prefers to retain full control over the integration or if the automatic guess is wrong.

                                    ODE problems are continuous ones. However, sometimes discrete events must be taken into account. The most frequent case is the stop condition of the integrator is not defined by the time t but by a target condition on state y (say y[0] = 1.0 for example).

                                    Discrete events detection is based on switching functions. The user provides a simple g(t, y) function depending on the current time and state. The integrator will monitor the value of the function throughout integration range and will trigger the event when its sign changes. The magnitude of the value is almost irrelevant. For the sake of root finding, it should however be continuous (but not necessarily smooth) at least in the roots vicinity. The steps are shortened as needed to ensure the events occur at step boundaries (even if the integrator is a fixed-step integrator).

                                    When an event is triggered, the event time, current state and an indicator whether the switching function was increasing or decreasing at event time are provided to the user. Several different options are available to him:

                                    • integration can be stopped (this is called a G-stop facility),
                                    • the state vector or the derivatives can be changed,
                                    • or integration can simply go on.

                                    The first case, G-stop, is the most common one. A typical use case is when an ODE must be solved up to some target state is reached, with a known value of the state but an unknown occurrence time. As an example, if we want to monitor a chemical reaction up to some predefined concentration for the first substance, we can use the following switching function setting:

                                    public double g(double t, double[] y) { return y[0] - targetConcentration; } public int eventOccurred(double t, double[] y, boolean increasing) { return STOP; }

                                    The second case, change state vector or derivatives is encountered when dealing with discontinuous dynamical models. A typical case would be the motion of a spacecraft when thrusters are fired for orbital maneuvers. The acceleration is smooth as long as no maneuvers are performed, depending only on gravity, drag, third body attraction, radiation pressure. Firing a thruster introduces a discontinuity that must be handled appropriately by the integrator. In such a case, we would use a switching function setting similar to this:

                                    public double g(double t, double[] y) { return (t - tManeuverStart) * (t - tManeuverStop); } public int eventOccurred(double t, double[] y, boolean increasing) { return RESET_DERIVATIVES; }

                                    The third case is useful mainly for monitoring purposes, a simple example is:

                                    public double g(double t, double[] y) { return y[0] - y[1]; } public int eventOccurred(double t, double[] y, boolean increasing) { logger.log("y0(t) and y1(t) curves cross at t = " + t); return CONTINUE; }

                                    The tables below show the various integrators available for non-stiff problems. Note that the implementations of Adams-Bashforth and Adams-Moulton are adaptive stepsize, not fixed stepsize as is usual for these multi-step integrators. This is due to the fact the implementation relies on the Nordsieck vector representation of the state.

                                    Fixed Step Integrators
                                    NameOrder
                                    Euler1
                                    Midpoint2
                                    Classical Runge-Kutta4
                                    Gill4
                                    3/84

                                    Adaptive Stepsize Integrators
                                    NameIntegration OrderError Estimation Order
                                    Higham and Hall54
                                    Dormand-Prince 5(4)54
                                    Dormand-Prince 8(5,3)85 and 3
                                    Gragg-Bulirsch-Stoervariable (up to 18 by default)variable
                                    Adams-Bashforthvariablevariable
                                    Adams-Moultonvariablevariable

                                    If in addition to state y(t) the user needs to compute the sensitivity of the state to the initial state or some parameter of the ODE, he will use the classes and interfaces from the org.apache.commons.ode.jacobians package instead of the top level ode package. These classes compute the jacobians dy(t)/dy0 and dy(t)/dp where y0 is the initial state and p is some ODE parameter.

                                    The classes and interfaces in this package mimic the behavior of the classes and interfaces of the top level ode package, only adding parameters arrays for the jacobians. The behavior of these classes is to create a compound state vector z containing both the state y(t) and its derivatives dy(t)/dy0 and dy(t)/dp and to set up an extended problem by adding the equations for the jacobians automatically. These extended state and problems are then provided to a classical underlying integrator chosen by user.

                                    This behavior imply there will be a top level integrator knowing about state and jacobians and a low level integrator knowing only about compound state (which may be big). If the user wants to deal with the top level only, he will use the specialized step handler and event handler classes registered at top level. He can also register classical step handlers and event handlers, but in this case will see the big compound state. This state is guaranteed to contain the original state in the first elements, followed by the jacobian with respect to initial state (in row order), followed by the jacobian with respect to parameters (in row order). If for example the original state dimension is 6 and there are 3 parameters, the compound state will be a 60 elements array. The first 6 elements will be the original state, the next 36 elements will be the jacobian with respect to initial state, and the remaining 18 will be the jacobian with respect to parameters. Dealing with low level step handlers and event handlers is cumbersome if one really needs the jacobians in these methods, but it also prevents many data being copied back and forth between state and jacobians on one side and compound state on the other side.

                                    In order to compute dy(t)/dy0 and dy(t/dp for any t, the algorithm needs not only the ODE function f such that y'=f(t,y) but also its local jacobians df(t, y, p)/dy and df(t, y, p)/dp.

                                    If the function f is too complex, the user can simply rely on internal differentiation using finite differences to compute these local jacobians. So rather than the FirstOrderDifferentialEquations interface he will implement the ParameterizedODE interface. Considering again our example where only ω is considered a parameter, we get:

                                    public class BasicCircleODE implements ParameterizedODE { private double[] c; private double omega; public BasicCircleODE(double[] c, double omega) { this.c = c; this.omega = omega; } public int getDimension() { return 2; } public void computeDerivatives(double t, double[] y, double[] yDot) { yDot[0] = omega * (c[1] - y[1]); yDot[1] = omega * (y[0] - c[0]); } public int getParametersDimension() { // we are only interested in the omega parameter return 1; } public void setParameter(int i, double value) { omega = value; } }

                                    This ODE is provided to the specialized integrator with two arrays specifying the step sizes to use for finite differences (one array for derivation with respect to state y, one array for derivation with respect to parameters p):

                                    double[] hY = new double[] { 0.001, 0.001 }; double[] hP = new double[] { 1.0e-6 }; FirstOrderIntegratorWithJacobians integrator = new FirstOrderIntegratorWithJacobians(dp853, ode, hY, hP); integrator.integrate(t0, y0, dy0dp, t, y, dydy0, dydp);

                                    If the function f is simple, the user can simply provide the local jacobians by himself. So rather than the FirstOrderDifferentialEquations interface he will implement the ODEWithJacobians interface. Considering again our example where only ω is considered a parameter, we get:

                                    public class EnhancedCircleODE implements ODEWithJacobians { private double[] c; private double omega; public EnhancedCircleODE(double[] c, double omega) { this.c = c; this.omega = omega; } public int getDimension() { return 2; } public void computeDerivatives(double t, double[] y, double[] yDot) { yDot[0] = omega * (c[1] - y[1]); yDot[1] = omega * (y[0] - c[0]); } public int getParametersDimension() { // we are only interested in the omega parameter return 1; } public void computeJacobians(double t, double[] y, double[] yDot, double[][] dFdY, double[][] dFdP) { dFdY[0][0] = 0; dFdY[0][1] = -omega; dFdY[1][0] = omega; dFdY[1][1] = 0; dFdP[0][0] = 0; dFdP[0][1] = omega; dFdP[0][2] = c[1] - y[1]; dFdP[1][0] = -omega; dFdP[1][1] = 0; dFdP[1][2] = y[0] - c[0]; } }

                                    This ODE is provided to the specialized integrator as is:

                                    FirstOrderIntegratorWithJacobians integrator = new FirstOrderIntegratorWithJacobians(dp853, ode); integrator.integrate(t0, y0, dy0dp, t, y, dydy0, dydp);
                                    commons-math3-3.2-src/src/site/xdoc/userguide/exceptions.xml100644 1750 1750 13047 12126627721 23003 0ustarlucluc 0 0 The Commons Math User Guide - Exceptions
                                    Commons Math defines a set of exceptions in order to convey the precise low-level cause of failure.

                                    Starting from version 3.0, all exceptions generated by the Commons Math code are unchecked (i.e. they inherit from the standard RuntimeException class). The main rationale supporting this design decision is that the exceptions generated in the library are not recoverable: They most of the time result from bad input parameters or some failure due to numerical problems. A thorough discussion of the pros and cons of checked and unchecked exceptions can be read in this post by Bruce Eckel.

                                    The exceptions defined by Commons Math follow the Java standard hierarchies:

                                    In all of the above exception hierarchies, several subclasses can exist, each conveying a specific underlying cause of the problem.

                                    • Localization

                                      The detailed error messages (i.e. the string returned by the getLocalizedMessage method) can be localized. However, besides the American/English default, French is the only language for which a translation resource is available.

                                    • Exception "context"

                                      Every exception generated by Commons Math implements the ExceptionContextProvider interface. A call to the getContext method will return the ExceptionContext instance stored in the exception, which the user can further customize by adding messages and/or any object.

                                    commons-math3-3.2-src/src/site/xdoc/userguide/optimization.xml100644 1750 1750 74035 12126627721 23354 0ustarlucluc 0 0 The Commons Math User Guide - Optimization

                                    The optimization package provides algorithms to optimize (i.e. either minimize or maximize) some objective or cost function. The package is split in several sub-packages dedicated to different kind of functions or algorithms.

                                    • the univariate package handles univariate scalar functions,
                                    • the linear package handles multivariate vector linear functions with linear constraints,
                                    • the direct package handles multivariate scalar functions using direct search methods (i.e. not using derivatives),
                                    • the general package handles multivariate scalar or vector functions using derivatives.
                                    • the fitting package handles curve fitting by univariate real functions

                                    The top level optimization package provides common interfaces for the optimization algorithms provided in sub-packages. The main interfaces defines defines optimizers and convergence checkers. The functions that are optimized by the algorithms provided by this package and its sub-packages are a subset of the one defined in the analysis package, namely the real and vector valued functions. These functions are called objective function here. When the goal is to minimize, the functions are often called cost function, this name is not used in this package.

                                    The type of goal, i.e. minimization or maximization, is defined by the enumerated GoalType which has only two values: MAXIMIZE and MINIMIZE.

                                    Optimizers are the algorithms that will either minimize or maximize, the objective function by changing its input variables set until an optimal set is found. There are only four interfaces defining the common behavior of optimizers, one for each supported type of objective function:

                                    Despite there are only four types of supported optimizers, it is possible to optimize a transform a non-differentiable multivariate vectorial function by converting it to a non-differentiable multivariate real function thanks to the LeastSquaresConverter helper class. The transformed function can be optimized using any implementation of the MultivariateOptimizer interface.

                                    For each of the four types of supported optimizers, there is a special implementation which wraps a classical optimizer in order to add it a multi-start feature. This feature call the underlying optimizer several times in sequence with different starting points and returns the best optimum found or all optima if desired. This is a classical way to prevent being trapped into a local extremum when looking for a global one.

                                    A UnivariateOptimizer is used to find the minimal values of a univariate real-valued function f.

                                    These algorithms usage is very similar to root-finding algorithms usage explained in the analysis package. The main difference is that the solve methods in root finding algorithms is replaced by optimize methods.

                                    This package provides an implementation of George Dantzig's simplex algorithm for solving linear optimization problems with linear equality and inequality constraints.

                                    Direct search methods only use cost function values, they don't need derivatives and don't either try to compute approximation of the derivatives. According to a 1996 paper by Margaret H. Wright (Direct Search Methods: Once Scorned, Now Respectable), they are used when either the computation of the derivative is impossible (noisy functions, unpredictable discontinuities) or difficult (complexity, computation cost). In the first cases, rather than an optimum, a not too bad point is desired. In the latter cases, an optimum is desired but cannot be reasonably found. In all cases direct search methods can be useful.

                                    Simplex-based direct search methods are based on comparison of the cost function values at the vertices of a simplex (which is a set of n+1 points in dimension n) that is updated by the algorithms steps.

                                    The instances can be built either in single-start or in multi-start mode. Multi-start is a traditional way to try to avoid being trapped in a local minimum and miss the global minimum of a function. It can also be used to verify the convergence of an algorithm. In multi-start mode, the minimizesmethod returns the best minimum found after all starts, and the etMinima method can be used to retrieve all minima from all starts (including the one already provided by the minimizes method).

                                    The direct package provides four solvers:

                                    The first two simplex-based methods do not handle simple bounds constraints by themselves. However there are two adapters( MultivariateFunctionMappingAdapter and MultivariateFunctionPenaltyAdapter) that can be used to wrap the user function in such a way the wrapped function is unbounded and can be used with these optimizers, despite the fact the underlying function is still bounded and will be called only with feasible points that fulfill the constraints. Note however that using these adapters are only a poor man solutions to simple bounds optimization constraints. Better solutions are to use an optimizer that directly supports simple bounds. Some caveats of the mapping adapter solution are that

                                    • behavior near the bounds may be numerically unstable as bounds are mapped from infinite values,
                                    • start value is evaluated by the optimizer as an unbounded variable, so it must be converted from bounded to unbounded by user,
                                    • optimum result is evaluated by the optimizer as an unbounded variable, so it must be converted from unbounded to bounded by user,
                                    • convergence values are evaluated by the optimizer as unbounded variables, so there will be scales differences when converted to bounded variables,
                                    • in the case of simplex based solvers, the initial simplex should be set up as delta in unbounded variables.
                                    One caveat of penalty adapter is that if start point or start simplex is outside of the allowed range, only the penalty function is used, and the optimizer may converge without ever entering the allowed range.

                                    The last methods do handle simple bounds constraints directly, so the adapters are not needed with them.

                                    The general package deals with non-linear vectorial optimization problems when the partial derivatives of the objective function are available.

                                    One important class of estimation problems is weighted least squares problems. They basically consist in finding the values for some parameters pk such that a cost function J = sum(wi(mesi - modi)2) is minimized. The various (targeti - modeli(pk)) terms are called residuals. They represent the deviation between a set of target values targeti and theoretical values computed from models modeli depending on free parameters pk. The wi factors are weights. One classical use case is when the target values are experimental observations or measurements.

                                    Solving a least-squares problem is finding the free parameters pk of the theoretical models such that they are close to the target values, i.e. when the residual are small.

                                    Two optimizers are available in the general package, both devoted to least-squares problems. The first one is based on the Gauss-Newton method. The second one is the Levenberg-Marquardt method.

                                    In order to solve a vectorial optimization problem, the user must provide it as an object implementing the DifferentiableMultivariateVectorFunction interface. The object will be provided to the estimate method of the optimizer, along with the target and weight arrays, thus allowing the optimizer to compute the residuals at will. The last parameter to the estimate method is the point from which the optimizer will start its search for the optimal point.

                                    Quadratic Problem Example
                                    We are looking to find the best parameters [a, b, c] for the quadratic function f(x) = a x2 + b x + c. The data set below was generated using [a = 8, b = 10, c = 16]. A random number between zero and one was added to each y value calculated.
                                    X Y
                                    1 34.234064369
                                    2 68.2681162306108
                                    3 118.615899084602
                                    4 184.138197238557
                                    5 266.599877916276
                                    6 364.147735251579
                                    7 478.019226091914
                                    8 608.140949270688
                                    9 754.598868667148
                                    10 916.128818085883

                                    First we need to implement the interface DifferentiableMultivariateVectorFunction. This requires the implementation of the method signatures:

                                    • MultivariateMatrixFunction jacobian()
                                    • double[] value(double[] point)

                                    We'll tackle the implementation of the MultivariateMatrixFunction jacobian() method first. You may wish to familiarize yourself with what a Jacobian Matrix is. In this case the Jacobian is the partial derivative of the function with respect to the parameters a, b and c. These derivatives are computed as follows:

                                    • d(ax2 + bx + c)/da = x2
                                    • d(ax2 + bx + c)/db = x
                                    • d(ax2 + bx + c)/dc = 1

                                    For a quadratic which has three variables the Jacobian Matrix will have three columns, one for each variable, and the number of rows will equal the number of rows in our data set, which in this case is ten. So for example for [a = 1, b = 1, c = 1], the Jacobian Matrix is (excluding the first column which shows the value of x):

                                    x d(ax2 + bx + c)/da d(ax2 + bx + c)/db d(ax2 + bx + c)/dc
                                    1 1 1 1
                                    2 4 2 1
                                    3 9 3 1
                                    4 16 4 1
                                    5 25 5 1
                                    6 36 6 1
                                    7 49 7 1
                                    8 64 8 1
                                    9 81 9 1
                                    10 100 10 1

                                    The implementation of the MultivariateMatrixFunction jacobian() for this problem looks like this (The x parameter is an ArrayList containing the independent values of the data set):

                                    private double[][] jacobian(double[] variables) { double[][] jacobian = new double[x.size()][3]; for (int i = 0; i < jacobian.length; ++i) { jacobian[i][0] = x.get(i) * x.get(i); jacobian[i][1] = x.get(i); jacobian[i][2] = 1.0; } return jacobian; } public MultivariateMatrixFunction jacobian() { return new MultivariateMatrixFunction() { private static final long serialVersionUID = -8673650298627399464L; public double[][] value(double[] point) { return jacobian(point); } }; }

                                    Note that if for some reason the derivative of the objective function with respect to its variables is difficult to obtain, Numerical differentiation can be used.

                                    The implementation of the double[] value(double[] point) method, which returns a double array containing the values the objective function returns per given independent value and the current set of variables or parameters, can be seen below:

                                    public double[] value(double[] variables) { double[] values = new double[x.size()]; for (int i = 0; i < values.length; ++i) { values[i] = (variables[0] * x.get(i) + variables[1]) * x.get(i) + variables[2]; } return values; }

                                    Below is the the class containing all the implementation details (Taken from the Apache Commons Math org.apache.commons.math3.optimization.general.LevenbergMarquardtOptimizerTest):

                                    private static class QuadraticProblem implements DifferentiableMultivariateVectorFunction, Serializable { private static final long serialVersionUID = 7072187082052755854L; private List<Double> x; private List<Double> y; public QuadraticProblem() { x = new ArrayList<Double>(); y = new ArrayList<Double>(); } public void addPoint(double x, double y) { this.x.add(x); this.y.add(y); } public double[] calculateTarget() { double[] target = new double[y.size()]; for (int i = 0; i < y.size(); i++) { target[i] = y.get(i).doubleValue(); } return target; } private double[][] jacobian(double[] variables) { double[][] jacobian = new double[x.size()][3]; for (int i = 0; i < jacobian.length; ++i) { jacobian[i][0] = x.get(i) * x.get(i); jacobian[i][1] = x.get(i); jacobian[i][2] = 1.0; } return jacobian; } public double[] value(double[] variables) { double[] values = new double[x.size()]; for (int i = 0; i < values.length; ++i) { values[i] = (variables[0] * x.get(i) + variables[1]) * x.get(i) + variables[2]; } return values; } public MultivariateMatrixFunction jacobian() { return new MultivariateMatrixFunction() { private static final long serialVersionUID = -8673650298627399464L; public double[][] value(double[] point) { return jacobian(point); } }; } }

                                    The below code shows how to go about using the above class and a LevenbergMarquardtOptimizer instance to produce an optimal set of quadratic curve fitting parameters:

                                    QuadraticProblem problem = new QuadraticProblem(); problem.addPoint(1, 34.234064369); problem.addPoint(2, 68.2681162306); problem.addPoint(3, 118.6158990846); problem.addPoint(4, 184.1381972386); problem.addPoint(5, 266.5998779163); problem.addPoint(6, 364.1477352516); problem.addPoint(7, 478.0192260919); problem.addPoint(8, 608.1409492707); problem.addPoint(9, 754.5988686671); problem.addPoint(10, 916.1288180859); LevenbergMarquardtOptimizer optimizer = new LevenbergMarquardtOptimizer(); final double[] weights = { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }; final double[] initialSolution = {1, 1, 1}; PointVectorValuePair optimum = optimizer.optimize(100, problem, problem.calculateTarget(), weights, initialSolution); final double[] optimalValues = optimum.getPoint(); System.out.println("A: " + optimalValues[0]); System.out.println("B: " + optimalValues[1]); System.out.println("C: " + optimalValues[2]);

                                    If you run the above sample you will see the following printed by the console:

                                    A: 7.998832172372726
                                    B: 10.001841530162448
                                    C: 16.324008168386605
                                    

                                    In addition to least squares solving, the NonLinearConjugateGradientOptimizer class provides a non-linear conjugate gradient algorithm to optimize DifferentiableMultivariateFunction. Both the Fletcher-Reeves and the Polak-Ribière search direction update methods are supported. It is also possible to set up a preconditioner or to change the line-search algorithm of the inner loop if desired (the default one is a Brent solver).

                                    The PowellOptimizer provides an optimization method for non-differentiable functions.

                                    The fitting package deals with curve fitting for univariate real functions. When a univariate real function y = f(x) does depend on some unknown parameters p0, p1 ... pn-1, curve fitting can be used to find these parameters. It does this by fitting the curve so it remains very close to a set of observed points (x0, y0), (x1, y1) ... (xk-1, yk-1). This fitting is done by finding the parameters values that minimizes the objective function sum(yi-f(xi))2. This is really a least squares problem.

                                    For all provided curve fitters, the operating principle is the same. Users must first create an instance of the fitter, then add the observed points and once the complete sample of observed points has been added they must call the fit method which will compute the parameters that best fit the sample. A weight is associated with each observed point, this allows to take into account uncertainty on some points when they come from loosy measurements for example. If no such information exist and all points should be treated the same, it is safe to put 1.0 as the weight for all points.

                                    The CurveFitter class provides curve fitting for general curves. Users must provide their own implementation of the curve template as a class implementing the ParametricUnivariateFunction interface and they must provide the initial guess of the parameters.

                                    The following example shows how to fit data with a polynomial function.

                                    final CurveFitter fitter = new CurveFitter(new LevenbergMarquardtOptimizer()); fitter.addObservedPoint(-1.00, 2.021170021833143); fitter.addObservedPoint(-0.99, 2.221135431136975); fitter.addObservedPoint(-0.98, 2.09985277659314); fitter.addObservedPoint(-0.97, 2.0211192647627025); // ... Lots of lines omitted ... fitter.addObservedPoint( 0.99, -2.4345814727089854); // The degree of the polynomial is deduced from the length of the array containing // the initial guess for the coefficients of the polynomial. final double[] init = { 12.9, -3.4, 2.1 }; // 12.9 - 3.4 x + 2.1 x^2 // Compute optimal coefficients. final double[] best = fitter.fit(new PolynomialFunction.Parametric(), init); // Construct the polynomial that best fits the data. final PolynomialFunction fitted = new PolynomialFunction(best);

                                    The more specialized HarmonicFitter classes requires neither an implementation of the parametric real function nor an initial guess as it is are able to compute them internally.

                                    commons-math3-3.2-src/src/site/xdoc/userguide/linear.xml100644 1750 1750 25322 12126627721 22073 0ustarlucluc 0 0 The Commons Math User Guide - Linear Algebra

                                    Linear algebra support in commons-math provides operations on real matrices (both dense and sparse matrices are supported) and vectors. It features basic operations (addition, subtraction ...) and decomposition algorithms that can be used to solve linear systems either in exact sense and in least squares sense.

                                    The RealMatrix interface represents a matrix with real numbers as entries. The following basic matrix operations are supported:

                                    • Matrix addition, subtraction, multiplication
                                    • Scalar addition and multiplication
                                    • transpose
                                    • Norm and Trace
                                    • Operation on a vector

                                    Example: // Create a real matrix with two rows and three columns, using a factory // method that selects the implementation class for us. double[][] matrixData = { {1d,2d,3d}, {2d,5d,3d}}; RealMatrix m = MatrixUtils.createRealMatrix(matrixData); // One more with three rows, two columns, this time instantiating the // RealMatrix implementation class directly. double[][] matrixData2 = { {1d,2d}, {2d,5d}, {1d, 7d}}; RealMatrix n = new Array2DRowRealMatrix(matrixData2); // Note: The constructor copies the input double[][] array in both cases. // Now multiply m by n RealMatrix p = m.multiply(n); System.out.println(p.getRowDimension()); // 2 System.out.println(p.getColumnDimension()); // 2 // Invert p, using LU decomposition RealMatrix pInverse = new LUDecomposition(p).getSolver().getInverse();

                                    The three main implementations of the interface are Array2DRowRealMatrix and BlockRealMatrix for dense matrices (the second one being more suited to dimensions above 50 or 100) and SparseRealMatrix for sparse matrices.

                                    The RealVector interface represents a vector with real numbers as entries. The following basic matrix operations are supported:

                                    • Vector addition, subtraction
                                    • Element by element multiplication, division
                                    • Scalar addition, subtraction, multiplication, division and power
                                    • Mapping of mathematical functions (cos, sin ...)
                                    • Dot product, outer product
                                    • Distance and norm according to norms L1, L2 and Linf

                                    The RealVectorFormat class handles input/output of vectors in a customizable textual format.

                                    The solve() methods of the DecompositionSolver interface support solving linear systems of equations of the form AX=B, either in linear sense or in least square sense. A RealMatrix instance is used to represent the coefficient matrix of the system. Solving the system is a two phases process: first the coefficient matrix is decomposed in some way and then a solver built from the decomposition solves the system. This allows to compute the decomposition and build the solver only once if several systems have to be solved with the same coefficient matrix.

                                    For example, to solve the linear system

                                               2x + 3y - 2z = 1
                                               -x + 7y + 6x = -2
                                               4x - 3y - 5z = 1
                                              
                                    Start by decomposing the coefficient matrix A (in this case using LU decomposition) and build a solver RealMatrix coefficients = new Array2DRowRealMatrix(new double[][] { { 2, 3, -2 }, { -1, 7, 6 }, { 4, -3, -5 } }, false); DecompositionSolver solver = new LUDecomposition(coefficients).getSolver(); Next create a RealVector array to represent the constant vector B and use solve(RealVector) to solve the system RealVector constants = new ArrayRealVector(new double[] { 1, -2, 1 }, false); RealVector solution = solver.solve(constants); The solution vector will contain values for x (solution.getEntry(0)), y (solution.getEntry(1)), and z (solution.getEntry(2)) that solve the system.

                                    Each type of decomposition has its specific semantics and constraints on the coefficient matrix as shown in the following table. For algorithms that solve AX=B in least squares sense the value returned for X is such that the residual AX-B has minimal norm. If an exact solution exist (i.e. if for some X the residual AX-B is exactly 0), then this exact solution is also the solution in least square sense. This implies that algorithms suited for least squares problems can also be used to solve exact problems, but the reverse is not true.

                                    Decomposition algorithms
                                    Namecoefficients matrixproblem type
                                    LUsquareexact solution only
                                    Choleskysymmetric positive definiteexact solution only
                                    QRanyleast squares solution
                                    eigen decompositionsquareexact solution only
                                    SVDanyleast squares solution

                                    It is possible to use a simple array of double instead of a RealVector. In this case, the solution will be provided also as an array of double.

                                    It is possible to solve multiple systems with the same coefficient matrix in one method call. To do this, create a matrix whose column vectors correspond to the constant vectors for the systems to be solved and use solve(RealMatrix), which returns a matrix with column vectors representing the solutions.

                                    Decomposition algorithms may be used for themselves and not only for linear system solving. This is of prime interest with eigen decomposition and singular value decomposition.

                                    The getEigenvalue(), getEigenvalues(), getEigenVector(), getV(), getD() and getVT() methods of the EigenDecomposition interface support solving eigenproblems of the form AX = lambda X where lambda is a real scalar.

                                    The getSingularValues(), getU(), getS() and getV() methods of the SingularValueDecomposition interface allow to solve singular values problems of the form AXi = lambda Yi where lambda is a real scalar, and where the Xi and Yi vectors form orthogonal bases of their respective vector spaces (which may have different dimensions).

                                    In addition to the real field, matrices and vectors using non-real field elements can be used. The fields already supported by the library are:

                                    commons-math3-3.2-src/src/site/xdoc/userguide/geometry.xml100644 1750 1750 26555 12126627721 22465 0ustarlucluc 0 0 The Commons Math User Guide - Geometry

                                    The geometry package provides classes useful for many physical simulations in Euclidean spaces, like vectors and rotations in 3D, as well as a general implentation of Binary Space Partitioning Trees (BSP trees).

                                    Interval and IntervalsSet represent one dimensional regions. All classical set operations are available for intervals sets: union, intersection, symmetric difference (exclusive or), difference, complement, as well as region predicates (point inside/outside/on boundary, emptiness, other region contained). It is also possible to compute geometrical properties like size, barycenter or boundary size. Intervals sets can be built by constructive geometry (union, intersection ...) or from a boundary representation.

                                    PolygonsSet represent two dimensional regions. All classical set operations are available for polygons sets: union, intersection, symmetric difference (exclusive or), difference, complement, as well as region predicates (point inside/outside/on boundary, emptiness, other region contained). It is also possible to compute geometrical properties like size, barycenter or boundary size and to extract the vertices. Polygons sets can be built by constructive geometry (union, intersection ...) or from a boundary representation.

                                    PolyhedronsSet represent three dimensional regions. All classical set operations are available for polyhedrons sets: union, intersection, symmetric difference (exclusive or), difference, complement, as well as region predicates (point inside/outside/on boundary, emptiness, other region contained). It is also possible to compute geometrical properties like size, barycenter or boundary size and to extract the vertices. Polyhedrons sets can be built by constructive geometry (union, intersection ...) or from a boundary representation.

                                    Vector3D provides a simple vector type. One important feature is that instances of this class are guaranteed to be immutable, this greatly simplifies modelling dynamical systems with changing states: once a vector has been computed, a reference to it is known to preserve its state as long as the reference itself is preserved.

                                    Numerous constructors are available to create vectors. In addition to the straightforward cartesian coordinates constructor, a constructor using azimuthal coordinates can build normalized vectors and linear constructors from one, two, three or four base vectors are also available. Constants have been defined for the most commons vectors (plus and minus canonical axes, null vector, and special vectors with infinite or NaN coordinates).

                                    The generic vectorial space operations are available including dot product, normalization, orthogonal vector finding and angular separation computation which have a specific meaning in 3D. The 3D geometry specific cross product is of course also implemented.

                                    Vector3DFormat is a specialized format for formatting output or parsing input with text representation of 3D vectors.

                                    Rotation represents 3D rotations. Rotation instances are also immutable objects, as Vector3D instances.

                                    Rotations can be represented by several different mathematical entities (matrices, axe and angle, Cardan or Euler angles, quaternions). This class presents a higher level abstraction, more user-oriented and hiding implementation details. Well, for the curious, we use quaternions for the internal representation. The user can build a rotation from any of these representations, and any of these representations can be retrieved from a Rotation instance (see the various constructors and getters). In addition, a rotation can also be built implicitely from a set of vectors and their image.

                                    This implies that this class can be used to convert from one representation to another one. For example, converting a rotation matrix into a set of Cardan angles can be done using the following single line of code:

                                    double[] angles = new Rotation(matrix, 1.0e-10).getAngles(RotationOrder.XYZ);

                                    Focus is oriented on what a rotation does rather than on its underlying representation. Once it has been built, and regardless of its internal representation, a rotation is an operator which basically transforms three dimensional vectors into other three dimensional vectors. Depending on the application, the meaning of these vectors may vary as well as the semantics of the rotation.

                                    For example in a spacecraft attitude simulation tool, users will often consider the vectors are fixed (say the Earth direction for example) and the rotation transforms the coordinates coordinates of this vector in inertial frame into the coordinates of the same vector in satellite frame. In this case, the rotation implicitly defines the relation between the two frames (we have fixed vectors and moving frame). Another example could be a telescope control application, where the rotation would transform the sighting direction at rest into the desired observing direction when the telescope is pointed towards an object of interest. In this case the rotation transforms the direction at rest in a topocentric frame into the sighting direction in the same topocentric frame (we have moving vectors in fixed frame). In many case, both approaches will be combined, in our telescope example, we will probably also need to transform the observing direction in the topocentric frame into the observing direction in inertial frame taking into account the observatory location and the Earth rotation.

                                    These examples show that a rotation means what the user wants it to mean, so this class does not push the user towards one specific definition and hence does not provide methods like projectVectorIntoDestinationFrame or computeTransformedDirection. It provides simpler and more generic methods: applyTo(Vector3D) and applyInverseTo(Vector3D).

                                    Since a rotation is basically a vectorial operator, several rotations can be composed together and the composite operation r = r1 o r2 (which means that for each vector u, r(u) = r1(r2(u))) is also a rotation. Hence we can consider that in addition to vectors, a rotation can be applied to other rotations as well (or to itself). With our previous notations, we would say we can apply r1 to r2 and the result we get is r = r1 o r2. For this purpose, the class provides the methods: applyTo(Rotation) and applyInverseTo(Rotation).

                                    BSP trees are an efficient way to represent space partitions and to associate attributes with each cell. Each node in a BSP tree represents a convex region which is partitioned in two convex sub-regions at each side of a cut hyperplane. The root tree contains the complete space.

                                    The main use of such partitions is to use a boolean attribute to define an inside/outside property, hence representing arbitrary polytopes (line segments in 1D, polygons in 2D and polyhedrons in 3D) and to operate on them.

                                    Another example would be to represent Voronoi tesselations, the attribute of each cell holding the defining point of the cell.

                                    The application-defined attributes are shared among copied instances and propagated to split parts. These attributes are not used by the BSP-tree algorithms themselves, so the application can use them for any purpose. Since the tree visiting method holds internal and leaf nodes differently, it is possible to use different classes for internal nodes attributes and leaf nodes attributes. This should be used with care, though, because if the tree is modified in any way after attributes have been set, some internal nodes may become leaf nodes and some leaf nodes may become internal nodes.

                                    commons-math3-3.2-src/src/site/xdoc/userguide/distribution.xml100644 1750 1750 12444 12126627721 23341 0ustarlucluc 0 0 The Commons Math User Guide - Distributions

                                    The distributions package provide a framework for some commonly used probability distributions.

                                    The distribution framework provides the means to compute probability density function (PDF) probabilities and cumulative distribution function (CDF) probabilities for common probability distributions. Along with the direct computation of PDF and CDF probabilities, the framework also allows for the computation of inverse PDF and inverse CDF values.

                                    Using a distribution object, PDF and CDF probabilities are easily computed using the cumulativeProbability methods. For a distribution X, and a domain value, x, cumulativeProbability computes P(X <= x) (i.e. the lower tail probability of X).

                                    TDistribution t = new TDistribution(29); double lowerTail = t.cumulativeProbability(-2.656); // P(T <= -2.656) double upperTail = 1.0 - t.cumulativeProbability(2.75); // P(T >= 2.75)

                                    The inverse PDF and CDF values are just as easily computed using the inverseCumulativeProbability methods. For a distribution X, and a probability, p, inverseCumulativeProbability computes the domain value x, such that:

                                    • P(X <= x) = p, for continuous distributions
                                    • P(X <= x) <= p, for discrete distributions
                                    Notice the different cases for continuous and discrete distributions. This is the result of PDFs not being invertible functions. As such, for discrete distributions, an exact domain value can not be returned. Only the "best" domain value. For Commons-Math, the "best" domain value is determined by the largest domain value whose cumulative probability is less-than or equal to the given probability.

                                    Since there are numerous distributions and Commons-Math only directly supports a handful, it may be necessary to extend the distribution framework to satisfy individual needs. It is recommended that the Distribution, ContinuousDistribution, DiscreteDistribution, and IntegerDistribution interfaces serve as base types for any extension. These serve as the basis for all the distributions directly supported by Commons-Math and using those interfaces for implementation purposes will ensure any extension is compatible with the remainder of Commons-Math. To aid in implementing a distribution extension, the AbstractDistribution, AbstractContinuousDistribution, and AbstractIntegerDistribution provide implementation building blocks and offer basic distribution functionality. By extending these abstract classes directly, much of the repetitive distribution implementation is already developed and should save time and effort in developing user-defined distributions.

                                    commons-math3-3.2-src/src/site/xdoc/userguide/complex.xml100644 1750 1750 15713 12126627721 22273 0ustarlucluc 0 0 The Commons Math User Guide - Complex Numbers

                                    The complex packages provides a complex number type as well as complex versions of common transcendental functions and complex number formatting.

                                    Complex provides a complex number type that forms the basis for the complex functionality found in commons-math.

                                    Complex functions and arithmetic operations are implemented in commons-math by applying standard computational formulas and following the rules for java.lang.Double arithmetic in handling infinite and NaN values. No attempt is made to comply with ANSII/IEC C99x Annex G or any other standard for Complex arithmetic. See the class and method javadocs for the Complex and ComplexUtils classes for details on computing formulas.

                                    To create a complex number, simply call the constructor passing in two floating-point arguments, the first being the real part of the complex number and the second being the imaginary part: Complex c = new Complex(1.0, 3.0); // 1 + 3i

                                    Complex numbers may also be created from polar representations using the polar2Complex method in ComplexUtils.

                                    The Complex class provides basic unary and binary complex number operations. These operations provide the means to add, subtract, multiply and divide complex numbers along with other complex number functions similar to the real number functions found in java.math.BigDecimal: Complex lhs = new Complex(1.0, 3.0); Complex rhs = new Complex(2.0, 5.0); Complex answer = lhs.add(rhs); // add two complex numbers answer = lhs.subtract(rhs); // subtract two complex numbers answer = lhs.abs(); // absolute value answer = lhs.conjugate(rhs); // complex conjugate

                                    Complex also provides implementations of serveral transcendental functions involving complex number arguments. Prior to version 1.2, these functions were provided by ComplexUtils in a way similar to the real number functions found in java.lang.Math, but this has been deprecated. These operations provide the means to compute the log, sine, tangent, and other complex values : Complex first = new Complex(1.0, 3.0); Complex second = new Complex(2.0, 5.0); Complex answer = first.log(); // natural logarithm. answer = first.cos(); // cosine answer = first.pow(second); // first raised to the power of second

                                    Complex instances can be converted to and from strings using the ComplexFormat class. ComplexFormat is a java.text.Format extension and, as such, is used like other formatting objects (e.g. java.text.SimpleDateFormat): ComplexFormat format = new ComplexFormat(); // default format Complex c = new Complex(1.1111, 2.2222); String s = format.format(c); // s contains "1.11 + 2.22i"

                                    To customize the formatting output, one or two java.text.NumberFormat instances can be used to construct a ComplexFormat. These number formats control the formatting of the real and imaginary values of the complex number: NumberFormat nf = NumberFormat.getInstance(); nf.setMinimumFractionDigits(3); nf.setMaximumFractionDigits(3); // create complex format with custom number format // when one number format is used, both real and // imaginary parts are formatted the same ComplexFormat cf = new ComplexFormat(nf); Complex c = new Complex(1.11, 2.2222); String s = format.format(c); // s contains "1.110 + 2.222i" NumberFormat nf2 = NumberFormat.getInstance(); nf.setMinimumFractionDigits(1); nf.setMaximumFractionDigits(1); // create complex format with custom number formats cf = new ComplexFormat(nf, nf2); s = format.format(c); // s contains "1.110 + 2.2i"

                                    Another formatting customization provided by ComplexFormat is the text used for the imaginary designation. By default, the imaginary notation is "i" but, it can be manipulated using the setImaginaryCharacter method.

                                    Formatting inverse operation, parsing, can also be performed by ComplexFormat. Parse a complex number from a string, simply call the parse method: ComplexFormat cf = new ComplexFormat(); Complex c = cf.parse("1.110 + 2.222i");

                                    commons-math3-3.2-src/src/site/xdoc/userguide/xdoc.xsl100644 1750 1750 4715 12126627721 21547 0ustarlucluc 0 0

                                                    
                                                
                                    commons-math3-3.2-src/src/site/resources/style/project.css100644 1750 1750 1545 12126627721 22461 0ustarlucluc 0 0 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */ @import url("http://commons.apache.org/style/commons-maven.css"); commons-math3-3.2-src/src/site/resources/images/math.gif100644 1750 1750 35576 12126627721 22061 0ustarlucluc 0 0 GIF89a,d.../13 7 :.-*(4 9>>A;D3HDG;DL$QD]y3"M,Z?&I.Vkv'uI4b3I֕2(m{ KB <^ C̫Ng/b#y<doa=aCL0f=EE;=>´ۼVZHSDS4cOrI{b@2.ᏯcˆyMG9 ]H0c@ݸ?&W@X2%|Ll]˓41Baۇ;~#r?t|j#Ơa#\!ui# [?֏C/ILb恏{xÈ=Q(!_8>sUn|mE2T bcG;LB(:kĈ tM[9Gut^!Z= tpm(: \=ޡn#s6h< \۝=aJkpvR<60wibϠ2{4ǁk[)NQzhǘ2w죠 zat(F2 e8#Ϙ=xy1ޡCG:,d8k6gBX1A2yYXaPPG3( c4#<桏w$2F4ޑq ōBQ0#ӈZ1 g0C`Z=!p=y\%`F3!¯cŰdB19zXXna>CG#HQgHe4021hưc+ a, c1) =1ͯE҈Q}jAב ) vtwH2h@c\k[ԑ.cݢ=Vt)5A hDXL2Bˏc@]ۑp3aU^MFN}Cil`HC`pR ")Z!e@K€q 7#=U43CX!Y_~Cՠގ fp3Ɍj),cH!';ߍ|ɶ aK}x5~ьd)C=𪓱brq`;=h\uHt3HʀGj gt tjkD\_G?1j>icY^X֝Qлށ9ev+lD@Z#7`}_pJǽو_4ьOaQv& - :F0F/(0L1v\ò-Ys `lc6<3r4qDe(~c  %6?v', ր ϕP1-"5@{1 WWE4errh6؃? 5i(Ӱ0Q HF`983ДEO)@;_W6q$4W y6Q' Q X-uN 0(" l;UYe@U` 7A2sGw@ Ҁ):}]q¨h9=9@ZЛP0.Au {;@@R[`mN toT%;@@L0 '&R   :`&OSx 7_HkV)A8B퀃 9^Ƀn1)z s'' <ڣ>" r@ T0-P-fP][諿@M?q*0(`5t&\ЇѺj ՠ8 $3P6!g|1z@ W@:Yhp 'G|3r Ű3=D΀T271P Ԁ Ț|?8!Z'@Re`v7 `P | [6'*U`pE7?K{ڙZ\ 5 9p_)@ /0#p' У"5 jG)< `Aiy(8vנy|dK%70H?CP ؐp yuۣ ݰ L5m@`]d}TFkT#it( 'w i)n$9SM ` ( aFzȢx _;Pǘ`z00&VmC}/%'@xw =p0 j#{ t5d$l\9%klnl']\0T0r6Pp7\[ շKGz ưB@P Ƞ!q6&?  %OAx Љ2pjprYr ɐ >hq7q.;p#:'P OuR D +9 0w,3P P6 ɸHSꃕeKT WL@q6YB (3DP ̠0Ҡ_ ] % Ax}(6!wipU=(k D*Ɓ!|xA  Ґ -ۣE@`YGAv0Z%D@a1Ր6@ @T ֠ L$^sh`i6-yC{xT \y\ZYtW D q{Q#m5 ^T6 0BhB;F5 w<#մK* `rAGVX5:1 Ȱgؠ z>] J҄sS A%ռ &q : {h9p40?`M U! D p+l j@ i&8PN1 0B̍ 0a CpYG.V 09r[Y ` 5fPf `  4 ` 0 [Ґ 0-̣I:e!\ǰk<5cRh*C/&Aji ŀ ! XPCP6 A9v w g}<<0o rq'7 >%  ӠhV DqU  W x r :HX<`a  2Twe >̣J ``lĊ%T:UsS@ E@ `0 `uG r)Cd䰊hV\Ѕ`C:@ @UڀA&9 w 0 z \C ymdJ6 w _ YjWa3aeIO>ɡP.X9F:c5E{efifi1df|aQikQfh$Bax}FkFfh`I&ZjFa|A4GT`4sKLJpdFa~#&]jIf@K"h)Xd\&c YaEchFHAbb L("7~ƙcϘifIPcgC`Ttye -BPWgEO`(х snT&d&i<5wgBstFd|ѮƣyUc5e-WŤ>ѿ`lA81 !kP0첼G cܢH%a}͈ˮhE4T|SzƳdC[YI/ę1ьgХ*RЫ 6@cLjoHh*g@g1AD {% 5 #z Ea cΈF-1 -( AEk\U衣0Hȟd$! Ȍ~X;)%_d&¬bR8AJ@#ÆA?jP"8^gT9H\ghVb[s]+a7%tc69!t#r5ߨNL.&=O1Cg3+YB"(0vV\{L^bJt y\V|+#GOP$LDP 5@5Bj4У<<.q,a\MO"iv~H)DZjy@9鬗&ju \3|B3LE*0F@9b&xF8rC4n`(Fi F1 X@9ɑ7Dۍnxc٠V38\ : 0'Bx T$( px٘F3>gEzAvu(kjW =G/S$tHB½q/vCW4-=*p<#Ƨq~ Gd8#qx*M-Z"x78)JD4 (91o\cɘlUst" (QoXղeɛ=; pGYw=zziBc5KnBfeZs)`?Woa9 )#yU @9zq톖ӟㅠH@Qr 2]-Qj*V! ÐW`* P@:̱ ȒE `f{IrFwOtn0&(NCf) TvuC08 j9BVw,^eG+`T AԱB@FQ.ʡn'k?f;1xƧ\n> &p3=#J rc5B`ZG%ڪA-c%$`ᔧB4UtRs:K<菇|MM] Gϫ AS&NY#mK@$^WY* &ruU ?П_IKH 3xq`卂}6ek0Z sF|şOPOU|""H3{7f0 kAu+C>(' ۲WxX xȄëxW;4u\H[)sYpiAGXEX$6*tx>tGZ@KO(V3@23( 7H8HqІ8԰9F "ЂS hfWYpFtF :C B  WS& .`Z@N: mK3vPGXX@T8EPXXXR@Xݔ $Ѓ+ 0QxlWP,0.x? 8`2xt9o.UdA$#_< hI2Rm<0NAJ.p`:PSALIEp[,8E@Ө?qTЃ=UNЃϺ0tp84@Vp30- x#Ѓ/.Q:X0' n  /ÎA@(# fTH?j q(V( >$0#K[PMQ`GHUVc%0 H>ЃLFp.Ƚ` &醁4mՌ%FVjRE_h)lODA%jMK󡃙@9@kU jm@Ix$0-gE#Z%8# px=`ѝ'`=sYpWH}; h E PȰ囀t؆(NCOzL*=O%@hЀ o@(GcF?8k8VPk6XvPl,N Wl0T8)xZT6)YHU0jm1r@o`W`,( ;s9 TȂ3d @rDFa)xDf- t˅Eť؅R0s`De%j؆]`T0Oˀ`9X>2 4 mp؂T8$)(Іl[ep!ς-paU`PcVXC(`bDҔV%`FaUc-F:=0pRAƤ7ރu4VVD$%0orr`VP1Hnp؆Q6W% ^6jQv~ 樀U #Ɗ~)e LuiNa.diLs?` Hki>• *>0(9]pXD̼}EpFp\Xq@Tds5t&ĀXy>ip*c=42e(:cq(!EDpwLx6݅kR0 +y}pux[Eڊ!S1 c^Qzu$iXWJ #^,KKb>8 2>j P20f8j(HP+kOsKH-;X? Hn]h#物nY-6. @NsV!. ^3\s3MS30p7Gp*77%s(j| nQbX=1d8&h! l6VdU2sW115}u>LZ[ICH,}! pK8\ZjuL(Rio&6CqȄMxG`f7aM2 lO@t sE. @<CV"=݄F 7Xҥ77GUwkqlp<*2kpZQ&?X DykS8WwHAm^tsp'9yK>6d~hb]6n)?R2O Ѝ&-0_2lp˟d0kmҔ ʄCH¸``!5ĕ#K+W8dtR@jT6l֩Sc^cݺ @uJ+#4HbiXjHH!:+V@ D ́X0 Eu%g 2aRl2̚7s̐ǐ 0-xP2\G88fp٠'O 6482zm& p&@-!-PpncCZ: 42zH#UREyH04̅ΩTL-:t*F< 47bK/ }2a:egz!!6Fhi8D jl6378SL0dR@8)NU1!SN[!)^^LqG.$5\h'Ю%LJ<1#h-X)f42tGt9 P%P*<@: N6hEM 6Qba,G)(4e{|)F@(PMB Q(@Qx8t"bJ.aGגsOV+7EC; aŅ.VƤ^6K,D؈㖘Q: $jȡ*gOG.4Ë7CK!:;p |qEXpD&a ڀApC"zp0Þ}fy (zl!E_wT.4xQz8sҒҨc, WZ M`<VːU/ D;@ Y*ƀ-40Q+laC큃 ( nxPLk\vl衊Ph–ØM x“@S0WI(<ƀ#ZpTZ,b񊘤`(;0cn"&uN+)W@atO@8 p\0q`I3asV0uP^'EIG/YƃvІ0UI !/Ymq *+ . 4BU$آ6Ib RX [6hB>i0` O , 4 pC%J0'j'd0 l!2@Ldc7mDBv%~9z,P}2i)d! *O, P1!KHؒm! ( HB4Q a ^PxÀ@@( rC*e-g.93-JH%<%0 d@A!yA-d;Ot(a*v%U>AaL#wzћ5X F0~Q 8ġ %MG:bk*0b / `3ԠXЬW1!n@!A 9H~`89r$< =\S6؆8bʐbvQs;Q Ը/+NmW: _@5 p P@v"(F3##x2 hy04op01jtc8G9oHv]HGt Zt2ьi"G.'#@/3^ 0 S5!HYe3ldA l8 6SQ9f.5a,A7dF7.R^`4 c x*#D8all9ȁpTE9zkcC7J;)UVE)a.Y{lbBN^c%{>L+8(SF7Pyh.a nd̘Áq܈D86 };ߌM3o('.iDc@ m\#%huC]f4 nlc0|aXޘ47vgJD=k"`G93 JN&ze|wpx٠F bx՘ƺ &6k0QW4nP᡿ d$ h@C7ğ3 0t2<4L-oN/14XX)(^W:UY sŞ ^8[EɓOeh^1(4T ^@5Húŕ/›_3,/64D6<34`9C4P^ 5D308^1$C3 @921aIC4aBrY%C|dD)343BvR,u1u $RF LW0 RF/ 1b3|"( "3a=",,΢1"DR1 ' 2 11 1 +&C202㑒fJRT)C4) 7 ^ Q$㳙0Ã\2V=c VBaİ9 d0AV#2+#=o#$50JD&CDm$7*!Ev;M(lTFE#KcZ ;Z;eh$H$NdFOv$On$PrdO$).9EC5\6tڠ 4 I)E@bvY9+ګ bjjSeS^+**c1$.l=j,FledNV«fn;commons-math3-3.2-src/src/site/design/solver.puml100644 1750 1750 10017 12126627721 20623 0ustarlucluc 0 0 ' Licensed to the Apache Software Foundation (ASF) under one or more ' contributor license agreements. See the NOTICE file distributed with ' this work for additional information regarding copyright ownership. ' The ASF licenses this file to You 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. @startuml skinparam svek true skinparam ClassBackgroundColor #F3EFEB skinparam ClassArrowColor #691616 skinparam ClassBorderColor #691616 skinparam NoteBackgroundColor #F3EFEB skinparam NoteBorderColor #691616 skinparam NoteFontColor #691616 skinparam ClassFontSize 11 package org.apache.commons.math3.differentiation.solvers #ECEBD8 enum AllowedSolution { ANY_SIDE LEFT_SIDE RIGHT_SIDE BELOW_SIDE ABOVE_SIDE } interface "BaseUnivariateSolver" as BaseUnivariateSolver_FUNC_ { +int getMaxEvaluations() +int getEvaluations() +double getAbsoluteAccuracy() +double getRelativeAccuracy() +double getFunctionValueAccuracy() +double solve(int maxEval, FUNC f, double min, double max) +double solve(int maxEval, FUNC f, double min, double max, double startValue) +double solve(int maxEval, FUNC f, double startValue) } abstract class "BaseAbstractUnivariateSolver" as BaseAbstractUnivariateSolver_FUNC_ { #{Abstract} double doSolve() } interface UnivariateSolver abstract class AbstractUnivariateSolver interface DifferentiableUnivariateSolver abstract class AbstractDifferentiableUnivariateSolver interface PolynomialSolver abstract class AbstractPolynomialSolver BaseUnivariateSolver_FUNC_ <|.. BaseAbstractUnivariateSolver_FUNC_ BaseUnivariateSolver_FUNC_ <|.. UnivariateSolver UnivariateSolver <|.. AbstractUnivariateSolver BaseAbstractUnivariateSolver_FUNC_ <|-- AbstractUnivariateSolver BaseUnivariateSolver_FUNC_ <|.. DifferentiableUnivariateSolver DifferentiableUnivariateSolver <|.. AbstractDifferentiableUnivariateSolver BaseAbstractUnivariateSolver_FUNC_ <|-- AbstractDifferentiableUnivariateSolver BaseUnivariateSolver_FUNC_ <|.. PolynomialSolver PolynomialSolver <|.. AbstractPolynomialSolver BaseAbstractUnivariateSolver_FUNC_ <|-- AbstractPolynomialSolver interface "BracketedUnivariateSolver" as BracketedUnivariateSolver_FUNC_ AllowedSolution <-- BracketedUnivariateSolver_FUNC_ : solution side selector BaseUnivariateSolver_FUNC_ <|.. BracketedUnivariateSolver_FUNC_ abstract class BaseSecantSolver AbstractUnivariateSolver <|-- BaseSecantSolver BracketedUnivariateSolver_FUNC_ <|.. BaseSecantSolver BaseSecantSolver <|-- IllinoisSolver BaseSecantSolver <|-- PegasusSolver BaseSecantSolver <|-- RegulaFalsiSolver AbstractUnivariateSolver <|-- BracketingNthOrderBrentSolver BracketedUnivariateSolver_FUNC_ <|.. BracketingNthOrderBrentSolver AbstractUnivariateSolver <|-- BrentSolver AbstractUnivariateSolver <|-- SecantSolver AbstractUnivariateSolver <|-- RiddersSolver AbstractUnivariateSolver <|-- MullerSolver AbstractUnivariateSolver <|-- MullerSolver2 AbstractDifferentiableUnivariateSolver <|-- NewtonSolver AbstractPolynomialSolver <|-- LaguerreSolver AbstractUnivariateSolver <|-- BisectionSolver end package @enduml commons-math3-3.2-src/src/site/design/threeD.puml100644 1750 1750 4731 12126627721 20512 0ustarlucluc 0 0 ' Licensed to the Apache Software Foundation (ASF) under one or more ' contributor license agreements. See the NOTICE file distributed with ' this work for additional information regarding copyright ownership. ' The ASF licenses this file to You 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. @startuml skinparam svek true skinparam ClassBackgroundColor #F3EFEB skinparam ClassArrowColor #691616 skinparam ClassBorderColor #691616 skinparam NoteBackgroundColor #F3EFEB skinparam NoteBorderColor #691616 skinparam NoteFontColor #691616 skinparam ClassFontSize 11 package org.apache.commons.math3.geometry #ECEBD8 interface Space { +int getDimension() +Space getSubSpace() } note top Space is mainly used as a parameter for generics and to link d-dimensional space with (d-1)-dimensional space end note interface "Vector" as Vector_S_ { +Space getSpace() +Vector getZero() +double getNorm() +Vector add() +Vector subtract() +Vector negate() +Vector normalize() +Vector scalarMultiply() +boolean isNaN() +boolean isInfinite() +double distance() +double dotProduct() } Space <-- Vector_S_ package partitioning #DDEBD8 interface "Region" as Region_S_ interface "Hyperplane" as Hyperplane_S_ interface "SubHyperplane" as SubHyperplane_S_ end package package euclidean #DDEBD8 package threed #DDDBD8 class Euclidean3D class Vector3D class Line class SubLine class Plane class SubPlane class PolyhedronsSet Space <|.. Euclidean3D Vector_S_ <|.. Vector3D Hyperplane_S_ <|.. Plane SubHyperplane_S_ <|.. SubPlane Region_S_ <|.. PolyhedronsSet end package end package end package @enduml commons-math3-3.2-src/src/site/design/twoD.puml100644 1750 1750 4644 12126627721 20217 0ustarlucluc 0 0 ' Licensed to the Apache Software Foundation (ASF) under one or more ' contributor license agreements. See the NOTICE file distributed with ' this work for additional information regarding copyright ownership. ' The ASF licenses this file to You 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. @startuml skinparam svek true skinparam ClassBackgroundColor #F3EFEB skinparam ClassArrowColor #691616 skinparam ClassBorderColor #691616 skinparam NoteBackgroundColor #F3EFEB skinparam NoteBorderColor #691616 skinparam NoteFontColor #691616 skinparam ClassFontSize 11 package org.apache.commons.math3.geometry #ECEBD8 interface Space { +int getDimension() +Space getSubSpace() } note top Space is mainly used as a parameter for generics and to link d-dimensional space with (d-1)-dimensional space end note interface "Vector" as Vector_S_ { +Space getSpace() +Vector getZero() +double getNorm() +Vector add() +Vector subtract() +Vector negate() +Vector normalize() +Vector scalarMultiply() +boolean isNaN() +boolean isInfinite() +double distance() +double dotProduct() } Space <-- Vector_S_ package partitioning #DDEBD8 interface "Region" as Region_S_ interface "Hyperplane" as Hyperplane_S_ interface "SubHyperplane" as SubHyperplane_S_ end package package euclidean #DDEBD8 package twod #DDDBD8 class Euclidean2D class Vector2D class Line class SubLine class PolygonsSet Space <|.. Euclidean2D Vector_S_ <|.. Vector2D Hyperplane_S_ <|.. Line SubHyperplane_S_ <|.. SubLine Region_S_ <|.. PolygonsSet end package end package end package @enduml commons-math3-3.2-src/src/site/design/partitioning.puml100644 1750 1750 7620 12126627721 22006 0ustarlucluc 0 0 ' Licensed to the Apache Software Foundation (ASF) under one or more ' contributor license agreements. See the NOTICE file distributed with ' this work for additional information regarding copyright ownership. ' The ASF licenses this file to You 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. @startuml skinparam svek true skinparam ClassBackgroundColor #F3EFEB skinparam ClassArrowColor #691616 skinparam ClassBorderColor #691616 skinparam NoteBackgroundColor #F3EFEB skinparam NoteBorderColor #691616 skinparam NoteFontColor #691616 skinparam ClassFontSize 11 package org.apache.commons.math3.geometry #ECEBD8 package partitioning #DDEBD8 abstract "AbstractSubHyperplane" as AbstractSubHyperplane_S_T_ note left an abstract sub-hyperplane contains - an hyperplane defined in space S - a region defined in space T T being a sub-space of S end note interface "Hyperplane" as Hyperplane_S_ { +double getOffset(Vector point) +boolean sameOrientationAs(Hyperplane other) +SubHyperplane wholeHyperplane() +Region wholeSpace() } interface "SubHyperplane" as SubHyperplane_S_ { +Hyperplane getHyperplane() +boolean isEmpty() +double getSize() +Side side(Hyperplane hyperplane) +SplitSubHyperplane split(Hyperplane hyperplane) +SubHyperplane reunite(SubHyperplane other) } class "BSPTree" as BSPTree_S_ { +boolean insertCut(Hyperplane hyperplane) +void setAttribute(Object attribute) +Object getAttribute() +void visit(BSPTreeVisitor visitor) +BSPTree getCell(Vector point) +BSPTree split(SubHyperplane sub) } interface "BSPTreeVisitor" as BSPTreeVisitor_S_ { +Order visitOrder(BSPTree node) +void visitInternalNode(BSPTree node) +void visitLeafNode(BSPTree node) } interface "Region" as Region_S_ { +boolean isEmpty() +boolean contains(Region region) +Location checkPoint(Vector point) +double getBoundarySize() +double getSize() +Vector getBarycenter() +Side side(Hyperplane hyperplane) +SubHyperplane intersection(SubHyperplane sub) } note top a region is a part of the space it may be empty, it may cover the whole space, it may have infinite non-closed boudaries, it may be in several disconnected sub-parts, it may contain holes end note enum Location { +INSIDE +OUTSIDE +BOUNDARY } enum Side { +PLUS +MINUS +BOTH +HYPER } Hyperplane_S_ "1" <--* "1" SubHyperplane_S_ SubHyperplane_S_ "0..1" <--* BSPTree_S_ : cut BSPTree_S_ <--o BSPTree_S_ : "parent " BSPTree_S_ o--> BSPTree_S_ : "children" Region_S_ *--> "1" BSPTree_S_ Hyperplane_S_ <-- Region_S_ AbstractSubHyperplane_S_T_ ..|> SubHyperplane_S_ AbstractSubHyperplane_S_T_ *--> "1" Hyperplane_S_ : hyperplane AbstractSubHyperplane_S_T_ *--> "1" Region_S_ : region Region_S_ --> Location Region_S_ --> Side BSPTree_S_ <-- BSPTreeVisitor_S_ : visits end package end package @enduml commons-math3-3.2-src/src/site/design/differentiation.puml100644 1750 1750 7452 12126627721 22454 0ustarlucluc 0 0 ' Licensed to the Apache Software Foundation (ASF) under one or more ' contributor license agreements. See the NOTICE file distributed with ' this work for additional information regarding copyright ownership. ' The ASF licenses this file to You 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. @startuml skinparam svek true skinparam ClassBackgroundColor #F3EFEB skinparam ClassArrowColor #691616 skinparam ClassBorderColor #691616 skinparam NoteBackgroundColor #F3EFEB skinparam NoteBorderColor #691616 skinparam NoteFontColor #691616 skinparam ClassFontSize 11 package org.apache.commons.math3 #ECEBD8 interface "FieldElement" as FieldElement_T_ { T add(T a) T subtract(T a) T negate() T multiply(int n) T multiply(T a) T divide(T a) T reciprocal() Field getField() } package analysis #DDEBD8 interface UnivariateFunction { double value(double x) } package differentiation #DDDBD8 class DerivativeStructure { -DerivativeStructure(DSCompiler compiler) +DerivativeStructure(int parameters, int order, double value) +DerivativeStructure(int parameters, int order, int index, double value) +int getFreeParameters() +int getOrder() +double getValue() +double getPartialDerivative(int[] orders) +double taylor(double[] delta) +int getExponent() +DerivativeStructure compose(double[] f) +DerivativeStructure cos() +DerivativeStructure sin() +DerivativeStructure exp() +DerivativeStructure sqrt() } note bottom lots of mathematical methods and constructors ommitted for clarity end note class DSCompiler { {static} +DSCompiler getCompiler(int parameters, int order) +int getPartialDerivativeIndex(int[] orders) +int[] getPartialDerivativeOrders(int index) +int getFreeParameters() +int getOrder() +int getSize() +void checkCompatibility(DSCompiler compiler) +void compose(double[] operand, int operandOffset, double[] f, double[] result, int resultOffset) +double taylor(double[] ds, int dsOffset, double[] delta) +void add(double[] lhs, int lhsOffset, double[] rhs, int rhsOffset, double[] result, int resultOffset) +void exp(double[] operand, int operandOffset, double[] result, int resultOffset) } note bottom one compiler is built for each pair (number of parameters, derivation order) they are cached for efficiency end note interface UnivariateDifferentiable { DerivativeStructure value(DerivativeStructure t) } interface UnivariateDifferentiator { UnivariateDifferentiable differentiate(UnivariateFunction function) } FieldElement_T_ <.. DerivativeStructure DerivativeStructure o--> "1" DSCompiler : delegates computation UnivariateFunction <|.. UnivariateDifferentiable UnivariateDifferentiable <-- UnivariateDifferentiator : creates UnivariateDifferentiable --> DerivativeStructure : uses end package end package end package @enduml commons-math3-3.2-src/src/site/design/oneD.puml100644 1750 1750 4504 12126627721 20162 0ustarlucluc 0 0 ' Licensed to the Apache Software Foundation (ASF) under one or more ' contributor license agreements. See the NOTICE file distributed with ' this work for additional information regarding copyright ownership. ' The ASF licenses this file to You 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. @startuml skinparam svek true skinparam ClassBackgroundColor #F3EFEB skinparam ClassArrowColor #691616 skinparam ClassBorderColor #691616 skinparam NoteBackgroundColor #F3EFEB skinparam NoteBorderColor #691616 skinparam NoteFontColor #691616 skinparam ClassFontSize 11 package org.apache.commons.math3.geometry #ECEBD8 interface Space { +int getDimension() +Space getSubSpace() } note top Space is mainly used as a parameter for generics and to link d-dimensional space with (d-1)-dimensional space end note interface "Vector" as Vector_S_ { +Space getSpace() +Vector getZero() +double getNorm() +Vector add() +Vector subtract() +Vector negate() +Vector normalize() +Vector scalarMultiply() +boolean isNaN() +boolean isInfinite() +double distance() +double dotProduct() } Space <-- Vector_S_ package partitioning #DDEBD8 interface "Region" as Region_S_ interface "Hyperplane" as Hyperplane_S_ interface "SubHyperplane" as SubHyperplane_S_ end package package euclidean #DDEBD8 package oned #DDDBD8 class Euclidean1D class OrientedPoint class Interval class IntervalSet Space <|.. Euclidean1D Vector_S_ <|.. OrientedPoint Region_S_ <|.. IntervalSet end package end package end package @enduml